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