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