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
30 #include "wx/stattext.h"
33 #include "wx/checkbox.h"
34 #include "wx/checklst.h"
35 #include "wx/msgdlg.h"
36 #include "wx/radiobox.h"
41 #include "wx/textctrl.h"
42 #include "wx/wizard.h"
44 #include "wiztest.xpm"
45 #include "wiztest2.xpm"
47 #include "../sample.xpm"
49 // ----------------------------------------------------------------------------
51 // ----------------------------------------------------------------------------
56 Wizard_About
= wxID_ABOUT
,
57 Wizard_Quit
= wxID_EXIT
,
58 Wizard_RunModal
= wxID_HIGHEST
,
67 // ----------------------------------------------------------------------------
69 // ----------------------------------------------------------------------------
71 // Define a new application type, each program should derive a class from wxApp
72 class MyApp
: public wxApp
75 // override base class virtuals
76 virtual bool OnInit();
79 class MyFrame
: public wxFrame
83 MyFrame(const wxString
& title
);
85 // event handlers (these functions should _not_ be virtual)
86 void OnQuit(wxCommandEvent
& event
);
87 void OnAbout(wxCommandEvent
& event
);
88 void OnRunWizard(wxCommandEvent
& event
);
89 void OnRunWizardNoSizer(wxCommandEvent
& event
);
90 void OnRunWizardModeless(wxCommandEvent
& event
);
91 void OnWizardCancel(wxWizardEvent
& event
);
92 void OnWizardFinished(wxWizardEvent
& event
);
95 // any class wishing to process wxWidgets events must use this macro
99 // ----------------------------------------------------------------------------
101 // ----------------------------------------------------------------------------
103 class MyWizard
: public wxWizard
106 MyWizard(wxFrame
*frame
, bool useSizer
= true);
108 wxWizardPage
*GetFirstPage() const { return m_page1
; }
111 wxWizardPageSimple
*m_page1
;
114 // ----------------------------------------------------------------------------
115 // some pages for our wizard
116 // ----------------------------------------------------------------------------
118 // This shows how to simply control the validity of the user input by just
119 // overriding TransferDataFromWindow() - of course, in a real program, the
120 // check wouldn't be so trivial and the data will be probably saved somewhere
123 // It also shows how to use a different bitmap for one of the pages.
124 class wxValidationPage
: public wxWizardPageSimple
127 wxValidationPage(wxWizard
*parent
) : wxWizardPageSimple(parent
)
129 m_bitmap
= wxBitmap(wiztest2_xpm
);
131 m_checkbox
= new wxCheckBox(this, wxID_ANY
, wxT("&Check me"));
133 wxBoxSizer
*mainSizer
= new wxBoxSizer(wxVERTICAL
);
135 new wxStaticText(this, wxID_ANY
,
136 wxT("You need to check the checkbox\n")
137 wxT("below before going to the next page\n")),
149 SetSizerAndFit(mainSizer
);
152 virtual bool TransferDataFromWindow()
154 if ( !m_checkbox
->GetValue() )
156 wxMessageBox(wxT("Check the checkbox first!"), wxT("No way"),
157 wxICON_WARNING
| wxOK
, this);
166 wxCheckBox
*m_checkbox
;
169 // This is a more complicated example of validity checking: using events we may
170 // allow to return to the previous page, but not to proceed. It also
171 // demonstrates how to intercept [Cancel] button press.
172 class wxRadioboxPage
: public wxWizardPageSimple
175 // directions in which we allow the user to proceed from this page
178 Forward
, Backward
, Both
, Neither
181 wxRadioboxPage(wxWizard
*parent
) : wxWizardPageSimple(parent
)
183 // should correspond to the enum above
184 // static wxString choices[] = { "forward", "backward", "both", "neither" };
185 // The above syntax can cause an internal compiler error with gcc.
187 choices
[0] = wxT("forward");
188 choices
[1] = wxT("backward");
189 choices
[2] = wxT("both");
190 choices
[3] = wxT("neither");
192 m_radio
= new wxRadioBox(this, wxID_ANY
, wxT("Allow to proceed:"),
193 wxDefaultPosition
, wxDefaultSize
,
194 WXSIZEOF(choices
), choices
,
195 1, wxRA_SPECIFY_COLS
);
196 m_radio
->SetSelection(Both
);
198 wxBoxSizer
*mainSizer
= new wxBoxSizer(wxVERTICAL
);
206 SetSizerAndFit(mainSizer
);
209 // wizard event handlers
210 void OnWizardCancel(wxWizardEvent
& event
)
212 if ( wxMessageBox(wxT("Do you really want to cancel?"), wxT("Question"),
213 wxICON_QUESTION
| wxYES_NO
, this) != wxYES
)
220 void OnWizardPageChanging(wxWizardEvent
& event
)
222 int sel
= m_radio
->GetSelection();
227 if ( event
.GetDirection() && sel
== Forward
)
230 if ( !event
.GetDirection() && sel
== Backward
)
233 wxMessageBox(wxT("You can't go there"), wxT("Not allowed"),
234 wxICON_WARNING
| wxOK
, this);
242 DECLARE_EVENT_TABLE()
245 // This shows how to dynamically (i.e. during run-time) arrange the page order.
246 class wxCheckboxPage
: public wxWizardPage
249 wxCheckboxPage(wxWizard
*parent
,
252 : wxWizardPage(parent
)
257 wxBoxSizer
*mainSizer
= new wxBoxSizer(wxVERTICAL
);
260 new wxStaticText(this, wxID_ANY
, wxT("Try checking the box below and\n")
261 wxT("then going back and clearing it")),
262 0, // No vertical stretching
267 m_checkbox
= new wxCheckBox(this, wxID_ANY
, wxT("&Skip the next page"));
270 0, // No vertical stretching
275 #if wxUSE_CHECKLISTBOX
276 static const wxChar
*aszChoices
[] =
290 m_checklistbox
= new wxCheckListBox
296 wxArrayString(WXSIZEOF(aszChoices
), aszChoices
)
301 0, // No vertical stretching
305 #endif // wxUSE_CHECKLISTBOX
307 wxSize textSize
= wxSize(150, 200);
308 if (((wxFrame
*) wxTheApp
->GetTopWindow())->GetMenuBar()->IsChecked(Wizard_LargeWizard
))
309 textSize
= wxSize(150, wxGetClientDisplayRect().GetHeight() - 200);
312 wxTextCtrl
* textCtrl
= new wxTextCtrl(this, wxID_ANY
, wxEmptyString
, wxDefaultPosition
, textSize
, wxTE_MULTILINE
);
313 mainSizer
->Add(textCtrl
, 0, wxALL
|wxEXPAND
, 5);
315 SetSizerAndFit(mainSizer
);
318 // implement wxWizardPage functions
319 virtual wxWizardPage
*GetPrev() const { return m_prev
; }
320 virtual wxWizardPage
*GetNext() const
322 return m_checkbox
->GetValue() ? m_next
->GetNext() : m_next
;
326 wxWizardPage
*m_prev
,
329 wxCheckBox
*m_checkbox
;
330 #if wxUSE_CHECKLISTBOX
331 wxCheckListBox
*m_checklistbox
;
335 // ============================================================================
337 // ============================================================================
339 // ----------------------------------------------------------------------------
340 // event tables and such
341 // ----------------------------------------------------------------------------
343 BEGIN_EVENT_TABLE(MyFrame
, wxFrame
)
344 EVT_MENU(Wizard_Quit
, MyFrame::OnQuit
)
345 EVT_MENU(Wizard_About
, MyFrame::OnAbout
)
346 EVT_MENU(Wizard_RunModal
, MyFrame::OnRunWizard
)
347 EVT_MENU(Wizard_RunNoSizer
, MyFrame::OnRunWizardNoSizer
)
348 EVT_MENU(Wizard_RunModeless
, MyFrame::OnRunWizardModeless
)
350 EVT_WIZARD_CANCEL(wxID_ANY
, MyFrame::OnWizardCancel
)
351 EVT_WIZARD_FINISHED(wxID_ANY
, MyFrame::OnWizardFinished
)
354 BEGIN_EVENT_TABLE(wxRadioboxPage
, wxWizardPageSimple
)
355 EVT_WIZARD_PAGE_CHANGING(wxID_ANY
, wxRadioboxPage::OnWizardPageChanging
)
356 EVT_WIZARD_CANCEL(wxID_ANY
, wxRadioboxPage::OnWizardCancel
)
361 // ----------------------------------------------------------------------------
362 // the application class
363 // ----------------------------------------------------------------------------
365 // `Main program' equivalent: the program execution "starts" here
368 if ( !wxApp::OnInit() )
371 MyFrame
*frame
= new MyFrame(wxT("wxWizard Sample"));
373 // and show it (the frames, unlike simple controls, are not shown when
374 // created initially)
381 // ----------------------------------------------------------------------------
383 // ----------------------------------------------------------------------------
385 MyWizard::MyWizard(wxFrame
*frame
, bool useSizer
)
387 SetExtraStyle(wxWIZARD_EX_HELPBUTTON
);
389 Create(frame
,wxID_ANY
,wxT("Absolutely Useless Wizard"),
390 wxBitmap(wiztest_xpm
),wxDefaultPosition
,
391 wxDEFAULT_DIALOG_STYLE
| wxRESIZE_BORDER
);
392 SetIcon(wxICON(sample
));
394 // Allow the bitmap to be expanded to fit the page height
395 if (frame
->GetMenuBar()->IsChecked(Wizard_ExpandBitmap
))
396 SetBitmapPlacement(wxWIZARD_VALIGN_CENTRE
);
398 // Enable scrolling adaptation
399 if (frame
->GetMenuBar()->IsChecked(Wizard_LargeWizard
))
400 SetLayoutAdaptationMode(wxDIALOG_ADAPTATION_MODE_ENABLED
);
402 // a wizard page may be either an object of predefined class
403 m_page1
= new wxWizardPageSimple(this);
405 /* wxStaticText *text = */ new wxStaticText(m_page1
, wxID_ANY
,
406 wxT("This wizard doesn't help you\nto do anything at all.\n")
408 wxT("The next pages will present you\nwith more useless controls."),
412 // ... or a derived class
413 wxRadioboxPage
*page3
= new wxRadioboxPage(this);
414 wxValidationPage
*page4
= new wxValidationPage(this);
416 // set the page order using a convenience function - could also use
417 // SetNext/Prev directly as below, but Chain() is shorter, avoids the risk
418 // of an error and can itself be chained, e.g. you could write
419 // page3.Chain(page4).Chain(page5) and so on.
422 // this page is not a wxWizardPageSimple, so we use SetNext/Prev to insert
423 // it into the chain of pages
424 wxCheckboxPage
*page2
= new wxCheckboxPage(this, m_page1
, page3
);
425 m_page1
->SetNext(page2
);
426 page3
->SetPrev(page2
);
430 // allow the wizard to size itself around the pages
431 GetPageAreaSizer()->Add(m_page1
);
435 // ----------------------------------------------------------------------------
437 // ----------------------------------------------------------------------------
439 MyFrame::MyFrame(const wxString
& title
)
440 :wxFrame((wxFrame
*)NULL
, wxID_ANY
, title
,
441 wxDefaultPosition
, wxSize(250, 150)) // small frame
443 wxMenu
*menuFile
= new wxMenu
;
444 menuFile
->Append(Wizard_RunModal
, wxT("&Run wizard modal...\tCtrl-R"));
445 menuFile
->Append(Wizard_RunNoSizer
, wxT("Run wizard &without sizer..."));
446 menuFile
->Append(Wizard_RunModeless
, wxT("Run wizard &modeless..."));
447 menuFile
->AppendSeparator();
448 menuFile
->Append(Wizard_Quit
, wxT("E&xit\tAlt-X"), wxT("Quit this program"));
450 wxMenu
*menuOptions
= new wxMenu
;
451 menuOptions
->AppendCheckItem(Wizard_LargeWizard
, wxT("&Scroll Wizard Pages"));
452 menuOptions
->AppendCheckItem(Wizard_ExpandBitmap
, wxT("Si&ze Bitmap To Page"));
454 wxMenu
*helpMenu
= new wxMenu
;
455 helpMenu
->Append(Wizard_About
, wxT("&About\tF1"), wxT("Show about dialog"));
457 // now append the freshly created menu to the menu bar...
458 wxMenuBar
*menuBar
= new wxMenuBar();
459 menuBar
->Append(menuFile
, wxT("&File"));
460 menuBar
->Append(menuOptions
, wxT("&Options"));
461 menuBar
->Append(helpMenu
, wxT("&Help"));
463 // ... and attach this menu bar to the frame
466 // also create status bar which we use in OnWizardCancel
469 #endif // wxUSE_STATUSBAR
472 void MyFrame::OnQuit(wxCommandEvent
& WXUNUSED(event
))
474 // true is to force the frame to close
478 void MyFrame::OnAbout(wxCommandEvent
& WXUNUSED(event
))
480 wxMessageBox(wxT("Demo of wxWizard class\n")
481 wxT("(c) 1999, 2000 Vadim Zeitlin"),
482 wxT("About wxWizard sample"), wxOK
| wxICON_INFORMATION
, this);
485 void MyFrame::OnRunWizard(wxCommandEvent
& WXUNUSED(event
))
487 MyWizard
wizard(this);
489 wizard
.RunWizard(wizard
.GetFirstPage());
492 void MyFrame::OnRunWizardNoSizer(wxCommandEvent
& WXUNUSED(event
))
494 MyWizard
wizard(this, false);
496 wizard
.RunWizard(wizard
.GetFirstPage());
499 void MyFrame::OnRunWizardModeless(wxCommandEvent
& WXUNUSED(event
))
501 MyWizard
*wizard
= new MyWizard(this);
502 wizard
->ShowPage(wizard
->GetFirstPage());
506 void MyFrame::OnWizardFinished(wxWizardEvent
& WXUNUSED(event
))
508 wxMessageBox(wxT("The wizard finished successfully."), wxT("Wizard notification"));
511 void MyFrame::OnWizardCancel(wxWizardEvent
& WXUNUSED(event
))
513 wxMessageBox(wxT("The wizard was cancelled."), wxT("Wizard notification"));