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
& wxFIXED_MINSIZE
) )
198 // Since the size of the window may change during runtime, we
199 // should use the current minimal size. If there is a MinSize,
200 // use it, otherwise use the BestSize.
201 wxSize min
= m_window
->GetMinSize();
202 wxSize best
= m_window
->GetBestSize();
203 if (min
.x
== -1) min
.x
= best
.x
;
204 if (min
.y
== -1) min
.y
= best
.y
;
215 if (m_flag
& wxNORTH
)
217 if (m_flag
& wxSOUTH
)
223 void wxSizerItem::SetDimension( wxPoint pos
, wxSize size
)
225 if (m_flag
& wxSHAPED
)
227 // adjust aspect ratio
228 int rwidth
= (int) (size
.y
* m_ratio
);
232 int rheight
= (int) (size
.x
/ m_ratio
);
233 // add vertical space
234 if (m_flag
& wxALIGN_CENTER_VERTICAL
)
235 pos
.y
+= (size
.y
- rheight
) / 2;
236 else if (m_flag
& wxALIGN_BOTTOM
)
237 pos
.y
+= (size
.y
- rheight
);
238 // use reduced dimensions
241 else if (rwidth
< size
.x
)
243 // add horizontal space
244 if (m_flag
& wxALIGN_CENTER_HORIZONTAL
)
245 pos
.x
+= (size
.x
- rwidth
) / 2;
246 else if (m_flag
& wxALIGN_RIGHT
)
247 pos
.x
+= (size
.x
- rwidth
);
252 // This is what GetPosition() returns. Since we calculate
253 // borders afterwards, GetPosition() will be the left/top
254 // corner of the surrounding border.
266 if (m_flag
& wxNORTH
)
271 if (m_flag
& wxSOUTH
)
277 m_sizer
->SetDimension( pos
.x
, pos
.y
, size
.x
, size
.y
);
280 m_window
->SetSize( pos
.x
, pos
.y
, size
.x
, size
.y
, wxSIZE_ALLOW_MINUS_ONE
);
285 void wxSizerItem::DeleteWindows()
294 m_sizer
->DeleteWindows();
297 bool wxSizerItem::IsWindow() const
299 return (m_window
!= NULL
);
302 bool wxSizerItem::IsSizer() const
304 return (m_sizer
!= NULL
);
307 bool wxSizerItem::IsSpacer() const
309 return (m_window
== NULL
) && (m_sizer
== NULL
);
312 void wxSizerItem::Show( bool show
)
317 m_window
->Show( show
);
319 m_sizer
->ShowItems( show
);
321 // ... nothing else to do to hide/show spacers
324 void wxSizerItem::SetOption( int option
)
326 SetProportion( option
);
329 int wxSizerItem::GetOption() const
331 return GetProportion();
335 //---------------------------------------------------------------------------
337 //---------------------------------------------------------------------------
340 : m_minSize( wxSize( 0, 0 ) )
346 WX_CLEAR_LIST(wxSizerItemList
, m_children
);
349 void wxSizer::Add( wxWindow
*window
, int proportion
, int flag
, int border
, wxObject
* userData
)
351 m_children
.Append( new wxSizerItem( window
, proportion
, flag
, border
, userData
) );
352 window
->SetContainingSizer( this );
355 void wxSizer::Add( wxSizer
*sizer
, int proportion
, int flag
, int border
, wxObject
* userData
)
357 m_children
.Append( new wxSizerItem( sizer
, proportion
, flag
, border
, userData
) );
360 void wxSizer::Add( int width
, int height
, int proportion
, int flag
, int border
, wxObject
* userData
)
362 m_children
.Append( new wxSizerItem( width
, height
, proportion
, flag
, border
, userData
) );
365 void wxSizer::Add( wxSizerItem
*item
)
367 m_children
.Append( item
);
369 if( item
->GetWindow() )
370 item
->GetWindow()->SetContainingSizer( this );
373 void wxSizer::Prepend( wxWindow
*window
, int proportion
, int flag
, int border
, wxObject
* userData
)
375 m_children
.Insert( new wxSizerItem( window
, proportion
, flag
, border
, userData
) );
376 window
->SetContainingSizer( this );
379 void wxSizer::Prepend( wxSizer
*sizer
, int proportion
, int flag
, int border
, wxObject
* userData
)
381 m_children
.Insert( new wxSizerItem( sizer
, proportion
, flag
, border
, userData
) );
384 void wxSizer::Prepend( int width
, int height
, int proportion
, int flag
, int border
, wxObject
* userData
)
386 m_children
.Insert( new wxSizerItem( width
, height
, proportion
, flag
, border
, userData
) );
389 void wxSizer::Prepend( wxSizerItem
*item
)
391 m_children
.Insert( item
);
393 if( item
->GetWindow() )
394 item
->GetWindow()->SetContainingSizer( this );
397 void wxSizer::Insert( size_t index
,
404 m_children
.Insert( index
,
405 new wxSizerItem( window
, proportion
, flag
, border
, userData
) );
406 window
->SetContainingSizer( this );
409 void wxSizer::Insert( size_t index
,
416 m_children
.Insert( index
,
417 new wxSizerItem( sizer
, proportion
, flag
, border
, userData
) );
420 void wxSizer::Insert( size_t index
,
428 m_children
.Insert( index
,
429 new wxSizerItem( width
, height
, proportion
, flag
, border
, userData
) );
432 void wxSizer::Insert( size_t index
, wxSizerItem
*item
)
434 m_children
.Insert( index
, item
);
436 if( item
->GetWindow() )
437 item
->GetWindow()->SetContainingSizer( this );
440 bool wxSizer::Remove( wxWindow
*window
)
442 return Detach( window
);
445 bool wxSizer::Remove( wxSizer
*sizer
)
447 wxASSERT_MSG( sizer
, _T("Removing NULL sizer") );
449 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
452 wxSizerItem
*item
= node
->GetData();
454 if (item
->GetSizer() == sizer
)
457 m_children
.Erase( node
);
461 node
= node
->GetNext();
467 bool wxSizer::Remove( int index
)
469 wxCHECK_MSG( index
>= 0 && (size_t)index
< m_children
.GetCount(),
471 _T("Remove index is out of range") );
473 wxSizerItemList::compatibility_iterator node
= m_children
.Item( index
);
475 wxCHECK_MSG( node
, false, _T("Failed to find child node") );
477 wxSizerItem
*item
= node
->GetData();
479 if( item
->IsWindow() )
480 item
->GetWindow()->SetContainingSizer( NULL
);
483 m_children
.Erase( node
);
487 bool wxSizer::Detach( wxSizer
*sizer
)
489 wxASSERT_MSG( sizer
, _T("Detaching NULL sizer") );
491 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
494 wxSizerItem
*item
= node
->GetData();
496 if (item
->GetSizer() == sizer
)
500 m_children
.Erase( node
);
503 node
= node
->GetNext();
509 bool wxSizer::Detach( wxWindow
*window
)
511 wxASSERT_MSG( window
, _T("Detaching NULL window") );
513 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
516 wxSizerItem
*item
= node
->GetData();
518 if (item
->GetWindow() == window
)
520 item
->GetWindow()->SetContainingSizer( NULL
);
522 m_children
.Erase( node
);
525 node
= node
->GetNext();
531 bool wxSizer::Detach( int index
)
533 wxCHECK_MSG( index
>= 0 && (size_t)index
< m_children
.GetCount(),
535 _T("Detach index is out of range") );
537 wxSizerItemList::compatibility_iterator node
= m_children
.Item( index
);
539 wxCHECK_MSG( node
, false, _T("Failed to find child node") );
541 wxSizerItem
*item
= node
->GetData();
543 if( item
->IsSizer() )
545 else if( item
->IsWindow() )
546 item
->GetWindow()->SetContainingSizer( NULL
);
549 m_children
.Erase( node
);
553 void wxSizer::Clear( bool delete_windows
)
555 // First clear the ContainingSizer pointers
556 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
559 wxSizerItem
*item
= node
->GetData();
561 if (item
->IsWindow())
562 item
->GetWindow()->SetContainingSizer( NULL
);
563 node
= node
->GetNext();
566 // Destroy the windows if needed
570 // Now empty the list
571 WX_CLEAR_LIST(wxSizerItemList
, m_children
);
574 void wxSizer::DeleteWindows()
576 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
579 wxSizerItem
*item
= node
->GetData();
581 item
->DeleteWindows();
582 node
= node
->GetNext();
586 wxSize
wxSizer::Fit( wxWindow
*window
)
588 wxSize
size(window
->IsTopLevel() ? FitSize(window
)
589 : GetMinWindowSize(window
));
591 window
->SetSize( size
);
596 void wxSizer::FitInside( wxWindow
*window
)
599 if (window
->IsTopLevel())
600 size
= VirtualFitSize( window
);
602 size
= GetMinClientSize( window
);
604 window
->SetVirtualSize( size
);
607 void wxSizer::Layout()
613 void wxSizer::SetSizeHints( wxWindow
*window
)
615 // Preserve the window's max size hints, but set the
616 // lower bound according to the sizer calculations.
618 wxSize size
= Fit( window
);
620 window
->SetSizeHints( size
.x
,
622 window
->GetMaxWidth(),
623 window
->GetMaxHeight() );
626 void wxSizer::SetVirtualSizeHints( wxWindow
*window
)
628 // Preserve the window's max size hints, but set the
629 // lower bound according to the sizer calculations.
632 wxSize
size( window
->GetVirtualSize() );
633 window
->SetVirtualSizeHints( size
.x
,
635 window
->GetMaxWidth(),
636 window
->GetMaxHeight() );
639 wxSize
wxSizer::GetMaxWindowSize( wxWindow
*window
) const
641 return window
->GetMaxSize();
644 wxSize
wxSizer::GetMinWindowSize( wxWindow
*window
)
646 wxSize
minSize( GetMinSize() );
647 wxSize
size( window
->GetSize() );
648 wxSize
client_size( window
->GetClientSize() );
650 return wxSize( minSize
.x
+size
.x
-client_size
.x
,
651 minSize
.y
+size
.y
-client_size
.y
);
654 // TODO on mac we need a function that determines how much free space this
655 // min size contains, in order to make sure that we have 20 pixels of free
656 // space around the controls
658 // Return a window size that will fit within the screens dimensions
659 wxSize
wxSizer::FitSize( wxWindow
*window
)
661 wxSize size
= GetMinWindowSize( window
);
662 wxSize sizeMax
= GetMaxWindowSize( window
);
664 // Limit the size if sizeMax != wxDefaultSize
666 if ( size
.x
> sizeMax
.x
&& sizeMax
.x
!= -1 )
668 if ( size
.y
> sizeMax
.y
&& sizeMax
.y
!= -1 )
674 wxSize
wxSizer::GetMaxClientSize( wxWindow
*window
) const
676 wxSize
maxSize( window
->GetMaxSize() );
678 if( maxSize
!= wxDefaultSize
)
680 wxSize
size( window
->GetSize() );
681 wxSize
client_size( window
->GetClientSize() );
683 return wxSize( maxSize
.x
+ client_size
.x
- size
.x
,
684 maxSize
.y
+ client_size
.y
- size
.y
);
687 return wxDefaultSize
;
690 wxSize
wxSizer::GetMinClientSize( wxWindow
*WXUNUSED(window
) )
692 return GetMinSize(); // Already returns client size.
695 wxSize
wxSizer::VirtualFitSize( wxWindow
*window
)
697 wxSize size
= GetMinClientSize( window
);
698 wxSize sizeMax
= GetMaxClientSize( window
);
700 // Limit the size if sizeMax != wxDefaultSize
702 if ( size
.x
> sizeMax
.x
&& sizeMax
.x
!= -1 )
704 if ( size
.y
> sizeMax
.y
&& sizeMax
.y
!= -1 )
710 void wxSizer::SetDimension( int x
, int y
, int width
, int height
)
719 wxSize
wxSizer::GetMinSize()
721 wxSize
ret( CalcMin() );
722 if (ret
.x
< m_minSize
.x
) ret
.x
= m_minSize
.x
;
723 if (ret
.y
< m_minSize
.y
) ret
.y
= m_minSize
.y
;
727 void wxSizer::DoSetMinSize( int width
, int height
)
730 m_minSize
.y
= height
;
733 bool wxSizer::DoSetItemMinSize( wxWindow
*window
, int width
, int height
)
735 wxASSERT_MSG( window
, _T("SetMinSize for NULL window") );
737 // Is it our immediate child?
739 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
742 wxSizerItem
*item
= node
->GetData();
744 if (item
->GetWindow() == window
)
746 item
->SetMinSize( width
, height
);
749 node
= node
->GetNext();
752 // No? Search any subsizers we own then
754 node
= m_children
.GetFirst();
757 wxSizerItem
*item
= node
->GetData();
759 if ( item
->GetSizer() &&
760 item
->GetSizer()->DoSetItemMinSize( window
, width
, height
) )
762 // A child sizer found the requested windw, exit.
765 node
= node
->GetNext();
771 bool wxSizer::DoSetItemMinSize( wxSizer
*sizer
, int width
, int height
)
773 wxASSERT_MSG( sizer
, _T("SetMinSize for NULL sizer") );
775 // Is it our immediate child?
777 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
780 wxSizerItem
*item
= node
->GetData();
782 if (item
->GetSizer() == sizer
)
784 item
->GetSizer()->DoSetMinSize( width
, height
);
787 node
= node
->GetNext();
790 // No? Search any subsizers we own then
792 node
= m_children
.GetFirst();
795 wxSizerItem
*item
= node
->GetData();
797 if ( item
->GetSizer() &&
798 item
->GetSizer()->DoSetItemMinSize( sizer
, width
, height
) )
800 // A child found the requested sizer, exit.
803 node
= node
->GetNext();
809 bool wxSizer::DoSetItemMinSize( size_t index
, int width
, int height
)
811 wxSizerItemList::compatibility_iterator node
= m_children
.Item( index
);
813 wxCHECK_MSG( node
, false, _T("Failed to find child node") );
815 wxSizerItem
*item
= node
->GetData();
817 if (item
->GetSizer())
819 // Sizers contains the minimal size in them, if not calculated ...
820 item
->GetSizer()->DoSetMinSize( width
, height
);
824 // ... but the minimal size of spacers and windows in stored in them
825 item
->SetMinSize( width
, height
);
831 void wxSizer::Show( wxWindow
*window
, bool show
)
833 wxASSERT_MSG( window
, _T("Show for NULL window") );
835 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
838 wxSizerItem
*item
= node
->GetData();
840 if (item
->GetWindow() == window
)
845 node
= node
->GetNext();
849 void wxSizer::Show( wxSizer
*sizer
, bool show
)
851 wxASSERT_MSG( sizer
, _T("Show for NULL sizer") );
853 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
856 wxSizerItem
*item
= node
->GetData();
858 if (item
->GetSizer() == sizer
)
863 node
= node
->GetNext();
867 void wxSizer::Show( size_t index
, bool show
)
869 wxCHECK_RET( index
< m_children
.GetCount(),
870 _T("Show index is out of range") );
872 m_children
.Item( index
)->GetData()->Show( show
);
875 void wxSizer::ShowItems( bool show
)
877 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
880 node
->GetData()->Show( show
);
881 node
= node
->GetNext();
885 bool wxSizer::IsShown( wxWindow
*window
) const
887 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
890 wxSizerItem
*item
= node
->GetData();
892 if (item
->GetWindow() == window
)
894 return item
->IsShown();
896 node
= node
->GetNext();
899 wxFAIL_MSG( _T("IsShown failed to find sizer item") );
904 bool wxSizer::IsShown( wxSizer
*sizer
) const
906 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
909 wxSizerItem
*item
= node
->GetData();
911 if (item
->GetSizer() == sizer
)
913 return item
->IsShown();
915 node
= node
->GetNext();
918 wxFAIL_MSG( _T("IsShown failed to find sizer item") );
923 bool wxSizer::IsShown( size_t index
) const
925 wxCHECK_MSG( index
< m_children
.GetCount(),
927 _T("IsShown index is out of range") );
929 return m_children
.Item( index
)->GetData()->IsShown();
933 //---------------------------------------------------------------------------
935 //---------------------------------------------------------------------------
937 wxGridSizer::wxGridSizer( int rows
, int cols
, int vgap
, int hgap
)
943 if (m_rows
== 0 && m_cols
== 0)
947 wxGridSizer::wxGridSizer( int cols
, int vgap
, int hgap
)
953 if (m_rows
== 0 && m_cols
== 0)
957 int wxGridSizer::CalcRowsCols(int& nrows
, int& ncols
) const
959 int nitems
= m_children
.GetCount();
965 nrows
= (nitems
+ m_cols
- 1) / m_cols
;
969 ncols
= (nitems
+ m_rows
- 1) / m_rows
;
972 else // 0 columns, 0 rows?
974 wxFAIL_MSG( _T("grid sizer must have either rows or columns fixed") );
983 void wxGridSizer::RecalcSizes()
985 int nitems
, nrows
, ncols
;
986 if ( (nitems
= CalcRowsCols(nrows
, ncols
)) == 0 )
989 wxSize
sz( GetSize() );
990 wxPoint
pt( GetPosition() );
992 int w
= (sz
.x
- (ncols
- 1) * m_hgap
) / ncols
;
993 int h
= (sz
.y
- (nrows
- 1) * m_vgap
) / nrows
;
996 for (int c
= 0; c
< ncols
; c
++)
999 for (int r
= 0; r
< nrows
; r
++)
1001 int i
= r
* ncols
+ c
;
1004 wxSizerItemList::compatibility_iterator node
= m_children
.Item( i
);
1006 wxASSERT_MSG( node
, _T("Failed to find SizerItemList node") );
1008 SetItemBounds( node
->GetData(), x
, y
, w
, h
);
1016 wxSize
wxGridSizer::CalcMin()
1019 if ( CalcRowsCols(nrows
, ncols
) == 0 )
1020 return wxSize(10, 10);
1022 // Find the max width and height for any component
1026 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
1029 wxSizerItem
*item
= node
->GetData();
1030 wxSize
sz( item
->CalcMin() );
1032 w
= wxMax( w
, sz
.x
);
1033 h
= wxMax( h
, sz
.y
);
1035 node
= node
->GetNext();
1038 return wxSize( ncols
* w
+ (ncols
-1) * m_hgap
,
1039 nrows
* h
+ (nrows
-1) * m_vgap
);
1042 void wxGridSizer::SetItemBounds( wxSizerItem
*item
, int x
, int y
, int w
, int h
)
1045 wxSize
sz( item
->CalcMin() );
1046 int flag
= item
->GetFlag();
1048 if ((flag
& wxEXPAND
) || (flag
& wxSHAPED
))
1054 if (flag
& wxALIGN_CENTER_HORIZONTAL
)
1056 pt
.x
= x
+ (w
- sz
.x
) / 2;
1058 else if (flag
& wxALIGN_RIGHT
)
1060 pt
.x
= x
+ (w
- sz
.x
);
1063 if (flag
& wxALIGN_CENTER_VERTICAL
)
1065 pt
.y
= y
+ (h
- sz
.y
) / 2;
1067 else if (flag
& wxALIGN_BOTTOM
)
1069 pt
.y
= y
+ (h
- sz
.y
);
1073 item
->SetDimension(pt
, sz
);
1076 //---------------------------------------------------------------------------
1078 //---------------------------------------------------------------------------
1080 wxFlexGridSizer::wxFlexGridSizer( int rows
, int cols
, int vgap
, int hgap
)
1081 : wxGridSizer( rows
, cols
, vgap
, hgap
),
1082 m_flexDirection(wxBOTH
),
1083 m_growMode(wxFLEX_GROWMODE_SPECIFIED
)
1087 wxFlexGridSizer::wxFlexGridSizer( int cols
, int vgap
, int hgap
)
1088 : wxGridSizer( cols
, vgap
, hgap
),
1089 m_flexDirection(wxBOTH
),
1090 m_growMode(wxFLEX_GROWMODE_SPECIFIED
)
1094 wxFlexGridSizer::~wxFlexGridSizer()
1098 void wxFlexGridSizer::RecalcSizes()
1100 int nitems
, nrows
, ncols
;
1101 if ( (nitems
= CalcRowsCols(nrows
, ncols
)) == 0 )
1104 wxPoint
pt( GetPosition() );
1105 wxSize
sz( GetSize() );
1106 wxSize
minsz( CalcMin() );
1108 AdjustForGrowables(sz
, minsz
, nrows
, ncols
);
1110 sz
= wxSize( pt
.x
+ sz
.x
, pt
.y
+ sz
.y
);
1113 for (int c
= 0; c
< ncols
; c
++)
1116 for (int r
= 0; r
< nrows
; r
++)
1118 int i
= r
* ncols
+ c
;
1121 wxSizerItemList::compatibility_iterator node
= m_children
.Item( i
);
1123 wxASSERT_MSG( node
, _T("Failed to find node") );
1125 int w
= wxMax( 0, wxMin( m_colWidths
[c
], sz
.x
- x
) );
1126 int h
= wxMax( 0, wxMin( m_rowHeights
[r
], sz
.y
- y
) );
1128 SetItemBounds( node
->GetData(), x
, y
, w
, h
);
1130 y
= y
+ m_rowHeights
[r
] + m_vgap
;
1132 x
= x
+ m_colWidths
[c
] + m_hgap
;
1136 wxSize
wxFlexGridSizer::CalcMin()
1142 // Number of rows/columns can change as items are added or removed.
1143 if ( !CalcRowsCols(nrows
, ncols
) )
1144 return wxSize(10, 10);
1146 m_rowHeights
.SetCount(nrows
);
1147 m_colWidths
.SetCount(ncols
);
1149 // We have to recalcuate the sizes in case the item minimum size has
1150 // changed since the previous layout, or the item has been hidden using
1151 // wxSizer::Show(). If all the items in a row/column are hidden, the final
1152 // dimension of the row/column will be -1, indicating that the column
1153 // itself is hidden.
1154 for( s
= m_rowHeights
.GetCount(), i
= 0; i
< s
; ++i
)
1155 m_rowHeights
[ i
] = -1;
1156 for( s
= m_colWidths
.GetCount(), i
= 0; i
< s
; ++i
)
1157 m_colWidths
[ i
] = -1;
1159 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
1164 wxSizerItem
*item
= node
->GetData();
1165 if ( item
->IsShown() )
1167 wxSize
sz( item
->CalcMin() );
1168 int row
= i
/ ncols
;
1169 int col
= i
% ncols
;
1171 m_rowHeights
[ row
] = wxMax( wxMax( 0, sz
.y
), m_rowHeights
[ row
] );
1172 m_colWidths
[ col
] = wxMax( wxMax( 0, sz
.x
), m_colWidths
[ col
] );
1175 node
= node
->GetNext();
1179 AdjustForFlexDirection();
1181 // Sum total minimum size, including gaps between rows/columns.
1182 // -1 is used as a magic number meaning empty column.
1184 for (int col
= 0; col
< ncols
; col
++)
1185 if ( m_colWidths
[ col
] != -1 )
1186 width
+= m_colWidths
[ col
] + ( col
== ncols
-1 ? 0 : m_hgap
);
1189 for (int row
= 0; row
< nrows
; row
++)
1190 if ( m_rowHeights
[ row
] != -1 )
1191 height
+= m_rowHeights
[ row
] + ( row
== nrows
-1 ? 0 : m_vgap
);
1193 return wxSize( width
, height
);
1196 void wxFlexGridSizer::AdjustForFlexDirection()
1198 // the logic in CalcMin works when we resize flexibly in both directions
1199 // but maybe this is not the case
1200 if ( m_flexDirection
!= wxBOTH
)
1202 // select the array corresponding to the direction in which we do *not*
1204 wxArrayInt
& array
= m_flexDirection
== wxVERTICAL
? m_colWidths
1207 const int count
= array
.GetCount();
1209 // find the largest value in this array
1211 for ( n
= 0; n
< count
; ++n
)
1213 if ( array
[n
] > largest
)
1217 // and now fill it with the largest value
1218 for ( n
= 0; n
< count
; ++n
)
1226 void wxFlexGridSizer::AdjustForGrowables(const wxSize
& sz
, const wxSize
& minsz
,
1227 int nrows
, int ncols
)
1229 // what to do with the rows? by default, resize them proportionally
1230 if ( sz
.y
> minsz
.y
&& ( (m_flexDirection
& wxVERTICAL
) || (m_growMode
== wxFLEX_GROWMODE_SPECIFIED
) ) )
1232 int sum_proportions
= 0;
1233 int growable_space
= 0;
1236 for (idx
= 0; idx
< m_growableRows
.GetCount(); idx
++)
1238 // Since the number of rows/columns can change as items are
1239 // inserted/deleted, we need to verify at runtime that the
1240 // requested growable rows/columns are still valid.
1241 if (m_growableRows
[idx
] >= nrows
)
1244 // If all items in a row/column are hidden, that row/column will
1245 // have a dimension of -1. This causes the row/column to be
1246 // hidden completely.
1247 if (m_rowHeights
[ m_growableRows
[idx
] ] == -1)
1249 sum_proportions
+= m_growableRowsProportions
[idx
];
1250 growable_space
+= m_rowHeights
[ m_growableRows
[idx
] ];
1256 for (idx
= 0; idx
< m_growableRows
.GetCount(); idx
++)
1258 if (m_growableRows
[idx
] >= nrows
)
1260 if (m_rowHeights
[ m_growableRows
[idx
] ] == -1)
1261 m_rowHeights
[ m_growableRows
[idx
] ] = 0;
1264 int delta
= (sz
.y
- minsz
.y
);
1265 if (sum_proportions
== 0)
1266 delta
= (delta
/num
) + m_rowHeights
[ m_growableRows
[idx
] ];
1268 delta
= ((delta
+growable_space
)*m_growableRowsProportions
[idx
]) / sum_proportions
;
1269 m_rowHeights
[ m_growableRows
[idx
] ] = delta
;
1274 else if ( (m_growMode
== wxFLEX_GROWMODE_ALL
) && (sz
.y
> minsz
.y
) )
1276 // rounding problem?
1277 for ( int row
= 0; row
< nrows
; ++row
)
1278 m_rowHeights
[ row
] = sz
.y
/ nrows
;
1281 // the same logic as above but for the columns
1282 if ( sz
.x
> minsz
.x
&& ( (m_flexDirection
& wxHORIZONTAL
) || (m_growMode
== wxFLEX_GROWMODE_SPECIFIED
) ) )
1284 int sum_proportions
= 0;
1285 int growable_space
= 0;
1288 for (idx
= 0; idx
< m_growableCols
.GetCount(); idx
++)
1290 // Since the number of rows/columns can change as items are
1291 // inserted/deleted, we need to verify at runtime that the
1292 // requested growable rows/columns are still valid.
1293 if (m_growableCols
[idx
] >= ncols
)
1296 // If all items in a row/column are hidden, that row/column will
1297 // have a dimension of -1. This causes the column to be hidden
1299 if (m_colWidths
[ m_growableCols
[idx
] ] == -1)
1301 sum_proportions
+= m_growableColsProportions
[idx
];
1302 growable_space
+= m_colWidths
[ m_growableCols
[idx
] ];
1308 for (idx
= 0; idx
< m_growableCols
.GetCount(); idx
++)
1310 if (m_growableCols
[idx
] >= ncols
)
1312 if (m_colWidths
[ m_growableCols
[idx
] ] == -1)
1313 m_colWidths
[ m_growableCols
[idx
] ] = 0;
1316 int delta
= (sz
.x
- minsz
.x
);
1317 if (sum_proportions
== 0)
1318 delta
= (delta
/num
) + m_colWidths
[ m_growableCols
[idx
] ];
1320 delta
= ((delta
+growable_space
)*m_growableColsProportions
[idx
])/sum_proportions
;
1321 m_colWidths
[ m_growableCols
[idx
] ] = delta
;
1326 else if ( (m_growMode
== wxFLEX_GROWMODE_ALL
) && (sz
.x
> minsz
.x
) )
1328 for ( int col
=0; col
< ncols
; ++col
)
1329 m_colWidths
[ col
] = sz
.x
/ ncols
;
1334 void wxFlexGridSizer::AddGrowableRow( size_t idx
, int proportion
)
1336 m_growableRows
.Add( idx
);
1337 m_growableRowsProportions
.Add( proportion
);
1340 void wxFlexGridSizer::RemoveGrowableRow( size_t idx
)
1342 m_growableRows
.Remove( idx
);
1345 void wxFlexGridSizer::AddGrowableCol( size_t idx
, int proportion
)
1347 m_growableCols
.Add( idx
);
1348 m_growableColsProportions
.Add( proportion
);
1351 void wxFlexGridSizer::RemoveGrowableCol( size_t idx
)
1353 m_growableCols
.Remove( idx
);
1356 //---------------------------------------------------------------------------
1358 //---------------------------------------------------------------------------
1360 wxBoxSizer::wxBoxSizer( int orient
)
1361 : m_orient( orient
)
1365 void wxBoxSizer::RecalcSizes()
1367 if (m_children
.GetCount() == 0)
1373 if (m_orient
== wxHORIZONTAL
)
1374 delta
= m_size
.x
- m_fixedWidth
;
1376 delta
= m_size
.y
- m_fixedHeight
;
1379 wxPoint
pt( m_position
);
1381 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
1384 wxSizerItem
*item
= node
->GetData();
1386 if (item
->IsShown())
1388 wxSize
size( item
->CalcMin() );
1390 if (m_orient
== wxVERTICAL
)
1392 wxCoord height
= size
.y
;
1393 if (item
->GetProportion())
1395 // Because of at least one visible item has non-zero
1396 // proportion then m_stretchable is not zero
1397 height
= (delta
* item
->GetProportion()) / m_stretchable
;
1400 wxPoint
child_pos( pt
);
1401 wxSize
child_size( wxSize( size
.x
, height
) );
1403 if (item
->GetFlag() & (wxEXPAND
| wxSHAPED
))
1404 child_size
.x
= m_size
.x
;
1405 else if (item
->GetFlag() & wxALIGN_RIGHT
)
1406 child_pos
.x
+= m_size
.x
- size
.x
;
1407 else if (item
->GetFlag() & (wxCENTER
| wxALIGN_CENTER_HORIZONTAL
))
1408 // XXX wxCENTER is added for backward compatibility;
1409 // wxALIGN_CENTER should be used in new code
1410 child_pos
.x
+= (m_size
.x
- size
.x
) / 2;
1412 item
->SetDimension( child_pos
, child_size
);
1418 wxCoord width
= size
.x
;
1419 if (item
->GetProportion())
1421 // Because of at least one visible item has non-zero
1422 // proportion then m_stretchable is not zero
1423 width
= (delta
* item
->GetProportion()) / m_stretchable
;
1426 wxPoint
child_pos( pt
);
1427 wxSize
child_size( wxSize(width
, size
.y
) );
1429 if (item
->GetFlag() & (wxEXPAND
| wxSHAPED
))
1430 child_size
.y
= m_size
.y
;
1431 else if (item
->GetFlag() & wxALIGN_BOTTOM
)
1432 child_pos
.y
+= m_size
.y
- size
.y
;
1433 else if (item
->GetFlag() & (wxCENTER
| wxALIGN_CENTER_VERTICAL
))
1434 // XXX wxCENTER is added for backward compatibility;
1435 // wxALIGN_CENTER should be used in new code
1436 child_pos
.y
+= (m_size
.y
- size
.y
) / 2;
1438 item
->SetDimension( child_pos
, child_size
);
1444 node
= node
->GetNext();
1448 wxSize
wxBoxSizer::CalcMin()
1450 if (m_children
.GetCount() == 0)
1451 return wxSize(10,10);
1459 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
1462 wxSizerItem
*item
= node
->GetData();
1464 if (item
->IsShown() && item
->GetProportion() != 0)
1465 m_stretchable
+= item
->GetProportion();
1467 node
= node
->GetNext();
1470 // Total minimum size (width or height) of sizer
1473 node
= m_children
.GetFirst();
1476 wxSizerItem
*item
= node
->GetData();
1478 if (item
->IsShown() && item
->GetProportion() != 0)
1480 int stretch
= item
->GetProportion();
1481 wxSize
size( item
->CalcMin() );
1484 // Integer division rounded up is (a + b - 1) / b
1485 // Round up needed in order to guarantee that all
1486 // all items will have size not less then their min size
1487 if (m_orient
== wxHORIZONTAL
)
1488 minSize
= ( size
.x
*m_stretchable
+ stretch
- 1)/stretch
;
1490 minSize
= ( size
.y
*m_stretchable
+ stretch
- 1)/stretch
;
1492 if (minSize
> maxMinSize
)
1493 maxMinSize
= minSize
;
1495 node
= node
->GetNext();
1498 // Calculate overall minimum size
1499 node
= m_children
.GetFirst();
1502 wxSizerItem
*item
= node
->GetData();
1504 if (item
->IsShown())
1506 wxSize
size( item
->CalcMin() );
1507 if (item
->GetProportion() != 0)
1509 if (m_orient
== wxHORIZONTAL
)
1510 size
.x
= (maxMinSize
*item
->GetProportion())/m_stretchable
;
1512 size
.y
= (maxMinSize
*item
->GetProportion())/m_stretchable
;
1516 if (m_orient
== wxVERTICAL
)
1518 m_fixedHeight
+= size
.y
;
1519 m_fixedWidth
= wxMax( m_fixedWidth
, size
.x
);
1523 m_fixedWidth
+= size
.x
;
1524 m_fixedHeight
= wxMax( m_fixedHeight
, size
.y
);
1528 if (m_orient
== wxHORIZONTAL
)
1530 m_minWidth
+= size
.x
;
1531 m_minHeight
= wxMax( m_minHeight
, size
.y
);
1535 m_minHeight
+= size
.y
;
1536 m_minWidth
= wxMax( m_minWidth
, size
.x
);
1539 node
= node
->GetNext();
1542 return wxSize( m_minWidth
, m_minHeight
);
1545 //---------------------------------------------------------------------------
1547 //---------------------------------------------------------------------------
1551 wxStaticBoxSizer::wxStaticBoxSizer( wxStaticBox
*box
, int orient
)
1552 : wxBoxSizer( orient
)
1553 , m_staticBox( box
)
1555 wxASSERT_MSG( box
, wxT("wxStaticBoxSizer needs a static box") );
1558 static void GetStaticBoxBorders( wxStaticBox
*box
,
1562 // this has to be done platform by platform as there is no way to
1563 // guess the thickness of a wxStaticBox border
1565 box
->GetBordersForSizer(borderTop
,borderOther
);
1566 #elif defined(__WXMAC__)
1568 static int extraTop
= -1; // Uninitted
1569 static int other
= 5;
1571 if ( extraTop
== -1 )
1573 // The minimal border used for the top. Later on the staticbox'
1574 // font height is added to this.
1577 if ( UMAGetSystemVersion() >= 0x1030 /*Panther*/ )
1579 // As indicated by the HIG, Panther needs an extra border of 11
1580 // pixels (otherwise overlapping occurs at the top). The "other"
1581 // border has to be 11.
1588 *borderTop
= extraTop
+ box
->GetCharHeight();
1589 *borderOther
= other
;
1593 if ( box
->GetLabel().IsEmpty() )
1597 *borderTop
= box
->GetCharHeight();
1600 #endif // __WXCOCOA__
1603 void wxStaticBoxSizer::RecalcSizes()
1605 int top_border
, other_border
;
1606 GetStaticBoxBorders(m_staticBox
, &top_border
, &other_border
);
1608 m_staticBox
->SetSize( m_position
.x
, m_position
.y
, m_size
.x
, m_size
.y
);
1610 wxPoint
old_pos( m_position
);
1611 m_position
.x
+= other_border
;
1612 m_position
.y
+= top_border
;
1613 wxSize
old_size( m_size
);
1614 m_size
.x
-= 2*other_border
;
1615 m_size
.y
-= top_border
+ other_border
;
1617 wxBoxSizer::RecalcSizes();
1619 m_position
= old_pos
;
1623 wxSize
wxStaticBoxSizer::CalcMin()
1625 int top_border
, other_border
;
1626 GetStaticBoxBorders(m_staticBox
, &top_border
, &other_border
);
1628 wxSize
ret( wxBoxSizer::CalcMin() );
1629 ret
.x
+= 2*other_border
;
1630 ret
.y
+= other_border
+ top_border
;
1635 void wxStaticBoxSizer::ShowItems( bool show
)
1637 m_staticBox
->Show( show
);
1638 wxBoxSizer::ShowItems( show
);
1641 #endif // wxUSE_STATBOX
1643 // ----------------------------------------------------------------------------
1645 // ----------------------------------------------------------------------------
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
;
1701 wxNotebookSizer::wxNotebookSizer(wxNotebook
*nb
)
1702 : wxBookCtrlSizer(nb
)
1706 #endif // wxUSE_NOTEBOOOK
1707 #endif // wxUSE_BOOKCTRL