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"
25 #include "wx/string.h"
31 #include "wx/statbox.h"
32 #include "wx/settings.h"
33 #include "wx/listimpl.cpp"
35 #if WXWIN_COMPATIBILITY_2_4
36 #include "wx/notebook.h"
39 //---------------------------------------------------------------------------
41 IMPLEMENT_CLASS(wxSizerItem
, wxObject
)
42 IMPLEMENT_CLASS(wxSizer
, wxObject
)
43 IMPLEMENT_CLASS(wxGridSizer
, wxSizer
)
44 IMPLEMENT_CLASS(wxFlexGridSizer
, wxGridSizer
)
45 IMPLEMENT_CLASS(wxBoxSizer
, wxSizer
)
47 IMPLEMENT_CLASS(wxStaticBoxSizer
, wxBoxSizer
)
50 IMPLEMENT_CLASS(wxStdDialogButtonSizer
, wxBoxSizer
)
53 WX_DEFINE_EXPORTED_LIST( wxSizerItemList
);
88 // ----------------------------------------------------------------------------
90 // ----------------------------------------------------------------------------
92 void wxSizerItem::Init(const wxSizerFlags
& flags
)
96 m_proportion
= flags
.GetProportion();
97 m_flag
= flags
.GetFlags();
98 m_border
= flags
.GetBorderInPixels();
101 wxSizerItem::wxSizerItem()
113 void wxSizerItem::SetWindow(wxWindow
*window
)
115 wxCHECK_RET( window
, _T("NULL window in wxSizerItem::SetWindow()") );
117 m_kind
= Item_Window
;
120 // window doesn't become smaller than its initial size, whatever happens
121 m_minSize
= window
->GetSize();
123 if ( m_flag
& wxFIXED_MINSIZE
)
124 window
->SetMinSize(m_minSize
);
126 // aspect ratio calculated from initial size
130 wxSizerItem::wxSizerItem(wxWindow
*window
,
135 : m_proportion(proportion
),
144 void wxSizerItem::SetSizer(wxSizer
*sizer
)
150 wxSizerItem::wxSizerItem(wxSizer
*sizer
,
155 : m_proportion(proportion
),
163 // m_minSize is set later
167 void wxSizerItem::SetSpacer(const wxSize
& size
)
169 m_kind
= Item_Spacer
;
170 m_spacer
= new wxSizerSpacer(size
);
175 wxSizerItem::wxSizerItem(int width
,
181 : m_minSize(width
, height
), // minimal size is the initial size
182 m_proportion(proportion
),
187 SetSpacer(width
, height
);
190 wxSizerItem::~wxSizerItem()
200 m_window
->SetContainingSizer(NULL
);
213 wxFAIL_MSG( _T("unexpected wxSizerItem::m_kind") );
217 wxSize
wxSizerItem::GetSpacer() const
220 if ( m_kind
== Item_Spacer
)
221 size
= m_spacer
->GetSize();
227 wxSize
wxSizerItem::GetSize() const
236 ret
= m_window
->GetSize();
240 ret
= m_sizer
->GetSize();
244 ret
= m_spacer
->GetSize();
249 wxFAIL_MSG( _T("unexpected wxSizerItem::m_kind") );
256 if (m_flag
& wxNORTH
)
258 if (m_flag
& wxSOUTH
)
264 wxSize
wxSizerItem::CalcMin()
268 m_minSize
= m_sizer
->GetMinSize();
270 // if we have to preserve aspect ratio _AND_ this is
271 // the first-time calculation, consider ret to be initial size
272 if ((m_flag
& wxSHAPED
) && !m_ratio
)
275 else if ( IsWindow() )
277 // Since the size of the window may change during runtime, we
278 // should use the current minimal/best size.
279 m_minSize
= m_window
->GetBestFittingSize();
282 return GetMinSizeWithBorder();
285 wxSize
wxSizerItem::GetMinSizeWithBorder() const
287 wxSize ret
= m_minSize
;
293 if (m_flag
& wxNORTH
)
295 if (m_flag
& wxSOUTH
)
302 void wxSizerItem::SetDimension( wxPoint pos
, wxSize size
)
304 if (m_flag
& wxSHAPED
)
306 // adjust aspect ratio
307 int rwidth
= (int) (size
.y
* m_ratio
);
311 int rheight
= (int) (size
.x
/ m_ratio
);
312 // add vertical space
313 if (m_flag
& wxALIGN_CENTER_VERTICAL
)
314 pos
.y
+= (size
.y
- rheight
) / 2;
315 else if (m_flag
& wxALIGN_BOTTOM
)
316 pos
.y
+= (size
.y
- rheight
);
317 // use reduced dimensions
320 else if (rwidth
< size
.x
)
322 // add horizontal space
323 if (m_flag
& wxALIGN_CENTER_HORIZONTAL
)
324 pos
.x
+= (size
.x
- rwidth
) / 2;
325 else if (m_flag
& wxALIGN_RIGHT
)
326 pos
.x
+= (size
.x
- rwidth
);
331 // This is what GetPosition() returns. Since we calculate
332 // borders afterwards, GetPosition() will be the left/top
333 // corner of the surrounding border.
345 if (m_flag
& wxNORTH
)
350 if (m_flag
& wxSOUTH
)
355 m_rect
= wxRect(pos
, size
);
360 wxFAIL_MSG( _T("can't set size of uninitialized sizer item") );
364 m_window
->SetSize(pos
.x
, pos
.y
, size
.x
, size
.y
,
365 wxSIZE_ALLOW_MINUS_ONE
);
369 m_sizer
->SetDimension(pos
.x
, pos
.y
, size
.x
, size
.y
);
373 m_spacer
->SetSize(size
);
378 wxFAIL_MSG( _T("unexpected wxSizerItem::m_kind") );
382 void wxSizerItem::DeleteWindows()
395 m_sizer
->DeleteWindows();
400 wxFAIL_MSG( _T("unexpected wxSizerItem::m_kind") );
406 void wxSizerItem::Show( bool show
)
411 wxFAIL_MSG( _T("can't show uninitialized sizer item") );
415 m_window
->Show(show
);
423 m_spacer
->Show(show
);
428 wxFAIL_MSG( _T("unexpected wxSizerItem::m_kind") );
432 bool wxSizerItem::IsShown() const
437 wxFAIL_MSG( _T("uninitialized sizer item") );
441 return m_window
->IsShown();
444 return m_sizer
->IsShown();
447 return m_spacer
->IsShown();
451 wxFAIL_MSG( _T("unexpected wxSizerItem::m_kind") );
457 void wxSizerItem::SetOption( int option
)
459 SetProportion( option
);
462 int wxSizerItem::GetOption() const
464 return GetProportion();
468 //---------------------------------------------------------------------------
470 //---------------------------------------------------------------------------
479 WX_CLEAR_LIST(wxSizerItemList
, m_children
);
482 wxSizerItem
* wxSizer::Insert( size_t index
, wxSizerItem
*item
)
484 m_children
.Insert( index
, item
);
486 if ( item
->GetWindow() )
487 item
->GetWindow()->SetContainingSizer( this );
492 bool wxSizer::Remove( wxWindow
*window
)
494 return Detach( window
);
497 bool wxSizer::Remove( wxSizer
*sizer
)
499 wxASSERT_MSG( sizer
, _T("Removing NULL sizer") );
501 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
504 wxSizerItem
*item
= node
->GetData();
506 if (item
->GetSizer() == sizer
)
509 m_children
.Erase( node
);
513 node
= node
->GetNext();
519 bool wxSizer::Remove( int index
)
521 wxCHECK_MSG( index
>= 0 && (size_t)index
< m_children
.GetCount(),
523 _T("Remove index is out of range") );
525 wxSizerItemList::compatibility_iterator node
= m_children
.Item( index
);
527 wxCHECK_MSG( node
, false, _T("Failed to find child node") );
529 wxSizerItem
*item
= node
->GetData();
531 if ( item
->IsWindow() )
532 item
->GetWindow()->SetContainingSizer( NULL
);
535 m_children
.Erase( node
);
539 bool wxSizer::Detach( wxSizer
*sizer
)
541 wxASSERT_MSG( sizer
, _T("Detaching NULL sizer") );
543 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
546 wxSizerItem
*item
= node
->GetData();
548 if (item
->GetSizer() == sizer
)
552 m_children
.Erase( node
);
555 node
= node
->GetNext();
561 bool wxSizer::Detach( wxWindow
*window
)
563 wxASSERT_MSG( window
, _T("Detaching NULL window") );
565 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
568 wxSizerItem
*item
= node
->GetData();
570 if (item
->GetWindow() == window
)
572 item
->GetWindow()->SetContainingSizer( NULL
);
574 m_children
.Erase( node
);
577 node
= node
->GetNext();
583 bool wxSizer::Detach( int index
)
585 wxCHECK_MSG( index
>= 0 && (size_t)index
< m_children
.GetCount(),
587 _T("Detach index is out of range") );
589 wxSizerItemList::compatibility_iterator node
= m_children
.Item( index
);
591 wxCHECK_MSG( node
, false, _T("Failed to find child node") );
593 wxSizerItem
*item
= node
->GetData();
595 if ( item
->IsSizer() )
597 else if ( item
->IsWindow() )
598 item
->GetWindow()->SetContainingSizer( NULL
);
601 m_children
.Erase( node
);
605 void wxSizer::Clear( bool delete_windows
)
607 // First clear the ContainingSizer pointers
608 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
611 wxSizerItem
*item
= node
->GetData();
613 if (item
->IsWindow())
614 item
->GetWindow()->SetContainingSizer( NULL
);
615 node
= node
->GetNext();
618 // Destroy the windows if needed
622 // Now empty the list
623 WX_CLEAR_LIST(wxSizerItemList
, m_children
);
626 void wxSizer::DeleteWindows()
628 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
631 wxSizerItem
*item
= node
->GetData();
633 item
->DeleteWindows();
634 node
= node
->GetNext();
638 wxSize
wxSizer::Fit( wxWindow
*window
)
640 wxSize
size(window
->IsTopLevel() ? FitSize(window
)
641 : GetMinWindowSize(window
));
643 window
->SetSize( size
);
648 void wxSizer::FitInside( wxWindow
*window
)
651 if (window
->IsTopLevel())
652 size
= VirtualFitSize( window
);
654 size
= GetMinClientSize( window
);
656 window
->SetVirtualSize( size
);
659 void wxSizer::Layout()
661 // (re)calculates minimums needed for each item and other preparations
665 // Applies the layout and repositions/resizes the items
669 void wxSizer::SetSizeHints( wxWindow
*window
)
671 // Preserve the window's max size hints, but set the
672 // lower bound according to the sizer calculations.
674 wxSize size
= Fit( window
);
676 window
->SetSizeHints( size
.x
,
678 window
->GetMaxWidth(),
679 window
->GetMaxHeight() );
682 void wxSizer::SetVirtualSizeHints( wxWindow
*window
)
684 // Preserve the window's max size hints, but set the
685 // lower bound according to the sizer calculations.
688 wxSize
size( window
->GetVirtualSize() );
689 window
->SetVirtualSizeHints( size
.x
,
691 window
->GetMaxWidth(),
692 window
->GetMaxHeight() );
695 wxSize
wxSizer::GetMaxWindowSize( wxWindow
*window
) const
697 return window
->GetMaxSize();
700 wxSize
wxSizer::GetMinWindowSize( wxWindow
*window
)
702 wxSize
minSize( GetMinSize() );
703 wxSize
size( window
->GetSize() );
704 wxSize
client_size( window
->GetClientSize() );
706 return wxSize( minSize
.x
+size
.x
-client_size
.x
,
707 minSize
.y
+size
.y
-client_size
.y
);
710 // TODO on mac we need a function that determines how much free space this
711 // min size contains, in order to make sure that we have 20 pixels of free
712 // space around the controls
714 // Return a window size that will fit within the screens dimensions
715 wxSize
wxSizer::FitSize( wxWindow
*window
)
717 wxSize size
= GetMinWindowSize( window
);
718 wxSize sizeMax
= GetMaxWindowSize( window
);
720 // Limit the size if sizeMax != wxDefaultSize
722 if ( size
.x
> sizeMax
.x
&& sizeMax
.x
!= wxDefaultCoord
)
724 if ( size
.y
> sizeMax
.y
&& sizeMax
.y
!= wxDefaultCoord
)
730 wxSize
wxSizer::GetMaxClientSize( wxWindow
*window
) const
732 wxSize
maxSize( window
->GetMaxSize() );
734 if ( maxSize
!= wxDefaultSize
)
736 wxSize
size( window
->GetSize() );
737 wxSize
client_size( window
->GetClientSize() );
739 return wxSize( maxSize
.x
+ client_size
.x
- size
.x
,
740 maxSize
.y
+ client_size
.y
- size
.y
);
743 return wxDefaultSize
;
746 wxSize
wxSizer::GetMinClientSize( wxWindow
*WXUNUSED(window
) )
748 return GetMinSize(); // Already returns client size.
751 wxSize
wxSizer::VirtualFitSize( wxWindow
*window
)
753 wxSize size
= GetMinClientSize( window
);
754 wxSize sizeMax
= GetMaxClientSize( window
);
756 // Limit the size if sizeMax != wxDefaultSize
758 if ( size
.x
> sizeMax
.x
&& sizeMax
.x
!= wxDefaultCoord
)
760 if ( size
.y
> sizeMax
.y
&& sizeMax
.y
!= wxDefaultCoord
)
766 void wxSizer::SetDimension( int x
, int y
, int width
, int height
)
775 wxSize
wxSizer::GetMinSize()
777 wxSize
ret( CalcMin() );
778 if (ret
.x
< m_minSize
.x
) ret
.x
= m_minSize
.x
;
779 if (ret
.y
< m_minSize
.y
) ret
.y
= m_minSize
.y
;
783 void wxSizer::DoSetMinSize( int width
, int height
)
786 m_minSize
.y
= height
;
789 bool wxSizer::DoSetItemMinSize( wxWindow
*window
, int width
, int height
)
791 wxASSERT_MSG( window
, _T("SetMinSize for NULL window") );
793 // Is it our immediate child?
795 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
798 wxSizerItem
*item
= node
->GetData();
800 if (item
->GetWindow() == window
)
802 item
->SetMinSize( width
, height
);
805 node
= node
->GetNext();
808 // No? Search any subsizers we own then
810 node
= m_children
.GetFirst();
813 wxSizerItem
*item
= node
->GetData();
815 if ( item
->GetSizer() &&
816 item
->GetSizer()->DoSetItemMinSize( window
, width
, height
) )
818 // A child sizer found the requested windw, exit.
821 node
= node
->GetNext();
827 bool wxSizer::DoSetItemMinSize( wxSizer
*sizer
, int width
, int height
)
829 wxASSERT_MSG( sizer
, _T("SetMinSize for NULL sizer") );
831 // Is it our immediate child?
833 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
836 wxSizerItem
*item
= node
->GetData();
838 if (item
->GetSizer() == sizer
)
840 item
->GetSizer()->DoSetMinSize( width
, height
);
843 node
= node
->GetNext();
846 // No? Search any subsizers we own then
848 node
= m_children
.GetFirst();
851 wxSizerItem
*item
= node
->GetData();
853 if ( item
->GetSizer() &&
854 item
->GetSizer()->DoSetItemMinSize( sizer
, width
, height
) )
856 // A child found the requested sizer, exit.
859 node
= node
->GetNext();
865 bool wxSizer::DoSetItemMinSize( size_t index
, int width
, int height
)
867 wxSizerItemList::compatibility_iterator node
= m_children
.Item( index
);
869 wxCHECK_MSG( node
, false, _T("Failed to find child node") );
871 wxSizerItem
*item
= node
->GetData();
873 if (item
->GetSizer())
875 // Sizers contains the minimal size in them, if not calculated ...
876 item
->GetSizer()->DoSetMinSize( width
, height
);
880 // ... but the minimal size of spacers and windows is stored via the item
881 item
->SetMinSize( width
, height
);
887 wxSizerItem
* wxSizer::GetItem( wxWindow
*window
, bool recursive
)
889 wxASSERT_MSG( window
, _T("GetItem for NULL window") );
891 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
894 wxSizerItem
*item
= node
->GetData();
896 if (item
->GetWindow() == window
)
900 else if (recursive
&& item
->IsSizer())
902 wxSizerItem
*subitem
= item
->GetSizer()->GetItem( window
, true );
907 node
= node
->GetNext();
913 wxSizerItem
* wxSizer::GetItem( wxSizer
*sizer
, bool recursive
)
915 wxASSERT_MSG( sizer
, _T("GetItem for NULL sizer") );
917 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
920 wxSizerItem
*item
= node
->GetData();
922 if (item
->GetSizer() == sizer
)
926 else if (recursive
&& item
->IsSizer())
928 wxSizerItem
*subitem
= item
->GetSizer()->GetItem( sizer
, true );
933 node
= node
->GetNext();
939 wxSizerItem
* wxSizer::GetItem( size_t index
)
941 wxCHECK_MSG( index
< m_children
.GetCount(),
943 _T("GetItem index is out of range") );
945 return m_children
.Item( index
)->GetData();
948 bool wxSizer::Show( wxWindow
*window
, bool show
, bool recursive
)
950 wxSizerItem
*item
= GetItem( window
, recursive
);
961 bool wxSizer::Show( wxSizer
*sizer
, bool show
, bool recursive
)
963 wxSizerItem
*item
= GetItem( sizer
, recursive
);
974 bool wxSizer::Show( size_t index
, bool show
)
976 wxSizerItem
*item
= GetItem( index
);
987 void wxSizer::ShowItems( bool show
)
989 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
992 node
->GetData()->Show( show
);
993 node
= node
->GetNext();
997 bool wxSizer::IsShown( wxWindow
*window
) const
999 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
1002 wxSizerItem
*item
= node
->GetData();
1004 if (item
->GetWindow() == window
)
1006 return item
->IsShown();
1008 node
= node
->GetNext();
1011 wxFAIL_MSG( _T("IsShown failed to find sizer item") );
1016 bool wxSizer::IsShown( wxSizer
*sizer
) const
1018 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
1021 wxSizerItem
*item
= node
->GetData();
1023 if (item
->GetSizer() == sizer
)
1025 return item
->IsShown();
1027 node
= node
->GetNext();
1030 wxFAIL_MSG( _T("IsShown failed to find sizer item") );
1035 bool wxSizer::IsShown( size_t index
) const
1037 wxCHECK_MSG( index
< m_children
.GetCount(),
1039 _T("IsShown index is out of range") );
1041 return m_children
.Item( index
)->GetData()->IsShown();
1045 //---------------------------------------------------------------------------
1047 //---------------------------------------------------------------------------
1049 wxGridSizer::wxGridSizer( int rows
, int cols
, int vgap
, int hgap
)
1055 if (m_rows
== 0 && m_cols
== 0)
1059 wxGridSizer::wxGridSizer( int cols
, int vgap
, int hgap
)
1065 if (m_rows
== 0 && m_cols
== 0)
1069 int wxGridSizer::CalcRowsCols(int& nrows
, int& ncols
) const
1071 int nitems
= m_children
.GetCount();
1077 nrows
= (nitems
+ m_cols
- 1) / m_cols
;
1081 ncols
= (nitems
+ m_rows
- 1) / m_rows
;
1084 else // 0 columns, 0 rows?
1086 wxFAIL_MSG( _T("grid sizer must have either rows or columns fixed") );
1095 void wxGridSizer::RecalcSizes()
1097 int nitems
, nrows
, ncols
;
1098 if ( (nitems
= CalcRowsCols(nrows
, ncols
)) == 0 )
1101 wxSize
sz( GetSize() );
1102 wxPoint
pt( GetPosition() );
1104 int w
= (sz
.x
- (ncols
- 1) * m_hgap
) / ncols
;
1105 int h
= (sz
.y
- (nrows
- 1) * m_vgap
) / nrows
;
1108 for (int c
= 0; c
< ncols
; c
++)
1111 for (int r
= 0; r
< nrows
; r
++)
1113 int i
= r
* ncols
+ c
;
1116 wxSizerItemList::compatibility_iterator node
= m_children
.Item( i
);
1118 wxASSERT_MSG( node
, _T("Failed to find SizerItemList node") );
1120 SetItemBounds( node
->GetData(), x
, y
, w
, h
);
1128 wxSize
wxGridSizer::CalcMin()
1131 if ( CalcRowsCols(nrows
, ncols
) == 0 )
1132 return wxSize(10, 10);
1134 // Find the max width and height for any component
1138 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
1141 wxSizerItem
*item
= node
->GetData();
1142 wxSize
sz( item
->CalcMin() );
1144 w
= wxMax( w
, sz
.x
);
1145 h
= wxMax( h
, sz
.y
);
1147 node
= node
->GetNext();
1150 return wxSize( ncols
* w
+ (ncols
-1) * m_hgap
,
1151 nrows
* h
+ (nrows
-1) * m_vgap
);
1154 void wxGridSizer::SetItemBounds( wxSizerItem
*item
, int x
, int y
, int w
, int h
)
1157 wxSize
sz( item
->GetMinSizeWithBorder() );
1158 int flag
= item
->GetFlag();
1160 if ((flag
& wxEXPAND
) || (flag
& wxSHAPED
))
1166 if (flag
& wxALIGN_CENTER_HORIZONTAL
)
1168 pt
.x
= x
+ (w
- sz
.x
) / 2;
1170 else if (flag
& wxALIGN_RIGHT
)
1172 pt
.x
= x
+ (w
- sz
.x
);
1175 if (flag
& wxALIGN_CENTER_VERTICAL
)
1177 pt
.y
= y
+ (h
- sz
.y
) / 2;
1179 else if (flag
& wxALIGN_BOTTOM
)
1181 pt
.y
= y
+ (h
- sz
.y
);
1185 item
->SetDimension(pt
, sz
);
1188 //---------------------------------------------------------------------------
1190 //---------------------------------------------------------------------------
1192 wxFlexGridSizer::wxFlexGridSizer( int rows
, int cols
, int vgap
, int hgap
)
1193 : wxGridSizer( rows
, cols
, vgap
, hgap
),
1194 m_flexDirection(wxBOTH
),
1195 m_growMode(wxFLEX_GROWMODE_SPECIFIED
)
1199 wxFlexGridSizer::wxFlexGridSizer( int cols
, int vgap
, int hgap
)
1200 : wxGridSizer( cols
, vgap
, hgap
),
1201 m_flexDirection(wxBOTH
),
1202 m_growMode(wxFLEX_GROWMODE_SPECIFIED
)
1206 wxFlexGridSizer::~wxFlexGridSizer()
1210 void wxFlexGridSizer::RecalcSizes()
1212 int nitems
, nrows
, ncols
;
1213 if ( (nitems
= CalcRowsCols(nrows
, ncols
)) == 0 )
1216 wxPoint
pt( GetPosition() );
1217 wxSize
sz( GetSize() );
1219 AdjustForGrowables(sz
, m_calculatedMinSize
, nrows
, ncols
);
1221 sz
= wxSize( pt
.x
+ sz
.x
, pt
.y
+ sz
.y
);
1224 for (int c
= 0; c
< ncols
; c
++)
1227 for (int r
= 0; r
< nrows
; r
++)
1229 int i
= r
* ncols
+ c
;
1232 wxSizerItemList::compatibility_iterator node
= m_children
.Item( i
);
1234 wxASSERT_MSG( node
, _T("Failed to find node") );
1236 int w
= wxMax( 0, wxMin( m_colWidths
[c
], sz
.x
- x
) );
1237 int h
= wxMax( 0, wxMin( m_rowHeights
[r
], sz
.y
- y
) );
1239 SetItemBounds( node
->GetData(), x
, y
, w
, h
);
1241 if (m_rowHeights
[r
] != -1)
1242 y
= y
+ m_rowHeights
[r
] + m_vgap
;
1244 if (m_colWidths
[c
] != -1)
1245 x
= x
+ m_colWidths
[c
] + m_hgap
;
1249 wxSize
wxFlexGridSizer::CalcMin()
1255 // Number of rows/columns can change as items are added or removed.
1256 if ( !CalcRowsCols(nrows
, ncols
) )
1257 return wxSize(10, 10);
1259 m_rowHeights
.SetCount(nrows
);
1260 m_colWidths
.SetCount(ncols
);
1262 // We have to recalcuate the sizes in case the item minimum size has
1263 // changed since the previous layout, or the item has been hidden using
1264 // wxSizer::Show(). If all the items in a row/column are hidden, the final
1265 // dimension of the row/column will be -1, indicating that the column
1266 // itself is hidden.
1267 for( s
= m_rowHeights
.GetCount(), i
= 0; i
< s
; ++i
)
1268 m_rowHeights
[ i
] = -1;
1269 for( s
= m_colWidths
.GetCount(), i
= 0; i
< s
; ++i
)
1270 m_colWidths
[ i
] = -1;
1272 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
1277 wxSizerItem
*item
= node
->GetData();
1278 if ( item
->IsShown() )
1280 wxSize
sz( item
->CalcMin() );
1281 int row
= i
/ ncols
;
1282 int col
= i
% ncols
;
1284 m_rowHeights
[ row
] = wxMax( wxMax( 0, sz
.y
), m_rowHeights
[ row
] );
1285 m_colWidths
[ col
] = wxMax( wxMax( 0, sz
.x
), m_colWidths
[ col
] );
1288 node
= node
->GetNext();
1292 AdjustForFlexDirection();
1294 // Sum total minimum size, including gaps between rows/columns.
1295 // -1 is used as a magic number meaning empty column.
1297 for (int col
= 0; col
< ncols
; col
++)
1298 if ( m_colWidths
[ col
] != -1 )
1299 width
+= m_colWidths
[ col
] + m_hgap
;
1304 for (int row
= 0; row
< nrows
; row
++)
1305 if ( m_rowHeights
[ row
] != -1 )
1306 height
+= m_rowHeights
[ row
] + m_vgap
;
1310 m_calculatedMinSize
= wxSize( width
, height
);
1311 return m_calculatedMinSize
;
1314 void wxFlexGridSizer::AdjustForFlexDirection()
1316 // the logic in CalcMin works when we resize flexibly in both directions
1317 // but maybe this is not the case
1318 if ( m_flexDirection
!= wxBOTH
)
1320 // select the array corresponding to the direction in which we do *not*
1322 wxArrayInt
& array
= m_flexDirection
== wxVERTICAL
? m_colWidths
1325 const int count
= array
.GetCount();
1327 // find the largest value in this array
1329 for ( n
= 0; n
< count
; ++n
)
1331 if ( array
[n
] > largest
)
1335 // and now fill it with the largest value
1336 for ( n
= 0; n
< count
; ++n
)
1344 void wxFlexGridSizer::AdjustForGrowables(const wxSize
& sz
, const wxSize
& minsz
,
1345 int nrows
, int ncols
)
1347 // what to do with the rows? by default, resize them proportionally
1348 if ( sz
.y
> minsz
.y
&& ( (m_flexDirection
& wxVERTICAL
) || (m_growMode
== wxFLEX_GROWMODE_SPECIFIED
) ) )
1350 int sum_proportions
= 0;
1351 int growable_space
= 0;
1354 for (idx
= 0; idx
< m_growableRows
.GetCount(); idx
++)
1356 // Since the number of rows/columns can change as items are
1357 // inserted/deleted, we need to verify at runtime that the
1358 // requested growable rows/columns are still valid.
1359 if (m_growableRows
[idx
] >= nrows
)
1362 // If all items in a row/column are hidden, that row/column will
1363 // have a dimension of -1. This causes the row/column to be
1364 // hidden completely.
1365 if (m_rowHeights
[ m_growableRows
[idx
] ] == -1)
1367 sum_proportions
+= m_growableRowsProportions
[idx
];
1368 growable_space
+= m_rowHeights
[ m_growableRows
[idx
] ];
1374 for (idx
= 0; idx
< m_growableRows
.GetCount(); idx
++)
1376 if (m_growableRows
[idx
] >= nrows
)
1378 if (m_rowHeights
[ m_growableRows
[idx
] ] == -1)
1379 m_rowHeights
[ m_growableRows
[idx
] ] = 0;
1382 int delta
= (sz
.y
- minsz
.y
);
1383 if (sum_proportions
== 0)
1384 delta
= (delta
/num
) + m_rowHeights
[ m_growableRows
[idx
] ];
1386 delta
= ((delta
+growable_space
)*m_growableRowsProportions
[idx
]) / sum_proportions
;
1387 m_rowHeights
[ m_growableRows
[idx
] ] = delta
;
1392 else if ( (m_growMode
== wxFLEX_GROWMODE_ALL
) && (sz
.y
> minsz
.y
) )
1394 // rounding problem?
1395 for ( int row
= 0; row
< nrows
; ++row
)
1396 m_rowHeights
[ row
] = sz
.y
/ nrows
;
1399 // the same logic as above but for the columns
1400 if ( sz
.x
> minsz
.x
&& ( (m_flexDirection
& wxHORIZONTAL
) || (m_growMode
== wxFLEX_GROWMODE_SPECIFIED
) ) )
1402 int sum_proportions
= 0;
1403 int growable_space
= 0;
1406 for (idx
= 0; idx
< m_growableCols
.GetCount(); idx
++)
1408 // Since the number of rows/columns can change as items are
1409 // inserted/deleted, we need to verify at runtime that the
1410 // requested growable rows/columns are still valid.
1411 if (m_growableCols
[idx
] >= ncols
)
1414 // If all items in a row/column are hidden, that row/column will
1415 // have a dimension of -1. This causes the column to be hidden
1417 if (m_colWidths
[ m_growableCols
[idx
] ] == -1)
1419 sum_proportions
+= m_growableColsProportions
[idx
];
1420 growable_space
+= m_colWidths
[ m_growableCols
[idx
] ];
1426 for (idx
= 0; idx
< m_growableCols
.GetCount(); idx
++)
1428 if (m_growableCols
[idx
] >= ncols
)
1430 if (m_colWidths
[ m_growableCols
[idx
] ] == -1)
1431 m_colWidths
[ m_growableCols
[idx
] ] = 0;
1434 int delta
= (sz
.x
- minsz
.x
);
1435 if (sum_proportions
== 0)
1436 delta
= (delta
/num
) + m_colWidths
[ m_growableCols
[idx
] ];
1438 delta
= ((delta
+growable_space
)*m_growableColsProportions
[idx
])/sum_proportions
;
1439 m_colWidths
[ m_growableCols
[idx
] ] = delta
;
1444 else if ( (m_growMode
== wxFLEX_GROWMODE_ALL
) && (sz
.x
> minsz
.x
) )
1446 for ( int col
=0; col
< ncols
; ++col
)
1447 m_colWidths
[ col
] = sz
.x
/ ncols
;
1452 void wxFlexGridSizer::AddGrowableRow( size_t idx
, int proportion
)
1454 m_growableRows
.Add( idx
);
1455 m_growableRowsProportions
.Add( proportion
);
1458 void wxFlexGridSizer::RemoveGrowableRow( size_t idx
)
1460 m_growableRows
.Remove( idx
);
1463 void wxFlexGridSizer::AddGrowableCol( size_t idx
, int proportion
)
1465 m_growableCols
.Add( idx
);
1466 m_growableColsProportions
.Add( proportion
);
1469 void wxFlexGridSizer::RemoveGrowableCol( size_t idx
)
1471 m_growableCols
.Remove( idx
);
1474 //---------------------------------------------------------------------------
1476 //---------------------------------------------------------------------------
1478 wxBoxSizer::wxBoxSizer( int orient
)
1479 : m_orient( orient
)
1483 void wxBoxSizer::RecalcSizes()
1485 if (m_children
.GetCount() == 0)
1491 if (m_orient
== wxHORIZONTAL
)
1492 delta
= m_size
.x
- m_fixedWidth
;
1494 delta
= m_size
.y
- m_fixedHeight
;
1497 wxPoint
pt( m_position
);
1499 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
1502 wxSizerItem
*item
= node
->GetData();
1504 if (item
->IsShown())
1506 wxSize
size( item
->GetMinSizeWithBorder() );
1508 if (m_orient
== wxVERTICAL
)
1510 wxCoord height
= size
.y
;
1511 if (item
->GetProportion())
1513 // Because of at least one visible item has non-zero
1514 // proportion then m_stretchable is not zero
1515 height
= (delta
* item
->GetProportion()) / m_stretchable
;
1518 wxPoint
child_pos( pt
);
1519 wxSize
child_size( size
.x
, height
);
1521 if (item
->GetFlag() & (wxEXPAND
| wxSHAPED
))
1522 child_size
.x
= m_size
.x
;
1523 else if (item
->GetFlag() & wxALIGN_RIGHT
)
1524 child_pos
.x
+= m_size
.x
- size
.x
;
1525 else if (item
->GetFlag() & (wxCENTER
| wxALIGN_CENTER_HORIZONTAL
))
1526 // XXX wxCENTER is added for backward compatibility;
1527 // wxALIGN_CENTER should be used in new code
1528 child_pos
.x
+= (m_size
.x
- size
.x
) / 2;
1530 item
->SetDimension( child_pos
, child_size
);
1536 wxCoord width
= size
.x
;
1537 if (item
->GetProportion())
1539 // Because of at least one visible item has non-zero
1540 // proportion then m_stretchable is not zero
1541 width
= (delta
* item
->GetProportion()) / m_stretchable
;
1544 wxPoint
child_pos( pt
);
1545 wxSize
child_size( width
, size
.y
);
1547 if (item
->GetFlag() & (wxEXPAND
| wxSHAPED
))
1548 child_size
.y
= m_size
.y
;
1549 else if (item
->GetFlag() & wxALIGN_BOTTOM
)
1550 child_pos
.y
+= m_size
.y
- size
.y
;
1551 else if (item
->GetFlag() & (wxCENTER
| wxALIGN_CENTER_VERTICAL
))
1552 // XXX wxCENTER is added for backward compatibility;
1553 // wxALIGN_CENTER should be used in new code
1554 child_pos
.y
+= (m_size
.y
- size
.y
) / 2;
1556 item
->SetDimension( child_pos
, child_size
);
1562 node
= node
->GetNext();
1566 wxSize
wxBoxSizer::CalcMin()
1568 if (m_children
.GetCount() == 0)
1569 return wxSize(10,10);
1577 // precalc item minsizes and count proportions
1578 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
1581 wxSizerItem
*item
= node
->GetData();
1583 if ( item
->IsShown() )
1585 item
->CalcMin(); // result is stored in the item
1587 m_stretchable
+= item
->GetProportion();
1590 node
= node
->GetNext();
1593 // Total minimum size (width or height) of sizer
1596 node
= m_children
.GetFirst();
1599 wxSizerItem
*item
= node
->GetData();
1601 if (item
->IsShown() && item
->GetProportion() != 0)
1603 int stretch
= item
->GetProportion();
1604 wxSize
size( item
->GetMinSizeWithBorder() );
1607 // Integer division rounded up is (a + b - 1) / b
1608 // Round up needed in order to guarantee that all
1609 // all items will have size not less then their min size
1610 if (m_orient
== wxHORIZONTAL
)
1611 minSize
= ( size
.x
*m_stretchable
+ stretch
- 1)/stretch
;
1613 minSize
= ( size
.y
*m_stretchable
+ stretch
- 1)/stretch
;
1615 if (minSize
> maxMinSize
)
1616 maxMinSize
= minSize
;
1618 node
= node
->GetNext();
1621 // Calculate overall minimum size
1622 node
= m_children
.GetFirst();
1625 wxSizerItem
*item
= node
->GetData();
1627 if (item
->IsShown())
1629 wxSize
size( item
->GetMinSizeWithBorder() );
1630 if (item
->GetProportion() != 0)
1632 if (m_orient
== wxHORIZONTAL
)
1633 size
.x
= (maxMinSize
*item
->GetProportion())/m_stretchable
;
1635 size
.y
= (maxMinSize
*item
->GetProportion())/m_stretchable
;
1639 if (m_orient
== wxVERTICAL
)
1641 m_fixedHeight
+= size
.y
;
1642 m_fixedWidth
= wxMax( m_fixedWidth
, size
.x
);
1646 m_fixedWidth
+= size
.x
;
1647 m_fixedHeight
= wxMax( m_fixedHeight
, size
.y
);
1651 if (m_orient
== wxHORIZONTAL
)
1653 m_minWidth
+= size
.x
;
1654 m_minHeight
= wxMax( m_minHeight
, size
.y
);
1658 m_minHeight
+= size
.y
;
1659 m_minWidth
= wxMax( m_minWidth
, size
.x
);
1662 node
= node
->GetNext();
1665 return wxSize( m_minWidth
, m_minHeight
);
1668 //---------------------------------------------------------------------------
1670 //---------------------------------------------------------------------------
1674 wxStaticBoxSizer::wxStaticBoxSizer( wxStaticBox
*box
, int orient
)
1675 : wxBoxSizer( orient
)
1676 , m_staticBox( box
)
1678 wxASSERT_MSG( box
, wxT("wxStaticBoxSizer needs a static box") );
1681 wxStaticBoxSizer::wxStaticBoxSizer(int orient
, wxWindow
*win
, const wxString
& s
)
1682 : wxBoxSizer(orient
),
1683 m_staticBox(new wxStaticBox(win
, wxID_ANY
, s
))
1687 static void GetStaticBoxBorders( wxStaticBox
*box
,
1691 // this has to be done platform by platform as there is no way to
1692 // guess the thickness of a wxStaticBox border
1693 box
->GetBordersForSizer(borderTop
, borderOther
);
1696 void wxStaticBoxSizer::RecalcSizes()
1698 int top_border
, other_border
;
1699 GetStaticBoxBorders(m_staticBox
, &top_border
, &other_border
);
1701 m_staticBox
->SetSize( m_position
.x
, m_position
.y
, m_size
.x
, m_size
.y
);
1703 wxPoint
old_pos( m_position
);
1704 m_position
.x
+= other_border
;
1705 m_position
.y
+= top_border
;
1706 wxSize
old_size( m_size
);
1707 m_size
.x
-= 2*other_border
;
1708 m_size
.y
-= top_border
+ other_border
;
1710 wxBoxSizer::RecalcSizes();
1712 m_position
= old_pos
;
1716 wxSize
wxStaticBoxSizer::CalcMin()
1718 int top_border
, other_border
;
1719 GetStaticBoxBorders(m_staticBox
, &top_border
, &other_border
);
1721 wxSize
ret( wxBoxSizer::CalcMin() );
1722 ret
.x
+= 2*other_border
;
1723 ret
.y
+= other_border
+ top_border
;
1728 void wxStaticBoxSizer::ShowItems( bool show
)
1730 m_staticBox
->Show( show
);
1731 wxBoxSizer::ShowItems( show
);
1734 #endif // wxUSE_STATBOX
1738 wxStdDialogButtonSizer::wxStdDialogButtonSizer()
1739 : wxBoxSizer(wxHORIZONTAL
)
1741 // Vertical buttons with lots of space on either side
1742 // looks rubbish on WinCE, so let's not do this for now.
1743 // If we are going to use vertical buttons, we should
1744 // put the sizer to the right of other controls in the dialog,
1745 // and that's beyond the scope of this sizer.
1747 bool is_pda
= (wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA
);
1748 // If we have a PDA screen, put yes/no button over
1749 // all other buttons, otherwise on the left side.
1751 m_orient
= wxVERTICAL
;
1754 m_buttonAffirmative
= NULL
;
1755 m_buttonApply
= NULL
;
1756 m_buttonNegative
= NULL
;
1757 m_buttonCancel
= NULL
;
1758 m_buttonHelp
= NULL
;
1761 void wxStdDialogButtonSizer::AddButton(wxButton
*mybutton
)
1763 switch (mybutton
->GetId())
1768 m_buttonAffirmative
= mybutton
;
1771 m_buttonApply
= mybutton
;
1774 m_buttonNegative
= mybutton
;
1777 m_buttonCancel
= mybutton
;
1780 case wxID_CONTEXT_HELP
:
1781 m_buttonHelp
= mybutton
;
1788 void wxStdDialogButtonSizer::SetAffirmativeButton( wxButton
*button
)
1790 m_buttonAffirmative
= button
;
1793 void wxStdDialogButtonSizer::SetNegativeButton( wxButton
*button
)
1795 m_buttonNegative
= button
;
1798 void wxStdDialogButtonSizer::SetCancelButton( wxButton
*button
)
1800 m_buttonCancel
= button
;
1803 void wxStdDialogButtonSizer::Realize()
1806 Add(0, 0, 0, wxLEFT
, 6);
1808 Add((wxWindow
*)m_buttonHelp
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, 6);
1810 if (m_buttonNegative
){
1811 // HIG POLICE BULLETIN - destructive buttons need extra padding
1812 // 24 pixels on either side
1813 Add((wxWindow
*)m_buttonNegative
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, 12);
1816 // extra whitespace between help/negative and cancel/ok buttons
1817 Add(0, 0, 1, wxEXPAND
, 0);
1819 if (m_buttonCancel
){
1820 Add((wxWindow
*)m_buttonCancel
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, 6);
1821 // Cancel or help should be default
1822 // m_buttonCancel->SetDefaultButton();
1825 // Ugh, Mac doesn't really have apply dialogs, so I'll just
1826 // figure the best place is between Cancel and OK
1828 Add((wxWindow
*)m_buttonApply
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, 6);
1830 if (m_buttonAffirmative
){
1831 Add((wxWindow
*)m_buttonAffirmative
, 0, wxALIGN_CENTRE
| wxLEFT
, 6);
1833 if (m_buttonAffirmative
->GetId() == wxID_SAVE
){
1834 // these buttons have set labels under Mac so we should use them
1835 m_buttonAffirmative
->SetLabel(_("Save"));
1836 m_buttonNegative
->SetLabel(_("Don't Save"));
1840 // Extra space around and at the right
1842 #elif defined(__WXGTK20__)
1843 Add(0, 0, 0, wxLEFT
, 9);
1845 Add((wxWindow
*)m_buttonHelp
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, 3);
1847 // extra whitespace between help and cancel/ok buttons
1848 Add(0, 0, 1, wxEXPAND
, 0);
1850 if (m_buttonNegative
){
1851 Add((wxWindow
*)m_buttonNegative
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, 3);
1854 if (m_buttonCancel
){
1855 Add((wxWindow
*)m_buttonCancel
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, 3);
1856 // Cancel or help should be default
1857 // m_buttonCancel->SetDefaultButton();
1861 Add((wxWindow
*)m_buttonApply
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, 3);
1863 if (m_buttonAffirmative
)
1864 Add((wxWindow
*)m_buttonAffirmative
, 0, wxALIGN_CENTRE
| wxLEFT
, 6);
1866 // do the same thing for GTK1 and Windows platforms
1867 // and assume any platform not accounted for here will use
1869 // Add(0, 0, 0, wxLEFT, 5); // Not sure what this was for but it unbalances the dialog
1871 Add((wxWindow
*)m_buttonHelp
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, m_buttonHelp
->ConvertDialogToPixels(wxSize(4, 0)).x
);
1873 // extra whitespace between help and cancel/ok buttons
1874 Add(0, 0, 1, wxEXPAND
, 0);
1877 Add((wxWindow
*)m_buttonApply
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, m_buttonApply
->ConvertDialogToPixels(wxSize(4, 0)).x
);
1879 if (m_buttonAffirmative
){
1880 Add((wxWindow
*)m_buttonAffirmative
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, m_buttonAffirmative
->ConvertDialogToPixels(wxSize(4, 0)).x
);
1883 if (m_buttonNegative
){
1884 Add((wxWindow
*)m_buttonNegative
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, m_buttonNegative
->ConvertDialogToPixels(wxSize(4, 0)).x
);
1887 if (m_buttonCancel
){
1888 Add((wxWindow
*)m_buttonCancel
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, m_buttonCancel
->ConvertDialogToPixels(wxSize(4, 0)).x
);
1889 // Cancel or help should be default
1890 // m_buttonCancel->SetDefaultButton();
1896 #endif // wxUSE_BUTTON
1898 #if WXWIN_COMPATIBILITY_2_4
1900 // ----------------------------------------------------------------------------
1902 // ----------------------------------------------------------------------------
1905 IMPLEMENT_CLASS(wxBookCtrlSizer
, wxSizer
)
1907 IMPLEMENT_CLASS(wxNotebookSizer
, wxBookCtrlSizer
)
1908 #endif // wxUSE_NOTEBOOK
1909 #endif // wxUSE_BOOKCTRL
1913 wxBookCtrlSizer::wxBookCtrlSizer(wxBookCtrlBase
*bookctrl
)
1914 : m_bookctrl(bookctrl
)
1916 wxASSERT_MSG( bookctrl
, wxT("wxBookCtrlSizer needs a control") );
1919 void wxBookCtrlSizer::RecalcSizes()
1921 m_bookctrl
->SetSize( m_position
.x
, m_position
.y
, m_size
.x
, m_size
.y
);
1924 wxSize
wxBookCtrlSizer::CalcMin()
1926 wxSize sizeBorder
= m_bookctrl
->CalcSizeFromPage(wxSize(0,0));
1931 if ( m_bookctrl
->GetPageCount() == 0 )
1933 return wxSize(sizeBorder
.x
+ 10, sizeBorder
.y
+ 10);
1939 wxWindowList::compatibility_iterator
1940 node
= m_bookctrl
->GetChildren().GetFirst();
1943 wxWindow
*item
= node
->GetData();
1944 wxSizer
*itemsizer
= item
->GetSizer();
1948 wxSize
subsize( itemsizer
->CalcMin() );
1950 if (subsize
.x
> maxX
)
1952 if (subsize
.y
> maxY
)
1956 node
= node
->GetNext();
1959 return wxSize( maxX
, maxY
) + sizeBorder
;
1964 wxNotebookSizer::wxNotebookSizer(wxNotebook
*nb
)
1966 wxASSERT_MSG( nb
, wxT("wxNotebookSizer needs a control") );
1970 #endif // wxUSE_NOTEBOOOK
1971 #endif // wxUSE_BOOKCTRL
1973 #endif // WXWIN_COMPATIBILITY_2_4