Image replacement
[wxWidgets.git] / samples / wizard / wizard.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wizard.cpp
3 // Purpose: wxWidgets sample demonstrating wxWizard control
4 // Author: Vadim Zeitlin
5 // Modified by: Robert Vazan (sizers)
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
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
27 // for all others, include the necessary headers
28 #ifndef WX_PRECOMP
29 #include "wx/stattext.h"
30 #include "wx/log.h"
31 #include "wx/app.h"
32 #include "wx/checkbox.h"
33 #include "wx/checklst.h"
34 #include "wx/msgdlg.h"
35 #include "wx/radiobox.h"
36 #include "wx/menu.h"
37 #include "wx/sizer.h"
38 #endif
39
40 #include "wx/wizard.h"
41
42 #include "wiztest.xpm"
43 #include "wiztest2.xpm"
44
45 // ----------------------------------------------------------------------------
46 // constants
47 // ----------------------------------------------------------------------------
48
49 // ids for menu items
50 enum
51 {
52 Wizard_Quit = 100,
53 Wizard_Run,
54 Wizard_About = 1000
55 };
56
57 // ----------------------------------------------------------------------------
58 // private classes
59 // ----------------------------------------------------------------------------
60
61 // Define a new application type, each program should derive a class from wxApp
62 class MyApp : public wxApp
63 {
64 public:
65 // override base class virtuals
66 virtual bool OnInit();
67 };
68
69 class MyFrame : public wxFrame
70 {
71 public:
72 // ctor(s)
73 MyFrame(const wxString& title);
74
75 // event handlers (these functions should _not_ be virtual)
76 void OnQuit(wxCommandEvent& event);
77 void OnAbout(wxCommandEvent& event);
78 void OnRunWizard(wxCommandEvent& event);
79 void OnWizardCancel(wxWizardEvent& event);
80 void OnWizardFinished(wxWizardEvent& event);
81
82 private:
83 // any class wishing to process wxWidgets events must use this macro
84 DECLARE_EVENT_TABLE()
85 };
86
87 // ----------------------------------------------------------------------------
88 // some pages for our wizard
89 // ----------------------------------------------------------------------------
90
91 // This shows how to simply control the validity of the user input by just
92 // overriding TransferDataFromWindow() - of course, in a real program, the
93 // check wouldn't be so trivial and the data will be probably saved somewhere
94 // too.
95 //
96 // It also shows how to use a different bitmap for one of the pages.
97 class wxValidationPage : public wxWizardPageSimple
98 {
99 public:
100 wxValidationPage(wxWizard *parent) : wxWizardPageSimple(parent)
101 {
102 m_bitmap = wxBitmap(wiztest2_xpm);
103
104 m_checkbox = new wxCheckBox(this, wxID_ANY, _T("&Check me"));
105
106 wxBoxSizer *mainSizer = new wxBoxSizer(wxVERTICAL);
107 mainSizer->Add(
108 new wxStaticText(this, wxID_ANY,
109 _T("You need to check the checkbox\n")
110 _T("below before going to the next page\n")),
111 0,
112 wxALL,
113 5
114 );
115
116 mainSizer->Add(
117 m_checkbox,
118 0, // No stretching
119 wxALL,
120 5 // Border
121 );
122 SetSizer(mainSizer);
123 mainSizer->Fit(this);
124 }
125
126 virtual bool TransferDataFromWindow()
127 {
128 if ( !m_checkbox->GetValue() )
129 {
130 wxMessageBox(_T("Check the checkbox first!"), _T("No way"),
131 wxICON_WARNING | wxOK, this);
132
133 return false;
134 }
135
136 return true;
137 }
138
139 private:
140 wxCheckBox *m_checkbox;
141 };
142
143 // This is a more complicated example of validity checking: using events we may
144 // allow to return to the previous page, but not to proceed. It also
145 // demonstrates how to intercept [Cancel] button press.
146 class wxRadioboxPage : public wxWizardPageSimple
147 {
148 public:
149 // directions in which we allow the user to proceed from this page
150 enum
151 {
152 Forward, Backward, Both, Neither
153 };
154
155 wxRadioboxPage(wxWizard *parent) : wxWizardPageSimple(parent)
156 {
157 // should correspond to the enum above
158 // static wxString choices[] = { "forward", "backward", "both", "neither" };
159 // The above syntax can cause an internal compiler error with gcc.
160 wxString choices[4];
161 choices[0] = _T("forward");
162 choices[1] = _T("backward");
163 choices[2] = _T("both");
164 choices[3] = _T("neither");
165
166 m_radio = new wxRadioBox(this, wxID_ANY, _T("Allow to proceed:"),
167 wxDefaultPosition, wxDefaultSize,
168 WXSIZEOF(choices), choices,
169 1, wxRA_SPECIFY_COLS);
170 m_radio->SetSelection(Both);
171
172 wxBoxSizer *mainSizer = new wxBoxSizer(wxVERTICAL);
173 mainSizer->Add(
174 m_radio,
175 0, // No stretching
176 wxALL,
177 5 // Border
178 );
179
180 SetSizer(mainSizer);
181 mainSizer->Fit(this);
182 }
183
184 // wizard event handlers
185 void OnWizardCancel(wxWizardEvent& event)
186 {
187 if ( wxMessageBox(_T("Do you really want to cancel?"), _T("Question"),
188 wxICON_QUESTION | wxYES_NO, this) != wxYES )
189 {
190 // not confirmed
191 event.Veto();
192 }
193 }
194
195 void OnWizardPageChanging(wxWizardEvent& event)
196 {
197 int sel = m_radio->GetSelection();
198
199 if ( sel == Both )
200 return;
201
202 if ( event.GetDirection() && sel == Forward )
203 return;
204
205 if ( !event.GetDirection() && sel == Backward )
206 return;
207
208 wxMessageBox(_T("You can't go there"), _T("Not allowed"),
209 wxICON_WARNING | wxOK, this);
210
211 event.Veto();
212 }
213
214 private:
215 wxRadioBox *m_radio;
216
217 DECLARE_EVENT_TABLE()
218 };
219
220 // This shows how to dynamically (i.e. during run-time) arrange the page order.
221 class wxCheckboxPage : public wxWizardPage
222 {
223 public:
224 wxCheckboxPage(wxWizard *parent,
225 wxWizardPage *prev,
226 wxWizardPage *next)
227 : wxWizardPage(parent)
228 {
229 m_prev = prev;
230 m_next = next;
231
232 wxBoxSizer *mainSizer = new wxBoxSizer(wxVERTICAL);
233
234 mainSizer->Add(
235 new wxStaticText(this, wxID_ANY, _T("Try checking the box below and\n")
236 _T("then going back and clearing it")),
237 0, // No vertical stretching
238 wxALL,
239 5 // Border width
240 );
241
242 m_checkbox = new wxCheckBox(this, wxID_ANY, _T("&Skip the next page"));
243 mainSizer->Add(
244 m_checkbox,
245 0, // No vertical stretching
246 wxALL,
247 5 // Border width
248 );
249
250 #if wxUSE_CHECKLISTBOX
251 static const wxChar *aszChoices[] =
252 { _T("Zeroth"), _T("First"), _T("Second"), _T("Third"), _T("Fourth"), _T("Fifth"), _T("Sixth"), _T("Seventh"), _T("Eighth"), _T("Nineth") };
253 wxString *astrChoices = new wxString[WXSIZEOF(aszChoices)];
254 unsigned int ui;
255 for ( ui = 0; ui < WXSIZEOF(aszChoices); ui++ )
256 astrChoices[ui] = aszChoices[ui];
257
258 m_checklistbox = new wxCheckListBox(this, wxID_ANY, wxDefaultPosition, wxSize(100,100),
259 WXSIZEOF(aszChoices), astrChoices);
260
261 mainSizer->Add(
262 m_checklistbox,
263 0, // No vertical stretching
264 wxALL,
265 5 // Border width
266 );
267 #endif // wxUSE_CHECKLISTBOX
268
269 SetSizer(mainSizer);
270 mainSizer->Fit(this);
271 }
272
273 // implement wxWizardPage functions
274 virtual wxWizardPage *GetPrev() const { return m_prev; }
275 virtual wxWizardPage *GetNext() const
276 {
277 return m_checkbox->GetValue() ? m_next->GetNext() : m_next;
278 }
279
280 private:
281 wxWizardPage *m_prev,
282 *m_next;
283
284 wxCheckBox *m_checkbox;
285 #if wxUSE_CHECKLISTBOX
286 wxCheckListBox *m_checklistbox;
287 #endif
288 };
289
290 // ============================================================================
291 // implementation
292 // ============================================================================
293
294 // ----------------------------------------------------------------------------
295 // event tables and such
296 // ----------------------------------------------------------------------------
297
298 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
299 EVT_MENU(Wizard_Quit, MyFrame::OnQuit)
300 EVT_MENU(Wizard_About, MyFrame::OnAbout)
301 EVT_MENU(Wizard_Run, MyFrame::OnRunWizard)
302
303 EVT_WIZARD_CANCEL(wxID_ANY, MyFrame::OnWizardCancel)
304 EVT_WIZARD_FINISHED(wxID_ANY, MyFrame::OnWizardFinished)
305 END_EVENT_TABLE()
306
307 BEGIN_EVENT_TABLE(wxRadioboxPage, wxWizardPageSimple)
308 EVT_WIZARD_PAGE_CHANGING(wxID_ANY, wxRadioboxPage::OnWizardPageChanging)
309 EVT_WIZARD_CANCEL(wxID_ANY, wxRadioboxPage::OnWizardCancel)
310 END_EVENT_TABLE()
311
312 IMPLEMENT_APP(MyApp)
313
314 // ----------------------------------------------------------------------------
315 // the application class
316 // ----------------------------------------------------------------------------
317
318 // `Main program' equivalent: the program execution "starts" here
319 bool MyApp::OnInit()
320 {
321 MyFrame *frame = new MyFrame(_T("wxWizard Sample"));
322
323 // and show it (the frames, unlike simple controls, are not shown when
324 // created initially)
325 frame->Show(true);
326
327 // we're done
328 return true;
329 }
330
331 // ----------------------------------------------------------------------------
332 // MyFrame
333 // ----------------------------------------------------------------------------
334
335 MyFrame::MyFrame(const wxString& title)
336 : wxFrame((wxFrame *)NULL, wxID_ANY, title,
337 wxDefaultPosition, wxSize(250, 150)) // small frame
338 {
339 wxMenu *menuFile = new wxMenu;
340 menuFile->Append(Wizard_Run, _T("&Run wizard...\tCtrl-R"));
341 menuFile->AppendSeparator();
342 menuFile->Append(Wizard_Quit, _T("E&xit\tAlt-X"), _T("Quit this program"));
343
344 wxMenu *helpMenu = new wxMenu;
345 helpMenu->Append(Wizard_About, _T("&About...\tF1"), _T("Show about dialog"));
346
347 // now append the freshly created menu to the menu bar...
348 wxMenuBar *menuBar = new wxMenuBar();
349 menuBar->Append(menuFile, _T("&File"));
350 menuBar->Append(helpMenu, _T("&Help"));
351
352 // ... and attach this menu bar to the frame
353 SetMenuBar(menuBar);
354
355 // also create status bar which we use in OnWizardCancel
356 #if wxUSE_STATUSBAR
357 CreateStatusBar();
358 #endif // wxUSE_STATUSBAR
359 }
360
361 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
362 {
363 // true is to force the frame to close
364 Close(true);
365 }
366
367 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
368 {
369 wxMessageBox(_T("Demo of wxWizard class\n")
370 _T("(c) 1999, 2000 Vadim Zeitlin"),
371 _T("About wxWizard sample"), wxOK | wxICON_INFORMATION, this);
372 }
373
374 void MyFrame::OnRunWizard(wxCommandEvent& WXUNUSED(event))
375 {
376 wxWizard *wizard = new wxWizard(this, wxID_ANY,
377 _T("Absolutely Useless Wizard"),
378 wxBitmap(wiztest_xpm),
379 wxDefaultPosition,
380 wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER);
381
382 // a wizard page may be either an object of predefined class
383 wxWizardPageSimple *page1 = new wxWizardPageSimple(wizard);
384
385 /* wxStaticText *text = */ new wxStaticText(page1, wxID_ANY,
386 _T("This wizard doesn't help you\nto do anything at all.\n")
387 _T("\n")
388 _T("The next pages will present you\nwith more useless controls."),
389 wxPoint(5,5)
390 );
391
392 // ... or a derived class
393 wxRadioboxPage *page3 = new wxRadioboxPage(wizard);
394 wxValidationPage *page4 = new wxValidationPage(wizard);
395
396 // set the page order using a convenience function - could also use
397 // SetNext/Prev directly as below
398 wxWizardPageSimple::Chain(page3, page4);
399
400 // this page is not a wxWizardPageSimple, so we use SetNext/Prev to insert
401 // it into the chain of pages
402 wxCheckboxPage *page2 = new wxCheckboxPage(wizard, page1, page3);
403 page1->SetNext(page2);
404 page3->SetPrev(page2);
405
406 // allow the wizard to size itself around the pages
407 wizard->GetPageAreaSizer()->Add(page1);
408
409 if ( wizard->RunWizard(page1) )
410 {
411 wxMessageBox(_T("The wizard successfully completed"), _T("That's all"),
412 wxICON_INFORMATION | wxOK);
413 }
414
415 wizard->Destroy();
416 }
417
418 void MyFrame::OnWizardFinished(wxWizardEvent& WXUNUSED(event))
419 {
420 wxLogStatus(this, wxT("The wizard finished successfully."));
421 }
422
423 void MyFrame::OnWizardCancel(wxWizardEvent& WXUNUSED(event))
424 {
425 wxLogStatus(this, wxT("The wizard was cancelled."));
426 }