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