aboutsummaryrefslogtreecommitdiff
path: root/Src/external_dependencies/openmpt-trunk/mptrack/ProgressDialog.cpp
blob: 7fc12ce3751cb81f82fb5775f6e1b6a166c1570f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
 * ProgressDialog.cpp
 * ------------------
 * Purpose: An abortable, progress-indicating dialog, e.g. for showing conversion progress.
 * Notes  : (currently none)
 * Authors: OpenMPT Devs
 * The OpenMPT source code is released under the BSD license. Read LICENSE for more details.
 */


#include "stdafx.h"
#include "resource.h"
#include "ProgressDialog.h"
#include "Mptrack.h"

OPENMPT_NAMESPACE_BEGIN

BEGIN_MESSAGE_MAP(CProgressDialog, CDialog)
	ON_COMMAND(IDC_BUTTON1,	&CProgressDialog::Run)
END_MESSAGE_MAP()

CProgressDialog::CProgressDialog(CWnd *parent, UINT resourceID)
	: CDialog{resourceID <= 0 ? IDD_PROGRESS : resourceID, parent}
	, m_customDialog{resourceID > 0}
{ }

CProgressDialog::~CProgressDialog()
{
	if(m_taskBarList)
	{
		m_taskBarList->SetProgressState(*theApp.m_pMainWnd, TBPF_NOPROGRESS);
		m_taskBarList->Release();
	}

	if(IsWindow(m_hWnd))
	{
		// This should only happen if this dialog gets destroyed as part of stack unwinding
		EndDialog(IDCANCEL);
		DestroyWindow();
	}
}

BOOL CProgressDialog::OnInitDialog()
{
	CDialog::OnInitDialog();
	if(!m_customDialog)
		PostMessage(WM_COMMAND, IDC_BUTTON1);
	return TRUE;
}


void CProgressDialog::SetTitle(const TCHAR *title)
{
	SetWindowText(title);
}



void CProgressDialog::SetAbortText(const TCHAR *abort)
{
	SetDlgItemText(IDCANCEL, abort);
}


void CProgressDialog::SetText(const TCHAR *text)
{
	SetDlgItemText(IDC_TEXT1, text);
}


void CProgressDialog::SetRange(uint64 min, uint64 max)
{
	MPT_ASSERT(min <= max);
	m_min = min;
	m_max = max;
	m_shift = 0;
	// Is the range too big for 32-bit values?
	while(max > int32_max)
	{
		m_shift++;
		max >>= 1;
	}
	::SendMessage(::GetDlgItem(m_hWnd, IDC_PROGRESS1), PBM_SETRANGE32, static_cast<uint32>(m_min >> m_shift), static_cast<uint32>(m_max >> m_shift));
}


void CProgressDialog::SetProgress(uint64 progress)
{
	::SendMessage(::GetDlgItem(m_hWnd, IDC_PROGRESS1), PBM_SETPOS, static_cast<uint32>(progress >> m_shift), 0);
	if(m_taskBarList != nullptr)
	{
		m_taskBarList->SetProgressValue(*theApp.m_pMainWnd, progress - m_min, m_max - m_min);
	}
}


void CProgressDialog::EnableTaskbarProgress()
{
	if(CoCreateInstance(CLSID_TaskbarList, nullptr, CLSCTX_ALL, IID_ITaskbarList3, (void**)&m_taskBarList) != S_OK)
	{
		return;
	}
	if(m_taskBarList != nullptr)
	{
		m_taskBarList->SetProgressState(*theApp.m_pMainWnd, TBPF_NORMAL);
	}
}


void CProgressDialog::ProcessMessages()
{
	MSG msg;
	while(::PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
	{
		::TranslateMessage(&msg);
		::DispatchMessage(&msg);
	}
}

OPENMPT_NAMESPACE_END