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
);
82 //---------------------------------------------------------------------------
84 //---------------------------------------------------------------------------
86 void wxSizerItem::Init()
94 void wxSizerItem::Init(const wxSizerFlags
& flags
)
98 m_proportion
= flags
.GetProportion();
99 m_flag
= flags
.GetFlags();
100 m_border
= flags
.GetBorderInPixels();
103 wxSizerItem::wxSizerItem( int width
, int height
, int proportion
, int flag
, int border
, wxObject
* userData
)
106 , m_size( wxSize( width
, height
) ) // size is set directly
107 , m_minSize( m_size
) // minimal size is the initial size
108 , m_proportion( proportion
)
112 , m_userData( userData
)
117 wxSizerItem::wxSizerItem( wxWindow
*window
, int proportion
, int flag
, int border
, wxObject
* userData
)
120 , m_proportion( proportion
)
124 , m_userData( userData
)
126 if (flag
& wxFIXED_MINSIZE
)
127 window
->SetMinSize(window
->GetSize());
128 m_minSize
= window
->GetSize();
130 // aspect ratio calculated from initial size
131 SetRatio( m_minSize
);
133 // m_size is calculated later
136 wxSizerItem::wxSizerItem( wxSizer
*sizer
, int proportion
, int flag
, int border
, wxObject
* userData
)
139 , m_proportion( proportion
)
144 , m_userData( userData
)
146 // m_minSize is calculated later
147 // m_size is calculated later
150 wxSizerItem::wxSizerItem()
159 wxSizerItem::~wxSizerItem()
165 m_window
->SetContainingSizer(NULL
);
167 else // we must be a sizer
174 wxSize
wxSizerItem::GetSize() const
178 ret
= m_sizer
->GetSize();
181 ret
= m_window
->GetSize();
188 if (m_flag
& wxNORTH
)
190 if (m_flag
& wxSOUTH
)
196 wxSize
wxSizerItem::CalcMin()
200 m_minSize
= m_sizer
->GetMinSize();
202 // if we have to preserve aspect ratio _AND_ this is
203 // the first-time calculation, consider ret to be initial size
204 if ((m_flag
& wxSHAPED
) && !m_ratio
)
207 else if ( IsWindow() )
209 // Since the size of the window may change during runtime, we
210 // should use the current minimal/best size.
211 m_minSize
= m_window
->GetBestFittingSize();
214 return GetMinSizeWithBorder();
217 wxSize
wxSizerItem::GetMinSizeWithBorder() const
219 wxSize ret
= m_minSize
;
225 if (m_flag
& wxNORTH
)
227 if (m_flag
& wxSOUTH
)
234 void wxSizerItem::SetDimension( wxPoint pos
, wxSize size
)
236 if (m_flag
& wxSHAPED
)
238 // adjust aspect ratio
239 int rwidth
= (int) (size
.y
* m_ratio
);
243 int rheight
= (int) (size
.x
/ m_ratio
);
244 // add vertical space
245 if (m_flag
& wxALIGN_CENTER_VERTICAL
)
246 pos
.y
+= (size
.y
- rheight
) / 2;
247 else if (m_flag
& wxALIGN_BOTTOM
)
248 pos
.y
+= (size
.y
- rheight
);
249 // use reduced dimensions
252 else if (rwidth
< size
.x
)
254 // add horizontal space
255 if (m_flag
& wxALIGN_CENTER_HORIZONTAL
)
256 pos
.x
+= (size
.x
- rwidth
) / 2;
257 else if (m_flag
& wxALIGN_RIGHT
)
258 pos
.x
+= (size
.x
- rwidth
);
263 // This is what GetPosition() returns. Since we calculate
264 // borders afterwards, GetPosition() will be the left/top
265 // corner of the surrounding border.
277 if (m_flag
& wxNORTH
)
282 if (m_flag
& wxSOUTH
)
288 m_sizer
->SetDimension( pos
.x
, pos
.y
, size
.x
, size
.y
);
291 m_window
->SetSize( pos
.x
, pos
.y
, size
.x
, size
.y
, wxSIZE_ALLOW_MINUS_ONE
);
296 void wxSizerItem::DeleteWindows()
305 m_sizer
->DeleteWindows();
308 bool wxSizerItem::IsWindow() const
310 return (m_window
!= NULL
);
313 bool wxSizerItem::IsSizer() const
315 return (m_sizer
!= NULL
);
318 bool wxSizerItem::IsSpacer() const
320 return (m_window
== NULL
) && (m_sizer
== NULL
);
323 void wxSizerItem::Show( bool show
)
328 m_window
->Show( show
);
330 m_sizer
->ShowItems( show
);
332 // ... nothing else to do to hide/show spacers
335 void wxSizerItem::SetOption( int option
)
337 SetProportion( option
);
340 int wxSizerItem::GetOption() const
342 return GetProportion();
346 //---------------------------------------------------------------------------
348 //---------------------------------------------------------------------------
351 : m_minSize( wxSize( 0, 0 ) )
357 WX_CLEAR_LIST(wxSizerItemList
, m_children
);
360 void wxSizer::Insert( size_t index
, wxSizerItem
*item
)
362 m_children
.Insert( index
, item
);
364 if( item
->GetWindow() )
365 item
->GetWindow()->SetContainingSizer( this );
368 bool wxSizer::Remove( wxWindow
*window
)
370 return Detach( window
);
373 bool wxSizer::Remove( wxSizer
*sizer
)
375 wxASSERT_MSG( sizer
, _T("Removing NULL sizer") );
377 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
380 wxSizerItem
*item
= node
->GetData();
382 if (item
->GetSizer() == sizer
)
385 m_children
.Erase( node
);
389 node
= node
->GetNext();
395 bool wxSizer::Remove( int index
)
397 wxCHECK_MSG( index
>= 0 && (size_t)index
< m_children
.GetCount(),
399 _T("Remove index is out of range") );
401 wxSizerItemList::compatibility_iterator node
= m_children
.Item( index
);
403 wxCHECK_MSG( node
, false, _T("Failed to find child node") );
405 wxSizerItem
*item
= node
->GetData();
407 if( item
->IsWindow() )
408 item
->GetWindow()->SetContainingSizer( NULL
);
411 m_children
.Erase( node
);
415 bool wxSizer::Detach( wxSizer
*sizer
)
417 wxASSERT_MSG( sizer
, _T("Detaching NULL sizer") );
419 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
422 wxSizerItem
*item
= node
->GetData();
424 if (item
->GetSizer() == sizer
)
428 m_children
.Erase( node
);
431 node
= node
->GetNext();
437 bool wxSizer::Detach( wxWindow
*window
)
439 wxASSERT_MSG( window
, _T("Detaching NULL window") );
441 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
444 wxSizerItem
*item
= node
->GetData();
446 if (item
->GetWindow() == window
)
448 item
->GetWindow()->SetContainingSizer( NULL
);
450 m_children
.Erase( node
);
453 node
= node
->GetNext();
459 bool wxSizer::Detach( int index
)
461 wxCHECK_MSG( index
>= 0 && (size_t)index
< m_children
.GetCount(),
463 _T("Detach index is out of range") );
465 wxSizerItemList::compatibility_iterator node
= m_children
.Item( index
);
467 wxCHECK_MSG( node
, false, _T("Failed to find child node") );
469 wxSizerItem
*item
= node
->GetData();
471 if( item
->IsSizer() )
473 else if( item
->IsWindow() )
474 item
->GetWindow()->SetContainingSizer( NULL
);
477 m_children
.Erase( node
);
481 void wxSizer::Clear( bool delete_windows
)
483 // First clear the ContainingSizer pointers
484 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
487 wxSizerItem
*item
= node
->GetData();
489 if (item
->IsWindow())
490 item
->GetWindow()->SetContainingSizer( NULL
);
491 node
= node
->GetNext();
494 // Destroy the windows if needed
498 // Now empty the list
499 WX_CLEAR_LIST(wxSizerItemList
, m_children
);
502 void wxSizer::DeleteWindows()
504 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
507 wxSizerItem
*item
= node
->GetData();
509 item
->DeleteWindows();
510 node
= node
->GetNext();
514 wxSize
wxSizer::Fit( wxWindow
*window
)
516 wxSize
size(window
->IsTopLevel() ? FitSize(window
)
517 : GetMinWindowSize(window
));
519 window
->SetSize( size
);
524 void wxSizer::FitInside( wxWindow
*window
)
527 if (window
->IsTopLevel())
528 size
= VirtualFitSize( window
);
530 size
= GetMinClientSize( window
);
532 window
->SetVirtualSize( size
);
535 void wxSizer::Layout()
537 // (re)calculates minimums needed for each item and other preparations
541 // Applies the layout and repositions/resizes the items
545 void wxSizer::SetSizeHints( wxWindow
*window
)
547 // Preserve the window's max size hints, but set the
548 // lower bound according to the sizer calculations.
550 wxSize size
= Fit( window
);
552 window
->SetSizeHints( size
.x
,
554 window
->GetMaxWidth(),
555 window
->GetMaxHeight() );
558 void wxSizer::SetVirtualSizeHints( wxWindow
*window
)
560 // Preserve the window's max size hints, but set the
561 // lower bound according to the sizer calculations.
564 wxSize
size( window
->GetVirtualSize() );
565 window
->SetVirtualSizeHints( size
.x
,
567 window
->GetMaxWidth(),
568 window
->GetMaxHeight() );
571 wxSize
wxSizer::GetMaxWindowSize( wxWindow
*window
) const
573 return window
->GetMaxSize();
576 wxSize
wxSizer::GetMinWindowSize( wxWindow
*window
)
578 wxSize
minSize( GetMinSize() );
579 wxSize
size( window
->GetSize() );
580 wxSize
client_size( window
->GetClientSize() );
582 return wxSize( minSize
.x
+size
.x
-client_size
.x
,
583 minSize
.y
+size
.y
-client_size
.y
);
586 // TODO on mac we need a function that determines how much free space this
587 // min size contains, in order to make sure that we have 20 pixels of free
588 // space around the controls
590 // Return a window size that will fit within the screens dimensions
591 wxSize
wxSizer::FitSize( wxWindow
*window
)
593 wxSize size
= GetMinWindowSize( window
);
594 wxSize sizeMax
= GetMaxWindowSize( window
);
596 // Limit the size if sizeMax != wxDefaultSize
598 if ( size
.x
> sizeMax
.x
&& sizeMax
.x
!= wxDefaultCoord
)
600 if ( size
.y
> sizeMax
.y
&& sizeMax
.y
!= wxDefaultCoord
)
606 wxSize
wxSizer::GetMaxClientSize( wxWindow
*window
) const
608 wxSize
maxSize( window
->GetMaxSize() );
610 if( maxSize
!= wxDefaultSize
)
612 wxSize
size( window
->GetSize() );
613 wxSize
client_size( window
->GetClientSize() );
615 return wxSize( maxSize
.x
+ client_size
.x
- size
.x
,
616 maxSize
.y
+ client_size
.y
- size
.y
);
619 return wxDefaultSize
;
622 wxSize
wxSizer::GetMinClientSize( wxWindow
*WXUNUSED(window
) )
624 return GetMinSize(); // Already returns client size.
627 wxSize
wxSizer::VirtualFitSize( wxWindow
*window
)
629 wxSize size
= GetMinClientSize( window
);
630 wxSize sizeMax
= GetMaxClientSize( window
);
632 // Limit the size if sizeMax != wxDefaultSize
634 if ( size
.x
> sizeMax
.x
&& sizeMax
.x
!= wxDefaultCoord
)
636 if ( size
.y
> sizeMax
.y
&& sizeMax
.y
!= wxDefaultCoord
)
642 void wxSizer::SetDimension( int x
, int y
, int width
, int height
)
651 wxSize
wxSizer::GetMinSize()
653 wxSize
ret( CalcMin() );
654 if (ret
.x
< m_minSize
.x
) ret
.x
= m_minSize
.x
;
655 if (ret
.y
< m_minSize
.y
) ret
.y
= m_minSize
.y
;
659 void wxSizer::DoSetMinSize( int width
, int height
)
662 m_minSize
.y
= height
;
665 bool wxSizer::DoSetItemMinSize( wxWindow
*window
, int width
, int height
)
667 wxASSERT_MSG( window
, _T("SetMinSize for NULL window") );
669 // Is it our immediate child?
671 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
674 wxSizerItem
*item
= node
->GetData();
676 if (item
->GetWindow() == window
)
678 item
->SetMinSize( width
, height
);
681 node
= node
->GetNext();
684 // No? Search any subsizers we own then
686 node
= m_children
.GetFirst();
689 wxSizerItem
*item
= node
->GetData();
691 if ( item
->GetSizer() &&
692 item
->GetSizer()->DoSetItemMinSize( window
, width
, height
) )
694 // A child sizer found the requested windw, exit.
697 node
= node
->GetNext();
703 bool wxSizer::DoSetItemMinSize( wxSizer
*sizer
, int width
, int height
)
705 wxASSERT_MSG( sizer
, _T("SetMinSize for NULL sizer") );
707 // Is it our immediate child?
709 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
712 wxSizerItem
*item
= node
->GetData();
714 if (item
->GetSizer() == sizer
)
716 item
->GetSizer()->DoSetMinSize( width
, height
);
719 node
= node
->GetNext();
722 // No? Search any subsizers we own then
724 node
= m_children
.GetFirst();
727 wxSizerItem
*item
= node
->GetData();
729 if ( item
->GetSizer() &&
730 item
->GetSizer()->DoSetItemMinSize( sizer
, width
, height
) )
732 // A child found the requested sizer, exit.
735 node
= node
->GetNext();
741 bool wxSizer::DoSetItemMinSize( size_t index
, int width
, int height
)
743 wxSizerItemList::compatibility_iterator node
= m_children
.Item( index
);
745 wxCHECK_MSG( node
, false, _T("Failed to find child node") );
747 wxSizerItem
*item
= node
->GetData();
749 if (item
->GetSizer())
751 // Sizers contains the minimal size in them, if not calculated ...
752 item
->GetSizer()->DoSetMinSize( width
, height
);
756 // ... but the minimal size of spacers and windows is stored via the item
757 item
->SetMinSize( width
, height
);
763 bool wxSizer::Show( wxWindow
*window
, bool show
, bool recursive
)
765 wxASSERT_MSG( window
, _T("Show for NULL window") );
767 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
770 wxSizerItem
*item
= node
->GetData();
772 if (item
->GetWindow() == window
)
778 else if (recursive
&& item
->IsSizer())
780 if (item
->GetSizer()->Show(window
, show
, recursive
))
784 node
= node
->GetNext();
790 bool wxSizer::Show( wxSizer
*sizer
, bool show
, bool recursive
)
792 wxASSERT_MSG( sizer
, _T("Show for NULL sizer") );
794 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
797 wxSizerItem
*item
= node
->GetData();
799 if (item
->GetSizer() == sizer
)
805 else if (recursive
&& item
->IsSizer())
807 if (item
->GetSizer()->Show(sizer
, show
, recursive
))
811 node
= node
->GetNext();
817 bool wxSizer::Show( size_t index
, bool show
)
819 wxCHECK_MSG( index
< m_children
.GetCount(),
821 _T("Show index is out of range") );
823 m_children
.Item( index
)->GetData()->Show( show
);
828 void wxSizer::ShowItems( bool show
)
830 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
833 node
->GetData()->Show( show
);
834 node
= node
->GetNext();
838 bool wxSizer::IsShown( wxWindow
*window
) const
840 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
843 wxSizerItem
*item
= node
->GetData();
845 if (item
->GetWindow() == window
)
847 return item
->IsShown();
849 node
= node
->GetNext();
852 wxFAIL_MSG( _T("IsShown failed to find sizer item") );
857 bool wxSizer::IsShown( wxSizer
*sizer
) const
859 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
862 wxSizerItem
*item
= node
->GetData();
864 if (item
->GetSizer() == sizer
)
866 return item
->IsShown();
868 node
= node
->GetNext();
871 wxFAIL_MSG( _T("IsShown failed to find sizer item") );
876 bool wxSizer::IsShown( size_t index
) const
878 wxCHECK_MSG( index
< m_children
.GetCount(),
880 _T("IsShown index is out of range") );
882 return m_children
.Item( index
)->GetData()->IsShown();
886 //---------------------------------------------------------------------------
888 //---------------------------------------------------------------------------
890 wxGridSizer::wxGridSizer( int rows
, int cols
, int vgap
, int hgap
)
896 if (m_rows
== 0 && m_cols
== 0)
900 wxGridSizer::wxGridSizer( int cols
, int vgap
, int hgap
)
906 if (m_rows
== 0 && m_cols
== 0)
910 int wxGridSizer::CalcRowsCols(int& nrows
, int& ncols
) const
912 int nitems
= m_children
.GetCount();
918 nrows
= (nitems
+ m_cols
- 1) / m_cols
;
922 ncols
= (nitems
+ m_rows
- 1) / m_rows
;
925 else // 0 columns, 0 rows?
927 wxFAIL_MSG( _T("grid sizer must have either rows or columns fixed") );
936 void wxGridSizer::RecalcSizes()
938 int nitems
, nrows
, ncols
;
939 if ( (nitems
= CalcRowsCols(nrows
, ncols
)) == 0 )
942 wxSize
sz( GetSize() );
943 wxPoint
pt( GetPosition() );
945 int w
= (sz
.x
- (ncols
- 1) * m_hgap
) / ncols
;
946 int h
= (sz
.y
- (nrows
- 1) * m_vgap
) / nrows
;
949 for (int c
= 0; c
< ncols
; c
++)
952 for (int r
= 0; r
< nrows
; r
++)
954 int i
= r
* ncols
+ c
;
957 wxSizerItemList::compatibility_iterator node
= m_children
.Item( i
);
959 wxASSERT_MSG( node
, _T("Failed to find SizerItemList node") );
961 SetItemBounds( node
->GetData(), x
, y
, w
, h
);
969 wxSize
wxGridSizer::CalcMin()
972 if ( CalcRowsCols(nrows
, ncols
) == 0 )
973 return wxSize(10, 10);
975 // Find the max width and height for any component
979 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
982 wxSizerItem
*item
= node
->GetData();
983 wxSize
sz( item
->CalcMin() );
985 w
= wxMax( w
, sz
.x
);
986 h
= wxMax( h
, sz
.y
);
988 node
= node
->GetNext();
991 return wxSize( ncols
* w
+ (ncols
-1) * m_hgap
,
992 nrows
* h
+ (nrows
-1) * m_vgap
);
995 void wxGridSizer::SetItemBounds( wxSizerItem
*item
, int x
, int y
, int w
, int h
)
998 wxSize
sz( item
->GetMinSizeWithBorder() );
999 int flag
= item
->GetFlag();
1001 if ((flag
& wxEXPAND
) || (flag
& wxSHAPED
))
1007 if (flag
& wxALIGN_CENTER_HORIZONTAL
)
1009 pt
.x
= x
+ (w
- sz
.x
) / 2;
1011 else if (flag
& wxALIGN_RIGHT
)
1013 pt
.x
= x
+ (w
- sz
.x
);
1016 if (flag
& wxALIGN_CENTER_VERTICAL
)
1018 pt
.y
= y
+ (h
- sz
.y
) / 2;
1020 else if (flag
& wxALIGN_BOTTOM
)
1022 pt
.y
= y
+ (h
- sz
.y
);
1026 item
->SetDimension(pt
, sz
);
1029 //---------------------------------------------------------------------------
1031 //---------------------------------------------------------------------------
1033 wxFlexGridSizer::wxFlexGridSizer( int rows
, int cols
, int vgap
, int hgap
)
1034 : wxGridSizer( rows
, cols
, vgap
, hgap
),
1035 m_flexDirection(wxBOTH
),
1036 m_growMode(wxFLEX_GROWMODE_SPECIFIED
)
1040 wxFlexGridSizer::wxFlexGridSizer( int cols
, int vgap
, int hgap
)
1041 : wxGridSizer( cols
, vgap
, hgap
),
1042 m_flexDirection(wxBOTH
),
1043 m_growMode(wxFLEX_GROWMODE_SPECIFIED
)
1047 wxFlexGridSizer::~wxFlexGridSizer()
1051 void wxFlexGridSizer::RecalcSizes()
1053 int nitems
, nrows
, ncols
;
1054 if ( (nitems
= CalcRowsCols(nrows
, ncols
)) == 0 )
1057 wxPoint
pt( GetPosition() );
1058 wxSize
sz( GetSize() );
1060 AdjustForGrowables(sz
, m_calculatedMinSize
, nrows
, ncols
);
1062 sz
= wxSize( pt
.x
+ sz
.x
, pt
.y
+ sz
.y
);
1065 for (int c
= 0; c
< ncols
; c
++)
1068 for (int r
= 0; r
< nrows
; r
++)
1070 int i
= r
* ncols
+ c
;
1073 wxSizerItemList::compatibility_iterator node
= m_children
.Item( i
);
1075 wxASSERT_MSG( node
, _T("Failed to find node") );
1077 int w
= wxMax( 0, wxMin( m_colWidths
[c
], sz
.x
- x
) );
1078 int h
= wxMax( 0, wxMin( m_rowHeights
[r
], sz
.y
- y
) );
1080 SetItemBounds( node
->GetData(), x
, y
, w
, h
);
1082 y
= y
+ m_rowHeights
[r
] + m_vgap
;
1084 x
= x
+ m_colWidths
[c
] + m_hgap
;
1088 wxSize
wxFlexGridSizer::CalcMin()
1094 // Number of rows/columns can change as items are added or removed.
1095 if ( !CalcRowsCols(nrows
, ncols
) )
1096 return wxSize(10, 10);
1098 m_rowHeights
.SetCount(nrows
);
1099 m_colWidths
.SetCount(ncols
);
1101 // We have to recalcuate the sizes in case the item minimum size has
1102 // changed since the previous layout, or the item has been hidden using
1103 // wxSizer::Show(). If all the items in a row/column are hidden, the final
1104 // dimension of the row/column will be -1, indicating that the column
1105 // itself is hidden.
1106 for( s
= m_rowHeights
.GetCount(), i
= 0; i
< s
; ++i
)
1107 m_rowHeights
[ i
] = -1;
1108 for( s
= m_colWidths
.GetCount(), i
= 0; i
< s
; ++i
)
1109 m_colWidths
[ i
] = -1;
1111 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
1116 wxSizerItem
*item
= node
->GetData();
1117 if ( item
->IsShown() )
1119 wxSize
sz( item
->CalcMin() );
1120 int row
= i
/ ncols
;
1121 int col
= i
% ncols
;
1123 m_rowHeights
[ row
] = wxMax( wxMax( 0, sz
.y
), m_rowHeights
[ row
] );
1124 m_colWidths
[ col
] = wxMax( wxMax( 0, sz
.x
), m_colWidths
[ col
] );
1127 node
= node
->GetNext();
1131 AdjustForFlexDirection();
1133 // Sum total minimum size, including gaps between rows/columns.
1134 // -1 is used as a magic number meaning empty column.
1136 for (int col
= 0; col
< ncols
; col
++)
1137 if ( m_colWidths
[ col
] != -1 )
1138 width
+= m_colWidths
[ col
] + ( col
== ncols
-1 ? 0 : m_hgap
);
1141 for (int row
= 0; row
< nrows
; row
++)
1142 if ( m_rowHeights
[ row
] != -1 )
1143 height
+= m_rowHeights
[ row
] + ( row
== nrows
-1 ? 0 : m_vgap
);
1145 m_calculatedMinSize
= wxSize( width
, height
);
1146 return m_calculatedMinSize
;
1149 void wxFlexGridSizer::AdjustForFlexDirection()
1151 // the logic in CalcMin works when we resize flexibly in both directions
1152 // but maybe this is not the case
1153 if ( m_flexDirection
!= wxBOTH
)
1155 // select the array corresponding to the direction in which we do *not*
1157 wxArrayInt
& array
= m_flexDirection
== wxVERTICAL
? m_colWidths
1160 const int count
= array
.GetCount();
1162 // find the largest value in this array
1164 for ( n
= 0; n
< count
; ++n
)
1166 if ( array
[n
] > largest
)
1170 // and now fill it with the largest value
1171 for ( n
= 0; n
< count
; ++n
)
1179 void wxFlexGridSizer::AdjustForGrowables(const wxSize
& sz
, const wxSize
& minsz
,
1180 int nrows
, int ncols
)
1182 // what to do with the rows? by default, resize them proportionally
1183 if ( sz
.y
> minsz
.y
&& ( (m_flexDirection
& wxVERTICAL
) || (m_growMode
== wxFLEX_GROWMODE_SPECIFIED
) ) )
1185 int sum_proportions
= 0;
1186 int growable_space
= 0;
1189 for (idx
= 0; idx
< m_growableRows
.GetCount(); idx
++)
1191 // Since the number of rows/columns can change as items are
1192 // inserted/deleted, we need to verify at runtime that the
1193 // requested growable rows/columns are still valid.
1194 if (m_growableRows
[idx
] >= nrows
)
1197 // If all items in a row/column are hidden, that row/column will
1198 // have a dimension of -1. This causes the row/column to be
1199 // hidden completely.
1200 if (m_rowHeights
[ m_growableRows
[idx
] ] == -1)
1202 sum_proportions
+= m_growableRowsProportions
[idx
];
1203 growable_space
+= m_rowHeights
[ m_growableRows
[idx
] ];
1209 for (idx
= 0; idx
< m_growableRows
.GetCount(); idx
++)
1211 if (m_growableRows
[idx
] >= nrows
)
1213 if (m_rowHeights
[ m_growableRows
[idx
] ] == -1)
1214 m_rowHeights
[ m_growableRows
[idx
] ] = 0;
1217 int delta
= (sz
.y
- minsz
.y
);
1218 if (sum_proportions
== 0)
1219 delta
= (delta
/num
) + m_rowHeights
[ m_growableRows
[idx
] ];
1221 delta
= ((delta
+growable_space
)*m_growableRowsProportions
[idx
]) / sum_proportions
;
1222 m_rowHeights
[ m_growableRows
[idx
] ] = delta
;
1227 else if ( (m_growMode
== wxFLEX_GROWMODE_ALL
) && (sz
.y
> minsz
.y
) )
1229 // rounding problem?
1230 for ( int row
= 0; row
< nrows
; ++row
)
1231 m_rowHeights
[ row
] = sz
.y
/ nrows
;
1234 // the same logic as above but for the columns
1235 if ( sz
.x
> minsz
.x
&& ( (m_flexDirection
& wxHORIZONTAL
) || (m_growMode
== wxFLEX_GROWMODE_SPECIFIED
) ) )
1237 int sum_proportions
= 0;
1238 int growable_space
= 0;
1241 for (idx
= 0; idx
< m_growableCols
.GetCount(); idx
++)
1243 // Since the number of rows/columns can change as items are
1244 // inserted/deleted, we need to verify at runtime that the
1245 // requested growable rows/columns are still valid.
1246 if (m_growableCols
[idx
] >= ncols
)
1249 // If all items in a row/column are hidden, that row/column will
1250 // have a dimension of -1. This causes the column to be hidden
1252 if (m_colWidths
[ m_growableCols
[idx
] ] == -1)
1254 sum_proportions
+= m_growableColsProportions
[idx
];
1255 growable_space
+= m_colWidths
[ m_growableCols
[idx
] ];
1261 for (idx
= 0; idx
< m_growableCols
.GetCount(); idx
++)
1263 if (m_growableCols
[idx
] >= ncols
)
1265 if (m_colWidths
[ m_growableCols
[idx
] ] == -1)
1266 m_colWidths
[ m_growableCols
[idx
] ] = 0;
1269 int delta
= (sz
.x
- minsz
.x
);
1270 if (sum_proportions
== 0)
1271 delta
= (delta
/num
) + m_colWidths
[ m_growableCols
[idx
] ];
1273 delta
= ((delta
+growable_space
)*m_growableColsProportions
[idx
])/sum_proportions
;
1274 m_colWidths
[ m_growableCols
[idx
] ] = delta
;
1279 else if ( (m_growMode
== wxFLEX_GROWMODE_ALL
) && (sz
.x
> minsz
.x
) )
1281 for ( int col
=0; col
< ncols
; ++col
)
1282 m_colWidths
[ col
] = sz
.x
/ ncols
;
1287 void wxFlexGridSizer::AddGrowableRow( size_t idx
, int proportion
)
1289 m_growableRows
.Add( idx
);
1290 m_growableRowsProportions
.Add( proportion
);
1293 void wxFlexGridSizer::RemoveGrowableRow( size_t idx
)
1295 m_growableRows
.Remove( idx
);
1298 void wxFlexGridSizer::AddGrowableCol( size_t idx
, int proportion
)
1300 m_growableCols
.Add( idx
);
1301 m_growableColsProportions
.Add( proportion
);
1304 void wxFlexGridSizer::RemoveGrowableCol( size_t idx
)
1306 m_growableCols
.Remove( idx
);
1309 //---------------------------------------------------------------------------
1311 //---------------------------------------------------------------------------
1313 wxBoxSizer::wxBoxSizer( int orient
)
1314 : m_orient( orient
)
1318 void wxBoxSizer::RecalcSizes()
1320 if (m_children
.GetCount() == 0)
1326 if (m_orient
== wxHORIZONTAL
)
1327 delta
= m_size
.x
- m_fixedWidth
;
1329 delta
= m_size
.y
- m_fixedHeight
;
1332 wxPoint
pt( m_position
);
1334 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
1337 wxSizerItem
*item
= node
->GetData();
1339 if (item
->IsShown())
1341 wxSize
size( item
->GetMinSizeWithBorder() );
1343 if (m_orient
== wxVERTICAL
)
1345 wxCoord height
= size
.y
;
1346 if (item
->GetProportion())
1348 // Because of at least one visible item has non-zero
1349 // proportion then m_stretchable is not zero
1350 height
= (delta
* item
->GetProportion()) / m_stretchable
;
1353 wxPoint
child_pos( pt
);
1354 wxSize
child_size( wxSize( size
.x
, height
) );
1356 if (item
->GetFlag() & (wxEXPAND
| wxSHAPED
))
1357 child_size
.x
= m_size
.x
;
1358 else if (item
->GetFlag() & wxALIGN_RIGHT
)
1359 child_pos
.x
+= m_size
.x
- size
.x
;
1360 else if (item
->GetFlag() & (wxCENTER
| wxALIGN_CENTER_HORIZONTAL
))
1361 // XXX wxCENTER is added for backward compatibility;
1362 // wxALIGN_CENTER should be used in new code
1363 child_pos
.x
+= (m_size
.x
- size
.x
) / 2;
1365 item
->SetDimension( child_pos
, child_size
);
1371 wxCoord width
= size
.x
;
1372 if (item
->GetProportion())
1374 // Because of at least one visible item has non-zero
1375 // proportion then m_stretchable is not zero
1376 width
= (delta
* item
->GetProportion()) / m_stretchable
;
1379 wxPoint
child_pos( pt
);
1380 wxSize
child_size( wxSize(width
, size
.y
) );
1382 if (item
->GetFlag() & (wxEXPAND
| wxSHAPED
))
1383 child_size
.y
= m_size
.y
;
1384 else if (item
->GetFlag() & wxALIGN_BOTTOM
)
1385 child_pos
.y
+= m_size
.y
- size
.y
;
1386 else if (item
->GetFlag() & (wxCENTER
| wxALIGN_CENTER_VERTICAL
))
1387 // XXX wxCENTER is added for backward compatibility;
1388 // wxALIGN_CENTER should be used in new code
1389 child_pos
.y
+= (m_size
.y
- size
.y
) / 2;
1391 item
->SetDimension( child_pos
, child_size
);
1397 node
= node
->GetNext();
1401 wxSize
wxBoxSizer::CalcMin()
1403 if (m_children
.GetCount() == 0)
1404 return wxSize(10,10);
1412 // precalc item minsizes and count proportions
1413 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
1416 wxSizerItem
*item
= node
->GetData();
1418 if (item
->IsShown())
1419 item
->CalcMin(); // result is stored in the item
1421 if (item
->IsShown() && item
->GetProportion() != 0)
1422 m_stretchable
+= item
->GetProportion();
1424 node
= node
->GetNext();
1427 // Total minimum size (width or height) of sizer
1430 node
= m_children
.GetFirst();
1433 wxSizerItem
*item
= node
->GetData();
1435 if (item
->IsShown() && item
->GetProportion() != 0)
1437 int stretch
= item
->GetProportion();
1438 wxSize
size( item
->GetMinSizeWithBorder() );
1441 // Integer division rounded up is (a + b - 1) / b
1442 // Round up needed in order to guarantee that all
1443 // all items will have size not less then their min size
1444 if (m_orient
== wxHORIZONTAL
)
1445 minSize
= ( size
.x
*m_stretchable
+ stretch
- 1)/stretch
;
1447 minSize
= ( size
.y
*m_stretchable
+ stretch
- 1)/stretch
;
1449 if (minSize
> maxMinSize
)
1450 maxMinSize
= minSize
;
1452 node
= node
->GetNext();
1455 // Calculate overall minimum size
1456 node
= m_children
.GetFirst();
1459 wxSizerItem
*item
= node
->GetData();
1461 if (item
->IsShown())
1463 wxSize
size( item
->GetMinSizeWithBorder() );
1464 if (item
->GetProportion() != 0)
1466 if (m_orient
== wxHORIZONTAL
)
1467 size
.x
= (maxMinSize
*item
->GetProportion())/m_stretchable
;
1469 size
.y
= (maxMinSize
*item
->GetProportion())/m_stretchable
;
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 if (m_orient
== wxHORIZONTAL
)
1487 m_minWidth
+= size
.x
;
1488 m_minHeight
= wxMax( m_minHeight
, size
.y
);
1492 m_minHeight
+= size
.y
;
1493 m_minWidth
= wxMax( m_minWidth
, size
.x
);
1496 node
= node
->GetNext();
1499 return wxSize( m_minWidth
, m_minHeight
);
1502 //---------------------------------------------------------------------------
1504 //---------------------------------------------------------------------------
1508 wxStaticBoxSizer::wxStaticBoxSizer( wxStaticBox
*box
, int orient
)
1509 : wxBoxSizer( orient
)
1510 , m_staticBox( box
)
1512 wxASSERT_MSG( box
, wxT("wxStaticBoxSizer needs a static box") );
1515 static void GetStaticBoxBorders( wxStaticBox
*box
,
1519 // this has to be done platform by platform as there is no way to
1520 // guess the thickness of a wxStaticBox border
1522 box
->GetBordersForSizer(borderTop
,borderOther
);
1523 #elif defined(__WXMAC__)
1525 static int extraTop
= -1; // Uninitted
1526 static int other
= 5;
1528 if ( extraTop
== -1 )
1530 // The minimal border used for the top. Later on the staticbox'
1531 // font height is added to this.
1534 if ( UMAGetSystemVersion() >= 0x1030 /*Panther*/ )
1536 // As indicated by the HIG, Panther needs an extra border of 11
1537 // pixels (otherwise overlapping occurs at the top). The "other"
1538 // border has to be 11.
1545 *borderTop
= extraTop
+ box
->GetCharHeight();
1546 *borderOther
= other
;
1550 if ( box
->GetLabel().IsEmpty() )
1554 *borderTop
= box
->GetCharHeight();
1557 #endif // __WXCOCOA__
1560 void wxStaticBoxSizer::RecalcSizes()
1562 int top_border
, other_border
;
1563 GetStaticBoxBorders(m_staticBox
, &top_border
, &other_border
);
1565 m_staticBox
->SetSize( m_position
.x
, m_position
.y
, m_size
.x
, m_size
.y
);
1567 wxPoint
old_pos( m_position
);
1568 m_position
.x
+= other_border
;
1569 m_position
.y
+= top_border
;
1570 wxSize
old_size( m_size
);
1571 m_size
.x
-= 2*other_border
;
1572 m_size
.y
-= top_border
+ other_border
;
1574 wxBoxSizer::RecalcSizes();
1576 m_position
= old_pos
;
1580 wxSize
wxStaticBoxSizer::CalcMin()
1582 int top_border
, other_border
;
1583 GetStaticBoxBorders(m_staticBox
, &top_border
, &other_border
);
1585 wxSize
ret( wxBoxSizer::CalcMin() );
1586 ret
.x
+= 2*other_border
;
1587 ret
.y
+= other_border
+ top_border
;
1592 void wxStaticBoxSizer::ShowItems( bool show
)
1594 m_staticBox
->Show( show
);
1595 wxBoxSizer::ShowItems( show
);
1598 #endif // wxUSE_STATBOX
1601 #if WXWIN_COMPATIBILITY_2_4
1603 // ----------------------------------------------------------------------------
1605 // ----------------------------------------------------------------------------
1608 IMPLEMENT_CLASS(wxBookCtrlSizer
, wxSizer
)
1610 IMPLEMENT_CLASS(wxNotebookSizer
, wxBookCtrlSizer
)
1611 #endif // wxUSE_NOTEBOOK
1612 #endif // wxUSE_BOOKCTRL
1616 wxBookCtrlSizer::wxBookCtrlSizer(wxBookCtrl
*bookctrl
)
1617 : m_bookctrl(bookctrl
)
1619 wxASSERT_MSG( bookctrl
, wxT("wxBookCtrlSizer needs a control") );
1622 void wxBookCtrlSizer::RecalcSizes()
1624 m_bookctrl
->SetSize( m_position
.x
, m_position
.y
, m_size
.x
, m_size
.y
);
1627 wxSize
wxBookCtrlSizer::CalcMin()
1629 wxSize sizeBorder
= m_bookctrl
->CalcSizeFromPage(wxSize(0, 0));
1634 if ( m_bookctrl
->GetPageCount() == 0 )
1636 return wxSize(sizeBorder
.x
+ 10, sizeBorder
.y
+ 10);
1642 wxWindowList::compatibility_iterator
1643 node
= m_bookctrl
->GetChildren().GetFirst();
1646 wxWindow
*item
= node
->GetData();
1647 wxSizer
*itemsizer
= item
->GetSizer();
1651 wxSize
subsize( itemsizer
->CalcMin() );
1653 if (subsize
.x
> maxX
)
1655 if (subsize
.y
> maxY
)
1659 node
= node
->GetNext();
1662 return wxSize( maxX
, maxY
) + sizeBorder
;
1667 wxNotebookSizer::wxNotebookSizer(wxNotebook
*nb
)
1669 wxASSERT_MSG( nb
, wxT("wxNotebookSizer needs a control") );
1673 #endif // wxUSE_NOTEBOOOK
1674 #endif // wxUSE_BOOKCTRL
1676 #endif // WXWIN_COMPATIBILITY_2_4