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