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;
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 m_hasAbortButton
= (style
& wxPD_CAN_ABORT
) != 0;
162 m_hasSkipButton
= (style
& wxPD_CAN_SKIP
) != 0;
164 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
165 // we have to remove the "Close" button from the title bar then as it is
166 // confusing to have it - it doesn't work anyhow
168 // FIXME: should probably have a (extended?) window style for this
169 if ( !m_hasAbortButton
)
171 EnableCloseButton(false);
175 #if defined(__SMARTPHONE__)
179 m_state
= m_hasAbortButton
? Continue
: Uncancelable
;
181 // top-level sizerTop
182 wxSizer
* const sizerTop
= new wxBoxSizer(wxVERTICAL
);
184 m_msg
= new wxStaticText(this, wxID_ANY
, message
);
185 sizerTop
->Add(m_msg
, 0, wxLEFT
| wxTOP
, 2*LAYOUT_MARGIN
);
189 int gauge_style
= wxGA_HORIZONTAL
;
190 if ( style
& wxPD_SMOOTH
)
191 gauge_style
|= wxGA_SMOOTH
;
192 m_gauge
= new wxGauge
198 // make the progress bar sufficiently long
199 wxSize(wxMin(wxGetClientDisplayRect().width
/3, 300), -1),
203 sizerTop
->Add(m_gauge
, 0, wxLEFT
| wxRIGHT
| wxTOP
| wxEXPAND
, 2*LAYOUT_MARGIN
);
204 m_gauge
->SetValue(0);
211 // create the estimated/remaining/total time zones if requested
216 // also count how many labels we really have
217 size_t nTimeLabels
= 0;
219 wxSizer
* const sizerLabels
= new wxFlexGridSizer(2);
221 if ( style
& wxPD_ELAPSED_TIME
)
225 m_elapsed
= CreateLabel(GetElapsedLabel(), sizerLabels
);
228 if ( style
& wxPD_ESTIMATED_TIME
)
232 m_estimated
= CreateLabel(GetEstimatedLabel(), sizerLabels
);
235 if ( style
& wxPD_REMAINING_TIME
)
239 m_remaining
= CreateLabel(GetRemainingLabel(), sizerLabels
);
241 sizerTop
->Add(sizerLabels
, 0, wxALIGN_CENTER_HORIZONTAL
| wxTOP
, LAYOUT_MARGIN
);
243 #if defined(__SMARTPHONE__)
244 if ( m_hasSkipButton
)
245 SetRightMenu(wxID_SKIP
, _("Skip"));
246 if ( m_hasAbortButton
)
247 SetLeftMenu(wxID_CANCEL
);
252 wxBoxSizer
*buttonSizer
= new wxBoxSizer(wxHORIZONTAL
);
254 // Windows dialogs usually have buttons in the lower right corner
255 const int sizerFlags
=
256 #if defined(__WXMSW__) || defined(__WXPM__)
257 wxALIGN_RIGHT
| wxALL
259 wxALIGN_CENTER_HORIZONTAL
| wxBOTTOM
| wxTOP
263 if ( m_hasSkipButton
)
265 m_btnSkip
= new wxButton(this, wxID_SKIP
, _("&Skip"));
267 buttonSizer
->Add(m_btnSkip
, 0, sizerFlags
, LAYOUT_MARGIN
);
270 if ( m_hasAbortButton
)
272 m_btnAbort
= new wxButton(this, wxID_CANCEL
);
274 buttonSizer
->Add(m_btnAbort
, 0, sizerFlags
, LAYOUT_MARGIN
);
277 if (!m_hasSkipButton
&& !m_hasAbortButton
)
278 buttonSizer
->AddSpacer(LAYOUT_MARGIN
);
280 sizerTop
->Add(buttonSizer
, 0, sizerFlags
, LAYOUT_MARGIN
);
281 #endif // __SMARTPHONE__/!__SMARTPHONE__
283 SetSizerAndFit(sizerTop
);
285 Centre(wxCENTER_FRAME
| wxBOTH
);
287 DisableOtherWindows();
292 // this one can be initialized even if the others are unknown for now
294 // NB: do it after calling Layout() to keep the labels correctly aligned
297 SetTimeLabel(0, m_elapsed
);
303 void wxGenericProgressDialog::UpdateTimeEstimates(int value
,
304 unsigned long &elapsedTime
,
305 unsigned long &estimatedTime
,
306 unsigned long &remainingTime
)
308 unsigned long elapsed
= wxGetCurrentTime() - m_timeStart
;
309 if ( value
!= 0 && (m_last_timeupdate
< elapsed
|| value
== m_maximum
) )
311 m_last_timeupdate
= elapsed
;
312 unsigned long estimated
= m_break
+
313 (unsigned long)(( (double) (elapsed
-m_break
) * m_maximum
) / ((double)value
)) ;
314 if ( estimated
> m_display_estimated
320 else if ( estimated
< m_display_estimated
330 if ( m_ctdelay
>= m_delay
// enough confirmations for a higher value
331 || m_ctdelay
<= (m_delay
*-1) // enough confirmations for a lower value
332 || value
== m_maximum
// to stay consistent
333 || elapsed
> m_display_estimated
// to stay consistent
334 || ( elapsed
> 0 && elapsed
< 4 ) // additional updates in the beginning
337 m_display_estimated
= estimated
;
344 long display_remaining
= m_display_estimated
- elapsed
;
345 if ( display_remaining
< 0 )
347 display_remaining
= 0;
350 estimatedTime
= m_display_estimated
;
351 remainingTime
= display_remaining
;
354 elapsedTime
= elapsed
;
358 wxString
wxGenericProgressDialog::GetFormattedTime(unsigned long timeInSec
)
362 if ( timeInSec
== (unsigned long)-1 )
364 timeAsHMS
= _("Unknown");
368 unsigned hours
= timeInSec
/ 3600;
369 unsigned minutes
= (timeInSec
% 3600) / 60;
370 unsigned seconds
= timeInSec
% 60;
371 timeAsHMS
.Printf("%u:%02u:%02u", hours
, minutes
, seconds
);
378 wxGenericProgressDialog::CreateLabel(const wxString
& text
, wxSizer
*sizer
)
380 wxStaticText
*label
= new wxStaticText(this, wxID_ANY
, text
);
381 wxStaticText
*value
= new wxStaticText(this, wxID_ANY
, _("unknown"));
383 // select placement most native or nice on target GUI
384 #if defined(__SMARTPHONE__)
385 // value and time to the left in two rows
386 sizer
->Add(label
, 1, wxALIGN_LEFT
);
387 sizer
->Add(value
, 1, wxALIGN_LEFT
);
388 #elif defined(__WXMSW__) || defined(__WXPM__) || defined(__WXMAC__) || defined(__WXGTK20__)
389 // value and time centered in one row
390 sizer
->Add(label
, 1, wxLARGESMALL(wxALIGN_RIGHT
,wxALIGN_LEFT
) | wxTOP
| wxRIGHT
, LAYOUT_MARGIN
);
391 sizer
->Add(value
, 1, wxALIGN_LEFT
| wxTOP
, LAYOUT_MARGIN
);
393 // value and time to the right in one row
395 sizer
->Add(value
, 0, wxLEFT
, LAYOUT_MARGIN
);
401 // ----------------------------------------------------------------------------
402 // wxGenericProgressDialog operations
403 // ----------------------------------------------------------------------------
406 wxGenericProgressDialog::Update(int value
, const wxString
& newmsg
, bool *skip
)
408 if ( !DoBeforeUpdate(skip
) )
411 wxASSERT_MSG( value
== -1 || m_gauge
, wxT("cannot update non existent dialog") );
417 wxASSERT_MSG( value
<= m_maximum
, wxT("invalid progress value") );
420 m_gauge
->SetValue(value
);
422 UpdateMessage(newmsg
);
424 if ( (m_elapsed
|| m_remaining
|| m_estimated
) && (value
!= 0) )
426 unsigned long elapsed
;
427 unsigned long display_remaining
;
429 UpdateTimeEstimates( value
,
434 SetTimeLabel(elapsed
, m_elapsed
);
435 SetTimeLabel(m_display_estimated
, m_estimated
);
436 SetTimeLabel(display_remaining
, m_remaining
);
439 if ( value
== m_maximum
)
441 if ( m_state
== Finished
)
443 // ignore multiple calls to Update(m_maximum): it may sometimes be
444 // troublesome to ensure that Update() is not called twice with the
445 // same value (e.g. because of the rounding errors) and if we don't
446 // return now we're going to generate asserts below
450 // so that we return true below and that out [Cancel] handler knew what
453 if( !HasFlag(wxPD_AUTO_HIDE
) )
457 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
461 if ( newmsg
.empty() )
463 // also provide the finishing message if the application didn't
464 m_msg
->SetLabel(_("Done."));
467 wxCHECK_MSG(wxEventLoopBase::GetActive(), false,
468 "wxGenericProgressDialog::Update needs a running event loop");
470 // allow the window to repaint:
471 // NOTE: since we yield only for UI events with this call, there
472 // should be no side-effects
473 wxEventLoopBase::GetActive()->YieldFor(wxEVT_CATEGORY_UI
);
475 // NOTE: this call results in a new event loop being created
476 // and to a call to ProcessPendingEvents() (which may generate
477 // unwanted re-entrancies).
482 // reenable other windows before hiding this one because otherwise
483 // Windows wouldn't give the focus back to the window which had
484 // been previously focused because it would still be disabled
485 ReenableOtherWindows();
490 else // not at maximum yet
495 // update the display in case yielding above didn't do it
498 return m_state
!= Canceled
;
501 bool wxGenericProgressDialog::Pulse(const wxString
& newmsg
, bool *skip
)
503 if ( !DoBeforeUpdate(skip
) )
506 wxASSERT_MSG( m_gauge
, wxT("cannot update non existent dialog") );
508 // show a bit of progress
511 UpdateMessage(newmsg
);
513 if (m_elapsed
|| m_remaining
|| m_estimated
)
515 unsigned long elapsed
= wxGetCurrentTime() - m_timeStart
;
517 SetTimeLabel(elapsed
, m_elapsed
);
518 SetTimeLabel((unsigned long)-1, m_estimated
);
519 SetTimeLabel((unsigned long)-1, m_remaining
);
524 return m_state
!= Canceled
;
527 bool wxGenericProgressDialog::DoBeforeUpdate(bool *skip
)
529 wxCHECK_MSG(wxEventLoopBase::GetActive(), false,
530 "wxGenericProgressDialog::DoBeforeUpdate needs a running event loop");
532 // we have to yield because not only we want to update the display but
533 // also to process the clicks on the cancel and skip buttons
534 // NOTE: using YieldFor() this call shouldn't give re-entrancy problems
535 // for event handlers not interested to UI/user-input events.
536 wxEventLoopBase::GetActive()->YieldFor(wxEVT_CATEGORY_UI
|wxEVT_CATEGORY_USER_INPUT
);
540 if ( m_skip
&& skip
&& !*skip
)
547 return m_state
!= Canceled
;
550 void wxGenericProgressDialog::DoAfterUpdate()
552 wxCHECK_RET(wxEventLoopBase::GetActive(),
553 "wxGenericProgressDialog::DoAfterUpdate needs a running event loop");
555 // allow the window to repaint:
556 // NOTE: since we yield only for UI events with this call, there
557 // should be no side-effects
558 wxEventLoopBase::GetActive()->YieldFor(wxEVT_CATEGORY_UI
);
561 void wxGenericProgressDialog::Resume()
564 m_ctdelay
= m_delay
; // force an update of the elapsed/estimated/remaining time
565 m_break
+= wxGetCurrentTime()-m_timeStop
;
572 bool wxGenericProgressDialog::Show( bool show
)
574 // reenable other windows before hiding this one because otherwise
575 // Windows wouldn't give the focus back to the window which had
576 // been previously focused because it would still be disabled
578 ReenableOtherWindows();
580 return wxDialog::Show(show
);
583 int wxGenericProgressDialog::GetValue() const
586 return m_gauge
->GetValue();
590 int wxGenericProgressDialog::GetRange() const
593 return m_gauge
->GetRange();
597 wxString
wxGenericProgressDialog::GetMessage() const
599 return m_msg
->GetLabel();
602 void wxGenericProgressDialog::SetRange(int maximum
)
604 wxASSERT_MSG(m_gauge
, "The dialog should have been constructed with a range > 0");
605 wxASSERT_MSG(maximum
> 0, "Invalid range");
607 m_gauge
->SetRange(maximum
);
610 #if defined(__WXMSW__) || defined(__WXPM__)
611 // we can't have values > 65,536 in the progress control under Windows, so
612 // scale everything down
613 m_factor
= m_maximum
/ 65536 + 1;
614 m_maximum
/= m_factor
;
619 bool wxGenericProgressDialog::WasCancelled() const
621 return m_hasAbortButton
&& m_state
== Canceled
;
624 bool wxGenericProgressDialog::WasSkipped() const
626 return m_hasSkipButton
&& m_skip
;
630 void wxGenericProgressDialog::SetTimeLabel(unsigned long val
,
637 if (val
!= (unsigned long)-1)
639 s
= GetFormattedTime(val
);
646 if ( s
!= label
->GetLabel() )
651 // ----------------------------------------------------------------------------
653 // ----------------------------------------------------------------------------
655 void wxGenericProgressDialog::OnCancel(wxCommandEvent
& event
)
657 if ( m_state
== Finished
)
659 // this means that the count down is already finished and we're being
660 // shown as a modal dialog - so just let the default handler do the job
665 // request to cancel was received, the next time Update() is called we
669 // update the buttons state immediately so that the user knows that the
670 // request has been noticed
674 // save the time when the dialog was stopped
675 m_timeStop
= wxGetCurrentTime();
679 void wxGenericProgressDialog::OnSkip(wxCommandEvent
& WXUNUSED(event
))
685 void wxGenericProgressDialog::OnClose(wxCloseEvent
& event
)
687 if ( m_state
== Uncancelable
)
689 // can't close this dialog
692 else if ( m_state
== Finished
)
694 // let the default handler close the window as we already terminated
699 // next Update() will notice it
704 m_timeStop
= wxGetCurrentTime();
708 // ----------------------------------------------------------------------------
710 // ----------------------------------------------------------------------------
712 wxGenericProgressDialog::~wxGenericProgressDialog()
714 // normally this should have been already done, but just in case
715 ReenableOtherWindows();
718 void wxGenericProgressDialog::DisableOtherWindows()
720 if ( HasFlag(wxPD_APP_MODAL
) )
722 m_winDisabler
= new wxWindowDisabler(this);
727 m_parentTop
->Disable();
728 m_winDisabler
= NULL
;
732 void wxGenericProgressDialog::ReenableOtherWindows()
734 if ( HasFlag(wxPD_APP_MODAL
) )
736 wxDELETE(m_winDisabler
);
741 m_parentTop
->Enable();
745 // ----------------------------------------------------------------------------
747 // ----------------------------------------------------------------------------
749 void wxGenericProgressDialog::EnableSkip(bool enable
)
753 #ifdef __SMARTPHONE__
755 SetRightMenu(wxID_SKIP
, _("Skip"));
760 m_btnSkip
->Enable(enable
);
765 void wxGenericProgressDialog::EnableAbort(bool enable
)
769 #ifdef __SMARTPHONE__
771 SetLeftMenu(wxID_CANCEL
); // stock buttons makes Cancel label
776 m_btnAbort
->Enable(enable
);
781 void wxGenericProgressDialog::EnableClose()
785 #ifdef __SMARTPHONE__
786 SetLeftMenu(wxID_CANCEL
, _("Close"));
790 m_btnAbort
->Enable();
791 m_btnAbort
->SetLabel(_("Close"));
797 void wxGenericProgressDialog::UpdateMessage(const wxString
&newmsg
)
799 wxCHECK_RET(wxEventLoopBase::GetActive(),
800 "wxGenericProgressDialog::UpdateMessage needs a running event loop");
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