]>
git.saurik.com Git - wxWidgets.git/blob - src/generic/progdlgg.cpp
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
)
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 m_parentTop
= parent
;
111 while ( m_parentTop
&& m_parentTop
->GetParent() )
113 m_parentTop
= m_parentTop
->GetParent();
116 wxLayoutConstraints
*c
;
119 dc
.SetFont(wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT
));
121 dc
.GetTextExtent(message
, &widthText
, NULL
, NULL
, NULL
, NULL
);
123 m_msg
= new wxStaticText(this, -1, message
);
124 c
= new wxLayoutConstraints
;
125 c
->left
.SameAs(this, wxLeft
, 2*LAYOUT_X_MARGIN
);
126 c
->top
.SameAs(this, wxTop
, 2*LAYOUT_Y_MARGIN
);
129 m_msg
->SetConstraints(c
);
131 wxSize sizeDlg
, sizeLabel
= m_msg
->GetSize();
132 sizeDlg
.y
= 2*LAYOUT_Y_MARGIN
+ sizeLabel
.y
;
134 wxWindow
*lastWindow
= m_msg
;
138 // note that we can't use wxGA_SMOOTH because it happens to also mean
139 // wxDIALOG_MODAL and will cause the dialog to be modal. Have an extra
140 // style argument to wxProgressDialog, perhaps.
141 m_gauge
= new wxGauge(this, -1, maximum
,
142 wxDefaultPosition
, wxDefaultSize
,
145 c
= new wxLayoutConstraints
;
146 c
->left
.SameAs(this, wxLeft
, 2*LAYOUT_X_MARGIN
);
147 c
->top
.Below(m_msg
, 2*LAYOUT_Y_MARGIN
);
148 c
->right
.SameAs(this, wxRight
, 2*LAYOUT_X_MARGIN
);
150 m_gauge
->SetConstraints(c
);
151 m_gauge
->SetValue(0);
152 lastWindow
= m_gauge
;
154 wxSize sizeGauge
= m_gauge
->GetSize();
155 sizeDlg
.y
+= 2*LAYOUT_Y_MARGIN
+ sizeGauge
.y
;
158 m_gauge
= (wxGauge
*)NULL
;
160 // create the estimated/remaining/total time zones if requested
161 m_elapsed
= m_estimated
= m_remaining
= (wxStaticText
*)NULL
;
164 if ( style
& wxPD_ELAPSED_TIME
)
168 m_elapsed
= CreateLabel(_("Elapsed time : "), &lastWindow
);
171 if ( style
& wxPD_ESTIMATED_TIME
)
175 m_estimated
= CreateLabel(_("Estimated time : "), &lastWindow
);
178 if ( style
& wxPD_REMAINING_TIME
)
182 m_remaining
= CreateLabel(_("Remaining time : "), &lastWindow
);
185 if ( nTimeLabels
> 0 )
187 // set it to the current time
188 m_timeStart
= wxGetCurrentTime();
189 sizeDlg
.y
+= nTimeLabels
* (sizeLabel
.y
+ LAYOUT_Y_MARGIN
);
192 if ( hasAbortButton
)
194 m_btnAbort
= new wxButton(this, wxID_CANCEL
, _("Cancel"));
195 c
= new wxLayoutConstraints
;
197 // Windows dialogs usually have buttons in the lower right corner
199 c
->right
.SameAs(this, wxRight
, 2*LAYOUT_X_MARGIN
);
201 c
->centreX
.SameAs(this, wxCentreX
);
203 c
->bottom
.SameAs(this, wxBottom
, 2*LAYOUT_Y_MARGIN
);
205 wxSize sizeBtn
= wxButton::GetDefaultSize();
206 c
->width
.Absolute(sizeBtn
.x
);
207 c
->height
.Absolute(sizeBtn
.y
);
209 m_btnAbort
->SetConstraints(c
);
211 sizeDlg
.y
+= 2*LAYOUT_Y_MARGIN
+ sizeBtn
.y
;
214 m_btnAbort
= (wxButton
*)NULL
;
219 sizeDlg
.y
+= 2*LAYOUT_Y_MARGIN
;
221 // try to make the dialog not square but rectangular of reasonabel width
222 sizeDlg
.x
= (wxCoord
)wxMax(widthText
, 4*sizeDlg
.y
/3);
225 SetClientSize(sizeDlg
);
227 Centre(wxCENTER_FRAME
| wxBOTH
);
229 if ( style
& wxPD_APP_MODAL
)
231 m_winDisabler
= new wxWindowDisabler(this);
236 m_parentTop
->Enable(FALSE
);
237 m_winDisabler
= NULL
;
241 Enable(TRUE
); // enable this window
243 // this one can be initialized even if the others are unknown for now
245 // NB: do it after calling Layout() to keep the labels correctly aligned
248 SetTimeLabel(0, m_elapsed
);
251 // Update the display (especially on X, GTK)
255 MacUpdateImmediately();
259 wxStaticText
*wxProgressDialog::CreateLabel(const wxString
& text
,
260 wxWindow
**lastWindow
)
262 wxLayoutConstraints
*c
;
264 wxStaticText
*label
= new wxStaticText(this, -1, _("unknown"));
265 c
= new wxLayoutConstraints
;
267 // VZ: I like the labels be centered - if the others don't mind, you may
268 // remove "#ifdef __WXMSW__" and use it for all ports
270 c
->left
.SameAs(this, wxCentreX
, LAYOUT_X_MARGIN
);
272 c
->right
.SameAs(this, wxRight
, 2*LAYOUT_X_MARGIN
);
274 c
->top
.Below(*lastWindow
, LAYOUT_Y_MARGIN
);
277 label
->SetConstraints(c
);
279 wxStaticText
*dummy
= new wxStaticText(this, -1, text
);
280 c
= new wxLayoutConstraints
;
281 c
->right
.LeftOf(label
);
282 c
->top
.SameAs(label
, wxTop
, 0);
285 dummy
->SetConstraints(c
);
293 wxProgressDialog::Update(int value
, const wxString
& newmsg
)
295 wxASSERT_MSG( value
== -1 || m_gauge
, wxT("cannot update non existent dialog") );
296 wxASSERT_MSG( value
<= m_maximum
, wxT("invalid progress value") );
299 m_gauge
->SetValue(value
+ 1);
301 if ( !newmsg
.IsEmpty() )
304 // this seems to be necessary or garbage is left when the new label is
305 // longer than the old one
306 m_msg
->SetLabel(wxEmptyString
);
309 m_msg
->SetLabel(newmsg
);
314 if ( (m_elapsed
|| m_remaining
|| m_estimated
) && (value
!= 0) )
316 unsigned long elapsed
= wxGetCurrentTime() - m_timeStart
;
317 unsigned long estimated
= elapsed
* m_maximum
/ value
;
318 unsigned long remaining
= estimated
- elapsed
;
320 SetTimeLabel(elapsed
, m_elapsed
);
321 SetTimeLabel(estimated
, m_estimated
);
322 SetTimeLabel(remaining
, m_remaining
);
325 if ( (value
== m_maximum
) && !(GetWindowStyle() & wxPD_AUTO_HIDE
) )
329 // tell the user what he should do...
330 m_btnAbort
->SetLabel(_("Close"));
332 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
333 else // enable the close button to give the user a way to close the dlg
335 EnableCloseButton(TRUE
);
341 // also provide the finishing message if the application didn't
342 m_msg
->SetLabel(_("Done."));
345 // so that we return TRUE below and that out [Cancel] handler knew what
355 // update the display
360 MacUpdateImmediately();
363 return m_state
!= Canceled
;
366 // ----------------------------------------------------------------------------
368 // ----------------------------------------------------------------------------
370 void wxProgressDialog::OnCancel(wxCommandEvent
& event
)
372 if ( m_state
== Finished
)
374 // this means that the count down is already finished and we're being
375 // shown as a modal dialog - so just let the default handler do the job
380 // request to cancel was received, the next time Update() is called we
384 // update the button state immediately so that the user knows that the
385 // request has been noticed
386 m_btnAbort
->Disable();
390 void wxProgressDialog::OnClose(wxCloseEvent
& event
)
392 if ( m_state
== Uncancelable
)
394 // can't close this dialog
397 else if ( m_state
== Finished
)
399 // let the default handler close the window as we already terminated
404 // next Update() will notice it
409 void wxProgressDialog::OnShow(wxShowEvent
& event
)
411 // if the dialog is being hidden, it was closed, so reenable other windows
413 if ( event
.GetShow() )
415 ReenableOtherWindows();
419 // ----------------------------------------------------------------------------
421 // ----------------------------------------------------------------------------
423 wxProgressDialog::~wxProgressDialog()
425 // normally this should have been already done, but just in case
426 ReenableOtherWindows();
429 void wxProgressDialog::ReenableOtherWindows()
431 if ( GetWindowStyle() & wxPD_APP_MODAL
)
433 delete m_winDisabler
;
434 m_winDisabler
= (wxWindowDisabler
*)NULL
;
439 m_parentTop
->Enable(TRUE
);
443 // ----------------------------------------------------------------------------
445 // ----------------------------------------------------------------------------
447 static void SetTimeLabel(unsigned long val
, wxStaticText
*label
)
452 unsigned long hours
= val
/ 3600;
453 unsigned long minutes
= (val
% 3600) / 60;
454 unsigned long seconds
= val
% 60;
455 s
.Printf(wxT("%lu:%02lu:%02lu"), hours
, minutes
, seconds
);
457 if ( s
!= label
->GetLabel() )
462 #endif // wxUSE_PROGRESSDLG