]>
Commit | Line | Data |
---|---|---|
66cd017c | 1 | ///////////////////////////////////////////////////////////////////////////// |
13f5935c | 2 | // Name: wizard.cpp |
be5a51fb | 3 | // Purpose: wxWidgets sample demonstrating wxWizard control |
66cd017c | 4 | // Author: Vadim Zeitlin |
07f20d9a | 5 | // Modified by: Robert Vazan (sizers) |
66cd017c VZ |
6 | // Created: 15.08.99 |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Vadim Zeitlin | |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | // ============================================================================ | |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
66cd017c VZ |
20 | // For compilers that support precompilation, includes "wx/wx.h". |
21 | #include "wx/wxprec.h" | |
22 | ||
23 | #ifdef __BORLANDC__ | |
24 | #pragma hdrstop | |
25 | #endif | |
26 | ||
07f20d9a | 27 | // for all others, include the necessary headers |
66cd017c | 28 | #ifndef WX_PRECOMP |
07f20d9a VZ |
29 | #include "wx/stattext.h" |
30 | #include "wx/log.h" | |
31 | #include "wx/app.h" | |
32 | #include "wx/checkbox.h" | |
f2e93537 | 33 | #include "wx/checklst.h" |
07f20d9a VZ |
34 | #include "wx/msgdlg.h" |
35 | #include "wx/radiobox.h" | |
36 | #include "wx/menu.h" | |
37 | #include "wx/sizer.h" | |
66cd017c VZ |
38 | #endif |
39 | ||
40 | #include "wx/wizard.h" | |
41 | ||
77ba7af1 JS |
42 | #include "wiztest.xpm" |
43 | #include "wiztest2.xpm" | |
b87654f3 | 44 | |
d93c719a VZ |
45 | // ---------------------------------------------------------------------------- |
46 | // constants | |
47 | // ---------------------------------------------------------------------------- | |
48 | ||
49 | // ids for menu items | |
50 | enum | |
51 | { | |
52 | Wizard_Quit = 100, | |
53 | Wizard_Run, | |
54 | Wizard_About = 1000 | |
55 | }; | |
56 | ||
66cd017c VZ |
57 | // ---------------------------------------------------------------------------- |
58 | // private classes | |
59 | // ---------------------------------------------------------------------------- | |
60 | ||
61 | // Define a new application type, each program should derive a class from wxApp | |
62 | class MyApp : public wxApp | |
63 | { | |
64 | public: | |
65 | // override base class virtuals | |
66 | virtual bool OnInit(); | |
67 | }; | |
68 | ||
d93c719a VZ |
69 | class MyFrame : public wxFrame |
70 | { | |
71 | public: | |
72 | // ctor(s) | |
73 | MyFrame(const wxString& title); | |
74 | ||
75 | // event handlers (these functions should _not_ be virtual) | |
76 | void OnQuit(wxCommandEvent& event); | |
77 | void OnAbout(wxCommandEvent& event); | |
78 | void OnRunWizard(wxCommandEvent& event); | |
79 | void OnWizardCancel(wxWizardEvent& event); | |
8a7dfb14 | 80 | void OnWizardFinished(wxWizardEvent& event); |
d93c719a VZ |
81 | |
82 | private: | |
be5a51fb | 83 | // any class wishing to process wxWidgets events must use this macro |
d93c719a VZ |
84 | DECLARE_EVENT_TABLE() |
85 | }; | |
66cd017c VZ |
86 | |
87 | // ---------------------------------------------------------------------------- | |
88 | // some pages for our wizard | |
89 | // ---------------------------------------------------------------------------- | |
90 | ||
436aae91 | 91 | // This shows how to simply control the validity of the user input by just |
66cd017c VZ |
92 | // overriding TransferDataFromWindow() - of course, in a real program, the |
93 | // check wouldn't be so trivial and the data will be probably saved somewhere | |
436aae91 | 94 | // too. |
d93c719a | 95 | // |
436aae91 | 96 | // It also shows how to use a different bitmap for one of the pages. |
74b31181 | 97 | class wxValidationPage : public wxWizardPageSimple |
66cd017c VZ |
98 | { |
99 | public: | |
74b31181 | 100 | wxValidationPage(wxWizard *parent) : wxWizardPageSimple(parent) |
66cd017c | 101 | { |
77ba7af1 | 102 | m_bitmap = wxBitmap(wiztest2_xpm); |
d93c719a | 103 | |
71572a74 | 104 | m_checkbox = new wxCheckBox(this, wxID_ANY, _T("&Check me")); |
226e5774 | 105 | |
07f20d9a VZ |
106 | wxBoxSizer *mainSizer = new wxBoxSizer(wxVERTICAL); |
107 | mainSizer->Add( | |
71572a74 | 108 | new wxStaticText(this, wxID_ANY, |
07f20d9a VZ |
109 | _T("You need to check the checkbox\n") |
110 | _T("below before going to the next page\n")), | |
111 | 0, | |
112 | wxALL, | |
113 | 5 | |
114 | ); | |
115 | ||
116 | mainSizer->Add( | |
117 | m_checkbox, | |
118 | 0, // No stretching | |
119 | wxALL, | |
120 | 5 // Border | |
121 | ); | |
122 | SetSizer(mainSizer); | |
123 | mainSizer->Fit(this); | |
66cd017c VZ |
124 | } |
125 | ||
126 | virtual bool TransferDataFromWindow() | |
127 | { | |
74b31181 | 128 | if ( !m_checkbox->GetValue() ) |
66cd017c | 129 | { |
9f84eccd | 130 | wxMessageBox(_T("Check the checkbox first!"), _T("No way"), |
b87654f3 | 131 | wxICON_WARNING | wxOK, this); |
66cd017c | 132 | |
71572a74 | 133 | return false; |
66cd017c VZ |
134 | } |
135 | ||
71572a74 | 136 | return true; |
66cd017c VZ |
137 | } |
138 | ||
139 | private: | |
140 | wxCheckBox *m_checkbox; | |
141 | }; | |
142 | ||
74b31181 VZ |
143 | // This is a more complicated example of validity checking: using events we may |
144 | // allow to return to the previous page, but not to proceed. It also | |
145 | // demonstrates how to intercept [Cancel] button press. | |
146 | class wxRadioboxPage : public wxWizardPageSimple | |
147 | { | |
148 | public: | |
149 | // directions in which we allow the user to proceed from this page | |
150 | enum | |
151 | { | |
152 | Forward, Backward, Both, Neither | |
153 | }; | |
154 | ||
155 | wxRadioboxPage(wxWizard *parent) : wxWizardPageSimple(parent) | |
156 | { | |
157 | // should correspond to the enum above | |
dc1efb1d JS |
158 | // static wxString choices[] = { "forward", "backward", "both", "neither" }; |
159 | // The above syntax can cause an internal compiler error with gcc. | |
160 | wxString choices[4]; | |
9f84eccd MB |
161 | choices[0] = _T("forward"); |
162 | choices[1] = _T("backward"); | |
163 | choices[2] = _T("both"); | |
164 | choices[3] = _T("neither"); | |
74b31181 | 165 | |
71572a74 | 166 | m_radio = new wxRadioBox(this, wxID_ANY, _T("Allow to proceed:"), |
07f20d9a | 167 | wxDefaultPosition, wxDefaultSize, |
74b31181 VZ |
168 | WXSIZEOF(choices), choices, |
169 | 1, wxRA_SPECIFY_COLS); | |
170 | m_radio->SetSelection(Both); | |
226e5774 | 171 | |
07f20d9a VZ |
172 | wxBoxSizer *mainSizer = new wxBoxSizer(wxVERTICAL); |
173 | mainSizer->Add( | |
174 | m_radio, | |
175 | 0, // No stretching | |
176 | wxALL, | |
177 | 5 // Border | |
178 | ); | |
226e5774 | 179 | |
07f20d9a VZ |
180 | SetSizer(mainSizer); |
181 | mainSizer->Fit(this); | |
74b31181 VZ |
182 | } |
183 | ||
184 | // wizard event handlers | |
185 | void OnWizardCancel(wxWizardEvent& event) | |
186 | { | |
9f84eccd | 187 | if ( wxMessageBox(_T("Do you really want to cancel?"), _T("Question"), |
74b31181 VZ |
188 | wxICON_QUESTION | wxYES_NO, this) != wxYES ) |
189 | { | |
190 | // not confirmed | |
191 | event.Veto(); | |
192 | } | |
193 | } | |
194 | ||
195 | void OnWizardPageChanging(wxWizardEvent& event) | |
196 | { | |
197 | int sel = m_radio->GetSelection(); | |
198 | ||
199 | if ( sel == Both ) | |
200 | return; | |
201 | ||
202 | if ( event.GetDirection() && sel == Forward ) | |
203 | return; | |
204 | ||
205 | if ( !event.GetDirection() && sel == Backward ) | |
206 | return; | |
207 | ||
9f84eccd | 208 | wxMessageBox(_T("You can't go there"), _T("Not allowed"), |
74b31181 VZ |
209 | wxICON_WARNING | wxOK, this); |
210 | ||
211 | event.Veto(); | |
212 | } | |
213 | ||
214 | private: | |
215 | wxRadioBox *m_radio; | |
216 | ||
217 | DECLARE_EVENT_TABLE() | |
218 | }; | |
219 | ||
436aae91 | 220 | // This shows how to dynamically (i.e. during run-time) arrange the page order. |
74b31181 VZ |
221 | class wxCheckboxPage : public wxWizardPage |
222 | { | |
223 | public: | |
224 | wxCheckboxPage(wxWizard *parent, | |
225 | wxWizardPage *prev, | |
226 | wxWizardPage *next) | |
227 | : wxWizardPage(parent) | |
228 | { | |
229 | m_prev = prev; | |
230 | m_next = next; | |
226e5774 | 231 | |
07f20d9a VZ |
232 | wxBoxSizer *mainSizer = new wxBoxSizer(wxVERTICAL); |
233 | ||
234 | mainSizer->Add( | |
71572a74 | 235 | new wxStaticText(this, wxID_ANY, _T("Try checking the box below and\n") |
07f20d9a VZ |
236 | _T("then going back and clearing it")), |
237 | 0, // No vertical stretching | |
238 | wxALL, | |
239 | 5 // Border width | |
240 | ); | |
74b31181 | 241 | |
71572a74 | 242 | m_checkbox = new wxCheckBox(this, wxID_ANY, _T("&Skip the next page")); |
07f20d9a VZ |
243 | mainSizer->Add( |
244 | m_checkbox, | |
245 | 0, // No vertical stretching | |
246 | wxALL, | |
247 | 5 // Border width | |
248 | ); | |
f2e93537 | 249 | |
653d7a50 | 250 | #if wxUSE_CHECKLISTBOX |
f2e93537 RR |
251 | static const wxChar *aszChoices[] = |
252 | { _T("Zeroth"), _T("First"), _T("Second"), _T("Third"), _T("Fourth"), _T("Fifth"), _T("Sixth"), _T("Seventh"), _T("Eighth"), _T("Nineth") }; | |
253 | wxString *astrChoices = new wxString[WXSIZEOF(aszChoices)]; | |
254 | unsigned int ui; | |
255 | for ( ui = 0; ui < WXSIZEOF(aszChoices); ui++ ) | |
256 | astrChoices[ui] = aszChoices[ui]; | |
653d7a50 | 257 | |
226e5774 | 258 | m_checklistbox = new wxCheckListBox(this, wxID_ANY, wxDefaultPosition, wxSize(100,100), |
f2e93537 | 259 | WXSIZEOF(aszChoices), astrChoices); |
226e5774 | 260 | |
f2e93537 RR |
261 | mainSizer->Add( |
262 | m_checklistbox, | |
263 | 0, // No vertical stretching | |
264 | wxALL, | |
265 | 5 // Border width | |
266 | ); | |
653d7a50 | 267 | #endif // wxUSE_CHECKLISTBOX |
226e5774 | 268 | |
07f20d9a VZ |
269 | SetSizer(mainSizer); |
270 | mainSizer->Fit(this); | |
74b31181 VZ |
271 | } |
272 | ||
273 | // implement wxWizardPage functions | |
274 | virtual wxWizardPage *GetPrev() const { return m_prev; } | |
275 | virtual wxWizardPage *GetNext() const | |
276 | { | |
277 | return m_checkbox->GetValue() ? m_next->GetNext() : m_next; | |
278 | } | |
279 | ||
280 | private: | |
281 | wxWizardPage *m_prev, | |
282 | *m_next; | |
283 | ||
284 | wxCheckBox *m_checkbox; | |
653d7a50 | 285 | #if wxUSE_CHECKLISTBOX |
f2e93537 | 286 | wxCheckListBox *m_checklistbox; |
653d7a50 | 287 | #endif |
74b31181 VZ |
288 | }; |
289 | ||
66cd017c VZ |
290 | // ============================================================================ |
291 | // implementation | |
292 | // ============================================================================ | |
293 | ||
d93c719a VZ |
294 | // ---------------------------------------------------------------------------- |
295 | // event tables and such | |
296 | // ---------------------------------------------------------------------------- | |
297 | ||
298 | BEGIN_EVENT_TABLE(MyFrame, wxFrame) | |
299 | EVT_MENU(Wizard_Quit, MyFrame::OnQuit) | |
300 | EVT_MENU(Wizard_About, MyFrame::OnAbout) | |
301 | EVT_MENU(Wizard_Run, MyFrame::OnRunWizard) | |
302 | ||
71572a74 WS |
303 | EVT_WIZARD_CANCEL(wxID_ANY, MyFrame::OnWizardCancel) |
304 | EVT_WIZARD_FINISHED(wxID_ANY, MyFrame::OnWizardFinished) | |
d93c719a VZ |
305 | END_EVENT_TABLE() |
306 | ||
74b31181 | 307 | BEGIN_EVENT_TABLE(wxRadioboxPage, wxWizardPageSimple) |
71572a74 WS |
308 | EVT_WIZARD_PAGE_CHANGING(wxID_ANY, wxRadioboxPage::OnWizardPageChanging) |
309 | EVT_WIZARD_CANCEL(wxID_ANY, wxRadioboxPage::OnWizardCancel) | |
74b31181 VZ |
310 | END_EVENT_TABLE() |
311 | ||
d93c719a VZ |
312 | IMPLEMENT_APP(MyApp) |
313 | ||
66cd017c VZ |
314 | // ---------------------------------------------------------------------------- |
315 | // the application class | |
316 | // ---------------------------------------------------------------------------- | |
317 | ||
318 | // `Main program' equivalent: the program execution "starts" here | |
319 | bool MyApp::OnInit() | |
320 | { | |
9f84eccd | 321 | MyFrame *frame = new MyFrame(_T("wxWizard Sample")); |
d93c719a VZ |
322 | |
323 | // and show it (the frames, unlike simple controls, are not shown when | |
324 | // created initially) | |
71572a74 | 325 | frame->Show(true); |
d93c719a VZ |
326 | |
327 | // we're done | |
71572a74 | 328 | return true; |
d93c719a VZ |
329 | } |
330 | ||
331 | // ---------------------------------------------------------------------------- | |
332 | // MyFrame | |
333 | // ---------------------------------------------------------------------------- | |
334 | ||
335 | MyFrame::MyFrame(const wxString& title) | |
71572a74 | 336 | : wxFrame((wxFrame *)NULL, wxID_ANY, title, |
d93c719a VZ |
337 | wxDefaultPosition, wxSize(250, 150)) // small frame |
338 | { | |
339 | wxMenu *menuFile = new wxMenu; | |
9f84eccd | 340 | menuFile->Append(Wizard_Run, _T("&Run wizard...\tCtrl-R")); |
d93c719a | 341 | menuFile->AppendSeparator(); |
9f84eccd | 342 | menuFile->Append(Wizard_Quit, _T("E&xit\tAlt-X"), _T("Quit this program")); |
d93c719a VZ |
343 | |
344 | wxMenu *helpMenu = new wxMenu; | |
9f84eccd | 345 | helpMenu->Append(Wizard_About, _T("&About...\tF1"), _T("Show about dialog")); |
d93c719a VZ |
346 | |
347 | // now append the freshly created menu to the menu bar... | |
348 | wxMenuBar *menuBar = new wxMenuBar(); | |
9f84eccd MB |
349 | menuBar->Append(menuFile, _T("&File")); |
350 | menuBar->Append(helpMenu, _T("&Help")); | |
66cd017c | 351 | |
d93c719a VZ |
352 | // ... and attach this menu bar to the frame |
353 | SetMenuBar(menuBar); | |
354 | ||
355 | // also create status bar which we use in OnWizardCancel | |
960a83cc | 356 | #if wxUSE_STATUSBAR |
d93c719a | 357 | CreateStatusBar(); |
960a83cc | 358 | #endif // wxUSE_STATUSBAR |
d93c719a VZ |
359 | } |
360 | ||
361 | void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event)) | |
362 | { | |
71572a74 WS |
363 | // true is to force the frame to close |
364 | Close(true); | |
d93c719a VZ |
365 | } |
366 | ||
367 | void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event)) | |
368 | { | |
9f84eccd | 369 | wxMessageBox(_T("Demo of wxWizard class\n") |
749bfe9a | 370 | _T("(c) 1999, 2000 Vadim Zeitlin"), |
9f84eccd | 371 | _T("About wxWizard sample"), wxOK | wxICON_INFORMATION, this); |
d93c719a VZ |
372 | } |
373 | ||
374 | void MyFrame::OnRunWizard(wxCommandEvent& WXUNUSED(event)) | |
375 | { | |
71572a74 | 376 | wxWizard *wizard = new wxWizard(this, wxID_ANY, |
9f84eccd | 377 | _T("Absolutely Useless Wizard"), |
77ba7af1 | 378 | wxBitmap(wiztest_xpm), |
07f20d9a VZ |
379 | wxDefaultPosition, |
380 | wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER); | |
2f6c54eb | 381 | |
74b31181 VZ |
382 | // a wizard page may be either an object of predefined class |
383 | wxWizardPageSimple *page1 = new wxWizardPageSimple(wizard); | |
226e5774 WS |
384 | |
385 | /* wxStaticText *text = */ new wxStaticText(page1, wxID_ANY, | |
07f20d9a | 386 | _T("This wizard doesn't help you\nto do anything at all.\n") |
9f84eccd | 387 | _T("\n") |
07f20d9a VZ |
388 | _T("The next pages will present you\nwith more useless controls."), |
389 | wxPoint(5,5) | |
f6bcfd97 | 390 | ); |
66cd017c | 391 | |
74b31181 VZ |
392 | // ... or a derived class |
393 | wxRadioboxPage *page3 = new wxRadioboxPage(wizard); | |
394 | wxValidationPage *page4 = new wxValidationPage(wizard); | |
395 | ||
396 | // set the page order using a convenience function - could also use | |
397 | // SetNext/Prev directly as below | |
398 | wxWizardPageSimple::Chain(page3, page4); | |
399 | ||
400 | // this page is not a wxWizardPageSimple, so we use SetNext/Prev to insert | |
401 | // it into the chain of pages | |
402 | wxCheckboxPage *page2 = new wxCheckboxPage(wizard, page1, page3); | |
403 | page1->SetNext(page2); | |
404 | page3->SetPrev(page2); | |
66cd017c | 405 | |
fa513361 | 406 | // allow the wizard to size itself around the pages |
07f20d9a | 407 | wizard->GetPageAreaSizer()->Add(page1); |
226e5774 | 408 | |
74b31181 | 409 | if ( wizard->RunWizard(page1) ) |
66cd017c | 410 | { |
9f84eccd | 411 | wxMessageBox(_T("The wizard successfully completed"), _T("That's all"), |
b87654f3 | 412 | wxICON_INFORMATION | wxOK); |
66cd017c VZ |
413 | } |
414 | ||
415 | wizard->Destroy(); | |
66cd017c | 416 | } |
01dba85a | 417 | |
8a7dfb14 VZ |
418 | void MyFrame::OnWizardFinished(wxWizardEvent& WXUNUSED(event)) |
419 | { | |
420 | wxLogStatus(this, wxT("The wizard finished successfully.")); | |
421 | } | |
422 | ||
d93c719a VZ |
423 | void MyFrame::OnWizardCancel(wxWizardEvent& WXUNUSED(event)) |
424 | { | |
4693b20c | 425 | wxLogStatus(this, wxT("The wizard was cancelled.")); |
d93c719a | 426 | } |