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 // ----------------------------------------------------------------------------
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"
47 #include "wx/wizard.h"
49 // ----------------------------------------------------------------------------
51 // ----------------------------------------------------------------------------
53 class wxWizardSizer
: public wxSizer
56 wxWizardSizer(wxWizard
*owner
);
61 wxSize
GetMaxChildSize();
65 wxSize
SiblingSize(wxSizerItem
*child
);
68 bool m_childSizeValid
;
72 // ----------------------------------------------------------------------------
73 // event tables and such
74 // ----------------------------------------------------------------------------
76 DEFINE_EVENT_TYPE(wxEVT_WIZARD_PAGE_CHANGED
)
77 DEFINE_EVENT_TYPE(wxEVT_WIZARD_PAGE_CHANGING
)
78 DEFINE_EVENT_TYPE(wxEVT_WIZARD_CANCEL
)
79 DEFINE_EVENT_TYPE(wxEVT_WIZARD_FINISHED
)
80 DEFINE_EVENT_TYPE(wxEVT_WIZARD_HELP
)
82 BEGIN_EVENT_TABLE(wxWizard
, wxDialog
)
83 EVT_BUTTON(wxID_CANCEL
, wxWizard::OnCancel
)
84 EVT_BUTTON(wxID_BACKWARD
, wxWizard::OnBackOrNext
)
85 EVT_BUTTON(wxID_FORWARD
, wxWizard::OnBackOrNext
)
86 EVT_BUTTON(wxID_HELP
, wxWizard::OnHelp
)
88 EVT_WIZARD_PAGE_CHANGED(-1, wxWizard::OnWizEvent
)
89 EVT_WIZARD_PAGE_CHANGING(-1, wxWizard::OnWizEvent
)
90 EVT_WIZARD_CANCEL(-1, wxWizard::OnWizEvent
)
91 EVT_WIZARD_FINISHED(-1, wxWizard::OnWizEvent
)
92 EVT_WIZARD_HELP(-1, wxWizard::OnWizEvent
)
95 IMPLEMENT_DYNAMIC_CLASS(wxWizard
, wxDialog
)
96 IMPLEMENT_ABSTRACT_CLASS(wxWizardPage
, wxPanel
)
97 IMPLEMENT_DYNAMIC_CLASS(wxWizardPageSimple
, wxWizardPage
)
98 IMPLEMENT_DYNAMIC_CLASS(wxWizardEvent
, wxNotifyEvent
)
100 // ============================================================================
102 // ============================================================================
104 // ----------------------------------------------------------------------------
106 // ----------------------------------------------------------------------------
108 void wxWizardPage::Init()
110 m_bitmap
= wxNullBitmap
;
113 wxWizardPage::wxWizardPage(wxWizard
*parent
,
114 const wxBitmap
& bitmap
,
115 const wxChar
*resource
)
117 Create(parent
, bitmap
, resource
);
120 bool wxWizardPage::Create(wxWizard
*parent
,
121 const wxBitmap
& bitmap
,
122 const wxChar
*resource
)
124 if ( !wxPanel::Create(parent
, -1) )
127 if ( resource
!= NULL
)
129 #if wxUSE_WX_RESOURCES
131 if ( !LoadFromResource(this, resource
) )
133 wxFAIL_MSG(wxT("wxWizardPage LoadFromResource failed!!!!"));
136 #endif // wxUSE_RESOURCES
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
)
168 m_childSizeValid
= false;
171 void wxWizardSizer::RecalcSizes()
173 // Effect of this function depends on m_owner->m_page and
174 // it should be called whenever it changes (wxWizard::ShowPage)
175 if ( m_owner
->m_page
)
177 m_owner
->m_page
->SetSize(m_position
.x
,m_position
.y
, m_size
.x
,m_size
.y
);
181 wxSize
wxWizardSizer::CalcMin()
183 return m_owner
->GetPageSize();
186 wxSize
wxWizardSizer::GetMaxChildSize()
188 #if !defined(__WXDEBUG__)
189 if ( m_childSizeValid
)
194 wxSizerItemList::Node
*childNode
;
196 for(childNode
= m_children
.GetFirst(); childNode
;
197 childNode
= childNode
->GetNext())
199 wxSizerItem
*child
= childNode
->GetData();
200 maxOfMin
.IncTo(child
->CalcMin());
201 maxOfMin
.IncTo(SiblingSize(child
));
205 if ( m_childSizeValid
&& m_childSize
!= maxOfMin
)
207 wxFAIL_MSG( _T("Size changed in wxWizard::GetPageAreaSizer()")
208 _T("after RunWizard().\n")
209 _T("Did you forget to call GetSizer()->Fit(this) ")
210 _T("for some page?")) ;
214 #endif // __WXDEBUG__
216 if ( m_owner
->m_started
)
218 m_childSizeValid
= true;
219 m_childSize
= maxOfMin
;
225 int wxWizardSizer::Border() const
227 if ( m_owner
->m_calledSetBorder
)
228 return m_owner
->m_border
;
230 return m_children
.IsEmpty() ? 5 : 0;
233 wxSize
wxWizardSizer::SiblingSize(wxSizerItem
*child
)
237 if ( child
->IsWindow() )
239 wxWizardPage
*page
= wxDynamicCast(child
->GetWindow(), wxWizardPage
);
242 for ( wxWizardPage
*sibling
= page
->GetNext();
244 sibling
= sibling
->GetNext() )
246 if ( sibling
->GetSizer() )
248 maxSibling
.IncTo(sibling
->GetSizer()->CalcMin());
257 // ----------------------------------------------------------------------------
258 // generic wxWizard implementation
259 // ----------------------------------------------------------------------------
261 void wxWizard::Init()
263 m_posWizard
= wxDefaultPosition
;
264 m_page
= (wxWizardPage
*)NULL
;
265 m_btnPrev
= m_btnNext
= NULL
;
267 m_sizerBmpAndPage
= NULL
;
269 m_calledSetBorder
= false;
274 bool wxWizard::Create(wxWindow
*parent
,
276 const wxString
& title
,
277 const wxBitmap
& bitmap
,
281 bool result
= wxDialog::Create(parent
,id
,title
,pos
,wxDefaultSize
,style
);
291 void wxWizard::AddBitmapRow(wxBoxSizer
*mainColumn
)
293 m_sizerBmpAndPage
= new wxBoxSizer(wxHORIZONTAL
);
296 1, // Vertically stretchable
297 wxEXPAND
// Horizonal stretching, no border
300 0, // No vertical stretching
301 wxEXPAND
// No border, (mostly useless) horizontal stretching
306 m_statbmp
= new wxStaticBitmap(this, -1, m_bitmap
);
307 m_sizerBmpAndPage
->Add(
309 0, // No horizontal stretching
310 wxALL
, // Border all around, top alignment
313 m_sizerBmpAndPage
->Add(
315 0, // No horizontal stretching
316 wxEXPAND
// No border, (mostly useless) vertical stretching
320 // Added to m_sizerBmpAndPage in FinishLayout
321 m_sizerPage
= new wxWizardSizer(this);
324 void wxWizard::AddStaticLine(wxBoxSizer
*mainColumn
)
328 new wxStaticLine(this, -1),
329 0, // Vertically unstretchable
330 wxEXPAND
| wxALL
, // Border all around, horizontally stretchable
334 0, // No vertical stretching
335 wxEXPAND
// No border, (mostly useless) horizontal stretching
339 #endif // wxUSE_STATLINE
342 void wxWizard::AddBackNextPair(wxBoxSizer
*buttonRow
)
344 // margin between Back and Next buttons
346 static const int BACKNEXT_MARGIN
= 10;
348 static const int BACKNEXT_MARGIN
= 0;
351 wxBoxSizer
*backNextPair
= new wxBoxSizer(wxHORIZONTAL
);
354 0, // No horizontal stretching
355 wxALL
, // Border all around
359 m_btnPrev
= new wxButton(this, wxID_BACKWARD
, _("< &Back"));
360 backNextPair
->Add(m_btnPrev
);
361 backNextPair
->Add(BACKNEXT_MARGIN
,0,
362 0, // No horizontal stretching
363 wxEXPAND
// No border, (mostly useless) vertical stretching
365 m_btnNext
= new wxButton(this, wxID_FORWARD
, _("&Next >"));
366 backNextPair
->Add(m_btnNext
);
369 void wxWizard::AddButtonRow(wxBoxSizer
*mainColumn
)
371 wxBoxSizer
*buttonRow
= new wxBoxSizer(wxHORIZONTAL
);
374 0, // Vertically unstretchable
375 wxALIGN_RIGHT
// Right aligned, no border
378 if (GetExtraStyle() & wxWIZARD_EX_HELPBUTTON
)
380 new wxButton(this, wxID_HELP
, _("&Help")),
381 0, // Horizontally unstretchable
382 wxALL
, // Border all around, top aligned
386 AddBackNextPair(buttonRow
);
389 new wxButton(this, wxID_CANCEL
, _("&Cancel")),
390 0, // Horizontally unstretchable
391 wxALL
, // Border all around, top aligned
396 void wxWizard::DoCreateControls()
398 // do nothing if the controls were already created
402 // wxWindow::SetSizer will be called at end
403 wxBoxSizer
*windowSizer
= new wxBoxSizer(wxVERTICAL
);
405 wxBoxSizer
*mainColumn
= new wxBoxSizer(wxVERTICAL
);
408 1, // Vertical stretching
409 wxALL
| wxEXPAND
, // Border all around, horizontal stretching
413 AddBitmapRow(mainColumn
);
414 AddStaticLine(mainColumn
);
415 AddButtonRow(mainColumn
);
417 // wxWindow::SetSizer should be followed by wxWindow::Fit, but
418 // this is done in FinishLayout anyway so why duplicate it
419 SetSizer(windowSizer
);
422 void wxWizard::SetPageSize(const wxSize
& size
)
424 wxCHECK_RET(!m_started
,wxT("wxWizard::SetPageSize after RunWizard"));
428 void wxWizard::FinishLayout()
430 m_sizerBmpAndPage
->Add(
432 1, // Horizontal stretching
433 wxEXPAND
| wxALL
, // Vertically stretchable
434 m_sizerPage
->Border()
437 GetSizer()->SetSizeHints(this);
438 if ( m_posWizard
== wxDefaultPosition
)
442 void wxWizard::FitToPage(const wxWizardPage
*page
)
444 wxCHECK_RET(!m_started
,wxT("wxWizard::FitToPage after RunWizard"));
448 wxSize size
= page
->GetBestSize();
450 m_sizePage
.IncTo(size
);
452 page
= page
->GetNext();
456 bool wxWizard::ShowPage(wxWizardPage
*page
, bool goingForward
)
458 wxASSERT_MSG( page
!= m_page
, wxT("this is useless") );
460 // we'll use this to decide whether we have to change the label of this
461 // button or not (initially the label is "Next")
462 bool btnLabelWasNext
= TRUE
;
464 // Modified 10-20-2001 Robert Cavanaugh.
465 // Fixed bug for displaying a new bitmap
466 // in each *consecutive* page
468 // flag to indicate if this page uses a new bitmap
469 bool bmpIsDefault
= TRUE
;
471 // use these labels to determine if we need to change the bitmap
473 wxBitmap bmpPrev
, bmpCur
;
475 // check for previous page
478 // send the event to the old page
479 wxWizardEvent
event(wxEVT_WIZARD_PAGE_CHANGING
, GetId(), goingForward
, m_page
);
480 if ( m_page
->GetEventHandler()->ProcessEvent(event
) &&
483 // vetoed by the page
489 btnLabelWasNext
= HasNextPage(m_page
);
491 // Get the bitmap of the previous page (if it exists)
492 if ( m_page
->GetBitmap().Ok() )
494 bmpPrev
= m_page
->GetBitmap();
504 // terminate successfully
508 wxWizardEvent
event(wxEVT_WIZARD_FINISHED
, GetId(),FALSE
, 0);
509 (void)GetEventHandler()->ProcessEvent(event
);
514 // position and show the new page
515 (void)m_page
->TransferDataToWindow();
517 // wxWizardSizer::RecalcSizes wants to be called when m_page changes
518 m_sizerPage
->RecalcSizes();
520 // check if bitmap needs to be updated
521 // update default flag as well
522 if ( m_page
->GetBitmap().Ok() )
524 bmpCur
= m_page
->GetBitmap();
525 bmpIsDefault
= FALSE
;
528 // change the bitmap if:
529 // 1) a default bitmap was selected in constructor
530 // 2) this page was constructed with a bitmap
531 // 3) this bitmap is not the previous bitmap
532 if ( m_statbmp
&& (bmpCur
!= bmpPrev
) )
538 bmp
= m_page
->GetBitmap();
539 m_statbmp
->SetBitmap(bmp
);
542 // and update the buttons state
543 m_btnPrev
->Enable(HasPrevPage(m_page
));
545 bool hasNext
= HasNextPage(m_page
);
546 if ( btnLabelWasNext
!= hasNext
)
550 m_btnNext
->SetLabel(_("&Finish"));
552 m_btnNext
->SetLabel(_("&Next >"));
554 // nothing to do: the label was already correct
556 // send the change event to the new page now
557 wxWizardEvent
event(wxEVT_WIZARD_PAGE_CHANGED
, GetId(), goingForward
, m_page
);
558 (void)m_page
->GetEventHandler()->ProcessEvent(event
);
560 // and finally show it
567 bool wxWizard::RunWizard(wxWizardPage
*firstPage
)
569 wxCHECK_MSG( firstPage
, FALSE
, wxT("can't run empty wizard") );
571 // Set before FinishLayout to enable wxWizardSizer::GetMaxChildSize
574 // This cannot be done sooner, because user can change layout options
578 // can't return FALSE here because there is no old page
579 (void)ShowPage(firstPage
, TRUE
/* forward */);
581 return ShowModal() == wxID_OK
;
584 wxWizardPage
*wxWizard::GetCurrentPage() const
589 wxSize
wxWizard::GetPageSize() const
591 wxSize
pageSize(GetManualPageSize());
592 pageSize
.IncTo(m_sizerPage
->GetMaxChildSize());
596 wxSizer
*wxWizard::GetPageAreaSizer() const
601 void wxWizard::SetBorder(int border
)
603 wxCHECK_RET(!m_started
,wxT("wxWizard::SetBorder after RunWizard"));
605 m_calledSetBorder
= true;
609 wxSize
wxWizard::GetManualPageSize() const
611 // default width and height of the page
612 static const int DEFAULT_PAGE_WIDTH
= 270;
613 static const int DEFAULT_PAGE_HEIGHT
= 290;
615 wxSize
totalPageSize(DEFAULT_PAGE_WIDTH
,DEFAULT_PAGE_HEIGHT
);
617 totalPageSize
.IncTo(m_sizePage
);
621 totalPageSize
.IncTo(wxSize(0, m_bitmap
.GetHeight()));
624 return totalPageSize
;
627 void wxWizard::OnCancel(wxCommandEvent
& WXUNUSED(eventUnused
))
629 // this function probably can never be called when we don't have an active
630 // page, but a small extra check won't hurt
631 wxWindow
*win
= m_page
? (wxWindow
*)m_page
: (wxWindow
*)this;
633 wxWizardEvent
event(wxEVT_WIZARD_CANCEL
, GetId(), FALSE
, m_page
);
634 if ( !win
->GetEventHandler()->ProcessEvent(event
) || event
.IsAllowed() )
636 // no objections - close the dialog
637 EndModal(wxID_CANCEL
);
639 //else: request to Cancel ignored
642 void wxWizard::OnBackOrNext(wxCommandEvent
& event
)
644 wxASSERT_MSG( (event
.GetEventObject() == m_btnNext
) ||
645 (event
.GetEventObject() == m_btnPrev
),
646 wxT("unknown button") );
648 // ask the current page first: notice that we do it before calling
649 // GetNext/Prev() because the data transfered from the controls of the page
650 // may change the value returned by these methods
651 if ( m_page
&& (!m_page
->Validate() || !m_page
->TransferDataFromWindow()) )
653 // the page data is incorrect, don't do anything
657 bool forward
= event
.GetEventObject() == m_btnNext
;
662 page
= m_page
->GetNext();
666 page
= m_page
->GetPrev();
668 wxASSERT_MSG( page
, wxT("\"<Back\" button should have been disabled") );
671 // just pass to the new page (or may be not - but we don't care here)
672 (void)ShowPage(page
, forward
);
675 void wxWizard::OnHelp(wxCommandEvent
& WXUNUSED(event
))
677 // this function probably can never be called when we don't have an active
678 // page, but a small extra check won't hurt
681 // Create and send the help event to the specific page handler
682 // event data contains the active page so that context-sensitive
684 wxWizardEvent
eventHelp(wxEVT_WIZARD_HELP
, GetId(), TRUE
, m_page
);
685 (void)m_page
->GetEventHandler()->ProcessEvent(eventHelp
);
689 void wxWizard::OnWizEvent(wxWizardEvent
& event
)
691 // the dialogs have wxWS_EX_BLOCK_EVENTS style on by default but we want to
692 // propagate wxEVT_WIZARD_XXX to the parent (if any), so do it manually
693 if ( !(GetExtraStyle() & wxWS_EX_BLOCK_EVENTS
) )
695 // the event will be propagated anyhow
699 wxWindow
*parent
= GetParent();
701 if ( !parent
|| !parent
->GetEventHandler()->ProcessEvent(event
) )
707 // ----------------------------------------------------------------------------
708 // our public interface
709 // ----------------------------------------------------------------------------
711 #ifdef WXWIN_COMPATIBILITY_2_2
714 wxWizard
*wxWizardBase::Create(wxWindow
*parent
,
716 const wxString
& title
,
717 const wxBitmap
& bitmap
,
719 const wxSize
& WXUNUSED(size
))
721 return new wxWizard(parent
, id
, title
, bitmap
, pos
);
724 #endif // WXWIN_COMPATIBILITY_2_2
726 // ----------------------------------------------------------------------------
728 // ----------------------------------------------------------------------------
730 wxWizardEvent::wxWizardEvent(wxEventType type
, int id
, bool direction
, wxWizardPage
* page
)
731 : wxNotifyEvent(type
, id
)
733 // Modified 10-20-2001 Robert Cavanaugh
734 // add the active page to the event data
735 m_direction
= direction
;
739 #endif // wxUSE_WIZARDDLG