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
= GetMinWindowSize(window
);
842 wxTopLevelWindow
*tlw
= wxDynamicCast(window
, wxTopLevelWindow
);
845 // hack for small screen devices where TLWs are always full screen
846 if ( tlw
->IsAlwaysMaximized() )
849 return tlw
->GetSize();
852 // limit the window to the size of the display it is on
853 int disp
= wxDisplay::GetFromWindow(window
);
854 if ( disp
== wxNOT_FOUND
)
856 // or, if we don't know which one it is, of the main one
860 wxSize sizeMax
= wxDisplay(disp
).GetClientArea().GetSize();
862 // space for decorations and toolbars etc.
863 wxSize tlw_client_size
= tlw
->GetClientSize();
864 wxSize tlw_size
= tlw
->GetSize();
865 sizeMax
.x
-= tlw_size
.x
- tlw_client_size
.x
;
866 sizeMax
.y
-= tlw_size
.y
- tlw_client_size
.y
;
868 if ( sizeMax
.x
!= wxDefaultCoord
&& size
.x
> sizeMax
.x
)
870 if ( sizeMax
.y
!= wxDefaultCoord
&& size
.y
> sizeMax
.y
)
874 tlw
->SetClientSize( size
);
876 // return entire size
877 return tlw
->GetSize();
881 wxSize sizeMax
= GetMaxWindowSize(window
);
883 if ( sizeMax
.x
!= wxDefaultCoord
&& size
.x
> sizeMax
.x
)
885 if ( sizeMax
.y
!= wxDefaultCoord
&& size
.y
> sizeMax
.y
)
889 window
->SetClientSize( size
);
891 // return entire size
892 return window
->GetSize();
896 void wxSizer::FitInside( wxWindow
*window
)
899 if (window
->IsTopLevel())
900 size
= VirtualFitSize( window
);
902 size
= GetMinClientSize( window
);
904 window
->SetVirtualSize( size
);
907 void wxSizer::Layout()
909 // (re)calculates minimums needed for each item and other preparations
913 // Applies the layout and repositions/resizes the items
917 void wxSizer::SetSizeHints( wxWindow
*window
)
919 // Preserve the window's max size hints, but set the
920 // lower bound according to the sizer calculations.
922 wxSize size
= Fit( window
);
924 window
->SetSizeHints( size
.x
,
926 window
->GetMaxWidth(),
927 window
->GetMaxHeight() );
930 #if WXWIN_COMPATIBILITY_2_8
931 void wxSizer::SetVirtualSizeHints( wxWindow
*window
)
935 #endif // WXWIN_COMPATIBILITY_2_8
937 wxSize
wxSizer::GetMaxWindowSize( wxWindow
*window
) const
939 return window
->GetMaxSize();
942 wxSize
wxSizer::GetMinWindowSize( wxWindow
*window
)
944 wxSize
minSize( GetMinSize() );
945 wxSize
size( window
->GetSize() );
946 wxSize
client_size( window
->GetClientSize() );
948 return wxSize( minSize
.x
+size
.x
-client_size
.x
,
949 minSize
.y
+size
.y
-client_size
.y
);
952 // TODO on mac we need a function that determines how much free space this
953 // min size contains, in order to make sure that we have 20 pixels of free
954 // space around the controls
955 wxSize
wxSizer::GetMaxClientSize( wxWindow
*window
) const
957 wxSize
maxSize( window
->GetMaxSize() );
959 if ( maxSize
!= wxDefaultSize
)
961 wxSize
size( window
->GetSize() );
962 wxSize
client_size( window
->GetClientSize() );
964 return wxSize( maxSize
.x
+ client_size
.x
- size
.x
,
965 maxSize
.y
+ client_size
.y
- size
.y
);
968 return wxDefaultSize
;
971 wxSize
wxSizer::GetMinClientSize( wxWindow
*WXUNUSED(window
) )
973 return GetMinSize(); // Already returns client size.
976 wxSize
wxSizer::VirtualFitSize( wxWindow
*window
)
978 wxSize size
= GetMinClientSize( window
);
979 wxSize sizeMax
= GetMaxClientSize( window
);
981 // Limit the size if sizeMax != wxDefaultSize
983 if ( size
.x
> sizeMax
.x
&& sizeMax
.x
!= wxDefaultCoord
)
985 if ( size
.y
> sizeMax
.y
&& sizeMax
.y
!= wxDefaultCoord
)
991 void wxSizer::SetDimension( int x
, int y
, int width
, int height
)
1000 wxSize
wxSizer::GetMinSize()
1002 wxSize
ret( CalcMin() );
1003 if (ret
.x
< m_minSize
.x
) ret
.x
= m_minSize
.x
;
1004 if (ret
.y
< m_minSize
.y
) ret
.y
= m_minSize
.y
;
1008 void wxSizer::DoSetMinSize( int width
, int height
)
1010 m_minSize
.x
= width
;
1011 m_minSize
.y
= height
;
1014 bool wxSizer::DoSetItemMinSize( wxWindow
*window
, int width
, int height
)
1016 wxASSERT_MSG( window
, _T("SetMinSize for NULL window") );
1018 // Is it our immediate child?
1020 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
1023 wxSizerItem
*item
= node
->GetData();
1025 if (item
->GetWindow() == window
)
1027 item
->SetMinSize( width
, height
);
1030 node
= node
->GetNext();
1033 // No? Search any subsizers we own then
1035 node
= m_children
.GetFirst();
1038 wxSizerItem
*item
= node
->GetData();
1040 if ( item
->GetSizer() &&
1041 item
->GetSizer()->DoSetItemMinSize( window
, width
, height
) )
1043 // A child sizer found the requested windw, exit.
1046 node
= node
->GetNext();
1052 bool wxSizer::DoSetItemMinSize( wxSizer
*sizer
, int width
, int height
)
1054 wxASSERT_MSG( sizer
, _T("SetMinSize for NULL sizer") );
1056 // Is it our immediate child?
1058 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
1061 wxSizerItem
*item
= node
->GetData();
1063 if (item
->GetSizer() == sizer
)
1065 item
->GetSizer()->DoSetMinSize( width
, height
);
1068 node
= node
->GetNext();
1071 // No? Search any subsizers we own then
1073 node
= m_children
.GetFirst();
1076 wxSizerItem
*item
= node
->GetData();
1078 if ( item
->GetSizer() &&
1079 item
->GetSizer()->DoSetItemMinSize( sizer
, width
, height
) )
1081 // A child found the requested sizer, exit.
1084 node
= node
->GetNext();
1090 bool wxSizer::DoSetItemMinSize( size_t index
, int width
, int height
)
1092 wxSizerItemList::compatibility_iterator node
= m_children
.Item( index
);
1094 wxCHECK_MSG( node
, false, _T("Failed to find child node") );
1096 wxSizerItem
*item
= node
->GetData();
1098 if (item
->GetSizer())
1100 // Sizers contains the minimal size in them, if not calculated ...
1101 item
->GetSizer()->DoSetMinSize( width
, height
);
1105 // ... but the minimal size of spacers and windows is stored via the item
1106 item
->SetMinSize( width
, height
);
1112 wxSizerItem
* wxSizer::GetItem( wxWindow
*window
, bool recursive
)
1114 wxASSERT_MSG( window
, _T("GetItem for NULL window") );
1116 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
1119 wxSizerItem
*item
= node
->GetData();
1121 if (item
->GetWindow() == window
)
1125 else if (recursive
&& item
->IsSizer())
1127 wxSizerItem
*subitem
= item
->GetSizer()->GetItem( window
, true );
1132 node
= node
->GetNext();
1138 wxSizerItem
* wxSizer::GetItem( wxSizer
*sizer
, bool recursive
)
1140 wxASSERT_MSG( sizer
, _T("GetItem for NULL sizer") );
1142 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
1145 wxSizerItem
*item
= node
->GetData();
1147 if (item
->GetSizer() == sizer
)
1151 else if (recursive
&& item
->IsSizer())
1153 wxSizerItem
*subitem
= item
->GetSizer()->GetItem( sizer
, true );
1158 node
= node
->GetNext();
1164 wxSizerItem
* wxSizer::GetItem( size_t index
)
1166 wxCHECK_MSG( index
< m_children
.GetCount(),
1168 _T("GetItem index is out of range") );
1170 return m_children
.Item( index
)->GetData();
1173 wxSizerItem
* wxSizer::GetItemById( int id
, bool recursive
)
1175 // This gets a sizer item by the id of the sizer item
1176 // and NOT the id of a window if the item is a window.
1178 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
1181 wxSizerItem
*item
= node
->GetData();
1183 if (item
->GetId() == id
)
1187 else if (recursive
&& item
->IsSizer())
1189 wxSizerItem
*subitem
= item
->GetSizer()->GetItemById( id
, true );
1194 node
= node
->GetNext();
1200 bool wxSizer::Show( wxWindow
*window
, bool show
, bool recursive
)
1202 wxSizerItem
*item
= GetItem( window
, recursive
);
1213 bool wxSizer::Show( wxSizer
*sizer
, bool show
, bool recursive
)
1215 wxSizerItem
*item
= GetItem( sizer
, recursive
);
1226 bool wxSizer::Show( size_t index
, bool show
)
1228 wxSizerItem
*item
= GetItem( index
);
1239 void wxSizer::ShowItems( bool show
)
1241 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
1244 node
->GetData()->Show( show
);
1245 node
= node
->GetNext();
1249 bool wxSizer::IsShown( wxWindow
*window
) const
1251 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
1254 wxSizerItem
*item
= node
->GetData();
1256 if (item
->GetWindow() == window
)
1258 return item
->IsShown();
1260 node
= node
->GetNext();
1263 wxFAIL_MSG( _T("IsShown failed to find sizer item") );
1268 bool wxSizer::IsShown( wxSizer
*sizer
) const
1270 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
1273 wxSizerItem
*item
= node
->GetData();
1275 if (item
->GetSizer() == sizer
)
1277 return item
->IsShown();
1279 node
= node
->GetNext();
1282 wxFAIL_MSG( _T("IsShown failed to find sizer item") );
1287 bool wxSizer::IsShown( size_t index
) const
1289 wxCHECK_MSG( index
< m_children
.GetCount(),
1291 _T("IsShown index is out of range") );
1293 return m_children
.Item( index
)->GetData()->IsShown();
1297 //---------------------------------------------------------------------------
1299 //---------------------------------------------------------------------------
1301 wxGridSizer::wxGridSizer( int rows
, int cols
, int vgap
, int hgap
)
1302 : m_rows( ( cols
== 0 && rows
== 0 ) ? 1 : rows
)
1309 wxGridSizer::wxGridSizer( int cols
, int vgap
, int hgap
)
1310 : m_rows( cols
== 0 ? 1 : 0 )
1317 int wxGridSizer::CalcRowsCols(int& nrows
, int& ncols
) const
1319 int nitems
= m_children
.GetCount();
1325 nrows
= (nitems
+ m_cols
- 1) / m_cols
;
1329 ncols
= (nitems
+ m_rows
- 1) / m_rows
;
1332 else // 0 columns, 0 rows?
1334 wxFAIL_MSG( _T("grid sizer must have either rows or columns fixed") );
1343 void wxGridSizer::RecalcSizes()
1345 int nitems
, nrows
, ncols
;
1346 if ( (nitems
= CalcRowsCols(nrows
, ncols
)) == 0 )
1349 wxSize
sz( GetSize() );
1350 wxPoint
pt( GetPosition() );
1352 int w
= (sz
.x
- (ncols
- 1) * m_hgap
) / ncols
;
1353 int h
= (sz
.y
- (nrows
- 1) * m_vgap
) / nrows
;
1356 for (int c
= 0; c
< ncols
; c
++)
1359 for (int r
= 0; r
< nrows
; r
++)
1361 int i
= r
* ncols
+ c
;
1364 wxSizerItemList::compatibility_iterator node
= m_children
.Item( i
);
1366 wxASSERT_MSG( node
, _T("Failed to find SizerItemList node") );
1368 SetItemBounds( node
->GetData(), x
, y
, w
, h
);
1376 wxSize
wxGridSizer::CalcMin()
1379 if ( CalcRowsCols(nrows
, ncols
) == 0 )
1382 // Find the max width and height for any component
1386 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
1389 wxSizerItem
*item
= node
->GetData();
1390 wxSize
sz( item
->CalcMin() );
1392 w
= wxMax( w
, sz
.x
);
1393 h
= wxMax( h
, sz
.y
);
1395 node
= node
->GetNext();
1398 // In case we have a nested sizer with a two step algo , give it
1399 // a chance to adjust to that (we give it width component)
1400 node
= m_children
.GetFirst();
1401 bool didChangeMinSize
= false;
1404 wxSizerItem
*item
= node
->GetData();
1405 didChangeMinSize
|= item
->InformFirstDirection( wxHORIZONTAL
, w
, -1 );
1407 node
= node
->GetNext();
1410 // And redo iteration in case min size changed
1411 if( didChangeMinSize
)
1413 node
= m_children
.GetFirst();
1417 wxSizerItem
*item
= node
->GetData();
1418 wxSize
sz( item
->GetMinSizeWithBorder() );
1420 w
= wxMax( w
, sz
.x
);
1421 h
= wxMax( h
, sz
.y
);
1423 node
= node
->GetNext();
1427 return wxSize( ncols
* w
+ (ncols
-1) * m_hgap
,
1428 nrows
* h
+ (nrows
-1) * m_vgap
);
1431 void wxGridSizer::SetItemBounds( wxSizerItem
*item
, int x
, int y
, int w
, int h
)
1434 wxSize
sz( item
->GetMinSizeWithBorder() );
1435 int flag
= item
->GetFlag();
1437 if ((flag
& wxEXPAND
) || (flag
& wxSHAPED
))
1443 if (flag
& wxALIGN_CENTER_HORIZONTAL
)
1445 pt
.x
= x
+ (w
- sz
.x
) / 2;
1447 else if (flag
& wxALIGN_RIGHT
)
1449 pt
.x
= x
+ (w
- sz
.x
);
1452 if (flag
& wxALIGN_CENTER_VERTICAL
)
1454 pt
.y
= y
+ (h
- sz
.y
) / 2;
1456 else if (flag
& wxALIGN_BOTTOM
)
1458 pt
.y
= y
+ (h
- sz
.y
);
1462 item
->SetDimension(pt
, sz
);
1465 //---------------------------------------------------------------------------
1467 //---------------------------------------------------------------------------
1469 wxFlexGridSizer::wxFlexGridSizer( int rows
, int cols
, int vgap
, int hgap
)
1470 : wxGridSizer( rows
, cols
, vgap
, hgap
),
1471 m_flexDirection(wxBOTH
),
1472 m_growMode(wxFLEX_GROWMODE_SPECIFIED
)
1476 wxFlexGridSizer::wxFlexGridSizer( int cols
, int vgap
, int hgap
)
1477 : wxGridSizer( cols
, vgap
, hgap
),
1478 m_flexDirection(wxBOTH
),
1479 m_growMode(wxFLEX_GROWMODE_SPECIFIED
)
1483 wxFlexGridSizer::~wxFlexGridSizer()
1487 void wxFlexGridSizer::RecalcSizes()
1490 if ( !CalcRowsCols(nrows
, ncols
) )
1493 const wxPoint
pt(GetPosition());
1494 const wxSize
sz(GetSize());
1496 AdjustForGrowables(sz
);
1498 wxSizerItemList::const_iterator i
= m_children
.begin();
1499 const wxSizerItemList::const_iterator end
= m_children
.end();
1502 for ( int r
= 0; r
< nrows
; r
++ )
1504 if ( m_rowHeights
[r
] == -1 )
1506 // this row is entirely hidden, skip it
1507 for ( int c
= 0; c
< ncols
; c
++ )
1518 const int hrow
= m_rowHeights
[r
];
1519 int h
= sz
.y
- y
; // max remaining height, don't overflow it
1524 for ( int c
= 0; c
< ncols
&& i
!= end
; c
++, ++i
)
1526 const int wcol
= m_colWidths
[c
];
1531 int w
= sz
.x
- x
; // max possible value, ensure we don't overflow
1535 SetItemBounds(*i
, pt
.x
+ x
, pt
.y
+ y
, w
, h
);
1547 // helper function used in CalcMin() to sum up the sizes of non-hidden items
1548 static int SumArraySizes(const wxArrayInt
& sizes
, int gap
)
1550 // Sum total minimum size, including gaps between rows/columns.
1551 // -1 is used as a magic number meaning empty row/column.
1554 const size_t count
= sizes
.size();
1555 for ( size_t n
= 0; n
< count
; n
++ )
1557 if ( sizes
[n
] != -1 )
1560 total
+= gap
; // separate from the previous column
1569 void wxFlexGridSizer::FindWidthsAndHeights(int nrows
, int ncols
)
1571 // We have to recalculate the sizes in case the item minimum size has
1572 // changed since the previous layout, or the item has been hidden using
1573 // wxSizer::Show(). If all the items in a row/column are hidden, the final
1574 // dimension of the row/column will be -1, indicating that the column
1575 // itself is hidden.
1576 m_rowHeights
.assign(nrows
, -1);
1577 m_colWidths
.assign(ncols
, -1);
1579 // n is the index of the item in left-to-right top-to-bottom order
1581 for ( wxSizerItemList::iterator i
= m_children
.begin();
1582 i
!= m_children
.end();
1585 wxSizerItem
* const item
= *i
;
1586 if ( item
->IsShown() )
1588 // NOTE: Not doing the calculation here, this is just
1589 // for finding max values.
1590 const wxSize
sz(item
->GetMinSizeWithBorder());
1592 const int row
= n
/ ncols
;
1593 const int col
= n
% ncols
;
1595 if ( sz
.y
> m_rowHeights
[row
] )
1596 m_rowHeights
[row
] = sz
.y
;
1597 if ( sz
.x
> m_colWidths
[col
] )
1598 m_colWidths
[col
] = sz
.x
;
1602 AdjustForFlexDirection();
1604 m_calculatedMinSize
= wxSize(SumArraySizes(m_colWidths
, m_hgap
),
1605 SumArraySizes(m_rowHeights
, m_vgap
));
1608 wxSize
wxFlexGridSizer::CalcMin()
1613 // Number of rows/columns can change as items are added or removed.
1614 if ( !CalcRowsCols(nrows
, ncols
) )
1618 // We have to recalculate the sizes in case the item minimum size has
1619 // changed since the previous layout, or the item has been hidden using
1620 // wxSizer::Show(). If all the items in a row/column are hidden, the final
1621 // dimension of the row/column will be -1, indicating that the column
1622 // itself is hidden.
1623 m_rowHeights
.assign(nrows
, -1);
1624 m_colWidths
.assign(ncols
, -1);
1626 // n is the index of the item in left-to-right top-to-bottom order
1628 for ( wxSizerItemList::iterator i
= m_children
.begin();
1629 i
!= m_children
.end();
1632 wxSizerItem
* const item
= *i
;
1633 if ( item
->IsShown() )
1639 // The stage of looking for max values in each row/column has been
1640 // made a separate function, since it's reused in AdjustForGrowables.
1641 FindWidthsAndHeights(nrows
,ncols
);
1643 return m_calculatedMinSize
;
1646 void wxFlexGridSizer::AdjustForFlexDirection()
1648 // the logic in CalcMin works when we resize flexibly in both directions
1649 // but maybe this is not the case
1650 if ( m_flexDirection
!= wxBOTH
)
1652 // select the array corresponding to the direction in which we do *not*
1654 wxArrayInt
& array
= m_flexDirection
== wxVERTICAL
? m_colWidths
1657 const size_t count
= array
.GetCount();
1659 // find the largest value in this array
1663 for ( n
= 0; n
< count
; ++n
)
1665 if ( array
[n
] > largest
)
1669 // and now fill it with the largest value
1670 for ( n
= 0; n
< count
; ++n
)
1672 // don't touch hidden rows
1673 if ( array
[n
] != -1 )
1679 // helper of AdjustForGrowables() which is called for rows/columns separately
1682 // delta: the extra space, we do nothing unless it's positive
1683 // growable: indices or growable rows/cols in sizes array
1684 // sizes: the height/widths of rows/cols to adjust
1685 // proportions: proportions of the growable rows/cols or NULL if they all
1686 // should be assumed to have proportion of 1
1688 DoAdjustForGrowables(int delta
,
1689 const wxArrayInt
& growable
,
1691 const wxArrayInt
*proportions
)
1696 // total sum of proportions of all non-hidden rows
1697 int sum_proportions
= 0;
1699 // number of currently shown growable rows
1702 const int max_idx
= sizes
.size();
1704 const size_t count
= growable
.size();
1706 for ( idx
= 0; idx
< count
; idx
++ )
1708 // Since the number of rows/columns can change as items are
1709 // inserted/deleted, we need to verify at runtime that the
1710 // requested growable rows/columns are still valid.
1711 if ( growable
[idx
] >= max_idx
)
1714 // If all items in a row/column are hidden, that row/column will
1715 // have a dimension of -1. This causes the row/column to be
1716 // hidden completely.
1717 if ( sizes
[growable
[idx
]] == -1 )
1721 sum_proportions
+= (*proportions
)[idx
];
1729 // the remaining extra free space, adjusted during each iteration
1730 for ( idx
= 0; idx
< count
; idx
++ )
1732 if ( growable
[idx
] >= max_idx
)
1735 if ( sizes
[ growable
[idx
] ] == -1 )
1739 if ( sum_proportions
== 0 )
1741 // no growable rows -- divide extra space evenly among all
1742 cur_delta
= delta
/num
;
1745 else // allocate extra space proportionally
1747 const int cur_prop
= (*proportions
)[idx
];
1748 cur_delta
= (delta
*cur_prop
)/sum_proportions
;
1749 sum_proportions
-= cur_prop
;
1752 sizes
[growable
[idx
]] += cur_delta
;
1757 void wxFlexGridSizer::AdjustForGrowables(const wxSize
& sz
)
1759 if ( (m_flexDirection
& wxHORIZONTAL
) || (m_growMode
!= wxFLEX_GROWMODE_NONE
) )
1761 DoAdjustForGrowables
1763 sz
.x
- m_calculatedMinSize
.x
,
1766 m_growMode
== wxFLEX_GROWMODE_SPECIFIED
? &m_growableColsProportions
1770 // This gives nested objects that benefit from knowing one size
1771 // component in advance the chance to use that.
1772 bool didAdjustMinSize
= false;
1774 CalcRowsCols(nrows
, ncols
);
1776 // Iterate over all items and inform about column width
1778 for ( wxSizerItemList::iterator i
= m_children
.begin();
1779 i
!= m_children
.end();
1782 const int col
= n
% ncols
;
1783 didAdjustMinSize
|= (*i
)->InformFirstDirection(wxHORIZONTAL
, m_colWidths
[col
], sz
.y
- m_calculatedMinSize
.y
);
1786 // Only redo if info was actually used
1787 if( didAdjustMinSize
)
1789 DoAdjustForGrowables
1791 sz
.x
- m_calculatedMinSize
.x
,
1794 m_growMode
== wxFLEX_GROWMODE_SPECIFIED
? &m_growableColsProportions
1800 if ( (m_flexDirection
& wxVERTICAL
) || (m_growMode
!= wxFLEX_GROWMODE_NONE
) )
1802 // pass NULL instead of proportions if the grow mode is ALL as we
1803 // should treat all rows as having proportion of 1 then
1804 DoAdjustForGrowables
1806 sz
.y
- m_calculatedMinSize
.y
,
1809 m_growMode
== wxFLEX_GROWMODE_SPECIFIED
? &m_growableRowsProportions
1816 void wxFlexGridSizer::AddGrowableRow( size_t idx
, int proportion
)
1818 m_growableRows
.Add( idx
);
1819 m_growableRowsProportions
.Add( proportion
);
1822 void wxFlexGridSizer::AddGrowableCol( size_t idx
, int proportion
)
1824 m_growableCols
.Add( idx
);
1825 m_growableColsProportions
.Add( proportion
);
1828 // helper function for RemoveGrowableCol/Row()
1830 DoRemoveFromArrays(size_t idx
, wxArrayInt
& items
, wxArrayInt
& proportions
)
1832 const size_t count
= items
.size();
1833 for ( size_t n
= 0; n
< count
; n
++ )
1835 if ( (size_t)items
[n
] == idx
)
1838 proportions
.RemoveAt(n
);
1843 wxFAIL_MSG( _T("column/row is already not growable") );
1846 void wxFlexGridSizer::RemoveGrowableCol( size_t idx
)
1848 DoRemoveFromArrays(idx
, m_growableCols
, m_growableColsProportions
);
1851 void wxFlexGridSizer::RemoveGrowableRow( size_t idx
)
1853 DoRemoveFromArrays(idx
, m_growableRows
, m_growableRowsProportions
);
1856 //---------------------------------------------------------------------------
1858 //---------------------------------------------------------------------------
1860 void wxBoxSizer::RecalcSizes()
1862 if ( m_children
.empty() )
1865 const wxCoord totalMinorSize
= GetSizeInMinorDir(m_size
);
1867 // the amount of free space which we should redistribute among the
1868 // stretchable items (i.e. those with non zero proportion)
1869 int delta
= GetSizeInMajorDir(m_size
) - GetSizeInMajorDir(m_minSize
);
1872 // Inform child items about the size in minor direction, that can
1873 // change how much free space we have in major dir and how to distribute it.
1874 int majorMinSum
= 0;
1875 wxSizerItemList::const_iterator i
;
1876 for ( i
= m_children
.begin();
1877 i
!= m_children
.end();
1880 wxSizerItem
* const item
= *i
;
1882 if ( !item
->IsShown() )
1885 wxSize szMinPrev
= item
->GetMinSizeWithBorder();
1886 item
->InformFirstDirection(m_orient
^wxBOTH
,totalMinorSize
,delta
);
1887 wxSize szMin
= item
->GetMinSizeWithBorder();
1888 int deltaChange
= GetSizeInMajorDir(szMin
-szMinPrev
);
1891 // Since we passed available space along to the item, it should not
1892 // take too much, so delta should not become negative.
1893 delta
-= deltaChange
;
1895 majorMinSum
+= GetSizeInMajorDir(item
->GetMinSizeWithBorder());
1897 // And update our min size
1898 SizeInMajorDir(m_minSize
) = majorMinSum
;
1901 // might have a new delta now
1902 delta
= GetSizeInMajorDir(m_size
) - GetSizeInMajorDir(m_minSize
);
1904 // the position at which we put the next child
1905 wxPoint
pt(m_position
);
1907 int totalProportion
= m_totalProportion
;
1908 for ( i
= m_children
.begin();
1909 i
!= m_children
.end();
1912 wxSizerItem
* const item
= *i
;
1914 if ( !item
->IsShown() )
1917 const wxSize
sizeThis(item
->GetMinSizeWithBorder());
1919 // adjust the size in the major direction using the proportion
1920 wxCoord majorSize
= GetSizeInMajorDir(sizeThis
);
1921 const int propItem
= item
->GetProportion();
1924 const int deltaItem
= (delta
* propItem
) / totalProportion
;
1926 majorSize
+= deltaItem
;
1929 totalProportion
-= propItem
;
1933 // apply the alignment in the minor direction
1934 wxPoint
posChild(pt
);
1936 wxCoord minorSize
= GetSizeInMinorDir(sizeThis
);
1937 const int flag
= item
->GetFlag();
1938 if ( flag
& (wxEXPAND
| wxSHAPED
) )
1940 minorSize
= totalMinorSize
;
1942 else if ( flag
& (IsVertical() ? wxALIGN_RIGHT
: wxALIGN_BOTTOM
) )
1944 PosInMinorDir(posChild
) += totalMinorSize
- minorSize
;
1946 // NB: wxCENTRE is used here only for backwards compatibility,
1947 // wxALIGN_CENTRE should be used in new code
1948 else if ( flag
& (wxCENTER
| wxALIGN_CENTRE
) )
1950 PosInMinorDir(posChild
) += (totalMinorSize
- minorSize
) / 2;
1954 // apply RTL adjustment for horizontal sizers:
1955 if ( !IsVertical() && m_containingWindow
)
1957 posChild
.x
= m_containingWindow
->AdjustForLayoutDirection
1965 // finally set size of this child and advance to the next one
1966 item
->SetDimension(posChild
, SizeFromMajorMinor(majorSize
, minorSize
));
1968 PosInMajorDir(pt
) += majorSize
;
1972 wxSize
wxBoxSizer::CalcMin()
1974 m_totalProportion
= 0;
1975 m_minSize
= wxSize(0, 0);
1977 // calculate the minimal sizes for all items and count sum of proportions
1978 for ( wxSizerItemList::const_iterator i
= m_children
.begin();
1979 i
!= m_children
.end();
1982 wxSizerItem
* const item
= *i
;
1984 if ( !item
->IsShown() )
1987 const wxSize sizeMinThis
= item
->CalcMin();
1988 SizeInMajorDir(m_minSize
) += GetSizeInMajorDir(sizeMinThis
);
1989 if ( GetSizeInMinorDir(sizeMinThis
) > GetSizeInMinorDir(m_minSize
) )
1990 SizeInMinorDir(m_minSize
) = GetSizeInMinorDir(sizeMinThis
);
1992 m_totalProportion
+= item
->GetProportion();
1998 //---------------------------------------------------------------------------
2000 //---------------------------------------------------------------------------
2002 #define wxDEFAULT_PROPORTION_LAST 1000000
2004 // User data to hold old proportion for last item on line
2005 // (which might be extended)
2006 struct wxPropHolder
: public wxObject
2008 wxPropHolder( ) : m_item(0), m_propOld(0) { }
2009 void Init( wxSizerItem
*item
, int propOld
) { m_item
=item
; m_propOld
=propOld
; }
2011 wxSizerItem
*m_item
;
2015 IMPLEMENT_DYNAMIC_CLASS(wxWrapSizer
, wxBoxSizer
);
2017 wxWrapSizer::wxWrapSizer( int orient
, int flags
)
2018 : wxBoxSizer(orient
),
2019 m_prim_size_last( -1 ),
2020 m_rows(orient
^wxBOTH
),
2025 wxWrapSizer::~wxWrapSizer()
2027 // Have to clear grand child items so that they're not deleted twice
2028 for( int ix
=m_rows
.GetChildren().GetCount()-1; ix
>=0; ix
-- )
2030 wxSizer
*psz
= m_rows
.GetItem((size_t)ix
)->GetSizer();
2031 wxSizerItemList
&sl
= psz
->GetChildren();
2032 while( sl
.GetLast() )
2033 sl
.Erase( sl
.GetLast() );
2038 bool wxWrapSizer::InformFirstDirection( int direction
, int size
, int WXUNUSED(availableOtherDir
) )
2042 // Better to keep value, then CalcMin will work better
2043 //m_prim_size_last = -1;
2046 if( direction
==m_orient
)
2048 // The direction is same as our primary, so we can make use of it
2049 m_prim_size_last
= size
;
2057 void wxWrapSizer::AdjustPropLastItem(wxSizer
*psz
, wxSizerItem
*itemLast
)
2059 wxSizerItem
*psi
= m_rows
.GetItem(psz
);
2061 wxPropHolder
*pph
= (wxPropHolder
*)psi
->GetUserData();
2063 psi
->SetUserData( pph
=new wxPropHolder
);
2065 pph
->Init( itemLast
, itemLast
->GetProportion() );
2066 itemLast
->SetProportion( wxDEFAULT_PROPORTION_LAST
);
2069 void wxWrapSizer::RecalcSizes()
2071 wxASSERT( m_orient
&wxBOTH
);
2072 if (m_children
.GetCount() == 0)
2075 // What we do here is to put our items into child box sizers,
2076 // as many of them as we have lines.
2078 // Empty all items in all rows in owned sizer.
2079 // We have to access the list directly, since we don't want to
2080 // destroy the wxSizerItems.
2081 for( int ix
=m_rows
.GetChildren().GetCount()-1; ix
>=0; ix
-- ){
2082 wxSizerItem
*psi
= m_rows
.GetItem( (size_t)ix
);
2084 // Restore proportion for last item on line (if item has not been deleted)
2085 wxPropHolder
*pph
= (wxPropHolder
*)psi
->GetUserData();
2086 if( pph
&& GetChildren().Find(pph
->m_item
) )
2087 pph
->m_item
->SetProportion(pph
->m_propOld
);
2089 wxSizer
*psz
= psi
->GetSizer();
2091 wxSizerItemList
&sl
= psz
->GetChildren();
2092 while( sl
.GetLast() )
2093 sl
.Erase( sl
.GetLast() );
2096 int lineSumMajor
= 0;
2097 int majorSize
= GetSizeInMajorDir(m_size
);
2099 // Make sure we have at least one child sizer
2101 if( !m_rows
.GetChildren().GetCount() )
2102 m_rows
.Add( new wxBoxSizer(GetOrientation()), 1, wxEXPAND
);
2104 // The sizer where to insert items in
2105 wxSizer
*psz
= m_rows
.GetItem((size_t)0)->GetSizer();
2108 // Now put our child items into child sizers instead
2109 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
2110 wxSizerItem
*item
= NULL
, *itemLast
=NULL
;
2113 item
= node
->GetData();
2114 if ( item
->IsShown() )
2116 wxSize minSz
= item
->GetMinSize();
2117 int minSzMajor
= GetSizeInMajorDir(minSz
);
2119 // More space on this line?
2120 if( !lineSumMajor
|| lineSumMajor
+minSzMajor
<=majorSize
)
2122 lineSumMajor
+= minSzMajor
;
2126 lineSumMajor
= minSzMajor
;
2127 // Get a new empty sizer to insert into
2128 if( (int)m_rows
.GetChildren().GetCount()<=m_n_line
)
2129 m_rows
.Add( new wxBoxSizer(GetOrientation()), 1, wxEXPAND
);
2131 // If we have extend-last-on-each-line mode, then do so now
2132 // Note: We must store old proportion value then.
2133 if( m_flags
&wxEXTEND_LAST_ON_EACH_LINE
)
2134 AdjustPropLastItem(psz
,itemLast
);
2136 // The sizer where to insert items in
2137 psz
= m_rows
.GetItem(m_n_line
++)->GetSizer();
2141 // If item is a window, it now has a pointer to the child sizer,
2142 // which is wrong. Set it to point to us.
2143 if( item
->GetWindow() )
2144 item
->GetWindow()->SetContainingSizer( this );
2146 node
= node
->GetNext();
2149 // If we have extend-last-on-each-line mode, then do so now
2150 if( m_flags
&wxEXTEND_LAST_ON_EACH_LINE
)
2151 AdjustPropLastItem(psz
,itemLast
);
2153 // If we have more sizers than lines, remove them
2154 while( (int)m_rows
.GetChildren().GetCount()>m_n_line
)
2155 m_rows
.Remove( m_n_line
);
2157 // Now do layout on row sizer
2158 m_rows
.SetDimension( m_position
.x
, m_position
.y
, m_size
.x
, m_size
.y
);
2160 // Remember this to next time (will be overridden by InformFirstDirection if used)
2161 m_prim_size_last
= GetSizeInMajorDir(m_size
);
2165 wxSize
wxWrapSizer::CalcMin()
2167 if (m_children
.GetCount() == 0)
2170 // Algorithm for calculating min size: (assuming horizontal orientation)
2171 // X: Max width of all members
2172 // Y: Based on last X, calculate how many lines needed
2173 // First time around, assume all items fits on one line
2177 int lineMaxMinor
= 0;
2178 int lineSumMajor
= 0;
2181 // precalc item minsizes and fit on lines (preliminary)
2182 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
2185 wxSizerItem
*item
= node
->GetData();
2186 if ( item
->IsShown() )
2188 wxSize minSz
= item
->CalcMin();
2189 int szMajor
= GetSizeInMajorDir(minSz
);
2190 int szMinor
= GetSizeInMinorDir(minSz
);
2191 if( szMajor
>maxMajor
) maxMajor
= szMajor
;
2192 // More space on this line?
2193 if( m_prim_size_last
<0 || !lineSumMajor
||
2194 lineSumMajor
+szMajor
<=m_prim_size_last
)
2196 lineSumMajor
+= szMajor
;
2197 if( szMinor
>lineMaxMinor
)
2198 lineMaxMinor
= szMinor
;
2202 minorSum
+= lineMaxMinor
; // Add height of highest item on last line
2204 lineMaxMinor
= szMinor
;
2205 lineSumMajor
= szMajor
;
2208 node
= node
->GetNext();
2210 minorSum
+= lineMaxMinor
; // Add height of highest item on last line
2212 m_minSize
= SizeFromMajorMinor(maxMajor
, minorSum
);
2216 //---------------------------------------------------------------------------
2218 //---------------------------------------------------------------------------
2222 wxStaticBoxSizer::wxStaticBoxSizer( wxStaticBox
*box
, int orient
)
2223 : wxBoxSizer( orient
),
2226 wxASSERT_MSG( box
, wxT("wxStaticBoxSizer needs a static box") );
2228 // do this so that our Detach() is called if the static box is destroyed
2230 m_staticBox
->SetContainingSizer(this);
2233 wxStaticBoxSizer::wxStaticBoxSizer(int orient
, wxWindow
*win
, const wxString
& s
)
2234 : wxBoxSizer(orient
),
2235 m_staticBox(new wxStaticBox(win
, wxID_ANY
, s
))
2238 m_staticBox
->SetContainingSizer(this);
2241 wxStaticBoxSizer::~wxStaticBoxSizer()
2246 static void GetStaticBoxBorders( wxStaticBox
*box
,
2250 // this has to be done platform by platform as there is no way to
2251 // guess the thickness of a wxStaticBox border
2252 box
->GetBordersForSizer(borderTop
, borderOther
);
2255 void wxStaticBoxSizer::RecalcSizes()
2257 int top_border
, other_border
;
2258 GetStaticBoxBorders(m_staticBox
, &top_border
, &other_border
);
2260 m_staticBox
->SetSize( m_position
.x
, m_position
.y
, m_size
.x
, m_size
.y
);
2262 wxPoint
old_pos( m_position
);
2263 m_position
.x
+= other_border
;
2264 m_position
.y
+= top_border
;
2265 wxSize
old_size( m_size
);
2266 m_size
.x
-= 2*other_border
;
2267 m_size
.y
-= top_border
+ other_border
;
2269 wxBoxSizer::RecalcSizes();
2271 m_position
= old_pos
;
2275 wxSize
wxStaticBoxSizer::CalcMin()
2277 int top_border
, other_border
;
2278 GetStaticBoxBorders(m_staticBox
, &top_border
, &other_border
);
2280 wxSize
ret( wxBoxSizer::CalcMin() );
2281 ret
.x
+= 2*other_border
;
2282 ret
.y
+= other_border
+ top_border
;
2287 void wxStaticBoxSizer::ShowItems( bool show
)
2289 m_staticBox
->Show( show
);
2290 wxBoxSizer::ShowItems( show
);
2293 bool wxStaticBoxSizer::Detach( wxWindow
*window
)
2295 // avoid deleting m_staticBox in our dtor if it's being detached from the
2296 // sizer (which can happen because it's being already destroyed for
2298 if ( window
== m_staticBox
)
2304 return wxSizer::Detach( window
);
2307 #endif // wxUSE_STATBOX
2311 wxStdDialogButtonSizer::wxStdDialogButtonSizer()
2312 : wxBoxSizer(wxHORIZONTAL
)
2314 // Vertical buttons with lots of space on either side
2315 // looks rubbish on WinCE, so let's not do this for now.
2316 // If we are going to use vertical buttons, we should
2317 // put the sizer to the right of other controls in the dialog,
2318 // and that's beyond the scope of this sizer.
2320 bool is_pda
= (wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA
);
2321 // If we have a PDA screen, put yes/no button over
2322 // all other buttons, otherwise on the left side.
2324 m_orient
= wxVERTICAL
;
2327 m_buttonAffirmative
= NULL
;
2328 m_buttonApply
= NULL
;
2329 m_buttonNegative
= NULL
;
2330 m_buttonCancel
= NULL
;
2331 m_buttonHelp
= NULL
;
2334 void wxStdDialogButtonSizer::AddButton(wxButton
*mybutton
)
2336 switch (mybutton
->GetId())
2341 m_buttonAffirmative
= mybutton
;
2344 m_buttonApply
= mybutton
;
2347 m_buttonNegative
= mybutton
;
2351 m_buttonCancel
= mybutton
;
2354 case wxID_CONTEXT_HELP
:
2355 m_buttonHelp
= mybutton
;
2362 void wxStdDialogButtonSizer::SetAffirmativeButton( wxButton
*button
)
2364 m_buttonAffirmative
= button
;
2367 void wxStdDialogButtonSizer::SetNegativeButton( wxButton
*button
)
2369 m_buttonNegative
= button
;
2372 void wxStdDialogButtonSizer::SetCancelButton( wxButton
*button
)
2374 m_buttonCancel
= button
;
2377 void wxStdDialogButtonSizer::Realize()
2380 Add(0, 0, 0, wxLEFT
, 6);
2382 Add((wxWindow
*)m_buttonHelp
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, 6);
2384 if (m_buttonNegative
){
2385 // HIG POLICE BULLETIN - destructive buttons need extra padding
2386 // 24 pixels on either side
2387 Add((wxWindow
*)m_buttonNegative
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, 12);
2390 // extra whitespace between help/negative and cancel/ok buttons
2391 Add(0, 0, 1, wxEXPAND
, 0);
2393 if (m_buttonCancel
){
2394 Add((wxWindow
*)m_buttonCancel
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, 6);
2395 // Cancel or help should be default
2396 // m_buttonCancel->SetDefaultButton();
2399 // Ugh, Mac doesn't really have apply dialogs, so I'll just
2400 // figure the best place is between Cancel and OK
2402 Add((wxWindow
*)m_buttonApply
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, 6);
2404 if (m_buttonAffirmative
){
2405 Add((wxWindow
*)m_buttonAffirmative
, 0, wxALIGN_CENTRE
| wxLEFT
, 6);
2407 if (m_buttonAffirmative
->GetId() == wxID_SAVE
){
2408 // these buttons have set labels under Mac so we should use them
2409 m_buttonAffirmative
->SetLabel(_("Save"));
2410 if (m_buttonNegative
)
2411 m_buttonNegative
->SetLabel(_("Don't Save"));
2415 // Extra space around and at the right
2417 #elif defined(__WXGTK20__)
2418 Add(0, 0, 0, wxLEFT
, 9);
2420 Add((wxWindow
*)m_buttonHelp
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, 3);
2422 // extra whitespace between help and cancel/ok buttons
2423 Add(0, 0, 1, wxEXPAND
, 0);
2425 if (m_buttonNegative
){
2426 Add((wxWindow
*)m_buttonNegative
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, 3);
2429 // according to HIG, in explicit apply windows the order is:
2430 // [ Help Apply Cancel OK ]
2432 Add((wxWindow
*)m_buttonApply
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, 3);
2434 if (m_buttonCancel
){
2435 Add((wxWindow
*)m_buttonCancel
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, 3);
2436 // Cancel or help should be default
2437 // m_buttonCancel->SetDefaultButton();
2440 if (m_buttonAffirmative
)
2441 Add((wxWindow
*)m_buttonAffirmative
, 0, wxALIGN_CENTRE
| wxLEFT
, 6);
2442 #elif defined(__WXMSW__)
2445 // right-justify buttons
2446 Add(0, 0, 1, wxEXPAND
, 0);
2448 if (m_buttonAffirmative
){
2449 Add((wxWindow
*)m_buttonAffirmative
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, m_buttonAffirmative
->ConvertDialogToPixels(wxSize(2, 0)).x
);
2452 if (m_buttonNegative
){
2453 Add((wxWindow
*)m_buttonNegative
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, m_buttonNegative
->ConvertDialogToPixels(wxSize(2, 0)).x
);
2456 if (m_buttonCancel
){
2457 Add((wxWindow
*)m_buttonCancel
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, m_buttonCancel
->ConvertDialogToPixels(wxSize(2, 0)).x
);
2460 Add((wxWindow
*)m_buttonApply
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, m_buttonApply
->ConvertDialogToPixels(wxSize(2, 0)).x
);
2463 Add((wxWindow
*)m_buttonHelp
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, m_buttonHelp
->ConvertDialogToPixels(wxSize(2, 0)).x
);
2465 // GTK+1 and any other platform
2467 // Add(0, 0, 0, wxLEFT, 5); // Not sure what this was for but it unbalances the dialog
2469 Add((wxWindow
*)m_buttonHelp
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, m_buttonHelp
->ConvertDialogToPixels(wxSize(4, 0)).x
);
2471 // extra whitespace between help and cancel/ok buttons
2472 Add(0, 0, 1, wxEXPAND
, 0);
2475 Add((wxWindow
*)m_buttonApply
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, m_buttonApply
->ConvertDialogToPixels(wxSize(4, 0)).x
);
2477 if (m_buttonAffirmative
){
2478 Add((wxWindow
*)m_buttonAffirmative
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, m_buttonAffirmative
->ConvertDialogToPixels(wxSize(4, 0)).x
);
2481 if (m_buttonNegative
){
2482 Add((wxWindow
*)m_buttonNegative
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, m_buttonNegative
->ConvertDialogToPixels(wxSize(4, 0)).x
);
2485 if (m_buttonCancel
){
2486 Add((wxWindow
*)m_buttonCancel
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, m_buttonCancel
->ConvertDialogToPixels(wxSize(4, 0)).x
);
2487 // Cancel or help should be default
2488 // m_buttonCancel->SetDefaultButton();
2494 #endif // wxUSE_BUTTON