1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxProgressDialog class
4 // Author: Karsten Ballüder
8 // Copyright: (c) Karsten Ballüder
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
21 #pragma implementation "progdlgg.h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
36 #include "wx/button.h"
37 #include "wx/stattext.h"
42 #include "wx/dcclient.h"
46 #include "wx/generic/progdlgg.h"
47 #include "wx/settings.h"
49 // ---------------------------------------------------------------------------
51 // ---------------------------------------------------------------------------
53 /* Macro for avoiding #ifdefs when value have to be different depending on size of
54 device we display on - take it from something like wxDesktopPolicy in the future
57 #if defined(__SMARTPHONE__)
58 #define wxLARGESMALL(large,small) small
60 #define wxLARGESMALL(large,small) large
63 // ----------------------------------------------------------------------------
65 // ----------------------------------------------------------------------------
67 #define LAYOUT_MARGIN wxLARGESMALL(8,2)
69 static const int wxID_SKIP
= 32000; // whatever
71 // ----------------------------------------------------------------------------
73 // ----------------------------------------------------------------------------
75 // update the label to show the given time (in seconds)
76 static void SetTimeLabel(unsigned long val
, wxStaticText
*label
);
78 // ----------------------------------------------------------------------------
80 // ----------------------------------------------------------------------------
82 BEGIN_EVENT_TABLE(wxProgressDialog
, wxDialog
)
83 EVT_BUTTON(wxID_CANCEL
, wxProgressDialog::OnCancel
)
84 EVT_BUTTON(wxID_SKIP
, wxProgressDialog::OnSkip
)
86 EVT_CLOSE(wxProgressDialog::OnClose
)
89 IMPLEMENT_CLASS(wxProgressDialog
, wxDialog
)
91 // ============================================================================
92 // wxProgressDialog implementation
93 // ============================================================================
95 // ----------------------------------------------------------------------------
96 // wxProgressDialog creation
97 // ----------------------------------------------------------------------------
99 wxProgressDialog::wxProgressDialog(wxString
const &title
,
100 wxString
const &message
,
104 : wxDialog(parent
, wxID_ANY
, title
),
107 m_hasAbortButton(false),
108 m_hasSkipButton(false)
110 // we may disappear at any moment, let the others know about it
111 SetExtraStyle(GetExtraStyle() | wxWS_EX_TRANSIENT
);
112 m_windowStyle
|= style
;
114 m_hasAbortButton
= (style
& wxPD_CAN_ABORT
) != 0;
115 m_hasSkipButton
= (style
& wxPD_CAN_SKIP
) != 0;
117 bool isPda
= (wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA
);
119 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
120 // we have to remove the "Close" button from the title bar then as it is
121 // confusing to have it - it doesn't work anyhow
123 // FIXME: should probably have a (extended?) window style for this
124 if ( !m_hasAbortButton
)
126 EnableCloseButton(false);
130 #if defined(__SMARTPHONE__)
134 m_state
= m_hasAbortButton
? Continue
: Uncancelable
;
137 #if defined(__WXMSW__) || defined(__WXPM__)
138 // we can't have values > 65,536 in the progress control under Windows, so
139 // scale everything down
140 m_factor
= m_maximum
/ 65536 + 1;
141 m_maximum
/= m_factor
;
144 m_parentTop
= wxGetTopLevelParent(parent
);
147 dc
.SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
149 dc
.GetTextExtent(message
, &widthText
, NULL
, NULL
, NULL
, NULL
);
151 wxBoxSizer
*sizer
= new wxBoxSizer(wxVERTICAL
);
153 m_msg
= new wxStaticText(this, wxID_ANY
, message
);
154 sizer
->Add(m_msg
, 0, wxLEFT
| wxTOP
, 2*LAYOUT_MARGIN
);
157 sizeLabel
= m_msg
->GetSize();
158 sizeDlg
.y
= 2*LAYOUT_MARGIN
+ sizeLabel
.y
;
162 int gauge_style
= wxGA_HORIZONTAL
;
163 if ( ( style
& wxPD_SMOOTH
) == wxPD_SMOOTH
)
164 gauge_style
|= wxGA_SMOOTH
;
165 m_gauge
= new wxGauge(this, wxID_ANY
, m_maximum
,
166 wxDefaultPosition
, wxDefaultSize
,
169 sizer
->Add(m_gauge
, 0, wxLEFT
| wxRIGHT
| wxTOP
| wxEXPAND
, 2*LAYOUT_MARGIN
);
170 m_gauge
->SetValue(0);
172 wxSize sizeGauge
= m_gauge
->GetSize();
173 sizeDlg
.y
+= 2*LAYOUT_MARGIN
+ sizeGauge
.y
;
176 m_gauge
= (wxGauge
*)NULL
;
178 // create the estimated/remaining/total time zones if requested
179 m_elapsed
= m_estimated
= m_remaining
= (wxStaticText
*)NULL
;
180 m_display_estimated
= m_last_timeupdate
= m_break
= 0;
183 // if we are going to have at least one label, remmeber it in this var
184 wxStaticText
*label
= NULL
;
186 // also count how many labels we really have
187 size_t nTimeLabels
= 0;
189 if ( style
& wxPD_ELAPSED_TIME
)
194 m_elapsed
= CreateLabel(_("Elapsed time : "), sizer
);
197 if ( style
& wxPD_ESTIMATED_TIME
)
202 m_estimated
= CreateLabel(_("Estimated time : "), sizer
);
205 if ( style
& wxPD_REMAINING_TIME
)
210 m_remaining
= CreateLabel(_("Remaining time : "), sizer
);
213 if ( nTimeLabels
> 0 )
215 // set it to the current time
216 m_timeStart
= wxGetCurrentTime();
217 sizeDlg
.y
+= nTimeLabels
* (label
->GetSize().y
+ LAYOUT_MARGIN
);
220 #if defined(__SMARTPHONE__)
221 if ( m_hasSkipButton
)
222 SetRightMenu(wxID_SKIP
, _("Skip"));
223 if ( m_hasAbortButton
)
224 SetLeftMenu(wxID_CANCEL
);
226 m_btnAbort
= m_btnSkip
= (wxButton
*)NULL
;
227 bool sizeDlgModified
= false;
228 wxBoxSizer
*buttonSizer
= new wxBoxSizer(wxHORIZONTAL
);
230 const int sizerFlags
=
231 #if defined(__WXMSW__) || defined(__WXPM__)
232 wxALIGN_RIGHT
| wxALL
234 wxALIGN_CENTER_HORIZONTAL
| wxBOTTOM
| wxTOP
238 if ( m_hasSkipButton
)
240 m_btnSkip
= new wxButton(this, wxID_SKIP
, _("Skip"));
242 // Windows dialogs usually have buttons in the lower right corner
243 buttonSizer
->Add(m_btnSkip
, 0, sizerFlags
, LAYOUT_MARGIN
);
244 sizeDlg
.y
+= 2*LAYOUT_MARGIN
+ wxButton::GetDefaultSize().y
;
245 sizeDlgModified
= true;
248 if ( m_hasAbortButton
)
250 m_btnAbort
= new wxButton(this, wxID_CANCEL
);
252 // Windows dialogs usually have buttons in the lower right corner
253 buttonSizer
->Add(m_btnAbort
, 0, sizerFlags
, LAYOUT_MARGIN
);
255 sizeDlg
.y
+= 2*LAYOUT_MARGIN
+ wxButton::GetDefaultSize().y
;
258 sizer
->Add(buttonSizer
, 0, sizerFlags
, LAYOUT_MARGIN
);
259 #endif // __SMARTPHONE__/!__SMARTPHONE__
261 SetSizerAndFit(sizer
);
265 sizeDlg
.y
+= 2*LAYOUT_MARGIN
;
267 // try to make the dialog not square but rectangular of reasonable width
268 sizeDlg
.x
= (wxCoord
)wxMax(widthText
, 4*sizeDlg
.y
/3);
271 SetClientSize(sizeDlg
);
274 Centre(wxCENTER_FRAME
| wxBOTH
);
276 if ( style
& wxPD_APP_MODAL
)
278 m_winDisabler
= new wxWindowDisabler(this);
283 m_parentTop
->Disable();
284 m_winDisabler
= NULL
;
290 // this one can be initialized even if the others are unknown for now
292 // NB: do it after calling Layout() to keep the labels correctly aligned
295 SetTimeLabel(0, m_elapsed
);
301 wxStaticText
*wxProgressDialog::CreateLabel(const wxString
& text
,
304 wxBoxSizer
*locsizer
= new wxBoxSizer(wxLARGESMALL(wxHORIZONTAL
,wxVERTICAL
));
306 wxStaticText
*dummy
= new wxStaticText(this, wxID_ANY
, text
);
307 wxStaticText
*label
= new wxStaticText(this, wxID_ANY
, _("unknown"));
309 // select placement most native or nice on target GUI
310 #if defined(__SMARTPHONE__)
311 // label and time to the left in two rows
312 locsizer
->Add(dummy
, 1, wxALIGN_LEFT
);
313 locsizer
->Add(label
, 1, wxALIGN_LEFT
);
314 sizer
->Add(locsizer
, 0, wxALIGN_LEFT
| wxTOP
| wxLEFT
, LAYOUT_MARGIN
);
315 #elif defined(__WXMSW__) || defined(__WXPM__) || defined(__WXMAC__)
316 // label and time centered in one row
317 locsizer
->Add(dummy
, 1, wxLARGESMALL(wxALIGN_RIGHT
,wxALIGN_LEFT
));
318 locsizer
->Add(label
, 1, wxALIGN_LEFT
| wxLEFT
, LAYOUT_MARGIN
);
319 sizer
->Add(locsizer
, 0, wxALIGN_CENTER_HORIZONTAL
| wxTOP
, LAYOUT_MARGIN
);
321 // label and time to the right in one row
322 sizer
->Add(locsizer
, 0, wxALIGN_RIGHT
| wxRIGHT
| wxTOP
, LAYOUT_MARGIN
);
323 locsizer
->Add(dummy
);
324 locsizer
->Add(label
, 0, wxLEFT
, LAYOUT_MARGIN
);
330 // ----------------------------------------------------------------------------
331 // wxProgressDialog operations
332 // ----------------------------------------------------------------------------
335 wxProgressDialog::Update(int value
, const wxString
& newmsg
, bool *skip
)
337 wxASSERT_MSG( value
== -1 || m_gauge
, wxT("cannot update non existent dialog") );
343 wxASSERT_MSG( value
<= m_maximum
, wxT("invalid progress value") );
345 // fill up the gauge if value == maximum because this means that the dialog
346 // is going to close and the gauge shouldn't be partly empty in this case
347 if ( m_gauge
&& value
<= m_maximum
)
349 m_gauge
->SetValue(value
== m_maximum
? value
: value
+ 1);
352 if ( !newmsg
.empty() && newmsg
!= m_msg
->GetLabel() )
354 m_msg
->SetLabel(newmsg
);
359 if ( (m_elapsed
|| m_remaining
|| m_estimated
) && (value
!= 0) )
361 unsigned long elapsed
= wxGetCurrentTime() - m_timeStart
;
362 if ( m_last_timeupdate
< elapsed
363 || value
== m_maximum
366 m_last_timeupdate
= elapsed
;
367 unsigned long estimated
= m_break
+
368 (unsigned long)(( (double) (elapsed
-m_break
) * m_maximum
) / ((double)value
)) ;
369 if ( estimated
> m_display_estimated
375 else if ( estimated
< m_display_estimated
385 if ( m_ctdelay
>= m_delay
// enough confirmations for a higher value
386 || m_ctdelay
<= (m_delay
*-1) // enough confirmations for a lower value
387 || value
== m_maximum
// to stay consistent
388 || elapsed
> m_display_estimated
// to stay consistent
389 || ( elapsed
> 0 && elapsed
< 4 ) // additional updates in the beginning
392 m_display_estimated
= estimated
;
397 long display_remaining
= m_display_estimated
- elapsed
;
398 if ( display_remaining
< 0 )
400 display_remaining
= 0;
403 SetTimeLabel(elapsed
, m_elapsed
);
404 SetTimeLabel(m_display_estimated
, m_estimated
);
405 SetTimeLabel(display_remaining
, m_remaining
);
408 if ( value
== m_maximum
)
410 if ( m_state
== Finished
)
412 // ignore multiple calls to Update(m_maximum): it may sometimes be
413 // troublesome to ensure that Update() is not called twice with the
414 // same value (e.g. because of the rounding errors) and if we don't
415 // return now we're going to generate asserts below
419 // so that we return true below and that out [Cancel] handler knew what
422 if( !(GetWindowStyle() & wxPD_AUTO_HIDE
) )
426 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
430 if ( !newmsg
.empty() )
432 // also provide the finishing message if the application didn't
433 m_msg
->SetLabel(_("Done."));
442 // reenable other windows before hiding this one because otherwise
443 // Windows wouldn't give the focus back to the window which had
444 // been previously focused because it would still be disabled
445 ReenableOtherWindows();
452 // we have to yield because not only we want to update the display but
453 // also to process the clicks on the cancel and skip buttons
456 if ( (m_skip
) && (skip
!= NULL
) && (*skip
== false) )
464 // update the display in case yielding above didn't do it
467 return m_state
!= Canceled
;
470 void wxProgressDialog::Resume()
473 m_ctdelay
= m_delay
; // force an update of the elapsed/estimated/remaining time
474 m_break
+= wxGetCurrentTime()-m_timeStop
;
481 bool wxProgressDialog::Show( bool show
)
483 // reenable other windows before hiding this one because otherwise
484 // Windows wouldn't give the focus back to the window which had
485 // been previously focused because it would still be disabled
487 ReenableOtherWindows();
489 return wxDialog::Show(show
);
492 // ----------------------------------------------------------------------------
494 // ----------------------------------------------------------------------------
496 void wxProgressDialog::OnCancel(wxCommandEvent
& event
)
498 if ( m_state
== Finished
)
500 // this means that the count down is already finished and we're being
501 // shown as a modal dialog - so just let the default handler do the job
506 // request to cancel was received, the next time Update() is called we
510 // update the buttons state immediately so that the user knows that the
511 // request has been noticed
515 // save the time when the dialog was stopped
516 m_timeStop
= wxGetCurrentTime();
520 void wxProgressDialog::OnSkip(wxCommandEvent
& WXUNUSED(event
))
526 void wxProgressDialog::OnClose(wxCloseEvent
& event
)
528 if ( m_state
== Uncancelable
)
530 // can't close this dialog
533 else if ( m_state
== Finished
)
535 // let the default handler close the window as we already terminated
540 // next Update() will notice it
545 m_timeStop
= wxGetCurrentTime();
549 // ----------------------------------------------------------------------------
551 // ----------------------------------------------------------------------------
553 wxProgressDialog::~wxProgressDialog()
555 // normally this should have been already done, but just in case
556 ReenableOtherWindows();
559 void wxProgressDialog::ReenableOtherWindows()
561 if ( GetWindowStyle() & wxPD_APP_MODAL
)
563 delete m_winDisabler
;
564 m_winDisabler
= (wxWindowDisabler
*)NULL
;
569 m_parentTop
->Enable();
573 // ----------------------------------------------------------------------------
575 // ----------------------------------------------------------------------------
577 static void SetTimeLabel(unsigned long val
, wxStaticText
*label
)
582 unsigned long hours
= val
/ 3600;
583 unsigned long minutes
= (val
% 3600) / 60;
584 unsigned long seconds
= val
% 60;
585 s
.Printf(wxT("%lu:%02lu:%02lu"), hours
, minutes
, seconds
);
587 if ( s
!= label
->GetLabel() )
592 void wxProgressDialog::EnableSkip(bool enable
)
596 #ifdef __SMARTPHONE__
598 SetRightMenu(wxID_SKIP
, _("Skip"));
603 m_btnSkip
->Enable(enable
);
608 void wxProgressDialog::EnableAbort(bool enable
)
612 #ifdef __SMARTPHONE__
614 SetLeftMenu(wxID_CANCEL
); // stock buttons makes Cancel label
619 m_btnAbort
->Enable(enable
);
624 void wxProgressDialog::EnableClose()
628 #ifdef __SMARTPHONE__
629 SetLeftMenu(wxID_CANCEL
, _("Close"));
633 m_btnAbort
->Enable();
634 m_btnAbort
->SetLabel(_("Close"));
640 #endif // wxUSE_PROGRESSDLG