1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxWidgets sample demonstrating wxWizard control
4 // Author: Vadim Zeitlin
5 // Modified by: Robert Vazan (sizers)
8 // Copyright: (c) Vadim Zeitlin
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx/wx.h".
21 #include "wx/wxprec.h"
27 // for all others, include the necessary headers
30 #include "wx/stattext.h"
33 #include "wx/checkbox.h"
34 #include "wx/checklst.h"
35 #include "wx/msgdlg.h"
36 #include "wx/radiobox.h"
41 #include "wx/textctrl.h"
42 #include "wx/wizard.h"
44 #include "wiztest.xpm"
45 #include "wiztest2.xpm"
47 #include "../sample.xpm"
49 // ----------------------------------------------------------------------------
51 // ----------------------------------------------------------------------------
56 Wizard_About
= wxID_ABOUT
,
57 Wizard_Quit
= wxID_EXIT
,
58 Wizard_RunModal
= wxID_HIGHEST
,
67 // ----------------------------------------------------------------------------
69 // ----------------------------------------------------------------------------
71 // Define a new application type, each program should derive a class from wxApp
72 class MyApp
: public wxApp
75 // override base class virtuals
76 virtual bool OnInit();
79 class MyFrame
: public wxFrame
83 MyFrame(const wxString
& title
);
85 // event handlers (these functions should _not_ be virtual)
86 void OnQuit(wxCommandEvent
& event
);
87 void OnAbout(wxCommandEvent
& event
);
88 void OnRunWizard(wxCommandEvent
& event
);
89 void OnRunWizardNoSizer(wxCommandEvent
& event
);
90 void OnRunWizardModeless(wxCommandEvent
& event
);
91 void OnWizardCancel(wxWizardEvent
& event
);
92 void OnWizardFinished(wxWizardEvent
& event
);
95 // any class wishing to process wxWidgets events must use this macro
99 // ----------------------------------------------------------------------------
101 // ----------------------------------------------------------------------------
103 class MyWizard
: public wxWizard
106 MyWizard(wxFrame
*frame
, bool useSizer
= true);
108 wxWizardPage
*GetFirstPage() const { return m_page1
; }
111 wxWizardPageSimple
*m_page1
;
114 // ----------------------------------------------------------------------------
115 // some pages for our wizard
116 // ----------------------------------------------------------------------------
118 // This shows how to simply control the validity of the user input by just
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
123 // It also shows how to use a different bitmap for one of the pages.
124 class wxValidationPage
: public wxWizardPageSimple
127 wxValidationPage(wxWizard
*parent
) : wxWizardPageSimple(parent
)
129 m_bitmap
= wxBitmap(wiztest2_xpm
);
131 m_checkbox
= new wxCheckBox(this, wxID_ANY
, _T("&Check me"));
133 wxBoxSizer
*mainSizer
= new wxBoxSizer(wxVERTICAL
);
135 new wxStaticText(this, wxID_ANY
,
136 _T("You need to check the checkbox\n")
137 _T("below before going to the next page\n")),
150 mainSizer
->Fit(this);
153 virtual bool TransferDataFromWindow()
155 if ( !m_checkbox
->GetValue() )
157 wxMessageBox(_T("Check the checkbox first!"), _T("No way"),
158 wxICON_WARNING
| wxOK
, this);
167 wxCheckBox
*m_checkbox
;
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.
173 class wxRadioboxPage
: public wxWizardPageSimple
176 // directions in which we allow the user to proceed from this page
179 Forward
, Backward
, Both
, Neither
182 wxRadioboxPage(wxWizard
*parent
) : wxWizardPageSimple(parent
)
184 // should correspond to the enum above
185 // static wxString choices[] = { "forward", "backward", "both", "neither" };
186 // The above syntax can cause an internal compiler error with gcc.
188 choices
[0] = _T("forward");
189 choices
[1] = _T("backward");
190 choices
[2] = _T("both");
191 choices
[3] = _T("neither");
193 m_radio
= new wxRadioBox(this, wxID_ANY
, _T("Allow to proceed:"),
194 wxDefaultPosition
, wxDefaultSize
,
195 WXSIZEOF(choices
), choices
,
196 1, wxRA_SPECIFY_COLS
);
197 m_radio
->SetSelection(Both
);
199 wxBoxSizer
*mainSizer
= new wxBoxSizer(wxVERTICAL
);
208 mainSizer
->Fit(this);
211 // wizard event handlers
212 void OnWizardCancel(wxWizardEvent
& event
)
214 if ( wxMessageBox(_T("Do you really want to cancel?"), _T("Question"),
215 wxICON_QUESTION
| wxYES_NO
, this) != wxYES
)
222 void OnWizardPageChanging(wxWizardEvent
& event
)
224 int sel
= m_radio
->GetSelection();
229 if ( event
.GetDirection() && sel
== Forward
)
232 if ( !event
.GetDirection() && sel
== Backward
)
235 wxMessageBox(_T("You can't go there"), _T("Not allowed"),
236 wxICON_WARNING
| wxOK
, this);
244 DECLARE_EVENT_TABLE()
247 // This shows how to dynamically (i.e. during run-time) arrange the page order.
248 class wxCheckboxPage
: public wxWizardPage
251 wxCheckboxPage(wxWizard
*parent
,
254 : wxWizardPage(parent
)
259 wxBoxSizer
*mainSizer
= new wxBoxSizer(wxVERTICAL
);
262 new wxStaticText(this, wxID_ANY
, _T("Try checking the box below and\n")
263 _T("then going back and clearing it")),
264 0, // No vertical stretching
269 m_checkbox
= new wxCheckBox(this, wxID_ANY
, _T("&Skip the next page"));
272 0, // No vertical stretching
277 #if wxUSE_CHECKLISTBOX
278 static const wxChar
*aszChoices
[] =
292 m_checklistbox
= new wxCheckListBox
298 wxArrayString(WXSIZEOF(aszChoices
), aszChoices
)
303 0, // No vertical stretching
307 #endif // wxUSE_CHECKLISTBOX
309 wxSize textSize
= wxSize(150, 200);
310 if (((wxFrame
*) wxTheApp
->GetTopWindow())->GetMenuBar()->IsChecked(Wizard_LargeWizard
))
311 textSize
= wxSize(150, wxGetClientDisplayRect().GetHeight() - 200);
314 wxTextCtrl
* textCtrl
= new wxTextCtrl(this, wxID_ANY
, wxEmptyString
, wxDefaultPosition
, textSize
, wxTE_MULTILINE
);
315 mainSizer
->Add(textCtrl
, 0, wxALL
|wxEXPAND
, 5);
318 mainSizer
->Fit(this);
321 // implement wxWizardPage functions
322 virtual wxWizardPage
*GetPrev() const { return m_prev
; }
323 virtual wxWizardPage
*GetNext() const
325 return m_checkbox
->GetValue() ? m_next
->GetNext() : m_next
;
329 wxWizardPage
*m_prev
,
332 wxCheckBox
*m_checkbox
;
333 #if wxUSE_CHECKLISTBOX
334 wxCheckListBox
*m_checklistbox
;
338 // ============================================================================
340 // ============================================================================
342 // ----------------------------------------------------------------------------
343 // event tables and such
344 // ----------------------------------------------------------------------------
346 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
347 EVT_MENU(Wizard_Quit
, MyFrame::OnQuit
)
348 EVT_MENU(Wizard_About
, MyFrame::OnAbout
)
349 EVT_MENU(Wizard_RunModal
, MyFrame::OnRunWizard
)
350 EVT_MENU(Wizard_RunNoSizer
, MyFrame::OnRunWizardNoSizer
)
351 EVT_MENU(Wizard_RunModeless
, MyFrame::OnRunWizardModeless
)
353 EVT_WIZARD_CANCEL(wxID_ANY
, MyFrame::OnWizardCancel
)
354 EVT_WIZARD_FINISHED(wxID_ANY
, MyFrame::OnWizardFinished
)
357 BEGIN_EVENT_TABLE(wxRadioboxPage
, wxWizardPageSimple
)
358 EVT_WIZARD_PAGE_CHANGING(wxID_ANY
, wxRadioboxPage::OnWizardPageChanging
)
359 EVT_WIZARD_CANCEL(wxID_ANY
, wxRadioboxPage::OnWizardCancel
)
364 // ----------------------------------------------------------------------------
365 // the application class
366 // ----------------------------------------------------------------------------
368 // `Main program' equivalent: the program execution "starts" here
371 if ( !wxApp::OnInit() )
374 MyFrame
*frame
= new MyFrame(_T("wxWizard Sample"));
376 // and show it (the frames, unlike simple controls, are not shown when
377 // created initially)
384 // ----------------------------------------------------------------------------
386 // ----------------------------------------------------------------------------
388 MyWizard::MyWizard(wxFrame
*frame
, bool useSizer
)
389 : wxWizard(frame
,wxID_ANY
,_T("Absolutely Useless Wizard"),
390 wxBitmap(wiztest_xpm
),wxDefaultPosition
,
391 wxDEFAULT_DIALOG_STYLE
| wxRESIZE_BORDER
)
393 SetIcon(wxIcon(sample_xpm
));
395 // Allow the bitmap to be expanded to fit the page height
396 if (frame
->GetMenuBar()->IsChecked(Wizard_ExpandBitmap
))
397 SetBitmapPlacement(wxWIZARD_VALIGN_CENTRE
);
399 // Enable scrolling adaptation
400 if (frame
->GetMenuBar()->IsChecked(Wizard_LargeWizard
))
401 SetLayoutAdaptationMode(wxDIALOG_ADAPTATION_MODE_ENABLED
);
403 // a wizard page may be either an object of predefined class
404 m_page1
= new wxWizardPageSimple(this);
406 /* wxStaticText *text = */ new wxStaticText(m_page1
, wxID_ANY
,
407 _T("This wizard doesn't help you\nto do anything at all.\n")
409 _T("The next pages will present you\nwith more useless controls."),
413 // ... or a derived class
414 wxRadioboxPage
*page3
= new wxRadioboxPage(this);
415 wxValidationPage
*page4
= new wxValidationPage(this);
417 // set the page order using a convenience function - could also use
418 // SetNext/Prev directly as below
419 wxWizardPageSimple::Chain(page3
, page4
);
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
, _T("&Run wizard modal...\tCtrl-R"));
444 menuFile
->Append(Wizard_RunNoSizer
, _T("Run wizard &without sizer..."));
445 menuFile
->Append(Wizard_RunModeless
, _T("Run wizard &modeless..."));
446 menuFile
->AppendSeparator();
447 menuFile
->Append(Wizard_Quit
, _T("E&xit\tAlt-X"), _T("Quit this program"));
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"));
453 wxMenu
*helpMenu
= new wxMenu
;
454 helpMenu
->Append(Wizard_About
, _T("&About...\tF1"), _T("Show about dialog"));
456 // now append the freshly created menu to the menu bar...
457 wxMenuBar
*menuBar
= new wxMenuBar();
458 menuBar
->Append(menuFile
, _T("&File"));
459 menuBar
->Append(menuOptions
, _T("&Options"));
460 menuBar
->Append(helpMenu
, _T("&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(_T("Demo of wxWizard class\n")
480 _T("(c) 1999, 2000 Vadim Zeitlin"),
481 _T("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"));