-
Notifications
You must be signed in to change notification settings - Fork 1
/
progressdlg.c
278 lines (253 loc) · 7.14 KB
/
progressdlg.c
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
//----------------------------------------------------------------------------
// EnsoniqFS plugin for TotalCommander
//
// PROGRESS DIALOG
//----------------------------------------------------------------------------
//
// (c) 2006 Thoralt Franz
//
// This source code was written using Dev-Cpp 4.9.9.2
// If you want to compile it, get Dev-Cpp. Normally the code should compile
// with other IDEs/compilers too (with small modifications), but I did not
// test it.
//
//----------------------------------------------------------------------------
// License
//----------------------------------------------------------------------------
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
// MA 02110-1301, USA.
//
// Alternatively, download a copy of the license here:
// http://www.gnu.org/licenses/gpl.txt
//----------------------------------------------------------------------------
#include <stdio.h>
#include <windows.h>
#include <process.h>
#include <commctrl.h>
#include "progressdlg.h"
#include "log.h"
#include "error.h"
#include "resource.h"
//----------------------------------------------------------------------------
// global flags and variables
//----------------------------------------------------------------------------
BOOL m_bProgressDone = FALSE;
HWND m_hProgressWnd = NULL;
extern HINSTANCE g_hInst;
//----------------------------------------------------------------------------
// ProgressDialogThread
//
// Main thread for progress window
//
// -> dummy = unused pointer
// <- --
//----------------------------------------------------------------------------
void ProgressDialogThread(void *arg)
{
HWND hWnd;
MSG msg;
int *pFlag = (int*)arg;
LOG("\nProgressDialogThread() started. ");
// create the dialog window
LOG("Creating progress window: ");
// m_hProgressWnd = CreateDialog(g_hInst, MAKEINTRESOURCE(IDD_DLG_PROGRESS), TC_HWND, 0);
m_hProgressWnd = CreateDialog(g_hInst, MAKEINTRESOURCE(IDD_DLG_PROGRESS), 0, 0);
LOG("OK\n");
hWnd = m_hProgressWnd;
if(hWnd!=NULL)
{
// show dialog
LOG("ShowWindow(): ");
ShowWindow(hWnd, SW_SHOW);
LOG("OK. ");
}
else
{
LOG("CreateDialog() failed.\n");
m_bProgressDone = TRUE;
*pFlag = 1;
return;
}
LOG("Entering message loop.\n");
// message loop
while(1)
{
*pFlag = 1;
// get next message
if(GetMessage(&msg, hWnd, 0, 0))
{
// abort?
if(msg.message==WM_USER+1)
{
LOG("ProgressDialogThread() stop: ");
DestroyWindow(hWnd);
m_bProgressDone = TRUE;
LOG("OK.\n");
break;
}
/* This is only for referencs (from Dev-Cpp example)
if(msg.message==WM_KEYUP)
{
int nVirtKey = (int)msg.wParam;
// if the user pressed the ESCAPE key, then
// print the text the user entered and quit
if(nVirtKey==VK_ESCAPE)
{
// get the edit control
HWND hEdit = GetDlgItem(hWnd, IDC_EDIT);
if(hEdit)
{
// get the input text the user entered
// and print it to the console window
char pText[3201];
int nSize = ::GetWindowText(hEdit, pText, 3200);
pText[nSize] = 0;
printf("\nYou have entered the ");
printf("following text in a second ");
printf("thread:\n\n%s\n\n",pText);
}
else
{
printf("Failed to get edit control\n");
}
// destroy the dialog and get out of the message loop
DestroyWindow(hWnd);
m_bProgressDone = TRUE;
break;
}
}
*/
// process message
TranslateMessage(&msg);
DispatchMessage(&msg);
}
else
{
m_bProgressDone = TRUE;
break;
}
}
}
//----------------------------------------------------------------------------
// UpdateProgressDialog
//
// Display new text and new progress bar position
//
// -> cText = text to display
// iProgress = progress bar position (0...100)
// <- --
//----------------------------------------------------------------------------
void UpdateProgressDialog(char *cText, int iProgress)
{
// check handle
if(NULL==m_hProgressWnd)
{
LOG("UpdateProgressDialog(): Warning: m_hProgressWnd==NULL\n");
return;
}
// update text
SendMessage(GetDlgItem(m_hProgressWnd, IDC_STATIC_STATUS), (UINT)WM_SETTEXT,
(WPARAM)0, (LPARAM) cText);
// update progress bar
SendMessage(GetDlgItem(m_hProgressWnd, IDC_PGB), (UINT)PBM_SETPOS,
(WPARAM)iProgress, 0);
Sleep(10);
}
//----------------------------------------------------------------------------
// CreateProgressDialog
//
// Create a new thread which creates a progress dialog
//
// -> --
// <- ERR_OK
// ERR_CREATE_DLG
//----------------------------------------------------------------------------
int CreateProgressDialog()
{
int iFlag = 0;
LOG("CreateProgressDialog(): ");
// check handle
if(NULL!=m_hProgressWnd)
{
LOG("Warning: Dialog was already created.\n");
return ERR_CREATE_DLG;
}
// create thread
if(-1==(int)_beginthread(ProgressDialogThread, 0, (void*)&iFlag))
{
LOG("Failed to create progress dialog thread.\n");
return ERR_CREATE_DLG;
}
LOG("OK.\nWaiting for thread to enter message loop... ");
while(!iFlag) Sleep(100);
LOG("Progress dialog ready.\n");
return ERR_OK;
}
//----------------------------------------------------------------------------
// GetProgressDialogHwnd
//
// Reads the window handle of the dialog
//
// -> --
// <- window handle or NULL
//----------------------------------------------------------------------------
HWND GetProgressDialogHwnd(void)
{
return m_hProgressWnd;
}
//----------------------------------------------------------------------------
// DestroyProgressDialog
//
// Destroy the progress dialog
//
// -> --
// <- ERR_OK
// ERR_DESTROY_DLG
//----------------------------------------------------------------------------
int DestroyProgressDialog()
{
int iCount = 0;
DWORD dwError;
LOG("DestroyProgressDialog(): ");
// check handle
if(NULL==m_hProgressWnd)
{
LOG("Warning: m_hProgressWnd==NULL\n");
return ERR_OK;
}
// post message to thread
if(0==PostMessage(m_hProgressWnd, WM_USER+1, 0, 0))
{
dwError = GetLastError();
LOG("SendMessage() failed: "); LOG_ERR(dwError);
}
// wait for thread to end
while((FALSE==m_bProgressDone)&&(iCount<5))
{
Sleep(100);
iCount++;
}
m_hProgressWnd = NULL;
if(FALSE==m_bProgressDone)
{
LOG("failed (timeout).\n");
return ERR_DESTROY_DLG;
}
else
{
LOG("OK.\n");
return ERR_OK;
}
}