1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/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)
12 // Copyright: (c) 1999 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
13 // Licence: wxWindows licence
14 ///////////////////////////////////////////////////////////////////////////////
16 // ============================================================================
18 // ============================================================================
20 // ----------------------------------------------------------------------------
22 // ----------------------------------------------------------------------------
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
34 #include "wx/dynarray.h"
36 #include "wx/statbmp.h"
37 #include "wx/button.h"
40 #include "wx/statline.h"
42 #include "wx/settings.h"
44 #include "wx/wizard.h"
46 // ----------------------------------------------------------------------------
48 // ----------------------------------------------------------------------------
50 class wxWizardSizer
: public wxSizer
53 wxWizardSizer(wxWizard
*owner
);
55 virtual wxSizerItem
*Insert(size_t index
, wxSizerItem
*item
);
57 virtual void RecalcSizes();
58 virtual wxSize
CalcMin();
60 // get the max size of all wizard pages
61 wxSize
GetMaxChildSize();
63 // return the border which can be either set using wxWizard::SetBorder() or
65 int GetBorder() const;
67 // hide the pages which we temporarily "show" when they're added to this
68 // sizer (see Insert())
72 wxSize
SiblingSize(wxSizerItem
*child
);
75 bool m_childSizeValid
;
79 // ----------------------------------------------------------------------------
80 // event tables and such
81 // ----------------------------------------------------------------------------
83 DEFINE_EVENT_TYPE(wxEVT_WIZARD_PAGE_CHANGED
)
84 DEFINE_EVENT_TYPE(wxEVT_WIZARD_PAGE_CHANGING
)
85 DEFINE_EVENT_TYPE(wxEVT_WIZARD_CANCEL
)
86 DEFINE_EVENT_TYPE(wxEVT_WIZARD_FINISHED
)
87 DEFINE_EVENT_TYPE(wxEVT_WIZARD_HELP
)
89 BEGIN_EVENT_TABLE(wxWizard
, wxDialog
)
90 EVT_BUTTON(wxID_CANCEL
, wxWizard::OnCancel
)
91 EVT_BUTTON(wxID_BACKWARD
, wxWizard::OnBackOrNext
)
92 EVT_BUTTON(wxID_FORWARD
, wxWizard::OnBackOrNext
)
93 EVT_BUTTON(wxID_HELP
, wxWizard::OnHelp
)
95 EVT_WIZARD_PAGE_CHANGED(wxID_ANY
, wxWizard::OnWizEvent
)
96 EVT_WIZARD_PAGE_CHANGING(wxID_ANY
, wxWizard::OnWizEvent
)
97 EVT_WIZARD_CANCEL(wxID_ANY
, wxWizard::OnWizEvent
)
98 EVT_WIZARD_FINISHED(wxID_ANY
, wxWizard::OnWizEvent
)
99 EVT_WIZARD_HELP(wxID_ANY
, wxWizard::OnWizEvent
)
102 IMPLEMENT_DYNAMIC_CLASS(wxWizard
, wxDialog
)
111 IMPLEMENT_ABSTRACT_CLASS(wxWizardPage
, wxPanel
)
112 IMPLEMENT_DYNAMIC_CLASS(wxWizardPageSimple
, wxWizardPage
)
113 IMPLEMENT_DYNAMIC_CLASS(wxWizardEvent
, wxNotifyEvent
)
115 // ============================================================================
117 // ============================================================================
119 // ----------------------------------------------------------------------------
121 // ----------------------------------------------------------------------------
123 void wxWizardPage::Init()
125 m_bitmap
= wxNullBitmap
;
128 wxWizardPage::wxWizardPage(wxWizard
*parent
,
129 const wxBitmap
& bitmap
,
130 const wxChar
*resource
)
132 Create(parent
, bitmap
, resource
);
135 bool wxWizardPage::Create(wxWizard
*parent
,
136 const wxBitmap
& bitmap
,
137 const wxChar
*resource
)
139 if ( !wxPanel::Create(parent
, wxID_ANY
) )
142 if ( resource
!= NULL
)
144 #if wxUSE_WX_RESOURCES
146 if ( !LoadFromResource(this, resource
) )
148 wxFAIL_MSG(wxT("wxWizardPage LoadFromResource failed!!!!"));
151 #endif // wxUSE_RESOURCES
156 // initially the page is hidden, it's shown only when it becomes current
162 // ----------------------------------------------------------------------------
163 // wxWizardPageSimple
164 // ----------------------------------------------------------------------------
166 wxWizardPage
*wxWizardPageSimple::GetPrev() const
171 wxWizardPage
*wxWizardPageSimple::GetNext() const
176 // ----------------------------------------------------------------------------
178 // ----------------------------------------------------------------------------
180 wxWizardSizer::wxWizardSizer(wxWizard
*owner
)
183 m_childSizeValid
= false;
186 wxSizerItem
*wxWizardSizer::Insert(size_t index
, wxSizerItem
*item
)
188 if ( item
->IsWindow() )
190 // we must pretend that the window is shown as otherwise it wouldn't be
191 // taken into account for the layout -- but avoid really showing it, so
192 // just set the internal flag instead of calling wxWindow::Show()
193 item
->GetWindow()->wxWindowBase::Show();
196 return wxSizer::Insert(index
, item
);
199 void wxWizardSizer::HidePages()
201 for ( wxSizerItemList::compatibility_iterator node
= GetChildren().GetFirst();
203 node
= node
->GetNext() )
205 wxSizerItem
* const item
= node
->GetData();
206 if ( item
->IsWindow() )
207 item
->GetWindow()->wxWindowBase::Show(false);
211 void wxWizardSizer::RecalcSizes()
213 // Effect of this function depends on m_owner->m_page and
214 // it should be called whenever it changes (wxWizard::ShowPage)
215 if ( m_owner
->m_page
)
217 m_owner
->m_page
->SetSize(m_position
.x
, m_position
.y
, m_size
.x
, m_size
.y
);
221 wxSize
wxWizardSizer::CalcMin()
223 return m_owner
->GetPageSize();
226 wxSize
wxWizardSizer::GetMaxChildSize()
228 #if !defined(__WXDEBUG__)
229 if ( m_childSizeValid
)
234 wxSizerItemList::compatibility_iterator childNode
;
236 for(childNode
= m_children
.GetFirst(); childNode
;
237 childNode
= childNode
->GetNext())
239 wxSizerItem
*child
= childNode
->GetData();
240 maxOfMin
.IncTo(child
->CalcMin());
241 maxOfMin
.IncTo(SiblingSize(child
));
245 if ( m_childSizeValid
&& m_childSize
!= maxOfMin
)
247 wxFAIL_MSG( _T("Size changed in wxWizard::GetPageAreaSizer()")
248 _T("after RunWizard().\n")
249 _T("Did you forget to call GetSizer()->Fit(this) ")
250 _T("for some page?")) ;
254 #endif // __WXDEBUG__
256 if ( m_owner
->m_started
)
258 m_childSizeValid
= true;
259 m_childSize
= maxOfMin
;
265 int wxWizardSizer::GetBorder() const
267 if ( m_owner
->m_calledSetBorder
)
268 return m_owner
->m_border
;
270 return m_children
.IsEmpty() ? 5 : 0;
273 wxSize
wxWizardSizer::SiblingSize(wxSizerItem
*child
)
277 if ( child
->IsWindow() )
279 wxWizardPage
*page
= wxDynamicCast(child
->GetWindow(), wxWizardPage
);
282 for ( wxWizardPage
*sibling
= page
->GetNext();
284 sibling
= sibling
->GetNext() )
286 if ( sibling
->GetSizer() )
288 maxSibling
.IncTo(sibling
->GetSizer()->CalcMin());
297 // ----------------------------------------------------------------------------
298 // generic wxWizard implementation
299 // ----------------------------------------------------------------------------
301 void wxWizard::Init()
303 m_posWizard
= wxDefaultPosition
;
304 m_page
= (wxWizardPage
*)NULL
;
305 m_btnPrev
= m_btnNext
= NULL
;
307 m_sizerBmpAndPage
= NULL
;
309 m_calledSetBorder
= false;
315 bool wxWizard::Create(wxWindow
*parent
,
317 const wxString
& title
,
318 const wxBitmap
& bitmap
,
322 bool result
= wxDialog::Create(parent
,id
,title
,pos
,wxDefaultSize
,style
);
332 void wxWizard::AddBitmapRow(wxBoxSizer
*mainColumn
)
334 m_sizerBmpAndPage
= new wxBoxSizer(wxHORIZONTAL
);
337 1, // Vertically stretchable
338 wxEXPAND
// Horizonal stretching, no border
341 0, // No vertical stretching
342 wxEXPAND
// No border, (mostly useless) horizontal stretching
348 m_statbmp
= new wxStaticBitmap(this, wxID_ANY
, m_bitmap
);
349 m_sizerBmpAndPage
->Add(
351 0, // No horizontal stretching
352 wxALL
, // Border all around, top alignment
355 m_sizerBmpAndPage
->Add(
357 0, // No horizontal stretching
358 wxEXPAND
// No border, (mostly useless) vertical stretching
363 // Added to m_sizerBmpAndPage in FinishLayout
364 m_sizerPage
= new wxWizardSizer(this);
367 void wxWizard::AddStaticLine(wxBoxSizer
*mainColumn
)
371 new wxStaticLine(this, wxID_ANY
),
372 0, // Vertically unstretchable
373 wxEXPAND
| wxALL
, // Border all around, horizontally stretchable
377 0, // No vertical stretching
378 wxEXPAND
// No border, (mostly useless) horizontal stretching
382 #endif // wxUSE_STATLINE
385 void wxWizard::AddBackNextPair(wxBoxSizer
*buttonRow
)
387 wxASSERT_MSG( m_btnNext
&& m_btnPrev
,
388 _T("You must create the buttons before calling ")
389 _T("wxWizard::AddBackNextPair") );
391 // margin between Back and Next buttons
393 static const int BACKNEXT_MARGIN
= 10;
395 static const int BACKNEXT_MARGIN
= 0;
398 wxBoxSizer
*backNextPair
= new wxBoxSizer(wxHORIZONTAL
);
401 0, // No horizontal stretching
402 wxALL
, // Border all around
406 backNextPair
->Add(m_btnPrev
);
407 backNextPair
->Add(BACKNEXT_MARGIN
,0,
408 0, // No horizontal stretching
409 wxEXPAND
// No border, (mostly useless) vertical stretching
411 backNextPair
->Add(m_btnNext
);
414 void wxWizard::AddButtonRow(wxBoxSizer
*mainColumn
)
416 // the order in which the buttons are created determines the TAB order - at least under MSWindows...
417 // although the 'back' button appears before the 'next' button, a more userfriendly tab order is
418 // to activate the 'next' button first (create the next button before the back button).
419 // The reason is: The user will repeatedly enter information in the wizard pages and then wants to
420 // press 'next'. If a user uses mostly the keyboard, he would have to skip the 'back' button
421 // everytime. This is annoying. There is a second reason: RETURN acts as TAB. If the 'next'
422 // button comes first in the TAB order, the user can enter information very fast using the RETURN
423 // key to TAB to the next entry field and page. This would not be possible, if the 'back' button
424 // was created before the 'next' button.
426 bool isPda
= (wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA
);
427 int buttonStyle
= isPda
? wxBU_EXACTFIT
: 0;
429 wxBoxSizer
*buttonRow
= new wxBoxSizer(wxHORIZONTAL
);
431 if (GetExtraStyle() & wxWIZARD_EX_HELPBUTTON
)
434 0, // Vertically unstretchable
435 wxGROW
|wxALIGN_CENTRE
441 0, // Vertically unstretchable
442 wxALIGN_RIGHT
// Right aligned, no border
445 // Desired TAB order is 'next', 'cancel', 'help', 'back'. This makes the 'back' button the last control on the page.
446 // Create the buttons in the right order...
449 if (GetExtraStyle() & wxWIZARD_EX_HELPBUTTON
)
450 btnHelp
=new wxButton(this, wxID_HELP
, _("&Help"), wxDefaultPosition
, wxDefaultSize
, buttonStyle
);
453 m_btnNext
= new wxButton(this, wxID_FORWARD
, _("&Next >"));
454 wxButton
*btnCancel
=new wxButton(this, wxID_CANCEL
, _("&Cancel"), wxDefaultPosition
, wxDefaultSize
, buttonStyle
);
456 if (GetExtraStyle() & wxWIZARD_EX_HELPBUTTON
)
457 btnHelp
=new wxButton(this, wxID_HELP
, _("&Help"), wxDefaultPosition
, wxDefaultSize
, buttonStyle
);
459 m_btnPrev
= new wxButton(this, wxID_BACKWARD
, _("< &Back"), wxDefaultPosition
, wxDefaultSize
, buttonStyle
);
465 0, // Horizontally unstretchable
466 wxALL
, // Border all around, top aligned
470 // Put stretchable space between help button and others
471 buttonRow
->Add(0, 0, 1, wxALIGN_CENTRE
, 0);
475 AddBackNextPair(buttonRow
);
479 0, // Horizontally unstretchable
480 wxALL
, // Border all around, top aligned
485 void wxWizard::DoCreateControls()
487 // do nothing if the controls were already created
491 bool isPda
= (wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA
);
493 // Horizontal stretching, and if not PDA, border all around
494 int mainColumnSizerFlags
= isPda
? wxEXPAND
: wxALL
|wxEXPAND
;
496 // wxWindow::SetSizer will be called at end
497 wxBoxSizer
*windowSizer
= new wxBoxSizer(wxVERTICAL
);
499 wxBoxSizer
*mainColumn
= new wxBoxSizer(wxVERTICAL
);
502 1, // Vertical stretching
503 mainColumnSizerFlags
,
507 AddBitmapRow(mainColumn
);
510 AddStaticLine(mainColumn
);
512 AddButtonRow(mainColumn
);
514 // wxWindow::SetSizer should be followed by wxWindow::Fit, but
515 // this is done in FinishLayout anyway so why duplicate it
516 SetSizer(windowSizer
);
519 void wxWizard::SetPageSize(const wxSize
& size
)
521 wxCHECK_RET(!m_started
,wxT("wxWizard::SetPageSize after RunWizard"));
525 void wxWizard::FinishLayout()
527 bool isPda
= (wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA
);
529 // Set to enable wxWizardSizer::GetMaxChildSize
532 m_sizerBmpAndPage
->Add(
534 1, // Horizontal stretching
535 wxEXPAND
| wxALL
, // Vertically stretchable
536 m_sizerPage
->GetBorder()
541 GetSizer()->SetSizeHints(this);
542 if ( m_posWizard
== wxDefaultPosition
)
546 // now that our layout is computed correctly, hide the pages artificially
547 // shown in wxWizardSizer::Insert() back again
548 m_sizerPage
->HidePages();
551 void wxWizard::FitToPage(const wxWizardPage
*page
)
553 wxCHECK_RET(!m_started
,wxT("wxWizard::FitToPage after RunWizard"));
557 wxSize size
= page
->GetBestSize();
559 m_sizePage
.IncTo(size
);
561 page
= page
->GetNext();
565 bool wxWizard::ShowPage(wxWizardPage
*page
, bool goingForward
)
567 wxASSERT_MSG( page
!= m_page
, wxT("this is useless") );
569 // we'll use this to decide whether we have to change the label of this
570 // button or not (initially the label is "Next")
571 bool btnLabelWasNext
= true;
573 // Modified 10-20-2001 Robert Cavanaugh.
574 // Fixed bug for displaying a new bitmap
575 // in each *consecutive* page
577 // flag to indicate if this page uses a new bitmap
578 bool bmpIsDefault
= true;
580 // use these labels to determine if we need to change the bitmap
582 wxBitmap bmpPrev
, bmpCur
;
584 // check for previous page
587 // send the event to the old page
588 wxWizardEvent
event(wxEVT_WIZARD_PAGE_CHANGING
, GetId(), goingForward
, m_page
);
589 if ( m_page
->GetEventHandler()->ProcessEvent(event
) &&
592 // vetoed by the page
598 btnLabelWasNext
= HasNextPage(m_page
);
600 // Get the bitmap of the previous page (if it exists)
601 if ( m_page
->GetBitmap().Ok() )
603 bmpPrev
= m_page
->GetBitmap();
613 // terminate successfully
620 SetReturnCode(wxID_OK
);
624 // and notify the user code (this is especially useful for modeless
626 wxWizardEvent
event(wxEVT_WIZARD_FINISHED
, GetId(), false, 0);
627 (void)GetEventHandler()->ProcessEvent(event
);
632 // position and show the new page
633 (void)m_page
->TransferDataToWindow();
635 // wxWizardSizer::RecalcSizes wants to be called when m_page changes
636 m_sizerPage
->RecalcSizes();
638 // check if bitmap needs to be updated
639 // update default flag as well
640 if ( m_page
->GetBitmap().Ok() )
642 bmpCur
= m_page
->GetBitmap();
643 bmpIsDefault
= false;
647 // change the bitmap if:
648 // 1) a default bitmap was selected in constructor
649 // 2) this page was constructed with a bitmap
650 // 3) this bitmap is not the previous bitmap
651 if ( m_statbmp
&& (bmpCur
!= bmpPrev
) )
657 bmp
= m_page
->GetBitmap();
658 m_statbmp
->SetBitmap(bmp
);
662 // and update the buttons state
663 m_btnPrev
->Enable(HasPrevPage(m_page
));
665 bool hasNext
= HasNextPage(m_page
);
666 if ( btnLabelWasNext
!= hasNext
)
670 m_btnNext
->SetLabel(_("&Finish"));
672 m_btnNext
->SetLabel(_("&Next >"));
674 m_btnNext
->SetDefault();
675 // nothing to do: the label was already correct
677 // send the change event to the new page now
678 wxWizardEvent
event(wxEVT_WIZARD_PAGE_CHANGED
, GetId(), goingForward
, m_page
);
679 (void)m_page
->GetEventHandler()->ProcessEvent(event
);
681 // and finally show it
688 bool wxWizard::RunWizard(wxWizardPage
*firstPage
)
690 wxCHECK_MSG( firstPage
, false, wxT("can't run empty wizard") );
692 // This cannot be done sooner, because user can change layout options
696 // can't return false here because there is no old page
697 (void)ShowPage(firstPage
, true /* forward */);
701 return ShowModal() == wxID_OK
;
704 wxWizardPage
*wxWizard::GetCurrentPage() const
709 wxSize
wxWizard::GetPageSize() const
711 wxSize
pageSize(GetManualPageSize());
712 pageSize
.IncTo(m_sizerPage
->GetMaxChildSize());
716 wxSizer
*wxWizard::GetPageAreaSizer() const
721 void wxWizard::SetBorder(int border
)
723 wxCHECK_RET(!m_started
,wxT("wxWizard::SetBorder after RunWizard"));
725 m_calledSetBorder
= true;
729 wxSize
wxWizard::GetManualPageSize() const
731 // default width and height of the page
732 int DEFAULT_PAGE_WIDTH
= 270;
733 int DEFAULT_PAGE_HEIGHT
= 270;
734 bool isPda
= (wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA
);
737 // Make the default page size small enough to fit on screen
738 DEFAULT_PAGE_WIDTH
= wxSystemSettings::GetMetric(wxSYS_SCREEN_X
) / 2;
739 DEFAULT_PAGE_HEIGHT
= wxSystemSettings::GetMetric(wxSYS_SCREEN_Y
) / 2;
742 wxSize
totalPageSize(DEFAULT_PAGE_WIDTH
,DEFAULT_PAGE_HEIGHT
);
744 totalPageSize
.IncTo(m_sizePage
);
748 totalPageSize
.IncTo(wxSize(0, m_bitmap
.GetHeight()));
751 return totalPageSize
;
754 void wxWizard::OnCancel(wxCommandEvent
& WXUNUSED(eventUnused
))
756 // this function probably can never be called when we don't have an active
757 // page, but a small extra check won't hurt
758 wxWindow
*win
= m_page
? (wxWindow
*)m_page
: (wxWindow
*)this;
760 wxWizardEvent
event(wxEVT_WIZARD_CANCEL
, GetId(), false, m_page
);
761 if ( !win
->GetEventHandler()->ProcessEvent(event
) || event
.IsAllowed() )
763 // no objections - close the dialog
766 EndModal(wxID_CANCEL
);
770 SetReturnCode(wxID_CANCEL
);
774 //else: request to Cancel ignored
777 void wxWizard::OnBackOrNext(wxCommandEvent
& event
)
779 wxASSERT_MSG( (event
.GetEventObject() == m_btnNext
) ||
780 (event
.GetEventObject() == m_btnPrev
),
781 wxT("unknown button") );
783 wxCHECK_RET( m_page
, _T("should have a valid current page") );
785 // ask the current page first: notice that we do it before calling
786 // GetNext/Prev() because the data transfered from the controls of the page
787 // may change the value returned by these methods
788 if ( !m_page
->Validate() || !m_page
->TransferDataFromWindow() )
790 // the page data is incorrect, don't do anything
794 bool forward
= event
.GetEventObject() == m_btnNext
;
799 page
= m_page
->GetNext();
803 page
= m_page
->GetPrev();
805 wxASSERT_MSG( page
, wxT("\"<Back\" button should have been disabled") );
808 // just pass to the new page (or may be not - but we don't care here)
809 (void)ShowPage(page
, forward
);
812 void wxWizard::OnHelp(wxCommandEvent
& WXUNUSED(event
))
814 // this function probably can never be called when we don't have an active
815 // page, but a small extra check won't hurt
818 // Create and send the help event to the specific page handler
819 // event data contains the active page so that context-sensitive
821 wxWizardEvent
eventHelp(wxEVT_WIZARD_HELP
, GetId(), true, m_page
);
822 (void)m_page
->GetEventHandler()->ProcessEvent(eventHelp
);
826 void wxWizard::OnWizEvent(wxWizardEvent
& event
)
828 // the dialogs have wxWS_EX_BLOCK_EVENTS style on by default but we want to
829 // propagate wxEVT_WIZARD_XXX to the parent (if any), so do it manually
830 if ( !(GetExtraStyle() & wxWS_EX_BLOCK_EVENTS
) )
832 // the event will be propagated anyhow
837 wxWindow
*parent
= GetParent();
839 if ( !parent
|| !parent
->GetEventHandler()->ProcessEvent(event
) )
845 if ( ( !m_wasModal
) &&
847 ( event
.GetEventType() == wxEVT_WIZARD_FINISHED
||
848 event
.GetEventType() == wxEVT_WIZARD_CANCEL
856 // ----------------------------------------------------------------------------
858 // ----------------------------------------------------------------------------
860 wxWizardEvent::wxWizardEvent(wxEventType type
, int id
, bool direction
, wxWizardPage
* page
)
861 : wxNotifyEvent(type
, id
)
863 // Modified 10-20-2001 Robert Cavanaugh
864 // add the active page to the event data
865 m_direction
= direction
;
869 #endif // wxUSE_WIZARDDLG