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