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 #define LAYOUT_MARGIN 8
55 // ----------------------------------------------------------------------------
57 // ----------------------------------------------------------------------------
59 // update the label to show the given time (in seconds)
60 static void SetTimeLabel(unsigned long val
, wxStaticText
*label
);
62 // ----------------------------------------------------------------------------
64 // ----------------------------------------------------------------------------
66 BEGIN_EVENT_TABLE(wxProgressDialog
, wxDialog
)
67 EVT_BUTTON(wxID_CANCEL
, wxProgressDialog::OnCancel
)
69 EVT_CLOSE(wxProgressDialog::OnClose
)
72 IMPLEMENT_CLASS(wxProgressDialog
, wxDialog
)
74 // ============================================================================
75 // wxProgressDialog implementation
76 // ============================================================================
78 // ----------------------------------------------------------------------------
79 // wxProgressDialog creation
80 // ----------------------------------------------------------------------------
82 wxProgressDialog::wxProgressDialog(wxString
const &title
,
83 wxString
const &message
,
87 : wxDialog(parent
, wxID_ANY
, title
),
90 // we may disappear at any moment, let the others know about it
91 SetExtraStyle(GetExtraStyle() | wxWS_EX_TRANSIENT
);
93 m_windowStyle
|= style
;
95 bool hasAbortButton
= (style
& wxPD_CAN_ABORT
) != 0;
97 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
98 // we have to remove the "Close" button from the title bar then as it is
99 // confusing to have it - it doesn't work anyhow
101 // FIXME: should probably have a (extended?) window style for this
102 if ( !hasAbortButton
)
104 EnableCloseButton(false);
108 m_state
= hasAbortButton
? Continue
: Uncancelable
;
111 #if defined(__WXMSW__) || defined(__WXPM__)
112 // we can't have values > 65,536 in the progress control under Windows, so
113 // scale everything down
114 m_factor
= m_maximum
/ 65536 + 1;
115 m_maximum
/= m_factor
;
118 m_parentTop
= wxGetTopLevelParent(parent
);
121 dc
.SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
123 dc
.GetTextExtent(message
, &widthText
, NULL
, NULL
, NULL
, NULL
);
125 wxBoxSizer
*sizer
= new wxBoxSizer(wxVERTICAL
);
127 m_msg
= new wxStaticText(this, wxID_ANY
, message
);
128 sizer
->Add(m_msg
, 0, wxLEFT
| wxTOP
, 2*LAYOUT_MARGIN
);
131 sizeLabel
= m_msg
->GetSize();
132 sizeDlg
.y
= 2*LAYOUT_MARGIN
+ sizeLabel
.y
;
136 // note that we can't use wxGA_SMOOTH because it happens to
137 // cause the dialog to be modal. Have an extra
138 // style argument to wxProgressDialog, perhaps.
139 m_gauge
= new wxGauge(this, wxID_ANY
, m_maximum
,
140 wxDefaultPosition
, wxDefaultSize
,
143 sizer
->Add(m_gauge
, 0, wxLEFT
| wxRIGHT
| wxTOP
| wxEXPAND
, 2*LAYOUT_MARGIN
);
144 m_gauge
->SetValue(0);
146 wxSize sizeGauge
= m_gauge
->GetSize();
147 sizeDlg
.y
+= 2*LAYOUT_MARGIN
+ sizeGauge
.y
;
150 m_gauge
= (wxGauge
*)NULL
;
152 // create the estimated/remaining/total time zones if requested
153 m_elapsed
= m_estimated
= m_remaining
= (wxStaticText
*)NULL
;
154 m_display_estimated
= m_last_timeupdate
= m_break
= 0;
157 // if we are going to have at least one label, remmeber it in this var
158 wxStaticText
*label
= NULL
;
160 // also count how many labels we really have
161 size_t nTimeLabels
= 0;
163 if ( style
& wxPD_ELAPSED_TIME
)
168 m_elapsed
= CreateLabel(_("Elapsed time : "), sizer
);
171 if ( style
& wxPD_ESTIMATED_TIME
)
176 m_estimated
= CreateLabel(_("Estimated time : "), sizer
);
179 if ( style
& wxPD_REMAINING_TIME
)
184 m_remaining
= CreateLabel(_("Remaining time : "), sizer
);
187 if ( nTimeLabels
> 0 )
189 // set it to the current time
190 m_timeStart
= wxGetCurrentTime();
191 sizeDlg
.y
+= nTimeLabels
* (label
->GetSize().y
+ LAYOUT_MARGIN
);
194 if ( hasAbortButton
)
196 m_btnAbort
= new wxButton(this, wxID_CANCEL
);
198 // Windows dialogs usually have buttons in the lower right corner
199 #if defined(__WXMSW__) || defined(__WXPM__)
200 sizer
->Add(m_btnAbort
, 0, wxALIGN_RIGHT
| wxALL
, 2*LAYOUT_MARGIN
);
202 sizer
->Add(m_btnAbort
, 0, wxALIGN_CENTER_HORIZONTAL
| wxBOTTOM
| wxTOP
, 2*LAYOUT_MARGIN
);
204 sizeDlg
.y
+= 2*LAYOUT_MARGIN
+ wxButton::GetDefaultSize().y
;
206 else // no "Cancel" button
208 m_btnAbort
= (wxButton
*)NULL
;
211 SetSizerAndFit(sizer
);
213 sizeDlg
.y
+= 2*LAYOUT_MARGIN
;
215 // try to make the dialog not square but rectangular of reasonabel width
216 sizeDlg
.x
= (wxCoord
)wxMax(widthText
, 4*sizeDlg
.y
/3);
219 SetClientSize(sizeDlg
);
221 Centre(wxCENTER_FRAME
| wxBOTH
);
223 if ( style
& wxPD_APP_MODAL
)
225 m_winDisabler
= new wxWindowDisabler(this);
230 m_parentTop
->Disable();
231 m_winDisabler
= NULL
;
237 // this one can be initialized even if the others are unknown for now
239 // NB: do it after calling Layout() to keep the labels correctly aligned
242 SetTimeLabel(0, m_elapsed
);
248 wxStaticText
*wxProgressDialog::CreateLabel(const wxString
& text
,
251 wxBoxSizer
*locsizer
= new wxBoxSizer(wxHORIZONTAL
);
253 wxStaticText
*dummy
= new wxStaticText(this, -1, text
);
254 wxStaticText
*label
= new wxStaticText(this, wxID_ANY
, _("unknown"));
256 // VZ: I like the labels be centered - if the others don't mind, you may
257 // remove "#ifdef __WXMSW__" and use it for all ports
258 #if defined(__WXMSW__) || defined(__WXPM__) || defined(__WXMAC__)
259 locsizer
->Add(dummy
, 1, wxALIGN_RIGHT
);
260 locsizer
->Add(label
, 1, wxALIGN_LEFT
| wxLEFT
, LAYOUT_MARGIN
);
261 sizer
->Add(locsizer
, 0, wxALIGN_CENTER_HORIZONTAL
| wxTOP
, LAYOUT_MARGIN
);
263 sizer
->Add(locsizer
, 0, wxALIGN_RIGHT
| wxRIGHT
| wxTOP
, LAYOUT_MARGIN
);
264 locsizer
->Add(dummy
);
265 locsizer
->Add(label
, 0, wxLEFT
, LAYOUT_MARGIN
);
271 // ----------------------------------------------------------------------------
272 // wxProgressDialog operations
273 // ----------------------------------------------------------------------------
276 wxProgressDialog::Update(int value
, const wxString
& newmsg
)
278 wxASSERT_MSG( value
== -1 || m_gauge
, wxT("cannot update non existent dialog") );
284 wxASSERT_MSG( value
<= m_maximum
, wxT("invalid progress value") );
286 // fill up the gauge if value == maximum because this means that the dialog
287 // is going to close and the gauge shouldn't be partly empty in this case
288 if ( m_gauge
&& value
<= m_maximum
)
290 m_gauge
->SetValue(value
== m_maximum
? value
: value
+ 1);
293 if ( !newmsg
.IsEmpty() )
295 m_msg
->SetLabel(newmsg
);
300 if ( (m_elapsed
|| m_remaining
|| m_estimated
) && (value
!= 0) )
302 unsigned long elapsed
= wxGetCurrentTime() - m_timeStart
;
303 if ( m_last_timeupdate
< elapsed
304 || value
== m_maximum
307 m_last_timeupdate
= elapsed
;
308 unsigned long estimated
= m_break
+
309 (unsigned long)(( (double) (elapsed
-m_break
) * m_maximum
) / ((double)value
)) ;
310 if ( estimated
> m_display_estimated
316 else if ( estimated
< m_display_estimated
326 if ( m_ctdelay
>= m_delay
// enough confirmations for a higher value
327 || m_ctdelay
<= (m_delay
*-1) // enough confirmations for a lower value
328 || value
== m_maximum
// to stay consistent
329 || elapsed
> m_display_estimated
// to stay consistent
330 || ( elapsed
> 0 && elapsed
< 4 ) // additional updates in the beginning
333 m_display_estimated
= estimated
;
338 unsigned long display_remaining
= m_display_estimated
- elapsed
;
339 if ( display_remaining
< 0 )
341 display_remaining
= 0;
344 SetTimeLabel(elapsed
, m_elapsed
);
345 SetTimeLabel(m_display_estimated
, m_estimated
);
346 SetTimeLabel(display_remaining
, m_remaining
);
349 if ( value
== m_maximum
)
351 if ( m_state
== Finished
)
353 // ignore multiple calls to Update(m_maximum): it may sometimes be
354 // troublesome to ensure that Update() is not called twice with the
355 // same value (e.g. because of the rounding errors) and if we don't
356 // return now we're going to generate asserts below
360 // so that we return true below and that out [Cancel] handler knew what
363 if( !(GetWindowStyle() & wxPD_AUTO_HIDE
) )
367 // tell the user what he should do...
368 m_btnAbort
->SetLabel(_("Close"));
370 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
371 else // enable the button to give the user a way to close the dlg
379 // also provide the finishing message if the application didn't
380 m_msg
->SetLabel(_("Done."));
389 // reenable other windows before hiding this one because otherwise
390 // Windows wouldn't give the focus back to the window which had
391 // been previously focused because it would still be disabled
392 ReenableOtherWindows();
399 // we have to yield because not only we want to update the display but
400 // also to process the clicks on the cancel button
404 // update the display in case yielding above didn't do it
407 return m_state
!= Canceled
;
410 void wxProgressDialog::Resume()
413 m_ctdelay
= m_delay
; // force an update of the elapsed/estimated/remaining time
414 m_break
+= wxGetCurrentTime()-m_timeStop
;
416 // it may have been disabled by OnCancel(), so enable it back to let the
417 // user interrupt us again if needed
418 m_btnAbort
->Enable();
421 bool wxProgressDialog::Show( bool show
)
423 // reenable other windows before hiding this one because otherwise
424 // Windows wouldn't give the focus back to the window which had
425 // been previously focused because it would still be disabled
427 ReenableOtherWindows();
429 return wxDialog::Show(show
);
432 // ----------------------------------------------------------------------------
434 // ----------------------------------------------------------------------------
436 void wxProgressDialog::OnCancel(wxCommandEvent
& event
)
438 if ( m_state
== Finished
)
440 // this means that the count down is already finished and we're being
441 // shown as a modal dialog - so just let the default handler do the job
446 // request to cancel was received, the next time Update() is called we
450 // update the button state immediately so that the user knows that the
451 // request has been noticed
452 m_btnAbort
->Disable();
454 // save the time when the dialog was stopped
455 m_timeStop
= wxGetCurrentTime();
459 void wxProgressDialog::OnClose(wxCloseEvent
& event
)
461 if ( m_state
== Uncancelable
)
463 // can't close this dialog
466 else if ( m_state
== Finished
)
468 // let the default handler close the window as we already terminated
473 // next Update() will notice it
475 m_btnAbort
->Disable();
476 m_timeStop
= wxGetCurrentTime();
480 // ----------------------------------------------------------------------------
482 // ----------------------------------------------------------------------------
484 wxProgressDialog::~wxProgressDialog()
486 // normally this should have been already done, but just in case
487 ReenableOtherWindows();
490 void wxProgressDialog::ReenableOtherWindows()
492 if ( GetWindowStyle() & wxPD_APP_MODAL
)
494 delete m_winDisabler
;
495 m_winDisabler
= (wxWindowDisabler
*)NULL
;
500 m_parentTop
->Enable();
504 // ----------------------------------------------------------------------------
506 // ----------------------------------------------------------------------------
508 static void SetTimeLabel(unsigned long val
, wxStaticText
*label
)
513 unsigned long hours
= val
/ 3600;
514 unsigned long minutes
= (val
% 3600) / 60;
515 unsigned long seconds
= val
% 60;
516 s
.Printf(wxT("%lu:%02lu:%02lu"), hours
, minutes
, seconds
);
518 if ( s
!= label
->GetLabel() )
523 #endif // wxUSE_PROGRESSDLG