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