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
)
60 DEFINE_EVENT_TYPE(wxEVT_WIZARD_HELP
)
62 BEGIN_EVENT_TABLE(wxWizard
, wxDialog
)
63 EVT_BUTTON(wxID_CANCEL
, wxWizard::OnCancel
)
64 EVT_BUTTON(wxID_BACKWARD
, wxWizard::OnBackOrNext
)
65 EVT_BUTTON(wxID_FORWARD
, wxWizard::OnBackOrNext
)
66 EVT_BUTTON(wxID_HELP
, wxWizard::OnHelp
)
69 IMPLEMENT_DYNAMIC_CLASS(wxWizard
, wxDialog
)
70 IMPLEMENT_ABSTRACT_CLASS(wxWizardPage
, wxPanel
)
71 IMPLEMENT_DYNAMIC_CLASS(wxWizardPageSimple
, wxWizardPage
)
72 IMPLEMENT_DYNAMIC_CLASS(wxWizardEvent
, wxNotifyEvent
)
74 // ============================================================================
76 // ============================================================================
78 // ----------------------------------------------------------------------------
80 // ----------------------------------------------------------------------------
82 wxWizardPage::wxWizardPage(wxWizard
*parent
,
83 const wxBitmap
& bitmap
,
84 const wxChar
*resource
)
87 if ( resource
!= NULL
)
89 if ( !LoadFromResource(this, resource
) )
91 wxFAIL_MSG(wxT("wxWizardPage LoadFromResource failed!!!!"));
95 m_PageBitmap
= bitmap
;
97 // initially the page is hidden, it's shown only when it becomes current
101 // ----------------------------------------------------------------------------
102 // wxWizardPageSimple
103 // ----------------------------------------------------------------------------
105 wxWizardPage
*wxWizardPageSimple::GetPrev() const
110 wxWizardPage
*wxWizardPageSimple::GetNext() const
114 // ----------------------------------------------------------------------------
115 // generic wxWizard implementation
116 // ----------------------------------------------------------------------------
118 void wxWizard::Init()
120 m_posWizard
= wxDefaultPosition
;
121 m_page
= (wxWizardPage
*)NULL
;
122 m_btnPrev
= m_btnNext
= NULL
;
126 bool wxWizard::Create(wxWindow
*parent
,
128 const wxString
& title
,
129 const wxBitmap
& bitmap
,
134 // just create the dialog itself here, the controls will be created in
135 // DoCreateControls() called later when we know our final size
136 m_page
= (wxWizardPage
*)NULL
;
137 m_btnPrev
= m_btnNext
= NULL
;
140 return wxDialog::Create(parent
, id
, title
, pos
);
143 void wxWizard::DoCreateControls()
145 // do nothing if the controls were already created
149 // constants defining the dialog layout
150 // ------------------------------------
152 // these constants define the position of the upper left corner of the
153 // bitmap or the page in the wizard
154 static const int X_MARGIN
= 10;
155 static const int Y_MARGIN
= 10;
157 // margin between the bitmap and the panel
158 static const int BITMAP_X_MARGIN
= 15;
160 // margin between the bitmap and the static line
161 static const int BITMAP_Y_MARGIN
= 15;
163 // margin between the static line and the buttons
164 static const int SEPARATOR_LINE_MARGIN
= 15;
166 // margin between "Next >" and "Cancel" buttons
167 static const int BUTTON_MARGIN
= 10;
169 // default width and height of the page
170 static const int DEFAULT_PAGE_WIDTH
= 270;
171 static const int DEFAULT_PAGE_HEIGHT
= 290;
176 wxSize sizeBtn
= wxButton::GetDefaultSize();
178 // the global dialog layout is: a row of buttons at the bottom (aligned to
179 // the right), the static line above them, the bitmap (if any) on the left
180 // of the upper part of the dialog and the panel in the remaining space
187 m_statbmp
= new wxStaticBitmap(this, -1, m_bitmap
, wxPoint(m_x
, m_y
));
189 m_x
+= m_bitmap
.GetWidth() + BITMAP_X_MARGIN
;
191 defaultHeight
= m_bitmap
.GetHeight();
195 m_statbmp
= (wxStaticBitmap
*)NULL
;
197 defaultHeight
= DEFAULT_PAGE_HEIGHT
;
200 // use default size if none given and also make sure that the dialog is
201 // not less than the default size
202 m_height
= m_sizePage
.y
== -1 ? defaultHeight
: m_sizePage
.y
;
203 m_width
= m_sizePage
.x
== -1 ? DEFAULT_PAGE_WIDTH
: m_sizePage
.x
;
204 if ( m_height
< defaultHeight
)
205 m_height
= defaultHeight
;
206 if ( m_width
< DEFAULT_PAGE_WIDTH
)
207 m_width
= DEFAULT_PAGE_WIDTH
;
210 int y
= m_y
+ m_height
+ BITMAP_Y_MARGIN
;
213 (void)new wxStaticLine(this, -1, wxPoint(x
, y
),
214 wxSize(m_x
+ m_width
- x
, 2));
215 #endif // wxUSE_STATLINE
217 x
= m_x
+ m_width
- 3*sizeBtn
.x
- BUTTON_MARGIN
;
218 y
+= SEPARATOR_LINE_MARGIN
;
220 if (GetExtraStyle() & wxWIZARD_EX_HELPBUTTON
)
225 (void)new wxButton(this, wxID_HELP
, _("&Help"), wxPoint(x
, y
), sizeBtn
);
230 m_btnPrev
= new wxButton(this, wxID_BACKWARD
, _("< &Back"), wxPoint(x
, y
), sizeBtn
);
233 m_btnNext
= new wxButton(this, wxID_FORWARD
, _("&Next >"), wxPoint(x
, y
), sizeBtn
);
235 x
+= sizeBtn
.x
+ BUTTON_MARGIN
;
236 (void)new wxButton(this, wxID_CANCEL
, _("&Cancel"), wxPoint(x
, y
), sizeBtn
);
238 // position and size the dialog
239 // ----------------------------
241 SetClientSize(m_x
+ m_width
+ X_MARGIN
,
242 m_y
+ m_height
+ BITMAP_Y_MARGIN
+
243 SEPARATOR_LINE_MARGIN
+ sizeBtn
.y
+ Y_MARGIN
);
245 if ( m_posWizard
== wxDefaultPosition
)
251 void wxWizard::SetPageSize(const wxSize
& size
)
253 // otherwise it will have no effect now as it's too late...
254 wxASSERT_MSG( !WasCreated(), _T("should be called before RunWizard()!") );
259 bool wxWizard::ShowPage(wxWizardPage
*page
, bool goingForward
)
261 wxASSERT_MSG( page
!= m_page
, wxT("this is useless") );
263 // we'll use this to decide whether we have to change the label of this
264 // button or not (initially the label is "Next")
265 bool btnLabelWasNext
= TRUE
;
267 // Modified 10-20-2001 Robert Cavanaugh.
268 // Fixed bug for displaying a new bitmap
269 // in each *consecutive* page
271 // flag to indicate if this page uses a new bitmap
272 bool bmpIsDefault
= TRUE
;
274 // use these labels to determine if we need to change the bitmap
276 wxBitmap PreviousBitmap
= wxNullBitmap
;
277 wxBitmap ThisBitmap
= wxNullBitmap
;
279 // check for previous page
282 // send the event to the old page
283 wxWizardEvent
event(wxEVT_WIZARD_PAGE_CHANGING
, GetId(), goingForward
);
284 if ( m_page
->GetEventHandler()->ProcessEvent(event
) &&
287 // vetoed by the page
293 btnLabelWasNext
= m_page
->GetNext() != (wxWizardPage
*)NULL
;
295 // Get the bitmap of the previous page (if it exists)
296 if(m_page
->GetBitmap().Ok())
298 PreviousBitmap
= m_page
->GetBitmap();
308 // terminate successfully
313 // send the change event to the new page now
314 wxWizardEvent
event(wxEVT_WIZARD_PAGE_CHANGED
, GetId(), goingForward
);
315 (void)m_page
->GetEventHandler()->ProcessEvent(event
);
317 // position and show the new page
318 (void)m_page
->TransferDataToWindow();
319 m_page
->SetSize(m_x
, m_y
, m_width
, m_height
);
322 // check if bitmap needs to be updated
323 // update default flag as well
324 if(m_page
->GetBitmap().Ok())
326 ThisBitmap
= m_page
->GetBitmap();
327 bmpIsDefault
= FALSE
;
330 // change the bitmap if:
331 // 1) a default bitmap was selected in constructor
332 // 2) this page was constructed with a bitmap
333 // 3) this bitmap is not the previous bitmap
334 if( m_statbmp
&& (ThisBitmap
!= PreviousBitmap
) )
340 bmp
= m_page
->GetBitmap();
341 m_statbmp
->SetBitmap(bmp
);
344 // and update the buttons state
345 m_btnPrev
->Enable(m_page
->GetPrev() != (wxWizardPage
*)NULL
);
347 bool hasNext
= m_page
->GetNext() != (wxWizardPage
*)NULL
;
348 if ( btnLabelWasNext
!= hasNext
)
352 m_btnNext
->SetLabel(_("&Finish"));
354 m_btnNext
->SetLabel(_("&Next >"));
356 // nothing to do: the label was already correct
361 bool wxWizard::RunWizard(wxWizardPage
*firstPage
)
363 wxCHECK_MSG( firstPage
, FALSE
, wxT("can't run empty wizard") );
367 // can't return FALSE here because there is no old page
368 (void)ShowPage(firstPage
, TRUE
/* forward */);
370 return ShowModal() == wxID_OK
;
373 wxWizardPage
*wxWizard::GetCurrentPage() const
378 wxSize
wxWizard::GetPageSize() const
380 // make sure that the controls are created because otherwise m_width and
381 // m_height would be both still -1
382 wxConstCast(this, wxWizard
)->DoCreateControls();
384 return wxSize(m_width
, m_height
);
387 void wxWizard::OnCancel(wxCommandEvent
& WXUNUSED(event
))
389 // this function probably can never be called when we don't have an active
390 // page, but a small extra check won't hurt
391 wxWindow
*win
= m_page
? (wxWindow
*)m_page
: (wxWindow
*)this;
393 wxWizardEvent
event(wxEVT_WIZARD_CANCEL
, GetId());
394 if ( !win
->GetEventHandler()->ProcessEvent(event
) || event
.IsAllowed() )
396 // no objections - close the dialog
397 EndModal(wxID_CANCEL
);
399 //else: request to Cancel ignored
402 void wxWizard::OnBackOrNext(wxCommandEvent
& event
)
404 wxASSERT_MSG( (event
.GetEventObject() == m_btnNext
) ||
405 (event
.GetEventObject() == m_btnPrev
),
406 wxT("unknown button") );
408 // ask the current page first: notice that we do it before calling
409 // GetNext/Prev() because the data transfered from the controls of the page
410 // may change the value returned by these methods
411 if ( m_page
&& !m_page
->TransferDataFromWindow() )
413 // the page data is incorrect, don't do anything
417 bool forward
= event
.GetEventObject() == m_btnNext
;
422 page
= m_page
->GetNext();
426 page
= m_page
->GetPrev();
428 wxASSERT_MSG( page
, wxT("\"<Back\" button should have been disabled") );
431 // just pass to the new page (or may be not - but we don't care here)
432 (void)ShowPage(page
, forward
);
435 void wxWizard::OnHelp(wxCommandEvent
& WXUNUSED(event
))
437 // this function probably can never be called when we don't have an active
438 // page, but a small extra check won't hurt
441 // Create and send the help event to the specific page handler
442 // event data contains the active page so that context-sensitive
444 wxWizardEvent
eventHelp(wxEVT_WIZARD_HELP
, GetId(), TRUE
, m_page
);
445 (void)m_page
->GetEventHandler()->ProcessEvent(eventHelp
);
450 // ----------------------------------------------------------------------------
451 // our public interface
452 // ----------------------------------------------------------------------------
455 wxWizard
*wxWizardBase::Create(wxWindow
*parent
,
457 const wxString
& title
,
458 const wxBitmap
& bitmap
,
460 const wxSize
& WXUNUSED(size
))
462 return new wxWizard(parent
, id
, title
, bitmap
, pos
);
465 // ----------------------------------------------------------------------------
467 // ----------------------------------------------------------------------------
469 wxWizardEvent::wxWizardEvent(wxEventType type
, int id
, bool direction
, wxWizardPage
* page
)
470 : wxNotifyEvent(type
, id
)
472 // Modified 10-20-2001 Robert Cavanaugh
473 // add the active page to the event data
474 m_direction
= direction
;
478 #endif // wxUSE_WIZARDDLG