1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxProgressDialog class
4 // Author: Karsten Ballüder
8 // Copyright: (c) Karsten Ballüder
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
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"
38 #include "wx/layout.h"
42 #include "wx/settings.h"
43 #include "wx/dcclient.h"
47 #include "wx/generic/progdlgg.h"
49 // ----------------------------------------------------------------------------
51 // ----------------------------------------------------------------------------
53 #define LAYOUT_X_MARGIN 8
54 #define LAYOUT_Y_MARGIN 8
56 // ----------------------------------------------------------------------------
58 // ----------------------------------------------------------------------------
60 // update the label to show the given time (in seconds)
61 static void SetTimeLabel(unsigned long val
, wxStaticText
*label
);
63 // ----------------------------------------------------------------------------
65 // ----------------------------------------------------------------------------
67 BEGIN_EVENT_TABLE(wxProgressDialog
, wxDialog
)
68 EVT_BUTTON(wxID_CANCEL
, wxProgressDialog
::OnCancel
)
70 EVT_SHOW(wxProgressDialog
::OnShow
)
72 EVT_CLOSE(wxProgressDialog
::OnClose
)
75 IMPLEMENT_CLASS(wxProgressDialog
, wxDialog
)
77 // ============================================================================
79 // ============================================================================
81 // ----------------------------------------------------------------------------
83 // ----------------------------------------------------------------------------
85 wxProgressDialog
::wxProgressDialog(wxString
const &title
,
86 wxString
const &message
,
90 : wxDialog(parent
, -1, title
,
91 wxDefaultPosition
, wxDefaultSize
,
92 wxDEFAULT_DIALOG_STYLE
| wxDIALOG_NO_PARENT
)
94 m_windowStyle
|= style
;
96 bool hasAbortButton
= (style
& wxPD_CAN_ABORT
) != 0;
98 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
99 // we have to remove the "Close" button from the title bar then as it is
100 // confusing to have it - it doesn't work anyhow
102 // FIXME: should probably have a (extended?) window style for this
103 if ( !hasAbortButton
)
105 EnableCloseButton(FALSE
);
109 m_state
= hasAbortButton ? Continue
: Uncancelable
;
112 m_parentTop
= parent
;
113 while ( m_parentTop
&& m_parentTop
->GetParent() )
115 m_parentTop
= m_parentTop
->GetParent();
118 wxLayoutConstraints
*c
;
121 dc
.SetFont(wxSystemSettings
::GetSystemFont(wxSYS_DEFAULT_GUI_FONT
));
123 dc
.GetTextExtent(message
, &widthText
, NULL
, NULL
, NULL
, NULL
);
125 m_msg
= new wxStaticText(this, -1, message
);
126 c
= new wxLayoutConstraints
;
127 c
->left
.SameAs(this, wxLeft
, 2*LAYOUT_X_MARGIN
);
128 c
->top
.SameAs(this, wxTop
, 2*LAYOUT_Y_MARGIN
);
131 m_msg
->SetConstraints(c
);
134 sizeLabel
= m_msg
->GetSize();
135 sizeDlg
.y
= 2*LAYOUT_Y_MARGIN
+ sizeLabel
.y
;
137 wxWindow
*lastWindow
= m_msg
;
141 // note that we can't use wxGA_SMOOTH because it happens to also mean
142 // wxDIALOG_MODAL and will cause the dialog to be modal. Have an extra
143 // style argument to wxProgressDialog, perhaps.
144 m_gauge
= new wxGauge(this, -1, maximum
,
145 wxDefaultPosition
, wxDefaultSize
,
148 c
= new wxLayoutConstraints
;
149 c
->left
.SameAs(this, wxLeft
, 2*LAYOUT_X_MARGIN
);
150 c
->top
.Below(m_msg
, 2*LAYOUT_Y_MARGIN
);
151 c
->right
.SameAs(this, wxRight
, 2*LAYOUT_X_MARGIN
);
153 m_gauge
->SetConstraints(c
);
154 m_gauge
->SetValue(0);
155 lastWindow
= m_gauge
;
157 wxSize sizeGauge
= m_gauge
->GetSize();
158 sizeDlg
.y
+= 2*LAYOUT_Y_MARGIN
+ sizeGauge
.y
;
161 m_gauge
= (wxGauge
*)NULL
;
163 // create the estimated/remaining/total time zones if requested
164 m_elapsed
= m_estimated
= m_remaining
= (wxStaticText
*)NULL
;
166 // if we are going to have at least one label, remmeber it in this var
167 wxStaticText
*label
= NULL
;
169 // also count how many labels we really have
170 size_t nTimeLabels
= 0;
172 if ( style
& wxPD_ELAPSED_TIME
)
177 m_elapsed
= CreateLabel(_("Elapsed time : "), &lastWindow
);
180 if ( style
& wxPD_ESTIMATED_TIME
)
185 m_estimated
= CreateLabel(_("Estimated time : "), &lastWindow
);
188 if ( style
& wxPD_REMAINING_TIME
)
193 m_remaining
= CreateLabel(_("Remaining time : "), &lastWindow
);
196 if ( nTimeLabels
> 0 )
198 // set it to the current time
199 m_timeStart
= wxGetCurrentTime();
200 sizeDlg
.y
+= nTimeLabels
* (label
->GetSize().y
+ LAYOUT_Y_MARGIN
);
203 if ( hasAbortButton
)
205 m_btnAbort
= new wxButton(this, wxID_CANCEL
, _("Cancel"));
206 c
= new wxLayoutConstraints
;
208 // Windows dialogs usually have buttons in the lower right corner
210 c
->right
.SameAs(this, wxRight
, 2*LAYOUT_X_MARGIN
);
212 c
->centreX
.SameAs(this, wxCentreX
);
214 c
->bottom
.SameAs(this, wxBottom
, 2*LAYOUT_Y_MARGIN
);
216 wxSize sizeBtn
= wxButton
::GetDefaultSize();
217 c
->width
.Absolute(sizeBtn
.x
);
218 c
->height
.Absolute(sizeBtn
.y
);
220 m_btnAbort
->SetConstraints(c
);
222 sizeDlg
.y
+= 2*LAYOUT_Y_MARGIN
+ sizeBtn
.y
;
225 m_btnAbort
= (wxButton
*)NULL
;
230 sizeDlg
.y
+= 2*LAYOUT_Y_MARGIN
;
232 // try to make the dialog not square but rectangular of reasonabel width
233 sizeDlg
.x
= (wxCoord
)wxMax(widthText
, 4*sizeDlg
.y
/3);
236 SetClientSize(sizeDlg
);
238 Centre(wxCENTER_FRAME
| wxBOTH
);
240 if ( style
& wxPD_APP_MODAL
)
242 m_winDisabler
= new wxWindowDisabler(this);
247 m_parentTop
->Enable(FALSE
);
248 m_winDisabler
= NULL
;
252 Enable(TRUE
); // enable this window
254 // this one can be initialized even if the others are unknown for now
256 // NB: do it after calling Layout() to keep the labels correctly aligned
259 SetTimeLabel(0, m_elapsed
);
262 // Update the display (especially on X, GTK)
266 MacUpdateImmediately();
270 wxStaticText
*wxProgressDialog
::CreateLabel(const wxString
& text
,
271 wxWindow
**lastWindow
)
273 wxLayoutConstraints
*c
;
275 wxStaticText
*label
= new wxStaticText(this, -1, _("unknown"));
276 c
= new wxLayoutConstraints
;
278 // VZ: I like the labels be centered - if the others don't mind, you may
279 // remove "#ifdef __WXMSW__" and use it for all ports
281 c
->left
.SameAs(this, wxCentreX
, LAYOUT_X_MARGIN
);
283 c
->right
.SameAs(this, wxRight
, 2*LAYOUT_X_MARGIN
);
285 c
->top
.Below(*lastWindow
, LAYOUT_Y_MARGIN
);
288 label
->SetConstraints(c
);
290 wxStaticText
*dummy
= new wxStaticText(this, -1, text
);
291 c
= new wxLayoutConstraints
;
292 c
->right
.LeftOf(label
);
293 c
->top
.SameAs(label
, wxTop
, 0);
296 dummy
->SetConstraints(c
);
304 wxProgressDialog
::Update(int value
, const wxString
& newmsg
)
306 wxASSERT_MSG( value
== -1 || m_gauge
, wxT("cannot update non existent dialog") );
307 wxASSERT_MSG( value
<= m_maximum
, wxT("invalid progress value") );
310 m_gauge
->SetValue(value
+ 1);
312 if ( !newmsg
.IsEmpty() )
315 // this seems to be necessary or garbage is left when the new label is
316 // longer than the old one
317 m_msg
->SetLabel(wxEmptyString
);
320 m_msg
->SetLabel(newmsg
);
325 if ( (m_elapsed
|| m_remaining
|| m_estimated
) && (value
!= 0) )
327 unsigned long elapsed
= wxGetCurrentTime() - m_timeStart
;
328 unsigned long estimated
= elapsed
* m_maximum
/ value
;
329 unsigned long remaining
= estimated
- elapsed
;
331 SetTimeLabel(elapsed
, m_elapsed
);
332 SetTimeLabel(estimated
, m_estimated
);
333 SetTimeLabel(remaining
, m_remaining
);
336 if ( (value
== m_maximum
) && !(GetWindowStyle() & wxPD_AUTO_HIDE
) )
340 // tell the user what he should do...
341 m_btnAbort
->SetLabel(_("Close"));
343 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
344 else // enable the close button to give the user a way to close the dlg
346 EnableCloseButton(TRUE
);
352 // also provide the finishing message if the application didn't
353 m_msg
->SetLabel(_("Done."));
356 // so that we return TRUE below and that out [Cancel] handler knew what
366 // update the display
371 MacUpdateImmediately();
374 return m_state
!= Canceled
;
377 // ----------------------------------------------------------------------------
379 // ----------------------------------------------------------------------------
381 void wxProgressDialog
::OnCancel(wxCommandEvent
& event
)
383 if ( m_state
== Finished
)
385 // this means that the count down is already finished and we're being
386 // shown as a modal dialog - so just let the default handler do the job
391 // request to cancel was received, the next time Update() is called we
395 // update the button state immediately so that the user knows that the
396 // request has been noticed
397 m_btnAbort
->Disable();
401 void wxProgressDialog
::OnClose(wxCloseEvent
& event
)
403 if ( m_state
== Uncancelable
)
405 // can't close this dialog
408 else if ( m_state
== Finished
)
410 // let the default handler close the window as we already terminated
415 // next Update() will notice it
420 void wxProgressDialog
::OnShow(wxShowEvent
& event
)
422 // if the dialog is being hidden, it was closed, so reenable other windows
424 if ( event
.GetShow() )
426 ReenableOtherWindows();
430 // ----------------------------------------------------------------------------
432 // ----------------------------------------------------------------------------
434 wxProgressDialog
::~wxProgressDialog()
436 // normally this should have been already done, but just in case
437 ReenableOtherWindows();
440 void wxProgressDialog
::ReenableOtherWindows()
442 if ( GetWindowStyle() & wxPD_APP_MODAL
)
444 delete m_winDisabler
;
445 m_winDisabler
= (wxWindowDisabler
*)NULL
;
450 m_parentTop
->Enable(TRUE
);
454 // ----------------------------------------------------------------------------
456 // ----------------------------------------------------------------------------
458 static void SetTimeLabel(unsigned long val
, wxStaticText
*label
)
463 unsigned long hours
= val
/ 3600;
464 unsigned long minutes
= (val
% 3600) / 60;
465 unsigned long seconds
= val
% 60;
466 s
.Printf(wxT("%lu:%02lu:%02lu"), hours
, minutes
, seconds
);
468 if ( s
!= label
->GetLabel() )
473 #endif // wxUSE_PROGRESSDLG