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"
46 #include "wx/modalhook.h"
49 #include "wx/display.h"
52 extern WXDLLEXPORT_DATA(const char) wxDialogNameStr
[] = "dialog";
54 // ----------------------------------------------------------------------------
56 // ----------------------------------------------------------------------------
58 wxDEFINE_FLAGS( wxDialogStyle
)
59 wxBEGIN_FLAGS( wxDialogStyle
)
60 // new style border flags, we put them first to
61 // use them for streaming out
62 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
63 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
64 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
65 wxFLAGS_MEMBER(wxBORDER_RAISED
)
66 wxFLAGS_MEMBER(wxBORDER_STATIC
)
67 wxFLAGS_MEMBER(wxBORDER_NONE
)
69 // old style border flags
70 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
71 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
72 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
73 wxFLAGS_MEMBER(wxRAISED_BORDER
)
74 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
75 wxFLAGS_MEMBER(wxNO_BORDER
)
77 // standard window styles
78 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
79 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
82 wxFLAGS_MEMBER(wxWS_EX_VALIDATE_RECURSIVELY
)
83 wxFLAGS_MEMBER(wxSTAY_ON_TOP
)
84 wxFLAGS_MEMBER(wxCAPTION
)
85 #if WXWIN_COMPATIBILITY_2_6
86 wxFLAGS_MEMBER(wxTHICK_FRAME
)
87 #endif // WXWIN_COMPATIBILITY_2_6
88 wxFLAGS_MEMBER(wxSYSTEM_MENU
)
89 wxFLAGS_MEMBER(wxRESIZE_BORDER
)
90 #if WXWIN_COMPATIBILITY_2_6
91 wxFLAGS_MEMBER(wxRESIZE_BOX
)
92 #endif // WXWIN_COMPATIBILITY_2_6
93 wxFLAGS_MEMBER(wxCLOSE_BOX
)
94 wxFLAGS_MEMBER(wxMAXIMIZE_BOX
)
95 wxFLAGS_MEMBER(wxMINIMIZE_BOX
)
96 wxEND_FLAGS( wxDialogStyle
)
98 wxIMPLEMENT_DYNAMIC_CLASS_XTI(wxDialog
, wxTopLevelWindow
, "wx/dialog.h")
100 wxBEGIN_PROPERTIES_TABLE(wxDialog
)
101 wxPROPERTY( Title
, wxString
, SetTitle
, GetTitle
, wxString(), \
102 0 /*flags*/, wxT("Helpstring"), wxT("group"))
104 wxPROPERTY_FLAGS( WindowStyle
, wxDialogStyle
, long, SetWindowStyleFlag
, \
105 GetWindowStyleFlag
, wxEMPTY_PARAMETER_VALUE
, 0 /*flags*/, \
106 wxT("Helpstring"), wxT("group")) // style
107 wxEND_PROPERTIES_TABLE()
109 wxEMPTY_HANDLERS_TABLE(wxDialog
)
111 wxCONSTRUCTOR_6( wxDialog
, wxWindow
*, Parent
, wxWindowID
, Id
, \
112 wxString
, Title
, wxPoint
, Position
, wxSize
, Size
, long, WindowStyle
)
114 // ----------------------------------------------------------------------------
116 // ----------------------------------------------------------------------------
118 BEGIN_EVENT_TABLE(wxDialogBase
, wxTopLevelWindow
)
119 EVT_BUTTON(wxID_ANY
, wxDialogBase::OnButton
)
121 EVT_CLOSE(wxDialogBase::OnCloseWindow
)
123 EVT_CHAR_HOOK(wxDialogBase::OnCharHook
)
126 wxDialogLayoutAdapter
* wxDialogBase::sm_layoutAdapter
= NULL
;
127 bool wxDialogBase::sm_layoutAdaptation
= false;
129 void wxDialogBase::Init()
132 m_affirmativeId
= wxID_OK
;
133 m_escapeId
= wxID_ANY
;
134 m_layoutAdaptationLevel
= 3;
135 m_layoutAdaptationDone
= FALSE
;
136 m_layoutAdaptationMode
= wxDIALOG_ADAPTATION_MODE_DEFAULT
;
138 // the dialogs have this flag on by default to prevent the events from the
139 // dialog controls from reaching the parent frame which is usually
140 // undesirable and can lead to unexpected and hard to find bugs
141 SetExtraStyle(GetExtraStyle() | wxWS_EX_BLOCK_EVENTS
);
144 wxWindow
*wxDialogBase::CheckIfCanBeUsedAsParent(wxWindow
*parent
) const
149 extern WXDLLIMPEXP_DATA_BASE(wxList
) wxPendingDelete
;
150 if ( wxPendingDelete
.Member(parent
) || parent
->IsBeingDeleted() )
152 // this window is being deleted and we shouldn't create any children
157 if ( parent
->HasExtraStyle(wxWS_EX_TRANSIENT
) )
159 // this window is not being deleted yet but it's going to disappear
160 // soon so still don't parent this window under it
164 if ( !parent
->IsShownOnScreen() )
166 // using hidden parent won't work correctly neither
170 // FIXME-VC6: this compiler requires an explicit const cast or it fails
172 if ( const_cast<const wxWindow
*>(parent
) == this )
174 // not sure if this can really happen but it doesn't hurt to guard
175 // against this clearly invalid situation
183 wxDialogBase::GetParentForModalDialog(wxWindow
*parent
, long style
) const
185 // creating a parent-less modal dialog will result (under e.g. wxGTK2)
186 // in an unfocused dialog, so try to find a valid parent for it unless we
187 // were explicitly asked not to
188 if ( style
& wxDIALOG_NO_PARENT
)
191 // first try the given parent
193 parent
= CheckIfCanBeUsedAsParent(wxGetTopLevelParent(parent
));
195 // then the currently active window
197 parent
= CheckIfCanBeUsedAsParent(
198 wxGetTopLevelParent(wxGetActiveWindow()));
200 // and finally the application main window
202 parent
= CheckIfCanBeUsedAsParent(wxTheApp
->GetTopWindow());
209 wxSizer
*wxDialogBase::CreateTextSizer(const wxString
& message
)
211 wxTextSizerWrapper
wrapper(this);
213 return CreateTextSizer(message
, wrapper
);
216 wxSizer
*wxDialogBase::CreateTextSizer(const wxString
& message
,
217 wxTextSizerWrapper
& wrapper
)
219 // I admit that this is complete bogus, but it makes
220 // message boxes work for pda screens temporarily..
222 const bool is_pda
= wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA
;
225 widthMax
= wxSystemSettings::GetMetric( wxSYS_SCREEN_X
) - 25;
228 return wrapper
.CreateSizer(message
, widthMax
);
231 #endif // wxUSE_STATTEXT
233 wxSizer
*wxDialogBase::CreateButtonSizer(long flags
)
235 #ifdef __SMARTPHONE__
236 wxDialog
* dialog
= (wxDialog
*) this;
238 dialog
->SetLeftMenu(wxID_OK
);
240 if ( flags
& wxCANCEL
)
241 dialog
->SetRightMenu(wxID_CANCEL
);
244 dialog
->SetLeftMenu(wxID_YES
);
247 dialog
->SetRightMenu(wxID_NO
);
250 #else // !__SMARTPHONE__
255 // PocketPC guidelines recommend for Ok/Cancel dialogs to use OK button
256 // located inside caption bar and implement Cancel functionality through
257 // Undo outside dialog. As native behaviour this will be default here but
258 // can be replaced with real wxButtons by setting the option below to 1
259 if ( (flags
& ~(wxCANCEL
|wxNO_DEFAULT
)) != wxOK
||
260 wxSystemOptions::GetOptionInt(wxT("wince.dialog.real-ok-cancel")) )
261 #endif // __POCKETPC__
263 return CreateStdDialogButtonSizer(flags
);
267 #endif // __POCKETPC__
269 #else // !wxUSE_BUTTON
273 #endif // wxUSE_BUTTON/!wxUSE_BUTTON
275 #endif // __SMARTPHONE__/!__SMARTPHONE__
278 wxSizer
*wxDialogBase::CreateSeparatedSizer(wxSizer
*sizer
)
280 // Mac Human Interface Guidelines recommend not to use static lines as
282 #if wxUSE_STATLINE && !defined(__WXMAC__)
283 wxBoxSizer
*topsizer
= new wxBoxSizer(wxVERTICAL
);
284 topsizer
->Add(new wxStaticLine(this),
285 wxSizerFlags().Expand().DoubleBorder(wxBOTTOM
));
286 topsizer
->Add(sizer
, wxSizerFlags().Expand());
288 #endif // wxUSE_STATLINE
293 wxSizer
*wxDialogBase::CreateSeparatedButtonSizer(long flags
)
295 wxSizer
*sizer
= CreateButtonSizer(flags
);
299 return CreateSeparatedSizer(sizer
);
304 wxStdDialogButtonSizer
*wxDialogBase::CreateStdDialogButtonSizer( long flags
)
306 wxStdDialogButtonSizer
*sizer
= new wxStdDialogButtonSizer();
309 wxButton
*yes
= NULL
;
314 ok
= new wxButton(this, wxID_OK
);
315 sizer
->AddButton(ok
);
318 if (flags
& wxCANCEL
)
320 wxButton
*cancel
= new wxButton(this, wxID_CANCEL
);
321 sizer
->AddButton(cancel
);
326 yes
= new wxButton(this, wxID_YES
);
327 sizer
->AddButton(yes
);
332 no
= new wxButton(this, wxID_NO
);
333 sizer
->AddButton(no
);
338 wxButton
*apply
= new wxButton(this, wxID_APPLY
);
339 sizer
->AddButton(apply
);
344 wxButton
*close
= new wxButton(this, wxID_CLOSE
);
345 sizer
->AddButton(close
);
350 wxButton
*help
= new wxButton(this, wxID_HELP
);
351 sizer
->AddButton(help
);
354 if (flags
& wxNO_DEFAULT
)
377 SetAffirmativeId(wxID_OK
);
378 else if (flags
& wxYES
)
379 SetAffirmativeId(wxID_YES
);
386 #endif // wxUSE_BUTTON
388 // ----------------------------------------------------------------------------
389 // standard buttons handling
390 // ----------------------------------------------------------------------------
392 void wxDialogBase::EndDialog(int rc
)
400 void wxDialogBase::AcceptAndClose()
402 if ( Validate() && TransferDataFromWindow() )
404 EndDialog(m_affirmativeId
);
408 void wxDialogBase::SetAffirmativeId(int affirmativeId
)
410 m_affirmativeId
= affirmativeId
;
413 void wxDialogBase::SetEscapeId(int escapeId
)
415 m_escapeId
= escapeId
;
418 bool wxDialogBase::EmulateButtonClickIfPresent(int id
)
421 wxButton
*btn
= wxDynamicCast(FindWindow(id
), wxButton
);
423 if ( !btn
|| !btn
->IsEnabled() || !btn
->IsShown() )
426 wxCommandEvent
event(wxEVT_BUTTON
, id
);
427 event
.SetEventObject(btn
);
428 btn
->GetEventHandler()->ProcessEvent(event
);
431 #else // !wxUSE_BUTTON
434 #endif // wxUSE_BUTTON/!wxUSE_BUTTON
437 bool wxDialogBase::SendCloseButtonClickEvent()
439 int idCancel
= GetEscapeId();
443 // The user doesn't want this dialog to close "implicitly".
447 // this value is special: it means translate Esc to wxID_CANCEL
448 // but if there is no such button, then fall back to wxID_OK
449 if ( EmulateButtonClickIfPresent(wxID_CANCEL
) )
451 idCancel
= GetAffirmativeId();
455 // translate Esc to button press for the button with given id
456 if ( EmulateButtonClickIfPresent(idCancel
) )
463 bool wxDialogBase::IsEscapeKey(const wxKeyEvent
& event
)
465 // For most platforms, Esc key is used to close the dialogs.
467 // Notice that we intentionally don't check for modifiers here, Shift-Esc,
468 // Alt-Esc and so on still close the dialog, typically.
469 return event
.GetKeyCode() == WXK_ESCAPE
;
472 void wxDialogBase::OnCharHook(wxKeyEvent
& event
)
474 if ( IsEscapeKey(event
) )
476 if ( SendCloseButtonClickEvent() )
478 // Skip the call to event.Skip() below, we did handle this key.
486 void wxDialogBase::OnButton(wxCommandEvent
& event
)
488 const int id
= event
.GetId();
489 if ( id
== GetAffirmativeId() )
493 else if ( id
== wxID_APPLY
)
496 TransferDataFromWindow();
498 // TODO: disable the Apply button until things change again
500 else if ( id
== GetEscapeId() ||
501 (id
== wxID_CANCEL
&& GetEscapeId() == wxID_ANY
) )
503 EndDialog(wxID_CANCEL
);
505 else // not a standard button
511 // ----------------------------------------------------------------------------
512 // compatibility methods for supporting the modality API
513 // ----------------------------------------------------------------------------
515 wxDEFINE_EVENT( wxEVT_WINDOW_MODAL_DIALOG_CLOSED
, wxWindowModalDialogEvent
);
517 IMPLEMENT_DYNAMIC_CLASS(wxWindowModalDialogEvent
, wxCommandEvent
)
519 void wxDialogBase::ShowWindowModal ()
522 SendWindowModalDialogEvent ( wxEVT_WINDOW_MODAL_DIALOG_CLOSED
);
525 void wxDialogBase::SendWindowModalDialogEvent ( wxEventType type
)
527 wxWindowModalDialogEvent
event ( type
, GetId());
528 event
.SetEventObject(this);
530 if ( !GetEventHandler()->ProcessEvent(event
) )
532 // the event is not propagated upwards to the parent automatically
533 // because the dialog is a top level window, so do it manually as
534 // in 9 cases of 10 the message must be processed by the dialog
535 // owner and not the dialog itself
536 (void)GetParent()->GetEventHandler()->ProcessEvent(event
);
541 wxDialogModality
wxDialogBase::GetModality() const
543 return IsModal() ? wxDIALOG_MODALITY_APP_MODAL
: wxDIALOG_MODALITY_NONE
;
546 // ----------------------------------------------------------------------------
547 // other event handlers
548 // ----------------------------------------------------------------------------
550 void wxDialogBase::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
552 // We'll send a Cancel message by default, which may close the dialog.
554 // Check for looping if the Cancel event handler calls Close().
556 // VZ: this is horrible and MT-unsafe. Can't we reuse some of these global
557 // lists here? don't dare to change it now, but should be done later!
558 static wxList closing
;
560 if ( closing
.Member(this) )
563 closing
.Append(this);
565 if ( !SendCloseButtonClickEvent() )
567 // If the handler didn't close the dialog (e.g. because there is no
568 // button with matching id) we still want to close it when the user
569 // clicks the "x" button in the title bar, otherwise we shouldn't even
570 // have put it there.
572 // Notice that using wxID_CLOSE might have been a better choice but we
573 // use wxID_CANCEL for compatibility reasons.
574 EndDialog(wxID_CANCEL
);
577 closing
.DeleteObject(this);
580 void wxDialogBase::OnSysColourChanged(wxSysColourChangedEvent
& event
)
583 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
));
590 /// Do the adaptation
591 bool wxDialogBase::DoLayoutAdaptation()
593 if (GetLayoutAdapter())
595 wxWindow
* focusWindow
= wxFindFocusDescendant(this); // from event.h
596 if (GetLayoutAdapter()->DoLayoutAdaptation((wxDialog
*) this))
599 focusWindow
->SetFocus();
609 /// Can we do the adaptation?
610 bool wxDialogBase::CanDoLayoutAdaptation()
612 // Check if local setting overrides the global setting
613 bool layoutEnabled
= (GetLayoutAdaptationMode() == wxDIALOG_ADAPTATION_MODE_ENABLED
) || (IsLayoutAdaptationEnabled() && (GetLayoutAdaptationMode() != wxDIALOG_ADAPTATION_MODE_DISABLED
));
615 return (layoutEnabled
&& !m_layoutAdaptationDone
&& GetLayoutAdaptationLevel() != 0 && GetLayoutAdapter() != NULL
&& GetLayoutAdapter()->CanDoLayoutAdaptation((wxDialog
*) this));
618 /// Set scrolling adapter class, returning old adapter
619 wxDialogLayoutAdapter
* wxDialogBase::SetLayoutAdapter(wxDialogLayoutAdapter
* adapter
)
621 wxDialogLayoutAdapter
* oldLayoutAdapter
= sm_layoutAdapter
;
622 sm_layoutAdapter
= adapter
;
623 return oldLayoutAdapter
;
630 IMPLEMENT_CLASS(wxDialogLayoutAdapter
, wxObject
)
632 IMPLEMENT_CLASS(wxStandardDialogLayoutAdapter
, wxDialogLayoutAdapter
)
634 // Allow for caption size on wxWidgets < 2.9
635 #if defined(__WXGTK__) && !wxCHECK_VERSION(2,9,0)
636 #define wxEXTRA_DIALOG_HEIGHT 30
638 #define wxEXTRA_DIALOG_HEIGHT 0
641 /// Indicate that adaptation should be done
642 bool wxStandardDialogLayoutAdapter::CanDoLayoutAdaptation(wxDialog
* dialog
)
644 if (dialog
->GetSizer())
646 wxSize windowSize
, displaySize
;
647 return MustScroll(dialog
, windowSize
, displaySize
) != 0;
653 bool wxStandardDialogLayoutAdapter::DoLayoutAdaptation(wxDialog
* dialog
)
655 if (dialog
->GetSizer())
658 wxBookCtrlBase
* bookContentWindow
= wxDynamicCast(dialog
->GetContentWindow(), wxBookCtrlBase
);
660 if (bookContentWindow
)
662 // If we have a book control, make all the pages (that use sizers) scrollable
663 wxWindowList windows
;
664 for (size_t i
= 0; i
< bookContentWindow
->GetPageCount(); i
++)
666 wxWindow
* page
= bookContentWindow
->GetPage(i
);
668 wxScrolledWindow
* scrolledWindow
= wxDynamicCast(page
, wxScrolledWindow
);
670 windows
.Append(scrolledWindow
);
671 else if (!scrolledWindow
&& page
->GetSizer())
673 // Create a scrolled window and reparent
674 scrolledWindow
= CreateScrolledWindow(page
);
675 wxSizer
* oldSizer
= page
->GetSizer();
677 wxSizer
* newSizer
= new wxBoxSizer(wxVERTICAL
);
678 newSizer
->Add(scrolledWindow
,1, wxEXPAND
, 0);
680 page
->SetSizer(newSizer
, false /* don't delete the old sizer */);
682 scrolledWindow
->SetSizer(oldSizer
);
684 ReparentControls(page
, scrolledWindow
);
686 windows
.Append(scrolledWindow
);
690 FitWithScrolling(dialog
, windows
);
693 #endif // wxUSE_BOOKCTRL
696 // If we have an arbitrary dialog, create a scrolling area for the main content, and a button sizer
697 // for the main buttons.
698 wxScrolledWindow
* scrolledWindow
= CreateScrolledWindow(dialog
);
700 int buttonSizerBorder
= 0;
702 // First try to find a wxStdDialogButtonSizer
703 wxSizer
* buttonSizer
= FindButtonSizer(true /* find std button sizer */, dialog
, dialog
->GetSizer(), buttonSizerBorder
);
705 // Next try to find a wxBoxSizer containing the controls
706 if (!buttonSizer
&& dialog
->GetLayoutAdaptationLevel() > wxDIALOG_ADAPTATION_STANDARD_SIZER
)
707 buttonSizer
= FindButtonSizer(false /* find ordinary sizer */, dialog
, dialog
->GetSizer(), buttonSizerBorder
);
709 // If we still don't have a button sizer, collect any 'loose' buttons in the layout
710 if (!buttonSizer
&& dialog
->GetLayoutAdaptationLevel() > wxDIALOG_ADAPTATION_ANY_SIZER
)
713 wxStdDialogButtonSizer
* stdButtonSizer
= new wxStdDialogButtonSizer
;
714 buttonSizer
= stdButtonSizer
;
716 FindLooseButtons(dialog
, stdButtonSizer
, dialog
->GetSizer(), count
);
718 stdButtonSizer
->Realize();
721 wxDELETE(buttonSizer
);
725 if (buttonSizerBorder
== 0)
726 buttonSizerBorder
= 5;
728 ReparentControls(dialog
, scrolledWindow
, buttonSizer
);
730 wxBoxSizer
* newTopSizer
= new wxBoxSizer(wxVERTICAL
);
731 wxSizer
* oldSizer
= dialog
->GetSizer();
733 dialog
->SetSizer(newTopSizer
, false /* don't delete old sizer */);
735 newTopSizer
->Add(scrolledWindow
, 1, wxEXPAND
|wxALL
, 0);
737 newTopSizer
->Add(buttonSizer
, 0, wxEXPAND
|wxALL
, buttonSizerBorder
);
739 scrolledWindow
->SetSizer(oldSizer
);
741 FitWithScrolling(dialog
, scrolledWindow
);
742 #endif // wxUSE_BUTTON
746 dialog
->SetLayoutAdaptationDone(true);
750 // Create the scrolled window
751 wxScrolledWindow
* wxStandardDialogLayoutAdapter::CreateScrolledWindow(wxWindow
* parent
)
753 wxScrolledWindow
* scrolledWindow
= new wxScrolledWindow(parent
, wxID_ANY
, wxDefaultPosition
, wxDefaultSize
, wxTAB_TRAVERSAL
|wxVSCROLL
|wxHSCROLL
|wxBORDER_NONE
);
754 return scrolledWindow
;
759 /// Find and remove the button sizer, if any
760 wxSizer
* wxStandardDialogLayoutAdapter::FindButtonSizer(bool stdButtonSizer
, wxDialog
* dialog
, wxSizer
* sizer
, int& retBorder
, int accumlatedBorder
)
762 for ( wxSizerItemList::compatibility_iterator node
= sizer
->GetChildren().GetFirst();
763 node
; node
= node
->GetNext() )
765 wxSizerItem
*item
= node
->GetData();
766 wxSizer
*childSizer
= item
->GetSizer();
770 int newBorder
= accumlatedBorder
;
771 if (item
->GetFlag() & wxALL
)
772 newBorder
+= item
->GetBorder();
774 if (stdButtonSizer
) // find wxStdDialogButtonSizer
776 wxStdDialogButtonSizer
* buttonSizer
= wxDynamicCast(childSizer
, wxStdDialogButtonSizer
);
779 sizer
->Detach(childSizer
);
780 retBorder
= newBorder
;
784 else // find a horizontal box sizer containing standard buttons
786 wxBoxSizer
* buttonSizer
= wxDynamicCast(childSizer
, wxBoxSizer
);
787 if (buttonSizer
&& IsOrdinaryButtonSizer(dialog
, buttonSizer
))
789 sizer
->Detach(childSizer
);
790 retBorder
= newBorder
;
795 wxSizer
* s
= FindButtonSizer(stdButtonSizer
, dialog
, childSizer
, retBorder
, newBorder
);
803 /// Check if this sizer contains standard buttons, and so can be repositioned in the dialog
804 bool wxStandardDialogLayoutAdapter::IsOrdinaryButtonSizer(wxDialog
* dialog
, wxBoxSizer
* sizer
)
806 if (sizer
->GetOrientation() != wxHORIZONTAL
)
809 for ( wxSizerItemList::compatibility_iterator node
= sizer
->GetChildren().GetFirst();
810 node
; node
= node
->GetNext() )
812 wxSizerItem
*item
= node
->GetData();
813 wxButton
*childButton
= wxDynamicCast(item
->GetWindow(), wxButton
);
815 if (childButton
&& IsStandardButton(dialog
, childButton
))
821 /// Check if this is a standard button
822 bool wxStandardDialogLayoutAdapter::IsStandardButton(wxDialog
* dialog
, wxButton
* button
)
824 wxWindowID id
= button
->GetId();
826 return (id
== wxID_OK
|| id
== wxID_CANCEL
|| id
== wxID_YES
|| id
== wxID_NO
|| id
== wxID_SAVE
||
827 id
== wxID_APPLY
|| id
== wxID_HELP
|| id
== wxID_CONTEXT_HELP
|| dialog
->IsMainButtonId(id
));
830 /// Find 'loose' main buttons in the existing layout and add them to the standard dialog sizer
831 bool wxStandardDialogLayoutAdapter::FindLooseButtons(wxDialog
* dialog
, wxStdDialogButtonSizer
* buttonSizer
, wxSizer
* sizer
, int& count
)
833 wxSizerItemList::compatibility_iterator node
= sizer
->GetChildren().GetFirst();
836 wxSizerItemList::compatibility_iterator next
= node
->GetNext();
837 wxSizerItem
*item
= node
->GetData();
838 wxSizer
*childSizer
= item
->GetSizer();
839 wxButton
*childButton
= wxDynamicCast(item
->GetWindow(), wxButton
);
841 if (childButton
&& IsStandardButton(dialog
, childButton
))
843 sizer
->Detach(childButton
);
844 buttonSizer
->AddButton(childButton
);
849 FindLooseButtons(dialog
, buttonSizer
, childSizer
, count
);
856 #endif // wxUSE_BUTTON
858 /// Reparent the controls to the scrolled window
859 void wxStandardDialogLayoutAdapter::ReparentControls(wxWindow
* parent
, wxWindow
* reparentTo
, wxSizer
* buttonSizer
)
861 DoReparentControls(parent
, reparentTo
, buttonSizer
);
864 void wxStandardDialogLayoutAdapter::DoReparentControls(wxWindow
* parent
, wxWindow
* reparentTo
, wxSizer
* buttonSizer
)
866 wxWindowList::compatibility_iterator node
= parent
->GetChildren().GetFirst();
869 wxWindowList::compatibility_iterator next
= node
->GetNext();
871 wxWindow
*win
= node
->GetData();
873 // Don't reparent the scrolled window or buttons in the button sizer
874 if (win
!= reparentTo
&& (!buttonSizer
|| !buttonSizer
->GetItem(win
)))
876 win
->Reparent(reparentTo
);
878 // Restore correct tab order
879 ::SetWindowPos((HWND
) win
->GetHWND(), HWND_BOTTOM
, -1, -1, -1, -1, SWP_NOMOVE
|SWP_NOSIZE
);
887 /// Find whether scrolling will be necessary for the dialog, returning wxVERTICAL, wxHORIZONTAL or both
888 int wxStandardDialogLayoutAdapter::MustScroll(wxDialog
* dialog
, wxSize
& windowSize
, wxSize
& displaySize
)
890 return DoMustScroll(dialog
, windowSize
, displaySize
);
893 /// Find whether scrolling will be necessary for the dialog, returning wxVERTICAL, wxHORIZONTAL or both
894 int wxStandardDialogLayoutAdapter::DoMustScroll(wxDialog
* dialog
, wxSize
& windowSize
, wxSize
& displaySize
)
896 wxSize minWindowSize
= dialog
->GetSizer()->GetMinSize();
897 windowSize
= dialog
->GetSize();
898 windowSize
= wxSize(wxMax(windowSize
.x
, minWindowSize
.x
), wxMax(windowSize
.y
, minWindowSize
.y
));
900 displaySize
= wxDisplay(wxDisplay::GetFromWindow(dialog
)).GetClientArea().GetSize();
902 displaySize
= wxGetClientDisplayRect().GetSize();
907 if (windowSize
.y
>= (displaySize
.y
- wxEXTRA_DIALOG_HEIGHT
))
909 if (windowSize
.x
>= displaySize
.x
)
910 flags
|= wxHORIZONTAL
;
915 // A function to fit the dialog around its contents, and then adjust for screen size.
916 // If scrolled windows are passed, scrolling is enabled in the required orientation(s).
917 bool wxStandardDialogLayoutAdapter::FitWithScrolling(wxDialog
* dialog
, wxWindowList
& windows
)
919 return DoFitWithScrolling(dialog
, windows
);
922 // A function to fit the dialog around its contents, and then adjust for screen size.
923 // If a scrolled window is passed, scrolling is enabled in the required orientation(s).
924 bool wxStandardDialogLayoutAdapter::FitWithScrolling(wxDialog
* dialog
, wxScrolledWindow
* scrolledWindow
)
926 return DoFitWithScrolling(dialog
, scrolledWindow
);
929 // A function to fit the dialog around its contents, and then adjust for screen size.
930 // If a scrolled window is passed, scrolling is enabled in the required orientation(s).
931 bool wxStandardDialogLayoutAdapter::DoFitWithScrolling(wxDialog
* dialog
, wxScrolledWindow
* scrolledWindow
)
933 wxWindowList windows
;
934 windows
.Append(scrolledWindow
);
935 return DoFitWithScrolling(dialog
, windows
);
938 bool wxStandardDialogLayoutAdapter::DoFitWithScrolling(wxDialog
* dialog
, wxWindowList
& windows
)
940 wxSizer
* sizer
= dialog
->GetSizer();
944 sizer
->SetSizeHints(dialog
);
946 wxSize windowSize
, displaySize
;
947 int scrollFlags
= DoMustScroll(dialog
, windowSize
, displaySize
);
948 int scrollBarSize
= 20;
952 int scrollBarExtraX
= 0, scrollBarExtraY
= 0;
953 bool resizeHorizontally
= (scrollFlags
& wxHORIZONTAL
) != 0;
954 bool resizeVertically
= (scrollFlags
& wxVERTICAL
) != 0;
956 if (windows
.GetCount() != 0)
958 // Allow extra for a scrollbar, assuming we resizing in one direction only.
959 if ((resizeVertically
&& !resizeHorizontally
) && (windowSize
.x
< (displaySize
.x
- scrollBarSize
)))
960 scrollBarExtraX
= scrollBarSize
;
961 if ((resizeHorizontally
&& !resizeVertically
) && (windowSize
.y
< (displaySize
.y
- scrollBarSize
)))
962 scrollBarExtraY
= scrollBarSize
;
965 wxWindowList::compatibility_iterator node
= windows
.GetFirst();
968 wxWindow
*win
= node
->GetData();
969 wxScrolledWindow
* scrolledWindow
= wxDynamicCast(win
, wxScrolledWindow
);
972 scrolledWindow
->SetScrollRate(resizeHorizontally
? 10 : 0, resizeVertically
? 10 : 0);
974 if (scrolledWindow
->GetSizer())
975 scrolledWindow
->GetSizer()->Fit(scrolledWindow
);
978 node
= node
->GetNext();
981 wxSize limitTo
= windowSize
+ wxSize(scrollBarExtraX
, scrollBarExtraY
);
982 if (resizeVertically
)
983 limitTo
.y
= displaySize
.y
- wxEXTRA_DIALOG_HEIGHT
;
984 if (resizeHorizontally
)
985 limitTo
.x
= displaySize
.x
;
987 dialog
->SetMinSize(limitTo
);
988 dialog
->SetSize(limitTo
);
990 dialog
->SetSizeHints( limitTo
.x
, limitTo
.y
, dialog
->GetMaxWidth(), dialog
->GetMaxHeight() );
997 * Module to initialise standard adapter
1000 class wxDialogLayoutAdapterModule
: public wxModule
1002 DECLARE_DYNAMIC_CLASS(wxDialogLayoutAdapterModule
)
1004 wxDialogLayoutAdapterModule() {}
1005 virtual void OnExit() { delete wxDialogBase::SetLayoutAdapter(NULL
); }
1006 virtual bool OnInit() { wxDialogBase::SetLayoutAdapter(new wxStandardDialogLayoutAdapter
); return true; }
1009 IMPLEMENT_DYNAMIC_CLASS(wxDialogLayoutAdapterModule
, wxModule
)