]> git.saurik.com Git - wxWidgets.git/blob - src/generic/progdlgg.cpp
ShowFullScreen is not MSW-only anymore, updated docs and samples
[wxWidgets.git] / src / generic / progdlgg.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: progdlgg.h
3 // Purpose: wxProgressDialog class
4 // Author: Karsten Ballüder
5 // Modified by:
6 // Created: 09.05.1999
7 // RCS-ID: $Id$
8 // Copyright: (c) Karsten Ballüder
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #ifdef __GNUG__
21 #pragma implementation "progdlgg.h"
22 #endif
23
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
26
27 #ifdef __BORLANDC__
28 #pragma hdrstop
29 #endif
30
31 #if wxUSE_PROGRESSDLG
32
33 #ifndef WX_PRECOMP
34 #include "wx/utils.h"
35 #include "wx/frame.h"
36 #include "wx/button.h"
37 #include "wx/stattext.h"
38 #include "wx/layout.h"
39 #include "wx/event.h"
40 #include "wx/gauge.h"
41 #include "wx/intl.h"
42 #include "wx/settings.h"
43 #include "wx/dcclient.h"
44 #include "wx/timer.h"
45 #endif
46
47 #include "wx/generic/progdlgg.h"
48
49 // ----------------------------------------------------------------------------
50 // constants
51 // ----------------------------------------------------------------------------
52
53 #define LAYOUT_X_MARGIN 8
54 #define LAYOUT_Y_MARGIN 8
55
56 // ----------------------------------------------------------------------------
57 // private functions
58 // ----------------------------------------------------------------------------
59
60 // update the label to show the given time (in seconds)
61 static void SetTimeLabel(unsigned long val, wxStaticText *label);
62
63 // ----------------------------------------------------------------------------
64 // event tables
65 // ----------------------------------------------------------------------------
66
67 BEGIN_EVENT_TABLE(wxProgressDialog, wxDialog)
68 EVT_BUTTON(wxID_CANCEL, wxProgressDialog::OnCancel)
69
70 EVT_SHOW(wxProgressDialog::OnShow)
71
72 EVT_CLOSE(wxProgressDialog::OnClose)
73 END_EVENT_TABLE()
74
75 IMPLEMENT_CLASS(wxProgressDialog, wxDialog)
76
77 // ============================================================================
78 // implementation
79 // ============================================================================
80
81 // ----------------------------------------------------------------------------
82 // wxProgressDialog
83 // ----------------------------------------------------------------------------
84
85 wxProgressDialog::wxProgressDialog(wxString const &title,
86 wxString const &message,
87 int maximum,
88 wxWindow *parent,
89 int style)
90 : wxDialog(parent, -1, title)
91 {
92 m_windowStyle |= style;
93
94 bool hasAbortButton = (style & wxPD_CAN_ABORT) != 0;
95 m_state = hasAbortButton ? Continue : Uncancelable;
96 m_maximum = maximum;
97
98 m_parentTop = parent;
99 while ( m_parentTop && m_parentTop->GetParent() )
100 {
101 m_parentTop = m_parentTop->GetParent();
102 }
103
104 wxLayoutConstraints *c;
105
106 wxClientDC dc(this);
107 dc.SetFont(wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT));
108 long widthText;
109 dc.GetTextExtent(message, &widthText, NULL, NULL, NULL, NULL);
110
111 m_msg = new wxStaticText(this, -1, message);
112 c = new wxLayoutConstraints;
113 c->left.SameAs(this, wxLeft, 2*LAYOUT_X_MARGIN);
114 c->top.SameAs(this, wxTop, 2*LAYOUT_Y_MARGIN);
115 c->width.AsIs();
116 c->height.AsIs();
117 m_msg->SetConstraints(c);
118
119 wxSize sizeDlg, sizeLabel = m_msg->GetSize();
120 sizeDlg.y = 2*LAYOUT_Y_MARGIN + sizeLabel.y;
121
122 wxWindow *lastWindow = m_msg;
123
124 if ( maximum > 0 )
125 {
126 m_gauge = new wxGauge(this, -1, maximum,
127 wxDefaultPosition, wxDefaultSize,
128 wxGA_HORIZONTAL | wxRAISED_BORDER);
129 // Sorry, but wxGA_SMOOTH happens to also mean wxDIALOG_MODAL and will
130 // cause the dialog to be modal. Have an extra style argument to wxProgressDialog,
131 // perhaps.
132 // wxGA_HORIZONTAL | wxRAISED_BORDER | (style & wxGA_SMOOTH));
133 c = new wxLayoutConstraints;
134 c->left.SameAs(this, wxLeft, 2*LAYOUT_X_MARGIN);
135 c->top.Below(m_msg, 2*LAYOUT_Y_MARGIN);
136 c->right.SameAs(this, wxRight, 2*LAYOUT_X_MARGIN);
137 c->height.AsIs();
138 m_gauge->SetConstraints(c);
139 m_gauge->SetValue(0);
140 lastWindow = m_gauge;
141
142 wxSize sizeGauge = m_gauge->GetSize();
143 sizeDlg.y += 2*LAYOUT_Y_MARGIN + sizeGauge.y;
144 }
145 else
146 m_gauge = (wxGauge *)NULL;
147
148 // create the estimated/remaining/total time zones if requested
149 m_elapsed = m_estimated = m_remaining = (wxStaticText*)NULL;
150
151 int nTimeLabels = 0;
152 if ( style & wxPD_ELAPSED_TIME )
153 {
154 nTimeLabels++;
155
156 m_elapsed = CreateLabel(_("Elapsed time : "), &lastWindow);
157 }
158
159 if ( style & wxPD_ESTIMATED_TIME )
160 {
161 nTimeLabels++;
162
163 m_estimated = CreateLabel(_("Estimated time : "), &lastWindow);
164 }
165
166 if ( style & wxPD_REMAINING_TIME )
167 {
168 nTimeLabels++;
169
170 m_remaining = CreateLabel(_("Remaining time : "), &lastWindow);
171 }
172
173 if ( nTimeLabels > 0 )
174 {
175 // set it to the current time
176 m_timeStart = wxGetCurrentTime();
177 sizeDlg.y += nTimeLabels * (sizeLabel.y + LAYOUT_Y_MARGIN);
178 }
179
180 if ( hasAbortButton )
181 {
182 m_btnAbort = new wxButton(this, wxID_CANCEL, _("Cancel"));
183 c = new wxLayoutConstraints;
184
185 // Windows dialogs usually have buttons in the lower right corner
186 #ifdef __WXMSW__
187 c->right.SameAs(this, wxRight, 2*LAYOUT_X_MARGIN);
188 #else // !MSW
189 c->centreX.SameAs(this, wxCentreX);
190 #endif // MSW/!MSW
191 c->bottom.SameAs(this, wxBottom, 2*LAYOUT_Y_MARGIN);
192
193 wxSize sizeBtn = wxButton::GetDefaultSize();
194 c->width.Absolute(sizeBtn.x);
195 c->height.Absolute(sizeBtn.y);
196
197 m_btnAbort->SetConstraints(c);
198
199 sizeDlg.y += 2*LAYOUT_Y_MARGIN + sizeBtn.y;
200 }
201 else
202 m_btnAbort = (wxButton *)NULL;
203
204 SetAutoLayout(TRUE);
205 Layout();
206
207 sizeDlg.y += 2*LAYOUT_Y_MARGIN;
208
209 // try to make the dialog not square but rectangular of reasonabel width
210 sizeDlg.x = (wxCoord)wxMax(widthText, 4*sizeDlg.y/3);
211 sizeDlg.x *= 3;
212 sizeDlg.x /= 2;
213 SetClientSize(sizeDlg);
214
215 Centre(wxCENTER_FRAME | wxBOTH);
216
217 if ( style & wxPD_APP_MODAL )
218 {
219 m_winDisabler = new wxWindowDisabler(this);
220 }
221 else
222 {
223 if ( m_parentTop )
224 m_parentTop->Enable(FALSE);
225 m_winDisabler = NULL;
226 }
227
228 Show(TRUE);
229 Enable(TRUE); // enable this window
230
231 // Update the display (especially on X, GTK)
232 wxYield();
233
234 #ifdef __WXMAC__
235 MacUpdateImmediately();
236 #endif
237 }
238
239 wxStaticText *wxProgressDialog::CreateLabel(const wxString& text,
240 wxWindow **lastWindow)
241 {
242 wxLayoutConstraints *c;
243
244 wxStaticText *label = new wxStaticText(this, -1, _("unknown"));
245 c = new wxLayoutConstraints;
246
247 // VZ: I like the labels be centered - if the others don't mind, you may
248 // remove "#ifdef __WXMSW__" and use it for all ports
249 #ifdef __WXMSW__
250 c->left.SameAs(this, wxCentreX, LAYOUT_X_MARGIN);
251 #else // !MSW
252 c->right.SameAs(this, wxRight, 2*LAYOUT_X_MARGIN);
253 #endif // MSW/!MSW
254 c->top.Below(*lastWindow, LAYOUT_Y_MARGIN);
255 c->width.AsIs();
256 c->height.AsIs();
257 label->SetConstraints(c);
258
259 wxStaticText *dummy = new wxStaticText(this, -1, text);
260 c = new wxLayoutConstraints;
261 c->right.LeftOf(label);
262 c->top.SameAs(label, wxTop, 0);
263 c->width.AsIs();
264 c->height.AsIs();
265 dummy->SetConstraints(c);
266
267 *lastWindow = label;
268
269 return label;
270 }
271
272 bool
273 wxProgressDialog::Update(int value, const wxString& newmsg)
274 {
275 wxASSERT_MSG( value == -1 || m_gauge, wxT("cannot update non existent dialog") );
276 wxASSERT_MSG( value <= m_maximum, wxT("invalid progress value") );
277
278 if ( m_gauge )
279 m_gauge->SetValue(value + 1);
280
281 if ( !newmsg.IsEmpty() )
282 {
283 #ifdef __WXMSW__
284 // this seems to be necessary or garbage is left when the new label is
285 // longer than the old one
286 m_msg->SetLabel(wxEmptyString);
287 #endif // MSW
288
289 m_msg->SetLabel(newmsg);
290
291 wxYield();
292 }
293
294 if ( (m_elapsed || m_remaining || m_estimated) && (value != 0) )
295 {
296 unsigned long elapsed = wxGetCurrentTime() - m_timeStart;
297 unsigned long estimated = elapsed * m_maximum / value;
298 unsigned long remaining = estimated - elapsed;
299
300 SetTimeLabel(elapsed, m_elapsed);
301 SetTimeLabel(estimated, m_estimated);
302 SetTimeLabel(remaining, m_remaining);
303 }
304
305 if ( (value == m_maximum ) && !(GetWindowStyle() & wxPD_AUTO_HIDE) )
306 {
307 if ( m_btnAbort )
308 {
309 // tell the user what he should do...
310 m_btnAbort->SetLabel(_("Close"));
311 }
312
313 if ( !newmsg )
314 {
315 // also provide the finishing message if the application didn't
316 m_msg->SetLabel(_("Done."));
317 }
318
319 // so that we return TRUE below and that out [Cancel] handler knew what
320 // to do
321 m_state = Finished;
322
323 wxYield();
324
325 (void)ShowModal();
326 }
327 else
328 {
329 // update the display
330 wxYield();
331 }
332
333 #ifdef __WXMAC__
334 MacUpdateImmediately();
335 #endif
336
337 return m_state != Canceled;
338 }
339
340 // ----------------------------------------------------------------------------
341 // event handlers
342 // ----------------------------------------------------------------------------
343
344 void wxProgressDialog::OnCancel(wxCommandEvent& event)
345 {
346 if ( m_state == Finished )
347 {
348 // this means that the count down is already finished and we're being
349 // shown as a modal dialog - so just let the default handler do the job
350 event.Skip();
351 }
352 else
353 {
354 // request to cancel was received, the next time Update() is called we
355 // will handle it
356 m_state = Canceled;
357
358 // update the button state immediately so that the user knows that the
359 // request has been noticed
360 m_btnAbort->Disable();
361 }
362 }
363
364 void wxProgressDialog::OnClose(wxCloseEvent& event)
365 {
366 if ( m_state == Uncancelable )
367 {
368 // can't close this dialog
369 event.Veto(TRUE);
370 }
371 else if ( m_state == Finished )
372 {
373 // let the default handler close the window as we already terminated
374 event.Skip();
375 }
376 else
377 {
378 // next Update() will notice it
379 m_state = Canceled;
380 }
381 }
382
383 void wxProgressDialog::OnShow(wxShowEvent& event)
384 {
385 // if the dialog is being hidden, it was closed, so reenable other windows
386 // now
387 if ( event.GetShow() )
388 {
389 ReenableOtherWindows();
390 }
391 }
392
393 // ----------------------------------------------------------------------------
394 // destruction
395 // ----------------------------------------------------------------------------
396
397 wxProgressDialog::~wxProgressDialog()
398 {
399 // normally this should have been already done, but just in case
400 ReenableOtherWindows();
401 }
402
403 void wxProgressDialog::ReenableOtherWindows()
404 {
405 if ( GetWindowStyle() & wxPD_APP_MODAL )
406 {
407 delete m_winDisabler;
408 m_winDisabler = (wxWindowDisabler *)NULL;
409 }
410 else
411 {
412 if ( m_parentTop )
413 m_parentTop->Enable(TRUE);
414 }
415 }
416
417 // ----------------------------------------------------------------------------
418 // private functions
419 // ----------------------------------------------------------------------------
420
421 static void SetTimeLabel(unsigned long val, wxStaticText *label)
422 {
423 if ( label )
424 {
425 wxString s;
426 unsigned long hours = val / 3600;
427 unsigned long minutes = (val % 3600) / 60;
428 unsigned long seconds = val % 60;
429 s.Printf(wxT("%lu:%02lu:%02lu"), hours, minutes, seconds);
430
431 if ( s != label->GetLabel() )
432 label->SetLabel(s);
433 }
434 }
435
436 #endif // wxUSE_PROGRESSDLG