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(wxNotebookSizer
, wxSizer
)
44 WX_DEFINE_EXPORTED_LIST( wxSizerItemList
);
78 //---------------------------------------------------------------------------
80 //---------------------------------------------------------------------------
82 wxSizerItem::wxSizerItem( int width
, int height
, int proportion
, int flag
, int border
, wxObject
* userData
)
85 , m_size( wxSize( width
, height
) ) // size is set directly
86 , m_minSize( m_size
) // minimal size is the initial size
87 , m_proportion( proportion
)
91 , m_userData( userData
)
96 wxSizerItem::wxSizerItem( wxWindow
*window
, int proportion
, int flag
, int border
, wxObject
* userData
)
99 , m_minSize( window
->GetSize() ) // minimal size is the initial size
100 , m_proportion( proportion
)
104 , m_userData( userData
)
106 // aspect ratio calculated from initial size
107 SetRatio( m_minSize
);
109 // m_size is calculated later
112 wxSizerItem::wxSizerItem( wxSizer
*sizer
, int proportion
, int flag
, int border
, wxObject
* userData
)
115 , m_proportion( proportion
)
120 , m_userData( userData
)
122 // m_minSize is calculated later
123 // m_size is calculated later
126 wxSizerItem::~wxSizerItem()
132 m_window
->SetContainingSizer(NULL
);
134 else // we must be a sizer
141 wxSize
wxSizerItem::GetSize() const
145 ret
= m_sizer
->GetSize();
148 ret
= m_window
->GetSize();
155 if (m_flag
& wxNORTH
)
157 if (m_flag
& wxSOUTH
)
163 wxSize
wxSizerItem::CalcMin()
168 ret
= m_sizer
->GetMinSize();
170 // if we have to preserve aspect ratio _AND_ this is
171 // the first-time calculation, consider ret to be initial size
172 if ((m_flag
& wxSHAPED
) && !m_ratio
)
177 if ( IsWindow() && (m_flag
& wxADJUST_MINSIZE
) )
179 // By user request, keep the minimal size for this item
180 // in sync with the largest of BestSize and any user supplied
181 // minimum size hint. Useful in cases where the item is
182 // changeable -- static text labels, etc.
183 m_minSize
= m_window
->GetAdjustedBestSize();
193 if (m_flag
& wxNORTH
)
195 if (m_flag
& wxSOUTH
)
201 void wxSizerItem::SetDimension( wxPoint pos
, wxSize size
)
203 if (m_flag
& wxSHAPED
)
205 // adjust aspect ratio
206 int rwidth
= (int) (size
.y
* m_ratio
);
210 int rheight
= (int) (size
.x
/ m_ratio
);
211 // add vertical space
212 if (m_flag
& wxALIGN_CENTER_VERTICAL
)
213 pos
.y
+= (size
.y
- rheight
) / 2;
214 else if (m_flag
& wxALIGN_BOTTOM
)
215 pos
.y
+= (size
.y
- rheight
);
216 // use reduced dimensions
219 else if (rwidth
< size
.x
)
221 // add horizontal space
222 if (m_flag
& wxALIGN_CENTER_HORIZONTAL
)
223 pos
.x
+= (size
.x
- rwidth
) / 2;
224 else if (m_flag
& wxALIGN_RIGHT
)
225 pos
.x
+= (size
.x
- rwidth
);
230 // This is what GetPosition() returns. Since we calculate
231 // borders afterwards, GetPosition() will be the left/top
232 // corner of the surrounding border.
244 if (m_flag
& wxNORTH
)
249 if (m_flag
& wxSOUTH
)
255 m_sizer
->SetDimension( pos
.x
, pos
.y
, size
.x
, size
.y
);
258 m_window
->SetSize( pos
.x
, pos
.y
, size
.x
, size
.y
, wxSIZE_ALLOW_MINUS_ONE
);
263 void wxSizerItem::DeleteWindows()
269 m_sizer
->DeleteWindows();
272 bool wxSizerItem::IsWindow() const
274 return (m_window
!= NULL
);
277 bool wxSizerItem::IsSizer() const
279 return (m_sizer
!= NULL
);
282 bool wxSizerItem::IsSpacer() const
284 return (m_window
== NULL
) && (m_sizer
== NULL
);
287 void wxSizerItem::Show( bool show
)
292 m_window
->Show( show
);
294 m_sizer
->ShowItems( show
);
296 // ... nothing else to do to hide/show spacers
299 void wxSizerItem::SetOption( int option
)
301 SetProportion( option
);
304 int wxSizerItem::GetOption() const
306 return GetProportion();
310 //---------------------------------------------------------------------------
312 //---------------------------------------------------------------------------
315 : m_minSize( wxSize( 0, 0 ) )
321 WX_CLEAR_LIST(wxSizerItemList
, m_children
);
324 void wxSizer::Add( wxWindow
*window
, int proportion
, int flag
, int border
, wxObject
* userData
)
326 m_children
.Append( new wxSizerItem( window
, proportion
, flag
, border
, userData
) );
327 window
->SetContainingSizer( this );
330 void wxSizer::Add( wxSizer
*sizer
, int proportion
, int flag
, int border
, wxObject
* userData
)
332 m_children
.Append( new wxSizerItem( sizer
, proportion
, flag
, border
, userData
) );
335 void wxSizer::Add( int width
, int height
, int proportion
, int flag
, int border
, wxObject
* userData
)
337 m_children
.Append( new wxSizerItem( width
, height
, proportion
, flag
, border
, userData
) );
340 void wxSizer::Add( wxSizerItem
*item
)
342 m_children
.Append( item
);
344 if( item
->GetWindow() )
345 item
->GetWindow()->SetContainingSizer( this );
348 void wxSizer::Prepend( wxWindow
*window
, int proportion
, int flag
, int border
, wxObject
* userData
)
350 m_children
.Insert( new wxSizerItem( window
, proportion
, flag
, border
, userData
) );
351 window
->SetContainingSizer( this );
354 void wxSizer::Prepend( wxSizer
*sizer
, int proportion
, int flag
, int border
, wxObject
* userData
)
356 m_children
.Insert( new wxSizerItem( sizer
, proportion
, flag
, border
, userData
) );
359 void wxSizer::Prepend( int width
, int height
, int proportion
, int flag
, int border
, wxObject
* userData
)
361 m_children
.Insert( new wxSizerItem( width
, height
, proportion
, flag
, border
, userData
) );
364 void wxSizer::Prepend( wxSizerItem
*item
)
366 m_children
.Insert( item
);
368 if( item
->GetWindow() )
369 item
->GetWindow()->SetContainingSizer( this );
372 void wxSizer::Insert( size_t index
,
379 m_children
.Insert( index
,
380 new wxSizerItem( window
, proportion
, flag
, border
, userData
) );
381 window
->SetContainingSizer( this );
384 void wxSizer::Insert( size_t index
,
391 m_children
.Insert( index
,
392 new wxSizerItem( sizer
, proportion
, flag
, border
, userData
) );
395 void wxSizer::Insert( size_t index
,
403 m_children
.Insert( index
,
404 new wxSizerItem( width
, height
, proportion
, flag
, border
, userData
) );
407 void wxSizer::Insert( size_t index
, wxSizerItem
*item
)
409 m_children
.Insert( index
, item
);
411 if( item
->GetWindow() )
412 item
->GetWindow()->SetContainingSizer( this );
415 bool wxSizer::Remove( wxWindow
*window
)
417 return Detach( window
);
420 bool wxSizer::Remove( wxSizer
*sizer
)
422 wxASSERT_MSG( sizer
, _T("Removing NULL sizer") );
424 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
427 wxSizerItem
*item
= node
->GetData();
429 if (item
->GetSizer() == sizer
)
432 m_children
.Erase( node
);
436 node
= node
->GetNext();
442 bool wxSizer::Remove( int index
)
444 wxCHECK_MSG( index
>= 0 && (size_t)index
< m_children
.GetCount(),
446 _T("Remove index is out of range") );
448 wxSizerItemList::compatibility_iterator node
= m_children
.Item( index
);
450 wxCHECK_MSG( node
, false, _T("Failed to find child node") );
452 wxSizerItem
*item
= node
->GetData();
454 if( item
->IsWindow() )
455 item
->GetWindow()->SetContainingSizer( NULL
);
458 m_children
.Erase( node
);
462 bool wxSizer::Detach( wxSizer
*sizer
)
464 wxASSERT_MSG( sizer
, _T("Detaching NULL sizer") );
466 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
469 wxSizerItem
*item
= node
->GetData();
471 if (item
->GetSizer() == sizer
)
475 m_children
.Erase( node
);
478 node
= node
->GetNext();
484 bool wxSizer::Detach( wxWindow
*window
)
486 wxASSERT_MSG( window
, _T("Detaching NULL window") );
488 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
491 wxSizerItem
*item
= node
->GetData();
493 if (item
->GetWindow() == window
)
495 item
->GetWindow()->SetContainingSizer( NULL
);
497 m_children
.Erase( node
);
500 node
= node
->GetNext();
506 bool wxSizer::Detach( int index
)
508 wxCHECK_MSG( index
>= 0 && (size_t)index
< m_children
.GetCount(),
510 _T("Detach index is out of range") );
512 wxSizerItemList::compatibility_iterator node
= m_children
.Item( index
);
514 wxCHECK_MSG( node
, false, _T("Failed to find child node") );
516 wxSizerItem
*item
= node
->GetData();
518 if( item
->IsSizer() )
520 else if( item
->IsWindow() )
521 item
->GetWindow()->SetContainingSizer( NULL
);
524 m_children
.Erase( node
);
528 void wxSizer::Clear( bool delete_windows
)
530 // First clear the ContainingSizer pointers
531 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
534 wxSizerItem
*item
= node
->GetData();
536 if (item
->IsWindow())
537 item
->GetWindow()->SetContainingSizer( NULL
);
538 node
= node
->GetNext();
541 // Destroy the windows if needed
545 // Now empty the list
546 WX_CLEAR_LIST(wxSizerItemList
, m_children
);
549 void wxSizer::DeleteWindows()
551 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
554 wxSizerItem
*item
= node
->GetData();
556 item
->DeleteWindows();
557 node
= node
->GetNext();
561 wxSize
wxSizer::Fit( wxWindow
*window
)
564 if (window
->IsTopLevel())
565 size
= FitSize( window
);
567 size
= 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
)
919 wxGridSizer::wxGridSizer( int cols
, int vgap
, int hgap
)
927 int wxGridSizer::CalcRowsCols(int& nrows
, int& ncols
) const
929 int nitems
= m_children
.GetCount();
935 nrows
= (nitems
+ m_cols
- 1) / m_cols
;
939 ncols
= (nitems
+ m_rows
- 1) / m_rows
;
942 else // 0 columns, 0 rows?
944 wxFAIL_MSG( _T("grid sizer must have either rows or columns fixed") );
953 void wxGridSizer::RecalcSizes()
955 int nitems
, nrows
, ncols
;
956 if ( (nitems
= CalcRowsCols(nrows
, ncols
)) == 0 )
959 wxSize
sz( GetSize() );
960 wxPoint
pt( GetPosition() );
962 int w
= (sz
.x
- (ncols
- 1) * m_hgap
) / ncols
;
963 int h
= (sz
.y
- (nrows
- 1) * m_vgap
) / nrows
;
966 for (int c
= 0; c
< ncols
; c
++)
969 for (int r
= 0; r
< nrows
; r
++)
971 int i
= r
* ncols
+ c
;
974 wxSizerItemList::compatibility_iterator node
= m_children
.Item( i
);
976 wxASSERT_MSG( node
, _T("Failed to find SizerItemList node") );
978 SetItemBounds( node
->GetData(), x
, y
, w
, h
);
986 wxSize
wxGridSizer::CalcMin()
988 int nitems
, nrows
, ncols
;
989 if ( (nitems
= CalcRowsCols(nrows
, ncols
)) == 0 )
990 return wxSize(10, 10);
992 // Find the max width and height for any component
996 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
999 wxSizerItem
*item
= node
->GetData();
1000 wxSize
sz( item
->CalcMin() );
1002 w
= wxMax( w
, sz
.x
);
1003 h
= wxMax( h
, sz
.y
);
1005 node
= node
->GetNext();
1008 return wxSize( ncols
* w
+ (ncols
-1) * m_hgap
,
1009 nrows
* h
+ (nrows
-1) * m_vgap
);
1012 void wxGridSizer::SetItemBounds( wxSizerItem
*item
, int x
, int y
, int w
, int h
)
1015 wxSize
sz( item
->CalcMin() );
1016 int flag
= item
->GetFlag();
1018 if ((flag
& wxEXPAND
) || (flag
& wxSHAPED
))
1024 if (flag
& wxALIGN_CENTER_HORIZONTAL
)
1026 pt
.x
= x
+ (w
- sz
.x
) / 2;
1028 else if (flag
& wxALIGN_RIGHT
)
1030 pt
.x
= x
+ (w
- sz
.x
);
1033 if (flag
& wxALIGN_CENTER_VERTICAL
)
1035 pt
.y
= y
+ (h
- sz
.y
) / 2;
1037 else if (flag
& wxALIGN_BOTTOM
)
1039 pt
.y
= y
+ (h
- sz
.y
);
1043 item
->SetDimension(pt
, sz
);
1046 //---------------------------------------------------------------------------
1048 //---------------------------------------------------------------------------
1050 wxFlexGridSizer::wxFlexGridSizer( int rows
, int cols
, int vgap
, int hgap
)
1051 : wxGridSizer( rows
, cols
, vgap
, hgap
),
1052 m_flexDirection(wxBOTH
),
1053 m_growMode(wxFLEX_GROWMODE_SPECIFIED
)
1057 wxFlexGridSizer::wxFlexGridSizer( int cols
, int vgap
, int hgap
)
1058 : wxGridSizer( cols
, vgap
, hgap
),
1059 m_flexDirection(wxBOTH
),
1060 m_growMode(wxFLEX_GROWMODE_SPECIFIED
)
1064 wxFlexGridSizer::~wxFlexGridSizer()
1068 void wxFlexGridSizer::RecalcSizes()
1070 int nitems
, nrows
, ncols
;
1071 if ( (nitems
= CalcRowsCols(nrows
, ncols
)) == 0 )
1074 wxSize
sz( GetSize() );
1075 wxSize
minsz( CalcMin() );
1076 wxPoint
pt( GetPosition() );
1078 // what to do with the rows? by default, resize them proportionally
1079 if ( sz
.y
> minsz
.y
&& ( (m_flexDirection
& wxVERTICAL
) || (m_growMode
== wxFLEX_GROWMODE_SPECIFIED
) ) )
1081 int sum_proportions
= 0;
1082 int growable_space
= 0;
1085 for (idx
= 0; idx
< m_growableRows
.GetCount(); idx
++)
1087 // Since the number of rows/columns can change as items are inserted/deleted, we need
1088 // to verify at runtime that the requested growable rows/columns are still valid.
1089 if (m_growableRows
[idx
] >= nrows
)
1091 // If all items in a row/column are hidden, that row/column will have a dimension of -1.
1092 // This causes the row/column to be hidden completely.
1093 if (m_rowHeights
[ m_growableRows
[idx
] ] == -1)
1095 sum_proportions
+= m_growableRowsProportions
[idx
];
1096 growable_space
+= m_rowHeights
[ m_growableRows
[idx
] ];
1102 for (idx
= 0; idx
< m_growableRows
.GetCount(); idx
++)
1104 if (m_growableRows
[idx
] >= nrows
)
1106 if (m_rowHeights
[ m_growableRows
[idx
] ] == -1)
1107 m_rowHeights
[ m_growableRows
[idx
] ] = 0;
1110 int delta
= (sz
.y
- minsz
.y
);
1111 if (sum_proportions
== 0)
1112 delta
= (delta
/num
) + m_rowHeights
[ m_growableRows
[idx
] ];
1114 delta
= ((delta
+growable_space
)*m_growableRowsProportions
[idx
]) / sum_proportions
;
1115 m_rowHeights
[ m_growableRows
[idx
] ] = delta
;
1120 else if ( (m_growMode
== wxFLEX_GROWMODE_ALL
) && (sz
.y
> minsz
.y
) )
1122 // rounding problem?
1123 for ( int row
= 0; row
< nrows
; ++row
)
1124 m_rowHeights
[ row
] = sz
.y
/ nrows
;
1127 // the same logic as above but for the columns
1128 if ( sz
.x
> minsz
.x
&& ( (m_flexDirection
& wxHORIZONTAL
) || (m_growMode
== wxFLEX_GROWMODE_SPECIFIED
) ) )
1130 int sum_proportions
= 0;
1131 int growable_space
= 0;
1134 for (idx
= 0; idx
< m_growableCols
.GetCount(); idx
++)
1136 // Since the number of rows/columns can change as items are inserted/deleted, we need
1137 // to verify at runtime that the requested growable rows/columns are still valid.
1138 if (m_growableCols
[idx
] >= ncols
)
1140 // If all items in a row/column are hidden, that row/column will have a dimension of -1.
1141 // This causes the column to be hidden completely.
1142 if (m_colWidths
[ m_growableCols
[idx
] ] == -1)
1144 sum_proportions
+= m_growableColsProportions
[idx
];
1145 // wtb 5/12/02 bugfix - was m_ColWidths[idx]!!
1146 growable_space
+= m_colWidths
[ m_growableCols
[idx
] ];
1152 for (idx
= 0; idx
< m_growableCols
.GetCount(); idx
++)
1154 if (m_growableCols
[idx
] >= ncols
)
1156 if (m_colWidths
[ m_growableCols
[idx
] ] == -1)
1157 m_colWidths
[ m_growableCols
[idx
] ] = 0;
1160 int delta
= (sz
.x
- minsz
.x
);
1161 if (sum_proportions
== 0)
1162 delta
= (delta
/num
) + m_colWidths
[ m_growableCols
[idx
] ];
1164 delta
= ((delta
+growable_space
)*m_growableColsProportions
[idx
])/sum_proportions
;
1165 m_colWidths
[ m_growableCols
[idx
] ] = delta
;
1170 else if ( (m_growMode
== wxFLEX_GROWMODE_ALL
) && (sz
.x
> minsz
.x
) )
1172 for ( int col
=0; col
< ncols
; ++col
)
1173 m_colWidths
[ col
] = sz
.x
/ ncols
;
1176 sz
= wxSize( pt
.x
+ sz
.x
, pt
.y
+ sz
.y
);
1179 for (int c
= 0; c
< ncols
; c
++)
1182 for (int r
= 0; r
< nrows
; r
++)
1184 int i
= r
* ncols
+ c
;
1187 wxSizerItemList::compatibility_iterator node
= m_children
.Item( i
);
1189 wxASSERT_MSG( node
, _T("Failed to find node") );
1191 int w
= wxMax( 0, wxMin( m_colWidths
[c
], sz
.x
- x
) );
1192 int h
= wxMax( 0, wxMin( m_rowHeights
[r
], sz
.y
- y
) );
1194 SetItemBounds( node
->GetData(), x
, y
, w
, h
);
1196 y
= y
+ m_rowHeights
[r
] + m_vgap
;
1198 x
= x
+ m_colWidths
[c
] + m_hgap
;
1202 wxSize
wxFlexGridSizer::CalcMin()
1208 // Number of rows/columns can change as items are added or removed.
1209 if ( !CalcRowsCols(nrows
, ncols
) )
1210 return wxSize(10, 10);
1212 m_rowHeights
.SetCount(nrows
);
1213 m_colWidths
.SetCount(ncols
);
1215 // We have to recalcuate the sizes in case an item has wxADJUST_MINSIZE, has changed
1216 // minimum size since the previous layout, or has been hidden using wxSizer::Show().
1217 // If all the items in a row/column are hidden, the final dimension of the row/column
1218 // will be -1, indicating that the column itself is hidden.
1219 for( s
= m_rowHeights
.GetCount(), i
= 0; i
< s
; ++i
)
1220 m_rowHeights
[ i
] = -1;
1221 for( s
= m_colWidths
.GetCount(), i
= 0; i
< s
; ++i
)
1222 m_colWidths
[ i
] = -1;
1224 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
1229 wxSizerItem
*item
= node
->GetData();
1230 if ( item
->IsShown() )
1232 wxSize
sz( item
->CalcMin() );
1233 int row
= i
/ ncols
;
1234 int col
= i
% ncols
;
1236 m_rowHeights
[ row
] = wxMax( wxMax( 0, sz
.y
), m_rowHeights
[ row
] );
1237 m_colWidths
[ col
] = wxMax( wxMax( 0, sz
.x
), m_colWidths
[ col
] );
1240 node
= node
->GetNext();
1244 // the logic above works when we resize flexibly in both directions but
1245 // maybe this is not the case
1246 if ( m_flexDirection
!= wxBOTH
)
1248 // select the array corresponding to the direction in which we do *not*
1250 wxArrayInt
& array
= m_flexDirection
== wxVERTICAL
? m_colWidths
1253 const int count
= array
.GetCount();
1255 // find the largest value in this array
1257 for ( n
= 0; n
< count
; ++n
)
1259 if ( array
[n
] > largest
)
1263 // and now fill it with the largest value
1264 for ( n
= 0; n
< count
; ++n
)
1270 // Sum total minimum size, including gaps between rows/columns.
1271 // -1 is used as a magic number meaning empty column.
1273 for (int col
= 0; col
< ncols
; col
++)
1274 if ( m_colWidths
[ col
] != -1 )
1275 width
+= m_colWidths
[ col
] + ( col
== ncols
-1 ? 0 : m_hgap
);
1278 for (int row
= 0; row
< nrows
; row
++)
1279 if ( m_rowHeights
[ row
] != -1 )
1280 height
+= m_rowHeights
[ row
] + ( row
== nrows
-1 ? 0 : m_vgap
);
1282 return wxSize( width
, height
);
1285 void wxFlexGridSizer::AddGrowableRow( size_t idx
, int proportion
)
1287 m_growableRows
.Add( idx
);
1288 m_growableRowsProportions
.Add( proportion
);
1291 void wxFlexGridSizer::RemoveGrowableRow( size_t WXUNUSED(idx
) )
1295 void wxFlexGridSizer::AddGrowableCol( size_t idx
, int proportion
)
1297 m_growableCols
.Add( idx
);
1298 m_growableColsProportions
.Add( proportion
);
1301 void wxFlexGridSizer::RemoveGrowableCol( size_t WXUNUSED(idx
) )
1305 //---------------------------------------------------------------------------
1307 //---------------------------------------------------------------------------
1309 wxBoxSizer::wxBoxSizer( int orient
)
1310 : m_orient( orient
)
1314 void wxBoxSizer::RecalcSizes()
1316 if (m_children
.GetCount() == 0)
1323 if (m_orient
== wxHORIZONTAL
)
1325 delta
= (m_size
.x
- m_fixedWidth
) / m_stretchable
;
1326 extra
= (m_size
.x
- m_fixedWidth
) % m_stretchable
;
1330 delta
= (m_size
.y
- m_fixedHeight
) / m_stretchable
;
1331 extra
= (m_size
.y
- m_fixedHeight
) % m_stretchable
;
1335 wxPoint
pt( m_position
);
1337 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
1340 wxSizerItem
*item
= node
->GetData();
1342 if (item
->IsShown())
1345 if (item
->GetProportion())
1346 weight
= item
->GetProportion();
1348 wxSize
size( item
->CalcMin() );
1350 if (m_orient
== wxVERTICAL
)
1352 wxCoord height
= size
.y
;
1353 if (item
->GetProportion())
1355 height
= (delta
* weight
) + extra
;
1356 extra
= 0; // only the first item will get the remainder as extra size
1359 wxPoint
child_pos( pt
);
1360 wxSize
child_size( wxSize( size
.x
, height
) );
1362 if (item
->GetFlag() & (wxEXPAND
| wxSHAPED
))
1363 child_size
.x
= m_size
.x
;
1364 else if (item
->GetFlag() & wxALIGN_RIGHT
)
1365 child_pos
.x
+= m_size
.x
- size
.x
;
1366 else if (item
->GetFlag() & (wxCENTER
| wxALIGN_CENTER_HORIZONTAL
))
1367 // XXX wxCENTER is added for backward compatibility;
1368 // wxALIGN_CENTER should be used in new code
1369 child_pos
.x
+= (m_size
.x
- size
.x
) / 2;
1371 item
->SetDimension( child_pos
, child_size
);
1377 wxCoord width
= size
.x
;
1378 if (item
->GetProportion())
1380 width
= (delta
* weight
) + extra
;
1381 extra
= 0; // only the first item will get the remainder as extra size
1384 wxPoint
child_pos( pt
);
1385 wxSize
child_size( wxSize(width
, size
.y
) );
1387 if (item
->GetFlag() & (wxEXPAND
| wxSHAPED
))
1388 child_size
.y
= m_size
.y
;
1389 else if (item
->GetFlag() & wxALIGN_BOTTOM
)
1390 child_pos
.y
+= m_size
.y
- size
.y
;
1391 else if (item
->GetFlag() & (wxCENTER
| wxALIGN_CENTER_VERTICAL
))
1392 // XXX wxCENTER is added for backward compatibility;
1393 // wxALIGN_CENTER should be used in new code
1394 child_pos
.y
+= (m_size
.y
- size
.y
) / 2;
1396 item
->SetDimension( child_pos
, child_size
);
1402 node
= node
->GetNext();
1406 wxSize
wxBoxSizer::CalcMin()
1408 if (m_children
.GetCount() == 0)
1409 return wxSize(10,10);
1417 // Find how long each stretch unit needs to be
1418 int stretchSize
= 1;
1419 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
1423 wxSizerItem
*item
= node
->GetData();
1425 if (item
->IsShown() && item
->GetProportion() != 0)
1427 int stretch
= item
->GetProportion();
1428 wxSize
size( item
->CalcMin() );
1430 // Integer division rounded up is (a + b - 1) / b
1431 if (m_orient
== wxHORIZONTAL
)
1432 sizePerStretch
= ( size
.x
+ stretch
- 1 ) / stretch
;
1434 sizePerStretch
= ( size
.y
+ stretch
- 1 ) / stretch
;
1435 if (sizePerStretch
> stretchSize
)
1436 stretchSize
= sizePerStretch
;
1438 node
= node
->GetNext();
1441 // Calculate overall minimum size
1442 node
= m_children
.GetFirst();
1445 wxSizerItem
*item
= node
->GetData();
1447 if (item
->IsShown())
1449 m_stretchable
+= item
->GetProportion();
1451 wxSize
size( item
->CalcMin() );
1452 if (item
->GetProportion() != 0)
1454 if (m_orient
== wxHORIZONTAL
)
1455 size
.x
= stretchSize
* item
->GetProportion();
1457 size
.y
= stretchSize
* item
->GetProportion();
1460 if (m_orient
== wxHORIZONTAL
)
1462 m_minWidth
+= size
.x
;
1463 m_minHeight
= wxMax( m_minHeight
, size
.y
);
1467 m_minHeight
+= size
.y
;
1468 m_minWidth
= wxMax( m_minWidth
, size
.x
);
1471 if (item
->GetProportion() == 0)
1473 if (m_orient
== wxVERTICAL
)
1475 m_fixedHeight
+= size
.y
;
1476 m_fixedWidth
= wxMax( m_fixedWidth
, size
.x
);
1480 m_fixedWidth
+= size
.x
;
1481 m_fixedHeight
= wxMax( m_fixedHeight
, size
.y
);
1485 node
= node
->GetNext();
1488 return wxSize( m_minWidth
, m_minHeight
);
1491 //---------------------------------------------------------------------------
1493 //---------------------------------------------------------------------------
1497 wxStaticBoxSizer::wxStaticBoxSizer( wxStaticBox
*box
, int orient
)
1498 : wxBoxSizer( orient
)
1499 , m_staticBox( box
)
1501 wxASSERT_MSG( box
, wxT("wxStaticBoxSizer needs a static box") );
1504 static void GetStaticBoxBorders( wxStaticBox
*box
,
1508 // this has to be done platform by platform as there is no way to
1509 // guess the thickness of a wxStaticBox border
1511 box
->GetBordersForSizer(borderTop
,borderOther
);
1512 #else // __WXCOCOA__
1514 if ( box
->GetLabel().IsEmpty() )
1518 *borderTop
= box
->GetCharHeight();
1521 #endif // __WXCOCOA__
1524 void wxStaticBoxSizer::RecalcSizes()
1526 int top_border
, other_border
;
1527 GetStaticBoxBorders(m_staticBox
, &top_border
, &other_border
);
1529 m_staticBox
->SetSize( m_position
.x
, m_position
.y
, m_size
.x
, m_size
.y
);
1531 wxPoint
old_pos( m_position
);
1532 m_position
.x
+= other_border
;
1533 m_position
.y
+= top_border
;
1534 wxSize
old_size( m_size
);
1535 m_size
.x
-= 2*other_border
;
1536 m_size
.y
-= top_border
+ other_border
;
1538 wxBoxSizer::RecalcSizes();
1540 m_position
= old_pos
;
1544 wxSize
wxStaticBoxSizer::CalcMin()
1546 int top_border
, other_border
;
1547 GetStaticBoxBorders(m_staticBox
, &top_border
, &other_border
);
1549 wxSize
ret( wxBoxSizer::CalcMin() );
1550 ret
.x
+= 2*other_border
;
1551 ret
.y
+= other_border
+ top_border
;
1556 #endif // wxUSE_STATBOX
1558 //---------------------------------------------------------------------------
1560 //---------------------------------------------------------------------------
1564 wxNotebookSizer::wxNotebookSizer( wxNotebook
*nb
)
1567 wxASSERT_MSG( nb
, wxT("wxNotebookSizer needs a notebook") );
1570 void wxNotebookSizer::RecalcSizes()
1572 m_notebook
->SetSize( m_position
.x
, m_position
.y
, m_size
.x
, m_size
.y
);
1575 wxSize
wxNotebookSizer::CalcMin()
1577 wxSize sizeBorder
= m_notebook
->CalcSizeFromPage(wxSize(0, 0));
1582 if (m_notebook
->GetChildren().GetCount() == 0)
1584 return wxSize(sizeBorder
.x
+ 10, sizeBorder
.y
+ 10);
1590 wxWindowList::compatibility_iterator node
= m_notebook
->GetChildren().GetFirst();
1593 wxWindow
*item
= node
->GetData();
1594 wxSizer
*itemsizer
= item
->GetSizer();
1598 wxSize
subsize( itemsizer
->CalcMin() );
1600 if (subsize
.x
> maxX
)
1602 if (subsize
.y
> maxY
)
1606 node
= node
->GetNext();
1609 return wxSize( maxX
, maxY
) + sizeBorder
;
1612 #endif // wxUSE_NOTEBOOK