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 m_children
.Append( new wxSizerItem( window
, proportion
, flag
, border
, userData
) );
348 window
->SetContainingSizer( this );
351 void wxSizer::Add( wxSizer
*sizer
, int proportion
, int flag
, int border
, wxObject
* userData
)
353 m_children
.Append( new wxSizerItem( sizer
, proportion
, flag
, border
, userData
) );
356 void wxSizer::Add( int width
, int height
, int proportion
, int flag
, int border
, wxObject
* userData
)
358 m_children
.Append( new wxSizerItem( width
, height
, proportion
, flag
, border
, userData
) );
361 void wxSizer::Add( wxSizerItem
*item
)
363 m_children
.Append( item
);
365 if( item
->GetWindow() )
366 item
->GetWindow()->SetContainingSizer( this );
369 void wxSizer::Prepend( wxWindow
*window
, int proportion
, int flag
, int border
, wxObject
* userData
)
371 m_children
.Insert( new wxSizerItem( window
, proportion
, flag
, border
, userData
) );
372 window
->SetContainingSizer( this );
375 void wxSizer::Prepend( wxSizer
*sizer
, int proportion
, int flag
, int border
, wxObject
* userData
)
377 m_children
.Insert( new wxSizerItem( sizer
, proportion
, flag
, border
, userData
) );
380 void wxSizer::Prepend( int width
, int height
, int proportion
, int flag
, int border
, wxObject
* userData
)
382 m_children
.Insert( new wxSizerItem( width
, height
, proportion
, flag
, border
, userData
) );
385 void wxSizer::Prepend( wxSizerItem
*item
)
387 m_children
.Insert( item
);
389 if( item
->GetWindow() )
390 item
->GetWindow()->SetContainingSizer( this );
393 void wxSizer::Insert( size_t index
,
400 m_children
.Insert( index
,
401 new wxSizerItem( window
, proportion
, flag
, border
, userData
) );
402 window
->SetContainingSizer( this );
405 void wxSizer::Insert( size_t index
,
412 m_children
.Insert( index
,
413 new wxSizerItem( sizer
, proportion
, flag
, border
, userData
) );
416 void wxSizer::Insert( size_t index
,
424 m_children
.Insert( index
,
425 new wxSizerItem( width
, height
, proportion
, flag
, border
, userData
) );
428 void wxSizer::Insert( size_t index
, wxSizerItem
*item
)
430 m_children
.Insert( index
, item
);
432 if( item
->GetWindow() )
433 item
->GetWindow()->SetContainingSizer( this );
436 bool wxSizer::Remove( wxWindow
*window
)
438 return Detach( window
);
441 bool wxSizer::Remove( wxSizer
*sizer
)
443 wxASSERT_MSG( sizer
, _T("Removing NULL sizer") );
445 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
448 wxSizerItem
*item
= node
->GetData();
450 if (item
->GetSizer() == sizer
)
453 m_children
.Erase( node
);
457 node
= node
->GetNext();
463 bool wxSizer::Remove( int index
)
465 wxCHECK_MSG( index
>= 0 && (size_t)index
< m_children
.GetCount(),
467 _T("Remove index is out of range") );
469 wxSizerItemList::compatibility_iterator node
= m_children
.Item( index
);
471 wxCHECK_MSG( node
, false, _T("Failed to find child node") );
473 wxSizerItem
*item
= node
->GetData();
475 if( item
->IsWindow() )
476 item
->GetWindow()->SetContainingSizer( NULL
);
479 m_children
.Erase( node
);
483 bool wxSizer::Detach( wxSizer
*sizer
)
485 wxASSERT_MSG( sizer
, _T("Detaching NULL sizer") );
487 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
490 wxSizerItem
*item
= node
->GetData();
492 if (item
->GetSizer() == sizer
)
496 m_children
.Erase( node
);
499 node
= node
->GetNext();
505 bool wxSizer::Detach( wxWindow
*window
)
507 wxASSERT_MSG( window
, _T("Detaching NULL window") );
509 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
512 wxSizerItem
*item
= node
->GetData();
514 if (item
->GetWindow() == window
)
516 item
->GetWindow()->SetContainingSizer( NULL
);
518 m_children
.Erase( node
);
521 node
= node
->GetNext();
527 bool wxSizer::Detach( int index
)
529 wxCHECK_MSG( index
>= 0 && (size_t)index
< m_children
.GetCount(),
531 _T("Detach index is out of range") );
533 wxSizerItemList::compatibility_iterator node
= m_children
.Item( index
);
535 wxCHECK_MSG( node
, false, _T("Failed to find child node") );
537 wxSizerItem
*item
= node
->GetData();
539 if( item
->IsSizer() )
541 else if( item
->IsWindow() )
542 item
->GetWindow()->SetContainingSizer( NULL
);
545 m_children
.Erase( node
);
549 void wxSizer::Clear( bool delete_windows
)
551 // First clear the ContainingSizer pointers
552 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
555 wxSizerItem
*item
= node
->GetData();
557 if (item
->IsWindow())
558 item
->GetWindow()->SetContainingSizer( NULL
);
559 node
= node
->GetNext();
562 // Destroy the windows if needed
566 // Now empty the list
567 WX_CLEAR_LIST(wxSizerItemList
, m_children
);
570 void wxSizer::DeleteWindows()
572 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
575 wxSizerItem
*item
= node
->GetData();
577 item
->DeleteWindows();
578 node
= node
->GetNext();
582 wxSize
wxSizer::Fit( wxWindow
*window
)
584 wxSize
size(window
->IsTopLevel() ? FitSize(window
)
585 : GetMinWindowSize(window
));
587 window
->SetSize( size
);
592 void wxSizer::FitInside( wxWindow
*window
)
595 if (window
->IsTopLevel())
596 size
= VirtualFitSize( window
);
598 size
= GetMinClientSize( window
);
600 window
->SetVirtualSize( size
);
603 void wxSizer::Layout()
605 // (re)calculates minimums needed for each item and other preparations
609 // Applies the layout and repositions/resizes the items
613 void wxSizer::SetSizeHints( wxWindow
*window
)
615 // Preserve the window's max size hints, but set the
616 // lower bound according to the sizer calculations.
618 wxSize size
= Fit( window
);
620 window
->SetSizeHints( size
.x
,
622 window
->GetMaxWidth(),
623 window
->GetMaxHeight() );
626 void wxSizer::SetVirtualSizeHints( wxWindow
*window
)
628 // Preserve the window's max size hints, but set the
629 // lower bound according to the sizer calculations.
632 wxSize
size( window
->GetVirtualSize() );
633 window
->SetVirtualSizeHints( size
.x
,
635 window
->GetMaxWidth(),
636 window
->GetMaxHeight() );
639 wxSize
wxSizer::GetMaxWindowSize( wxWindow
*window
) const
641 return window
->GetMaxSize();
644 wxSize
wxSizer::GetMinWindowSize( wxWindow
*window
)
646 wxSize
minSize( GetMinSize() );
647 wxSize
size( window
->GetSize() );
648 wxSize
client_size( window
->GetClientSize() );
650 return wxSize( minSize
.x
+size
.x
-client_size
.x
,
651 minSize
.y
+size
.y
-client_size
.y
);
654 // TODO on mac we need a function that determines how much free space this
655 // min size contains, in order to make sure that we have 20 pixels of free
656 // space around the controls
658 // Return a window size that will fit within the screens dimensions
659 wxSize
wxSizer::FitSize( wxWindow
*window
)
661 wxSize size
= GetMinWindowSize( window
);
662 wxSize sizeMax
= GetMaxWindowSize( window
);
664 // Limit the size if sizeMax != wxDefaultSize
666 if ( size
.x
> sizeMax
.x
&& sizeMax
.x
!= -1 )
668 if ( size
.y
> sizeMax
.y
&& sizeMax
.y
!= -1 )
674 wxSize
wxSizer::GetMaxClientSize( wxWindow
*window
) const
676 wxSize
maxSize( window
->GetMaxSize() );
678 if( maxSize
!= wxDefaultSize
)
680 wxSize
size( window
->GetSize() );
681 wxSize
client_size( window
->GetClientSize() );
683 return wxSize( maxSize
.x
+ client_size
.x
- size
.x
,
684 maxSize
.y
+ client_size
.y
- size
.y
);
687 return wxDefaultSize
;
690 wxSize
wxSizer::GetMinClientSize( wxWindow
*WXUNUSED(window
) )
692 return GetMinSize(); // Already returns client size.
695 wxSize
wxSizer::VirtualFitSize( wxWindow
*window
)
697 wxSize size
= GetMinClientSize( window
);
698 wxSize sizeMax
= GetMaxClientSize( window
);
700 // Limit the size if sizeMax != wxDefaultSize
702 if ( size
.x
> sizeMax
.x
&& sizeMax
.x
!= -1 )
704 if ( size
.y
> sizeMax
.y
&& sizeMax
.y
!= -1 )
710 void wxSizer::SetDimension( int x
, int y
, int width
, int height
)
719 wxSize
wxSizer::GetMinSize()
721 wxSize
ret( CalcMin() );
722 if (ret
.x
< m_minSize
.x
) ret
.x
= m_minSize
.x
;
723 if (ret
.y
< m_minSize
.y
) ret
.y
= m_minSize
.y
;
727 void wxSizer::DoSetMinSize( int width
, int height
)
730 m_minSize
.y
= height
;
733 bool wxSizer::DoSetItemMinSize( wxWindow
*window
, int width
, int height
)
735 wxASSERT_MSG( window
, _T("SetMinSize for NULL window") );
737 // Is it our immediate child?
739 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
742 wxSizerItem
*item
= node
->GetData();
744 if (item
->GetWindow() == window
)
746 item
->SetMinSize( width
, height
);
749 node
= node
->GetNext();
752 // No? Search any subsizers we own then
754 node
= m_children
.GetFirst();
757 wxSizerItem
*item
= node
->GetData();
759 if ( item
->GetSizer() &&
760 item
->GetSizer()->DoSetItemMinSize( window
, width
, height
) )
762 // A child sizer found the requested windw, exit.
765 node
= node
->GetNext();
771 bool wxSizer::DoSetItemMinSize( wxSizer
*sizer
, int width
, int height
)
773 wxASSERT_MSG( sizer
, _T("SetMinSize for NULL sizer") );
775 // Is it our immediate child?
777 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
780 wxSizerItem
*item
= node
->GetData();
782 if (item
->GetSizer() == sizer
)
784 item
->GetSizer()->DoSetMinSize( width
, height
);
787 node
= node
->GetNext();
790 // No? Search any subsizers we own then
792 node
= m_children
.GetFirst();
795 wxSizerItem
*item
= node
->GetData();
797 if ( item
->GetSizer() &&
798 item
->GetSizer()->DoSetItemMinSize( sizer
, width
, height
) )
800 // A child found the requested sizer, exit.
803 node
= node
->GetNext();
809 bool wxSizer::DoSetItemMinSize( size_t index
, int width
, int height
)
811 wxSizerItemList::compatibility_iterator node
= m_children
.Item( index
);
813 wxCHECK_MSG( node
, false, _T("Failed to find child node") );
815 wxSizerItem
*item
= node
->GetData();
817 if (item
->GetSizer())
819 // Sizers contains the minimal size in them, if not calculated ...
820 item
->GetSizer()->DoSetMinSize( width
, height
);
824 // ... but the minimal size of spacers and windows is stored via the item
825 item
->SetMinSize( width
, height
);
831 void wxSizer::Show( wxWindow
*window
, bool show
)
833 wxASSERT_MSG( window
, _T("Show for NULL window") );
835 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
838 wxSizerItem
*item
= node
->GetData();
840 if (item
->GetWindow() == window
)
845 node
= node
->GetNext();
849 void wxSizer::Show( wxSizer
*sizer
, bool show
)
851 wxASSERT_MSG( sizer
, _T("Show for NULL sizer") );
853 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
856 wxSizerItem
*item
= node
->GetData();
858 if (item
->GetSizer() == sizer
)
863 node
= node
->GetNext();
867 void wxSizer::Show( size_t index
, bool show
)
869 wxCHECK_RET( index
< m_children
.GetCount(),
870 _T("Show index is out of range") );
872 m_children
.Item( index
)->GetData()->Show( show
);
875 void wxSizer::ShowItems( bool show
)
877 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
880 node
->GetData()->Show( show
);
881 node
= node
->GetNext();
885 bool wxSizer::IsShown( wxWindow
*window
) const
887 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
890 wxSizerItem
*item
= node
->GetData();
892 if (item
->GetWindow() == window
)
894 return item
->IsShown();
896 node
= node
->GetNext();
899 wxFAIL_MSG( _T("IsShown failed to find sizer item") );
904 bool wxSizer::IsShown( wxSizer
*sizer
) const
906 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
909 wxSizerItem
*item
= node
->GetData();
911 if (item
->GetSizer() == sizer
)
913 return item
->IsShown();
915 node
= node
->GetNext();
918 wxFAIL_MSG( _T("IsShown failed to find sizer item") );
923 bool wxSizer::IsShown( size_t index
) const
925 wxCHECK_MSG( index
< m_children
.GetCount(),
927 _T("IsShown index is out of range") );
929 return m_children
.Item( index
)->GetData()->IsShown();
933 //---------------------------------------------------------------------------
935 //---------------------------------------------------------------------------
937 wxGridSizer::wxGridSizer( int rows
, int cols
, int vgap
, int hgap
)
943 if (m_rows
== 0 && m_cols
== 0)
947 wxGridSizer::wxGridSizer( int cols
, int vgap
, int hgap
)
953 if (m_rows
== 0 && m_cols
== 0)
957 int wxGridSizer::CalcRowsCols(int& nrows
, int& ncols
) const
959 int nitems
= m_children
.GetCount();
965 nrows
= (nitems
+ m_cols
- 1) / m_cols
;
969 ncols
= (nitems
+ m_rows
- 1) / m_rows
;
972 else // 0 columns, 0 rows?
974 wxFAIL_MSG( _T("grid sizer must have either rows or columns fixed") );
983 void wxGridSizer::RecalcSizes()
985 int nitems
, nrows
, ncols
;
986 if ( (nitems
= CalcRowsCols(nrows
, ncols
)) == 0 )
989 wxSize
sz( GetSize() );
990 wxPoint
pt( GetPosition() );
992 int w
= (sz
.x
- (ncols
- 1) * m_hgap
) / ncols
;
993 int h
= (sz
.y
- (nrows
- 1) * m_vgap
) / nrows
;
996 for (int c
= 0; c
< ncols
; c
++)
999 for (int r
= 0; r
< nrows
; r
++)
1001 int i
= r
* ncols
+ c
;
1004 wxSizerItemList::compatibility_iterator node
= m_children
.Item( i
);
1006 wxASSERT_MSG( node
, _T("Failed to find SizerItemList node") );
1008 SetItemBounds( node
->GetData(), x
, y
, w
, h
);
1016 wxSize
wxGridSizer::CalcMin()
1019 if ( CalcRowsCols(nrows
, ncols
) == 0 )
1020 return wxSize(10, 10);
1022 // Find the max width and height for any component
1026 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
1029 wxSizerItem
*item
= node
->GetData();
1030 wxSize
sz( item
->CalcMin() );
1032 w
= wxMax( w
, sz
.x
);
1033 h
= wxMax( h
, sz
.y
);
1035 node
= node
->GetNext();
1038 return wxSize( ncols
* w
+ (ncols
-1) * m_hgap
,
1039 nrows
* h
+ (nrows
-1) * m_vgap
);
1042 void wxGridSizer::SetItemBounds( wxSizerItem
*item
, int x
, int y
, int w
, int h
)
1045 wxSize
sz( item
->GetMinSizeWithBorder() );
1046 int flag
= item
->GetFlag();
1048 if ((flag
& wxEXPAND
) || (flag
& wxSHAPED
))
1054 if (flag
& wxALIGN_CENTER_HORIZONTAL
)
1056 pt
.x
= x
+ (w
- sz
.x
) / 2;
1058 else if (flag
& wxALIGN_RIGHT
)
1060 pt
.x
= x
+ (w
- sz
.x
);
1063 if (flag
& wxALIGN_CENTER_VERTICAL
)
1065 pt
.y
= y
+ (h
- sz
.y
) / 2;
1067 else if (flag
& wxALIGN_BOTTOM
)
1069 pt
.y
= y
+ (h
- sz
.y
);
1073 item
->SetDimension(pt
, sz
);
1076 //---------------------------------------------------------------------------
1078 //---------------------------------------------------------------------------
1080 wxFlexGridSizer::wxFlexGridSizer( int rows
, int cols
, int vgap
, int hgap
)
1081 : wxGridSizer( rows
, cols
, vgap
, hgap
),
1082 m_flexDirection(wxBOTH
),
1083 m_growMode(wxFLEX_GROWMODE_SPECIFIED
)
1087 wxFlexGridSizer::wxFlexGridSizer( int cols
, int vgap
, int hgap
)
1088 : wxGridSizer( cols
, vgap
, hgap
),
1089 m_flexDirection(wxBOTH
),
1090 m_growMode(wxFLEX_GROWMODE_SPECIFIED
)
1094 wxFlexGridSizer::~wxFlexGridSizer()
1098 void wxFlexGridSizer::RecalcSizes()
1100 int nitems
, nrows
, ncols
;
1101 if ( (nitems
= CalcRowsCols(nrows
, ncols
)) == 0 )
1104 wxPoint
pt( GetPosition() );
1105 wxSize
sz( GetSize() );
1107 AdjustForGrowables(sz
, m_calculatedMinSize
, nrows
, ncols
);
1109 sz
= wxSize( pt
.x
+ sz
.x
, pt
.y
+ sz
.y
);
1112 for (int c
= 0; c
< ncols
; c
++)
1115 for (int r
= 0; r
< nrows
; r
++)
1117 int i
= r
* ncols
+ c
;
1120 wxSizerItemList::compatibility_iterator node
= m_children
.Item( i
);
1122 wxASSERT_MSG( node
, _T("Failed to find node") );
1124 int w
= wxMax( 0, wxMin( m_colWidths
[c
], sz
.x
- x
) );
1125 int h
= wxMax( 0, wxMin( m_rowHeights
[r
], sz
.y
- y
) );
1127 SetItemBounds( node
->GetData(), x
, y
, w
, h
);
1129 y
= y
+ m_rowHeights
[r
] + m_vgap
;
1131 x
= x
+ m_colWidths
[c
] + m_hgap
;
1135 wxSize
wxFlexGridSizer::CalcMin()
1141 // Number of rows/columns can change as items are added or removed.
1142 if ( !CalcRowsCols(nrows
, ncols
) )
1143 return wxSize(10, 10);
1145 m_rowHeights
.SetCount(nrows
);
1146 m_colWidths
.SetCount(ncols
);
1148 // We have to recalcuate the sizes in case the item minimum size has
1149 // changed since the previous layout, or the item has been hidden using
1150 // wxSizer::Show(). If all the items in a row/column are hidden, the final
1151 // dimension of the row/column will be -1, indicating that the column
1152 // itself is hidden.
1153 for( s
= m_rowHeights
.GetCount(), i
= 0; i
< s
; ++i
)
1154 m_rowHeights
[ i
] = -1;
1155 for( s
= m_colWidths
.GetCount(), i
= 0; i
< s
; ++i
)
1156 m_colWidths
[ i
] = -1;
1158 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
1163 wxSizerItem
*item
= node
->GetData();
1164 if ( item
->IsShown() )
1166 wxSize
sz( item
->CalcMin() );
1167 int row
= i
/ ncols
;
1168 int col
= i
% ncols
;
1170 m_rowHeights
[ row
] = wxMax( wxMax( 0, sz
.y
), m_rowHeights
[ row
] );
1171 m_colWidths
[ col
] = wxMax( wxMax( 0, sz
.x
), m_colWidths
[ col
] );
1174 node
= node
->GetNext();
1178 AdjustForFlexDirection();
1180 // Sum total minimum size, including gaps between rows/columns.
1181 // -1 is used as a magic number meaning empty column.
1183 for (int col
= 0; col
< ncols
; col
++)
1184 if ( m_colWidths
[ col
] != -1 )
1185 width
+= m_colWidths
[ col
] + ( col
== ncols
-1 ? 0 : m_hgap
);
1188 for (int row
= 0; row
< nrows
; row
++)
1189 if ( m_rowHeights
[ row
] != -1 )
1190 height
+= m_rowHeights
[ row
] + ( row
== nrows
-1 ? 0 : m_vgap
);
1192 m_calculatedMinSize
= wxSize( width
, height
);
1193 return m_calculatedMinSize
;
1196 void wxFlexGridSizer::AdjustForFlexDirection()
1198 // the logic in CalcMin works when we resize flexibly in both directions
1199 // but maybe this is not the case
1200 if ( m_flexDirection
!= wxBOTH
)
1202 // select the array corresponding to the direction in which we do *not*
1204 wxArrayInt
& array
= m_flexDirection
== wxVERTICAL
? m_colWidths
1207 const int count
= array
.GetCount();
1209 // find the largest value in this array
1211 for ( n
= 0; n
< count
; ++n
)
1213 if ( array
[n
] > largest
)
1217 // and now fill it with the largest value
1218 for ( n
= 0; n
< count
; ++n
)
1226 void wxFlexGridSizer::AdjustForGrowables(const wxSize
& sz
, const wxSize
& minsz
,
1227 int nrows
, int ncols
)
1229 // what to do with the rows? by default, resize them proportionally
1230 if ( sz
.y
> minsz
.y
&& ( (m_flexDirection
& wxVERTICAL
) || (m_growMode
== wxFLEX_GROWMODE_SPECIFIED
) ) )
1232 int sum_proportions
= 0;
1233 int growable_space
= 0;
1236 for (idx
= 0; idx
< m_growableRows
.GetCount(); idx
++)
1238 // Since the number of rows/columns can change as items are
1239 // inserted/deleted, we need to verify at runtime that the
1240 // requested growable rows/columns are still valid.
1241 if (m_growableRows
[idx
] >= nrows
)
1244 // If all items in a row/column are hidden, that row/column will
1245 // have a dimension of -1. This causes the row/column to be
1246 // hidden completely.
1247 if (m_rowHeights
[ m_growableRows
[idx
] ] == -1)
1249 sum_proportions
+= m_growableRowsProportions
[idx
];
1250 growable_space
+= m_rowHeights
[ m_growableRows
[idx
] ];
1256 for (idx
= 0; idx
< m_growableRows
.GetCount(); idx
++)
1258 if (m_growableRows
[idx
] >= nrows
)
1260 if (m_rowHeights
[ m_growableRows
[idx
] ] == -1)
1261 m_rowHeights
[ m_growableRows
[idx
] ] = 0;
1264 int delta
= (sz
.y
- minsz
.y
);
1265 if (sum_proportions
== 0)
1266 delta
= (delta
/num
) + m_rowHeights
[ m_growableRows
[idx
] ];
1268 delta
= ((delta
+growable_space
)*m_growableRowsProportions
[idx
]) / sum_proportions
;
1269 m_rowHeights
[ m_growableRows
[idx
] ] = delta
;
1274 else if ( (m_growMode
== wxFLEX_GROWMODE_ALL
) && (sz
.y
> minsz
.y
) )
1276 // rounding problem?
1277 for ( int row
= 0; row
< nrows
; ++row
)
1278 m_rowHeights
[ row
] = sz
.y
/ nrows
;
1281 // the same logic as above but for the columns
1282 if ( sz
.x
> minsz
.x
&& ( (m_flexDirection
& wxHORIZONTAL
) || (m_growMode
== wxFLEX_GROWMODE_SPECIFIED
) ) )
1284 int sum_proportions
= 0;
1285 int growable_space
= 0;
1288 for (idx
= 0; idx
< m_growableCols
.GetCount(); idx
++)
1290 // Since the number of rows/columns can change as items are
1291 // inserted/deleted, we need to verify at runtime that the
1292 // requested growable rows/columns are still valid.
1293 if (m_growableCols
[idx
] >= ncols
)
1296 // If all items in a row/column are hidden, that row/column will
1297 // have a dimension of -1. This causes the column to be hidden
1299 if (m_colWidths
[ m_growableCols
[idx
] ] == -1)
1301 sum_proportions
+= m_growableColsProportions
[idx
];
1302 growable_space
+= m_colWidths
[ m_growableCols
[idx
] ];
1308 for (idx
= 0; idx
< m_growableCols
.GetCount(); idx
++)
1310 if (m_growableCols
[idx
] >= ncols
)
1312 if (m_colWidths
[ m_growableCols
[idx
] ] == -1)
1313 m_colWidths
[ m_growableCols
[idx
] ] = 0;
1316 int delta
= (sz
.x
- minsz
.x
);
1317 if (sum_proportions
== 0)
1318 delta
= (delta
/num
) + m_colWidths
[ m_growableCols
[idx
] ];
1320 delta
= ((delta
+growable_space
)*m_growableColsProportions
[idx
])/sum_proportions
;
1321 m_colWidths
[ m_growableCols
[idx
] ] = delta
;
1326 else if ( (m_growMode
== wxFLEX_GROWMODE_ALL
) && (sz
.x
> minsz
.x
) )
1328 for ( int col
=0; col
< ncols
; ++col
)
1329 m_colWidths
[ col
] = sz
.x
/ ncols
;
1334 void wxFlexGridSizer::AddGrowableRow( size_t idx
, int proportion
)
1336 m_growableRows
.Add( idx
);
1337 m_growableRowsProportions
.Add( proportion
);
1340 void wxFlexGridSizer::RemoveGrowableRow( size_t idx
)
1342 m_growableRows
.Remove( idx
);
1345 void wxFlexGridSizer::AddGrowableCol( size_t idx
, int proportion
)
1347 m_growableCols
.Add( idx
);
1348 m_growableColsProportions
.Add( proportion
);
1351 void wxFlexGridSizer::RemoveGrowableCol( size_t idx
)
1353 m_growableCols
.Remove( idx
);
1356 //---------------------------------------------------------------------------
1358 //---------------------------------------------------------------------------
1360 wxBoxSizer::wxBoxSizer( int orient
)
1361 : m_orient( orient
)
1365 void wxBoxSizer::RecalcSizes()
1367 if (m_children
.GetCount() == 0)
1373 if (m_orient
== wxHORIZONTAL
)
1374 delta
= m_size
.x
- m_fixedWidth
;
1376 delta
= m_size
.y
- m_fixedHeight
;
1379 wxPoint
pt( m_position
);
1381 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
1384 wxSizerItem
*item
= node
->GetData();
1386 if (item
->IsShown())
1388 wxSize
size( item
->GetMinSizeWithBorder() );
1390 if (m_orient
== wxVERTICAL
)
1392 wxCoord height
= size
.y
;
1393 if (item
->GetProportion())
1395 // Because of at least one visible item has non-zero
1396 // proportion then m_stretchable is not zero
1397 height
= (delta
* item
->GetProportion()) / m_stretchable
;
1400 wxPoint
child_pos( pt
);
1401 wxSize
child_size( wxSize( size
.x
, height
) );
1403 if (item
->GetFlag() & (wxEXPAND
| wxSHAPED
))
1404 child_size
.x
= m_size
.x
;
1405 else if (item
->GetFlag() & wxALIGN_RIGHT
)
1406 child_pos
.x
+= m_size
.x
- size
.x
;
1407 else if (item
->GetFlag() & (wxCENTER
| wxALIGN_CENTER_HORIZONTAL
))
1408 // XXX wxCENTER is added for backward compatibility;
1409 // wxALIGN_CENTER should be used in new code
1410 child_pos
.x
+= (m_size
.x
- size
.x
) / 2;
1412 item
->SetDimension( child_pos
, child_size
);
1418 wxCoord width
= size
.x
;
1419 if (item
->GetProportion())
1421 // Because of at least one visible item has non-zero
1422 // proportion then m_stretchable is not zero
1423 width
= (delta
* item
->GetProportion()) / m_stretchable
;
1426 wxPoint
child_pos( pt
);
1427 wxSize
child_size( wxSize(width
, size
.y
) );
1429 if (item
->GetFlag() & (wxEXPAND
| wxSHAPED
))
1430 child_size
.y
= m_size
.y
;
1431 else if (item
->GetFlag() & wxALIGN_BOTTOM
)
1432 child_pos
.y
+= m_size
.y
- size
.y
;
1433 else if (item
->GetFlag() & (wxCENTER
| wxALIGN_CENTER_VERTICAL
))
1434 // XXX wxCENTER is added for backward compatibility;
1435 // wxALIGN_CENTER should be used in new code
1436 child_pos
.y
+= (m_size
.y
- size
.y
) / 2;
1438 item
->SetDimension( child_pos
, child_size
);
1444 node
= node
->GetNext();
1448 wxSize
wxBoxSizer::CalcMin()
1450 if (m_children
.GetCount() == 0)
1451 return wxSize(10,10);
1459 // precalc item minsizes and count proportions
1460 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
1463 wxSizerItem
*item
= node
->GetData();
1465 if (item
->IsShown())
1466 item
->CalcMin(); // result is stored in the item
1468 if (item
->IsShown() && item
->GetProportion() != 0)
1469 m_stretchable
+= item
->GetProportion();
1471 node
= node
->GetNext();
1474 // Total minimum size (width or height) of sizer
1477 node
= m_children
.GetFirst();
1480 wxSizerItem
*item
= node
->GetData();
1482 if (item
->IsShown() && item
->GetProportion() != 0)
1484 int stretch
= item
->GetProportion();
1485 wxSize
size( item
->GetMinSizeWithBorder() );
1488 // Integer division rounded up is (a + b - 1) / b
1489 // Round up needed in order to guarantee that all
1490 // all items will have size not less then their min size
1491 if (m_orient
== wxHORIZONTAL
)
1492 minSize
= ( size
.x
*m_stretchable
+ stretch
- 1)/stretch
;
1494 minSize
= ( size
.y
*m_stretchable
+ stretch
- 1)/stretch
;
1496 if (minSize
> maxMinSize
)
1497 maxMinSize
= minSize
;
1499 node
= node
->GetNext();
1502 // Calculate overall minimum size
1503 node
= m_children
.GetFirst();
1506 wxSizerItem
*item
= node
->GetData();
1508 if (item
->IsShown())
1510 wxSize
size( item
->GetMinSizeWithBorder() );
1511 if (item
->GetProportion() != 0)
1513 if (m_orient
== wxHORIZONTAL
)
1514 size
.x
= (maxMinSize
*item
->GetProportion())/m_stretchable
;
1516 size
.y
= (maxMinSize
*item
->GetProportion())/m_stretchable
;
1520 if (m_orient
== wxVERTICAL
)
1522 m_fixedHeight
+= size
.y
;
1523 m_fixedWidth
= wxMax( m_fixedWidth
, size
.x
);
1527 m_fixedWidth
+= size
.x
;
1528 m_fixedHeight
= wxMax( m_fixedHeight
, size
.y
);
1532 if (m_orient
== wxHORIZONTAL
)
1534 m_minWidth
+= size
.x
;
1535 m_minHeight
= wxMax( m_minHeight
, size
.y
);
1539 m_minHeight
+= size
.y
;
1540 m_minWidth
= wxMax( m_minWidth
, size
.x
);
1543 node
= node
->GetNext();
1546 return wxSize( m_minWidth
, m_minHeight
);
1549 //---------------------------------------------------------------------------
1551 //---------------------------------------------------------------------------
1555 wxStaticBoxSizer::wxStaticBoxSizer( wxStaticBox
*box
, int orient
)
1556 : wxBoxSizer( orient
)
1557 , m_staticBox( box
)
1559 wxASSERT_MSG( box
, wxT("wxStaticBoxSizer needs a static box") );
1562 static void GetStaticBoxBorders( wxStaticBox
*box
,
1566 // this has to be done platform by platform as there is no way to
1567 // guess the thickness of a wxStaticBox border
1569 box
->GetBordersForSizer(borderTop
,borderOther
);
1570 #elif defined(__WXMAC__)
1572 static int extraTop
= -1; // Uninitted
1573 static int other
= 5;
1575 if ( extraTop
== -1 )
1577 // The minimal border used for the top. Later on the staticbox'
1578 // font height is added to this.
1581 if ( UMAGetSystemVersion() >= 0x1030 /*Panther*/ )
1583 // As indicated by the HIG, Panther needs an extra border of 11
1584 // pixels (otherwise overlapping occurs at the top). The "other"
1585 // border has to be 11.
1592 *borderTop
= extraTop
+ box
->GetCharHeight();
1593 *borderOther
= other
;
1597 if ( box
->GetLabel().IsEmpty() )
1601 *borderTop
= box
->GetCharHeight();
1604 #endif // __WXCOCOA__
1607 void wxStaticBoxSizer::RecalcSizes()
1609 int top_border
, other_border
;
1610 GetStaticBoxBorders(m_staticBox
, &top_border
, &other_border
);
1612 m_staticBox
->SetSize( m_position
.x
, m_position
.y
, m_size
.x
, m_size
.y
);
1614 wxPoint
old_pos( m_position
);
1615 m_position
.x
+= other_border
;
1616 m_position
.y
+= top_border
;
1617 wxSize
old_size( m_size
);
1618 m_size
.x
-= 2*other_border
;
1619 m_size
.y
-= top_border
+ other_border
;
1621 wxBoxSizer::RecalcSizes();
1623 m_position
= old_pos
;
1627 wxSize
wxStaticBoxSizer::CalcMin()
1629 int top_border
, other_border
;
1630 GetStaticBoxBorders(m_staticBox
, &top_border
, &other_border
);
1632 wxSize
ret( wxBoxSizer::CalcMin() );
1633 ret
.x
+= 2*other_border
;
1634 ret
.y
+= other_border
+ top_border
;
1639 void wxStaticBoxSizer::ShowItems( bool show
)
1641 m_staticBox
->Show( show
);
1642 wxBoxSizer::ShowItems( show
);
1645 #endif // wxUSE_STATBOX
1648 #if WXWIN_COMPATIBILITY_2_4
1650 // ----------------------------------------------------------------------------
1652 // ----------------------------------------------------------------------------
1655 IMPLEMENT_CLASS(wxBookCtrlSizer
, wxSizer
)
1657 IMPLEMENT_CLASS(wxNotebookSizer
, wxBookCtrlSizer
)
1658 #endif // wxUSE_NOTEBOOK
1659 #endif // wxUSE_BOOKCTRL
1663 wxBookCtrlSizer::wxBookCtrlSizer(wxBookCtrl
*bookctrl
)
1664 : m_bookctrl(bookctrl
)
1666 wxASSERT_MSG( bookctrl
, wxT("wxBookCtrlSizer needs a control") );
1669 void wxBookCtrlSizer::RecalcSizes()
1671 m_bookctrl
->SetSize( m_position
.x
, m_position
.y
, m_size
.x
, m_size
.y
);
1674 wxSize
wxBookCtrlSizer::CalcMin()
1676 wxSize sizeBorder
= m_bookctrl
->CalcSizeFromPage(wxSize(0, 0));
1681 if ( m_bookctrl
->GetPageCount() == 0 )
1683 return wxSize(sizeBorder
.x
+ 10, sizeBorder
.y
+ 10);
1689 wxWindowList::compatibility_iterator
1690 node
= m_bookctrl
->GetChildren().GetFirst();
1693 wxWindow
*item
= node
->GetData();
1694 wxSizer
*itemsizer
= item
->GetSizer();
1698 wxSize
subsize( itemsizer
->CalcMin() );
1700 if (subsize
.x
> maxX
)
1702 if (subsize
.y
> maxY
)
1706 node
= node
->GetNext();
1709 return wxSize( maxX
, maxY
) + sizeBorder
;
1714 wxNotebookSizer::wxNotebookSizer(wxNotebook
*nb
)
1716 wxASSERT_MSG( nb
, wxT("wxNotebookSizer needs a control") );
1720 #endif // wxUSE_NOTEBOOOK
1721 #endif // wxUSE_BOOKCTRL
1723 #endif // WXWIN_COMPATIBILITY_2_4