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