GCC warning fix.
[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 licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
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/sizer.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 // macros
51 // ---------------------------------------------------------------------------
52
53 /* Macro for avoiding #ifdefs when value have to be different depending on size of
54 device we display on - take it from something like wxDesktopPolicy in the future
55 */
56
57 #if defined(__SMARTPHONE__)
58 #define wxLARGESMALL(large,small) small
59 #else
60 #define wxLARGESMALL(large,small) large
61 #endif
62
63 // ----------------------------------------------------------------------------
64 // constants
65 // ----------------------------------------------------------------------------
66
67 #define LAYOUT_MARGIN wxLARGESMALL(8,2)
68
69 static const int wxID_SKIP = 32000; // whatever
70
71 // ----------------------------------------------------------------------------
72 // private functions
73 // ----------------------------------------------------------------------------
74
75 // update the label to show the given time (in seconds)
76 static void SetTimeLabel(unsigned long val, wxStaticText *label);
77
78 // ----------------------------------------------------------------------------
79 // event tables
80 // ----------------------------------------------------------------------------
81
82 BEGIN_EVENT_TABLE(wxProgressDialog, wxDialog)
83 EVT_BUTTON(wxID_CANCEL, wxProgressDialog::OnCancel)
84 EVT_BUTTON(wxID_SKIP, wxProgressDialog::OnSkip)
85
86 EVT_CLOSE(wxProgressDialog::OnClose)
87 END_EVENT_TABLE()
88
89 IMPLEMENT_CLASS(wxProgressDialog, wxDialog)
90
91 // ============================================================================
92 // wxProgressDialog implementation
93 // ============================================================================
94
95 // ----------------------------------------------------------------------------
96 // wxProgressDialog creation
97 // ----------------------------------------------------------------------------
98
99 wxProgressDialog::wxProgressDialog(wxString const &title,
100 wxString const &message,
101 int maximum,
102 wxWindow *parent,
103 int style)
104 : wxDialog(parent, wxID_ANY, title),
105 m_skip(false),
106 m_delay(3)
107 {
108 // we may disappear at any moment, let the others know about it
109 SetExtraStyle(GetExtraStyle() | wxWS_EX_TRANSIENT);
110 m_windowStyle |= style;
111
112 bool hasAbortButton = (style & wxPD_CAN_ABORT) != 0;
113 bool hasSkipButton = (style & wxPD_CAN_SKIP) != 0;
114
115 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
116 // we have to remove the "Close" button from the title bar then as it is
117 // confusing to have it - it doesn't work anyhow
118 //
119 // FIXME: should probably have a (extended?) window style for this
120 if ( !hasAbortButton )
121 {
122 EnableCloseButton(false);
123 }
124 #endif // wxMSW
125
126 #if defined(__SMARTPHONE__)
127 SetLeftMenu();
128 #endif
129
130 m_state = hasAbortButton ? Continue : Uncancelable;
131 m_maximum = maximum;
132
133 #if defined(__WXMSW__) || defined(__WXPM__)
134 // we can't have values > 65,536 in the progress control under Windows, so
135 // scale everything down
136 m_factor = m_maximum / 65536 + 1;
137 m_maximum /= m_factor;
138 #endif // __WXMSW__
139
140 m_parentTop = wxGetTopLevelParent(parent);
141
142 wxClientDC dc(this);
143 dc.SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT));
144 long widthText;
145 dc.GetTextExtent(message, &widthText, NULL, NULL, NULL, NULL);
146
147 wxBoxSizer *sizer = new wxBoxSizer(wxVERTICAL);
148
149 m_msg = new wxStaticText(this, wxID_ANY, message);
150 sizer->Add(m_msg, 0, wxLEFT | wxTOP, 2*LAYOUT_MARGIN);
151
152 wxSize sizeDlg,
153 sizeLabel = m_msg->GetSize();
154 sizeDlg.y = 2*LAYOUT_MARGIN + sizeLabel.y;
155
156 if ( maximum > 0 )
157 {
158 int gauge_style = wxGA_HORIZONTAL;
159 if ( ( style & wxPD_SMOOTH ) == wxPD_SMOOTH )
160 gauge_style |= wxGA_SMOOTH;
161 m_gauge = new wxGauge(this, wxID_ANY, m_maximum,
162 wxDefaultPosition, wxDefaultSize,
163 gauge_style );
164
165 sizer->Add(m_gauge, 0, wxLEFT | wxRIGHT | wxTOP | wxEXPAND, 2*LAYOUT_MARGIN);
166 m_gauge->SetValue(0);
167
168 wxSize sizeGauge = m_gauge->GetSize();
169 sizeDlg.y += 2*LAYOUT_MARGIN + sizeGauge.y;
170 }
171 else
172 m_gauge = (wxGauge *)NULL;
173
174 // create the estimated/remaining/total time zones if requested
175 m_elapsed = m_estimated = m_remaining = (wxStaticText*)NULL;
176 m_display_estimated = m_last_timeupdate = m_break = 0;
177 m_ctdelay = 0;
178
179 // if we are going to have at least one label, remmeber it in this var
180 wxStaticText *label = NULL;
181
182 // also count how many labels we really have
183 size_t nTimeLabels = 0;
184
185 if ( style & wxPD_ELAPSED_TIME )
186 {
187 nTimeLabels++;
188
189 label =
190 m_elapsed = CreateLabel(_("Elapsed time : "), sizer);
191 }
192
193 if ( style & wxPD_ESTIMATED_TIME )
194 {
195 nTimeLabels++;
196
197 label =
198 m_estimated = CreateLabel(_("Estimated time : "), sizer);
199 }
200
201 if ( style & wxPD_REMAINING_TIME )
202 {
203 nTimeLabels++;
204
205 label =
206 m_remaining = CreateLabel(_("Remaining time : "), sizer);
207 }
208
209 if ( nTimeLabels > 0 )
210 {
211 // set it to the current time
212 m_timeStart = wxGetCurrentTime();
213 sizeDlg.y += nTimeLabels * (label->GetSize().y + LAYOUT_MARGIN);
214 }
215
216 #if defined(__SMARTPHONE__)
217 if ( hasSkipButton )
218 SetRightMenu(wxID_SKIP, _("Skip"));
219 if ( hasAbortButton )
220 SetLeftMenu(wxID_CANCEL);
221 #else
222 m_btnAbort = m_btnSkip = (wxButton *)NULL;
223 bool sizeDlgModified = false;
224 wxBoxSizer *buttonSizer = new wxBoxSizer(wxHORIZONTAL);
225
226 const int sizerFlags =
227 #if defined(__WXMSW__) || defined(__WXPM__)
228 wxALIGN_RIGHT | wxALL
229 #else // !MSW
230 wxALIGN_CENTER_HORIZONTAL | wxBOTTOM | wxTOP
231 #endif // MSW/!MSW
232 ;
233
234 if ( hasSkipButton )
235 {
236 m_btnSkip = new wxButton(this, wxID_SKIP, _("Skip"));
237
238 // Windows dialogs usually have buttons in the lower right corner
239 buttonSizer->Add(m_btnSkip, 0, sizerFlags, LAYOUT_MARGIN);
240 sizeDlg.y += 2*LAYOUT_MARGIN + wxButton::GetDefaultSize().y;
241 sizeDlgModified = true;
242 }
243
244 if ( hasAbortButton )
245 {
246 m_btnAbort = new wxButton(this, wxID_CANCEL);
247
248 // Windows dialogs usually have buttons in the lower right corner
249 buttonSizer->Add(m_btnAbort, 0, sizerFlags, LAYOUT_MARGIN);
250 if(!sizeDlgModified)
251 sizeDlg.y += 2*LAYOUT_MARGIN + wxButton::GetDefaultSize().y;
252 }
253
254 sizer->Add(buttonSizer, 0, sizerFlags, LAYOUT_MARGIN );
255 #endif // __SMARTPHONE__/!__SMARTPHONE__
256
257 SetSizerAndFit(sizer);
258
259 sizeDlg.y += 2*LAYOUT_MARGIN;
260
261 // try to make the dialog not square but rectangular of reasonabel width
262 sizeDlg.x = (wxCoord)wxMax(widthText, 4*sizeDlg.y/3);
263 sizeDlg.x *= 3;
264 sizeDlg.x /= 2;
265 SetClientSize(sizeDlg);
266
267 Centre(wxCENTER_FRAME | wxBOTH);
268
269 if ( style & wxPD_APP_MODAL )
270 {
271 m_winDisabler = new wxWindowDisabler(this);
272 }
273 else
274 {
275 if ( m_parentTop )
276 m_parentTop->Disable();
277 m_winDisabler = NULL;
278 }
279
280 Show();
281 Enable();
282
283 // this one can be initialized even if the others are unknown for now
284 //
285 // NB: do it after calling Layout() to keep the labels correctly aligned
286 if ( m_elapsed )
287 {
288 SetTimeLabel(0, m_elapsed);
289 }
290
291 Update();
292 }
293
294 wxStaticText *wxProgressDialog::CreateLabel(const wxString& text,
295 wxSizer *sizer)
296 {
297 wxBoxSizer *locsizer = new wxBoxSizer(wxLARGESMALL(wxHORIZONTAL,wxVERTICAL));
298
299 wxStaticText *dummy = new wxStaticText(this, wxID_ANY, text);
300 wxStaticText *label = new wxStaticText(this, wxID_ANY, _("unknown"));
301
302 // select placement most native or nice on target GUI
303 #if defined(__SMARTPHONE__)
304 // label and time to the left in two rows
305 locsizer->Add(dummy, 1, wxALIGN_LEFT);
306 locsizer->Add(label, 1, wxALIGN_LEFT);
307 sizer->Add(locsizer, 0, wxALIGN_LEFT | wxTOP | wxLEFT, LAYOUT_MARGIN);
308 #elif defined(__WXMSW__) || defined(__WXPM__) || defined(__WXMAC__)
309 // label and time centered in one row
310 locsizer->Add(dummy, 1, wxLARGESMALL(wxALIGN_RIGHT,wxALIGN_LEFT));
311 locsizer->Add(label, 1, wxALIGN_LEFT | wxLEFT, LAYOUT_MARGIN);
312 sizer->Add(locsizer, 0, wxALIGN_CENTER_HORIZONTAL | wxTOP, LAYOUT_MARGIN);
313 #else
314 // label and time to the right in one row
315 sizer->Add(locsizer, 0, wxALIGN_RIGHT | wxRIGHT | wxTOP, LAYOUT_MARGIN);
316 locsizer->Add(dummy);
317 locsizer->Add(label, 0, wxLEFT, LAYOUT_MARGIN);
318 #endif
319
320 return label;
321 }
322
323 // ----------------------------------------------------------------------------
324 // wxProgressDialog operations
325 // ----------------------------------------------------------------------------
326
327 bool
328 wxProgressDialog::Update(int value, const wxString& newmsg, bool *skip)
329 {
330 wxASSERT_MSG( value == -1 || m_gauge, wxT("cannot update non existent dialog") );
331
332 #ifdef __WXMSW__
333 value /= m_factor;
334 #endif // __WXMSW__
335
336 wxASSERT_MSG( value <= m_maximum, wxT("invalid progress value") );
337
338 // fill up the gauge if value == maximum because this means that the dialog
339 // is going to close and the gauge shouldn't be partly empty in this case
340 if ( m_gauge && value <= m_maximum )
341 {
342 m_gauge->SetValue(value == m_maximum ? value : value + 1);
343 }
344
345 if ( !newmsg.empty() && newmsg != m_msg->GetLabel() )
346 {
347 m_msg->SetLabel(newmsg);
348
349 wxYieldIfNeeded() ;
350 }
351
352 if ( (m_elapsed || m_remaining || m_estimated) && (value != 0) )
353 {
354 unsigned long elapsed = wxGetCurrentTime() - m_timeStart;
355 if ( m_last_timeupdate < elapsed
356 || value == m_maximum
357 )
358 {
359 m_last_timeupdate = elapsed;
360 unsigned long estimated = m_break +
361 (unsigned long)(( (double) (elapsed-m_break) * m_maximum ) / ((double)value)) ;
362 if ( estimated > m_display_estimated
363 && m_ctdelay >= 0
364 )
365 {
366 ++m_ctdelay;
367 }
368 else if ( estimated < m_display_estimated
369 && m_ctdelay <= 0
370 )
371 {
372 --m_ctdelay;
373 }
374 else
375 {
376 m_ctdelay = 0;
377 }
378 if ( m_ctdelay >= m_delay // enough confirmations for a higher value
379 || m_ctdelay <= (m_delay*-1) // enough confirmations for a lower value
380 || value == m_maximum // to stay consistent
381 || elapsed > m_display_estimated // to stay consistent
382 || ( elapsed > 0 && elapsed < 4 ) // additional updates in the beginning
383 )
384 {
385 m_display_estimated = estimated;
386 m_ctdelay = 0;
387 }
388 }
389
390 long display_remaining = m_display_estimated - elapsed;
391 if ( display_remaining < 0 )
392 {
393 display_remaining = 0;
394 }
395
396 SetTimeLabel(elapsed, m_elapsed);
397 SetTimeLabel(m_display_estimated, m_estimated);
398 SetTimeLabel(display_remaining, m_remaining);
399 }
400
401 if ( value == m_maximum )
402 {
403 if ( m_state == Finished )
404 {
405 // ignore multiple calls to Update(m_maximum): it may sometimes be
406 // troublesome to ensure that Update() is not called twice with the
407 // same value (e.g. because of the rounding errors) and if we don't
408 // return now we're going to generate asserts below
409 return true;
410 }
411
412 // so that we return true below and that out [Cancel] handler knew what
413 // to do
414 m_state = Finished;
415 if( !(GetWindowStyle() & wxPD_AUTO_HIDE) )
416 {
417 #if defined(__SMARTPHONE__)
418 SetLeftMenu(wxID_CANCEL, _("Close"));
419 SetRightMenu();
420 #endif
421 if ( m_btnSkip )
422 {
423 // tell the user what he should do...
424 m_btnSkip->Disable();
425 }
426
427 if ( m_btnAbort )
428 {
429 // tell the user what he should do...
430 m_btnAbort->SetLabel(_("Close"));
431 }
432 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
433 else // enable the button to give the user a way to close the dlg
434 {
435 EnableCloseButton();
436 }
437 #endif // __WXMSW__
438
439 if ( !newmsg.empty() )
440 {
441 // also provide the finishing message if the application didn't
442 m_msg->SetLabel(_("Done."));
443 }
444
445 wxYieldIfNeeded() ;
446
447 (void)ShowModal();
448 }
449 else // auto hide
450 {
451 // reenable other windows before hiding this one because otherwise
452 // Windows wouldn't give the focus back to the window which had
453 // been previously focused because it would still be disabled
454 ReenableOtherWindows();
455
456 Hide();
457 }
458 }
459 else
460 {
461 // we have to yield because not only we want to update the display but
462 // also to process the clicks on the cancel and skip buttons
463 wxYieldIfNeeded() ;
464
465 if ( (m_skip) && (skip != NULL) && (*skip == false) )
466 {
467 *skip = true;
468 m_skip = false;
469 if(m_btnSkip)
470 m_btnSkip->Enable();
471 }
472 }
473
474 // update the display in case yielding above didn't do it
475 Update();
476
477 return m_state != Canceled;
478 }
479
480 void wxProgressDialog::Resume()
481 {
482 m_state = Continue;
483 m_ctdelay = m_delay; // force an update of the elapsed/estimated/remaining time
484 m_break += wxGetCurrentTime()-m_timeStop;
485
486 // it may have been disabled by OnCancel(), so enable it back to let the
487 // user interrupt us again if needed
488 if(m_btnAbort)
489 m_btnAbort->Enable();
490
491 // enable skipping because the one before OnCancel() is no more valid
492 m_skip = false;
493 if(m_btnSkip)
494 m_btnSkip->Enable();
495
496 #if defined(__SMARTPHONE__)
497 SetLeftMenu(wxID_CANCEL, _("Cancel"));
498 #endif
499 }
500
501 bool wxProgressDialog::Show( bool show )
502 {
503 // reenable other windows before hiding this one because otherwise
504 // Windows wouldn't give the focus back to the window which had
505 // been previously focused because it would still be disabled
506 if(!show)
507 ReenableOtherWindows();
508
509 return wxDialog::Show(show);
510 }
511
512 // ----------------------------------------------------------------------------
513 // event handlers
514 // ----------------------------------------------------------------------------
515
516 void wxProgressDialog::OnCancel(wxCommandEvent& event)
517 {
518 if ( m_state == Finished )
519 {
520 // this means that the count down is already finished and we're being
521 // shown as a modal dialog - so just let the default handler do the job
522 event.Skip();
523 }
524 else
525 {
526 // request to cancel was received, the next time Update() is called we
527 // will handle it
528 m_state = Canceled;
529
530 // update the button state immediately so that the user knows that the
531 // request has been noticed
532 if(m_btnAbort)
533 m_btnAbort->Disable();
534 if(m_btnSkip)
535 m_btnSkip->Disable();
536
537 #if defined(__SMARTPHONE__)
538 SetLeftMenu();
539 #endif
540
541 // save the time when the dialog was stopped
542 m_timeStop = wxGetCurrentTime();
543 }
544 }
545
546 void wxProgressDialog::OnSkip(wxCommandEvent& WXUNUSED(event))
547 {
548 if(m_btnSkip)
549 m_btnSkip->Disable();
550 m_skip = true;
551 }
552
553 void wxProgressDialog::OnClose(wxCloseEvent& event)
554 {
555 if ( m_state == Uncancelable )
556 {
557 // can't close this dialog
558 event.Veto();
559 }
560 else if ( m_state == Finished )
561 {
562 // let the default handler close the window as we already terminated
563 event.Skip();
564 }
565 else
566 {
567 // next Update() will notice it
568 m_state = Canceled;
569 if(m_btnAbort)
570 m_btnAbort->Disable();
571 if(m_btnSkip)
572 m_btnSkip->Disable();
573
574 #if defined(__SMARTPHONE__)
575 SetLeftMenu();
576 #endif
577 m_timeStop = wxGetCurrentTime();
578 }
579 }
580
581 // ----------------------------------------------------------------------------
582 // destruction
583 // ----------------------------------------------------------------------------
584
585 wxProgressDialog::~wxProgressDialog()
586 {
587 // normally this should have been already done, but just in case
588 ReenableOtherWindows();
589 }
590
591 void wxProgressDialog::ReenableOtherWindows()
592 {
593 if ( GetWindowStyle() & wxPD_APP_MODAL )
594 {
595 delete m_winDisabler;
596 m_winDisabler = (wxWindowDisabler *)NULL;
597 }
598 else
599 {
600 if ( m_parentTop )
601 m_parentTop->Enable();
602 }
603 }
604
605 // ----------------------------------------------------------------------------
606 // private functions
607 // ----------------------------------------------------------------------------
608
609 static void SetTimeLabel(unsigned long val, wxStaticText *label)
610 {
611 if ( label )
612 {
613 wxString s;
614 unsigned long hours = val / 3600;
615 unsigned long minutes = (val % 3600) / 60;
616 unsigned long seconds = val % 60;
617 s.Printf(wxT("%lu:%02lu:%02lu"), hours, minutes, seconds);
618
619 if ( s != label->GetLabel() )
620 label->SetLabel(s);
621 }
622 }
623
624 #endif // wxUSE_PROGRESSDLG