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