]> git.saurik.com Git - wxWidgets.git/blame - samples/wizard/wizard.cpp
Add tests of other hatched brushes to the drawing sample.
[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
9a83f860 131 m_checkbox = new wxCheckBox(this, wxID_ANY, wxT("&Check me"));
226e5774 132
07f20d9a
VZ
133 wxBoxSizer *mainSizer = new wxBoxSizer(wxVERTICAL);
134 mainSizer->Add(
71572a74 135 new wxStaticText(this, wxID_ANY,
9a83f860
VZ
136 wxT("You need to check the checkbox\n")
137 wxT("below before going to the next page\n")),
07f20d9a
VZ
138 0,
139 wxALL,
140 5
141 );
142
143 mainSizer->Add(
144 m_checkbox,
145 0, // No stretching
146 wxALL,
147 5 // Border
148 );
92c01615 149 SetSizerAndFit(mainSizer);
66cd017c
VZ
150 }
151
152 virtual bool TransferDataFromWindow()
153 {
74b31181 154 if ( !m_checkbox->GetValue() )
66cd017c 155 {
9a83f860 156 wxMessageBox(wxT("Check the checkbox first!"), wxT("No way"),
b87654f3 157 wxICON_WARNING | wxOK, this);
66cd017c 158
71572a74 159 return false;
66cd017c
VZ
160 }
161
71572a74 162 return true;
66cd017c
VZ
163 }
164
165private:
166 wxCheckBox *m_checkbox;
167};
168
74b31181
VZ
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.
172class wxRadioboxPage : public wxWizardPageSimple
173{
174public:
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
dc1efb1d
JS
184 // static wxString choices[] = { "forward", "backward", "both", "neither" };
185 // The above syntax can cause an internal compiler error with gcc.
186 wxString choices[4];
9a83f860
VZ
187 choices[0] = wxT("forward");
188 choices[1] = wxT("backward");
189 choices[2] = wxT("both");
190 choices[3] = wxT("neither");
74b31181 191
9a83f860 192 m_radio = new wxRadioBox(this, wxID_ANY, wxT("Allow to proceed:"),
07f20d9a 193 wxDefaultPosition, wxDefaultSize,
74b31181
VZ
194 WXSIZEOF(choices), choices,
195 1, wxRA_SPECIFY_COLS);
196 m_radio->SetSelection(Both);
226e5774 197
07f20d9a
VZ
198 wxBoxSizer *mainSizer = new wxBoxSizer(wxVERTICAL);
199 mainSizer->Add(
200 m_radio,
201 0, // No stretching
202 wxALL,
203 5 // Border
204 );
226e5774 205
92c01615 206 SetSizerAndFit(mainSizer);
74b31181
VZ
207 }
208
209 // wizard event handlers
210 void OnWizardCancel(wxWizardEvent& event)
211 {
9a83f860 212 if ( wxMessageBox(wxT("Do you really want to cancel?"), wxT("Question"),
74b31181
VZ
213 wxICON_QUESTION | wxYES_NO, this) != wxYES )
214 {
215 // not confirmed
216 event.Veto();
217 }
218 }
219
220 void OnWizardPageChanging(wxWizardEvent& event)
221 {
222 int sel = m_radio->GetSelection();
223
224 if ( sel == Both )
225 return;
226
227 if ( event.GetDirection() && sel == Forward )
228 return;
229
230 if ( !event.GetDirection() && sel == Backward )
231 return;
232
9a83f860 233 wxMessageBox(wxT("You can't go there"), wxT("Not allowed"),
74b31181
VZ
234 wxICON_WARNING | wxOK, this);
235
236 event.Veto();
237 }
238
239private:
240 wxRadioBox *m_radio;
241
242 DECLARE_EVENT_TABLE()
243};
244
436aae91 245// This shows how to dynamically (i.e. during run-time) arrange the page order.
74b31181
VZ
246class wxCheckboxPage : public wxWizardPage
247{
248public:
249 wxCheckboxPage(wxWizard *parent,
250 wxWizardPage *prev,
251 wxWizardPage *next)
252 : wxWizardPage(parent)
253 {
254 m_prev = prev;
255 m_next = next;
226e5774 256
07f20d9a
VZ
257 wxBoxSizer *mainSizer = new wxBoxSizer(wxVERTICAL);
258
259 mainSizer->Add(
9a83f860
VZ
260 new wxStaticText(this, wxID_ANY, wxT("Try checking the box below and\n")
261 wxT("then going back and clearing it")),
07f20d9a
VZ
262 0, // No vertical stretching
263 wxALL,
264 5 // Border width
265 );
74b31181 266
9a83f860 267 m_checkbox = new wxCheckBox(this, wxID_ANY, wxT("&Skip the next page"));
07f20d9a
VZ
268 mainSizer->Add(
269 m_checkbox,
270 0, // No vertical stretching
271 wxALL,
272 5 // Border width
273 );
f2e93537 274
653d7a50 275#if wxUSE_CHECKLISTBOX
f2e93537 276 static const wxChar *aszChoices[] =
9002a61c 277 {
9a83f860
VZ
278 wxT("Zeroth"),
279 wxT("First"),
280 wxT("Second"),
281 wxT("Third"),
282 wxT("Fourth"),
283 wxT("Fifth"),
284 wxT("Sixth"),
285 wxT("Seventh"),
286 wxT("Eighth"),
287 wxT("Nineth")
9002a61c
VZ
288 };
289
290 m_checklistbox = new wxCheckListBox
291 (
292 this,
293 wxID_ANY,
294 wxDefaultPosition,
295 wxSize(100,100),
296 wxArrayString(WXSIZEOF(aszChoices), aszChoices)
297 );
226e5774 298
f2e93537
RR
299 mainSizer->Add(
300 m_checklistbox,
301 0, // No vertical stretching
302 wxALL,
303 5 // Border width
304 );
653d7a50 305#endif // wxUSE_CHECKLISTBOX
226e5774 306
3aa8e4ea
JS
307 wxSize textSize = wxSize(150, 200);
308 if (((wxFrame*) wxTheApp->GetTopWindow())->GetMenuBar()->IsChecked(Wizard_LargeWizard))
309 textSize = wxSize(150, wxGetClientDisplayRect().GetHeight() - 200);
310
311
312 wxTextCtrl* textCtrl = new wxTextCtrl(this, wxID_ANY, wxEmptyString, wxDefaultPosition, textSize, wxTE_MULTILINE);
313 mainSizer->Add(textCtrl, 0, wxALL|wxEXPAND, 5);
314
92c01615 315 SetSizerAndFit(mainSizer);
74b31181
VZ
316 }
317
318 // implement wxWizardPage functions
319 virtual wxWizardPage *GetPrev() const { return m_prev; }
320 virtual wxWizardPage *GetNext() const
321 {
322 return m_checkbox->GetValue() ? m_next->GetNext() : m_next;
323 }
324
325private:
326 wxWizardPage *m_prev,
327 *m_next;
328
329 wxCheckBox *m_checkbox;
653d7a50 330#if wxUSE_CHECKLISTBOX
f2e93537 331 wxCheckListBox *m_checklistbox;
653d7a50 332#endif
74b31181
VZ
333};
334
66cd017c
VZ
335// ============================================================================
336// implementation
337// ============================================================================
338
d93c719a
VZ
339// ----------------------------------------------------------------------------
340// event tables and such
341// ----------------------------------------------------------------------------
342
343BEGIN_EVENT_TABLE(MyFrame, wxFrame)
b3eb133b
WS
344 EVT_MENU(Wizard_Quit, MyFrame::OnQuit)
345 EVT_MENU(Wizard_About, MyFrame::OnAbout)
346 EVT_MENU(Wizard_RunModal, MyFrame::OnRunWizard)
0a089246
VZ
347 EVT_MENU(Wizard_RunNoSizer, MyFrame::OnRunWizardNoSizer)
348 EVT_MENU(Wizard_RunModeless, MyFrame::OnRunWizardModeless)
d93c719a 349
b3eb133b 350 EVT_WIZARD_CANCEL(wxID_ANY, MyFrame::OnWizardCancel)
71572a74 351 EVT_WIZARD_FINISHED(wxID_ANY, MyFrame::OnWizardFinished)
d93c719a
VZ
352END_EVENT_TABLE()
353
74b31181 354BEGIN_EVENT_TABLE(wxRadioboxPage, wxWizardPageSimple)
71572a74
WS
355 EVT_WIZARD_PAGE_CHANGING(wxID_ANY, wxRadioboxPage::OnWizardPageChanging)
356 EVT_WIZARD_CANCEL(wxID_ANY, wxRadioboxPage::OnWizardCancel)
74b31181
VZ
357END_EVENT_TABLE()
358
d93c719a
VZ
359IMPLEMENT_APP(MyApp)
360
66cd017c
VZ
361// ----------------------------------------------------------------------------
362// the application class
363// ----------------------------------------------------------------------------
364
365// `Main program' equivalent: the program execution "starts" here
366bool MyApp::OnInit()
367{
45e6e6f8
VZ
368 if ( !wxApp::OnInit() )
369 return false;
370
9a83f860 371 MyFrame *frame = new MyFrame(wxT("wxWizard Sample"));
d93c719a
VZ
372
373 // and show it (the frames, unlike simple controls, are not shown when
374 // created initially)
71572a74 375 frame->Show(true);
d93c719a
VZ
376
377 // we're done
71572a74 378 return true;
d93c719a
VZ
379}
380
b3eb133b
WS
381// ----------------------------------------------------------------------------
382// MyWizard
383// ----------------------------------------------------------------------------
37f6a080 384
0a089246 385MyWizard::MyWizard(wxFrame *frame, bool useSizer)
b3eb133b 386{
92b929f0
JS
387 SetExtraStyle(wxWIZARD_EX_HELPBUTTON);
388
389 Create(frame,wxID_ANY,wxT("Absolutely Useless Wizard"),
390 wxBitmap(wiztest_xpm),wxDefaultPosition,
391 wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER);
3cb332c1 392 SetIcon(wxICON(sample));
3aa8e4ea
JS
393
394 // Allow the bitmap to be expanded to fit the page height
395 if (frame->GetMenuBar()->IsChecked(Wizard_ExpandBitmap))
396 SetBitmapPlacement(wxWIZARD_VALIGN_CENTRE);
397
398 // Enable scrolling adaptation
399 if (frame->GetMenuBar()->IsChecked(Wizard_LargeWizard))
400 SetLayoutAdaptationMode(wxDIALOG_ADAPTATION_MODE_ENABLED);
401
b3eb133b
WS
402 // a wizard page may be either an object of predefined class
403 m_page1 = new wxWizardPageSimple(this);
404
405 /* wxStaticText *text = */ new wxStaticText(m_page1, wxID_ANY,
9a83f860
VZ
406 wxT("This wizard doesn't help you\nto do anything at all.\n")
407 wxT("\n")
408 wxT("The next pages will present you\nwith more useless controls."),
b3eb133b
WS
409 wxPoint(5,5)
410 );
411
412 // ... or a derived class
413 wxRadioboxPage *page3 = new wxRadioboxPage(this);
414 wxValidationPage *page4 = new wxValidationPage(this);
415
416 // set the page order using a convenience function - could also use
417 // SetNext/Prev directly as below
418 wxWizardPageSimple::Chain(page3, page4);
419
420 // this page is not a wxWizardPageSimple, so we use SetNext/Prev to insert
421 // it into the chain of pages
422 wxCheckboxPage *page2 = new wxCheckboxPage(this, m_page1, page3);
423 m_page1->SetNext(page2);
424 page3->SetPrev(page2);
425
0a089246 426 if ( useSizer )
b3eb133b 427 {
0a089246
VZ
428 // allow the wizard to size itself around the pages
429 GetPageAreaSizer()->Add(m_page1);
b3eb133b
WS
430 }
431}
432
d93c719a
VZ
433// ----------------------------------------------------------------------------
434// MyFrame
435// ----------------------------------------------------------------------------
436
437MyFrame::MyFrame(const wxString& title)
b3eb133b 438 :wxFrame((wxFrame *)NULL, wxID_ANY, title,
d93c719a
VZ
439 wxDefaultPosition, wxSize(250, 150)) // small frame
440{
441 wxMenu *menuFile = new wxMenu;
9a83f860
VZ
442 menuFile->Append(Wizard_RunModal, wxT("&Run wizard modal...\tCtrl-R"));
443 menuFile->Append(Wizard_RunNoSizer, wxT("Run wizard &without sizer..."));
444 menuFile->Append(Wizard_RunModeless, wxT("Run wizard &modeless..."));
d93c719a 445 menuFile->AppendSeparator();
9a83f860 446 menuFile->Append(Wizard_Quit, wxT("E&xit\tAlt-X"), wxT("Quit this program"));
d93c719a 447
3aa8e4ea 448 wxMenu *menuOptions = new wxMenu;
9a83f860
VZ
449 menuOptions->AppendCheckItem(Wizard_LargeWizard, wxT("&Scroll Wizard Pages"));
450 menuOptions->AppendCheckItem(Wizard_ExpandBitmap, wxT("Si&ze Bitmap To Page"));
3aa8e4ea 451
d93c719a 452 wxMenu *helpMenu = new wxMenu;
9a83f860 453 helpMenu->Append(Wizard_About, wxT("&About...\tF1"), wxT("Show about dialog"));
d93c719a
VZ
454
455 // now append the freshly created menu to the menu bar...
456 wxMenuBar *menuBar = new wxMenuBar();
9a83f860
VZ
457 menuBar->Append(menuFile, wxT("&File"));
458 menuBar->Append(menuOptions, wxT("&Options"));
459 menuBar->Append(helpMenu, wxT("&Help"));
66cd017c 460
d93c719a
VZ
461 // ... and attach this menu bar to the frame
462 SetMenuBar(menuBar);
463
464 // also create status bar which we use in OnWizardCancel
960a83cc 465#if wxUSE_STATUSBAR
d93c719a 466 CreateStatusBar();
960a83cc 467#endif // wxUSE_STATUSBAR
d93c719a
VZ
468}
469
470void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
471{
71572a74
WS
472 // true is to force the frame to close
473 Close(true);
d93c719a
VZ
474}
475
476void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
477{
9a83f860
VZ
478 wxMessageBox(wxT("Demo of wxWizard class\n")
479 wxT("(c) 1999, 2000 Vadim Zeitlin"),
480 wxT("About wxWizard sample"), wxOK | wxICON_INFORMATION, this);
d93c719a
VZ
481}
482
0a089246 483void MyFrame::OnRunWizard(wxCommandEvent& WXUNUSED(event))
d93c719a 484{
0a089246
VZ
485 MyWizard wizard(this);
486
487 wizard.RunWizard(wizard.GetFirstPage());
488}
489
490void MyFrame::OnRunWizardNoSizer(wxCommandEvent& WXUNUSED(event))
491{
492 MyWizard wizard(this, false);
66cd017c 493
0a089246
VZ
494 wizard.RunWizard(wizard.GetFirstPage());
495}
496
497void MyFrame::OnRunWizardModeless(wxCommandEvent& WXUNUSED(event))
498{
499 MyWizard *wizard = new MyWizard(this);
500 wizard->ShowPage(wizard->GetFirstPage());
501 wizard->Show(true);
66cd017c 502}
01dba85a 503
8a7dfb14
VZ
504void MyFrame::OnWizardFinished(wxWizardEvent& WXUNUSED(event))
505{
b3eb133b 506 wxMessageBox(wxT("The wizard finished successfully."), wxT("Wizard notification"));
8a7dfb14
VZ
507}
508
d93c719a
VZ
509void MyFrame::OnWizardCancel(wxWizardEvent& WXUNUSED(event))
510{
b3eb133b 511 wxMessageBox(wxT("The wizard was cancelled."), wxT("Wizard notification"));
d93c719a 512}