]>
git.saurik.com Git - wxWidgets.git/blob - samples/wizard/wizard.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxWindows sample demonstrating wxWizard control
4 // Author: Vadim Zeitlin
8 // Copyright: (c) Vadim Zeitlin
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
21 #pragma implementation "wizard.cpp"
22 #pragma interface "wizard.cpp"
25 // For compilers that support precompilation, includes "wx/wx.h".
26 #include "wx/wxprec.h"
32 // for all others, include the necessary headers (this file is usually all you
33 // need because it includes almost all "standard" wxWindows headers
38 #include "wx/wizard.h"
41 #include "wiztest.xpm"
42 #include "wiztest2.xpm"
45 // ----------------------------------------------------------------------------
47 // ----------------------------------------------------------------------------
57 // ----------------------------------------------------------------------------
59 // ----------------------------------------------------------------------------
61 // Define a new application type, each program should derive a class from wxApp
62 class MyApp
: public wxApp
65 // override base class virtuals
66 virtual bool OnInit();
69 class MyFrame
: public wxFrame
73 MyFrame(const wxString
& title
);
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
);
82 // any class wishing to process wxWindows events must use this macro
86 // ----------------------------------------------------------------------------
87 // some pages for our wizard
88 // ----------------------------------------------------------------------------
90 // this shows how to simply control the validity of the user input by just
91 // overriding TransferDataFromWindow() - of course, in a real program, the
92 // check wouldn't be so trivial and the data will be probably saved somewhere
95 // it also shows how to use a different bitmap for one of the pages
96 class wxValidationPage
: public wxWizardPageSimple
99 wxValidationPage(wxWizard
*parent
) : wxWizardPageSimple(parent
)
101 m_bitmap
= wxBITMAP(wiztest2
);
103 m_checkbox
= new wxCheckBox(this, -1, "&Check me");
106 virtual bool TransferDataFromWindow()
108 if ( !m_checkbox
->GetValue() )
110 wxMessageBox("Check the checkbox first!", "No way",
111 wxICON_WARNING
| wxOK
, this);
120 wxCheckBox
*m_checkbox
;
123 // This is a more complicated example of validity checking: using events we may
124 // allow to return to the previous page, but not to proceed. It also
125 // demonstrates how to intercept [Cancel] button press.
126 class wxRadioboxPage
: public wxWizardPageSimple
129 // directions in which we allow the user to proceed from this page
132 Forward
, Backward
, Both
, Neither
135 wxRadioboxPage(wxWizard
*parent
) : wxWizardPageSimple(parent
)
137 // should correspond to the enum above
138 // static wxString choices[] = { "forward", "backward", "both", "neither" };
139 // The above syntax can cause an internal compiler error with gcc.
141 choices
[0] = "forward";
142 choices
[1] = "backward";
144 choices
[3] = "neither";
146 m_radio
= new wxRadioBox(this, -1, "Allow to proceed:",
147 wxPoint(5, 5), wxDefaultSize
,
148 WXSIZEOF(choices
), choices
,
149 1, wxRA_SPECIFY_COLS
);
150 m_radio
->SetSelection(Both
);
153 // wizard event handlers
154 void OnWizardCancel(wxWizardEvent
& event
)
156 if ( wxMessageBox("Do you really want to cancel?", "Question",
157 wxICON_QUESTION
| wxYES_NO
, this) != wxYES
)
164 void OnWizardPageChanging(wxWizardEvent
& event
)
166 int sel
= m_radio
->GetSelection();
171 if ( event
.GetDirection() && sel
== Forward
)
174 if ( !event
.GetDirection() && sel
== Backward
)
177 wxMessageBox("You can't go there", "Not allowed",
178 wxICON_WARNING
| wxOK
, this);
186 DECLARE_EVENT_TABLE()
189 // this shows how to dynamically (i.e. during run-time) arrange the page order
190 class wxCheckboxPage
: public wxWizardPage
193 wxCheckboxPage(wxWizard
*parent
,
196 : wxWizardPage(parent
)
201 (void)new wxStaticText(this, -1, "Try checking the box below and\n"
202 "then going back and clearing it");
204 m_checkbox
= new wxCheckBox(this, -1, "&Skip the next page",
208 // implement wxWizardPage functions
209 virtual wxWizardPage
*GetPrev() const { return m_prev
; }
210 virtual wxWizardPage
*GetNext() const
212 return m_checkbox
->GetValue() ? m_next
->GetNext() : m_next
;
216 wxWizardPage
*m_prev
,
219 wxCheckBox
*m_checkbox
;
222 // ============================================================================
224 // ============================================================================
226 // ----------------------------------------------------------------------------
227 // event tables and such
228 // ----------------------------------------------------------------------------
230 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
231 EVT_MENU(Wizard_Quit
, MyFrame::OnQuit
)
232 EVT_MENU(Wizard_About
, MyFrame::OnAbout
)
233 EVT_MENU(Wizard_Run
, MyFrame::OnRunWizard
)
235 EVT_WIZARD_CANCEL(-1, MyFrame::OnWizardCancel
)
238 BEGIN_EVENT_TABLE(wxRadioboxPage
, wxWizardPageSimple
)
239 EVT_WIZARD_PAGE_CHANGING(-1, wxRadioboxPage::OnWizardPageChanging
)
240 EVT_WIZARD_CANCEL(-1, wxRadioboxPage::OnWizardCancel
)
245 // ----------------------------------------------------------------------------
246 // the application class
247 // ----------------------------------------------------------------------------
249 // `Main program' equivalent: the program execution "starts" here
252 MyFrame
*frame
= new MyFrame("wxWizard Sample");
254 // and show it (the frames, unlike simple controls, are not shown when
255 // created initially)
262 // ----------------------------------------------------------------------------
264 // ----------------------------------------------------------------------------
266 MyFrame::MyFrame(const wxString
& title
)
267 : wxFrame((wxFrame
*)NULL
, -1, title
,
268 wxDefaultPosition
, wxSize(250, 150)) // small frame
270 wxMenu
*menuFile
= new wxMenu
;
271 menuFile
->Append(Wizard_Run
, "&Run wizard...\tCtrl-R");
272 menuFile
->AppendSeparator();
273 menuFile
->Append(Wizard_Quit
, "E&xit\tAlt-X", "Quit this program");
275 wxMenu
*helpMenu
= new wxMenu
;
276 helpMenu
->Append(Wizard_About
, "&About...\tF1", "Show about dialog");
278 // now append the freshly created menu to the menu bar...
279 wxMenuBar
*menuBar
= new wxMenuBar();
280 menuBar
->Append(menuFile
, "&File");
281 menuBar
->Append(helpMenu
, "&Help");
283 // ... and attach this menu bar to the frame
286 // also create status bar which we use in OnWizardCancel
290 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
292 // TRUE is to force the frame to close
296 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
298 wxMessageBox("Demo of wxWizard class\n"
299 "© 1999, 2000 Vadim Zeitlin",
300 "About wxWizard sample", wxOK
| wxICON_INFORMATION
, this);
303 void MyFrame::OnRunWizard(wxCommandEvent
& WXUNUSED(event
))
305 wxWizard
*wizard
= new wxWizard(this, -1,
306 "Absolutely Useless Wizard",
309 // a wizard page may be either an object of predefined class
310 wxWizardPageSimple
*page1
= new wxWizardPageSimple(wizard
);
311 wxStaticText
*text
= new wxStaticText(page1
, -1,
312 "This wizard doesn't help you to do anything at all.\n"
314 "The next pages will present you with more useless controls."
316 wxSize size
= text
->GetBestSize();
318 // ... or a derived class
319 wxRadioboxPage
*page3
= new wxRadioboxPage(wizard
);
320 wxValidationPage
*page4
= new wxValidationPage(wizard
);
322 // set the page order using a convenience function - could also use
323 // SetNext/Prev directly as below
324 wxWizardPageSimple::Chain(page3
, page4
);
326 // this page is not a wxWizardPageSimple, so we use SetNext/Prev to insert
327 // it into the chain of pages
328 wxCheckboxPage
*page2
= new wxCheckboxPage(wizard
, page1
, page3
);
329 page1
->SetNext(page2
);
330 page3
->SetPrev(page2
);
332 wizard
->SetPageSize(size
);
333 if ( wizard
->RunWizard(page1
) )
335 wxMessageBox("The wizard successfully completed", "That's all",
336 wxICON_INFORMATION
| wxOK
);
342 void MyFrame::OnWizardCancel(wxWizardEvent
& WXUNUSED(event
))
344 wxLogStatus(this, wxT("The wizard was cancelled."));