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