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
)
89 // we may disappear at any moment, let the others know about it
90 SetExtraStyle(GetExtraStyle() | wxWS_EX_TRANSIENT
);
92 m_windowStyle
|= style
;
94 bool hasAbortButton
= (style
& wxPD_CAN_ABORT
) != 0;
96 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
97 // we have to remove the "Close" button from the title bar then as it is
98 // confusing to have it - it doesn't work anyhow
100 // FIXME: should probably have a (extended?) window style for this
101 if ( !hasAbortButton
)
103 EnableCloseButton(false);
107 m_state
= hasAbortButton
? Continue
: Uncancelable
;
110 #if defined(__WXMSW__) || defined(__WXPM__)
111 // we can't have values > 65,536 in the progress control under Windows, so
112 // scale everything down
113 m_factor
= m_maximum
/ 65536 + 1;
114 m_maximum
/= m_factor
;
117 m_parentTop
= wxGetTopLevelParent(parent
);
120 dc
.SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
122 dc
.GetTextExtent(message
, &widthText
, NULL
, NULL
, NULL
, NULL
);
124 wxBoxSizer
*sizer
= new wxBoxSizer(wxVERTICAL
);
126 m_msg
= new wxStaticText(this, wxID_ANY
, message
);
127 sizer
->Add(m_msg
, 0, wxLEFT
| wxTOP
, 2*LAYOUT_MARGIN
);
130 sizeLabel
= m_msg
->GetSize();
131 sizeDlg
.y
= 2*LAYOUT_MARGIN
+ sizeLabel
.y
;
135 // note that we can't use wxGA_SMOOTH because it happens to
136 // cause the dialog to be modal. Have an extra
137 // style argument to wxProgressDialog, perhaps.
138 m_gauge
= new wxGauge(this, wxID_ANY
, m_maximum
,
139 wxDefaultPosition
, wxDefaultSize
,
142 sizer
->Add(m_gauge
, 0, wxLEFT
| wxRIGHT
| wxTOP
| wxEXPAND
, 2*LAYOUT_MARGIN
);
143 m_gauge
->SetValue(0);
145 wxSize sizeGauge
= m_gauge
->GetSize();
146 sizeDlg
.y
+= 2*LAYOUT_MARGIN
+ sizeGauge
.y
;
149 m_gauge
= (wxGauge
*)NULL
;
151 // create the estimated/remaining/total time zones if requested
152 m_elapsed
= m_estimated
= m_remaining
= (wxStaticText
*)NULL
;
154 // if we are going to have at least one label, remmeber it in this var
155 wxStaticText
*label
= NULL
;
157 // also count how many labels we really have
158 size_t nTimeLabels
= 0;
160 if ( style
& wxPD_ELAPSED_TIME
)
165 m_elapsed
= CreateLabel(_("Elapsed time : "), sizer
);
168 if ( style
& wxPD_ESTIMATED_TIME
)
173 m_estimated
= CreateLabel(_("Estimated time : "), sizer
);
176 if ( style
& wxPD_REMAINING_TIME
)
181 m_remaining
= CreateLabel(_("Remaining time : "), sizer
);
184 if ( nTimeLabels
> 0 )
186 // set it to the current time
187 m_timeStart
= wxGetCurrentTime();
188 sizeDlg
.y
+= nTimeLabels
* (label
->GetSize().y
+ LAYOUT_MARGIN
);
191 if ( hasAbortButton
)
193 m_btnAbort
= new wxButton(this, wxID_CANCEL
);
195 // Windows dialogs usually have buttons in the lower right corner
196 #if defined(__WXMSW__) || defined(__WXPM__)
197 sizer
->Add(m_btnAbort
, 0, wxALIGN_RIGHT
| wxALL
, 2*LAYOUT_MARGIN
);
199 sizer
->Add(m_btnAbort
, 0, wxALIGN_CENTER_HORIZONTAL
| wxBOTTOM
| wxTOP
, 2*LAYOUT_MARGIN
);
201 sizeDlg
.y
+= 2*LAYOUT_MARGIN
+ wxButton::GetDefaultSize().y
;
203 else // no "Cancel" button
205 m_btnAbort
= (wxButton
*)NULL
;
208 SetSizerAndFit(sizer
);
210 sizeDlg
.y
+= 2*LAYOUT_MARGIN
;
212 // try to make the dialog not square but rectangular of reasonabel width
213 sizeDlg
.x
= (wxCoord
)wxMax(widthText
, 4*sizeDlg
.y
/3);
216 SetClientSize(sizeDlg
);
218 Centre(wxCENTER_FRAME
| wxBOTH
);
220 if ( style
& wxPD_APP_MODAL
)
222 m_winDisabler
= new wxWindowDisabler(this);
227 m_parentTop
->Disable();
228 m_winDisabler
= NULL
;
234 // this one can be initialized even if the others are unknown for now
236 // NB: do it after calling Layout() to keep the labels correctly aligned
239 SetTimeLabel(0, m_elapsed
);
245 wxStaticText
*wxProgressDialog::CreateLabel(const wxString
& text
,
248 wxBoxSizer
*locsizer
= new wxBoxSizer(wxHORIZONTAL
);
250 wxStaticText
*dummy
= new wxStaticText(this, -1, text
);
251 wxStaticText
*label
= new wxStaticText(this, wxID_ANY
, _("unknown"));
253 // VZ: I like the labels be centered - if the others don't mind, you may
254 // remove "#ifdef __WXMSW__" and use it for all ports
255 #if defined(__WXMSW__) || defined(__WXPM__) || defined(__WXMAC__)
256 locsizer
->Add(dummy
, 1, wxALIGN_RIGHT
);
257 locsizer
->Add(label
, 1, wxALIGN_LEFT
| wxLEFT
, LAYOUT_MARGIN
);
258 sizer
->Add(locsizer
, 0, wxALIGN_CENTER_HORIZONTAL
| wxTOP
, LAYOUT_MARGIN
);
260 sizer
->Add(locsizer
, 0, wxALIGN_RIGHT
| wxRIGHT
| wxTOP
, LAYOUT_MARGIN
);
261 locsizer
->Add(dummy
);
262 locsizer
->Add(label
, 0, wxLEFT
, LAYOUT_MARGIN
);
268 // ----------------------------------------------------------------------------
269 // wxProgressDialog operations
270 // ----------------------------------------------------------------------------
273 wxProgressDialog::Update(int value
, const wxString
& newmsg
)
275 wxASSERT_MSG( value
== -1 || m_gauge
, wxT("cannot update non existent dialog") );
281 wxASSERT_MSG( value
<= m_maximum
, wxT("invalid progress value") );
283 // fill up the gauge if value == maximum because this means that the dialog
284 // is going to close and the gauge shouldn't be partly empty in this case
285 if ( m_gauge
&& value
<= m_maximum
)
287 m_gauge
->SetValue(value
== m_maximum
? value
: value
+ 1);
290 if ( !newmsg
.IsEmpty() )
292 m_msg
->SetLabel(newmsg
);
297 if ( (m_elapsed
|| m_remaining
|| m_estimated
) && (value
!= 0) )
299 unsigned long elapsed
= wxGetCurrentTime() - m_timeStart
;
300 unsigned long estimated
= (unsigned long)(( (double) elapsed
* m_maximum
) / ((double)value
)) ;
301 unsigned long remaining
= estimated
- elapsed
;
303 SetTimeLabel(elapsed
, m_elapsed
);
304 SetTimeLabel(estimated
, m_estimated
);
305 SetTimeLabel(remaining
, m_remaining
);
308 if ( value
== m_maximum
)
310 // so that we return true below and that out [Cancel] handler knew what
313 if( !(GetWindowStyle() & wxPD_AUTO_HIDE
) )
317 // tell the user what he should do...
318 m_btnAbort
->SetLabel(_("Close"));
320 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
321 else // enable the button to give the user a way to close the dlg
329 // also provide the finishing message if the application didn't
330 m_msg
->SetLabel(_("Done."));
339 // reenable other windows before hiding this one because otherwise
340 // Windows wouldn't give the focus back to the window which had
341 // been previously focused because it would still be disabled
342 ReenableOtherWindows();
349 // we have to yield because not only we want to update the display but
350 // also to process the clicks on the cancel button
354 // update the display in case yielding above didn't do it
357 return m_state
!= Canceled
;
360 void wxProgressDialog::Resume()
364 // it may have been disabled by OnCancel(), so enable it back to let the
365 // user interrupt us again if needed
366 m_btnAbort
->Enable();
369 bool wxProgressDialog::Show( bool show
)
371 // reenable other windows before hiding this one because otherwise
372 // Windows wouldn't give the focus back to the window which had
373 // been previously focused because it would still be disabled
375 ReenableOtherWindows();
377 return wxDialog::Show(show
);
380 // ----------------------------------------------------------------------------
382 // ----------------------------------------------------------------------------
384 void wxProgressDialog::OnCancel(wxCommandEvent
& event
)
386 if ( m_state
== Finished
)
388 // this means that the count down is already finished and we're being
389 // shown as a modal dialog - so just let the default handler do the job
394 // request to cancel was received, the next time Update() is called we
398 // update the button state immediately so that the user knows that the
399 // request has been noticed
400 m_btnAbort
->Disable();
404 void wxProgressDialog::OnClose(wxCloseEvent
& event
)
406 if ( m_state
== Uncancelable
)
408 // can't close this dialog
411 else if ( m_state
== Finished
)
413 // let the default handler close the window as we already terminated
418 // next Update() will notice it
423 // ----------------------------------------------------------------------------
425 // ----------------------------------------------------------------------------
427 wxProgressDialog::~wxProgressDialog()
429 // normally this should have been already done, but just in case
430 ReenableOtherWindows();
433 void wxProgressDialog::ReenableOtherWindows()
435 if ( GetWindowStyle() & wxPD_APP_MODAL
)
437 delete m_winDisabler
;
438 m_winDisabler
= (wxWindowDisabler
*)NULL
;
443 m_parentTop
->Enable();
447 // ----------------------------------------------------------------------------
449 // ----------------------------------------------------------------------------
451 static void SetTimeLabel(unsigned long val
, wxStaticText
*label
)
456 unsigned long hours
= val
/ 3600;
457 unsigned long minutes
= (val
% 3600) / 60;
458 unsigned long seconds
= val
% 60;
459 s
.Printf(wxT("%lu:%02lu:%02lu"), hours
, minutes
, seconds
);
461 if ( s
!= label
->GetLabel() )
466 #endif // wxUSE_PROGRESSDLG