]> git.saurik.com Git - wxWidgets.git/blob - samples/wizard/wizard.cpp
Updated date
[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 MyFrame *frame = new MyFrame(_T("wxWizard Sample"));
357
358 // and show it (the frames, unlike simple controls, are not shown when
359 // created initially)
360 frame->Show(true);
361
362 // we're done
363 return true;
364 }
365
366 // ----------------------------------------------------------------------------
367 // MyWizard
368 // ----------------------------------------------------------------------------
369
370 MyWizard::MyWizard(wxFrame *frame, bool useSizer)
371 : wxWizard(frame,wxID_ANY,_T("Absolutely Useless Wizard"),
372 wxBitmap(wiztest_xpm),wxDefaultPosition,
373 wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER)
374 {
375 // a wizard page may be either an object of predefined class
376 m_page1 = new wxWizardPageSimple(this);
377
378 /* wxStaticText *text = */ new wxStaticText(m_page1, wxID_ANY,
379 _T("This wizard doesn't help you\nto do anything at all.\n")
380 _T("\n")
381 _T("The next pages will present you\nwith more useless controls."),
382 wxPoint(5,5)
383 );
384
385 // ... or a derived class
386 wxRadioboxPage *page3 = new wxRadioboxPage(this);
387 wxValidationPage *page4 = new wxValidationPage(this);
388
389 // set the page order using a convenience function - could also use
390 // SetNext/Prev directly as below
391 wxWizardPageSimple::Chain(page3, page4);
392
393 // this page is not a wxWizardPageSimple, so we use SetNext/Prev to insert
394 // it into the chain of pages
395 wxCheckboxPage *page2 = new wxCheckboxPage(this, m_page1, page3);
396 m_page1->SetNext(page2);
397 page3->SetPrev(page2);
398
399 if ( useSizer )
400 {
401 // allow the wizard to size itself around the pages
402 GetPageAreaSizer()->Add(m_page1);
403 }
404 }
405
406 // ----------------------------------------------------------------------------
407 // MyFrame
408 // ----------------------------------------------------------------------------
409
410 MyFrame::MyFrame(const wxString& title)
411 :wxFrame((wxFrame *)NULL, wxID_ANY, title,
412 wxDefaultPosition, wxSize(250, 150)) // small frame
413 {
414 wxMenu *menuFile = new wxMenu;
415 menuFile->Append(Wizard_RunModal, _T("&Run wizard modal...\tCtrl-R"));
416 menuFile->Append(Wizard_RunNoSizer, _T("Run wizard &without sizer..."));
417 menuFile->Append(Wizard_RunModeless, _T("Run wizard &modeless..."));
418 menuFile->AppendSeparator();
419 menuFile->Append(Wizard_Quit, _T("E&xit\tAlt-X"), _T("Quit this program"));
420
421 wxMenu *helpMenu = new wxMenu;
422 helpMenu->Append(Wizard_About, _T("&About...\tF1"), _T("Show about dialog"));
423
424 // now append the freshly created menu to the menu bar...
425 wxMenuBar *menuBar = new wxMenuBar();
426 menuBar->Append(menuFile, _T("&File"));
427 menuBar->Append(helpMenu, _T("&Help"));
428
429 // ... and attach this menu bar to the frame
430 SetMenuBar(menuBar);
431
432 // also create status bar which we use in OnWizardCancel
433 #if wxUSE_STATUSBAR
434 CreateStatusBar();
435 #endif // wxUSE_STATUSBAR
436 }
437
438 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
439 {
440 // true is to force the frame to close
441 Close(true);
442 }
443
444 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
445 {
446 wxMessageBox(_T("Demo of wxWizard class\n")
447 _T("(c) 1999, 2000 Vadim Zeitlin"),
448 _T("About wxWizard sample"), wxOK | wxICON_INFORMATION, this);
449 }
450
451 void MyFrame::OnRunWizard(wxCommandEvent& WXUNUSED(event))
452 {
453 MyWizard wizard(this);
454
455 wizard.RunWizard(wizard.GetFirstPage());
456 }
457
458 void MyFrame::OnRunWizardNoSizer(wxCommandEvent& WXUNUSED(event))
459 {
460 MyWizard wizard(this, false);
461
462 wizard.RunWizard(wizard.GetFirstPage());
463 }
464
465 void MyFrame::OnRunWizardModeless(wxCommandEvent& WXUNUSED(event))
466 {
467 MyWizard *wizard = new MyWizard(this);
468 wizard->ShowPage(wizard->GetFirstPage());
469 wizard->Show(true);
470 }
471
472 void MyFrame::OnWizardFinished(wxWizardEvent& WXUNUSED(event))
473 {
474 wxMessageBox(wxT("The wizard finished successfully."), wxT("Wizard notification"));
475 }
476
477 void MyFrame::OnWizardCancel(wxWizardEvent& WXUNUSED(event))
478 {
479 wxMessageBox(wxT("The wizard was cancelled."), wxT("Wizard notification"));
480 }