1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/sizer.cpp
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 // For compilers that support precompilation, includes "wx.h".
14 #include "wx/wxprec.h"
20 #include "wx/display.h"
24 #include "wx/string.h"
28 #include "wx/settings.h"
29 #include "wx/button.h"
30 #include "wx/statbox.h"
31 #include "wx/toplevel.h"
34 #include "wx/listimpl.cpp"
37 //---------------------------------------------------------------------------
39 IMPLEMENT_CLASS(wxSizerItem
, wxObject
)
40 IMPLEMENT_CLASS(wxSizer
, wxObject
)
41 IMPLEMENT_CLASS(wxGridSizer
, wxSizer
)
42 IMPLEMENT_CLASS(wxFlexGridSizer
, wxGridSizer
)
43 IMPLEMENT_CLASS(wxBoxSizer
, wxSizer
)
45 IMPLEMENT_CLASS(wxStaticBoxSizer
, wxBoxSizer
)
48 IMPLEMENT_CLASS(wxStdDialogButtonSizer
, wxBoxSizer
)
51 WX_DEFINE_EXPORTED_LIST( wxSizerItemList
)
86 // ----------------------------------------------------------------------------
88 // ----------------------------------------------------------------------------
90 void wxSizerItem::Init(const wxSizerFlags
& flags
)
94 m_proportion
= flags
.GetProportion();
95 m_flag
= flags
.GetFlags();
96 m_border
= flags
.GetBorderInPixels();
99 wxSizerItem::wxSizerItem()
109 void wxSizerItem::DoSetWindow(wxWindow
*window
)
111 wxCHECK_RET( window
, _T("NULL window in wxSizerItem::SetWindow()") );
113 m_kind
= Item_Window
;
116 // window doesn't become smaller than its initial size, whatever happens
117 m_minSize
= window
->GetSize();
119 if ( m_flag
& wxFIXED_MINSIZE
)
120 window
->SetMinSize(m_minSize
);
122 // aspect ratio calculated from initial size
126 wxSizerItem::wxSizerItem(wxWindow
*window
,
132 m_proportion(proportion
),
141 void wxSizerItem::DoSetSizer(wxSizer
*sizer
)
147 wxSizerItem::wxSizerItem(wxSizer
*sizer
,
154 m_proportion(proportion
),
162 // m_minSize is set later
166 void wxSizerItem::DoSetSpacer(const wxSize
& size
)
168 m_kind
= Item_Spacer
;
169 m_spacer
= new wxSizerSpacer(size
);
174 wxSizerItem::wxSizerItem(int width
,
182 m_minSize(width
, height
), // minimal size is the initial size
183 m_proportion(proportion
),
188 DoSetSpacer(wxSize(width
, height
));
191 wxSizerItem::~wxSizerItem()
197 void wxSizerItem::Free()
205 m_window
->SetContainingSizer(NULL
);
218 wxFAIL_MSG( _T("unexpected wxSizerItem::m_kind") );
224 wxSize
wxSizerItem::GetSpacer() const
227 if ( m_kind
== Item_Spacer
)
228 size
= m_spacer
->GetSize();
234 wxSize
wxSizerItem::GetSize() const
243 ret
= m_window
->GetSize();
247 ret
= m_sizer
->GetSize();
251 ret
= m_spacer
->GetSize();
256 wxFAIL_MSG( _T("unexpected wxSizerItem::m_kind") );
263 if (m_flag
& wxNORTH
)
265 if (m_flag
& wxSOUTH
)
271 wxSize
wxSizerItem::CalcMin()
275 m_minSize
= m_sizer
->GetMinSize();
277 // if we have to preserve aspect ratio _AND_ this is
278 // the first-time calculation, consider ret to be initial size
279 if ( (m_flag
& wxSHAPED
) && wxIsNullDouble(m_ratio
) )
282 else if ( IsWindow() )
284 // Since the size of the window may change during runtime, we
285 // should use the current minimal/best size.
286 m_minSize
= m_window
->GetEffectiveMinSize();
289 return GetMinSizeWithBorder();
292 wxSize
wxSizerItem::GetMinSizeWithBorder() const
294 wxSize ret
= m_minSize
;
300 if (m_flag
& wxNORTH
)
302 if (m_flag
& wxSOUTH
)
309 void wxSizerItem::SetDimension( const wxPoint
& pos_
, const wxSize
& size_
)
313 if (m_flag
& wxSHAPED
)
315 // adjust aspect ratio
316 int rwidth
= (int) (size
.y
* m_ratio
);
320 int rheight
= (int) (size
.x
/ m_ratio
);
321 // add vertical space
322 if (m_flag
& wxALIGN_CENTER_VERTICAL
)
323 pos
.y
+= (size
.y
- rheight
) / 2;
324 else if (m_flag
& wxALIGN_BOTTOM
)
325 pos
.y
+= (size
.y
- rheight
);
326 // use reduced dimensions
329 else if (rwidth
< size
.x
)
331 // add horizontal space
332 if (m_flag
& wxALIGN_CENTER_HORIZONTAL
)
333 pos
.x
+= (size
.x
- rwidth
) / 2;
334 else if (m_flag
& wxALIGN_RIGHT
)
335 pos
.x
+= (size
.x
- rwidth
);
340 // This is what GetPosition() returns. Since we calculate
341 // borders afterwards, GetPosition() will be the left/top
342 // corner of the surrounding border.
354 if (m_flag
& wxNORTH
)
359 if (m_flag
& wxSOUTH
)
369 m_rect
= wxRect(pos
, size
);
374 wxFAIL_MSG( _T("can't set size of uninitialized sizer item") );
378 m_window
->SetSize(pos
.x
, pos
.y
, size
.x
, size
.y
,
379 wxSIZE_ALLOW_MINUS_ONE
);
383 m_sizer
->SetDimension(pos
.x
, pos
.y
, size
.x
, size
.y
);
387 m_spacer
->SetSize(size
);
392 wxFAIL_MSG( _T("unexpected wxSizerItem::m_kind") );
396 void wxSizerItem::DeleteWindows()
405 //We are deleting the window from this sizer - normally
406 //the window destroys the sizer associated with it,
407 //which might destroy this, which we don't want
408 m_window
->SetContainingSizer(NULL
);
410 //Putting this after the switch will result in a spacer
411 //not being deleted properly on destruction
416 m_sizer
->DeleteWindows();
421 wxFAIL_MSG( _T("unexpected wxSizerItem::m_kind") );
426 void wxSizerItem::Show( bool show
)
431 wxFAIL_MSG( _T("can't show uninitialized sizer item") );
435 m_window
->Show(show
);
443 m_spacer
->Show(show
);
448 wxFAIL_MSG( _T("unexpected wxSizerItem::m_kind") );
452 bool wxSizerItem::IsShown() const
457 // we may be called from CalcMin(), just return false so that we're
462 return m_window
->IsShown();
465 // arbitrarily decide that if at least one of our elements is
466 // shown, so are we (this arbitrariness is the reason for
467 // deprecating this function)
469 // Some apps (such as dialog editors) depend on an empty sizer still
470 // being laid out correctly and reporting the correct size and position.
471 if (m_sizer
->GetChildren().GetCount() == 0)
474 for ( wxSizerItemList::compatibility_iterator
475 node
= m_sizer
->GetChildren().GetFirst();
477 node
= node
->GetNext() )
479 if ( node
->GetData()->IsShown() )
486 return m_spacer
->IsShown();
490 wxFAIL_MSG( _T("unexpected wxSizerItem::m_kind") );
496 #if WXWIN_COMPATIBILITY_2_6
497 void wxSizerItem::SetOption( int option
)
499 SetProportion( option
);
502 int wxSizerItem::GetOption() const
504 return GetProportion();
506 #endif // WXWIN_COMPATIBILITY_2_6
509 //---------------------------------------------------------------------------
511 //---------------------------------------------------------------------------
515 WX_CLEAR_LIST(wxSizerItemList
, m_children
);
518 wxSizerItem
* wxSizer::Insert( size_t index
, wxSizerItem
*item
)
520 m_children
.Insert( index
, item
);
522 if ( item
->GetWindow() )
523 item
->GetWindow()->SetContainingSizer( this );
525 if ( item
->GetSizer() )
526 item
->GetSizer()->SetContainingWindow( m_containingWindow
);
531 void wxSizer::SetContainingWindow(wxWindow
*win
)
533 if ( win
== m_containingWindow
)
536 m_containingWindow
= win
;
538 // set the same window for all nested sizers as well, they also are in the
540 for ( wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
542 node
= node
->GetNext() )
544 wxSizerItem
*const item
= node
->GetData();
545 wxSizer
*const sizer
= item
->GetSizer();
549 sizer
->SetContainingWindow(win
);
554 #if WXWIN_COMPATIBILITY_2_6
555 bool wxSizer::Remove( wxWindow
*window
)
557 return Detach( window
);
559 #endif // WXWIN_COMPATIBILITY_2_6
561 bool wxSizer::Remove( wxSizer
*sizer
)
563 wxASSERT_MSG( sizer
, _T("Removing NULL sizer") );
565 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
568 wxSizerItem
*item
= node
->GetData();
570 if (item
->GetSizer() == sizer
)
573 m_children
.Erase( node
);
577 node
= node
->GetNext();
583 bool wxSizer::Remove( int index
)
585 wxCHECK_MSG( index
>= 0 && (size_t)index
< m_children
.GetCount(),
587 _T("Remove 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 delete node
->GetData();
594 m_children
.Erase( node
);
599 bool wxSizer::Detach( wxSizer
*sizer
)
601 wxASSERT_MSG( sizer
, _T("Detaching NULL sizer") );
603 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
606 wxSizerItem
*item
= node
->GetData();
608 if (item
->GetSizer() == sizer
)
612 m_children
.Erase( node
);
615 node
= node
->GetNext();
621 bool wxSizer::Detach( wxWindow
*window
)
623 wxASSERT_MSG( window
, _T("Detaching NULL window") );
625 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
628 wxSizerItem
*item
= node
->GetData();
630 if (item
->GetWindow() == window
)
633 m_children
.Erase( node
);
636 node
= node
->GetNext();
642 bool wxSizer::Detach( int index
)
644 wxCHECK_MSG( index
>= 0 && (size_t)index
< m_children
.GetCount(),
646 _T("Detach index is out of range") );
648 wxSizerItemList::compatibility_iterator node
= m_children
.Item( index
);
650 wxCHECK_MSG( node
, false, _T("Failed to find child node") );
652 wxSizerItem
*item
= node
->GetData();
654 if ( item
->IsSizer() )
658 m_children
.Erase( node
);
662 bool wxSizer::Replace( wxWindow
*oldwin
, wxWindow
*newwin
, bool recursive
)
664 wxASSERT_MSG( oldwin
, _T("Replacing NULL window") );
665 wxASSERT_MSG( newwin
, _T("Replacing with NULL window") );
667 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
670 wxSizerItem
*item
= node
->GetData();
672 if (item
->GetWindow() == oldwin
)
674 item
->AssignWindow(newwin
);
675 newwin
->SetContainingSizer( this );
678 else if (recursive
&& item
->IsSizer())
680 if (item
->GetSizer()->Replace( oldwin
, newwin
, true ))
684 node
= node
->GetNext();
690 bool wxSizer::Replace( wxSizer
*oldsz
, wxSizer
*newsz
, bool recursive
)
692 wxASSERT_MSG( oldsz
, _T("Replacing NULL sizer") );
693 wxASSERT_MSG( newsz
, _T("Replacing with NULL sizer") );
695 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
698 wxSizerItem
*item
= node
->GetData();
700 if (item
->GetSizer() == oldsz
)
702 item
->AssignSizer(newsz
);
705 else if (recursive
&& item
->IsSizer())
707 if (item
->GetSizer()->Replace( oldsz
, newsz
, true ))
711 node
= node
->GetNext();
717 bool wxSizer::Replace( size_t old
, wxSizerItem
*newitem
)
719 wxCHECK_MSG( old
< m_children
.GetCount(), false, _T("Replace index is out of range") );
720 wxASSERT_MSG( newitem
, _T("Replacing with NULL item") );
722 wxSizerItemList::compatibility_iterator node
= m_children
.Item( old
);
724 wxCHECK_MSG( node
, false, _T("Failed to find child node") );
726 wxSizerItem
*item
= node
->GetData();
727 node
->SetData(newitem
);
733 void wxSizer::Clear( bool delete_windows
)
735 // First clear the ContainingSizer pointers
736 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
739 wxSizerItem
*item
= node
->GetData();
741 if (item
->IsWindow())
742 item
->GetWindow()->SetContainingSizer( NULL
);
743 node
= node
->GetNext();
746 // Destroy the windows if needed
750 // Now empty the list
751 WX_CLEAR_LIST(wxSizerItemList
, m_children
);
754 void wxSizer::DeleteWindows()
756 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
759 wxSizerItem
*item
= node
->GetData();
761 item
->DeleteWindows();
762 node
= node
->GetNext();
766 wxSize
wxSizer::Fit( wxWindow
*window
)
768 // take the min size by default and limit it by max size
769 wxSize size
= GetMinWindowSize(window
);
770 wxSize sizeMax
= GetMaxWindowSize(window
);
772 wxTopLevelWindow
*tlw
= wxDynamicCast(window
, wxTopLevelWindow
);
775 // hack for small screen devices where TLWs are always full screen
776 if ( tlw
->IsAlwaysMaximized() )
778 size
= tlw
->GetSize();
780 else // normal situation
782 // limit the window to the size of the display it is on
783 int disp
= wxDisplay::GetFromWindow(window
);
784 if ( disp
== wxNOT_FOUND
)
786 // or, if we don't know which one it is, of the main one
790 sizeMax
= wxDisplay(disp
).GetClientArea().GetSize();
794 if ( sizeMax
.x
!= wxDefaultCoord
&& size
.x
> sizeMax
.x
)
796 if ( sizeMax
.y
!= wxDefaultCoord
&& size
.y
> sizeMax
.y
)
800 window
->SetSize( size
);
805 void wxSizer::FitInside( wxWindow
*window
)
808 if (window
->IsTopLevel())
809 size
= VirtualFitSize( window
);
811 size
= GetMinClientSize( window
);
813 window
->SetVirtualSize( size
);
816 void wxSizer::Layout()
818 // (re)calculates minimums needed for each item and other preparations
822 // Applies the layout and repositions/resizes the items
826 void wxSizer::SetSizeHints( wxWindow
*window
)
828 // Preserve the window's max size hints, but set the
829 // lower bound according to the sizer calculations.
831 wxSize size
= Fit( window
);
833 window
->SetSizeHints( size
.x
,
835 window
->GetMaxWidth(),
836 window
->GetMaxHeight() );
839 void wxSizer::SetVirtualSizeHints( wxWindow
*window
)
841 // Preserve the window's max size hints, but set the
842 // lower bound according to the sizer calculations.
845 wxSize
size( window
->GetVirtualSize() );
846 window
->SetVirtualSizeHints( size
.x
,
848 window
->GetMaxWidth(),
849 window
->GetMaxHeight() );
852 wxSize
wxSizer::GetMaxWindowSize( wxWindow
*window
) const
854 return window
->GetMaxSize();
857 wxSize
wxSizer::GetMinWindowSize( wxWindow
*window
)
859 wxSize
minSize( GetMinSize() );
860 wxSize
size( window
->GetSize() );
861 wxSize
client_size( window
->GetClientSize() );
863 return wxSize( minSize
.x
+size
.x
-client_size
.x
,
864 minSize
.y
+size
.y
-client_size
.y
);
867 // TODO on mac we need a function that determines how much free space this
868 // min size contains, in order to make sure that we have 20 pixels of free
869 // space around the controls
870 wxSize
wxSizer::GetMaxClientSize( wxWindow
*window
) const
872 wxSize
maxSize( window
->GetMaxSize() );
874 if ( maxSize
!= wxDefaultSize
)
876 wxSize
size( window
->GetSize() );
877 wxSize
client_size( window
->GetClientSize() );
879 return wxSize( maxSize
.x
+ client_size
.x
- size
.x
,
880 maxSize
.y
+ client_size
.y
- size
.y
);
883 return wxDefaultSize
;
886 wxSize
wxSizer::GetMinClientSize( wxWindow
*WXUNUSED(window
) )
888 return GetMinSize(); // Already returns client size.
891 wxSize
wxSizer::VirtualFitSize( wxWindow
*window
)
893 wxSize size
= GetMinClientSize( window
);
894 wxSize sizeMax
= GetMaxClientSize( window
);
896 // Limit the size if sizeMax != wxDefaultSize
898 if ( size
.x
> sizeMax
.x
&& sizeMax
.x
!= wxDefaultCoord
)
900 if ( size
.y
> sizeMax
.y
&& sizeMax
.y
!= wxDefaultCoord
)
906 void wxSizer::SetDimension( int x
, int y
, int width
, int height
)
915 wxSize
wxSizer::GetMinSize()
917 wxSize
ret( CalcMin() );
918 if (ret
.x
< m_minSize
.x
) ret
.x
= m_minSize
.x
;
919 if (ret
.y
< m_minSize
.y
) ret
.y
= m_minSize
.y
;
923 void wxSizer::DoSetMinSize( int width
, int height
)
926 m_minSize
.y
= height
;
929 bool wxSizer::DoSetItemMinSize( wxWindow
*window
, int width
, int height
)
931 wxASSERT_MSG( window
, _T("SetMinSize for NULL window") );
933 // Is it our immediate child?
935 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
938 wxSizerItem
*item
= node
->GetData();
940 if (item
->GetWindow() == window
)
942 item
->SetMinSize( width
, height
);
945 node
= node
->GetNext();
948 // No? Search any subsizers we own then
950 node
= m_children
.GetFirst();
953 wxSizerItem
*item
= node
->GetData();
955 if ( item
->GetSizer() &&
956 item
->GetSizer()->DoSetItemMinSize( window
, width
, height
) )
958 // A child sizer found the requested windw, exit.
961 node
= node
->GetNext();
967 bool wxSizer::DoSetItemMinSize( wxSizer
*sizer
, int width
, int height
)
969 wxASSERT_MSG( sizer
, _T("SetMinSize for NULL sizer") );
971 // Is it our immediate child?
973 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
976 wxSizerItem
*item
= node
->GetData();
978 if (item
->GetSizer() == sizer
)
980 item
->GetSizer()->DoSetMinSize( width
, height
);
983 node
= node
->GetNext();
986 // No? Search any subsizers we own then
988 node
= m_children
.GetFirst();
991 wxSizerItem
*item
= node
->GetData();
993 if ( item
->GetSizer() &&
994 item
->GetSizer()->DoSetItemMinSize( sizer
, width
, height
) )
996 // A child found the requested sizer, exit.
999 node
= node
->GetNext();
1005 bool wxSizer::DoSetItemMinSize( size_t index
, int width
, int height
)
1007 wxSizerItemList::compatibility_iterator node
= m_children
.Item( index
);
1009 wxCHECK_MSG( node
, false, _T("Failed to find child node") );
1011 wxSizerItem
*item
= node
->GetData();
1013 if (item
->GetSizer())
1015 // Sizers contains the minimal size in them, if not calculated ...
1016 item
->GetSizer()->DoSetMinSize( width
, height
);
1020 // ... but the minimal size of spacers and windows is stored via the item
1021 item
->SetMinSize( width
, height
);
1027 wxSizerItem
* wxSizer::GetItem( wxWindow
*window
, bool recursive
)
1029 wxASSERT_MSG( window
, _T("GetItem for NULL window") );
1031 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
1034 wxSizerItem
*item
= node
->GetData();
1036 if (item
->GetWindow() == window
)
1040 else if (recursive
&& item
->IsSizer())
1042 wxSizerItem
*subitem
= item
->GetSizer()->GetItem( window
, true );
1047 node
= node
->GetNext();
1053 wxSizerItem
* wxSizer::GetItem( wxSizer
*sizer
, bool recursive
)
1055 wxASSERT_MSG( sizer
, _T("GetItem for NULL sizer") );
1057 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
1060 wxSizerItem
*item
= node
->GetData();
1062 if (item
->GetSizer() == sizer
)
1066 else if (recursive
&& item
->IsSizer())
1068 wxSizerItem
*subitem
= item
->GetSizer()->GetItem( sizer
, true );
1073 node
= node
->GetNext();
1079 wxSizerItem
* wxSizer::GetItem( size_t index
)
1081 wxCHECK_MSG( index
< m_children
.GetCount(),
1083 _T("GetItem index is out of range") );
1085 return m_children
.Item( index
)->GetData();
1088 bool wxSizer::Show( wxWindow
*window
, bool show
, bool recursive
)
1090 wxSizerItem
*item
= GetItem( window
, recursive
);
1101 bool wxSizer::Show( wxSizer
*sizer
, bool show
, bool recursive
)
1103 wxSizerItem
*item
= GetItem( sizer
, recursive
);
1114 bool wxSizer::Show( size_t index
, bool show
)
1116 wxSizerItem
*item
= GetItem( index
);
1127 void wxSizer::ShowItems( bool show
)
1129 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
1132 node
->GetData()->Show( show
);
1133 node
= node
->GetNext();
1137 bool wxSizer::IsShown( wxWindow
*window
) const
1139 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
1142 wxSizerItem
*item
= node
->GetData();
1144 if (item
->GetWindow() == window
)
1146 return item
->IsShown();
1148 node
= node
->GetNext();
1151 wxFAIL_MSG( _T("IsShown failed to find sizer item") );
1156 bool wxSizer::IsShown( wxSizer
*sizer
) const
1158 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
1161 wxSizerItem
*item
= node
->GetData();
1163 if (item
->GetSizer() == sizer
)
1165 return item
->IsShown();
1167 node
= node
->GetNext();
1170 wxFAIL_MSG( _T("IsShown failed to find sizer item") );
1175 bool wxSizer::IsShown( size_t index
) const
1177 wxCHECK_MSG( index
< m_children
.GetCount(),
1179 _T("IsShown index is out of range") );
1181 return m_children
.Item( index
)->GetData()->IsShown();
1185 //---------------------------------------------------------------------------
1187 //---------------------------------------------------------------------------
1189 wxGridSizer::wxGridSizer( int rows
, int cols
, int vgap
, int hgap
)
1190 : m_rows( ( cols
== 0 && rows
== 0 ) ? 1 : rows
)
1197 wxGridSizer::wxGridSizer( int cols
, int vgap
, int hgap
)
1198 : m_rows( cols
== 0 ? 1 : 0 )
1205 int wxGridSizer::CalcRowsCols(int& nrows
, int& ncols
) const
1207 int nitems
= m_children
.GetCount();
1213 nrows
= (nitems
+ m_cols
- 1) / m_cols
;
1217 ncols
= (nitems
+ m_rows
- 1) / m_rows
;
1220 else // 0 columns, 0 rows?
1222 wxFAIL_MSG( _T("grid sizer must have either rows or columns fixed") );
1231 void wxGridSizer::RecalcSizes()
1233 int nitems
, nrows
, ncols
;
1234 if ( (nitems
= CalcRowsCols(nrows
, ncols
)) == 0 )
1237 wxSize
sz( GetSize() );
1238 wxPoint
pt( GetPosition() );
1240 int w
= (sz
.x
- (ncols
- 1) * m_hgap
) / ncols
;
1241 int h
= (sz
.y
- (nrows
- 1) * m_vgap
) / nrows
;
1244 for (int c
= 0; c
< ncols
; c
++)
1247 for (int r
= 0; r
< nrows
; r
++)
1249 int i
= r
* ncols
+ c
;
1252 wxSizerItemList::compatibility_iterator node
= m_children
.Item( i
);
1254 wxASSERT_MSG( node
, _T("Failed to find SizerItemList node") );
1256 SetItemBounds( node
->GetData(), x
, y
, w
, h
);
1264 wxSize
wxGridSizer::CalcMin()
1267 if ( CalcRowsCols(nrows
, ncols
) == 0 )
1270 // Find the max width and height for any component
1274 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
1277 wxSizerItem
*item
= node
->GetData();
1278 wxSize
sz( item
->CalcMin() );
1280 w
= wxMax( w
, sz
.x
);
1281 h
= wxMax( h
, sz
.y
);
1283 node
= node
->GetNext();
1286 return wxSize( ncols
* w
+ (ncols
-1) * m_hgap
,
1287 nrows
* h
+ (nrows
-1) * m_vgap
);
1290 void wxGridSizer::SetItemBounds( wxSizerItem
*item
, int x
, int y
, int w
, int h
)
1293 wxSize
sz( item
->GetMinSizeWithBorder() );
1294 int flag
= item
->GetFlag();
1296 if ((flag
& wxEXPAND
) || (flag
& wxSHAPED
))
1302 if (flag
& wxALIGN_CENTER_HORIZONTAL
)
1304 pt
.x
= x
+ (w
- sz
.x
) / 2;
1306 else if (flag
& wxALIGN_RIGHT
)
1308 pt
.x
= x
+ (w
- sz
.x
);
1311 if (flag
& wxALIGN_CENTER_VERTICAL
)
1313 pt
.y
= y
+ (h
- sz
.y
) / 2;
1315 else if (flag
& wxALIGN_BOTTOM
)
1317 pt
.y
= y
+ (h
- sz
.y
);
1321 item
->SetDimension(pt
, sz
);
1324 //---------------------------------------------------------------------------
1326 //---------------------------------------------------------------------------
1328 wxFlexGridSizer::wxFlexGridSizer( int rows
, int cols
, int vgap
, int hgap
)
1329 : wxGridSizer( rows
, cols
, vgap
, hgap
),
1330 m_flexDirection(wxBOTH
),
1331 m_growMode(wxFLEX_GROWMODE_SPECIFIED
)
1335 wxFlexGridSizer::wxFlexGridSizer( int cols
, int vgap
, int hgap
)
1336 : wxGridSizer( cols
, vgap
, hgap
),
1337 m_flexDirection(wxBOTH
),
1338 m_growMode(wxFLEX_GROWMODE_SPECIFIED
)
1342 wxFlexGridSizer::~wxFlexGridSizer()
1346 void wxFlexGridSizer::RecalcSizes()
1348 int nitems
, nrows
, ncols
;
1349 if ( (nitems
= CalcRowsCols(nrows
, ncols
)) == 0 )
1352 const wxPoint
pt(GetPosition());
1353 const wxSize
sz(GetSize());
1355 AdjustForGrowables(sz
);
1357 wxSizerItemList::const_iterator i
= m_children
.begin();
1358 const wxSizerItemList::const_iterator end
= m_children
.end();
1361 for ( int r
= 0; r
< nrows
; r
++ )
1363 if ( m_rowHeights
[r
] == -1 )
1365 // this row is entirely hidden, skip it
1366 for ( int c
= 0; c
< ncols
; c
++ )
1377 const int hrow
= m_rowHeights
[r
];
1378 int h
= sz
.y
- y
; // max remaining height, don't overflow it
1383 for ( int c
= 0; c
< ncols
&& i
!= end
; c
++, ++i
)
1385 const int wcol
= m_colWidths
[c
];
1390 int w
= sz
.x
- x
; // max possible value, ensure we don't overflow
1394 SetItemBounds(*i
, pt
.x
+ x
, pt
.y
+ y
, w
, h
);
1406 // helper function used in CalcMin() to sum up the sizes of non-hidden items
1407 static int SumArraySizes(const wxArrayInt
& sizes
, int gap
)
1409 // Sum total minimum size, including gaps between rows/columns.
1410 // -1 is used as a magic number meaning empty row/column.
1413 const size_t count
= sizes
.size();
1414 for ( size_t n
= 0; n
< count
; n
++ )
1416 if ( sizes
[n
] != -1 )
1419 total
+= gap
; // separate from the previous column
1428 wxSize
wxFlexGridSizer::CalcMin()
1433 // Number of rows/columns can change as items are added or removed.
1434 if ( !CalcRowsCols(nrows
, ncols
) )
1438 // We have to recalculate the sizes in case the item minimum size has
1439 // changed since the previous layout, or the item has been hidden using
1440 // wxSizer::Show(). If all the items in a row/column are hidden, the final
1441 // dimension of the row/column will be -1, indicating that the column
1442 // itself is hidden.
1443 m_rowHeights
.assign(nrows
, -1);
1444 m_colWidths
.assign(ncols
, -1);
1446 // n is the index of the item in left-to-right top-to-bottom order
1448 for ( wxSizerItemList::iterator i
= m_children
.begin();
1449 i
!= m_children
.end();
1452 wxSizerItem
* const item
= *i
;
1453 if ( item
->IsShown() )
1455 const wxSize
sz(item
->CalcMin());
1457 const int row
= n
/ ncols
;
1458 const int col
= n
% ncols
;
1460 if ( sz
.y
> m_rowHeights
[row
] )
1461 m_rowHeights
[row
] = sz
.y
;
1462 if ( sz
.x
> m_colWidths
[col
] )
1463 m_colWidths
[col
] = sz
.x
;
1467 AdjustForFlexDirection();
1469 m_calculatedMinSize
= wxSize(SumArraySizes(m_colWidths
, m_hgap
),
1470 SumArraySizes(m_rowHeights
, m_vgap
));
1472 return m_calculatedMinSize
;
1475 void wxFlexGridSizer::AdjustForFlexDirection()
1477 // the logic in CalcMin works when we resize flexibly in both directions
1478 // but maybe this is not the case
1479 if ( m_flexDirection
!= wxBOTH
)
1481 // select the array corresponding to the direction in which we do *not*
1483 wxArrayInt
& array
= m_flexDirection
== wxVERTICAL
? m_colWidths
1486 const size_t count
= array
.GetCount();
1488 // find the largest value in this array
1492 for ( n
= 0; n
< count
; ++n
)
1494 if ( array
[n
] > largest
)
1498 // and now fill it with the largest value
1499 for ( n
= 0; n
< count
; ++n
)
1501 // don't touch hidden rows
1502 if ( array
[n
] != -1 )
1508 // helper of AdjustForGrowables() which is called for rows/columns separately
1511 // delta: the extra space, we do nothing unless it's positive
1512 // growable: indices or growable rows/cols in sizes array
1513 // sizes: the height/widths of rows/cols to adjust
1514 // proportions: proportions of the growable rows/cols or NULL if they all
1515 // should be assumed to have proportion of 1
1517 DoAdjustForGrowables(int delta
,
1518 const wxArrayInt
& growable
,
1520 const wxArrayInt
*proportions
)
1525 // total sum of proportions of all non-hidden rows
1526 int sum_proportions
= 0;
1528 // number of currently shown growable rows
1531 const int max_idx
= sizes
.size();
1533 const size_t count
= growable
.size();
1535 for ( idx
= 0; idx
< count
; idx
++ )
1537 // Since the number of rows/columns can change as items are
1538 // inserted/deleted, we need to verify at runtime that the
1539 // requested growable rows/columns are still valid.
1540 if ( growable
[idx
] >= max_idx
)
1543 // If all items in a row/column are hidden, that row/column will
1544 // have a dimension of -1. This causes the row/column to be
1545 // hidden completely.
1546 if ( sizes
[growable
[idx
]] == -1 )
1550 sum_proportions
+= (*proportions
)[idx
];
1558 // the remaining extra free space, adjusted during each iteration
1559 for ( idx
= 0; idx
< count
; idx
++ )
1561 if ( growable
[idx
] >= max_idx
)
1564 if ( sizes
[ growable
[idx
] ] == -1 )
1568 if ( sum_proportions
== 0 )
1570 // no growable rows -- divide extra space evenly among all
1571 cur_delta
= delta
/num
;
1574 else // allocate extra space proportionally
1576 const int cur_prop
= (*proportions
)[idx
];
1577 cur_delta
= (delta
*cur_prop
)/sum_proportions
;
1578 sum_proportions
-= cur_prop
;
1581 sizes
[growable
[idx
]] += cur_delta
;
1586 void wxFlexGridSizer::AdjustForGrowables(const wxSize
& sz
)
1588 if ( (m_flexDirection
& wxVERTICAL
) || (m_growMode
!= wxFLEX_GROWMODE_NONE
) )
1590 // pass NULL instead of proportions if the grow mode is ALL as we
1591 // should treat all rows as having proportion of 1 then
1592 DoAdjustForGrowables
1594 sz
.y
- m_calculatedMinSize
.y
,
1597 m_growMode
== wxFLEX_GROWMODE_SPECIFIED
? &m_growableRowsProportions
1602 if ( (m_flexDirection
& wxHORIZONTAL
) || (m_growMode
!= wxFLEX_GROWMODE_NONE
) )
1604 DoAdjustForGrowables
1606 sz
.x
- m_calculatedMinSize
.x
,
1609 m_growMode
== wxFLEX_GROWMODE_SPECIFIED
? &m_growableColsProportions
1616 void wxFlexGridSizer::AddGrowableRow( size_t idx
, int proportion
)
1618 m_growableRows
.Add( idx
);
1619 m_growableRowsProportions
.Add( proportion
);
1622 void wxFlexGridSizer::AddGrowableCol( size_t idx
, int proportion
)
1624 m_growableCols
.Add( idx
);
1625 m_growableColsProportions
.Add( proportion
);
1628 // helper function for RemoveGrowableCol/Row()
1630 DoRemoveFromArrays(size_t idx
, wxArrayInt
& items
, wxArrayInt
& proportions
)
1632 const size_t count
= items
.size();
1633 for ( size_t n
= 0; n
< count
; n
++ )
1635 if ( (size_t)items
[n
] == idx
)
1638 proportions
.RemoveAt(n
);
1643 wxFAIL_MSG( _T("column/row is already not growable") );
1646 void wxFlexGridSizer::RemoveGrowableCol( size_t idx
)
1648 DoRemoveFromArrays(idx
, m_growableCols
, m_growableColsProportions
);
1651 void wxFlexGridSizer::RemoveGrowableRow( size_t idx
)
1653 DoRemoveFromArrays(idx
, m_growableRows
, m_growableRowsProportions
);
1656 //---------------------------------------------------------------------------
1658 //---------------------------------------------------------------------------
1660 void wxBoxSizer::RecalcSizes()
1662 if ( m_children
.empty() )
1665 // the amount of free space which we should redistribute among the
1666 // stretchable items (i.e. those with non zero proportion)
1667 int delta
= SizeInMajorDir(m_size
) - SizeInMajorDir(m_minSize
);
1669 // the position at which we put the next child
1670 wxPoint
pt(m_position
);
1672 const wxCoord totalMinorSize
= SizeInMinorDir(m_size
);
1674 int totalProportion
= m_totalProportion
;
1675 for ( wxSizerItemList::const_iterator i
= m_children
.begin();
1676 i
!= m_children
.end();
1679 wxSizerItem
* const item
= *i
;
1681 if ( !item
->IsShown() )
1684 const wxSize
sizeThis(item
->GetMinSizeWithBorder());
1687 // adjust the size in the major direction using the proportion
1688 wxCoord majorSize
= SizeInMajorDir(sizeThis
);
1689 const int propItem
= item
->GetProportion();
1692 const int deltaItem
= (delta
* propItem
) / totalProportion
;
1694 majorSize
+= deltaItem
;
1697 totalProportion
-= propItem
;
1701 // apply the alignment in the minor direction
1702 wxPoint
posChild(pt
);
1704 wxCoord minorSize
= SizeInMinorDir(sizeThis
);
1705 const int flag
= item
->GetFlag();
1706 if ( flag
& (wxEXPAND
| wxSHAPED
) )
1708 minorSize
= totalMinorSize
;
1710 else if ( flag
& (IsVertical() ? wxALIGN_RIGHT
: wxALIGN_BOTTOM
) )
1712 PosInMinorDir(posChild
) += totalMinorSize
- minorSize
;
1714 // NB: wxCENTRE is used here only for backwards compatibility,
1715 // wxALIGN_CENTRE should be used in new code
1716 else if ( flag
& (wxCENTER
| wxALIGN_CENTRE
) )
1718 PosInMinorDir(posChild
) += (totalMinorSize
- minorSize
) / 2;
1722 // apply RTL adjustment for horizontal sizers:
1723 if ( !IsVertical() && m_containingWindow
)
1725 posChild
.x
= m_containingWindow
->AdjustForLayoutDirection
1733 // finally set size of this child and advance to the next one
1734 item
->SetDimension(posChild
, SizeFromMajorMinor(majorSize
, minorSize
));
1736 PosInMajorDir(pt
) += majorSize
;
1740 wxSize
wxBoxSizer::CalcMin()
1742 m_totalProportion
= 0;
1743 m_minSize
= wxSize(0, 0);
1745 // calculate the minimal sizes for all items and count sum of proportions
1746 for ( wxSizerItemList::const_iterator i
= m_children
.begin();
1747 i
!= m_children
.end();
1750 wxSizerItem
* const item
= *i
;
1752 if ( !item
->IsShown() )
1755 const wxSize sizeMinThis
= item
->CalcMin();
1757 SizeInMajorDir(m_minSize
) += SizeInMajorDir(sizeMinThis
);
1758 if ( SizeInMinorDir(sizeMinThis
) > SizeInMinorDir(m_minSize
) )
1759 SizeInMinorDir(m_minSize
) = SizeInMinorDir(sizeMinThis
);
1761 m_totalProportion
+= item
->GetProportion();
1767 //---------------------------------------------------------------------------
1769 //---------------------------------------------------------------------------
1773 wxStaticBoxSizer::wxStaticBoxSizer( wxStaticBox
*box
, int orient
)
1774 : wxBoxSizer( orient
),
1777 wxASSERT_MSG( box
, wxT("wxStaticBoxSizer needs a static box") );
1779 // do this so that our Detach() is called if the static box is destroyed
1781 m_staticBox
->SetContainingSizer(this);
1784 wxStaticBoxSizer::wxStaticBoxSizer(int orient
, wxWindow
*win
, const wxString
& s
)
1785 : wxBoxSizer(orient
),
1786 m_staticBox(new wxStaticBox(win
, wxID_ANY
, s
))
1789 m_staticBox
->SetContainingSizer(this);
1792 wxStaticBoxSizer::~wxStaticBoxSizer()
1797 static void GetStaticBoxBorders( wxStaticBox
*box
,
1801 // this has to be done platform by platform as there is no way to
1802 // guess the thickness of a wxStaticBox border
1803 box
->GetBordersForSizer(borderTop
, borderOther
);
1806 void wxStaticBoxSizer::RecalcSizes()
1808 int top_border
, other_border
;
1809 GetStaticBoxBorders(m_staticBox
, &top_border
, &other_border
);
1811 m_staticBox
->SetSize( m_position
.x
, m_position
.y
, m_size
.x
, m_size
.y
);
1813 wxPoint
old_pos( m_position
);
1814 m_position
.x
+= other_border
;
1815 m_position
.y
+= top_border
;
1816 wxSize
old_size( m_size
);
1817 m_size
.x
-= 2*other_border
;
1818 m_size
.y
-= top_border
+ other_border
;
1820 wxBoxSizer::RecalcSizes();
1822 m_position
= old_pos
;
1826 wxSize
wxStaticBoxSizer::CalcMin()
1828 int top_border
, other_border
;
1829 GetStaticBoxBorders(m_staticBox
, &top_border
, &other_border
);
1831 wxSize
ret( wxBoxSizer::CalcMin() );
1832 ret
.x
+= 2*other_border
;
1833 ret
.y
+= other_border
+ top_border
;
1838 void wxStaticBoxSizer::ShowItems( bool show
)
1840 m_staticBox
->Show( show
);
1841 wxBoxSizer::ShowItems( show
);
1844 bool wxStaticBoxSizer::Detach( wxWindow
*window
)
1846 // avoid deleting m_staticBox in our dtor if it's being detached from the
1847 // sizer (which can happen because it's being already destroyed for
1849 if ( window
== m_staticBox
)
1855 return wxSizer::Detach( window
);
1858 #endif // wxUSE_STATBOX
1862 wxStdDialogButtonSizer::wxStdDialogButtonSizer()
1863 : wxBoxSizer(wxHORIZONTAL
)
1865 // Vertical buttons with lots of space on either side
1866 // looks rubbish on WinCE, so let's not do this for now.
1867 // If we are going to use vertical buttons, we should
1868 // put the sizer to the right of other controls in the dialog,
1869 // and that's beyond the scope of this sizer.
1871 bool is_pda
= (wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA
);
1872 // If we have a PDA screen, put yes/no button over
1873 // all other buttons, otherwise on the left side.
1875 m_orient
= wxVERTICAL
;
1878 m_buttonAffirmative
= NULL
;
1879 m_buttonApply
= NULL
;
1880 m_buttonNegative
= NULL
;
1881 m_buttonCancel
= NULL
;
1882 m_buttonHelp
= NULL
;
1885 void wxStdDialogButtonSizer::AddButton(wxButton
*mybutton
)
1887 switch (mybutton
->GetId())
1892 m_buttonAffirmative
= mybutton
;
1895 m_buttonApply
= mybutton
;
1898 m_buttonNegative
= mybutton
;
1902 m_buttonCancel
= mybutton
;
1905 case wxID_CONTEXT_HELP
:
1906 m_buttonHelp
= mybutton
;
1913 void wxStdDialogButtonSizer::SetAffirmativeButton( wxButton
*button
)
1915 m_buttonAffirmative
= button
;
1918 void wxStdDialogButtonSizer::SetNegativeButton( wxButton
*button
)
1920 m_buttonNegative
= button
;
1923 void wxStdDialogButtonSizer::SetCancelButton( wxButton
*button
)
1925 m_buttonCancel
= button
;
1928 void wxStdDialogButtonSizer::Realize()
1931 Add(0, 0, 0, wxLEFT
, 6);
1933 Add((wxWindow
*)m_buttonHelp
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, 6);
1935 if (m_buttonNegative
){
1936 // HIG POLICE BULLETIN - destructive buttons need extra padding
1937 // 24 pixels on either side
1938 Add((wxWindow
*)m_buttonNegative
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, 12);
1941 // extra whitespace between help/negative and cancel/ok buttons
1942 Add(0, 0, 1, wxEXPAND
, 0);
1944 if (m_buttonCancel
){
1945 Add((wxWindow
*)m_buttonCancel
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, 6);
1946 // Cancel or help should be default
1947 // m_buttonCancel->SetDefaultButton();
1950 // Ugh, Mac doesn't really have apply dialogs, so I'll just
1951 // figure the best place is between Cancel and OK
1953 Add((wxWindow
*)m_buttonApply
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, 6);
1955 if (m_buttonAffirmative
){
1956 Add((wxWindow
*)m_buttonAffirmative
, 0, wxALIGN_CENTRE
| wxLEFT
, 6);
1958 if (m_buttonAffirmative
->GetId() == wxID_SAVE
){
1959 // these buttons have set labels under Mac so we should use them
1960 m_buttonAffirmative
->SetLabel(_("Save"));
1961 if (m_buttonNegative
)
1962 m_buttonNegative
->SetLabel(_("Don't Save"));
1966 // Extra space around and at the right
1968 #elif defined(__WXGTK20__)
1969 Add(0, 0, 0, wxLEFT
, 9);
1971 Add((wxWindow
*)m_buttonHelp
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, 3);
1973 // extra whitespace between help and cancel/ok buttons
1974 Add(0, 0, 1, wxEXPAND
, 0);
1976 if (m_buttonNegative
){
1977 Add((wxWindow
*)m_buttonNegative
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, 3);
1980 // according to HIG, in explicit apply windows the order is:
1981 // [ Help Apply Cancel OK ]
1983 Add((wxWindow
*)m_buttonApply
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, 3);
1985 if (m_buttonCancel
){
1986 Add((wxWindow
*)m_buttonCancel
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, 3);
1987 // Cancel or help should be default
1988 // m_buttonCancel->SetDefaultButton();
1991 if (m_buttonAffirmative
)
1992 Add((wxWindow
*)m_buttonAffirmative
, 0, wxALIGN_CENTRE
| wxLEFT
, 6);
1993 #elif defined(__WXMSW__)
1996 // right-justify buttons
1997 Add(0, 0, 1, wxEXPAND
, 0);
1999 if (m_buttonAffirmative
){
2000 Add((wxWindow
*)m_buttonAffirmative
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, m_buttonAffirmative
->ConvertDialogToPixels(wxSize(2, 0)).x
);
2003 if (m_buttonNegative
){
2004 Add((wxWindow
*)m_buttonNegative
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, m_buttonNegative
->ConvertDialogToPixels(wxSize(2, 0)).x
);
2007 if (m_buttonCancel
){
2008 Add((wxWindow
*)m_buttonCancel
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, m_buttonCancel
->ConvertDialogToPixels(wxSize(2, 0)).x
);
2011 Add((wxWindow
*)m_buttonApply
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, m_buttonApply
->ConvertDialogToPixels(wxSize(2, 0)).x
);
2014 Add((wxWindow
*)m_buttonHelp
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, m_buttonHelp
->ConvertDialogToPixels(wxSize(2, 0)).x
);
2016 // GTK+1 and any other platform
2018 // Add(0, 0, 0, wxLEFT, 5); // Not sure what this was for but it unbalances the dialog
2020 Add((wxWindow
*)m_buttonHelp
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, m_buttonHelp
->ConvertDialogToPixels(wxSize(4, 0)).x
);
2022 // extra whitespace between help and cancel/ok buttons
2023 Add(0, 0, 1, wxEXPAND
, 0);
2026 Add((wxWindow
*)m_buttonApply
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, m_buttonApply
->ConvertDialogToPixels(wxSize(4, 0)).x
);
2028 if (m_buttonAffirmative
){
2029 Add((wxWindow
*)m_buttonAffirmative
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, m_buttonAffirmative
->ConvertDialogToPixels(wxSize(4, 0)).x
);
2032 if (m_buttonNegative
){
2033 Add((wxWindow
*)m_buttonNegative
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, m_buttonNegative
->ConvertDialogToPixels(wxSize(4, 0)).x
);
2036 if (m_buttonCancel
){
2037 Add((wxWindow
*)m_buttonCancel
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, m_buttonCancel
->ConvertDialogToPixels(wxSize(4, 0)).x
);
2038 // Cancel or help should be default
2039 // m_buttonCancel->SetDefaultButton();
2045 #endif // wxUSE_BUTTON