]>
git.saurik.com Git - wxWidgets.git/blob - src/generic/progdlgg.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxProgressDialog class
4 // Author: Karsten Ballüder
8 // Copyright: (c) Karsten Ballüder
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
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 // ============================================================================
77 // ============================================================================
79 // ----------------------------------------------------------------------------
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
;
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
= parent
;
119 while ( m_parentTop
&& m_parentTop
->GetParent() )
121 m_parentTop
= m_parentTop
->GetParent();
124 wxLayoutConstraints
*c
;
127 dc
.SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT
));
129 dc
.GetTextExtent(message
, &widthText
, NULL
, NULL
, NULL
, NULL
);
131 m_msg
= new wxStaticText(this, -1, message
);
132 c
= new wxLayoutConstraints
;
133 c
->left
.SameAs(this, wxLeft
, 2*LAYOUT_X_MARGIN
);
134 c
->top
.SameAs(this, wxTop
, 2*LAYOUT_Y_MARGIN
);
137 m_msg
->SetConstraints(c
);
140 sizeLabel
= m_msg
->GetSize();
141 sizeDlg
.y
= 2*LAYOUT_Y_MARGIN
+ sizeLabel
.y
;
143 wxWindow
*lastWindow
= m_msg
;
147 // note that we can't use wxGA_SMOOTH because it happens to also mean
148 // wxDIALOG_MODAL and will cause the dialog to be modal. Have an extra
149 // style argument to wxProgressDialog, perhaps.
150 m_gauge
= new wxGauge(this, -1, m_maximum
,
151 wxDefaultPosition
, wxDefaultSize
,
154 c
= new wxLayoutConstraints
;
155 c
->left
.SameAs(this, wxLeft
, 2*LAYOUT_X_MARGIN
);
156 c
->top
.Below(m_msg
, 2*LAYOUT_Y_MARGIN
);
157 c
->right
.SameAs(this, wxRight
, 2*LAYOUT_X_MARGIN
);
159 m_gauge
->SetConstraints(c
);
160 m_gauge
->SetValue(0);
161 lastWindow
= m_gauge
;
163 wxSize sizeGauge
= m_gauge
->GetSize();
164 sizeDlg
.y
+= 2*LAYOUT_Y_MARGIN
+ sizeGauge
.y
;
167 m_gauge
= (wxGauge
*)NULL
;
169 // create the estimated/remaining/total time zones if requested
170 m_elapsed
= m_estimated
= m_remaining
= (wxStaticText
*)NULL
;
172 // if we are going to have at least one label, remmeber it in this var
173 wxStaticText
*label
= NULL
;
175 // also count how many labels we really have
176 size_t nTimeLabels
= 0;
178 if ( style
& wxPD_ELAPSED_TIME
)
183 m_elapsed
= CreateLabel(_("Elapsed time : "), &lastWindow
);
186 if ( style
& wxPD_ESTIMATED_TIME
)
191 m_estimated
= CreateLabel(_("Estimated time : "), &lastWindow
);
194 if ( style
& wxPD_REMAINING_TIME
)
199 m_remaining
= CreateLabel(_("Remaining time : "), &lastWindow
);
202 if ( nTimeLabels
> 0 )
204 // set it to the current time
205 m_timeStart
= wxGetCurrentTime();
206 sizeDlg
.y
+= nTimeLabels
* (label
->GetSize().y
+ LAYOUT_Y_MARGIN
);
209 if ( hasAbortButton
)
211 m_btnAbort
= new wxButton(this, wxID_CANCEL
, _("Cancel"));
212 c
= new wxLayoutConstraints
;
214 // Windows dialogs usually have buttons in the lower right corner
216 c
->right
.SameAs(this, wxRight
, 2*LAYOUT_X_MARGIN
);
218 c
->centreX
.SameAs(this, wxCentreX
);
220 c
->bottom
.SameAs(this, wxBottom
, 2*LAYOUT_Y_MARGIN
);
225 m_btnAbort
->SetConstraints(c
);
227 sizeDlg
.y
+= 2*LAYOUT_Y_MARGIN
+ wxButton::GetDefaultSize().y
;
229 else // no "Cancel" button
231 m_btnAbort
= (wxButton
*)NULL
;
237 sizeDlg
.y
+= 2*LAYOUT_Y_MARGIN
;
239 // try to make the dialog not square but rectangular of reasonabel width
240 sizeDlg
.x
= (wxCoord
)wxMax(widthText
, 4*sizeDlg
.y
/3);
243 SetClientSize(sizeDlg
);
245 Centre(wxCENTER_FRAME
| wxBOTH
);
247 if ( style
& wxPD_APP_MODAL
)
249 m_winDisabler
= new wxWindowDisabler(this);
254 m_parentTop
->Enable(FALSE
);
255 m_winDisabler
= NULL
;
259 Enable(TRUE
); // enable this window
261 // this one can be initialized even if the others are unknown for now
263 // NB: do it after calling Layout() to keep the labels correctly aligned
266 SetTimeLabel(0, m_elapsed
);
269 // Update the display (especially on X, GTK)
274 MacUpdateImmediately();
278 wxStaticText
*wxProgressDialog::CreateLabel(const wxString
& text
,
279 wxWindow
**lastWindow
)
281 wxLayoutConstraints
*c
;
283 wxStaticText
*label
= new wxStaticText(this, -1, _("unknown"));
284 c
= new wxLayoutConstraints
;
286 // VZ: I like the labels be centered - if the others don't mind, you may
287 // remove "#ifdef __WXMSW__" and use it for all ports
289 c
->left
.SameAs(this, wxCentreX
, LAYOUT_X_MARGIN
);
291 c
->right
.SameAs(this, wxRight
, 2*LAYOUT_X_MARGIN
);
293 c
->top
.Below(*lastWindow
, LAYOUT_Y_MARGIN
);
296 label
->SetConstraints(c
);
298 wxStaticText
*dummy
= new wxStaticText(this, -1, text
);
299 c
= new wxLayoutConstraints
;
300 c
->right
.LeftOf(label
);
301 c
->top
.SameAs(label
, wxTop
, 0);
304 dummy
->SetConstraints(c
);
312 wxProgressDialog::Update(int value
, const wxString
& newmsg
)
314 wxASSERT_MSG( value
== -1 || m_gauge
, wxT("cannot update non existent dialog") );
320 wxASSERT_MSG( value
<= m_maximum
, wxT("invalid progress value") );
324 m_gauge
->SetValue(value
+ 1);
327 if ( !newmsg
.IsEmpty() )
329 m_msg
->SetLabel(newmsg
);
335 if ( (m_elapsed
|| m_remaining
|| m_estimated
) && (value
!= 0) )
337 unsigned long elapsed
= wxGetCurrentTime() - m_timeStart
;
338 unsigned long estimated
= elapsed
* m_maximum
/ value
;
339 unsigned long remaining
= estimated
- elapsed
;
341 SetTimeLabel(elapsed
, m_elapsed
);
342 SetTimeLabel(estimated
, m_estimated
);
343 SetTimeLabel(remaining
, m_remaining
);
346 if ( (value
== m_maximum
) )
348 // so that we return TRUE below and that out [Cancel] handler knew what
351 if( !(GetWindowStyle() & wxPD_AUTO_HIDE
) )
355 // tell the user what he should do...
356 m_btnAbort
->SetLabel(_("Close"));
358 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
359 else // enable the close button to give the user a way to close the dlg
361 EnableCloseButton(TRUE
);
367 // also provide the finishing message if the application didn't
368 m_msg
->SetLabel(_("Done."));
379 ReenableOtherWindows();
384 // update the display
390 MacUpdateImmediately();
393 return m_state
!= Canceled
;
396 // ----------------------------------------------------------------------------
398 // ----------------------------------------------------------------------------
400 void wxProgressDialog::OnCancel(wxCommandEvent
& event
)
402 if ( m_state
== Finished
)
404 // this means that the count down is already finished and we're being
405 // shown as a modal dialog - so just let the default handler do the job
410 // request to cancel was received, the next time Update() is called we
414 // update the button state immediately so that the user knows that the
415 // request has been noticed
416 m_btnAbort
->Disable();
420 void wxProgressDialog::OnClose(wxCloseEvent
& event
)
422 if ( m_state
== Uncancelable
)
424 // can't close this dialog
427 else if ( m_state
== Finished
)
429 // let the default handler close the window as we already terminated
434 // next Update() will notice it
439 // ----------------------------------------------------------------------------
441 // ----------------------------------------------------------------------------
443 wxProgressDialog::~wxProgressDialog()
445 // normally this should have been already done, but just in case
446 ReenableOtherWindows();
449 void wxProgressDialog::ReenableOtherWindows()
451 if ( GetWindowStyle() & wxPD_APP_MODAL
)
453 delete m_winDisabler
;
454 m_winDisabler
= (wxWindowDisabler
*)NULL
;
459 m_parentTop
->Enable(TRUE
);
463 // ----------------------------------------------------------------------------
465 // ----------------------------------------------------------------------------
467 static void SetTimeLabel(unsigned long val
, wxStaticText
*label
)
472 unsigned long hours
= val
/ 3600;
473 unsigned long minutes
= (val
% 3600) / 60;
474 unsigned long seconds
= val
% 60;
475 s
.Printf(wxT("%lu:%02lu:%02lu"), hours
, minutes
, seconds
);
477 if ( s
!= label
->GetLabel() )
482 #endif // wxUSE_PROGRESSDLG