]>
Commit | Line | Data |
---|---|---|
66cd017c | 1 | ///////////////////////////////////////////////////////////////////////////// |
13f5935c | 2 | // Name: wizard.cpp |
66cd017c VZ |
3 | // Purpose: wxWindows sample demonstrating wxWizard control |
4 | // Author: Vadim Zeitlin | |
5 | // Modified by: | |
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 | ||
788233da | 20 | #if defined(__GNUG__) && !defined(__APPLE__) |
13f5935c RL |
21 | #pragma implementation "wizard.cpp" |
22 | #pragma interface "wizard.cpp" | |
66cd017c VZ |
23 | #endif |
24 | ||
25 | // For compilers that support precompilation, includes "wx/wx.h". | |
26 | #include "wx/wxprec.h" | |
27 | ||
28 | #ifdef __BORLANDC__ | |
29 | #pragma hdrstop | |
30 | #endif | |
31 | ||
32 | // for all others, include the necessary headers (this file is usually all you | |
33 | // need because it includes almost all "standard" wxWindows headers | |
34 | #ifndef WX_PRECOMP | |
35 | #include "wx/wx.h" | |
36 | #endif | |
37 | ||
38 | #include "wx/wizard.h" | |
39 | ||
b87654f3 VZ |
40 | #ifndef __WXMSW__ |
41 | #include "wiztest.xpm" | |
d32011d4 | 42 | #include "wiztest2.xpm" |
b87654f3 VZ |
43 | #endif |
44 | ||
d93c719a VZ |
45 | // ---------------------------------------------------------------------------- |
46 | // constants | |
47 | // ---------------------------------------------------------------------------- | |
48 | ||
49 | // ids for menu items | |
50 | enum | |
51 | { | |
52 | Wizard_Quit = 100, | |
53 | Wizard_Run, | |
54 | Wizard_About = 1000 | |
55 | }; | |
56 | ||
66cd017c VZ |
57 | // ---------------------------------------------------------------------------- |
58 | // private classes | |
59 | // ---------------------------------------------------------------------------- | |
60 | ||
61 | // Define a new application type, each program should derive a class from wxApp | |
62 | class MyApp : public wxApp | |
63 | { | |
64 | public: | |
65 | // override base class virtuals | |
66 | virtual bool OnInit(); | |
67 | }; | |
68 | ||
d93c719a VZ |
69 | class MyFrame : public wxFrame |
70 | { | |
71 | public: | |
72 | // ctor(s) | |
73 | MyFrame(const wxString& title); | |
74 | ||
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); | |
80 | ||
81 | private: | |
82 | // any class wishing to process wxWindows events must use this macro | |
83 | DECLARE_EVENT_TABLE() | |
84 | }; | |
66cd017c VZ |
85 | |
86 | // ---------------------------------------------------------------------------- | |
87 | // some pages for our wizard | |
88 | // ---------------------------------------------------------------------------- | |
89 | ||
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 | |
93 | // too | |
d93c719a VZ |
94 | // |
95 | // it also shows how to use a different bitmap for one of the pages | |
74b31181 | 96 | class wxValidationPage : public wxWizardPageSimple |
66cd017c VZ |
97 | { |
98 | public: | |
74b31181 | 99 | wxValidationPage(wxWizard *parent) : wxWizardPageSimple(parent) |
66cd017c | 100 | { |
d32011d4 | 101 | m_bitmap = wxBITMAP(wiztest2); |
d93c719a | 102 | |
9f84eccd | 103 | m_checkbox = new wxCheckBox(this, -1, _T("&Check me")); |
66cd017c VZ |
104 | } |
105 | ||
106 | virtual bool TransferDataFromWindow() | |
107 | { | |
74b31181 | 108 | if ( !m_checkbox->GetValue() ) |
66cd017c | 109 | { |
9f84eccd | 110 | wxMessageBox(_T("Check the checkbox first!"), _T("No way"), |
b87654f3 | 111 | wxICON_WARNING | wxOK, this); |
66cd017c VZ |
112 | |
113 | return FALSE; | |
114 | } | |
115 | ||
116 | return TRUE; | |
117 | } | |
118 | ||
119 | private: | |
120 | wxCheckBox *m_checkbox; | |
121 | }; | |
122 | ||
74b31181 VZ |
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 | |
127 | { | |
128 | public: | |
129 | // directions in which we allow the user to proceed from this page | |
130 | enum | |
131 | { | |
132 | Forward, Backward, Both, Neither | |
133 | }; | |
134 | ||
135 | wxRadioboxPage(wxWizard *parent) : wxWizardPageSimple(parent) | |
136 | { | |
137 | // should correspond to the enum above | |
dc1efb1d JS |
138 | // static wxString choices[] = { "forward", "backward", "both", "neither" }; |
139 | // The above syntax can cause an internal compiler error with gcc. | |
140 | wxString choices[4]; | |
9f84eccd MB |
141 | choices[0] = _T("forward"); |
142 | choices[1] = _T("backward"); | |
143 | choices[2] = _T("both"); | |
144 | choices[3] = _T("neither"); | |
74b31181 | 145 | |
9f84eccd | 146 | m_radio = new wxRadioBox(this, -1, _T("Allow to proceed:"), |
74b31181 VZ |
147 | wxPoint(5, 5), wxDefaultSize, |
148 | WXSIZEOF(choices), choices, | |
149 | 1, wxRA_SPECIFY_COLS); | |
150 | m_radio->SetSelection(Both); | |
151 | } | |
152 | ||
153 | // wizard event handlers | |
154 | void OnWizardCancel(wxWizardEvent& event) | |
155 | { | |
9f84eccd | 156 | if ( wxMessageBox(_T("Do you really want to cancel?"), _T("Question"), |
74b31181 VZ |
157 | wxICON_QUESTION | wxYES_NO, this) != wxYES ) |
158 | { | |
159 | // not confirmed | |
160 | event.Veto(); | |
161 | } | |
162 | } | |
163 | ||
164 | void OnWizardPageChanging(wxWizardEvent& event) | |
165 | { | |
166 | int sel = m_radio->GetSelection(); | |
167 | ||
168 | if ( sel == Both ) | |
169 | return; | |
170 | ||
171 | if ( event.GetDirection() && sel == Forward ) | |
172 | return; | |
173 | ||
174 | if ( !event.GetDirection() && sel == Backward ) | |
175 | return; | |
176 | ||
9f84eccd | 177 | wxMessageBox(_T("You can't go there"), _T("Not allowed"), |
74b31181 VZ |
178 | wxICON_WARNING | wxOK, this); |
179 | ||
180 | event.Veto(); | |
181 | } | |
182 | ||
183 | private: | |
184 | wxRadioBox *m_radio; | |
185 | ||
186 | DECLARE_EVENT_TABLE() | |
187 | }; | |
188 | ||
189 | // this shows how to dynamically (i.e. during run-time) arrange the page order | |
190 | class wxCheckboxPage : public wxWizardPage | |
191 | { | |
192 | public: | |
193 | wxCheckboxPage(wxWizard *parent, | |
194 | wxWizardPage *prev, | |
195 | wxWizardPage *next) | |
196 | : wxWizardPage(parent) | |
197 | { | |
198 | m_prev = prev; | |
199 | m_next = next; | |
200 | ||
9f84eccd MB |
201 | (void)new wxStaticText(this, -1, _T("Try checking the box below and\n") |
202 | _T("then going back and clearing it")); | |
74b31181 | 203 | |
9f84eccd | 204 | m_checkbox = new wxCheckBox(this, -1, _T("&Skip the next page"), |
74b31181 VZ |
205 | wxPoint(5, 30)); |
206 | } | |
207 | ||
208 | // implement wxWizardPage functions | |
209 | virtual wxWizardPage *GetPrev() const { return m_prev; } | |
210 | virtual wxWizardPage *GetNext() const | |
211 | { | |
212 | return m_checkbox->GetValue() ? m_next->GetNext() : m_next; | |
213 | } | |
214 | ||
215 | private: | |
216 | wxWizardPage *m_prev, | |
217 | *m_next; | |
218 | ||
219 | wxCheckBox *m_checkbox; | |
220 | }; | |
221 | ||
66cd017c VZ |
222 | // ============================================================================ |
223 | // implementation | |
224 | // ============================================================================ | |
225 | ||
d93c719a VZ |
226 | // ---------------------------------------------------------------------------- |
227 | // event tables and such | |
228 | // ---------------------------------------------------------------------------- | |
229 | ||
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) | |
234 | ||
235 | EVT_WIZARD_CANCEL(-1, MyFrame::OnWizardCancel) | |
236 | END_EVENT_TABLE() | |
237 | ||
74b31181 VZ |
238 | BEGIN_EVENT_TABLE(wxRadioboxPage, wxWizardPageSimple) |
239 | EVT_WIZARD_PAGE_CHANGING(-1, wxRadioboxPage::OnWizardPageChanging) | |
240 | EVT_WIZARD_CANCEL(-1, wxRadioboxPage::OnWizardCancel) | |
241 | END_EVENT_TABLE() | |
242 | ||
d93c719a VZ |
243 | IMPLEMENT_APP(MyApp) |
244 | ||
66cd017c VZ |
245 | // ---------------------------------------------------------------------------- |
246 | // the application class | |
247 | // ---------------------------------------------------------------------------- | |
248 | ||
249 | // `Main program' equivalent: the program execution "starts" here | |
250 | bool MyApp::OnInit() | |
251 | { | |
9f84eccd | 252 | MyFrame *frame = new MyFrame(_T("wxWizard Sample")); |
d93c719a VZ |
253 | |
254 | // and show it (the frames, unlike simple controls, are not shown when | |
255 | // created initially) | |
256 | frame->Show(TRUE); | |
257 | ||
258 | // we're done | |
259 | return TRUE; | |
260 | } | |
261 | ||
262 | // ---------------------------------------------------------------------------- | |
263 | // MyFrame | |
264 | // ---------------------------------------------------------------------------- | |
265 | ||
266 | MyFrame::MyFrame(const wxString& title) | |
267 | : wxFrame((wxFrame *)NULL, -1, title, | |
268 | wxDefaultPosition, wxSize(250, 150)) // small frame | |
269 | { | |
270 | wxMenu *menuFile = new wxMenu; | |
9f84eccd | 271 | menuFile->Append(Wizard_Run, _T("&Run wizard...\tCtrl-R")); |
d93c719a | 272 | menuFile->AppendSeparator(); |
9f84eccd | 273 | menuFile->Append(Wizard_Quit, _T("E&xit\tAlt-X"), _T("Quit this program")); |
d93c719a VZ |
274 | |
275 | wxMenu *helpMenu = new wxMenu; | |
9f84eccd | 276 | helpMenu->Append(Wizard_About, _T("&About...\tF1"), _T("Show about dialog")); |
d93c719a VZ |
277 | |
278 | // now append the freshly created menu to the menu bar... | |
279 | wxMenuBar *menuBar = new wxMenuBar(); | |
9f84eccd MB |
280 | menuBar->Append(menuFile, _T("&File")); |
281 | menuBar->Append(helpMenu, _T("&Help")); | |
66cd017c | 282 | |
d93c719a VZ |
283 | // ... and attach this menu bar to the frame |
284 | SetMenuBar(menuBar); | |
285 | ||
286 | // also create status bar which we use in OnWizardCancel | |
287 | CreateStatusBar(); | |
288 | } | |
289 | ||
290 | void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event)) | |
291 | { | |
292 | // TRUE is to force the frame to close | |
293 | Close(TRUE); | |
294 | } | |
295 | ||
296 | void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event)) | |
297 | { | |
9f84eccd MB |
298 | wxMessageBox(_T("Demo of wxWizard class\n") |
299 |