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"
21 #include "wx/private/flagscheck.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/display.h"
35 #include "wx/vector.h"
36 #include "wx/listimpl.cpp"
39 //---------------------------------------------------------------------------
41 IMPLEMENT_CLASS(wxSizerItem
, wxObject
)
42 IMPLEMENT_CLASS(wxSizer
, wxObject
)
43 IMPLEMENT_CLASS(wxGridSizer
, wxSizer
)
44 IMPLEMENT_CLASS(wxFlexGridSizer
, wxGridSizer
)
45 IMPLEMENT_CLASS(wxBoxSizer
, wxSizer
)
47 IMPLEMENT_CLASS(wxStaticBoxSizer
, wxBoxSizer
)
50 IMPLEMENT_CLASS(wxStdDialogButtonSizer
, wxBoxSizer
)
53 WX_DEFINE_EXPORTED_LIST( wxSizerItemList
)
88 // ----------------------------------------------------------------------------
90 // ----------------------------------------------------------------------------
92 // check for flags conflicts
93 static const int SIZER_FLAGS_MASK
=
95 wxADD_FLAG(wxHORIZONTAL
,
96 wxADD_FLAG(wxVERTICAL
,
101 wxADD_FLAG(wxALIGN_NOT
,
102 wxADD_FLAG(wxALIGN_CENTER_HORIZONTAL
,
103 wxADD_FLAG(wxALIGN_RIGHT
,
104 wxADD_FLAG(wxALIGN_BOTTOM
,
105 wxADD_FLAG(wxALIGN_CENTER_VERTICAL
,
106 wxADD_FLAG(wxFIXED_MINSIZE
,
107 wxADD_FLAG(wxRESERVE_SPACE_EVEN_IF_HIDDEN
,
108 wxADD_FLAG(wxSTRETCH_NOT
,
114 #define ASSERT_VALID_SIZER_FLAGS(f) wxASSERT_VALID_FLAGS(f, SIZER_FLAGS_MASK)
117 void wxSizerItem::Init(const wxSizerFlags
& flags
)
121 m_proportion
= flags
.GetProportion();
122 m_flag
= flags
.GetFlags();
123 m_border
= flags
.GetBorderInPixels();
125 ASSERT_VALID_SIZER_FLAGS( m_flag
);
128 wxSizerItem::wxSizerItem()
139 void wxSizerItem::DoSetWindow(wxWindow
*window
)
141 wxCHECK_RET( window
, wxT("NULL window in wxSizerItem::SetWindow()") );
143 m_kind
= Item_Window
;
146 // window doesn't become smaller than its initial size, whatever happens
147 m_minSize
= window
->GetSize();
149 if ( m_flag
& wxFIXED_MINSIZE
)
150 window
->SetMinSize(m_minSize
);
152 // aspect ratio calculated from initial size
156 wxSizerItem::wxSizerItem(wxWindow
*window
,
162 m_proportion(proportion
),
168 ASSERT_VALID_SIZER_FLAGS( m_flag
);
174 void wxSizerItem::DoSetSizer(wxSizer
*sizer
)
180 wxSizerItem::wxSizerItem(wxSizer
*sizer
,
187 m_proportion(proportion
),
194 ASSERT_VALID_SIZER_FLAGS( m_flag
);
198 // m_minSize is set later
202 void wxSizerItem::DoSetSpacer(const wxSize
& size
)
204 m_kind
= Item_Spacer
;
205 m_spacer
= new wxSizerSpacer(size
);
210 wxSizerItem::wxSizerItem(int width
,
218 m_minSize(width
, height
), // minimal size is the initial size
219 m_proportion(proportion
),
225 ASSERT_VALID_SIZER_FLAGS( m_flag
);
227 DoSetSpacer(wxSize(width
, height
));
230 wxSizerItem::~wxSizerItem()
236 void wxSizerItem::Free()
244 m_window
->SetContainingSizer(NULL
);
257 wxFAIL_MSG( wxT("unexpected wxSizerItem::m_kind") );
263 wxSize
wxSizerItem::GetSpacer() const
266 if ( m_kind
== Item_Spacer
)
267 size
= m_spacer
->GetSize();
273 wxSize
wxSizerItem::GetSize() const
282 ret
= m_window
->GetSize();
286 ret
= m_sizer
->GetSize();
290 ret
= m_spacer
->GetSize();
295 wxFAIL_MSG( wxT("unexpected wxSizerItem::m_kind") );
302 if (m_flag
& wxNORTH
)
304 if (m_flag
& wxSOUTH
)
310 bool wxSizerItem::InformFirstDirection(int direction
, int size
, int availableOtherDir
)
312 // The size that come here will be including borders. Child items should get it
316 if( direction
==wxHORIZONTAL
)
323 else if( direction
==wxVERTICAL
)
325 if (m_flag
& wxNORTH
)
327 if (m_flag
& wxSOUTH
)
333 // Pass the information along to the held object
336 didUse
= GetSizer()->InformFirstDirection(direction
,size
,availableOtherDir
);
338 m_minSize
= GetSizer()->CalcMin();
342 didUse
= GetWindow()->InformFirstDirection(direction
,size
,availableOtherDir
);
344 m_minSize
= m_window
->GetEffectiveMinSize();
346 // This information is useful for items with wxSHAPED flag, since
347 // we can request an optimal min size for such an item. Even if
348 // we overwrite the m_minSize member here, we can read it back from
349 // the owned window (happens automatically).
350 if( (m_flag
& wxSHAPED
) && (m_flag
& wxEXPAND
) && direction
)
352 if( !wxIsNullDouble(m_ratio
) )
354 wxCHECK_MSG( (m_proportion
==0), false, wxT("Shaped item, non-zero proportion in wxSizerItem::InformFirstDirection()") );
355 if( direction
==wxHORIZONTAL
&& !wxIsNullDouble(m_ratio
) )
357 // Clip size so that we don't take too much
358 if( availableOtherDir
>=0 && int(size
/m_ratio
)-m_minSize
.y
>availableOtherDir
)
359 size
= int((availableOtherDir
+m_minSize
.y
)*m_ratio
);
360 m_minSize
= wxSize(size
,int(size
/m_ratio
));
362 else if( direction
==wxVERTICAL
)
364 // Clip size so that we don't take too much
365 if( availableOtherDir
>=0 && int(size
*m_ratio
)-m_minSize
.x
>availableOtherDir
)
366 size
= int((availableOtherDir
+m_minSize
.x
)/m_ratio
);
367 m_minSize
= wxSize(int(size
*m_ratio
),size
);
377 wxSize
wxSizerItem::CalcMin()
381 m_minSize
= m_sizer
->GetMinSize();
383 // if we have to preserve aspect ratio _AND_ this is
384 // the first-time calculation, consider ret to be initial size
385 if ( (m_flag
& wxSHAPED
) && wxIsNullDouble(m_ratio
) )
388 else if ( IsWindow() )
390 // Since the size of the window may change during runtime, we
391 // should use the current minimal/best size.
392 m_minSize
= m_window
->GetEffectiveMinSize();
395 return GetMinSizeWithBorder();
398 wxSize
wxSizerItem::GetMinSizeWithBorder() const
400 wxSize ret
= m_minSize
;
406 if (m_flag
& wxNORTH
)
408 if (m_flag
& wxSOUTH
)
415 void wxSizerItem::SetDimension( const wxPoint
& pos_
, const wxSize
& size_
)
419 if (m_flag
& wxSHAPED
)
421 // adjust aspect ratio
422 int rwidth
= (int) (size
.y
* m_ratio
);
426 int rheight
= (int) (size
.x
/ m_ratio
);
427 // add vertical space
428 if (m_flag
& wxALIGN_CENTER_VERTICAL
)
429 pos
.y
+= (size
.y
- rheight
) / 2;
430 else if (m_flag
& wxALIGN_BOTTOM
)
431 pos
.y
+= (size
.y
- rheight
);
432 // use reduced dimensions
435 else if (rwidth
< size
.x
)
437 // add horizontal space
438 if (m_flag
& wxALIGN_CENTER_HORIZONTAL
)
439 pos
.x
+= (size
.x
- rwidth
) / 2;
440 else if (m_flag
& wxALIGN_RIGHT
)
441 pos
.x
+= (size
.x
- rwidth
);
446 // This is what GetPosition() returns. Since we calculate
447 // borders afterwards, GetPosition() will be the left/top
448 // corner of the surrounding border.
460 if (m_flag
& wxNORTH
)
465 if (m_flag
& wxSOUTH
)
475 m_rect
= wxRect(pos
, size
);
480 wxFAIL_MSG( wxT("can't set size of uninitialized sizer item") );
485 // Use wxSIZE_FORCE_EVENT here since a sizer item might
486 // have changed alignment or some other property which would
487 // not change the size of the window. In such a case, no
488 // wxSizeEvent would normally be generated and thus the
489 // control wouldn't get layed out correctly here.
491 m_window
->SetSize(pos
.x
, pos
.y
, size
.x
, size
.y
,
492 wxSIZE_ALLOW_MINUS_ONE
|wxSIZE_FORCE_EVENT
);
494 m_window
->SetSize(pos
.x
, pos
.y
, size
.x
, size
.y
,
495 wxSIZE_ALLOW_MINUS_ONE
);
500 m_sizer
->SetDimension(pos
, size
);
504 m_spacer
->SetSize(size
);
509 wxFAIL_MSG( wxT("unexpected wxSizerItem::m_kind") );
513 void wxSizerItem::DeleteWindows()
522 //We are deleting the window from this sizer - normally
523 //the window destroys the sizer associated with it,
524 //which might destroy this, which we don't want
525 m_window
->SetContainingSizer(NULL
);
527 //Putting this after the switch will result in a spacer
528 //not being deleted properly on destruction
533 m_sizer
->DeleteWindows();
538 wxFAIL_MSG( wxT("unexpected wxSizerItem::m_kind") );
543 void wxSizerItem::Show( bool show
)
548 wxFAIL_MSG( wxT("can't show uninitialized sizer item") );
552 m_window
->Show(show
);
560 m_spacer
->Show(show
);
565 wxFAIL_MSG( wxT("unexpected wxSizerItem::m_kind") );
569 bool wxSizerItem::IsShown() const
571 if ( m_flag
& wxRESERVE_SPACE_EVEN_IF_HIDDEN
)
577 // we may be called from CalcMin(), just return false so that we're
582 return m_window
->IsShown();
586 // arbitrarily decide that if at least one of our elements is
587 // shown, so are we (this arbitrariness is the reason for
588 // deprecating this function)
589 for ( wxSizerItemList::compatibility_iterator
590 node
= m_sizer
->GetChildren().GetFirst();
592 node
= node
->GetNext() )
594 if ( node
->GetData()->IsShown() )
601 return m_spacer
->IsShown();
605 wxFAIL_MSG( wxT("unexpected wxSizerItem::m_kind") );
611 #if WXWIN_COMPATIBILITY_2_6
612 void wxSizerItem::SetOption( int option
)
614 SetProportion( option
);
617 int wxSizerItem::GetOption() const
619 return GetProportion();
621 #endif // WXWIN_COMPATIBILITY_2_6
624 //---------------------------------------------------------------------------
626 //---------------------------------------------------------------------------
630 WX_CLEAR_LIST(wxSizerItemList
, m_children
);
633 wxSizerItem
* wxSizer::DoInsert( size_t index
, wxSizerItem
*item
)
635 m_children
.Insert( index
, item
);
637 if ( item
->GetWindow() )
638 item
->GetWindow()->SetContainingSizer( this );
640 if ( item
->GetSizer() )
641 item
->GetSizer()->SetContainingWindow( m_containingWindow
);
646 void wxSizer::SetContainingWindow(wxWindow
*win
)
648 if ( win
== m_containingWindow
)
651 m_containingWindow
= win
;
653 // set the same window for all nested sizers as well, they also are in the
655 for ( wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
657 node
= node
->GetNext() )
659 wxSizerItem
*const item
= node
->GetData();
660 wxSizer
*const sizer
= item
->GetSizer();
664 sizer
->SetContainingWindow(win
);
669 #if WXWIN_COMPATIBILITY_2_6
670 bool wxSizer::Remove( wxWindow
*window
)
672 return Detach( window
);
674 #endif // WXWIN_COMPATIBILITY_2_6
676 bool wxSizer::Remove( wxSizer
*sizer
)
678 wxASSERT_MSG( sizer
, wxT("Removing NULL sizer") );
680 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
683 wxSizerItem
*item
= node
->GetData();
685 if (item
->GetSizer() == sizer
)
688 m_children
.Erase( node
);
692 node
= node
->GetNext();
698 bool wxSizer::Remove( int index
)
700 wxCHECK_MSG( index
>= 0 && (size_t)index
< m_children
.GetCount(),
702 wxT("Remove index is out of range") );
704 wxSizerItemList::compatibility_iterator node
= m_children
.Item( index
);
706 wxCHECK_MSG( node
, false, wxT("Failed to find child node") );
708 delete node
->GetData();
709 m_children
.Erase( node
);
714 bool wxSizer::Detach( wxSizer
*sizer
)
716 wxASSERT_MSG( sizer
, wxT("Detaching NULL sizer") );
718 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
721 wxSizerItem
*item
= node
->GetData();
723 if (item
->GetSizer() == sizer
)
727 m_children
.Erase( node
);
730 node
= node
->GetNext();
736 bool wxSizer::Detach( wxWindow
*window
)
738 wxASSERT_MSG( window
, wxT("Detaching NULL window") );
740 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
743 wxSizerItem
*item
= node
->GetData();
745 if (item
->GetWindow() == window
)
748 m_children
.Erase( node
);
751 node
= node
->GetNext();
757 bool wxSizer::Detach( int index
)
759 wxCHECK_MSG( index
>= 0 && (size_t)index
< m_children
.GetCount(),
761 wxT("Detach index is out of range") );
763 wxSizerItemList::compatibility_iterator node
= m_children
.Item( index
);
765 wxCHECK_MSG( node
, false, wxT("Failed to find child node") );
767 wxSizerItem
*item
= node
->GetData();
769 if ( item
->IsSizer() )
773 m_children
.Erase( node
);
777 bool wxSizer::Replace( wxWindow
*oldwin
, wxWindow
*newwin
, bool recursive
)
779 wxASSERT_MSG( oldwin
, wxT("Replacing NULL window") );
780 wxASSERT_MSG( newwin
, wxT("Replacing with NULL window") );
782 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
785 wxSizerItem
*item
= node
->GetData();
787 if (item
->GetWindow() == oldwin
)
789 item
->AssignWindow(newwin
);
790 newwin
->SetContainingSizer( this );
793 else if (recursive
&& item
->IsSizer())
795 if (item
->GetSizer()->Replace( oldwin
, newwin
, true ))
799 node
= node
->GetNext();
805 bool wxSizer::Replace( wxSizer
*oldsz
, wxSizer
*newsz
, bool recursive
)
807 wxASSERT_MSG( oldsz
, wxT("Replacing NULL sizer") );
808 wxASSERT_MSG( newsz
, wxT("Replacing with NULL sizer") );
810 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
813 wxSizerItem
*item
= node
->GetData();
815 if (item
->GetSizer() == oldsz
)
817 item
->AssignSizer(newsz
);
820 else if (recursive
&& item
->IsSizer())
822 if (item
->GetSizer()->Replace( oldsz
, newsz
, true ))
826 node
= node
->GetNext();
832 bool wxSizer::Replace( size_t old
, wxSizerItem
*newitem
)
834 wxCHECK_MSG( old
< m_children
.GetCount(), false, wxT("Replace index is out of range") );
835 wxASSERT_MSG( newitem
, wxT("Replacing with NULL item") );
837 wxSizerItemList::compatibility_iterator node
= m_children
.Item( old
);
839 wxCHECK_MSG( node
, false, wxT("Failed to find child node") );
841 wxSizerItem
*item
= node
->GetData();
842 node
->SetData(newitem
);
844 if (item
->IsWindow() && item
->GetWindow())
845 item
->GetWindow()->SetContainingSizer(NULL
);
852 void wxSizer::Clear( bool delete_windows
)
854 // First clear the ContainingSizer pointers
855 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
858 wxSizerItem
*item
= node
->GetData();
860 if (item
->IsWindow())
861 item
->GetWindow()->SetContainingSizer( NULL
);
862 node
= node
->GetNext();
865 // Destroy the windows if needed
869 // Now empty the list
870 WX_CLEAR_LIST(wxSizerItemList
, m_children
);
873 void wxSizer::DeleteWindows()
875 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
878 wxSizerItem
*item
= node
->GetData();
880 item
->DeleteWindows();
881 node
= node
->GetNext();
885 wxSize
wxSizer::ComputeFittingClientSize(wxWindow
*window
)
887 wxCHECK_MSG( window
, wxDefaultSize
, "window can't be NULL" );
889 // take the min size by default and limit it by max size
890 wxSize size
= GetMinClientSize(window
);
893 wxTopLevelWindow
*tlw
= wxDynamicCast(window
, wxTopLevelWindow
);
896 // hack for small screen devices where TLWs are always full screen
897 if ( tlw
->IsAlwaysMaximized() )
899 return tlw
->GetClientSize();
902 // limit the window to the size of the display it is on
903 int disp
= wxDisplay::GetFromWindow(window
);
904 if ( disp
== wxNOT_FOUND
)
906 // or, if we don't know which one it is, of the main one
910 sizeMax
= wxDisplay(disp
).GetClientArea().GetSize();
912 // space for decorations and toolbars etc.
913 sizeMax
= tlw
->WindowToClientSize(sizeMax
);
917 sizeMax
= GetMaxClientSize(window
);
920 if ( sizeMax
.x
!= wxDefaultCoord
&& size
.x
> sizeMax
.x
)
922 if ( sizeMax
.y
!= wxDefaultCoord
&& size
.y
> sizeMax
.y
)
928 wxSize
wxSizer::ComputeFittingWindowSize(wxWindow
*window
)
930 wxCHECK_MSG( window
, wxDefaultSize
, "window can't be NULL" );
932 return window
->ClientToWindowSize(ComputeFittingClientSize(window
));
935 wxSize
wxSizer::Fit( wxWindow
*window
)
937 wxCHECK_MSG( window
, wxDefaultSize
, "window can't be NULL" );
940 window
->SetClientSize(ComputeFittingClientSize(window
));
942 // return entire size
943 return window
->GetSize();
946 void wxSizer::FitInside( wxWindow
*window
)
949 if (window
->IsTopLevel())
950 size
= VirtualFitSize( window
);
952 size
= GetMinClientSize( window
);
954 window
->SetVirtualSize( size
);
957 void wxSizer::Layout()
959 // (re)calculates minimums needed for each item and other preparations
963 // Applies the layout and repositions/resizes the items
967 void wxSizer::SetSizeHints( wxWindow
*window
)
969 // Preserve the window's max size hints, but set the
970 // lower bound according to the sizer calculations.
972 // This is equivalent to calling Fit(), except that we need to set
973 // the size hints _in between_ the two steps performed by Fit
974 // (1. ComputeFittingClientSize, 2. SetClientSize). That's because
975 // otherwise SetClientSize() could have no effect if there already are
976 // size hints in effect that forbid requested client size.
978 const wxSize clientSize
= ComputeFittingClientSize(window
);
980 window
->SetMinClientSize(clientSize
);
981 window
->SetClientSize(clientSize
);
984 #if WXWIN_COMPATIBILITY_2_8
985 void wxSizer::SetVirtualSizeHints( wxWindow
*window
)
989 #endif // WXWIN_COMPATIBILITY_2_8
991 // TODO on mac we need a function that determines how much free space this
992 // min size contains, in order to make sure that we have 20 pixels of free
993 // space around the controls
994 wxSize
wxSizer::GetMaxClientSize( wxWindow
*window
) const
996 return window
->WindowToClientSize(window
->GetMaxSize());
999 wxSize
wxSizer::GetMinClientSize( wxWindow
*WXUNUSED(window
) )
1001 return GetMinSize(); // Already returns client size.
1004 wxSize
wxSizer::VirtualFitSize( wxWindow
*window
)
1006 wxSize size
= GetMinClientSize( window
);
1007 wxSize sizeMax
= GetMaxClientSize( window
);
1009 // Limit the size if sizeMax != wxDefaultSize
1011 if ( size
.x
> sizeMax
.x
&& sizeMax
.x
!= wxDefaultCoord
)
1013 if ( size
.y
> sizeMax
.y
&& sizeMax
.y
!= wxDefaultCoord
)
1019 wxSize
wxSizer::GetMinSize()
1021 wxSize
ret( CalcMin() );
1022 if (ret
.x
< m_minSize
.x
) ret
.x
= m_minSize
.x
;
1023 if (ret
.y
< m_minSize
.y
) ret
.y
= m_minSize
.y
;
1027 void wxSizer::DoSetMinSize( int width
, int height
)
1029 m_minSize
.x
= width
;
1030 m_minSize
.y
= height
;
1033 bool wxSizer::DoSetItemMinSize( wxWindow
*window
, int width
, int height
)
1035 wxASSERT_MSG( window
, wxT("SetMinSize for NULL window") );
1037 // Is it our immediate child?
1039 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
1042 wxSizerItem
*item
= node
->GetData();
1044 if (item
->GetWindow() == window
)
1046 item
->SetMinSize( width
, height
);
1049 node
= node
->GetNext();
1052 // No? Search any subsizers we own then
1054 node
= m_children
.GetFirst();
1057 wxSizerItem
*item
= node
->GetData();
1059 if ( item
->GetSizer() &&
1060 item
->GetSizer()->DoSetItemMinSize( window
, width
, height
) )
1062 // A child sizer found the requested windw, exit.
1065 node
= node
->GetNext();
1071 bool wxSizer::DoSetItemMinSize( wxSizer
*sizer
, int width
, int height
)
1073 wxASSERT_MSG( sizer
, wxT("SetMinSize for NULL sizer") );
1075 // Is it our immediate child?
1077 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
1080 wxSizerItem
*item
= node
->GetData();
1082 if (item
->GetSizer() == sizer
)
1084 item
->GetSizer()->DoSetMinSize( width
, height
);
1087 node
= node
->GetNext();
1090 // No? Search any subsizers we own then
1092 node
= m_children
.GetFirst();
1095 wxSizerItem
*item
= node
->GetData();
1097 if ( item
->GetSizer() &&
1098 item
->GetSizer()->DoSetItemMinSize( sizer
, width
, height
) )
1100 // A child found the requested sizer, exit.
1103 node
= node
->GetNext();
1109 bool wxSizer::DoSetItemMinSize( size_t index
, int width
, int height
)
1111 wxSizerItemList::compatibility_iterator node
= m_children
.Item( index
);
1113 wxCHECK_MSG( node
, false, wxT("Failed to find child node") );
1115 wxSizerItem
*item
= node
->GetData();
1117 if (item
->GetSizer())
1119 // Sizers contains the minimal size in them, if not calculated ...
1120 item
->GetSizer()->DoSetMinSize( width
, height
);
1124 // ... but the minimal size of spacers and windows is stored via the item
1125 item
->SetMinSize( width
, height
);
1131 wxSizerItem
* wxSizer::GetItem( wxWindow
*window
, bool recursive
)
1133 wxASSERT_MSG( window
, wxT("GetItem for NULL window") );
1135 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
1138 wxSizerItem
*item
= node
->GetData();
1140 if (item
->GetWindow() == window
)
1144 else if (recursive
&& item
->IsSizer())
1146 wxSizerItem
*subitem
= item
->GetSizer()->GetItem( window
, true );
1151 node
= node
->GetNext();
1157 wxSizerItem
* wxSizer::GetItem( wxSizer
*sizer
, bool recursive
)
1159 wxASSERT_MSG( sizer
, wxT("GetItem for NULL sizer") );
1161 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
1164 wxSizerItem
*item
= node
->GetData();
1166 if (item
->GetSizer() == sizer
)
1170 else if (recursive
&& item
->IsSizer())
1172 wxSizerItem
*subitem
= item
->GetSizer()->GetItem( sizer
, true );
1177 node
= node
->GetNext();
1183 wxSizerItem
* wxSizer::GetItem( size_t index
)
1185 wxCHECK_MSG( index
< m_children
.GetCount(),
1187 wxT("GetItem index is out of range") );
1189 return m_children
.Item( index
)->GetData();
1192 wxSizerItem
* wxSizer::GetItemById( int id
, bool recursive
)
1194 // This gets a sizer item by the id of the sizer item
1195 // and NOT the id of a window if the item is a window.
1197 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
1200 wxSizerItem
*item
= node
->GetData();
1202 if (item
->GetId() == id
)
1206 else if (recursive
&& item
->IsSizer())
1208 wxSizerItem
*subitem
= item
->GetSizer()->GetItemById( id
, true );
1213 node
= node
->GetNext();
1219 bool wxSizer::Show( wxWindow
*window
, bool show
, bool recursive
)
1221 wxSizerItem
*item
= GetItem( window
, recursive
);
1232 bool wxSizer::Show( wxSizer
*sizer
, bool show
, bool recursive
)
1234 wxSizerItem
*item
= GetItem( sizer
, recursive
);
1245 bool wxSizer::Show( size_t index
, bool show
)
1247 wxSizerItem
*item
= GetItem( index
);
1258 void wxSizer::ShowItems( bool show
)
1260 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
1263 node
->GetData()->Show( show
);
1264 node
= node
->GetNext();
1268 bool wxSizer::IsShown( wxWindow
*window
) const
1270 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
1273 wxSizerItem
*item
= node
->GetData();
1275 if (item
->GetWindow() == window
)
1277 return item
->IsShown();
1279 node
= node
->GetNext();
1282 wxFAIL_MSG( wxT("IsShown failed to find sizer item") );
1287 bool wxSizer::IsShown( wxSizer
*sizer
) const
1289 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
1292 wxSizerItem
*item
= node
->GetData();
1294 if (item
->GetSizer() == sizer
)
1296 return item
->IsShown();
1298 node
= node
->GetNext();
1301 wxFAIL_MSG( wxT("IsShown failed to find sizer item") );
1306 bool wxSizer::IsShown( size_t index
) const
1308 wxCHECK_MSG( index
< m_children
.GetCount(),
1310 wxT("IsShown index is out of range") );
1312 return m_children
.Item( index
)->GetData()->IsShown();
1316 //---------------------------------------------------------------------------
1318 //---------------------------------------------------------------------------
1320 wxGridSizer::wxGridSizer( int cols
, int vgap
, int hgap
)
1321 : m_rows( cols
== 0 ? 1 : 0 ),
1326 wxASSERT(cols
>= 0);
1329 wxGridSizer::wxGridSizer( int cols
, const wxSize
& gap
)
1330 : m_rows( cols
== 0 ? 1 : 0 ),
1332 m_vgap( gap
.GetHeight() ),
1333 m_hgap( gap
.GetWidth() )
1335 wxASSERT(cols
>= 0);
1338 wxGridSizer::wxGridSizer( int rows
, int cols
, int vgap
, int hgap
)
1339 : m_rows( rows
|| cols
? rows
: 1 ),
1344 wxASSERT(rows
>= 0 && cols
>= 0);
1347 wxGridSizer::wxGridSizer( int rows
, int cols
, const wxSize
& gap
)
1348 : m_rows( rows
|| cols
? rows
: 1 ),
1350 m_vgap( gap
.GetHeight() ),
1351 m_hgap( gap
.GetWidth() )
1353 wxASSERT(rows
>= 0 && cols
>= 0);
1356 wxSizerItem
*wxGridSizer::DoInsert(size_t index
, wxSizerItem
*item
)
1358 // if only the number of columns or the number of rows is specified for a
1359 // sizer, arbitrarily many items can be added to it but if both of them are
1360 // fixed, then the sizer can't have more than that many items -- check for
1361 // this here to ensure that we detect errors as soon as possible
1362 if ( m_cols
&& m_rows
)
1364 const int nitems
= m_children
.GetCount();
1365 if ( nitems
== m_cols
*m_rows
)
1369 "too many items (%d > %d*%d) in grid sizer (maybe you "
1370 "should omit the number of either rows or columns?)",
1371 nitems
+ 1, m_cols
, m_rows
)
1374 // additionally, continuing to use the specified number of columns
1375 // and rows is not a good idea as callers of CalcRowsCols() expect
1376 // that all sizer items can fit into m_cols-/m_rows-sized arrays
1377 // which is not the case if there are too many items and results in
1378 // crashes, so let it compute the number of rows automatically by
1379 // forgetting the (wrong) number of rows specified (this also has a
1380 // nice side effect of giving only one assert even if there are
1381 // many more items than allowed in this sizer)
1386 return wxSizer::DoInsert(index
, item
);
1389 int wxGridSizer::CalcRowsCols(int& nrows
, int& ncols
) const
1391 const int nitems
= m_children
.GetCount();
1393 ncols
= GetEffectiveColsCount();
1394 nrows
= GetEffectiveRowsCount();
1396 // Since Insert() checks for overpopulation, the following
1397 // should only assert if the grid was shrunk via SetRows() / SetCols()
1398 wxASSERT_MSG( nitems
<= ncols
*nrows
, "logic error in wxGridSizer" );
1403 void wxGridSizer::RecalcSizes()
1405 int nitems
, nrows
, ncols
;
1406 if ( (nitems
= CalcRowsCols(nrows
, ncols
)) == 0 )
1409 wxSize
sz( GetSize() );
1410 wxPoint
pt( GetPosition() );
1412 int w
= (sz
.x
- (ncols
- 1) * m_hgap
) / ncols
;
1413 int h
= (sz
.y
- (nrows
- 1) * m_vgap
) / nrows
;
1416 for (int c
= 0; c
< ncols
; c
++)
1419 for (int r
= 0; r
< nrows
; r
++)
1421 int i
= r
* ncols
+ c
;
1424 wxSizerItemList::compatibility_iterator node
= m_children
.Item( i
);
1426 wxASSERT_MSG( node
, wxT("Failed to find SizerItemList node") );
1428 SetItemBounds( node
->GetData(), x
, y
, w
, h
);
1436 wxSize
wxGridSizer::CalcMin()
1439 if ( CalcRowsCols(nrows
, ncols
) == 0 )
1442 // Find the max width and height for any component
1446 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
1449 wxSizerItem
*item
= node
->GetData();
1450 wxSize
sz( item
->CalcMin() );
1452 w
= wxMax( w
, sz
.x
);
1453 h
= wxMax( h
, sz
.y
);
1455 node
= node
->GetNext();
1458 // In case we have a nested sizer with a two step algo , give it
1459 // a chance to adjust to that (we give it width component)
1460 node
= m_children
.GetFirst();
1461 bool didChangeMinSize
= false;
1464 wxSizerItem
*item
= node
->GetData();
1465 didChangeMinSize
|= item
->InformFirstDirection( wxHORIZONTAL
, w
, -1 );
1467 node
= node
->GetNext();
1470 // And redo iteration in case min size changed
1471 if( didChangeMinSize
)
1473 node
= m_children
.GetFirst();
1477 wxSizerItem
*item
= node
->GetData();
1478 wxSize
sz( item
->GetMinSizeWithBorder() );
1480 w
= wxMax( w
, sz
.x
);
1481 h
= wxMax( h
, sz
.y
);
1483 node
= node
->GetNext();
1487 return wxSize( ncols
* w
+ (ncols
-1) * m_hgap
,
1488 nrows
* h
+ (nrows
-1) * m_vgap
);
1491 void wxGridSizer::SetItemBounds( wxSizerItem
*item
, int x
, int y
, int w
, int h
)
1494 wxSize
sz( item
->GetMinSizeWithBorder() );
1495 int flag
= item
->GetFlag();
1497 if ((flag
& wxEXPAND
) || (flag
& wxSHAPED
))
1503 if (flag
& wxALIGN_CENTER_HORIZONTAL
)
1505 pt
.x
= x
+ (w
- sz
.x
) / 2;
1507 else if (flag
& wxALIGN_RIGHT
)
1509 pt
.x
= x
+ (w
- sz
.x
);
1512 if (flag
& wxALIGN_CENTER_VERTICAL
)
1514 pt
.y
= y
+ (h
- sz
.y
) / 2;
1516 else if (flag
& wxALIGN_BOTTOM
)
1518 pt
.y
= y
+ (h
- sz
.y
);
1522 item
->SetDimension(pt
, sz
);
1525 //---------------------------------------------------------------------------
1527 //---------------------------------------------------------------------------
1529 wxFlexGridSizer::wxFlexGridSizer( int cols
, int vgap
, int hgap
)
1530 : wxGridSizer( cols
, vgap
, hgap
),
1531 m_flexDirection(wxBOTH
),
1532 m_growMode(wxFLEX_GROWMODE_SPECIFIED
)
1536 wxFlexGridSizer::wxFlexGridSizer( int cols
, const wxSize
& gap
)
1537 : wxGridSizer( cols
, gap
),
1538 m_flexDirection(wxBOTH
),
1539 m_growMode(wxFLEX_GROWMODE_SPECIFIED
)
1543 wxFlexGridSizer::wxFlexGridSizer( int rows
, int cols
, int vgap
, int hgap
)
1544 : wxGridSizer( rows
, cols
, vgap
, hgap
),
1545 m_flexDirection(wxBOTH
),
1546 m_growMode(wxFLEX_GROWMODE_SPECIFIED
)
1550 wxFlexGridSizer::wxFlexGridSizer( int rows
, int cols
, const wxSize
& gap
)
1551 : wxGridSizer( rows
, cols
, gap
),
1552 m_flexDirection(wxBOTH
),
1553 m_growMode(wxFLEX_GROWMODE_SPECIFIED
)
1557 wxFlexGridSizer::~wxFlexGridSizer()
1561 void wxFlexGridSizer::RecalcSizes()
1564 if ( !CalcRowsCols(nrows
, ncols
) )
1567 const wxPoint
pt(GetPosition());
1568 const wxSize
sz(GetSize());
1570 AdjustForGrowables(sz
);
1572 wxSizerItemList::const_iterator i
= m_children
.begin();
1573 const wxSizerItemList::const_iterator end
= m_children
.end();
1576 for ( int r
= 0; r
< nrows
; r
++ )
1578 if ( m_rowHeights
[r
] == -1 )
1580 // this row is entirely hidden, skip it
1581 for ( int c
= 0; c
< ncols
; c
++ )
1592 const int hrow
= m_rowHeights
[r
];
1593 int h
= sz
.y
- y
; // max remaining height, don't overflow it
1598 for ( int c
= 0; c
< ncols
&& i
!= end
; c
++, ++i
)
1600 const int wcol
= m_colWidths
[c
];
1605 int w
= sz
.x
- x
; // max possible value, ensure we don't overflow
1609 SetItemBounds(*i
, pt
.x
+ x
, pt
.y
+ y
, w
, h
);
1621 // helper function used in CalcMin() to sum up the sizes of non-hidden items
1622 static int SumArraySizes(const wxArrayInt
& sizes
, int gap
)
1624 // Sum total minimum size, including gaps between rows/columns.
1625 // -1 is used as a magic number meaning empty row/column.
1628 const size_t count
= sizes
.size();
1629 for ( size_t n
= 0; n
< count
; n
++ )
1631 if ( sizes
[n
] != -1 )
1634 total
+= gap
; // separate from the previous column
1643 void wxFlexGridSizer::FindWidthsAndHeights(int nrows
, int ncols
)
1645 // We have to recalculate the sizes in case the item minimum size has
1646 // changed since the previous layout, or the item has been hidden using
1647 // wxSizer::Show(). If all the items in a row/column are hidden, the final
1648 // dimension of the row/column will be -1, indicating that the column
1649 // itself is hidden.
1650 m_rowHeights
.assign(nrows
, -1);
1651 m_colWidths
.assign(ncols
, -1);
1653 // n is the index of the item in left-to-right top-to-bottom order
1655 for ( wxSizerItemList::iterator i
= m_children
.begin();
1656 i
!= m_children
.end();
1659 wxSizerItem
* const item
= *i
;
1660 if ( item
->IsShown() )
1662 // NOTE: Not doing the calculation here, this is just
1663 // for finding max values.
1664 const wxSize
sz(item
->GetMinSizeWithBorder());
1666 const int row
= n
/ ncols
;
1667 const int col
= n
% ncols
;
1669 if ( sz
.y
> m_rowHeights
[row
] )
1670 m_rowHeights
[row
] = sz
.y
;
1671 if ( sz
.x
> m_colWidths
[col
] )
1672 m_colWidths
[col
] = sz
.x
;
1676 AdjustForFlexDirection();
1678 m_calculatedMinSize
= wxSize(SumArraySizes(m_colWidths
, m_hgap
),
1679 SumArraySizes(m_rowHeights
, m_vgap
));
1682 wxSize
wxFlexGridSizer::CalcMin()
1687 // Number of rows/columns can change as items are added or removed.
1688 if ( !CalcRowsCols(nrows
, ncols
) )
1692 // We have to recalculate the sizes in case the item minimum size has
1693 // changed since the previous layout, or the item has been hidden using
1694 // wxSizer::Show(). If all the items in a row/column are hidden, the final
1695 // dimension of the row/column will be -1, indicating that the column
1696 // itself is hidden.
1697 m_rowHeights
.assign(nrows
, -1);
1698 m_colWidths
.assign(ncols
, -1);
1700 for ( wxSizerItemList::iterator i
= m_children
.begin();
1701 i
!= m_children
.end();
1704 wxSizerItem
* const item
= *i
;
1705 if ( item
->IsShown() )
1711 // The stage of looking for max values in each row/column has been
1712 // made a separate function, since it's reused in AdjustForGrowables.
1713 FindWidthsAndHeights(nrows
,ncols
);
1715 return m_calculatedMinSize
;
1718 void wxFlexGridSizer::AdjustForFlexDirection()
1720 // the logic in CalcMin works when we resize flexibly in both directions
1721 // but maybe this is not the case
1722 if ( m_flexDirection
!= wxBOTH
)
1724 // select the array corresponding to the direction in which we do *not*
1726 wxArrayInt
& array
= m_flexDirection
== wxVERTICAL
? m_colWidths
1729 const size_t count
= array
.GetCount();
1731 // find the largest value in this array
1735 for ( n
= 0; n
< count
; ++n
)
1737 if ( array
[n
] > largest
)
1741 // and now fill it with the largest value
1742 for ( n
= 0; n
< count
; ++n
)
1744 // don't touch hidden rows
1745 if ( array
[n
] != -1 )
1751 // helper of AdjustForGrowables() which is called for rows/columns separately
1754 // delta: the extra space, we do nothing unless it's positive
1755 // growable: indices or growable rows/cols in sizes array
1756 // sizes: the height/widths of rows/cols to adjust
1757 // proportions: proportions of the growable rows/cols or NULL if they all
1758 // should be assumed to have proportion of 1
1760 DoAdjustForGrowables(int delta
,
1761 const wxArrayInt
& growable
,
1763 const wxArrayInt
*proportions
)
1768 // total sum of proportions of all non-hidden rows
1769 int sum_proportions
= 0;
1771 // number of currently shown growable rows
1774 const int max_idx
= sizes
.size();
1776 const size_t count
= growable
.size();
1778 for ( idx
= 0; idx
< count
; idx
++ )
1780 // Since the number of rows/columns can change as items are
1781 // inserted/deleted, we need to verify at runtime that the
1782 // requested growable rows/columns are still valid.
1783 if ( growable
[idx
] >= max_idx
)
1786 // If all items in a row/column are hidden, that row/column will
1787 // have a dimension of -1. This causes the row/column to be
1788 // hidden completely.
1789 if ( sizes
[growable
[idx
]] == -1 )
1793 sum_proportions
+= (*proportions
)[idx
];
1801 // the remaining extra free space, adjusted during each iteration
1802 for ( idx
= 0; idx
< count
; idx
++ )
1804 if ( growable
[idx
] >= max_idx
)
1807 if ( sizes
[ growable
[idx
] ] == -1 )
1811 if ( sum_proportions
== 0 )
1813 // no growable rows -- divide extra space evenly among all
1814 cur_delta
= delta
/num
;
1817 else // allocate extra space proportionally
1819 const int cur_prop
= (*proportions
)[idx
];
1820 cur_delta
= (delta
*cur_prop
)/sum_proportions
;
1821 sum_proportions
-= cur_prop
;
1824 sizes
[growable
[idx
]] += cur_delta
;
1829 void wxFlexGridSizer::AdjustForGrowables(const wxSize
& sz
)
1832 // by the time this function is called, the sizer should be already fully
1833 // initialized and hence the number of its columns and rows is known and we
1834 // can check that all indices in m_growableCols/Rows are valid (see also
1835 // comments in AddGrowableCol/Row())
1836 if ( !m_rows
|| !m_cols
)
1840 int nrows
= CalcRows();
1842 for ( size_t n
= 0; n
< m_growableRows
.size(); n
++ )
1844 wxASSERT_MSG( m_growableRows
[n
] < nrows
,
1845 "invalid growable row index" );
1851 int ncols
= CalcCols();
1853 for ( size_t n
= 0; n
< m_growableCols
.size(); n
++ )
1855 wxASSERT_MSG( m_growableCols
[n
] < ncols
,
1856 "invalid growable column index" );
1860 #endif // wxDEBUG_LEVEL
1863 if ( (m_flexDirection
& wxHORIZONTAL
) || (m_growMode
!= wxFLEX_GROWMODE_NONE
) )
1865 DoAdjustForGrowables
1867 sz
.x
- m_calculatedMinSize
.x
,
1870 m_growMode
== wxFLEX_GROWMODE_SPECIFIED
? &m_growableColsProportions
1874 // This gives nested objects that benefit from knowing one size
1875 // component in advance the chance to use that.
1876 bool didAdjustMinSize
= false;
1878 // Iterate over all items and inform about column width
1879 const int ncols
= GetEffectiveColsCount();
1881 for ( wxSizerItemList::iterator i
= m_children
.begin();
1882 i
!= m_children
.end();
1885 didAdjustMinSize
|= (*i
)->InformFirstDirection(wxHORIZONTAL
, m_colWidths
[col
], sz
.y
- m_calculatedMinSize
.y
);
1886 if ( ++col
== ncols
)
1890 // Only redo if info was actually used
1891 if( didAdjustMinSize
)
1893 DoAdjustForGrowables
1895 sz
.x
- m_calculatedMinSize
.x
,
1898 m_growMode
== wxFLEX_GROWMODE_SPECIFIED
? &m_growableColsProportions
1904 if ( (m_flexDirection
& wxVERTICAL
) || (m_growMode
!= wxFLEX_GROWMODE_NONE
) )
1906 // pass NULL instead of proportions if the grow mode is ALL as we
1907 // should treat all rows as having proportion of 1 then
1908 DoAdjustForGrowables
1910 sz
.y
- m_calculatedMinSize
.y
,
1913 m_growMode
== wxFLEX_GROWMODE_SPECIFIED
? &m_growableRowsProportions
1919 bool wxFlexGridSizer::IsRowGrowable( size_t idx
)
1921 return m_growableRows
.Index( idx
) != wxNOT_FOUND
;
1924 bool wxFlexGridSizer::IsColGrowable( size_t idx
)
1926 return m_growableCols
.Index( idx
) != wxNOT_FOUND
;
1929 void wxFlexGridSizer::AddGrowableRow( size_t idx
, int proportion
)
1931 wxASSERT_MSG( !IsRowGrowable( idx
),
1932 "AddGrowableRow() called for growable row" );
1934 // notice that we intentionally don't check the index validity here in (the
1935 // common) case when the number of rows was not specified in the ctor -- in
1936 // this case it will be computed only later, when all items are added to
1937 // the sizer, and the check will be done in AdjustForGrowables()
1938 wxCHECK_RET( !m_rows
|| idx
< (size_t)m_rows
, "invalid row index" );
1940 m_growableRows
.Add( idx
);
1941 m_growableRowsProportions
.Add( proportion
);
1944 void wxFlexGridSizer::AddGrowableCol( size_t idx
, int proportion
)
1946 wxASSERT_MSG( !IsColGrowable( idx
),
1947 "AddGrowableCol() called for growable column" );
1949 // see comment in AddGrowableRow(): although it's less common to omit the
1950 // specification of the number of columns, it still can also happen
1951 wxCHECK_RET( !m_cols
|| idx
< (size_t)m_cols
, "invalid column index" );
1953 m_growableCols
.Add( idx
);
1954 m_growableColsProportions
.Add( proportion
);
1957 // helper function for RemoveGrowableCol/Row()
1959 DoRemoveFromArrays(size_t idx
, wxArrayInt
& items
, wxArrayInt
& proportions
)
1961 const size_t count
= items
.size();
1962 for ( size_t n
= 0; n
< count
; n
++ )
1964 if ( (size_t)items
[n
] == idx
)
1967 proportions
.RemoveAt(n
);
1972 wxFAIL_MSG( wxT("column/row is already not growable") );
1975 void wxFlexGridSizer::RemoveGrowableCol( size_t idx
)
1977 DoRemoveFromArrays(idx
, m_growableCols
, m_growableColsProportions
);
1980 void wxFlexGridSizer::RemoveGrowableRow( size_t idx
)
1982 DoRemoveFromArrays(idx
, m_growableRows
, m_growableRowsProportions
);
1985 //---------------------------------------------------------------------------
1987 //---------------------------------------------------------------------------
1989 wxSizerItem
*wxBoxSizer::AddSpacer(int size
)
1991 return IsVertical() ? Add(0, size
) : Add(size
, 0);
1998 Helper of RecalcSizes(): checks if there is enough remaining space for the
1999 min size of the given item and returns its min size or the entire remaining
2000 space depending on which one is greater.
2002 This function updates the remaining space parameter to account for the size
2003 effectively allocated to the item.
2006 GetMinOrRemainingSize(int orient
, const wxSizerItem
*item
, int *remainingSpace_
)
2008 int& remainingSpace
= *remainingSpace_
;
2011 if ( remainingSpace
> 0 )
2013 const wxSize sizeMin
= item
->GetMinSizeWithBorder();
2014 size
= orient
== wxHORIZONTAL
? sizeMin
.x
: sizeMin
.y
;
2016 if ( size
>= remainingSpace
)
2018 // truncate the item to fit in the remaining space, this is better
2019 // than showing it only partially in general, even if both choices
2020 // are bad -- but there is nothing else we can do
2021 size
= remainingSpace
;
2024 remainingSpace
-= size
;
2026 else // no remaining space
2028 // no space at all left, no need to even query the item for its min
2029 // size as we can't give it to it anyhow
2036 } // anonymous namespace
2038 void wxBoxSizer::RecalcSizes()
2040 if ( m_children
.empty() )
2043 const wxCoord totalMinorSize
= GetSizeInMinorDir(m_size
);
2044 const wxCoord totalMajorSize
= GetSizeInMajorDir(m_size
);
2046 // the amount of free space which we should redistribute among the
2047 // stretchable items (i.e. those with non zero proportion)
2048 int delta
= totalMajorSize
- GetSizeInMajorDir(m_minSize
);
2050 // declare loop variables used below:
2051 wxSizerItemList::const_iterator i
; // iterator in m_children list
2052 unsigned n
= 0; // item index in majorSizes array
2055 // First, inform item about the available size in minor direction as this
2056 // can change their size in the major direction. Also compute the number of
2057 // visible items and sum of their min sizes in major direction.
2059 int minMajorSize
= 0;
2060 for ( i
= m_children
.begin(); i
!= m_children
.end(); ++i
)
2062 wxSizerItem
* const item
= *i
;
2064 if ( !item
->IsShown() )
2067 wxSize szMinPrev
= item
->GetMinSizeWithBorder();
2068 item
->InformFirstDirection(m_orient
^wxBOTH
,totalMinorSize
,delta
);
2069 wxSize szMin
= item
->GetMinSizeWithBorder();
2070 int deltaChange
= GetSizeInMajorDir(szMin
-szMinPrev
);
2073 // Since we passed available space along to the item, it should not
2074 // take too much, so delta should not become negative.
2075 delta
-= deltaChange
;
2077 minMajorSize
+= GetSizeInMajorDir(item
->GetMinSizeWithBorder());
2080 // update our min size and delta which may have changed
2081 SizeInMajorDir(m_minSize
) = minMajorSize
;
2082 delta
= totalMajorSize
- minMajorSize
;
2085 // space and sum of proportions for the remaining items, both may change
2087 wxCoord remaining
= totalMajorSize
;
2088 int totalProportion
= m_totalProportion
;
2090 // size of the (visible) items in major direction, -1 means "not fixed yet"
2091 wxVector
<int> majorSizes(GetItemCount(), wxDefaultCoord
);
2094 // Check for the degenerated case when we don't have enough space for even
2095 // the min sizes of all the items: in this case we really can't do much
2096 // more than to allocate the min size to as many of fixed size items as
2097 // possible (on the assumption that variable size items such as text zones
2098 // or list boxes may use scrollbars to show their content even if their
2099 // size is less than min size but that fixed size items such as buttons
2100 // will suffer even more if we don't give them their min size)
2101 if ( totalMajorSize
< minMajorSize
)
2103 // Second degenerated case pass: allocate min size to all fixed size
2105 for ( i
= m_children
.begin(), n
= 0; i
!= m_children
.end(); ++i
, ++n
)
2107 wxSizerItem
* const item
= *i
;
2109 if ( !item
->IsShown() )
2112 // deal with fixed size items only during this pass
2113 if ( item
->GetProportion() )
2116 majorSizes
[n
] = GetMinOrRemainingSize(m_orient
, item
, &remaining
);
2120 // Third degenerated case pass: allocate min size to all the remaining,
2121 // i.e. non-fixed size, items.
2122 for ( i
= m_children
.begin(), n
= 0; i
!= m_children
.end(); ++i
, ++n
)
2124 wxSizerItem
* const item
= *i
;
2126 if ( !item
->IsShown() )
2129 // we've already dealt with fixed size items above
2130 if ( !item
->GetProportion() )
2133 majorSizes
[n
] = GetMinOrRemainingSize(m_orient
, item
, &remaining
);
2136 else // we do have enough space to give at least min sizes to all items
2138 // Second and maybe more passes in the non-degenerated case: deal with
2139 // fixed size items and items whose min size is greater than what we
2140 // would allocate to them taking their proportion into account. For
2141 // both of them, we will just use their min size, but for the latter we
2142 // also need to reexamine all the items as the items which fitted
2143 // before we adjusted their size upwards might not fit any more. This
2144 // does make for a quadratic algorithm but it's not obvious how to
2145 // avoid it and hopefully it's not a huge problem in practice as the
2146 // sizers don't have many items usually (and, of course, the algorithm
2147 // still reduces into a linear one if there is enough space for all the
2149 bool nonFixedSpaceChanged
= false;
2150 for ( i
= m_children
.begin(), n
= 0; ; ++i
, ++n
)
2152 if ( nonFixedSpaceChanged
)
2154 i
= m_children
.begin();
2156 nonFixedSpaceChanged
= false;
2159 // check for the end of the loop only after the check above as
2160 // otherwise we wouldn't do another pass if the last child resulted
2161 // in non fixed space reduction
2162 if ( i
== m_children
.end() )
2165 wxSizerItem
* const item
= *i
;
2167 if ( !item
->IsShown() )
2170 // don't check the item which we had already dealt with during a
2171 // previous pass (this is more than an optimization, the code
2172 // wouldn't work correctly if we kept adjusting for the same item
2173 // over and over again)
2174 if ( majorSizes
[n
] != wxDefaultCoord
)
2177 wxCoord minMajor
= GetSizeInMajorDir(item
->GetMinSizeWithBorder());
2179 // it doesn't make sense for min size to be negative but right now
2180 // it's possible to create e.g. a spacer with (-1, 10) as size and
2181 // people do it in their code apparently (see #11842) so ensure
2182 // that we don't use this -1 as real min size as it conflicts with
2183 // the meaning we use for it here and negative min sizes just don't
2184 // make sense anyhow (which is why it might be a better idea to
2185 // deal with them at wxSizerItem level in the future but for now
2186 // this is the minimal fix for the bug)
2190 const int propItem
= item
->GetProportion();
2193 // is the desired size of this item big enough?
2194 if ( (remaining
*propItem
)/totalProportion
>= minMajor
)
2196 // yes, it is, we'll determine the real size of this
2197 // item later, for now just leave it as wxDefaultCoord
2201 // the proportion of this item won't count, it has
2202 // effectively become fixed
2203 totalProportion
-= propItem
;
2206 // we can already allocate space for this item
2207 majorSizes
[n
] = minMajor
;
2209 // change the amount of the space remaining to the other items,
2210 // as this can result in not being able to satisfy their
2211 // proportions any more we will need to redo another loop
2213 remaining
-= minMajor
;
2215 nonFixedSpaceChanged
= true;
2219 // Last by one pass: distribute the remaining space among the non-fixed
2220 // items whose size weren't fixed yet according to their proportions.
2221 for ( i
= m_children
.begin(), n
= 0; i
!= m_children
.end(); ++i
, ++n
)
2223 wxSizerItem
* const item
= *i
;
2225 if ( !item
->IsShown() )
2228 if ( majorSizes
[n
] == wxDefaultCoord
)
2230 const int propItem
= item
->GetProportion();
2231 majorSizes
[n
] = (remaining
*propItem
)/totalProportion
;
2233 remaining
-= majorSizes
[n
];
2234 totalProportion
-= propItem
;
2240 // the position at which we put the next child
2241 wxPoint
pt(m_position
);
2244 // Final pass: finally do position the items correctly using their sizes as
2245 // determined above.
2246 for ( i
= m_children
.begin(), n
= 0; i
!= m_children
.end(); ++i
, ++n
)
2248 wxSizerItem
* const item
= *i
;
2250 if ( !item
->IsShown() )
2253 const int majorSize
= majorSizes
[n
];
2255 const wxSize
sizeThis(item
->GetMinSizeWithBorder());
2257 // apply the alignment in the minor direction
2258 wxPoint
posChild(pt
);
2260 wxCoord minorSize
= GetSizeInMinorDir(sizeThis
);
2261 const int flag
= item
->GetFlag();
2262 if ( (flag
& (wxEXPAND
| wxSHAPED
)) || (minorSize
> totalMinorSize
) )
2264 // occupy all the available space if wxEXPAND was given and also if
2265 // the item is too big to fit -- in this case we truncate it below
2266 // its minimal size which is bad but better than not showing parts
2267 // of the window at all
2268 minorSize
= totalMinorSize
;
2270 else if ( flag
& (IsVertical() ? wxALIGN_RIGHT
: wxALIGN_BOTTOM
) )
2272 PosInMinorDir(posChild
) += totalMinorSize
- minorSize
;
2274 // NB: wxCENTRE is used here only for backwards compatibility,
2275 // wxALIGN_CENTRE should be used in new code
2276 else if ( flag
& (wxCENTER
| (IsVertical() ? wxALIGN_CENTRE_HORIZONTAL
2277 : wxALIGN_CENTRE_VERTICAL
)) )
2279 PosInMinorDir(posChild
) += (totalMinorSize
- minorSize
) / 2;
2283 // apply RTL adjustment for horizontal sizers:
2284 if ( !IsVertical() && m_containingWindow
)
2286 posChild
.x
= m_containingWindow
->AdjustForLayoutDirection
2294 // finally set size of this child and advance to the next one
2295 item
->SetDimension(posChild
, SizeFromMajorMinor(majorSize
, minorSize
));
2297 PosInMajorDir(pt
) += majorSize
;
2301 wxSize
wxBoxSizer::CalcMin()
2303 m_totalProportion
= 0;
2304 m_minSize
= wxSize(0, 0);
2306 // The minimal size for the sizer should be big enough to allocate its
2307 // element at least its minimal size but also, and this is the non trivial
2308 // part, to respect the children proportion. To satisfy the latter
2309 // condition we must find the greatest min-size-to-proportion ratio for all
2310 // elements with non-zero proportion.
2311 float maxMinSizeToProp
= 0.;
2312 for ( wxSizerItemList::const_iterator i
= m_children
.begin();
2313 i
!= m_children
.end();
2316 wxSizerItem
* const item
= *i
;
2318 if ( !item
->IsShown() )
2321 const wxSize sizeMinThis
= item
->CalcMin();
2322 if ( const int propThis
= item
->GetProportion() )
2324 float minSizeToProp
= GetSizeInMajorDir(sizeMinThis
);
2325 minSizeToProp
/= propThis
;
2327 if ( minSizeToProp
> maxMinSizeToProp
)
2328 maxMinSizeToProp
= minSizeToProp
;
2330 m_totalProportion
+= item
->GetProportion();
2332 else // fixed size item
2334 // Just account for its size directly
2335 SizeInMajorDir(m_minSize
) += GetSizeInMajorDir(sizeMinThis
);
2338 // In the transversal direction we just need to find the maximum.
2339 if ( GetSizeInMinorDir(sizeMinThis
) > GetSizeInMinorDir(m_minSize
) )
2340 SizeInMinorDir(m_minSize
) = GetSizeInMinorDir(sizeMinThis
);
2343 // Using the max ratio ensures that the min size is big enough for all
2344 // items to have their min size and satisfy the proportions among them.
2345 SizeInMajorDir(m_minSize
) += (int)(maxMinSizeToProp
*m_totalProportion
);
2350 //---------------------------------------------------------------------------
2352 //---------------------------------------------------------------------------
2356 wxStaticBoxSizer::wxStaticBoxSizer( wxStaticBox
*box
, int orient
)
2357 : wxBoxSizer( orient
),
2360 wxASSERT_MSG( box
, wxT("wxStaticBoxSizer needs a static box") );
2362 // do this so that our Detach() is called if the static box is destroyed
2364 m_staticBox
->SetContainingSizer(this);
2367 wxStaticBoxSizer::wxStaticBoxSizer(int orient
, wxWindow
*win
, const wxString
& s
)
2368 : wxBoxSizer(orient
),
2369 m_staticBox(new wxStaticBox(win
, wxID_ANY
, s
))
2372 m_staticBox
->SetContainingSizer(this);
2375 wxStaticBoxSizer::~wxStaticBoxSizer()
2380 void wxStaticBoxSizer::RecalcSizes()
2382 int top_border
, other_border
;
2383 m_staticBox
->GetBordersForSizer(&top_border
, &other_border
);
2385 m_staticBox
->SetSize( m_position
.x
, m_position
.y
, m_size
.x
, m_size
.y
);
2387 wxSize
old_size( m_size
);
2388 m_size
.x
-= 2*other_border
;
2389 m_size
.y
-= top_border
+ other_border
;
2391 wxPoint
old_pos( m_position
);
2392 if (m_staticBox
->GetChildren().GetCount() > 0)
2394 #if defined( __WXGTK20__ )
2395 // if the wxStaticBox has created a wxPizza to contain its children
2396 // (see wxStaticBox::AddChild) then we need to place the items it contains
2397 // in the wxBoxSizer::RecalcSizes() call below using coordinates relative
2398 // to the top-left corner of the staticbox:
2399 m_position
.x
= m_position
.y
= 0;
2400 #elif defined(__WXOSX__) && wxOSX_USE_COCOA
2401 // the distance from the 'inner' content view to the embedded controls
2402 m_position
.x
= m_position
.y
= 10;
2404 // if the wxStaticBox has childrens, then these windows must be placed
2405 // by the wxBoxSizer::RecalcSizes() call below using coordinates relative
2406 // to the top-left corner of the staticbox (but unlike wxGTK, we need
2407 // to keep in count the static borders here!):
2408 m_position
.x
= other_border
;
2409 m_position
.y
= top_border
;
2414 // the windows contained in the staticbox have been created as siblings of the
2415 // staticbox (this is the "old" way of staticbox contents creation); in this
2416 // case we need to position them with coordinates relative to our common parent
2417 m_position
.x
+= other_border
;
2418 m_position
.y
+= top_border
;
2421 wxBoxSizer::RecalcSizes();
2423 m_position
= old_pos
;
2427 wxSize
wxStaticBoxSizer::CalcMin()
2429 int top_border
, other_border
;
2430 m_staticBox
->GetBordersForSizer(&top_border
, &other_border
);
2432 wxSize
ret( wxBoxSizer::CalcMin() );
2433 ret
.x
+= 2*other_border
;
2435 // ensure that we're wide enough to show the static box label (there is no
2436 // need to check for the static box best size in vertical direction though)
2437 const int boxWidth
= m_staticBox
->GetBestSize().x
;
2438 if ( ret
.x
< boxWidth
)
2441 ret
.y
+= other_border
+ top_border
;
2446 void wxStaticBoxSizer::ShowItems( bool show
)
2448 m_staticBox
->Show( show
);
2449 wxBoxSizer::ShowItems( show
);
2452 bool wxStaticBoxSizer::Detach( wxWindow
*window
)
2454 // avoid deleting m_staticBox in our dtor if it's being detached from the
2455 // sizer (which can happen because it's being already destroyed for
2457 if ( window
== m_staticBox
)
2463 return wxSizer::Detach( window
);
2466 #endif // wxUSE_STATBOX
2468 //---------------------------------------------------------------------------
2469 // wxStdDialogButtonSizer
2470 //---------------------------------------------------------------------------
2474 wxStdDialogButtonSizer::wxStdDialogButtonSizer()
2475 : wxBoxSizer(wxHORIZONTAL
)
2477 // Vertical buttons with lots of space on either side
2478 // looks rubbish on WinCE, so let's not do this for now.
2479 // If we are going to use vertical buttons, we should
2480 // put the sizer to the right of other controls in the dialog,
2481 // and that's beyond the scope of this sizer.
2483 bool is_pda
= (wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA
);
2484 // If we have a PDA screen, put yes/no button over
2485 // all other buttons, otherwise on the left side.
2487 m_orient
= wxVERTICAL
;
2490 m_buttonAffirmative
= NULL
;
2491 m_buttonApply
= NULL
;
2492 m_buttonNegative
= NULL
;
2493 m_buttonCancel
= NULL
;
2494 m_buttonHelp
= NULL
;
2497 void wxStdDialogButtonSizer::AddButton(wxButton
*mybutton
)
2499 switch (mybutton
->GetId())
2504 m_buttonAffirmative
= mybutton
;
2507 m_buttonApply
= mybutton
;
2510 m_buttonNegative
= mybutton
;
2514 m_buttonCancel
= mybutton
;
2517 case wxID_CONTEXT_HELP
:
2518 m_buttonHelp
= mybutton
;
2525 void wxStdDialogButtonSizer::SetAffirmativeButton( wxButton
*button
)
2527 m_buttonAffirmative
= button
;
2530 void wxStdDialogButtonSizer::SetNegativeButton( wxButton
*button
)
2532 m_buttonNegative
= button
;
2535 void wxStdDialogButtonSizer::SetCancelButton( wxButton
*button
)
2537 m_buttonCancel
= button
;
2540 void wxStdDialogButtonSizer::Realize()
2543 Add(0, 0, 0, wxLEFT
, 6);
2545 Add((wxWindow
*)m_buttonHelp
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, 6);
2547 if (m_buttonNegative
){
2548 // HIG POLICE BULLETIN - destructive buttons need extra padding
2549 // 24 pixels on either side
2550 Add((wxWindow
*)m_buttonNegative
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, 12);
2553 // extra whitespace between help/negative and cancel/ok buttons
2554 Add(0, 0, 1, wxEXPAND
, 0);
2556 if (m_buttonCancel
){
2557 Add((wxWindow
*)m_buttonCancel
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, 6);
2558 // Cancel or help should be default
2559 // m_buttonCancel->SetDefaultButton();
2562 // Ugh, Mac doesn't really have apply dialogs, so I'll just
2563 // figure the best place is between Cancel and OK
2565 Add((wxWindow
*)m_buttonApply
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, 6);
2567 if (m_buttonAffirmative
){
2568 Add((wxWindow
*)m_buttonAffirmative
, 0, wxALIGN_CENTRE
| wxLEFT
, 6);
2570 if (m_buttonAffirmative
->GetId() == wxID_SAVE
){
2571 // these buttons have set labels under Mac so we should use them
2572 m_buttonAffirmative
->SetLabel(_("Save"));
2573 if (m_buttonNegative
)
2574 m_buttonNegative
->SetLabel(_("Don't Save"));
2578 // Extra space around and at the right
2580 #elif defined(__WXGTK20__)
2581 // http://library.gnome.org/devel/hig-book/stable/windows-alert.html.en
2582 // says that the correct button order is
2584 // [Help] [Alternative] [Cancel] [Affirmative]
2586 // Flags ensuring that margins between the buttons are 6 pixels.
2588 flagsBtn
= wxSizerFlags().Centre().Border(wxLEFT
| wxRIGHT
, 3);
2590 // Margin around the entire sizer button should be 12.
2594 Add(m_buttonHelp
, flagsBtn
);
2596 // Align the rest of the buttons to the right.
2599 if (m_buttonNegative
)
2600 Add(m_buttonNegative
, flagsBtn
);
2603 Add(m_buttonApply
, flagsBtn
);
2606 Add(m_buttonCancel
, flagsBtn
);
2608 if (m_buttonAffirmative
)
2609 Add(m_buttonAffirmative
, flagsBtn
);
2611 // Ensure that the right margin is 12 as well.
2613 #elif defined(__WXMSW__)
2616 // right-justify buttons
2617 Add(0, 0, 1, wxEXPAND
, 0);
2619 if (m_buttonAffirmative
){
2620 Add((wxWindow
*)m_buttonAffirmative
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, m_buttonAffirmative
->ConvertDialogToPixels(wxSize(2, 0)).x
);
2623 if (m_buttonNegative
){
2624 Add((wxWindow
*)m_buttonNegative
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, m_buttonNegative
->ConvertDialogToPixels(wxSize(2, 0)).x
);
2627 if (m_buttonCancel
){
2628 Add((wxWindow
*)m_buttonCancel
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, m_buttonCancel
->ConvertDialogToPixels(wxSize(2, 0)).x
);
2631 Add((wxWindow
*)m_buttonApply
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, m_buttonApply
->ConvertDialogToPixels(wxSize(2, 0)).x
);
2634 Add((wxWindow
*)m_buttonHelp
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, m_buttonHelp
->ConvertDialogToPixels(wxSize(2, 0)).x
);
2636 // GTK+1 and any other platform
2638 // Add(0, 0, 0, wxLEFT, 5); // Not sure what this was for but it unbalances the dialog
2640 Add((wxWindow
*)m_buttonHelp
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, m_buttonHelp
->ConvertDialogToPixels(wxSize(4, 0)).x
);
2642 // extra whitespace between help and cancel/ok buttons
2643 Add(0, 0, 1, wxEXPAND
, 0);
2646 Add((wxWindow
*)m_buttonApply
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, m_buttonApply
->ConvertDialogToPixels(wxSize(4, 0)).x
);
2648 if (m_buttonAffirmative
){
2649 Add((wxWindow
*)m_buttonAffirmative
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, m_buttonAffirmative
->ConvertDialogToPixels(wxSize(4, 0)).x
);
2652 if (m_buttonNegative
){
2653 Add((wxWindow
*)m_buttonNegative
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, m_buttonNegative
->ConvertDialogToPixels(wxSize(4, 0)).x
);
2656 if (m_buttonCancel
){
2657 Add((wxWindow
*)m_buttonCancel
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, m_buttonCancel
->ConvertDialogToPixels(wxSize(4, 0)).x
);
2658 // Cancel or help should be default
2659 // m_buttonCancel->SetDefaultButton();
2665 #endif // wxUSE_BUTTON