]>
Commit | Line | Data |
---|---|---|
1 | /////////////////////////////////////////////////////////////////////////////// | |
2 | // Name: src/generic/wizard.cpp | |
3 | // Purpose: generic implementation of wxWizard class | |
4 | // Author: Vadim Zeitlin | |
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 | |
9 | // Robert Vazan (sizers) | |
10 | // Created: 15.08.99 | |
11 | // RCS-ID: $Id$ | |
12 | // Copyright: (c) 1999 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr> | |
13 | // Licence: wxWindows licence | |
14 | /////////////////////////////////////////////////////////////////////////////// | |
15 | ||
16 | // ============================================================================ | |
17 | // declarations | |
18 | // ============================================================================ | |
19 | ||
20 | // ---------------------------------------------------------------------------- | |
21 | // headers | |
22 | // ---------------------------------------------------------------------------- | |
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 | #include "wx/settings.h" | |
39 | #include "wx/sizer.h" | |
40 | #endif //WX_PRECOMP | |
41 | ||
42 | #include "wx/statline.h" | |
43 | ||
44 | #include "wx/scrolwin.h" | |
45 | #include "wx/wizard.h" | |
46 | #include "wx/dcmemory.h" | |
47 | ||
48 | // ---------------------------------------------------------------------------- | |
49 | // wxWizardSizer | |
50 | // ---------------------------------------------------------------------------- | |
51 | ||
52 | class wxWizardSizer : public wxSizer | |
53 | { | |
54 | public: | |
55 | wxWizardSizer(wxWizard *owner); | |
56 | ||
57 | virtual wxSizerItem *Insert(size_t index, wxSizerItem *item); | |
58 | ||
59 | virtual void RecalcSizes(); | |
60 | virtual wxSize CalcMin(); | |
61 | ||
62 | // get the max size of all wizard pages | |
63 | wxSize GetMaxChildSize(); | |
64 | ||
65 | // return the border which can be either set using wxWizard::SetBorder() or | |
66 | // have default value | |
67 | int GetBorder() const; | |
68 | ||
69 | // hide the pages which we temporarily "show" when they're added to this | |
70 | // sizer (see Insert()) | |
71 | void HidePages(); | |
72 | ||
73 | private: | |
74 | wxSize SiblingSize(wxSizerItem *child); | |
75 | ||
76 | wxWizard *m_owner; | |
77 | wxSize m_childSize; | |
78 | }; | |
79 | ||
80 | // ---------------------------------------------------------------------------- | |
81 | // event tables and such | |
82 | // ---------------------------------------------------------------------------- | |
83 | ||
84 | wxDEFINE_EVENT( wxEVT_WIZARD_PAGE_CHANGED, wxWizardEvent ); | |
85 | wxDEFINE_EVENT( wxEVT_WIZARD_PAGE_CHANGING, wxWizardEvent ); | |
86 | wxDEFINE_EVENT( wxEVT_WIZARD_CANCEL, wxWizardEvent ); | |
87 | wxDEFINE_EVENT( wxEVT_WIZARD_FINISHED, wxWizardEvent ); | |
88 | wxDEFINE_EVENT( wxEVT_WIZARD_HELP, wxWizardEvent ); | |
89 | ||
90 | BEGIN_EVENT_TABLE(wxWizard, wxDialog) | |
91 | EVT_BUTTON(wxID_CANCEL, wxWizard::OnCancel) | |
92 | EVT_BUTTON(wxID_BACKWARD, wxWizard::OnBackOrNext) | |
93 | EVT_BUTTON(wxID_FORWARD, wxWizard::OnBackOrNext) | |
94 | EVT_BUTTON(wxID_HELP, wxWizard::OnHelp) | |
95 | ||
96 | EVT_WIZARD_PAGE_CHANGED(wxID_ANY, wxWizard::OnWizEvent) | |
97 | EVT_WIZARD_PAGE_CHANGING(wxID_ANY, wxWizard::OnWizEvent) | |
98 | EVT_WIZARD_CANCEL(wxID_ANY, wxWizard::OnWizEvent) | |
99 | EVT_WIZARD_FINISHED(wxID_ANY, wxWizard::OnWizEvent) | |
100 | EVT_WIZARD_HELP(wxID_ANY, wxWizard::OnWizEvent) | |
101 | END_EVENT_TABLE() | |
102 | ||
103 | IMPLEMENT_DYNAMIC_CLASS(wxWizard, wxDialog) | |
104 | ||
105 | /* | |
106 | TODO PROPERTIES : | |
107 | wxWizard | |
108 | extstyle | |
109 | title | |
110 | */ | |
111 | ||
112 | IMPLEMENT_ABSTRACT_CLASS(wxWizardPage, wxPanel) | |
113 | IMPLEMENT_DYNAMIC_CLASS(wxWizardPageSimple, wxWizardPage) | |
114 | IMPLEMENT_DYNAMIC_CLASS(wxWizardEvent, wxNotifyEvent) | |
115 | ||
116 | // ============================================================================ | |
117 | // implementation | |
118 | // ============================================================================ | |
119 | ||
120 | // ---------------------------------------------------------------------------- | |
121 | // wxWizardPage | |
122 | // ---------------------------------------------------------------------------- | |
123 | ||
124 | void wxWizardPage::Init() | |
125 | { | |
126 | m_bitmap = wxNullBitmap; | |
127 | } | |
128 | ||
129 | wxWizardPage::wxWizardPage(wxWizard *parent, | |
130 | const wxBitmap& bitmap) | |
131 | { | |
132 | Create(parent, bitmap); | |
133 | } | |
134 | ||
135 | bool wxWizardPage::Create(wxWizard *parent, | |
136 | const wxBitmap& bitmap) | |
137 | { | |
138 | if ( !wxPanel::Create(parent, wxID_ANY) ) | |
139 | return false; | |
140 | ||
141 | m_bitmap = bitmap; | |
142 | ||
143 | // initially the page is hidden, it's shown only when it becomes current | |
144 | Hide(); | |
145 | ||
146 | return true; | |
147 | } | |
148 | ||
149 | // ---------------------------------------------------------------------------- | |
150 | // wxWizardPageSimple | |
151 | // ---------------------------------------------------------------------------- | |
152 | ||
153 | wxWizardPage *wxWizardPageSimple::GetPrev() const | |
154 | { | |
155 | return m_prev; | |
156 | } | |
157 | ||
158 | wxWizardPage *wxWizardPageSimple::GetNext() const | |
159 | { | |
160 | return m_next; | |
161 | } | |
162 | ||
163 | // ---------------------------------------------------------------------------- | |
164 | // wxWizardSizer | |
165 | // ---------------------------------------------------------------------------- | |
166 | ||
167 | wxWizardSizer::wxWizardSizer(wxWizard *owner) | |
168 | : m_owner(owner), | |
169 | m_childSize(wxDefaultSize) | |
170 | { | |
171 | } | |
172 | ||
173 | wxSizerItem *wxWizardSizer::Insert(size_t index, wxSizerItem *item) | |
174 | { | |
175 | m_owner->m_usingSizer = true; | |
176 | ||
177 | if ( item->IsWindow() ) | |
178 | { | |
179 | // we must pretend that the window is shown as otherwise it wouldn't be | |
180 | // taken into account for the layout -- but avoid really showing it, so | |
181 | // just set the internal flag instead of calling wxWindow::Show() | |
182 | item->GetWindow()->wxWindowBase::Show(); | |
183 | } | |
184 | ||
185 | return wxSizer::Insert(index, item); | |
186 | } | |
187 | ||
188 | void wxWizardSizer::HidePages() | |
189 | { | |
190 | for ( wxSizerItemList::compatibility_iterator node = GetChildren().GetFirst(); | |
191 | node; | |
192 | node = node->GetNext() ) | |
193 | { | |
194 | wxSizerItem * const item = node->GetData(); | |
195 | if ( item->IsWindow() ) | |
196 | item->GetWindow()->wxWindowBase::Show(false); | |
197 | } | |
198 | } | |
199 | ||
200 | void wxWizardSizer::RecalcSizes() | |
201 | { | |
202 | // Effect of this function depends on m_owner->m_page and | |
203 | // it should be called whenever it changes (wxWizard::ShowPage) | |
204 | if ( m_owner->m_page ) | |
205 | { | |
206 | m_owner->m_page->SetSize(wxRect(m_position, m_size)); | |
207 | } | |
208 | } | |
209 | ||
210 | wxSize wxWizardSizer::CalcMin() | |
211 | { | |
212 | return m_owner->GetPageSize(); | |
213 | } | |
214 | ||
215 | wxSize wxWizardSizer::GetMaxChildSize() | |
216 | { | |
217 | wxSize maxOfMin; | |
218 | ||
219 | for ( wxSizerItemList::compatibility_iterator childNode = m_children.GetFirst(); | |
220 | childNode; | |
221 | childNode = childNode->GetNext() ) | |
222 | { | |
223 | wxSizerItem *child = childNode->GetData(); | |
224 | maxOfMin.IncTo(child->CalcMin()); | |
225 | maxOfMin.IncTo(SiblingSize(child)); | |
226 | } | |
227 | ||
228 | if ( m_owner->m_started ) | |
229 | { | |
230 | m_childSize = maxOfMin; | |
231 | } | |
232 | ||
233 | return maxOfMin; | |
234 | } | |
235 | ||
236 | int wxWizardSizer::GetBorder() const | |
237 | { | |
238 | return m_owner->m_border; | |
239 | } | |
240 | ||
241 | wxSize wxWizardSizer::SiblingSize(wxSizerItem *child) | |
242 | { | |
243 | wxSize maxSibling; | |
244 | ||
245 | if ( child->IsWindow() ) | |
246 | { | |
247 | wxWizardPage *page = wxDynamicCast(child->GetWindow(), wxWizardPage); | |
248 | if ( page ) | |
249 | { | |
250 | for ( wxWizardPage *sibling = page->GetNext(); | |
251 | sibling; | |
252 | sibling = sibling->GetNext() ) | |
253 | { | |
254 | if ( sibling->GetSizer() ) | |
255 | { | |
256 | maxSibling.IncTo(sibling->GetSizer()->CalcMin()); | |
257 | } | |
258 | } | |
259 | } | |
260 | } | |
261 | ||
262 | return maxSibling; | |
263 | } | |
264 | ||
265 | // ---------------------------------------------------------------------------- | |
266 | // generic wxWizard implementation | |
267 | // ---------------------------------------------------------------------------- | |
268 | ||
269 | void wxWizard::Init() | |
270 | { | |
271 | m_posWizard = wxDefaultPosition; | |
272 | m_page = NULL; | |
273 | m_btnPrev = m_btnNext = NULL; | |
274 | m_statbmp = NULL; | |
275 | m_sizerBmpAndPage = NULL; | |
276 | m_sizerPage = NULL; | |
277 | m_border = 5; | |
278 | m_started = false; | |
279 | m_wasModal = false; | |
280 | m_usingSizer = false; | |
281 | m_bitmapBackgroundColour = *wxWHITE; | |
282 | m_bitmapPlacement = 0; | |
283 | m_bitmapMinimumWidth = 115; | |
284 | } | |
285 | ||
286 | bool wxWizard::Create(wxWindow *parent, | |
287 | int id, | |
288 | const wxString& title, | |
289 | const wxBitmap& bitmap, | |
290 | const wxPoint& pos, | |
291 | long style) | |
292 | { | |
293 | bool result = wxDialog::Create(parent,id,title,pos,wxDefaultSize,style); | |
294 | ||
295 | m_posWizard = pos; | |
296 | m_bitmap = bitmap ; | |
297 | ||
298 | DoCreateControls(); | |
299 | ||
300 | return result; | |
301 | } | |
302 | ||
303 | wxWizard::~wxWizard() | |
304 | { | |
305 | // normally we don't have to delete this sizer as it's deleted by the | |
306 | // associated window but if we never used it or didn't set it as the window | |
307 | // sizer yet, do delete it manually | |
308 | if ( !m_usingSizer || !m_started ) | |
309 | delete m_sizerPage; | |
310 | } | |
311 | ||
312 | void wxWizard::AddBitmapRow(wxBoxSizer *mainColumn) | |
313 | { | |
314 | m_sizerBmpAndPage = new wxBoxSizer(wxHORIZONTAL); | |
315 | mainColumn->Add( | |
316 | m_sizerBmpAndPage, | |
317 | 1, // Vertically stretchable | |
318 | wxEXPAND // Horizonal stretching, no border | |
319 | ); | |
320 | mainColumn->Add(0,5, | |
321 | 0, // No vertical stretching | |
322 | wxEXPAND // No border, (mostly useless) horizontal stretching | |
323 | ); | |
324 | ||
325 | #if wxUSE_STATBMP | |
326 | if ( m_bitmap.Ok() ) | |
327 | { | |
328 | wxSize bitmapSize(wxDefaultSize); | |
329 | if (GetBitmapPlacement()) | |
330 | bitmapSize.x = GetMinimumBitmapWidth(); | |
331 | ||
332 | m_statbmp = new wxStaticBitmap(this, wxID_ANY, m_bitmap, wxDefaultPosition, bitmapSize); | |
333 | m_sizerBmpAndPage->Add( | |
334 | m_statbmp, | |
335 | 0, // No horizontal stretching | |
336 | wxALL, // Border all around, top alignment | |
337 | 5 // Border width | |
338 | ); | |
339 | m_sizerBmpAndPage->Add( | |
340 | 5,0, | |
341 | 0, // No horizontal stretching | |
342 | wxEXPAND // No border, (mostly useless) vertical stretching | |
343 | ); | |
344 | } | |
345 | #endif | |
346 | ||
347 | // Added to m_sizerBmpAndPage later | |
348 | m_sizerPage = new wxWizardSizer(this); | |
349 | } | |
350 | ||
351 | void wxWizard::AddStaticLine(wxBoxSizer *mainColumn) | |
352 | { | |
353 | #if wxUSE_STATLINE | |
354 | mainColumn->Add( | |
355 | new wxStaticLine(this, wxID_ANY), | |
356 | 0, // Vertically unstretchable | |
357 | wxEXPAND | wxALL, // Border all around, horizontally stretchable | |
358 | 5 // Border width | |
359 | ); | |
360 | mainColumn->Add(0,5, | |
361 | 0, // No vertical stretching | |
362 | wxEXPAND // No border, (mostly useless) horizontal stretching | |
363 | ); | |
364 | #else | |
365 | (void)mainColumn; | |
366 | #endif // wxUSE_STATLINE | |
367 | } | |
368 | ||
369 | void wxWizard::AddBackNextPair(wxBoxSizer *buttonRow) | |
370 | { | |
371 | wxASSERT_MSG( m_btnNext && m_btnPrev, | |
372 | wxT("You must create the buttons before calling ") | |
373 | wxT("wxWizard::AddBackNextPair") ); | |
374 | ||
375 | // margin between Back and Next buttons | |
376 | #ifdef __WXMAC__ | |
377 | static const int BACKNEXT_MARGIN = 10; | |
378 | #else | |
379 | static const int BACKNEXT_MARGIN = 0; | |
380 | #endif | |
381 | ||
382 | wxBoxSizer *backNextPair = new wxBoxSizer(wxHORIZONTAL); | |
383 | buttonRow->Add( | |
384 | backNextPair, | |
385 | 0, // No horizontal stretching | |
386 | wxALL, // Border all around | |
387 | 5 // Border width | |
388 | ); | |
389 | ||
390 | backNextPair->Add(m_btnPrev); | |
391 | backNextPair->Add(BACKNEXT_MARGIN,0, | |
392 | 0, // No horizontal stretching | |
393 | wxEXPAND // No border, (mostly useless) vertical stretching | |
394 | ); | |
395 | backNextPair->Add(m_btnNext); | |
396 | } | |
397 | ||
398 | void wxWizard::AddButtonRow(wxBoxSizer *mainColumn) | |
399 | { | |
400 | // the order in which the buttons are created determines the TAB order - at least under MSWindows... | |
401 | // although the 'back' button appears before the 'next' button, a more userfriendly tab order is | |
402 | // to activate the 'next' button first (create the next button before the back button). | |
403 | // The reason is: The user will repeatedly enter information in the wizard pages and then wants to | |
404 | // press 'next'. If a user uses mostly the keyboard, he would have to skip the 'back' button | |
405 | // everytime. This is annoying. There is a second reason: RETURN acts as TAB. If the 'next' | |
406 | // button comes first in the TAB order, the user can enter information very fast using the RETURN | |
407 | // key to TAB to the next entry field and page. This would not be possible, if the 'back' button | |
408 | // was created before the 'next' button. | |
409 | ||
410 | bool isPda = (wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA); | |
411 | int buttonStyle = isPda ? wxBU_EXACTFIT : 0; | |
412 | ||
413 | wxBoxSizer *buttonRow = new wxBoxSizer(wxHORIZONTAL); | |
414 | #ifdef __WXMAC__ | |
415 | if (GetExtraStyle() & wxWIZARD_EX_HELPBUTTON) | |
416 | mainColumn->Add( | |
417 | buttonRow, | |
418 | 0, // Vertically unstretchable | |
419 | wxGROW|wxALIGN_CENTRE | |
420 | ); | |
421 | else | |
422 | #endif | |
423 | mainColumn->Add( | |
424 | buttonRow, | |
425 | 0, // Vertically unstretchable | |
426 | wxALIGN_RIGHT // Right aligned, no border | |
427 | ); | |
428 | ||
429 | // Desired TAB order is 'next', 'cancel', 'help', 'back'. This makes the 'back' button the last control on the page. | |
430 | // Create the buttons in the right order... | |
431 | wxButton *btnHelp=0; | |
432 | #ifdef __WXMAC__ | |
433 | if (GetExtraStyle() & wxWIZARD_EX_HELPBUTTON) | |
434 | btnHelp=new wxButton(this, wxID_HELP, _("&Help"), wxDefaultPosition, wxDefaultSize, buttonStyle); | |
435 | #endif | |
436 | ||
437 | m_btnNext = new wxButton(this, wxID_FORWARD, _("&Next >")); | |
438 | wxButton *btnCancel=new wxButton(this, wxID_CANCEL, _("&Cancel"), wxDefaultPosition, wxDefaultSize, buttonStyle); | |
439 | #ifndef __WXMAC__ | |
440 | if (GetExtraStyle() & wxWIZARD_EX_HELPBUTTON) | |
441 | btnHelp=new wxButton(this, wxID_HELP, _("&Help"), wxDefaultPosition, wxDefaultSize, buttonStyle); | |
442 | #endif | |
443 | m_btnPrev = new wxButton(this, wxID_BACKWARD, _("< &Back"), wxDefaultPosition, wxDefaultSize, buttonStyle); | |
444 | ||
445 | if (btnHelp) | |
446 | { | |
447 | buttonRow->Add( | |
448 | btnHelp, | |
449 | 0, // Horizontally unstretchable | |
450 | wxALL, // Border all around, top aligned | |
451 | 5 // Border width | |
452 | ); | |
453 | #ifdef __WXMAC__ | |
454 | // Put stretchable space between help button and others | |
455 | buttonRow->Add(0, 0, 1, wxALIGN_CENTRE, 0); | |
456 | #endif | |
457 | } | |
458 | ||
459 | AddBackNextPair(buttonRow); | |
460 | ||
461 | buttonRow->Add( | |
462 | btnCancel, | |
463 | 0, // Horizontally unstretchable | |
464 | wxALL, // Border all around, top aligned | |
465 | 5 // Border width | |
466 | ); | |
467 | } | |
468 | ||
469 | void wxWizard::DoCreateControls() | |
470 | { | |
471 | // do nothing if the controls were already created | |
472 | if ( WasCreated() ) | |
473 | return; | |
474 | ||
475 | bool isPda = (wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA); | |
476 | ||
477 | // Horizontal stretching, and if not PDA, border all around | |
478 | int mainColumnSizerFlags = isPda ? wxEXPAND : wxALL|wxEXPAND ; | |
479 | ||
480 | // wxWindow::SetSizer will be called at end | |
481 | wxBoxSizer *windowSizer = new wxBoxSizer(wxVERTICAL); | |
482 | ||
483 | wxBoxSizer *mainColumn = new wxBoxSizer(wxVERTICAL); | |
484 | windowSizer->Add( | |
485 | mainColumn, | |
486 | 1, // Vertical stretching | |
487 | mainColumnSizerFlags, | |
488 | 5 // Border width | |
489 | ); | |
490 | ||
491 | AddBitmapRow(mainColumn); | |
492 | ||
493 | if (!isPda) | |
494 | AddStaticLine(mainColumn); | |
495 | ||
496 | AddButtonRow(mainColumn); | |
497 | ||
498 | SetSizer(windowSizer); | |
499 | } | |
500 | ||
501 | void wxWizard::SetPageSize(const wxSize& size) | |
502 | { | |
503 | wxCHECK_RET(!m_started, wxT("wxWizard::SetPageSize after RunWizard")); | |
504 | m_sizePage = size; | |
505 | } | |
506 | ||
507 | void wxWizard::FitToPage(const wxWizardPage *page) | |
508 | { | |
509 | wxCHECK_RET(!m_started, wxT("wxWizard::FitToPage after RunWizard")); | |
510 | ||
511 | while ( page ) | |
512 | { | |
513 | wxSize size = page->GetBestSize(); | |
514 | ||
515 | m_sizePage.IncTo(size); | |
516 | ||
517 | page = page->GetNext(); | |
518 | } | |
519 | } | |
520 | ||
521 | bool wxWizard::ShowPage(wxWizardPage *page, bool goingForward) | |
522 | { | |
523 | wxASSERT_MSG( page != m_page, wxT("this is useless") ); | |
524 | ||
525 | wxSizerFlags flags(1); | |
526 | flags.Border(wxALL, m_border).Expand(); | |
527 | ||
528 | if ( !m_started ) | |
529 | { | |
530 | if ( m_usingSizer ) | |
531 | { | |
532 | m_sizerBmpAndPage->Add(m_sizerPage, flags); | |
533 | ||
534 | // now that our layout is computed correctly, hide the pages | |
535 | // artificially shown in wxWizardSizer::Insert() back again | |
536 | m_sizerPage->HidePages(); | |
537 | } | |
538 | } | |
539 | ||
540 | ||
541 | // remember the old bitmap (if any) to compare with the new one later | |
542 | wxBitmap bmpPrev; | |
543 | ||
544 | // check for previous page | |
545 | if ( m_page ) | |
546 | { | |
547 | // send the event to the old page | |
548 | wxWizardEvent event(wxEVT_WIZARD_PAGE_CHANGING, GetId(), | |
549 | goingForward, m_page); | |
550 | if ( m_page->GetEventHandler()->ProcessEvent(event) && | |
551 | !event.IsAllowed() ) | |
552 | { | |
553 | // vetoed by the page | |
554 | return false; | |
555 | } | |
556 | ||
557 | m_page->Hide(); | |
558 | ||
559 | bmpPrev = m_page->GetBitmap(); | |
560 | ||
561 | if ( !m_usingSizer ) | |
562 | m_sizerBmpAndPage->Detach(m_page); | |
563 | } | |
564 | ||
565 | // set the new page | |
566 | m_page = page; | |
567 | ||
568 | // is this the end? | |
569 | if ( !m_page ) | |
570 | { | |
571 | // terminate successfully | |
572 | if ( IsModal() ) | |
573 | { | |
574 | EndModal(wxID_OK); | |
575 | } | |
576 | else | |
577 | { | |
578 | SetReturnCode(wxID_OK); | |
579 | Hide(); | |
580 | } | |
581 | ||
582 | // and notify the user code (this is especially useful for modeless | |
583 | // wizards) | |
584 | wxWizardEvent event(wxEVT_WIZARD_FINISHED, GetId(), false, 0); | |
585 | (void)GetEventHandler()->ProcessEvent(event); | |
586 | ||
587 | return true; | |
588 | } | |
589 | ||
590 | // position and show the new page | |
591 | (void)m_page->TransferDataToWindow(); | |
592 | ||
593 | if ( m_usingSizer ) | |
594 | { | |
595 | // wxWizardSizer::RecalcSizes wants to be called when m_page changes | |
596 | m_sizerPage->RecalcSizes(); | |
597 | } | |
598 | else // pages are not managed by the sizer | |
599 | { | |
600 | m_sizerBmpAndPage->Add(m_page, flags); | |
601 | m_sizerBmpAndPage->SetItemMinSize(m_page, GetPageSize()); | |
602 | } | |
603 | ||
604 | #if wxUSE_STATBMP | |
605 | // update the bitmap if:it changed | |
606 | wxBitmap bmp; | |
607 | if ( m_statbmp ) | |
608 | { | |
609 | bmp = m_page->GetBitmap(); | |
610 | if ( !bmp.Ok() ) | |
611 | bmp = m_bitmap; | |
612 | ||
613 | if ( !bmpPrev.Ok() ) | |
614 | bmpPrev = m_bitmap; | |
615 | ||
616 | if (!GetBitmapPlacement()) | |
617 | { | |
618 | if ( !bmp.IsSameAs(bmpPrev) ) | |
619 | m_statbmp->SetBitmap(bmp); | |
620 | } | |
621 | } | |
622 | #endif // wxUSE_STATBMP | |
623 | ||
624 | ||
625 | // and update the buttons state | |
626 | m_btnPrev->Enable(HasPrevPage(m_page)); | |
627 | ||
628 | const bool hasNext = HasNextPage(m_page); | |
629 | const wxString label = hasNext ? _("&Next >") : _("&Finish"); | |
630 | if ( label != m_btnNext->GetLabel() ) | |
631 | m_btnNext->SetLabel(label); | |
632 | ||
633 | m_btnNext->SetDefault(); | |
634 | ||
635 | ||
636 | // send the change event to the new page now | |
637 | wxWizardEvent event(wxEVT_WIZARD_PAGE_CHANGED, GetId(), goingForward, m_page); | |
638 | (void)m_page->GetEventHandler()->ProcessEvent(event); | |
639 | ||
640 | // and finally show it | |
641 | m_page->Show(); | |
642 | m_page->SetFocus(); | |
643 | ||
644 | if ( !m_usingSizer ) | |
645 | m_sizerBmpAndPage->Layout(); | |
646 | ||
647 | if ( !m_started ) | |
648 | { | |
649 | m_started = true; | |
650 | ||
651 | DoWizardLayout(); | |
652 | } | |
653 | ||
654 | if (GetBitmapPlacement() && m_statbmp) | |
655 | { | |
656 | ResizeBitmap(bmp); | |
657 | ||
658 | if ( !bmp.IsSameAs(bmpPrev) ) | |
659 | m_statbmp->SetBitmap(bmp); | |
660 | ||
661 | if (m_usingSizer) | |
662 | m_sizerPage->RecalcSizes(); | |
663 | } | |
664 | ||
665 | return true; | |
666 | } | |
667 | ||
668 | /// Do fit, and adjust to screen size if necessary | |
669 | void wxWizard::DoWizardLayout() | |
670 | { | |
671 | if ( wxSystemSettings::GetScreenType() > wxSYS_SCREEN_PDA ) | |
672 | { | |
673 | if (CanDoLayoutAdaptation()) | |
674 | DoLayoutAdaptation(); | |
675 | else | |
676 | GetSizer()->SetSizeHints(this); | |
677 | ||
678 | if ( m_posWizard == wxDefaultPosition ) | |
679 | CentreOnScreen(); | |
680 | } | |
681 | ||
682 | SetLayoutAdaptationDone(true); | |
683 | } | |
684 | ||
685 | bool wxWizard::RunWizard(wxWizardPage *firstPage) | |
686 | { | |
687 | wxCHECK_MSG( firstPage, false, wxT("can't run empty wizard") ); | |
688 | ||
689 | // can't return false here because there is no old page | |
690 | (void)ShowPage(firstPage, true /* forward */); | |
691 | ||
692 | m_wasModal = true; | |
693 | ||
694 | return ShowModal() == wxID_OK; | |
695 | } | |
696 | ||
697 | wxWizardPage *wxWizard::GetCurrentPage() const | |
698 | { | |
699 | return m_page; | |
700 | } | |
701 | ||
702 | wxSize wxWizard::GetPageSize() const | |
703 | { | |
704 | // default width and height of the page | |
705 | int DEFAULT_PAGE_WIDTH, | |
706 | DEFAULT_PAGE_HEIGHT; | |
707 | if ( wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA ) | |
708 | { | |
709 | // Make the default page size small enough to fit on screen | |
710 | DEFAULT_PAGE_WIDTH = wxSystemSettings::GetMetric(wxSYS_SCREEN_X) / 2; | |
711 | DEFAULT_PAGE_HEIGHT = wxSystemSettings::GetMetric(wxSYS_SCREEN_Y) / 2; | |
712 | } | |
713 | else // !PDA | |
714 | { | |
715 | DEFAULT_PAGE_WIDTH = | |
716 | DEFAULT_PAGE_HEIGHT = 270; | |
717 | } | |
718 | ||
719 | // start with default minimal size | |
720 | wxSize pageSize(DEFAULT_PAGE_WIDTH, DEFAULT_PAGE_HEIGHT); | |
721 | ||
722 | // make the page at least as big as specified by user | |
723 | pageSize.IncTo(m_sizePage); | |
724 | ||
725 | if ( m_statbmp ) | |
726 | { | |
727 | // make the page at least as tall as the bitmap | |
728 | pageSize.IncTo(wxSize(0, m_bitmap.GetHeight())); | |
729 | } | |
730 | ||
731 | if ( m_usingSizer ) | |
732 | { | |
733 | // make it big enough to contain all pages added to the sizer | |
734 | pageSize.IncTo(m_sizerPage->GetMaxChildSize()); | |
735 | } | |
736 | ||
737 | return pageSize; | |
738 | } | |
739 | ||
740 | wxSizer *wxWizard::GetPageAreaSizer() const | |
741 | { | |
742 | return m_sizerPage; | |
743 | } | |
744 | ||
745 | void wxWizard::SetBorder(int border) | |
746 | { | |
747 | wxCHECK_RET(!m_started, wxT("wxWizard::SetBorder after RunWizard")); | |
748 | ||
749 | m_border = border; | |
750 | } | |
751 | ||
752 | void wxWizard::OnCancel(wxCommandEvent& WXUNUSED(eventUnused)) | |
753 | { | |
754 | // this function probably can never be called when we don't have an active | |
755 | // page, but a small extra check won't hurt | |
756 | wxWindow *win = m_page ? (wxWindow *)m_page : (wxWindow *)this; | |
757 | ||
758 | wxWizardEvent event(wxEVT_WIZARD_CANCEL, GetId(), false, m_page); | |
759 | if ( !win->GetEventHandler()->ProcessEvent(event) || event.IsAllowed() ) | |
760 | { | |
761 | // no objections - close the dialog | |
762 | if(IsModal()) | |
763 | { | |
764 | EndModal(wxID_CANCEL); | |
765 | } | |
766 | else | |
767 | { | |
768 | SetReturnCode(wxID_CANCEL); | |
769 | Hide(); | |
770 | } | |
771 | } | |
772 | //else: request to Cancel ignored | |
773 | } | |
774 | ||
775 | void wxWizard::OnBackOrNext(wxCommandEvent& event) | |
776 | { | |
777 | wxASSERT_MSG( (event.GetEventObject() == m_btnNext) || | |
778 | (event.GetEventObject() == m_btnPrev), | |
779 | wxT("unknown button") ); | |
780 | ||
781 | wxCHECK_RET( m_page, wxT("should have a valid current page") ); | |
782 | ||
783 | // ask the current page first: notice that we do it before calling | |
784 | // GetNext/Prev() because the data transfered from the controls of the page | |
785 | // may change the value returned by these methods | |
786 | if ( !m_page->Validate() || !m_page->TransferDataFromWindow() ) | |
787 | { | |
788 | // the page data is incorrect, don't do anything | |
789 | return; | |
790 | } | |
791 | ||
792 | bool forward = event.GetEventObject() == m_btnNext; | |
793 | ||
794 | wxWizardPage *page; | |
795 | if ( forward ) | |
796 | { | |
797 | page = m_page->GetNext(); | |
798 | } | |
799 | else // back | |
800 | { | |
801 | page = m_page->GetPrev(); | |
802 | ||
803 | wxASSERT_MSG( page, wxT("\"<Back\" button should have been disabled") ); | |
804 | } | |
805 | ||
806 | // just pass to the new page (or maybe not - but we don't care here) | |
807 | (void)ShowPage(page, forward); | |
808 | } | |
809 | ||
810 | void wxWizard::OnHelp(wxCommandEvent& WXUNUSED(event)) | |
811 | { | |
812 | // this function probably can never be called when we don't have an active | |
813 | // page, but a small extra check won't hurt | |
814 | if(m_page != NULL) | |
815 | { | |
816 | // Create and send the help event to the specific page handler | |
817 | // event data contains the active page so that context-sensitive | |
818 | // help is possible | |
819 | wxWizardEvent eventHelp(wxEVT_WIZARD_HELP, GetId(), true, m_page); | |
820 | (void)m_page->GetEventHandler()->ProcessEvent(eventHelp); | |
821 | } | |
822 | } | |
823 | ||
824 | void wxWizard::OnWizEvent(wxWizardEvent& event) | |
825 | { | |
826 | // the dialogs have wxWS_EX_BLOCK_EVENTS style on by default but we want to | |
827 | // propagate wxEVT_WIZARD_XXX to the parent (if any), so do it manually | |
828 | if ( !(GetExtraStyle() & wxWS_EX_BLOCK_EVENTS) ) | |
829 | { | |
830 | // the event will be propagated anyhow | |
831 | event.Skip(); | |
832 | } | |
833 | else | |
834 | { | |
835 | wxWindow *parent = GetParent(); | |
836 | ||
837 | if ( !parent || !parent->GetEventHandler()->ProcessEvent(event) ) | |
838 | { | |
839 | event.Skip(); | |
840 | } | |
841 | } | |
842 | ||
843 | if ( ( !m_wasModal ) && | |
844 | event.IsAllowed() && | |
845 | ( event.GetEventType() == wxEVT_WIZARD_FINISHED || | |
846 | event.GetEventType() == wxEVT_WIZARD_CANCEL | |
847 | ) | |
848 | ) | |
849 | { | |
850 | Destroy(); | |
851 | } | |
852 | } | |
853 | ||
854 | void wxWizard::SetBitmap(const wxBitmap& bitmap) | |
855 | { | |
856 | m_bitmap = bitmap; | |
857 | if (m_statbmp) | |
858 | m_statbmp->SetBitmap(m_bitmap); | |
859 | } | |
860 | ||
861 | // ---------------------------------------------------------------------------- | |
862 | // wxWizardEvent | |
863 | // ---------------------------------------------------------------------------- | |
864 | ||
865 | wxWizardEvent::wxWizardEvent(wxEventType type, int id, bool direction, wxWizardPage* page) | |
866 | : wxNotifyEvent(type, id) | |
867 | { | |
868 | // Modified 10-20-2001 Robert Cavanaugh | |
869 | // add the active page to the event data | |
870 | m_direction = direction; | |
871 | m_page = page; | |
872 | } | |
873 | ||
874 | /// Do the adaptation | |
875 | bool wxWizard::DoLayoutAdaptation() | |
876 | { | |
877 | wxWindowList windows; | |
878 | wxWindowList pages; | |
879 | ||
880 | // Make all the pages (that use sizers) scrollable | |
881 | for ( wxSizerItemList::compatibility_iterator node = m_sizerPage->GetChildren().GetFirst(); node; node = node->GetNext() ) | |
882 | { | |
883 | wxSizerItem * const item = node->GetData(); | |
884 | if ( item->IsWindow() ) | |
885 | { | |
886 | wxWizardPage* page = wxDynamicCast(item->GetWindow(), wxWizardPage); | |
887 | if (page) | |
888 | { | |
889 | while (page) | |
890 | { | |
891 | if (!pages.Find(page) && page->GetSizer()) | |
892 | { | |
893 | // Create a scrolled window and reparent | |
894 | wxScrolledWindow* scrolledWindow = new wxScrolledWindow(page, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL|wxVSCROLL|wxHSCROLL|wxBORDER_NONE); | |
895 | wxSizer* oldSizer = page->GetSizer(); | |
896 | ||
897 | wxSizer* newSizer = new wxBoxSizer(wxVERTICAL); | |
898 | newSizer->Add(scrolledWindow,1, wxEXPAND, 0); | |
899 | ||
900 | page->SetSizer(newSizer, false /* don't delete the old sizer */); | |
901 | ||
902 | scrolledWindow->SetSizer(oldSizer); | |
903 | ||
904 | wxStandardDialogLayoutAdapter::DoReparentControls(page, scrolledWindow); | |
905 | ||
906 | pages.Append(page); | |
907 | windows.Append(scrolledWindow); | |
908 | } | |
909 | page = page->GetNext(); | |
910 | } | |
911 | } | |
912 | } | |
913 | } | |
914 | ||
915 | wxStandardDialogLayoutAdapter::DoFitWithScrolling(this, windows); | |
916 | ||
917 | // Size event doesn't get sent soon enough on wxGTK | |
918 | DoLayout(); | |
919 | ||
920 | SetLayoutAdaptationDone(true); | |
921 | ||
922 | return true; | |
923 | } | |
924 | ||
925 | bool wxWizard::ResizeBitmap(wxBitmap& bmp) | |
926 | { | |
927 | if (!GetBitmapPlacement()) | |
928 | return false; | |
929 | ||
930 | if (bmp.Ok()) | |
931 | { | |
932 | wxSize pageSize = m_sizerPage->GetSize(); | |
933 | if (pageSize == wxSize(0,0)) | |
934 | pageSize = GetPageSize(); | |
935 | int bitmapWidth = wxMax(bmp.GetWidth(), GetMinimumBitmapWidth()); | |
936 | int bitmapHeight = pageSize.y; | |
937 | ||
938 | if (!m_statbmp->GetBitmap().Ok() || m_statbmp->GetBitmap().GetHeight() != bitmapHeight) | |
939 | { | |
940 | wxBitmap bitmap(bitmapWidth, bitmapHeight); | |
941 | { | |
942 | wxMemoryDC dc; | |
943 | dc.SelectObject(bitmap); | |
944 | dc.SetBackground(wxBrush(m_bitmapBackgroundColour)); | |
945 | dc.Clear(); | |
946 | ||
947 | if (GetBitmapPlacement() & wxWIZARD_TILE) | |
948 | { | |
949 | TileBitmap(wxRect(0, 0, bitmapWidth, bitmapHeight), dc, bmp); | |
950 | } | |
951 | else | |
952 | { | |
953 | int x, y; | |
954 | ||
955 | if (GetBitmapPlacement() & wxWIZARD_HALIGN_LEFT) | |
956 | x = 0; | |
957 | else if (GetBitmapPlacement() & wxWIZARD_HALIGN_RIGHT) | |
958 | x = bitmapWidth - bmp.GetWidth(); | |
959 | else | |
960 | x = (bitmapWidth - bmp.GetWidth())/2; | |
961 | ||
962 | if (GetBitmapPlacement() & wxWIZARD_VALIGN_TOP) | |
963 | y = 0; | |
964 | else if (GetBitmapPlacement() & wxWIZARD_VALIGN_BOTTOM) | |
965 | y = bitmapHeight - bmp.GetHeight(); | |
966 | else | |
967 | y = (bitmapHeight - bmp.GetHeight())/2; | |
968 | ||
969 | dc.DrawBitmap(bmp, x, y, true); | |
970 | dc.SelectObject(wxNullBitmap); | |
971 | } | |
972 | } | |
973 | ||
974 | bmp = bitmap; | |
975 | } | |
976 | } | |
977 | ||
978 | return true; | |
979 | } | |
980 | ||
981 | bool wxWizard::TileBitmap(const wxRect& rect, wxDC& dc, const wxBitmap& bitmap) | |
982 | { | |
983 | int w = bitmap.GetWidth(); | |
984 | int h = bitmap.GetHeight(); | |
985 | ||
986 | wxMemoryDC dcMem; | |
987 | ||
988 | dcMem.SelectObjectAsSource(bitmap); | |
989 | ||
990 | int i, j; | |
991 | for (i = rect.x; i < rect.x + rect.width; i += w) | |
992 | { | |
993 | for (j = rect.y; j < rect.y + rect.height; j+= h) | |
994 | dc.Blit(i, j, bitmap.GetWidth(), bitmap.GetHeight(), & dcMem, 0, 0); | |
995 | } | |
996 | dcMem.SelectObject(wxNullBitmap); | |
997 | ||
998 | return true; | |
999 | } | |
1000 | ||
1001 | #endif // wxUSE_WIZARDDLG |