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