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