1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: generic/wizard.cpp
3 // Purpose: generic implementation of wxWizard class
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 1999 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows license
10 ///////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
21 #pragma implementation ".h"
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
34 #include "wx/dynarray.h"
36 #include "wx/statbmp.h"
37 #include "wx/button.h"
40 #include "wx/statline.h"
42 #include "wx/wizard.h"
44 // ----------------------------------------------------------------------------
46 // ----------------------------------------------------------------------------
48 WX_DEFINE_ARRAY(wxPanel
*, wxArrayPages
);
50 // ----------------------------------------------------------------------------
51 // event tables and such
52 // ----------------------------------------------------------------------------
54 DEFINE_EVENT_TYPE(wxEVT_WIZARD_PAGE_CHANGED
)
55 DEFINE_EVENT_TYPE(wxEVT_WIZARD_PAGE_CHANGING
)
56 DEFINE_EVENT_TYPE(wxEVT_WIZARD_CANCEL
)
58 BEGIN_EVENT_TABLE(wxWizard
, wxDialog
)
59 EVT_BUTTON(wxID_CANCEL
, wxWizard::OnCancel
)
60 EVT_BUTTON(-1, wxWizard::OnBackOrNext
)
63 IMPLEMENT_DYNAMIC_CLASS(wxWizard
, wxDialog
)
64 IMPLEMENT_ABSTRACT_CLASS(wxWizardPage
, wxPanel
)
65 IMPLEMENT_DYNAMIC_CLASS(wxWizardPageSimple
, wxWizardPage
)
66 IMPLEMENT_DYNAMIC_CLASS(wxWizardEvent
, wxNotifyEvent
)
68 // ============================================================================
70 // ============================================================================
72 // ----------------------------------------------------------------------------
74 // ----------------------------------------------------------------------------
76 wxWizardPage::wxWizardPage(wxWizard
*parent
, const wxBitmap
& bitmap
)
77 : wxPanel(parent
), m_bitmap(bitmap
)
79 // initially the page is hidden, it's shown only when it becomes current
83 // ----------------------------------------------------------------------------
85 // ----------------------------------------------------------------------------
87 wxWizardPage
*wxWizardPageSimple::GetPrev() const
92 wxWizardPage
*wxWizardPageSimple::GetNext() const
96 // ----------------------------------------------------------------------------
97 // generic wxWizard implementation
98 // ----------------------------------------------------------------------------
100 void wxWizard::Init()
102 m_posWizard
= wxDefaultPosition
;
103 m_page
= (wxWizardPage
*)NULL
;
104 m_btnPrev
= m_btnNext
= NULL
;
108 bool wxWizard::Create(wxWindow
*parent
,
110 const wxString
& title
,
111 const wxBitmap
& bitmap
,
116 // just create the dialog itself here, the controls will be created in
117 // DoCreateControls() called later when we know our final size
118 m_page
= (wxWizardPage
*)NULL
;
119 m_btnPrev
= m_btnNext
= NULL
;
122 return wxDialog::Create(parent
, id
, title
, pos
);
125 void wxWizard::DoCreateControls()
127 // do nothing if the controls were already created
131 // constants defining the dialog layout
132 // ------------------------------------
134 // these constants define the position of the upper left corner of the
135 // bitmap or the page in the wizard
136 static const int X_MARGIN
= 10;
137 static const int Y_MARGIN
= 10;
139 // margin between the bitmap and the panel
140 static const int BITMAP_X_MARGIN
= 15;
142 // margin between the bitmap and the static line
143 static const int BITMAP_Y_MARGIN
= 15;
145 // margin between the static line and the buttons
146 static const int SEPARATOR_LINE_MARGIN
= 15;
148 // margin between "Next >" and "Cancel" buttons
149 static const int BUTTON_MARGIN
= 10;
151 // default width and height of the page
152 static const int DEFAULT_PAGE_WIDTH
= 270;
153 static const int DEFAULT_PAGE_HEIGHT
= 290;
158 wxSize sizeBtn
= wxButton::GetDefaultSize();
160 // the global dialog layout is: a row of buttons at the bottom (aligned to
161 // the right), the static line above them, the bitmap (if any) on the left
162 // of the upper part of the dialog and the panel in the remaining space
169 m_statbmp
= new wxStaticBitmap(this, -1, m_bitmap
, wxPoint(m_x
, m_y
));
171 m_x
+= m_bitmap
.GetWidth() + BITMAP_X_MARGIN
;
173 defaultHeight
= m_bitmap
.GetHeight();
177 m_statbmp
= (wxStaticBitmap
*)NULL
;
179 defaultHeight
= DEFAULT_PAGE_HEIGHT
;
182 // use default size if none given and also make sure that the dialog is
183 // not less than the default size
184 m_height
= m_sizePage
.y
== -1 ? defaultHeight
: m_sizePage
.y
;
185 m_width
= m_sizePage
.x
== -1 ? DEFAULT_PAGE_WIDTH
: m_sizePage
.x
;
186 if ( m_height
< defaultHeight
)
187 m_height
= defaultHeight
;
188 if ( m_width
< DEFAULT_PAGE_WIDTH
)
189 m_width
= DEFAULT_PAGE_WIDTH
;
192 int y
= m_y
+ m_height
+ BITMAP_Y_MARGIN
;
195 (void)new wxStaticLine(this, -1, wxPoint(x
, y
),
196 wxSize(m_x
+ m_width
- x
, 2));
197 #endif // wxUSE_STATLINE
199 x
= m_x
+ m_width
- 3*sizeBtn
.x
- BUTTON_MARGIN
;
200 y
+= SEPARATOR_LINE_MARGIN
;
202 if (GetExtraStyle() & wxWIZARD_EX_HELPBUTTON
)
207 (void)new wxButton(this, wxID_HELP
, _("&Help"), wxPoint(x
, y
), sizeBtn
);
212 m_btnPrev
= new wxButton(this, wxID_BACKWARD
, _("< &Back"), wxPoint(x
, y
), sizeBtn
);
215 m_btnNext
= new wxButton(this, wxID_FORWARD
, _("&Next >"), wxPoint(x
, y
), sizeBtn
);
217 x
+= sizeBtn
.x
+ BUTTON_MARGIN
;
218 (void)new wxButton(this, wxID_CANCEL
, _("&Cancel"), wxPoint(x
, y
), sizeBtn
);
220 // position and size the dialog
221 // ----------------------------
223 SetClientSize(m_x
+ m_width
+ X_MARGIN
,
224 m_y
+ m_height
+ BITMAP_Y_MARGIN
+
225 SEPARATOR_LINE_MARGIN
+ sizeBtn
.y
+ Y_MARGIN
);
227 if ( m_posWizard
== wxDefaultPosition
)
233 void wxWizard::SetPageSize(const wxSize
& size
)
235 // otherwise it will have no effect now as it's too late...
236 wxASSERT_MSG( !WasCreated(), _T("should be called before RunWizard()!") );
241 bool wxWizard::ShowPage(wxWizardPage
*page
, bool goingForward
)
243 wxASSERT_MSG( page
!= m_page
, wxT("this is useless") );
245 // we'll use this to decide whether we have to change the label of this
246 // button or not (initially the label is "Next")
247 bool btnLabelWasNext
= TRUE
;
249 // and this tells us whether we already had the default bitmap before
254 // send the event to the old page
255 wxWizardEvent
event(wxEVT_WIZARD_PAGE_CHANGING
, GetId(), goingForward
);
256 if ( m_page
->GetEventHandler()->ProcessEvent(event
) &&
259 // vetoed by the page
265 btnLabelWasNext
= m_page
->GetNext() != (wxWizardPage
*)NULL
;
266 bmpWasDefault
= !m_page
->GetBitmap().Ok();
268 else // no previous page
270 // always set the bitmap
280 // terminate successfully
286 // send the event to the new page now
287 wxWizardEvent
event(wxEVT_WIZARD_PAGE_CHANGED
, GetId(), goingForward
);
288 (void)m_page
->GetEventHandler()->ProcessEvent(event
);
290 // position and show the new page
291 (void)m_page
->TransferDataToWindow();
292 m_page
->SetSize(m_x
, m_y
, m_width
, m_height
);
295 // change the bitmap if necessary (and if we have it at all)
296 int bmpIsDefault
= !m_page
->GetBitmap().Ok();
297 if ( m_statbmp
&& (bmpIsDefault
!= bmpWasDefault
) )
303 bmp
= m_page
->GetBitmap();
304 m_statbmp
->SetBitmap(bmp
);
307 // and update the buttons state
308 m_btnPrev
->Enable(m_page
->GetPrev() != (wxWizardPage
*)NULL
);
310 bool hasNext
= m_page
->GetNext() != (wxWizardPage
*)NULL
;
311 if ( btnLabelWasNext
!= hasNext
)
315 m_btnNext
->SetLabel(_("&Finish"));
317 m_btnNext
->SetLabel(_("&Next >"));
319 // nothing to do: the label was already correct
324 bool wxWizard::RunWizard(wxWizardPage
*firstPage
)
326 wxCHECK_MSG( firstPage
, FALSE
, wxT("can't run empty wizard") );
330 // can't return FALSE here because there is no old page
331 (void)ShowPage(firstPage
, TRUE
/* forward */);
333 return ShowModal() == wxID_OK
;
336 wxWizardPage
*wxWizard::GetCurrentPage() const
341 wxSize
wxWizard::GetPageSize() const
343 // make sure that the controls are created because otherwise m_width and
344 // m_height would be both still -1
345 wxConstCast(this, wxWizard
)->DoCreateControls();
347 return wxSize(m_width
, m_height
);
350 void wxWizard::OnCancel(wxCommandEvent
& WXUNUSED(event
))
352 // this function probably can never be called when we don't have an active
353 // page, but a small extra check won't hurt
354 wxWindow
*win
= m_page
? (wxWindow
*)m_page
: (wxWindow
*)this;
356 wxWizardEvent
event(wxEVT_WIZARD_CANCEL
, GetId());
357 if ( !win
->GetEventHandler()->ProcessEvent(event
) || event
.IsAllowed() )
359 // no objections - close the dialog
360 EndModal(wxID_CANCEL
);
362 //else: request to Cancel ignored
365 void wxWizard::OnBackOrNext(wxCommandEvent
& event
)
367 wxASSERT_MSG( (event
.GetEventObject() == m_btnNext
) ||
368 (event
.GetEventObject() == m_btnPrev
),
369 wxT("unknown button") );
371 // ask the current page first: notice that we do it before calling
372 // GetNext/Prev() because the data transfered from the controls of the page
373 // may change the value returned by these methods
374 if ( m_page
&& !m_page
->TransferDataFromWindow() )
376 // the page data is incorrect, don't do anything
380 bool forward
= event
.GetEventObject() == m_btnNext
;
385 page
= m_page
->GetNext();
389 page
= m_page
->GetPrev();
391 wxASSERT_MSG( page
, wxT("\"<Back\" button should have been disabled") );
394 // just pass to the new page (or may be not - but we don't care here)
395 (void)ShowPage(page
, forward
);
398 // ----------------------------------------------------------------------------
399 // our public interface
400 // ----------------------------------------------------------------------------
403 wxWizard
*wxWizardBase::Create(wxWindow
*parent
,
405 const wxString
& title
,
406 const wxBitmap
& bitmap
,
408 const wxSize
& WXUNUSED(size
))
410 return new wxWizard(parent
, id
, title
, bitmap
, pos
);
413 // ----------------------------------------------------------------------------
415 // ----------------------------------------------------------------------------
417 wxWizardEvent::wxWizardEvent(wxEventType type
, int id
, bool direction
)
418 : wxNotifyEvent(type
, id
)
420 m_direction
= direction
;
423 #endif // wxUSE_WIZARDDLG