]> git.saurik.com Git - wxWidgets.git/blob - src/generic/progdlgg.cpp
Initial version of wxStyledTextCtrl, a Scintilla wrapper. There is
[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 | (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 if ( m_parentTop )
220 m_parentTop->Enable(FALSE);
221 }
222 else
223 {
224 wxEnableTopLevelWindows(FALSE);
225 }
226
227 Show(TRUE);
228 Enable(TRUE); // enable this window
229
230 // Update the display (especially on X, GTK)
231 wxYield();
232
233 #ifdef __WXMAC__
234 MacUpdateImmediately();
235 #endif
236 }
237
238 wxStaticText *wxProgressDialog::CreateLabel(const wxString& text,
239 wxWindow **lastWindow)
240 {
241 wxLayoutConstraints *c;
242
243 wxStaticText *label = new wxStaticText(this, -1, _("unknown"));
244 c = new wxLayoutConstraints;
245
246 // VZ: I like the labels be centered - if the others don't mind, you may
247 // remove "#ifdef __WXMSW__" and use it for all ports
248 #ifdef __WXMSW__
249 c->left.SameAs(this, wxCentreX, LAYOUT_X_MARGIN);
250 #else // !MSW
251 c->right.SameAs(this, wxRight, 2*LAYOUT_X_MARGIN);
252 #endif // MSW/!MSW
253 c->top.Below(*lastWindow, LAYOUT_Y_MARGIN);
254 c->width.AsIs();
255 c->height.AsIs();
256 label->SetConstraints(c);
257
258 wxStaticText *dummy = new wxStaticText(this, -1, text);
259 c = new wxLayoutConstraints;
260 c->right.LeftOf(label);
261 c->top.SameAs(label, wxTop, 0);
262 c->width.AsIs();
263 c->height.AsIs();
264 dummy->SetConstraints(c);
265
266 *lastWindow = label;
267
268 return label;
269 }
270
271 bool
272 wxProgressDialog::Update(int value, const wxString& newmsg)
273 {
274 wxASSERT_MSG( value == -1 || m_gauge, wxT("cannot update non existent dialog") );
275 wxASSERT_MSG( value <= m_maximum, wxT("invalid progress value") );
276
277 if ( m_gauge )
278 m_gauge->SetValue(value + 1);
279
280 if ( !newmsg.IsEmpty() )
281 {
282 #ifdef __WXMSW__
283 // this seems to be necessary or garbage is left when the new label is
284 // longer than the old one
285 m_msg->SetLabel(wxEmptyString);
286 #endif // MSW
287
288 m_msg->SetLabel(newmsg);
289
290 wxYield();
291 }
292
293 if ( (m_elapsed || m_remaining || m_estimated) && (value != 0) )
294 {
295 unsigned long elapsed = wxGetCurrentTime() - m_timeStart;
296 unsigned long estimated = elapsed * m_maximum / value;
297 unsigned long remaining = estimated - elapsed;
298
299 SetTimeLabel(elapsed, m_elapsed);
300 SetTimeLabel(estimated, m_estimated);
301 SetTimeLabel(remaining, m_remaining);
302 }
303
304 if ( (value == m_maximum ) && !(GetWindowStyle() & wxPD_AUTO_HIDE) )
305 {
306 if ( m_btnAbort )
307 {
308 // tell the user what he should do...
309 m_btnAbort->SetLabel(_("Close"));
310 }
311
312 if ( !newmsg )
313 {
314 // also provide the finishing message if the application didn't
315 m_msg->SetLabel(_("Done."));
316 }
317
318 // so that we return TRUE below and that out [Cancel] handler knew what
319 // to do
320 m_state = Finished;
321
322 wxYield();
323
324 (void)ShowModal();
325 }
326 else
327 {
328 // update the display
329 wxYield();
330 }
331
332 #ifdef __WXMAC__
333 MacUpdateImmediately();
334 #endif
335
336 return m_state != Canceled;
337 }
338
339 // ----------------------------------------------------------------------------
340 // event handlers
341 // ----------------------------------------------------------------------------
342
343 void wxProgressDialog::OnCancel(wxCommandEvent& event)
344 {
345 if ( m_state == Finished )
346 {
347 // this means that the count down is already finished and we're being
348 // shown as a modal dialog - so just let the default handler do the job
349 event.Skip();
350 }
351 else
352 {
353 // request to cancel was received, the next time Update() is called we
354 // will handle it
355 m_state = Canceled;
356
357 // update the button state immediately so that the user knows that the
358 // request has been noticed
359 m_btnAbort->Disable();
360 }
361 }
362
363 void wxProgressDialog::OnClose(wxCloseEvent& event)
364 {
365 if ( m_state == Uncancelable )
366 {
367 // can't close this dialog
368 event.Veto(TRUE);
369 }
370 else if ( m_state == Finished )
371 {
372 // let the default handler close the window as we already terminated
373 event.Skip();
374 }
375 else
376 {
377 // next Update() will notice it
378 m_state = Canceled;
379 }
380 }
381
382 // ----------------------------------------------------------------------------
383 // destruction
384 // ----------------------------------------------------------------------------
385
386 wxProgressDialog::~wxProgressDialog()
387 {
388 if ( !(GetWindowStyle() & wxPD_APP_MODAL) )
389 {
390 if ( m_parentTop )
391 m_parentTop->Enable(TRUE);
392 }
393 else
394 {
395 wxEnableTopLevelWindows(TRUE);
396 }
397 }
398
399 // ----------------------------------------------------------------------------
400 // private functions
401 // ----------------------------------------------------------------------------
402
403 static void SetTimeLabel(unsigned long val, wxStaticText *label)
404 {
405 if ( label )
406 {
407 wxString s;
408 unsigned long hours = val / 3600;
409 unsigned long minutes = (val % 3600) / 60;
410 unsigned long seconds = val % 60;
411 s.Printf(wxT("%lu:%02lu:%02lu"), hours, minutes, seconds);
412
413 if ( s != label->GetLabel() )
414 label->SetLabel(s);
415 }
416 }
417
418 #endif // wxUSE_PROGRESSDLG