]> git.saurik.com Git - wxWidgets.git/blame - src/generic/wizard.cpp
Fixed wxRichTextCtrl caret test case
[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
d9af3670 44#include "wx/scrolwin.h"
66cd017c 45#include "wx/wizard.h"
d9af3670 46#include "wx/dcmemory.h"
66cd017c 47
aedd6d6a
VZ
48// ----------------------------------------------------------------------------
49// wxWizardSizer
50// ----------------------------------------------------------------------------
51
52class wxWizardSizer : public wxSizer
53{
54public:
55 wxWizardSizer(wxWizard *owner);
56
0a9ed8f1
VZ
57 virtual wxSizerItem *Insert(size_t index, wxSizerItem *item);
58
88517d90
VZ
59 virtual void RecalcSizes();
60 virtual wxSize CalcMin();
aedd6d6a 61
88517d90 62 // get the max size of all wizard pages
aedd6d6a 63 wxSize GetMaxChildSize();
88517d90
VZ
64
65 // return the border which can be either set using wxWizard::SetBorder() or
66 // have default value
67 int GetBorder() const;
ca65c044 68
0a9ed8f1
VZ
69 // hide the pages which we temporarily "show" when they're added to this
70 // sizer (see Insert())
71 void HidePages();
72
aedd6d6a
VZ
73private:
74 wxSize SiblingSize(wxSizerItem *child);
ca65c044 75
aedd6d6a 76 wxWizard *m_owner;
aedd6d6a
VZ
77 wxSize m_childSize;
78};
79
66cd017c
VZ
80// ----------------------------------------------------------------------------
81// event tables and such
82// ----------------------------------------------------------------------------
83
9b11752c
VZ
84wxDEFINE_EVENT( wxEVT_WIZARD_PAGE_CHANGED, wxWizardEvent );
85wxDEFINE_EVENT( wxEVT_WIZARD_PAGE_CHANGING, wxWizardEvent );
c9f18835 86wxDEFINE_EVENT( wxEVT_WIZARD_BEFORE_PAGE_CHANGED, wxWizardEvent );
9b11752c
VZ
87wxDEFINE_EVENT( wxEVT_WIZARD_CANCEL, wxWizardEvent );
88wxDEFINE_EVENT( wxEVT_WIZARD_FINISHED, wxWizardEvent );
89wxDEFINE_EVENT( wxEVT_WIZARD_HELP, wxWizardEvent );
deeb0a89 90wxDEFINE_EVENT( wxEVT_WIZARD_PAGE_SHOWN, wxWizardEvent );
2e4df4bf 91
74b31181
VZ
92BEGIN_EVENT_TABLE(wxWizard, wxDialog)
93 EVT_BUTTON(wxID_CANCEL, wxWizard::OnCancel)
f80bf901
VZ
94 EVT_BUTTON(wxID_BACKWARD, wxWizard::OnBackOrNext)
95 EVT_BUTTON(wxID_FORWARD, wxWizard::OnBackOrNext)
96 EVT_BUTTON(wxID_HELP, wxWizard::OnHelp)
91c68292 97
ca65c044
WS
98 EVT_WIZARD_PAGE_CHANGED(wxID_ANY, wxWizard::OnWizEvent)
99 EVT_WIZARD_PAGE_CHANGING(wxID_ANY, wxWizard::OnWizEvent)
100 EVT_WIZARD_CANCEL(wxID_ANY, wxWizard::OnWizEvent)
101 EVT_WIZARD_FINISHED(wxID_ANY, wxWizard::OnWizEvent)
102 EVT_WIZARD_HELP(wxID_ANY, wxWizard::OnWizEvent)
66cd017c
VZ
103END_EVENT_TABLE()
104
74b31181 105IMPLEMENT_DYNAMIC_CLASS(wxWizard, wxDialog)
066f1b7a
SC
106
107/*
ca65c044
WS
108 TODO PROPERTIES :
109 wxWizard
110 extstyle
111 title
066f1b7a
SC
112*/
113
74b31181
VZ
114IMPLEMENT_ABSTRACT_CLASS(wxWizardPage, wxPanel)
115IMPLEMENT_DYNAMIC_CLASS(wxWizardPageSimple, wxWizardPage)
66cd017c
VZ
116IMPLEMENT_DYNAMIC_CLASS(wxWizardEvent, wxNotifyEvent)
117
118// ============================================================================
119// implementation
120// ============================================================================
121
74b31181
VZ
122// ----------------------------------------------------------------------------
123// wxWizardPage
124// ----------------------------------------------------------------------------
125
c7de4135
VS
126void wxWizardPage::Init()
127{
128 m_bitmap = wxNullBitmap;
129}
130
a0a48a3f 131wxWizardPage::wxWizardPage(wxWizard *parent,
cf63f3d3 132 const wxBitmap& bitmap)
74b31181 133{
cf63f3d3 134 Create(parent, bitmap);
c7de4135 135}
91c68292 136
c7de4135 137bool wxWizardPage::Create(wxWizard *parent,
cf63f3d3 138 const wxBitmap& bitmap)
c7de4135 139{
ca65c044
WS
140 if ( !wxPanel::Create(parent, wxID_ANY) )
141 return false;
c7de4135 142
636d266b 143 m_bitmap = bitmap;
a0a48a3f 144
74b31181
VZ
145 // initially the page is hidden, it's shown only when it becomes current
146 Hide();
91c68292 147
ca65c044 148 return true;
74b31181
VZ
149}
150
151// ----------------------------------------------------------------------------
152// wxWizardPageSimple
153// ----------------------------------------------------------------------------
154
155wxWizardPage *wxWizardPageSimple::GetPrev() const
156{
157 return m_prev;
158}
159
160wxWizardPage *wxWizardPageSimple::GetNext() const
161{
162 return m_next;
163}
07f20d9a
VZ
164
165// ----------------------------------------------------------------------------
166// wxWizardSizer
167// ----------------------------------------------------------------------------
168
07f20d9a 169wxWizardSizer::wxWizardSizer(wxWizard *owner)
0a089246
VZ
170 : m_owner(owner),
171 m_childSize(wxDefaultSize)
07f20d9a 172{
07f20d9a
VZ
173}
174
0a9ed8f1
VZ
175wxSizerItem *wxWizardSizer::Insert(size_t index, wxSizerItem *item)
176{
0a089246
VZ
177 m_owner->m_usingSizer = true;
178
0a9ed8f1
VZ
179 if ( item->IsWindow() )
180 {
181 // we must pretend that the window is shown as otherwise it wouldn't be
182 // taken into account for the layout -- but avoid really showing it, so
183 // just set the internal flag instead of calling wxWindow::Show()
184 item->GetWindow()->wxWindowBase::Show();
185 }
186
187 return wxSizer::Insert(index, item);
188}
189
190void wxWizardSizer::HidePages()
191{
192 for ( wxSizerItemList::compatibility_iterator node = GetChildren().GetFirst();
193 node;
194 node = node->GetNext() )
195 {
196 wxSizerItem * const item = node->GetData();
197 if ( item->IsWindow() )
198 item->GetWindow()->wxWindowBase::Show(false);
199 }
200}
201
07f20d9a
VZ
202void wxWizardSizer::RecalcSizes()
203{
204 // Effect of this function depends on m_owner->m_page and
205 // it should be called whenever it changes (wxWizard::ShowPage)
206 if ( m_owner->m_page )
207 {
0a089246 208 m_owner->m_page->SetSize(wxRect(m_position, m_size));
07f20d9a
VZ
209 }
210}
211
212wxSize wxWizardSizer::CalcMin()
213{
214 return m_owner->GetPageSize();
215}
216
217wxSize wxWizardSizer::GetMaxChildSize()
218{
07f20d9a 219 wxSize maxOfMin;
07f20d9a 220
0a089246
VZ
221 for ( wxSizerItemList::compatibility_iterator childNode = m_children.GetFirst();
222 childNode;
223 childNode = childNode->GetNext() )
07f20d9a
VZ
224 {
225 wxSizerItem *child = childNode->GetData();
226 maxOfMin.IncTo(child->CalcMin());
227 maxOfMin.IncTo(SiblingSize(child));
228 }
229
07f20d9a
VZ
230 if ( m_owner->m_started )
231 {
07f20d9a
VZ
232 m_childSize = maxOfMin;
233 }
ca65c044 234
07f20d9a
VZ
235 return maxOfMin;
236}
237
88517d90 238int wxWizardSizer::GetBorder() const
07f20d9a 239{
0a089246 240 return m_owner->m_border;
07f20d9a
VZ
241}
242
243wxSize wxWizardSizer::SiblingSize(wxSizerItem *child)
244{
245 wxSize maxSibling;
ca65c044 246
07f20d9a
VZ
247 if ( child->IsWindow() )
248 {
249 wxWizardPage *page = wxDynamicCast(child->GetWindow(), wxWizardPage);
250 if ( page )
251 {
252 for ( wxWizardPage *sibling = page->GetNext();
253 sibling;
254 sibling = sibling->GetNext() )
255 {
256 if ( sibling->GetSizer() )
257 {
258 maxSibling.IncTo(sibling->GetSizer()->CalcMin());
259 }
260 }
261 }
262 }
ca65c044 263
07f20d9a
VZ
264 return maxSibling;
265}
266
66cd017c
VZ
267// ----------------------------------------------------------------------------
268// generic wxWizard implementation
269// ----------------------------------------------------------------------------
270
77436c4c
JS
271void wxWizard::Init()
272{
273 m_posWizard = wxDefaultPosition;
d3b9f782 274 m_page = NULL;
77436c4c
JS
275 m_btnPrev = m_btnNext = NULL;
276 m_statbmp = NULL;
aedd6d6a 277 m_sizerBmpAndPage = NULL;
07f20d9a 278 m_sizerPage = NULL;
0a089246 279 m_border = 5;
07f20d9a 280 m_started = false;
94c09a19 281 m_wasModal = false;
0a089246 282 m_usingSizer = false;
3aa8e4ea
JS
283 m_bitmapBackgroundColour = *wxWHITE;
284 m_bitmapPlacement = 0;
285 m_bitmapMinimumWidth = 115;
77436c4c
JS
286}
287
288bool wxWizard::Create(wxWindow *parent,
aedd6d6a
VZ
289 int id,
290 const wxString& title,
291 const wxBitmap& bitmap,
292 const wxPoint& pos,
293 long style)
66cd017c 294{
07f20d9a 295 bool result = wxDialog::Create(parent,id,title,pos,wxDefaultSize,style);
ca65c044 296
77436c4c
JS
297 m_posWizard = pos;
298 m_bitmap = bitmap ;
636d266b 299
07f20d9a 300 DoCreateControls();
ca65c044 301
07f20d9a 302 return result;
f6bcfd97
BP
303}
304
781130bf
VZ
305wxWizard::~wxWizard()
306{
307 // normally we don't have to delete this sizer as it's deleted by the
308 // associated window but if we never used it or didn't set it as the window
309 // sizer yet, do delete it manually
310 if ( !m_usingSizer || !m_started )
311 delete m_sizerPage;
312}
313
07f20d9a 314void wxWizard::AddBitmapRow(wxBoxSizer *mainColumn)
f6bcfd97 315{
07f20d9a
VZ
316 m_sizerBmpAndPage = new wxBoxSizer(wxHORIZONTAL);
317 mainColumn->Add(
318 m_sizerBmpAndPage,
319 1, // Vertically stretchable
4c51a665 320 wxEXPAND // Horizontal stretching, no border
07f20d9a
VZ
321 );
322 mainColumn->Add(0,5,
323 0, // No vertical stretching
324 wxEXPAND // No border, (mostly useless) horizontal stretching
325 );
66cd017c 326
461dae94 327#if wxUSE_STATBMP
a1b806b9 328 if ( m_bitmap.IsOk() )
66cd017c 329 {
3aa8e4ea
JS
330 wxSize bitmapSize(wxDefaultSize);
331 if (GetBitmapPlacement())
332 bitmapSize.x = GetMinimumBitmapWidth();
333
334 m_statbmp = new wxStaticBitmap(this, wxID_ANY, m_bitmap, wxDefaultPosition, bitmapSize);
07f20d9a
VZ
335 m_sizerBmpAndPage->Add(
336 m_statbmp,
337 0, // No horizontal stretching
338 wxALL, // Border all around, top alignment
339 5 // Border width
340 );
341 m_sizerBmpAndPage->Add(
342 5,0,
343 0, // No horizontal stretching
344 wxEXPAND // No border, (mostly useless) vertical stretching
345 );
66cd017c 346 }
461dae94 347#endif
f1df0927 348
0a089246 349 // Added to m_sizerBmpAndPage later
07f20d9a
VZ
350 m_sizerPage = new wxWizardSizer(this);
351}
74b31181 352
07f20d9a
VZ
353void wxWizard::AddStaticLine(wxBoxSizer *mainColumn)
354{
74b31181 355#if wxUSE_STATLINE
07f20d9a 356 mainColumn->Add(
ca65c044 357 new wxStaticLine(this, wxID_ANY),
07f20d9a
VZ
358 0, // Vertically unstretchable
359 wxEXPAND | wxALL, // Border all around, horizontally stretchable
360 5 // Border width
361 );
362 mainColumn->Add(0,5,
363 0, // No vertical stretching
364 wxEXPAND // No border, (mostly useless) horizontal stretching
365 );
366#else
367 (void)mainColumn;
f6bcfd97 368#endif // wxUSE_STATLINE
07f20d9a 369}
74b31181 370
07f20d9a
VZ
371void wxWizard::AddBackNextPair(wxBoxSizer *buttonRow)
372{
54cf600d 373 wxASSERT_MSG( m_btnNext && m_btnPrev,
9a83f860
VZ
374 wxT("You must create the buttons before calling ")
375 wxT("wxWizard::AddBackNextPair") );
54cf600d 376
07f20d9a
VZ
377 wxBoxSizer *backNextPair = new wxBoxSizer(wxHORIZONTAL);
378 buttonRow->Add(
379 backNextPair,
380 0, // No horizontal stretching
381 wxALL, // Border all around
382 5 // Border width
383 );
ca65c044 384
07f20d9a 385 backNextPair->Add(m_btnPrev);
96989020 386 backNextPair->Add(10, 0,
07f20d9a
VZ
387 0, // No horizontal stretching
388 wxEXPAND // No border, (mostly useless) vertical stretching
389 );
07f20d9a
VZ
390 backNextPair->Add(m_btnNext);
391}
66cd017c 392
07f20d9a
VZ
393void wxWizard::AddButtonRow(wxBoxSizer *mainColumn)
394{
9473e9a4
VZ
395 // the order in which the buttons are created determines the TAB order - at least under MSWindows...
396 // although the 'back' button appears before the 'next' button, a more userfriendly tab order is
397 // to activate the 'next' button first (create the next button before the back button).
398 // The reason is: The user will repeatedly enter information in the wizard pages and then wants to
399 // press 'next'. If a user uses mostly the keyboard, he would have to skip the 'back' button
e75e8815 400 // every time. This is annoying. There is a second reason: RETURN acts as TAB. If the 'next'
9473e9a4
VZ
401 // button comes first in the TAB order, the user can enter information very fast using the RETURN
402 // key to TAB to the next entry field and page. This would not be possible, if the 'back' button
403 // was created before the 'next' button.
404
a6328131 405 bool isPda = (wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA);
94c09a19 406 int buttonStyle = isPda ? wxBU_EXACTFIT : 0;
a6328131 407
07f20d9a 408 wxBoxSizer *buttonRow = new wxBoxSizer(wxHORIZONTAL);
977a3818
JS
409#ifdef __WXMAC__
410 if (GetExtraStyle() & wxWIZARD_EX_HELPBUTTON)
411 mainColumn->Add(
412 buttonRow,
413 0, // Vertically unstretchable
414 wxGROW|wxALIGN_CENTRE
415 );
416 else
417#endif
07f20d9a
VZ
418 mainColumn->Add(
419 buttonRow,
420 0, // Vertically unstretchable
421 wxALIGN_RIGHT // Right aligned, no border
422 );
66cd017c 423
9473e9a4
VZ
424 // Desired TAB order is 'next', 'cancel', 'help', 'back'. This makes the 'back' button the last control on the page.
425 // Create the buttons in the right order...
977a3818
JS
426 wxButton *btnHelp=0;
427#ifdef __WXMAC__
428 if (GetExtraStyle() & wxWIZARD_EX_HELPBUTTON)
d90443b8 429 btnHelp=new wxButton(this, wxID_HELP, wxEmptyString, wxDefaultPosition, wxDefaultSize, buttonStyle);
977a3818
JS
430#endif
431
9473e9a4 432 m_btnNext = new wxButton(this, wxID_FORWARD, _("&Next >"));
a6328131 433 wxButton *btnCancel=new wxButton(this, wxID_CANCEL, _("&Cancel"), wxDefaultPosition, wxDefaultSize, buttonStyle);
977a3818 434#ifndef __WXMAC__
07f20d9a 435 if (GetExtraStyle() & wxWIZARD_EX_HELPBUTTON)
a6328131 436 btnHelp=new wxButton(this, wxID_HELP, _("&Help"), wxDefaultPosition, wxDefaultSize, buttonStyle);
977a3818 437#endif
a6328131 438 m_btnPrev = new wxButton(this, wxID_BACKWARD, _("< &Back"), wxDefaultPosition, wxDefaultSize, buttonStyle);
9473e9a4
VZ
439
440 if (btnHelp)
977a3818 441 {
07f20d9a 442 buttonRow->Add(
9473e9a4 443 btnHelp,
07f20d9a
VZ
444 0, // Horizontally unstretchable
445 wxALL, // Border all around, top aligned
446 5 // Border width
977a3818
JS
447 );
448#ifdef __WXMAC__
449 // Put stretchable space between help button and others
450 buttonRow->Add(0, 0, 1, wxALIGN_CENTRE, 0);
451#endif
452 }
07f20d9a
VZ
453
454 AddBackNextPair(buttonRow);
ca65c044 455
07f20d9a 456 buttonRow->Add(
9473e9a4 457 btnCancel,
07f20d9a
VZ
458 0, // Horizontally unstretchable
459 wxALL, // Border all around, top aligned
460 5 // Border width
461 );
462}
66cd017c 463
07f20d9a
VZ
464void wxWizard::DoCreateControls()
465{
466 // do nothing if the controls were already created
467 if ( WasCreated() )
468 return;
ca65c044 469
a6328131 470 bool isPda = (wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA);
94c09a19 471
a6328131
JS
472 // Horizontal stretching, and if not PDA, border all around
473 int mainColumnSizerFlags = isPda ? wxEXPAND : wxALL|wxEXPAND ;
94c09a19 474
07f20d9a
VZ
475 // wxWindow::SetSizer will be called at end
476 wxBoxSizer *windowSizer = new wxBoxSizer(wxVERTICAL);
ca65c044 477
07f20d9a
VZ
478 wxBoxSizer *mainColumn = new wxBoxSizer(wxVERTICAL);
479 windowSizer->Add(
480 mainColumn,
481 1, // Vertical stretching
a6328131 482 mainColumnSizerFlags,
07f20d9a
VZ
483 5 // Border width
484 );
ca65c044 485
07f20d9a 486 AddBitmapRow(mainColumn);
94c09a19 487
a6328131
JS
488 if (!isPda)
489 AddStaticLine(mainColumn);
94c09a19 490
07f20d9a 491 AddButtonRow(mainColumn);
ca65c044 492
07f20d9a 493 SetSizer(windowSizer);
66cd017c
VZ
494}
495
f6bcfd97
BP
496void wxWizard::SetPageSize(const wxSize& size)
497{
0a089246 498 wxCHECK_RET(!m_started, wxT("wxWizard::SetPageSize after RunWizard"));
f6bcfd97
BP
499 m_sizePage = size;
500}
501
07f20d9a
VZ
502void wxWizard::FitToPage(const wxWizardPage *page)
503{
0a089246 504 wxCHECK_RET(!m_started, wxT("wxWizard::FitToPage after RunWizard"));
ca65c044 505
c73b439f
VZ
506 while ( page )
507 {
508 wxSize size = page->GetBestSize();
509
07f20d9a 510 m_sizePage.IncTo(size);
c73b439f
VZ
511
512 page = page->GetNext();
513 }
c73b439f
VZ
514}
515
74b31181 516bool wxWizard::ShowPage(wxWizardPage *page, bool goingForward)
66cd017c 517{
223d09f6 518 wxASSERT_MSG( page != m_page, wxT("this is useless") );
66cd017c 519
0a089246
VZ
520 wxSizerFlags flags(1);
521 flags.Border(wxALL, m_border).Expand();
522
523 if ( !m_started )
524 {
525 if ( m_usingSizer )
526 {
527 m_sizerBmpAndPage->Add(m_sizerPage, flags);
528
529 // now that our layout is computed correctly, hide the pages
530 // artificially shown in wxWizardSizer::Insert() back again
531 m_sizerPage->HidePages();
532 }
533 }
534
535
0a089246
VZ
536 // remember the old bitmap (if any) to compare with the new one later
537 wxBitmap bmpPrev;
7cc5041d
VZ
538
539 // check for previous page
74b31181 540 if ( m_page )
66cd017c 541 {
74b31181 542 // send the event to the old page
0a089246
VZ
543 wxWizardEvent event(wxEVT_WIZARD_PAGE_CHANGING, GetId(),
544 goingForward, m_page);
74b31181
VZ
545 if ( m_page->GetEventHandler()->ProcessEvent(event) &&
546 !event.IsAllowed() )
547 {
548 // vetoed by the page
ca65c044 549 return false;
74b31181 550 }
66cd017c 551
74b31181
VZ
552 m_page->Hide();
553
0a089246
VZ
554 bmpPrev = m_page->GetBitmap();
555
556 if ( !m_usingSizer )
557 m_sizerBmpAndPage->Detach(m_page);
dee5b92f 558 }
66cd017c 559
74b31181 560 // is this the end?
71d6cd60 561 if ( !page )
66cd017c 562 {
74b31181 563 // terminate successfully
0a089246 564 if ( IsModal() )
b3eb133b
WS
565 {
566 EndModal(wxID_OK);
567 }
568 else
569 {
570 SetReturnCode(wxID_OK);
571 Hide();
572 }
64c2fa13
VZ
573
574 // and notify the user code (this is especially useful for modeless
575 // wizards)
71d6cd60 576 wxWizardEvent event(wxEVT_WIZARD_FINISHED, GetId(), false, m_page);
64c2fa13
VZ
577 (void)GetEventHandler()->ProcessEvent(event);
578
71d6cd60
VZ
579 m_page = NULL;
580
ca65c044 581 return true;
74b31181 582 }
66cd017c 583
71d6cd60
VZ
584 // notice that we change m_page only here so that wxEVT_WIZARD_FINISHED
585 // event above could still use the correct (i.e. old) value of m_page
586 m_page = page;
587
74b31181
VZ
588 // position and show the new page
589 (void)m_page->TransferDataToWindow();
ca65c044 590
0a089246 591 if ( m_usingSizer )
7cc5041d 592 {
0a089246
VZ
593 // wxWizardSizer::RecalcSizes wants to be called when m_page changes
594 m_sizerPage->RecalcSizes();
595 }
596 else // pages are not managed by the sizer
597 {
598 m_sizerBmpAndPage->Add(m_page, flags);
599 m_sizerBmpAndPage->SetItemMinSize(m_page, GetPageSize());
7cc5041d
VZ
600 }
601
461dae94 602#if wxUSE_STATBMP
0a089246 603 // update the bitmap if:it changed
3aa8e4ea 604 wxBitmap bmp;
0a089246 605 if ( m_statbmp )
f1df0927 606 {
3aa8e4ea 607 bmp = m_page->GetBitmap();
a1b806b9 608 if ( !bmp.IsOk() )
cfd88569 609 bmp = m_bitmap;
0a089246 610
a1b806b9 611 if ( !bmpPrev.IsOk() )
0a089246
VZ
612 bmpPrev = m_bitmap;
613
3aa8e4ea
JS
614 if (!GetBitmapPlacement())
615 {
616 if ( !bmp.IsSameAs(bmpPrev) )
617 m_statbmp->SetBitmap(bmp);
618 }
f1df0927 619 }
0a089246
VZ
620#endif // wxUSE_STATBMP
621
f1df0927 622
74b31181 623 // and update the buttons state
2b5f62a0 624 m_btnPrev->Enable(HasPrevPage(m_page));
66cd017c 625
7f3bf33a
VZ
626 const bool hasNext = HasNextPage(m_page);
627 const wxString label = hasNext ? _("&Next >") : _("&Finish");
628 if ( label != m_btnNext->GetLabel() )
629 m_btnNext->SetLabel(label);
66cd017c 630
0a089246
VZ
631 m_btnNext->SetDefault();
632
633
5bc28e84 634 // send the change event to the new page now
2365e5cb 635 wxWizardEvent event(wxEVT_WIZARD_PAGE_CHANGED, GetId(), goingForward, m_page);
5bc28e84
VZ
636 (void)m_page->GetEventHandler()->ProcessEvent(event);
637
638 // and finally show it
639 m_page->Show();
640 m_page->SetFocus();
641
0a089246
VZ
642 if ( !m_usingSizer )
643 m_sizerBmpAndPage->Layout();
644
645 if ( !m_started )
646 {
647 m_started = true;
648
3aa8e4ea
JS
649 DoWizardLayout();
650 }
651
634bb98a 652 if (GetBitmapPlacement() && m_statbmp)
3aa8e4ea
JS
653 {
654 ResizeBitmap(bmp);
634bb98a 655
3aa8e4ea
JS
656 if ( !bmp.IsSameAs(bmpPrev) )
657 m_statbmp->SetBitmap(bmp);
634bb98a 658
3aa8e4ea
JS
659 if (m_usingSizer)
660 m_sizerPage->RecalcSizes();
0a089246
VZ
661 }
662
deeb0a89
JS
663 wxWizardEvent pageShownEvent(wxEVT_WIZARD_PAGE_SHOWN, GetId(),
664 goingForward, m_page);
665 m_page->GetEventHandler()->ProcessEvent(pageShownEvent);
666
ca65c044 667 return true;
66cd017c
VZ
668}
669
3aa8e4ea
JS
670/// Do fit, and adjust to screen size if necessary
671void wxWizard::DoWizardLayout()
672{
673 if ( wxSystemSettings::GetScreenType() > wxSYS_SCREEN_PDA )
674 {
675 if (CanDoLayoutAdaptation())
676 DoLayoutAdaptation();
677 else
678 GetSizer()->SetSizeHints(this);
679
680 if ( m_posWizard == wxDefaultPosition )
681 CentreOnScreen();
682 }
683
684 SetLayoutAdaptationDone(true);
685}
686
74b31181 687bool wxWizard::RunWizard(wxWizardPage *firstPage)
66cd017c 688{
ca65c044
WS
689 wxCHECK_MSG( firstPage, false, wxT("can't run empty wizard") );
690
ca65c044
WS
691 // can't return false here because there is no old page
692 (void)ShowPage(firstPage, true /* forward */);
66cd017c 693
94c09a19 694 m_wasModal = true;
37f6a080 695
66cd017c
VZ
696 return ShowModal() == wxID_OK;
697}
698
74b31181 699wxWizardPage *wxWizard::GetCurrentPage() const
66cd017c 700{
74b31181 701 return m_page;
66cd017c
VZ
702}
703
4fe5383d 704wxSize wxWizard::GetPageSize() const
07f20d9a
VZ
705{
706 // default width and height of the page
0a089246
VZ
707 int DEFAULT_PAGE_WIDTH,
708 DEFAULT_PAGE_HEIGHT;
709 if ( wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA )
a6328131
JS
710 {
711 // Make the default page size small enough to fit on screen
712 DEFAULT_PAGE_WIDTH = wxSystemSettings::GetMetric(wxSYS_SCREEN_X) / 2;
713 DEFAULT_PAGE_HEIGHT = wxSystemSettings::GetMetric(wxSYS_SCREEN_Y) / 2;
714 }
0a089246
VZ
715 else // !PDA
716 {
717 DEFAULT_PAGE_WIDTH =
718 DEFAULT_PAGE_HEIGHT = 270;
719 }
94c09a19 720
0a089246
VZ
721 // start with default minimal size
722 wxSize pageSize(DEFAULT_PAGE_WIDTH, DEFAULT_PAGE_HEIGHT);
ca65c044 723
0a089246
VZ
724 // make the page at least as big as specified by user
725 pageSize.IncTo(m_sizePage);
ca65c044 726
aedd6d6a
VZ
727 if ( m_statbmp )
728 {
0a089246
VZ
729 // make the page at least as tall as the bitmap
730 pageSize.IncTo(wxSize(0, m_bitmap.GetHeight()));
aedd6d6a 731 }
ca65c044 732
0a089246
VZ
733 if ( m_usingSizer )
734 {
735 // make it big enough to contain all pages added to the sizer
736 pageSize.IncTo(m_sizerPage->GetMaxChildSize());
737 }
738
739 return pageSize;
740}
741
742wxSizer *wxWizard::GetPageAreaSizer() const
743{
744 return m_sizerPage;
745}
746
747void wxWizard::SetBorder(int border)
748{
749 wxCHECK_RET(!m_started, wxT("wxWizard::SetBorder after RunWizard"));
750
751 m_border = border;
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
9a83f860 783 wxCHECK_RET( m_page, wxT("should have a valid current page") );
47e64664 784
f6bcfd97 785 // ask the current page first: notice that we do it before calling
e75e8815 786 // GetNext/Prev() because the data transferred from the controls of the page
f6bcfd97 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
c9f18835
JS
796 // Give the application a chance to set state which may influence GetNext()/GetPrev()
797 wxWizardEvent eventPreChanged(wxEVT_WIZARD_BEFORE_PAGE_CHANGED, GetId(), forward, m_page);
798 (void)m_page->GetEventHandler()->ProcessEvent(eventPreChanged);
799
800 if (!eventPreChanged.IsAllowed())
801 return;
802
74b31181
VZ
803 wxWizardPage *page;
804 if ( forward )
66cd017c 805 {
74b31181 806 page = m_page->GetNext();
66cd017c 807 }
74b31181 808 else // back
66cd017c 809 {
74b31181
VZ
810 page = m_page->GetPrev();
811
223d09f6 812 wxASSERT_MSG( page, wxT("\"<Back\" button should have been disabled") );
66cd017c 813 }
74b31181 814
0a089246 815 // just pass to the new page (or maybe not - but we don't care here)
74b31181 816 (void)ShowPage(page, forward);
66cd017c
VZ
817}
818
f80bf901
VZ
819void wxWizard::OnHelp(wxCommandEvent& WXUNUSED(event))
820{
821 // this function probably can never be called when we don't have an active
822 // page, but a small extra check won't hurt
823 if(m_page != NULL)
824 {
825 // Create and send the help event to the specific page handler
826 // event data contains the active page so that context-sensitive
827 // help is possible
ca65c044 828 wxWizardEvent eventHelp(wxEVT_WIZARD_HELP, GetId(), true, m_page);
f80bf901
VZ
829 (void)m_page->GetEventHandler()->ProcessEvent(eventHelp);
830 }
831}
832
91c68292
VZ
833void wxWizard::OnWizEvent(wxWizardEvent& event)
834{
835 // the dialogs have wxWS_EX_BLOCK_EVENTS style on by default but we want to
836 // propagate wxEVT_WIZARD_XXX to the parent (if any), so do it manually
837 if ( !(GetExtraStyle() & wxWS_EX_BLOCK_EVENTS) )
838 {
839 // the event will be propagated anyhow
0a640208 840 event.Skip();
91c68292 841 }
b3eb133b
WS
842 else
843 {
844 wxWindow *parent = GetParent();
91c68292 845
b3eb133b
WS
846 if ( !parent || !parent->GetEventHandler()->ProcessEvent(event) )
847 {
848 event.Skip();
849 }
850 }
37f6a080 851
94c09a19 852 if ( ( !m_wasModal ) &&
37f6a080
JS
853 event.IsAllowed() &&
854 ( event.GetEventType() == wxEVT_WIZARD_FINISHED ||
855 event.GetEventType() == wxEVT_WIZARD_CANCEL
856 )
857 )
858 {
37f6a080
JS
859 Destroy();
860 }
91c68292 861}
f80bf901 862
f49fd6d0
JS
863void wxWizard::SetBitmap(const wxBitmap& bitmap)
864{
865 m_bitmap = bitmap;
866 if (m_statbmp)
867 m_statbmp->SetBitmap(m_bitmap);
868}
869
66cd017c
VZ
870// ----------------------------------------------------------------------------
871// wxWizardEvent
872// ----------------------------------------------------------------------------
873
f80bf901 874wxWizardEvent::wxWizardEvent(wxEventType type, int id, bool direction, wxWizardPage* page)
66cd017c
VZ
875 : wxNotifyEvent(type, id)
876{
f80bf901
VZ
877 // Modified 10-20-2001 Robert Cavanaugh
878 // add the active page to the event data
74b31181 879 m_direction = direction;
f80bf901 880 m_page = page;
66cd017c 881}
74b31181 882
3aa8e4ea
JS
883/// Do the adaptation
884bool wxWizard::DoLayoutAdaptation()
885{
886 wxWindowList windows;
887 wxWindowList pages;
888
889 // Make all the pages (that use sizers) scrollable
890 for ( wxSizerItemList::compatibility_iterator node = m_sizerPage->GetChildren().GetFirst(); node; node = node->GetNext() )
891 {
892 wxSizerItem * const item = node->GetData();
893 if ( item->IsWindow() )
894 {
895 wxWizardPage* page = wxDynamicCast(item->GetWindow(), wxWizardPage);
896 if (page)
897 {
898 while (page)
899 {
900 if (!pages.Find(page) && page->GetSizer())
901 {
902 // Create a scrolled window and reparent
903 wxScrolledWindow* scrolledWindow = new wxScrolledWindow(page, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL|wxVSCROLL|wxHSCROLL|wxBORDER_NONE);
904 wxSizer* oldSizer = page->GetSizer();
634bb98a 905
3aa8e4ea
JS
906 wxSizer* newSizer = new wxBoxSizer(wxVERTICAL);
907 newSizer->Add(scrolledWindow,1, wxEXPAND, 0);
634bb98a 908
3aa8e4ea 909 page->SetSizer(newSizer, false /* don't delete the old sizer */);
634bb98a 910
3aa8e4ea 911 scrolledWindow->SetSizer(oldSizer);
634bb98a 912
3aa8e4ea 913 wxStandardDialogLayoutAdapter::DoReparentControls(page, scrolledWindow);
634bb98a 914
3aa8e4ea
JS
915 pages.Append(page);
916 windows.Append(scrolledWindow);
917 }
918 page = page->GetNext();
919 }
920 }
921 }
922 }
923
924 wxStandardDialogLayoutAdapter::DoFitWithScrolling(this, windows);
925
a8eeee19
JS
926 // Size event doesn't get sent soon enough on wxGTK
927 DoLayout();
03647350 928
3aa8e4ea
JS
929 SetLayoutAdaptationDone(true);
930
931 return true;
932}
933
934bool wxWizard::ResizeBitmap(wxBitmap& bmp)
935{
936 if (!GetBitmapPlacement())
937 return false;
938
a1b806b9 939 if (bmp.IsOk())
3aa8e4ea
JS
940 {
941 wxSize pageSize = m_sizerPage->GetSize();
49b75749
JS
942 if (pageSize == wxSize(0,0))
943 pageSize = GetPageSize();
3aa8e4ea
JS
944 int bitmapWidth = wxMax(bmp.GetWidth(), GetMinimumBitmapWidth());
945 int bitmapHeight = pageSize.y;
946
a1b806b9 947 if (!m_statbmp->GetBitmap().IsOk() || m_statbmp->GetBitmap().GetHeight() != bitmapHeight)
3aa8e4ea
JS
948 {
949 wxBitmap bitmap(bitmapWidth, bitmapHeight);
950 {
951 wxMemoryDC dc;
952 dc.SelectObject(bitmap);
953 dc.SetBackground(wxBrush(m_bitmapBackgroundColour));
954 dc.Clear();
955
956 if (GetBitmapPlacement() & wxWIZARD_TILE)
957 {
958 TileBitmap(wxRect(0, 0, bitmapWidth, bitmapHeight), dc, bmp);
959 }
960 else
961 {
962 int x, y;
963
964 if (GetBitmapPlacement() & wxWIZARD_HALIGN_LEFT)
965 x = 0;
966 else if (GetBitmapPlacement() & wxWIZARD_HALIGN_RIGHT)
634bb98a 967 x = bitmapWidth - bmp.GetWidth();
3aa8e4ea 968 else
634bb98a 969 x = (bitmapWidth - bmp.GetWidth())/2;
3aa8e4ea
JS
970
971 if (GetBitmapPlacement() & wxWIZARD_VALIGN_TOP)
972 y = 0;
973 else if (GetBitmapPlacement() & wxWIZARD_VALIGN_BOTTOM)
634bb98a 974 y = bitmapHeight - bmp.GetHeight();
3aa8e4ea 975 else
634bb98a 976 y = (bitmapHeight - bmp.GetHeight())/2;
3aa8e4ea
JS
977
978 dc.DrawBitmap(bmp, x, y, true);
979 dc.SelectObject(wxNullBitmap);
980 }
981 }
982
983 bmp = bitmap;
984 }
985 }
986
987 return true;
988}
989
990bool wxWizard::TileBitmap(const wxRect& rect, wxDC& dc, const wxBitmap& bitmap)
991{
992 int w = bitmap.GetWidth();
993 int h = bitmap.GetHeight();
994
995 wxMemoryDC dcMem;
996
997 dcMem.SelectObjectAsSource(bitmap);
998
999 int i, j;
1000 for (i = rect.x; i < rect.x + rect.width; i += w)
1001 {
1002 for (j = rect.y; j < rect.y + rect.height; j+= h)
1003 dc.Blit(i, j, bitmap.GetWidth(), bitmap.GetHeight(), & dcMem, 0, 0);
1004 }
1005 dcMem.SelectObject(wxNullBitmap);
1006
1007 return true;
1008}
1009
1e6feb95 1010#endif // wxUSE_WIZARDDLG