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()
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).
107 m_state
= Uncancelable
;
110 m_timeStart
= wxGetCurrentTime();
111 m_timeStop
= (unsigned long)-1;
116 #if !defined(__SMARTPHONE__)
121 m_display_estimated
=
127 m_winDisabler
= NULL
;
128 m_tempEventLoop
= NULL
;
131 wxGenericProgressDialog::wxGenericProgressDialog()
137 wxGenericProgressDialog::wxGenericProgressDialog(const wxString
& title
,
138 const wxString
& message
,
146 Create( title
, message
, maximum
, parent
, style
);
149 void wxGenericProgressDialog::SetTopParent(wxWindow
* parent
)
151 m_parentTop
= GetParentForModalDialog(parent
, GetWindowStyle());
154 bool wxGenericProgressDialog::Create( const wxString
& title
,
155 const wxString
& message
,
160 SetTopParent(parent
);
162 m_parentTop
= wxGetTopLevelParent(parent
);
166 realParent
= GetParentForModalDialog(parent
, GetWindowStyle());
168 if (!wxDialog::Create(realParent
, wxID_ANY
, title
))
173 // We need a running event loop in order to update the dialog and be able
174 // to process clicks on its buttons, so ensure that there is one running
175 // even if this means we have to start it ourselves (this happens most
176 // commonly during the program initialization, e.g. for the progress
177 // dialogs shown from overridden wxApp::OnInit()).
178 if ( !wxEventLoopBase::GetActive() )
180 m_tempEventLoop
= new wxEventLoop
;
181 wxEventLoop::SetActive(m_tempEventLoop
);
184 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
185 // we have to remove the "Close" button from the title bar then as it is
186 // confusing to have it - it doesn't work anyhow
188 // FIXME: should probably have a (extended?) window style for this
189 if ( !HasPDFlag(wxPD_CAN_ABORT
) )
191 EnableCloseButton(false);
195 #if defined(__SMARTPHONE__)
199 m_state
= HasPDFlag(wxPD_CAN_ABORT
) ? Continue
: Uncancelable
;
201 // top-level sizerTop
202 wxSizer
* const sizerTop
= new wxBoxSizer(wxVERTICAL
);
204 m_msg
= new wxStaticText(this, wxID_ANY
, message
);
205 sizerTop
->Add(m_msg
, 0, wxLEFT
| wxTOP
, 2*LAYOUT_MARGIN
);
207 int gauge_style
= wxGA_HORIZONTAL
;
208 if ( style
& wxPD_SMOOTH
)
209 gauge_style
|= wxGA_SMOOTH
;
215 m_gauge
= new wxGauge
221 // make the progress bar sufficiently long
222 wxSize(wxMin(wxGetClientDisplayRect().width
/3, 300), -1),
226 sizerTop
->Add(m_gauge
, 0, wxLEFT
| wxRIGHT
| wxTOP
| wxEXPAND
, 2*LAYOUT_MARGIN
);
227 m_gauge
->SetValue(0);
229 // create the estimated/remaining/total time zones if requested
234 // also count how many labels we really have
235 size_t nTimeLabels
= 0;
237 wxSizer
* const sizerLabels
= new wxFlexGridSizer(2);
239 if ( style
& wxPD_ELAPSED_TIME
)
243 m_elapsed
= CreateLabel(GetElapsedLabel(), sizerLabels
);
246 if ( style
& wxPD_ESTIMATED_TIME
)
250 m_estimated
= CreateLabel(GetEstimatedLabel(), sizerLabels
);
253 if ( style
& wxPD_REMAINING_TIME
)
257 m_remaining
= CreateLabel(GetRemainingLabel(), sizerLabels
);
259 sizerTop
->Add(sizerLabels
, 0, wxALIGN_CENTER_HORIZONTAL
| wxTOP
, LAYOUT_MARGIN
);
261 #if defined(__SMARTPHONE__)
262 if ( HasPDFlag(wxPD_CAN_SKIP
) )
263 SetRightMenu(wxID_SKIP
, _("Skip"));
264 if ( HasPDFlag(wxPD_CAN_ABORT
) )
265 SetLeftMenu(wxID_CANCEL
);
270 wxBoxSizer
*buttonSizer
= new wxBoxSizer(wxHORIZONTAL
);
272 // Windows dialogs usually have buttons in the lower right corner
273 const int sizerFlags
=
274 #if defined(__WXMSW__) || defined(__WXPM__) || defined(__WXOSX__)
275 wxALIGN_RIGHT
| wxALL
277 wxALIGN_CENTER_HORIZONTAL
| wxBOTTOM
| wxTOP
281 if ( HasPDFlag(wxPD_CAN_SKIP
) )
283 m_btnSkip
= new wxButton(this, wxID_SKIP
, _("&Skip"));
285 buttonSizer
->Add(m_btnSkip
, 0, sizerFlags
, LAYOUT_MARGIN
);
288 if ( HasPDFlag(wxPD_CAN_ABORT
) )
290 m_btnAbort
= new wxButton(this, wxID_CANCEL
);
292 buttonSizer
->Add(m_btnAbort
, 0, sizerFlags
, LAYOUT_MARGIN
);
295 if ( !HasPDFlag(wxPD_CAN_SKIP
| wxPD_CAN_ABORT
) )
296 buttonSizer
->AddSpacer(LAYOUT_MARGIN
);
298 sizerTop
->Add(buttonSizer
, 0, sizerFlags
, LAYOUT_MARGIN
);
299 #endif // __SMARTPHONE__/!__SMARTPHONE__
301 SetSizerAndFit(sizerTop
);
303 Centre(wxCENTER_FRAME
| wxBOTH
);
305 DisableOtherWindows();
310 // this one can be initialized even if the others are unknown for now
312 // NB: do it after calling Layout() to keep the labels correctly aligned
315 SetTimeLabel(0, m_elapsed
);
322 void wxGenericProgressDialog::UpdateTimeEstimates(int value
,
323 unsigned long &elapsedTime
,
324 unsigned long &estimatedTime
,
325 unsigned long &remainingTime
)
327 unsigned long elapsed
= wxGetCurrentTime() - m_timeStart
;
328 if ( value
!= 0 && (m_last_timeupdate
< elapsed
|| value
== m_maximum
) )
330 m_last_timeupdate
= elapsed
;
331 unsigned long estimated
= m_break
+
332 (unsigned long)(( (double) (elapsed
-m_break
) * m_maximum
) / ((double)value
)) ;
333 if ( estimated
> m_display_estimated
339 else if ( estimated
< m_display_estimated
349 if ( m_ctdelay
>= m_delay
// enough confirmations for a higher value
350 || m_ctdelay
<= (m_delay
*-1) // enough confirmations for a lower value
351 || value
== m_maximum
// to stay consistent
352 || elapsed
> m_display_estimated
// to stay consistent
353 || ( elapsed
> 0 && elapsed
< 4 ) // additional updates in the beginning
356 m_display_estimated
= estimated
;
363 long display_remaining
= m_display_estimated
- elapsed
;
364 if ( display_remaining
< 0 )
366 display_remaining
= 0;
369 estimatedTime
= m_display_estimated
;
370 remainingTime
= display_remaining
;
373 elapsedTime
= elapsed
;
377 wxString
wxGenericProgressDialog::GetFormattedTime(unsigned long timeInSec
)
381 if ( timeInSec
== (unsigned long)-1 )
383 timeAsHMS
= _("Unknown");
387 unsigned hours
= timeInSec
/ 3600;
388 unsigned minutes
= (timeInSec
% 3600) / 60;
389 unsigned seconds
= timeInSec
% 60;
390 timeAsHMS
.Printf("%u:%02u:%02u", hours
, minutes
, seconds
);
397 wxGenericProgressDialog::CreateLabel(const wxString
& text
, wxSizer
*sizer
)
399 wxStaticText
*label
= new wxStaticText(this, wxID_ANY
, text
);
400 wxStaticText
*value
= new wxStaticText(this, wxID_ANY
, _("unknown"));
402 // select placement most native or nice on target GUI
403 #if defined(__SMARTPHONE__)
404 // value and time to the left in two rows
405 sizer
->Add(label
, 1, wxALIGN_LEFT
);
406 sizer
->Add(value
, 1, wxALIGN_LEFT
);
407 #elif defined(__WXMSW__) || defined(__WXPM__) || defined(__WXMAC__) || defined(__WXGTK20__)
408 // value and time centered in one row
409 sizer
->Add(label
, 1, wxLARGESMALL(wxALIGN_RIGHT
,wxALIGN_LEFT
) | wxTOP
| wxRIGHT
, LAYOUT_MARGIN
);
410 sizer
->Add(value
, 1, wxALIGN_LEFT
| wxTOP
, LAYOUT_MARGIN
);
412 // value and time to the right in one row
414 sizer
->Add(value
, 0, wxLEFT
, LAYOUT_MARGIN
);
420 // ----------------------------------------------------------------------------
421 // wxGenericProgressDialog operations
422 // ----------------------------------------------------------------------------
425 wxGenericProgressDialog::Update(int value
, const wxString
& newmsg
, bool *skip
)
427 if ( !DoBeforeUpdate(skip
) )
430 wxCHECK_MSG( m_gauge
, false, "dialog should be fully created" );
436 wxASSERT_MSG( value
<= m_maximum
, wxT("invalid progress value") );
438 m_gauge
->SetValue(value
);
440 UpdateMessage(newmsg
);
442 if ( (m_elapsed
|| m_remaining
|| m_estimated
) && (value
!= 0) )
444 unsigned long elapsed
;
445 unsigned long display_remaining
;
447 UpdateTimeEstimates( value
,
452 SetTimeLabel(elapsed
, m_elapsed
);
453 SetTimeLabel(m_display_estimated
, m_estimated
);
454 SetTimeLabel(display_remaining
, m_remaining
);
457 if ( value
== m_maximum
)
459 if ( m_state
== Finished
)
461 // ignore multiple calls to Update(m_maximum): it may sometimes be
462 // troublesome to ensure that Update() is not called twice with the
463 // same value (e.g. because of the rounding errors) and if we don't
464 // return now we're going to generate asserts below
468 // so that we return true below and that out [Cancel] handler knew what
471 if( !HasPDFlag(wxPD_AUTO_HIDE
) )
475 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
479 if ( newmsg
.empty() )
481 // also provide the finishing message if the application didn't
482 m_msg
->SetLabel(_("Done."));
485 // allow the window to repaint:
486 // NOTE: since we yield only for UI events with this call, there
487 // should be no side-effects
488 wxEventLoopBase::GetActive()->YieldFor(wxEVT_CATEGORY_UI
);
490 // NOTE: this call results in a new event loop being created
491 // and to a call to ProcessPendingEvents() (which may generate
492 // unwanted re-entrancies).
497 // reenable other windows before hiding this one because otherwise
498 // Windows wouldn't give the focus back to the window which had
499 // been previously focused because it would still be disabled
500 ReenableOtherWindows();
505 else // not at maximum yet
510 // update the display in case yielding above didn't do it
513 return m_state
!= Canceled
;
516 bool wxGenericProgressDialog::Pulse(const wxString
& newmsg
, bool *skip
)
518 if ( !DoBeforeUpdate(skip
) )
521 wxCHECK_MSG( m_gauge
, false, "dialog should be fully created" );
523 // show a bit of progress
526 UpdateMessage(newmsg
);
528 if (m_elapsed
|| m_remaining
|| m_estimated
)
530 unsigned long elapsed
= wxGetCurrentTime() - m_timeStart
;
532 SetTimeLabel(elapsed
, m_elapsed
);
533 SetTimeLabel((unsigned long)-1, m_estimated
);
534 SetTimeLabel((unsigned long)-1, m_remaining
);
539 return m_state
!= Canceled
;
542 bool wxGenericProgressDialog::DoBeforeUpdate(bool *skip
)
544 // we have to yield because not only we want to update the display but
545 // also to process the clicks on the cancel and skip buttons
546 // NOTE: using YieldFor() this call shouldn't give re-entrancy problems
547 // for event handlers not interested to UI/user-input events.
548 wxEventLoopBase::GetActive()->YieldFor(wxEVT_CATEGORY_UI
|wxEVT_CATEGORY_USER_INPUT
);
552 if ( m_skip
&& skip
&& !*skip
)
559 return m_state
!= Canceled
;
562 void wxGenericProgressDialog::DoAfterUpdate()
564 // allow the window to repaint:
565 // NOTE: since we yield only for UI events with this call, there
566 // should be no side-effects
567 wxEventLoopBase::GetActive()->YieldFor(wxEVT_CATEGORY_UI
);
570 void wxGenericProgressDialog::Resume()
573 m_ctdelay
= m_delay
; // force an update of the elapsed/estimated/remaining time
574 m_break
+= wxGetCurrentTime()-m_timeStop
;
581 bool wxGenericProgressDialog::Show( bool show
)
583 // reenable other windows before hiding this one because otherwise
584 // Windows wouldn't give the focus back to the window which had
585 // been previously focused because it would still be disabled
587 ReenableOtherWindows();
589 return wxDialog::Show(show
);
592 int wxGenericProgressDialog::GetValue() const
594 wxCHECK_MSG( m_gauge
, -1, "dialog should be fully created" );
596 return m_gauge
->GetValue();
599 int wxGenericProgressDialog::GetRange() const
604 wxString
wxGenericProgressDialog::GetMessage() const
606 return m_msg
->GetLabel();
609 void wxGenericProgressDialog::SetRange(int maximum
)
611 wxCHECK_RET( m_gauge
, "dialog should be fully created" );
613 wxCHECK_RET( maximum
> 0, "Invalid range" );
615 m_gauge
->SetRange(maximum
);
620 void wxGenericProgressDialog::SetMaximum(int maximum
)
624 #if defined(__WXMSW__) || defined(__WXPM__)
625 // we can't have values > 65,536 in the progress control under Windows, so
626 // scale everything down
627 m_factor
= m_maximum
/ 65536 + 1;
632 bool wxGenericProgressDialog::WasCancelled() const
634 return HasPDFlag(wxPD_CAN_ABORT
) && m_state
== Canceled
;
637 bool wxGenericProgressDialog::WasSkipped() const
639 return HasPDFlag(wxPD_CAN_SKIP
) && m_skip
;
643 void wxGenericProgressDialog::SetTimeLabel(unsigned long val
,
650 if (val
!= (unsigned long)-1)
652 s
= GetFormattedTime(val
);
659 if ( s
!= label
->GetLabel() )
664 // ----------------------------------------------------------------------------
666 // ----------------------------------------------------------------------------
668 void wxGenericProgressDialog::OnCancel(wxCommandEvent
& event
)
670 if ( m_state
== Finished
)
672 // this means that the count down is already finished and we're being
673 // shown as a modal dialog - so just let the default handler do the job
678 // request to cancel was received, the next time Update() is called we
682 // update the buttons state immediately so that the user knows that the
683 // request has been noticed
687 // save the time when the dialog was stopped
688 m_timeStop
= wxGetCurrentTime();
692 void wxGenericProgressDialog::OnSkip(wxCommandEvent
& WXUNUSED(event
))
698 void wxGenericProgressDialog::OnClose(wxCloseEvent
& event
)
700 if ( m_state
== Uncancelable
)
702 // can't close this dialog
705 else if ( m_state
== Finished
)
707 // let the default handler close the window as we already terminated
712 // next Update() will notice it
717 m_timeStop
= wxGetCurrentTime();
721 // ----------------------------------------------------------------------------
723 // ----------------------------------------------------------------------------
725 wxGenericProgressDialog::~wxGenericProgressDialog()
727 // normally this should have been already done, but just in case
728 ReenableOtherWindows();
730 if ( m_tempEventLoop
)
732 wxEventLoopBase::SetActive(NULL
);
733 delete m_tempEventLoop
;
737 void wxGenericProgressDialog::DisableOtherWindows()
739 if ( HasPDFlag(wxPD_APP_MODAL
) )
741 m_winDisabler
= new wxWindowDisabler(this);
746 m_parentTop
->Disable();
747 m_winDisabler
= NULL
;
751 void wxGenericProgressDialog::ReenableOtherWindows()
753 if ( HasPDFlag(wxPD_APP_MODAL
) )
755 wxDELETE(m_winDisabler
);
760 m_parentTop
->Enable();
764 // ----------------------------------------------------------------------------
766 // ----------------------------------------------------------------------------
768 void wxGenericProgressDialog::EnableSkip(bool enable
)
770 if ( HasPDFlag(wxPD_CAN_SKIP
) )
772 #ifdef __SMARTPHONE__
774 SetRightMenu(wxID_SKIP
, _("Skip"));
779 m_btnSkip
->Enable(enable
);
784 void wxGenericProgressDialog::EnableAbort(bool enable
)
786 if( HasPDFlag(wxPD_CAN_ABORT
) )
788 #ifdef __SMARTPHONE__
790 SetLeftMenu(wxID_CANCEL
); // stock buttons makes Cancel label
795 m_btnAbort
->Enable(enable
);
800 void wxGenericProgressDialog::EnableClose()
802 if(HasPDFlag(wxPD_CAN_ABORT
))
804 #ifdef __SMARTPHONE__
805 SetLeftMenu(wxID_CANCEL
, _("Close"));
809 m_btnAbort
->Enable();
810 m_btnAbort
->SetLabel(_("Close"));
816 void wxGenericProgressDialog::UpdateMessage(const wxString
&newmsg
)
818 if ( !newmsg
.empty() && newmsg
!= m_msg
->GetLabel() )
820 m_msg
->SetLabel(newmsg
);
822 // allow the window to repaint:
823 // NOTE: since we yield only for UI events with this call, there
824 // should be no side-effects
825 wxEventLoopBase::GetActive()->YieldFor(wxEVT_CATEGORY_UI
);
829 #endif // wxUSE_PROGRESSDLG