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"
20 #include "wx/display.h"
22 #include "wx/private/flagscheck.h"
25 #include "wx/string.h"
29 #include "wx/settings.h"
30 #include "wx/button.h"
31 #include "wx/statbox.h"
32 #include "wx/toplevel.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
.x
, pos
.y
, size
.x
, size
.y
);
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 void wxSizer::SetDimension( int x
, int y
, int width
, int height
)
1017 wxSize
wxSizer::GetMinSize()
1019 wxSize
ret( CalcMin() );
1020 if (ret
.x
< m_minSize
.x
) ret
.x
= m_minSize
.x
;
1021 if (ret
.y
< m_minSize
.y
) ret
.y
= m_minSize
.y
;
1025 void wxSizer::DoSetMinSize( int width
, int height
)
1027 m_minSize
.x
= width
;
1028 m_minSize
.y
= height
;
1031 bool wxSizer::DoSetItemMinSize( wxWindow
*window
, int width
, int height
)
1033 wxASSERT_MSG( window
, _T("SetMinSize for NULL window") );
1035 // Is it our immediate child?
1037 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
1040 wxSizerItem
*item
= node
->GetData();
1042 if (item
->GetWindow() == window
)
1044 item
->SetMinSize( width
, height
);
1047 node
= node
->GetNext();
1050 // No? Search any subsizers we own then
1052 node
= m_children
.GetFirst();
1055 wxSizerItem
*item
= node
->GetData();
1057 if ( item
->GetSizer() &&
1058 item
->GetSizer()->DoSetItemMinSize( window
, width
, height
) )
1060 // A child sizer found the requested windw, exit.
1063 node
= node
->GetNext();
1069 bool wxSizer::DoSetItemMinSize( wxSizer
*sizer
, int width
, int height
)
1071 wxASSERT_MSG( sizer
, _T("SetMinSize for NULL sizer") );
1073 // Is it our immediate child?
1075 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
1078 wxSizerItem
*item
= node
->GetData();
1080 if (item
->GetSizer() == sizer
)
1082 item
->GetSizer()->DoSetMinSize( width
, height
);
1085 node
= node
->GetNext();
1088 // No? Search any subsizers we own then
1090 node
= m_children
.GetFirst();
1093 wxSizerItem
*item
= node
->GetData();
1095 if ( item
->GetSizer() &&
1096 item
->GetSizer()->DoSetItemMinSize( sizer
, width
, height
) )
1098 // A child found the requested sizer, exit.
1101 node
= node
->GetNext();
1107 bool wxSizer::DoSetItemMinSize( size_t index
, int width
, int height
)
1109 wxSizerItemList::compatibility_iterator node
= m_children
.Item( index
);
1111 wxCHECK_MSG( node
, false, _T("Failed to find child node") );
1113 wxSizerItem
*item
= node
->GetData();
1115 if (item
->GetSizer())
1117 // Sizers contains the minimal size in them, if not calculated ...
1118 item
->GetSizer()->DoSetMinSize( width
, height
);
1122 // ... but the minimal size of spacers and windows is stored via the item
1123 item
->SetMinSize( width
, height
);
1129 wxSizerItem
* wxSizer::GetItem( wxWindow
*window
, bool recursive
)
1131 wxASSERT_MSG( window
, _T("GetItem for NULL window") );
1133 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
1136 wxSizerItem
*item
= node
->GetData();
1138 if (item
->GetWindow() == window
)
1142 else if (recursive
&& item
->IsSizer())
1144 wxSizerItem
*subitem
= item
->GetSizer()->GetItem( window
, true );
1149 node
= node
->GetNext();
1155 wxSizerItem
* wxSizer::GetItem( wxSizer
*sizer
, bool recursive
)
1157 wxASSERT_MSG( sizer
, _T("GetItem for NULL sizer") );
1159 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
1162 wxSizerItem
*item
= node
->GetData();
1164 if (item
->GetSizer() == sizer
)
1168 else if (recursive
&& item
->IsSizer())
1170 wxSizerItem
*subitem
= item
->GetSizer()->GetItem( sizer
, true );
1175 node
= node
->GetNext();
1181 wxSizerItem
* wxSizer::GetItem( size_t index
)
1183 wxCHECK_MSG( index
< m_children
.GetCount(),
1185 _T("GetItem index is out of range") );
1187 return m_children
.Item( index
)->GetData();
1190 wxSizerItem
* wxSizer::GetItemById( int id
, bool recursive
)
1192 // This gets a sizer item by the id of the sizer item
1193 // and NOT the id of a window if the item is a window.
1195 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
1198 wxSizerItem
*item
= node
->GetData();
1200 if (item
->GetId() == id
)
1204 else if (recursive
&& item
->IsSizer())
1206 wxSizerItem
*subitem
= item
->GetSizer()->GetItemById( id
, true );
1211 node
= node
->GetNext();
1217 bool wxSizer::Show( wxWindow
*window
, bool show
, bool recursive
)
1219 wxSizerItem
*item
= GetItem( window
, recursive
);
1230 bool wxSizer::Show( wxSizer
*sizer
, bool show
, bool recursive
)
1232 wxSizerItem
*item
= GetItem( sizer
, recursive
);
1243 bool wxSizer::Show( size_t index
, bool show
)
1245 wxSizerItem
*item
= GetItem( index
);
1256 void wxSizer::ShowItems( bool show
)
1258 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
1261 node
->GetData()->Show( show
);
1262 node
= node
->GetNext();
1266 bool wxSizer::IsShown( wxWindow
*window
) const
1268 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
1271 wxSizerItem
*item
= node
->GetData();
1273 if (item
->GetWindow() == window
)
1275 return item
->IsShown();
1277 node
= node
->GetNext();
1280 wxFAIL_MSG( _T("IsShown failed to find sizer item") );
1285 bool wxSizer::IsShown( wxSizer
*sizer
) const
1287 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
1290 wxSizerItem
*item
= node
->GetData();
1292 if (item
->GetSizer() == sizer
)
1294 return item
->IsShown();
1296 node
= node
->GetNext();
1299 wxFAIL_MSG( _T("IsShown failed to find sizer item") );
1304 bool wxSizer::IsShown( size_t index
) const
1306 wxCHECK_MSG( index
< m_children
.GetCount(),
1308 _T("IsShown index is out of range") );
1310 return m_children
.Item( index
)->GetData()->IsShown();
1314 //---------------------------------------------------------------------------
1316 //---------------------------------------------------------------------------
1318 wxGridSizer::wxGridSizer( int rows
, int cols
, int vgap
, int hgap
)
1319 : m_rows( ( cols
== 0 && rows
== 0 ) ? 1 : rows
)
1326 wxGridSizer::wxGridSizer( int cols
, int vgap
, int hgap
)
1327 : m_rows( cols
== 0 ? 1 : 0 )
1334 int wxGridSizer::CalcRowsCols(int& nrows
, int& ncols
) const
1336 int nitems
= m_children
.GetCount();
1342 nrows
= (nitems
+ m_cols
- 1) / m_cols
;
1346 ncols
= (nitems
+ m_rows
- 1) / m_rows
;
1349 else // 0 columns, 0 rows?
1351 wxFAIL_MSG( _T("grid sizer must have either rows or columns fixed") );
1360 void wxGridSizer::RecalcSizes()
1362 int nitems
, nrows
, ncols
;
1363 if ( (nitems
= CalcRowsCols(nrows
, ncols
)) == 0 )
1366 wxSize
sz( GetSize() );
1367 wxPoint
pt( GetPosition() );
1369 int w
= (sz
.x
- (ncols
- 1) * m_hgap
) / ncols
;
1370 int h
= (sz
.y
- (nrows
- 1) * m_vgap
) / nrows
;
1373 for (int c
= 0; c
< ncols
; c
++)
1376 for (int r
= 0; r
< nrows
; r
++)
1378 int i
= r
* ncols
+ c
;
1381 wxSizerItemList::compatibility_iterator node
= m_children
.Item( i
);
1383 wxASSERT_MSG( node
, _T("Failed to find SizerItemList node") );
1385 SetItemBounds( node
->GetData(), x
, y
, w
, h
);
1393 wxSize
wxGridSizer::CalcMin()
1396 if ( CalcRowsCols(nrows
, ncols
) == 0 )
1399 // Find the max width and height for any component
1403 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
1406 wxSizerItem
*item
= node
->GetData();
1407 wxSize
sz( item
->CalcMin() );
1409 w
= wxMax( w
, sz
.x
);
1410 h
= wxMax( h
, sz
.y
);
1412 node
= node
->GetNext();
1415 // In case we have a nested sizer with a two step algo , give it
1416 // a chance to adjust to that (we give it width component)
1417 node
= m_children
.GetFirst();
1418 bool didChangeMinSize
= false;
1421 wxSizerItem
*item
= node
->GetData();
1422 didChangeMinSize
|= item
->InformFirstDirection( wxHORIZONTAL
, w
, -1 );
1424 node
= node
->GetNext();
1427 // And redo iteration in case min size changed
1428 if( didChangeMinSize
)
1430 node
= m_children
.GetFirst();
1434 wxSizerItem
*item
= node
->GetData();
1435 wxSize
sz( item
->GetMinSizeWithBorder() );
1437 w
= wxMax( w
, sz
.x
);
1438 h
= wxMax( h
, sz
.y
);
1440 node
= node
->GetNext();
1444 return wxSize( ncols
* w
+ (ncols
-1) * m_hgap
,
1445 nrows
* h
+ (nrows
-1) * m_vgap
);
1448 void wxGridSizer::SetItemBounds( wxSizerItem
*item
, int x
, int y
, int w
, int h
)
1451 wxSize
sz( item
->GetMinSizeWithBorder() );
1452 int flag
= item
->GetFlag();
1454 if ((flag
& wxEXPAND
) || (flag
& wxSHAPED
))
1460 if (flag
& wxALIGN_CENTER_HORIZONTAL
)
1462 pt
.x
= x
+ (w
- sz
.x
) / 2;
1464 else if (flag
& wxALIGN_RIGHT
)
1466 pt
.x
= x
+ (w
- sz
.x
);
1469 if (flag
& wxALIGN_CENTER_VERTICAL
)
1471 pt
.y
= y
+ (h
- sz
.y
) / 2;
1473 else if (flag
& wxALIGN_BOTTOM
)
1475 pt
.y
= y
+ (h
- sz
.y
);
1479 item
->SetDimension(pt
, sz
);
1482 //---------------------------------------------------------------------------
1484 //---------------------------------------------------------------------------
1486 wxFlexGridSizer::wxFlexGridSizer( int rows
, int cols
, int vgap
, int hgap
)
1487 : wxGridSizer( rows
, cols
, vgap
, hgap
),
1488 m_flexDirection(wxBOTH
),
1489 m_growMode(wxFLEX_GROWMODE_SPECIFIED
)
1493 wxFlexGridSizer::wxFlexGridSizer( int cols
, int vgap
, int hgap
)
1494 : wxGridSizer( cols
, vgap
, hgap
),
1495 m_flexDirection(wxBOTH
),
1496 m_growMode(wxFLEX_GROWMODE_SPECIFIED
)
1500 wxFlexGridSizer::~wxFlexGridSizer()
1504 void wxFlexGridSizer::RecalcSizes()
1507 if ( !CalcRowsCols(nrows
, ncols
) )
1510 const wxPoint
pt(GetPosition());
1511 const wxSize
sz(GetSize());
1513 AdjustForGrowables(sz
);
1515 wxSizerItemList::const_iterator i
= m_children
.begin();
1516 const wxSizerItemList::const_iterator end
= m_children
.end();
1519 for ( int r
= 0; r
< nrows
; r
++ )
1521 if ( m_rowHeights
[r
] == -1 )
1523 // this row is entirely hidden, skip it
1524 for ( int c
= 0; c
< ncols
; c
++ )
1535 const int hrow
= m_rowHeights
[r
];
1536 int h
= sz
.y
- y
; // max remaining height, don't overflow it
1541 for ( int c
= 0; c
< ncols
&& i
!= end
; c
++, ++i
)
1543 const int wcol
= m_colWidths
[c
];
1548 int w
= sz
.x
- x
; // max possible value, ensure we don't overflow
1552 SetItemBounds(*i
, pt
.x
+ x
, pt
.y
+ y
, w
, h
);
1564 // helper function used in CalcMin() to sum up the sizes of non-hidden items
1565 static int SumArraySizes(const wxArrayInt
& sizes
, int gap
)
1567 // Sum total minimum size, including gaps between rows/columns.
1568 // -1 is used as a magic number meaning empty row/column.
1571 const size_t count
= sizes
.size();
1572 for ( size_t n
= 0; n
< count
; n
++ )
1574 if ( sizes
[n
] != -1 )
1577 total
+= gap
; // separate from the previous column
1586 void wxFlexGridSizer::FindWidthsAndHeights(int nrows
, int ncols
)
1588 // We have to recalculate the sizes in case the item minimum size has
1589 // changed since the previous layout, or the item has been hidden using
1590 // wxSizer::Show(). If all the items in a row/column are hidden, the final
1591 // dimension of the row/column will be -1, indicating that the column
1592 // itself is hidden.
1593 m_rowHeights
.assign(nrows
, -1);
1594 m_colWidths
.assign(ncols
, -1);
1596 // n is the index of the item in left-to-right top-to-bottom order
1598 for ( wxSizerItemList::iterator i
= m_children
.begin();
1599 i
!= m_children
.end();
1602 wxSizerItem
* const item
= *i
;
1603 if ( item
->IsShown() )
1605 // NOTE: Not doing the calculation here, this is just
1606 // for finding max values.
1607 const wxSize
sz(item
->GetMinSizeWithBorder());
1609 const int row
= n
/ ncols
;
1610 const int col
= n
% ncols
;
1612 if ( sz
.y
> m_rowHeights
[row
] )
1613 m_rowHeights
[row
] = sz
.y
;
1614 if ( sz
.x
> m_colWidths
[col
] )
1615 m_colWidths
[col
] = sz
.x
;
1619 AdjustForFlexDirection();
1621 m_calculatedMinSize
= wxSize(SumArraySizes(m_colWidths
, m_hgap
),
1622 SumArraySizes(m_rowHeights
, m_vgap
));
1625 wxSize
wxFlexGridSizer::CalcMin()
1630 // Number of rows/columns can change as items are added or removed.
1631 if ( !CalcRowsCols(nrows
, ncols
) )
1635 // We have to recalculate the sizes in case the item minimum size has
1636 // changed since the previous layout, or the item has been hidden using
1637 // wxSizer::Show(). If all the items in a row/column are hidden, the final
1638 // dimension of the row/column will be -1, indicating that the column
1639 // itself is hidden.
1640 m_rowHeights
.assign(nrows
, -1);
1641 m_colWidths
.assign(ncols
, -1);
1643 // n is the index of the item in left-to-right top-to-bottom order
1645 for ( wxSizerItemList::iterator i
= m_children
.begin();
1646 i
!= m_children
.end();
1649 wxSizerItem
* const item
= *i
;
1650 if ( item
->IsShown() )
1656 // The stage of looking for max values in each row/column has been
1657 // made a separate function, since it's reused in AdjustForGrowables.
1658 FindWidthsAndHeights(nrows
,ncols
);
1660 return m_calculatedMinSize
;
1663 void wxFlexGridSizer::AdjustForFlexDirection()
1665 // the logic in CalcMin works when we resize flexibly in both directions
1666 // but maybe this is not the case
1667 if ( m_flexDirection
!= wxBOTH
)
1669 // select the array corresponding to the direction in which we do *not*
1671 wxArrayInt
& array
= m_flexDirection
== wxVERTICAL
? m_colWidths
1674 const size_t count
= array
.GetCount();
1676 // find the largest value in this array
1680 for ( n
= 0; n
< count
; ++n
)
1682 if ( array
[n
] > largest
)
1686 // and now fill it with the largest value
1687 for ( n
= 0; n
< count
; ++n
)
1689 // don't touch hidden rows
1690 if ( array
[n
] != -1 )
1696 // helper of AdjustForGrowables() which is called for rows/columns separately
1699 // delta: the extra space, we do nothing unless it's positive
1700 // growable: indices or growable rows/cols in sizes array
1701 // sizes: the height/widths of rows/cols to adjust
1702 // proportions: proportions of the growable rows/cols or NULL if they all
1703 // should be assumed to have proportion of 1
1705 DoAdjustForGrowables(int delta
,
1706 const wxArrayInt
& growable
,
1708 const wxArrayInt
*proportions
)
1713 // total sum of proportions of all non-hidden rows
1714 int sum_proportions
= 0;
1716 // number of currently shown growable rows
1719 const int max_idx
= sizes
.size();
1721 const size_t count
= growable
.size();
1723 for ( idx
= 0; idx
< count
; idx
++ )
1725 // Since the number of rows/columns can change as items are
1726 // inserted/deleted, we need to verify at runtime that the
1727 // requested growable rows/columns are still valid.
1728 if ( growable
[idx
] >= max_idx
)
1731 // If all items in a row/column are hidden, that row/column will
1732 // have a dimension of -1. This causes the row/column to be
1733 // hidden completely.
1734 if ( sizes
[growable
[idx
]] == -1 )
1738 sum_proportions
+= (*proportions
)[idx
];
1746 // the remaining extra free space, adjusted during each iteration
1747 for ( idx
= 0; idx
< count
; idx
++ )
1749 if ( growable
[idx
] >= max_idx
)
1752 if ( sizes
[ growable
[idx
] ] == -1 )
1756 if ( sum_proportions
== 0 )
1758 // no growable rows -- divide extra space evenly among all
1759 cur_delta
= delta
/num
;
1762 else // allocate extra space proportionally
1764 const int cur_prop
= (*proportions
)[idx
];
1765 cur_delta
= (delta
*cur_prop
)/sum_proportions
;
1766 sum_proportions
-= cur_prop
;
1769 sizes
[growable
[idx
]] += cur_delta
;
1774 void wxFlexGridSizer::AdjustForGrowables(const wxSize
& sz
)
1776 if ( (m_flexDirection
& wxHORIZONTAL
) || (m_growMode
!= wxFLEX_GROWMODE_NONE
) )
1778 DoAdjustForGrowables
1780 sz
.x
- m_calculatedMinSize
.x
,
1783 m_growMode
== wxFLEX_GROWMODE_SPECIFIED
? &m_growableColsProportions
1787 // This gives nested objects that benefit from knowing one size
1788 // component in advance the chance to use that.
1789 bool didAdjustMinSize
= false;
1791 CalcRowsCols(nrows
, ncols
);
1793 // Iterate over all items and inform about column width
1795 for ( wxSizerItemList::iterator i
= m_children
.begin();
1796 i
!= m_children
.end();
1799 const int col
= n
% ncols
;
1800 didAdjustMinSize
|= (*i
)->InformFirstDirection(wxHORIZONTAL
, m_colWidths
[col
], sz
.y
- m_calculatedMinSize
.y
);
1803 // Only redo if info was actually used
1804 if( didAdjustMinSize
)
1806 DoAdjustForGrowables
1808 sz
.x
- m_calculatedMinSize
.x
,
1811 m_growMode
== wxFLEX_GROWMODE_SPECIFIED
? &m_growableColsProportions
1817 if ( (m_flexDirection
& wxVERTICAL
) || (m_growMode
!= wxFLEX_GROWMODE_NONE
) )
1819 // pass NULL instead of proportions if the grow mode is ALL as we
1820 // should treat all rows as having proportion of 1 then
1821 DoAdjustForGrowables
1823 sz
.y
- m_calculatedMinSize
.y
,
1826 m_growMode
== wxFLEX_GROWMODE_SPECIFIED
? &m_growableRowsProportions
1833 void wxFlexGridSizer::AddGrowableRow( size_t idx
, int proportion
)
1835 m_growableRows
.Add( idx
);
1836 m_growableRowsProportions
.Add( proportion
);
1839 void wxFlexGridSizer::AddGrowableCol( size_t idx
, int proportion
)
1841 m_growableCols
.Add( idx
);
1842 m_growableColsProportions
.Add( proportion
);
1845 // helper function for RemoveGrowableCol/Row()
1847 DoRemoveFromArrays(size_t idx
, wxArrayInt
& items
, wxArrayInt
& proportions
)
1849 const size_t count
= items
.size();
1850 for ( size_t n
= 0; n
< count
; n
++ )
1852 if ( (size_t)items
[n
] == idx
)
1855 proportions
.RemoveAt(n
);
1860 wxFAIL_MSG( _T("column/row is already not growable") );
1863 void wxFlexGridSizer::RemoveGrowableCol( size_t idx
)
1865 DoRemoveFromArrays(idx
, m_growableCols
, m_growableColsProportions
);
1868 void wxFlexGridSizer::RemoveGrowableRow( size_t idx
)
1870 DoRemoveFromArrays(idx
, m_growableRows
, m_growableRowsProportions
);
1873 //---------------------------------------------------------------------------
1875 //---------------------------------------------------------------------------
1877 void wxBoxSizer::RecalcSizes()
1879 if ( m_children
.empty() )
1882 const wxCoord totalMinorSize
= GetSizeInMinorDir(m_size
);
1884 // the amount of free space which we should redistribute among the
1885 // stretchable items (i.e. those with non zero proportion)
1886 int delta
= GetSizeInMajorDir(m_size
) - GetSizeInMajorDir(m_minSize
);
1889 // Inform child items about the size in minor direction, that can
1890 // change how much free space we have in major dir and how to distribute it.
1891 int majorMinSum
= 0;
1892 wxSizerItemList::const_iterator i
;
1893 for ( i
= m_children
.begin();
1894 i
!= m_children
.end();
1897 wxSizerItem
* const item
= *i
;
1899 if ( !item
->IsShown() )
1902 wxSize szMinPrev
= item
->GetMinSizeWithBorder();
1903 item
->InformFirstDirection(m_orient
^wxBOTH
,totalMinorSize
,delta
);
1904 wxSize szMin
= item
->GetMinSizeWithBorder();
1905 int deltaChange
= GetSizeInMajorDir(szMin
-szMinPrev
);
1908 // Since we passed available space along to the item, it should not
1909 // take too much, so delta should not become negative.
1910 delta
-= deltaChange
;
1912 majorMinSum
+= GetSizeInMajorDir(item
->GetMinSizeWithBorder());
1914 // And update our min size
1915 SizeInMajorDir(m_minSize
) = majorMinSum
;
1918 // might have a new delta now
1919 delta
= GetSizeInMajorDir(m_size
) - GetSizeInMajorDir(m_minSize
);
1921 // the position at which we put the next child
1922 wxPoint
pt(m_position
);
1924 int totalProportion
= m_totalProportion
;
1925 for ( i
= m_children
.begin();
1926 i
!= m_children
.end();
1929 wxSizerItem
* const item
= *i
;
1931 if ( !item
->IsShown() )
1934 const wxSize
sizeThis(item
->GetMinSizeWithBorder());
1936 // adjust the size in the major direction using the proportion
1937 wxCoord majorSize
= GetSizeInMajorDir(sizeThis
);
1938 const int propItem
= item
->GetProportion();
1941 const int deltaItem
= (delta
* propItem
) / totalProportion
;
1943 majorSize
+= deltaItem
;
1946 totalProportion
-= propItem
;
1950 // apply the alignment in the minor direction
1951 wxPoint
posChild(pt
);
1953 wxCoord minorSize
= GetSizeInMinorDir(sizeThis
);
1954 const int flag
= item
->GetFlag();
1955 if ( flag
& (wxEXPAND
| wxSHAPED
) )
1957 minorSize
= totalMinorSize
;
1959 else if ( flag
& (IsVertical() ? wxALIGN_RIGHT
: wxALIGN_BOTTOM
) )
1961 PosInMinorDir(posChild
) += totalMinorSize
- minorSize
;
1963 // NB: wxCENTRE is used here only for backwards compatibility,
1964 // wxALIGN_CENTRE should be used in new code
1965 else if ( flag
& (wxCENTER
| wxALIGN_CENTRE
) )
1967 PosInMinorDir(posChild
) += (totalMinorSize
- minorSize
) / 2;
1971 // apply RTL adjustment for horizontal sizers:
1972 if ( !IsVertical() && m_containingWindow
)
1974 posChild
.x
= m_containingWindow
->AdjustForLayoutDirection
1982 // finally set size of this child and advance to the next one
1983 item
->SetDimension(posChild
, SizeFromMajorMinor(majorSize
, minorSize
));
1985 PosInMajorDir(pt
) += majorSize
;
1989 wxSize
wxBoxSizer::CalcMin()
1991 m_totalProportion
= 0;
1992 m_minSize
= wxSize(0, 0);
1994 // calculate the minimal sizes for all items and count sum of proportions
1995 for ( wxSizerItemList::const_iterator i
= m_children
.begin();
1996 i
!= m_children
.end();
1999 wxSizerItem
* const item
= *i
;
2001 if ( !item
->IsShown() )
2004 const wxSize sizeMinThis
= item
->CalcMin();
2005 SizeInMajorDir(m_minSize
) += GetSizeInMajorDir(sizeMinThis
);
2006 if ( GetSizeInMinorDir(sizeMinThis
) > GetSizeInMinorDir(m_minSize
) )
2007 SizeInMinorDir(m_minSize
) = GetSizeInMinorDir(sizeMinThis
);
2009 m_totalProportion
+= item
->GetProportion();
2015 //---------------------------------------------------------------------------
2017 //---------------------------------------------------------------------------
2019 #define wxDEFAULT_PROPORTION_LAST 1000000
2021 // User data to hold old proportion for last item on line
2022 // (which might be extended)
2023 struct wxPropHolder
: public wxObject
2025 wxPropHolder( ) : m_item(0), m_propOld(0) { }
2026 void Init( wxSizerItem
*item
, int propOld
) { m_item
=item
; m_propOld
=propOld
; }
2028 wxSizerItem
*m_item
;
2032 IMPLEMENT_DYNAMIC_CLASS(wxWrapSizer
, wxBoxSizer
);
2034 wxWrapSizer::wxWrapSizer( int orient
, int flags
)
2035 : wxBoxSizer(orient
),
2036 m_prim_size_last( -1 ),
2037 m_rows(orient
^wxBOTH
),
2042 wxWrapSizer::~wxWrapSizer()
2044 // Have to clear grand child items so that they're not deleted twice
2045 for( int ix
=m_rows
.GetChildren().GetCount()-1; ix
>=0; ix
-- )
2047 wxSizer
*psz
= m_rows
.GetItem((size_t)ix
)->GetSizer();
2048 wxSizerItemList
&sl
= psz
->GetChildren();
2049 while( sl
.GetLast() )
2050 sl
.Erase( sl
.GetLast() );
2055 bool wxWrapSizer::InformFirstDirection( int direction
, int size
, int WXUNUSED(availableOtherDir
) )
2059 // Better to keep value, then CalcMin will work better
2060 //m_prim_size_last = -1;
2063 if( direction
==m_orient
)
2065 // The direction is same as our primary, so we can make use of it
2066 m_prim_size_last
= size
;
2074 void wxWrapSizer::AdjustPropLastItem(wxSizer
*psz
, wxSizerItem
*itemLast
)
2076 wxSizerItem
*psi
= m_rows
.GetItem(psz
);
2078 wxPropHolder
*pph
= (wxPropHolder
*)psi
->GetUserData();
2080 psi
->SetUserData( pph
=new wxPropHolder
);
2082 pph
->Init( itemLast
, itemLast
->GetProportion() );
2083 itemLast
->SetProportion( wxDEFAULT_PROPORTION_LAST
);
2086 void wxWrapSizer::RecalcSizes()
2088 wxASSERT( m_orient
&wxBOTH
);
2089 if (m_children
.GetCount() == 0)
2092 // What we do here is to put our items into child box sizers,
2093 // as many of them as we have lines.
2095 // Empty all items in all rows in owned sizer.
2096 // We have to access the list directly, since we don't want to
2097 // destroy the wxSizerItems.
2098 for( int ix
=m_rows
.GetChildren().GetCount()-1; ix
>=0; ix
-- ){
2099 wxSizerItem
*psi
= m_rows
.GetItem( (size_t)ix
);
2101 // Restore proportion for last item on line (if item has not been deleted)
2102 wxPropHolder
*pph
= (wxPropHolder
*)psi
->GetUserData();
2103 if( pph
&& GetChildren().Find(pph
->m_item
) )
2104 pph
->m_item
->SetProportion(pph
->m_propOld
);
2106 wxSizer
*psz
= psi
->GetSizer();
2108 wxSizerItemList
&sl
= psz
->GetChildren();
2109 while( sl
.GetLast() )
2110 sl
.Erase( sl
.GetLast() );
2113 int lineSumMajor
= 0;
2114 int majorSize
= GetSizeInMajorDir(m_size
);
2116 // Make sure we have at least one child sizer
2118 if( !m_rows
.GetChildren().GetCount() )
2119 m_rows
.Add( new wxBoxSizer(GetOrientation()), 1, wxEXPAND
);
2121 // The sizer where to insert items in
2122 wxSizer
*psz
= m_rows
.GetItem((size_t)0)->GetSizer();
2125 // Now put our child items into child sizers instead
2126 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
2127 wxSizerItem
*item
= NULL
, *itemLast
=NULL
;
2130 item
= node
->GetData();
2131 if ( item
->IsShown() )
2133 wxSize minSz
= item
->GetMinSize();
2134 int minSzMajor
= GetSizeInMajorDir(minSz
);
2136 // More space on this line?
2137 if( !lineSumMajor
|| lineSumMajor
+minSzMajor
<=majorSize
)
2139 lineSumMajor
+= minSzMajor
;
2143 lineSumMajor
= minSzMajor
;
2144 // Get a new empty sizer to insert into
2145 if( (int)m_rows
.GetChildren().GetCount()<=m_n_line
)
2146 m_rows
.Add( new wxBoxSizer(GetOrientation()), 1, wxEXPAND
);
2148 // If we have extend-last-on-each-line mode, then do so now
2149 // Note: We must store old proportion value then.
2150 if( m_flags
&wxEXTEND_LAST_ON_EACH_LINE
)
2151 AdjustPropLastItem(psz
,itemLast
);
2153 // The sizer where to insert items in
2154 psz
= m_rows
.GetItem(m_n_line
++)->GetSizer();
2158 // If item is a window, it now has a pointer to the child sizer,
2159 // which is wrong. Set it to point to us.
2160 if( item
->GetWindow() )
2161 item
->GetWindow()->SetContainingSizer( this );
2163 node
= node
->GetNext();
2166 // If we have extend-last-on-each-line mode, then do so now
2167 if( m_flags
&wxEXTEND_LAST_ON_EACH_LINE
)
2168 AdjustPropLastItem(psz
,itemLast
);
2170 // If we have more sizers than lines, remove them
2171 while( (int)m_rows
.GetChildren().GetCount()>m_n_line
)
2172 m_rows
.Remove( m_n_line
);
2174 // Now do layout on row sizer
2175 m_rows
.SetDimension( m_position
.x
, m_position
.y
, m_size
.x
, m_size
.y
);
2177 // Remember this to next time (will be overridden by InformFirstDirection if used)
2178 m_prim_size_last
= GetSizeInMajorDir(m_size
);
2182 wxSize
wxWrapSizer::CalcMin()
2184 if (m_children
.GetCount() == 0)
2187 // Algorithm for calculating min size: (assuming horizontal orientation)
2188 // X: Max width of all members
2189 // Y: Based on last X, calculate how many lines needed
2190 // First time around, assume all items fits on one line
2194 int lineMaxMinor
= 0;
2195 int lineSumMajor
= 0;
2198 // precalc item minsizes and fit on lines (preliminary)
2199 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
2202 wxSizerItem
*item
= node
->GetData();
2203 if ( item
->IsShown() )
2205 wxSize minSz
= item
->CalcMin();
2206 int szMajor
= GetSizeInMajorDir(minSz
);
2207 int szMinor
= GetSizeInMinorDir(minSz
);
2208 if( szMajor
>maxMajor
) maxMajor
= szMajor
;
2209 // More space on this line?
2210 if( m_prim_size_last
<0 || !lineSumMajor
||
2211 lineSumMajor
+szMajor
<=m_prim_size_last
)
2213 lineSumMajor
+= szMajor
;
2214 if( szMinor
>lineMaxMinor
)
2215 lineMaxMinor
= szMinor
;
2219 minorSum
+= lineMaxMinor
; // Add height of highest item on last line
2221 lineMaxMinor
= szMinor
;
2222 lineSumMajor
= szMajor
;
2225 node
= node
->GetNext();
2227 minorSum
+= lineMaxMinor
; // Add height of highest item on last line
2229 m_minSize
= SizeFromMajorMinor(maxMajor
, minorSum
);
2233 //---------------------------------------------------------------------------
2235 //---------------------------------------------------------------------------
2239 wxStaticBoxSizer::wxStaticBoxSizer( wxStaticBox
*box
, int orient
)
2240 : wxBoxSizer( orient
),
2243 wxASSERT_MSG( box
, wxT("wxStaticBoxSizer needs a static box") );
2245 // do this so that our Detach() is called if the static box is destroyed
2247 m_staticBox
->SetContainingSizer(this);
2250 wxStaticBoxSizer::wxStaticBoxSizer(int orient
, wxWindow
*win
, const wxString
& s
)
2251 : wxBoxSizer(orient
),
2252 m_staticBox(new wxStaticBox(win
, wxID_ANY
, s
))
2255 m_staticBox
->SetContainingSizer(this);
2258 wxStaticBoxSizer::~wxStaticBoxSizer()
2263 static void GetStaticBoxBorders( wxStaticBox
*box
,
2267 // this has to be done platform by platform as there is no way to
2268 // guess the thickness of a wxStaticBox border
2269 box
->GetBordersForSizer(borderTop
, borderOther
);
2272 void wxStaticBoxSizer::RecalcSizes()
2274 int top_border
, other_border
;
2275 GetStaticBoxBorders(m_staticBox
, &top_border
, &other_border
);
2277 m_staticBox
->SetSize( m_position
.x
, m_position
.y
, m_size
.x
, m_size
.y
);
2279 wxPoint
old_pos( m_position
);
2280 m_position
.x
+= other_border
;
2281 m_position
.y
+= top_border
;
2282 wxSize
old_size( m_size
);
2283 m_size
.x
-= 2*other_border
;
2284 m_size
.y
-= top_border
+ other_border
;
2286 wxBoxSizer::RecalcSizes();
2288 m_position
= old_pos
;
2292 wxSize
wxStaticBoxSizer::CalcMin()
2294 int top_border
, other_border
;
2295 GetStaticBoxBorders(m_staticBox
, &top_border
, &other_border
);
2297 wxSize
ret( wxBoxSizer::CalcMin() );
2298 ret
.x
+= 2*other_border
;
2299 ret
.y
+= other_border
+ top_border
;
2304 void wxStaticBoxSizer::ShowItems( bool show
)
2306 m_staticBox
->Show( show
);
2307 wxBoxSizer::ShowItems( show
);
2310 bool wxStaticBoxSizer::Detach( wxWindow
*window
)
2312 // avoid deleting m_staticBox in our dtor if it's being detached from the
2313 // sizer (which can happen because it's being already destroyed for
2315 if ( window
== m_staticBox
)
2321 return wxSizer::Detach( window
);
2324 #endif // wxUSE_STATBOX
2328 wxStdDialogButtonSizer::wxStdDialogButtonSizer()
2329 : wxBoxSizer(wxHORIZONTAL
)
2331 // Vertical buttons with lots of space on either side
2332 // looks rubbish on WinCE, so let's not do this for now.
2333 // If we are going to use vertical buttons, we should
2334 // put the sizer to the right of other controls in the dialog,
2335 // and that's beyond the scope of this sizer.
2337 bool is_pda
= (wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA
);
2338 // If we have a PDA screen, put yes/no button over
2339 // all other buttons, otherwise on the left side.
2341 m_orient
= wxVERTICAL
;
2344 m_buttonAffirmative
= NULL
;
2345 m_buttonApply
= NULL
;
2346 m_buttonNegative
= NULL
;
2347 m_buttonCancel
= NULL
;
2348 m_buttonHelp
= NULL
;
2351 void wxStdDialogButtonSizer::AddButton(wxButton
*mybutton
)
2353 switch (mybutton
->GetId())
2358 m_buttonAffirmative
= mybutton
;
2361 m_buttonApply
= mybutton
;
2364 m_buttonNegative
= mybutton
;
2368 m_buttonCancel
= mybutton
;
2371 case wxID_CONTEXT_HELP
:
2372 m_buttonHelp
= mybutton
;
2379 void wxStdDialogButtonSizer::SetAffirmativeButton( wxButton
*button
)
2381 m_buttonAffirmative
= button
;
2384 void wxStdDialogButtonSizer::SetNegativeButton( wxButton
*button
)
2386 m_buttonNegative
= button
;
2389 void wxStdDialogButtonSizer::SetCancelButton( wxButton
*button
)
2391 m_buttonCancel
= button
;
2394 void wxStdDialogButtonSizer::Realize()
2397 Add(0, 0, 0, wxLEFT
, 6);
2399 Add((wxWindow
*)m_buttonHelp
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, 6);
2401 if (m_buttonNegative
){
2402 // HIG POLICE BULLETIN - destructive buttons need extra padding
2403 // 24 pixels on either side
2404 Add((wxWindow
*)m_buttonNegative
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, 12);
2407 // extra whitespace between help/negative and cancel/ok buttons
2408 Add(0, 0, 1, wxEXPAND
, 0);
2410 if (m_buttonCancel
){
2411 Add((wxWindow
*)m_buttonCancel
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, 6);
2412 // Cancel or help should be default
2413 // m_buttonCancel->SetDefaultButton();
2416 // Ugh, Mac doesn't really have apply dialogs, so I'll just
2417 // figure the best place is between Cancel and OK
2419 Add((wxWindow
*)m_buttonApply
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, 6);
2421 if (m_buttonAffirmative
){
2422 Add((wxWindow
*)m_buttonAffirmative
, 0, wxALIGN_CENTRE
| wxLEFT
, 6);
2424 if (m_buttonAffirmative
->GetId() == wxID_SAVE
){
2425 // these buttons have set labels under Mac so we should use them
2426 m_buttonAffirmative
->SetLabel(_("Save"));
2427 if (m_buttonNegative
)
2428 m_buttonNegative
->SetLabel(_("Don't Save"));
2432 // Extra space around and at the right
2434 #elif defined(__WXGTK20__)
2435 Add(0, 0, 0, wxLEFT
, 9);
2437 Add((wxWindow
*)m_buttonHelp
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, 3);
2439 // extra whitespace between help and cancel/ok buttons
2440 Add(0, 0, 1, wxEXPAND
, 0);
2442 if (m_buttonNegative
){
2443 Add((wxWindow
*)m_buttonNegative
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, 3);
2446 // according to HIG, in explicit apply windows the order is:
2447 // [ Help Apply Cancel OK ]
2449 Add((wxWindow
*)m_buttonApply
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, 3);
2451 if (m_buttonCancel
){
2452 Add((wxWindow
*)m_buttonCancel
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, 3);
2453 // Cancel or help should be default
2454 // m_buttonCancel->SetDefaultButton();
2457 if (m_buttonAffirmative
)
2458 Add((wxWindow
*)m_buttonAffirmative
, 0, wxALIGN_CENTRE
| wxLEFT
, 6);
2459 #elif defined(__WXMSW__)
2462 // right-justify buttons
2463 Add(0, 0, 1, wxEXPAND
, 0);
2465 if (m_buttonAffirmative
){
2466 Add((wxWindow
*)m_buttonAffirmative
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, m_buttonAffirmative
->ConvertDialogToPixels(wxSize(2, 0)).x
);
2469 if (m_buttonNegative
){
2470 Add((wxWindow
*)m_buttonNegative
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, m_buttonNegative
->ConvertDialogToPixels(wxSize(2, 0)).x
);
2473 if (m_buttonCancel
){
2474 Add((wxWindow
*)m_buttonCancel
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, m_buttonCancel
->ConvertDialogToPixels(wxSize(2, 0)).x
);
2477 Add((wxWindow
*)m_buttonApply
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, m_buttonApply
->ConvertDialogToPixels(wxSize(2, 0)).x
);
2480 Add((wxWindow
*)m_buttonHelp
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, m_buttonHelp
->ConvertDialogToPixels(wxSize(2, 0)).x
);
2482 // GTK+1 and any other platform
2484 // Add(0, 0, 0, wxLEFT, 5); // Not sure what this was for but it unbalances the dialog
2486 Add((wxWindow
*)m_buttonHelp
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, m_buttonHelp
->ConvertDialogToPixels(wxSize(4, 0)).x
);
2488 // extra whitespace between help and cancel/ok buttons
2489 Add(0, 0, 1, wxEXPAND
, 0);
2492 Add((wxWindow
*)m_buttonApply
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, m_buttonApply
->ConvertDialogToPixels(wxSize(4, 0)).x
);
2494 if (m_buttonAffirmative
){
2495 Add((wxWindow
*)m_buttonAffirmative
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, m_buttonAffirmative
->ConvertDialogToPixels(wxSize(4, 0)).x
);
2498 if (m_buttonNegative
){
2499 Add((wxWindow
*)m_buttonNegative
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, m_buttonNegative
->ConvertDialogToPixels(wxSize(4, 0)).x
);
2502 if (m_buttonCancel
){
2503 Add((wxWindow
*)m_buttonCancel
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, m_buttonCancel
->ConvertDialogToPixels(wxSize(4, 0)).x
);
2504 // Cancel or help should be default
2505 // m_buttonCancel->SetDefaultButton();
2511 #endif // wxUSE_BUTTON