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