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
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 // ----------------------------------------------------------------------------
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
)
83 EVT_CLOSE(wxProgressDialog::OnClose
)
86 IMPLEMENT_CLASS(wxProgressDialog
, wxDialog
)
88 // ============================================================================
89 // wxProgressDialog implementation
90 // ============================================================================
92 // ----------------------------------------------------------------------------
93 // wxProgressDialog creation
94 // ----------------------------------------------------------------------------
96 wxProgressDialog::wxProgressDialog(wxString
const &title
,
97 wxString
const &message
,
101 : wxDialog(parent
, wxID_ANY
, title
),
104 // we may disappear at any moment, let the others know about it
105 SetExtraStyle(GetExtraStyle() | wxWS_EX_TRANSIENT
);
107 m_windowStyle
|= style
;
109 bool hasAbortButton
= (style
& wxPD_CAN_ABORT
) != 0;
111 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
112 // we have to remove the "Close" button from the title bar then as it is
113 // confusing to have it - it doesn't work anyhow
115 // FIXME: should probably have a (extended?) window style for this
116 if ( !hasAbortButton
)
118 EnableCloseButton(false);
122 #if defined(__SMARTPHONE__)
126 m_state
= hasAbortButton
? Continue
: Uncancelable
;
129 #if defined(__WXMSW__) || defined(__WXPM__)
130 // we can't have values > 65,536 in the progress control under Windows, so
131 // scale everything down
132 m_factor
= m_maximum
/ 65536 + 1;
133 m_maximum
/= m_factor
;
136 m_parentTop
= wxGetTopLevelParent(parent
);
139 dc
.SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
141 dc
.GetTextExtent(message
, &widthText
, NULL
, NULL
, NULL
, NULL
);
143 wxBoxSizer
*sizer
= new wxBoxSizer(wxVERTICAL
);
145 m_msg
= new wxStaticText(this, wxID_ANY
, message
);
146 sizer
->Add(m_msg
, 0, wxLEFT
| wxTOP
, 2*LAYOUT_MARGIN
);
149 sizeLabel
= m_msg
->GetSize();
150 sizeDlg
.y
= 2*LAYOUT_MARGIN
+ sizeLabel
.y
;
154 // note that we can't use wxGA_SMOOTH because it happens to
155 // cause the dialog to be modal. Have an extra
156 // style argument to wxProgressDialog, perhaps.
157 m_gauge
= new wxGauge(this, wxID_ANY
, m_maximum
,
158 wxDefaultPosition
, wxDefaultSize
,
161 sizer
->Add(m_gauge
, 0, wxLEFT
| wxRIGHT
| wxTOP
| wxEXPAND
, 2*LAYOUT_MARGIN
);
162 m_gauge
->SetValue(0);
164 wxSize sizeGauge
= m_gauge
->GetSize();
165 sizeDlg
.y
+= 2*LAYOUT_MARGIN
+ sizeGauge
.y
;
168 m_gauge
= (wxGauge
*)NULL
;
170 // create the estimated/remaining/total time zones if requested
171 m_elapsed
= m_estimated
= m_remaining
= (wxStaticText
*)NULL
;
172 m_display_estimated
= m_last_timeupdate
= m_break
= 0;
175 // if we are going to have at least one label, remmeber it in this var
176 wxStaticText
*label
= NULL
;
178 // also count how many labels we really have
179 size_t nTimeLabels
= 0;
181 if ( style
& wxPD_ELAPSED_TIME
)
186 m_elapsed
= CreateLabel(_("Elapsed time : "), sizer
);
189 if ( style
& wxPD_ESTIMATED_TIME
)
194 m_estimated
= CreateLabel(_("Estimated time : "), sizer
);
197 if ( style
& wxPD_REMAINING_TIME
)
202 m_remaining
= CreateLabel(_("Remaining time : "), sizer
);
205 if ( nTimeLabels
> 0 )
207 // set it to the current time
208 m_timeStart
= wxGetCurrentTime();
209 sizeDlg
.y
+= nTimeLabels
* (label
->GetSize().y
+ LAYOUT_MARGIN
);
212 if ( hasAbortButton
)
214 #if defined(__SMARTPHONE__)
215 SetLeftMenu(wxID_CANCEL
, _("Cancel"));
218 m_btnAbort
= new wxButton(this, wxID_CANCEL
);
220 // Windows dialogs usually have buttons in the lower right corner
221 #if defined(__WXMSW__) || defined(__WXPM__)
222 sizer
->Add(m_btnAbort
, 0, wxALIGN_RIGHT
| wxALL
, 2*LAYOUT_MARGIN
);
224 sizer
->Add(m_btnAbort
, 0, wxALIGN_CENTER_HORIZONTAL
| wxBOTTOM
| wxTOP
, 2*LAYOUT_MARGIN
);
226 sizeDlg
.y
+= 2*LAYOUT_MARGIN
+ wxButton::GetDefaultSize().y
;
228 else // no "Cancel" button
229 #endif // __SMARTPHONE__/!__SMARTPHONE__
231 m_btnAbort
= (wxButton
*)NULL
;
234 SetSizerAndFit(sizer
);
236 sizeDlg
.y
+= 2*LAYOUT_MARGIN
;
238 // try to make the dialog not square but rectangular of reasonabel width
239 sizeDlg
.x
= (wxCoord
)wxMax(widthText
, 4*sizeDlg
.y
/3);
242 SetClientSize(sizeDlg
);
244 Centre(wxCENTER_FRAME
| wxBOTH
);
246 if ( style
& wxPD_APP_MODAL
)
248 m_winDisabler
= new wxWindowDisabler(this);
253 m_parentTop
->Disable();
254 m_winDisabler
= NULL
;
260 // this one can be initialized even if the others are unknown for now
262 // NB: do it after calling Layout() to keep the labels correctly aligned
265 SetTimeLabel(0, m_elapsed
);
271 wxStaticText
*wxProgressDialog::CreateLabel(const wxString
& text
,
274 wxBoxSizer
*locsizer
= new wxBoxSizer(wxLARGESMALL(wxHORIZONTAL
,wxVERTICAL
));
276 wxStaticText
*dummy
= new wxStaticText(this, wxID_ANY
, text
);
277 wxStaticText
*label
= new wxStaticText(this, wxID_ANY
, _("unknown"));
279 // select placement most native or nice on target GUI
280 #if defined(__SMARTPHONE__)
281 // label and time to the left in two rows
282 locsizer
->Add(dummy
, 1, wxALIGN_LEFT
);
283 locsizer
->Add(label
, 1, wxALIGN_LEFT
);
284 sizer
->Add(locsizer
, 0, wxALIGN_LEFT
| wxTOP
| wxLEFT
, LAYOUT_MARGIN
);
285 #elif defined(__WXMSW__) || defined(__WXPM__) || defined(__WXMAC__)
286 // label and time centered in one row
287 locsizer
->Add(dummy
, 1, wxLARGESMALL(wxALIGN_RIGHT
,wxALIGN_LEFT
));
288 locsizer
->Add(label
, 1, wxALIGN_LEFT
| wxLEFT
, LAYOUT_MARGIN
);
289 sizer
->Add(locsizer
, 0, wxALIGN_CENTER_HORIZONTAL
| wxTOP
, LAYOUT_MARGIN
);
291 // label and time to the right in one row
292 sizer
->Add(locsizer
, 0, wxALIGN_RIGHT
| wxRIGHT
| wxTOP
, LAYOUT_MARGIN
);
293 locsizer
->Add(dummy
);
294 locsizer
->Add(label
, 0, wxLEFT
, LAYOUT_MARGIN
);
300 // ----------------------------------------------------------------------------
301 // wxProgressDialog operations
302 // ----------------------------------------------------------------------------
305 wxProgressDialog::Update(int value
, const wxString
& newmsg
)
307 wxASSERT_MSG( value
== -1 || m_gauge
, wxT("cannot update non existent dialog") );
313 wxASSERT_MSG( value
<= m_maximum
, wxT("invalid progress value") );
315 // fill up the gauge if value == maximum because this means that the dialog
316 // is going to close and the gauge shouldn't be partly empty in this case
317 if ( m_gauge
&& value
<= m_maximum
)
319 m_gauge
->SetValue(value
== m_maximum
? value
: value
+ 1);
322 if ( !newmsg
.IsEmpty() )
324 m_msg
->SetLabel(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( !(GetWindowStyle() & wxPD_AUTO_HIDE
) )
394 #if defined(__SMARTPHONE__)
395 SetLeftMenu(wxID_CANCEL
, _("Close"));
399 // tell the user what he should do...
400 m_btnAbort
->SetLabel(_("Close"));
402 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
403 else // enable the button to give the user a way to close the dlg
411 // also provide the finishing message if the application didn't
412 m_msg
->SetLabel(_("Done."));
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();
431 // we have to yield because not only we want to update the display but
432 // also to process the clicks on the cancel button
436 // update the display in case yielding above didn't do it
439 return m_state
!= Canceled
;
442 void wxProgressDialog::Resume()
445 m_ctdelay
= m_delay
; // force an update of the elapsed/estimated/remaining time
446 m_break
+= wxGetCurrentTime()-m_timeStop
;
448 // it may have been disabled by OnCancel(), so enable it back to let the
449 // user interrupt us again if needed
451 m_btnAbort
->Enable();
452 #if defined(__SMARTPHONE__)
453 SetLeftMenu(wxID_CANCEL
, _("Cancel"));
457 bool wxProgressDialog::Show( bool show
)
459 // reenable other windows before hiding this one because otherwise
460 // Windows wouldn't give the focus back to the window which had
461 // been previously focused because it would still be disabled
463 ReenableOtherWindows();
465 return wxDialog::Show(show
);
468 // ----------------------------------------------------------------------------
470 // ----------------------------------------------------------------------------
472 void wxProgressDialog::OnCancel(wxCommandEvent
& event
)
474 if ( m_state
== Finished
)
476 // this means that the count down is already finished and we're being
477 // shown as a modal dialog - so just let the default handler do the job
482 // request to cancel was received, the next time Update() is called we
486 // update the button state immediately so that the user knows that the
487 // request has been noticed
489 m_btnAbort
->Disable();
491 #if defined(__SMARTPHONE__)
495 // save the time when the dialog was stopped
496 m_timeStop
= wxGetCurrentTime();
500 void wxProgressDialog::OnClose(wxCloseEvent
& event
)
502 if ( m_state
== Uncancelable
)
504 // can't close this dialog
507 else if ( m_state
== Finished
)
509 // let the default handler close the window as we already terminated
514 // next Update() will notice it
517 m_btnAbort
->Disable();
518 #if defined(__SMARTPHONE__)
521 m_timeStop
= wxGetCurrentTime();
525 // ----------------------------------------------------------------------------
527 // ----------------------------------------------------------------------------
529 wxProgressDialog::~wxProgressDialog()
531 // normally this should have been already done, but just in case
532 ReenableOtherWindows();
535 void wxProgressDialog::ReenableOtherWindows()
537 if ( GetWindowStyle() & wxPD_APP_MODAL
)
539 delete m_winDisabler
;
540 m_winDisabler
= (wxWindowDisabler
*)NULL
;
545 m_parentTop
->Enable();
549 // ----------------------------------------------------------------------------
551 // ----------------------------------------------------------------------------
553 static void SetTimeLabel(unsigned long val
, wxStaticText
*label
)
558 unsigned long hours
= val
/ 3600;
559 unsigned long minutes
= (val
% 3600) / 60;
560 unsigned long seconds
= val
% 60;
561 s
.Printf(wxT("%lu:%02lu:%02lu"), hours
, minutes
, seconds
);
563 if ( s
!= label
->GetLabel() )
568 #endif // wxUSE_PROGRESSDLG