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"
32 #include "wx/dynarray.h"
34 #include "wx/statbmp.h"
37 #include "wx/statline.h"
39 #include "wx/wizard.h"
41 // ----------------------------------------------------------------------------
43 // ----------------------------------------------------------------------------
45 WX_DEFINE_ARRAY(wxPanel
*, wxArrayPages
);
47 // ----------------------------------------------------------------------------
48 // event tables and such
49 // ----------------------------------------------------------------------------
51 BEGIN_EVENT_TABLE(wxWizard
, wxDialog
)
52 EVT_BUTTON(wxID_CANCEL
, wxWizard::OnCancel
)
53 EVT_BUTTON(-1, wxWizard::OnBackOrNext
)
56 IMPLEMENT_DYNAMIC_CLASS(wxWizard
, wxDialog
)
57 IMPLEMENT_ABSTRACT_CLASS(wxWizardPage
, wxPanel
)
58 IMPLEMENT_DYNAMIC_CLASS(wxWizardPageSimple
, wxWizardPage
)
59 IMPLEMENT_DYNAMIC_CLASS(wxWizardEvent
, wxNotifyEvent
)
61 // ============================================================================
63 // ============================================================================
65 // ----------------------------------------------------------------------------
67 // ----------------------------------------------------------------------------
69 wxWizardPage::wxWizardPage(wxWizard
*parent
) : wxPanel(parent
)
71 // initially the page is hidden, it's shown only when it becomes current
75 // ----------------------------------------------------------------------------
77 // ----------------------------------------------------------------------------
79 wxWizardPage
*wxWizardPageSimple::GetPrev() const
84 wxWizardPage
*wxWizardPageSimple::GetNext() const
88 // ----------------------------------------------------------------------------
89 // generic wxWizard implementation
90 // ----------------------------------------------------------------------------
92 wxWizard::wxWizard(wxWindow
*parent
,
94 const wxString
& title
,
95 const wxBitmap
& bitmap
,
99 // constants defining the dialog layout
100 // ------------------------------------
102 // these constants define the position of the upper left corner of the
103 // bitmap or the page in the wizard
104 static const int X_MARGIN
= 10;
105 static const int Y_MARGIN
= 10;
107 // margin between the bitmap and the panel
108 static const int BITMAP_X_MARGIN
= 15;
110 // margin between the bitmap and the static line
111 static const int BITMAP_Y_MARGIN
= 15;
113 // margin between the static line and the buttons
114 static const int SEPARATOR_LINE_MARGIN
= 15;
116 // margin between "Next >" and "Cancel" buttons
117 static const int BUTTON_MARGIN
= 10;
119 // default width and height of the page
120 static const int DEFAULT_PAGE_WIDTH
= 270;
121 static const int DEFAULT_PAGE_HEIGHT
= 290;
126 m_page
= (wxWizardPage
*)NULL
;
131 wxSize sizeBtn
= wxButton::GetDefaultSize();
133 (void)wxDialog::Create(parent
, id
, title
, pos
, size
);
135 // the global dialog layout is: a row of buttons at the bottom (aligned to
136 // the right), the static line above them, the bitmap (if any) on the left
137 // of the upper part of the dialog and the panel in the remaining space
142 (void)new wxStaticBitmap(this, -1, bitmap
, wxPoint(m_x
, m_y
));
144 m_x
+= bitmap
.GetWidth() + BITMAP_X_MARGIN
;
145 m_height
= bitmap
.GetHeight();
149 m_height
= DEFAULT_PAGE_HEIGHT
;
152 m_width
= DEFAULT_PAGE_WIDTH
;
155 int y
= m_y
+ m_height
+ BITMAP_Y_MARGIN
;
158 (void)new wxStaticLine(this, -1, wxPoint(x
, y
),
159 wxSize(m_x
+ m_width
- x
, 2));
162 x
= m_x
+ m_width
- 3*sizeBtn
.x
- BUTTON_MARGIN
;
163 y
+= SEPARATOR_LINE_MARGIN
;
164 m_btnPrev
= new wxButton(this, -1, _("< &Back"), wxPoint(x
, y
), sizeBtn
);
167 m_btnNext
= new wxButton(this, -1, _("&Next >"), wxPoint(x
, y
), sizeBtn
);
169 x
+= sizeBtn
.x
+ BUTTON_MARGIN
;
170 (void)new wxButton(this, wxID_CANCEL
, _("Cancel"), wxPoint(x
, y
), sizeBtn
);
172 // position and size the dialog
173 // ----------------------------
175 if ( size
== wxDefaultSize
)
177 SetClientSize(m_x
+ m_width
+ X_MARGIN
,
178 m_y
+ m_height
+ BITMAP_Y_MARGIN
+
179 SEPARATOR_LINE_MARGIN
+ sizeBtn
.y
+ Y_MARGIN
);
182 if ( pos
== wxDefaultPosition
)
188 bool wxWizard::ShowPage(wxWizardPage
*page
, bool goingForward
)
190 wxASSERT_MSG( page
!= m_page
, T("this is useless") );
192 // we'll use this to decide whether we have to change the label of this
193 // button or not (initially the label is "Next")
194 bool btnLabelWasNext
= TRUE
;
198 // ask the current page first
199 if ( !m_page
->TransferDataFromWindow() )
201 // the page data is incorrect
205 // send the event to the old page
206 wxWizardEvent
event(wxEVT_WIZARD_PAGE_CHANGING
, GetId(), goingForward
);
207 if ( m_page
->GetEventHandler()->ProcessEvent(event
) &&
210 // vetoed by the page
216 btnLabelWasNext
= m_page
->GetNext() != (wxWizardPage
*)NULL
;
225 // terminate successfully
231 // send the event to the new page now
232 wxWizardEvent
event(wxEVT_WIZARD_PAGE_CHANGED
, GetId(), goingForward
);
233 (void)m_page
->GetEventHandler()->ProcessEvent(event
);
235 // position and show the new page
236 (void)m_page
->TransferDataToWindow();
237 m_page
->SetSize(m_x
, m_y
, m_width
, m_height
);
240 // and update the buttons state
241 m_btnPrev
->Enable(m_page
->GetPrev() != (wxWizardPage
*)NULL
);
243 if ( btnLabelWasNext
!= (m_page
->GetNext() != (wxWizardPage
*)NULL
) )
246 m_btnNext
->SetLabel(btnLabelWasNext
? _("&Finish") : _("&Next >"));
248 // nothing to do: the label was already correct
253 bool wxWizard::RunWizard(wxWizardPage
*firstPage
)
255 wxCHECK_MSG( firstPage
, FALSE
, T("can't run empty wizard") );
257 // can't return FALSE here because there is no old page
258 (void)ShowPage(firstPage
, TRUE
/* forward */);
260 return ShowModal() == wxID_OK
;
263 wxWizardPage
*wxWizard::GetCurrentPage() const
268 void wxWizard::OnCancel(wxCommandEvent
& WXUNUSED(event
))
270 // this function probably can never be called when we don't have an active
271 // page, but a small extra check won't hurt
272 wxWindow
*win
= m_page
? (wxWindow
*)m_page
: (wxWindow
*)this;
274 wxWizardEvent
event(wxEVT_WIZARD_CANCEL
, GetId());
275 if ( !win
->GetEventHandler()->ProcessEvent(event
) || event
.IsAllowed() )
277 // no objections - close the dialog
278 EndModal(wxID_CANCEL
);
280 //else: request to Cancel ignored
283 void wxWizard::OnBackOrNext(wxCommandEvent
& event
)
285 wxASSERT_MSG( (event
.GetEventObject() == m_btnNext
) ||
286 (event
.GetEventObject() == m_btnPrev
),
287 T("unknown button") );
289 bool forward
= event
.GetEventObject() == m_btnNext
;
294 page
= m_page
->GetNext();
298 page
= m_page
->GetPrev();
300 wxASSERT_MSG( page
, T("\"<Back\" button should have been disabled") );
303 // just pass to the new page (or may be not - but we don't care here)
304 (void)ShowPage(page
, forward
);
307 // ----------------------------------------------------------------------------
308 // our public interface
309 // ----------------------------------------------------------------------------
312 wxWizard
*wxWizardBase::Create(wxWindow
*parent
,
314 const wxString
& title
,
315 const wxBitmap
& bitmap
,
319 return new wxWizard(parent
, id
, title
, bitmap
, pos
, size
);
322 // ----------------------------------------------------------------------------
324 // ----------------------------------------------------------------------------
326 wxWizardEvent::wxWizardEvent(wxEventType type
, int id
, bool direction
)
327 : wxNotifyEvent(type
, id
)
329 m_direction
= direction
;