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