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"
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
)
104 IMPLEMENT_ABSTRACT_CLASS(wxWizardPage
, wxPanel
)
105 IMPLEMENT_DYNAMIC_CLASS(wxWizardPageSimple
, wxWizardPage
)
106 IMPLEMENT_DYNAMIC_CLASS(wxWizardEvent
, wxNotifyEvent
)
108 // ============================================================================
110 // ============================================================================
112 // ----------------------------------------------------------------------------
114 // ----------------------------------------------------------------------------
116 void wxWizardPage::Init()
118 m_bitmap
= wxNullBitmap
;
121 wxWizardPage::wxWizardPage(wxWizard
*parent
,
122 const wxBitmap
& bitmap
,
123 const wxChar
*resource
)
125 Create(parent
, bitmap
, resource
);
128 bool wxWizardPage::Create(wxWizard
*parent
,
129 const wxBitmap
& bitmap
,
130 const wxChar
*resource
)
132 if ( !wxPanel::Create(parent
, -1) )
135 if ( resource
!= NULL
)
137 #if wxUSE_WX_RESOURCES
139 if ( !LoadFromResource(this, resource
) )
141 wxFAIL_MSG(wxT("wxWizardPage LoadFromResource failed!!!!"));
144 #endif // wxUSE_RESOURCES
149 // initially the page is hidden, it's shown only when it becomes current
155 // ----------------------------------------------------------------------------
156 // wxWizardPageSimple
157 // ----------------------------------------------------------------------------
159 wxWizardPage
*wxWizardPageSimple::GetPrev() const
164 wxWizardPage
*wxWizardPageSimple::GetNext() const
169 // ----------------------------------------------------------------------------
171 // ----------------------------------------------------------------------------
173 wxWizardSizer::wxWizardSizer(wxWizard
*owner
)
176 m_childSizeValid
= false;
179 void wxWizardSizer::RecalcSizes()
181 // Effect of this function depends on m_owner->m_page and
182 // it should be called whenever it changes (wxWizard::ShowPage)
183 if ( m_owner
->m_page
)
185 m_owner
->m_page
->SetSize(m_position
.x
,m_position
.y
, m_size
.x
,m_size
.y
);
189 wxSize
wxWizardSizer::CalcMin()
191 return m_owner
->GetPageSize();
194 wxSize
wxWizardSizer::GetMaxChildSize()
196 #if !defined(__WXDEBUG__)
197 if ( m_childSizeValid
)
202 wxSizerItemList::compatibility_iterator childNode
;
204 for(childNode
= m_children
.GetFirst(); childNode
;
205 childNode
= childNode
->GetNext())
207 wxSizerItem
*child
= childNode
->GetData();
208 maxOfMin
.IncTo(child
->CalcMin());
209 maxOfMin
.IncTo(SiblingSize(child
));
213 if ( m_childSizeValid
&& m_childSize
!= maxOfMin
)
215 wxFAIL_MSG( _T("Size changed in wxWizard::GetPageAreaSizer()")
216 _T("after RunWizard().\n")
217 _T("Did you forget to call GetSizer()->Fit(this) ")
218 _T("for some page?")) ;
222 #endif // __WXDEBUG__
224 if ( m_owner
->m_started
)
226 m_childSizeValid
= true;
227 m_childSize
= maxOfMin
;
233 int wxWizardSizer::Border() const
235 if ( m_owner
->m_calledSetBorder
)
236 return m_owner
->m_border
;
238 return m_children
.IsEmpty() ? 5 : 0;
241 wxSize
wxWizardSizer::SiblingSize(wxSizerItem
*child
)
245 if ( child
->IsWindow() )
247 wxWizardPage
*page
= wxDynamicCast(child
->GetWindow(), wxWizardPage
);
250 for ( wxWizardPage
*sibling
= page
->GetNext();
252 sibling
= sibling
->GetNext() )
254 if ( sibling
->GetSizer() )
256 maxSibling
.IncTo(sibling
->GetSizer()->CalcMin());
265 // ----------------------------------------------------------------------------
266 // generic wxWizard implementation
267 // ----------------------------------------------------------------------------
269 void wxWizard::Init()
271 m_posWizard
= wxDefaultPosition
;
272 m_page
= (wxWizardPage
*)NULL
;
273 m_btnPrev
= m_btnNext
= NULL
;
275 m_sizerBmpAndPage
= NULL
;
277 m_calledSetBorder
= false;
282 bool wxWizard::Create(wxWindow
*parent
,
284 const wxString
& title
,
285 const wxBitmap
& bitmap
,
289 bool result
= wxDialog::Create(parent
,id
,title
,pos
,wxDefaultSize
,style
);
299 void wxWizard::AddBitmapRow(wxBoxSizer
*mainColumn
)
301 m_sizerBmpAndPage
= new wxBoxSizer(wxHORIZONTAL
);
304 1, // Vertically stretchable
305 wxEXPAND
// Horizonal stretching, no border
308 0, // No vertical stretching
309 wxEXPAND
// No border, (mostly useless) horizontal stretching
314 m_statbmp
= new wxStaticBitmap(this, -1, m_bitmap
);
315 m_sizerBmpAndPage
->Add(
317 0, // No horizontal stretching
318 wxALL
, // Border all around, top alignment
321 m_sizerBmpAndPage
->Add(
323 0, // No horizontal stretching
324 wxEXPAND
// No border, (mostly useless) vertical stretching
328 // Added to m_sizerBmpAndPage in FinishLayout
329 m_sizerPage
= new wxWizardSizer(this);
332 void wxWizard::AddStaticLine(wxBoxSizer
*mainColumn
)
336 new wxStaticLine(this, -1),
337 0, // Vertically unstretchable
338 wxEXPAND
| wxALL
, // Border all around, horizontally stretchable
342 0, // No vertical stretching
343 wxEXPAND
// No border, (mostly useless) horizontal stretching
347 #endif // wxUSE_STATLINE
350 void wxWizard::AddBackNextPair(wxBoxSizer
*buttonRow
)
352 // margin between Back and Next buttons
354 static const int BACKNEXT_MARGIN
= 10;
356 static const int BACKNEXT_MARGIN
= 0;
359 wxBoxSizer
*backNextPair
= new wxBoxSizer(wxHORIZONTAL
);
362 0, // No horizontal stretching
363 wxALL
, // Border all around
367 m_btnPrev
= new wxButton(this, wxID_BACKWARD
, _("< &Back"));
368 backNextPair
->Add(m_btnPrev
);
369 backNextPair
->Add(BACKNEXT_MARGIN
,0,
370 0, // No horizontal stretching
371 wxEXPAND
// No border, (mostly useless) vertical stretching
373 m_btnNext
= new wxButton(this, wxID_FORWARD
, _("&Next >"));
374 backNextPair
->Add(m_btnNext
);
377 void wxWizard::AddButtonRow(wxBoxSizer
*mainColumn
)
379 wxBoxSizer
*buttonRow
= new wxBoxSizer(wxHORIZONTAL
);
382 0, // Vertically unstretchable
383 wxALIGN_RIGHT
// Right aligned, no border
386 if (GetExtraStyle() & wxWIZARD_EX_HELPBUTTON
)
388 new wxButton(this, wxID_HELP
, _("&Help")),
389 0, // Horizontally unstretchable
390 wxALL
, // Border all around, top aligned
394 AddBackNextPair(buttonRow
);
397 new wxButton(this, wxID_CANCEL
, _("&Cancel")),
398 0, // Horizontally unstretchable
399 wxALL
, // Border all around, top aligned
404 void wxWizard::DoCreateControls()
406 // do nothing if the controls were already created
410 // wxWindow::SetSizer will be called at end
411 wxBoxSizer
*windowSizer
= new wxBoxSizer(wxVERTICAL
);
413 wxBoxSizer
*mainColumn
= new wxBoxSizer(wxVERTICAL
);
416 1, // Vertical stretching
417 wxALL
| wxEXPAND
, // Border all around, horizontal stretching
421 AddBitmapRow(mainColumn
);
422 AddStaticLine(mainColumn
);
423 AddButtonRow(mainColumn
);
425 // wxWindow::SetSizer should be followed by wxWindow::Fit, but
426 // this is done in FinishLayout anyway so why duplicate it
427 SetSizer(windowSizer
);
430 void wxWizard::SetPageSize(const wxSize
& size
)
432 wxCHECK_RET(!m_started
,wxT("wxWizard::SetPageSize after RunWizard"));
436 void wxWizard::FinishLayout()
438 m_sizerBmpAndPage
->Add(
440 1, // Horizontal stretching
441 wxEXPAND
| wxALL
, // Vertically stretchable
442 m_sizerPage
->Border()
445 GetSizer()->SetSizeHints(this);
446 if ( m_posWizard
== wxDefaultPosition
)
450 void wxWizard::FitToPage(const wxWizardPage
*page
)
452 wxCHECK_RET(!m_started
,wxT("wxWizard::FitToPage after RunWizard"));
456 wxSize size
= page
->GetBestSize();
458 m_sizePage
.IncTo(size
);
460 page
= page
->GetNext();
464 bool wxWizard::ShowPage(wxWizardPage
*page
, bool goingForward
)
466 wxASSERT_MSG( page
!= m_page
, wxT("this is useless") );
468 // we'll use this to decide whether we have to change the label of this
469 // button or not (initially the label is "Next")
470 bool btnLabelWasNext
= TRUE
;
472 // Modified 10-20-2001 Robert Cavanaugh.
473 // Fixed bug for displaying a new bitmap
474 // in each *consecutive* page
476 // flag to indicate if this page uses a new bitmap
477 bool bmpIsDefault
= TRUE
;
479 // use these labels to determine if we need to change the bitmap
481 wxBitmap bmpPrev
, bmpCur
;
483 // check for previous page
486 // send the event to the old page
487 wxWizardEvent
event(wxEVT_WIZARD_PAGE_CHANGING
, GetId(), goingForward
, m_page
);
488 if ( m_page
->GetEventHandler()->ProcessEvent(event
) &&
491 // vetoed by the page
497 btnLabelWasNext
= HasNextPage(m_page
);
499 // Get the bitmap of the previous page (if it exists)
500 if ( m_page
->GetBitmap().Ok() )
502 bmpPrev
= m_page
->GetBitmap();
512 // terminate successfully
516 wxWizardEvent
event(wxEVT_WIZARD_FINISHED
, GetId(),FALSE
, 0);
517 (void)GetEventHandler()->ProcessEvent(event
);
522 // position and show the new page
523 (void)m_page
->TransferDataToWindow();
525 // wxWizardSizer::RecalcSizes wants to be called when m_page changes
526 m_sizerPage
->RecalcSizes();
528 // check if bitmap needs to be updated
529 // update default flag as well
530 if ( m_page
->GetBitmap().Ok() )
532 bmpCur
= m_page
->GetBitmap();
533 bmpIsDefault
= FALSE
;
536 // change the bitmap if:
537 // 1) a default bitmap was selected in constructor
538 // 2) this page was constructed with a bitmap
539 // 3) this bitmap is not the previous bitmap
540 if ( m_statbmp
&& (bmpCur
!= bmpPrev
) )
546 bmp
= m_page
->GetBitmap();
547 m_statbmp
->SetBitmap(bmp
);
550 // and update the buttons state
551 m_btnPrev
->Enable(HasPrevPage(m_page
));
553 bool hasNext
= HasNextPage(m_page
);
554 if ( btnLabelWasNext
!= hasNext
)
558 m_btnNext
->SetLabel(_("&Finish"));
560 m_btnNext
->SetLabel(_("&Next >"));
562 // nothing to do: the label was already correct
564 // send the change event to the new page now
565 wxWizardEvent
event(wxEVT_WIZARD_PAGE_CHANGED
, GetId(), goingForward
, m_page
);
566 (void)m_page
->GetEventHandler()->ProcessEvent(event
);
568 // and finally show it
575 bool wxWizard::RunWizard(wxWizardPage
*firstPage
)
577 wxCHECK_MSG( firstPage
, FALSE
, wxT("can't run empty wizard") );
579 // Set before FinishLayout to enable wxWizardSizer::GetMaxChildSize
582 // This cannot be done sooner, because user can change layout options
586 // can't return FALSE here because there is no old page
587 (void)ShowPage(firstPage
, TRUE
/* forward */);
589 return ShowModal() == wxID_OK
;
592 wxWizardPage
*wxWizard::GetCurrentPage() const
597 wxSize
wxWizard::GetPageSize() const
599 wxSize
pageSize(GetManualPageSize());
600 pageSize
.IncTo(m_sizerPage
->GetMaxChildSize());
604 wxSizer
*wxWizard::GetPageAreaSizer() const
609 void wxWizard::SetBorder(int border
)
611 wxCHECK_RET(!m_started
,wxT("wxWizard::SetBorder after RunWizard"));
613 m_calledSetBorder
= true;
617 wxSize
wxWizard::GetManualPageSize() const
619 // default width and height of the page
620 static const int DEFAULT_PAGE_WIDTH
= 270;
621 static const int DEFAULT_PAGE_HEIGHT
= 290;
623 wxSize
totalPageSize(DEFAULT_PAGE_WIDTH
,DEFAULT_PAGE_HEIGHT
);
625 totalPageSize
.IncTo(m_sizePage
);
629 totalPageSize
.IncTo(wxSize(0, m_bitmap
.GetHeight()));
632 return totalPageSize
;
635 void wxWizard::OnCancel(wxCommandEvent
& WXUNUSED(eventUnused
))
637 // this function probably can never be called when we don't have an active
638 // page, but a small extra check won't hurt
639 wxWindow
*win
= m_page
? (wxWindow
*)m_page
: (wxWindow
*)this;
641 wxWizardEvent
event(wxEVT_WIZARD_CANCEL
, GetId(), FALSE
, m_page
);
642 if ( !win
->GetEventHandler()->ProcessEvent(event
) || event
.IsAllowed() )
644 // no objections - close the dialog
645 EndModal(wxID_CANCEL
);
647 //else: request to Cancel ignored
650 void wxWizard::OnBackOrNext(wxCommandEvent
& event
)
652 wxASSERT_MSG( (event
.GetEventObject() == m_btnNext
) ||
653 (event
.GetEventObject() == m_btnPrev
),
654 wxT("unknown button") );
656 // ask the current page first: notice that we do it before calling
657 // GetNext/Prev() because the data transfered from the controls of the page
658 // may change the value returned by these methods
659 if ( m_page
&& (!m_page
->Validate() || !m_page
->TransferDataFromWindow()) )
661 // the page data is incorrect, don't do anything
665 bool forward
= event
.GetEventObject() == m_btnNext
;
670 page
= m_page
->GetNext();
674 page
= m_page
->GetPrev();
676 wxASSERT_MSG( page
, wxT("\"<Back\" button should have been disabled") );
679 // just pass to the new page (or may be not - but we don't care here)
680 (void)ShowPage(page
, forward
);
683 void wxWizard::OnHelp(wxCommandEvent
& WXUNUSED(event
))
685 // this function probably can never be called when we don't have an active
686 // page, but a small extra check won't hurt
689 // Create and send the help event to the specific page handler
690 // event data contains the active page so that context-sensitive
692 wxWizardEvent
eventHelp(wxEVT_WIZARD_HELP
, GetId(), TRUE
, m_page
);
693 (void)m_page
->GetEventHandler()->ProcessEvent(eventHelp
);
697 void wxWizard::OnWizEvent(wxWizardEvent
& event
)
699 // the dialogs have wxWS_EX_BLOCK_EVENTS style on by default but we want to
700 // propagate wxEVT_WIZARD_XXX to the parent (if any), so do it manually
701 if ( !(GetExtraStyle() & wxWS_EX_BLOCK_EVENTS
) )
703 // the event will be propagated anyhow
707 wxWindow
*parent
= GetParent();
709 if ( !parent
|| !parent
->GetEventHandler()->ProcessEvent(event
) )
715 // ----------------------------------------------------------------------------
716 // our public interface
717 // ----------------------------------------------------------------------------
719 #ifdef WXWIN_COMPATIBILITY_2_2
722 wxWizard
*wxWizardBase::Create(wxWindow
*parent
,
724 const wxString
& title
,
725 const wxBitmap
& bitmap
,
727 const wxSize
& WXUNUSED(size
))
729 return new wxWizard(parent
, id
, title
, bitmap
, pos
);
732 #endif // WXWIN_COMPATIBILITY_2_2
734 // ----------------------------------------------------------------------------
736 // ----------------------------------------------------------------------------
738 wxWizardEvent::wxWizardEvent(wxEventType type
, int id
, bool direction
, wxWizardPage
* page
)
739 : wxNotifyEvent(type
, id
)
741 // Modified 10-20-2001 Robert Cavanaugh
742 // add the active page to the event data
743 m_direction
= direction
;
747 #endif // wxUSE_WIZARDDLG