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 void wxWizardPage::Init()
84 m_bitmap
= wxNullBitmap
;
87 wxWizardPage::wxWizardPage(wxWizard
*parent
,
88 const wxBitmap
& bitmap
,
89 const wxChar
*resource
)
91 Create(parent
, bitmap
, resource
);
94 bool wxWizardPage::Create(wxWizard
*parent
,
95 const wxBitmap
& bitmap
,
96 const wxChar
*resource
)
98 if ( !wxPanel::Create(parent
, -1) )
101 if ( resource
!= NULL
)
103 #if wxUSE_WX_RESOURCES
104 if ( !LoadFromResource(this, resource
) )
106 wxFAIL_MSG(wxT("wxWizardPage LoadFromResource failed!!!!"));
108 #endif // wxUSE_RESOURCES
113 // initially the page is hidden, it's shown only when it becomes current
119 // ----------------------------------------------------------------------------
120 // wxWizardPageSimple
121 // ----------------------------------------------------------------------------
123 wxWizardPage
*wxWizardPageSimple::GetPrev() const
128 wxWizardPage
*wxWizardPageSimple::GetNext() const
132 // ----------------------------------------------------------------------------
133 // generic wxWizard implementation
134 // ----------------------------------------------------------------------------
136 void wxWizard::Init()
138 m_posWizard
= wxDefaultPosition
;
139 m_page
= (wxWizardPage
*)NULL
;
140 m_btnPrev
= m_btnNext
= NULL
;
144 bool wxWizard::Create(wxWindow
*parent
,
146 const wxString
& title
,
147 const wxBitmap
& bitmap
,
153 // just create the dialog itself here, the controls will be created in
154 // DoCreateControls() called later when we know our final size
155 m_page
= (wxWizardPage
*)NULL
;
156 m_btnPrev
= m_btnNext
= NULL
;
159 return wxDialog::Create(parent
, id
, title
, pos
);
162 void wxWizard::DoCreateControls()
164 // do nothing if the controls were already created
168 // constants defining the dialog layout
169 // ------------------------------------
171 // these constants define the position of the upper left corner of the
172 // bitmap or the page in the wizard
173 static const int X_MARGIN
= 10;
174 static const int Y_MARGIN
= 10;
176 // margin between the bitmap and the panel
177 static const int BITMAP_X_MARGIN
= 15;
179 // margin between the bitmap and the static line
180 static const int BITMAP_Y_MARGIN
= 15;
182 // margin between the static line and the buttons
183 static const int SEPARATOR_LINE_MARGIN
= 15;
185 // margin between "Next >" and "Cancel" buttons
186 static const int BUTTON_MARGIN
= 10;
188 // default width and height of the page
189 static const int DEFAULT_PAGE_WIDTH
= 270;
190 static const int DEFAULT_PAGE_HEIGHT
= 290;
195 wxSize sizeBtn
= wxButton::GetDefaultSize();
197 // the global dialog layout is: a row of buttons at the bottom (aligned to
198 // the right), the static line above them, the bitmap (if any) on the left
199 // of the upper part of the dialog and the panel in the remaining space
206 m_statbmp
= new wxStaticBitmap(this, -1, m_bitmap
, wxPoint(m_x
, m_y
));
208 m_x
+= m_bitmap
.GetWidth() + BITMAP_X_MARGIN
;
210 defaultHeight
= m_bitmap
.GetHeight();
214 m_statbmp
= (wxStaticBitmap
*)NULL
;
216 defaultHeight
= DEFAULT_PAGE_HEIGHT
;
219 // use default size if none given and also make sure that the dialog is
220 // not less than the default size
221 m_height
= m_sizePage
.y
== -1 ? defaultHeight
: m_sizePage
.y
;
222 m_width
= m_sizePage
.x
== -1 ? DEFAULT_PAGE_WIDTH
: m_sizePage
.x
;
223 if ( m_height
< defaultHeight
)
224 m_height
= defaultHeight
;
225 if ( m_width
< DEFAULT_PAGE_WIDTH
)
226 m_width
= DEFAULT_PAGE_WIDTH
;
229 int y
= m_y
+ m_height
+ BITMAP_Y_MARGIN
;
232 (void)new wxStaticLine(this, -1, wxPoint(x
, y
),
233 wxSize(m_x
+ m_width
- x
, 2));
234 #endif // wxUSE_STATLINE
236 x
= m_x
+ m_width
- 3*sizeBtn
.x
- BUTTON_MARGIN
;
237 y
+= SEPARATOR_LINE_MARGIN
;
239 if (GetExtraStyle() & wxWIZARD_EX_HELPBUTTON
)
244 (void)new wxButton(this, wxID_HELP
, _("&Help"), wxPoint(x
, y
), sizeBtn
);
249 m_btnPrev
= new wxButton(this, wxID_BACKWARD
, _("< &Back"), wxPoint(x
, y
), sizeBtn
);
252 m_btnNext
= new wxButton(this, wxID_FORWARD
, _("&Next >"), wxPoint(x
, y
), sizeBtn
);
254 x
+= sizeBtn
.x
+ BUTTON_MARGIN
;
255 (void)new wxButton(this, wxID_CANCEL
, _("&Cancel"), wxPoint(x
, y
), sizeBtn
);
257 // position and size the dialog
258 // ----------------------------
260 SetClientSize(m_x
+ m_width
+ X_MARGIN
,
261 m_y
+ m_height
+ BITMAP_Y_MARGIN
+
262 SEPARATOR_LINE_MARGIN
+ sizeBtn
.y
+ Y_MARGIN
);
264 if ( m_posWizard
== wxDefaultPosition
)
270 void wxWizard::SetPageSize(const wxSize
& size
)
272 // otherwise it will have no effect now as it's too late...
273 wxASSERT_MSG( !WasCreated(), _T("should be called before RunWizard()!") );
278 bool wxWizard::ShowPage(wxWizardPage
*page
, bool goingForward
)
280 wxASSERT_MSG( page
!= m_page
, wxT("this is useless") );
282 // we'll use this to decide whether we have to change the label of this
283 // button or not (initially the label is "Next")
284 bool btnLabelWasNext
= TRUE
;
286 // Modified 10-20-2001 Robert Cavanaugh.
287 // Fixed bug for displaying a new bitmap
288 // in each *consecutive* page
290 // flag to indicate if this page uses a new bitmap
291 bool bmpIsDefault
= TRUE
;
293 // use these labels to determine if we need to change the bitmap
295 wxBitmap PreviousBitmap
= wxNullBitmap
;
296 wxBitmap ThisBitmap
= wxNullBitmap
;
298 // check for previous page
301 // send the event to the old page
302 wxWizardEvent
event(wxEVT_WIZARD_PAGE_CHANGING
, GetId(), goingForward
);
303 if ( m_page
->GetEventHandler()->ProcessEvent(event
) &&
306 // vetoed by the page
312 btnLabelWasNext
= m_page
->GetNext() != (wxWizardPage
*)NULL
;
314 // Get the bitmap of the previous page (if it exists)
315 if(m_page
->GetBitmap().Ok())
317 PreviousBitmap
= m_page
->GetBitmap();
327 // terminate successfully
332 // send the change event to the new page now
333 wxWizardEvent
event(wxEVT_WIZARD_PAGE_CHANGED
, GetId(), goingForward
);
334 (void)m_page
->GetEventHandler()->ProcessEvent(event
);
336 // position and show the new page
337 (void)m_page
->TransferDataToWindow();
338 m_page
->SetSize(m_x
, m_y
, m_width
, m_height
);
341 // check if bitmap needs to be updated
342 // update default flag as well
343 if(m_page
->GetBitmap().Ok())
345 ThisBitmap
= m_page
->GetBitmap();
346 bmpIsDefault
= FALSE
;
349 // change the bitmap if:
350 // 1) a default bitmap was selected in constructor
351 // 2) this page was constructed with a bitmap
352 // 3) this bitmap is not the previous bitmap
353 if( m_statbmp
&& (ThisBitmap
!= PreviousBitmap
) )
359 bmp
= m_page
->GetBitmap();
360 m_statbmp
->SetBitmap(bmp
);
363 // and update the buttons state
364 m_btnPrev
->Enable(m_page
->GetPrev() != (wxWizardPage
*)NULL
);
366 bool hasNext
= m_page
->GetNext() != (wxWizardPage
*)NULL
;
367 if ( btnLabelWasNext
!= hasNext
)
371 m_btnNext
->SetLabel(_("&Finish"));
373 m_btnNext
->SetLabel(_("&Next >"));
375 // nothing to do: the label was already correct
380 bool wxWizard::RunWizard(wxWizardPage
*firstPage
)
382 wxCHECK_MSG( firstPage
, FALSE
, wxT("can't run empty wizard") );
386 // can't return FALSE here because there is no old page
387 (void)ShowPage(firstPage
, TRUE
/* forward */);
389 return ShowModal() == wxID_OK
;
392 wxWizardPage
*wxWizard::GetCurrentPage() const
397 wxSize
wxWizard::GetPageSize() const
399 // make sure that the controls are created because otherwise m_width and
400 // m_height would be both still -1
401 wxConstCast(this, wxWizard
)->DoCreateControls();
403 return wxSize(m_width
, m_height
);
406 void wxWizard::OnCancel(wxCommandEvent
& WXUNUSED(event
))
408 // this function probably can never be called when we don't have an active
409 // page, but a small extra check won't hurt
410 wxWindow
*win
= m_page
? (wxWindow
*)m_page
: (wxWindow
*)this;
412 wxWizardEvent
event(wxEVT_WIZARD_CANCEL
, GetId());
413 if ( !win
->GetEventHandler()->ProcessEvent(event
) || event
.IsAllowed() )
415 // no objections - close the dialog
416 EndModal(wxID_CANCEL
);
418 //else: request to Cancel ignored
421 void wxWizard::OnBackOrNext(wxCommandEvent
& event
)
423 wxASSERT_MSG( (event
.GetEventObject() == m_btnNext
) ||
424 (event
.GetEventObject() == m_btnPrev
),
425 wxT("unknown button") );
427 // ask the current page first: notice that we do it before calling
428 // GetNext/Prev() because the data transfered from the controls of the page
429 // may change the value returned by these methods
430 if ( m_page
&& !m_page
->TransferDataFromWindow() )
432 // the page data is incorrect, don't do anything
436 bool forward
= event
.GetEventObject() == m_btnNext
;
441 page
= m_page
->GetNext();
445 page
= m_page
->GetPrev();
447 wxASSERT_MSG( page
, wxT("\"<Back\" button should have been disabled") );
450 // just pass to the new page (or may be not - but we don't care here)
451 (void)ShowPage(page
, forward
);
454 void wxWizard::OnHelp(wxCommandEvent
& WXUNUSED(event
))
456 // this function probably can never be called when we don't have an active
457 // page, but a small extra check won't hurt
460 // Create and send the help event to the specific page handler
461 // event data contains the active page so that context-sensitive
463 wxWizardEvent
eventHelp(wxEVT_WIZARD_HELP
, GetId(), TRUE
, m_page
);
464 (void)m_page
->GetEventHandler()->ProcessEvent(eventHelp
);
469 // ----------------------------------------------------------------------------
470 // our public interface
471 // ----------------------------------------------------------------------------
474 wxWizard
*wxWizardBase::Create(wxWindow
*parent
,
476 const wxString
& title
,
477 const wxBitmap
& bitmap
,
479 const wxSize
& WXUNUSED(size
))
481 return new wxWizard(parent
, id
, title
, bitmap
, pos
);
484 // ----------------------------------------------------------------------------
486 // ----------------------------------------------------------------------------
488 wxWizardEvent::wxWizardEvent(wxEventType type
, int id
, bool direction
, wxWizardPage
* page
)
489 : wxNotifyEvent(type
, id
)
491 // Modified 10-20-2001 Robert Cavanaugh
492 // add the active page to the event data
493 m_direction
= direction
;
497 #endif // wxUSE_WIZARDDLG