]>
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"
42 #include "wiztest.xpm"
43 #include "wiztest2.xpm"
45 // ----------------------------------------------------------------------------
47 // ----------------------------------------------------------------------------
52 Wizard_Quit
= wxID_EXIT
,
53 Wizard_RunModal
= wxID_HIGHEST
,
56 Wizard_About
= wxID_ABOUT
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 OnRunWizardNoSizer(wxCommandEvent
& event
);
82 void OnRunWizardModeless(wxCommandEvent
& event
);
83 void OnWizardCancel(wxWizardEvent
& event
);
84 void OnWizardFinished(wxWizardEvent
& event
);
87 // any class wishing to process wxWidgets events must use this macro
91 // ----------------------------------------------------------------------------
93 // ----------------------------------------------------------------------------
95 class MyWizard
: public wxWizard
98 MyWizard(wxFrame
*frame
, bool useSizer
= true);
100 wxWizardPage
*GetFirstPage() const { return m_page1
; }
103 wxWizardPageSimple
*m_page1
;
106 // ----------------------------------------------------------------------------
107 // some pages for our wizard
108 // ----------------------------------------------------------------------------
110 // This shows how to simply control the validity of the user input by just
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
115 // It also shows how to use a different bitmap for one of the pages.
116 class wxValidationPage
: public wxWizardPageSimple
119 wxValidationPage(wxWizard
*parent
) : wxWizardPageSimple(parent
)
121 m_bitmap
= wxBitmap(wiztest2_xpm
);
123 m_checkbox
= new wxCheckBox(this, wxID_ANY
, _T("&Check me"));
125 wxBoxSizer
*mainSizer
= new wxBoxSizer(wxVERTICAL
);
127 new wxStaticText(this, wxID_ANY
,
128 _T("You need to check the checkbox\n")
129 _T("below before going to the next page\n")),
142 mainSizer
->Fit(this);
145 virtual bool TransferDataFromWindow()
147 if ( !m_checkbox
->GetValue() )
149 wxMessageBox(_T("Check the checkbox first!"), _T("No way"),
150 wxICON_WARNING
| wxOK
, this);
159 wxCheckBox
*m_checkbox
;
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
168 // directions in which we allow the user to proceed from this page
171 Forward
, Backward
, Both
, Neither
174 wxRadioboxPage(wxWizard
*parent
) : wxWizardPageSimple(parent
)
176 // should correspond to the enum above
177 // static wxString choices[] = { "forward", "backward", "both", "neither" };
178 // The above syntax can cause an internal compiler error with gcc.
180 choices
[0] = _T("forward");
181 choices
[1] = _T("backward");
182 choices
[2] = _T("both");
183 choices
[3] = _T("neither");
185 m_radio
= new wxRadioBox(this, wxID_ANY
, _T("Allow to proceed:"),
186 wxDefaultPosition
, wxDefaultSize
,
187 WXSIZEOF(choices
), choices
,
188 1, wxRA_SPECIFY_COLS
);
189 m_radio
->SetSelection(Both
);
191 wxBoxSizer
*mainSizer
= new wxBoxSizer(wxVERTICAL
);
200 mainSizer
->Fit(this);
203 // wizard event handlers
204 void OnWizardCancel(wxWizardEvent
& event
)
206 if ( wxMessageBox(_T("Do you really want to cancel?"), _T("Question"),
207 wxICON_QUESTION
| wxYES_NO
, this) != wxYES
)
214 void OnWizardPageChanging(wxWizardEvent
& event
)
216 int sel
= m_radio
->GetSelection();
221 if ( event
.GetDirection() && sel
== Forward
)
224 if ( !event
.GetDirection() && sel
== Backward
)
227 wxMessageBox(_T("You can't go there"), _T("Not allowed"),
228 wxICON_WARNING
| wxOK
, this);
236 DECLARE_EVENT_TABLE()
239 // This shows how to dynamically (i.e. during run-time) arrange the page order.
240 class wxCheckboxPage
: public wxWizardPage
243 wxCheckboxPage(wxWizard
*parent
,
246 : wxWizardPage(parent
)
251 wxBoxSizer
*mainSizer
= new wxBoxSizer(wxVERTICAL
);
254 new wxStaticText(this, wxID_ANY
, _T("Try checking the box below and\n")
255 _T("then going back and clearing it")),
256 0, // No vertical stretching
261 m_checkbox
= new wxCheckBox(this, wxID_ANY
, _T("&Skip the next page"));
264 0, // No vertical stretching
269 #if wxUSE_CHECKLISTBOX
270 static const wxChar
*aszChoices
[] =
284 m_checklistbox
= new wxCheckListBox
290 wxArrayString(WXSIZEOF(aszChoices
), aszChoices
)
295 0, // No vertical stretching
299 #endif // wxUSE_CHECKLISTBOX
302 mainSizer
->Fit(this);
305 // implement wxWizardPage functions
306 virtual wxWizardPage
*GetPrev() const { return m_prev
; }
307 virtual wxWizardPage
*GetNext() const
309 return m_checkbox
->GetValue() ? m_next
->GetNext() : m_next
;
313 wxWizardPage
*m_prev
,
316 wxCheckBox
*m_checkbox
;
317 #if wxUSE_CHECKLISTBOX
318 wxCheckListBox
*m_checklistbox
;
322 // ============================================================================
324 // ============================================================================
326 // ----------------------------------------------------------------------------
327 // event tables and such
328 // ----------------------------------------------------------------------------
330 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
331 EVT_MENU(Wizard_Quit
, MyFrame::OnQuit
)
332 EVT_MENU(Wizard_About
, MyFrame::OnAbout
)
333 EVT_MENU(Wizard_RunModal
, MyFrame::OnRunWizard
)
334 EVT_MENU(Wizard_RunNoSizer
, MyFrame::OnRunWizardNoSizer
)
335 EVT_MENU(Wizard_RunModeless
, MyFrame::OnRunWizardModeless
)
337 EVT_WIZARD_CANCEL(wxID_ANY
, MyFrame::OnWizardCancel
)
338 EVT_WIZARD_FINISHED(wxID_ANY
, MyFrame::OnWizardFinished
)
341 BEGIN_EVENT_TABLE(wxRadioboxPage
, wxWizardPageSimple
)
342 EVT_WIZARD_PAGE_CHANGING(wxID_ANY
, wxRadioboxPage::OnWizardPageChanging
)
343 EVT_WIZARD_CANCEL(wxID_ANY
, wxRadioboxPage::OnWizardCancel
)
348 // ----------------------------------------------------------------------------
349 // the application class
350 // ----------------------------------------------------------------------------
352 // `Main program' equivalent: the program execution "starts" here
355 MyFrame
*frame
= new MyFrame(_T("wxWizard Sample"));
357 // and show it (the frames, unlike simple controls, are not shown when
358 // created initially)
365 // ----------------------------------------------------------------------------
367 // ----------------------------------------------------------------------------
369 MyWizard::MyWizard(wxFrame
*frame
, bool useSizer
)
370 : wxWizard(frame
,wxID_ANY
,_T("Absolutely Useless Wizard"),
371 wxBitmap(wiztest_xpm
),wxDefaultPosition
,
372 wxDEFAULT_DIALOG_STYLE
| wxRESIZE_BORDER
)
374 // a wizard page may be either an object of predefined class
375 m_page1
= new wxWizardPageSimple(this);
377 /* wxStaticText *text = */ new wxStaticText(m_page1
, wxID_ANY
,
378 _T("This wizard doesn't help you\nto do anything at all.\n")
380 _T("The next pages will present you\nwith more useless controls."),
384 // ... or a derived class
385 wxRadioboxPage
*page3
= new wxRadioboxPage(this);
386 wxValidationPage
*page4
= new wxValidationPage(this);
388 // set the page order using a convenience function - could also use
389 // SetNext/Prev directly as below
390 wxWizardPageSimple::Chain(page3
, page4
);
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
);
400 // allow the wizard to size itself around the pages
401 GetPageAreaSizer()->Add(m_page1
);
405 // ----------------------------------------------------------------------------
407 // ----------------------------------------------------------------------------
409 MyFrame::MyFrame(const wxString
& title
)
410 :wxFrame((wxFrame
*)NULL
, wxID_ANY
, title
,
411 wxDefaultPosition
, wxSize(250, 150)) // small frame
413 wxMenu
*menuFile
= new wxMenu
;
414 menuFile
->Append(Wizard_RunModal
, _T("&Run wizard modal...\tCtrl-R"));
415 menuFile
->Append(Wizard_RunNoSizer
, _T("Run wizard &without sizer..."));
416 menuFile
->Append(Wizard_RunModeless
, _T("Run wizard &modeless..."));
417 menuFile
->AppendSeparator();
418 menuFile
->Append(Wizard_Quit
, _T("E&xit\tAlt-X"), _T("Quit this program"));
420 wxMenu
*helpMenu
= new wxMenu
;
421 helpMenu
->Append(Wizard_About
, _T("&About...\tF1"), _T("Show about dialog"));
423 // now append the freshly created menu to the menu bar...
424 wxMenuBar
*menuBar
= new wxMenuBar();
425 menuBar
->Append(menuFile
, _T("&File"));
426 menuBar
->Append(helpMenu
, _T("&Help"));
428 // ... and attach this menu bar to the frame
431 // also create status bar which we use in OnWizardCancel
434 #endif // wxUSE_STATUSBAR
437 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
439 // true is to force the frame to close
443 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
445 wxMessageBox(_T("Demo of wxWizard class\n")
446 _T("(c) 1999, 2000 Vadim Zeitlin"),
447 _T("About wxWizard sample"), wxOK
| wxICON_INFORMATION
, this);
450 void MyFrame::OnRunWizard(wxCommandEvent
& WXUNUSED(event
))
452 MyWizard
wizard(this);
454 wizard
.RunWizard(wizard
.GetFirstPage());
457 void MyFrame::OnRunWizardNoSizer(wxCommandEvent
& WXUNUSED(event
))
459 MyWizard
wizard(this, false);
461 wizard
.RunWizard(wizard
.GetFirstPage());
464 void MyFrame::OnRunWizardModeless(wxCommandEvent
& WXUNUSED(event
))
466 MyWizard
*wizard
= new MyWizard(this);
467 wizard
->ShowPage(wizard
->GetFirstPage());
471 void MyFrame::OnWizardFinished(wxWizardEvent
& WXUNUSED(event
))
473 wxMessageBox(wxT("The wizard finished successfully."), wxT("Wizard notification"));
476 void MyFrame::OnWizardCancel(wxWizardEvent
& WXUNUSED(event
))
478 wxMessageBox(wxT("The wizard was cancelled."), wxT("Wizard notification"));