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