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/private/stattext.h"
44 #include "wx/bookctrl.h"
45 #include "wx/scrolwin.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 // helper of GetParentForModalDialog()
82 static bool CanBeUsedAsParent(wxWindow
*parent
)
84 extern WXDLLIMPEXP_DATA_CORE(wxList
) wxPendingDelete
;
86 return !parent
->HasExtraStyle(wxWS_EX_TRANSIENT
) &&
87 parent
->IsShownOnScreen() &&
88 !wxPendingDelete
.Member(parent
) &&
89 !parent
->IsBeingDeleted();
92 wxWindow
*wxDialogBase::GetParentForModalDialog(wxWindow
*parent
) const
94 // creating a parent-less modal dialog will result (under e.g. wxGTK2)
95 // in an unfocused dialog, so try to find a valid parent for it:
97 parent
= wxGetTopLevelParent(parent
);
99 if ( !parent
|| !CanBeUsedAsParent(parent
) )
100 parent
= wxTheApp
->GetTopWindow();
102 if ( parent
&& !CanBeUsedAsParent(parent
) )
104 // can't use this one, it's going to disappear
113 class wxTextSizerWrapper
: public wxTextWrapper
116 wxTextSizerWrapper(wxWindow
*win
)
122 wxSizer
*CreateSizer(const wxString
& text
, int widthMax
)
124 m_sizer
= new wxBoxSizer(wxVERTICAL
);
125 Wrap(m_win
, text
, widthMax
);
130 virtual void OnOutputLine(const wxString
& line
)
134 m_sizer
->Add(new wxStaticText(m_win
, wxID_ANY
, line
));
136 else // empty line, no need to create a control for it
139 m_hLine
= m_win
->GetCharHeight();
141 m_sizer
->Add(5, m_hLine
);
151 wxSizer
*wxDialogBase::CreateTextSizer(const wxString
& message
)
153 // I admit that this is complete bogus, but it makes
154 // message boxes work for pda screens temporarily..
156 const bool is_pda
= wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA
;
159 widthMax
= wxSystemSettings::GetMetric( wxSYS_SCREEN_X
) - 25;
162 // '&' is used as accel mnemonic prefix in the wxWidgets controls but in
163 // the static messages created by CreateTextSizer() (used by wxMessageBox,
164 // for example), we don't want this special meaning, so we need to quote it
165 wxString
text(message
);
166 text
.Replace(_T("&"), _T("&&"));
168 wxTextSizerWrapper
wrapper(this);
170 return wrapper
.CreateSizer(text
, widthMax
);
173 #endif // wxUSE_STATTEXT
175 wxSizer
*wxDialogBase::CreateButtonSizer(long flags
)
177 #ifdef __SMARTPHONE__
178 wxDialog
* dialog
= (wxDialog
*) this;
180 dialog
->SetLeftMenu(wxID_OK
);
182 if ( flags
& wxCANCEL
)
183 dialog
->SetRightMenu(wxID_CANCEL
);
186 dialog
->SetLeftMenu(wxID_YES
);
189 dialog
->SetRightMenu(wxID_NO
);
192 #else // !__SMARTPHONE__
197 // PocketPC guidelines recommend for Ok/Cancel dialogs to use OK button
198 // located inside caption bar and implement Cancel functionality through
199 // Undo outside dialog. As native behaviour this will be default here but
200 // can be replaced with real wxButtons by setting the option below to 1
201 if ( (flags
& ~(wxCANCEL
|wxNO_DEFAULT
)) != wxOK
||
202 wxSystemOptions::GetOptionInt(wxT("wince.dialog.real-ok-cancel")) )
203 #endif // __POCKETPC__
205 return CreateStdDialogButtonSizer(flags
);
209 #endif // __POCKETPC__
211 #else // !wxUSE_BUTTON
215 #endif // wxUSE_BUTTON/!wxUSE_BUTTON
217 #endif // __SMARTPHONE__/!__SMARTPHONE__
220 wxSizer
*wxDialogBase::CreateSeparatedButtonSizer(long flags
)
222 wxSizer
*sizer
= CreateButtonSizer(flags
);
226 // Mac Human Interface Guidelines recommend not to use static lines as
228 #if wxUSE_STATLINE && !defined(__WXMAC__)
229 wxBoxSizer
*topsizer
= new wxBoxSizer(wxVERTICAL
);
230 topsizer
->Add(new wxStaticLine(this),
231 wxSizerFlags().Expand().DoubleBorder(wxBOTTOM
));
232 topsizer
->Add(sizer
, wxSizerFlags().Expand());
234 #endif // wxUSE_STATLINE
241 wxStdDialogButtonSizer
*wxDialogBase::CreateStdDialogButtonSizer( long flags
)
243 wxStdDialogButtonSizer
*sizer
= new wxStdDialogButtonSizer();
246 wxButton
*yes
= NULL
;
251 ok
= new wxButton(this, wxID_OK
);
252 sizer
->AddButton(ok
);
255 if (flags
& wxCANCEL
)
257 wxButton
*cancel
= new wxButton(this, wxID_CANCEL
);
258 sizer
->AddButton(cancel
);
263 yes
= new wxButton(this, wxID_YES
);
264 sizer
->AddButton(yes
);
269 no
= new wxButton(this, wxID_NO
);
270 sizer
->AddButton(no
);
275 wxButton
*apply
= new wxButton(this, wxID_APPLY
);
276 sizer
->AddButton(apply
);
281 wxButton
*close
= new wxButton(this, wxID_CLOSE
);
282 sizer
->AddButton(close
);
287 wxButton
*help
= new wxButton(this, wxID_HELP
);
288 sizer
->AddButton(help
);
291 if (flags
& wxNO_DEFAULT
)
314 SetAffirmativeId(wxID_OK
);
315 else if (flags
& wxYES
)
316 SetAffirmativeId(wxID_YES
);
323 #endif // wxUSE_BUTTON
325 // ----------------------------------------------------------------------------
326 // standard buttons handling
327 // ----------------------------------------------------------------------------
329 void wxDialogBase::EndDialog(int rc
)
337 void wxDialogBase::AcceptAndClose()
339 if ( Validate() && TransferDataFromWindow() )
341 EndDialog(m_affirmativeId
);
345 void wxDialogBase::SetAffirmativeId(int affirmativeId
)
347 m_affirmativeId
= affirmativeId
;
350 void wxDialogBase::SetEscapeId(int escapeId
)
352 m_escapeId
= escapeId
;
355 bool wxDialogBase::EmulateButtonClickIfPresent(int id
)
358 wxButton
*btn
= wxDynamicCast(FindWindow(id
), wxButton
);
360 if ( !btn
|| !btn
->IsEnabled() || !btn
->IsShown() )
363 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, id
);
364 event
.SetEventObject(btn
);
365 btn
->GetEventHandler()->ProcessEvent(event
);
368 #else // !wxUSE_BUTTON
371 #endif // wxUSE_BUTTON/!wxUSE_BUTTON
374 bool wxDialogBase::IsEscapeKey(const wxKeyEvent
& event
)
376 // for most platforms, Esc key is used to close the dialogs
377 return event
.GetKeyCode() == WXK_ESCAPE
&&
378 event
.GetModifiers() == wxMOD_NONE
;
381 void wxDialogBase::OnCharHook(wxKeyEvent
& event
)
383 if ( event
.GetKeyCode() == WXK_ESCAPE
)
385 int idCancel
= GetEscapeId();
389 // don't handle Esc specially at all
393 // this value is special: it means translate Esc to wxID_CANCEL
394 // but if there is no such button, then fall back to wxID_OK
395 if ( EmulateButtonClickIfPresent(wxID_CANCEL
) )
397 idCancel
= GetAffirmativeId();
401 // translate Esc to button press for the button with given id
402 if ( EmulateButtonClickIfPresent(idCancel
) )
410 void wxDialogBase::OnButton(wxCommandEvent
& event
)
412 const int id
= event
.GetId();
413 if ( id
== GetAffirmativeId() )
417 else if ( id
== wxID_APPLY
)
420 TransferDataFromWindow();
422 // TODO: disable the Apply button until things change again
424 else if ( id
== GetEscapeId() ||
425 (id
== wxID_CANCEL
&& GetEscapeId() == wxID_ANY
) )
427 EndDialog(wxID_CANCEL
);
429 else // not a standard button
435 // ----------------------------------------------------------------------------
436 // other event handlers
437 // ----------------------------------------------------------------------------
439 void wxDialogBase::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
441 // We'll send a Cancel message by default, which may close the dialog.
442 // Check for looping if the Cancel event handler calls Close().
444 // Note that if a cancel button and handler aren't present in the dialog,
445 // nothing will happen when you close the dialog via the window manager, or
446 // via Close(). We wouldn't want to destroy the dialog by default, since
447 // the dialog may have been created on the stack. However, this does mean
448 // that calling dialog->Close() won't delete the dialog unless the handler
449 // for wxID_CANCEL does so. So use Destroy() if you want to be sure to
450 // destroy the dialog. The default OnCancel (above) simply ends a modal
451 // dialog, and hides a modeless dialog.
453 // VZ: this is horrible and MT-unsafe. Can't we reuse some of these global
454 // lists here? don't dare to change it now, but should be done later!
455 static wxList closing
;
457 if ( closing
.Member(this) )
460 closing
.Append(this);
462 wxCommandEvent
cancelEvent(wxEVT_COMMAND_BUTTON_CLICKED
, wxID_CANCEL
);
463 cancelEvent
.SetEventObject( this );
464 GetEventHandler()->ProcessEvent(cancelEvent
); // This may close the dialog
466 closing
.DeleteObject(this);
469 void wxDialogBase::OnSysColourChanged(wxSysColourChangedEvent
& event
)
472 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
));
479 /// Do the adaptation
480 bool wxDialogBase::DoLayoutAdaptation()
482 if (GetLayoutAdapter())
484 wxWindow
* focusWindow
= wxFindFocusDescendant(this); // from event.h
485 if (GetLayoutAdapter()->DoLayoutAdaptation((wxDialog
*) this))
488 focusWindow
->SetFocus();
498 /// Can we do the adaptation?
499 bool wxDialogBase::CanDoLayoutAdaptation()
501 // Check if local setting overrides the global setting
502 bool layoutEnabled
= (GetLayoutAdaptationMode() == wxDIALOG_ADAPTATION_MODE_ENABLED
) || (IsLayoutAdaptationEnabled() && (GetLayoutAdaptationMode() != wxDIALOG_ADAPTATION_MODE_DISABLED
));
504 return (layoutEnabled
&& !m_layoutAdaptationDone
&& GetLayoutAdaptationLevel() != 0 && GetLayoutAdapter() != NULL
&& GetLayoutAdapter()->CanDoLayoutAdaptation((wxDialog
*) this));
507 /// Set scrolling adapter class, returning old adapter
508 wxDialogLayoutAdapter
* wxDialogBase::SetLayoutAdapter(wxDialogLayoutAdapter
* adapter
)
510 wxDialogLayoutAdapter
* oldLayoutAdapter
= sm_layoutAdapter
;
511 sm_layoutAdapter
= adapter
;
512 return oldLayoutAdapter
;
519 IMPLEMENT_CLASS(wxDialogLayoutAdapter
, wxObject
)
521 IMPLEMENT_CLASS(wxStandardDialogLayoutAdapter
, wxDialogLayoutAdapter
)
523 // Allow for caption size on wxWidgets < 2.9
524 #if defined(__WXGTK__) && !wxCHECK_VERSION(2,9,0)
525 #define wxEXTRA_DIALOG_HEIGHT 30
527 #define wxEXTRA_DIALOG_HEIGHT 0
530 /// Indicate that adaptation should be done
531 bool wxStandardDialogLayoutAdapter::CanDoLayoutAdaptation(wxDialog
* dialog
)
533 if (dialog
->GetSizer())
535 wxSize windowSize
, displaySize
;
536 return MustScroll(dialog
, windowSize
, displaySize
) != 0;
542 bool wxStandardDialogLayoutAdapter::DoLayoutAdaptation(wxDialog
* dialog
)
544 if (dialog
->GetSizer())
547 wxBookCtrlBase
* bookContentWindow
= wxDynamicCast(dialog
->GetContentWindow(), wxBookCtrlBase
);
549 if (bookContentWindow
)
551 // If we have a book control, make all the pages (that use sizers) scrollable
552 wxWindowList windows
;
553 for (size_t i
= 0; i
< bookContentWindow
->GetPageCount(); i
++)
555 wxWindow
* page
= bookContentWindow
->GetPage(i
);
557 wxScrolledWindow
* scrolledWindow
= wxDynamicCast(page
, wxScrolledWindow
);
559 windows
.Append(scrolledWindow
);
560 else if (!scrolledWindow
&& page
->GetSizer())
562 // Create a scrolled window and reparent
563 scrolledWindow
= CreateScrolledWindow(page
);
564 wxSizer
* oldSizer
= page
->GetSizer();
566 wxSizer
* newSizer
= new wxBoxSizer(wxVERTICAL
);
567 newSizer
->Add(scrolledWindow
,1, wxEXPAND
, 0);
569 page
->SetSizer(newSizer
, false /* don't delete the old sizer */);
571 scrolledWindow
->SetSizer(oldSizer
);
573 ReparentControls(page
, scrolledWindow
);
575 windows
.Append(scrolledWindow
);
579 FitWithScrolling(dialog
, windows
);
582 #endif // wxUSE_BOOKCTRL
584 // If we have an arbitrary dialog, create a scrolling area for the main content, and a button sizer
585 // for the main buttons.
586 wxScrolledWindow
* scrolledWindow
= CreateScrolledWindow(dialog
);
588 int buttonSizerBorder
= 0;
590 // First try to find a wxStdDialogButtonSizer
591 wxSizer
* buttonSizer
= FindButtonSizer(true /* find std button sizer */, dialog
, dialog
->GetSizer(), buttonSizerBorder
);
593 // Next try to find a wxBoxSizer containing the controls
594 if (!buttonSizer
&& dialog
->GetLayoutAdaptationLevel() > wxDIALOG_ADAPTATION_STANDARD_SIZER
)
595 buttonSizer
= FindButtonSizer(false /* find ordinary sizer */, dialog
, dialog
->GetSizer(), buttonSizerBorder
);
597 // If we still don't have a button sizer, collect any 'loose' buttons in the layout
598 if (!buttonSizer
&& dialog
->GetLayoutAdaptationLevel() > wxDIALOG_ADAPTATION_ANY_SIZER
)
601 wxStdDialogButtonSizer
* stdButtonSizer
= new wxStdDialogButtonSizer
;
602 buttonSizer
= stdButtonSizer
;
604 FindLooseButtons(dialog
, stdButtonSizer
, dialog
->GetSizer(), count
);
606 stdButtonSizer
->Realize();
614 if (buttonSizerBorder
== 0)
615 buttonSizerBorder
= 5;
617 ReparentControls(dialog
, scrolledWindow
, buttonSizer
);
619 wxBoxSizer
* newTopSizer
= new wxBoxSizer(wxVERTICAL
);
620 wxSizer
* oldSizer
= dialog
->GetSizer();
622 dialog
->SetSizer(newTopSizer
, false /* don't delete old sizer */);
624 newTopSizer
->Add(scrolledWindow
, 1, wxEXPAND
|wxALL
, 0);
626 newTopSizer
->Add(buttonSizer
, 0, wxEXPAND
|wxALL
, buttonSizerBorder
);
628 scrolledWindow
->SetSizer(oldSizer
);
630 FitWithScrolling(dialog
, scrolledWindow
);
634 dialog
->SetLayoutAdaptationDone(true);
638 // Create the scrolled window
639 wxScrolledWindow
* wxStandardDialogLayoutAdapter::CreateScrolledWindow(wxWindow
* parent
)
641 wxScrolledWindow
* scrolledWindow
= new wxScrolledWindow(parent
, wxID_ANY
, wxDefaultPosition
, wxDefaultSize
, wxTAB_TRAVERSAL
|wxVSCROLL
|wxHSCROLL
|wxBORDER_NONE
);
642 return scrolledWindow
;
645 /// Find and remove the button sizer, if any
646 wxSizer
* wxStandardDialogLayoutAdapter::FindButtonSizer(bool stdButtonSizer
, wxDialog
* dialog
, wxSizer
* sizer
, int& retBorder
, int accumlatedBorder
)
648 for ( wxSizerItemList::compatibility_iterator node
= sizer
->GetChildren().GetFirst();
649 node
; node
= node
->GetNext() )
651 wxSizerItem
*item
= node
->GetData();
652 wxSizer
*childSizer
= item
->GetSizer();
656 int newBorder
= accumlatedBorder
;
657 if (item
->GetFlag() & wxALL
)
658 newBorder
+= item
->GetBorder();
660 if (stdButtonSizer
) // find wxStdDialogButtonSizer
662 wxStdDialogButtonSizer
* buttonSizer
= wxDynamicCast(childSizer
, wxStdDialogButtonSizer
);
665 sizer
->Detach(childSizer
);
666 retBorder
= newBorder
;
670 else // find a horizontal box sizer containing standard buttons
672 wxBoxSizer
* buttonSizer
= wxDynamicCast(childSizer
, wxBoxSizer
);
673 if (buttonSizer
&& IsOrdinaryButtonSizer(dialog
, buttonSizer
))
675 sizer
->Detach(childSizer
);
676 retBorder
= newBorder
;
681 wxSizer
* s
= FindButtonSizer(stdButtonSizer
, dialog
, childSizer
, retBorder
, newBorder
);
689 /// Check if this sizer contains standard buttons, and so can be repositioned in the dialog
690 bool wxStandardDialogLayoutAdapter::IsOrdinaryButtonSizer(wxDialog
* dialog
, wxBoxSizer
* sizer
)
692 if (sizer
->GetOrientation() != wxHORIZONTAL
)
695 for ( wxSizerItemList::compatibility_iterator node
= sizer
->GetChildren().GetFirst();
696 node
; node
= node
->GetNext() )
698 wxSizerItem
*item
= node
->GetData();
699 wxButton
*childButton
= wxDynamicCast(item
->GetWindow(), wxButton
);
701 if (childButton
&& IsStandardButton(dialog
, childButton
))
707 /// Check if this is a standard button
708 bool wxStandardDialogLayoutAdapter::IsStandardButton(wxDialog
* dialog
, wxButton
* button
)
710 wxWindowID id
= button
->GetId();
712 return (id
== wxID_OK
|| id
== wxID_CANCEL
|| id
== wxID_YES
|| id
== wxID_NO
|| id
== wxID_SAVE
||
713 id
== wxID_APPLY
|| id
== wxID_HELP
|| id
== wxID_CONTEXT_HELP
|| dialog
->IsMainButtonId(id
));
716 /// Find 'loose' main buttons in the existing layout and add them to the standard dialog sizer
717 bool wxStandardDialogLayoutAdapter::FindLooseButtons(wxDialog
* dialog
, wxStdDialogButtonSizer
* buttonSizer
, wxSizer
* sizer
, int& count
)
719 wxSizerItemList::compatibility_iterator node
= sizer
->GetChildren().GetFirst();
722 wxSizerItemList::compatibility_iterator next
= node
->GetNext();
723 wxSizerItem
*item
= node
->GetData();
724 wxSizer
*childSizer
= item
->GetSizer();
725 wxButton
*childButton
= wxDynamicCast(item
->GetWindow(), wxButton
);
727 if (childButton
&& IsStandardButton(dialog
, childButton
))
729 sizer
->Detach(childButton
);
730 buttonSizer
->AddButton(childButton
);
735 FindLooseButtons(dialog
, buttonSizer
, childSizer
, count
);
742 /// Reparent the controls to the scrolled window
743 void wxStandardDialogLayoutAdapter::ReparentControls(wxWindow
* parent
, wxWindow
* reparentTo
, wxSizer
* buttonSizer
)
745 DoReparentControls(parent
, reparentTo
, buttonSizer
);
748 void wxStandardDialogLayoutAdapter::DoReparentControls(wxWindow
* parent
, wxWindow
* reparentTo
, wxSizer
* buttonSizer
)
750 wxWindowList::compatibility_iterator node
= parent
->GetChildren().GetFirst();
753 wxWindowList::compatibility_iterator next
= node
->GetNext();
755 wxWindow
*win
= node
->GetData();
757 // Don't reparent the scrolled window or buttons in the button sizer
758 if (win
!= reparentTo
&& (!buttonSizer
|| !buttonSizer
->GetItem(win
)))
760 win
->Reparent(reparentTo
);
762 // Restore correct tab order
763 ::SetWindowPos((HWND
) win
->GetHWND(), HWND_BOTTOM
, -1, -1, -1, -1, SWP_NOMOVE
|SWP_NOSIZE
);
771 /// Find whether scrolling will be necessary for the dialog, returning wxVERTICAL, wxHORIZONTAL or both
772 int wxStandardDialogLayoutAdapter::MustScroll(wxDialog
* dialog
, wxSize
& windowSize
, wxSize
& displaySize
)
774 return DoMustScroll(dialog
, windowSize
, displaySize
);
777 /// Find whether scrolling will be necessary for the dialog, returning wxVERTICAL, wxHORIZONTAL or both
778 int wxStandardDialogLayoutAdapter::DoMustScroll(wxDialog
* dialog
, wxSize
& windowSize
, wxSize
& displaySize
)
780 wxSize minWindowSize
= dialog
->GetSizer()->GetMinSize();
781 windowSize
= dialog
->GetSize();
782 windowSize
= wxSize(wxMax(windowSize
.x
, minWindowSize
.x
), wxMax(windowSize
.y
, minWindowSize
.y
));
784 displaySize
= wxDisplay(wxDisplay::GetFromWindow(dialog
)).GetClientArea().GetSize();
786 displaySize
= wxGetClientDisplayRect().GetSize();
791 if (windowSize
.y
>= (displaySize
.y
- wxEXTRA_DIALOG_HEIGHT
))
793 if (windowSize
.x
>= displaySize
.x
)
794 flags
|= wxHORIZONTAL
;
799 // A function to fit the dialog around its contents, and then adjust for screen size.
800 // If scrolled windows are passed, scrolling is enabled in the required orientation(s).
801 bool wxStandardDialogLayoutAdapter::FitWithScrolling(wxDialog
* dialog
, wxWindowList
& windows
)
803 return DoFitWithScrolling(dialog
, windows
);
806 // A function to fit the dialog around its contents, and then adjust for screen size.
807 // If a scrolled window is passed, scrolling is enabled in the required orientation(s).
808 bool wxStandardDialogLayoutAdapter::FitWithScrolling(wxDialog
* dialog
, wxScrolledWindow
* scrolledWindow
)
810 return DoFitWithScrolling(dialog
, scrolledWindow
);
813 // A function to fit the dialog around its contents, and then adjust for screen size.
814 // If a scrolled window is passed, scrolling is enabled in the required orientation(s).
815 bool wxStandardDialogLayoutAdapter::DoFitWithScrolling(wxDialog
* dialog
, wxScrolledWindow
* scrolledWindow
)
817 wxWindowList windows
;
818 windows
.Append(scrolledWindow
);
819 return DoFitWithScrolling(dialog
, windows
);
822 bool wxStandardDialogLayoutAdapter::DoFitWithScrolling(wxDialog
* dialog
, wxWindowList
& windows
)
824 wxSizer
* sizer
= dialog
->GetSizer();
828 sizer
->SetSizeHints(dialog
);
830 wxSize windowSize
, displaySize
;
831 int scrollFlags
= DoMustScroll(dialog
, windowSize
, displaySize
);
832 int scrollBarSize
= 20;
836 int scrollBarExtraX
= 0, scrollBarExtraY
= 0;
837 bool resizeHorizontally
= (scrollFlags
& wxHORIZONTAL
) != 0;
838 bool resizeVertically
= (scrollFlags
& wxVERTICAL
) != 0;
840 if (windows
.GetCount() != 0)
842 // Allow extra for a scrollbar, assuming we resizing in one direction only.
843 if ((resizeVertically
&& !resizeHorizontally
) && (windowSize
.x
< (displaySize
.x
- scrollBarSize
)))
844 scrollBarExtraX
= scrollBarSize
;
845 if ((resizeHorizontally
&& !resizeVertically
) && (windowSize
.y
< (displaySize
.y
- scrollBarSize
)))
846 scrollBarExtraY
= scrollBarSize
;
849 wxWindowList::compatibility_iterator node
= windows
.GetFirst();
852 wxWindow
*win
= node
->GetData();
853 wxScrolledWindow
* scrolledWindow
= wxDynamicCast(win
, wxScrolledWindow
);
856 scrolledWindow
->SetScrollRate(resizeHorizontally
? 10 : 0, resizeVertically
? 10 : 0);
858 if (scrolledWindow
->GetSizer())
859 scrolledWindow
->GetSizer()->Fit(scrolledWindow
);
862 node
= node
->GetNext();
865 wxSize limitTo
= windowSize
+ wxSize(scrollBarExtraX
, scrollBarExtraY
);
866 if (resizeVertically
)
867 limitTo
.y
= displaySize
.y
- wxEXTRA_DIALOG_HEIGHT
;
868 if (resizeHorizontally
)
869 limitTo
.x
= displaySize
.x
;
871 dialog
->SetMinSize(limitTo
);
872 dialog
->SetSize(limitTo
);
874 dialog
->SetSizeHints( limitTo
.x
, limitTo
.y
, dialog
->GetMaxWidth(), dialog
->GetMaxHeight() );
881 * Module to initialise standard adapter
884 class wxDialogLayoutAdapterModule
: public wxModule
886 DECLARE_DYNAMIC_CLASS(wxDialogLayoutAdapterModule
)
888 wxDialogLayoutAdapterModule() {}
889 virtual void OnExit() { delete wxDialogBase::SetLayoutAdapter(NULL
); }
890 virtual bool OnInit() { wxDialogBase::SetLayoutAdapter(new wxStandardDialogLayoutAdapter
); return true; }
893 IMPLEMENT_DYNAMIC_CLASS(wxDialogLayoutAdapterModule
, wxModule
)