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 /////////////////////////////////////////////////////////////////////////////
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>
30 //---------------------------------------------------------------------------
32 IMPLEMENT_CLASS(wxSizerItem
, wxObject
)
33 IMPLEMENT_CLASS(wxSizer
, wxObject
)
34 IMPLEMENT_CLASS(wxGridSizer
, wxSizer
)
35 IMPLEMENT_CLASS(wxFlexGridSizer
, wxGridSizer
)
36 IMPLEMENT_CLASS(wxBoxSizer
, wxSizer
)
38 IMPLEMENT_CLASS(wxStaticBoxSizer
, wxBoxSizer
)
41 IMPLEMENT_CLASS(wxNotebookSizer
, wxSizer
)
44 WX_DEFINE_EXPORTED_LIST( wxSizerItemList
);
47 //---------------------------------------------------------------------------
49 //---------------------------------------------------------------------------
51 wxSizerItem::wxSizerItem( int width
, int height
, int proportion
, int flag
, int border
, wxObject
* userData
)
54 , m_size( wxSize( width
, height
) ) // size is set directly
55 , m_minSize( m_size
) // minimal size is the initial size
56 , m_proportion( proportion
)
60 , m_userData( userData
)
65 wxSizerItem::wxSizerItem( wxWindow
*window
, int proportion
, int flag
, int border
, wxObject
* userData
)
68 , m_minSize( window
->GetSize() ) // minimal size is the initial size
69 , m_proportion( proportion
)
73 , m_userData( userData
)
75 // aspect ratio calculated from initial size
76 SetRatio( m_minSize
);
78 // m_size is calculated later
81 wxSizerItem::wxSizerItem( wxSizer
*sizer
, int proportion
, int flag
, int border
, wxObject
* userData
)
84 , m_proportion( proportion
)
89 , m_userData( userData
)
91 // m_minSize is calculated later
92 // m_size is calculated later
95 wxSizerItem::~wxSizerItem()
104 wxSize
wxSizerItem::GetSize() const
108 ret
= m_sizer
->GetSize();
111 ret
= m_window
->GetSize();
118 if (m_flag
& wxNORTH
)
120 if (m_flag
& wxSOUTH
)
126 wxSize
wxSizerItem::CalcMin()
131 ret
= m_sizer
->GetMinSize();
133 // if we have to preserve aspect ratio _AND_ this is
134 // the first-time calculation, consider ret to be initial size
135 if ((m_flag
& wxSHAPED
) && !m_ratio
)
140 if ( IsWindow() && (m_flag
& wxADJUST_MINSIZE
) )
142 // By user request, keep the minimal size for this item
143 // in sync with the largest of BestSize and any user supplied
144 // minimum size hint. Useful in cases where the item is
145 // changeable -- static text labels, etc.
146 m_minSize
= m_window
->GetAdjustedBestSize();
156 if (m_flag
& wxNORTH
)
158 if (m_flag
& wxSOUTH
)
164 void wxSizerItem::SetDimension( wxPoint pos
, wxSize size
)
166 if (m_flag
& wxSHAPED
)
168 // adjust aspect ratio
169 int rwidth
= (int) (size
.y
* m_ratio
);
173 int rheight
= (int) (size
.x
/ m_ratio
);
174 // add vertical space
175 if (m_flag
& wxALIGN_CENTER_VERTICAL
)
176 pos
.y
+= (size
.y
- rheight
) / 2;
177 else if (m_flag
& wxALIGN_BOTTOM
)
178 pos
.y
+= (size
.y
- rheight
);
179 // use reduced dimensions
182 else if (rwidth
< size
.x
)
184 // add horizontal space
185 if (m_flag
& wxALIGN_CENTER_HORIZONTAL
)
186 pos
.x
+= (size
.x
- rwidth
) / 2;
187 else if (m_flag
& wxALIGN_RIGHT
)
188 pos
.x
+= (size
.x
- rwidth
);
193 // This is what GetPosition() returns. Since we calculate
194 // borders afterwards, GetPosition() will be the left/top
195 // corner of the surrounding border.
207 if (m_flag
& wxNORTH
)
212 if (m_flag
& wxSOUTH
)
218 m_sizer
->SetDimension( pos
.x
, pos
.y
, size
.x
, size
.y
);
221 m_window
->SetSize( pos
.x
, pos
.y
, size
.x
, size
.y
, wxSIZE_ALLOW_MINUS_ONE
);
226 void wxSizerItem::DeleteWindows()
232 m_sizer
->DeleteWindows();
235 bool wxSizerItem::IsWindow() const
237 return (m_window
!= NULL
);
240 bool wxSizerItem::IsSizer() const
242 return (m_sizer
!= NULL
);
245 bool wxSizerItem::IsSpacer() const
247 return (m_window
== NULL
) && (m_sizer
== NULL
);
250 void wxSizerItem::Show( bool show
)
255 m_window
->Show( show
);
257 m_sizer
->ShowItems( show
);
259 // ... nothing else to do to hide/show spacers
262 void wxSizerItem::SetOption( int option
)
264 SetProportion( option
);
267 int wxSizerItem::GetOption() const
269 return GetProportion();
273 //---------------------------------------------------------------------------
275 //---------------------------------------------------------------------------
278 : m_minSize( wxSize( 0, 0 ) )
284 WX_CLEAR_LIST(wxSizerItemList
, m_children
);
287 void wxSizer::Add( wxWindow
*window
, int proportion
, int flag
, int border
, wxObject
* userData
)
289 m_children
.Append( new wxSizerItem( window
, proportion
, flag
, border
, userData
) );
290 window
->SetContainingSizer( this );
293 void wxSizer::Add( wxSizer
*sizer
, int proportion
, int flag
, int border
, wxObject
* userData
)
295 m_children
.Append( new wxSizerItem( sizer
, proportion
, flag
, border
, userData
) );
298 void wxSizer::Add( int width
, int height
, int proportion
, int flag
, int border
, wxObject
* userData
)
300 m_children
.Append( new wxSizerItem( width
, height
, proportion
, flag
, border
, userData
) );
303 void wxSizer::Add( wxSizerItem
*item
)
305 m_children
.Append( item
);
307 if( item
->GetWindow() )
308 item
->GetWindow()->SetContainingSizer( this );
311 void wxSizer::Prepend( wxWindow
*window
, int proportion
, int flag
, int border
, wxObject
* userData
)
313 m_children
.Insert( new wxSizerItem( window
, proportion
, flag
, border
, userData
) );
314 window
->SetContainingSizer( this );
317 void wxSizer::Prepend( wxSizer
*sizer
, int proportion
, int flag
, int border
, wxObject
* userData
)
319 m_children
.Insert( new wxSizerItem( sizer
, proportion
, flag
, border
, userData
) );
322 void wxSizer::Prepend( int width
, int height
, int proportion
, int flag
, int border
, wxObject
* userData
)
324 m_children
.Insert( new wxSizerItem( width
, height
, proportion
, flag
, border
, userData
) );
327 void wxSizer::Prepend( wxSizerItem
*item
)
329 m_children
.Insert( item
);
331 if( item
->GetWindow() )
332 item
->GetWindow()->SetContainingSizer( this );
335 void wxSizer::Insert( size_t index
,
342 m_children
.Insert( index
,
343 new wxSizerItem( window
, proportion
, flag
, border
, userData
) );
344 window
->SetContainingSizer( this );
347 void wxSizer::Insert( size_t index
,
354 m_children
.Insert( index
,
355 new wxSizerItem( sizer
, proportion
, flag
, border
, userData
) );
358 void wxSizer::Insert( size_t index
,
366 m_children
.Insert( index
,
367 new wxSizerItem( width
, height
, proportion
, flag
, border
, userData
) );
370 void wxSizer::Insert( size_t index
, wxSizerItem
*item
)
372 m_children
.Insert( index
, item
);
374 if( item
->GetWindow() )
375 item
->GetWindow()->SetContainingSizer( this );
378 bool wxSizer::Remove( wxWindow
*window
)
380 return Detach( window
);
383 bool wxSizer::Remove( wxSizer
*sizer
)
385 wxASSERT_MSG( sizer
, _T("Removing NULL sizer") );
387 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
390 wxSizerItem
*item
= node
->GetData();
392 if (item
->GetSizer() == sizer
)
395 m_children
.Erase( node
);
399 node
= node
->GetNext();
405 bool wxSizer::Remove( int index
)
407 wxCHECK_MSG( index
>= 0 && (size_t)index
< m_children
.GetCount(),
409 _T("Remove index is out of range") );
411 wxSizerItemList::compatibility_iterator node
= m_children
.Item( index
);
413 wxCHECK_MSG( node
, false, _T("Failed to find child node") );
415 wxSizerItem
*item
= node
->GetData();
417 if( item
->IsWindow() )
418 item
->GetWindow()->SetContainingSizer( NULL
);
421 m_children
.Erase( node
);
425 bool wxSizer::Detach( wxSizer
*sizer
)
427 wxASSERT_MSG( sizer
, _T("Detaching NULL sizer") );
429 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
432 wxSizerItem
*item
= node
->GetData();
434 if (item
->GetSizer() == sizer
)
438 m_children
.Erase( node
);
441 node
= node
->GetNext();
447 bool wxSizer::Detach( wxWindow
*window
)
449 wxASSERT_MSG( window
, _T("Detaching NULL window") );
451 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
454 wxSizerItem
*item
= node
->GetData();
456 if (item
->GetWindow() == window
)
458 item
->GetWindow()->SetContainingSizer( NULL
);
460 m_children
.Erase( node
);
463 node
= node
->GetNext();
469 bool wxSizer::Detach( int index
)
471 wxCHECK_MSG( index
>= 0 && (size_t)index
< m_children
.GetCount(),
473 _T("Detach index is out of range") );
475 wxSizerItemList::compatibility_iterator node
= m_children
.Item( index
);
477 wxCHECK_MSG( node
, false, _T("Failed to find child node") );
479 wxSizerItem
*item
= node
->GetData();
481 if( item
->IsSizer() )
483 else if( item
->IsWindow() )
484 item
->GetWindow()->SetContainingSizer( NULL
);
487 m_children
.Erase( node
);
491 void wxSizer::Clear( bool delete_windows
)
493 // First clear the ContainingSizer pointers
494 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
497 wxSizerItem
*item
= node
->GetData();
499 if (item
->IsWindow())
500 item
->GetWindow()->SetContainingSizer( NULL
);
501 node
= node
->GetNext();
504 // Destroy the windows if needed
508 // Now empty the list
509 WX_CLEAR_LIST(wxSizerItemList
, m_children
);
512 void wxSizer::DeleteWindows()
514 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
517 wxSizerItem
*item
= node
->GetData();
519 item
->DeleteWindows();
520 node
= node
->GetNext();
524 wxSize
wxSizer::Fit( wxWindow
*window
)
527 if (window
->IsTopLevel())
528 size
= FitSize( window
);
530 size
= GetMinWindowSize( window
);
532 window
->SetSize( size
);
537 void wxSizer::FitInside( wxWindow
*window
)
540 if (window
->IsTopLevel())
541 size
= VirtualFitSize( window
);
543 size
= GetMinClientSize( window
);
545 window
->SetVirtualSize( size
);
548 void wxSizer::Layout()
554 void wxSizer::SetSizeHints( wxWindow
*window
)
556 // Preserve the window's max size hints, but set the
557 // lower bound according to the sizer calculations.
559 wxSize size
= Fit( window
);
561 window
->SetSizeHints( size
.x
,
563 window
->GetMaxWidth(),
564 window
->GetMaxHeight() );
567 void wxSizer::SetVirtualSizeHints( wxWindow
*window
)
569 // Preserve the window's max size hints, but set the
570 // lower bound according to the sizer calculations.
573 wxSize
size( window
->GetVirtualSize() );
574 window
->SetVirtualSizeHints( size
.x
,
576 window
->GetMaxWidth(),
577 window
->GetMaxHeight() );
580 wxSize
wxSizer::GetMaxWindowSize( wxWindow
*window
) const
582 return window
->GetMaxSize();
585 wxSize
wxSizer::GetMinWindowSize( wxWindow
*window
)
587 wxSize
minSize( GetMinSize() );
588 wxSize
size( window
->GetSize() );
589 wxSize
client_size( window
->GetClientSize() );
591 return wxSize( minSize
.x
+size
.x
-client_size
.x
,
592 minSize
.y
+size
.y
-client_size
.y
);
595 // Return a window size that will fit within the screens dimensions
596 wxSize
wxSizer::FitSize( wxWindow
*window
)
598 wxSize size
= GetMinWindowSize( window
);
599 wxSize sizeMax
= GetMaxWindowSize( window
);
601 // Limit the size if sizeMax != wxDefaultSize
603 if ( size
.x
> sizeMax
.x
&& sizeMax
.x
!= -1 )
605 if ( size
.y
> sizeMax
.y
&& sizeMax
.y
!= -1 )
611 wxSize
wxSizer::GetMaxClientSize( wxWindow
*window
) const
613 wxSize
maxSize( window
->GetMaxSize() );
615 if( maxSize
!= wxDefaultSize
)
617 wxSize
size( window
->GetSize() );
618 wxSize
client_size( window
->GetClientSize() );
620 return wxSize( maxSize
.x
+ client_size
.x
- size
.x
,
621 maxSize
.y
+ client_size
.y
- size
.y
);
624 return wxDefaultSize
;
627 wxSize
wxSizer::GetMinClientSize( wxWindow
*WXUNUSED(window
) )
629 return GetMinSize(); // Already returns client size.
632 wxSize
wxSizer::VirtualFitSize( wxWindow
*window
)
634 wxSize size
= GetMinClientSize( window
);
635 wxSize sizeMax
= GetMaxClientSize( window
);
637 // Limit the size if sizeMax != wxDefaultSize
639 if ( size
.x
> sizeMax
.x
&& sizeMax
.x
!= -1 )
641 if ( size
.y
> sizeMax
.y
&& sizeMax
.y
!= -1 )
647 void wxSizer::SetDimension( int x
, int y
, int width
, int height
)
656 wxSize
wxSizer::GetMinSize()
658 wxSize
ret( CalcMin() );
659 if (ret
.x
< m_minSize
.x
) ret
.x
= m_minSize
.x
;
660 if (ret
.y
< m_minSize
.y
) ret
.y
= m_minSize
.y
;
664 void wxSizer::DoSetMinSize( int width
, int height
)
667 m_minSize
.y
= height
;
670 bool wxSizer::DoSetItemMinSize( wxWindow
*window
, int width
, int height
)
672 wxASSERT_MSG( window
, _T("SetMinSize for NULL window") );
674 // Is it our immediate child?
676 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
679 wxSizerItem
*item
= node
->GetData();
681 if (item
->GetWindow() == window
)
683 item
->SetInitSize( width
, height
);
686 node
= node
->GetNext();
689 // No? Search any subsizers we own then
691 node
= m_children
.GetFirst();
694 wxSizerItem
*item
= node
->GetData();
696 if ( item
->GetSizer() &&
697 item
->GetSizer()->DoSetItemMinSize( window
, width
, height
) )
699 // A child sizer found the requested windw, exit.
702 node
= node
->GetNext();
708 bool wxSizer::DoSetItemMinSize( wxSizer
*sizer
, int width
, int height
)
710 wxASSERT_MSG( sizer
, _T("SetMinSize for NULL sizer") );
712 // Is it our immediate child?
714 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
717 wxSizerItem
*item
= node
->GetData();
719 if (item
->GetSizer() == sizer
)
721 item
->GetSizer()->DoSetMinSize( width
, height
);
724 node
= node
->GetNext();
727 // No? Search any subsizers we own then
729 node
= m_children
.GetFirst();
732 wxSizerItem
*item
= node
->GetData();
734 if ( item
->GetSizer() &&
735 item
->GetSizer()->DoSetItemMinSize( sizer
, width
, height
) )
737 // A child found the requested sizer, exit.
740 node
= node
->GetNext();
746 bool wxSizer::DoSetItemMinSize( size_t index
, int width
, int height
)
748 wxSizerItemList::compatibility_iterator node
= m_children
.Item( index
);
750 wxCHECK_MSG( node
, false, _T("Failed to find child node") );
752 wxSizerItem
*item
= node
->GetData();
754 if (item
->GetSizer())
756 // Sizers contains the minimal size in them, if not calculated ...
757 item
->GetSizer()->DoSetMinSize( width
, height
);
761 // ... but the minimal size of spacers and windows in stored in them
762 item
->SetInitSize( width
, height
);
768 void wxSizer::Show( wxWindow
*window
, bool show
)
770 wxASSERT_MSG( window
, _T("Show for NULL window") );
772 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
775 wxSizerItem
*item
= node
->GetData();
777 if (item
->GetWindow() == window
)
782 node
= node
->GetNext();
786 void wxSizer::Show( wxSizer
*sizer
, bool show
)
788 wxASSERT_MSG( sizer
, _T("Show for NULL sizer") );
790 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
793 wxSizerItem
*item
= node
->GetData();
795 if (item
->GetSizer() == sizer
)
800 node
= node
->GetNext();
804 void wxSizer::Show( size_t index
, bool show
)
806 wxCHECK_RET( index
< m_children
.GetCount(),
807 _T("Show index is out of range") );
809 m_children
.Item( index
)->GetData()->Show( show
);
812 void wxSizer::ShowItems( bool show
)
814 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
817 node
->GetData()->Show( show
);
818 node
= node
->GetNext();
822 bool wxSizer::IsShown( wxWindow
*window
) const
824 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
827 wxSizerItem
*item
= node
->GetData();
829 if (item
->GetWindow() == window
)
831 return item
->IsShown();
833 node
= node
->GetNext();
836 wxFAIL_MSG( _T("IsShown failed to find sizer item") );
841 bool wxSizer::IsShown( wxSizer
*sizer
) const
843 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
846 wxSizerItem
*item
= node
->GetData();
848 if (item
->GetSizer() == sizer
)
850 return item
->IsShown();
852 node
= node
->GetNext();
855 wxFAIL_MSG( _T("IsShown failed to find sizer item") );
860 bool wxSizer::IsShown( size_t index
) const
862 wxCHECK_MSG( index
< m_children
.GetCount(),
864 _T("IsShown index is out of range") );
866 return m_children
.Item( index
)->GetData()->IsShown();
870 //---------------------------------------------------------------------------
872 //---------------------------------------------------------------------------
874 wxGridSizer::wxGridSizer( int rows
, int cols
, int vgap
, int hgap
)
882 wxGridSizer::wxGridSizer( int cols
, int vgap
, int hgap
)
890 int wxGridSizer::CalcRowsCols(int& nrows
, int& ncols
) const
892 int nitems
= m_children
.GetCount();
898 nrows
= (nitems
+ m_cols
- 1) / m_cols
;
902 ncols
= (nitems
+ m_rows
- 1) / m_rows
;
905 else // 0 columns, 0 rows?
907 wxFAIL_MSG( _T("grid sizer must have either rows or columns fixed") );
916 void wxGridSizer::RecalcSizes()
918 int nitems
, nrows
, ncols
;
919 if ( (nitems
= CalcRowsCols(nrows
, ncols
)) == 0 )
922 wxSize
sz( GetSize() );
923 wxPoint
pt( GetPosition() );
925 int w
= (sz
.x
- (ncols
- 1) * m_hgap
) / ncols
;
926 int h
= (sz
.y
- (nrows
- 1) * m_vgap
) / nrows
;
929 for (int c
= 0; c
< ncols
; c
++)
932 for (int r
= 0; r
< nrows
; r
++)
934 int i
= r
* ncols
+ c
;
937 wxSizerItemList::compatibility_iterator node
= m_children
.Item( i
);
939 wxASSERT_MSG( node
, _T("Failed to find SizerItemList node") );
941 SetItemBounds( node
->GetData(), x
, y
, w
, h
);
949 wxSize
wxGridSizer::CalcMin()
951 int nitems
, nrows
, ncols
;
952 if ( (nitems
= CalcRowsCols(nrows
, ncols
)) == 0 )
953 return wxSize(10, 10);
955 // Find the max width and height for any component
959 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
962 wxSizerItem
*item
= node
->GetData();
963 wxSize
sz( item
->CalcMin() );
965 w
= wxMax( w
, sz
.x
);
966 h
= wxMax( h
, sz
.y
);
968 node
= node
->GetNext();
971 return wxSize( ncols
* w
+ (ncols
-1) * m_hgap
,
972 nrows
* h
+ (nrows
-1) * m_vgap
);
975 void wxGridSizer::SetItemBounds( wxSizerItem
*item
, int x
, int y
, int w
, int h
)
978 wxSize
sz( item
->CalcMin() );
979 int flag
= item
->GetFlag();
981 if ((flag
& wxEXPAND
) || (flag
& wxSHAPED
))
987 if (flag
& wxALIGN_CENTER_HORIZONTAL
)
989 pt
.x
= x
+ (w
- sz
.x
) / 2;
991 else if (flag
& wxALIGN_RIGHT
)
993 pt
.x
= x
+ (w
- sz
.x
);
996 if (flag
& wxALIGN_CENTER_VERTICAL
)
998 pt
.y
= y
+ (h
- sz
.y
) / 2;
1000 else if (flag
& wxALIGN_BOTTOM
)
1002 pt
.y
= y
+ (h
- sz
.y
);
1006 item
->SetDimension(pt
, sz
);
1009 //---------------------------------------------------------------------------
1011 //---------------------------------------------------------------------------
1013 wxFlexGridSizer::wxFlexGridSizer( int rows
, int cols
, int vgap
, int hgap
)
1014 : wxGridSizer( rows
, cols
, vgap
, hgap
),
1015 m_flexDirection(wxBOTH
),
1016 m_growMode(wxFLEX_GROWMODE_SPECIFIED
)
1020 wxFlexGridSizer::wxFlexGridSizer( int cols
, int vgap
, int hgap
)
1021 : wxGridSizer( cols
, vgap
, hgap
),
1022 m_flexDirection(wxBOTH
),
1023 m_growMode(wxFLEX_GROWMODE_SPECIFIED
)
1027 wxFlexGridSizer::~wxFlexGridSizer()
1031 void wxFlexGridSizer::RecalcSizes()
1033 int nitems
, nrows
, ncols
;
1034 if ( (nitems
= CalcRowsCols(nrows
, ncols
)) == 0 )
1037 wxSize
sz( GetSize() );
1038 wxSize
minsz( CalcMin() );
1039 wxPoint
pt( GetPosition() );
1041 // what to do with the rows? by default, resize them proportionally
1042 if ( sz
.y
> minsz
.y
&& ( (m_flexDirection
& wxVERTICAL
) || (m_growMode
== wxFLEX_GROWMODE_SPECIFIED
) ) )
1044 int sum_proportions
= 0;
1045 int growable_space
= 0;
1048 for (idx
= 0; idx
< m_growableRows
.GetCount(); idx
++)
1050 // Since the number of rows/columns can change as items are inserted/deleted, we need
1051 // to verify at runtime that the requested growable rows/columns are still valid.
1052 if (m_growableRows
[idx
] >= nrows
)
1054 // If all items in a row/column are hidden, that row/column will have a dimension of -1.
1055 // This causes the row/column to be hidden completely.
1056 if (m_rowHeights
[ m_growableRows
[idx
] ] == -1)
1058 sum_proportions
+= m_growableRowsProportions
[idx
];
1059 growable_space
+= m_rowHeights
[ m_growableRows
[idx
] ];
1065 for (idx
= 0; idx
< m_growableRows
.GetCount(); idx
++)
1067 if (m_growableRows
[idx
] >= nrows
)
1069 if (m_rowHeights
[ m_growableRows
[idx
] ] == -1)
1070 m_rowHeights
[ m_growableRows
[idx
] ] = 0;
1073 int delta
= (sz
.y
- minsz
.y
);
1074 if (sum_proportions
== 0)
1075 delta
= (delta
/num
) + m_rowHeights
[ m_growableRows
[idx
] ];
1077 delta
= ((delta
+growable_space
)*m_growableRowsProportions
[idx
]) / sum_proportions
;
1078 m_rowHeights
[ m_growableRows
[idx
] ] = delta
;
1083 else if ( (m_growMode
== wxFLEX_GROWMODE_ALL
) && (sz
.y
> minsz
.y
) )
1085 // rounding problem?
1086 for ( int row
= 0; row
< nrows
; ++row
)
1087 m_rowHeights
[ row
] = sz
.y
/ nrows
;
1090 // the same logic as above but for the columns
1091 if ( sz
.x
> minsz
.x
&& ( (m_flexDirection
& wxHORIZONTAL
) || (m_growMode
== wxFLEX_GROWMODE_SPECIFIED
) ) )
1093 int sum_proportions
= 0;
1094 int growable_space
= 0;
1097 for (idx
= 0; idx
< m_growableCols
.GetCount(); idx
++)
1099 // Since the number of rows/columns can change as items are inserted/deleted, we need
1100 // to verify at runtime that the requested growable rows/columns are still valid.
1101 if (m_growableCols
[idx
] >= ncols
)
1103 // If all items in a row/column are hidden, that row/column will have a dimension of -1.
1104 // This causes the column to be hidden completely.
1105 if (m_colWidths
[ m_growableCols
[idx
] ] == -1)
1107 sum_proportions
+= m_growableColsProportions
[idx
];
1108 // wtb 5/12/02 bugfix - was m_ColWidths[idx]!!
1109 growable_space
+= m_colWidths
[ m_growableCols
[idx
] ];
1115 for (idx
= 0; idx
< m_growableCols
.GetCount(); idx
++)
1117 if (m_growableCols
[idx
] >= ncols
)
1119 if (m_colWidths
[ m_growableCols
[idx
] ] == -1)
1120 m_colWidths
[ m_growableCols
[idx
] ] = 0;
1123 int delta
= (sz
.x
- minsz
.x
);
1124 if (sum_proportions
== 0)
1125 delta
= (delta
/num
) + m_colWidths
[ m_growableCols
[idx
] ];
1127 delta
= ((delta
+growable_space
)*m_growableColsProportions
[idx
])/sum_proportions
;
1128 m_colWidths
[ m_growableCols
[idx
] ] = delta
;
1133 else if ( (m_growMode
== wxFLEX_GROWMODE_ALL
) && (sz
.x
> minsz
.x
) )
1135 for ( int col
=0; col
< ncols
; ++col
)
1136 m_colWidths
[ col
] = sz
.x
/ ncols
;
1139 sz
= wxSize( pt
.x
+ sz
.x
, pt
.y
+ sz
.y
);
1142 for (int c
= 0; c
< ncols
; c
++)
1145 for (int r
= 0; r
< nrows
; r
++)
1147 int i
= r
* ncols
+ c
;
1150 wxSizerItemList::compatibility_iterator node
= m_children
.Item( i
);
1152 wxASSERT_MSG( node
, _T("Failed to find node") );
1154 int w
= wxMax( 0, wxMin( m_colWidths
[c
], sz
.x
- x
) );
1155 int h
= wxMax( 0, wxMin( m_rowHeights
[r
], sz
.y
- y
) );
1157 SetItemBounds( node
->GetData(), x
, y
, w
, h
);
1159 y
= y
+ m_rowHeights
[r
] + m_vgap
;
1161 x
= x
+ m_colWidths
[c
] + m_hgap
;
1165 wxSize
wxFlexGridSizer::CalcMin()
1171 // Number of rows/columns can change as items are added or removed.
1172 if ( !CalcRowsCols(nrows
, ncols
) )
1173 return wxSize(10, 10);
1175 m_rowHeights
.SetCount(nrows
);
1176 m_colWidths
.SetCount(ncols
);
1178 // We have to recalcuate the sizes in case an item has wxADJUST_MINSIZE, has changed
1179 // minimum size since the previous layout, or has been hidden using wxSizer::Show().
1180 // If all the items in a row/column are hidden, the final dimension of the row/column
1181 // will be -1, indicating that the column itself is hidden.
1182 for( s
= m_rowHeights
.GetCount(), i
= 0; i
< s
; ++i
)
1183 m_rowHeights
[ i
] = -1;
1184 for( s
= m_colWidths
.GetCount(), i
= 0; i
< s
; ++i
)
1185 m_colWidths
[ i
] = -1;
1187 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
1192 wxSizerItem
*item
= node
->GetData();
1193 if ( item
->IsShown() )
1195 wxSize
sz( item
->CalcMin() );
1196 int row
= i
/ ncols
;
1197 int col
= i
% ncols
;
1199 m_rowHeights
[ row
] = wxMax( wxMax( 0, sz
.y
), m_rowHeights
[ row
] );
1200 m_colWidths
[ col
] = wxMax( wxMax( 0, sz
.x
), m_colWidths
[ col
] );
1203 node
= node
->GetNext();
1207 // the logic above works when we resize flexibly in both directions but
1208 // maybe this is not the case
1209 if ( m_flexDirection
!= wxBOTH
)
1211 // select the array corresponding to the direction in which we do *not*
1213 wxArrayInt
& array
= m_flexDirection
== wxVERTICAL
? m_colWidths
1216 const int count
= array
.GetCount();
1218 // find the largest value in this array
1220 for ( n
= 0; n
< count
; ++n
)
1222 if ( array
[n
] > largest
)
1226 // and now fill it with the largest value
1227 for ( n
= 0; n
< count
; ++n
)
1233 // Sum total minimum size, including gaps between rows/columns.
1234 // -1 is used as a magic number meaning empty column.
1236 for (int col
= 0; col
< ncols
; col
++)
1237 if ( m_colWidths
[ col
] != -1 )
1238 width
+= m_colWidths
[ col
] + ( col
== ncols
-1 ? 0 : m_hgap
);
1241 for (int row
= 0; row
< nrows
; row
++)
1242 if ( m_rowHeights
[ row
] != -1 )
1243 height
+= m_rowHeights
[ row
] + ( row
== nrows
-1 ? 0 : m_vgap
);
1245 return wxSize( width
, height
);
1248 void wxFlexGridSizer::AddGrowableRow( size_t idx
, int proportion
)
1250 m_growableRows
.Add( idx
);
1251 m_growableRowsProportions
.Add( proportion
);
1254 void wxFlexGridSizer::RemoveGrowableRow( size_t WXUNUSED(idx
) )
1258 void wxFlexGridSizer::AddGrowableCol( size_t idx
, int proportion
)
1260 m_growableCols
.Add( idx
);
1261 m_growableColsProportions
.Add( proportion
);
1264 void wxFlexGridSizer::RemoveGrowableCol( size_t WXUNUSED(idx
) )
1268 //---------------------------------------------------------------------------
1270 //---------------------------------------------------------------------------
1272 wxBoxSizer::wxBoxSizer( int orient
)
1273 : m_orient( orient
)
1277 void wxBoxSizer::RecalcSizes()
1279 if (m_children
.GetCount() == 0)
1286 if (m_orient
== wxHORIZONTAL
)
1288 delta
= (m_size
.x
- m_fixedWidth
) / m_stretchable
;
1289 extra
= (m_size
.x
- m_fixedWidth
) % m_stretchable
;
1293 delta
= (m_size
.y
- m_fixedHeight
) / m_stretchable
;
1294 extra
= (m_size
.y
- m_fixedHeight
) % m_stretchable
;
1298 wxPoint
pt( m_position
);
1300 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
1303 wxSizerItem
*item
= node
->GetData();
1305 if (item
->IsShown())
1308 if (item
->GetProportion())
1309 weight
= item
->GetProportion();
1311 wxSize
size( item
->CalcMin() );
1313 if (m_orient
== wxVERTICAL
)
1315 wxCoord height
= size
.y
;
1316 if (item
->GetProportion())
1318 height
= (delta
* weight
) + extra
;
1319 extra
= 0; // only the first item will get the remainder as extra size
1322 wxPoint
child_pos( pt
);
1323 wxSize
child_size( wxSize( size
.x
, height
) );
1325 if (item
->GetFlag() & (wxEXPAND
| wxSHAPED
))
1326 child_size
.x
= m_size
.x
;
1327 else if (item
->GetFlag() & wxALIGN_RIGHT
)
1328 child_pos
.x
+= m_size
.x
- size
.x
;
1329 else if (item
->GetFlag() & (wxCENTER
| wxALIGN_CENTER_HORIZONTAL
))
1330 // XXX wxCENTER is added for backward compatibility;
1331 // wxALIGN_CENTER should be used in new code
1332 child_pos
.x
+= (m_size
.x
- size
.x
) / 2;
1334 item
->SetDimension( child_pos
, child_size
);
1340 wxCoord width
= size
.x
;
1341 if (item
->GetProportion())
1343 width
= (delta
* weight
) + extra
;
1344 extra
= 0; // only the first item will get the remainder as extra size
1347 wxPoint
child_pos( pt
);
1348 wxSize
child_size( wxSize(width
, size
.y
) );
1350 if (item
->GetFlag() & (wxEXPAND
| wxSHAPED
))
1351 child_size
.y
= m_size
.y
;
1352 else if (item
->GetFlag() & wxALIGN_BOTTOM
)
1353 child_pos
.y
+= m_size
.y
- size
.y
;
1354 else if (item
->GetFlag() & (wxCENTER
| wxALIGN_CENTER_VERTICAL
))
1355 // XXX wxCENTER is added for backward compatibility;
1356 // wxALIGN_CENTER should be used in new code
1357 child_pos
.y
+= (m_size
.y
- size
.y
) / 2;
1359 item
->SetDimension( child_pos
, child_size
);
1365 node
= node
->GetNext();
1369 wxSize
wxBoxSizer::CalcMin()
1371 if (m_children
.GetCount() == 0)
1372 return wxSize(10,10);
1380 // Find how long each stretch unit needs to be
1381 int stretchSize
= 1;
1382 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
1386 wxSizerItem
*item
= node
->GetData();
1388 if (item
->IsShown() && item
->GetProportion() != 0)
1390 int stretch
= item
->GetProportion();
1391 wxSize
size( item
->CalcMin() );
1393 // Integer division rounded up is (a + b - 1) / b
1394 if (m_orient
== wxHORIZONTAL
)
1395 sizePerStretch
= ( size
.x
+ stretch
- 1 ) / stretch
;
1397 sizePerStretch
= ( size
.y
+ stretch
- 1 ) / stretch
;
1398 if (sizePerStretch
> stretchSize
)
1399 stretchSize
= sizePerStretch
;
1401 node
= node
->GetNext();
1404 // Calculate overall minimum size
1405 node
= m_children
.GetFirst();
1408 wxSizerItem
*item
= node
->GetData();
1410 if (item
->IsShown())
1412 m_stretchable
+= item
->GetProportion();
1414 wxSize
size( item
->CalcMin() );
1415 if (item
->GetProportion() != 0)
1417 if (m_orient
== wxHORIZONTAL
)
1418 size
.x
= stretchSize
* item
->GetProportion();
1420 size
.y
= stretchSize
* item
->GetProportion();
1423 if (m_orient
== wxHORIZONTAL
)
1425 m_minWidth
+= size
.x
;
1426 m_minHeight
= wxMax( m_minHeight
, size
.y
);
1430 m_minHeight
+= size
.y
;
1431 m_minWidth
= wxMax( m_minWidth
, size
.x
);
1434 if (item
->GetProportion() == 0)
1436 if (m_orient
== wxVERTICAL
)
1438 m_fixedHeight
+= size
.y
;
1439 m_fixedWidth
= wxMax( m_fixedWidth
, size
.x
);
1443 m_fixedWidth
+= size
.x
;
1444 m_fixedHeight
= wxMax( m_fixedHeight
, size
.y
);
1448 node
= node
->GetNext();
1451 return wxSize( m_minWidth
, m_minHeight
);
1454 //---------------------------------------------------------------------------
1456 //---------------------------------------------------------------------------
1460 wxStaticBoxSizer::wxStaticBoxSizer( wxStaticBox
*box
, int orient
)
1461 : wxBoxSizer( orient
)
1462 , m_staticBox( box
)
1464 wxASSERT_MSG( box
, wxT("wxStaticBoxSizer needs a static box") );
1467 static void GetStaticBoxBorders( wxStaticBox
*box
,
1471 // this has to be done platform by platform as there is no way to
1472 // guess the thickness of a wxStaticBox border
1474 box
->GetBordersForSizer(borderTop
,borderOther
);
1475 #else // __WXCOCOA__
1477 if ( box
->GetLabel().IsEmpty() )
1481 *borderTop
= box
->GetCharHeight();
1484 #endif // __WXCOCOA__
1487 void wxStaticBoxSizer::RecalcSizes()
1489 int top_border
, other_border
;
1490 GetStaticBoxBorders(m_staticBox
, &top_border
, &other_border
);
1492 m_staticBox
->SetSize( m_position
.x
, m_position
.y
, m_size
.x
, m_size
.y
);
1494 wxPoint
old_pos( m_position
);
1495 m_position
.x
+= other_border
;
1496 m_position
.y
+= top_border
;
1497 wxSize
old_size( m_size
);
1498 m_size
.x
-= 2*other_border
;
1499 m_size
.y
-= top_border
+ other_border
;
1501 wxBoxSizer::RecalcSizes();
1503 m_position
= old_pos
;
1507 wxSize
wxStaticBoxSizer::CalcMin()
1509 int top_border
, other_border
;
1510 GetStaticBoxBorders(m_staticBox
, &top_border
, &other_border
);
1512 wxSize
ret( wxBoxSizer::CalcMin() );
1513 ret
.x
+= 2*other_border
;
1514 ret
.y
+= other_border
+ top_border
;
1519 #endif // wxUSE_STATBOX
1521 //---------------------------------------------------------------------------
1523 //---------------------------------------------------------------------------
1527 wxNotebookSizer::wxNotebookSizer( wxNotebook
*nb
)
1530 wxASSERT_MSG( nb
, wxT("wxNotebookSizer needs a notebook") );
1533 void wxNotebookSizer::RecalcSizes()
1535 m_notebook
->SetSize( m_position
.x
, m_position
.y
, m_size
.x
, m_size
.y
);
1538 wxSize
wxNotebookSizer::CalcMin()
1540 wxSize sizeBorder
= m_notebook
->CalcSizeFromPage(wxSize(0, 0));
1545 if (m_notebook
->GetChildren().GetCount() == 0)
1547 return wxSize(sizeBorder
.x
+ 10, sizeBorder
.y
+ 10);
1553 wxWindowList::compatibility_iterator node
= m_notebook
->GetChildren().GetFirst();
1556 wxWindow
*item
= node
->GetData();
1557 wxSizer
*itemsizer
= item
->GetSizer();
1561 wxSize
subsize( itemsizer
->CalcMin() );
1563 if (subsize
.x
> maxX
)
1565 if (subsize
.y
> maxY
)
1569 node
= node
->GetNext();
1572 return wxSize( maxX
, maxY
) + sizeBorder
;
1575 #endif // wxUSE_NOTEBOOK