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
)
90 if ( !LoadFromResource(this, resource
) )
92 wxFAIL_MSG(wxT("wxWizardPage LoadFromResource failed!!!!"));
94 #endif // wxUSE_RESOURCES
97 m_PageBitmap
= bitmap
;
99 // initially the page is hidden, it's shown only when it becomes current
103 // ----------------------------------------------------------------------------
104 // wxWizardPageSimple
105 // ----------------------------------------------------------------------------
107 wxWizardPage
*wxWizardPageSimple::GetPrev() const
112 wxWizardPage
*wxWizardPageSimple::GetNext() const
116 // ----------------------------------------------------------------------------
117 // generic wxWizard implementation
118 // ----------------------------------------------------------------------------
120 void wxWizard::Init()
122 m_posWizard
= wxDefaultPosition
;
123 m_page
= (wxWizardPage
*)NULL
;
124 m_btnPrev
= m_btnNext
= NULL
;
128 bool wxWizard::Create(wxWindow
*parent
,
130 const wxString
& title
,
131 const wxBitmap
& bitmap
,
136 // just create the dialog itself here, the controls will be created in
137 // DoCreateControls() called later when we know our final size
138 m_page
= (wxWizardPage
*)NULL
;
139 m_btnPrev
= m_btnNext
= NULL
;
142 return wxDialog::Create(parent
, id
, title
, pos
);
145 void wxWizard::DoCreateControls()
147 // do nothing if the controls were already created
151 // constants defining the dialog layout
152 // ------------------------------------
154 // these constants define the position of the upper left corner of the
155 // bitmap or the page in the wizard
156 static const int X_MARGIN
= 10;
157 static const int Y_MARGIN
= 10;
159 // margin between the bitmap and the panel
160 static const int BITMAP_X_MARGIN
= 15;
162 // margin between the bitmap and the static line
163 static const int BITMAP_Y_MARGIN
= 15;
165 // margin between the static line and the buttons
166 static const int SEPARATOR_LINE_MARGIN
= 15;
168 // margin between "Next >" and "Cancel" buttons
169 static const int BUTTON_MARGIN
= 10;
171 // default width and height of the page
172 static const int DEFAULT_PAGE_WIDTH
= 270;
173 static const int DEFAULT_PAGE_HEIGHT
= 290;
178 wxSize sizeBtn
= wxButton::GetDefaultSize();
180 // the global dialog layout is: a row of buttons at the bottom (aligned to
181 // the right), the static line above them, the bitmap (if any) on the left
182 // of the upper part of the dialog and the panel in the remaining space
189 m_statbmp
= new wxStaticBitmap(this, -1, m_bitmap
, wxPoint(m_x
, m_y
));
191 m_x
+= m_bitmap
.GetWidth() + BITMAP_X_MARGIN
;
193 defaultHeight
= m_bitmap
.GetHeight();
197 m_statbmp
= (wxStaticBitmap
*)NULL
;
199 defaultHeight
= DEFAULT_PAGE_HEIGHT
;
202 // use default size if none given and also make sure that the dialog is
203 // not less than the default size
204 m_height
= m_sizePage
.y
== -1 ? defaultHeight
: m_sizePage
.y
;
205 m_width
= m_sizePage
.x
== -1 ? DEFAULT_PAGE_WIDTH
: m_sizePage
.x
;
206 if ( m_height
< defaultHeight
)
207 m_height
= defaultHeight
;
208 if ( m_width
< DEFAULT_PAGE_WIDTH
)
209 m_width
= DEFAULT_PAGE_WIDTH
;
212 int y
= m_y
+ m_height
+ BITMAP_Y_MARGIN
;
215 (void)new wxStaticLine(this, -1, wxPoint(x
, y
),
216 wxSize(m_x
+ m_width
- x
, 2));
217 #endif // wxUSE_STATLINE
219 x
= m_x
+ m_width
- 3*sizeBtn
.x
- BUTTON_MARGIN
;
220 y
+= SEPARATOR_LINE_MARGIN
;
222 if (GetExtraStyle() & wxWIZARD_EX_HELPBUTTON
)
227 (void)new wxButton(this, wxID_HELP
, _("&Help"), wxPoint(x
, y
), sizeBtn
);
232 m_btnPrev
= new wxButton(this, wxID_BACKWARD
, _("< &Back"), wxPoint(x
, y
), sizeBtn
);
235 m_btnNext
= new wxButton(this, wxID_FORWARD
, _("&Next >"), wxPoint(x
, y
), sizeBtn
);
237 x
+= sizeBtn
.x
+ BUTTON_MARGIN
;
238 (void)new wxButton(this, wxID_CANCEL
, _("&Cancel"), wxPoint(x
, y
), sizeBtn
);
240 // position and size the dialog
241 // ----------------------------
243 SetClientSize(m_x
+ m_width
+ X_MARGIN
,
244 m_y
+ m_height
+ BITMAP_Y_MARGIN
+
245 SEPARATOR_LINE_MARGIN
+ sizeBtn
.y
+ Y_MARGIN
);
247 if ( m_posWizard
== wxDefaultPosition
)
253 void wxWizard::SetPageSize(const wxSize
& size
)
255 // otherwise it will have no effect now as it's too late...
256 wxASSERT_MSG( !WasCreated(), _T("should be called before RunWizard()!") );
261 bool wxWizard::ShowPage(wxWizardPage
*page
, bool goingForward
)
263 wxASSERT_MSG( page
!= m_page
, wxT("this is useless") );
265 // we'll use this to decide whether we have to change the label of this
266 // button or not (initially the label is "Next")
267 bool btnLabelWasNext
= TRUE
;
269 // Modified 10-20-2001 Robert Cavanaugh.
270 // Fixed bug for displaying a new bitmap
271 // in each *consecutive* page
273 // flag to indicate if this page uses a new bitmap
274 bool bmpIsDefault
= TRUE
;
276 // use these labels to determine if we need to change the bitmap
278 wxBitmap PreviousBitmap
= wxNullBitmap
;
279 wxBitmap ThisBitmap
= wxNullBitmap
;
281 // check for previous page
284 // send the event to the old page
285 wxWizardEvent
event(wxEVT_WIZARD_PAGE_CHANGING
, GetId(), goingForward
);
286 if ( m_page
->GetEventHandler()->ProcessEvent(event
) &&
289 // vetoed by the page
295 btnLabelWasNext
= m_page
->GetNext() != (wxWizardPage
*)NULL
;
297 // Get the bitmap of the previous page (if it exists)
298 if(m_page
->GetBitmap().Ok())
300 PreviousBitmap
= m_page
->GetBitmap();
310 // terminate successfully
315 // send the change event to the new page now
316 wxWizardEvent
event(wxEVT_WIZARD_PAGE_CHANGED
, GetId(), goingForward
);
317 (void)m_page
->GetEventHandler()->ProcessEvent(event
);
319 // position and show the new page
320 (void)m_page
->TransferDataToWindow();
321 m_page
->SetSize(m_x
, m_y
, m_width
, m_height
);
324 // check if bitmap needs to be updated
325 // update default flag as well
326 if(m_page
->GetBitmap().Ok())
328 ThisBitmap
= m_page
->GetBitmap();
329 bmpIsDefault
= FALSE
;
332 // change the bitmap if:
333 // 1) a default bitmap was selected in constructor
334 // 2) this page was constructed with a bitmap
335 // 3) this bitmap is not the previous bitmap
336 if( m_statbmp
&& (ThisBitmap
!= PreviousBitmap
) )
342 bmp
= m_page
->GetBitmap();
343 m_statbmp
->SetBitmap(bmp
);
346 // and update the buttons state
347 m_btnPrev
->Enable(m_page
->GetPrev() != (wxWizardPage
*)NULL
);
349 bool hasNext
= m_page
->GetNext() != (wxWizardPage
*)NULL
;
350 if ( btnLabelWasNext
!= hasNext
)
354 m_btnNext
->SetLabel(_("&Finish"));
356 m_btnNext
->SetLabel(_("&Next >"));
358 // nothing to do: the label was already correct
363 bool wxWizard::RunWizard(wxWizardPage
*firstPage
)
365 wxCHECK_MSG( firstPage
, FALSE
, wxT("can't run empty wizard") );
369 // can't return FALSE here because there is no old page
370 (void)ShowPage(firstPage
, TRUE
/* forward */);
372 return ShowModal() == wxID_OK
;
375 wxWizardPage
*wxWizard::GetCurrentPage() const
380 wxSize
wxWizard::GetPageSize() const
382 // make sure that the controls are created because otherwise m_width and
383 // m_height would be both still -1
384 wxConstCast(this, wxWizard
)->DoCreateControls();
386 return wxSize(m_width
, m_height
);
389 void wxWizard::OnCancel(wxCommandEvent
& WXUNUSED(event
))
391 // this function probably can never be called when we don't have an active
392 // page, but a small extra check won't hurt
393 wxWindow
*win
= m_page
? (wxWindow
*)m_page
: (wxWindow
*)this;
395 wxWizardEvent
event(wxEVT_WIZARD_CANCEL
, GetId());
396 if ( !win
->GetEventHandler()->ProcessEvent(event
) || event
.IsAllowed() )
398 // no objections - close the dialog
399 EndModal(wxID_CANCEL
);
401 //else: request to Cancel ignored
404 void wxWizard::OnBackOrNext(wxCommandEvent
& event
)
406 wxASSERT_MSG( (event
.GetEventObject() == m_btnNext
) ||
407 (event
.GetEventObject() == m_btnPrev
),
408 wxT("unknown button") );
410 // ask the current page first: notice that we do it before calling
411 // GetNext/Prev() because the data transfered from the controls of the page
412 // may change the value returned by these methods
413 if ( m_page
&& !m_page
->TransferDataFromWindow() )
415 // the page data is incorrect, don't do anything
419 bool forward
= event
.GetEventObject() == m_btnNext
;
424 page
= m_page
->GetNext();
428 page
= m_page
->GetPrev();
430 wxASSERT_MSG( page
, wxT("\"<Back\" button should have been disabled") );
433 // just pass to the new page (or may be not - but we don't care here)
434 (void)ShowPage(page
, forward
);
437 void wxWizard::OnHelp(wxCommandEvent
& WXUNUSED(event
))
439 // this function probably can never be called when we don't have an active
440 // page, but a small extra check won't hurt
443 // Create and send the help event to the specific page handler
444 // event data contains the active page so that context-sensitive
446 wxWizardEvent
eventHelp(wxEVT_WIZARD_HELP
, GetId(), TRUE
, m_page
);
447 (void)m_page
->GetEventHandler()->ProcessEvent(eventHelp
);
452 // ----------------------------------------------------------------------------
453 // our public interface
454 // ----------------------------------------------------------------------------
457 wxWizard
*wxWizardBase::Create(wxWindow
*parent
,
459 const wxString
& title
,
460 const wxBitmap
& bitmap
,
462 const wxSize
& WXUNUSED(size
))
464 return new wxWizard(parent
, id
, title
, bitmap
, pos
);
467 // ----------------------------------------------------------------------------
469 // ----------------------------------------------------------------------------
471 wxWizardEvent::wxWizardEvent(wxEventType type
, int id
, bool direction
, wxWizardPage
* page
)
472 : wxNotifyEvent(type
, id
)
474 // Modified 10-20-2001 Robert Cavanaugh
475 // add the active page to the event data
476 m_direction
= direction
;
480 #endif // wxUSE_WIZARDDLG