1 /////////////////////////////////////////////////////////////////////////////
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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
14 #pragma implementation "sizer.h"
17 // For compilers that support precompilation, includes "wx.h".
18 #include "wx/wxprec.h"
26 #include "wx/statbox.h"
27 #include "wx/listimpl.cpp"
28 #if WXWIN_COMPATIBILITY_2_4
29 #include "wx/notebook.h"
33 # include "wx/mac/uma.h"
36 //---------------------------------------------------------------------------
38 IMPLEMENT_CLASS(wxSizerItem
, wxObject
)
39 IMPLEMENT_CLASS(wxSizer
, wxObject
)
40 IMPLEMENT_CLASS(wxGridSizer
, wxSizer
)
41 IMPLEMENT_CLASS(wxFlexGridSizer
, wxGridSizer
)
42 IMPLEMENT_CLASS(wxBoxSizer
, wxSizer
)
44 IMPLEMENT_CLASS(wxStaticBoxSizer
, wxBoxSizer
)
47 WX_DEFINE_EXPORTED_LIST( wxSizerItemList
);
82 //---------------------------------------------------------------------------
84 //---------------------------------------------------------------------------
86 void wxSizerItem::Init()
92 m_zoneRect
= wxRect( 0, 0, 0, 0 );
95 void wxSizerItem::Init(const wxSizerFlags
& flags
)
99 m_proportion
= flags
.GetProportion();
100 m_flag
= flags
.GetFlags();
101 m_border
= flags
.GetBorderInPixels();
104 wxSizerItem::wxSizerItem( int width
, int height
, int proportion
, int flag
, int border
, wxObject
* userData
)
107 , m_size( wxSize( width
, height
) ) // size is set directly
108 , m_minSize( m_size
) // minimal size is the initial size
109 , m_proportion( proportion
)
112 , m_zoneRect( 0, 0, 0, 0 )
114 , m_userData( userData
)
119 wxSizerItem::wxSizerItem( wxWindow
*window
, int proportion
, int flag
, int border
, wxObject
* userData
)
122 , m_proportion( proportion
)
125 , m_zoneRect( 0, 0, 0, 0 )
127 , m_userData( userData
)
129 if (flag
& wxFIXED_MINSIZE
)
130 window
->SetMinSize(window
->GetSize());
131 m_minSize
= window
->GetSize();
133 // aspect ratio calculated from initial size
134 SetRatio( m_minSize
);
136 // m_size is calculated later
139 wxSizerItem::wxSizerItem( wxSizer
*sizer
, int proportion
, int flag
, int border
, wxObject
* userData
)
142 , m_proportion( proportion
)
145 , m_zoneRect( 0, 0, 0, 0 )
148 , m_userData( userData
)
150 // m_minSize is calculated later
151 // m_size is calculated later
154 wxSizerItem::wxSizerItem()
163 wxSizerItem::~wxSizerItem()
169 m_window
->SetContainingSizer(NULL
);
171 else // we must be a sizer
178 wxSize
wxSizerItem::GetSize() const
182 ret
= m_sizer
->GetSize();
185 ret
= m_window
->GetSize();
192 if (m_flag
& wxNORTH
)
194 if (m_flag
& wxSOUTH
)
200 wxSize
wxSizerItem::CalcMin()
204 m_minSize
= m_sizer
->GetMinSize();
206 // if we have to preserve aspect ratio _AND_ this is
207 // the first-time calculation, consider ret to be initial size
208 if ((m_flag
& wxSHAPED
) && !m_ratio
)
211 else if ( IsWindow() )
213 // Since the size of the window may change during runtime, we
214 // should use the current minimal/best size.
215 m_minSize
= m_window
->GetBestFittingSize();
218 return GetMinSizeWithBorder();
221 wxSize
wxSizerItem::GetMinSizeWithBorder() const
223 wxSize ret
= m_minSize
;
229 if (m_flag
& wxNORTH
)
231 if (m_flag
& wxSOUTH
)
238 void wxSizerItem::SetDimension( wxPoint pos
, wxSize size
)
240 if (m_flag
& wxSHAPED
)
242 // adjust aspect ratio
243 int rwidth
= (int) (size
.y
* m_ratio
);
247 int rheight
= (int) (size
.x
/ m_ratio
);
248 // add vertical space
249 if (m_flag
& wxALIGN_CENTER_VERTICAL
)
250 pos
.y
+= (size
.y
- rheight
) / 2;
251 else if (m_flag
& wxALIGN_BOTTOM
)
252 pos
.y
+= (size
.y
- rheight
);
253 // use reduced dimensions
256 else if (rwidth
< size
.x
)
258 // add horizontal space
259 if (m_flag
& wxALIGN_CENTER_HORIZONTAL
)
260 pos
.x
+= (size
.x
- rwidth
) / 2;
261 else if (m_flag
& wxALIGN_RIGHT
)
262 pos
.x
+= (size
.x
- rwidth
);
267 // This is what GetPosition() returns. Since we calculate
268 // borders afterwards, GetPosition() will be the left/top
269 // corner of the surrounding border.
281 if (m_flag
& wxNORTH
)
286 if (m_flag
& wxSOUTH
)
292 m_sizer
->SetDimension( pos
.x
, pos
.y
, size
.x
, size
.y
);
294 m_zoneRect
= wxRect(pos
, size
);
296 m_window
->SetSize( pos
.x
, pos
.y
, size
.x
, size
.y
, wxSIZE_ALLOW_MINUS_ONE
);
301 void wxSizerItem::DeleteWindows()
310 m_sizer
->DeleteWindows();
313 bool wxSizerItem::IsWindow() const
315 return (m_window
!= NULL
);
318 bool wxSizerItem::IsSizer() const
320 return (m_sizer
!= NULL
);
323 bool wxSizerItem::IsSpacer() const
325 return (m_window
== NULL
) && (m_sizer
== NULL
);
328 void wxSizerItem::Show( bool show
)
333 m_window
->Show( show
);
335 m_sizer
->ShowItems( show
);
337 // ... nothing else to do to hide/show spacers
340 void wxSizerItem::SetOption( int option
)
342 SetProportion( option
);
345 int wxSizerItem::GetOption() const
347 return GetProportion();
351 //---------------------------------------------------------------------------
353 //---------------------------------------------------------------------------
356 : m_minSize( wxSize( 0, 0 ) )
362 WX_CLEAR_LIST(wxSizerItemList
, m_children
);
365 wxSizerItem
* wxSizer::Insert( size_t index
, wxSizerItem
*item
)
367 m_children
.Insert( index
, item
);
369 if( item
->GetWindow() )
370 item
->GetWindow()->SetContainingSizer( this );
375 bool wxSizer::Remove( wxWindow
*window
)
377 return Detach( window
);
380 bool wxSizer::Remove( wxSizer
*sizer
)
382 wxASSERT_MSG( sizer
, _T("Removing NULL sizer") );
384 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
387 wxSizerItem
*item
= node
->GetData();
389 if (item
->GetSizer() == sizer
)
392 m_children
.Erase( node
);
396 node
= node
->GetNext();
402 bool wxSizer::Remove( int index
)
404 wxCHECK_MSG( index
>= 0 && (size_t)index
< m_children
.GetCount(),
406 _T("Remove index is out of range") );
408 wxSizerItemList::compatibility_iterator node
= m_children
.Item( index
);
410 wxCHECK_MSG( node
, false, _T("Failed to find child node") );
412 wxSizerItem
*item
= node
->GetData();
414 if( item
->IsWindow() )
415 item
->GetWindow()->SetContainingSizer( NULL
);
418 m_children
.Erase( node
);
422 bool wxSizer::Detach( wxSizer
*sizer
)
424 wxASSERT_MSG( sizer
, _T("Detaching NULL sizer") );
426 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
429 wxSizerItem
*item
= node
->GetData();
431 if (item
->GetSizer() == sizer
)
435 m_children
.Erase( node
);
438 node
= node
->GetNext();
444 bool wxSizer::Detach( wxWindow
*window
)
446 wxASSERT_MSG( window
, _T("Detaching NULL window") );
448 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
451 wxSizerItem
*item
= node
->GetData();
453 if (item
->GetWindow() == window
)
455 item
->GetWindow()->SetContainingSizer( NULL
);
457 m_children
.Erase( node
);
460 node
= node
->GetNext();
466 bool wxSizer::Detach( int index
)
468 wxCHECK_MSG( index
>= 0 && (size_t)index
< m_children
.GetCount(),
470 _T("Detach index is out of range") );
472 wxSizerItemList::compatibility_iterator node
= m_children
.Item( index
);
474 wxCHECK_MSG( node
, false, _T("Failed to find child node") );
476 wxSizerItem
*item
= node
->GetData();
478 if( item
->IsSizer() )
480 else if( item
->IsWindow() )
481 item
->GetWindow()->SetContainingSizer( NULL
);
484 m_children
.Erase( node
);
488 void wxSizer::Clear( bool delete_windows
)
490 // First clear the ContainingSizer pointers
491 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
494 wxSizerItem
*item
= node
->GetData();
496 if (item
->IsWindow())
497 item
->GetWindow()->SetContainingSizer( NULL
);
498 node
= node
->GetNext();
501 // Destroy the windows if needed
505 // Now empty the list
506 WX_CLEAR_LIST(wxSizerItemList
, m_children
);
509 void wxSizer::DeleteWindows()
511 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
514 wxSizerItem
*item
= node
->GetData();
516 item
->DeleteWindows();
517 node
= node
->GetNext();
521 wxSize
wxSizer::Fit( wxWindow
*window
)
523 wxSize
size(window
->IsTopLevel() ? FitSize(window
)
524 : GetMinWindowSize(window
));
526 window
->SetSize( size
);
531 void wxSizer::FitInside( wxWindow
*window
)
534 if (window
->IsTopLevel())
535 size
= VirtualFitSize( window
);
537 size
= GetMinClientSize( window
);
539 window
->SetVirtualSize( size
);
542 void wxSizer::Layout()
544 // (re)calculates minimums needed for each item and other preparations
548 // Applies the layout and repositions/resizes the items
552 void wxSizer::SetSizeHints( wxWindow
*window
)
554 // Preserve the window's max size hints, but set the
555 // lower bound according to the sizer calculations.
557 wxSize size
= Fit( window
);
559 window
->SetSizeHints( size
.x
,
561 window
->GetMaxWidth(),
562 window
->GetMaxHeight() );
565 void wxSizer::SetVirtualSizeHints( wxWindow
*window
)
567 // Preserve the window's max size hints, but set the
568 // lower bound according to the sizer calculations.
571 wxSize
size( window
->GetVirtualSize() );
572 window
->SetVirtualSizeHints( size
.x
,
574 window
->GetMaxWidth(),
575 window
->GetMaxHeight() );
578 wxSize
wxSizer::GetMaxWindowSize( wxWindow
*window
) const
580 return window
->GetMaxSize();
583 wxSize
wxSizer::GetMinWindowSize( wxWindow
*window
)
585 wxSize
minSize( GetMinSize() );
586 wxSize
size( window
->GetSize() );
587 wxSize
client_size( window
->GetClientSize() );
589 return wxSize( minSize
.x
+size
.x
-client_size
.x
,
590 minSize
.y
+size
.y
-client_size
.y
);
593 // TODO on mac we need a function that determines how much free space this
594 // min size contains, in order to make sure that we have 20 pixels of free
595 // space around the controls
597 // Return a window size that will fit within the screens dimensions
598 wxSize
wxSizer::FitSize( wxWindow
*window
)
600 wxSize size
= GetMinWindowSize( window
);
601 wxSize sizeMax
= GetMaxWindowSize( window
);
603 // Limit the size if sizeMax != wxDefaultSize
605 if ( size
.x
> sizeMax
.x
&& sizeMax
.x
!= wxDefaultCoord
)
607 if ( size
.y
> sizeMax
.y
&& sizeMax
.y
!= wxDefaultCoord
)
613 wxSize
wxSizer::GetMaxClientSize( wxWindow
*window
) const
615 wxSize
maxSize( window
->GetMaxSize() );
617 if( maxSize
!= wxDefaultSize
)
619 wxSize
size( window
->GetSize() );
620 wxSize
client_size( window
->GetClientSize() );
622 return wxSize( maxSize
.x
+ client_size
.x
- size
.x
,
623 maxSize
.y
+ client_size
.y
- size
.y
);
626 return wxDefaultSize
;
629 wxSize
wxSizer::GetMinClientSize( wxWindow
*WXUNUSED(window
) )
631 return GetMinSize(); // Already returns client size.
634 wxSize
wxSizer::VirtualFitSize( wxWindow
*window
)
636 wxSize size
= GetMinClientSize( window
);
637 wxSize sizeMax
= GetMaxClientSize( window
);
639 // Limit the size if sizeMax != wxDefaultSize
641 if ( size
.x
> sizeMax
.x
&& sizeMax
.x
!= wxDefaultCoord
)
643 if ( size
.y
> sizeMax
.y
&& sizeMax
.y
!= wxDefaultCoord
)
649 void wxSizer::SetDimension( int x
, int y
, int width
, int height
)
658 wxSize
wxSizer::GetMinSize()
660 wxSize
ret( CalcMin() );
661 if (ret
.x
< m_minSize
.x
) ret
.x
= m_minSize
.x
;
662 if (ret
.y
< m_minSize
.y
) ret
.y
= m_minSize
.y
;
666 void wxSizer::DoSetMinSize( int width
, int height
)
669 m_minSize
.y
= height
;
672 bool wxSizer::DoSetItemMinSize( wxWindow
*window
, int width
, int height
)
674 wxASSERT_MSG( window
, _T("SetMinSize for NULL window") );
676 // Is it our immediate child?
678 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
681 wxSizerItem
*item
= node
->GetData();
683 if (item
->GetWindow() == window
)
685 item
->SetMinSize( width
, height
);
688 node
= node
->GetNext();
691 // No? Search any subsizers we own then
693 node
= m_children
.GetFirst();
696 wxSizerItem
*item
= node
->GetData();
698 if ( item
->GetSizer() &&
699 item
->GetSizer()->DoSetItemMinSize( window
, width
, height
) )
701 // A child sizer found the requested windw, exit.
704 node
= node
->GetNext();
710 bool wxSizer::DoSetItemMinSize( wxSizer
*sizer
, int width
, int height
)
712 wxASSERT_MSG( sizer
, _T("SetMinSize for NULL sizer") );
714 // Is it our immediate child?
716 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
719 wxSizerItem
*item
= node
->GetData();
721 if (item
->GetSizer() == sizer
)
723 item
->GetSizer()->DoSetMinSize( width
, height
);
726 node
= node
->GetNext();
729 // No? Search any subsizers we own then
731 node
= m_children
.GetFirst();
734 wxSizerItem
*item
= node
->GetData();
736 if ( item
->GetSizer() &&
737 item
->GetSizer()->DoSetItemMinSize( sizer
, width
, height
) )
739 // A child found the requested sizer, exit.
742 node
= node
->GetNext();
748 bool wxSizer::DoSetItemMinSize( size_t index
, int width
, int height
)
750 wxSizerItemList::compatibility_iterator node
= m_children
.Item( index
);
752 wxCHECK_MSG( node
, false, _T("Failed to find child node") );
754 wxSizerItem
*item
= node
->GetData();
756 if (item
->GetSizer())
758 // Sizers contains the minimal size in them, if not calculated ...
759 item
->GetSizer()->DoSetMinSize( width
, height
);
763 // ... but the minimal size of spacers and windows is stored via the item
764 item
->SetMinSize( width
, height
);
770 wxSizerItem
* wxSizer::GetItem( wxWindow
*window
, bool recursive
)
772 wxASSERT_MSG( window
, _T("GetItem for NULL window") );
774 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
777 wxSizerItem
*item
= node
->GetData();
779 if (item
->GetWindow() == window
)
783 else if (recursive
&& item
->IsSizer())
785 wxSizerItem
*subitem
= item
->GetSizer()->GetItem( window
, true );
790 node
= node
->GetNext();
796 wxSizerItem
* wxSizer::GetItem( wxSizer
*sizer
, bool recursive
)
798 wxASSERT_MSG( sizer
, _T("GetItem for NULL sizer") );
800 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
803 wxSizerItem
*item
= node
->GetData();
805 if (item
->GetSizer() == sizer
)
809 else if (recursive
&& item
->IsSizer())
811 wxSizerItem
*subitem
= item
->GetSizer()->GetItem( sizer
, true );
816 node
= node
->GetNext();
822 wxSizerItem
* wxSizer::GetItem( size_t index
)
824 wxCHECK_MSG( index
< m_children
.GetCount(),
826 _T("GetItem index is out of range") );
828 return m_children
.Item( index
)->GetData();
831 bool wxSizer::Show( wxWindow
*window
, bool show
, bool recursive
)
833 wxSizerItem
*item
= GetItem( window
, recursive
);
844 bool wxSizer::Show( wxSizer
*sizer
, bool show
, bool recursive
)
846 wxSizerItem
*item
= GetItem( sizer
, recursive
);
857 bool wxSizer::Show( size_t index
, bool show
)
859 wxSizerItem
*item
= GetItem( index
);
870 void wxSizer::ShowItems( bool show
)
872 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
875 node
->GetData()->Show( show
);
876 node
= node
->GetNext();
880 bool wxSizer::IsShown( wxWindow
*window
) const
882 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
885 wxSizerItem
*item
= node
->GetData();
887 if (item
->GetWindow() == window
)
889 return item
->IsShown();
891 node
= node
->GetNext();
894 wxFAIL_MSG( _T("IsShown failed to find sizer item") );
899 bool wxSizer::IsShown( wxSizer
*sizer
) const
901 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
904 wxSizerItem
*item
= node
->GetData();
906 if (item
->GetSizer() == sizer
)
908 return item
->IsShown();
910 node
= node
->GetNext();
913 wxFAIL_MSG( _T("IsShown failed to find sizer item") );
918 bool wxSizer::IsShown( size_t index
) const
920 wxCHECK_MSG( index
< m_children
.GetCount(),
922 _T("IsShown index is out of range") );
924 return m_children
.Item( index
)->GetData()->IsShown();
928 //---------------------------------------------------------------------------
930 //---------------------------------------------------------------------------
932 wxGridSizer::wxGridSizer( int rows
, int cols
, int vgap
, int hgap
)
938 if (m_rows
== 0 && m_cols
== 0)
942 wxGridSizer::wxGridSizer( int cols
, int vgap
, int hgap
)
948 if (m_rows
== 0 && m_cols
== 0)
952 int wxGridSizer::CalcRowsCols(int& nrows
, int& ncols
) const
954 int nitems
= m_children
.GetCount();
960 nrows
= (nitems
+ m_cols
- 1) / m_cols
;
964 ncols
= (nitems
+ m_rows
- 1) / m_rows
;
967 else // 0 columns, 0 rows?
969 wxFAIL_MSG( _T("grid sizer must have either rows or columns fixed") );
978 void wxGridSizer::RecalcSizes()
980 int nitems
, nrows
, ncols
;
981 if ( (nitems
= CalcRowsCols(nrows
, ncols
)) == 0 )
984 wxSize
sz( GetSize() );
985 wxPoint
pt( GetPosition() );
987 int w
= (sz
.x
- (ncols
- 1) * m_hgap
) / ncols
;
988 int h
= (sz
.y
- (nrows
- 1) * m_vgap
) / nrows
;
991 for (int c
= 0; c
< ncols
; c
++)
994 for (int r
= 0; r
< nrows
; r
++)
996 int i
= r
* ncols
+ c
;
999 wxSizerItemList::compatibility_iterator node
= m_children
.Item( i
);
1001 wxASSERT_MSG( node
, _T("Failed to find SizerItemList node") );
1003 SetItemBounds( node
->GetData(), x
, y
, w
, h
);
1011 wxSize
wxGridSizer::CalcMin()
1014 if ( CalcRowsCols(nrows
, ncols
) == 0 )
1015 return wxSize(10, 10);
1017 // Find the max width and height for any component
1021 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
1024 wxSizerItem
*item
= node
->GetData();
1025 wxSize
sz( item
->CalcMin() );
1027 w
= wxMax( w
, sz
.x
);
1028 h
= wxMax( h
, sz
.y
);
1030 node
= node
->GetNext();
1033 return wxSize( ncols
* w
+ (ncols
-1) * m_hgap
,
1034 nrows
* h
+ (nrows
-1) * m_vgap
);
1037 void wxGridSizer::SetItemBounds( wxSizerItem
*item
, int x
, int y
, int w
, int h
)
1040 wxSize
sz( item
->GetMinSizeWithBorder() );
1041 int flag
= item
->GetFlag();
1043 if ((flag
& wxEXPAND
) || (flag
& wxSHAPED
))
1049 if (flag
& wxALIGN_CENTER_HORIZONTAL
)
1051 pt
.x
= x
+ (w
- sz
.x
) / 2;
1053 else if (flag
& wxALIGN_RIGHT
)
1055 pt
.x
= x
+ (w
- sz
.x
);
1058 if (flag
& wxALIGN_CENTER_VERTICAL
)
1060 pt
.y
= y
+ (h
- sz
.y
) / 2;
1062 else if (flag
& wxALIGN_BOTTOM
)
1064 pt
.y
= y
+ (h
- sz
.y
);
1068 item
->SetDimension(pt
, sz
);
1071 //---------------------------------------------------------------------------
1073 //---------------------------------------------------------------------------
1075 wxFlexGridSizer::wxFlexGridSizer( int rows
, int cols
, int vgap
, int hgap
)
1076 : wxGridSizer( rows
, cols
, vgap
, hgap
),
1077 m_flexDirection(wxBOTH
),
1078 m_growMode(wxFLEX_GROWMODE_SPECIFIED
)
1082 wxFlexGridSizer::wxFlexGridSizer( int cols
, int vgap
, int hgap
)
1083 : wxGridSizer( cols
, vgap
, hgap
),
1084 m_flexDirection(wxBOTH
),
1085 m_growMode(wxFLEX_GROWMODE_SPECIFIED
)
1089 wxFlexGridSizer::~wxFlexGridSizer()
1093 void wxFlexGridSizer::RecalcSizes()
1095 int nitems
, nrows
, ncols
;
1096 if ( (nitems
= CalcRowsCols(nrows
, ncols
)) == 0 )
1099 wxPoint
pt( GetPosition() );
1100 wxSize
sz( GetSize() );
1102 AdjustForGrowables(sz
, m_calculatedMinSize
, nrows
, ncols
);
1104 sz
= wxSize( pt
.x
+ sz
.x
, pt
.y
+ sz
.y
);
1107 for (int c
= 0; c
< ncols
; c
++)
1110 for (int r
= 0; r
< nrows
; r
++)
1112 int i
= r
* ncols
+ c
;
1115 wxSizerItemList::compatibility_iterator node
= m_children
.Item( i
);
1117 wxASSERT_MSG( node
, _T("Failed to find node") );
1119 int w
= wxMax( 0, wxMin( m_colWidths
[c
], sz
.x
- x
) );
1120 int h
= wxMax( 0, wxMin( m_rowHeights
[r
], sz
.y
- y
) );
1122 SetItemBounds( node
->GetData(), x
, y
, w
, h
);
1124 y
= y
+ m_rowHeights
[r
] + m_vgap
;
1126 x
= x
+ m_colWidths
[c
] + m_hgap
;
1130 wxSize
wxFlexGridSizer::CalcMin()
1136 // Number of rows/columns can change as items are added or removed.
1137 if ( !CalcRowsCols(nrows
, ncols
) )
1138 return wxSize(10, 10);
1140 m_rowHeights
.SetCount(nrows
);
1141 m_colWidths
.SetCount(ncols
);
1143 // We have to recalcuate the sizes in case the item minimum size has
1144 // changed since the previous layout, or the item has been hidden using
1145 // wxSizer::Show(). If all the items in a row/column are hidden, the final
1146 // dimension of the row/column will be -1, indicating that the column
1147 // itself is hidden.
1148 for( s
= m_rowHeights
.GetCount(), i
= 0; i
< s
; ++i
)
1149 m_rowHeights
[ i
] = -1;
1150 for( s
= m_colWidths
.GetCount(), i
= 0; i
< s
; ++i
)
1151 m_colWidths
[ i
] = -1;
1153 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
1158 wxSizerItem
*item
= node
->GetData();
1159 if ( item
->IsShown() )
1161 wxSize
sz( item
->CalcMin() );
1162 int row
= i
/ ncols
;
1163 int col
= i
% ncols
;
1165 m_rowHeights
[ row
] = wxMax( wxMax( 0, sz
.y
), m_rowHeights
[ row
] );
1166 m_colWidths
[ col
] = wxMax( wxMax( 0, sz
.x
), m_colWidths
[ col
] );
1169 node
= node
->GetNext();
1173 AdjustForFlexDirection();
1175 // Sum total minimum size, including gaps between rows/columns.
1176 // -1 is used as a magic number meaning empty column.
1178 for (int col
= 0; col
< ncols
; col
++)
1179 if ( m_colWidths
[ col
] != -1 )
1180 width
+= m_colWidths
[ col
] + ( col
== ncols
-1 ? 0 : m_hgap
);
1183 for (int row
= 0; row
< nrows
; row
++)
1184 if ( m_rowHeights
[ row
] != -1 )
1185 height
+= m_rowHeights
[ row
] + ( row
== nrows
-1 ? 0 : m_vgap
);
1187 m_calculatedMinSize
= wxSize( width
, height
);
1188 return m_calculatedMinSize
;
1191 void wxFlexGridSizer::AdjustForFlexDirection()
1193 // the logic in CalcMin works when we resize flexibly in both directions
1194 // but maybe this is not the case
1195 if ( m_flexDirection
!= wxBOTH
)
1197 // select the array corresponding to the direction in which we do *not*
1199 wxArrayInt
& array
= m_flexDirection
== wxVERTICAL
? m_colWidths
1202 const int count
= array
.GetCount();
1204 // find the largest value in this array
1206 for ( n
= 0; n
< count
; ++n
)
1208 if ( array
[n
] > largest
)
1212 // and now fill it with the largest value
1213 for ( n
= 0; n
< count
; ++n
)
1221 void wxFlexGridSizer::AdjustForGrowables(const wxSize
& sz
, const wxSize
& minsz
,
1222 int nrows
, int ncols
)
1224 // what to do with the rows? by default, resize them proportionally
1225 if ( sz
.y
> minsz
.y
&& ( (m_flexDirection
& wxVERTICAL
) || (m_growMode
== wxFLEX_GROWMODE_SPECIFIED
) ) )
1227 int sum_proportions
= 0;
1228 int growable_space
= 0;
1231 for (idx
= 0; idx
< m_growableRows
.GetCount(); idx
++)
1233 // Since the number of rows/columns can change as items are
1234 // inserted/deleted, we need to verify at runtime that the
1235 // requested growable rows/columns are still valid.
1236 if (m_growableRows
[idx
] >= nrows
)
1239 // If all items in a row/column are hidden, that row/column will
1240 // have a dimension of -1. This causes the row/column to be
1241 // hidden completely.
1242 if (m_rowHeights
[ m_growableRows
[idx
] ] == -1)
1244 sum_proportions
+= m_growableRowsProportions
[idx
];
1245 growable_space
+= m_rowHeights
[ m_growableRows
[idx
] ];
1251 for (idx
= 0; idx
< m_growableRows
.GetCount(); idx
++)
1253 if (m_growableRows
[idx
] >= nrows
)
1255 if (m_rowHeights
[ m_growableRows
[idx
] ] == -1)
1256 m_rowHeights
[ m_growableRows
[idx
] ] = 0;
1259 int delta
= (sz
.y
- minsz
.y
);
1260 if (sum_proportions
== 0)
1261 delta
= (delta
/num
) + m_rowHeights
[ m_growableRows
[idx
] ];
1263 delta
= ((delta
+growable_space
)*m_growableRowsProportions
[idx
]) / sum_proportions
;
1264 m_rowHeights
[ m_growableRows
[idx
] ] = delta
;
1269 else if ( (m_growMode
== wxFLEX_GROWMODE_ALL
) && (sz
.y
> minsz
.y
) )
1271 // rounding problem?
1272 for ( int row
= 0; row
< nrows
; ++row
)
1273 m_rowHeights
[ row
] = sz
.y
/ nrows
;
1276 // the same logic as above but for the columns
1277 if ( sz
.x
> minsz
.x
&& ( (m_flexDirection
& wxHORIZONTAL
) || (m_growMode
== wxFLEX_GROWMODE_SPECIFIED
) ) )
1279 int sum_proportions
= 0;
1280 int growable_space
= 0;
1283 for (idx
= 0; idx
< m_growableCols
.GetCount(); idx
++)
1285 // Since the number of rows/columns can change as items are
1286 // inserted/deleted, we need to verify at runtime that the
1287 // requested growable rows/columns are still valid.
1288 if (m_growableCols
[idx
] >= ncols
)
1291 // If all items in a row/column are hidden, that row/column will
1292 // have a dimension of -1. This causes the column to be hidden
1294 if (m_colWidths
[ m_growableCols
[idx
] ] == -1)
1296 sum_proportions
+= m_growableColsProportions
[idx
];
1297 growable_space
+= m_colWidths
[ m_growableCols
[idx
] ];
1303 for (idx
= 0; idx
< m_growableCols
.GetCount(); idx
++)
1305 if (m_growableCols
[idx
] >= ncols
)
1307 if (m_colWidths
[ m_growableCols
[idx
] ] == -1)
1308 m_colWidths
[ m_growableCols
[idx
] ] = 0;
1311 int delta
= (sz
.x
- minsz
.x
);
1312 if (sum_proportions
== 0)
1313 delta
= (delta
/num
) + m_colWidths
[ m_growableCols
[idx
] ];
1315 delta
= ((delta
+growable_space
)*m_growableColsProportions
[idx
])/sum_proportions
;
1316 m_colWidths
[ m_growableCols
[idx
] ] = delta
;
1321 else if ( (m_growMode
== wxFLEX_GROWMODE_ALL
) && (sz
.x
> minsz
.x
) )
1323 for ( int col
=0; col
< ncols
; ++col
)
1324 m_colWidths
[ col
] = sz
.x
/ ncols
;
1329 void wxFlexGridSizer::AddGrowableRow( size_t idx
, int proportion
)
1331 m_growableRows
.Add( idx
);
1332 m_growableRowsProportions
.Add( proportion
);
1335 void wxFlexGridSizer::RemoveGrowableRow( size_t idx
)
1337 m_growableRows
.Remove( idx
);
1340 void wxFlexGridSizer::AddGrowableCol( size_t idx
, int proportion
)
1342 m_growableCols
.Add( idx
);
1343 m_growableColsProportions
.Add( proportion
);
1346 void wxFlexGridSizer::RemoveGrowableCol( size_t idx
)
1348 m_growableCols
.Remove( idx
);
1351 //---------------------------------------------------------------------------
1353 //---------------------------------------------------------------------------
1355 wxBoxSizer::wxBoxSizer( int orient
)
1356 : m_orient( orient
)
1360 void wxBoxSizer::RecalcSizes()
1362 if (m_children
.GetCount() == 0)
1368 if (m_orient
== wxHORIZONTAL
)
1369 delta
= m_size
.x
- m_fixedWidth
;
1371 delta
= m_size
.y
- m_fixedHeight
;
1374 wxPoint
pt( m_position
);
1376 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
1379 wxSizerItem
*item
= node
->GetData();
1381 if (item
->IsShown())
1383 wxSize
size( item
->GetMinSizeWithBorder() );
1385 if (m_orient
== wxVERTICAL
)
1387 wxCoord height
= size
.y
;
1388 if (item
->GetProportion())
1390 // Because of at least one visible item has non-zero
1391 // proportion then m_stretchable is not zero
1392 height
= (delta
* item
->GetProportion()) / m_stretchable
;
1395 wxPoint
child_pos( pt
);
1396 wxSize
child_size( size
.x
, height
);
1398 if (item
->GetFlag() & (wxEXPAND
| wxSHAPED
))
1399 child_size
.x
= m_size
.x
;
1400 else if (item
->GetFlag() & wxALIGN_RIGHT
)
1401 child_pos
.x
+= m_size
.x
- size
.x
;
1402 else if (item
->GetFlag() & (wxCENTER
| wxALIGN_CENTER_HORIZONTAL
))
1403 // XXX wxCENTER is added for backward compatibility;
1404 // wxALIGN_CENTER should be used in new code
1405 child_pos
.x
+= (m_size
.x
- size
.x
) / 2;
1407 item
->SetDimension( child_pos
, child_size
);
1413 wxCoord width
= size
.x
;
1414 if (item
->GetProportion())
1416 // Because of at least one visible item has non-zero
1417 // proportion then m_stretchable is not zero
1418 width
= (delta
* item
->GetProportion()) / m_stretchable
;
1421 wxPoint
child_pos( pt
);
1422 wxSize
child_size( width
, size
.y
);
1424 if (item
->GetFlag() & (wxEXPAND
| wxSHAPED
))
1425 child_size
.y
= m_size
.y
;
1426 else if (item
->GetFlag() & wxALIGN_BOTTOM
)
1427 child_pos
.y
+= m_size
.y
- size
.y
;
1428 else if (item
->GetFlag() & (wxCENTER
| wxALIGN_CENTER_VERTICAL
))
1429 // XXX wxCENTER is added for backward compatibility;
1430 // wxALIGN_CENTER should be used in new code
1431 child_pos
.y
+= (m_size
.y
- size
.y
) / 2;
1433 item
->SetDimension( child_pos
, child_size
);
1439 node
= node
->GetNext();
1443 wxSize
wxBoxSizer::CalcMin()
1445 if (m_children
.GetCount() == 0)
1446 return wxSize(10,10);
1454 // precalc item minsizes and count proportions
1455 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
1458 wxSizerItem
*item
= node
->GetData();
1460 if (item
->IsShown())
1461 item
->CalcMin(); // result is stored in the item
1463 if (item
->IsShown() && item
->GetProportion() != 0)
1464 m_stretchable
+= item
->GetProportion();
1466 node
= node
->GetNext();
1469 // Total minimum size (width or height) of sizer
1472 node
= m_children
.GetFirst();
1475 wxSizerItem
*item
= node
->GetData();
1477 if (item
->IsShown() && item
->GetProportion() != 0)
1479 int stretch
= item
->GetProportion();
1480 wxSize
size( item
->GetMinSizeWithBorder() );
1483 // Integer division rounded up is (a + b - 1) / b
1484 // Round up needed in order to guarantee that all
1485 // all items will have size not less then their min size
1486 if (m_orient
== wxHORIZONTAL
)
1487 minSize
= ( size
.x
*m_stretchable
+ stretch
- 1)/stretch
;
1489 minSize
= ( size
.y
*m_stretchable
+ stretch
- 1)/stretch
;
1491 if (minSize
> maxMinSize
)
1492 maxMinSize
= minSize
;
1494 node
= node
->GetNext();
1497 // Calculate overall minimum size
1498 node
= m_children
.GetFirst();
1501 wxSizerItem
*item
= node
->GetData();
1503 if (item
->IsShown())
1505 wxSize
size( item
->GetMinSizeWithBorder() );
1506 if (item
->GetProportion() != 0)
1508 if (m_orient
== wxHORIZONTAL
)
1509 size
.x
= (maxMinSize
*item
->GetProportion())/m_stretchable
;
1511 size
.y
= (maxMinSize
*item
->GetProportion())/m_stretchable
;
1515 if (m_orient
== wxVERTICAL
)
1517 m_fixedHeight
+= size
.y
;
1518 m_fixedWidth
= wxMax( m_fixedWidth
, size
.x
);
1522 m_fixedWidth
+= size
.x
;
1523 m_fixedHeight
= wxMax( m_fixedHeight
, size
.y
);
1527 if (m_orient
== wxHORIZONTAL
)
1529 m_minWidth
+= size
.x
;
1530 m_minHeight
= wxMax( m_minHeight
, size
.y
);
1534 m_minHeight
+= size
.y
;
1535 m_minWidth
= wxMax( m_minWidth
, size
.x
);
1538 node
= node
->GetNext();
1541 return wxSize( m_minWidth
, m_minHeight
);
1544 //---------------------------------------------------------------------------
1546 //---------------------------------------------------------------------------
1550 wxStaticBoxSizer::wxStaticBoxSizer( wxStaticBox
*box
, int orient
)
1551 : wxBoxSizer( orient
)
1552 , m_staticBox( box
)
1554 wxASSERT_MSG( box
, wxT("wxStaticBoxSizer needs a static box") );
1557 static void GetStaticBoxBorders( wxStaticBox
*box
,
1561 // this has to be done platform by platform as there is no way to
1562 // guess the thickness of a wxStaticBox border
1564 box
->GetBordersForSizer(borderTop
,borderOther
);
1565 #elif defined(__WXMAC__)
1567 static int extraTop
= -1; // Uninitted
1568 static int other
= 5;
1570 if ( extraTop
== -1 )
1572 // The minimal border used for the top. Later on the staticbox'
1573 // font height is added to this.
1576 if ( UMAGetSystemVersion() >= 0x1030 /*Panther*/ )
1578 // As indicated by the HIG, Panther needs an extra border of 11
1579 // pixels (otherwise overlapping occurs at the top). The "other"
1580 // border has to be 11.
1587 *borderTop
= extraTop
+ box
->GetCharHeight();
1588 *borderOther
= other
;
1592 if ( box
->GetLabel().empty() )
1596 *borderTop
= box
->GetCharHeight();
1599 #endif // __WXCOCOA__
1602 void wxStaticBoxSizer::RecalcSizes()
1604 int top_border
, other_border
;
1605 GetStaticBoxBorders(m_staticBox
, &top_border
, &other_border
);
1607 m_staticBox
->SetSize( m_position
.x
, m_position
.y
, m_size
.x
, m_size
.y
);
1609 wxPoint
old_pos( m_position
);
1610 m_position
.x
+= other_border
;
1611 m_position
.y
+= top_border
;
1612 wxSize
old_size( m_size
);
1613 m_size
.x
-= 2*other_border
;
1614 m_size
.y
-= top_border
+ other_border
;
1616 wxBoxSizer::RecalcSizes();
1618 m_position
= old_pos
;
1622 wxSize
wxStaticBoxSizer::CalcMin()
1624 int top_border
, other_border
;
1625 GetStaticBoxBorders(m_staticBox
, &top_border
, &other_border
);
1627 wxSize
ret( wxBoxSizer::CalcMin() );
1628 ret
.x
+= 2*other_border
;
1629 ret
.y
+= other_border
+ top_border
;
1634 void wxStaticBoxSizer::ShowItems( bool show
)
1636 m_staticBox
->Show( show
);
1637 wxBoxSizer::ShowItems( show
);
1640 #endif // wxUSE_STATBOX
1643 #if WXWIN_COMPATIBILITY_2_4
1645 // ----------------------------------------------------------------------------
1647 // ----------------------------------------------------------------------------
1650 IMPLEMENT_CLASS(wxBookCtrlSizer
, wxSizer
)
1652 IMPLEMENT_CLASS(wxNotebookSizer
, wxBookCtrlSizer
)
1653 #endif // wxUSE_NOTEBOOK
1654 #endif // wxUSE_BOOKCTRL
1658 wxBookCtrlSizer::wxBookCtrlSizer(wxBookCtrlBase
*bookctrl
)
1659 : m_bookctrl(bookctrl
)
1661 wxASSERT_MSG( bookctrl
, wxT("wxBookCtrlSizer needs a control") );
1664 void wxBookCtrlSizer::RecalcSizes()
1666 m_bookctrl
->SetSize( m_position
.x
, m_position
.y
, m_size
.x
, m_size
.y
);
1669 wxSize
wxBookCtrlSizer::CalcMin()
1671 wxSize sizeBorder
= m_bookctrl
->CalcSizeFromPage(wxSize(0, 0));
1676 if ( m_bookctrl
->GetPageCount() == 0 )
1678 return wxSize(sizeBorder
.x
+ 10, sizeBorder
.y
+ 10);
1684 wxWindowList::compatibility_iterator
1685 node
= m_bookctrl
->GetChildren().GetFirst();
1688 wxWindow
*item
= node
->GetData();
1689 wxSizer
*itemsizer
= item
->GetSizer();
1693 wxSize
subsize( itemsizer
->CalcMin() );
1695 if (subsize
.x
> maxX
)
1697 if (subsize
.y
> maxY
)
1701 node
= node
->GetNext();
1704 return wxSize( maxX
, maxY
) + sizeBorder
;
1709 wxNotebookSizer::wxNotebookSizer(wxNotebook
*nb
)
1711 wxASSERT_MSG( nb
, wxT("wxNotebookSizer needs a control") );
1715 #endif // wxUSE_NOTEBOOOK
1716 #endif // wxUSE_BOOKCTRL
1718 #endif // WXWIN_COMPATIBILITY_2_4