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
), 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
);
143 dc
.SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
144 wxCoord widthText
= 0;
145 dc
.GetTextExtent(message
, &widthText
, NULL
, NULL
, NULL
, NULL
);
147 // top-level sizerTop
148 wxSizer
* const sizerTop
= new wxBoxSizer(wxVERTICAL
);
150 m_msg
= new wxStaticText(this, wxID_ANY
, message
);
151 sizerTop
->Add(m_msg
, 0, wxLEFT
| wxTOP
, 2*LAYOUT_MARGIN
);
155 int gauge_style
= wxGA_HORIZONTAL
;
156 if ( style
& wxPD_SMOOTH
)
157 gauge_style
|= wxGA_SMOOTH
;
158 m_gauge
= new wxGauge
164 // make the progress bar sufficiently long
165 wxSize(wxMin(wxGetClientDisplayRect().width
/3, 300), -1),
169 sizerTop
->Add(m_gauge
, 0, wxLEFT
| wxRIGHT
| wxTOP
| wxEXPAND
, 2*LAYOUT_MARGIN
);
170 m_gauge
->SetValue(0);
177 // create the estimated/remaining/total time zones if requested
181 m_display_estimated
=
186 // also count how many labels we really have
187 size_t nTimeLabels
= 0;
189 wxSizer
* const sizerLabels
= new wxFlexGridSizer(2);
191 if ( style
& wxPD_ELAPSED_TIME
)
195 m_elapsed
= CreateLabel(_("Elapsed time:"), sizerLabels
);
198 if ( style
& wxPD_ESTIMATED_TIME
)
202 m_estimated
= CreateLabel(_("Estimated time:"), sizerLabels
);
205 if ( style
& wxPD_REMAINING_TIME
)
209 m_remaining
= CreateLabel(_("Remaining time:"), sizerLabels
);
211 sizerTop
->Add(sizerLabels
, 0, wxALIGN_CENTER_HORIZONTAL
| wxTOP
, LAYOUT_MARGIN
);
213 if ( nTimeLabels
> 0 )
215 // set it to the current time
216 m_timeStart
= wxGetCurrentTime();
219 #if defined(__SMARTPHONE__)
220 if ( m_hasSkipButton
)
221 SetRightMenu(wxID_SKIP
, _("Skip"));
222 if ( m_hasAbortButton
)
223 SetLeftMenu(wxID_CANCEL
);
228 wxBoxSizer
*buttonSizer
= new wxBoxSizer(wxHORIZONTAL
);
230 // Windows dialogs usually have buttons in the lower right corner
231 const int sizerFlags
=
232 #if defined(__WXMSW__) || defined(__WXPM__)
233 wxALIGN_RIGHT
| wxALL
235 wxALIGN_CENTER_HORIZONTAL
| wxBOTTOM
| wxTOP
239 if ( m_hasSkipButton
)
241 m_btnSkip
= new wxButton(this, wxID_SKIP
, _("&Skip"));
243 buttonSizer
->Add(m_btnSkip
, 0, sizerFlags
, LAYOUT_MARGIN
);
246 if ( m_hasAbortButton
)
248 m_btnAbort
= new wxButton(this, wxID_CANCEL
);
250 buttonSizer
->Add(m_btnAbort
, 0, sizerFlags
, LAYOUT_MARGIN
);
253 sizerTop
->Add(buttonSizer
, 0, sizerFlags
, LAYOUT_MARGIN
);
254 #endif // __SMARTPHONE__/!__SMARTPHONE__
256 SetSizerAndFit(sizerTop
);
258 Centre(wxCENTER_FRAME
| wxBOTH
);
260 if ( style
& wxPD_APP_MODAL
)
262 m_winDisabler
= new wxWindowDisabler(this);
267 m_parentTop
->Disable();
268 m_winDisabler
= NULL
;
274 // this one can be initialized even if the others are unknown for now
276 // NB: do it after calling Layout() to keep the labels correctly aligned
279 SetTimeLabel(0, m_elapsed
);
286 wxProgressDialog::CreateLabel(const wxString
& text
, wxSizer
*sizer
)
288 wxStaticText
*label
= new wxStaticText(this, wxID_ANY
, text
);
289 wxStaticText
*value
= new wxStaticText(this, wxID_ANY
, _("unknown"));
291 // select placement most native or nice on target GUI
292 #if defined(__SMARTPHONE__)
293 // value and time to the left in two rows
294 sizer
->Add(label
, 1, wxALIGN_LEFT
);
295 sizer
->Add(value
, 1, wxALIGN_LEFT
);
296 #elif defined(__WXMSW__) || defined(__WXPM__) || defined(__WXMAC__) || defined(__WXGTK20__)
297 // value and time centered in one row
298 sizer
->Add(label
, 1, wxLARGESMALL(wxALIGN_RIGHT
,wxALIGN_LEFT
) | wxTOP
| wxRIGHT
, LAYOUT_MARGIN
);
299 sizer
->Add(value
, 1, wxALIGN_LEFT
| wxTOP
, LAYOUT_MARGIN
);
301 // value and time to the right in one row
303 sizer
->Add(value
, 0, wxLEFT
, LAYOUT_MARGIN
);
309 // ----------------------------------------------------------------------------
310 // wxProgressDialog operations
311 // ----------------------------------------------------------------------------
314 wxProgressDialog::Update(int value
, const wxString
& newmsg
, bool *skip
)
316 wxASSERT_MSG( value
== -1 || m_gauge
, wxT("cannot update non existent dialog") );
322 wxASSERT_MSG( value
<= m_maximum
, wxT("invalid progress value") );
325 m_gauge
->SetValue(value
);
327 UpdateMessage(newmsg
);
329 if ( (m_elapsed
|| m_remaining
|| m_estimated
) && (value
!= 0) )
331 unsigned long elapsed
= wxGetCurrentTime() - m_timeStart
;
332 if ( m_last_timeupdate
< elapsed
333 || value
== m_maximum
336 m_last_timeupdate
= elapsed
;
337 unsigned long estimated
= m_break
+
338 (unsigned long)(( (double) (elapsed
-m_break
) * m_maximum
) / ((double)value
)) ;
339 if ( estimated
> m_display_estimated
345 else if ( estimated
< m_display_estimated
355 if ( m_ctdelay
>= m_delay
// enough confirmations for a higher value
356 || m_ctdelay
<= (m_delay
*-1) // enough confirmations for a lower value
357 || value
== m_maximum
// to stay consistent
358 || elapsed
> m_display_estimated
// to stay consistent
359 || ( elapsed
> 0 && elapsed
< 4 ) // additional updates in the beginning
362 m_display_estimated
= estimated
;
367 long display_remaining
= m_display_estimated
- elapsed
;
368 if ( display_remaining
< 0 )
370 display_remaining
= 0;
373 SetTimeLabel(elapsed
, m_elapsed
);
374 SetTimeLabel(m_display_estimated
, m_estimated
);
375 SetTimeLabel(display_remaining
, m_remaining
);
378 if ( value
== m_maximum
)
380 if ( m_state
== Finished
)
382 // ignore multiple calls to Update(m_maximum): it may sometimes be
383 // troublesome to ensure that Update() is not called twice with the
384 // same value (e.g. because of the rounding errors) and if we don't
385 // return now we're going to generate asserts below
389 // so that we return true below and that out [Cancel] handler knew what
392 if( !HasFlag(wxPD_AUTO_HIDE
) )
396 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
400 if ( newmsg
.empty() )
402 // also provide the finishing message if the application didn't
403 m_msg
->SetLabel(_("Done."));
406 wxCHECK_MSG(wxEventLoopBase::GetActive(), false,
407 "wxProgressDialog::Update needs a running event loop");
409 // allow the window to repaint:
410 // NOTE: since we yield only for UI events with this call, there
411 // should be no side-effects
412 wxEventLoopBase::GetActive()->YieldFor(wxEVT_CATEGORY_UI
);
414 // NOTE: this call results in a new event loop being created
415 // and to a call to ProcessPendingEvents() (which may generate
416 // unwanted re-entrancies).
421 // reenable other windows before hiding this one because otherwise
422 // Windows wouldn't give the focus back to the window which had
423 // been previously focused because it would still be disabled
424 ReenableOtherWindows();
429 else // not at maximum yet
431 return DoAfterUpdate(skip
);
434 // update the display in case yielding above didn't do it
437 return m_state
!= Canceled
;
440 bool wxProgressDialog::Pulse(const wxString
& newmsg
, bool *skip
)
442 wxASSERT_MSG( m_gauge
, wxT("cannot update non existent dialog") );
444 // show a bit of progress
447 UpdateMessage(newmsg
);
449 if (m_elapsed
|| m_remaining
|| m_estimated
)
451 unsigned long elapsed
= wxGetCurrentTime() - m_timeStart
;
453 SetTimeLabel(elapsed
, m_elapsed
);
454 SetTimeLabel((unsigned long)-1, m_estimated
);
455 SetTimeLabel((unsigned long)-1, m_remaining
);
458 return DoAfterUpdate(skip
);
461 bool wxProgressDialog::DoAfterUpdate(bool *skip
)
463 wxCHECK_MSG(wxEventLoopBase::GetActive(), false,
464 "wxProgressDialog::DoAfterUpdate needs a running event loop");
466 // we have to yield because not only we want to update the display but
467 // also to process the clicks on the cancel and skip buttons
468 // NOTE: using YieldFor() this call shouldn't give re-entrancy problems
469 // for event handlers not interested to UI/user-input events.
470 wxEventLoopBase::GetActive()->YieldFor(wxEVT_CATEGORY_UI
|wxEVT_CATEGORY_USER_INPUT
);
474 if ( m_skip
&& skip
&& !*skip
)
481 return m_state
!= Canceled
;
484 void wxProgressDialog::Resume()
487 m_ctdelay
= m_delay
; // force an update of the elapsed/estimated/remaining time
488 m_break
+= wxGetCurrentTime()-m_timeStop
;
495 bool wxProgressDialog::Show( bool show
)
497 // reenable other windows before hiding this one because otherwise
498 // Windows wouldn't give the focus back to the window which had
499 // been previously focused because it would still be disabled
501 ReenableOtherWindows();
503 return wxDialog::Show(show
);
506 int wxProgressDialog::GetValue() const
509 return m_gauge
->GetValue();
513 int wxProgressDialog::GetRange() const
516 return m_gauge
->GetRange();
520 wxString
wxProgressDialog::GetMessage() const
522 return m_msg
->GetLabel();
525 // ----------------------------------------------------------------------------
527 // ----------------------------------------------------------------------------
529 void wxProgressDialog::OnCancel(wxCommandEvent
& event
)
531 if ( m_state
== Finished
)
533 // this means that the count down is already finished and we're being
534 // shown as a modal dialog - so just let the default handler do the job
539 // request to cancel was received, the next time Update() is called we
543 // update the buttons state immediately so that the user knows that the
544 // request has been noticed
548 // save the time when the dialog was stopped
549 m_timeStop
= wxGetCurrentTime();
553 void wxProgressDialog::OnSkip(wxCommandEvent
& WXUNUSED(event
))
559 void wxProgressDialog::OnClose(wxCloseEvent
& event
)
561 if ( m_state
== Uncancelable
)
563 // can't close this dialog
566 else if ( m_state
== Finished
)
568 // let the default handler close the window as we already terminated
573 // next Update() will notice it
578 m_timeStop
= wxGetCurrentTime();
582 // ----------------------------------------------------------------------------
584 // ----------------------------------------------------------------------------
586 wxProgressDialog::~wxProgressDialog()
588 // normally this should have been already done, but just in case
589 ReenableOtherWindows();
592 void wxProgressDialog::ReenableOtherWindows()
594 if ( HasFlag(wxPD_APP_MODAL
) )
596 delete m_winDisabler
;
597 m_winDisabler
= NULL
;
602 m_parentTop
->Enable();
606 // ----------------------------------------------------------------------------
608 // ----------------------------------------------------------------------------
610 static void SetTimeLabel(unsigned long val
, wxStaticText
*label
)
616 if (val
!= (unsigned long)-1)
618 unsigned long hours
= val
/ 3600;
619 unsigned long minutes
= (val
% 3600) / 60;
620 unsigned long seconds
= val
% 60;
621 s
.Printf(wxT("%lu:%02lu:%02lu"), hours
, minutes
, seconds
);
628 if ( s
!= label
->GetLabel() )
633 void wxProgressDialog::EnableSkip(bool enable
)
637 #ifdef __SMARTPHONE__
639 SetRightMenu(wxID_SKIP
, _("Skip"));
644 m_btnSkip
->Enable(enable
);
649 void wxProgressDialog::EnableAbort(bool enable
)
653 #ifdef __SMARTPHONE__
655 SetLeftMenu(wxID_CANCEL
); // stock buttons makes Cancel label
660 m_btnAbort
->Enable(enable
);
665 void wxProgressDialog::EnableClose()
669 #ifdef __SMARTPHONE__
670 SetLeftMenu(wxID_CANCEL
, _("Close"));
674 m_btnAbort
->Enable();
675 m_btnAbort
->SetLabel(_("Close"));
681 void wxProgressDialog::UpdateMessage(const wxString
&newmsg
)
683 wxCHECK_RET(wxEventLoopBase::GetActive(),
684 "wxProgressDialog::UpdateMessage needs a running event loop");
686 if ( !newmsg
.empty() && newmsg
!= m_msg
->GetLabel() )
688 m_msg
->SetLabel(newmsg
);
690 // allow the window to repaint:
691 // NOTE: since we yield only for UI events with this call, there
692 // should be no side-effects
693 wxEventLoopBase::GetActive()->YieldFor(wxEVT_CATEGORY_UI
);
697 #endif // wxUSE_PROGRESSDLG