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 ".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
)
61 BEGIN_EVENT_TABLE(wxWizard
, wxDialog
)
62 EVT_BUTTON(wxID_CANCEL
, wxWizard::OnCancel
)
63 EVT_BUTTON(-1, wxWizard::OnBackOrNext
)
66 IMPLEMENT_DYNAMIC_CLASS(wxWizard
, wxDialog
)
67 IMPLEMENT_ABSTRACT_CLASS(wxWizardPage
, wxPanel
)
68 IMPLEMENT_DYNAMIC_CLASS(wxWizardPageSimple
, wxWizardPage
)
69 IMPLEMENT_DYNAMIC_CLASS(wxWizardEvent
, wxNotifyEvent
)
71 // ============================================================================
73 // ============================================================================
75 // ----------------------------------------------------------------------------
77 // ----------------------------------------------------------------------------
79 wxWizardPage::wxWizardPage(wxWizard
*parent
,
80 const wxBitmap
& bitmap
,
81 const wxChar
*resource
)
84 if ( resource
!= NULL
)
86 if ( !LoadFromResource(this, resource
) )
88 wxFAIL_MSG(wxT("wxWizardPage LoadFromResource failed!!!!"));
92 m_PageBitmap
= bitmap
;
94 // initially the page is hidden, it's shown only when it becomes current
98 // ----------------------------------------------------------------------------
100 // ----------------------------------------------------------------------------
102 wxWizardPage
*wxWizardPageSimple::GetPrev() const
107 wxWizardPage
*wxWizardPageSimple::GetNext() const
111 // ----------------------------------------------------------------------------
112 // generic wxWizard implementation
113 // ----------------------------------------------------------------------------
115 void wxWizard::Init()
117 m_posWizard
= wxDefaultPosition
;
118 m_page
= (wxWizardPage
*)NULL
;
119 m_btnPrev
= m_btnNext
= NULL
;
123 bool wxWizard::Create(wxWindow
*parent
,
125 const wxString
& title
,
126 const wxBitmap
& bitmap
,
131 // just create the dialog itself here, the controls will be created in
132 // DoCreateControls() called later when we know our final size
133 m_page
= (wxWizardPage
*)NULL
;
134 m_btnPrev
= m_btnNext
= NULL
;
137 return wxDialog::Create(parent
, id
, title
, pos
);
140 void wxWizard::DoCreateControls()
142 // do nothing if the controls were already created
146 // constants defining the dialog layout
147 // ------------------------------------
149 // these constants define the position of the upper left corner of the
150 // bitmap or the page in the wizard
151 static const int X_MARGIN
= 10;
152 static const int Y_MARGIN
= 10;
154 // margin between the bitmap and the panel
155 static const int BITMAP_X_MARGIN
= 15;
157 // margin between the bitmap and the static line
158 static const int BITMAP_Y_MARGIN
= 15;
160 // margin between the static line and the buttons
161 static const int SEPARATOR_LINE_MARGIN
= 15;
163 // margin between "Next >" and "Cancel" buttons
164 static const int BUTTON_MARGIN
= 10;
166 // default width and height of the page
167 static const int DEFAULT_PAGE_WIDTH
= 270;
168 static const int DEFAULT_PAGE_HEIGHT
= 290;
173 wxSize sizeBtn
= wxButton::GetDefaultSize();
175 // the global dialog layout is: a row of buttons at the bottom (aligned to
176 // the right), the static line above them, the bitmap (if any) on the left
177 // of the upper part of the dialog and the panel in the remaining space
184 m_statbmp
= new wxStaticBitmap(this, -1, m_bitmap
, wxPoint(m_x
, m_y
));
186 m_x
+= m_bitmap
.GetWidth() + BITMAP_X_MARGIN
;
188 defaultHeight
= m_bitmap
.GetHeight();
192 m_statbmp
= (wxStaticBitmap
*)NULL
;
194 defaultHeight
= DEFAULT_PAGE_HEIGHT
;
197 // use default size if none given and also make sure that the dialog is
198 // not less than the default size
199 m_height
= m_sizePage
.y
== -1 ? defaultHeight
: m_sizePage
.y
;
200 m_width
= m_sizePage
.x
== -1 ? DEFAULT_PAGE_WIDTH
: m_sizePage
.x
;
201 if ( m_height
< defaultHeight
)
202 m_height
= defaultHeight
;
203 if ( m_width
< DEFAULT_PAGE_WIDTH
)
204 m_width
= DEFAULT_PAGE_WIDTH
;
207 int y
= m_y
+ m_height
+ BITMAP_Y_MARGIN
;
210 (void)new wxStaticLine(this, -1, wxPoint(x
, y
),
211 wxSize(m_x
+ m_width
- x
, 2));
212 #endif // wxUSE_STATLINE
214 x
= m_x
+ m_width
- 3*sizeBtn
.x
- BUTTON_MARGIN
;
215 y
+= SEPARATOR_LINE_MARGIN
;
217 if (GetExtraStyle() & wxWIZARD_EX_HELPBUTTON
)
222 (void)new wxButton(this, wxID_HELP
, _("&Help"), wxPoint(x
, y
), sizeBtn
);
227 m_btnPrev
= new wxButton(this, wxID_BACKWARD
, _("< &Back"), wxPoint(x
, y
), sizeBtn
);
230 m_btnNext
= new wxButton(this, wxID_FORWARD
, _("&Next >"), wxPoint(x
, y
), sizeBtn
);
232 x
+= sizeBtn
.x
+ BUTTON_MARGIN
;
233 (void)new wxButton(this, wxID_CANCEL
, _("&Cancel"), wxPoint(x
, y
), sizeBtn
);
235 // position and size the dialog
236 // ----------------------------
238 SetClientSize(m_x
+ m_width
+ X_MARGIN
,
239 m_y
+ m_height
+ BITMAP_Y_MARGIN
+
240 SEPARATOR_LINE_MARGIN
+ sizeBtn
.y
+ Y_MARGIN
);
242 if ( m_posWizard
== wxDefaultPosition
)
248 void wxWizard::SetPageSize(const wxSize
& size
)
250 // otherwise it will have no effect now as it's too late...
251 wxASSERT_MSG( !WasCreated(), _T("should be called before RunWizard()!") );
256 bool wxWizard::ShowPage(wxWizardPage
*page
, bool goingForward
)
258 wxASSERT_MSG( page
!= m_page
, wxT("this is useless") );
260 // we'll use this to decide whether we have to change the label of this
261 // button or not (initially the label is "Next")
262 bool btnLabelWasNext
= TRUE
;
264 // and this tells us whether we already had the default bitmap before
269 // send the event to the old page
270 wxWizardEvent
event(wxEVT_WIZARD_PAGE_CHANGING
, GetId(), goingForward
);
271 if ( m_page
->GetEventHandler()->ProcessEvent(event
) &&
274 // vetoed by the page
280 btnLabelWasNext
= m_page
->GetNext() != (wxWizardPage
*)NULL
;
281 bmpWasDefault
= !m_page
->GetBitmap().Ok();
283 else // no previous page
285 // always set the bitmap
295 // terminate successfully
301 // send the event to the new page now
302 wxWizardEvent
event(wxEVT_WIZARD_PAGE_CHANGED
, GetId(), goingForward
);
303 (void)m_page
->GetEventHandler()->ProcessEvent(event
);
305 // position and show the new page
306 (void)m_page
->TransferDataToWindow();
307 m_page
->SetSize(m_x
, m_y
, m_width
, m_height
);
310 // change the bitmap if necessary (and if we have it at all)
311 int bmpIsDefault
= !m_page
->GetBitmap().Ok();
312 if ( m_statbmp
&& (bmpIsDefault
!= bmpWasDefault
) )
318 bmp
= m_page
->GetBitmap();
319 m_statbmp
->SetBitmap(bmp
);
322 // and update the buttons state
323 m_btnPrev
->Enable(m_page
->GetPrev() != (wxWizardPage
*)NULL
);
325 bool hasNext
= m_page
->GetNext() != (wxWizardPage
*)NULL
;
326 if ( btnLabelWasNext
!= hasNext
)
330 m_btnNext
->SetLabel(_("&Finish"));
332 m_btnNext
->SetLabel(_("&Next >"));
334 // nothing to do: the label was already correct
339 bool wxWizard::RunWizard(wxWizardPage
*firstPage
)
341 wxCHECK_MSG( firstPage
, FALSE
, wxT("can't run empty wizard") );
345 // can't return FALSE here because there is no old page
346 (void)ShowPage(firstPage
, TRUE
/* forward */);
348 return ShowModal() == wxID_OK
;
351 wxWizardPage
*wxWizard::GetCurrentPage() const
356 wxSize
wxWizard::GetPageSize() const
358 // make sure that the controls are created because otherwise m_width and
359 // m_height would be both still -1
360 wxConstCast(this, wxWizard
)->DoCreateControls();
362 return wxSize(m_width
, m_height
);
365 void wxWizard::OnCancel(wxCommandEvent
& WXUNUSED(event
))
367 // this function probably can never be called when we don't have an active
368 // page, but a small extra check won't hurt
369 wxWindow
*win
= m_page
? (wxWindow
*)m_page
: (wxWindow
*)this;
371 wxWizardEvent
event(wxEVT_WIZARD_CANCEL
, GetId());
372 if ( !win
->GetEventHandler()->ProcessEvent(event
) || event
.IsAllowed() )
374 // no objections - close the dialog
375 EndModal(wxID_CANCEL
);
377 //else: request to Cancel ignored
380 void wxWizard::OnBackOrNext(wxCommandEvent
& event
)
382 wxASSERT_MSG( (event
.GetEventObject() == m_btnNext
) ||
383 (event
.GetEventObject() == m_btnPrev
),
384 wxT("unknown button") );
386 // ask the current page first: notice that we do it before calling
387 // GetNext/Prev() because the data transfered from the controls of the page
388 // may change the value returned by these methods
389 if ( m_page
&& !m_page
->TransferDataFromWindow() )
391 // the page data is incorrect, don't do anything
395 bool forward
= event
.GetEventObject() == m_btnNext
;
400 page
= m_page
->GetNext();
404 page
= m_page
->GetPrev();
406 wxASSERT_MSG( page
, wxT("\"<Back\" button should have been disabled") );
409 // just pass to the new page (or may be not - but we don't care here)
410 (void)ShowPage(page
, forward
);
413 // ----------------------------------------------------------------------------
414 // our public interface
415 // ----------------------------------------------------------------------------
418 wxWizard
*wxWizardBase::Create(wxWindow
*parent
,
420 const wxString
& title
,
421 const wxBitmap
& bitmap
,
423 const wxSize
& WXUNUSED(size
))
425 return new wxWizard(parent
, id
, title
, bitmap
, pos
);
428 // ----------------------------------------------------------------------------
430 // ----------------------------------------------------------------------------
432 wxWizardEvent::wxWizardEvent(wxEventType type
, int id
, bool direction
)
433 : wxNotifyEvent(type
, id
)
435 m_direction
= direction
;
438 #endif // wxUSE_WIZARDDLG