]>
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 // ----------------------------------------------------------------------------
52 Wizard_Quit
= wxID_EXIT
,
53 Wizard_RunModal
= wxID_HIGHEST
,
55 Wizard_About
= wxID_ABOUT
58 // ----------------------------------------------------------------------------
60 // ----------------------------------------------------------------------------
62 // Define a new application type, each program should derive a class from wxApp
63 class MyApp
: public wxApp
66 // override base class virtuals
67 virtual bool OnInit();
70 class MyFrame
: public wxFrame
74 MyFrame(const wxString
& title
);
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 void OnWizardFinished(wxWizardEvent
& event
);
84 // any class wishing to process wxWidgets events must use this macro
88 // ----------------------------------------------------------------------------
90 // ----------------------------------------------------------------------------
92 class MyWizard
: public wxWizard
95 MyWizard(wxFrame
*frame
);
96 void RunIt(bool modal
);
99 wxWizardPageSimple
*m_page1
;
102 // ----------------------------------------------------------------------------
103 // some pages for our wizard
104 // ----------------------------------------------------------------------------
106 // This shows how to simply control the validity of the user input by just
107 // overriding TransferDataFromWindow() - of course, in a real program, the
108 // check wouldn't be so trivial and the data will be probably saved somewhere
111 // It also shows how to use a different bitmap for one of the pages.
112 class wxValidationPage
: public wxWizardPageSimple
115 wxValidationPage(wxWizard
*parent
) : wxWizardPageSimple(parent
)
117 m_bitmap
= wxBitmap(wiztest2_xpm
);
119 m_checkbox
= new wxCheckBox(this, wxID_ANY
, _T("&Check me"));
121 wxBoxSizer
*mainSizer
= new wxBoxSizer(wxVERTICAL
);
123 new wxStaticText(this, wxID_ANY
,
124 _T("You need to check the checkbox\n")
125 _T("below before going to the next page\n")),
138 mainSizer
->Fit(this);
141 virtual bool TransferDataFromWindow()
143 if ( !m_checkbox
->GetValue() )
145 wxMessageBox(_T("Check the checkbox first!"), _T("No way"),
146 wxICON_WARNING
| wxOK
, this);
155 wxCheckBox
*m_checkbox
;
158 // This is a more complicated example of validity checking: using events we may
159 // allow to return to the previous page, but not to proceed. It also
160 // demonstrates how to intercept [Cancel] button press.
161 class wxRadioboxPage
: public wxWizardPageSimple
164 // directions in which we allow the user to proceed from this page
167 Forward
, Backward
, Both
, Neither
170 wxRadioboxPage(wxWizard
*parent
) : wxWizardPageSimple(parent
)
172 // should correspond to the enum above
173 // static wxString choices[] = { "forward", "backward", "both", "neither" };
174 // The above syntax can cause an internal compiler error with gcc.
176 choices
[0] = _T("forward");
177 choices
[1] = _T("backward");
178 choices
[2] = _T("both");
179 choices
[3] = _T("neither");
181 m_radio
= new wxRadioBox(this, wxID_ANY
, _T("Allow to proceed:"),
182 wxDefaultPosition
, wxDefaultSize
,
183 WXSIZEOF(choices
), choices
,
184 1, wxRA_SPECIFY_COLS
);
185 m_radio
->SetSelection(Both
);
187 wxBoxSizer
*mainSizer
= new wxBoxSizer(wxVERTICAL
);
196 mainSizer
->Fit(this);
199 // wizard event handlers
200 void OnWizardCancel(wxWizardEvent
& event
)
202 if ( wxMessageBox(_T("Do you really want to cancel?"), _T("Question"),
203 wxICON_QUESTION
| wxYES_NO
, this) != wxYES
)
210 void OnWizardPageChanging(wxWizardEvent
& event
)
212 int sel
= m_radio
->GetSelection();
217 if ( event
.GetDirection() && sel
== Forward
)
220 if ( !event
.GetDirection() && sel
== Backward
)
223 wxMessageBox(_T("You can't go there"), _T("Not allowed"),
224 wxICON_WARNING
| wxOK
, this);
232 DECLARE_EVENT_TABLE()
235 // This shows how to dynamically (i.e. during run-time) arrange the page order.
236 class wxCheckboxPage
: public wxWizardPage
239 wxCheckboxPage(wxWizard
*parent
,
242 : wxWizardPage(parent
)
247 wxBoxSizer
*mainSizer
= new wxBoxSizer(wxVERTICAL
);
250 new wxStaticText(this, wxID_ANY
, _T("Try checking the box below and\n")
251 _T("then going back and clearing it")),
252 0, // No vertical stretching
257 m_checkbox
= new wxCheckBox(this, wxID_ANY
, _T("&Skip the next page"));
260 0, // No vertical stretching
265 #if wxUSE_CHECKLISTBOX
266 static const wxChar
*aszChoices
[] =
280 m_checklistbox
= new wxCheckListBox
286 wxArrayString(WXSIZEOF(aszChoices
), aszChoices
)
291 0, // No vertical stretching
295 #endif // wxUSE_CHECKLISTBOX
298 mainSizer
->Fit(this);
301 // implement wxWizardPage functions
302 virtual wxWizardPage
*GetPrev() const { return m_prev
; }
303 virtual wxWizardPage
*GetNext() const
305 return m_checkbox
->GetValue() ? m_next
->GetNext() : m_next
;
309 wxWizardPage
*m_prev
,
312 wxCheckBox
*m_checkbox
;
313 #if wxUSE_CHECKLISTBOX
314 wxCheckListBox
*m_checklistbox
;
318 // ============================================================================
320 // ============================================================================
322 // ----------------------------------------------------------------------------
323 // event tables and such
324 // ----------------------------------------------------------------------------
326 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
327 EVT_MENU(Wizard_Quit
, MyFrame::OnQuit
)
328 EVT_MENU(Wizard_About
, MyFrame::OnAbout
)
329 EVT_MENU(Wizard_RunModal
, MyFrame::OnRunWizard
)
330 EVT_MENU(Wizard_RunModeless
, MyFrame::OnRunWizard
)
332 EVT_WIZARD_CANCEL(wxID_ANY
, MyFrame::OnWizardCancel
)
333 EVT_WIZARD_FINISHED(wxID_ANY
, MyFrame::OnWizardFinished
)
336 BEGIN_EVENT_TABLE(wxRadioboxPage
, wxWizardPageSimple
)
337 EVT_WIZARD_PAGE_CHANGING(wxID_ANY
, wxRadioboxPage::OnWizardPageChanging
)
338 EVT_WIZARD_CANCEL(wxID_ANY
, wxRadioboxPage::OnWizardCancel
)
343 // ----------------------------------------------------------------------------
344 // the application class
345 // ----------------------------------------------------------------------------
347 // `Main program' equivalent: the program execution "starts" here
350 MyFrame
*frame
= new MyFrame(_T("wxWizard Sample"));
352 // and show it (the frames, unlike simple controls, are not shown when
353 // created initially)
360 // ----------------------------------------------------------------------------
362 // ----------------------------------------------------------------------------
364 MyWizard::MyWizard(wxFrame
*frame
)
365 :wxWizard(frame
,wxID_ANY
,_T("Absolutely Useless Wizard"),
366 wxBitmap(wiztest_xpm
),wxDefaultPosition
,
367 wxDEFAULT_DIALOG_STYLE
| wxRESIZE_BORDER
)
369 // a wizard page may be either an object of predefined class
370 m_page1
= new wxWizardPageSimple(this);
372 /* wxStaticText *text = */ new wxStaticText(m_page1
, wxID_ANY
,
373 _T("This wizard doesn't help you\nto do anything at all.\n")
375 _T("The next pages will present you\nwith more useless controls."),
379 // ... or a derived class
380 wxRadioboxPage
*page3
= new wxRadioboxPage(this);
381 wxValidationPage
*page4
= new wxValidationPage(this);
383 // set the page order using a convenience function - could also use
384 // SetNext/Prev directly as below
385 wxWizardPageSimple::Chain(page3
, page4
);
387 // this page is not a wxWizardPageSimple, so we use SetNext/Prev to insert
388 // it into the chain of pages
389 wxCheckboxPage
*page2
= new wxCheckboxPage(this, m_page1
, page3
);
390 m_page1
->SetNext(page2
);
391 page3
->SetPrev(page2
);
393 // allow the wizard to size itself around the pages
394 GetPageAreaSizer()->Add(m_page1
);
397 void MyWizard::RunIt(bool modal
)
401 if ( RunWizard(m_page1
) )
416 // ----------------------------------------------------------------------------
418 // ----------------------------------------------------------------------------
420 MyFrame::MyFrame(const wxString
& title
)
421 :wxFrame((wxFrame
*)NULL
, wxID_ANY
, title
,
422 wxDefaultPosition
, wxSize(250, 150)) // small frame
424 wxMenu
*menuFile
= new wxMenu
;
425 menuFile
->Append(Wizard_RunModal
, _T("&Run wizard modal...\tCtrl-R"));
426 menuFile
->Append(Wizard_RunModeless
, _T("&Run wizard modeless..."));
427 menuFile
->AppendSeparator();
428 menuFile
->Append(Wizard_Quit
, _T("E&xit\tAlt-X"), _T("Quit this program"));
430 wxMenu
*helpMenu
= new wxMenu
;
431 helpMenu
->Append(Wizard_About
, _T("&About...\tF1"), _T("Show about dialog"));
433 // now append the freshly created menu to the menu bar...
434 wxMenuBar
*menuBar
= new wxMenuBar();
435 menuBar
->Append(menuFile
, _T("&File"));
436 menuBar
->Append(helpMenu
, _T("&Help"));
438 // ... and attach this menu bar to the frame
441 // also create status bar which we use in OnWizardCancel
444 #endif // wxUSE_STATUSBAR
447 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
449 // true is to force the frame to close
453 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
455 wxMessageBox(_T("Demo of wxWizard class\n")
456 _T("(c) 1999, 2000 Vadim Zeitlin"),
457 _T("About wxWizard sample"), wxOK
| wxICON_INFORMATION
, this);
460 void MyFrame::OnRunWizard(wxCommandEvent
& event
)
462 MyWizard
*wizard
= new MyWizard(this);
464 wizard
->RunIt( event
.GetId() == Wizard_RunModal
);
467 void MyFrame::OnWizardFinished(wxWizardEvent
& WXUNUSED(event
))
469 wxMessageBox(wxT("The wizard finished successfully."), wxT("Wizard notification"));
472 void MyFrame::OnWizardCancel(wxWizardEvent
& WXUNUSED(event
))
474 wxMessageBox(wxT("The wizard was cancelled."), wxT("Wizard notification"));