]> git.saurik.com Git - wxWidgets.git/blame - samples/wizard/wizard.cpp
EP_ADD_OPTS defaults to true
[wxWidgets.git] / samples / wizard / wizard.cpp
CommitLineData
66cd017c 1/////////////////////////////////////////////////////////////////////////////
13f5935c 2// Name: wizard.cpp
be5a51fb 3// Purpose: wxWidgets sample demonstrating wxWizard control
66cd017c 4// Author: Vadim Zeitlin
07f20d9a 5// Modified by: Robert Vazan (sizers)
66cd017c
VZ
6// Created: 15.08.99
7// RCS-ID: $Id$
8// Copyright: (c) Vadim Zeitlin
9// Licence: wxWindows licence
10/////////////////////////////////////////////////////////////////////////////
11
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
66cd017c
VZ
20// For compilers that support precompilation, includes "wx/wx.h".
21#include "wx/wxprec.h"
22
23#ifdef __BORLANDC__
24 #pragma hdrstop
25#endif
26
07f20d9a 27// for all others, include the necessary headers
66cd017c 28#ifndef WX_PRECOMP
07f20d9a
VZ
29 #include "wx/stattext.h"
30 #include "wx/log.h"
31 #include "wx/app.h"
32 #include "wx/checkbox.h"
33 #include "wx/msgdlg.h"
34 #include "wx/radiobox.h"
35 #include "wx/menu.h"
36 #include "wx/sizer.h"
66cd017c
VZ
37#endif
38
39#include "wx/wizard.h"
40
b87654f3
VZ
41#ifndef __WXMSW__
42 #include "wiztest.xpm"
d32011d4 43 #include "wiztest2.xpm"
b87654f3
VZ
44#endif
45
d93c719a
VZ
46// ----------------------------------------------------------------------------
47// constants
48// ----------------------------------------------------------------------------
49
50// ids for menu items
51enum
52{
53 Wizard_Quit = 100,
54 Wizard_Run,
55 Wizard_About = 1000
56};
57
66cd017c
VZ
58// ----------------------------------------------------------------------------
59// private classes
60// ----------------------------------------------------------------------------
61
62// Define a new application type, each program should derive a class from wxApp
63class MyApp : public wxApp
64{
65public:
66 // override base class virtuals
67 virtual bool OnInit();
68};
69
d93c719a
VZ
70class MyFrame : public wxFrame
71{
72public:
73 // ctor(s)
74 MyFrame(const wxString& title);
75
76 // event handlers (these functions should _not_ be virtual)
77 void OnQuit(wxCommandEvent& event);
78 void OnAbout(wxCommandEvent& event);
79 void OnRunWizard(wxCommandEvent& event);
80 void OnWizardCancel(wxWizardEvent& event);
8a7dfb14 81 void OnWizardFinished(wxWizardEvent& event);
d93c719a
VZ
82
83private:
be5a51fb 84 // any class wishing to process wxWidgets events must use this macro
d93c719a
VZ
85 DECLARE_EVENT_TABLE()
86};
66cd017c
VZ
87
88// ----------------------------------------------------------------------------
89// some pages for our wizard
90// ----------------------------------------------------------------------------
91
92// this shows how to simply control the validity of the user input by just
93// overriding TransferDataFromWindow() - of course, in a real program, the
94// check wouldn't be so trivial and the data will be probably saved somewhere
95// too
d93c719a
VZ
96//
97// it also shows how to use a different bitmap for one of the pages
74b31181 98class wxValidationPage : public wxWizardPageSimple
66cd017c
VZ
99{
100public:
74b31181 101 wxValidationPage(wxWizard *parent) : wxWizardPageSimple(parent)
66cd017c 102 {
d32011d4 103 m_bitmap = wxBITMAP(wiztest2);
d93c719a 104
71572a74 105 m_checkbox = new wxCheckBox(this, wxID_ANY, _T("&Check me"));
07f20d9a
VZ
106
107 wxBoxSizer *mainSizer = new wxBoxSizer(wxVERTICAL);
108 mainSizer->Add(
71572a74 109 new wxStaticText(this, wxID_ANY,
07f20d9a
VZ
110 _T("You need to check the checkbox\n")
111 _T("below before going to the next page\n")),
112 0,
113 wxALL,
114 5
115 );
116
117 mainSizer->Add(
118 m_checkbox,
119 0, // No stretching
120 wxALL,
121 5 // Border
122 );
123 SetSizer(mainSizer);
124 mainSizer->Fit(this);
66cd017c
VZ
125 }
126
127 virtual bool TransferDataFromWindow()
128 {
74b31181 129 if ( !m_checkbox->GetValue() )
66cd017c 130 {
9f84eccd 131 wxMessageBox(_T("Check the checkbox first!"), _T("No way"),
b87654f3 132 wxICON_WARNING | wxOK, this);
66cd017c 133
71572a74 134 return false;
66cd017c
VZ
135 }
136
71572a74 137 return true;
66cd017c
VZ
138 }
139
140private:
141 wxCheckBox *m_checkbox;
142};
143
74b31181
VZ
144// This is a more complicated example of validity checking: using events we may
145// allow to return to the previous page, but not to proceed. It also
146// demonstrates how to intercept [Cancel] button press.
147class wxRadioboxPage : public wxWizardPageSimple
148{
149public:
150 // directions in which we allow the user to proceed from this page
151 enum
152 {
153 Forward, Backward, Both, Neither
154 };
155
156 wxRadioboxPage(wxWizard *parent) : wxWizardPageSimple(parent)
157 {
158 // should correspond to the enum above
dc1efb1d
JS
159 // static wxString choices[] = { "forward", "backward", "both", "neither" };
160 // The above syntax can cause an internal compiler error with gcc.
161 wxString choices[4];
9f84eccd
MB
162 choices[0] = _T("forward");
163 choices[1] = _T("backward");
164 choices[2] = _T("both");
165 choices[3] = _T("neither");
74b31181 166
71572a74 167 m_radio = new wxRadioBox(this, wxID_ANY, _T("Allow to proceed:"),
07f20d9a 168 wxDefaultPosition, wxDefaultSize,
74b31181
VZ
169 WXSIZEOF(choices), choices,
170 1, wxRA_SPECIFY_COLS);
171 m_radio->SetSelection(Both);
07f20d9a
VZ
172
173 wxBoxSizer *mainSizer = new wxBoxSizer(wxVERTICAL);
174 mainSizer->Add(
175 m_radio,
176 0, // No stretching
177 wxALL,
178 5 // Border
179 );
180 SetSizer(mainSizer);
181 mainSizer->Fit(this);
74b31181
VZ
182 }
183
184 // wizard event handlers
185 void OnWizardCancel(wxWizardEvent& event)
186 {
9f84eccd 187 if ( wxMessageBox(_T("Do you really want to cancel?"), _T("Question"),
74b31181
VZ
188 wxICON_QUESTION | wxYES_NO, this) != wxYES )
189 {
190 // not confirmed
191 event.Veto();
192 }
193 }
194
195 void OnWizardPageChanging(wxWizardEvent& event)
196 {
197 int sel = m_radio->GetSelection();
198
199 if ( sel == Both )
200 return;
201
202 if ( event.GetDirection() && sel == Forward )
203 return;
204
205 if ( !event.GetDirection() && sel == Backward )
206 return;
207
9f84eccd 208 wxMessageBox(_T("You can't go there"), _T("Not allowed"),
74b31181
VZ
209 wxICON_WARNING | wxOK, this);
210
211 event.Veto();
212 }
213
214private:
215 wxRadioBox *m_radio;
216
217 DECLARE_EVENT_TABLE()
218};
219
220// this shows how to dynamically (i.e. during run-time) arrange the page order
221class wxCheckboxPage : public wxWizardPage
222{
223public:
224 wxCheckboxPage(wxWizard *parent,
225 wxWizardPage *prev,
226 wxWizardPage *next)
227 : wxWizardPage(parent)
228 {
229 m_prev = prev;
230 m_next = next;
07f20d9a
VZ
231
232 wxBoxSizer *mainSizer = new wxBoxSizer(wxVERTICAL);
233
234 mainSizer->Add(
71572a74 235 new wxStaticText(this, wxID_ANY, _T("Try checking the box below and\n")
07f20d9a
VZ
236 _T("then going back and clearing it")),
237 0, // No vertical stretching
238 wxALL,
239 5 // Border width
240 );
74b31181 241
71572a74 242 m_checkbox = new wxCheckBox(this, wxID_ANY, _T("&Skip the next page"));
07f20d9a
VZ
243 mainSizer->Add(
244 m_checkbox,
245 0, // No vertical stretching
246 wxALL,
247 5 // Border width
248 );
249
250 SetSizer(mainSizer);
251 mainSizer->Fit(this);
74b31181
VZ
252 }
253
254 // implement wxWizardPage functions
255 virtual wxWizardPage *GetPrev() const { return m_prev; }
256 virtual wxWizardPage *GetNext() const
257 {
258 return m_checkbox->GetValue() ? m_next->GetNext() : m_next;
259 }
260
261private:
262 wxWizardPage *m_prev,
263 *m_next;
264
265 wxCheckBox *m_checkbox;
266};
267
66cd017c
VZ
268// ============================================================================
269// implementation
270// ============================================================================
271
d93c719a
VZ
272// ----------------------------------------------------------------------------
273// event tables and such
274// ----------------------------------------------------------------------------
275
276BEGIN_EVENT_TABLE(MyFrame, wxFrame)
277 EVT_MENU(Wizard_Quit, MyFrame::OnQuit)
278 EVT_MENU(Wizard_About, MyFrame::OnAbout)
279 EVT_MENU(Wizard_Run, MyFrame::OnRunWizard)
280
71572a74
WS
281 EVT_WIZARD_CANCEL(wxID_ANY, MyFrame::OnWizardCancel)
282 EVT_WIZARD_FINISHED(wxID_ANY, MyFrame::OnWizardFinished)
d93c719a
VZ
283END_EVENT_TABLE()
284
74b31181 285BEGIN_EVENT_TABLE(wxRadioboxPage, wxWizardPageSimple)
71572a74
WS
286 EVT_WIZARD_PAGE_CHANGING(wxID_ANY, wxRadioboxPage::OnWizardPageChanging)
287 EVT_WIZARD_CANCEL(wxID_ANY, wxRadioboxPage::OnWizardCancel)
74b31181
VZ
288END_EVENT_TABLE()
289
d93c719a
VZ
290IMPLEMENT_APP(MyApp)
291
66cd017c
VZ
292// ----------------------------------------------------------------------------
293// the application class
294// ----------------------------------------------------------------------------
295
296// `Main program' equivalent: the program execution "starts" here
297bool MyApp::OnInit()
298{
9f84eccd 299 MyFrame *frame = new MyFrame(_T("wxWizard Sample"));
d93c719a
VZ
300
301 // and show it (the frames, unlike simple controls, are not shown when
302 // created initially)
71572a74 303 frame->Show(true);
d93c719a
VZ
304
305 // we're done
71572a74 306 return true;
d93c719a
VZ
307}
308
309// ----------------------------------------------------------------------------
310// MyFrame
311// ----------------------------------------------------------------------------
312
313MyFrame::MyFrame(const wxString& title)
71572a74 314 : wxFrame((wxFrame *)NULL, wxID_ANY, title,
d93c719a
VZ
315 wxDefaultPosition, wxSize(250, 150)) // small frame
316{
317 wxMenu *menuFile = new wxMenu;
9f84eccd 318 menuFile->Append(Wizard_Run, _T("&Run wizard...\tCtrl-R"));
d93c719a 319 menuFile->AppendSeparator();
9f84eccd 320 menuFile->Append(Wizard_Quit, _T("E&xit\tAlt-X"), _T("Quit this program"));
d93c719a
VZ
321
322 wxMenu *helpMenu = new wxMenu;
9f84eccd 323 helpMenu->Append(Wizard_About, _T("&About...\tF1"), _T("Show about dialog"));
d93c719a
VZ
324
325 // now append the freshly created menu to the menu bar...
326 wxMenuBar *menuBar = new wxMenuBar();
9f84eccd
MB
327 menuBar->Append(menuFile, _T("&File"));
328 menuBar->Append(helpMenu, _T("&Help"));
66cd017c 329
d93c719a
VZ
330 // ... and attach this menu bar to the frame
331 SetMenuBar(menuBar);
332
333 // also create status bar which we use in OnWizardCancel
960a83cc 334#if wxUSE_STATUSBAR
d93c719a 335 CreateStatusBar();
960a83cc 336#endif // wxUSE_STATUSBAR
d93c719a
VZ
337}
338
339void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
340{
71572a74
WS
341 // true is to force the frame to close
342 Close(true);
d93c719a
VZ
343}
344
345void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
346{
9f84eccd 347 wxMessageBox(_T("Demo of wxWizard class\n")
749bfe9a 348 _T("(c) 1999, 2000 Vadim Zeitlin"),
9f84eccd 349 _T("About wxWizard sample"), wxOK | wxICON_INFORMATION, this);
d93c719a
VZ
350}
351
352void MyFrame::OnRunWizard(wxCommandEvent& WXUNUSED(event))
353{
71572a74 354 wxWizard *wizard = new wxWizard(this, wxID_ANY,
9f84eccd 355 _T("Absolutely Useless Wizard"),
07f20d9a
VZ
356 wxBITMAP(wiztest),
357 wxDefaultPosition,
358 wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER);
2f6c54eb 359
74b31181
VZ
360 // a wizard page may be either an object of predefined class
361 wxWizardPageSimple *page1 = new wxWizardPageSimple(wizard);
71572a74 362 wxStaticText *text = new wxStaticText(page1, wxID_ANY,
07f20d9a 363 _T("This wizard doesn't help you\nto do anything at all.\n")
9f84eccd 364 _T("\n")
07f20d9a
VZ
365 _T("The next pages will present you\nwith more useless controls."),
366 wxPoint(5,5)
f6bcfd97
BP
367 );
368 wxSize size = text->GetBestSize();
66cd017c 369
74b31181
VZ
370 // ... or a derived class
371 wxRadioboxPage *page3 = new wxRadioboxPage(wizard);
372 wxValidationPage *page4 = new wxValidationPage(wizard);
373
374 // set the page order using a convenience function - could also use
375 // SetNext/Prev directly as below
376 wxWizardPageSimple::Chain(page3, page4);
377
378 // this page is not a wxWizardPageSimple, so we use SetNext/Prev to insert
379 // it into the chain of pages
380 wxCheckboxPage *page2 = new wxCheckboxPage(wizard, page1, page3);
381 page1->SetNext(page2);
382 page3->SetPrev(page2);
66cd017c 383
f6bcfd97 384 wizard->SetPageSize(size);
07f20d9a
VZ
385 wizard->GetPageAreaSizer()->Add(page1);
386
74b31181 387 if ( wizard->RunWizard(page1) )
66cd017c 388 {
9f84eccd 389 wxMessageBox(_T("The wizard successfully completed"), _T("That's all"),
b87654f3 390 wxICON_INFORMATION | wxOK);
66cd017c
VZ
391 }
392
393 wizard->Destroy();
66cd017c 394}
01dba85a 395
8a7dfb14
VZ
396void MyFrame::OnWizardFinished(wxWizardEvent& WXUNUSED(event))
397{
398 wxLogStatus(this, wxT("The wizard finished successfully."));
399}
400
d93c719a
VZ
401void MyFrame::OnWizardCancel(wxWizardEvent& WXUNUSED(event))
402{
4693b20c 403 wxLogStatus(this, wxT("The wizard was cancelled."));
d93c719a 404}