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