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