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"
46 // ---------------------------------------------------------------------------
48 // ---------------------------------------------------------------------------
50 /* Macro for avoiding #ifdefs when value have to be different depending on size of
51 device we display on - take it from something like wxDesktopPolicy in the future
54 #if defined(__SMARTPHONE__)
55 #define wxLARGESMALL(large,small) small
57 #define wxLARGESMALL(large,small) large
60 // ----------------------------------------------------------------------------
62 // ----------------------------------------------------------------------------
64 #define LAYOUT_MARGIN wxLARGESMALL(8,2)
66 static const int wxID_SKIP
= 32000; // whatever
68 // ----------------------------------------------------------------------------
70 // ----------------------------------------------------------------------------
72 // update the label to show the given time (in seconds)
73 static void SetTimeLabel(unsigned long val
, wxStaticText
*label
);
75 // ----------------------------------------------------------------------------
77 // ----------------------------------------------------------------------------
79 BEGIN_EVENT_TABLE(wxProgressDialog
, wxDialog
)
80 EVT_BUTTON(wxID_CANCEL
, wxProgressDialog::OnCancel
)
81 EVT_BUTTON(wxID_SKIP
, wxProgressDialog::OnSkip
)
83 EVT_CLOSE(wxProgressDialog::OnClose
)
86 IMPLEMENT_CLASS(wxProgressDialog
, wxDialog
)
88 // ============================================================================
89 // wxProgressDialog implementation
90 // ============================================================================
92 // ----------------------------------------------------------------------------
93 // wxProgressDialog creation
94 // ----------------------------------------------------------------------------
96 wxProgressDialog::wxProgressDialog(const wxString
& title
,
97 const wxString
& message
,
101 : wxDialog(GetParentForModalDialog(parent
), wxID_ANY
, title
),
104 m_hasAbortButton(false),
105 m_hasSkipButton(false)
107 // we may disappear at any moment, let the others know about it
108 SetExtraStyle(GetExtraStyle() | wxWS_EX_TRANSIENT
);
109 m_windowStyle
|= style
;
111 m_hasAbortButton
= (style
& wxPD_CAN_ABORT
) != 0;
112 m_hasSkipButton
= (style
& wxPD_CAN_SKIP
) != 0;
114 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
115 // we have to remove the "Close" button from the title bar then as it is
116 // confusing to have it - it doesn't work anyhow
118 // FIXME: should probably have a (extended?) window style for this
119 if ( !m_hasAbortButton
)
121 EnableCloseButton(false);
125 #if defined(__SMARTPHONE__)
129 m_state
= m_hasAbortButton
? Continue
: Uncancelable
;
132 #if defined(__WXMSW__) || defined(__WXPM__)
133 // we can't have values > 65,536 in the progress control under Windows, so
134 // scale everything down
135 m_factor
= m_maximum
/ 65536 + 1;
136 m_maximum
/= m_factor
;
139 m_parentTop
= wxGetTopLevelParent(parent
);
142 dc
.SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
143 wxCoord widthText
= 0;
144 dc
.GetTextExtent(message
, &widthText
, NULL
, NULL
, NULL
, NULL
);
146 // top-level sizerTop
147 wxSizer
* const sizerTop
= new wxBoxSizer(wxVERTICAL
);
149 m_msg
= new wxStaticText(this, wxID_ANY
, message
);
150 sizerTop
->Add(m_msg
, 0, wxLEFT
| wxTOP
, 2*LAYOUT_MARGIN
);
154 int gauge_style
= wxGA_HORIZONTAL
;
155 if ( style
& wxPD_SMOOTH
)
156 gauge_style
|= wxGA_SMOOTH
;
157 m_gauge
= new wxGauge
163 // make the progress bar sufficiently long
164 wxSize(wxMin(wxGetClientDisplayRect().width
/3, 300), -1),
168 sizerTop
->Add(m_gauge
, 0, wxLEFT
| wxRIGHT
| wxTOP
| wxEXPAND
, 2*LAYOUT_MARGIN
);
169 m_gauge
->SetValue(0);
176 // create the estimated/remaining/total time zones if requested
180 m_display_estimated
=
185 // also count how many labels we really have
186 size_t nTimeLabels
= 0;
188 wxSizer
* const sizerLabels
= new wxFlexGridSizer(2);
190 if ( style
& wxPD_ELAPSED_TIME
)
194 m_elapsed
= CreateLabel(_("Elapsed time:"), sizerLabels
);
197 if ( style
& wxPD_ESTIMATED_TIME
)
201 m_estimated
= CreateLabel(_("Estimated time:"), sizerLabels
);
204 if ( style
& wxPD_REMAINING_TIME
)
208 m_remaining
= CreateLabel(_("Remaining time:"), sizerLabels
);
210 sizerTop
->Add(sizerLabels
, 0, wxALIGN_CENTER_HORIZONTAL
| wxTOP
, LAYOUT_MARGIN
);
212 if ( nTimeLabels
> 0 )
214 // set it to the current time
215 m_timeStart
= wxGetCurrentTime();
218 #if defined(__SMARTPHONE__)
219 if ( m_hasSkipButton
)
220 SetRightMenu(wxID_SKIP
, _("Skip"));
221 if ( m_hasAbortButton
)
222 SetLeftMenu(wxID_CANCEL
);
227 wxBoxSizer
*buttonSizer
= new wxBoxSizer(wxHORIZONTAL
);
229 // Windows dialogs usually have buttons in the lower right corner
230 const int sizerFlags
=
231 #if defined(__WXMSW__) || defined(__WXPM__)
232 wxALIGN_RIGHT
| wxALL
234 wxALIGN_CENTER_HORIZONTAL
| wxBOTTOM
| wxTOP
238 if ( m_hasSkipButton
)
240 m_btnSkip
= new wxButton(this, wxID_SKIP
, _("&Skip"));
242 buttonSizer
->Add(m_btnSkip
, 0, sizerFlags
, LAYOUT_MARGIN
);
245 if ( m_hasAbortButton
)
247 m_btnAbort
= new wxButton(this, wxID_CANCEL
);
249 buttonSizer
->Add(m_btnAbort
, 0, sizerFlags
, LAYOUT_MARGIN
);
252 sizerTop
->Add(buttonSizer
, 0, sizerFlags
, LAYOUT_MARGIN
);
253 #endif // __SMARTPHONE__/!__SMARTPHONE__
255 SetSizerAndFit(sizerTop
);
257 Centre(wxCENTER_FRAME
| wxBOTH
);
259 if ( style
& wxPD_APP_MODAL
)
261 m_winDisabler
= new wxWindowDisabler(this);
266 m_parentTop
->Disable();
267 m_winDisabler
= NULL
;
273 // this one can be initialized even if the others are unknown for now
275 // NB: do it after calling Layout() to keep the labels correctly aligned
278 SetTimeLabel(0, m_elapsed
);
285 wxProgressDialog::CreateLabel(const wxString
& text
, wxSizer
*sizer
)
287 wxStaticText
*label
= new wxStaticText(this, wxID_ANY
, text
);
288 wxStaticText
*value
= new wxStaticText(this, wxID_ANY
, _("unknown"));
290 // select placement most native or nice on target GUI
291 #if defined(__SMARTPHONE__)
292 // value and time to the left in two rows
293 sizer
->Add(label
, 1, wxALIGN_LEFT
);
294 sizer
->Add(value
, 1, wxALIGN_LEFT
);
295 #elif defined(__WXMSW__) || defined(__WXPM__) || defined(__WXMAC__) || defined(__WXGTK20__)
296 // value and time centered in one row
297 sizer
->Add(label
, 1, wxLARGESMALL(wxALIGN_RIGHT
,wxALIGN_LEFT
) | wxTOP
| wxRIGHT
, LAYOUT_MARGIN
);
298 sizer
->Add(value
, 1, wxALIGN_LEFT
| wxTOP
, LAYOUT_MARGIN
);
300 // value and time to the right in one row
302 sizer
->Add(value
, 0, wxLEFT
, LAYOUT_MARGIN
);
308 // ----------------------------------------------------------------------------
309 // wxProgressDialog operations
310 // ----------------------------------------------------------------------------
313 wxProgressDialog::Update(int value
, const wxString
& newmsg
, bool *skip
)
315 wxASSERT_MSG( value
== -1 || m_gauge
, wxT("cannot update non existent dialog") );
321 wxASSERT_MSG( value
<= m_maximum
, wxT("invalid progress value") );
324 m_gauge
->SetValue(value
);
326 UpdateMessage(newmsg
);
328 if ( (m_elapsed
|| m_remaining
|| m_estimated
) && (value
!= 0) )
330 unsigned long elapsed
= wxGetCurrentTime() - m_timeStart
;
331 if ( m_last_timeupdate
< elapsed
332 || value
== m_maximum
335 m_last_timeupdate
= elapsed
;
336 unsigned long estimated
= m_break
+
337 (unsigned long)(( (double) (elapsed
-m_break
) * m_maximum
) / ((double)value
)) ;
338 if ( estimated
> m_display_estimated
344 else if ( estimated
< m_display_estimated
354 if ( m_ctdelay
>= m_delay
// enough confirmations for a higher value
355 || m_ctdelay
<= (m_delay
*-1) // enough confirmations for a lower value
356 || value
== m_maximum
// to stay consistent
357 || elapsed
> m_display_estimated
// to stay consistent
358 || ( elapsed
> 0 && elapsed
< 4 ) // additional updates in the beginning
361 m_display_estimated
= estimated
;
366 long display_remaining
= m_display_estimated
- elapsed
;
367 if ( display_remaining
< 0 )
369 display_remaining
= 0;
372 SetTimeLabel(elapsed
, m_elapsed
);
373 SetTimeLabel(m_display_estimated
, m_estimated
);
374 SetTimeLabel(display_remaining
, m_remaining
);
377 if ( value
== m_maximum
)
379 if ( m_state
== Finished
)
381 // ignore multiple calls to Update(m_maximum): it may sometimes be
382 // troublesome to ensure that Update() is not called twice with the
383 // same value (e.g. because of the rounding errors) and if we don't
384 // return now we're going to generate asserts below
388 // so that we return true below and that out [Cancel] handler knew what
391 if( !HasFlag(wxPD_AUTO_HIDE
) )
395 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
399 if ( newmsg
.empty() )
401 // also provide the finishing message if the application didn't
402 m_msg
->SetLabel(_("Done."));
405 wxTheApp
->YieldFor(wxEVT_CATEGORY_UI
);
411 // reenable other windows before hiding this one because otherwise
412 // Windows wouldn't give the focus back to the window which had
413 // been previously focused because it would still be disabled
414 ReenableOtherWindows();
419 else // not at maximum yet
421 return DoAfterUpdate(skip
);
424 // update the display in case yielding above didn't do it
427 return m_state
!= Canceled
;
430 bool wxProgressDialog::Pulse(const wxString
& newmsg
, bool *skip
)
432 wxASSERT_MSG( m_gauge
, wxT("cannot update non existent dialog") );
434 // show a bit of progress
437 UpdateMessage(newmsg
);
439 if (m_elapsed
|| m_remaining
|| m_estimated
)
441 unsigned long elapsed
= wxGetCurrentTime() - m_timeStart
;
443 SetTimeLabel(elapsed
, m_elapsed
);
444 SetTimeLabel((unsigned long)-1, m_estimated
);
445 SetTimeLabel((unsigned long)-1, m_remaining
);
448 return DoAfterUpdate(skip
);
451 bool wxProgressDialog::DoAfterUpdate(bool *skip
)
453 // we have to yield because not only we want to update the display but
454 // also to process the clicks on the cancel and skip buttons
455 wxTheApp
->YieldFor(wxEVT_CATEGORY_UI
|wxEVT_CATEGORY_USER_INPUT
);
459 if ( m_skip
&& skip
&& !*skip
)
466 return m_state
!= Canceled
;
469 void wxProgressDialog::Resume()
472 m_ctdelay
= m_delay
; // force an update of the elapsed/estimated/remaining time
473 m_break
+= wxGetCurrentTime()-m_timeStop
;
480 bool wxProgressDialog::Show( bool show
)
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
486 ReenableOtherWindows();
488 return wxDialog::Show(show
);
491 int wxProgressDialog::GetValue() const
494 return m_gauge
->GetValue();
498 int wxProgressDialog::GetRange() const
501 return m_gauge
->GetRange();
505 wxString
wxProgressDialog::GetMessage() const
507 return m_msg
->GetLabel();
510 // ----------------------------------------------------------------------------
512 // ----------------------------------------------------------------------------
514 void wxProgressDialog::OnCancel(wxCommandEvent
& event
)
516 if ( m_state
== Finished
)
518 // this means that the count down is already finished and we're being
519 // shown as a modal dialog - so just let the default handler do the job
524 // request to cancel was received, the next time Update() is called we
528 // update the buttons state immediately so that the user knows that the
529 // request has been noticed
533 // save the time when the dialog was stopped
534 m_timeStop
= wxGetCurrentTime();
538 void wxProgressDialog::OnSkip(wxCommandEvent
& WXUNUSED(event
))
544 void wxProgressDialog::OnClose(wxCloseEvent
& event
)
546 if ( m_state
== Uncancelable
)
548 // can't close this dialog
551 else if ( m_state
== Finished
)
553 // let the default handler close the window as we already terminated
558 // next Update() will notice it
563 m_timeStop
= wxGetCurrentTime();
567 // ----------------------------------------------------------------------------
569 // ----------------------------------------------------------------------------
571 wxProgressDialog::~wxProgressDialog()
573 // normally this should have been already done, but just in case
574 ReenableOtherWindows();
577 void wxProgressDialog::ReenableOtherWindows()
579 if ( HasFlag(wxPD_APP_MODAL
) )
581 delete m_winDisabler
;
582 m_winDisabler
= NULL
;
587 m_parentTop
->Enable();
591 // ----------------------------------------------------------------------------
593 // ----------------------------------------------------------------------------
595 static void SetTimeLabel(unsigned long val
, wxStaticText
*label
)
601 if (val
!= (unsigned long)-1)
603 unsigned long hours
= val
/ 3600;
604 unsigned long minutes
= (val
% 3600) / 60;
605 unsigned long seconds
= val
% 60;
606 s
.Printf(wxT("%lu:%02lu:%02lu"), hours
, minutes
, seconds
);
613 if ( s
!= label
->GetLabel() )
618 void wxProgressDialog::EnableSkip(bool enable
)
622 #ifdef __SMARTPHONE__
624 SetRightMenu(wxID_SKIP
, _("Skip"));
629 m_btnSkip
->Enable(enable
);
634 void wxProgressDialog::EnableAbort(bool enable
)
638 #ifdef __SMARTPHONE__
640 SetLeftMenu(wxID_CANCEL
); // stock buttons makes Cancel label
645 m_btnAbort
->Enable(enable
);
650 void wxProgressDialog::EnableClose()
654 #ifdef __SMARTPHONE__
655 SetLeftMenu(wxID_CANCEL
, _("Close"));
659 m_btnAbort
->Enable();
660 m_btnAbort
->SetLabel(_("Close"));
666 void wxProgressDialog::UpdateMessage(const wxString
&newmsg
)
668 if ( !newmsg
.empty() && newmsg
!= m_msg
->GetLabel() )
670 m_msg
->SetLabel(newmsg
);
672 Fit(); // adapt to the new label size
674 wxTheApp
->YieldFor(wxEVT_CATEGORY_UI
);
678 #endif // wxUSE_PROGRESSDLG