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()
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).
107 m_state
= Uncancelable
;
110 m_timeStart
= wxGetCurrentTime();
111 m_timeStop
= (unsigned long)-1;
116 #if !defined(__SMARTPHONE__)
121 m_display_estimated
=
127 m_winDisabler
= NULL
;
128 m_tempEventLoop
= NULL
;
131 wxGenericProgressDialog::wxGenericProgressDialog()
137 wxGenericProgressDialog::wxGenericProgressDialog(const wxString
& title
,
138 const wxString
& message
,
146 Create( title
, message
, maximum
, parent
, style
);
149 bool wxGenericProgressDialog::Create( const wxString
& title
,
150 const wxString
& message
,
155 m_parentTop
= wxGetTopLevelParent(parent
);
159 realParent
= GetParentForModalDialog(parent
, GetWindowStyle());
161 if (!wxDialog::Create(realParent
, wxID_ANY
, title
))
166 // We need a running event loop in order to update the dialog and be able
167 // to process clicks on its buttons, so ensure that there is one running
168 // even if this means we have to start it ourselves (this happens most
169 // commonly during the program initialization, e.g. for the progress
170 // dialogs shown from overridden wxApp::OnInit()).
171 if ( !wxEventLoopBase::GetActive() )
173 m_tempEventLoop
= new wxEventLoop
;
174 wxEventLoop::SetActive(m_tempEventLoop
);
177 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
178 // we have to remove the "Close" button from the title bar then as it is
179 // confusing to have it - it doesn't work anyhow
181 // FIXME: should probably have a (extended?) window style for this
182 if ( !HasPDFlag(wxPD_CAN_ABORT
) )
184 EnableCloseButton(false);
188 #if defined(__SMARTPHONE__)
192 m_state
= HasPDFlag(wxPD_CAN_ABORT
) ? Continue
: Uncancelable
;
194 // top-level sizerTop
195 wxSizer
* const sizerTop
= new wxBoxSizer(wxVERTICAL
);
197 m_msg
= new wxStaticText(this, wxID_ANY
, message
);
198 sizerTop
->Add(m_msg
, 0, wxLEFT
| wxTOP
, 2*LAYOUT_MARGIN
);
200 int gauge_style
= wxGA_HORIZONTAL
;
201 if ( style
& wxPD_SMOOTH
)
202 gauge_style
|= wxGA_SMOOTH
;
208 m_gauge
= new wxGauge
214 // make the progress bar sufficiently long
215 wxSize(wxMin(wxGetClientDisplayRect().width
/3, 300), -1),
219 sizerTop
->Add(m_gauge
, 0, wxLEFT
| wxRIGHT
| wxTOP
| wxEXPAND
, 2*LAYOUT_MARGIN
);
220 m_gauge
->SetValue(0);
222 // create the estimated/remaining/total time zones if requested
227 // also count how many labels we really have
228 size_t nTimeLabels
= 0;
230 wxSizer
* const sizerLabels
= new wxFlexGridSizer(2);
232 if ( style
& wxPD_ELAPSED_TIME
)
236 m_elapsed
= CreateLabel(GetElapsedLabel(), sizerLabels
);
239 if ( style
& wxPD_ESTIMATED_TIME
)
243 m_estimated
= CreateLabel(GetEstimatedLabel(), sizerLabels
);
246 if ( style
& wxPD_REMAINING_TIME
)
250 m_remaining
= CreateLabel(GetRemainingLabel(), sizerLabels
);
252 sizerTop
->Add(sizerLabels
, 0, wxALIGN_CENTER_HORIZONTAL
| wxTOP
, LAYOUT_MARGIN
);
254 #if defined(__SMARTPHONE__)
255 if ( HasPDFlag(wxPD_CAN_SKIP
) )
256 SetRightMenu(wxID_SKIP
, _("Skip"));
257 if ( HasPDFlag(wxPD_CAN_ABORT
) )
258 SetLeftMenu(wxID_CANCEL
);
263 wxBoxSizer
*buttonSizer
= new wxBoxSizer(wxHORIZONTAL
);
265 // Windows dialogs usually have buttons in the lower right corner
266 const int sizerFlags
=
267 #if defined(__WXMSW__) || defined(__WXPM__) || defined(__WXOSX__)
268 wxALIGN_RIGHT
| wxALL
270 wxALIGN_CENTER_HORIZONTAL
| wxBOTTOM
| wxTOP
274 if ( HasPDFlag(wxPD_CAN_SKIP
) )
276 m_btnSkip
= new wxButton(this, wxID_SKIP
, _("&Skip"));
278 buttonSizer
->Add(m_btnSkip
, 0, sizerFlags
, LAYOUT_MARGIN
);
281 if ( HasPDFlag(wxPD_CAN_ABORT
) )
283 m_btnAbort
= new wxButton(this, wxID_CANCEL
);
285 buttonSizer
->Add(m_btnAbort
, 0, sizerFlags
, LAYOUT_MARGIN
);
288 if ( !HasPDFlag(wxPD_CAN_SKIP
| wxPD_CAN_ABORT
) )
289 buttonSizer
->AddSpacer(LAYOUT_MARGIN
);
291 sizerTop
->Add(buttonSizer
, 0, sizerFlags
, LAYOUT_MARGIN
);
292 #endif // __SMARTPHONE__/!__SMARTPHONE__
294 SetSizerAndFit(sizerTop
);
296 Centre(wxCENTER_FRAME
| wxBOTH
);
298 DisableOtherWindows();
303 // this one can be initialized even if the others are unknown for now
305 // NB: do it after calling Layout() to keep the labels correctly aligned
308 SetTimeLabel(0, m_elapsed
);
315 void wxGenericProgressDialog::UpdateTimeEstimates(int value
,
316 unsigned long &elapsedTime
,
317 unsigned long &estimatedTime
,
318 unsigned long &remainingTime
)
320 unsigned long elapsed
= wxGetCurrentTime() - m_timeStart
;
321 if ( value
!= 0 && (m_last_timeupdate
< elapsed
|| value
== m_maximum
) )
323 m_last_timeupdate
= elapsed
;
324 unsigned long estimated
= m_break
+
325 (unsigned long)(( (double) (elapsed
-m_break
) * m_maximum
) / ((double)value
)) ;
326 if ( estimated
> m_display_estimated
332 else if ( estimated
< m_display_estimated
342 if ( m_ctdelay
>= m_delay
// enough confirmations for a higher value
343 || m_ctdelay
<= (m_delay
*-1) // enough confirmations for a lower value
344 || value
== m_maximum
// to stay consistent
345 || elapsed
> m_display_estimated
// to stay consistent
346 || ( elapsed
> 0 && elapsed
< 4 ) // additional updates in the beginning
349 m_display_estimated
= estimated
;
356 long display_remaining
= m_display_estimated
- elapsed
;
357 if ( display_remaining
< 0 )
359 display_remaining
= 0;
362 estimatedTime
= m_display_estimated
;
363 remainingTime
= display_remaining
;
366 elapsedTime
= elapsed
;
370 wxString
wxGenericProgressDialog::GetFormattedTime(unsigned long timeInSec
)
374 if ( timeInSec
== (unsigned long)-1 )
376 timeAsHMS
= _("Unknown");
380 unsigned hours
= timeInSec
/ 3600;
381 unsigned minutes
= (timeInSec
% 3600) / 60;
382 unsigned seconds
= timeInSec
% 60;
383 timeAsHMS
.Printf("%u:%02u:%02u", hours
, minutes
, seconds
);
390 wxGenericProgressDialog::CreateLabel(const wxString
& text
, wxSizer
*sizer
)
392 wxStaticText
*label
= new wxStaticText(this, wxID_ANY
, text
);
393 wxStaticText
*value
= new wxStaticText(this, wxID_ANY
, _("unknown"));
395 // select placement most native or nice on target GUI
396 #if defined(__SMARTPHONE__)
397 // value and time to the left in two rows
398 sizer
->Add(label
, 1, wxALIGN_LEFT
);
399 sizer
->Add(value
, 1, wxALIGN_LEFT
);
400 #elif defined(__WXMSW__) || defined(__WXPM__) || defined(__WXMAC__) || defined(__WXGTK20__)
401 // value and time centered in one row
402 sizer
->Add(label
, 1, wxLARGESMALL(wxALIGN_RIGHT
,wxALIGN_LEFT
) | wxTOP
| wxRIGHT
, LAYOUT_MARGIN
);
403 sizer
->Add(value
, 1, wxALIGN_LEFT
| wxTOP
, LAYOUT_MARGIN
);
405 // value and time to the right in one row
407 sizer
->Add(value
, 0, wxLEFT
, LAYOUT_MARGIN
);
413 // ----------------------------------------------------------------------------
414 // wxGenericProgressDialog operations
415 // ----------------------------------------------------------------------------
418 wxGenericProgressDialog::Update(int value
, const wxString
& newmsg
, bool *skip
)
420 if ( !DoBeforeUpdate(skip
) )
423 wxCHECK_MSG( m_gauge
, false, "dialog should be fully created" );
429 wxASSERT_MSG( value
<= m_maximum
, wxT("invalid progress value") );
431 m_gauge
->SetValue(value
);
433 UpdateMessage(newmsg
);
435 if ( (m_elapsed
|| m_remaining
|| m_estimated
) && (value
!= 0) )
437 unsigned long elapsed
;
438 unsigned long display_remaining
;
440 UpdateTimeEstimates( value
,
445 SetTimeLabel(elapsed
, m_elapsed
);
446 SetTimeLabel(m_display_estimated
, m_estimated
);
447 SetTimeLabel(display_remaining
, m_remaining
);
450 if ( value
== m_maximum
)
452 if ( m_state
== Finished
)
454 // ignore multiple calls to Update(m_maximum): it may sometimes be
455 // troublesome to ensure that Update() is not called twice with the
456 // same value (e.g. because of the rounding errors) and if we don't
457 // return now we're going to generate asserts below
461 // so that we return true below and that out [Cancel] handler knew what
464 if( !HasPDFlag(wxPD_AUTO_HIDE
) )
468 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
472 if ( newmsg
.empty() )
474 // also provide the finishing message if the application didn't
475 m_msg
->SetLabel(_("Done."));
478 // allow the window to repaint:
479 // NOTE: since we yield only for UI events with this call, there
480 // should be no side-effects
481 wxEventLoopBase::GetActive()->YieldFor(wxEVT_CATEGORY_UI
);
483 // NOTE: this call results in a new event loop being created
484 // and to a call to ProcessPendingEvents() (which may generate
485 // unwanted re-entrancies).
490 // reenable other windows before hiding this one because otherwise
491 // Windows wouldn't give the focus back to the window which had
492 // been previously focused because it would still be disabled
493 ReenableOtherWindows();
498 else // not at maximum yet
503 // update the display in case yielding above didn't do it
506 return m_state
!= Canceled
;
509 bool wxGenericProgressDialog::Pulse(const wxString
& newmsg
, bool *skip
)
511 if ( !DoBeforeUpdate(skip
) )
514 wxCHECK_MSG( m_gauge
, false, "dialog should be fully created" );
516 // show a bit of progress
519 UpdateMessage(newmsg
);
521 if (m_elapsed
|| m_remaining
|| m_estimated
)
523 unsigned long elapsed
= wxGetCurrentTime() - m_timeStart
;
525 SetTimeLabel(elapsed
, m_elapsed
);
526 SetTimeLabel((unsigned long)-1, m_estimated
);
527 SetTimeLabel((unsigned long)-1, m_remaining
);
532 return m_state
!= Canceled
;
535 bool wxGenericProgressDialog::DoBeforeUpdate(bool *skip
)
537 // we have to yield because not only we want to update the display but
538 // also to process the clicks on the cancel and skip buttons
539 // NOTE: using YieldFor() this call shouldn't give re-entrancy problems
540 // for event handlers not interested to UI/user-input events.
541 wxEventLoopBase::GetActive()->YieldFor(wxEVT_CATEGORY_UI
|wxEVT_CATEGORY_USER_INPUT
);
545 if ( m_skip
&& skip
&& !*skip
)
552 return m_state
!= Canceled
;
555 void wxGenericProgressDialog::DoAfterUpdate()
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
587 wxCHECK_MSG( m_gauge
, -1, "dialog should be fully created" );
589 return m_gauge
->GetValue();
592 int wxGenericProgressDialog::GetRange() const
597 wxString
wxGenericProgressDialog::GetMessage() const
599 return m_msg
->GetLabel();
602 void wxGenericProgressDialog::SetRange(int maximum
)
604 wxCHECK_RET( m_gauge
, "dialog should be fully created" );
606 wxCHECK_RET( maximum
> 0, "Invalid range" );
608 m_gauge
->SetRange(maximum
);
613 void wxGenericProgressDialog::SetMaximum(int maximum
)
617 #if defined(__WXMSW__) || defined(__WXPM__)
618 // we can't have values > 65,536 in the progress control under Windows, so
619 // scale everything down
620 m_factor
= m_maximum
/ 65536 + 1;
625 bool wxGenericProgressDialog::WasCancelled() const
627 return HasPDFlag(wxPD_CAN_ABORT
) && m_state
== Canceled
;
630 bool wxGenericProgressDialog::WasSkipped() const
632 return HasPDFlag(wxPD_CAN_SKIP
) && m_skip
;
636 void wxGenericProgressDialog::SetTimeLabel(unsigned long val
,
643 if (val
!= (unsigned long)-1)
645 s
= GetFormattedTime(val
);
652 if ( s
!= label
->GetLabel() )
657 // ----------------------------------------------------------------------------
659 // ----------------------------------------------------------------------------
661 void wxGenericProgressDialog::OnCancel(wxCommandEvent
& event
)
663 if ( m_state
== Finished
)
665 // this means that the count down is already finished and we're being
666 // shown as a modal dialog - so just let the default handler do the job
671 // request to cancel was received, the next time Update() is called we
675 // update the buttons state immediately so that the user knows that the
676 // request has been noticed
680 // save the time when the dialog was stopped
681 m_timeStop
= wxGetCurrentTime();
685 void wxGenericProgressDialog::OnSkip(wxCommandEvent
& WXUNUSED(event
))
691 void wxGenericProgressDialog::OnClose(wxCloseEvent
& event
)
693 if ( m_state
== Uncancelable
)
695 // can't close this dialog
698 else if ( m_state
== Finished
)
700 // let the default handler close the window as we already terminated
705 // next Update() will notice it
710 m_timeStop
= wxGetCurrentTime();
714 // ----------------------------------------------------------------------------
716 // ----------------------------------------------------------------------------
718 wxGenericProgressDialog::~wxGenericProgressDialog()
720 // normally this should have been already done, but just in case
721 ReenableOtherWindows();
723 if ( m_tempEventLoop
)
725 wxEventLoopBase::SetActive(NULL
);
726 delete m_tempEventLoop
;
730 void wxGenericProgressDialog::DisableOtherWindows()
732 if ( HasPDFlag(wxPD_APP_MODAL
) )
734 m_winDisabler
= new wxWindowDisabler(this);
739 m_parentTop
->Disable();
740 m_winDisabler
= NULL
;
744 void wxGenericProgressDialog::ReenableOtherWindows()
746 if ( HasPDFlag(wxPD_APP_MODAL
) )
748 wxDELETE(m_winDisabler
);
753 m_parentTop
->Enable();
757 // ----------------------------------------------------------------------------
759 // ----------------------------------------------------------------------------
761 void wxGenericProgressDialog::EnableSkip(bool enable
)
763 if ( HasPDFlag(wxPD_CAN_SKIP
) )
765 #ifdef __SMARTPHONE__
767 SetRightMenu(wxID_SKIP
, _("Skip"));
772 m_btnSkip
->Enable(enable
);
777 void wxGenericProgressDialog::EnableAbort(bool enable
)
779 if( HasPDFlag(wxPD_CAN_ABORT
) )
781 #ifdef __SMARTPHONE__
783 SetLeftMenu(wxID_CANCEL
); // stock buttons makes Cancel label
788 m_btnAbort
->Enable(enable
);
793 void wxGenericProgressDialog::EnableClose()
795 if(HasPDFlag(wxPD_CAN_ABORT
))
797 #ifdef __SMARTPHONE__
798 SetLeftMenu(wxID_CANCEL
, _("Close"));
802 m_btnAbort
->Enable();
803 m_btnAbort
->SetLabel(_("Close"));
809 void wxGenericProgressDialog::UpdateMessage(const wxString
&newmsg
)
811 if ( !newmsg
.empty() && newmsg
!= m_msg
->GetLabel() )
813 m_msg
->SetLabel(newmsg
);
815 // allow the window to repaint:
816 // NOTE: since we yield only for UI events with this call, there
817 // should be no side-effects
818 wxEventLoopBase::GetActive()->YieldFor(wxEVT_CATEGORY_UI
);
822 #endif // wxUSE_PROGRESSDLG