]> git.saurik.com Git - wxWidgets.git/blob - samples/wizard/wizard.cpp
note font dialog change
[wxWidgets.git] / samples / wizard / wizard.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wizard.cpp
3 // Purpose: wxWidgets sample demonstrating wxWizard control
4 // Author: Vadim Zeitlin
5 // Modified by: Robert Vazan (sizers)
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
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
27 // for all others, include the necessary headers
28 #ifndef WX_PRECOMP
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"
37 #endif
38
39 #include "wx/wizard.h"
40
41 #ifndef __WXMSW__
42 #include "wiztest.xpm"
43 #include "wiztest2.xpm"
44 #endif
45
46 // ----------------------------------------------------------------------------
47 // constants
48 // ----------------------------------------------------------------------------
49
50 // ids for menu items
51 enum
52 {
53 Wizard_Quit = 100,
54 Wizard_Run,
55 Wizard_About = 1000
56 };
57
58 // ----------------------------------------------------------------------------
59 // private classes
60 // ----------------------------------------------------------------------------
61
62 // Define a new application type, each program should derive a class from wxApp
63 class MyApp : public wxApp
64 {
65 public:
66 // override base class virtuals
67 virtual bool OnInit();
68 };
69
70 class MyFrame : public wxFrame
71 {
72 public:
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);
81 void OnWizardFinished(wxWizardEvent& event);
82
83 private:
84 // any class wishing to process wxWidgets events must use this macro
85 DECLARE_EVENT_TABLE()
86 };
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
96 //
97 // it also shows how to use a different bitmap for one of the pages
98 class wxValidationPage : public wxWizardPageSimple
99 {
100 public:
101 wxValidationPage(wxWizard *parent) : wxWizardPageSimple(parent)
102 {
103 m_bitmap = wxBITMAP(wiztest2);
104
105 m_checkbox = new wxCheckBox(this, wxID_ANY, _T("&Check me"));
106
107 wxBoxSizer *mainSizer = new wxBoxSizer(wxVERTICAL);
108 mainSizer->Add(
109 new wxStaticText(this, wxID_ANY,
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);
125 }
126
127 virtual bool TransferDataFromWindow()
128 {
129 if ( !m_checkbox->GetValue() )
130 {
131 wxMessageBox(_T("Check the checkbox first!"), _T("No way"),
132 wxICON_WARNING | wxOK, this);
133
134 return false;
135 }
136
137 return true;
138 }
139
140 private:
141 wxCheckBox *m_checkbox;
142 };
143
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.
147 class wxRadioboxPage : public wxWizardPageSimple
148 {
149 public:
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
159 // static wxString choices[] = { "forward", "backward", "both", "neither" };
160 // The above syntax can cause an internal compiler error with gcc.
161 wxString choices[4];
162 choices[0] = _T("forward");
163 choices[1] = _T("backward");
164 choices[2] = _T("both");
165 choices[3] = _T("neither");
166
167 m_radio = new wxRadioBox(this, wxID_ANY, _T("Allow to proceed:"),
168 wxDefaultPosition, wxDefaultSize,
169 WXSIZEOF(choices), choices,
170 1, wxRA_SPECIFY_COLS);
171 m_radio->SetSelection(Both);
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);
182 }
183
184 // wizard event handlers
185 void OnWizardCancel(wxWizardEvent& event)
186 {
187 if ( wxMessageBox(_T("Do you really want to cancel?"), _T("Question"),
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
208 wxMessageBox(_T("You can't go there"), _T("Not allowed"),
209 wxICON_WARNING | wxOK, this);
210
211 event.Veto();
212 }
213
214 private:
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
221 class wxCheckboxPage : public wxWizardPage
222 {
223 public:
224 wxCheckboxPage(wxWizard *parent,
225 wxWizardPage *prev,
226 wxWizardPage *next)
227 : wxWizardPage(parent)
228 {
229 m_prev = prev;
230 m_next = next;
231
232 wxBoxSizer *mainSizer = new wxBoxSizer(wxVERTICAL);
233
234 mainSizer->Add(
235 new wxStaticText(this, wxID_ANY, _T("Try checking the box below and\n")
236 _T("then going back and clearing it")),
237 0, // No vertical stretching
238 wxALL,
239 5 // Border width
240 );
241
242 m_checkbox = new wxCheckBox(this, wxID_ANY, _T("&Skip the next page"));
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);
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
261 private:
262 wxWizardPage *m_prev,
263 *m_next;
264
265 wxCheckBox *m_checkbox;
266 };
267
268 // ============================================================================
269 // implementation
270 // ============================================================================
271
272 // ----------------------------------------------------------------------------
273 // event tables and such
274 // ----------------------------------------------------------------------------
275
276 BEGIN_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
281 EVT_WIZARD_CANCEL(wxID_ANY, MyFrame::OnWizardCancel)
282 EVT_WIZARD_FINISHED(wxID_ANY, MyFrame::OnWizardFinished)
283 END_EVENT_TABLE()
284
285 BEGIN_EVENT_TABLE(wxRadioboxPage, wxWizardPageSimple)
286 EVT_WIZARD_PAGE_CHANGING(wxID_ANY, wxRadioboxPage::OnWizardPageChanging)
287 EVT_WIZARD_CANCEL(wxID_ANY, wxRadioboxPage::OnWizardCancel)
288 END_EVENT_TABLE()
289
290 IMPLEMENT_APP(MyApp)
291
292 // ----------------------------------------------------------------------------
293 // the application class
294 // ----------------------------------------------------------------------------
295
296 // `Main program' equivalent: the program execution "starts" here
297 bool MyApp::OnInit()
298 {
299 MyFrame *frame = new MyFrame(_T("wxWizard Sample"));
300
301 // and show it (the frames, unlike simple controls, are not shown when
302 // created initially)
303 frame->Show(true);
304
305 // we're done
306 return true;
307 }
308
309 // ----------------------------------------------------------------------------
310 // MyFrame
311 // ----------------------------------------------------------------------------
312
313 MyFrame::MyFrame(const wxString& title)
314 : wxFrame((wxFrame *)NULL, wxID_ANY, title,
315 wxDefaultPosition, wxSize(250, 150)) // small frame
316 {
317 wxMenu *menuFile = new wxMenu;
318 menuFile->Append(Wizard_Run, _T("&Run wizard...\tCtrl-R"));
319 menuFile->AppendSeparator();
320 menuFile->Append(Wizard_Quit, _T("E&xit\tAlt-X"), _T("Quit this program"));
321
322 wxMenu *helpMenu = new wxMenu;
323 helpMenu->Append(Wizard_About, _T("&About...\tF1"), _T("Show about dialog"));
324
325 // now append the freshly created menu to the menu bar...
326 wxMenuBar *menuBar = new wxMenuBar();
327 menuBar->Append(menuFile, _T("&File"));
328 menuBar->Append(helpMenu, _T("&Help"));
329
330 // ... and attach this menu bar to the frame
331 SetMenuBar(menuBar);
332
333 // also create status bar which we use in OnWizardCancel
334 #if wxUSE_STATUSBAR
335 CreateStatusBar();
336 #endif // wxUSE_STATUSBAR
337 }
338
339 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
340 {
341 // true is to force the frame to close
342 Close(true);
343 }
344
345 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
346 {
347 wxMessageBox(_T("Demo of wxWizard class\n")
348 _T("© 1999, 2000 Vadim Zeitlin"),
349 _T("About wxWizard sample"), wxOK | wxICON_INFORMATION, this);
350 }
351
352 void MyFrame::OnRunWizard(wxCommandEvent& WXUNUSED(event))
353 {
354 wxWizard *wizard = new wxWizard(this, wxID_ANY,
355 _T("Absolutely Useless Wizard"),
356 wxBITMAP(wiztest),
357 wxDefaultPosition,
358 wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER);
359
360 // a wizard page may be either an object of predefined class
361 wxWizardPageSimple *page1 = new wxWizardPageSimple(wizard);
362 wxStaticText *text = new wxStaticText(page1, wxID_ANY,
363 _T("This wizard doesn't help you\nto do anything at all.\n")
364 _T("\n")
365 _T("The next pages will present you\nwith more useless controls."),
366 wxPoint(5,5)
367 );
368 wxSize size = text->GetBestSize();
369
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);
383
384 wizard->SetPageSize(size);
385 wizard->GetPageAreaSizer()->Add(page1);
386
387 if ( wizard->RunWizard(page1) )
388 {
389 wxMessageBox(_T("The wizard successfully completed"), _T("That's all"),
390 wxICON_INFORMATION | wxOK);
391 }
392
393 wizard->Destroy();
394 }
395
396 void MyFrame::OnWizardFinished(wxWizardEvent& WXUNUSED(event))
397 {
398 wxLogStatus(this, wxT("The wizard finished successfully."));
399 }
400
401 void MyFrame::OnWizardCancel(wxWizardEvent& WXUNUSED(event))
402 {
403 wxLogStatus(this, wxT("The wizard was cancelled."));
404 }