]>
Commit | Line | Data |
---|---|---|
8fa2e6a2 | 1 | ///////////////////////////////////////////////////////////////////////////// |
9eddec69 | 2 | // Name: src/generic/progdlgg.cpp |
8fa2e6a2 | 3 | // Purpose: wxProgressDialog class |
9d55bfef | 4 | // Author: Karsten Ballueder |
8fa2e6a2 KB |
5 | // Modified by: |
6 | // Created: 09.05.1999 | |
7 | // RCS-ID: $Id$ | |
9d55bfef | 8 | // Copyright: (c) Karsten Ballueder |
65571936 | 9 | // Licence: wxWindows licence |
8fa2e6a2 KB |
10 | ///////////////////////////////////////////////////////////////////////////// |
11 | ||
0655ad29 VZ |
12 | // ============================================================================ |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
8fa2e6a2 KB |
20 | // For compilers that support precompilation, includes "wx.h". |
21 | #include "wx/wxprec.h" | |
22 | ||
23 | #ifdef __BORLANDC__ | |
f81a6620 | 24 | #pragma hdrstop |
8fa2e6a2 KB |
25 | #endif |
26 | ||
0655ad29 VZ |
27 | #if wxUSE_PROGRESSDLG |
28 | ||
8fa2e6a2 | 29 | #ifndef WX_PRECOMP |
f81a6620 VZ |
30 | #include "wx/utils.h" |
31 | #include "wx/frame.h" | |
32 | #include "wx/button.h" | |
33 | #include "wx/stattext.h" | |
0ca3b64f | 34 | #include "wx/sizer.h" |
f81a6620 VZ |
35 | #include "wx/event.h" |
36 | #include "wx/gauge.h" | |
37 | #include "wx/intl.h" | |
720dcba6 | 38 | #include "wx/dcclient.h" |
9b61f868 | 39 | #include "wx/timer.h" |
9eddec69 | 40 | #include "wx/settings.h" |
731bcffc | 41 | #include "wx/app.h" |
8fa2e6a2 KB |
42 | #endif |
43 | ||
fe8635a7 | 44 | #include "wx/progdlg.h" |
dde19c21 | 45 | #include "wx/evtloop.h" |
8fa2e6a2 | 46 | |
d2cdad17 WS |
47 | // --------------------------------------------------------------------------- |
48 | // macros | |
49 | // --------------------------------------------------------------------------- | |
50 | ||
51 | /* Macro for avoiding #ifdefs when value have to be different depending on size of | |
9a357011 | 52 | device we display on - take it from something like wxDesktopPolicy in the future |
d2cdad17 WS |
53 | */ |
54 | ||
55 | #if defined(__SMARTPHONE__) | |
56 | #define wxLARGESMALL(large,small) small | |
57 | #else | |
58 | #define wxLARGESMALL(large,small) large | |
59 | #endif | |
60 | ||
0655ad29 VZ |
61 | // ---------------------------------------------------------------------------- |
62 | // constants | |
63 | // ---------------------------------------------------------------------------- | |
64 | ||
d2cdad17 | 65 | #define LAYOUT_MARGIN wxLARGESMALL(8,2) |
8fa2e6a2 | 66 | |
ecda9475 WS |
67 | static const int wxID_SKIP = 32000; // whatever |
68 | ||
0655ad29 VZ |
69 | // ---------------------------------------------------------------------------- |
70 | // private functions | |
71 | // ---------------------------------------------------------------------------- | |
72 | ||
73 | // update the label to show the given time (in seconds) | |
74 | static void SetTimeLabel(unsigned long val, wxStaticText *label); | |
75 | ||
76 | // ---------------------------------------------------------------------------- | |
77 | // event tables | |
78 | // ---------------------------------------------------------------------------- | |
8fa2e6a2 | 79 | |
1b6452df | 80 | BEGIN_EVENT_TABLE(wxProgressDialog, wxDialog) |
ef8698d6 | 81 | EVT_BUTTON(wxID_CANCEL, wxProgressDialog::OnCancel) |
ecda9475 | 82 | EVT_BUTTON(wxID_SKIP, wxProgressDialog::OnSkip) |
ef8698d6 | 83 | |
ef8698d6 | 84 | EVT_CLOSE(wxProgressDialog::OnClose) |
1b6452df | 85 | END_EVENT_TABLE() |
8fa2e6a2 | 86 | |
1b6452df | 87 | IMPLEMENT_CLASS(wxProgressDialog, wxDialog) |
8fa2e6a2 | 88 | |
0655ad29 | 89 | // ============================================================================ |
db1a42b8 | 90 | // wxProgressDialog implementation |
0655ad29 | 91 | // ============================================================================ |
8fa2e6a2 | 92 | |
0655ad29 | 93 | // ---------------------------------------------------------------------------- |
db1a42b8 | 94 | // wxProgressDialog creation |
0655ad29 | 95 | // ---------------------------------------------------------------------------- |
f81a6620 | 96 | |
695f550b VZ |
97 | wxProgressDialog::wxProgressDialog(const wxString& title, |
98 | const wxString& message, | |
0655ad29 VZ |
99 | int maximum, |
100 | wxWindow *parent, | |
101 | int style) | |
cdc48273 | 102 | : wxDialog(GetParentForModalDialog(parent, style), wxID_ANY, title), |
3786ce5a | 103 | m_skip(false), |
69c69546 WS |
104 | m_delay(3), |
105 | m_hasAbortButton(false), | |
106 | m_hasSkipButton(false) | |
0655ad29 | 107 | { |
39cc7a0b VZ |
108 | // we may disappear at any moment, let the others know about it |
109 | SetExtraStyle(GetExtraStyle() | wxWS_EX_TRANSIENT); | |
96becbd8 VS |
110 | m_windowStyle |= style; |
111 | ||
69c69546 WS |
112 | m_hasAbortButton = (style & wxPD_CAN_ABORT) != 0; |
113 | m_hasSkipButton = (style & wxPD_CAN_SKIP) != 0; | |
c4d305b7 | 114 | |
cff7ef89 | 115 | #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__) |
c4d305b7 VZ |
116 | // we have to remove the "Close" button from the title bar then as it is |
117 | // confusing to have it - it doesn't work anyhow | |
118 | // | |
119 | // FIXME: should probably have a (extended?) window style for this | |
69c69546 | 120 | if ( !m_hasAbortButton ) |
c4d305b7 | 121 | { |
dabbc6a5 | 122 | EnableCloseButton(false); |
c4d305b7 VZ |
123 | } |
124 | #endif // wxMSW | |
125 | ||
d2cdad17 WS |
126 | #if defined(__SMARTPHONE__) |
127 | SetLeftMenu(); | |
128 | #endif | |
129 | ||
69c69546 | 130 | m_state = m_hasAbortButton ? Continue : Uncancelable; |
0655ad29 VZ |
131 | m_maximum = maximum; |
132 | ||
94640e04 | 133 | #if defined(__WXMSW__) || defined(__WXPM__) |
7c349adb VZ |
134 | // we can't have values > 65,536 in the progress control under Windows, so |
135 | // scale everything down | |
136 | m_factor = m_maximum / 65536 + 1; | |
137 | m_maximum /= m_factor; | |
138 | #endif // __WXMSW__ | |
139 | ||
6258e418 | 140 | m_parentTop = wxGetTopLevelParent(parent); |
abceee76 | 141 | |
695f550b VZ |
142 | // top-level sizerTop |
143 | wxSizer * const sizerTop = new wxBoxSizer(wxVERTICAL); | |
d2cdad17 | 144 | |
dabbc6a5 | 145 | m_msg = new wxStaticText(this, wxID_ANY, message); |
695f550b | 146 | sizerTop->Add(m_msg, 0, wxLEFT | wxTOP, 2*LAYOUT_MARGIN); |
0655ad29 | 147 | |
0655ad29 VZ |
148 | if ( maximum > 0 ) |
149 | { | |
ecda9475 | 150 | int gauge_style = wxGA_HORIZONTAL; |
695f550b | 151 | if ( style & wxPD_SMOOTH ) |
ecda9475 | 152 | gauge_style |= wxGA_SMOOTH; |
695f550b VZ |
153 | m_gauge = new wxGauge |
154 | ( | |
155 | this, | |
156 | wxID_ANY, | |
157 | m_maximum, | |
158 | wxDefaultPosition, | |
159 | // make the progress bar sufficiently long | |
160 | wxSize(wxMin(wxGetClientDisplayRect().width/3, 300), -1), | |
161 | gauge_style | |
162 | ); | |
163 | ||
164 | sizerTop->Add(m_gauge, 0, wxLEFT | wxRIGHT | wxTOP | wxEXPAND, 2*LAYOUT_MARGIN); | |
0655ad29 | 165 | m_gauge->SetValue(0); |
0655ad29 VZ |
166 | } |
167 | else | |
695f550b VZ |
168 | { |
169 | m_gauge = NULL; | |
170 | } | |
0655ad29 VZ |
171 | |
172 | // create the estimated/remaining/total time zones if requested | |
695f550b VZ |
173 | m_elapsed = |
174 | m_estimated = | |
175 | m_remaining = NULL; | |
176 | m_display_estimated = | |
177 | m_last_timeupdate = | |
178 | m_break = 0; | |
2c5ef4e2 | 179 | m_ctdelay = 0; |
0655ad29 | 180 | |
8d6854a6 VZ |
181 | // also count how many labels we really have |
182 | size_t nTimeLabels = 0; | |
183 | ||
695f550b VZ |
184 | wxSizer * const sizerLabels = new wxFlexGridSizer(2); |
185 | ||
0655ad29 VZ |
186 | if ( style & wxPD_ELAPSED_TIME ) |
187 | { | |
188 | nTimeLabels++; | |
189 | ||
695f550b | 190 | m_elapsed = CreateLabel(_("Elapsed time:"), sizerLabels); |
0655ad29 VZ |
191 | } |
192 | ||
193 | if ( style & wxPD_ESTIMATED_TIME ) | |
194 | { | |
195 | nTimeLabels++; | |
196 | ||
695f550b | 197 | m_estimated = CreateLabel(_("Estimated time:"), sizerLabels); |
0655ad29 VZ |
198 | } |
199 | ||
200 | if ( style & wxPD_REMAINING_TIME ) | |
201 | { | |
202 | nTimeLabels++; | |
203 | ||
695f550b | 204 | m_remaining = CreateLabel(_("Remaining time:"), sizerLabels); |
0655ad29 | 205 | } |
695f550b | 206 | sizerTop->Add(sizerLabels, 0, wxALIGN_CENTER_HORIZONTAL | wxTOP, LAYOUT_MARGIN); |
0655ad29 VZ |
207 | |
208 | if ( nTimeLabels > 0 ) | |
209 | { | |
210 | // set it to the current time | |
211 | m_timeStart = wxGetCurrentTime(); | |
0655ad29 VZ |
212 | } |
213 | ||
d2cdad17 | 214 | #if defined(__SMARTPHONE__) |
69c69546 | 215 | if ( m_hasSkipButton ) |
ecda9475 | 216 | SetRightMenu(wxID_SKIP, _("Skip")); |
69c69546 | 217 | if ( m_hasAbortButton ) |
ecda9475 | 218 | SetLeftMenu(wxID_CANCEL); |
d2cdad17 | 219 | #else |
695f550b VZ |
220 | m_btnAbort = |
221 | m_btnSkip = NULL; | |
222 | ||
ecda9475 | 223 | wxBoxSizer *buttonSizer = new wxBoxSizer(wxHORIZONTAL); |
0655ad29 | 224 | |
695f550b | 225 | // Windows dialogs usually have buttons in the lower right corner |
3786ce5a | 226 | const int sizerFlags = |
94640e04 | 227 | #if defined(__WXMSW__) || defined(__WXPM__) |
ecda9475 | 228 | wxALIGN_RIGHT | wxALL |
0655ad29 | 229 | #else // !MSW |
ecda9475 | 230 | wxALIGN_CENTER_HORIZONTAL | wxBOTTOM | wxTOP |
0655ad29 | 231 | #endif // MSW/!MSW |
ecda9475 WS |
232 | ; |
233 | ||
69c69546 | 234 | if ( m_hasSkipButton ) |
ecda9475 | 235 | { |
695f550b | 236 | m_btnSkip = new wxButton(this, wxID_SKIP, _("&Skip")); |
ecda9475 | 237 | |
ecda9475 | 238 | buttonSizer->Add(m_btnSkip, 0, sizerFlags, LAYOUT_MARGIN); |
0655ad29 | 239 | } |
ecda9475 | 240 | |
69c69546 | 241 | if ( m_hasAbortButton ) |
e269a9be | 242 | { |
ecda9475 WS |
243 | m_btnAbort = new wxButton(this, wxID_CANCEL); |
244 | ||
ecda9475 | 245 | buttonSizer->Add(m_btnAbort, 0, sizerFlags, LAYOUT_MARGIN); |
e269a9be | 246 | } |
0655ad29 | 247 | |
6333ffcc FM |
248 | if (!m_hasSkipButton && !m_hasAbortButton) |
249 | buttonSizer->AddSpacer(LAYOUT_MARGIN); | |
250 | ||
695f550b | 251 | sizerTop->Add(buttonSizer, 0, sizerFlags, LAYOUT_MARGIN ); |
ecda9475 WS |
252 | #endif // __SMARTPHONE__/!__SMARTPHONE__ |
253 | ||
695f550b | 254 | SetSizerAndFit(sizerTop); |
0655ad29 VZ |
255 | |
256 | Centre(wxCENTER_FRAME | wxBOTH); | |
257 | ||
cbc66a27 | 258 | if ( style & wxPD_APP_MODAL ) |
0655ad29 | 259 | { |
cbc66a27 | 260 | m_winDisabler = new wxWindowDisabler(this); |
0655ad29 VZ |
261 | } |
262 | else | |
263 | { | |
cbc66a27 | 264 | if ( m_parentTop ) |
dabbc6a5 | 265 | m_parentTop->Disable(); |
cbc66a27 | 266 | m_winDisabler = NULL; |
0655ad29 VZ |
267 | } |
268 | ||
dabbc6a5 DS |
269 | Show(); |
270 | Enable(); | |
33961d59 | 271 | |
70f3fad6 VZ |
272 | // this one can be initialized even if the others are unknown for now |
273 | // | |
274 | // NB: do it after calling Layout() to keep the labels correctly aligned | |
275 | if ( m_elapsed ) | |
276 | { | |
277 | SetTimeLabel(0, m_elapsed); | |
278 | } | |
279 | ||
f89d65ea | 280 | Update(); |
8fa2e6a2 KB |
281 | } |
282 | ||
695f550b VZ |
283 | wxStaticText * |
284 | wxProgressDialog::CreateLabel(const wxString& text, wxSizer *sizer) | |
0655ad29 | 285 | { |
695f550b VZ |
286 | wxStaticText *label = new wxStaticText(this, wxID_ANY, text); |
287 | wxStaticText *value = new wxStaticText(this, wxID_ANY, _("unknown")); | |
0655ad29 | 288 | |
d2cdad17 WS |
289 | // select placement most native or nice on target GUI |
290 | #if defined(__SMARTPHONE__) | |
695f550b VZ |
291 | // value and time to the left in two rows |
292 | sizer->Add(label, 1, wxALIGN_LEFT); | |
293 | sizer->Add(value, 1, wxALIGN_LEFT); | |
fe8635a7 | 294 | #elif defined(__WXMSW__) || defined(__WXPM__) || defined(__WXMAC__) || defined(__WXGTK20__) |
695f550b VZ |
295 | // value and time centered in one row |
296 | sizer->Add(label, 1, wxLARGESMALL(wxALIGN_RIGHT,wxALIGN_LEFT) | wxTOP | wxRIGHT, LAYOUT_MARGIN); | |
297 | sizer->Add(value, 1, wxALIGN_LEFT | wxTOP, LAYOUT_MARGIN); | |
d2cdad17 | 298 | #else |
695f550b VZ |
299 | // value and time to the right in one row |
300 | sizer->Add(label); | |
301 | sizer->Add(value, 0, wxLEFT, LAYOUT_MARGIN); | |
d2cdad17 | 302 | #endif |
0655ad29 | 303 | |
695f550b | 304 | return value; |
0655ad29 | 305 | } |
8fa2e6a2 | 306 | |
db1a42b8 VZ |
307 | // ---------------------------------------------------------------------------- |
308 | // wxProgressDialog operations | |
309 | // ---------------------------------------------------------------------------- | |
310 | ||
8fa2e6a2 | 311 | bool |
ecda9475 | 312 | wxProgressDialog::Update(int value, const wxString& newmsg, bool *skip) |
8fa2e6a2 | 313 | { |
86417abf VZ |
314 | if ( !DoBeforeUpdate(skip) ) |
315 | return false; | |
316 | ||
abceee76 | 317 | wxASSERT_MSG( value == -1 || m_gauge, wxT("cannot update non existent dialog") ); |
7c349adb VZ |
318 | |
319 | #ifdef __WXMSW__ | |
320 | value /= m_factor; | |
321 | #endif // __WXMSW__ | |
322 | ||
abceee76 VZ |
323 | wxASSERT_MSG( value <= m_maximum, wxT("invalid progress value") ); |
324 | ||
c4c6ada9 VZ |
325 | if ( m_gauge ) |
326 | m_gauge->SetValue(value); | |
abceee76 | 327 | |
fe8635a7 | 328 | UpdateMessage(newmsg); |
abceee76 VZ |
329 | |
330 | if ( (m_elapsed || m_remaining || m_estimated) && (value != 0) ) | |
331 | { | |
332 | unsigned long elapsed = wxGetCurrentTime() - m_timeStart; | |
2c5ef4e2 VZ |
333 | if ( m_last_timeupdate < elapsed |
334 | || value == m_maximum | |
335 | ) | |
336 | { | |
337 | m_last_timeupdate = elapsed; | |
338 | unsigned long estimated = m_break + | |
339 | (unsigned long)(( (double) (elapsed-m_break) * m_maximum ) / ((double)value)) ; | |
340 | if ( estimated > m_display_estimated | |
341 | && m_ctdelay >= 0 | |
342 | ) | |
343 | { | |
344 | ++m_ctdelay; | |
345 | } | |
346 | else if ( estimated < m_display_estimated | |
347 | && m_ctdelay <= 0 | |
348 | ) | |
349 | { | |
350 | --m_ctdelay; | |
351 | } | |
352 | else | |
353 | { | |
354 | m_ctdelay = 0; | |
355 | } | |
356 | if ( m_ctdelay >= m_delay // enough confirmations for a higher value | |
357 | || m_ctdelay <= (m_delay*-1) // enough confirmations for a lower value | |
358 | || value == m_maximum // to stay consistent | |
359 | || elapsed > m_display_estimated // to stay consistent | |
360 | || ( elapsed > 0 && elapsed < 4 ) // additional updates in the beginning | |
361 | ) | |
362 | { | |
363 | m_display_estimated = estimated; | |
364 | m_ctdelay = 0; | |
365 | } | |
366 | } | |
367 | ||
ce07cd8a | 368 | long display_remaining = m_display_estimated - elapsed; |
2c5ef4e2 VZ |
369 | if ( display_remaining < 0 ) |
370 | { | |
371 | display_remaining = 0; | |
372 | } | |
abceee76 VZ |
373 | |
374 | SetTimeLabel(elapsed, m_elapsed); | |
2c5ef4e2 VZ |
375 | SetTimeLabel(m_display_estimated, m_estimated); |
376 | SetTimeLabel(display_remaining, m_remaining); | |
abceee76 VZ |
377 | } |
378 | ||
ff095200 | 379 | if ( value == m_maximum ) |
abceee76 | 380 | { |
837adaa9 VZ |
381 | if ( m_state == Finished ) |
382 | { | |
383 | // ignore multiple calls to Update(m_maximum): it may sometimes be | |
384 | // troublesome to ensure that Update() is not called twice with the | |
385 | // same value (e.g. because of the rounding errors) and if we don't | |
386 | // return now we're going to generate asserts below | |
387 | return true; | |
388 | } | |
389 | ||
dabbc6a5 | 390 | // so that we return true below and that out [Cancel] handler knew what |
1c79904b MB |
391 | // to do |
392 | m_state = Finished; | |
f62c5581 | 393 | if( !HasFlag(wxPD_AUTO_HIDE) ) |
abceee76 | 394 | { |
69c69546 WS |
395 | EnableClose(); |
396 | DisableSkip(); | |
cff7ef89 | 397 | #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__) |
69c69546 | 398 | EnableCloseButton(); |
c4d305b7 | 399 | #endif // __WXMSW__ |
abceee76 | 400 | |
60ad766e | 401 | if ( newmsg.empty() ) |
1c79904b MB |
402 | { |
403 | // also provide the finishing message if the application didn't | |
404 | m_msg->SetLabel(_("Done.")); | |
405 | } | |
abceee76 | 406 | |
33957ef5 FM |
407 | wxCHECK_MSG(wxEventLoopBase::GetActive(), false, |
408 | "wxProgressDialog::Update needs a running event loop"); | |
409 | ||
977a41ec FM |
410 | // allow the window to repaint: |
411 | // NOTE: since we yield only for UI events with this call, there | |
412 | // should be no side-effects | |
dde19c21 | 413 | wxEventLoopBase::GetActive()->YieldFor(wxEVT_CATEGORY_UI); |
ff095200 | 414 | |
8f139810 FM |
415 | // NOTE: this call results in a new event loop being created |
416 | // and to a call to ProcessPendingEvents() (which may generate | |
417 | // unwanted re-entrancies). | |
1c79904b MB |
418 | (void)ShowModal(); |
419 | } | |
ff095200 | 420 | else // auto hide |
1c79904b | 421 | { |
ff095200 VZ |
422 | // reenable other windows before hiding this one because otherwise |
423 | // Windows wouldn't give the focus back to the window which had | |
424 | // been previously focused because it would still be disabled | |
1c79904b | 425 | ReenableOtherWindows(); |
ff095200 VZ |
426 | |
427 | Hide(); | |
1c79904b | 428 | } |
abceee76 | 429 | } |
f4aa7ec3 | 430 | else // not at maximum yet |
abceee76 | 431 | { |
86417abf | 432 | DoAfterUpdate(); |
abceee76 | 433 | } |
1b6452df | 434 | |
1d1c3a9f | 435 | // update the display in case yielding above didn't do it |
f89d65ea | 436 | Update(); |
0655ad29 | 437 | |
abceee76 | 438 | return m_state != Canceled; |
8fa2e6a2 KB |
439 | } |
440 | ||
f4aa7ec3 | 441 | bool wxProgressDialog::Pulse(const wxString& newmsg, bool *skip) |
fe8635a7 | 442 | { |
86417abf VZ |
443 | if ( !DoBeforeUpdate(skip) ) |
444 | return false; | |
445 | ||
fe8635a7 RR |
446 | wxASSERT_MSG( m_gauge, wxT("cannot update non existent dialog") ); |
447 | ||
448 | // show a bit of progress | |
449 | m_gauge->Pulse(); | |
450 | ||
451 | UpdateMessage(newmsg); | |
452 | ||
453 | if (m_elapsed || m_remaining || m_estimated) | |
454 | { | |
455 | unsigned long elapsed = wxGetCurrentTime() - m_timeStart; | |
456 | ||
457 | SetTimeLabel(elapsed, m_elapsed); | |
458 | SetTimeLabel((unsigned long)-1, m_estimated); | |
459 | SetTimeLabel((unsigned long)-1, m_remaining); | |
460 | } | |
461 | ||
86417abf VZ |
462 | DoAfterUpdate(); |
463 | ||
464 | return m_state != Canceled; | |
f4aa7ec3 VZ |
465 | } |
466 | ||
86417abf | 467 | bool wxProgressDialog::DoBeforeUpdate(bool *skip) |
f4aa7ec3 | 468 | { |
33957ef5 | 469 | wxCHECK_MSG(wxEventLoopBase::GetActive(), false, |
86417abf | 470 | "wxProgressDialog::DoBeforeUpdate needs a running event loop"); |
33957ef5 | 471 | |
fe8635a7 RR |
472 | // we have to yield because not only we want to update the display but |
473 | // also to process the clicks on the cancel and skip buttons | |
977a41ec FM |
474 | // NOTE: using YieldFor() this call shouldn't give re-entrancy problems |
475 | // for event handlers not interested to UI/user-input events. | |
dde19c21 | 476 | wxEventLoopBase::GetActive()->YieldFor(wxEVT_CATEGORY_UI|wxEVT_CATEGORY_USER_INPUT); |
f4aa7ec3 VZ |
477 | |
478 | Update(); | |
fe8635a7 | 479 | |
f4aa7ec3 | 480 | if ( m_skip && skip && !*skip ) |
fe8635a7 RR |
481 | { |
482 | *skip = true; | |
483 | m_skip = false; | |
484 | EnableSkip(); | |
485 | } | |
486 | ||
487 | return m_state != Canceled; | |
488 | } | |
489 | ||
86417abf VZ |
490 | void wxProgressDialog::DoAfterUpdate() |
491 | { | |
492 | wxCHECK_RET(wxEventLoopBase::GetActive(), | |
493 | "wxProgressDialog::DoAfterUpdate needs a running event loop"); | |
494 | ||
495 | // allow the window to repaint: | |
496 | // NOTE: since we yield only for UI events with this call, there | |
497 | // should be no side-effects | |
498 | wxEventLoopBase::GetActive()->YieldFor(wxEVT_CATEGORY_UI); | |
499 | } | |
500 | ||
db1a42b8 VZ |
501 | void wxProgressDialog::Resume() |
502 | { | |
503 | m_state = Continue; | |
2c5ef4e2 VZ |
504 | m_ctdelay = m_delay; // force an update of the elapsed/estimated/remaining time |
505 | m_break += wxGetCurrentTime()-m_timeStop; | |
db1a42b8 | 506 | |
69c69546 WS |
507 | EnableAbort(); |
508 | EnableSkip(); | |
ecda9475 | 509 | m_skip = false; |
db1a42b8 VZ |
510 | } |
511 | ||
7d1dcec2 JS |
512 | bool wxProgressDialog::Show( bool show ) |
513 | { | |
514 | // reenable other windows before hiding this one because otherwise | |
515 | // Windows wouldn't give the focus back to the window which had | |
516 | // been previously focused because it would still be disabled | |
517 | if(!show) | |
518 | ReenableOtherWindows(); | |
519 | ||
520 | return wxDialog::Show(show); | |
521 | } | |
522 | ||
af237ae4 FM |
523 | int wxProgressDialog::GetValue() const |
524 | { | |
525 | if (m_gauge) | |
526 | return m_gauge->GetValue(); | |
527 | return wxNOT_FOUND; | |
528 | } | |
529 | ||
530 | int wxProgressDialog::GetRange() const | |
531 | { | |
532 | if (m_gauge) | |
533 | return m_gauge->GetRange(); | |
534 | return wxNOT_FOUND; | |
535 | } | |
536 | ||
537 | wxString wxProgressDialog::GetMessage() const | |
538 | { | |
539 | return m_msg->GetLabel(); | |
540 | } | |
541 | ||
ed1288ee FM |
542 | void wxProgressDialog::SetRange(int maximum) |
543 | { | |
544 | wxASSERT_MSG(m_gauge, "The dialog should have been constructed with a range > 0"); | |
545 | wxASSERT_MSG(maximum > 0, "Invalid range"); | |
546 | ||
547 | m_gauge->SetRange(maximum); | |
548 | m_maximum = maximum; | |
549 | ||
550 | #if defined(__WXMSW__) || defined(__WXPM__) | |
551 | // we can't have values > 65,536 in the progress control under Windows, so | |
552 | // scale everything down | |
553 | m_factor = m_maximum / 65536 + 1; | |
554 | m_maximum /= m_factor; | |
555 | #endif // __WXMSW__ | |
556 | } | |
557 | ||
f994a8ac VZ |
558 | |
559 | bool wxProgressDialog::WasCancelled() const | |
560 | { | |
561 | return m_hasAbortButton && m_state == Canceled; | |
562 | } | |
563 | ||
564 | bool wxProgressDialog::WasSkipped() const | |
565 | { | |
566 | return m_hasSkipButton && m_skip; | |
567 | } | |
568 | ||
569 | ||
0655ad29 VZ |
570 | // ---------------------------------------------------------------------------- |
571 | // event handlers | |
572 | // ---------------------------------------------------------------------------- | |
573 | ||
574 | void wxProgressDialog::OnCancel(wxCommandEvent& event) | |
575 | { | |
576 | if ( m_state == Finished ) | |
577 | { | |
578 | // this means that the count down is already finished and we're being | |
579 | // shown as a modal dialog - so just let the default handler do the job | |
580 | event.Skip(); | |
581 | } | |
582 | else | |
583 | { | |
584 | // request to cancel was received, the next time Update() is called we | |
585 | // will handle it | |
586 | m_state = Canceled; | |
1b6452df | 587 | |
69c69546 | 588 | // update the buttons state immediately so that the user knows that the |
1b6452df | 589 | // request has been noticed |
69c69546 WS |
590 | DisableAbort(); |
591 | DisableSkip(); | |
2c5ef4e2 VZ |
592 | |
593 | // save the time when the dialog was stopped | |
594 | m_timeStop = wxGetCurrentTime(); | |
0655ad29 VZ |
595 | } |
596 | } | |
597 | ||
ecda9475 WS |
598 | void wxProgressDialog::OnSkip(wxCommandEvent& WXUNUSED(event)) |
599 | { | |
69c69546 | 600 | DisableSkip(); |
ecda9475 WS |
601 | m_skip = true; |
602 | } | |
603 | ||
8fa2e6a2 KB |
604 | void wxProgressDialog::OnClose(wxCloseEvent& event) |
605 | { | |
0655ad29 | 606 | if ( m_state == Uncancelable ) |
abceee76 VZ |
607 | { |
608 | // can't close this dialog | |
dabbc6a5 | 609 | event.Veto(); |
abceee76 VZ |
610 | } |
611 | else if ( m_state == Finished ) | |
612 | { | |
613 | // let the default handler close the window as we already terminated | |
614 | event.Skip(); | |
615 | } | |
0655ad29 | 616 | else |
abceee76 VZ |
617 | { |
618 | // next Update() will notice it | |
0655ad29 | 619 | m_state = Canceled; |
69c69546 WS |
620 | DisableAbort(); |
621 | DisableSkip(); | |
ecda9475 | 622 | |
2c5ef4e2 | 623 | m_timeStop = wxGetCurrentTime(); |
abceee76 | 624 | } |
8fa2e6a2 KB |
625 | } |
626 | ||
0655ad29 VZ |
627 | // ---------------------------------------------------------------------------- |
628 | // destruction | |
629 | // ---------------------------------------------------------------------------- | |
8fa2e6a2 KB |
630 | |
631 | wxProgressDialog::~wxProgressDialog() | |
ef8698d6 VZ |
632 | { |
633 | // normally this should have been already done, but just in case | |
634 | ReenableOtherWindows(); | |
635 | } | |
636 | ||
637 | void wxProgressDialog::ReenableOtherWindows() | |
8fa2e6a2 | 638 | { |
f62c5581 | 639 | if ( HasFlag(wxPD_APP_MODAL) ) |
0655ad29 | 640 | { |
5276b0a5 | 641 | wxDELETE(m_winDisabler); |
0655ad29 VZ |
642 | } |
643 | else | |
644 | { | |
cbc66a27 | 645 | if ( m_parentTop ) |
dabbc6a5 | 646 | m_parentTop->Enable(); |
0655ad29 | 647 | } |
8fa2e6a2 | 648 | } |
ce4169a4 | 649 | |
0655ad29 VZ |
650 | // ---------------------------------------------------------------------------- |
651 | // private functions | |
652 | // ---------------------------------------------------------------------------- | |
653 | ||
654 | static void SetTimeLabel(unsigned long val, wxStaticText *label) | |
655 | { | |
656 | if ( label ) | |
657 | { | |
658 | wxString s; | |
fe8635a7 RR |
659 | |
660 | if (val != (unsigned long)-1) | |
661 | { | |
f62c5581 FM |
662 | unsigned long hours = val / 3600; |
663 | unsigned long minutes = (val % 3600) / 60; | |
664 | unsigned long seconds = val % 60; | |
665 | s.Printf(wxT("%lu:%02lu:%02lu"), hours, minutes, seconds); | |
fe8635a7 RR |
666 | } |
667 | else | |
668 | { | |
669 | s = _("Unknown"); | |
670 | } | |
0655ad29 VZ |
671 | |
672 | if ( s != label->GetLabel() ) | |
673 | label->SetLabel(s); | |
674 | } | |
675 | } | |
676 | ||
69c69546 WS |
677 | void wxProgressDialog::EnableSkip(bool enable) |
678 | { | |
679 | if(m_hasSkipButton) | |
680 | { | |
681 | #ifdef __SMARTPHONE__ | |
682 | if(enable) | |
683 | SetRightMenu(wxID_SKIP, _("Skip")); | |
684 | else | |
685 | SetRightMenu(); | |
686 | #else | |
687 | if(m_btnSkip) | |
688 | m_btnSkip->Enable(enable); | |
689 | #endif | |
690 | } | |
691 | } | |
692 | ||
693 | void wxProgressDialog::EnableAbort(bool enable) | |
694 | { | |
695 | if(m_hasAbortButton) | |
696 | { | |
697 | #ifdef __SMARTPHONE__ | |
698 | if(enable) | |
9a2256da | 699 | SetLeftMenu(wxID_CANCEL); // stock buttons makes Cancel label |
69c69546 WS |
700 | else |
701 | SetLeftMenu(); | |
702 | #else | |
703 | if(m_btnAbort) | |
704 | m_btnAbort->Enable(enable); | |
705 | #endif | |
706 | } | |
707 | } | |
708 | ||
709 | void wxProgressDialog::EnableClose() | |
710 | { | |
711 | if(m_hasAbortButton) | |
712 | { | |
713 | #ifdef __SMARTPHONE__ | |
714 | SetLeftMenu(wxID_CANCEL, _("Close")); | |
715 | #else | |
716 | if(m_btnAbort) | |
717 | { | |
718 | m_btnAbort->Enable(); | |
719 | m_btnAbort->SetLabel(_("Close")); | |
720 | } | |
721 | #endif | |
722 | } | |
723 | } | |
724 | ||
fe8635a7 RR |
725 | void wxProgressDialog::UpdateMessage(const wxString &newmsg) |
726 | { | |
33957ef5 FM |
727 | wxCHECK_RET(wxEventLoopBase::GetActive(), |
728 | "wxProgressDialog::UpdateMessage needs a running event loop"); | |
729 | ||
fe8635a7 RR |
730 | if ( !newmsg.empty() && newmsg != m_msg->GetLabel() ) |
731 | { | |
732 | m_msg->SetLabel(newmsg); | |
733 | ||
977a41ec FM |
734 | // allow the window to repaint: |
735 | // NOTE: since we yield only for UI events with this call, there | |
736 | // should be no side-effects | |
dde19c21 | 737 | wxEventLoopBase::GetActive()->YieldFor(wxEVT_CATEGORY_UI); |
fe8635a7 RR |
738 | } |
739 | } | |
740 | ||
0655ad29 | 741 | #endif // wxUSE_PROGRESSDLG |