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 // Return a window size that will fit within the screens dimensions
649 wxSize
wxSizer::FitSize( wxWindow
*window
)
651 wxSize size
= GetMinWindowSize( window
);
652 wxSize sizeMax
= GetMaxWindowSize( window
);
654 // Limit the size if sizeMax != wxDefaultSize
656 if ( size
.x
> sizeMax
.x
&& sizeMax
.x
!= -1 )
658 if ( size
.y
> sizeMax
.y
&& sizeMax
.y
!= -1 )
664 wxSize
wxSizer::GetMaxClientSize( wxWindow
*window
) const
666 wxSize
maxSize( window
->GetMaxSize() );
668 if( maxSize
!= wxDefaultSize
)
670 wxSize
size( window
->GetSize() );
671 wxSize
client_size( window
->GetClientSize() );
673 return wxSize( maxSize
.x
+ client_size
.x
- size
.x
,
674 maxSize
.y
+ client_size
.y
- size
.y
);
677 return wxDefaultSize
;
680 wxSize
wxSizer::GetMinClientSize( wxWindow
*WXUNUSED(window
) )
682 return GetMinSize(); // Already returns client size.
685 wxSize
wxSizer::VirtualFitSize( wxWindow
*window
)
687 wxSize size
= GetMinClientSize( window
);
688 wxSize sizeMax
= GetMaxClientSize( window
);
690 // Limit the size if sizeMax != wxDefaultSize
692 if ( size
.x
> sizeMax
.x
&& sizeMax
.x
!= -1 )
694 if ( size
.y
> sizeMax
.y
&& sizeMax
.y
!= -1 )
700 void wxSizer::SetDimension( int x
, int y
, int width
, int height
)
709 wxSize
wxSizer::GetMinSize()
711 wxSize
ret( CalcMin() );
712 if (ret
.x
< m_minSize
.x
) ret
.x
= m_minSize
.x
;
713 if (ret
.y
< m_minSize
.y
) ret
.y
= m_minSize
.y
;
717 void wxSizer::DoSetMinSize( int width
, int height
)
720 m_minSize
.y
= height
;
723 bool wxSizer::DoSetItemMinSize( wxWindow
*window
, int width
, int height
)
725 wxASSERT_MSG( window
, _T("SetMinSize for NULL window") );
727 // Is it our immediate child?
729 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
732 wxSizerItem
*item
= node
->GetData();
734 if (item
->GetWindow() == window
)
736 item
->SetInitSize( width
, height
);
739 node
= node
->GetNext();
742 // No? Search any subsizers we own then
744 node
= m_children
.GetFirst();
747 wxSizerItem
*item
= node
->GetData();
749 if ( item
->GetSizer() &&
750 item
->GetSizer()->DoSetItemMinSize( window
, width
, height
) )
752 // A child sizer found the requested windw, exit.
755 node
= node
->GetNext();
761 bool wxSizer::DoSetItemMinSize( wxSizer
*sizer
, int width
, int height
)
763 wxASSERT_MSG( sizer
, _T("SetMinSize for NULL sizer") );
765 // Is it our immediate child?
767 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
770 wxSizerItem
*item
= node
->GetData();
772 if (item
->GetSizer() == sizer
)
774 item
->GetSizer()->DoSetMinSize( width
, height
);
777 node
= node
->GetNext();
780 // No? Search any subsizers we own then
782 node
= m_children
.GetFirst();
785 wxSizerItem
*item
= node
->GetData();
787 if ( item
->GetSizer() &&
788 item
->GetSizer()->DoSetItemMinSize( sizer
, width
, height
) )
790 // A child found the requested sizer, exit.
793 node
= node
->GetNext();
799 bool wxSizer::DoSetItemMinSize( size_t index
, int width
, int height
)
801 wxSizerItemList::compatibility_iterator node
= m_children
.Item( index
);
803 wxCHECK_MSG( node
, false, _T("Failed to find child node") );
805 wxSizerItem
*item
= node
->GetData();
807 if (item
->GetSizer())
809 // Sizers contains the minimal size in them, if not calculated ...
810 item
->GetSizer()->DoSetMinSize( width
, height
);
814 // ... but the minimal size of spacers and windows in stored in them
815 item
->SetInitSize( width
, height
);
821 void wxSizer::Show( wxWindow
*window
, bool show
)
823 wxASSERT_MSG( window
, _T("Show for NULL window") );
825 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
828 wxSizerItem
*item
= node
->GetData();
830 if (item
->GetWindow() == window
)
835 node
= node
->GetNext();
839 void wxSizer::Show( wxSizer
*sizer
, bool show
)
841 wxASSERT_MSG( sizer
, _T("Show for NULL sizer") );
843 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
846 wxSizerItem
*item
= node
->GetData();
848 if (item
->GetSizer() == sizer
)
853 node
= node
->GetNext();
857 void wxSizer::Show( size_t index
, bool show
)
859 wxCHECK_RET( index
< m_children
.GetCount(),
860 _T("Show index is out of range") );
862 m_children
.Item( index
)->GetData()->Show( show
);
865 void wxSizer::ShowItems( bool show
)
867 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
870 node
->GetData()->Show( show
);
871 node
= node
->GetNext();
875 bool wxSizer::IsShown( wxWindow
*window
) const
877 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
880 wxSizerItem
*item
= node
->GetData();
882 if (item
->GetWindow() == window
)
884 return item
->IsShown();
886 node
= node
->GetNext();
889 wxFAIL_MSG( _T("IsShown failed to find sizer item") );
894 bool wxSizer::IsShown( wxSizer
*sizer
) const
896 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
899 wxSizerItem
*item
= node
->GetData();
901 if (item
->GetSizer() == sizer
)
903 return item
->IsShown();
905 node
= node
->GetNext();
908 wxFAIL_MSG( _T("IsShown failed to find sizer item") );
913 bool wxSizer::IsShown( size_t index
) const
915 wxCHECK_MSG( index
< m_children
.GetCount(),
917 _T("IsShown index is out of range") );
919 return m_children
.Item( index
)->GetData()->IsShown();
923 //---------------------------------------------------------------------------
925 //---------------------------------------------------------------------------
927 wxGridSizer::wxGridSizer( int rows
, int cols
, int vgap
, int hgap
)
933 if (m_rows
== 0 && m_cols
== 0)
937 wxGridSizer::wxGridSizer( int cols
, int vgap
, int hgap
)
943 if (m_rows
== 0 && m_cols
== 0)
947 int wxGridSizer::CalcRowsCols(int& nrows
, int& ncols
) const
949 int nitems
= m_children
.GetCount();
955 nrows
= (nitems
+ m_cols
- 1) / m_cols
;
959 ncols
= (nitems
+ m_rows
- 1) / m_rows
;
962 else // 0 columns, 0 rows?
964 wxFAIL_MSG( _T("grid sizer must have either rows or columns fixed") );
973 void wxGridSizer::RecalcSizes()
975 int nitems
, nrows
, ncols
;
976 if ( (nitems
= CalcRowsCols(nrows
, ncols
)) == 0 )
979 wxSize
sz( GetSize() );
980 wxPoint
pt( GetPosition() );
982 int w
= (sz
.x
- (ncols
- 1) * m_hgap
) / ncols
;
983 int h
= (sz
.y
- (nrows
- 1) * m_vgap
) / nrows
;
986 for (int c
= 0; c
< ncols
; c
++)
989 for (int r
= 0; r
< nrows
; r
++)
991 int i
= r
* ncols
+ c
;
994 wxSizerItemList::compatibility_iterator node
= m_children
.Item( i
);
996 wxASSERT_MSG( node
, _T("Failed to find SizerItemList node") );
998 SetItemBounds( node
->GetData(), x
, y
, w
, h
);
1006 wxSize
wxGridSizer::CalcMin()
1009 if ( CalcRowsCols(nrows
, ncols
) == 0 )
1010 return wxSize(10, 10);
1012 // Find the max width and height for any component
1016 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
1019 wxSizerItem
*item
= node
->GetData();
1020 wxSize
sz( item
->CalcMin() );
1022 w
= wxMax( w
, sz
.x
);
1023 h
= wxMax( h
, sz
.y
);
1025 node
= node
->GetNext();
1028 return wxSize( ncols
* w
+ (ncols
-1) * m_hgap
,
1029 nrows
* h
+ (nrows
-1) * m_vgap
);
1032 void wxGridSizer::SetItemBounds( wxSizerItem
*item
, int x
, int y
, int w
, int h
)
1035 wxSize
sz( item
->CalcMin() );
1036 int flag
= item
->GetFlag();
1038 if ((flag
& wxEXPAND
) || (flag
& wxSHAPED
))
1044 if (flag
& wxALIGN_CENTER_HORIZONTAL
)
1046 pt
.x
= x
+ (w
- sz
.x
) / 2;
1048 else if (flag
& wxALIGN_RIGHT
)
1050 pt
.x
= x
+ (w
- sz
.x
);
1053 if (flag
& wxALIGN_CENTER_VERTICAL
)
1055 pt
.y
= y
+ (h
- sz
.y
) / 2;
1057 else if (flag
& wxALIGN_BOTTOM
)
1059 pt
.y
= y
+ (h
- sz
.y
);
1063 item
->SetDimension(pt
, sz
);
1066 //---------------------------------------------------------------------------
1068 //---------------------------------------------------------------------------
1070 wxFlexGridSizer::wxFlexGridSizer( int rows
, int cols
, int vgap
, int hgap
)
1071 : wxGridSizer( rows
, cols
, vgap
, hgap
),
1072 m_flexDirection(wxBOTH
),
1073 m_growMode(wxFLEX_GROWMODE_SPECIFIED
)
1077 wxFlexGridSizer::wxFlexGridSizer( int cols
, int vgap
, int hgap
)
1078 : wxGridSizer( cols
, vgap
, hgap
),
1079 m_flexDirection(wxBOTH
),
1080 m_growMode(wxFLEX_GROWMODE_SPECIFIED
)
1084 wxFlexGridSizer::~wxFlexGridSizer()
1088 void wxFlexGridSizer::RecalcSizes()
1090 int nitems
, nrows
, ncols
;
1091 if ( (nitems
= CalcRowsCols(nrows
, ncols
)) == 0 )
1094 wxPoint
pt( GetPosition() );
1095 wxSize
sz( GetSize() );
1096 wxSize
minsz( CalcMin() );
1098 AdjustForGrowables(sz
, minsz
, nrows
, ncols
);
1100 sz
= wxSize( pt
.x
+ sz
.x
, pt
.y
+ sz
.y
);
1103 for (int c
= 0; c
< ncols
; c
++)
1106 for (int r
= 0; r
< nrows
; r
++)
1108 int i
= r
* ncols
+ c
;
1111 wxSizerItemList::compatibility_iterator node
= m_children
.Item( i
);
1113 wxASSERT_MSG( node
, _T("Failed to find node") );
1115 int w
= wxMax( 0, wxMin( m_colWidths
[c
], sz
.x
- x
) );
1116 int h
= wxMax( 0, wxMin( m_rowHeights
[r
], sz
.y
- y
) );
1118 SetItemBounds( node
->GetData(), x
, y
, w
, h
);
1120 y
= y
+ m_rowHeights
[r
] + m_vgap
;
1122 x
= x
+ m_colWidths
[c
] + m_hgap
;
1126 wxSize
wxFlexGridSizer::CalcMin()
1132 // Number of rows/columns can change as items are added or removed.
1133 if ( !CalcRowsCols(nrows
, ncols
) )
1134 return wxSize(10, 10);
1136 m_rowHeights
.SetCount(nrows
);
1137 m_colWidths
.SetCount(ncols
);
1139 // We have to recalcuate the sizes in case an item has wxADJUST_MINSIZE, has changed
1140 // minimum size since the previous layout, or has been hidden using wxSizer::Show().
1141 // If all the items in a row/column are hidden, the final dimension of the row/column
1142 // will be -1, indicating that the column itself is hidden.
1143 for( s
= m_rowHeights
.GetCount(), i
= 0; i
< s
; ++i
)
1144 m_rowHeights
[ i
] = -1;
1145 for( s
= m_colWidths
.GetCount(), i
= 0; i
< s
; ++i
)
1146 m_colWidths
[ i
] = -1;
1148 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
1153 wxSizerItem
*item
= node
->GetData();
1154 if ( item
->IsShown() )
1156 wxSize
sz( item
->CalcMin() );
1157 int row
= i
/ ncols
;
1158 int col
= i
% ncols
;
1160 m_rowHeights
[ row
] = wxMax( wxMax( 0, sz
.y
), m_rowHeights
[ row
] );
1161 m_colWidths
[ col
] = wxMax( wxMax( 0, sz
.x
), m_colWidths
[ col
] );
1164 node
= node
->GetNext();
1168 AdjustForFlexDirection();
1170 // Sum total minimum size, including gaps between rows/columns.
1171 // -1 is used as a magic number meaning empty column.
1173 for (int col
= 0; col
< ncols
; col
++)
1174 if ( m_colWidths
[ col
] != -1 )
1175 width
+= m_colWidths
[ col
] + ( col
== ncols
-1 ? 0 : m_hgap
);
1178 for (int row
= 0; row
< nrows
; row
++)
1179 if ( m_rowHeights
[ row
] != -1 )
1180 height
+= m_rowHeights
[ row
] + ( row
== nrows
-1 ? 0 : m_vgap
);
1182 return wxSize( width
, height
);
1185 void wxFlexGridSizer::AdjustForFlexDirection()
1187 // the logic in CalcMin works when we resize flexibly in both directions
1188 // but maybe this is not the case
1189 if ( m_flexDirection
!= wxBOTH
)
1191 // select the array corresponding to the direction in which we do *not*
1193 wxArrayInt
& array
= m_flexDirection
== wxVERTICAL
? m_colWidths
1196 const int count
= array
.GetCount();
1198 // find the largest value in this array
1200 for ( n
= 0; n
< count
; ++n
)
1202 if ( array
[n
] > largest
)
1206 // and now fill it with the largest value
1207 for ( n
= 0; n
< count
; ++n
)
1215 void wxFlexGridSizer::AdjustForGrowables(const wxSize
& sz
, const wxSize
& minsz
,
1216 int nrows
, int ncols
)
1218 // what to do with the rows? by default, resize them proportionally
1219 if ( sz
.y
> minsz
.y
&& ( (m_flexDirection
& wxVERTICAL
) || (m_growMode
== wxFLEX_GROWMODE_SPECIFIED
) ) )
1221 int sum_proportions
= 0;
1222 int growable_space
= 0;
1225 for (idx
= 0; idx
< m_growableRows
.GetCount(); idx
++)
1227 // Since the number of rows/columns can change as items are
1228 // inserted/deleted, we need to verify at runtime that the
1229 // requested growable rows/columns are still valid.
1230 if (m_growableRows
[idx
] >= nrows
)
1233 // If all items in a row/column are hidden, that row/column will
1234 // have a dimension of -1. This causes the row/column to be
1235 // hidden completely.
1236 if (m_rowHeights
[ m_growableRows
[idx
] ] == -1)
1238 sum_proportions
+= m_growableRowsProportions
[idx
];
1239 growable_space
+= m_rowHeights
[ m_growableRows
[idx
] ];
1245 for (idx
= 0; idx
< m_growableRows
.GetCount(); idx
++)
1247 if (m_growableRows
[idx
] >= nrows
)
1249 if (m_rowHeights
[ m_growableRows
[idx
] ] == -1)
1250 m_rowHeights
[ m_growableRows
[idx
] ] = 0;
1253 int delta
= (sz
.y
- minsz
.y
);
1254 if (sum_proportions
== 0)
1255 delta
= (delta
/num
) + m_rowHeights
[ m_growableRows
[idx
] ];
1257 delta
= ((delta
+growable_space
)*m_growableRowsProportions
[idx
]) / sum_proportions
;
1258 m_rowHeights
[ m_growableRows
[idx
] ] = delta
;
1263 else if ( (m_growMode
== wxFLEX_GROWMODE_ALL
) && (sz
.y
> minsz
.y
) )
1265 // rounding problem?
1266 for ( int row
= 0; row
< nrows
; ++row
)
1267 m_rowHeights
[ row
] = sz
.y
/ nrows
;
1270 // the same logic as above but for the columns
1271 if ( sz
.x
> minsz
.x
&& ( (m_flexDirection
& wxHORIZONTAL
) || (m_growMode
== wxFLEX_GROWMODE_SPECIFIED
) ) )
1273 int sum_proportions
= 0;
1274 int growable_space
= 0;
1277 for (idx
= 0; idx
< m_growableCols
.GetCount(); idx
++)
1279 // Since the number of rows/columns can change as items are
1280 // inserted/deleted, we need to verify at runtime that the
1281 // requested growable rows/columns are still valid.
1282 if (m_growableCols
[idx
] >= ncols
)
1285 // If all items in a row/column are hidden, that row/column will
1286 // have a dimension of -1. This causes the column to be hidden
1288 if (m_colWidths
[ m_growableCols
[idx
] ] == -1)
1290 sum_proportions
+= m_growableColsProportions
[idx
];
1291 growable_space
+= m_colWidths
[ m_growableCols
[idx
] ];
1297 for (idx
= 0; idx
< m_growableCols
.GetCount(); idx
++)
1299 if (m_growableCols
[idx
] >= ncols
)
1301 if (m_colWidths
[ m_growableCols
[idx
] ] == -1)
1302 m_colWidths
[ m_growableCols
[idx
] ] = 0;
1305 int delta
= (sz
.x
- minsz
.x
);
1306 if (sum_proportions
== 0)
1307 delta
= (delta
/num
) + m_colWidths
[ m_growableCols
[idx
] ];
1309 delta
= ((delta
+growable_space
)*m_growableColsProportions
[idx
])/sum_proportions
;
1310 m_colWidths
[ m_growableCols
[idx
] ] = delta
;
1315 else if ( (m_growMode
== wxFLEX_GROWMODE_ALL
) && (sz
.x
> minsz
.x
) )
1317 for ( int col
=0; col
< ncols
; ++col
)
1318 m_colWidths
[ col
] = sz
.x
/ ncols
;
1323 void wxFlexGridSizer::AddGrowableRow( size_t idx
, int proportion
)
1325 m_growableRows
.Add( idx
);
1326 m_growableRowsProportions
.Add( proportion
);
1329 void wxFlexGridSizer::RemoveGrowableRow( size_t idx
)
1331 m_growableRows
.Remove( idx
);
1334 void wxFlexGridSizer::AddGrowableCol( size_t idx
, int proportion
)
1336 m_growableCols
.Add( idx
);
1337 m_growableColsProportions
.Add( proportion
);
1340 void wxFlexGridSizer::RemoveGrowableCol( size_t idx
)
1342 m_growableCols
.Remove( idx
);
1345 //---------------------------------------------------------------------------
1347 //---------------------------------------------------------------------------
1349 wxBoxSizer::wxBoxSizer( int orient
)
1350 : m_orient( orient
)
1354 void wxBoxSizer::RecalcSizes()
1356 if (m_children
.GetCount() == 0)
1362 if (m_orient
== wxHORIZONTAL
)
1363 delta
= m_size
.x
- m_fixedWidth
;
1365 delta
= m_size
.y
- m_fixedHeight
;
1368 wxPoint
pt( m_position
);
1370 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
1373 wxSizerItem
*item
= node
->GetData();
1375 if (item
->IsShown())
1377 wxSize
size( item
->CalcMin() );
1379 if (m_orient
== wxVERTICAL
)
1381 wxCoord height
= size
.y
;
1382 if (item
->GetProportion())
1384 // Because of at least one visible item has non-zero
1385 // proportion then m_stretchable is not zero
1386 height
= (delta
* item
->GetProportion()) / m_stretchable
;
1389 wxPoint
child_pos( pt
);
1390 wxSize
child_size( wxSize( size
.x
, height
) );
1392 if (item
->GetFlag() & (wxEXPAND
| wxSHAPED
))
1393 child_size
.x
= m_size
.x
;
1394 else if (item
->GetFlag() & wxALIGN_RIGHT
)
1395 child_pos
.x
+= m_size
.x
- size
.x
;
1396 else if (item
->GetFlag() & (wxCENTER
| wxALIGN_CENTER_HORIZONTAL
))
1397 // XXX wxCENTER is added for backward compatibility;
1398 // wxALIGN_CENTER should be used in new code
1399 child_pos
.x
+= (m_size
.x
- size
.x
) / 2;
1401 item
->SetDimension( child_pos
, child_size
);
1407 wxCoord width
= size
.x
;
1408 if (item
->GetProportion())
1410 // Because of at least one visible item has non-zero
1411 // proportion then m_stretchable is not zero
1412 width
= (delta
* item
->GetProportion()) / m_stretchable
;
1415 wxPoint
child_pos( pt
);
1416 wxSize
child_size( wxSize(width
, size
.y
) );
1418 if (item
->GetFlag() & (wxEXPAND
| wxSHAPED
))
1419 child_size
.y
= m_size
.y
;
1420 else if (item
->GetFlag() & wxALIGN_BOTTOM
)
1421 child_pos
.y
+= m_size
.y
- size
.y
;
1422 else if (item
->GetFlag() & (wxCENTER
| wxALIGN_CENTER_VERTICAL
))
1423 // XXX wxCENTER is added for backward compatibility;
1424 // wxALIGN_CENTER should be used in new code
1425 child_pos
.y
+= (m_size
.y
- size
.y
) / 2;
1427 item
->SetDimension( child_pos
, child_size
);
1433 node
= node
->GetNext();
1437 wxSize
wxBoxSizer::CalcMin()
1439 if (m_children
.GetCount() == 0)
1440 return wxSize(10,10);
1448 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
1451 wxSizerItem
*item
= node
->GetData();
1453 if (item
->IsShown() && item
->GetProportion() != 0)
1454 m_stretchable
+= item
->GetProportion();
1456 node
= node
->GetNext();
1459 // Total minimum size (width or height) of sizer
1462 node
= m_children
.GetFirst();
1465 wxSizerItem
*item
= node
->GetData();
1467 if (item
->IsShown() && item
->GetProportion() != 0)
1469 int stretch
= item
->GetProportion();
1470 wxSize
size( item
->CalcMin() );
1473 // Integer division rounded up is (a + b - 1) / b
1474 // Round up needed in order to guarantee that all
1475 // all items will have size not less then their min size
1476 if (m_orient
== wxHORIZONTAL
)
1477 minSize
= ( size
.x
*m_stretchable
+ stretch
- 1)/stretch
;
1479 minSize
= ( size
.y
*m_stretchable
+ stretch
- 1)/stretch
;
1481 if (minSize
> maxMinSize
)
1482 maxMinSize
= minSize
;
1484 node
= node
->GetNext();
1487 // Calculate overall minimum size
1488 node
= m_children
.GetFirst();
1491 wxSizerItem
*item
= node
->GetData();
1493 if (item
->IsShown())
1495 wxSize
size( item
->CalcMin() );
1496 if (item
->GetProportion() != 0)
1498 if (m_orient
== wxHORIZONTAL
)
1499 size
.x
= (maxMinSize
*item
->GetProportion())/m_stretchable
;
1501 size
.y
= (maxMinSize
*item
->GetProportion())/m_stretchable
;
1505 if (m_orient
== wxVERTICAL
)
1507 m_fixedHeight
+= size
.y
;
1508 m_fixedWidth
= wxMax( m_fixedWidth
, size
.x
);
1512 m_fixedWidth
+= size
.x
;
1513 m_fixedHeight
= wxMax( m_fixedHeight
, size
.y
);
1517 if (m_orient
== wxHORIZONTAL
)
1519 m_minWidth
+= size
.x
;
1520 m_minHeight
= wxMax( m_minHeight
, size
.y
);
1524 m_minHeight
+= size
.y
;
1525 m_minWidth
= wxMax( m_minWidth
, size
.x
);
1528 node
= node
->GetNext();
1531 return wxSize( m_minWidth
, m_minHeight
);
1534 //---------------------------------------------------------------------------
1536 //---------------------------------------------------------------------------
1540 wxStaticBoxSizer::wxStaticBoxSizer( wxStaticBox
*box
, int orient
)
1541 : wxBoxSizer( orient
)
1542 , m_staticBox( box
)
1544 wxASSERT_MSG( box
, wxT("wxStaticBoxSizer needs a static box") );
1547 static void GetStaticBoxBorders( wxStaticBox
*box
,
1551 // this has to be done platform by platform as there is no way to
1552 // guess the thickness of a wxStaticBox border
1554 box
->GetBordersForSizer(borderTop
,borderOther
);
1555 #elif defined(__WXMAC__)
1557 static int extraTop
= -1; // Uninitted
1558 static int other
= 5;
1560 if ( extraTop
== -1 )
1562 // The minimal border used for the top. Later on the staticbox'
1563 // font height is added to this.
1566 if ( UMAGetSystemVersion() >= 0x1030 /*Panther*/ )
1568 // As indicated by the HIG, Panther needs an extra border of 11
1569 // pixels (otherwise overlapping occurs at the top). The "other"
1570 // border has to be 11.
1577 *borderTop
= extraTop
+ box
->GetCharHeight();
1578 *borderOther
= other
;
1582 if ( box
->GetLabel().IsEmpty() )
1586 *borderTop
= box
->GetCharHeight();
1589 #endif // __WXCOCOA__
1592 void wxStaticBoxSizer::RecalcSizes()
1594 int top_border
, other_border
;
1595 GetStaticBoxBorders(m_staticBox
, &top_border
, &other_border
);
1597 m_staticBox
->SetSize( m_position
.x
, m_position
.y
, m_size
.x
, m_size
.y
);
1599 wxPoint
old_pos( m_position
);
1600 m_position
.x
+= other_border
;
1601 m_position
.y
+= top_border
;
1602 wxSize
old_size( m_size
);
1603 m_size
.x
-= 2*other_border
;
1604 m_size
.y
-= top_border
+ other_border
;
1606 wxBoxSizer::RecalcSizes();
1608 m_position
= old_pos
;
1612 wxSize
wxStaticBoxSizer::CalcMin()
1614 int top_border
, other_border
;
1615 GetStaticBoxBorders(m_staticBox
, &top_border
, &other_border
);
1617 wxSize
ret( wxBoxSizer::CalcMin() );
1618 ret
.x
+= 2*other_border
;
1619 ret
.y
+= other_border
+ top_border
;
1624 #endif // wxUSE_STATBOX
1626 // ----------------------------------------------------------------------------
1628 // ----------------------------------------------------------------------------
1632 wxBookCtrlSizer::wxBookCtrlSizer(wxBookCtrl
*bookctrl
)
1633 : m_bookctrl(bookctrl
)
1635 wxASSERT_MSG( bookctrl
, wxT("wxBookCtrlSizer needs a control") );
1638 void wxBookCtrlSizer::RecalcSizes()
1640 m_bookctrl
->SetSize( m_position
.x
, m_position
.y
, m_size
.x
, m_size
.y
);
1643 wxSize
wxBookCtrlSizer::CalcMin()
1645 wxSize sizeBorder
= m_bookctrl
->CalcSizeFromPage(wxSize(0, 0));
1650 if ( m_bookctrl
->GetPageCount() == 0 )
1652 return wxSize(sizeBorder
.x
+ 10, sizeBorder
.y
+ 10);
1658 wxWindowList::compatibility_iterator
1659 node
= m_bookctrl
->GetChildren().GetFirst();
1662 wxWindow
*item
= node
->GetData();
1663 wxSizer
*itemsizer
= item
->GetSizer();
1667 wxSize
subsize( itemsizer
->CalcMin() );
1669 if (subsize
.x
> maxX
)
1671 if (subsize
.y
> maxY
)
1675 node
= node
->GetNext();
1678 return wxSize( maxX
, maxY
) + sizeBorder
;
1684 wxNotebookSizer::wxNotebookSizer(wxNotebook
*nb
)
1685 : wxBookCtrlSizer(nb
)
1689 #endif // wxUSE_NOTEBOOOK
1690 #endif // wxUSE_BOOKCTRL