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
)
130 Create(parent
, bitmap
);
133 bool wxWizardPage::Create(wxWizard
*parent
,
134 const wxBitmap
& bitmap
)
136 if ( !wxPanel::Create(parent
, wxID_ANY
) )
141 // initially the page is hidden, it's shown only when it becomes current
147 // ----------------------------------------------------------------------------
148 // wxWizardPageSimple
149 // ----------------------------------------------------------------------------
151 wxWizardPage
*wxWizardPageSimple::GetPrev() const
156 wxWizardPage
*wxWizardPageSimple::GetNext() const
161 // ----------------------------------------------------------------------------
163 // ----------------------------------------------------------------------------
165 wxWizardSizer::wxWizardSizer(wxWizard
*owner
)
167 m_childSize(wxDefaultSize
)
171 wxSizerItem
*wxWizardSizer::Insert(size_t index
, wxSizerItem
*item
)
173 m_owner
->m_usingSizer
= true;
175 if ( item
->IsWindow() )
177 // we must pretend that the window is shown as otherwise it wouldn't be
178 // taken into account for the layout -- but avoid really showing it, so
179 // just set the internal flag instead of calling wxWindow::Show()
180 item
->GetWindow()->wxWindowBase::Show();
183 return wxSizer::Insert(index
, item
);
186 void wxWizardSizer::HidePages()
188 for ( wxSizerItemList::compatibility_iterator node
= GetChildren().GetFirst();
190 node
= node
->GetNext() )
192 wxSizerItem
* const item
= node
->GetData();
193 if ( item
->IsWindow() )
194 item
->GetWindow()->wxWindowBase::Show(false);
198 void wxWizardSizer::RecalcSizes()
200 // Effect of this function depends on m_owner->m_page and
201 // it should be called whenever it changes (wxWizard::ShowPage)
202 if ( m_owner
->m_page
)
204 m_owner
->m_page
->SetSize(wxRect(m_position
, m_size
));
208 wxSize
wxWizardSizer::CalcMin()
210 return m_owner
->GetPageSize();
213 wxSize
wxWizardSizer::GetMaxChildSize()
215 #if !defined(__WXDEBUG__)
216 if ( m_childSize
.IsFullySpecified() )
222 for ( wxSizerItemList::compatibility_iterator childNode
= m_children
.GetFirst();
224 childNode
= childNode
->GetNext() )
226 wxSizerItem
*child
= childNode
->GetData();
227 maxOfMin
.IncTo(child
->CalcMin());
228 maxOfMin
.IncTo(SiblingSize(child
));
232 if ( m_childSize
.IsFullySpecified() && m_childSize
!= maxOfMin
)
234 wxFAIL_MSG( _T("Size changed in wxWizard::GetPageAreaSizer()")
235 _T("after RunWizard().\n")
236 _T("Did you forget to call GetSizer()->Fit(this) ")
237 _T("for some page?")) ;
241 #endif // __WXDEBUG__
243 if ( m_owner
->m_started
)
245 m_childSize
= maxOfMin
;
251 int wxWizardSizer::GetBorder() const
253 return m_owner
->m_border
;
256 wxSize
wxWizardSizer::SiblingSize(wxSizerItem
*child
)
260 if ( child
->IsWindow() )
262 wxWizardPage
*page
= wxDynamicCast(child
->GetWindow(), wxWizardPage
);
265 for ( wxWizardPage
*sibling
= page
->GetNext();
267 sibling
= sibling
->GetNext() )
269 if ( sibling
->GetSizer() )
271 maxSibling
.IncTo(sibling
->GetSizer()->CalcMin());
280 // ----------------------------------------------------------------------------
281 // generic wxWizard implementation
282 // ----------------------------------------------------------------------------
284 void wxWizard::Init()
286 m_posWizard
= wxDefaultPosition
;
287 m_page
= (wxWizardPage
*)NULL
;
288 m_btnPrev
= m_btnNext
= NULL
;
290 m_sizerBmpAndPage
= NULL
;
295 m_usingSizer
= false;
298 bool wxWizard::Create(wxWindow
*parent
,
300 const wxString
& title
,
301 const wxBitmap
& bitmap
,
305 bool result
= wxDialog::Create(parent
,id
,title
,pos
,wxDefaultSize
,style
);
315 void wxWizard::AddBitmapRow(wxBoxSizer
*mainColumn
)
317 m_sizerBmpAndPage
= new wxBoxSizer(wxHORIZONTAL
);
320 1, // Vertically stretchable
321 wxEXPAND
// Horizonal stretching, no border
324 0, // No vertical stretching
325 wxEXPAND
// No border, (mostly useless) horizontal stretching
331 m_statbmp
= new wxStaticBitmap(this, wxID_ANY
, m_bitmap
);
332 m_sizerBmpAndPage
->Add(
334 0, // No horizontal stretching
335 wxALL
, // Border all around, top alignment
338 m_sizerBmpAndPage
->Add(
340 0, // No horizontal stretching
341 wxEXPAND
// No border, (mostly useless) vertical stretching
346 // Added to m_sizerBmpAndPage later
347 m_sizerPage
= new wxWizardSizer(this);
350 void wxWizard::AddStaticLine(wxBoxSizer
*mainColumn
)
354 new wxStaticLine(this, wxID_ANY
),
355 0, // Vertically unstretchable
356 wxEXPAND
| wxALL
, // Border all around, horizontally stretchable
360 0, // No vertical stretching
361 wxEXPAND
// No border, (mostly useless) horizontal stretching
365 #endif // wxUSE_STATLINE
368 void wxWizard::AddBackNextPair(wxBoxSizer
*buttonRow
)
370 wxASSERT_MSG( m_btnNext
&& m_btnPrev
,
371 _T("You must create the buttons before calling ")
372 _T("wxWizard::AddBackNextPair") );
374 // margin between Back and Next buttons
376 static const int BACKNEXT_MARGIN
= 10;
378 static const int BACKNEXT_MARGIN
= 0;
381 wxBoxSizer
*backNextPair
= new wxBoxSizer(wxHORIZONTAL
);
384 0, // No horizontal stretching
385 wxALL
, // Border all around
389 backNextPair
->Add(m_btnPrev
);
390 backNextPair
->Add(BACKNEXT_MARGIN
,0,
391 0, // No horizontal stretching
392 wxEXPAND
// No border, (mostly useless) vertical stretching
394 backNextPair
->Add(m_btnNext
);
397 void wxWizard::AddButtonRow(wxBoxSizer
*mainColumn
)
399 // the order in which the buttons are created determines the TAB order - at least under MSWindows...
400 // although the 'back' button appears before the 'next' button, a more userfriendly tab order is
401 // to activate the 'next' button first (create the next button before the back button).
402 // The reason is: The user will repeatedly enter information in the wizard pages and then wants to
403 // press 'next'. If a user uses mostly the keyboard, he would have to skip the 'back' button
404 // everytime. This is annoying. There is a second reason: RETURN acts as TAB. If the 'next'
405 // button comes first in the TAB order, the user can enter information very fast using the RETURN
406 // key to TAB to the next entry field and page. This would not be possible, if the 'back' button
407 // was created before the 'next' button.
409 bool isPda
= (wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA
);
410 int buttonStyle
= isPda
? wxBU_EXACTFIT
: 0;
412 wxBoxSizer
*buttonRow
= new wxBoxSizer(wxHORIZONTAL
);
414 if (GetExtraStyle() & wxWIZARD_EX_HELPBUTTON
)
417 0, // Vertically unstretchable
418 wxGROW
|wxALIGN_CENTRE
424 0, // Vertically unstretchable
425 wxALIGN_RIGHT
// Right aligned, no border
428 // Desired TAB order is 'next', 'cancel', 'help', 'back'. This makes the 'back' button the last control on the page.
429 // Create the buttons in the right order...
432 if (GetExtraStyle() & wxWIZARD_EX_HELPBUTTON
)
433 btnHelp
=new wxButton(this, wxID_HELP
, _("&Help"), wxDefaultPosition
, wxDefaultSize
, buttonStyle
);
436 m_btnNext
= new wxButton(this, wxID_FORWARD
, _("&Next >"));
437 wxButton
*btnCancel
=new wxButton(this, wxID_CANCEL
, _("&Cancel"), wxDefaultPosition
, wxDefaultSize
, buttonStyle
);
439 if (GetExtraStyle() & wxWIZARD_EX_HELPBUTTON
)
440 btnHelp
=new wxButton(this, wxID_HELP
, _("&Help"), wxDefaultPosition
, wxDefaultSize
, buttonStyle
);
442 m_btnPrev
= new wxButton(this, wxID_BACKWARD
, _("< &Back"), wxDefaultPosition
, wxDefaultSize
, buttonStyle
);
448 0, // Horizontally unstretchable
449 wxALL
, // Border all around, top aligned
453 // Put stretchable space between help button and others
454 buttonRow
->Add(0, 0, 1, wxALIGN_CENTRE
, 0);
458 AddBackNextPair(buttonRow
);
462 0, // Horizontally unstretchable
463 wxALL
, // Border all around, top aligned
468 void wxWizard::DoCreateControls()
470 // do nothing if the controls were already created
474 bool isPda
= (wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA
);
476 // Horizontal stretching, and if not PDA, border all around
477 int mainColumnSizerFlags
= isPda
? wxEXPAND
: wxALL
|wxEXPAND
;
479 // wxWindow::SetSizer will be called at end
480 wxBoxSizer
*windowSizer
= new wxBoxSizer(wxVERTICAL
);
482 wxBoxSizer
*mainColumn
= new wxBoxSizer(wxVERTICAL
);
485 1, // Vertical stretching
486 mainColumnSizerFlags
,
490 AddBitmapRow(mainColumn
);
493 AddStaticLine(mainColumn
);
495 AddButtonRow(mainColumn
);
497 SetSizer(windowSizer
);
500 void wxWizard::SetPageSize(const wxSize
& size
)
502 wxCHECK_RET(!m_started
, wxT("wxWizard::SetPageSize after RunWizard"));
506 void wxWizard::FitToPage(const wxWizardPage
*page
)
508 wxCHECK_RET(!m_started
, wxT("wxWizard::FitToPage after RunWizard"));
512 wxSize size
= page
->GetBestSize();
514 m_sizePage
.IncTo(size
);
516 page
= page
->GetNext();
520 bool wxWizard::ShowPage(wxWizardPage
*page
, bool goingForward
)
522 wxASSERT_MSG( page
!= m_page
, wxT("this is useless") );
524 wxSizerFlags
flags(1);
525 flags
.Border(wxALL
, m_border
).Expand();
531 m_sizerBmpAndPage
->Add(m_sizerPage
, flags
);
533 // now that our layout is computed correctly, hide the pages
534 // artificially shown in wxWizardSizer::Insert() back again
535 m_sizerPage
->HidePages();
540 // we'll use this to decide whether we have to change the label of this
541 // button or not (initially the label is "Next")
542 bool btnLabelWasNext
= true;
544 // remember the old bitmap (if any) to compare with the new one later
547 // check for previous page
550 // send the event to the old page
551 wxWizardEvent
event(wxEVT_WIZARD_PAGE_CHANGING
, GetId(),
552 goingForward
, m_page
);
553 if ( m_page
->GetEventHandler()->ProcessEvent(event
) &&
556 // vetoed by the page
562 btnLabelWasNext
= HasNextPage(m_page
);
564 bmpPrev
= m_page
->GetBitmap();
567 m_sizerBmpAndPage
->Detach(m_page
);
576 // terminate successfully
583 SetReturnCode(wxID_OK
);
587 // and notify the user code (this is especially useful for modeless
589 wxWizardEvent
event(wxEVT_WIZARD_FINISHED
, GetId(), false, 0);
590 (void)GetEventHandler()->ProcessEvent(event
);
595 // position and show the new page
596 (void)m_page
->TransferDataToWindow();
600 // wxWizardSizer::RecalcSizes wants to be called when m_page changes
601 m_sizerPage
->RecalcSizes();
603 else // pages are not managed by the sizer
605 m_sizerBmpAndPage
->Add(m_page
, flags
);
606 m_sizerBmpAndPage
->SetItemMinSize(m_page
, GetPageSize());
610 // update the bitmap if:it changed
613 wxBitmap bmp
= m_page
->GetBitmap();
620 if ( !bmp
.IsSameAs(bmpPrev
) )
621 m_statbmp
->SetBitmap(bmp
);
623 #endif // wxUSE_STATBMP
626 // and update the buttons state
627 m_btnPrev
->Enable(HasPrevPage(m_page
));
629 bool hasNext
= HasNextPage(m_page
);
630 if ( btnLabelWasNext
!= hasNext
)
633 m_btnNext
->SetLabel(_("&Next >"));
635 m_btnNext
->SetLabel(_("&Finish"));
637 // nothing to do: the label was already correct
639 m_btnNext
->SetDefault();
642 // send the change event to the new page now
643 wxWizardEvent
event(wxEVT_WIZARD_PAGE_CHANGED
, GetId(), goingForward
, m_page
);
644 (void)m_page
->GetEventHandler()->ProcessEvent(event
);
646 // and finally show it
651 m_sizerBmpAndPage
->Layout();
657 if ( wxSystemSettings::GetScreenType() > wxSYS_SCREEN_PDA
)
659 GetSizer()->SetSizeHints(this);
660 if ( m_posWizard
== wxDefaultPosition
)
668 bool wxWizard::RunWizard(wxWizardPage
*firstPage
)
670 wxCHECK_MSG( firstPage
, false, wxT("can't run empty wizard") );
672 // can't return false here because there is no old page
673 (void)ShowPage(firstPage
, true /* forward */);
677 return ShowModal() == wxID_OK
;
680 wxWizardPage
*wxWizard::GetCurrentPage() const
685 wxSize
wxWizard::GetPageSize() const
687 // default width and height of the page
688 int DEFAULT_PAGE_WIDTH
,
690 if ( wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA
)
692 // Make the default page size small enough to fit on screen
693 DEFAULT_PAGE_WIDTH
= wxSystemSettings::GetMetric(wxSYS_SCREEN_X
) / 2;
694 DEFAULT_PAGE_HEIGHT
= wxSystemSettings::GetMetric(wxSYS_SCREEN_Y
) / 2;
699 DEFAULT_PAGE_HEIGHT
= 270;
702 // start with default minimal size
703 wxSize
pageSize(DEFAULT_PAGE_WIDTH
, DEFAULT_PAGE_HEIGHT
);
705 // make the page at least as big as specified by user
706 pageSize
.IncTo(m_sizePage
);
710 // make the page at least as tall as the bitmap
711 pageSize
.IncTo(wxSize(0, m_bitmap
.GetHeight()));
716 // make it big enough to contain all pages added to the sizer
717 pageSize
.IncTo(m_sizerPage
->GetMaxChildSize());
723 wxSizer
*wxWizard::GetPageAreaSizer() const
728 void wxWizard::SetBorder(int border
)
730 wxCHECK_RET(!m_started
, wxT("wxWizard::SetBorder after RunWizard"));
735 void wxWizard::OnCancel(wxCommandEvent
& WXUNUSED(eventUnused
))
737 // this function probably can never be called when we don't have an active
738 // page, but a small extra check won't hurt
739 wxWindow
*win
= m_page
? (wxWindow
*)m_page
: (wxWindow
*)this;
741 wxWizardEvent
event(wxEVT_WIZARD_CANCEL
, GetId(), false, m_page
);
742 if ( !win
->GetEventHandler()->ProcessEvent(event
) || event
.IsAllowed() )
744 // no objections - close the dialog
747 EndModal(wxID_CANCEL
);
751 SetReturnCode(wxID_CANCEL
);
755 //else: request to Cancel ignored
758 void wxWizard::OnBackOrNext(wxCommandEvent
& event
)
760 wxASSERT_MSG( (event
.GetEventObject() == m_btnNext
) ||
761 (event
.GetEventObject() == m_btnPrev
),
762 wxT("unknown button") );
764 wxCHECK_RET( m_page
, _T("should have a valid current page") );
766 // ask the current page first: notice that we do it before calling
767 // GetNext/Prev() because the data transfered from the controls of the page
768 // may change the value returned by these methods
769 if ( !m_page
->Validate() || !m_page
->TransferDataFromWindow() )
771 // the page data is incorrect, don't do anything
775 bool forward
= event
.GetEventObject() == m_btnNext
;
780 page
= m_page
->GetNext();
784 page
= m_page
->GetPrev();
786 wxASSERT_MSG( page
, wxT("\"<Back\" button should have been disabled") );
789 // just pass to the new page (or maybe not - but we don't care here)
790 (void)ShowPage(page
, forward
);
793 void wxWizard::OnHelp(wxCommandEvent
& WXUNUSED(event
))
795 // this function probably can never be called when we don't have an active
796 // page, but a small extra check won't hurt
799 // Create and send the help event to the specific page handler
800 // event data contains the active page so that context-sensitive
802 wxWizardEvent
eventHelp(wxEVT_WIZARD_HELP
, GetId(), true, m_page
);
803 (void)m_page
->GetEventHandler()->ProcessEvent(eventHelp
);
807 void wxWizard::OnWizEvent(wxWizardEvent
& event
)
809 // the dialogs have wxWS_EX_BLOCK_EVENTS style on by default but we want to
810 // propagate wxEVT_WIZARD_XXX to the parent (if any), so do it manually
811 if ( !(GetExtraStyle() & wxWS_EX_BLOCK_EVENTS
) )
813 // the event will be propagated anyhow
818 wxWindow
*parent
= GetParent();
820 if ( !parent
|| !parent
->GetEventHandler()->ProcessEvent(event
) )
826 if ( ( !m_wasModal
) &&
828 ( event
.GetEventType() == wxEVT_WIZARD_FINISHED
||
829 event
.GetEventType() == wxEVT_WIZARD_CANCEL
837 // ----------------------------------------------------------------------------
839 // ----------------------------------------------------------------------------
841 wxWizardEvent::wxWizardEvent(wxEventType type
, int id
, bool direction
, wxWizardPage
* page
)
842 : wxNotifyEvent(type
, id
)
844 // Modified 10-20-2001 Robert Cavanaugh
845 // add the active page to the event data
846 m_direction
= direction
;
850 #endif // wxUSE_WIZARDDLG