1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/progdlgg.cpp
3 // Purpose: wxGenericProgressDialog class
4 // Author: Karsten Ballueder
7 // Copyright: (c) Karsten Ballueder
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 // For compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
31 #include "wx/button.h"
32 #include "wx/stattext.h"
37 #include "wx/dcclient.h"
39 #include "wx/settings.h"
43 #include "wx/progdlg.h"
44 #include "wx/evtloop.h"
46 // ---------------------------------------------------------------------------
48 // ---------------------------------------------------------------------------
50 /* Macro for avoiding #ifdefs when value have to be different depending on size of
51 device we display on - take it from something like wxDesktopPolicy in the future
54 #if defined(__SMARTPHONE__)
55 #define wxLARGESMALL(large,small) small
57 #define wxLARGESMALL(large,small) large
60 // ----------------------------------------------------------------------------
62 // ----------------------------------------------------------------------------
64 #define LAYOUT_MARGIN wxLARGESMALL(8,2)
66 static const int wxID_SKIP
= 32000; // whatever
68 // ----------------------------------------------------------------------------
70 // ----------------------------------------------------------------------------
72 BEGIN_EVENT_TABLE(wxGenericProgressDialog
, wxDialog
)
73 EVT_BUTTON(wxID_CANCEL
, wxGenericProgressDialog::OnCancel
)
74 EVT_BUTTON(wxID_SKIP
, wxGenericProgressDialog::OnSkip
)
76 EVT_CLOSE(wxGenericProgressDialog::OnClose
)
79 // ============================================================================
80 // wxGenericProgressDialog implementation
81 // ============================================================================
83 wxIMPLEMENT_CLASS(wxProgressDialog
, wxDialog
)
85 // ----------------------------------------------------------------------------
86 // wxGenericProgressDialog creation
87 // ----------------------------------------------------------------------------
89 void wxGenericProgressDialog::Init()
91 // we may disappear at any moment, let the others know about it
92 SetExtraStyle(GetExtraStyle() | wxWS_EX_TRANSIENT
);
94 // Initialize all our members that we always use (even when we don't
95 // create a valid window in this class).
106 m_state
= Uncancelable
;
109 m_timeStart
= wxGetCurrentTime();
110 m_timeStop
= (unsigned long)-1;
115 #if !defined(__SMARTPHONE__)
120 m_display_estimated
=
126 m_winDisabler
= NULL
;
127 m_tempEventLoop
= NULL
;
130 wxGenericProgressDialog::wxGenericProgressDialog()
136 wxGenericProgressDialog::wxGenericProgressDialog(const wxString
& title
,
137 const wxString
& message
,
145 Create( title
, message
, maximum
, parent
, style
);
148 void wxGenericProgressDialog::SetTopParent(wxWindow
* parent
)
150 m_parentTop
= GetParentForModalDialog(parent
, GetWindowStyle());
153 bool wxGenericProgressDialog::Create( const wxString
& title
,
154 const wxString
& message
,
159 SetTopParent(parent
);
161 m_parentTop
= wxGetTopLevelParent(parent
);
165 realParent
= GetParentForModalDialog(parent
, GetWindowStyle());
167 if (!wxDialog::Create(realParent
, wxID_ANY
, title
))
172 // We need a running event loop in order to update the dialog and be able
173 // to process clicks on its buttons, so ensure that there is one running
174 // even if this means we have to start it ourselves (this happens most
175 // commonly during the program initialization, e.g. for the progress
176 // dialogs shown from overridden wxApp::OnInit()).
177 if ( !wxEventLoopBase::GetActive() )
179 m_tempEventLoop
= new wxEventLoop
;
180 wxEventLoop::SetActive(m_tempEventLoop
);
183 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
184 // we have to remove the "Close" button from the title bar then as it is
185 // confusing to have it - it doesn't work anyhow
187 // FIXME: should probably have a (extended?) window style for this
188 if ( !HasPDFlag(wxPD_CAN_ABORT
) )
190 EnableCloseButton(false);
194 #if defined(__SMARTPHONE__)
198 m_state
= HasPDFlag(wxPD_CAN_ABORT
) ? Continue
: Uncancelable
;
200 // top-level sizerTop
201 wxSizer
* const sizerTop
= new wxBoxSizer(wxVERTICAL
);
203 m_msg
= new wxStaticText(this, wxID_ANY
, message
);
204 sizerTop
->Add(m_msg
, 0, wxLEFT
| wxTOP
, 2*LAYOUT_MARGIN
);
206 int gauge_style
= wxGA_HORIZONTAL
;
207 if ( style
& wxPD_SMOOTH
)
208 gauge_style
|= wxGA_SMOOTH
;
214 m_gauge
= new wxGauge
220 // make the progress bar sufficiently long
221 wxSize(wxMin(wxGetClientDisplayRect().width
/3, 300), -1),
225 sizerTop
->Add(m_gauge
, 0, wxLEFT
| wxRIGHT
| wxTOP
| wxEXPAND
, 2*LAYOUT_MARGIN
);
226 m_gauge
->SetValue(0);
228 // create the estimated/remaining/total time zones if requested
233 // also count how many labels we really have
234 size_t nTimeLabels
= 0;
236 wxSizer
* const sizerLabels
= new wxFlexGridSizer(2);
238 if ( style
& wxPD_ELAPSED_TIME
)
242 m_elapsed
= CreateLabel(GetElapsedLabel(), sizerLabels
);
245 if ( style
& wxPD_ESTIMATED_TIME
)
249 m_estimated
= CreateLabel(GetEstimatedLabel(), sizerLabels
);
252 if ( style
& wxPD_REMAINING_TIME
)
256 m_remaining
= CreateLabel(GetRemainingLabel(), sizerLabels
);
258 sizerTop
->Add(sizerLabels
, 0, wxALIGN_CENTER_HORIZONTAL
| wxTOP
, LAYOUT_MARGIN
);
260 #if defined(__SMARTPHONE__)
261 if ( HasPDFlag(wxPD_CAN_SKIP
) )
262 SetRightMenu(wxID_SKIP
, _("Skip"));
263 if ( HasPDFlag(wxPD_CAN_ABORT
) )
264 SetLeftMenu(wxID_CANCEL
);
269 wxBoxSizer
*buttonSizer
= new wxBoxSizer(wxHORIZONTAL
);
271 // Windows dialogs usually have buttons in the lower right corner
272 const int sizerFlags
=
273 #if defined(__WXMSW__) || defined(__WXPM__) || defined(__WXOSX__)
274 wxALIGN_RIGHT
| wxALL
276 wxALIGN_CENTER_HORIZONTAL
| wxBOTTOM
| wxTOP
280 if ( HasPDFlag(wxPD_CAN_SKIP
) )
282 m_btnSkip
= new wxButton(this, wxID_SKIP
, _("&Skip"));
284 buttonSizer
->Add(m_btnSkip
, 0, sizerFlags
, LAYOUT_MARGIN
);
287 if ( HasPDFlag(wxPD_CAN_ABORT
) )
289 m_btnAbort
= new wxButton(this, wxID_CANCEL
);
291 buttonSizer
->Add(m_btnAbort
, 0, sizerFlags
, LAYOUT_MARGIN
);
294 if ( !HasPDFlag(wxPD_CAN_SKIP
| wxPD_CAN_ABORT
) )
295 buttonSizer
->AddSpacer(LAYOUT_MARGIN
);
297 sizerTop
->Add(buttonSizer
, 0, sizerFlags
, LAYOUT_MARGIN
);
298 #endif // __SMARTPHONE__/!__SMARTPHONE__
300 SetSizerAndFit(sizerTop
);
302 Centre(wxCENTER_FRAME
| wxBOTH
);
304 DisableOtherWindows();
309 // this one can be initialized even if the others are unknown for now
311 // NB: do it after calling Layout() to keep the labels correctly aligned
314 SetTimeLabel(0, m_elapsed
);
321 void wxGenericProgressDialog::UpdateTimeEstimates(int value
,
322 unsigned long &elapsedTime
,
323 unsigned long &estimatedTime
,
324 unsigned long &remainingTime
)
326 unsigned long elapsed
= wxGetCurrentTime() - m_timeStart
;
327 if ( value
!= 0 && (m_last_timeupdate
< elapsed
|| value
== m_maximum
) )
329 m_last_timeupdate
= elapsed
;
330 unsigned long estimated
= m_break
+
331 (unsigned long)(( (double) (elapsed
-m_break
) * m_maximum
) / ((double)value
)) ;
332 if ( estimated
> m_display_estimated
338 else if ( estimated
< m_display_estimated
348 if ( m_ctdelay
>= m_delay
// enough confirmations for a higher value
349 || m_ctdelay
<= (m_delay
*-1) // enough confirmations for a lower value
350 || value
== m_maximum
// to stay consistent
351 || elapsed
> m_display_estimated
// to stay consistent
352 || ( elapsed
> 0 && elapsed
< 4 ) // additional updates in the beginning
355 m_display_estimated
= estimated
;
362 long display_remaining
= m_display_estimated
- elapsed
;
363 if ( display_remaining
< 0 )
365 display_remaining
= 0;
368 estimatedTime
= m_display_estimated
;
369 remainingTime
= display_remaining
;
372 elapsedTime
= elapsed
;
376 wxString
wxGenericProgressDialog::GetFormattedTime(unsigned long timeInSec
)
380 if ( timeInSec
== (unsigned long)-1 )
382 timeAsHMS
= _("Unknown");
386 unsigned hours
= timeInSec
/ 3600;
387 unsigned minutes
= (timeInSec
% 3600) / 60;
388 unsigned seconds
= timeInSec
% 60;
389 timeAsHMS
.Printf("%u:%02u:%02u", hours
, minutes
, seconds
);
396 wxGenericProgressDialog::CreateLabel(const wxString
& text
, wxSizer
*sizer
)
398 wxStaticText
*label
= new wxStaticText(this, wxID_ANY
, text
);
399 wxStaticText
*value
= new wxStaticText(this, wxID_ANY
, _("unknown"));
401 // select placement most native or nice on target GUI
402 #if defined(__SMARTPHONE__)
403 // value and time to the left in two rows
404 sizer
->Add(label
, 1, wxALIGN_LEFT
);
405 sizer
->Add(value
, 1, wxALIGN_LEFT
);
406 #elif defined(__WXMSW__) || defined(__WXPM__) || defined(__WXMAC__) || defined(__WXGTK20__)
407 // value and time centered in one row
408 sizer
->Add(label
, 1, wxLARGESMALL(wxALIGN_RIGHT
,wxALIGN_LEFT
) | wxTOP
| wxRIGHT
, LAYOUT_MARGIN
);
409 sizer
->Add(value
, 1, wxALIGN_LEFT
| wxTOP
, LAYOUT_MARGIN
);
411 // value and time to the right in one row
413 sizer
->Add(value
, 0, wxLEFT
, LAYOUT_MARGIN
);
419 // ----------------------------------------------------------------------------
420 // wxGenericProgressDialog operations
421 // ----------------------------------------------------------------------------
424 wxGenericProgressDialog::Update(int value
, const wxString
& newmsg
, bool *skip
)
426 if ( !DoBeforeUpdate(skip
) )
429 wxCHECK_MSG( m_gauge
, false, "dialog should be fully created" );
435 wxASSERT_MSG( value
<= m_maximum
, wxT("invalid progress value") );
437 m_gauge
->SetValue(value
);
439 UpdateMessage(newmsg
);
441 if ( (m_elapsed
|| m_remaining
|| m_estimated
) && (value
!= 0) )
443 unsigned long elapsed
;
444 unsigned long display_remaining
;
446 UpdateTimeEstimates( value
,
451 SetTimeLabel(elapsed
, m_elapsed
);
452 SetTimeLabel(m_display_estimated
, m_estimated
);
453 SetTimeLabel(display_remaining
, m_remaining
);
456 if ( value
== m_maximum
)
458 if ( m_state
== Finished
)
460 // ignore multiple calls to Update(m_maximum): it may sometimes be
461 // troublesome to ensure that Update() is not called twice with the
462 // same value (e.g. because of the rounding errors) and if we don't
463 // return now we're going to generate asserts below
467 // so that we return true below and that out [Cancel] handler knew what
470 if( !HasPDFlag(wxPD_AUTO_HIDE
) )
474 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
478 if ( newmsg
.empty() )
480 // also provide the finishing message if the application didn't
481 m_msg
->SetLabel(_("Done."));
484 // allow the window to repaint:
485 // NOTE: since we yield only for UI events with this call, there
486 // should be no side-effects
487 wxEventLoopBase::GetActive()->YieldFor(wxEVT_CATEGORY_UI
);
489 // NOTE: this call results in a new event loop being created
490 // and to a call to ProcessPendingEvents() (which may generate
491 // unwanted re-entrancies).
496 // reenable other windows before hiding this one because otherwise
497 // Windows wouldn't give the focus back to the window which had
498 // been previously focused because it would still be disabled
499 ReenableOtherWindows();
504 else // not at maximum yet
509 // update the display in case yielding above didn't do it
512 return m_state
!= Canceled
;
515 bool wxGenericProgressDialog::Pulse(const wxString
& newmsg
, bool *skip
)
517 if ( !DoBeforeUpdate(skip
) )
520 wxCHECK_MSG( m_gauge
, false, "dialog should be fully created" );
522 // show a bit of progress
525 UpdateMessage(newmsg
);
527 if (m_elapsed
|| m_remaining
|| m_estimated
)
529 unsigned long elapsed
= wxGetCurrentTime() - m_timeStart
;
531 SetTimeLabel(elapsed
, m_elapsed
);
532 SetTimeLabel((unsigned long)-1, m_estimated
);
533 SetTimeLabel((unsigned long)-1, m_remaining
);
538 return m_state
!= Canceled
;
541 bool wxGenericProgressDialog::DoBeforeUpdate(bool *skip
)
543 // we have to yield because not only we want to update the display but
544 // also to process the clicks on the cancel and skip buttons
545 // NOTE: using YieldFor() this call shouldn't give re-entrancy problems
546 // for event handlers not interested to UI/user-input events.
547 wxEventLoopBase::GetActive()->YieldFor(wxEVT_CATEGORY_UI
|wxEVT_CATEGORY_USER_INPUT
);
551 if ( m_skip
&& skip
&& !*skip
)
558 return m_state
!= Canceled
;
561 void wxGenericProgressDialog::DoAfterUpdate()
563 // allow the window to repaint:
564 // NOTE: since we yield only for UI events with this call, there
565 // should be no side-effects
566 wxEventLoopBase::GetActive()->YieldFor(wxEVT_CATEGORY_UI
);
569 void wxGenericProgressDialog::Resume()
572 m_ctdelay
= m_delay
; // force an update of the elapsed/estimated/remaining time
573 m_break
+= wxGetCurrentTime()-m_timeStop
;
580 bool wxGenericProgressDialog::Show( bool show
)
582 // reenable other windows before hiding this one because otherwise
583 // Windows wouldn't give the focus back to the window which had
584 // been previously focused because it would still be disabled
586 ReenableOtherWindows();
588 return wxDialog::Show(show
);
591 int wxGenericProgressDialog::GetValue() const
593 wxCHECK_MSG( m_gauge
, -1, "dialog should be fully created" );
595 return m_gauge
->GetValue();
598 int wxGenericProgressDialog::GetRange() const
603 wxString
wxGenericProgressDialog::GetMessage() const
605 return m_msg
->GetLabel();
608 void wxGenericProgressDialog::SetRange(int maximum
)
610 wxCHECK_RET( m_gauge
, "dialog should be fully created" );
612 wxCHECK_RET( maximum
> 0, "Invalid range" );
614 m_gauge
->SetRange(maximum
);
619 void wxGenericProgressDialog::SetMaximum(int maximum
)
623 #if defined(__WXMSW__) || defined(__WXPM__)
624 // we can't have values > 65,536 in the progress control under Windows, so
625 // scale everything down
626 m_factor
= m_maximum
/ 65536 + 1;
631 bool wxGenericProgressDialog::WasCancelled() const
633 return HasPDFlag(wxPD_CAN_ABORT
) && m_state
== Canceled
;
636 bool wxGenericProgressDialog::WasSkipped() const
638 return HasPDFlag(wxPD_CAN_SKIP
) && m_skip
;
642 void wxGenericProgressDialog::SetTimeLabel(unsigned long val
,
649 if (val
!= (unsigned long)-1)
651 s
= GetFormattedTime(val
);
658 if ( s
!= label
->GetLabel() )
663 // ----------------------------------------------------------------------------
665 // ----------------------------------------------------------------------------
667 void wxGenericProgressDialog::OnCancel(wxCommandEvent
& event
)
669 if ( m_state
== Finished
)
671 // this means that the count down is already finished and we're being
672 // shown as a modal dialog - so just let the default handler do the job
677 // request to cancel was received, the next time Update() is called we
681 // update the buttons state immediately so that the user knows that the
682 // request has been noticed
686 // save the time when the dialog was stopped
687 m_timeStop
= wxGetCurrentTime();
691 void wxGenericProgressDialog::OnSkip(wxCommandEvent
& WXUNUSED(event
))
697 void wxGenericProgressDialog::OnClose(wxCloseEvent
& event
)
699 if ( m_state
== Uncancelable
)
701 // can't close this dialog
704 else if ( m_state
== Finished
)
706 // let the default handler close the window as we already terminated
711 // next Update() will notice it
716 m_timeStop
= wxGetCurrentTime();
720 // ----------------------------------------------------------------------------
722 // ----------------------------------------------------------------------------
724 wxGenericProgressDialog::~wxGenericProgressDialog()
726 // normally this should have been already done, but just in case
727 ReenableOtherWindows();
729 if ( m_tempEventLoop
)
731 wxEventLoopBase::SetActive(NULL
);
732 delete m_tempEventLoop
;
736 void wxGenericProgressDialog::DisableOtherWindows()
738 if ( HasPDFlag(wxPD_APP_MODAL
) )
740 m_winDisabler
= new wxWindowDisabler(this);
745 m_parentTop
->Disable();
746 m_winDisabler
= NULL
;
750 void wxGenericProgressDialog::ReenableOtherWindows()
752 if ( HasPDFlag(wxPD_APP_MODAL
) )
754 wxDELETE(m_winDisabler
);
759 m_parentTop
->Enable();
763 // ----------------------------------------------------------------------------
765 // ----------------------------------------------------------------------------
767 void wxGenericProgressDialog::EnableSkip(bool enable
)
769 if ( HasPDFlag(wxPD_CAN_SKIP
) )
771 #ifdef __SMARTPHONE__
773 SetRightMenu(wxID_SKIP
, _("Skip"));
778 m_btnSkip
->Enable(enable
);
783 void wxGenericProgressDialog::EnableAbort(bool enable
)
785 if( HasPDFlag(wxPD_CAN_ABORT
) )
787 #ifdef __SMARTPHONE__
789 SetLeftMenu(wxID_CANCEL
); // stock buttons makes Cancel label
794 m_btnAbort
->Enable(enable
);
799 void wxGenericProgressDialog::EnableClose()
801 if(HasPDFlag(wxPD_CAN_ABORT
))
803 #ifdef __SMARTPHONE__
804 SetLeftMenu(wxID_CANCEL
, _("Close"));
808 m_btnAbort
->Enable();
809 m_btnAbort
->SetLabel(_("Close"));
815 void wxGenericProgressDialog::UpdateMessage(const wxString
&newmsg
)
817 if ( !newmsg
.empty() && newmsg
!= m_msg
->GetLabel() )
819 m_msg
->SetLabel(newmsg
);
821 // allow the window to repaint:
822 // NOTE: since we yield only for UI events with this call, there
823 // should be no side-effects
824 wxEventLoopBase::GetActive()->YieldFor(wxEVT_CATEGORY_UI
);
828 #endif // wxUSE_PROGRESSDLG