]>
git.saurik.com Git - wxWidgets.git/blob - src/generic/progdlgg.cpp
95e23580b6d15c6c7a0b475d908785b9addc3d40
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"
38 #include "wx/layout.h"
42 #include "wx/settings.h"
43 #include "wx/dcclient.h"
47 #include "wx/generic/progdlgg.h"
49 // ----------------------------------------------------------------------------
51 // ----------------------------------------------------------------------------
53 #define LAYOUT_X_MARGIN 8
54 #define LAYOUT_Y_MARGIN 8
56 // ----------------------------------------------------------------------------
58 // ----------------------------------------------------------------------------
60 // update the label to show the given time (in seconds)
61 static void SetTimeLabel(unsigned long val
, wxStaticText
*label
);
63 // ----------------------------------------------------------------------------
65 // ----------------------------------------------------------------------------
67 BEGIN_EVENT_TABLE(wxProgressDialog
, wxDialog
)
68 EVT_BUTTON(wxID_CANCEL
, wxProgressDialog::OnCancel
)
70 EVT_CLOSE(wxProgressDialog::OnClose
)
73 IMPLEMENT_CLASS(wxProgressDialog
, wxDialog
)
75 // ============================================================================
76 // wxProgressDialog implementation
77 // ============================================================================
79 // ----------------------------------------------------------------------------
80 // wxProgressDialog creation
81 // ----------------------------------------------------------------------------
83 wxProgressDialog::wxProgressDialog(wxString
const &title
,
84 wxString
const &message
,
88 : wxDialog(parent
, -1, title
)
90 // we may disappear at any moment, let the others know about it
91 SetExtraStyle(GetExtraStyle() | wxWS_EX_TRANSIENT
);
93 m_windowStyle
|= style
;
95 bool hasAbortButton
= (style
& wxPD_CAN_ABORT
) != 0;
97 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
98 // we have to remove the "Close" button from the title bar then as it is
99 // confusing to have it - it doesn't work anyhow
101 // FIXME: should probably have a (extended?) window style for this
102 if ( !hasAbortButton
)
104 EnableCloseButton(FALSE
);
108 m_state
= hasAbortButton
? Continue
: Uncancelable
;
111 #if defined(__WXMSW__) || defined(__WXPM__)
112 // we can't have values > 65,536 in the progress control under Windows, so
113 // scale everything down
114 m_factor
= m_maximum
/ 65536 + 1;
115 m_maximum
/= m_factor
;
118 m_parentTop
= wxGetTopLevelParent(parent
);
120 wxLayoutConstraints
*c
;
123 dc
.SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
125 dc
.GetTextExtent(message
, &widthText
, NULL
, NULL
, NULL
, NULL
);
127 m_msg
= new wxStaticText(this, -1, message
);
128 c
= new wxLayoutConstraints
;
129 c
->left
.SameAs(this, wxLeft
, 2*LAYOUT_X_MARGIN
);
130 c
->top
.SameAs(this, wxTop
, 2*LAYOUT_Y_MARGIN
);
133 m_msg
->SetConstraints(c
);
136 sizeLabel
= m_msg
->GetSize();
137 sizeDlg
.y
= 2*LAYOUT_Y_MARGIN
+ sizeLabel
.y
;
139 wxWindow
*lastWindow
= m_msg
;
143 // note that we can't use wxGA_SMOOTH because it happens to
144 // cause the dialog to be modal. Have an extra
145 // style argument to wxProgressDialog, perhaps.
146 m_gauge
= new wxGauge(this, -1, m_maximum
,
147 wxDefaultPosition
, wxDefaultSize
,
150 c
= new wxLayoutConstraints
;
151 c
->left
.SameAs(this, wxLeft
, 2*LAYOUT_X_MARGIN
);
152 c
->top
.Below(m_msg
, 2*LAYOUT_Y_MARGIN
);
153 c
->right
.SameAs(this, wxRight
, 2*LAYOUT_X_MARGIN
);
155 m_gauge
->SetConstraints(c
);
156 m_gauge
->SetValue(0);
157 lastWindow
= m_gauge
;
159 wxSize sizeGauge
= m_gauge
->GetSize();
160 sizeDlg
.y
+= 2*LAYOUT_Y_MARGIN
+ sizeGauge
.y
;
163 m_gauge
= (wxGauge
*)NULL
;
165 // create the estimated/remaining/total time zones if requested
166 m_elapsed
= m_estimated
= m_remaining
= (wxStaticText
*)NULL
;
168 // if we are going to have at least one label, remmeber it in this var
169 wxStaticText
*label
= NULL
;
171 // also count how many labels we really have
172 size_t nTimeLabels
= 0;
174 if ( style
& wxPD_ELAPSED_TIME
)
179 m_elapsed
= CreateLabel(_("Elapsed time : "), &lastWindow
);
182 if ( style
& wxPD_ESTIMATED_TIME
)
187 m_estimated
= CreateLabel(_("Estimated time : "), &lastWindow
);
190 if ( style
& wxPD_REMAINING_TIME
)
195 m_remaining
= CreateLabel(_("Remaining time : "), &lastWindow
);
198 if ( nTimeLabels
> 0 )
200 // set it to the current time
201 m_timeStart
= wxGetCurrentTime();
202 sizeDlg
.y
+= nTimeLabels
* (label
->GetSize().y
+ LAYOUT_Y_MARGIN
);
205 if ( hasAbortButton
)
207 m_btnAbort
= new wxButton(this, wxID_CANCEL
, _("Cancel"));
208 c
= new wxLayoutConstraints
;
210 // Windows dialogs usually have buttons in the lower right corner
211 #if defined(__WXMSW__) || defined(__WXPM__)
212 c
->right
.SameAs(this, wxRight
, 2*LAYOUT_X_MARGIN
);
214 c
->centreX
.SameAs(this, wxCentreX
);
216 c
->bottom
.SameAs(this, wxBottom
, 2*LAYOUT_Y_MARGIN
);
221 m_btnAbort
->SetConstraints(c
);
223 sizeDlg
.y
+= 2*LAYOUT_Y_MARGIN
+ wxButton::GetDefaultSize().y
;
225 else // no "Cancel" button
227 m_btnAbort
= (wxButton
*)NULL
;
233 sizeDlg
.y
+= 2*LAYOUT_Y_MARGIN
;
235 // try to make the dialog not square but rectangular of reasonabel width
236 sizeDlg
.x
= (wxCoord
)wxMax(widthText
, 4*sizeDlg
.y
/3);
239 SetClientSize(sizeDlg
);
241 Centre(wxCENTER_FRAME
| wxBOTH
);
243 if ( style
& wxPD_APP_MODAL
)
245 m_winDisabler
= new wxWindowDisabler(this);
250 m_parentTop
->Enable(FALSE
);
251 m_winDisabler
= NULL
;
255 Enable(TRUE
); // enable this window
257 // this one can be initialized even if the others are unknown for now
259 // NB: do it after calling Layout() to keep the labels correctly aligned
262 SetTimeLabel(0, m_elapsed
);
268 wxStaticText
*wxProgressDialog::CreateLabel(const wxString
& text
,
269 wxWindow
**lastWindow
)
271 wxLayoutConstraints
*c
;
273 wxStaticText
*label
= new wxStaticText(this, -1, _("unknown"));
274 c
= new wxLayoutConstraints
;
276 // VZ: I like the labels be centered - if the others don't mind, you may
277 // remove "#ifdef __WXMSW__" and use it for all ports
278 #if defined(__WXMSW__) || defined(__WXPM__) || defined(__WXMAC__)
279 c
->left
.SameAs(this, wxCentreX
, LAYOUT_X_MARGIN
);
281 c
->right
.SameAs(this, wxRight
, 2*LAYOUT_X_MARGIN
);
283 c
->top
.Below(*lastWindow
, LAYOUT_Y_MARGIN
);
286 label
->SetConstraints(c
);
288 wxStaticText
*dummy
= new wxStaticText(this, -1, text
);
289 c
= new wxLayoutConstraints
;
290 c
->right
.LeftOf(label
);
291 c
->top
.SameAs(label
, wxTop
, 0);
294 dummy
->SetConstraints(c
);
301 // ----------------------------------------------------------------------------
302 // wxProgressDialog operations
303 // ----------------------------------------------------------------------------
306 wxProgressDialog::Update(int value
, const wxString
& newmsg
)
308 wxASSERT_MSG( value
== -1 || m_gauge
, wxT("cannot update non existent dialog") );
314 wxASSERT_MSG( value
<= m_maximum
, wxT("invalid progress value") );
316 if ( m_gauge
&& value
< m_maximum
)
318 m_gauge
->SetValue(value
+ 1);
321 if ( !newmsg
.IsEmpty() )
323 m_msg
->SetLabel(newmsg
);
328 if ( (m_elapsed
|| m_remaining
|| m_estimated
) && (value
!= 0) )
330 unsigned long elapsed
= wxGetCurrentTime() - m_timeStart
;
331 unsigned long estimated
= (unsigned long)(( (double) elapsed
* m_maximum
) / ((double)value
)) ;
332 unsigned long remaining
= estimated
- elapsed
;
334 SetTimeLabel(elapsed
, m_elapsed
);
335 SetTimeLabel(estimated
, m_estimated
);
336 SetTimeLabel(remaining
, m_remaining
);
339 if ( value
== m_maximum
)
341 // so that we return TRUE below and that out [Cancel] handler knew what
344 if( !(GetWindowStyle() & wxPD_AUTO_HIDE
) )
348 // tell the user what he should do...
349 m_btnAbort
->SetLabel(_("Close"));
351 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
352 else // enable the button to give the user a way to close the dlg
354 EnableCloseButton(TRUE
);
360 // also provide the finishing message if the application didn't
361 m_msg
->SetLabel(_("Done."));
370 // reenable other windows before hiding this one because otherwise
371 // Windows wouldn't give the focus back to the window which had
372 // been previously focused because it would still be disabled
373 ReenableOtherWindows();
380 // we have to yield because not only we want to update the display but
381 // also to process the clicks on the cancel button
385 // update the display in case yielding above didn't do it
388 return m_state
!= Canceled
;
391 void wxProgressDialog::Resume()
395 // it may have been disabled by OnCancel(), so enable it back to let the
396 // user interrupt us again if needed
397 m_btnAbort
->Enable();
400 bool wxProgressDialog::Show( bool show
)
402 // reenable other windows before hiding this one because otherwise
403 // Windows wouldn't give the focus back to the window which had
404 // been previously focused because it would still be disabled
406 ReenableOtherWindows();
408 return wxDialog::Show(show
);
411 // ----------------------------------------------------------------------------
413 // ----------------------------------------------------------------------------
415 void wxProgressDialog::OnCancel(wxCommandEvent
& event
)
417 if ( m_state
== Finished
)
419 // this means that the count down is already finished and we're being
420 // shown as a modal dialog - so just let the default handler do the job
425 // request to cancel was received, the next time Update() is called we
429 // update the button state immediately so that the user knows that the
430 // request has been noticed
431 m_btnAbort
->Disable();
435 void wxProgressDialog::OnClose(wxCloseEvent
& event
)
437 if ( m_state
== Uncancelable
)
439 // can't close this dialog
442 else if ( m_state
== Finished
)
444 // let the default handler close the window as we already terminated
449 // next Update() will notice it
454 // ----------------------------------------------------------------------------
456 // ----------------------------------------------------------------------------
458 wxProgressDialog::~wxProgressDialog()
460 // normally this should have been already done, but just in case
461 ReenableOtherWindows();
464 void wxProgressDialog::ReenableOtherWindows()
466 if ( GetWindowStyle() & wxPD_APP_MODAL
)
468 delete m_winDisabler
;
469 m_winDisabler
= (wxWindowDisabler
*)NULL
;
474 m_parentTop
->Enable(TRUE
);
478 // ----------------------------------------------------------------------------
480 // ----------------------------------------------------------------------------
482 static void SetTimeLabel(unsigned long val
, wxStaticText
*label
)
487 unsigned long hours
= val
/ 3600;
488 unsigned long minutes
= (val
% 3600) / 60;
489 unsigned long seconds
= val
% 60;
490 s
.Printf(wxT("%lu:%02lu:%02lu"), hours
, minutes
, seconds
);
492 if ( s
!= label
->GetLabel() )
497 #endif // wxUSE_PROGRESSDLG