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/listimpl.cpp"
28 #if WXWIN_COMPATIBILITY_2_4
29 #include "wx/notebook.h"
33 # include "wx/mac/uma.h"
36 //---------------------------------------------------------------------------
38 IMPLEMENT_CLASS(wxSizerItem
, wxObject
)
39 IMPLEMENT_CLASS(wxSizer
, wxObject
)
40 IMPLEMENT_CLASS(wxGridSizer
, wxSizer
)
41 IMPLEMENT_CLASS(wxFlexGridSizer
, wxGridSizer
)
42 IMPLEMENT_CLASS(wxBoxSizer
, wxSizer
)
44 IMPLEMENT_CLASS(wxStaticBoxSizer
, wxBoxSizer
)
47 WX_DEFINE_EXPORTED_LIST( wxSizerItemList
);
81 //---------------------------------------------------------------------------
83 //---------------------------------------------------------------------------
85 wxSizerItem::wxSizerItem( int width
, int height
, int proportion
, int flag
, int border
, wxObject
* userData
)
88 , m_size( wxSize( width
, height
) ) // size is set directly
89 , m_minSize( m_size
) // minimal size is the initial size
90 , m_proportion( proportion
)
94 , m_userData( userData
)
99 wxSizerItem::wxSizerItem( wxWindow
*window
, int proportion
, int flag
, int border
, wxObject
* userData
)
102 , m_minSize( window
->GetSize() ) // minimal size is the initial size
103 , m_proportion( proportion
)
107 , m_userData( userData
)
109 // aspect ratio calculated from initial size
110 SetRatio( m_minSize
);
112 // m_size is calculated later
115 wxSizerItem::wxSizerItem( wxSizer
*sizer
, int proportion
, int flag
, int border
, wxObject
* userData
)
118 , m_proportion( proportion
)
123 , m_userData( userData
)
125 // m_minSize is calculated later
126 // m_size is calculated later
129 wxSizerItem::wxSizerItem()
141 wxSizerItem::~wxSizerItem()
147 m_window
->SetContainingSizer(NULL
);
149 else // we must be a sizer
156 wxSize
wxSizerItem::GetSize() const
160 ret
= m_sizer
->GetSize();
163 ret
= m_window
->GetSize();
170 if (m_flag
& wxNORTH
)
172 if (m_flag
& wxSOUTH
)
178 wxSize
wxSizerItem::CalcMin()
183 ret
= m_sizer
->GetMinSize();
185 // if we have to preserve aspect ratio _AND_ this is
186 // the first-time calculation, consider ret to be initial size
187 if ((m_flag
& wxSHAPED
) && !m_ratio
)
192 if ( IsWindow() && !(m_flag
& wxFIXED_MINSIZE
) )
194 // Since the size of the window may change during runtime, we
195 // should use the current minimal/best size.
196 m_minSize
= m_window
->GetBestFittingSize();
205 if (m_flag
& wxNORTH
)
207 if (m_flag
& wxSOUTH
)
213 void wxSizerItem::SetDimension( wxPoint pos
, wxSize size
)
215 if (m_flag
& wxSHAPED
)
217 // adjust aspect ratio
218 int rwidth
= (int) (size
.y
* m_ratio
);
222 int rheight
= (int) (size
.x
/ m_ratio
);
223 // add vertical space
224 if (m_flag
& wxALIGN_CENTER_VERTICAL
)
225 pos
.y
+= (size
.y
- rheight
) / 2;
226 else if (m_flag
& wxALIGN_BOTTOM
)
227 pos
.y
+= (size
.y
- rheight
);
228 // use reduced dimensions
231 else if (rwidth
< size
.x
)
233 // add horizontal space
234 if (m_flag
& wxALIGN_CENTER_HORIZONTAL
)
235 pos
.x
+= (size
.x
- rwidth
) / 2;
236 else if (m_flag
& wxALIGN_RIGHT
)
237 pos
.x
+= (size
.x
- rwidth
);
242 // This is what GetPosition() returns. Since we calculate
243 // borders afterwards, GetPosition() will be the left/top
244 // corner of the surrounding border.
256 if (m_flag
& wxNORTH
)
261 if (m_flag
& wxSOUTH
)
267 m_sizer
->SetDimension( pos
.x
, pos
.y
, size
.x
, size
.y
);
270 m_window
->SetSize( pos
.x
, pos
.y
, size
.x
, size
.y
, wxSIZE_ALLOW_MINUS_ONE
);
275 void wxSizerItem::DeleteWindows()
284 m_sizer
->DeleteWindows();
287 bool wxSizerItem::IsWindow() const
289 return (m_window
!= NULL
);
292 bool wxSizerItem::IsSizer() const
294 return (m_sizer
!= NULL
);
297 bool wxSizerItem::IsSpacer() const
299 return (m_window
== NULL
) && (m_sizer
== NULL
);
302 void wxSizerItem::Show( bool show
)
307 m_window
->Show( show
);
309 m_sizer
->ShowItems( show
);
311 // ... nothing else to do to hide/show spacers
314 void wxSizerItem::SetOption( int option
)
316 SetProportion( option
);
319 int wxSizerItem::GetOption() const
321 return GetProportion();
325 //---------------------------------------------------------------------------
327 //---------------------------------------------------------------------------
330 : m_minSize( wxSize( 0, 0 ) )
336 WX_CLEAR_LIST(wxSizerItemList
, m_children
);
339 void wxSizer::Add( wxWindow
*window
, int proportion
, int flag
, int border
, wxObject
* userData
)
341 m_children
.Append( new wxSizerItem( window
, proportion
, flag
, border
, userData
) );
342 window
->SetContainingSizer( this );
345 void wxSizer::Add( wxSizer
*sizer
, int proportion
, int flag
, int border
, wxObject
* userData
)
347 m_children
.Append( new wxSizerItem( sizer
, proportion
, flag
, border
, userData
) );
350 void wxSizer::Add( int width
, int height
, int proportion
, int flag
, int border
, wxObject
* userData
)
352 m_children
.Append( new wxSizerItem( width
, height
, proportion
, flag
, border
, userData
) );
355 void wxSizer::Add( wxSizerItem
*item
)
357 m_children
.Append( item
);
359 if( item
->GetWindow() )
360 item
->GetWindow()->SetContainingSizer( this );
363 void wxSizer::Prepend( wxWindow
*window
, int proportion
, int flag
, int border
, wxObject
* userData
)
365 m_children
.Insert( new wxSizerItem( window
, proportion
, flag
, border
, userData
) );
366 window
->SetContainingSizer( this );
369 void wxSizer::Prepend( wxSizer
*sizer
, int proportion
, int flag
, int border
, wxObject
* userData
)
371 m_children
.Insert( new wxSizerItem( sizer
, proportion
, flag
, border
, userData
) );
374 void wxSizer::Prepend( int width
, int height
, int proportion
, int flag
, int border
, wxObject
* userData
)
376 m_children
.Insert( new wxSizerItem( width
, height
, proportion
, flag
, border
, userData
) );
379 void wxSizer::Prepend( wxSizerItem
*item
)
381 m_children
.Insert( item
);
383 if( item
->GetWindow() )
384 item
->GetWindow()->SetContainingSizer( this );
387 void wxSizer::Insert( size_t index
,
394 m_children
.Insert( index
,
395 new wxSizerItem( window
, proportion
, flag
, border
, userData
) );
396 window
->SetContainingSizer( this );
399 void wxSizer::Insert( size_t index
,
406 m_children
.Insert( index
,
407 new wxSizerItem( sizer
, proportion
, flag
, border
, userData
) );
410 void wxSizer::Insert( size_t index
,
418 m_children
.Insert( index
,
419 new wxSizerItem( width
, height
, proportion
, flag
, border
, userData
) );
422 void wxSizer::Insert( size_t index
, wxSizerItem
*item
)
424 m_children
.Insert( index
, item
);
426 if( item
->GetWindow() )
427 item
->GetWindow()->SetContainingSizer( this );
430 bool wxSizer::Remove( wxWindow
*window
)
432 return Detach( window
);
435 bool wxSizer::Remove( wxSizer
*sizer
)
437 wxASSERT_MSG( sizer
, _T("Removing NULL sizer") );
439 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
442 wxSizerItem
*item
= node
->GetData();
444 if (item
->GetSizer() == sizer
)
447 m_children
.Erase( node
);
451 node
= node
->GetNext();
457 bool wxSizer::Remove( int index
)
459 wxCHECK_MSG( index
>= 0 && (size_t)index
< m_children
.GetCount(),
461 _T("Remove index is out of range") );
463 wxSizerItemList::compatibility_iterator node
= m_children
.Item( index
);
465 wxCHECK_MSG( node
, false, _T("Failed to find child node") );
467 wxSizerItem
*item
= node
->GetData();
469 if( item
->IsWindow() )
470 item
->GetWindow()->SetContainingSizer( NULL
);
473 m_children
.Erase( node
);
477 bool wxSizer::Detach( wxSizer
*sizer
)
479 wxASSERT_MSG( sizer
, _T("Detaching NULL sizer") );
481 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
484 wxSizerItem
*item
= node
->GetData();
486 if (item
->GetSizer() == sizer
)
490 m_children
.Erase( node
);
493 node
= node
->GetNext();
499 bool wxSizer::Detach( wxWindow
*window
)
501 wxASSERT_MSG( window
, _T("Detaching NULL window") );
503 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
506 wxSizerItem
*item
= node
->GetData();
508 if (item
->GetWindow() == window
)
510 item
->GetWindow()->SetContainingSizer( NULL
);
512 m_children
.Erase( node
);
515 node
= node
->GetNext();
521 bool wxSizer::Detach( int index
)
523 wxCHECK_MSG( index
>= 0 && (size_t)index
< m_children
.GetCount(),
525 _T("Detach index is out of range") );
527 wxSizerItemList::compatibility_iterator node
= m_children
.Item( index
);
529 wxCHECK_MSG( node
, false, _T("Failed to find child node") );
531 wxSizerItem
*item
= node
->GetData();
533 if( item
->IsSizer() )
535 else if( item
->IsWindow() )
536 item
->GetWindow()->SetContainingSizer( NULL
);
539 m_children
.Erase( node
);
543 void wxSizer::Clear( bool delete_windows
)
545 // First clear the ContainingSizer pointers
546 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
549 wxSizerItem
*item
= node
->GetData();
551 if (item
->IsWindow())
552 item
->GetWindow()->SetContainingSizer( NULL
);
553 node
= node
->GetNext();
556 // Destroy the windows if needed
560 // Now empty the list
561 WX_CLEAR_LIST(wxSizerItemList
, m_children
);
564 void wxSizer::DeleteWindows()
566 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
569 wxSizerItem
*item
= node
->GetData();
571 item
->DeleteWindows();
572 node
= node
->GetNext();
576 wxSize
wxSizer::Fit( wxWindow
*window
)
578 wxSize
size(window
->IsTopLevel() ? FitSize(window
)
579 : GetMinWindowSize(window
));
581 window
->SetSize( size
);
586 void wxSizer::FitInside( wxWindow
*window
)
589 if (window
->IsTopLevel())
590 size
= VirtualFitSize( window
);
592 size
= GetMinClientSize( window
);
594 window
->SetVirtualSize( size
);
597 void wxSizer::Layout()
603 void wxSizer::SetSizeHints( wxWindow
*window
)
605 // Preserve the window's max size hints, but set the
606 // lower bound according to the sizer calculations.
608 wxSize size
= Fit( window
);
610 window
->SetSizeHints( size
.x
,
612 window
->GetMaxWidth(),
613 window
->GetMaxHeight() );
616 void wxSizer::SetVirtualSizeHints( wxWindow
*window
)
618 // Preserve the window's max size hints, but set the
619 // lower bound according to the sizer calculations.
622 wxSize
size( window
->GetVirtualSize() );
623 window
->SetVirtualSizeHints( size
.x
,
625 window
->GetMaxWidth(),
626 window
->GetMaxHeight() );
629 wxSize
wxSizer::GetMaxWindowSize( wxWindow
*window
) const
631 return window
->GetMaxSize();
634 wxSize
wxSizer::GetMinWindowSize( wxWindow
*window
)
636 wxSize
minSize( GetMinSize() );
637 wxSize
size( window
->GetSize() );
638 wxSize
client_size( window
->GetClientSize() );
640 return wxSize( minSize
.x
+size
.x
-client_size
.x
,
641 minSize
.y
+size
.y
-client_size
.y
);
644 // TODO on mac we need a function that determines how much free space this
645 // min size contains, in order to make sure that we have 20 pixels of free
646 // space around the controls
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
->SetMinSize( 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
->SetMinSize( 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
- m_hgap
) / 2;
1048 else if (flag
& wxALIGN_RIGHT
)
1050 pt
.x
= x
+ (w
- sz
.x
- m_hgap
);
1053 if (flag
& wxALIGN_CENTER_VERTICAL
)
1055 pt
.y
= y
+ (h
- sz
.y
- m_vgap
) / 2;
1057 else if (flag
& wxALIGN_BOTTOM
)
1059 pt
.y
= y
+ (h
- sz
.y
- m_vgap
);
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 the item minimum size has
1140 // changed since the previous layout, or the item has been hidden using
1141 // wxSizer::Show(). If all the items in a row/column are hidden, the final
1142 // dimension of the row/column will be -1, indicating that the column
1143 // itself is hidden.
1144 for( s
= m_rowHeights
.GetCount(), i
= 0; i
< s
; ++i
)
1145 m_rowHeights
[ i
] = -1;
1146 for( s
= m_colWidths
.GetCount(), i
= 0; i
< s
; ++i
)
1147 m_colWidths
[ i
] = -1;
1149 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
1154 wxSizerItem
*item
= node
->GetData();
1155 if ( item
->IsShown() )
1157 wxSize
sz( item
->CalcMin() );
1158 int row
= i
/ ncols
;
1159 int col
= i
% ncols
;
1161 m_rowHeights
[ row
] = wxMax( wxMax( 0, sz
.y
), m_rowHeights
[ row
] );
1162 m_colWidths
[ col
] = wxMax( wxMax( 0, sz
.x
), m_colWidths
[ col
] );
1165 node
= node
->GetNext();
1169 AdjustForFlexDirection();
1171 // Sum total minimum size, including gaps between rows/columns.
1172 // -1 is used as a magic number meaning empty column.
1174 for (int col
= 0; col
< ncols
; col
++)
1175 if ( m_colWidths
[ col
] != -1 )
1176 width
+= m_colWidths
[ col
] + ( col
== ncols
-1 ? 0 : m_hgap
);
1179 for (int row
= 0; row
< nrows
; row
++)
1180 if ( m_rowHeights
[ row
] != -1 )
1181 height
+= m_rowHeights
[ row
] + ( row
== nrows
-1 ? 0 : m_vgap
);
1183 return wxSize( width
, height
);
1186 void wxFlexGridSizer::AdjustForFlexDirection()
1188 // the logic in CalcMin works when we resize flexibly in both directions
1189 // but maybe this is not the case
1190 if ( m_flexDirection
!= wxBOTH
)
1192 // select the array corresponding to the direction in which we do *not*
1194 wxArrayInt
& array
= m_flexDirection
== wxVERTICAL
? m_colWidths
1197 const int count
= array
.GetCount();
1199 // find the largest value in this array
1201 for ( n
= 0; n
< count
; ++n
)
1203 if ( array
[n
] > largest
)
1207 // and now fill it with the largest value
1208 for ( n
= 0; n
< count
; ++n
)
1216 void wxFlexGridSizer::AdjustForGrowables(const wxSize
& sz
, const wxSize
& minsz
,
1217 int nrows
, int ncols
)
1219 // what to do with the rows? by default, resize them proportionally
1220 if ( sz
.y
> minsz
.y
&& ( (m_flexDirection
& wxVERTICAL
) || (m_growMode
== wxFLEX_GROWMODE_SPECIFIED
) ) )
1222 int sum_proportions
= 0;
1223 int growable_space
= 0;
1226 for (idx
= 0; idx
< m_growableRows
.GetCount(); idx
++)
1228 // Since the number of rows/columns can change as items are
1229 // inserted/deleted, we need to verify at runtime that the
1230 // requested growable rows/columns are still valid.
1231 if (m_growableRows
[idx
] >= nrows
)
1234 // If all items in a row/column are hidden, that row/column will
1235 // have a dimension of -1. This causes the row/column to be
1236 // hidden completely.
1237 if (m_rowHeights
[ m_growableRows
[idx
] ] == -1)
1239 sum_proportions
+= m_growableRowsProportions
[idx
];
1240 growable_space
+= m_rowHeights
[ m_growableRows
[idx
] ];
1246 for (idx
= 0; idx
< m_growableRows
.GetCount(); idx
++)
1248 if (m_growableRows
[idx
] >= nrows
)
1250 if (m_rowHeights
[ m_growableRows
[idx
] ] == -1)
1251 m_rowHeights
[ m_growableRows
[idx
] ] = 0;
1254 int delta
= (sz
.y
- minsz
.y
);
1255 if (sum_proportions
== 0)
1256 delta
= (delta
/num
) + m_rowHeights
[ m_growableRows
[idx
] ];
1258 delta
= ((delta
+growable_space
)*m_growableRowsProportions
[idx
]) / sum_proportions
;
1259 m_rowHeights
[ m_growableRows
[idx
] ] = delta
;
1264 else if ( (m_growMode
== wxFLEX_GROWMODE_ALL
) && (sz
.y
> minsz
.y
) )
1266 // rounding problem?
1267 for ( int row
= 0; row
< nrows
; ++row
)
1268 m_rowHeights
[ row
] = sz
.y
/ nrows
;
1271 // the same logic as above but for the columns
1272 if ( sz
.x
> minsz
.x
&& ( (m_flexDirection
& wxHORIZONTAL
) || (m_growMode
== wxFLEX_GROWMODE_SPECIFIED
) ) )
1274 int sum_proportions
= 0;
1275 int growable_space
= 0;
1278 for (idx
= 0; idx
< m_growableCols
.GetCount(); idx
++)
1280 // Since the number of rows/columns can change as items are
1281 // inserted/deleted, we need to verify at runtime that the
1282 // requested growable rows/columns are still valid.
1283 if (m_growableCols
[idx
] >= ncols
)
1286 // If all items in a row/column are hidden, that row/column will
1287 // have a dimension of -1. This causes the column to be hidden
1289 if (m_colWidths
[ m_growableCols
[idx
] ] == -1)
1291 sum_proportions
+= m_growableColsProportions
[idx
];
1292 growable_space
+= m_colWidths
[ m_growableCols
[idx
] ];
1298 for (idx
= 0; idx
< m_growableCols
.GetCount(); idx
++)
1300 if (m_growableCols
[idx
] >= ncols
)
1302 if (m_colWidths
[ m_growableCols
[idx
] ] == -1)
1303 m_colWidths
[ m_growableCols
[idx
] ] = 0;
1306 int delta
= (sz
.x
- minsz
.x
);
1307 if (sum_proportions
== 0)
1308 delta
= (delta
/num
) + m_colWidths
[ m_growableCols
[idx
] ];
1310 delta
= ((delta
+growable_space
)*m_growableColsProportions
[idx
])/sum_proportions
;
1311 m_colWidths
[ m_growableCols
[idx
] ] = delta
;
1316 else if ( (m_growMode
== wxFLEX_GROWMODE_ALL
) && (sz
.x
> minsz
.x
) )
1318 for ( int col
=0; col
< ncols
; ++col
)
1319 m_colWidths
[ col
] = sz
.x
/ ncols
;
1324 void wxFlexGridSizer::AddGrowableRow( size_t idx
, int proportion
)
1326 m_growableRows
.Add( idx
);
1327 m_growableRowsProportions
.Add( proportion
);
1330 void wxFlexGridSizer::RemoveGrowableRow( size_t idx
)
1332 m_growableRows
.Remove( idx
);
1335 void wxFlexGridSizer::AddGrowableCol( size_t idx
, int proportion
)
1337 m_growableCols
.Add( idx
);
1338 m_growableColsProportions
.Add( proportion
);
1341 void wxFlexGridSizer::RemoveGrowableCol( size_t idx
)
1343 m_growableCols
.Remove( idx
);
1346 //---------------------------------------------------------------------------
1348 //---------------------------------------------------------------------------
1350 wxBoxSizer::wxBoxSizer( int orient
)
1351 : m_orient( orient
)
1355 void wxBoxSizer::RecalcSizes()
1357 if (m_children
.GetCount() == 0)
1363 if (m_orient
== wxHORIZONTAL
)
1364 delta
= m_size
.x
- m_fixedWidth
;
1366 delta
= m_size
.y
- m_fixedHeight
;
1369 wxPoint
pt( m_position
);
1371 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
1374 wxSizerItem
*item
= node
->GetData();
1376 if (item
->IsShown())
1378 wxSize
size( item
->CalcMin() );
1380 if (m_orient
== wxVERTICAL
)
1382 wxCoord height
= size
.y
;
1383 if (item
->GetProportion())
1385 // Because of at least one visible item has non-zero
1386 // proportion then m_stretchable is not zero
1387 height
= (delta
* item
->GetProportion()) / m_stretchable
;
1390 wxPoint
child_pos( pt
);
1391 wxSize
child_size( wxSize( size
.x
, height
) );
1393 if (item
->GetFlag() & (wxEXPAND
| wxSHAPED
))
1394 child_size
.x
= m_size
.x
;
1395 else if (item
->GetFlag() & wxALIGN_RIGHT
)
1396 child_pos
.x
+= m_size
.x
- size
.x
;
1397 else if (item
->GetFlag() & (wxCENTER
| wxALIGN_CENTER_HORIZONTAL
))
1398 // XXX wxCENTER is added for backward compatibility;
1399 // wxALIGN_CENTER should be used in new code
1400 child_pos
.x
+= (m_size
.x
- size
.x
) / 2;
1402 item
->SetDimension( child_pos
, child_size
);
1408 wxCoord width
= size
.x
;
1409 if (item
->GetProportion())
1411 // Because of at least one visible item has non-zero
1412 // proportion then m_stretchable is not zero
1413 width
= (delta
* item
->GetProportion()) / m_stretchable
;
1416 wxPoint
child_pos( pt
);
1417 wxSize
child_size( wxSize(width
, size
.y
) );
1419 if (item
->GetFlag() & (wxEXPAND
| wxSHAPED
))
1420 child_size
.y
= m_size
.y
;
1421 else if (item
->GetFlag() & wxALIGN_BOTTOM
)
1422 child_pos
.y
+= m_size
.y
- size
.y
;
1423 else if (item
->GetFlag() & (wxCENTER
| wxALIGN_CENTER_VERTICAL
))
1424 // XXX wxCENTER is added for backward compatibility;
1425 // wxALIGN_CENTER should be used in new code
1426 child_pos
.y
+= (m_size
.y
- size
.y
) / 2;
1428 item
->SetDimension( child_pos
, child_size
);
1434 node
= node
->GetNext();
1438 wxSize
wxBoxSizer::CalcMin()
1440 if (m_children
.GetCount() == 0)
1441 return wxSize(10,10);
1449 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
1452 wxSizerItem
*item
= node
->GetData();
1454 if (item
->IsShown() && item
->GetProportion() != 0)
1455 m_stretchable
+= item
->GetProportion();
1457 node
= node
->GetNext();
1460 // Total minimum size (width or height) of sizer
1463 node
= m_children
.GetFirst();
1466 wxSizerItem
*item
= node
->GetData();
1468 if (item
->IsShown() && item
->GetProportion() != 0)
1470 int stretch
= item
->GetProportion();
1471 wxSize
size( item
->CalcMin() );
1474 // Integer division rounded up is (a + b - 1) / b
1475 // Round up needed in order to guarantee that all
1476 // all items will have size not less then their min size
1477 if (m_orient
== wxHORIZONTAL
)
1478 minSize
= ( size
.x
*m_stretchable
+ stretch
- 1)/stretch
;
1480 minSize
= ( size
.y
*m_stretchable
+ stretch
- 1)/stretch
;
1482 if (minSize
> maxMinSize
)
1483 maxMinSize
= minSize
;
1485 node
= node
->GetNext();
1488 // Calculate overall minimum size
1489 node
= m_children
.GetFirst();
1492 wxSizerItem
*item
= node
->GetData();
1494 if (item
->IsShown())
1496 wxSize
size( item
->CalcMin() );
1497 if (item
->GetProportion() != 0)
1499 if (m_orient
== wxHORIZONTAL
)
1500 size
.x
= (maxMinSize
*item
->GetProportion())/m_stretchable
;
1502 size
.y
= (maxMinSize
*item
->GetProportion())/m_stretchable
;
1506 if (m_orient
== wxVERTICAL
)
1508 m_fixedHeight
+= size
.y
;
1509 m_fixedWidth
= wxMax( m_fixedWidth
, size
.x
);
1513 m_fixedWidth
+= size
.x
;
1514 m_fixedHeight
= wxMax( m_fixedHeight
, size
.y
);
1518 if (m_orient
== wxHORIZONTAL
)
1520 m_minWidth
+= size
.x
;
1521 m_minHeight
= wxMax( m_minHeight
, size
.y
);
1525 m_minHeight
+= size
.y
;
1526 m_minWidth
= wxMax( m_minWidth
, size
.x
);
1529 node
= node
->GetNext();
1532 return wxSize( m_minWidth
, m_minHeight
);
1535 //---------------------------------------------------------------------------
1537 //---------------------------------------------------------------------------
1541 wxStaticBoxSizer::wxStaticBoxSizer( wxStaticBox
*box
, int orient
)
1542 : wxBoxSizer( orient
)
1543 , m_staticBox( box
)
1545 wxASSERT_MSG( box
, wxT("wxStaticBoxSizer needs a static box") );
1548 static void GetStaticBoxBorders( wxStaticBox
*box
,
1552 // this has to be done platform by platform as there is no way to
1553 // guess the thickness of a wxStaticBox border
1555 box
->GetBordersForSizer(borderTop
,borderOther
);
1556 #elif defined(__WXMAC__)
1558 static int extraTop
= -1; // Uninitted
1559 static int other
= 5;
1561 if ( extraTop
== -1 )
1563 // The minimal border used for the top. Later on the staticbox'
1564 // font height is added to this.
1567 if ( UMAGetSystemVersion() >= 0x1030 /*Panther*/ )
1569 // As indicated by the HIG, Panther needs an extra border of 11
1570 // pixels (otherwise overlapping occurs at the top). The "other"
1571 // border has to be 11.
1578 *borderTop
= extraTop
+ box
->GetCharHeight();
1579 *borderOther
= other
;
1583 if ( box
->GetLabel().IsEmpty() )
1587 *borderTop
= box
->GetCharHeight();
1590 #endif // __WXCOCOA__
1593 void wxStaticBoxSizer::RecalcSizes()
1595 int top_border
, other_border
;
1596 GetStaticBoxBorders(m_staticBox
, &top_border
, &other_border
);
1598 m_staticBox
->SetSize( m_position
.x
, m_position
.y
, m_size
.x
, m_size
.y
);
1600 wxPoint
old_pos( m_position
);
1601 m_position
.x
+= other_border
;
1602 m_position
.y
+= top_border
;
1603 wxSize
old_size( m_size
);
1604 m_size
.x
-= 2*other_border
;
1605 m_size
.y
-= top_border
+ other_border
;
1607 wxBoxSizer::RecalcSizes();
1609 m_position
= old_pos
;
1613 wxSize
wxStaticBoxSizer::CalcMin()
1615 int top_border
, other_border
;
1616 GetStaticBoxBorders(m_staticBox
, &top_border
, &other_border
);
1618 wxSize
ret( wxBoxSizer::CalcMin() );
1619 ret
.x
+= 2*other_border
;
1620 ret
.y
+= other_border
+ top_border
;
1625 void wxStaticBoxSizer::ShowItems( bool show
)
1627 m_staticBox
->Show( show
);
1628 wxBoxSizer::ShowItems( show
);
1631 #endif // wxUSE_STATBOX
1634 #if WXWIN_COMPATIBILITY_2_4
1636 // ----------------------------------------------------------------------------
1638 // ----------------------------------------------------------------------------
1641 IMPLEMENT_CLASS(wxBookCtrlSizer
, wxSizer
)
1643 IMPLEMENT_CLASS(wxNotebookSizer
, wxBookCtrlSizer
)
1644 #endif // wxUSE_NOTEBOOK
1645 #endif // wxUSE_BOOKCTRL
1649 wxBookCtrlSizer::wxBookCtrlSizer(wxBookCtrl
*bookctrl
)
1650 : m_bookctrl(bookctrl
)
1652 wxASSERT_MSG( bookctrl
, wxT("wxBookCtrlSizer needs a control") );
1655 void wxBookCtrlSizer::RecalcSizes()
1657 m_bookctrl
->SetSize( m_position
.x
, m_position
.y
, m_size
.x
, m_size
.y
);
1660 wxSize
wxBookCtrlSizer::CalcMin()
1662 wxSize sizeBorder
= m_bookctrl
->CalcSizeFromPage(wxSize(0, 0));
1667 if ( m_bookctrl
->GetPageCount() == 0 )
1669 return wxSize(sizeBorder
.x
+ 10, sizeBorder
.y
+ 10);
1675 wxWindowList::compatibility_iterator
1676 node
= m_bookctrl
->GetChildren().GetFirst();
1679 wxWindow
*item
= node
->GetData();
1680 wxSizer
*itemsizer
= item
->GetSizer();
1684 wxSize
subsize( itemsizer
->CalcMin() );
1686 if (subsize
.x
> maxX
)
1688 if (subsize
.y
> maxY
)
1692 node
= node
->GetNext();
1695 return wxSize( maxX
, maxY
) + sizeBorder
;
1700 wxNotebookSizer::wxNotebookSizer(wxNotebook
*nb
)
1702 wxASSERT_MSG( nb
, wxT("wxNotebookSizer needs a control") );
1706 #endif // wxUSE_NOTEBOOOK
1707 #endif // wxUSE_BOOKCTRL
1709 #endif // WXWIN_COMPATIBILITY_2_4