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 m_display_estimated
=
123 m_winDisabler
= NULL
;
124 m_tempEventLoop
= NULL
;
127 wxGenericProgressDialog
::wxGenericProgressDialog(wxWindow
*parent
, int style
)
133 wxGenericProgressDialog
::wxGenericProgressDialog(const wxString
& title
,
134 const wxString
& message
,
142 Create( title
, message
, maximum
, parent
, style
);
145 void wxGenericProgressDialog
::Create( const wxString
& title
,
146 const wxString
& message
,
151 wxDialog
::Create(GetParentForModalDialog(parent
, style
), wxID_ANY
, title
);
153 SetParent( GetParentForModalDialog(parent
, style
) );
158 // We need a running event loop in order to update the dialog and be able
159 // to process clicks on its buttons, so ensure that there is one running
160 // even if this means we have to start it ourselves (this happens most
161 // commonly during the program initialization, e.g. for the progress
162 // dialogs shown from overridden wxApp::OnInit()).
163 if ( !wxEventLoopBase
::GetActive() )
165 m_tempEventLoop
= new wxEventLoop
;
166 wxEventLoop
::SetActive(m_tempEventLoop
);
169 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
170 // we have to remove the "Close" button from the title bar then as it is
171 // confusing to have it - it doesn't work anyhow
173 // FIXME: should probably have a (extended?) window style for this
174 if ( !HasPDFlag(wxPD_CAN_ABORT
) )
176 EnableCloseButton(false);
180 #if defined(__SMARTPHONE__)
184 m_state
= HasPDFlag(wxPD_CAN_ABORT
) ? Continue
: Uncancelable
;
186 // top-level sizerTop
187 wxSizer
* const sizerTop
= new wxBoxSizer(wxVERTICAL
);
189 m_msg
= new wxStaticText(this, wxID_ANY
, message
);
190 sizerTop
->Add(m_msg
, 0, wxLEFT
| wxTOP
, 2*LAYOUT_MARGIN
);
192 int gauge_style
= wxGA_HORIZONTAL
;
193 if ( style
& wxPD_SMOOTH
)
194 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);
214 // create the estimated/remaining/total time zones if requested
219 // also count how many labels we really have
220 size_t nTimeLabels
= 0;
222 wxSizer
* const sizerLabels
= new wxFlexGridSizer(2);
224 if ( style
& wxPD_ELAPSED_TIME
)
228 m_elapsed
= CreateLabel(GetElapsedLabel(), sizerLabels
);
231 if ( style
& wxPD_ESTIMATED_TIME
)
235 m_estimated
= CreateLabel(GetEstimatedLabel(), sizerLabels
);
238 if ( style
& wxPD_REMAINING_TIME
)
242 m_remaining
= CreateLabel(GetRemainingLabel(), sizerLabels
);
244 sizerTop
->Add(sizerLabels
, 0, wxALIGN_CENTER_HORIZONTAL
| wxTOP
, LAYOUT_MARGIN
);
246 #if defined(__SMARTPHONE__)
247 if ( HasPDFlag(wxPD_CAN_SKIP
) )
248 SetRightMenu(wxID_SKIP
, _("Skip"));
249 if ( HasPDFlag(wxPD_CAN_ABORT
) )
250 SetLeftMenu(wxID_CANCEL
);
255 wxBoxSizer
*buttonSizer
= new wxBoxSizer(wxHORIZONTAL
);
257 // Windows dialogs usually have buttons in the lower right corner
258 const int sizerFlags
=
259 #if defined(__WXMSW__) || defined(__WXPM__)
260 wxALIGN_RIGHT
| wxALL
262 wxALIGN_CENTER_HORIZONTAL
| wxBOTTOM
| wxTOP
266 if ( HasPDFlag(wxPD_CAN_SKIP
) )
268 m_btnSkip
= new wxButton(this, wxID_SKIP
, _("&Skip"));
270 buttonSizer
->Add(m_btnSkip
, 0, sizerFlags
, LAYOUT_MARGIN
);
273 if ( HasPDFlag(wxPD_CAN_ABORT
) )
275 m_btnAbort
= new wxButton(this, wxID_CANCEL
);
277 buttonSizer
->Add(m_btnAbort
, 0, sizerFlags
, LAYOUT_MARGIN
);
280 if ( !HasPDFlag(wxPD_CAN_SKIP
| wxPD_CAN_ABORT
) )
281 buttonSizer
->AddSpacer(LAYOUT_MARGIN
);
283 sizerTop
->Add(buttonSizer
, 0, sizerFlags
, LAYOUT_MARGIN
);
284 #endif // __SMARTPHONE__/!__SMARTPHONE__
286 SetSizerAndFit(sizerTop
);
288 Centre(wxCENTER_FRAME
| wxBOTH
);
290 DisableOtherWindows();
295 // this one can be initialized even if the others are unknown for now
297 // NB: do it after calling Layout() to keep the labels correctly aligned
300 SetTimeLabel(0, m_elapsed
);
306 void wxGenericProgressDialog
::UpdateTimeEstimates(int value
,
307 unsigned long &elapsedTime
,
308 unsigned long &estimatedTime
,
309 unsigned long &remainingTime
)
311 unsigned long elapsed
= wxGetCurrentTime() - m_timeStart
;
312 if ( value
!= 0 && (m_last_timeupdate
< elapsed
|| value
== m_maximum
) )
314 m_last_timeupdate
= elapsed
;
315 unsigned long estimated
= m_break
+
316 (unsigned long)(( (double) (elapsed
-m_break
) * m_maximum
) / ((double)value
)) ;
317 if ( estimated
> m_display_estimated
323 else if ( estimated
< m_display_estimated
333 if ( m_ctdelay
>= m_delay
// enough confirmations for a higher value
334 || m_ctdelay
<= (m_delay
*-1) // enough confirmations for a lower value
335 || value
== m_maximum
// to stay consistent
336 || elapsed
> m_display_estimated
// to stay consistent
337 || ( elapsed
> 0 && elapsed
< 4 ) // additional updates in the beginning
340 m_display_estimated
= estimated
;
347 long display_remaining
= m_display_estimated
- elapsed
;
348 if ( display_remaining
< 0 )
350 display_remaining
= 0;
353 estimatedTime
= m_display_estimated
;
354 remainingTime
= display_remaining
;
357 elapsedTime
= elapsed
;
361 wxString wxGenericProgressDialog
::GetFormattedTime(unsigned long timeInSec
)
365 if ( timeInSec
== (unsigned long)-1 )
367 timeAsHMS
= _("Unknown");
371 unsigned hours
= timeInSec
/ 3600;
372 unsigned minutes
= (timeInSec
% 3600) / 60;
373 unsigned seconds
= timeInSec
% 60;
374 timeAsHMS
.Printf("%u:%02u:%02u", hours
, minutes
, seconds
);
381 wxGenericProgressDialog
::CreateLabel(const wxString
& text
, wxSizer
*sizer
)
383 wxStaticText
*label
= new wxStaticText(this, wxID_ANY
, text
);
384 wxStaticText
*value
= new wxStaticText(this, wxID_ANY
, _("unknown"));
386 // select placement most native or nice on target GUI
387 #if defined(__SMARTPHONE__)
388 // value and time to the left in two rows
389 sizer
->Add(label
, 1, wxALIGN_LEFT
);
390 sizer
->Add(value
, 1, wxALIGN_LEFT
);
391 #elif defined(__WXMSW__) || defined(__WXPM__) || defined(__WXMAC__) || defined(__WXGTK20__)
392 // value and time centered in one row
393 sizer
->Add(label
, 1, wxLARGESMALL(wxALIGN_RIGHT
,wxALIGN_LEFT
) | wxTOP
| wxRIGHT
, LAYOUT_MARGIN
);
394 sizer
->Add(value
, 1, wxALIGN_LEFT
| wxTOP
, LAYOUT_MARGIN
);
396 // value and time to the right in one row
398 sizer
->Add(value
, 0, wxLEFT
, LAYOUT_MARGIN
);
404 // ----------------------------------------------------------------------------
405 // wxGenericProgressDialog operations
406 // ----------------------------------------------------------------------------
409 wxGenericProgressDialog
::Update(int value
, const wxString
& newmsg
, bool *skip
)
411 if ( !DoBeforeUpdate(skip
) )
414 wxCHECK_MSG( m_gauge
, false, "dialog should be fully created" );
420 wxASSERT_MSG( value
<= m_maximum
, wxT("invalid progress value") );
422 m_gauge
->SetValue(value
);
424 UpdateMessage(newmsg
);
426 if ( (m_elapsed
|| m_remaining
|| m_estimated
) && (value
!= 0) )
428 unsigned long elapsed
;
429 unsigned long display_remaining
;
431 UpdateTimeEstimates( value
,
436 SetTimeLabel(elapsed
, m_elapsed
);
437 SetTimeLabel(m_display_estimated
, m_estimated
);
438 SetTimeLabel(display_remaining
, m_remaining
);
441 if ( value
== m_maximum
)
443 if ( m_state
== Finished
)
445 // ignore multiple calls to Update(m_maximum): it may sometimes be
446 // troublesome to ensure that Update() is not called twice with the
447 // same value (e.g. because of the rounding errors) and if we don't
448 // return now we're going to generate asserts below
452 // so that we return true below and that out [Cancel] handler knew what
455 if( !HasPDFlag(wxPD_AUTO_HIDE
) )
459 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
463 if ( newmsg
.empty() )
465 // also provide the finishing message if the application didn't
466 m_msg
->SetLabel(_("Done."));
469 // allow the window to repaint:
470 // NOTE: since we yield only for UI events with this call, there
471 // should be no side-effects
472 wxEventLoopBase
::GetActive()->YieldFor(wxEVT_CATEGORY_UI
);
474 // NOTE: this call results in a new event loop being created
475 // and to a call to ProcessPendingEvents() (which may generate
476 // unwanted re-entrancies).
481 // reenable other windows before hiding this one because otherwise
482 // Windows wouldn't give the focus back to the window which had
483 // been previously focused because it would still be disabled
484 ReenableOtherWindows();
489 else // not at maximum yet
494 // update the display in case yielding above didn't do it
497 return m_state
!= Canceled
;
500 bool wxGenericProgressDialog
::Pulse(const wxString
& newmsg
, bool *skip
)
502 if ( !DoBeforeUpdate(skip
) )
505 wxCHECK_MSG( m_gauge
, false, "dialog should be fully created" );
507 // show a bit of progress
510 UpdateMessage(newmsg
);
512 if (m_elapsed
|| m_remaining
|| m_estimated
)
514 unsigned long elapsed
= wxGetCurrentTime() - m_timeStart
;
516 SetTimeLabel(elapsed
, m_elapsed
);
517 SetTimeLabel((unsigned long)-1, m_estimated
);
518 SetTimeLabel((unsigned long)-1, m_remaining
);
523 return m_state
!= Canceled
;
526 bool wxGenericProgressDialog
::DoBeforeUpdate(bool *skip
)
528 // we have to yield because not only we want to update the display but
529 // also to process the clicks on the cancel and skip buttons
530 // NOTE: using YieldFor() this call shouldn't give re-entrancy problems
531 // for event handlers not interested to UI/user-input events.
532 wxEventLoopBase
::GetActive()->YieldFor(wxEVT_CATEGORY_UI
|wxEVT_CATEGORY_USER_INPUT
);
536 if ( m_skip
&& skip
&& !*skip
)
543 return m_state
!= Canceled
;
546 void wxGenericProgressDialog
::DoAfterUpdate()
548 // allow the window to repaint:
549 // NOTE: since we yield only for UI events with this call, there
550 // should be no side-effects
551 wxEventLoopBase
::GetActive()->YieldFor(wxEVT_CATEGORY_UI
);
554 void wxGenericProgressDialog
::Resume()
557 m_ctdelay
= m_delay
; // force an update of the elapsed/estimated/remaining time
558 m_break
+= wxGetCurrentTime()-m_timeStop
;
565 bool wxGenericProgressDialog
::Show( bool show
)
567 // reenable other windows before hiding this one because otherwise
568 // Windows wouldn't give the focus back to the window which had
569 // been previously focused because it would still be disabled
571 ReenableOtherWindows();
573 return wxDialog
::Show(show
);
576 int wxGenericProgressDialog
::GetValue() const
578 wxCHECK_MSG( m_gauge
, -1, "dialog should be fully created" );
580 return m_gauge
->GetValue();
583 int wxGenericProgressDialog
::GetRange() const
588 wxString wxGenericProgressDialog
::GetMessage() const
590 return m_msg
->GetLabel();
593 void wxGenericProgressDialog
::SetRange(int maximum
)
595 wxCHECK_RET( m_gauge
, "dialog should be fully created" );
597 wxCHECK_RET( maximum
> 0, "Invalid range" );
599 m_gauge
->SetRange(maximum
);
604 void wxGenericProgressDialog
::SetMaximum(int maximum
)
608 #if defined(__WXMSW__) || defined(__WXPM__)
609 // we can't have values > 65,536 in the progress control under Windows, so
610 // scale everything down
611 m_factor
= m_maximum
/ 65536 + 1;
616 bool wxGenericProgressDialog
::WasCancelled() const
618 return HasPDFlag(wxPD_CAN_ABORT
) && m_state
== Canceled
;
621 bool wxGenericProgressDialog
::WasSkipped() const
623 return HasPDFlag(wxPD_CAN_SKIP
) && m_skip
;
627 void wxGenericProgressDialog
::SetTimeLabel(unsigned long val
,
634 if (val
!= (unsigned long)-1)
636 s
= GetFormattedTime(val
);
643 if ( s
!= label
->GetLabel() )
648 // ----------------------------------------------------------------------------
650 // ----------------------------------------------------------------------------
652 void wxGenericProgressDialog
::OnCancel(wxCommandEvent
& event
)
654 if ( m_state
== Finished
)
656 // this means that the count down is already finished and we're being
657 // shown as a modal dialog - so just let the default handler do the job
662 // request to cancel was received, the next time Update() is called we
666 // update the buttons state immediately so that the user knows that the
667 // request has been noticed
671 // save the time when the dialog was stopped
672 m_timeStop
= wxGetCurrentTime();
676 void wxGenericProgressDialog
::OnSkip(wxCommandEvent
& WXUNUSED(event
))
682 void wxGenericProgressDialog
::OnClose(wxCloseEvent
& event
)
684 if ( m_state
== Uncancelable
)
686 // can't close this dialog
689 else if ( m_state
== Finished
)
691 // let the default handler close the window as we already terminated
696 // next Update() will notice it
701 m_timeStop
= wxGetCurrentTime();
705 // ----------------------------------------------------------------------------
707 // ----------------------------------------------------------------------------
709 wxGenericProgressDialog
::~wxGenericProgressDialog()
711 // normally this should have been already done, but just in case
712 ReenableOtherWindows();
714 if ( m_tempEventLoop
)
716 wxEventLoopBase
::SetActive(NULL
);
717 delete m_tempEventLoop
;
721 void wxGenericProgressDialog
::DisableOtherWindows()
723 if ( HasPDFlag(wxPD_APP_MODAL
) )
725 m_winDisabler
= new wxWindowDisabler(this);
730 m_parentTop
->Disable();
731 m_winDisabler
= NULL
;
735 void wxGenericProgressDialog
::ReenableOtherWindows()
737 if ( HasPDFlag(wxPD_APP_MODAL
) )
739 wxDELETE(m_winDisabler
);
744 m_parentTop
->Enable();
748 // ----------------------------------------------------------------------------
750 // ----------------------------------------------------------------------------
752 void wxGenericProgressDialog
::EnableSkip(bool enable
)
754 if ( HasPDFlag(wxPD_CAN_SKIP
) )
756 #ifdef __SMARTPHONE__
758 SetRightMenu(wxID_SKIP
, _("Skip"));
763 m_btnSkip
->Enable(enable
);
768 void wxGenericProgressDialog
::EnableAbort(bool enable
)
770 if( HasPDFlag(wxPD_CAN_ABORT
) )
772 #ifdef __SMARTPHONE__
774 SetLeftMenu(wxID_CANCEL
); // stock buttons makes Cancel label
779 m_btnAbort
->Enable(enable
);
784 void wxGenericProgressDialog
::EnableClose()
786 if(HasPDFlag(wxPD_CAN_ABORT
))
788 #ifdef __SMARTPHONE__
789 SetLeftMenu(wxID_CANCEL
, _("Close"));
793 m_btnAbort
->Enable();
794 m_btnAbort
->SetLabel(_("Close"));
800 void wxGenericProgressDialog
::UpdateMessage(const wxString
&newmsg
)
802 if ( !newmsg
.empty() && newmsg
!= m_msg
->GetLabel() )
804 m_msg
->SetLabel(newmsg
);
806 // allow the window to repaint:
807 // NOTE: since we yield only for UI events with this call, there
808 // should be no side-effects
809 wxEventLoopBase
::GetActive()->YieldFor(wxEVT_CATEGORY_UI
);
813 #endif // wxUSE_PROGRESSDLG