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 licence
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 // ----------------------------------------------------------------------------
48 // event tables and such
49 // ----------------------------------------------------------------------------
51 DEFINE_EVENT_TYPE(wxEVT_WIZARD_PAGE_CHANGED
)
52 DEFINE_EVENT_TYPE(wxEVT_WIZARD_PAGE_CHANGING
)
53 DEFINE_EVENT_TYPE(wxEVT_WIZARD_CANCEL
)
54 DEFINE_EVENT_TYPE(wxEVT_WIZARD_FINISHED
)
55 DEFINE_EVENT_TYPE(wxEVT_WIZARD_HELP
)
57 BEGIN_EVENT_TABLE(wxWizard
, wxDialog
)
58 EVT_BUTTON(wxID_CANCEL
, wxWizard::OnCancel
)
59 EVT_BUTTON(wxID_BACKWARD
, wxWizard::OnBackOrNext
)
60 EVT_BUTTON(wxID_FORWARD
, wxWizard::OnBackOrNext
)
61 EVT_BUTTON(wxID_HELP
, wxWizard::OnHelp
)
63 EVT_WIZARD_PAGE_CHANGED(-1, wxWizard::OnWizEvent
)
64 EVT_WIZARD_PAGE_CHANGING(-1, wxWizard::OnWizEvent
)
65 EVT_WIZARD_CANCEL(-1, wxWizard::OnWizEvent
)
66 EVT_WIZARD_FINISHED(-1, wxWizard::OnWizEvent
)
67 EVT_WIZARD_HELP(-1, wxWizard::OnWizEvent
)
70 IMPLEMENT_DYNAMIC_CLASS(wxWizard
, wxDialog
)
71 IMPLEMENT_ABSTRACT_CLASS(wxWizardPage
, wxPanel
)
72 IMPLEMENT_DYNAMIC_CLASS(wxWizardPageSimple
, wxWizardPage
)
73 IMPLEMENT_DYNAMIC_CLASS(wxWizardEvent
, wxNotifyEvent
)
75 // ============================================================================
77 // ============================================================================
79 // ----------------------------------------------------------------------------
81 // ----------------------------------------------------------------------------
83 void wxWizardPage::Init()
85 m_bitmap
= wxNullBitmap
;
88 wxWizardPage::wxWizardPage(wxWizard
*parent
,
89 const wxBitmap
& bitmap
,
90 const wxChar
*resource
)
92 Create(parent
, bitmap
, resource
);
95 bool wxWizardPage::Create(wxWizard
*parent
,
96 const wxBitmap
& bitmap
,
97 const wxChar
*resource
)
99 if ( !wxPanel::Create(parent
, -1) )
102 if ( resource
!= NULL
)
104 #if wxUSE_WX_RESOURCES
106 if ( !LoadFromResource(this, resource
) )
108 wxFAIL_MSG(wxT("wxWizardPage LoadFromResource failed!!!!"));
111 #endif // wxUSE_RESOURCES
116 // initially the page is hidden, it's shown only when it becomes current
122 // ----------------------------------------------------------------------------
123 // wxWizardPageSimple
124 // ----------------------------------------------------------------------------
126 wxWizardPage
*wxWizardPageSimple::GetPrev() const
131 wxWizardPage
*wxWizardPageSimple::GetNext() const
135 // ----------------------------------------------------------------------------
136 // generic wxWizard implementation
137 // ----------------------------------------------------------------------------
139 void wxWizard::Init()
141 m_posWizard
= wxDefaultPosition
;
142 m_page
= (wxWizardPage
*)NULL
;
143 m_btnPrev
= m_btnNext
= NULL
;
147 bool wxWizard::Create(wxWindow
*parent
,
149 const wxString
& title
,
150 const wxBitmap
& bitmap
,
156 // just create the dialog itself here, the controls will be created in
157 // DoCreateControls() called later when we know our final size
158 m_page
= (wxWizardPage
*)NULL
;
159 m_btnPrev
= m_btnNext
= NULL
;
162 return wxDialog::Create(parent
, id
, title
, pos
);
165 void wxWizard::DoCreateControls()
167 // do nothing if the controls were already created
171 // constants defining the dialog layout
172 // ------------------------------------
174 // these constants define the position of the upper left corner of the
175 // bitmap or the page in the wizard
176 static const int X_MARGIN
= 10;
177 static const int Y_MARGIN
= 10;
179 // margin between the bitmap and the panel
180 static const int BITMAP_X_MARGIN
= 15;
182 // margin between the bitmap and the static line
183 static const int BITMAP_Y_MARGIN
= 15;
185 // margin between the static line and the buttons
186 static const int SEPARATOR_LINE_MARGIN
= 15;
188 // margin between "Next >" and "Cancel" buttons
189 static const int BUTTON_MARGIN
= 10;
191 // margin between Back and Next buttons
193 static const int BACKNEXT_MARGIN
= 10;
195 static const int BACKNEXT_MARGIN
= 0;
198 // default width and height of the page
199 static const int DEFAULT_PAGE_WIDTH
= 270;
200 static const int DEFAULT_PAGE_HEIGHT
= 290;
205 wxSize sizeBtn
= wxButton::GetDefaultSize();
207 // the global dialog layout is: a row of buttons at the bottom (aligned to
208 // the right), the static line above them, the bitmap (if any) on the left
209 // of the upper part of the dialog and the panel in the remaining space
216 m_statbmp
= new wxStaticBitmap(this, -1, m_bitmap
, wxPoint(m_x
, m_y
));
218 m_x
+= m_bitmap
.GetWidth() + BITMAP_X_MARGIN
;
220 defaultHeight
= m_bitmap
.GetHeight();
224 m_statbmp
= (wxStaticBitmap
*)NULL
;
226 defaultHeight
= DEFAULT_PAGE_HEIGHT
;
229 // use default size if none given and also make sure that the dialog is
230 // not less than the default size
231 m_height
= m_sizePage
.y
== -1 ? defaultHeight
: m_sizePage
.y
;
232 m_width
= m_sizePage
.x
== -1 ? DEFAULT_PAGE_WIDTH
: m_sizePage
.x
;
233 if ( m_height
< defaultHeight
)
234 m_height
= defaultHeight
;
235 if ( m_width
< DEFAULT_PAGE_WIDTH
)
236 m_width
= DEFAULT_PAGE_WIDTH
;
239 int y
= m_y
+ m_height
+ BITMAP_Y_MARGIN
;
242 (void)new wxStaticLine(this, -1, wxPoint(x
, y
),
243 wxSize(m_x
+ m_width
- x
, 2));
244 #endif // wxUSE_STATLINE
246 x
= m_x
+ m_width
- 3*sizeBtn
.x
- BUTTON_MARGIN
- BACKNEXT_MARGIN
;
247 y
+= SEPARATOR_LINE_MARGIN
;
249 if (GetExtraStyle() & wxWIZARD_EX_HELPBUTTON
)
254 (void)new wxButton(this, wxID_HELP
, _("&Help"), wxPoint(x
, y
), sizeBtn
);
259 m_btnPrev
= new wxButton(this, wxID_BACKWARD
, _("< &Back"), wxPoint(x
, y
), sizeBtn
);
262 x
+= BACKNEXT_MARGIN
;
264 m_btnNext
= new wxButton(this, wxID_FORWARD
, _("&Next >"), wxPoint(x
, y
), sizeBtn
);
266 x
+= sizeBtn
.x
+ BUTTON_MARGIN
;
267 (void)new wxButton(this, wxID_CANCEL
, _("&Cancel"), wxPoint(x
, y
), sizeBtn
);
269 // position and size the dialog
270 // ----------------------------
272 SetClientSize(m_x
+ m_width
+ X_MARGIN
,
273 m_y
+ m_height
+ BITMAP_Y_MARGIN
+
274 SEPARATOR_LINE_MARGIN
+ sizeBtn
.y
+ Y_MARGIN
);
276 if ( m_posWizard
== wxDefaultPosition
)
282 void wxWizard::SetPageSize(const wxSize
& size
)
284 // otherwise it will have no effect now as it's too late...
285 wxASSERT_MSG( !WasCreated(), _T("should be called before RunWizard()!") );
290 void wxWizard::FitToPage(const wxWizardPage
*page
)
292 // otherwise it will have no effect now as it's too late...
293 wxASSERT_MSG( !WasCreated(), _T("should be called before RunWizard()!") );
298 wxSize size
= page
->GetBestSize();
300 if ( size
.x
> sizeMax
.x
)
303 if ( size
.y
> sizeMax
.y
)
306 page
= page
->GetNext();
309 if ( sizeMax
.x
> m_sizePage
.x
)
310 m_sizePage
.x
= sizeMax
.x
;
312 if ( sizeMax
.y
> m_sizePage
.y
)
313 m_sizePage
.y
= sizeMax
.y
;
316 bool wxWizard::ShowPage(wxWizardPage
*page
, bool goingForward
)
318 wxASSERT_MSG( page
!= m_page
, wxT("this is useless") );
320 // we'll use this to decide whether we have to change the label of this
321 // button or not (initially the label is "Next")
322 bool btnLabelWasNext
= TRUE
;
324 // Modified 10-20-2001 Robert Cavanaugh.
325 // Fixed bug for displaying a new bitmap
326 // in each *consecutive* page
328 // flag to indicate if this page uses a new bitmap
329 bool bmpIsDefault
= TRUE
;
331 // use these labels to determine if we need to change the bitmap
333 wxBitmap bmpPrev
, bmpCur
;
335 // check for previous page
338 // send the event to the old page
339 wxWizardEvent
event(wxEVT_WIZARD_PAGE_CHANGING
, GetId(), goingForward
, m_page
);
340 if ( m_page
->GetEventHandler()->ProcessEvent(event
) &&
343 // vetoed by the page
349 btnLabelWasNext
= HasNextPage(m_page
);
351 // Get the bitmap of the previous page (if it exists)
352 if ( m_page
->GetBitmap().Ok() )
354 bmpPrev
= m_page
->GetBitmap();
364 // terminate successfully
368 wxWizardEvent
event(wxEVT_WIZARD_FINISHED
, GetId(),FALSE
, 0);
369 (void)GetEventHandler()->ProcessEvent(event
);
374 // position and show the new page
375 (void)m_page
->TransferDataToWindow();
376 m_page
->SetSize(m_x
, m_y
, m_width
, m_height
);
378 // check if bitmap needs to be updated
379 // update default flag as well
380 if ( m_page
->GetBitmap().Ok() )
382 bmpCur
= m_page
->GetBitmap();
383 bmpIsDefault
= FALSE
;
386 // change the bitmap if:
387 // 1) a default bitmap was selected in constructor
388 // 2) this page was constructed with a bitmap
389 // 3) this bitmap is not the previous bitmap
390 if ( m_statbmp
&& (bmpCur
!= bmpPrev
) )
396 bmp
= m_page
->GetBitmap();
397 m_statbmp
->SetBitmap(bmp
);
400 // and update the buttons state
401 m_btnPrev
->Enable(HasPrevPage(m_page
));
403 bool hasNext
= HasNextPage(m_page
);
404 if ( btnLabelWasNext
!= hasNext
)
408 m_btnNext
->SetLabel(_("&Finish"));
410 m_btnNext
->SetLabel(_("&Next >"));
412 // nothing to do: the label was already correct
414 // send the change event to the new page now
415 wxWizardEvent
event(wxEVT_WIZARD_PAGE_CHANGED
, GetId(), goingForward
, m_page
);
416 (void)m_page
->GetEventHandler()->ProcessEvent(event
);
418 // and finally show it
425 bool wxWizard::RunWizard(wxWizardPage
*firstPage
)
427 wxCHECK_MSG( firstPage
, FALSE
, wxT("can't run empty wizard") );
431 // can't return FALSE here because there is no old page
432 (void)ShowPage(firstPage
, TRUE
/* forward */);
434 return ShowModal() == wxID_OK
;
437 wxWizardPage
*wxWizard::GetCurrentPage() const
442 wxSize
wxWizard::GetPageSize() const
444 // make sure that the controls are created because otherwise m_width and
445 // m_height would be both still -1
446 wxConstCast(this, wxWizard
)->DoCreateControls();
448 return wxSize(m_width
, m_height
);
451 void wxWizard::OnCancel(wxCommandEvent
& WXUNUSED(event
))
453 // this function probably can never be called when we don't have an active
454 // page, but a small extra check won't hurt
455 wxWindow
*win
= m_page
? (wxWindow
*)m_page
: (wxWindow
*)this;
457 wxWizardEvent
event(wxEVT_WIZARD_CANCEL
, GetId(), FALSE
, m_page
);
458 if ( !win
->GetEventHandler()->ProcessEvent(event
) || event
.IsAllowed() )
460 // no objections - close the dialog
461 EndModal(wxID_CANCEL
);
463 //else: request to Cancel ignored
466 void wxWizard::OnBackOrNext(wxCommandEvent
& event
)
468 wxASSERT_MSG( (event
.GetEventObject() == m_btnNext
) ||
469 (event
.GetEventObject() == m_btnPrev
),
470 wxT("unknown button") );
472 // ask the current page first: notice that we do it before calling
473 // GetNext/Prev() because the data transfered from the controls of the page
474 // may change the value returned by these methods
475 if ( m_page
&& !m_page
->TransferDataFromWindow() )
477 // the page data is incorrect, don't do anything
481 bool forward
= event
.GetEventObject() == m_btnNext
;
486 page
= m_page
->GetNext();
490 page
= m_page
->GetPrev();
492 wxASSERT_MSG( page
, wxT("\"<Back\" button should have been disabled") );
495 // just pass to the new page (or may be not - but we don't care here)
496 (void)ShowPage(page
, forward
);
499 void wxWizard::OnHelp(wxCommandEvent
& WXUNUSED(event
))
501 // this function probably can never be called when we don't have an active
502 // page, but a small extra check won't hurt
505 // Create and send the help event to the specific page handler
506 // event data contains the active page so that context-sensitive
508 wxWizardEvent
eventHelp(wxEVT_WIZARD_HELP
, GetId(), TRUE
, m_page
);
509 (void)m_page
->GetEventHandler()->ProcessEvent(eventHelp
);
513 void wxWizard::OnWizEvent(wxWizardEvent
& event
)
515 // the dialogs have wxWS_EX_BLOCK_EVENTS style on by default but we want to
516 // propagate wxEVT_WIZARD_XXX to the parent (if any), so do it manually
517 if ( !(GetExtraStyle() & wxWS_EX_BLOCK_EVENTS
) )
519 // the event will be propagated anyhow
523 wxWindow
*parent
= GetParent();
525 if ( !parent
|| !parent
->GetEventHandler()->ProcessEvent(event
) )
531 // ----------------------------------------------------------------------------
532 // our public interface
533 // ----------------------------------------------------------------------------
535 #ifdef WXWIN_COMPATIBILITY_2_2
538 wxWizard
*wxWizardBase::Create(wxWindow
*parent
,
540 const wxString
& title
,
541 const wxBitmap
& bitmap
,
543 const wxSize
& WXUNUSED(size
))
545 return new wxWizard(parent
, id
, title
, bitmap
, pos
);
548 #endif // WXWIN_COMPATIBILITY_2_2
550 // ----------------------------------------------------------------------------
552 // ----------------------------------------------------------------------------
554 wxWizardEvent::wxWizardEvent(wxEventType type
, int id
, bool direction
, wxWizardPage
* page
)
555 : wxNotifyEvent(type
, id
)
557 // Modified 10-20-2001 Robert Cavanaugh
558 // add the active page to the event data
559 m_direction
= direction
;
563 #endif // wxUSE_WIZARDDLG