]> git.saurik.com Git - wxWidgets.git/blame - src/generic/wizard.cpp
use WS_EX_CONTROLPARENT to ensure correct Alt-mnemonic handling with nested panels
[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__
91c68292 24 #pragma implementation "wizardg.h"
66cd017c
VZ
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)
91c68292
VZ
67
68 EVT_WIZARD_PAGE_CHANGED(-1, wxWizard::OnWizEvent)
69 EVT_WIZARD_PAGE_CHANGING(-1, wxWizard::OnWizEvent)
70 EVT_WIZARD_CANCEL(-1, wxWizard::OnWizEvent)
71 EVT_WIZARD_HELP(-1, wxWizard::OnWizEvent)
66cd017c
VZ
72END_EVENT_TABLE()
73
74b31181
VZ
74IMPLEMENT_DYNAMIC_CLASS(wxWizard, wxDialog)
75IMPLEMENT_ABSTRACT_CLASS(wxWizardPage, wxPanel)
76IMPLEMENT_DYNAMIC_CLASS(wxWizardPageSimple, wxWizardPage)
66cd017c
VZ
77IMPLEMENT_DYNAMIC_CLASS(wxWizardEvent, wxNotifyEvent)
78
79// ============================================================================
80// implementation
81// ============================================================================
82
74b31181
VZ
83// ----------------------------------------------------------------------------
84// wxWizardPage
85// ----------------------------------------------------------------------------
86
c7de4135
VS
87void wxWizardPage::Init()
88{
89 m_bitmap = wxNullBitmap;
90}
91
a0a48a3f
VZ
92wxWizardPage::wxWizardPage(wxWizard *parent,
93 const wxBitmap& bitmap,
94 const wxChar *resource)
74b31181 95{
c7de4135
VS
96 Create(parent, bitmap, resource);
97}
91c68292 98
c7de4135
VS
99bool wxWizardPage::Create(wxWizard *parent,
100 const wxBitmap& bitmap,
101 const wxChar *resource)
102{
103 if ( !wxPanel::Create(parent, -1) )
104 return FALSE;
105
a0a48a3f
VZ
106 if ( resource != NULL )
107 {
5d5da6f6 108#if wxUSE_WX_RESOURCES
a0a48a3f
VZ
109 if ( !LoadFromResource(this, resource) )
110 {
111 wxFAIL_MSG(wxT("wxWizardPage LoadFromResource failed!!!!"));
112 }
c8804076 113#endif // wxUSE_RESOURCES
a0a48a3f
VZ
114 }
115
636d266b 116 m_bitmap = bitmap;
a0a48a3f 117
74b31181
VZ
118 // initially the page is hidden, it's shown only when it becomes current
119 Hide();
91c68292 120
c7de4135 121 return TRUE;
74b31181
VZ
122}
123
124// ----------------------------------------------------------------------------
125// wxWizardPageSimple
126// ----------------------------------------------------------------------------
127
128wxWizardPage *wxWizardPageSimple::GetPrev() const
129{
130 return m_prev;
131}
132
133wxWizardPage *wxWizardPageSimple::GetNext() const
134{
135 return m_next;
136}
66cd017c
VZ
137// ----------------------------------------------------------------------------
138// generic wxWizard implementation
139// ----------------------------------------------------------------------------
140
77436c4c
JS
141void wxWizard::Init()
142{
143 m_posWizard = wxDefaultPosition;
144 m_page = (wxWizardPage *)NULL;
145 m_btnPrev = m_btnNext = NULL;
146 m_statbmp = NULL;
147}
148
149bool wxWizard::Create(wxWindow *parent,
74b31181
VZ
150 int id,
151 const wxString& title,
152 const wxBitmap& bitmap,
f6bcfd97 153 const wxPoint& pos)
66cd017c 154{
77436c4c
JS
155 m_posWizard = pos;
156 m_bitmap = bitmap ;
636d266b 157
f6bcfd97
BP
158 // just create the dialog itself here, the controls will be created in
159 // DoCreateControls() called later when we know our final size
160 m_page = (wxWizardPage *)NULL;
161 m_btnPrev = m_btnNext = NULL;
162 m_statbmp = NULL;
163
77436c4c 164 return wxDialog::Create(parent, id, title, pos);
f6bcfd97
BP
165}
166
167void wxWizard::DoCreateControls()
168{
169 // do nothing if the controls were already created
170 if ( WasCreated() )
171 return;
172
66cd017c
VZ
173 // constants defining the dialog layout
174 // ------------------------------------
175
176 // these constants define the position of the upper left corner of the
177 // bitmap or the page in the wizard
178 static const int X_MARGIN = 10;
179 static const int Y_MARGIN = 10;
180
181 // margin between the bitmap and the panel
182 static const int BITMAP_X_MARGIN = 15;
183
184 // margin between the bitmap and the static line
185 static const int BITMAP_Y_MARGIN = 15;
186
187 // margin between the static line and the buttons
188 static const int SEPARATOR_LINE_MARGIN = 15;
189
190 // margin between "Next >" and "Cancel" buttons
191 static const int BUTTON_MARGIN = 10;
192
193 // default width and height of the page
194 static const int DEFAULT_PAGE_WIDTH = 270;
195 static const int DEFAULT_PAGE_HEIGHT = 290;
196
66cd017c
VZ
197 // create controls
198 // ---------------
199
200 wxSize sizeBtn = wxButton::GetDefaultSize();
201
66cd017c
VZ
202 // the global dialog layout is: a row of buttons at the bottom (aligned to
203 // the right), the static line above them, the bitmap (if any) on the left
204 // of the upper part of the dialog and the panel in the remaining space
205 m_x = X_MARGIN;
206 m_y = Y_MARGIN;
f6bcfd97
BP
207
208 int defaultHeight;
209 if ( m_bitmap.Ok() )
66cd017c 210 {
f6bcfd97 211 m_statbmp = new wxStaticBitmap(this, -1, m_bitmap, wxPoint(m_x, m_y));
66cd017c 212
f6bcfd97
BP
213 m_x += m_bitmap.GetWidth() + BITMAP_X_MARGIN;
214
215 defaultHeight = m_bitmap.GetHeight();
66cd017c
VZ
216 }
217 else
218 {
f1df0927
VZ
219 m_statbmp = (wxStaticBitmap *)NULL;
220
f6bcfd97 221 defaultHeight = DEFAULT_PAGE_HEIGHT;
66cd017c
VZ
222 }
223
f6bcfd97
BP
224 // use default size if none given and also make sure that the dialog is
225 // not less than the default size
226 m_height = m_sizePage.y == -1 ? defaultHeight : m_sizePage.y;
227 m_width = m_sizePage.x == -1 ? DEFAULT_PAGE_WIDTH : m_sizePage.x;
228 if ( m_height < defaultHeight )
229 m_height = defaultHeight;
230 if ( m_width < DEFAULT_PAGE_WIDTH )
231 m_width = DEFAULT_PAGE_WIDTH;
66cd017c
VZ
232
233 int x = X_MARGIN;
234 int y = m_y + m_height + BITMAP_Y_MARGIN;
74b31181
VZ
235
236#if wxUSE_STATLINE
66cd017c
VZ
237 (void)new wxStaticLine(this, -1, wxPoint(x, y),
238 wxSize(m_x + m_width - x, 2));
f6bcfd97 239#endif // wxUSE_STATLINE
74b31181 240
66cd017c
VZ
241 x = m_x + m_width - 3*sizeBtn.x - BUTTON_MARGIN;
242 y += SEPARATOR_LINE_MARGIN;
77436c4c
JS
243
244 if (GetExtraStyle() & wxWIZARD_EX_HELPBUTTON)
245 {
246 x -= sizeBtn.x;
247 x -= BUTTON_MARGIN ;
248
a8973b12 249 (void)new wxButton(this, wxID_HELP, _("&Help"), wxPoint(x, y), sizeBtn);
77436c4c
JS
250 x += sizeBtn.x;
251 x += BUTTON_MARGIN ;
252 }
253
f6bcfd97 254 m_btnPrev = new wxButton(this, wxID_BACKWARD, _("< &Back"), wxPoint(x, y), sizeBtn);
66cd017c
VZ
255
256 x += sizeBtn.x;
f6bcfd97 257 m_btnNext = new wxButton(this, wxID_FORWARD, _("&Next >"), wxPoint(x, y), sizeBtn);
66cd017c
VZ
258
259 x += sizeBtn.x + BUTTON_MARGIN;
77436c4c 260 (void)new wxButton(this, wxID_CANCEL, _("&Cancel"), wxPoint(x, y), sizeBtn);
66cd017c
VZ
261
262 // position and size the dialog
263 // ----------------------------
264
f6bcfd97
BP
265 SetClientSize(m_x + m_width + X_MARGIN,
266 m_y + m_height + BITMAP_Y_MARGIN +
267 SEPARATOR_LINE_MARGIN + sizeBtn.y + Y_MARGIN);
66cd017c 268
f6bcfd97 269 if ( m_posWizard == wxDefaultPosition )
66cd017c 270 {
91b4c08d 271 CentreOnScreen();
66cd017c
VZ
272 }
273}
274
f6bcfd97
BP
275void wxWizard::SetPageSize(const wxSize& size)
276{
277 // otherwise it will have no effect now as it's too late...
278 wxASSERT_MSG( !WasCreated(), _T("should be called before RunWizard()!") );
279
280 m_sizePage = size;
281}
282
c73b439f
VZ
283void wxWizard::Fit(const wxWizardPage *page)
284{
285 // otherwise it will have no effect now as it's too late...
286 wxASSERT_MSG( !WasCreated(), _T("should be called before RunWizard()!") );
287
288 wxSize sizeMax;
289 while ( page )
290 {
291 wxSize size = page->GetBestSize();
292
293 if ( size.x > sizeMax.x )
294 sizeMax.x = size.x;
295
296 if ( size.y > sizeMax.y )
297 sizeMax.y = size.y;
298
299 page = page->GetNext();
300 }
301
302 if ( sizeMax.x > m_sizePage.x )
303 m_sizePage.x = sizeMax.x;
304
305 if ( sizeMax.y > m_sizePage.y )
306 m_sizePage.y = sizeMax.y;
307}
308
74b31181 309bool wxWizard::ShowPage(wxWizardPage *page, bool goingForward)
66cd017c 310{
223d09f6 311 wxASSERT_MSG( page != m_page, wxT("this is useless") );
66cd017c 312
74b31181
VZ
313 // we'll use this to decide whether we have to change the label of this
314 // button or not (initially the label is "Next")
315 bool btnLabelWasNext = TRUE;
66cd017c 316
7cc5041d
VZ
317 // Modified 10-20-2001 Robert Cavanaugh.
318 // Fixed bug for displaying a new bitmap
319 // in each *consecutive* page
f1df0927 320
7cc5041d
VZ
321 // flag to indicate if this page uses a new bitmap
322 bool bmpIsDefault = TRUE;
323
324 // use these labels to determine if we need to change the bitmap
325 // for this page
326 wxBitmap PreviousBitmap = wxNullBitmap;
327 wxBitmap ThisBitmap = wxNullBitmap;
328
329 // check for previous page
74b31181 330 if ( m_page )
66cd017c 331 {
74b31181
VZ
332 // send the event to the old page
333 wxWizardEvent event(wxEVT_WIZARD_PAGE_CHANGING, GetId(), goingForward);
334 if ( m_page->GetEventHandler()->ProcessEvent(event) &&
335 !event.IsAllowed() )
336 {
337 // vetoed by the page
66cd017c 338 return FALSE;
74b31181 339 }
66cd017c 340
74b31181
VZ
341 m_page->Hide();
342
343 btnLabelWasNext = m_page->GetNext() != (wxWizardPage *)NULL;
7cc5041d
VZ
344
345 // Get the bitmap of the previous page (if it exists)
346 if(m_page->GetBitmap().Ok())
dee5b92f 347 {
7cc5041d
VZ
348 PreviousBitmap = m_page->GetBitmap();
349 }
dee5b92f 350 }
66cd017c 351
7cc5041d 352 // set the new page
66cd017c 353 m_page = page;
66cd017c 354
74b31181
VZ
355 // is this the end?
356 if ( !m_page )
66cd017c 357 {
74b31181
VZ
358 // terminate successfully
359 EndModal(wxID_OK);
74b31181
VZ
360 return TRUE;
361 }
66cd017c 362
7cc5041d 363 // send the change event to the new page now
74b31181
VZ
364 wxWizardEvent event(wxEVT_WIZARD_PAGE_CHANGED, GetId(), goingForward);
365 (void)m_page->GetEventHandler()->ProcessEvent(event);
66cd017c 366
74b31181
VZ
367 // position and show the new page
368 (void)m_page->TransferDataToWindow();
369 m_page->SetSize(m_x, m_y, m_width, m_height);
370 m_page->Show();
66cd017c 371
7cc5041d
VZ
372 // check if bitmap needs to be updated
373 // update default flag as well
374 if(m_page->GetBitmap().Ok())
375 {
376 ThisBitmap = m_page->GetBitmap();
377 bmpIsDefault = FALSE;
378 }
379
380 // change the bitmap if:
381 // 1) a default bitmap was selected in constructor
382 // 2) this page was constructed with a bitmap
383 // 3) this bitmap is not the previous bitmap
384 if( m_statbmp && (ThisBitmap != PreviousBitmap) )
f1df0927 385 {
cfd88569
VZ
386 wxBitmap bmp;
387 if ( bmpIsDefault )
388 bmp = m_bitmap;
389 else
390 bmp = m_page->GetBitmap();
391 m_statbmp->SetBitmap(bmp);
f1df0927
VZ
392 }
393
74b31181
VZ
394 // and update the buttons state
395 m_btnPrev->Enable(m_page->GetPrev() != (wxWizardPage *)NULL);
66cd017c 396
8f177c8e
VZ
397 bool hasNext = m_page->GetNext() != (wxWizardPage *)NULL;
398 if ( btnLabelWasNext != hasNext )
66cd017c 399 {
74b31181 400 // need to update
e9fa7581
OK
401 if (btnLabelWasNext)
402 m_btnNext->SetLabel(_("&Finish"));
403 else
404 m_btnNext->SetLabel(_("&Next >"));
66cd017c 405 }
74b31181 406 // nothing to do: the label was already correct
66cd017c 407
74b31181 408 return TRUE;
66cd017c
VZ
409}
410
74b31181 411bool wxWizard::RunWizard(wxWizardPage *firstPage)
66cd017c 412{
223d09f6 413 wxCHECK_MSG( firstPage, FALSE, wxT("can't run empty wizard") );
66cd017c 414
f6bcfd97
BP
415 DoCreateControls();
416
66cd017c 417 // can't return FALSE here because there is no old page
74b31181 418 (void)ShowPage(firstPage, TRUE /* forward */);
66cd017c
VZ
419
420 return ShowModal() == wxID_OK;
421}
422
74b31181 423wxWizardPage *wxWizard::GetCurrentPage() const
66cd017c 424{
74b31181 425 return m_page;
66cd017c
VZ
426}
427
4fe5383d
VZ
428wxSize wxWizard::GetPageSize() const
429{
f6bcfd97
BP
430 // make sure that the controls are created because otherwise m_width and
431 // m_height would be both still -1
432 wxConstCast(this, wxWizard)->DoCreateControls();
433
4fe5383d
VZ
434 return wxSize(m_width, m_height);
435}
436
74b31181 437void wxWizard::OnCancel(wxCommandEvent& WXUNUSED(event))
66cd017c 438{
74b31181
VZ
439 // this function probably can never be called when we don't have an active
440 // page, but a small extra check won't hurt
441 wxWindow *win = m_page ? (wxWindow *)m_page : (wxWindow *)this;
442
66cd017c 443 wxWizardEvent event(wxEVT_WIZARD_CANCEL, GetId());
74b31181 444 if ( !win->GetEventHandler()->ProcessEvent(event) || event.IsAllowed() )
66cd017c
VZ
445 {
446 // no objections - close the dialog
447 EndModal(wxID_CANCEL);
448 }
449 //else: request to Cancel ignored
450}
451
74b31181 452void wxWizard::OnBackOrNext(wxCommandEvent& event)
66cd017c
VZ
453{
454 wxASSERT_MSG( (event.GetEventObject() == m_btnNext) ||
455 (event.GetEventObject() == m_btnPrev),
223d09f6 456 wxT("unknown button") );
66cd017c 457
f6bcfd97
BP
458 // ask the current page first: notice that we do it before calling
459 // GetNext/Prev() because the data transfered from the controls of the page
460 // may change the value returned by these methods
461 if ( m_page && !m_page->TransferDataFromWindow() )
462 {
463 // the page data is incorrect, don't do anything
464 return;
465 }
466
74b31181 467 bool forward = event.GetEventObject() == m_btnNext;
66cd017c 468
74b31181
VZ
469 wxWizardPage *page;
470 if ( forward )
66cd017c 471 {
74b31181 472 page = m_page->GetNext();
66cd017c 473 }
74b31181 474 else // back
66cd017c 475 {
74b31181
VZ
476 page = m_page->GetPrev();
477
223d09f6 478 wxASSERT_MSG( page, wxT("\"<Back\" button should have been disabled") );
66cd017c 479 }
74b31181
VZ
480
481 // just pass to the new page (or may be not - but we don't care here)
482 (void)ShowPage(page, forward);
66cd017c
VZ
483}
484
f80bf901
VZ
485void wxWizard::OnHelp(wxCommandEvent& WXUNUSED(event))
486{
487 // this function probably can never be called when we don't have an active
488 // page, but a small extra check won't hurt
489 if(m_page != NULL)
490 {
491 // Create and send the help event to the specific page handler
492 // event data contains the active page so that context-sensitive
493 // help is possible
494 wxWizardEvent eventHelp(wxEVT_WIZARD_HELP, GetId(), TRUE, m_page);
495 (void)m_page->GetEventHandler()->ProcessEvent(eventHelp);
496 }
497}
498
91c68292
VZ
499void wxWizard::OnWizEvent(wxWizardEvent& event)
500{
501 // the dialogs have wxWS_EX_BLOCK_EVENTS style on by default but we want to
502 // propagate wxEVT_WIZARD_XXX to the parent (if any), so do it manually
503 if ( !(GetExtraStyle() & wxWS_EX_BLOCK_EVENTS) )
504 {
505 // the event will be propagated anyhow
506 return;
507 }
508
509 wxWindow *parent = GetParent();
510
511 if ( !parent || !parent->GetEventHandler()->ProcessEvent(event) )
512 {
513 event.Skip();
514 }
515}
f80bf901 516
66cd017c
VZ
517// ----------------------------------------------------------------------------
518// our public interface
519// ----------------------------------------------------------------------------
520
74b31181
VZ
521/* static */
522wxWizard *wxWizardBase::Create(wxWindow *parent,
523 int id,
524 const wxString& title,
525 const wxBitmap& bitmap,
526 const wxPoint& pos,
f6bcfd97 527 const wxSize& WXUNUSED(size))
66cd017c 528{
f6bcfd97 529 return new wxWizard(parent, id, title, bitmap, pos);
66cd017c
VZ
530}
531
532// ----------------------------------------------------------------------------
533// wxWizardEvent
534// ----------------------------------------------------------------------------
535
f80bf901 536wxWizardEvent::wxWizardEvent(wxEventType type, int id, bool direction, wxWizardPage* page)
66cd017c
VZ
537 : wxNotifyEvent(type, id)
538{
f80bf901
VZ
539 // Modified 10-20-2001 Robert Cavanaugh
540 // add the active page to the event data
74b31181 541 m_direction = direction;
f80bf901 542 m_page = page;
66cd017c 543}
74b31181 544
1e6feb95 545#endif // wxUSE_WIZARDDLG