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 extern WXDLLEXPORT_DATA(const char) wxDialogNameStr
[] = "dialog";
53 // ----------------------------------------------------------------------------
55 // ----------------------------------------------------------------------------
57 wxDEFINE_FLAGS( wxDialogStyle
)
58 wxBEGIN_FLAGS( wxDialogStyle
)
59 // new style border flags, we put them first to
60 // use them for streaming out
61 wxFLAGS_MEMBER(wxBORDER_SIMPLE
)
62 wxFLAGS_MEMBER(wxBORDER_SUNKEN
)
63 wxFLAGS_MEMBER(wxBORDER_DOUBLE
)
64 wxFLAGS_MEMBER(wxBORDER_RAISED
)
65 wxFLAGS_MEMBER(wxBORDER_STATIC
)
66 wxFLAGS_MEMBER(wxBORDER_NONE
)
68 // old style border flags
69 wxFLAGS_MEMBER(wxSIMPLE_BORDER
)
70 wxFLAGS_MEMBER(wxSUNKEN_BORDER
)
71 wxFLAGS_MEMBER(wxDOUBLE_BORDER
)
72 wxFLAGS_MEMBER(wxRAISED_BORDER
)
73 wxFLAGS_MEMBER(wxSTATIC_BORDER
)
74 wxFLAGS_MEMBER(wxNO_BORDER
)
76 // standard window styles
77 wxFLAGS_MEMBER(wxTAB_TRAVERSAL
)
78 wxFLAGS_MEMBER(wxCLIP_CHILDREN
)
81 wxFLAGS_MEMBER(wxWS_EX_VALIDATE_RECURSIVELY
)
82 wxFLAGS_MEMBER(wxSTAY_ON_TOP
)
83 wxFLAGS_MEMBER(wxCAPTION
)
84 #if WXWIN_COMPATIBILITY_2_6
85 wxFLAGS_MEMBER(wxTHICK_FRAME
)
86 #endif // WXWIN_COMPATIBILITY_2_6
87 wxFLAGS_MEMBER(wxSYSTEM_MENU
)
88 wxFLAGS_MEMBER(wxRESIZE_BORDER
)
89 #if WXWIN_COMPATIBILITY_2_6
90 wxFLAGS_MEMBER(wxRESIZE_BOX
)
91 #endif // WXWIN_COMPATIBILITY_2_6
92 wxFLAGS_MEMBER(wxCLOSE_BOX
)
93 wxFLAGS_MEMBER(wxMAXIMIZE_BOX
)
94 wxFLAGS_MEMBER(wxMINIMIZE_BOX
)
95 wxEND_FLAGS( wxDialogStyle
)
97 wxIMPLEMENT_DYNAMIC_CLASS_XTI(wxDialog
, wxTopLevelWindow
, "wx/dialog.h")
99 wxBEGIN_PROPERTIES_TABLE(wxDialog
)
100 wxPROPERTY( Title
, wxString
, SetTitle
, GetTitle
, wxString(), \
101 0 /*flags*/, wxT("Helpstring"), wxT("group"))
103 wxPROPERTY_FLAGS( WindowStyle
, wxDialogStyle
, long, SetWindowStyleFlag
, \
104 GetWindowStyleFlag
, wxEMPTY_PARAMETER_VALUE
, 0 /*flags*/, \
105 wxT("Helpstring"), wxT("group")) // style
106 wxEND_PROPERTIES_TABLE()
108 wxEMPTY_HANDLERS_TABLE(wxDialog
)
110 wxCONSTRUCTOR_6( wxDialog
, wxWindow
*, Parent
, wxWindowID
, Id
, \
111 wxString
, Title
, wxPoint
, Position
, wxSize
, Size
, long, WindowStyle
)
113 // ----------------------------------------------------------------------------
115 // ----------------------------------------------------------------------------
117 BEGIN_EVENT_TABLE(wxDialogBase
, wxTopLevelWindow
)
118 EVT_BUTTON(wxID_ANY
, wxDialogBase::OnButton
)
120 EVT_CLOSE(wxDialogBase::OnCloseWindow
)
122 EVT_CHAR_HOOK(wxDialogBase::OnCharHook
)
125 wxDialogLayoutAdapter
* wxDialogBase::sm_layoutAdapter
= NULL
;
126 bool wxDialogBase::sm_layoutAdaptation
= false;
128 void wxDialogBase::Init()
131 m_affirmativeId
= wxID_OK
;
132 m_escapeId
= wxID_ANY
;
133 m_layoutAdaptationLevel
= 3;
134 m_layoutAdaptationDone
= FALSE
;
135 m_layoutAdaptationMode
= wxDIALOG_ADAPTATION_MODE_DEFAULT
;
137 // the dialogs have this flag on by default to prevent the events from the
138 // dialog controls from reaching the parent frame which is usually
139 // undesirable and can lead to unexpected and hard to find bugs
140 SetExtraStyle(GetExtraStyle() | wxWS_EX_BLOCK_EVENTS
);
143 wxWindow
*wxDialogBase::CheckIfCanBeUsedAsParent(wxWindow
*parent
) const
148 extern WXDLLIMPEXP_DATA_BASE(wxList
) wxPendingDelete
;
149 if ( wxPendingDelete
.Member(parent
) || parent
->IsBeingDeleted() )
151 // this window is being deleted and we shouldn't create any children
156 if ( parent
->HasExtraStyle(wxWS_EX_TRANSIENT
) )
158 // this window is not being deleted yet but it's going to disappear
159 // soon so still don't parent this window under it
163 if ( !parent
->IsShownOnScreen() )
165 // using hidden parent won't work correctly neither
169 // FIXME-VC6: this compiler requires an explicit const cast or it fails
171 if ( const_cast<const wxWindow
*>(parent
) == this )
173 // not sure if this can really happen but it doesn't hurt to guard
174 // against this clearly invalid situation
182 wxDialogBase::GetParentForModalDialog(wxWindow
*parent
, long style
) const
184 // creating a parent-less modal dialog will result (under e.g. wxGTK2)
185 // in an unfocused dialog, so try to find a valid parent for it unless we
186 // were explicitly asked not to
187 if ( style
& wxDIALOG_NO_PARENT
)
190 // first try the given parent
192 parent
= CheckIfCanBeUsedAsParent(wxGetTopLevelParent(parent
));
194 // then the currently active window
196 parent
= CheckIfCanBeUsedAsParent(
197 wxGetTopLevelParent(wxGetActiveWindow()));
199 // and finally the application main window
201 parent
= CheckIfCanBeUsedAsParent(wxTheApp
->GetTopWindow());
208 wxSizer
*wxDialogBase::CreateTextSizer(const wxString
& message
)
210 wxTextSizerWrapper
wrapper(this);
212 return CreateTextSizer(message
, wrapper
);
215 wxSizer
*wxDialogBase::CreateTextSizer(const wxString
& message
,
216 wxTextSizerWrapper
& wrapper
)
218 // I admit that this is complete bogus, but it makes
219 // message boxes work for pda screens temporarily..
221 const bool is_pda
= wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA
;
224 widthMax
= wxSystemSettings::GetMetric( wxSYS_SCREEN_X
) - 25;
227 return wrapper
.CreateSizer(message
, widthMax
);
230 #endif // wxUSE_STATTEXT
232 wxSizer
*wxDialogBase::CreateButtonSizer(long flags
)
234 #ifdef __SMARTPHONE__
235 wxDialog
* dialog
= (wxDialog
*) this;
237 dialog
->SetLeftMenu(wxID_OK
);
239 if ( flags
& wxCANCEL
)
240 dialog
->SetRightMenu(wxID_CANCEL
);
243 dialog
->SetLeftMenu(wxID_YES
);
246 dialog
->SetRightMenu(wxID_NO
);
249 #else // !__SMARTPHONE__
254 // PocketPC guidelines recommend for Ok/Cancel dialogs to use OK button
255 // located inside caption bar and implement Cancel functionality through
256 // Undo outside dialog. As native behaviour this will be default here but
257 // can be replaced with real wxButtons by setting the option below to 1
258 if ( (flags
& ~(wxCANCEL
|wxNO_DEFAULT
)) != wxOK
||
259 wxSystemOptions::GetOptionInt(wxT("wince.dialog.real-ok-cancel")) )
260 #endif // __POCKETPC__
262 return CreateStdDialogButtonSizer(flags
);
266 #endif // __POCKETPC__
268 #else // !wxUSE_BUTTON
272 #endif // wxUSE_BUTTON/!wxUSE_BUTTON
274 #endif // __SMARTPHONE__/!__SMARTPHONE__
277 wxSizer
*wxDialogBase::CreateSeparatedSizer(wxSizer
*sizer
)
279 // Mac Human Interface Guidelines recommend not to use static lines as
281 #if wxUSE_STATLINE && !defined(__WXMAC__)
282 wxBoxSizer
*topsizer
= new wxBoxSizer(wxVERTICAL
);
283 topsizer
->Add(new wxStaticLine(this),
284 wxSizerFlags().Expand().DoubleBorder(wxBOTTOM
));
285 topsizer
->Add(sizer
, wxSizerFlags().Expand());
287 #endif // wxUSE_STATLINE
292 wxSizer
*wxDialogBase::CreateSeparatedButtonSizer(long flags
)
294 wxSizer
*sizer
= CreateButtonSizer(flags
);
298 return CreateSeparatedSizer(sizer
);
303 wxStdDialogButtonSizer
*wxDialogBase::CreateStdDialogButtonSizer( long flags
)
305 wxStdDialogButtonSizer
*sizer
= new wxStdDialogButtonSizer();
308 wxButton
*yes
= NULL
;
313 ok
= new wxButton(this, wxID_OK
);
314 sizer
->AddButton(ok
);
317 if (flags
& wxCANCEL
)
319 wxButton
*cancel
= new wxButton(this, wxID_CANCEL
);
320 sizer
->AddButton(cancel
);
325 yes
= new wxButton(this, wxID_YES
);
326 sizer
->AddButton(yes
);
331 no
= new wxButton(this, wxID_NO
);
332 sizer
->AddButton(no
);
337 wxButton
*apply
= new wxButton(this, wxID_APPLY
);
338 sizer
->AddButton(apply
);
343 wxButton
*close
= new wxButton(this, wxID_CLOSE
);
344 sizer
->AddButton(close
);
349 wxButton
*help
= new wxButton(this, wxID_HELP
);
350 sizer
->AddButton(help
);
353 if (flags
& wxNO_DEFAULT
)
376 SetAffirmativeId(wxID_OK
);
377 else if (flags
& wxYES
)
378 SetAffirmativeId(wxID_YES
);
385 #endif // wxUSE_BUTTON
387 // ----------------------------------------------------------------------------
388 // standard buttons handling
389 // ----------------------------------------------------------------------------
391 void wxDialogBase::EndDialog(int rc
)
399 void wxDialogBase::AcceptAndClose()
401 if ( Validate() && TransferDataFromWindow() )
403 EndDialog(m_affirmativeId
);
407 void wxDialogBase::SetAffirmativeId(int affirmativeId
)
409 m_affirmativeId
= affirmativeId
;
412 void wxDialogBase::SetEscapeId(int escapeId
)
414 m_escapeId
= escapeId
;
417 bool wxDialogBase::EmulateButtonClickIfPresent(int id
)
420 wxButton
*btn
= wxDynamicCast(FindWindow(id
), wxButton
);
422 if ( !btn
|| !btn
->IsEnabled() || !btn
->IsShown() )
425 wxCommandEvent
event(wxEVT_COMMAND_BUTTON_CLICKED
, id
);
426 event
.SetEventObject(btn
);
427 btn
->GetEventHandler()->ProcessEvent(event
);
430 #else // !wxUSE_BUTTON
433 #endif // wxUSE_BUTTON/!wxUSE_BUTTON
436 bool wxDialogBase::SendCloseButtonClickEvent()
438 int idCancel
= GetEscapeId();
442 // The user doesn't want this dialog to close "implicitly".
446 // this value is special: it means translate Esc to wxID_CANCEL
447 // but if there is no such button, then fall back to wxID_OK
448 if ( EmulateButtonClickIfPresent(wxID_CANCEL
) )
450 idCancel
= GetAffirmativeId();
454 // translate Esc to button press for the button with given id
455 if ( EmulateButtonClickIfPresent(idCancel
) )
462 bool wxDialogBase::IsEscapeKey(const wxKeyEvent
& event
)
464 // for most platforms, Esc key is used to close the dialogs
465 return event
.GetKeyCode() == WXK_ESCAPE
&&
466 event
.GetModifiers() == wxMOD_NONE
;
469 void wxDialogBase::OnCharHook(wxKeyEvent
& event
)
471 if ( event
.GetKeyCode() == WXK_ESCAPE
)
473 if ( SendCloseButtonClickEvent() )
475 // Skip the call to event.Skip() below, we did handle this key.
483 void wxDialogBase::OnButton(wxCommandEvent
& event
)
485 const int id
= event
.GetId();
486 if ( id
== GetAffirmativeId() )
490 else if ( id
== wxID_APPLY
)
493 TransferDataFromWindow();
495 // TODO: disable the Apply button until things change again
497 else if ( id
== GetEscapeId() ||
498 (id
== wxID_CANCEL
&& GetEscapeId() == wxID_ANY
) )
500 EndDialog(wxID_CANCEL
);
502 else // not a standard button
508 // ----------------------------------------------------------------------------
509 // compatibility methods for supporting the modality API
510 // ----------------------------------------------------------------------------
512 wxDEFINE_EVENT( wxEVT_WINDOW_MODAL_DIALOG_CLOSED
, wxWindowModalDialogEvent
);
514 IMPLEMENT_DYNAMIC_CLASS(wxWindowModalDialogEvent
, wxCommandEvent
)
516 void wxDialogBase::ShowWindowModal ()
519 SendWindowModalDialogEvent ( wxEVT_WINDOW_MODAL_DIALOG_CLOSED
);
522 void wxDialogBase::SendWindowModalDialogEvent ( wxEventType type
)
524 wxWindowModalDialogEvent
event ( type
, GetId());
525 event
.SetEventObject(this);
527 if ( !GetEventHandler()->ProcessEvent(event
) )
529 // the event is not propagated upwards to the parent automatically
530 // because the dialog is a top level window, so do it manually as
531 // in 9 cases of 10 the message must be processed by the dialog
532 // owner and not the dialog itself
533 (void)GetParent()->GetEventHandler()->ProcessEvent(event
);
538 wxDialogModality
wxDialogBase::GetModality() const
540 return IsModal() ? wxDIALOG_MODALITY_APP_MODAL
: wxDIALOG_MODALITY_NONE
;
543 // ----------------------------------------------------------------------------
544 // other event handlers
545 // ----------------------------------------------------------------------------
547 void wxDialogBase::OnCloseWindow(wxCloseEvent
& WXUNUSED(event
))
549 // We'll send a Cancel message by default, which may close the dialog.
551 // Check for looping if the Cancel event handler calls Close().
553 // VZ: this is horrible and MT-unsafe. Can't we reuse some of these global
554 // lists here? don't dare to change it now, but should be done later!
555 static wxList closing
;
557 if ( closing
.Member(this) )
560 closing
.Append(this);
562 if ( !SendCloseButtonClickEvent() )
564 // If the handler didn't close the dialog (e.g. because there is no
565 // button with matching id) we still want to close it when the user
566 // clicks the "x" button in the title bar, otherwise we shouldn't even
567 // have put it there.
569 // Notice that using wxID_CLOSE might have been a better choice but we
570 // use wxID_CANCEL for compatibility reasons.
571 EndDialog(wxID_CANCEL
);
574 closing
.DeleteObject(this);
577 void wxDialogBase::OnSysColourChanged(wxSysColourChangedEvent
& event
)
580 SetBackgroundColour(wxSystemSettings::GetColour(wxSYS_COLOUR_3DFACE
));
587 /// Do the adaptation
588 bool wxDialogBase::DoLayoutAdaptation()
590 if (GetLayoutAdapter())
592 wxWindow
* focusWindow
= wxFindFocusDescendant(this); // from event.h
593 if (GetLayoutAdapter()->DoLayoutAdaptation((wxDialog
*) this))
596 focusWindow
->SetFocus();
606 /// Can we do the adaptation?
607 bool wxDialogBase::CanDoLayoutAdaptation()
609 // Check if local setting overrides the global setting
610 bool layoutEnabled
= (GetLayoutAdaptationMode() == wxDIALOG_ADAPTATION_MODE_ENABLED
) || (IsLayoutAdaptationEnabled() && (GetLayoutAdaptationMode() != wxDIALOG_ADAPTATION_MODE_DISABLED
));
612 return (layoutEnabled
&& !m_layoutAdaptationDone
&& GetLayoutAdaptationLevel() != 0 && GetLayoutAdapter() != NULL
&& GetLayoutAdapter()->CanDoLayoutAdaptation((wxDialog
*) this));
615 /// Set scrolling adapter class, returning old adapter
616 wxDialogLayoutAdapter
* wxDialogBase::SetLayoutAdapter(wxDialogLayoutAdapter
* adapter
)
618 wxDialogLayoutAdapter
* oldLayoutAdapter
= sm_layoutAdapter
;
619 sm_layoutAdapter
= adapter
;
620 return oldLayoutAdapter
;
627 IMPLEMENT_CLASS(wxDialogLayoutAdapter
, wxObject
)
629 IMPLEMENT_CLASS(wxStandardDialogLayoutAdapter
, wxDialogLayoutAdapter
)
631 // Allow for caption size on wxWidgets < 2.9
632 #if defined(__WXGTK__) && !wxCHECK_VERSION(2,9,0)
633 #define wxEXTRA_DIALOG_HEIGHT 30
635 #define wxEXTRA_DIALOG_HEIGHT 0
638 /// Indicate that adaptation should be done
639 bool wxStandardDialogLayoutAdapter::CanDoLayoutAdaptation(wxDialog
* dialog
)
641 if (dialog
->GetSizer())
643 wxSize windowSize
, displaySize
;
644 return MustScroll(dialog
, windowSize
, displaySize
) != 0;
650 bool wxStandardDialogLayoutAdapter::DoLayoutAdaptation(wxDialog
* dialog
)
652 if (dialog
->GetSizer())
655 wxBookCtrlBase
* bookContentWindow
= wxDynamicCast(dialog
->GetContentWindow(), wxBookCtrlBase
);
657 if (bookContentWindow
)
659 // If we have a book control, make all the pages (that use sizers) scrollable
660 wxWindowList windows
;
661 for (size_t i
= 0; i
< bookContentWindow
->GetPageCount(); i
++)
663 wxWindow
* page
= bookContentWindow
->GetPage(i
);
665 wxScrolledWindow
* scrolledWindow
= wxDynamicCast(page
, wxScrolledWindow
);
667 windows
.Append(scrolledWindow
);
668 else if (!scrolledWindow
&& page
->GetSizer())
670 // Create a scrolled window and reparent
671 scrolledWindow
= CreateScrolledWindow(page
);
672 wxSizer
* oldSizer
= page
->GetSizer();
674 wxSizer
* newSizer
= new wxBoxSizer(wxVERTICAL
);
675 newSizer
->Add(scrolledWindow
,1, wxEXPAND
, 0);
677 page
->SetSizer(newSizer
, false /* don't delete the old sizer */);
679 scrolledWindow
->SetSizer(oldSizer
);
681 ReparentControls(page
, scrolledWindow
);
683 windows
.Append(scrolledWindow
);
687 FitWithScrolling(dialog
, windows
);
690 #endif // wxUSE_BOOKCTRL
693 // If we have an arbitrary dialog, create a scrolling area for the main content, and a button sizer
694 // for the main buttons.
695 wxScrolledWindow
* scrolledWindow
= CreateScrolledWindow(dialog
);
697 int buttonSizerBorder
= 0;
699 // First try to find a wxStdDialogButtonSizer
700 wxSizer
* buttonSizer
= FindButtonSizer(true /* find std button sizer */, dialog
, dialog
->GetSizer(), buttonSizerBorder
);
702 // Next try to find a wxBoxSizer containing the controls
703 if (!buttonSizer
&& dialog
->GetLayoutAdaptationLevel() > wxDIALOG_ADAPTATION_STANDARD_SIZER
)
704 buttonSizer
= FindButtonSizer(false /* find ordinary sizer */, dialog
, dialog
->GetSizer(), buttonSizerBorder
);
706 // If we still don't have a button sizer, collect any 'loose' buttons in the layout
707 if (!buttonSizer
&& dialog
->GetLayoutAdaptationLevel() > wxDIALOG_ADAPTATION_ANY_SIZER
)
710 wxStdDialogButtonSizer
* stdButtonSizer
= new wxStdDialogButtonSizer
;
711 buttonSizer
= stdButtonSizer
;
713 FindLooseButtons(dialog
, stdButtonSizer
, dialog
->GetSizer(), count
);
715 stdButtonSizer
->Realize();
718 wxDELETE(buttonSizer
);
722 if (buttonSizerBorder
== 0)
723 buttonSizerBorder
= 5;
725 ReparentControls(dialog
, scrolledWindow
, buttonSizer
);
727 wxBoxSizer
* newTopSizer
= new wxBoxSizer(wxVERTICAL
);
728 wxSizer
* oldSizer
= dialog
->GetSizer();
730 dialog
->SetSizer(newTopSizer
, false /* don't delete old sizer */);
732 newTopSizer
->Add(scrolledWindow
, 1, wxEXPAND
|wxALL
, 0);
734 newTopSizer
->Add(buttonSizer
, 0, wxEXPAND
|wxALL
, buttonSizerBorder
);
736 scrolledWindow
->SetSizer(oldSizer
);
738 FitWithScrolling(dialog
, scrolledWindow
);
739 #endif // wxUSE_BUTTON
743 dialog
->SetLayoutAdaptationDone(true);
747 // Create the scrolled window
748 wxScrolledWindow
* wxStandardDialogLayoutAdapter::CreateScrolledWindow(wxWindow
* parent
)
750 wxScrolledWindow
* scrolledWindow
= new wxScrolledWindow(parent
, wxID_ANY
, wxDefaultPosition
, wxDefaultSize
, wxTAB_TRAVERSAL
|wxVSCROLL
|wxHSCROLL
|wxBORDER_NONE
);
751 return scrolledWindow
;
756 /// Find and remove the button sizer, if any
757 wxSizer
* wxStandardDialogLayoutAdapter::FindButtonSizer(bool stdButtonSizer
, wxDialog
* dialog
, wxSizer
* sizer
, int& retBorder
, int accumlatedBorder
)
759 for ( wxSizerItemList::compatibility_iterator node
= sizer
->GetChildren().GetFirst();
760 node
; node
= node
->GetNext() )
762 wxSizerItem
*item
= node
->GetData();
763 wxSizer
*childSizer
= item
->GetSizer();
767 int newBorder
= accumlatedBorder
;
768 if (item
->GetFlag() & wxALL
)
769 newBorder
+= item
->GetBorder();
771 if (stdButtonSizer
) // find wxStdDialogButtonSizer
773 wxStdDialogButtonSizer
* buttonSizer
= wxDynamicCast(childSizer
, wxStdDialogButtonSizer
);
776 sizer
->Detach(childSizer
);
777 retBorder
= newBorder
;
781 else // find a horizontal box sizer containing standard buttons
783 wxBoxSizer
* buttonSizer
= wxDynamicCast(childSizer
, wxBoxSizer
);
784 if (buttonSizer
&& IsOrdinaryButtonSizer(dialog
, buttonSizer
))
786 sizer
->Detach(childSizer
);
787 retBorder
= newBorder
;
792 wxSizer
* s
= FindButtonSizer(stdButtonSizer
, dialog
, childSizer
, retBorder
, newBorder
);
800 /// Check if this sizer contains standard buttons, and so can be repositioned in the dialog
801 bool wxStandardDialogLayoutAdapter::IsOrdinaryButtonSizer(wxDialog
* dialog
, wxBoxSizer
* sizer
)
803 if (sizer
->GetOrientation() != wxHORIZONTAL
)
806 for ( wxSizerItemList::compatibility_iterator node
= sizer
->GetChildren().GetFirst();
807 node
; node
= node
->GetNext() )
809 wxSizerItem
*item
= node
->GetData();
810 wxButton
*childButton
= wxDynamicCast(item
->GetWindow(), wxButton
);
812 if (childButton
&& IsStandardButton(dialog
, childButton
))
818 /// Check if this is a standard button
819 bool wxStandardDialogLayoutAdapter::IsStandardButton(wxDialog
* dialog
, wxButton
* button
)
821 wxWindowID id
= button
->GetId();
823 return (id
== wxID_OK
|| id
== wxID_CANCEL
|| id
== wxID_YES
|| id
== wxID_NO
|| id
== wxID_SAVE
||
824 id
== wxID_APPLY
|| id
== wxID_HELP
|| id
== wxID_CONTEXT_HELP
|| dialog
->IsMainButtonId(id
));
827 /// Find 'loose' main buttons in the existing layout and add them to the standard dialog sizer
828 bool wxStandardDialogLayoutAdapter::FindLooseButtons(wxDialog
* dialog
, wxStdDialogButtonSizer
* buttonSizer
, wxSizer
* sizer
, int& count
)
830 wxSizerItemList::compatibility_iterator node
= sizer
->GetChildren().GetFirst();
833 wxSizerItemList::compatibility_iterator next
= node
->GetNext();
834 wxSizerItem
*item
= node
->GetData();
835 wxSizer
*childSizer
= item
->GetSizer();
836 wxButton
*childButton
= wxDynamicCast(item
->GetWindow(), wxButton
);
838 if (childButton
&& IsStandardButton(dialog
, childButton
))
840 sizer
->Detach(childButton
);
841 buttonSizer
->AddButton(childButton
);
846 FindLooseButtons(dialog
, buttonSizer
, childSizer
, count
);
853 #endif // wxUSE_BUTTON
855 /// Reparent the controls to the scrolled window
856 void wxStandardDialogLayoutAdapter::ReparentControls(wxWindow
* parent
, wxWindow
* reparentTo
, wxSizer
* buttonSizer
)
858 DoReparentControls(parent
, reparentTo
, buttonSizer
);
861 void wxStandardDialogLayoutAdapter::DoReparentControls(wxWindow
* parent
, wxWindow
* reparentTo
, wxSizer
* buttonSizer
)
863 wxWindowList::compatibility_iterator node
= parent
->GetChildren().GetFirst();
866 wxWindowList::compatibility_iterator next
= node
->GetNext();
868 wxWindow
*win
= node
->GetData();
870 // Don't reparent the scrolled window or buttons in the button sizer
871 if (win
!= reparentTo
&& (!buttonSizer
|| !buttonSizer
->GetItem(win
)))
873 win
->Reparent(reparentTo
);
875 // Restore correct tab order
876 ::SetWindowPos((HWND
) win
->GetHWND(), HWND_BOTTOM
, -1, -1, -1, -1, SWP_NOMOVE
|SWP_NOSIZE
);
884 /// Find whether scrolling will be necessary for the dialog, returning wxVERTICAL, wxHORIZONTAL or both
885 int wxStandardDialogLayoutAdapter::MustScroll(wxDialog
* dialog
, wxSize
& windowSize
, wxSize
& displaySize
)
887 return DoMustScroll(dialog
, windowSize
, displaySize
);
890 /// Find whether scrolling will be necessary for the dialog, returning wxVERTICAL, wxHORIZONTAL or both
891 int wxStandardDialogLayoutAdapter::DoMustScroll(wxDialog
* dialog
, wxSize
& windowSize
, wxSize
& displaySize
)
893 wxSize minWindowSize
= dialog
->GetSizer()->GetMinSize();
894 windowSize
= dialog
->GetSize();
895 windowSize
= wxSize(wxMax(windowSize
.x
, minWindowSize
.x
), wxMax(windowSize
.y
, minWindowSize
.y
));
897 displaySize
= wxDisplay(wxDisplay::GetFromWindow(dialog
)).GetClientArea().GetSize();
899 displaySize
= wxGetClientDisplayRect().GetSize();
904 if (windowSize
.y
>= (displaySize
.y
- wxEXTRA_DIALOG_HEIGHT
))
906 if (windowSize
.x
>= displaySize
.x
)
907 flags
|= wxHORIZONTAL
;
912 // A function to fit the dialog around its contents, and then adjust for screen size.
913 // If scrolled windows are passed, scrolling is enabled in the required orientation(s).
914 bool wxStandardDialogLayoutAdapter::FitWithScrolling(wxDialog
* dialog
, wxWindowList
& windows
)
916 return DoFitWithScrolling(dialog
, windows
);
919 // A function to fit the dialog around its contents, and then adjust for screen size.
920 // If a scrolled window is passed, scrolling is enabled in the required orientation(s).
921 bool wxStandardDialogLayoutAdapter::FitWithScrolling(wxDialog
* dialog
, wxScrolledWindow
* scrolledWindow
)
923 return DoFitWithScrolling(dialog
, scrolledWindow
);
926 // A function to fit the dialog around its contents, and then adjust for screen size.
927 // If a scrolled window is passed, scrolling is enabled in the required orientation(s).
928 bool wxStandardDialogLayoutAdapter::DoFitWithScrolling(wxDialog
* dialog
, wxScrolledWindow
* scrolledWindow
)
930 wxWindowList windows
;
931 windows
.Append(scrolledWindow
);
932 return DoFitWithScrolling(dialog
, windows
);
935 bool wxStandardDialogLayoutAdapter::DoFitWithScrolling(wxDialog
* dialog
, wxWindowList
& windows
)
937 wxSizer
* sizer
= dialog
->GetSizer();
941 sizer
->SetSizeHints(dialog
);
943 wxSize windowSize
, displaySize
;
944 int scrollFlags
= DoMustScroll(dialog
, windowSize
, displaySize
);
945 int scrollBarSize
= 20;
949 int scrollBarExtraX
= 0, scrollBarExtraY
= 0;
950 bool resizeHorizontally
= (scrollFlags
& wxHORIZONTAL
) != 0;
951 bool resizeVertically
= (scrollFlags
& wxVERTICAL
) != 0;
953 if (windows
.GetCount() != 0)
955 // Allow extra for a scrollbar, assuming we resizing in one direction only.
956 if ((resizeVertically
&& !resizeHorizontally
) && (windowSize
.x
< (displaySize
.x
- scrollBarSize
)))
957 scrollBarExtraX
= scrollBarSize
;
958 if ((resizeHorizontally
&& !resizeVertically
) && (windowSize
.y
< (displaySize
.y
- scrollBarSize
)))
959 scrollBarExtraY
= scrollBarSize
;
962 wxWindowList::compatibility_iterator node
= windows
.GetFirst();
965 wxWindow
*win
= node
->GetData();
966 wxScrolledWindow
* scrolledWindow
= wxDynamicCast(win
, wxScrolledWindow
);
969 scrolledWindow
->SetScrollRate(resizeHorizontally
? 10 : 0, resizeVertically
? 10 : 0);
971 if (scrolledWindow
->GetSizer())
972 scrolledWindow
->GetSizer()->Fit(scrolledWindow
);
975 node
= node
->GetNext();
978 wxSize limitTo
= windowSize
+ wxSize(scrollBarExtraX
, scrollBarExtraY
);
979 if (resizeVertically
)
980 limitTo
.y
= displaySize
.y
- wxEXTRA_DIALOG_HEIGHT
;
981 if (resizeHorizontally
)
982 limitTo
.x
= displaySize
.x
;
984 dialog
->SetMinSize(limitTo
);
985 dialog
->SetSize(limitTo
);
987 dialog
->SetSizeHints( limitTo
.x
, limitTo
.y
, dialog
->GetMaxWidth(), dialog
->GetMaxHeight() );
994 * Module to initialise standard adapter
997 class wxDialogLayoutAdapterModule
: public wxModule
999 DECLARE_DYNAMIC_CLASS(wxDialogLayoutAdapterModule
)
1001 wxDialogLayoutAdapterModule() {}
1002 virtual void OnExit() { delete wxDialogBase::SetLayoutAdapter(NULL
); }
1003 virtual bool OnInit() { wxDialogBase::SetLayoutAdapter(new wxStandardDialogLayoutAdapter
); return true; }
1006 IMPLEMENT_DYNAMIC_CLASS(wxDialogLayoutAdapterModule
, wxModule
)