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
);
97 m_windowStyle
|= style
;
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
=
127 m_hasSkipButton
= false;
129 m_winDisabler
= NULL
;
132 wxGenericProgressDialog
::wxGenericProgressDialog(wxWindow
*parent
,
137 Init(parent
, maximum
, style
);
140 wxGenericProgressDialog
::wxGenericProgressDialog(const wxString
& title
,
141 const wxString
& message
,
147 Init(parent
, maximum
, style
);
149 Create( title
, message
, maximum
, parent
, style
);
152 void wxGenericProgressDialog
::Create( const wxString
& title
,
153 const wxString
& message
,
158 wxDialog
::Create(GetParentForModalDialog(parent
, style
), wxID_ANY
, title
);
160 SetParent( GetParentForModalDialog(parent
, style
) );
163 m_hasAbortButton
= (style
& wxPD_CAN_ABORT
) != 0;
164 m_hasSkipButton
= (style
& wxPD_CAN_SKIP
) != 0;
166 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
167 // we have to remove the "Close" button from the title bar then as it is
168 // confusing to have it - it doesn't work anyhow
170 // FIXME: should probably have a (extended?) window style for this
171 if ( !m_hasAbortButton
)
173 EnableCloseButton(false);
177 #if defined(__SMARTPHONE__)
181 m_state
= m_hasAbortButton ? Continue
: Uncancelable
;
183 // top-level sizerTop
184 wxSizer
* const sizerTop
= new wxBoxSizer(wxVERTICAL
);
186 m_msg
= new wxStaticText(this, wxID_ANY
, message
);
187 sizerTop
->Add(m_msg
, 0, wxLEFT
| wxTOP
, 2*LAYOUT_MARGIN
);
191 int gauge_style
= wxGA_HORIZONTAL
;
192 if ( style
& wxPD_SMOOTH
)
193 gauge_style
|= wxGA_SMOOTH
;
194 m_gauge
= new wxGauge
200 // make the progress bar sufficiently long
201 wxSize(wxMin(wxGetClientDisplayRect().width
/3, 300), -1),
205 sizerTop
->Add(m_gauge
, 0, wxLEFT
| wxRIGHT
| wxTOP
| wxEXPAND
, 2*LAYOUT_MARGIN
);
206 m_gauge
->SetValue(0);
213 // create the estimated/remaining/total time zones if requested
218 // also count how many labels we really have
219 size_t nTimeLabels
= 0;
221 wxSizer
* const sizerLabels
= new wxFlexGridSizer(2);
223 if ( style
& wxPD_ELAPSED_TIME
)
227 m_elapsed
= CreateLabel(GetElapsedLabel(), sizerLabels
);
230 if ( style
& wxPD_ESTIMATED_TIME
)
234 m_estimated
= CreateLabel(GetEstimatedLabel(), sizerLabels
);
237 if ( style
& wxPD_REMAINING_TIME
)
241 m_remaining
= CreateLabel(GetRemainingLabel(), sizerLabels
);
243 sizerTop
->Add(sizerLabels
, 0, wxALIGN_CENTER_HORIZONTAL
| wxTOP
, LAYOUT_MARGIN
);
245 #if defined(__SMARTPHONE__)
246 if ( m_hasSkipButton
)
247 SetRightMenu(wxID_SKIP
, _("Skip"));
248 if ( m_hasAbortButton
)
249 SetLeftMenu(wxID_CANCEL
);
254 wxBoxSizer
*buttonSizer
= new wxBoxSizer(wxHORIZONTAL
);
256 // Windows dialogs usually have buttons in the lower right corner
257 const int sizerFlags
=
258 #if defined(__WXMSW__) || defined(__WXPM__)
259 wxALIGN_RIGHT
| wxALL
261 wxALIGN_CENTER_HORIZONTAL
| wxBOTTOM
| wxTOP
265 if ( m_hasSkipButton
)
267 m_btnSkip
= new wxButton(this, wxID_SKIP
, _("&Skip"));
269 buttonSizer
->Add(m_btnSkip
, 0, sizerFlags
, LAYOUT_MARGIN
);
272 if ( m_hasAbortButton
)
274 m_btnAbort
= new wxButton(this, wxID_CANCEL
);
276 buttonSizer
->Add(m_btnAbort
, 0, sizerFlags
, LAYOUT_MARGIN
);
279 if (!m_hasSkipButton
&& !m_hasAbortButton
)
280 buttonSizer
->AddSpacer(LAYOUT_MARGIN
);
282 sizerTop
->Add(buttonSizer
, 0, sizerFlags
, LAYOUT_MARGIN
);
283 #endif // __SMARTPHONE__/!__SMARTPHONE__
285 SetSizerAndFit(sizerTop
);
287 Centre(wxCENTER_FRAME
| wxBOTH
);
289 DisableOtherWindows();
294 // this one can be initialized even if the others are unknown for now
296 // NB: do it after calling Layout() to keep the labels correctly aligned
299 SetTimeLabel(0, m_elapsed
);
305 void wxGenericProgressDialog
::UpdateTimeEstimates(int value
,
306 unsigned long &elapsedTime
,
307 unsigned long &estimatedTime
,
308 unsigned long &remainingTime
)
310 unsigned long elapsed
= wxGetCurrentTime() - m_timeStart
;
311 if ( value
!= 0 && (m_last_timeupdate
< elapsed
|| value
== m_maximum
) )
313 m_last_timeupdate
= elapsed
;
314 unsigned long estimated
= m_break
+
315 (unsigned long)(( (double) (elapsed
-m_break
) * m_maximum
) / ((double)value
)) ;
316 if ( estimated
> m_display_estimated
322 else if ( estimated
< m_display_estimated
332 if ( m_ctdelay
>= m_delay
// enough confirmations for a higher value
333 || m_ctdelay
<= (m_delay
*-1) // enough confirmations for a lower value
334 || value
== m_maximum
// to stay consistent
335 || elapsed
> m_display_estimated
// to stay consistent
336 || ( elapsed
> 0 && elapsed
< 4 ) // additional updates in the beginning
339 m_display_estimated
= estimated
;
346 long display_remaining
= m_display_estimated
- elapsed
;
347 if ( display_remaining
< 0 )
349 display_remaining
= 0;
352 estimatedTime
= m_display_estimated
;
353 remainingTime
= display_remaining
;
356 elapsedTime
= elapsed
;
360 wxString wxGenericProgressDialog
::GetFormattedTime(unsigned long timeInSec
)
364 if ( timeInSec
== (unsigned long)-1 )
366 timeAsHMS
= _("Unknown");
370 unsigned hours
= timeInSec
/ 3600;
371 unsigned minutes
= (timeInSec
% 3600) / 60;
372 unsigned seconds
= timeInSec
% 60;
373 timeAsHMS
.Printf("%u:%02u:%02u", hours
, minutes
, seconds
);
380 wxGenericProgressDialog
::CreateLabel(const wxString
& text
, wxSizer
*sizer
)
382 wxStaticText
*label
= new wxStaticText(this, wxID_ANY
, text
);
383 wxStaticText
*value
= new wxStaticText(this, wxID_ANY
, _("unknown"));
385 // select placement most native or nice on target GUI
386 #if defined(__SMARTPHONE__)
387 // value and time to the left in two rows
388 sizer
->Add(label
, 1, wxALIGN_LEFT
);
389 sizer
->Add(value
, 1, wxALIGN_LEFT
);
390 #elif defined(__WXMSW__) || defined(__WXPM__) || defined(__WXMAC__) || defined(__WXGTK20__)
391 // value and time centered in one row
392 sizer
->Add(label
, 1, wxLARGESMALL(wxALIGN_RIGHT
,wxALIGN_LEFT
) | wxTOP
| wxRIGHT
, LAYOUT_MARGIN
);
393 sizer
->Add(value
, 1, wxALIGN_LEFT
| wxTOP
, LAYOUT_MARGIN
);
395 // value and time to the right in one row
397 sizer
->Add(value
, 0, wxLEFT
, LAYOUT_MARGIN
);
403 // ----------------------------------------------------------------------------
404 // wxGenericProgressDialog operations
405 // ----------------------------------------------------------------------------
408 wxGenericProgressDialog
::Update(int value
, const wxString
& newmsg
, bool *skip
)
410 if ( !DoBeforeUpdate(skip
) )
413 wxASSERT_MSG( value
== -1 || m_gauge
, wxT("cannot update non existent dialog") );
419 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( !HasFlag(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 wxCHECK_MSG(wxEventLoopBase
::GetActive(), false,
470 "wxGenericProgressDialog::Update needs a running event loop");
472 // allow the window to repaint:
473 // NOTE: since we yield only for UI events with this call, there
474 // should be no side-effects
475 wxEventLoopBase
::GetActive()->YieldFor(wxEVT_CATEGORY_UI
);
477 // NOTE: this call results in a new event loop being created
478 // and to a call to ProcessPendingEvents() (which may generate
479 // unwanted re-entrancies).
484 // reenable other windows before hiding this one because otherwise
485 // Windows wouldn't give the focus back to the window which had
486 // been previously focused because it would still be disabled
487 ReenableOtherWindows();
492 else // not at maximum yet
497 // update the display in case yielding above didn't do it
500 return m_state
!= Canceled
;
503 bool wxGenericProgressDialog
::Pulse(const wxString
& newmsg
, bool *skip
)
505 if ( !DoBeforeUpdate(skip
) )
508 wxASSERT_MSG( m_gauge
, wxT("cannot update non existent dialog") );
510 // show a bit of progress
513 UpdateMessage(newmsg
);
515 if (m_elapsed
|| m_remaining
|| m_estimated
)
517 unsigned long elapsed
= wxGetCurrentTime() - m_timeStart
;
519 SetTimeLabel(elapsed
, m_elapsed
);
520 SetTimeLabel((unsigned long)-1, m_estimated
);
521 SetTimeLabel((unsigned long)-1, m_remaining
);
526 return m_state
!= Canceled
;
529 bool wxGenericProgressDialog
::DoBeforeUpdate(bool *skip
)
531 wxCHECK_MSG(wxEventLoopBase
::GetActive(), false,
532 "wxGenericProgressDialog::DoBeforeUpdate needs a running event loop");
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 wxCHECK_RET(wxEventLoopBase
::GetActive(),
555 "wxGenericProgressDialog::DoAfterUpdate needs a running event loop");
557 // allow the window to repaint:
558 // NOTE: since we yield only for UI events with this call, there
559 // should be no side-effects
560 wxEventLoopBase
::GetActive()->YieldFor(wxEVT_CATEGORY_UI
);
563 void wxGenericProgressDialog
::Resume()
566 m_ctdelay
= m_delay
; // force an update of the elapsed/estimated/remaining time
567 m_break
+= wxGetCurrentTime()-m_timeStop
;
574 bool wxGenericProgressDialog
::Show( bool show
)
576 // reenable other windows before hiding this one because otherwise
577 // Windows wouldn't give the focus back to the window which had
578 // been previously focused because it would still be disabled
580 ReenableOtherWindows();
582 return wxDialog
::Show(show
);
585 int wxGenericProgressDialog
::GetValue() const
588 return m_gauge
->GetValue();
592 int wxGenericProgressDialog
::GetRange() const
595 return m_gauge
->GetRange();
599 wxString wxGenericProgressDialog
::GetMessage() const
601 return m_msg
->GetLabel();
604 void wxGenericProgressDialog
::SetRange(int maximum
)
606 wxASSERT_MSG(m_gauge
, "The dialog should have been constructed with a range > 0");
607 wxASSERT_MSG(maximum
> 0, "Invalid range");
609 m_gauge
->SetRange(maximum
);
612 #if defined(__WXMSW__) || defined(__WXPM__)
613 // we can't have values > 65,536 in the progress control under Windows, so
614 // scale everything down
615 m_factor
= m_maximum
/ 65536 + 1;
616 m_maximum
/= m_factor
;
621 bool wxGenericProgressDialog
::WasCancelled() const
623 return m_hasAbortButton
&& m_state
== Canceled
;
626 bool wxGenericProgressDialog
::WasSkipped() const
628 return m_hasSkipButton
&& 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();
720 void wxGenericProgressDialog
::DisableOtherWindows()
722 if ( HasFlag(wxPD_APP_MODAL
) )
724 m_winDisabler
= new wxWindowDisabler(this);
729 m_parentTop
->Disable();
730 m_winDisabler
= NULL
;
734 void wxGenericProgressDialog
::ReenableOtherWindows()
736 if ( HasFlag(wxPD_APP_MODAL
) )
738 wxDELETE(m_winDisabler
);
743 m_parentTop
->Enable();
747 // ----------------------------------------------------------------------------
749 // ----------------------------------------------------------------------------
751 void wxGenericProgressDialog
::EnableSkip(bool enable
)
755 #ifdef __SMARTPHONE__
757 SetRightMenu(wxID_SKIP
, _("Skip"));
762 m_btnSkip
->Enable(enable
);
767 void wxGenericProgressDialog
::EnableAbort(bool enable
)
771 #ifdef __SMARTPHONE__
773 SetLeftMenu(wxID_CANCEL
); // stock buttons makes Cancel label
778 m_btnAbort
->Enable(enable
);
783 void wxGenericProgressDialog
::EnableClose()
787 #ifdef __SMARTPHONE__
788 SetLeftMenu(wxID_CANCEL
, _("Close"));
792 m_btnAbort
->Enable();
793 m_btnAbort
->SetLabel(_("Close"));
799 void wxGenericProgressDialog
::UpdateMessage(const wxString
&newmsg
)
801 wxCHECK_RET(wxEventLoopBase
::GetActive(),
802 "wxGenericProgressDialog::UpdateMessage needs a running event loop");
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