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 // 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
);
58 wxSize
GetMaxChildSize();
62 wxSize
SiblingSize(wxSizerItem
*child
);
65 bool m_childSizeValid
;
69 // ----------------------------------------------------------------------------
70 // event tables and such
71 // ----------------------------------------------------------------------------
73 DEFINE_EVENT_TYPE(wxEVT_WIZARD_PAGE_CHANGED
)
74 DEFINE_EVENT_TYPE(wxEVT_WIZARD_PAGE_CHANGING
)
75 DEFINE_EVENT_TYPE(wxEVT_WIZARD_CANCEL
)
76 DEFINE_EVENT_TYPE(wxEVT_WIZARD_FINISHED
)
77 DEFINE_EVENT_TYPE(wxEVT_WIZARD_HELP
)
79 BEGIN_EVENT_TABLE(wxWizard
, wxDialog
)
80 EVT_BUTTON(wxID_CANCEL
, wxWizard::OnCancel
)
81 EVT_BUTTON(wxID_BACKWARD
, wxWizard::OnBackOrNext
)
82 EVT_BUTTON(wxID_FORWARD
, wxWizard::OnBackOrNext
)
83 EVT_BUTTON(wxID_HELP
, wxWizard::OnHelp
)
85 EVT_WIZARD_PAGE_CHANGED(wxID_ANY
, wxWizard::OnWizEvent
)
86 EVT_WIZARD_PAGE_CHANGING(wxID_ANY
, wxWizard::OnWizEvent
)
87 EVT_WIZARD_CANCEL(wxID_ANY
, wxWizard::OnWizEvent
)
88 EVT_WIZARD_FINISHED(wxID_ANY
, wxWizard::OnWizEvent
)
89 EVT_WIZARD_HELP(wxID_ANY
, wxWizard::OnWizEvent
)
92 IMPLEMENT_DYNAMIC_CLASS(wxWizard
, wxDialog
)
101 IMPLEMENT_ABSTRACT_CLASS(wxWizardPage
, wxPanel
)
102 IMPLEMENT_DYNAMIC_CLASS(wxWizardPageSimple
, wxWizardPage
)
103 IMPLEMENT_DYNAMIC_CLASS(wxWizardEvent
, wxNotifyEvent
)
105 // ============================================================================
107 // ============================================================================
109 // ----------------------------------------------------------------------------
111 // ----------------------------------------------------------------------------
113 void wxWizardPage::Init()
115 m_bitmap
= wxNullBitmap
;
118 wxWizardPage::wxWizardPage(wxWizard
*parent
,
119 const wxBitmap
& bitmap
,
120 const wxChar
*resource
)
122 Create(parent
, bitmap
, resource
);
125 bool wxWizardPage::Create(wxWizard
*parent
,
126 const wxBitmap
& bitmap
,
127 const wxChar
*resource
)
129 if ( !wxPanel::Create(parent
, wxID_ANY
) )
132 if ( resource
!= NULL
)
134 #if wxUSE_WX_RESOURCES
136 if ( !LoadFromResource(this, resource
) )
138 wxFAIL_MSG(wxT("wxWizardPage LoadFromResource failed!!!!"));
141 #endif // wxUSE_RESOURCES
146 // initially the page is hidden, it's shown only when it becomes current
152 // ----------------------------------------------------------------------------
153 // wxWizardPageSimple
154 // ----------------------------------------------------------------------------
156 wxWizardPage
*wxWizardPageSimple::GetPrev() const
161 wxWizardPage
*wxWizardPageSimple::GetNext() const
166 // ----------------------------------------------------------------------------
168 // ----------------------------------------------------------------------------
170 wxWizardSizer::wxWizardSizer(wxWizard
*owner
)
173 m_childSizeValid
= false;
176 void wxWizardSizer::RecalcSizes()
178 // Effect of this function depends on m_owner->m_page and
179 // it should be called whenever it changes (wxWizard::ShowPage)
180 if ( m_owner
->m_page
)
182 m_owner
->m_page
->SetSize(m_position
.x
,m_position
.y
, m_size
.x
,m_size
.y
);
186 wxSize
wxWizardSizer::CalcMin()
188 return m_owner
->GetPageSize();
191 wxSize
wxWizardSizer::GetMaxChildSize()
193 #if !defined(__WXDEBUG__)
194 if ( m_childSizeValid
)
199 wxSizerItemList::compatibility_iterator childNode
;
201 for(childNode
= m_children
.GetFirst(); childNode
;
202 childNode
= childNode
->GetNext())
204 wxSizerItem
*child
= childNode
->GetData();
205 maxOfMin
.IncTo(child
->CalcMin());
206 maxOfMin
.IncTo(SiblingSize(child
));
210 if ( m_childSizeValid
&& m_childSize
!= maxOfMin
)
212 wxFAIL_MSG( _T("Size changed in wxWizard::GetPageAreaSizer()")
213 _T("after RunWizard().\n")
214 _T("Did you forget to call GetSizer()->Fit(this) ")
215 _T("for some page?")) ;
219 #endif // __WXDEBUG__
221 if ( m_owner
->m_started
)
223 m_childSizeValid
= true;
224 m_childSize
= maxOfMin
;
230 int wxWizardSizer::Border() const
232 if ( m_owner
->m_calledSetBorder
)
233 return m_owner
->m_border
;
235 return m_children
.IsEmpty() ? 5 : 0;
238 wxSize
wxWizardSizer::SiblingSize(wxSizerItem
*child
)
242 if ( child
->IsWindow() )
244 wxWizardPage
*page
= wxDynamicCast(child
->GetWindow(), wxWizardPage
);
247 for ( wxWizardPage
*sibling
= page
->GetNext();
249 sibling
= sibling
->GetNext() )
251 if ( sibling
->GetSizer() )
253 maxSibling
.IncTo(sibling
->GetSizer()->CalcMin());
262 // ----------------------------------------------------------------------------
263 // generic wxWizard implementation
264 // ----------------------------------------------------------------------------
266 #if wxCHECK_VERSION(2, 7, 0)
267 #error "Fix wxGTK vs. wxMSW difference other way"
269 WX_DEFINE_ARRAY_PTR(wxWizard
*, wxModelessWizards
);
270 wxModelessWizards modelessWizards
;
273 void wxWizard::Init()
275 m_posWizard
= wxDefaultPosition
;
276 m_page
= (wxWizardPage
*)NULL
;
277 m_btnPrev
= m_btnNext
= NULL
;
279 m_sizerBmpAndPage
= NULL
;
281 m_calledSetBorder
= false;
284 modelessWizards
.Add(this);
287 bool wxWizard::Create(wxWindow
*parent
,
289 const wxString
& title
,
290 const wxBitmap
& bitmap
,
294 bool result
= wxDialog::Create(parent
,id
,title
,pos
,wxDefaultSize
,style
);
304 void wxWizard::AddBitmapRow(wxBoxSizer
*mainColumn
)
306 m_sizerBmpAndPage
= new wxBoxSizer(wxHORIZONTAL
);
309 1, // Vertically stretchable
310 wxEXPAND
// Horizonal stretching, no border
313 0, // No vertical stretching
314 wxEXPAND
// No border, (mostly useless) horizontal stretching
320 m_statbmp
= new wxStaticBitmap(this, wxID_ANY
, m_bitmap
);
321 m_sizerBmpAndPage
->Add(
323 0, // No horizontal stretching
324 wxALL
, // Border all around, top alignment
327 m_sizerBmpAndPage
->Add(
329 0, // No horizontal stretching
330 wxEXPAND
// No border, (mostly useless) vertical stretching
335 // Added to m_sizerBmpAndPage in FinishLayout
336 m_sizerPage
= new wxWizardSizer(this);
339 void wxWizard::AddStaticLine(wxBoxSizer
*mainColumn
)
343 new wxStaticLine(this, wxID_ANY
),
344 0, // Vertically unstretchable
345 wxEXPAND
| wxALL
, // Border all around, horizontally stretchable
349 0, // No vertical stretching
350 wxEXPAND
// No border, (mostly useless) horizontal stretching
354 #endif // wxUSE_STATLINE
357 void wxWizard::AddBackNextPair(wxBoxSizer
*buttonRow
)
359 wxASSERT_MSG( m_btnNext
&& m_btnPrev
,
360 _T("You must create the buttons before calling ")
361 _T("wxWizard::AddBackNextPair") );
363 // margin between Back and Next buttons
365 static const int BACKNEXT_MARGIN
= 10;
367 static const int BACKNEXT_MARGIN
= 0;
370 wxBoxSizer
*backNextPair
= new wxBoxSizer(wxHORIZONTAL
);
373 0, // No horizontal stretching
374 wxALL
, // Border all around
378 backNextPair
->Add(m_btnPrev
);
379 backNextPair
->Add(BACKNEXT_MARGIN
,0,
380 0, // No horizontal stretching
381 wxEXPAND
// No border, (mostly useless) vertical stretching
383 backNextPair
->Add(m_btnNext
);
386 void wxWizard::AddButtonRow(wxBoxSizer
*mainColumn
)
388 // the order in which the buttons are created determines the TAB order - at least under MSWindows...
389 // although the 'back' button appears before the 'next' button, a more userfriendly tab order is
390 // to activate the 'next' button first (create the next button before the back button).
391 // The reason is: The user will repeatedly enter information in the wizard pages and then wants to
392 // press 'next'. If a user uses mostly the keyboard, he would have to skip the 'back' button
393 // everytime. This is annoying. There is a second reason: RETURN acts as TAB. If the 'next'
394 // button comes first in the TAB order, the user can enter information very fast using the RETURN
395 // key to TAB to the next entry field and page. This would not be possible, if the 'back' button
396 // was created before the 'next' button.
398 bool isPda
= (wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA
);
399 int buttonStyle
= isPda
? wxBU_EXACTFIT
: 0;
401 wxBoxSizer
*buttonRow
= new wxBoxSizer(wxHORIZONTAL
);
403 if (GetExtraStyle() & wxWIZARD_EX_HELPBUTTON
)
406 0, // Vertically unstretchable
407 wxGROW
|wxALIGN_CENTRE
413 0, // Vertically unstretchable
414 wxALIGN_RIGHT
// Right aligned, no border
417 // Desired TAB order is 'next', 'cancel', 'help', 'back'. This makes the 'back' button the last control on the page.
418 // Create the buttons in the right order...
421 if (GetExtraStyle() & wxWIZARD_EX_HELPBUTTON
)
422 btnHelp
=new wxButton(this, wxID_HELP
, _("&Help"), wxDefaultPosition
, wxDefaultSize
, buttonStyle
);
425 m_btnNext
= new wxButton(this, wxID_FORWARD
, _("&Next >"));
426 wxButton
*btnCancel
=new wxButton(this, wxID_CANCEL
, _("&Cancel"), wxDefaultPosition
, wxDefaultSize
, buttonStyle
);
428 if (GetExtraStyle() & wxWIZARD_EX_HELPBUTTON
)
429 btnHelp
=new wxButton(this, wxID_HELP
, _("&Help"), wxDefaultPosition
, wxDefaultSize
, buttonStyle
);
431 m_btnPrev
= new wxButton(this, wxID_BACKWARD
, _("< &Back"), wxDefaultPosition
, wxDefaultSize
, buttonStyle
);
437 0, // Horizontally unstretchable
438 wxALL
, // Border all around, top aligned
442 // Put stretchable space between help button and others
443 buttonRow
->Add(0, 0, 1, wxALIGN_CENTRE
, 0);
447 AddBackNextPair(buttonRow
);
451 0, // Horizontally unstretchable
452 wxALL
, // Border all around, top aligned
457 void wxWizard::DoCreateControls()
459 // do nothing if the controls were already created
463 bool isPda
= (wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA
);
465 // Horizontal stretching, and if not PDA, border all around
466 int mainColumnSizerFlags
= isPda
? wxEXPAND
: wxALL
|wxEXPAND
;
468 // wxWindow::SetSizer will be called at end
469 wxBoxSizer
*windowSizer
= new wxBoxSizer(wxVERTICAL
);
471 wxBoxSizer
*mainColumn
= new wxBoxSizer(wxVERTICAL
);
474 1, // Vertical stretching
475 mainColumnSizerFlags
,
479 AddBitmapRow(mainColumn
);
482 AddStaticLine(mainColumn
);
484 AddButtonRow(mainColumn
);
486 // wxWindow::SetSizer should be followed by wxWindow::Fit, but
487 // this is done in FinishLayout anyway so why duplicate it
488 SetSizer(windowSizer
);
491 void wxWizard::SetPageSize(const wxSize
& size
)
493 wxCHECK_RET(!m_started
,wxT("wxWizard::SetPageSize after RunWizard"));
497 void wxWizard::FinishLayout()
499 bool isPda
= (wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA
);
501 // Set to enable wxWizardSizer::GetMaxChildSize
504 m_sizerBmpAndPage
->Add(
506 1, // Horizontal stretching
507 wxEXPAND
| wxALL
, // Vertically stretchable
508 m_sizerPage
->Border()
513 GetSizer()->SetSizeHints(this);
514 if ( m_posWizard
== wxDefaultPosition
)
519 void wxWizard::FitToPage(const wxWizardPage
*page
)
521 wxCHECK_RET(!m_started
,wxT("wxWizard::FitToPage after RunWizard"));
525 wxSize size
= page
->GetBestSize();
527 m_sizePage
.IncTo(size
);
529 page
= page
->GetNext();
533 bool wxWizard::ShowPage(wxWizardPage
*page
, bool goingForward
)
535 wxASSERT_MSG( page
!= m_page
, wxT("this is useless") );
537 // we'll use this to decide whether we have to change the label of this
538 // button or not (initially the label is "Next")
539 bool btnLabelWasNext
= true;
541 // Modified 10-20-2001 Robert Cavanaugh.
542 // Fixed bug for displaying a new bitmap
543 // in each *consecutive* page
545 // flag to indicate if this page uses a new bitmap
546 bool bmpIsDefault
= true;
548 // use these labels to determine if we need to change the bitmap
550 wxBitmap bmpPrev
, bmpCur
;
552 // check for previous page
555 // send the event to the old page
556 wxWizardEvent
event(wxEVT_WIZARD_PAGE_CHANGING
, GetId(), goingForward
, m_page
);
557 if ( m_page
->GetEventHandler()->ProcessEvent(event
) &&
560 // vetoed by the page
566 btnLabelWasNext
= HasNextPage(m_page
);
568 // Get the bitmap of the previous page (if it exists)
569 if ( m_page
->GetBitmap().Ok() )
571 bmpPrev
= m_page
->GetBitmap();
581 // terminate successfully
588 SetReturnCode(wxID_OK
);
592 // and notify the user code (this is especially useful for modeless
594 wxWizardEvent
event(wxEVT_WIZARD_FINISHED
, GetId(), false, 0);
595 (void)GetEventHandler()->ProcessEvent(event
);
600 // position and show the new page
601 (void)m_page
->TransferDataToWindow();
603 // wxWizardSizer::RecalcSizes wants to be called when m_page changes
604 m_sizerPage
->RecalcSizes();
606 // check if bitmap needs to be updated
607 // update default flag as well
608 if ( m_page
->GetBitmap().Ok() )
610 bmpCur
= m_page
->GetBitmap();
611 bmpIsDefault
= false;
615 // change the bitmap if:
616 // 1) a default bitmap was selected in constructor
617 // 2) this page was constructed with a bitmap
618 // 3) this bitmap is not the previous bitmap
619 if ( m_statbmp
&& (bmpCur
!= bmpPrev
) )
625 bmp
= m_page
->GetBitmap();
626 m_statbmp
->SetBitmap(bmp
);
630 // and update the buttons state
631 m_btnPrev
->Enable(HasPrevPage(m_page
));
633 bool hasNext
= HasNextPage(m_page
);
634 if ( btnLabelWasNext
!= hasNext
)
638 m_btnNext
->SetLabel(_("&Finish"));
640 m_btnNext
->SetLabel(_("&Next >"));
642 m_btnNext
->SetDefault();
643 // nothing to do: the label was already correct
645 // send the change event to the new page now
646 wxWizardEvent
event(wxEVT_WIZARD_PAGE_CHANGED
, GetId(), goingForward
, m_page
);
647 (void)m_page
->GetEventHandler()->ProcessEvent(event
);
649 // and finally show it
656 bool wxWizard::RunWizard(wxWizardPage
*firstPage
)
658 wxCHECK_MSG( firstPage
, false, wxT("can't run empty wizard") );
660 // This cannot be done sooner, because user can change layout options
664 // can't return false here because there is no old page
665 (void)ShowPage(firstPage
, true /* forward */);
667 modelessWizards
.Remove(this);
669 return ShowModal() == wxID_OK
;
672 wxWizardPage
*wxWizard::GetCurrentPage() const
677 wxSize
wxWizard::GetPageSize() const
679 wxSize
pageSize(GetManualPageSize());
680 pageSize
.IncTo(m_sizerPage
->GetMaxChildSize());
684 wxSizer
*wxWizard::GetPageAreaSizer() const
689 void wxWizard::SetBorder(int border
)
691 wxCHECK_RET(!m_started
,wxT("wxWizard::SetBorder after RunWizard"));
693 m_calledSetBorder
= true;
697 wxSize
wxWizard::GetManualPageSize() const
699 // default width and height of the page
700 int DEFAULT_PAGE_WIDTH
= 270;
701 int DEFAULT_PAGE_HEIGHT
= 270;
702 bool isPda
= (wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA
);
705 // Make the default page size small enough to fit on screen
706 DEFAULT_PAGE_WIDTH
= wxSystemSettings::GetMetric(wxSYS_SCREEN_X
) / 2;
707 DEFAULT_PAGE_HEIGHT
= wxSystemSettings::GetMetric(wxSYS_SCREEN_Y
) / 2;
710 wxSize
totalPageSize(DEFAULT_PAGE_WIDTH
,DEFAULT_PAGE_HEIGHT
);
712 totalPageSize
.IncTo(m_sizePage
);
716 totalPageSize
.IncTo(wxSize(0, m_bitmap
.GetHeight()));
719 return totalPageSize
;
722 void wxWizard::OnCancel(wxCommandEvent
& WXUNUSED(eventUnused
))
724 // this function probably can never be called when we don't have an active
725 // page, but a small extra check won't hurt
726 wxWindow
*win
= m_page
? (wxWindow
*)m_page
: (wxWindow
*)this;
728 wxWizardEvent
event(wxEVT_WIZARD_CANCEL
, GetId(), false, m_page
);
729 if ( !win
->GetEventHandler()->ProcessEvent(event
) || event
.IsAllowed() )
731 // no objections - close the dialog
734 EndModal(wxID_CANCEL
);
738 SetReturnCode(wxID_CANCEL
);
742 //else: request to Cancel ignored
745 void wxWizard::OnBackOrNext(wxCommandEvent
& event
)
747 wxASSERT_MSG( (event
.GetEventObject() == m_btnNext
) ||
748 (event
.GetEventObject() == m_btnPrev
),
749 wxT("unknown button") );
751 // ask the current page first: notice that we do it before calling
752 // GetNext/Prev() because the data transfered from the controls of the page
753 // may change the value returned by these methods
754 if ( m_page
&& (!m_page
->Validate() || !m_page
->TransferDataFromWindow()) )
756 // the page data is incorrect, don't do anything
760 bool forward
= event
.GetEventObject() == m_btnNext
;
765 page
= m_page
->GetNext();
769 page
= m_page
->GetPrev();
771 wxASSERT_MSG( page
, wxT("\"<Back\" button should have been disabled") );
774 // just pass to the new page (or may be not - but we don't care here)
775 (void)ShowPage(page
, forward
);
778 void wxWizard::OnHelp(wxCommandEvent
& WXUNUSED(event
))
780 // this function probably can never be called when we don't have an active
781 // page, but a small extra check won't hurt
784 // Create and send the help event to the specific page handler
785 // event data contains the active page so that context-sensitive
787 wxWizardEvent
eventHelp(wxEVT_WIZARD_HELP
, GetId(), true, m_page
);
788 (void)m_page
->GetEventHandler()->ProcessEvent(eventHelp
);
792 void wxWizard::OnWizEvent(wxWizardEvent
& event
)
794 // the dialogs have wxWS_EX_BLOCK_EVENTS style on by default but we want to
795 // propagate wxEVT_WIZARD_XXX to the parent (if any), so do it manually
796 if ( !(GetExtraStyle() & wxWS_EX_BLOCK_EVENTS
) )
798 // the event will be propagated anyhow
803 wxWindow
*parent
= GetParent();
805 if ( !parent
|| !parent
->GetEventHandler()->ProcessEvent(event
) )
811 if ( ( modelessWizards
.Index(this) != wxNOT_FOUND
) &&
813 ( event
.GetEventType() == wxEVT_WIZARD_FINISHED
||
814 event
.GetEventType() == wxEVT_WIZARD_CANCEL
818 modelessWizards
.Remove(this);
823 // ----------------------------------------------------------------------------
824 // our public interface
825 // ----------------------------------------------------------------------------
827 #if WXWIN_COMPATIBILITY_2_2
830 wxWizard
*wxWizardBase::Create(wxWindow
*parent
,
832 const wxString
& title
,
833 const wxBitmap
& bitmap
,
835 const wxSize
& WXUNUSED(size
))
837 return new wxWizard(parent
, id
, title
, bitmap
, pos
);
840 #endif // WXWIN_COMPATIBILITY_2_2
842 // ----------------------------------------------------------------------------
844 // ----------------------------------------------------------------------------
846 wxWizardEvent::wxWizardEvent(wxEventType type
, int id
, bool direction
, wxWizardPage
* page
)
847 : wxNotifyEvent(type
, id
)
849 // Modified 10-20-2001 Robert Cavanaugh
850 // add the active page to the event data
851 m_direction
= direction
;
855 #endif // wxUSE_WIZARDDLG