1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxWidgets sample demonstrating wxWizard control
4 // Author: Vadim Zeitlin
5 // Modified by: Robert Vazan (sizers)
7 // Copyright: (c) Vadim Zeitlin
8 // Licence: wxWindows licence
9 /////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 // For compilers that support precompilation, includes "wx/wx.h".
20 #include "wx/wxprec.h"
26 // for all others, include the necessary headers
29 #include "wx/stattext.h"
32 #include "wx/checkbox.h"
33 #include "wx/checklst.h"
34 #include "wx/msgdlg.h"
35 #include "wx/radiobox.h"
40 #include "wx/textctrl.h"
41 #include "wx/wizard.h"
43 #include "wiztest.xpm"
44 #include "wiztest2.xpm"
46 #include "../sample.xpm"
48 // ----------------------------------------------------------------------------
50 // ----------------------------------------------------------------------------
55 Wizard_About
= wxID_ABOUT
,
56 Wizard_Quit
= wxID_EXIT
,
57 Wizard_RunModal
= wxID_HIGHEST
,
66 // ----------------------------------------------------------------------------
68 // ----------------------------------------------------------------------------
70 // Define a new application type, each program should derive a class from wxApp
71 class MyApp
: public wxApp
74 // override base class virtuals
75 virtual bool OnInit();
78 class MyFrame
: public wxFrame
82 MyFrame(const wxString
& title
);
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
);
94 // any class wishing to process wxWidgets events must use this macro
98 // ----------------------------------------------------------------------------
100 // ----------------------------------------------------------------------------
102 class MyWizard
: public wxWizard
105 MyWizard(wxFrame
*frame
, bool useSizer
= true);
107 wxWizardPage
*GetFirstPage() const { return m_page1
; }
110 wxWizardPageSimple
*m_page1
;
113 // ----------------------------------------------------------------------------
114 // some pages for our wizard
115 // ----------------------------------------------------------------------------
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
122 // It also shows how to use a different bitmap for one of the pages.
123 class wxValidationPage
: public wxWizardPageSimple
126 wxValidationPage(wxWizard
*parent
) : wxWizardPageSimple(parent
)
128 m_bitmap
= wxBitmap(wiztest2_xpm
);
130 m_checkbox
= new wxCheckBox(this, wxID_ANY
, wxT("&Check me"));
132 wxBoxSizer
*mainSizer
= new wxBoxSizer(wxVERTICAL
);
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")),
148 SetSizerAndFit(mainSizer
);
151 virtual bool TransferDataFromWindow()
153 if ( !m_checkbox
->GetValue() )
155 wxMessageBox(wxT("Check the checkbox first!"), wxT("No way"),
156 wxICON_WARNING
| wxOK
, this);
165 wxCheckBox
*m_checkbox
;
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
174 // directions in which we allow the user to proceed from this page
177 Forward
, Backward
, Both
, Neither
180 wxRadioboxPage(wxWizard
*parent
) : wxWizardPageSimple(parent
)
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.
186 choices
[0] = wxT("forward");
187 choices
[1] = wxT("backward");
188 choices
[2] = wxT("both");
189 choices
[3] = wxT("neither");
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
);
197 wxBoxSizer
*mainSizer
= new wxBoxSizer(wxVERTICAL
);
205 SetSizerAndFit(mainSizer
);
208 // wizard event handlers
209 void OnWizardCancel(wxWizardEvent
& event
)
211 if ( wxMessageBox(wxT("Do you really want to cancel?"), wxT("Question"),
212 wxICON_QUESTION
| wxYES_NO
, this) != wxYES
)
219 void OnWizardPageChanging(wxWizardEvent
& event
)
221 int sel
= m_radio
->GetSelection();
226 if ( event
.GetDirection() && sel
== Forward
)
229 if ( !event
.GetDirection() && sel
== Backward
)
232 wxMessageBox(wxT("You can't go there"), wxT("Not allowed"),
233 wxICON_WARNING
| wxOK
, this);
241 DECLARE_EVENT_TABLE()
244 // This shows how to dynamically (i.e. during run-time) arrange the page order.
245 class wxCheckboxPage
: public wxWizardPage
248 wxCheckboxPage(wxWizard
*parent
,
251 : wxWizardPage(parent
)
256 wxBoxSizer
*mainSizer
= new wxBoxSizer(wxVERTICAL
);
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
266 m_checkbox
= new wxCheckBox(this, wxID_ANY
, wxT("&Skip the next page"));
269 0, // No vertical stretching
274 #if wxUSE_CHECKLISTBOX
275 static const wxChar
*aszChoices
[] =
289 m_checklistbox
= new wxCheckListBox
295 wxArrayString(WXSIZEOF(aszChoices
), aszChoices
)
300 0, // No vertical stretching
304 #endif // wxUSE_CHECKLISTBOX
306 wxSize textSize
= wxSize(150, 200);
307 if (((wxFrame
*) wxTheApp
->GetTopWindow())->GetMenuBar()->IsChecked(Wizard_LargeWizard
))
308 textSize
= wxSize(150, wxGetClientDisplayRect().GetHeight() - 200);
311 wxTextCtrl
* textCtrl
= new wxTextCtrl(this, wxID_ANY
, wxEmptyString
, wxDefaultPosition
, textSize
, wxTE_MULTILINE
);
312 mainSizer
->Add(textCtrl
, 0, wxALL
|wxEXPAND
, 5);
314 SetSizerAndFit(mainSizer
);
317 // implement wxWizardPage functions
318 virtual wxWizardPage
*GetPrev() const { return m_prev
; }
319 virtual wxWizardPage
*GetNext() const
321 return m_checkbox
->GetValue() ? m_next
->GetNext() : m_next
;
325 wxWizardPage
*m_prev
,
328 wxCheckBox
*m_checkbox
;
329 #if wxUSE_CHECKLISTBOX
330 wxCheckListBox
*m_checklistbox
;
334 // ============================================================================
336 // ============================================================================
338 // ----------------------------------------------------------------------------
339 // event tables and such
340 // ----------------------------------------------------------------------------
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
)
349 EVT_WIZARD_CANCEL(wxID_ANY
, MyFrame::OnWizardCancel
)
350 EVT_WIZARD_FINISHED(wxID_ANY
, MyFrame::OnWizardFinished
)
353 BEGIN_EVENT_TABLE(wxRadioboxPage
, wxWizardPageSimple
)
354 EVT_WIZARD_PAGE_CHANGING(wxID_ANY
, wxRadioboxPage::OnWizardPageChanging
)
355 EVT_WIZARD_CANCEL(wxID_ANY
, wxRadioboxPage::OnWizardCancel
)
360 // ----------------------------------------------------------------------------
361 // the application class
362 // ----------------------------------------------------------------------------
364 // `Main program' equivalent: the program execution "starts" here
367 if ( !wxApp::OnInit() )
370 MyFrame
*frame
= new MyFrame(wxT("wxWizard Sample"));
372 // and show it (the frames, unlike simple controls, are not shown when
373 // created initially)
380 // ----------------------------------------------------------------------------
382 // ----------------------------------------------------------------------------
384 MyWizard::MyWizard(wxFrame
*frame
, bool useSizer
)
386 SetExtraStyle(wxWIZARD_EX_HELPBUTTON
);
388 Create(frame
,wxID_ANY
,wxT("Absolutely Useless Wizard"),
389 wxBitmap(wiztest_xpm
),wxDefaultPosition
,
390 wxDEFAULT_DIALOG_STYLE
| wxRESIZE_BORDER
);
391 SetIcon(wxICON(sample
));
393 // Allow the bitmap to be expanded to fit the page height
394 if (frame
->GetMenuBar()->IsChecked(Wizard_ExpandBitmap
))
395 SetBitmapPlacement(wxWIZARD_VALIGN_CENTRE
);
397 // Enable scrolling adaptation
398 if (frame
->GetMenuBar()->IsChecked(Wizard_LargeWizard
))
399 SetLayoutAdaptationMode(wxDIALOG_ADAPTATION_MODE_ENABLED
);
401 // a wizard page may be either an object of predefined class
402 m_page1
= new wxWizardPageSimple(this);
404 /* wxStaticText *text = */ new wxStaticText(m_page1
, wxID_ANY
,
405 wxT("This wizard doesn't help you\nto do anything at all.\n")
407 wxT("The next pages will present you\nwith more useless controls."),
411 // ... or a derived class
412 wxRadioboxPage
*page3
= new wxRadioboxPage(this);
413 wxValidationPage
*page4
= new wxValidationPage(this);
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.
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
);
429 // allow the wizard to size itself around the pages
430 GetPageAreaSizer()->Add(m_page1
);
434 // ----------------------------------------------------------------------------
436 // ----------------------------------------------------------------------------
438 MyFrame::MyFrame(const wxString
& title
)
439 :wxFrame((wxFrame
*)NULL
, wxID_ANY
, title
,
440 wxDefaultPosition
, wxSize(250, 150)) // small frame
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"));
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"));
453 wxMenu
*helpMenu
= new wxMenu
;
454 helpMenu
->Append(Wizard_About
, wxT("&About\tF1"), wxT("Show about dialog"));
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"));
462 // ... and attach this menu bar to the frame
465 // also create status bar which we use in OnWizardCancel
468 #endif // wxUSE_STATUSBAR
471 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
473 // true is to force the frame to close
477 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
479 wxMessageBox(wxT("Demo of wxWizard class\n")
480 wxT("(c) 1999, 2000 Vadim Zeitlin"),
481 wxT("About wxWizard sample"), wxOK
| wxICON_INFORMATION
, this);
484 void MyFrame::OnRunWizard(wxCommandEvent
& WXUNUSED(event
))
486 MyWizard
wizard(this);
488 wizard
.RunWizard(wizard
.GetFirstPage());
491 void MyFrame::OnRunWizardNoSizer(wxCommandEvent
& WXUNUSED(event
))
493 MyWizard
wizard(this, false);
495 wizard
.RunWizard(wizard
.GetFirstPage());
498 void MyFrame::OnRunWizardModeless(wxCommandEvent
& WXUNUSED(event
))
500 MyWizard
*wizard
= new MyWizard(this);
501 wizard
->ShowPage(wizard
->GetFirstPage());
505 void MyFrame::OnWizardFinished(wxWizardEvent
& WXUNUSED(event
))
507 wxMessageBox(wxT("The wizard finished successfully."), wxT("Wizard notification"));
510 void MyFrame::OnWizardCancel(wxWizardEvent
& WXUNUSED(event
))
512 wxMessageBox(wxT("The wizard was cancelled."), wxT("Wizard notification"));