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