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/settings.h"
28 #include "wx/listimpl.cpp"
29 #if WXWIN_COMPATIBILITY_2_4
30 #include "wx/notebook.h"
34 # include "wx/mac/uma.h"
37 //---------------------------------------------------------------------------
39 IMPLEMENT_CLASS(wxSizerItem
, wxObject
)
40 IMPLEMENT_CLASS(wxSizer
, wxObject
)
41 IMPLEMENT_CLASS(wxGridSizer
, wxSizer
)
42 IMPLEMENT_CLASS(wxFlexGridSizer
, wxGridSizer
)
43 IMPLEMENT_CLASS(wxBoxSizer
, wxSizer
)
45 IMPLEMENT_CLASS(wxStaticBoxSizer
, wxBoxSizer
)
47 IMPLEMENT_CLASS(wxStdDialogButtonSizer
, wxBoxSizer
)
49 WX_DEFINE_EXPORTED_LIST( wxSizerItemList
);
84 //---------------------------------------------------------------------------
86 //---------------------------------------------------------------------------
88 void wxSizerItem::Init()
94 m_zoneRect
= wxRect( 0, 0, 0, 0 );
97 void wxSizerItem::Init(const wxSizerFlags
& flags
)
101 m_proportion
= flags
.GetProportion();
102 m_flag
= flags
.GetFlags();
103 m_border
= flags
.GetBorderInPixels();
106 wxSizerItem::wxSizerItem( int width
, int height
, int proportion
, int flag
, int border
, wxObject
* userData
)
109 , m_size( wxSize( width
, height
) ) // size is set directly
110 , m_minSize( m_size
) // minimal size is the initial size
111 , m_proportion( proportion
)
114 , m_zoneRect( 0, 0, 0, 0 )
116 , m_userData( userData
)
121 wxSizerItem::wxSizerItem( wxWindow
*window
, int proportion
, int flag
, int border
, wxObject
* userData
)
124 , m_proportion( proportion
)
127 , m_zoneRect( 0, 0, 0, 0 )
129 , m_userData( userData
)
131 if (flag
& wxFIXED_MINSIZE
)
132 window
->SetMinSize(window
->GetSize());
133 m_minSize
= window
->GetSize();
135 // aspect ratio calculated from initial size
136 SetRatio( m_minSize
);
138 // m_size is calculated later
141 wxSizerItem::wxSizerItem( wxSizer
*sizer
, int proportion
, int flag
, int border
, wxObject
* userData
)
144 , m_proportion( proportion
)
147 , m_zoneRect( 0, 0, 0, 0 )
150 , m_userData( userData
)
152 // m_minSize is calculated later
153 // m_size is calculated later
156 wxSizerItem::wxSizerItem()
165 wxSizerItem::~wxSizerItem()
171 m_window
->SetContainingSizer(NULL
);
173 else // we must be a sizer
180 wxSize
wxSizerItem::GetSize() const
184 ret
= m_sizer
->GetSize();
187 ret
= m_window
->GetSize();
194 if (m_flag
& wxNORTH
)
196 if (m_flag
& wxSOUTH
)
202 wxSize
wxSizerItem::CalcMin()
206 m_minSize
= m_sizer
->GetMinSize();
208 // if we have to preserve aspect ratio _AND_ this is
209 // the first-time calculation, consider ret to be initial size
210 if ((m_flag
& wxSHAPED
) && !m_ratio
)
213 else if ( IsWindow() )
215 // Since the size of the window may change during runtime, we
216 // should use the current minimal/best size.
217 m_minSize
= m_window
->GetBestFittingSize();
220 return GetMinSizeWithBorder();
223 wxSize
wxSizerItem::GetMinSizeWithBorder() const
225 wxSize ret
= m_minSize
;
231 if (m_flag
& wxNORTH
)
233 if (m_flag
& wxSOUTH
)
240 void wxSizerItem::SetDimension( wxPoint pos
, wxSize size
)
242 if (m_flag
& wxSHAPED
)
244 // adjust aspect ratio
245 int rwidth
= (int) (size
.y
* m_ratio
);
249 int rheight
= (int) (size
.x
/ m_ratio
);
250 // add vertical space
251 if (m_flag
& wxALIGN_CENTER_VERTICAL
)
252 pos
.y
+= (size
.y
- rheight
) / 2;
253 else if (m_flag
& wxALIGN_BOTTOM
)
254 pos
.y
+= (size
.y
- rheight
);
255 // use reduced dimensions
258 else if (rwidth
< size
.x
)
260 // add horizontal space
261 if (m_flag
& wxALIGN_CENTER_HORIZONTAL
)
262 pos
.x
+= (size
.x
- rwidth
) / 2;
263 else if (m_flag
& wxALIGN_RIGHT
)
264 pos
.x
+= (size
.x
- rwidth
);
269 // This is what GetPosition() returns. Since we calculate
270 // borders afterwards, GetPosition() will be the left/top
271 // corner of the surrounding border.
283 if (m_flag
& wxNORTH
)
288 if (m_flag
& wxSOUTH
)
294 m_sizer
->SetDimension( pos
.x
, pos
.y
, size
.x
, size
.y
);
296 m_zoneRect
= wxRect(pos
, size
);
298 m_window
->SetSize( pos
.x
, pos
.y
, size
.x
, size
.y
, wxSIZE_ALLOW_MINUS_ONE
);
303 void wxSizerItem::DeleteWindows()
312 m_sizer
->DeleteWindows();
315 bool wxSizerItem::IsWindow() const
317 return (m_window
!= NULL
);
320 bool wxSizerItem::IsSizer() const
322 return (m_sizer
!= NULL
);
325 bool wxSizerItem::IsSpacer() const
327 return (m_window
== NULL
) && (m_sizer
== NULL
);
330 void wxSizerItem::Show( bool show
)
335 m_window
->Show( show
);
337 m_sizer
->ShowItems( show
);
339 // ... nothing else to do to hide/show spacers
342 void wxSizerItem::SetOption( int option
)
344 SetProportion( option
);
347 int wxSizerItem::GetOption() const
349 return GetProportion();
353 //---------------------------------------------------------------------------
355 //---------------------------------------------------------------------------
358 : m_minSize( wxSize( 0, 0 ) )
364 WX_CLEAR_LIST(wxSizerItemList
, m_children
);
367 wxSizerItem
* wxSizer::Insert( size_t index
, wxSizerItem
*item
)
369 m_children
.Insert( index
, item
);
371 if( item
->GetWindow() )
372 item
->GetWindow()->SetContainingSizer( this );
377 bool wxSizer::Remove( wxWindow
*window
)
379 return Detach( window
);
382 bool wxSizer::Remove( wxSizer
*sizer
)
384 wxASSERT_MSG( sizer
, _T("Removing NULL sizer") );
386 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
389 wxSizerItem
*item
= node
->GetData();
391 if (item
->GetSizer() == sizer
)
394 m_children
.Erase( node
);
398 node
= node
->GetNext();
404 bool wxSizer::Remove( int index
)
406 wxCHECK_MSG( index
>= 0 && (size_t)index
< m_children
.GetCount(),
408 _T("Remove index is out of range") );
410 wxSizerItemList::compatibility_iterator node
= m_children
.Item( index
);
412 wxCHECK_MSG( node
, false, _T("Failed to find child node") );
414 wxSizerItem
*item
= node
->GetData();
416 if( item
->IsWindow() )
417 item
->GetWindow()->SetContainingSizer( NULL
);
420 m_children
.Erase( node
);
424 bool wxSizer::Detach( wxSizer
*sizer
)
426 wxASSERT_MSG( sizer
, _T("Detaching NULL sizer") );
428 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
431 wxSizerItem
*item
= node
->GetData();
433 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
);
459 m_children
.Erase( node
);
462 node
= node
->GetNext();
468 bool wxSizer::Detach( int index
)
470 wxCHECK_MSG( index
>= 0 && (size_t)index
< m_children
.GetCount(),
472 _T("Detach index is out of range") );
474 wxSizerItemList::compatibility_iterator node
= m_children
.Item( index
);
476 wxCHECK_MSG( node
, false, _T("Failed to find child node") );
478 wxSizerItem
*item
= node
->GetData();
480 if( item
->IsSizer() )
482 else if( item
->IsWindow() )
483 item
->GetWindow()->SetContainingSizer( NULL
);
486 m_children
.Erase( node
);
490 void wxSizer::Clear( bool delete_windows
)
492 // First clear the ContainingSizer pointers
493 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
496 wxSizerItem
*item
= node
->GetData();
498 if (item
->IsWindow())
499 item
->GetWindow()->SetContainingSizer( NULL
);
500 node
= node
->GetNext();
503 // Destroy the windows if needed
507 // Now empty the list
508 WX_CLEAR_LIST(wxSizerItemList
, m_children
);
511 void wxSizer::DeleteWindows()
513 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
516 wxSizerItem
*item
= node
->GetData();
518 item
->DeleteWindows();
519 node
= node
->GetNext();
523 wxSize
wxSizer::Fit( wxWindow
*window
)
525 wxSize
size(window
->IsTopLevel() ? FitSize(window
)
526 : GetMinWindowSize(window
));
528 window
->SetSize( size
);
533 void wxSizer::FitInside( wxWindow
*window
)
536 if (window
->IsTopLevel())
537 size
= VirtualFitSize( window
);
539 size
= GetMinClientSize( window
);
541 window
->SetVirtualSize( size
);
544 void wxSizer::Layout()
546 // (re)calculates minimums needed for each item and other preparations
550 // Applies the layout and repositions/resizes the items
554 void wxSizer::SetSizeHints( wxWindow
*window
)
556 // Preserve the window's max size hints, but set the
557 // lower bound according to the sizer calculations.
559 wxSize size
= Fit( window
);
561 window
->SetSizeHints( size
.x
,
563 window
->GetMaxWidth(),
564 window
->GetMaxHeight() );
567 void wxSizer::SetVirtualSizeHints( wxWindow
*window
)
569 // Preserve the window's max size hints, but set the
570 // lower bound according to the sizer calculations.
573 wxSize
size( window
->GetVirtualSize() );
574 window
->SetVirtualSizeHints( size
.x
,
576 window
->GetMaxWidth(),
577 window
->GetMaxHeight() );
580 wxSize
wxSizer::GetMaxWindowSize( wxWindow
*window
) const
582 return window
->GetMaxSize();
585 wxSize
wxSizer::GetMinWindowSize( wxWindow
*window
)
587 wxSize
minSize( GetMinSize() );
588 wxSize
size( window
->GetSize() );
589 wxSize
client_size( window
->GetClientSize() );
591 return wxSize( minSize
.x
+size
.x
-client_size
.x
,
592 minSize
.y
+size
.y
-client_size
.y
);
595 // TODO on mac we need a function that determines how much free space this
596 // min size contains, in order to make sure that we have 20 pixels of free
597 // space around the controls
599 // Return a window size that will fit within the screens dimensions
600 wxSize
wxSizer::FitSize( wxWindow
*window
)
602 wxSize size
= GetMinWindowSize( window
);
603 wxSize sizeMax
= GetMaxWindowSize( window
);
605 // Limit the size if sizeMax != wxDefaultSize
607 if ( size
.x
> sizeMax
.x
&& sizeMax
.x
!= wxDefaultCoord
)
609 if ( size
.y
> sizeMax
.y
&& sizeMax
.y
!= wxDefaultCoord
)
615 wxSize
wxSizer::GetMaxClientSize( wxWindow
*window
) const
617 wxSize
maxSize( window
->GetMaxSize() );
619 if( maxSize
!= wxDefaultSize
)
621 wxSize
size( window
->GetSize() );
622 wxSize
client_size( window
->GetClientSize() );
624 return wxSize( maxSize
.x
+ client_size
.x
- size
.x
,
625 maxSize
.y
+ client_size
.y
- size
.y
);
628 return wxDefaultSize
;
631 wxSize
wxSizer::GetMinClientSize( wxWindow
*WXUNUSED(window
) )
633 return GetMinSize(); // Already returns client size.
636 wxSize
wxSizer::VirtualFitSize( wxWindow
*window
)
638 wxSize size
= GetMinClientSize( window
);
639 wxSize sizeMax
= GetMaxClientSize( window
);
641 // Limit the size if sizeMax != wxDefaultSize
643 if ( size
.x
> sizeMax
.x
&& sizeMax
.x
!= wxDefaultCoord
)
645 if ( size
.y
> sizeMax
.y
&& sizeMax
.y
!= wxDefaultCoord
)
651 void wxSizer::SetDimension( int x
, int y
, int width
, int height
)
660 wxSize
wxSizer::GetMinSize()
662 wxSize
ret( CalcMin() );
663 if (ret
.x
< m_minSize
.x
) ret
.x
= m_minSize
.x
;
664 if (ret
.y
< m_minSize
.y
) ret
.y
= m_minSize
.y
;
668 void wxSizer::DoSetMinSize( int width
, int height
)
671 m_minSize
.y
= height
;
674 bool wxSizer::DoSetItemMinSize( wxWindow
*window
, int width
, int height
)
676 wxASSERT_MSG( window
, _T("SetMinSize for NULL window") );
678 // Is it our immediate child?
680 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
683 wxSizerItem
*item
= node
->GetData();
685 if (item
->GetWindow() == window
)
687 item
->SetMinSize( width
, height
);
690 node
= node
->GetNext();
693 // No? Search any subsizers we own then
695 node
= m_children
.GetFirst();
698 wxSizerItem
*item
= node
->GetData();
700 if ( item
->GetSizer() &&
701 item
->GetSizer()->DoSetItemMinSize( window
, width
, height
) )
703 // A child sizer found the requested windw, exit.
706 node
= node
->GetNext();
712 bool wxSizer::DoSetItemMinSize( wxSizer
*sizer
, int width
, int height
)
714 wxASSERT_MSG( sizer
, _T("SetMinSize for NULL sizer") );
716 // Is it our immediate child?
718 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
721 wxSizerItem
*item
= node
->GetData();
723 if (item
->GetSizer() == sizer
)
725 item
->GetSizer()->DoSetMinSize( width
, height
);
728 node
= node
->GetNext();
731 // No? Search any subsizers we own then
733 node
= m_children
.GetFirst();
736 wxSizerItem
*item
= node
->GetData();
738 if ( item
->GetSizer() &&
739 item
->GetSizer()->DoSetItemMinSize( sizer
, width
, height
) )
741 // A child found the requested sizer, exit.
744 node
= node
->GetNext();
750 bool wxSizer::DoSetItemMinSize( size_t index
, int width
, int height
)
752 wxSizerItemList::compatibility_iterator node
= m_children
.Item( index
);
754 wxCHECK_MSG( node
, false, _T("Failed to find child node") );
756 wxSizerItem
*item
= node
->GetData();
758 if (item
->GetSizer())
760 // Sizers contains the minimal size in them, if not calculated ...
761 item
->GetSizer()->DoSetMinSize( width
, height
);
765 // ... but the minimal size of spacers and windows is stored via the item
766 item
->SetMinSize( width
, height
);
772 wxSizerItem
* wxSizer::GetItem( wxWindow
*window
, bool recursive
)
774 wxASSERT_MSG( window
, _T("GetItem for NULL window") );
776 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
779 wxSizerItem
*item
= node
->GetData();
781 if (item
->GetWindow() == window
)
785 else if (recursive
&& item
->IsSizer())
787 wxSizerItem
*subitem
= item
->GetSizer()->GetItem( window
, true );
792 node
= node
->GetNext();
798 wxSizerItem
* wxSizer::GetItem( wxSizer
*sizer
, bool recursive
)
800 wxASSERT_MSG( sizer
, _T("GetItem for NULL sizer") );
802 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
805 wxSizerItem
*item
= node
->GetData();
807 if (item
->GetSizer() == sizer
)
811 else if (recursive
&& item
->IsSizer())
813 wxSizerItem
*subitem
= item
->GetSizer()->GetItem( sizer
, true );
818 node
= node
->GetNext();
824 wxSizerItem
* wxSizer::GetItem( size_t index
)
826 wxCHECK_MSG( index
< m_children
.GetCount(),
828 _T("GetItem index is out of range") );
830 return m_children
.Item( index
)->GetData();
833 bool wxSizer::Show( wxWindow
*window
, bool show
, bool recursive
)
835 wxSizerItem
*item
= GetItem( window
, recursive
);
846 bool wxSizer::Show( wxSizer
*sizer
, bool show
, bool recursive
)
848 wxSizerItem
*item
= GetItem( sizer
, recursive
);
859 bool wxSizer::Show( size_t index
, bool show
)
861 wxSizerItem
*item
= GetItem( index
);
872 void wxSizer::ShowItems( bool show
)
874 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
877 node
->GetData()->Show( show
);
878 node
= node
->GetNext();
882 bool wxSizer::IsShown( wxWindow
*window
) const
884 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
887 wxSizerItem
*item
= node
->GetData();
889 if (item
->GetWindow() == window
)
891 return item
->IsShown();
893 node
= node
->GetNext();
896 wxFAIL_MSG( _T("IsShown failed to find sizer item") );
901 bool wxSizer::IsShown( wxSizer
*sizer
) const
903 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
906 wxSizerItem
*item
= node
->GetData();
908 if (item
->GetSizer() == sizer
)
910 return item
->IsShown();
912 node
= node
->GetNext();
915 wxFAIL_MSG( _T("IsShown failed to find sizer item") );
920 bool wxSizer::IsShown( size_t index
) const
922 wxCHECK_MSG( index
< m_children
.GetCount(),
924 _T("IsShown index is out of range") );
926 return m_children
.Item( index
)->GetData()->IsShown();
930 //---------------------------------------------------------------------------
932 //---------------------------------------------------------------------------
934 wxGridSizer::wxGridSizer( int rows
, int cols
, int vgap
, int hgap
)
940 if (m_rows
== 0 && m_cols
== 0)
944 wxGridSizer::wxGridSizer( int cols
, int vgap
, int hgap
)
950 if (m_rows
== 0 && m_cols
== 0)
954 int wxGridSizer::CalcRowsCols(int& nrows
, int& ncols
) const
956 int nitems
= m_children
.GetCount();
962 nrows
= (nitems
+ m_cols
- 1) / m_cols
;
966 ncols
= (nitems
+ m_rows
- 1) / m_rows
;
969 else // 0 columns, 0 rows?
971 wxFAIL_MSG( _T("grid sizer must have either rows or columns fixed") );
980 void wxGridSizer::RecalcSizes()
982 int nitems
, nrows
, ncols
;
983 if ( (nitems
= CalcRowsCols(nrows
, ncols
)) == 0 )
986 wxSize
sz( GetSize() );
987 wxPoint
pt( GetPosition() );
989 int w
= (sz
.x
- (ncols
- 1) * m_hgap
) / ncols
;
990 int h
= (sz
.y
- (nrows
- 1) * m_vgap
) / nrows
;
993 for (int c
= 0; c
< ncols
; c
++)
996 for (int r
= 0; r
< nrows
; r
++)
998 int i
= r
* ncols
+ c
;
1001 wxSizerItemList::compatibility_iterator node
= m_children
.Item( i
);
1003 wxASSERT_MSG( node
, _T("Failed to find SizerItemList node") );
1005 SetItemBounds( node
->GetData(), x
, y
, w
, h
);
1013 wxSize
wxGridSizer::CalcMin()
1016 if ( CalcRowsCols(nrows
, ncols
) == 0 )
1017 return wxSize(10, 10);
1019 // Find the max width and height for any component
1023 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
1026 wxSizerItem
*item
= node
->GetData();
1027 wxSize
sz( item
->CalcMin() );
1029 w
= wxMax( w
, sz
.x
);
1030 h
= wxMax( h
, sz
.y
);
1032 node
= node
->GetNext();
1035 return wxSize( ncols
* w
+ (ncols
-1) * m_hgap
,
1036 nrows
* h
+ (nrows
-1) * m_vgap
);
1039 void wxGridSizer::SetItemBounds( wxSizerItem
*item
, int x
, int y
, int w
, int h
)
1042 wxSize
sz( item
->GetMinSizeWithBorder() );
1043 int flag
= item
->GetFlag();
1045 if ((flag
& wxEXPAND
) || (flag
& wxSHAPED
))
1051 if (flag
& wxALIGN_CENTER_HORIZONTAL
)
1053 pt
.x
= x
+ (w
- sz
.x
) / 2;
1055 else if (flag
& wxALIGN_RIGHT
)
1057 pt
.x
= x
+ (w
- sz
.x
);
1060 if (flag
& wxALIGN_CENTER_VERTICAL
)
1062 pt
.y
= y
+ (h
- sz
.y
) / 2;
1064 else if (flag
& wxALIGN_BOTTOM
)
1066 pt
.y
= y
+ (h
- sz
.y
);
1070 item
->SetDimension(pt
, sz
);
1073 //---------------------------------------------------------------------------
1075 //---------------------------------------------------------------------------
1077 wxFlexGridSizer::wxFlexGridSizer( int rows
, int cols
, int vgap
, int hgap
)
1078 : wxGridSizer( rows
, cols
, vgap
, hgap
),
1079 m_flexDirection(wxBOTH
),
1080 m_growMode(wxFLEX_GROWMODE_SPECIFIED
)
1084 wxFlexGridSizer::wxFlexGridSizer( int cols
, int vgap
, int hgap
)
1085 : wxGridSizer( cols
, vgap
, hgap
),
1086 m_flexDirection(wxBOTH
),
1087 m_growMode(wxFLEX_GROWMODE_SPECIFIED
)
1091 wxFlexGridSizer::~wxFlexGridSizer()
1095 void wxFlexGridSizer::RecalcSizes()
1097 int nitems
, nrows
, ncols
;
1098 if ( (nitems
= CalcRowsCols(nrows
, ncols
)) == 0 )
1101 wxPoint
pt( GetPosition() );
1102 wxSize
sz( GetSize() );
1104 AdjustForGrowables(sz
, m_calculatedMinSize
, nrows
, ncols
);
1106 sz
= wxSize( pt
.x
+ sz
.x
, pt
.y
+ sz
.y
);
1109 for (int c
= 0; c
< ncols
; c
++)
1112 for (int r
= 0; r
< nrows
; r
++)
1114 int i
= r
* ncols
+ c
;
1117 wxSizerItemList::compatibility_iterator node
= m_children
.Item( i
);
1119 wxASSERT_MSG( node
, _T("Failed to find node") );
1121 int w
= wxMax( 0, wxMin( m_colWidths
[c
], sz
.x
- x
) );
1122 int h
= wxMax( 0, wxMin( m_rowHeights
[r
], sz
.y
- y
) );
1124 SetItemBounds( node
->GetData(), x
, y
, w
, h
);
1126 y
= y
+ m_rowHeights
[r
] + m_vgap
;
1128 x
= x
+ m_colWidths
[c
] + m_hgap
;
1132 wxSize
wxFlexGridSizer::CalcMin()
1138 // Number of rows/columns can change as items are added or removed.
1139 if ( !CalcRowsCols(nrows
, ncols
) )
1140 return wxSize(10, 10);
1142 m_rowHeights
.SetCount(nrows
);
1143 m_colWidths
.SetCount(ncols
);
1145 // We have to recalcuate the sizes in case the item minimum size has
1146 // changed since the previous layout, or the item has been hidden using
1147 // wxSizer::Show(). If all the items in a row/column are hidden, the final
1148 // dimension of the row/column will be -1, indicating that the column
1149 // itself is hidden.
1150 for( s
= m_rowHeights
.GetCount(), i
= 0; i
< s
; ++i
)
1151 m_rowHeights
[ i
] = -1;
1152 for( s
= m_colWidths
.GetCount(), i
= 0; i
< s
; ++i
)
1153 m_colWidths
[ i
] = -1;
1155 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
1160 wxSizerItem
*item
= node
->GetData();
1161 if ( item
->IsShown() )
1163 wxSize
sz( item
->CalcMin() );
1164 int row
= i
/ ncols
;
1165 int col
= i
% ncols
;
1167 m_rowHeights
[ row
] = wxMax( wxMax( 0, sz
.y
), m_rowHeights
[ row
] );
1168 m_colWidths
[ col
] = wxMax( wxMax( 0, sz
.x
), m_colWidths
[ col
] );
1171 node
= node
->GetNext();
1175 AdjustForFlexDirection();
1177 // Sum total minimum size, including gaps between rows/columns.
1178 // -1 is used as a magic number meaning empty column.
1180 for (int col
= 0; col
< ncols
; col
++)
1181 if ( m_colWidths
[ col
] != -1 )
1182 width
+= m_colWidths
[ col
] + ( col
== ncols
-1 ? 0 : m_hgap
);
1185 for (int row
= 0; row
< nrows
; row
++)
1186 if ( m_rowHeights
[ row
] != -1 )
1187 height
+= m_rowHeights
[ row
] + ( row
== nrows
-1 ? 0 : m_vgap
);
1189 m_calculatedMinSize
= wxSize( width
, height
);
1190 return m_calculatedMinSize
;
1193 void wxFlexGridSizer::AdjustForFlexDirection()
1195 // the logic in CalcMin works when we resize flexibly in both directions
1196 // but maybe this is not the case
1197 if ( m_flexDirection
!= wxBOTH
)
1199 // select the array corresponding to the direction in which we do *not*
1201 wxArrayInt
& array
= m_flexDirection
== wxVERTICAL
? m_colWidths
1204 const int count
= array
.GetCount();
1206 // find the largest value in this array
1208 for ( n
= 0; n
< count
; ++n
)
1210 if ( array
[n
] > largest
)
1214 // and now fill it with the largest value
1215 for ( n
= 0; n
< count
; ++n
)
1223 void wxFlexGridSizer::AdjustForGrowables(const wxSize
& sz
, const wxSize
& minsz
,
1224 int nrows
, int ncols
)
1226 // what to do with the rows? by default, resize them proportionally
1227 if ( sz
.y
> minsz
.y
&& ( (m_flexDirection
& wxVERTICAL
) || (m_growMode
== wxFLEX_GROWMODE_SPECIFIED
) ) )
1229 int sum_proportions
= 0;
1230 int growable_space
= 0;
1233 for (idx
= 0; idx
< m_growableRows
.GetCount(); idx
++)
1235 // Since the number of rows/columns can change as items are
1236 // inserted/deleted, we need to verify at runtime that the
1237 // requested growable rows/columns are still valid.
1238 if (m_growableRows
[idx
] >= nrows
)
1241 // If all items in a row/column are hidden, that row/column will
1242 // have a dimension of -1. This causes the row/column to be
1243 // hidden completely.
1244 if (m_rowHeights
[ m_growableRows
[idx
] ] == -1)
1246 sum_proportions
+= m_growableRowsProportions
[idx
];
1247 growable_space
+= m_rowHeights
[ m_growableRows
[idx
] ];
1253 for (idx
= 0; idx
< m_growableRows
.GetCount(); idx
++)
1255 if (m_growableRows
[idx
] >= nrows
)
1257 if (m_rowHeights
[ m_growableRows
[idx
] ] == -1)
1258 m_rowHeights
[ m_growableRows
[idx
] ] = 0;
1261 int delta
= (sz
.y
- minsz
.y
);
1262 if (sum_proportions
== 0)
1263 delta
= (delta
/num
) + m_rowHeights
[ m_growableRows
[idx
] ];
1265 delta
= ((delta
+growable_space
)*m_growableRowsProportions
[idx
]) / sum_proportions
;
1266 m_rowHeights
[ m_growableRows
[idx
] ] = delta
;
1271 else if ( (m_growMode
== wxFLEX_GROWMODE_ALL
) && (sz
.y
> minsz
.y
) )
1273 // rounding problem?
1274 for ( int row
= 0; row
< nrows
; ++row
)
1275 m_rowHeights
[ row
] = sz
.y
/ nrows
;
1278 // the same logic as above but for the columns
1279 if ( sz
.x
> minsz
.x
&& ( (m_flexDirection
& wxHORIZONTAL
) || (m_growMode
== wxFLEX_GROWMODE_SPECIFIED
) ) )
1281 int sum_proportions
= 0;
1282 int growable_space
= 0;
1285 for (idx
= 0; idx
< m_growableCols
.GetCount(); idx
++)
1287 // Since the number of rows/columns can change as items are
1288 // inserted/deleted, we need to verify at runtime that the
1289 // requested growable rows/columns are still valid.
1290 if (m_growableCols
[idx
] >= ncols
)
1293 // If all items in a row/column are hidden, that row/column will
1294 // have a dimension of -1. This causes the column to be hidden
1296 if (m_colWidths
[ m_growableCols
[idx
] ] == -1)
1298 sum_proportions
+= m_growableColsProportions
[idx
];
1299 growable_space
+= m_colWidths
[ m_growableCols
[idx
] ];
1305 for (idx
= 0; idx
< m_growableCols
.GetCount(); idx
++)
1307 if (m_growableCols
[idx
] >= ncols
)
1309 if (m_colWidths
[ m_growableCols
[idx
] ] == -1)
1310 m_colWidths
[ m_growableCols
[idx
] ] = 0;
1313 int delta
= (sz
.x
- minsz
.x
);
1314 if (sum_proportions
== 0)
1315 delta
= (delta
/num
) + m_colWidths
[ m_growableCols
[idx
] ];
1317 delta
= ((delta
+growable_space
)*m_growableColsProportions
[idx
])/sum_proportions
;
1318 m_colWidths
[ m_growableCols
[idx
] ] = delta
;
1323 else if ( (m_growMode
== wxFLEX_GROWMODE_ALL
) && (sz
.x
> minsz
.x
) )
1325 for ( int col
=0; col
< ncols
; ++col
)
1326 m_colWidths
[ col
] = sz
.x
/ ncols
;
1331 void wxFlexGridSizer::AddGrowableRow( size_t idx
, int proportion
)
1333 m_growableRows
.Add( idx
);
1334 m_growableRowsProportions
.Add( proportion
);
1337 void wxFlexGridSizer::RemoveGrowableRow( size_t idx
)
1339 m_growableRows
.Remove( idx
);
1342 void wxFlexGridSizer::AddGrowableCol( size_t idx
, int proportion
)
1344 m_growableCols
.Add( idx
);
1345 m_growableColsProportions
.Add( proportion
);
1348 void wxFlexGridSizer::RemoveGrowableCol( size_t idx
)
1350 m_growableCols
.Remove( idx
);
1353 //---------------------------------------------------------------------------
1355 //---------------------------------------------------------------------------
1357 wxBoxSizer::wxBoxSizer( int orient
)
1358 : m_orient( orient
)
1362 void wxBoxSizer::RecalcSizes()
1364 if (m_children
.GetCount() == 0)
1370 if (m_orient
== wxHORIZONTAL
)
1371 delta
= m_size
.x
- m_fixedWidth
;
1373 delta
= m_size
.y
- m_fixedHeight
;
1376 wxPoint
pt( m_position
);
1378 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
1381 wxSizerItem
*item
= node
->GetData();
1383 if (item
->IsShown())
1385 wxSize
size( item
->GetMinSizeWithBorder() );
1387 if (m_orient
== wxVERTICAL
)
1389 wxCoord height
= size
.y
;
1390 if (item
->GetProportion())
1392 // Because of at least one visible item has non-zero
1393 // proportion then m_stretchable is not zero
1394 height
= (delta
* item
->GetProportion()) / m_stretchable
;
1397 wxPoint
child_pos( pt
);
1398 wxSize
child_size( size
.x
, height
);
1400 if (item
->GetFlag() & (wxEXPAND
| wxSHAPED
))
1401 child_size
.x
= m_size
.x
;
1402 else if (item
->GetFlag() & wxALIGN_RIGHT
)
1403 child_pos
.x
+= m_size
.x
- size
.x
;
1404 else if (item
->GetFlag() & (wxCENTER
| wxALIGN_CENTER_HORIZONTAL
))
1405 // XXX wxCENTER is added for backward compatibility;
1406 // wxALIGN_CENTER should be used in new code
1407 child_pos
.x
+= (m_size
.x
- size
.x
) / 2;
1409 item
->SetDimension( child_pos
, child_size
);
1415 wxCoord width
= size
.x
;
1416 if (item
->GetProportion())
1418 // Because of at least one visible item has non-zero
1419 // proportion then m_stretchable is not zero
1420 width
= (delta
* item
->GetProportion()) / m_stretchable
;
1423 wxPoint
child_pos( pt
);
1424 wxSize
child_size( width
, size
.y
);
1426 if (item
->GetFlag() & (wxEXPAND
| wxSHAPED
))
1427 child_size
.y
= m_size
.y
;
1428 else if (item
->GetFlag() & wxALIGN_BOTTOM
)
1429 child_pos
.y
+= m_size
.y
- size
.y
;
1430 else if (item
->GetFlag() & (wxCENTER
| wxALIGN_CENTER_VERTICAL
))
1431 // XXX wxCENTER is added for backward compatibility;
1432 // wxALIGN_CENTER should be used in new code
1433 child_pos
.y
+= (m_size
.y
- size
.y
) / 2;
1435 item
->SetDimension( child_pos
, child_size
);
1441 node
= node
->GetNext();
1445 wxSize
wxBoxSizer::CalcMin()
1447 if (m_children
.GetCount() == 0)
1448 return wxSize(10,10);
1456 // precalc item minsizes and count proportions
1457 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
1460 wxSizerItem
*item
= node
->GetData();
1462 if (item
->IsShown())
1463 item
->CalcMin(); // result is stored in the item
1465 if (item
->IsShown() && item
->GetProportion() != 0)
1466 m_stretchable
+= item
->GetProportion();
1468 node
= node
->GetNext();
1471 // Total minimum size (width or height) of sizer
1474 node
= m_children
.GetFirst();
1477 wxSizerItem
*item
= node
->GetData();
1479 if (item
->IsShown() && item
->GetProportion() != 0)
1481 int stretch
= item
->GetProportion();
1482 wxSize
size( item
->GetMinSizeWithBorder() );
1485 // Integer division rounded up is (a + b - 1) / b
1486 // Round up needed in order to guarantee that all
1487 // all items will have size not less then their min size
1488 if (m_orient
== wxHORIZONTAL
)
1489 minSize
= ( size
.x
*m_stretchable
+ stretch
- 1)/stretch
;
1491 minSize
= ( size
.y
*m_stretchable
+ stretch
- 1)/stretch
;
1493 if (minSize
> maxMinSize
)
1494 maxMinSize
= minSize
;
1496 node
= node
->GetNext();
1499 // Calculate overall minimum size
1500 node
= m_children
.GetFirst();
1503 wxSizerItem
*item
= node
->GetData();
1505 if (item
->IsShown())
1507 wxSize
size( item
->GetMinSizeWithBorder() );
1508 if (item
->GetProportion() != 0)
1510 if (m_orient
== wxHORIZONTAL
)
1511 size
.x
= (maxMinSize
*item
->GetProportion())/m_stretchable
;
1513 size
.y
= (maxMinSize
*item
->GetProportion())/m_stretchable
;
1517 if (m_orient
== wxVERTICAL
)
1519 m_fixedHeight
+= size
.y
;
1520 m_fixedWidth
= wxMax( m_fixedWidth
, size
.x
);
1524 m_fixedWidth
+= size
.x
;
1525 m_fixedHeight
= wxMax( m_fixedHeight
, size
.y
);
1529 if (m_orient
== wxHORIZONTAL
)
1531 m_minWidth
+= size
.x
;
1532 m_minHeight
= wxMax( m_minHeight
, size
.y
);
1536 m_minHeight
+= size
.y
;
1537 m_minWidth
= wxMax( m_minWidth
, size
.x
);
1540 node
= node
->GetNext();
1543 return wxSize( m_minWidth
, m_minHeight
);
1546 //---------------------------------------------------------------------------
1548 //---------------------------------------------------------------------------
1552 wxStaticBoxSizer::wxStaticBoxSizer( wxStaticBox
*box
, int orient
)
1553 : wxBoxSizer( orient
)
1554 , m_staticBox( box
)
1556 wxASSERT_MSG( box
, wxT("wxStaticBoxSizer needs a static box") );
1559 static void GetStaticBoxBorders( wxStaticBox
*box
,
1563 // this has to be done platform by platform as there is no way to
1564 // guess the thickness of a wxStaticBox border
1566 box
->GetBordersForSizer(borderTop
,borderOther
);
1567 #elif defined(__WXMAC__)
1569 static int extraTop
= -1; // Uninitted
1570 static int other
= 5;
1572 if ( extraTop
== -1 )
1574 // The minimal border used for the top. Later on the staticbox'
1575 // font height is added to this.
1578 if ( UMAGetSystemVersion() >= 0x1030 /*Panther*/ )
1580 // As indicated by the HIG, Panther needs an extra border of 11
1581 // pixels (otherwise overlapping occurs at the top). The "other"
1582 // border has to be 11.
1589 *borderTop
= extraTop
+ box
->GetCharHeight();
1590 *borderOther
= other
;
1594 if ( box
->GetLabel().empty() )
1598 *borderTop
= box
->GetCharHeight();
1601 #endif // __WXCOCOA__
1604 void wxStaticBoxSizer::RecalcSizes()
1606 int top_border
, other_border
;
1607 GetStaticBoxBorders(m_staticBox
, &top_border
, &other_border
);
1609 m_staticBox
->SetSize( m_position
.x
, m_position
.y
, m_size
.x
, m_size
.y
);
1611 wxPoint
old_pos( m_position
);
1612 m_position
.x
+= other_border
;
1613 m_position
.y
+= top_border
;
1614 wxSize
old_size( m_size
);
1615 m_size
.x
-= 2*other_border
;
1616 m_size
.y
-= top_border
+ other_border
;
1618 wxBoxSizer::RecalcSizes();
1620 m_position
= old_pos
;
1624 wxSize
wxStaticBoxSizer::CalcMin()
1626 int top_border
, other_border
;
1627 GetStaticBoxBorders(m_staticBox
, &top_border
, &other_border
);
1629 wxSize
ret( wxBoxSizer::CalcMin() );
1630 ret
.x
+= 2*other_border
;
1631 ret
.y
+= other_border
+ top_border
;
1636 void wxStaticBoxSizer::ShowItems( bool show
)
1638 m_staticBox
->Show( show
);
1639 wxBoxSizer::ShowItems( show
);
1642 #endif // wxUSE_STATBOX
1644 wxStdDialogButtonSizer::wxStdDialogButtonSizer()
1645 : wxBoxSizer(wxHORIZONTAL
)
1647 bool is_pda
= (wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA
);
1649 // If we have a PDA screen, put yes/no button over
1650 // all other buttons, otherwise on the left side.
1652 m_orient
= wxVERTICAL
;
1654 m_buttonAffirmative
= NULL
;
1655 m_buttonApply
= NULL
;
1656 m_buttonNegative
= NULL
;
1657 m_buttonCancel
= NULL
;
1658 m_buttonHelp
= NULL
;
1661 void wxStdDialogButtonSizer::AddButton(wxButton
*mybutton
)
1663 switch (mybutton
->GetId())
1668 m_buttonAffirmative
= mybutton
;
1671 m_buttonApply
= mybutton
;
1674 m_buttonNegative
= mybutton
;
1677 m_buttonCancel
= mybutton
;
1680 m_buttonHelp
= mybutton
;
1687 void wxStdDialogButtonSizer::Finalise()
1690 Add(0, 0, 0, wxLEFT
, 6);
1692 Add((wxWindow
*)m_buttonHelp
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, 6);
1694 if (m_buttonNegative
){
1695 // HIG POLICE BULLETIN - destructive buttons need extra padding
1696 // 24 pixels on either side
1697 Add((wxWindow
*)m_buttonNegative
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, 12);
1700 // extra whitespace between help/negative and cancel/ok buttons
1701 Add(0, 0, 1, wxEXPAND
, 0);
1703 if (m_buttonCancel
){
1704 Add((wxWindow
*)m_buttonCancel
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, 6);
1705 // Cancel or help should be default
1706 // m_buttonCancel->SetDefaultButton();
1709 // Ugh, Mac doesn't really have apply dialogs, so I'll just
1710 // figure the best place is between Cancel and OK
1712 Add((wxWindow
*)m_buttonApply
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, 6);
1714 if (m_buttonAffirmative
){
1715 Add((wxWindow
*)m_buttonAffirmative
, 0, wxALIGN_CENTRE
| wxLEFT
, 6);
1717 if (m_buttonAffirmative
->GetId() == wxID_SAVE
){
1718 // these buttons have set labels under Mac so we should use them
1719 m_buttonAffirmative
->SetLabel(_("Save"));
1720 m_buttonNegative
->SetLabel(_("Don't Save"));
1724 // Extra space around and at the right
1726 #elif defined(__WXGTK20__)
1727 Add(0, 0, 0, wxLEFT
, 9);
1729 Add((wxWindow
*)m_buttonHelp
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, 3);
1731 // extra whitespace between help and cancel/ok buttons
1732 Add(0, 0, 1, wxEXPAND
, 0);
1734 if (m_buttonNegative
){
1735 Add((wxWindow
*)m_buttonNegative
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, 3);
1738 if (m_buttonCancel
){
1739 Add((wxWindow
*)m_buttonCancel
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, 3);
1740 // Cancel or help should be default
1741 // m_buttonCancel->SetDefaultButton();
1745 Add((wxWindow
*)m_buttonApply
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, 3);
1747 if (m_buttonAffirmative
)
1748 Add((wxWindow
*)m_buttonAffirmative
, 0, wxALIGN_CENTRE
| wxLEFT
, 6);
1750 // do the same thing for GTK1 and Windows platforms
1751 // and assume any platform not accounted for here will use
1753 Add(0, 0, 0, wxLEFT
, 9);
1755 Add((wxWindow
*)m_buttonHelp
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, m_buttonHelp
->ConvertDialogToPixels(wxSize(4, 0)).x
);
1757 // extra whitespace between help and cancel/ok buttons
1758 Add(0, 0, 1, wxEXPAND
, 0);
1761 Add((wxWindow
*)m_buttonApply
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, m_buttonApply
->ConvertDialogToPixels(wxSize(4, 0)).x
);
1763 if (m_buttonAffirmative
){
1764 Add((wxWindow
*)m_buttonAffirmative
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, m_buttonAffirmative
->ConvertDialogToPixels(wxSize(4, 0)).x
);
1767 if (m_buttonNegative
){
1768 Add((wxWindow
*)m_buttonNegative
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, m_buttonNegative
->ConvertDialogToPixels(wxSize(4, 0)).x
);
1771 if (m_buttonCancel
){
1772 Add((wxWindow
*)m_buttonCancel
, 0, wxALIGN_CENTRE
| wxLEFT
, m_buttonCancel
->ConvertDialogToPixels(wxSize(4, 0)).x
);
1773 // Cancel or help should be default
1774 // m_buttonCancel->SetDefaultButton();
1780 #if WXWIN_COMPATIBILITY_2_4
1782 // ----------------------------------------------------------------------------
1784 // ----------------------------------------------------------------------------
1787 IMPLEMENT_CLASS(wxBookCtrlSizer
, wxSizer
)
1789 IMPLEMENT_CLASS(wxNotebookSizer
, wxBookCtrlSizer
)
1790 #endif // wxUSE_NOTEBOOK
1791 #endif // wxUSE_BOOKCTRL
1795 wxBookCtrlSizer::wxBookCtrlSizer(wxBookCtrlBase
*bookctrl
)
1796 : m_bookctrl(bookctrl
)
1798 wxASSERT_MSG( bookctrl
, wxT("wxBookCtrlSizer needs a control") );
1801 void wxBookCtrlSizer::RecalcSizes()
1803 m_bookctrl
->SetSize( m_position
.x
, m_position
.y
, m_size
.x
, m_size
.y
);
1806 wxSize
wxBookCtrlSizer::CalcMin()
1808 wxSize sizeBorder
= m_bookctrl
->CalcSizeFromPage(wxSize(0, 0));
1813 if ( m_bookctrl
->GetPageCount() == 0 )
1815 return wxSize(sizeBorder
.x
+ 10, sizeBorder
.y
+ 10);
1821 wxWindowList::compatibility_iterator
1822 node
= m_bookctrl
->GetChildren().GetFirst();
1825 wxWindow
*item
= node
->GetData();
1826 wxSizer
*itemsizer
= item
->GetSizer();
1830 wxSize
subsize( itemsizer
->CalcMin() );
1832 if (subsize
.x
> maxX
)
1834 if (subsize
.y
> maxY
)
1838 node
= node
->GetNext();
1841 return wxSize( maxX
, maxY
) + sizeBorder
;
1846 wxNotebookSizer::wxNotebookSizer(wxNotebook
*nb
)
1848 wxASSERT_MSG( nb
, wxT("wxNotebookSizer needs a control") );
1852 #endif // wxUSE_NOTEBOOOK
1853 #endif // wxUSE_BOOKCTRL
1855 #endif // WXWIN_COMPATIBILITY_2_4