fixed propagating of EVT_WIZARD_XXX events to the parent
[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 // Created: 15.08.99
10 // RCS-ID: $Id$
11 // Copyright: (c) 1999 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
12 // Licence: wxWindows license
13 ///////////////////////////////////////////////////////////////////////////////
14
15 // ============================================================================
16 // declarations
17 // ============================================================================
18
19 // ----------------------------------------------------------------------------
20 // headers
21 // ----------------------------------------------------------------------------
22
23 #ifdef __GNUG__
24 #pragma implementation "wizardg.h"
25 #endif
26
27 // For compilers that support precompilation, includes "wx.h".
28 #include "wx/wxprec.h"
29
30 #ifdef __BORLANDC__
31 #pragma hdrstop
32 #endif
33
34 #if wxUSE_WIZARDDLG
35
36 #ifndef WX_PRECOMP
37 #include "wx/dynarray.h"
38 #include "wx/intl.h"
39 #include "wx/statbmp.h"
40 #include "wx/button.h"
41 #endif //WX_PRECOMP
42
43 #include "wx/statline.h"
44
45 #include "wx/wizard.h"
46
47 // ----------------------------------------------------------------------------
48 // simple types
49 // ----------------------------------------------------------------------------
50
51 WX_DEFINE_ARRAY(wxPanel *, wxArrayPages);
52
53 // ----------------------------------------------------------------------------
54 // event tables and such
55 // ----------------------------------------------------------------------------
56
57 DEFINE_EVENT_TYPE(wxEVT_WIZARD_PAGE_CHANGED)
58 DEFINE_EVENT_TYPE(wxEVT_WIZARD_PAGE_CHANGING)
59 DEFINE_EVENT_TYPE(wxEVT_WIZARD_CANCEL)
60 DEFINE_EVENT_TYPE(wxEVT_WIZARD_HELP)
61
62 BEGIN_EVENT_TABLE(wxWizard, wxDialog)
63 EVT_BUTTON(wxID_CANCEL, wxWizard::OnCancel)
64 EVT_BUTTON(wxID_BACKWARD, wxWizard::OnBackOrNext)
65 EVT_BUTTON(wxID_FORWARD, wxWizard::OnBackOrNext)
66 EVT_BUTTON(wxID_HELP, wxWizard::OnHelp)
67
68 EVT_WIZARD_PAGE_CHANGED(-1, wxWizard::OnWizEvent)
69 EVT_WIZARD_PAGE_CHANGING(-1, wxWizard::OnWizEvent)
70 EVT_WIZARD_CANCEL(-1, wxWizard::OnWizEvent)
71 EVT_WIZARD_HELP(-1, wxWizard::OnWizEvent)
72 END_EVENT_TABLE()
73
74 IMPLEMENT_DYNAMIC_CLASS(wxWizard, wxDialog)
75 IMPLEMENT_ABSTRACT_CLASS(wxWizardPage, wxPanel)
76 IMPLEMENT_DYNAMIC_CLASS(wxWizardPageSimple, wxWizardPage)
77 IMPLEMENT_DYNAMIC_CLASS(wxWizardEvent, wxNotifyEvent)
78
79 // ============================================================================
80 // implementation
81 // ============================================================================
82
83 // ----------------------------------------------------------------------------
84 // wxWizardPage
85 // ----------------------------------------------------------------------------
86
87 void wxWizardPage::Init()
88 {
89 m_bitmap = wxNullBitmap;
90 }
91
92 wxWizardPage::wxWizardPage(wxWizard *parent,
93 const wxBitmap& bitmap,
94 const wxChar *resource)
95 {
96 Create(parent, bitmap, resource);
97 }
98
99 bool wxWizardPage::Create(wxWizard *parent,
100 const wxBitmap& bitmap,
101 const wxChar *resource)
102 {
103 if ( !wxPanel::Create(parent, -1) )
104 return FALSE;
105
106 if ( resource != NULL )
107 {
108 #if wxUSE_WX_RESOURCES
109 if ( !LoadFromResource(this, resource) )
110 {
111 wxFAIL_MSG(wxT("wxWizardPage LoadFromResource failed!!!!"));
112 }
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 // generic wxWizard implementation
139 // ----------------------------------------------------------------------------
140
141 void wxWizard::Init()
142 {
143 m_posWizard = wxDefaultPosition;
144 m_page = (wxWizardPage *)NULL;
145 m_btnPrev = m_btnNext = NULL;
146 m_statbmp = NULL;
147 }
148
149 bool wxWizard::Create(wxWindow *parent,
150 int id,
151 const wxString& title,
152 const wxBitmap& bitmap,
153 const wxPoint& pos)
154 {
155 m_posWizard = pos;
156 m_bitmap = bitmap ;
157
158 // just create the dialog itself here, the controls will be created in
159 // DoCreateControls() called later when we know our final size
160 m_page = (wxWizardPage *)NULL;
161 m_btnPrev = m_btnNext = NULL;
162 m_statbmp = NULL;
163
164 return wxDialog::Create(parent, id, title, pos);
165 }
166
167 void wxWizard::DoCreateControls()
168 {
169 // do nothing if the controls were already created
170 if ( WasCreated() )
171 return;
172
173 // constants defining the dialog layout
174 // ------------------------------------
175
176 // these constants define the position of the upper left corner of the
177 // bitmap or the page in the wizard
178 static const int X_MARGIN = 10;
179 static const int Y_MARGIN = 10;
180
181 // margin between the bitmap and the panel
182 static const int BITMAP_X_MARGIN = 15;
183
184 // margin between the bitmap and the static line
185 static const int BITMAP_Y_MARGIN = 15;
186
187 // margin between the static line and the buttons
188 static const int SEPARATOR_LINE_MARGIN = 15;
189
190 // margin between "Next >" and "Cancel" buttons
191 static const int BUTTON_MARGIN = 10;
192
193 // default width and height of the page
194 static const int DEFAULT_PAGE_WIDTH = 270;
195 static const int DEFAULT_PAGE_HEIGHT = 290;
196
197 // create controls
198 // ---------------
199
200 wxSize sizeBtn = wxButton::GetDefaultSize();
201
202 // the global dialog layout is: a row of buttons at the bottom (aligned to
203 // the right), the static line above them, the bitmap (if any) on the left
204 // of the upper part of the dialog and the panel in the remaining space
205 m_x = X_MARGIN;
206 m_y = Y_MARGIN;
207
208 int defaultHeight;
209 if ( m_bitmap.Ok() )
210 {
211 m_statbmp = new wxStaticBitmap(this, -1, m_bitmap, wxPoint(m_x, m_y));
212
213 m_x += m_bitmap.GetWidth() + BITMAP_X_MARGIN;
214
215 defaultHeight = m_bitmap.GetHeight();
216 }
217 else
218 {
219 m_statbmp = (wxStaticBitmap *)NULL;
220
221 defaultHeight = DEFAULT_PAGE_HEIGHT;
222 }
223
224 // use default size if none given and also make sure that the dialog is
225 // not less than the default size
226 m_height = m_sizePage.y == -1 ? defaultHeight : m_sizePage.y;
227 m_width = m_sizePage.x == -1 ? DEFAULT_PAGE_WIDTH : m_sizePage.x;
228 if ( m_height < defaultHeight )
229 m_height = defaultHeight;
230 if ( m_width < DEFAULT_PAGE_WIDTH )
231 m_width = DEFAULT_PAGE_WIDTH;
232
233 int x = X_MARGIN;
234 int y = m_y + m_height + BITMAP_Y_MARGIN;
235
236 #if wxUSE_STATLINE
237 (void)new wxStaticLine(this, -1, wxPoint(x, y),
238 wxSize(m_x + m_width - x, 2));
239 #endif // wxUSE_STATLINE
240
241 x = m_x + m_width - 3*sizeBtn.x - BUTTON_MARGIN;
242 y += SEPARATOR_LINE_MARGIN;
243
244 if (GetExtraStyle() & wxWIZARD_EX_HELPBUTTON)
245 {
246 x -= sizeBtn.x;
247 x -= BUTTON_MARGIN ;
248
249 (void)new wxButton(this, wxID_HELP, _("&Help"), wxPoint(x, y), sizeBtn);
250 x += sizeBtn.x;
251 x += BUTTON_MARGIN ;
252 }
253
254 m_btnPrev = new wxButton(this, wxID_BACKWARD, _("< &Back"), wxPoint(x, y), sizeBtn);
255
256 x += sizeBtn.x;
257 m_btnNext = new wxButton(this, wxID_FORWARD, _("&Next >"), wxPoint(x, y), sizeBtn);
258
259 x += sizeBtn.x + BUTTON_MARGIN;
260 (void)new wxButton(this, wxID_CANCEL, _("&Cancel"), wxPoint(x, y), sizeBtn);
261
262 // position and size the dialog
263 // ----------------------------
264
265 SetClientSize(m_x + m_width + X_MARGIN,
266 m_y + m_height + BITMAP_Y_MARGIN +
267 SEPARATOR_LINE_MARGIN + sizeBtn.y + Y_MARGIN);
268
269 if ( m_posWizard == wxDefaultPosition )
270 {
271 CentreOnScreen();
272 }
273 }
274
275 void wxWizard::SetPageSize(const wxSize& size)
276 {
277 // otherwise it will have no effect now as it's too late...
278 wxASSERT_MSG( !WasCreated(), _T("should be called before RunWizard()!") );
279
280 m_sizePage = size;
281 }
282
283 bool wxWizard::ShowPage(wxWizardPage *page, bool goingForward)
284 {
285 wxASSERT_MSG( page != m_page, wxT("this is useless") );
286
287 // we'll use this to decide whether we have to change the label of this
288 // button or not (initially the label is "Next")
289 bool btnLabelWasNext = TRUE;
290
291 // Modified 10-20-2001 Robert Cavanaugh.
292 // Fixed bug for displaying a new bitmap
293 // in each *consecutive* page
294
295 // flag to indicate if this page uses a new bitmap
296 bool bmpIsDefault = TRUE;
297
298 // use these labels to determine if we need to change the bitmap
299 // for this page
300 wxBitmap PreviousBitmap = wxNullBitmap;
301 wxBitmap ThisBitmap = wxNullBitmap;
302
303 // check for previous page
304 if ( m_page )
305 {
306 // send the event to the old page
307 wxWizardEvent event(wxEVT_WIZARD_PAGE_CHANGING, GetId(), goingForward);
308 if ( m_page->GetEventHandler()->ProcessEvent(event) &&
309 !event.IsAllowed() )
310 {
311 // vetoed by the page
312 return FALSE;
313 }
314
315 m_page->Hide();
316
317 btnLabelWasNext = m_page->GetNext() != (wxWizardPage *)NULL;
318
319 // Get the bitmap of the previous page (if it exists)
320 if(m_page->GetBitmap().Ok())
321 {
322 PreviousBitmap = m_page->GetBitmap();
323 }
324 }
325
326 // set the new page
327 m_page = page;
328
329 // is this the end?
330 if ( !m_page )
331 {
332 // terminate successfully
333 EndModal(wxID_OK);
334 return TRUE;
335 }
336
337 // send the change event to the new page now
338 wxWizardEvent event(wxEVT_WIZARD_PAGE_CHANGED, GetId(), goingForward);
339 (void)m_page->GetEventHandler()->ProcessEvent(event);
340
341 // position and show the new page
342 (void)m_page->TransferDataToWindow();
343 m_page->SetSize(m_x, m_y, m_width, m_height);
344 m_page->Show();
345
346 // check if bitmap needs to be updated
347 // update default flag as well
348 if(m_page->GetBitmap().Ok())
349 {
350 ThisBitmap = m_page->GetBitmap();
351 bmpIsDefault = FALSE;
352 }
353
354 // change the bitmap if:
355 // 1) a default bitmap was selected in constructor
356 // 2) this page was constructed with a bitmap
357 // 3) this bitmap is not the previous bitmap
358 if( m_statbmp && (ThisBitmap != PreviousBitmap) )
359 {
360 wxBitmap bmp;
361 if ( bmpIsDefault )
362 bmp = m_bitmap;
363 else
364 bmp = m_page->GetBitmap();
365 m_statbmp->SetBitmap(bmp);
366 }
367
368 // and update the buttons state
369 m_btnPrev->Enable(m_page->GetPrev() != (wxWizardPage *)NULL);
370
371 bool hasNext = m_page->GetNext() != (wxWizardPage *)NULL;
372 if ( btnLabelWasNext != hasNext )
373 {
374 // need to update
375 if (btnLabelWasNext)
376 m_btnNext->SetLabel(_("&Finish"));
377 else
378 m_btnNext->SetLabel(_("&Next >"));
379 }
380 // nothing to do: the label was already correct
381
382 return TRUE;
383 }
384
385 bool wxWizard::RunWizard(wxWizardPage *firstPage)
386 {
387 wxCHECK_MSG( firstPage, FALSE, wxT("can't run empty wizard") );
388
389 DoCreateControls();
390
391 // can't return FALSE here because there is no old page
392 (void)ShowPage(firstPage, TRUE /* forward */);
393
394 return ShowModal() == wxID_OK;
395 }
396
397 wxWizardPage *wxWizard::GetCurrentPage() const
398 {
399 return m_page;
400 }
401
402 wxSize wxWizard::GetPageSize() const
403 {
404 // make sure that the controls are created because otherwise m_width and
405 // m_height would be both still -1
406 wxConstCast(this, wxWizard)->DoCreateControls();
407
408 return wxSize(m_width, m_height);
409 }
410
411 void wxWizard::OnCancel(wxCommandEvent& WXUNUSED(event))
412 {
413 // this function probably can never be called when we don't have an active
414 // page, but a small extra check won't hurt
415 wxWindow *win = m_page ? (wxWindow *)m_page : (wxWindow *)this;
416
417 wxWizardEvent event(wxEVT_WIZARD_CANCEL, GetId());
418 if ( !win->GetEventHandler()->ProcessEvent(event) || event.IsAllowed() )
419 {
420 // no objections - close the dialog
421 EndModal(wxID_CANCEL);
422 }
423 //else: request to Cancel ignored
424 }
425
426 void wxWizard::OnBackOrNext(wxCommandEvent& event)
427 {
428 wxASSERT_MSG( (event.GetEventObject() == m_btnNext) ||
429 (event.GetEventObject() == m_btnPrev),
430 wxT("unknown button") );
431
432 // ask the current page first: notice that we do it before calling
433 // GetNext/Prev() because the data transfered from the controls of the page
434 // may change the value returned by these methods
435 if ( m_page && !m_page->TransferDataFromWindow() )
436 {
437 // the page data is incorrect, don't do anything
438 return;
439 }
440
441 bool forward = event.GetEventObject() == m_btnNext;
442
443 wxWizardPage *page;
444 if ( forward )
445 {
446 page = m_page->GetNext();
447 }
448 else // back
449 {
450 page = m_page->GetPrev();
451
452 wxASSERT_MSG( page, wxT("\"<Back\" button should have been disabled") );
453 }
454
455 // just pass to the new page (or may be not - but we don't care here)
456 (void)ShowPage(page, forward);
457 }
458
459 void wxWizard::OnHelp(wxCommandEvent& WXUNUSED(event))
460 {
461 // this function probably can never be called when we don't have an active
462 // page, but a small extra check won't hurt
463 if(m_page != NULL)
464 {
465 // Create and send the help event to the specific page handler
466 // event data contains the active page so that context-sensitive
467 // help is possible
468 wxWizardEvent eventHelp(wxEVT_WIZARD_HELP, GetId(), TRUE, m_page);
469 (void)m_page->GetEventHandler()->ProcessEvent(eventHelp);
470 }
471 }
472
473 void wxWizard::OnWizEvent(wxWizardEvent& event)
474 {
475 // the dialogs have wxWS_EX_BLOCK_EVENTS style on by default but we want to
476 // propagate wxEVT_WIZARD_XXX to the parent (if any), so do it manually
477 if ( !(GetExtraStyle() & wxWS_EX_BLOCK_EVENTS) )
478 {
479 // the event will be propagated anyhow
480 return;
481 }
482
483 wxWindow *parent = GetParent();
484
485 if ( !parent || !parent->GetEventHandler()->ProcessEvent(event) )
486 {
487 event.Skip();
488 }
489 }
490
491 // ----------------------------------------------------------------------------
492 // our public interface
493 // ----------------------------------------------------------------------------
494
495 /* static */
496 wxWizard *wxWizardBase::Create(wxWindow *parent,
497 int id,
498 const wxString& title,
499 const wxBitmap& bitmap,
500 const wxPoint& pos,
501 const wxSize& WXUNUSED(size))
502 {
503 return new wxWizard(parent, id, title, bitmap, pos);
504 }
505
506 // ----------------------------------------------------------------------------
507 // wxWizardEvent
508 // ----------------------------------------------------------------------------
509
510 wxWizardEvent::wxWizardEvent(wxEventType type, int id, bool direction, wxWizardPage* page)
511 : wxNotifyEvent(type, id)
512 {
513 // Modified 10-20-2001 Robert Cavanaugh
514 // add the active page to the event data
515 m_direction = direction;
516 m_page = page;
517 }
518
519 #endif // wxUSE_WIZARDDLG