1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/progdlgg.cpp
3 // Purpose: wxGenericProgressDialog class
4 // Author: Karsten Ballueder
8 // Copyright: (c) Karsten Ballueder
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
32 #include "wx/button.h"
33 #include "wx/stattext.h"
38 #include "wx/dcclient.h"
40 #include "wx/settings.h"
44 #include "wx/progdlg.h"
45 #include "wx/evtloop.h"
47 // ---------------------------------------------------------------------------
49 // ---------------------------------------------------------------------------
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
55 #if defined(__SMARTPHONE__)
56 #define wxLARGESMALL(large,small) small
58 #define wxLARGESMALL(large,small) large
61 // ----------------------------------------------------------------------------
63 // ----------------------------------------------------------------------------
65 #define LAYOUT_MARGIN wxLARGESMALL(8,2)
67 static const int wxID_SKIP
= 32000; // whatever
69 // ----------------------------------------------------------------------------
71 // ----------------------------------------------------------------------------
73 BEGIN_EVENT_TABLE(wxGenericProgressDialog
, wxDialog
)
74 EVT_BUTTON(wxID_CANCEL
, wxGenericProgressDialog::OnCancel
)
75 EVT_BUTTON(wxID_SKIP
, wxGenericProgressDialog::OnSkip
)
77 EVT_CLOSE(wxGenericProgressDialog::OnClose
)
80 // ============================================================================
81 // wxGenericProgressDialog implementation
82 // ============================================================================
84 wxIMPLEMENT_CLASS(wxProgressDialog
, wxDialog
)
86 // ----------------------------------------------------------------------------
87 // wxGenericProgressDialog creation
88 // ----------------------------------------------------------------------------
90 void wxGenericProgressDialog::Init(wxWindow
*parent
, int style
)
92 // we may disappear at any moment, let the others know about it
93 SetExtraStyle(GetExtraStyle() | wxWS_EX_TRANSIENT
);
95 // Initialize all our members that we always use (even when we don't
96 // create a valid window in this class).
100 m_parentTop
= wxGetTopLevelParent(parent
);
108 m_state
= Uncancelable
;
111 m_timeStart
= wxGetCurrentTime();
112 m_timeStop
= (unsigned long)-1;
117 #if !defined(__SMARTPHONE__)
122 m_display_estimated
=
128 m_winDisabler
= NULL
;
129 m_tempEventLoop
= NULL
;
132 wxGenericProgressDialog::wxGenericProgressDialog(wxWindow
*parent
, int style
)
138 wxGenericProgressDialog::wxGenericProgressDialog(const wxString
& title
,
139 const wxString
& message
,
147 Create( title
, message
, maximum
, parent
, style
);
150 void wxGenericProgressDialog::Create( const wxString
& title
,
151 const wxString
& message
,
156 // Notice that GetParentForModalDialog() needs the dialog window style, not
157 // our wxProgressDialog-specific style.
159 realParent
= GetParentForModalDialog(parent
, GetWindowStyle());
160 wxDialog::Create(realParent
, wxID_ANY
, title
);
166 // We need a running event loop in order to update the dialog and be able
167 // to process clicks on its buttons, so ensure that there is one running
168 // even if this means we have to start it ourselves (this happens most
169 // commonly during the program initialization, e.g. for the progress
170 // dialogs shown from overridden wxApp::OnInit()).
171 if ( !wxEventLoopBase::GetActive() )
173 m_tempEventLoop
= new wxEventLoop
;
174 wxEventLoop::SetActive(m_tempEventLoop
);
177 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
178 // we have to remove the "Close" button from the title bar then as it is
179 // confusing to have it - it doesn't work anyhow
181 // FIXME: should probably have a (extended?) window style for this
182 if ( !HasPDFlag(wxPD_CAN_ABORT
) )
184 EnableCloseButton(false);
188 #if defined(__SMARTPHONE__)
192 m_state
= HasPDFlag(wxPD_CAN_ABORT
) ? Continue
: Uncancelable
;
194 // top-level sizerTop
195 wxSizer
* const sizerTop
= new wxBoxSizer(wxVERTICAL
);
197 m_msg
= new wxStaticText(this, wxID_ANY
, message
);
198 sizerTop
->Add(m_msg
, 0, wxLEFT
| wxTOP
, 2*LAYOUT_MARGIN
);
200 int gauge_style
= wxGA_HORIZONTAL
;
201 if ( style
& wxPD_SMOOTH
)
202 gauge_style
|= wxGA_SMOOTH
;
208 m_gauge
= new wxGauge
214 // make the progress bar sufficiently long
215 wxSize(wxMin(wxGetClientDisplayRect().width
/3, 300), -1),
219 sizerTop
->Add(m_gauge
, 0, wxLEFT
| wxRIGHT
| wxTOP
| wxEXPAND
, 2*LAYOUT_MARGIN
);
220 m_gauge
->SetValue(0);
222 // create the estimated/remaining/total time zones if requested
227 // also count how many labels we really have
228 size_t nTimeLabels
= 0;
230 wxSizer
* const sizerLabels
= new wxFlexGridSizer(2);
232 if ( style
& wxPD_ELAPSED_TIME
)
236 m_elapsed
= CreateLabel(GetElapsedLabel(), sizerLabels
);
239 if ( style
& wxPD_ESTIMATED_TIME
)
243 m_estimated
= CreateLabel(GetEstimatedLabel(), sizerLabels
);
246 if ( style
& wxPD_REMAINING_TIME
)
250 m_remaining
= CreateLabel(GetRemainingLabel(), sizerLabels
);
252 sizerTop
->Add(sizerLabels
, 0, wxALIGN_CENTER_HORIZONTAL
| wxTOP
, LAYOUT_MARGIN
);
254 #if defined(__SMARTPHONE__)
255 if ( HasPDFlag(wxPD_CAN_SKIP
) )
256 SetRightMenu(wxID_SKIP
, _("Skip"));
257 if ( HasPDFlag(wxPD_CAN_ABORT
) )
258 SetLeftMenu(wxID_CANCEL
);
263 wxBoxSizer
*buttonSizer
= new wxBoxSizer(wxHORIZONTAL
);
265 // Windows dialogs usually have buttons in the lower right corner
266 const int sizerFlags
=
267 #if defined(__WXMSW__) || defined(__WXPM__) || defined(__WXOSX__)
268 wxALIGN_RIGHT
| wxALL
270 wxALIGN_CENTER_HORIZONTAL
| wxBOTTOM
| wxTOP
274 if ( HasPDFlag(wxPD_CAN_SKIP
) )
276 m_btnSkip
= new wxButton(this, wxID_SKIP
, _("&Skip"));
278 buttonSizer
->Add(m_btnSkip
, 0, sizerFlags
, LAYOUT_MARGIN
);
281 if ( HasPDFlag(wxPD_CAN_ABORT
) )
283 m_btnAbort
= new wxButton(this, wxID_CANCEL
);
285 buttonSizer
->Add(m_btnAbort
, 0, sizerFlags
, LAYOUT_MARGIN
);
288 if ( !HasPDFlag(wxPD_CAN_SKIP
| wxPD_CAN_ABORT
) )
289 buttonSizer
->AddSpacer(LAYOUT_MARGIN
);
291 sizerTop
->Add(buttonSizer
, 0, sizerFlags
, LAYOUT_MARGIN
);
292 #endif // __SMARTPHONE__/!__SMARTPHONE__
294 SetSizerAndFit(sizerTop
);
296 Centre(wxCENTER_FRAME
| wxBOTH
);
298 DisableOtherWindows();
303 // this one can be initialized even if the others are unknown for now
305 // NB: do it after calling Layout() to keep the labels correctly aligned
308 SetTimeLabel(0, m_elapsed
);
314 void wxGenericProgressDialog::UpdateTimeEstimates(int value
,
315 unsigned long &elapsedTime
,
316 unsigned long &estimatedTime
,
317 unsigned long &remainingTime
)
319 unsigned long elapsed
= wxGetCurrentTime() - m_timeStart
;
320 if ( value
!= 0 && (m_last_timeupdate
< elapsed
|| value
== m_maximum
) )
322 m_last_timeupdate
= elapsed
;
323 unsigned long estimated
= m_break
+
324 (unsigned long)(( (double) (elapsed
-m_break
) * m_maximum
) / ((double)value
)) ;
325 if ( estimated
> m_display_estimated
331 else if ( estimated
< m_display_estimated
341 if ( m_ctdelay
>= m_delay
// enough confirmations for a higher value
342 || m_ctdelay
<= (m_delay
*-1) // enough confirmations for a lower value
343 || value
== m_maximum
// to stay consistent
344 || elapsed
> m_display_estimated
// to stay consistent
345 || ( elapsed
> 0 && elapsed
< 4 ) // additional updates in the beginning
348 m_display_estimated
= estimated
;
355 long display_remaining
= m_display_estimated
- elapsed
;
356 if ( display_remaining
< 0 )
358 display_remaining
= 0;
361 estimatedTime
= m_display_estimated
;
362 remainingTime
= display_remaining
;
365 elapsedTime
= elapsed
;
369 wxString
wxGenericProgressDialog::GetFormattedTime(unsigned long timeInSec
)
373 if ( timeInSec
== (unsigned long)-1 )
375 timeAsHMS
= _("Unknown");
379 unsigned hours
= timeInSec
/ 3600;
380 unsigned minutes
= (timeInSec
% 3600) / 60;
381 unsigned seconds
= timeInSec
% 60;
382 timeAsHMS
.Printf("%u:%02u:%02u", hours
, minutes
, seconds
);
389 wxGenericProgressDialog::CreateLabel(const wxString
& text
, wxSizer
*sizer
)
391 wxStaticText
*label
= new wxStaticText(this, wxID_ANY
, text
);
392 wxStaticText
*value
= new wxStaticText(this, wxID_ANY
, _("unknown"));
394 // select placement most native or nice on target GUI
395 #if defined(__SMARTPHONE__)
396 // value and time to the left in two rows
397 sizer
->Add(label
, 1, wxALIGN_LEFT
);
398 sizer
->Add(value
, 1, wxALIGN_LEFT
);
399 #elif defined(__WXMSW__) || defined(__WXPM__) || defined(__WXMAC__) || defined(__WXGTK20__)
400 // value and time centered in one row
401 sizer
->Add(label
, 1, wxLARGESMALL(wxALIGN_RIGHT
,wxALIGN_LEFT
) | wxTOP
| wxRIGHT
, LAYOUT_MARGIN
);
402 sizer
->Add(value
, 1, wxALIGN_LEFT
| wxTOP
, LAYOUT_MARGIN
);
404 // value and time to the right in one row
406 sizer
->Add(value
, 0, wxLEFT
, LAYOUT_MARGIN
);
412 // ----------------------------------------------------------------------------
413 // wxGenericProgressDialog operations
414 // ----------------------------------------------------------------------------
417 wxGenericProgressDialog::Update(int value
, const wxString
& newmsg
, bool *skip
)
419 if ( !DoBeforeUpdate(skip
) )
422 wxCHECK_MSG( m_gauge
, false, "dialog should be fully created" );
428 wxASSERT_MSG( value
<= m_maximum
, wxT("invalid progress value") );
430 m_gauge
->SetValue(value
);
432 UpdateMessage(newmsg
);
434 if ( (m_elapsed
|| m_remaining
|| m_estimated
) && (value
!= 0) )
436 unsigned long elapsed
;
437 unsigned long display_remaining
;
439 UpdateTimeEstimates( value
,
444 SetTimeLabel(elapsed
, m_elapsed
);
445 SetTimeLabel(m_display_estimated
, m_estimated
);
446 SetTimeLabel(display_remaining
, m_remaining
);
449 if ( value
== m_maximum
)
451 if ( m_state
== Finished
)
453 // ignore multiple calls to Update(m_maximum): it may sometimes be
454 // troublesome to ensure that Update() is not called twice with the
455 // same value (e.g. because of the rounding errors) and if we don't
456 // return now we're going to generate asserts below
460 // so that we return true below and that out [Cancel] handler knew what
463 if( !HasPDFlag(wxPD_AUTO_HIDE
) )
467 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
471 if ( newmsg
.empty() )
473 // also provide the finishing message if the application didn't
474 m_msg
->SetLabel(_("Done."));
477 // allow the window to repaint:
478 // NOTE: since we yield only for UI events with this call, there
479 // should be no side-effects
480 wxEventLoopBase::GetActive()->YieldFor(wxEVT_CATEGORY_UI
);
482 // NOTE: this call results in a new event loop being created
483 // and to a call to ProcessPendingEvents() (which may generate
484 // unwanted re-entrancies).
489 // reenable other windows before hiding this one because otherwise
490 // Windows wouldn't give the focus back to the window which had
491 // been previously focused because it would still be disabled
492 ReenableOtherWindows();
497 else // not at maximum yet
502 // update the display in case yielding above didn't do it
505 return m_state
!= Canceled
;
508 bool wxGenericProgressDialog::Pulse(const wxString
& newmsg
, bool *skip
)
510 if ( !DoBeforeUpdate(skip
) )
513 wxCHECK_MSG( m_gauge
, false, "dialog should be fully created" );
515 // show a bit of progress
518 UpdateMessage(newmsg
);
520 if (m_elapsed
|| m_remaining
|| m_estimated
)
522 unsigned long elapsed
= wxGetCurrentTime() - m_timeStart
;
524 SetTimeLabel(elapsed
, m_elapsed
);
525 SetTimeLabel((unsigned long)-1, m_estimated
);
526 SetTimeLabel((unsigned long)-1, m_remaining
);
531 return m_state
!= Canceled
;
534 bool wxGenericProgressDialog::DoBeforeUpdate(bool *skip
)
536 // we have to yield because not only we want to update the display but
537 // also to process the clicks on the cancel and skip buttons
538 // NOTE: using YieldFor() this call shouldn't give re-entrancy problems
539 // for event handlers not interested to UI/user-input events.
540 wxEventLoopBase::GetActive()->YieldFor(wxEVT_CATEGORY_UI
|wxEVT_CATEGORY_USER_INPUT
);
544 if ( m_skip
&& skip
&& !*skip
)
551 return m_state
!= Canceled
;
554 void wxGenericProgressDialog::DoAfterUpdate()
556 // allow the window to repaint:
557 // NOTE: since we yield only for UI events with this call, there
558 // should be no side-effects
559 wxEventLoopBase::GetActive()->YieldFor(wxEVT_CATEGORY_UI
);
562 void wxGenericProgressDialog::Resume()
565 m_ctdelay
= m_delay
; // force an update of the elapsed/estimated/remaining time
566 m_break
+= wxGetCurrentTime()-m_timeStop
;
573 bool wxGenericProgressDialog::Show( bool show
)
575 // reenable other windows before hiding this one because otherwise
576 // Windows wouldn't give the focus back to the window which had
577 // been previously focused because it would still be disabled
579 ReenableOtherWindows();
581 return wxDialog::Show(show
);
584 int wxGenericProgressDialog::GetValue() const
586 wxCHECK_MSG( m_gauge
, -1, "dialog should be fully created" );
588 return m_gauge
->GetValue();
591 int wxGenericProgressDialog::GetRange() const
596 wxString
wxGenericProgressDialog::GetMessage() const
598 return m_msg
->GetLabel();
601 void wxGenericProgressDialog::SetRange(int maximum
)
603 wxCHECK_RET( m_gauge
, "dialog should be fully created" );
605 wxCHECK_RET( maximum
> 0, "Invalid range" );
607 m_gauge
->SetRange(maximum
);
612 void wxGenericProgressDialog::SetMaximum(int maximum
)
616 #if defined(__WXMSW__) || defined(__WXPM__)
617 // we can't have values > 65,536 in the progress control under Windows, so
618 // scale everything down
619 m_factor
= m_maximum
/ 65536 + 1;
624 bool wxGenericProgressDialog::WasCancelled() const
626 return HasPDFlag(wxPD_CAN_ABORT
) && m_state
== Canceled
;
629 bool wxGenericProgressDialog::WasSkipped() const
631 return HasPDFlag(wxPD_CAN_SKIP
) && m_skip
;
635 void wxGenericProgressDialog::SetTimeLabel(unsigned long val
,
642 if (val
!= (unsigned long)-1)
644 s
= GetFormattedTime(val
);
651 if ( s
!= label
->GetLabel() )
656 // ----------------------------------------------------------------------------
658 // ----------------------------------------------------------------------------
660 void wxGenericProgressDialog::OnCancel(wxCommandEvent
& event
)
662 if ( m_state
== Finished
)
664 // this means that the count down is already finished and we're being
665 // shown as a modal dialog - so just let the default handler do the job
670 // request to cancel was received, the next time Update() is called we
674 // update the buttons state immediately so that the user knows that the
675 // request has been noticed
679 // save the time when the dialog was stopped
680 m_timeStop
= wxGetCurrentTime();
684 void wxGenericProgressDialog::OnSkip(wxCommandEvent
& WXUNUSED(event
))
690 void wxGenericProgressDialog::OnClose(wxCloseEvent
& event
)
692 if ( m_state
== Uncancelable
)
694 // can't close this dialog
697 else if ( m_state
== Finished
)
699 // let the default handler close the window as we already terminated
704 // next Update() will notice it
709 m_timeStop
= wxGetCurrentTime();
713 // ----------------------------------------------------------------------------
715 // ----------------------------------------------------------------------------
717 wxGenericProgressDialog::~wxGenericProgressDialog()
719 // normally this should have been already done, but just in case
720 ReenableOtherWindows();
722 if ( m_tempEventLoop
)
724 wxEventLoopBase::SetActive(NULL
);
725 delete m_tempEventLoop
;
729 void wxGenericProgressDialog::DisableOtherWindows()
731 if ( HasPDFlag(wxPD_APP_MODAL
) )
733 m_winDisabler
= new wxWindowDisabler(this);
738 m_parentTop
->Disable();
739 m_winDisabler
= NULL
;
743 void wxGenericProgressDialog::ReenableOtherWindows()
745 if ( HasPDFlag(wxPD_APP_MODAL
) )
747 wxDELETE(m_winDisabler
);
752 m_parentTop
->Enable();
756 // ----------------------------------------------------------------------------
758 // ----------------------------------------------------------------------------
760 void wxGenericProgressDialog::EnableSkip(bool enable
)
762 if ( HasPDFlag(wxPD_CAN_SKIP
) )
764 #ifdef __SMARTPHONE__
766 SetRightMenu(wxID_SKIP
, _("Skip"));
771 m_btnSkip
->Enable(enable
);
776 void wxGenericProgressDialog::EnableAbort(bool enable
)
778 if( HasPDFlag(wxPD_CAN_ABORT
) )
780 #ifdef __SMARTPHONE__
782 SetLeftMenu(wxID_CANCEL
); // stock buttons makes Cancel label
787 m_btnAbort
->Enable(enable
);
792 void wxGenericProgressDialog::EnableClose()
794 if(HasPDFlag(wxPD_CAN_ABORT
))
796 #ifdef __SMARTPHONE__
797 SetLeftMenu(wxID_CANCEL
, _("Close"));
801 m_btnAbort
->Enable();
802 m_btnAbort
->SetLabel(_("Close"));
808 void wxGenericProgressDialog::UpdateMessage(const wxString
&newmsg
)
810 if ( !newmsg
.empty() && newmsg
!= m_msg
->GetLabel() )
812 m_msg
->SetLabel(newmsg
);
814 // allow the window to repaint:
815 // NOTE: since we yield only for UI events with this call, there
816 // should be no side-effects
817 wxEventLoopBase::GetActive()->YieldFor(wxEVT_CATEGORY_UI
);
821 #endif // wxUSE_PROGRESSDLG