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"
38 #include "wx/settings.h"
42 #include "wx/statline.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
);
78 // ----------------------------------------------------------------------------
79 // event tables and such
80 // ----------------------------------------------------------------------------
82 DEFINE_EVENT_TYPE(wxEVT_WIZARD_PAGE_CHANGED
)
83 DEFINE_EVENT_TYPE(wxEVT_WIZARD_PAGE_CHANGING
)
84 DEFINE_EVENT_TYPE(wxEVT_WIZARD_CANCEL
)
85 DEFINE_EVENT_TYPE(wxEVT_WIZARD_FINISHED
)
86 DEFINE_EVENT_TYPE(wxEVT_WIZARD_HELP
)
88 BEGIN_EVENT_TABLE(wxWizard
, wxDialog
)
89 EVT_BUTTON(wxID_CANCEL
, wxWizard::OnCancel
)
90 EVT_BUTTON(wxID_BACKWARD
, wxWizard::OnBackOrNext
)
91 EVT_BUTTON(wxID_FORWARD
, wxWizard::OnBackOrNext
)
92 EVT_BUTTON(wxID_HELP
, wxWizard::OnHelp
)
94 EVT_WIZARD_PAGE_CHANGED(wxID_ANY
, wxWizard::OnWizEvent
)
95 EVT_WIZARD_PAGE_CHANGING(wxID_ANY
, wxWizard::OnWizEvent
)
96 EVT_WIZARD_CANCEL(wxID_ANY
, wxWizard::OnWizEvent
)
97 EVT_WIZARD_FINISHED(wxID_ANY
, wxWizard::OnWizEvent
)
98 EVT_WIZARD_HELP(wxID_ANY
, wxWizard::OnWizEvent
)
101 IMPLEMENT_DYNAMIC_CLASS(wxWizard
, wxDialog
)
110 IMPLEMENT_ABSTRACT_CLASS(wxWizardPage
, wxPanel
)
111 IMPLEMENT_DYNAMIC_CLASS(wxWizardPageSimple
, wxWizardPage
)
112 IMPLEMENT_DYNAMIC_CLASS(wxWizardEvent
, wxNotifyEvent
)
114 // ============================================================================
116 // ============================================================================
118 // ----------------------------------------------------------------------------
120 // ----------------------------------------------------------------------------
122 void wxWizardPage::Init()
124 m_bitmap
= wxNullBitmap
;
127 wxWizardPage::wxWizardPage(wxWizard
*parent
,
128 const wxBitmap
& bitmap
,
129 const wxChar
*resource
)
131 Create(parent
, bitmap
, resource
);
134 bool wxWizardPage::Create(wxWizard
*parent
,
135 const wxBitmap
& bitmap
,
136 const wxChar
*resource
)
138 if ( !wxPanel::Create(parent
, wxID_ANY
) )
141 if ( resource
!= NULL
)
143 #if wxUSE_WX_RESOURCES
145 if ( !LoadFromResource(this, resource
) )
147 wxFAIL_MSG(wxT("wxWizardPage LoadFromResource failed!!!!"));
150 #endif // wxUSE_RESOURCES
155 // initially the page is hidden, it's shown only when it becomes current
161 // ----------------------------------------------------------------------------
162 // wxWizardPageSimple
163 // ----------------------------------------------------------------------------
165 wxWizardPage
*wxWizardPageSimple::GetPrev() const
170 wxWizardPage
*wxWizardPageSimple::GetNext() const
175 // ----------------------------------------------------------------------------
177 // ----------------------------------------------------------------------------
179 wxWizardSizer::wxWizardSizer(wxWizard
*owner
)
181 m_childSize(wxDefaultSize
)
185 wxSizerItem
*wxWizardSizer::Insert(size_t index
, wxSizerItem
*item
)
187 m_owner
->m_usingSizer
= true;
189 if ( item
->IsWindow() )
191 // we must pretend that the window is shown as otherwise it wouldn't be
192 // taken into account for the layout -- but avoid really showing it, so
193 // just set the internal flag instead of calling wxWindow::Show()
194 item
->GetWindow()->wxWindowBase::Show();
197 return wxSizer::Insert(index
, item
);
200 void wxWizardSizer::HidePages()
202 for ( wxSizerItemList::compatibility_iterator node
= GetChildren().GetFirst();
204 node
= node
->GetNext() )
206 wxSizerItem
* const item
= node
->GetData();
207 if ( item
->IsWindow() )
208 item
->GetWindow()->wxWindowBase::Show(false);
212 void wxWizardSizer::RecalcSizes()
214 // Effect of this function depends on m_owner->m_page and
215 // it should be called whenever it changes (wxWizard::ShowPage)
216 if ( m_owner
->m_page
)
218 m_owner
->m_page
->SetSize(wxRect(m_position
, m_size
));
222 wxSize
wxWizardSizer::CalcMin()
224 return m_owner
->GetPageSize();
227 wxSize
wxWizardSizer::GetMaxChildSize()
229 #if !defined(__WXDEBUG__)
230 if ( m_childSize
.IsFullySpecified() )
236 for ( wxSizerItemList::compatibility_iterator childNode
= m_children
.GetFirst();
238 childNode
= childNode
->GetNext() )
240 wxSizerItem
*child
= childNode
->GetData();
241 maxOfMin
.IncTo(child
->CalcMin());
242 maxOfMin
.IncTo(SiblingSize(child
));
246 if ( m_childSize
.IsFullySpecified() && m_childSize
!= maxOfMin
)
248 wxFAIL_MSG( _T("Size changed in wxWizard::GetPageAreaSizer()")
249 _T("after RunWizard().\n")
250 _T("Did you forget to call GetSizer()->Fit(this) ")
251 _T("for some page?")) ;
255 #endif // __WXDEBUG__
257 if ( m_owner
->m_started
)
259 m_childSize
= maxOfMin
;
265 int wxWizardSizer::GetBorder() const
267 return m_owner
->m_border
;
270 wxSize
wxWizardSizer::SiblingSize(wxSizerItem
*child
)
274 if ( child
->IsWindow() )
276 wxWizardPage
*page
= wxDynamicCast(child
->GetWindow(), wxWizardPage
);
279 for ( wxWizardPage
*sibling
= page
->GetNext();
281 sibling
= sibling
->GetNext() )
283 if ( sibling
->GetSizer() )
285 maxSibling
.IncTo(sibling
->GetSizer()->CalcMin());
294 // ----------------------------------------------------------------------------
295 // generic wxWizard implementation
296 // ----------------------------------------------------------------------------
298 void wxWizard::Init()
300 m_posWizard
= wxDefaultPosition
;
301 m_page
= (wxWizardPage
*)NULL
;
302 m_btnPrev
= m_btnNext
= NULL
;
304 m_sizerBmpAndPage
= NULL
;
309 m_usingSizer
= false;
312 bool wxWizard::Create(wxWindow
*parent
,
314 const wxString
& title
,
315 const wxBitmap
& bitmap
,
319 bool result
= wxDialog::Create(parent
,id
,title
,pos
,wxDefaultSize
,style
);
329 void wxWizard::AddBitmapRow(wxBoxSizer
*mainColumn
)
331 m_sizerBmpAndPage
= new wxBoxSizer(wxHORIZONTAL
);
334 1, // Vertically stretchable
335 wxEXPAND
// Horizonal stretching, no border
338 0, // No vertical stretching
339 wxEXPAND
// No border, (mostly useless) horizontal stretching
345 m_statbmp
= new wxStaticBitmap(this, wxID_ANY
, m_bitmap
);
346 m_sizerBmpAndPage
->Add(
348 0, // No horizontal stretching
349 wxALL
, // Border all around, top alignment
352 m_sizerBmpAndPage
->Add(
354 0, // No horizontal stretching
355 wxEXPAND
// No border, (mostly useless) vertical stretching
360 // Added to m_sizerBmpAndPage later
361 m_sizerPage
= new wxWizardSizer(this);
364 void wxWizard::AddStaticLine(wxBoxSizer
*mainColumn
)
368 new wxStaticLine(this, wxID_ANY
),
369 0, // Vertically unstretchable
370 wxEXPAND
| wxALL
, // Border all around, horizontally stretchable
374 0, // No vertical stretching
375 wxEXPAND
// No border, (mostly useless) horizontal stretching
379 #endif // wxUSE_STATLINE
382 void wxWizard::AddBackNextPair(wxBoxSizer
*buttonRow
)
384 wxASSERT_MSG( m_btnNext
&& m_btnPrev
,
385 _T("You must create the buttons before calling ")
386 _T("wxWizard::AddBackNextPair") );
388 // margin between Back and Next buttons
390 static const int BACKNEXT_MARGIN
= 10;
392 static const int BACKNEXT_MARGIN
= 0;
395 wxBoxSizer
*backNextPair
= new wxBoxSizer(wxHORIZONTAL
);
398 0, // No horizontal stretching
399 wxALL
, // Border all around
403 backNextPair
->Add(m_btnPrev
);
404 backNextPair
->Add(BACKNEXT_MARGIN
,0,
405 0, // No horizontal stretching
406 wxEXPAND
// No border, (mostly useless) vertical stretching
408 backNextPair
->Add(m_btnNext
);
411 void wxWizard::AddButtonRow(wxBoxSizer
*mainColumn
)
413 // the order in which the buttons are created determines the TAB order - at least under MSWindows...
414 // although the 'back' button appears before the 'next' button, a more userfriendly tab order is
415 // to activate the 'next' button first (create the next button before the back button).
416 // The reason is: The user will repeatedly enter information in the wizard pages and then wants to
417 // press 'next'. If a user uses mostly the keyboard, he would have to skip the 'back' button
418 // everytime. This is annoying. There is a second reason: RETURN acts as TAB. If the 'next'
419 // button comes first in the TAB order, the user can enter information very fast using the RETURN
420 // key to TAB to the next entry field and page. This would not be possible, if the 'back' button
421 // was created before the 'next' button.
423 bool isPda
= (wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA
);
424 int buttonStyle
= isPda
? wxBU_EXACTFIT
: 0;
426 wxBoxSizer
*buttonRow
= new wxBoxSizer(wxHORIZONTAL
);
428 if (GetExtraStyle() & wxWIZARD_EX_HELPBUTTON
)
431 0, // Vertically unstretchable
432 wxGROW
|wxALIGN_CENTRE
438 0, // Vertically unstretchable
439 wxALIGN_RIGHT
// Right aligned, no border
442 // Desired TAB order is 'next', 'cancel', 'help', 'back'. This makes the 'back' button the last control on the page.
443 // Create the buttons in the right order...
446 if (GetExtraStyle() & wxWIZARD_EX_HELPBUTTON
)
447 btnHelp
=new wxButton(this, wxID_HELP
, _("&Help"), wxDefaultPosition
, wxDefaultSize
, buttonStyle
);
450 m_btnNext
= new wxButton(this, wxID_FORWARD
, _("&Next >"));
451 wxButton
*btnCancel
=new wxButton(this, wxID_CANCEL
, _("&Cancel"), wxDefaultPosition
, wxDefaultSize
, buttonStyle
);
453 if (GetExtraStyle() & wxWIZARD_EX_HELPBUTTON
)
454 btnHelp
=new wxButton(this, wxID_HELP
, _("&Help"), wxDefaultPosition
, wxDefaultSize
, buttonStyle
);
456 m_btnPrev
= new wxButton(this, wxID_BACKWARD
, _("< &Back"), wxDefaultPosition
, wxDefaultSize
, buttonStyle
);
462 0, // Horizontally unstretchable
463 wxALL
, // Border all around, top aligned
467 // Put stretchable space between help button and others
468 buttonRow
->Add(0, 0, 1, wxALIGN_CENTRE
, 0);
472 AddBackNextPair(buttonRow
);
476 0, // Horizontally unstretchable
477 wxALL
, // Border all around, top aligned
482 void wxWizard::DoCreateControls()
484 // do nothing if the controls were already created
488 bool isPda
= (wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA
);
490 // Horizontal stretching, and if not PDA, border all around
491 int mainColumnSizerFlags
= isPda
? wxEXPAND
: wxALL
|wxEXPAND
;
493 // wxWindow::SetSizer will be called at end
494 wxBoxSizer
*windowSizer
= new wxBoxSizer(wxVERTICAL
);
496 wxBoxSizer
*mainColumn
= new wxBoxSizer(wxVERTICAL
);
499 1, // Vertical stretching
500 mainColumnSizerFlags
,
504 AddBitmapRow(mainColumn
);
507 AddStaticLine(mainColumn
);
509 AddButtonRow(mainColumn
);
511 SetSizer(windowSizer
);
514 void wxWizard::SetPageSize(const wxSize
& size
)
516 wxCHECK_RET(!m_started
, wxT("wxWizard::SetPageSize after RunWizard"));
520 void wxWizard::FitToPage(const wxWizardPage
*page
)
522 wxCHECK_RET(!m_started
, wxT("wxWizard::FitToPage after RunWizard"));
526 wxSize size
= page
->GetBestSize();
528 m_sizePage
.IncTo(size
);
530 page
= page
->GetNext();
534 bool wxWizard::ShowPage(wxWizardPage
*page
, bool goingForward
)
536 wxASSERT_MSG( page
!= m_page
, wxT("this is useless") );
538 wxSizerFlags
flags(1);
539 flags
.Border(wxALL
, m_border
).Expand();
545 m_sizerBmpAndPage
->Add(m_sizerPage
, flags
);
547 // now that our layout is computed correctly, hide the pages
548 // artificially shown in wxWizardSizer::Insert() back again
549 m_sizerPage
->HidePages();
554 // we'll use this to decide whether we have to change the label of this
555 // button or not (initially the label is "Next")
556 bool btnLabelWasNext
= true;
558 // remember the old bitmap (if any) to compare with the new one later
561 // check for previous page
564 // send the event to the old page
565 wxWizardEvent
event(wxEVT_WIZARD_PAGE_CHANGING
, GetId(),
566 goingForward
, m_page
);
567 if ( m_page
->GetEventHandler()->ProcessEvent(event
) &&
570 // vetoed by the page
576 btnLabelWasNext
= HasNextPage(m_page
);
578 bmpPrev
= m_page
->GetBitmap();
581 m_sizerBmpAndPage
->Detach(m_page
);
590 // terminate successfully
597 SetReturnCode(wxID_OK
);
601 // and notify the user code (this is especially useful for modeless
603 wxWizardEvent
event(wxEVT_WIZARD_FINISHED
, GetId(), false, 0);
604 (void)GetEventHandler()->ProcessEvent(event
);
609 // position and show the new page
610 (void)m_page
->TransferDataToWindow();
614 // wxWizardSizer::RecalcSizes wants to be called when m_page changes
615 m_sizerPage
->RecalcSizes();
617 else // pages are not managed by the sizer
619 m_sizerBmpAndPage
->Add(m_page
, flags
);
620 m_sizerBmpAndPage
->SetItemMinSize(m_page
, GetPageSize());
624 // update the bitmap if:it changed
627 wxBitmap bmp
= m_page
->GetBitmap();
634 if ( !bmp
.IsSameAs(bmpPrev
) )
635 m_statbmp
->SetBitmap(bmp
);
637 #endif // wxUSE_STATBMP
640 // and update the buttons state
641 m_btnPrev
->Enable(HasPrevPage(m_page
));
643 bool hasNext
= HasNextPage(m_page
);
644 if ( btnLabelWasNext
!= hasNext
)
646 m_btnNext
->SetLabel(hasNext
? _("&Next >") : _("&Finish"));
648 // nothing to do: the label was already correct
650 m_btnNext
->SetDefault();
653 // send the change event to the new page now
654 wxWizardEvent
event(wxEVT_WIZARD_PAGE_CHANGED
, GetId(), goingForward
, m_page
);
655 (void)m_page
->GetEventHandler()->ProcessEvent(event
);
657 // and finally show it
662 m_sizerBmpAndPage
->Layout();
668 if ( wxSystemSettings::GetScreenType() > wxSYS_SCREEN_PDA
)
670 GetSizer()->SetSizeHints(this);
671 if ( m_posWizard
== wxDefaultPosition
)
679 bool wxWizard::RunWizard(wxWizardPage
*firstPage
)
681 wxCHECK_MSG( firstPage
, false, wxT("can't run empty wizard") );
683 // can't return false here because there is no old page
684 (void)ShowPage(firstPage
, true /* forward */);
688 return ShowModal() == wxID_OK
;
691 wxWizardPage
*wxWizard::GetCurrentPage() const
696 wxSize
wxWizard::GetPageSize() const
698 // default width and height of the page
699 int DEFAULT_PAGE_WIDTH
,
701 if ( wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA
)
703 // Make the default page size small enough to fit on screen
704 DEFAULT_PAGE_WIDTH
= wxSystemSettings::GetMetric(wxSYS_SCREEN_X
) / 2;
705 DEFAULT_PAGE_HEIGHT
= wxSystemSettings::GetMetric(wxSYS_SCREEN_Y
) / 2;
710 DEFAULT_PAGE_HEIGHT
= 270;
713 // start with default minimal size
714 wxSize
pageSize(DEFAULT_PAGE_WIDTH
, DEFAULT_PAGE_HEIGHT
);
716 // make the page at least as big as specified by user
717 pageSize
.IncTo(m_sizePage
);
721 // make the page at least as tall as the bitmap
722 pageSize
.IncTo(wxSize(0, m_bitmap
.GetHeight()));
727 // make it big enough to contain all pages added to the sizer
728 pageSize
.IncTo(m_sizerPage
->GetMaxChildSize());
734 wxSizer
*wxWizard::GetPageAreaSizer() const
739 void wxWizard::SetBorder(int border
)
741 wxCHECK_RET(!m_started
, wxT("wxWizard::SetBorder after RunWizard"));
746 void wxWizard::OnCancel(wxCommandEvent
& WXUNUSED(eventUnused
))
748 // this function probably can never be called when we don't have an active
749 // page, but a small extra check won't hurt
750 wxWindow
*win
= m_page
? (wxWindow
*)m_page
: (wxWindow
*)this;
752 wxWizardEvent
event(wxEVT_WIZARD_CANCEL
, GetId(), false, m_page
);
753 if ( !win
->GetEventHandler()->ProcessEvent(event
) || event
.IsAllowed() )
755 // no objections - close the dialog
758 EndModal(wxID_CANCEL
);
762 SetReturnCode(wxID_CANCEL
);
766 //else: request to Cancel ignored
769 void wxWizard::OnBackOrNext(wxCommandEvent
& event
)
771 wxASSERT_MSG( (event
.GetEventObject() == m_btnNext
) ||
772 (event
.GetEventObject() == m_btnPrev
),
773 wxT("unknown button") );
775 wxCHECK_RET( m_page
, _T("should have a valid current page") );
777 // ask the current page first: notice that we do it before calling
778 // GetNext/Prev() because the data transfered from the controls of the page
779 // may change the value returned by these methods
780 if ( !m_page
->Validate() || !m_page
->TransferDataFromWindow() )
782 // the page data is incorrect, don't do anything
786 bool forward
= event
.GetEventObject() == m_btnNext
;
791 page
= m_page
->GetNext();
795 page
= m_page
->GetPrev();
797 wxASSERT_MSG( page
, wxT("\"<Back\" button should have been disabled") );
800 // just pass to the new page (or maybe not - but we don't care here)
801 (void)ShowPage(page
, forward
);
804 void wxWizard::OnHelp(wxCommandEvent
& WXUNUSED(event
))
806 // this function probably can never be called when we don't have an active
807 // page, but a small extra check won't hurt
810 // Create and send the help event to the specific page handler
811 // event data contains the active page so that context-sensitive
813 wxWizardEvent
eventHelp(wxEVT_WIZARD_HELP
, GetId(), true, m_page
);
814 (void)m_page
->GetEventHandler()->ProcessEvent(eventHelp
);
818 void wxWizard::OnWizEvent(wxWizardEvent
& event
)
820 // the dialogs have wxWS_EX_BLOCK_EVENTS style on by default but we want to
821 // propagate wxEVT_WIZARD_XXX to the parent (if any), so do it manually
822 if ( !(GetExtraStyle() & wxWS_EX_BLOCK_EVENTS
) )
824 // the event will be propagated anyhow
829 wxWindow
*parent
= GetParent();
831 if ( !parent
|| !parent
->GetEventHandler()->ProcessEvent(event
) )
837 if ( ( !m_wasModal
) &&
839 ( event
.GetEventType() == wxEVT_WIZARD_FINISHED
||
840 event
.GetEventType() == wxEVT_WIZARD_CANCEL
848 // ----------------------------------------------------------------------------
850 // ----------------------------------------------------------------------------
852 wxWizardEvent::wxWizardEvent(wxEventType type
, int id
, bool direction
, wxWizardPage
* page
)
853 : wxNotifyEvent(type
, id
)
855 // Modified 10-20-2001 Robert Cavanaugh
856 // add the active page to the event data
857 m_direction
= direction
;
861 #endif // wxUSE_WIZARDDLG