1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxProgressDialog class
4 // Author: Karsten Ballüder
8 // Copyright: (c) Karsten Ballüder
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
21 #pragma implementation "progdlgg.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
36 #include "wx/button.h"
37 #include "wx/stattext.h"
42 #include "wx/settings.h"
43 #include "wx/dcclient.h"
47 #include "wx/generic/progdlgg.h"
49 // ---------------------------------------------------------------------------
51 // ---------------------------------------------------------------------------
53 /* Macro for avoiding #ifdefs when value have to be different depending on size of
54 device we display on - take it from something like wxDesktopPolicy in the future
57 #if defined(__SMARTPHONE__)
58 #define wxLARGESMALL(large,small) small
60 #define wxLARGESMALL(large,small) large
63 // ----------------------------------------------------------------------------
65 // ----------------------------------------------------------------------------
67 #define LAYOUT_MARGIN wxLARGESMALL(8,2)
69 static const int wxID_SKIP
= 32000; // whatever
71 // ----------------------------------------------------------------------------
73 // ----------------------------------------------------------------------------
75 // update the label to show the given time (in seconds)
76 static void SetTimeLabel(unsigned long val
, wxStaticText
*label
);
78 // ----------------------------------------------------------------------------
80 // ----------------------------------------------------------------------------
82 BEGIN_EVENT_TABLE(wxProgressDialog
, wxDialog
)
83 EVT_BUTTON(wxID_CANCEL
, wxProgressDialog::OnCancel
)
84 EVT_BUTTON(wxID_SKIP
, wxProgressDialog::OnSkip
)
86 EVT_CLOSE(wxProgressDialog::OnClose
)
89 IMPLEMENT_CLASS(wxProgressDialog
, wxDialog
)
91 // ============================================================================
92 // wxProgressDialog implementation
93 // ============================================================================
95 // ----------------------------------------------------------------------------
96 // wxProgressDialog creation
97 // ----------------------------------------------------------------------------
99 wxProgressDialog::wxProgressDialog(wxString
const &title
,
100 wxString
const &message
,
104 : wxDialog(parent
, wxID_ANY
, title
),
107 m_hasAbortButton(false),
108 m_hasSkipButton(false)
110 // we may disappear at any moment, let the others know about it
111 SetExtraStyle(GetExtraStyle() | wxWS_EX_TRANSIENT
);
112 m_windowStyle
|= style
;
114 m_hasAbortButton
= (style
& wxPD_CAN_ABORT
) != 0;
115 m_hasSkipButton
= (style
& wxPD_CAN_SKIP
) != 0;
117 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
118 // we have to remove the "Close" button from the title bar then as it is
119 // confusing to have it - it doesn't work anyhow
121 // FIXME: should probably have a (extended?) window style for this
122 if ( !m_hasAbortButton
)
124 EnableCloseButton(false);
128 #if defined(__SMARTPHONE__)
132 m_state
= m_hasAbortButton
? Continue
: Uncancelable
;
135 #if defined(__WXMSW__) || defined(__WXPM__)
136 // we can't have values > 65,536 in the progress control under Windows, so
137 // scale everything down
138 m_factor
= m_maximum
/ 65536 + 1;
139 m_maximum
/= m_factor
;
142 m_parentTop
= wxGetTopLevelParent(parent
);
145 dc
.SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
147 dc
.GetTextExtent(message
, &widthText
, NULL
, NULL
, NULL
, NULL
);
149 wxBoxSizer
*sizer
= new wxBoxSizer(wxVERTICAL
);
151 m_msg
= new wxStaticText(this, wxID_ANY
, message
);
152 sizer
->Add(m_msg
, 0, wxLEFT
| wxTOP
, 2*LAYOUT_MARGIN
);
155 sizeLabel
= m_msg
->GetSize();
156 sizeDlg
.y
= 2*LAYOUT_MARGIN
+ sizeLabel
.y
;
160 int gauge_style
= wxGA_HORIZONTAL
;
161 if ( ( style
& wxPD_SMOOTH
) == wxPD_SMOOTH
)
162 gauge_style
|= wxGA_SMOOTH
;
163 m_gauge
= new wxGauge(this, wxID_ANY
, m_maximum
,
164 wxDefaultPosition
, wxDefaultSize
,
167 sizer
->Add(m_gauge
, 0, wxLEFT
| wxRIGHT
| wxTOP
| wxEXPAND
, 2*LAYOUT_MARGIN
);
168 m_gauge
->SetValue(0);
170 wxSize sizeGauge
= m_gauge
->GetSize();
171 sizeDlg
.y
+= 2*LAYOUT_MARGIN
+ sizeGauge
.y
;
174 m_gauge
= (wxGauge
*)NULL
;
176 // create the estimated/remaining/total time zones if requested
177 m_elapsed
= m_estimated
= m_remaining
= (wxStaticText
*)NULL
;
178 m_display_estimated
= m_last_timeupdate
= m_break
= 0;
181 // if we are going to have at least one label, remmeber it in this var
182 wxStaticText
*label
= NULL
;
184 // also count how many labels we really have
185 size_t nTimeLabels
= 0;
187 if ( style
& wxPD_ELAPSED_TIME
)
192 m_elapsed
= CreateLabel(_("Elapsed time : "), sizer
);
195 if ( style
& wxPD_ESTIMATED_TIME
)
200 m_estimated
= CreateLabel(_("Estimated time : "), sizer
);
203 if ( style
& wxPD_REMAINING_TIME
)
208 m_remaining
= CreateLabel(_("Remaining time : "), sizer
);
211 if ( nTimeLabels
> 0 )
213 // set it to the current time
214 m_timeStart
= wxGetCurrentTime();
215 sizeDlg
.y
+= nTimeLabels
* (label
->GetSize().y
+ LAYOUT_MARGIN
);
218 #if defined(__SMARTPHONE__)
219 if ( m_hasSkipButton
)
220 SetRightMenu(wxID_SKIP
, _("Skip"));
221 if ( m_hasAbortButton
)
222 SetLeftMenu(wxID_CANCEL
);
224 m_btnAbort
= m_btnSkip
= (wxButton
*)NULL
;
225 bool sizeDlgModified
= false;
226 wxBoxSizer
*buttonSizer
= new wxBoxSizer(wxHORIZONTAL
);
228 const int sizerFlags
=
229 #if defined(__WXMSW__) || defined(__WXPM__)
230 wxALIGN_RIGHT
| wxALL
232 wxALIGN_CENTER_HORIZONTAL
| wxBOTTOM
| wxTOP
236 if ( m_hasSkipButton
)
238 m_btnSkip
= new wxButton(this, wxID_SKIP
, _("Skip"));
240 // Windows dialogs usually have buttons in the lower right corner
241 buttonSizer
->Add(m_btnSkip
, 0, sizerFlags
, LAYOUT_MARGIN
);
242 sizeDlg
.y
+= 2*LAYOUT_MARGIN
+ wxButton::GetDefaultSize().y
;
243 sizeDlgModified
= true;
246 if ( m_hasAbortButton
)
248 m_btnAbort
= new wxButton(this, wxID_CANCEL
);
250 // Windows dialogs usually have buttons in the lower right corner
251 buttonSizer
->Add(m_btnAbort
, 0, sizerFlags
, LAYOUT_MARGIN
);
253 sizeDlg
.y
+= 2*LAYOUT_MARGIN
+ wxButton::GetDefaultSize().y
;
256 sizer
->Add(buttonSizer
, 0, sizerFlags
, LAYOUT_MARGIN
);
257 #endif // __SMARTPHONE__/!__SMARTPHONE__
259 SetSizerAndFit(sizer
);
261 sizeDlg
.y
+= 2*LAYOUT_MARGIN
;
263 // try to make the dialog not square but rectangular of reasonabel width
264 sizeDlg
.x
= (wxCoord
)wxMax(widthText
, 4*sizeDlg
.y
/3);
267 SetClientSize(sizeDlg
);
269 Centre(wxCENTER_FRAME
| wxBOTH
);
271 if ( style
& wxPD_APP_MODAL
)
273 m_winDisabler
= new wxWindowDisabler(this);
278 m_parentTop
->Disable();
279 m_winDisabler
= NULL
;
285 // this one can be initialized even if the others are unknown for now
287 // NB: do it after calling Layout() to keep the labels correctly aligned
290 SetTimeLabel(0, m_elapsed
);
296 wxStaticText
*wxProgressDialog::CreateLabel(const wxString
& text
,
299 wxBoxSizer
*locsizer
= new wxBoxSizer(wxLARGESMALL(wxHORIZONTAL
,wxVERTICAL
));
301 wxStaticText
*dummy
= new wxStaticText(this, wxID_ANY
, text
);
302 wxStaticText
*label
= new wxStaticText(this, wxID_ANY
, _("unknown"));
304 // select placement most native or nice on target GUI
305 #if defined(__SMARTPHONE__)
306 // label and time to the left in two rows
307 locsizer
->Add(dummy
, 1, wxALIGN_LEFT
);
308 locsizer
->Add(label
, 1, wxALIGN_LEFT
);
309 sizer
->Add(locsizer
, 0, wxALIGN_LEFT
| wxTOP
| wxLEFT
, LAYOUT_MARGIN
);
310 #elif defined(__WXMSW__) || defined(__WXPM__) || defined(__WXMAC__)
311 // label and time centered in one row
312 locsizer
->Add(dummy
, 1, wxLARGESMALL(wxALIGN_RIGHT
,wxALIGN_LEFT
));
313 locsizer
->Add(label
, 1, wxALIGN_LEFT
| wxLEFT
, LAYOUT_MARGIN
);
314 sizer
->Add(locsizer
, 0, wxALIGN_CENTER_HORIZONTAL
| wxTOP
, LAYOUT_MARGIN
);
316 // label and time to the right in one row
317 sizer
->Add(locsizer
, 0, wxALIGN_RIGHT
| wxRIGHT
| wxTOP
, LAYOUT_MARGIN
);
318 locsizer
->Add(dummy
);
319 locsizer
->Add(label
, 0, wxLEFT
, LAYOUT_MARGIN
);
325 // ----------------------------------------------------------------------------
326 // wxProgressDialog operations
327 // ----------------------------------------------------------------------------
330 wxProgressDialog::Update(int value
, const wxString
& newmsg
, bool *skip
)
332 wxASSERT_MSG( value
== -1 || m_gauge
, wxT("cannot update non existent dialog") );
338 wxASSERT_MSG( value
<= m_maximum
, wxT("invalid progress value") );
340 // fill up the gauge if value == maximum because this means that the dialog
341 // is going to close and the gauge shouldn't be partly empty in this case
342 if ( m_gauge
&& value
<= m_maximum
)
344 m_gauge
->SetValue(value
== m_maximum
? value
: value
+ 1);
347 if ( !newmsg
.empty() && newmsg
!= m_msg
->GetLabel() )
349 m_msg
->SetLabel(newmsg
);
354 if ( (m_elapsed
|| m_remaining
|| m_estimated
) && (value
!= 0) )
356 unsigned long elapsed
= wxGetCurrentTime() - m_timeStart
;
357 if ( m_last_timeupdate
< elapsed
358 || value
== m_maximum
361 m_last_timeupdate
= elapsed
;
362 unsigned long estimated
= m_break
+
363 (unsigned long)(( (double) (elapsed
-m_break
) * m_maximum
) / ((double)value
)) ;
364 if ( estimated
> m_display_estimated
370 else if ( estimated
< m_display_estimated
380 if ( m_ctdelay
>= m_delay
// enough confirmations for a higher value
381 || m_ctdelay
<= (m_delay
*-1) // enough confirmations for a lower value
382 || value
== m_maximum
// to stay consistent
383 || elapsed
> m_display_estimated
// to stay consistent
384 || ( elapsed
> 0 && elapsed
< 4 ) // additional updates in the beginning
387 m_display_estimated
= estimated
;
392 long display_remaining
= m_display_estimated
- elapsed
;
393 if ( display_remaining
< 0 )
395 display_remaining
= 0;
398 SetTimeLabel(elapsed
, m_elapsed
);
399 SetTimeLabel(m_display_estimated
, m_estimated
);
400 SetTimeLabel(display_remaining
, m_remaining
);
403 if ( value
== m_maximum
)
405 if ( m_state
== Finished
)
407 // ignore multiple calls to Update(m_maximum): it may sometimes be
408 // troublesome to ensure that Update() is not called twice with the
409 // same value (e.g. because of the rounding errors) and if we don't
410 // return now we're going to generate asserts below
414 // so that we return true below and that out [Cancel] handler knew what
417 if( !(GetWindowStyle() & wxPD_AUTO_HIDE
) )
421 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
425 if ( !newmsg
.empty() )
427 // also provide the finishing message if the application didn't
428 m_msg
->SetLabel(_("Done."));
437 // reenable other windows before hiding this one because otherwise
438 // Windows wouldn't give the focus back to the window which had
439 // been previously focused because it would still be disabled
440 ReenableOtherWindows();
447 // we have to yield because not only we want to update the display but
448 // also to process the clicks on the cancel and skip buttons
451 if ( (m_skip
) && (skip
!= NULL
) && (*skip
== false) )
459 // update the display in case yielding above didn't do it
462 return m_state
!= Canceled
;
465 void wxProgressDialog::Resume()
468 m_ctdelay
= m_delay
; // force an update of the elapsed/estimated/remaining time
469 m_break
+= wxGetCurrentTime()-m_timeStop
;
476 bool wxProgressDialog::Show( bool show
)
478 // reenable other windows before hiding this one because otherwise
479 // Windows wouldn't give the focus back to the window which had
480 // been previously focused because it would still be disabled
482 ReenableOtherWindows();
484 return wxDialog::Show(show
);
487 // ----------------------------------------------------------------------------
489 // ----------------------------------------------------------------------------
491 void wxProgressDialog::OnCancel(wxCommandEvent
& event
)
493 if ( m_state
== Finished
)
495 // this means that the count down is already finished and we're being
496 // shown as a modal dialog - so just let the default handler do the job
501 // request to cancel was received, the next time Update() is called we
505 // update the buttons state immediately so that the user knows that the
506 // request has been noticed
510 // save the time when the dialog was stopped
511 m_timeStop
= wxGetCurrentTime();
515 void wxProgressDialog::OnSkip(wxCommandEvent
& WXUNUSED(event
))
521 void wxProgressDialog::OnClose(wxCloseEvent
& event
)
523 if ( m_state
== Uncancelable
)
525 // can't close this dialog
528 else if ( m_state
== Finished
)
530 // let the default handler close the window as we already terminated
535 // next Update() will notice it
540 m_timeStop
= wxGetCurrentTime();
544 // ----------------------------------------------------------------------------
546 // ----------------------------------------------------------------------------
548 wxProgressDialog::~wxProgressDialog()
550 // normally this should have been already done, but just in case
551 ReenableOtherWindows();
554 void wxProgressDialog::ReenableOtherWindows()
556 if ( GetWindowStyle() & wxPD_APP_MODAL
)
558 delete m_winDisabler
;
559 m_winDisabler
= (wxWindowDisabler
*)NULL
;
564 m_parentTop
->Enable();
568 // ----------------------------------------------------------------------------
570 // ----------------------------------------------------------------------------
572 static void SetTimeLabel(unsigned long val
, wxStaticText
*label
)
577 unsigned long hours
= val
/ 3600;
578 unsigned long minutes
= (val
% 3600) / 60;
579 unsigned long seconds
= val
% 60;
580 s
.Printf(wxT("%lu:%02lu:%02lu"), hours
, minutes
, seconds
);
582 if ( s
!= label
->GetLabel() )
587 void wxProgressDialog::EnableSkip(bool enable
)
591 #ifdef __SMARTPHONE__
593 SetRightMenu(wxID_SKIP
, _("Skip"));
598 m_btnSkip
->Enable(enable
);
603 void wxProgressDialog::EnableAbort(bool enable
)
607 #ifdef __SMARTPHONE__
609 SetLeftMenu(wxID_CANCEL
); // stock buttons makes Cancel label
614 m_btnAbort
->Enable(enable
);
619 void wxProgressDialog::EnableClose()
623 #ifdef __SMARTPHONE__
624 SetLeftMenu(wxID_CANCEL
, _("Close"));
628 m_btnAbort
->Enable();
629 m_btnAbort
->SetLabel(_("Close"));
635 #endif // wxUSE_PROGRESSDLG