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