]> git.saurik.com Git - wxWidgets.git/blob - src/generic/wizard.cpp
multilib mode compilation fix
[wxWidgets.git] / src / generic / wizard.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: generic/wizard.cpp
3 // Purpose: generic implementation of wxWizard class
4 // Author: Vadim Zeitlin
5 // Modified by: Robert Cavanaugh
6 // 1) Added capability for wxWizardPage to accept resources
7 // 2) Added "Help" button handler stub
8 // 3) Fixed ShowPage() bug on displaying bitmaps
9 // Robert Vazan (sizers)
10 // Created: 15.08.99
11 // RCS-ID: $Id$
12 // Copyright: (c) 1999 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
13 // Licence: wxWindows licence
14 ///////////////////////////////////////////////////////////////////////////////
15
16 // ============================================================================
17 // declarations
18 // ============================================================================
19
20 // ----------------------------------------------------------------------------
21 // headers
22 // ----------------------------------------------------------------------------
23
24 #ifdef __GNUG__
25 #pragma implementation "wizardg.h"
26 #endif
27
28 // For compilers that support precompilation, includes "wx.h".
29 #include "wx/wxprec.h"
30
31 #ifdef __BORLANDC__
32 #pragma hdrstop
33 #endif
34
35 #if wxUSE_WIZARDDLG
36
37 #ifndef WX_PRECOMP
38 #include "wx/dynarray.h"
39 #include "wx/intl.h"
40 #include "wx/statbmp.h"
41 #include "wx/button.h"
42 #endif //WX_PRECOMP
43
44 #include "wx/statline.h"
45 #include "wx/sizer.h"
46
47 #include "wx/wizard.h"
48
49 // ----------------------------------------------------------------------------
50 // wxWizardSizer
51 // ----------------------------------------------------------------------------
52
53 class wxWizardSizer : public wxSizer
54 {
55 public:
56 wxWizardSizer(wxWizard *owner);
57
58 void RecalcSizes();
59 wxSize CalcMin();
60
61 wxSize GetMaxChildSize();
62 int Border() const;
63
64 private:
65 wxSize SiblingSize(wxSizerItem *child);
66
67 wxWizard *m_owner;
68 bool m_childSizeValid;
69 wxSize m_childSize;
70 };
71
72 // ----------------------------------------------------------------------------
73 // event tables and such
74 // ----------------------------------------------------------------------------
75
76 DEFINE_EVENT_TYPE(wxEVT_WIZARD_PAGE_CHANGED)
77 DEFINE_EVENT_TYPE(wxEVT_WIZARD_PAGE_CHANGING)
78 DEFINE_EVENT_TYPE(wxEVT_WIZARD_CANCEL)
79 DEFINE_EVENT_TYPE(wxEVT_WIZARD_FINISHED)
80 DEFINE_EVENT_TYPE(wxEVT_WIZARD_HELP)
81
82 BEGIN_EVENT_TABLE(wxWizard, wxDialog)
83 EVT_BUTTON(wxID_CANCEL, wxWizard::OnCancel)
84 EVT_BUTTON(wxID_BACKWARD, wxWizard::OnBackOrNext)
85 EVT_BUTTON(wxID_FORWARD, wxWizard::OnBackOrNext)
86 EVT_BUTTON(wxID_HELP, wxWizard::OnHelp)
87
88 EVT_WIZARD_PAGE_CHANGED(-1, wxWizard::OnWizEvent)
89 EVT_WIZARD_PAGE_CHANGING(-1, wxWizard::OnWizEvent)
90 EVT_WIZARD_CANCEL(-1, wxWizard::OnWizEvent)
91 EVT_WIZARD_FINISHED(-1, wxWizard::OnWizEvent)
92 EVT_WIZARD_HELP(-1, wxWizard::OnWizEvent)
93 END_EVENT_TABLE()
94
95 IMPLEMENT_DYNAMIC_CLASS(wxWizard, wxDialog)
96 IMPLEMENT_ABSTRACT_CLASS(wxWizardPage, wxPanel)
97 IMPLEMENT_DYNAMIC_CLASS(wxWizardPageSimple, wxWizardPage)
98 IMPLEMENT_DYNAMIC_CLASS(wxWizardEvent, wxNotifyEvent)
99
100 // ============================================================================
101 // implementation
102 // ============================================================================
103
104 // ----------------------------------------------------------------------------
105 // wxWizardPage
106 // ----------------------------------------------------------------------------
107
108 void wxWizardPage::Init()
109 {
110 m_bitmap = wxNullBitmap;
111 }
112
113 wxWizardPage::wxWizardPage(wxWizard *parent,
114 const wxBitmap& bitmap,
115 const wxChar *resource)
116 {
117 Create(parent, bitmap, resource);
118 }
119
120 bool wxWizardPage::Create(wxWizard *parent,
121 const wxBitmap& bitmap,
122 const wxChar *resource)
123 {
124 if ( !wxPanel::Create(parent, -1) )
125 return FALSE;
126
127 if ( resource != NULL )
128 {
129 #if wxUSE_WX_RESOURCES
130 #if 0
131 if ( !LoadFromResource(this, resource) )
132 {
133 wxFAIL_MSG(wxT("wxWizardPage LoadFromResource failed!!!!"));
134 }
135 #endif
136 #endif // wxUSE_RESOURCES
137 }
138
139 m_bitmap = bitmap;
140
141 // initially the page is hidden, it's shown only when it becomes current
142 Hide();
143
144 return TRUE;
145 }
146
147 // ----------------------------------------------------------------------------
148 // wxWizardPageSimple
149 // ----------------------------------------------------------------------------
150
151 wxWizardPage *wxWizardPageSimple::GetPrev() const
152 {
153 return m_prev;
154 }
155
156 wxWizardPage *wxWizardPageSimple::GetNext() const
157 {
158 return m_next;
159 }
160
161 // ----------------------------------------------------------------------------
162 // wxWizardSizer
163 // ----------------------------------------------------------------------------
164
165 wxWizardSizer::wxWizardSizer(wxWizard *owner)
166 : m_owner(owner)
167 {
168 m_childSizeValid = false;
169 }
170
171 void wxWizardSizer::RecalcSizes()
172 {
173 // Effect of this function depends on m_owner->m_page and
174 // it should be called whenever it changes (wxWizard::ShowPage)
175 if ( m_owner->m_page )
176 {
177 m_owner->m_page->SetSize(m_position.x,m_position.y, m_size.x,m_size.y);
178 }
179 }
180
181 wxSize wxWizardSizer::CalcMin()
182 {
183 return m_owner->GetPageSize();
184 }
185
186 wxSize wxWizardSizer::GetMaxChildSize()
187 {
188 #if !defined(__WXDEBUG__)
189 if ( m_childSizeValid )
190 return m_childSize;
191 #endif
192
193 wxSize maxOfMin;
194 wxSizerItemList::compatibility_iterator childNode;
195
196 for(childNode = m_children.GetFirst(); childNode;
197 childNode = childNode->GetNext())
198 {
199 wxSizerItem *child = childNode->GetData();
200 maxOfMin.IncTo(child->CalcMin());
201 maxOfMin.IncTo(SiblingSize(child));
202 }
203
204 #ifdef __WXDEBUG__
205 if ( m_childSizeValid && m_childSize != maxOfMin )
206 {
207 wxFAIL_MSG( _T("Size changed in wxWizard::GetPageAreaSizer()")
208 _T("after RunWizard().\n")
209 _T("Did you forget to call GetSizer()->Fit(this) ")
210 _T("for some page?")) ;
211
212 return m_childSize;
213 }
214 #endif // __WXDEBUG__
215
216 if ( m_owner->m_started )
217 {
218 m_childSizeValid = true;
219 m_childSize = maxOfMin;
220 }
221
222 return maxOfMin;
223 }
224
225 int wxWizardSizer::Border() const
226 {
227 if ( m_owner->m_calledSetBorder )
228 return m_owner->m_border;
229
230 return m_children.IsEmpty() ? 5 : 0;
231 }
232
233 wxSize wxWizardSizer::SiblingSize(wxSizerItem *child)
234 {
235 wxSize maxSibling;
236
237 if ( child->IsWindow() )
238 {
239 wxWizardPage *page = wxDynamicCast(child->GetWindow(), wxWizardPage);
240 if ( page )
241 {
242 for ( wxWizardPage *sibling = page->GetNext();
243 sibling;
244 sibling = sibling->GetNext() )
245 {
246 if ( sibling->GetSizer() )
247 {
248 maxSibling.IncTo(sibling->GetSizer()->CalcMin());
249 }
250 }
251 }
252 }
253
254 return maxSibling;
255 }
256
257 // ----------------------------------------------------------------------------
258 // generic wxWizard implementation
259 // ----------------------------------------------------------------------------
260
261 void wxWizard::Init()
262 {
263 m_posWizard = wxDefaultPosition;
264 m_page = (wxWizardPage *)NULL;
265 m_btnPrev = m_btnNext = NULL;
266 m_statbmp = NULL;
267 m_sizerBmpAndPage = NULL;
268 m_sizerPage = NULL;
269 m_calledSetBorder = false;
270 m_border = 0;
271 m_started = false;
272 }
273
274 bool wxWizard::Create(wxWindow *parent,
275 int id,
276 const wxString& title,
277 const wxBitmap& bitmap,
278 const wxPoint& pos,
279 long style)
280 {
281 bool result = wxDialog::Create(parent,id,title,pos,wxDefaultSize,style);
282
283 m_posWizard = pos;
284 m_bitmap = bitmap ;
285
286 DoCreateControls();
287
288 return result;
289 }
290
291 void wxWizard::AddBitmapRow(wxBoxSizer *mainColumn)
292 {
293 m_sizerBmpAndPage = new wxBoxSizer(wxHORIZONTAL);
294 mainColumn->Add(
295 m_sizerBmpAndPage,
296 1, // Vertically stretchable
297 wxEXPAND // Horizonal stretching, no border
298 );
299 mainColumn->Add(0,5,
300 0, // No vertical stretching
301 wxEXPAND // No border, (mostly useless) horizontal stretching
302 );
303
304 if ( m_bitmap.Ok() )
305 {
306 m_statbmp = new wxStaticBitmap(this, -1, m_bitmap);
307 m_sizerBmpAndPage->Add(
308 m_statbmp,
309 0, // No horizontal stretching
310 wxALL, // Border all around, top alignment
311 5 // Border width
312 );
313 m_sizerBmpAndPage->Add(
314 5,0,
315 0, // No horizontal stretching
316 wxEXPAND // No border, (mostly useless) vertical stretching
317 );
318 }
319
320 // Added to m_sizerBmpAndPage in FinishLayout
321 m_sizerPage = new wxWizardSizer(this);
322 }
323
324 void wxWizard::AddStaticLine(wxBoxSizer *mainColumn)
325 {
326 #if wxUSE_STATLINE
327 mainColumn->Add(
328 new wxStaticLine(this, -1),
329 0, // Vertically unstretchable
330 wxEXPAND | wxALL, // Border all around, horizontally stretchable
331 5 // Border width
332 );
333 mainColumn->Add(0,5,
334 0, // No vertical stretching
335 wxEXPAND // No border, (mostly useless) horizontal stretching
336 );
337 #else
338 (void)mainColumn;
339 #endif // wxUSE_STATLINE
340 }
341
342 void wxWizard::AddBackNextPair(wxBoxSizer *buttonRow)
343 {
344 // margin between Back and Next buttons
345 #ifdef __WXMAC__
346 static const int BACKNEXT_MARGIN = 10;
347 #else
348 static const int BACKNEXT_MARGIN = 0;
349 #endif
350
351 wxBoxSizer *backNextPair = new wxBoxSizer(wxHORIZONTAL);
352 buttonRow->Add(
353 backNextPair,
354 0, // No horizontal stretching
355 wxALL, // Border all around
356 5 // Border width
357 );
358
359 m_btnPrev = new wxButton(this, wxID_BACKWARD, _("< &Back"));
360 backNextPair->Add(m_btnPrev);
361 backNextPair->Add(BACKNEXT_MARGIN,0,
362 0, // No horizontal stretching
363 wxEXPAND // No border, (mostly useless) vertical stretching
364 );
365 m_btnNext = new wxButton(this, wxID_FORWARD, _("&Next >"));
366 backNextPair->Add(m_btnNext);
367 }
368
369 void wxWizard::AddButtonRow(wxBoxSizer *mainColumn)
370 {
371 wxBoxSizer *buttonRow = new wxBoxSizer(wxHORIZONTAL);
372 mainColumn->Add(
373 buttonRow,
374 0, // Vertically unstretchable
375 wxALIGN_RIGHT // Right aligned, no border
376 );
377
378 if (GetExtraStyle() & wxWIZARD_EX_HELPBUTTON)
379 buttonRow->Add(
380 new wxButton(this, wxID_HELP, _("&Help")),
381 0, // Horizontally unstretchable
382 wxALL, // Border all around, top aligned
383 5 // Border width
384 );
385
386 AddBackNextPair(buttonRow);
387
388 buttonRow->Add(
389 new wxButton(this, wxID_CANCEL, _("&Cancel")),
390 0, // Horizontally unstretchable
391 wxALL, // Border all around, top aligned
392 5 // Border width
393 );
394 }
395
396 void wxWizard::DoCreateControls()
397 {
398 // do nothing if the controls were already created
399 if ( WasCreated() )
400 return;
401
402 // wxWindow::SetSizer will be called at end
403 wxBoxSizer *windowSizer = new wxBoxSizer(wxVERTICAL);
404
405 wxBoxSizer *mainColumn = new wxBoxSizer(wxVERTICAL);
406 windowSizer->Add(
407 mainColumn,
408 1, // Vertical stretching
409 wxALL | wxEXPAND, // Border all around, horizontal stretching
410 5 // Border width
411 );
412
413 AddBitmapRow(mainColumn);
414 AddStaticLine(mainColumn);
415 AddButtonRow(mainColumn);
416
417 // wxWindow::SetSizer should be followed by wxWindow::Fit, but
418 // this is done in FinishLayout anyway so why duplicate it
419 SetSizer(windowSizer);
420 }
421
422 void wxWizard::SetPageSize(const wxSize& size)
423 {
424 wxCHECK_RET(!m_started,wxT("wxWizard::SetPageSize after RunWizard"));
425 m_sizePage = size;
426 }
427
428 void wxWizard::FinishLayout()
429 {
430 m_sizerBmpAndPage->Add(
431 m_sizerPage,
432 1, // Horizontal stretching
433 wxEXPAND | wxALL, // Vertically stretchable
434 m_sizerPage->Border()
435 );
436
437 GetSizer()->SetSizeHints(this);
438 if ( m_posWizard == wxDefaultPosition )
439 CentreOnScreen();
440 }
441
442 void wxWizard::FitToPage(const wxWizardPage *page)
443 {
444 wxCHECK_RET(!m_started,wxT("wxWizard::FitToPage after RunWizard"));
445
446 while ( page )
447 {
448 wxSize size = page->GetBestSize();
449
450 m_sizePage.IncTo(size);
451
452 page = page->GetNext();
453 }
454 }
455
456 bool wxWizard::ShowPage(wxWizardPage *page, bool goingForward)
457 {
458 wxASSERT_MSG( page != m_page, wxT("this is useless") );
459
460 // we'll use this to decide whether we have to change the label of this
461 // button or not (initially the label is "Next")
462 bool btnLabelWasNext = TRUE;
463
464 // Modified 10-20-2001 Robert Cavanaugh.
465 // Fixed bug for displaying a new bitmap
466 // in each *consecutive* page
467
468 // flag to indicate if this page uses a new bitmap
469 bool bmpIsDefault = TRUE;
470
471 // use these labels to determine if we need to change the bitmap
472 // for this page
473 wxBitmap bmpPrev, bmpCur;
474
475 // check for previous page
476 if ( m_page )
477 {
478 // send the event to the old page
479 wxWizardEvent event(wxEVT_WIZARD_PAGE_CHANGING, GetId(), goingForward, m_page);
480 if ( m_page->GetEventHandler()->ProcessEvent(event) &&
481 !event.IsAllowed() )
482 {
483 // vetoed by the page
484 return FALSE;
485 }
486
487 m_page->Hide();
488
489 btnLabelWasNext = HasNextPage(m_page);
490
491 // Get the bitmap of the previous page (if it exists)
492 if ( m_page->GetBitmap().Ok() )
493 {
494 bmpPrev = m_page->GetBitmap();
495 }
496 }
497
498 // set the new page
499 m_page = page;
500
501 // is this the end?
502 if ( !m_page )
503 {
504 // terminate successfully
505 EndModal(wxID_OK);
506 if ( !IsModal() )
507 {
508 wxWizardEvent event(wxEVT_WIZARD_FINISHED, GetId(),FALSE, 0);
509 (void)GetEventHandler()->ProcessEvent(event);
510 }
511 return TRUE;
512 }
513
514 // position and show the new page
515 (void)m_page->TransferDataToWindow();
516
517 // wxWizardSizer::RecalcSizes wants to be called when m_page changes
518 m_sizerPage->RecalcSizes();
519
520 // check if bitmap needs to be updated
521 // update default flag as well
522 if ( m_page->GetBitmap().Ok() )
523 {
524 bmpCur = m_page->GetBitmap();
525 bmpIsDefault = FALSE;
526 }
527
528 // change the bitmap if:
529 // 1) a default bitmap was selected in constructor
530 // 2) this page was constructed with a bitmap
531 // 3) this bitmap is not the previous bitmap
532 if ( m_statbmp && (bmpCur != bmpPrev) )
533 {
534 wxBitmap bmp;
535 if ( bmpIsDefault )
536 bmp = m_bitmap;
537 else
538 bmp = m_page->GetBitmap();
539 m_statbmp->SetBitmap(bmp);
540 }
541
542 // and update the buttons state
543 m_btnPrev->Enable(HasPrevPage(m_page));
544
545 bool hasNext = HasNextPage(m_page);
546 if ( btnLabelWasNext != hasNext )
547 {
548 // need to update
549 if (btnLabelWasNext)
550 m_btnNext->SetLabel(_("&Finish"));
551 else
552 m_btnNext->SetLabel(_("&Next >"));
553 }
554 // nothing to do: the label was already correct
555
556 // send the change event to the new page now
557 wxWizardEvent event(wxEVT_WIZARD_PAGE_CHANGED, GetId(), goingForward, m_page);
558 (void)m_page->GetEventHandler()->ProcessEvent(event);
559
560 // and finally show it
561 m_page->Show();
562 m_page->SetFocus();
563
564 return TRUE;
565 }
566
567 bool wxWizard::RunWizard(wxWizardPage *firstPage)
568 {
569 wxCHECK_MSG( firstPage, FALSE, wxT("can't run empty wizard") );
570
571 // Set before FinishLayout to enable wxWizardSizer::GetMaxChildSize
572 m_started = true;
573
574 // This cannot be done sooner, because user can change layout options
575 // up to this moment
576 FinishLayout();
577
578 // can't return FALSE here because there is no old page
579 (void)ShowPage(firstPage, TRUE /* forward */);
580
581 return ShowModal() == wxID_OK;
582 }
583
584 wxWizardPage *wxWizard::GetCurrentPage() const
585 {
586 return m_page;
587 }
588
589 wxSize wxWizard::GetPageSize() const
590 {
591 wxSize pageSize(GetManualPageSize());
592 pageSize.IncTo(m_sizerPage->GetMaxChildSize());
593 return pageSize;
594 }
595
596 wxSizer *wxWizard::GetPageAreaSizer() const
597 {
598 return m_sizerPage;
599 }
600
601 void wxWizard::SetBorder(int border)
602 {
603 wxCHECK_RET(!m_started,wxT("wxWizard::SetBorder after RunWizard"));
604
605 m_calledSetBorder = true;
606 m_border = border;
607 }
608
609 wxSize wxWizard::GetManualPageSize() const
610 {
611 // default width and height of the page
612 static const int DEFAULT_PAGE_WIDTH = 270;
613 static const int DEFAULT_PAGE_HEIGHT = 290;
614
615 wxSize totalPageSize(DEFAULT_PAGE_WIDTH,DEFAULT_PAGE_HEIGHT);
616
617 totalPageSize.IncTo(m_sizePage);
618
619 if ( m_statbmp )
620 {
621 totalPageSize.IncTo(wxSize(0, m_bitmap.GetHeight()));
622 }
623
624 return totalPageSize;
625 }
626
627 void wxWizard::OnCancel(wxCommandEvent& WXUNUSED(eventUnused))
628 {
629 // this function probably can never be called when we don't have an active
630 // page, but a small extra check won't hurt
631 wxWindow *win = m_page ? (wxWindow *)m_page : (wxWindow *)this;
632
633 wxWizardEvent event(wxEVT_WIZARD_CANCEL, GetId(), FALSE, m_page);
634 if ( !win->GetEventHandler()->ProcessEvent(event) || event.IsAllowed() )
635 {
636 // no objections - close the dialog
637 EndModal(wxID_CANCEL);
638 }
639 //else: request to Cancel ignored
640 }
641
642 void wxWizard::OnBackOrNext(wxCommandEvent& event)
643 {
644 wxASSERT_MSG( (event.GetEventObject() == m_btnNext) ||
645 (event.GetEventObject() == m_btnPrev),
646 wxT("unknown button") );
647
648 // ask the current page first: notice that we do it before calling
649 // GetNext/Prev() because the data transfered from the controls of the page
650 // may change the value returned by these methods
651 if ( m_page && (!m_page->Validate() || !m_page->TransferDataFromWindow()) )
652 {
653 // the page data is incorrect, don't do anything
654 return;
655 }
656
657 bool forward = event.GetEventObject() == m_btnNext;
658
659 wxWizardPage *page;
660 if ( forward )
661 {
662 page = m_page->GetNext();
663 }
664 else // back
665 {
666 page = m_page->GetPrev();
667
668 wxASSERT_MSG( page, wxT("\"<Back\" button should have been disabled") );
669 }
670
671 // just pass to the new page (or may be not - but we don't care here)
672 (void)ShowPage(page, forward);
673 }
674
675 void wxWizard::OnHelp(wxCommandEvent& WXUNUSED(event))
676 {
677 // this function probably can never be called when we don't have an active
678 // page, but a small extra check won't hurt
679 if(m_page != NULL)
680 {
681 // Create and send the help event to the specific page handler
682 // event data contains the active page so that context-sensitive
683 // help is possible
684 wxWizardEvent eventHelp(wxEVT_WIZARD_HELP, GetId(), TRUE, m_page);
685 (void)m_page->GetEventHandler()->ProcessEvent(eventHelp);
686 }
687 }
688
689 void wxWizard::OnWizEvent(wxWizardEvent& event)
690 {
691 // the dialogs have wxWS_EX_BLOCK_EVENTS style on by default but we want to
692 // propagate wxEVT_WIZARD_XXX to the parent (if any), so do it manually
693 if ( !(GetExtraStyle() & wxWS_EX_BLOCK_EVENTS) )
694 {
695 // the event will be propagated anyhow
696 return;
697 }
698
699 wxWindow *parent = GetParent();
700
701 if ( !parent || !parent->GetEventHandler()->ProcessEvent(event) )
702 {
703 event.Skip();
704 }
705 }
706
707 // ----------------------------------------------------------------------------
708 // our public interface
709 // ----------------------------------------------------------------------------
710
711 #ifdef WXWIN_COMPATIBILITY_2_2
712
713 /* static */
714 wxWizard *wxWizardBase::Create(wxWindow *parent,
715 int id,
716 const wxString& title,
717 const wxBitmap& bitmap,
718 const wxPoint& pos,
719 const wxSize& WXUNUSED(size))
720 {
721 return new wxWizard(parent, id, title, bitmap, pos);
722 }
723
724 #endif // WXWIN_COMPATIBILITY_2_2
725
726 // ----------------------------------------------------------------------------
727 // wxWizardEvent
728 // ----------------------------------------------------------------------------
729
730 wxWizardEvent::wxWizardEvent(wxEventType type, int id, bool direction, wxWizardPage* page)
731 : wxNotifyEvent(type, id)
732 {
733 // Modified 10-20-2001 Robert Cavanaugh
734 // add the active page to the event data
735 m_direction = direction;
736 m_page = page;
737 }
738
739 #endif // wxUSE_WIZARDDLG