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"
25 #include "wx/string.h"
31 #include "wx/statbox.h"
32 #include "wx/settings.h"
33 #include "wx/listimpl.cpp"
34 #if WXWIN_COMPATIBILITY_2_4
35 #include "wx/notebook.h"
39 # include "wx/mac/uma.h"
42 //---------------------------------------------------------------------------
44 IMPLEMENT_CLASS(wxSizerItem
, wxObject
)
45 IMPLEMENT_CLASS(wxSizer
, wxObject
)
46 IMPLEMENT_CLASS(wxGridSizer
, wxSizer
)
47 IMPLEMENT_CLASS(wxFlexGridSizer
, wxGridSizer
)
48 IMPLEMENT_CLASS(wxBoxSizer
, wxSizer
)
50 IMPLEMENT_CLASS(wxStaticBoxSizer
, wxBoxSizer
)
53 IMPLEMENT_CLASS(wxStdDialogButtonSizer
, wxBoxSizer
)
56 WX_DEFINE_EXPORTED_LIST( wxSizerItemList
);
91 //---------------------------------------------------------------------------
93 //---------------------------------------------------------------------------
95 void wxSizerItem::Init()
101 m_zoneRect
= wxRect( 0, 0, 0, 0 );
104 void wxSizerItem::Init(const wxSizerFlags
& flags
)
108 m_proportion
= flags
.GetProportion();
109 m_flag
= flags
.GetFlags();
110 m_border
= flags
.GetBorderInPixels();
113 wxSizerItem::wxSizerItem( int width
, int height
, int proportion
, int flag
, int border
, wxObject
* userData
)
116 , m_size( wxSize( width
, height
) ) // size is set directly
117 , m_minSize( m_size
) // minimal size is the initial size
118 , m_proportion( proportion
)
121 , m_zoneRect( 0, 0, 0, 0 )
123 , m_userData( userData
)
128 wxSizerItem::wxSizerItem( wxWindow
*window
, int proportion
, int flag
, int border
, wxObject
* userData
)
131 , m_proportion( proportion
)
134 , m_zoneRect( 0, 0, 0, 0 )
136 , m_userData( userData
)
138 if (flag
& wxFIXED_MINSIZE
)
139 window
->SetMinSize(window
->GetSize());
140 m_minSize
= window
->GetSize();
142 // aspect ratio calculated from initial size
143 SetRatio( m_minSize
);
145 // m_size is calculated later
148 wxSizerItem::wxSizerItem( wxSizer
*sizer
, int proportion
, int flag
, int border
, wxObject
* userData
)
151 , m_proportion( proportion
)
154 , m_zoneRect( 0, 0, 0, 0 )
157 , m_userData( userData
)
159 // m_minSize is calculated later
160 // m_size is calculated later
163 wxSizerItem::wxSizerItem()
172 wxSizerItem::~wxSizerItem()
178 m_window
->SetContainingSizer(NULL
);
180 else // we must be a sizer
187 wxSize
wxSizerItem::GetSize() const
191 ret
= m_sizer
->GetSize();
194 ret
= m_window
->GetSize();
201 if (m_flag
& wxNORTH
)
203 if (m_flag
& wxSOUTH
)
209 wxSize
wxSizerItem::CalcMin()
213 m_minSize
= m_sizer
->GetMinSize();
215 // if we have to preserve aspect ratio _AND_ this is
216 // the first-time calculation, consider ret to be initial size
217 if ((m_flag
& wxSHAPED
) && !m_ratio
)
220 else if ( IsWindow() )
222 // Since the size of the window may change during runtime, we
223 // should use the current minimal/best size.
224 m_minSize
= m_window
->GetBestFittingSize();
227 return GetMinSizeWithBorder();
230 wxSize
wxSizerItem::GetMinSizeWithBorder() const
232 wxSize ret
= m_minSize
;
238 if (m_flag
& wxNORTH
)
240 if (m_flag
& wxSOUTH
)
247 void wxSizerItem::SetDimension( wxPoint pos
, wxSize size
)
249 if (m_flag
& wxSHAPED
)
251 // adjust aspect ratio
252 int rwidth
= (int) (size
.y
* m_ratio
);
256 int rheight
= (int) (size
.x
/ m_ratio
);
257 // add vertical space
258 if (m_flag
& wxALIGN_CENTER_VERTICAL
)
259 pos
.y
+= (size
.y
- rheight
) / 2;
260 else if (m_flag
& wxALIGN_BOTTOM
)
261 pos
.y
+= (size
.y
- rheight
);
262 // use reduced dimensions
265 else if (rwidth
< size
.x
)
267 // add horizontal space
268 if (m_flag
& wxALIGN_CENTER_HORIZONTAL
)
269 pos
.x
+= (size
.x
- rwidth
) / 2;
270 else if (m_flag
& wxALIGN_RIGHT
)
271 pos
.x
+= (size
.x
- rwidth
);
276 // This is what GetPosition() returns. Since we calculate
277 // borders afterwards, GetPosition() will be the left/top
278 // corner of the surrounding border.
290 if (m_flag
& wxNORTH
)
295 if (m_flag
& wxSOUTH
)
301 m_sizer
->SetDimension( pos
.x
, pos
.y
, size
.x
, size
.y
);
303 m_zoneRect
= wxRect(pos
, size
);
305 m_window
->SetSize( pos
.x
, pos
.y
, size
.x
, size
.y
, wxSIZE_ALLOW_MINUS_ONE
);
310 void wxSizerItem::DeleteWindows()
319 m_sizer
->DeleteWindows();
322 bool wxSizerItem::IsWindow() const
324 return (m_window
!= NULL
);
327 bool wxSizerItem::IsSizer() const
329 return (m_sizer
!= NULL
);
332 bool wxSizerItem::IsSpacer() const
334 return (m_window
== NULL
) && (m_sizer
== NULL
);
337 void wxSizerItem::Show( bool show
)
342 m_window
->Show( show
);
344 m_sizer
->ShowItems( show
);
346 // ... nothing else to do to hide/show spacers
349 void wxSizerItem::SetOption( int option
)
351 SetProportion( option
);
354 int wxSizerItem::GetOption() const
356 return GetProportion();
360 //---------------------------------------------------------------------------
362 //---------------------------------------------------------------------------
365 : m_minSize( wxSize( 0, 0 ) )
371 WX_CLEAR_LIST(wxSizerItemList
, m_children
);
374 wxSizerItem
* wxSizer::Insert( size_t index
, wxSizerItem
*item
)
376 m_children
.Insert( index
, item
);
378 if( item
->GetWindow() )
379 item
->GetWindow()->SetContainingSizer( this );
384 bool wxSizer::Remove( wxWindow
*window
)
386 return Detach( window
);
389 bool wxSizer::Remove( wxSizer
*sizer
)
391 wxASSERT_MSG( sizer
, _T("Removing NULL sizer") );
393 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
396 wxSizerItem
*item
= node
->GetData();
398 if (item
->GetSizer() == sizer
)
401 m_children
.Erase( node
);
405 node
= node
->GetNext();
411 bool wxSizer::Remove( int index
)
413 wxCHECK_MSG( index
>= 0 && (size_t)index
< m_children
.GetCount(),
415 _T("Remove index is out of range") );
417 wxSizerItemList::compatibility_iterator node
= m_children
.Item( index
);
419 wxCHECK_MSG( node
, false, _T("Failed to find child node") );
421 wxSizerItem
*item
= node
->GetData();
423 if( item
->IsWindow() )
424 item
->GetWindow()->SetContainingSizer( NULL
);
427 m_children
.Erase( node
);
431 bool wxSizer::Detach( wxSizer
*sizer
)
433 wxASSERT_MSG( sizer
, _T("Detaching NULL sizer") );
435 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
438 wxSizerItem
*item
= node
->GetData();
440 if (item
->GetSizer() == sizer
)
444 m_children
.Erase( node
);
447 node
= node
->GetNext();
453 bool wxSizer::Detach( wxWindow
*window
)
455 wxASSERT_MSG( window
, _T("Detaching NULL window") );
457 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
460 wxSizerItem
*item
= node
->GetData();
462 if (item
->GetWindow() == window
)
464 item
->GetWindow()->SetContainingSizer( NULL
);
466 m_children
.Erase( node
);
469 node
= node
->GetNext();
475 bool wxSizer::Detach( int index
)
477 wxCHECK_MSG( index
>= 0 && (size_t)index
< m_children
.GetCount(),
479 _T("Detach index is out of range") );
481 wxSizerItemList::compatibility_iterator node
= m_children
.Item( index
);
483 wxCHECK_MSG( node
, false, _T("Failed to find child node") );
485 wxSizerItem
*item
= node
->GetData();
487 if( item
->IsSizer() )
489 else if( item
->IsWindow() )
490 item
->GetWindow()->SetContainingSizer( NULL
);
493 m_children
.Erase( node
);
497 void wxSizer::Clear( bool delete_windows
)
499 // First clear the ContainingSizer pointers
500 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
503 wxSizerItem
*item
= node
->GetData();
505 if (item
->IsWindow())
506 item
->GetWindow()->SetContainingSizer( NULL
);
507 node
= node
->GetNext();
510 // Destroy the windows if needed
514 // Now empty the list
515 WX_CLEAR_LIST(wxSizerItemList
, m_children
);
518 void wxSizer::DeleteWindows()
520 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
523 wxSizerItem
*item
= node
->GetData();
525 item
->DeleteWindows();
526 node
= node
->GetNext();
530 wxSize
wxSizer::Fit( wxWindow
*window
)
532 wxSize
size(window
->IsTopLevel() ? FitSize(window
)
533 : GetMinWindowSize(window
));
535 window
->SetSize( size
);
540 void wxSizer::FitInside( wxWindow
*window
)
543 if (window
->IsTopLevel())
544 size
= VirtualFitSize( window
);
546 size
= GetMinClientSize( window
);
548 window
->SetVirtualSize( size
);
551 void wxSizer::Layout()
553 // (re)calculates minimums needed for each item and other preparations
557 // Applies the layout and repositions/resizes the items
561 void wxSizer::SetSizeHints( wxWindow
*window
)
563 // Preserve the window's max size hints, but set the
564 // lower bound according to the sizer calculations.
566 wxSize size
= Fit( window
);
568 window
->SetSizeHints( size
.x
,
570 window
->GetMaxWidth(),
571 window
->GetMaxHeight() );
574 void wxSizer::SetVirtualSizeHints( wxWindow
*window
)
576 // Preserve the window's max size hints, but set the
577 // lower bound according to the sizer calculations.
580 wxSize
size( window
->GetVirtualSize() );
581 window
->SetVirtualSizeHints( size
.x
,
583 window
->GetMaxWidth(),
584 window
->GetMaxHeight() );
587 wxSize
wxSizer::GetMaxWindowSize( wxWindow
*window
) const
589 return window
->GetMaxSize();
592 wxSize
wxSizer::GetMinWindowSize( wxWindow
*window
)
594 wxSize
minSize( GetMinSize() );
595 wxSize
size( window
->GetSize() );
596 wxSize
client_size( window
->GetClientSize() );
598 return wxSize( minSize
.x
+size
.x
-client_size
.x
,
599 minSize
.y
+size
.y
-client_size
.y
);
602 // TODO on mac we need a function that determines how much free space this
603 // min size contains, in order to make sure that we have 20 pixels of free
604 // space around the controls
606 // Return a window size that will fit within the screens dimensions
607 wxSize
wxSizer::FitSize( wxWindow
*window
)
609 wxSize size
= GetMinWindowSize( window
);
610 wxSize sizeMax
= GetMaxWindowSize( window
);
612 // Limit the size if sizeMax != wxDefaultSize
614 if ( size
.x
> sizeMax
.x
&& sizeMax
.x
!= wxDefaultCoord
)
616 if ( size
.y
> sizeMax
.y
&& sizeMax
.y
!= wxDefaultCoord
)
622 wxSize
wxSizer::GetMaxClientSize( wxWindow
*window
) const
624 wxSize
maxSize( window
->GetMaxSize() );
626 if( maxSize
!= wxDefaultSize
)
628 wxSize
size( window
->GetSize() );
629 wxSize
client_size( window
->GetClientSize() );
631 return wxSize( maxSize
.x
+ client_size
.x
- size
.x
,
632 maxSize
.y
+ client_size
.y
- size
.y
);
635 return wxDefaultSize
;
638 wxSize
wxSizer::GetMinClientSize( wxWindow
*WXUNUSED(window
) )
640 return GetMinSize(); // Already returns client size.
643 wxSize
wxSizer::VirtualFitSize( wxWindow
*window
)
645 wxSize size
= GetMinClientSize( window
);
646 wxSize sizeMax
= GetMaxClientSize( window
);
648 // Limit the size if sizeMax != wxDefaultSize
650 if ( size
.x
> sizeMax
.x
&& sizeMax
.x
!= wxDefaultCoord
)
652 if ( size
.y
> sizeMax
.y
&& sizeMax
.y
!= wxDefaultCoord
)
658 void wxSizer::SetDimension( int x
, int y
, int width
, int height
)
667 wxSize
wxSizer::GetMinSize()
669 wxSize
ret( CalcMin() );
670 if (ret
.x
< m_minSize
.x
) ret
.x
= m_minSize
.x
;
671 if (ret
.y
< m_minSize
.y
) ret
.y
= m_minSize
.y
;
675 void wxSizer::DoSetMinSize( int width
, int height
)
678 m_minSize
.y
= height
;
681 bool wxSizer::DoSetItemMinSize( wxWindow
*window
, int width
, int height
)
683 wxASSERT_MSG( window
, _T("SetMinSize for NULL window") );
685 // Is it our immediate child?
687 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
690 wxSizerItem
*item
= node
->GetData();
692 if (item
->GetWindow() == window
)
694 item
->SetMinSize( width
, height
);
697 node
= node
->GetNext();
700 // No? Search any subsizers we own then
702 node
= m_children
.GetFirst();
705 wxSizerItem
*item
= node
->GetData();
707 if ( item
->GetSizer() &&
708 item
->GetSizer()->DoSetItemMinSize( window
, width
, height
) )
710 // A child sizer found the requested windw, exit.
713 node
= node
->GetNext();
719 bool wxSizer::DoSetItemMinSize( wxSizer
*sizer
, int width
, int height
)
721 wxASSERT_MSG( sizer
, _T("SetMinSize for NULL sizer") );
723 // Is it our immediate child?
725 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
728 wxSizerItem
*item
= node
->GetData();
730 if (item
->GetSizer() == sizer
)
732 item
->GetSizer()->DoSetMinSize( width
, height
);
735 node
= node
->GetNext();
738 // No? Search any subsizers we own then
740 node
= m_children
.GetFirst();
743 wxSizerItem
*item
= node
->GetData();
745 if ( item
->GetSizer() &&
746 item
->GetSizer()->DoSetItemMinSize( sizer
, width
, height
) )
748 // A child found the requested sizer, exit.
751 node
= node
->GetNext();
757 bool wxSizer::DoSetItemMinSize( size_t index
, int width
, int height
)
759 wxSizerItemList::compatibility_iterator node
= m_children
.Item( index
);
761 wxCHECK_MSG( node
, false, _T("Failed to find child node") );
763 wxSizerItem
*item
= node
->GetData();
765 if (item
->GetSizer())
767 // Sizers contains the minimal size in them, if not calculated ...
768 item
->GetSizer()->DoSetMinSize( width
, height
);
772 // ... but the minimal size of spacers and windows is stored via the item
773 item
->SetMinSize( width
, height
);
779 wxSizerItem
* wxSizer::GetItem( wxWindow
*window
, bool recursive
)
781 wxASSERT_MSG( window
, _T("GetItem for NULL window") );
783 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
786 wxSizerItem
*item
= node
->GetData();
788 if (item
->GetWindow() == window
)
792 else if (recursive
&& item
->IsSizer())
794 wxSizerItem
*subitem
= item
->GetSizer()->GetItem( window
, true );
799 node
= node
->GetNext();
805 wxSizerItem
* wxSizer::GetItem( wxSizer
*sizer
, bool recursive
)
807 wxASSERT_MSG( sizer
, _T("GetItem for NULL sizer") );
809 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
812 wxSizerItem
*item
= node
->GetData();
814 if (item
->GetSizer() == sizer
)
818 else if (recursive
&& item
->IsSizer())
820 wxSizerItem
*subitem
= item
->GetSizer()->GetItem( sizer
, true );
825 node
= node
->GetNext();
831 wxSizerItem
* wxSizer::GetItem( size_t index
)
833 wxCHECK_MSG( index
< m_children
.GetCount(),
835 _T("GetItem index is out of range") );
837 return m_children
.Item( index
)->GetData();
840 bool wxSizer::Show( wxWindow
*window
, bool show
, bool recursive
)
842 wxSizerItem
*item
= GetItem( window
, recursive
);
853 bool wxSizer::Show( wxSizer
*sizer
, bool show
, bool recursive
)
855 wxSizerItem
*item
= GetItem( sizer
, recursive
);
866 bool wxSizer::Show( size_t index
, bool show
)
868 wxSizerItem
*item
= GetItem( index
);
879 void wxSizer::ShowItems( bool show
)
881 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
884 node
->GetData()->Show( show
);
885 node
= node
->GetNext();
889 bool wxSizer::IsShown( wxWindow
*window
) const
891 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
894 wxSizerItem
*item
= node
->GetData();
896 if (item
->GetWindow() == window
)
898 return item
->IsShown();
900 node
= node
->GetNext();
903 wxFAIL_MSG( _T("IsShown failed to find sizer item") );
908 bool wxSizer::IsShown( wxSizer
*sizer
) const
910 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
913 wxSizerItem
*item
= node
->GetData();
915 if (item
->GetSizer() == sizer
)
917 return item
->IsShown();
919 node
= node
->GetNext();
922 wxFAIL_MSG( _T("IsShown failed to find sizer item") );
927 bool wxSizer::IsShown( size_t index
) const
929 wxCHECK_MSG( index
< m_children
.GetCount(),
931 _T("IsShown index is out of range") );
933 return m_children
.Item( index
)->GetData()->IsShown();
937 //---------------------------------------------------------------------------
939 //---------------------------------------------------------------------------
941 wxGridSizer::wxGridSizer( int rows
, int cols
, int vgap
, int hgap
)
947 if (m_rows
== 0 && m_cols
== 0)
951 wxGridSizer::wxGridSizer( int cols
, int vgap
, int hgap
)
957 if (m_rows
== 0 && m_cols
== 0)
961 int wxGridSizer::CalcRowsCols(int& nrows
, int& ncols
) const
963 int nitems
= m_children
.GetCount();
969 nrows
= (nitems
+ m_cols
- 1) / m_cols
;
973 ncols
= (nitems
+ m_rows
- 1) / m_rows
;
976 else // 0 columns, 0 rows?
978 wxFAIL_MSG( _T("grid sizer must have either rows or columns fixed") );
987 void wxGridSizer::RecalcSizes()
989 int nitems
, nrows
, ncols
;
990 if ( (nitems
= CalcRowsCols(nrows
, ncols
)) == 0 )
993 wxSize
sz( GetSize() );
994 wxPoint
pt( GetPosition() );
996 int w
= (sz
.x
- (ncols
- 1) * m_hgap
) / ncols
;
997 int h
= (sz
.y
- (nrows
- 1) * m_vgap
) / nrows
;
1000 for (int c
= 0; c
< ncols
; c
++)
1003 for (int r
= 0; r
< nrows
; r
++)
1005 int i
= r
* ncols
+ c
;
1008 wxSizerItemList::compatibility_iterator node
= m_children
.Item( i
);
1010 wxASSERT_MSG( node
, _T("Failed to find SizerItemList node") );
1012 SetItemBounds( node
->GetData(), x
, y
, w
, h
);
1020 wxSize
wxGridSizer::CalcMin()
1023 if ( CalcRowsCols(nrows
, ncols
) == 0 )
1024 return wxSize(10, 10);
1026 // Find the max width and height for any component
1030 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
1033 wxSizerItem
*item
= node
->GetData();
1034 wxSize
sz( item
->CalcMin() );
1036 w
= wxMax( w
, sz
.x
);
1037 h
= wxMax( h
, sz
.y
);
1039 node
= node
->GetNext();
1042 return wxSize( ncols
* w
+ (ncols
-1) * m_hgap
,
1043 nrows
* h
+ (nrows
-1) * m_vgap
);
1046 void wxGridSizer::SetItemBounds( wxSizerItem
*item
, int x
, int y
, int w
, int h
)
1049 wxSize
sz( item
->GetMinSizeWithBorder() );
1050 int flag
= item
->GetFlag();
1052 if ((flag
& wxEXPAND
) || (flag
& wxSHAPED
))
1058 if (flag
& wxALIGN_CENTER_HORIZONTAL
)
1060 pt
.x
= x
+ (w
- sz
.x
) / 2;
1062 else if (flag
& wxALIGN_RIGHT
)
1064 pt
.x
= x
+ (w
- sz
.x
);
1067 if (flag
& wxALIGN_CENTER_VERTICAL
)
1069 pt
.y
= y
+ (h
- sz
.y
) / 2;
1071 else if (flag
& wxALIGN_BOTTOM
)
1073 pt
.y
= y
+ (h
- sz
.y
);
1077 item
->SetDimension(pt
, sz
);
1080 //---------------------------------------------------------------------------
1082 //---------------------------------------------------------------------------
1084 wxFlexGridSizer::wxFlexGridSizer( int rows
, int cols
, int vgap
, int hgap
)
1085 : wxGridSizer( rows
, cols
, vgap
, hgap
),
1086 m_flexDirection(wxBOTH
),
1087 m_growMode(wxFLEX_GROWMODE_SPECIFIED
)
1091 wxFlexGridSizer::wxFlexGridSizer( int cols
, int vgap
, int hgap
)
1092 : wxGridSizer( cols
, vgap
, hgap
),
1093 m_flexDirection(wxBOTH
),
1094 m_growMode(wxFLEX_GROWMODE_SPECIFIED
)
1098 wxFlexGridSizer::~wxFlexGridSizer()
1102 void wxFlexGridSizer::RecalcSizes()
1104 int nitems
, nrows
, ncols
;
1105 if ( (nitems
= CalcRowsCols(nrows
, ncols
)) == 0 )
1108 wxPoint
pt( GetPosition() );
1109 wxSize
sz( GetSize() );
1111 AdjustForGrowables(sz
, m_calculatedMinSize
, nrows
, ncols
);
1113 sz
= wxSize( pt
.x
+ sz
.x
, pt
.y
+ sz
.y
);
1116 for (int c
= 0; c
< ncols
; c
++)
1119 for (int r
= 0; r
< nrows
; r
++)
1121 int i
= r
* ncols
+ c
;
1124 wxSizerItemList::compatibility_iterator node
= m_children
.Item( i
);
1126 wxASSERT_MSG( node
, _T("Failed to find node") );
1128 int w
= wxMax( 0, wxMin( m_colWidths
[c
], sz
.x
- x
) );
1129 int h
= wxMax( 0, wxMin( m_rowHeights
[r
], sz
.y
- y
) );
1131 SetItemBounds( node
->GetData(), x
, y
, w
, h
);
1133 y
= y
+ m_rowHeights
[r
] + m_vgap
;
1135 x
= x
+ m_colWidths
[c
] + m_hgap
;
1139 wxSize
wxFlexGridSizer::CalcMin()
1145 // Number of rows/columns can change as items are added or removed.
1146 if ( !CalcRowsCols(nrows
, ncols
) )
1147 return wxSize(10, 10);
1149 m_rowHeights
.SetCount(nrows
);
1150 m_colWidths
.SetCount(ncols
);
1152 // We have to recalcuate the sizes in case the item minimum size has
1153 // changed since the previous layout, or the item has been hidden using
1154 // wxSizer::Show(). If all the items in a row/column are hidden, the final
1155 // dimension of the row/column will be -1, indicating that the column
1156 // itself is hidden.
1157 for( s
= m_rowHeights
.GetCount(), i
= 0; i
< s
; ++i
)
1158 m_rowHeights
[ i
] = -1;
1159 for( s
= m_colWidths
.GetCount(), i
= 0; i
< s
; ++i
)
1160 m_colWidths
[ i
] = -1;
1162 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
1167 wxSizerItem
*item
= node
->GetData();
1168 if ( item
->IsShown() )
1170 wxSize
sz( item
->CalcMin() );
1171 int row
= i
/ ncols
;
1172 int col
= i
% ncols
;
1174 m_rowHeights
[ row
] = wxMax( wxMax( 0, sz
.y
), m_rowHeights
[ row
] );
1175 m_colWidths
[ col
] = wxMax( wxMax( 0, sz
.x
), m_colWidths
[ col
] );
1178 node
= node
->GetNext();
1182 AdjustForFlexDirection();
1184 // Sum total minimum size, including gaps between rows/columns.
1185 // -1 is used as a magic number meaning empty column.
1187 for (int col
= 0; col
< ncols
; col
++)
1188 if ( m_colWidths
[ col
] != -1 )
1189 width
+= m_colWidths
[ col
] + ( col
== ncols
-1 ? 0 : m_hgap
);
1192 for (int row
= 0; row
< nrows
; row
++)
1193 if ( m_rowHeights
[ row
] != -1 )
1194 height
+= m_rowHeights
[ row
] + ( row
== nrows
-1 ? 0 : m_vgap
);
1196 m_calculatedMinSize
= wxSize( width
, height
);
1197 return m_calculatedMinSize
;
1200 void wxFlexGridSizer::AdjustForFlexDirection()
1202 // the logic in CalcMin works when we resize flexibly in both directions
1203 // but maybe this is not the case
1204 if ( m_flexDirection
!= wxBOTH
)
1206 // select the array corresponding to the direction in which we do *not*
1208 wxArrayInt
& array
= m_flexDirection
== wxVERTICAL
? m_colWidths
1211 const int count
= array
.GetCount();
1213 // find the largest value in this array
1215 for ( n
= 0; n
< count
; ++n
)
1217 if ( array
[n
] > largest
)
1221 // and now fill it with the largest value
1222 for ( n
= 0; n
< count
; ++n
)
1230 void wxFlexGridSizer::AdjustForGrowables(const wxSize
& sz
, const wxSize
& minsz
,
1231 int nrows
, int ncols
)
1233 // what to do with the rows? by default, resize them proportionally
1234 if ( sz
.y
> minsz
.y
&& ( (m_flexDirection
& wxVERTICAL
) || (m_growMode
== wxFLEX_GROWMODE_SPECIFIED
) ) )
1236 int sum_proportions
= 0;
1237 int growable_space
= 0;
1240 for (idx
= 0; idx
< m_growableRows
.GetCount(); idx
++)
1242 // Since the number of rows/columns can change as items are
1243 // inserted/deleted, we need to verify at runtime that the
1244 // requested growable rows/columns are still valid.
1245 if (m_growableRows
[idx
] >= nrows
)
1248 // If all items in a row/column are hidden, that row/column will
1249 // have a dimension of -1. This causes the row/column to be
1250 // hidden completely.
1251 if (m_rowHeights
[ m_growableRows
[idx
] ] == -1)
1253 sum_proportions
+= m_growableRowsProportions
[idx
];
1254 growable_space
+= m_rowHeights
[ m_growableRows
[idx
] ];
1260 for (idx
= 0; idx
< m_growableRows
.GetCount(); idx
++)
1262 if (m_growableRows
[idx
] >= nrows
)
1264 if (m_rowHeights
[ m_growableRows
[idx
] ] == -1)
1265 m_rowHeights
[ m_growableRows
[idx
] ] = 0;
1268 int delta
= (sz
.y
- minsz
.y
);
1269 if (sum_proportions
== 0)
1270 delta
= (delta
/num
) + m_rowHeights
[ m_growableRows
[idx
] ];
1272 delta
= ((delta
+growable_space
)*m_growableRowsProportions
[idx
]) / sum_proportions
;
1273 m_rowHeights
[ m_growableRows
[idx
] ] = delta
;
1278 else if ( (m_growMode
== wxFLEX_GROWMODE_ALL
) && (sz
.y
> minsz
.y
) )
1280 // rounding problem?
1281 for ( int row
= 0; row
< nrows
; ++row
)
1282 m_rowHeights
[ row
] = sz
.y
/ nrows
;
1285 // the same logic as above but for the columns
1286 if ( sz
.x
> minsz
.x
&& ( (m_flexDirection
& wxHORIZONTAL
) || (m_growMode
== wxFLEX_GROWMODE_SPECIFIED
) ) )
1288 int sum_proportions
= 0;
1289 int growable_space
= 0;
1292 for (idx
= 0; idx
< m_growableCols
.GetCount(); idx
++)
1294 // Since the number of rows/columns can change as items are
1295 // inserted/deleted, we need to verify at runtime that the
1296 // requested growable rows/columns are still valid.
1297 if (m_growableCols
[idx
] >= ncols
)
1300 // If all items in a row/column are hidden, that row/column will
1301 // have a dimension of -1. This causes the column to be hidden
1303 if (m_colWidths
[ m_growableCols
[idx
] ] == -1)
1305 sum_proportions
+= m_growableColsProportions
[idx
];
1306 growable_space
+= m_colWidths
[ m_growableCols
[idx
] ];
1312 for (idx
= 0; idx
< m_growableCols
.GetCount(); idx
++)
1314 if (m_growableCols
[idx
] >= ncols
)
1316 if (m_colWidths
[ m_growableCols
[idx
] ] == -1)
1317 m_colWidths
[ m_growableCols
[idx
] ] = 0;
1320 int delta
= (sz
.x
- minsz
.x
);
1321 if (sum_proportions
== 0)
1322 delta
= (delta
/num
) + m_colWidths
[ m_growableCols
[idx
] ];
1324 delta
= ((delta
+growable_space
)*m_growableColsProportions
[idx
])/sum_proportions
;
1325 m_colWidths
[ m_growableCols
[idx
] ] = delta
;
1330 else if ( (m_growMode
== wxFLEX_GROWMODE_ALL
) && (sz
.x
> minsz
.x
) )
1332 for ( int col
=0; col
< ncols
; ++col
)
1333 m_colWidths
[ col
] = sz
.x
/ ncols
;
1338 void wxFlexGridSizer::AddGrowableRow( size_t idx
, int proportion
)
1340 m_growableRows
.Add( idx
);
1341 m_growableRowsProportions
.Add( proportion
);
1344 void wxFlexGridSizer::RemoveGrowableRow( size_t idx
)
1346 m_growableRows
.Remove( idx
);
1349 void wxFlexGridSizer::AddGrowableCol( size_t idx
, int proportion
)
1351 m_growableCols
.Add( idx
);
1352 m_growableColsProportions
.Add( proportion
);
1355 void wxFlexGridSizer::RemoveGrowableCol( size_t idx
)
1357 m_growableCols
.Remove( idx
);
1360 //---------------------------------------------------------------------------
1362 //---------------------------------------------------------------------------
1364 wxBoxSizer::wxBoxSizer( int orient
)
1365 : m_orient( orient
)
1369 void wxBoxSizer::RecalcSizes()
1371 if (m_children
.GetCount() == 0)
1377 if (m_orient
== wxHORIZONTAL
)
1378 delta
= m_size
.x
- m_fixedWidth
;
1380 delta
= m_size
.y
- m_fixedHeight
;
1383 wxPoint
pt( m_position
);
1385 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
1388 wxSizerItem
*item
= node
->GetData();
1390 if (item
->IsShown())
1392 wxSize
size( item
->GetMinSizeWithBorder() );
1394 if (m_orient
== wxVERTICAL
)
1396 wxCoord height
= size
.y
;
1397 if (item
->GetProportion())
1399 // Because of at least one visible item has non-zero
1400 // proportion then m_stretchable is not zero
1401 height
= (delta
* item
->GetProportion()) / m_stretchable
;
1404 wxPoint
child_pos( pt
);
1405 wxSize
child_size( size
.x
, height
);
1407 if (item
->GetFlag() & (wxEXPAND
| wxSHAPED
))
1408 child_size
.x
= m_size
.x
;
1409 else if (item
->GetFlag() & wxALIGN_RIGHT
)
1410 child_pos
.x
+= m_size
.x
- size
.x
;
1411 else if (item
->GetFlag() & (wxCENTER
| wxALIGN_CENTER_HORIZONTAL
))
1412 // XXX wxCENTER is added for backward compatibility;
1413 // wxALIGN_CENTER should be used in new code
1414 child_pos
.x
+= (m_size
.x
- size
.x
) / 2;
1416 item
->SetDimension( child_pos
, child_size
);
1422 wxCoord width
= size
.x
;
1423 if (item
->GetProportion())
1425 // Because of at least one visible item has non-zero
1426 // proportion then m_stretchable is not zero
1427 width
= (delta
* item
->GetProportion()) / m_stretchable
;
1430 wxPoint
child_pos( pt
);
1431 wxSize
child_size( width
, size
.y
);
1433 if (item
->GetFlag() & (wxEXPAND
| wxSHAPED
))
1434 child_size
.y
= m_size
.y
;
1435 else if (item
->GetFlag() & wxALIGN_BOTTOM
)
1436 child_pos
.y
+= m_size
.y
- size
.y
;
1437 else if (item
->GetFlag() & (wxCENTER
| wxALIGN_CENTER_VERTICAL
))
1438 // XXX wxCENTER is added for backward compatibility;
1439 // wxALIGN_CENTER should be used in new code
1440 child_pos
.y
+= (m_size
.y
- size
.y
) / 2;
1442 item
->SetDimension( child_pos
, child_size
);
1448 node
= node
->GetNext();
1452 wxSize
wxBoxSizer::CalcMin()
1454 if (m_children
.GetCount() == 0)
1455 return wxSize(10,10);
1463 // precalc item minsizes and count proportions
1464 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
1467 wxSizerItem
*item
= node
->GetData();
1469 if (item
->IsShown())
1470 item
->CalcMin(); // result is stored in the item
1472 if (item
->IsShown() && item
->GetProportion() != 0)
1473 m_stretchable
+= item
->GetProportion();
1475 node
= node
->GetNext();
1478 // Total minimum size (width or height) of sizer
1481 node
= m_children
.GetFirst();
1484 wxSizerItem
*item
= node
->GetData();
1486 if (item
->IsShown() && item
->GetProportion() != 0)
1488 int stretch
= item
->GetProportion();
1489 wxSize
size( item
->GetMinSizeWithBorder() );
1492 // Integer division rounded up is (a + b - 1) / b
1493 // Round up needed in order to guarantee that all
1494 // all items will have size not less then their min size
1495 if (m_orient
== wxHORIZONTAL
)
1496 minSize
= ( size
.x
*m_stretchable
+ stretch
- 1)/stretch
;
1498 minSize
= ( size
.y
*m_stretchable
+ stretch
- 1)/stretch
;
1500 if (minSize
> maxMinSize
)
1501 maxMinSize
= minSize
;
1503 node
= node
->GetNext();
1506 // Calculate overall minimum size
1507 node
= m_children
.GetFirst();
1510 wxSizerItem
*item
= node
->GetData();
1512 if (item
->IsShown())
1514 wxSize
size( item
->GetMinSizeWithBorder() );
1515 if (item
->GetProportion() != 0)
1517 if (m_orient
== wxHORIZONTAL
)
1518 size
.x
= (maxMinSize
*item
->GetProportion())/m_stretchable
;
1520 size
.y
= (maxMinSize
*item
->GetProportion())/m_stretchable
;
1524 if (m_orient
== wxVERTICAL
)
1526 m_fixedHeight
+= size
.y
;
1527 m_fixedWidth
= wxMax( m_fixedWidth
, size
.x
);
1531 m_fixedWidth
+= size
.x
;
1532 m_fixedHeight
= wxMax( m_fixedHeight
, size
.y
);
1536 if (m_orient
== wxHORIZONTAL
)
1538 m_minWidth
+= size
.x
;
1539 m_minHeight
= wxMax( m_minHeight
, size
.y
);
1543 m_minHeight
+= size
.y
;
1544 m_minWidth
= wxMax( m_minWidth
, size
.x
);
1547 node
= node
->GetNext();
1550 return wxSize( m_minWidth
, m_minHeight
);
1553 //---------------------------------------------------------------------------
1555 //---------------------------------------------------------------------------
1559 wxStaticBoxSizer::wxStaticBoxSizer( wxStaticBox
*box
, int orient
)
1560 : wxBoxSizer( orient
)
1561 , m_staticBox( box
)
1563 wxASSERT_MSG( box
, wxT("wxStaticBoxSizer needs a static box") );
1566 static void GetStaticBoxBorders( wxStaticBox
*box
,
1570 // this has to be done platform by platform as there is no way to
1571 // guess the thickness of a wxStaticBox border
1573 box
->GetBordersForSizer(borderTop
,borderOther
);
1574 #elif defined(__WXMAC__)
1576 static int extraTop
= -1; // Uninitted
1577 static int other
= 5;
1579 if ( extraTop
== -1 )
1581 // The minimal border used for the top. Later on the staticbox'
1582 // font height is added to this.
1585 if ( UMAGetSystemVersion() >= 0x1030 /*Panther*/ )
1587 // As indicated by the HIG, Panther needs an extra border of 11
1588 // pixels (otherwise overlapping occurs at the top). The "other"
1589 // border has to be 11.
1596 *borderTop
= extraTop
+ box
->GetCharHeight();
1597 *borderOther
= other
;
1601 if ( box
->GetLabel().empty() )
1605 *borderTop
= box
->GetCharHeight();
1608 #endif // __WXCOCOA__
1611 void wxStaticBoxSizer::RecalcSizes()
1613 int top_border
, other_border
;
1614 GetStaticBoxBorders(m_staticBox
, &top_border
, &other_border
);
1616 m_staticBox
->SetSize( m_position
.x
, m_position
.y
, m_size
.x
, m_size
.y
);
1618 wxPoint
old_pos( m_position
);
1619 m_position
.x
+= other_border
;
1620 m_position
.y
+= top_border
;
1621 wxSize
old_size( m_size
);
1622 m_size
.x
-= 2*other_border
;
1623 m_size
.y
-= top_border
+ other_border
;
1625 wxBoxSizer::RecalcSizes();
1627 m_position
= old_pos
;
1631 wxSize
wxStaticBoxSizer::CalcMin()
1633 int top_border
, other_border
;
1634 GetStaticBoxBorders(m_staticBox
, &top_border
, &other_border
);
1636 wxSize
ret( wxBoxSizer::CalcMin() );
1637 ret
.x
+= 2*other_border
;
1638 ret
.y
+= other_border
+ top_border
;
1643 void wxStaticBoxSizer::ShowItems( bool show
)
1645 m_staticBox
->Show( show
);
1646 wxBoxSizer::ShowItems( show
);
1649 #endif // wxUSE_STATBOX
1653 wxStdDialogButtonSizer::wxStdDialogButtonSizer()
1654 : wxBoxSizer(wxHORIZONTAL
)
1656 bool is_pda
= (wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA
);
1658 // If we have a PDA screen, put yes/no button over
1659 // all other buttons, otherwise on the left side.
1661 m_orient
= wxVERTICAL
;
1663 m_buttonAffirmative
= NULL
;
1664 m_buttonApply
= NULL
;
1665 m_buttonNegative
= NULL
;
1666 m_buttonCancel
= NULL
;
1667 m_buttonHelp
= NULL
;
1670 void wxStdDialogButtonSizer::AddButton(wxButton
*mybutton
)
1672 switch (mybutton
->GetId())
1677 m_buttonAffirmative
= mybutton
;
1680 m_buttonApply
= mybutton
;
1683 m_buttonNegative
= mybutton
;
1686 m_buttonCancel
= mybutton
;
1689 m_buttonHelp
= mybutton
;
1696 void wxStdDialogButtonSizer::SetAffirmativeButton( wxButton
*button
)
1698 m_buttonAffirmative
= button
;
1701 void wxStdDialogButtonSizer::SetNegativeButton( wxButton
*button
)
1703 m_buttonNegative
= button
;
1706 void wxStdDialogButtonSizer::SetCancelButton( wxButton
*button
)
1708 m_buttonCancel
= button
;
1711 void wxStdDialogButtonSizer::Finalise()
1714 Add(0, 0, 0, wxLEFT
, 6);
1716 Add((wxWindow
*)m_buttonHelp
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, 6);
1718 if (m_buttonNegative
){
1719 // HIG POLICE BULLETIN - destructive buttons need extra padding
1720 // 24 pixels on either side
1721 Add((wxWindow
*)m_buttonNegative
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, 12);
1724 // extra whitespace between help/negative and cancel/ok buttons
1725 Add(0, 0, 1, wxEXPAND
, 0);
1727 if (m_buttonCancel
){
1728 Add((wxWindow
*)m_buttonCancel
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, 6);
1729 // Cancel or help should be default
1730 // m_buttonCancel->SetDefaultButton();
1733 // Ugh, Mac doesn't really have apply dialogs, so I'll just
1734 // figure the best place is between Cancel and OK
1736 Add((wxWindow
*)m_buttonApply
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, 6);
1738 if (m_buttonAffirmative
){
1739 Add((wxWindow
*)m_buttonAffirmative
, 0, wxALIGN_CENTRE
| wxLEFT
, 6);
1741 if (m_buttonAffirmative
->GetId() == wxID_SAVE
){
1742 // these buttons have set labels under Mac so we should use them
1743 m_buttonAffirmative
->SetLabel(_("Save"));
1744 m_buttonNegative
->SetLabel(_("Don't Save"));
1748 // Extra space around and at the right
1750 #elif defined(__WXGTK20__)
1751 Add(0, 0, 0, wxLEFT
, 9);
1753 Add((wxWindow
*)m_buttonHelp
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, 3);
1755 // extra whitespace between help and cancel/ok buttons
1756 Add(0, 0, 1, wxEXPAND
, 0);
1758 if (m_buttonNegative
){
1759 Add((wxWindow
*)m_buttonNegative
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, 3);
1762 if (m_buttonCancel
){
1763 Add((wxWindow
*)m_buttonCancel
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, 3);
1764 // Cancel or help should be default
1765 // m_buttonCancel->SetDefaultButton();
1769 Add((wxWindow
*)m_buttonApply
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, 3);
1771 if (m_buttonAffirmative
)
1772 Add((wxWindow
*)m_buttonAffirmative
, 0, wxALIGN_CENTRE
| wxLEFT
, 6);
1774 // do the same thing for GTK1 and Windows platforms
1775 // and assume any platform not accounted for here will use
1777 Add(0, 0, 0, wxLEFT
, 9);
1779 Add((wxWindow
*)m_buttonHelp
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, m_buttonHelp
->ConvertDialogToPixels(wxSize(4, 0)).x
);
1781 // extra whitespace between help and cancel/ok buttons
1782 Add(0, 0, 1, wxEXPAND
, 0);
1785 Add((wxWindow
*)m_buttonApply
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, m_buttonApply
->ConvertDialogToPixels(wxSize(4, 0)).x
);
1787 if (m_buttonAffirmative
){
1788 Add((wxWindow
*)m_buttonAffirmative
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, m_buttonAffirmative
->ConvertDialogToPixels(wxSize(4, 0)).x
);
1791 if (m_buttonNegative
){
1792 Add((wxWindow
*)m_buttonNegative
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, m_buttonNegative
->ConvertDialogToPixels(wxSize(4, 0)).x
);
1795 if (m_buttonCancel
){
1796 Add((wxWindow
*)m_buttonCancel
, 0, wxALIGN_CENTRE
| wxLEFT
, m_buttonCancel
->ConvertDialogToPixels(wxSize(4, 0)).x
);
1797 // Cancel or help should be default
1798 // m_buttonCancel->SetDefaultButton();
1804 #endif // wxUSE_BUTTON
1806 #if WXWIN_COMPATIBILITY_2_4
1808 // ----------------------------------------------------------------------------
1810 // ----------------------------------------------------------------------------
1813 IMPLEMENT_CLASS(wxBookCtrlSizer
, wxSizer
)
1815 IMPLEMENT_CLASS(wxNotebookSizer
, wxBookCtrlSizer
)
1816 #endif // wxUSE_NOTEBOOK
1817 #endif // wxUSE_BOOKCTRL
1821 wxBookCtrlSizer::wxBookCtrlSizer(wxBookCtrlBase
*bookctrl
)
1822 : m_bookctrl(bookctrl
)
1824 wxASSERT_MSG( bookctrl
, wxT("wxBookCtrlSizer needs a control") );
1827 void wxBookCtrlSizer::RecalcSizes()
1829 m_bookctrl
->SetSize( m_position
.x
, m_position
.y
, m_size
.x
, m_size
.y
);
1832 wxSize
wxBookCtrlSizer::CalcMin()
1834 wxSize sizeBorder
= m_bookctrl
->CalcSizeFromPage(wxSize(0, 0));
1839 if ( m_bookctrl
->GetPageCount() == 0 )
1841 return wxSize(sizeBorder
.x
+ 10, sizeBorder
.y
+ 10);
1847 wxWindowList::compatibility_iterator
1848 node
= m_bookctrl
->GetChildren().GetFirst();
1851 wxWindow
*item
= node
->GetData();
1852 wxSizer
*itemsizer
= item
->GetSizer();
1856 wxSize
subsize( itemsizer
->CalcMin() );
1858 if (subsize
.x
> maxX
)
1860 if (subsize
.y
> maxY
)
1864 node
= node
->GetNext();
1867 return wxSize( maxX
, maxY
) + sizeBorder
;
1872 wxNotebookSizer::wxNotebookSizer(wxNotebook
*nb
)
1874 wxASSERT_MSG( nb
, wxT("wxNotebookSizer needs a control") );
1878 #endif // wxUSE_NOTEBOOOK
1879 #endif // wxUSE_BOOKCTRL
1881 #endif // WXWIN_COMPATIBILITY_2_4