]> git.saurik.com Git - wxWidgets.git/blob - src/generic/wizard.cpp
changed char event handling for ctrl-chars and skipped events
[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 ".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 END_EVENT_TABLE()
68
69 IMPLEMENT_DYNAMIC_CLASS(wxWizard, wxDialog)
70 IMPLEMENT_ABSTRACT_CLASS(wxWizardPage, wxPanel)
71 IMPLEMENT_DYNAMIC_CLASS(wxWizardPageSimple, wxWizardPage)
72 IMPLEMENT_DYNAMIC_CLASS(wxWizardEvent, wxNotifyEvent)
73
74 // ============================================================================
75 // implementation
76 // ============================================================================
77
78 // ----------------------------------------------------------------------------
79 // wxWizardPage
80 // ----------------------------------------------------------------------------
81
82 void wxWizardPage::Init()
83 {
84 m_bitmap = wxNullBitmap;
85 }
86
87 wxWizardPage::wxWizardPage(wxWizard *parent,
88 const wxBitmap& bitmap,
89 const wxChar *resource)
90 {
91 Create(parent, bitmap, resource);
92 }
93
94 bool wxWizardPage::Create(wxWizard *parent,
95 const wxBitmap& bitmap,
96 const wxChar *resource)
97 {
98 if ( !wxPanel::Create(parent, -1) )
99 return FALSE;
100
101 if ( resource != NULL )
102 {
103 #if wxUSE_WX_RESOURCES
104 if ( !LoadFromResource(this, resource) )
105 {
106 wxFAIL_MSG(wxT("wxWizardPage LoadFromResource failed!!!!"));
107 }
108 #endif // wxUSE_RESOURCES
109 }
110
111 m_bitmap = bitmap;
112
113 // initially the page is hidden, it's shown only when it becomes current
114 Hide();
115
116 return TRUE;
117 }
118
119 // ----------------------------------------------------------------------------
120 // wxWizardPageSimple
121 // ----------------------------------------------------------------------------
122
123 wxWizardPage *wxWizardPageSimple::GetPrev() const
124 {
125 return m_prev;
126 }
127
128 wxWizardPage *wxWizardPageSimple::GetNext() const
129 {
130 return m_next;
131 }
132 // ----------------------------------------------------------------------------
133 // generic wxWizard implementation
134 // ----------------------------------------------------------------------------
135
136 void wxWizard::Init()
137 {
138 m_posWizard = wxDefaultPosition;
139 m_page = (wxWizardPage *)NULL;
140 m_btnPrev = m_btnNext = NULL;
141 m_statbmp = NULL;
142 }
143
144 bool wxWizard::Create(wxWindow *parent,
145 int id,
146 const wxString& title,
147 const wxBitmap& bitmap,
148 const wxPoint& pos)
149 {
150 m_posWizard = pos;
151 m_bitmap = bitmap ;
152
153 // just create the dialog itself here, the controls will be created in
154 // DoCreateControls() called later when we know our final size
155 m_page = (wxWizardPage *)NULL;
156 m_btnPrev = m_btnNext = NULL;
157 m_statbmp = NULL;
158
159 return wxDialog::Create(parent, id, title, pos);
160 }
161
162 void wxWizard::DoCreateControls()
163 {
164 // do nothing if the controls were already created
165 if ( WasCreated() )
166 return;
167
168 // constants defining the dialog layout
169 // ------------------------------------
170
171 // these constants define the position of the upper left corner of the
172 // bitmap or the page in the wizard
173 static const int X_MARGIN = 10;
174 static const int Y_MARGIN = 10;
175
176 // margin between the bitmap and the panel
177 static const int BITMAP_X_MARGIN = 15;
178
179 // margin between the bitmap and the static line
180 static const int BITMAP_Y_MARGIN = 15;
181
182 // margin between the static line and the buttons
183 static const int SEPARATOR_LINE_MARGIN = 15;
184
185 // margin between "Next >" and "Cancel" buttons
186 static const int BUTTON_MARGIN = 10;
187
188 // default width and height of the page
189 static const int DEFAULT_PAGE_WIDTH = 270;
190 static const int DEFAULT_PAGE_HEIGHT = 290;
191
192 // create controls
193 // ---------------
194
195 wxSize sizeBtn = wxButton::GetDefaultSize();
196
197 // the global dialog layout is: a row of buttons at the bottom (aligned to
198 // the right), the static line above them, the bitmap (if any) on the left
199 // of the upper part of the dialog and the panel in the remaining space
200 m_x = X_MARGIN;
201 m_y = Y_MARGIN;
202
203 int defaultHeight;
204 if ( m_bitmap.Ok() )
205 {
206 m_statbmp = new wxStaticBitmap(this, -1, m_bitmap, wxPoint(m_x, m_y));
207
208 m_x += m_bitmap.GetWidth() + BITMAP_X_MARGIN;
209
210 defaultHeight = m_bitmap.GetHeight();
211 }
212 else
213 {
214 m_statbmp = (wxStaticBitmap *)NULL;
215
216 defaultHeight = DEFAULT_PAGE_HEIGHT;
217 }
218
219 // use default size if none given and also make sure that the dialog is
220 // not less than the default size
221 m_height = m_sizePage.y == -1 ? defaultHeight : m_sizePage.y;
222 m_width = m_sizePage.x == -1 ? DEFAULT_PAGE_WIDTH : m_sizePage.x;
223 if ( m_height < defaultHeight )
224 m_height = defaultHeight;
225 if ( m_width < DEFAULT_PAGE_WIDTH )
226 m_width = DEFAULT_PAGE_WIDTH;
227
228 int x = X_MARGIN;
229 int y = m_y + m_height + BITMAP_Y_MARGIN;
230
231 #if wxUSE_STATLINE
232 (void)new wxStaticLine(this, -1, wxPoint(x, y),
233 wxSize(m_x + m_width - x, 2));
234 #endif // wxUSE_STATLINE
235
236 x = m_x + m_width - 3*sizeBtn.x - BUTTON_MARGIN;
237 y += SEPARATOR_LINE_MARGIN;
238
239 if (GetExtraStyle() & wxWIZARD_EX_HELPBUTTON)
240 {
241 x -= sizeBtn.x;
242 x -= BUTTON_MARGIN ;
243
244 (void)new wxButton(this, wxID_HELP, _("&Help"), wxPoint(x, y), sizeBtn);
245 x += sizeBtn.x;
246 x += BUTTON_MARGIN ;
247 }
248
249 m_btnPrev = new wxButton(this, wxID_BACKWARD, _("< &Back"), wxPoint(x, y), sizeBtn);
250
251 x += sizeBtn.x;
252 m_btnNext = new wxButton(this, wxID_FORWARD, _("&Next >"), wxPoint(x, y), sizeBtn);
253
254 x += sizeBtn.x + BUTTON_MARGIN;
255 (void)new wxButton(this, wxID_CANCEL, _("&Cancel"), wxPoint(x, y), sizeBtn);
256
257 // position and size the dialog
258 // ----------------------------
259
260 SetClientSize(m_x + m_width + X_MARGIN,
261 m_y + m_height + BITMAP_Y_MARGIN +
262 SEPARATOR_LINE_MARGIN + sizeBtn.y + Y_MARGIN);
263
264 if ( m_posWizard == wxDefaultPosition )
265 {
266 CentreOnScreen();
267 }
268 }
269
270 void wxWizard::SetPageSize(const wxSize& size)
271 {
272 // otherwise it will have no effect now as it's too late...
273 wxASSERT_MSG( !WasCreated(), _T("should be called before RunWizard()!") );
274
275 m_sizePage = size;
276 }
277
278 bool wxWizard::ShowPage(wxWizardPage *page, bool goingForward)
279 {
280 wxASSERT_MSG( page != m_page, wxT("this is useless") );
281
282 // we'll use this to decide whether we have to change the label of this
283 // button or not (initially the label is "Next")
284 bool btnLabelWasNext = TRUE;
285
286 // Modified 10-20-2001 Robert Cavanaugh.
287 // Fixed bug for displaying a new bitmap
288 // in each *consecutive* page
289
290 // flag to indicate if this page uses a new bitmap
291 bool bmpIsDefault = TRUE;
292
293 // use these labels to determine if we need to change the bitmap
294 // for this page
295 wxBitmap PreviousBitmap = wxNullBitmap;
296 wxBitmap ThisBitmap = wxNullBitmap;
297
298 // check for previous page
299 if ( m_page )
300 {
301 // send the event to the old page
302 wxWizardEvent event(wxEVT_WIZARD_PAGE_CHANGING, GetId(), goingForward);
303 if ( m_page->GetEventHandler()->ProcessEvent(event) &&
304 !event.IsAllowed() )
305 {
306 // vetoed by the page
307 return FALSE;
308 }
309
310 m_page->Hide();
311
312 btnLabelWasNext = m_page->GetNext() != (wxWizardPage *)NULL;
313
314 // Get the bitmap of the previous page (if it exists)
315 if(m_page->GetBitmap().Ok())
316 {
317 PreviousBitmap = m_page->GetBitmap();
318 }
319 }
320
321 // set the new page
322 m_page = page;
323
324 // is this the end?
325 if ( !m_page )
326 {
327 // terminate successfully
328 EndModal(wxID_OK);
329 return TRUE;
330 }
331
332 // send the change event to the new page now
333 wxWizardEvent event(wxEVT_WIZARD_PAGE_CHANGED, GetId(), goingForward);
334 (void)m_page->GetEventHandler()->ProcessEvent(event);
335
336 // position and show the new page
337 (void)m_page->TransferDataToWindow();
338 m_page->SetSize(m_x, m_y, m_width, m_height);
339 m_page->Show();
340
341 // check if bitmap needs to be updated
342 // update default flag as well
343 if(m_page->GetBitmap().Ok())
344 {
345 ThisBitmap = m_page->GetBitmap();
346 bmpIsDefault = FALSE;
347 }
348
349 // change the bitmap if:
350 // 1) a default bitmap was selected in constructor
351 // 2) this page was constructed with a bitmap
352 // 3) this bitmap is not the previous bitmap
353 if( m_statbmp && (ThisBitmap != PreviousBitmap) )
354 {
355 wxBitmap bmp;
356 if ( bmpIsDefault )
357 bmp = m_bitmap;
358 else
359 bmp = m_page->GetBitmap();
360 m_statbmp->SetBitmap(bmp);
361 }
362
363 // and update the buttons state
364 m_btnPrev->Enable(m_page->GetPrev() != (wxWizardPage *)NULL);
365
366 bool hasNext = m_page->GetNext() != (wxWizardPage *)NULL;
367 if ( btnLabelWasNext != hasNext )
368 {
369 // need to update
370 if (btnLabelWasNext)
371 m_btnNext->SetLabel(_("&Finish"));
372 else
373 m_btnNext->SetLabel(_("&Next >"));
374 }
375 // nothing to do: the label was already correct
376
377 return TRUE;
378 }
379
380 bool wxWizard::RunWizard(wxWizardPage *firstPage)
381 {
382 wxCHECK_MSG( firstPage, FALSE, wxT("can't run empty wizard") );
383
384 DoCreateControls();
385
386 // can't return FALSE here because there is no old page
387 (void)ShowPage(firstPage, TRUE /* forward */);
388
389 return ShowModal() == wxID_OK;
390 }
391
392 wxWizardPage *wxWizard::GetCurrentPage() const
393 {
394 return m_page;
395 }
396
397 wxSize wxWizard::GetPageSize() const
398 {
399 // make sure that the controls are created because otherwise m_width and
400 // m_height would be both still -1
401 wxConstCast(this, wxWizard)->DoCreateControls();
402
403 return wxSize(m_width, m_height);
404 }
405
406 void wxWizard::OnCancel(wxCommandEvent& WXUNUSED(event))
407 {
408 // this function probably can never be called when we don't have an active
409 // page, but a small extra check won't hurt
410 wxWindow *win = m_page ? (wxWindow *)m_page : (wxWindow *)this;
411
412 wxWizardEvent event(wxEVT_WIZARD_CANCEL, GetId());
413 if ( !win->GetEventHandler()->ProcessEvent(event) || event.IsAllowed() )
414 {
415 // no objections - close the dialog
416 EndModal(wxID_CANCEL);
417 }
418 //else: request to Cancel ignored
419 }
420
421 void wxWizard::OnBackOrNext(wxCommandEvent& event)
422 {
423 wxASSERT_MSG( (event.GetEventObject() == m_btnNext) ||
424 (event.GetEventObject() == m_btnPrev),
425 wxT("unknown button") );
426
427 // ask the current page first: notice that we do it before calling
428 // GetNext/Prev() because the data transfered from the controls of the page
429 // may change the value returned by these methods
430 if ( m_page && !m_page->TransferDataFromWindow() )
431 {
432 // the page data is incorrect, don't do anything
433 return;
434 }
435
436 bool forward = event.GetEventObject() == m_btnNext;
437
438 wxWizardPage *page;
439 if ( forward )
440 {
441 page = m_page->GetNext();
442 }
443 else // back
444 {
445 page = m_page->GetPrev();
446
447 wxASSERT_MSG( page, wxT("\"<Back\" button should have been disabled") );
448 }
449
450 // just pass to the new page (or may be not - but we don't care here)
451 (void)ShowPage(page, forward);
452 }
453
454 void wxWizard::OnHelp(wxCommandEvent& WXUNUSED(event))
455 {
456 // this function probably can never be called when we don't have an active
457 // page, but a small extra check won't hurt
458 if(m_page != NULL)
459 {
460 // Create and send the help event to the specific page handler
461 // event data contains the active page so that context-sensitive
462 // help is possible
463 wxWizardEvent eventHelp(wxEVT_WIZARD_HELP, GetId(), TRUE, m_page);
464 (void)m_page->GetEventHandler()->ProcessEvent(eventHelp);
465 }
466 }
467
468
469 // ----------------------------------------------------------------------------
470 // our public interface
471 // ----------------------------------------------------------------------------
472
473 /* static */
474 wxWizard *wxWizardBase::Create(wxWindow *parent,
475 int id,
476 const wxString& title,
477 const wxBitmap& bitmap,
478 const wxPoint& pos,
479 const wxSize& WXUNUSED(size))
480 {
481 return new wxWizard(parent, id, title, bitmap, pos);
482 }
483
484 // ----------------------------------------------------------------------------
485 // wxWizardEvent
486 // ----------------------------------------------------------------------------
487
488 wxWizardEvent::wxWizardEvent(wxEventType type, int id, bool direction, wxWizardPage* page)
489 : wxNotifyEvent(type, id)
490 {
491 // Modified 10-20-2001 Robert Cavanaugh
492 // add the active page to the event data
493 m_direction = direction;
494 m_page = page;
495 }
496
497 #endif // wxUSE_WIZARDDLG