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>
30 //---------------------------------------------------------------------------
32 IMPLEMENT_CLASS(wxSizerItem
, wxObject
)
33 IMPLEMENT_CLASS(wxSizer
, wxObject
)
34 IMPLEMENT_CLASS(wxGridSizer
, wxSizer
)
35 IMPLEMENT_CLASS(wxFlexGridSizer
, wxGridSizer
)
36 IMPLEMENT_CLASS(wxBoxSizer
, wxSizer
)
38 IMPLEMENT_CLASS(wxStaticBoxSizer
, wxBoxSizer
)
41 IMPLEMENT_CLASS(wxBookCtrlSizer
, wxSizer
)
43 IMPLEMENT_CLASS(wxNotebookSizer
, wxBookCtrlSizer
)
44 #endif // wxUSE_NOTEBOOK
45 #endif // wxUSE_BOOKCTRL
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
& wxADJUST_MINSIZE
) )
194 // By user request, keep the minimal size for this item
195 // in sync with the largest of BestSize and any user supplied
196 // minimum size hint. Useful in cases where the item is
197 // changeable -- static text labels, etc.
198 m_minSize
= m_window
->GetAdjustedBestSize();
208 if (m_flag
& wxNORTH
)
210 if (m_flag
& wxSOUTH
)
216 void wxSizerItem::SetDimension( wxPoint pos
, wxSize size
)
218 if (m_flag
& wxSHAPED
)
220 // adjust aspect ratio
221 int rwidth
= (int) (size
.y
* m_ratio
);
225 int rheight
= (int) (size
.x
/ m_ratio
);
226 // add vertical space
227 if (m_flag
& wxALIGN_CENTER_VERTICAL
)
228 pos
.y
+= (size
.y
- rheight
) / 2;
229 else if (m_flag
& wxALIGN_BOTTOM
)
230 pos
.y
+= (size
.y
- rheight
);
231 // use reduced dimensions
234 else if (rwidth
< size
.x
)
236 // add horizontal space
237 if (m_flag
& wxALIGN_CENTER_HORIZONTAL
)
238 pos
.x
+= (size
.x
- rwidth
) / 2;
239 else if (m_flag
& wxALIGN_RIGHT
)
240 pos
.x
+= (size
.x
- rwidth
);
245 // This is what GetPosition() returns. Since we calculate
246 // borders afterwards, GetPosition() will be the left/top
247 // corner of the surrounding border.
259 if (m_flag
& wxNORTH
)
264 if (m_flag
& wxSOUTH
)
270 m_sizer
->SetDimension( pos
.x
, pos
.y
, size
.x
, size
.y
);
273 m_window
->SetSize( pos
.x
, pos
.y
, size
.x
, size
.y
, wxSIZE_ALLOW_MINUS_ONE
);
278 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 // Return a window size that will fit within the screens dimensions
645 wxSize
wxSizer::FitSize( wxWindow
*window
)
647 wxSize size
= GetMinWindowSize( window
);
648 wxSize sizeMax
= GetMaxWindowSize( window
);
650 // Limit the size if sizeMax != wxDefaultSize
652 if ( size
.x
> sizeMax
.x
&& sizeMax
.x
!= -1 )
654 if ( size
.y
> sizeMax
.y
&& sizeMax
.y
!= -1 )
660 wxSize
wxSizer::GetMaxClientSize( wxWindow
*window
) const
662 wxSize
maxSize( window
->GetMaxSize() );
664 if( maxSize
!= wxDefaultSize
)
666 wxSize
size( window
->GetSize() );
667 wxSize
client_size( window
->GetClientSize() );
669 return wxSize( maxSize
.x
+ client_size
.x
- size
.x
,
670 maxSize
.y
+ client_size
.y
- size
.y
);
673 return wxDefaultSize
;
676 wxSize
wxSizer::GetMinClientSize( wxWindow
*WXUNUSED(window
) )
678 return GetMinSize(); // Already returns client size.
681 wxSize
wxSizer::VirtualFitSize( wxWindow
*window
)
683 wxSize size
= GetMinClientSize( window
);
684 wxSize sizeMax
= GetMaxClientSize( window
);
686 // Limit the size if sizeMax != wxDefaultSize
688 if ( size
.x
> sizeMax
.x
&& sizeMax
.x
!= -1 )
690 if ( size
.y
> sizeMax
.y
&& sizeMax
.y
!= -1 )
696 void wxSizer::SetDimension( int x
, int y
, int width
, int height
)
705 wxSize
wxSizer::GetMinSize()
707 wxSize
ret( CalcMin() );
708 if (ret
.x
< m_minSize
.x
) ret
.x
= m_minSize
.x
;
709 if (ret
.y
< m_minSize
.y
) ret
.y
= m_minSize
.y
;
713 void wxSizer::DoSetMinSize( int width
, int height
)
716 m_minSize
.y
= height
;
719 bool wxSizer::DoSetItemMinSize( wxWindow
*window
, int width
, int height
)
721 wxASSERT_MSG( window
, _T("SetMinSize for NULL window") );
723 // Is it our immediate child?
725 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
728 wxSizerItem
*item
= node
->GetData();
730 if (item
->GetWindow() == window
)
732 item
->SetInitSize( width
, height
);
735 node
= node
->GetNext();
738 // No? Search any subsizers we own then
740 node
= m_children
.GetFirst();
743 wxSizerItem
*item
= node
->GetData();
745 if ( item
->GetSizer() &&
746 item
->GetSizer()->DoSetItemMinSize( window
, width
, height
) )
748 // A child sizer found the requested windw, exit.
751 node
= node
->GetNext();
757 bool wxSizer::DoSetItemMinSize( wxSizer
*sizer
, int width
, int height
)
759 wxASSERT_MSG( sizer
, _T("SetMinSize for NULL sizer") );
761 // Is it our immediate child?
763 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
766 wxSizerItem
*item
= node
->GetData();
768 if (item
->GetSizer() == sizer
)
770 item
->GetSizer()->DoSetMinSize( width
, height
);
773 node
= node
->GetNext();
776 // No? Search any subsizers we own then
778 node
= m_children
.GetFirst();
781 wxSizerItem
*item
= node
->GetData();
783 if ( item
->GetSizer() &&
784 item
->GetSizer()->DoSetItemMinSize( sizer
, width
, height
) )
786 // A child found the requested sizer, exit.
789 node
= node
->GetNext();
795 bool wxSizer::DoSetItemMinSize( size_t index
, int width
, int height
)
797 wxSizerItemList::compatibility_iterator node
= m_children
.Item( index
);
799 wxCHECK_MSG( node
, false, _T("Failed to find child node") );
801 wxSizerItem
*item
= node
->GetData();
803 if (item
->GetSizer())
805 // Sizers contains the minimal size in them, if not calculated ...
806 item
->GetSizer()->DoSetMinSize( width
, height
);
810 // ... but the minimal size of spacers and windows in stored in them
811 item
->SetInitSize( width
, height
);
817 void wxSizer::Show( wxWindow
*window
, bool show
)
819 wxASSERT_MSG( window
, _T("Show for NULL window") );
821 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
824 wxSizerItem
*item
= node
->GetData();
826 if (item
->GetWindow() == window
)
831 node
= node
->GetNext();
835 void wxSizer::Show( wxSizer
*sizer
, bool show
)
837 wxASSERT_MSG( sizer
, _T("Show for NULL sizer") );
839 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
842 wxSizerItem
*item
= node
->GetData();
844 if (item
->GetSizer() == sizer
)
849 node
= node
->GetNext();
853 void wxSizer::Show( size_t index
, bool show
)
855 wxCHECK_RET( index
< m_children
.GetCount(),
856 _T("Show index is out of range") );
858 m_children
.Item( index
)->GetData()->Show( show
);
861 void wxSizer::ShowItems( bool show
)
863 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
866 node
->GetData()->Show( show
);
867 node
= node
->GetNext();
871 bool wxSizer::IsShown( wxWindow
*window
) const
873 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
876 wxSizerItem
*item
= node
->GetData();
878 if (item
->GetWindow() == window
)
880 return item
->IsShown();
882 node
= node
->GetNext();
885 wxFAIL_MSG( _T("IsShown failed to find sizer item") );
890 bool wxSizer::IsShown( wxSizer
*sizer
) const
892 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
895 wxSizerItem
*item
= node
->GetData();
897 if (item
->GetSizer() == sizer
)
899 return item
->IsShown();
901 node
= node
->GetNext();
904 wxFAIL_MSG( _T("IsShown failed to find sizer item") );
909 bool wxSizer::IsShown( size_t index
) const
911 wxCHECK_MSG( index
< m_children
.GetCount(),
913 _T("IsShown index is out of range") );
915 return m_children
.Item( index
)->GetData()->IsShown();
919 //---------------------------------------------------------------------------
921 //---------------------------------------------------------------------------
923 wxGridSizer::wxGridSizer( int rows
, int cols
, int vgap
, int hgap
)
929 if (m_rows
== 0 && m_cols
== 0)
933 wxGridSizer::wxGridSizer( int cols
, int vgap
, int hgap
)
939 if (m_rows
== 0 && m_cols
== 0)
943 int wxGridSizer::CalcRowsCols(int& nrows
, int& ncols
) const
945 int nitems
= m_children
.GetCount();
951 nrows
= (nitems
+ m_cols
- 1) / m_cols
;
955 ncols
= (nitems
+ m_rows
- 1) / m_rows
;
958 else // 0 columns, 0 rows?
960 wxFAIL_MSG( _T("grid sizer must have either rows or columns fixed") );
969 void wxGridSizer::RecalcSizes()
971 int nitems
, nrows
, ncols
;
972 if ( (nitems
= CalcRowsCols(nrows
, ncols
)) == 0 )
975 wxSize
sz( GetSize() );
976 wxPoint
pt( GetPosition() );
978 int w
= (sz
.x
- (ncols
- 1) * m_hgap
) / ncols
;
979 int h
= (sz
.y
- (nrows
- 1) * m_vgap
) / nrows
;
982 for (int c
= 0; c
< ncols
; c
++)
985 for (int r
= 0; r
< nrows
; r
++)
987 int i
= r
* ncols
+ c
;
990 wxSizerItemList::compatibility_iterator node
= m_children
.Item( i
);
992 wxASSERT_MSG( node
, _T("Failed to find SizerItemList node") );
994 SetItemBounds( node
->GetData(), x
, y
, w
, h
);
1002 wxSize
wxGridSizer::CalcMin()
1005 if ( CalcRowsCols(nrows
, ncols
) == 0 )
1006 return wxSize(10, 10);
1008 // Find the max width and height for any component
1012 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
1015 wxSizerItem
*item
= node
->GetData();
1016 wxSize
sz( item
->CalcMin() );
1018 w
= wxMax( w
, sz
.x
);
1019 h
= wxMax( h
, sz
.y
);
1021 node
= node
->GetNext();
1024 return wxSize( ncols
* w
+ (ncols
-1) * m_hgap
,
1025 nrows
* h
+ (nrows
-1) * m_vgap
);
1028 void wxGridSizer::SetItemBounds( wxSizerItem
*item
, int x
, int y
, int w
, int h
)
1031 wxSize
sz( item
->CalcMin() );
1032 int flag
= item
->GetFlag();
1034 if ((flag
& wxEXPAND
) || (flag
& wxSHAPED
))
1040 if (flag
& wxALIGN_CENTER_HORIZONTAL
)
1042 pt
.x
= x
+ (w
- sz
.x
) / 2;
1044 else if (flag
& wxALIGN_RIGHT
)
1046 pt
.x
= x
+ (w
- sz
.x
);
1049 if (flag
& wxALIGN_CENTER_VERTICAL
)
1051 pt
.y
= y
+ (h
- sz
.y
) / 2;
1053 else if (flag
& wxALIGN_BOTTOM
)
1055 pt
.y
= y
+ (h
- sz
.y
);
1059 item
->SetDimension(pt
, sz
);
1062 //---------------------------------------------------------------------------
1064 //---------------------------------------------------------------------------
1066 wxFlexGridSizer::wxFlexGridSizer( int rows
, int cols
, int vgap
, int hgap
)
1067 : wxGridSizer( rows
, cols
, vgap
, hgap
),
1068 m_flexDirection(wxBOTH
),
1069 m_growMode(wxFLEX_GROWMODE_SPECIFIED
)
1073 wxFlexGridSizer::wxFlexGridSizer( int cols
, int vgap
, int hgap
)
1074 : wxGridSizer( cols
, vgap
, hgap
),
1075 m_flexDirection(wxBOTH
),
1076 m_growMode(wxFLEX_GROWMODE_SPECIFIED
)
1080 wxFlexGridSizer::~wxFlexGridSizer()
1084 void wxFlexGridSizer::RecalcSizes()
1086 int nitems
, nrows
, ncols
;
1087 if ( (nitems
= CalcRowsCols(nrows
, ncols
)) == 0 )
1090 wxPoint
pt( GetPosition() );
1091 wxSize
sz( GetSize() );
1092 wxSize
minsz( CalcMin() );
1094 AdjustForGrowables(sz
, minsz
, nrows
, ncols
);
1096 sz
= wxSize( pt
.x
+ sz
.x
, pt
.y
+ sz
.y
);
1099 for (int c
= 0; c
< ncols
; c
++)
1102 for (int r
= 0; r
< nrows
; r
++)
1104 int i
= r
* ncols
+ c
;
1107 wxSizerItemList::compatibility_iterator node
= m_children
.Item( i
);
1109 wxASSERT_MSG( node
, _T("Failed to find node") );
1111 int w
= wxMax( 0, wxMin( m_colWidths
[c
], sz
.x
- x
) );
1112 int h
= wxMax( 0, wxMin( m_rowHeights
[r
], sz
.y
- y
) );
1114 SetItemBounds( node
->GetData(), x
, y
, w
, h
);
1116 y
= y
+ m_rowHeights
[r
] + m_vgap
;
1118 x
= x
+ m_colWidths
[c
] + m_hgap
;
1122 wxSize
wxFlexGridSizer::CalcMin()
1128 // Number of rows/columns can change as items are added or removed.
1129 if ( !CalcRowsCols(nrows
, ncols
) )
1130 return wxSize(10, 10);
1132 m_rowHeights
.SetCount(nrows
);
1133 m_colWidths
.SetCount(ncols
);
1135 // We have to recalcuate the sizes in case an item has wxADJUST_MINSIZE, has changed
1136 // minimum size since the previous layout, or has been hidden using wxSizer::Show().
1137 // If all the items in a row/column are hidden, the final dimension of the row/column
1138 // will be -1, indicating that the column itself is hidden.
1139 for( s
= m_rowHeights
.GetCount(), i
= 0; i
< s
; ++i
)
1140 m_rowHeights
[ i
] = -1;
1141 for( s
= m_colWidths
.GetCount(), i
= 0; i
< s
; ++i
)
1142 m_colWidths
[ i
] = -1;
1144 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
1149 wxSizerItem
*item
= node
->GetData();
1150 if ( item
->IsShown() )
1152 wxSize
sz( item
->CalcMin() );
1153 int row
= i
/ ncols
;
1154 int col
= i
% ncols
;
1156 m_rowHeights
[ row
] = wxMax( wxMax( 0, sz
.y
), m_rowHeights
[ row
] );
1157 m_colWidths
[ col
] = wxMax( wxMax( 0, sz
.x
), m_colWidths
[ col
] );
1160 node
= node
->GetNext();
1164 AdjustForFlexDirection();
1166 // Sum total minimum size, including gaps between rows/columns.
1167 // -1 is used as a magic number meaning empty column.
1169 for (int col
= 0; col
< ncols
; col
++)
1170 if ( m_colWidths
[ col
] != -1 )
1171 width
+= m_colWidths
[ col
] + ( col
== ncols
-1 ? 0 : m_hgap
);
1174 for (int row
= 0; row
< nrows
; row
++)
1175 if ( m_rowHeights
[ row
] != -1 )
1176 height
+= m_rowHeights
[ row
] + ( row
== nrows
-1 ? 0 : m_vgap
);
1178 return wxSize( width
, height
);
1181 void wxFlexGridSizer::AdjustForFlexDirection()
1183 // the logic in CalcMin works when we resize flexibly in both directions
1184 // but maybe this is not the case
1185 if ( m_flexDirection
!= wxBOTH
)
1187 // select the array corresponding to the direction in which we do *not*
1189 wxArrayInt
& array
= m_flexDirection
== wxVERTICAL
? m_colWidths
1192 const int count
= array
.GetCount();
1194 // find the largest value in this array
1196 for ( n
= 0; n
< count
; ++n
)
1198 if ( array
[n
] > largest
)
1202 // and now fill it with the largest value
1203 for ( n
= 0; n
< count
; ++n
)
1211 void wxFlexGridSizer::AdjustForGrowables(const wxSize
& sz
, const wxSize
& minsz
,
1212 int nrows
, int ncols
)
1214 // what to do with the rows? by default, resize them proportionally
1215 if ( sz
.y
> minsz
.y
&& ( (m_flexDirection
& wxVERTICAL
) || (m_growMode
== wxFLEX_GROWMODE_SPECIFIED
) ) )
1217 int sum_proportions
= 0;
1218 int growable_space
= 0;
1221 for (idx
= 0; idx
< m_growableRows
.GetCount(); idx
++)
1223 // Since the number of rows/columns can change as items are
1224 // inserted/deleted, we need to verify at runtime that the
1225 // requested growable rows/columns are still valid.
1226 if (m_growableRows
[idx
] >= nrows
)
1229 // If all items in a row/column are hidden, that row/column will
1230 // have a dimension of -1. This causes the row/column to be
1231 // hidden completely.
1232 if (m_rowHeights
[ m_growableRows
[idx
] ] == -1)
1234 sum_proportions
+= m_growableRowsProportions
[idx
];
1235 growable_space
+= m_rowHeights
[ m_growableRows
[idx
] ];
1241 for (idx
= 0; idx
< m_growableRows
.GetCount(); idx
++)
1243 if (m_growableRows
[idx
] >= nrows
)
1245 if (m_rowHeights
[ m_growableRows
[idx
] ] == -1)
1246 m_rowHeights
[ m_growableRows
[idx
] ] = 0;
1249 int delta
= (sz
.y
- minsz
.y
);
1250 if (sum_proportions
== 0)
1251 delta
= (delta
/num
) + m_rowHeights
[ m_growableRows
[idx
] ];
1253 delta
= ((delta
+growable_space
)*m_growableRowsProportions
[idx
]) / sum_proportions
;
1254 m_rowHeights
[ m_growableRows
[idx
] ] = delta
;
1259 else if ( (m_growMode
== wxFLEX_GROWMODE_ALL
) && (sz
.y
> minsz
.y
) )
1261 // rounding problem?
1262 for ( int row
= 0; row
< nrows
; ++row
)
1263 m_rowHeights
[ row
] = sz
.y
/ nrows
;
1266 // the same logic as above but for the columns
1267 if ( sz
.x
> minsz
.x
&& ( (m_flexDirection
& wxHORIZONTAL
) || (m_growMode
== wxFLEX_GROWMODE_SPECIFIED
) ) )
1269 int sum_proportions
= 0;
1270 int growable_space
= 0;
1273 for (idx
= 0; idx
< m_growableCols
.GetCount(); idx
++)
1275 // Since the number of rows/columns can change as items are
1276 // inserted/deleted, we need to verify at runtime that the
1277 // requested growable rows/columns are still valid.
1278 if (m_growableCols
[idx
] >= ncols
)
1281 // If all items in a row/column are hidden, that row/column will
1282 // have a dimension of -1. This causes the column to be hidden
1284 if (m_colWidths
[ m_growableCols
[idx
] ] == -1)
1286 sum_proportions
+= m_growableColsProportions
[idx
];
1287 growable_space
+= m_colWidths
[ m_growableCols
[idx
] ];
1293 for (idx
= 0; idx
< m_growableCols
.GetCount(); idx
++)
1295 if (m_growableCols
[idx
] >= ncols
)
1297 if (m_colWidths
[ m_growableCols
[idx
] ] == -1)
1298 m_colWidths
[ m_growableCols
[idx
] ] = 0;
1301 int delta
= (sz
.x
- minsz
.x
);
1302 if (sum_proportions
== 0)
1303 delta
= (delta
/num
) + m_colWidths
[ m_growableCols
[idx
] ];
1305 delta
= ((delta
+growable_space
)*m_growableColsProportions
[idx
])/sum_proportions
;
1306 m_colWidths
[ m_growableCols
[idx
] ] = delta
;
1311 else if ( (m_growMode
== wxFLEX_GROWMODE_ALL
) && (sz
.x
> minsz
.x
) )
1313 for ( int col
=0; col
< ncols
; ++col
)
1314 m_colWidths
[ col
] = sz
.x
/ ncols
;
1319 void wxFlexGridSizer::AddGrowableRow( size_t idx
, int proportion
)
1321 m_growableRows
.Add( idx
);
1322 m_growableRowsProportions
.Add( proportion
);
1325 void wxFlexGridSizer::RemoveGrowableRow( size_t idx
)
1327 m_growableRows
.Remove( idx
);
1330 void wxFlexGridSizer::AddGrowableCol( size_t idx
, int proportion
)
1332 m_growableCols
.Add( idx
);
1333 m_growableColsProportions
.Add( proportion
);
1336 void wxFlexGridSizer::RemoveGrowableCol( size_t idx
)
1338 m_growableCols
.Remove( idx
);
1341 //---------------------------------------------------------------------------
1343 //---------------------------------------------------------------------------
1345 wxBoxSizer::wxBoxSizer( int orient
)
1346 : m_orient( orient
)
1350 void wxBoxSizer::RecalcSizes()
1352 if (m_children
.GetCount() == 0)
1358 if (m_orient
== wxHORIZONTAL
)
1359 delta
= m_size
.x
- m_fixedWidth
;
1361 delta
= m_size
.y
- m_fixedHeight
;
1364 wxPoint
pt( m_position
);
1366 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
1369 wxSizerItem
*item
= node
->GetData();
1371 if (item
->IsShown())
1373 wxSize
size( item
->CalcMin() );
1375 if (m_orient
== wxVERTICAL
)
1377 wxCoord height
= size
.y
;
1378 if (item
->GetProportion())
1380 // Because of at least one visible item has non-zero
1381 // proportion then m_stretchable is not zero
1382 height
= (delta
* item
->GetProportion()) / m_stretchable
;
1385 wxPoint
child_pos( pt
);
1386 wxSize
child_size( wxSize( size
.x
, height
) );
1388 if (item
->GetFlag() & (wxEXPAND
| wxSHAPED
))
1389 child_size
.x
= m_size
.x
;
1390 else if (item
->GetFlag() & wxALIGN_RIGHT
)
1391 child_pos
.x
+= m_size
.x
- size
.x
;
1392 else if (item
->GetFlag() & (wxCENTER
| wxALIGN_CENTER_HORIZONTAL
))
1393 // XXX wxCENTER is added for backward compatibility;
1394 // wxALIGN_CENTER should be used in new code
1395 child_pos
.x
+= (m_size
.x
- size
.x
) / 2;
1397 item
->SetDimension( child_pos
, child_size
);
1403 wxCoord width
= size
.x
;
1404 if (item
->GetProportion())
1406 // Because of at least one visible item has non-zero
1407 // proportion then m_stretchable is not zero
1408 width
= (delta
* item
->GetProportion()) / m_stretchable
;
1411 wxPoint
child_pos( pt
);
1412 wxSize
child_size( wxSize(width
, size
.y
) );
1414 if (item
->GetFlag() & (wxEXPAND
| wxSHAPED
))
1415 child_size
.y
= m_size
.y
;
1416 else if (item
->GetFlag() & wxALIGN_BOTTOM
)
1417 child_pos
.y
+= m_size
.y
- size
.y
;
1418 else if (item
->GetFlag() & (wxCENTER
| wxALIGN_CENTER_VERTICAL
))
1419 // XXX wxCENTER is added for backward compatibility;
1420 // wxALIGN_CENTER should be used in new code
1421 child_pos
.y
+= (m_size
.y
- size
.y
) / 2;
1423 item
->SetDimension( child_pos
, child_size
);
1429 node
= node
->GetNext();
1433 wxSize
wxBoxSizer::CalcMin()
1435 if (m_children
.GetCount() == 0)
1436 return wxSize(10,10);
1444 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
1447 wxSizerItem
*item
= node
->GetData();
1449 if (item
->IsShown() && item
->GetProportion() != 0)
1450 m_stretchable
+= item
->GetProportion();
1452 node
= node
->GetNext();
1455 // Total minimum size (width or height) of sizer
1458 node
= m_children
.GetFirst();
1461 wxSizerItem
*item
= node
->GetData();
1463 if (item
->IsShown() && item
->GetProportion() != 0)
1465 int stretch
= item
->GetProportion();
1466 wxSize
size( item
->CalcMin() );
1469 // Integer division rounded up is (a + b - 1) / b
1470 // Round up needed in order to guarantee that all
1471 // all items will have size not less then their min size
1472 if (m_orient
== wxHORIZONTAL
)
1473 minSize
= ( size
.x
*m_stretchable
+ stretch
- 1)/stretch
;
1475 minSize
= ( size
.y
*m_stretchable
+ stretch
- 1)/stretch
;
1477 if (minSize
> maxMinSize
)
1478 maxMinSize
= minSize
;
1480 node
= node
->GetNext();
1483 // Calculate overall minimum size
1484 node
= m_children
.GetFirst();
1487 wxSizerItem
*item
= node
->GetData();
1489 if (item
->IsShown())
1491 wxSize
size( item
->CalcMin() );
1492 if (item
->GetProportion() != 0)
1494 if (m_orient
== wxHORIZONTAL
)
1495 size
.x
= (maxMinSize
*item
->GetProportion())/m_stretchable
;
1497 size
.y
= (maxMinSize
*item
->GetProportion())/m_stretchable
;
1501 if (m_orient
== wxVERTICAL
)
1503 m_fixedHeight
+= size
.y
;
1504 m_fixedWidth
= wxMax( m_fixedWidth
, size
.x
);
1508 m_fixedWidth
+= size
.x
;
1509 m_fixedHeight
= wxMax( m_fixedHeight
, size
.y
);
1513 if (m_orient
== wxHORIZONTAL
)
1515 m_minWidth
+= size
.x
;
1516 m_minHeight
= wxMax( m_minHeight
, size
.y
);
1520 m_minHeight
+= size
.y
;
1521 m_minWidth
= wxMax( m_minWidth
, size
.x
);
1524 node
= node
->GetNext();
1527 return wxSize( m_minWidth
, m_minHeight
);
1530 //---------------------------------------------------------------------------
1532 //---------------------------------------------------------------------------
1536 wxStaticBoxSizer::wxStaticBoxSizer( wxStaticBox
*box
, int orient
)
1537 : wxBoxSizer( orient
)
1538 , m_staticBox( box
)
1540 wxASSERT_MSG( box
, wxT("wxStaticBoxSizer needs a static box") );
1543 static void GetStaticBoxBorders( wxStaticBox
*box
,
1547 // this has to be done platform by platform as there is no way to
1548 // guess the thickness of a wxStaticBox border
1550 box
->GetBordersForSizer(borderTop
,borderOther
);
1551 #elif defined(__WXMAC__)
1553 static int extraTop
= -1; // Uninitted
1554 static int other
= 5;
1556 if ( extraTop
== -1 )
1559 (void) ::wxGetOsVersion(&verMaj
, &verMin
);
1561 // The minimal border used for the top. Later on the staticbox'
1562 // font height is added to this.
1565 // Is the Mac OS version OS X Panther or higher?
1566 if ( ((verMaj
<< 16) + verMin
) >= 0x00100030 )
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