]> git.saurik.com Git - wxWidgets.git/blob - src/generic/progdlgg.cpp
removed redundant InitDialog methods
[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
96 #ifdef __WXMSW__
97 // we have to remove the "Close" button from the title bar then as it is
98 // confusing to have it - it doesn't work anyhow
99 //
100 // FIXME: should probably have a (extended?) window style for this
101 if ( !hasAbortButton )
102 {
103 EnableCloseButton(FALSE);
104 }
105 #endif // wxMSW
106
107 m_state = hasAbortButton ? Continue : Uncancelable;
108 m_maximum = maximum;
109
110 m_parentTop = parent;
111 while ( m_parentTop && m_parentTop->GetParent() )
112 {
113 m_parentTop = m_parentTop->GetParent();
114 }
115
116 wxLayoutConstraints *c;
117
118 wxClientDC dc(this);
119 dc.SetFont(wxSystemSettings::GetSystemFont(wxSYS_DEFAULT_GUI_FONT));
120 long widthText;
121 dc.GetTextExtent(message, &widthText, NULL, NULL, NULL, NULL);
122
123 m_msg = new wxStaticText(this, -1, message);
124 c = new wxLayoutConstraints;
125 c->left.SameAs(this, wxLeft, 2*LAYOUT_X_MARGIN);
126 c->top.SameAs(this, wxTop, 2*LAYOUT_Y_MARGIN);
127 c->width.AsIs();
128 c->height.AsIs();
129 m_msg->SetConstraints(c);
130
131 wxSize sizeDlg, sizeLabel = m_msg->GetSize();
132 sizeDlg.y = 2*LAYOUT_Y_MARGIN + sizeLabel.y;
133
134 wxWindow *lastWindow = m_msg;
135
136 if ( maximum > 0 )
137 {
138 // note that we can't use wxGA_SMOOTH because it happens to also mean
139 // wxDIALOG_MODAL and will cause the dialog to be modal. Have an extra
140 // style argument to wxProgressDialog, perhaps.
141 m_gauge = new wxGauge(this, -1, maximum,
142 wxDefaultPosition, wxDefaultSize,
143 wxGA_HORIZONTAL);
144
145 c = new wxLayoutConstraints;
146 c->left.SameAs(this, wxLeft, 2*LAYOUT_X_MARGIN);
147 c->top.Below(m_msg, 2*LAYOUT_Y_MARGIN);
148 c->right.SameAs(this, wxRight, 2*LAYOUT_X_MARGIN);
149 c->height.AsIs();
150 m_gauge->SetConstraints(c);
151 m_gauge->SetValue(0);
152 lastWindow = m_gauge;
153
154 wxSize sizeGauge = m_gauge->GetSize();
155 sizeDlg.y += 2*LAYOUT_Y_MARGIN + sizeGauge.y;
156 }
157 else
158 m_gauge = (wxGauge *)NULL;
159
160 // create the estimated/remaining/total time zones if requested
161 m_elapsed = m_estimated = m_remaining = (wxStaticText*)NULL;
162
163 int nTimeLabels = 0;
164 if ( style & wxPD_ELAPSED_TIME )
165 {
166 nTimeLabels++;
167
168 m_elapsed = CreateLabel(_("Elapsed time : "), &lastWindow);
169 }
170
171 if ( style & wxPD_ESTIMATED_TIME )
172 {
173 nTimeLabels++;
174
175 m_estimated = CreateLabel(_("Estimated time : "), &lastWindow);
176 }
177
178 if ( style & wxPD_REMAINING_TIME )
179 {
180 nTimeLabels++;
181
182 m_remaining = CreateLabel(_("Remaining time : "), &lastWindow);
183 }
184
185 if ( nTimeLabels > 0 )
186 {
187 // set it to the current time
188 m_timeStart = wxGetCurrentTime();
189 sizeDlg.y += nTimeLabels * (sizeLabel.y + LAYOUT_Y_MARGIN);
190 }
191
192 if ( hasAbortButton )
193 {
194 m_btnAbort = new wxButton(this, wxID_CANCEL, _("Cancel"));
195 c = new wxLayoutConstraints;
196
197 // Windows dialogs usually have buttons in the lower right corner
198 #ifdef __WXMSW__
199 c->right.SameAs(this, wxRight, 2*LAYOUT_X_MARGIN);
200 #else // !MSW
201 c->centreX.SameAs(this, wxCentreX);
202 #endif // MSW/!MSW
203 c->bottom.SameAs(this, wxBottom, 2*LAYOUT_Y_MARGIN);
204
205 wxSize sizeBtn = wxButton::GetDefaultSize();
206 c->width.Absolute(sizeBtn.x);
207 c->height.Absolute(sizeBtn.y);
208
209 m_btnAbort->SetConstraints(c);
210
211 sizeDlg.y += 2*LAYOUT_Y_MARGIN + sizeBtn.y;
212 }
213 else
214 m_btnAbort = (wxButton *)NULL;
215
216 SetAutoLayout(TRUE);
217 Layout();
218
219 sizeDlg.y += 2*LAYOUT_Y_MARGIN;
220
221 // try to make the dialog not square but rectangular of reasonabel width
222 sizeDlg.x = (wxCoord)wxMax(widthText, 4*sizeDlg.y/3);
223 sizeDlg.x *= 3;
224 sizeDlg.x /= 2;
225 SetClientSize(sizeDlg);
226
227 Centre(wxCENTER_FRAME | wxBOTH);
228
229 if ( style & wxPD_APP_MODAL )
230 {
231 m_winDisabler = new wxWindowDisabler(this);
232 }
233 else
234 {
235 if ( m_parentTop )
236 m_parentTop->Enable(FALSE);
237 m_winDisabler = NULL;
238 }
239
240 Show(TRUE);
241 Enable(TRUE); // enable this window
242
243 // this one can be initialized even if the others are unknown for now
244 //
245 // NB: do it after calling Layout() to keep the labels correctly aligned
246 if ( m_elapsed )
247 {
248 SetTimeLabel(0, m_elapsed);
249 }
250
251 // Update the display (especially on X, GTK)
252 wxYield();
253
254 #ifdef __WXMAC__
255 MacUpdateImmediately();
256 #endif
257 }
258
259 wxStaticText *wxProgressDialog::CreateLabel(const wxString& text,
260 wxWindow **lastWindow)
261 {
262 wxLayoutConstraints *c;
263
264 wxStaticText *label = new wxStaticText(this, -1, _("unknown"));
265 c = new wxLayoutConstraints;
266
267 // VZ: I like the labels be centered - if the others don't mind, you may
268 // remove "#ifdef __WXMSW__" and use it for all ports
269 #ifdef __WXMSW__
270 c->left.SameAs(this, wxCentreX, LAYOUT_X_MARGIN);
271 #else // !MSW
272 c->right.SameAs(this, wxRight, 2*LAYOUT_X_MARGIN);
273 #endif // MSW/!MSW
274 c->top.Below(*lastWindow, LAYOUT_Y_MARGIN);
275 c->width.AsIs();
276 c->height.AsIs();
277 label->SetConstraints(c);
278
279 wxStaticText *dummy = new wxStaticText(this, -1, text);
280 c = new wxLayoutConstraints;
281 c->right.LeftOf(label);
282 c->top.SameAs(label, wxTop, 0);
283 c->width.AsIs();
284 c->height.AsIs();
285 dummy->SetConstraints(c);
286
287 *lastWindow = label;
288
289 return label;
290 }
291
292 bool
293 wxProgressDialog::Update(int value, const wxString& newmsg)
294 {
295 wxASSERT_MSG( value == -1 || m_gauge, wxT("cannot update non existent dialog") );
296 wxASSERT_MSG( value <= m_maximum, wxT("invalid progress value") );
297
298 if ( m_gauge )
299 m_gauge->SetValue(value + 1);
300
301 if ( !newmsg.IsEmpty() )
302 {
303 #ifdef __WXMSW__
304 // this seems to be necessary or garbage is left when the new label is
305 // longer than the old one
306 m_msg->SetLabel(wxEmptyString);
307 #endif // MSW
308
309 m_msg->SetLabel(newmsg);
310
311 wxYield();
312 }
313
314 if ( (m_elapsed || m_remaining || m_estimated) && (value != 0) )
315 {
316 unsigned long elapsed = wxGetCurrentTime() - m_timeStart;
317 unsigned long estimated = elapsed * m_maximum / value;
318 unsigned long remaining = estimated - elapsed;
319
320 SetTimeLabel(elapsed, m_elapsed);
321 SetTimeLabel(estimated, m_estimated);
322 SetTimeLabel(remaining, m_remaining);
323 }
324
325 if ( (value == m_maximum ) && !(GetWindowStyle() & wxPD_AUTO_HIDE) )
326 {
327 if ( m_btnAbort )
328 {
329 // tell the user what he should do...
330 m_btnAbort->SetLabel(_("Close"));
331 }
332 #ifdef __WXMSW__
333 else // enable the close button to give the user a way to close the dlg
334 {
335 EnableCloseButton(TRUE);
336 }
337 #endif // __WXMSW__
338
339 if ( !newmsg )
340 {
341 // also provide the finishing message if the application didn't
342 m_msg->SetLabel(_("Done."));
343 }
344
345 // so that we return TRUE below and that out [Cancel] handler knew what
346 // to do
347 m_state = Finished;
348
349 wxYield();
350
351 (void)ShowModal();
352 }
353 else
354 {
355 // update the display
356 wxYield();
357 }
358
359 #ifdef __WXMAC__
360 MacUpdateImmediately();
361 #endif
362
363 return m_state != Canceled;
364 }
365
366 // ----------------------------------------------------------------------------
367 // event handlers
368 // ----------------------------------------------------------------------------
369
370 void wxProgressDialog::OnCancel(wxCommandEvent& event)
371 {
372 if ( m_state == Finished )
373 {
374 // this means that the count down is already finished and we're being
375 // shown as a modal dialog - so just let the default handler do the job
376 event.Skip();
377 }
378 else
379 {
380 // request to cancel was received, the next time Update() is called we
381 // will handle it
382 m_state = Canceled;
383
384 // update the button state immediately so that the user knows that the
385 // request has been noticed
386 m_btnAbort->Disable();
387 }
388 }
389
390 void wxProgressDialog::OnClose(wxCloseEvent& event)
391 {
392 if ( m_state == Uncancelable )
393 {
394 // can't close this dialog
395 event.Veto(TRUE);
396 }
397 else if ( m_state == Finished )
398 {
399 // let the default handler close the window as we already terminated
400 event.Skip();
401 }
402 else
403 {
404 // next Update() will notice it
405 m_state = Canceled;
406 }
407 }
408
409 void wxProgressDialog::OnShow(wxShowEvent& event)
410 {
411 // if the dialog is being hidden, it was closed, so reenable other windows
412 // now
413 if ( event.GetShow() )
414 {
415 ReenableOtherWindows();
416 }
417 }
418
419 // ----------------------------------------------------------------------------
420 // destruction
421 // ----------------------------------------------------------------------------
422
423 wxProgressDialog::~wxProgressDialog()
424 {
425 // normally this should have been already done, but just in case
426 ReenableOtherWindows();
427 }
428
429 void wxProgressDialog::ReenableOtherWindows()
430 {
431 if ( GetWindowStyle() & wxPD_APP_MODAL )
432 {
433 delete m_winDisabler;
434 m_winDisabler = (wxWindowDisabler *)NULL;
435 }
436 else
437 {
438 if ( m_parentTop )
439 m_parentTop->Enable(TRUE);
440 }
441 }
442
443 // ----------------------------------------------------------------------------
444 // private functions
445 // ----------------------------------------------------------------------------
446
447 static void SetTimeLabel(unsigned long val, wxStaticText *label)
448 {
449 if ( label )
450 {
451 wxString s;
452 unsigned long hours = val / 3600;
453 unsigned long minutes = (val % 3600) / 60;
454 unsigned long seconds = val % 60;
455 s.Printf(wxT("%lu:%02lu:%02lu"), hours, minutes, seconds);
456
457 if ( s != label->GetLabel() )
458 label->SetLabel(s);
459 }
460 }
461
462 #endif // wxUSE_PROGRESSDLG