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/notebook.h"
28 #include <wx/listimpl.cpp>
30 //---------------------------------------------------------------------------
32 IMPLEMENT_CLASS(wxSizerItem
, wxObject
)
33 IMPLEMENT_CLASS(wxSizer
, wxObject
)
34 IMPLEMENT_CLASS(wxGridSizer
, wxSizer
)
35 IMPLEMENT_CLASS(wxFlexGridSizer
, wxGridSizer
)
36 IMPLEMENT_CLASS(wxBoxSizer
, wxSizer
)
38 IMPLEMENT_CLASS(wxStaticBoxSizer
, wxBoxSizer
)
41 IMPLEMENT_CLASS(wxBookCtrlSizer
, wxSizer
)
43 IMPLEMENT_CLASS(wxNotebookSizer
, wxBookCtrlSizer
)
44 #endif // wxUSE_NOTEBOOK
45 #endif // wxUSE_BOOKCTRL
47 WX_DEFINE_EXPORTED_LIST( wxSizerItemList
);
81 //---------------------------------------------------------------------------
83 //---------------------------------------------------------------------------
85 wxSizerItem::wxSizerItem( int width
, int height
, int proportion
, int flag
, int border
, wxObject
* userData
)
88 , m_size( wxSize( width
, height
) ) // size is set directly
89 , m_minSize( m_size
) // minimal size is the initial size
90 , m_proportion( proportion
)
94 , m_userData( userData
)
99 wxSizerItem::wxSizerItem( wxWindow
*window
, int proportion
, int flag
, int border
, wxObject
* userData
)
102 , m_minSize( window
->GetSize() ) // minimal size is the initial size
103 , m_proportion( proportion
)
107 , m_userData( userData
)
109 // aspect ratio calculated from initial size
110 SetRatio( m_minSize
);
112 // m_size is calculated later
115 wxSizerItem::wxSizerItem( wxSizer
*sizer
, int proportion
, int flag
, int border
, wxObject
* userData
)
118 , m_proportion( proportion
)
123 , m_userData( userData
)
125 // m_minSize is calculated later
126 // m_size is calculated later
129 wxSizerItem::~wxSizerItem()
135 m_window
->SetContainingSizer(NULL
);
137 else // we must be a sizer
144 wxSize
wxSizerItem::GetSize() const
148 ret
= m_sizer
->GetSize();
151 ret
= m_window
->GetSize();
158 if (m_flag
& wxNORTH
)
160 if (m_flag
& wxSOUTH
)
166 wxSize
wxSizerItem::CalcMin()
171 ret
= m_sizer
->GetMinSize();
173 // if we have to preserve aspect ratio _AND_ this is
174 // the first-time calculation, consider ret to be initial size
175 if ((m_flag
& wxSHAPED
) && !m_ratio
)
180 if ( IsWindow() && (m_flag
& wxADJUST_MINSIZE
) )
182 // By user request, keep the minimal size for this item
183 // in sync with the largest of BestSize and any user supplied
184 // minimum size hint. Useful in cases where the item is
185 // changeable -- static text labels, etc.
186 m_minSize
= m_window
->GetAdjustedBestSize();
196 if (m_flag
& wxNORTH
)
198 if (m_flag
& wxSOUTH
)
204 void wxSizerItem::SetDimension( wxPoint pos
, wxSize size
)
206 if (m_flag
& wxSHAPED
)
208 // adjust aspect ratio
209 int rwidth
= (int) (size
.y
* m_ratio
);
213 int rheight
= (int) (size
.x
/ m_ratio
);
214 // add vertical space
215 if (m_flag
& wxALIGN_CENTER_VERTICAL
)
216 pos
.y
+= (size
.y
- rheight
) / 2;
217 else if (m_flag
& wxALIGN_BOTTOM
)
218 pos
.y
+= (size
.y
- rheight
);
219 // use reduced dimensions
222 else if (rwidth
< size
.x
)
224 // add horizontal space
225 if (m_flag
& wxALIGN_CENTER_HORIZONTAL
)
226 pos
.x
+= (size
.x
- rwidth
) / 2;
227 else if (m_flag
& wxALIGN_RIGHT
)
228 pos
.x
+= (size
.x
- rwidth
);
233 // This is what GetPosition() returns. Since we calculate
234 // borders afterwards, GetPosition() will be the left/top
235 // corner of the surrounding border.
247 if (m_flag
& wxNORTH
)
252 if (m_flag
& wxSOUTH
)
258 m_sizer
->SetDimension( pos
.x
, pos
.y
, size
.x
, size
.y
);
261 m_window
->SetSize( pos
.x
, pos
.y
, size
.x
, size
.y
, wxSIZE_ALLOW_MINUS_ONE
);
266 void wxSizerItem::DeleteWindows()
272 m_sizer
->DeleteWindows();
275 bool wxSizerItem::IsWindow() const
277 return (m_window
!= NULL
);
280 bool wxSizerItem::IsSizer() const
282 return (m_sizer
!= NULL
);
285 bool wxSizerItem::IsSpacer() const
287 return (m_window
== NULL
) && (m_sizer
== NULL
);
290 void wxSizerItem::Show( bool show
)
295 m_window
->Show( show
);
297 m_sizer
->ShowItems( show
);
299 // ... nothing else to do to hide/show spacers
302 void wxSizerItem::SetOption( int option
)
304 SetProportion( option
);
307 int wxSizerItem::GetOption() const
309 return GetProportion();
313 //---------------------------------------------------------------------------
315 //---------------------------------------------------------------------------
318 : m_minSize( wxSize( 0, 0 ) )
324 WX_CLEAR_LIST(wxSizerItemList
, m_children
);
327 void wxSizer::Add( wxWindow
*window
, int proportion
, int flag
, int border
, wxObject
* userData
)
329 m_children
.Append( new wxSizerItem( window
, proportion
, flag
, border
, userData
) );
330 window
->SetContainingSizer( this );
333 void wxSizer::Add( wxSizer
*sizer
, int proportion
, int flag
, int border
, wxObject
* userData
)
335 m_children
.Append( new wxSizerItem( sizer
, proportion
, flag
, border
, userData
) );
338 void wxSizer::Add( int width
, int height
, int proportion
, int flag
, int border
, wxObject
* userData
)
340 m_children
.Append( new wxSizerItem( width
, height
, proportion
, flag
, border
, userData
) );
343 void wxSizer::Add( wxSizerItem
*item
)
345 m_children
.Append( item
);
347 if( item
->GetWindow() )
348 item
->GetWindow()->SetContainingSizer( this );
351 void wxSizer::Prepend( wxWindow
*window
, int proportion
, int flag
, int border
, wxObject
* userData
)
353 m_children
.Insert( new wxSizerItem( window
, proportion
, flag
, border
, userData
) );
354 window
->SetContainingSizer( this );
357 void wxSizer::Prepend( wxSizer
*sizer
, int proportion
, int flag
, int border
, wxObject
* userData
)
359 m_children
.Insert( new wxSizerItem( sizer
, proportion
, flag
, border
, userData
) );
362 void wxSizer::Prepend( int width
, int height
, int proportion
, int flag
, int border
, wxObject
* userData
)
364 m_children
.Insert( new wxSizerItem( width
, height
, proportion
, flag
, border
, userData
) );
367 void wxSizer::Prepend( wxSizerItem
*item
)
369 m_children
.Insert( item
);
371 if( item
->GetWindow() )
372 item
->GetWindow()->SetContainingSizer( this );
375 void wxSizer::Insert( size_t index
,
382 m_children
.Insert( index
,
383 new wxSizerItem( window
, proportion
, flag
, border
, userData
) );
384 window
->SetContainingSizer( this );
387 void wxSizer::Insert( size_t index
,
394 m_children
.Insert( index
,
395 new wxSizerItem( sizer
, proportion
, flag
, border
, userData
) );
398 void wxSizer::Insert( size_t index
,
406 m_children
.Insert( index
,
407 new wxSizerItem( width
, height
, proportion
, flag
, border
, userData
) );
410 void wxSizer::Insert( size_t index
, wxSizerItem
*item
)
412 m_children
.Insert( index
, item
);
414 if( item
->GetWindow() )
415 item
->GetWindow()->SetContainingSizer( this );
418 bool wxSizer::Remove( wxWindow
*window
)
420 return Detach( window
);
423 bool wxSizer::Remove( wxSizer
*sizer
)
425 wxASSERT_MSG( sizer
, _T("Removing NULL sizer") );
427 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
430 wxSizerItem
*item
= node
->GetData();
432 if (item
->GetSizer() == sizer
)
435 m_children
.Erase( node
);
439 node
= node
->GetNext();
445 bool wxSizer::Remove( int index
)
447 wxCHECK_MSG( index
>= 0 && (size_t)index
< m_children
.GetCount(),
449 _T("Remove index is out of range") );
451 wxSizerItemList::compatibility_iterator node
= m_children
.Item( index
);
453 wxCHECK_MSG( node
, false, _T("Failed to find child node") );
455 wxSizerItem
*item
= node
->GetData();
457 if( item
->IsWindow() )
458 item
->GetWindow()->SetContainingSizer( NULL
);
461 m_children
.Erase( node
);
465 bool wxSizer::Detach( wxSizer
*sizer
)
467 wxASSERT_MSG( sizer
, _T("Detaching NULL sizer") );
469 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
472 wxSizerItem
*item
= node
->GetData();
474 if (item
->GetSizer() == sizer
)
478 m_children
.Erase( node
);
481 node
= node
->GetNext();
487 bool wxSizer::Detach( wxWindow
*window
)
489 wxASSERT_MSG( window
, _T("Detaching NULL window") );
491 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
494 wxSizerItem
*item
= node
->GetData();
496 if (item
->GetWindow() == window
)
498 item
->GetWindow()->SetContainingSizer( NULL
);
500 m_children
.Erase( node
);
503 node
= node
->GetNext();
509 bool wxSizer::Detach( int index
)
511 wxCHECK_MSG( index
>= 0 && (size_t)index
< m_children
.GetCount(),
513 _T("Detach index is out of range") );
515 wxSizerItemList::compatibility_iterator node
= m_children
.Item( index
);
517 wxCHECK_MSG( node
, false, _T("Failed to find child node") );
519 wxSizerItem
*item
= node
->GetData();
521 if( item
->IsSizer() )
523 else if( item
->IsWindow() )
524 item
->GetWindow()->SetContainingSizer( NULL
);
527 m_children
.Erase( node
);
531 void wxSizer::Clear( bool delete_windows
)
533 // First clear the ContainingSizer pointers
534 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
537 wxSizerItem
*item
= node
->GetData();
539 if (item
->IsWindow())
540 item
->GetWindow()->SetContainingSizer( NULL
);
541 node
= node
->GetNext();
544 // Destroy the windows if needed
548 // Now empty the list
549 WX_CLEAR_LIST(wxSizerItemList
, m_children
);
552 void wxSizer::DeleteWindows()
554 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
557 wxSizerItem
*item
= node
->GetData();
559 item
->DeleteWindows();
560 node
= node
->GetNext();
564 wxSize
wxSizer::Fit( wxWindow
*window
)
567 if (window
->IsTopLevel())
568 size
= FitSize( window
);
570 size
= GetMinWindowSize( window
);
572 window
->SetSize( size
);
577 void wxSizer::FitInside( wxWindow
*window
)
580 if (window
->IsTopLevel())
581 size
= VirtualFitSize( window
);
583 size
= GetMinClientSize( window
);
585 window
->SetVirtualSize( size
);
588 void wxSizer::Layout()
594 void wxSizer::SetSizeHints( wxWindow
*window
)
596 // Preserve the window's max size hints, but set the
597 // lower bound according to the sizer calculations.
599 wxSize size
= Fit( window
);
601 window
->SetSizeHints( size
.x
,
603 window
->GetMaxWidth(),
604 window
->GetMaxHeight() );
607 void wxSizer::SetVirtualSizeHints( wxWindow
*window
)
609 // Preserve the window's max size hints, but set the
610 // lower bound according to the sizer calculations.
613 wxSize
size( window
->GetVirtualSize() );
614 window
->SetVirtualSizeHints( size
.x
,
616 window
->GetMaxWidth(),
617 window
->GetMaxHeight() );
620 wxSize
wxSizer::GetMaxWindowSize( wxWindow
*window
) const
622 return window
->GetMaxSize();
625 wxSize
wxSizer::GetMinWindowSize( wxWindow
*window
)
627 wxSize
minSize( GetMinSize() );
628 wxSize
size( window
->GetSize() );
629 wxSize
client_size( window
->GetClientSize() );
631 return wxSize( minSize
.x
+size
.x
-client_size
.x
,
632 minSize
.y
+size
.y
-client_size
.y
);
635 // Return a window size that will fit within the screens dimensions
636 wxSize
wxSizer::FitSize( wxWindow
*window
)
638 wxSize size
= GetMinWindowSize( window
);
639 wxSize sizeMax
= GetMaxWindowSize( window
);
641 // Limit the size if sizeMax != wxDefaultSize
643 if ( size
.x
> sizeMax
.x
&& sizeMax
.x
!= -1 )
645 if ( size
.y
> sizeMax
.y
&& sizeMax
.y
!= -1 )
651 wxSize
wxSizer::GetMaxClientSize( wxWindow
*window
) const
653 wxSize
maxSize( window
->GetMaxSize() );
655 if( maxSize
!= wxDefaultSize
)
657 wxSize
size( window
->GetSize() );
658 wxSize
client_size( window
->GetClientSize() );
660 return wxSize( maxSize
.x
+ client_size
.x
- size
.x
,
661 maxSize
.y
+ client_size
.y
- size
.y
);
664 return wxDefaultSize
;
667 wxSize
wxSizer::GetMinClientSize( wxWindow
*WXUNUSED(window
) )
669 return GetMinSize(); // Already returns client size.
672 wxSize
wxSizer::VirtualFitSize( wxWindow
*window
)
674 wxSize size
= GetMinClientSize( window
);
675 wxSize sizeMax
= GetMaxClientSize( window
);
677 // Limit the size if sizeMax != wxDefaultSize
679 if ( size
.x
> sizeMax
.x
&& sizeMax
.x
!= -1 )
681 if ( size
.y
> sizeMax
.y
&& sizeMax
.y
!= -1 )
687 void wxSizer::SetDimension( int x
, int y
, int width
, int height
)
696 wxSize
wxSizer::GetMinSize()
698 wxSize
ret( CalcMin() );
699 if (ret
.x
< m_minSize
.x
) ret
.x
= m_minSize
.x
;
700 if (ret
.y
< m_minSize
.y
) ret
.y
= m_minSize
.y
;
704 void wxSizer::DoSetMinSize( int width
, int height
)
707 m_minSize
.y
= height
;
710 bool wxSizer::DoSetItemMinSize( wxWindow
*window
, int width
, int height
)
712 wxASSERT_MSG( window
, _T("SetMinSize for NULL window") );
714 // Is it our immediate child?
716 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
719 wxSizerItem
*item
= node
->GetData();
721 if (item
->GetWindow() == window
)
723 item
->SetInitSize( 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( window
, width
, height
) )
739 // A child sizer found the requested windw, exit.
742 node
= node
->GetNext();
748 bool wxSizer::DoSetItemMinSize( wxSizer
*sizer
, int width
, int height
)
750 wxASSERT_MSG( sizer
, _T("SetMinSize for NULL sizer") );
752 // Is it our immediate child?
754 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
757 wxSizerItem
*item
= node
->GetData();
759 if (item
->GetSizer() == sizer
)
761 item
->GetSizer()->DoSetMinSize( width
, height
);
764 node
= node
->GetNext();
767 // No? Search any subsizers we own then
769 node
= m_children
.GetFirst();
772 wxSizerItem
*item
= node
->GetData();
774 if ( item
->GetSizer() &&
775 item
->GetSizer()->DoSetItemMinSize( sizer
, width
, height
) )
777 // A child found the requested sizer, exit.
780 node
= node
->GetNext();
786 bool wxSizer::DoSetItemMinSize( size_t index
, int width
, int height
)
788 wxSizerItemList::compatibility_iterator node
= m_children
.Item( index
);
790 wxCHECK_MSG( node
, false, _T("Failed to find child node") );
792 wxSizerItem
*item
= node
->GetData();
794 if (item
->GetSizer())
796 // Sizers contains the minimal size in them, if not calculated ...
797 item
->GetSizer()->DoSetMinSize( width
, height
);
801 // ... but the minimal size of spacers and windows in stored in them
802 item
->SetInitSize( width
, height
);
808 void wxSizer::Show( wxWindow
*window
, bool show
)
810 wxASSERT_MSG( window
, _T("Show for NULL window") );
812 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
815 wxSizerItem
*item
= node
->GetData();
817 if (item
->GetWindow() == window
)
822 node
= node
->GetNext();
826 void wxSizer::Show( wxSizer
*sizer
, bool show
)
828 wxASSERT_MSG( sizer
, _T("Show for NULL sizer") );
830 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
833 wxSizerItem
*item
= node
->GetData();
835 if (item
->GetSizer() == sizer
)
840 node
= node
->GetNext();
844 void wxSizer::Show( size_t index
, bool show
)
846 wxCHECK_RET( index
< m_children
.GetCount(),
847 _T("Show index is out of range") );
849 m_children
.Item( index
)->GetData()->Show( show
);
852 void wxSizer::ShowItems( bool show
)
854 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
857 node
->GetData()->Show( show
);
858 node
= node
->GetNext();
862 bool wxSizer::IsShown( wxWindow
*window
) const
864 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
867 wxSizerItem
*item
= node
->GetData();
869 if (item
->GetWindow() == window
)
871 return item
->IsShown();
873 node
= node
->GetNext();
876 wxFAIL_MSG( _T("IsShown failed to find sizer item") );
881 bool wxSizer::IsShown( wxSizer
*sizer
) const
883 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
886 wxSizerItem
*item
= node
->GetData();
888 if (item
->GetSizer() == sizer
)
890 return item
->IsShown();
892 node
= node
->GetNext();
895 wxFAIL_MSG( _T("IsShown failed to find sizer item") );
900 bool wxSizer::IsShown( size_t index
) const
902 wxCHECK_MSG( index
< m_children
.GetCount(),
904 _T("IsShown index is out of range") );
906 return m_children
.Item( index
)->GetData()->IsShown();
910 //---------------------------------------------------------------------------
912 //---------------------------------------------------------------------------
914 wxGridSizer::wxGridSizer( int rows
, int cols
, int vgap
, int hgap
)
922 wxGridSizer::wxGridSizer( int cols
, int vgap
, int hgap
)
930 int wxGridSizer::CalcRowsCols(int& nrows
, int& ncols
) const
932 int nitems
= m_children
.GetCount();
938 nrows
= (nitems
+ m_cols
- 1) / m_cols
;
942 ncols
= (nitems
+ m_rows
- 1) / m_rows
;
945 else // 0 columns, 0 rows?
947 wxFAIL_MSG( _T("grid sizer must have either rows or columns fixed") );
956 void wxGridSizer::RecalcSizes()
958 int nitems
, nrows
, ncols
;
959 if ( (nitems
= CalcRowsCols(nrows
, ncols
)) == 0 )
962 wxSize
sz( GetSize() );
963 wxPoint
pt( GetPosition() );
965 int w
= (sz
.x
- (ncols
- 1) * m_hgap
) / ncols
;
966 int h
= (sz
.y
- (nrows
- 1) * m_vgap
) / nrows
;
969 for (int c
= 0; c
< ncols
; c
++)
972 for (int r
= 0; r
< nrows
; r
++)
974 int i
= r
* ncols
+ c
;
977 wxSizerItemList::compatibility_iterator node
= m_children
.Item( i
);
979 wxASSERT_MSG( node
, _T("Failed to find SizerItemList node") );
981 SetItemBounds( node
->GetData(), x
, y
, w
, h
);
989 wxSize
wxGridSizer::CalcMin()
992 if ( CalcRowsCols(nrows
, ncols
) == 0 )
993 return wxSize(10, 10);
995 // Find the max width and height for any component
999 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
1002 wxSizerItem
*item
= node
->GetData();
1003 wxSize
sz( item
->CalcMin() );
1005 w
= wxMax( w
, sz
.x
);
1006 h
= wxMax( h
, sz
.y
);
1008 node
= node
->GetNext();
1011 return wxSize( ncols
* w
+ (ncols
-1) * m_hgap
,
1012 nrows
* h
+ (nrows
-1) * m_vgap
);
1015 void wxGridSizer::SetItemBounds( wxSizerItem
*item
, int x
, int y
, int w
, int h
)
1018 wxSize
sz( item
->CalcMin() );
1019 int flag
= item
->GetFlag();
1021 if ((flag
& wxEXPAND
) || (flag
& wxSHAPED
))
1027 if (flag
& wxALIGN_CENTER_HORIZONTAL
)
1029 pt
.x
= x
+ (w
- sz
.x
) / 2;
1031 else if (flag
& wxALIGN_RIGHT
)
1033 pt
.x
= x
+ (w
- sz
.x
);
1036 if (flag
& wxALIGN_CENTER_VERTICAL
)
1038 pt
.y
= y
+ (h
- sz
.y
) / 2;
1040 else if (flag
& wxALIGN_BOTTOM
)
1042 pt
.y
= y
+ (h
- sz
.y
);
1046 item
->SetDimension(pt
, sz
);
1049 //---------------------------------------------------------------------------
1051 //---------------------------------------------------------------------------
1053 wxFlexGridSizer::wxFlexGridSizer( int rows
, int cols
, int vgap
, int hgap
)
1054 : wxGridSizer( rows
, cols
, vgap
, hgap
),
1055 m_flexDirection(wxBOTH
),
1056 m_growMode(wxFLEX_GROWMODE_SPECIFIED
)
1060 wxFlexGridSizer::wxFlexGridSizer( int cols
, int vgap
, int hgap
)
1061 : wxGridSizer( cols
, vgap
, hgap
),
1062 m_flexDirection(wxBOTH
),
1063 m_growMode(wxFLEX_GROWMODE_SPECIFIED
)
1067 wxFlexGridSizer::~wxFlexGridSizer()
1071 void wxFlexGridSizer::RecalcSizes()
1073 int nitems
, nrows
, ncols
;
1074 if ( (nitems
= CalcRowsCols(nrows
, ncols
)) == 0 )
1077 wxSize
sz( GetSize() );
1078 wxSize
minsz( CalcMin() );
1079 wxPoint
pt( GetPosition() );
1081 // what to do with the rows? by default, resize them proportionally
1082 if ( sz
.y
> minsz
.y
&& ( (m_flexDirection
& wxVERTICAL
) || (m_growMode
== wxFLEX_GROWMODE_SPECIFIED
) ) )
1084 int sum_proportions
= 0;
1085 int growable_space
= 0;
1088 for (idx
= 0; idx
< m_growableRows
.GetCount(); idx
++)
1090 // Since the number of rows/columns can change as items are inserted/deleted, we need
1091 // to verify at runtime that the requested growable rows/columns are still valid.
1092 if (m_growableRows
[idx
] >= nrows
)
1094 // If all items in a row/column are hidden, that row/column will have a dimension of -1.
1095 // This causes the row/column to be hidden completely.
1096 if (m_rowHeights
[ m_growableRows
[idx
] ] == -1)
1098 sum_proportions
+= m_growableRowsProportions
[idx
];
1099 growable_space
+= m_rowHeights
[ m_growableRows
[idx
] ];
1105 for (idx
= 0; idx
< m_growableRows
.GetCount(); idx
++)
1107 if (m_growableRows
[idx
] >= nrows
)
1109 if (m_rowHeights
[ m_growableRows
[idx
] ] == -1)
1110 m_rowHeights
[ m_growableRows
[idx
] ] = 0;
1113 int delta
= (sz
.y
- minsz
.y
);
1114 if (sum_proportions
== 0)
1115 delta
= (delta
/num
) + m_rowHeights
[ m_growableRows
[idx
] ];
1117 delta
= ((delta
+growable_space
)*m_growableRowsProportions
[idx
]) / sum_proportions
;
1118 m_rowHeights
[ m_growableRows
[idx
] ] = delta
;
1123 else if ( (m_growMode
== wxFLEX_GROWMODE_ALL
) && (sz
.y
> minsz
.y
) )
1125 // rounding problem?
1126 for ( int row
= 0; row
< nrows
; ++row
)
1127 m_rowHeights
[ row
] = sz
.y
/ nrows
;
1130 // the same logic as above but for the columns
1131 if ( sz
.x
> minsz
.x
&& ( (m_flexDirection
& wxHORIZONTAL
) || (m_growMode
== wxFLEX_GROWMODE_SPECIFIED
) ) )
1133 int sum_proportions
= 0;
1134 int growable_space
= 0;
1137 for (idx
= 0; idx
< m_growableCols
.GetCount(); idx
++)
1139 // Since the number of rows/columns can change as items are inserted/deleted, we need
1140 // to verify at runtime that the requested growable rows/columns are still valid.
1141 if (m_growableCols
[idx
] >= ncols
)
1143 // If all items in a row/column are hidden, that row/column will have a dimension of -1.
1144 // This causes the column to be hidden completely.
1145 if (m_colWidths
[ m_growableCols
[idx
] ] == -1)
1147 sum_proportions
+= m_growableColsProportions
[idx
];
1148 // wtb 5/12/02 bugfix - was m_ColWidths[idx]!!
1149 growable_space
+= m_colWidths
[ m_growableCols
[idx
] ];
1155 for (idx
= 0; idx
< m_growableCols
.GetCount(); idx
++)
1157 if (m_growableCols
[idx
] >= ncols
)
1159 if (m_colWidths
[ m_growableCols
[idx
] ] == -1)
1160 m_colWidths
[ m_growableCols
[idx
] ] = 0;
1163 int delta
= (sz
.x
- minsz
.x
);
1164 if (sum_proportions
== 0)
1165 delta
= (delta
/num
) + m_colWidths
[ m_growableCols
[idx
] ];
1167 delta
= ((delta
+growable_space
)*m_growableColsProportions
[idx
])/sum_proportions
;
1168 m_colWidths
[ m_growableCols
[idx
] ] = delta
;
1173 else if ( (m_growMode
== wxFLEX_GROWMODE_ALL
) && (sz
.x
> minsz
.x
) )
1175 for ( int col
=0; col
< ncols
; ++col
)
1176 m_colWidths
[ col
] = sz
.x
/ ncols
;
1179 sz
= wxSize( pt
.x
+ sz
.x
, pt
.y
+ sz
.y
);
1182 for (int c
= 0; c
< ncols
; c
++)
1185 for (int r
= 0; r
< nrows
; r
++)
1187 int i
= r
* ncols
+ c
;
1190 wxSizerItemList::compatibility_iterator node
= m_children
.Item( i
);
1192 wxASSERT_MSG( node
, _T("Failed to find node") );
1194 int w
= wxMax( 0, wxMin( m_colWidths
[c
], sz
.x
- x
) );
1195 int h
= wxMax( 0, wxMin( m_rowHeights
[r
], sz
.y
- y
) );
1197 SetItemBounds( node
->GetData(), x
, y
, w
, h
);
1199 y
= y
+ m_rowHeights
[r
] + m_vgap
;
1201 x
= x
+ m_colWidths
[c
] + m_hgap
;
1205 wxSize
wxFlexGridSizer::CalcMin()
1211 // Number of rows/columns can change as items are added or removed.
1212 if ( !CalcRowsCols(nrows
, ncols
) )
1213 return wxSize(10, 10);
1215 m_rowHeights
.SetCount(nrows
);
1216 m_colWidths
.SetCount(ncols
);
1218 // We have to recalcuate the sizes in case an item has wxADJUST_MINSIZE, has changed
1219 // minimum size since the previous layout, or has been hidden using wxSizer::Show().
1220 // If all the items in a row/column are hidden, the final dimension of the row/column
1221 // will be -1, indicating that the column itself is hidden.
1222 for( s
= m_rowHeights
.GetCount(), i
= 0; i
< s
; ++i
)
1223 m_rowHeights
[ i
] = -1;
1224 for( s
= m_colWidths
.GetCount(), i
= 0; i
< s
; ++i
)
1225 m_colWidths
[ i
] = -1;
1227 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
1232 wxSizerItem
*item
= node
->GetData();
1233 if ( item
->IsShown() )
1235 wxSize
sz( item
->CalcMin() );
1236 int row
= i
/ ncols
;
1237 int col
= i
% ncols
;
1239 m_rowHeights
[ row
] = wxMax( wxMax( 0, sz
.y
), m_rowHeights
[ row
] );
1240 m_colWidths
[ col
] = wxMax( wxMax( 0, sz
.x
), m_colWidths
[ col
] );
1243 node
= node
->GetNext();
1247 // the logic above works when we resize flexibly in both directions but
1248 // maybe this is not the case
1249 if ( m_flexDirection
!= wxBOTH
)
1251 // select the array corresponding to the direction in which we do *not*
1253 wxArrayInt
& array
= m_flexDirection
== wxVERTICAL
? m_colWidths
1256 const int count
= array
.GetCount();
1258 // find the largest value in this array
1260 for ( n
= 0; n
< count
; ++n
)
1262 if ( array
[n
] > largest
)
1266 // and now fill it with the largest value
1267 for ( n
= 0; n
< count
; ++n
)
1273 // Sum total minimum size, including gaps between rows/columns.
1274 // -1 is used as a magic number meaning empty column.
1276 for (int col
= 0; col
< ncols
; col
++)
1277 if ( m_colWidths
[ col
] != -1 )
1278 width
+= m_colWidths
[ col
] + ( col
== ncols
-1 ? 0 : m_hgap
);
1281 for (int row
= 0; row
< nrows
; row
++)
1282 if ( m_rowHeights
[ row
] != -1 )
1283 height
+= m_rowHeights
[ row
] + ( row
== nrows
-1 ? 0 : m_vgap
);
1285 return wxSize( width
, height
);
1288 void wxFlexGridSizer::AddGrowableRow( size_t idx
, int proportion
)
1290 m_growableRows
.Add( idx
);
1291 m_growableRowsProportions
.Add( proportion
);
1294 void wxFlexGridSizer::RemoveGrowableRow( size_t WXUNUSED(idx
) )
1298 void wxFlexGridSizer::AddGrowableCol( size_t idx
, int proportion
)
1300 m_growableCols
.Add( idx
);
1301 m_growableColsProportions
.Add( proportion
);
1304 void wxFlexGridSizer::RemoveGrowableCol( size_t WXUNUSED(idx
) )
1308 //---------------------------------------------------------------------------
1310 //---------------------------------------------------------------------------
1312 wxBoxSizer::wxBoxSizer( int orient
)
1313 : m_orient( orient
)
1317 void wxBoxSizer::RecalcSizes()
1319 if (m_children
.GetCount() == 0)
1326 if (m_orient
== wxHORIZONTAL
)
1328 delta
= (m_size
.x
- m_fixedWidth
) / m_stretchable
;
1329 extra
= (m_size
.x
- m_fixedWidth
) % m_stretchable
;
1333 delta
= (m_size
.y
- m_fixedHeight
) / m_stretchable
;
1334 extra
= (m_size
.y
- m_fixedHeight
) % m_stretchable
;
1338 wxPoint
pt( m_position
);
1340 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
1343 wxSizerItem
*item
= node
->GetData();
1345 if (item
->IsShown())
1348 if (item
->GetProportion())
1349 weight
= item
->GetProportion();
1351 wxSize
size( item
->CalcMin() );
1353 if (m_orient
== wxVERTICAL
)
1355 wxCoord height
= size
.y
;
1356 if (item
->GetProportion())
1358 height
= (delta
* weight
) + extra
;
1359 extra
= 0; // only the first item will get the remainder as extra size
1362 wxPoint
child_pos( pt
);
1363 wxSize
child_size( wxSize( size
.x
, height
) );
1365 if (item
->GetFlag() & (wxEXPAND
| wxSHAPED
))
1366 child_size
.x
= m_size
.x
;
1367 else if (item
->GetFlag() & wxALIGN_RIGHT
)
1368 child_pos
.x
+= m_size
.x
- size
.x
;
1369 else if (item
->GetFlag() & (wxCENTER
| wxALIGN_CENTER_HORIZONTAL
))
1370 // XXX wxCENTER is added for backward compatibility;
1371 // wxALIGN_CENTER should be used in new code
1372 child_pos
.x
+= (m_size
.x
- size
.x
) / 2;
1374 item
->SetDimension( child_pos
, child_size
);
1380 wxCoord width
= size
.x
;
1381 if (item
->GetProportion())
1383 width
= (delta
* weight
) + extra
;
1384 extra
= 0; // only the first item will get the remainder as extra size
1387 wxPoint
child_pos( pt
);
1388 wxSize
child_size( wxSize(width
, size
.y
) );
1390 if (item
->GetFlag() & (wxEXPAND
| wxSHAPED
))
1391 child_size
.y
= m_size
.y
;
1392 else if (item
->GetFlag() & wxALIGN_BOTTOM
)
1393 child_pos
.y
+= m_size
.y
- size
.y
;
1394 else if (item
->GetFlag() & (wxCENTER
| wxALIGN_CENTER_VERTICAL
))
1395 // XXX wxCENTER is added for backward compatibility;
1396 // wxALIGN_CENTER should be used in new code
1397 child_pos
.y
+= (m_size
.y
- size
.y
) / 2;
1399 item
->SetDimension( child_pos
, child_size
);
1405 node
= node
->GetNext();
1409 wxSize
wxBoxSizer::CalcMin()
1411 if (m_children
.GetCount() == 0)
1412 return wxSize(10,10);
1420 // Find how long each stretch unit needs to be
1421 int stretchSize
= 1;
1422 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
1426 wxSizerItem
*item
= node
->GetData();
1428 if (item
->IsShown() && item
->GetProportion() != 0)
1430 int stretch
= item
->GetProportion();
1431 wxSize
size( item
->CalcMin() );
1433 // Integer division rounded up is (a + b - 1) / b
1434 if (m_orient
== wxHORIZONTAL
)
1435 sizePerStretch
= ( size
.x
+ stretch
- 1 ) / stretch
;
1437 sizePerStretch
= ( size
.y
+ stretch
- 1 ) / stretch
;
1438 if (sizePerStretch
> stretchSize
)
1439 stretchSize
= sizePerStretch
;
1441 node
= node
->GetNext();
1444 // Calculate overall minimum size
1445 node
= m_children
.GetFirst();
1448 wxSizerItem
*item
= node
->GetData();
1450 if (item
->IsShown())
1452 m_stretchable
+= item
->GetProportion();
1454 wxSize
size( item
->CalcMin() );
1455 if (item
->GetProportion() != 0)
1457 if (m_orient
== wxHORIZONTAL
)
1458 size
.x
= stretchSize
* item
->GetProportion();
1460 size
.y
= stretchSize
* item
->GetProportion();
1463 if (m_orient
== wxHORIZONTAL
)
1465 m_minWidth
+= size
.x
;
1466 m_minHeight
= wxMax( m_minHeight
, size
.y
);
1470 m_minHeight
+= size
.y
;
1471 m_minWidth
= wxMax( m_minWidth
, size
.x
);
1474 if (item
->GetProportion() == 0)
1476 if (m_orient
== wxVERTICAL
)
1478 m_fixedHeight
+= size
.y
;
1479 m_fixedWidth
= wxMax( m_fixedWidth
, size
.x
);
1483 m_fixedWidth
+= size
.x
;
1484 m_fixedHeight
= wxMax( m_fixedHeight
, size
.y
);
1488 node
= node
->GetNext();
1491 return wxSize( m_minWidth
, m_minHeight
);
1494 //---------------------------------------------------------------------------
1496 //---------------------------------------------------------------------------
1500 wxStaticBoxSizer::wxStaticBoxSizer( wxStaticBox
*box
, int orient
)
1501 : wxBoxSizer( orient
)
1502 , m_staticBox( box
)
1504 wxASSERT_MSG( box
, wxT("wxStaticBoxSizer needs a static box") );
1507 static void GetStaticBoxBorders( wxStaticBox
*box
,
1511 // this has to be done platform by platform as there is no way to
1512 // guess the thickness of a wxStaticBox border
1514 box
->GetBordersForSizer(borderTop
,borderOther
);
1515 #else // __WXCOCOA__
1517 if ( box
->GetLabel().IsEmpty() )
1521 *borderTop
= box
->GetCharHeight();
1524 #endif // __WXCOCOA__
1527 void wxStaticBoxSizer::RecalcSizes()
1529 int top_border
, other_border
;
1530 GetStaticBoxBorders(m_staticBox
, &top_border
, &other_border
);
1532 m_staticBox
->SetSize( m_position
.x
, m_position
.y
, m_size
.x
, m_size
.y
);
1534 wxPoint
old_pos( m_position
);
1535 m_position
.x
+= other_border
;
1536 m_position
.y
+= top_border
;
1537 wxSize
old_size( m_size
);
1538 m_size
.x
-= 2*other_border
;
1539 m_size
.y
-= top_border
+ other_border
;
1541 wxBoxSizer::RecalcSizes();
1543 m_position
= old_pos
;
1547 wxSize
wxStaticBoxSizer::CalcMin()
1549 int top_border
, other_border
;
1550 GetStaticBoxBorders(m_staticBox
, &top_border
, &other_border
);
1552 wxSize
ret( wxBoxSizer::CalcMin() );
1553 ret
.x
+= 2*other_border
;
1554 ret
.y
+= other_border
+ top_border
;
1559 #endif // wxUSE_STATBOX
1561 // ----------------------------------------------------------------------------
1563 // ----------------------------------------------------------------------------
1567 wxBookCtrlSizer::wxBookCtrlSizer(wxBookCtrl
*bookctrl
)
1568 : m_bookctrl(bookctrl
)
1570 wxASSERT_MSG( bookctrl
, wxT("wxBookCtrlSizer needs a control") );
1573 void wxBookCtrlSizer::RecalcSizes()
1575 m_bookctrl
->SetSize( m_position
.x
, m_position
.y
, m_size
.x
, m_size
.y
);
1578 wxSize
wxBookCtrlSizer::CalcMin()
1580 wxSize sizeBorder
= m_bookctrl
->CalcSizeFromPage(wxSize(0, 0));
1585 if ( m_bookctrl
->GetPageCount() == 0 )
1587 return wxSize(sizeBorder
.x
+ 10, sizeBorder
.y
+ 10);
1593 wxWindowList::compatibility_iterator
1594 node
= m_bookctrl
->GetChildren().GetFirst();
1597 wxWindow
*item
= node
->GetData();
1598 wxSizer
*itemsizer
= item
->GetSizer();
1602 wxSize
subsize( itemsizer
->CalcMin() );
1604 if (subsize
.x
> maxX
)
1606 if (subsize
.y
> maxY
)
1610 node
= node
->GetNext();
1613 return wxSize( maxX
, maxY
) + sizeBorder
;
1616 #endif // wxUSE_BOOKCTRL