]>
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
);
81 void OnWizardFinished(wxWizardEvent
& event
);
84 // any class wishing to process wxWindows events must use this macro
88 // ----------------------------------------------------------------------------
89 // some pages for our wizard
90 // ----------------------------------------------------------------------------
92 // this shows how to simply control the validity of the user input by just
93 // overriding TransferDataFromWindow() - of course, in a real program, the
94 // check wouldn't be so trivial and the data will be probably saved somewhere
97 // it also shows how to use a different bitmap for one of the pages
98 class wxValidationPage
: public wxWizardPageSimple
101 wxValidationPage(wxWizard
*parent
) : wxWizardPageSimple(parent
)
103 m_bitmap
= wxBITMAP(wiztest2
);
105 m_checkbox
= new wxCheckBox(this, -1, _T("&Check me"));
107 wxBoxSizer
*mainSizer
= new wxBoxSizer(wxVERTICAL
);
109 new wxStaticText(this, -1,
110 _T("You need to check the checkbox\n")
111 _T("below before going to the next page\n")),
124 mainSizer
->Fit(this);
127 virtual bool TransferDataFromWindow()
129 if ( !m_checkbox
->GetValue() )
131 wxMessageBox(_T("Check the checkbox first!"), _T("No way"),
132 wxICON_WARNING
| wxOK
, this);
141 wxCheckBox
*m_checkbox
;
144 // This is a more complicated example of validity checking: using events we may
145 // allow to return to the previous page, but not to proceed. It also
146 // demonstrates how to intercept [Cancel] button press.
147 class wxRadioboxPage
: public wxWizardPageSimple
150 // directions in which we allow the user to proceed from this page
153 Forward
, Backward
, Both
, Neither
156 wxRadioboxPage(wxWizard
*parent
) : wxWizardPageSimple(parent
)
158 // should correspond to the enum above
159 // static wxString choices[] = { "forward", "backward", "both", "neither" };
160 // The above syntax can cause an internal compiler error with gcc.
162 choices
[0] = _T("forward");
163 choices
[1] = _T("backward");
164 choices
[2] = _T("both");
165 choices
[3] = _T("neither");
167 m_radio
= new wxRadioBox(this, -1, _T("Allow to proceed:"),
168 wxDefaultPosition
, wxDefaultSize
,
169 WXSIZEOF(choices
), choices
,
170 1, wxRA_SPECIFY_COLS
);
171 m_radio
->SetSelection(Both
);
173 wxBoxSizer
*mainSizer
= new wxBoxSizer(wxVERTICAL
);
181 mainSizer
->Fit(this);
184 // wizard event handlers
185 void OnWizardCancel(wxWizardEvent
& event
)
187 if ( wxMessageBox(_T("Do you really want to cancel?"), _T("Question"),
188 wxICON_QUESTION
| wxYES_NO
, this) != wxYES
)
195 void OnWizardPageChanging(wxWizardEvent
& event
)
197 int sel
= m_radio
->GetSelection();
202 if ( event
.GetDirection() && sel
== Forward
)
205 if ( !event
.GetDirection() && sel
== Backward
)
208 wxMessageBox(_T("You can't go there"), _T("Not allowed"),
209 wxICON_WARNING
| wxOK
, this);
217 DECLARE_EVENT_TABLE()
220 // this shows how to dynamically (i.e. during run-time) arrange the page order
221 class wxCheckboxPage
: public wxWizardPage
224 wxCheckboxPage(wxWizard
*parent
,
227 : wxWizardPage(parent
)
232 wxBoxSizer
*mainSizer
= new wxBoxSizer(wxVERTICAL
);
235 new wxStaticText(this, -1, _T("Try checking the box below and\n")
236 _T("then going back and clearing it")),
237 0, // No vertical stretching
242 m_checkbox
= new wxCheckBox(this, -1, _T("&Skip the next page"));
245 0, // No vertical stretching
251 mainSizer
->Fit(this);
254 // implement wxWizardPage functions
255 virtual wxWizardPage
*GetPrev() const { return m_prev
; }
256 virtual wxWizardPage
*GetNext() const
258 return m_checkbox
->GetValue() ? m_next
->GetNext() : m_next
;
262 wxWizardPage
*m_prev
,
265 wxCheckBox
*m_checkbox
;
268 // ============================================================================
270 // ============================================================================
272 // ----------------------------------------------------------------------------
273 // event tables and such
274 // ----------------------------------------------------------------------------
276 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
277 EVT_MENU(Wizard_Quit
, MyFrame::OnQuit
)
278 EVT_MENU(Wizard_About
, MyFrame::OnAbout
)
279 EVT_MENU(Wizard_Run
, MyFrame::OnRunWizard
)
281 EVT_WIZARD_CANCEL(-1, MyFrame::OnWizardCancel
)
282 EVT_WIZARD_FINISHED(-1, MyFrame::OnWizardFinished
)
285 BEGIN_EVENT_TABLE(wxRadioboxPage
, wxWizardPageSimple
)
286 EVT_WIZARD_PAGE_CHANGING(-1, wxRadioboxPage::OnWizardPageChanging
)
287 EVT_WIZARD_CANCEL(-1, wxRadioboxPage::OnWizardCancel
)
292 // ----------------------------------------------------------------------------
293 // the application class
294 // ----------------------------------------------------------------------------
296 // `Main program' equivalent: the program execution "starts" here
299 MyFrame
*frame
= new MyFrame(_T("wxWizard Sample"));
301 // and show it (the frames, unlike simple controls, are not shown when
302 // created initially)
309 // ----------------------------------------------------------------------------
311 // ----------------------------------------------------------------------------
313 MyFrame::MyFrame(const wxString
& title
)
314 : wxFrame((wxFrame
*)NULL
, -1, title
,
315 wxDefaultPosition
, wxSize(250, 150)) // small frame
317 wxMenu
*menuFile
= new wxMenu
;
318 menuFile
->Append(Wizard_Run
, _T("&Run wizard...\tCtrl-R"));
319 menuFile
->AppendSeparator();
320 menuFile
->Append(Wizard_Quit
, _T("E&xit\tAlt-X"), _T("Quit this program"));
322 wxMenu
*helpMenu
= new wxMenu
;
323 helpMenu
->Append(Wizard_About
, _T("&About...\tF1"), _T("Show about dialog"));
325 // now append the freshly created menu to the menu bar...
326 wxMenuBar
*menuBar
= new wxMenuBar();
327 menuBar
->Append(menuFile
, _T("&File"));
328 menuBar
->Append(helpMenu
, _T("&Help"));
330 // ... and attach this menu bar to the frame
333 // also create status bar which we use in OnWizardCancel
337 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
339 // TRUE is to force the frame to close
343 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
345 wxMessageBox(_T("Demo of wxWizard class\n")
346 _T("© 1999, 2000 Vadim Zeitlin"),
347 _T("About wxWizard sample"), wxOK
| wxICON_INFORMATION
, this);
350 void MyFrame::OnRunWizard(wxCommandEvent
& WXUNUSED(event
))
352 wxWizard
*wizard
= new wxWizard(this, -1,
353 _T("Absolutely Useless Wizard"),
356 wxDEFAULT_DIALOG_STYLE
| wxRESIZE_BORDER
);
358 // a wizard page may be either an object of predefined class
359 wxWizardPageSimple
*page1
= new wxWizardPageSimple(wizard
);
360 wxStaticText
*text
= new wxStaticText(page1
, -1,
361 _T("This wizard doesn't help you\nto do anything at all.\n")
363 _T("The next pages will present you\nwith more useless controls."),
366 wxSize size
= text
->GetBestSize();
368 // ... or a derived class
369 wxRadioboxPage
*page3
= new wxRadioboxPage(wizard
);
370 wxValidationPage
*page4
= new wxValidationPage(wizard
);
372 // set the page order using a convenience function - could also use
373 // SetNext/Prev directly as below
374 wxWizardPageSimple::Chain(page3
, page4
);
376 // this page is not a wxWizardPageSimple, so we use SetNext/Prev to insert
377 // it into the chain of pages
378 wxCheckboxPage
*page2
= new wxCheckboxPage(wizard
, page1
, page3
);
379 page1
->SetNext(page2
);
380 page3
->SetPrev(page2
);
382 wizard
->SetPageSize(size
);
383 wizard
->GetPageAreaSizer()->Add(page1
);
385 if ( wizard
->RunWizard(page1
) )
387 wxMessageBox(_T("The wizard successfully completed"), _T("That's all"),
388 wxICON_INFORMATION
| wxOK
);
394 void MyFrame::OnWizardFinished(wxWizardEvent
& WXUNUSED(event
))
396 wxLogStatus(this, wxT("The wizard finished successfully."));
399 void MyFrame::OnWizardCancel(wxWizardEvent
& WXUNUSED(event
))
401 wxLogStatus(this, wxT("The wizard was cancelled."));