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