]>
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 MyFrame
*frame
= new MyFrame(_T("wxWizard Sample"));
358 // and show it (the frames, unlike simple controls, are not shown when
359 // created initially)
366 // ----------------------------------------------------------------------------
368 // ----------------------------------------------------------------------------
370 MyWizard::MyWizard(wxFrame
*frame
, bool useSizer
)
371 : wxWizard(frame
,wxID_ANY
,_T("Absolutely Useless Wizard"),
372 wxBitmap(wiztest_xpm
),wxDefaultPosition
,
373 wxDEFAULT_DIALOG_STYLE
| wxRESIZE_BORDER
)
375 // a wizard page may be either an object of predefined class
376 m_page1
= new wxWizardPageSimple(this);
378 /* wxStaticText *text = */ new wxStaticText(m_page1
, wxID_ANY
,
379 _T("This wizard doesn't help you\nto do anything at all.\n")
381 _T("The next pages will present you\nwith more useless controls."),
385 // ... or a derived class
386 wxRadioboxPage
*page3
= new wxRadioboxPage(this);
387 wxValidationPage
*page4
= new wxValidationPage(this);
389 // set the page order using a convenience function - could also use
390 // SetNext/Prev directly as below
391 wxWizardPageSimple::Chain(page3
, page4
);
393 // this page is not a wxWizardPageSimple, so we use SetNext/Prev to insert
394 // it into the chain of pages
395 wxCheckboxPage
*page2
= new wxCheckboxPage(this, m_page1
, page3
);
396 m_page1
->SetNext(page2
);
397 page3
->SetPrev(page2
);
401 // allow the wizard to size itself around the pages
402 GetPageAreaSizer()->Add(m_page1
);
406 // ----------------------------------------------------------------------------
408 // ----------------------------------------------------------------------------
410 MyFrame::MyFrame(const wxString
& title
)
411 :wxFrame((wxFrame
*)NULL
, wxID_ANY
, title
,
412 wxDefaultPosition
, wxSize(250, 150)) // small frame
414 wxMenu
*menuFile
= new wxMenu
;
415 menuFile
->Append(Wizard_RunModal
, _T("&Run wizard modal...\tCtrl-R"));
416 menuFile
->Append(Wizard_RunNoSizer
, _T("Run wizard &without sizer..."));
417 menuFile
->Append(Wizard_RunModeless
, _T("Run wizard &modeless..."));
418 menuFile
->AppendSeparator();
419 menuFile
->Append(Wizard_Quit
, _T("E&xit\tAlt-X"), _T("Quit this program"));
421 wxMenu
*helpMenu
= new wxMenu
;
422 helpMenu
->Append(Wizard_About
, _T("&About...\tF1"), _T("Show about dialog"));
424 // now append the freshly created menu to the menu bar...
425 wxMenuBar
*menuBar
= new wxMenuBar();
426 menuBar
->Append(menuFile
, _T("&File"));
427 menuBar
->Append(helpMenu
, _T("&Help"));
429 // ... and attach this menu bar to the frame
432 // also create status bar which we use in OnWizardCancel
435 #endif // wxUSE_STATUSBAR
438 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
440 // true is to force the frame to close
444 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
446 wxMessageBox(_T("Demo of wxWizard class\n")
447 _T("(c) 1999, 2000 Vadim Zeitlin"),
448 _T("About wxWizard sample"), wxOK
| wxICON_INFORMATION
, this);
451 void MyFrame::OnRunWizard(wxCommandEvent
& WXUNUSED(event
))
453 MyWizard
wizard(this);
455 wizard
.RunWizard(wizard
.GetFirstPage());
458 void MyFrame::OnRunWizardNoSizer(wxCommandEvent
& WXUNUSED(event
))
460 MyWizard
wizard(this, false);
462 wizard
.RunWizard(wizard
.GetFirstPage());
465 void MyFrame::OnRunWizardModeless(wxCommandEvent
& WXUNUSED(event
))
467 MyWizard
*wizard
= new MyWizard(this);
468 wizard
->ShowPage(wizard
->GetFirstPage());
472 void MyFrame::OnWizardFinished(wxWizardEvent
& WXUNUSED(event
))
474 wxMessageBox(wxT("The wizard finished successfully."), wxT("Wizard notification"));
477 void MyFrame::OnWizardCancel(wxWizardEvent
& WXUNUSED(event
))
479 wxMessageBox(wxT("The wizard was cancelled."), wxT("Wizard notification"));