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
, const wxBitmap
& bitmap
)
70 : wxPanel(parent
), m_bitmap(bitmap
)
72 // initially the page is hidden, it's shown only when it becomes current
76 // ----------------------------------------------------------------------------
78 // ----------------------------------------------------------------------------
80 wxWizardPage
*wxWizardPageSimple::GetPrev() const
85 wxWizardPage
*wxWizardPageSimple::GetNext() const
89 // ----------------------------------------------------------------------------
90 // generic wxWizard implementation
91 // ----------------------------------------------------------------------------
93 wxWizard::wxWizard(wxWindow
*parent
,
95 const wxString
& title
,
96 const wxBitmap
& bitmap
,
101 // constants defining the dialog layout
102 // ------------------------------------
104 // these constants define the position of the upper left corner of the
105 // bitmap or the page in the wizard
106 static const int X_MARGIN
= 10;
107 static const int Y_MARGIN
= 10;
109 // margin between the bitmap and the panel
110 static const int BITMAP_X_MARGIN
= 15;
112 // margin between the bitmap and the static line
113 static const int BITMAP_Y_MARGIN
= 15;
115 // margin between the static line and the buttons
116 static const int SEPARATOR_LINE_MARGIN
= 15;
118 // margin between "Next >" and "Cancel" buttons
119 static const int BUTTON_MARGIN
= 10;
121 // default width and height of the page
122 static const int DEFAULT_PAGE_WIDTH
= 270;
123 static const int DEFAULT_PAGE_HEIGHT
= 290;
128 m_page
= (wxWizardPage
*)NULL
;
133 wxSize sizeBtn
= wxButton::GetDefaultSize();
135 (void)wxDialog::Create(parent
, id
, title
, pos
, size
);
137 // the global dialog layout is: a row of buttons at the bottom (aligned to
138 // the right), the static line above them, the bitmap (if any) on the left
139 // of the upper part of the dialog and the panel in the remaining space
144 m_statbmp
= new wxStaticBitmap(this, -1, bitmap
, wxPoint(m_x
, m_y
));
146 m_x
+= bitmap
.GetWidth() + BITMAP_X_MARGIN
;
147 m_height
= bitmap
.GetHeight();
151 m_statbmp
= (wxStaticBitmap
*)NULL
;
153 m_height
= DEFAULT_PAGE_HEIGHT
;
156 m_width
= DEFAULT_PAGE_WIDTH
;
159 int y
= m_y
+ m_height
+ BITMAP_Y_MARGIN
;
162 (void)new wxStaticLine(this, -1, wxPoint(x
, y
),
163 wxSize(m_x
+ m_width
- x
, 2));
166 x
= m_x
+ m_width
- 3*sizeBtn
.x
- BUTTON_MARGIN
;
167 y
+= SEPARATOR_LINE_MARGIN
;
168 m_btnPrev
= new wxButton(this, -1, _("< &Back"), wxPoint(x
, y
), sizeBtn
);
171 m_btnNext
= new wxButton(this, -1, _("&Next >"), wxPoint(x
, y
), sizeBtn
);
173 x
+= sizeBtn
.x
+ BUTTON_MARGIN
;
174 (void)new wxButton(this, wxID_CANCEL
, _("Cancel"), wxPoint(x
, y
), sizeBtn
);
176 // position and size the dialog
177 // ----------------------------
179 if ( size
== wxDefaultSize
)
181 SetClientSize(m_x
+ m_width
+ X_MARGIN
,
182 m_y
+ m_height
+ BITMAP_Y_MARGIN
+
183 SEPARATOR_LINE_MARGIN
+ sizeBtn
.y
+ Y_MARGIN
);
186 if ( pos
== wxDefaultPosition
)
192 bool wxWizard::ShowPage(wxWizardPage
*page
, bool goingForward
)
194 wxASSERT_MSG( page
!= m_page
, wxT("this is useless") );
196 // we'll use this to decide whether we have to change the label of this
197 // button or not (initially the label is "Next")
198 bool btnLabelWasNext
= TRUE
;
200 // and this tells us whether we already had the default bitmap before
201 bool bmpWasDefault
= TRUE
;
205 // ask the current page first
206 if ( !m_page
->TransferDataFromWindow() )
208 // the page data is incorrect
212 // send the event to the old page
213 wxWizardEvent
event(wxEVT_WIZARD_PAGE_CHANGING
, GetId(), goingForward
);
214 if ( m_page
->GetEventHandler()->ProcessEvent(event
) &&
217 // vetoed by the page
223 btnLabelWasNext
= m_page
->GetNext() != (wxWizardPage
*)NULL
;
224 bmpWasDefault
= !m_page
->GetBitmap().Ok();
233 // terminate successfully
239 // send the event to the new page now
240 wxWizardEvent
event(wxEVT_WIZARD_PAGE_CHANGED
, GetId(), goingForward
);
241 (void)m_page
->GetEventHandler()->ProcessEvent(event
);
243 // position and show the new page
244 (void)m_page
->TransferDataToWindow();
245 m_page
->SetSize(m_x
, m_y
, m_width
, m_height
);
248 // change the bitmap if necessary (and if we have it at all)
249 bool bmpIsDefault
= !m_page
->GetBitmap().Ok();
250 if ( m_statbmp
&& (bmpIsDefault
!= bmpWasDefault
) )
256 bmp
= m_page
->GetBitmap();
257 m_statbmp
->SetBitmap(bmp
);
260 // and update the buttons state
261 m_btnPrev
->Enable(m_page
->GetPrev() != (wxWizardPage
*)NULL
);
263 bool hasNext
= m_page
->GetNext() != (wxWizardPage
*)NULL
;
264 if ( btnLabelWasNext
!= hasNext
)
268 m_btnNext
->SetLabel(_("&Finish"));
270 m_btnNext
->SetLabel(_("&Next >"));
272 // nothing to do: the label was already correct
277 bool wxWizard::RunWizard(wxWizardPage
*firstPage
)
279 wxCHECK_MSG( firstPage
, FALSE
, wxT("can't run empty wizard") );
281 // can't return FALSE here because there is no old page
282 (void)ShowPage(firstPage
, TRUE
/* forward */);
284 return ShowModal() == wxID_OK
;
287 wxWizardPage
*wxWizard::GetCurrentPage() const
292 wxSize
wxWizard::GetPageSize() const
294 return wxSize(m_width
, m_height
);
297 void wxWizard::OnCancel(wxCommandEvent
& WXUNUSED(event
))
299 // this function probably can never be called when we don't have an active
300 // page, but a small extra check won't hurt
301 wxWindow
*win
= m_page
? (wxWindow
*)m_page
: (wxWindow
*)this;
303 wxWizardEvent
event(wxEVT_WIZARD_CANCEL
, GetId());
304 if ( !win
->GetEventHandler()->ProcessEvent(event
) || event
.IsAllowed() )
306 // no objections - close the dialog
307 EndModal(wxID_CANCEL
);
309 //else: request to Cancel ignored
312 void wxWizard::OnBackOrNext(wxCommandEvent
& event
)
314 wxASSERT_MSG( (event
.GetEventObject() == m_btnNext
) ||
315 (event
.GetEventObject() == m_btnPrev
),
316 wxT("unknown button") );
318 bool forward
= event
.GetEventObject() == m_btnNext
;
323 page
= m_page
->GetNext();
327 page
= m_page
->GetPrev();
329 wxASSERT_MSG( page
, wxT("\"<Back\" button should have been disabled") );
332 // just pass to the new page (or may be not - but we don't care here)
333 (void)ShowPage(page
, forward
);
336 // ----------------------------------------------------------------------------
337 // our public interface
338 // ----------------------------------------------------------------------------
341 wxWizard
*wxWizardBase::Create(wxWindow
*parent
,
343 const wxString
& title
,
344 const wxBitmap
& bitmap
,
348 return new wxWizard(parent
, id
, title
, bitmap
, pos
, size
);
351 // ----------------------------------------------------------------------------
353 // ----------------------------------------------------------------------------
355 wxWizardEvent::wxWizardEvent(wxEventType type
, int id
, bool direction
)
356 : wxNotifyEvent(type
, id
)
358 m_direction
= direction
;