added sizers support; allow resizeable wizards (Robert Vazan)
[wxWidgets.git] / src / generic / wizard.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: 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 #ifdef __GNUG__
25 #pragma implementation "wizardg.h"
26 #endif
27
28 // For compilers that support precompilation, includes "wx.h".
29 #include "wx/wxprec.h"
30
31 #ifdef __BORLANDC__
32 #pragma hdrstop
33 #endif
34
35 #if wxUSE_WIZARDDLG
36
37 #ifndef WX_PRECOMP
38 #include "wx/dynarray.h"
39 #include "wx/intl.h"
40 #include "wx/statbmp.h"
41 #include "wx/button.h"
42 #endif //WX_PRECOMP
43
44 #include "wx/statline.h"
45 #include "wx/sizer.h"
46
47 #include "wx/wizard.h"
48
49 // ----------------------------------------------------------------------------
50 // event tables and such
51 // ----------------------------------------------------------------------------
52
53 DEFINE_EVENT_TYPE(wxEVT_WIZARD_PAGE_CHANGED)
54 DEFINE_EVENT_TYPE(wxEVT_WIZARD_PAGE_CHANGING)
55 DEFINE_EVENT_TYPE(wxEVT_WIZARD_CANCEL)
56 DEFINE_EVENT_TYPE(wxEVT_WIZARD_FINISHED)
57 DEFINE_EVENT_TYPE(wxEVT_WIZARD_HELP)
58
59 BEGIN_EVENT_TABLE(wxWizard, wxDialog)
60 EVT_BUTTON(wxID_CANCEL, wxWizard::OnCancel)
61 EVT_BUTTON(wxID_BACKWARD, wxWizard::OnBackOrNext)
62 EVT_BUTTON(wxID_FORWARD, wxWizard::OnBackOrNext)
63 EVT_BUTTON(wxID_HELP, wxWizard::OnHelp)
64
65 EVT_WIZARD_PAGE_CHANGED(-1, wxWizard::OnWizEvent)
66 EVT_WIZARD_PAGE_CHANGING(-1, wxWizard::OnWizEvent)
67 EVT_WIZARD_CANCEL(-1, wxWizard::OnWizEvent)
68 EVT_WIZARD_FINISHED(-1, wxWizard::OnWizEvent)
69 EVT_WIZARD_HELP(-1, wxWizard::OnWizEvent)
70 END_EVENT_TABLE()
71
72 IMPLEMENT_DYNAMIC_CLASS(wxWizard, wxDialog)
73 IMPLEMENT_ABSTRACT_CLASS(wxWizardPage, wxPanel)
74 IMPLEMENT_DYNAMIC_CLASS(wxWizardPageSimple, wxWizardPage)
75 IMPLEMENT_DYNAMIC_CLASS(wxWizardEvent, wxNotifyEvent)
76
77 // ============================================================================
78 // implementation
79 // ============================================================================
80
81 // ----------------------------------------------------------------------------
82 // wxWizardPage
83 // ----------------------------------------------------------------------------
84
85 void wxWizardPage::Init()
86 {
87 m_bitmap = wxNullBitmap;
88 }
89
90 wxWizardPage::wxWizardPage(wxWizard *parent,
91 const wxBitmap& bitmap,
92 const wxChar *resource)
93 {
94 Create(parent, bitmap, resource);
95 }
96
97 bool wxWizardPage::Create(wxWizard *parent,
98 const wxBitmap& bitmap,
99 const wxChar *resource)
100 {
101 if ( !wxPanel::Create(parent, -1) )
102 return FALSE;
103
104 if ( resource != NULL )
105 {
106 #if wxUSE_WX_RESOURCES
107 #if 0
108 if ( !LoadFromResource(this, resource) )
109 {
110 wxFAIL_MSG(wxT("wxWizardPage LoadFromResource failed!!!!"));
111 }
112 #endif
113 #endif // wxUSE_RESOURCES
114 }
115
116 m_bitmap = bitmap;
117
118 // initially the page is hidden, it's shown only when it becomes current
119 Hide();
120
121 return TRUE;
122 }
123
124 // ----------------------------------------------------------------------------
125 // wxWizardPageSimple
126 // ----------------------------------------------------------------------------
127
128 wxWizardPage *wxWizardPageSimple::GetPrev() const
129 {
130 return m_prev;
131 }
132
133 wxWizardPage *wxWizardPageSimple::GetNext() const
134 {
135 return m_next;
136 }
137
138 // ----------------------------------------------------------------------------
139 // wxWizardSizer
140 // ----------------------------------------------------------------------------
141
142 class wxWizardSizer : public wxSizer
143 {
144 public:
145 wxWizardSizer(wxWizard *owner);
146
147 void RecalcSizes();
148 wxSize CalcMin();
149
150 wxSize GetMaxChildSize();
151 int Border() const;
152
153 private:
154 wxSize SiblingSize(wxSizerItem *child);
155
156 wxWizard *m_owner;
157 bool m_childSizeValid;
158 wxSize m_childSize;
159
160 DECLARE_CLASS(wxWizardSizer);
161 };
162
163 IMPLEMENT_CLASS(wxWizardSizer, wxSizer)
164
165 wxWizardSizer::wxWizardSizer(wxWizard *owner)
166 : m_owner(owner)
167 {
168 m_childSizeValid = false;
169 }
170
171 void wxWizardSizer::RecalcSizes()
172 {
173 // Effect of this function depends on m_owner->m_page and
174 // it should be called whenever it changes (wxWizard::ShowPage)
175 if ( m_owner->m_page )
176 {
177 m_owner->m_page->SetSize(m_position.x,m_position.y, m_size.x,m_size.y);
178 }
179 }
180
181 wxSize wxWizardSizer::CalcMin()
182 {
183 return m_owner->GetPageSize();
184 }
185
186 wxSize wxWizardSizer::GetMaxChildSize()
187 {
188 #if !defined(__WXDEBUG__)
189 if ( m_childSizeValid )
190 return m_childSize;
191 #endif
192
193 wxSize maxOfMin;
194 wxSizerItemList::Node *childNode;
195
196 for(childNode = m_children.GetFirst(); childNode;
197 childNode = childNode->GetNext())
198 {
199 wxSizerItem *child = childNode->GetData();
200 maxOfMin.IncTo(child->CalcMin());
201 maxOfMin.IncTo(SiblingSize(child));
202 }
203
204 #ifdef __WXDEBUG__
205 if ( m_childSizeValid && m_childSize != maxOfMin )
206 {
207 wxFAIL_MSG( _T("Size changed in wxWizard::GetPageAreaSizer()")
208 _T("after RunWizard().\n")
209 _T("Did you forget to call GetSizer()->Fit(this) ")
210 _T("for some page?")) ;
211
212 return m_childSize;
213 }
214 #endif // __WXDEBUG__
215
216 if ( m_owner->m_started )
217 {
218 m_childSizeValid = true;
219 m_childSize = maxOfMin;
220 }
221
222 return maxOfMin;
223 }
224
225 int wxWizardSizer::Border() const
226 {
227 if ( m_owner->m_calledSetBorder )
228 return m_owner->m_border;
229
230 return m_children.IsEmpty() ? 5 : 0;
231 }
232
233 wxSize wxWizardSizer::SiblingSize(wxSizerItem *child)
234 {
235 wxSize maxSibling;
236
237 if ( child->IsWindow() )
238 {
239 wxWizardPage *page = wxDynamicCast(child->GetWindow(), wxWizardPage);
240 if ( page )
241 {
242 for ( wxWizardPage *sibling = page->GetNext();
243 sibling;
244 sibling = sibling->GetNext() )
245 {
246 if ( sibling->GetSizer() )
247 {
248 maxSibling.IncTo(sibling->GetSizer()->CalcMin());
249 }
250 }
251 }
252 }
253
254 return maxSibling;
255 }
256
257 // ----------------------------------------------------------------------------
258 // generic wxWizard implementation
259 // ----------------------------------------------------------------------------
260
261 void wxWizard::Init()
262 {
263 m_posWizard = wxDefaultPosition;
264 m_page = (wxWizardPage *)NULL;
265 m_btnPrev = m_btnNext = NULL;
266 m_statbmp = NULL;
267 m_sizerPage = NULL;
268 m_calledSetBorder = false;
269 m_border = 0;
270 m_started = false;
271 }
272
273 bool wxWizard::Create(wxWindow *parent,
274 int id,
275 const wxString& title,
276 const wxBitmap& bitmap,
277 const wxPoint& pos,
278 long style)
279 {
280 bool result = wxDialog::Create(parent,id,title,pos,wxDefaultSize,style);
281
282 m_posWizard = pos;
283 m_bitmap = bitmap ;
284
285 DoCreateControls();
286
287 return result;
288 }
289
290 void wxWizard::AddBitmapRow(wxBoxSizer *mainColumn)
291 {
292 m_sizerBmpAndPage = new wxBoxSizer(wxHORIZONTAL);
293 mainColumn->Add(
294 m_sizerBmpAndPage,
295 1, // Vertically stretchable
296 wxEXPAND // Horizonal stretching, no border
297 );
298 mainColumn->Add(0,5,
299 0, // No vertical stretching
300 wxEXPAND // No border, (mostly useless) horizontal stretching
301 );
302
303 if ( m_bitmap.Ok() )
304 {
305 m_statbmp = new wxStaticBitmap(this, -1, m_bitmap);
306 m_sizerBmpAndPage->Add(
307 m_statbmp,
308 0, // No horizontal stretching
309 wxALL, // Border all around, top alignment
310 5 // Border width
311 );
312 m_sizerBmpAndPage->Add(
313 5,0,
314 0, // No horizontal stretching
315 wxEXPAND // No border, (mostly useless) vertical stretching
316 );
317 }
318 else
319 m_statbmp = (wxStaticBitmap *)NULL;
320
321 // Added to m_sizerBmpAndPage in FinishLayout
322 m_sizerPage = new wxWizardSizer(this);
323 }
324
325 void wxWizard::AddStaticLine(wxBoxSizer *mainColumn)
326 {
327 #if wxUSE_STATLINE
328 mainColumn->Add(
329 new wxStaticLine(this, -1),
330 0, // Vertically unstretchable
331 wxEXPAND | wxALL, // Border all around, horizontally stretchable
332 5 // Border width
333 );
334 mainColumn->Add(0,5,
335 0, // No vertical stretching
336 wxEXPAND // No border, (mostly useless) horizontal stretching
337 );
338 #else
339 (void)mainColumn;
340 #endif // wxUSE_STATLINE
341 }
342
343 void wxWizard::AddBackNextPair(wxBoxSizer *buttonRow)
344 {
345 // margin between Back and Next buttons
346 #ifdef __WXMAC__
347 static const int BACKNEXT_MARGIN = 10;
348 #else
349 static const int BACKNEXT_MARGIN = 0;
350 #endif
351
352 wxBoxSizer *backNextPair = new wxBoxSizer(wxHORIZONTAL);
353 buttonRow->Add(
354 backNextPair,
355 0, // No horizontal stretching
356 wxALL, // Border all around
357 5 // Border width
358 );
359
360 m_btnPrev = new wxButton(this, wxID_BACKWARD, _("< &Back"));
361 backNextPair->Add(m_btnPrev);
362 backNextPair->Add(BACKNEXT_MARGIN,0,
363 0, // No horizontal stretching
364 wxEXPAND // No border, (mostly useless) vertical stretching
365 );
366 m_btnNext = new wxButton(this, wxID_FORWARD, _("&Next >"));
367 backNextPair->Add(m_btnNext);
368 }
369
370 void wxWizard::AddButtonRow(wxBoxSizer *mainColumn)
371 {
372 wxBoxSizer *buttonRow = new wxBoxSizer(wxHORIZONTAL);
373 mainColumn->Add(
374 buttonRow,
375 0, // Vertically unstretchable
376 wxALIGN_RIGHT // Right aligned, no border
377 );
378
379 if (GetExtraStyle() & wxWIZARD_EX_HELPBUTTON)
380 buttonRow->Add(
381 new wxButton(this, wxID_HELP, _("&Help")),
382 0, // Horizontally unstretchable
383 wxALL, // Border all around, top aligned
384 5 // Border width
385 );
386
387 AddBackNextPair(buttonRow);
388
389 buttonRow->Add(
390 new wxButton(this, wxID_CANCEL, _("&Cancel")),
391 0, // Horizontally unstretchable
392 wxALL, // Border all around, top aligned
393 5 // Border width
394 );
395 }
396
397 void wxWizard::DoCreateControls()
398 {
399 // do nothing if the controls were already created
400 if ( WasCreated() )
401 return;
402
403 // wxWindow::SetSizer will be called at end
404 wxBoxSizer *windowSizer = new wxBoxSizer(wxVERTICAL);
405
406 wxBoxSizer *mainColumn = new wxBoxSizer(wxVERTICAL);
407 windowSizer->Add(
408 mainColumn,
409 1, // Vertical stretching
410 wxALL | wxEXPAND, // Border all around, horizontal stretching
411 5 // Border width
412 );
413
414 AddBitmapRow(mainColumn);
415 AddStaticLine(mainColumn);
416 AddButtonRow(mainColumn);
417
418 // wxWindow::SetSizer should be followed by wxWindow::Fit, but
419 // this is done in FinishLayout anyway so why duplicate it
420 SetSizer(windowSizer);
421 }
422
423 void wxWizard::SetPageSize(const wxSize& size)
424 {
425 wxCHECK_RET(!m_started,wxT("wxWizard::SetPageSize after RunWizard"));
426 m_sizePage = size;
427 }
428
429 void wxWizard::FinishLayout()
430 {
431 m_sizerBmpAndPage->Add(
432 m_sizerPage,
433 1, // Horizontal stretching
434 wxEXPAND | wxALL, // Vertically stretchable
435 m_sizerPage->Border()
436 );
437
438 GetSizer()->SetSizeHints(this);
439 if ( m_posWizard == wxDefaultPosition )
440 CentreOnScreen();
441 }
442
443 void wxWizard::FitToPage(const wxWizardPage *page)
444 {
445 wxCHECK_RET(!m_started,wxT("wxWizard::FitToPage after RunWizard"));
446
447 while ( page )
448 {
449 wxSize size = page->GetBestSize();
450
451 m_sizePage.IncTo(size);
452
453 page = page->GetNext();
454 }
455 }
456
457 bool wxWizard::ShowPage(wxWizardPage *page, bool goingForward)
458 {
459 wxASSERT_MSG( page != m_page, wxT("this is useless") );
460
461 // we'll use this to decide whether we have to change the label of this
462 // button or not (initially the label is "Next")
463 bool btnLabelWasNext = TRUE;
464
465 // Modified 10-20-2001 Robert Cavanaugh.
466 // Fixed bug for displaying a new bitmap
467 // in each *consecutive* page
468
469 // flag to indicate if this page uses a new bitmap
470 bool bmpIsDefault = TRUE;
471
472 // use these labels to determine if we need to change the bitmap
473 // for this page
474 wxBitmap bmpPrev, bmpCur;
475
476 // check for previous page
477 if ( m_page )
478 {
479 // send the event to the old page
480 wxWizardEvent event(wxEVT_WIZARD_PAGE_CHANGING, GetId(), goingForward, m_page);
481 if ( m_page->GetEventHandler()->ProcessEvent(event) &&
482 !event.IsAllowed() )
483 {
484 // vetoed by the page
485 return FALSE;
486 }
487
488 m_page->Hide();
489
490 btnLabelWasNext = HasNextPage(m_page);
491
492 // Get the bitmap of the previous page (if it exists)
493 if ( m_page->GetBitmap().Ok() )
494 {
495 bmpPrev = m_page->GetBitmap();
496 }
497 }
498
499 // set the new page
500 m_page = page;
501
502 // is this the end?
503 if ( !m_page )
504 {
505 // terminate successfully
506 EndModal(wxID_OK);
507 if ( !IsModal() )
508 {
509 wxWizardEvent event(wxEVT_WIZARD_FINISHED, GetId(),FALSE, 0);
510 (void)GetEventHandler()->ProcessEvent(event);
511 }
512 return TRUE;
513 }
514
515 // position and show the new page
516 (void)m_page->TransferDataToWindow();
517
518 // wxWizardSizer::RecalcSizes wants to be called when m_page changes
519 m_sizerPage->RecalcSizes();
520
521 // check if bitmap needs to be updated
522 // update default flag as well
523 if ( m_page->GetBitmap().Ok() )
524 {
525 bmpCur = m_page->GetBitmap();
526 bmpIsDefault = FALSE;
527 }
528
529 // change the bitmap if:
530 // 1) a default bitmap was selected in constructor
531 // 2) this page was constructed with a bitmap
532 // 3) this bitmap is not the previous bitmap
533 if ( m_statbmp && (bmpCur != bmpPrev) )
534 {
535 wxBitmap bmp;
536 if ( bmpIsDefault )
537 bmp = m_bitmap;
538 else
539 bmp = m_page->GetBitmap();
540 m_statbmp->SetBitmap(bmp);
541 }
542
543 // and update the buttons state
544 m_btnPrev->Enable(HasPrevPage(m_page));
545
546 bool hasNext = HasNextPage(m_page);
547 if ( btnLabelWasNext != hasNext )
548 {
549 // need to update
550 if (btnLabelWasNext)
551 m_btnNext->SetLabel(_("&Finish"));
552 else
553 m_btnNext->SetLabel(_("&Next >"));
554 }
555 // nothing to do: the label was already correct
556
557 // send the change event to the new page now
558 wxWizardEvent event(wxEVT_WIZARD_PAGE_CHANGED, GetId(), goingForward, m_page);
559 (void)m_page->GetEventHandler()->ProcessEvent(event);
560
561 // and finally show it
562 m_page->Show();
563 m_page->SetFocus();
564
565 return TRUE;
566 }
567
568 bool wxWizard::RunWizard(wxWizardPage *firstPage)
569 {
570 wxCHECK_MSG( firstPage, FALSE, wxT("can't run empty wizard") );
571
572 // Set before FinishLayout to enable wxWizardSizer::GetMaxChildSize
573 m_started = true;
574
575 // This cannot be done sooner, because user can change layout options
576 // up to this moment
577 FinishLayout();
578
579 // can't return FALSE here because there is no old page
580 (void)ShowPage(firstPage, TRUE /* forward */);
581
582 return ShowModal() == wxID_OK;
583 }
584
585 wxWizardPage *wxWizard::GetCurrentPage() const
586 {
587 return m_page;
588 }
589
590 wxSize wxWizard::GetPageSize() const
591 {
592 wxSize pageSize(GetManualPageSize());
593 pageSize.IncTo(m_sizerPage->GetMaxChildSize());
594 return pageSize;
595 }
596
597 wxSizer *wxWizard::GetPageAreaSizer() const
598 {
599 return m_sizerPage;
600 }
601
602 void wxWizard::SetBorder(int border)
603 {
604 wxCHECK_RET(!m_started,wxT("wxWizard::SetBorder after RunWizard"));
605 m_calledSetBorder = true;
606 m_border = border;
607 }
608
609 wxSize wxWizard::GetManualPageSize() const
610 {
611 // default width and height of the page
612 static const int DEFAULT_PAGE_WIDTH = 270;
613 static const int DEFAULT_PAGE_HEIGHT = 290;
614
615 wxSize totalPageSize(DEFAULT_PAGE_WIDTH,DEFAULT_PAGE_HEIGHT);
616
617 totalPageSize.IncTo(m_sizePage);
618
619 if(m_statbmp)
620 totalPageSize.IncTo(wxSize(0,m_bitmap.GetHeight()));
621
622 return totalPageSize;
623 }
624
625 void wxWizard::OnCancel(wxCommandEvent& WXUNUSED(eventUnused))
626 {
627 // this function probably can never be called when we don't have an active
628 // page, but a small extra check won't hurt
629 wxWindow *win = m_page ? (wxWindow *)m_page : (wxWindow *)this;
630
631 wxWizardEvent event(wxEVT_WIZARD_CANCEL, GetId(), FALSE, m_page);
632 if ( !win->GetEventHandler()->ProcessEvent(event) || event.IsAllowed() )
633 {
634 // no objections - close the dialog
635 EndModal(wxID_CANCEL);
636 }
637 //else: request to Cancel ignored
638 }
639
640 void wxWizard::OnBackOrNext(wxCommandEvent& event)
641 {
642 wxASSERT_MSG( (event.GetEventObject() == m_btnNext) ||
643 (event.GetEventObject() == m_btnPrev),
644 wxT("unknown button") );
645
646 // ask the current page first: notice that we do it before calling
647 // GetNext/Prev() because the data transfered from the controls of the page
648 // may change the value returned by these methods
649 if ( m_page && (!m_page->Validate() || !m_page->TransferDataFromWindow()) )
650 {
651 // the page data is incorrect, don't do anything
652 return;
653 }
654
655 bool forward = event.GetEventObject() == m_btnNext;
656
657 wxWizardPage *page;
658 if ( forward )
659 {
660 page = m_page->GetNext();
661 }
662 else // back
663 {
664 page = m_page->GetPrev();
665
666 wxASSERT_MSG( page, wxT("\"<Back\" button should have been disabled") );
667 }
668
669 // just pass to the new page (or may be not - but we don't care here)
670 (void)ShowPage(page, forward);
671 }
672
673 void wxWizard::OnHelp(wxCommandEvent& WXUNUSED(event))
674 {
675 // this function probably can never be called when we don't have an active
676 // page, but a small extra check won't hurt
677 if(m_page != NULL)
678 {
679 // Create and send the help event to the specific page handler
680 // event data contains the active page so that context-sensitive
681 // help is possible
682 wxWizardEvent eventHelp(wxEVT_WIZARD_HELP, GetId(), TRUE, m_page);
683 (void)m_page->GetEventHandler()->ProcessEvent(eventHelp);
684 }
685 }
686
687 void wxWizard::OnWizEvent(wxWizardEvent& event)
688 {
689 // the dialogs have wxWS_EX_BLOCK_EVENTS style on by default but we want to
690 // propagate wxEVT_WIZARD_XXX to the parent (if any), so do it manually
691 if ( !(GetExtraStyle() & wxWS_EX_BLOCK_EVENTS) )
692 {
693 // the event will be propagated anyhow
694 return;
695 }
696
697 wxWindow *parent = GetParent();
698
699 if ( !parent || !parent->GetEventHandler()->ProcessEvent(event) )
700 {
701 event.Skip();
702 }
703 }
704
705 // ----------------------------------------------------------------------------
706 // our public interface
707 // ----------------------------------------------------------------------------
708
709 #ifdef WXWIN_COMPATIBILITY_2_2
710
711 /* static */
712 wxWizard *wxWizardBase::Create(wxWindow *parent,
713 int id,
714 const wxString& title,
715 const wxBitmap& bitmap,
716 const wxPoint& pos,
717 const wxSize& WXUNUSED(size))
718 {
719 return new wxWizard(parent, id, title, bitmap, pos);
720 }
721
722 #endif // WXWIN_COMPATIBILITY_2_2
723
724 // ----------------------------------------------------------------------------
725 // wxWizardEvent
726 // ----------------------------------------------------------------------------
727
728 wxWizardEvent::wxWizardEvent(wxEventType type, int id, bool direction, wxWizardPage* page)
729 : wxNotifyEvent(type, id)
730 {
731 // Modified 10-20-2001 Robert Cavanaugh
732 // add the active page to the event data
733 m_direction = direction;
734 m_page = page;
735 }
736
737 #endif // wxUSE_WIZARDDLG