1
0

ProgressDialog.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * ProgressDialog.cpp
  3. * ------------------
  4. * Purpose: An abortable, progress-indicating dialog, e.g. for showing conversion progress.
  5. * Notes : (currently none)
  6. * Authors: OpenMPT Devs
  7. * The OpenMPT source code is released under the BSD license. Read LICENSE for more details.
  8. */
  9. #include "stdafx.h"
  10. #include "resource.h"
  11. #include "ProgressDialog.h"
  12. #include "Mptrack.h"
  13. OPENMPT_NAMESPACE_BEGIN
  14. BEGIN_MESSAGE_MAP(CProgressDialog, CDialog)
  15. ON_COMMAND(IDC_BUTTON1, &CProgressDialog::Run)
  16. END_MESSAGE_MAP()
  17. CProgressDialog::CProgressDialog(CWnd *parent, UINT resourceID)
  18. : CDialog{resourceID <= 0 ? IDD_PROGRESS : resourceID, parent}
  19. , m_customDialog{resourceID > 0}
  20. { }
  21. CProgressDialog::~CProgressDialog()
  22. {
  23. if(m_taskBarList)
  24. {
  25. m_taskBarList->SetProgressState(*theApp.m_pMainWnd, TBPF_NOPROGRESS);
  26. m_taskBarList->Release();
  27. }
  28. if(IsWindow(m_hWnd))
  29. {
  30. // This should only happen if this dialog gets destroyed as part of stack unwinding
  31. EndDialog(IDCANCEL);
  32. DestroyWindow();
  33. }
  34. }
  35. BOOL CProgressDialog::OnInitDialog()
  36. {
  37. CDialog::OnInitDialog();
  38. if(!m_customDialog)
  39. PostMessage(WM_COMMAND, IDC_BUTTON1);
  40. return TRUE;
  41. }
  42. void CProgressDialog::SetTitle(const TCHAR *title)
  43. {
  44. SetWindowText(title);
  45. }
  46. void CProgressDialog::SetAbortText(const TCHAR *abort)
  47. {
  48. SetDlgItemText(IDCANCEL, abort);
  49. }
  50. void CProgressDialog::SetText(const TCHAR *text)
  51. {
  52. SetDlgItemText(IDC_TEXT1, text);
  53. }
  54. void CProgressDialog::SetRange(uint64 min, uint64 max)
  55. {
  56. MPT_ASSERT(min <= max);
  57. m_min = min;
  58. m_max = max;
  59. m_shift = 0;
  60. // Is the range too big for 32-bit values?
  61. while(max > int32_max)
  62. {
  63. m_shift++;
  64. max >>= 1;
  65. }
  66. ::SendMessage(::GetDlgItem(m_hWnd, IDC_PROGRESS1), PBM_SETRANGE32, static_cast<uint32>(m_min >> m_shift), static_cast<uint32>(m_max >> m_shift));
  67. }
  68. void CProgressDialog::SetProgress(uint64 progress)
  69. {
  70. ::SendMessage(::GetDlgItem(m_hWnd, IDC_PROGRESS1), PBM_SETPOS, static_cast<uint32>(progress >> m_shift), 0);
  71. if(m_taskBarList != nullptr)
  72. {
  73. m_taskBarList->SetProgressValue(*theApp.m_pMainWnd, progress - m_min, m_max - m_min);
  74. }
  75. }
  76. void CProgressDialog::EnableTaskbarProgress()
  77. {
  78. if(CoCreateInstance(CLSID_TaskbarList, nullptr, CLSCTX_ALL, IID_ITaskbarList3, (void**)&m_taskBarList) != S_OK)
  79. {
  80. return;
  81. }
  82. if(m_taskBarList != nullptr)
  83. {
  84. m_taskBarList->SetProgressState(*theApp.m_pMainWnd, TBPF_NORMAL);
  85. }
  86. }
  87. void CProgressDialog::ProcessMessages()
  88. {
  89. MSG msg;
  90. while(::PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
  91. {
  92. ::TranslateMessage(&msg);
  93. ::DispatchMessage(&msg);
  94. }
  95. }
  96. OPENMPT_NAMESPACE_END