]> git.saurik.com Git - wxWidgets.git/blob - src/generic/progdlgg.cpp
pen and brush are platform neutral on osx
[wxWidgets.git] / src / generic / progdlgg.cpp
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 #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(const wxString& title,
96 const wxString& message,
97 int maximum,
98 wxWindow *parent,
99 int style)
100 : wxDialog(GetParentForModalDialog(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 #if defined(__WXMSW__) && !defined(__WXUNIVERSAL__)
114 // we have to remove the "Close" button from the title bar then as it is
115 // confusing to have it - it doesn't work anyhow
116 //
117 // FIXME: should probably have a (extended?) window style for this
118 if ( !m_hasAbortButton )
119 {
120 EnableCloseButton(false);
121 }
122 #endif // wxMSW
123
124 #if defined(__SMARTPHONE__)
125 SetLeftMenu();
126 #endif
127
128 m_state = m_hasAbortButton ? Continue : Uncancelable;
129 m_maximum = maximum;
130
131 #if defined(__WXMSW__) || defined(__WXPM__)
132 // we can't have values > 65,536 in the progress control under Windows, so
133 // scale everything down
134 m_factor = m_maximum / 65536 + 1;
135 m_maximum /= m_factor;
136 #endif // __WXMSW__
137
138 m_parentTop = wxGetTopLevelParent(parent);
139
140 wxClientDC dc(this);
141 dc.SetFont(wxSystemSettings::GetFont(wxSYS_DEFAULT_GUI_FONT));
142 wxCoord widthText = 0;
143 dc.GetTextExtent(message, &widthText, NULL, NULL, NULL, NULL);
144
145 // top-level sizerTop
146 wxSizer * const sizerTop = new wxBoxSizer(wxVERTICAL);
147
148 m_msg = new wxStaticText(this, wxID_ANY, message);
149 sizerTop->Add(m_msg, 0, wxLEFT | wxTOP, 2*LAYOUT_MARGIN);
150
151 wxSize sizeLabel = m_msg->GetSize();
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
284 wxStaticText *
285 wxProgressDialog::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
312 bool
313 wxProgressDialog::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( !(GetWindowStyle() & 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 wxYieldIfNeeded() ;
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
430 bool 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
451 bool 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 wxYieldIfNeeded();
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
469 void 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
480 bool 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
491 // ----------------------------------------------------------------------------
492 // event handlers
493 // ----------------------------------------------------------------------------
494
495 void wxProgressDialog::OnCancel(wxCommandEvent& event)
496 {
497 if ( m_state == Finished )
498 {
499 // this means that the count down is already finished and we're being
500 // shown as a modal dialog - so just let the default handler do the job
501 event.Skip();
502 }
503 else
504 {
505 // request to cancel was received, the next time Update() is called we
506 // will handle it
507 m_state = Canceled;
508
509 // update the buttons state immediately so that the user knows that the
510 // request has been noticed
511 DisableAbort();
512 DisableSkip();
513
514 // save the time when the dialog was stopped
515 m_timeStop = wxGetCurrentTime();
516 }
517 }
518
519 void wxProgressDialog::OnSkip(wxCommandEvent& WXUNUSED(event))
520 {
521 DisableSkip();
522 m_skip = true;
523 }
524
525 void wxProgressDialog::OnClose(wxCloseEvent& event)
526 {
527 if ( m_state == Uncancelable )
528 {
529 // can't close this dialog
530 event.Veto();
531 }
532 else if ( m_state == Finished )
533 {
534 // let the default handler close the window as we already terminated
535 event.Skip();
536 }
537 else
538 {
539 // next Update() will notice it
540 m_state = Canceled;
541 DisableAbort();
542 DisableSkip();
543
544 m_timeStop = wxGetCurrentTime();
545 }
546 }
547
548 // ----------------------------------------------------------------------------
549 // destruction
550 // ----------------------------------------------------------------------------
551
552 wxProgressDialog::~wxProgressDialog()
553 {
554 // normally this should have been already done, but just in case
555 ReenableOtherWindows();
556 }
557
558 void wxProgressDialog::ReenableOtherWindows()
559 {
560 if ( GetWindowStyle() & wxPD_APP_MODAL )
561 {
562 delete m_winDisabler;
563 m_winDisabler = (wxWindowDisabler *)NULL;
564 }
565 else
566 {
567 if ( m_parentTop )
568 m_parentTop->Enable();
569 }
570 }
571
572 // ----------------------------------------------------------------------------
573 // private functions
574 // ----------------------------------------------------------------------------
575
576 static void SetTimeLabel(unsigned long val, wxStaticText *label)
577 {
578 if ( label )
579 {
580 wxString s;
581
582 if (val != (unsigned long)-1)
583 {
584 unsigned long hours = val / 3600;
585 unsigned long minutes = (val % 3600) / 60;
586 unsigned long seconds = val % 60;
587 s.Printf(wxT("%lu:%02lu:%02lu"), hours, minutes, seconds);
588 }
589 else
590 {
591 s = _("Unknown");
592 }
593
594 if ( s != label->GetLabel() )
595 label->SetLabel(s);
596 }
597 }
598
599 void wxProgressDialog::EnableSkip(bool enable)
600 {
601 if(m_hasSkipButton)
602 {
603 #ifdef __SMARTPHONE__
604 if(enable)
605 SetRightMenu(wxID_SKIP, _("Skip"));
606 else
607 SetRightMenu();
608 #else
609 if(m_btnSkip)
610 m_btnSkip->Enable(enable);
611 #endif
612 }
613 }
614
615 void wxProgressDialog::EnableAbort(bool enable)
616 {
617 if(m_hasAbortButton)
618 {
619 #ifdef __SMARTPHONE__
620 if(enable)
621 SetLeftMenu(wxID_CANCEL); // stock buttons makes Cancel label
622 else
623 SetLeftMenu();
624 #else
625 if(m_btnAbort)
626 m_btnAbort->Enable(enable);
627 #endif
628 }
629 }
630
631 void wxProgressDialog::EnableClose()
632 {
633 if(m_hasAbortButton)
634 {
635 #ifdef __SMARTPHONE__
636 SetLeftMenu(wxID_CANCEL, _("Close"));
637 #else
638 if(m_btnAbort)
639 {
640 m_btnAbort->Enable();
641 m_btnAbort->SetLabel(_("Close"));
642 }
643 #endif
644 }
645 }
646
647 void wxProgressDialog::UpdateMessage(const wxString &newmsg)
648 {
649 if ( !newmsg.empty() && newmsg != m_msg->GetLabel() )
650 {
651 m_msg->SetLabel(newmsg);
652
653 wxYieldIfNeeded() ;
654 }
655 }
656
657 #endif // wxUSE_PROGRESSDLG