]> git.saurik.com Git - wxWidgets.git/blame - samples/wizard/wizard.cpp
Use NSBox with the NSBoxSeparator box type
[wxWidgets.git] / samples / wizard / wizard.cpp
CommitLineData
66cd017c 1/////////////////////////////////////////////////////////////////////////////
13f5935c 2// Name: wizard.cpp
66cd017c
VZ
3// Purpose: wxWindows sample demonstrating wxWizard control
4// Author: Vadim Zeitlin
07f20d9a 5// Modified by: Robert Vazan (sizers)
66cd017c
VZ
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
66cd017c
VZ
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
07f20d9a 27// for all others, include the necessary headers
66cd017c 28#ifndef WX_PRECOMP
07f20d9a
VZ
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"
66cd017c
VZ
37#endif
38
39#include "wx/wizard.h"
40
b87654f3
VZ
41#ifndef __WXMSW__
42 #include "wiztest.xpm"
d32011d4 43 #include "wiztest2.xpm"
b87654f3
VZ
44#endif
45
d93c719a
VZ
46// ----------------------------------------------------------------------------
47// constants
48// ----------------------------------------------------------------------------
49
50// ids for menu items
51enum
52{
53 Wizard_Quit = 100,
54 Wizard_Run,
55 Wizard_About = 1000
56};
57
66cd017c
VZ
58// ----------------------------------------------------------------------------
59// private classes
60// ----------------------------------------------------------------------------
61
62// Define a new application type, each program should derive a class from wxApp
63class MyApp : public wxApp
64{
65public:
66 // override base class virtuals
67 virtual bool OnInit();
68};
69
d93c719a
VZ
70class MyFrame : public wxFrame
71{
72public:
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
82private:
83 // any class wishing to process wxWindows events must use this macro
84 DECLARE_EVENT_TABLE()
85};
66cd017c
VZ
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
d93c719a
VZ
95//
96// it also shows how to use a different bitmap for one of the pages
74b31181 97class wxValidationPage : public wxWizardPageSimple
66cd017c
VZ
98{
99public:
74b31181 100 wxValidationPage(wxWizard *parent) : wxWizardPageSimple(parent)
66cd017c 101 {
d32011d4 102 m_bitmap = wxBITMAP(wiztest2);
d93c719a 103
9f84eccd 104 m_checkbox = new wxCheckBox(this, -1, _T("&Check me"));
07f20d9a
VZ
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);
66cd017c
VZ
124 }
125
126 virtual bool TransferDataFromWindow()
127 {
74b31181 128 if ( !m_checkbox->GetValue() )
66cd017c 129 {
9f84eccd 130 wxMessageBox(_T("Check the checkbox first!"), _T("No way"),
b87654f3 131 wxICON_WARNING | wxOK, this);
66cd017c
VZ
132
133 return FALSE;
134 }
135
136 return TRUE;
137 }
138
139private:
140 wxCheckBox *m_checkbox;
141};
142
74b31181
VZ
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.
146class wxRadioboxPage : public wxWizardPageSimple
147{
148public:
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
dc1efb1d
JS
158 // static wxString choices[] = { "forward", "backward", "both", "neither" };
159 // The above syntax can cause an internal compiler error with gcc.
160 wxString choices[4];
9f84eccd
MB
161 choices[0] = _T("forward");
162 choices[1] = _T("backward");
163 choices[2] = _T("both");
164 choices[3] = _T("neither");
74b31181 165
9f84eccd 166 m_radio = new wxRadioBox(this, -1, _T("Allow to proceed:"),
07f20d9a 167 wxDefaultPosition, wxDefaultSize,
74b31181
VZ
168 WXSIZEOF(choices), choices,
169 1, wxRA_SPECIFY_COLS);
170 m_radio->SetSelection(Both);
07f20d9a
VZ
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);
74b31181
VZ
181 }
182
183 // wizard event handlers
184 void OnWizardCancel(wxWizardEvent& event)
185 {
9f84eccd 186 if ( wxMessageBox(_T("Do you really want to cancel?"), _T("Question"),
74b31181
VZ
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
9f84eccd 207 wxMessageBox(_T("You can't go there"), _T("Not allowed"),
74b31181
VZ
208 wxICON_WARNING | wxOK, this);
209
210 event.Veto();
211 }
212
213private:
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
220class wxCheckboxPage : public wxWizardPage
221{
222public:
223 wxCheckboxPage(wxWizard *parent,
224 wxWizardPage *prev,
225 wxWizardPage *next)
226 : wxWizardPage(parent)
227 {
228 m_prev = prev;
229 m_next = next;
07f20d9a
VZ
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 );
74b31181 240
07f20d9a
VZ
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);
74b31181
VZ
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
260private:
261 wxWizardPage *m_prev,
262 *m_next;
263
264 wxCheckBox *m_checkbox;
265};
266
66cd017c
VZ
267// ============================================================================
268// implementation
269// ============================================================================
270
d93c719a
VZ
271// ----------------------------------------------------------------------------
272// event tables and such
273// ----------------------------------------------------------------------------
274
275BEGIN_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)
281END_EVENT_TABLE()
282
74b31181
VZ
283BEGIN_EVENT_TABLE(wxRadioboxPage, wxWizardPageSimple)
284 EVT_WIZARD_PAGE_CHANGING(-1, wxRadioboxPage::OnWizardPageChanging)
285 EVT_WIZARD_CANCEL(-1, wxRadioboxPage::OnWizardCancel)
286END_EVENT_TABLE()
287
d93c719a
VZ
288IMPLEMENT_APP(MyApp)
289
66cd017c
VZ
290// ----------------------------------------------------------------------------
291// the application class
292// ----------------------------------------------------------------------------
293
294// `Main program' equivalent: the program execution "starts" here
295bool MyApp::OnInit()
296{
9f84eccd 297 MyFrame *frame = new MyFrame(_T("wxWizard Sample"));
d93c719a
VZ
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
311MyFrame::MyFrame(const wxString& title)
312 : wxFrame((wxFrame *)NULL, -1, title,
313 wxDefaultPosition, wxSize(250, 150)) // small frame
314{
315 wxMenu *menuFile = new wxMenu;
9f84eccd 316 menuFile->Append(Wizard_Run, _T("&Run wizard...\tCtrl-R"));
d93c719a 317 menuFile->AppendSeparator();
9f84eccd 318 menuFile->Append(Wizard_Quit, _T("E&xit\tAlt-X"), _T("Quit this program"));
d93c719a
VZ
319
320 wxMenu *helpMenu = new wxMenu;
9f84eccd 321 helpMenu->Append(Wizard_About, _T("&About...\tF1"), _T("Show about dialog"));
d93c719a
VZ
322
323 // now append the freshly created menu to the menu bar...
324 wxMenuBar *menuBar = new wxMenuBar();
9f84eccd
MB
325 menuBar->Append(menuFile, _T("&File"));
326 menuBar->Append(helpMenu, _T("&Help"));
66cd017c 327
d93c719a
VZ
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
335void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))
336{
337 // TRUE is to force the frame to close
338 Close(TRUE);
339}
340
341void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
342{
9f84eccd
MB
343 wxMessageBox(_T("Demo of wxWizard class\n")
344