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"
43 #include "wx/progdlg.h"
45 // ---------------------------------------------------------------------------
47 // ---------------------------------------------------------------------------
49 /* Macro for avoiding #ifdefs when value have to be different depending on size of
50 device we display on - take it from something like wxDesktopPolicy in the future
53 #if defined(__SMARTPHONE__)
54 #define wxLARGESMALL(large,small) small
56 #define wxLARGESMALL(large,small) large
59 // ----------------------------------------------------------------------------
61 // ----------------------------------------------------------------------------
63 #define LAYOUT_MARGIN wxLARGESMALL(8,2)
65 static const int wxID_SKIP
= 32000; // whatever
67 // ----------------------------------------------------------------------------
69 // ----------------------------------------------------------------------------
71 // update the label to show the given time (in seconds)
72 static void SetTimeLabel(unsigned long val
, wxStaticText
*label
);
74 // ----------------------------------------------------------------------------
76 // ----------------------------------------------------------------------------
78 BEGIN_EVENT_TABLE(wxProgressDialog
, wxDialog
)
79 EVT_BUTTON(wxID_CANCEL
, wxProgressDialog::OnCancel
)
80 EVT_BUTTON(wxID_SKIP
, wxProgressDialog::OnSkip
)
82 EVT_CLOSE(wxProgressDialog::OnClose
)
85 IMPLEMENT_CLASS(wxProgressDialog
, wxDialog
)
87 // ============================================================================
88 // wxProgressDialog implementation
89 // ============================================================================
91 // ----------------------------------------------------------------------------
92 // wxProgressDialog creation
93 // ----------------------------------------------------------------------------
95 wxProgressDialog::wxProgressDialog(const wxString
& title
,
96 const wxString
& message
,
100 : wxDialog(GetParentForModalDialog(parent
), wxID_ANY
, title
),
103 m_hasAbortButton(false),
104 m_hasSkipButton(false)
106 // we may disappear at any moment, let the others know about it
107 SetExtraStyle(GetExtraStyle() | wxWS_EX_TRANSIENT
);
108 m_windowStyle
|= style
;
110 m_hasAbortButton
= (style
& wxPD_CAN_ABORT
) != 0;
111 m_hasSkipButton
= (style
& wxPD_CAN_SKIP
) != 0;
113 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
114 // we have to remove the "Close" button from the title bar then as it is
115 // confusing to have it - it doesn't work anyhow
117 // FIXME: should probably have a (extended?) window style for this
118 if ( !m_hasAbortButton
)
120 EnableCloseButton(false);
124 #if defined(__SMARTPHONE__)
128 m_state
= m_hasAbortButton
? Continue
: Uncancelable
;
131 #if defined(__WXMSW__) || defined(__WXPM__)
132 // we can't have values > 65,536 in the progress control under Windows, so
133 // scale everything down
134 m_factor
= m_maximum
/ 65536 + 1;
135 m_maximum
/= m_factor
;
138 m_parentTop
= wxGetTopLevelParent(parent
);
141 dc
.SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
142 wxCoord widthText
= 0;
143 dc
.GetTextExtent(message
, &widthText
, NULL
, NULL
, NULL
, NULL
);
145 // top-level sizerTop
146 wxSizer
* const sizerTop
= new wxBoxSizer(wxVERTICAL
);
148 m_msg
= new wxStaticText(this, wxID_ANY
, message
);
149 sizerTop
->Add(m_msg
, 0, wxLEFT
| wxTOP
, 2*LAYOUT_MARGIN
);
151 wxSize sizeLabel
= m_msg
->GetSize();
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( !(GetWindowStyle() & 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."));
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
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 // ----------------------------------------------------------------------------
493 // ----------------------------------------------------------------------------
495 void wxProgressDialog::OnCancel(wxCommandEvent
& event
)
497 if ( m_state
== Finished
)
499 // this means that the count down is already finished and we're being
500 // shown as a modal dialog - so just let the default handler do the job
505 // request to cancel was received, the next time Update() is called we
509 // update the buttons state immediately so that the user knows that the
510 // request has been noticed
514 // save the time when the dialog was stopped
515 m_timeStop
= wxGetCurrentTime();
519 void wxProgressDialog::OnSkip(wxCommandEvent
& WXUNUSED(event
))
525 void wxProgressDialog::OnClose(wxCloseEvent
& event
)
527 if ( m_state
== Uncancelable
)
529 // can't close this dialog
532 else if ( m_state
== Finished
)
534 // let the default handler close the window as we already terminated
539 // next Update() will notice it
544 m_timeStop
= wxGetCurrentTime();
548 // ----------------------------------------------------------------------------
550 // ----------------------------------------------------------------------------
552 wxProgressDialog::~wxProgressDialog()
554 // normally this should have been already done, but just in case
555 ReenableOtherWindows();
558 void wxProgressDialog::ReenableOtherWindows()
560 if ( GetWindowStyle() & wxPD_APP_MODAL
)
562 delete m_winDisabler
;
563 m_winDisabler
= (wxWindowDisabler
*)NULL
;
568 m_parentTop
->Enable();
572 // ----------------------------------------------------------------------------
574 // ----------------------------------------------------------------------------
576 static void SetTimeLabel(unsigned long val
, wxStaticText
*label
)
582 if (val
!= (unsigned long)-1)
584 unsigned long hours
= val
/ 3600;
585 unsigned long minutes
= (val
% 3600) / 60;
586 unsigned long seconds
= val
% 60;
587 s
.Printf(wxT("%lu:%02lu:%02lu"), hours
, minutes
, seconds
);
594 if ( s
!= label
->GetLabel() )
599 void wxProgressDialog::EnableSkip(bool enable
)
603 #ifdef __SMARTPHONE__
605 SetRightMenu(wxID_SKIP
, _("Skip"));
610 m_btnSkip
->Enable(enable
);
615 void wxProgressDialog::EnableAbort(bool enable
)
619 #ifdef __SMARTPHONE__
621 SetLeftMenu(wxID_CANCEL
); // stock buttons makes Cancel label
626 m_btnAbort
->Enable(enable
);
631 void wxProgressDialog::EnableClose()
635 #ifdef __SMARTPHONE__
636 SetLeftMenu(wxID_CANCEL
, _("Close"));
640 m_btnAbort
->Enable();
641 m_btnAbort
->SetLabel(_("Close"));
647 void wxProgressDialog::UpdateMessage(const wxString
&newmsg
)
649 if ( !newmsg
.empty() && newmsg
!= m_msg
->GetLabel() )
651 m_msg
->SetLabel(newmsg
);
657 #endif // wxUSE_PROGRESSDLG