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
)
437 m_children
.Erase( node
);
440 node
= node
->GetNext();
446 bool wxSizer::Detach( wxWindow
*window
)
448 wxASSERT_MSG( window
, _T("Detaching NULL window") );
450 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
453 wxSizerItem
*item
= node
->GetData();
455 if (item
->GetWindow() == window
)
457 item
->GetWindow()->SetContainingSizer( NULL
);
458 m_children
.Erase( node
);
461 node
= node
->GetNext();
467 bool wxSizer::Detach( int index
)
469 wxCHECK_MSG( index
>= 0 && (size_t)index
< m_children
.GetCount(),
471 _T("Detach index is out of range") );
473 wxSizerItemList::compatibility_iterator node
= m_children
.Item( index
);
475 wxCHECK_MSG( node
, false, _T("Failed to find child node") );
477 wxSizerItem
*item
= node
->GetData();
479 if( item
->IsSizer() )
481 else if( item
->IsWindow() )
482 item
->GetWindow()->SetContainingSizer( NULL
);
484 m_children
.Erase( node
);
488 void wxSizer::Clear( bool delete_windows
)
490 // First clear the ContainingSizer pointers
491 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
494 wxSizerItem
*item
= node
->GetData();
496 if (item
->IsWindow())
497 item
->GetWindow()->SetContainingSizer( NULL
);
498 node
= node
->GetNext();
501 // Destroy the windows if needed
505 // Now empty the list
506 WX_CLEAR_LIST(wxSizerItemList
, m_children
);
509 void wxSizer::DeleteWindows()
511 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
514 wxSizerItem
*item
= node
->GetData();
516 item
->DeleteWindows();
517 node
= node
->GetNext();
521 wxSize
wxSizer::Fit( wxWindow
*window
)
524 if (window
->IsTopLevel())
525 size
= FitSize( window
);
527 size
= GetMinWindowSize( window
);
529 window
->SetSize( size
);
534 void wxSizer::FitInside( wxWindow
*window
)
537 if (window
->IsTopLevel())
538 size
= VirtualFitSize( window
);
540 size
= GetMinClientSize( window
);
542 window
->SetVirtualSize( size
);
545 void wxSizer::Layout()
551 void wxSizer::SetSizeHints( wxWindow
*window
)
553 // Preserve the window's max size hints, but set the
554 // lower bound according to the sizer calculations.
556 wxSize size
= Fit( window
);
558 window
->SetSizeHints( size
.x
,
560 window
->GetMaxWidth(),
561 window
->GetMaxHeight() );
564 void wxSizer::SetVirtualSizeHints( wxWindow
*window
)
566 // Preserve the window's max size hints, but set the
567 // lower bound according to the sizer calculations.
570 wxSize
size( window
->GetVirtualSize() );
571 window
->SetVirtualSizeHints( size
.x
,
573 window
->GetMaxWidth(),
574 window
->GetMaxHeight() );
577 wxSize
wxSizer::GetMaxWindowSize( wxWindow
*window
) const
579 return window
->GetMaxSize();
582 wxSize
wxSizer::GetMinWindowSize( wxWindow
*window
)
584 wxSize
minSize( GetMinSize() );
585 wxSize
size( window
->GetSize() );
586 wxSize
client_size( window
->GetClientSize() );
588 return wxSize( minSize
.x
+size
.x
-client_size
.x
,
589 minSize
.y
+size
.y
-client_size
.y
);
592 // Return a window size that will fit within the screens dimensions
593 wxSize
wxSizer::FitSize( wxWindow
*window
)
595 wxSize size
= GetMinWindowSize( window
);
596 wxSize sizeMax
= GetMaxWindowSize( window
);
598 // Limit the size if sizeMax != wxDefaultSize
600 if ( size
.x
> sizeMax
.x
&& sizeMax
.x
!= -1 )
602 if ( size
.y
> sizeMax
.y
&& sizeMax
.y
!= -1 )
608 wxSize
wxSizer::GetMaxClientSize( wxWindow
*window
) const
610 wxSize
maxSize( window
->GetMaxSize() );
612 if( maxSize
!= wxDefaultSize
)
614 wxSize
size( window
->GetSize() );
615 wxSize
client_size( window
->GetClientSize() );
617 return wxSize( maxSize
.x
+ client_size
.x
- size
.x
,
618 maxSize
.y
+ client_size
.y
- size
.y
);
621 return wxDefaultSize
;
624 wxSize
wxSizer::GetMinClientSize( wxWindow
*WXUNUSED(window
) )
626 return GetMinSize(); // Already returns client size.
629 wxSize
wxSizer::VirtualFitSize( wxWindow
*window
)
631 wxSize size
= GetMinClientSize( window
);
632 wxSize sizeMax
= GetMaxClientSize( window
);
634 // Limit the size if sizeMax != wxDefaultSize
636 if ( size
.x
> sizeMax
.x
&& sizeMax
.x
!= -1 )
638 if ( size
.y
> sizeMax
.y
&& sizeMax
.y
!= -1 )
644 void wxSizer::SetDimension( int x
, int y
, int width
, int height
)
653 wxSize
wxSizer::GetMinSize()
655 wxSize
ret( CalcMin() );
656 if (ret
.x
< m_minSize
.x
) ret
.x
= m_minSize
.x
;
657 if (ret
.y
< m_minSize
.y
) ret
.y
= m_minSize
.y
;
661 void wxSizer::DoSetMinSize( int width
, int height
)
664 m_minSize
.y
= height
;
667 bool wxSizer::DoSetItemMinSize( wxWindow
*window
, int width
, int height
)
669 wxASSERT_MSG( window
, _T("SetMinSize for NULL window") );
671 // Is it our immediate child?
673 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
676 wxSizerItem
*item
= node
->GetData();
678 if (item
->GetWindow() == window
)
680 item
->SetInitSize( width
, height
);
683 node
= node
->GetNext();
686 // No? Search any subsizers we own then
688 node
= m_children
.GetFirst();
691 wxSizerItem
*item
= node
->GetData();
693 if ( item
->GetSizer() &&
694 item
->GetSizer()->DoSetItemMinSize( window
, width
, height
) )
696 // A child sizer found the requested windw, exit.
699 node
= node
->GetNext();
705 bool wxSizer::DoSetItemMinSize( wxSizer
*sizer
, int width
, int height
)
707 wxASSERT_MSG( sizer
, _T("SetMinSize for NULL sizer") );
709 // Is it our immediate child?
711 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
714 wxSizerItem
*item
= node
->GetData();
716 if (item
->GetSizer() == sizer
)
718 item
->GetSizer()->DoSetMinSize( width
, height
);
721 node
= node
->GetNext();
724 // No? Search any subsizers we own then
726 node
= m_children
.GetFirst();
729 wxSizerItem
*item
= node
->GetData();
731 if ( item
->GetSizer() &&
732 item
->GetSizer()->DoSetItemMinSize( sizer
, width
, height
) )
734 // A child found the requested sizer, exit.
737 node
= node
->GetNext();
743 bool wxSizer::DoSetItemMinSize( size_t index
, int width
, int height
)
745 wxSizerItemList::compatibility_iterator node
= m_children
.Item( index
);
747 wxCHECK_MSG( node
, false, _T("Failed to find child node") );
749 wxSizerItem
*item
= node
->GetData();
751 if (item
->GetSizer())
753 // Sizers contains the minimal size in them, if not calculated ...
754 item
->GetSizer()->DoSetMinSize( width
, height
);
758 // ... but the minimal size of spacers and windows in stored in them
759 item
->SetInitSize( width
, height
);
765 void wxSizer::Show( wxWindow
*window
, bool show
)
767 wxASSERT_MSG( window
, _T("Show for NULL window") );
769 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
772 wxSizerItem
*item
= node
->GetData();
774 if (item
->GetWindow() == window
)
779 node
= node
->GetNext();
783 void wxSizer::Show( wxSizer
*sizer
, bool show
)
785 wxASSERT_MSG( sizer
, _T("Show for NULL sizer") );
787 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
790 wxSizerItem
*item
= node
->GetData();
792 if (item
->GetSizer() == sizer
)
797 node
= node
->GetNext();
801 void wxSizer::Show( size_t index
, bool show
)
803 wxCHECK_RET( index
< m_children
.GetCount(),
804 _T("Show index is out of range") );
806 m_children
.Item( index
)->GetData()->Show( show
);
809 void wxSizer::ShowItems( bool show
)
811 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
814 node
->GetData()->Show( show
);
815 node
= node
->GetNext();
819 bool wxSizer::IsShown( wxWindow
*window
) const
821 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
824 wxSizerItem
*item
= node
->GetData();
826 if (item
->GetWindow() == window
)
828 return item
->IsShown();
830 node
= node
->GetNext();
833 wxFAIL_MSG( _T("IsShown failed to find sizer item") );
838 bool wxSizer::IsShown( wxSizer
*sizer
) const
840 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
843 wxSizerItem
*item
= node
->GetData();
845 if (item
->GetSizer() == sizer
)
847 return item
->IsShown();
849 node
= node
->GetNext();
852 wxFAIL_MSG( _T("IsShown failed to find sizer item") );
857 bool wxSizer::IsShown( size_t index
) const
859 wxCHECK_MSG( index
< m_children
.GetCount(),
861 _T("IsShown index is out of range") );
863 return m_children
.Item( index
)->GetData()->IsShown();
867 //---------------------------------------------------------------------------
869 //---------------------------------------------------------------------------
871 wxGridSizer::wxGridSizer( int rows
, int cols
, int vgap
, int hgap
)
879 wxGridSizer::wxGridSizer( int cols
, int vgap
, int hgap
)
887 int wxGridSizer::CalcRowsCols(int& nrows
, int& ncols
) const
889 int nitems
= m_children
.GetCount();
895 nrows
= (nitems
+ m_cols
- 1) / m_cols
;
899 ncols
= (nitems
+ m_rows
- 1) / m_rows
;
902 else // 0 columns, 0 rows?
904 wxFAIL_MSG( _T("grid sizer must have either rows or columns fixed") );
913 void wxGridSizer::RecalcSizes()
915 int nitems
, nrows
, ncols
;
916 if ( (nitems
= CalcRowsCols(nrows
, ncols
)) == 0 )
919 wxSize
sz( GetSize() );
920 wxPoint
pt( GetPosition() );
922 int w
= (sz
.x
- (ncols
- 1) * m_hgap
) / ncols
;
923 int h
= (sz
.y
- (nrows
- 1) * m_vgap
) / nrows
;
926 for (int c
= 0; c
< ncols
; c
++)
929 for (int r
= 0; r
< nrows
; r
++)
931 int i
= r
* ncols
+ c
;
934 wxSizerItemList::compatibility_iterator node
= m_children
.Item( i
);
936 wxASSERT_MSG( node
, _T("Failed to find SizerItemList node") );
938 SetItemBounds( node
->GetData(), x
, y
, w
, h
);
946 wxSize
wxGridSizer::CalcMin()
948 int nitems
, nrows
, ncols
;
949 if ( (nitems
= CalcRowsCols(nrows
, ncols
)) == 0 )
950 return wxSize(10, 10);
952 // Find the max width and height for any component
956 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
959 wxSizerItem
*item
= node
->GetData();
960 wxSize
sz( item
->CalcMin() );
962 w
= wxMax( w
, sz
.x
);
963 h
= wxMax( h
, sz
.y
);
965 node
= node
->GetNext();
968 return wxSize( ncols
* w
+ (ncols
-1) * m_hgap
,
969 nrows
* h
+ (nrows
-1) * m_vgap
);
972 void wxGridSizer::SetItemBounds( wxSizerItem
*item
, int x
, int y
, int w
, int h
)
975 wxSize
sz( item
->CalcMin() );
976 int flag
= item
->GetFlag();
978 if ((flag
& wxEXPAND
) || (flag
& wxSHAPED
))
984 if (flag
& wxALIGN_CENTER_HORIZONTAL
)
986 pt
.x
= x
+ (w
- sz
.x
) / 2;
988 else if (flag
& wxALIGN_RIGHT
)
990 pt
.x
= x
+ (w
- sz
.x
);
993 if (flag
& wxALIGN_CENTER_VERTICAL
)
995 pt
.y
= y
+ (h
- sz
.y
) / 2;
997 else if (flag
& wxALIGN_BOTTOM
)
999 pt
.y
= y
+ (h
- sz
.y
);
1003 item
->SetDimension(pt
, sz
);
1006 //---------------------------------------------------------------------------
1008 //---------------------------------------------------------------------------
1010 wxFlexGridSizer::wxFlexGridSizer( int rows
, int cols
, int vgap
, int hgap
)
1011 : wxGridSizer( rows
, cols
, vgap
, hgap
),
1012 m_flexDirection(wxBOTH
),
1013 m_growMode(wxFLEX_GROWMODE_SPECIFIED
)
1017 wxFlexGridSizer::wxFlexGridSizer( int cols
, int vgap
, int hgap
)
1018 : wxGridSizer( cols
, vgap
, hgap
),
1019 m_flexDirection(wxBOTH
),
1020 m_growMode(wxFLEX_GROWMODE_SPECIFIED
)
1024 wxFlexGridSizer::~wxFlexGridSizer()
1028 void wxFlexGridSizer::RecalcSizes()
1030 int nitems
, nrows
, ncols
;
1031 if ( (nitems
= CalcRowsCols(nrows
, ncols
)) == 0 )
1034 wxSize
sz( GetSize() );
1035 wxSize
minsz( CalcMin() );
1036 wxPoint
pt( GetPosition() );
1038 // what to do with the rows? by default, resize them proportionally
1039 if ( sz
.y
> minsz
.y
&& ( (m_flexDirection
& wxVERTICAL
) || (m_growMode
== wxFLEX_GROWMODE_SPECIFIED
) ) )
1041 int sum_proportions
= 0;
1042 int growable_space
= 0;
1045 for (idx
= 0; idx
< m_growableRows
.GetCount(); idx
++)
1047 // Since the number of rows/columns can change as items are inserted/deleted, we need
1048 // to verify at runtime that the requested growable rows/columns are still valid.
1049 if (m_growableRows
[idx
] >= nrows
)
1051 // If all items in a row/column are hidden, that row/column will have a dimension of -1.
1052 // This causes the row/column to be hidden completely.
1053 if (m_rowHeights
[ m_growableRows
[idx
] ] == -1)
1055 sum_proportions
+= m_growableRowsProportions
[idx
];
1056 growable_space
+= m_rowHeights
[ m_growableRows
[idx
] ];
1062 for (idx
= 0; idx
< m_growableRows
.GetCount(); idx
++)
1064 if (m_growableRows
[idx
] >= nrows
)
1066 if (m_rowHeights
[ m_growableRows
[idx
] ] == -1)
1067 m_rowHeights
[ m_growableRows
[idx
] ] = 0;
1070 int delta
= (sz
.y
- minsz
.y
);
1071 if (sum_proportions
== 0)
1072 delta
= (delta
/num
) + m_rowHeights
[ m_growableRows
[idx
] ];
1074 delta
= ((delta
+growable_space
)*m_growableRowsProportions
[idx
]) / sum_proportions
;
1075 m_rowHeights
[ m_growableRows
[idx
] ] = delta
;
1080 else if ( (m_growMode
== wxFLEX_GROWMODE_ALL
) && (sz
.y
> minsz
.y
) )
1082 // rounding problem?
1083 for ( int row
= 0; row
< nrows
; ++row
)
1084 m_rowHeights
[ row
] = sz
.y
/ nrows
;
1087 // the same logic as above but for the columns
1088 if ( sz
.x
> minsz
.x
&& ( (m_flexDirection
& wxHORIZONTAL
) || (m_growMode
== wxFLEX_GROWMODE_SPECIFIED
) ) )
1090 int sum_proportions
= 0;
1091 int growable_space
= 0;
1094 for (idx
= 0; idx
< m_growableCols
.GetCount(); idx
++)
1096 // Since the number of rows/columns can change as items are inserted/deleted, we need
1097 // to verify at runtime that the requested growable rows/columns are still valid.
1098 if (m_growableCols
[idx
] >= ncols
)
1100 // If all items in a row/column are hidden, that row/column will have a dimension of -1.
1101 // This causes the column to be hidden completely.
1102 if (m_colWidths
[ m_growableCols
[idx
] ] == -1)
1104 sum_proportions
+= m_growableColsProportions
[idx
];
1105 // wtb 5/12/02 bugfix - was m_ColWidths[idx]!!
1106 growable_space
+= m_colWidths
[ m_growableCols
[idx
] ];
1112 for (idx
= 0; idx
< m_growableCols
.GetCount(); idx
++)
1114 if (m_growableCols
[idx
] >= ncols
)
1116 if (m_colWidths
[ m_growableCols
[idx
] ] == -1)
1117 m_colWidths
[ m_growableCols
[idx
] ] = 0;
1120 int delta
= (sz
.x
- minsz
.x
);
1121 if (sum_proportions
== 0)
1122 delta
= (delta
/num
) + m_colWidths
[ m_growableCols
[idx
] ];
1124 delta
= ((delta
+growable_space
)*m_growableColsProportions
[idx
])/sum_proportions
;
1125 m_colWidths
[ m_growableCols
[idx
] ] = delta
;
1130 else if ( (m_growMode
== wxFLEX_GROWMODE_ALL
) && (sz
.x
> minsz
.x
) )
1132 for ( int col
=0; col
< ncols
; ++col
)
1133 m_colWidths
[ col
] = sz
.x
/ ncols
;
1136 sz
= wxSize( pt
.x
+ sz
.x
, pt
.y
+ sz
.y
);
1139 for (int c
= 0; c
< ncols
; c
++)
1142 for (int r
= 0; r
< nrows
; r
++)
1144 int i
= r
* ncols
+ c
;
1147 wxSizerItemList::compatibility_iterator node
= m_children
.Item( i
);
1149 wxASSERT_MSG( node
, _T("Failed to find node") );
1151 int w
= wxMax( 0, wxMin( m_colWidths
[c
], sz
.x
- x
) );
1152 int h
= wxMax( 0, wxMin( m_rowHeights
[r
], sz
.y
- y
) );
1154 SetItemBounds( node
->GetData(), x
, y
, w
, h
);
1156 y
= y
+ m_rowHeights
[r
] + m_vgap
;
1158 x
= x
+ m_colWidths
[c
] + m_hgap
;
1162 wxSize
wxFlexGridSizer::CalcMin()
1168 // Number of rows/columns can change as items are added or removed.
1169 if ( !CalcRowsCols(nrows
, ncols
) )
1170 return wxSize(10, 10);
1172 m_rowHeights
.SetCount(nrows
);
1173 m_colWidths
.SetCount(ncols
);
1175 // We have to recalcuate the sizes in case an item has wxADJUST_MINSIZE, has changed
1176 // minimum size since the previous layout, or has been hidden using wxSizer::Show().
1177 // If all the items in a row/column are hidden, the final dimension of the row/column
1178 // will be -1, indicating that the column itself is hidden.
1179 for( s
= m_rowHeights
.GetCount(), i
= 0; i
< s
; ++i
)
1180 m_rowHeights
[ i
] = -1;
1181 for( s
= m_colWidths
.GetCount(), i
= 0; i
< s
; ++i
)
1182 m_colWidths
[ i
] = -1;
1184 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
1189 wxSizerItem
*item
= node
->GetData();
1190 if ( item
->IsShown() )
1192 wxSize
sz( item
->CalcMin() );
1193 int row
= i
/ ncols
;
1194 int col
= i
% ncols
;
1196 m_rowHeights
[ row
] = wxMax( wxMax( 0, sz
.y
), m_rowHeights
[ row
] );
1197 m_colWidths
[ col
] = wxMax( wxMax( 0, sz
.x
), m_colWidths
[ col
] );
1200 node
= node
->GetNext();
1204 // the logic above works when we resize flexibly in both directions but
1205 // maybe this is not the case
1206 if ( m_flexDirection
!= wxBOTH
)
1208 // select the array corresponding to the direction in which we do *not*
1210 wxArrayInt
& array
= m_flexDirection
== wxVERTICAL
? m_colWidths
1213 const int count
= array
.GetCount();
1215 // find the largest value in this array
1217 for ( n
= 0; n
< count
; ++n
)
1219 if ( array
[n
] > largest
)
1223 // and now fill it with the largest value
1224 for ( n
= 0; n
< count
; ++n
)
1230 // Sum total minimum size, including gaps between rows/columns.
1231 // -1 is used as a magic number meaning empty column.
1233 for (int col
= 0; col
< ncols
; col
++)
1234 if ( m_colWidths
[ col
] != -1 )
1235 width
+= m_colWidths
[ col
] + ( col
== ncols
-1 ? 0 : m_hgap
);
1238 for (int row
= 0; row
< nrows
; row
++)
1239 if ( m_rowHeights
[ row
] != -1 )
1240 height
+= m_rowHeights
[ row
] + ( row
== nrows
-1 ? 0 : m_vgap
);
1242 return wxSize( width
, height
);
1245 void wxFlexGridSizer::AddGrowableRow( size_t idx
, int proportion
)
1247 m_growableRows
.Add( idx
);
1248 m_growableRowsProportions
.Add( proportion
);
1251 void wxFlexGridSizer::RemoveGrowableRow( size_t WXUNUSED(idx
) )
1255 void wxFlexGridSizer::AddGrowableCol( size_t idx
, int proportion
)
1257 m_growableCols
.Add( idx
);
1258 m_growableColsProportions
.Add( proportion
);
1261 void wxFlexGridSizer::RemoveGrowableCol( size_t WXUNUSED(idx
) )
1265 //---------------------------------------------------------------------------
1267 //---------------------------------------------------------------------------
1269 wxBoxSizer::wxBoxSizer( int orient
)
1270 : m_orient( orient
)
1274 void wxBoxSizer::RecalcSizes()
1276 if (m_children
.GetCount() == 0)
1283 if (m_orient
== wxHORIZONTAL
)
1285 delta
= (m_size
.x
- m_fixedWidth
) / m_stretchable
;
1286 extra
= (m_size
.x
- m_fixedWidth
) % m_stretchable
;
1290 delta
= (m_size
.y
- m_fixedHeight
) / m_stretchable
;
1291 extra
= (m_size
.y
- m_fixedHeight
) % m_stretchable
;
1295 wxPoint
pt( m_position
);
1297 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
1300 wxSizerItem
*item
= node
->GetData();
1302 if (item
->IsShown())
1305 if (item
->GetProportion())
1306 weight
= item
->GetProportion();
1308 wxSize
size( item
->CalcMin() );
1310 if (m_orient
== wxVERTICAL
)
1312 wxCoord height
= size
.y
;
1313 if (item
->GetProportion())
1315 height
= (delta
* weight
) + extra
;
1316 extra
= 0; // only the first item will get the remainder as extra size
1319 wxPoint
child_pos( pt
);
1320 wxSize
child_size( wxSize( size
.x
, height
) );
1322 if (item
->GetFlag() & (wxEXPAND
| wxSHAPED
))
1323 child_size
.x
= m_size
.x
;
1324 else if (item
->GetFlag() & wxALIGN_RIGHT
)
1325 child_pos
.x
+= m_size
.x
- size
.x
;
1326 else if (item
->GetFlag() & (wxCENTER
| wxALIGN_CENTER_HORIZONTAL
))
1327 // XXX wxCENTER is added for backward compatibility;
1328 // wxALIGN_CENTER should be used in new code
1329 child_pos
.x
+= (m_size
.x
- size
.x
) / 2;
1331 item
->SetDimension( child_pos
, child_size
);
1337 wxCoord width
= size
.x
;
1338 if (item
->GetProportion())
1340 width
= (delta
* weight
) + extra
;
1341 extra
= 0; // only the first item will get the remainder as extra size
1344 wxPoint
child_pos( pt
);
1345 wxSize
child_size( wxSize(width
, size
.y
) );
1347 if (item
->GetFlag() & (wxEXPAND
| wxSHAPED
))
1348 child_size
.y
= m_size
.y
;
1349 else if (item
->GetFlag() & wxALIGN_BOTTOM
)
1350 child_pos
.y
+= m_size
.y
- size
.y
;
1351 else if (item
->GetFlag() & (wxCENTER
| wxALIGN_CENTER_VERTICAL
))
1352 // XXX wxCENTER is added for backward compatibility;
1353 // wxALIGN_CENTER should be used in new code
1354 child_pos
.y
+= (m_size
.y
- size
.y
) / 2;
1356 item
->SetDimension( child_pos
, child_size
);
1362 node
= node
->GetNext();
1366 wxSize
wxBoxSizer::CalcMin()
1368 if (m_children
.GetCount() == 0)
1369 return wxSize(10,10);
1377 // Find how long each stretch unit needs to be
1378 int stretchSize
= 1;
1379 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
1383 wxSizerItem
*item
= node
->GetData();
1385 if (item
->IsShown() && item
->GetProportion() != 0)
1387 int stretch
= item
->GetProportion();
1388 wxSize
size( item
->CalcMin() );
1390 // Integer division rounded up is (a + b - 1) / b
1391 if (m_orient
== wxHORIZONTAL
)
1392 sizePerStretch
= ( size
.x
+ stretch
- 1 ) / stretch
;
1394 sizePerStretch
= ( size
.y
+ stretch
- 1 ) / stretch
;
1395 if (sizePerStretch
> stretchSize
)
1396 stretchSize
= sizePerStretch
;
1398 node
= node
->GetNext();
1401 // Calculate overall minimum size
1402 node
= m_children
.GetFirst();
1405 wxSizerItem
*item
= node
->GetData();
1407 if (item
->IsShown())
1409 m_stretchable
+= item
->GetProportion();
1411 wxSize
size( item
->CalcMin() );
1412 if (item
->GetProportion() != 0)
1414 if (m_orient
== wxHORIZONTAL
)
1415 size
.x
= stretchSize
* item
->GetProportion();
1417 size
.y
= stretchSize
* item
->GetProportion();
1420 if (m_orient
== wxHORIZONTAL
)
1422 m_minWidth
+= size
.x
;
1423 m_minHeight
= wxMax( m_minHeight
, size
.y
);
1427 m_minHeight
+= size
.y
;
1428 m_minWidth
= wxMax( m_minWidth
, size
.x
);
1431 if (item
->GetProportion() == 0)
1433 if (m_orient
== wxVERTICAL
)
1435 m_fixedHeight
+= size
.y
;
1436 m_fixedWidth
= wxMax( m_fixedWidth
, size
.x
);
1440 m_fixedWidth
+= size
.x
;
1441 m_fixedHeight
= wxMax( m_fixedHeight
, size
.y
);
1445 node
= node
->GetNext();
1448 return wxSize( m_minWidth
, m_minHeight
);
1451 //---------------------------------------------------------------------------
1453 //---------------------------------------------------------------------------
1457 wxStaticBoxSizer::wxStaticBoxSizer( wxStaticBox
*box
, int orient
)
1458 : wxBoxSizer( orient
)
1459 , m_staticBox( box
)
1461 wxASSERT_MSG( box
, wxT("wxStaticBoxSizer needs a static box") );
1464 static void GetStaticBoxBorders( wxStaticBox
*box
,
1468 // this has to be done platform by platform as there is no way to
1469 // guess the thickness of a wxStaticBox border
1471 box
->GetBordersForSizer(borderTop
,borderOther
);
1472 #else // __WXCOCOA__
1474 if ( box
->GetLabel().IsEmpty() )
1478 *borderTop
= box
->GetCharHeight();
1481 #endif // __WXCOCOA__
1484 void wxStaticBoxSizer::RecalcSizes()
1486 int top_border
, other_border
;
1487 GetStaticBoxBorders(m_staticBox
, &top_border
, &other_border
);
1489 m_staticBox
->SetSize( m_position
.x
, m_position
.y
, m_size
.x
, m_size
.y
);
1491 wxPoint
old_pos( m_position
);
1492 m_position
.x
+= other_border
;
1493 m_position
.y
+= top_border
;
1494 wxSize
old_size( m_size
);
1495 m_size
.x
-= 2*other_border
;
1496 m_size
.y
-= top_border
+ other_border
;
1498 wxBoxSizer::RecalcSizes();
1500 m_position
= old_pos
;
1504 wxSize
wxStaticBoxSizer::CalcMin()
1506 int top_border
, other_border
;
1507 GetStaticBoxBorders(m_staticBox
, &top_border
, &other_border
);
1509 wxSize
ret( wxBoxSizer::CalcMin() );
1510 ret
.x
+= 2*other_border
;
1511 ret
.y
+= other_border
+ top_border
;
1516 #endif // wxUSE_STATBOX
1518 //---------------------------------------------------------------------------
1520 //---------------------------------------------------------------------------
1524 wxNotebookSizer::wxNotebookSizer( wxNotebook
*nb
)
1527 wxASSERT_MSG( nb
, wxT("wxNotebookSizer needs a notebook") );
1530 void wxNotebookSizer::RecalcSizes()
1532 m_notebook
->SetSize( m_position
.x
, m_position
.y
, m_size
.x
, m_size
.y
);
1535 wxSize
wxNotebookSizer::CalcMin()
1537 wxSize sizeBorder
= m_notebook
->CalcSizeFromPage(wxSize(0, 0));
1542 if (m_notebook
->GetChildren().GetCount() == 0)
1544 return wxSize(sizeBorder
.x
+ 10, sizeBorder
.y
+ 10);
1550 wxWindowList::compatibility_iterator node
= m_notebook
->GetChildren().GetFirst();
1553 wxWindow
*item
= node
->GetData();
1554 wxSizer
*itemsizer
= item
->GetSizer();
1558 wxSize
subsize( itemsizer
->CalcMin() );
1560 if (subsize
.x
> maxX
)
1562 if (subsize
.y
> maxY
)
1566 node
= node
->GetNext();
1569 return wxSize( maxX
, maxY
) + sizeBorder
;
1572 #endif // wxUSE_NOTEBOOK