]> git.saurik.com Git - wxWidgets.git/blame - samples/wizard/wizard.cpp
Sizing fixes for generic control.
[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
574d4d1c 29 #include "wx/frame.h"
07f20d9a
VZ
30 #include "wx/stattext.h"
31 #include "wx/log.h"
32 #include "wx/app.h"
33 #include "wx/checkbox.h"
f2e93537 34 #include "wx/checklst.h"
07f20d9a
VZ
35 #include "wx/msgdlg.h"
36 #include "wx/radiobox.h"
37 #include "wx/menu.h"
38 #include "wx/sizer.h"
66cd017c
VZ
39#endif
40
41#include "wx/wizard.h"
42
77ba7af1
JS
43#include "wiztest.xpm"
44#include "wiztest2.xpm"
b87654f3 45
d93c719a
VZ
46// ----------------------------------------------------------------------------
47// constants
48// ----------------------------------------------------------------------------
49
50// ids for menu items
51enum
52{
b3eb133b
WS
53 Wizard_Quit = wxID_EXIT,
54 Wizard_RunModal = wxID_HIGHEST,
0a089246 55 Wizard_RunNoSizer,
b3eb133b
WS
56 Wizard_RunModeless,
57 Wizard_About = wxID_ABOUT
d93c719a
VZ
58};
59
66cd017c
VZ
60// ----------------------------------------------------------------------------
61// private classes
62// ----------------------------------------------------------------------------
63
64// Define a new application type, each program should derive a class from wxApp
65class MyApp : public wxApp
66{
67public:
68 // override base class virtuals
69 virtual bool OnInit();
70};
71
d93c719a
VZ
72class MyFrame : public wxFrame
73{
74public:
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);
0a089246
VZ
82 void OnRunWizardNoSizer(wxCommandEvent& event);
83 void OnRunWizardModeless(wxCommandEvent& event);
d93c719a 84 void OnWizardCancel(wxWizardEvent& event);
8a7dfb14 85 void OnWizardFinished(wxWizardEvent& event);
d93c719a
VZ
86
87private:
be5a51fb 88 // any class wishing to process wxWidgets events must use this macro
d93c719a
VZ
89 DECLARE_EVENT_TABLE()
90};
66cd017c 91
b3eb133b
WS
92// ----------------------------------------------------------------------------
93// our wizard
94// ----------------------------------------------------------------------------
95
96class MyWizard : public wxWizard
97{
98public:
0a089246
VZ
99 MyWizard(wxFrame *frame, bool useSizer = true);
100
101 wxWizardPage *GetFirstPage() const { return m_page1; }
b3eb133b
WS
102
103private:
104 wxWizardPageSimple *m_page1;
105};
106
66cd017c
VZ
107// ----------------------------------------------------------------------------
108// some pages for our wizard
109// ----------------------------------------------------------------------------
110
436aae91 111// This shows how to simply control the validity of the user input by just
66cd017c
VZ
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
436aae91 114// too.
d93c719a 115//
436aae91 116// It also shows how to use a different bitmap for one of the pages.
74b31181 117class wxValidationPage : public wxWizardPageSimple
66cd017c
VZ
118{
119public:
74b31181 120 wxValidationPage(wxWizard *parent) : wxWizardPageSimple(parent)
66cd017c 121 {
77ba7af1 122 m_bitmap = wxBitmap(wiztest2_xpm);
d93c719a 123
71572a74 124 m_checkbox = new wxCheckBox(this, wxID_ANY, _T("&Check me"));
226e5774 125
07f20d9a
VZ
126 wxBoxSizer *mainSizer = new wxBoxSizer(wxVERTICAL);
127 mainSizer->Add(
71572a74 128 new wxStaticText(this, wxID_ANY,
07f20d9a
VZ
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);
66cd017c
VZ
144 }
145
146 virtual bool TransferDataFromWindow()
147 {
74b31181 148 if ( !m_checkbox->GetValue() )
66cd017c 149 {
9f84eccd 150 wxMessageBox(_T("Check the checkbox first!"), _T("No way"),
b87654f3 151 wxICON_WARNING | wxOK, this);
66cd017c 152
71572a74 153 return false;
66cd017c
VZ
154 }
155
71572a74 156 return true;
66cd017c
VZ
157 }
158
159private:
160 wxCheckBox *m_checkbox;
161};
162
74b31181
VZ
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.
166class wxRadioboxPage : public wxWizardPageSimple
167{
168public:
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
dc1efb1d
JS
178 // static wxString choices[] = { "forward", "backward", "both", "neither" };
179 // The above syntax can cause an internal compiler error with gcc.
180 wxString choices[4];
9f84eccd
MB
181 choices[0] = _T("forward");
182 choices[1] = _T("backward");
183 choices[2] = _T("both");
184 choices[3] = _T("neither");
74b31181 185
71572a74 186 m_radio = new wxRadioBox(this, wxID_ANY, _T("Allow to proceed:"),
07f20d9a 187 wxDefaultPosition, wxDefaultSize,
74b31181
VZ
188 WXSIZEOF(choices), choices,
189 1, wxRA_SPECIFY_COLS);
190 m_radio->SetSelection(Both);
226e5774 191
07f20d9a
VZ
192 wxBoxSizer *mainSizer = new wxBoxSizer(wxVERTICAL);
193 mainSizer->Add(
194 m_radio,
195 0, // No stretching
196 wxALL,
197 5 // Border
198 );
226e5774 199
07f20d9a
VZ
200 SetSizer(mainSizer);
201 mainSizer->Fit(this);
74b31181
VZ
202 }
203
204 // wizard event handlers
205 void OnWizardCancel(wxWizardEvent& event)
206 {
9f84eccd 207 if ( wxMessageBox(_T("Do you really want to cancel?"), _T("Question"),
74b31181
VZ
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
9f84eccd 228 wxMessageBox(_T("You can't go there"), _T("Not allowed"),
74b31181
VZ
229 wxICON_WARNING | wxOK, this);
230
231 event.Veto();
232 }
233
234private:
235 wxRadioBox *m_radio;
236
237 DECLARE_EVENT_TABLE()
238};
239
436aae91 240// This shows how to dynamically (i.e. during run-time) arrange the page order.
74b31181
VZ
241class wxCheckboxPage : public wxWizardPage
242{
243public:
244 wxCheckboxPage(wxWizard *parent,
245 wxWizardPage *prev,
246 wxWizardPage *next)
247 : wxWizardPage(parent)
248 {
249 m_prev = prev;
250 m_next = next;
226e5774 251
07f20d9a
VZ
252 wxBoxSizer *mainSizer = new wxBoxSizer(wxVERTICAL);
253
254 mainSizer->Add(
71572a74 255 new wxStaticText(this, wxID_ANY, _T("Try checking the box below and\n")
07f20d9a
VZ
256 _T("then going back and clearing it")),
257 0, // No vertical stretching
258 wxALL,
259 5 // Border width
260 );
74b31181 261
71572a74 262 m_checkbox = new wxCheckBox(this, wxID_ANY, _T("&Skip the next page"));
07f20d9a
VZ
263 mainSizer->Add(
264 m_checkbox,
265 0, // No vertical stretching
266 wxALL,
267 5 // Border width
268 );
f2e93537 269
653d7a50 270#if wxUSE_CHECKLISTBOX
f2e93537 271 static const wxChar *aszChoices[] =
9002a61c
VZ
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 );
226e5774 293
f2e93537
RR
294 mainSizer->Add(
295 m_checklistbox,
296 0, // No vertical stretching
297 wxALL,
298 5 // Border width
299 );
653d7a50 300#endif // wxUSE_CHECKLISTBOX
226e5774 301
07f20d9a
VZ
302 SetSizer(mainSizer);
303 mainSizer->Fit(this);
74b31181
VZ
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
313private:
314 wxWizardPage *m_prev,
315 *m_next;
316
317 wxCheckBox *m_checkbox;
653d7a50 318#if wxUSE_CHECKLISTBOX
f2e93537 319 wxCheckListBox *m_checklistbox;
653d7a50 320#endif
74b31181
VZ
321};
322
66cd017c
VZ
323// ============================================================================
324// implementation
325// ============================================================================
326
d93c719a
VZ
327// ----------------------------------------------------------------------------
328// event tables and such
329// ----------------------------------------------------------------------------
330
331BEGIN_EVENT_TABLE(MyFrame, wxFrame)
b3eb133b
WS
332 EVT_MENU(Wizard_Quit, MyFrame::OnQuit)
333 EVT_MENU(Wizard_About, MyFrame::OnAbout)
334 EVT_MENU(Wizard_RunModal, MyFrame::OnRunWizard)
0a089246
VZ
335 EVT_MENU(Wizard_RunNoSizer, MyFrame::OnRunWizardNoSizer)
336 EVT_MENU(Wizard_RunModeless, MyFrame::OnRunWizardModeless)
d93c719a 337
b3eb133b 338 EVT_WIZARD_CANCEL(wxID_ANY, MyFrame::OnWizardCancel)
71572a74 339 EVT_WIZARD_FINISHED(wxID_ANY, MyFrame::OnWizardFinished)
d93c719a
VZ
340END_EVENT_TABLE()
341
74b31181 342BEGIN_EVENT_TABLE(wxRadioboxPage, wxWizardPageSimple)
71572a74
WS
343 EVT_WIZARD_PAGE_CHANGING(wxID_ANY, wxRadioboxPage::OnWizardPageChanging)
344 EVT_WIZARD_CANCEL(wxID_ANY, wxRadioboxPage::OnWizardCancel)
74b31181
VZ
345END_EVENT_TABLE()
346
d93c719a
VZ
347IMPLEMENT_APP(MyApp)
348
66cd017c
VZ
349// ----------------------------------------------------------------------------
350// the application class
351// ----------------------------------------------------------------------------
352
353// `Main program' equivalent: the program execution "starts" here
354bool MyApp::OnInit()
355{
9f84eccd 356 MyFrame *frame = new MyFrame(_T("wxWizard Sample"));
d93c719a
VZ
357
358 // and show it (the frames, unlike simple controls, are not shown when
359 // created initially)
71572a74 360 frame->Show(true);
d93c719a
VZ
361
362 // we're done
71572a74 363 return true;
d93c719a
VZ
364}
365
b3eb133b
WS
366// ----------------------------------------------------------------------------
367// MyWizard
368// ----------------------------------------------------------------------------
37f6a080 369
0a089246
VZ
370MyWizard::MyWizard(wxFrame *frame, bool useSizer)
371 : wxWizard(frame,wxID_ANY,_T("Absolutely Useless Wizard"),
b3eb133b
WS
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
0a089246 399 if ( useSizer )
b3eb133b 400 {
0a089246
VZ
401 // allow the wizard to size itself around the pages
402 GetPageAreaSizer()->Add(m_page1);
b3eb133b
WS
403 }
404}
405
d93c719a
VZ
406// ----------------------------------------------------------------------------
407// MyFrame
408// ----------------------------------------------------------------------------
409
410MyFrame::MyFrame(const wxString& title)
b3eb133b 411 :wxFrame((wxFrame *)NULL, wxID_ANY, title,
d93c719a
VZ
412 wxDefaultPosition, wxSize(250, 150)) // small frame
413{
414 wxMenu *menuFile = new wxMenu;
b3eb133b 415 menuFile->Append(Wizard_RunModal, _T("&Run wizard modal...\tCtrl-R"));
0a089246
VZ
416 menuFile->Append(Wizard_RunNoSizer, _T("Run wizard &without sizer..."));
417 menuFile->Append(Wizard_RunModeless, _T("Run wizard &modeless..."));
d93c719a 418 menuFile->AppendSeparator();
9f84eccd 419 menuFile->Append(Wizard_Quit, _T("E&xit\tAlt-X"), _T("Quit this program"));
d93c719a
VZ
420
421 wxMenu *helpMenu = new wxMenu;
9f84eccd 422 helpMenu->Append(Wizard_About, _T("&About...\tF1"), _T("Show about dialog"));
d93c719a
VZ
423
424 // now append the freshly created menu to the menu bar...
425 wxMenuBar *menuBar = new wxMenuBar();
9f84eccd
MB
426 menuBar->Append(menuFile, _T("&File"));
427 menuBar->Append(helpMenu, _T("&Help"));
66cd017c 428
d93c719a
VZ
429 // ... and attach this menu bar to the frame
430 SetMenuBar(menuBar);
431
432 // also create status bar which we use in OnWizardCancel
960a83cc 433#if wxUSE_STATUSBAR
d93c719a 434 CreateStatusBar();
960a83cc 435#endif // wxUSE_STATUSBAR
d93c719a
VZ
436}
437
438void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
439{
71572a74
WS
440 // true is to force the frame to close
441 Close(true);
d93c719a
VZ
442}
443
444void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
445{
9f84eccd 446 wxMessageBox(_T("Demo of wxWizard class\n")
749bfe9a 447 _T("(c) 1999, 2000 Vadim Zeitlin"),
9f84eccd 448 _T("About wxWizard sample"), wxOK | wxICON_INFORMATION, this);
d93c719a
VZ
449}
450
0a089246 451void MyFrame::OnRunWizard(wxCommandEvent& WXUNUSED(event))
d93c719a 452{
0a089246
VZ
453 MyWizard wizard(this);
454
455 wizard.RunWizard(wizard.GetFirstPage());
456}
457
458void MyFrame::OnRunWizardNoSizer(wxCommandEvent& WXUNUSED(event))
459{
460 MyWizard wizard(this, false);
66cd017c 461
0a089246
VZ
462 wizard.RunWizard(wizard.GetFirstPage());
463}
464
465void MyFrame::OnRunWizardModeless(wxCommandEvent& WXUNUSED(event))
466{
467 MyWizard *wizard = new MyWizard(this);
468 wizard->ShowPage(wizard->GetFirstPage());
469 wizard->Show(true);
66cd017c 470}
01dba85a 471
8a7dfb14
VZ
472void MyFrame::OnWizardFinished(wxWizardEvent& WXUNUSED(event))
473{
b3eb133b 474 wxMessageBox(wxT("The wizard finished successfully."), wxT("Wizard notification"));
8a7dfb14
VZ
475}
476
d93c719a
VZ
477void MyFrame::OnWizardCancel(wxWizardEvent& WXUNUSED(event))
478{
b3eb133b 479 wxMessageBox(wxT("The wizard was cancelled."), wxT("Wizard notification"));
d93c719a 480}