]>
Commit | Line | Data |
---|---|---|
8fa2e6a2 | 1 | ///////////////////////////////////////////////////////////////////////////// |
9eddec69 | 2 | // Name: src/generic/progdlgg.cpp |
c31d9c7f | 3 | // Purpose: wxGenericProgressDialog 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 | // event tables | |
71 | // ---------------------------------------------------------------------------- | |
8fa2e6a2 | 72 | |
c31d9c7f VZ |
73 | BEGIN_EVENT_TABLE(wxGenericProgressDialog, wxDialog) |
74 | EVT_BUTTON(wxID_CANCEL, wxGenericProgressDialog::OnCancel) | |
75 | EVT_BUTTON(wxID_SKIP, wxGenericProgressDialog::OnSkip) | |
ef8698d6 | 76 | |
c31d9c7f | 77 | EVT_CLOSE(wxGenericProgressDialog::OnClose) |
1b6452df | 78 | END_EVENT_TABLE() |
8fa2e6a2 | 79 | |
0655ad29 | 80 | // ============================================================================ |
c31d9c7f | 81 | // wxGenericProgressDialog implementation |
0655ad29 | 82 | // ============================================================================ |
8fa2e6a2 | 83 | |
c31d9c7f VZ |
84 | wxIMPLEMENT_CLASS(wxProgressDialog, wxDialog) |
85 | ||
0655ad29 | 86 | // ---------------------------------------------------------------------------- |
c31d9c7f | 87 | // wxGenericProgressDialog creation |
0655ad29 | 88 | // ---------------------------------------------------------------------------- |
f81a6620 | 89 | |
f27f9577 | 90 | void wxGenericProgressDialog::Init() |
0655ad29 | 91 | { |
39cc7a0b VZ |
92 | // we may disappear at any moment, let the others know about it |
93 | SetExtraStyle(GetExtraStyle() | wxWS_EX_TRANSIENT); | |
2de77c6a VZ |
94 | |
95 | // Initialize all our members that we always use (even when we don't | |
96 | // create a valid window in this class). | |
97 | ||
f27f9577 VZ |
98 | m_pdStyle = 0; |
99 | m_parentTop = NULL; | |
c31d9c7f | 100 | |
2de77c6a VZ |
101 | m_gauge = NULL; |
102 | m_msg = NULL; | |
103 | m_elapsed = | |
104 | m_estimated = | |
105 | m_remaining = NULL; | |
c31d9c7f | 106 | |
c31d9c7f | 107 | m_state = Uncancelable; |
2de77c6a | 108 | m_maximum = 0; |
c31d9c7f VZ |
109 | |
110 | m_timeStart = wxGetCurrentTime(); | |
111 | m_timeStop = (unsigned long)-1; | |
112 | m_break = 0; | |
113 | ||
114 | m_skip = false; | |
115 | ||
3e4f133a VZ |
116 | #if !defined(__SMARTPHONE__) |
117 | m_btnAbort = | |
118 | m_btnSkip = NULL; | |
119 | #endif | |
120 | ||
c31d9c7f VZ |
121 | m_display_estimated = |
122 | m_last_timeupdate = | |
123 | m_ctdelay = 0; | |
124 | ||
125 | m_delay = 3; | |
126 | ||
79e58a40 | 127 | m_winDisabler = NULL; |
7ad8a38a | 128 | m_tempEventLoop = NULL; |
c31d9c7f VZ |
129 | } |
130 | ||
f27f9577 | 131 | wxGenericProgressDialog::wxGenericProgressDialog() |
c31d9c7f VZ |
132 | : wxDialog() |
133 | { | |
f27f9577 | 134 | Init(); |
c31d9c7f VZ |
135 | } |
136 | ||
137 | wxGenericProgressDialog::wxGenericProgressDialog(const wxString& title, | |
138 | const wxString& message, | |
139 | int maximum, | |
140 | wxWindow *parent, | |
141 | int style) | |
142 | : wxDialog() | |
143 | { | |
f27f9577 | 144 | Init(); |
c31d9c7f VZ |
145 | |
146 | Create( title, message, maximum, parent, style ); | |
147 | } | |
148 | ||
7e083675 VZ |
149 | void wxGenericProgressDialog::SetTopParent(wxWindow* parent) |
150 | { | |
151 | m_parentTop = GetParentForModalDialog(parent, GetWindowStyle()); | |
152 | } | |
153 | ||
f27f9577 | 154 | bool wxGenericProgressDialog::Create( const wxString& title, |
c31d9c7f VZ |
155 | const wxString& message, |
156 | int maximum, | |
157 | wxWindow *parent, | |
158 | int style ) | |
159 | { | |
7e083675 VZ |
160 | SetTopParent(parent); |
161 | ||
f27f9577 VZ |
162 | m_parentTop = wxGetTopLevelParent(parent); |
163 | m_pdStyle = style; | |
164 | ||
516ed23a VZ |
165 | wxWindow* const |
166 | realParent = GetParentForModalDialog(parent, GetWindowStyle()); | |
c31d9c7f | 167 | |
f27f9577 VZ |
168 | if (!wxDialog::Create(realParent, wxID_ANY, title)) |
169 | return false; | |
c31d9c7f | 170 | |
2de77c6a VZ |
171 | SetMaximum(maximum); |
172 | ||
7ad8a38a VZ |
173 | // We need a running event loop in order to update the dialog and be able |
174 | // to process clicks on its buttons, so ensure that there is one running | |
175 | // even if this means we have to start it ourselves (this happens most | |
176 | // commonly during the program initialization, e.g. for the progress | |
177 | // dialogs shown from overridden wxApp::OnInit()). | |
178 | if ( !wxEventLoopBase::GetActive() ) | |
179 | { | |
180 | m_tempEventLoop = new wxEventLoop; | |
181 | wxEventLoop::SetActive(m_tempEventLoop); | |
182 | } | |
183 | ||
cff7ef89 | 184 | #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__) |
c4d305b7 VZ |
185 | // we have to remove the "Close" button from the title bar then as it is |
186 | // confusing to have it - it doesn't work anyhow | |
187 | // | |
188 | // FIXME: should probably have a (extended?) window style for this | |
e77570de | 189 | if ( !HasPDFlag(wxPD_CAN_ABORT) ) |
c4d305b7 | 190 | { |
dabbc6a5 | 191 | EnableCloseButton(false); |
c4d305b7 VZ |
192 | } |
193 | #endif // wxMSW | |
194 | ||
d2cdad17 WS |
195 | #if defined(__SMARTPHONE__) |
196 | SetLeftMenu(); | |
197 | #endif | |
198 | ||
e77570de | 199 | m_state = HasPDFlag(wxPD_CAN_ABORT) ? Continue : Uncancelable; |
abceee76 | 200 | |
695f550b VZ |
201 | // top-level sizerTop |
202 | wxSizer * const sizerTop = new wxBoxSizer(wxVERTICAL); | |
d2cdad17 | 203 | |
dabbc6a5 | 204 | m_msg = new wxStaticText(this, wxID_ANY, message); |
695f550b | 205 | sizerTop->Add(m_msg, 0, wxLEFT | wxTOP, 2*LAYOUT_MARGIN); |
0655ad29 | 206 | |
2de77c6a VZ |
207 | int gauge_style = wxGA_HORIZONTAL; |
208 | if ( style & wxPD_SMOOTH ) | |
209 | gauge_style |= wxGA_SMOOTH; | |
210 | ||
211 | #ifdef __WXMSW__ | |
212 | maximum /= m_factor; | |
213 | #endif | |
214 | ||
215 | m_gauge = new wxGauge | |
216 | ( | |
217 | this, | |
218 | wxID_ANY, | |
219 | maximum, | |
220 | wxDefaultPosition, | |
221 | // make the progress bar sufficiently long | |
222 | wxSize(wxMin(wxGetClientDisplayRect().width/3, 300), -1), | |
223 | gauge_style | |
224 | ); | |
225 | ||
226 | sizerTop->Add(m_gauge, 0, wxLEFT | wxRIGHT | wxTOP | wxEXPAND, 2*LAYOUT_MARGIN); | |
227 | m_gauge->SetValue(0); | |
0655ad29 VZ |
228 | |
229 | // create the estimated/remaining/total time zones if requested | |
695f550b VZ |
230 | m_elapsed = |
231 | m_estimated = | |
232 | m_remaining = NULL; | |
0655ad29 | 233 | |
8d6854a6 VZ |
234 | // also count how many labels we really have |
235 | size_t nTimeLabels = 0; | |
236 | ||
695f550b VZ |
237 | wxSizer * const sizerLabels = new wxFlexGridSizer(2); |
238 | ||
0655ad29 VZ |
239 | if ( style & wxPD_ELAPSED_TIME ) |
240 | { | |
241 | nTimeLabels++; | |
242 | ||
c31d9c7f | 243 | m_elapsed = CreateLabel(GetElapsedLabel(), sizerLabels); |
0655ad29 VZ |
244 | } |
245 | ||
246 | if ( style & wxPD_ESTIMATED_TIME ) | |
247 | { | |
248 | nTimeLabels++; | |
249 | ||
c31d9c7f | 250 | m_estimated = CreateLabel(GetEstimatedLabel(), sizerLabels); |
0655ad29 VZ |
251 | } |
252 | ||
253 | if ( style & wxPD_REMAINING_TIME ) | |
254 | { | |
255 | nTimeLabels++; | |
256 | ||
c31d9c7f | 257 | m_remaining = CreateLabel(GetRemainingLabel(), sizerLabels); |
0655ad29 | 258 | } |
695f550b | 259 | sizerTop->Add(sizerLabels, 0, wxALIGN_CENTER_HORIZONTAL | wxTOP, LAYOUT_MARGIN); |
0655ad29 | 260 | |
d2cdad17 | 261 | #if defined(__SMARTPHONE__) |
e77570de | 262 | if ( HasPDFlag(wxPD_CAN_SKIP) ) |
ecda9475 | 263 | SetRightMenu(wxID_SKIP, _("Skip")); |
e77570de | 264 | if ( HasPDFlag(wxPD_CAN_ABORT) ) |
ecda9475 | 265 | SetLeftMenu(wxID_CANCEL); |
d2cdad17 | 266 | #else |
695f550b VZ |
267 | m_btnAbort = |
268 | m_btnSkip = NULL; | |
269 | ||
ecda9475 | 270 | wxBoxSizer *buttonSizer = new wxBoxSizer(wxHORIZONTAL); |
0655ad29 | 271 | |
695f550b | 272 | // Windows dialogs usually have buttons in the lower right corner |
3786ce5a | 273 | const int sizerFlags = |
e4185523 | 274 | #if defined(__WXMSW__) || defined(__WXPM__) || defined(__WXOSX__) |
ecda9475 | 275 | wxALIGN_RIGHT | wxALL |
0655ad29 | 276 | #else // !MSW |
ecda9475 | 277 | wxALIGN_CENTER_HORIZONTAL | wxBOTTOM | wxTOP |
0655ad29 | 278 | #endif // MSW/!MSW |
ecda9475 WS |
279 | ; |
280 | ||
e77570de | 281 | if ( HasPDFlag(wxPD_CAN_SKIP) ) |
ecda9475 | 282 | { |
695f550b | 283 | m_btnSkip = new wxButton(this, wxID_SKIP, _("&Skip")); |
ecda9475 | 284 | |
ecda9475 | 285 | buttonSizer->Add(m_btnSkip, 0, sizerFlags, LAYOUT_MARGIN); |
0655ad29 | 286 | } |
ecda9475 | 287 | |
e77570de | 288 | if ( HasPDFlag(wxPD_CAN_ABORT) ) |
e269a9be | 289 | { |
ecda9475 WS |
290 | m_btnAbort = new wxButton(this, wxID_CANCEL); |
291 | ||
ecda9475 | 292 | buttonSizer->Add(m_btnAbort, 0, sizerFlags, LAYOUT_MARGIN); |
e269a9be | 293 | } |
0655ad29 | 294 | |
e77570de | 295 | if ( !HasPDFlag(wxPD_CAN_SKIP | wxPD_CAN_ABORT) ) |
6333ffcc FM |
296 | buttonSizer->AddSpacer(LAYOUT_MARGIN); |
297 | ||
695f550b | 298 | sizerTop->Add(buttonSizer, 0, sizerFlags, LAYOUT_MARGIN ); |
ecda9475 WS |
299 | #endif // __SMARTPHONE__/!__SMARTPHONE__ |
300 | ||
695f550b | 301 | SetSizerAndFit(sizerTop); |
0655ad29 VZ |
302 | |
303 | Centre(wxCENTER_FRAME | wxBOTH); | |
304 | ||
c31d9c7f | 305 | DisableOtherWindows(); |
0655ad29 | 306 | |
dabbc6a5 DS |
307 | Show(); |
308 | Enable(); | |
33961d59 | 309 | |
70f3fad6 VZ |
310 | // this one can be initialized even if the others are unknown for now |
311 | // | |
312 | // NB: do it after calling Layout() to keep the labels correctly aligned | |
313 | if ( m_elapsed ) | |
314 | { | |
315 | SetTimeLabel(0, m_elapsed); | |
316 | } | |
317 | ||
f89d65ea | 318 | Update(); |
f27f9577 | 319 | return true; |
8fa2e6a2 KB |
320 | } |
321 | ||
c31d9c7f VZ |
322 | void wxGenericProgressDialog::UpdateTimeEstimates(int value, |
323 | unsigned long &elapsedTime, | |
324 | unsigned long &estimatedTime, | |
325 | unsigned long &remainingTime) | |
326 | { | |
327 | unsigned long elapsed = wxGetCurrentTime() - m_timeStart; | |
328 | if ( value != 0 && (m_last_timeupdate < elapsed || value == m_maximum) ) | |
329 | { | |
330 | m_last_timeupdate = elapsed; | |
331 | unsigned long estimated = m_break + | |
332 | (unsigned long)(( (double) (elapsed-m_break) * m_maximum ) / ((double)value)) ; | |
333 | if ( estimated > m_display_estimated | |
334 | && m_ctdelay >= 0 | |
335 | ) | |
336 | { | |
337 | ++m_ctdelay; | |
338 | } | |
339 | else if ( estimated < m_display_estimated | |
340 | && m_ctdelay <= 0 | |
341 | ) | |
342 | { | |
343 | --m_ctdelay; | |
344 | } | |
345 | else | |
346 | { | |
347 | m_ctdelay = 0; | |
348 | } | |
349 | if ( m_ctdelay >= m_delay // enough confirmations for a higher value | |
350 | || m_ctdelay <= (m_delay*-1) // enough confirmations for a lower value | |
351 | || value == m_maximum // to stay consistent | |
352 | || elapsed > m_display_estimated // to stay consistent | |
353 | || ( elapsed > 0 && elapsed < 4 ) // additional updates in the beginning | |
354 | ) | |
355 | { | |
356 | m_display_estimated = estimated; | |
357 | m_ctdelay = 0; | |
358 | } | |
359 | } | |
360 | ||
361 | if ( value != 0 ) | |
362 | { | |
363 | long display_remaining = m_display_estimated - elapsed; | |
364 | if ( display_remaining < 0 ) | |
365 | { | |
366 | display_remaining = 0; | |
367 | } | |
368 | ||
369 | estimatedTime = m_display_estimated; | |
370 | remainingTime = display_remaining; | |
371 | } | |
372 | ||
373 | elapsedTime = elapsed; | |
374 | } | |
375 | ||
376 | // static | |
377 | wxString wxGenericProgressDialog::GetFormattedTime(unsigned long timeInSec) | |
378 | { | |
379 | wxString timeAsHMS; | |
380 | ||
381 | if ( timeInSec == (unsigned long)-1 ) | |
382 | { | |
383 | timeAsHMS = _("Unknown"); | |
384 | } | |
385 | else | |
386 | { | |
387 | unsigned hours = timeInSec / 3600; | |
388 | unsigned minutes = (timeInSec % 3600) / 60; | |
389 | unsigned seconds = timeInSec % 60; | |
390 | timeAsHMS.Printf("%u:%02u:%02u", hours, minutes, seconds); | |
391 | } | |
392 | ||
393 | return timeAsHMS; | |
394 | } | |
395 | ||
695f550b | 396 | wxStaticText * |
c31d9c7f | 397 | wxGenericProgressDialog::CreateLabel(const wxString& text, wxSizer *sizer) |
0655ad29 | 398 | { |
695f550b VZ |
399 | wxStaticText *label = new wxStaticText(this, wxID_ANY, text); |
400 | wxStaticText *value = new wxStaticText(this, wxID_ANY, _("unknown")); | |
0655ad29 | 401 | |
d2cdad17 WS |
402 | // select placement most native or nice on target GUI |
403 | #if defined(__SMARTPHONE__) | |
695f550b VZ |
404 | // value and time to the left in two rows |
405 | sizer->Add(label, 1, wxALIGN_LEFT); | |
406 | sizer->Add(value, 1, wxALIGN_LEFT); | |
fe8635a7 | 407 | #elif defined(__WXMSW__) || defined(__WXPM__) || defined(__WXMAC__) || defined(__WXGTK20__) |
695f550b VZ |
408 | // value and time centered in one row |
409 | sizer->Add(label, 1, wxLARGESMALL(wxALIGN_RIGHT,wxALIGN_LEFT) | wxTOP | wxRIGHT, LAYOUT_MARGIN); | |
410 | sizer->Add(value, 1, wxALIGN_LEFT | wxTOP, LAYOUT_MARGIN); | |
d2cdad17 | 411 | #else |
695f550b VZ |
412 | // value and time to the right in one row |
413 | sizer->Add(label); | |
414 | sizer->Add(value, 0, wxLEFT, LAYOUT_MARGIN); | |
d2cdad17 | 415 | #endif |
0655ad29 | 416 | |
695f550b | 417 | return value; |
0655ad29 | 418 | } |
8fa2e6a2 | 419 | |
db1a42b8 | 420 | // ---------------------------------------------------------------------------- |
c31d9c7f | 421 | // wxGenericProgressDialog operations |
db1a42b8 VZ |
422 | // ---------------------------------------------------------------------------- |
423 | ||
8fa2e6a2 | 424 | bool |
c31d9c7f | 425 | wxGenericProgressDialog::Update(int value, const wxString& newmsg, bool *skip) |
8fa2e6a2 | 426 | { |
86417abf VZ |
427 | if ( !DoBeforeUpdate(skip) ) |
428 | return false; | |
429 | ||
2de77c6a | 430 | wxCHECK_MSG( m_gauge, false, "dialog should be fully created" ); |
7c349adb VZ |
431 | |
432 | #ifdef __WXMSW__ | |
433 | value /= m_factor; | |
434 | #endif // __WXMSW__ | |
435 | ||
abceee76 VZ |
436 | wxASSERT_MSG( value <= m_maximum, wxT("invalid progress value") ); |
437 | ||
2de77c6a | 438 | m_gauge->SetValue(value); |
abceee76 | 439 | |
fe8635a7 | 440 | UpdateMessage(newmsg); |
abceee76 VZ |
441 | |
442 | if ( (m_elapsed || m_remaining || m_estimated) && (value != 0) ) | |
443 | { | |
c31d9c7f VZ |
444 | unsigned long elapsed; |
445 | unsigned long display_remaining; | |
2c5ef4e2 | 446 | |
c31d9c7f VZ |
447 | UpdateTimeEstimates( value, |
448 | elapsed, | |
449 | m_display_estimated, | |
450 | display_remaining ); | |
abceee76 VZ |
451 | |
452 | SetTimeLabel(elapsed, m_elapsed); | |
2c5ef4e2 VZ |
453 | SetTimeLabel(m_display_estimated, m_estimated); |
454 | SetTimeLabel(display_remaining, m_remaining); | |
abceee76 VZ |
455 | } |
456 | ||
ff095200 | 457 | if ( value == m_maximum ) |
abceee76 | 458 | { |
837adaa9 VZ |
459 | if ( m_state == Finished ) |
460 | { | |
461 | // ignore multiple calls to Update(m_maximum): it may sometimes be | |
462 | // troublesome to ensure that Update() is not called twice with the | |
463 | // same value (e.g. because of the rounding errors) and if we don't | |
464 | // return now we're going to generate asserts below | |
465 | return true; | |
466 | } | |
467 | ||
dabbc6a5 | 468 | // so that we return true below and that out [Cancel] handler knew what |
1c79904b MB |
469 | // to do |
470 | m_state = Finished; | |
e77570de | 471 | if( !HasPDFlag(wxPD_AUTO_HIDE) ) |
abceee76 | 472 | { |
69c69546 WS |
473 | EnableClose(); |
474 | DisableSkip(); | |
cff7ef89 | 475 | #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__) |
69c69546 | 476 | EnableCloseButton(); |
c4d305b7 | 477 | #endif // __WXMSW__ |
abceee76 | 478 | |
60ad766e | 479 | if ( newmsg.empty() ) |
1c79904b MB |
480 | { |
481 | // also provide the finishing message if the application didn't | |
482 | m_msg->SetLabel(_("Done.")); | |
483 | } | |
abceee76 | 484 | |
977a41ec FM |
485 | // allow the window to repaint: |
486 | // NOTE: since we yield only for UI events with this call, there | |
487 | // should be no side-effects | |
dde19c21 | 488 | wxEventLoopBase::GetActive()->YieldFor(wxEVT_CATEGORY_UI); |
ff095200 | 489 | |
8f139810 FM |
490 | // NOTE: this call results in a new event loop being created |
491 | // and to a call to ProcessPendingEvents() (which may generate | |
492 | // unwanted re-entrancies). | |
1c79904b MB |
493 | (void)ShowModal(); |
494 | } | |
ff095200 | 495 | else // auto hide |
1c79904b | 496 | { |
ff095200 VZ |
497 | // reenable other windows before hiding this one because otherwise |
498 | // Windows wouldn't give the focus back to the window which had | |
499 | // been previously focused because it would still be disabled | |
1c79904b | 500 | ReenableOtherWindows(); |
ff095200 VZ |
501 | |
502 | Hide(); | |
1c79904b | 503 | } |
abceee76 | 504 | } |
f4aa7ec3 | 505 | else // not at maximum yet |
abceee76 | 506 | { |
86417abf | 507 | DoAfterUpdate(); |
abceee76 | 508 | } |
1b6452df | 509 | |
1d1c3a9f | 510 | // update the display in case yielding above didn't do it |
f89d65ea | 511 | Update(); |
0655ad29 | 512 | |
abceee76 | 513 | return m_state != Canceled; |
8fa2e6a2 KB |
514 | } |
515 | ||
c31d9c7f | 516 | bool wxGenericProgressDialog::Pulse(const wxString& newmsg, bool *skip) |
fe8635a7 | 517 | { |
86417abf VZ |
518 | if ( !DoBeforeUpdate(skip) ) |
519 | return false; | |
520 | ||
2de77c6a | 521 | wxCHECK_MSG( m_gauge, false, "dialog should be fully created" ); |
fe8635a7 RR |
522 | |
523 | // show a bit of progress | |
524 | m_gauge->Pulse(); | |
525 | ||
526 | UpdateMessage(newmsg); | |
527 | ||
528 | if (m_elapsed || m_remaining || m_estimated) | |
529 | { | |
530 | unsigned long elapsed = wxGetCurrentTime() - m_timeStart; | |
531 | ||
532 | SetTimeLabel(elapsed, m_elapsed); | |
533 | SetTimeLabel((unsigned long)-1, m_estimated); | |
534 | SetTimeLabel((unsigned long)-1, m_remaining); | |
535 | } | |
536 | ||
86417abf VZ |
537 | DoAfterUpdate(); |
538 | ||
539 | return m_state != Canceled; | |
f4aa7ec3 VZ |
540 | } |
541 | ||
c31d9c7f | 542 | bool wxGenericProgressDialog::DoBeforeUpdate(bool *skip) |
f4aa7ec3 | 543 | { |
fe8635a7 RR |
544 | // we have to yield because not only we want to update the display but |
545 | // also to process the clicks on the cancel and skip buttons | |
977a41ec FM |
546 | // NOTE: using YieldFor() this call shouldn't give re-entrancy problems |
547 | // for event handlers not interested to UI/user-input events. | |
dde19c21 | 548 | wxEventLoopBase::GetActive()->YieldFor(wxEVT_CATEGORY_UI|wxEVT_CATEGORY_USER_INPUT); |
f4aa7ec3 VZ |
549 | |
550 | Update(); | |
fe8635a7 | 551 | |
f4aa7ec3 | 552 | if ( m_skip && skip && !*skip ) |
fe8635a7 RR |
553 | { |
554 | *skip = true; | |
555 | m_skip = false; | |
556 | EnableSkip(); | |
557 | } | |
558 | ||
559 | return m_state != Canceled; | |
560 | } | |
561 | ||
c31d9c7f | 562 | void wxGenericProgressDialog::DoAfterUpdate() |
86417abf | 563 | { |
86417abf VZ |
564 | // allow the window to repaint: |
565 | // NOTE: since we yield only for UI events with this call, there | |
566 | // should be no side-effects | |
567 | wxEventLoopBase::GetActive()->YieldFor(wxEVT_CATEGORY_UI); | |
568 | } | |
569 | ||
c31d9c7f | 570 | void wxGenericProgressDialog::Resume() |
db1a42b8 VZ |
571 | { |
572 | m_state = Continue; | |
2c5ef4e2 VZ |
573 | m_ctdelay = m_delay; // force an update of the elapsed/estimated/remaining time |
574 | m_break += wxGetCurrentTime()-m_timeStop; | |
db1a42b8 | 575 | |
69c69546 WS |
576 | EnableAbort(); |
577 | EnableSkip(); | |
ecda9475 | 578 | m_skip = false; |
db1a42b8 VZ |
579 | } |
580 | ||
c31d9c7f | 581 | bool wxGenericProgressDialog::Show( bool show ) |
7d1dcec2 JS |
582 | { |
583 | // reenable other windows before hiding this one because otherwise | |
584 | // Windows wouldn't give the focus back to the window which had | |
585 | // been previously focused because it would still be disabled | |
586 | if(!show) | |
587 | ReenableOtherWindows(); | |
588 | ||
589 | return wxDialog::Show(show); | |
590 | } | |
591 | ||
c31d9c7f | 592 | int wxGenericProgressDialog::GetValue() const |
af237ae4 | 593 | { |
2de77c6a VZ |
594 | wxCHECK_MSG( m_gauge, -1, "dialog should be fully created" ); |
595 | ||
596 | return m_gauge->GetValue(); | |
af237ae4 FM |
597 | } |
598 | ||
c31d9c7f | 599 | int wxGenericProgressDialog::GetRange() const |
af237ae4 | 600 | { |
2de77c6a | 601 | return m_maximum; |
af237ae4 FM |
602 | } |
603 | ||
c31d9c7f | 604 | wxString wxGenericProgressDialog::GetMessage() const |
af237ae4 FM |
605 | { |
606 | return m_msg->GetLabel(); | |
607 | } | |
608 | ||
c31d9c7f | 609 | void wxGenericProgressDialog::SetRange(int maximum) |
ed1288ee | 610 | { |
2de77c6a VZ |
611 | wxCHECK_RET( m_gauge, "dialog should be fully created" ); |
612 | ||
613 | wxCHECK_RET( maximum > 0, "Invalid range" ); | |
ed1288ee FM |
614 | |
615 | m_gauge->SetRange(maximum); | |
2de77c6a VZ |
616 | |
617 | SetMaximum(maximum); | |
618 | } | |
619 | ||
620 | void wxGenericProgressDialog::SetMaximum(int maximum) | |
621 | { | |
ed1288ee FM |
622 | m_maximum = maximum; |
623 | ||
624 | #if defined(__WXMSW__) || defined(__WXPM__) | |
625 | // we can't have values > 65,536 in the progress control under Windows, so | |
626 | // scale everything down | |
627 | m_factor = m_maximum / 65536 + 1; | |
ed1288ee FM |
628 | #endif // __WXMSW__ |
629 | } | |
630 | ||
f994a8ac | 631 | |
c31d9c7f | 632 | bool wxGenericProgressDialog::WasCancelled() const |
f994a8ac | 633 | { |
e77570de | 634 | return HasPDFlag(wxPD_CAN_ABORT) && m_state == Canceled; |
f994a8ac VZ |
635 | } |
636 | ||
c31d9c7f | 637 | bool wxGenericProgressDialog::WasSkipped() const |
f994a8ac | 638 | { |
e77570de | 639 | return HasPDFlag(wxPD_CAN_SKIP) && m_skip; |
f994a8ac VZ |
640 | } |
641 | ||
c31d9c7f VZ |
642 | // static |
643 | void wxGenericProgressDialog::SetTimeLabel(unsigned long val, | |
644 | wxStaticText *label) | |
645 | { | |
646 | if ( label ) | |
647 | { | |
648 | wxString s; | |
649 | ||
650 | if (val != (unsigned long)-1) | |
651 | { | |
652 | s = GetFormattedTime(val); | |
653 | } | |
654 | else | |
655 | { | |
656 | s = _("Unknown"); | |
657 | } | |
658 | ||
659 | if ( s != label->GetLabel() ) | |
660 | label->SetLabel(s); | |
661 | } | |
662 | } | |
f994a8ac | 663 | |
0655ad29 VZ |
664 | // ---------------------------------------------------------------------------- |
665 | // event handlers | |
666 | // ---------------------------------------------------------------------------- | |
667 | ||
c31d9c7f | 668 | void wxGenericProgressDialog::OnCancel(wxCommandEvent& event) |
0655ad29 VZ |
669 | { |
670 | if ( m_state == Finished ) | |
671 | { | |
672 | // this means that the count down is already finished and we're being | |
673 | // shown as a modal dialog - so just let the default handler do the job | |
674 | event.Skip(); | |
675 | } | |
676 | else | |
677 | { | |
678 | // request to cancel was received, the next time Update() is called we | |
679 | // will handle it | |
680 | m_state = Canceled; | |
1b6452df | 681 | |
69c69546 | 682 | // update the buttons state immediately so that the user knows that the |
1b6452df | 683 | // request has been noticed |
69c69546 WS |
684 | DisableAbort(); |
685 | DisableSkip(); | |
2c5ef4e2 VZ |
686 | |
687 | // save the time when the dialog was stopped | |
688 | m_timeStop = wxGetCurrentTime(); | |
0655ad29 VZ |
689 | } |
690 | } | |
691 | ||
c31d9c7f | 692 | void wxGenericProgressDialog::OnSkip(wxCommandEvent& WXUNUSED(event)) |
ecda9475 | 693 | { |
69c69546 | 694 | DisableSkip(); |
ecda9475 WS |
695 | m_skip = true; |
696 | } | |
697 | ||
c31d9c7f | 698 | void wxGenericProgressDialog::OnClose(wxCloseEvent& event) |
8fa2e6a2 | 699 | { |
0655ad29 | 700 | if ( m_state == Uncancelable ) |
abceee76 VZ |
701 | { |
702 | // can't close this dialog | |
dabbc6a5 | 703 | event.Veto(); |
abceee76 VZ |
704 | } |
705 | else if ( m_state == Finished ) | |
706 | { | |
707 | // let the default handler close the window as we already terminated | |
708 | event.Skip(); | |
709 | } | |
0655ad29 | 710 | else |
abceee76 VZ |
711 | { |
712 | // next Update() will notice it | |
0655ad29 | 713 | m_state = Canceled; |
69c69546 WS |
714 | DisableAbort(); |
715 | DisableSkip(); | |
ecda9475 | 716 | |
2c5ef4e2 | 717 | m_timeStop = wxGetCurrentTime(); |
abceee76 | 718 | } |
8fa2e6a2 KB |
719 | } |
720 | ||
0655ad29 VZ |
721 | // ---------------------------------------------------------------------------- |
722 | // destruction | |
723 | // ---------------------------------------------------------------------------- | |
8fa2e6a2 | 724 | |
c31d9c7f | 725 | wxGenericProgressDialog::~wxGenericProgressDialog() |
ef8698d6 VZ |
726 | { |
727 | // normally this should have been already done, but just in case | |
728 | ReenableOtherWindows(); | |
7ad8a38a VZ |
729 | |
730 | if ( m_tempEventLoop ) | |
731 | { | |
732 | wxEventLoopBase::SetActive(NULL); | |
733 | delete m_tempEventLoop; | |
734 | } | |
ef8698d6 VZ |
735 | } |
736 | ||
c31d9c7f VZ |
737 | void wxGenericProgressDialog::DisableOtherWindows() |
738 | { | |
e77570de | 739 | if ( HasPDFlag(wxPD_APP_MODAL) ) |
c31d9c7f VZ |
740 | { |
741 | m_winDisabler = new wxWindowDisabler(this); | |
742 | } | |
743 | else | |
744 | { | |
745 | if ( m_parentTop ) | |
746 | m_parentTop->Disable(); | |
747 | m_winDisabler = NULL; | |
748 | } | |
749 | } | |
750 | ||
751 | void wxGenericProgressDialog::ReenableOtherWindows() | |
8fa2e6a2 | 752 | { |
e77570de | 753 | if ( HasPDFlag(wxPD_APP_MODAL) ) |
0655ad29 | 754 | { |
5276b0a5 | 755 | wxDELETE(m_winDisabler); |
0655ad29 VZ |
756 | } |
757 | else | |
758 | { | |
cbc66a27 | 759 | if ( m_parentTop ) |
dabbc6a5 | 760 | m_parentTop->Enable(); |
0655ad29 | 761 | } |
8fa2e6a2 | 762 | } |
ce4169a4 | 763 | |
0655ad29 VZ |
764 | // ---------------------------------------------------------------------------- |
765 | // private functions | |
766 | // ---------------------------------------------------------------------------- | |
767 | ||
c31d9c7f | 768 | void wxGenericProgressDialog::EnableSkip(bool enable) |
69c69546 | 769 | { |
e77570de | 770 | if ( HasPDFlag(wxPD_CAN_SKIP) ) |
69c69546 WS |
771 | { |
772 | #ifdef __SMARTPHONE__ | |
773 | if(enable) | |
774 | SetRightMenu(wxID_SKIP, _("Skip")); | |
775 | else | |
776 | SetRightMenu(); | |
777 | #else | |
778 | if(m_btnSkip) | |
779 | m_btnSkip->Enable(enable); | |
780 | #endif | |
781 | } | |
782 | } | |
783 | ||
c31d9c7f | 784 | void wxGenericProgressDialog::EnableAbort(bool enable) |
69c69546 | 785 | { |
e77570de | 786 | if( HasPDFlag(wxPD_CAN_ABORT) ) |
69c69546 WS |
787 | { |
788 | #ifdef __SMARTPHONE__ | |
789 | if(enable) | |
9a2256da | 790 | SetLeftMenu(wxID_CANCEL); // stock buttons makes Cancel label |
69c69546 WS |
791 | else |
792 | SetLeftMenu(); | |
793 | #else | |
794 | if(m_btnAbort) | |
795 | m_btnAbort->Enable(enable); | |
796 | #endif | |
797 | } | |
798 | } | |
799 | ||
c31d9c7f | 800 | void wxGenericProgressDialog::EnableClose() |
69c69546 | 801 | { |
e77570de | 802 | if(HasPDFlag(wxPD_CAN_ABORT)) |
69c69546 WS |
803 | { |
804 | #ifdef __SMARTPHONE__ | |
805 | SetLeftMenu(wxID_CANCEL, _("Close")); | |
806 | #else | |
807 | if(m_btnAbort) | |
808 | { | |
809 | m_btnAbort->Enable(); | |
810 | m_btnAbort->SetLabel(_("Close")); | |
811 | } | |
812 | #endif | |
813 | } | |
814 | } | |
815 | ||
c31d9c7f | 816 | void wxGenericProgressDialog::UpdateMessage(const wxString &newmsg) |
fe8635a7 RR |
817 | { |
818 | if ( !newmsg.empty() && newmsg != m_msg->GetLabel() ) | |
819 | { | |
820 | m_msg->SetLabel(newmsg); | |
821 | ||
977a41ec FM |
822 | // allow the window to repaint: |
823 | // NOTE: since we yield only for UI events with this call, there | |
824 | // should be no side-effects | |
dde19c21 | 825 | wxEventLoopBase::GetActive()->YieldFor(wxEVT_CATEGORY_UI); |
fe8635a7 RR |
826 | } |
827 | } | |
828 | ||
0655ad29 | 829 | #endif // wxUSE_PROGRESSDLG |