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