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
)
48 IMPLEMENT_CLASS(wxStdDialogButtonSizer
, wxBoxSizer
)
51 WX_DEFINE_EXPORTED_LIST( wxSizerItemList
);
86 //---------------------------------------------------------------------------
88 //---------------------------------------------------------------------------
90 void wxSizerItem::Init()
96 m_zoneRect
= wxRect( 0, 0, 0, 0 );
99 void wxSizerItem::Init(const wxSizerFlags
& flags
)
103 m_proportion
= flags
.GetProportion();
104 m_flag
= flags
.GetFlags();
105 m_border
= flags
.GetBorderInPixels();
108 wxSizerItem::wxSizerItem( int width
, int height
, int proportion
, int flag
, int border
, wxObject
* userData
)
111 , m_size( wxSize( width
, height
) ) // size is set directly
112 , m_minSize( m_size
) // minimal size is the initial size
113 , m_proportion( proportion
)
116 , m_zoneRect( 0, 0, 0, 0 )
118 , m_userData( userData
)
123 wxSizerItem::wxSizerItem( wxWindow
*window
, int proportion
, int flag
, int border
, wxObject
* userData
)
126 , m_proportion( proportion
)
129 , m_zoneRect( 0, 0, 0, 0 )
131 , m_userData( userData
)
133 if (flag
& wxFIXED_MINSIZE
)
134 window
->SetMinSize(window
->GetSize());
135 m_minSize
= window
->GetSize();
137 // aspect ratio calculated from initial size
138 SetRatio( m_minSize
);
140 // m_size is calculated later
143 wxSizerItem::wxSizerItem( wxSizer
*sizer
, int proportion
, int flag
, int border
, wxObject
* userData
)
146 , m_proportion( proportion
)
149 , m_zoneRect( 0, 0, 0, 0 )
152 , m_userData( userData
)
154 // m_minSize is calculated later
155 // m_size is calculated later
158 wxSizerItem::wxSizerItem()
167 wxSizerItem::~wxSizerItem()
173 m_window
->SetContainingSizer(NULL
);
175 else // we must be a sizer
182 wxSize
wxSizerItem::GetSize() const
186 ret
= m_sizer
->GetSize();
189 ret
= m_window
->GetSize();
196 if (m_flag
& wxNORTH
)
198 if (m_flag
& wxSOUTH
)
204 wxSize
wxSizerItem::CalcMin()
208 m_minSize
= m_sizer
->GetMinSize();
210 // if we have to preserve aspect ratio _AND_ this is
211 // the first-time calculation, consider ret to be initial size
212 if ((m_flag
& wxSHAPED
) && !m_ratio
)
215 else if ( IsWindow() )
217 // Since the size of the window may change during runtime, we
218 // should use the current minimal/best size.
219 m_minSize
= m_window
->GetBestFittingSize();
222 return GetMinSizeWithBorder();
225 wxSize
wxSizerItem::GetMinSizeWithBorder() const
227 wxSize ret
= m_minSize
;
233 if (m_flag
& wxNORTH
)
235 if (m_flag
& wxSOUTH
)
242 void wxSizerItem::SetDimension( wxPoint pos
, wxSize size
)
244 if (m_flag
& wxSHAPED
)
246 // adjust aspect ratio
247 int rwidth
= (int) (size
.y
* m_ratio
);
251 int rheight
= (int) (size
.x
/ m_ratio
);
252 // add vertical space
253 if (m_flag
& wxALIGN_CENTER_VERTICAL
)
254 pos
.y
+= (size
.y
- rheight
) / 2;
255 else if (m_flag
& wxALIGN_BOTTOM
)
256 pos
.y
+= (size
.y
- rheight
);
257 // use reduced dimensions
260 else if (rwidth
< size
.x
)
262 // add horizontal space
263 if (m_flag
& wxALIGN_CENTER_HORIZONTAL
)
264 pos
.x
+= (size
.x
- rwidth
) / 2;
265 else if (m_flag
& wxALIGN_RIGHT
)
266 pos
.x
+= (size
.x
- rwidth
);
271 // This is what GetPosition() returns. Since we calculate
272 // borders afterwards, GetPosition() will be the left/top
273 // corner of the surrounding border.
285 if (m_flag
& wxNORTH
)
290 if (m_flag
& wxSOUTH
)
296 m_sizer
->SetDimension( pos
.x
, pos
.y
, size
.x
, size
.y
);
298 m_zoneRect
= wxRect(pos
, size
);
300 m_window
->SetSize( pos
.x
, pos
.y
, size
.x
, size
.y
, wxSIZE_ALLOW_MINUS_ONE
);
305 void wxSizerItem::DeleteWindows()
314 m_sizer
->DeleteWindows();
317 bool wxSizerItem::IsWindow() const
319 return (m_window
!= NULL
);
322 bool wxSizerItem::IsSizer() const
324 return (m_sizer
!= NULL
);
327 bool wxSizerItem::IsSpacer() const
329 return (m_window
== NULL
) && (m_sizer
== NULL
);
332 void wxSizerItem::Show( bool show
)
337 m_window
->Show( show
);
339 m_sizer
->ShowItems( show
);
341 // ... nothing else to do to hide/show spacers
344 void wxSizerItem::SetOption( int option
)
346 SetProportion( option
);
349 int wxSizerItem::GetOption() const
351 return GetProportion();
355 //---------------------------------------------------------------------------
357 //---------------------------------------------------------------------------
360 : m_minSize( wxSize( 0, 0 ) )
366 WX_CLEAR_LIST(wxSizerItemList
, m_children
);
369 wxSizerItem
* wxSizer::Insert( size_t index
, wxSizerItem
*item
)
371 m_children
.Insert( index
, item
);
373 if( item
->GetWindow() )
374 item
->GetWindow()->SetContainingSizer( this );
379 bool wxSizer::Remove( wxWindow
*window
)
381 return Detach( window
);
384 bool wxSizer::Remove( wxSizer
*sizer
)
386 wxASSERT_MSG( sizer
, _T("Removing NULL sizer") );
388 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
391 wxSizerItem
*item
= node
->GetData();
393 if (item
->GetSizer() == sizer
)
396 m_children
.Erase( node
);
400 node
= node
->GetNext();
406 bool wxSizer::Remove( int index
)
408 wxCHECK_MSG( index
>= 0 && (size_t)index
< m_children
.GetCount(),
410 _T("Remove index is out of range") );
412 wxSizerItemList::compatibility_iterator node
= m_children
.Item( index
);
414 wxCHECK_MSG( node
, false, _T("Failed to find child node") );
416 wxSizerItem
*item
= node
->GetData();
418 if( item
->IsWindow() )
419 item
->GetWindow()->SetContainingSizer( NULL
);
422 m_children
.Erase( node
);
426 bool wxSizer::Detach( wxSizer
*sizer
)
428 wxASSERT_MSG( sizer
, _T("Detaching NULL sizer") );
430 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
433 wxSizerItem
*item
= node
->GetData();
435 if (item
->GetSizer() == sizer
)
439 m_children
.Erase( node
);
442 node
= node
->GetNext();
448 bool wxSizer::Detach( wxWindow
*window
)
450 wxASSERT_MSG( window
, _T("Detaching NULL window") );
452 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
455 wxSizerItem
*item
= node
->GetData();
457 if (item
->GetWindow() == window
)
459 item
->GetWindow()->SetContainingSizer( NULL
);
461 m_children
.Erase( node
);
464 node
= node
->GetNext();
470 bool wxSizer::Detach( int index
)
472 wxCHECK_MSG( index
>= 0 && (size_t)index
< m_children
.GetCount(),
474 _T("Detach index is out of range") );
476 wxSizerItemList::compatibility_iterator node
= m_children
.Item( index
);
478 wxCHECK_MSG( node
, false, _T("Failed to find child node") );
480 wxSizerItem
*item
= node
->GetData();
482 if( item
->IsSizer() )
484 else if( item
->IsWindow() )
485 item
->GetWindow()->SetContainingSizer( NULL
);
488 m_children
.Erase( node
);
492 void wxSizer::Clear( bool delete_windows
)
494 // First clear the ContainingSizer pointers
495 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
498 wxSizerItem
*item
= node
->GetData();
500 if (item
->IsWindow())
501 item
->GetWindow()->SetContainingSizer( NULL
);
502 node
= node
->GetNext();
505 // Destroy the windows if needed
509 // Now empty the list
510 WX_CLEAR_LIST(wxSizerItemList
, m_children
);
513 void wxSizer::DeleteWindows()
515 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
518 wxSizerItem
*item
= node
->GetData();
520 item
->DeleteWindows();
521 node
= node
->GetNext();
525 wxSize
wxSizer::Fit( wxWindow
*window
)
527 wxSize
size(window
->IsTopLevel() ? FitSize(window
)
528 : GetMinWindowSize(window
));
530 window
->SetSize( size
);
535 void wxSizer::FitInside( wxWindow
*window
)
538 if (window
->IsTopLevel())
539 size
= VirtualFitSize( window
);
541 size
= GetMinClientSize( window
);
543 window
->SetVirtualSize( size
);
546 void wxSizer::Layout()
548 // (re)calculates minimums needed for each item and other preparations
552 // Applies the layout and repositions/resizes the items
556 void wxSizer::SetSizeHints( wxWindow
*window
)
558 // Preserve the window's max size hints, but set the
559 // lower bound according to the sizer calculations.
561 wxSize size
= Fit( window
);
563 window
->SetSizeHints( size
.x
,
565 window
->GetMaxWidth(),
566 window
->GetMaxHeight() );
569 void wxSizer::SetVirtualSizeHints( wxWindow
*window
)
571 // Preserve the window's max size hints, but set the
572 // lower bound according to the sizer calculations.
575 wxSize
size( window
->GetVirtualSize() );
576 window
->SetVirtualSizeHints( size
.x
,
578 window
->GetMaxWidth(),
579 window
->GetMaxHeight() );
582 wxSize
wxSizer::GetMaxWindowSize( wxWindow
*window
) const
584 return window
->GetMaxSize();
587 wxSize
wxSizer::GetMinWindowSize( wxWindow
*window
)
589 wxSize
minSize( GetMinSize() );
590 wxSize
size( window
->GetSize() );
591 wxSize
client_size( window
->GetClientSize() );
593 return wxSize( minSize
.x
+size
.x
-client_size
.x
,
594 minSize
.y
+size
.y
-client_size
.y
);
597 // TODO on mac we need a function that determines how much free space this
598 // min size contains, in order to make sure that we have 20 pixels of free
599 // space around the controls
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
!= wxDefaultCoord
)
611 if ( size
.y
> sizeMax
.y
&& sizeMax
.y
!= wxDefaultCoord
)
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
!= wxDefaultCoord
)
647 if ( size
.y
> sizeMax
.y
&& sizeMax
.y
!= wxDefaultCoord
)
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
->SetMinSize( 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 is stored via the item
768 item
->SetMinSize( width
, height
);
774 wxSizerItem
* wxSizer::GetItem( wxWindow
*window
, bool recursive
)
776 wxASSERT_MSG( window
, _T("GetItem for NULL window") );
778 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
781 wxSizerItem
*item
= node
->GetData();
783 if (item
->GetWindow() == window
)
787 else if (recursive
&& item
->IsSizer())
789 wxSizerItem
*subitem
= item
->GetSizer()->GetItem( window
, true );
794 node
= node
->GetNext();
800 wxSizerItem
* wxSizer::GetItem( wxSizer
*sizer
, bool recursive
)
802 wxASSERT_MSG( sizer
, _T("GetItem for NULL sizer") );
804 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
807 wxSizerItem
*item
= node
->GetData();
809 if (item
->GetSizer() == sizer
)
813 else if (recursive
&& item
->IsSizer())
815 wxSizerItem
*subitem
= item
->GetSizer()->GetItem( sizer
, true );
820 node
= node
->GetNext();
826 wxSizerItem
* wxSizer::GetItem( size_t index
)
828 wxCHECK_MSG( index
< m_children
.GetCount(),
830 _T("GetItem index is out of range") );
832 return m_children
.Item( index
)->GetData();
835 bool wxSizer::Show( wxWindow
*window
, bool show
, bool recursive
)
837 wxSizerItem
*item
= GetItem( window
, recursive
);
848 bool wxSizer::Show( wxSizer
*sizer
, bool show
, bool recursive
)
850 wxSizerItem
*item
= GetItem( sizer
, recursive
);
861 bool wxSizer::Show( size_t index
, bool show
)
863 wxSizerItem
*item
= GetItem( index
);
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
->GetMinSizeWithBorder() );
1045 int flag
= item
->GetFlag();
1047 if ((flag
& wxEXPAND
) || (flag
& wxSHAPED
))
1053 if (flag
& wxALIGN_CENTER_HORIZONTAL
)
1055 pt
.x
= x
+ (w
- sz
.x
) / 2;
1057 else if (flag
& wxALIGN_RIGHT
)
1059 pt
.x
= x
+ (w
- sz
.x
);
1062 if (flag
& wxALIGN_CENTER_VERTICAL
)
1064 pt
.y
= y
+ (h
- sz
.y
) / 2;
1066 else if (flag
& wxALIGN_BOTTOM
)
1068 pt
.y
= y
+ (h
- sz
.y
);
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() );
1106 AdjustForGrowables(sz
, m_calculatedMinSize
, nrows
, ncols
);
1108 sz
= wxSize( pt
.x
+ sz
.x
, pt
.y
+ sz
.y
);
1111 for (int c
= 0; c
< ncols
; c
++)
1114 for (int r
= 0; r
< nrows
; r
++)
1116 int i
= r
* ncols
+ c
;
1119 wxSizerItemList::compatibility_iterator node
= m_children
.Item( i
);
1121 wxASSERT_MSG( node
, _T("Failed to find node") );
1123 int w
= wxMax( 0, wxMin( m_colWidths
[c
], sz
.x
- x
) );
1124 int h
= wxMax( 0, wxMin( m_rowHeights
[r
], sz
.y
- y
) );
1126 SetItemBounds( node
->GetData(), x
, y
, w
, h
);
1128 y
= y
+ m_rowHeights
[r
] + m_vgap
;
1130 x
= x
+ m_colWidths
[c
] + m_hgap
;
1134 wxSize
wxFlexGridSizer::CalcMin()
1140 // Number of rows/columns can change as items are added or removed.
1141 if ( !CalcRowsCols(nrows
, ncols
) )
1142 return wxSize(10, 10);
1144 m_rowHeights
.SetCount(nrows
);
1145 m_colWidths
.SetCount(ncols
);
1147 // We have to recalcuate the sizes in case the item minimum size has
1148 // changed since the previous layout, or the item has been hidden using
1149 // wxSizer::Show(). If all the items in a row/column are hidden, the final
1150 // dimension of the row/column will be -1, indicating that the column
1151 // itself is hidden.
1152 for( s
= m_rowHeights
.GetCount(), i
= 0; i
< s
; ++i
)
1153 m_rowHeights
[ i
] = -1;
1154 for( s
= m_colWidths
.GetCount(), i
= 0; i
< s
; ++i
)
1155 m_colWidths
[ i
] = -1;
1157 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
1162 wxSizerItem
*item
= node
->GetData();
1163 if ( item
->IsShown() )
1165 wxSize
sz( item
->CalcMin() );
1166 int row
= i
/ ncols
;
1167 int col
= i
% ncols
;
1169 m_rowHeights
[ row
] = wxMax( wxMax( 0, sz
.y
), m_rowHeights
[ row
] );
1170 m_colWidths
[ col
] = wxMax( wxMax( 0, sz
.x
), m_colWidths
[ col
] );
1173 node
= node
->GetNext();
1177 AdjustForFlexDirection();
1179 // Sum total minimum size, including gaps between rows/columns.
1180 // -1 is used as a magic number meaning empty column.
1182 for (int col
= 0; col
< ncols
; col
++)
1183 if ( m_colWidths
[ col
] != -1 )
1184 width
+= m_colWidths
[ col
] + ( col
== ncols
-1 ? 0 : m_hgap
);
1187 for (int row
= 0; row
< nrows
; row
++)
1188 if ( m_rowHeights
[ row
] != -1 )
1189 height
+= m_rowHeights
[ row
] + ( row
== nrows
-1 ? 0 : m_vgap
);
1191 m_calculatedMinSize
= wxSize( width
, height
);
1192 return m_calculatedMinSize
;
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
->GetMinSizeWithBorder() );
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( 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( 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 // precalc item minsizes and count proportions
1459 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
1462 wxSizerItem
*item
= node
->GetData();
1464 if (item
->IsShown())
1465 item
->CalcMin(); // result is stored in the item
1467 if (item
->IsShown() && item
->GetProportion() != 0)
1468 m_stretchable
+= item
->GetProportion();
1470 node
= node
->GetNext();
1473 // Total minimum size (width or height) of sizer
1476 node
= m_children
.GetFirst();
1479 wxSizerItem
*item
= node
->GetData();
1481 if (item
->IsShown() && item
->GetProportion() != 0)
1483 int stretch
= item
->GetProportion();
1484 wxSize
size( item
->GetMinSizeWithBorder() );
1487 // Integer division rounded up is (a + b - 1) / b
1488 // Round up needed in order to guarantee that all
1489 // all items will have size not less then their min size
1490 if (m_orient
== wxHORIZONTAL
)
1491 minSize
= ( size
.x
*m_stretchable
+ stretch
- 1)/stretch
;
1493 minSize
= ( size
.y
*m_stretchable
+ stretch
- 1)/stretch
;
1495 if (minSize
> maxMinSize
)
1496 maxMinSize
= minSize
;
1498 node
= node
->GetNext();
1501 // Calculate overall minimum size
1502 node
= m_children
.GetFirst();
1505 wxSizerItem
*item
= node
->GetData();
1507 if (item
->IsShown())
1509 wxSize
size( item
->GetMinSizeWithBorder() );
1510 if (item
->GetProportion() != 0)
1512 if (m_orient
== wxHORIZONTAL
)
1513 size
.x
= (maxMinSize
*item
->GetProportion())/m_stretchable
;
1515 size
.y
= (maxMinSize
*item
->GetProportion())/m_stretchable
;
1519 if (m_orient
== wxVERTICAL
)
1521 m_fixedHeight
+= size
.y
;
1522 m_fixedWidth
= wxMax( m_fixedWidth
, size
.x
);
1526 m_fixedWidth
+= size
.x
;
1527 m_fixedHeight
= wxMax( m_fixedHeight
, size
.y
);
1531 if (m_orient
== wxHORIZONTAL
)
1533 m_minWidth
+= size
.x
;
1534 m_minHeight
= wxMax( m_minHeight
, size
.y
);
1538 m_minHeight
+= size
.y
;
1539 m_minWidth
= wxMax( m_minWidth
, size
.x
);
1542 node
= node
->GetNext();
1545 return wxSize( m_minWidth
, m_minHeight
);
1548 //---------------------------------------------------------------------------
1550 //---------------------------------------------------------------------------
1554 wxStaticBoxSizer::wxStaticBoxSizer( wxStaticBox
*box
, int orient
)
1555 : wxBoxSizer( orient
)
1556 , m_staticBox( box
)
1558 wxASSERT_MSG( box
, wxT("wxStaticBoxSizer needs a static box") );
1561 static void GetStaticBoxBorders( wxStaticBox
*box
,
1565 // this has to be done platform by platform as there is no way to
1566 // guess the thickness of a wxStaticBox border
1568 box
->GetBordersForSizer(borderTop
,borderOther
);
1569 #elif defined(__WXMAC__)
1571 static int extraTop
= -1; // Uninitted
1572 static int other
= 5;
1574 if ( extraTop
== -1 )
1576 // The minimal border used for the top. Later on the staticbox'
1577 // font height is added to this.
1580 if ( UMAGetSystemVersion() >= 0x1030 /*Panther*/ )
1582 // As indicated by the HIG, Panther needs an extra border of 11
1583 // pixels (otherwise overlapping occurs at the top). The "other"
1584 // border has to be 11.
1591 *borderTop
= extraTop
+ box
->GetCharHeight();
1592 *borderOther
= other
;
1596 if ( box
->GetLabel().empty() )
1600 *borderTop
= box
->GetCharHeight();
1603 #endif // __WXCOCOA__
1606 void wxStaticBoxSizer::RecalcSizes()
1608 int top_border
, other_border
;
1609 GetStaticBoxBorders(m_staticBox
, &top_border
, &other_border
);
1611 m_staticBox
->SetSize( m_position
.x
, m_position
.y
, m_size
.x
, m_size
.y
);
1613 wxPoint
old_pos( m_position
);
1614 m_position
.x
+= other_border
;
1615 m_position
.y
+= top_border
;
1616 wxSize
old_size( m_size
);
1617 m_size
.x
-= 2*other_border
;
1618 m_size
.y
-= top_border
+ other_border
;
1620 wxBoxSizer::RecalcSizes();
1622 m_position
= old_pos
;
1626 wxSize
wxStaticBoxSizer::CalcMin()
1628 int top_border
, other_border
;
1629 GetStaticBoxBorders(m_staticBox
, &top_border
, &other_border
);
1631 wxSize
ret( wxBoxSizer::CalcMin() );
1632 ret
.x
+= 2*other_border
;
1633 ret
.y
+= other_border
+ top_border
;
1638 void wxStaticBoxSizer::ShowItems( bool show
)
1640 m_staticBox
->Show( show
);
1641 wxBoxSizer::ShowItems( show
);
1644 #endif // wxUSE_STATBOX
1648 wxStdDialogButtonSizer::wxStdDialogButtonSizer()
1649 : wxBoxSizer(wxHORIZONTAL
)
1651 bool is_pda
= (wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA
);
1653 // If we have a PDA screen, put yes/no button over
1654 // all other buttons, otherwise on the left side.
1656 m_orient
= wxVERTICAL
;
1658 m_buttonAffirmative
= NULL
;
1659 m_buttonApply
= NULL
;
1660 m_buttonNegative
= NULL
;
1661 m_buttonCancel
= NULL
;
1662 m_buttonHelp
= NULL
;
1665 void wxStdDialogButtonSizer::AddButton(wxButton
*mybutton
)
1667 switch (mybutton
->GetId())
1672 m_buttonAffirmative
= mybutton
;
1675 m_buttonApply
= mybutton
;
1678 m_buttonNegative
= mybutton
;
1681 m_buttonCancel
= mybutton
;
1684 m_buttonHelp
= mybutton
;
1691 void wxStdDialogButtonSizer::SetAffirmativeButton( wxButton
*button
)
1693 m_buttonAffirmative
= button
;
1696 void wxStdDialogButtonSizer::SetNegativeButton( wxButton
*button
)
1698 m_buttonNegative
= button
;
1701 void wxStdDialogButtonSizer::SetCancelButton( wxButton
*button
)
1703 m_buttonCancel
= button
;
1706 void wxStdDialogButtonSizer::Finalise()
1709 Add(0, 0, 0, wxLEFT
, 6);
1711 Add((wxWindow
*)m_buttonHelp
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, 6);
1713 if (m_buttonNegative
){
1714 // HIG POLICE BULLETIN - destructive buttons need extra padding
1715 // 24 pixels on either side
1716 Add((wxWindow
*)m_buttonNegative
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, 12);
1719 // extra whitespace between help/negative and cancel/ok buttons
1720 Add(0, 0, 1, wxEXPAND
, 0);
1722 if (m_buttonCancel
){
1723 Add((wxWindow
*)m_buttonCancel
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, 6);
1724 // Cancel or help should be default
1725 // m_buttonCancel->SetDefaultButton();
1728 // Ugh, Mac doesn't really have apply dialogs, so I'll just
1729 // figure the best place is between Cancel and OK
1731 Add((wxWindow
*)m_buttonApply
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, 6);
1733 if (m_buttonAffirmative
){
1734 Add((wxWindow
*)m_buttonAffirmative
, 0, wxALIGN_CENTRE
| wxLEFT
, 6);
1736 if (m_buttonAffirmative
->GetId() == wxID_SAVE
){
1737 // these buttons have set labels under Mac so we should use them
1738 m_buttonAffirmative
->SetLabel(_("Save"));
1739 m_buttonNegative
->SetLabel(_("Don't Save"));
1743 // Extra space around and at the right
1745 #elif defined(__WXGTK20__)
1746 Add(0, 0, 0, wxLEFT
, 9);
1748 Add((wxWindow
*)m_buttonHelp
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, 3);
1750 // extra whitespace between help and cancel/ok buttons
1751 Add(0, 0, 1, wxEXPAND
, 0);
1753 if (m_buttonNegative
){
1754 Add((wxWindow
*)m_buttonNegative
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, 3);
1757 if (m_buttonCancel
){
1758 Add((wxWindow
*)m_buttonCancel
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, 3);
1759 // Cancel or help should be default
1760 // m_buttonCancel->SetDefaultButton();
1764 Add((wxWindow
*)m_buttonApply
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, 3);
1766 if (m_buttonAffirmative
)
1767 Add((wxWindow
*)m_buttonAffirmative
, 0, wxALIGN_CENTRE
| wxLEFT
, 6);
1769 // do the same thing for GTK1 and Windows platforms
1770 // and assume any platform not accounted for here will use
1772 Add(0, 0, 0, wxLEFT
, 9);
1774 Add((wxWindow
*)m_buttonHelp
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, m_buttonHelp
->ConvertDialogToPixels(wxSize(4, 0)).x
);
1776 // extra whitespace between help and cancel/ok buttons
1777 Add(0, 0, 1, wxEXPAND
, 0);
1780 Add((wxWindow
*)m_buttonApply
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, m_buttonApply
->ConvertDialogToPixels(wxSize(4, 0)).x
);
1782 if (m_buttonAffirmative
){
1783 Add((wxWindow
*)m_buttonAffirmative
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, m_buttonAffirmative
->ConvertDialogToPixels(wxSize(4, 0)).x
);
1786 if (m_buttonNegative
){
1787 Add((wxWindow
*)m_buttonNegative
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, m_buttonNegative
->ConvertDialogToPixels(wxSize(4, 0)).x
);
1790 if (m_buttonCancel
){
1791 Add((wxWindow
*)m_buttonCancel
, 0, wxALIGN_CENTRE
| wxLEFT
, m_buttonCancel
->ConvertDialogToPixels(wxSize(4, 0)).x
);
1792 // Cancel or help should be default
1793 // m_buttonCancel->SetDefaultButton();
1799 #endif // wxUSE_BUTTON
1801 #if WXWIN_COMPATIBILITY_2_4
1803 // ----------------------------------------------------------------------------
1805 // ----------------------------------------------------------------------------
1808 IMPLEMENT_CLASS(wxBookCtrlSizer
, wxSizer
)
1810 IMPLEMENT_CLASS(wxNotebookSizer
, wxBookCtrlSizer
)
1811 #endif // wxUSE_NOTEBOOK
1812 #endif // wxUSE_BOOKCTRL
1816 wxBookCtrlSizer::wxBookCtrlSizer(wxBookCtrlBase
*bookctrl
)
1817 : m_bookctrl(bookctrl
)
1819 wxASSERT_MSG( bookctrl
, wxT("wxBookCtrlSizer needs a control") );
1822 void wxBookCtrlSizer::RecalcSizes()
1824 m_bookctrl
->SetSize( m_position
.x
, m_position
.y
, m_size
.x
, m_size
.y
);
1827 wxSize
wxBookCtrlSizer::CalcMin()
1829 wxSize sizeBorder
= m_bookctrl
->CalcSizeFromPage(wxSize(0, 0));
1834 if ( m_bookctrl
->GetPageCount() == 0 )
1836 return wxSize(sizeBorder
.x
+ 10, sizeBorder
.y
+ 10);
1842 wxWindowList::compatibility_iterator
1843 node
= m_bookctrl
->GetChildren().GetFirst();
1846 wxWindow
*item
= node
->GetData();
1847 wxSizer
*itemsizer
= item
->GetSizer();
1851 wxSize
subsize( itemsizer
->CalcMin() );
1853 if (subsize
.x
> maxX
)
1855 if (subsize
.y
> maxY
)
1859 node
= node
->GetNext();
1862 return wxSize( maxX
, maxY
) + sizeBorder
;
1867 wxNotebookSizer::wxNotebookSizer(wxNotebook
*nb
)
1869 wxASSERT_MSG( nb
, wxT("wxNotebookSizer needs a control") );
1873 #endif // wxUSE_NOTEBOOOK
1874 #endif // wxUSE_BOOKCTRL
1876 #endif // WXWIN_COMPATIBILITY_2_4