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