Remove obsolete VisualAge-related files.
[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 // Copyright: (c) Vadim Zeitlin
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
10
11 // ============================================================================
12 // declarations
13 // ============================================================================
14
15 // ----------------------------------------------------------------------------
16 // headers
17 // ----------------------------------------------------------------------------
18
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
26 // for all others, include the necessary headers
27 #ifndef WX_PRECOMP
28 #include "wx/frame.h"
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/textctrl.h"
41 #include "wx/wizard.h"
42
43 #include "wiztest.xpm"
44 #include "wiztest2.xpm"
45
46 #include "../sample.xpm"
47
48 // ----------------------------------------------------------------------------
49 // constants
50 // ----------------------------------------------------------------------------
51
52 // ids for menu items
53 enum
54 {
55 Wizard_About = wxID_ABOUT,
56 Wizard_Quit = wxID_EXIT,
57 Wizard_RunModal = wxID_HIGHEST,
58
59 Wizard_RunNoSizer,
60 Wizard_RunModeless,
61
62 Wizard_LargeWizard,
63 Wizard_ExpandBitmap
64 };
65
66 // ----------------------------------------------------------------------------
67 // private classes
68 // ----------------------------------------------------------------------------
69
70 // Define a new application type, each program should derive a class from wxApp
71 class MyApp : public wxApp
72 {
73 public:
74 // override base class virtuals
75 virtual bool OnInit();
76 };
77
78 class MyFrame : public wxFrame
79 {
80 public:
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);
88 void OnRunWizardNoSizer(wxCommandEvent& event);
89 void OnRunWizardModeless(wxCommandEvent& event);
90 void OnWizardCancel(wxWizardEvent& event);
91 void OnWizardFinished(wxWizardEvent& event);
92
93 private:
94 // any class wishing to process wxWidgets events must use this macro
95 DECLARE_EVENT_TABLE()
96 };
97
98 // ----------------------------------------------------------------------------
99 // our wizard
100 // ----------------------------------------------------------------------------
101
102 class MyWizard : public wxWizard
103 {
104 public:
105 MyWizard(wxFrame *frame, bool useSizer = true);
106
107 wxWizardPage *GetFirstPage() const { return m_page1; }
108
109 private:
110 wxWizardPageSimple *m_page1;
111 };
112
113 // ----------------------------------------------------------------------------
114 // some pages for our wizard
115 // ----------------------------------------------------------------------------
116
117 // This shows how to simply control the validity of the user input by just
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
120 // too.
121 //
122 // It also shows how to use a different bitmap for one of the pages.
123 class wxValidationPage : public wxWizardPageSimple
124 {
125 public:
126 wxValidationPage(wxWizard *parent) : wxWizardPageSimple(parent)
127 {
128 m_bitmap = wxBitmap(wiztest2_xpm);
129
130 m_checkbox = new wxCheckBox(this, wxID_ANY, wxT("&Check me"));
131
132 wxBoxSizer *mainSizer = new wxBoxSizer(wxVERTICAL);
133 mainSizer->Add(
134 new wxStaticText(this, wxID_ANY,
135 wxT("You need to check the checkbox\n")
136 wxT("below before going to the next page\n")),
137 0,
138 wxALL,
139 5
140 );
141
142 mainSizer->Add(
143 m_checkbox,
144 0, // No stretching
145 wxALL,
146 5 // Border
147 );
148 SetSizerAndFit(mainSizer);
149 }
150
151 virtual bool TransferDataFromWindow()
152 {
153 if ( !m_checkbox->GetValue() )
154 {
155 wxMessageBox(wxT("Check the checkbox first!"), wxT("No way"),
156 wxICON_WARNING | wxOK, this);
157
158 return false;
159 }
160
161 return true;
162 }
163
164 private:
165 wxCheckBox *m_checkbox;
166 };
167
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.
171 class wxRadioboxPage : public wxWizardPageSimple
172 {
173 public:
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
183 // static wxString choices[] = { "forward", "backward", "both", "neither" };
184 // The above syntax can cause an internal compiler error with gcc.
185 wxString choices[4];
186 choices[0] = wxT("forward");
187 choices[1] = wxT("backward");
188 choices[2] = wxT("both");
189 choices[3] = wxT("neither");
190
191 m_radio = new wxRadioBox(this, wxID_ANY, wxT("Allow to proceed:"),
192 wxDefaultPosition, wxDefaultSize,
193 WXSIZEOF(choices), choices,
194 1, wxRA_SPECIFY_COLS);
195 m_radio->SetSelection(Both);
196
197 wxBoxSizer *mainSizer = new wxBoxSizer(wxVERTICAL);
198 mainSizer->Add(
199 m_radio,
200 0, // No stretching
201 wxALL,
202 5 // Border
203 );
204
205 SetSizerAndFit(mainSizer);
206 }
207
208 // wizard event handlers
209 void OnWizardCancel(wxWizardEvent& event)
210 {
211 if ( wxMessageBox(wxT("Do you really want to cancel?"), wxT("Question"),
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
232 wxMessageBox(wxT("You can't go there"), wxT("Not allowed"),
233 wxICON_WARNING | wxOK, this);
234
235 event.Veto();
236 }
237
238 private:
239 wxRadioBox *m_radio;
240
241 DECLARE_EVENT_TABLE()
242 };
243
244 // This shows how to dynamically (i.e. during run-time) arrange the page order.
245 class wxCheckboxPage : public wxWizardPage
246 {
247 public:
248 wxCheckboxPage(wxWizard *parent,
249 wxWizardPage *prev,
250 wxWizardPage *next)
251 : wxWizardPage(parent)
252 {
253 m_prev = prev;
254 m_next = next;
255
256 wxBoxSizer *mainSizer = new wxBoxSizer(wxVERTICAL);
257
258 mainSizer->Add(
259 new wxStaticText(this, wxID_ANY, wxT("Try checking the box below and\n")
260 wxT("then going back and clearing it")),
261 0, // No vertical stretching
262 wxALL,
263 5 // Border width
264 );
265
266 m_checkbox = new wxCheckBox(this, wxID_ANY, wxT("&Skip the next page"));
267 mainSizer->Add(
268 m_checkbox,
269 0, // No vertical stretching
270 wxALL,
271 5 // Border width
272 );
273
274 #if wxUSE_CHECKLISTBOX
275 static const wxChar *aszChoices[] =
276 {
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")
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 );
297
298 mainSizer->Add(
299 m_checklistbox,
300 0, // No vertical stretching
301 wxALL,
302 5 // Border width
303 );
304 #endif // wxUSE_CHECKLISTBOX
305
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
314 SetSizerAndFit(mainSizer);
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
324 private:
325 wxWizardPage *m_prev,
326 *m_next;
327
328 wxCheckBox *m_checkbox;
329 #if wxUSE_CHECKLISTBOX
330 wxCheckListBox *m_checklistbox;
331 #endif
332 };
333
334 // ============================================================================
335 // implementation
336 // ============================================================================
337
338 // ----------------------------------------------------------------------------
339 // event tables and such
340 // ----------------------------------------------------------------------------
341
342 BEGIN_EVENT_TABLE(MyFrame, wxFrame)
343 EVT_MENU(Wizard_Quit, MyFrame::OnQuit)
344 EVT_MENU(Wizard_About, MyFrame::OnAbout)
345 EVT_MENU(Wizard_RunModal, MyFrame::OnRunWizard)
346 EVT_MENU(Wizard_RunNoSizer, MyFrame::OnRunWizardNoSizer)
347 EVT_MENU(Wizard_RunModeless, MyFrame::OnRunWizardModeless)
348
349 EVT_WIZARD_CANCEL(wxID_ANY, MyFrame::OnWizardCancel)
350 EVT_WIZARD_FINISHED(wxID_ANY, MyFrame::OnWizardFinished)
351 END_EVENT_TABLE()
352
353 BEGIN_EVENT_TABLE(wxRadioboxPage, wxWizardPageSimple)
354 EVT_WIZARD_PAGE_CHANGING(wxID_ANY, wxRadioboxPage::OnWizardPageChanging)
355 EVT_WIZARD_CANCEL(wxID_ANY, wxRadioboxPage::OnWizardCancel)
356 END_EVENT_TABLE()
357
358 IMPLEMENT_APP(MyApp)
359
360 // ----------------------------------------------------------------------------
361 // the application class
362 // ----------------------------------------------------------------------------
363
364 // `Main program' equivalent: the program execution "starts" here
365 bool MyApp::OnInit()
366 {
367 if ( !wxApp::OnInit() )
368 return false;
369
370 MyFrame *frame = new MyFrame(wxT("wxWizard Sample"));
371
372 // and show it (the frames, unlike simple controls, are not shown when
373 // created initially)
374 frame->Show(true);
375
376 // we're done
377 return true;
378 }
379
380 // ----------------------------------------------------------------------------
381 // MyWizard
382 // ----------------------------------------------------------------------------
383
384 MyWizard::MyWizard(wxFrame *frame, bool useSizer)
385 {
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);
391 SetIcon(wxICON(sample));
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
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,
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."),
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
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);
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
427 if ( useSizer )
428 {
429 // allow the wizard to size itself around the pages
430 GetPageAreaSizer()->Add(m_page1);
431 }
432 }
433
434 // ----------------------------------------------------------------------------
435 // MyFrame
436 // ----------------------------------------------------------------------------
437
438 MyFrame::MyFrame(const wxString& title)
439 :wxFrame((wxFrame *)NULL, wxID_ANY, title,
440 wxDefaultPosition, wxSize(250, 150)) // small frame
441 {
442 wxMenu *menuFile = new wxMenu;
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..."));
446 menuFile->AppendSeparator();
447 menuFile->Append(Wizard_Quit, wxT("E&xit\tAlt-X"), wxT("Quit this program"));
448
449 wxMenu *menuOptions = new wxMenu;
450 menuOptions->AppendCheckItem(Wizard_LargeWizard, wxT("&Scroll Wizard Pages"));
451 menuOptions->AppendCheckItem(Wizard_ExpandBitmap, wxT("Si&ze Bitmap To Page"));
452
453 wxMenu *helpMenu = new wxMenu;
454 helpMenu->Append(Wizard_About, wxT("&About\tF1"), wxT("Show about dialog"));
455
456 // now append the freshly created menu to the menu bar...
457 wxMenuBar *menuBar = new wxMenuBar();
458 menuBar->Append(menuFile, wxT("&File"));
459 menuBar->Append(menuOptions, wxT("&Options"));
460 menuBar->Append(helpMenu, wxT("&Help"));
461
462 // ... and attach this menu bar to the frame
463 SetMenuBar(menuBar);
464
465 // also create status bar which we use in OnWizardCancel
466 #if wxUSE_STATUSBAR
467 CreateStatusBar();
468 #endif // wxUSE_STATUSBAR
469 }
470
471 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
472 {
473 // true is to force the frame to close
474 Close(true);
475 }
476
477 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
478 {
479 wxMessageBox(wxT("Demo of wxWizard class\n")
480 wxT("(c) 1999, 2000 Vadim Zeitlin"),
481 wxT("About wxWizard sample"), wxOK | wxICON_INFORMATION, this);
482 }
483
484 void MyFrame::OnRunWizard(wxCommandEvent& WXUNUSED(event))
485 {
486 MyWizard wizard(this);
487
488 wizard.RunWizard(wizard.GetFirstPage());
489 }
490
491 void MyFrame::OnRunWizardNoSizer(wxCommandEvent& WXUNUSED(event))
492 {
493 MyWizard wizard(this, false);
494
495 wizard.RunWizard(wizard.GetFirstPage());
496 }
497
498 void MyFrame::OnRunWizardModeless(wxCommandEvent& WXUNUSED(event))
499 {
500 MyWizard *wizard = new MyWizard(this);
501 wizard->ShowPage(wizard->GetFirstPage());
502 wizard->Show(true);
503 }
504
505 void MyFrame::OnWizardFinished(wxWizardEvent& WXUNUSED(event))
506 {
507 wxMessageBox(wxT("The wizard finished successfully."), wxT("Wizard notification"));
508 }
509
510 void MyFrame::OnWizardCancel(wxWizardEvent& WXUNUSED(event))
511 {
512 wxMessageBox(wxT("The wizard was cancelled."), wxT("Wizard notification"));
513 }