]>
git.saurik.com Git - wxWidgets.git/blob - samples/wizard/wizard.cpp
1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxWidgets 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/checklst.h"
34 #include "wx/msgdlg.h"
35 #include "wx/radiobox.h"
40 #include "wx/wizard.h"
42 #include "wiztest.xpm"
43 #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
);
80 void OnWizardFinished(wxWizardEvent
& event
);
83 // any class wishing to process wxWidgets events must use this macro
87 // ----------------------------------------------------------------------------
88 // some pages for our wizard
89 // ----------------------------------------------------------------------------
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
96 // It also shows how to use a different bitmap for one of the pages.
97 class wxValidationPage
: public wxWizardPageSimple
100 wxValidationPage(wxWizard
*parent
) : wxWizardPageSimple(parent
)
102 m_bitmap
= wxBitmap(wiztest2_xpm
);
104 m_checkbox
= new wxCheckBox(this, wxID_ANY
, _T("&Check me"));
106 wxBoxSizer
*mainSizer
= new wxBoxSizer(wxVERTICAL
);
108 new wxStaticText(this, wxID_ANY
,
109 _T("You need to check the checkbox\n")
110 _T("below before going to the next page\n")),
123 mainSizer
->Fit(this);
126 virtual bool TransferDataFromWindow()
128 if ( !m_checkbox
->GetValue() )
130 wxMessageBox(_T("Check the checkbox first!"), _T("No way"),
131 wxICON_WARNING
| wxOK
, this);
140 wxCheckBox
*m_checkbox
;
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
149 // directions in which we allow the user to proceed from this page
152 Forward
, Backward
, Both
, Neither
155 wxRadioboxPage(wxWizard
*parent
) : wxWizardPageSimple(parent
)
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.
161 choices
[0] = _T("forward");
162 choices
[1] = _T("backward");
163 choices
[2] = _T("both");
164 choices
[3] = _T("neither");
166 m_radio
= new wxRadioBox(this, wxID_ANY
, _T("Allow to proceed:"),
167 wxDefaultPosition
, wxDefaultSize
,
168 WXSIZEOF(choices
), choices
,
169 1, wxRA_SPECIFY_COLS
);
170 m_radio
->SetSelection(Both
);
172 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, wxID_ANY
, _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, wxID_ANY
, _T("&Skip the next page"));
245 0, // No vertical stretching
250 #if wxUSE_CHECKLISTBOX
251 static const wxChar
*aszChoices
[] =
252 { _T("Zeroth"), _T("First"), _T("Second"), _T("Third"), _T("Fourth"), _T("Fifth"), _T("Sixth"), _T("Seventh"), _T("Eighth"), _T("Nineth") };
253 wxString
*astrChoices
= new wxString
[WXSIZEOF(aszChoices
)];
255 for ( ui
= 0; ui
< WXSIZEOF(aszChoices
); ui
++ )
256 astrChoices
[ui
] = aszChoices
[ui
];
258 m_checklistbox
= new wxCheckListBox(this, wxID_ANY
, wxDefaultPosition
, wxSize(100,100),
259 WXSIZEOF(aszChoices
), astrChoices
);
263 0, // No vertical stretching
267 #endif // wxUSE_CHECKLISTBOX
270 mainSizer
->Fit(this);
273 // implement wxWizardPage functions
274 virtual wxWizardPage
*GetPrev() const { return m_prev
; }
275 virtual wxWizardPage
*GetNext() const
277 return m_checkbox
->GetValue() ? m_next
->GetNext() : m_next
;
281 wxWizardPage
*m_prev
,
284 wxCheckBox
*m_checkbox
;
285 #if wxUSE_CHECKLISTBOX
286 wxCheckListBox
*m_checklistbox
;
290 // ============================================================================
292 // ============================================================================
294 // ----------------------------------------------------------------------------
295 // event tables and such
296 // ----------------------------------------------------------------------------
298 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
299 EVT_MENU(Wizard_Quit
, MyFrame::OnQuit
)
300 EVT_MENU(Wizard_About
, MyFrame::OnAbout
)
301 EVT_MENU(Wizard_Run
, MyFrame::OnRunWizard
)
303 EVT_WIZARD_CANCEL(wxID_ANY
, MyFrame::OnWizardCancel
)
304 EVT_WIZARD_FINISHED(wxID_ANY
, MyFrame::OnWizardFinished
)
307 BEGIN_EVENT_TABLE(wxRadioboxPage
, wxWizardPageSimple
)
308 EVT_WIZARD_PAGE_CHANGING(wxID_ANY
, wxRadioboxPage::OnWizardPageChanging
)
309 EVT_WIZARD_CANCEL(wxID_ANY
, wxRadioboxPage::OnWizardCancel
)
314 // ----------------------------------------------------------------------------
315 // the application class
316 // ----------------------------------------------------------------------------
318 // `Main program' equivalent: the program execution "starts" here
321 MyFrame
*frame
= new MyFrame(_T("wxWizard Sample"));
323 // and show it (the frames, unlike simple controls, are not shown when
324 // created initially)
331 // ----------------------------------------------------------------------------
333 // ----------------------------------------------------------------------------
335 MyFrame::MyFrame(const wxString
& title
)
336 : wxFrame((wxFrame
*)NULL
, wxID_ANY
, title
,
337 wxDefaultPosition
, wxSize(250, 150)) // small frame
339 wxMenu
*menuFile
= new wxMenu
;
340 menuFile
->Append(Wizard_Run
, _T("&Run wizard...\tCtrl-R"));
341 menuFile
->AppendSeparator();
342 menuFile
->Append(Wizard_Quit
, _T("E&xit\tAlt-X"), _T("Quit this program"));
344 wxMenu
*helpMenu
= new wxMenu
;
345 helpMenu
->Append(Wizard_About
, _T("&About...\tF1"), _T("Show about dialog"));
347 // now append the freshly created menu to the menu bar...
348 wxMenuBar
*menuBar
= new wxMenuBar();
349 menuBar
->Append(menuFile
, _T("&File"));
350 menuBar
->Append(helpMenu
, _T("&Help"));
352 // ... and attach this menu bar to the frame
355 // also create status bar which we use in OnWizardCancel
358 #endif // wxUSE_STATUSBAR
361 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
363 // true is to force the frame to close
367 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
369 wxMessageBox(_T("Demo of wxWizard class\n")
370 _T("(c) 1999, 2000 Vadim Zeitlin"),
371 _T("About wxWizard sample"), wxOK
| wxICON_INFORMATION
, this);
374 void MyFrame::OnRunWizard(wxCommandEvent
& WXUNUSED(event
))
376 wxWizard
*wizard
= new wxWizard(this, wxID_ANY
,
377 _T("Absolutely Useless Wizard"),
378 wxBitmap(wiztest_xpm
),
380 wxDEFAULT_DIALOG_STYLE
| wxRESIZE_BORDER
);
382 // a wizard page may be either an object of predefined class
383 wxWizardPageSimple
*page1
= new wxWizardPageSimple(wizard
);
385 /* wxStaticText *text = */ new wxStaticText(page1
, wxID_ANY
,
386 _T("This wizard doesn't help you\nto do anything at all.\n")
388 _T("The next pages will present you\nwith more useless controls."),
392 // ... or a derived class
393 wxRadioboxPage
*page3
= new wxRadioboxPage(wizard
);
394 wxValidationPage
*page4
= new wxValidationPage(wizard
);
396 // set the page order using a convenience function - could also use
397 // SetNext/Prev directly as below
398 wxWizardPageSimple::Chain(page3
, page4
);
400 // this page is not a wxWizardPageSimple, so we use SetNext/Prev to insert
401 // it into the chain of pages
402 wxCheckboxPage
*page2
= new wxCheckboxPage(wizard
, page1
, page3
);
403 page1
->SetNext(page2
);
404 page3
->SetPrev(page2
);
406 // allow the wizard to size itself around the pages
407 wizard
->GetPageAreaSizer()->Add(page1
);
409 if ( wizard
->RunWizard(page1
) )
411 wxMessageBox(_T("The wizard successfully completed"), _T("That's all"),
412 wxICON_INFORMATION
| wxOK
);
418 void MyFrame::OnWizardFinished(wxWizardEvent
& WXUNUSED(event
))
420 wxLogStatus(this, wxT("The wizard finished successfully."));
423 void MyFrame::OnWizardCancel(wxWizardEvent
& WXUNUSED(event
))
425 wxLogStatus(this, wxT("The wizard was cancelled."));