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/notebook.h"
28 #include "wx/listimpl.cpp"
31 # include "wx/mac/uma.h"
34 //---------------------------------------------------------------------------
36 IMPLEMENT_CLASS(wxSizerItem
, wxObject
)
37 IMPLEMENT_CLASS(wxSizer
, wxObject
)
38 IMPLEMENT_CLASS(wxGridSizer
, wxSizer
)
39 IMPLEMENT_CLASS(wxFlexGridSizer
, wxGridSizer
)
40 IMPLEMENT_CLASS(wxBoxSizer
, wxSizer
)
42 IMPLEMENT_CLASS(wxStaticBoxSizer
, wxBoxSizer
)
45 IMPLEMENT_CLASS(wxBookCtrlSizer
, wxSizer
)
47 IMPLEMENT_CLASS(wxNotebookSizer
, wxBookCtrlSizer
)
48 #endif // wxUSE_NOTEBOOK
49 #endif // wxUSE_BOOKCTRL
51 WX_DEFINE_EXPORTED_LIST( wxSizerItemList
);
85 //---------------------------------------------------------------------------
87 //---------------------------------------------------------------------------
89 wxSizerItem::wxSizerItem( int width
, int height
, int proportion
, int flag
, int border
, wxObject
* userData
)
92 , m_size( wxSize( width
, height
) ) // size is set directly
93 , m_minSize( m_size
) // minimal size is the initial size
94 , m_proportion( proportion
)
98 , m_userData( userData
)
103 wxSizerItem::wxSizerItem( wxWindow
*window
, int proportion
, int flag
, int border
, wxObject
* userData
)
106 , m_minSize( window
->GetSize() ) // minimal size is the initial size
107 , m_proportion( proportion
)
111 , m_userData( userData
)
113 // aspect ratio calculated from initial size
114 SetRatio( m_minSize
);
116 // m_size is calculated later
119 wxSizerItem::wxSizerItem( wxSizer
*sizer
, int proportion
, int flag
, int border
, wxObject
* userData
)
122 , m_proportion( proportion
)
127 , m_userData( userData
)
129 // m_minSize is calculated later
130 // m_size is calculated later
133 wxSizerItem::wxSizerItem()
145 wxSizerItem::~wxSizerItem()
151 m_window
->SetContainingSizer(NULL
);
153 else // we must be a sizer
160 wxSize
wxSizerItem::GetSize() const
164 ret
= m_sizer
->GetSize();
167 ret
= m_window
->GetSize();
174 if (m_flag
& wxNORTH
)
176 if (m_flag
& wxSOUTH
)
182 wxSize
wxSizerItem::CalcMin()
187 ret
= 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
)
196 if ( IsWindow() && !(m_flag
& wxFIXED_MINSIZE
) )
198 // the size of the window may change during run-time, we should
199 // use the current minimal size
200 m_minSize
= m_window
->GetAdjustedBestSize();
210 if (m_flag
& wxNORTH
)
212 if (m_flag
& wxSOUTH
)
218 void wxSizerItem::SetDimension( wxPoint pos
, wxSize size
)
220 if (m_flag
& wxSHAPED
)
222 // adjust aspect ratio
223 int rwidth
= (int) (size
.y
* m_ratio
);
227 int rheight
= (int) (size
.x
/ m_ratio
);
228 // add vertical space
229 if (m_flag
& wxALIGN_CENTER_VERTICAL
)
230 pos
.y
+= (size
.y
- rheight
) / 2;
231 else if (m_flag
& wxALIGN_BOTTOM
)
232 pos
.y
+= (size
.y
- rheight
);
233 // use reduced dimensions
236 else if (rwidth
< size
.x
)
238 // add horizontal space
239 if (m_flag
& wxALIGN_CENTER_HORIZONTAL
)
240 pos
.x
+= (size
.x
- rwidth
) / 2;
241 else if (m_flag
& wxALIGN_RIGHT
)
242 pos
.x
+= (size
.x
- rwidth
);
247 // This is what GetPosition() returns. Since we calculate
248 // borders afterwards, GetPosition() will be the left/top
249 // corner of the surrounding border.
261 if (m_flag
& wxNORTH
)
266 if (m_flag
& wxSOUTH
)
272 m_sizer
->SetDimension( pos
.x
, pos
.y
, size
.x
, size
.y
);
275 m_window
->SetSize( pos
.x
, pos
.y
, size
.x
, size
.y
, wxSIZE_ALLOW_MINUS_ONE
);
280 void wxSizerItem::DeleteWindows()
289 m_sizer
->DeleteWindows();
292 bool wxSizerItem::IsWindow() const
294 return (m_window
!= NULL
);
297 bool wxSizerItem::IsSizer() const
299 return (m_sizer
!= NULL
);
302 bool wxSizerItem::IsSpacer() const
304 return (m_window
== NULL
) && (m_sizer
== NULL
);
307 void wxSizerItem::Show( bool show
)
312 m_window
->Show( show
);
314 m_sizer
->ShowItems( show
);
316 // ... nothing else to do to hide/show spacers
319 void wxSizerItem::SetOption( int option
)
321 SetProportion( option
);
324 int wxSizerItem::GetOption() const
326 return GetProportion();
330 //---------------------------------------------------------------------------
332 //---------------------------------------------------------------------------
335 : m_minSize( wxSize( 0, 0 ) )
341 WX_CLEAR_LIST(wxSizerItemList
, m_children
);
344 void wxSizer::Add( wxWindow
*window
, int proportion
, int flag
, int border
, wxObject
* userData
)
346 m_children
.Append( new wxSizerItem( window
, proportion
, flag
, border
, userData
) );
347 window
->SetContainingSizer( this );
350 void wxSizer::Add( wxSizer
*sizer
, int proportion
, int flag
, int border
, wxObject
* userData
)
352 m_children
.Append( new wxSizerItem( sizer
, proportion
, flag
, border
, userData
) );
355 void wxSizer::Add( int width
, int height
, int proportion
, int flag
, int border
, wxObject
* userData
)
357 m_children
.Append( new wxSizerItem( width
, height
, proportion
, flag
, border
, userData
) );
360 void wxSizer::Add( wxSizerItem
*item
)
362 m_children
.Append( item
);
364 if( item
->GetWindow() )
365 item
->GetWindow()->SetContainingSizer( this );
368 void wxSizer::Prepend( wxWindow
*window
, int proportion
, int flag
, int border
, wxObject
* userData
)
370 m_children
.Insert( new wxSizerItem( window
, proportion
, flag
, border
, userData
) );
371 window
->SetContainingSizer( this );
374 void wxSizer::Prepend( wxSizer
*sizer
, int proportion
, int flag
, int border
, wxObject
* userData
)
376 m_children
.Insert( new wxSizerItem( sizer
, proportion
, flag
, border
, userData
) );
379 void wxSizer::Prepend( int width
, int height
, int proportion
, int flag
, int border
, wxObject
* userData
)
381 m_children
.Insert( new wxSizerItem( width
, height
, proportion
, flag
, border
, userData
) );
384 void wxSizer::Prepend( wxSizerItem
*item
)
386 m_children
.Insert( item
);
388 if( item
->GetWindow() )
389 item
->GetWindow()->SetContainingSizer( this );
392 void wxSizer::Insert( size_t index
,
399 m_children
.Insert( index
,
400 new wxSizerItem( window
, proportion
, flag
, border
, userData
) );
401 window
->SetContainingSizer( this );
404 void wxSizer::Insert( size_t index
,
411 m_children
.Insert( index
,
412 new wxSizerItem( sizer
, proportion
, flag
, border
, userData
) );
415 void wxSizer::Insert( size_t index
,
423 m_children
.Insert( index
,
424 new wxSizerItem( width
, height
, proportion
, flag
, border
, userData
) );
427 void wxSizer::Insert( size_t index
, wxSizerItem
*item
)
429 m_children
.Insert( index
, item
);
431 if( item
->GetWindow() )
432 item
->GetWindow()->SetContainingSizer( this );
435 bool wxSizer::Remove( wxWindow
*window
)
437 return Detach( window
);
440 bool wxSizer::Remove( wxSizer
*sizer
)
442 wxASSERT_MSG( sizer
, _T("Removing NULL sizer") );
444 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
447 wxSizerItem
*item
= node
->GetData();
449 if (item
->GetSizer() == sizer
)
452 m_children
.Erase( node
);
456 node
= node
->GetNext();
462 bool wxSizer::Remove( int index
)
464 wxCHECK_MSG( index
>= 0 && (size_t)index
< m_children
.GetCount(),
466 _T("Remove index is out of range") );
468 wxSizerItemList::compatibility_iterator node
= m_children
.Item( index
);
470 wxCHECK_MSG( node
, false, _T("Failed to find child node") );
472 wxSizerItem
*item
= node
->GetData();
474 if( item
->IsWindow() )
475 item
->GetWindow()->SetContainingSizer( NULL
);
478 m_children
.Erase( node
);
482 bool wxSizer::Detach( wxSizer
*sizer
)
484 wxASSERT_MSG( sizer
, _T("Detaching NULL sizer") );
486 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
489 wxSizerItem
*item
= node
->GetData();
491 if (item
->GetSizer() == sizer
)
495 m_children
.Erase( node
);
498 node
= node
->GetNext();
504 bool wxSizer::Detach( wxWindow
*window
)
506 wxASSERT_MSG( window
, _T("Detaching NULL window") );
508 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
511 wxSizerItem
*item
= node
->GetData();
513 if (item
->GetWindow() == window
)
515 item
->GetWindow()->SetContainingSizer( NULL
);
517 m_children
.Erase( node
);
520 node
= node
->GetNext();
526 bool wxSizer::Detach( int index
)
528 wxCHECK_MSG( index
>= 0 && (size_t)index
< m_children
.GetCount(),
530 _T("Detach index is out of range") );
532 wxSizerItemList::compatibility_iterator node
= m_children
.Item( index
);
534 wxCHECK_MSG( node
, false, _T("Failed to find child node") );
536 wxSizerItem
*item
= node
->GetData();
538 if( item
->IsSizer() )
540 else if( item
->IsWindow() )
541 item
->GetWindow()->SetContainingSizer( NULL
);
544 m_children
.Erase( node
);
548 void wxSizer::Clear( bool delete_windows
)
550 // First clear the ContainingSizer pointers
551 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
554 wxSizerItem
*item
= node
->GetData();
556 if (item
->IsWindow())
557 item
->GetWindow()->SetContainingSizer( NULL
);
558 node
= node
->GetNext();
561 // Destroy the windows if needed
565 // Now empty the list
566 WX_CLEAR_LIST(wxSizerItemList
, m_children
);
569 void wxSizer::DeleteWindows()
571 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
574 wxSizerItem
*item
= node
->GetData();
576 item
->DeleteWindows();
577 node
= node
->GetNext();
581 wxSize
wxSizer::Fit( wxWindow
*window
)
583 wxSize
size(window
->IsTopLevel() ? FitSize(window
)
584 : GetMinWindowSize(window
));
586 window
->SetSize( size
);
591 void wxSizer::FitInside( wxWindow
*window
)
594 if (window
->IsTopLevel())
595 size
= VirtualFitSize( window
);
597 size
= GetMinClientSize( window
);
599 window
->SetVirtualSize( size
);
602 void wxSizer::Layout()
608 void wxSizer::SetSizeHints( wxWindow
*window
)
610 // Preserve the window's max size hints, but set the
611 // lower bound according to the sizer calculations.
613 wxSize size
= Fit( window
);
615 window
->SetSizeHints( size
.x
,
617 window
->GetMaxWidth(),
618 window
->GetMaxHeight() );
621 void wxSizer::SetVirtualSizeHints( wxWindow
*window
)
623 // Preserve the window's max size hints, but set the
624 // lower bound according to the sizer calculations.
627 wxSize
size( window
->GetVirtualSize() );
628 window
->SetVirtualSizeHints( size
.x
,
630 window
->GetMaxWidth(),
631 window
->GetMaxHeight() );
634 wxSize
wxSizer::GetMaxWindowSize( wxWindow
*window
) const
636 return window
->GetMaxSize();
639 wxSize
wxSizer::GetMinWindowSize( wxWindow
*window
)
641 wxSize
minSize( GetMinSize() );
642 wxSize
size( window
->GetSize() );
643 wxSize
client_size( window
->GetClientSize() );
645 return wxSize( minSize
.x
+size
.x
-client_size
.x
,
646 minSize
.y
+size
.y
-client_size
.y
);
649 // TODO on mac we need a function that determines how much free space this
650 // min size contains, in order to make sure that we have 20 pixels of free
651 // space around the controls
653 // Return a window size that will fit within the screens dimensions
654 wxSize
wxSizer::FitSize( wxWindow
*window
)
656 wxSize size
= GetMinWindowSize( window
);
657 wxSize sizeMax
= GetMaxWindowSize( window
);
659 // Limit the size if sizeMax != wxDefaultSize
661 if ( size
.x
> sizeMax
.x
&& sizeMax
.x
!= -1 )
663 if ( size
.y
> sizeMax
.y
&& sizeMax
.y
!= -1 )
669 wxSize
wxSizer::GetMaxClientSize( wxWindow
*window
) const
671 wxSize
maxSize( window
->GetMaxSize() );
673 if( maxSize
!= wxDefaultSize
)
675 wxSize
size( window
->GetSize() );
676 wxSize
client_size( window
->GetClientSize() );
678 return wxSize( maxSize
.x
+ client_size
.x
- size
.x
,
679 maxSize
.y
+ client_size
.y
- size
.y
);
682 return wxDefaultSize
;
685 wxSize
wxSizer::GetMinClientSize( wxWindow
*WXUNUSED(window
) )
687 return GetMinSize(); // Already returns client size.
690 wxSize
wxSizer::VirtualFitSize( wxWindow
*window
)
692 wxSize size
= GetMinClientSize( window
);
693 wxSize sizeMax
= GetMaxClientSize( window
);
695 // Limit the size if sizeMax != wxDefaultSize
697 if ( size
.x
> sizeMax
.x
&& sizeMax
.x
!= -1 )
699 if ( size
.y
> sizeMax
.y
&& sizeMax
.y
!= -1 )
705 void wxSizer::SetDimension( int x
, int y
, int width
, int height
)
714 wxSize
wxSizer::GetMinSize()
716 wxSize
ret( CalcMin() );
717 if (ret
.x
< m_minSize
.x
) ret
.x
= m_minSize
.x
;
718 if (ret
.y
< m_minSize
.y
) ret
.y
= m_minSize
.y
;
722 void wxSizer::DoSetMinSize( int width
, int height
)
725 m_minSize
.y
= height
;
728 bool wxSizer::DoSetItemMinSize( wxWindow
*window
, int width
, int height
)
730 wxASSERT_MSG( window
, _T("SetMinSize for NULL window") );
732 // Is it our immediate child?
734 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
737 wxSizerItem
*item
= node
->GetData();
739 if (item
->GetWindow() == window
)
741 item
->SetMinSize( width
, height
);
744 node
= node
->GetNext();
747 // No? Search any subsizers we own then
749 node
= m_children
.GetFirst();
752 wxSizerItem
*item
= node
->GetData();
754 if ( item
->GetSizer() &&
755 item
->GetSizer()->DoSetItemMinSize( window
, width
, height
) )
757 // A child sizer found the requested windw, exit.
760 node
= node
->GetNext();
766 bool wxSizer::DoSetItemMinSize( wxSizer
*sizer
, int width
, int height
)
768 wxASSERT_MSG( sizer
, _T("SetMinSize for NULL sizer") );
770 // Is it our immediate child?
772 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
775 wxSizerItem
*item
= node
->GetData();
777 if (item
->GetSizer() == sizer
)
779 item
->GetSizer()->DoSetMinSize( width
, height
);
782 node
= node
->GetNext();
785 // No? Search any subsizers we own then
787 node
= m_children
.GetFirst();
790 wxSizerItem
*item
= node
->GetData();
792 if ( item
->GetSizer() &&
793 item
->GetSizer()->DoSetItemMinSize( sizer
, width
, height
) )
795 // A child found the requested sizer, exit.
798 node
= node
->GetNext();
804 bool wxSizer::DoSetItemMinSize( size_t index
, int width
, int height
)
806 wxSizerItemList::compatibility_iterator node
= m_children
.Item( index
);
808 wxCHECK_MSG( node
, false, _T("Failed to find child node") );
810 wxSizerItem
*item
= node
->GetData();
812 if (item
->GetSizer())
814 // Sizers contains the minimal size in them, if not calculated ...
815 item
->GetSizer()->DoSetMinSize( width
, height
);
819 // ... but the minimal size of spacers and windows in stored in them
820 item
->SetMinSize( width
, height
);
826 void wxSizer::Show( wxWindow
*window
, bool show
)
828 wxASSERT_MSG( window
, _T("Show for NULL window") );
830 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
833 wxSizerItem
*item
= node
->GetData();
835 if (item
->GetWindow() == window
)
840 node
= node
->GetNext();
844 void wxSizer::Show( wxSizer
*sizer
, bool show
)
846 wxASSERT_MSG( sizer
, _T("Show for NULL sizer") );
848 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
851 wxSizerItem
*item
= node
->GetData();
853 if (item
->GetSizer() == sizer
)
858 node
= node
->GetNext();
862 void wxSizer::Show( size_t index
, bool show
)
864 wxCHECK_RET( index
< m_children
.GetCount(),
865 _T("Show index is out of range") );
867 m_children
.Item( index
)->GetData()->Show( show
);
870 void wxSizer::ShowItems( bool show
)
872 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
875 node
->GetData()->Show( show
);
876 node
= node
->GetNext();
880 bool wxSizer::IsShown( wxWindow
*window
) const
882 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
885 wxSizerItem
*item
= node
->GetData();
887 if (item
->GetWindow() == window
)
889 return item
->IsShown();
891 node
= node
->GetNext();
894 wxFAIL_MSG( _T("IsShown failed to find sizer item") );
899 bool wxSizer::IsShown( wxSizer
*sizer
) const
901 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
904 wxSizerItem
*item
= node
->GetData();
906 if (item
->GetSizer() == sizer
)
908 return item
->IsShown();
910 node
= node
->GetNext();
913 wxFAIL_MSG( _T("IsShown failed to find sizer item") );
918 bool wxSizer::IsShown( size_t index
) const
920 wxCHECK_MSG( index
< m_children
.GetCount(),
922 _T("IsShown index is out of range") );
924 return m_children
.Item( index
)->GetData()->IsShown();
928 //---------------------------------------------------------------------------
930 //---------------------------------------------------------------------------
932 wxGridSizer::wxGridSizer( int rows
, int cols
, int vgap
, int hgap
)
938 if (m_rows
== 0 && m_cols
== 0)
942 wxGridSizer::wxGridSizer( int cols
, int vgap
, int hgap
)
948 if (m_rows
== 0 && m_cols
== 0)
952 int wxGridSizer::CalcRowsCols(int& nrows
, int& ncols
) const
954 int nitems
= m_children
.GetCount();
960 nrows
= (nitems
+ m_cols
- 1) / m_cols
;
964 ncols
= (nitems
+ m_rows
- 1) / m_rows
;
967 else // 0 columns, 0 rows?
969 wxFAIL_MSG( _T("grid sizer must have either rows or columns fixed") );
978 void wxGridSizer::RecalcSizes()
980 int nitems
, nrows
, ncols
;
981 if ( (nitems
= CalcRowsCols(nrows
, ncols
)) == 0 )
984 wxSize
sz( GetSize() );
985 wxPoint
pt( GetPosition() );
987 int w
= (sz
.x
- (ncols
- 1) * m_hgap
) / ncols
;
988 int h
= (sz
.y
- (nrows
- 1) * m_vgap
) / nrows
;
991 for (int c
= 0; c
< ncols
; c
++)
994 for (int r
= 0; r
< nrows
; r
++)
996 int i
= r
* ncols
+ c
;
999 wxSizerItemList::compatibility_iterator node
= m_children
.Item( i
);
1001 wxASSERT_MSG( node
, _T("Failed to find SizerItemList node") );
1003 SetItemBounds( node
->GetData(), x
, y
, w
, h
);
1011 wxSize
wxGridSizer::CalcMin()
1014 if ( CalcRowsCols(nrows
, ncols
) == 0 )
1015 return wxSize(10, 10);
1017 // Find the max width and height for any component
1021 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
1024 wxSizerItem
*item
= node
->GetData();
1025 wxSize
sz( item
->CalcMin() );
1027 w
= wxMax( w
, sz
.x
);
1028 h
= wxMax( h
, sz
.y
);
1030 node
= node
->GetNext();
1033 return wxSize( ncols
* w
+ (ncols
-1) * m_hgap
,
1034 nrows
* h
+ (nrows
-1) * m_vgap
);
1037 void wxGridSizer::SetItemBounds( wxSizerItem
*item
, int x
, int y
, int w
, int h
)
1040 wxSize
sz( item
->CalcMin() );
1041 int flag
= item
->GetFlag();
1043 if ((flag
& wxEXPAND
) || (flag
& wxSHAPED
))
1049 if (flag
& wxALIGN_CENTER_HORIZONTAL
)
1051 pt
.x
= x
+ (w
- sz
.x
) / 2;
1053 else if (flag
& wxALIGN_RIGHT
)
1055 pt
.x
= x
+ (w
- sz
.x
);
1058 if (flag
& wxALIGN_CENTER_VERTICAL
)
1060 pt
.y
= y
+ (h
- sz
.y
) / 2;
1062 else if (flag
& wxALIGN_BOTTOM
)
1064 pt
.y
= y
+ (h
- sz
.y
);
1068 item
->SetDimension(pt
, sz
);
1071 //---------------------------------------------------------------------------
1073 //---------------------------------------------------------------------------
1075 wxFlexGridSizer::wxFlexGridSizer( int rows
, int cols
, int vgap
, int hgap
)
1076 : wxGridSizer( rows
, cols
, vgap
, hgap
),
1077 m_flexDirection(wxBOTH
),
1078 m_growMode(wxFLEX_GROWMODE_SPECIFIED
)
1082 wxFlexGridSizer::wxFlexGridSizer( int cols
, int vgap
, int hgap
)
1083 : wxGridSizer( cols
, vgap
, hgap
),
1084 m_flexDirection(wxBOTH
),
1085 m_growMode(wxFLEX_GROWMODE_SPECIFIED
)
1089 wxFlexGridSizer::~wxFlexGridSizer()
1093 void wxFlexGridSizer::RecalcSizes()
1095 int nitems
, nrows
, ncols
;
1096 if ( (nitems
= CalcRowsCols(nrows
, ncols
)) == 0 )
1099 wxPoint
pt( GetPosition() );
1100 wxSize
sz( GetSize() );
1101 wxSize
minsz( CalcMin() );
1103 AdjustForGrowables(sz
, minsz
, nrows
, ncols
);
1105 sz
= wxSize( pt
.x
+ sz
.x
, pt
.y
+ sz
.y
);
1108 for (int c
= 0; c
< ncols
; c
++)
1111 for (int r
= 0; r
< nrows
; r
++)
1113 int i
= r
* ncols
+ c
;
1116 wxSizerItemList::compatibility_iterator node
= m_children
.Item( i
);
1118 wxASSERT_MSG( node
, _T("Failed to find node") );
1120 int w
= wxMax( 0, wxMin( m_colWidths
[c
], sz
.x
- x
) );
1121 int h
= wxMax( 0, wxMin( m_rowHeights
[r
], sz
.y
- y
) );
1123 SetItemBounds( node
->GetData(), x
, y
, w
, h
);
1125 y
= y
+ m_rowHeights
[r
] + m_vgap
;
1127 x
= x
+ m_colWidths
[c
] + m_hgap
;
1131 wxSize
wxFlexGridSizer::CalcMin()
1137 // Number of rows/columns can change as items are added or removed.
1138 if ( !CalcRowsCols(nrows
, ncols
) )
1139 return wxSize(10, 10);
1141 m_rowHeights
.SetCount(nrows
);
1142 m_colWidths
.SetCount(ncols
);
1144 // We have to recalcuate the sizes in case the item minimum size has
1145 // changed since the previous layout, or the item has been hidden using
1146 // wxSizer::Show(). If all the items in a row/column are hidden, the final
1147 // dimension of the row/column will be -1, indicating that the column
1148 // itself is hidden.
1149 for( s
= m_rowHeights
.GetCount(), i
= 0; i
< s
; ++i
)
1150 m_rowHeights
[ i
] = -1;
1151 for( s
= m_colWidths
.GetCount(), i
= 0; i
< s
; ++i
)
1152 m_colWidths
[ i
] = -1;
1154 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
1159 wxSizerItem
*item
= node
->GetData();
1160 if ( item
->IsShown() )
1162 wxSize
sz( item
->CalcMin() );
1163 int row
= i
/ ncols
;
1164 int col
= i
% ncols
;
1166 m_rowHeights
[ row
] = wxMax( wxMax( 0, sz
.y
), m_rowHeights
[ row
] );
1167 m_colWidths
[ col
] = wxMax( wxMax( 0, sz
.x
), m_colWidths
[ col
] );
1170 node
= node
->GetNext();
1174 AdjustForFlexDirection();
1176 // Sum total minimum size, including gaps between rows/columns.
1177 // -1 is used as a magic number meaning empty column.
1179 for (int col
= 0; col
< ncols
; col
++)
1180 if ( m_colWidths
[ col
] != -1 )
1181 width
+= m_colWidths
[ col
] + ( col
== ncols
-1 ? 0 : m_hgap
);
1184 for (int row
= 0; row
< nrows
; row
++)
1185 if ( m_rowHeights
[ row
] != -1 )
1186 height
+= m_rowHeights
[ row
] + ( row
== nrows
-1 ? 0 : m_vgap
);
1188 return wxSize( width
, height
);
1191 void wxFlexGridSizer::AdjustForFlexDirection()
1193 // the logic in CalcMin works when we resize flexibly in both directions
1194 // but maybe this is not the case
1195 if ( m_flexDirection
!= wxBOTH
)
1197 // select the array corresponding to the direction in which we do *not*
1199 wxArrayInt
& array
= m_flexDirection
== wxVERTICAL
? m_colWidths
1202 const int count
= array
.GetCount();
1204 // find the largest value in this array
1206 for ( n
= 0; n
< count
; ++n
)
1208 if ( array
[n
] > largest
)
1212 // and now fill it with the largest value
1213 for ( n
= 0; n
< count
; ++n
)
1221 void wxFlexGridSizer::AdjustForGrowables(const wxSize
& sz
, const wxSize
& minsz
,
1222 int nrows
, int ncols
)
1224 // what to do with the rows? by default, resize them proportionally
1225 if ( sz
.y
> minsz
.y
&& ( (m_flexDirection
& wxVERTICAL
) || (m_growMode
== wxFLEX_GROWMODE_SPECIFIED
) ) )
1227 int sum_proportions
= 0;
1228 int growable_space
= 0;
1231 for (idx
= 0; idx
< m_growableRows
.GetCount(); idx
++)
1233 // Since the number of rows/columns can change as items are
1234 // inserted/deleted, we need to verify at runtime that the
1235 // requested growable rows/columns are still valid.
1236 if (m_growableRows
[idx
] >= nrows
)
1239 // If all items in a row/column are hidden, that row/column will
1240 // have a dimension of -1. This causes the row/column to be
1241 // hidden completely.
1242 if (m_rowHeights
[ m_growableRows
[idx
] ] == -1)
1244 sum_proportions
+= m_growableRowsProportions
[idx
];
1245 growable_space
+= m_rowHeights
[ m_growableRows
[idx
] ];
1251 for (idx
= 0; idx
< m_growableRows
.GetCount(); idx
++)
1253 if (m_growableRows
[idx
] >= nrows
)
1255 if (m_rowHeights
[ m_growableRows
[idx
] ] == -1)
1256 m_rowHeights
[ m_growableRows
[idx
] ] = 0;
1259 int delta
= (sz
.y
- minsz
.y
);
1260 if (sum_proportions
== 0)
1261 delta
= (delta
/num
) + m_rowHeights
[ m_growableRows
[idx
] ];
1263 delta
= ((delta
+growable_space
)*m_growableRowsProportions
[idx
]) / sum_proportions
;
1264 m_rowHeights
[ m_growableRows
[idx
] ] = delta
;
1269 else if ( (m_growMode
== wxFLEX_GROWMODE_ALL
) && (sz
.y
> minsz
.y
) )
1271 // rounding problem?
1272 for ( int row
= 0; row
< nrows
; ++row
)
1273 m_rowHeights
[ row
] = sz
.y
/ nrows
;
1276 // the same logic as above but for the columns
1277 if ( sz
.x
> minsz
.x
&& ( (m_flexDirection
& wxHORIZONTAL
) || (m_growMode
== wxFLEX_GROWMODE_SPECIFIED
) ) )
1279 int sum_proportions
= 0;
1280 int growable_space
= 0;
1283 for (idx
= 0; idx
< m_growableCols
.GetCount(); idx
++)
1285 // Since the number of rows/columns can change as items are
1286 // inserted/deleted, we need to verify at runtime that the
1287 // requested growable rows/columns are still valid.
1288 if (m_growableCols
[idx
] >= ncols
)
1291 // If all items in a row/column are hidden, that row/column will
1292 // have a dimension of -1. This causes the column to be hidden
1294 if (m_colWidths
[ m_growableCols
[idx
] ] == -1)
1296 sum_proportions
+= m_growableColsProportions
[idx
];
1297 growable_space
+= m_colWidths
[ m_growableCols
[idx
] ];
1303 for (idx
= 0; idx
< m_growableCols
.GetCount(); idx
++)
1305 if (m_growableCols
[idx
] >= ncols
)
1307 if (m_colWidths
[ m_growableCols
[idx
] ] == -1)
1308 m_colWidths
[ m_growableCols
[idx
] ] = 0;
1311 int delta
= (sz
.x
- minsz
.x
);
1312 if (sum_proportions
== 0)
1313 delta
= (delta
/num
) + m_colWidths
[ m_growableCols
[idx
] ];
1315 delta
= ((delta
+growable_space
)*m_growableColsProportions
[idx
])/sum_proportions
;
1316 m_colWidths
[ m_growableCols
[idx
] ] = delta
;
1321 else if ( (m_growMode
== wxFLEX_GROWMODE_ALL
) && (sz
.x
> minsz
.x
) )
1323 for ( int col
=0; col
< ncols
; ++col
)
1324 m_colWidths
[ col
] = sz
.x
/ ncols
;
1329 void wxFlexGridSizer::AddGrowableRow( size_t idx
, int proportion
)
1331 m_growableRows
.Add( idx
);
1332 m_growableRowsProportions
.Add( proportion
);
1335 void wxFlexGridSizer::RemoveGrowableRow( size_t idx
)
1337 m_growableRows
.Remove( idx
);
1340 void wxFlexGridSizer::AddGrowableCol( size_t idx
, int proportion
)
1342 m_growableCols
.Add( idx
);
1343 m_growableColsProportions
.Add( proportion
);
1346 void wxFlexGridSizer::RemoveGrowableCol( size_t idx
)
1348 m_growableCols
.Remove( idx
);
1351 //---------------------------------------------------------------------------
1353 //---------------------------------------------------------------------------
1355 wxBoxSizer::wxBoxSizer( int orient
)
1356 : m_orient( orient
)
1360 void wxBoxSizer::RecalcSizes()
1362 if (m_children
.GetCount() == 0)
1368 if (m_orient
== wxHORIZONTAL
)
1369 delta
= m_size
.x
- m_fixedWidth
;
1371 delta
= m_size
.y
- m_fixedHeight
;
1374 wxPoint
pt( m_position
);
1376 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
1379 wxSizerItem
*item
= node
->GetData();
1381 if (item
->IsShown())
1383 wxSize
size( item
->CalcMin() );
1385 if (m_orient
== wxVERTICAL
)
1387 wxCoord height
= size
.y
;
1388 if (item
->GetProportion())
1390 // Because of at least one visible item has non-zero
1391 // proportion then m_stretchable is not zero
1392 height
= (delta
* item
->GetProportion()) / m_stretchable
;
1395 wxPoint
child_pos( pt
);
1396 wxSize
child_size( wxSize( size
.x
, height
) );
1398 if (item
->GetFlag() & (wxEXPAND
| wxSHAPED
))
1399 child_size
.x
= m_size
.x
;
1400 else if (item
->GetFlag() & wxALIGN_RIGHT
)
1401 child_pos
.x
+= m_size
.x
- size
.x
;
1402 else if (item
->GetFlag() & (wxCENTER
| wxALIGN_CENTER_HORIZONTAL
))
1403 // XXX wxCENTER is added for backward compatibility;
1404 // wxALIGN_CENTER should be used in new code
1405 child_pos
.x
+= (m_size
.x
- size
.x
) / 2;
1407 item
->SetDimension( child_pos
, child_size
);
1413 wxCoord width
= size
.x
;
1414 if (item
->GetProportion())
1416 // Because of at least one visible item has non-zero
1417 // proportion then m_stretchable is not zero
1418 width
= (delta
* item
->GetProportion()) / m_stretchable
;
1421 wxPoint
child_pos( pt
);
1422 wxSize
child_size( wxSize(width
, size
.y
) );
1424 if (item
->GetFlag() & (wxEXPAND
| wxSHAPED
))
1425 child_size
.y
= m_size
.y
;
1426 else if (item
->GetFlag() & wxALIGN_BOTTOM
)
1427 child_pos
.y
+= m_size
.y
- size
.y
;
1428 else if (item
->GetFlag() & (wxCENTER
| wxALIGN_CENTER_VERTICAL
))
1429 // XXX wxCENTER is added for backward compatibility;
1430 // wxALIGN_CENTER should be used in new code
1431 child_pos
.y
+= (m_size
.y
- size
.y
) / 2;
1433 item
->SetDimension( child_pos
, child_size
);
1439 node
= node
->GetNext();
1443 wxSize
wxBoxSizer::CalcMin()
1445 if (m_children
.GetCount() == 0)
1446 return wxSize(10,10);
1454 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
1457 wxSizerItem
*item
= node
->GetData();
1459 if (item
->IsShown() && item
->GetProportion() != 0)
1460 m_stretchable
+= item
->GetProportion();
1462 node
= node
->GetNext();
1465 // Total minimum size (width or height) of sizer
1468 node
= m_children
.GetFirst();
1471 wxSizerItem
*item
= node
->GetData();
1473 if (item
->IsShown() && item
->GetProportion() != 0)
1475 int stretch
= item
->GetProportion();
1476 wxSize
size( item
->CalcMin() );
1479 // Integer division rounded up is (a + b - 1) / b
1480 // Round up needed in order to guarantee that all
1481 // all items will have size not less then their min size
1482 if (m_orient
== wxHORIZONTAL
)
1483 minSize
= ( size
.x
*m_stretchable
+ stretch
- 1)/stretch
;
1485 minSize
= ( size
.y
*m_stretchable
+ stretch
- 1)/stretch
;
1487 if (minSize
> maxMinSize
)
1488 maxMinSize
= minSize
;
1490 node
= node
->GetNext();
1493 // Calculate overall minimum size
1494 node
= m_children
.GetFirst();
1497 wxSizerItem
*item
= node
->GetData();
1499 if (item
->IsShown())
1501 wxSize
size( item
->CalcMin() );
1502 if (item
->GetProportion() != 0)
1504 if (m_orient
== wxHORIZONTAL
)
1505 size
.x
= (maxMinSize
*item
->GetProportion())/m_stretchable
;
1507 size
.y
= (maxMinSize
*item
->GetProportion())/m_stretchable
;
1511 if (m_orient
== wxVERTICAL
)
1513 m_fixedHeight
+= size
.y
;
1514 m_fixedWidth
= wxMax( m_fixedWidth
, size
.x
);
1518 m_fixedWidth
+= size
.x
;
1519 m_fixedHeight
= wxMax( m_fixedHeight
, size
.y
);
1523 if (m_orient
== wxHORIZONTAL
)
1525 m_minWidth
+= size
.x
;
1526 m_minHeight
= wxMax( m_minHeight
, size
.y
);
1530 m_minHeight
+= size
.y
;
1531 m_minWidth
= wxMax( m_minWidth
, size
.x
);
1534 node
= node
->GetNext();
1537 return wxSize( m_minWidth
, m_minHeight
);
1540 //---------------------------------------------------------------------------
1542 //---------------------------------------------------------------------------
1546 wxStaticBoxSizer::wxStaticBoxSizer( wxStaticBox
*box
, int orient
)
1547 : wxBoxSizer( orient
)
1548 , m_staticBox( box
)
1550 wxASSERT_MSG( box
, wxT("wxStaticBoxSizer needs a static box") );
1553 static void GetStaticBoxBorders( wxStaticBox
*box
,
1557 // this has to be done platform by platform as there is no way to
1558 // guess the thickness of a wxStaticBox border
1560 box
->GetBordersForSizer(borderTop
,borderOther
);
1561 #elif defined(__WXMAC__)
1563 static int extraTop
= -1; // Uninitted
1564 static int other
= 5;
1566 if ( extraTop
== -1 )
1568 // The minimal border used for the top. Later on the staticbox'
1569 // font height is added to this.
1572 if ( UMAGetSystemVersion() >= 0x1030 /*Panther*/ )
1574 // As indicated by the HIG, Panther needs an extra border of 11
1575 // pixels (otherwise overlapping occurs at the top). The "other"
1576 // border has to be 11.
1583 *borderTop
= extraTop
+ box
->GetCharHeight();
1584 *borderOther
= other
;
1588 if ( box
->GetLabel().IsEmpty() )
1592 *borderTop
= box
->GetCharHeight();
1595 #endif // __WXCOCOA__
1598 void wxStaticBoxSizer::RecalcSizes()
1600 int top_border
, other_border
;
1601 GetStaticBoxBorders(m_staticBox
, &top_border
, &other_border
);
1603 m_staticBox
->SetSize( m_position
.x
, m_position
.y
, m_size
.x
, m_size
.y
);
1605 wxPoint
old_pos( m_position
);
1606 m_position
.x
+= other_border
;
1607 m_position
.y
+= top_border
;
1608 wxSize
old_size( m_size
);
1609 m_size
.x
-= 2*other_border
;
1610 m_size
.y
-= top_border
+ other_border
;
1612 wxBoxSizer::RecalcSizes();
1614 m_position
= old_pos
;
1618 wxSize
wxStaticBoxSizer::CalcMin()
1620 int top_border
, other_border
;
1621 GetStaticBoxBorders(m_staticBox
, &top_border
, &other_border
);
1623 wxSize
ret( wxBoxSizer::CalcMin() );
1624 ret
.x
+= 2*other_border
;
1625 ret
.y
+= other_border
+ top_border
;
1630 void wxStaticBoxSizer::ShowItems( bool show
)
1632 m_staticBox
->Show( show
);
1633 wxBoxSizer::ShowItems( show
);
1636 #endif // wxUSE_STATBOX
1638 // ----------------------------------------------------------------------------
1640 // ----------------------------------------------------------------------------
1644 wxBookCtrlSizer::wxBookCtrlSizer(wxBookCtrl
*bookctrl
)
1645 : m_bookctrl(bookctrl
)
1647 wxASSERT_MSG( bookctrl
, wxT("wxBookCtrlSizer needs a control") );
1650 void wxBookCtrlSizer::RecalcSizes()
1652 m_bookctrl
->SetSize( m_position
.x
, m_position
.y
, m_size
.x
, m_size
.y
);
1655 wxSize
wxBookCtrlSizer::CalcMin()
1657 wxSize sizeBorder
= m_bookctrl
->CalcSizeFromPage(wxSize(0, 0));
1662 if ( m_bookctrl
->GetPageCount() == 0 )
1664 return wxSize(sizeBorder
.x
+ 10, sizeBorder
.y
+ 10);
1670 wxWindowList::compatibility_iterator
1671 node
= m_bookctrl
->GetChildren().GetFirst();
1674 wxWindow
*item
= node
->GetData();
1675 wxSizer
*itemsizer
= item
->GetSizer();
1679 wxSize
subsize( itemsizer
->CalcMin() );
1681 if (subsize
.x
> maxX
)
1683 if (subsize
.y
> maxY
)
1687 node
= node
->GetNext();
1690 return wxSize( maxX
, maxY
) + sizeBorder
;
1696 wxNotebookSizer::wxNotebookSizer(wxNotebook
*nb
)
1697 : wxBookCtrlSizer(nb
)
1701 #endif // wxUSE_NOTEBOOOK
1702 #endif // wxUSE_BOOKCTRL