]> git.saurik.com Git - wxWidgets.git/blame - src/generic/wizard.cpp
Fixed wxMotif compilo
[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
c7de4135
VS
82void wxWizardPage::Init()
83{
84 m_bitmap = wxNullBitmap;
85}
86
a0a48a3f
VZ
87wxWizardPage::wxWizardPage(wxWizard *parent,
88 const wxBitmap& bitmap,
89 const wxChar *resource)
74b31181 90{
c7de4135
VS
91 Create(parent, bitmap, resource);
92}
93
94bool wxWizardPage::Create(wxWizard *parent,
95 const wxBitmap& bitmap,
96 const wxChar *resource)
97{
98 if ( !wxPanel::Create(parent, -1) )
99 return FALSE;
100
a0a48a3f
VZ
101 if ( resource != NULL )
102 {
5d5da6f6 103#if wxUSE_WX_RESOURCES
a0a48a3f
VZ
104 if ( !LoadFromResource(this, resource) )
105 {
106 wxFAIL_MSG(wxT("wxWizardPage LoadFromResource failed!!!!"));
107 }
c8804076 108#endif // wxUSE_RESOURCES
a0a48a3f
VZ
109 }
110
636d266b 111 m_bitmap = bitmap;
a0a48a3f 112
74b31181
VZ
113 // initially the page is hidden, it's shown only when it becomes current
114 Hide();
c7de4135
VS
115
116 return TRUE;
74b31181
VZ
117}
118
119// ----------------------------------------------------------------------------
120// wxWizardPageSimple
121// ----------------------------------------------------------------------------
122
123wxWizardPage *wxWizardPageSimple::GetPrev() const
124{
125 return m_prev;
126}
127
128wxWizardPage *wxWizardPageSimple::GetNext() const
129{
130 return m_next;
131}
66cd017c
VZ
132// ----------------------------------------------------------------------------
133// generic wxWizard implementation
134// ----------------------------------------------------------------------------
135
77436c4c
JS
136void wxWizard::Init()
137{
138 m_posWizard = wxDefaultPosition;
139 m_page = (wxWizardPage *)NULL;
140 m_btnPrev = m_btnNext = NULL;
141 m_statbmp = NULL;
142}
143
144bool wxWizard::Create(wxWindow *parent,
74b31181
VZ
145 int id,
146 const wxString& title,
147 const wxBitmap& bitmap,
f6bcfd97 148 const wxPoint& pos)
66cd017c 149{
77436c4c
JS
150 m_posWizard = pos;
151 m_bitmap = bitmap ;
636d266b 152
f6bcfd97
BP
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
77436c4c 159 return wxDialog::Create(parent, id, title, pos);
f6bcfd97
BP
160}
161
162void wxWizard::DoCreateControls()
163{
164 // do nothing if the controls were already created
165 if ( WasCreated() )
166 return;
167
66cd017c
VZ
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
66cd017c
VZ
192 // create controls
193 // ---------------
194
195 wxSize sizeBtn = wxButton::GetDefaultSize();
196
66cd017c
VZ
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;
f6bcfd97
BP
202
203 int defaultHeight;
204 if ( m_bitmap.Ok() )
66cd017c 205 {
f6bcfd97 206 m_statbmp = new wxStaticBitmap(this, -1, m_bitmap, wxPoint(m_x, m_y));
66cd017c 207
f6bcfd97
BP
208 m_x += m_bitmap.GetWidth() + BITMAP_X_MARGIN;
209
210 defaultHeight = m_bitmap.GetHeight();
66cd017c
VZ
211 }
212 else
213 {
f1df0927
VZ
214 m_statbmp = (wxStaticBitmap *)NULL;
215
f6bcfd97 216 defaultHeight = DEFAULT_PAGE_HEIGHT;
66cd017c
VZ
217 }
218
f6bcfd97
BP
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;
66cd017c
VZ
227
228 int x = X_MARGIN;
229 int y = m_y + m_height + BITMAP_Y_MARGIN;
74b31181
VZ
230
231#if wxUSE_STATLINE
66cd017c
VZ
232 (void)new wxStaticLine(this, -1, wxPoint(x, y),
233 wxSize(m_x + m_width - x, 2));
f6bcfd97 234#endif // wxUSE_STATLINE
74b31181 235
66cd017c
VZ
236 x = m_x + m_width - 3*sizeBtn.x - BUTTON_MARGIN;
237 y += SEPARATOR_LINE_MARGIN;
77436c4c
JS
238
239 if (GetExtraStyle() & wxWIZARD_EX_HELPBUTTON)
240 {
241 x -= sizeBtn.x;
242 x -= BUTTON_MARGIN ;
243
a8973b12 244 (void)new wxButton(this, wxID_HELP, _("&Help"), wxPoint(x, y), sizeBtn);
77436c4c
JS
245 x += sizeBtn.x;
246 x += BUTTON_MARGIN ;
247 }
248
f6bcfd97 249 m_btnPrev = new wxButton(this, wxID_BACKWARD, _("< &Back"), wxPoint(x, y), sizeBtn);
66cd017c
VZ
250
251 x += sizeBtn.x;
f6bcfd97 252 m_btnNext = new wxButton(this, wxID_FORWARD, _("&Next >"), wxPoint(x, y), sizeBtn);
66cd017c
VZ
253
254 x += sizeBtn.x + BUTTON_MARGIN;
77436c4c 255 (void)new wxButton(this, wxID_CANCEL, _("&Cancel"), wxPoint(x, y), sizeBtn);
66cd017c
VZ
256
257 // position and size the dialog
258 // ----------------------------
259
f6bcfd97
BP
260 SetClientSize(m_x + m_width + X_MARGIN,
261 m_y + m_height + BITMAP_Y_MARGIN +
262 SEPARATOR_LINE_MARGIN + sizeBtn.y + Y_MARGIN);
66cd017c 263
f6bcfd97 264 if ( m_posWizard == wxDefaultPosition )
66cd017c 265 {
91b4c08d 266 CentreOnScreen();
66cd017c
VZ
267 }
268}
269
f6bcfd97
BP
270void 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
74b31181 278bool wxWizard::ShowPage(wxWizardPage *page, bool goingForward)
66cd017c 279{
223d09f6 280 wxASSERT_MSG( page != m_page, wxT("this is useless") );
66cd017c 281
74b31181
VZ
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;
66cd017c 285
7cc5041d
VZ
286 // Modified 10-20-2001 Robert Cavanaugh.
287 // Fixed bug for displaying a new bitmap
288 // in each *consecutive* page
f1df0927 289
7cc5041d
VZ
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
74b31181 299 if ( m_page )
66cd017c 300 {
74b31181
VZ
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
66cd017c 307 return FALSE;
74b31181 308 }
66cd017c 309
74b31181
VZ
310 m_page->Hide();
311
312 btnLabelWasNext = m_page->GetNext() != (wxWizardPage *)NULL;
7cc5041d
VZ
313
314 // Get the bitmap of the previous page (if it exists)
315 if(m_page->GetBitmap().Ok())
dee5b92f 316 {
7cc5041d
VZ
317 PreviousBitmap = m_page->GetBitmap();
318 }
dee5b92f 319 }
66cd017c 320
7cc5041d 321 // set the new page
66cd017c 322 m_page = page;
66cd017c 323
74b31181
VZ
324 // is this the end?
325 if ( !m_page )
66cd017c 326 {
74b31181
VZ
327 // terminate successfully
328 EndModal(wxID_OK);
74b31181
VZ
329 return TRUE;
330 }
66cd017c 331
7cc5041d 332 // send the change event to the new page now
74b31181
VZ
333 wxWizardEvent event(wxEVT_WIZARD_PAGE_CHANGED, GetId(), goingForward);
334 (void)m_page->GetEventHandler()->ProcessEvent(event);
66cd017c 335
74b31181
VZ
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();
66cd017c 340
7cc5041d
VZ
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) )
f1df0927 354 {
cfd88569
VZ
355 wxBitmap bmp;
356 if ( bmpIsDefault )
357 bmp = m_bitmap;
358 else
359 bmp = m_page->GetBitmap();
360 m_statbmp->SetBitmap(bmp);
f1df0927
VZ
361 }
362
74b31181
VZ
363 // and update the buttons state
364 m_btnPrev->Enable(m_page->GetPrev() != (wxWizardPage *)NULL);
66cd017c 365
8f177c8e
VZ
366 bool hasNext = m_page->GetNext() != (wxWizardPage *)NULL;
367 if ( btnLabelWasNext != hasNext )
66cd017c 368 {
74b31181 369 // need to update
e9fa7581
OK
370 if (btnLabelWasNext)
371 m_btnNext->SetLabel(_("&Finish"));
372 else
373 m_btnNext->SetLabel(_("&Next >"));
66cd017c 374 }
74b31181 375 // nothing to do: the label was already correct
66cd017c 376
74b31181 377 return TRUE;
66cd017c
VZ
378}
379
74b31181 380bool wxWizard::RunWizard(wxWizardPage *firstPage)
66cd017c 381{
223d09f6 382 wxCHECK_MSG( firstPage, FALSE, wxT("can't run empty wizard") );
66cd017c 383
f6bcfd97
BP
384 DoCreateControls();
385
66cd017c 386 // can't return FALSE here because there is no old page
74b31181 387 (void)ShowPage(firstPage, TRUE /* forward */);
66cd017c
VZ
388
389 return ShowModal() == wxID_OK;
390}
391
74b31181 392wxWizardPage *wxWizard::GetCurrentPage() const
66cd017c 393{
74b31181 394 return m_page;
66cd017c
VZ
395}
396
4fe5383d
VZ
397wxSize wxWizard::GetPageSize() const
398{
f6bcfd97
BP
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
4fe5383d
VZ
403 return wxSize(m_width, m_height);
404}
405
74b31181 406void wxWizard::OnCancel(wxCommandEvent& WXUNUSED(event))
66cd017c 407{
74b31181
VZ
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
66cd017c 412 wxWizardEvent event(wxEVT_WIZARD_CANCEL, GetId());
74b31181 413 if ( !win->GetEventHandler()->ProcessEvent(event) || event.IsAllowed() )
66cd017c
VZ
414 {
415 // no objections - close the dialog
416 EndModal(wxID_CANCEL);
417 }
418 //else: request to Cancel ignored
419}
420
74b31181 421void wxWizard::OnBackOrNext(wxCommandEvent& event)
66cd017c
VZ
422{
423 wxASSERT_MSG( (event.GetEventObject() == m_btnNext) ||
424 (event.GetEventObject() == m_btnPrev),
223d09f6 425 wxT("unknown button") );
66cd017c 426
f6bcfd97
BP
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
74b31181 436 bool forward = event.GetEventObject() == m_btnNext;
66cd017c 437
74b31181
VZ
438 wxWizardPage *page;
439 if ( forward )
66cd017c 440 {
74b31181 441 page = m_page->GetNext();
66cd017c 442 }
74b31181 443 else // back
66cd017c 444 {
74b31181
VZ
445 page = m_page->GetPrev();
446
223d09f6 447 wxASSERT_MSG( page, wxT("\"<Back\" button should have been disabled") );
66cd017c 448 }
74b31181
VZ
449
450 // just pass to the new page (or may be not - but we don't care here)
451 (void)ShowPage(page, forward);
66cd017c
VZ
452}
453
f80bf901
VZ
454void 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
66cd017c
VZ
469// ----------------------------------------------------------------------------
470// our public interface
471// ----------------------------------------------------------------------------
472
74b31181
VZ
473/* static */
474wxWizard *wxWizardBase::Create(wxWindow *parent,
475 int id,
476 const wxString& title,
477 const wxBitmap& bitmap,
478 const wxPoint& pos,
f6bcfd97 479 const wxSize& WXUNUSED(size))
66cd017c 480{
f6bcfd97 481 return new wxWizard(parent, id, title, bitmap, pos);
66cd017c
VZ
482}
483
484// ----------------------------------------------------------------------------
485// wxWizardEvent
486// ----------------------------------------------------------------------------
487
f80bf901 488wxWizardEvent::wxWizardEvent(wxEventType type, int id, bool direction, wxWizardPage* page)
66cd017c
VZ
489 : wxNotifyEvent(type, id)
490{
f80bf901
VZ
491 // Modified 10-20-2001 Robert Cavanaugh
492 // add the active page to the event data
74b31181 493 m_direction = direction;
f80bf901 494 m_page = page;
66cd017c 495}
74b31181 496
1e6feb95 497#endif // wxUSE_WIZARDDLG