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_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
)
68 EVT_WIZARD_PAGE_CHANGED(-1, wxWizard::OnWizEvent
)
69 EVT_WIZARD_PAGE_CHANGING(-1, wxWizard::OnWizEvent
)
70 EVT_WIZARD_CANCEL(-1, wxWizard::OnWizEvent
)
71 EVT_WIZARD_HELP(-1, wxWizard::OnWizEvent
)
74 IMPLEMENT_DYNAMIC_CLASS(wxWizard
, wxDialog
)
75 IMPLEMENT_ABSTRACT_CLASS(wxWizardPage
, wxPanel
)
76 IMPLEMENT_DYNAMIC_CLASS(wxWizardPageSimple
, wxWizardPage
)
77 IMPLEMENT_DYNAMIC_CLASS(wxWizardEvent
, wxNotifyEvent
)
79 // ============================================================================
81 // ============================================================================
83 // ----------------------------------------------------------------------------
85 // ----------------------------------------------------------------------------
87 void wxWizardPage::Init()
89 m_bitmap
= wxNullBitmap
;
92 wxWizardPage::wxWizardPage(wxWizard
*parent
,
93 const wxBitmap
& bitmap
,
94 const wxChar
*resource
)
96 Create(parent
, bitmap
, resource
);
99 bool wxWizardPage::Create(wxWizard
*parent
,
100 const wxBitmap
& bitmap
,
101 const wxChar
*resource
)
103 if ( !wxPanel::Create(parent
, -1) )
106 if ( resource
!= NULL
)
108 #if wxUSE_WX_RESOURCES
109 if ( !LoadFromResource(this, resource
) )
111 wxFAIL_MSG(wxT("wxWizardPage LoadFromResource failed!!!!"));
113 #endif // wxUSE_RESOURCES
118 // initially the page is hidden, it's shown only when it becomes current
124 // ----------------------------------------------------------------------------
125 // wxWizardPageSimple
126 // ----------------------------------------------------------------------------
128 wxWizardPage
*wxWizardPageSimple::GetPrev() const
133 wxWizardPage
*wxWizardPageSimple::GetNext() const
137 // ----------------------------------------------------------------------------
138 // generic wxWizard implementation
139 // ----------------------------------------------------------------------------
141 void wxWizard::Init()
143 m_posWizard
= wxDefaultPosition
;
144 m_page
= (wxWizardPage
*)NULL
;
145 m_btnPrev
= m_btnNext
= NULL
;
149 bool wxWizard::Create(wxWindow
*parent
,
151 const wxString
& title
,
152 const wxBitmap
& bitmap
,
158 // just create the dialog itself here, the controls will be created in
159 // DoCreateControls() called later when we know our final size
160 m_page
= (wxWizardPage
*)NULL
;
161 m_btnPrev
= m_btnNext
= NULL
;
164 return wxDialog::Create(parent
, id
, title
, pos
);
167 void wxWizard::DoCreateControls()
169 // do nothing if the controls were already created
173 // constants defining the dialog layout
174 // ------------------------------------
176 // these constants define the position of the upper left corner of the
177 // bitmap or the page in the wizard
178 static const int X_MARGIN
= 10;
179 static const int Y_MARGIN
= 10;
181 // margin between the bitmap and the panel
182 static const int BITMAP_X_MARGIN
= 15;
184 // margin between the bitmap and the static line
185 static const int BITMAP_Y_MARGIN
= 15;
187 // margin between the static line and the buttons
188 static const int SEPARATOR_LINE_MARGIN
= 15;
190 // margin between "Next >" and "Cancel" buttons
191 static const int BUTTON_MARGIN
= 10;
193 // default width and height of the page
194 static const int DEFAULT_PAGE_WIDTH
= 270;
195 static const int DEFAULT_PAGE_HEIGHT
= 290;
200 wxSize sizeBtn
= wxButton::GetDefaultSize();
202 // the global dialog layout is: a row of buttons at the bottom (aligned to
203 // the right), the static line above them, the bitmap (if any) on the left
204 // of the upper part of the dialog and the panel in the remaining space
211 m_statbmp
= new wxStaticBitmap(this, -1, m_bitmap
, wxPoint(m_x
, m_y
));
213 m_x
+= m_bitmap
.GetWidth() + BITMAP_X_MARGIN
;
215 defaultHeight
= m_bitmap
.GetHeight();
219 m_statbmp
= (wxStaticBitmap
*)NULL
;
221 defaultHeight
= DEFAULT_PAGE_HEIGHT
;
224 // use default size if none given and also make sure that the dialog is
225 // not less than the default size
226 m_height
= m_sizePage
.y
== -1 ? defaultHeight
: m_sizePage
.y
;
227 m_width
= m_sizePage
.x
== -1 ? DEFAULT_PAGE_WIDTH
: m_sizePage
.x
;
228 if ( m_height
< defaultHeight
)
229 m_height
= defaultHeight
;
230 if ( m_width
< DEFAULT_PAGE_WIDTH
)
231 m_width
= DEFAULT_PAGE_WIDTH
;
234 int y
= m_y
+ m_height
+ BITMAP_Y_MARGIN
;
237 (void)new wxStaticLine(this, -1, wxPoint(x
, y
),
238 wxSize(m_x
+ m_width
- x
, 2));
239 #endif // wxUSE_STATLINE
241 x
= m_x
+ m_width
- 3*sizeBtn
.x
- BUTTON_MARGIN
;
242 y
+= SEPARATOR_LINE_MARGIN
;
244 if (GetExtraStyle() & wxWIZARD_EX_HELPBUTTON
)
249 (void)new wxButton(this, wxID_HELP
, _("&Help"), wxPoint(x
, y
), sizeBtn
);
254 m_btnPrev
= new wxButton(this, wxID_BACKWARD
, _("< &Back"), wxPoint(x
, y
), sizeBtn
);
257 m_btnNext
= new wxButton(this, wxID_FORWARD
, _("&Next >"), wxPoint(x
, y
), sizeBtn
);
259 x
+= sizeBtn
.x
+ BUTTON_MARGIN
;
260 (void)new wxButton(this, wxID_CANCEL
, _("&Cancel"), wxPoint(x
, y
), sizeBtn
);
262 // position and size the dialog
263 // ----------------------------
265 SetClientSize(m_x
+ m_width
+ X_MARGIN
,
266 m_y
+ m_height
+ BITMAP_Y_MARGIN
+
267 SEPARATOR_LINE_MARGIN
+ sizeBtn
.y
+ Y_MARGIN
);
269 if ( m_posWizard
== wxDefaultPosition
)
275 void wxWizard::SetPageSize(const wxSize
& size
)
277 // otherwise it will have no effect now as it's too late...
278 wxASSERT_MSG( !WasCreated(), _T("should be called before RunWizard()!") );
283 void wxWizard::FitToPage(const wxWizardPage
*page
)
285 // otherwise it will have no effect now as it's too late...
286 wxASSERT_MSG( !WasCreated(), _T("should be called before RunWizard()!") );
291 wxSize size
= page
->GetBestSize();
293 if ( size
.x
> sizeMax
.x
)
296 if ( size
.y
> sizeMax
.y
)
299 page
= page
->GetNext();
302 if ( sizeMax
.x
> m_sizePage
.x
)
303 m_sizePage
.x
= sizeMax
.x
;
305 if ( sizeMax
.y
> m_sizePage
.y
)
306 m_sizePage
.y
= sizeMax
.y
;
309 bool wxWizard::ShowPage(wxWizardPage
*page
, bool goingForward
)
311 wxASSERT_MSG( page
!= m_page
, wxT("this is useless") );
313 // we'll use this to decide whether we have to change the label of this
314 // button or not (initially the label is "Next")
315 bool btnLabelWasNext
= TRUE
;
317 // Modified 10-20-2001 Robert Cavanaugh.
318 // Fixed bug for displaying a new bitmap
319 // in each *consecutive* page
321 // flag to indicate if this page uses a new bitmap
322 bool bmpIsDefault
= TRUE
;
324 // use these labels to determine if we need to change the bitmap
326 wxBitmap bmpPrev
, bmpCur
;
328 // check for previous page
331 // send the event to the old page
332 wxWizardEvent
event(wxEVT_WIZARD_PAGE_CHANGING
, GetId(), goingForward
);
333 if ( m_page
->GetEventHandler()->ProcessEvent(event
) &&
336 // vetoed by the page
342 btnLabelWasNext
= m_page
->GetNext() != (wxWizardPage
*)NULL
;
344 // Get the bitmap of the previous page (if it exists)
345 if ( m_page
->GetBitmap().Ok() )
347 bmpPrev
= m_page
->GetBitmap();
357 // terminate successfully
362 // position and show the new page
363 (void)m_page
->TransferDataToWindow();
364 m_page
->SetSize(m_x
, m_y
, m_width
, m_height
);
366 // check if bitmap needs to be updated
367 // update default flag as well
368 if ( m_page
->GetBitmap().Ok() )
370 bmpCur
= m_page
->GetBitmap();
371 bmpIsDefault
= FALSE
;
374 // change the bitmap if:
375 // 1) a default bitmap was selected in constructor
376 // 2) this page was constructed with a bitmap
377 // 3) this bitmap is not the previous bitmap
378 if ( m_statbmp
&& (bmpCur
!= bmpPrev
) )
384 bmp
= m_page
->GetBitmap();
385 m_statbmp
->SetBitmap(bmp
);
388 // and update the buttons state
389 m_btnPrev
->Enable(m_page
->GetPrev() != (wxWizardPage
*)NULL
);
391 bool hasNext
= m_page
->GetNext() != (wxWizardPage
*)NULL
;
392 if ( btnLabelWasNext
!= hasNext
)
396 m_btnNext
->SetLabel(_("&Finish"));
398 m_btnNext
->SetLabel(_("&Next >"));
400 // nothing to do: the label was already correct
402 // send the change event to the new page now
403 wxWizardEvent
event(wxEVT_WIZARD_PAGE_CHANGED
, GetId(), goingForward
);
404 (void)m_page
->GetEventHandler()->ProcessEvent(event
);
406 // and finally show it
413 bool wxWizard::RunWizard(wxWizardPage
*firstPage
)
415 wxCHECK_MSG( firstPage
, FALSE
, wxT("can't run empty wizard") );
419 // can't return FALSE here because there is no old page
420 (void)ShowPage(firstPage
, TRUE
/* forward */);
422 return ShowModal() == wxID_OK
;
425 wxWizardPage
*wxWizard::GetCurrentPage() const
430 wxSize
wxWizard::GetPageSize() const
432 // make sure that the controls are created because otherwise m_width and
433 // m_height would be both still -1
434 wxConstCast(this, wxWizard
)->DoCreateControls();
436 return wxSize(m_width
, m_height
);
439 void wxWizard::OnCancel(wxCommandEvent
& WXUNUSED(event
))
441 // this function probably can never be called when we don't have an active
442 // page, but a small extra check won't hurt
443 wxWindow
*win
= m_page
? (wxWindow
*)m_page
: (wxWindow
*)this;
445 wxWizardEvent
event(wxEVT_WIZARD_CANCEL
, GetId());
446 if ( !win
->GetEventHandler()->ProcessEvent(event
) || event
.IsAllowed() )
448 // no objections - close the dialog
449 EndModal(wxID_CANCEL
);
451 //else: request to Cancel ignored
454 void wxWizard::OnBackOrNext(wxCommandEvent
& event
)
456 wxASSERT_MSG( (event
.GetEventObject() == m_btnNext
) ||
457 (event
.GetEventObject() == m_btnPrev
),
458 wxT("unknown button") );
460 // ask the current page first: notice that we do it before calling
461 // GetNext/Prev() because the data transfered from the controls of the page
462 // may change the value returned by these methods
463 if ( m_page
&& !m_page
->TransferDataFromWindow() )
465 // the page data is incorrect, don't do anything
469 bool forward
= event
.GetEventObject() == m_btnNext
;
474 page
= m_page
->GetNext();
478 page
= m_page
->GetPrev();
480 wxASSERT_MSG( page
, wxT("\"<Back\" button should have been disabled") );
483 // just pass to the new page (or may be not - but we don't care here)
484 (void)ShowPage(page
, forward
);
487 void wxWizard::OnHelp(wxCommandEvent
& WXUNUSED(event
))
489 // this function probably can never be called when we don't have an active
490 // page, but a small extra check won't hurt
493 // Create and send the help event to the specific page handler
494 // event data contains the active page so that context-sensitive
496 wxWizardEvent
eventHelp(wxEVT_WIZARD_HELP
, GetId(), TRUE
, m_page
);
497 (void)m_page
->GetEventHandler()->ProcessEvent(eventHelp
);
501 void wxWizard::OnWizEvent(wxWizardEvent
& event
)
503 // the dialogs have wxWS_EX_BLOCK_EVENTS style on by default but we want to
504 // propagate wxEVT_WIZARD_XXX to the parent (if any), so do it manually
505 if ( !(GetExtraStyle() & wxWS_EX_BLOCK_EVENTS
) )
507 // the event will be propagated anyhow
511 wxWindow
*parent
= GetParent();
513 if ( !parent
|| !parent
->GetEventHandler()->ProcessEvent(event
) )
519 // ----------------------------------------------------------------------------
520 // our public interface
521 // ----------------------------------------------------------------------------
524 wxWizard
*wxWizardBase::Create(wxWindow
*parent
,
526 const wxString
& title
,
527 const wxBitmap
& bitmap
,
529 const wxSize
& WXUNUSED(size
))
531 return new wxWizard(parent
, id
, title
, bitmap
, pos
);
534 // ----------------------------------------------------------------------------
536 // ----------------------------------------------------------------------------
538 wxWizardEvent::wxWizardEvent(wxEventType type
, int id
, bool direction
, wxWizardPage
* page
)
539 : wxNotifyEvent(type
, id
)
541 // Modified 10-20-2001 Robert Cavanaugh
542 // add the active page to the event data
543 m_direction
= direction
;
547 #endif // wxUSE_WIZARDDLG