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