]>
git.saurik.com Git - wxWidgets.git/blob - samples/wizard/wizard.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxWindows 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/msgdlg.h"
34 #include "wx/radiobox.h"
39 #include "wx/wizard.h"
42 #include "wiztest.xpm"
43 #include "wiztest2.xpm"
46 // ----------------------------------------------------------------------------
48 // ----------------------------------------------------------------------------
58 // ----------------------------------------------------------------------------
60 // ----------------------------------------------------------------------------
62 // Define a new application type, each program should derive a class from wxApp
63 class MyApp
: public wxApp
66 // override base class virtuals
67 virtual bool OnInit();
70 class MyFrame
: public wxFrame
74 MyFrame(const wxString
& title
);
76 // event handlers (these functions should _not_ be virtual)
77 void OnQuit(wxCommandEvent
& event
);
78 void OnAbout(wxCommandEvent
& event
);
79 void OnRunWizard(wxCommandEvent
& event
);
80 void OnWizardCancel(wxWizardEvent
& event
);
83 // any class wishing to process wxWindows events must use this macro
87 // ----------------------------------------------------------------------------
88 // some pages for our wizard
89 // ----------------------------------------------------------------------------
91 // this shows how to simply control the validity of the user input by just
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
96 // it also shows how to use a different bitmap for one of the pages
97 class wxValidationPage
: public wxWizardPageSimple
100 wxValidationPage(wxWizard
*parent
) : wxWizardPageSimple(parent
)
102 m_bitmap
= wxBITMAP(wiztest2
);
104 m_checkbox
= new wxCheckBox(this, -1, _T("&Check me"));
106 wxBoxSizer
*mainSizer
= new wxBoxSizer(wxVERTICAL
);
108 new wxStaticText(this, -1,
109 _T("You need to check the checkbox\n")
110 _T("below before going to the next page\n")),
123 mainSizer
->Fit(this);
126 virtual bool TransferDataFromWindow()
128 if ( !m_checkbox
->GetValue() )
130 wxMessageBox(_T("Check the checkbox first!"), _T("No way"),
131 wxICON_WARNING
| wxOK
, this);
140 wxCheckBox
*m_checkbox
;
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
149 // directions in which we allow the user to proceed from this page
152 Forward
, Backward
, Both
, Neither
155 wxRadioboxPage(wxWizard
*parent
) : wxWizardPageSimple(parent
)
157 // should correspond to the enum above
158 // static wxString choices[] = { "forward", "backward", "both", "neither" };
159 // The above syntax can cause an internal compiler error with gcc.
161 choices
[0] = _T("forward");
162 choices
[1] = _T("backward");
163 choices
[2] = _T("both");
164 choices
[3] = _T("neither");
166 m_radio
= new wxRadioBox(this, -1, _T("Allow to proceed:"),
167 wxDefaultPosition
, wxDefaultSize
,
168 WXSIZEOF(choices
), choices
,
169 1, wxRA_SPECIFY_COLS
);
170 m_radio
->SetSelection(Both
);
172 wxBoxSizer
*mainSizer
= new wxBoxSizer(wxVERTICAL
);
180 mainSizer
->Fit(this);
183 // wizard event handlers
184 void OnWizardCancel(wxWizardEvent
& event
)
186 if ( wxMessageBox(_T("Do you really want to cancel?"), _T("Question"),
187 wxICON_QUESTION
| wxYES_NO
, this) != wxYES
)
194 void OnWizardPageChanging(wxWizardEvent
& event
)
196 int sel
= m_radio
->GetSelection();
201 if ( event
.GetDirection() && sel
== Forward
)
204 if ( !event
.GetDirection() && sel
== Backward
)
207 wxMessageBox(_T("You can't go there"), _T("Not allowed"),
208 wxICON_WARNING
| wxOK
, this);
216 DECLARE_EVENT_TABLE()
219 // this shows how to dynamically (i.e. during run-time) arrange the page order
220 class wxCheckboxPage
: public wxWizardPage
223 wxCheckboxPage(wxWizard
*parent
,
226 : wxWizardPage(parent
)
231 wxBoxSizer
*mainSizer
= new wxBoxSizer(wxVERTICAL
);
234 new wxStaticText(this, -1, _T("Try checking the box below and\n")
235 _T("then going back and clearing it")),
236 0, // No vertical stretching
241 m_checkbox
= new wxCheckBox(this, -1, _T("&Skip the next page"));
244 0, // No vertical stretching
250 mainSizer
->Fit(this);
253 // implement wxWizardPage functions
254 virtual wxWizardPage
*GetPrev() const { return m_prev
; }
255 virtual wxWizardPage
*GetNext() const
257 return m_checkbox
->GetValue() ? m_next
->GetNext() : m_next
;
261 wxWizardPage
*m_prev
,
264 wxCheckBox
*m_checkbox
;
267 // ============================================================================
269 // ============================================================================
271 // ----------------------------------------------------------------------------
272 // event tables and such
273 // ----------------------------------------------------------------------------
275 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
276 EVT_MENU(Wizard_Quit
, MyFrame::OnQuit
)
277 EVT_MENU(Wizard_About
, MyFrame::OnAbout
)
278 EVT_MENU(Wizard_Run
, MyFrame::OnRunWizard
)
280 EVT_WIZARD_CANCEL(-1, MyFrame::OnWizardCancel
)
283 BEGIN_EVENT_TABLE(wxRadioboxPage
, wxWizardPageSimple
)
284 EVT_WIZARD_PAGE_CHANGING(-1, wxRadioboxPage::OnWizardPageChanging
)
285 EVT_WIZARD_CANCEL(-1, wxRadioboxPage::OnWizardCancel
)
290 // ----------------------------------------------------------------------------
291 // the application class
292 // ----------------------------------------------------------------------------
294 // `Main program' equivalent: the program execution "starts" here
297 MyFrame
*frame
= new MyFrame(_T("wxWizard Sample"));
299 // and show it (the frames, unlike simple controls, are not shown when
300 // created initially)
307 // ----------------------------------------------------------------------------
309 // ----------------------------------------------------------------------------
311 MyFrame::MyFrame(const wxString
& title
)
312 : wxFrame((wxFrame
*)NULL
, -1, title
,
313 wxDefaultPosition
, wxSize(250, 150)) // small frame
315 wxMenu
*menuFile
= new wxMenu
;
316 menuFile
->Append(Wizard_Run
, _T("&Run wizard...\tCtrl-R"));
317 menuFile
->AppendSeparator();
318 menuFile
->Append(Wizard_Quit
, _T("E&xit\tAlt-X"), _T("Quit this program"));
320 wxMenu
*helpMenu
= new wxMenu
;
321 helpMenu
->Append(Wizard_About
, _T("&About...\tF1"), _T("Show about dialog"));
323 // now append the freshly created menu to the menu bar...
324 wxMenuBar
*menuBar
= new wxMenuBar();
325 menuBar
->Append(menuFile
, _T("&File"));
326 menuBar
->Append(helpMenu
, _T("&Help"));
328 // ... and attach this menu bar to the frame
331 // also create status bar which we use in OnWizardCancel
335 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
337 // TRUE is to force the frame to close
341 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
343 wxMessageBox(_T("Demo of wxWizard class\n")
344 _T("© 1999, 2000 Vadim Zeitlin"),
345 _T("About wxWizard sample"), wxOK
| wxICON_INFORMATION
, this);
348 void MyFrame::OnRunWizard(wxCommandEvent
& WXUNUSED(event
))
350 wxWizard
*wizard
= new wxWizard(this, -1,
351 _T("Absolutely Useless Wizard"),
354 wxDEFAULT_DIALOG_STYLE
| wxRESIZE_BORDER
);
356 // a wizard page may be either an object of predefined class
357 wxWizardPageSimple
*page1
= new wxWizardPageSimple(wizard
);
358 wxStaticText
*text
= new wxStaticText(page1
, -1,
359 _T("This wizard doesn't help you\nto do anything at all.\n")
361 _T("The next pages will present you\nwith more useless controls."),
364 wxSize size
= text
->GetBestSize();
366 // ... or a derived class
367 wxRadioboxPage
*page3
= new wxRadioboxPage(wizard
);
368 wxValidationPage
*page4
= new wxValidationPage(wizard
);
370 // set the page order using a convenience function - could also use
371 // SetNext/Prev directly as below
372 wxWizardPageSimple::Chain(page3
, page4
);
374 // this page is not a wxWizardPageSimple, so we use SetNext/Prev to insert
375 // it into the chain of pages
376 wxCheckboxPage
*page2
= new wxCheckboxPage(wizard
, page1
, page3
);
377 page1
->SetNext(page2
);
378 page3
->SetPrev(page2
);
380 wizard
->SetPageSize(size
);
381 wizard
->GetPageAreaSizer()->Add(page1
);
383 if ( wizard
->RunWizard(page1
) )
385 wxMessageBox(_T("The wizard successfully completed"), _T("That's all"),
386 wxICON_INFORMATION
| wxOK
);
392 void MyFrame::OnWizardCancel(wxWizardEvent
& WXUNUSED(event
))
394 wxLogStatus(this, wxT("The wizard was cancelled."));