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