1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: provide new wxSizer class for layout
4 // Author: Robert Roebling and Robin Dunn, contributions by
5 // Dirk Holtwick, Ron Lee
6 // Modified by: Ron Lee
9 // Copyright: (c) Robin Dunn, Robert Roebling
10 // Licence: wxWindows licence
11 /////////////////////////////////////////////////////////////////////////////
13 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
14 #pragma implementation "sizer.h"
17 // For compilers that support precompilation, includes "wx.h".
18 #include "wx/wxprec.h"
26 #include "wx/statbox.h"
27 #include "wx/listimpl.cpp"
28 #if WXWIN_COMPATIBILITY_2_4
29 #include "wx/notebook.h"
33 # include "wx/mac/uma.h"
36 //---------------------------------------------------------------------------
38 IMPLEMENT_CLASS(wxSizerItem
, wxObject
)
39 IMPLEMENT_CLASS(wxSizer
, wxObject
)
40 IMPLEMENT_CLASS(wxGridSizer
, wxSizer
)
41 IMPLEMENT_CLASS(wxFlexGridSizer
, wxGridSizer
)
42 IMPLEMENT_CLASS(wxBoxSizer
, wxSizer
)
44 IMPLEMENT_CLASS(wxStaticBoxSizer
, wxBoxSizer
)
47 WX_DEFINE_EXPORTED_LIST( wxSizerItemList
);
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_proportion( proportion
)
106 , m_userData( userData
)
108 if (flag
& wxFIXED_MINSIZE
)
109 window
->SetMinSize(window
->GetSize());
110 m_minSize
= window
->GetSize();
112 // aspect ratio calculated from initial size
113 SetRatio( m_minSize
);
115 // m_size is calculated later
118 wxSizerItem::wxSizerItem( wxSizer
*sizer
, int proportion
, int flag
, int border
, wxObject
* userData
)
121 , m_proportion( proportion
)
126 , m_userData( userData
)
128 // m_minSize is calculated later
129 // m_size is calculated later
132 wxSizerItem::wxSizerItem()
144 wxSizerItem::~wxSizerItem()
150 m_window
->SetContainingSizer(NULL
);
152 else // we must be a sizer
159 wxSize
wxSizerItem::GetSize() const
163 ret
= m_sizer
->GetSize();
166 ret
= m_window
->GetSize();
173 if (m_flag
& wxNORTH
)
175 if (m_flag
& wxSOUTH
)
181 wxSize
wxSizerItem::CalcMin()
185 m_minSize
= m_sizer
->GetMinSize();
187 // if we have to preserve aspect ratio _AND_ this is
188 // the first-time calculation, consider ret to be initial size
189 if ((m_flag
& wxSHAPED
) && !m_ratio
)
192 else if ( IsWindow() )
194 // Since the size of the window may change during runtime, we
195 // should use the current minimal/best size.
196 m_minSize
= m_window
->GetBestFittingSize();
199 return GetMinSizeWithBorder();
202 wxSize
wxSizerItem::GetMinSizeWithBorder() const
204 wxSize ret
= m_minSize
;
210 if (m_flag
& wxNORTH
)
212 if (m_flag
& wxSOUTH
)
219 void wxSizerItem::SetDimension( wxPoint pos
, wxSize size
)
221 if (m_flag
& wxSHAPED
)
223 // adjust aspect ratio
224 int rwidth
= (int) (size
.y
* m_ratio
);
228 int rheight
= (int) (size
.x
/ m_ratio
);
229 // add vertical space
230 if (m_flag
& wxALIGN_CENTER_VERTICAL
)
231 pos
.y
+= (size
.y
- rheight
) / 2;
232 else if (m_flag
& wxALIGN_BOTTOM
)
233 pos
.y
+= (size
.y
- rheight
);
234 // use reduced dimensions
237 else if (rwidth
< size
.x
)
239 // add horizontal space
240 if (m_flag
& wxALIGN_CENTER_HORIZONTAL
)
241 pos
.x
+= (size
.x
- rwidth
) / 2;
242 else if (m_flag
& wxALIGN_RIGHT
)
243 pos
.x
+= (size
.x
- rwidth
);
248 // This is what GetPosition() returns. Since we calculate
249 // borders afterwards, GetPosition() will be the left/top
250 // corner of the surrounding border.
262 if (m_flag
& wxNORTH
)
267 if (m_flag
& wxSOUTH
)
273 m_sizer
->SetDimension( pos
.x
, pos
.y
, size
.x
, size
.y
);
276 m_window
->SetSize( pos
.x
, pos
.y
, size
.x
, size
.y
, wxSIZE_ALLOW_MINUS_ONE
);
281 void wxSizerItem::DeleteWindows()
290 m_sizer
->DeleteWindows();
293 bool wxSizerItem::IsWindow() const
295 return (m_window
!= NULL
);
298 bool wxSizerItem::IsSizer() const
300 return (m_sizer
!= NULL
);
303 bool wxSizerItem::IsSpacer() const
305 return (m_window
== NULL
) && (m_sizer
== NULL
);
308 void wxSizerItem::Show( bool show
)
313 m_window
->Show( show
);
315 m_sizer
->ShowItems( show
);
317 // ... nothing else to do to hide/show spacers
320 void wxSizerItem::SetOption( int option
)
322 SetProportion( option
);
325 int wxSizerItem::GetOption() const
327 return GetProportion();
331 //---------------------------------------------------------------------------
333 //---------------------------------------------------------------------------
336 : m_minSize( wxSize( 0, 0 ) )
342 WX_CLEAR_LIST(wxSizerItemList
, m_children
);
345 void wxSizer::Insert( size_t index
, wxSizerItem
*item
)
347 m_children
.Insert( index
, item
);
349 if( item
->GetWindow() )
350 item
->GetWindow()->SetContainingSizer( this );
353 bool wxSizer::Remove( wxWindow
*window
)
355 return Detach( window
);
358 bool wxSizer::Remove( wxSizer
*sizer
)
360 wxASSERT_MSG( sizer
, _T("Removing NULL sizer") );
362 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
365 wxSizerItem
*item
= node
->GetData();
367 if (item
->GetSizer() == sizer
)
370 m_children
.Erase( node
);
374 node
= node
->GetNext();
380 bool wxSizer::Remove( int index
)
382 wxCHECK_MSG( index
>= 0 && (size_t)index
< m_children
.GetCount(),
384 _T("Remove index is out of range") );
386 wxSizerItemList::compatibility_iterator node
= m_children
.Item( index
);
388 wxCHECK_MSG( node
, false, _T("Failed to find child node") );
390 wxSizerItem
*item
= node
->GetData();
392 if( item
->IsWindow() )
393 item
->GetWindow()->SetContainingSizer( NULL
);
396 m_children
.Erase( node
);
400 bool wxSizer::Detach( wxSizer
*sizer
)
402 wxASSERT_MSG( sizer
, _T("Detaching NULL sizer") );
404 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
407 wxSizerItem
*item
= node
->GetData();
409 if (item
->GetSizer() == sizer
)
413 m_children
.Erase( node
);
416 node
= node
->GetNext();
422 bool wxSizer::Detach( wxWindow
*window
)
424 wxASSERT_MSG( window
, _T("Detaching NULL window") );
426 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
429 wxSizerItem
*item
= node
->GetData();
431 if (item
->GetWindow() == window
)
433 item
->GetWindow()->SetContainingSizer( NULL
);
435 m_children
.Erase( node
);
438 node
= node
->GetNext();
444 bool wxSizer::Detach( int index
)
446 wxCHECK_MSG( index
>= 0 && (size_t)index
< m_children
.GetCount(),
448 _T("Detach index is out of range") );
450 wxSizerItemList::compatibility_iterator node
= m_children
.Item( index
);
452 wxCHECK_MSG( node
, false, _T("Failed to find child node") );
454 wxSizerItem
*item
= node
->GetData();
456 if( item
->IsSizer() )
458 else if( item
->IsWindow() )
459 item
->GetWindow()->SetContainingSizer( NULL
);
462 m_children
.Erase( node
);
466 void wxSizer::Clear( bool delete_windows
)
468 // First clear the ContainingSizer pointers
469 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
472 wxSizerItem
*item
= node
->GetData();
474 if (item
->IsWindow())
475 item
->GetWindow()->SetContainingSizer( NULL
);
476 node
= node
->GetNext();
479 // Destroy the windows if needed
483 // Now empty the list
484 WX_CLEAR_LIST(wxSizerItemList
, m_children
);
487 void wxSizer::DeleteWindows()
489 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
492 wxSizerItem
*item
= node
->GetData();
494 item
->DeleteWindows();
495 node
= node
->GetNext();
499 wxSize
wxSizer::Fit( wxWindow
*window
)
501 wxSize
size(window
->IsTopLevel() ? FitSize(window
)
502 : GetMinWindowSize(window
));
504 window
->SetSize( size
);
509 void wxSizer::FitInside( wxWindow
*window
)
512 if (window
->IsTopLevel())
513 size
= VirtualFitSize( window
);
515 size
= GetMinClientSize( window
);
517 window
->SetVirtualSize( size
);
520 void wxSizer::Layout()
522 // (re)calculates minimums needed for each item and other preparations
526 // Applies the layout and repositions/resizes the items
530 void wxSizer::SetSizeHints( wxWindow
*window
)
532 // Preserve the window's max size hints, but set the
533 // lower bound according to the sizer calculations.
535 wxSize size
= Fit( window
);
537 window
->SetSizeHints( size
.x
,
539 window
->GetMaxWidth(),
540 window
->GetMaxHeight() );
543 void wxSizer::SetVirtualSizeHints( wxWindow
*window
)
545 // Preserve the window's max size hints, but set the
546 // lower bound according to the sizer calculations.
549 wxSize
size( window
->GetVirtualSize() );
550 window
->SetVirtualSizeHints( size
.x
,
552 window
->GetMaxWidth(),
553 window
->GetMaxHeight() );
556 wxSize
wxSizer::GetMaxWindowSize( wxWindow
*window
) const
558 return window
->GetMaxSize();
561 wxSize
wxSizer::GetMinWindowSize( wxWindow
*window
)
563 wxSize
minSize( GetMinSize() );
564 wxSize
size( window
->GetSize() );
565 wxSize
client_size( window
->GetClientSize() );
567 return wxSize( minSize
.x
+size
.x
-client_size
.x
,
568 minSize
.y
+size
.y
-client_size
.y
);
571 // TODO on mac we need a function that determines how much free space this
572 // min size contains, in order to make sure that we have 20 pixels of free
573 // space around the controls
575 // Return a window size that will fit within the screens dimensions
576 wxSize
wxSizer::FitSize( wxWindow
*window
)
578 wxSize size
= GetMinWindowSize( window
);
579 wxSize sizeMax
= GetMaxWindowSize( window
);
581 // Limit the size if sizeMax != wxDefaultSize
583 if ( size
.x
> sizeMax
.x
&& sizeMax
.x
!= -1 )
585 if ( size
.y
> sizeMax
.y
&& sizeMax
.y
!= -1 )
591 wxSize
wxSizer::GetMaxClientSize( wxWindow
*window
) const
593 wxSize
maxSize( window
->GetMaxSize() );
595 if( maxSize
!= wxDefaultSize
)
597 wxSize
size( window
->GetSize() );
598 wxSize
client_size( window
->GetClientSize() );
600 return wxSize( maxSize
.x
+ client_size
.x
- size
.x
,
601 maxSize
.y
+ client_size
.y
- size
.y
);
604 return wxDefaultSize
;
607 wxSize
wxSizer::GetMinClientSize( wxWindow
*WXUNUSED(window
) )
609 return GetMinSize(); // Already returns client size.
612 wxSize
wxSizer::VirtualFitSize( wxWindow
*window
)
614 wxSize size
= GetMinClientSize( window
);
615 wxSize sizeMax
= GetMaxClientSize( window
);
617 // Limit the size if sizeMax != wxDefaultSize
619 if ( size
.x
> sizeMax
.x
&& sizeMax
.x
!= -1 )
621 if ( size
.y
> sizeMax
.y
&& sizeMax
.y
!= -1 )
627 void wxSizer::SetDimension( int x
, int y
, int width
, int height
)
636 wxSize
wxSizer::GetMinSize()
638 wxSize
ret( CalcMin() );
639 if (ret
.x
< m_minSize
.x
) ret
.x
= m_minSize
.x
;
640 if (ret
.y
< m_minSize
.y
) ret
.y
= m_minSize
.y
;
644 void wxSizer::DoSetMinSize( int width
, int height
)
647 m_minSize
.y
= height
;
650 bool wxSizer::DoSetItemMinSize( wxWindow
*window
, int width
, int height
)
652 wxASSERT_MSG( window
, _T("SetMinSize for NULL window") );
654 // Is it our immediate child?
656 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
659 wxSizerItem
*item
= node
->GetData();
661 if (item
->GetWindow() == window
)
663 item
->SetMinSize( width
, height
);
666 node
= node
->GetNext();
669 // No? Search any subsizers we own then
671 node
= m_children
.GetFirst();
674 wxSizerItem
*item
= node
->GetData();
676 if ( item
->GetSizer() &&
677 item
->GetSizer()->DoSetItemMinSize( window
, width
, height
) )
679 // A child sizer found the requested windw, exit.
682 node
= node
->GetNext();
688 bool wxSizer::DoSetItemMinSize( wxSizer
*sizer
, int width
, int height
)
690 wxASSERT_MSG( sizer
, _T("SetMinSize for NULL sizer") );
692 // Is it our immediate child?
694 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
697 wxSizerItem
*item
= node
->GetData();
699 if (item
->GetSizer() == sizer
)
701 item
->GetSizer()->DoSetMinSize( width
, height
);
704 node
= node
->GetNext();
707 // No? Search any subsizers we own then
709 node
= m_children
.GetFirst();
712 wxSizerItem
*item
= node
->GetData();
714 if ( item
->GetSizer() &&
715 item
->GetSizer()->DoSetItemMinSize( sizer
, width
, height
) )
717 // A child found the requested sizer, exit.
720 node
= node
->GetNext();
726 bool wxSizer::DoSetItemMinSize( size_t index
, int width
, int height
)
728 wxSizerItemList::compatibility_iterator node
= m_children
.Item( index
);
730 wxCHECK_MSG( node
, false, _T("Failed to find child node") );
732 wxSizerItem
*item
= node
->GetData();
734 if (item
->GetSizer())
736 // Sizers contains the minimal size in them, if not calculated ...
737 item
->GetSizer()->DoSetMinSize( width
, height
);
741 // ... but the minimal size of spacers and windows is stored via the item
742 item
->SetMinSize( width
, height
);
748 bool wxSizer::Show( wxWindow
*window
, bool show
, bool recursive
)
750 wxASSERT_MSG( window
, _T("Show for NULL window") );
752 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
755 wxSizerItem
*item
= node
->GetData();
757 if (item
->GetWindow() == window
)
763 else if (recursive
&& item
->IsSizer())
765 if (item
->GetSizer()->Show(window
, show
, recursive
))
769 node
= node
->GetNext();
775 bool wxSizer::Show( wxSizer
*sizer
, bool show
, bool recursive
)
777 wxASSERT_MSG( sizer
, _T("Show for NULL sizer") );
779 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
782 wxSizerItem
*item
= node
->GetData();
784 if (item
->GetSizer() == sizer
)
790 else if (recursive
&& item
->IsSizer())
792 if (item
->GetSizer()->Show(sizer
, show
, recursive
))
796 node
= node
->GetNext();
802 bool wxSizer::Show( size_t index
, bool show
)
804 wxCHECK_MSG( index
< m_children
.GetCount(),
806 _T("Show index is out of range") );
808 m_children
.Item( index
)->GetData()->Show( show
);
813 void wxSizer::ShowItems( bool show
)
815 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
818 node
->GetData()->Show( show
);
819 node
= node
->GetNext();
823 bool wxSizer::IsShown( wxWindow
*window
) const
825 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
828 wxSizerItem
*item
= node
->GetData();
830 if (item
->GetWindow() == window
)
832 return item
->IsShown();
834 node
= node
->GetNext();
837 wxFAIL_MSG( _T("IsShown failed to find sizer item") );
842 bool wxSizer::IsShown( wxSizer
*sizer
) const
844 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
847 wxSizerItem
*item
= node
->GetData();
849 if (item
->GetSizer() == sizer
)
851 return item
->IsShown();
853 node
= node
->GetNext();
856 wxFAIL_MSG( _T("IsShown failed to find sizer item") );
861 bool wxSizer::IsShown( size_t index
) const
863 wxCHECK_MSG( index
< m_children
.GetCount(),
865 _T("IsShown index is out of range") );
867 return m_children
.Item( index
)->GetData()->IsShown();
871 //---------------------------------------------------------------------------
873 //---------------------------------------------------------------------------
875 wxGridSizer::wxGridSizer( int rows
, int cols
, int vgap
, int hgap
)
881 if (m_rows
== 0 && m_cols
== 0)
885 wxGridSizer::wxGridSizer( int cols
, int vgap
, int hgap
)
891 if (m_rows
== 0 && m_cols
== 0)
895 int wxGridSizer::CalcRowsCols(int& nrows
, int& ncols
) const
897 int nitems
= m_children
.GetCount();
903 nrows
= (nitems
+ m_cols
- 1) / m_cols
;
907 ncols
= (nitems
+ m_rows
- 1) / m_rows
;
910 else // 0 columns, 0 rows?
912 wxFAIL_MSG( _T("grid sizer must have either rows or columns fixed") );
921 void wxGridSizer::RecalcSizes()
923 int nitems
, nrows
, ncols
;
924 if ( (nitems
= CalcRowsCols(nrows
, ncols
)) == 0 )
927 wxSize
sz( GetSize() );
928 wxPoint
pt( GetPosition() );
930 int w
= (sz
.x
- (ncols
- 1) * m_hgap
) / ncols
;
931 int h
= (sz
.y
- (nrows
- 1) * m_vgap
) / nrows
;
934 for (int c
= 0; c
< ncols
; c
++)
937 for (int r
= 0; r
< nrows
; r
++)
939 int i
= r
* ncols
+ c
;
942 wxSizerItemList::compatibility_iterator node
= m_children
.Item( i
);
944 wxASSERT_MSG( node
, _T("Failed to find SizerItemList node") );
946 SetItemBounds( node
->GetData(), x
, y
, w
, h
);
954 wxSize
wxGridSizer::CalcMin()
957 if ( CalcRowsCols(nrows
, ncols
) == 0 )
958 return wxSize(10, 10);
960 // Find the max width and height for any component
964 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
967 wxSizerItem
*item
= node
->GetData();
968 wxSize
sz( item
->CalcMin() );
970 w
= wxMax( w
, sz
.x
);
971 h
= wxMax( h
, sz
.y
);
973 node
= node
->GetNext();
976 return wxSize( ncols
* w
+ (ncols
-1) * m_hgap
,
977 nrows
* h
+ (nrows
-1) * m_vgap
);
980 void wxGridSizer::SetItemBounds( wxSizerItem
*item
, int x
, int y
, int w
, int h
)
983 wxSize
sz( item
->GetMinSizeWithBorder() );
984 int flag
= item
->GetFlag();
986 if ((flag
& wxEXPAND
) || (flag
& wxSHAPED
))
992 if (flag
& wxALIGN_CENTER_HORIZONTAL
)
994 pt
.x
= x
+ (w
- sz
.x
) / 2;
996 else if (flag
& wxALIGN_RIGHT
)
998 pt
.x
= x
+ (w
- sz
.x
);
1001 if (flag
& wxALIGN_CENTER_VERTICAL
)
1003 pt
.y
= y
+ (h
- sz
.y
) / 2;
1005 else if (flag
& wxALIGN_BOTTOM
)
1007 pt
.y
= y
+ (h
- sz
.y
);
1011 item
->SetDimension(pt
, sz
);
1014 //---------------------------------------------------------------------------
1016 //---------------------------------------------------------------------------
1018 wxFlexGridSizer::wxFlexGridSizer( int rows
, int cols
, int vgap
, int hgap
)
1019 : wxGridSizer( rows
, cols
, vgap
, hgap
),
1020 m_flexDirection(wxBOTH
),
1021 m_growMode(wxFLEX_GROWMODE_SPECIFIED
)
1025 wxFlexGridSizer::wxFlexGridSizer( int cols
, int vgap
, int hgap
)
1026 : wxGridSizer( cols
, vgap
, hgap
),
1027 m_flexDirection(wxBOTH
),
1028 m_growMode(wxFLEX_GROWMODE_SPECIFIED
)
1032 wxFlexGridSizer::~wxFlexGridSizer()
1036 void wxFlexGridSizer::RecalcSizes()
1038 int nitems
, nrows
, ncols
;
1039 if ( (nitems
= CalcRowsCols(nrows
, ncols
)) == 0 )
1042 wxPoint
pt( GetPosition() );
1043 wxSize
sz( GetSize() );
1045 AdjustForGrowables(sz
, m_calculatedMinSize
, nrows
, ncols
);
1047 sz
= wxSize( pt
.x
+ sz
.x
, pt
.y
+ sz
.y
);
1050 for (int c
= 0; c
< ncols
; c
++)
1053 for (int r
= 0; r
< nrows
; r
++)
1055 int i
= r
* ncols
+ c
;
1058 wxSizerItemList::compatibility_iterator node
= m_children
.Item( i
);
1060 wxASSERT_MSG( node
, _T("Failed to find node") );
1062 int w
= wxMax( 0, wxMin( m_colWidths
[c
], sz
.x
- x
) );
1063 int h
= wxMax( 0, wxMin( m_rowHeights
[r
], sz
.y
- y
) );
1065 SetItemBounds( node
->GetData(), x
, y
, w
, h
);
1067 y
= y
+ m_rowHeights
[r
] + m_vgap
;
1069 x
= x
+ m_colWidths
[c
] + m_hgap
;
1073 wxSize
wxFlexGridSizer::CalcMin()
1079 // Number of rows/columns can change as items are added or removed.
1080 if ( !CalcRowsCols(nrows
, ncols
) )
1081 return wxSize(10, 10);
1083 m_rowHeights
.SetCount(nrows
);
1084 m_colWidths
.SetCount(ncols
);
1086 // We have to recalcuate the sizes in case the item minimum size has
1087 // changed since the previous layout, or the item has been hidden using
1088 // wxSizer::Show(). If all the items in a row/column are hidden, the final
1089 // dimension of the row/column will be -1, indicating that the column
1090 // itself is hidden.
1091 for( s
= m_rowHeights
.GetCount(), i
= 0; i
< s
; ++i
)
1092 m_rowHeights
[ i
] = -1;
1093 for( s
= m_colWidths
.GetCount(), i
= 0; i
< s
; ++i
)
1094 m_colWidths
[ i
] = -1;
1096 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
1101 wxSizerItem
*item
= node
->GetData();
1102 if ( item
->IsShown() )
1104 wxSize
sz( item
->CalcMin() );
1105 int row
= i
/ ncols
;
1106 int col
= i
% ncols
;
1108 m_rowHeights
[ row
] = wxMax( wxMax( 0, sz
.y
), m_rowHeights
[ row
] );
1109 m_colWidths
[ col
] = wxMax( wxMax( 0, sz
.x
), m_colWidths
[ col
] );
1112 node
= node
->GetNext();
1116 AdjustForFlexDirection();
1118 // Sum total minimum size, including gaps between rows/columns.
1119 // -1 is used as a magic number meaning empty column.
1121 for (int col
= 0; col
< ncols
; col
++)
1122 if ( m_colWidths
[ col
] != -1 )
1123 width
+= m_colWidths
[ col
] + ( col
== ncols
-1 ? 0 : m_hgap
);
1126 for (int row
= 0; row
< nrows
; row
++)
1127 if ( m_rowHeights
[ row
] != -1 )
1128 height
+= m_rowHeights
[ row
] + ( row
== nrows
-1 ? 0 : m_vgap
);
1130 m_calculatedMinSize
= wxSize( width
, height
);
1131 return m_calculatedMinSize
;
1134 void wxFlexGridSizer::AdjustForFlexDirection()
1136 // the logic in CalcMin works when we resize flexibly in both directions
1137 // but maybe this is not the case
1138 if ( m_flexDirection
!= wxBOTH
)
1140 // select the array corresponding to the direction in which we do *not*
1142 wxArrayInt
& array
= m_flexDirection
== wxVERTICAL
? m_colWidths
1145 const int count
= array
.GetCount();
1147 // find the largest value in this array
1149 for ( n
= 0; n
< count
; ++n
)
1151 if ( array
[n
] > largest
)
1155 // and now fill it with the largest value
1156 for ( n
= 0; n
< count
; ++n
)
1164 void wxFlexGridSizer::AdjustForGrowables(const wxSize
& sz
, const wxSize
& minsz
,
1165 int nrows
, int ncols
)
1167 // what to do with the rows? by default, resize them proportionally
1168 if ( sz
.y
> minsz
.y
&& ( (m_flexDirection
& wxVERTICAL
) || (m_growMode
== wxFLEX_GROWMODE_SPECIFIED
) ) )
1170 int sum_proportions
= 0;
1171 int growable_space
= 0;
1174 for (idx
= 0; idx
< m_growableRows
.GetCount(); idx
++)
1176 // Since the number of rows/columns can change as items are
1177 // inserted/deleted, we need to verify at runtime that the
1178 // requested growable rows/columns are still valid.
1179 if (m_growableRows
[idx
] >= nrows
)
1182 // If all items in a row/column are hidden, that row/column will
1183 // have a dimension of -1. This causes the row/column to be
1184 // hidden completely.
1185 if (m_rowHeights
[ m_growableRows
[idx
] ] == -1)
1187 sum_proportions
+= m_growableRowsProportions
[idx
];
1188 growable_space
+= m_rowHeights
[ m_growableRows
[idx
] ];
1194 for (idx
= 0; idx
< m_growableRows
.GetCount(); idx
++)
1196 if (m_growableRows
[idx
] >= nrows
)
1198 if (m_rowHeights
[ m_growableRows
[idx
] ] == -1)
1199 m_rowHeights
[ m_growableRows
[idx
] ] = 0;
1202 int delta
= (sz
.y
- minsz
.y
);
1203 if (sum_proportions
== 0)
1204 delta
= (delta
/num
) + m_rowHeights
[ m_growableRows
[idx
] ];
1206 delta
= ((delta
+growable_space
)*m_growableRowsProportions
[idx
]) / sum_proportions
;
1207 m_rowHeights
[ m_growableRows
[idx
] ] = delta
;
1212 else if ( (m_growMode
== wxFLEX_GROWMODE_ALL
) && (sz
.y
> minsz
.y
) )
1214 // rounding problem?
1215 for ( int row
= 0; row
< nrows
; ++row
)
1216 m_rowHeights
[ row
] = sz
.y
/ nrows
;
1219 // the same logic as above but for the columns
1220 if ( sz
.x
> minsz
.x
&& ( (m_flexDirection
& wxHORIZONTAL
) || (m_growMode
== wxFLEX_GROWMODE_SPECIFIED
) ) )
1222 int sum_proportions
= 0;
1223 int growable_space
= 0;
1226 for (idx
= 0; idx
< m_growableCols
.GetCount(); idx
++)
1228 // Since the number of rows/columns can change as items are
1229 // inserted/deleted, we need to verify at runtime that the
1230 // requested growable rows/columns are still valid.
1231 if (m_growableCols
[idx
] >= ncols
)
1234 // If all items in a row/column are hidden, that row/column will
1235 // have a dimension of -1. This causes the column to be hidden
1237 if (m_colWidths
[ m_growableCols
[idx
] ] == -1)
1239 sum_proportions
+= m_growableColsProportions
[idx
];
1240 growable_space
+= m_colWidths
[ m_growableCols
[idx
] ];
1246 for (idx
= 0; idx
< m_growableCols
.GetCount(); idx
++)
1248 if (m_growableCols
[idx
] >= ncols
)
1250 if (m_colWidths
[ m_growableCols
[idx
] ] == -1)
1251 m_colWidths
[ m_growableCols
[idx
] ] = 0;
1254 int delta
= (sz
.x
- minsz
.x
);
1255 if (sum_proportions
== 0)
1256 delta
= (delta
/num
) + m_colWidths
[ m_growableCols
[idx
] ];
1258 delta
= ((delta
+growable_space
)*m_growableColsProportions
[idx
])/sum_proportions
;
1259 m_colWidths
[ m_growableCols
[idx
] ] = delta
;
1264 else if ( (m_growMode
== wxFLEX_GROWMODE_ALL
) && (sz
.x
> minsz
.x
) )
1266 for ( int col
=0; col
< ncols
; ++col
)
1267 m_colWidths
[ col
] = sz
.x
/ ncols
;
1272 void wxFlexGridSizer::AddGrowableRow( size_t idx
, int proportion
)
1274 m_growableRows
.Add( idx
);
1275 m_growableRowsProportions
.Add( proportion
);
1278 void wxFlexGridSizer::RemoveGrowableRow( size_t idx
)
1280 m_growableRows
.Remove( idx
);
1283 void wxFlexGridSizer::AddGrowableCol( size_t idx
, int proportion
)
1285 m_growableCols
.Add( idx
);
1286 m_growableColsProportions
.Add( proportion
);
1289 void wxFlexGridSizer::RemoveGrowableCol( size_t idx
)
1291 m_growableCols
.Remove( idx
);
1294 //---------------------------------------------------------------------------
1296 //---------------------------------------------------------------------------
1298 wxBoxSizer::wxBoxSizer( int orient
)
1299 : m_orient( orient
)
1303 void wxBoxSizer::RecalcSizes()
1305 if (m_children
.GetCount() == 0)
1311 if (m_orient
== wxHORIZONTAL
)
1312 delta
= m_size
.x
- m_fixedWidth
;
1314 delta
= m_size
.y
- m_fixedHeight
;
1317 wxPoint
pt( m_position
);
1319 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
1322 wxSizerItem
*item
= node
->GetData();
1324 if (item
->IsShown())
1326 wxSize
size( item
->GetMinSizeWithBorder() );
1328 if (m_orient
== wxVERTICAL
)
1330 wxCoord height
= size
.y
;
1331 if (item
->GetProportion())
1333 // Because of at least one visible item has non-zero
1334 // proportion then m_stretchable is not zero
1335 height
= (delta
* item
->GetProportion()) / m_stretchable
;
1338 wxPoint
child_pos( pt
);
1339 wxSize
child_size( wxSize( size
.x
, height
) );
1341 if (item
->GetFlag() & (wxEXPAND
| wxSHAPED
))
1342 child_size
.x
= m_size
.x
;
1343 else if (item
->GetFlag() & wxALIGN_RIGHT
)
1344 child_pos
.x
+= m_size
.x
- size
.x
;
1345 else if (item
->GetFlag() & (wxCENTER
| wxALIGN_CENTER_HORIZONTAL
))
1346 // XXX wxCENTER is added for backward compatibility;
1347 // wxALIGN_CENTER should be used in new code
1348 child_pos
.x
+= (m_size
.x
- size
.x
) / 2;
1350 item
->SetDimension( child_pos
, child_size
);
1356 wxCoord width
= size
.x
;
1357 if (item
->GetProportion())
1359 // Because of at least one visible item has non-zero
1360 // proportion then m_stretchable is not zero
1361 width
= (delta
* item
->GetProportion()) / m_stretchable
;
1364 wxPoint
child_pos( pt
);
1365 wxSize
child_size( wxSize(width
, size
.y
) );
1367 if (item
->GetFlag() & (wxEXPAND
| wxSHAPED
))
1368 child_size
.y
= m_size
.y
;
1369 else if (item
->GetFlag() & wxALIGN_BOTTOM
)
1370 child_pos
.y
+= m_size
.y
- size
.y
;
1371 else if (item
->GetFlag() & (wxCENTER
| wxALIGN_CENTER_VERTICAL
))
1372 // XXX wxCENTER is added for backward compatibility;
1373 // wxALIGN_CENTER should be used in new code
1374 child_pos
.y
+= (m_size
.y
- size
.y
) / 2;
1376 item
->SetDimension( child_pos
, child_size
);
1382 node
= node
->GetNext();
1386 wxSize
wxBoxSizer::CalcMin()
1388 if (m_children
.GetCount() == 0)
1389 return wxSize(10,10);
1397 // precalc item minsizes and count proportions
1398 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
1401 wxSizerItem
*item
= node
->GetData();
1403 if (item
->IsShown())
1404 item
->CalcMin(); // result is stored in the item
1406 if (item
->IsShown() && item
->GetProportion() != 0)
1407 m_stretchable
+= item
->GetProportion();
1409 node
= node
->GetNext();
1412 // Total minimum size (width or height) of sizer
1415 node
= m_children
.GetFirst();
1418 wxSizerItem
*item
= node
->GetData();
1420 if (item
->IsShown() && item
->GetProportion() != 0)
1422 int stretch
= item
->GetProportion();
1423 wxSize
size( item
->GetMinSizeWithBorder() );
1426 // Integer division rounded up is (a + b - 1) / b
1427 // Round up needed in order to guarantee that all
1428 // all items will have size not less then their min size
1429 if (m_orient
== wxHORIZONTAL
)
1430 minSize
= ( size
.x
*m_stretchable
+ stretch
- 1)/stretch
;
1432 minSize
= ( size
.y
*m_stretchable
+ stretch
- 1)/stretch
;
1434 if (minSize
> maxMinSize
)
1435 maxMinSize
= minSize
;
1437 node
= node
->GetNext();
1440 // Calculate overall minimum size
1441 node
= m_children
.GetFirst();
1444 wxSizerItem
*item
= node
->GetData();
1446 if (item
->IsShown())
1448 wxSize
size( item
->GetMinSizeWithBorder() );
1449 if (item
->GetProportion() != 0)
1451 if (m_orient
== wxHORIZONTAL
)
1452 size
.x
= (maxMinSize
*item
->GetProportion())/m_stretchable
;
1454 size
.y
= (maxMinSize
*item
->GetProportion())/m_stretchable
;
1458 if (m_orient
== wxVERTICAL
)
1460 m_fixedHeight
+= size
.y
;
1461 m_fixedWidth
= wxMax( m_fixedWidth
, size
.x
);
1465 m_fixedWidth
+= size
.x
;
1466 m_fixedHeight
= wxMax( m_fixedHeight
, size
.y
);
1470 if (m_orient
== wxHORIZONTAL
)
1472 m_minWidth
+= size
.x
;
1473 m_minHeight
= wxMax( m_minHeight
, size
.y
);
1477 m_minHeight
+= size
.y
;
1478 m_minWidth
= wxMax( m_minWidth
, size
.x
);
1481 node
= node
->GetNext();
1484 return wxSize( m_minWidth
, m_minHeight
);
1487 //---------------------------------------------------------------------------
1489 //---------------------------------------------------------------------------
1493 wxStaticBoxSizer::wxStaticBoxSizer( wxStaticBox
*box
, int orient
)
1494 : wxBoxSizer( orient
)
1495 , m_staticBox( box
)
1497 wxASSERT_MSG( box
, wxT("wxStaticBoxSizer needs a static box") );
1500 static void GetStaticBoxBorders( wxStaticBox
*box
,
1504 // this has to be done platform by platform as there is no way to
1505 // guess the thickness of a wxStaticBox border
1507 box
->GetBordersForSizer(borderTop
,borderOther
);
1508 #elif defined(__WXMAC__)
1510 static int extraTop
= -1; // Uninitted
1511 static int other
= 5;
1513 if ( extraTop
== -1 )
1515 // The minimal border used for the top. Later on the staticbox'
1516 // font height is added to this.
1519 if ( UMAGetSystemVersion() >= 0x1030 /*Panther*/ )
1521 // As indicated by the HIG, Panther needs an extra border of 11
1522 // pixels (otherwise overlapping occurs at the top). The "other"
1523 // border has to be 11.
1530 *borderTop
= extraTop
+ box
->GetCharHeight();
1531 *borderOther
= other
;
1535 if ( box
->GetLabel().IsEmpty() )
1539 *borderTop
= box
->GetCharHeight();
1542 #endif // __WXCOCOA__
1545 void wxStaticBoxSizer::RecalcSizes()
1547 int top_border
, other_border
;
1548 GetStaticBoxBorders(m_staticBox
, &top_border
, &other_border
);
1550 m_staticBox
->SetSize( m_position
.x
, m_position
.y
, m_size
.x
, m_size
.y
);
1552 wxPoint
old_pos( m_position
);
1553 m_position
.x
+= other_border
;
1554 m_position
.y
+= top_border
;
1555 wxSize
old_size( m_size
);
1556 m_size
.x
-= 2*other_border
;
1557 m_size
.y
-= top_border
+ other_border
;
1559 wxBoxSizer::RecalcSizes();
1561 m_position
= old_pos
;
1565 wxSize
wxStaticBoxSizer::CalcMin()
1567 int top_border
, other_border
;
1568 GetStaticBoxBorders(m_staticBox
, &top_border
, &other_border
);
1570 wxSize
ret( wxBoxSizer::CalcMin() );
1571 ret
.x
+= 2*other_border
;
1572 ret
.y
+= other_border
+ top_border
;
1577 void wxStaticBoxSizer::ShowItems( bool show
)
1579 m_staticBox
->Show( show
);
1580 wxBoxSizer::ShowItems( show
);
1583 #endif // wxUSE_STATBOX
1586 #if WXWIN_COMPATIBILITY_2_4
1588 // ----------------------------------------------------------------------------
1590 // ----------------------------------------------------------------------------
1593 IMPLEMENT_CLASS(wxBookCtrlSizer
, wxSizer
)
1595 IMPLEMENT_CLASS(wxNotebookSizer
, wxBookCtrlSizer
)
1596 #endif // wxUSE_NOTEBOOK
1597 #endif // wxUSE_BOOKCTRL
1601 wxBookCtrlSizer::wxBookCtrlSizer(wxBookCtrl
*bookctrl
)
1602 : m_bookctrl(bookctrl
)
1604 wxASSERT_MSG( bookctrl
, wxT("wxBookCtrlSizer needs a control") );
1607 void wxBookCtrlSizer::RecalcSizes()
1609 m_bookctrl
->SetSize( m_position
.x
, m_position
.y
, m_size
.x
, m_size
.y
);
1612 wxSize
wxBookCtrlSizer::CalcMin()
1614 wxSize sizeBorder
= m_bookctrl
->CalcSizeFromPage(wxSize(0, 0));
1619 if ( m_bookctrl
->GetPageCount() == 0 )
1621 return wxSize(sizeBorder
.x
+ 10, sizeBorder
.y
+ 10);
1627 wxWindowList::compatibility_iterator
1628 node
= m_bookctrl
->GetChildren().GetFirst();
1631 wxWindow
*item
= node
->GetData();
1632 wxSizer
*itemsizer
= item
->GetSizer();
1636 wxSize
subsize( itemsizer
->CalcMin() );
1638 if (subsize
.x
> maxX
)
1640 if (subsize
.y
> maxY
)
1644 node
= node
->GetNext();
1647 return wxSize( maxX
, maxY
) + sizeBorder
;
1652 wxNotebookSizer::wxNotebookSizer(wxNotebook
*nb
)
1654 wxASSERT_MSG( nb
, wxT("wxNotebookSizer needs a control") );
1658 #endif // wxUSE_NOTEBOOOK
1659 #endif // wxUSE_BOOKCTRL
1661 #endif // WXWIN_COMPATIBILITY_2_4