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