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