1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: provide new wxSizer class for layout
4 // Author: Robert Roebling and Robin Dunn, contributions by
5 // Dirk Holtwick, Ron Lee
6 // Modified by: Ron Lee
9 // Copyright: (c) Robin Dunn, Robert Roebling
10 // Licence: wxWindows licence
11 /////////////////////////////////////////////////////////////////////////////
13 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
14 #pragma implementation "sizer.h"
17 // For compilers that support precompilation, includes "wx.h".
18 #include "wx/wxprec.h"
26 #include "wx/statbox.h"
27 #include "wx/listimpl.cpp"
28 #if WXWIN_COMPATIBILITY_2_4
29 #include "wx/notebook.h"
33 # include "wx/mac/uma.h"
36 //---------------------------------------------------------------------------
38 IMPLEMENT_CLASS(wxSizerItem
, wxObject
)
39 IMPLEMENT_CLASS(wxSizer
, wxObject
)
40 IMPLEMENT_CLASS(wxGridSizer
, wxSizer
)
41 IMPLEMENT_CLASS(wxFlexGridSizer
, wxGridSizer
)
42 IMPLEMENT_CLASS(wxBoxSizer
, wxSizer
)
44 IMPLEMENT_CLASS(wxStaticBoxSizer
, wxBoxSizer
)
47 WX_DEFINE_EXPORTED_LIST( wxSizerItemList
);
81 //---------------------------------------------------------------------------
83 //---------------------------------------------------------------------------
85 wxSizerItem::wxSizerItem( int width
, int height
, int proportion
, int flag
, int border
, wxObject
* userData
)
88 , m_size( wxSize( width
, height
) ) // size is set directly
89 , m_minSize( m_size
) // minimal size is the initial size
90 , m_proportion( proportion
)
94 , m_userData( userData
)
99 wxSizerItem::wxSizerItem( wxWindow
*window
, int proportion
, int flag
, int border
, wxObject
* userData
)
102 , m_minSize( window
->GetSize() ) // minimal size is the initial size
103 , m_proportion( proportion
)
107 , m_userData( userData
)
109 // aspect ratio calculated from initial size
110 SetRatio( m_minSize
);
112 // m_size is calculated later
115 wxSizerItem::wxSizerItem( wxSizer
*sizer
, int proportion
, int flag
, int border
, wxObject
* userData
)
118 , m_proportion( proportion
)
123 , m_userData( userData
)
125 // m_minSize is calculated later
126 // m_size is calculated later
129 wxSizerItem::wxSizerItem()
141 wxSizerItem::~wxSizerItem()
147 m_window
->SetContainingSizer(NULL
);
149 else // we must be a sizer
156 wxSize
wxSizerItem::GetSize() const
160 ret
= m_sizer
->GetSize();
163 ret
= m_window
->GetSize();
170 if (m_flag
& wxNORTH
)
172 if (m_flag
& wxSOUTH
)
178 wxSize
wxSizerItem::CalcMin()
183 ret
= m_sizer
->GetMinSize();
185 // if we have to preserve aspect ratio _AND_ this is
186 // the first-time calculation, consider ret to be initial size
187 if ((m_flag
& wxSHAPED
) && !m_ratio
)
192 if ( IsWindow() && !(m_flag
& wxFIXED_MINSIZE
) )
194 // Since the size of the window may change during runtime, we
195 // should use the current minimal size. If there is a MinSize,
196 // use it, otherwise use the BestSize.
197 wxSize min
= m_window
->GetMinSize();
198 if (min
.x
== -1 || min
.y
== -1)
200 wxSize best
= m_window
->GetBestSize();
201 if (min
.x
== -1) min
.x
= best
.x
;
202 if (min
.y
== -1) min
.y
= best
.y
;
214 if (m_flag
& wxNORTH
)
216 if (m_flag
& wxSOUTH
)
222 void wxSizerItem::SetDimension( wxPoint pos
, wxSize size
)
224 if (m_flag
& wxSHAPED
)
226 // adjust aspect ratio
227 int rwidth
= (int) (size
.y
* m_ratio
);
231 int rheight
= (int) (size
.x
/ m_ratio
);
232 // add vertical space
233 if (m_flag
& wxALIGN_CENTER_VERTICAL
)
234 pos
.y
+= (size
.y
- rheight
) / 2;
235 else if (m_flag
& wxALIGN_BOTTOM
)
236 pos
.y
+= (size
.y
- rheight
);
237 // use reduced dimensions
240 else if (rwidth
< size
.x
)
242 // add horizontal space
243 if (m_flag
& wxALIGN_CENTER_HORIZONTAL
)
244 pos
.x
+= (size
.x
- rwidth
) / 2;
245 else if (m_flag
& wxALIGN_RIGHT
)
246 pos
.x
+= (size
.x
- rwidth
);
251 // This is what GetPosition() returns. Since we calculate
252 // borders afterwards, GetPosition() will be the left/top
253 // corner of the surrounding border.
265 if (m_flag
& wxNORTH
)
270 if (m_flag
& wxSOUTH
)
276 m_sizer
->SetDimension( pos
.x
, pos
.y
, size
.x
, size
.y
);
279 m_window
->SetSize( pos
.x
, pos
.y
, size
.x
, size
.y
, wxSIZE_ALLOW_MINUS_ONE
);
284 void wxSizerItem::DeleteWindows()
293 m_sizer
->DeleteWindows();
296 bool wxSizerItem::IsWindow() const
298 return (m_window
!= NULL
);
301 bool wxSizerItem::IsSizer() const
303 return (m_sizer
!= NULL
);
306 bool wxSizerItem::IsSpacer() const
308 return (m_window
== NULL
) && (m_sizer
== NULL
);
311 void wxSizerItem::Show( bool show
)
316 m_window
->Show( show
);
318 m_sizer
->ShowItems( show
);
320 // ... nothing else to do to hide/show spacers
323 void wxSizerItem::SetOption( int option
)
325 SetProportion( option
);
328 int wxSizerItem::GetOption() const
330 return GetProportion();
334 //---------------------------------------------------------------------------
336 //---------------------------------------------------------------------------
339 : m_minSize( wxSize( 0, 0 ) )
345 WX_CLEAR_LIST(wxSizerItemList
, m_children
);
348 void wxSizer::Add( wxWindow
*window
, int proportion
, int flag
, int border
, wxObject
* userData
)
350 m_children
.Append( new wxSizerItem( window
, proportion
, flag
, border
, userData
) );
351 window
->SetContainingSizer( this );
354 void wxSizer::Add( wxSizer
*sizer
, int proportion
, int flag
, int border
, wxObject
* userData
)
356 m_children
.Append( new wxSizerItem( sizer
, proportion
, flag
, border
, userData
) );
359 void wxSizer::Add( int width
, int height
, int proportion
, int flag
, int border
, wxObject
* userData
)
361 m_children
.Append( new wxSizerItem( width
, height
, proportion
, flag
, border
, userData
) );
364 void wxSizer::Add( wxSizerItem
*item
)
366 m_children
.Append( item
);
368 if( item
->GetWindow() )
369 item
->GetWindow()->SetContainingSizer( this );
372 void wxSizer::Prepend( wxWindow
*window
, int proportion
, int flag
, int border
, wxObject
* userData
)
374 m_children
.Insert( new wxSizerItem( window
, proportion
, flag
, border
, userData
) );
375 window
->SetContainingSizer( this );
378 void wxSizer::Prepend( wxSizer
*sizer
, int proportion
, int flag
, int border
, wxObject
* userData
)
380 m_children
.Insert( new wxSizerItem( sizer
, proportion
, flag
, border
, userData
) );
383 void wxSizer::Prepend( int width
, int height
, int proportion
, int flag
, int border
, wxObject
* userData
)
385 m_children
.Insert( new wxSizerItem( width
, height
, proportion
, flag
, border
, userData
) );
388 void wxSizer::Prepend( wxSizerItem
*item
)
390 m_children
.Insert( item
);
392 if( item
->GetWindow() )
393 item
->GetWindow()->SetContainingSizer( this );
396 void wxSizer::Insert( size_t index
,
403 m_children
.Insert( index
,
404 new wxSizerItem( window
, proportion
, flag
, border
, userData
) );
405 window
->SetContainingSizer( this );
408 void wxSizer::Insert( size_t index
,
415 m_children
.Insert( index
,
416 new wxSizerItem( sizer
, proportion
, flag
, border
, userData
) );
419 void wxSizer::Insert( size_t index
,
427 m_children
.Insert( index
,
428 new wxSizerItem( width
, height
, proportion
, flag
, border
, userData
) );
431 void wxSizer::Insert( size_t index
, wxSizerItem
*item
)
433 m_children
.Insert( index
, item
);
435 if( item
->GetWindow() )
436 item
->GetWindow()->SetContainingSizer( this );
439 bool wxSizer::Remove( wxWindow
*window
)
441 return Detach( window
);
444 bool wxSizer::Remove( wxSizer
*sizer
)
446 wxASSERT_MSG( sizer
, _T("Removing NULL sizer") );
448 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
451 wxSizerItem
*item
= node
->GetData();
453 if (item
->GetSizer() == sizer
)
456 m_children
.Erase( node
);
460 node
= node
->GetNext();
466 bool wxSizer::Remove( int index
)
468 wxCHECK_MSG( index
>= 0 && (size_t)index
< m_children
.GetCount(),
470 _T("Remove index is out of range") );
472 wxSizerItemList::compatibility_iterator node
= m_children
.Item( index
);
474 wxCHECK_MSG( node
, false, _T("Failed to find child node") );
476 wxSizerItem
*item
= node
->GetData();
478 if( item
->IsWindow() )
479 item
->GetWindow()->SetContainingSizer( NULL
);
482 m_children
.Erase( node
);
486 bool wxSizer::Detach( wxSizer
*sizer
)
488 wxASSERT_MSG( sizer
, _T("Detaching NULL sizer") );
490 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
493 wxSizerItem
*item
= node
->GetData();
495 if (item
->GetSizer() == sizer
)
499 m_children
.Erase( node
);
502 node
= node
->GetNext();
508 bool wxSizer::Detach( wxWindow
*window
)
510 wxASSERT_MSG( window
, _T("Detaching NULL window") );
512 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
515 wxSizerItem
*item
= node
->GetData();
517 if (item
->GetWindow() == window
)
519 item
->GetWindow()->SetContainingSizer( NULL
);
521 m_children
.Erase( node
);
524 node
= node
->GetNext();
530 bool wxSizer::Detach( int index
)
532 wxCHECK_MSG( index
>= 0 && (size_t)index
< m_children
.GetCount(),
534 _T("Detach index is out of range") );
536 wxSizerItemList::compatibility_iterator node
= m_children
.Item( index
);
538 wxCHECK_MSG( node
, false, _T("Failed to find child node") );
540 wxSizerItem
*item
= node
->GetData();
542 if( item
->IsSizer() )
544 else if( item
->IsWindow() )
545 item
->GetWindow()->SetContainingSizer( NULL
);
548 m_children
.Erase( node
);
552 void wxSizer::Clear( bool delete_windows
)
554 // First clear the ContainingSizer pointers
555 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
558 wxSizerItem
*item
= node
->GetData();
560 if (item
->IsWindow())
561 item
->GetWindow()->SetContainingSizer( NULL
);
562 node
= node
->GetNext();
565 // Destroy the windows if needed
569 // Now empty the list
570 WX_CLEAR_LIST(wxSizerItemList
, m_children
);
573 void wxSizer::DeleteWindows()
575 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
578 wxSizerItem
*item
= node
->GetData();
580 item
->DeleteWindows();
581 node
= node
->GetNext();
585 wxSize
wxSizer::Fit( wxWindow
*window
)
587 wxSize
size(window
->IsTopLevel() ? FitSize(window
)
588 : GetMinWindowSize(window
));
590 window
->SetSize( size
);
595 void wxSizer::FitInside( wxWindow
*window
)
598 if (window
->IsTopLevel())
599 size
= VirtualFitSize( window
);
601 size
= GetMinClientSize( window
);
603 window
->SetVirtualSize( size
);
606 void wxSizer::Layout()
612 void wxSizer::SetSizeHints( wxWindow
*window
)
614 // Preserve the window's max size hints, but set the
615 // lower bound according to the sizer calculations.
617 wxSize size
= Fit( window
);
619 window
->SetSizeHints( size
.x
,
621 window
->GetMaxWidth(),
622 window
->GetMaxHeight() );
625 void wxSizer::SetVirtualSizeHints( wxWindow
*window
)
627 // Preserve the window's max size hints, but set the
628 // lower bound according to the sizer calculations.
631 wxSize
size( window
->GetVirtualSize() );
632 window
->SetVirtualSizeHints( size
.x
,
634 window
->GetMaxWidth(),
635 window
->GetMaxHeight() );
638 wxSize
wxSizer::GetMaxWindowSize( wxWindow
*window
) const
640 return window
->GetMaxSize();
643 wxSize
wxSizer::GetMinWindowSize( wxWindow
*window
)
645 wxSize
minSize( GetMinSize() );
646 wxSize
size( window
->GetSize() );
647 wxSize
client_size( window
->GetClientSize() );
649 return wxSize( minSize
.x
+size
.x
-client_size
.x
,
650 minSize
.y
+size
.y
-client_size
.y
);
653 // TODO on mac we need a function that determines how much free space this
654 // min size contains, in order to make sure that we have 20 pixels of free
655 // space around the controls
657 // Return a window size that will fit within the screens dimensions
658 wxSize
wxSizer::FitSize( wxWindow
*window
)
660 wxSize size
= GetMinWindowSize( window
);
661 wxSize sizeMax
= GetMaxWindowSize( window
);
663 // Limit the size if sizeMax != wxDefaultSize
665 if ( size
.x
> sizeMax
.x
&& sizeMax
.x
!= -1 )
667 if ( size
.y
> sizeMax
.y
&& sizeMax
.y
!= -1 )
673 wxSize
wxSizer::GetMaxClientSize( wxWindow
*window
) const
675 wxSize
maxSize( window
->GetMaxSize() );
677 if( maxSize
!= wxDefaultSize
)
679 wxSize
size( window
->GetSize() );
680 wxSize
client_size( window
->GetClientSize() );
682 return wxSize( maxSize
.x
+ client_size
.x
- size
.x
,
683 maxSize
.y
+ client_size
.y
- size
.y
);
686 return wxDefaultSize
;
689 wxSize
wxSizer::GetMinClientSize( wxWindow
*WXUNUSED(window
) )
691 return GetMinSize(); // Already returns client size.
694 wxSize
wxSizer::VirtualFitSize( wxWindow
*window
)
696 wxSize size
= GetMinClientSize( window
);
697 wxSize sizeMax
= GetMaxClientSize( window
);
699 // Limit the size if sizeMax != wxDefaultSize
701 if ( size
.x
> sizeMax
.x
&& sizeMax
.x
!= -1 )
703 if ( size
.y
> sizeMax
.y
&& sizeMax
.y
!= -1 )
709 void wxSizer::SetDimension( int x
, int y
, int width
, int height
)
718 wxSize
wxSizer::GetMinSize()
720 wxSize
ret( CalcMin() );
721 if (ret
.x
< m_minSize
.x
) ret
.x
= m_minSize
.x
;
722 if (ret
.y
< m_minSize
.y
) ret
.y
= m_minSize
.y
;
726 void wxSizer::DoSetMinSize( int width
, int height
)
729 m_minSize
.y
= height
;
732 bool wxSizer::DoSetItemMinSize( wxWindow
*window
, int width
, int height
)
734 wxASSERT_MSG( window
, _T("SetMinSize for NULL window") );
736 // Is it our immediate child?
738 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
741 wxSizerItem
*item
= node
->GetData();
743 if (item
->GetWindow() == window
)
745 item
->SetMinSize( width
, height
);
748 node
= node
->GetNext();
751 // No? Search any subsizers we own then
753 node
= m_children
.GetFirst();
756 wxSizerItem
*item
= node
->GetData();
758 if ( item
->GetSizer() &&
759 item
->GetSizer()->DoSetItemMinSize( window
, width
, height
) )
761 // A child sizer found the requested windw, exit.
764 node
= node
->GetNext();
770 bool wxSizer::DoSetItemMinSize( wxSizer
*sizer
, int width
, int height
)
772 wxASSERT_MSG( sizer
, _T("SetMinSize for NULL sizer") );
774 // Is it our immediate child?
776 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
779 wxSizerItem
*item
= node
->GetData();
781 if (item
->GetSizer() == sizer
)
783 item
->GetSizer()->DoSetMinSize( width
, height
);
786 node
= node
->GetNext();
789 // No? Search any subsizers we own then
791 node
= m_children
.GetFirst();
794 wxSizerItem
*item
= node
->GetData();
796 if ( item
->GetSizer() &&
797 item
->GetSizer()->DoSetItemMinSize( sizer
, width
, height
) )
799 // A child found the requested sizer, exit.
802 node
= node
->GetNext();
808 bool wxSizer::DoSetItemMinSize( size_t index
, int width
, int height
)
810 wxSizerItemList::compatibility_iterator node
= m_children
.Item( index
);
812 wxCHECK_MSG( node
, false, _T("Failed to find child node") );
814 wxSizerItem
*item
= node
->GetData();
816 if (item
->GetSizer())
818 // Sizers contains the minimal size in them, if not calculated ...
819 item
->GetSizer()->DoSetMinSize( width
, height
);
823 // ... but the minimal size of spacers and windows in stored in them
824 item
->SetMinSize( width
, height
);
830 void wxSizer::Show( wxWindow
*window
, bool show
)
832 wxASSERT_MSG( window
, _T("Show for NULL window") );
834 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
837 wxSizerItem
*item
= node
->GetData();
839 if (item
->GetWindow() == window
)
844 node
= node
->GetNext();
848 void wxSizer::Show( wxSizer
*sizer
, bool show
)
850 wxASSERT_MSG( sizer
, _T("Show for NULL sizer") );
852 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
855 wxSizerItem
*item
= node
->GetData();
857 if (item
->GetSizer() == sizer
)
862 node
= node
->GetNext();
866 void wxSizer::Show( size_t index
, bool show
)
868 wxCHECK_RET( index
< m_children
.GetCount(),
869 _T("Show index is out of range") );
871 m_children
.Item( index
)->GetData()->Show( show
);
874 void wxSizer::ShowItems( bool show
)
876 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
879 node
->GetData()->Show( show
);
880 node
= node
->GetNext();
884 bool wxSizer::IsShown( wxWindow
*window
) const
886 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
889 wxSizerItem
*item
= node
->GetData();
891 if (item
->GetWindow() == window
)
893 return item
->IsShown();
895 node
= node
->GetNext();
898 wxFAIL_MSG( _T("IsShown failed to find sizer item") );
903 bool wxSizer::IsShown( wxSizer
*sizer
) const
905 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
908 wxSizerItem
*item
= node
->GetData();
910 if (item
->GetSizer() == sizer
)
912 return item
->IsShown();
914 node
= node
->GetNext();
917 wxFAIL_MSG( _T("IsShown failed to find sizer item") );
922 bool wxSizer::IsShown( size_t index
) const
924 wxCHECK_MSG( index
< m_children
.GetCount(),
926 _T("IsShown index is out of range") );
928 return m_children
.Item( index
)->GetData()->IsShown();
932 //---------------------------------------------------------------------------
934 //---------------------------------------------------------------------------
936 wxGridSizer::wxGridSizer( int rows
, int cols
, int vgap
, int hgap
)
942 if (m_rows
== 0 && m_cols
== 0)
946 wxGridSizer::wxGridSizer( int cols
, int vgap
, int hgap
)
952 if (m_rows
== 0 && m_cols
== 0)
956 int wxGridSizer::CalcRowsCols(int& nrows
, int& ncols
) const
958 int nitems
= m_children
.GetCount();
964 nrows
= (nitems
+ m_cols
- 1) / m_cols
;
968 ncols
= (nitems
+ m_rows
- 1) / m_rows
;
971 else // 0 columns, 0 rows?
973 wxFAIL_MSG( _T("grid sizer must have either rows or columns fixed") );
982 void wxGridSizer::RecalcSizes()
984 int nitems
, nrows
, ncols
;
985 if ( (nitems
= CalcRowsCols(nrows
, ncols
)) == 0 )
988 wxSize
sz( GetSize() );
989 wxPoint
pt( GetPosition() );
991 int w
= (sz
.x
- (ncols
- 1) * m_hgap
) / ncols
;
992 int h
= (sz
.y
- (nrows
- 1) * m_vgap
) / nrows
;
995 for (int c
= 0; c
< ncols
; c
++)
998 for (int r
= 0; r
< nrows
; r
++)
1000 int i
= r
* ncols
+ c
;
1003 wxSizerItemList::compatibility_iterator node
= m_children
.Item( i
);
1005 wxASSERT_MSG( node
, _T("Failed to find SizerItemList node") );
1007 SetItemBounds( node
->GetData(), x
, y
, w
, h
);
1015 wxSize
wxGridSizer::CalcMin()
1018 if ( CalcRowsCols(nrows
, ncols
) == 0 )
1019 return wxSize(10, 10);
1021 // Find the max width and height for any component
1025 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
1028 wxSizerItem
*item
= node
->GetData();
1029 wxSize
sz( item
->CalcMin() );
1031 w
= wxMax( w
, sz
.x
);
1032 h
= wxMax( h
, sz
.y
);
1034 node
= node
->GetNext();
1037 return wxSize( ncols
* w
+ (ncols
-1) * m_hgap
,
1038 nrows
* h
+ (nrows
-1) * m_vgap
);
1041 void wxGridSizer::SetItemBounds( wxSizerItem
*item
, int x
, int y
, int w
, int h
)
1044 wxSize
sz( item
->CalcMin() );
1045 int flag
= item
->GetFlag();
1047 if ((flag
& wxEXPAND
) || (flag
& wxSHAPED
))
1053 if (flag
& wxALIGN_CENTER_HORIZONTAL
)
1055 pt
.x
= x
+ (w
- sz
.x
- m_hgap
) / 2;
1057 else if (flag
& wxALIGN_RIGHT
)
1059 pt
.x
= x
+ (w
- sz
.x
- m_hgap
);
1062 if (flag
& wxALIGN_CENTER_VERTICAL
)
1064 pt
.y
= y
+ (h
- sz
.y
- m_vgap
) / 2;
1066 else if (flag
& wxALIGN_BOTTOM
)
1068 pt
.y
= y
+ (h
- sz
.y
- m_vgap
);
1072 item
->SetDimension(pt
, sz
);
1075 //---------------------------------------------------------------------------
1077 //---------------------------------------------------------------------------
1079 wxFlexGridSizer::wxFlexGridSizer( int rows
, int cols
, int vgap
, int hgap
)
1080 : wxGridSizer( rows
, cols
, vgap
, hgap
),
1081 m_flexDirection(wxBOTH
),
1082 m_growMode(wxFLEX_GROWMODE_SPECIFIED
)
1086 wxFlexGridSizer::wxFlexGridSizer( int cols
, int vgap
, int hgap
)
1087 : wxGridSizer( cols
, vgap
, hgap
),
1088 m_flexDirection(wxBOTH
),
1089 m_growMode(wxFLEX_GROWMODE_SPECIFIED
)
1093 wxFlexGridSizer::~wxFlexGridSizer()
1097 void wxFlexGridSizer::RecalcSizes()
1099 int nitems
, nrows
, ncols
;
1100 if ( (nitems
= CalcRowsCols(nrows
, ncols
)) == 0 )
1103 wxPoint
pt( GetPosition() );
1104 wxSize
sz( GetSize() );
1105 wxSize
minsz( CalcMin() );
1107 AdjustForGrowables(sz
, minsz
, nrows
, ncols
);
1109 sz
= wxSize( pt
.x
+ sz
.x
, pt
.y
+ sz
.y
);
1112 for (int c
= 0; c
< ncols
; c
++)
1115 for (int r
= 0; r
< nrows
; r
++)
1117 int i
= r
* ncols
+ c
;
1120 wxSizerItemList::compatibility_iterator node
= m_children
.Item( i
);
1122 wxASSERT_MSG( node
, _T("Failed to find node") );
1124 int w
= wxMax( 0, wxMin( m_colWidths
[c
], sz
.x
- x
) );
1125 int h
= wxMax( 0, wxMin( m_rowHeights
[r
], sz
.y
- y
) );
1127 SetItemBounds( node
->GetData(), x
, y
, w
, h
);
1129 y
= y
+ m_rowHeights
[r
] + m_vgap
;
1131 x
= x
+ m_colWidths
[c
] + m_hgap
;
1135 wxSize
wxFlexGridSizer::CalcMin()
1141 // Number of rows/columns can change as items are added or removed.
1142 if ( !CalcRowsCols(nrows
, ncols
) )
1143 return wxSize(10, 10);
1145 m_rowHeights
.SetCount(nrows
);
1146 m_colWidths
.SetCount(ncols
);
1148 // We have to recalcuate the sizes in case the item minimum size has
1149 // changed since the previous layout, or the item has been hidden using
1150 // wxSizer::Show(). If all the items in a row/column are hidden, the final
1151 // dimension of the row/column will be -1, indicating that the column
1152 // itself is hidden.
1153 for( s
= m_rowHeights
.GetCount(), i
= 0; i
< s
; ++i
)
1154 m_rowHeights
[ i
] = -1;
1155 for( s
= m_colWidths
.GetCount(), i
= 0; i
< s
; ++i
)
1156 m_colWidths
[ i
] = -1;
1158 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
1163 wxSizerItem
*item
= node
->GetData();
1164 if ( item
->IsShown() )
1166 wxSize
sz( item
->CalcMin() );
1167 int row
= i
/ ncols
;
1168 int col
= i
% ncols
;
1170 m_rowHeights
[ row
] = wxMax( wxMax( 0, sz
.y
), m_rowHeights
[ row
] );
1171 m_colWidths
[ col
] = wxMax( wxMax( 0, sz
.x
), m_colWidths
[ col
] );
1174 node
= node
->GetNext();
1178 AdjustForFlexDirection();
1180 // Sum total minimum size, including gaps between rows/columns.
1181 // -1 is used as a magic number meaning empty column.
1183 for (int col
= 0; col
< ncols
; col
++)
1184 if ( m_colWidths
[ col
] != -1 )
1185 width
+= m_colWidths
[ col
] + ( col
== ncols
-1 ? 0 : m_hgap
);
1188 for (int row
= 0; row
< nrows
; row
++)
1189 if ( m_rowHeights
[ row
] != -1 )
1190 height
+= m_rowHeights
[ row
] + ( row
== nrows
-1 ? 0 : m_vgap
);
1192 return wxSize( width
, height
);
1195 void wxFlexGridSizer::AdjustForFlexDirection()
1197 // the logic in CalcMin works when we resize flexibly in both directions
1198 // but maybe this is not the case
1199 if ( m_flexDirection
!= wxBOTH
)
1201 // select the array corresponding to the direction in which we do *not*
1203 wxArrayInt
& array
= m_flexDirection
== wxVERTICAL
? m_colWidths
1206 const int count
= array
.GetCount();
1208 // find the largest value in this array
1210 for ( n
= 0; n
< count
; ++n
)
1212 if ( array
[n
] > largest
)
1216 // and now fill it with the largest value
1217 for ( n
= 0; n
< count
; ++n
)
1225 void wxFlexGridSizer::AdjustForGrowables(const wxSize
& sz
, const wxSize
& minsz
,
1226 int nrows
, int ncols
)
1228 // what to do with the rows? by default, resize them proportionally
1229 if ( sz
.y
> minsz
.y
&& ( (m_flexDirection
& wxVERTICAL
) || (m_growMode
== wxFLEX_GROWMODE_SPECIFIED
) ) )
1231 int sum_proportions
= 0;
1232 int growable_space
= 0;
1235 for (idx
= 0; idx
< m_growableRows
.GetCount(); idx
++)
1237 // Since the number of rows/columns can change as items are
1238 // inserted/deleted, we need to verify at runtime that the
1239 // requested growable rows/columns are still valid.
1240 if (m_growableRows
[idx
] >= nrows
)
1243 // If all items in a row/column are hidden, that row/column will
1244 // have a dimension of -1. This causes the row/column to be
1245 // hidden completely.
1246 if (m_rowHeights
[ m_growableRows
[idx
] ] == -1)
1248 sum_proportions
+= m_growableRowsProportions
[idx
];
1249 growable_space
+= m_rowHeights
[ m_growableRows
[idx
] ];
1255 for (idx
= 0; idx
< m_growableRows
.GetCount(); idx
++)
1257 if (m_growableRows
[idx
] >= nrows
)
1259 if (m_rowHeights
[ m_growableRows
[idx
] ] == -1)
1260 m_rowHeights
[ m_growableRows
[idx
] ] = 0;
1263 int delta
= (sz
.y
- minsz
.y
);
1264 if (sum_proportions
== 0)
1265 delta
= (delta
/num
) + m_rowHeights
[ m_growableRows
[idx
] ];
1267 delta
= ((delta
+growable_space
)*m_growableRowsProportions
[idx
]) / sum_proportions
;
1268 m_rowHeights
[ m_growableRows
[idx
] ] = delta
;
1273 else if ( (m_growMode
== wxFLEX_GROWMODE_ALL
) && (sz
.y
> minsz
.y
) )
1275 // rounding problem?
1276 for ( int row
= 0; row
< nrows
; ++row
)
1277 m_rowHeights
[ row
] = sz
.y
/ nrows
;
1280 // the same logic as above but for the columns
1281 if ( sz
.x
> minsz
.x
&& ( (m_flexDirection
& wxHORIZONTAL
) || (m_growMode
== wxFLEX_GROWMODE_SPECIFIED
) ) )
1283 int sum_proportions
= 0;
1284 int growable_space
= 0;
1287 for (idx
= 0; idx
< m_growableCols
.GetCount(); idx
++)
1289 // Since the number of rows/columns can change as items are
1290 // inserted/deleted, we need to verify at runtime that the
1291 // requested growable rows/columns are still valid.
1292 if (m_growableCols
[idx
] >= ncols
)
1295 // If all items in a row/column are hidden, that row/column will
1296 // have a dimension of -1. This causes the column to be hidden
1298 if (m_colWidths
[ m_growableCols
[idx
] ] == -1)
1300 sum_proportions
+= m_growableColsProportions
[idx
];
1301 growable_space
+= m_colWidths
[ m_growableCols
[idx
] ];
1307 for (idx
= 0; idx
< m_growableCols
.GetCount(); idx
++)
1309 if (m_growableCols
[idx
] >= ncols
)
1311 if (m_colWidths
[ m_growableCols
[idx
] ] == -1)
1312 m_colWidths
[ m_growableCols
[idx
] ] = 0;
1315 int delta
= (sz
.x
- minsz
.x
);
1316 if (sum_proportions
== 0)
1317 delta
= (delta
/num
) + m_colWidths
[ m_growableCols
[idx
] ];
1319 delta
= ((delta
+growable_space
)*m_growableColsProportions
[idx
])/sum_proportions
;
1320 m_colWidths
[ m_growableCols
[idx
] ] = delta
;
1325 else if ( (m_growMode
== wxFLEX_GROWMODE_ALL
) && (sz
.x
> minsz
.x
) )
1327 for ( int col
=0; col
< ncols
; ++col
)
1328 m_colWidths
[ col
] = sz
.x
/ ncols
;
1333 void wxFlexGridSizer::AddGrowableRow( size_t idx
, int proportion
)
1335 m_growableRows
.Add( idx
);
1336 m_growableRowsProportions
.Add( proportion
);
1339 void wxFlexGridSizer::RemoveGrowableRow( size_t idx
)
1341 m_growableRows
.Remove( idx
);
1344 void wxFlexGridSizer::AddGrowableCol( size_t idx
, int proportion
)
1346 m_growableCols
.Add( idx
);
1347 m_growableColsProportions
.Add( proportion
);
1350 void wxFlexGridSizer::RemoveGrowableCol( size_t idx
)
1352 m_growableCols
.Remove( idx
);
1355 //---------------------------------------------------------------------------
1357 //---------------------------------------------------------------------------
1359 wxBoxSizer::wxBoxSizer( int orient
)
1360 : m_orient( orient
)
1364 void wxBoxSizer::RecalcSizes()
1366 if (m_children
.GetCount() == 0)
1372 if (m_orient
== wxHORIZONTAL
)
1373 delta
= m_size
.x
- m_fixedWidth
;
1375 delta
= m_size
.y
- m_fixedHeight
;
1378 wxPoint
pt( m_position
);
1380 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
1383 wxSizerItem
*item
= node
->GetData();
1385 if (item
->IsShown())
1387 wxSize
size( item
->CalcMin() );
1389 if (m_orient
== wxVERTICAL
)
1391 wxCoord height
= size
.y
;
1392 if (item
->GetProportion())
1394 // Because of at least one visible item has non-zero
1395 // proportion then m_stretchable is not zero
1396 height
= (delta
* item
->GetProportion()) / m_stretchable
;
1399 wxPoint
child_pos( pt
);
1400 wxSize
child_size( wxSize( size
.x
, height
) );
1402 if (item
->GetFlag() & (wxEXPAND
| wxSHAPED
))
1403 child_size
.x
= m_size
.x
;
1404 else if (item
->GetFlag() & wxALIGN_RIGHT
)
1405 child_pos
.x
+= m_size
.x
- size
.x
;
1406 else if (item
->GetFlag() & (wxCENTER
| wxALIGN_CENTER_HORIZONTAL
))
1407 // XXX wxCENTER is added for backward compatibility;
1408 // wxALIGN_CENTER should be used in new code
1409 child_pos
.x
+= (m_size
.x
- size
.x
) / 2;
1411 item
->SetDimension( child_pos
, child_size
);
1417 wxCoord width
= size
.x
;
1418 if (item
->GetProportion())
1420 // Because of at least one visible item has non-zero
1421 // proportion then m_stretchable is not zero
1422 width
= (delta
* item
->GetProportion()) / m_stretchable
;
1425 wxPoint
child_pos( pt
);
1426 wxSize
child_size( wxSize(width
, size
.y
) );
1428 if (item
->GetFlag() & (wxEXPAND
| wxSHAPED
))
1429 child_size
.y
= m_size
.y
;
1430 else if (item
->GetFlag() & wxALIGN_BOTTOM
)
1431 child_pos
.y
+= m_size
.y
- size
.y
;
1432 else if (item
->GetFlag() & (wxCENTER
| wxALIGN_CENTER_VERTICAL
))
1433 // XXX wxCENTER is added for backward compatibility;
1434 // wxALIGN_CENTER should be used in new code
1435 child_pos
.y
+= (m_size
.y
- size
.y
) / 2;
1437 item
->SetDimension( child_pos
, child_size
);
1443 node
= node
->GetNext();
1447 wxSize
wxBoxSizer::CalcMin()
1449 if (m_children
.GetCount() == 0)
1450 return wxSize(10,10);
1458 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
1461 wxSizerItem
*item
= node
->GetData();
1463 if (item
->IsShown() && item
->GetProportion() != 0)
1464 m_stretchable
+= item
->GetProportion();
1466 node
= node
->GetNext();
1469 // Total minimum size (width or height) of sizer
1472 node
= m_children
.GetFirst();
1475 wxSizerItem
*item
= node
->GetData();
1477 if (item
->IsShown() && item
->GetProportion() != 0)
1479 int stretch
= item
->GetProportion();
1480 wxSize
size( item
->CalcMin() );
1483 // Integer division rounded up is (a + b - 1) / b
1484 // Round up needed in order to guarantee that all
1485 // all items will have size not less then their min size
1486 if (m_orient
== wxHORIZONTAL
)
1487 minSize
= ( size
.x
*m_stretchable
+ stretch
- 1)/stretch
;
1489 minSize
= ( size
.y
*m_stretchable
+ stretch
- 1)/stretch
;
1491 if (minSize
> maxMinSize
)
1492 maxMinSize
= minSize
;
1494 node
= node
->GetNext();
1497 // Calculate overall minimum size
1498 node
= m_children
.GetFirst();
1501 wxSizerItem
*item
= node
->GetData();
1503 if (item
->IsShown())
1505 wxSize
size( item
->CalcMin() );
1506 if (item
->GetProportion() != 0)
1508 if (m_orient
== wxHORIZONTAL
)
1509 size
.x
= (maxMinSize
*item
->GetProportion())/m_stretchable
;
1511 size
.y
= (maxMinSize
*item
->GetProportion())/m_stretchable
;
1515 if (m_orient
== wxVERTICAL
)
1517 m_fixedHeight
+= size
.y
;
1518 m_fixedWidth
= wxMax( m_fixedWidth
, size
.x
);
1522 m_fixedWidth
+= size
.x
;
1523 m_fixedHeight
= wxMax( m_fixedHeight
, size
.y
);
1527 if (m_orient
== wxHORIZONTAL
)
1529 m_minWidth
+= size
.x
;
1530 m_minHeight
= wxMax( m_minHeight
, size
.y
);
1534 m_minHeight
+= size
.y
;
1535 m_minWidth
= wxMax( m_minWidth
, size
.x
);
1538 node
= node
->GetNext();
1541 return wxSize( m_minWidth
, m_minHeight
);
1544 //---------------------------------------------------------------------------
1546 //---------------------------------------------------------------------------
1550 wxStaticBoxSizer::wxStaticBoxSizer( wxStaticBox
*box
, int orient
)
1551 : wxBoxSizer( orient
)
1552 , m_staticBox( box
)
1554 wxASSERT_MSG( box
, wxT("wxStaticBoxSizer needs a static box") );
1557 static void GetStaticBoxBorders( wxStaticBox
*box
,
1561 // this has to be done platform by platform as there is no way to
1562 // guess the thickness of a wxStaticBox border
1564 box
->GetBordersForSizer(borderTop
,borderOther
);
1565 #elif defined(__WXMAC__)
1567 static int extraTop
= -1; // Uninitted
1568 static int other
= 5;
1570 if ( extraTop
== -1 )
1572 // The minimal border used for the top. Later on the staticbox'
1573 // font height is added to this.
1576 if ( UMAGetSystemVersion() >= 0x1030 /*Panther*/ )
1578 // As indicated by the HIG, Panther needs an extra border of 11
1579 // pixels (otherwise overlapping occurs at the top). The "other"
1580 // border has to be 11.
1587 *borderTop
= extraTop
+ box
->GetCharHeight();
1588 *borderOther
= other
;
1592 if ( box
->GetLabel().IsEmpty() )
1596 *borderTop
= box
->GetCharHeight();
1599 #endif // __WXCOCOA__
1602 void wxStaticBoxSizer::RecalcSizes()
1604 int top_border
, other_border
;
1605 GetStaticBoxBorders(m_staticBox
, &top_border
, &other_border
);
1607 m_staticBox
->SetSize( m_position
.x
, m_position
.y
, m_size
.x
, m_size
.y
);
1609 wxPoint
old_pos( m_position
);
1610 m_position
.x
+= other_border
;
1611 m_position
.y
+= top_border
;
1612 wxSize
old_size( m_size
);
1613 m_size
.x
-= 2*other_border
;
1614 m_size
.y
-= top_border
+ other_border
;
1616 wxBoxSizer::RecalcSizes();
1618 m_position
= old_pos
;
1622 wxSize
wxStaticBoxSizer::CalcMin()
1624 int top_border
, other_border
;
1625 GetStaticBoxBorders(m_staticBox
, &top_border
, &other_border
);
1627 wxSize
ret( wxBoxSizer::CalcMin() );
1628 ret
.x
+= 2*other_border
;
1629 ret
.y
+= other_border
+ top_border
;
1634 void wxStaticBoxSizer::ShowItems( bool show
)
1636 m_staticBox
->Show( show
);
1637 wxBoxSizer::ShowItems( show
);
1640 #endif // wxUSE_STATBOX
1643 #if WXWIN_COMPATIBILITY_2_4
1645 // ----------------------------------------------------------------------------
1647 // ----------------------------------------------------------------------------
1650 IMPLEMENT_CLASS(wxBookCtrlSizer
, wxSizer
)
1652 IMPLEMENT_CLASS(wxNotebookSizer
, wxBookCtrlSizer
)
1653 #endif // wxUSE_NOTEBOOK
1654 #endif // wxUSE_BOOKCTRL
1658 wxBookCtrlSizer::wxBookCtrlSizer(wxBookCtrl
*bookctrl
)
1659 : m_bookctrl(bookctrl
)
1661 wxASSERT_MSG( bookctrl
, wxT("wxBookCtrlSizer needs a control") );
1664 void wxBookCtrlSizer::RecalcSizes()
1666 m_bookctrl
->SetSize( m_position
.x
, m_position
.y
, m_size
.x
, m_size
.y
);
1669 wxSize
wxBookCtrlSizer::CalcMin()
1671 wxSize sizeBorder
= m_bookctrl
->CalcSizeFromPage(wxSize(0, 0));
1676 if ( m_bookctrl
->GetPageCount() == 0 )
1678 return wxSize(sizeBorder
.x
+ 10, sizeBorder
.y
+ 10);
1684 wxWindowList::compatibility_iterator
1685 node
= m_bookctrl
->GetChildren().GetFirst();
1688 wxWindow
*item
= node
->GetData();
1689 wxSizer
*itemsizer
= item
->GetSizer();
1693 wxSize
subsize( itemsizer
->CalcMin() );
1695 if (subsize
.x
> maxX
)
1697 if (subsize
.y
> maxY
)
1701 node
= node
->GetNext();
1704 return wxSize( maxX
, maxY
) + sizeBorder
;
1709 wxNotebookSizer::wxNotebookSizer(wxNotebook
*nb
)
1711 wxASSERT_MSG( nb
, wxT("wxNotebookSizer needs a control") );
1715 #endif // wxUSE_NOTEBOOOK
1716 #endif // wxUSE_BOOKCTRL
1718 #endif // WXWIN_COMPATIBILITY_2_4