1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/progdlgg.cpp
3 // Purpose: wxProgressDialog 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"
43 #include "wx/progdlg.h"
45 // ---------------------------------------------------------------------------
47 // ---------------------------------------------------------------------------
49 /* Macro for avoiding #ifdefs when value have to be different depending on size of
50 device we display on - take it from something like wxDesktopPolicy in the future
53 #if defined(__SMARTPHONE__)
54 #define wxLARGESMALL(large,small) small
56 #define wxLARGESMALL(large,small) large
59 // ----------------------------------------------------------------------------
61 // ----------------------------------------------------------------------------
63 #define LAYOUT_MARGIN wxLARGESMALL(8,2)
65 static const int wxID_SKIP
= 32000; // whatever
67 // ----------------------------------------------------------------------------
69 // ----------------------------------------------------------------------------
71 // update the label to show the given time (in seconds)
72 static void SetTimeLabel(unsigned long val
, wxStaticText
*label
);
74 // ----------------------------------------------------------------------------
76 // ----------------------------------------------------------------------------
78 BEGIN_EVENT_TABLE(wxProgressDialog
, wxDialog
)
79 EVT_BUTTON(wxID_CANCEL
, wxProgressDialog::OnCancel
)
80 EVT_BUTTON(wxID_SKIP
, wxProgressDialog::OnSkip
)
82 EVT_CLOSE(wxProgressDialog::OnClose
)
85 IMPLEMENT_CLASS(wxProgressDialog
, wxDialog
)
87 // ============================================================================
88 // wxProgressDialog implementation
89 // ============================================================================
91 // ----------------------------------------------------------------------------
92 // wxProgressDialog creation
93 // ----------------------------------------------------------------------------
95 wxProgressDialog::wxProgressDialog(const wxString
& title
,
96 const wxString
& message
,
100 : wxDialog(GetParentForModalDialog(parent
), wxID_ANY
, title
),
103 m_hasAbortButton(false),
104 m_hasSkipButton(false)
106 // we may disappear at any moment, let the others know about it
107 SetExtraStyle(GetExtraStyle() | wxWS_EX_TRANSIENT
);
108 m_windowStyle
|= style
;
110 m_hasAbortButton
= (style
& wxPD_CAN_ABORT
) != 0;
111 m_hasSkipButton
= (style
& wxPD_CAN_SKIP
) != 0;
113 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
114 // we have to remove the "Close" button from the title bar then as it is
115 // confusing to have it - it doesn't work anyhow
117 // FIXME: should probably have a (extended?) window style for this
118 if ( !m_hasAbortButton
)
120 EnableCloseButton(false);
124 #if defined(__SMARTPHONE__)
128 m_state
= m_hasAbortButton
? Continue
: Uncancelable
;
131 #if defined(__WXMSW__) || defined(__WXPM__)
132 // we can't have values > 65,536 in the progress control under Windows, so
133 // scale everything down
134 m_factor
= m_maximum
/ 65536 + 1;
135 m_maximum
/= m_factor
;
138 m_parentTop
= wxGetTopLevelParent(parent
);
141 dc
.SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
142 wxCoord widthText
= 0;
143 dc
.GetTextExtent(message
, &widthText
, NULL
, NULL
, NULL
, NULL
);
145 // top-level sizerTop
146 wxSizer
* const sizerTop
= new wxBoxSizer(wxVERTICAL
);
148 m_msg
= new wxStaticText(this, wxID_ANY
, message
);
149 sizerTop
->Add(m_msg
, 0, wxLEFT
| wxTOP
, 2*LAYOUT_MARGIN
);
151 wxSize sizeLabel
= m_msg
->GetSize();
154 int gauge_style
= wxGA_HORIZONTAL
;
155 if ( style
& wxPD_SMOOTH
)
156 gauge_style
|= wxGA_SMOOTH
;
157 m_gauge
= new wxGauge
163 // make the progress bar sufficiently long
164 wxSize(wxMin(wxGetClientDisplayRect().width
/3, 300), -1),
168 sizerTop
->Add(m_gauge
, 0, wxLEFT
| wxRIGHT
| wxTOP
| wxEXPAND
, 2*LAYOUT_MARGIN
);
169 m_gauge
->SetValue(0);
176 // create the estimated/remaining/total time zones if requested
180 m_display_estimated
=
185 // if we are going to have at least one label, remember it in this var
186 wxStaticText
*label
= NULL
;
188 // also count how many labels we really have
189 size_t nTimeLabels
= 0;
191 wxSizer
* const sizerLabels
= new wxFlexGridSizer(2);
193 if ( style
& wxPD_ELAPSED_TIME
)
198 m_elapsed
= CreateLabel(_("Elapsed time:"), sizerLabels
);
201 if ( style
& wxPD_ESTIMATED_TIME
)
206 m_estimated
= CreateLabel(_("Estimated time:"), sizerLabels
);
209 if ( style
& wxPD_REMAINING_TIME
)
214 m_remaining
= CreateLabel(_("Remaining time:"), sizerLabels
);
216 sizerTop
->Add(sizerLabels
, 0, wxALIGN_CENTER_HORIZONTAL
| wxTOP
, LAYOUT_MARGIN
);
218 if ( nTimeLabels
> 0 )
220 // set it to the current time
221 m_timeStart
= wxGetCurrentTime();
224 #if defined(__SMARTPHONE__)
225 if ( m_hasSkipButton
)
226 SetRightMenu(wxID_SKIP
, _("Skip"));
227 if ( m_hasAbortButton
)
228 SetLeftMenu(wxID_CANCEL
);
233 wxBoxSizer
*buttonSizer
= new wxBoxSizer(wxHORIZONTAL
);
235 // Windows dialogs usually have buttons in the lower right corner
236 const int sizerFlags
=
237 #if defined(__WXMSW__) || defined(__WXPM__)
238 wxALIGN_RIGHT
| wxALL
240 wxALIGN_CENTER_HORIZONTAL
| wxBOTTOM
| wxTOP
244 if ( m_hasSkipButton
)
246 m_btnSkip
= new wxButton(this, wxID_SKIP
, _("&Skip"));
248 buttonSizer
->Add(m_btnSkip
, 0, sizerFlags
, LAYOUT_MARGIN
);
251 if ( m_hasAbortButton
)
253 m_btnAbort
= new wxButton(this, wxID_CANCEL
);
255 buttonSizer
->Add(m_btnAbort
, 0, sizerFlags
, LAYOUT_MARGIN
);
258 sizerTop
->Add(buttonSizer
, 0, sizerFlags
, LAYOUT_MARGIN
);
259 #endif // __SMARTPHONE__/!__SMARTPHONE__
261 SetSizerAndFit(sizerTop
);
263 Centre(wxCENTER_FRAME
| wxBOTH
);
265 if ( style
& wxPD_APP_MODAL
)
267 m_winDisabler
= new wxWindowDisabler(this);
272 m_parentTop
->Disable();
273 m_winDisabler
= NULL
;
279 // this one can be initialized even if the others are unknown for now
281 // NB: do it after calling Layout() to keep the labels correctly aligned
284 SetTimeLabel(0, m_elapsed
);
291 wxProgressDialog::CreateLabel(const wxString
& text
, wxSizer
*sizer
)
293 wxStaticText
*label
= new wxStaticText(this, wxID_ANY
, text
);
294 wxStaticText
*value
= new wxStaticText(this, wxID_ANY
, _("unknown"));
296 // select placement most native or nice on target GUI
297 #if defined(__SMARTPHONE__)
298 // value and time to the left in two rows
299 sizer
->Add(label
, 1, wxALIGN_LEFT
);
300 sizer
->Add(value
, 1, wxALIGN_LEFT
);
301 #elif defined(__WXMSW__) || defined(__WXPM__) || defined(__WXMAC__) || defined(__WXGTK20__)
302 // value and time centered in one row
303 sizer
->Add(label
, 1, wxLARGESMALL(wxALIGN_RIGHT
,wxALIGN_LEFT
) | wxTOP
| wxRIGHT
, LAYOUT_MARGIN
);
304 sizer
->Add(value
, 1, wxALIGN_LEFT
| wxTOP
, LAYOUT_MARGIN
);
306 // value and time to the right in one row
308 sizer
->Add(value
, 0, wxLEFT
, LAYOUT_MARGIN
);
314 // ----------------------------------------------------------------------------
315 // wxProgressDialog operations
316 // ----------------------------------------------------------------------------
319 wxProgressDialog::Update(int value
, const wxString
& newmsg
, bool *skip
)
321 wxASSERT_MSG( value
== -1 || m_gauge
, wxT("cannot update non existent dialog") );
327 wxASSERT_MSG( value
<= m_maximum
, wxT("invalid progress value") );
330 m_gauge
->SetValue(value
);
332 UpdateMessage(newmsg
);
334 if ( (m_elapsed
|| m_remaining
|| m_estimated
) && (value
!= 0) )
336 unsigned long elapsed
= wxGetCurrentTime() - m_timeStart
;
337 if ( m_last_timeupdate
< elapsed
338 || value
== m_maximum
341 m_last_timeupdate
= elapsed
;
342 unsigned long estimated
= m_break
+
343 (unsigned long)(( (double) (elapsed
-m_break
) * m_maximum
) / ((double)value
)) ;
344 if ( estimated
> m_display_estimated
350 else if ( estimated
< m_display_estimated
360 if ( m_ctdelay
>= m_delay
// enough confirmations for a higher value
361 || m_ctdelay
<= (m_delay
*-1) // enough confirmations for a lower value
362 || value
== m_maximum
// to stay consistent
363 || elapsed
> m_display_estimated
// to stay consistent
364 || ( elapsed
> 0 && elapsed
< 4 ) // additional updates in the beginning
367 m_display_estimated
= estimated
;
372 long display_remaining
= m_display_estimated
- elapsed
;
373 if ( display_remaining
< 0 )
375 display_remaining
= 0;
378 SetTimeLabel(elapsed
, m_elapsed
);
379 SetTimeLabel(m_display_estimated
, m_estimated
);
380 SetTimeLabel(display_remaining
, m_remaining
);
383 if ( value
== m_maximum
)
385 if ( m_state
== Finished
)
387 // ignore multiple calls to Update(m_maximum): it may sometimes be
388 // troublesome to ensure that Update() is not called twice with the
389 // same value (e.g. because of the rounding errors) and if we don't
390 // return now we're going to generate asserts below
394 // so that we return true below and that out [Cancel] handler knew what
397 if( !(GetWindowStyle() & wxPD_AUTO_HIDE
) )
401 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
405 if ( newmsg
.empty() )
407 // also provide the finishing message if the application didn't
408 m_msg
->SetLabel(_("Done."));
417 // reenable other windows before hiding this one because otherwise
418 // Windows wouldn't give the focus back to the window which had
419 // been previously focused because it would still be disabled
420 ReenableOtherWindows();
425 else // not at maximum yet
427 return DoAfterUpdate(skip
);
430 // update the display in case yielding above didn't do it
433 return m_state
!= Canceled
;
436 bool wxProgressDialog::Pulse(const wxString
& newmsg
, bool *skip
)
438 wxASSERT_MSG( m_gauge
, wxT("cannot update non existent dialog") );
440 // show a bit of progress
443 UpdateMessage(newmsg
);
445 if (m_elapsed
|| m_remaining
|| m_estimated
)
447 unsigned long elapsed
= wxGetCurrentTime() - m_timeStart
;
449 SetTimeLabel(elapsed
, m_elapsed
);
450 SetTimeLabel((unsigned long)-1, m_estimated
);
451 SetTimeLabel((unsigned long)-1, m_remaining
);
454 return DoAfterUpdate(skip
);
457 bool wxProgressDialog::DoAfterUpdate(bool *skip
)
459 // we have to yield because not only we want to update the display but
460 // also to process the clicks on the cancel and skip buttons
465 if ( m_skip
&& skip
&& !*skip
)
472 return m_state
!= Canceled
;
475 void wxProgressDialog::Resume()
478 m_ctdelay
= m_delay
; // force an update of the elapsed/estimated/remaining time
479 m_break
+= wxGetCurrentTime()-m_timeStop
;
486 bool wxProgressDialog::Show( bool show
)
488 // reenable other windows before hiding this one because otherwise
489 // Windows wouldn't give the focus back to the window which had
490 // been previously focused because it would still be disabled
492 ReenableOtherWindows();
494 return wxDialog::Show(show
);
497 // ----------------------------------------------------------------------------
499 // ----------------------------------------------------------------------------
501 void wxProgressDialog::OnCancel(wxCommandEvent
& event
)
503 if ( m_state
== Finished
)
505 // this means that the count down is already finished and we're being
506 // shown as a modal dialog - so just let the default handler do the job
511 // request to cancel was received, the next time Update() is called we
515 // update the buttons state immediately so that the user knows that the
516 // request has been noticed
520 // save the time when the dialog was stopped
521 m_timeStop
= wxGetCurrentTime();
525 void wxProgressDialog::OnSkip(wxCommandEvent
& WXUNUSED(event
))
531 void wxProgressDialog::OnClose(wxCloseEvent
& event
)
533 if ( m_state
== Uncancelable
)
535 // can't close this dialog
538 else if ( m_state
== Finished
)
540 // let the default handler close the window as we already terminated
545 // next Update() will notice it
550 m_timeStop
= wxGetCurrentTime();
554 // ----------------------------------------------------------------------------
556 // ----------------------------------------------------------------------------
558 wxProgressDialog::~wxProgressDialog()
560 // normally this should have been already done, but just in case
561 ReenableOtherWindows();
564 void wxProgressDialog::ReenableOtherWindows()
566 if ( GetWindowStyle() & wxPD_APP_MODAL
)
568 delete m_winDisabler
;
569 m_winDisabler
= (wxWindowDisabler
*)NULL
;
574 m_parentTop
->Enable();
578 // ----------------------------------------------------------------------------
580 // ----------------------------------------------------------------------------
582 static void SetTimeLabel(unsigned long val
, wxStaticText
*label
)
588 if (val
!= (unsigned long)-1)
590 unsigned long hours
= val
/ 3600;
591 unsigned long minutes
= (val
% 3600) / 60;
592 unsigned long seconds
= val
% 60;
593 s
.Printf(wxT("%lu:%02lu:%02lu"), hours
, minutes
, seconds
);
600 if ( s
!= label
->GetLabel() )
605 void wxProgressDialog::EnableSkip(bool enable
)
609 #ifdef __SMARTPHONE__
611 SetRightMenu(wxID_SKIP
, _("Skip"));
616 m_btnSkip
->Enable(enable
);
621 void wxProgressDialog::EnableAbort(bool enable
)
625 #ifdef __SMARTPHONE__
627 SetLeftMenu(wxID_CANCEL
); // stock buttons makes Cancel label
632 m_btnAbort
->Enable(enable
);
637 void wxProgressDialog::EnableClose()
641 #ifdef __SMARTPHONE__
642 SetLeftMenu(wxID_CANCEL
, _("Close"));
646 m_btnAbort
->Enable();
647 m_btnAbort
->SetLabel(_("Close"));
653 void wxProgressDialog::UpdateMessage(const wxString
&newmsg
)
655 if ( !newmsg
.empty() && newmsg
!= m_msg
->GetLabel() )
657 m_msg
->SetLabel(newmsg
);
663 #endif // wxUSE_PROGRESSDLG