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()
110 void wxSizerItem::DoSetWindow(wxWindow
*window
)
112 wxCHECK_RET( window
, _T("NULL window in wxSizerItem::SetWindow()") );
114 m_kind
= Item_Window
;
117 // window doesn't become smaller than its initial size, whatever happens
118 m_minSize
= window
->GetSize();
120 if ( m_flag
& wxFIXED_MINSIZE
)
121 window
->SetMinSize(m_minSize
);
123 // aspect ratio calculated from initial size
127 wxSizerItem::wxSizerItem(wxWindow
*window
,
133 m_proportion(proportion
),
143 void wxSizerItem::DoSetSizer(wxSizer
*sizer
)
149 wxSizerItem::wxSizerItem(wxSizer
*sizer
,
156 m_proportion(proportion
),
165 // m_minSize is set later
169 void wxSizerItem::DoSetSpacer(const wxSize
& size
)
171 m_kind
= Item_Spacer
;
172 m_spacer
= new wxSizerSpacer(size
);
177 wxSizerItem::wxSizerItem(int width
,
185 m_minSize(width
, height
), // minimal size is the initial size
186 m_proportion(proportion
),
192 DoSetSpacer(wxSize(width
, height
));
195 wxSizerItem::~wxSizerItem()
201 void wxSizerItem::Free()
209 m_window
->SetContainingSizer(NULL
);
222 wxFAIL_MSG( _T("unexpected wxSizerItem::m_kind") );
228 wxSize
wxSizerItem::GetSpacer() const
231 if ( m_kind
== Item_Spacer
)
232 size
= m_spacer
->GetSize();
238 wxSize
wxSizerItem::GetSize() const
247 ret
= m_window
->GetSize();
251 ret
= m_sizer
->GetSize();
255 ret
= m_spacer
->GetSize();
260 wxFAIL_MSG( _T("unexpected wxSizerItem::m_kind") );
267 if (m_flag
& wxNORTH
)
269 if (m_flag
& wxSOUTH
)
275 bool wxSizerItem::InformFirstDirection(int direction
, int size
, int availableOtherDir
)
277 // The size that come here will be including borders. Child items should get it
281 if( direction
==wxHORIZONTAL
)
288 else if( direction
==wxVERTICAL
)
290 if (m_flag
& wxNORTH
)
292 if (m_flag
& wxSOUTH
)
298 // Pass the information along to the held object
301 didUse
= GetSizer()->InformFirstDirection(direction
,size
,availableOtherDir
);
303 m_minSize
= GetSizer()->CalcMin();
307 didUse
= GetWindow()->InformFirstDirection(direction
,size
,availableOtherDir
);
309 m_minSize
= m_window
->GetEffectiveMinSize();
311 // This information is useful for items with wxSHAPED flag, since
312 // we can request an optimal min size for such an item. Even if
313 // we overwrite the m_minSize member here, we can read it back from
314 // the owned window (happens automatically).
315 if( (m_flag
& wxSHAPED
) && (m_flag
& wxEXPAND
) && direction
)
317 if( !wxIsNullDouble(m_ratio
) )
319 wxCHECK_MSG( (m_proportion
==0), false, _T("Shaped item, non-zero proportion in wxSizerItem::InformFirstDirection()") );
320 if( direction
==wxHORIZONTAL
&& !wxIsNullDouble(m_ratio
) )
322 // Clip size so that we don't take too much
323 if( availableOtherDir
>=0 && int(size
/m_ratio
)-m_minSize
.y
>availableOtherDir
)
324 size
= int((availableOtherDir
+m_minSize
.y
)*m_ratio
);
325 m_minSize
= wxSize(size
,int(size
/m_ratio
));
327 else if( direction
==wxVERTICAL
)
329 // Clip size so that we don't take too much
330 if( availableOtherDir
>=0 && int(size
*m_ratio
)-m_minSize
.x
>availableOtherDir
)
331 size
= int((availableOtherDir
+m_minSize
.x
)/m_ratio
);
332 m_minSize
= wxSize(int(size
*m_ratio
),size
);
342 wxSize
wxSizerItem::CalcMin()
346 m_minSize
= m_sizer
->GetMinSize();
348 // if we have to preserve aspect ratio _AND_ this is
349 // the first-time calculation, consider ret to be initial size
350 if ( (m_flag
& wxSHAPED
) && wxIsNullDouble(m_ratio
) )
353 else if ( IsWindow() )
355 // Since the size of the window may change during runtime, we
356 // should use the current minimal/best size.
357 m_minSize
= m_window
->GetEffectiveMinSize();
360 return GetMinSizeWithBorder();
363 wxSize
wxSizerItem::GetMinSizeWithBorder() const
365 wxSize ret
= m_minSize
;
371 if (m_flag
& wxNORTH
)
373 if (m_flag
& wxSOUTH
)
380 void wxSizerItem::SetDimension( const wxPoint
& pos_
, const wxSize
& size_
)
384 if (m_flag
& wxSHAPED
)
386 // adjust aspect ratio
387 int rwidth
= (int) (size
.y
* m_ratio
);
391 int rheight
= (int) (size
.x
/ m_ratio
);
392 // add vertical space
393 if (m_flag
& wxALIGN_CENTER_VERTICAL
)
394 pos
.y
+= (size
.y
- rheight
) / 2;
395 else if (m_flag
& wxALIGN_BOTTOM
)
396 pos
.y
+= (size
.y
- rheight
);
397 // use reduced dimensions
400 else if (rwidth
< size
.x
)
402 // add horizontal space
403 if (m_flag
& wxALIGN_CENTER_HORIZONTAL
)
404 pos
.x
+= (size
.x
- rwidth
) / 2;
405 else if (m_flag
& wxALIGN_RIGHT
)
406 pos
.x
+= (size
.x
- rwidth
);
411 // This is what GetPosition() returns. Since we calculate
412 // borders afterwards, GetPosition() will be the left/top
413 // corner of the surrounding border.
425 if (m_flag
& wxNORTH
)
430 if (m_flag
& wxSOUTH
)
440 m_rect
= wxRect(pos
, size
);
445 wxFAIL_MSG( _T("can't set size of uninitialized sizer item") );
449 m_window
->SetSize(pos
.x
, pos
.y
, size
.x
, size
.y
,
450 wxSIZE_ALLOW_MINUS_ONE
);
454 m_sizer
->SetDimension(pos
.x
, pos
.y
, size
.x
, size
.y
);
458 m_spacer
->SetSize(size
);
463 wxFAIL_MSG( _T("unexpected wxSizerItem::m_kind") );
467 void wxSizerItem::DeleteWindows()
476 //We are deleting the window from this sizer - normally
477 //the window destroys the sizer associated with it,
478 //which might destroy this, which we don't want
479 m_window
->SetContainingSizer(NULL
);
481 //Putting this after the switch will result in a spacer
482 //not being deleted properly on destruction
487 m_sizer
->DeleteWindows();
492 wxFAIL_MSG( _T("unexpected wxSizerItem::m_kind") );
497 void wxSizerItem::Show( bool show
)
502 wxFAIL_MSG( _T("can't show uninitialized sizer item") );
506 m_window
->Show(show
);
514 m_spacer
->Show(show
);
519 wxFAIL_MSG( _T("unexpected wxSizerItem::m_kind") );
523 bool wxSizerItem::IsShown() const
528 // we may be called from CalcMin(), just return false so that we're
533 return m_window
->IsShown();
536 // arbitrarily decide that if at least one of our elements is
537 // shown, so are we (this arbitrariness is the reason for
538 // deprecating this function)
540 // Some apps (such as dialog editors) depend on an empty sizer still
541 // being laid out correctly and reporting the correct size and position.
542 if (m_sizer
->GetChildren().GetCount() == 0)
545 for ( wxSizerItemList::compatibility_iterator
546 node
= m_sizer
->GetChildren().GetFirst();
548 node
= node
->GetNext() )
550 if ( node
->GetData()->IsShown() )
557 return m_spacer
->IsShown();
561 wxFAIL_MSG( _T("unexpected wxSizerItem::m_kind") );
567 #if WXWIN_COMPATIBILITY_2_6
568 void wxSizerItem::SetOption( int option
)
570 SetProportion( option
);
573 int wxSizerItem::GetOption() const
575 return GetProportion();
577 #endif // WXWIN_COMPATIBILITY_2_6
580 //---------------------------------------------------------------------------
582 //---------------------------------------------------------------------------
586 WX_CLEAR_LIST(wxSizerItemList
, m_children
);
589 wxSizerItem
* wxSizer::Insert( size_t index
, wxSizerItem
*item
)
591 m_children
.Insert( index
, item
);
593 if ( item
->GetWindow() )
594 item
->GetWindow()->SetContainingSizer( this );
596 if ( item
->GetSizer() )
597 item
->GetSizer()->SetContainingWindow( m_containingWindow
);
602 void wxSizer::SetContainingWindow(wxWindow
*win
)
604 if ( win
== m_containingWindow
)
607 m_containingWindow
= win
;
609 // set the same window for all nested sizers as well, they also are in the
611 for ( wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
613 node
= node
->GetNext() )
615 wxSizerItem
*const item
= node
->GetData();
616 wxSizer
*const sizer
= item
->GetSizer();
620 sizer
->SetContainingWindow(win
);
625 #if WXWIN_COMPATIBILITY_2_6
626 bool wxSizer::Remove( wxWindow
*window
)
628 return Detach( window
);
630 #endif // WXWIN_COMPATIBILITY_2_6
632 bool wxSizer::Remove( wxSizer
*sizer
)
634 wxASSERT_MSG( sizer
, _T("Removing NULL sizer") );
636 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
639 wxSizerItem
*item
= node
->GetData();
641 if (item
->GetSizer() == sizer
)
644 m_children
.Erase( node
);
648 node
= node
->GetNext();
654 bool wxSizer::Remove( int index
)
656 wxCHECK_MSG( index
>= 0 && (size_t)index
< m_children
.GetCount(),
658 _T("Remove index is out of range") );
660 wxSizerItemList::compatibility_iterator node
= m_children
.Item( index
);
662 wxCHECK_MSG( node
, false, _T("Failed to find child node") );
664 delete node
->GetData();
665 m_children
.Erase( node
);
670 bool wxSizer::Detach( wxSizer
*sizer
)
672 wxASSERT_MSG( sizer
, _T("Detaching NULL sizer") );
674 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
677 wxSizerItem
*item
= node
->GetData();
679 if (item
->GetSizer() == sizer
)
683 m_children
.Erase( node
);
686 node
= node
->GetNext();
692 bool wxSizer::Detach( wxWindow
*window
)
694 wxASSERT_MSG( window
, _T("Detaching NULL window") );
696 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
699 wxSizerItem
*item
= node
->GetData();
701 if (item
->GetWindow() == window
)
704 m_children
.Erase( node
);
707 node
= node
->GetNext();
713 bool wxSizer::Detach( int index
)
715 wxCHECK_MSG( index
>= 0 && (size_t)index
< m_children
.GetCount(),
717 _T("Detach index is out of range") );
719 wxSizerItemList::compatibility_iterator node
= m_children
.Item( index
);
721 wxCHECK_MSG( node
, false, _T("Failed to find child node") );
723 wxSizerItem
*item
= node
->GetData();
725 if ( item
->IsSizer() )
729 m_children
.Erase( node
);
733 bool wxSizer::Replace( wxWindow
*oldwin
, wxWindow
*newwin
, bool recursive
)
735 wxASSERT_MSG( oldwin
, _T("Replacing NULL window") );
736 wxASSERT_MSG( newwin
, _T("Replacing with NULL window") );
738 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
741 wxSizerItem
*item
= node
->GetData();
743 if (item
->GetWindow() == oldwin
)
745 item
->AssignWindow(newwin
);
746 newwin
->SetContainingSizer( this );
749 else if (recursive
&& item
->IsSizer())
751 if (item
->GetSizer()->Replace( oldwin
, newwin
, true ))
755 node
= node
->GetNext();
761 bool wxSizer::Replace( wxSizer
*oldsz
, wxSizer
*newsz
, bool recursive
)
763 wxASSERT_MSG( oldsz
, _T("Replacing NULL sizer") );
764 wxASSERT_MSG( newsz
, _T("Replacing with NULL sizer") );
766 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
769 wxSizerItem
*item
= node
->GetData();
771 if (item
->GetSizer() == oldsz
)
773 item
->AssignSizer(newsz
);
776 else if (recursive
&& item
->IsSizer())
778 if (item
->GetSizer()->Replace( oldsz
, newsz
, true ))
782 node
= node
->GetNext();
788 bool wxSizer::Replace( size_t old
, wxSizerItem
*newitem
)
790 wxCHECK_MSG( old
< m_children
.GetCount(), false, _T("Replace index is out of range") );
791 wxASSERT_MSG( newitem
, _T("Replacing with NULL item") );
793 wxSizerItemList::compatibility_iterator node
= m_children
.Item( old
);
795 wxCHECK_MSG( node
, false, _T("Failed to find child node") );
797 wxSizerItem
*item
= node
->GetData();
798 node
->SetData(newitem
);
804 void wxSizer::Clear( bool delete_windows
)
806 // First clear the ContainingSizer pointers
807 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
810 wxSizerItem
*item
= node
->GetData();
812 if (item
->IsWindow())
813 item
->GetWindow()->SetContainingSizer( NULL
);
814 node
= node
->GetNext();
817 // Destroy the windows if needed
821 // Now empty the list
822 WX_CLEAR_LIST(wxSizerItemList
, m_children
);
825 void wxSizer::DeleteWindows()
827 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
830 wxSizerItem
*item
= node
->GetData();
832 item
->DeleteWindows();
833 node
= node
->GetNext();
837 wxSize
wxSizer::Fit( wxWindow
*window
)
839 // take the min size by default and limit it by max size
840 wxSize size
= GetMinClientSize(window
);
843 wxTopLevelWindow
*tlw
= wxDynamicCast(window
, wxTopLevelWindow
);
846 // hack for small screen devices where TLWs are always full screen
847 if ( tlw
->IsAlwaysMaximized() )
850 return tlw
->GetSize();
853 // limit the window to the size of the display it is on
854 int disp
= wxDisplay::GetFromWindow(window
);
855 if ( disp
== wxNOT_FOUND
)
857 // or, if we don't know which one it is, of the main one
861 sizeMax
= wxDisplay(disp
).GetClientArea().GetSize();
863 // space for decorations and toolbars etc.
864 sizeMax
= tlw
->WindowToClientSize(sizeMax
);
868 sizeMax
= GetMaxClientSize(window
);
871 if ( sizeMax
.x
!= wxDefaultCoord
&& size
.x
> sizeMax
.x
)
873 if ( sizeMax
.y
!= wxDefaultCoord
&& size
.y
> sizeMax
.y
)
877 window
->SetClientSize( size
);
879 // return entire size
880 return window
->GetSize();
883 void wxSizer::FitInside( wxWindow
*window
)
886 if (window
->IsTopLevel())
887 size
= VirtualFitSize( window
);
889 size
= GetMinClientSize( window
);
891 window
->SetVirtualSize( size
);
894 void wxSizer::Layout()
896 // (re)calculates minimums needed for each item and other preparations
900 // Applies the layout and repositions/resizes the items
904 void wxSizer::SetSizeHints( wxWindow
*window
)
906 // Preserve the window's max size hints, but set the
907 // lower bound according to the sizer calculations.
909 wxSize size
= Fit( window
);
911 window
->SetSizeHints( size
.x
,
913 window
->GetMaxWidth(),
914 window
->GetMaxHeight() );
917 #if WXWIN_COMPATIBILITY_2_8
918 void wxSizer::SetVirtualSizeHints( wxWindow
*window
)
922 #endif // WXWIN_COMPATIBILITY_2_8
924 wxSize
wxSizer::GetMaxWindowSize( wxWindow
*window
) const
926 return window
->GetMaxSize();
929 wxSize
wxSizer::GetMinWindowSize( wxWindow
*window
)
931 return window
->ClientToWindowSize(GetMinSize());
934 // TODO on mac we need a function that determines how much free space this
935 // min size contains, in order to make sure that we have 20 pixels of free
936 // space around the controls
937 wxSize
wxSizer::GetMaxClientSize( wxWindow
*window
) const
939 return window
->WindowToClientSize(window
->GetMaxSize());
942 wxSize
wxSizer::GetMinClientSize( wxWindow
*WXUNUSED(window
) )
944 return GetMinSize(); // Already returns client size.
947 wxSize
wxSizer::VirtualFitSize( wxWindow
*window
)
949 wxSize size
= GetMinClientSize( window
);
950 wxSize sizeMax
= GetMaxClientSize( window
);
952 // Limit the size if sizeMax != wxDefaultSize
954 if ( size
.x
> sizeMax
.x
&& sizeMax
.x
!= wxDefaultCoord
)
956 if ( size
.y
> sizeMax
.y
&& sizeMax
.y
!= wxDefaultCoord
)
962 void wxSizer::SetDimension( int x
, int y
, int width
, int height
)
971 wxSize
wxSizer::GetMinSize()
973 wxSize
ret( CalcMin() );
974 if (ret
.x
< m_minSize
.x
) ret
.x
= m_minSize
.x
;
975 if (ret
.y
< m_minSize
.y
) ret
.y
= m_minSize
.y
;
979 void wxSizer::DoSetMinSize( int width
, int height
)
982 m_minSize
.y
= height
;
985 bool wxSizer::DoSetItemMinSize( wxWindow
*window
, int width
, int height
)
987 wxASSERT_MSG( window
, _T("SetMinSize for NULL window") );
989 // Is it our immediate child?
991 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
994 wxSizerItem
*item
= node
->GetData();
996 if (item
->GetWindow() == window
)
998 item
->SetMinSize( width
, height
);
1001 node
= node
->GetNext();
1004 // No? Search any subsizers we own then
1006 node
= m_children
.GetFirst();
1009 wxSizerItem
*item
= node
->GetData();
1011 if ( item
->GetSizer() &&
1012 item
->GetSizer()->DoSetItemMinSize( window
, width
, height
) )
1014 // A child sizer found the requested windw, exit.
1017 node
= node
->GetNext();
1023 bool wxSizer::DoSetItemMinSize( wxSizer
*sizer
, int width
, int height
)
1025 wxASSERT_MSG( sizer
, _T("SetMinSize for NULL sizer") );
1027 // Is it our immediate child?
1029 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
1032 wxSizerItem
*item
= node
->GetData();
1034 if (item
->GetSizer() == sizer
)
1036 item
->GetSizer()->DoSetMinSize( width
, height
);
1039 node
= node
->GetNext();
1042 // No? Search any subsizers we own then
1044 node
= m_children
.GetFirst();
1047 wxSizerItem
*item
= node
->GetData();
1049 if ( item
->GetSizer() &&
1050 item
->GetSizer()->DoSetItemMinSize( sizer
, width
, height
) )
1052 // A child found the requested sizer, exit.
1055 node
= node
->GetNext();
1061 bool wxSizer::DoSetItemMinSize( size_t index
, int width
, int height
)
1063 wxSizerItemList::compatibility_iterator node
= m_children
.Item( index
);
1065 wxCHECK_MSG( node
, false, _T("Failed to find child node") );
1067 wxSizerItem
*item
= node
->GetData();
1069 if (item
->GetSizer())
1071 // Sizers contains the minimal size in them, if not calculated ...
1072 item
->GetSizer()->DoSetMinSize( width
, height
);
1076 // ... but the minimal size of spacers and windows is stored via the item
1077 item
->SetMinSize( width
, height
);
1083 wxSizerItem
* wxSizer::GetItem( wxWindow
*window
, bool recursive
)
1085 wxASSERT_MSG( window
, _T("GetItem for NULL window") );
1087 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
1090 wxSizerItem
*item
= node
->GetData();
1092 if (item
->GetWindow() == window
)
1096 else if (recursive
&& item
->IsSizer())
1098 wxSizerItem
*subitem
= item
->GetSizer()->GetItem( window
, true );
1103 node
= node
->GetNext();
1109 wxSizerItem
* wxSizer::GetItem( wxSizer
*sizer
, bool recursive
)
1111 wxASSERT_MSG( sizer
, _T("GetItem for NULL sizer") );
1113 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
1116 wxSizerItem
*item
= node
->GetData();
1118 if (item
->GetSizer() == sizer
)
1122 else if (recursive
&& item
->IsSizer())
1124 wxSizerItem
*subitem
= item
->GetSizer()->GetItem( sizer
, true );
1129 node
= node
->GetNext();
1135 wxSizerItem
* wxSizer::GetItem( size_t index
)
1137 wxCHECK_MSG( index
< m_children
.GetCount(),
1139 _T("GetItem index is out of range") );
1141 return m_children
.Item( index
)->GetData();
1144 wxSizerItem
* wxSizer::GetItemById( int id
, bool recursive
)
1146 // This gets a sizer item by the id of the sizer item
1147 // and NOT the id of a window if the item is a window.
1149 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
1152 wxSizerItem
*item
= node
->GetData();
1154 if (item
->GetId() == id
)
1158 else if (recursive
&& item
->IsSizer())
1160 wxSizerItem
*subitem
= item
->GetSizer()->GetItemById( id
, true );
1165 node
= node
->GetNext();
1171 bool wxSizer::Show( wxWindow
*window
, bool show
, bool recursive
)
1173 wxSizerItem
*item
= GetItem( window
, recursive
);
1184 bool wxSizer::Show( wxSizer
*sizer
, bool show
, bool recursive
)
1186 wxSizerItem
*item
= GetItem( sizer
, recursive
);
1197 bool wxSizer::Show( size_t index
, bool show
)
1199 wxSizerItem
*item
= GetItem( index
);
1210 void wxSizer::ShowItems( bool show
)
1212 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
1215 node
->GetData()->Show( show
);
1216 node
= node
->GetNext();
1220 bool wxSizer::IsShown( wxWindow
*window
) const
1222 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
1225 wxSizerItem
*item
= node
->GetData();
1227 if (item
->GetWindow() == window
)
1229 return item
->IsShown();
1231 node
= node
->GetNext();
1234 wxFAIL_MSG( _T("IsShown failed to find sizer item") );
1239 bool wxSizer::IsShown( wxSizer
*sizer
) const
1241 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
1244 wxSizerItem
*item
= node
->GetData();
1246 if (item
->GetSizer() == sizer
)
1248 return item
->IsShown();
1250 node
= node
->GetNext();
1253 wxFAIL_MSG( _T("IsShown failed to find sizer item") );
1258 bool wxSizer::IsShown( size_t index
) const
1260 wxCHECK_MSG( index
< m_children
.GetCount(),
1262 _T("IsShown index is out of range") );
1264 return m_children
.Item( index
)->GetData()->IsShown();
1268 //---------------------------------------------------------------------------
1270 //---------------------------------------------------------------------------
1272 wxGridSizer::wxGridSizer( int rows
, int cols
, int vgap
, int hgap
)
1273 : m_rows( ( cols
== 0 && rows
== 0 ) ? 1 : rows
)
1280 wxGridSizer::wxGridSizer( int cols
, int vgap
, int hgap
)
1281 : m_rows( cols
== 0 ? 1 : 0 )
1288 int wxGridSizer::CalcRowsCols(int& nrows
, int& ncols
) const
1290 int nitems
= m_children
.GetCount();
1296 nrows
= (nitems
+ m_cols
- 1) / m_cols
;
1300 ncols
= (nitems
+ m_rows
- 1) / m_rows
;
1303 else // 0 columns, 0 rows?
1305 wxFAIL_MSG( _T("grid sizer must have either rows or columns fixed") );
1314 void wxGridSizer::RecalcSizes()
1316 int nitems
, nrows
, ncols
;
1317 if ( (nitems
= CalcRowsCols(nrows
, ncols
)) == 0 )
1320 wxSize
sz( GetSize() );
1321 wxPoint
pt( GetPosition() );
1323 int w
= (sz
.x
- (ncols
- 1) * m_hgap
) / ncols
;
1324 int h
= (sz
.y
- (nrows
- 1) * m_vgap
) / nrows
;
1327 for (int c
= 0; c
< ncols
; c
++)
1330 for (int r
= 0; r
< nrows
; r
++)
1332 int i
= r
* ncols
+ c
;
1335 wxSizerItemList::compatibility_iterator node
= m_children
.Item( i
);
1337 wxASSERT_MSG( node
, _T("Failed to find SizerItemList node") );
1339 SetItemBounds( node
->GetData(), x
, y
, w
, h
);
1347 wxSize
wxGridSizer::CalcMin()
1350 if ( CalcRowsCols(nrows
, ncols
) == 0 )
1353 // Find the max width and height for any component
1357 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
1360 wxSizerItem
*item
= node
->GetData();
1361 wxSize
sz( item
->CalcMin() );
1363 w
= wxMax( w
, sz
.x
);
1364 h
= wxMax( h
, sz
.y
);
1366 node
= node
->GetNext();
1369 // In case we have a nested sizer with a two step algo , give it
1370 // a chance to adjust to that (we give it width component)
1371 node
= m_children
.GetFirst();
1372 bool didChangeMinSize
= false;
1375 wxSizerItem
*item
= node
->GetData();
1376 didChangeMinSize
|= item
->InformFirstDirection( wxHORIZONTAL
, w
, -1 );
1378 node
= node
->GetNext();
1381 // And redo iteration in case min size changed
1382 if( didChangeMinSize
)
1384 node
= m_children
.GetFirst();
1388 wxSizerItem
*item
= node
->GetData();
1389 wxSize
sz( item
->GetMinSizeWithBorder() );
1391 w
= wxMax( w
, sz
.x
);
1392 h
= wxMax( h
, sz
.y
);
1394 node
= node
->GetNext();
1398 return wxSize( ncols
* w
+ (ncols
-1) * m_hgap
,
1399 nrows
* h
+ (nrows
-1) * m_vgap
);
1402 void wxGridSizer::SetItemBounds( wxSizerItem
*item
, int x
, int y
, int w
, int h
)
1405 wxSize
sz( item
->GetMinSizeWithBorder() );
1406 int flag
= item
->GetFlag();
1408 if ((flag
& wxEXPAND
) || (flag
& wxSHAPED
))
1414 if (flag
& wxALIGN_CENTER_HORIZONTAL
)
1416 pt
.x
= x
+ (w
- sz
.x
) / 2;
1418 else if (flag
& wxALIGN_RIGHT
)
1420 pt
.x
= x
+ (w
- sz
.x
);
1423 if (flag
& wxALIGN_CENTER_VERTICAL
)
1425 pt
.y
= y
+ (h
- sz
.y
) / 2;
1427 else if (flag
& wxALIGN_BOTTOM
)
1429 pt
.y
= y
+ (h
- sz
.y
);
1433 item
->SetDimension(pt
, sz
);
1436 //---------------------------------------------------------------------------
1438 //---------------------------------------------------------------------------
1440 wxFlexGridSizer::wxFlexGridSizer( int rows
, int cols
, int vgap
, int hgap
)
1441 : wxGridSizer( rows
, cols
, vgap
, hgap
),
1442 m_flexDirection(wxBOTH
),
1443 m_growMode(wxFLEX_GROWMODE_SPECIFIED
)
1447 wxFlexGridSizer::wxFlexGridSizer( int cols
, int vgap
, int hgap
)
1448 : wxGridSizer( cols
, vgap
, hgap
),
1449 m_flexDirection(wxBOTH
),
1450 m_growMode(wxFLEX_GROWMODE_SPECIFIED
)
1454 wxFlexGridSizer::~wxFlexGridSizer()
1458 void wxFlexGridSizer::RecalcSizes()
1461 if ( !CalcRowsCols(nrows
, ncols
) )
1464 const wxPoint
pt(GetPosition());
1465 const wxSize
sz(GetSize());
1467 AdjustForGrowables(sz
);
1469 wxSizerItemList::const_iterator i
= m_children
.begin();
1470 const wxSizerItemList::const_iterator end
= m_children
.end();
1473 for ( int r
= 0; r
< nrows
; r
++ )
1475 if ( m_rowHeights
[r
] == -1 )
1477 // this row is entirely hidden, skip it
1478 for ( int c
= 0; c
< ncols
; c
++ )
1489 const int hrow
= m_rowHeights
[r
];
1490 int h
= sz
.y
- y
; // max remaining height, don't overflow it
1495 for ( int c
= 0; c
< ncols
&& i
!= end
; c
++, ++i
)
1497 const int wcol
= m_colWidths
[c
];
1502 int w
= sz
.x
- x
; // max possible value, ensure we don't overflow
1506 SetItemBounds(*i
, pt
.x
+ x
, pt
.y
+ y
, w
, h
);
1518 // helper function used in CalcMin() to sum up the sizes of non-hidden items
1519 static int SumArraySizes(const wxArrayInt
& sizes
, int gap
)
1521 // Sum total minimum size, including gaps between rows/columns.
1522 // -1 is used as a magic number meaning empty row/column.
1525 const size_t count
= sizes
.size();
1526 for ( size_t n
= 0; n
< count
; n
++ )
1528 if ( sizes
[n
] != -1 )
1531 total
+= gap
; // separate from the previous column
1540 void wxFlexGridSizer::FindWidthsAndHeights(int nrows
, int ncols
)
1542 // We have to recalculate the sizes in case the item minimum size has
1543 // changed since the previous layout, or the item has been hidden using
1544 // wxSizer::Show(). If all the items in a row/column are hidden, the final
1545 // dimension of the row/column will be -1, indicating that the column
1546 // itself is hidden.
1547 m_rowHeights
.assign(nrows
, -1);
1548 m_colWidths
.assign(ncols
, -1);
1550 // n is the index of the item in left-to-right top-to-bottom order
1552 for ( wxSizerItemList::iterator i
= m_children
.begin();
1553 i
!= m_children
.end();
1556 wxSizerItem
* const item
= *i
;
1557 if ( item
->IsShown() )
1559 // NOTE: Not doing the calculation here, this is just
1560 // for finding max values.
1561 const wxSize
sz(item
->GetMinSizeWithBorder());
1563 const int row
= n
/ ncols
;
1564 const int col
= n
% ncols
;
1566 if ( sz
.y
> m_rowHeights
[row
] )
1567 m_rowHeights
[row
] = sz
.y
;
1568 if ( sz
.x
> m_colWidths
[col
] )
1569 m_colWidths
[col
] = sz
.x
;
1573 AdjustForFlexDirection();
1575 m_calculatedMinSize
= wxSize(SumArraySizes(m_colWidths
, m_hgap
),
1576 SumArraySizes(m_rowHeights
, m_vgap
));
1579 wxSize
wxFlexGridSizer::CalcMin()
1584 // Number of rows/columns can change as items are added or removed.
1585 if ( !CalcRowsCols(nrows
, ncols
) )
1589 // We have to recalculate the sizes in case the item minimum size has
1590 // changed since the previous layout, or the item has been hidden using
1591 // wxSizer::Show(). If all the items in a row/column are hidden, the final
1592 // dimension of the row/column will be -1, indicating that the column
1593 // itself is hidden.
1594 m_rowHeights
.assign(nrows
, -1);
1595 m_colWidths
.assign(ncols
, -1);
1597 // n is the index of the item in left-to-right top-to-bottom order
1599 for ( wxSizerItemList::iterator i
= m_children
.begin();
1600 i
!= m_children
.end();
1603 wxSizerItem
* const item
= *i
;
1604 if ( item
->IsShown() )
1610 // The stage of looking for max values in each row/column has been
1611 // made a separate function, since it's reused in AdjustForGrowables.
1612 FindWidthsAndHeights(nrows
,ncols
);
1614 return m_calculatedMinSize
;
1617 void wxFlexGridSizer::AdjustForFlexDirection()
1619 // the logic in CalcMin works when we resize flexibly in both directions
1620 // but maybe this is not the case
1621 if ( m_flexDirection
!= wxBOTH
)
1623 // select the array corresponding to the direction in which we do *not*
1625 wxArrayInt
& array
= m_flexDirection
== wxVERTICAL
? m_colWidths
1628 const size_t count
= array
.GetCount();
1630 // find the largest value in this array
1634 for ( n
= 0; n
< count
; ++n
)
1636 if ( array
[n
] > largest
)
1640 // and now fill it with the largest value
1641 for ( n
= 0; n
< count
; ++n
)
1643 // don't touch hidden rows
1644 if ( array
[n
] != -1 )
1650 // helper of AdjustForGrowables() which is called for rows/columns separately
1653 // delta: the extra space, we do nothing unless it's positive
1654 // growable: indices or growable rows/cols in sizes array
1655 // sizes: the height/widths of rows/cols to adjust
1656 // proportions: proportions of the growable rows/cols or NULL if they all
1657 // should be assumed to have proportion of 1
1659 DoAdjustForGrowables(int delta
,
1660 const wxArrayInt
& growable
,
1662 const wxArrayInt
*proportions
)
1667 // total sum of proportions of all non-hidden rows
1668 int sum_proportions
= 0;
1670 // number of currently shown growable rows
1673 const int max_idx
= sizes
.size();
1675 const size_t count
= growable
.size();
1677 for ( idx
= 0; idx
< count
; idx
++ )
1679 // Since the number of rows/columns can change as items are
1680 // inserted/deleted, we need to verify at runtime that the
1681 // requested growable rows/columns are still valid.
1682 if ( growable
[idx
] >= max_idx
)
1685 // If all items in a row/column are hidden, that row/column will
1686 // have a dimension of -1. This causes the row/column to be
1687 // hidden completely.
1688 if ( sizes
[growable
[idx
]] == -1 )
1692 sum_proportions
+= (*proportions
)[idx
];
1700 // the remaining extra free space, adjusted during each iteration
1701 for ( idx
= 0; idx
< count
; idx
++ )
1703 if ( growable
[idx
] >= max_idx
)
1706 if ( sizes
[ growable
[idx
] ] == -1 )
1710 if ( sum_proportions
== 0 )
1712 // no growable rows -- divide extra space evenly among all
1713 cur_delta
= delta
/num
;
1716 else // allocate extra space proportionally
1718 const int cur_prop
= (*proportions
)[idx
];
1719 cur_delta
= (delta
*cur_prop
)/sum_proportions
;
1720 sum_proportions
-= cur_prop
;
1723 sizes
[growable
[idx
]] += cur_delta
;
1728 void wxFlexGridSizer::AdjustForGrowables(const wxSize
& sz
)
1730 if ( (m_flexDirection
& wxHORIZONTAL
) || (m_growMode
!= wxFLEX_GROWMODE_NONE
) )
1732 DoAdjustForGrowables
1734 sz
.x
- m_calculatedMinSize
.x
,
1737 m_growMode
== wxFLEX_GROWMODE_SPECIFIED
? &m_growableColsProportions
1741 // This gives nested objects that benefit from knowing one size
1742 // component in advance the chance to use that.
1743 bool didAdjustMinSize
= false;
1745 CalcRowsCols(nrows
, ncols
);
1747 // Iterate over all items and inform about column width
1749 for ( wxSizerItemList::iterator i
= m_children
.begin();
1750 i
!= m_children
.end();
1753 const int col
= n
% ncols
;
1754 didAdjustMinSize
|= (*i
)->InformFirstDirection(wxHORIZONTAL
, m_colWidths
[col
], sz
.y
- m_calculatedMinSize
.y
);
1757 // Only redo if info was actually used
1758 if( didAdjustMinSize
)
1760 DoAdjustForGrowables
1762 sz
.x
- m_calculatedMinSize
.x
,
1765 m_growMode
== wxFLEX_GROWMODE_SPECIFIED
? &m_growableColsProportions
1771 if ( (m_flexDirection
& wxVERTICAL
) || (m_growMode
!= wxFLEX_GROWMODE_NONE
) )
1773 // pass NULL instead of proportions if the grow mode is ALL as we
1774 // should treat all rows as having proportion of 1 then
1775 DoAdjustForGrowables
1777 sz
.y
- m_calculatedMinSize
.y
,
1780 m_growMode
== wxFLEX_GROWMODE_SPECIFIED
? &m_growableRowsProportions
1787 void wxFlexGridSizer::AddGrowableRow( size_t idx
, int proportion
)
1789 m_growableRows
.Add( idx
);
1790 m_growableRowsProportions
.Add( proportion
);
1793 void wxFlexGridSizer::AddGrowableCol( size_t idx
, int proportion
)
1795 m_growableCols
.Add( idx
);
1796 m_growableColsProportions
.Add( proportion
);
1799 // helper function for RemoveGrowableCol/Row()
1801 DoRemoveFromArrays(size_t idx
, wxArrayInt
& items
, wxArrayInt
& proportions
)
1803 const size_t count
= items
.size();
1804 for ( size_t n
= 0; n
< count
; n
++ )
1806 if ( (size_t)items
[n
] == idx
)
1809 proportions
.RemoveAt(n
);
1814 wxFAIL_MSG( _T("column/row is already not growable") );
1817 void wxFlexGridSizer::RemoveGrowableCol( size_t idx
)
1819 DoRemoveFromArrays(idx
, m_growableCols
, m_growableColsProportions
);
1822 void wxFlexGridSizer::RemoveGrowableRow( size_t idx
)
1824 DoRemoveFromArrays(idx
, m_growableRows
, m_growableRowsProportions
);
1827 //---------------------------------------------------------------------------
1829 //---------------------------------------------------------------------------
1831 void wxBoxSizer::RecalcSizes()
1833 if ( m_children
.empty() )
1836 const wxCoord totalMinorSize
= GetSizeInMinorDir(m_size
);
1838 // the amount of free space which we should redistribute among the
1839 // stretchable items (i.e. those with non zero proportion)
1840 int delta
= GetSizeInMajorDir(m_size
) - GetSizeInMajorDir(m_minSize
);
1843 // Inform child items about the size in minor direction, that can
1844 // change how much free space we have in major dir and how to distribute it.
1845 int majorMinSum
= 0;
1846 wxSizerItemList::const_iterator i
;
1847 for ( i
= m_children
.begin();
1848 i
!= m_children
.end();
1851 wxSizerItem
* const item
= *i
;
1853 if ( !item
->IsShown() )
1856 wxSize szMinPrev
= item
->GetMinSizeWithBorder();
1857 item
->InformFirstDirection(m_orient
^wxBOTH
,totalMinorSize
,delta
);
1858 wxSize szMin
= item
->GetMinSizeWithBorder();
1859 int deltaChange
= GetSizeInMajorDir(szMin
-szMinPrev
);
1862 // Since we passed available space along to the item, it should not
1863 // take too much, so delta should not become negative.
1864 delta
-= deltaChange
;
1866 majorMinSum
+= GetSizeInMajorDir(item
->GetMinSizeWithBorder());
1868 // And update our min size
1869 SizeInMajorDir(m_minSize
) = majorMinSum
;
1872 // might have a new delta now
1873 delta
= GetSizeInMajorDir(m_size
) - GetSizeInMajorDir(m_minSize
);
1875 // the position at which we put the next child
1876 wxPoint
pt(m_position
);
1878 int totalProportion
= m_totalProportion
;
1879 for ( i
= m_children
.begin();
1880 i
!= m_children
.end();
1883 wxSizerItem
* const item
= *i
;
1885 if ( !item
->IsShown() )
1888 const wxSize
sizeThis(item
->GetMinSizeWithBorder());
1890 // adjust the size in the major direction using the proportion
1891 wxCoord majorSize
= GetSizeInMajorDir(sizeThis
);
1892 const int propItem
= item
->GetProportion();
1895 const int deltaItem
= (delta
* propItem
) / totalProportion
;
1897 majorSize
+= deltaItem
;
1900 totalProportion
-= propItem
;
1904 // apply the alignment in the minor direction
1905 wxPoint
posChild(pt
);
1907 wxCoord minorSize
= GetSizeInMinorDir(sizeThis
);
1908 const int flag
= item
->GetFlag();
1909 if ( flag
& (wxEXPAND
| wxSHAPED
) )
1911 minorSize
= totalMinorSize
;
1913 else if ( flag
& (IsVertical() ? wxALIGN_RIGHT
: wxALIGN_BOTTOM
) )
1915 PosInMinorDir(posChild
) += totalMinorSize
- minorSize
;
1917 // NB: wxCENTRE is used here only for backwards compatibility,
1918 // wxALIGN_CENTRE should be used in new code
1919 else if ( flag
& (wxCENTER
| wxALIGN_CENTRE
) )
1921 PosInMinorDir(posChild
) += (totalMinorSize
- minorSize
) / 2;
1925 // apply RTL adjustment for horizontal sizers:
1926 if ( !IsVertical() && m_containingWindow
)
1928 posChild
.x
= m_containingWindow
->AdjustForLayoutDirection
1936 // finally set size of this child and advance to the next one
1937 item
->SetDimension(posChild
, SizeFromMajorMinor(majorSize
, minorSize
));
1939 PosInMajorDir(pt
) += majorSize
;
1943 wxSize
wxBoxSizer::CalcMin()
1945 m_totalProportion
= 0;
1946 m_minSize
= wxSize(0, 0);
1948 // calculate the minimal sizes for all items and count sum of proportions
1949 for ( wxSizerItemList::const_iterator i
= m_children
.begin();
1950 i
!= m_children
.end();
1953 wxSizerItem
* const item
= *i
;
1955 if ( !item
->IsShown() )
1958 const wxSize sizeMinThis
= item
->CalcMin();
1959 SizeInMajorDir(m_minSize
) += GetSizeInMajorDir(sizeMinThis
);
1960 if ( GetSizeInMinorDir(sizeMinThis
) > GetSizeInMinorDir(m_minSize
) )
1961 SizeInMinorDir(m_minSize
) = GetSizeInMinorDir(sizeMinThis
);
1963 m_totalProportion
+= item
->GetProportion();
1969 //---------------------------------------------------------------------------
1971 //---------------------------------------------------------------------------
1973 #define wxDEFAULT_PROPORTION_LAST 1000000
1975 // User data to hold old proportion for last item on line
1976 // (which might be extended)
1977 struct wxPropHolder
: public wxObject
1979 wxPropHolder( ) : m_item(0), m_propOld(0) { }
1980 void Init( wxSizerItem
*item
, int propOld
) { m_item
=item
; m_propOld
=propOld
; }
1982 wxSizerItem
*m_item
;
1986 IMPLEMENT_DYNAMIC_CLASS(wxWrapSizer
, wxBoxSizer
);
1988 wxWrapSizer::wxWrapSizer( int orient
, int flags
)
1989 : wxBoxSizer(orient
),
1990 m_prim_size_last( -1 ),
1991 m_rows(orient
^wxBOTH
),
1996 wxWrapSizer::~wxWrapSizer()
1998 // Have to clear grand child items so that they're not deleted twice
1999 for( int ix
=m_rows
.GetChildren().GetCount()-1; ix
>=0; ix
-- )
2001 wxSizer
*psz
= m_rows
.GetItem((size_t)ix
)->GetSizer();
2002 wxSizerItemList
&sl
= psz
->GetChildren();
2003 while( sl
.GetLast() )
2004 sl
.Erase( sl
.GetLast() );
2009 bool wxWrapSizer::InformFirstDirection( int direction
, int size
, int WXUNUSED(availableOtherDir
) )
2013 // Better to keep value, then CalcMin will work better
2014 //m_prim_size_last = -1;
2017 if( direction
==m_orient
)
2019 // The direction is same as our primary, so we can make use of it
2020 m_prim_size_last
= size
;
2028 void wxWrapSizer::AdjustPropLastItem(wxSizer
*psz
, wxSizerItem
*itemLast
)
2030 wxSizerItem
*psi
= m_rows
.GetItem(psz
);
2032 wxPropHolder
*pph
= (wxPropHolder
*)psi
->GetUserData();
2034 psi
->SetUserData( pph
=new wxPropHolder
);
2036 pph
->Init( itemLast
, itemLast
->GetProportion() );
2037 itemLast
->SetProportion( wxDEFAULT_PROPORTION_LAST
);
2040 void wxWrapSizer::RecalcSizes()
2042 wxASSERT( m_orient
&wxBOTH
);
2043 if (m_children
.GetCount() == 0)
2046 // What we do here is to put our items into child box sizers,
2047 // as many of them as we have lines.
2049 // Empty all items in all rows in owned sizer.
2050 // We have to access the list directly, since we don't want to
2051 // destroy the wxSizerItems.
2052 for( int ix
=m_rows
.GetChildren().GetCount()-1; ix
>=0; ix
-- ){
2053 wxSizerItem
*psi
= m_rows
.GetItem( (size_t)ix
);
2055 // Restore proportion for last item on line (if item has not been deleted)
2056 wxPropHolder
*pph
= (wxPropHolder
*)psi
->GetUserData();
2057 if( pph
&& GetChildren().Find(pph
->m_item
) )
2058 pph
->m_item
->SetProportion(pph
->m_propOld
);
2060 wxSizer
*psz
= psi
->GetSizer();
2062 wxSizerItemList
&sl
= psz
->GetChildren();
2063 while( sl
.GetLast() )
2064 sl
.Erase( sl
.GetLast() );
2067 int lineSumMajor
= 0;
2068 int majorSize
= GetSizeInMajorDir(m_size
);
2070 // Make sure we have at least one child sizer
2072 if( !m_rows
.GetChildren().GetCount() )
2073 m_rows
.Add( new wxBoxSizer(GetOrientation()), 1, wxEXPAND
);
2075 // The sizer where to insert items in
2076 wxSizer
*psz
= m_rows
.GetItem((size_t)0)->GetSizer();
2079 // Now put our child items into child sizers instead
2080 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
2081 wxSizerItem
*item
= NULL
, *itemLast
=NULL
;
2084 item
= node
->GetData();
2085 if ( item
->IsShown() )
2087 wxSize minSz
= item
->GetMinSize();
2088 int minSzMajor
= GetSizeInMajorDir(minSz
);
2090 // More space on this line?
2091 if( !lineSumMajor
|| lineSumMajor
+minSzMajor
<=majorSize
)
2093 lineSumMajor
+= minSzMajor
;
2097 lineSumMajor
= minSzMajor
;
2098 // Get a new empty sizer to insert into
2099 if( (int)m_rows
.GetChildren().GetCount()<=m_n_line
)
2100 m_rows
.Add( new wxBoxSizer(GetOrientation()), 1, wxEXPAND
);
2102 // If we have extend-last-on-each-line mode, then do so now
2103 // Note: We must store old proportion value then.
2104 if( m_flags
&wxEXTEND_LAST_ON_EACH_LINE
)
2105 AdjustPropLastItem(psz
,itemLast
);
2107 // The sizer where to insert items in
2108 psz
= m_rows
.GetItem(m_n_line
++)->GetSizer();
2112 // If item is a window, it now has a pointer to the child sizer,
2113 // which is wrong. Set it to point to us.
2114 if( item
->GetWindow() )
2115 item
->GetWindow()->SetContainingSizer( this );
2117 node
= node
->GetNext();
2120 // If we have extend-last-on-each-line mode, then do so now
2121 if( m_flags
&wxEXTEND_LAST_ON_EACH_LINE
)
2122 AdjustPropLastItem(psz
,itemLast
);
2124 // If we have more sizers than lines, remove them
2125 while( (int)m_rows
.GetChildren().GetCount()>m_n_line
)
2126 m_rows
.Remove( m_n_line
);
2128 // Now do layout on row sizer
2129 m_rows
.SetDimension( m_position
.x
, m_position
.y
, m_size
.x
, m_size
.y
);
2131 // Remember this to next time (will be overridden by InformFirstDirection if used)
2132 m_prim_size_last
= GetSizeInMajorDir(m_size
);
2136 wxSize
wxWrapSizer::CalcMin()
2138 if (m_children
.GetCount() == 0)
2141 // Algorithm for calculating min size: (assuming horizontal orientation)
2142 // X: Max width of all members
2143 // Y: Based on last X, calculate how many lines needed
2144 // First time around, assume all items fits on one line
2148 int lineMaxMinor
= 0;
2149 int lineSumMajor
= 0;
2152 // precalc item minsizes and fit on lines (preliminary)
2153 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
2156 wxSizerItem
*item
= node
->GetData();
2157 if ( item
->IsShown() )
2159 wxSize minSz
= item
->CalcMin();
2160 int szMajor
= GetSizeInMajorDir(minSz
);
2161 int szMinor
= GetSizeInMinorDir(minSz
);
2162 if( szMajor
>maxMajor
) maxMajor
= szMajor
;
2163 // More space on this line?
2164 if( m_prim_size_last
<0 || !lineSumMajor
||
2165 lineSumMajor
+szMajor
<=m_prim_size_last
)
2167 lineSumMajor
+= szMajor
;
2168 if( szMinor
>lineMaxMinor
)
2169 lineMaxMinor
= szMinor
;
2173 minorSum
+= lineMaxMinor
; // Add height of highest item on last line
2175 lineMaxMinor
= szMinor
;
2176 lineSumMajor
= szMajor
;
2179 node
= node
->GetNext();
2181 minorSum
+= lineMaxMinor
; // Add height of highest item on last line
2183 m_minSize
= SizeFromMajorMinor(maxMajor
, minorSum
);
2187 //---------------------------------------------------------------------------
2189 //---------------------------------------------------------------------------
2193 wxStaticBoxSizer::wxStaticBoxSizer( wxStaticBox
*box
, int orient
)
2194 : wxBoxSizer( orient
),
2197 wxASSERT_MSG( box
, wxT("wxStaticBoxSizer needs a static box") );
2199 // do this so that our Detach() is called if the static box is destroyed
2201 m_staticBox
->SetContainingSizer(this);
2204 wxStaticBoxSizer::wxStaticBoxSizer(int orient
, wxWindow
*win
, const wxString
& s
)
2205 : wxBoxSizer(orient
),
2206 m_staticBox(new wxStaticBox(win
, wxID_ANY
, s
))
2209 m_staticBox
->SetContainingSizer(this);
2212 wxStaticBoxSizer::~wxStaticBoxSizer()
2217 static void GetStaticBoxBorders( wxStaticBox
*box
,
2221 // this has to be done platform by platform as there is no way to
2222 // guess the thickness of a wxStaticBox border
2223 box
->GetBordersForSizer(borderTop
, borderOther
);
2226 void wxStaticBoxSizer::RecalcSizes()
2228 int top_border
, other_border
;
2229 GetStaticBoxBorders(m_staticBox
, &top_border
, &other_border
);
2231 m_staticBox
->SetSize( m_position
.x
, m_position
.y
, m_size
.x
, m_size
.y
);
2233 wxPoint
old_pos( m_position
);
2234 m_position
.x
+= other_border
;
2235 m_position
.y
+= top_border
;
2236 wxSize
old_size( m_size
);
2237 m_size
.x
-= 2*other_border
;
2238 m_size
.y
-= top_border
+ other_border
;
2240 wxBoxSizer::RecalcSizes();
2242 m_position
= old_pos
;
2246 wxSize
wxStaticBoxSizer::CalcMin()
2248 int top_border
, other_border
;
2249 GetStaticBoxBorders(m_staticBox
, &top_border
, &other_border
);
2251 wxSize
ret( wxBoxSizer::CalcMin() );
2252 ret
.x
+= 2*other_border
;
2253 ret
.y
+= other_border
+ top_border
;
2258 void wxStaticBoxSizer::ShowItems( bool show
)
2260 m_staticBox
->Show( show
);
2261 wxBoxSizer::ShowItems( show
);
2264 bool wxStaticBoxSizer::Detach( wxWindow
*window
)
2266 // avoid deleting m_staticBox in our dtor if it's being detached from the
2267 // sizer (which can happen because it's being already destroyed for
2269 if ( window
== m_staticBox
)
2275 return wxSizer::Detach( window
);
2278 #endif // wxUSE_STATBOX
2282 wxStdDialogButtonSizer::wxStdDialogButtonSizer()
2283 : wxBoxSizer(wxHORIZONTAL
)
2285 // Vertical buttons with lots of space on either side
2286 // looks rubbish on WinCE, so let's not do this for now.
2287 // If we are going to use vertical buttons, we should
2288 // put the sizer to the right of other controls in the dialog,
2289 // and that's beyond the scope of this sizer.
2291 bool is_pda
= (wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA
);
2292 // If we have a PDA screen, put yes/no button over
2293 // all other buttons, otherwise on the left side.
2295 m_orient
= wxVERTICAL
;
2298 m_buttonAffirmative
= NULL
;
2299 m_buttonApply
= NULL
;
2300 m_buttonNegative
= NULL
;
2301 m_buttonCancel
= NULL
;
2302 m_buttonHelp
= NULL
;
2305 void wxStdDialogButtonSizer::AddButton(wxButton
*mybutton
)
2307 switch (mybutton
->GetId())
2312 m_buttonAffirmative
= mybutton
;
2315 m_buttonApply
= mybutton
;
2318 m_buttonNegative
= mybutton
;
2322 m_buttonCancel
= mybutton
;
2325 case wxID_CONTEXT_HELP
:
2326 m_buttonHelp
= mybutton
;
2333 void wxStdDialogButtonSizer::SetAffirmativeButton( wxButton
*button
)
2335 m_buttonAffirmative
= button
;
2338 void wxStdDialogButtonSizer::SetNegativeButton( wxButton
*button
)
2340 m_buttonNegative
= button
;
2343 void wxStdDialogButtonSizer::SetCancelButton( wxButton
*button
)
2345 m_buttonCancel
= button
;
2348 void wxStdDialogButtonSizer::Realize()
2351 Add(0, 0, 0, wxLEFT
, 6);
2353 Add((wxWindow
*)m_buttonHelp
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, 6);
2355 if (m_buttonNegative
){
2356 // HIG POLICE BULLETIN - destructive buttons need extra padding
2357 // 24 pixels on either side
2358 Add((wxWindow
*)m_buttonNegative
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, 12);
2361 // extra whitespace between help/negative and cancel/ok buttons
2362 Add(0, 0, 1, wxEXPAND
, 0);
2364 if (m_buttonCancel
){
2365 Add((wxWindow
*)m_buttonCancel
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, 6);
2366 // Cancel or help should be default
2367 // m_buttonCancel->SetDefaultButton();
2370 // Ugh, Mac doesn't really have apply dialogs, so I'll just
2371 // figure the best place is between Cancel and OK
2373 Add((wxWindow
*)m_buttonApply
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, 6);
2375 if (m_buttonAffirmative
){
2376 Add((wxWindow
*)m_buttonAffirmative
, 0, wxALIGN_CENTRE
| wxLEFT
, 6);
2378 if (m_buttonAffirmative
->GetId() == wxID_SAVE
){
2379 // these buttons have set labels under Mac so we should use them
2380 m_buttonAffirmative
->SetLabel(_("Save"));
2381 if (m_buttonNegative
)
2382 m_buttonNegative
->SetLabel(_("Don't Save"));
2386 // Extra space around and at the right
2388 #elif defined(__WXGTK20__)
2389 Add(0, 0, 0, wxLEFT
, 9);
2391 Add((wxWindow
*)m_buttonHelp
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, 3);
2393 // extra whitespace between help and cancel/ok buttons
2394 Add(0, 0, 1, wxEXPAND
, 0);
2396 if (m_buttonNegative
){
2397 Add((wxWindow
*)m_buttonNegative
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, 3);
2400 // according to HIG, in explicit apply windows the order is:
2401 // [ Help Apply Cancel OK ]
2403 Add((wxWindow
*)m_buttonApply
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, 3);
2405 if (m_buttonCancel
){
2406 Add((wxWindow
*)m_buttonCancel
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, 3);
2407 // Cancel or help should be default
2408 // m_buttonCancel->SetDefaultButton();
2411 if (m_buttonAffirmative
)
2412 Add((wxWindow
*)m_buttonAffirmative
, 0, wxALIGN_CENTRE
| wxLEFT
, 6);
2413 #elif defined(__WXMSW__)
2416 // right-justify buttons
2417 Add(0, 0, 1, wxEXPAND
, 0);
2419 if (m_buttonAffirmative
){
2420 Add((wxWindow
*)m_buttonAffirmative
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, m_buttonAffirmative
->ConvertDialogToPixels(wxSize(2, 0)).x
);
2423 if (m_buttonNegative
){
2424 Add((wxWindow
*)m_buttonNegative
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, m_buttonNegative
->ConvertDialogToPixels(wxSize(2, 0)).x
);
2427 if (m_buttonCancel
){
2428 Add((wxWindow
*)m_buttonCancel
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, m_buttonCancel
->ConvertDialogToPixels(wxSize(2, 0)).x
);
2431 Add((wxWindow
*)m_buttonApply
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, m_buttonApply
->ConvertDialogToPixels(wxSize(2, 0)).x
);
2434 Add((wxWindow
*)m_buttonHelp
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, m_buttonHelp
->ConvertDialogToPixels(wxSize(2, 0)).x
);
2436 // GTK+1 and any other platform
2438 // Add(0, 0, 0, wxLEFT, 5); // Not sure what this was for but it unbalances the dialog
2440 Add((wxWindow
*)m_buttonHelp
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, m_buttonHelp
->ConvertDialogToPixels(wxSize(4, 0)).x
);
2442 // extra whitespace between help and cancel/ok buttons
2443 Add(0, 0, 1, wxEXPAND
, 0);
2446 Add((wxWindow
*)m_buttonApply
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, m_buttonApply
->ConvertDialogToPixels(wxSize(4, 0)).x
);
2448 if (m_buttonAffirmative
){
2449 Add((wxWindow
*)m_buttonAffirmative
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, m_buttonAffirmative
->ConvertDialogToPixels(wxSize(4, 0)).x
);
2452 if (m_buttonNegative
){
2453 Add((wxWindow
*)m_buttonNegative
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, m_buttonNegative
->ConvertDialogToPixels(wxSize(4, 0)).x
);
2456 if (m_buttonCancel
){
2457 Add((wxWindow
*)m_buttonCancel
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, m_buttonCancel
->ConvertDialogToPixels(wxSize(4, 0)).x
);
2458 // Cancel or help should be default
2459 // m_buttonCancel->SetDefaultButton();
2465 #endif // wxUSE_BUTTON