]> git.saurik.com Git - wxWidgets.git/blob - src/generic/progdlgg.cpp
Project file updates; cast in strconv.cpp to compile with VC++ 6
[wxWidgets.git] / src / generic / progdlgg.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: progdlgg.h
3 // Purpose: wxProgressDialog class
4 // Author: Karsten Ballüder
5 // Modified by:
6 // Created: 09.05.1999
7 // RCS-ID: $Id$
8 // Copyright: (c) Karsten Ballüder
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #ifdef __GNUG__
21 #pragma implementation "progdlgg.h"
22 #endif
23
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
26
27 #ifdef __BORLANDC__
28 #pragma hdrstop
29 #endif
30
31 #if wxUSE_PROGRESSDLG
32
33 #ifndef WX_PRECOMP
34 #include "wx/utils.h"
35 #include "wx/frame.h"
36 #include "wx/button.h"
37 #include "wx/stattext.h"
38 #include "wx/layout.h"
39 #include "wx/event.h"
40 #include "wx/gauge.h"
41 #include "wx/intl.h"
42 #include "wx/settings.h"
43 #include "wx/dcclient.h"
44 #include "wx/timer.h"
45 #endif
46
47 #include "wx/generic/progdlgg.h"
48
49 // ----------------------------------------------------------------------------
50 // constants
51 // ----------------------------------------------------------------------------
52
53 #define LAYOUT_X_MARGIN 8
54 #define LAYOUT_Y_MARGIN 8
55
56 // ----------------------------------------------------------------------------
57 // private functions
58 // ----------------------------------------------------------------------------
59
60 // update the label to show the given time (in seconds)
61 static void SetTimeLabel(unsigned long val, wxStaticText *label);
62
63 // ----------------------------------------------------------------------------
64 // event tables
65 // ----------------------------------------------------------------------------
66
67 BEGIN_EVENT_TABLE(wxProgressDialog, wxDialog)
68 EVT_BUTTON(wxID_CANCEL, wxProgressDialog::OnCancel)
69
70 EVT_SHOW(wxProgressDialog::OnShow)
71
72 EVT_CLOSE(wxProgressDialog::OnClose)
73 END_EVENT_TABLE()
74
75 IMPLEMENT_CLASS(wxProgressDialog, wxDialog)
76
77 // ============================================================================
78 // implementation
79 // ============================================================================
80
81 // ----------------------------------------------------------------------------
82 // wxProgressDialog
83 // ----------------------------------------------------------------------------
84
85 wxProgressDialog::wxProgressDialog(wxString const &title,
86 wxString const &message,
87 int maximum,
88 wxWindow *parent,
89 int style)
90 : wxDialog(parent, -1, title)
91 {
92 m_windowStyle |= style;
93
94 bool hasAbortButton = (style & wxPD_CAN_ABORT) != 0;
95
96 #ifdef __WXMSW__
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
99 //
100 // FIXME: should probably have a (extended?) window style for this
101 if ( !hasAbortButton )
102 {
103 EnableCloseButton(FALSE);
104 }
105 #endif // wxMSW
106
107 m_state = hasAbortButton ? Continue : Uncancelable;
108 m_maximum = maximum;
109
110 m_parentTop = parent;
111 while ( m_parentTop && m_parentTop->GetParent() )
112 {
113 m_parentTop = m_parentTop->GetParent();
114 }
115
116 wxLayoutConstraints *c;
117
118 wxClientDC dc(this);
119 dc.SetFont(wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT));
120 long widthText;
121 dc.GetTextExtent(message, &widthText, NULL, NULL, NULL, NULL);
122
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);
127 c->width.AsIs();
128 c->height.AsIs();
129 m_msg->SetConstraints(c);
130
131 wxSize sizeDlg, sizeLabel = m_msg->GetSize();
132 sizeDlg.y = 2*LAYOUT_Y_MARGIN + sizeLabel.y;
133
134 wxWindow *lastWindow = m_msg;
135
136 if ( maximum > 0 )
137 {
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,
143 wxGA_HORIZONTAL);
144
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);
149 c->height.AsIs();
150 m_gauge->SetConstraints(c);
151 m_gauge->SetValue(0);
152 lastWindow = m_gauge;
153
154 wxSize sizeGauge = m_gauge->GetSize();
155 sizeDlg.y += 2*LAYOUT_Y_MARGIN + sizeGauge.y;
156 }
157 else
158 m_gauge = (wxGauge *)NULL;
159
160 // create the estimated/remaining/total time zones if requested
161 m_elapsed = m_estimated = m_remaining = (wxStaticText*)NULL;
162
163 int nTimeLabels = 0;
164 if ( style & wxPD_ELAPSED_TIME )
165 {
166 nTimeLabels++;
167
168 m_elapsed = CreateLabel(_("Elapsed time : "), &lastWindow);
169 SetTimeLabel(0, m_elapsed);
170 }
171
172 if ( style & wxPD_ESTIMATED_TIME )
173 {
174 nTimeLabels++;
175
176 m_estimated = CreateLabel(_("Estimated time : "), &lastWindow);
177 }
178
179 if ( style & wxPD_REMAINING_TIME )
180 {
181 nTimeLabels++;
182
183 m_remaining = CreateLabel(_("Remaining time : "), &lastWindow);
184 }
185
186 if ( nTimeLabels > 0 )
187 {
188 // set it to the current time
189 m_timeStart = wxGetCurrentTime();
190 sizeDlg.y += nTimeLabels * (sizeLabel.y + LAYOUT_Y_MARGIN);
191 }
192
193 if ( hasAbortButton )
194 {
195 m_btnAbort = new wxButton(this, wxID_CANCEL, _("Cancel"));
196 c = new wxLayoutConstraints;
197
198 // Windows dialogs usually have buttons in the lower right corner
199 #ifdef __WXMSW__
200 c->right.SameAs(this, wxRight, 2*LAYOUT_X_MARGIN);
201 #else // !MSW
202 c->centreX.SameAs(this, wxCentreX);
203 #endif // MSW/!MSW
204 c->bottom.SameAs(this, wxBottom, 2*LAYOUT_Y_MARGIN);
205
206 wxSize sizeBtn = wxButton::GetDefaultSize();
207 c->width.Absolute(sizeBtn.x);
208 c->height.Absolute(sizeBtn.y);
209
210 m_btnAbort->SetConstraints(c);
211
212 sizeDlg.y += 2*LAYOUT_Y_MARGIN + sizeBtn.y;
213 }
214 else
215 m_btnAbort = (wxButton *)NULL;
216
217 SetAutoLayout(TRUE);
218 Layout();
219
220 sizeDlg.y += 2*LAYOUT_Y_MARGIN;
221
222 // try to make the dialog not square but rectangular of reasonabel width
223 sizeDlg.x = (wxCoord)wxMax(widthText, 4*sizeDlg.y/3);
224 sizeDlg.x *= 3;
225 sizeDlg.x /= 2;
226 SetClientSize(sizeDlg);
227
228 Centre(wxCENTER_FRAME | wxBOTH);
229
230 if ( style & wxPD_APP_MODAL )
231 {
232 m_winDisabler = new wxWindowDisabler(this);
233 }
234 else
235 {
236 if ( m_parentTop )
237 m_parentTop->Enable(FALSE);
238 m_winDisabler = NULL;
239 }
240
241 Show(TRUE);
242 Enable(TRUE); // enable this window
243
244 // Update the display (especially on X, GTK)
245 wxYield();
246
247 #ifdef __WXMAC__
248 MacUpdateImmediately();
249 #endif
250 }
251
252 wxStaticText *wxProgressDialog::CreateLabel(const wxString& text,
253 wxWindow **lastWindow)
254 {
255 wxLayoutConstraints *c;
256
257 wxStaticText *label = new wxStaticText(this, -1, _("unknown"));
258 c = new wxLayoutConstraints;
259
260 // VZ: I like the labels be centered - if the others don't mind, you may
261 // remove "#ifdef __WXMSW__" and use it for all ports
262 #ifdef __WXMSW__
263 c->left.SameAs(this, wxCentreX, LAYOUT_X_MARGIN);
264 #else // !MSW
265 c->right.SameAs(this, wxRight, 2*LAYOUT_X_MARGIN);
266 #endif // MSW/!MSW
267 c->top.Below(*lastWindow, LAYOUT_Y_MARGIN);
268 c->width.AsIs();
269 c->height.AsIs();
270 label->SetConstraints(c);
271
272 wxStaticText *dummy = new wxStaticText(this, -1, text);
273 c = new wxLayoutConstraints;
274 c->right.LeftOf(label);
275 c->top.SameAs(label, wxTop, 0);
276 c->width.AsIs();
277 c->height.AsIs();
278 dummy->SetConstraints(c);
279
280 *lastWindow = label;
281
282 return label;
283 }
284
285 bool
286 wxProgressDialog::Update(int value, const wxString& newmsg)
287 {
288 wxASSERT_MSG( value == -1 || m_gauge, wxT("cannot update non existent dialog") );
289 wxASSERT_MSG( value <= m_maximum, wxT("invalid progress value") );
290
291 if ( m_gauge )
292 m_gauge->SetValue(value + 1);
293
294 if ( !newmsg.IsEmpty() )
295 {
296 #ifdef __WXMSW__
297 // this seems to be necessary or garbage is left when the new label is
298 // longer than the old one
299 m_msg->SetLabel(wxEmptyString);
300 #endif // MSW
301
302 m_msg->SetLabel(newmsg);
303
304 wxYield();
305 }
306
307 if ( (m_elapsed || m_remaining || m_estimated) && (value != 0) )
308 {
309 unsigned long elapsed = wxGetCurrentTime() - m_timeStart;
310 unsigned long estimated = elapsed * m_maximum / value;
311 unsigned long remaining = estimated - elapsed;
312
313 SetTimeLabel(elapsed, m_elapsed);
314 SetTimeLabel(estimated, m_estimated);
315 SetTimeLabel(remaining, m_remaining);
316 }
317
318 if ( (value == m_maximum ) && !(GetWindowStyle() & wxPD_AUTO_HIDE) )
319 {
320 if ( m_btnAbort )
321 {
322 // tell the user what he should do...
323 m_btnAbort->SetLabel(_("Close"));
324 }
325 #ifdef __WXMSW__
326 else // enable the close button to give the user a way to close the dlg
327 {
328 EnableCloseButton(TRUE);
329 }
330 #endif // __WXMSW__
331
332 if ( !newmsg )
333 {
334 // also provide the finishing message if the application didn't
335 m_msg->SetLabel(_("Done."));
336 }
337
338 // so that we return TRUE below and that out [Cancel] handler knew what
339 // to do
340 m_state = Finished;
341
342 wxYield();
343
344 (void)ShowModal();
345 }
346 else
347 {
348 // update the display
349 wxYield();
350 }
351
352 #ifdef __WXMAC__
353 MacUpdateImmediately();
354 #endif
355
356 return m_state != Canceled;
357 }
358
359 // ----------------------------------------------------------------------------
360 // event handlers
361 // ----------------------------------------------------------------------------
362
363 void wxProgressDialog::OnCancel(wxCommandEvent& event)
364 {
365 if ( m_state == Finished )
366 {
367 // this means that the count down is already finished and we're being
368 // shown as a modal dialog - so just let the default handler do the job
369 event.Skip();
370 }
371 else
372 {
373 // request to cancel was received, the next time Update() is called we
374 // will handle it
375 m_state = Canceled;
376
377 // update the button state immediately so that the user knows that the
378 // request has been noticed
379 m_btnAbort->Disable();
380 }
381 }
382
383 void wxProgressDialog::OnClose(wxCloseEvent& event)
384 {
385 if ( m_state == Uncancelable )
386 {
387 // can't close this dialog
388 event.Veto(TRUE);
389 }
390 else if ( m_state == Finished )
391 {
392 // let the default handler close the window as we already terminated
393 event.Skip();
394 }
395 else
396 {
397 // next Update() will notice it
398 m_state = Canceled;
399 }
400 }
401
402 void wxProgressDialog::OnShow(wxShowEvent& event)
403 {
404 // if the dialog is being hidden, it was closed, so reenable other windows
405 // now
406 if ( event.GetShow() )
407 {
408 ReenableOtherWindows();
409 }
410 }
411
412 // ----------------------------------------------------------------------------
413 // destruction
414 // ----------------------------------------------------------------------------
415
416 wxProgressDialog::~wxProgressDialog()
417 {
418 // normally this should have been already done, but just in case
419 ReenableOtherWindows();
420 }
421
422 void wxProgressDialog::ReenableOtherWindows()
423 {
424 if ( GetWindowStyle() & wxPD_APP_MODAL )
425 {
426 delete m_winDisabler;
427 m_winDisabler = (wxWindowDisabler *)NULL;
428 }
429 else
430 {
431 if ( m_parentTop )
432 m_parentTop->Enable(TRUE);
433 }
434 }
435
436 // ----------------------------------------------------------------------------
437 // private functions
438 // ----------------------------------------------------------------------------
439
440 static void SetTimeLabel(unsigned long val, wxStaticText *label)
441 {
442 if ( label )
443 {
444 wxString s;
445 unsigned long hours = val / 3600;
446 unsigned long minutes = (val % 3600) / 60;
447 unsigned long seconds = val % 60;
448 s.Printf(wxT("%lu:%02lu:%02lu"), hours, minutes, seconds);
449
450 if ( s != label->GetLabel() )
451 label->SetLabel(s);
452 }
453 }
454
455 #endif // wxUSE_PROGRESSDLG