]> git.saurik.com Git - wxWidgets.git/blob - samples/wizard/wizard.cpp
Implement NSMenuValidation protocol for the wxNSMenuItemTarget
[wxWidgets.git] / samples / wizard / wizard.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wizard.cpp
3 // Purpose: wxWindows sample demonstrating wxWizard control
4 // Author: Vadim Zeitlin
5 // Modified by: Robert Vazan (sizers)
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
20 // For compilers that support precompilation, includes "wx/wx.h".
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 // for all others, include the necessary headers
28 #ifndef WX_PRECOMP
29 #include "wx/stattext.h"
30 #include "wx/log.h"
31 #include "wx/app.h"
32 #include "wx/checkbox.h"
33 #include "wx/msgdlg.h"
34 #include "wx/radiobox.h"
35 #include "wx/menu.h"
36 #include "wx/sizer.h"
37 #endif
38
39 #include "wx/wizard.h"
40
41 #ifndef __WXMSW__
42 #include "wiztest.xpm"
43 #include "wiztest2.xpm"
44 #endif
45
46 // ----------------------------------------------------------------------------
47 // constants
48 // ----------------------------------------------------------------------------
49
50 // ids for menu items
51 enum
52 {
53 Wizard_Quit = 100,
54 Wizard_Run,
55 Wizard_About = 1000
56 };
57
58 // ----------------------------------------------------------------------------
59 // private classes
60 // ----------------------------------------------------------------------------
61
62 // Define a new application type, each program should derive a class from wxApp
63 class MyApp : public wxApp
64 {
65 public:
66 // override base class virtuals
67 virtual bool OnInit();
68 };
69
70 class MyFrame : public wxFrame
71 {
72 public:
73 // ctor(s)
74 MyFrame(const wxString& title);
75
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
82 private:
83 // any class wishing to process wxWindows events must use this macro
84 DECLARE_EVENT_TABLE()
85 };
86
87 // ----------------------------------------------------------------------------
88 // some pages for our wizard
89 // ----------------------------------------------------------------------------
90
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
94 // too
95 //
96 // it also shows how to use a different bitmap for one of the pages
97 class wxValidationPage : public wxWizardPageSimple
98 {
99 public:
100 wxValidationPage(wxWizard *parent) : wxWizardPageSimple(parent)
101 {
102 m_bitmap = wxBITMAP(wiztest2);
103
104 m_checkbox = new wxCheckBox(this, -1, _T("&Check me"));
105
106 wxBoxSizer *mainSizer = new wxBoxSizer(wxVERTICAL);
107 mainSizer->Add(
108 new wxStaticText(this, -1,
109 _T("You need to check the checkbox\n")
110 _T("below before going to the next page\n")),
111 0,
112 wxALL,
113 5
114 );
115
116 mainSizer->Add(
117 m_checkbox,
118 0, // No stretching
119 wxALL,
120 5 // Border
121 );
122 SetSizer(mainSizer);
123 mainSizer->Fit(this);
124 }
125
126 virtual bool TransferDataFromWindow()
127 {
128 if ( !m_checkbox->GetValue() )
129 {
130 wxMessageBox(_T("Check the checkbox first!"), _T("No way"),
131 wxICON_WARNING | wxOK, this);
132
133 return FALSE;
134 }
135
136 return TRUE;
137 }
138
139 private:
140 wxCheckBox *m_checkbox;
141 };
142
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
147 {
148 public:
149 // directions in which we allow the user to proceed from this page
150 enum
151 {
152 Forward, Backward, Both, Neither
153 };
154
155 wxRadioboxPage(wxWizard *parent) : wxWizardPageSimple(parent)
156 {
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.
160 wxString choices[4];
161 choices[0] = _T("forward");
162 choices[1] = _T("backward");
163 choices[2] = _T("both");
164 choices[3] = _T("neither");
165
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);
171
172 wxBoxSizer *mainSizer = new wxBoxSizer(wxVERTICAL);
173 mainSizer->Add(
174 m_radio,
175 0, // No stretching
176 wxALL,
177 5 // Border
178 );
179 SetSizer(mainSizer);
180 mainSizer->Fit(this);
181 }
182
183 // wizard event handlers
184 void OnWizardCancel(wxWizardEvent& event)
185 {
186 if ( wxMessageBox(_T("Do you really want to cancel?"), _T("Question"),
187 wxICON_QUESTION | wxYES_NO, this) != wxYES )
188 {
189 // not confirmed
190 event.Veto();
191 }
192 }
193
194 void OnWizardPageChanging(wxWizardEvent& event)
195 {
196 int sel = m_radio->GetSelection();
197
198 if ( sel == Both )
199 return;
200
201 if ( event.GetDirection() && sel == Forward )
202 return;
203
204 if ( !event.GetDirection() && sel == Backward )
205 return;
206
207 wxMessageBox(_T("You can't go there"), _T("Not allowed"),
208 wxICON_WARNING | wxOK, this);
209
210 event.Veto();
211 }
212
213 private:
214 wxRadioBox *m_radio;
215
216 DECLARE_EVENT_TABLE()
217 };
218
219 // this shows how to dynamically (i.e. during run-time) arrange the page order
220 class wxCheckboxPage : public wxWizardPage
221 {
222 public:
223 wxCheckboxPage(wxWizard *parent,
224 wxWizardPage *prev,
225 wxWizardPage *next)
226 : wxWizardPage(parent)
227 {
228 m_prev = prev;
229 m_next = next;
230
231 wxBoxSizer *mainSizer = new wxBoxSizer(wxVERTICAL);
232
233 mainSizer->Add(
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
237 wxALL,
238 5 // Border width
239 );
240
241 m_checkbox = new wxCheckBox(this, -1, _T("&Skip the next page"));
242 mainSizer->Add(
243 m_checkbox,
244 0, // No vertical stretching
245 wxALL,
246 5 // Border width
247 );
248
249 SetSizer(mainSizer);
250 mainSizer->Fit(this);
251 }
252
253 // implement wxWizardPage functions
254 virtual wxWizardPage *GetPrev() const { return m_prev; }
255 virtual wxWizardPage *GetNext() const
256 {
257 return m_checkbox->GetValue() ? m_next->GetNext() : m_next;
258 }
259
260 private:
261 wxWizardPage *m_prev,
262 *m_next;
263
264 wxCheckBox *m_checkbox;
265 };
266
267 // ============================================================================
268 // implementation
269 // ============================================================================
270
271 // ----------------------------------------------------------------------------
272 // event tables and such
273 // ----------------------------------------------------------------------------
274
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)
279
280 EVT_WIZARD_CANCEL(-1, MyFrame::OnWizardCancel)
281 END_EVENT_TABLE()
282
283 BEGIN_EVENT_TABLE(wxRadioboxPage, wxWizardPageSimple)
284 EVT_WIZARD_PAGE_CHANGING(-1, wxRadioboxPage::OnWizardPageChanging)
285 EVT_WIZARD_CANCEL(-1, wxRadioboxPage::OnWizardCancel)
286 END_EVENT_TABLE()
287
288 IMPLEMENT_APP(MyApp)
289
290 // ----------------------------------------------------------------------------
291 // the application class
292 // ----------------------------------------------------------------------------
293
294 // `Main program' equivalent: the program execution "starts" here
295 bool MyApp::OnInit()
296 {
297 MyFrame *frame = new MyFrame(_T("wxWizard Sample"));
298
299 // and show it (the frames, unlike simple controls, are not shown when
300 // created initially)
301 frame->Show(TRUE);
302
303 // we're done
304 return TRUE;
305 }
306
307 // ----------------------------------------------------------------------------
308 // MyFrame
309 // ----------------------------------------------------------------------------
310
311 MyFrame::MyFrame(const wxString& title)
312 : wxFrame((wxFrame *)NULL, -1, title,
313 wxDefaultPosition, wxSize(250, 150)) // small frame
314 {
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"));
319
320 wxMenu *helpMenu = new wxMenu;
321 helpMenu->Append(Wizard_About, _T("&About...\tF1"), _T("Show about dialog"));
322
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"));
327
328 // ... and attach this menu bar to the frame
329 SetMenuBar(menuBar);
330
331 // also create status bar which we use in OnWizardCancel
332 CreateStatusBar();
333 }
334
335 void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
336 {
337 // TRUE is to force the frame to close
338 Close(TRUE);
339 }
340
341 void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
342 {
343 wxMessageBox(_T("Demo of wxWizard class\n")
344 _T("© 1999, 2000 Vadim Zeitlin"),
345 _T("About wxWizard sample"), wxOK | wxICON_INFORMATION, this);
346 }
347
348 void MyFrame::OnRunWizard(wxCommandEvent& WXUNUSED(event))
349 {
350 wxWizard *wizard = new wxWizard(this, -1,
351 _T("Absolutely Useless Wizard"),
352 wxBITMAP(wiztest),
353 wxDefaultPosition,
354 wxDEFAULT_DIALOG_STYLE | wxRESIZE_BORDER);
355
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")
360 _T("\n")
361 _T("The next pages will present you\nwith more useless controls."),
362 wxPoint(5,5)
363 );
364 wxSize size = text->GetBestSize();
365
366 // ... or a derived class
367 wxRadioboxPage *page3 = new wxRadioboxPage(wizard);
368 wxValidationPage *page4 = new wxValidationPage(wizard);
369
370 // set the page order using a convenience function - could also use
371 // SetNext/Prev directly as below
372 wxWizardPageSimple::Chain(page3, page4);
373
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);
379
380 wizard->SetPageSize(size);
381 wizard->GetPageAreaSizer()->Add(page1);
382
383 if ( wizard->RunWizard(page1) )
384 {
385 wxMessageBox(_T("The wizard successfully completed"), _T("That's all"),
386 wxICON_INFORMATION | wxOK);
387 }
388
389 wizard->Destroy();
390 }
391
392 void MyFrame::OnWizardCancel(wxWizardEvent& WXUNUSED(event))
393 {
394 wxLogStatus(this, wxT("The wizard was cancelled."));
395 }