1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/progdlgg.cpp
3 // Purpose: wxProgressDialog class
4 // Author: Karsten Ballueder
8 // Copyright: (c) Karsten Ballueder
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
32 #include "wx/button.h"
33 #include "wx/stattext.h"
38 #include "wx/dcclient.h"
40 #include "wx/settings.h"
44 #include "wx/progdlg.h"
45 #include "wx/evtloop.h"
47 // ---------------------------------------------------------------------------
49 // ---------------------------------------------------------------------------
51 /* Macro for avoiding #ifdefs when value have to be different depending on size of
52 device we display on - take it from something like wxDesktopPolicy in the future
55 #if defined(__SMARTPHONE__)
56 #define wxLARGESMALL(large,small) small
58 #define wxLARGESMALL(large,small) large
61 // ----------------------------------------------------------------------------
63 // ----------------------------------------------------------------------------
65 #define LAYOUT_MARGIN wxLARGESMALL(8,2)
67 static const int wxID_SKIP
= 32000; // whatever
69 // ----------------------------------------------------------------------------
71 // ----------------------------------------------------------------------------
73 // update the label to show the given time (in seconds)
74 static void SetTimeLabel(unsigned long val
, wxStaticText
*label
);
76 // ----------------------------------------------------------------------------
78 // ----------------------------------------------------------------------------
80 BEGIN_EVENT_TABLE(wxProgressDialog
, wxDialog
)
81 EVT_BUTTON(wxID_CANCEL
, wxProgressDialog::OnCancel
)
82 EVT_BUTTON(wxID_SKIP
, wxProgressDialog::OnSkip
)
84 EVT_CLOSE(wxProgressDialog::OnClose
)
87 IMPLEMENT_CLASS(wxProgressDialog
, wxDialog
)
89 // ============================================================================
90 // wxProgressDialog implementation
91 // ============================================================================
93 // ----------------------------------------------------------------------------
94 // wxProgressDialog creation
95 // ----------------------------------------------------------------------------
97 wxProgressDialog::wxProgressDialog(const wxString
& title
,
98 const wxString
& message
,
102 : wxDialog(GetParentForModalDialog(parent
, style
), wxID_ANY
, title
),
105 m_hasAbortButton(false),
106 m_hasSkipButton(false)
108 // we may disappear at any moment, let the others know about it
109 SetExtraStyle(GetExtraStyle() | wxWS_EX_TRANSIENT
);
110 m_windowStyle
|= style
;
112 m_hasAbortButton
= (style
& wxPD_CAN_ABORT
) != 0;
113 m_hasSkipButton
= (style
& wxPD_CAN_SKIP
) != 0;
115 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
116 // we have to remove the "Close" button from the title bar then as it is
117 // confusing to have it - it doesn't work anyhow
119 // FIXME: should probably have a (extended?) window style for this
120 if ( !m_hasAbortButton
)
122 EnableCloseButton(false);
126 #if defined(__SMARTPHONE__)
130 m_state
= m_hasAbortButton
? Continue
: Uncancelable
;
133 #if defined(__WXMSW__) || defined(__WXPM__)
134 // we can't have values > 65,536 in the progress control under Windows, so
135 // scale everything down
136 m_factor
= m_maximum
/ 65536 + 1;
137 m_maximum
/= m_factor
;
140 m_parentTop
= wxGetTopLevelParent(parent
);
142 // top-level sizerTop
143 wxSizer
* const sizerTop
= new wxBoxSizer(wxVERTICAL
);
145 m_msg
= new wxStaticText(this, wxID_ANY
, message
);
146 sizerTop
->Add(m_msg
, 0, wxLEFT
| wxTOP
, 2*LAYOUT_MARGIN
);
150 int gauge_style
= wxGA_HORIZONTAL
;
151 if ( style
& wxPD_SMOOTH
)
152 gauge_style
|= wxGA_SMOOTH
;
153 m_gauge
= new wxGauge
159 // make the progress bar sufficiently long
160 wxSize(wxMin(wxGetClientDisplayRect().width
/3, 300), -1),
164 sizerTop
->Add(m_gauge
, 0, wxLEFT
| wxRIGHT
| wxTOP
| wxEXPAND
, 2*LAYOUT_MARGIN
);
165 m_gauge
->SetValue(0);
172 // create the estimated/remaining/total time zones if requested
176 m_display_estimated
=
181 // also count how many labels we really have
182 size_t nTimeLabels
= 0;
184 wxSizer
* const sizerLabels
= new wxFlexGridSizer(2);
186 if ( style
& wxPD_ELAPSED_TIME
)
190 m_elapsed
= CreateLabel(_("Elapsed time:"), sizerLabels
);
193 if ( style
& wxPD_ESTIMATED_TIME
)
197 m_estimated
= CreateLabel(_("Estimated time:"), sizerLabels
);
200 if ( style
& wxPD_REMAINING_TIME
)
204 m_remaining
= CreateLabel(_("Remaining time:"), sizerLabels
);
206 sizerTop
->Add(sizerLabels
, 0, wxALIGN_CENTER_HORIZONTAL
| wxTOP
, LAYOUT_MARGIN
);
208 if ( nTimeLabels
> 0 )
210 // set it to the current time
211 m_timeStart
= wxGetCurrentTime();
214 #if defined(__SMARTPHONE__)
215 if ( m_hasSkipButton
)
216 SetRightMenu(wxID_SKIP
, _("Skip"));
217 if ( m_hasAbortButton
)
218 SetLeftMenu(wxID_CANCEL
);
223 wxBoxSizer
*buttonSizer
= new wxBoxSizer(wxHORIZONTAL
);
225 // Windows dialogs usually have buttons in the lower right corner
226 const int sizerFlags
=
227 #if defined(__WXMSW__) || defined(__WXPM__)
228 wxALIGN_RIGHT
| wxALL
230 wxALIGN_CENTER_HORIZONTAL
| wxBOTTOM
| wxTOP
234 if ( m_hasSkipButton
)
236 m_btnSkip
= new wxButton(this, wxID_SKIP
, _("&Skip"));
238 buttonSizer
->Add(m_btnSkip
, 0, sizerFlags
, LAYOUT_MARGIN
);
241 if ( m_hasAbortButton
)
243 m_btnAbort
= new wxButton(this, wxID_CANCEL
);
245 buttonSizer
->Add(m_btnAbort
, 0, sizerFlags
, LAYOUT_MARGIN
);
248 if (!m_hasSkipButton
&& !m_hasAbortButton
)
249 buttonSizer
->AddSpacer(LAYOUT_MARGIN
);
251 sizerTop
->Add(buttonSizer
, 0, sizerFlags
, LAYOUT_MARGIN
);
252 #endif // __SMARTPHONE__/!__SMARTPHONE__
254 SetSizerAndFit(sizerTop
);
256 Centre(wxCENTER_FRAME
| wxBOTH
);
258 if ( style
& wxPD_APP_MODAL
)
260 m_winDisabler
= new wxWindowDisabler(this);
265 m_parentTop
->Disable();
266 m_winDisabler
= NULL
;
272 // this one can be initialized even if the others are unknown for now
274 // NB: do it after calling Layout() to keep the labels correctly aligned
277 SetTimeLabel(0, m_elapsed
);
284 wxProgressDialog::CreateLabel(const wxString
& text
, wxSizer
*sizer
)
286 wxStaticText
*label
= new wxStaticText(this, wxID_ANY
, text
);
287 wxStaticText
*value
= new wxStaticText(this, wxID_ANY
, _("unknown"));
289 // select placement most native or nice on target GUI
290 #if defined(__SMARTPHONE__)
291 // value and time to the left in two rows
292 sizer
->Add(label
, 1, wxALIGN_LEFT
);
293 sizer
->Add(value
, 1, wxALIGN_LEFT
);
294 #elif defined(__WXMSW__) || defined(__WXPM__) || defined(__WXMAC__) || defined(__WXGTK20__)
295 // value and time centered in one row
296 sizer
->Add(label
, 1, wxLARGESMALL(wxALIGN_RIGHT
,wxALIGN_LEFT
) | wxTOP
| wxRIGHT
, LAYOUT_MARGIN
);
297 sizer
->Add(value
, 1, wxALIGN_LEFT
| wxTOP
, LAYOUT_MARGIN
);
299 // value and time to the right in one row
301 sizer
->Add(value
, 0, wxLEFT
, LAYOUT_MARGIN
);
307 // ----------------------------------------------------------------------------
308 // wxProgressDialog operations
309 // ----------------------------------------------------------------------------
312 wxProgressDialog::Update(int value
, const wxString
& newmsg
, bool *skip
)
314 if ( !DoBeforeUpdate(skip
) )
317 wxASSERT_MSG( value
== -1 || m_gauge
, wxT("cannot update non existent dialog") );
323 wxASSERT_MSG( value
<= m_maximum
, wxT("invalid progress value") );
326 m_gauge
->SetValue(value
);
328 UpdateMessage(newmsg
);
330 if ( (m_elapsed
|| m_remaining
|| m_estimated
) && (value
!= 0) )
332 unsigned long elapsed
= wxGetCurrentTime() - m_timeStart
;
333 if ( m_last_timeupdate
< elapsed
334 || value
== m_maximum
337 m_last_timeupdate
= elapsed
;
338 unsigned long estimated
= m_break
+
339 (unsigned long)(( (double) (elapsed
-m_break
) * m_maximum
) / ((double)value
)) ;
340 if ( estimated
> m_display_estimated
346 else if ( estimated
< m_display_estimated
356 if ( m_ctdelay
>= m_delay
// enough confirmations for a higher value
357 || m_ctdelay
<= (m_delay
*-1) // enough confirmations for a lower value
358 || value
== m_maximum
// to stay consistent
359 || elapsed
> m_display_estimated
// to stay consistent
360 || ( elapsed
> 0 && elapsed
< 4 ) // additional updates in the beginning
363 m_display_estimated
= estimated
;
368 long display_remaining
= m_display_estimated
- elapsed
;
369 if ( display_remaining
< 0 )
371 display_remaining
= 0;
374 SetTimeLabel(elapsed
, m_elapsed
);
375 SetTimeLabel(m_display_estimated
, m_estimated
);
376 SetTimeLabel(display_remaining
, m_remaining
);
379 if ( value
== m_maximum
)
381 if ( m_state
== Finished
)
383 // ignore multiple calls to Update(m_maximum): it may sometimes be
384 // troublesome to ensure that Update() is not called twice with the
385 // same value (e.g. because of the rounding errors) and if we don't
386 // return now we're going to generate asserts below
390 // so that we return true below and that out [Cancel] handler knew what
393 if( !HasFlag(wxPD_AUTO_HIDE
) )
397 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
401 if ( newmsg
.empty() )
403 // also provide the finishing message if the application didn't
404 m_msg
->SetLabel(_("Done."));
407 wxCHECK_MSG(wxEventLoopBase::GetActive(), false,
408 "wxProgressDialog::Update needs a running event loop");
410 // allow the window to repaint:
411 // NOTE: since we yield only for UI events with this call, there
412 // should be no side-effects
413 wxEventLoopBase::GetActive()->YieldFor(wxEVT_CATEGORY_UI
);
415 // NOTE: this call results in a new event loop being created
416 // and to a call to ProcessPendingEvents() (which may generate
417 // unwanted re-entrancies).
422 // reenable other windows before hiding this one because otherwise
423 // Windows wouldn't give the focus back to the window which had
424 // been previously focused because it would still be disabled
425 ReenableOtherWindows();
430 else // not at maximum yet
435 // update the display in case yielding above didn't do it
438 return m_state
!= Canceled
;
441 bool wxProgressDialog::Pulse(const wxString
& newmsg
, bool *skip
)
443 if ( !DoBeforeUpdate(skip
) )
446 wxASSERT_MSG( m_gauge
, wxT("cannot update non existent dialog") );
448 // show a bit of progress
451 UpdateMessage(newmsg
);
453 if (m_elapsed
|| m_remaining
|| m_estimated
)
455 unsigned long elapsed
= wxGetCurrentTime() - m_timeStart
;
457 SetTimeLabel(elapsed
, m_elapsed
);
458 SetTimeLabel((unsigned long)-1, m_estimated
);
459 SetTimeLabel((unsigned long)-1, m_remaining
);
464 return m_state
!= Canceled
;
467 bool wxProgressDialog::DoBeforeUpdate(bool *skip
)
469 wxCHECK_MSG(wxEventLoopBase::GetActive(), false,
470 "wxProgressDialog::DoBeforeUpdate needs a running event loop");
472 // we have to yield because not only we want to update the display but
473 // also to process the clicks on the cancel and skip buttons
474 // NOTE: using YieldFor() this call shouldn't give re-entrancy problems
475 // for event handlers not interested to UI/user-input events.
476 wxEventLoopBase::GetActive()->YieldFor(wxEVT_CATEGORY_UI
|wxEVT_CATEGORY_USER_INPUT
);
480 if ( m_skip
&& skip
&& !*skip
)
487 return m_state
!= Canceled
;
490 void wxProgressDialog::DoAfterUpdate()
492 wxCHECK_RET(wxEventLoopBase::GetActive(),
493 "wxProgressDialog::DoAfterUpdate needs a running event loop");
495 // allow the window to repaint:
496 // NOTE: since we yield only for UI events with this call, there
497 // should be no side-effects
498 wxEventLoopBase::GetActive()->YieldFor(wxEVT_CATEGORY_UI
);
501 void wxProgressDialog::Resume()
504 m_ctdelay
= m_delay
; // force an update of the elapsed/estimated/remaining time
505 m_break
+= wxGetCurrentTime()-m_timeStop
;
512 bool wxProgressDialog::Show( bool show
)
514 // reenable other windows before hiding this one because otherwise
515 // Windows wouldn't give the focus back to the window which had
516 // been previously focused because it would still be disabled
518 ReenableOtherWindows();
520 return wxDialog::Show(show
);
523 int wxProgressDialog::GetValue() const
526 return m_gauge
->GetValue();
530 int wxProgressDialog::GetRange() const
533 return m_gauge
->GetRange();
537 wxString
wxProgressDialog::GetMessage() const
539 return m_msg
->GetLabel();
542 void wxProgressDialog::SetRange(int maximum
)
544 wxASSERT_MSG(m_gauge
, "The dialog should have been constructed with a range > 0");
545 wxASSERT_MSG(maximum
> 0, "Invalid range");
547 m_gauge
->SetRange(maximum
);
550 #if defined(__WXMSW__) || defined(__WXPM__)
551 // we can't have values > 65,536 in the progress control under Windows, so
552 // scale everything down
553 m_factor
= m_maximum
/ 65536 + 1;
554 m_maximum
/= m_factor
;
559 bool wxProgressDialog::WasCancelled() const
561 return m_hasAbortButton
&& m_state
== Canceled
;
564 bool wxProgressDialog::WasSkipped() const
566 return m_hasSkipButton
&& m_skip
;
570 // ----------------------------------------------------------------------------
572 // ----------------------------------------------------------------------------
574 void wxProgressDialog::OnCancel(wxCommandEvent
& event
)
576 if ( m_state
== Finished
)
578 // this means that the count down is already finished and we're being
579 // shown as a modal dialog - so just let the default handler do the job
584 // request to cancel was received, the next time Update() is called we
588 // update the buttons state immediately so that the user knows that the
589 // request has been noticed
593 // save the time when the dialog was stopped
594 m_timeStop
= wxGetCurrentTime();
598 void wxProgressDialog::OnSkip(wxCommandEvent
& WXUNUSED(event
))
604 void wxProgressDialog::OnClose(wxCloseEvent
& event
)
606 if ( m_state
== Uncancelable
)
608 // can't close this dialog
611 else if ( m_state
== Finished
)
613 // let the default handler close the window as we already terminated
618 // next Update() will notice it
623 m_timeStop
= wxGetCurrentTime();
627 // ----------------------------------------------------------------------------
629 // ----------------------------------------------------------------------------
631 wxProgressDialog::~wxProgressDialog()
633 // normally this should have been already done, but just in case
634 ReenableOtherWindows();
637 void wxProgressDialog::ReenableOtherWindows()
639 if ( HasFlag(wxPD_APP_MODAL
) )
641 wxDELETE(m_winDisabler
);
646 m_parentTop
->Enable();
650 // ----------------------------------------------------------------------------
652 // ----------------------------------------------------------------------------
654 static void SetTimeLabel(unsigned long val
, wxStaticText
*label
)
660 if (val
!= (unsigned long)-1)
662 unsigned long hours
= val
/ 3600;
663 unsigned long minutes
= (val
% 3600) / 60;
664 unsigned long seconds
= val
% 60;
665 s
.Printf(wxT("%lu:%02lu:%02lu"), hours
, minutes
, seconds
);
672 if ( s
!= label
->GetLabel() )
677 void wxProgressDialog::EnableSkip(bool enable
)
681 #ifdef __SMARTPHONE__
683 SetRightMenu(wxID_SKIP
, _("Skip"));
688 m_btnSkip
->Enable(enable
);
693 void wxProgressDialog::EnableAbort(bool enable
)
697 #ifdef __SMARTPHONE__
699 SetLeftMenu(wxID_CANCEL
); // stock buttons makes Cancel label
704 m_btnAbort
->Enable(enable
);
709 void wxProgressDialog::EnableClose()
713 #ifdef __SMARTPHONE__
714 SetLeftMenu(wxID_CANCEL
, _("Close"));
718 m_btnAbort
->Enable();
719 m_btnAbort
->SetLabel(_("Close"));
725 void wxProgressDialog::UpdateMessage(const wxString
&newmsg
)
727 wxCHECK_RET(wxEventLoopBase::GetActive(),
728 "wxProgressDialog::UpdateMessage needs a running event loop");
730 if ( !newmsg
.empty() && newmsg
!= m_msg
->GetLabel() )
732 m_msg
->SetLabel(newmsg
);
734 // allow the window to repaint:
735 // NOTE: since we yield only for UI events with this call, there
736 // should be no side-effects
737 wxEventLoopBase::GetActive()->YieldFor(wxEVT_CATEGORY_UI
);
741 #endif // wxUSE_PROGRESSDLG