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