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/notebook.h"
28 #include "wx/listimpl.cpp"
31 # include "wx/mac/uma.h"
34 //---------------------------------------------------------------------------
36 IMPLEMENT_CLASS(wxSizerItem
, wxObject
)
37 IMPLEMENT_CLASS(wxSizer
, wxObject
)
38 IMPLEMENT_CLASS(wxGridSizer
, wxSizer
)
39 IMPLEMENT_CLASS(wxFlexGridSizer
, wxGridSizer
)
40 IMPLEMENT_CLASS(wxBoxSizer
, wxSizer
)
42 IMPLEMENT_CLASS(wxStaticBoxSizer
, wxBoxSizer
)
45 IMPLEMENT_CLASS(wxBookCtrlSizer
, wxSizer
)
47 IMPLEMENT_CLASS(wxNotebookSizer
, wxBookCtrlSizer
)
48 #endif // wxUSE_NOTEBOOK
49 #endif // wxUSE_BOOKCTRL
51 WX_DEFINE_EXPORTED_LIST( wxSizerItemList
);
85 //---------------------------------------------------------------------------
87 //---------------------------------------------------------------------------
89 wxSizerItem::wxSizerItem( int width
, int height
, int proportion
, int flag
, int border
, wxObject
* userData
)
92 , m_size( wxSize( width
, height
) ) // size is set directly
93 , m_minSize( m_size
) // minimal size is the initial size
94 , m_proportion( proportion
)
98 , m_userData( userData
)
103 wxSizerItem::wxSizerItem( wxWindow
*window
, int proportion
, int flag
, int border
, wxObject
* userData
)
106 , m_minSize( window
->GetSize() ) // minimal size is the initial size
107 , m_proportion( proportion
)
111 , m_userData( userData
)
113 // aspect ratio calculated from initial size
114 SetRatio( m_minSize
);
116 // m_size is calculated later
119 wxSizerItem::wxSizerItem( wxSizer
*sizer
, int proportion
, int flag
, int border
, wxObject
* userData
)
122 , m_proportion( proportion
)
127 , m_userData( userData
)
129 // m_minSize is calculated later
130 // m_size is calculated later
133 wxSizerItem::wxSizerItem()
145 wxSizerItem::~wxSizerItem()
151 m_window
->SetContainingSizer(NULL
);
153 else // we must be a sizer
160 wxSize
wxSizerItem::GetSize() const
164 ret
= m_sizer
->GetSize();
167 ret
= m_window
->GetSize();
174 if (m_flag
& wxNORTH
)
176 if (m_flag
& wxSOUTH
)
182 wxSize
wxSizerItem::CalcMin()
187 ret
= m_sizer
->GetMinSize();
189 // if we have to preserve aspect ratio _AND_ this is
190 // the first-time calculation, consider ret to be initial size
191 if ((m_flag
& wxSHAPED
) && !m_ratio
)
196 if ( IsWindow() && (m_flag
& wxADJUST_MINSIZE
) )
198 // By user request, keep the minimal size for this item
199 // in sync with the largest of BestSize and any user supplied
200 // minimum size hint. Useful in cases where the item is
201 // changeable -- static text labels, etc.
202 m_minSize
= m_window
->GetAdjustedBestSize();
212 if (m_flag
& wxNORTH
)
214 if (m_flag
& wxSOUTH
)
220 void wxSizerItem::SetDimension( wxPoint pos
, wxSize size
)
222 if (m_flag
& wxSHAPED
)
224 // adjust aspect ratio
225 int rwidth
= (int) (size
.y
* m_ratio
);
229 int rheight
= (int) (size
.x
/ m_ratio
);
230 // add vertical space
231 if (m_flag
& wxALIGN_CENTER_VERTICAL
)
232 pos
.y
+= (size
.y
- rheight
) / 2;
233 else if (m_flag
& wxALIGN_BOTTOM
)
234 pos
.y
+= (size
.y
- rheight
);
235 // use reduced dimensions
238 else if (rwidth
< size
.x
)
240 // add horizontal space
241 if (m_flag
& wxALIGN_CENTER_HORIZONTAL
)
242 pos
.x
+= (size
.x
- rwidth
) / 2;
243 else if (m_flag
& wxALIGN_RIGHT
)
244 pos
.x
+= (size
.x
- rwidth
);
249 // This is what GetPosition() returns. Since we calculate
250 // borders afterwards, GetPosition() will be the left/top
251 // corner of the surrounding border.
263 if (m_flag
& wxNORTH
)
268 if (m_flag
& wxSOUTH
)
274 m_sizer
->SetDimension( pos
.x
, pos
.y
, size
.x
, size
.y
);
277 m_window
->SetSize( pos
.x
, pos
.y
, size
.x
, size
.y
, wxSIZE_ALLOW_MINUS_ONE
);
282 void wxSizerItem::DeleteWindows()
288 m_sizer
->DeleteWindows();
291 bool wxSizerItem::IsWindow() const
293 return (m_window
!= NULL
);
296 bool wxSizerItem::IsSizer() const
298 return (m_sizer
!= NULL
);
301 bool wxSizerItem::IsSpacer() const
303 return (m_window
== NULL
) && (m_sizer
== NULL
);
306 void wxSizerItem::Show( bool show
)
311 m_window
->Show( show
);
313 m_sizer
->ShowItems( show
);
315 // ... nothing else to do to hide/show spacers
318 void wxSizerItem::SetOption( int option
)
320 SetProportion( option
);
323 int wxSizerItem::GetOption() const
325 return GetProportion();
329 //---------------------------------------------------------------------------
331 //---------------------------------------------------------------------------
334 : m_minSize( wxSize( 0, 0 ) )
340 WX_CLEAR_LIST(wxSizerItemList
, m_children
);
343 void wxSizer::Add( wxWindow
*window
, int proportion
, int flag
, int border
, wxObject
* userData
)
345 m_children
.Append( new wxSizerItem( window
, proportion
, flag
, border
, userData
) );
346 window
->SetContainingSizer( this );
349 void wxSizer::Add( wxSizer
*sizer
, int proportion
, int flag
, int border
, wxObject
* userData
)
351 m_children
.Append( new wxSizerItem( sizer
, proportion
, flag
, border
, userData
) );
354 void wxSizer::Add( int width
, int height
, int proportion
, int flag
, int border
, wxObject
* userData
)
356 m_children
.Append( new wxSizerItem( width
, height
, proportion
, flag
, border
, userData
) );
359 void wxSizer::Add( wxSizerItem
*item
)
361 m_children
.Append( item
);
363 if( item
->GetWindow() )
364 item
->GetWindow()->SetContainingSizer( this );
367 void wxSizer::Prepend( wxWindow
*window
, int proportion
, int flag
, int border
, wxObject
* userData
)
369 m_children
.Insert( new wxSizerItem( window
, proportion
, flag
, border
, userData
) );
370 window
->SetContainingSizer( this );
373 void wxSizer::Prepend( wxSizer
*sizer
, int proportion
, int flag
, int border
, wxObject
* userData
)
375 m_children
.Insert( new wxSizerItem( sizer
, proportion
, flag
, border
, userData
) );
378 void wxSizer::Prepend( int width
, int height
, int proportion
, int flag
, int border
, wxObject
* userData
)
380 m_children
.Insert( new wxSizerItem( width
, height
, proportion
, flag
, border
, userData
) );
383 void wxSizer::Prepend( wxSizerItem
*item
)
385 m_children
.Insert( item
);
387 if( item
->GetWindow() )
388 item
->GetWindow()->SetContainingSizer( this );
391 void wxSizer::Insert( size_t index
,
398 m_children
.Insert( index
,
399 new wxSizerItem( window
, proportion
, flag
, border
, userData
) );
400 window
->SetContainingSizer( this );
403 void wxSizer::Insert( size_t index
,
410 m_children
.Insert( index
,
411 new wxSizerItem( sizer
, proportion
, flag
, border
, userData
) );
414 void wxSizer::Insert( size_t index
,
422 m_children
.Insert( index
,
423 new wxSizerItem( width
, height
, proportion
, flag
, border
, userData
) );
426 void wxSizer::Insert( size_t index
, wxSizerItem
*item
)
428 m_children
.Insert( index
, item
);
430 if( item
->GetWindow() )
431 item
->GetWindow()->SetContainingSizer( this );
434 bool wxSizer::Remove( wxWindow
*window
)
436 return Detach( window
);
439 bool wxSizer::Remove( wxSizer
*sizer
)
441 wxASSERT_MSG( sizer
, _T("Removing NULL sizer") );
443 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
446 wxSizerItem
*item
= node
->GetData();
448 if (item
->GetSizer() == sizer
)
451 m_children
.Erase( node
);
455 node
= node
->GetNext();
461 bool wxSizer::Remove( int index
)
463 wxCHECK_MSG( index
>= 0 && (size_t)index
< m_children
.GetCount(),
465 _T("Remove index is out of range") );
467 wxSizerItemList::compatibility_iterator node
= m_children
.Item( index
);
469 wxCHECK_MSG( node
, false, _T("Failed to find child node") );
471 wxSizerItem
*item
= node
->GetData();
473 if( item
->IsWindow() )
474 item
->GetWindow()->SetContainingSizer( NULL
);
477 m_children
.Erase( node
);
481 bool wxSizer::Detach( wxSizer
*sizer
)
483 wxASSERT_MSG( sizer
, _T("Detaching NULL sizer") );
485 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
488 wxSizerItem
*item
= node
->GetData();
490 if (item
->GetSizer() == sizer
)
494 m_children
.Erase( node
);
497 node
= node
->GetNext();
503 bool wxSizer::Detach( wxWindow
*window
)
505 wxASSERT_MSG( window
, _T("Detaching NULL window") );
507 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
510 wxSizerItem
*item
= node
->GetData();
512 if (item
->GetWindow() == window
)
514 item
->GetWindow()->SetContainingSizer( NULL
);
516 m_children
.Erase( node
);
519 node
= node
->GetNext();
525 bool wxSizer::Detach( int index
)
527 wxCHECK_MSG( index
>= 0 && (size_t)index
< m_children
.GetCount(),
529 _T("Detach index is out of range") );
531 wxSizerItemList::compatibility_iterator node
= m_children
.Item( index
);
533 wxCHECK_MSG( node
, false, _T("Failed to find child node") );
535 wxSizerItem
*item
= node
->GetData();
537 if( item
->IsSizer() )
539 else if( item
->IsWindow() )
540 item
->GetWindow()->SetContainingSizer( NULL
);
543 m_children
.Erase( node
);
547 void wxSizer::Clear( bool delete_windows
)
549 // First clear the ContainingSizer pointers
550 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
553 wxSizerItem
*item
= node
->GetData();
555 if (item
->IsWindow())
556 item
->GetWindow()->SetContainingSizer( NULL
);
557 node
= node
->GetNext();
560 // Destroy the windows if needed
564 // Now empty the list
565 WX_CLEAR_LIST(wxSizerItemList
, m_children
);
568 void wxSizer::DeleteWindows()
570 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
573 wxSizerItem
*item
= node
->GetData();
575 item
->DeleteWindows();
576 node
= node
->GetNext();
580 wxSize
wxSizer::Fit( wxWindow
*window
)
582 wxSize
size(window
->IsTopLevel() ? FitSize(window
)
583 : GetMinWindowSize(window
));
585 window
->SetSize( size
);
590 void wxSizer::FitInside( wxWindow
*window
)
593 if (window
->IsTopLevel())
594 size
= VirtualFitSize( window
);
596 size
= GetMinClientSize( window
);
598 window
->SetVirtualSize( size
);
601 void wxSizer::Layout()
607 void wxSizer::SetSizeHints( wxWindow
*window
)
609 // Preserve the window's max size hints, but set the
610 // lower bound according to the sizer calculations.
612 wxSize size
= Fit( window
);
614 window
->SetSizeHints( size
.x
,
616 window
->GetMaxWidth(),
617 window
->GetMaxHeight() );
620 void wxSizer::SetVirtualSizeHints( wxWindow
*window
)
622 // Preserve the window's max size hints, but set the
623 // lower bound according to the sizer calculations.
626 wxSize
size( window
->GetVirtualSize() );
627 window
->SetVirtualSizeHints( size
.x
,
629 window
->GetMaxWidth(),
630 window
->GetMaxHeight() );
633 wxSize
wxSizer::GetMaxWindowSize( wxWindow
*window
) const
635 return window
->GetMaxSize();
638 wxSize
wxSizer::GetMinWindowSize( wxWindow
*window
)
640 wxSize
minSize( GetMinSize() );
641 wxSize
size( window
->GetSize() );
642 wxSize
client_size( window
->GetClientSize() );
644 return wxSize( minSize
.x
+size
.x
-client_size
.x
,
645 minSize
.y
+size
.y
-client_size
.y
);
648 // TODO on mac we need a function that determines how much free space this
649 // min size contains, in order to make sure that we have 20 pixels of free
650 // space around the controls
652 // Return a window size that will fit within the screens dimensions
653 wxSize
wxSizer::FitSize( wxWindow
*window
)
655 wxSize size
= GetMinWindowSize( window
);
656 wxSize sizeMax
= GetMaxWindowSize( window
);
658 // Limit the size if sizeMax != wxDefaultSize
660 if ( size
.x
> sizeMax
.x
&& sizeMax
.x
!= -1 )
662 if ( size
.y
> sizeMax
.y
&& sizeMax
.y
!= -1 )
668 wxSize
wxSizer::GetMaxClientSize( wxWindow
*window
) const
670 wxSize
maxSize( window
->GetMaxSize() );
672 if( maxSize
!= wxDefaultSize
)
674 wxSize
size( window
->GetSize() );
675 wxSize
client_size( window
->GetClientSize() );
677 return wxSize( maxSize
.x
+ client_size
.x
- size
.x
,
678 maxSize
.y
+ client_size
.y
- size
.y
);
681 return wxDefaultSize
;
684 wxSize
wxSizer::GetMinClientSize( wxWindow
*WXUNUSED(window
) )
686 return GetMinSize(); // Already returns client size.
689 wxSize
wxSizer::VirtualFitSize( wxWindow
*window
)
691 wxSize size
= GetMinClientSize( window
);
692 wxSize sizeMax
= GetMaxClientSize( window
);
694 // Limit the size if sizeMax != wxDefaultSize
696 if ( size
.x
> sizeMax
.x
&& sizeMax
.x
!= -1 )
698 if ( size
.y
> sizeMax
.y
&& sizeMax
.y
!= -1 )
704 void wxSizer::SetDimension( int x
, int y
, int width
, int height
)
713 wxSize
wxSizer::GetMinSize()
715 wxSize
ret( CalcMin() );
716 if (ret
.x
< m_minSize
.x
) ret
.x
= m_minSize
.x
;
717 if (ret
.y
< m_minSize
.y
) ret
.y
= m_minSize
.y
;
721 void wxSizer::DoSetMinSize( int width
, int height
)
724 m_minSize
.y
= height
;
727 bool wxSizer::DoSetItemMinSize( wxWindow
*window
, int width
, int height
)
729 wxASSERT_MSG( window
, _T("SetMinSize for NULL window") );
731 // Is it our immediate child?
733 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
736 wxSizerItem
*item
= node
->GetData();
738 if (item
->GetWindow() == window
)
740 item
->SetInitSize( width
, height
);
743 node
= node
->GetNext();
746 // No? Search any subsizers we own then
748 node
= m_children
.GetFirst();
751 wxSizerItem
*item
= node
->GetData();
753 if ( item
->GetSizer() &&
754 item
->GetSizer()->DoSetItemMinSize( window
, width
, height
) )
756 // A child sizer found the requested windw, exit.
759 node
= node
->GetNext();
765 bool wxSizer::DoSetItemMinSize( wxSizer
*sizer
, int width
, int height
)
767 wxASSERT_MSG( sizer
, _T("SetMinSize for NULL sizer") );
769 // Is it our immediate child?
771 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
774 wxSizerItem
*item
= node
->GetData();
776 if (item
->GetSizer() == sizer
)
778 item
->GetSizer()->DoSetMinSize( width
, height
);
781 node
= node
->GetNext();
784 // No? Search any subsizers we own then
786 node
= m_children
.GetFirst();
789 wxSizerItem
*item
= node
->GetData();
791 if ( item
->GetSizer() &&
792 item
->GetSizer()->DoSetItemMinSize( sizer
, width
, height
) )
794 // A child found the requested sizer, exit.
797 node
= node
->GetNext();
803 bool wxSizer::DoSetItemMinSize( size_t index
, int width
, int height
)
805 wxSizerItemList::compatibility_iterator node
= m_children
.Item( index
);
807 wxCHECK_MSG( node
, false, _T("Failed to find child node") );
809 wxSizerItem
*item
= node
->GetData();
811 if (item
->GetSizer())
813 // Sizers contains the minimal size in them, if not calculated ...
814 item
->GetSizer()->DoSetMinSize( width
, height
);
818 // ... but the minimal size of spacers and windows in stored in them
819 item
->SetInitSize( width
, height
);
825 void wxSizer::Show( wxWindow
*window
, bool show
)
827 wxASSERT_MSG( window
, _T("Show for NULL window") );
829 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
832 wxSizerItem
*item
= node
->GetData();
834 if (item
->GetWindow() == window
)
839 node
= node
->GetNext();
843 void wxSizer::Show( wxSizer
*sizer
, bool show
)
845 wxASSERT_MSG( sizer
, _T("Show for NULL sizer") );
847 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
850 wxSizerItem
*item
= node
->GetData();
852 if (item
->GetSizer() == sizer
)
857 node
= node
->GetNext();
861 void wxSizer::Show( size_t index
, bool show
)
863 wxCHECK_RET( index
< m_children
.GetCount(),
864 _T("Show index is out of range") );
866 m_children
.Item( index
)->GetData()->Show( show
);
869 void wxSizer::ShowItems( bool show
)
871 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
874 node
->GetData()->Show( show
);
875 node
= node
->GetNext();
879 bool wxSizer::IsShown( wxWindow
*window
) const
881 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
884 wxSizerItem
*item
= node
->GetData();
886 if (item
->GetWindow() == window
)
888 return item
->IsShown();
890 node
= node
->GetNext();
893 wxFAIL_MSG( _T("IsShown failed to find sizer item") );
898 bool wxSizer::IsShown( wxSizer
*sizer
) const
900 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
903 wxSizerItem
*item
= node
->GetData();
905 if (item
->GetSizer() == sizer
)
907 return item
->IsShown();
909 node
= node
->GetNext();
912 wxFAIL_MSG( _T("IsShown failed to find sizer item") );
917 bool wxSizer::IsShown( size_t index
) const
919 wxCHECK_MSG( index
< m_children
.GetCount(),
921 _T("IsShown index is out of range") );
923 return m_children
.Item( index
)->GetData()->IsShown();
927 //---------------------------------------------------------------------------
929 //---------------------------------------------------------------------------
931 wxGridSizer::wxGridSizer( int rows
, int cols
, int vgap
, int hgap
)
937 if (m_rows
== 0 && m_cols
== 0)
941 wxGridSizer::wxGridSizer( int cols
, int vgap
, int hgap
)
947 if (m_rows
== 0 && m_cols
== 0)
951 int wxGridSizer::CalcRowsCols(int& nrows
, int& ncols
) const
953 int nitems
= m_children
.GetCount();
959 nrows
= (nitems
+ m_cols
- 1) / m_cols
;
963 ncols
= (nitems
+ m_rows
- 1) / m_rows
;
966 else // 0 columns, 0 rows?
968 wxFAIL_MSG( _T("grid sizer must have either rows or columns fixed") );
977 void wxGridSizer::RecalcSizes()
979 int nitems
, nrows
, ncols
;
980 if ( (nitems
= CalcRowsCols(nrows
, ncols
)) == 0 )
983 wxSize
sz( GetSize() );
984 wxPoint
pt( GetPosition() );
986 int w
= (sz
.x
- (ncols
- 1) * m_hgap
) / ncols
;
987 int h
= (sz
.y
- (nrows
- 1) * m_vgap
) / nrows
;
990 for (int c
= 0; c
< ncols
; c
++)
993 for (int r
= 0; r
< nrows
; r
++)
995 int i
= r
* ncols
+ c
;
998 wxSizerItemList::compatibility_iterator node
= m_children
.Item( i
);
1000 wxASSERT_MSG( node
, _T("Failed to find SizerItemList node") );
1002 SetItemBounds( node
->GetData(), x
, y
, w
, h
);
1010 wxSize
wxGridSizer::CalcMin()
1013 if ( CalcRowsCols(nrows
, ncols
) == 0 )
1014 return wxSize(10, 10);
1016 // Find the max width and height for any component
1020 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
1023 wxSizerItem
*item
= node
->GetData();
1024 wxSize
sz( item
->CalcMin() );
1026 w
= wxMax( w
, sz
.x
);
1027 h
= wxMax( h
, sz
.y
);
1029 node
= node
->GetNext();
1032 return wxSize( ncols
* w
+ (ncols
-1) * m_hgap
,
1033 nrows
* h
+ (nrows
-1) * m_vgap
);
1036 void wxGridSizer::SetItemBounds( wxSizerItem
*item
, int x
, int y
, int w
, int h
)
1039 wxSize
sz( item
->CalcMin() );
1040 int flag
= item
->GetFlag();
1042 if ((flag
& wxEXPAND
) || (flag
& wxSHAPED
))
1048 if (flag
& wxALIGN_CENTER_HORIZONTAL
)
1050 pt
.x
= x
+ (w
- sz
.x
) / 2;
1052 else if (flag
& wxALIGN_RIGHT
)
1054 pt
.x
= x
+ (w
- sz
.x
);
1057 if (flag
& wxALIGN_CENTER_VERTICAL
)
1059 pt
.y
= y
+ (h
- sz
.y
) / 2;
1061 else if (flag
& wxALIGN_BOTTOM
)
1063 pt
.y
= y
+ (h
- sz
.y
);
1067 item
->SetDimension(pt
, sz
);
1070 //---------------------------------------------------------------------------
1072 //---------------------------------------------------------------------------
1074 wxFlexGridSizer::wxFlexGridSizer( int rows
, int cols
, int vgap
, int hgap
)
1075 : wxGridSizer( rows
, cols
, vgap
, hgap
),
1076 m_flexDirection(wxBOTH
),
1077 m_growMode(wxFLEX_GROWMODE_SPECIFIED
)
1081 wxFlexGridSizer::wxFlexGridSizer( int cols
, int vgap
, int hgap
)
1082 : wxGridSizer( cols
, vgap
, hgap
),
1083 m_flexDirection(wxBOTH
),
1084 m_growMode(wxFLEX_GROWMODE_SPECIFIED
)
1088 wxFlexGridSizer::~wxFlexGridSizer()
1092 void wxFlexGridSizer::RecalcSizes()
1094 int nitems
, nrows
, ncols
;
1095 if ( (nitems
= CalcRowsCols(nrows
, ncols
)) == 0 )
1098 wxPoint
pt( GetPosition() );
1099 wxSize
sz( GetSize() );
1100 wxSize
minsz( CalcMin() );
1102 AdjustForGrowables(sz
, minsz
, nrows
, ncols
);
1104 sz
= wxSize( pt
.x
+ sz
.x
, pt
.y
+ sz
.y
);
1107 for (int c
= 0; c
< ncols
; c
++)
1110 for (int r
= 0; r
< nrows
; r
++)
1112 int i
= r
* ncols
+ c
;
1115 wxSizerItemList::compatibility_iterator node
= m_children
.Item( i
);
1117 wxASSERT_MSG( node
, _T("Failed to find node") );
1119 int w
= wxMax( 0, wxMin( m_colWidths
[c
], sz
.x
- x
) );
1120 int h
= wxMax( 0, wxMin( m_rowHeights
[r
], sz
.y
- y
) );
1122 SetItemBounds( node
->GetData(), x
, y
, w
, h
);
1124 y
= y
+ m_rowHeights
[r
] + m_vgap
;
1126 x
= x
+ m_colWidths
[c
] + m_hgap
;
1130 wxSize
wxFlexGridSizer::CalcMin()
1136 // Number of rows/columns can change as items are added or removed.
1137 if ( !CalcRowsCols(nrows
, ncols
) )
1138 return wxSize(10, 10);
1140 m_rowHeights
.SetCount(nrows
);
1141 m_colWidths
.SetCount(ncols
);
1143 // We have to recalcuate the sizes in case an item has wxADJUST_MINSIZE, has changed
1144 // minimum size since the previous layout, or has been hidden using wxSizer::Show().
1145 // If all the items in a row/column are hidden, the final dimension of the row/column
1146 // will be -1, indicating that the column itself is hidden.
1147 for( s
= m_rowHeights
.GetCount(), i
= 0; i
< s
; ++i
)
1148 m_rowHeights
[ i
] = -1;
1149 for( s
= m_colWidths
.GetCount(), i
= 0; i
< s
; ++i
)
1150 m_colWidths
[ i
] = -1;
1152 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
1157 wxSizerItem
*item
= node
->GetData();
1158 if ( item
->IsShown() )
1160 wxSize
sz( item
->CalcMin() );
1161 int row
= i
/ ncols
;
1162 int col
= i
% ncols
;
1164 m_rowHeights
[ row
] = wxMax( wxMax( 0, sz
.y
), m_rowHeights
[ row
] );
1165 m_colWidths
[ col
] = wxMax( wxMax( 0, sz
.x
), m_colWidths
[ col
] );
1168 node
= node
->GetNext();
1172 AdjustForFlexDirection();
1174 // Sum total minimum size, including gaps between rows/columns.
1175 // -1 is used as a magic number meaning empty column.
1177 for (int col
= 0; col
< ncols
; col
++)
1178 if ( m_colWidths
[ col
] != -1 )
1179 width
+= m_colWidths
[ col
] + ( col
== ncols
-1 ? 0 : m_hgap
);
1182 for (int row
= 0; row
< nrows
; row
++)
1183 if ( m_rowHeights
[ row
] != -1 )
1184 height
+= m_rowHeights
[ row
] + ( row
== nrows
-1 ? 0 : m_vgap
);
1186 return wxSize( width
, height
);
1189 void wxFlexGridSizer::AdjustForFlexDirection()
1191 // the logic in CalcMin works when we resize flexibly in both directions
1192 // but maybe this is not the case
1193 if ( m_flexDirection
!= wxBOTH
)
1195 // select the array corresponding to the direction in which we do *not*
1197 wxArrayInt
& array
= m_flexDirection
== wxVERTICAL
? m_colWidths
1200 const int count
= array
.GetCount();
1202 // find the largest value in this array
1204 for ( n
= 0; n
< count
; ++n
)
1206 if ( array
[n
] > largest
)
1210 // and now fill it with the largest value
1211 for ( n
= 0; n
< count
; ++n
)
1219 void wxFlexGridSizer::AdjustForGrowables(const wxSize
& sz
, const wxSize
& minsz
,
1220 int nrows
, int ncols
)
1222 // what to do with the rows? by default, resize them proportionally
1223 if ( sz
.y
> minsz
.y
&& ( (m_flexDirection
& wxVERTICAL
) || (m_growMode
== wxFLEX_GROWMODE_SPECIFIED
) ) )
1225 int sum_proportions
= 0;
1226 int growable_space
= 0;
1229 for (idx
= 0; idx
< m_growableRows
.GetCount(); idx
++)
1231 // Since the number of rows/columns can change as items are
1232 // inserted/deleted, we need to verify at runtime that the
1233 // requested growable rows/columns are still valid.
1234 if (m_growableRows
[idx
] >= nrows
)
1237 // If all items in a row/column are hidden, that row/column will
1238 // have a dimension of -1. This causes the row/column to be
1239 // hidden completely.
1240 if (m_rowHeights
[ m_growableRows
[idx
] ] == -1)
1242 sum_proportions
+= m_growableRowsProportions
[idx
];
1243 growable_space
+= m_rowHeights
[ m_growableRows
[idx
] ];
1249 for (idx
= 0; idx
< m_growableRows
.GetCount(); idx
++)
1251 if (m_growableRows
[idx
] >= nrows
)
1253 if (m_rowHeights
[ m_growableRows
[idx
] ] == -1)
1254 m_rowHeights
[ m_growableRows
[idx
] ] = 0;
1257 int delta
= (sz
.y
- minsz
.y
);
1258 if (sum_proportions
== 0)
1259 delta
= (delta
/num
) + m_rowHeights
[ m_growableRows
[idx
] ];
1261 delta
= ((delta
+growable_space
)*m_growableRowsProportions
[idx
]) / sum_proportions
;
1262 m_rowHeights
[ m_growableRows
[idx
] ] = delta
;
1267 else if ( (m_growMode
== wxFLEX_GROWMODE_ALL
) && (sz
.y
> minsz
.y
) )
1269 // rounding problem?
1270 for ( int row
= 0; row
< nrows
; ++row
)
1271 m_rowHeights
[ row
] = sz
.y
/ nrows
;
1274 // the same logic as above but for the columns
1275 if ( sz
.x
> minsz
.x
&& ( (m_flexDirection
& wxHORIZONTAL
) || (m_growMode
== wxFLEX_GROWMODE_SPECIFIED
) ) )
1277 int sum_proportions
= 0;
1278 int growable_space
= 0;
1281 for (idx
= 0; idx
< m_growableCols
.GetCount(); idx
++)
1283 // Since the number of rows/columns can change as items are
1284 // inserted/deleted, we need to verify at runtime that the
1285 // requested growable rows/columns are still valid.
1286 if (m_growableCols
[idx
] >= ncols
)
1289 // If all items in a row/column are hidden, that row/column will
1290 // have a dimension of -1. This causes the column to be hidden
1292 if (m_colWidths
[ m_growableCols
[idx
] ] == -1)
1294 sum_proportions
+= m_growableColsProportions
[idx
];
1295 growable_space
+= m_colWidths
[ m_growableCols
[idx
] ];
1301 for (idx
= 0; idx
< m_growableCols
.GetCount(); idx
++)
1303 if (m_growableCols
[idx
] >= ncols
)
1305 if (m_colWidths
[ m_growableCols
[idx
] ] == -1)
1306 m_colWidths
[ m_growableCols
[idx
] ] = 0;
1309 int delta
= (sz
.x
- minsz
.x
);
1310 if (sum_proportions
== 0)
1311 delta
= (delta
/num
) + m_colWidths
[ m_growableCols
[idx
] ];
1313 delta
= ((delta
+growable_space
)*m_growableColsProportions
[idx
])/sum_proportions
;
1314 m_colWidths
[ m_growableCols
[idx
] ] = delta
;
1319 else if ( (m_growMode
== wxFLEX_GROWMODE_ALL
) && (sz
.x
> minsz
.x
) )
1321 for ( int col
=0; col
< ncols
; ++col
)
1322 m_colWidths
[ col
] = sz
.x
/ ncols
;
1327 void wxFlexGridSizer::AddGrowableRow( size_t idx
, int proportion
)
1329 m_growableRows
.Add( idx
);
1330 m_growableRowsProportions
.Add( proportion
);
1333 void wxFlexGridSizer::RemoveGrowableRow( size_t idx
)
1335 m_growableRows
.Remove( idx
);
1338 void wxFlexGridSizer::AddGrowableCol( size_t idx
, int proportion
)
1340 m_growableCols
.Add( idx
);
1341 m_growableColsProportions
.Add( proportion
);
1344 void wxFlexGridSizer::RemoveGrowableCol( size_t idx
)
1346 m_growableCols
.Remove( idx
);
1349 //---------------------------------------------------------------------------
1351 //---------------------------------------------------------------------------
1353 wxBoxSizer::wxBoxSizer( int orient
)
1354 : m_orient( orient
)
1358 void wxBoxSizer::RecalcSizes()
1360 if (m_children
.GetCount() == 0)
1366 if (m_orient
== wxHORIZONTAL
)
1367 delta
= m_size
.x
- m_fixedWidth
;
1369 delta
= m_size
.y
- m_fixedHeight
;
1372 wxPoint
pt( m_position
);
1374 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
1377 wxSizerItem
*item
= node
->GetData();
1379 if (item
->IsShown())
1381 wxSize
size( item
->CalcMin() );
1383 if (m_orient
== wxVERTICAL
)
1385 wxCoord height
= size
.y
;
1386 if (item
->GetProportion())
1388 // Because of at least one visible item has non-zero
1389 // proportion then m_stretchable is not zero
1390 height
= (delta
* item
->GetProportion()) / m_stretchable
;
1393 wxPoint
child_pos( pt
);
1394 wxSize
child_size( wxSize( size
.x
, height
) );
1396 if (item
->GetFlag() & (wxEXPAND
| wxSHAPED
))
1397 child_size
.x
= m_size
.x
;
1398 else if (item
->GetFlag() & wxALIGN_RIGHT
)
1399 child_pos
.x
+= m_size
.x
- size
.x
;
1400 else if (item
->GetFlag() & (wxCENTER
| wxALIGN_CENTER_HORIZONTAL
))
1401 // XXX wxCENTER is added for backward compatibility;
1402 // wxALIGN_CENTER should be used in new code
1403 child_pos
.x
+= (m_size
.x
- size
.x
) / 2;
1405 item
->SetDimension( child_pos
, child_size
);
1411 wxCoord width
= size
.x
;
1412 if (item
->GetProportion())
1414 // Because of at least one visible item has non-zero
1415 // proportion then m_stretchable is not zero
1416 width
= (delta
* item
->GetProportion()) / m_stretchable
;
1419 wxPoint
child_pos( pt
);
1420 wxSize
child_size( wxSize(width
, size
.y
) );
1422 if (item
->GetFlag() & (wxEXPAND
| wxSHAPED
))
1423 child_size
.y
= m_size
.y
;
1424 else if (item
->GetFlag() & wxALIGN_BOTTOM
)
1425 child_pos
.y
+= m_size
.y
- size
.y
;
1426 else if (item
->GetFlag() & (wxCENTER
| wxALIGN_CENTER_VERTICAL
))
1427 // XXX wxCENTER is added for backward compatibility;
1428 // wxALIGN_CENTER should be used in new code
1429 child_pos
.y
+= (m_size
.y
- size
.y
) / 2;
1431 item
->SetDimension( child_pos
, child_size
);
1437 node
= node
->GetNext();
1441 wxSize
wxBoxSizer::CalcMin()
1443 if (m_children
.GetCount() == 0)
1444 return wxSize(10,10);
1452 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
1455 wxSizerItem
*item
= node
->GetData();
1457 if (item
->IsShown() && item
->GetProportion() != 0)
1458 m_stretchable
+= item
->GetProportion();
1460 node
= node
->GetNext();
1463 // Total minimum size (width or height) of sizer
1466 node
= m_children
.GetFirst();
1469 wxSizerItem
*item
= node
->GetData();
1471 if (item
->IsShown() && item
->GetProportion() != 0)
1473 int stretch
= item
->GetProportion();
1474 wxSize
size( item
->CalcMin() );
1477 // Integer division rounded up is (a + b - 1) / b
1478 // Round up needed in order to guarantee that all
1479 // all items will have size not less then their min size
1480 if (m_orient
== wxHORIZONTAL
)
1481 minSize
= ( size
.x
*m_stretchable
+ stretch
- 1)/stretch
;
1483 minSize
= ( size
.y
*m_stretchable
+ stretch
- 1)/stretch
;
1485 if (minSize
> maxMinSize
)
1486 maxMinSize
= minSize
;
1488 node
= node
->GetNext();
1491 // Calculate overall minimum size
1492 node
= m_children
.GetFirst();
1495 wxSizerItem
*item
= node
->GetData();
1497 if (item
->IsShown())
1499 wxSize
size( item
->CalcMin() );
1500 if (item
->GetProportion() != 0)
1502 if (m_orient
== wxHORIZONTAL
)
1503 size
.x
= (maxMinSize
*item
->GetProportion())/m_stretchable
;
1505 size
.y
= (maxMinSize
*item
->GetProportion())/m_stretchable
;
1509 if (m_orient
== wxVERTICAL
)
1511 m_fixedHeight
+= size
.y
;
1512 m_fixedWidth
= wxMax( m_fixedWidth
, size
.x
);
1516 m_fixedWidth
+= size
.x
;
1517 m_fixedHeight
= wxMax( m_fixedHeight
, size
.y
);
1521 if (m_orient
== wxHORIZONTAL
)
1523 m_minWidth
+= size
.x
;
1524 m_minHeight
= wxMax( m_minHeight
, size
.y
);
1528 m_minHeight
+= size
.y
;
1529 m_minWidth
= wxMax( m_minWidth
, size
.x
);
1532 node
= node
->GetNext();
1535 return wxSize( m_minWidth
, m_minHeight
);
1538 //---------------------------------------------------------------------------
1540 //---------------------------------------------------------------------------
1544 wxStaticBoxSizer::wxStaticBoxSizer( wxStaticBox
*box
, int orient
)
1545 : wxBoxSizer( orient
)
1546 , m_staticBox( box
)
1548 wxASSERT_MSG( box
, wxT("wxStaticBoxSizer needs a static box") );
1551 static void GetStaticBoxBorders( wxStaticBox
*box
,
1555 // this has to be done platform by platform as there is no way to
1556 // guess the thickness of a wxStaticBox border
1558 box
->GetBordersForSizer(borderTop
,borderOther
);
1559 #elif defined(__WXMAC__)
1561 static int extraTop
= -1; // Uninitted
1562 static int other
= 5;
1564 if ( extraTop
== -1 )
1566 // The minimal border used for the top. Later on the staticbox'
1567 // font height is added to this.
1570 if ( UMAGetSystemVersion() >= 0x1030 /*Panther*/ )
1572 // As indicated by the HIG, Panther needs an extra border of 11
1573 // pixels (otherwise overlapping occurs at the top). The "other"
1574 // border has to be 11.
1581 *borderTop
= extraTop
+ box
->GetCharHeight();
1582 *borderOther
= other
;
1586 if ( box
->GetLabel().IsEmpty() )
1590 *borderTop
= box
->GetCharHeight();
1593 #endif // __WXCOCOA__
1596 void wxStaticBoxSizer::RecalcSizes()
1598 int top_border
, other_border
;
1599 GetStaticBoxBorders(m_staticBox
, &top_border
, &other_border
);
1601 m_staticBox
->SetSize( m_position
.x
, m_position
.y
, m_size
.x
, m_size
.y
);
1603 wxPoint
old_pos( m_position
);
1604 m_position
.x
+= other_border
;
1605 m_position
.y
+= top_border
;
1606 wxSize
old_size( m_size
);
1607 m_size
.x
-= 2*other_border
;
1608 m_size
.y
-= top_border
+ other_border
;
1610 wxBoxSizer::RecalcSizes();
1612 m_position
= old_pos
;
1616 wxSize
wxStaticBoxSizer::CalcMin()
1618 int top_border
, other_border
;
1619 GetStaticBoxBorders(m_staticBox
, &top_border
, &other_border
);
1621 wxSize
ret( wxBoxSizer::CalcMin() );
1622 ret
.x
+= 2*other_border
;
1623 ret
.y
+= other_border
+ top_border
;
1628 #endif // wxUSE_STATBOX
1630 // ----------------------------------------------------------------------------
1632 // ----------------------------------------------------------------------------
1636 wxBookCtrlSizer::wxBookCtrlSizer(wxBookCtrl
*bookctrl
)
1637 : m_bookctrl(bookctrl
)
1639 wxASSERT_MSG( bookctrl
, wxT("wxBookCtrlSizer needs a control") );
1642 void wxBookCtrlSizer::RecalcSizes()
1644 m_bookctrl
->SetSize( m_position
.x
, m_position
.y
, m_size
.x
, m_size
.y
);
1647 wxSize
wxBookCtrlSizer::CalcMin()
1649 wxSize sizeBorder
= m_bookctrl
->CalcSizeFromPage(wxSize(0, 0));
1654 if ( m_bookctrl
->GetPageCount() == 0 )
1656 return wxSize(sizeBorder
.x
+ 10, sizeBorder
.y
+ 10);
1662 wxWindowList::compatibility_iterator
1663 node
= m_bookctrl
->GetChildren().GetFirst();
1666 wxWindow
*item
= node
->GetData();
1667 wxSizer
*itemsizer
= item
->GetSizer();
1671 wxSize
subsize( itemsizer
->CalcMin() );
1673 if (subsize
.x
> maxX
)
1675 if (subsize
.y
> maxY
)
1679 node
= node
->GetNext();
1682 return wxSize( maxX
, maxY
) + sizeBorder
;
1688 wxNotebookSizer::wxNotebookSizer(wxNotebook
*nb
)
1689 : wxBookCtrlSizer(nb
)
1693 #endif // wxUSE_NOTEBOOOK
1694 #endif // wxUSE_BOOKCTRL