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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
|
/*
* AUTHOR
* N. Nielsen
*
* LICENSE
* This software is in the public domain.
*
* The software is provided "as is", without warranty of any kind,
* express or implied, including but not limited to the warranties
* of merchantability, fitness for a particular purpose, and
* noninfringement. In no event shall the author(s) be liable for any
* claim, damages, or other liability, whether in an action of
* contract, tort, or otherwise, arising from, out of, or in connection
* with the software or the use or other dealings in the software.
*
* SUPPORT
* Send bug reports to: <nielsen@memberwebs.com>
*/
// ProgressDlg.cpp : Implementation of CProgressDlg
#include "stdafx.h"
#include "ProgressDlg.h"
// (Con|De)struction: -----------------------------------------------
ProgressDlg::ProgressDlg()
{
m_numFiles = 0;
m_numReplaces = 0;
m_flip = false;
m_hThread = NULL;
m_hEvent = NULL;
m_isCancelled = false;
InitializeCriticalSection(&m_sec);
}
ProgressDlg::~ProgressDlg()
{
DeleteCriticalSection(&m_sec);
if(m_hEvent)
CloseHandle(m_hEvent);
}
// threadProc: ---------------------------------------------------------
// Runs the dialog in another thread
static DWORD WINAPI threadProc(void* pv)
{
ProgressDlg* dlg = (ProgressDlg*)pv;
return dlg->DoModal(NULL, NULL);
}
// startDialog: --------------------------------------------------------
// Starts the dialog thread
HRESULT ProgressDlg::startDialog()
{
ASSERT(m_hThread == NULL);
HRESULT ret = S_OK;
if(m_hEvent)
CloseHandle(m_hEvent);
m_hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
DWORD threadID;
m_hThread = CreateThread(NULL, 0, threadProc, this, CREATE_SUSPENDED, &threadID);
if(!m_hThread)
ret = HRESULT_FROM_WIN32(::GetLastError());
else
ResumeThread(m_hThread);
// Wait for dialog to start up.
::WaitForSingleObject(m_hEvent, 10000);
CloseHandle(m_hEvent);
m_hEvent = NULL;
return S_OK;
}
// stopDialog: ----------------------------------------------------------
// Stops the dialog thread and closes the dialog
int ProgressDlg::stopDialog()
{
int ret = 0;
if(m_hThread && m_hWnd)
{
::PostMessage(m_hWnd, WM_CLOSE, 0, 0);
WaitForSingleObject(m_hThread, INFINITE);
}
else if(m_hThread)
{
EnterCriticalSection(&m_sec);
TerminateThread(m_hThread, -1);
}
if(m_hThread)
{
DWORD code = 0;
GetExitCodeThread(m_hThread, &code);
CloseHandle(m_hThread);
m_hThread = NULL;
ret = (int)code;
}
return 0;
}
// isCancelled: -----------------------------------------------------------
// Checks if user pressed cancel button
bool ProgressDlg::isCancelled()
{
bool ret = false;
EnterCriticalSection(&m_sec);
ret = m_isCancelled;
LeaveCriticalSection(&m_sec);
return ret;
}
// updateControls: --------------------------------------------------------
// Update dialog status
void ProgressDlg::updateControls()
{
if(IsWindow())
{
string status;
status.format(_T("%d replaces in %d files"), m_numReplaces, m_numFiles);
SetDlgItemText(IDC_STATUS, status);
}
}
// onReplaced: -------------------------------------------------------------
// Called when a new replacement was made
void ProgressDlg::onReplaced()
{
m_flip = !m_flip;
m_numReplaces++;
// Do flicker thing
if(IsWindow())
::ShowWindow(GetDlgItem(IDC_FLIP), m_flip ? SW_SHOW : SW_HIDE);
updateControls();
}
// onNewFile: --------------------------------------------------------------
// Called when a new file starts processing
void ProgressDlg::onNewFile(LPCTSTR fileName)
{
if(IsWindow())
SetDlgItemText(IDC_FILENAME, fileName);
m_numFiles++;
updateControls();
}
// setTitle: ---------------------------------------------------------------
// Set the title. If no window then just store it
void ProgressDlg::setTitle(const string& title)
{
m_title = title;
if(IsWindow())
SetWindowText(title);
}
// onInitDialog: ------------------------------------------------------------
// Dialog initialization. We clear the event that says we've
// started up.
LRESULT ProgressDlg::onInitDialog(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
EnterCriticalSection(&m_sec);
if(!m_title.empty())
SetWindowText(m_title.c_str());
if(m_hEvent)
SetEvent(m_hEvent);
CenterWindow(NULL);
LeaveCriticalSection(&m_sec);
return 1;
}
// onClose: ------------------------------------------------------------------
// Dialog was closed
LRESULT ProgressDlg::onClose(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
EnterCriticalSection(&m_sec);
m_isCancelled = true;
LeaveCriticalSection(&m_sec);
// TODO: will this get called by NC area?
EndDialog(IDOK);
return 1;
}
// onCancel: ------------------------------------------------------------------
// User clicked cancel, set flag
LRESULT ProgressDlg::onCancel(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
EnterCriticalSection(&m_sec);
m_isCancelled = true;
LeaveCriticalSection(&m_sec);
EndDialog(IDCANCEL);
return 0;
}
|