]> git.saurik.com Git - wxWidgets.git/blame - src/generic/wizard.cpp
cleaned up the checkbox creation code (~70 lines of code became 3)
[wxWidgets.git] / src / generic / wizard.cpp
CommitLineData
66cd017c
VZ
1///////////////////////////////////////////////////////////////////////////////
2// Name: generic/wizard.cpp
3// Purpose: generic implementation of wxWizard class
4// Author: Vadim Zeitlin
a0a48a3f
VZ
5// Modified by: Robert Cavanaugh
6// 1) Added capability for wxWizardPage to accept resources
7// 2) Added "Help" button handler stub
8// 3) Fixed ShowPage() bug on displaying bitmaps
66cd017c
VZ
9// Created: 15.08.99
10// RCS-ID: $Id$
11// Copyright: (c) 1999 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
12// Licence: wxWindows license
13///////////////////////////////////////////////////////////////////////////////
14
15// ============================================================================
16// declarations
17// ============================================================================
18
19// ----------------------------------------------------------------------------
20// headers
21// ----------------------------------------------------------------------------
22
23#ifdef __GNUG__
24 #pragma implementation ".h"
25#endif
26
27// For compilers that support precompilation, includes "wx.h".
28#include "wx/wxprec.h"
29
30#ifdef __BORLANDC__
31 #pragma hdrstop
32#endif
33
1e6feb95
VZ
34#if wxUSE_WIZARDDLG
35
66cd017c
VZ
36#ifndef WX_PRECOMP
37 #include "wx/dynarray.h"
38 #include "wx/intl.h"
b87654f3 39 #include "wx/statbmp.h"
f6bcfd97 40 #include "wx/button.h"
66cd017c
VZ
41#endif //WX_PRECOMP
42
43#include "wx/statline.h"
44
45#include "wx/wizard.h"
46
47// ----------------------------------------------------------------------------
48// simple types
49// ----------------------------------------------------------------------------
50
51WX_DEFINE_ARRAY(wxPanel *, wxArrayPages);
52
66cd017c
VZ
53// ----------------------------------------------------------------------------
54// event tables and such
55// ----------------------------------------------------------------------------
56
2e4df4bf
VZ
57DEFINE_EVENT_TYPE(wxEVT_WIZARD_PAGE_CHANGED)
58DEFINE_EVENT_TYPE(wxEVT_WIZARD_PAGE_CHANGING)
59DEFINE_EVENT_TYPE(wxEVT_WIZARD_CANCEL)
f80bf901 60DEFINE_EVENT_TYPE(wxEVT_WIZARD_HELP)
2e4df4bf 61
74b31181
VZ
62BEGIN_EVENT_TABLE(wxWizard, wxDialog)
63 EVT_BUTTON(wxID_CANCEL, wxWizard::OnCancel)
f80bf901
VZ
64 EVT_BUTTON(wxID_BACKWARD, wxWizard::OnBackOrNext)
65 EVT_BUTTON(wxID_FORWARD, wxWizard::OnBackOrNext)
66 EVT_BUTTON(wxID_HELP, wxWizard::OnHelp)
66cd017c
VZ
67END_EVENT_TABLE()
68
74b31181
VZ
69IMPLEMENT_DYNAMIC_CLASS(wxWizard, wxDialog)
70IMPLEMENT_ABSTRACT_CLASS(wxWizardPage, wxPanel)
71IMPLEMENT_DYNAMIC_CLASS(wxWizardPageSimple, wxWizardPage)
66cd017c
VZ
72IMPLEMENT_DYNAMIC_CLASS(wxWizardEvent, wxNotifyEvent)
73
74// ============================================================================
75// implementation
76// ============================================================================
77
74b31181
VZ
78// ----------------------------------------------------------------------------
79// wxWizardPage
80// ----------------------------------------------------------------------------
81
a0a48a3f
VZ
82wxWizardPage::wxWizardPage(wxWizard *parent,
83 const wxBitmap& bitmap,
84 const wxChar *resource)
85 : wxPanel(parent)
74b31181 86{
a0a48a3f
VZ
87 if ( resource != NULL )
88 {
5d5da6f6 89#if wxUSE_WX_RESOURCES
a0a48a3f
VZ
90 if ( !LoadFromResource(this, resource) )
91 {
92 wxFAIL_MSG(wxT("wxWizardPage LoadFromResource failed!!!!"));
93 }
c8804076 94#endif // wxUSE_RESOURCES
a0a48a3f
VZ
95 }
96
636d266b 97 m_bitmap = bitmap;
a0a48a3f 98
74b31181
VZ
99 // initially the page is hidden, it's shown only when it becomes current
100 Hide();
101}
102
103// ----------------------------------------------------------------------------
104// wxWizardPageSimple
105// ----------------------------------------------------------------------------
106
107wxWizardPage *wxWizardPageSimple::GetPrev() const
108{
109 return m_prev;
110}
111
112wxWizardPage *wxWizardPageSimple::GetNext() const
113{
114 return m_next;
115}
66cd017c
VZ
116// ----------------------------------------------------------------------------
117// generic wxWizard implementation
118// ----------------------------------------------------------------------------
119
77436c4c
JS
120void wxWizard::Init()
121{
122 m_posWizard = wxDefaultPosition;
123 m_page = (wxWizardPage *)NULL;
124 m_btnPrev = m_btnNext = NULL;
125 m_statbmp = NULL;
126}
127
128bool wxWizard::Create(wxWindow *parent,
74b31181
VZ
129 int id,
130 const wxString& title,
131 const wxBitmap& bitmap,
f6bcfd97 132 const wxPoint& pos)
66cd017c 133{
77436c4c
JS
134 m_posWizard = pos;
135 m_bitmap = bitmap ;
636d266b 136
f6bcfd97
BP
137 // just create the dialog itself here, the controls will be created in
138 // DoCreateControls() called later when we know our final size
139 m_page = (wxWizardPage *)NULL;
140 m_btnPrev = m_btnNext = NULL;
141 m_statbmp = NULL;
142
77436c4c 143 return wxDialog::Create(parent, id, title, pos);
f6bcfd97
BP
144}
145
146void wxWizard::DoCreateControls()
147{
148 // do nothing if the controls were already created
149 if ( WasCreated() )
150 return;
151
66cd017c
VZ
152 // constants defining the dialog layout
153 // ------------------------------------
154
155 // these constants define the position of the upper left corner of the
156 // bitmap or the page in the wizard
157 static const int X_MARGIN = 10;
158 static const int Y_MARGIN = 10;
159
160 // margin between the bitmap and the panel
161 static const int BITMAP_X_MARGIN = 15;
162
163 // margin between the bitmap and the static line
164 static const int BITMAP_Y_MARGIN = 15;
165
166 // margin between the static line and the buttons
167 static const int SEPARATOR_LINE_MARGIN = 15;
168
169 // margin between "Next >" and "Cancel" buttons
170 static const int BUTTON_MARGIN = 10;
171
172 // default width and height of the page
173 static const int DEFAULT_PAGE_WIDTH = 270;
174 static const int DEFAULT_PAGE_HEIGHT = 290;
175
66cd017c
VZ
176 // create controls
177 // ---------------
178
179 wxSize sizeBtn = wxButton::GetDefaultSize();
180
66cd017c
VZ
181 // the global dialog layout is: a row of buttons at the bottom (aligned to
182 // the right), the static line above them, the bitmap (if any) on the left
183 // of the upper part of the dialog and the panel in the remaining space
184 m_x = X_MARGIN;
185 m_y = Y_MARGIN;
f6bcfd97
BP
186
187 int defaultHeight;
188 if ( m_bitmap.Ok() )
66cd017c 189 {
f6bcfd97 190 m_statbmp = new wxStaticBitmap(this, -1, m_bitmap, wxPoint(m_x, m_y));
66cd017c 191
f6bcfd97
BP
192 m_x += m_bitmap.GetWidth() + BITMAP_X_MARGIN;
193
194 defaultHeight = m_bitmap.GetHeight();
66cd017c
VZ
195 }
196 else
197 {
f1df0927
VZ
198 m_statbmp = (wxStaticBitmap *)NULL;
199
f6bcfd97 200 defaultHeight = DEFAULT_PAGE_HEIGHT;
66cd017c
VZ
201 }
202
f6bcfd97
BP
203 // use default size if none given and also make sure that the dialog is
204 // not less than the default size
205 m_height = m_sizePage.y == -1 ? defaultHeight : m_sizePage.y;
206 m_width = m_sizePage.x == -1 ? DEFAULT_PAGE_WIDTH : m_sizePage.x;
207 if ( m_height < defaultHeight )
208 m_height = defaultHeight;
209 if ( m_width < DEFAULT_PAGE_WIDTH )
210 m_width = DEFAULT_PAGE_WIDTH;
66cd017c
VZ
211
212 int x = X_MARGIN;
213 int y = m_y + m_height + BITMAP_Y_MARGIN;
74b31181
VZ
214
215#if wxUSE_STATLINE
66cd017c
VZ
216 (void)new wxStaticLine(this, -1, wxPoint(x, y),
217 wxSize(m_x + m_width - x, 2));
f6bcfd97 218#endif // wxUSE_STATLINE
74b31181 219
66cd017c
VZ
220 x = m_x + m_width - 3*sizeBtn.x - BUTTON_MARGIN;
221 y += SEPARATOR_LINE_MARGIN;
77436c4c
JS
222
223 if (GetExtraStyle() & wxWIZARD_EX_HELPBUTTON)
224 {
225 x -= sizeBtn.x;
226 x -= BUTTON_MARGIN ;
227
a8973b12 228 (void)new wxButton(this, wxID_HELP, _("&Help"), wxPoint(x, y), sizeBtn);
77436c4c
JS
229 x += sizeBtn.x;
230 x += BUTTON_MARGIN ;
231 }
232
f6bcfd97 233 m_btnPrev = new wxButton(this, wxID_BACKWARD, _("< &Back"), wxPoint(x, y), sizeBtn);
66cd017c
VZ
234
235 x += sizeBtn.x;
f6bcfd97 236 m_btnNext = new wxButton(this, wxID_FORWARD, _("&Next >"), wxPoint(x, y), sizeBtn);
66cd017c
VZ
237
238 x += sizeBtn.x + BUTTON_MARGIN;
77436c4c 239 (void)new wxButton(this, wxID_CANCEL, _("&Cancel"), wxPoint(x, y), sizeBtn);
66cd017c
VZ
240
241 // position and size the dialog
242 // ----------------------------
243
f6bcfd97
BP
244 SetClientSize(m_x + m_width + X_MARGIN,
245 m_y + m_height + BITMAP_Y_MARGIN +
246 SEPARATOR_LINE_MARGIN + sizeBtn.y + Y_MARGIN);
66cd017c 247
f6bcfd97 248 if ( m_posWizard == wxDefaultPosition )
66cd017c 249 {
91b4c08d 250 CentreOnScreen();
66cd017c
VZ
251 }
252}
253
f6bcfd97
BP
254void wxWizard::SetPageSize(const wxSize& size)
255{
256 // otherwise it will have no effect now as it's too late...
257 wxASSERT_MSG( !WasCreated(), _T("should be called before RunWizard()!") );
258
259 m_sizePage = size;
260}
261
74b31181 262bool wxWizard::ShowPage(wxWizardPage *page, bool goingForward)
66cd017c 263{
223d09f6 264 wxASSERT_MSG( page != m_page, wxT("this is useless") );
66cd017c 265
74b31181
VZ
266 // we'll use this to decide whether we have to change the label of this
267 // button or not (initially the label is "Next")
268 bool btnLabelWasNext = TRUE;
66cd017c 269
7cc5041d
VZ
270 // Modified 10-20-2001 Robert Cavanaugh.
271 // Fixed bug for displaying a new bitmap
272 // in each *consecutive* page
f1df0927 273
7cc5041d
VZ
274 // flag to indicate if this page uses a new bitmap
275 bool bmpIsDefault = TRUE;
276
277 // use these labels to determine if we need to change the bitmap
278 // for this page
279 wxBitmap PreviousBitmap = wxNullBitmap;
280 wxBitmap ThisBitmap = wxNullBitmap;
281
282 // check for previous page
74b31181 283 if ( m_page )
66cd017c 284 {
74b31181
VZ
285 // send the event to the old page
286 wxWizardEvent event(wxEVT_WIZARD_PAGE_CHANGING, GetId(), goingForward);
287 if ( m_page->GetEventHandler()->ProcessEvent(event) &&
288 !event.IsAllowed() )
289 {
290 // vetoed by the page
66cd017c 291 return FALSE;
74b31181 292 }
66cd017c 293
74b31181
VZ
294 m_page->Hide();
295
296 btnLabelWasNext = m_page->GetNext() != (wxWizardPage *)NULL;
7cc5041d
VZ
297
298 // Get the bitmap of the previous page (if it exists)
299 if(m_page->GetBitmap().Ok())
dee5b92f 300 {
7cc5041d
VZ
301 PreviousBitmap = m_page->GetBitmap();
302 }
dee5b92f 303 }
66cd017c 304
7cc5041d 305 // set the new page
66cd017c 306 m_page = page;
66cd017c 307
74b31181
VZ
308 // is this the end?
309 if ( !m_page )
66cd017c 310 {
74b31181
VZ
311 // terminate successfully
312 EndModal(wxID_OK);
74b31181
VZ
313 return TRUE;
314 }
66cd017c 315
7cc5041d 316 // send the change event to the new page now
74b31181
VZ
317 wxWizardEvent event(wxEVT_WIZARD_PAGE_CHANGED, GetId(), goingForward);
318 (void)m_page->GetEventHandler()->ProcessEvent(event);
66cd017c 319
74b31181
VZ
320 // position and show the new page
321 (void)m_page->TransferDataToWindow();
322 m_page->SetSize(m_x, m_y, m_width, m_height);
323 m_page->Show();
66cd017c 324
7cc5041d
VZ
325 // check if bitmap needs to be updated
326 // update default flag as well
327 if(m_page->GetBitmap().Ok())
328 {
329 ThisBitmap = m_page->GetBitmap();
330 bmpIsDefault = FALSE;
331 }
332
333 // change the bitmap if:
334 // 1) a default bitmap was selected in constructor
335 // 2) this page was constructed with a bitmap
336 // 3) this bitmap is not the previous bitmap
337 if( m_statbmp && (ThisBitmap != PreviousBitmap) )
f1df0927 338 {
cfd88569
VZ
339 wxBitmap bmp;
340 if ( bmpIsDefault )
341 bmp = m_bitmap;
342 else
343 bmp = m_page->GetBitmap();
344 m_statbmp->SetBitmap(bmp);
f1df0927
VZ
345 }
346
74b31181
VZ
347 // and update the buttons state
348 m_btnPrev->Enable(m_page->GetPrev() != (wxWizardPage *)NULL);
66cd017c 349
8f177c8e
VZ
350 bool hasNext = m_page->GetNext() != (wxWizardPage *)NULL;
351 if ( btnLabelWasNext != hasNext )
66cd017c 352 {
74b31181 353 // need to update
e9fa7581
OK
354 if (btnLabelWasNext)
355 m_btnNext->SetLabel(_("&Finish"));
356 else
357 m_btnNext->SetLabel(_("&Next >"));
66cd017c 358 }
74b31181 359 // nothing to do: the label was already correct
66cd017c 360
74b31181 361 return TRUE;
66cd017c
VZ
362}
363
74b31181 364bool wxWizard::RunWizard(wxWizardPage *firstPage)
66cd017c 365{
223d09f6 366 wxCHECK_MSG( firstPage, FALSE, wxT("can't run empty wizard") );
66cd017c 367
f6bcfd97
BP
368 DoCreateControls();
369
66cd017c 370 // can't return FALSE here because there is no old page
74b31181 371 (void)ShowPage(firstPage, TRUE /* forward */);
66cd017c
VZ
372
373 return ShowModal() == wxID_OK;
374}
375
74b31181 376wxWizardPage *wxWizard::GetCurrentPage() const
66cd017c 377{
74b31181 378 return m_page;
66cd017c
VZ
379}
380
4fe5383d
VZ
381wxSize wxWizard::GetPageSize() const
382{
f6bcfd97
BP
383 // make sure that the controls are created because otherwise m_width and
384 // m_height would be both still -1
385 wxConstCast(this, wxWizard)->DoCreateControls();
386
4fe5383d
VZ
387 return wxSize(m_width, m_height);
388}
389
74b31181 390void wxWizard::OnCancel(wxCommandEvent& WXUNUSED(event))
66cd017c 391{
74b31181
VZ
392 // this function probably can never be called when we don't have an active
393 // page, but a small extra check won't hurt
394 wxWindow *win = m_page ? (wxWindow *)m_page : (wxWindow *)this;
395
66cd017c 396 wxWizardEvent event(wxEVT_WIZARD_CANCEL, GetId());
74b31181 397 if ( !win->GetEventHandler()->ProcessEvent(event) || event.IsAllowed() )
66cd017c
VZ
398 {
399 // no objections - close the dialog
400 EndModal(wxID_CANCEL);
401 }
402 //else: request to Cancel ignored
403}
404
74b31181 405void wxWizard::OnBackOrNext(wxCommandEvent& event)
66cd017c
VZ
406{
407 wxASSERT_MSG( (event.GetEventObject() == m_btnNext) ||
408 (event.GetEventObject() == m_btnPrev),
223d09f6 409 wxT("unknown button") );
66cd017c 410
f6bcfd97
BP
411 // ask the current page first: notice that we do it before calling
412 // GetNext/Prev() because the data transfered from the controls of the page
413 // may change the value returned by these methods
414 if ( m_page && !m_page->TransferDataFromWindow() )
415 {
416 // the page data is incorrect, don't do anything
417 return;
418 }
419
74b31181 420 bool forward = event.GetEventObject() == m_btnNext;
66cd017c 421
74b31181
VZ
422 wxWizardPage *page;
423 if ( forward )
66cd017c 424 {
74b31181 425 page = m_page->GetNext();
66cd017c 426 }
74b31181 427 else // back
66cd017c 428 {
74b31181
VZ
429 page = m_page->GetPrev();
430
223d09f6 431 wxASSERT_MSG( page, wxT("\"<Back\" button should have been disabled") );
66cd017c 432 }
74b31181
VZ
433
434 // just pass to the new page (or may be not - but we don't care here)
435 (void)ShowPage(page, forward);
66cd017c
VZ
436}
437
f80bf901
VZ
438void wxWizard::OnHelp(wxCommandEvent& WXUNUSED(event))
439{
440 // this function probably can never be called when we don't have an active
441 // page, but a small extra check won't hurt
442 if(m_page != NULL)
443 {
444 // Create and send the help event to the specific page handler
445 // event data contains the active page so that context-sensitive
446 // help is possible
447 wxWizardEvent eventHelp(wxEVT_WIZARD_HELP, GetId(), TRUE, m_page);
448 (void)m_page->GetEventHandler()->ProcessEvent(eventHelp);
449 }
450}
451
452
66cd017c
VZ
453// ----------------------------------------------------------------------------
454// our public interface
455// ----------------------------------------------------------------------------
456
74b31181
VZ
457/* static */
458wxWizard *wxWizardBase::Create(wxWindow *parent,
459 int id,
460 const wxString& title,
461 const wxBitmap& bitmap,
462 const wxPoint& pos,
f6bcfd97 463 const wxSize& WXUNUSED(size))
66cd017c 464{
f6bcfd97 465 return new wxWizard(parent, id, title, bitmap, pos);
66cd017c
VZ
466}
467
468// ----------------------------------------------------------------------------
469// wxWizardEvent
470// ----------------------------------------------------------------------------
471
f80bf901 472wxWizardEvent::wxWizardEvent(wxEventType type, int id, bool direction, wxWizardPage* page)
66cd017c
VZ
473 : wxNotifyEvent(type, id)
474{
f80bf901
VZ
475 // Modified 10-20-2001 Robert Cavanaugh
476 // add the active page to the event data
74b31181 477 m_direction = direction;
f80bf901 478 m_page = page;
66cd017c 479}
74b31181 480
1e6feb95 481#endif // wxUSE_WIZARDDLG