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 wxDialog::Create(GetParentForModalDialog(parent
, style
), wxID_ANY
, title
);
158 SetParent( GetParentForModalDialog(parent
, style
) );
163 // We need a running event loop in order to update the dialog and be able
164 // to process clicks on its buttons, so ensure that there is one running
165 // even if this means we have to start it ourselves (this happens most
166 // commonly during the program initialization, e.g. for the progress
167 // dialogs shown from overridden wxApp::OnInit()).
168 if ( !wxEventLoopBase::GetActive() )
170 m_tempEventLoop
= new wxEventLoop
;
171 wxEventLoop::SetActive(m_tempEventLoop
);
174 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
175 // we have to remove the "Close" button from the title bar then as it is
176 // confusing to have it - it doesn't work anyhow
178 // FIXME: should probably have a (extended?) window style for this
179 if ( !HasPDFlag(wxPD_CAN_ABORT
) )
181 EnableCloseButton(false);
185 #if defined(__SMARTPHONE__)
189 m_state
= HasPDFlag(wxPD_CAN_ABORT
) ? Continue
: Uncancelable
;
191 // top-level sizerTop
192 wxSizer
* const sizerTop
= new wxBoxSizer(wxVERTICAL
);
194 m_msg
= new wxStaticText(this, wxID_ANY
, message
);
195 sizerTop
->Add(m_msg
, 0, wxLEFT
| wxTOP
, 2*LAYOUT_MARGIN
);
197 int gauge_style
= wxGA_HORIZONTAL
;
198 if ( style
& wxPD_SMOOTH
)
199 gauge_style
|= wxGA_SMOOTH
;
205 m_gauge
= new wxGauge
211 // make the progress bar sufficiently long
212 wxSize(wxMin(wxGetClientDisplayRect().width
/3, 300), -1),
216 sizerTop
->Add(m_gauge
, 0, wxLEFT
| wxRIGHT
| wxTOP
| wxEXPAND
, 2*LAYOUT_MARGIN
);
217 m_gauge
->SetValue(0);
219 // create the estimated/remaining/total time zones if requested
224 // also count how many labels we really have
225 size_t nTimeLabels
= 0;
227 wxSizer
* const sizerLabels
= new wxFlexGridSizer(2);
229 if ( style
& wxPD_ELAPSED_TIME
)
233 m_elapsed
= CreateLabel(GetElapsedLabel(), sizerLabels
);
236 if ( style
& wxPD_ESTIMATED_TIME
)
240 m_estimated
= CreateLabel(GetEstimatedLabel(), sizerLabels
);
243 if ( style
& wxPD_REMAINING_TIME
)
247 m_remaining
= CreateLabel(GetRemainingLabel(), sizerLabels
);
249 sizerTop
->Add(sizerLabels
, 0, wxALIGN_CENTER_HORIZONTAL
| wxTOP
, LAYOUT_MARGIN
);
251 #if defined(__SMARTPHONE__)
252 if ( HasPDFlag(wxPD_CAN_SKIP
) )
253 SetRightMenu(wxID_SKIP
, _("Skip"));
254 if ( HasPDFlag(wxPD_CAN_ABORT
) )
255 SetLeftMenu(wxID_CANCEL
);
260 wxBoxSizer
*buttonSizer
= new wxBoxSizer(wxHORIZONTAL
);
262 // Windows dialogs usually have buttons in the lower right corner
263 const int sizerFlags
=
264 #if defined(__WXMSW__) || defined(__WXPM__) || defined(__WXOSX__)
265 wxALIGN_RIGHT
| wxALL
267 wxALIGN_CENTER_HORIZONTAL
| wxBOTTOM
| wxTOP
271 if ( HasPDFlag(wxPD_CAN_SKIP
) )
273 m_btnSkip
= new wxButton(this, wxID_SKIP
, _("&Skip"));
275 buttonSizer
->Add(m_btnSkip
, 0, sizerFlags
, LAYOUT_MARGIN
);
278 if ( HasPDFlag(wxPD_CAN_ABORT
) )
280 m_btnAbort
= new wxButton(this, wxID_CANCEL
);
282 buttonSizer
->Add(m_btnAbort
, 0, sizerFlags
, LAYOUT_MARGIN
);
285 if ( !HasPDFlag(wxPD_CAN_SKIP
| wxPD_CAN_ABORT
) )
286 buttonSizer
->AddSpacer(LAYOUT_MARGIN
);
288 sizerTop
->Add(buttonSizer
, 0, sizerFlags
, LAYOUT_MARGIN
);
289 #endif // __SMARTPHONE__/!__SMARTPHONE__
291 SetSizerAndFit(sizerTop
);
293 Centre(wxCENTER_FRAME
| wxBOTH
);
295 DisableOtherWindows();
300 // this one can be initialized even if the others are unknown for now
302 // NB: do it after calling Layout() to keep the labels correctly aligned
305 SetTimeLabel(0, m_elapsed
);
311 void wxGenericProgressDialog::UpdateTimeEstimates(int value
,
312 unsigned long &elapsedTime
,
313 unsigned long &estimatedTime
,
314 unsigned long &remainingTime
)
316 unsigned long elapsed
= wxGetCurrentTime() - m_timeStart
;
317 if ( value
!= 0 && (m_last_timeupdate
< elapsed
|| value
== m_maximum
) )
319 m_last_timeupdate
= elapsed
;
320 unsigned long estimated
= m_break
+
321 (unsigned long)(( (double) (elapsed
-m_break
) * m_maximum
) / ((double)value
)) ;
322 if ( estimated
> m_display_estimated
328 else if ( estimated
< m_display_estimated
338 if ( m_ctdelay
>= m_delay
// enough confirmations for a higher value
339 || m_ctdelay
<= (m_delay
*-1) // enough confirmations for a lower value
340 || value
== m_maximum
// to stay consistent
341 || elapsed
> m_display_estimated
// to stay consistent
342 || ( elapsed
> 0 && elapsed
< 4 ) // additional updates in the beginning
345 m_display_estimated
= estimated
;
352 long display_remaining
= m_display_estimated
- elapsed
;
353 if ( display_remaining
< 0 )
355 display_remaining
= 0;
358 estimatedTime
= m_display_estimated
;
359 remainingTime
= display_remaining
;
362 elapsedTime
= elapsed
;
366 wxString
wxGenericProgressDialog::GetFormattedTime(unsigned long timeInSec
)
370 if ( timeInSec
== (unsigned long)-1 )
372 timeAsHMS
= _("Unknown");
376 unsigned hours
= timeInSec
/ 3600;
377 unsigned minutes
= (timeInSec
% 3600) / 60;
378 unsigned seconds
= timeInSec
% 60;
379 timeAsHMS
.Printf("%u:%02u:%02u", hours
, minutes
, seconds
);
386 wxGenericProgressDialog::CreateLabel(const wxString
& text
, wxSizer
*sizer
)
388 wxStaticText
*label
= new wxStaticText(this, wxID_ANY
, text
);
389 wxStaticText
*value
= new wxStaticText(this, wxID_ANY
, _("unknown"));
391 // select placement most native or nice on target GUI
392 #if defined(__SMARTPHONE__)
393 // value and time to the left in two rows
394 sizer
->Add(label
, 1, wxALIGN_LEFT
);
395 sizer
->Add(value
, 1, wxALIGN_LEFT
);
396 #elif defined(__WXMSW__) || defined(__WXPM__) || defined(__WXMAC__) || defined(__WXGTK20__)
397 // value and time centered in one row
398 sizer
->Add(label
, 1, wxLARGESMALL(wxALIGN_RIGHT
,wxALIGN_LEFT
) | wxTOP
| wxRIGHT
, LAYOUT_MARGIN
);
399 sizer
->Add(value
, 1, wxALIGN_LEFT
| wxTOP
, LAYOUT_MARGIN
);
401 // value and time to the right in one row
403 sizer
->Add(value
, 0, wxLEFT
, LAYOUT_MARGIN
);
409 // ----------------------------------------------------------------------------
410 // wxGenericProgressDialog operations
411 // ----------------------------------------------------------------------------
414 wxGenericProgressDialog::Update(int value
, const wxString
& newmsg
, bool *skip
)
416 if ( !DoBeforeUpdate(skip
) )
419 wxCHECK_MSG( m_gauge
, false, "dialog should be fully created" );
425 wxASSERT_MSG( value
<= m_maximum
, wxT("invalid progress value") );
427 m_gauge
->SetValue(value
);
429 UpdateMessage(newmsg
);
431 if ( (m_elapsed
|| m_remaining
|| m_estimated
) && (value
!= 0) )
433 unsigned long elapsed
;
434 unsigned long display_remaining
;
436 UpdateTimeEstimates( value
,
441 SetTimeLabel(elapsed
, m_elapsed
);
442 SetTimeLabel(m_display_estimated
, m_estimated
);
443 SetTimeLabel(display_remaining
, m_remaining
);
446 if ( value
== m_maximum
)
448 if ( m_state
== Finished
)
450 // ignore multiple calls to Update(m_maximum): it may sometimes be
451 // troublesome to ensure that Update() is not called twice with the
452 // same value (e.g. because of the rounding errors) and if we don't
453 // return now we're going to generate asserts below
457 // so that we return true below and that out [Cancel] handler knew what
460 if( !HasPDFlag(wxPD_AUTO_HIDE
) )
464 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
468 if ( newmsg
.empty() )
470 // also provide the finishing message if the application didn't
471 m_msg
->SetLabel(_("Done."));
474 // allow the window to repaint:
475 // NOTE: since we yield only for UI events with this call, there
476 // should be no side-effects
477 wxEventLoopBase::GetActive()->YieldFor(wxEVT_CATEGORY_UI
);
479 // NOTE: this call results in a new event loop being created
480 // and to a call to ProcessPendingEvents() (which may generate
481 // unwanted re-entrancies).
486 // reenable other windows before hiding this one because otherwise
487 // Windows wouldn't give the focus back to the window which had
488 // been previously focused because it would still be disabled
489 ReenableOtherWindows();
494 else // not at maximum yet
499 // update the display in case yielding above didn't do it
502 return m_state
!= Canceled
;
505 bool wxGenericProgressDialog::Pulse(const wxString
& newmsg
, bool *skip
)
507 if ( !DoBeforeUpdate(skip
) )
510 wxCHECK_MSG( m_gauge
, false, "dialog should be fully created" );
512 // show a bit of progress
515 UpdateMessage(newmsg
);
517 if (m_elapsed
|| m_remaining
|| m_estimated
)
519 unsigned long elapsed
= wxGetCurrentTime() - m_timeStart
;
521 SetTimeLabel(elapsed
, m_elapsed
);
522 SetTimeLabel((unsigned long)-1, m_estimated
);
523 SetTimeLabel((unsigned long)-1, m_remaining
);
528 return m_state
!= Canceled
;
531 bool wxGenericProgressDialog::DoBeforeUpdate(bool *skip
)
533 // we have to yield because not only we want to update the display but
534 // also to process the clicks on the cancel and skip buttons
535 // NOTE: using YieldFor() this call shouldn't give re-entrancy problems
536 // for event handlers not interested to UI/user-input events.
537 wxEventLoopBase::GetActive()->YieldFor(wxEVT_CATEGORY_UI
|wxEVT_CATEGORY_USER_INPUT
);
541 if ( m_skip
&& skip
&& !*skip
)
548 return m_state
!= Canceled
;
551 void wxGenericProgressDialog::DoAfterUpdate()
553 // allow the window to repaint:
554 // NOTE: since we yield only for UI events with this call, there
555 // should be no side-effects
556 wxEventLoopBase::GetActive()->YieldFor(wxEVT_CATEGORY_UI
);
559 void wxGenericProgressDialog::Resume()
562 m_ctdelay
= m_delay
; // force an update of the elapsed/estimated/remaining time
563 m_break
+= wxGetCurrentTime()-m_timeStop
;
570 bool wxGenericProgressDialog::Show( bool show
)
572 // reenable other windows before hiding this one because otherwise
573 // Windows wouldn't give the focus back to the window which had
574 // been previously focused because it would still be disabled
576 ReenableOtherWindows();
578 return wxDialog::Show(show
);
581 int wxGenericProgressDialog::GetValue() const
583 wxCHECK_MSG( m_gauge
, -1, "dialog should be fully created" );
585 return m_gauge
->GetValue();
588 int wxGenericProgressDialog::GetRange() const
593 wxString
wxGenericProgressDialog::GetMessage() const
595 return m_msg
->GetLabel();
598 void wxGenericProgressDialog::SetRange(int maximum
)
600 wxCHECK_RET( m_gauge
, "dialog should be fully created" );
602 wxCHECK_RET( maximum
> 0, "Invalid range" );
604 m_gauge
->SetRange(maximum
);
609 void wxGenericProgressDialog::SetMaximum(int maximum
)
613 #if defined(__WXMSW__) || defined(__WXPM__)
614 // we can't have values > 65,536 in the progress control under Windows, so
615 // scale everything down
616 m_factor
= m_maximum
/ 65536 + 1;
621 bool wxGenericProgressDialog::WasCancelled() const
623 return HasPDFlag(wxPD_CAN_ABORT
) && m_state
== Canceled
;
626 bool wxGenericProgressDialog::WasSkipped() const
628 return HasPDFlag(wxPD_CAN_SKIP
) && m_skip
;
632 void wxGenericProgressDialog::SetTimeLabel(unsigned long val
,
639 if (val
!= (unsigned long)-1)
641 s
= GetFormattedTime(val
);
648 if ( s
!= label
->GetLabel() )
653 // ----------------------------------------------------------------------------
655 // ----------------------------------------------------------------------------
657 void wxGenericProgressDialog::OnCancel(wxCommandEvent
& event
)
659 if ( m_state
== Finished
)
661 // this means that the count down is already finished and we're being
662 // shown as a modal dialog - so just let the default handler do the job
667 // request to cancel was received, the next time Update() is called we
671 // update the buttons state immediately so that the user knows that the
672 // request has been noticed
676 // save the time when the dialog was stopped
677 m_timeStop
= wxGetCurrentTime();
681 void wxGenericProgressDialog::OnSkip(wxCommandEvent
& WXUNUSED(event
))
687 void wxGenericProgressDialog::OnClose(wxCloseEvent
& event
)
689 if ( m_state
== Uncancelable
)
691 // can't close this dialog
694 else if ( m_state
== Finished
)
696 // let the default handler close the window as we already terminated
701 // next Update() will notice it
706 m_timeStop
= wxGetCurrentTime();
710 // ----------------------------------------------------------------------------
712 // ----------------------------------------------------------------------------
714 wxGenericProgressDialog::~wxGenericProgressDialog()
716 // normally this should have been already done, but just in case
717 ReenableOtherWindows();
719 if ( m_tempEventLoop
)
721 wxEventLoopBase::SetActive(NULL
);
722 delete m_tempEventLoop
;
726 void wxGenericProgressDialog::DisableOtherWindows()
728 if ( HasPDFlag(wxPD_APP_MODAL
) )
730 m_winDisabler
= new wxWindowDisabler(this);
735 m_parentTop
->Disable();
736 m_winDisabler
= NULL
;
740 void wxGenericProgressDialog::ReenableOtherWindows()
742 if ( HasPDFlag(wxPD_APP_MODAL
) )
744 wxDELETE(m_winDisabler
);
749 m_parentTop
->Enable();
753 // ----------------------------------------------------------------------------
755 // ----------------------------------------------------------------------------
757 void wxGenericProgressDialog::EnableSkip(bool enable
)
759 if ( HasPDFlag(wxPD_CAN_SKIP
) )
761 #ifdef __SMARTPHONE__
763 SetRightMenu(wxID_SKIP
, _("Skip"));
768 m_btnSkip
->Enable(enable
);
773 void wxGenericProgressDialog::EnableAbort(bool enable
)
775 if( HasPDFlag(wxPD_CAN_ABORT
) )
777 #ifdef __SMARTPHONE__
779 SetLeftMenu(wxID_CANCEL
); // stock buttons makes Cancel label
784 m_btnAbort
->Enable(enable
);
789 void wxGenericProgressDialog::EnableClose()
791 if(HasPDFlag(wxPD_CAN_ABORT
))
793 #ifdef __SMARTPHONE__
794 SetLeftMenu(wxID_CANCEL
, _("Close"));
798 m_btnAbort
->Enable();
799 m_btnAbort
->SetLabel(_("Close"));
805 void wxGenericProgressDialog::UpdateMessage(const wxString
&newmsg
)
807 if ( !newmsg
.empty() && newmsg
!= m_msg
->GetLabel() )
809 m_msg
->SetLabel(newmsg
);
811 // allow the window to repaint:
812 // NOTE: since we yield only for UI events with this call, there
813 // should be no side-effects
814 wxEventLoopBase::GetActive()->YieldFor(wxEVT_CATEGORY_UI
);
818 #endif // wxUSE_PROGRESSDLG