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 // aspect ratio calculated from initial size
109 SetRatio( m_minSize
);
111 if (flag
& wxFIXED_MINSIZE
)
112 window
->SetMinSize(window
->GetSize());
113 m_minSize
= window
->GetSize();
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()
187 m_minSize
= m_sizer
->GetMinSize();
189 // if we have to preserve aspect ratio _AND_ this is
190 // the first-time calculation, consider ret to be initial size
191 if ((m_flag
& wxSHAPED
) && !m_ratio
)
194 else if ( IsWindow() )
196 // Since the size of the window may change during runtime, we
197 // should use the current minimal/best size.
198 m_minSize
= m_window
->GetBestFittingSize();
201 return GetMinSizeWithBorder();
204 wxSize
wxSizerItem::GetMinSizeWithBorder() const
206 wxSize ret
= m_minSize
;
212 if (m_flag
& wxNORTH
)
214 if (m_flag
& wxSOUTH
)
221 void wxSizerItem::SetDimension( wxPoint pos
, wxSize size
)
223 if (m_flag
& wxSHAPED
)
225 // adjust aspect ratio
226 int rwidth
= (int) (size
.y
* m_ratio
);
230 int rheight
= (int) (size
.x
/ m_ratio
);
231 // add vertical space
232 if (m_flag
& wxALIGN_CENTER_VERTICAL
)
233 pos
.y
+= (size
.y
- rheight
) / 2;
234 else if (m_flag
& wxALIGN_BOTTOM
)
235 pos
.y
+= (size
.y
- rheight
);
236 // use reduced dimensions
239 else if (rwidth
< size
.x
)
241 // add horizontal space
242 if (m_flag
& wxALIGN_CENTER_HORIZONTAL
)
243 pos
.x
+= (size
.x
- rwidth
) / 2;
244 else if (m_flag
& wxALIGN_RIGHT
)
245 pos
.x
+= (size
.x
- rwidth
);
250 // This is what GetPosition() returns. Since we calculate
251 // borders afterwards, GetPosition() will be the left/top
252 // corner of the surrounding border.
264 if (m_flag
& wxNORTH
)
269 if (m_flag
& wxSOUTH
)
275 m_sizer
->SetDimension( pos
.x
, pos
.y
, size
.x
, size
.y
);
278 m_window
->SetSize( pos
.x
, pos
.y
, size
.x
, size
.y
, wxSIZE_ALLOW_MINUS_ONE
);
283 void wxSizerItem::DeleteWindows()
292 m_sizer
->DeleteWindows();
295 bool wxSizerItem::IsWindow() const
297 return (m_window
!= NULL
);
300 bool wxSizerItem::IsSizer() const
302 return (m_sizer
!= NULL
);
305 bool wxSizerItem::IsSpacer() const
307 return (m_window
== NULL
) && (m_sizer
== NULL
);
310 void wxSizerItem::Show( bool show
)
315 m_window
->Show( show
);
317 m_sizer
->ShowItems( show
);
319 // ... nothing else to do to hide/show spacers
322 void wxSizerItem::SetOption( int option
)
324 SetProportion( option
);
327 int wxSizerItem::GetOption() const
329 return GetProportion();
333 //---------------------------------------------------------------------------
335 //---------------------------------------------------------------------------
338 : m_minSize( wxSize( 0, 0 ) )
344 WX_CLEAR_LIST(wxSizerItemList
, m_children
);
347 void wxSizer::Add( wxWindow
*window
, int proportion
, int flag
, int border
, wxObject
* userData
)
349 m_children
.Append( new wxSizerItem( window
, proportion
, flag
, border
, userData
) );
350 window
->SetContainingSizer( this );
353 void wxSizer::Add( wxSizer
*sizer
, int proportion
, int flag
, int border
, wxObject
* userData
)
355 m_children
.Append( new wxSizerItem( sizer
, proportion
, flag
, border
, userData
) );
358 void wxSizer::Add( int width
, int height
, int proportion
, int flag
, int border
, wxObject
* userData
)
360 m_children
.Append( new wxSizerItem( width
, height
, proportion
, flag
, border
, userData
) );
363 void wxSizer::Add( wxSizerItem
*item
)
365 m_children
.Append( item
);
367 if( item
->GetWindow() )
368 item
->GetWindow()->SetContainingSizer( this );
371 void wxSizer::Prepend( wxWindow
*window
, int proportion
, int flag
, int border
, wxObject
* userData
)
373 m_children
.Insert( new wxSizerItem( window
, proportion
, flag
, border
, userData
) );
374 window
->SetContainingSizer( this );
377 void wxSizer::Prepend( wxSizer
*sizer
, int proportion
, int flag
, int border
, wxObject
* userData
)
379 m_children
.Insert( new wxSizerItem( sizer
, proportion
, flag
, border
, userData
) );
382 void wxSizer::Prepend( int width
, int height
, int proportion
, int flag
, int border
, wxObject
* userData
)
384 m_children
.Insert( new wxSizerItem( width
, height
, proportion
, flag
, border
, userData
) );
387 void wxSizer::Prepend( wxSizerItem
*item
)
389 m_children
.Insert( item
);
391 if( item
->GetWindow() )
392 item
->GetWindow()->SetContainingSizer( this );
395 void wxSizer::Insert( size_t index
,
402 m_children
.Insert( index
,
403 new wxSizerItem( window
, proportion
, flag
, border
, userData
) );
404 window
->SetContainingSizer( this );
407 void wxSizer::Insert( size_t index
,
414 m_children
.Insert( index
,
415 new wxSizerItem( sizer
, proportion
, flag
, border
, userData
) );
418 void wxSizer::Insert( size_t index
,
426 m_children
.Insert( index
,
427 new wxSizerItem( width
, height
, proportion
, flag
, border
, userData
) );
430 void wxSizer::Insert( size_t index
, wxSizerItem
*item
)
432 m_children
.Insert( index
, item
);
434 if( item
->GetWindow() )
435 item
->GetWindow()->SetContainingSizer( this );
438 bool wxSizer::Remove( wxWindow
*window
)
440 return Detach( window
);
443 bool wxSizer::Remove( wxSizer
*sizer
)
445 wxASSERT_MSG( sizer
, _T("Removing NULL sizer") );
447 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
450 wxSizerItem
*item
= node
->GetData();
452 if (item
->GetSizer() == sizer
)
455 m_children
.Erase( node
);
459 node
= node
->GetNext();
465 bool wxSizer::Remove( int index
)
467 wxCHECK_MSG( index
>= 0 && (size_t)index
< m_children
.GetCount(),
469 _T("Remove index is out of range") );
471 wxSizerItemList::compatibility_iterator node
= m_children
.Item( index
);
473 wxCHECK_MSG( node
, false, _T("Failed to find child node") );
475 wxSizerItem
*item
= node
->GetData();
477 if( item
->IsWindow() )
478 item
->GetWindow()->SetContainingSizer( NULL
);
481 m_children
.Erase( node
);
485 bool wxSizer::Detach( wxSizer
*sizer
)
487 wxASSERT_MSG( sizer
, _T("Detaching NULL sizer") );
489 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
492 wxSizerItem
*item
= node
->GetData();
494 if (item
->GetSizer() == sizer
)
498 m_children
.Erase( node
);
501 node
= node
->GetNext();
507 bool wxSizer::Detach( wxWindow
*window
)
509 wxASSERT_MSG( window
, _T("Detaching NULL window") );
511 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
514 wxSizerItem
*item
= node
->GetData();
516 if (item
->GetWindow() == window
)
518 item
->GetWindow()->SetContainingSizer( NULL
);
520 m_children
.Erase( node
);
523 node
= node
->GetNext();
529 bool wxSizer::Detach( int index
)
531 wxCHECK_MSG( index
>= 0 && (size_t)index
< m_children
.GetCount(),
533 _T("Detach index is out of range") );
535 wxSizerItemList::compatibility_iterator node
= m_children
.Item( index
);
537 wxCHECK_MSG( node
, false, _T("Failed to find child node") );
539 wxSizerItem
*item
= node
->GetData();
541 if( item
->IsSizer() )
543 else if( item
->IsWindow() )
544 item
->GetWindow()->SetContainingSizer( NULL
);
547 m_children
.Erase( node
);
551 void wxSizer::Clear( bool delete_windows
)
553 // First clear the ContainingSizer pointers
554 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
557 wxSizerItem
*item
= node
->GetData();
559 if (item
->IsWindow())
560 item
->GetWindow()->SetContainingSizer( NULL
);
561 node
= node
->GetNext();
564 // Destroy the windows if needed
568 // Now empty the list
569 WX_CLEAR_LIST(wxSizerItemList
, m_children
);
572 void wxSizer::DeleteWindows()
574 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
577 wxSizerItem
*item
= node
->GetData();
579 item
->DeleteWindows();
580 node
= node
->GetNext();
584 wxSize
wxSizer::Fit( wxWindow
*window
)
586 wxSize
size(window
->IsTopLevel() ? FitSize(window
)
587 : GetMinWindowSize(window
));
589 window
->SetSize( size
);
594 void wxSizer::FitInside( wxWindow
*window
)
597 if (window
->IsTopLevel())
598 size
= VirtualFitSize( window
);
600 size
= GetMinClientSize( window
);
602 window
->SetVirtualSize( size
);
605 void wxSizer::Layout()
607 // (re)calculates minimums needed for each item and other preparations
611 // Applies the layout and repositions/resizes the items
615 void wxSizer::SetSizeHints( wxWindow
*window
)
617 // Preserve the window's max size hints, but set the
618 // lower bound according to the sizer calculations.
620 wxSize size
= Fit( window
);
622 window
->SetSizeHints( size
.x
,
624 window
->GetMaxWidth(),
625 window
->GetMaxHeight() );
628 void wxSizer::SetVirtualSizeHints( wxWindow
*window
)
630 // Preserve the window's max size hints, but set the
631 // lower bound according to the sizer calculations.
634 wxSize
size( window
->GetVirtualSize() );
635 window
->SetVirtualSizeHints( size
.x
,
637 window
->GetMaxWidth(),
638 window
->GetMaxHeight() );
641 wxSize
wxSizer::GetMaxWindowSize( wxWindow
*window
) const
643 return window
->GetMaxSize();
646 wxSize
wxSizer::GetMinWindowSize( wxWindow
*window
)
648 wxSize
minSize( GetMinSize() );
649 wxSize
size( window
->GetSize() );
650 wxSize
client_size( window
->GetClientSize() );
652 return wxSize( minSize
.x
+size
.x
-client_size
.x
,
653 minSize
.y
+size
.y
-client_size
.y
);
656 // TODO on mac we need a function that determines how much free space this
657 // min size contains, in order to make sure that we have 20 pixels of free
658 // space around the controls
660 // Return a window size that will fit within the screens dimensions
661 wxSize
wxSizer::FitSize( wxWindow
*window
)
663 wxSize size
= GetMinWindowSize( window
);
664 wxSize sizeMax
= GetMaxWindowSize( window
);
666 // Limit the size if sizeMax != wxDefaultSize
668 if ( size
.x
> sizeMax
.x
&& sizeMax
.x
!= -1 )
670 if ( size
.y
> sizeMax
.y
&& sizeMax
.y
!= -1 )
676 wxSize
wxSizer::GetMaxClientSize( wxWindow
*window
) const
678 wxSize
maxSize( window
->GetMaxSize() );
680 if( maxSize
!= wxDefaultSize
)
682 wxSize
size( window
->GetSize() );
683 wxSize
client_size( window
->GetClientSize() );
685 return wxSize( maxSize
.x
+ client_size
.x
- size
.x
,
686 maxSize
.y
+ client_size
.y
- size
.y
);
689 return wxDefaultSize
;
692 wxSize
wxSizer::GetMinClientSize( wxWindow
*WXUNUSED(window
) )
694 return GetMinSize(); // Already returns client size.
697 wxSize
wxSizer::VirtualFitSize( wxWindow
*window
)
699 wxSize size
= GetMinClientSize( window
);
700 wxSize sizeMax
= GetMaxClientSize( window
);
702 // Limit the size if sizeMax != wxDefaultSize
704 if ( size
.x
> sizeMax
.x
&& sizeMax
.x
!= -1 )
706 if ( size
.y
> sizeMax
.y
&& sizeMax
.y
!= -1 )
712 void wxSizer::SetDimension( int x
, int y
, int width
, int height
)
721 wxSize
wxSizer::GetMinSize()
723 wxSize
ret( CalcMin() );
724 if (ret
.x
< m_minSize
.x
) ret
.x
= m_minSize
.x
;
725 if (ret
.y
< m_minSize
.y
) ret
.y
= m_minSize
.y
;
729 void wxSizer::DoSetMinSize( int width
, int height
)
732 m_minSize
.y
= height
;
735 bool wxSizer::DoSetItemMinSize( wxWindow
*window
, int width
, int height
)
737 wxASSERT_MSG( window
, _T("SetMinSize for NULL window") );
739 // Is it our immediate child?
741 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
744 wxSizerItem
*item
= node
->GetData();
746 if (item
->GetWindow() == window
)
748 item
->SetMinSize( width
, height
);
751 node
= node
->GetNext();
754 // No? Search any subsizers we own then
756 node
= m_children
.GetFirst();
759 wxSizerItem
*item
= node
->GetData();
761 if ( item
->GetSizer() &&
762 item
->GetSizer()->DoSetItemMinSize( window
, width
, height
) )
764 // A child sizer found the requested windw, exit.
767 node
= node
->GetNext();
773 bool wxSizer::DoSetItemMinSize( wxSizer
*sizer
, int width
, int height
)
775 wxASSERT_MSG( sizer
, _T("SetMinSize for NULL sizer") );
777 // Is it our immediate child?
779 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
782 wxSizerItem
*item
= node
->GetData();
784 if (item
->GetSizer() == sizer
)
786 item
->GetSizer()->DoSetMinSize( width
, height
);
789 node
= node
->GetNext();
792 // No? Search any subsizers we own then
794 node
= m_children
.GetFirst();
797 wxSizerItem
*item
= node
->GetData();
799 if ( item
->GetSizer() &&
800 item
->GetSizer()->DoSetItemMinSize( sizer
, width
, height
) )
802 // A child found the requested sizer, exit.
805 node
= node
->GetNext();
811 bool wxSizer::DoSetItemMinSize( size_t index
, int width
, int height
)
813 wxSizerItemList::compatibility_iterator node
= m_children
.Item( index
);
815 wxCHECK_MSG( node
, false, _T("Failed to find child node") );
817 wxSizerItem
*item
= node
->GetData();
819 if (item
->GetSizer())
821 // Sizers contains the minimal size in them, if not calculated ...
822 item
->GetSizer()->DoSetMinSize( width
, height
);
826 // ... but the minimal size of spacers and windows is stored via the item
827 item
->SetMinSize( width
, height
);
833 void wxSizer::Show( wxWindow
*window
, bool show
)
835 wxASSERT_MSG( window
, _T("Show for NULL window") );
837 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
840 wxSizerItem
*item
= node
->GetData();
842 if (item
->GetWindow() == window
)
847 node
= node
->GetNext();
851 void wxSizer::Show( wxSizer
*sizer
, bool show
)
853 wxASSERT_MSG( sizer
, _T("Show for NULL sizer") );
855 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
858 wxSizerItem
*item
= node
->GetData();
860 if (item
->GetSizer() == sizer
)
865 node
= node
->GetNext();
869 void wxSizer::Show( size_t index
, bool show
)
871 wxCHECK_RET( index
< m_children
.GetCount(),
872 _T("Show index is out of range") );
874 m_children
.Item( index
)->GetData()->Show( show
);
877 void wxSizer::ShowItems( bool show
)
879 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
882 node
->GetData()->Show( show
);
883 node
= node
->GetNext();
887 bool wxSizer::IsShown( wxWindow
*window
) const
889 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
892 wxSizerItem
*item
= node
->GetData();
894 if (item
->GetWindow() == window
)
896 return item
->IsShown();
898 node
= node
->GetNext();
901 wxFAIL_MSG( _T("IsShown failed to find sizer item") );
906 bool wxSizer::IsShown( wxSizer
*sizer
) const
908 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
911 wxSizerItem
*item
= node
->GetData();
913 if (item
->GetSizer() == sizer
)
915 return item
->IsShown();
917 node
= node
->GetNext();
920 wxFAIL_MSG( _T("IsShown failed to find sizer item") );
925 bool wxSizer::IsShown( size_t index
) const
927 wxCHECK_MSG( index
< m_children
.GetCount(),
929 _T("IsShown index is out of range") );
931 return m_children
.Item( index
)->GetData()->IsShown();
935 //---------------------------------------------------------------------------
937 //---------------------------------------------------------------------------
939 wxGridSizer::wxGridSizer( int rows
, int cols
, int vgap
, int hgap
)
945 if (m_rows
== 0 && m_cols
== 0)
949 wxGridSizer::wxGridSizer( int cols
, int vgap
, int hgap
)
955 if (m_rows
== 0 && m_cols
== 0)
959 int wxGridSizer::CalcRowsCols(int& nrows
, int& ncols
) const
961 int nitems
= m_children
.GetCount();
967 nrows
= (nitems
+ m_cols
- 1) / m_cols
;
971 ncols
= (nitems
+ m_rows
- 1) / m_rows
;
974 else // 0 columns, 0 rows?
976 wxFAIL_MSG( _T("grid sizer must have either rows or columns fixed") );
985 void wxGridSizer::RecalcSizes()
987 int nitems
, nrows
, ncols
;
988 if ( (nitems
= CalcRowsCols(nrows
, ncols
)) == 0 )
991 wxSize
sz( GetSize() );
992 wxPoint
pt( GetPosition() );
994 int w
= (sz
.x
- (ncols
- 1) * m_hgap
) / ncols
;
995 int h
= (sz
.y
- (nrows
- 1) * m_vgap
) / nrows
;
998 for (int c
= 0; c
< ncols
; c
++)
1001 for (int r
= 0; r
< nrows
; r
++)
1003 int i
= r
* ncols
+ c
;
1006 wxSizerItemList::compatibility_iterator node
= m_children
.Item( i
);
1008 wxASSERT_MSG( node
, _T("Failed to find SizerItemList node") );
1010 SetItemBounds( node
->GetData(), x
, y
, w
, h
);
1018 wxSize
wxGridSizer::CalcMin()
1021 if ( CalcRowsCols(nrows
, ncols
) == 0 )
1022 return wxSize(10, 10);
1024 // Find the max width and height for any component
1028 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
1031 wxSizerItem
*item
= node
->GetData();
1032 wxSize
sz( item
->CalcMin() );
1034 w
= wxMax( w
, sz
.x
);
1035 h
= wxMax( h
, sz
.y
);
1037 node
= node
->GetNext();
1040 return wxSize( ncols
* w
+ (ncols
-1) * m_hgap
,
1041 nrows
* h
+ (nrows
-1) * m_vgap
);
1044 void wxGridSizer::SetItemBounds( wxSizerItem
*item
, int x
, int y
, int w
, int h
)
1047 wxSize
sz( item
->GetMinSizeWithBorder() );
1048 int flag
= item
->GetFlag();
1050 if ((flag
& wxEXPAND
) || (flag
& wxSHAPED
))
1056 if (flag
& wxALIGN_CENTER_HORIZONTAL
)
1058 pt
.x
= x
+ (w
- sz
.x
) / 2;
1060 else if (flag
& wxALIGN_RIGHT
)
1062 pt
.x
= x
+ (w
- sz
.x
);
1065 if (flag
& wxALIGN_CENTER_VERTICAL
)
1067 pt
.y
= y
+ (h
- sz
.y
) / 2;
1069 else if (flag
& wxALIGN_BOTTOM
)
1071 pt
.y
= y
+ (h
- sz
.y
);
1075 item
->SetDimension(pt
, sz
);
1078 //---------------------------------------------------------------------------
1080 //---------------------------------------------------------------------------
1082 wxFlexGridSizer::wxFlexGridSizer( int rows
, int cols
, int vgap
, int hgap
)
1083 : wxGridSizer( rows
, cols
, vgap
, hgap
),
1084 m_flexDirection(wxBOTH
),
1085 m_growMode(wxFLEX_GROWMODE_SPECIFIED
)
1089 wxFlexGridSizer::wxFlexGridSizer( int cols
, int vgap
, int hgap
)
1090 : wxGridSizer( cols
, vgap
, hgap
),
1091 m_flexDirection(wxBOTH
),
1092 m_growMode(wxFLEX_GROWMODE_SPECIFIED
)
1096 wxFlexGridSizer::~wxFlexGridSizer()
1100 void wxFlexGridSizer::RecalcSizes()
1102 int nitems
, nrows
, ncols
;
1103 if ( (nitems
= CalcRowsCols(nrows
, ncols
)) == 0 )
1106 wxPoint
pt( GetPosition() );
1107 wxSize
sz( GetSize() );
1109 AdjustForGrowables(sz
, m_calculatedMinSize
, nrows
, ncols
);
1111 sz
= wxSize( pt
.x
+ sz
.x
, pt
.y
+ sz
.y
);
1114 for (int c
= 0; c
< ncols
; c
++)
1117 for (int r
= 0; r
< nrows
; r
++)
1119 int i
= r
* ncols
+ c
;
1122 wxSizerItemList::compatibility_iterator node
= m_children
.Item( i
);
1124 wxASSERT_MSG( node
, _T("Failed to find node") );
1126 int w
= wxMax( 0, wxMin( m_colWidths
[c
], sz
.x
- x
) );
1127 int h
= wxMax( 0, wxMin( m_rowHeights
[r
], sz
.y
- y
) );
1129 SetItemBounds( node
->GetData(), x
, y
, w
, h
);
1131 y
= y
+ m_rowHeights
[r
] + m_vgap
;
1133 x
= x
+ m_colWidths
[c
] + m_hgap
;
1137 wxSize
wxFlexGridSizer::CalcMin()
1143 // Number of rows/columns can change as items are added or removed.
1144 if ( !CalcRowsCols(nrows
, ncols
) )
1145 return wxSize(10, 10);
1147 m_rowHeights
.SetCount(nrows
);
1148 m_colWidths
.SetCount(ncols
);
1150 // We have to recalcuate the sizes in case the item minimum size has
1151 // changed since the previous layout, or the item has been hidden using
1152 // wxSizer::Show(). If all the items in a row/column are hidden, the final
1153 // dimension of the row/column will be -1, indicating that the column
1154 // itself is hidden.
1155 for( s
= m_rowHeights
.GetCount(), i
= 0; i
< s
; ++i
)
1156 m_rowHeights
[ i
] = -1;
1157 for( s
= m_colWidths
.GetCount(), i
= 0; i
< s
; ++i
)
1158 m_colWidths
[ i
] = -1;
1160 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
1165 wxSizerItem
*item
= node
->GetData();
1166 if ( item
->IsShown() )
1168 wxSize
sz( item
->CalcMin() );
1169 int row
= i
/ ncols
;
1170 int col
= i
% ncols
;
1172 m_rowHeights
[ row
] = wxMax( wxMax( 0, sz
.y
), m_rowHeights
[ row
] );
1173 m_colWidths
[ col
] = wxMax( wxMax( 0, sz
.x
), m_colWidths
[ col
] );
1176 node
= node
->GetNext();
1180 AdjustForFlexDirection();
1182 // Sum total minimum size, including gaps between rows/columns.
1183 // -1 is used as a magic number meaning empty column.
1185 for (int col
= 0; col
< ncols
; col
++)
1186 if ( m_colWidths
[ col
] != -1 )
1187 width
+= m_colWidths
[ col
] + ( col
== ncols
-1 ? 0 : m_hgap
);
1190 for (int row
= 0; row
< nrows
; row
++)
1191 if ( m_rowHeights
[ row
] != -1 )
1192 height
+= m_rowHeights
[ row
] + ( row
== nrows
-1 ? 0 : m_vgap
);
1194 m_calculatedMinSize
= wxSize( width
, height
);
1195 return m_calculatedMinSize
;
1198 void wxFlexGridSizer::AdjustForFlexDirection()
1200 // the logic in CalcMin works when we resize flexibly in both directions
1201 // but maybe this is not the case
1202 if ( m_flexDirection
!= wxBOTH
)
1204 // select the array corresponding to the direction in which we do *not*
1206 wxArrayInt
& array
= m_flexDirection
== wxVERTICAL
? m_colWidths
1209 const int count
= array
.GetCount();
1211 // find the largest value in this array
1213 for ( n
= 0; n
< count
; ++n
)
1215 if ( array
[n
] > largest
)
1219 // and now fill it with the largest value
1220 for ( n
= 0; n
< count
; ++n
)
1228 void wxFlexGridSizer::AdjustForGrowables(const wxSize
& sz
, const wxSize
& minsz
,
1229 int nrows
, int ncols
)
1231 // what to do with the rows? by default, resize them proportionally
1232 if ( sz
.y
> minsz
.y
&& ( (m_flexDirection
& wxVERTICAL
) || (m_growMode
== wxFLEX_GROWMODE_SPECIFIED
) ) )
1234 int sum_proportions
= 0;
1235 int growable_space
= 0;
1238 for (idx
= 0; idx
< m_growableRows
.GetCount(); idx
++)
1240 // Since the number of rows/columns can change as items are
1241 // inserted/deleted, we need to verify at runtime that the
1242 // requested growable rows/columns are still valid.
1243 if (m_growableRows
[idx
] >= nrows
)
1246 // If all items in a row/column are hidden, that row/column will
1247 // have a dimension of -1. This causes the row/column to be
1248 // hidden completely.
1249 if (m_rowHeights
[ m_growableRows
[idx
] ] == -1)
1251 sum_proportions
+= m_growableRowsProportions
[idx
];
1252 growable_space
+= m_rowHeights
[ m_growableRows
[idx
] ];
1258 for (idx
= 0; idx
< m_growableRows
.GetCount(); idx
++)
1260 if (m_growableRows
[idx
] >= nrows
)
1262 if (m_rowHeights
[ m_growableRows
[idx
] ] == -1)
1263 m_rowHeights
[ m_growableRows
[idx
] ] = 0;
1266 int delta
= (sz
.y
- minsz
.y
);
1267 if (sum_proportions
== 0)
1268 delta
= (delta
/num
) + m_rowHeights
[ m_growableRows
[idx
] ];
1270 delta
= ((delta
+growable_space
)*m_growableRowsProportions
[idx
]) / sum_proportions
;
1271 m_rowHeights
[ m_growableRows
[idx
] ] = delta
;
1276 else if ( (m_growMode
== wxFLEX_GROWMODE_ALL
) && (sz
.y
> minsz
.y
) )
1278 // rounding problem?
1279 for ( int row
= 0; row
< nrows
; ++row
)
1280 m_rowHeights
[ row
] = sz
.y
/ nrows
;
1283 // the same logic as above but for the columns
1284 if ( sz
.x
> minsz
.x
&& ( (m_flexDirection
& wxHORIZONTAL
) || (m_growMode
== wxFLEX_GROWMODE_SPECIFIED
) ) )
1286 int sum_proportions
= 0;
1287 int growable_space
= 0;
1290 for (idx
= 0; idx
< m_growableCols
.GetCount(); idx
++)
1292 // Since the number of rows/columns can change as items are
1293 // inserted/deleted, we need to verify at runtime that the
1294 // requested growable rows/columns are still valid.
1295 if (m_growableCols
[idx
] >= ncols
)
1298 // If all items in a row/column are hidden, that row/column will
1299 // have a dimension of -1. This causes the column to be hidden
1301 if (m_colWidths
[ m_growableCols
[idx
] ] == -1)
1303 sum_proportions
+= m_growableColsProportions
[idx
];
1304 growable_space
+= m_colWidths
[ m_growableCols
[idx
] ];
1310 for (idx
= 0; idx
< m_growableCols
.GetCount(); idx
++)
1312 if (m_growableCols
[idx
] >= ncols
)
1314 if (m_colWidths
[ m_growableCols
[idx
] ] == -1)
1315 m_colWidths
[ m_growableCols
[idx
] ] = 0;
1318 int delta
= (sz
.x
- minsz
.x
);
1319 if (sum_proportions
== 0)
1320 delta
= (delta
/num
) + m_colWidths
[ m_growableCols
[idx
] ];
1322 delta
= ((delta
+growable_space
)*m_growableColsProportions
[idx
])/sum_proportions
;
1323 m_colWidths
[ m_growableCols
[idx
] ] = delta
;
1328 else if ( (m_growMode
== wxFLEX_GROWMODE_ALL
) && (sz
.x
> minsz
.x
) )
1330 for ( int col
=0; col
< ncols
; ++col
)
1331 m_colWidths
[ col
] = sz
.x
/ ncols
;
1336 void wxFlexGridSizer::AddGrowableRow( size_t idx
, int proportion
)
1338 m_growableRows
.Add( idx
);
1339 m_growableRowsProportions
.Add( proportion
);
1342 void wxFlexGridSizer::RemoveGrowableRow( size_t idx
)
1344 m_growableRows
.Remove( idx
);
1347 void wxFlexGridSizer::AddGrowableCol( size_t idx
, int proportion
)
1349 m_growableCols
.Add( idx
);
1350 m_growableColsProportions
.Add( proportion
);
1353 void wxFlexGridSizer::RemoveGrowableCol( size_t idx
)
1355 m_growableCols
.Remove( idx
);
1358 //---------------------------------------------------------------------------
1360 //---------------------------------------------------------------------------
1362 wxBoxSizer::wxBoxSizer( int orient
)
1363 : m_orient( orient
)
1367 void wxBoxSizer::RecalcSizes()
1369 if (m_children
.GetCount() == 0)
1375 if (m_orient
== wxHORIZONTAL
)
1376 delta
= m_size
.x
- m_fixedWidth
;
1378 delta
= m_size
.y
- m_fixedHeight
;
1381 wxPoint
pt( m_position
);
1383 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
1386 wxSizerItem
*item
= node
->GetData();
1388 if (item
->IsShown())
1390 wxSize
size( item
->GetMinSizeWithBorder() );
1392 if (m_orient
== wxVERTICAL
)
1394 wxCoord height
= size
.y
;
1395 if (item
->GetProportion())
1397 // Because of at least one visible item has non-zero
1398 // proportion then m_stretchable is not zero
1399 height
= (delta
* item
->GetProportion()) / m_stretchable
;
1402 wxPoint
child_pos( pt
);
1403 wxSize
child_size( wxSize( size
.x
, height
) );
1405 if (item
->GetFlag() & (wxEXPAND
| wxSHAPED
))
1406 child_size
.x
= m_size
.x
;
1407 else if (item
->GetFlag() & wxALIGN_RIGHT
)
1408 child_pos
.x
+= m_size
.x
- size
.x
;
1409 else if (item
->GetFlag() & (wxCENTER
| wxALIGN_CENTER_HORIZONTAL
))
1410 // XXX wxCENTER is added for backward compatibility;
1411 // wxALIGN_CENTER should be used in new code
1412 child_pos
.x
+= (m_size
.x
- size
.x
) / 2;
1414 item
->SetDimension( child_pos
, child_size
);
1420 wxCoord width
= size
.x
;
1421 if (item
->GetProportion())
1423 // Because of at least one visible item has non-zero
1424 // proportion then m_stretchable is not zero
1425 width
= (delta
* item
->GetProportion()) / m_stretchable
;
1428 wxPoint
child_pos( pt
);
1429 wxSize
child_size( wxSize(width
, size
.y
) );
1431 if (item
->GetFlag() & (wxEXPAND
| wxSHAPED
))
1432 child_size
.y
= m_size
.y
;
1433 else if (item
->GetFlag() & wxALIGN_BOTTOM
)
1434 child_pos
.y
+= m_size
.y
- size
.y
;
1435 else if (item
->GetFlag() & (wxCENTER
| wxALIGN_CENTER_VERTICAL
))
1436 // XXX wxCENTER is added for backward compatibility;
1437 // wxALIGN_CENTER should be used in new code
1438 child_pos
.y
+= (m_size
.y
- size
.y
) / 2;
1440 item
->SetDimension( child_pos
, child_size
);
1446 node
= node
->GetNext();
1450 wxSize
wxBoxSizer::CalcMin()
1452 if (m_children
.GetCount() == 0)
1453 return wxSize(10,10);
1461 // precalc item minsizes and count proportions
1462 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
1465 wxSizerItem
*item
= node
->GetData();
1467 if (item
->IsShown())
1468 item
->CalcMin(); // result is stored in the item
1470 if (item
->IsShown() && item
->GetProportion() != 0)
1471 m_stretchable
+= item
->GetProportion();
1473 node
= node
->GetNext();
1476 // Total minimum size (width or height) of sizer
1479 node
= m_children
.GetFirst();
1482 wxSizerItem
*item
= node
->GetData();
1484 if (item
->IsShown() && item
->GetProportion() != 0)
1486 int stretch
= item
->GetProportion();
1487 wxSize
size( item
->GetMinSizeWithBorder() );
1490 // Integer division rounded up is (a + b - 1) / b
1491 // Round up needed in order to guarantee that all
1492 // all items will have size not less then their min size
1493 if (m_orient
== wxHORIZONTAL
)
1494 minSize
= ( size
.x
*m_stretchable
+ stretch
- 1)/stretch
;
1496 minSize
= ( size
.y
*m_stretchable
+ stretch
- 1)/stretch
;
1498 if (minSize
> maxMinSize
)
1499 maxMinSize
= minSize
;
1501 node
= node
->GetNext();
1504 // Calculate overall minimum size
1505 node
= m_children
.GetFirst();
1508 wxSizerItem
*item
= node
->GetData();
1510 if (item
->IsShown())
1512 wxSize
size( item
->GetMinSizeWithBorder() );
1513 if (item
->GetProportion() != 0)
1515 if (m_orient
== wxHORIZONTAL
)
1516 size
.x
= (maxMinSize
*item
->GetProportion())/m_stretchable
;
1518 size
.y
= (maxMinSize
*item
->GetProportion())/m_stretchable
;
1522 if (m_orient
== wxVERTICAL
)
1524 m_fixedHeight
+= size
.y
;
1525 m_fixedWidth
= wxMax( m_fixedWidth
, size
.x
);
1529 m_fixedWidth
+= size
.x
;
1530 m_fixedHeight
= wxMax( m_fixedHeight
, size
.y
);
1534 if (m_orient
== wxHORIZONTAL
)
1536 m_minWidth
+= size
.x
;
1537 m_minHeight
= wxMax( m_minHeight
, size
.y
);
1541 m_minHeight
+= size
.y
;
1542 m_minWidth
= wxMax( m_minWidth
, size
.x
);
1545 node
= node
->GetNext();
1548 return wxSize( m_minWidth
, m_minHeight
);
1551 //---------------------------------------------------------------------------
1553 //---------------------------------------------------------------------------
1557 wxStaticBoxSizer::wxStaticBoxSizer( wxStaticBox
*box
, int orient
)
1558 : wxBoxSizer( orient
)
1559 , m_staticBox( box
)
1561 wxASSERT_MSG( box
, wxT("wxStaticBoxSizer needs a static box") );
1564 static void GetStaticBoxBorders( wxStaticBox
*box
,
1568 // this has to be done platform by platform as there is no way to
1569 // guess the thickness of a wxStaticBox border
1571 box
->GetBordersForSizer(borderTop
,borderOther
);
1572 #elif defined(__WXMAC__)
1574 static int extraTop
= -1; // Uninitted
1575 static int other
= 5;
1577 if ( extraTop
== -1 )
1579 // The minimal border used for the top. Later on the staticbox'
1580 // font height is added to this.
1583 if ( UMAGetSystemVersion() >= 0x1030 /*Panther*/ )
1585 // As indicated by the HIG, Panther needs an extra border of 11
1586 // pixels (otherwise overlapping occurs at the top). The "other"
1587 // border has to be 11.
1594 *borderTop
= extraTop
+ box
->GetCharHeight();
1595 *borderOther
= other
;
1599 if ( box
->GetLabel().IsEmpty() )
1603 *borderTop
= box
->GetCharHeight();
1606 #endif // __WXCOCOA__
1609 void wxStaticBoxSizer::RecalcSizes()
1611 int top_border
, other_border
;
1612 GetStaticBoxBorders(m_staticBox
, &top_border
, &other_border
);
1614 m_staticBox
->SetSize( m_position
.x
, m_position
.y
, m_size
.x
, m_size
.y
);
1616 wxPoint
old_pos( m_position
);
1617 m_position
.x
+= other_border
;
1618 m_position
.y
+= top_border
;
1619 wxSize
old_size( m_size
);
1620 m_size
.x
-= 2*other_border
;
1621 m_size
.y
-= top_border
+ other_border
;
1623 wxBoxSizer::RecalcSizes();
1625 m_position
= old_pos
;
1629 wxSize
wxStaticBoxSizer::CalcMin()
1631 int top_border
, other_border
;
1632 GetStaticBoxBorders(m_staticBox
, &top_border
, &other_border
);
1634 wxSize
ret( wxBoxSizer::CalcMin() );
1635 ret
.x
+= 2*other_border
;
1636 ret
.y
+= other_border
+ top_border
;
1641 void wxStaticBoxSizer::ShowItems( bool show
)
1643 m_staticBox
->Show( show
);
1644 wxBoxSizer::ShowItems( show
);
1647 #endif // wxUSE_STATBOX
1650 #if WXWIN_COMPATIBILITY_2_4
1652 // ----------------------------------------------------------------------------
1654 // ----------------------------------------------------------------------------
1657 IMPLEMENT_CLASS(wxBookCtrlSizer
, wxSizer
)
1659 IMPLEMENT_CLASS(wxNotebookSizer
, wxBookCtrlSizer
)
1660 #endif // wxUSE_NOTEBOOK
1661 #endif // wxUSE_BOOKCTRL
1665 wxBookCtrlSizer::wxBookCtrlSizer(wxBookCtrl
*bookctrl
)
1666 : m_bookctrl(bookctrl
)
1668 wxASSERT_MSG( bookctrl
, wxT("wxBookCtrlSizer needs a control") );
1671 void wxBookCtrlSizer::RecalcSizes()
1673 m_bookctrl
->SetSize( m_position
.x
, m_position
.y
, m_size
.x
, m_size
.y
);
1676 wxSize
wxBookCtrlSizer::CalcMin()
1678 wxSize sizeBorder
= m_bookctrl
->CalcSizeFromPage(wxSize(0, 0));
1683 if ( m_bookctrl
->GetPageCount() == 0 )
1685 return wxSize(sizeBorder
.x
+ 10, sizeBorder
.y
+ 10);
1691 wxWindowList::compatibility_iterator
1692 node
= m_bookctrl
->GetChildren().GetFirst();
1695 wxWindow
*item
= node
->GetData();
1696 wxSizer
*itemsizer
= item
->GetSizer();
1700 wxSize
subsize( itemsizer
->CalcMin() );
1702 if (subsize
.x
> maxX
)
1704 if (subsize
.y
> maxY
)
1708 node
= node
->GetNext();
1711 return wxSize( maxX
, maxY
) + sizeBorder
;
1716 wxNotebookSizer::wxNotebookSizer(wxNotebook
*nb
)
1718 wxASSERT_MSG( nb
, wxT("wxNotebookSizer needs a control") );
1722 #endif // wxUSE_NOTEBOOOK
1723 #endif // wxUSE_BOOKCTRL
1725 #endif // WXWIN_COMPATIBILITY_2_4