]>
git.saurik.com Git - wxWidgets.git/blob - samples/wizard/wizard.cpp
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/wizard.h"
43 #include "wiztest.xpm"
44 #include "wiztest2.xpm"
46 // ----------------------------------------------------------------------------
48 // ----------------------------------------------------------------------------
53 Wizard_Quit
= wxID_EXIT
,
54 Wizard_RunModal
= wxID_HIGHEST
,
57 Wizard_About
= wxID_ABOUT
60 // ----------------------------------------------------------------------------
62 // ----------------------------------------------------------------------------
64 // Define a new application type, each program should derive a class from wxApp
65 class MyApp
: public wxApp
68 // override base class virtuals
69 virtual bool OnInit();
72 class MyFrame
: public wxFrame
76 MyFrame(const wxString
& title
);
78 // event handlers (these functions should _not_ be virtual)
79 void OnQuit(wxCommandEvent
& event
);
80 void OnAbout(wxCommandEvent
& event
);
81 void OnRunWizard(wxCommandEvent
& event
);
82 void OnRunWizardNoSizer(wxCommandEvent
& event
);
83 void OnRunWizardModeless(wxCommandEvent
& event
);
84 void OnWizardCancel(wxWizardEvent
& event
);
85 void OnWizardFinished(wxWizardEvent
& event
);
88 // any class wishing to process wxWidgets events must use this macro
92 // ----------------------------------------------------------------------------
94 // ----------------------------------------------------------------------------
96 class MyWizard
: public wxWizard
99 MyWizard(wxFrame
*frame
, bool useSizer
= true);
101 wxWizardPage
*GetFirstPage() const { return m_page1
; }
104 wxWizardPageSimple
*m_page1
;
107 // ----------------------------------------------------------------------------
108 // some pages for our wizard
109 // ----------------------------------------------------------------------------
111 // This shows how to simply control the validity of the user input by just
112 // overriding TransferDataFromWindow() - of course, in a real program, the
113 // check wouldn't be so trivial and the data will be probably saved somewhere
116 // It also shows how to use a different bitmap for one of the pages.
117 class wxValidationPage
: public wxWizardPageSimple
120 wxValidationPage(wxWizard
*parent
) : wxWizardPageSimple(parent
)
122 m_bitmap
= wxBitmap(wiztest2_xpm
);
124 m_checkbox
= new wxCheckBox(this, wxID_ANY
, _T("&Check me"));
126 wxBoxSizer
*mainSizer
= new wxBoxSizer(wxVERTICAL
);
128 new wxStaticText(this, wxID_ANY
,
129 _T("You need to check the checkbox\n")
130 _T("below before going to the next page\n")),
143 mainSizer
->Fit(this);
146 virtual bool TransferDataFromWindow()
148 if ( !m_checkbox
->GetValue() )
150 wxMessageBox(_T("Check the checkbox first!"), _T("No way"),
151 wxICON_WARNING
| wxOK
, this);
160 wxCheckBox
*m_checkbox
;
163 // This is a more complicated example of validity checking: using events we may
164 // allow to return to the previous page, but not to proceed. It also
165 // demonstrates how to intercept [Cancel] button press.
166 class wxRadioboxPage
: public wxWizardPageSimple
169 // directions in which we allow the user to proceed from this page
172 Forward
, Backward
, Both
, Neither
175 wxRadioboxPage(wxWizard
*parent
) : wxWizardPageSimple(parent
)
177 // should correspond to the enum above
178 // static wxString choices[] = { "forward", "backward", "both", "neither" };
179 // The above syntax can cause an internal compiler error with gcc.
181 choices
[0] = _T("forward");
182 choices
[1] = _T("backward");
183 choices
[2] = _T("both");
184 choices
[3] = _T("neither");
186 m_radio
= new wxRadioBox(this, wxID_ANY
, _T("Allow to proceed:"),
187 wxDefaultPosition
, wxDefaultSize
,
188 WXSIZEOF(choices
), choices
,
189 1, wxRA_SPECIFY_COLS
);
190 m_radio
->SetSelection(Both
);
192 wxBoxSizer
*mainSizer
= new wxBoxSizer(wxVERTICAL
);
201 mainSizer
->Fit(this);
204 // wizard event handlers
205 void OnWizardCancel(wxWizardEvent
& event
)
207 if ( wxMessageBox(_T("Do you really want to cancel?"), _T("Question"),
208 wxICON_QUESTION
| wxYES_NO
, this) != wxYES
)
215 void OnWizardPageChanging(wxWizardEvent
& event
)
217 int sel
= m_radio
->GetSelection();
222 if ( event
.GetDirection() && sel
== Forward
)
225 if ( !event
.GetDirection() && sel
== Backward
)
228 wxMessageBox(_T("You can't go there"), _T("Not allowed"),
229 wxICON_WARNING
| wxOK
, this);
237 DECLARE_EVENT_TABLE()
240 // This shows how to dynamically (i.e. during run-time) arrange the page order.
241 class wxCheckboxPage
: public wxWizardPage
244 wxCheckboxPage(wxWizard
*parent
,
247 : wxWizardPage(parent
)
252 wxBoxSizer
*mainSizer
= new wxBoxSizer(wxVERTICAL
);
255 new wxStaticText(this, wxID_ANY
, _T("Try checking the box below and\n")
256 _T("then going back and clearing it")),
257 0, // No vertical stretching
262 m_checkbox
= new wxCheckBox(this, wxID_ANY
, _T("&Skip the next page"));
265 0, // No vertical stretching
270 #if wxUSE_CHECKLISTBOX
271 static const wxChar
*aszChoices
[] =
285 m_checklistbox
= new wxCheckListBox
291 wxArrayString(WXSIZEOF(aszChoices
), aszChoices
)
296 0, // No vertical stretching
300 #endif // wxUSE_CHECKLISTBOX
303 mainSizer
->Fit(this);
306 // implement wxWizardPage functions
307 virtual wxWizardPage
*GetPrev() const { return m_prev
; }
308 virtual wxWizardPage
*GetNext() const
310 return m_checkbox
->GetValue() ? m_next
->GetNext() : m_next
;
314 wxWizardPage
*m_prev
,
317 wxCheckBox
*m_checkbox
;
318 #if wxUSE_CHECKLISTBOX
319 wxCheckListBox
*m_checklistbox
;
323 // ============================================================================
325 // ============================================================================
327 // ----------------------------------------------------------------------------
328 // event tables and such
329 // ----------------------------------------------------------------------------
331 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
332 EVT_MENU(Wizard_Quit
, MyFrame::OnQuit
)
333 EVT_MENU(Wizard_About
, MyFrame::OnAbout
)
334 EVT_MENU(Wizard_RunModal
, MyFrame::OnRunWizard
)
335 EVT_MENU(Wizard_RunNoSizer
, MyFrame::OnRunWizardNoSizer
)
336 EVT_MENU(Wizard_RunModeless
, MyFrame::OnRunWizardModeless
)
338 EVT_WIZARD_CANCEL(wxID_ANY
, MyFrame::OnWizardCancel
)
339 EVT_WIZARD_FINISHED(wxID_ANY
, MyFrame::OnWizardFinished
)
342 BEGIN_EVENT_TABLE(wxRadioboxPage
, wxWizardPageSimple
)
343 EVT_WIZARD_PAGE_CHANGING(wxID_ANY
, wxRadioboxPage::OnWizardPageChanging
)
344 EVT_WIZARD_CANCEL(wxID_ANY
, wxRadioboxPage::OnWizardCancel
)
349 // ----------------------------------------------------------------------------
350 // the application class
351 // ----------------------------------------------------------------------------
353 // `Main program' equivalent: the program execution "starts" here
356 if ( !wxApp::OnInit() )
359 MyFrame
*frame
= new MyFrame(_T("wxWizard Sample"));
361 // and show it (the frames, unlike simple controls, are not shown when
362 // created initially)
369 // ----------------------------------------------------------------------------
371 // ----------------------------------------------------------------------------
373 MyWizard::MyWizard(wxFrame
*frame
, bool useSizer
)
374 : wxWizard(frame
,wxID_ANY
,_T("Absolutely Useless Wizard"),
375 wxBitmap(wiztest_xpm
),wxDefaultPosition
,
376 wxDEFAULT_DIALOG_STYLE
| wxRESIZE_BORDER
)
378 // a wizard page may be either an object of predefined class
379 m_page1
= new wxWizardPageSimple(this);
381 /* wxStaticText *text = */ new wxStaticText(m_page1
, wxID_ANY
,
382 _T("This wizard doesn't help you\nto do anything at all.\n")
384 _T("The next pages will present you\nwith more useless controls."),
388 // ... or a derived class
389 wxRadioboxPage
*page3
= new wxRadioboxPage(this);
390 wxValidationPage
*page4
= new wxValidationPage(this);
392 // set the page order using a convenience function - could also use
393 // SetNext/Prev directly as below
394 wxWizardPageSimple::Chain(page3
, page4
);
396 // this page is not a wxWizardPageSimple, so we use SetNext/Prev to insert
397 // it into the chain of pages
398 wxCheckboxPage
*page2
= new wxCheckboxPage(this, m_page1
, page3
);
399 m_page1
->SetNext(page2
);
400 page3
->SetPrev(page2
);
404 // allow the wizard to size itself around the pages
405 GetPageAreaSizer()->Add(m_page1
);
409 // ----------------------------------------------------------------------------
411 // ----------------------------------------------------------------------------
413 MyFrame::MyFrame(const wxString
& title
)
414 :wxFrame((wxFrame
*)NULL
, wxID_ANY
, title
,
415 wxDefaultPosition
, wxSize(250, 150)) // small frame
417 wxMenu
*menuFile
= new wxMenu
;
418 menuFile
->Append(Wizard_RunModal
, _T("&Run wizard modal...\tCtrl-R"));
419 menuFile
->Append(Wizard_RunNoSizer
, _T("Run wizard &without sizer..."));
420 menuFile
->Append(Wizard_RunModeless
, _T("Run wizard &modeless..."));
421 menuFile
->AppendSeparator();
422 menuFile
->Append(Wizard_Quit
, _T("E&xit\tAlt-X"), _T("Quit this program"));
424 wxMenu
*helpMenu
= new wxMenu
;
425 helpMenu
->Append(Wizard_About
, _T("&About...\tF1"), _T("Show about dialog"));
427 // now append the freshly created menu to the menu bar...
428 wxMenuBar
*menuBar
= new wxMenuBar();
429 menuBar
->Append(menuFile
, _T("&File"));
430 menuBar
->Append(helpMenu
, _T("&Help"));
432 // ... and attach this menu bar to the frame
435 // also create status bar which we use in OnWizardCancel
438 #endif // wxUSE_STATUSBAR
441 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
443 // true is to force the frame to close
447 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
449 wxMessageBox(_T("Demo of wxWizard class\n")
450 _T("(c) 1999, 2000 Vadim Zeitlin"),
451 _T("About wxWizard sample"), wxOK
| wxICON_INFORMATION
, this);
454 void MyFrame::OnRunWizard(wxCommandEvent
& WXUNUSED(event
))
456 MyWizard
wizard(this);
458 wizard
.RunWizard(wizard
.GetFirstPage());
461 void MyFrame::OnRunWizardNoSizer(wxCommandEvent
& WXUNUSED(event
))
463 MyWizard
wizard(this, false);
465 wizard
.RunWizard(wizard
.GetFirstPage());
468 void MyFrame::OnRunWizardModeless(wxCommandEvent
& WXUNUSED(event
))
470 MyWizard
*wizard
= new MyWizard(this);
471 wizard
->ShowPage(wizard
->GetFirstPage());
475 void MyFrame::OnWizardFinished(wxWizardEvent
& WXUNUSED(event
))
477 wxMessageBox(wxT("The wizard finished successfully."), wxT("Wizard notification"));
480 void MyFrame::OnWizardCancel(wxWizardEvent
& WXUNUSED(event
))
482 wxMessageBox(wxT("The wizard was cancelled."), wxT("Wizard notification"));