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