]> git.saurik.com Git - wxWidgets.git/blame - src/generic/progdlgg.cpp
in addition to key events, also prevent unhandled mouse events from propagating up...
[wxWidgets.git] / src / generic / progdlgg.cpp
CommitLineData
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
67static const int wxID_SKIP = 32000; // whatever
68
0655ad29
VZ
69// ----------------------------------------------------------------------------
70// event tables
71// ----------------------------------------------------------------------------
8fa2e6a2 72
c31d9c7f
VZ
73BEGIN_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 78END_EVENT_TABLE()
8fa2e6a2 79
0655ad29 80// ============================================================================
c31d9c7f 81// wxGenericProgressDialog implementation
0655ad29 82// ============================================================================
8fa2e6a2 83
c31d9c7f
VZ
84wxIMPLEMENT_CLASS(wxProgressDialog, wxDialog)
85
0655ad29 86// ----------------------------------------------------------------------------
c31d9c7f 87// wxGenericProgressDialog creation
0655ad29 88// ----------------------------------------------------------------------------
f81a6620 89
f27f9577 90void 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 131wxGenericProgressDialog::wxGenericProgressDialog()
c31d9c7f
VZ
132 : wxDialog()
133{
f27f9577 134 Init();
c31d9c7f
VZ
135}
136
137wxGenericProgressDialog::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
f27f9577 149bool wxGenericProgressDialog::Create( const wxString& title,
c31d9c7f
VZ
150 const wxString& message,
151 int maximum,
152 wxWindow *parent,
153 int style )
154{
f27f9577
VZ
155 m_parentTop = wxGetTopLevelParent(parent);
156 m_pdStyle = style;
157
516ed23a
VZ
158 wxWindow* const
159 realParent = GetParentForModalDialog(parent, GetWindowStyle());
c31d9c7f 160
f27f9577
VZ
161 if (!wxDialog::Create(realParent, wxID_ANY, title))
162 return false;
c31d9c7f 163
2de77c6a
VZ
164 SetMaximum(maximum);
165
7ad8a38a
VZ
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
cff7ef89 177#if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
c4d305b7
VZ
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
e77570de 182 if ( !HasPDFlag(wxPD_CAN_ABORT) )
c4d305b7 183 {
dabbc6a5 184 EnableCloseButton(false);
c4d305b7
VZ
185 }
186#endif // wxMSW
187
d2cdad17
WS
188#if defined(__SMARTPHONE__)
189 SetLeftMenu();
190#endif
191
e77570de 192 m_state = HasPDFlag(wxPD_CAN_ABORT) ? Continue : Uncancelable;
abceee76 193
695f550b
VZ
194 // top-level sizerTop
195 wxSizer * const sizerTop = new wxBoxSizer(wxVERTICAL);
d2cdad17 196
dabbc6a5 197 m_msg = new wxStaticText(this, wxID_ANY, message);
695f550b 198 sizerTop->Add(m_msg, 0, wxLEFT | wxTOP, 2*LAYOUT_MARGIN);
0655ad29 199
2de77c6a
VZ
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);
0655ad29
VZ
221
222 // create the estimated/remaining/total time zones if requested
695f550b
VZ
223 m_elapsed =
224 m_estimated =
225 m_remaining = NULL;
0655ad29 226
8d6854a6
VZ
227 // also count how many labels we really have
228 size_t nTimeLabels = 0;
229
695f550b
VZ
230 wxSizer * const sizerLabels = new wxFlexGridSizer(2);
231
0655ad29
VZ
232 if ( style & wxPD_ELAPSED_TIME )
233 {
234 nTimeLabels++;
235
c31d9c7f 236 m_elapsed = CreateLabel(GetElapsedLabel(), sizerLabels);
0655ad29
VZ
237 }
238
239 if ( style & wxPD_ESTIMATED_TIME )
240 {
241 nTimeLabels++;
242
c31d9c7f 243 m_estimated = CreateLabel(GetEstimatedLabel(), sizerLabels);
0655ad29
VZ
244 }
245
246 if ( style & wxPD_REMAINING_TIME )
247 {
248 nTimeLabels++;
249
c31d9c7f 250 m_remaining = CreateLabel(GetRemainingLabel(), sizerLabels);
0655ad29 251 }
695f550b 252 sizerTop->Add(sizerLabels, 0, wxALIGN_CENTER_HORIZONTAL | wxTOP, LAYOUT_MARGIN);
0655ad29 253
d2cdad17 254#if defined(__SMARTPHONE__)
e77570de 255 if ( HasPDFlag(wxPD_CAN_SKIP) )
ecda9475 256 SetRightMenu(wxID_SKIP, _("Skip"));
e77570de 257 if ( HasPDFlag(wxPD_CAN_ABORT) )
ecda9475 258 SetLeftMenu(wxID_CANCEL);
d2cdad17 259#else
695f550b
VZ
260 m_btnAbort =
261 m_btnSkip = NULL;
262
ecda9475 263 wxBoxSizer *buttonSizer = new wxBoxSizer(wxHORIZONTAL);
0655ad29 264
695f550b 265 // Windows dialogs usually have buttons in the lower right corner
3786ce5a 266 const int sizerFlags =
e4185523 267#if defined(__WXMSW__) || defined(__WXPM__) || defined(__WXOSX__)
ecda9475 268 wxALIGN_RIGHT | wxALL
0655ad29 269#else // !MSW
ecda9475 270 wxALIGN_CENTER_HORIZONTAL | wxBOTTOM | wxTOP
0655ad29 271#endif // MSW/!MSW
ecda9475
WS
272 ;
273
e77570de 274 if ( HasPDFlag(wxPD_CAN_SKIP) )
ecda9475 275 {
695f550b 276 m_btnSkip = new wxButton(this, wxID_SKIP, _("&Skip"));
ecda9475 277
ecda9475 278 buttonSizer->Add(m_btnSkip, 0, sizerFlags, LAYOUT_MARGIN);
0655ad29 279 }
ecda9475 280
e77570de 281 if ( HasPDFlag(wxPD_CAN_ABORT) )
e269a9be 282 {
ecda9475
WS
283 m_btnAbort = new wxButton(this, wxID_CANCEL);
284
ecda9475 285 buttonSizer->Add(m_btnAbort, 0, sizerFlags, LAYOUT_MARGIN);
e269a9be 286 }
0655ad29 287
e77570de 288 if ( !HasPDFlag(wxPD_CAN_SKIP | wxPD_CAN_ABORT) )
6333ffcc
FM
289 buttonSizer->AddSpacer(LAYOUT_MARGIN);
290
695f550b 291 sizerTop->Add(buttonSizer, 0, sizerFlags, LAYOUT_MARGIN );
ecda9475
WS
292#endif // __SMARTPHONE__/!__SMARTPHONE__
293
695f550b 294 SetSizerAndFit(sizerTop);
0655ad29
VZ
295
296 Centre(wxCENTER_FRAME | wxBOTH);
297
c31d9c7f 298 DisableOtherWindows();
0655ad29 299
dabbc6a5
DS
300 Show();
301 Enable();
33961d59 302
70f3fad6
VZ
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
f89d65ea 311 Update();
f27f9577 312 return true;
8fa2e6a2
KB
313}
314
c31d9c7f
VZ
315void 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
370wxString 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
695f550b 389wxStaticText *
c31d9c7f 390wxGenericProgressDialog::CreateLabel(const wxString& text, wxSizer *sizer)
0655ad29 391{
695f550b
VZ
392 wxStaticText *label = new wxStaticText(this, wxID_ANY, text);
393 wxStaticText *value = new wxStaticText(this, wxID_ANY, _("unknown"));
0655ad29 394
d2cdad17
WS
395 // select placement most native or nice on target GUI
396#if defined(__SMARTPHONE__)
695f550b
VZ
397 // value and time to the left in two rows
398 sizer->Add(label, 1, wxALIGN_LEFT);
399 sizer->Add(value, 1, wxALIGN_LEFT);
fe8635a7 400#elif defined(__WXMSW__) || defined(__WXPM__) || defined(__WXMAC__) || defined(__WXGTK20__)
695f550b
VZ
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);
d2cdad17 404#else
695f550b
VZ
405 // value and time to the right in one row
406 sizer->Add(label);
407 sizer->Add(value, 0, wxLEFT, LAYOUT_MARGIN);
d2cdad17 408#endif
0655ad29 409
695f550b 410 return value;
0655ad29 411}
8fa2e6a2 412
db1a42b8 413// ----------------------------------------------------------------------------
c31d9c7f 414// wxGenericProgressDialog operations
db1a42b8
VZ
415// ----------------------------------------------------------------------------
416
8fa2e6a2 417bool
c31d9c7f 418wxGenericProgressDialog::Update(int value, const wxString& newmsg, bool *skip)
8fa2e6a2 419{
86417abf
VZ
420 if ( !DoBeforeUpdate(skip) )
421 return false;
422
2de77c6a 423 wxCHECK_MSG( m_gauge, false, "dialog should be fully created" );
7c349adb
VZ
424
425#ifdef __WXMSW__
426 value /= m_factor;
427#endif // __WXMSW__
428
abceee76
VZ
429 wxASSERT_MSG( value <= m_maximum, wxT("invalid progress value") );
430
2de77c6a 431 m_gauge->SetValue(value);
abceee76 432
fe8635a7 433 UpdateMessage(newmsg);
abceee76
VZ
434
435 if ( (m_elapsed || m_remaining || m_estimated) && (value != 0) )
436 {
c31d9c7f
VZ
437 unsigned long elapsed;
438 unsigned long display_remaining;
2c5ef4e2 439
c31d9c7f
VZ
440 UpdateTimeEstimates( value,
441 elapsed,
442 m_display_estimated,
443 display_remaining );
abceee76
VZ
444
445 SetTimeLabel(elapsed, m_elapsed);
2c5ef4e2
VZ
446 SetTimeLabel(m_display_estimated, m_estimated);
447 SetTimeLabel(display_remaining, m_remaining);
abceee76
VZ
448 }
449
ff095200 450 if ( value == m_maximum )
abceee76 451 {
837adaa9
VZ
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
dabbc6a5 461 // so that we return true below and that out [Cancel] handler knew what
1c79904b
MB
462 // to do
463 m_state = Finished;
e77570de 464 if( !HasPDFlag(wxPD_AUTO_HIDE) )
abceee76 465 {
69c69546
WS
466 EnableClose();
467 DisableSkip();
cff7ef89 468#if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
69c69546 469 EnableCloseButton();
c4d305b7 470#endif // __WXMSW__
abceee76 471
60ad766e 472 if ( newmsg.empty() )
1c79904b
MB
473 {
474 // also provide the finishing message if the application didn't
475 m_msg->SetLabel(_("Done."));
476 }
abceee76 477
977a41ec
FM
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
dde19c21 481 wxEventLoopBase::GetActive()->YieldFor(wxEVT_CATEGORY_UI);
ff095200 482
8f139810
FM
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).
1c79904b
MB
486 (void)ShowModal();
487 }
ff095200 488 else // auto hide
1c79904b 489 {
ff095200
VZ
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
1c79904b 493 ReenableOtherWindows();
ff095200
VZ
494
495 Hide();
1c79904b 496 }
abceee76 497 }
f4aa7ec3 498 else // not at maximum yet
abceee76 499 {
86417abf 500 DoAfterUpdate();
abceee76 501 }
1b6452df 502
1d1c3a9f 503 // update the display in case yielding above didn't do it
f89d65ea 504 Update();
0655ad29 505
abceee76 506 return m_state != Canceled;
8fa2e6a2
KB
507}
508
c31d9c7f 509bool wxGenericProgressDialog::Pulse(const wxString& newmsg, bool *skip)
fe8635a7 510{
86417abf
VZ
511 if ( !DoBeforeUpdate(skip) )
512 return false;
513
2de77c6a 514 wxCHECK_MSG( m_gauge, false, "dialog should be fully created" );
fe8635a7
RR
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
86417abf
VZ
530 DoAfterUpdate();
531
532 return m_state != Canceled;
f4aa7ec3
VZ
533}
534
c31d9c7f 535bool wxGenericProgressDialog::DoBeforeUpdate(bool *skip)
f4aa7ec3 536{
fe8635a7
RR
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
977a41ec
FM
539 // NOTE: using YieldFor() this call shouldn't give re-entrancy problems
540 // for event handlers not interested to UI/user-input events.
dde19c21 541 wxEventLoopBase::GetActive()->YieldFor(wxEVT_CATEGORY_UI|wxEVT_CATEGORY_USER_INPUT);
f4aa7ec3
VZ
542
543 Update();
fe8635a7 544
f4aa7ec3 545 if ( m_skip && skip && !*skip )
fe8635a7
RR
546 {
547 *skip = true;
548 m_skip = false;
549 EnableSkip();
550 }
551
552 return m_state != Canceled;
553}
554
c31d9c7f 555void wxGenericProgressDialog::DoAfterUpdate()
86417abf 556{
86417abf
VZ
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
c31d9c7f 563void wxGenericProgressDialog::Resume()
db1a42b8
VZ
564{
565 m_state = Continue;
2c5ef4e2
VZ
566 m_ctdelay = m_delay; // force an update of the elapsed/estimated/remaining time
567 m_break += wxGetCurrentTime()-m_timeStop;
db1a42b8 568
69c69546
WS
569 EnableAbort();
570 EnableSkip();
ecda9475 571 m_skip = false;
db1a42b8
VZ
572}
573
c31d9c7f 574bool wxGenericProgressDialog::Show( bool show )
7d1dcec2
JS
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
c31d9c7f 585int wxGenericProgressDialog::GetValue() const
af237ae4 586{
2de77c6a
VZ
587 wxCHECK_MSG( m_gauge, -1, "dialog should be fully created" );
588
589 return m_gauge->GetValue();
af237ae4
FM
590}
591
c31d9c7f 592int wxGenericProgressDialog::GetRange() const
af237ae4 593{
2de77c6a 594 return m_maximum;
af237ae4
FM
595}
596
c31d9c7f 597wxString wxGenericProgressDialog::GetMessage() const
af237ae4
FM
598{
599 return m_msg->GetLabel();
600}
601
c31d9c7f 602void wxGenericProgressDialog::SetRange(int maximum)
ed1288ee 603{
2de77c6a
VZ
604 wxCHECK_RET( m_gauge, "dialog should be fully created" );
605
606 wxCHECK_RET( maximum > 0, "Invalid range" );
ed1288ee
FM
607
608 m_gauge->SetRange(maximum);
2de77c6a
VZ
609
610 SetMaximum(maximum);
611}
612
613void wxGenericProgressDialog::SetMaximum(int maximum)
614{
ed1288ee
FM
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;
ed1288ee
FM
621#endif // __WXMSW__
622}
623
f994a8ac 624
c31d9c7f 625bool wxGenericProgressDialog::WasCancelled() const
f994a8ac 626{
e77570de 627 return HasPDFlag(wxPD_CAN_ABORT) && m_state == Canceled;
f994a8ac
VZ
628}
629
c31d9c7f 630bool wxGenericProgressDialog::WasSkipped() const
f994a8ac 631{
e77570de 632 return HasPDFlag(wxPD_CAN_SKIP) && m_skip;
f994a8ac
VZ
633}
634
c31d9c7f
VZ
635// static
636void 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}
f994a8ac 656
0655ad29
VZ
657// ----------------------------------------------------------------------------
658// event handlers
659// ----------------------------------------------------------------------------
660
c31d9c7f 661void wxGenericProgressDialog::OnCancel(wxCommandEvent& event)
0655ad29
VZ
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;
1b6452df 674
69c69546 675 // update the buttons state immediately so that the user knows that the
1b6452df 676 // request has been noticed
69c69546
WS
677 DisableAbort();
678 DisableSkip();
2c5ef4e2
VZ
679
680 // save the time when the dialog was stopped
681 m_timeStop = wxGetCurrentTime();
0655ad29
VZ
682 }
683}
684
c31d9c7f 685void wxGenericProgressDialog::OnSkip(wxCommandEvent& WXUNUSED(event))
ecda9475 686{
69c69546 687 DisableSkip();
ecda9475
WS
688 m_skip = true;
689}
690
c31d9c7f 691void wxGenericProgressDialog::OnClose(wxCloseEvent& event)
8fa2e6a2 692{
0655ad29 693 if ( m_state == Uncancelable )
abceee76
VZ
694 {
695 // can't close this dialog
dabbc6a5 696 event.Veto();
abceee76
VZ
697 }
698 else if ( m_state == Finished )
699 {
700 // let the default handler close the window as we already terminated
701 event.Skip();
702 }
0655ad29 703 else
abceee76
VZ
704 {
705 // next Update() will notice it
0655ad29 706 m_state = Canceled;
69c69546
WS
707 DisableAbort();
708 DisableSkip();
ecda9475 709
2c5ef4e2 710 m_timeStop = wxGetCurrentTime();
abceee76 711 }
8fa2e6a2
KB
712}
713
0655ad29
VZ
714// ----------------------------------------------------------------------------
715// destruction
716// ----------------------------------------------------------------------------
8fa2e6a2 717
c31d9c7f 718wxGenericProgressDialog::~wxGenericProgressDialog()
ef8698d6
VZ
719{
720 // normally this should have been already done, but just in case
721 ReenableOtherWindows();
7ad8a38a
VZ
722
723 if ( m_tempEventLoop )
724 {
725 wxEventLoopBase::SetActive(NULL);
726 delete m_tempEventLoop;
727 }
ef8698d6
VZ
728}
729
c31d9c7f
VZ
730void wxGenericProgressDialog::DisableOtherWindows()
731{
e77570de 732 if ( HasPDFlag(wxPD_APP_MODAL) )
c31d9c7f
VZ
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
744void wxGenericProgressDialog::ReenableOtherWindows()
8fa2e6a2 745{
e77570de 746 if ( HasPDFlag(wxPD_APP_MODAL) )
0655ad29 747 {
5276b0a5 748 wxDELETE(m_winDisabler);
0655ad29
VZ
749 }
750 else
751 {
cbc66a27 752 if ( m_parentTop )
dabbc6a5 753 m_parentTop->Enable();
0655ad29 754 }
8fa2e6a2 755}
ce4169a4 756
0655ad29
VZ
757// ----------------------------------------------------------------------------
758// private functions
759// ----------------------------------------------------------------------------
760
c31d9c7f 761void wxGenericProgressDialog::EnableSkip(bool enable)
69c69546 762{
e77570de 763 if ( HasPDFlag(wxPD_CAN_SKIP) )
69c69546
WS
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
c31d9c7f 777void wxGenericProgressDialog::EnableAbort(bool enable)
69c69546 778{
e77570de 779 if( HasPDFlag(wxPD_CAN_ABORT) )
69c69546
WS
780 {
781#ifdef __SMARTPHONE__
782 if(enable)
9a2256da 783 SetLeftMenu(wxID_CANCEL); // stock buttons makes Cancel label
69c69546
WS
784 else
785 SetLeftMenu();
786#else
787 if(m_btnAbort)
788 m_btnAbort->Enable(enable);
789#endif
790 }
791}
792
c31d9c7f 793void wxGenericProgressDialog::EnableClose()
69c69546 794{
e77570de 795 if(HasPDFlag(wxPD_CAN_ABORT))
69c69546
WS
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
c31d9c7f 809void wxGenericProgressDialog::UpdateMessage(const wxString &newmsg)
fe8635a7
RR
810{
811 if ( !newmsg.empty() && newmsg != m_msg->GetLabel() )
812 {
813 m_msg->SetLabel(newmsg);
814
977a41ec
FM
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
dde19c21 818 wxEventLoopBase::GetActive()->YieldFor(wxEVT_CATEGORY_UI);
fe8635a7
RR
819 }
820}
821
0655ad29 822#endif // wxUSE_PROGRESSDLG