]>
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
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/wizard.h"
43 #include "wiztest.xpm"
44 #include "wiztest2.xpm"
47 // ----------------------------------------------------------------------------
49 // ----------------------------------------------------------------------------
59 // ----------------------------------------------------------------------------
61 // ----------------------------------------------------------------------------
63 // Define a new application type, each program should derive a class from wxApp
64 class MyApp
: public wxApp
67 // override base class virtuals
68 virtual bool OnInit();
71 class MyFrame
: public wxFrame
75 MyFrame(const wxString
& title
);
77 // event handlers (these functions should _not_ be virtual)
78 void OnQuit(wxCommandEvent
& event
);
79 void OnAbout(wxCommandEvent
& event
);
80 void OnRunWizard(wxCommandEvent
& event
);
81 void OnWizardCancel(wxWizardEvent
& event
);
82 void OnWizardFinished(wxWizardEvent
& event
);
85 // any class wishing to process wxWidgets events must use this macro
89 // ----------------------------------------------------------------------------
90 // some pages for our wizard
91 // ----------------------------------------------------------------------------
93 // this shows how to simply control the validity of the user input by just
94 // overriding TransferDataFromWindow() - of course, in a real program, the
95 // check wouldn't be so trivial and the data will be probably saved somewhere
98 // it also shows how to use a different bitmap for one of the pages
99 class wxValidationPage
: public wxWizardPageSimple
102 wxValidationPage(wxWizard
*parent
) : wxWizardPageSimple(parent
)
104 m_bitmap
= wxBITMAP(wiztest2
);
106 m_checkbox
= new wxCheckBox(this, wxID_ANY
, _T("&Check me"));
108 wxBoxSizer
*mainSizer
= new wxBoxSizer(wxVERTICAL
);
110 new wxStaticText(this, wxID_ANY
,
111 _T("You need to check the checkbox\n")
112 _T("below before going to the next page\n")),
125 mainSizer
->Fit(this);
128 virtual bool TransferDataFromWindow()
130 if ( !m_checkbox
->GetValue() )
132 wxMessageBox(_T("Check the checkbox first!"), _T("No way"),
133 wxICON_WARNING
| wxOK
, this);
142 wxCheckBox
*m_checkbox
;
145 // This is a more complicated example of validity checking: using events we may
146 // allow to return to the previous page, but not to proceed. It also
147 // demonstrates how to intercept [Cancel] button press.
148 class wxRadioboxPage
: public wxWizardPageSimple
151 // directions in which we allow the user to proceed from this page
154 Forward
, Backward
, Both
, Neither
157 wxRadioboxPage(wxWizard
*parent
) : wxWizardPageSimple(parent
)
159 // should correspond to the enum above
160 // static wxString choices[] = { "forward", "backward", "both", "neither" };
161 // The above syntax can cause an internal compiler error with gcc.
163 choices
[0] = _T("forward");
164 choices
[1] = _T("backward");
165 choices
[2] = _T("both");
166 choices
[3] = _T("neither");
168 m_radio
= new wxRadioBox(this, wxID_ANY
, _T("Allow to proceed:"),
169 wxDefaultPosition
, wxDefaultSize
,
170 WXSIZEOF(choices
), choices
,
171 1, wxRA_SPECIFY_COLS
);
172 m_radio
->SetSelection(Both
);
174 wxBoxSizer
*mainSizer
= new wxBoxSizer(wxVERTICAL
);
183 mainSizer
->Fit(this);
186 // wizard event handlers
187 void OnWizardCancel(wxWizardEvent
& event
)
189 if ( wxMessageBox(_T("Do you really want to cancel?"), _T("Question"),
190 wxICON_QUESTION
| wxYES_NO
, this) != wxYES
)
197 void OnWizardPageChanging(wxWizardEvent
& event
)
199 int sel
= m_radio
->GetSelection();
204 if ( event
.GetDirection() && sel
== Forward
)
207 if ( !event
.GetDirection() && sel
== Backward
)
210 wxMessageBox(_T("You can't go there"), _T("Not allowed"),
211 wxICON_WARNING
| wxOK
, this);
219 DECLARE_EVENT_TABLE()
222 // this shows how to dynamically (i.e. during run-time) arrange the page order
223 class wxCheckboxPage
: public wxWizardPage
226 wxCheckboxPage(wxWizard
*parent
,
229 : wxWizardPage(parent
)
234 wxBoxSizer
*mainSizer
= new wxBoxSizer(wxVERTICAL
);
237 new wxStaticText(this, wxID_ANY
, _T("Try checking the box below and\n")
238 _T("then going back and clearing it")),
239 0, // No vertical stretching
244 m_checkbox
= new wxCheckBox(this, wxID_ANY
, _T("&Skip the next page"));
247 0, // No vertical stretching
252 static const wxChar
*aszChoices
[] =
253 { _T("Zeroth"), _T("First"), _T("Second"), _T("Third"), _T("Fourth"), _T("Fifth"), _T("Sixth"), _T("Seventh"), _T("Eighth"), _T("Nineth") };
254 wxString
*astrChoices
= new wxString
[WXSIZEOF(aszChoices
)];
256 for ( ui
= 0; ui
< WXSIZEOF(aszChoices
); ui
++ )
257 astrChoices
[ui
] = aszChoices
[ui
];
258 m_checklistbox
= new wxCheckListBox(this, wxID_ANY
, wxDefaultPosition
, wxSize(100,100),
259 WXSIZEOF(aszChoices
), astrChoices
);
263 0, // No vertical stretching
270 mainSizer
->Fit(this);
273 // implement wxWizardPage functions
274 virtual wxWizardPage
*GetPrev() const { return m_prev
; }
275 virtual wxWizardPage
*GetNext() const
277 return m_checkbox
->GetValue() ? m_next
->GetNext() : m_next
;
281 wxWizardPage
*m_prev
,
284 wxCheckBox
*m_checkbox
;
285 wxCheckListBox
*m_checklistbox
;
288 // ============================================================================
290 // ============================================================================
292 // ----------------------------------------------------------------------------
293 // event tables and such
294 // ----------------------------------------------------------------------------
296 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
297 EVT_MENU(Wizard_Quit
, MyFrame::OnQuit
)
298 EVT_MENU(Wizard_About
, MyFrame::OnAbout
)
299 EVT_MENU(Wizard_Run
, MyFrame::OnRunWizard
)
301 EVT_WIZARD_CANCEL(wxID_ANY
, MyFrame::OnWizardCancel
)
302 EVT_WIZARD_FINISHED(wxID_ANY
, MyFrame::OnWizardFinished
)
305 BEGIN_EVENT_TABLE(wxRadioboxPage
, wxWizardPageSimple
)
306 EVT_WIZARD_PAGE_CHANGING(wxID_ANY
, wxRadioboxPage::OnWizardPageChanging
)
307 EVT_WIZARD_CANCEL(wxID_ANY
, wxRadioboxPage::OnWizardCancel
)
312 // ----------------------------------------------------------------------------
313 // the application class
314 // ----------------------------------------------------------------------------
316 // `Main program' equivalent: the program execution "starts" here
319 MyFrame
*frame
= new MyFrame(_T("wxWizard Sample"));
321 // and show it (the frames, unlike simple controls, are not shown when
322 // created initially)
329 // ----------------------------------------------------------------------------
331 // ----------------------------------------------------------------------------
333 MyFrame::MyFrame(const wxString
& title
)
334 : wxFrame((wxFrame
*)NULL
, wxID_ANY
, title
,
335 wxDefaultPosition
, wxSize(250, 150)) // small frame
337 wxMenu
*menuFile
= new wxMenu
;
338 menuFile
->Append(Wizard_Run
, _T("&Run wizard...\tCtrl-R"));
339 menuFile
->AppendSeparator();
340 menuFile
->Append(Wizard_Quit
, _T("E&xit\tAlt-X"), _T("Quit this program"));
342 wxMenu
*helpMenu
= new wxMenu
;
343 helpMenu
->Append(Wizard_About
, _T("&About...\tF1"), _T("Show about dialog"));
345 // now append the freshly created menu to the menu bar...
346 wxMenuBar
*menuBar
= new wxMenuBar();
347 menuBar
->Append(menuFile
, _T("&File"));
348 menuBar
->Append(helpMenu
, _T("&Help"));
350 // ... and attach this menu bar to the frame
353 // also create status bar which we use in OnWizardCancel
356 #endif // wxUSE_STATUSBAR
359 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
361 // true is to force the frame to close
365 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
367 wxMessageBox(_T("Demo of wxWizard class\n")
368 _T("(c) 1999, 2000 Vadim Zeitlin"),
369 _T("About wxWizard sample"), wxOK
| wxICON_INFORMATION
, this);
372 void MyFrame::OnRunWizard(wxCommandEvent
& WXUNUSED(event
))
374 wxWizard
*wizard
= new wxWizard(this, wxID_ANY
,
375 _T("Absolutely Useless Wizard"),
378 wxDEFAULT_DIALOG_STYLE
| wxRESIZE_BORDER
);
380 // a wizard page may be either an object of predefined class
381 wxWizardPageSimple
*page1
= new wxWizardPageSimple(wizard
);
383 /* wxStaticText *text = */ new wxStaticText(page1
, wxID_ANY
,
384 _T("This wizard doesn't help you\nto do anything at all.\n")
386 _T("The next pages will present you\nwith more useless controls."),
390 // ... or a derived class
391 wxRadioboxPage
*page3
= new wxRadioboxPage(wizard
);
392 wxValidationPage
*page4
= new wxValidationPage(wizard
);
394 // set the page order using a convenience function - could also use
395 // SetNext/Prev directly as below
396 wxWizardPageSimple::Chain(page3
, page4
);
398 // this page is not a wxWizardPageSimple, so we use SetNext/Prev to insert
399 // it into the chain of pages
400 wxCheckboxPage
*page2
= new wxCheckboxPage(wizard
, page1
, page3
);
401 page1
->SetNext(page2
);
402 page3
->SetPrev(page2
);
404 // allow the wizard to size itself around the pages
405 wizard
->GetPageAreaSizer()->Add(page1
);
407 if ( wizard
->RunWizard(page1
) )
409 wxMessageBox(_T("The wizard successfully completed"), _T("That's all"),
410 wxICON_INFORMATION
| wxOK
);
416 void MyFrame::OnWizardFinished(wxWizardEvent
& WXUNUSED(event
))
418 wxLogStatus(this, wxT("The wizard finished successfully."));
421 void MyFrame::OnWizardCancel(wxWizardEvent
& WXUNUSED(event
))
423 wxLogStatus(this, wxT("The wizard was cancelled."));