reverted previous change - it doesn't fix the bug I wanted to fix
[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 #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
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,
132 sizeLabel = m_msg->GetSize();
133 sizeDlg.y = 2*LAYOUT_Y_MARGIN + sizeLabel.y;
134
135 wxWindow *lastWindow = m_msg;
136
137 if ( maximum > 0 )
138 {
139 // note that we can't use wxGA_SMOOTH because it happens to also mean
140 // wxDIALOG_MODAL and will cause the dialog to be modal. Have an extra
141 // style argument to wxProgressDialog, perhaps.
142 m_gauge = new wxGauge(this, -1, maximum,
143 wxDefaultPosition, wxDefaultSize,
144 wxGA_HORIZONTAL);
145
146 c = new wxLayoutConstraints;
147 c->left.SameAs(this, wxLeft, 2*LAYOUT_X_MARGIN);
148 c->top.Below(m_msg, 2*LAYOUT_Y_MARGIN);
149 c->right.SameAs(this, wxRight, 2*LAYOUT_X_MARGIN);
150 c->height.AsIs();
151 m_gauge->SetConstraints(c);
152 m_gauge->SetValue(0);
153 lastWindow = m_gauge;
154
155 wxSize sizeGauge = m_gauge->GetSize();
156 sizeDlg.y += 2*LAYOUT_Y_MARGIN + sizeGauge.y;
157 }
158 else
159 m_gauge = (wxGauge *)NULL;
160
161 // create the estimated/remaining/total time zones if requested
162 m_elapsed = m_estimated = m_remaining = (wxStaticText*)NULL;
163
164 // if we are going to have at least one label, remmeber it in this var
165 wxStaticText *label = NULL;
166
167 // also count how many labels we really have
168 size_t nTimeLabels = 0;
169
170 if ( style & wxPD_ELAPSED_TIME )
171 {
172 nTimeLabels++;
173
174 label =
175 m_elapsed = CreateLabel(_("Elapsed time : "), &lastWindow);
176 }
177
178 if ( style & wxPD_ESTIMATED_TIME )
179 {
180 nTimeLabels++;
181
182 label =
183 m_estimated = CreateLabel(_("Estimated time : "), &lastWindow);
184 }
185
186 if ( style & wxPD_REMAINING_TIME )
187 {
188 nTimeLabels++;
189
190 label =
191 m_remaining = CreateLabel(_("Remaining time : "), &lastWindow);
192 }
193
194 if ( nTimeLabels > 0 )
195 {
196 // set it to the current time
197 m_timeStart = wxGetCurrentTime();
198 sizeDlg.y += nTimeLabels * (label->GetSize().y + LAYOUT_Y_MARGIN);
199 }
200
201 if ( hasAbortButton )
202 {
203 m_btnAbort = new wxButton(this, wxID_CANCEL, _("Cancel"));
204 c = new wxLayoutConstraints;
205
206 // Windows dialogs usually have buttons in the lower right corner
207 #ifdef __WXMSW__
208 c->right.SameAs(this, wxRight, 2*LAYOUT_X_MARGIN);
209 #else // !MSW
210 c->centreX.SameAs(this, wxCentreX);
211 #endif // MSW/!MSW
212 c->bottom.SameAs(this, wxBottom, 2*LAYOUT_Y_MARGIN);
213
214 wxSize sizeBtn = wxButton::GetDefaultSize();
215 c->width.Absolute(sizeBtn.x);
216 c->height.Absolute(sizeBtn.y);
217
218 m_btnAbort->SetConstraints(c);
219
220 sizeDlg.y += 2*LAYOUT_Y_MARGIN + sizeBtn.y;
221 }
222 else
223 m_btnAbort = (wxButton *)NULL;
224
225 SetAutoLayout(TRUE);
226 Layout();
227
228 sizeDlg.y += 2*LAYOUT_Y_MARGIN;
229
230 // try to make the dialog not square but rectangular of reasonabel width
231 sizeDlg.x = (wxCoord)wxMax(widthText, 4*sizeDlg.y/3);
232 sizeDlg.x *= 3;
233 sizeDlg.x /= 2;
234 SetClientSize(sizeDlg);
235
236 Centre(wxCENTER_FRAME | wxBOTH);
237
238 if ( style & wxPD_APP_MODAL )
239 {
240 m_winDisabler = new wxWindowDisabler(this);
241 }
242 else
243 {
244 if ( m_parentTop )
245 m_parentTop->Enable(FALSE);
246 m_winDisabler = NULL;
247 }
248
249 Show(TRUE);
250 Enable(TRUE); // enable this window
251
252 // this one can be initialized even if the others are unknown for now
253 //
254 // NB: do it after calling Layout() to keep the labels correctly aligned
255 if ( m_elapsed )
256 {
257 SetTimeLabel(0, m_elapsed);
258 }
259
260 // Update the display (especially on X, GTK)
261 wxYield();
262
263 #ifdef __WXMAC__
264 MacUpdateImmediately();
265 #endif
266 }
267
268 wxStaticText *wxProgressDialog::CreateLabel(const wxString& text,
269 wxWindow **lastWindow)
270 {
271 wxLayoutConstraints *c;
272
273 wxStaticText *label = new wxStaticText(this, -1, _("unknown"));
274 c = new wxLayoutConstraints;
275
276 // VZ: I like the labels be centered - if the others don't mind, you may
277 // remove "#ifdef __WXMSW__" and use it for all ports
278 #ifdef __WXMSW__
279 c->left.SameAs(this, wxCentreX, LAYOUT_X_MARGIN);
280 #else // !MSW
281 c->right.SameAs(this, wxRight, 2*LAYOUT_X_MARGIN);
282 #endif // MSW/!MSW
283 c->top.Below(*lastWindow, LAYOUT_Y_MARGIN);
284 c->width.AsIs();
285 c->height.AsIs();
286 label->SetConstraints(c);
287
288 wxStaticText *dummy = new wxStaticText(this, -1, text);
289 c = new wxLayoutConstraints;
290 c->right.LeftOf(label);
291 c->top.SameAs(label, wxTop, 0);
292 c->width.AsIs();
293 c->height.AsIs();
294 dummy->SetConstraints(c);
295
296 *lastWindow = label;
297
298 return label;
299 }
300
301 bool
302 wxProgressDialog::Update(int value, const wxString& newmsg)
303 {
304 wxASSERT_MSG( value == -1 || m_gauge, wxT("cannot update non existent dialog") );
305 wxASSERT_MSG( value <= m_maximum, wxT("invalid progress value") );
306
307 if ( m_gauge )
308 m_gauge->SetValue(value + 1);
309
310 if ( !newmsg.IsEmpty() )
311 {
312 #ifdef __WXMSW__
313 // this seems to be necessary or garbage is left when the new label is
314 // longer than the old one
315 m_msg->SetLabel(wxEmptyString);
316 #endif // MSW
317
318 m_msg->SetLabel(newmsg);
319
320 wxYield();
321 }
322
323 if ( (m_elapsed || m_remaining || m_estimated) && (value != 0) )
324 {
325 unsigned long elapsed = wxGetCurrentTime() - m_timeStart;
326 unsigned long estimated = elapsed * m_maximum / value;
327 unsigned long remaining = estimated - elapsed;
328
329 SetTimeLabel(elapsed, m_elapsed);
330 SetTimeLabel(estimated, m_estimated);
331 SetTimeLabel(remaining, m_remaining);
332 }
333
334 if ( (value == m_maximum ) && !(GetWindowStyle() & wxPD_AUTO_HIDE) )
335 {
336 if ( m_btnAbort )
337 {
338 // tell the user what he should do...
339 m_btnAbort->SetLabel(_("Close"));
340 }
341 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
342 else // enable the close button to give the user a way to close the dlg
343 {
344 EnableCloseButton(TRUE);
345 }
346 #endif // __WXMSW__
347
348 if ( !newmsg )
349 {
350 // also provide the finishing message if the application didn't
351 m_msg->SetLabel(_("Done."));
352 }
353
354 // so that we return TRUE below and that out [Cancel] handler knew what
355 // to do
356 m_state = Finished;
357
358 wxYield();
359
360 (void)ShowModal();
361 }
362 else
363 {
364 // update the display
365 wxYield();
366 }
367
368 #ifdef __WXMAC__
369 MacUpdateImmediately();
370 #endif
371
372 return m_state != Canceled;
373 }
374
375 // ----------------------------------------------------------------------------
376 // event handlers
377 // ----------------------------------------------------------------------------
378
379 void wxProgressDialog::OnCancel(wxCommandEvent& event)
380 {
381 if ( m_state == Finished )
382 {
383 // this means that the count down is already finished and we're being
384 // shown as a modal dialog - so just let the default handler do the job
385 event.Skip();
386 }
387 else
388 {
389 // request to cancel was received, the next time Update() is called we
390 // will handle it
391 m_state = Canceled;
392
393 // update the button state immediately so that the user knows that the
394 // request has been noticed
395 m_btnAbort->Disable();
396 }
397 }
398
399 void wxProgressDialog::OnClose(wxCloseEvent& event)
400 {
401 if ( m_state == Uncancelable )
402 {
403 // can't close this dialog
404 event.Veto(TRUE);
405 }
406 else if ( m_state == Finished )
407 {
408 // let the default handler close the window as we already terminated
409 event.Skip();
410 }
411 else
412 {
413 // next Update() will notice it
414 m_state = Canceled;
415 }
416 }
417
418 void wxProgressDialog::OnShow(wxShowEvent& event)
419 {
420 // if the dialog is being hidden, it was closed, so reenable other windows
421 // now
422 if ( event.GetShow() )
423 {
424 ReenableOtherWindows();
425 }
426 }
427
428 // ----------------------------------------------------------------------------
429 // destruction
430 // ----------------------------------------------------------------------------
431
432 wxProgressDialog::~wxProgressDialog()
433 {
434 // normally this should have been already done, but just in case
435 ReenableOtherWindows();
436 }
437
438 void wxProgressDialog::ReenableOtherWindows()
439 {
440 if ( GetWindowStyle() & wxPD_APP_MODAL )
441 {
442 delete m_winDisabler;
443 m_winDisabler = (wxWindowDisabler *)NULL;
444 }
445 else
446 {
447 if ( m_parentTop )
448 m_parentTop->Enable(TRUE);
449 }
450 }
451
452 // ----------------------------------------------------------------------------
453 // private functions
454 // ----------------------------------------------------------------------------
455
456 static void SetTimeLabel(unsigned long val, wxStaticText *label)
457 {
458 if ( label )
459 {
460 wxString s;
461 unsigned long hours = val / 3600;
462 unsigned long minutes = (val % 3600) / 60;
463 unsigned long seconds = val % 60;
464 s.Printf(wxT("%lu:%02lu:%02lu"), hours, minutes, seconds);
465
466 if ( s != label->GetLabel() )
467 label->SetLabel(s);
468 }
469 }
470
471 #endif // wxUSE_PROGRESSDLG