1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/dlgcmn.cpp
3 // Purpose: common (to all ports) wxDialog functions
4 // Author: Vadim Zeitlin
8 // Copyright: (c) Vadim Zeitlin
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
27 #include "wx/dialog.h"
31 #include "wx/button.h"
32 #include "wx/dcclient.h"
34 #include "wx/settings.h"
35 #include "wx/stattext.h"
37 #include "wx/containr.h"
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"
48 #include "wx/display.h"
51 // ----------------------------------------------------------------------------
53 // ----------------------------------------------------------------------------
55 BEGIN_EVENT_TABLE(wxDialogBase
, wxTopLevelWindow
)
56 EVT_BUTTON(wxID_ANY
, wxDialogBase::OnButton
)
58 EVT_CLOSE(wxDialogBase::OnCloseWindow
)
60 EVT_CHAR_HOOK(wxDialogBase::OnCharHook
)
63 wxDialogLayoutAdapter
* wxDialogBase::sm_layoutAdapter
= NULL
;
64 bool wxDialogBase::sm_layoutAdaptation
= false;
66 void wxDialogBase::Init()
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
;
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
);
81 wxWindow
*wxDialogBase::CheckIfCanBeUsedAsParent(wxWindow
*parent
) const
86 extern WXDLLIMPEXP_DATA_BASE(wxList
) wxPendingDelete
;
87 if ( wxPendingDelete
.Member(parent
) || parent
->IsBeingDeleted() )
89 // this window is being deleted and we shouldn't create any children
94 if ( parent
->HasExtraStyle(wxWS_EX_TRANSIENT
) )
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
101 if ( !parent
->IsShownOnScreen() )
103 // using hidden parent won't work correctly neither
107 // FIXME-VC6: this compiler requires an explicit const cast or it fails
109 if ( const_cast<const wxWindow
*>(parent
) == this )
111 // not sure if this can really happen but it doesn't hurt to guard
112 // against this clearly invalid situation
120 wxDialogBase::GetParentForModalDialog(wxWindow
*parent
, long style
) const
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
)
128 // first try the given parent
130 parent
= CheckIfCanBeUsedAsParent(wxGetTopLevelParent(parent
));
132 // then the currently active window
134 parent
= CheckIfCanBeUsedAsParent(
135 wxGetTopLevelParent(wxGetActiveWindow()));
137 // and finally the application main window
139 parent
= CheckIfCanBeUsedAsParent(wxTheApp
->GetTopWindow());
146 wxSizer
*wxDialogBase::CreateTextSizer(const wxString
& message
)
148 wxTextSizerWrapper
wrapper(this);
150 return CreateTextSizer(message
, wrapper
);
153 wxSizer
*wxDialogBase::CreateTextSizer(const wxString
& message
,
154 wxTextSizerWrapper
& wrapper
)
156 // I admit that this is complete bogus, but it makes
157 // message boxes work for pda screens temporarily..
159 const bool is_pda
= wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA
;
162 widthMax
= wxSystemSettings::GetMetric( wxSYS_SCREEN_X
) - 25;
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("&&"));
171 return wrapper
.CreateSizer(text
, widthMax
);
174 #endif // wxUSE_STATTEXT
176 wxSizer
*wxDialogBase::CreateButtonSizer(long flags
)
178 #ifdef __SMARTPHONE__
179 wxDialog
* dialog
= (wxDialog
*) this;
181 dialog
->SetLeftMenu(wxID_OK
);
183 if ( flags
& wxCANCEL
)
184 dialog
->SetRightMenu(wxID_CANCEL
);
187 dialog
->SetLeftMenu(wxID_YES
);
190 dialog
->SetRightMenu(wxID_NO
);
193 #else // !__SMARTPHONE__
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__
206 return CreateStdDialogButtonSizer(flags
);
210 #endif // __POCKETPC__
212 #else // !wxUSE_BUTTON
216 #endif // wxUSE_BUTTON/!wxUSE_BUTTON
218 #endif // __SMARTPHONE__/!__SMARTPHONE__
221 wxSizer
*wxDialogBase::CreateSeparatedSizer(wxSizer
*sizer
)
223 // Mac Human Interface Guidelines recommend not to use static lines as
225 #if wxUSE_STATLINE && !defined(__WXMAC__)
226 wxBoxSizer
*topsizer
= new wxBoxSizer(wxVERTICAL
);
227 topsizer
->Add(new wxStaticLine(this),
228 wxSizerFlags().Expand().DoubleBorder(wxBOTTOM
));
229 topsizer
->Add(sizer
, wxSizerFlags().Expand());
231 #endif // wxUSE_STATLINE
236 wxSizer
*wxDialogBase::CreateSeparatedButtonSizer(long flags
)
238 wxSizer
*sizer
= CreateButtonSizer(flags
);
242 return CreateSeparatedSizer(sizer
);
247 wxStdDialogButtonSizer
*wxDialogBase::CreateStdDialogButtonSizer( long flags
)
249 wxStdDialogButtonSizer
*sizer
= new wxStdDialogButtonSizer();
252 wxButton
*yes
= NULL
;
257 ok
= new wxButton(this, wxID_OK
);
258 sizer
->AddButton(ok
);
261 if (flags
& wxCANCEL
)
263 wxButton
*cancel
= new wxButton(this, wxID_CANCEL
);
264 sizer
->AddButton(cancel
);
269 yes
= new wxButton(this, wxID_YES
);
270 sizer
->AddButton(yes
);
275 no
= new wxButton(this, wxID_NO
);
276 sizer
->AddButton(no
);
281 wxButton
*apply
= new wxButton(this, wxID_APPLY
);
282 sizer
->AddButton(apply
);
287 wxButton
*close
= new wxButton(this, wxID_CLOSE
);
288 sizer
->AddButton(close
);
293 wxButton
*help
= new wxButton(this, wxID_HELP
);
294 sizer
->AddButton(help
);
297 if (flags
& wxNO_DEFAULT
)
320 SetAffirmativeId(wxID_OK
);
321 else if (flags
& wxYES
)
322 SetAffirmativeId(wxID_YES
);
329 #endif // wxUSE_BUTTON
331 // ----------------------------------------------------------------------------
332 // standard buttons handling
333 // ----------------------------------------------------------------------------
335 void wxDialogBase::EndDialog(int rc
)
343 void wxDialogBase::AcceptAndClose()
345 if ( Validate() && TransferDataFromWindow() )
347 EndDialog(m_affirmativeId
);
351 void wxDialogBase::SetAffirmativeId(int affirmativeId
)
353 m_affirmativeId
= affirmativeId
;
356 void wxDialogBase::SetEscapeId(int escapeId
)
358 m_escapeId
= escapeId
;
361 bool wxDialogBase::EmulateButtonClickIfPresent(int id
)
364 wxButton
*btn
= wxDynamicCast(FindWindow(id
), wxButton
);
366 if ( !btn
|| !btn
->IsEnabled() || !btn
->IsShown() )
369 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, id
);
370 event
.SetEventObject(btn
);
371 btn
->GetEventHandler()->ProcessEvent(event
);
374 #else // !wxUSE_BUTTON
377 #endif // wxUSE_BUTTON/!wxUSE_BUTTON
380 bool wxDialogBase::SendCloseButtonClickEvent()
382 int idCancel
= GetEscapeId();
386 // The user doesn't want this dialog to close "implicitly".
390 // this value is special: it means translate Esc to wxID_CANCEL
391 // but if there is no such button, then fall back to wxID_OK
392 if ( EmulateButtonClickIfPresent(wxID_CANCEL
) )
394 idCancel
= GetAffirmativeId();
398 // translate Esc to button press for the button with given id
399 if ( EmulateButtonClickIfPresent(idCancel
) )
406 bool wxDialogBase::IsEscapeKey(const wxKeyEvent
& event
)
408 // for most platforms, Esc key is used to close the dialogs
409 return event
.GetKeyCode() == WXK_ESCAPE
&&
410 event
.GetModifiers() == wxMOD_NONE
;
413 void wxDialogBase::OnCharHook(wxKeyEvent
& event
)
415 if ( event
.GetKeyCode() == WXK_ESCAPE
)
417 if ( SendCloseButtonClickEvent() )
419 // Skip the call to event.Skip() below, we did handle this key.
427 void wxDialogBase::OnButton(wxCommandEvent
& event
)
429 const int id
= event
.GetId();
430 if ( id
== GetAffirmativeId() )
434 else if ( id
== wxID_APPLY
)
437 TransferDataFromWindow();
439 // TODO: disable the Apply button until things change again
441 else if ( id
== GetEscapeId() ||
442 (id
== wxID_CANCEL
&& GetEscapeId() == wxID_ANY
) )
444 EndDialog(wxID_CANCEL
);
446 else // not a standard button
452 // ----------------------------------------------------------------------------
453 // compatibility methods for supporting the modality API
454 // ----------------------------------------------------------------------------
456 wxDEFINE_EVENT( wxEVT_WINDOW_MODAL_DIALOG_CLOSED
, wxWindowModalDialogEvent
);
458 IMPLEMENT_DYNAMIC_CLASS(wxWindowModalDialogEvent
, wxCommandEvent
)
460 void wxDialogBase::ShowWindowModal ()
463 SendWindowModalDialogEvent ( wxEVT_WINDOW_MODAL_DIALOG_CLOSED
);
466 void wxDialogBase::SendWindowModalDialogEvent ( wxEventType type
)
468 wxWindowModalDialogEvent
event ( type
, GetId());
469 event
.SetEventObject(this);
471 if ( !GetEventHandler()->ProcessEvent(event
) )
473 // the event is not propagated upwards to the parent automatically
474 // because the dialog is a top level window, so do it manually as
475 // in 9 cases of 10 the message must be processed by the dialog
476 // owner and not the dialog itself
477 (void)GetParent()->GetEventHandler()->ProcessEvent(event
);
482 wxDialogModality
wxDialogBase::GetModality() const
484 return IsModal() ? wxDIALOG_MODALITY_APP_MODAL
: wxDIALOG_MODALITY_NONE
;
487 // ----------------------------------------------------------------------------
488 // other event handlers
489 // ----------------------------------------------------------------------------
491 void wxDialogBase::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
493 // We'll send a Cancel message by default, which may close the dialog.
495 // Check for looping if the Cancel event handler calls Close().
497 // VZ: this is horrible and MT-unsafe. Can't we reuse some of these global
498 // lists here? don't dare to change it now, but should be done later!
499 static wxList closing
;
501 if ( closing
.Member(this) )
504 closing
.Append(this);
506 // Note that if a cancel button and handler aren't present in the dialog,
507 // nothing will happen when you close the dialog via the window manager, or
508 // via Close(). We wouldn't want to destroy the dialog by default, since
509 // the dialog may have been created on the stack. However, this does mean
510 // that calling dialog->Close() won't delete the dialog unless the handler
511 // for wxID_CANCEL does so. So use Destroy() if you want to be sure to
512 // destroy the dialog. The default OnCancel (above) simply ends a modal
513 // dialog, and hides a modeless dialog.
514 SendCloseButtonClickEvent();
516 closing
.DeleteObject(this);
519 void wxDialogBase::OnSysColourChanged(wxSysColourChangedEvent
& event
)
522 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
));
529 /// Do the adaptation
530 bool wxDialogBase::DoLayoutAdaptation()
532 if (GetLayoutAdapter())
534 wxWindow
* focusWindow
= wxFindFocusDescendant(this); // from event.h
535 if (GetLayoutAdapter()->DoLayoutAdaptation((wxDialog
*) this))
538 focusWindow
->SetFocus();
548 /// Can we do the adaptation?
549 bool wxDialogBase::CanDoLayoutAdaptation()
551 // Check if local setting overrides the global setting
552 bool layoutEnabled
= (GetLayoutAdaptationMode() == wxDIALOG_ADAPTATION_MODE_ENABLED
) || (IsLayoutAdaptationEnabled() && (GetLayoutAdaptationMode() != wxDIALOG_ADAPTATION_MODE_DISABLED
));
554 return (layoutEnabled
&& !m_layoutAdaptationDone
&& GetLayoutAdaptationLevel() != 0 && GetLayoutAdapter() != NULL
&& GetLayoutAdapter()->CanDoLayoutAdaptation((wxDialog
*) this));
557 /// Set scrolling adapter class, returning old adapter
558 wxDialogLayoutAdapter
* wxDialogBase::SetLayoutAdapter(wxDialogLayoutAdapter
* adapter
)
560 wxDialogLayoutAdapter
* oldLayoutAdapter
= sm_layoutAdapter
;
561 sm_layoutAdapter
= adapter
;
562 return oldLayoutAdapter
;
569 IMPLEMENT_CLASS(wxDialogLayoutAdapter
, wxObject
)
571 IMPLEMENT_CLASS(wxStandardDialogLayoutAdapter
, wxDialogLayoutAdapter
)
573 // Allow for caption size on wxWidgets < 2.9
574 #if defined(__WXGTK__) && !wxCHECK_VERSION(2,9,0)
575 #define wxEXTRA_DIALOG_HEIGHT 30
577 #define wxEXTRA_DIALOG_HEIGHT 0
580 /// Indicate that adaptation should be done
581 bool wxStandardDialogLayoutAdapter::CanDoLayoutAdaptation(wxDialog
* dialog
)
583 if (dialog
->GetSizer())
585 wxSize windowSize
, displaySize
;
586 return MustScroll(dialog
, windowSize
, displaySize
) != 0;
592 bool wxStandardDialogLayoutAdapter::DoLayoutAdaptation(wxDialog
* dialog
)
594 if (dialog
->GetSizer())
597 wxBookCtrlBase
* bookContentWindow
= wxDynamicCast(dialog
->GetContentWindow(), wxBookCtrlBase
);
599 if (bookContentWindow
)
601 // If we have a book control, make all the pages (that use sizers) scrollable
602 wxWindowList windows
;
603 for (size_t i
= 0; i
< bookContentWindow
->GetPageCount(); i
++)
605 wxWindow
* page
= bookContentWindow
->GetPage(i
);
607 wxScrolledWindow
* scrolledWindow
= wxDynamicCast(page
, wxScrolledWindow
);
609 windows
.Append(scrolledWindow
);
610 else if (!scrolledWindow
&& page
->GetSizer())
612 // Create a scrolled window and reparent
613 scrolledWindow
= CreateScrolledWindow(page
);
614 wxSizer
* oldSizer
= page
->GetSizer();
616 wxSizer
* newSizer
= new wxBoxSizer(wxVERTICAL
);
617 newSizer
->Add(scrolledWindow
,1, wxEXPAND
, 0);
619 page
->SetSizer(newSizer
, false /* don't delete the old sizer */);
621 scrolledWindow
->SetSizer(oldSizer
);
623 ReparentControls(page
, scrolledWindow
);
625 windows
.Append(scrolledWindow
);
629 FitWithScrolling(dialog
, windows
);
632 #endif // wxUSE_BOOKCTRL
634 // If we have an arbitrary dialog, create a scrolling area for the main content, and a button sizer
635 // for the main buttons.
636 wxScrolledWindow
* scrolledWindow
= CreateScrolledWindow(dialog
);
638 int buttonSizerBorder
= 0;
640 // First try to find a wxStdDialogButtonSizer
641 wxSizer
* buttonSizer
= FindButtonSizer(true /* find std button sizer */, dialog
, dialog
->GetSizer(), buttonSizerBorder
);
643 // Next try to find a wxBoxSizer containing the controls
644 if (!buttonSizer
&& dialog
->GetLayoutAdaptationLevel() > wxDIALOG_ADAPTATION_STANDARD_SIZER
)
645 buttonSizer
= FindButtonSizer(false /* find ordinary sizer */, dialog
, dialog
->GetSizer(), buttonSizerBorder
);
647 // If we still don't have a button sizer, collect any 'loose' buttons in the layout
648 if (!buttonSizer
&& dialog
->GetLayoutAdaptationLevel() > wxDIALOG_ADAPTATION_ANY_SIZER
)
651 wxStdDialogButtonSizer
* stdButtonSizer
= new wxStdDialogButtonSizer
;
652 buttonSizer
= stdButtonSizer
;
654 FindLooseButtons(dialog
, stdButtonSizer
, dialog
->GetSizer(), count
);
656 stdButtonSizer
->Realize();
659 wxDELETE(buttonSizer
);
663 if (buttonSizerBorder
== 0)
664 buttonSizerBorder
= 5;
666 ReparentControls(dialog
, scrolledWindow
, buttonSizer
);
668 wxBoxSizer
* newTopSizer
= new wxBoxSizer(wxVERTICAL
);
669 wxSizer
* oldSizer
= dialog
->GetSizer();
671 dialog
->SetSizer(newTopSizer
, false /* don't delete old sizer */);
673 newTopSizer
->Add(scrolledWindow
, 1, wxEXPAND
|wxALL
, 0);
675 newTopSizer
->Add(buttonSizer
, 0, wxEXPAND
|wxALL
, buttonSizerBorder
);
677 scrolledWindow
->SetSizer(oldSizer
);
679 FitWithScrolling(dialog
, scrolledWindow
);
683 dialog
->SetLayoutAdaptationDone(true);
687 // Create the scrolled window
688 wxScrolledWindow
* wxStandardDialogLayoutAdapter::CreateScrolledWindow(wxWindow
* parent
)
690 wxScrolledWindow
* scrolledWindow
= new wxScrolledWindow(parent
, wxID_ANY
, wxDefaultPosition
, wxDefaultSize
, wxTAB_TRAVERSAL
|wxVSCROLL
|wxHSCROLL
|wxBORDER_NONE
);
691 return scrolledWindow
;
694 /// Find and remove the button sizer, if any
695 wxSizer
* wxStandardDialogLayoutAdapter::FindButtonSizer(bool stdButtonSizer
, wxDialog
* dialog
, wxSizer
* sizer
, int& retBorder
, int accumlatedBorder
)
697 for ( wxSizerItemList::compatibility_iterator node
= sizer
->GetChildren().GetFirst();
698 node
; node
= node
->GetNext() )
700 wxSizerItem
*item
= node
->GetData();
701 wxSizer
*childSizer
= item
->GetSizer();
705 int newBorder
= accumlatedBorder
;
706 if (item
->GetFlag() & wxALL
)
707 newBorder
+= item
->GetBorder();
709 if (stdButtonSizer
) // find wxStdDialogButtonSizer
711 wxStdDialogButtonSizer
* buttonSizer
= wxDynamicCast(childSizer
, wxStdDialogButtonSizer
);
714 sizer
->Detach(childSizer
);
715 retBorder
= newBorder
;
719 else // find a horizontal box sizer containing standard buttons
721 wxBoxSizer
* buttonSizer
= wxDynamicCast(childSizer
, wxBoxSizer
);
722 if (buttonSizer
&& IsOrdinaryButtonSizer(dialog
, buttonSizer
))
724 sizer
->Detach(childSizer
);
725 retBorder
= newBorder
;
730 wxSizer
* s
= FindButtonSizer(stdButtonSizer
, dialog
, childSizer
, retBorder
, newBorder
);
738 /// Check if this sizer contains standard buttons, and so can be repositioned in the dialog
739 bool wxStandardDialogLayoutAdapter::IsOrdinaryButtonSizer(wxDialog
* dialog
, wxBoxSizer
* sizer
)
741 if (sizer
->GetOrientation() != wxHORIZONTAL
)
744 for ( wxSizerItemList::compatibility_iterator node
= sizer
->GetChildren().GetFirst();
745 node
; node
= node
->GetNext() )
747 wxSizerItem
*item
= node
->GetData();
748 wxButton
*childButton
= wxDynamicCast(item
->GetWindow(), wxButton
);
750 if (childButton
&& IsStandardButton(dialog
, childButton
))
756 /// Check if this is a standard button
757 bool wxStandardDialogLayoutAdapter::IsStandardButton(wxDialog
* dialog
, wxButton
* button
)
759 wxWindowID id
= button
->GetId();
761 return (id
== wxID_OK
|| id
== wxID_CANCEL
|| id
== wxID_YES
|| id
== wxID_NO
|| id
== wxID_SAVE
||
762 id
== wxID_APPLY
|| id
== wxID_HELP
|| id
== wxID_CONTEXT_HELP
|| dialog
->IsMainButtonId(id
));
765 /// Find 'loose' main buttons in the existing layout and add them to the standard dialog sizer
766 bool wxStandardDialogLayoutAdapter::FindLooseButtons(wxDialog
* dialog
, wxStdDialogButtonSizer
* buttonSizer
, wxSizer
* sizer
, int& count
)
768 wxSizerItemList::compatibility_iterator node
= sizer
->GetChildren().GetFirst();
771 wxSizerItemList::compatibility_iterator next
= node
->GetNext();
772 wxSizerItem
*item
= node
->GetData();
773 wxSizer
*childSizer
= item
->GetSizer();
774 wxButton
*childButton
= wxDynamicCast(item
->GetWindow(), wxButton
);
776 if (childButton
&& IsStandardButton(dialog
, childButton
))
778 sizer
->Detach(childButton
);
779 buttonSizer
->AddButton(childButton
);
784 FindLooseButtons(dialog
, buttonSizer
, childSizer
, count
);
791 /// Reparent the controls to the scrolled window
792 void wxStandardDialogLayoutAdapter::ReparentControls(wxWindow
* parent
, wxWindow
* reparentTo
, wxSizer
* buttonSizer
)
794 DoReparentControls(parent
, reparentTo
, buttonSizer
);
797 void wxStandardDialogLayoutAdapter::DoReparentControls(wxWindow
* parent
, wxWindow
* reparentTo
, wxSizer
* buttonSizer
)
799 wxWindowList::compatibility_iterator node
= parent
->GetChildren().GetFirst();
802 wxWindowList::compatibility_iterator next
= node
->GetNext();
804 wxWindow
*win
= node
->GetData();
806 // Don't reparent the scrolled window or buttons in the button sizer
807 if (win
!= reparentTo
&& (!buttonSizer
|| !buttonSizer
->GetItem(win
)))
809 win
->Reparent(reparentTo
);
811 // Restore correct tab order
812 ::SetWindowPos((HWND
) win
->GetHWND(), HWND_BOTTOM
, -1, -1, -1, -1, SWP_NOMOVE
|SWP_NOSIZE
);
820 /// Find whether scrolling will be necessary for the dialog, returning wxVERTICAL, wxHORIZONTAL or both
821 int wxStandardDialogLayoutAdapter::MustScroll(wxDialog
* dialog
, wxSize
& windowSize
, wxSize
& displaySize
)
823 return DoMustScroll(dialog
, windowSize
, displaySize
);
826 /// Find whether scrolling will be necessary for the dialog, returning wxVERTICAL, wxHORIZONTAL or both
827 int wxStandardDialogLayoutAdapter::DoMustScroll(wxDialog
* dialog
, wxSize
& windowSize
, wxSize
& displaySize
)
829 wxSize minWindowSize
= dialog
->GetSizer()->GetMinSize();
830 windowSize
= dialog
->GetSize();
831 windowSize
= wxSize(wxMax(windowSize
.x
, minWindowSize
.x
), wxMax(windowSize
.y
, minWindowSize
.y
));
833 displaySize
= wxDisplay(wxDisplay::GetFromWindow(dialog
)).GetClientArea().GetSize();
835 displaySize
= wxGetClientDisplayRect().GetSize();
840 if (windowSize
.y
>= (displaySize
.y
- wxEXTRA_DIALOG_HEIGHT
))
842 if (windowSize
.x
>= displaySize
.x
)
843 flags
|= wxHORIZONTAL
;
848 // A function to fit the dialog around its contents, and then adjust for screen size.
849 // If scrolled windows are passed, scrolling is enabled in the required orientation(s).
850 bool wxStandardDialogLayoutAdapter::FitWithScrolling(wxDialog
* dialog
, wxWindowList
& windows
)
852 return DoFitWithScrolling(dialog
, windows
);
855 // A function to fit the dialog around its contents, and then adjust for screen size.
856 // If a scrolled window is passed, scrolling is enabled in the required orientation(s).
857 bool wxStandardDialogLayoutAdapter::FitWithScrolling(wxDialog
* dialog
, wxScrolledWindow
* scrolledWindow
)
859 return DoFitWithScrolling(dialog
, scrolledWindow
);
862 // A function to fit the dialog around its contents, and then adjust for screen size.
863 // If a scrolled window is passed, scrolling is enabled in the required orientation(s).
864 bool wxStandardDialogLayoutAdapter::DoFitWithScrolling(wxDialog
* dialog
, wxScrolledWindow
* scrolledWindow
)
866 wxWindowList windows
;
867 windows
.Append(scrolledWindow
);
868 return DoFitWithScrolling(dialog
, windows
);
871 bool wxStandardDialogLayoutAdapter::DoFitWithScrolling(wxDialog
* dialog
, wxWindowList
& windows
)
873 wxSizer
* sizer
= dialog
->GetSizer();
877 sizer
->SetSizeHints(dialog
);
879 wxSize windowSize
, displaySize
;
880 int scrollFlags
= DoMustScroll(dialog
, windowSize
, displaySize
);
881 int scrollBarSize
= 20;
885 int scrollBarExtraX
= 0, scrollBarExtraY
= 0;
886 bool resizeHorizontally
= (scrollFlags
& wxHORIZONTAL
) != 0;
887 bool resizeVertically
= (scrollFlags
& wxVERTICAL
) != 0;
889 if (windows
.GetCount() != 0)
891 // Allow extra for a scrollbar, assuming we resizing in one direction only.
892 if ((resizeVertically
&& !resizeHorizontally
) && (windowSize
.x
< (displaySize
.x
- scrollBarSize
)))
893 scrollBarExtraX
= scrollBarSize
;
894 if ((resizeHorizontally
&& !resizeVertically
) && (windowSize
.y
< (displaySize
.y
- scrollBarSize
)))
895 scrollBarExtraY
= scrollBarSize
;
898 wxWindowList::compatibility_iterator node
= windows
.GetFirst();
901 wxWindow
*win
= node
->GetData();
902 wxScrolledWindow
* scrolledWindow
= wxDynamicCast(win
, wxScrolledWindow
);
905 scrolledWindow
->SetScrollRate(resizeHorizontally
? 10 : 0, resizeVertically
? 10 : 0);
907 if (scrolledWindow
->GetSizer())
908 scrolledWindow
->GetSizer()->Fit(scrolledWindow
);
911 node
= node
->GetNext();
914 wxSize limitTo
= windowSize
+ wxSize(scrollBarExtraX
, scrollBarExtraY
);
915 if (resizeVertically
)
916 limitTo
.y
= displaySize
.y
- wxEXTRA_DIALOG_HEIGHT
;
917 if (resizeHorizontally
)
918 limitTo
.x
= displaySize
.x
;
920 dialog
->SetMinSize(limitTo
);
921 dialog
->SetSize(limitTo
);
923 dialog
->SetSizeHints( limitTo
.x
, limitTo
.y
, dialog
->GetMaxWidth(), dialog
->GetMaxHeight() );
930 * Module to initialise standard adapter
933 class wxDialogLayoutAdapterModule
: public wxModule
935 DECLARE_DYNAMIC_CLASS(wxDialogLayoutAdapterModule
)
937 wxDialogLayoutAdapterModule() {}
938 virtual void OnExit() { delete wxDialogBase::SetLayoutAdapter(NULL
); }
939 virtual bool OnInit() { wxDialogBase::SetLayoutAdapter(new wxStandardDialogLayoutAdapter
); return true; }
942 IMPLEMENT_DYNAMIC_CLASS(wxDialogLayoutAdapterModule
, wxModule
)