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)
12 // Copyright: (c) 1999 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
13 // Licence: wxWindows licence
14 ///////////////////////////////////////////////////////////////////////////////
16 // ============================================================================
18 // ============================================================================
20 // ----------------------------------------------------------------------------
22 // ----------------------------------------------------------------------------
24 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
25 #pragma implementation "wizardg.h"
28 // For compilers that support precompilation, includes "wx.h".
29 #include "wx/wxprec.h"
38 #include "wx/dynarray.h"
40 #include "wx/statbmp.h"
41 #include "wx/button.h"
44 #include "wx/statline.h"
46 #include "wx/settings.h"
48 #include "wx/wizard.h"
50 // ----------------------------------------------------------------------------
52 // ----------------------------------------------------------------------------
54 class wxWizardSizer
: public wxSizer
57 wxWizardSizer(wxWizard
*owner
);
62 wxSize
GetMaxChildSize();
66 wxSize
SiblingSize(wxSizerItem
*child
);
69 bool m_childSizeValid
;
73 // ----------------------------------------------------------------------------
74 // event tables and such
75 // ----------------------------------------------------------------------------
77 DEFINE_EVENT_TYPE(wxEVT_WIZARD_PAGE_CHANGED
)
78 DEFINE_EVENT_TYPE(wxEVT_WIZARD_PAGE_CHANGING
)
79 DEFINE_EVENT_TYPE(wxEVT_WIZARD_CANCEL
)
80 DEFINE_EVENT_TYPE(wxEVT_WIZARD_FINISHED
)
81 DEFINE_EVENT_TYPE(wxEVT_WIZARD_HELP
)
83 BEGIN_EVENT_TABLE(wxWizard
, wxDialog
)
84 EVT_BUTTON(wxID_CANCEL
, wxWizard::OnCancel
)
85 EVT_BUTTON(wxID_BACKWARD
, wxWizard::OnBackOrNext
)
86 EVT_BUTTON(wxID_FORWARD
, wxWizard::OnBackOrNext
)
87 EVT_BUTTON(wxID_HELP
, wxWizard::OnHelp
)
89 EVT_WIZARD_PAGE_CHANGED(wxID_ANY
, wxWizard::OnWizEvent
)
90 EVT_WIZARD_PAGE_CHANGING(wxID_ANY
, wxWizard::OnWizEvent
)
91 EVT_WIZARD_CANCEL(wxID_ANY
, wxWizard::OnWizEvent
)
92 EVT_WIZARD_FINISHED(wxID_ANY
, wxWizard::OnWizEvent
)
93 EVT_WIZARD_HELP(wxID_ANY
, wxWizard::OnWizEvent
)
96 IMPLEMENT_DYNAMIC_CLASS(wxWizard
, wxDialog
)
105 IMPLEMENT_ABSTRACT_CLASS(wxWizardPage
, wxPanel
)
106 IMPLEMENT_DYNAMIC_CLASS(wxWizardPageSimple
, wxWizardPage
)
107 IMPLEMENT_DYNAMIC_CLASS(wxWizardEvent
, wxNotifyEvent
)
109 // ============================================================================
111 // ============================================================================
113 // ----------------------------------------------------------------------------
115 // ----------------------------------------------------------------------------
117 void wxWizardPage::Init()
119 m_bitmap
= wxNullBitmap
;
122 wxWizardPage::wxWizardPage(wxWizard
*parent
,
123 const wxBitmap
& bitmap
,
124 const wxChar
*resource
)
126 Create(parent
, bitmap
, resource
);
129 bool wxWizardPage::Create(wxWizard
*parent
,
130 const wxBitmap
& bitmap
,
131 const wxChar
*resource
)
133 if ( !wxPanel::Create(parent
, wxID_ANY
) )
136 if ( resource
!= NULL
)
138 #if wxUSE_WX_RESOURCES
140 if ( !LoadFromResource(this, resource
) )
142 wxFAIL_MSG(wxT("wxWizardPage LoadFromResource failed!!!!"));
145 #endif // wxUSE_RESOURCES
150 // initially the page is hidden, it's shown only when it becomes current
156 // ----------------------------------------------------------------------------
157 // wxWizardPageSimple
158 // ----------------------------------------------------------------------------
160 wxWizardPage
*wxWizardPageSimple::GetPrev() const
165 wxWizardPage
*wxWizardPageSimple::GetNext() const
170 // ----------------------------------------------------------------------------
172 // ----------------------------------------------------------------------------
174 wxWizardSizer::wxWizardSizer(wxWizard
*owner
)
177 m_childSizeValid
= false;
180 void wxWizardSizer::RecalcSizes()
182 // Effect of this function depends on m_owner->m_page and
183 // it should be called whenever it changes (wxWizard::ShowPage)
184 if ( m_owner
->m_page
)
186 m_owner
->m_page
->SetSize(m_position
.x
,m_position
.y
, m_size
.x
,m_size
.y
);
190 wxSize
wxWizardSizer::CalcMin()
192 return m_owner
->GetPageSize();
195 wxSize
wxWizardSizer::GetMaxChildSize()
197 #if !defined(__WXDEBUG__)
198 if ( m_childSizeValid
)
203 wxSizerItemList::compatibility_iterator childNode
;
205 for(childNode
= m_children
.GetFirst(); childNode
;
206 childNode
= childNode
->GetNext())
208 wxSizerItem
*child
= childNode
->GetData();
209 maxOfMin
.IncTo(child
->CalcMin());
210 maxOfMin
.IncTo(SiblingSize(child
));
214 if ( m_childSizeValid
&& m_childSize
!= maxOfMin
)
216 wxFAIL_MSG( _T("Size changed in wxWizard::GetPageAreaSizer()")
217 _T("after RunWizard().\n")
218 _T("Did you forget to call GetSizer()->Fit(this) ")
219 _T("for some page?")) ;
223 #endif // __WXDEBUG__
225 if ( m_owner
->m_started
)
227 m_childSizeValid
= true;
228 m_childSize
= maxOfMin
;
234 int wxWizardSizer::Border() const
236 if ( m_owner
->m_calledSetBorder
)
237 return m_owner
->m_border
;
239 return m_children
.IsEmpty() ? 5 : 0;
242 wxSize
wxWizardSizer::SiblingSize(wxSizerItem
*child
)
246 if ( child
->IsWindow() )
248 wxWizardPage
*page
= wxDynamicCast(child
->GetWindow(), wxWizardPage
);
251 for ( wxWizardPage
*sibling
= page
->GetNext();
253 sibling
= sibling
->GetNext() )
255 if ( sibling
->GetSizer() )
257 maxSibling
.IncTo(sibling
->GetSizer()->CalcMin());
266 // ----------------------------------------------------------------------------
267 // generic wxWizard implementation
268 // ----------------------------------------------------------------------------
270 #if wxCHECK_VERSION(2, 7, 0)
271 #error "Fix wxGTK vs. wxMSW difference other way"
273 WX_DEFINE_ARRAY_PTR(wxWizard
*, wxModelessWizards
);
274 wxModelessWizards modelessWizards
;
277 void wxWizard::Init()
279 m_posWizard
= wxDefaultPosition
;
280 m_page
= (wxWizardPage
*)NULL
;
281 m_btnPrev
= m_btnNext
= NULL
;
283 m_sizerBmpAndPage
= NULL
;
285 m_calledSetBorder
= false;
288 modelessWizards
.Add(this);
291 bool wxWizard::Create(wxWindow
*parent
,
293 const wxString
& title
,
294 const wxBitmap
& bitmap
,
298 bool result
= wxDialog::Create(parent
,id
,title
,pos
,wxDefaultSize
,style
);
308 void wxWizard::AddBitmapRow(wxBoxSizer
*mainColumn
)
310 m_sizerBmpAndPage
= new wxBoxSizer(wxHORIZONTAL
);
313 1, // Vertically stretchable
314 wxEXPAND
// Horizonal stretching, no border
317 0, // No vertical stretching
318 wxEXPAND
// No border, (mostly useless) horizontal stretching
324 m_statbmp
= new wxStaticBitmap(this, wxID_ANY
, m_bitmap
);
325 m_sizerBmpAndPage
->Add(
327 0, // No horizontal stretching
328 wxALL
, // Border all around, top alignment
331 m_sizerBmpAndPage
->Add(
333 0, // No horizontal stretching
334 wxEXPAND
// No border, (mostly useless) vertical stretching
339 // Added to m_sizerBmpAndPage in FinishLayout
340 m_sizerPage
= new wxWizardSizer(this);
343 void wxWizard::AddStaticLine(wxBoxSizer
*mainColumn
)
347 new wxStaticLine(this, wxID_ANY
),
348 0, // Vertically unstretchable
349 wxEXPAND
| wxALL
, // Border all around, horizontally stretchable
353 0, // No vertical stretching
354 wxEXPAND
// No border, (mostly useless) horizontal stretching
358 #endif // wxUSE_STATLINE
361 void wxWizard::AddBackNextPair(wxBoxSizer
*buttonRow
)
363 wxASSERT_MSG( m_btnNext
&& m_btnPrev
,
364 _T("You must create the buttons before calling ")
365 _T("wxWizard::AddBackNextPair") );
367 // margin between Back and Next buttons
369 static const int BACKNEXT_MARGIN
= 10;
371 static const int BACKNEXT_MARGIN
= 0;
374 wxBoxSizer
*backNextPair
= new wxBoxSizer(wxHORIZONTAL
);
377 0, // No horizontal stretching
378 wxALL
, // Border all around
382 backNextPair
->Add(m_btnPrev
);
383 backNextPair
->Add(BACKNEXT_MARGIN
,0,
384 0, // No horizontal stretching
385 wxEXPAND
// No border, (mostly useless) vertical stretching
387 backNextPair
->Add(m_btnNext
);
390 void wxWizard::AddButtonRow(wxBoxSizer
*mainColumn
)
392 // the order in which the buttons are created determines the TAB order - at least under MSWindows...
393 // although the 'back' button appears before the 'next' button, a more userfriendly tab order is
394 // to activate the 'next' button first (create the next button before the back button).
395 // The reason is: The user will repeatedly enter information in the wizard pages and then wants to
396 // press 'next'. If a user uses mostly the keyboard, he would have to skip the 'back' button
397 // everytime. This is annoying. There is a second reason: RETURN acts as TAB. If the 'next'
398 // button comes first in the TAB order, the user can enter information very fast using the RETURN
399 // key to TAB to the next entry field and page. This would not be possible, if the 'back' button
400 // was created before the 'next' button.
402 bool isPda
= (wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA
);
403 int buttonStyle
= isPda
? wxBU_EXACTFIT
: 0;
405 wxBoxSizer
*buttonRow
= new wxBoxSizer(wxHORIZONTAL
);
407 if (GetExtraStyle() & wxWIZARD_EX_HELPBUTTON
)
410 0, // Vertically unstretchable
411 wxGROW
|wxALIGN_CENTRE
417 0, // Vertically unstretchable
418 wxALIGN_RIGHT
// Right aligned, no border
421 // Desired TAB order is 'next', 'cancel', 'help', 'back'. This makes the 'back' button the last control on the page.
422 // Create the buttons in the right order...
425 if (GetExtraStyle() & wxWIZARD_EX_HELPBUTTON
)
426 btnHelp
=new wxButton(this, wxID_HELP
, _("&Help"), wxDefaultPosition
, wxDefaultSize
, buttonStyle
);
429 m_btnNext
= new wxButton(this, wxID_FORWARD
, _("&Next >"));
430 wxButton
*btnCancel
=new wxButton(this, wxID_CANCEL
, _("&Cancel"), wxDefaultPosition
, wxDefaultSize
, buttonStyle
);
432 if (GetExtraStyle() & wxWIZARD_EX_HELPBUTTON
)
433 btnHelp
=new wxButton(this, wxID_HELP
, _("&Help"), wxDefaultPosition
, wxDefaultSize
, buttonStyle
);
435 m_btnPrev
= new wxButton(this, wxID_BACKWARD
, _("< &Back"), wxDefaultPosition
, wxDefaultSize
, buttonStyle
);
441 0, // Horizontally unstretchable
442 wxALL
, // Border all around, top aligned
446 // Put stretchable space between help button and others
447 buttonRow
->Add(0, 0, 1, wxALIGN_CENTRE
, 0);
451 AddBackNextPair(buttonRow
);
455 0, // Horizontally unstretchable
456 wxALL
, // Border all around, top aligned
461 void wxWizard::DoCreateControls()
463 // do nothing if the controls were already created
467 bool isPda
= (wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA
);
469 // Horizontal stretching, and if not PDA, border all around
470 int mainColumnSizerFlags
= isPda
? wxEXPAND
: wxALL
|wxEXPAND
;
472 // wxWindow::SetSizer will be called at end
473 wxBoxSizer
*windowSizer
= new wxBoxSizer(wxVERTICAL
);
475 wxBoxSizer
*mainColumn
= new wxBoxSizer(wxVERTICAL
);
478 1, // Vertical stretching
479 mainColumnSizerFlags
,
483 AddBitmapRow(mainColumn
);
486 AddStaticLine(mainColumn
);
488 AddButtonRow(mainColumn
);
490 // wxWindow::SetSizer should be followed by wxWindow::Fit, but
491 // this is done in FinishLayout anyway so why duplicate it
492 SetSizer(windowSizer
);
495 void wxWizard::SetPageSize(const wxSize
& size
)
497 wxCHECK_RET(!m_started
,wxT("wxWizard::SetPageSize after RunWizard"));
501 void wxWizard::FinishLayout()
503 bool isPda
= (wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA
);
505 // Set to enable wxWizardSizer::GetMaxChildSize
508 m_sizerBmpAndPage
->Add(
510 1, // Horizontal stretching
511 wxEXPAND
| wxALL
, // Vertically stretchable
512 m_sizerPage
->Border()
517 GetSizer()->SetSizeHints(this);
518 if ( m_posWizard
== wxDefaultPosition
)
523 void wxWizard::FitToPage(const wxWizardPage
*page
)
525 wxCHECK_RET(!m_started
,wxT("wxWizard::FitToPage after RunWizard"));
529 wxSize size
= page
->GetBestSize();
531 m_sizePage
.IncTo(size
);
533 page
= page
->GetNext();
537 bool wxWizard::ShowPage(wxWizardPage
*page
, bool goingForward
)
539 wxASSERT_MSG( page
!= m_page
, wxT("this is useless") );
541 // we'll use this to decide whether we have to change the label of this
542 // button or not (initially the label is "Next")
543 bool btnLabelWasNext
= true;
545 // Modified 10-20-2001 Robert Cavanaugh.
546 // Fixed bug for displaying a new bitmap
547 // in each *consecutive* page
549 // flag to indicate if this page uses a new bitmap
550 bool bmpIsDefault
= true;
552 // use these labels to determine if we need to change the bitmap
554 wxBitmap bmpPrev
, bmpCur
;
556 // check for previous page
559 // send the event to the old page
560 wxWizardEvent
event(wxEVT_WIZARD_PAGE_CHANGING
, GetId(), goingForward
, m_page
);
561 if ( m_page
->GetEventHandler()->ProcessEvent(event
) &&
564 // vetoed by the page
570 btnLabelWasNext
= HasNextPage(m_page
);
572 // Get the bitmap of the previous page (if it exists)
573 if ( m_page
->GetBitmap().Ok() )
575 bmpPrev
= m_page
->GetBitmap();
585 // terminate successfully
592 SetReturnCode(wxID_OK
);
596 // and notify the user code (this is especially useful for modeless
598 wxWizardEvent
event(wxEVT_WIZARD_FINISHED
, GetId(), false, 0);
599 (void)GetEventHandler()->ProcessEvent(event
);
604 // position and show the new page
605 (void)m_page
->TransferDataToWindow();
607 // wxWizardSizer::RecalcSizes wants to be called when m_page changes
608 m_sizerPage
->RecalcSizes();
610 // check if bitmap needs to be updated
611 // update default flag as well
612 if ( m_page
->GetBitmap().Ok() )
614 bmpCur
= m_page
->GetBitmap();
615 bmpIsDefault
= false;
619 // change the bitmap if:
620 // 1) a default bitmap was selected in constructor
621 // 2) this page was constructed with a bitmap
622 // 3) this bitmap is not the previous bitmap
623 if ( m_statbmp
&& (bmpCur
!= bmpPrev
) )
629 bmp
= m_page
->GetBitmap();
630 m_statbmp
->SetBitmap(bmp
);
634 // and update the buttons state
635 m_btnPrev
->Enable(HasPrevPage(m_page
));
637 bool hasNext
= HasNextPage(m_page
);
638 if ( btnLabelWasNext
!= hasNext
)
642 m_btnNext
->SetLabel(_("&Finish"));
644 m_btnNext
->SetLabel(_("&Next >"));
646 m_btnNext
->SetDefault();
647 // nothing to do: the label was already correct
649 // send the change event to the new page now
650 wxWizardEvent
event(wxEVT_WIZARD_PAGE_CHANGED
, GetId(), goingForward
, m_page
);
651 (void)m_page
->GetEventHandler()->ProcessEvent(event
);
653 // and finally show it
660 bool wxWizard::RunWizard(wxWizardPage
*firstPage
)
662 wxCHECK_MSG( firstPage
, false, wxT("can't run empty wizard") );
664 // This cannot be done sooner, because user can change layout options
668 // can't return false here because there is no old page
669 (void)ShowPage(firstPage
, true /* forward */);
671 modelessWizards
.Remove(this);
673 return ShowModal() == wxID_OK
;
676 wxWizardPage
*wxWizard::GetCurrentPage() const
681 wxSize
wxWizard::GetPageSize() const
683 wxSize
pageSize(GetManualPageSize());
684 pageSize
.IncTo(m_sizerPage
->GetMaxChildSize());
688 wxSizer
*wxWizard::GetPageAreaSizer() const
693 void wxWizard::SetBorder(int border
)
695 wxCHECK_RET(!m_started
,wxT("wxWizard::SetBorder after RunWizard"));
697 m_calledSetBorder
= true;
701 wxSize
wxWizard::GetManualPageSize() const
703 // default width and height of the page
704 int DEFAULT_PAGE_WIDTH
= 270;
705 int DEFAULT_PAGE_HEIGHT
= 270;
706 bool isPda
= (wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA
);
709 // Make the default page size small enough to fit on screen
710 DEFAULT_PAGE_WIDTH
= wxSystemSettings::GetMetric(wxSYS_SCREEN_X
) / 2;
711 DEFAULT_PAGE_HEIGHT
= wxSystemSettings::GetMetric(wxSYS_SCREEN_Y
) / 2;
714 wxSize
totalPageSize(DEFAULT_PAGE_WIDTH
,DEFAULT_PAGE_HEIGHT
);
716 totalPageSize
.IncTo(m_sizePage
);
720 totalPageSize
.IncTo(wxSize(0, m_bitmap
.GetHeight()));
723 return totalPageSize
;
726 void wxWizard::OnCancel(wxCommandEvent
& WXUNUSED(eventUnused
))
728 // this function probably can never be called when we don't have an active
729 // page, but a small extra check won't hurt
730 wxWindow
*win
= m_page
? (wxWindow
*)m_page
: (wxWindow
*)this;
732 wxWizardEvent
event(wxEVT_WIZARD_CANCEL
, GetId(), false, m_page
);
733 if ( !win
->GetEventHandler()->ProcessEvent(event
) || event
.IsAllowed() )
735 // no objections - close the dialog
738 EndModal(wxID_CANCEL
);
742 SetReturnCode(wxID_CANCEL
);
746 //else: request to Cancel ignored
749 void wxWizard::OnBackOrNext(wxCommandEvent
& event
)
751 wxASSERT_MSG( (event
.GetEventObject() == m_btnNext
) ||
752 (event
.GetEventObject() == m_btnPrev
),
753 wxT("unknown button") );
755 // ask the current page first: notice that we do it before calling
756 // GetNext/Prev() because the data transfered from the controls of the page
757 // may change the value returned by these methods
758 if ( m_page
&& (!m_page
->Validate() || !m_page
->TransferDataFromWindow()) )
760 // the page data is incorrect, don't do anything
764 bool forward
= event
.GetEventObject() == m_btnNext
;
769 page
= m_page
->GetNext();
773 page
= m_page
->GetPrev();
775 wxASSERT_MSG( page
, wxT("\"<Back\" button should have been disabled") );
778 // just pass to the new page (or may be not - but we don't care here)
779 (void)ShowPage(page
, forward
);
782 void wxWizard::OnHelp(wxCommandEvent
& WXUNUSED(event
))
784 // this function probably can never be called when we don't have an active
785 // page, but a small extra check won't hurt
788 // Create and send the help event to the specific page handler
789 // event data contains the active page so that context-sensitive
791 wxWizardEvent
eventHelp(wxEVT_WIZARD_HELP
, GetId(), true, m_page
);
792 (void)m_page
->GetEventHandler()->ProcessEvent(eventHelp
);
796 void wxWizard::OnWizEvent(wxWizardEvent
& event
)
798 // the dialogs have wxWS_EX_BLOCK_EVENTS style on by default but we want to
799 // propagate wxEVT_WIZARD_XXX to the parent (if any), so do it manually
800 if ( !(GetExtraStyle() & wxWS_EX_BLOCK_EVENTS
) )
802 // the event will be propagated anyhow
807 wxWindow
*parent
= GetParent();
809 if ( !parent
|| !parent
->GetEventHandler()->ProcessEvent(event
) )
815 if ( ( modelessWizards
.Index(this) != wxNOT_FOUND
) &&
817 ( event
.GetEventType() == wxEVT_WIZARD_FINISHED
||
818 event
.GetEventType() == wxEVT_WIZARD_CANCEL
822 modelessWizards
.Remove(this);
827 // ----------------------------------------------------------------------------
828 // our public interface
829 // ----------------------------------------------------------------------------
831 #if WXWIN_COMPATIBILITY_2_2
834 wxWizard
*wxWizardBase::Create(wxWindow
*parent
,
836 const wxString
& title
,
837 const wxBitmap
& bitmap
,
839 const wxSize
& WXUNUSED(size
))
841 return new wxWizard(parent
, id
, title
, bitmap
, pos
);
844 #endif // WXWIN_COMPATIBILITY_2_2
846 // ----------------------------------------------------------------------------
848 // ----------------------------------------------------------------------------
850 wxWizardEvent::wxWizardEvent(wxEventType type
, int id
, bool direction
, wxWizardPage
* page
)
851 : wxNotifyEvent(type
, id
)
853 // Modified 10-20-2001 Robert Cavanaugh
854 // add the active page to the event data
855 m_direction
= direction
;
859 #endif // wxUSE_WIZARDDLG