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