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