1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/sizer.cpp
3 // Purpose: provide new wxSizer class for layout
4 // Author: Robert Roebling and Robin Dunn, contributions by
5 // Dirk Holtwick, Ron Lee
6 // Modified by: Ron Lee
9 // Copyright: (c) Robin Dunn, Robert Roebling
10 // Licence: wxWindows licence
11 /////////////////////////////////////////////////////////////////////////////
13 // For compilers that support precompilation, includes "wx.h".
14 #include "wx/wxprec.h"
21 #include "wx/private/flagscheck.h"
24 #include "wx/string.h"
28 #include "wx/settings.h"
29 #include "wx/button.h"
30 #include "wx/statbox.h"
31 #include "wx/toplevel.h"
34 #include "wx/display.h"
35 #include "wx/listimpl.cpp"
38 //---------------------------------------------------------------------------
40 IMPLEMENT_CLASS(wxSizerItem
, wxObject
)
41 IMPLEMENT_CLASS(wxSizer
, wxObject
)
42 IMPLEMENT_CLASS(wxGridSizer
, wxSizer
)
43 IMPLEMENT_CLASS(wxFlexGridSizer
, wxGridSizer
)
44 IMPLEMENT_CLASS(wxBoxSizer
, wxSizer
)
46 IMPLEMENT_CLASS(wxStaticBoxSizer
, wxBoxSizer
)
49 IMPLEMENT_CLASS(wxStdDialogButtonSizer
, wxBoxSizer
)
52 WX_DEFINE_EXPORTED_LIST( wxSizerItemList
)
87 // ----------------------------------------------------------------------------
89 // ----------------------------------------------------------------------------
91 // check for flags conflicts
92 static const int SIZER_FLAGS_MASK
=
94 wxADD_FLAG(wxHORIZONTAL
,
95 wxADD_FLAG(wxVERTICAL
,
100 wxADD_FLAG(wxALIGN_NOT
,
101 wxADD_FLAG(wxALIGN_CENTER_HORIZONTAL
,
102 wxADD_FLAG(wxALIGN_RIGHT
,
103 wxADD_FLAG(wxALIGN_BOTTOM
,
104 wxADD_FLAG(wxALIGN_CENTER_VERTICAL
,
105 wxADD_FLAG(wxFIXED_MINSIZE
,
106 wxADD_FLAG(wxRESERVE_SPACE_EVEN_IF_HIDDEN
,
107 wxADD_FLAG(wxSTRETCH_NOT
,
113 #define ASSERT_VALID_SIZER_FLAGS(f) wxASSERT_VALID_FLAGS(f, SIZER_FLAGS_MASK)
116 void wxSizerItem::Init(const wxSizerFlags
& flags
)
120 m_proportion
= flags
.GetProportion();
121 m_flag
= flags
.GetFlags();
122 m_border
= flags
.GetBorderInPixels();
124 ASSERT_VALID_SIZER_FLAGS( m_flag
);
127 wxSizerItem::wxSizerItem()
138 void wxSizerItem::DoSetWindow(wxWindow
*window
)
140 wxCHECK_RET( window
, _T("NULL window in wxSizerItem::SetWindow()") );
142 m_kind
= Item_Window
;
145 // window doesn't become smaller than its initial size, whatever happens
146 m_minSize
= window
->GetSize();
148 if ( m_flag
& wxFIXED_MINSIZE
)
149 window
->SetMinSize(m_minSize
);
151 // aspect ratio calculated from initial size
155 wxSizerItem::wxSizerItem(wxWindow
*window
,
161 m_proportion(proportion
),
167 ASSERT_VALID_SIZER_FLAGS( m_flag
);
173 void wxSizerItem::DoSetSizer(wxSizer
*sizer
)
179 wxSizerItem::wxSizerItem(wxSizer
*sizer
,
186 m_proportion(proportion
),
193 ASSERT_VALID_SIZER_FLAGS( m_flag
);
197 // m_minSize is set later
201 void wxSizerItem::DoSetSpacer(const wxSize
& size
)
203 m_kind
= Item_Spacer
;
204 m_spacer
= new wxSizerSpacer(size
);
209 wxSizerItem::wxSizerItem(int width
,
217 m_minSize(width
, height
), // minimal size is the initial size
218 m_proportion(proportion
),
224 ASSERT_VALID_SIZER_FLAGS( m_flag
);
226 DoSetSpacer(wxSize(width
, height
));
229 wxSizerItem::~wxSizerItem()
235 void wxSizerItem::Free()
243 m_window
->SetContainingSizer(NULL
);
256 wxFAIL_MSG( _T("unexpected wxSizerItem::m_kind") );
262 wxSize
wxSizerItem::GetSpacer() const
265 if ( m_kind
== Item_Spacer
)
266 size
= m_spacer
->GetSize();
272 wxSize
wxSizerItem::GetSize() const
281 ret
= m_window
->GetSize();
285 ret
= m_sizer
->GetSize();
289 ret
= m_spacer
->GetSize();
294 wxFAIL_MSG( _T("unexpected wxSizerItem::m_kind") );
301 if (m_flag
& wxNORTH
)
303 if (m_flag
& wxSOUTH
)
309 bool wxSizerItem::InformFirstDirection(int direction
, int size
, int availableOtherDir
)
311 // The size that come here will be including borders. Child items should get it
315 if( direction
==wxHORIZONTAL
)
322 else if( direction
==wxVERTICAL
)
324 if (m_flag
& wxNORTH
)
326 if (m_flag
& wxSOUTH
)
332 // Pass the information along to the held object
335 didUse
= GetSizer()->InformFirstDirection(direction
,size
,availableOtherDir
);
337 m_minSize
= GetSizer()->CalcMin();
341 didUse
= GetWindow()->InformFirstDirection(direction
,size
,availableOtherDir
);
343 m_minSize
= m_window
->GetEffectiveMinSize();
345 // This information is useful for items with wxSHAPED flag, since
346 // we can request an optimal min size for such an item. Even if
347 // we overwrite the m_minSize member here, we can read it back from
348 // the owned window (happens automatically).
349 if( (m_flag
& wxSHAPED
) && (m_flag
& wxEXPAND
) && direction
)
351 if( !wxIsNullDouble(m_ratio
) )
353 wxCHECK_MSG( (m_proportion
==0), false, _T("Shaped item, non-zero proportion in wxSizerItem::InformFirstDirection()") );
354 if( direction
==wxHORIZONTAL
&& !wxIsNullDouble(m_ratio
) )
356 // Clip size so that we don't take too much
357 if( availableOtherDir
>=0 && int(size
/m_ratio
)-m_minSize
.y
>availableOtherDir
)
358 size
= int((availableOtherDir
+m_minSize
.y
)*m_ratio
);
359 m_minSize
= wxSize(size
,int(size
/m_ratio
));
361 else if( direction
==wxVERTICAL
)
363 // Clip size so that we don't take too much
364 if( availableOtherDir
>=0 && int(size
*m_ratio
)-m_minSize
.x
>availableOtherDir
)
365 size
= int((availableOtherDir
+m_minSize
.x
)/m_ratio
);
366 m_minSize
= wxSize(int(size
*m_ratio
),size
);
376 wxSize
wxSizerItem::CalcMin()
380 m_minSize
= m_sizer
->GetMinSize();
382 // if we have to preserve aspect ratio _AND_ this is
383 // the first-time calculation, consider ret to be initial size
384 if ( (m_flag
& wxSHAPED
) && wxIsNullDouble(m_ratio
) )
387 else if ( IsWindow() )
389 // Since the size of the window may change during runtime, we
390 // should use the current minimal/best size.
391 m_minSize
= m_window
->GetEffectiveMinSize();
394 return GetMinSizeWithBorder();
397 wxSize
wxSizerItem::GetMinSizeWithBorder() const
399 wxSize ret
= m_minSize
;
405 if (m_flag
& wxNORTH
)
407 if (m_flag
& wxSOUTH
)
414 void wxSizerItem::SetDimension( const wxPoint
& pos_
, const wxSize
& size_
)
418 if (m_flag
& wxSHAPED
)
420 // adjust aspect ratio
421 int rwidth
= (int) (size
.y
* m_ratio
);
425 int rheight
= (int) (size
.x
/ m_ratio
);
426 // add vertical space
427 if (m_flag
& wxALIGN_CENTER_VERTICAL
)
428 pos
.y
+= (size
.y
- rheight
) / 2;
429 else if (m_flag
& wxALIGN_BOTTOM
)
430 pos
.y
+= (size
.y
- rheight
);
431 // use reduced dimensions
434 else if (rwidth
< size
.x
)
436 // add horizontal space
437 if (m_flag
& wxALIGN_CENTER_HORIZONTAL
)
438 pos
.x
+= (size
.x
- rwidth
) / 2;
439 else if (m_flag
& wxALIGN_RIGHT
)
440 pos
.x
+= (size
.x
- rwidth
);
445 // This is what GetPosition() returns. Since we calculate
446 // borders afterwards, GetPosition() will be the left/top
447 // corner of the surrounding border.
459 if (m_flag
& wxNORTH
)
464 if (m_flag
& wxSOUTH
)
474 m_rect
= wxRect(pos
, size
);
479 wxFAIL_MSG( _T("can't set size of uninitialized sizer item") );
483 m_window
->SetSize(pos
.x
, pos
.y
, size
.x
, size
.y
,
484 wxSIZE_ALLOW_MINUS_ONE
);
488 m_sizer
->SetDimension(pos
, size
);
492 m_spacer
->SetSize(size
);
497 wxFAIL_MSG( _T("unexpected wxSizerItem::m_kind") );
501 void wxSizerItem::DeleteWindows()
510 //We are deleting the window from this sizer - normally
511 //the window destroys the sizer associated with it,
512 //which might destroy this, which we don't want
513 m_window
->SetContainingSizer(NULL
);
515 //Putting this after the switch will result in a spacer
516 //not being deleted properly on destruction
521 m_sizer
->DeleteWindows();
526 wxFAIL_MSG( _T("unexpected wxSizerItem::m_kind") );
531 void wxSizerItem::Show( bool show
)
536 wxFAIL_MSG( _T("can't show uninitialized sizer item") );
540 m_window
->Show(show
);
548 m_spacer
->Show(show
);
553 wxFAIL_MSG( _T("unexpected wxSizerItem::m_kind") );
557 bool wxSizerItem::IsShown() const
559 if ( m_flag
& wxRESERVE_SPACE_EVEN_IF_HIDDEN
)
565 // we may be called from CalcMin(), just return false so that we're
570 return m_window
->IsShown();
573 // arbitrarily decide that if at least one of our elements is
574 // shown, so are we (this arbitrariness is the reason for
575 // deprecating this function)
577 // Some apps (such as dialog editors) depend on an empty sizer still
578 // being laid out correctly and reporting the correct size and position.
579 if (m_sizer
->GetChildren().GetCount() == 0)
582 for ( wxSizerItemList::compatibility_iterator
583 node
= m_sizer
->GetChildren().GetFirst();
585 node
= node
->GetNext() )
587 if ( node
->GetData()->IsShown() )
594 return m_spacer
->IsShown();
598 wxFAIL_MSG( _T("unexpected wxSizerItem::m_kind") );
604 #if WXWIN_COMPATIBILITY_2_6
605 void wxSizerItem::SetOption( int option
)
607 SetProportion( option
);
610 int wxSizerItem::GetOption() const
612 return GetProportion();
614 #endif // WXWIN_COMPATIBILITY_2_6
617 //---------------------------------------------------------------------------
619 //---------------------------------------------------------------------------
623 WX_CLEAR_LIST(wxSizerItemList
, m_children
);
626 wxSizerItem
* wxSizer::Insert( size_t index
, wxSizerItem
*item
)
628 m_children
.Insert( index
, item
);
630 if ( item
->GetWindow() )
631 item
->GetWindow()->SetContainingSizer( this );
633 if ( item
->GetSizer() )
634 item
->GetSizer()->SetContainingWindow( m_containingWindow
);
639 void wxSizer::SetContainingWindow(wxWindow
*win
)
641 if ( win
== m_containingWindow
)
644 m_containingWindow
= win
;
646 // set the same window for all nested sizers as well, they also are in the
648 for ( wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
650 node
= node
->GetNext() )
652 wxSizerItem
*const item
= node
->GetData();
653 wxSizer
*const sizer
= item
->GetSizer();
657 sizer
->SetContainingWindow(win
);
662 #if WXWIN_COMPATIBILITY_2_6
663 bool wxSizer::Remove( wxWindow
*window
)
665 return Detach( window
);
667 #endif // WXWIN_COMPATIBILITY_2_6
669 bool wxSizer::Remove( wxSizer
*sizer
)
671 wxASSERT_MSG( sizer
, _T("Removing NULL sizer") );
673 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
676 wxSizerItem
*item
= node
->GetData();
678 if (item
->GetSizer() == sizer
)
681 m_children
.Erase( node
);
685 node
= node
->GetNext();
691 bool wxSizer::Remove( int index
)
693 wxCHECK_MSG( index
>= 0 && (size_t)index
< m_children
.GetCount(),
695 _T("Remove index is out of range") );
697 wxSizerItemList::compatibility_iterator node
= m_children
.Item( index
);
699 wxCHECK_MSG( node
, false, _T("Failed to find child node") );
701 delete node
->GetData();
702 m_children
.Erase( node
);
707 bool wxSizer::Detach( wxSizer
*sizer
)
709 wxASSERT_MSG( sizer
, _T("Detaching NULL sizer") );
711 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
714 wxSizerItem
*item
= node
->GetData();
716 if (item
->GetSizer() == sizer
)
720 m_children
.Erase( node
);
723 node
= node
->GetNext();
729 bool wxSizer::Detach( wxWindow
*window
)
731 wxASSERT_MSG( window
, _T("Detaching NULL window") );
733 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
736 wxSizerItem
*item
= node
->GetData();
738 if (item
->GetWindow() == window
)
741 m_children
.Erase( node
);
744 node
= node
->GetNext();
750 bool wxSizer::Detach( int index
)
752 wxCHECK_MSG( index
>= 0 && (size_t)index
< m_children
.GetCount(),
754 _T("Detach index is out of range") );
756 wxSizerItemList::compatibility_iterator node
= m_children
.Item( index
);
758 wxCHECK_MSG( node
, false, _T("Failed to find child node") );
760 wxSizerItem
*item
= node
->GetData();
762 if ( item
->IsSizer() )
766 m_children
.Erase( node
);
770 bool wxSizer::Replace( wxWindow
*oldwin
, wxWindow
*newwin
, bool recursive
)
772 wxASSERT_MSG( oldwin
, _T("Replacing NULL window") );
773 wxASSERT_MSG( newwin
, _T("Replacing with NULL window") );
775 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
778 wxSizerItem
*item
= node
->GetData();
780 if (item
->GetWindow() == oldwin
)
782 item
->AssignWindow(newwin
);
783 newwin
->SetContainingSizer( this );
786 else if (recursive
&& item
->IsSizer())
788 if (item
->GetSizer()->Replace( oldwin
, newwin
, true ))
792 node
= node
->GetNext();
798 bool wxSizer::Replace( wxSizer
*oldsz
, wxSizer
*newsz
, bool recursive
)
800 wxASSERT_MSG( oldsz
, _T("Replacing NULL sizer") );
801 wxASSERT_MSG( newsz
, _T("Replacing with NULL sizer") );
803 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
806 wxSizerItem
*item
= node
->GetData();
808 if (item
->GetSizer() == oldsz
)
810 item
->AssignSizer(newsz
);
813 else if (recursive
&& item
->IsSizer())
815 if (item
->GetSizer()->Replace( oldsz
, newsz
, true ))
819 node
= node
->GetNext();
825 bool wxSizer::Replace( size_t old
, wxSizerItem
*newitem
)
827 wxCHECK_MSG( old
< m_children
.GetCount(), false, _T("Replace index is out of range") );
828 wxASSERT_MSG( newitem
, _T("Replacing with NULL item") );
830 wxSizerItemList::compatibility_iterator node
= m_children
.Item( old
);
832 wxCHECK_MSG( node
, false, _T("Failed to find child node") );
834 wxSizerItem
*item
= node
->GetData();
835 node
->SetData(newitem
);
841 void wxSizer::Clear( bool delete_windows
)
843 // First clear the ContainingSizer pointers
844 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
847 wxSizerItem
*item
= node
->GetData();
849 if (item
->IsWindow())
850 item
->GetWindow()->SetContainingSizer( NULL
);
851 node
= node
->GetNext();
854 // Destroy the windows if needed
858 // Now empty the list
859 WX_CLEAR_LIST(wxSizerItemList
, m_children
);
862 void wxSizer::DeleteWindows()
864 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
867 wxSizerItem
*item
= node
->GetData();
869 item
->DeleteWindows();
870 node
= node
->GetNext();
874 wxSize
wxSizer::ComputeFittingClientSize(wxWindow
*window
)
876 wxCHECK_MSG( window
, wxDefaultSize
, "window can't be NULL" );
878 // take the min size by default and limit it by max size
879 wxSize size
= GetMinClientSize(window
);
882 wxTopLevelWindow
*tlw
= wxDynamicCast(window
, wxTopLevelWindow
);
885 // hack for small screen devices where TLWs are always full screen
886 if ( tlw
->IsAlwaysMaximized() )
888 return tlw
->GetClientSize();
891 // limit the window to the size of the display it is on
892 int disp
= wxDisplay::GetFromWindow(window
);
893 if ( disp
== wxNOT_FOUND
)
895 // or, if we don't know which one it is, of the main one
899 sizeMax
= wxDisplay(disp
).GetClientArea().GetSize();
901 // space for decorations and toolbars etc.
902 sizeMax
= tlw
->WindowToClientSize(sizeMax
);
906 sizeMax
= GetMaxClientSize(window
);
909 if ( sizeMax
.x
!= wxDefaultCoord
&& size
.x
> sizeMax
.x
)
911 if ( sizeMax
.y
!= wxDefaultCoord
&& size
.y
> sizeMax
.y
)
917 wxSize
wxSizer::ComputeFittingWindowSize(wxWindow
*window
)
919 wxCHECK_MSG( window
, wxDefaultSize
, "window can't be NULL" );
921 return window
->ClientToWindowSize(ComputeFittingClientSize(window
));
924 wxSize
wxSizer::Fit( wxWindow
*window
)
926 wxCHECK_MSG( window
, wxDefaultSize
, "window can't be NULL" );
929 window
->SetClientSize(ComputeFittingClientSize(window
));
931 // return entire size
932 return window
->GetSize();
935 void wxSizer::FitInside( wxWindow
*window
)
938 if (window
->IsTopLevel())
939 size
= VirtualFitSize( window
);
941 size
= GetMinClientSize( window
);
943 window
->SetVirtualSize( size
);
946 void wxSizer::Layout()
948 // (re)calculates minimums needed for each item and other preparations
952 // Applies the layout and repositions/resizes the items
956 void wxSizer::SetSizeHints( wxWindow
*window
)
958 // Preserve the window's max size hints, but set the
959 // lower bound according to the sizer calculations.
961 // This is equivalent to calling Fit(), except that we need to set
962 // the size hints _in between_ the two steps performed by Fit
963 // (1. ComputeFittingClientSize, 2. SetClientSize). That's because
964 // otherwise SetClientSize() could have no effect if there already are
965 // size hints in effect that forbid requested client size.
967 const wxSize clientSize
= ComputeFittingClientSize(window
);
969 window
->SetMinClientSize(clientSize
);
970 window
->SetClientSize(clientSize
);
973 #if WXWIN_COMPATIBILITY_2_8
974 void wxSizer::SetVirtualSizeHints( wxWindow
*window
)
978 #endif // WXWIN_COMPATIBILITY_2_8
980 // TODO on mac we need a function that determines how much free space this
981 // min size contains, in order to make sure that we have 20 pixels of free
982 // space around the controls
983 wxSize
wxSizer::GetMaxClientSize( wxWindow
*window
) const
985 return window
->WindowToClientSize(window
->GetMaxSize());
988 wxSize
wxSizer::GetMinClientSize( wxWindow
*WXUNUSED(window
) )
990 return GetMinSize(); // Already returns client size.
993 wxSize
wxSizer::VirtualFitSize( wxWindow
*window
)
995 wxSize size
= GetMinClientSize( window
);
996 wxSize sizeMax
= GetMaxClientSize( window
);
998 // Limit the size if sizeMax != wxDefaultSize
1000 if ( size
.x
> sizeMax
.x
&& sizeMax
.x
!= wxDefaultCoord
)
1002 if ( size
.y
> sizeMax
.y
&& sizeMax
.y
!= wxDefaultCoord
)
1008 wxSize
wxSizer::GetMinSize()
1010 wxSize
ret( CalcMin() );
1011 if (ret
.x
< m_minSize
.x
) ret
.x
= m_minSize
.x
;
1012 if (ret
.y
< m_minSize
.y
) ret
.y
= m_minSize
.y
;
1016 void wxSizer::DoSetMinSize( int width
, int height
)
1018 m_minSize
.x
= width
;
1019 m_minSize
.y
= height
;
1022 bool wxSizer::DoSetItemMinSize( wxWindow
*window
, int width
, int height
)
1024 wxASSERT_MSG( window
, _T("SetMinSize for NULL window") );
1026 // Is it our immediate child?
1028 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
1031 wxSizerItem
*item
= node
->GetData();
1033 if (item
->GetWindow() == window
)
1035 item
->SetMinSize( width
, height
);
1038 node
= node
->GetNext();
1041 // No? Search any subsizers we own then
1043 node
= m_children
.GetFirst();
1046 wxSizerItem
*item
= node
->GetData();
1048 if ( item
->GetSizer() &&
1049 item
->GetSizer()->DoSetItemMinSize( window
, width
, height
) )
1051 // A child sizer found the requested windw, exit.
1054 node
= node
->GetNext();
1060 bool wxSizer::DoSetItemMinSize( wxSizer
*sizer
, int width
, int height
)
1062 wxASSERT_MSG( sizer
, _T("SetMinSize for NULL sizer") );
1064 // Is it our immediate child?
1066 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
1069 wxSizerItem
*item
= node
->GetData();
1071 if (item
->GetSizer() == sizer
)
1073 item
->GetSizer()->DoSetMinSize( width
, height
);
1076 node
= node
->GetNext();
1079 // No? Search any subsizers we own then
1081 node
= m_children
.GetFirst();
1084 wxSizerItem
*item
= node
->GetData();
1086 if ( item
->GetSizer() &&
1087 item
->GetSizer()->DoSetItemMinSize( sizer
, width
, height
) )
1089 // A child found the requested sizer, exit.
1092 node
= node
->GetNext();
1098 bool wxSizer::DoSetItemMinSize( size_t index
, int width
, int height
)
1100 wxSizerItemList::compatibility_iterator node
= m_children
.Item( index
);
1102 wxCHECK_MSG( node
, false, _T("Failed to find child node") );
1104 wxSizerItem
*item
= node
->GetData();
1106 if (item
->GetSizer())
1108 // Sizers contains the minimal size in them, if not calculated ...
1109 item
->GetSizer()->DoSetMinSize( width
, height
);
1113 // ... but the minimal size of spacers and windows is stored via the item
1114 item
->SetMinSize( width
, height
);
1120 wxSizerItem
* wxSizer::GetItem( wxWindow
*window
, bool recursive
)
1122 wxASSERT_MSG( window
, _T("GetItem for NULL window") );
1124 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
1127 wxSizerItem
*item
= node
->GetData();
1129 if (item
->GetWindow() == window
)
1133 else if (recursive
&& item
->IsSizer())
1135 wxSizerItem
*subitem
= item
->GetSizer()->GetItem( window
, true );
1140 node
= node
->GetNext();
1146 wxSizerItem
* wxSizer::GetItem( wxSizer
*sizer
, bool recursive
)
1148 wxASSERT_MSG( sizer
, _T("GetItem for NULL sizer") );
1150 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
1153 wxSizerItem
*item
= node
->GetData();
1155 if (item
->GetSizer() == sizer
)
1159 else if (recursive
&& item
->IsSizer())
1161 wxSizerItem
*subitem
= item
->GetSizer()->GetItem( sizer
, true );
1166 node
= node
->GetNext();
1172 wxSizerItem
* wxSizer::GetItem( size_t index
)
1174 wxCHECK_MSG( index
< m_children
.GetCount(),
1176 _T("GetItem index is out of range") );
1178 return m_children
.Item( index
)->GetData();
1181 wxSizerItem
* wxSizer::GetItemById( int id
, bool recursive
)
1183 // This gets a sizer item by the id of the sizer item
1184 // and NOT the id of a window if the item is a window.
1186 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
1189 wxSizerItem
*item
= node
->GetData();
1191 if (item
->GetId() == id
)
1195 else if (recursive
&& item
->IsSizer())
1197 wxSizerItem
*subitem
= item
->GetSizer()->GetItemById( id
, true );
1202 node
= node
->GetNext();
1208 bool wxSizer::Show( wxWindow
*window
, bool show
, bool recursive
)
1210 wxSizerItem
*item
= GetItem( window
, recursive
);
1221 bool wxSizer::Show( wxSizer
*sizer
, bool show
, bool recursive
)
1223 wxSizerItem
*item
= GetItem( sizer
, recursive
);
1234 bool wxSizer::Show( size_t index
, bool show
)
1236 wxSizerItem
*item
= GetItem( index
);
1247 void wxSizer::ShowItems( bool show
)
1249 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
1252 node
->GetData()->Show( show
);
1253 node
= node
->GetNext();
1257 bool wxSizer::IsShown( wxWindow
*window
) const
1259 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
1262 wxSizerItem
*item
= node
->GetData();
1264 if (item
->GetWindow() == window
)
1266 return item
->IsShown();
1268 node
= node
->GetNext();
1271 wxFAIL_MSG( _T("IsShown failed to find sizer item") );
1276 bool wxSizer::IsShown( wxSizer
*sizer
) const
1278 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
1281 wxSizerItem
*item
= node
->GetData();
1283 if (item
->GetSizer() == sizer
)
1285 return item
->IsShown();
1287 node
= node
->GetNext();
1290 wxFAIL_MSG( _T("IsShown failed to find sizer item") );
1295 bool wxSizer::IsShown( size_t index
) const
1297 wxCHECK_MSG( index
< m_children
.GetCount(),
1299 _T("IsShown index is out of range") );
1301 return m_children
.Item( index
)->GetData()->IsShown();
1305 //---------------------------------------------------------------------------
1307 //---------------------------------------------------------------------------
1309 wxGridSizer::wxGridSizer( int rows
, int cols
, int vgap
, int hgap
)
1310 : m_rows( ( cols
== 0 && rows
== 0 ) ? 1 : rows
)
1317 wxGridSizer::wxGridSizer( int cols
, int vgap
, int hgap
)
1318 : m_rows( cols
== 0 ? 1 : 0 )
1325 int wxGridSizer::CalcRowsCols(int& nrows
, int& ncols
) const
1327 int nitems
= m_children
.GetCount();
1333 nrows
= (nitems
+ m_cols
- 1) / m_cols
;
1337 ncols
= (nitems
+ m_rows
- 1) / m_rows
;
1340 else // 0 columns, 0 rows?
1342 wxFAIL_MSG( _T("grid sizer must have either rows or columns fixed") );
1351 void wxGridSizer::RecalcSizes()
1353 int nitems
, nrows
, ncols
;
1354 if ( (nitems
= CalcRowsCols(nrows
, ncols
)) == 0 )
1357 wxSize
sz( GetSize() );
1358 wxPoint
pt( GetPosition() );
1360 int w
= (sz
.x
- (ncols
- 1) * m_hgap
) / ncols
;
1361 int h
= (sz
.y
- (nrows
- 1) * m_vgap
) / nrows
;
1364 for (int c
= 0; c
< ncols
; c
++)
1367 for (int r
= 0; r
< nrows
; r
++)
1369 int i
= r
* ncols
+ c
;
1372 wxSizerItemList::compatibility_iterator node
= m_children
.Item( i
);
1374 wxASSERT_MSG( node
, _T("Failed to find SizerItemList node") );
1376 SetItemBounds( node
->GetData(), x
, y
, w
, h
);
1384 wxSize
wxGridSizer::CalcMin()
1387 if ( CalcRowsCols(nrows
, ncols
) == 0 )
1390 // Find the max width and height for any component
1394 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
1397 wxSizerItem
*item
= node
->GetData();
1398 wxSize
sz( item
->CalcMin() );
1400 w
= wxMax( w
, sz
.x
);
1401 h
= wxMax( h
, sz
.y
);
1403 node
= node
->GetNext();
1406 // In case we have a nested sizer with a two step algo , give it
1407 // a chance to adjust to that (we give it width component)
1408 node
= m_children
.GetFirst();
1409 bool didChangeMinSize
= false;
1412 wxSizerItem
*item
= node
->GetData();
1413 didChangeMinSize
|= item
->InformFirstDirection( wxHORIZONTAL
, w
, -1 );
1415 node
= node
->GetNext();
1418 // And redo iteration in case min size changed
1419 if( didChangeMinSize
)
1421 node
= m_children
.GetFirst();
1425 wxSizerItem
*item
= node
->GetData();
1426 wxSize
sz( item
->GetMinSizeWithBorder() );
1428 w
= wxMax( w
, sz
.x
);
1429 h
= wxMax( h
, sz
.y
);
1431 node
= node
->GetNext();
1435 return wxSize( ncols
* w
+ (ncols
-1) * m_hgap
,
1436 nrows
* h
+ (nrows
-1) * m_vgap
);
1439 void wxGridSizer::SetItemBounds( wxSizerItem
*item
, int x
, int y
, int w
, int h
)
1442 wxSize
sz( item
->GetMinSizeWithBorder() );
1443 int flag
= item
->GetFlag();
1445 if ((flag
& wxEXPAND
) || (flag
& wxSHAPED
))
1451 if (flag
& wxALIGN_CENTER_HORIZONTAL
)
1453 pt
.x
= x
+ (w
- sz
.x
) / 2;
1455 else if (flag
& wxALIGN_RIGHT
)
1457 pt
.x
= x
+ (w
- sz
.x
);
1460 if (flag
& wxALIGN_CENTER_VERTICAL
)
1462 pt
.y
= y
+ (h
- sz
.y
) / 2;
1464 else if (flag
& wxALIGN_BOTTOM
)
1466 pt
.y
= y
+ (h
- sz
.y
);
1470 item
->SetDimension(pt
, sz
);
1473 //---------------------------------------------------------------------------
1475 //---------------------------------------------------------------------------
1477 wxFlexGridSizer::wxFlexGridSizer( int rows
, int cols
, int vgap
, int hgap
)
1478 : wxGridSizer( rows
, cols
, vgap
, hgap
),
1479 m_flexDirection(wxBOTH
),
1480 m_growMode(wxFLEX_GROWMODE_SPECIFIED
)
1484 wxFlexGridSizer::wxFlexGridSizer( int cols
, int vgap
, int hgap
)
1485 : wxGridSizer( cols
, vgap
, hgap
),
1486 m_flexDirection(wxBOTH
),
1487 m_growMode(wxFLEX_GROWMODE_SPECIFIED
)
1491 wxFlexGridSizer::~wxFlexGridSizer()
1495 void wxFlexGridSizer::RecalcSizes()
1498 if ( !CalcRowsCols(nrows
, ncols
) )
1501 const wxPoint
pt(GetPosition());
1502 const wxSize
sz(GetSize());
1504 AdjustForGrowables(sz
);
1506 wxSizerItemList::const_iterator i
= m_children
.begin();
1507 const wxSizerItemList::const_iterator end
= m_children
.end();
1510 for ( int r
= 0; r
< nrows
; r
++ )
1512 if ( m_rowHeights
[r
] == -1 )
1514 // this row is entirely hidden, skip it
1515 for ( int c
= 0; c
< ncols
; c
++ )
1526 const int hrow
= m_rowHeights
[r
];
1527 int h
= sz
.y
- y
; // max remaining height, don't overflow it
1532 for ( int c
= 0; c
< ncols
&& i
!= end
; c
++, ++i
)
1534 const int wcol
= m_colWidths
[c
];
1539 int w
= sz
.x
- x
; // max possible value, ensure we don't overflow
1543 SetItemBounds(*i
, pt
.x
+ x
, pt
.y
+ y
, w
, h
);
1555 // helper function used in CalcMin() to sum up the sizes of non-hidden items
1556 static int SumArraySizes(const wxArrayInt
& sizes
, int gap
)
1558 // Sum total minimum size, including gaps between rows/columns.
1559 // -1 is used as a magic number meaning empty row/column.
1562 const size_t count
= sizes
.size();
1563 for ( size_t n
= 0; n
< count
; n
++ )
1565 if ( sizes
[n
] != -1 )
1568 total
+= gap
; // separate from the previous column
1577 void wxFlexGridSizer::FindWidthsAndHeights(int nrows
, int ncols
)
1579 // We have to recalculate the sizes in case the item minimum size has
1580 // changed since the previous layout, or the item has been hidden using
1581 // wxSizer::Show(). If all the items in a row/column are hidden, the final
1582 // dimension of the row/column will be -1, indicating that the column
1583 // itself is hidden.
1584 m_rowHeights
.assign(nrows
, -1);
1585 m_colWidths
.assign(ncols
, -1);
1587 // n is the index of the item in left-to-right top-to-bottom order
1589 for ( wxSizerItemList::iterator i
= m_children
.begin();
1590 i
!= m_children
.end();
1593 wxSizerItem
* const item
= *i
;
1594 if ( item
->IsShown() )
1596 // NOTE: Not doing the calculation here, this is just
1597 // for finding max values.
1598 const wxSize
sz(item
->GetMinSizeWithBorder());
1600 const int row
= n
/ ncols
;
1601 const int col
= n
% ncols
;
1603 if ( sz
.y
> m_rowHeights
[row
] )
1604 m_rowHeights
[row
] = sz
.y
;
1605 if ( sz
.x
> m_colWidths
[col
] )
1606 m_colWidths
[col
] = sz
.x
;
1610 AdjustForFlexDirection();
1612 m_calculatedMinSize
= wxSize(SumArraySizes(m_colWidths
, m_hgap
),
1613 SumArraySizes(m_rowHeights
, m_vgap
));
1616 wxSize
wxFlexGridSizer::CalcMin()
1621 // Number of rows/columns can change as items are added or removed.
1622 if ( !CalcRowsCols(nrows
, ncols
) )
1626 // We have to recalculate the sizes in case the item minimum size has
1627 // changed since the previous layout, or the item has been hidden using
1628 // wxSizer::Show(). If all the items in a row/column are hidden, the final
1629 // dimension of the row/column will be -1, indicating that the column
1630 // itself is hidden.
1631 m_rowHeights
.assign(nrows
, -1);
1632 m_colWidths
.assign(ncols
, -1);
1634 for ( wxSizerItemList::iterator i
= m_children
.begin();
1635 i
!= m_children
.end();
1638 wxSizerItem
* const item
= *i
;
1639 if ( item
->IsShown() )
1645 // The stage of looking for max values in each row/column has been
1646 // made a separate function, since it's reused in AdjustForGrowables.
1647 FindWidthsAndHeights(nrows
,ncols
);
1649 return m_calculatedMinSize
;
1652 void wxFlexGridSizer::AdjustForFlexDirection()
1654 // the logic in CalcMin works when we resize flexibly in both directions
1655 // but maybe this is not the case
1656 if ( m_flexDirection
!= wxBOTH
)
1658 // select the array corresponding to the direction in which we do *not*
1660 wxArrayInt
& array
= m_flexDirection
== wxVERTICAL
? m_colWidths
1663 const size_t count
= array
.GetCount();
1665 // find the largest value in this array
1669 for ( n
= 0; n
< count
; ++n
)
1671 if ( array
[n
] > largest
)
1675 // and now fill it with the largest value
1676 for ( n
= 0; n
< count
; ++n
)
1678 // don't touch hidden rows
1679 if ( array
[n
] != -1 )
1685 // helper of AdjustForGrowables() which is called for rows/columns separately
1688 // delta: the extra space, we do nothing unless it's positive
1689 // growable: indices or growable rows/cols in sizes array
1690 // sizes: the height/widths of rows/cols to adjust
1691 // proportions: proportions of the growable rows/cols or NULL if they all
1692 // should be assumed to have proportion of 1
1694 DoAdjustForGrowables(int delta
,
1695 const wxArrayInt
& growable
,
1697 const wxArrayInt
*proportions
)
1702 // total sum of proportions of all non-hidden rows
1703 int sum_proportions
= 0;
1705 // number of currently shown growable rows
1708 const int max_idx
= sizes
.size();
1710 const size_t count
= growable
.size();
1712 for ( idx
= 0; idx
< count
; idx
++ )
1714 // Since the number of rows/columns can change as items are
1715 // inserted/deleted, we need to verify at runtime that the
1716 // requested growable rows/columns are still valid.
1717 if ( growable
[idx
] >= max_idx
)
1720 // If all items in a row/column are hidden, that row/column will
1721 // have a dimension of -1. This causes the row/column to be
1722 // hidden completely.
1723 if ( sizes
[growable
[idx
]] == -1 )
1727 sum_proportions
+= (*proportions
)[idx
];
1735 // the remaining extra free space, adjusted during each iteration
1736 for ( idx
= 0; idx
< count
; idx
++ )
1738 if ( growable
[idx
] >= max_idx
)
1741 if ( sizes
[ growable
[idx
] ] == -1 )
1745 if ( sum_proportions
== 0 )
1747 // no growable rows -- divide extra space evenly among all
1748 cur_delta
= delta
/num
;
1751 else // allocate extra space proportionally
1753 const int cur_prop
= (*proportions
)[idx
];
1754 cur_delta
= (delta
*cur_prop
)/sum_proportions
;
1755 sum_proportions
-= cur_prop
;
1758 sizes
[growable
[idx
]] += cur_delta
;
1763 void wxFlexGridSizer::AdjustForGrowables(const wxSize
& sz
)
1765 if ( (m_flexDirection
& wxHORIZONTAL
) || (m_growMode
!= wxFLEX_GROWMODE_NONE
) )
1767 DoAdjustForGrowables
1769 sz
.x
- m_calculatedMinSize
.x
,
1772 m_growMode
== wxFLEX_GROWMODE_SPECIFIED
? &m_growableColsProportions
1776 // This gives nested objects that benefit from knowing one size
1777 // component in advance the chance to use that.
1778 bool didAdjustMinSize
= false;
1780 CalcRowsCols(nrows
, ncols
);
1782 // Iterate over all items and inform about column width
1784 for ( wxSizerItemList::iterator i
= m_children
.begin();
1785 i
!= m_children
.end();
1788 const int col
= n
% ncols
;
1789 didAdjustMinSize
|= (*i
)->InformFirstDirection(wxHORIZONTAL
, m_colWidths
[col
], sz
.y
- m_calculatedMinSize
.y
);
1792 // Only redo if info was actually used
1793 if( didAdjustMinSize
)
1795 DoAdjustForGrowables
1797 sz
.x
- m_calculatedMinSize
.x
,
1800 m_growMode
== wxFLEX_GROWMODE_SPECIFIED
? &m_growableColsProportions
1806 if ( (m_flexDirection
& wxVERTICAL
) || (m_growMode
!= wxFLEX_GROWMODE_NONE
) )
1808 // pass NULL instead of proportions if the grow mode is ALL as we
1809 // should treat all rows as having proportion of 1 then
1810 DoAdjustForGrowables
1812 sz
.y
- m_calculatedMinSize
.y
,
1815 m_growMode
== wxFLEX_GROWMODE_SPECIFIED
? &m_growableRowsProportions
1821 bool wxFlexGridSizer::IsRowGrowable( size_t idx
)
1823 return m_growableRows
.Index( idx
) != wxNOT_FOUND
;
1826 bool wxFlexGridSizer::IsColGrowable( size_t idx
)
1828 return m_growableCols
.Index( idx
) != wxNOT_FOUND
;
1831 void wxFlexGridSizer::AddGrowableRow( size_t idx
, int proportion
)
1833 wxASSERT_MSG( !IsRowGrowable( idx
),
1834 "AddGrowableRow() called for growable row" );
1835 m_growableRows
.Add( idx
);
1836 m_growableRowsProportions
.Add( proportion
);
1839 void wxFlexGridSizer::AddGrowableCol( size_t idx
, int proportion
)
1841 wxASSERT_MSG( !IsColGrowable( idx
),
1842 "AddGrowableCol() called for growable column" );
1843 m_growableCols
.Add( idx
);
1844 m_growableColsProportions
.Add( proportion
);
1847 // helper function for RemoveGrowableCol/Row()
1849 DoRemoveFromArrays(size_t idx
, wxArrayInt
& items
, wxArrayInt
& proportions
)
1851 const size_t count
= items
.size();
1852 for ( size_t n
= 0; n
< count
; n
++ )
1854 if ( (size_t)items
[n
] == idx
)
1857 proportions
.RemoveAt(n
);
1862 wxFAIL_MSG( _T("column/row is already not growable") );
1865 void wxFlexGridSizer::RemoveGrowableCol( size_t idx
)
1867 DoRemoveFromArrays(idx
, m_growableCols
, m_growableColsProportions
);
1870 void wxFlexGridSizer::RemoveGrowableRow( size_t idx
)
1872 DoRemoveFromArrays(idx
, m_growableRows
, m_growableRowsProportions
);
1875 //---------------------------------------------------------------------------
1877 //---------------------------------------------------------------------------
1879 void wxBoxSizer::RecalcSizes()
1881 if ( m_children
.empty() )
1884 const wxCoord totalMinorSize
= GetSizeInMinorDir(m_size
);
1886 // the amount of free space which we should redistribute among the
1887 // stretchable items (i.e. those with non zero proportion)
1888 int delta
= GetSizeInMajorDir(m_size
) - GetSizeInMajorDir(m_minSize
);
1891 // Inform child items about the size in minor direction, that can
1892 // change how much free space we have in major dir and how to distribute it.
1893 int majorMinSum
= 0;
1894 wxSizerItemList::const_iterator i
;
1895 for ( i
= m_children
.begin();
1896 i
!= m_children
.end();
1899 wxSizerItem
* const item
= *i
;
1901 if ( !item
->IsShown() )
1904 wxSize szMinPrev
= item
->GetMinSizeWithBorder();
1905 item
->InformFirstDirection(m_orient
^wxBOTH
,totalMinorSize
,delta
);
1906 wxSize szMin
= item
->GetMinSizeWithBorder();
1907 int deltaChange
= GetSizeInMajorDir(szMin
-szMinPrev
);
1910 // Since we passed available space along to the item, it should not
1911 // take too much, so delta should not become negative.
1912 delta
-= deltaChange
;
1914 majorMinSum
+= GetSizeInMajorDir(item
->GetMinSizeWithBorder());
1916 // And update our min size
1917 SizeInMajorDir(m_minSize
) = majorMinSum
;
1920 // might have a new delta now
1921 delta
= GetSizeInMajorDir(m_size
) - GetSizeInMajorDir(m_minSize
);
1923 // the position at which we put the next child
1924 wxPoint
pt(m_position
);
1926 int totalProportion
= m_totalProportion
;
1927 for ( i
= m_children
.begin();
1928 i
!= m_children
.end();
1931 wxSizerItem
* const item
= *i
;
1933 if ( !item
->IsShown() )
1936 const wxSize
sizeThis(item
->GetMinSizeWithBorder());
1938 // adjust the size in the major direction using the proportion
1939 wxCoord majorSize
= GetSizeInMajorDir(sizeThis
);
1941 // if there is not enough space, don't try to distribute negative space
1942 // among the children, this would result in overlapping windows which
1946 const int propItem
= item
->GetProportion();
1949 const int deltaItem
= (delta
* propItem
) / totalProportion
;
1951 majorSize
+= deltaItem
;
1954 totalProportion
-= propItem
;
1959 // apply the alignment in the minor direction
1960 wxPoint
posChild(pt
);
1962 wxCoord minorSize
= GetSizeInMinorDir(sizeThis
);
1963 const int flag
= item
->GetFlag();
1964 if ( flag
& (wxEXPAND
| wxSHAPED
) )
1966 minorSize
= totalMinorSize
;
1968 else if ( flag
& (IsVertical() ? wxALIGN_RIGHT
: wxALIGN_BOTTOM
) )
1970 PosInMinorDir(posChild
) += totalMinorSize
- minorSize
;
1972 // NB: wxCENTRE is used here only for backwards compatibility,
1973 // wxALIGN_CENTRE should be used in new code
1974 else if ( flag
& (wxCENTER
| wxALIGN_CENTRE
) )
1976 PosInMinorDir(posChild
) += (totalMinorSize
- minorSize
) / 2;
1980 // apply RTL adjustment for horizontal sizers:
1981 if ( !IsVertical() && m_containingWindow
)
1983 posChild
.x
= m_containingWindow
->AdjustForLayoutDirection
1991 // finally set size of this child and advance to the next one
1992 item
->SetDimension(posChild
, SizeFromMajorMinor(majorSize
, minorSize
));
1994 PosInMajorDir(pt
) += majorSize
;
1998 wxSize
wxBoxSizer::CalcMin()
2000 m_totalProportion
= 0;
2001 m_minSize
= wxSize(0, 0);
2003 // calculate the minimal sizes for all items and count sum of proportions
2004 for ( wxSizerItemList::const_iterator i
= m_children
.begin();
2005 i
!= m_children
.end();
2008 wxSizerItem
* const item
= *i
;
2010 if ( !item
->IsShown() )
2013 const wxSize sizeMinThis
= item
->CalcMin();
2014 SizeInMajorDir(m_minSize
) += GetSizeInMajorDir(sizeMinThis
);
2015 if ( GetSizeInMinorDir(sizeMinThis
) > GetSizeInMinorDir(m_minSize
) )
2016 SizeInMinorDir(m_minSize
) = GetSizeInMinorDir(sizeMinThis
);
2018 m_totalProportion
+= item
->GetProportion();
2024 //---------------------------------------------------------------------------
2026 //---------------------------------------------------------------------------
2030 wxStaticBoxSizer::wxStaticBoxSizer( wxStaticBox
*box
, int orient
)
2031 : wxBoxSizer( orient
),
2034 wxASSERT_MSG( box
, wxT("wxStaticBoxSizer needs a static box") );
2036 // do this so that our Detach() is called if the static box is destroyed
2038 m_staticBox
->SetContainingSizer(this);
2041 wxStaticBoxSizer::wxStaticBoxSizer(int orient
, wxWindow
*win
, const wxString
& s
)
2042 : wxBoxSizer(orient
),
2043 m_staticBox(new wxStaticBox(win
, wxID_ANY
, s
))
2046 m_staticBox
->SetContainingSizer(this);
2049 wxStaticBoxSizer::~wxStaticBoxSizer()
2054 static void GetStaticBoxBorders( wxStaticBox
*box
,
2058 // this has to be done platform by platform as there is no way to
2059 // guess the thickness of a wxStaticBox border
2060 box
->GetBordersForSizer(borderTop
, borderOther
);
2063 void wxStaticBoxSizer::RecalcSizes()
2065 int top_border
, other_border
;
2066 GetStaticBoxBorders(m_staticBox
, &top_border
, &other_border
);
2068 m_staticBox
->SetSize( m_position
.x
, m_position
.y
, m_size
.x
, m_size
.y
);
2070 wxPoint
old_pos( m_position
);
2071 m_position
.x
+= other_border
;
2072 m_position
.y
+= top_border
;
2073 wxSize
old_size( m_size
);
2074 m_size
.x
-= 2*other_border
;
2075 m_size
.y
-= top_border
+ other_border
;
2077 wxBoxSizer::RecalcSizes();
2079 m_position
= old_pos
;
2083 wxSize
wxStaticBoxSizer::CalcMin()
2085 int top_border
, other_border
;
2086 GetStaticBoxBorders(m_staticBox
, &top_border
, &other_border
);
2088 wxSize
ret( wxBoxSizer::CalcMin() );
2089 ret
.x
+= 2*other_border
;
2090 ret
.y
+= other_border
+ top_border
;
2095 void wxStaticBoxSizer::ShowItems( bool show
)
2097 m_staticBox
->Show( show
);
2098 wxBoxSizer::ShowItems( show
);
2101 bool wxStaticBoxSizer::Detach( wxWindow
*window
)
2103 // avoid deleting m_staticBox in our dtor if it's being detached from the
2104 // sizer (which can happen because it's being already destroyed for
2106 if ( window
== m_staticBox
)
2112 return wxSizer::Detach( window
);
2115 #endif // wxUSE_STATBOX
2119 wxStdDialogButtonSizer::wxStdDialogButtonSizer()
2120 : wxBoxSizer(wxHORIZONTAL
)
2122 // Vertical buttons with lots of space on either side
2123 // looks rubbish on WinCE, so let's not do this for now.
2124 // If we are going to use vertical buttons, we should
2125 // put the sizer to the right of other controls in the dialog,
2126 // and that's beyond the scope of this sizer.
2128 bool is_pda
= (wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA
);
2129 // If we have a PDA screen, put yes/no button over
2130 // all other buttons, otherwise on the left side.
2132 m_orient
= wxVERTICAL
;
2135 m_buttonAffirmative
= NULL
;
2136 m_buttonApply
= NULL
;
2137 m_buttonNegative
= NULL
;
2138 m_buttonCancel
= NULL
;
2139 m_buttonHelp
= NULL
;
2142 void wxStdDialogButtonSizer::AddButton(wxButton
*mybutton
)
2144 switch (mybutton
->GetId())
2149 m_buttonAffirmative
= mybutton
;
2152 m_buttonApply
= mybutton
;
2155 m_buttonNegative
= mybutton
;
2159 m_buttonCancel
= mybutton
;
2162 case wxID_CONTEXT_HELP
:
2163 m_buttonHelp
= mybutton
;
2170 void wxStdDialogButtonSizer::SetAffirmativeButton( wxButton
*button
)
2172 m_buttonAffirmative
= button
;
2175 void wxStdDialogButtonSizer::SetNegativeButton( wxButton
*button
)
2177 m_buttonNegative
= button
;
2180 void wxStdDialogButtonSizer::SetCancelButton( wxButton
*button
)
2182 m_buttonCancel
= button
;
2185 void wxStdDialogButtonSizer::Realize()
2188 Add(0, 0, 0, wxLEFT
, 6);
2190 Add((wxWindow
*)m_buttonHelp
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, 6);
2192 if (m_buttonNegative
){
2193 // HIG POLICE BULLETIN - destructive buttons need extra padding
2194 // 24 pixels on either side
2195 Add((wxWindow
*)m_buttonNegative
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, 12);
2198 // extra whitespace between help/negative and cancel/ok buttons
2199 Add(0, 0, 1, wxEXPAND
, 0);
2201 if (m_buttonCancel
){
2202 Add((wxWindow
*)m_buttonCancel
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, 6);
2203 // Cancel or help should be default
2204 // m_buttonCancel->SetDefaultButton();
2207 // Ugh, Mac doesn't really have apply dialogs, so I'll just
2208 // figure the best place is between Cancel and OK
2210 Add((wxWindow
*)m_buttonApply
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, 6);
2212 if (m_buttonAffirmative
){
2213 Add((wxWindow
*)m_buttonAffirmative
, 0, wxALIGN_CENTRE
| wxLEFT
, 6);
2215 if (m_buttonAffirmative
->GetId() == wxID_SAVE
){
2216 // these buttons have set labels under Mac so we should use them
2217 m_buttonAffirmative
->SetLabel(_("Save"));
2218 if (m_buttonNegative
)
2219 m_buttonNegative
->SetLabel(_("Don't Save"));
2223 // Extra space around and at the right
2225 #elif defined(__WXGTK20__)
2226 Add(0, 0, 0, wxLEFT
, 9);
2228 Add((wxWindow
*)m_buttonHelp
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, 3);
2230 // extra whitespace between help and cancel/ok buttons
2231 Add(0, 0, 1, wxEXPAND
, 0);
2233 if (m_buttonNegative
){
2234 Add((wxWindow
*)m_buttonNegative
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, 3);
2237 // according to HIG, in explicit apply windows the order is:
2238 // [ Help Apply Cancel OK ]
2240 Add((wxWindow
*)m_buttonApply
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, 3);
2242 if (m_buttonCancel
){
2243 Add((wxWindow
*)m_buttonCancel
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, 3);
2244 // Cancel or help should be default
2245 // m_buttonCancel->SetDefaultButton();
2248 if (m_buttonAffirmative
)
2249 Add((wxWindow
*)m_buttonAffirmative
, 0, wxALIGN_CENTRE
| wxLEFT
, 6);
2250 #elif defined(__WXMSW__)
2253 // right-justify buttons
2254 Add(0, 0, 1, wxEXPAND
, 0);
2256 if (m_buttonAffirmative
){
2257 Add((wxWindow
*)m_buttonAffirmative
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, m_buttonAffirmative
->ConvertDialogToPixels(wxSize(2, 0)).x
);
2260 if (m_buttonNegative
){
2261 Add((wxWindow
*)m_buttonNegative
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, m_buttonNegative
->ConvertDialogToPixels(wxSize(2, 0)).x
);
2264 if (m_buttonCancel
){
2265 Add((wxWindow
*)m_buttonCancel
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, m_buttonCancel
->ConvertDialogToPixels(wxSize(2, 0)).x
);
2268 Add((wxWindow
*)m_buttonApply
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, m_buttonApply
->ConvertDialogToPixels(wxSize(2, 0)).x
);
2271 Add((wxWindow
*)m_buttonHelp
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, m_buttonHelp
->ConvertDialogToPixels(wxSize(2, 0)).x
);
2273 // GTK+1 and any other platform
2275 // Add(0, 0, 0, wxLEFT, 5); // Not sure what this was for but it unbalances the dialog
2277 Add((wxWindow
*)m_buttonHelp
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, m_buttonHelp
->ConvertDialogToPixels(wxSize(4, 0)).x
);
2279 // extra whitespace between help and cancel/ok buttons
2280 Add(0, 0, 1, wxEXPAND
, 0);
2283 Add((wxWindow
*)m_buttonApply
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, m_buttonApply
->ConvertDialogToPixels(wxSize(4, 0)).x
);
2285 if (m_buttonAffirmative
){
2286 Add((wxWindow
*)m_buttonAffirmative
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, m_buttonAffirmative
->ConvertDialogToPixels(wxSize(4, 0)).x
);
2289 if (m_buttonNegative
){
2290 Add((wxWindow
*)m_buttonNegative
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, m_buttonNegative
->ConvertDialogToPixels(wxSize(4, 0)).x
);
2293 if (m_buttonCancel
){
2294 Add((wxWindow
*)m_buttonCancel
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, m_buttonCancel
->ConvertDialogToPixels(wxSize(4, 0)).x
);
2295 // Cancel or help should be default
2296 // m_buttonCancel->SetDefaultButton();
2302 #endif // wxUSE_BUTTON