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
)
566 wxSize
size(window
->IsTopLevel() ? FitSize(window
)
567 : GetMinWindowSize(window
));
569 window
->SetSize( size
);
574 void wxSizer::FitInside( wxWindow
*window
)
577 if (window
->IsTopLevel())
578 size
= VirtualFitSize( window
);
580 size
= GetMinClientSize( window
);
582 window
->SetVirtualSize( size
);
585 void wxSizer::Layout()
591 void wxSizer::SetSizeHints( wxWindow
*window
)
593 // Preserve the window's max size hints, but set the
594 // lower bound according to the sizer calculations.
596 wxSize size
= Fit( window
);
598 window
->SetSizeHints( size
.x
,
600 window
->GetMaxWidth(),
601 window
->GetMaxHeight() );
604 void wxSizer::SetVirtualSizeHints( wxWindow
*window
)
606 // Preserve the window's max size hints, but set the
607 // lower bound according to the sizer calculations.
610 wxSize
size( window
->GetVirtualSize() );
611 window
->SetVirtualSizeHints( size
.x
,
613 window
->GetMaxWidth(),
614 window
->GetMaxHeight() );
617 wxSize
wxSizer::GetMaxWindowSize( wxWindow
*window
) const
619 return window
->GetMaxSize();
622 wxSize
wxSizer::GetMinWindowSize( wxWindow
*window
)
624 wxSize
minSize( GetMinSize() );
625 wxSize
size( window
->GetSize() );
626 wxSize
client_size( window
->GetClientSize() );
628 return wxSize( minSize
.x
+size
.x
-client_size
.x
,
629 minSize
.y
+size
.y
-client_size
.y
);
632 // Return a window size that will fit within the screens dimensions
633 wxSize
wxSizer::FitSize( wxWindow
*window
)
635 wxSize size
= GetMinWindowSize( window
);
636 wxSize sizeMax
= GetMaxWindowSize( window
);
638 // Limit the size if sizeMax != wxDefaultSize
640 if ( size
.x
> sizeMax
.x
&& sizeMax
.x
!= -1 )
642 if ( size
.y
> sizeMax
.y
&& sizeMax
.y
!= -1 )
648 wxSize
wxSizer::GetMaxClientSize( wxWindow
*window
) const
650 wxSize
maxSize( window
->GetMaxSize() );
652 if( maxSize
!= wxDefaultSize
)
654 wxSize
size( window
->GetSize() );
655 wxSize
client_size( window
->GetClientSize() );
657 return wxSize( maxSize
.x
+ client_size
.x
- size
.x
,
658 maxSize
.y
+ client_size
.y
- size
.y
);
661 return wxDefaultSize
;
664 wxSize
wxSizer::GetMinClientSize( wxWindow
*WXUNUSED(window
) )
666 return GetMinSize(); // Already returns client size.
669 wxSize
wxSizer::VirtualFitSize( wxWindow
*window
)
671 wxSize size
= GetMinClientSize( window
);
672 wxSize sizeMax
= GetMaxClientSize( window
);
674 // Limit the size if sizeMax != wxDefaultSize
676 if ( size
.x
> sizeMax
.x
&& sizeMax
.x
!= -1 )
678 if ( size
.y
> sizeMax
.y
&& sizeMax
.y
!= -1 )
684 void wxSizer::SetDimension( int x
, int y
, int width
, int height
)
693 wxSize
wxSizer::GetMinSize()
695 wxSize
ret( CalcMin() );
696 if (ret
.x
< m_minSize
.x
) ret
.x
= m_minSize
.x
;
697 if (ret
.y
< m_minSize
.y
) ret
.y
= m_minSize
.y
;
701 void wxSizer::DoSetMinSize( int width
, int height
)
704 m_minSize
.y
= height
;
707 bool wxSizer::DoSetItemMinSize( wxWindow
*window
, int width
, int height
)
709 wxASSERT_MSG( window
, _T("SetMinSize for NULL window") );
711 // Is it our immediate child?
713 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
716 wxSizerItem
*item
= node
->GetData();
718 if (item
->GetWindow() == window
)
720 item
->SetInitSize( width
, height
);
723 node
= node
->GetNext();
726 // No? Search any subsizers we own then
728 node
= m_children
.GetFirst();
731 wxSizerItem
*item
= node
->GetData();
733 if ( item
->GetSizer() &&
734 item
->GetSizer()->DoSetItemMinSize( window
, width
, height
) )
736 // A child sizer found the requested windw, exit.
739 node
= node
->GetNext();
745 bool wxSizer::DoSetItemMinSize( wxSizer
*sizer
, int width
, int height
)
747 wxASSERT_MSG( sizer
, _T("SetMinSize for NULL sizer") );
749 // Is it our immediate child?
751 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
754 wxSizerItem
*item
= node
->GetData();
756 if (item
->GetSizer() == sizer
)
758 item
->GetSizer()->DoSetMinSize( width
, height
);
761 node
= node
->GetNext();
764 // No? Search any subsizers we own then
766 node
= m_children
.GetFirst();
769 wxSizerItem
*item
= node
->GetData();
771 if ( item
->GetSizer() &&
772 item
->GetSizer()->DoSetItemMinSize( sizer
, width
, height
) )
774 // A child found the requested sizer, exit.
777 node
= node
->GetNext();
783 bool wxSizer::DoSetItemMinSize( size_t index
, int width
, int height
)
785 wxSizerItemList::compatibility_iterator node
= m_children
.Item( index
);
787 wxCHECK_MSG( node
, false, _T("Failed to find child node") );
789 wxSizerItem
*item
= node
->GetData();
791 if (item
->GetSizer())
793 // Sizers contains the minimal size in them, if not calculated ...
794 item
->GetSizer()->DoSetMinSize( width
, height
);
798 // ... but the minimal size of spacers and windows in stored in them
799 item
->SetInitSize( width
, height
);
805 void wxSizer::Show( wxWindow
*window
, bool show
)
807 wxASSERT_MSG( window
, _T("Show for NULL window") );
809 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
812 wxSizerItem
*item
= node
->GetData();
814 if (item
->GetWindow() == window
)
819 node
= node
->GetNext();
823 void wxSizer::Show( wxSizer
*sizer
, bool show
)
825 wxASSERT_MSG( sizer
, _T("Show for NULL sizer") );
827 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
830 wxSizerItem
*item
= node
->GetData();
832 if (item
->GetSizer() == sizer
)
837 node
= node
->GetNext();
841 void wxSizer::Show( size_t index
, bool show
)
843 wxCHECK_RET( index
< m_children
.GetCount(),
844 _T("Show index is out of range") );
846 m_children
.Item( index
)->GetData()->Show( show
);
849 void wxSizer::ShowItems( bool show
)
851 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
854 node
->GetData()->Show( show
);
855 node
= node
->GetNext();
859 bool wxSizer::IsShown( wxWindow
*window
) const
861 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
864 wxSizerItem
*item
= node
->GetData();
866 if (item
->GetWindow() == window
)
868 return item
->IsShown();
870 node
= node
->GetNext();
873 wxFAIL_MSG( _T("IsShown failed to find sizer item") );
878 bool wxSizer::IsShown( wxSizer
*sizer
) const
880 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
883 wxSizerItem
*item
= node
->GetData();
885 if (item
->GetSizer() == sizer
)
887 return item
->IsShown();
889 node
= node
->GetNext();
892 wxFAIL_MSG( _T("IsShown failed to find sizer item") );
897 bool wxSizer::IsShown( size_t index
) const
899 wxCHECK_MSG( index
< m_children
.GetCount(),
901 _T("IsShown index is out of range") );
903 return m_children
.Item( index
)->GetData()->IsShown();
907 //---------------------------------------------------------------------------
909 //---------------------------------------------------------------------------
911 wxGridSizer::wxGridSizer( int rows
, int cols
, int vgap
, int hgap
)
917 if (m_rows
== 0 && m_cols
== 0)
921 wxGridSizer::wxGridSizer( int cols
, int vgap
, int hgap
)
927 if (m_rows
== 0 && m_cols
== 0)
931 int wxGridSizer::CalcRowsCols(int& nrows
, int& ncols
) const
933 int nitems
= m_children
.GetCount();
939 nrows
= (nitems
+ m_cols
- 1) / m_cols
;
943 ncols
= (nitems
+ m_rows
- 1) / m_rows
;
946 else // 0 columns, 0 rows?
948 wxFAIL_MSG( _T("grid sizer must have either rows or columns fixed") );
957 void wxGridSizer::RecalcSizes()
959 int nitems
, nrows
, ncols
;
960 if ( (nitems
= CalcRowsCols(nrows
, ncols
)) == 0 )
963 wxSize
sz( GetSize() );
964 wxPoint
pt( GetPosition() );
966 int w
= (sz
.x
- (ncols
- 1) * m_hgap
) / ncols
;
967 int h
= (sz
.y
- (nrows
- 1) * m_vgap
) / nrows
;
970 for (int c
= 0; c
< ncols
; c
++)
973 for (int r
= 0; r
< nrows
; r
++)
975 int i
= r
* ncols
+ c
;
978 wxSizerItemList::compatibility_iterator node
= m_children
.Item( i
);
980 wxASSERT_MSG( node
, _T("Failed to find SizerItemList node") );
982 SetItemBounds( node
->GetData(), x
, y
, w
, h
);
990 wxSize
wxGridSizer::CalcMin()
993 if ( CalcRowsCols(nrows
, ncols
) == 0 )
994 return wxSize(10, 10);
996 // Find the max width and height for any component
1000 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
1003 wxSizerItem
*item
= node
->GetData();
1004 wxSize
sz( item
->CalcMin() );
1006 w
= wxMax( w
, sz
.x
);
1007 h
= wxMax( h
, sz
.y
);
1009 node
= node
->GetNext();
1012 return wxSize( ncols
* w
+ (ncols
-1) * m_hgap
,
1013 nrows
* h
+ (nrows
-1) * m_vgap
);
1016 void wxGridSizer::SetItemBounds( wxSizerItem
*item
, int x
, int y
, int w
, int h
)
1019 wxSize
sz( item
->CalcMin() );
1020 int flag
= item
->GetFlag();
1022 if ((flag
& wxEXPAND
) || (flag
& wxSHAPED
))
1028 if (flag
& wxALIGN_CENTER_HORIZONTAL
)
1030 pt
.x
= x
+ (w
- sz
.x
) / 2;
1032 else if (flag
& wxALIGN_RIGHT
)
1034 pt
.x
= x
+ (w
- sz
.x
);
1037 if (flag
& wxALIGN_CENTER_VERTICAL
)
1039 pt
.y
= y
+ (h
- sz
.y
) / 2;
1041 else if (flag
& wxALIGN_BOTTOM
)
1043 pt
.y
= y
+ (h
- sz
.y
);
1047 item
->SetDimension(pt
, sz
);
1050 //---------------------------------------------------------------------------
1052 //---------------------------------------------------------------------------
1054 wxFlexGridSizer::wxFlexGridSizer( int rows
, int cols
, int vgap
, int hgap
)
1055 : wxGridSizer( rows
, cols
, vgap
, hgap
),
1056 m_flexDirection(wxBOTH
),
1057 m_growMode(wxFLEX_GROWMODE_SPECIFIED
)
1061 wxFlexGridSizer::wxFlexGridSizer( int cols
, int vgap
, int hgap
)
1062 : wxGridSizer( cols
, vgap
, hgap
),
1063 m_flexDirection(wxBOTH
),
1064 m_growMode(wxFLEX_GROWMODE_SPECIFIED
)
1068 wxFlexGridSizer::~wxFlexGridSizer()
1072 void wxFlexGridSizer::RecalcSizes()
1074 int nitems
, nrows
, ncols
;
1075 if ( (nitems
= CalcRowsCols(nrows
, ncols
)) == 0 )
1078 wxSize
sz( GetSize() );
1079 wxSize
minsz( CalcMin() );
1080 wxPoint
pt( GetPosition() );
1082 // what to do with the rows? by default, resize them proportionally
1083 if ( sz
.y
> minsz
.y
&& ( (m_flexDirection
& wxVERTICAL
) || (m_growMode
== wxFLEX_GROWMODE_SPECIFIED
) ) )
1085 int sum_proportions
= 0;
1086 int growable_space
= 0;
1089 for (idx
= 0; idx
< m_growableRows
.GetCount(); idx
++)
1091 // Since the number of rows/columns can change as items are inserted/deleted, we need
1092 // to verify at runtime that the requested growable rows/columns are still valid.
1093 if (m_growableRows
[idx
] >= nrows
)
1095 // If all items in a row/column are hidden, that row/column will have a dimension of -1.
1096 // This causes the row/column to be hidden completely.
1097 if (m_rowHeights
[ m_growableRows
[idx
] ] == -1)
1099 sum_proportions
+= m_growableRowsProportions
[idx
];
1100 growable_space
+= m_rowHeights
[ m_growableRows
[idx
] ];
1106 for (idx
= 0; idx
< m_growableRows
.GetCount(); idx
++)
1108 if (m_growableRows
[idx
] >= nrows
)
1110 if (m_rowHeights
[ m_growableRows
[idx
] ] == -1)
1111 m_rowHeights
[ m_growableRows
[idx
] ] = 0;
1114 int delta
= (sz
.y
- minsz
.y
);
1115 if (sum_proportions
== 0)
1116 delta
= (delta
/num
) + m_rowHeights
[ m_growableRows
[idx
] ];
1118 delta
= ((delta
+growable_space
)*m_growableRowsProportions
[idx
]) / sum_proportions
;
1119 m_rowHeights
[ m_growableRows
[idx
] ] = delta
;
1124 else if ( (m_growMode
== wxFLEX_GROWMODE_ALL
) && (sz
.y
> minsz
.y
) )
1126 // rounding problem?
1127 for ( int row
= 0; row
< nrows
; ++row
)
1128 m_rowHeights
[ row
] = sz
.y
/ nrows
;
1131 // the same logic as above but for the columns
1132 if ( sz
.x
> minsz
.x
&& ( (m_flexDirection
& wxHORIZONTAL
) || (m_growMode
== wxFLEX_GROWMODE_SPECIFIED
) ) )
1134 int sum_proportions
= 0;
1135 int growable_space
= 0;
1138 for (idx
= 0; idx
< m_growableCols
.GetCount(); idx
++)
1140 // Since the number of rows/columns can change as items are inserted/deleted, we need
1141 // to verify at runtime that the requested growable rows/columns are still valid.
1142 if (m_growableCols
[idx
] >= ncols
)
1144 // If all items in a row/column are hidden, that row/column will have a dimension of -1.
1145 // This causes the column to be hidden completely.
1146 if (m_colWidths
[ m_growableCols
[idx
] ] == -1)
1148 sum_proportions
+= m_growableColsProportions
[idx
];
1149 // wtb 5/12/02 bugfix - was m_ColWidths[idx]!!
1150 growable_space
+= m_colWidths
[ m_growableCols
[idx
] ];
1156 for (idx
= 0; idx
< m_growableCols
.GetCount(); idx
++)
1158 if (m_growableCols
[idx
] >= ncols
)
1160 if (m_colWidths
[ m_growableCols
[idx
] ] == -1)
1161 m_colWidths
[ m_growableCols
[idx
] ] = 0;
1164 int delta
= (sz
.x
- minsz
.x
);
1165 if (sum_proportions
== 0)
1166 delta
= (delta
/num
) + m_colWidths
[ m_growableCols
[idx
] ];
1168 delta
= ((delta
+growable_space
)*m_growableColsProportions
[idx
])/sum_proportions
;
1169 m_colWidths
[ m_growableCols
[idx
] ] = delta
;
1174 else if ( (m_growMode
== wxFLEX_GROWMODE_ALL
) && (sz
.x
> minsz
.x
) )
1176 for ( int col
=0; col
< ncols
; ++col
)
1177 m_colWidths
[ col
] = sz
.x
/ ncols
;
1180 sz
= wxSize( pt
.x
+ sz
.x
, pt
.y
+ sz
.y
);
1183 for (int c
= 0; c
< ncols
; c
++)
1186 for (int r
= 0; r
< nrows
; r
++)
1188 int i
= r
* ncols
+ c
;
1191 wxSizerItemList::compatibility_iterator node
= m_children
.Item( i
);
1193 wxASSERT_MSG( node
, _T("Failed to find node") );
1195 int w
= wxMax( 0, wxMin( m_colWidths
[c
], sz
.x
- x
) );
1196 int h
= wxMax( 0, wxMin( m_rowHeights
[r
], sz
.y
- y
) );
1198 SetItemBounds( node
->GetData(), x
, y
, w
, h
);
1200 y
= y
+ m_rowHeights
[r
] + m_vgap
;
1202 x
= x
+ m_colWidths
[c
] + m_hgap
;
1206 wxSize
wxFlexGridSizer::CalcMin()
1212 // Number of rows/columns can change as items are added or removed.
1213 if ( !CalcRowsCols(nrows
, ncols
) )
1214 return wxSize(10, 10);
1216 m_rowHeights
.SetCount(nrows
);
1217 m_colWidths
.SetCount(ncols
);
1219 // We have to recalcuate the sizes in case an item has wxADJUST_MINSIZE, has changed
1220 // minimum size since the previous layout, or has been hidden using wxSizer::Show().
1221 // If all the items in a row/column are hidden, the final dimension of the row/column
1222 // will be -1, indicating that the column itself is hidden.
1223 for( s
= m_rowHeights
.GetCount(), i
= 0; i
< s
; ++i
)
1224 m_rowHeights
[ i
] = -1;
1225 for( s
= m_colWidths
.GetCount(), i
= 0; i
< s
; ++i
)
1226 m_colWidths
[ i
] = -1;
1228 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
1233 wxSizerItem
*item
= node
->GetData();
1234 if ( item
->IsShown() )
1236 wxSize
sz( item
->CalcMin() );
1237 int row
= i
/ ncols
;
1238 int col
= i
% ncols
;
1240 m_rowHeights
[ row
] = wxMax( wxMax( 0, sz
.y
), m_rowHeights
[ row
] );
1241 m_colWidths
[ col
] = wxMax( wxMax( 0, sz
.x
), m_colWidths
[ col
] );
1244 node
= node
->GetNext();
1248 // the logic above works when we resize flexibly in both directions but
1249 // maybe this is not the case
1250 if ( m_flexDirection
!= wxBOTH
)
1252 // select the array corresponding to the direction in which we do *not*
1254 wxArrayInt
& array
= m_flexDirection
== wxVERTICAL
? m_colWidths
1257 const int count
= array
.GetCount();
1259 // find the largest value in this array
1261 for ( n
= 0; n
< count
; ++n
)
1263 if ( array
[n
] > largest
)
1267 // and now fill it with the largest value
1268 for ( n
= 0; n
< count
; ++n
)
1274 // Sum total minimum size, including gaps between rows/columns.
1275 // -1 is used as a magic number meaning empty column.
1277 for (int col
= 0; col
< ncols
; col
++)
1278 if ( m_colWidths
[ col
] != -1 )
1279 width
+= m_colWidths
[ col
] + ( col
== ncols
-1 ? 0 : m_hgap
);
1282 for (int row
= 0; row
< nrows
; row
++)
1283 if ( m_rowHeights
[ row
] != -1 )
1284 height
+= m_rowHeights
[ row
] + ( row
== nrows
-1 ? 0 : m_vgap
);
1286 return wxSize( width
, height
);
1289 void wxFlexGridSizer::AddGrowableRow( size_t idx
, int proportion
)
1291 m_growableRows
.Add( idx
);
1292 m_growableRowsProportions
.Add( proportion
);
1295 void wxFlexGridSizer::RemoveGrowableRow( size_t idx
)
1297 m_growableRows
.Remove( idx
);
1300 void wxFlexGridSizer::AddGrowableCol( size_t idx
, int proportion
)
1302 m_growableCols
.Add( idx
);
1303 m_growableColsProportions
.Add( proportion
);
1306 void wxFlexGridSizer::RemoveGrowableCol( size_t idx
)
1308 m_growableCols
.Remove( idx
);
1311 //---------------------------------------------------------------------------
1313 //---------------------------------------------------------------------------
1315 wxBoxSizer::wxBoxSizer( int orient
)
1316 : m_orient( orient
)
1320 void wxBoxSizer::RecalcSizes()
1322 if (m_children
.GetCount() == 0)
1328 if (m_orient
== wxHORIZONTAL
)
1329 delta
= m_size
.x
- m_fixedWidth
;
1331 delta
= m_size
.y
- m_fixedHeight
;
1334 wxPoint
pt( m_position
);
1336 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
1339 wxSizerItem
*item
= node
->GetData();
1341 if (item
->IsShown())
1343 wxSize
size( item
->CalcMin() );
1345 if (m_orient
== wxVERTICAL
)
1347 wxCoord height
= size
.y
;
1348 if (item
->GetProportion())
1350 // Because of at least one visible item has non-zero
1351 // proportion then m_stretchable is not zero
1352 height
= (delta
* item
->GetProportion()) / m_stretchable
;
1355 wxPoint
child_pos( pt
);
1356 wxSize
child_size( wxSize( size
.x
, height
) );
1358 if (item
->GetFlag() & (wxEXPAND
| wxSHAPED
))
1359 child_size
.x
= m_size
.x
;
1360 else if (item
->GetFlag() & wxALIGN_RIGHT
)
1361 child_pos
.x
+= m_size
.x
- size
.x
;
1362 else if (item
->GetFlag() & (wxCENTER
| wxALIGN_CENTER_HORIZONTAL
))
1363 // XXX wxCENTER is added for backward compatibility;
1364 // wxALIGN_CENTER should be used in new code
1365 child_pos
.x
+= (m_size
.x
- size
.x
) / 2;
1367 item
->SetDimension( child_pos
, child_size
);
1373 wxCoord width
= size
.x
;
1374 if (item
->GetProportion())
1376 // Because of at least one visible item has non-zero
1377 // proportion then m_stretchable is not zero
1378 width
= (delta
* item
->GetProportion()) / m_stretchable
;
1381 wxPoint
child_pos( pt
);
1382 wxSize
child_size( wxSize(width
, size
.y
) );
1384 if (item
->GetFlag() & (wxEXPAND
| wxSHAPED
))
1385 child_size
.y
= m_size
.y
;
1386 else if (item
->GetFlag() & wxALIGN_BOTTOM
)
1387 child_pos
.y
+= m_size
.y
- size
.y
;
1388 else if (item
->GetFlag() & (wxCENTER
| wxALIGN_CENTER_VERTICAL
))
1389 // XXX wxCENTER is added for backward compatibility;
1390 // wxALIGN_CENTER should be used in new code
1391 child_pos
.y
+= (m_size
.y
- size
.y
) / 2;
1393 item
->SetDimension( child_pos
, child_size
);
1399 node
= node
->GetNext();
1403 wxSize
wxBoxSizer::CalcMin()
1405 if (m_children
.GetCount() == 0)
1406 return wxSize(10,10);
1414 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
1417 wxSizerItem
*item
= node
->GetData();
1419 if (item
->IsShown() && item
->GetProportion() != 0)
1420 m_stretchable
+= item
->GetProportion();
1422 node
= node
->GetNext();
1425 // Total minimum size (width or height) of sizer
1428 node
= m_children
.GetFirst();
1431 wxSizerItem
*item
= node
->GetData();
1433 if (item
->IsShown() && item
->GetProportion() != 0)
1435 int stretch
= item
->GetProportion();
1436 wxSize
size( item
->CalcMin() );
1439 // Integer division rounded up is (a + b - 1) / b
1440 // Round up needed in order to guarantee that all
1441 // all items will have size not less then their min size
1442 if (m_orient
== wxHORIZONTAL
)
1443 minSize
= ( size
.x
*m_stretchable
+ stretch
- 1)/stretch
;
1445 minSize
= ( size
.y
*m_stretchable
+ stretch
- 1)/stretch
;
1447 if (minSize
> maxMinSize
)
1448 maxMinSize
= minSize
;
1450 node
= node
->GetNext();
1453 // Calculate overall minimum size
1454 node
= m_children
.GetFirst();
1457 wxSizerItem
*item
= node
->GetData();
1459 if (item
->IsShown())
1461 wxSize
size( item
->CalcMin() );
1462 if (item
->GetProportion() != 0)
1464 if (m_orient
== wxHORIZONTAL
)
1465 size
.x
= (maxMinSize
*item
->GetProportion())/m_stretchable
;
1467 size
.y
= (maxMinSize
*item
->GetProportion())/m_stretchable
;
1471 if (m_orient
== wxVERTICAL
)
1473 m_fixedHeight
+= size
.y
;
1474 m_fixedWidth
= wxMax( m_fixedWidth
, size
.x
);
1478 m_fixedWidth
+= size
.x
;
1479 m_fixedHeight
= wxMax( m_fixedHeight
, size
.y
);
1483 if (m_orient
== wxHORIZONTAL
)
1485 m_minWidth
+= size
.x
;
1486 m_minHeight
= wxMax( m_minHeight
, size
.y
);
1490 m_minHeight
+= size
.y
;
1491 m_minWidth
= wxMax( m_minWidth
, size
.x
);
1494 node
= node
->GetNext();
1497 return wxSize( m_minWidth
, m_minHeight
);
1500 //---------------------------------------------------------------------------
1502 //---------------------------------------------------------------------------
1506 wxStaticBoxSizer::wxStaticBoxSizer( wxStaticBox
*box
, int orient
)
1507 : wxBoxSizer( orient
)
1508 , m_staticBox( box
)
1510 wxASSERT_MSG( box
, wxT("wxStaticBoxSizer needs a static box") );
1513 static void GetStaticBoxBorders( wxStaticBox
*box
,
1517 // this has to be done platform by platform as there is no way to
1518 // guess the thickness of a wxStaticBox border
1520 box
->GetBordersForSizer(borderTop
,borderOther
);
1521 #else // __WXCOCOA__
1523 if ( box
->GetLabel().IsEmpty() )
1527 *borderTop
= box
->GetCharHeight();
1530 #endif // __WXCOCOA__
1533 void wxStaticBoxSizer::RecalcSizes()
1535 int top_border
, other_border
;
1536 GetStaticBoxBorders(m_staticBox
, &top_border
, &other_border
);
1538 m_staticBox
->SetSize( m_position
.x
, m_position
.y
, m_size
.x
, m_size
.y
);
1540 wxPoint
old_pos( m_position
);
1541 m_position
.x
+= other_border
;
1542 m_position
.y
+= top_border
;
1543 wxSize
old_size( m_size
);
1544 m_size
.x
-= 2*other_border
;
1545 m_size
.y
-= top_border
+ other_border
;
1547 wxBoxSizer::RecalcSizes();
1549 m_position
= old_pos
;
1553 wxSize
wxStaticBoxSizer::CalcMin()
1555 int top_border
, other_border
;
1556 GetStaticBoxBorders(m_staticBox
, &top_border
, &other_border
);
1558 wxSize
ret( wxBoxSizer::CalcMin() );
1559 ret
.x
+= 2*other_border
;
1560 ret
.y
+= other_border
+ top_border
;
1565 #endif // wxUSE_STATBOX
1567 // ----------------------------------------------------------------------------
1569 // ----------------------------------------------------------------------------
1573 wxBookCtrlSizer::wxBookCtrlSizer(wxBookCtrl
*bookctrl
)
1574 : m_bookctrl(bookctrl
)
1576 wxASSERT_MSG( bookctrl
, wxT("wxBookCtrlSizer needs a control") );
1579 void wxBookCtrlSizer::RecalcSizes()
1581 m_bookctrl
->SetSize( m_position
.x
, m_position
.y
, m_size
.x
, m_size
.y
);
1584 wxSize
wxBookCtrlSizer::CalcMin()
1586 wxSize sizeBorder
= m_bookctrl
->CalcSizeFromPage(wxSize(0, 0));
1591 if ( m_bookctrl
->GetPageCount() == 0 )
1593 return wxSize(sizeBorder
.x
+ 10, sizeBorder
.y
+ 10);
1599 wxWindowList::compatibility_iterator
1600 node
= m_bookctrl
->GetChildren().GetFirst();
1603 wxWindow
*item
= node
->GetData();
1604 wxSizer
*itemsizer
= item
->GetSizer();
1608 wxSize
subsize( itemsizer
->CalcMin() );
1610 if (subsize
.x
> maxX
)
1612 if (subsize
.y
> maxY
)
1616 node
= node
->GetNext();
1619 return wxSize( maxX
, maxY
) + sizeBorder
;
1625 wxNotebookSizer::wxNotebookSizer(wxNotebook
*nb
)
1626 : wxBookCtrlSizer(nb
)
1630 #endif // wxUSE_NOTEBOOOK
1631 #endif // wxUSE_BOOKCTRL