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