]> git.saurik.com Git - wxWidgets.git/blob - samples/wizard/wizard.cpp
cleanup
[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/frame.h"
30 #include "wx/stattext.h"
31 #include "wx/log.h"
32 #include "wx/app.h"
33 #include "wx/checkbox.h"
34 #include "wx/checklst.h"
35 #include "wx/msgdlg.h"
36 #include "wx/radiobox.h"
37 #include "wx/menu.h"
38 #include "wx/sizer.h"
39 #endif
40
41 #include "wx/wizard.h"
42
43 #include "wiztest.xpm"
44 #include "wiztest2.xpm"
45
46 // ----------------------------------------------------------------------------
47 // constants
48 // ----------------------------------------------------------------------------
49
50 // ids for menu items
51 enum
52 {
53 Wizard_Quit = wxID_EXIT,
54 Wizard_RunModal = wxID_HIGHEST,
55 Wizard_RunNoSizer,
56 Wizard_RunModeless,
57 Wizard_About = wxID_ABOUT
58 };
59
60 // ----------------------------------------------------------------------------
61 // private classes
62 // ----------------------------------------------------------------------------
63
64 // Define a new application type, each program should derive a class from wxApp
65 class MyApp : public wxApp
66 {
67 public:
68 // override base class virtuals
69 virtual bool OnInit();
70 };
71
72 class MyFrame : public wxFrame
73 {
74 public:
75 // ctor(s)
76 MyFrame(const wxString& title);
77
78 // event handlers (these functions should _not_ be virtual)
79 void OnQuit(wxCommandEvent& event);
80 void OnAbout(wxCommandEvent& event);
81 void OnRunWizard(wxCommandEvent& event);
82 void OnRunWizardNoSizer(wxCommandEvent& event);
83 void OnRunWizardModeless(wxCommandEvent& event);
84 void OnWizardCancel(wxWizardEvent& event);
85 void OnWizardFinished(wxWizardEvent& event);
86
87 private:
88 // any class wishing to process wxWidgets events must use this macro
89 DECLARE_EVENT_TABLE()
90 };
91
92 // ----------------------------------------------------------------------------
93 // our wizard
94 // ----------------------------------------------------------------------------
95
96 class MyWizard : public wxWizard
97 {
98 public:
99 MyWizard(wxFrame *frame, bool useSizer = true);
100
101 wxWizardPage *GetFirstPage() const { return m_page1; }
102
103 private:
104 wxWizardPageSimple *m_page1;
105 };
106
107 // ----------------------------------------------------------------------------
108 // some pages for our wizard
109 // ----------------------------------------------------------------------------
110
111 // This shows how to simply control the validity of the user input by just
112 // overriding TransferDataFromWindow() - of course, in a real program, the
113 // check wouldn't be so trivial and the data will be probably saved somewhere
114 // too.
115 //
116 // It also shows how to use a different bitmap for one of the pages.
117 class wxValidationPage : public wxWizardPageSimple
118 {
119 public:
120 wxValidationPage(wxWizard *parent) : wxWizardPageSimple(parent)
121 {
122 m_bitmap = wxBitmap(wiztest2_xpm);
123
124 m_checkbox = new wxCheckBox(this, wxID_ANY, _T("&Check me"));
125
126 wxBoxSizer *mainSizer = new wxBoxSizer(wxVERTICAL);
127 mainSizer->Add(
128 new wxStaticText(this, wxID_ANY,
129 _T("You need to check the checkbox\n")
130 _T("below before going to the next page\n")),
131 0,
132 wxALL,
133 5
134 );
135
136 mainSizer->Add(
137 m_checkbox,
138 0, // No stretching
139 wxALL,
140 5 // Border
141 );
142 SetSizer(mainSizer);
143 mainSizer->Fit(this);
144 }
145
146 virtual bool TransferDataFromWindow()
147 {
148 if ( !m_checkbox->GetValue() )
149 {
150 wxMessageBox(_T("Check the checkbox first!"), _T("No way"),
151 wxICON_WARNING | wxOK, this);
152
153 return false;
154 }
155
156 return true;
157 }
158
159 private:
160 wxCheckBox *m_checkbox;
161 };
162
163 // This is a more complicated example of validity checking: using events we may
164 // allow to return to the previous page, but not to proceed. It also
165 // demonstrates how to intercept [Cancel] button press.
166 class wxRadioboxPage : public wxWizardPageSimple
167 {
168 public:
169 // directions in which we allow the user to proceed from this page
170 enum
171 {
172 Forward, Backward, Both, Neither
173 };
174
175 wxRadioboxPage(wxWizard *parent) : wxWizardPageSimple(parent)
176 {
177 // should correspond to the enum above
178 // static wxString choices[] = { "forward", "backward", "both", "neither" };
179 // The above syntax can cause an internal compiler error with gcc.
180 wxString choices[4];
181 choices[0] = _T("forward");
182 choices[1] = _T("backward");
183 choices[2] = _T("both");
184 choices[3] = _T("neither");
185
186 m_radio = new wxRadioBox(this, wxID_ANY, _T("Allow to proceed:"),
187 wxDefaultPosition, wxDefaultSize,
188 WXSIZEOF(choices), choices,
189 1, wxRA_SPECIFY_COLS);
190 m_radio->SetSelection(Both);
191
192 wxBoxSizer *mainSizer = new wxBoxSizer(wxVERTICAL);
193 mainSizer->Add(
194 m_radio,
195 0, // No stretching
196 wxALL,
197 5 // Border
198 );
199
200 SetSizer(mainSizer);
201 mainSizer->Fit(this);
202 }
203
204 // wizard event handlers
205 void OnWizardCancel(wxWizardEvent& event)
206 {
207 if ( wxMessageBox(_T("Do you really want to cancel?"), _T("Question"),
208 wxICON_QUESTION | wxYES_NO, this) != wxYES )
209 {
210 // not confirmed
211 event.Veto();
212 }
213 }
214
215 void OnWizardPageChanging(wxWizardEvent& event)
216 {
217 int sel = m_radio->GetSelection();
218
219 if ( sel == Both )
220 return;
221
222 if ( event.GetDirection() && sel == Forward )
223 return;
224
225 if ( !event.GetDirection() && sel == Backward )
226 return;
227
228 wxMessageBox(_T("You can't go there"), _T("Not allowed"),
229 wxICON_WARNING | wxOK, this);
230
231 event.Veto();
232 }
233
234 private:
235 wxRadioBox *m_radio;
236
237 DECLARE_EVENT_TABLE()
238 };
239
240 // This shows how to dynamically (i.e. during run-time) arrange the page order.
241 class wxCheckboxPage : public wxWizardPage
242 {
243 public:
244 wxCheckboxPage(wxWizard *parent,
245 wxWizardPage *prev,
246 wxWizardPage *next)
247 : wxWizardPage(parent)
248 {
249 m_prev = prev;
250 m_next = next;
251
252 wxBoxSizer *mainSizer = new wxBoxSizer(wxVERTICAL);
253
254 mainSizer->Add(
255 new wxStaticText(this, wxID_ANY, _T("Try checking the box below and\n")
256 _T("then going back and clearing it")),
257 0, // No vertical stretching
258 wxALL,
259 5 // Border width
260 );
261
262 m_checkbox = new wxCheckBox(this, wxID_ANY, _T("&Skip the next page"));
263 mainSizer->Add(
264 m_checkbox,
265 0, // No vertical stretching
266 wxALL,
267 5 // Border width
268 );
269
270 #if wxUSE_CHECKLISTBOX
271 static const wxChar *aszChoices[] =
272 {
273 _T("Zeroth"),
274 _T("First"),
275 _T("Second"),
276 _T("Third"),
277 _T("Fourth"),
278 _T("Fifth"),
279 _T("Sixth"),
280 _T("Seventh"),
281 _T("Eighth"),
282 _T("Nineth")
283 };
284
285 m_checklistbox = new wxCheckListBox
286 (
287 this,
288 wxID_ANY,
289 wxDefaultPosition,
290 wxSize(100,100),
291 wxArrayString(WXSIZEOF(aszChoices), aszChoices)
292 );
293
294 mainSizer->Add(
295 m_checklistbox,
296 0, // No vertical stretching
297 wxALL,
298 5 // Border width
299 );
300 #endif // wxUSE_CHECKLISTBOX
301
302 SetSizer(mainSizer);
303 mainSizer->Fit(this);
304 }
305
306 // implement wxWizardPage functions
307 virtual wxWizardPage *GetPrev() const { return m_prev; }
308 virtual wxWizardPage *GetNext() const
309 {
310 return m_checkbox->GetValue() ? m_next->GetNext() : m_next;
311 }
312
313 private:
314 wxWizardPage *m_prev,
315 *m_next;
316
317 wxCheckBox *m_checkbox;
318 #if wxUSE_CHECKLISTBOX
319 wxCheckListBox *m_checklistbox;
320 #endif
321 };
322
323 // ============================================================================
324 // implementation
325 // ============================================================================
326
327 // ----------------------------------------------------------------------------
328 // event tables and such
329 // ----------------------------------------------------------------------------
330
331 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
332 EVT_MENU(Wizard_Quit, MyFrame::OnQuit)
333 EVT_MENU(Wizard_About, MyFrame::OnAbout)
334 EVT_MENU(Wizard_RunModal, MyFrame::OnRunWizard)
335 EVT_MENU(Wizard_RunNoSizer, MyFrame::OnRunWizardNoSizer)
336 EVT_MENU(Wizard_RunModeless, MyFrame::OnRunWizardModeless)
337
338 EVT_WIZARD_CANCEL(wxID_ANY, MyFrame::OnWizardCancel)
339 EVT_WIZARD_FINISHED(wxID_ANY, MyFrame::OnWizardFinished)
340 END_EVENT_TABLE()
341
342 BEGIN_EVENT_TABLE(wxRadioboxPage, wxWizardPageSimple)
343 EVT_WIZARD_PAGE_CHANGING(wxID_ANY, wxRadioboxPage::OnWizardPageChanging)
344 EVT_WIZARD_CANCEL(wxID_ANY, wxRadioboxPage::OnWizardCancel)
345 END_EVENT_TABLE()
346
347 IMPLEMENT_APP(MyApp)
348
349 // ----------------------------------------------------------------------------
350 // the application class
351 // ----------------------------------------------------------------------------
352
353 // `Main program' equivalent: the program execution "starts" here
354 bool MyApp::OnInit()
355 {
356 if ( !wxApp::OnInit() )
357 return false;
358
359 MyFrame *frame = new MyFrame(_T("wxWizard Sample"));
360
361 // and show it (the frames, unlike simple controls, are not shown when
362 // created initially)
363 frame->Show(true);
364
365 // we're done
366 return true;
367 }
368
369 // ----------------------------------------------------------------------------
370 // MyWizard
371 // ----------------------------------------------------------------------------
372
373 MyWizard::MyWizard(wxFrame *frame, bool useSizer)
374 : wxWizard(frame,wxID_ANY,_T("Absolutely Useless Wizard"),
375 wxBitmap(wiztest_xpm),wxDefaultPosition,
376 wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER)
377 {
378 // a wizard page may be either an object of predefined class
379 m_page1 = new wxWizardPageSimple(this);
380
381 /* wxStaticText *text = */ new wxStaticText(m_page1, wxID_ANY,
382 _T("This wizard doesn't help you\nto do anything at all.\n")
383 _T("\n")
384 _T("The next pages will present you\nwith more useless controls."),
385 wxPoint(5,5)
386 );
387
388 // ... or a derived class
389 wxRadioboxPage *page3 = new wxRadioboxPage(this);
390 wxValidationPage *page4 = new wxValidationPage(this);
391
392 // set the page order using a convenience function - could also use
393 // SetNext/Prev directly as below
394 wxWizardPageSimple::Chain(page3, page4);
395
396 // this page is not a wxWizardPageSimple, so we use SetNext/Prev to insert
397 // it into the chain of pages
398 wxCheckboxPage *page2 = new wxCheckboxPage(this, m_page1, page3);
399 m_page1->SetNext(page2);
400 page3->SetPrev(page2);
401
402 if ( useSizer )
403 {
404 // allow the wizard to size itself around the pages
405 GetPageAreaSizer()->Add(m_page1);
406 }
407 }
408
409 // ----------------------------------------------------------------------------
410 // MyFrame
411 // ----------------------------------------------------------------------------
412
413 MyFrame::MyFrame(const wxString& title)
414 :wxFrame((wxFrame *)NULL, wxID_ANY, title,
415 wxDefaultPosition, wxSize(250, 150)) // small frame
416 {
417 wxMenu *menuFile = new wxMenu;
418 menuFile->Append(Wizard_RunModal, _T("&Run wizard modal...\tCtrl-R"));
419 menuFile->Append(Wizard_RunNoSizer, _T("Run wizard &without sizer..."));
420 menuFile->Append(Wizard_RunModeless, _T("Run wizard &modeless..."));
421 menuFile->AppendSeparator();
422 menuFile->Append(Wizard_Quit, _T("E&xit\tAlt-X"), _T("Quit this program"));
423
424 wxMenu *helpMenu = new wxMenu;
425 helpMenu->Append(Wizard_About, _T("&About...\tF1"), _T("Show about dialog"));
426
427 // now append the freshly created menu to the menu bar...
428 wxMenuBar *menuBar = new wxMenuBar();
429 menuBar->Append(menuFile, _T("&File"));
430 menuBar->Append(helpMenu, _T("&Help"));
431
432 // ... and attach this menu bar to the frame
433 SetMenuBar(menuBar);
434
435 // also create status bar which we use in OnWizardCancel
436 #if wxUSE_STATUSBAR
437 CreateStatusBar();
438 #endif // wxUSE_STATUSBAR
439 }
440
441 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
442 {
443 // true is to force the frame to close
444 Close(true);
445 }
446
447 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
448 {
449 wxMessageBox(_T("Demo of wxWizard class\n")
450 _T("(c) 1999, 2000 Vadim Zeitlin"),
451 _T("About wxWizard sample"), wxOK | wxICON_INFORMATION, this);
452 }
453
454 void MyFrame::OnRunWizard(wxCommandEvent& WXUNUSED(event))
455 {
456 MyWizard wizard(this);
457
458 wizard.RunWizard(wizard.GetFirstPage());
459 }
460
461 void MyFrame::OnRunWizardNoSizer(wxCommandEvent& WXUNUSED(event))
462 {
463 MyWizard wizard(this, false);
464
465 wizard.RunWizard(wizard.GetFirstPage());
466 }
467
468 void MyFrame::OnRunWizardModeless(wxCommandEvent& WXUNUSED(event))
469 {
470 MyWizard *wizard = new MyWizard(this);
471 wizard->ShowPage(wizard->GetFirstPage());
472 wizard->Show(true);
473 }
474
475 void MyFrame::OnWizardFinished(wxWizardEvent& WXUNUSED(event))
476 {
477 wxMessageBox(wxT("The wizard finished successfully."), wxT("Wizard notification"));
478 }
479
480 void MyFrame::OnWizardCancel(wxWizardEvent& WXUNUSED(event))
481 {
482 wxMessageBox(wxT("The wizard was cancelled."), wxT("Wizard notification"));
483 }