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