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 // Since the size of the window may change during runtime, we
199 // should use the current minimal size. If there is a MinSize,
200 // use it, otherwise use the BestSize.
201 wxSize min
= m_window
->GetMinSize();
202 if (min
.x
== -1 || min
.y
== -1)
204 wxSize best
= m_window
->GetBestSize();
205 if (min
.x
== -1) min
.x
= best
.x
;
206 if (min
.y
== -1) min
.y
= best
.y
;
218 if (m_flag
& wxNORTH
)
220 if (m_flag
& wxSOUTH
)
226 void wxSizerItem::SetDimension( wxPoint pos
, wxSize size
)
228 if (m_flag
& wxSHAPED
)
230 // adjust aspect ratio
231 int rwidth
= (int) (size
.y
* m_ratio
);
235 int rheight
= (int) (size
.x
/ m_ratio
);
236 // add vertical space
237 if (m_flag
& wxALIGN_CENTER_VERTICAL
)
238 pos
.y
+= (size
.y
- rheight
) / 2;
239 else if (m_flag
& wxALIGN_BOTTOM
)
240 pos
.y
+= (size
.y
- rheight
);
241 // use reduced dimensions
244 else if (rwidth
< size
.x
)
246 // add horizontal space
247 if (m_flag
& wxALIGN_CENTER_HORIZONTAL
)
248 pos
.x
+= (size
.x
- rwidth
) / 2;
249 else if (m_flag
& wxALIGN_RIGHT
)
250 pos
.x
+= (size
.x
- rwidth
);
255 // This is what GetPosition() returns. Since we calculate
256 // borders afterwards, GetPosition() will be the left/top
257 // corner of the surrounding border.
269 if (m_flag
& wxNORTH
)
274 if (m_flag
& wxSOUTH
)
280 m_sizer
->SetDimension( pos
.x
, pos
.y
, size
.x
, size
.y
);
283 m_window
->SetSize( pos
.x
, pos
.y
, size
.x
, size
.y
, wxSIZE_ALLOW_MINUS_ONE
);
288 void wxSizerItem::DeleteWindows()
297 m_sizer
->DeleteWindows();
300 bool wxSizerItem::IsWindow() const
302 return (m_window
!= NULL
);
305 bool wxSizerItem::IsSizer() const
307 return (m_sizer
!= NULL
);
310 bool wxSizerItem::IsSpacer() const
312 return (m_window
== NULL
) && (m_sizer
== NULL
);
315 void wxSizerItem::Show( bool show
)
320 m_window
->Show( show
);
322 m_sizer
->ShowItems( show
);
324 // ... nothing else to do to hide/show spacers
327 void wxSizerItem::SetOption( int option
)
329 SetProportion( option
);
332 int wxSizerItem::GetOption() const
334 return GetProportion();
338 //---------------------------------------------------------------------------
340 //---------------------------------------------------------------------------
343 : m_minSize( wxSize( 0, 0 ) )
349 WX_CLEAR_LIST(wxSizerItemList
, m_children
);
352 void wxSizer::Add( wxWindow
*window
, int proportion
, int flag
, int border
, wxObject
* userData
)
354 m_children
.Append( new wxSizerItem( window
, proportion
, flag
, border
, userData
) );
355 window
->SetContainingSizer( this );
358 void wxSizer::Add( wxSizer
*sizer
, int proportion
, int flag
, int border
, wxObject
* userData
)
360 m_children
.Append( new wxSizerItem( sizer
, proportion
, flag
, border
, userData
) );
363 void wxSizer::Add( int width
, int height
, int proportion
, int flag
, int border
, wxObject
* userData
)
365 m_children
.Append( new wxSizerItem( width
, height
, proportion
, flag
, border
, userData
) );
368 void wxSizer::Add( wxSizerItem
*item
)
370 m_children
.Append( item
);
372 if( item
->GetWindow() )
373 item
->GetWindow()->SetContainingSizer( this );
376 void wxSizer::Prepend( wxWindow
*window
, int proportion
, int flag
, int border
, wxObject
* userData
)
378 m_children
.Insert( new wxSizerItem( window
, proportion
, flag
, border
, userData
) );
379 window
->SetContainingSizer( this );
382 void wxSizer::Prepend( wxSizer
*sizer
, int proportion
, int flag
, int border
, wxObject
* userData
)
384 m_children
.Insert( new wxSizerItem( sizer
, proportion
, flag
, border
, userData
) );
387 void wxSizer::Prepend( int width
, int height
, int proportion
, int flag
, int border
, wxObject
* userData
)
389 m_children
.Insert( new wxSizerItem( width
, height
, proportion
, flag
, border
, userData
) );
392 void wxSizer::Prepend( wxSizerItem
*item
)
394 m_children
.Insert( item
);
396 if( item
->GetWindow() )
397 item
->GetWindow()->SetContainingSizer( this );
400 void wxSizer::Insert( size_t index
,
407 m_children
.Insert( index
,
408 new wxSizerItem( window
, proportion
, flag
, border
, userData
) );
409 window
->SetContainingSizer( this );
412 void wxSizer::Insert( size_t index
,
419 m_children
.Insert( index
,
420 new wxSizerItem( sizer
, proportion
, flag
, border
, userData
) );
423 void wxSizer::Insert( size_t index
,
431 m_children
.Insert( index
,
432 new wxSizerItem( width
, height
, proportion
, flag
, border
, userData
) );
435 void wxSizer::Insert( size_t index
, wxSizerItem
*item
)
437 m_children
.Insert( index
, item
);
439 if( item
->GetWindow() )
440 item
->GetWindow()->SetContainingSizer( this );
443 bool wxSizer::Remove( wxWindow
*window
)
445 return Detach( window
);
448 bool wxSizer::Remove( wxSizer
*sizer
)
450 wxASSERT_MSG( sizer
, _T("Removing NULL sizer") );
452 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
455 wxSizerItem
*item
= node
->GetData();
457 if (item
->GetSizer() == sizer
)
460 m_children
.Erase( node
);
464 node
= node
->GetNext();
470 bool wxSizer::Remove( int index
)
472 wxCHECK_MSG( index
>= 0 && (size_t)index
< m_children
.GetCount(),
474 _T("Remove index is out of range") );
476 wxSizerItemList::compatibility_iterator node
= m_children
.Item( index
);
478 wxCHECK_MSG( node
, false, _T("Failed to find child node") );
480 wxSizerItem
*item
= node
->GetData();
482 if( item
->IsWindow() )
483 item
->GetWindow()->SetContainingSizer( NULL
);
486 m_children
.Erase( node
);
490 bool wxSizer::Detach( wxSizer
*sizer
)
492 wxASSERT_MSG( sizer
, _T("Detaching NULL sizer") );
494 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
497 wxSizerItem
*item
= node
->GetData();
499 if (item
->GetSizer() == sizer
)
503 m_children
.Erase( node
);
506 node
= node
->GetNext();
512 bool wxSizer::Detach( wxWindow
*window
)
514 wxASSERT_MSG( window
, _T("Detaching NULL window") );
516 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
519 wxSizerItem
*item
= node
->GetData();
521 if (item
->GetWindow() == window
)
523 item
->GetWindow()->SetContainingSizer( NULL
);
525 m_children
.Erase( node
);
528 node
= node
->GetNext();
534 bool wxSizer::Detach( int index
)
536 wxCHECK_MSG( index
>= 0 && (size_t)index
< m_children
.GetCount(),
538 _T("Detach index is out of range") );
540 wxSizerItemList::compatibility_iterator node
= m_children
.Item( index
);
542 wxCHECK_MSG( node
, false, _T("Failed to find child node") );
544 wxSizerItem
*item
= node
->GetData();
546 if( item
->IsSizer() )
548 else if( item
->IsWindow() )
549 item
->GetWindow()->SetContainingSizer( NULL
);
552 m_children
.Erase( node
);
556 void wxSizer::Clear( bool delete_windows
)
558 // First clear the ContainingSizer pointers
559 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
562 wxSizerItem
*item
= node
->GetData();
564 if (item
->IsWindow())
565 item
->GetWindow()->SetContainingSizer( NULL
);
566 node
= node
->GetNext();
569 // Destroy the windows if needed
573 // Now empty the list
574 WX_CLEAR_LIST(wxSizerItemList
, m_children
);
577 void wxSizer::DeleteWindows()
579 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
582 wxSizerItem
*item
= node
->GetData();
584 item
->DeleteWindows();
585 node
= node
->GetNext();
589 wxSize
wxSizer::Fit( wxWindow
*window
)
591 wxSize
size(window
->IsTopLevel() ? FitSize(window
)
592 : GetMinWindowSize(window
));
594 window
->SetSize( size
);
599 void wxSizer::FitInside( wxWindow
*window
)
602 if (window
->IsTopLevel())
603 size
= VirtualFitSize( window
);
605 size
= GetMinClientSize( window
);
607 window
->SetVirtualSize( size
);
610 void wxSizer::Layout()
616 void wxSizer::SetSizeHints( wxWindow
*window
)
618 // Preserve the window's max size hints, but set the
619 // lower bound according to the sizer calculations.
621 wxSize size
= Fit( window
);
623 window
->SetSizeHints( size
.x
,
625 window
->GetMaxWidth(),
626 window
->GetMaxHeight() );
629 void wxSizer::SetVirtualSizeHints( wxWindow
*window
)
631 // Preserve the window's max size hints, but set the
632 // lower bound according to the sizer calculations.
635 wxSize
size( window
->GetVirtualSize() );
636 window
->SetVirtualSizeHints( size
.x
,
638 window
->GetMaxWidth(),
639 window
->GetMaxHeight() );
642 wxSize
wxSizer::GetMaxWindowSize( wxWindow
*window
) const
644 return window
->GetMaxSize();
647 wxSize
wxSizer::GetMinWindowSize( wxWindow
*window
)
649 wxSize
minSize( GetMinSize() );
650 wxSize
size( window
->GetSize() );
651 wxSize
client_size( window
->GetClientSize() );
653 return wxSize( minSize
.x
+size
.x
-client_size
.x
,
654 minSize
.y
+size
.y
-client_size
.y
);
657 // TODO on mac we need a function that determines how much free space this
658 // min size contains, in order to make sure that we have 20 pixels of free
659 // space around the controls
661 // Return a window size that will fit within the screens dimensions
662 wxSize
wxSizer::FitSize( wxWindow
*window
)
664 wxSize size
= GetMinWindowSize( window
);
665 wxSize sizeMax
= GetMaxWindowSize( window
);
667 // Limit the size if sizeMax != wxDefaultSize
669 if ( size
.x
> sizeMax
.x
&& sizeMax
.x
!= -1 )
671 if ( size
.y
> sizeMax
.y
&& sizeMax
.y
!= -1 )
677 wxSize
wxSizer::GetMaxClientSize( wxWindow
*window
) const
679 wxSize
maxSize( window
->GetMaxSize() );
681 if( maxSize
!= wxDefaultSize
)
683 wxSize
size( window
->GetSize() );
684 wxSize
client_size( window
->GetClientSize() );
686 return wxSize( maxSize
.x
+ client_size
.x
- size
.x
,
687 maxSize
.y
+ client_size
.y
- size
.y
);
690 return wxDefaultSize
;
693 wxSize
wxSizer::GetMinClientSize( wxWindow
*WXUNUSED(window
) )
695 return GetMinSize(); // Already returns client size.
698 wxSize
wxSizer::VirtualFitSize( wxWindow
*window
)
700 wxSize size
= GetMinClientSize( window
);
701 wxSize sizeMax
= GetMaxClientSize( window
);
703 // Limit the size if sizeMax != wxDefaultSize
705 if ( size
.x
> sizeMax
.x
&& sizeMax
.x
!= -1 )
707 if ( size
.y
> sizeMax
.y
&& sizeMax
.y
!= -1 )
713 void wxSizer::SetDimension( int x
, int y
, int width
, int height
)
722 wxSize
wxSizer::GetMinSize()
724 wxSize
ret( CalcMin() );
725 if (ret
.x
< m_minSize
.x
) ret
.x
= m_minSize
.x
;
726 if (ret
.y
< m_minSize
.y
) ret
.y
= m_minSize
.y
;
730 void wxSizer::DoSetMinSize( int width
, int height
)
733 m_minSize
.y
= height
;
736 bool wxSizer::DoSetItemMinSize( wxWindow
*window
, int width
, int height
)
738 wxASSERT_MSG( window
, _T("SetMinSize for NULL window") );
740 // Is it our immediate child?
742 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
745 wxSizerItem
*item
= node
->GetData();
747 if (item
->GetWindow() == window
)
749 item
->SetMinSize( width
, height
);
752 node
= node
->GetNext();
755 // No? Search any subsizers we own then
757 node
= m_children
.GetFirst();
760 wxSizerItem
*item
= node
->GetData();
762 if ( item
->GetSizer() &&
763 item
->GetSizer()->DoSetItemMinSize( window
, width
, height
) )
765 // A child sizer found the requested windw, exit.
768 node
= node
->GetNext();
774 bool wxSizer::DoSetItemMinSize( wxSizer
*sizer
, int width
, int height
)
776 wxASSERT_MSG( sizer
, _T("SetMinSize for NULL sizer") );
778 // Is it our immediate child?
780 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
783 wxSizerItem
*item
= node
->GetData();
785 if (item
->GetSizer() == sizer
)
787 item
->GetSizer()->DoSetMinSize( width
, height
);
790 node
= node
->GetNext();
793 // No? Search any subsizers we own then
795 node
= m_children
.GetFirst();
798 wxSizerItem
*item
= node
->GetData();
800 if ( item
->GetSizer() &&
801 item
->GetSizer()->DoSetItemMinSize( sizer
, width
, height
) )
803 // A child found the requested sizer, exit.
806 node
= node
->GetNext();
812 bool wxSizer::DoSetItemMinSize( size_t index
, int width
, int height
)
814 wxSizerItemList::compatibility_iterator node
= m_children
.Item( index
);
816 wxCHECK_MSG( node
, false, _T("Failed to find child node") );
818 wxSizerItem
*item
= node
->GetData();
820 if (item
->GetSizer())
822 // Sizers contains the minimal size in them, if not calculated ...
823 item
->GetSizer()->DoSetMinSize( width
, height
);
827 // ... but the minimal size of spacers and windows in stored in them
828 item
->SetMinSize( width
, height
);
834 void wxSizer::Show( wxWindow
*window
, bool show
)
836 wxASSERT_MSG( window
, _T("Show for NULL window") );
838 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
841 wxSizerItem
*item
= node
->GetData();
843 if (item
->GetWindow() == window
)
848 node
= node
->GetNext();
852 void wxSizer::Show( wxSizer
*sizer
, bool show
)
854 wxASSERT_MSG( sizer
, _T("Show for NULL sizer") );
856 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
859 wxSizerItem
*item
= node
->GetData();
861 if (item
->GetSizer() == sizer
)
866 node
= node
->GetNext();
870 void wxSizer::Show( size_t index
, bool show
)
872 wxCHECK_RET( index
< m_children
.GetCount(),
873 _T("Show index is out of range") );
875 m_children
.Item( index
)->GetData()->Show( show
);
878 void wxSizer::ShowItems( bool show
)
880 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
883 node
->GetData()->Show( show
);
884 node
= node
->GetNext();
888 bool wxSizer::IsShown( wxWindow
*window
) const
890 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
893 wxSizerItem
*item
= node
->GetData();
895 if (item
->GetWindow() == window
)
897 return item
->IsShown();
899 node
= node
->GetNext();
902 wxFAIL_MSG( _T("IsShown failed to find sizer item") );
907 bool wxSizer::IsShown( wxSizer
*sizer
) const
909 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
912 wxSizerItem
*item
= node
->GetData();
914 if (item
->GetSizer() == sizer
)
916 return item
->IsShown();
918 node
= node
->GetNext();
921 wxFAIL_MSG( _T("IsShown failed to find sizer item") );
926 bool wxSizer::IsShown( size_t index
) const
928 wxCHECK_MSG( index
< m_children
.GetCount(),
930 _T("IsShown index is out of range") );
932 return m_children
.Item( index
)->GetData()->IsShown();
936 //---------------------------------------------------------------------------
938 //---------------------------------------------------------------------------
940 wxGridSizer::wxGridSizer( int rows
, int cols
, int vgap
, int hgap
)
946 if (m_rows
== 0 && m_cols
== 0)
950 wxGridSizer::wxGridSizer( int cols
, int vgap
, int hgap
)
956 if (m_rows
== 0 && m_cols
== 0)
960 int wxGridSizer::CalcRowsCols(int& nrows
, int& ncols
) const
962 int nitems
= m_children
.GetCount();
968 nrows
= (nitems
+ m_cols
- 1) / m_cols
;
972 ncols
= (nitems
+ m_rows
- 1) / m_rows
;
975 else // 0 columns, 0 rows?
977 wxFAIL_MSG( _T("grid sizer must have either rows or columns fixed") );
986 void wxGridSizer::RecalcSizes()
988 int nitems
, nrows
, ncols
;
989 if ( (nitems
= CalcRowsCols(nrows
, ncols
)) == 0 )
992 wxSize
sz( GetSize() );
993 wxPoint
pt( GetPosition() );
995 int w
= (sz
.x
- (ncols
- 1) * m_hgap
) / ncols
;
996 int h
= (sz
.y
- (nrows
- 1) * m_vgap
) / nrows
;
999 for (int c
= 0; c
< ncols
; c
++)
1002 for (int r
= 0; r
< nrows
; r
++)
1004 int i
= r
* ncols
+ c
;
1007 wxSizerItemList::compatibility_iterator node
= m_children
.Item( i
);
1009 wxASSERT_MSG( node
, _T("Failed to find SizerItemList node") );
1011 SetItemBounds( node
->GetData(), x
, y
, w
, h
);
1019 wxSize
wxGridSizer::CalcMin()
1022 if ( CalcRowsCols(nrows
, ncols
) == 0 )
1023 return wxSize(10, 10);
1025 // Find the max width and height for any component
1029 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
1032 wxSizerItem
*item
= node
->GetData();
1033 wxSize
sz( item
->CalcMin() );
1035 w
= wxMax( w
, sz
.x
);
1036 h
= wxMax( h
, sz
.y
);
1038 node
= node
->GetNext();
1041 return wxSize( ncols
* w
+ (ncols
-1) * m_hgap
,
1042 nrows
* h
+ (nrows
-1) * m_vgap
);
1045 void wxGridSizer::SetItemBounds( wxSizerItem
*item
, int x
, int y
, int w
, int h
)
1048 wxSize
sz( item
->CalcMin() );
1049 int flag
= item
->GetFlag();
1051 if ((flag
& wxEXPAND
) || (flag
& wxSHAPED
))
1057 if (flag
& wxALIGN_CENTER_HORIZONTAL
)
1059 pt
.x
= x
+ (w
- sz
.x
- m_hgap
) / 2;
1061 else if (flag
& wxALIGN_RIGHT
)
1063 pt
.x
= x
+ (w
- sz
.x
- m_hgap
);
1066 if (flag
& wxALIGN_CENTER_VERTICAL
)
1068 pt
.y
= y
+ (h
- sz
.y
- m_vgap
) / 2;
1070 else if (flag
& wxALIGN_BOTTOM
)
1072 pt
.y
= y
+ (h
- sz
.y
- m_vgap
);
1076 item
->SetDimension(pt
, sz
);
1079 //---------------------------------------------------------------------------
1081 //---------------------------------------------------------------------------
1083 wxFlexGridSizer::wxFlexGridSizer( int rows
, int cols
, int vgap
, int hgap
)
1084 : wxGridSizer( rows
, cols
, vgap
, hgap
),
1085 m_flexDirection(wxBOTH
),
1086 m_growMode(wxFLEX_GROWMODE_SPECIFIED
)
1090 wxFlexGridSizer::wxFlexGridSizer( int cols
, int vgap
, int hgap
)
1091 : wxGridSizer( cols
, vgap
, hgap
),
1092 m_flexDirection(wxBOTH
),
1093 m_growMode(wxFLEX_GROWMODE_SPECIFIED
)
1097 wxFlexGridSizer::~wxFlexGridSizer()
1101 void wxFlexGridSizer::RecalcSizes()
1103 int nitems
, nrows
, ncols
;
1104 if ( (nitems
= CalcRowsCols(nrows
, ncols
)) == 0 )
1107 wxPoint
pt( GetPosition() );
1108 wxSize
sz( GetSize() );
1109 wxSize
minsz( CalcMin() );
1111 AdjustForGrowables(sz
, minsz
, nrows
, ncols
);
1113 sz
= wxSize( pt
.x
+ sz
.x
, pt
.y
+ sz
.y
);
1116 for (int c
= 0; c
< ncols
; c
++)
1119 for (int r
= 0; r
< nrows
; r
++)
1121 int i
= r
* ncols
+ c
;
1124 wxSizerItemList::compatibility_iterator node
= m_children
.Item( i
);
1126 wxASSERT_MSG( node
, _T("Failed to find node") );
1128 int w
= wxMax( 0, wxMin( m_colWidths
[c
], sz
.x
- x
) );
1129 int h
= wxMax( 0, wxMin( m_rowHeights
[r
], sz
.y
- y
) );
1131 SetItemBounds( node
->GetData(), x
, y
, w
, h
);
1133 y
= y
+ m_rowHeights
[r
] + m_vgap
;
1135 x
= x
+ m_colWidths
[c
] + m_hgap
;
1139 wxSize
wxFlexGridSizer::CalcMin()
1145 // Number of rows/columns can change as items are added or removed.
1146 if ( !CalcRowsCols(nrows
, ncols
) )
1147 return wxSize(10, 10);
1149 m_rowHeights
.SetCount(nrows
);
1150 m_colWidths
.SetCount(ncols
);
1152 // We have to recalcuate the sizes in case the item minimum size has
1153 // changed since the previous layout, or the item has been hidden using
1154 // wxSizer::Show(). If all the items in a row/column are hidden, the final
1155 // dimension of the row/column will be -1, indicating that the column
1156 // itself is hidden.
1157 for( s
= m_rowHeights
.GetCount(), i
= 0; i
< s
; ++i
)
1158 m_rowHeights
[ i
] = -1;
1159 for( s
= m_colWidths
.GetCount(), i
= 0; i
< s
; ++i
)
1160 m_colWidths
[ i
] = -1;
1162 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
1167 wxSizerItem
*item
= node
->GetData();
1168 if ( item
->IsShown() )
1170 wxSize
sz( item
->CalcMin() );
1171 int row
= i
/ ncols
;
1172 int col
= i
% ncols
;
1174 m_rowHeights
[ row
] = wxMax( wxMax( 0, sz
.y
), m_rowHeights
[ row
] );
1175 m_colWidths
[ col
] = wxMax( wxMax( 0, sz
.x
), m_colWidths
[ col
] );
1178 node
= node
->GetNext();
1182 AdjustForFlexDirection();
1184 // Sum total minimum size, including gaps between rows/columns.
1185 // -1 is used as a magic number meaning empty column.
1187 for (int col
= 0; col
< ncols
; col
++)
1188 if ( m_colWidths
[ col
] != -1 )
1189 width
+= m_colWidths
[ col
] + ( col
== ncols
-1 ? 0 : m_hgap
);
1192 for (int row
= 0; row
< nrows
; row
++)
1193 if ( m_rowHeights
[ row
] != -1 )
1194 height
+= m_rowHeights
[ row
] + ( row
== nrows
-1 ? 0 : m_vgap
);
1196 return wxSize( width
, height
);
1199 void wxFlexGridSizer::AdjustForFlexDirection()
1201 // the logic in CalcMin works when we resize flexibly in both directions
1202 // but maybe this is not the case
1203 if ( m_flexDirection
!= wxBOTH
)
1205 // select the array corresponding to the direction in which we do *not*
1207 wxArrayInt
& array
= m_flexDirection
== wxVERTICAL
? m_colWidths
1210 const int count
= array
.GetCount();
1212 // find the largest value in this array
1214 for ( n
= 0; n
< count
; ++n
)
1216 if ( array
[n
] > largest
)
1220 // and now fill it with the largest value
1221 for ( n
= 0; n
< count
; ++n
)
1229 void wxFlexGridSizer::AdjustForGrowables(const wxSize
& sz
, const wxSize
& minsz
,
1230 int nrows
, int ncols
)
1232 // what to do with the rows? by default, resize them proportionally
1233 if ( sz
.y
> minsz
.y
&& ( (m_flexDirection
& wxVERTICAL
) || (m_growMode
== wxFLEX_GROWMODE_SPECIFIED
) ) )
1235 int sum_proportions
= 0;
1236 int growable_space
= 0;
1239 for (idx
= 0; idx
< m_growableRows
.GetCount(); idx
++)
1241 // Since the number of rows/columns can change as items are
1242 // inserted/deleted, we need to verify at runtime that the
1243 // requested growable rows/columns are still valid.
1244 if (m_growableRows
[idx
] >= nrows
)
1247 // If all items in a row/column are hidden, that row/column will
1248 // have a dimension of -1. This causes the row/column to be
1249 // hidden completely.
1250 if (m_rowHeights
[ m_growableRows
[idx
] ] == -1)
1252 sum_proportions
+= m_growableRowsProportions
[idx
];
1253 growable_space
+= m_rowHeights
[ m_growableRows
[idx
] ];
1259 for (idx
= 0; idx
< m_growableRows
.GetCount(); idx
++)
1261 if (m_growableRows
[idx
] >= nrows
)
1263 if (m_rowHeights
[ m_growableRows
[idx
] ] == -1)
1264 m_rowHeights
[ m_growableRows
[idx
] ] = 0;
1267 int delta
= (sz
.y
- minsz
.y
);
1268 if (sum_proportions
== 0)
1269 delta
= (delta
/num
) + m_rowHeights
[ m_growableRows
[idx
] ];
1271 delta
= ((delta
+growable_space
)*m_growableRowsProportions
[idx
]) / sum_proportions
;
1272 m_rowHeights
[ m_growableRows
[idx
] ] = delta
;
1277 else if ( (m_growMode
== wxFLEX_GROWMODE_ALL
) && (sz
.y
> minsz
.y
) )
1279 // rounding problem?
1280 for ( int row
= 0; row
< nrows
; ++row
)
1281 m_rowHeights
[ row
] = sz
.y
/ nrows
;
1284 // the same logic as above but for the columns
1285 if ( sz
.x
> minsz
.x
&& ( (m_flexDirection
& wxHORIZONTAL
) || (m_growMode
== wxFLEX_GROWMODE_SPECIFIED
) ) )
1287 int sum_proportions
= 0;
1288 int growable_space
= 0;
1291 for (idx
= 0; idx
< m_growableCols
.GetCount(); idx
++)
1293 // Since the number of rows/columns can change as items are
1294 // inserted/deleted, we need to verify at runtime that the
1295 // requested growable rows/columns are still valid.
1296 if (m_growableCols
[idx
] >= ncols
)
1299 // If all items in a row/column are hidden, that row/column will
1300 // have a dimension of -1. This causes the column to be hidden
1302 if (m_colWidths
[ m_growableCols
[idx
] ] == -1)
1304 sum_proportions
+= m_growableColsProportions
[idx
];
1305 growable_space
+= m_colWidths
[ m_growableCols
[idx
] ];
1311 for (idx
= 0; idx
< m_growableCols
.GetCount(); idx
++)
1313 if (m_growableCols
[idx
] >= ncols
)
1315 if (m_colWidths
[ m_growableCols
[idx
] ] == -1)
1316 m_colWidths
[ m_growableCols
[idx
] ] = 0;
1319 int delta
= (sz
.x
- minsz
.x
);
1320 if (sum_proportions
== 0)
1321 delta
= (delta
/num
) + m_colWidths
[ m_growableCols
[idx
] ];
1323 delta
= ((delta
+growable_space
)*m_growableColsProportions
[idx
])/sum_proportions
;
1324 m_colWidths
[ m_growableCols
[idx
] ] = delta
;
1329 else if ( (m_growMode
== wxFLEX_GROWMODE_ALL
) && (sz
.x
> minsz
.x
) )
1331 for ( int col
=0; col
< ncols
; ++col
)
1332 m_colWidths
[ col
] = sz
.x
/ ncols
;
1337 void wxFlexGridSizer::AddGrowableRow( size_t idx
, int proportion
)
1339 m_growableRows
.Add( idx
);
1340 m_growableRowsProportions
.Add( proportion
);
1343 void wxFlexGridSizer::RemoveGrowableRow( size_t idx
)
1345 m_growableRows
.Remove( idx
);
1348 void wxFlexGridSizer::AddGrowableCol( size_t idx
, int proportion
)
1350 m_growableCols
.Add( idx
);
1351 m_growableColsProportions
.Add( proportion
);
1354 void wxFlexGridSizer::RemoveGrowableCol( size_t idx
)
1356 m_growableCols
.Remove( idx
);
1359 //---------------------------------------------------------------------------
1361 //---------------------------------------------------------------------------
1363 wxBoxSizer::wxBoxSizer( int orient
)
1364 : m_orient( orient
)
1368 void wxBoxSizer::RecalcSizes()
1370 if (m_children
.GetCount() == 0)
1376 if (m_orient
== wxHORIZONTAL
)
1377 delta
= m_size
.x
- m_fixedWidth
;
1379 delta
= m_size
.y
- m_fixedHeight
;
1382 wxPoint
pt( m_position
);
1384 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
1387 wxSizerItem
*item
= node
->GetData();
1389 if (item
->IsShown())
1391 wxSize
size( item
->CalcMin() );
1393 if (m_orient
== wxVERTICAL
)
1395 wxCoord height
= size
.y
;
1396 if (item
->GetProportion())
1398 // Because of at least one visible item has non-zero
1399 // proportion then m_stretchable is not zero
1400 height
= (delta
* item
->GetProportion()) / m_stretchable
;
1403 wxPoint
child_pos( pt
);
1404 wxSize
child_size( wxSize( size
.x
, height
) );
1406 if (item
->GetFlag() & (wxEXPAND
| wxSHAPED
))
1407 child_size
.x
= m_size
.x
;
1408 else if (item
->GetFlag() & wxALIGN_RIGHT
)
1409 child_pos
.x
+= m_size
.x
- size
.x
;
1410 else if (item
->GetFlag() & (wxCENTER
| wxALIGN_CENTER_HORIZONTAL
))
1411 // XXX wxCENTER is added for backward compatibility;
1412 // wxALIGN_CENTER should be used in new code
1413 child_pos
.x
+= (m_size
.x
- size
.x
) / 2;
1415 item
->SetDimension( child_pos
, child_size
);
1421 wxCoord width
= size
.x
;
1422 if (item
->GetProportion())
1424 // Because of at least one visible item has non-zero
1425 // proportion then m_stretchable is not zero
1426 width
= (delta
* item
->GetProportion()) / m_stretchable
;
1429 wxPoint
child_pos( pt
);
1430 wxSize
child_size( wxSize(width
, size
.y
) );
1432 if (item
->GetFlag() & (wxEXPAND
| wxSHAPED
))
1433 child_size
.y
= m_size
.y
;
1434 else if (item
->GetFlag() & wxALIGN_BOTTOM
)
1435 child_pos
.y
+= m_size
.y
- size
.y
;
1436 else if (item
->GetFlag() & (wxCENTER
| wxALIGN_CENTER_VERTICAL
))
1437 // XXX wxCENTER is added for backward compatibility;
1438 // wxALIGN_CENTER should be used in new code
1439 child_pos
.y
+= (m_size
.y
- size
.y
) / 2;
1441 item
->SetDimension( child_pos
, child_size
);
1447 node
= node
->GetNext();
1451 wxSize
wxBoxSizer::CalcMin()
1453 if (m_children
.GetCount() == 0)
1454 return wxSize(10,10);
1462 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
1465 wxSizerItem
*item
= node
->GetData();
1467 if (item
->IsShown() && item
->GetProportion() != 0)
1468 m_stretchable
+= item
->GetProportion();
1470 node
= node
->GetNext();
1473 // Total minimum size (width or height) of sizer
1476 node
= m_children
.GetFirst();
1479 wxSizerItem
*item
= node
->GetData();
1481 if (item
->IsShown() && item
->GetProportion() != 0)
1483 int stretch
= item
->GetProportion();
1484 wxSize
size( item
->CalcMin() );
1487 // Integer division rounded up is (a + b - 1) / b
1488 // Round up needed in order to guarantee that all
1489 // all items will have size not less then their min size
1490 if (m_orient
== wxHORIZONTAL
)
1491 minSize
= ( size
.x
*m_stretchable
+ stretch
- 1)/stretch
;
1493 minSize
= ( size
.y
*m_stretchable
+ stretch
- 1)/stretch
;
1495 if (minSize
> maxMinSize
)
1496 maxMinSize
= minSize
;
1498 node
= node
->GetNext();
1501 // Calculate overall minimum size
1502 node
= m_children
.GetFirst();
1505 wxSizerItem
*item
= node
->GetData();
1507 if (item
->IsShown())
1509 wxSize
size( item
->CalcMin() );
1510 if (item
->GetProportion() != 0)
1512 if (m_orient
== wxHORIZONTAL
)
1513 size
.x
= (maxMinSize
*item
->GetProportion())/m_stretchable
;
1515 size
.y
= (maxMinSize
*item
->GetProportion())/m_stretchable
;
1519 if (m_orient
== wxVERTICAL
)
1521 m_fixedHeight
+= size
.y
;
1522 m_fixedWidth
= wxMax( m_fixedWidth
, size
.x
);
1526 m_fixedWidth
+= size
.x
;
1527 m_fixedHeight
= wxMax( m_fixedHeight
, size
.y
);
1531 if (m_orient
== wxHORIZONTAL
)
1533 m_minWidth
+= size
.x
;
1534 m_minHeight
= wxMax( m_minHeight
, size
.y
);
1538 m_minHeight
+= size
.y
;
1539 m_minWidth
= wxMax( m_minWidth
, size
.x
);
1542 node
= node
->GetNext();
1545 return wxSize( m_minWidth
, m_minHeight
);
1548 //---------------------------------------------------------------------------
1550 //---------------------------------------------------------------------------
1554 wxStaticBoxSizer::wxStaticBoxSizer( wxStaticBox
*box
, int orient
)
1555 : wxBoxSizer( orient
)
1556 , m_staticBox( box
)
1558 wxASSERT_MSG( box
, wxT("wxStaticBoxSizer needs a static box") );
1561 static void GetStaticBoxBorders( wxStaticBox
*box
,
1565 // this has to be done platform by platform as there is no way to
1566 // guess the thickness of a wxStaticBox border
1568 box
->GetBordersForSizer(borderTop
,borderOther
);
1569 #elif defined(__WXMAC__)
1571 static int extraTop
= -1; // Uninitted
1572 static int other
= 5;
1574 if ( extraTop
== -1 )
1576 // The minimal border used for the top. Later on the staticbox'
1577 // font height is added to this.
1580 if ( UMAGetSystemVersion() >= 0x1030 /*Panther*/ )
1582 // As indicated by the HIG, Panther needs an extra border of 11
1583 // pixels (otherwise overlapping occurs at the top). The "other"
1584 // border has to be 11.
1591 *borderTop
= extraTop
+ box
->GetCharHeight();
1592 *borderOther
= other
;
1596 if ( box
->GetLabel().IsEmpty() )
1600 *borderTop
= box
->GetCharHeight();
1603 #endif // __WXCOCOA__
1606 void wxStaticBoxSizer::RecalcSizes()
1608 int top_border
, other_border
;
1609 GetStaticBoxBorders(m_staticBox
, &top_border
, &other_border
);
1611 m_staticBox
->SetSize( m_position
.x
, m_position
.y
, m_size
.x
, m_size
.y
);
1613 wxPoint
old_pos( m_position
);
1614 m_position
.x
+= other_border
;
1615 m_position
.y
+= top_border
;
1616 wxSize
old_size( m_size
);
1617 m_size
.x
-= 2*other_border
;
1618 m_size
.y
-= top_border
+ other_border
;
1620 wxBoxSizer::RecalcSizes();
1622 m_position
= old_pos
;
1626 wxSize
wxStaticBoxSizer::CalcMin()
1628 int top_border
, other_border
;
1629 GetStaticBoxBorders(m_staticBox
, &top_border
, &other_border
);
1631 wxSize
ret( wxBoxSizer::CalcMin() );
1632 ret
.x
+= 2*other_border
;
1633 ret
.y
+= other_border
+ top_border
;
1638 void wxStaticBoxSizer::ShowItems( bool show
)
1640 m_staticBox
->Show( show
);
1641 wxBoxSizer::ShowItems( show
);
1644 #endif // wxUSE_STATBOX
1646 // ----------------------------------------------------------------------------
1648 // ----------------------------------------------------------------------------
1652 wxBookCtrlSizer::wxBookCtrlSizer(wxBookCtrl
*bookctrl
)
1653 : m_bookctrl(bookctrl
)
1655 wxASSERT_MSG( bookctrl
, wxT("wxBookCtrlSizer needs a control") );
1658 void wxBookCtrlSizer::RecalcSizes()
1660 m_bookctrl
->SetSize( m_position
.x
, m_position
.y
, m_size
.x
, m_size
.y
);
1663 wxSize
wxBookCtrlSizer::CalcMin()
1665 wxSize sizeBorder
= m_bookctrl
->CalcSizeFromPage(wxSize(0, 0));
1670 if ( m_bookctrl
->GetPageCount() == 0 )
1672 return wxSize(sizeBorder
.x
+ 10, sizeBorder
.y
+ 10);
1678 wxWindowList::compatibility_iterator
1679 node
= m_bookctrl
->GetChildren().GetFirst();
1682 wxWindow
*item
= node
->GetData();
1683 wxSizer
*itemsizer
= item
->GetSizer();
1687 wxSize
subsize( itemsizer
->CalcMin() );
1689 if (subsize
.x
> maxX
)
1691 if (subsize
.y
> maxY
)
1695 node
= node
->GetNext();
1698 return wxSize( maxX
, maxY
) + sizeBorder
;
1704 wxNotebookSizer::wxNotebookSizer(wxNotebook
*nb
)
1705 : wxBookCtrlSizer(nb
)
1709 #endif // wxUSE_NOTEBOOOK
1710 #endif // wxUSE_BOOKCTRL