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 maximum
, int style
)
92 // Initialize the inherited members that we always use (even when we don't
93 // create a valid window here).
95 // we may disappear at any moment, let the others know about it
96 SetExtraStyle(GetExtraStyle() | wxWS_EX_TRANSIENT
);
99 m_parentTop
= wxGetTopLevelParent(parent
);
102 // Initialize our own members.
103 m_state
= Uncancelable
;
106 #if defined(__WXMSW__) || defined(__WXPM__)
107 // we can't have values > 65,536 in the progress control under Windows, so
108 // scale everything down
109 m_factor
= m_maximum
/ 65536 + 1;
110 m_maximum
/= m_factor
;
114 m_timeStart
= wxGetCurrentTime();
115 m_timeStop
= (unsigned long)-1;
120 m_display_estimated
=
126 m_winDisabler
= NULL
;
127 m_tempEventLoop
= NULL
;
130 wxGenericProgressDialog
::wxGenericProgressDialog(wxWindow
*parent
,
135 Init(parent
, maximum
, style
);
138 wxGenericProgressDialog
::wxGenericProgressDialog(const wxString
& title
,
139 const wxString
& message
,
145 Init(parent
, maximum
, style
);
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
) );
161 // We need a running event loop in order to update the dialog and be able
162 // to process clicks on its buttons, so ensure that there is one running
163 // even if this means we have to start it ourselves (this happens most
164 // commonly during the program initialization, e.g. for the progress
165 // dialogs shown from overridden wxApp::OnInit()).
166 if ( !wxEventLoopBase
::GetActive() )
168 m_tempEventLoop
= new wxEventLoop
;
169 wxEventLoop
::SetActive(m_tempEventLoop
);
172 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
173 // we have to remove the "Close" button from the title bar then as it is
174 // confusing to have it - it doesn't work anyhow
176 // FIXME: should probably have a (extended?) window style for this
177 if ( !HasPDFlag(wxPD_CAN_ABORT
) )
179 EnableCloseButton(false);
183 #if defined(__SMARTPHONE__)
187 m_state
= HasPDFlag(wxPD_CAN_ABORT
) ? Continue
: Uncancelable
;
189 // top-level sizerTop
190 wxSizer
* const sizerTop
= new wxBoxSizer(wxVERTICAL
);
192 m_msg
= new wxStaticText(this, wxID_ANY
, message
);
193 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
;
200 m_gauge
= new wxGauge
206 // make the progress bar sufficiently long
207 wxSize(wxMin(wxGetClientDisplayRect().width
/3, 300), -1),
211 sizerTop
->Add(m_gauge
, 0, wxLEFT
| wxRIGHT
| wxTOP
| wxEXPAND
, 2*LAYOUT_MARGIN
);
212 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__)
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 wxASSERT_MSG( value
== -1 || m_gauge
, wxT("cannot update non existent dialog") );
425 wxASSERT_MSG( value
<= m_maximum
, wxT("invalid progress value") );
428 m_gauge
->SetValue(value
);
430 UpdateMessage(newmsg
);
432 if ( (m_elapsed
|| m_remaining
|| m_estimated
) && (value
!= 0) )
434 unsigned long elapsed
;
435 unsigned long display_remaining
;
437 UpdateTimeEstimates( value
,
442 SetTimeLabel(elapsed
, m_elapsed
);
443 SetTimeLabel(m_display_estimated
, m_estimated
);
444 SetTimeLabel(display_remaining
, m_remaining
);
447 if ( value
== m_maximum
)
449 if ( m_state
== Finished
)
451 // ignore multiple calls to Update(m_maximum): it may sometimes be
452 // troublesome to ensure that Update() is not called twice with the
453 // same value (e.g. because of the rounding errors) and if we don't
454 // return now we're going to generate asserts below
458 // so that we return true below and that out [Cancel] handler knew what
461 if( !HasPDFlag(wxPD_AUTO_HIDE
) )
465 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
469 if ( newmsg
.empty() )
471 // also provide the finishing message if the application didn't
472 m_msg
->SetLabel(_("Done."));
475 // allow the window to repaint:
476 // NOTE: since we yield only for UI events with this call, there
477 // should be no side-effects
478 wxEventLoopBase
::GetActive()->YieldFor(wxEVT_CATEGORY_UI
);
480 // NOTE: this call results in a new event loop being created
481 // and to a call to ProcessPendingEvents() (which may generate
482 // unwanted re-entrancies).
487 // reenable other windows before hiding this one because otherwise
488 // Windows wouldn't give the focus back to the window which had
489 // been previously focused because it would still be disabled
490 ReenableOtherWindows();
495 else // not at maximum yet
500 // update the display in case yielding above didn't do it
503 return m_state
!= Canceled
;
506 bool wxGenericProgressDialog
::Pulse(const wxString
& newmsg
, bool *skip
)
508 if ( !DoBeforeUpdate(skip
) )
511 wxASSERT_MSG( m_gauge
, wxT("cannot update non existent dialog") );
513 // show a bit of progress
516 UpdateMessage(newmsg
);
518 if (m_elapsed
|| m_remaining
|| m_estimated
)
520 unsigned long elapsed
= wxGetCurrentTime() - m_timeStart
;
522 SetTimeLabel(elapsed
, m_elapsed
);
523 SetTimeLabel((unsigned long)-1, m_estimated
);
524 SetTimeLabel((unsigned long)-1, m_remaining
);
529 return m_state
!= Canceled
;
532 bool wxGenericProgressDialog
::DoBeforeUpdate(bool *skip
)
534 // we have to yield because not only we want to update the display but
535 // also to process the clicks on the cancel and skip buttons
536 // NOTE: using YieldFor() this call shouldn't give re-entrancy problems
537 // for event handlers not interested to UI/user-input events.
538 wxEventLoopBase
::GetActive()->YieldFor(wxEVT_CATEGORY_UI
|wxEVT_CATEGORY_USER_INPUT
);
542 if ( m_skip
&& skip
&& !*skip
)
549 return m_state
!= Canceled
;
552 void wxGenericProgressDialog
::DoAfterUpdate()
554 // allow the window to repaint:
555 // NOTE: since we yield only for UI events with this call, there
556 // should be no side-effects
557 wxEventLoopBase
::GetActive()->YieldFor(wxEVT_CATEGORY_UI
);
560 void wxGenericProgressDialog
::Resume()
563 m_ctdelay
= m_delay
; // force an update of the elapsed/estimated/remaining time
564 m_break
+= wxGetCurrentTime()-m_timeStop
;
571 bool wxGenericProgressDialog
::Show( bool show
)
573 // reenable other windows before hiding this one because otherwise
574 // Windows wouldn't give the focus back to the window which had
575 // been previously focused because it would still be disabled
577 ReenableOtherWindows();
579 return wxDialog
::Show(show
);
582 int wxGenericProgressDialog
::GetValue() const
585 return m_gauge
->GetValue();
589 int wxGenericProgressDialog
::GetRange() const
592 return m_gauge
->GetRange();
596 wxString wxGenericProgressDialog
::GetMessage() const
598 return m_msg
->GetLabel();
601 void wxGenericProgressDialog
::SetRange(int maximum
)
603 wxASSERT_MSG(m_gauge
, "The dialog should have been constructed with a range > 0");
604 wxASSERT_MSG(maximum
> 0, "Invalid range");
606 m_gauge
->SetRange(maximum
);
609 #if defined(__WXMSW__) || defined(__WXPM__)
610 // we can't have values > 65,536 in the progress control under Windows, so
611 // scale everything down
612 m_factor
= m_maximum
/ 65536 + 1;
613 m_maximum
/= m_factor
;
618 bool wxGenericProgressDialog
::WasCancelled() const
620 return HasPDFlag(wxPD_CAN_ABORT
) && m_state
== Canceled
;
623 bool wxGenericProgressDialog
::WasSkipped() const
625 return HasPDFlag(wxPD_CAN_SKIP
) && m_skip
;
629 void wxGenericProgressDialog
::SetTimeLabel(unsigned long val
,
636 if (val
!= (unsigned long)-1)
638 s
= GetFormattedTime(val
);
645 if ( s
!= label
->GetLabel() )
650 // ----------------------------------------------------------------------------
652 // ----------------------------------------------------------------------------
654 void wxGenericProgressDialog
::OnCancel(wxCommandEvent
& event
)
656 if ( m_state
== Finished
)
658 // this means that the count down is already finished and we're being
659 // shown as a modal dialog - so just let the default handler do the job
664 // request to cancel was received, the next time Update() is called we
668 // update the buttons state immediately so that the user knows that the
669 // request has been noticed
673 // save the time when the dialog was stopped
674 m_timeStop
= wxGetCurrentTime();
678 void wxGenericProgressDialog
::OnSkip(wxCommandEvent
& WXUNUSED(event
))
684 void wxGenericProgressDialog
::OnClose(wxCloseEvent
& event
)
686 if ( m_state
== Uncancelable
)
688 // can't close this dialog
691 else if ( m_state
== Finished
)
693 // let the default handler close the window as we already terminated
698 // next Update() will notice it
703 m_timeStop
= wxGetCurrentTime();
707 // ----------------------------------------------------------------------------
709 // ----------------------------------------------------------------------------
711 wxGenericProgressDialog
::~wxGenericProgressDialog()
713 // normally this should have been already done, but just in case
714 ReenableOtherWindows();
716 if ( m_tempEventLoop
)
718 wxEventLoopBase
::SetActive(NULL
);
719 delete m_tempEventLoop
;
723 void wxGenericProgressDialog
::DisableOtherWindows()
725 if ( HasPDFlag(wxPD_APP_MODAL
) )
727 m_winDisabler
= new wxWindowDisabler(this);
732 m_parentTop
->Disable();
733 m_winDisabler
= NULL
;
737 void wxGenericProgressDialog
::ReenableOtherWindows()
739 if ( HasPDFlag(wxPD_APP_MODAL
) )
741 wxDELETE(m_winDisabler
);
746 m_parentTop
->Enable();
750 // ----------------------------------------------------------------------------
752 // ----------------------------------------------------------------------------
754 void wxGenericProgressDialog
::EnableSkip(bool enable
)
756 if ( HasPDFlag(wxPD_CAN_SKIP
) )
758 #ifdef __SMARTPHONE__
760 SetRightMenu(wxID_SKIP
, _("Skip"));
765 m_btnSkip
->Enable(enable
);
770 void wxGenericProgressDialog
::EnableAbort(bool enable
)
772 if( HasPDFlag(wxPD_CAN_ABORT
) )
774 #ifdef __SMARTPHONE__
776 SetLeftMenu(wxID_CANCEL
); // stock buttons makes Cancel label
781 m_btnAbort
->Enable(enable
);
786 void wxGenericProgressDialog
::EnableClose()
788 if(HasPDFlag(wxPD_CAN_ABORT
))
790 #ifdef __SMARTPHONE__
791 SetLeftMenu(wxID_CANCEL
, _("Close"));
795 m_btnAbort
->Enable();
796 m_btnAbort
->SetLabel(_("Close"));
802 void wxGenericProgressDialog
::UpdateMessage(const wxString
&newmsg
)
804 if ( !newmsg
.empty() && newmsg
!= m_msg
->GetLabel() )
806 m_msg
->SetLabel(newmsg
);
808 // allow the window to repaint:
809 // NOTE: since we yield only for UI events with this call, there
810 // should be no side-effects
811 wxEventLoopBase
::GetActive()->YieldFor(wxEVT_CATEGORY_UI
);
815 #endif // wxUSE_PROGRESSDLG