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
11 // Copyright: (c) 1999 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
12 // Licence: wxWindows license
13 ///////////////////////////////////////////////////////////////////////////////
15 // ============================================================================
17 // ============================================================================
19 // ----------------------------------------------------------------------------
21 // ----------------------------------------------------------------------------
24 #pragma implementation "wizardg.h"
27 // For compilers that support precompilation, includes "wx.h".
28 #include "wx/wxprec.h"
37 #include "wx/dynarray.h"
39 #include "wx/statbmp.h"
40 #include "wx/button.h"
43 #include "wx/statline.h"
45 #include "wx/wizard.h"
47 // ----------------------------------------------------------------------------
49 // ----------------------------------------------------------------------------
51 WX_DEFINE_ARRAY(wxPanel
*, wxArrayPages
);
53 // ----------------------------------------------------------------------------
54 // event tables and such
55 // ----------------------------------------------------------------------------
57 DEFINE_EVENT_TYPE(wxEVT_WIZARD_PAGE_CHANGED
)
58 DEFINE_EVENT_TYPE(wxEVT_WIZARD_PAGE_CHANGING
)
59 DEFINE_EVENT_TYPE(wxEVT_WIZARD_CANCEL
)
60 DEFINE_EVENT_TYPE(wxEVT_WIZARD_FINISHED
)
61 DEFINE_EVENT_TYPE(wxEVT_WIZARD_HELP
)
63 BEGIN_EVENT_TABLE(wxWizard
, wxDialog
)
64 EVT_BUTTON(wxID_CANCEL
, wxWizard::OnCancel
)
65 EVT_BUTTON(wxID_BACKWARD
, wxWizard::OnBackOrNext
)
66 EVT_BUTTON(wxID_FORWARD
, wxWizard::OnBackOrNext
)
67 EVT_BUTTON(wxID_HELP
, wxWizard::OnHelp
)
69 EVT_WIZARD_PAGE_CHANGED(-1, wxWizard::OnWizEvent
)
70 EVT_WIZARD_PAGE_CHANGING(-1, wxWizard::OnWizEvent
)
71 EVT_WIZARD_CANCEL(-1, wxWizard::OnWizEvent
)
72 EVT_WIZARD_FINISHED(-1, wxWizard::OnWizEvent
)
73 EVT_WIZARD_HELP(-1, wxWizard::OnWizEvent
)
76 IMPLEMENT_DYNAMIC_CLASS(wxWizard
, wxDialog
)
77 IMPLEMENT_ABSTRACT_CLASS(wxWizardPage
, wxPanel
)
78 IMPLEMENT_DYNAMIC_CLASS(wxWizardPageSimple
, wxWizardPage
)
79 IMPLEMENT_DYNAMIC_CLASS(wxWizardEvent
, wxNotifyEvent
)
81 // ============================================================================
83 // ============================================================================
85 // ----------------------------------------------------------------------------
87 // ----------------------------------------------------------------------------
89 void wxWizardPage::Init()
91 m_bitmap
= wxNullBitmap
;
94 wxWizardPage::wxWizardPage(wxWizard
*parent
,
95 const wxBitmap
& bitmap
,
96 const wxChar
*resource
)
98 Create(parent
, bitmap
, resource
);
101 bool wxWizardPage::Create(wxWizard
*parent
,
102 const wxBitmap
& bitmap
,
103 const wxChar
*resource
)
105 if ( !wxPanel::Create(parent
, -1) )
108 if ( resource
!= NULL
)
110 #if wxUSE_WX_RESOURCES
111 if ( !LoadFromResource(this, resource
) )
113 wxFAIL_MSG(wxT("wxWizardPage LoadFromResource failed!!!!"));
115 #endif // wxUSE_RESOURCES
120 // initially the page is hidden, it's shown only when it becomes current
126 // ----------------------------------------------------------------------------
127 // wxWizardPageSimple
128 // ----------------------------------------------------------------------------
130 wxWizardPage
*wxWizardPageSimple::GetPrev() const
135 wxWizardPage
*wxWizardPageSimple::GetNext() const
139 // ----------------------------------------------------------------------------
140 // generic wxWizard implementation
141 // ----------------------------------------------------------------------------
143 void wxWizard::Init()
145 m_posWizard
= wxDefaultPosition
;
146 m_page
= (wxWizardPage
*)NULL
;
147 m_btnPrev
= m_btnNext
= NULL
;
151 bool wxWizard::Create(wxWindow
*parent
,
153 const wxString
& title
,
154 const wxBitmap
& bitmap
,
160 // just create the dialog itself here, the controls will be created in
161 // DoCreateControls() called later when we know our final size
162 m_page
= (wxWizardPage
*)NULL
;
163 m_btnPrev
= m_btnNext
= NULL
;
166 return wxDialog::Create(parent
, id
, title
, pos
);
169 void wxWizard::DoCreateControls()
171 // do nothing if the controls were already created
175 // constants defining the dialog layout
176 // ------------------------------------
178 // these constants define the position of the upper left corner of the
179 // bitmap or the page in the wizard
180 static const int X_MARGIN
= 10;
181 static const int Y_MARGIN
= 10;
183 // margin between the bitmap and the panel
184 static const int BITMAP_X_MARGIN
= 15;
186 // margin between the bitmap and the static line
187 static const int BITMAP_Y_MARGIN
= 15;
189 // margin between the static line and the buttons
190 static const int SEPARATOR_LINE_MARGIN
= 15;
192 // margin between "Next >" and "Cancel" buttons
193 static const int BUTTON_MARGIN
= 10;
195 // margin between Back and Next buttons
197 static const int BACKNEXT_MARGIN
= 10;
199 static const int BACKNEXT_MARGIN
= 0;
202 // default width and height of the page
203 static const int DEFAULT_PAGE_WIDTH
= 270;
204 static const int DEFAULT_PAGE_HEIGHT
= 290;
209 wxSize sizeBtn
= wxButton::GetDefaultSize();
211 // the global dialog layout is: a row of buttons at the bottom (aligned to
212 // the right), the static line above them, the bitmap (if any) on the left
213 // of the upper part of the dialog and the panel in the remaining space
220 m_statbmp
= new wxStaticBitmap(this, -1, m_bitmap
, wxPoint(m_x
, m_y
));
222 m_x
+= m_bitmap
.GetWidth() + BITMAP_X_MARGIN
;
224 defaultHeight
= m_bitmap
.GetHeight();
228 m_statbmp
= (wxStaticBitmap
*)NULL
;
230 defaultHeight
= DEFAULT_PAGE_HEIGHT
;
233 // use default size if none given and also make sure that the dialog is
234 // not less than the default size
235 m_height
= m_sizePage
.y
== -1 ? defaultHeight
: m_sizePage
.y
;
236 m_width
= m_sizePage
.x
== -1 ? DEFAULT_PAGE_WIDTH
: m_sizePage
.x
;
237 if ( m_height
< defaultHeight
)
238 m_height
= defaultHeight
;
239 if ( m_width
< DEFAULT_PAGE_WIDTH
)
240 m_width
= DEFAULT_PAGE_WIDTH
;
243 int y
= m_y
+ m_height
+ BITMAP_Y_MARGIN
;
246 (void)new wxStaticLine(this, -1, wxPoint(x
, y
),
247 wxSize(m_x
+ m_width
- x
, 2));
248 #endif // wxUSE_STATLINE
250 x
= m_x
+ m_width
- 3*sizeBtn
.x
- BUTTON_MARGIN
- BACKNEXT_MARGIN
;
251 y
+= SEPARATOR_LINE_MARGIN
;
253 if (GetExtraStyle() & wxWIZARD_EX_HELPBUTTON
)
258 (void)new wxButton(this, wxID_HELP
, _("&Help"), wxPoint(x
, y
), sizeBtn
);
263 m_btnPrev
= new wxButton(this, wxID_BACKWARD
, _("< &Back"), wxPoint(x
, y
), sizeBtn
);
266 x
+= BACKNEXT_MARGIN
;
268 m_btnNext
= new wxButton(this, wxID_FORWARD
, _("&Next >"), wxPoint(x
, y
), sizeBtn
);
270 x
+= sizeBtn
.x
+ BUTTON_MARGIN
;
271 (void)new wxButton(this, wxID_CANCEL
, _("&Cancel"), wxPoint(x
, y
), sizeBtn
);
273 // position and size the dialog
274 // ----------------------------
276 SetClientSize(m_x
+ m_width
+ X_MARGIN
,
277 m_y
+ m_height
+ BITMAP_Y_MARGIN
+
278 SEPARATOR_LINE_MARGIN
+ sizeBtn
.y
+ Y_MARGIN
);
280 if ( m_posWizard
== wxDefaultPosition
)
286 void wxWizard::SetPageSize(const wxSize
& size
)
288 // otherwise it will have no effect now as it's too late...
289 wxASSERT_MSG( !WasCreated(), _T("should be called before RunWizard()!") );
294 void wxWizard::FitToPage(const wxWizardPage
*page
)
296 // otherwise it will have no effect now as it's too late...
297 wxASSERT_MSG( !WasCreated(), _T("should be called before RunWizard()!") );
302 wxSize size
= page
->GetBestSize();
304 if ( size
.x
> sizeMax
.x
)
307 if ( size
.y
> sizeMax
.y
)
310 page
= page
->GetNext();
313 if ( sizeMax
.x
> m_sizePage
.x
)
314 m_sizePage
.x
= sizeMax
.x
;
316 if ( sizeMax
.y
> m_sizePage
.y
)
317 m_sizePage
.y
= sizeMax
.y
;
320 bool wxWizard::ShowPage(wxWizardPage
*page
, bool goingForward
)
322 wxASSERT_MSG( page
!= m_page
, wxT("this is useless") );
324 // we'll use this to decide whether we have to change the label of this
325 // button or not (initially the label is "Next")
326 bool btnLabelWasNext
= TRUE
;
328 // Modified 10-20-2001 Robert Cavanaugh.
329 // Fixed bug for displaying a new bitmap
330 // in each *consecutive* page
332 // flag to indicate if this page uses a new bitmap
333 bool bmpIsDefault
= TRUE
;
335 // use these labels to determine if we need to change the bitmap
337 wxBitmap bmpPrev
, bmpCur
;
339 // check for previous page
342 // send the event to the old page
343 wxWizardEvent
event(wxEVT_WIZARD_PAGE_CHANGING
, GetId(), goingForward
, m_page
);
344 if ( m_page
->GetEventHandler()->ProcessEvent(event
) &&
347 // vetoed by the page
353 btnLabelWasNext
= HasNextPage(m_page
);
355 // Get the bitmap of the previous page (if it exists)
356 if ( m_page
->GetBitmap().Ok() )
358 bmpPrev
= m_page
->GetBitmap();
368 // terminate successfully
372 wxWizardEvent
event(wxEVT_WIZARD_FINISHED
, GetId(),FALSE
, 0);
373 (void)GetEventHandler()->ProcessEvent(event
);
378 // position and show the new page
379 (void)m_page
->TransferDataToWindow();
380 m_page
->SetSize(m_x
, m_y
, m_width
, m_height
);
382 // check if bitmap needs to be updated
383 // update default flag as well
384 if ( m_page
->GetBitmap().Ok() )
386 bmpCur
= m_page
->GetBitmap();
387 bmpIsDefault
= FALSE
;
390 // change the bitmap if:
391 // 1) a default bitmap was selected in constructor
392 // 2) this page was constructed with a bitmap
393 // 3) this bitmap is not the previous bitmap
394 if ( m_statbmp
&& (bmpCur
!= bmpPrev
) )
400 bmp
= m_page
->GetBitmap();
401 m_statbmp
->SetBitmap(bmp
);
404 // and update the buttons state
405 m_btnPrev
->Enable(HasPrevPage(m_page
));
407 bool hasNext
= HasNextPage(m_page
);
408 if ( btnLabelWasNext
!= hasNext
)
412 m_btnNext
->SetLabel(_("&Finish"));
414 m_btnNext
->SetLabel(_("&Next >"));
416 // nothing to do: the label was already correct
418 // send the change event to the new page now
419 wxWizardEvent
event(wxEVT_WIZARD_PAGE_CHANGED
, GetId(), goingForward
, m_page
);
420 (void)m_page
->GetEventHandler()->ProcessEvent(event
);
422 // and finally show it
429 bool wxWizard::RunWizard(wxWizardPage
*firstPage
)
431 wxCHECK_MSG( firstPage
, FALSE
, wxT("can't run empty wizard") );
435 // can't return FALSE here because there is no old page
436 (void)ShowPage(firstPage
, TRUE
/* forward */);
438 return ShowModal() == wxID_OK
;
441 wxWizardPage
*wxWizard::GetCurrentPage() const
446 wxSize
wxWizard::GetPageSize() const
448 // make sure that the controls are created because otherwise m_width and
449 // m_height would be both still -1
450 wxConstCast(this, wxWizard
)->DoCreateControls();
452 return wxSize(m_width
, m_height
);
455 void wxWizard::OnCancel(wxCommandEvent
& WXUNUSED(event
))
457 // this function probably can never be called when we don't have an active
458 // page, but a small extra check won't hurt
459 wxWindow
*win
= m_page
? (wxWindow
*)m_page
: (wxWindow
*)this;
461 wxWizardEvent
event(wxEVT_WIZARD_CANCEL
, GetId(), FALSE
, m_page
);
462 if ( !win
->GetEventHandler()->ProcessEvent(event
) || event
.IsAllowed() )
464 // no objections - close the dialog
465 EndModal(wxID_CANCEL
);
467 //else: request to Cancel ignored
470 void wxWizard::OnBackOrNext(wxCommandEvent
& event
)
472 wxASSERT_MSG( (event
.GetEventObject() == m_btnNext
) ||
473 (event
.GetEventObject() == m_btnPrev
),
474 wxT("unknown button") );
476 // ask the current page first: notice that we do it before calling
477 // GetNext/Prev() because the data transfered from the controls of the page
478 // may change the value returned by these methods
479 if ( m_page
&& !m_page
->TransferDataFromWindow() )
481 // the page data is incorrect, don't do anything
485 bool forward
= event
.GetEventObject() == m_btnNext
;
490 page
= m_page
->GetNext();
494 page
= m_page
->GetPrev();
496 wxASSERT_MSG( page
, wxT("\"<Back\" button should have been disabled") );
499 // just pass to the new page (or may be not - but we don't care here)
500 (void)ShowPage(page
, forward
);
503 void wxWizard::OnHelp(wxCommandEvent
& WXUNUSED(event
))
505 // this function probably can never be called when we don't have an active
506 // page, but a small extra check won't hurt
509 // Create and send the help event to the specific page handler
510 // event data contains the active page so that context-sensitive
512 wxWizardEvent
eventHelp(wxEVT_WIZARD_HELP
, GetId(), TRUE
, m_page
);
513 (void)m_page
->GetEventHandler()->ProcessEvent(eventHelp
);
517 void wxWizard::OnWizEvent(wxWizardEvent
& event
)
519 // the dialogs have wxWS_EX_BLOCK_EVENTS style on by default but we want to
520 // propagate wxEVT_WIZARD_XXX to the parent (if any), so do it manually
521 if ( !(GetExtraStyle() & wxWS_EX_BLOCK_EVENTS
) )
523 // the event will be propagated anyhow
527 wxWindow
*parent
= GetParent();
529 if ( !parent
|| !parent
->GetEventHandler()->ProcessEvent(event
) )
535 // ----------------------------------------------------------------------------
536 // our public interface
537 // ----------------------------------------------------------------------------
539 #ifdef WXWIN_COMPATIBILITY_2_2
542 wxWizard
*wxWizardBase::Create(wxWindow
*parent
,
544 const wxString
& title
,
545 const wxBitmap
& bitmap
,
547 const wxSize
& WXUNUSED(size
))
549 return new wxWizard(parent
, id
, title
, bitmap
, pos
);
552 #endif // WXWIN_COMPATIBILITY_2_2
554 // ----------------------------------------------------------------------------
556 // ----------------------------------------------------------------------------
558 wxWizardEvent::wxWizardEvent(wxEventType type
, int id
, bool direction
, wxWizardPage
* page
)
559 : wxNotifyEvent(type
, id
)
561 // Modified 10-20-2001 Robert Cavanaugh
562 // add the active page to the event data
563 m_direction
= direction
;
567 #endif // wxUSE_WIZARDDLG