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::Add( wxWindow
*window
, int proportion
, int flag
, int border
, wxObject
* userData
)
347 Add( new wxSizerItem( window
, proportion
, flag
, border
, userData
) );
350 void wxSizer::Add( wxSizer
*sizer
, int proportion
, int flag
, int border
, wxObject
* userData
)
352 Add( new wxSizerItem( sizer
, proportion
, flag
, border
, userData
) );
355 void wxSizer::Add( int width
, int height
, int proportion
, int flag
, int border
, wxObject
* userData
)
357 Add( new wxSizerItem( width
, height
, proportion
, flag
, border
, userData
) );
360 void wxSizer::Add( wxSizerItem
*item
)
362 Insert( m_children
.GetCount(), item
);
365 void wxSizer::AddSpacer(int size
)
370 void wxSizer::AddStretchSpacer(int prop
)
375 void wxSizer::Prepend( wxWindow
*window
, int proportion
, int flag
, int border
, wxObject
* userData
)
377 Prepend( new wxSizerItem( window
, proportion
, flag
, border
, userData
) );
380 void wxSizer::Prepend( wxSizer
*sizer
, int proportion
, int flag
, int border
, wxObject
* userData
)
382 Prepend( new wxSizerItem( sizer
, proportion
, flag
, border
, userData
) );
385 void wxSizer::Prepend( int width
, int height
, int proportion
, int flag
, int border
, wxObject
* userData
)
387 Prepend( new wxSizerItem( width
, height
, proportion
, flag
, border
, userData
) );
390 void wxSizer::Prepend( wxSizerItem
*item
)
395 void wxSizer::PrependSpacer(int size
)
400 void wxSizer::PrependStretchSpacer(int prop
)
405 void wxSizer::Insert( size_t index
,
412 Insert( index
, new wxSizerItem( window
, proportion
, flag
, border
, userData
) );
415 void wxSizer::Insert( size_t index
,
422 Insert( index
, new wxSizerItem( sizer
, proportion
, flag
, border
, userData
) );
425 void wxSizer::Insert( size_t index
,
433 Insert( index
, new wxSizerItem( width
, height
, proportion
, flag
, border
, userData
) );
436 void wxSizer::Insert( size_t index
, wxSizerItem
*item
)
438 m_children
.Insert( index
, item
);
440 if( item
->GetWindow() )
441 item
->GetWindow()->SetContainingSizer( this );
444 void wxSizer::InsertSpacer(size_t index
, int size
)
446 Insert(index
, size
, size
);
449 void wxSizer::InsertStretchSpacer(size_t index
, int prop
)
451 Insert(index
, 0, 0, prop
);
454 bool wxSizer::Remove( wxWindow
*window
)
456 return Detach( window
);
459 bool wxSizer::Remove( wxSizer
*sizer
)
461 wxASSERT_MSG( sizer
, _T("Removing NULL sizer") );
463 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
466 wxSizerItem
*item
= node
->GetData();
468 if (item
->GetSizer() == sizer
)
471 m_children
.Erase( node
);
475 node
= node
->GetNext();
481 bool wxSizer::Remove( int index
)
483 wxCHECK_MSG( index
>= 0 && (size_t)index
< m_children
.GetCount(),
485 _T("Remove index is out of range") );
487 wxSizerItemList::compatibility_iterator node
= m_children
.Item( index
);
489 wxCHECK_MSG( node
, false, _T("Failed to find child node") );
491 wxSizerItem
*item
= node
->GetData();
493 if( item
->IsWindow() )
494 item
->GetWindow()->SetContainingSizer( NULL
);
497 m_children
.Erase( node
);
501 bool wxSizer::Detach( wxSizer
*sizer
)
503 wxASSERT_MSG( sizer
, _T("Detaching NULL sizer") );
505 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
508 wxSizerItem
*item
= node
->GetData();
510 if (item
->GetSizer() == sizer
)
514 m_children
.Erase( node
);
517 node
= node
->GetNext();
523 bool wxSizer::Detach( wxWindow
*window
)
525 wxASSERT_MSG( window
, _T("Detaching NULL window") );
527 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
530 wxSizerItem
*item
= node
->GetData();
532 if (item
->GetWindow() == window
)
534 item
->GetWindow()->SetContainingSizer( NULL
);
536 m_children
.Erase( node
);
539 node
= node
->GetNext();
545 bool wxSizer::Detach( int index
)
547 wxCHECK_MSG( index
>= 0 && (size_t)index
< m_children
.GetCount(),
549 _T("Detach index is out of range") );
551 wxSizerItemList::compatibility_iterator node
= m_children
.Item( index
);
553 wxCHECK_MSG( node
, false, _T("Failed to find child node") );
555 wxSizerItem
*item
= node
->GetData();
557 if( item
->IsSizer() )
559 else if( item
->IsWindow() )
560 item
->GetWindow()->SetContainingSizer( NULL
);
563 m_children
.Erase( node
);
567 void wxSizer::Clear( bool delete_windows
)
569 // First clear the ContainingSizer pointers
570 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
573 wxSizerItem
*item
= node
->GetData();
575 if (item
->IsWindow())
576 item
->GetWindow()->SetContainingSizer( NULL
);
577 node
= node
->GetNext();
580 // Destroy the windows if needed
584 // Now empty the list
585 WX_CLEAR_LIST(wxSizerItemList
, m_children
);
588 void wxSizer::DeleteWindows()
590 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
593 wxSizerItem
*item
= node
->GetData();
595 item
->DeleteWindows();
596 node
= node
->GetNext();
600 wxSize
wxSizer::Fit( wxWindow
*window
)
602 wxSize
size(window
->IsTopLevel() ? FitSize(window
)
603 : GetMinWindowSize(window
));
605 window
->SetSize( size
);
610 void wxSizer::FitInside( wxWindow
*window
)
613 if (window
->IsTopLevel())
614 size
= VirtualFitSize( window
);
616 size
= GetMinClientSize( window
);
618 window
->SetVirtualSize( size
);
621 void wxSizer::Layout()
623 // (re)calculates minimums needed for each item and other preparations
627 // Applies the layout and repositions/resizes the items
631 void wxSizer::SetSizeHints( wxWindow
*window
)
633 // Preserve the window's max size hints, but set the
634 // lower bound according to the sizer calculations.
636 wxSize size
= Fit( window
);
638 window
->SetSizeHints( size
.x
,
640 window
->GetMaxWidth(),
641 window
->GetMaxHeight() );
644 void wxSizer::SetVirtualSizeHints( wxWindow
*window
)
646 // Preserve the window's max size hints, but set the
647 // lower bound according to the sizer calculations.
650 wxSize
size( window
->GetVirtualSize() );
651 window
->SetVirtualSizeHints( size
.x
,
653 window
->GetMaxWidth(),
654 window
->GetMaxHeight() );
657 wxSize
wxSizer::GetMaxWindowSize( wxWindow
*window
) const
659 return window
->GetMaxSize();
662 wxSize
wxSizer::GetMinWindowSize( wxWindow
*window
)
664 wxSize
minSize( GetMinSize() );
665 wxSize
size( window
->GetSize() );
666 wxSize
client_size( window
->GetClientSize() );
668 return wxSize( minSize
.x
+size
.x
-client_size
.x
,
669 minSize
.y
+size
.y
-client_size
.y
);
672 // TODO on mac we need a function that determines how much free space this
673 // min size contains, in order to make sure that we have 20 pixels of free
674 // space around the controls
676 // Return a window size that will fit within the screens dimensions
677 wxSize
wxSizer::FitSize( wxWindow
*window
)
679 wxSize size
= GetMinWindowSize( window
);
680 wxSize sizeMax
= GetMaxWindowSize( window
);
682 // Limit the size if sizeMax != wxDefaultSize
684 if ( size
.x
> sizeMax
.x
&& sizeMax
.x
!= -1 )
686 if ( size
.y
> sizeMax
.y
&& sizeMax
.y
!= -1 )
692 wxSize
wxSizer::GetMaxClientSize( wxWindow
*window
) const
694 wxSize
maxSize( window
->GetMaxSize() );
696 if( maxSize
!= wxDefaultSize
)
698 wxSize
size( window
->GetSize() );
699 wxSize
client_size( window
->GetClientSize() );
701 return wxSize( maxSize
.x
+ client_size
.x
- size
.x
,
702 maxSize
.y
+ client_size
.y
- size
.y
);
705 return wxDefaultSize
;
708 wxSize
wxSizer::GetMinClientSize( wxWindow
*WXUNUSED(window
) )
710 return GetMinSize(); // Already returns client size.
713 wxSize
wxSizer::VirtualFitSize( wxWindow
*window
)
715 wxSize size
= GetMinClientSize( window
);
716 wxSize sizeMax
= GetMaxClientSize( window
);
718 // Limit the size if sizeMax != wxDefaultSize
720 if ( size
.x
> sizeMax
.x
&& sizeMax
.x
!= -1 )
722 if ( size
.y
> sizeMax
.y
&& sizeMax
.y
!= -1 )
728 void wxSizer::SetDimension( int x
, int y
, int width
, int height
)
737 wxSize
wxSizer::GetMinSize()
739 wxSize
ret( CalcMin() );
740 if (ret
.x
< m_minSize
.x
) ret
.x
= m_minSize
.x
;
741 if (ret
.y
< m_minSize
.y
) ret
.y
= m_minSize
.y
;
745 void wxSizer::DoSetMinSize( int width
, int height
)
748 m_minSize
.y
= height
;
751 bool wxSizer::DoSetItemMinSize( wxWindow
*window
, int width
, int height
)
753 wxASSERT_MSG( window
, _T("SetMinSize for NULL window") );
755 // Is it our immediate child?
757 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
760 wxSizerItem
*item
= node
->GetData();
762 if (item
->GetWindow() == window
)
764 item
->SetMinSize( width
, height
);
767 node
= node
->GetNext();
770 // No? Search any subsizers we own then
772 node
= m_children
.GetFirst();
775 wxSizerItem
*item
= node
->GetData();
777 if ( item
->GetSizer() &&
778 item
->GetSizer()->DoSetItemMinSize( window
, width
, height
) )
780 // A child sizer found the requested windw, exit.
783 node
= node
->GetNext();
789 bool wxSizer::DoSetItemMinSize( wxSizer
*sizer
, int width
, int height
)
791 wxASSERT_MSG( sizer
, _T("SetMinSize for NULL sizer") );
793 // Is it our immediate child?
795 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
798 wxSizerItem
*item
= node
->GetData();
800 if (item
->GetSizer() == sizer
)
802 item
->GetSizer()->DoSetMinSize( width
, height
);
805 node
= node
->GetNext();
808 // No? Search any subsizers we own then
810 node
= m_children
.GetFirst();
813 wxSizerItem
*item
= node
->GetData();
815 if ( item
->GetSizer() &&
816 item
->GetSizer()->DoSetItemMinSize( sizer
, width
, height
) )
818 // A child found the requested sizer, exit.
821 node
= node
->GetNext();
827 bool wxSizer::DoSetItemMinSize( size_t index
, int width
, int height
)
829 wxSizerItemList::compatibility_iterator node
= m_children
.Item( index
);
831 wxCHECK_MSG( node
, false, _T("Failed to find child node") );
833 wxSizerItem
*item
= node
->GetData();
835 if (item
->GetSizer())
837 // Sizers contains the minimal size in them, if not calculated ...
838 item
->GetSizer()->DoSetMinSize( width
, height
);
842 // ... but the minimal size of spacers and windows is stored via the item
843 item
->SetMinSize( width
, height
);
849 bool wxSizer::Show( wxWindow
*window
, bool show
, bool recursive
)
851 wxASSERT_MSG( window
, _T("Show for NULL window") );
853 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
856 wxSizerItem
*item
= node
->GetData();
858 if (item
->GetWindow() == window
)
864 else if (recursive
&& item
->IsSizer())
866 if (item
->GetSizer()->Show(window
, show
, recursive
))
870 node
= node
->GetNext();
876 bool wxSizer::Show( wxSizer
*sizer
, bool show
, bool recursive
)
878 wxASSERT_MSG( sizer
, _T("Show for NULL sizer") );
880 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
883 wxSizerItem
*item
= node
->GetData();
885 if (item
->GetSizer() == sizer
)
891 else if (recursive
&& item
->IsSizer())
893 if (item
->GetSizer()->Show(sizer
, show
, recursive
))
897 node
= node
->GetNext();
903 bool wxSizer::Show( size_t index
, bool show
)
905 wxCHECK_MSG( index
< m_children
.GetCount(),
907 _T("Show index is out of range") );
909 m_children
.Item( index
)->GetData()->Show( show
);
914 void wxSizer::ShowItems( bool show
)
916 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
919 node
->GetData()->Show( show
);
920 node
= node
->GetNext();
924 bool wxSizer::IsShown( wxWindow
*window
) const
926 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
929 wxSizerItem
*item
= node
->GetData();
931 if (item
->GetWindow() == window
)
933 return item
->IsShown();
935 node
= node
->GetNext();
938 wxFAIL_MSG( _T("IsShown failed to find sizer item") );
943 bool wxSizer::IsShown( wxSizer
*sizer
) const
945 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
948 wxSizerItem
*item
= node
->GetData();
950 if (item
->GetSizer() == sizer
)
952 return item
->IsShown();
954 node
= node
->GetNext();
957 wxFAIL_MSG( _T("IsShown failed to find sizer item") );
962 bool wxSizer::IsShown( size_t index
) const
964 wxCHECK_MSG( index
< m_children
.GetCount(),
966 _T("IsShown index is out of range") );
968 return m_children
.Item( index
)->GetData()->IsShown();
972 //---------------------------------------------------------------------------
974 //---------------------------------------------------------------------------
976 wxGridSizer::wxGridSizer( int rows
, int cols
, int vgap
, int hgap
)
982 if (m_rows
== 0 && m_cols
== 0)
986 wxGridSizer::wxGridSizer( int cols
, int vgap
, int hgap
)
992 if (m_rows
== 0 && m_cols
== 0)
996 int wxGridSizer::CalcRowsCols(int& nrows
, int& ncols
) const
998 int nitems
= m_children
.GetCount();
1004 nrows
= (nitems
+ m_cols
- 1) / m_cols
;
1008 ncols
= (nitems
+ m_rows
- 1) / m_rows
;
1011 else // 0 columns, 0 rows?
1013 wxFAIL_MSG( _T("grid sizer must have either rows or columns fixed") );
1022 void wxGridSizer::RecalcSizes()
1024 int nitems
, nrows
, ncols
;
1025 if ( (nitems
= CalcRowsCols(nrows
, ncols
)) == 0 )
1028 wxSize
sz( GetSize() );
1029 wxPoint
pt( GetPosition() );
1031 int w
= (sz
.x
- (ncols
- 1) * m_hgap
) / ncols
;
1032 int h
= (sz
.y
- (nrows
- 1) * m_vgap
) / nrows
;
1035 for (int c
= 0; c
< ncols
; c
++)
1038 for (int r
= 0; r
< nrows
; r
++)
1040 int i
= r
* ncols
+ c
;
1043 wxSizerItemList::compatibility_iterator node
= m_children
.Item( i
);
1045 wxASSERT_MSG( node
, _T("Failed to find SizerItemList node") );
1047 SetItemBounds( node
->GetData(), x
, y
, w
, h
);
1055 wxSize
wxGridSizer::CalcMin()
1058 if ( CalcRowsCols(nrows
, ncols
) == 0 )
1059 return wxSize(10, 10);
1061 // Find the max width and height for any component
1065 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
1068 wxSizerItem
*item
= node
->GetData();
1069 wxSize
sz( item
->CalcMin() );
1071 w
= wxMax( w
, sz
.x
);
1072 h
= wxMax( h
, sz
.y
);
1074 node
= node
->GetNext();
1077 return wxSize( ncols
* w
+ (ncols
-1) * m_hgap
,
1078 nrows
* h
+ (nrows
-1) * m_vgap
);
1081 void wxGridSizer::SetItemBounds( wxSizerItem
*item
, int x
, int y
, int w
, int h
)
1084 wxSize
sz( item
->GetMinSizeWithBorder() );
1085 int flag
= item
->GetFlag();
1087 if ((flag
& wxEXPAND
) || (flag
& wxSHAPED
))
1093 if (flag
& wxALIGN_CENTER_HORIZONTAL
)
1095 pt
.x
= x
+ (w
- sz
.x
) / 2;
1097 else if (flag
& wxALIGN_RIGHT
)
1099 pt
.x
= x
+ (w
- sz
.x
);
1102 if (flag
& wxALIGN_CENTER_VERTICAL
)
1104 pt
.y
= y
+ (h
- sz
.y
) / 2;
1106 else if (flag
& wxALIGN_BOTTOM
)
1108 pt
.y
= y
+ (h
- sz
.y
);
1112 item
->SetDimension(pt
, sz
);
1115 //---------------------------------------------------------------------------
1117 //---------------------------------------------------------------------------
1119 wxFlexGridSizer::wxFlexGridSizer( int rows
, int cols
, int vgap
, int hgap
)
1120 : wxGridSizer( rows
, cols
, vgap
, hgap
),
1121 m_flexDirection(wxBOTH
),
1122 m_growMode(wxFLEX_GROWMODE_SPECIFIED
)
1126 wxFlexGridSizer::wxFlexGridSizer( int cols
, int vgap
, int hgap
)
1127 : wxGridSizer( cols
, vgap
, hgap
),
1128 m_flexDirection(wxBOTH
),
1129 m_growMode(wxFLEX_GROWMODE_SPECIFIED
)
1133 wxFlexGridSizer::~wxFlexGridSizer()
1137 void wxFlexGridSizer::RecalcSizes()
1139 int nitems
, nrows
, ncols
;
1140 if ( (nitems
= CalcRowsCols(nrows
, ncols
)) == 0 )
1143 wxPoint
pt( GetPosition() );
1144 wxSize
sz( GetSize() );
1146 AdjustForGrowables(sz
, m_calculatedMinSize
, nrows
, ncols
);
1148 sz
= wxSize( pt
.x
+ sz
.x
, pt
.y
+ sz
.y
);
1151 for (int c
= 0; c
< ncols
; c
++)
1154 for (int r
= 0; r
< nrows
; r
++)
1156 int i
= r
* ncols
+ c
;
1159 wxSizerItemList::compatibility_iterator node
= m_children
.Item( i
);
1161 wxASSERT_MSG( node
, _T("Failed to find node") );
1163 int w
= wxMax( 0, wxMin( m_colWidths
[c
], sz
.x
- x
) );
1164 int h
= wxMax( 0, wxMin( m_rowHeights
[r
], sz
.y
- y
) );
1166 SetItemBounds( node
->GetData(), x
, y
, w
, h
);
1168 y
= y
+ m_rowHeights
[r
] + m_vgap
;
1170 x
= x
+ m_colWidths
[c
] + m_hgap
;
1174 wxSize
wxFlexGridSizer::CalcMin()
1180 // Number of rows/columns can change as items are added or removed.
1181 if ( !CalcRowsCols(nrows
, ncols
) )
1182 return wxSize(10, 10);
1184 m_rowHeights
.SetCount(nrows
);
1185 m_colWidths
.SetCount(ncols
);
1187 // We have to recalcuate the sizes in case the item minimum size has
1188 // changed since the previous layout, or the item has been hidden using
1189 // wxSizer::Show(). If all the items in a row/column are hidden, the final
1190 // dimension of the row/column will be -1, indicating that the column
1191 // itself is hidden.
1192 for( s
= m_rowHeights
.GetCount(), i
= 0; i
< s
; ++i
)
1193 m_rowHeights
[ i
] = -1;
1194 for( s
= m_colWidths
.GetCount(), i
= 0; i
< s
; ++i
)
1195 m_colWidths
[ i
] = -1;
1197 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
1202 wxSizerItem
*item
= node
->GetData();
1203 if ( item
->IsShown() )
1205 wxSize
sz( item
->CalcMin() );
1206 int row
= i
/ ncols
;
1207 int col
= i
% ncols
;
1209 m_rowHeights
[ row
] = wxMax( wxMax( 0, sz
.y
), m_rowHeights
[ row
] );
1210 m_colWidths
[ col
] = wxMax( wxMax( 0, sz
.x
), m_colWidths
[ col
] );
1213 node
= node
->GetNext();
1217 AdjustForFlexDirection();
1219 // Sum total minimum size, including gaps between rows/columns.
1220 // -1 is used as a magic number meaning empty column.
1222 for (int col
= 0; col
< ncols
; col
++)
1223 if ( m_colWidths
[ col
] != -1 )
1224 width
+= m_colWidths
[ col
] + ( col
== ncols
-1 ? 0 : m_hgap
);
1227 for (int row
= 0; row
< nrows
; row
++)
1228 if ( m_rowHeights
[ row
] != -1 )
1229 height
+= m_rowHeights
[ row
] + ( row
== nrows
-1 ? 0 : m_vgap
);
1231 m_calculatedMinSize
= wxSize( width
, height
);
1232 return m_calculatedMinSize
;
1235 void wxFlexGridSizer::AdjustForFlexDirection()
1237 // the logic in CalcMin works when we resize flexibly in both directions
1238 // but maybe this is not the case
1239 if ( m_flexDirection
!= wxBOTH
)
1241 // select the array corresponding to the direction in which we do *not*
1243 wxArrayInt
& array
= m_flexDirection
== wxVERTICAL
? m_colWidths
1246 const int count
= array
.GetCount();
1248 // find the largest value in this array
1250 for ( n
= 0; n
< count
; ++n
)
1252 if ( array
[n
] > largest
)
1256 // and now fill it with the largest value
1257 for ( n
= 0; n
< count
; ++n
)
1265 void wxFlexGridSizer::AdjustForGrowables(const wxSize
& sz
, const wxSize
& minsz
,
1266 int nrows
, int ncols
)
1268 // what to do with the rows? by default, resize them proportionally
1269 if ( sz
.y
> minsz
.y
&& ( (m_flexDirection
& wxVERTICAL
) || (m_growMode
== wxFLEX_GROWMODE_SPECIFIED
) ) )
1271 int sum_proportions
= 0;
1272 int growable_space
= 0;
1275 for (idx
= 0; idx
< m_growableRows
.GetCount(); idx
++)
1277 // Since the number of rows/columns can change as items are
1278 // inserted/deleted, we need to verify at runtime that the
1279 // requested growable rows/columns are still valid.
1280 if (m_growableRows
[idx
] >= nrows
)
1283 // If all items in a row/column are hidden, that row/column will
1284 // have a dimension of -1. This causes the row/column to be
1285 // hidden completely.
1286 if (m_rowHeights
[ m_growableRows
[idx
] ] == -1)
1288 sum_proportions
+= m_growableRowsProportions
[idx
];
1289 growable_space
+= m_rowHeights
[ m_growableRows
[idx
] ];
1295 for (idx
= 0; idx
< m_growableRows
.GetCount(); idx
++)
1297 if (m_growableRows
[idx
] >= nrows
)
1299 if (m_rowHeights
[ m_growableRows
[idx
] ] == -1)
1300 m_rowHeights
[ m_growableRows
[idx
] ] = 0;
1303 int delta
= (sz
.y
- minsz
.y
);
1304 if (sum_proportions
== 0)
1305 delta
= (delta
/num
) + m_rowHeights
[ m_growableRows
[idx
] ];
1307 delta
= ((delta
+growable_space
)*m_growableRowsProportions
[idx
]) / sum_proportions
;
1308 m_rowHeights
[ m_growableRows
[idx
] ] = delta
;
1313 else if ( (m_growMode
== wxFLEX_GROWMODE_ALL
) && (sz
.y
> minsz
.y
) )
1315 // rounding problem?
1316 for ( int row
= 0; row
< nrows
; ++row
)
1317 m_rowHeights
[ row
] = sz
.y
/ nrows
;
1320 // the same logic as above but for the columns
1321 if ( sz
.x
> minsz
.x
&& ( (m_flexDirection
& wxHORIZONTAL
) || (m_growMode
== wxFLEX_GROWMODE_SPECIFIED
) ) )
1323 int sum_proportions
= 0;
1324 int growable_space
= 0;
1327 for (idx
= 0; idx
< m_growableCols
.GetCount(); idx
++)
1329 // Since the number of rows/columns can change as items are
1330 // inserted/deleted, we need to verify at runtime that the
1331 // requested growable rows/columns are still valid.
1332 if (m_growableCols
[idx
] >= ncols
)
1335 // If all items in a row/column are hidden, that row/column will
1336 // have a dimension of -1. This causes the column to be hidden
1338 if (m_colWidths
[ m_growableCols
[idx
] ] == -1)
1340 sum_proportions
+= m_growableColsProportions
[idx
];
1341 growable_space
+= m_colWidths
[ m_growableCols
[idx
] ];
1347 for (idx
= 0; idx
< m_growableCols
.GetCount(); idx
++)
1349 if (m_growableCols
[idx
] >= ncols
)
1351 if (m_colWidths
[ m_growableCols
[idx
] ] == -1)
1352 m_colWidths
[ m_growableCols
[idx
] ] = 0;
1355 int delta
= (sz
.x
- minsz
.x
);
1356 if (sum_proportions
== 0)
1357 delta
= (delta
/num
) + m_colWidths
[ m_growableCols
[idx
] ];
1359 delta
= ((delta
+growable_space
)*m_growableColsProportions
[idx
])/sum_proportions
;
1360 m_colWidths
[ m_growableCols
[idx
] ] = delta
;
1365 else if ( (m_growMode
== wxFLEX_GROWMODE_ALL
) && (sz
.x
> minsz
.x
) )
1367 for ( int col
=0; col
< ncols
; ++col
)
1368 m_colWidths
[ col
] = sz
.x
/ ncols
;
1373 void wxFlexGridSizer::AddGrowableRow( size_t idx
, int proportion
)
1375 m_growableRows
.Add( idx
);
1376 m_growableRowsProportions
.Add( proportion
);
1379 void wxFlexGridSizer::RemoveGrowableRow( size_t idx
)
1381 m_growableRows
.Remove( idx
);
1384 void wxFlexGridSizer::AddGrowableCol( size_t idx
, int proportion
)
1386 m_growableCols
.Add( idx
);
1387 m_growableColsProportions
.Add( proportion
);
1390 void wxFlexGridSizer::RemoveGrowableCol( size_t idx
)
1392 m_growableCols
.Remove( idx
);
1395 //---------------------------------------------------------------------------
1397 //---------------------------------------------------------------------------
1399 wxBoxSizer::wxBoxSizer( int orient
)
1400 : m_orient( orient
)
1404 void wxBoxSizer::RecalcSizes()
1406 if (m_children
.GetCount() == 0)
1412 if (m_orient
== wxHORIZONTAL
)
1413 delta
= m_size
.x
- m_fixedWidth
;
1415 delta
= m_size
.y
- m_fixedHeight
;
1418 wxPoint
pt( m_position
);
1420 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
1423 wxSizerItem
*item
= node
->GetData();
1425 if (item
->IsShown())
1427 wxSize
size( item
->GetMinSizeWithBorder() );
1429 if (m_orient
== wxVERTICAL
)
1431 wxCoord height
= size
.y
;
1432 if (item
->GetProportion())
1434 // Because of at least one visible item has non-zero
1435 // proportion then m_stretchable is not zero
1436 height
= (delta
* item
->GetProportion()) / m_stretchable
;
1439 wxPoint
child_pos( pt
);
1440 wxSize
child_size( wxSize( size
.x
, height
) );
1442 if (item
->GetFlag() & (wxEXPAND
| wxSHAPED
))
1443 child_size
.x
= m_size
.x
;
1444 else if (item
->GetFlag() & wxALIGN_RIGHT
)
1445 child_pos
.x
+= m_size
.x
- size
.x
;
1446 else if (item
->GetFlag() & (wxCENTER
| wxALIGN_CENTER_HORIZONTAL
))
1447 // XXX wxCENTER is added for backward compatibility;
1448 // wxALIGN_CENTER should be used in new code
1449 child_pos
.x
+= (m_size
.x
- size
.x
) / 2;
1451 item
->SetDimension( child_pos
, child_size
);
1457 wxCoord width
= size
.x
;
1458 if (item
->GetProportion())
1460 // Because of at least one visible item has non-zero
1461 // proportion then m_stretchable is not zero
1462 width
= (delta
* item
->GetProportion()) / m_stretchable
;
1465 wxPoint
child_pos( pt
);
1466 wxSize
child_size( wxSize(width
, size
.y
) );
1468 if (item
->GetFlag() & (wxEXPAND
| wxSHAPED
))
1469 child_size
.y
= m_size
.y
;
1470 else if (item
->GetFlag() & wxALIGN_BOTTOM
)
1471 child_pos
.y
+= m_size
.y
- size
.y
;
1472 else if (item
->GetFlag() & (wxCENTER
| wxALIGN_CENTER_VERTICAL
))
1473 // XXX wxCENTER is added for backward compatibility;
1474 // wxALIGN_CENTER should be used in new code
1475 child_pos
.y
+= (m_size
.y
- size
.y
) / 2;
1477 item
->SetDimension( child_pos
, child_size
);
1483 node
= node
->GetNext();
1487 wxSize
wxBoxSizer::CalcMin()
1489 if (m_children
.GetCount() == 0)
1490 return wxSize(10,10);
1498 // precalc item minsizes and count proportions
1499 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
1502 wxSizerItem
*item
= node
->GetData();
1504 if (item
->IsShown())
1505 item
->CalcMin(); // result is stored in the item
1507 if (item
->IsShown() && item
->GetProportion() != 0)
1508 m_stretchable
+= item
->GetProportion();
1510 node
= node
->GetNext();
1513 // Total minimum size (width or height) of sizer
1516 node
= m_children
.GetFirst();
1519 wxSizerItem
*item
= node
->GetData();
1521 if (item
->IsShown() && item
->GetProportion() != 0)
1523 int stretch
= item
->GetProportion();
1524 wxSize
size( item
->GetMinSizeWithBorder() );
1527 // Integer division rounded up is (a + b - 1) / b
1528 // Round up needed in order to guarantee that all
1529 // all items will have size not less then their min size
1530 if (m_orient
== wxHORIZONTAL
)
1531 minSize
= ( size
.x
*m_stretchable
+ stretch
- 1)/stretch
;
1533 minSize
= ( size
.y
*m_stretchable
+ stretch
- 1)/stretch
;
1535 if (minSize
> maxMinSize
)
1536 maxMinSize
= minSize
;
1538 node
= node
->GetNext();
1541 // Calculate overall minimum size
1542 node
= m_children
.GetFirst();
1545 wxSizerItem
*item
= node
->GetData();
1547 if (item
->IsShown())
1549 wxSize
size( item
->GetMinSizeWithBorder() );
1550 if (item
->GetProportion() != 0)
1552 if (m_orient
== wxHORIZONTAL
)
1553 size
.x
= (maxMinSize
*item
->GetProportion())/m_stretchable
;
1555 size
.y
= (maxMinSize
*item
->GetProportion())/m_stretchable
;
1559 if (m_orient
== wxVERTICAL
)
1561 m_fixedHeight
+= size
.y
;
1562 m_fixedWidth
= wxMax( m_fixedWidth
, size
.x
);
1566 m_fixedWidth
+= size
.x
;
1567 m_fixedHeight
= wxMax( m_fixedHeight
, size
.y
);
1571 if (m_orient
== wxHORIZONTAL
)
1573 m_minWidth
+= size
.x
;
1574 m_minHeight
= wxMax( m_minHeight
, size
.y
);
1578 m_minHeight
+= size
.y
;
1579 m_minWidth
= wxMax( m_minWidth
, size
.x
);
1582 node
= node
->GetNext();
1585 return wxSize( m_minWidth
, m_minHeight
);
1588 //---------------------------------------------------------------------------
1590 //---------------------------------------------------------------------------
1594 wxStaticBoxSizer::wxStaticBoxSizer( wxStaticBox
*box
, int orient
)
1595 : wxBoxSizer( orient
)
1596 , m_staticBox( box
)
1598 wxASSERT_MSG( box
, wxT("wxStaticBoxSizer needs a static box") );
1601 static void GetStaticBoxBorders( wxStaticBox
*box
,
1605 // this has to be done platform by platform as there is no way to
1606 // guess the thickness of a wxStaticBox border
1608 box
->GetBordersForSizer(borderTop
,borderOther
);
1609 #elif defined(__WXMAC__)
1611 static int extraTop
= -1; // Uninitted
1612 static int other
= 5;
1614 if ( extraTop
== -1 )
1616 // The minimal border used for the top. Later on the staticbox'
1617 // font height is added to this.
1620 if ( UMAGetSystemVersion() >= 0x1030 /*Panther*/ )
1622 // As indicated by the HIG, Panther needs an extra border of 11
1623 // pixels (otherwise overlapping occurs at the top). The "other"
1624 // border has to be 11.
1631 *borderTop
= extraTop
+ box
->GetCharHeight();
1632 *borderOther
= other
;
1636 if ( box
->GetLabel().IsEmpty() )
1640 *borderTop
= box
->GetCharHeight();
1643 #endif // __WXCOCOA__
1646 void wxStaticBoxSizer::RecalcSizes()
1648 int top_border
, other_border
;
1649 GetStaticBoxBorders(m_staticBox
, &top_border
, &other_border
);
1651 m_staticBox
->SetSize( m_position
.x
, m_position
.y
, m_size
.x
, m_size
.y
);
1653 wxPoint
old_pos( m_position
);
1654 m_position
.x
+= other_border
;
1655 m_position
.y
+= top_border
;
1656 wxSize
old_size( m_size
);
1657 m_size
.x
-= 2*other_border
;
1658 m_size
.y
-= top_border
+ other_border
;
1660 wxBoxSizer::RecalcSizes();
1662 m_position
= old_pos
;
1666 wxSize
wxStaticBoxSizer::CalcMin()
1668 int top_border
, other_border
;
1669 GetStaticBoxBorders(m_staticBox
, &top_border
, &other_border
);
1671 wxSize
ret( wxBoxSizer::CalcMin() );
1672 ret
.x
+= 2*other_border
;
1673 ret
.y
+= other_border
+ top_border
;
1678 void wxStaticBoxSizer::ShowItems( bool show
)
1680 m_staticBox
->Show( show
);
1681 wxBoxSizer::ShowItems( show
);
1684 #endif // wxUSE_STATBOX
1687 #if WXWIN_COMPATIBILITY_2_4
1689 // ----------------------------------------------------------------------------
1691 // ----------------------------------------------------------------------------
1694 IMPLEMENT_CLASS(wxBookCtrlSizer
, wxSizer
)
1696 IMPLEMENT_CLASS(wxNotebookSizer
, wxBookCtrlSizer
)
1697 #endif // wxUSE_NOTEBOOK
1698 #endif // wxUSE_BOOKCTRL
1702 wxBookCtrlSizer::wxBookCtrlSizer(wxBookCtrl
*bookctrl
)
1703 : m_bookctrl(bookctrl
)
1705 wxASSERT_MSG( bookctrl
, wxT("wxBookCtrlSizer needs a control") );
1708 void wxBookCtrlSizer::RecalcSizes()
1710 m_bookctrl
->SetSize( m_position
.x
, m_position
.y
, m_size
.x
, m_size
.y
);
1713 wxSize
wxBookCtrlSizer::CalcMin()
1715 wxSize sizeBorder
= m_bookctrl
->CalcSizeFromPage(wxSize(0, 0));
1720 if ( m_bookctrl
->GetPageCount() == 0 )
1722 return wxSize(sizeBorder
.x
+ 10, sizeBorder
.y
+ 10);
1728 wxWindowList::compatibility_iterator
1729 node
= m_bookctrl
->GetChildren().GetFirst();
1732 wxWindow
*item
= node
->GetData();
1733 wxSizer
*itemsizer
= item
->GetSizer();
1737 wxSize
subsize( itemsizer
->CalcMin() );
1739 if (subsize
.x
> maxX
)
1741 if (subsize
.y
> maxY
)
1745 node
= node
->GetNext();
1748 return wxSize( maxX
, maxY
) + sizeBorder
;
1753 wxNotebookSizer::wxNotebookSizer(wxNotebook
*nb
)
1755 wxASSERT_MSG( nb
, wxT("wxNotebookSizer needs a control") );
1759 #endif // wxUSE_NOTEBOOOK
1760 #endif // wxUSE_BOOKCTRL
1762 #endif // WXWIN_COMPATIBILITY_2_4