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