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