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