-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathProgressDlg.cpp
255 lines (184 loc) · 4.76 KB
/
ProgressDlg.cpp
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
// *****************************************************************************
//
// Copyright (c) 2008, Pleora Technologies Inc., All rights reserved.
//
// *****************************************************************************
#include "stdafx.h"
#include "ProgressDlg.h"
#include <PvDevice.h>
#include <PvInterface.h>
IMPLEMENT_DYNAMIC(ProgressDlg, CDialog)
BEGIN_MESSAGE_MAP(ProgressDlg, CDialog)
ON_WM_DESTROY()
ON_WM_TIMER()
END_MESSAGE_MAP()
///
/// \brief Constructor
///
ProgressDlg::ProgressDlg( CWnd* pParent /*=NULL*/ )
: CDialog( ProgressDlg::IDD, pParent )
, mTask( NULL )
, mThreadHandle( 0 )
, mTitle( _T( "Progress Dialog" ) )
{
}
///
/// \brief Destructor
///
ProgressDlg::~ProgressDlg()
{
}
///
/// \brief DoDataExchange
///
void ProgressDlg::DoDataExchange( CDataExchange* pDX )
{
CDialog::DoDataExchange( pDX );
DDX_Control( pDX, IDC_STATUS, mStatusLabel );
}
///
/// \brief OK override: do nothing!
///
void ProgressDlg::OnOK()
{
}
///
/// \brief Cancel override: do nothing!
///
void ProgressDlg::OnCancel()
{
}
///
/// \brief Runs a task
///
int ProgressDlg::RunTask( Task *aTask )
{
mTask = aTask;
mTask->SetProgress( this );
INT_PTR lReturn = DoModal();
mTask->SetProgress( NULL );
mTask = NULL;
return static_cast<int>( lReturn );
}
///
/// \brief Dialog init handler
///
BOOL ProgressDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Load processing wheel bitmap
VERIFY( mWheelBitmap.LoadBitmap( IDB_WHEEL ) );
// Init UI - before the dialog is visible
Update();
// Start task thread (before we start the timer)
DWORD lID = 0;
mThreadHandle = ::CreateThread(
NULL, // Security attributes
0, // Stack size, 0 is default
Link, // Start address
this, // Parameter
0, // Creation flags
&lID ); // Thread ID
// Create timer that will be used to pump status info to the UI
mTimer = SetTimer( 1, 50, NULL );
SetWindowText( mTitle );
return TRUE;
}
///
/// \brief Timer event handler
///
void ProgressDlg::OnTimer( UINT_PTR nIDEvent )
{
if ( nIDEvent == 1 )
{
// Pain DC
CClientDC lClientDC( this );
// Bitmap DC
CDC lBitmapDC;
lBitmapDC.CreateCompatibleDC( &lClientDC );
lBitmapDC.SelectObject( mWheelBitmap );
// Blit the wheel on screen
lClientDC.FillSolidRect(
12, 20 - 1, 16, 16,
::GetSysColor( COLOR_3DFACE ) );
lClientDC.TransparentBlt(
12, 20 - 1, 16, 16,
&lBitmapDC,
mWheelIndex * 16, 0, 16, 16,
RGB( 0xFF, 0xFF, 0xFF ) );
// Advance to next image in sprite
( ++mWheelIndex ) %= 8;
// Update dialog
Update();
// If the task thread is done, complete
if ( ::WaitForSingleObject( mThreadHandle, 0 ) == WAIT_OBJECT_0 )
{
EndDialog( IDOK );
}
}
CDialog::OnTimer( nIDEvent );
}
///
/// \brief On destroy handler
///
void ProgressDlg::OnDestroy()
{
CDialog::OnDestroy();
if ( mTimer != 0 )
{
KillTimer( 1 );
mTimer = 0;
}
if ( mThreadHandle != 0 )
{
::WaitForSingleObject( mThreadHandle, INFINITE );
mThreadHandle = 0;
}
}
///
/// \brief Progress dialog update method. Can be called from UI or task thread.
///
void ProgressDlg::Update()
{
CString lOldStr;
mStatusLabel.GetWindowText( lOldStr );
/////////////////////////////////////////////////////////////////
mMutex.Lock();
if ( lOldStr != mStatus )
{
mStatusLabel.SetWindowText( mStatus );
}
mMutex.Unlock();
/////////////////////////////////////////////////////////////////
}
///
/// \brief Sets the progress status. Can be called from UI or task thread.
///
void ProgressDlg::SetStatus( const std::string &aStatus )
{
/////////////////////////////////////////////////////////////////
mMutex.Lock();
PvString lString( aStatus.c_str() );
mStatus = lString.GetUnicode();
mMutex.Unlock();
/////////////////////////////////////////////////////////////////
// Make sure every step is visible, if only for a little while
::Sleep( 50 );
}
///
/// \brief Shows a warning (status change + delay)
///
void ProgressDlg::ShowWarning( const std::string &aStatus )
{
SetStatus( aStatus );
::Sleep( 3000 );
}
///
/// \brief The thread entry point. Simply runs the task.
///
unsigned long WINAPI ProgressDlg::Link( void *aParam )
{
ProgressDlg *lThis = reinterpret_cast<ProgressDlg *>( aParam );
lThis->mTask->Execute();
return lThis->mTask->GetResult().GetCode();
}