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()
101 m_window
->SetContainingSizer(NULL
);
103 else // we must be a sizer
110 wxSize
wxSizerItem::GetSize() const
114 ret
= m_sizer
->GetSize();
117 ret
= m_window
->GetSize();
124 if (m_flag
& wxNORTH
)
126 if (m_flag
& wxSOUTH
)
132 wxSize
wxSizerItem::CalcMin()
137 ret
= m_sizer
->GetMinSize();
139 // if we have to preserve aspect ratio _AND_ this is
140 // the first-time calculation, consider ret to be initial size
141 if ((m_flag
& wxSHAPED
) && !m_ratio
)
146 if ( IsWindow() && (m_flag
& wxADJUST_MINSIZE
) )
148 // By user request, keep the minimal size for this item
149 // in sync with the largest of BestSize and any user supplied
150 // minimum size hint. Useful in cases where the item is
151 // changeable -- static text labels, etc.
152 m_minSize
= m_window
->GetAdjustedBestSize();
162 if (m_flag
& wxNORTH
)
164 if (m_flag
& wxSOUTH
)
170 void wxSizerItem::SetDimension( wxPoint pos
, wxSize size
)
172 if (m_flag
& wxSHAPED
)
174 // adjust aspect ratio
175 int rwidth
= (int) (size
.y
* m_ratio
);
179 int rheight
= (int) (size
.x
/ m_ratio
);
180 // add vertical space
181 if (m_flag
& wxALIGN_CENTER_VERTICAL
)
182 pos
.y
+= (size
.y
- rheight
) / 2;
183 else if (m_flag
& wxALIGN_BOTTOM
)
184 pos
.y
+= (size
.y
- rheight
);
185 // use reduced dimensions
188 else if (rwidth
< size
.x
)
190 // add horizontal space
191 if (m_flag
& wxALIGN_CENTER_HORIZONTAL
)
192 pos
.x
+= (size
.x
- rwidth
) / 2;
193 else if (m_flag
& wxALIGN_RIGHT
)
194 pos
.x
+= (size
.x
- rwidth
);
199 // This is what GetPosition() returns. Since we calculate
200 // borders afterwards, GetPosition() will be the left/top
201 // corner of the surrounding border.
213 if (m_flag
& wxNORTH
)
218 if (m_flag
& wxSOUTH
)
224 m_sizer
->SetDimension( pos
.x
, pos
.y
, size
.x
, size
.y
);
227 m_window
->SetSize( pos
.x
, pos
.y
, size
.x
, size
.y
, wxSIZE_ALLOW_MINUS_ONE
);
232 void wxSizerItem::DeleteWindows()
238 m_sizer
->DeleteWindows();
241 bool wxSizerItem::IsWindow() const
243 return (m_window
!= NULL
);
246 bool wxSizerItem::IsSizer() const
248 return (m_sizer
!= NULL
);
251 bool wxSizerItem::IsSpacer() const
253 return (m_window
== NULL
) && (m_sizer
== NULL
);
256 void wxSizerItem::Show( bool show
)
261 m_window
->Show( show
);
263 m_sizer
->ShowItems( show
);
265 // ... nothing else to do to hide/show spacers
268 void wxSizerItem::SetOption( int option
)
270 SetProportion( option
);
273 int wxSizerItem::GetOption() const
275 return GetProportion();
279 //---------------------------------------------------------------------------
281 //---------------------------------------------------------------------------
284 : m_minSize( wxSize( 0, 0 ) )
290 WX_CLEAR_LIST(wxSizerItemList
, m_children
);
293 void wxSizer::Add( wxWindow
*window
, int proportion
, int flag
, int border
, wxObject
* userData
)
295 m_children
.Append( new wxSizerItem( window
, proportion
, flag
, border
, userData
) );
296 window
->SetContainingSizer( this );
299 void wxSizer::Add( wxSizer
*sizer
, int proportion
, int flag
, int border
, wxObject
* userData
)
301 m_children
.Append( new wxSizerItem( sizer
, proportion
, flag
, border
, userData
) );
304 void wxSizer::Add( int width
, int height
, int proportion
, int flag
, int border
, wxObject
* userData
)
306 m_children
.Append( new wxSizerItem( width
, height
, proportion
, flag
, border
, userData
) );
309 void wxSizer::Add( wxSizerItem
*item
)
311 m_children
.Append( item
);
313 if( item
->GetWindow() )
314 item
->GetWindow()->SetContainingSizer( this );
317 void wxSizer::Prepend( wxWindow
*window
, int proportion
, int flag
, int border
, wxObject
* userData
)
319 m_children
.Insert( new wxSizerItem( window
, proportion
, flag
, border
, userData
) );
320 window
->SetContainingSizer( this );
323 void wxSizer::Prepend( wxSizer
*sizer
, int proportion
, int flag
, int border
, wxObject
* userData
)
325 m_children
.Insert( new wxSizerItem( sizer
, proportion
, flag
, border
, userData
) );
328 void wxSizer::Prepend( int width
, int height
, int proportion
, int flag
, int border
, wxObject
* userData
)
330 m_children
.Insert( new wxSizerItem( width
, height
, proportion
, flag
, border
, userData
) );
333 void wxSizer::Prepend( wxSizerItem
*item
)
335 m_children
.Insert( item
);
337 if( item
->GetWindow() )
338 item
->GetWindow()->SetContainingSizer( this );
341 void wxSizer::Insert( size_t index
,
348 m_children
.Insert( index
,
349 new wxSizerItem( window
, proportion
, flag
, border
, userData
) );
350 window
->SetContainingSizer( this );
353 void wxSizer::Insert( size_t index
,
360 m_children
.Insert( index
,
361 new wxSizerItem( sizer
, proportion
, flag
, border
, userData
) );
364 void wxSizer::Insert( size_t index
,
372 m_children
.Insert( index
,
373 new wxSizerItem( width
, height
, proportion
, flag
, border
, userData
) );
376 void wxSizer::Insert( size_t index
, wxSizerItem
*item
)
378 m_children
.Insert( index
, item
);
380 if( item
->GetWindow() )
381 item
->GetWindow()->SetContainingSizer( this );
384 bool wxSizer::Remove( wxWindow
*window
)
386 return Detach( window
);
389 bool wxSizer::Remove( wxSizer
*sizer
)
391 wxASSERT_MSG( sizer
, _T("Removing NULL sizer") );
393 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
396 wxSizerItem
*item
= node
->GetData();
398 if (item
->GetSizer() == sizer
)
401 m_children
.Erase( node
);
405 node
= node
->GetNext();
411 bool wxSizer::Remove( int index
)
413 wxCHECK_MSG( index
>= 0 && (size_t)index
< m_children
.GetCount(),
415 _T("Remove index is out of range") );
417 wxSizerItemList::compatibility_iterator node
= m_children
.Item( index
);
419 wxCHECK_MSG( node
, false, _T("Failed to find child node") );
421 wxSizerItem
*item
= node
->GetData();
423 if( item
->IsWindow() )
424 item
->GetWindow()->SetContainingSizer( NULL
);
427 m_children
.Erase( node
);
431 bool wxSizer::Detach( wxSizer
*sizer
)
433 wxASSERT_MSG( sizer
, _T("Detaching NULL sizer") );
435 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
438 wxSizerItem
*item
= node
->GetData();
440 if (item
->GetSizer() == sizer
)
444 m_children
.Erase( node
);
447 node
= node
->GetNext();
453 bool wxSizer::Detach( wxWindow
*window
)
455 wxASSERT_MSG( window
, _T("Detaching NULL window") );
457 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
460 wxSizerItem
*item
= node
->GetData();
462 if (item
->GetWindow() == window
)
464 item
->GetWindow()->SetContainingSizer( NULL
);
466 m_children
.Erase( node
);
469 node
= node
->GetNext();
475 bool wxSizer::Detach( int index
)
477 wxCHECK_MSG( index
>= 0 && (size_t)index
< m_children
.GetCount(),
479 _T("Detach index is out of range") );
481 wxSizerItemList::compatibility_iterator node
= m_children
.Item( index
);
483 wxCHECK_MSG( node
, false, _T("Failed to find child node") );
485 wxSizerItem
*item
= node
->GetData();
487 if( item
->IsSizer() )
489 else if( item
->IsWindow() )
490 item
->GetWindow()->SetContainingSizer( NULL
);
493 m_children
.Erase( node
);
497 void wxSizer::Clear( bool delete_windows
)
499 // First clear the ContainingSizer pointers
500 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
503 wxSizerItem
*item
= node
->GetData();
505 if (item
->IsWindow())
506 item
->GetWindow()->SetContainingSizer( NULL
);
507 node
= node
->GetNext();
510 // Destroy the windows if needed
514 // Now empty the list
515 WX_CLEAR_LIST(wxSizerItemList
, m_children
);
518 void wxSizer::DeleteWindows()
520 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
523 wxSizerItem
*item
= node
->GetData();
525 item
->DeleteWindows();
526 node
= node
->GetNext();
530 wxSize
wxSizer::Fit( wxWindow
*window
)
533 if (window
->IsTopLevel())
534 size
= FitSize( window
);
536 size
= GetMinWindowSize( window
);
538 window
->SetSize( size
);
543 void wxSizer::FitInside( wxWindow
*window
)
546 if (window
->IsTopLevel())
547 size
= VirtualFitSize( window
);
549 size
= GetMinClientSize( window
);
551 window
->SetVirtualSize( size
);
554 void wxSizer::Layout()
560 void wxSizer::SetSizeHints( wxWindow
*window
)
562 // Preserve the window's max size hints, but set the
563 // lower bound according to the sizer calculations.
565 wxSize size
= Fit( window
);
567 window
->SetSizeHints( size
.x
,
569 window
->GetMaxWidth(),
570 window
->GetMaxHeight() );
573 void wxSizer::SetVirtualSizeHints( wxWindow
*window
)
575 // Preserve the window's max size hints, but set the
576 // lower bound according to the sizer calculations.
579 wxSize
size( window
->GetVirtualSize() );
580 window
->SetVirtualSizeHints( size
.x
,
582 window
->GetMaxWidth(),
583 window
->GetMaxHeight() );
586 wxSize
wxSizer::GetMaxWindowSize( wxWindow
*window
) const
588 return window
->GetMaxSize();
591 wxSize
wxSizer::GetMinWindowSize( wxWindow
*window
)
593 wxSize
minSize( GetMinSize() );
594 wxSize
size( window
->GetSize() );
595 wxSize
client_size( window
->GetClientSize() );
597 return wxSize( minSize
.x
+size
.x
-client_size
.x
,
598 minSize
.y
+size
.y
-client_size
.y
);
601 // Return a window size that will fit within the screens dimensions
602 wxSize
wxSizer::FitSize( wxWindow
*window
)
604 wxSize size
= GetMinWindowSize( window
);
605 wxSize sizeMax
= GetMaxWindowSize( window
);
607 // Limit the size if sizeMax != wxDefaultSize
609 if ( size
.x
> sizeMax
.x
&& sizeMax
.x
!= -1 )
611 if ( size
.y
> sizeMax
.y
&& sizeMax
.y
!= -1 )
617 wxSize
wxSizer::GetMaxClientSize( wxWindow
*window
) const
619 wxSize
maxSize( window
->GetMaxSize() );
621 if( maxSize
!= wxDefaultSize
)
623 wxSize
size( window
->GetSize() );
624 wxSize
client_size( window
->GetClientSize() );
626 return wxSize( maxSize
.x
+ client_size
.x
- size
.x
,
627 maxSize
.y
+ client_size
.y
- size
.y
);
630 return wxDefaultSize
;
633 wxSize
wxSizer::GetMinClientSize( wxWindow
*WXUNUSED(window
) )
635 return GetMinSize(); // Already returns client size.
638 wxSize
wxSizer::VirtualFitSize( wxWindow
*window
)
640 wxSize size
= GetMinClientSize( window
);
641 wxSize sizeMax
= GetMaxClientSize( window
);
643 // Limit the size if sizeMax != wxDefaultSize
645 if ( size
.x
> sizeMax
.x
&& sizeMax
.x
!= -1 )
647 if ( size
.y
> sizeMax
.y
&& sizeMax
.y
!= -1 )
653 void wxSizer::SetDimension( int x
, int y
, int width
, int height
)
662 wxSize
wxSizer::GetMinSize()
664 wxSize
ret( CalcMin() );
665 if (ret
.x
< m_minSize
.x
) ret
.x
= m_minSize
.x
;
666 if (ret
.y
< m_minSize
.y
) ret
.y
= m_minSize
.y
;
670 void wxSizer::DoSetMinSize( int width
, int height
)
673 m_minSize
.y
= height
;
676 bool wxSizer::DoSetItemMinSize( wxWindow
*window
, int width
, int height
)
678 wxASSERT_MSG( window
, _T("SetMinSize for NULL window") );
680 // Is it our immediate child?
682 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
685 wxSizerItem
*item
= node
->GetData();
687 if (item
->GetWindow() == window
)
689 item
->SetInitSize( width
, height
);
692 node
= node
->GetNext();
695 // No? Search any subsizers we own then
697 node
= m_children
.GetFirst();
700 wxSizerItem
*item
= node
->GetData();
702 if ( item
->GetSizer() &&
703 item
->GetSizer()->DoSetItemMinSize( window
, width
, height
) )
705 // A child sizer found the requested windw, exit.
708 node
= node
->GetNext();
714 bool wxSizer::DoSetItemMinSize( wxSizer
*sizer
, int width
, int height
)
716 wxASSERT_MSG( sizer
, _T("SetMinSize for NULL sizer") );
718 // Is it our immediate child?
720 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
723 wxSizerItem
*item
= node
->GetData();
725 if (item
->GetSizer() == sizer
)
727 item
->GetSizer()->DoSetMinSize( width
, height
);
730 node
= node
->GetNext();
733 // No? Search any subsizers we own then
735 node
= m_children
.GetFirst();
738 wxSizerItem
*item
= node
->GetData();
740 if ( item
->GetSizer() &&
741 item
->GetSizer()->DoSetItemMinSize( sizer
, width
, height
) )
743 // A child found the requested sizer, exit.
746 node
= node
->GetNext();
752 bool wxSizer::DoSetItemMinSize( size_t index
, int width
, int height
)
754 wxSizerItemList::compatibility_iterator node
= m_children
.Item( index
);
756 wxCHECK_MSG( node
, false, _T("Failed to find child node") );
758 wxSizerItem
*item
= node
->GetData();
760 if (item
->GetSizer())
762 // Sizers contains the minimal size in them, if not calculated ...
763 item
->GetSizer()->DoSetMinSize( width
, height
);
767 // ... but the minimal size of spacers and windows in stored in them
768 item
->SetInitSize( width
, height
);
774 void wxSizer::Show( wxWindow
*window
, bool show
)
776 wxASSERT_MSG( window
, _T("Show for NULL window") );
778 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
781 wxSizerItem
*item
= node
->GetData();
783 if (item
->GetWindow() == window
)
788 node
= node
->GetNext();
792 void wxSizer::Show( wxSizer
*sizer
, bool show
)
794 wxASSERT_MSG( sizer
, _T("Show for NULL sizer") );
796 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
799 wxSizerItem
*item
= node
->GetData();
801 if (item
->GetSizer() == sizer
)
806 node
= node
->GetNext();
810 void wxSizer::Show( size_t index
, bool show
)
812 wxCHECK_RET( index
< m_children
.GetCount(),
813 _T("Show index is out of range") );
815 m_children
.Item( index
)->GetData()->Show( show
);
818 void wxSizer::ShowItems( bool show
)
820 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
823 node
->GetData()->Show( show
);
824 node
= node
->GetNext();
828 bool wxSizer::IsShown( wxWindow
*window
) const
830 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
833 wxSizerItem
*item
= node
->GetData();
835 if (item
->GetWindow() == window
)
837 return item
->IsShown();
839 node
= node
->GetNext();
842 wxFAIL_MSG( _T("IsShown failed to find sizer item") );
847 bool wxSizer::IsShown( wxSizer
*sizer
) const
849 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
852 wxSizerItem
*item
= node
->GetData();
854 if (item
->GetSizer() == sizer
)
856 return item
->IsShown();
858 node
= node
->GetNext();
861 wxFAIL_MSG( _T("IsShown failed to find sizer item") );
866 bool wxSizer::IsShown( size_t index
) const
868 wxCHECK_MSG( index
< m_children
.GetCount(),
870 _T("IsShown index is out of range") );
872 return m_children
.Item( index
)->GetData()->IsShown();
876 //---------------------------------------------------------------------------
878 //---------------------------------------------------------------------------
880 wxGridSizer::wxGridSizer( int rows
, int cols
, int vgap
, int hgap
)
888 wxGridSizer::wxGridSizer( int cols
, int vgap
, int hgap
)
896 int wxGridSizer::CalcRowsCols(int& nrows
, int& ncols
) const
898 int nitems
= m_children
.GetCount();
904 nrows
= (nitems
+ m_cols
- 1) / m_cols
;
908 ncols
= (nitems
+ m_rows
- 1) / m_rows
;
911 else // 0 columns, 0 rows?
913 wxFAIL_MSG( _T("grid sizer must have either rows or columns fixed") );
922 void wxGridSizer::RecalcSizes()
924 int nitems
, nrows
, ncols
;
925 if ( (nitems
= CalcRowsCols(nrows
, ncols
)) == 0 )
928 wxSize
sz( GetSize() );
929 wxPoint
pt( GetPosition() );
931 int w
= (sz
.x
- (ncols
- 1) * m_hgap
) / ncols
;
932 int h
= (sz
.y
- (nrows
- 1) * m_vgap
) / nrows
;
935 for (int c
= 0; c
< ncols
; c
++)
938 for (int r
= 0; r
< nrows
; r
++)
940 int i
= r
* ncols
+ c
;
943 wxSizerItemList::compatibility_iterator node
= m_children
.Item( i
);
945 wxASSERT_MSG( node
, _T("Failed to find SizerItemList node") );
947 SetItemBounds( node
->GetData(), x
, y
, w
, h
);
955 wxSize
wxGridSizer::CalcMin()
957 int nitems
, nrows
, ncols
;
958 if ( (nitems
= CalcRowsCols(nrows
, ncols
)) == 0 )
959 return wxSize(10, 10);
961 // Find the max width and height for any component
965 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
968 wxSizerItem
*item
= node
->GetData();
969 wxSize
sz( item
->CalcMin() );
971 w
= wxMax( w
, sz
.x
);
972 h
= wxMax( h
, sz
.y
);
974 node
= node
->GetNext();
977 return wxSize( ncols
* w
+ (ncols
-1) * m_hgap
,
978 nrows
* h
+ (nrows
-1) * m_vgap
);
981 void wxGridSizer::SetItemBounds( wxSizerItem
*item
, int x
, int y
, int w
, int h
)
984 wxSize
sz( item
->CalcMin() );
985 int flag
= item
->GetFlag();
987 if ((flag
& wxEXPAND
) || (flag
& wxSHAPED
))
993 if (flag
& wxALIGN_CENTER_HORIZONTAL
)
995 pt
.x
= x
+ (w
- sz
.x
) / 2;
997 else if (flag
& wxALIGN_RIGHT
)
999 pt
.x
= x
+ (w
- sz
.x
);
1002 if (flag
& wxALIGN_CENTER_VERTICAL
)
1004 pt
.y
= y
+ (h
- sz
.y
) / 2;
1006 else if (flag
& wxALIGN_BOTTOM
)
1008 pt
.y
= y
+ (h
- sz
.y
);
1012 item
->SetDimension(pt
, sz
);
1015 //---------------------------------------------------------------------------
1017 //---------------------------------------------------------------------------
1019 wxFlexGridSizer::wxFlexGridSizer( int rows
, int cols
, int vgap
, int hgap
)
1020 : wxGridSizer( rows
, cols
, vgap
, hgap
),
1021 m_flexDirection(wxBOTH
),
1022 m_growMode(wxFLEX_GROWMODE_SPECIFIED
)
1026 wxFlexGridSizer::wxFlexGridSizer( int cols
, int vgap
, int hgap
)
1027 : wxGridSizer( cols
, vgap
, hgap
),
1028 m_flexDirection(wxBOTH
),
1029 m_growMode(wxFLEX_GROWMODE_SPECIFIED
)
1033 wxFlexGridSizer::~wxFlexGridSizer()
1037 void wxFlexGridSizer::RecalcSizes()
1039 int nitems
, nrows
, ncols
;
1040 if ( (nitems
= CalcRowsCols(nrows
, ncols
)) == 0 )
1043 wxSize
sz( GetSize() );
1044 wxSize
minsz( CalcMin() );
1045 wxPoint
pt( GetPosition() );
1047 // what to do with the rows? by default, resize them proportionally
1048 if ( sz
.y
> minsz
.y
&& ( (m_flexDirection
& wxVERTICAL
) || (m_growMode
== wxFLEX_GROWMODE_SPECIFIED
) ) )
1050 int sum_proportions
= 0;
1051 int growable_space
= 0;
1054 for (idx
= 0; idx
< m_growableRows
.GetCount(); idx
++)
1056 // Since the number of rows/columns can change as items are inserted/deleted, we need
1057 // to verify at runtime that the requested growable rows/columns are still valid.
1058 if (m_growableRows
[idx
] >= nrows
)
1060 // If all items in a row/column are hidden, that row/column will have a dimension of -1.
1061 // This causes the row/column to be hidden completely.
1062 if (m_rowHeights
[ m_growableRows
[idx
] ] == -1)
1064 sum_proportions
+= m_growableRowsProportions
[idx
];
1065 growable_space
+= m_rowHeights
[ m_growableRows
[idx
] ];
1071 for (idx
= 0; idx
< m_growableRows
.GetCount(); idx
++)
1073 if (m_growableRows
[idx
] >= nrows
)
1075 if (m_rowHeights
[ m_growableRows
[idx
] ] == -1)
1076 m_rowHeights
[ m_growableRows
[idx
] ] = 0;
1079 int delta
= (sz
.y
- minsz
.y
);
1080 if (sum_proportions
== 0)
1081 delta
= (delta
/num
) + m_rowHeights
[ m_growableRows
[idx
] ];
1083 delta
= ((delta
+growable_space
)*m_growableRowsProportions
[idx
]) / sum_proportions
;
1084 m_rowHeights
[ m_growableRows
[idx
] ] = delta
;
1089 else if ( (m_growMode
== wxFLEX_GROWMODE_ALL
) && (sz
.y
> minsz
.y
) )
1091 // rounding problem?
1092 for ( int row
= 0; row
< nrows
; ++row
)
1093 m_rowHeights
[ row
] = sz
.y
/ nrows
;
1096 // the same logic as above but for the columns
1097 if ( sz
.x
> minsz
.x
&& ( (m_flexDirection
& wxHORIZONTAL
) || (m_growMode
== wxFLEX_GROWMODE_SPECIFIED
) ) )
1099 int sum_proportions
= 0;
1100 int growable_space
= 0;
1103 for (idx
= 0; idx
< m_growableCols
.GetCount(); idx
++)
1105 // Since the number of rows/columns can change as items are inserted/deleted, we need
1106 // to verify at runtime that the requested growable rows/columns are still valid.
1107 if (m_growableCols
[idx
] >= ncols
)
1109 // If all items in a row/column are hidden, that row/column will have a dimension of -1.
1110 // This causes the column to be hidden completely.
1111 if (m_colWidths
[ m_growableCols
[idx
] ] == -1)
1113 sum_proportions
+= m_growableColsProportions
[idx
];
1114 // wtb 5/12/02 bugfix - was m_ColWidths[idx]!!
1115 growable_space
+= m_colWidths
[ m_growableCols
[idx
] ];
1121 for (idx
= 0; idx
< m_growableCols
.GetCount(); idx
++)
1123 if (m_growableCols
[idx
] >= ncols
)
1125 if (m_colWidths
[ m_growableCols
[idx
] ] == -1)
1126 m_colWidths
[ m_growableCols
[idx
] ] = 0;
1129 int delta
= (sz
.x
- minsz
.x
);
1130 if (sum_proportions
== 0)
1131 delta
= (delta
/num
) + m_colWidths
[ m_growableCols
[idx
] ];
1133 delta
= ((delta
+growable_space
)*m_growableColsProportions
[idx
])/sum_proportions
;
1134 m_colWidths
[ m_growableCols
[idx
] ] = delta
;
1139 else if ( (m_growMode
== wxFLEX_GROWMODE_ALL
) && (sz
.x
> minsz
.x
) )
1141 for ( int col
=0; col
< ncols
; ++col
)
1142 m_colWidths
[ col
] = sz
.x
/ ncols
;
1145 sz
= wxSize( pt
.x
+ sz
.x
, pt
.y
+ sz
.y
);
1148 for (int c
= 0; c
< ncols
; c
++)
1151 for (int r
= 0; r
< nrows
; r
++)
1153 int i
= r
* ncols
+ c
;
1156 wxSizerItemList::compatibility_iterator node
= m_children
.Item( i
);
1158 wxASSERT_MSG( node
, _T("Failed to find node") );
1160 int w
= wxMax( 0, wxMin( m_colWidths
[c
], sz
.x
- x
) );
1161 int h
= wxMax( 0, wxMin( m_rowHeights
[r
], sz
.y
- y
) );
1163 SetItemBounds( node
->GetData(), x
, y
, w
, h
);
1165 y
= y
+ m_rowHeights
[r
] + m_vgap
;
1167 x
= x
+ m_colWidths
[c
] + m_hgap
;
1171 wxSize
wxFlexGridSizer::CalcMin()
1177 // Number of rows/columns can change as items are added or removed.
1178 if ( !CalcRowsCols(nrows
, ncols
) )
1179 return wxSize(10, 10);
1181 m_rowHeights
.SetCount(nrows
);
1182 m_colWidths
.SetCount(ncols
);
1184 // We have to recalcuate the sizes in case an item has wxADJUST_MINSIZE, has changed
1185 // minimum size since the previous layout, or has been hidden using wxSizer::Show().
1186 // If all the items in a row/column are hidden, the final dimension of the row/column
1187 // will be -1, indicating that the column itself is hidden.
1188 for( s
= m_rowHeights
.GetCount(), i
= 0; i
< s
; ++i
)
1189 m_rowHeights
[ i
] = -1;
1190 for( s
= m_colWidths
.GetCount(), i
= 0; i
< s
; ++i
)
1191 m_colWidths
[ i
] = -1;
1193 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
1198 wxSizerItem
*item
= node
->GetData();
1199 if ( item
->IsShown() )
1201 wxSize
sz( item
->CalcMin() );
1202 int row
= i
/ ncols
;
1203 int col
= i
% ncols
;
1205 m_rowHeights
[ row
] = wxMax( wxMax( 0, sz
.y
), m_rowHeights
[ row
] );
1206 m_colWidths
[ col
] = wxMax( wxMax( 0, sz
.x
), m_colWidths
[ col
] );
1209 node
= node
->GetNext();
1213 // the logic above works when we resize flexibly in both directions but
1214 // maybe this is not the case
1215 if ( m_flexDirection
!= wxBOTH
)
1217 // select the array corresponding to the direction in which we do *not*
1219 wxArrayInt
& array
= m_flexDirection
== wxVERTICAL
? m_colWidths
1222 const int count
= array
.GetCount();
1224 // find the largest value in this array
1226 for ( n
= 0; n
< count
; ++n
)
1228 if ( array
[n
] > largest
)
1232 // and now fill it with the largest value
1233 for ( n
= 0; n
< count
; ++n
)
1239 // Sum total minimum size, including gaps between rows/columns.
1240 // -1 is used as a magic number meaning empty column.
1242 for (int col
= 0; col
< ncols
; col
++)
1243 if ( m_colWidths
[ col
] != -1 )
1244 width
+= m_colWidths
[ col
] + ( col
== ncols
-1 ? 0 : m_hgap
);
1247 for (int row
= 0; row
< nrows
; row
++)
1248 if ( m_rowHeights
[ row
] != -1 )
1249 height
+= m_rowHeights
[ row
] + ( row
== nrows
-1 ? 0 : m_vgap
);
1251 return wxSize( width
, height
);
1254 void wxFlexGridSizer::AddGrowableRow( size_t idx
, int proportion
)
1256 m_growableRows
.Add( idx
);
1257 m_growableRowsProportions
.Add( proportion
);
1260 void wxFlexGridSizer::RemoveGrowableRow( size_t WXUNUSED(idx
) )
1264 void wxFlexGridSizer::AddGrowableCol( size_t idx
, int proportion
)
1266 m_growableCols
.Add( idx
);
1267 m_growableColsProportions
.Add( proportion
);
1270 void wxFlexGridSizer::RemoveGrowableCol( size_t WXUNUSED(idx
) )
1274 //---------------------------------------------------------------------------
1276 //---------------------------------------------------------------------------
1278 wxBoxSizer::wxBoxSizer( int orient
)
1279 : m_orient( orient
)
1283 void wxBoxSizer::RecalcSizes()
1285 if (m_children
.GetCount() == 0)
1292 if (m_orient
== wxHORIZONTAL
)
1294 delta
= (m_size
.x
- m_fixedWidth
) / m_stretchable
;
1295 extra
= (m_size
.x
- m_fixedWidth
) % m_stretchable
;
1299 delta
= (m_size
.y
- m_fixedHeight
) / m_stretchable
;
1300 extra
= (m_size
.y
- m_fixedHeight
) % m_stretchable
;
1304 wxPoint
pt( m_position
);
1306 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
1309 wxSizerItem
*item
= node
->GetData();
1311 if (item
->IsShown())
1314 if (item
->GetProportion())
1315 weight
= item
->GetProportion();
1317 wxSize
size( item
->CalcMin() );
1319 if (m_orient
== wxVERTICAL
)
1321 wxCoord height
= size
.y
;
1322 if (item
->GetProportion())
1324 height
= (delta
* weight
) + extra
;
1325 extra
= 0; // only the first item will get the remainder as extra size
1328 wxPoint
child_pos( pt
);
1329 wxSize
child_size( wxSize( size
.x
, height
) );
1331 if (item
->GetFlag() & (wxEXPAND
| wxSHAPED
))
1332 child_size
.x
= m_size
.x
;
1333 else if (item
->GetFlag() & wxALIGN_RIGHT
)
1334 child_pos
.x
+= m_size
.x
- size
.x
;
1335 else if (item
->GetFlag() & (wxCENTER
| wxALIGN_CENTER_HORIZONTAL
))
1336 // XXX wxCENTER is added for backward compatibility;
1337 // wxALIGN_CENTER should be used in new code
1338 child_pos
.x
+= (m_size
.x
- size
.x
) / 2;
1340 item
->SetDimension( child_pos
, child_size
);
1346 wxCoord width
= size
.x
;
1347 if (item
->GetProportion())
1349 width
= (delta
* weight
) + extra
;
1350 extra
= 0; // only the first item will get the remainder as extra size
1353 wxPoint
child_pos( pt
);
1354 wxSize
child_size( wxSize(width
, size
.y
) );
1356 if (item
->GetFlag() & (wxEXPAND
| wxSHAPED
))
1357 child_size
.y
= m_size
.y
;
1358 else if (item
->GetFlag() & wxALIGN_BOTTOM
)
1359 child_pos
.y
+= m_size
.y
- size
.y
;
1360 else if (item
->GetFlag() & (wxCENTER
| wxALIGN_CENTER_VERTICAL
))
1361 // XXX wxCENTER is added for backward compatibility;
1362 // wxALIGN_CENTER should be used in new code
1363 child_pos
.y
+= (m_size
.y
- size
.y
) / 2;
1365 item
->SetDimension( child_pos
, child_size
);
1371 node
= node
->GetNext();
1375 wxSize
wxBoxSizer::CalcMin()
1377 if (m_children
.GetCount() == 0)
1378 return wxSize(10,10);
1386 // Find how long each stretch unit needs to be
1387 int stretchSize
= 1;
1388 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
1392 wxSizerItem
*item
= node
->GetData();
1394 if (item
->IsShown() && item
->GetProportion() != 0)
1396 int stretch
= item
->GetProportion();
1397 wxSize
size( item
->CalcMin() );
1399 // Integer division rounded up is (a + b - 1) / b
1400 if (m_orient
== wxHORIZONTAL
)
1401 sizePerStretch
= ( size
.x
+ stretch
- 1 ) / stretch
;
1403 sizePerStretch
= ( size
.y
+ stretch
- 1 ) / stretch
;
1404 if (sizePerStretch
> stretchSize
)
1405 stretchSize
= sizePerStretch
;
1407 node
= node
->GetNext();
1410 // Calculate overall minimum size
1411 node
= m_children
.GetFirst();
1414 wxSizerItem
*item
= node
->GetData();
1416 if (item
->IsShown())
1418 m_stretchable
+= item
->GetProportion();
1420 wxSize
size( item
->CalcMin() );
1421 if (item
->GetProportion() != 0)
1423 if (m_orient
== wxHORIZONTAL
)
1424 size
.x
= stretchSize
* item
->GetProportion();
1426 size
.y
= stretchSize
* item
->GetProportion();
1429 if (m_orient
== wxHORIZONTAL
)
1431 m_minWidth
+= size
.x
;
1432 m_minHeight
= wxMax( m_minHeight
, size
.y
);
1436 m_minHeight
+= size
.y
;
1437 m_minWidth
= wxMax( m_minWidth
, size
.x
);
1440 if (item
->GetProportion() == 0)
1442 if (m_orient
== wxVERTICAL
)
1444 m_fixedHeight
+= size
.y
;
1445 m_fixedWidth
= wxMax( m_fixedWidth
, size
.x
);
1449 m_fixedWidth
+= size
.x
;
1450 m_fixedHeight
= wxMax( m_fixedHeight
, size
.y
);
1454 node
= node
->GetNext();
1457 return wxSize( m_minWidth
, m_minHeight
);
1460 //---------------------------------------------------------------------------
1462 //---------------------------------------------------------------------------
1466 wxStaticBoxSizer::wxStaticBoxSizer( wxStaticBox
*box
, int orient
)
1467 : wxBoxSizer( orient
)
1468 , m_staticBox( box
)
1470 wxASSERT_MSG( box
, wxT("wxStaticBoxSizer needs a static box") );
1473 static void GetStaticBoxBorders( wxStaticBox
*box
,
1477 // this has to be done platform by platform as there is no way to
1478 // guess the thickness of a wxStaticBox border
1480 box
->GetBordersForSizer(borderTop
,borderOther
);
1481 #else // __WXCOCOA__
1483 if ( box
->GetLabel().IsEmpty() )
1487 *borderTop
= box
->GetCharHeight();
1490 #endif // __WXCOCOA__
1493 void wxStaticBoxSizer::RecalcSizes()
1495 int top_border
, other_border
;
1496 GetStaticBoxBorders(m_staticBox
, &top_border
, &other_border
);
1498 m_staticBox
->SetSize( m_position
.x
, m_position
.y
, m_size
.x
, m_size
.y
);
1500 wxPoint
old_pos( m_position
);
1501 m_position
.x
+= other_border
;
1502 m_position
.y
+= top_border
;
1503 wxSize
old_size( m_size
);
1504 m_size
.x
-= 2*other_border
;
1505 m_size
.y
-= top_border
+ other_border
;
1507 wxBoxSizer::RecalcSizes();
1509 m_position
= old_pos
;
1513 wxSize
wxStaticBoxSizer::CalcMin()
1515 int top_border
, other_border
;
1516 GetStaticBoxBorders(m_staticBox
, &top_border
, &other_border
);
1518 wxSize
ret( wxBoxSizer::CalcMin() );
1519 ret
.x
+= 2*other_border
;
1520 ret
.y
+= other_border
+ top_border
;
1525 #endif // wxUSE_STATBOX
1527 //---------------------------------------------------------------------------
1529 //---------------------------------------------------------------------------
1533 wxNotebookSizer::wxNotebookSizer( wxNotebook
*nb
)
1536 wxASSERT_MSG( nb
, wxT("wxNotebookSizer needs a notebook") );
1539 void wxNotebookSizer::RecalcSizes()
1541 m_notebook
->SetSize( m_position
.x
, m_position
.y
, m_size
.x
, m_size
.y
);
1544 wxSize
wxNotebookSizer::CalcMin()
1546 wxSize sizeBorder
= m_notebook
->CalcSizeFromPage(wxSize(0, 0));
1551 if (m_notebook
->GetChildren().GetCount() == 0)
1553 return wxSize(sizeBorder
.x
+ 10, sizeBorder
.y
+ 10);
1559 wxWindowList::compatibility_iterator node
= m_notebook
->GetChildren().GetFirst();
1562 wxWindow
*item
= node
->GetData();
1563 wxSizer
*itemsizer
= item
->GetSizer();
1567 wxSize
subsize( itemsizer
->CalcMin() );
1569 if (subsize
.x
> maxX
)
1571 if (subsize
.y
> maxY
)
1575 node
= node
->GetNext();
1578 return wxSize( maxX
, maxY
) + sizeBorder
;
1581 #endif // wxUSE_NOTEBOOK