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