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