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