]> git.saurik.com Git - wxWidgets.git/blame - src/generic/wizard.cpp
must be AnyState , not NoState to get all elements (error in apple's doc, reported)
[wxWidgets.git] / src / generic / wizard.cpp
CommitLineData
66cd017c 1///////////////////////////////////////////////////////////////////////////////
94c09a19 2// Name: src/generic/wizard.cpp
66cd017c
VZ
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
07f20d9a 9// Robert Vazan (sizers)
66cd017c
VZ
10// Created: 15.08.99
11// RCS-ID: $Id$
12// Copyright: (c) 1999 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
65571936 13// Licence: wxWindows licence
66cd017c
VZ
14///////////////////////////////////////////////////////////////////////////////
15
16// ============================================================================
17// declarations
18// ============================================================================
19
20// ----------------------------------------------------------------------------
21// headers
22// ----------------------------------------------------------------------------
23
66cd017c
VZ
24// For compilers that support precompilation, includes "wx.h".
25#include "wx/wxprec.h"
26
27#ifdef __BORLANDC__
28 #pragma hdrstop
29#endif
30
1e6feb95
VZ
31#if wxUSE_WIZARDDLG
32
66cd017c
VZ
33#ifndef WX_PRECOMP
34 #include "wx/dynarray.h"
35 #include "wx/intl.h"
b87654f3 36 #include "wx/statbmp.h"
f6bcfd97 37 #include "wx/button.h"
66cd017c
VZ
38#endif //WX_PRECOMP
39
40#include "wx/statline.h"
07f20d9a 41#include "wx/sizer.h"
a6328131 42#include "wx/settings.h"
66cd017c
VZ
43
44#include "wx/wizard.h"
45
aedd6d6a
VZ
46// ----------------------------------------------------------------------------
47// wxWizardSizer
48// ----------------------------------------------------------------------------
49
50class wxWizardSizer : public wxSizer
51{
52public:
53 wxWizardSizer(wxWizard *owner);
54
0a9ed8f1
VZ
55 virtual wxSizerItem *Insert(size_t index, wxSizerItem *item);
56
88517d90
VZ
57 virtual void RecalcSizes();
58 virtual wxSize CalcMin();
aedd6d6a 59
88517d90 60 // get the max size of all wizard pages
aedd6d6a 61 wxSize GetMaxChildSize();
88517d90
VZ
62
63 // return the border which can be either set using wxWizard::SetBorder() or
64 // have default value
65 int GetBorder() const;
ca65c044 66
0a9ed8f1
VZ
67 // hide the pages which we temporarily "show" when they're added to this
68 // sizer (see Insert())
69 void HidePages();
70
aedd6d6a
VZ
71private:
72 wxSize SiblingSize(wxSizerItem *child);
ca65c044 73
aedd6d6a
VZ
74 wxWizard *m_owner;
75 bool m_childSizeValid;
76 wxSize m_childSize;
77};
78
66cd017c
VZ
79// ----------------------------------------------------------------------------
80// event tables and such
81// ----------------------------------------------------------------------------
82
2e4df4bf
VZ
83DEFINE_EVENT_TYPE(wxEVT_WIZARD_PAGE_CHANGED)
84DEFINE_EVENT_TYPE(wxEVT_WIZARD_PAGE_CHANGING)
85DEFINE_EVENT_TYPE(wxEVT_WIZARD_CANCEL)
1d30a0a1 86DEFINE_EVENT_TYPE(wxEVT_WIZARD_FINISHED)
f80bf901 87DEFINE_EVENT_TYPE(wxEVT_WIZARD_HELP)
2e4df4bf 88
74b31181
VZ
89BEGIN_EVENT_TABLE(wxWizard, wxDialog)
90 EVT_BUTTON(wxID_CANCEL, wxWizard::OnCancel)
f80bf901
VZ
91 EVT_BUTTON(wxID_BACKWARD, wxWizard::OnBackOrNext)
92 EVT_BUTTON(wxID_FORWARD, wxWizard::OnBackOrNext)
93 EVT_BUTTON(wxID_HELP, wxWizard::OnHelp)
91c68292 94
ca65c044
WS
95 EVT_WIZARD_PAGE_CHANGED(wxID_ANY, wxWizard::OnWizEvent)
96 EVT_WIZARD_PAGE_CHANGING(wxID_ANY, wxWizard::OnWizEvent)
97 EVT_WIZARD_CANCEL(wxID_ANY, wxWizard::OnWizEvent)
98 EVT_WIZARD_FINISHED(wxID_ANY, wxWizard::OnWizEvent)
99 EVT_WIZARD_HELP(wxID_ANY, wxWizard::OnWizEvent)
66cd017c
VZ
100END_EVENT_TABLE()
101
74b31181 102IMPLEMENT_DYNAMIC_CLASS(wxWizard, wxDialog)
066f1b7a
SC
103
104/*
ca65c044
WS
105 TODO PROPERTIES :
106 wxWizard
107 extstyle
108 title
066f1b7a
SC
109*/
110
74b31181
VZ
111IMPLEMENT_ABSTRACT_CLASS(wxWizardPage, wxPanel)
112IMPLEMENT_DYNAMIC_CLASS(wxWizardPageSimple, wxWizardPage)
66cd017c
VZ
113IMPLEMENT_DYNAMIC_CLASS(wxWizardEvent, wxNotifyEvent)
114
115// ============================================================================
116// implementation
117// ============================================================================
118
74b31181
VZ
119// ----------------------------------------------------------------------------
120// wxWizardPage
121// ----------------------------------------------------------------------------
122
c7de4135
VS
123void wxWizardPage::Init()
124{
125 m_bitmap = wxNullBitmap;
126}
127
a0a48a3f
VZ
128wxWizardPage::wxWizardPage(wxWizard *parent,
129 const wxBitmap& bitmap,
130 const wxChar *resource)
74b31181 131{
c7de4135
VS
132 Create(parent, bitmap, resource);
133}
91c68292 134
c7de4135
VS
135bool wxWizardPage::Create(wxWizard *parent,
136 const wxBitmap& bitmap,
137 const wxChar *resource)
138{
ca65c044
WS
139 if ( !wxPanel::Create(parent, wxID_ANY) )
140 return false;
c7de4135 141
a0a48a3f
VZ
142 if ( resource != NULL )
143 {
5d5da6f6 144#if wxUSE_WX_RESOURCES
96f4ca42
JJ
145#if 0
146 if ( !LoadFromResource(this, resource) )
a0a48a3f
VZ
147 {
148 wxFAIL_MSG(wxT("wxWizardPage LoadFromResource failed!!!!"));
149 }
96f4ca42 150#endif
c8804076 151#endif // wxUSE_RESOURCES
a0a48a3f
VZ
152 }
153
636d266b 154 m_bitmap = bitmap;
a0a48a3f 155
74b31181
VZ
156 // initially the page is hidden, it's shown only when it becomes current
157 Hide();
91c68292 158
ca65c044 159 return true;
74b31181
VZ
160}
161
162// ----------------------------------------------------------------------------
163// wxWizardPageSimple
164// ----------------------------------------------------------------------------
165
166wxWizardPage *wxWizardPageSimple::GetPrev() const
167{
168 return m_prev;
169}
170
171wxWizardPage *wxWizardPageSimple::GetNext() const
172{
173 return m_next;
174}
07f20d9a
VZ
175
176// ----------------------------------------------------------------------------
177// wxWizardSizer
178// ----------------------------------------------------------------------------
179
07f20d9a 180wxWizardSizer::wxWizardSizer(wxWizard *owner)
88517d90 181 : m_owner(owner)
07f20d9a
VZ
182{
183 m_childSizeValid = false;
184}
185
0a9ed8f1
VZ
186wxSizerItem *wxWizardSizer::Insert(size_t index, wxSizerItem *item)
187{
188 if ( item->IsWindow() )
189 {
190 // we must pretend that the window is shown as otherwise it wouldn't be
191 // taken into account for the layout -- but avoid really showing it, so
192 // just set the internal flag instead of calling wxWindow::Show()
193 item->GetWindow()->wxWindowBase::Show();
194 }
195
196 return wxSizer::Insert(index, item);
197}
198
199void wxWizardSizer::HidePages()
200{
201 for ( wxSizerItemList::compatibility_iterator node = GetChildren().GetFirst();
202 node;
203 node = node->GetNext() )
204 {
205 wxSizerItem * const item = node->GetData();
206 if ( item->IsWindow() )
207 item->GetWindow()->wxWindowBase::Show(false);
208 }
209}
210
07f20d9a
VZ
211void wxWizardSizer::RecalcSizes()
212{
213 // Effect of this function depends on m_owner->m_page and
214 // it should be called whenever it changes (wxWizard::ShowPage)
215 if ( m_owner->m_page )
216 {
88517d90 217 m_owner->m_page->SetSize(m_position.x, m_position.y, m_size.x, m_size.y);
07f20d9a
VZ
218 }
219}
220
221wxSize wxWizardSizer::CalcMin()
222{
223 return m_owner->GetPageSize();
224}
225
226wxSize wxWizardSizer::GetMaxChildSize()
227{
228#if !defined(__WXDEBUG__)
229 if ( m_childSizeValid )
230 return m_childSize;
231#endif
232
233 wxSize maxOfMin;
a381fd1c 234 wxSizerItemList::compatibility_iterator childNode;
07f20d9a
VZ
235
236 for(childNode = m_children.GetFirst(); childNode;
237 childNode = childNode->GetNext())
238 {
239 wxSizerItem *child = childNode->GetData();
240 maxOfMin.IncTo(child->CalcMin());
241 maxOfMin.IncTo(SiblingSize(child));
242 }
243
244#ifdef __WXDEBUG__
245 if ( m_childSizeValid && m_childSize != maxOfMin )
246 {
247 wxFAIL_MSG( _T("Size changed in wxWizard::GetPageAreaSizer()")
248 _T("after RunWizard().\n")
249 _T("Did you forget to call GetSizer()->Fit(this) ")
250 _T("for some page?")) ;
251
252 return m_childSize;
253 }
254#endif // __WXDEBUG__
255
256 if ( m_owner->m_started )
257 {
258 m_childSizeValid = true;
259 m_childSize = maxOfMin;
260 }
ca65c044 261
07f20d9a
VZ
262 return maxOfMin;
263}
264
88517d90 265int wxWizardSizer::GetBorder() const
07f20d9a
VZ
266{
267 if ( m_owner->m_calledSetBorder )
268 return m_owner->m_border;
269
270 return m_children.IsEmpty() ? 5 : 0;
271}
272
273wxSize wxWizardSizer::SiblingSize(wxSizerItem *child)
274{
275 wxSize maxSibling;
ca65c044 276
07f20d9a
VZ
277 if ( child->IsWindow() )
278 {
279 wxWizardPage *page = wxDynamicCast(child->GetWindow(), wxWizardPage);
280 if ( page )
281 {
282 for ( wxWizardPage *sibling = page->GetNext();
283 sibling;
284 sibling = sibling->GetNext() )
285 {
286 if ( sibling->GetSizer() )
287 {
288 maxSibling.IncTo(sibling->GetSizer()->CalcMin());
289 }
290 }
291 }
292 }
ca65c044 293
07f20d9a
VZ
294 return maxSibling;
295}
296
66cd017c
VZ
297// ----------------------------------------------------------------------------
298// generic wxWizard implementation
299// ----------------------------------------------------------------------------
300
77436c4c
JS
301void wxWizard::Init()
302{
303 m_posWizard = wxDefaultPosition;
304 m_page = (wxWizardPage *)NULL;
305 m_btnPrev = m_btnNext = NULL;
306 m_statbmp = NULL;
aedd6d6a 307 m_sizerBmpAndPage = NULL;
07f20d9a
VZ
308 m_sizerPage = NULL;
309 m_calledSetBorder = false;
310 m_border = 0;
311 m_started = false;
94c09a19 312 m_wasModal = false;
77436c4c
JS
313}
314
315bool wxWizard::Create(wxWindow *parent,
aedd6d6a
VZ
316 int id,
317 const wxString& title,
318 const wxBitmap& bitmap,
319 const wxPoint& pos,
320 long style)
66cd017c 321{
07f20d9a 322 bool result = wxDialog::Create(parent,id,title,pos,wxDefaultSize,style);
ca65c044 323
77436c4c
JS
324 m_posWizard = pos;
325 m_bitmap = bitmap ;
636d266b 326
07f20d9a 327 DoCreateControls();
ca65c044 328
07f20d9a 329 return result;
f6bcfd97
BP
330}
331
07f20d9a 332void wxWizard::AddBitmapRow(wxBoxSizer *mainColumn)
f6bcfd97 333{
07f20d9a
VZ
334 m_sizerBmpAndPage = new wxBoxSizer(wxHORIZONTAL);
335 mainColumn->Add(
336 m_sizerBmpAndPage,
337 1, // Vertically stretchable
338 wxEXPAND // Horizonal stretching, no border
339 );
340 mainColumn->Add(0,5,
341 0, // No vertical stretching
342 wxEXPAND // No border, (mostly useless) horizontal stretching
343 );
66cd017c 344
461dae94 345#if wxUSE_STATBMP
f6bcfd97 346 if ( m_bitmap.Ok() )
66cd017c 347 {
ca65c044 348 m_statbmp = new wxStaticBitmap(this, wxID_ANY, m_bitmap);
07f20d9a
VZ
349 m_sizerBmpAndPage->Add(
350 m_statbmp,
351 0, // No horizontal stretching
352 wxALL, // Border all around, top alignment
353 5 // Border width
354 );
355 m_sizerBmpAndPage->Add(
356 5,0,
357 0, // No horizontal stretching
358 wxEXPAND // No border, (mostly useless) vertical stretching
359 );
66cd017c 360 }
461dae94 361#endif
f1df0927 362
07f20d9a
VZ
363 // Added to m_sizerBmpAndPage in FinishLayout
364 m_sizerPage = new wxWizardSizer(this);
365}
74b31181 366
07f20d9a
VZ
367void wxWizard::AddStaticLine(wxBoxSizer *mainColumn)
368{
74b31181 369#if wxUSE_STATLINE
07f20d9a 370 mainColumn->Add(
ca65c044 371 new wxStaticLine(this, wxID_ANY),
07f20d9a
VZ
372 0, // Vertically unstretchable
373 wxEXPAND | wxALL, // Border all around, horizontally stretchable
374 5 // Border width
375 );
376 mainColumn->Add(0,5,
377 0, // No vertical stretching
378 wxEXPAND // No border, (mostly useless) horizontal stretching
379 );
380#else
381 (void)mainColumn;
f6bcfd97 382#endif // wxUSE_STATLINE
07f20d9a 383}
74b31181 384
07f20d9a
VZ
385void wxWizard::AddBackNextPair(wxBoxSizer *buttonRow)
386{
54cf600d
VZ
387 wxASSERT_MSG( m_btnNext && m_btnPrev,
388 _T("You must create the buttons before calling ")
389 _T("wxWizard::AddBackNextPair") );
390
07f20d9a
VZ
391 // margin between Back and Next buttons
392#ifdef __WXMAC__
393 static const int BACKNEXT_MARGIN = 10;
394#else
395 static const int BACKNEXT_MARGIN = 0;
396#endif
66cd017c 397
07f20d9a
VZ
398 wxBoxSizer *backNextPair = new wxBoxSizer(wxHORIZONTAL);
399 buttonRow->Add(
400 backNextPair,
401 0, // No horizontal stretching
402 wxALL, // Border all around
403 5 // Border width
404 );
ca65c044 405
07f20d9a
VZ
406 backNextPair->Add(m_btnPrev);
407 backNextPair->Add(BACKNEXT_MARGIN,0,
408 0, // No horizontal stretching
409 wxEXPAND // No border, (mostly useless) vertical stretching
410 );
07f20d9a
VZ
411 backNextPair->Add(m_btnNext);
412}
66cd017c 413
07f20d9a
VZ
414void wxWizard::AddButtonRow(wxBoxSizer *mainColumn)
415{
9473e9a4
VZ
416 // the order in which the buttons are created determines the TAB order - at least under MSWindows...
417 // although the 'back' button appears before the 'next' button, a more userfriendly tab order is
418 // to activate the 'next' button first (create the next button before the back button).
419 // The reason is: The user will repeatedly enter information in the wizard pages and then wants to
420 // press 'next'. If a user uses mostly the keyboard, he would have to skip the 'back' button
421 // everytime. This is annoying. There is a second reason: RETURN acts as TAB. If the 'next'
422 // button comes first in the TAB order, the user can enter information very fast using the RETURN
423 // key to TAB to the next entry field and page. This would not be possible, if the 'back' button
424 // was created before the 'next' button.
425
a6328131 426 bool isPda = (wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA);
94c09a19 427 int buttonStyle = isPda ? wxBU_EXACTFIT : 0;
a6328131 428
07f20d9a 429 wxBoxSizer *buttonRow = new wxBoxSizer(wxHORIZONTAL);
977a3818
JS
430#ifdef __WXMAC__
431 if (GetExtraStyle() & wxWIZARD_EX_HELPBUTTON)
432 mainColumn->Add(
433 buttonRow,
434 0, // Vertically unstretchable
435 wxGROW|wxALIGN_CENTRE
436 );
437 else
438#endif
07f20d9a
VZ
439 mainColumn->Add(
440 buttonRow,
441 0, // Vertically unstretchable
442 wxALIGN_RIGHT // Right aligned, no border
443 );
66cd017c 444
9473e9a4
VZ
445 // Desired TAB order is 'next', 'cancel', 'help', 'back'. This makes the 'back' button the last control on the page.
446 // Create the buttons in the right order...
977a3818
JS
447 wxButton *btnHelp=0;
448#ifdef __WXMAC__
449 if (GetExtraStyle() & wxWIZARD_EX_HELPBUTTON)
a6328131 450 btnHelp=new wxButton(this, wxID_HELP, _("&Help"), wxDefaultPosition, wxDefaultSize, buttonStyle);
977a3818
JS
451#endif
452
9473e9a4 453 m_btnNext = new wxButton(this, wxID_FORWARD, _("&Next >"));
a6328131 454 wxButton *btnCancel=new wxButton(this, wxID_CANCEL, _("&Cancel"), wxDefaultPosition, wxDefaultSize, buttonStyle);
977a3818 455#ifndef __WXMAC__
07f20d9a 456 if (GetExtraStyle() & wxWIZARD_EX_HELPBUTTON)
a6328131 457 btnHelp=new wxButton(this, wxID_HELP, _("&Help"), wxDefaultPosition, wxDefaultSize, buttonStyle);
977a3818 458#endif
a6328131 459 m_btnPrev = new wxButton(this, wxID_BACKWARD, _("< &Back"), wxDefaultPosition, wxDefaultSize, buttonStyle);
9473e9a4
VZ
460
461 if (btnHelp)
977a3818 462 {
07f20d9a 463 buttonRow->Add(
9473e9a4 464 btnHelp,
07f20d9a
VZ
465 0, // Horizontally unstretchable
466 wxALL, // Border all around, top aligned
467 5 // Border width
977a3818
JS
468 );
469#ifdef __WXMAC__
470 // Put stretchable space between help button and others
471 buttonRow->Add(0, 0, 1, wxALIGN_CENTRE, 0);
472#endif
473 }
07f20d9a
VZ
474
475 AddBackNextPair(buttonRow);
ca65c044 476
07f20d9a 477 buttonRow->Add(
9473e9a4 478 btnCancel,
07f20d9a
VZ
479 0, // Horizontally unstretchable
480 wxALL, // Border all around, top aligned
481 5 // Border width
482 );
483}
66cd017c 484
07f20d9a
VZ
485void wxWizard::DoCreateControls()
486{
487 // do nothing if the controls were already created
488 if ( WasCreated() )
489 return;
ca65c044 490
a6328131 491 bool isPda = (wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA);
94c09a19 492
a6328131
JS
493 // Horizontal stretching, and if not PDA, border all around
494 int mainColumnSizerFlags = isPda ? wxEXPAND : wxALL|wxEXPAND ;
94c09a19 495
07f20d9a
VZ
496 // wxWindow::SetSizer will be called at end
497 wxBoxSizer *windowSizer = new wxBoxSizer(wxVERTICAL);
ca65c044 498
07f20d9a
VZ
499 wxBoxSizer *mainColumn = new wxBoxSizer(wxVERTICAL);
500 windowSizer->Add(
501 mainColumn,
502 1, // Vertical stretching
a6328131 503 mainColumnSizerFlags,
07f20d9a
VZ
504 5 // Border width
505 );
ca65c044 506
07f20d9a 507 AddBitmapRow(mainColumn);
94c09a19 508
a6328131
JS
509 if (!isPda)
510 AddStaticLine(mainColumn);
94c09a19 511
07f20d9a 512 AddButtonRow(mainColumn);
ca65c044 513
07f20d9a
VZ
514 // wxWindow::SetSizer should be followed by wxWindow::Fit, but
515 // this is done in FinishLayout anyway so why duplicate it
516 SetSizer(windowSizer);
66cd017c
VZ
517}
518
f6bcfd97
BP
519void wxWizard::SetPageSize(const wxSize& size)
520{
07f20d9a 521 wxCHECK_RET(!m_started,wxT("wxWizard::SetPageSize after RunWizard"));
f6bcfd97
BP
522 m_sizePage = size;
523}
524
07f20d9a 525void wxWizard::FinishLayout()
c73b439f 526{
a6328131 527 bool isPda = (wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA);
94c09a19 528
b3eb133b
WS
529 // Set to enable wxWizardSizer::GetMaxChildSize
530 m_started = true;
531
07f20d9a
VZ
532 m_sizerBmpAndPage->Add(
533 m_sizerPage,
534 1, // Horizontal stretching
535 wxEXPAND | wxALL, // Vertically stretchable
88517d90 536 m_sizerPage->GetBorder()
07f20d9a 537 );
ca65c044 538
a6328131
JS
539 if (!isPda)
540 {
541 GetSizer()->SetSizeHints(this);
542 if ( m_posWizard == wxDefaultPosition )
543 CentreOnScreen();
544 }
0a9ed8f1
VZ
545
546 // now that our layout is computed correctly, hide the pages artificially
547 // shown in wxWizardSizer::Insert() back again
548 m_sizerPage->HidePages();
07f20d9a 549}
c73b439f 550
07f20d9a
VZ
551void wxWizard::FitToPage(const wxWizardPage *page)
552{
553 wxCHECK_RET(!m_started,wxT("wxWizard::FitToPage after RunWizard"));
ca65c044 554
c73b439f
VZ
555 while ( page )
556 {
557 wxSize size = page->GetBestSize();
558
07f20d9a 559 m_sizePage.IncTo(size);
c73b439f
VZ
560
561 page = page->GetNext();
562 }
c73b439f
VZ
563}
564
74b31181 565bool wxWizard::ShowPage(wxWizardPage *page, bool goingForward)
66cd017c 566{
223d09f6 567 wxASSERT_MSG( page != m_page, wxT("this is useless") );
66cd017c 568
74b31181
VZ
569 // we'll use this to decide whether we have to change the label of this
570 // button or not (initially the label is "Next")
ca65c044 571 bool btnLabelWasNext = true;
66cd017c 572
7cc5041d
VZ
573 // Modified 10-20-2001 Robert Cavanaugh.
574 // Fixed bug for displaying a new bitmap
575 // in each *consecutive* page
f1df0927 576
7cc5041d 577 // flag to indicate if this page uses a new bitmap
ca65c044 578 bool bmpIsDefault = true;
7cc5041d
VZ
579
580 // use these labels to determine if we need to change the bitmap
581 // for this page
a3e8c656 582 wxBitmap bmpPrev, bmpCur;
7cc5041d
VZ
583
584 // check for previous page
74b31181 585 if ( m_page )
66cd017c 586 {
74b31181 587 // send the event to the old page
2365e5cb 588 wxWizardEvent event(wxEVT_WIZARD_PAGE_CHANGING, GetId(), goingForward, m_page);
74b31181
VZ
589 if ( m_page->GetEventHandler()->ProcessEvent(event) &&
590 !event.IsAllowed() )
591 {
592 // vetoed by the page
ca65c044 593 return false;
74b31181 594 }
66cd017c 595
74b31181
VZ
596 m_page->Hide();
597
2b5f62a0 598 btnLabelWasNext = HasNextPage(m_page);
7cc5041d
VZ
599
600 // Get the bitmap of the previous page (if it exists)
a3e8c656
VZ
601 if ( m_page->GetBitmap().Ok() )
602 {
603 bmpPrev = m_page->GetBitmap();
7cc5041d 604 }
dee5b92f 605 }
66cd017c 606
7cc5041d 607 // set the new page
66cd017c 608 m_page = page;
66cd017c 609
74b31181
VZ
610 // is this the end?
611 if ( !m_page )
66cd017c 612 {
74b31181 613 // terminate successfully
b3eb133b
WS
614 if(IsModal())
615 {
616 EndModal(wxID_OK);
617 }
618 else
619 {
620 SetReturnCode(wxID_OK);
621 Hide();
622 }
64c2fa13
VZ
623
624 // and notify the user code (this is especially useful for modeless
625 // wizards)
ca65c044 626 wxWizardEvent event(wxEVT_WIZARD_FINISHED, GetId(), false, 0);
64c2fa13
VZ
627 (void)GetEventHandler()->ProcessEvent(event);
628
ca65c044 629 return true;
74b31181 630 }
66cd017c 631
74b31181
VZ
632 // position and show the new page
633 (void)m_page->TransferDataToWindow();
ca65c044 634
07f20d9a
VZ
635 // wxWizardSizer::RecalcSizes wants to be called when m_page changes
636 m_sizerPage->RecalcSizes();
66cd017c 637
7cc5041d
VZ
638 // check if bitmap needs to be updated
639 // update default flag as well
a3e8c656 640 if ( m_page->GetBitmap().Ok() )
7cc5041d 641 {
a3e8c656 642 bmpCur = m_page->GetBitmap();
ca65c044 643 bmpIsDefault = false;
7cc5041d
VZ
644 }
645
461dae94 646#if wxUSE_STATBMP
7cc5041d
VZ
647 // change the bitmap if:
648 // 1) a default bitmap was selected in constructor
649 // 2) this page was constructed with a bitmap
650 // 3) this bitmap is not the previous bitmap
a3e8c656 651 if ( m_statbmp && (bmpCur != bmpPrev) )
f1df0927 652 {
cfd88569
VZ
653 wxBitmap bmp;
654 if ( bmpIsDefault )
655 bmp = m_bitmap;
656 else
657 bmp = m_page->GetBitmap();
658 m_statbmp->SetBitmap(bmp);
f1df0927 659 }
461dae94 660#endif
f1df0927 661
74b31181 662 // and update the buttons state
2b5f62a0 663 m_btnPrev->Enable(HasPrevPage(m_page));
66cd017c 664
2b5f62a0 665 bool hasNext = HasNextPage(m_page);
8f177c8e 666 if ( btnLabelWasNext != hasNext )
66cd017c 667 {
74b31181 668 // need to update
e9fa7581
OK
669 if (btnLabelWasNext)
670 m_btnNext->SetLabel(_("&Finish"));
671 else
672 m_btnNext->SetLabel(_("&Next >"));
66cd017c 673 }
7b3daa84 674 m_btnNext->SetDefault();
74b31181 675 // nothing to do: the label was already correct
66cd017c 676
5bc28e84 677 // send the change event to the new page now
2365e5cb 678 wxWizardEvent event(wxEVT_WIZARD_PAGE_CHANGED, GetId(), goingForward, m_page);
5bc28e84
VZ
679 (void)m_page->GetEventHandler()->ProcessEvent(event);
680
681 // and finally show it
682 m_page->Show();
683 m_page->SetFocus();
684
ca65c044 685 return true;
66cd017c
VZ
686}
687
74b31181 688bool wxWizard::RunWizard(wxWizardPage *firstPage)
66cd017c 689{
ca65c044
WS
690 wxCHECK_MSG( firstPage, false, wxT("can't run empty wizard") );
691
07f20d9a
VZ
692 // This cannot be done sooner, because user can change layout options
693 // up to this moment
694 FinishLayout();
ca65c044
WS
695
696 // can't return false here because there is no old page
697 (void)ShowPage(firstPage, true /* forward */);
66cd017c 698
94c09a19 699 m_wasModal = true;
37f6a080 700
66cd017c
VZ
701 return ShowModal() == wxID_OK;
702}
703
74b31181 704wxWizardPage *wxWizard::GetCurrentPage() const
66cd017c 705{
74b31181 706 return m_page;
66cd017c
VZ
707}
708
4fe5383d
VZ
709wxSize wxWizard::GetPageSize() const
710{
07f20d9a
VZ
711 wxSize pageSize(GetManualPageSize());
712 pageSize.IncTo(m_sizerPage->GetMaxChildSize());
713 return pageSize;
714}
715
716wxSizer *wxWizard::GetPageAreaSizer() const
717{
718 return m_sizerPage;
719}
720
721void wxWizard::SetBorder(int border)
722{
723 wxCHECK_RET(!m_started,wxT("wxWizard::SetBorder after RunWizard"));
aedd6d6a 724
07f20d9a
VZ
725 m_calledSetBorder = true;
726 m_border = border;
727}
728
729wxSize wxWizard::GetManualPageSize() const
730{
731 // default width and height of the page
a6328131
JS
732 int DEFAULT_PAGE_WIDTH = 270;
733 int DEFAULT_PAGE_HEIGHT = 270;
734 bool isPda = (wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA);
735 if (isPda)
736 {
737 // Make the default page size small enough to fit on screen
738 DEFAULT_PAGE_WIDTH = wxSystemSettings::GetMetric(wxSYS_SCREEN_X) / 2;
739 DEFAULT_PAGE_HEIGHT = wxSystemSettings::GetMetric(wxSYS_SCREEN_Y) / 2;
740 }
94c09a19 741
07f20d9a 742 wxSize totalPageSize(DEFAULT_PAGE_WIDTH,DEFAULT_PAGE_HEIGHT);
ca65c044 743
07f20d9a 744 totalPageSize.IncTo(m_sizePage);
ca65c044 745
aedd6d6a
VZ
746 if ( m_statbmp )
747 {
748 totalPageSize.IncTo(wxSize(0, m_bitmap.GetHeight()));
749 }
ca65c044 750
07f20d9a 751 return totalPageSize;
4fe5383d
VZ
752}
753
20f18ea1 754void wxWizard::OnCancel(wxCommandEvent& WXUNUSED(eventUnused))
66cd017c 755{
74b31181
VZ
756 // this function probably can never be called when we don't have an active
757 // page, but a small extra check won't hurt
758 wxWindow *win = m_page ? (wxWindow *)m_page : (wxWindow *)this;
759
ca65c044 760 wxWizardEvent event(wxEVT_WIZARD_CANCEL, GetId(), false, m_page);
74b31181 761 if ( !win->GetEventHandler()->ProcessEvent(event) || event.IsAllowed() )
66cd017c
VZ
762 {
763 // no objections - close the dialog
b3eb133b
WS
764 if(IsModal())
765 {
766 EndModal(wxID_CANCEL);
767 }
768 else
769 {
770 SetReturnCode(wxID_CANCEL);
771 Hide();
772 }
66cd017c
VZ
773 }
774 //else: request to Cancel ignored
775}
776
74b31181 777void wxWizard::OnBackOrNext(wxCommandEvent& event)
66cd017c
VZ
778{
779 wxASSERT_MSG( (event.GetEventObject() == m_btnNext) ||
780 (event.GetEventObject() == m_btnPrev),
223d09f6 781 wxT("unknown button") );
66cd017c 782
47e64664
VZ
783 wxCHECK_RET( m_page, _T("should have a valid current page") );
784
f6bcfd97
BP
785 // ask the current page first: notice that we do it before calling
786 // GetNext/Prev() because the data transfered from the controls of the page
787 // may change the value returned by these methods
47e64664 788 if ( !m_page->Validate() || !m_page->TransferDataFromWindow() )
f6bcfd97
BP
789 {
790 // the page data is incorrect, don't do anything
791 return;
792 }
793
74b31181 794 bool forward = event.GetEventObject() == m_btnNext;
66cd017c 795
74b31181
VZ
796 wxWizardPage *page;
797 if ( forward )
66cd017c 798 {
74b31181 799 page = m_page->GetNext();
66cd017c 800 }
74b31181 801 else // back
66cd017c 802 {
74b31181
VZ
803 page = m_page->GetPrev();
804
223d09f6 805 wxASSERT_MSG( page, wxT("\"<Back\" button should have been disabled") );
66cd017c 806 }
74b31181
VZ
807
808 // just pass to the new page (or may be not - but we don't care here)
809 (void)ShowPage(page, forward);
66cd017c
VZ
810}
811
f80bf901
VZ
812void wxWizard::OnHelp(wxCommandEvent& WXUNUSED(event))
813{
814 // this function probably can never be called when we don't have an active
815 // page, but a small extra check won't hurt
816 if(m_page != NULL)
817 {
818 // Create and send the help event to the specific page handler
819 // event data contains the active page so that context-sensitive
820 // help is possible
ca65c044 821 wxWizardEvent eventHelp(wxEVT_WIZARD_HELP, GetId(), true, m_page);
f80bf901
VZ
822 (void)m_page->GetEventHandler()->ProcessEvent(eventHelp);
823 }
824}
825
91c68292
VZ
826void wxWizard::OnWizEvent(wxWizardEvent& event)
827{
828 // the dialogs have wxWS_EX_BLOCK_EVENTS style on by default but we want to
829 // propagate wxEVT_WIZARD_XXX to the parent (if any), so do it manually
830 if ( !(GetExtraStyle() & wxWS_EX_BLOCK_EVENTS) )
831 {
832 // the event will be propagated anyhow
0a640208 833 event.Skip();
91c68292 834 }
b3eb133b
WS
835 else
836 {
837 wxWindow *parent = GetParent();
91c68292 838
b3eb133b
WS
839 if ( !parent || !parent->GetEventHandler()->ProcessEvent(event) )
840 {
841 event.Skip();
842 }
843 }
37f6a080 844
94c09a19 845 if ( ( !m_wasModal ) &&
37f6a080
JS
846 event.IsAllowed() &&
847 ( event.GetEventType() == wxEVT_WIZARD_FINISHED ||
848 event.GetEventType() == wxEVT_WIZARD_CANCEL
849 )
850 )
851 {
37f6a080
JS
852 Destroy();
853 }
91c68292 854}
f80bf901 855
66cd017c
VZ
856// ----------------------------------------------------------------------------
857// wxWizardEvent
858// ----------------------------------------------------------------------------
859
f80bf901 860wxWizardEvent::wxWizardEvent(wxEventType type, int id, bool direction, wxWizardPage* page)
66cd017c
VZ
861 : wxNotifyEvent(type, id)
862{
f80bf901
VZ
863 // Modified 10-20-2001 Robert Cavanaugh
864 // add the active page to the event data
74b31181 865 m_direction = direction;
f80bf901 866 m_page = page;
66cd017c 867}
74b31181 868
1e6feb95 869#endif // wxUSE_WIZARDDLG