]>
Commit | Line | Data |
---|---|---|
1 | ///////////////////////////////////////////////////////////////////////////// | |
2 | // Name: src/common/dlgcmn.cpp | |
3 | // Purpose: common (to all ports) wxDialog functions | |
4 | // Author: Vadim Zeitlin | |
5 | // Modified by: | |
6 | // Created: 28.06.99 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Vadim Zeitlin | |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
12 | // ============================================================================ | |
13 | // declarations | |
14 | // ============================================================================ | |
15 | ||
16 | // ---------------------------------------------------------------------------- | |
17 | // headers | |
18 | // ---------------------------------------------------------------------------- | |
19 | ||
20 | // For compilers that support precompilation, includes "wx.h". | |
21 | #include "wx/wxprec.h" | |
22 | ||
23 | #ifdef __BORLANDC__ | |
24 | #pragma hdrstop | |
25 | #endif | |
26 | ||
27 | #include "wx/dialog.h" | |
28 | ||
29 | #ifndef WX_PRECOMP | |
30 | #include "wx/app.h" | |
31 | #include "wx/button.h" | |
32 | #include "wx/dcclient.h" | |
33 | #include "wx/intl.h" | |
34 | #include "wx/settings.h" | |
35 | #include "wx/stattext.h" | |
36 | #include "wx/sizer.h" | |
37 | #include "wx/containr.h" | |
38 | #endif | |
39 | ||
40 | #include "wx/statline.h" | |
41 | #include "wx/sysopt.h" | |
42 | #include "wx/module.h" | |
43 | #include "wx/bookctrl.h" | |
44 | #include "wx/scrolwin.h" | |
45 | #include "wx/textwrapper.h" | |
46 | ||
47 | #if wxUSE_DISPLAY | |
48 | #include "wx/display.h" | |
49 | #endif | |
50 | ||
51 | // ---------------------------------------------------------------------------- | |
52 | // wxDialogBase | |
53 | // ---------------------------------------------------------------------------- | |
54 | ||
55 | BEGIN_EVENT_TABLE(wxDialogBase, wxTopLevelWindow) | |
56 | EVT_BUTTON(wxID_ANY, wxDialogBase::OnButton) | |
57 | ||
58 | EVT_CLOSE(wxDialogBase::OnCloseWindow) | |
59 | ||
60 | EVT_CHAR_HOOK(wxDialogBase::OnCharHook) | |
61 | END_EVENT_TABLE() | |
62 | ||
63 | wxDialogLayoutAdapter* wxDialogBase::sm_layoutAdapter = NULL; | |
64 | bool wxDialogBase::sm_layoutAdaptation = false; | |
65 | ||
66 | void wxDialogBase::Init() | |
67 | { | |
68 | m_returnCode = 0; | |
69 | m_affirmativeId = wxID_OK; | |
70 | m_escapeId = wxID_ANY; | |
71 | m_layoutAdaptationLevel = 3; | |
72 | m_layoutAdaptationDone = FALSE; | |
73 | m_layoutAdaptationMode = wxDIALOG_ADAPTATION_MODE_DEFAULT; | |
74 | ||
75 | // the dialogs have this flag on by default to prevent the events from the | |
76 | // dialog controls from reaching the parent frame which is usually | |
77 | // undesirable and can lead to unexpected and hard to find bugs | |
78 | SetExtraStyle(GetExtraStyle() | wxWS_EX_BLOCK_EVENTS); | |
79 | } | |
80 | ||
81 | wxWindow *wxDialogBase::CheckIfCanBeUsedAsParent(wxWindow *parent) const | |
82 | { | |
83 | if ( !parent ) | |
84 | return NULL; | |
85 | ||
86 | extern WXDLLIMPEXP_DATA_BASE(wxList) wxPendingDelete; | |
87 | if ( wxPendingDelete.Member(parent) || parent->IsBeingDeleted() ) | |
88 | { | |
89 | // this window is being deleted and we shouldn't create any children | |
90 | // under it | |
91 | return NULL; | |
92 | } | |
93 | ||
94 | if ( parent->HasExtraStyle(wxWS_EX_TRANSIENT) ) | |
95 | { | |
96 | // this window is not being deleted yet but it's going to disappear | |
97 | // soon so still don't parent this window under it | |
98 | return NULL; | |
99 | } | |
100 | ||
101 | if ( !parent->IsShownOnScreen() ) | |
102 | { | |
103 | // using hidden parent won't work correctly neither | |
104 | return NULL; | |
105 | } | |
106 | ||
107 | // FIXME-VC6: this compiler requires an explicit const cast or it fails | |
108 | // with error C2446 | |
109 | if ( const_cast<const wxWindow *>(parent) == this ) | |
110 | { | |
111 | // not sure if this can really happen but it doesn't hurt to guard | |
112 | // against this clearly invalid situation | |
113 | return NULL; | |
114 | } | |
115 | ||
116 | return parent; | |
117 | } | |
118 | ||
119 | wxWindow * | |
120 | wxDialogBase::GetParentForModalDialog(wxWindow *parent, long style) const | |
121 | { | |
122 | // creating a parent-less modal dialog will result (under e.g. wxGTK2) | |
123 | // in an unfocused dialog, so try to find a valid parent for it unless we | |
124 | // were explicitly asked not to | |
125 | if ( style & wxDIALOG_NO_PARENT ) | |
126 | return NULL; | |
127 | ||
128 | // first try the given parent | |
129 | if ( parent ) | |
130 | parent = CheckIfCanBeUsedAsParent(wxGetTopLevelParent(parent)); | |
131 | ||
132 | // then the currently active window | |
133 | if ( !parent ) | |
134 | parent = CheckIfCanBeUsedAsParent( | |
135 | wxGetTopLevelParent(wxGetActiveWindow())); | |
136 | ||
137 | // and finally the application main window | |
138 | if ( !parent ) | |
139 | parent = CheckIfCanBeUsedAsParent(wxTheApp->GetTopWindow()); | |
140 | ||
141 | return parent; | |
142 | } | |
143 | ||
144 | #if wxUSE_STATTEXT | |
145 | ||
146 | wxSizer *wxDialogBase::CreateTextSizer(const wxString& message) | |
147 | { | |
148 | wxTextSizerWrapper wrapper(this); | |
149 | ||
150 | return CreateTextSizer(message, wrapper); | |
151 | } | |
152 | ||
153 | wxSizer *wxDialogBase::CreateTextSizer(const wxString& message, | |
154 | wxTextSizerWrapper& wrapper) | |
155 | { | |
156 | // I admit that this is complete bogus, but it makes | |
157 | // message boxes work for pda screens temporarily.. | |
158 | int widthMax = -1; | |
159 | const bool is_pda = wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA; | |
160 | if (is_pda) | |
161 | { | |
162 | widthMax = wxSystemSettings::GetMetric( wxSYS_SCREEN_X ) - 25; | |
163 | } | |
164 | ||
165 | // '&' is used as accel mnemonic prefix in the wxWidgets controls but in | |
166 | // the static messages created by CreateTextSizer() (used by wxMessageBox, | |
167 | // for example), we don't want this special meaning, so we need to quote it | |
168 | wxString text(message); | |
169 | text.Replace(wxT("&"), wxT("&&")); | |
170 | ||
171 | return wrapper.CreateSizer(text, widthMax); | |
172 | } | |
173 | ||
174 | #endif // wxUSE_STATTEXT | |
175 | ||
176 | wxSizer *wxDialogBase::CreateButtonSizer(long flags) | |
177 | { | |
178 | #ifdef __SMARTPHONE__ | |
179 | wxDialog* dialog = (wxDialog*) this; | |
180 | if ( flags & wxOK ) | |
181 | dialog->SetLeftMenu(wxID_OK); | |
182 | ||
183 | if ( flags & wxCANCEL ) | |
184 | dialog->SetRightMenu(wxID_CANCEL); | |
185 | ||
186 | if ( flags & wxYES ) | |
187 | dialog->SetLeftMenu(wxID_YES); | |
188 | ||
189 | if ( flags & wxNO ) | |
190 | dialog->SetRightMenu(wxID_NO); | |
191 | ||
192 | return NULL; | |
193 | #else // !__SMARTPHONE__ | |
194 | ||
195 | #if wxUSE_BUTTON | |
196 | ||
197 | #ifdef __POCKETPC__ | |
198 | // PocketPC guidelines recommend for Ok/Cancel dialogs to use OK button | |
199 | // located inside caption bar and implement Cancel functionality through | |
200 | // Undo outside dialog. As native behaviour this will be default here but | |
201 | // can be replaced with real wxButtons by setting the option below to 1 | |
202 | if ( (flags & ~(wxCANCEL|wxNO_DEFAULT)) != wxOK || | |
203 | wxSystemOptions::GetOptionInt(wxT("wince.dialog.real-ok-cancel")) ) | |
204 | #endif // __POCKETPC__ | |
205 | { | |
206 | return CreateStdDialogButtonSizer(flags); | |
207 | } | |
208 | #ifdef __POCKETPC__ | |
209 | return NULL; | |
210 | #endif // __POCKETPC__ | |
211 | ||
212 | #else // !wxUSE_BUTTON | |
213 | wxUnusedVar(flags); | |
214 | ||
215 | return NULL; | |
216 | #endif // wxUSE_BUTTON/!wxUSE_BUTTON | |
217 | ||
218 | #endif // __SMARTPHONE__/!__SMARTPHONE__ | |
219 | } | |
220 | ||
221 | wxSizer *wxDialogBase::CreateSeparatedButtonSizer(long flags) | |
222 | { | |
223 | wxSizer *sizer = CreateButtonSizer(flags); | |
224 | if ( !sizer ) | |
225 | return NULL; | |
226 | ||
227 | // Mac Human Interface Guidelines recommend not to use static lines as | |
228 | // grouping elements | |
229 | #if wxUSE_STATLINE && !defined(__WXMAC__) | |
230 | wxBoxSizer *topsizer = new wxBoxSizer(wxVERTICAL); | |
231 | topsizer->Add(new wxStaticLine(this), | |
232 | wxSizerFlags().Expand().DoubleBorder(wxBOTTOM)); | |
233 | topsizer->Add(sizer, wxSizerFlags().Expand()); | |
234 | sizer = topsizer; | |
235 | #endif // wxUSE_STATLINE | |
236 | ||
237 | return sizer; | |
238 | } | |
239 | ||
240 | #if wxUSE_BUTTON | |
241 | ||
242 | wxStdDialogButtonSizer *wxDialogBase::CreateStdDialogButtonSizer( long flags ) | |
243 | { | |
244 | wxStdDialogButtonSizer *sizer = new wxStdDialogButtonSizer(); | |
245 | ||
246 | wxButton *ok = NULL; | |
247 | wxButton *yes = NULL; | |
248 | wxButton *no = NULL; | |
249 | ||
250 | if (flags & wxOK) | |
251 | { | |
252 | ok = new wxButton(this, wxID_OK); | |
253 | sizer->AddButton(ok); | |
254 | } | |
255 | ||
256 | if (flags & wxCANCEL) | |
257 | { | |
258 | wxButton *cancel = new wxButton(this, wxID_CANCEL); | |
259 | sizer->AddButton(cancel); | |
260 | } | |
261 | ||
262 | if (flags & wxYES) | |
263 | { | |
264 | yes = new wxButton(this, wxID_YES); | |
265 | sizer->AddButton(yes); | |
266 | } | |
267 | ||
268 | if (flags & wxNO) | |
269 | { | |
270 | no = new wxButton(this, wxID_NO); | |
271 | sizer->AddButton(no); | |
272 | } | |
273 | ||
274 | if (flags & wxAPPLY) | |
275 | { | |
276 | wxButton *apply = new wxButton(this, wxID_APPLY); | |
277 | sizer->AddButton(apply); | |
278 | } | |
279 | ||
280 | if (flags & wxCLOSE) | |
281 | { | |
282 | wxButton *close = new wxButton(this, wxID_CLOSE); | |
283 | sizer->AddButton(close); | |
284 | } | |
285 | ||
286 | if (flags & wxHELP) | |
287 | { | |
288 | wxButton *help = new wxButton(this, wxID_HELP); | |
289 | sizer->AddButton(help); | |
290 | } | |
291 | ||
292 | if (flags & wxNO_DEFAULT) | |
293 | { | |
294 | if (no) | |
295 | { | |
296 | no->SetDefault(); | |
297 | no->SetFocus(); | |
298 | } | |
299 | } | |
300 | else | |
301 | { | |
302 | if (ok) | |
303 | { | |
304 | ok->SetDefault(); | |
305 | ok->SetFocus(); | |
306 | } | |
307 | else if (yes) | |
308 | { | |
309 | yes->SetDefault(); | |
310 | yes->SetFocus(); | |
311 | } | |
312 | } | |
313 | ||
314 | if (flags & wxOK) | |
315 | SetAffirmativeId(wxID_OK); | |
316 | else if (flags & wxYES) | |
317 | SetAffirmativeId(wxID_YES); | |
318 | ||
319 | sizer->Realize(); | |
320 | ||
321 | return sizer; | |
322 | } | |
323 | ||
324 | #endif // wxUSE_BUTTON | |
325 | ||
326 | // ---------------------------------------------------------------------------- | |
327 | // standard buttons handling | |
328 | // ---------------------------------------------------------------------------- | |
329 | ||
330 | void wxDialogBase::EndDialog(int rc) | |
331 | { | |
332 | if ( IsModal() ) | |
333 | EndModal(rc); | |
334 | else | |
335 | Hide(); | |
336 | } | |
337 | ||
338 | void wxDialogBase::AcceptAndClose() | |
339 | { | |
340 | if ( Validate() && TransferDataFromWindow() ) | |
341 | { | |
342 | EndDialog(m_affirmativeId); | |
343 | } | |
344 | } | |
345 | ||
346 | void wxDialogBase::SetAffirmativeId(int affirmativeId) | |
347 | { | |
348 | m_affirmativeId = affirmativeId; | |
349 | } | |
350 | ||
351 | void wxDialogBase::SetEscapeId(int escapeId) | |
352 | { | |
353 | m_escapeId = escapeId; | |
354 | } | |
355 | ||
356 | bool wxDialogBase::EmulateButtonClickIfPresent(int id) | |
357 | { | |
358 | #if wxUSE_BUTTON | |
359 | wxButton *btn = wxDynamicCast(FindWindow(id), wxButton); | |
360 | ||
361 | if ( !btn || !btn->IsEnabled() || !btn->IsShown() ) | |
362 | return false; | |
363 | ||
364 | wxCommandEvent event(wxEVT_COMMAND_BUTTON_CLICKED, id); | |
365 | event.SetEventObject(btn); | |
366 | btn->GetEventHandler()->ProcessEvent(event); | |
367 | ||
368 | return true; | |
369 | #else // !wxUSE_BUTTON | |
370 | wxUnusedVar(id); | |
371 | return false; | |
372 | #endif // wxUSE_BUTTON/!wxUSE_BUTTON | |
373 | } | |
374 | ||
375 | bool wxDialogBase::IsEscapeKey(const wxKeyEvent& event) | |
376 | { | |
377 | // for most platforms, Esc key is used to close the dialogs | |
378 | return event.GetKeyCode() == WXK_ESCAPE && | |
379 | event.GetModifiers() == wxMOD_NONE; | |
380 | } | |
381 | ||
382 | void wxDialogBase::OnCharHook(wxKeyEvent& event) | |
383 | { | |
384 | if ( event.GetKeyCode() == WXK_ESCAPE ) | |
385 | { | |
386 | int idCancel = GetEscapeId(); | |
387 | switch ( idCancel ) | |
388 | { | |
389 | case wxID_NONE: | |
390 | // don't handle Esc specially at all | |
391 | break; | |
392 | ||
393 | case wxID_ANY: | |
394 | // this value is special: it means translate Esc to wxID_CANCEL | |
395 | // but if there is no such button, then fall back to wxID_OK | |
396 | if ( EmulateButtonClickIfPresent(wxID_CANCEL) ) | |
397 | return; | |
398 | idCancel = GetAffirmativeId(); | |
399 | // fall through | |
400 | ||
401 | default: | |
402 | // translate Esc to button press for the button with given id | |
403 | if ( EmulateButtonClickIfPresent(idCancel) ) | |
404 | return; | |
405 | } | |
406 | } | |
407 | ||
408 | event.Skip(); | |
409 | } | |
410 | ||
411 | void wxDialogBase::OnButton(wxCommandEvent& event) | |
412 | { | |
413 | const int id = event.GetId(); | |
414 | if ( id == GetAffirmativeId() ) | |
415 | { | |
416 | AcceptAndClose(); | |
417 | } | |
418 | else if ( id == wxID_APPLY ) | |
419 | { | |
420 | if ( Validate() ) | |
421 | TransferDataFromWindow(); | |
422 | ||
423 | // TODO: disable the Apply button until things change again | |
424 | } | |
425 | else if ( id == GetEscapeId() || | |
426 | (id == wxID_CANCEL && GetEscapeId() == wxID_ANY) ) | |
427 | { | |
428 | EndDialog(wxID_CANCEL); | |
429 | } | |
430 | else // not a standard button | |
431 | { | |
432 | event.Skip(); | |
433 | } | |
434 | } | |
435 | ||
436 | // ---------------------------------------------------------------------------- | |
437 | // compatibility methods for supporting the modality API | |
438 | // ---------------------------------------------------------------------------- | |
439 | ||
440 | wxDEFINE_EVENT( wxEVT_WINDOW_MODAL_DIALOG_CLOSED , wxWindowModalDialogEvent ); | |
441 | ||
442 | IMPLEMENT_DYNAMIC_CLASS(wxWindowModalDialogEvent, wxCommandEvent) | |
443 | ||
444 | void wxDialogBase::ShowWindowModal () | |
445 | { | |
446 | ShowModal(); | |
447 | SendWindowModalDialogEvent ( wxEVT_WINDOW_MODAL_DIALOG_CLOSED ); | |
448 | } | |
449 | ||
450 | void wxDialogBase::SendWindowModalDialogEvent ( wxEventType type ) | |
451 | { | |
452 | wxWindowModalDialogEvent event ( type, GetId()); | |
453 | event.SetEventObject(this); | |
454 | ||
455 | if ( !GetEventHandler()->ProcessEvent(event) ) | |
456 | { | |
457 | // the event is not propagated upwards to the parent automatically | |
458 | // because the dialog is a top level window, so do it manually as | |
459 | // in 9 cases of 10 the message must be processed by the dialog | |
460 | // owner and not the dialog itself | |
461 | (void)GetParent()->GetEventHandler()->ProcessEvent(event); | |
462 | } | |
463 | } | |
464 | ||
465 | ||
466 | wxDialogModality wxDialogBase::GetModality() const | |
467 | { | |
468 | return IsModal() ? wxDIALOG_MODALITY_APP_MODAL : wxDIALOG_MODALITY_NONE; | |
469 | } | |
470 | ||
471 | // ---------------------------------------------------------------------------- | |
472 | // other event handlers | |
473 | // ---------------------------------------------------------------------------- | |
474 | ||
475 | void wxDialogBase::OnCloseWindow(wxCloseEvent& WXUNUSED(event)) | |
476 | { | |
477 | // We'll send a Cancel message by default, which may close the dialog. | |
478 | // Check for looping if the Cancel event handler calls Close(). | |
479 | ||
480 | // Note that if a cancel button and handler aren't present in the dialog, | |
481 | // nothing will happen when you close the dialog via the window manager, or | |
482 | // via Close(). We wouldn't want to destroy the dialog by default, since | |
483 | // the dialog may have been created on the stack. However, this does mean | |
484 | // that calling dialog->Close() won't delete the dialog unless the handler | |
485 | // for wxID_CANCEL does so. So use Destroy() if you want to be sure to | |
486 | // destroy the dialog. The default OnCancel (above) simply ends a modal | |
487 | // dialog, and hides a modeless dialog. | |
488 | ||
489 | int idCancel = GetEscapeId(); | |
490 | if ( idCancel == wxID_NONE ) | |
491 | return; | |
492 | if ( idCancel == wxID_ANY ) | |
493 | idCancel = wxID_CANCEL; | |
494 | ||
495 | // VZ: this is horrible and MT-unsafe. Can't we reuse some of these global | |
496 | // lists here? don't dare to change it now, but should be done later! | |
497 | static wxList closing; | |
498 | ||
499 | if ( closing.Member(this) ) | |
500 | return; | |
501 | ||
502 | closing.Append(this); | |
503 | ||
504 | wxCommandEvent cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED, idCancel); | |
505 | cancelEvent.SetEventObject( this ); | |
506 | GetEventHandler()->ProcessEvent(cancelEvent); // This may close the dialog | |
507 | ||
508 | closing.DeleteObject(this); | |
509 | } | |
510 | ||
511 | void wxDialogBase::OnSysColourChanged(wxSysColourChangedEvent& event) | |
512 | { | |
513 | #ifndef __WXGTK__ | |
514 | SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE)); | |
515 | Refresh(); | |
516 | #endif | |
517 | ||
518 | event.Skip(); | |
519 | } | |
520 | ||
521 | /// Do the adaptation | |
522 | bool wxDialogBase::DoLayoutAdaptation() | |
523 | { | |
524 | if (GetLayoutAdapter()) | |
525 | { | |
526 | wxWindow* focusWindow = wxFindFocusDescendant(this); // from event.h | |
527 | if (GetLayoutAdapter()->DoLayoutAdaptation((wxDialog*) this)) | |
528 | { | |
529 | if (focusWindow) | |
530 | focusWindow->SetFocus(); | |
531 | return true; | |
532 | } | |
533 | else | |
534 | return false; | |
535 | } | |
536 | else | |
537 | return false; | |
538 | } | |
539 | ||
540 | /// Can we do the adaptation? | |
541 | bool wxDialogBase::CanDoLayoutAdaptation() | |
542 | { | |
543 | // Check if local setting overrides the global setting | |
544 | bool layoutEnabled = (GetLayoutAdaptationMode() == wxDIALOG_ADAPTATION_MODE_ENABLED) || (IsLayoutAdaptationEnabled() && (GetLayoutAdaptationMode() != wxDIALOG_ADAPTATION_MODE_DISABLED)); | |
545 | ||
546 | return (layoutEnabled && !m_layoutAdaptationDone && GetLayoutAdaptationLevel() != 0 && GetLayoutAdapter() != NULL && GetLayoutAdapter()->CanDoLayoutAdaptation((wxDialog*) this)); | |
547 | } | |
548 | ||
549 | /// Set scrolling adapter class, returning old adapter | |
550 | wxDialogLayoutAdapter* wxDialogBase::SetLayoutAdapter(wxDialogLayoutAdapter* adapter) | |
551 | { | |
552 | wxDialogLayoutAdapter* oldLayoutAdapter = sm_layoutAdapter; | |
553 | sm_layoutAdapter = adapter; | |
554 | return oldLayoutAdapter; | |
555 | } | |
556 | ||
557 | /*! | |
558 | * Standard adapter | |
559 | */ | |
560 | ||
561 | IMPLEMENT_CLASS(wxDialogLayoutAdapter, wxObject) | |
562 | ||
563 | IMPLEMENT_CLASS(wxStandardDialogLayoutAdapter, wxDialogLayoutAdapter) | |
564 | ||
565 | // Allow for caption size on wxWidgets < 2.9 | |
566 | #if defined(__WXGTK__) && !wxCHECK_VERSION(2,9,0) | |
567 | #define wxEXTRA_DIALOG_HEIGHT 30 | |
568 | #else | |
569 | #define wxEXTRA_DIALOG_HEIGHT 0 | |
570 | #endif | |
571 | ||
572 | /// Indicate that adaptation should be done | |
573 | bool wxStandardDialogLayoutAdapter::CanDoLayoutAdaptation(wxDialog* dialog) | |
574 | { | |
575 | if (dialog->GetSizer()) | |
576 | { | |
577 | wxSize windowSize, displaySize; | |
578 | return MustScroll(dialog, windowSize, displaySize) != 0; | |
579 | } | |
580 | else | |
581 | return false; | |
582 | } | |
583 | ||
584 | bool wxStandardDialogLayoutAdapter::DoLayoutAdaptation(wxDialog* dialog) | |
585 | { | |
586 | if (dialog->GetSizer()) | |
587 | { | |
588 | #if wxUSE_BOOKCTRL | |
589 | wxBookCtrlBase* bookContentWindow = wxDynamicCast(dialog->GetContentWindow(), wxBookCtrlBase); | |
590 | ||
591 | if (bookContentWindow) | |
592 | { | |
593 | // If we have a book control, make all the pages (that use sizers) scrollable | |
594 | wxWindowList windows; | |
595 | for (size_t i = 0; i < bookContentWindow->GetPageCount(); i++) | |
596 | { | |
597 | wxWindow* page = bookContentWindow->GetPage(i); | |
598 | ||
599 | wxScrolledWindow* scrolledWindow = wxDynamicCast(page, wxScrolledWindow); | |
600 | if (scrolledWindow) | |
601 | windows.Append(scrolledWindow); | |
602 | else if (!scrolledWindow && page->GetSizer()) | |
603 | { | |
604 | // Create a scrolled window and reparent | |
605 | scrolledWindow = CreateScrolledWindow(page); | |
606 | wxSizer* oldSizer = page->GetSizer(); | |
607 | ||
608 | wxSizer* newSizer = new wxBoxSizer(wxVERTICAL); | |
609 | newSizer->Add(scrolledWindow,1, wxEXPAND, 0); | |
610 | ||
611 | page->SetSizer(newSizer, false /* don't delete the old sizer */); | |
612 | ||
613 | scrolledWindow->SetSizer(oldSizer); | |
614 | ||
615 | ReparentControls(page, scrolledWindow); | |
616 | ||
617 | windows.Append(scrolledWindow); | |
618 | } | |
619 | } | |
620 | ||
621 | FitWithScrolling(dialog, windows); | |
622 | } | |
623 | else | |
624 | #endif // wxUSE_BOOKCTRL | |
625 | { | |
626 | // If we have an arbitrary dialog, create a scrolling area for the main content, and a button sizer | |
627 | // for the main buttons. | |
628 | wxScrolledWindow* scrolledWindow = CreateScrolledWindow(dialog); | |
629 | ||
630 | int buttonSizerBorder = 0; | |
631 | ||
632 | // First try to find a wxStdDialogButtonSizer | |
633 | wxSizer* buttonSizer = FindButtonSizer(true /* find std button sizer */, dialog, dialog->GetSizer(), buttonSizerBorder); | |
634 | ||
635 | // Next try to find a wxBoxSizer containing the controls | |
636 | if (!buttonSizer && dialog->GetLayoutAdaptationLevel() > wxDIALOG_ADAPTATION_STANDARD_SIZER) | |
637 | buttonSizer = FindButtonSizer(false /* find ordinary sizer */, dialog, dialog->GetSizer(), buttonSizerBorder); | |
638 | ||
639 | // If we still don't have a button sizer, collect any 'loose' buttons in the layout | |
640 | if (!buttonSizer && dialog->GetLayoutAdaptationLevel() > wxDIALOG_ADAPTATION_ANY_SIZER) | |
641 | { | |
642 | int count = 0; | |
643 | wxStdDialogButtonSizer* stdButtonSizer = new wxStdDialogButtonSizer; | |
644 | buttonSizer = stdButtonSizer; | |
645 | ||
646 | FindLooseButtons(dialog, stdButtonSizer, dialog->GetSizer(), count); | |
647 | if (count > 0) | |
648 | stdButtonSizer->Realize(); | |
649 | else | |
650 | { | |
651 | wxDELETE(buttonSizer); | |
652 | } | |
653 | } | |
654 | ||
655 | if (buttonSizerBorder == 0) | |
656 | buttonSizerBorder = 5; | |
657 | ||
658 | ReparentControls(dialog, scrolledWindow, buttonSizer); | |
659 | ||
660 | wxBoxSizer* newTopSizer = new wxBoxSizer(wxVERTICAL); | |
661 | wxSizer* oldSizer = dialog->GetSizer(); | |
662 | ||
663 | dialog->SetSizer(newTopSizer, false /* don't delete old sizer */); | |
664 | ||
665 | newTopSizer->Add(scrolledWindow, 1, wxEXPAND|wxALL, 0); | |
666 | if (buttonSizer) | |
667 | newTopSizer->Add(buttonSizer, 0, wxEXPAND|wxALL, buttonSizerBorder); | |
668 | ||
669 | scrolledWindow->SetSizer(oldSizer); | |
670 | ||
671 | FitWithScrolling(dialog, scrolledWindow); | |
672 | } | |
673 | } | |
674 | ||
675 | dialog->SetLayoutAdaptationDone(true); | |
676 | return true; | |
677 | } | |
678 | ||
679 | // Create the scrolled window | |
680 | wxScrolledWindow* wxStandardDialogLayoutAdapter::CreateScrolledWindow(wxWindow* parent) | |
681 | { | |
682 | wxScrolledWindow* scrolledWindow = new wxScrolledWindow(parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxTAB_TRAVERSAL|wxVSCROLL|wxHSCROLL|wxBORDER_NONE); | |
683 | return scrolledWindow; | |
684 | } | |
685 | ||
686 | /// Find and remove the button sizer, if any | |
687 | wxSizer* wxStandardDialogLayoutAdapter::FindButtonSizer(bool stdButtonSizer, wxDialog* dialog, wxSizer* sizer, int& retBorder, int accumlatedBorder) | |
688 | { | |
689 | for ( wxSizerItemList::compatibility_iterator node = sizer->GetChildren().GetFirst(); | |
690 | node; node = node->GetNext() ) | |
691 | { | |
692 | wxSizerItem *item = node->GetData(); | |
693 | wxSizer *childSizer = item->GetSizer(); | |
694 | ||
695 | if ( childSizer ) | |
696 | { | |
697 | int newBorder = accumlatedBorder; | |
698 | if (item->GetFlag() & wxALL) | |
699 | newBorder += item->GetBorder(); | |
700 | ||
701 | if (stdButtonSizer) // find wxStdDialogButtonSizer | |
702 | { | |
703 | wxStdDialogButtonSizer* buttonSizer = wxDynamicCast(childSizer, wxStdDialogButtonSizer); | |
704 | if (buttonSizer) | |
705 | { | |
706 | sizer->Detach(childSizer); | |
707 | retBorder = newBorder; | |
708 | return buttonSizer; | |
709 | } | |
710 | } | |
711 | else // find a horizontal box sizer containing standard buttons | |
712 | { | |
713 | wxBoxSizer* buttonSizer = wxDynamicCast(childSizer, wxBoxSizer); | |
714 | if (buttonSizer && IsOrdinaryButtonSizer(dialog, buttonSizer)) | |
715 | { | |
716 | sizer->Detach(childSizer); | |
717 | retBorder = newBorder; | |
718 | return buttonSizer; | |
719 | } | |
720 | } | |
721 | ||
722 | wxSizer* s = FindButtonSizer(stdButtonSizer, dialog, childSizer, retBorder, newBorder); | |
723 | if (s) | |
724 | return s; | |
725 | } | |
726 | } | |
727 | return NULL; | |
728 | } | |
729 | ||
730 | /// Check if this sizer contains standard buttons, and so can be repositioned in the dialog | |
731 | bool wxStandardDialogLayoutAdapter::IsOrdinaryButtonSizer(wxDialog* dialog, wxBoxSizer* sizer) | |
732 | { | |
733 | if (sizer->GetOrientation() != wxHORIZONTAL) | |
734 | return false; | |
735 | ||
736 | for ( wxSizerItemList::compatibility_iterator node = sizer->GetChildren().GetFirst(); | |
737 | node; node = node->GetNext() ) | |
738 | { | |
739 | wxSizerItem *item = node->GetData(); | |
740 | wxButton *childButton = wxDynamicCast(item->GetWindow(), wxButton); | |
741 | ||
742 | if (childButton && IsStandardButton(dialog, childButton)) | |
743 | return true; | |
744 | } | |
745 | return false; | |
746 | } | |
747 | ||
748 | /// Check if this is a standard button | |
749 | bool wxStandardDialogLayoutAdapter::IsStandardButton(wxDialog* dialog, wxButton* button) | |
750 | { | |
751 | wxWindowID id = button->GetId(); | |
752 | ||
753 | return (id == wxID_OK || id == wxID_CANCEL || id == wxID_YES || id == wxID_NO || id == wxID_SAVE || | |
754 | id == wxID_APPLY || id == wxID_HELP || id == wxID_CONTEXT_HELP || dialog->IsMainButtonId(id)); | |
755 | } | |
756 | ||
757 | /// Find 'loose' main buttons in the existing layout and add them to the standard dialog sizer | |
758 | bool wxStandardDialogLayoutAdapter::FindLooseButtons(wxDialog* dialog, wxStdDialogButtonSizer* buttonSizer, wxSizer* sizer, int& count) | |
759 | { | |
760 | wxSizerItemList::compatibility_iterator node = sizer->GetChildren().GetFirst(); | |
761 | while (node) | |
762 | { | |
763 | wxSizerItemList::compatibility_iterator next = node->GetNext(); | |
764 | wxSizerItem *item = node->GetData(); | |
765 | wxSizer *childSizer = item->GetSizer(); | |
766 | wxButton *childButton = wxDynamicCast(item->GetWindow(), wxButton); | |
767 | ||
768 | if (childButton && IsStandardButton(dialog, childButton)) | |
769 | { | |
770 | sizer->Detach(childButton); | |
771 | buttonSizer->AddButton(childButton); | |
772 | count ++; | |
773 | } | |
774 | ||
775 | if (childSizer) | |
776 | FindLooseButtons(dialog, buttonSizer, childSizer, count); | |
777 | ||
778 | node = next; | |
779 | } | |
780 | return true; | |
781 | } | |
782 | ||
783 | /// Reparent the controls to the scrolled window | |
784 | void wxStandardDialogLayoutAdapter::ReparentControls(wxWindow* parent, wxWindow* reparentTo, wxSizer* buttonSizer) | |
785 | { | |
786 | DoReparentControls(parent, reparentTo, buttonSizer); | |
787 | } | |
788 | ||
789 | void wxStandardDialogLayoutAdapter::DoReparentControls(wxWindow* parent, wxWindow* reparentTo, wxSizer* buttonSizer) | |
790 | { | |
791 | wxWindowList::compatibility_iterator node = parent->GetChildren().GetFirst(); | |
792 | while (node) | |
793 | { | |
794 | wxWindowList::compatibility_iterator next = node->GetNext(); | |
795 | ||
796 | wxWindow *win = node->GetData(); | |
797 | ||
798 | // Don't reparent the scrolled window or buttons in the button sizer | |
799 | if (win != reparentTo && (!buttonSizer || !buttonSizer->GetItem(win))) | |
800 | { | |
801 | win->Reparent(reparentTo); | |
802 | #ifdef __WXMSW__ | |
803 | // Restore correct tab order | |
804 | ::SetWindowPos((HWND) win->GetHWND(), HWND_BOTTOM, -1, -1, -1, -1, SWP_NOMOVE|SWP_NOSIZE); | |
805 | #endif | |
806 | } | |
807 | ||
808 | node = next; | |
809 | } | |
810 | } | |
811 | ||
812 | /// Find whether scrolling will be necessary for the dialog, returning wxVERTICAL, wxHORIZONTAL or both | |
813 | int wxStandardDialogLayoutAdapter::MustScroll(wxDialog* dialog, wxSize& windowSize, wxSize& displaySize) | |
814 | { | |
815 | return DoMustScroll(dialog, windowSize, displaySize); | |
816 | } | |
817 | ||
818 | /// Find whether scrolling will be necessary for the dialog, returning wxVERTICAL, wxHORIZONTAL or both | |
819 | int wxStandardDialogLayoutAdapter::DoMustScroll(wxDialog* dialog, wxSize& windowSize, wxSize& displaySize) | |
820 | { | |
821 | wxSize minWindowSize = dialog->GetSizer()->GetMinSize(); | |
822 | windowSize = dialog->GetSize(); | |
823 | windowSize = wxSize(wxMax(windowSize.x, minWindowSize.x), wxMax(windowSize.y, minWindowSize.y)); | |
824 | #if wxUSE_DISPLAY | |
825 | displaySize = wxDisplay(wxDisplay::GetFromWindow(dialog)).GetClientArea().GetSize(); | |
826 | #else | |
827 | displaySize = wxGetClientDisplayRect().GetSize(); | |
828 | #endif | |
829 | ||
830 | int flags = 0; | |
831 | ||
832 | if (windowSize.y >= (displaySize.y - wxEXTRA_DIALOG_HEIGHT)) | |
833 | flags |= wxVERTICAL; | |
834 | if (windowSize.x >= displaySize.x) | |
835 | flags |= wxHORIZONTAL; | |
836 | ||
837 | return flags; | |
838 | } | |
839 | ||
840 | // A function to fit the dialog around its contents, and then adjust for screen size. | |
841 | // If scrolled windows are passed, scrolling is enabled in the required orientation(s). | |
842 | bool wxStandardDialogLayoutAdapter::FitWithScrolling(wxDialog* dialog, wxWindowList& windows) | |
843 | { | |
844 | return DoFitWithScrolling(dialog, windows); | |
845 | } | |
846 | ||
847 | // A function to fit the dialog around its contents, and then adjust for screen size. | |
848 | // If a scrolled window is passed, scrolling is enabled in the required orientation(s). | |
849 | bool wxStandardDialogLayoutAdapter::FitWithScrolling(wxDialog* dialog, wxScrolledWindow* scrolledWindow) | |
850 | { | |
851 | return DoFitWithScrolling(dialog, scrolledWindow); | |
852 | } | |
853 | ||
854 | // A function to fit the dialog around its contents, and then adjust for screen size. | |
855 | // If a scrolled window is passed, scrolling is enabled in the required orientation(s). | |
856 | bool wxStandardDialogLayoutAdapter::DoFitWithScrolling(wxDialog* dialog, wxScrolledWindow* scrolledWindow) | |
857 | { | |
858 | wxWindowList windows; | |
859 | windows.Append(scrolledWindow); | |
860 | return DoFitWithScrolling(dialog, windows); | |
861 | } | |
862 | ||
863 | bool wxStandardDialogLayoutAdapter::DoFitWithScrolling(wxDialog* dialog, wxWindowList& windows) | |
864 | { | |
865 | wxSizer* sizer = dialog->GetSizer(); | |
866 | if (!sizer) | |
867 | return false; | |
868 | ||
869 | sizer->SetSizeHints(dialog); | |
870 | ||
871 | wxSize windowSize, displaySize; | |
872 | int scrollFlags = DoMustScroll(dialog, windowSize, displaySize); | |
873 | int scrollBarSize = 20; | |
874 | ||
875 | if (scrollFlags) | |
876 | { | |
877 | int scrollBarExtraX = 0, scrollBarExtraY = 0; | |
878 | bool resizeHorizontally = (scrollFlags & wxHORIZONTAL) != 0; | |
879 | bool resizeVertically = (scrollFlags & wxVERTICAL) != 0; | |
880 | ||
881 | if (windows.GetCount() != 0) | |
882 | { | |
883 | // Allow extra for a scrollbar, assuming we resizing in one direction only. | |
884 | if ((resizeVertically && !resizeHorizontally) && (windowSize.x < (displaySize.x - scrollBarSize))) | |
885 | scrollBarExtraX = scrollBarSize; | |
886 | if ((resizeHorizontally && !resizeVertically) && (windowSize.y < (displaySize.y - scrollBarSize))) | |
887 | scrollBarExtraY = scrollBarSize; | |
888 | } | |
889 | ||
890 | wxWindowList::compatibility_iterator node = windows.GetFirst(); | |
891 | while (node) | |
892 | { | |
893 | wxWindow *win = node->GetData(); | |
894 | wxScrolledWindow* scrolledWindow = wxDynamicCast(win, wxScrolledWindow); | |
895 | if (scrolledWindow) | |
896 | { | |
897 | scrolledWindow->SetScrollRate(resizeHorizontally ? 10 : 0, resizeVertically ? 10 : 0); | |
898 | ||
899 | if (scrolledWindow->GetSizer()) | |
900 | scrolledWindow->GetSizer()->Fit(scrolledWindow); | |
901 | } | |
902 | ||
903 | node = node->GetNext(); | |
904 | } | |
905 | ||
906 | wxSize limitTo = windowSize + wxSize(scrollBarExtraX, scrollBarExtraY); | |
907 | if (resizeVertically) | |
908 | limitTo.y = displaySize.y - wxEXTRA_DIALOG_HEIGHT; | |
909 | if (resizeHorizontally) | |
910 | limitTo.x = displaySize.x; | |
911 | ||
912 | dialog->SetMinSize(limitTo); | |
913 | dialog->SetSize(limitTo); | |
914 | ||
915 | dialog->SetSizeHints( limitTo.x, limitTo.y, dialog->GetMaxWidth(), dialog->GetMaxHeight() ); | |
916 | } | |
917 | ||
918 | return true; | |
919 | } | |
920 | ||
921 | /*! | |
922 | * Module to initialise standard adapter | |
923 | */ | |
924 | ||
925 | class wxDialogLayoutAdapterModule: public wxModule | |
926 | { | |
927 | DECLARE_DYNAMIC_CLASS(wxDialogLayoutAdapterModule) | |
928 | public: | |
929 | wxDialogLayoutAdapterModule() {} | |
930 | virtual void OnExit() { delete wxDialogBase::SetLayoutAdapter(NULL); } | |
931 | virtual bool OnInit() { wxDialogBase::SetLayoutAdapter(new wxStandardDialogLayoutAdapter); return true; } | |
932 | }; | |
933 | ||
934 | IMPLEMENT_DYNAMIC_CLASS(wxDialogLayoutAdapterModule, wxModule) |