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 // FIXME: this is a hack
267 WX_DEFINE_ARRAY_PTR(wxWizard
*, wxModelessWizards
);
268 static wxModelessWizards modelessWizards
;
270 void wxWizard::Init()
272 m_posWizard
= wxDefaultPosition
;
273 m_page
= (wxWizardPage
*)NULL
;
274 m_btnPrev
= m_btnNext
= NULL
;
276 m_sizerBmpAndPage
= NULL
;
278 m_calledSetBorder
= false;
281 modelessWizards
.Add(this);
284 bool wxWizard::Create(wxWindow
*parent
,
286 const wxString
& title
,
287 const wxBitmap
& bitmap
,
291 bool result
= wxDialog::Create(parent
,id
,title
,pos
,wxDefaultSize
,style
);
301 void wxWizard::AddBitmapRow(wxBoxSizer
*mainColumn
)
303 m_sizerBmpAndPage
= new wxBoxSizer(wxHORIZONTAL
);
306 1, // Vertically stretchable
307 wxEXPAND
// Horizonal stretching, no border
310 0, // No vertical stretching
311 wxEXPAND
// No border, (mostly useless) horizontal stretching
317 m_statbmp
= new wxStaticBitmap(this, wxID_ANY
, m_bitmap
);
318 m_sizerBmpAndPage
->Add(
320 0, // No horizontal stretching
321 wxALL
, // Border all around, top alignment
324 m_sizerBmpAndPage
->Add(
326 0, // No horizontal stretching
327 wxEXPAND
// No border, (mostly useless) vertical stretching
332 // Added to m_sizerBmpAndPage in FinishLayout
333 m_sizerPage
= new wxWizardSizer(this);
336 void wxWizard::AddStaticLine(wxBoxSizer
*mainColumn
)
340 new wxStaticLine(this, wxID_ANY
),
341 0, // Vertically unstretchable
342 wxEXPAND
| wxALL
, // Border all around, horizontally stretchable
346 0, // No vertical stretching
347 wxEXPAND
// No border, (mostly useless) horizontal stretching
351 #endif // wxUSE_STATLINE
354 void wxWizard::AddBackNextPair(wxBoxSizer
*buttonRow
)
356 wxASSERT_MSG( m_btnNext
&& m_btnPrev
,
357 _T("You must create the buttons before calling ")
358 _T("wxWizard::AddBackNextPair") );
360 // margin between Back and Next buttons
362 static const int BACKNEXT_MARGIN
= 10;
364 static const int BACKNEXT_MARGIN
= 0;
367 wxBoxSizer
*backNextPair
= new wxBoxSizer(wxHORIZONTAL
);
370 0, // No horizontal stretching
371 wxALL
, // Border all around
375 backNextPair
->Add(m_btnPrev
);
376 backNextPair
->Add(BACKNEXT_MARGIN
,0,
377 0, // No horizontal stretching
378 wxEXPAND
// No border, (mostly useless) vertical stretching
380 backNextPair
->Add(m_btnNext
);
383 void wxWizard::AddButtonRow(wxBoxSizer
*mainColumn
)
385 // the order in which the buttons are created determines the TAB order - at least under MSWindows...
386 // although the 'back' button appears before the 'next' button, a more userfriendly tab order is
387 // to activate the 'next' button first (create the next button before the back button).
388 // The reason is: The user will repeatedly enter information in the wizard pages and then wants to
389 // press 'next'. If a user uses mostly the keyboard, he would have to skip the 'back' button
390 // everytime. This is annoying. There is a second reason: RETURN acts as TAB. If the 'next'
391 // button comes first in the TAB order, the user can enter information very fast using the RETURN
392 // key to TAB to the next entry field and page. This would not be possible, if the 'back' button
393 // was created before the 'next' button.
395 bool isPda
= (wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA
);
396 int buttonStyle
= isPda
? wxBU_EXACTFIT
: 0;
398 wxBoxSizer
*buttonRow
= new wxBoxSizer(wxHORIZONTAL
);
400 if (GetExtraStyle() & wxWIZARD_EX_HELPBUTTON
)
403 0, // Vertically unstretchable
404 wxGROW
|wxALIGN_CENTRE
410 0, // Vertically unstretchable
411 wxALIGN_RIGHT
// Right aligned, no border
414 // Desired TAB order is 'next', 'cancel', 'help', 'back'. This makes the 'back' button the last control on the page.
415 // Create the buttons in the right order...
418 if (GetExtraStyle() & wxWIZARD_EX_HELPBUTTON
)
419 btnHelp
=new wxButton(this, wxID_HELP
, _("&Help"), wxDefaultPosition
, wxDefaultSize
, buttonStyle
);
422 m_btnNext
= new wxButton(this, wxID_FORWARD
, _("&Next >"));
423 wxButton
*btnCancel
=new wxButton(this, wxID_CANCEL
, _("&Cancel"), wxDefaultPosition
, wxDefaultSize
, buttonStyle
);
425 if (GetExtraStyle() & wxWIZARD_EX_HELPBUTTON
)
426 btnHelp
=new wxButton(this, wxID_HELP
, _("&Help"), wxDefaultPosition
, wxDefaultSize
, buttonStyle
);
428 m_btnPrev
= new wxButton(this, wxID_BACKWARD
, _("< &Back"), wxDefaultPosition
, wxDefaultSize
, buttonStyle
);
434 0, // Horizontally unstretchable
435 wxALL
, // Border all around, top aligned
439 // Put stretchable space between help button and others
440 buttonRow
->Add(0, 0, 1, wxALIGN_CENTRE
, 0);
444 AddBackNextPair(buttonRow
);
448 0, // Horizontally unstretchable
449 wxALL
, // Border all around, top aligned
454 void wxWizard::DoCreateControls()
456 // do nothing if the controls were already created
460 bool isPda
= (wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA
);
462 // Horizontal stretching, and if not PDA, border all around
463 int mainColumnSizerFlags
= isPda
? wxEXPAND
: wxALL
|wxEXPAND
;
465 // wxWindow::SetSizer will be called at end
466 wxBoxSizer
*windowSizer
= new wxBoxSizer(wxVERTICAL
);
468 wxBoxSizer
*mainColumn
= new wxBoxSizer(wxVERTICAL
);
471 1, // Vertical stretching
472 mainColumnSizerFlags
,
476 AddBitmapRow(mainColumn
);
479 AddStaticLine(mainColumn
);
481 AddButtonRow(mainColumn
);
483 // wxWindow::SetSizer should be followed by wxWindow::Fit, but
484 // this is done in FinishLayout anyway so why duplicate it
485 SetSizer(windowSizer
);
488 void wxWizard::SetPageSize(const wxSize
& size
)
490 wxCHECK_RET(!m_started
,wxT("wxWizard::SetPageSize after RunWizard"));
494 void wxWizard::FinishLayout()
496 bool isPda
= (wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA
);
498 // Set to enable wxWizardSizer::GetMaxChildSize
501 m_sizerBmpAndPage
->Add(
503 1, // Horizontal stretching
504 wxEXPAND
| wxALL
, // Vertically stretchable
505 m_sizerPage
->Border()
510 GetSizer()->SetSizeHints(this);
511 if ( m_posWizard
== wxDefaultPosition
)
516 void wxWizard::FitToPage(const wxWizardPage
*page
)
518 wxCHECK_RET(!m_started
,wxT("wxWizard::FitToPage after RunWizard"));
522 wxSize size
= page
->GetBestSize();
524 m_sizePage
.IncTo(size
);
526 page
= page
->GetNext();
530 bool wxWizard::ShowPage(wxWizardPage
*page
, bool goingForward
)
532 wxASSERT_MSG( page
!= m_page
, wxT("this is useless") );
534 // we'll use this to decide whether we have to change the label of this
535 // button or not (initially the label is "Next")
536 bool btnLabelWasNext
= true;
538 // Modified 10-20-2001 Robert Cavanaugh.
539 // Fixed bug for displaying a new bitmap
540 // in each *consecutive* page
542 // flag to indicate if this page uses a new bitmap
543 bool bmpIsDefault
= true;
545 // use these labels to determine if we need to change the bitmap
547 wxBitmap bmpPrev
, bmpCur
;
549 // check for previous page
552 // send the event to the old page
553 wxWizardEvent
event(wxEVT_WIZARD_PAGE_CHANGING
, GetId(), goingForward
, m_page
);
554 if ( m_page
->GetEventHandler()->ProcessEvent(event
) &&
557 // vetoed by the page
563 btnLabelWasNext
= HasNextPage(m_page
);
565 // Get the bitmap of the previous page (if it exists)
566 if ( m_page
->GetBitmap().Ok() )
568 bmpPrev
= m_page
->GetBitmap();
578 // terminate successfully
585 SetReturnCode(wxID_OK
);
589 // and notify the user code (this is especially useful for modeless
591 wxWizardEvent
event(wxEVT_WIZARD_FINISHED
, GetId(), false, 0);
592 (void)GetEventHandler()->ProcessEvent(event
);
597 // position and show the new page
598 (void)m_page
->TransferDataToWindow();
600 // wxWizardSizer::RecalcSizes wants to be called when m_page changes
601 m_sizerPage
->RecalcSizes();
603 // check if bitmap needs to be updated
604 // update default flag as well
605 if ( m_page
->GetBitmap().Ok() )
607 bmpCur
= m_page
->GetBitmap();
608 bmpIsDefault
= false;
612 // change the bitmap if:
613 // 1) a default bitmap was selected in constructor
614 // 2) this page was constructed with a bitmap
615 // 3) this bitmap is not the previous bitmap
616 if ( m_statbmp
&& (bmpCur
!= bmpPrev
) )
622 bmp
= m_page
->GetBitmap();
623 m_statbmp
->SetBitmap(bmp
);
627 // and update the buttons state
628 m_btnPrev
->Enable(HasPrevPage(m_page
));
630 bool hasNext
= HasNextPage(m_page
);
631 if ( btnLabelWasNext
!= hasNext
)
635 m_btnNext
->SetLabel(_("&Finish"));
637 m_btnNext
->SetLabel(_("&Next >"));
639 m_btnNext
->SetDefault();
640 // nothing to do: the label was already correct
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
653 bool wxWizard::RunWizard(wxWizardPage
*firstPage
)
655 wxCHECK_MSG( firstPage
, false, wxT("can't run empty wizard") );
657 // This cannot be done sooner, because user can change layout options
661 // can't return false here because there is no old page
662 (void)ShowPage(firstPage
, true /* forward */);
664 modelessWizards
.Remove(this);
666 return ShowModal() == wxID_OK
;
669 wxWizardPage
*wxWizard::GetCurrentPage() const
674 wxSize
wxWizard::GetPageSize() const
676 wxSize
pageSize(GetManualPageSize());
677 pageSize
.IncTo(m_sizerPage
->GetMaxChildSize());
681 wxSizer
*wxWizard::GetPageAreaSizer() const
686 void wxWizard::SetBorder(int border
)
688 wxCHECK_RET(!m_started
,wxT("wxWizard::SetBorder after RunWizard"));
690 m_calledSetBorder
= true;
694 wxSize
wxWizard::GetManualPageSize() const
696 // default width and height of the page
697 int DEFAULT_PAGE_WIDTH
= 270;
698 int DEFAULT_PAGE_HEIGHT
= 270;
699 bool isPda
= (wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA
);
702 // Make the default page size small enough to fit on screen
703 DEFAULT_PAGE_WIDTH
= wxSystemSettings::GetMetric(wxSYS_SCREEN_X
) / 2;
704 DEFAULT_PAGE_HEIGHT
= wxSystemSettings::GetMetric(wxSYS_SCREEN_Y
) / 2;
707 wxSize
totalPageSize(DEFAULT_PAGE_WIDTH
,DEFAULT_PAGE_HEIGHT
);
709 totalPageSize
.IncTo(m_sizePage
);
713 totalPageSize
.IncTo(wxSize(0, m_bitmap
.GetHeight()));
716 return totalPageSize
;
719 void wxWizard::OnCancel(wxCommandEvent
& WXUNUSED(eventUnused
))
721 // this function probably can never be called when we don't have an active
722 // page, but a small extra check won't hurt
723 wxWindow
*win
= m_page
? (wxWindow
*)m_page
: (wxWindow
*)this;
725 wxWizardEvent
event(wxEVT_WIZARD_CANCEL
, GetId(), false, m_page
);
726 if ( !win
->GetEventHandler()->ProcessEvent(event
) || event
.IsAllowed() )
728 // no objections - close the dialog
731 EndModal(wxID_CANCEL
);
735 SetReturnCode(wxID_CANCEL
);
739 //else: request to Cancel ignored
742 void wxWizard::OnBackOrNext(wxCommandEvent
& event
)
744 wxASSERT_MSG( (event
.GetEventObject() == m_btnNext
) ||
745 (event
.GetEventObject() == m_btnPrev
),
746 wxT("unknown button") );
748 // ask the current page first: notice that we do it before calling
749 // GetNext/Prev() because the data transfered from the controls of the page
750 // may change the value returned by these methods
751 if ( m_page
&& (!m_page
->Validate() || !m_page
->TransferDataFromWindow()) )
753 // the page data is incorrect, don't do anything
757 bool forward
= event
.GetEventObject() == m_btnNext
;
762 page
= m_page
->GetNext();
766 page
= m_page
->GetPrev();
768 wxASSERT_MSG( page
, wxT("\"<Back\" button should have been disabled") );
771 // just pass to the new page (or may be not - but we don't care here)
772 (void)ShowPage(page
, forward
);
775 void wxWizard::OnHelp(wxCommandEvent
& WXUNUSED(event
))
777 // this function probably can never be called when we don't have an active
778 // page, but a small extra check won't hurt
781 // Create and send the help event to the specific page handler
782 // event data contains the active page so that context-sensitive
784 wxWizardEvent
eventHelp(wxEVT_WIZARD_HELP
, GetId(), true, m_page
);
785 (void)m_page
->GetEventHandler()->ProcessEvent(eventHelp
);
789 void wxWizard::OnWizEvent(wxWizardEvent
& event
)
791 // the dialogs have wxWS_EX_BLOCK_EVENTS style on by default but we want to
792 // propagate wxEVT_WIZARD_XXX to the parent (if any), so do it manually
793 if ( !(GetExtraStyle() & wxWS_EX_BLOCK_EVENTS
) )
795 // the event will be propagated anyhow
800 wxWindow
*parent
= GetParent();
802 if ( !parent
|| !parent
->GetEventHandler()->ProcessEvent(event
) )
808 if ( ( modelessWizards
.Index(this) != wxNOT_FOUND
) &&
810 ( event
.GetEventType() == wxEVT_WIZARD_FINISHED
||
811 event
.GetEventType() == wxEVT_WIZARD_CANCEL
815 modelessWizards
.Remove(this);
820 // ----------------------------------------------------------------------------
821 // our public interface
822 // ----------------------------------------------------------------------------
824 #if WXWIN_COMPATIBILITY_2_2
827 wxWizard
*wxWizardBase::Create(wxWindow
*parent
,
829 const wxString
& title
,
830 const wxBitmap
& bitmap
,
832 const wxSize
& WXUNUSED(size
))
834 return new wxWizard(parent
, id
, title
, bitmap
, pos
);
837 #endif // WXWIN_COMPATIBILITY_2_2
839 // ----------------------------------------------------------------------------
841 // ----------------------------------------------------------------------------
843 wxWizardEvent::wxWizardEvent(wxEventType type
, int id
, bool direction
, wxWizardPage
* page
)
844 : wxNotifyEvent(type
, id
)
846 // Modified 10-20-2001 Robert Cavanaugh
847 // add the active page to the event data
848 m_direction
= direction
;
852 #endif // wxUSE_WIZARDDLG