]> git.saurik.com Git - wxWidgets.git/blob - src/generic/progdlgg.cpp
Restore the max printf arguments to 64.
[wxWidgets.git] / src / generic / progdlgg.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/generic/progdlgg.cpp
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 licence
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 #if wxUSE_PROGRESSDLG
28
29 #ifndef WX_PRECOMP
30 #include "wx/utils.h"
31 #include "wx/frame.h"
32 #include "wx/button.h"
33 #include "wx/stattext.h"
34 #include "wx/sizer.h"
35 #include "wx/event.h"
36 #include "wx/gauge.h"
37 #include "wx/intl.h"
38 #include "wx/dcclient.h"
39 #include "wx/timer.h"
40 #include "wx/settings.h"
41 #endif
42
43 #include "wx/progdlg.h"
44
45 // ---------------------------------------------------------------------------
46 // macros
47 // ---------------------------------------------------------------------------
48
49 /* Macro for avoiding #ifdefs when value have to be different depending on size of
50 device we display on - take it from something like wxDesktopPolicy in the future
51 */
52
53 #if defined(__SMARTPHONE__)
54 #define wxLARGESMALL(large,small) small
55 #else
56 #define wxLARGESMALL(large,small) large
57 #endif
58
59 // ----------------------------------------------------------------------------
60 // constants
61 // ----------------------------------------------------------------------------
62
63 #define LAYOUT_MARGIN wxLARGESMALL(8,2)
64
65 static const int wxID_SKIP = 32000; // whatever
66
67 // ----------------------------------------------------------------------------
68 // private functions
69 // ----------------------------------------------------------------------------
70
71 // update the label to show the given time (in seconds)
72 static void SetTimeLabel(unsigned long val, wxStaticText *label);
73
74 // ----------------------------------------------------------------------------
75 // event tables
76 // ----------------------------------------------------------------------------
77
78 BEGIN_EVENT_TABLE(wxProgressDialog, wxDialog)
79 EVT_BUTTON(wxID_CANCEL, wxProgressDialog::OnCancel)
80 EVT_BUTTON(wxID_SKIP, wxProgressDialog::OnSkip)
81
82 EVT_CLOSE(wxProgressDialog::OnClose)
83 END_EVENT_TABLE()
84
85 IMPLEMENT_CLASS(wxProgressDialog, wxDialog)
86
87 // ============================================================================
88 // wxProgressDialog implementation
89 // ============================================================================
90
91 // ----------------------------------------------------------------------------
92 // wxProgressDialog creation
93 // ----------------------------------------------------------------------------
94
95 wxProgressDialog::wxProgressDialog(wxString const &title,
96 wxString const &message,
97 int maximum,
98 wxWindow *parent,
99 int style)
100 : wxDialog(parent, wxID_ANY, title),
101 m_skip(false),
102 m_delay(3),
103 m_hasAbortButton(false),
104 m_hasSkipButton(false)
105 {
106 // we may disappear at any moment, let the others know about it
107 SetExtraStyle(GetExtraStyle() | wxWS_EX_TRANSIENT);
108 m_windowStyle |= style;
109
110 m_hasAbortButton = (style & wxPD_CAN_ABORT) != 0;
111 m_hasSkipButton = (style & wxPD_CAN_SKIP) != 0;
112
113 bool isPda = (wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA);
114
115 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
116 // we have to remove the "Close" button from the title bar then as it is
117 // confusing to have it - it doesn't work anyhow
118 //
119 // FIXME: should probably have a (extended?) window style for this
120 if ( !m_hasAbortButton )
121 {
122 EnableCloseButton(false);
123 }
124 #endif // wxMSW
125
126 #if defined(__SMARTPHONE__)
127 SetLeftMenu();
128 #endif
129
130 m_state = m_hasAbortButton ? Continue : Uncancelable;
131 m_maximum = maximum;
132
133 #if defined(__WXMSW__) || defined(__WXPM__)
134 // we can't have values > 65,536 in the progress control under Windows, so
135 // scale everything down
136 m_factor = m_maximum / 65536 + 1;
137 m_maximum /= m_factor;
138 #endif // __WXMSW__
139
140 m_parentTop = wxGetTopLevelParent(parent);
141
142 wxClientDC dc(this);
143 dc.SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT));
144 long widthText = 0;
145 dc.GetTextExtent(message, &widthText, NULL, NULL, NULL, NULL);
146
147 wxBoxSizer *sizer = new wxBoxSizer(wxVERTICAL);
148
149 m_msg = new wxStaticText(this, wxID_ANY, message);
150 sizer->Add(m_msg, 0, wxLEFT | wxTOP, 2*LAYOUT_MARGIN);
151
152 wxSize sizeDlg,
153 sizeLabel = m_msg->GetSize();
154 sizeDlg.y = 2*LAYOUT_MARGIN + sizeLabel.y;
155
156 if ( maximum > 0 )
157 {
158 int gauge_style = wxGA_HORIZONTAL;
159 if ( ( style & wxPD_SMOOTH ) == wxPD_SMOOTH )
160 gauge_style |= wxGA_SMOOTH;
161 m_gauge = new wxGauge(this, wxID_ANY, m_maximum,
162 wxDefaultPosition, wxDefaultSize,
163 gauge_style );
164
165 sizer->Add(m_gauge, 0, wxLEFT | wxRIGHT | wxTOP | wxEXPAND, 2*LAYOUT_MARGIN);
166 m_gauge->SetValue(0);
167
168 wxSize sizeGauge = m_gauge->GetSize();
169 sizeDlg.y += 2*LAYOUT_MARGIN + sizeGauge.y;
170 }
171 else
172 m_gauge = (wxGauge *)NULL;
173
174 // create the estimated/remaining/total time zones if requested
175 m_elapsed = m_estimated = m_remaining = (wxStaticText*)NULL;
176 m_display_estimated = m_last_timeupdate = m_break = 0;
177 m_ctdelay = 0;
178
179 // if we are going to have at least one label, remmeber it in this var
180 wxStaticText *label = NULL;
181
182 // also count how many labels we really have
183 size_t nTimeLabels = 0;
184
185 if ( style & wxPD_ELAPSED_TIME )
186 {
187 nTimeLabels++;
188
189 label =
190 m_elapsed = CreateLabel(_("Elapsed time : "), sizer);
191 }
192
193 if ( style & wxPD_ESTIMATED_TIME )
194 {
195 nTimeLabels++;
196
197 label =
198 m_estimated = CreateLabel(_("Estimated time : "), sizer);
199 }
200
201 if ( style & wxPD_REMAINING_TIME )
202 {
203 nTimeLabels++;
204
205 label =
206 m_remaining = CreateLabel(_("Remaining time : "), sizer);
207 }
208
209 if ( nTimeLabels > 0 )
210 {
211 // set it to the current time
212 m_timeStart = wxGetCurrentTime();
213 sizeDlg.y += nTimeLabels * (label->GetSize().y + LAYOUT_MARGIN);
214 }
215
216 #if defined(__SMARTPHONE__)
217 if ( m_hasSkipButton )
218 SetRightMenu(wxID_SKIP, _("Skip"));
219 if ( m_hasAbortButton )
220 SetLeftMenu(wxID_CANCEL);
221 #else
222 m_btnAbort = m_btnSkip = (wxButton *)NULL;
223 bool sizeDlgModified = false;
224 wxBoxSizer *buttonSizer = new wxBoxSizer(wxHORIZONTAL);
225
226 const int sizerFlags =
227 #if defined(__WXMSW__) || defined(__WXPM__)
228 wxALIGN_RIGHT | wxALL
229 #else // !MSW
230 wxALIGN_CENTER_HORIZONTAL | wxBOTTOM | wxTOP
231 #endif // MSW/!MSW
232 ;
233
234 if ( m_hasSkipButton )
235 {
236 m_btnSkip = new wxButton(this, wxID_SKIP, _("Skip"));
237
238 // Windows dialogs usually have buttons in the lower right corner
239 buttonSizer->Add(m_btnSkip, 0, sizerFlags, LAYOUT_MARGIN);
240 sizeDlg.y += 2*LAYOUT_MARGIN + wxButton::GetDefaultSize().y;
241 sizeDlgModified = true;
242 }
243
244 if ( m_hasAbortButton )
245 {
246 m_btnAbort = new wxButton(this, wxID_CANCEL);
247
248 // Windows dialogs usually have buttons in the lower right corner
249 buttonSizer->Add(m_btnAbort, 0, sizerFlags, LAYOUT_MARGIN);
250 if(!sizeDlgModified)
251 sizeDlg.y += 2*LAYOUT_MARGIN + wxButton::GetDefaultSize().y;
252 }
253
254 sizer->Add(buttonSizer, 0, sizerFlags, LAYOUT_MARGIN );
255 #endif // __SMARTPHONE__/!__SMARTPHONE__
256
257 SetSizerAndFit(sizer);
258
259 if (!isPda)
260 {
261 sizeDlg.y += 2*LAYOUT_MARGIN;
262
263 // try to make the dialog not square but rectangular of reasonable width
264 sizeDlg.x = (wxCoord)wxMax(widthText, 4*sizeDlg.y/3);
265 sizeDlg.x *= 3;
266 sizeDlg.x /= 2;
267 SetClientSize(sizeDlg);
268 }
269
270 Centre(wxCENTER_FRAME | wxBOTH);
271
272 if ( style & wxPD_APP_MODAL )
273 {
274 m_winDisabler = new wxWindowDisabler(this);
275 }
276 else
277 {
278 if ( m_parentTop )
279 m_parentTop->Disable();
280 m_winDisabler = NULL;
281 }
282
283 Show();
284 Enable();
285
286 // this one can be initialized even if the others are unknown for now
287 //
288 // NB: do it after calling Layout() to keep the labels correctly aligned
289 if ( m_elapsed )
290 {
291 SetTimeLabel(0, m_elapsed);
292 }
293
294 Update();
295 }
296
297 wxStaticText *wxProgressDialog::CreateLabel(const wxString& text,
298 wxSizer *sizer)
299 {
300 wxBoxSizer *locsizer = new wxBoxSizer(wxLARGESMALL(wxHORIZONTAL,wxVERTICAL));
301
302 wxStaticText *dummy = new wxStaticText(this, wxID_ANY, text);
303 wxStaticText *label = new wxStaticText(this, wxID_ANY, _("unknown"));
304
305 // select placement most native or nice on target GUI
306 #if defined(__SMARTPHONE__)
307 // label and time to the left in two rows
308 locsizer->Add(dummy, 1, wxALIGN_LEFT);
309 locsizer->Add(label, 1, wxALIGN_LEFT);
310 sizer->Add(locsizer, 0, wxALIGN_LEFT | wxTOP | wxLEFT, LAYOUT_MARGIN);
311 #elif defined(__WXMSW__) || defined(__WXPM__) || defined(__WXMAC__) || defined(__WXGTK20__)
312 // label and time centered in one row
313 locsizer->Add(dummy, 1, wxLARGESMALL(wxALIGN_RIGHT,wxALIGN_LEFT));
314 locsizer->Add(label, 1, wxALIGN_LEFT | wxLEFT, LAYOUT_MARGIN);
315 sizer->Add(locsizer, 0, wxALIGN_CENTER_HORIZONTAL | wxTOP, LAYOUT_MARGIN);
316 #else
317 // label and time to the right in one row
318 sizer->Add(locsizer, 0, wxALIGN_RIGHT | wxRIGHT | wxTOP, LAYOUT_MARGIN);
319 locsizer->Add(dummy);
320 locsizer->Add(label, 0, wxLEFT, LAYOUT_MARGIN);
321 #endif
322
323 return label;
324 }
325
326 // ----------------------------------------------------------------------------
327 // wxProgressDialog operations
328 // ----------------------------------------------------------------------------
329
330 bool
331 wxProgressDialog::Update(int value, const wxString& newmsg, bool *skip)
332 {
333 wxASSERT_MSG( value == -1 || m_gauge, wxT("cannot update non existent dialog") );
334
335 #ifdef __WXMSW__
336 value /= m_factor;
337 #endif // __WXMSW__
338
339 wxASSERT_MSG( value <= m_maximum, wxT("invalid progress value") );
340
341 // fill up the gauge if value == maximum because this means that the dialog
342 // is going to close and the gauge shouldn't be partly empty in this case
343 if ( m_gauge && value <= m_maximum )
344 {
345 m_gauge->SetValue(value == m_maximum ? value : value + 1);
346 }
347
348 UpdateMessage(newmsg);
349
350 if ( (m_elapsed || m_remaining || m_estimated) && (value != 0) )
351 {
352 unsigned long elapsed = wxGetCurrentTime() - m_timeStart;
353 if ( m_last_timeupdate < elapsed
354 || value == m_maximum
355 )
356 {
357 m_last_timeupdate = elapsed;
358 unsigned long estimated = m_break +
359 (unsigned long)(( (double) (elapsed-m_break) * m_maximum ) / ((double)value)) ;
360 if ( estimated > m_display_estimated
361 && m_ctdelay >= 0
362 )
363 {
364 ++m_ctdelay;
365 }
366 else if ( estimated < m_display_estimated
367 && m_ctdelay <= 0
368 )
369 {
370 --m_ctdelay;
371 }
372 else
373 {
374 m_ctdelay = 0;
375 }
376 if ( m_ctdelay >= m_delay // enough confirmations for a higher value
377 || m_ctdelay <= (m_delay*-1) // enough confirmations for a lower value
378 || value == m_maximum // to stay consistent
379 || elapsed > m_display_estimated // to stay consistent
380 || ( elapsed > 0 && elapsed < 4 ) // additional updates in the beginning
381 )
382 {
383 m_display_estimated = estimated;
384 m_ctdelay = 0;
385 }
386 }
387
388 long display_remaining = m_display_estimated - elapsed;
389 if ( display_remaining < 0 )
390 {
391 display_remaining = 0;
392 }
393
394 SetTimeLabel(elapsed, m_elapsed);
395 SetTimeLabel(m_display_estimated, m_estimated);
396 SetTimeLabel(display_remaining, m_remaining);
397 }
398
399 if ( value == m_maximum )
400 {
401 if ( m_state == Finished )
402 {
403 // ignore multiple calls to Update(m_maximum): it may sometimes be
404 // troublesome to ensure that Update() is not called twice with the
405 // same value (e.g. because of the rounding errors) and if we don't
406 // return now we're going to generate asserts below
407 return true;
408 }
409
410 // so that we return true below and that out [Cancel] handler knew what
411 // to do
412 m_state = Finished;
413 if( !(GetWindowStyle() & wxPD_AUTO_HIDE) )
414 {
415 EnableClose();
416 DisableSkip();
417 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
418 EnableCloseButton();
419 #endif // __WXMSW__
420
421 if ( newmsg.empty() )
422 {
423 // also provide the finishing message if the application didn't
424 m_msg->SetLabel(_("Done."));
425 }
426
427 wxYieldIfNeeded() ;
428
429 (void)ShowModal();
430 }
431 else // auto hide
432 {
433 // reenable other windows before hiding this one because otherwise
434 // Windows wouldn't give the focus back to the window which had
435 // been previously focused because it would still be disabled
436 ReenableOtherWindows();
437
438 Hide();
439 }
440 }
441 else // not at maximum yet
442 {
443 return DoAfterUpdate(skip);
444 }
445
446 // update the display in case yielding above didn't do it
447 Update();
448
449 return m_state != Canceled;
450 }
451
452 bool wxProgressDialog::Pulse(const wxString& newmsg, bool *skip)
453 {
454 wxASSERT_MSG( m_gauge, wxT("cannot update non existent dialog") );
455
456 // show a bit of progress
457 m_gauge->Pulse();
458
459 UpdateMessage(newmsg);
460
461 if (m_elapsed || m_remaining || m_estimated)
462 {
463 unsigned long elapsed = wxGetCurrentTime() - m_timeStart;
464
465 SetTimeLabel(elapsed, m_elapsed);
466 SetTimeLabel((unsigned long)-1, m_estimated);
467 SetTimeLabel((unsigned long)-1, m_remaining);
468 }
469
470 return DoAfterUpdate(skip);
471 }
472
473 bool wxProgressDialog::DoAfterUpdate(bool *skip)
474 {
475 // we have to yield because not only we want to update the display but
476 // also to process the clicks on the cancel and skip buttons
477 wxYieldIfNeeded();
478
479 Update();
480
481 if ( m_skip && skip && !*skip )
482 {
483 *skip = true;
484 m_skip = false;
485 EnableSkip();
486 }
487
488 return m_state != Canceled;
489 }
490
491 void wxProgressDialog::Resume()
492 {
493 m_state = Continue;
494 m_ctdelay = m_delay; // force an update of the elapsed/estimated/remaining time
495 m_break += wxGetCurrentTime()-m_timeStop;
496
497 EnableAbort();
498 EnableSkip();
499 m_skip = false;
500 }
501
502 bool wxProgressDialog::Show( bool show )
503 {
504 // reenable other windows before hiding this one because otherwise
505 // Windows wouldn't give the focus back to the window which had
506 // been previously focused because it would still be disabled
507 if(!show)
508 ReenableOtherWindows();
509
510 return wxDialog::Show(show);
511 }
512
513 // ----------------------------------------------------------------------------
514 // event handlers
515 // ----------------------------------------------------------------------------
516
517 void wxProgressDialog::OnCancel(wxCommandEvent& event)
518 {
519 if ( m_state == Finished )
520 {
521 // this means that the count down is already finished and we're being
522 // shown as a modal dialog - so just let the default handler do the job
523 event.Skip();
524 }
525 else
526 {
527 // request to cancel was received, the next time Update() is called we
528 // will handle it
529 m_state = Canceled;
530
531 // update the buttons state immediately so that the user knows that the
532 // request has been noticed
533 DisableAbort();
534 DisableSkip();
535
536 // save the time when the dialog was stopped
537 m_timeStop = wxGetCurrentTime();
538 }
539 }
540
541 void wxProgressDialog::OnSkip(wxCommandEvent& WXUNUSED(event))
542 {
543 DisableSkip();
544 m_skip = true;
545 }
546
547 void wxProgressDialog::OnClose(wxCloseEvent& event)
548 {
549 if ( m_state == Uncancelable )
550 {
551 // can't close this dialog
552 event.Veto();
553 }
554 else if ( m_state == Finished )
555 {
556 // let the default handler close the window as we already terminated
557 event.Skip();
558 }
559 else
560 {
561 // next Update() will notice it
562 m_state = Canceled;
563 DisableAbort();
564 DisableSkip();
565
566 m_timeStop = wxGetCurrentTime();
567 }
568 }
569
570 // ----------------------------------------------------------------------------
571 // destruction
572 // ----------------------------------------------------------------------------
573
574 wxProgressDialog::~wxProgressDialog()
575 {
576 // normally this should have been already done, but just in case
577 ReenableOtherWindows();
578 }
579
580 void wxProgressDialog::ReenableOtherWindows()
581 {
582 if ( GetWindowStyle() & wxPD_APP_MODAL )
583 {
584 delete m_winDisabler;
585 m_winDisabler = (wxWindowDisabler *)NULL;
586 }
587 else
588 {
589 if ( m_parentTop )
590 m_parentTop->Enable();
591 }
592 }
593
594 // ----------------------------------------------------------------------------
595 // private functions
596 // ----------------------------------------------------------------------------
597
598 static void SetTimeLabel(unsigned long val, wxStaticText *label)
599 {
600 if ( label )
601 {
602 wxString s;
603
604 if (val != (unsigned long)-1)
605 {
606 unsigned long hours = val / 3600;
607 unsigned long minutes = (val % 3600) / 60;
608 unsigned long seconds = val % 60;
609 s.Printf(wxT("%lu:%02lu:%02lu"), hours, minutes, seconds);
610 }
611 else
612 {
613 s = _("Unknown");
614 }
615
616 if ( s != label->GetLabel() )
617 label->SetLabel(s);
618 }
619 }
620
621 void wxProgressDialog::EnableSkip(bool enable)
622 {
623 if(m_hasSkipButton)
624 {
625 #ifdef __SMARTPHONE__
626 if(enable)
627 SetRightMenu(wxID_SKIP, _("Skip"));
628 else
629 SetRightMenu();
630 #else
631 if(m_btnSkip)
632 m_btnSkip->Enable(enable);
633 #endif
634 }
635 }
636
637 void wxProgressDialog::EnableAbort(bool enable)
638 {
639 if(m_hasAbortButton)
640 {
641 #ifdef __SMARTPHONE__
642 if(enable)
643 SetLeftMenu(wxID_CANCEL); // stock buttons makes Cancel label
644 else
645 SetLeftMenu();
646 #else
647 if(m_btnAbort)
648 m_btnAbort->Enable(enable);
649 #endif
650 }
651 }
652
653 void wxProgressDialog::EnableClose()
654 {
655 if(m_hasAbortButton)
656 {
657 #ifdef __SMARTPHONE__
658 SetLeftMenu(wxID_CANCEL, _("Close"));
659 #else
660 if(m_btnAbort)
661 {
662 m_btnAbort->Enable();
663 m_btnAbort->SetLabel(_("Close"));
664 }
665 #endif
666 }
667 }
668
669 void wxProgressDialog::UpdateMessage(const wxString &newmsg)
670 {
671 if ( !newmsg.empty() && newmsg != m_msg->GetLabel() )
672 {
673 m_msg->SetLabel(newmsg);
674
675 wxYieldIfNeeded() ;
676 }
677 }
678
679 #endif // wxUSE_PROGRESSDLG