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