]> git.saurik.com Git - wxWidgets.git/blob - src/generic/progdlgg.cpp
Account for largest item in wxGenericListCtrl::GetBestSize().
[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 #include "wx/app.h"
42 #endif
43
44 #include "wx/progdlg.h"
45 #include "wx/evtloop.h"
46
47 // ---------------------------------------------------------------------------
48 // macros
49 // ---------------------------------------------------------------------------
50
51 /* Macro for avoiding #ifdefs when value have to be different depending on size of
52 device we display on - take it from something like wxDesktopPolicy in the future
53 */
54
55 #if defined(__SMARTPHONE__)
56 #define wxLARGESMALL(large,small) small
57 #else
58 #define wxLARGESMALL(large,small) large
59 #endif
60
61 // ----------------------------------------------------------------------------
62 // constants
63 // ----------------------------------------------------------------------------
64
65 #define LAYOUT_MARGIN wxLARGESMALL(8,2)
66
67 static const int wxID_SKIP = 32000; // whatever
68
69 // ----------------------------------------------------------------------------
70 // private functions
71 // ----------------------------------------------------------------------------
72
73 // update the label to show the given time (in seconds)
74 static void SetTimeLabel(unsigned long val, wxStaticText *label);
75
76 // ----------------------------------------------------------------------------
77 // event tables
78 // ----------------------------------------------------------------------------
79
80 BEGIN_EVENT_TABLE(wxProgressDialog, wxDialog)
81 EVT_BUTTON(wxID_CANCEL, wxProgressDialog::OnCancel)
82 EVT_BUTTON(wxID_SKIP, wxProgressDialog::OnSkip)
83
84 EVT_CLOSE(wxProgressDialog::OnClose)
85 END_EVENT_TABLE()
86
87 IMPLEMENT_CLASS(wxProgressDialog, wxDialog)
88
89 // ============================================================================
90 // wxProgressDialog implementation
91 // ============================================================================
92
93 // ----------------------------------------------------------------------------
94 // wxProgressDialog creation
95 // ----------------------------------------------------------------------------
96
97 wxProgressDialog::wxProgressDialog(const wxString& title,
98 const wxString& message,
99 int maximum,
100 wxWindow *parent,
101 int style)
102 : wxDialog(GetParentForModalDialog(parent, style), wxID_ANY, title),
103 m_skip(false),
104 m_delay(3),
105 m_hasAbortButton(false),
106 m_hasSkipButton(false)
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 m_hasAbortButton = (style & wxPD_CAN_ABORT) != 0;
113 m_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 ( !m_hasAbortButton )
121 {
122 EnableCloseButton(false);
123 }
124 #endif // wxMSW
125
126 #if defined(__SMARTPHONE__)
127 SetLeftMenu();
128 #endif
129
130 m_state = m_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 // top-level sizerTop
143 wxSizer * const sizerTop = new wxBoxSizer(wxVERTICAL);
144
145 m_msg = new wxStaticText(this, wxID_ANY, message);
146 sizerTop->Add(m_msg, 0, wxLEFT | wxTOP, 2*LAYOUT_MARGIN);
147
148 if ( maximum > 0 )
149 {
150 int gauge_style = wxGA_HORIZONTAL;
151 if ( style & wxPD_SMOOTH )
152 gauge_style |= wxGA_SMOOTH;
153 m_gauge = new wxGauge
154 (
155 this,
156 wxID_ANY,
157 m_maximum,
158 wxDefaultPosition,
159 // make the progress bar sufficiently long
160 wxSize(wxMin(wxGetClientDisplayRect().width/3, 300), -1),
161 gauge_style
162 );
163
164 sizerTop->Add(m_gauge, 0, wxLEFT | wxRIGHT | wxTOP | wxEXPAND, 2*LAYOUT_MARGIN);
165 m_gauge->SetValue(0);
166 }
167 else
168 {
169 m_gauge = NULL;
170 }
171
172 // create the estimated/remaining/total time zones if requested
173 m_elapsed =
174 m_estimated =
175 m_remaining = NULL;
176 m_display_estimated =
177 m_last_timeupdate =
178 m_break = 0;
179 m_ctdelay = 0;
180
181 // also count how many labels we really have
182 size_t nTimeLabels = 0;
183
184 wxSizer * const sizerLabels = new wxFlexGridSizer(2);
185
186 if ( style & wxPD_ELAPSED_TIME )
187 {
188 nTimeLabels++;
189
190 m_elapsed = CreateLabel(_("Elapsed time:"), sizerLabels);
191 }
192
193 if ( style & wxPD_ESTIMATED_TIME )
194 {
195 nTimeLabels++;
196
197 m_estimated = CreateLabel(_("Estimated time:"), sizerLabels);
198 }
199
200 if ( style & wxPD_REMAINING_TIME )
201 {
202 nTimeLabels++;
203
204 m_remaining = CreateLabel(_("Remaining time:"), sizerLabels);
205 }
206 sizerTop->Add(sizerLabels, 0, wxALIGN_CENTER_HORIZONTAL | wxTOP, LAYOUT_MARGIN);
207
208 if ( nTimeLabels > 0 )
209 {
210 // set it to the current time
211 m_timeStart = wxGetCurrentTime();
212 }
213
214 #if defined(__SMARTPHONE__)
215 if ( m_hasSkipButton )
216 SetRightMenu(wxID_SKIP, _("Skip"));
217 if ( m_hasAbortButton )
218 SetLeftMenu(wxID_CANCEL);
219 #else
220 m_btnAbort =
221 m_btnSkip = NULL;
222
223 wxBoxSizer *buttonSizer = new wxBoxSizer(wxHORIZONTAL);
224
225 // Windows dialogs usually have buttons in the lower right corner
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 ( m_hasSkipButton )
235 {
236 m_btnSkip = new wxButton(this, wxID_SKIP, _("&Skip"));
237
238 buttonSizer->Add(m_btnSkip, 0, sizerFlags, LAYOUT_MARGIN);
239 }
240
241 if ( m_hasAbortButton )
242 {
243 m_btnAbort = new wxButton(this, wxID_CANCEL);
244
245 buttonSizer->Add(m_btnAbort, 0, sizerFlags, LAYOUT_MARGIN);
246 }
247
248 sizerTop->Add(buttonSizer, 0, sizerFlags, LAYOUT_MARGIN );
249 #endif // __SMARTPHONE__/!__SMARTPHONE__
250
251 SetSizerAndFit(sizerTop);
252
253 Centre(wxCENTER_FRAME | wxBOTH);
254
255 if ( style & wxPD_APP_MODAL )
256 {
257 m_winDisabler = new wxWindowDisabler(this);
258 }
259 else
260 {
261 if ( m_parentTop )
262 m_parentTop->Disable();
263 m_winDisabler = NULL;
264 }
265
266 Show();
267 Enable();
268
269 // this one can be initialized even if the others are unknown for now
270 //
271 // NB: do it after calling Layout() to keep the labels correctly aligned
272 if ( m_elapsed )
273 {
274 SetTimeLabel(0, m_elapsed);
275 }
276
277 Update();
278 }
279
280 wxStaticText *
281 wxProgressDialog::CreateLabel(const wxString& text, wxSizer *sizer)
282 {
283 wxStaticText *label = new wxStaticText(this, wxID_ANY, text);
284 wxStaticText *value = new wxStaticText(this, wxID_ANY, _("unknown"));
285
286 // select placement most native or nice on target GUI
287 #if defined(__SMARTPHONE__)
288 // value and time to the left in two rows
289 sizer->Add(label, 1, wxALIGN_LEFT);
290 sizer->Add(value, 1, wxALIGN_LEFT);
291 #elif defined(__WXMSW__) || defined(__WXPM__) || defined(__WXMAC__) || defined(__WXGTK20__)
292 // value and time centered in one row
293 sizer->Add(label, 1, wxLARGESMALL(wxALIGN_RIGHT,wxALIGN_LEFT) | wxTOP | wxRIGHT, LAYOUT_MARGIN);
294 sizer->Add(value, 1, wxALIGN_LEFT | wxTOP, LAYOUT_MARGIN);
295 #else
296 // value and time to the right in one row
297 sizer->Add(label);
298 sizer->Add(value, 0, wxLEFT, LAYOUT_MARGIN);
299 #endif
300
301 return value;
302 }
303
304 // ----------------------------------------------------------------------------
305 // wxProgressDialog operations
306 // ----------------------------------------------------------------------------
307
308 bool
309 wxProgressDialog::Update(int value, const wxString& newmsg, bool *skip)
310 {
311 if ( !DoBeforeUpdate(skip) )
312 return false;
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( !HasFlag(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 wxCHECK_MSG(wxEventLoopBase::GetActive(), false,
405 "wxProgressDialog::Update needs a running event loop");
406
407 // allow the window to repaint:
408 // NOTE: since we yield only for UI events with this call, there
409 // should be no side-effects
410 wxEventLoopBase::GetActive()->YieldFor(wxEVT_CATEGORY_UI);
411
412 // NOTE: this call results in a new event loop being created
413 // and to a call to ProcessPendingEvents() (which may generate
414 // unwanted re-entrancies).
415 (void)ShowModal();
416 }
417 else // auto hide
418 {
419 // reenable other windows before hiding this one because otherwise
420 // Windows wouldn't give the focus back to the window which had
421 // been previously focused because it would still be disabled
422 ReenableOtherWindows();
423
424 Hide();
425 }
426 }
427 else // not at maximum yet
428 {
429 DoAfterUpdate();
430 }
431
432 // update the display in case yielding above didn't do it
433 Update();
434
435 return m_state != Canceled;
436 }
437
438 bool wxProgressDialog::Pulse(const wxString& newmsg, bool *skip)
439 {
440 if ( !DoBeforeUpdate(skip) )
441 return false;
442
443 wxASSERT_MSG( m_gauge, wxT("cannot update non existent dialog") );
444
445 // show a bit of progress
446 m_gauge->Pulse();
447
448 UpdateMessage(newmsg);
449
450 if (m_elapsed || m_remaining || m_estimated)
451 {
452 unsigned long elapsed = wxGetCurrentTime() - m_timeStart;
453
454 SetTimeLabel(elapsed, m_elapsed);
455 SetTimeLabel((unsigned long)-1, m_estimated);
456 SetTimeLabel((unsigned long)-1, m_remaining);
457 }
458
459 DoAfterUpdate();
460
461 return m_state != Canceled;
462 }
463
464 bool wxProgressDialog::DoBeforeUpdate(bool *skip)
465 {
466 wxCHECK_MSG(wxEventLoopBase::GetActive(), false,
467 "wxProgressDialog::DoBeforeUpdate needs a running event loop");
468
469 // we have to yield because not only we want to update the display but
470 // also to process the clicks on the cancel and skip buttons
471 // NOTE: using YieldFor() this call shouldn't give re-entrancy problems
472 // for event handlers not interested to UI/user-input events.
473 wxEventLoopBase::GetActive()->YieldFor(wxEVT_CATEGORY_UI|wxEVT_CATEGORY_USER_INPUT);
474
475 Update();
476
477 if ( m_skip && skip && !*skip )
478 {
479 *skip = true;
480 m_skip = false;
481 EnableSkip();
482 }
483
484 return m_state != Canceled;
485 }
486
487 void wxProgressDialog::DoAfterUpdate()
488 {
489 wxCHECK_RET(wxEventLoopBase::GetActive(),
490 "wxProgressDialog::DoAfterUpdate needs a running event loop");
491
492 // allow the window to repaint:
493 // NOTE: since we yield only for UI events with this call, there
494 // should be no side-effects
495 wxEventLoopBase::GetActive()->YieldFor(wxEVT_CATEGORY_UI);
496 }
497
498 void wxProgressDialog::Resume()
499 {
500 m_state = Continue;
501 m_ctdelay = m_delay; // force an update of the elapsed/estimated/remaining time
502 m_break += wxGetCurrentTime()-m_timeStop;
503
504 EnableAbort();
505 EnableSkip();
506 m_skip = false;
507 }
508
509 bool wxProgressDialog::Show( bool show )
510 {
511 // reenable other windows before hiding this one because otherwise
512 // Windows wouldn't give the focus back to the window which had
513 // been previously focused because it would still be disabled
514 if(!show)
515 ReenableOtherWindows();
516
517 return wxDialog::Show(show);
518 }
519
520 int wxProgressDialog::GetValue() const
521 {
522 if (m_gauge)
523 return m_gauge->GetValue();
524 return wxNOT_FOUND;
525 }
526
527 int wxProgressDialog::GetRange() const
528 {
529 if (m_gauge)
530 return m_gauge->GetRange();
531 return wxNOT_FOUND;
532 }
533
534 wxString wxProgressDialog::GetMessage() const
535 {
536 return m_msg->GetLabel();
537 }
538
539 void wxProgressDialog::SetRange(int maximum)
540 {
541 wxASSERT_MSG(m_gauge, "The dialog should have been constructed with a range > 0");
542 wxASSERT_MSG(maximum > 0, "Invalid range");
543
544 m_gauge->SetRange(maximum);
545 m_maximum = maximum;
546
547 #if defined(__WXMSW__) || defined(__WXPM__)
548 // we can't have values > 65,536 in the progress control under Windows, so
549 // scale everything down
550 m_factor = m_maximum / 65536 + 1;
551 m_maximum /= m_factor;
552 #endif // __WXMSW__
553 }
554
555
556 bool wxProgressDialog::WasCancelled() const
557 {
558 return m_hasAbortButton && m_state == Canceled;
559 }
560
561 bool wxProgressDialog::WasSkipped() const
562 {
563 return m_hasSkipButton && m_skip;
564 }
565
566
567 // ----------------------------------------------------------------------------
568 // event handlers
569 // ----------------------------------------------------------------------------
570
571 void wxProgressDialog::OnCancel(wxCommandEvent& event)
572 {
573 if ( m_state == Finished )
574 {
575 // this means that the count down is already finished and we're being
576 // shown as a modal dialog - so just let the default handler do the job
577 event.Skip();
578 }
579 else
580 {
581 // request to cancel was received, the next time Update() is called we
582 // will handle it
583 m_state = Canceled;
584
585 // update the buttons state immediately so that the user knows that the
586 // request has been noticed
587 DisableAbort();
588 DisableSkip();
589
590 // save the time when the dialog was stopped
591 m_timeStop = wxGetCurrentTime();
592 }
593 }
594
595 void wxProgressDialog::OnSkip(wxCommandEvent& WXUNUSED(event))
596 {
597 DisableSkip();
598 m_skip = true;
599 }
600
601 void wxProgressDialog::OnClose(wxCloseEvent& event)
602 {
603 if ( m_state == Uncancelable )
604 {
605 // can't close this dialog
606 event.Veto();
607 }
608 else if ( m_state == Finished )
609 {
610 // let the default handler close the window as we already terminated
611 event.Skip();
612 }
613 else
614 {
615 // next Update() will notice it
616 m_state = Canceled;
617 DisableAbort();
618 DisableSkip();
619
620 m_timeStop = wxGetCurrentTime();
621 }
622 }
623
624 // ----------------------------------------------------------------------------
625 // destruction
626 // ----------------------------------------------------------------------------
627
628 wxProgressDialog::~wxProgressDialog()
629 {
630 // normally this should have been already done, but just in case
631 ReenableOtherWindows();
632 }
633
634 void wxProgressDialog::ReenableOtherWindows()
635 {
636 if ( HasFlag(wxPD_APP_MODAL) )
637 {
638 wxDELETE(m_winDisabler);
639 }
640 else
641 {
642 if ( m_parentTop )
643 m_parentTop->Enable();
644 }
645 }
646
647 // ----------------------------------------------------------------------------
648 // private functions
649 // ----------------------------------------------------------------------------
650
651 static void SetTimeLabel(unsigned long val, wxStaticText *label)
652 {
653 if ( label )
654 {
655 wxString s;
656
657 if (val != (unsigned long)-1)
658 {
659 unsigned long hours = val / 3600;
660 unsigned long minutes = (val % 3600) / 60;
661 unsigned long seconds = val % 60;
662 s.Printf(wxT("%lu:%02lu:%02lu"), hours, minutes, seconds);
663 }
664 else
665 {
666 s = _("Unknown");
667 }
668
669 if ( s != label->GetLabel() )
670 label->SetLabel(s);
671 }
672 }
673
674 void wxProgressDialog::EnableSkip(bool enable)
675 {
676 if(m_hasSkipButton)
677 {
678 #ifdef __SMARTPHONE__
679 if(enable)
680 SetRightMenu(wxID_SKIP, _("Skip"));
681 else
682 SetRightMenu();
683 #else
684 if(m_btnSkip)
685 m_btnSkip->Enable(enable);
686 #endif
687 }
688 }
689
690 void wxProgressDialog::EnableAbort(bool enable)
691 {
692 if(m_hasAbortButton)
693 {
694 #ifdef __SMARTPHONE__
695 if(enable)
696 SetLeftMenu(wxID_CANCEL); // stock buttons makes Cancel label
697 else
698 SetLeftMenu();
699 #else
700 if(m_btnAbort)
701 m_btnAbort->Enable(enable);
702 #endif
703 }
704 }
705
706 void wxProgressDialog::EnableClose()
707 {
708 if(m_hasAbortButton)
709 {
710 #ifdef __SMARTPHONE__
711 SetLeftMenu(wxID_CANCEL, _("Close"));
712 #else
713 if(m_btnAbort)
714 {
715 m_btnAbort->Enable();
716 m_btnAbort->SetLabel(_("Close"));
717 }
718 #endif
719 }
720 }
721
722 void wxProgressDialog::UpdateMessage(const wxString &newmsg)
723 {
724 wxCHECK_RET(wxEventLoopBase::GetActive(),
725 "wxProgressDialog::UpdateMessage needs a running event loop");
726
727 if ( !newmsg.empty() && newmsg != m_msg->GetLabel() )
728 {
729 m_msg->SetLabel(newmsg);
730
731 // allow the window to repaint:
732 // NOTE: since we yield only for UI events with this call, there
733 // should be no side-effects
734 wxEventLoopBase::GetActive()->YieldFor(wxEVT_CATEGORY_UI);
735 }
736 }
737
738 #endif // wxUSE_PROGRESSDLG