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/listimpl.cpp"
38 //---------------------------------------------------------------------------
40 IMPLEMENT_CLASS(wxSizerItem
, wxObject
)
41 IMPLEMENT_CLASS(wxSizer
, wxObject
)
42 IMPLEMENT_CLASS(wxGridSizer
, wxSizer
)
43 IMPLEMENT_CLASS(wxFlexGridSizer
, wxGridSizer
)
44 IMPLEMENT_CLASS(wxBoxSizer
, wxSizer
)
46 IMPLEMENT_CLASS(wxStaticBoxSizer
, wxBoxSizer
)
49 IMPLEMENT_CLASS(wxStdDialogButtonSizer
, wxBoxSizer
)
52 WX_DEFINE_EXPORTED_LIST( wxSizerItemList
)
87 // ----------------------------------------------------------------------------
89 // ----------------------------------------------------------------------------
91 // check for flags conflicts
92 static const int SIZER_FLAGS_MASK
=
94 wxADD_FLAG(wxHORIZONTAL
,
95 wxADD_FLAG(wxVERTICAL
,
100 wxADD_FLAG(wxALIGN_NOT
,
101 wxADD_FLAG(wxALIGN_CENTER_HORIZONTAL
,
102 wxADD_FLAG(wxALIGN_RIGHT
,
103 wxADD_FLAG(wxALIGN_BOTTOM
,
104 wxADD_FLAG(wxALIGN_CENTER_VERTICAL
,
105 wxADD_FLAG(wxFIXED_MINSIZE
,
106 wxADD_FLAG(wxRESERVE_SPACE_EVEN_IF_HIDDEN
,
107 wxADD_FLAG(wxSTRETCH_NOT
,
113 #define ASSERT_VALID_SIZER_FLAGS(f) wxASSERT_VALID_FLAGS(f, SIZER_FLAGS_MASK)
116 void wxSizerItem::Init(const wxSizerFlags
& flags
)
120 m_proportion
= flags
.GetProportion();
121 m_flag
= flags
.GetFlags();
122 m_border
= flags
.GetBorderInPixels();
124 ASSERT_VALID_SIZER_FLAGS( m_flag
);
127 wxSizerItem::wxSizerItem()
138 void wxSizerItem::DoSetWindow(wxWindow
*window
)
140 wxCHECK_RET( window
, wxT("NULL window in wxSizerItem::SetWindow()") );
142 m_kind
= Item_Window
;
145 // window doesn't become smaller than its initial size, whatever happens
146 m_minSize
= window
->GetSize();
148 if ( m_flag
& wxFIXED_MINSIZE
)
149 window
->SetMinSize(m_minSize
);
151 // aspect ratio calculated from initial size
155 wxSizerItem::wxSizerItem(wxWindow
*window
,
161 m_proportion(proportion
),
167 ASSERT_VALID_SIZER_FLAGS( m_flag
);
173 void wxSizerItem::DoSetSizer(wxSizer
*sizer
)
179 wxSizerItem::wxSizerItem(wxSizer
*sizer
,
186 m_proportion(proportion
),
193 ASSERT_VALID_SIZER_FLAGS( m_flag
);
197 // m_minSize is set later
201 void wxSizerItem::DoSetSpacer(const wxSize
& size
)
203 m_kind
= Item_Spacer
;
204 m_spacer
= new wxSizerSpacer(size
);
209 wxSizerItem::wxSizerItem(int width
,
217 m_minSize(width
, height
), // minimal size is the initial size
218 m_proportion(proportion
),
224 ASSERT_VALID_SIZER_FLAGS( m_flag
);
226 DoSetSpacer(wxSize(width
, height
));
229 wxSizerItem::~wxSizerItem()
235 void wxSizerItem::Free()
243 m_window
->SetContainingSizer(NULL
);
256 wxFAIL_MSG( wxT("unexpected wxSizerItem::m_kind") );
262 wxSize
wxSizerItem::GetSpacer() const
265 if ( m_kind
== Item_Spacer
)
266 size
= m_spacer
->GetSize();
272 wxSize
wxSizerItem::GetSize() const
281 ret
= m_window
->GetSize();
285 ret
= m_sizer
->GetSize();
289 ret
= m_spacer
->GetSize();
294 wxFAIL_MSG( wxT("unexpected wxSizerItem::m_kind") );
301 if (m_flag
& wxNORTH
)
303 if (m_flag
& wxSOUTH
)
309 bool wxSizerItem::InformFirstDirection(int direction
, int size
, int availableOtherDir
)
311 // The size that come here will be including borders. Child items should get it
315 if( direction
==wxHORIZONTAL
)
322 else if( direction
==wxVERTICAL
)
324 if (m_flag
& wxNORTH
)
326 if (m_flag
& wxSOUTH
)
332 // Pass the information along to the held object
335 didUse
= GetSizer()->InformFirstDirection(direction
,size
,availableOtherDir
);
337 m_minSize
= GetSizer()->CalcMin();
341 didUse
= GetWindow()->InformFirstDirection(direction
,size
,availableOtherDir
);
343 m_minSize
= m_window
->GetEffectiveMinSize();
345 // This information is useful for items with wxSHAPED flag, since
346 // we can request an optimal min size for such an item. Even if
347 // we overwrite the m_minSize member here, we can read it back from
348 // the owned window (happens automatically).
349 if( (m_flag
& wxSHAPED
) && (m_flag
& wxEXPAND
) && direction
)
351 if( !wxIsNullDouble(m_ratio
) )
353 wxCHECK_MSG( (m_proportion
==0), false, wxT("Shaped item, non-zero proportion in wxSizerItem::InformFirstDirection()") );
354 if( direction
==wxHORIZONTAL
&& !wxIsNullDouble(m_ratio
) )
356 // Clip size so that we don't take too much
357 if( availableOtherDir
>=0 && int(size
/m_ratio
)-m_minSize
.y
>availableOtherDir
)
358 size
= int((availableOtherDir
+m_minSize
.y
)*m_ratio
);
359 m_minSize
= wxSize(size
,int(size
/m_ratio
));
361 else if( direction
==wxVERTICAL
)
363 // Clip size so that we don't take too much
364 if( availableOtherDir
>=0 && int(size
*m_ratio
)-m_minSize
.x
>availableOtherDir
)
365 size
= int((availableOtherDir
+m_minSize
.x
)/m_ratio
);
366 m_minSize
= wxSize(int(size
*m_ratio
),size
);
376 wxSize
wxSizerItem::CalcMin()
380 m_minSize
= m_sizer
->GetMinSize();
382 // if we have to preserve aspect ratio _AND_ this is
383 // the first-time calculation, consider ret to be initial size
384 if ( (m_flag
& wxSHAPED
) && wxIsNullDouble(m_ratio
) )
387 else if ( IsWindow() )
389 // Since the size of the window may change during runtime, we
390 // should use the current minimal/best size.
391 m_minSize
= m_window
->GetEffectiveMinSize();
394 return GetMinSizeWithBorder();
397 wxSize
wxSizerItem::GetMinSizeWithBorder() const
399 wxSize ret
= m_minSize
;
405 if (m_flag
& wxNORTH
)
407 if (m_flag
& wxSOUTH
)
414 void wxSizerItem::SetDimension( const wxPoint
& pos_
, const wxSize
& size_
)
418 if (m_flag
& wxSHAPED
)
420 // adjust aspect ratio
421 int rwidth
= (int) (size
.y
* m_ratio
);
425 int rheight
= (int) (size
.x
/ m_ratio
);
426 // add vertical space
427 if (m_flag
& wxALIGN_CENTER_VERTICAL
)
428 pos
.y
+= (size
.y
- rheight
) / 2;
429 else if (m_flag
& wxALIGN_BOTTOM
)
430 pos
.y
+= (size
.y
- rheight
);
431 // use reduced dimensions
434 else if (rwidth
< size
.x
)
436 // add horizontal space
437 if (m_flag
& wxALIGN_CENTER_HORIZONTAL
)
438 pos
.x
+= (size
.x
- rwidth
) / 2;
439 else if (m_flag
& wxALIGN_RIGHT
)
440 pos
.x
+= (size
.x
- rwidth
);
445 // This is what GetPosition() returns. Since we calculate
446 // borders afterwards, GetPosition() will be the left/top
447 // corner of the surrounding border.
459 if (m_flag
& wxNORTH
)
464 if (m_flag
& wxSOUTH
)
474 m_rect
= wxRect(pos
, size
);
479 wxFAIL_MSG( wxT("can't set size of uninitialized sizer item") );
484 // Use wxSIZE_FORCE_EVENT here since a sizer item might
485 // have changed alignment or some other property which would
486 // not change the size of the window. In such a case, no
487 // wxSizeEvent would normally be generated and thus the
488 // control wouldn't get layed out correctly here.
490 m_window
->SetSize(pos
.x
, pos
.y
, size
.x
, size
.y
,
491 wxSIZE_ALLOW_MINUS_ONE
|wxSIZE_FORCE_EVENT
);
493 m_window
->SetSize(pos
.x
, pos
.y
, size
.x
, size
.y
,
494 wxSIZE_ALLOW_MINUS_ONE
);
499 m_sizer
->SetDimension(pos
, size
);
503 m_spacer
->SetSize(size
);
508 wxFAIL_MSG( wxT("unexpected wxSizerItem::m_kind") );
512 void wxSizerItem::DeleteWindows()
521 //We are deleting the window from this sizer - normally
522 //the window destroys the sizer associated with it,
523 //which might destroy this, which we don't want
524 m_window
->SetContainingSizer(NULL
);
526 //Putting this after the switch will result in a spacer
527 //not being deleted properly on destruction
532 m_sizer
->DeleteWindows();
537 wxFAIL_MSG( wxT("unexpected wxSizerItem::m_kind") );
542 void wxSizerItem::Show( bool show
)
547 wxFAIL_MSG( wxT("can't show uninitialized sizer item") );
551 m_window
->Show(show
);
559 m_spacer
->Show(show
);
564 wxFAIL_MSG( wxT("unexpected wxSizerItem::m_kind") );
568 bool wxSizerItem::IsShown() const
570 if ( m_flag
& wxRESERVE_SPACE_EVEN_IF_HIDDEN
)
576 // we may be called from CalcMin(), just return false so that we're
581 return m_window
->IsShown();
584 // arbitrarily decide that if at least one of our elements is
585 // shown, so are we (this arbitrariness is the reason for
586 // deprecating this function)
588 // Some apps (such as dialog editors) depend on an empty sizer still
589 // being laid out correctly and reporting the correct size and position.
590 if (m_sizer
->GetChildren().GetCount() == 0)
593 for ( wxSizerItemList::compatibility_iterator
594 node
= m_sizer
->GetChildren().GetFirst();
596 node
= node
->GetNext() )
598 if ( node
->GetData()->IsShown() )
605 return m_spacer
->IsShown();
609 wxFAIL_MSG( wxT("unexpected wxSizerItem::m_kind") );
615 #if WXWIN_COMPATIBILITY_2_6
616 void wxSizerItem::SetOption( int option
)
618 SetProportion( option
);
621 int wxSizerItem::GetOption() const
623 return GetProportion();
625 #endif // WXWIN_COMPATIBILITY_2_6
628 //---------------------------------------------------------------------------
630 //---------------------------------------------------------------------------
634 WX_CLEAR_LIST(wxSizerItemList
, m_children
);
637 wxSizerItem
* wxSizer::Insert( size_t index
, wxSizerItem
*item
)
639 m_children
.Insert( index
, item
);
641 if ( item
->GetWindow() )
642 item
->GetWindow()->SetContainingSizer( this );
644 if ( item
->GetSizer() )
645 item
->GetSizer()->SetContainingWindow( m_containingWindow
);
650 void wxSizer::SetContainingWindow(wxWindow
*win
)
652 if ( win
== m_containingWindow
)
655 m_containingWindow
= win
;
657 // set the same window for all nested sizers as well, they also are in the
659 for ( wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
661 node
= node
->GetNext() )
663 wxSizerItem
*const item
= node
->GetData();
664 wxSizer
*const sizer
= item
->GetSizer();
668 sizer
->SetContainingWindow(win
);
673 #if WXWIN_COMPATIBILITY_2_6
674 bool wxSizer::Remove( wxWindow
*window
)
676 return Detach( window
);
678 #endif // WXWIN_COMPATIBILITY_2_6
680 bool wxSizer::Remove( wxSizer
*sizer
)
682 wxASSERT_MSG( sizer
, wxT("Removing NULL sizer") );
684 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
687 wxSizerItem
*item
= node
->GetData();
689 if (item
->GetSizer() == sizer
)
692 m_children
.Erase( node
);
696 node
= node
->GetNext();
702 bool wxSizer::Remove( int index
)
704 wxCHECK_MSG( index
>= 0 && (size_t)index
< m_children
.GetCount(),
706 wxT("Remove index is out of range") );
708 wxSizerItemList::compatibility_iterator node
= m_children
.Item( index
);
710 wxCHECK_MSG( node
, false, wxT("Failed to find child node") );
712 delete node
->GetData();
713 m_children
.Erase( node
);
718 bool wxSizer::Detach( wxSizer
*sizer
)
720 wxASSERT_MSG( sizer
, wxT("Detaching NULL sizer") );
722 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
725 wxSizerItem
*item
= node
->GetData();
727 if (item
->GetSizer() == sizer
)
731 m_children
.Erase( node
);
734 node
= node
->GetNext();
740 bool wxSizer::Detach( wxWindow
*window
)
742 wxASSERT_MSG( window
, wxT("Detaching NULL window") );
744 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
747 wxSizerItem
*item
= node
->GetData();
749 if (item
->GetWindow() == window
)
752 m_children
.Erase( node
);
755 node
= node
->GetNext();
761 bool wxSizer::Detach( int index
)
763 wxCHECK_MSG( index
>= 0 && (size_t)index
< m_children
.GetCount(),
765 wxT("Detach index is out of range") );
767 wxSizerItemList::compatibility_iterator node
= m_children
.Item( index
);
769 wxCHECK_MSG( node
, false, wxT("Failed to find child node") );
771 wxSizerItem
*item
= node
->GetData();
773 if ( item
->IsSizer() )
777 m_children
.Erase( node
);
781 bool wxSizer::Replace( wxWindow
*oldwin
, wxWindow
*newwin
, bool recursive
)
783 wxASSERT_MSG( oldwin
, wxT("Replacing NULL window") );
784 wxASSERT_MSG( newwin
, wxT("Replacing with NULL window") );
786 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
789 wxSizerItem
*item
= node
->GetData();
791 if (item
->GetWindow() == oldwin
)
793 item
->AssignWindow(newwin
);
794 newwin
->SetContainingSizer( this );
797 else if (recursive
&& item
->IsSizer())
799 if (item
->GetSizer()->Replace( oldwin
, newwin
, true ))
803 node
= node
->GetNext();
809 bool wxSizer::Replace( wxSizer
*oldsz
, wxSizer
*newsz
, bool recursive
)
811 wxASSERT_MSG( oldsz
, wxT("Replacing NULL sizer") );
812 wxASSERT_MSG( newsz
, wxT("Replacing with NULL sizer") );
814 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
817 wxSizerItem
*item
= node
->GetData();
819 if (item
->GetSizer() == oldsz
)
821 item
->AssignSizer(newsz
);
824 else if (recursive
&& item
->IsSizer())
826 if (item
->GetSizer()->Replace( oldsz
, newsz
, true ))
830 node
= node
->GetNext();
836 bool wxSizer::Replace( size_t old
, wxSizerItem
*newitem
)
838 wxCHECK_MSG( old
< m_children
.GetCount(), false, wxT("Replace index is out of range") );
839 wxASSERT_MSG( newitem
, wxT("Replacing with NULL item") );
841 wxSizerItemList::compatibility_iterator node
= m_children
.Item( old
);
843 wxCHECK_MSG( node
, false, wxT("Failed to find child node") );
845 wxSizerItem
*item
= node
->GetData();
846 node
->SetData(newitem
);
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 ),
1328 wxGridSizer::wxGridSizer( int cols
, const wxSize
& gap
)
1329 : m_rows( cols
== 0 ? 1 : 0 ),
1331 m_vgap( gap
.GetHeight() ),
1332 m_hgap( gap
.GetWidth() )
1336 wxGridSizer::wxGridSizer( int rows
, int cols
, int vgap
, int hgap
)
1337 : m_rows( rows
|| cols
? rows
: 1 ),
1344 wxGridSizer::wxGridSizer( int rows
, int cols
, const wxSize
& gap
)
1345 : m_rows( rows
|| cols
? rows
: 1 ),
1347 m_vgap( gap
.GetHeight() ),
1348 m_hgap( gap
.GetWidth() )
1352 wxSizerItem
*wxGridSizer::Insert(size_t index
, wxSizerItem
*item
)
1354 // if only the number of columns or the number of rows is specified for a
1355 // sizer, arbitrarily many items can be added to it but if both of them are
1356 // fixed, then the sizer can't have more than that many items -- check for
1357 // this here to ensure that we detect errors as soon as possible
1358 if ( m_cols
&& m_rows
)
1360 const int nitems
= m_children
.GetCount();
1361 if ( nitems
== m_cols
*m_rows
)
1365 "too many items (%d > %d*%d) in grid sizer (maybe you "
1366 "should omit the number of either rows or columns?)",
1367 nitems
+ 1, m_cols
, m_rows
)
1370 // additionally, continuing to use the specified number of columns
1371 // and rows is not a good idea as callers of CalcRowsCols() expect
1372 // that all sizer items can fit into m_cols-/m_rows-sized arrays
1373 // which is not the case if there are too many items and results in
1374 // crashes, so let it compute the number of rows automatically by
1375 // forgetting the (wrong) number of rows specified (this also has a
1376 // nice side effect of giving only one assert even if there are
1377 // many more items than allowed in this sizer)
1382 return wxSizer::Insert(index
, item
);
1385 int wxGridSizer::CalcRowsCols(int& nrows
, int& ncols
) const
1387 const int nitems
= m_children
.GetCount();
1389 ncols
= GetEffectiveColsCount();
1390 nrows
= GetEffectiveRowsCount();
1392 // Since Insert() checks for overpopulation, the following
1393 // should only assert if the grid was shrunk via SetRows() / SetCols()
1394 wxASSERT_MSG( nitems
<= ncols
*nrows
, "logic error in wxGridSizer" );
1399 void wxGridSizer::RecalcSizes()
1401 int nitems
, nrows
, ncols
;
1402 if ( (nitems
= CalcRowsCols(nrows
, ncols
)) == 0 )
1405 wxSize
sz( GetSize() );
1406 wxPoint
pt( GetPosition() );
1408 int w
= (sz
.x
- (ncols
- 1) * m_hgap
) / ncols
;
1409 int h
= (sz
.y
- (nrows
- 1) * m_vgap
) / nrows
;
1412 for (int c
= 0; c
< ncols
; c
++)
1415 for (int r
= 0; r
< nrows
; r
++)
1417 int i
= r
* ncols
+ c
;
1420 wxSizerItemList::compatibility_iterator node
= m_children
.Item( i
);
1422 wxASSERT_MSG( node
, wxT("Failed to find SizerItemList node") );
1424 SetItemBounds( node
->GetData(), x
, y
, w
, h
);
1432 wxSize
wxGridSizer::CalcMin()
1435 if ( CalcRowsCols(nrows
, ncols
) == 0 )
1438 // Find the max width and height for any component
1442 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
1445 wxSizerItem
*item
= node
->GetData();
1446 wxSize
sz( item
->CalcMin() );
1448 w
= wxMax( w
, sz
.x
);
1449 h
= wxMax( h
, sz
.y
);
1451 node
= node
->GetNext();
1454 // In case we have a nested sizer with a two step algo , give it
1455 // a chance to adjust to that (we give it width component)
1456 node
= m_children
.GetFirst();
1457 bool didChangeMinSize
= false;
1460 wxSizerItem
*item
= node
->GetData();
1461 didChangeMinSize
|= item
->InformFirstDirection( wxHORIZONTAL
, w
, -1 );
1463 node
= node
->GetNext();
1466 // And redo iteration in case min size changed
1467 if( didChangeMinSize
)
1469 node
= m_children
.GetFirst();
1473 wxSizerItem
*item
= node
->GetData();
1474 wxSize
sz( item
->GetMinSizeWithBorder() );
1476 w
= wxMax( w
, sz
.x
);
1477 h
= wxMax( h
, sz
.y
);
1479 node
= node
->GetNext();
1483 return wxSize( ncols
* w
+ (ncols
-1) * m_hgap
,
1484 nrows
* h
+ (nrows
-1) * m_vgap
);
1487 void wxGridSizer::SetItemBounds( wxSizerItem
*item
, int x
, int y
, int w
, int h
)
1490 wxSize
sz( item
->GetMinSizeWithBorder() );
1491 int flag
= item
->GetFlag();
1493 if ((flag
& wxEXPAND
) || (flag
& wxSHAPED
))
1499 if (flag
& wxALIGN_CENTER_HORIZONTAL
)
1501 pt
.x
= x
+ (w
- sz
.x
) / 2;
1503 else if (flag
& wxALIGN_RIGHT
)
1505 pt
.x
= x
+ (w
- sz
.x
);
1508 if (flag
& wxALIGN_CENTER_VERTICAL
)
1510 pt
.y
= y
+ (h
- sz
.y
) / 2;
1512 else if (flag
& wxALIGN_BOTTOM
)
1514 pt
.y
= y
+ (h
- sz
.y
);
1518 item
->SetDimension(pt
, sz
);
1521 //---------------------------------------------------------------------------
1523 //---------------------------------------------------------------------------
1525 wxFlexGridSizer::wxFlexGridSizer( int cols
, int vgap
, int hgap
)
1526 : wxGridSizer( cols
, vgap
, hgap
),
1527 m_flexDirection(wxBOTH
),
1528 m_growMode(wxFLEX_GROWMODE_SPECIFIED
)
1532 wxFlexGridSizer::wxFlexGridSizer( int cols
, const wxSize
& gap
)
1533 : wxGridSizer( cols
, gap
),
1534 m_flexDirection(wxBOTH
),
1535 m_growMode(wxFLEX_GROWMODE_SPECIFIED
)
1539 wxFlexGridSizer::wxFlexGridSizer( int rows
, int cols
, int vgap
, int hgap
)
1540 : wxGridSizer( rows
, cols
, vgap
, hgap
),
1541 m_flexDirection(wxBOTH
),
1542 m_growMode(wxFLEX_GROWMODE_SPECIFIED
)
1546 wxFlexGridSizer::wxFlexGridSizer( int rows
, int cols
, const wxSize
& gap
)
1547 : wxGridSizer( rows
, cols
, gap
),
1548 m_flexDirection(wxBOTH
),
1549 m_growMode(wxFLEX_GROWMODE_SPECIFIED
)
1553 wxFlexGridSizer::~wxFlexGridSizer()
1557 void wxFlexGridSizer::RecalcSizes()
1560 if ( !CalcRowsCols(nrows
, ncols
) )
1563 const wxPoint
pt(GetPosition());
1564 const wxSize
sz(GetSize());
1566 AdjustForGrowables(sz
);
1568 wxSizerItemList::const_iterator i
= m_children
.begin();
1569 const wxSizerItemList::const_iterator end
= m_children
.end();
1572 for ( int r
= 0; r
< nrows
; r
++ )
1574 if ( m_rowHeights
[r
] == -1 )
1576 // this row is entirely hidden, skip it
1577 for ( int c
= 0; c
< ncols
; c
++ )
1588 const int hrow
= m_rowHeights
[r
];
1589 int h
= sz
.y
- y
; // max remaining height, don't overflow it
1594 for ( int c
= 0; c
< ncols
&& i
!= end
; c
++, ++i
)
1596 const int wcol
= m_colWidths
[c
];
1601 int w
= sz
.x
- x
; // max possible value, ensure we don't overflow
1605 SetItemBounds(*i
, pt
.x
+ x
, pt
.y
+ y
, w
, h
);
1617 // helper function used in CalcMin() to sum up the sizes of non-hidden items
1618 static int SumArraySizes(const wxArrayInt
& sizes
, int gap
)
1620 // Sum total minimum size, including gaps between rows/columns.
1621 // -1 is used as a magic number meaning empty row/column.
1624 const size_t count
= sizes
.size();
1625 for ( size_t n
= 0; n
< count
; n
++ )
1627 if ( sizes
[n
] != -1 )
1630 total
+= gap
; // separate from the previous column
1639 void wxFlexGridSizer::FindWidthsAndHeights(int nrows
, int ncols
)
1641 // We have to recalculate the sizes in case the item minimum size has
1642 // changed since the previous layout, or the item has been hidden using
1643 // wxSizer::Show(). If all the items in a row/column are hidden, the final
1644 // dimension of the row/column will be -1, indicating that the column
1645 // itself is hidden.
1646 m_rowHeights
.assign(nrows
, -1);
1647 m_colWidths
.assign(ncols
, -1);
1649 // n is the index of the item in left-to-right top-to-bottom order
1651 for ( wxSizerItemList::iterator i
= m_children
.begin();
1652 i
!= m_children
.end();
1655 wxSizerItem
* const item
= *i
;
1656 if ( item
->IsShown() )
1658 // NOTE: Not doing the calculation here, this is just
1659 // for finding max values.
1660 const wxSize
sz(item
->GetMinSizeWithBorder());
1662 const int row
= n
/ ncols
;
1663 const int col
= n
% ncols
;
1665 if ( sz
.y
> m_rowHeights
[row
] )
1666 m_rowHeights
[row
] = sz
.y
;
1667 if ( sz
.x
> m_colWidths
[col
] )
1668 m_colWidths
[col
] = sz
.x
;
1672 AdjustForFlexDirection();
1674 m_calculatedMinSize
= wxSize(SumArraySizes(m_colWidths
, m_hgap
),
1675 SumArraySizes(m_rowHeights
, m_vgap
));
1678 wxSize
wxFlexGridSizer::CalcMin()
1683 // Number of rows/columns can change as items are added or removed.
1684 if ( !CalcRowsCols(nrows
, ncols
) )
1688 // We have to recalculate the sizes in case the item minimum size has
1689 // changed since the previous layout, or the item has been hidden using
1690 // wxSizer::Show(). If all the items in a row/column are hidden, the final
1691 // dimension of the row/column will be -1, indicating that the column
1692 // itself is hidden.
1693 m_rowHeights
.assign(nrows
, -1);
1694 m_colWidths
.assign(ncols
, -1);
1696 for ( wxSizerItemList::iterator i
= m_children
.begin();
1697 i
!= m_children
.end();
1700 wxSizerItem
* const item
= *i
;
1701 if ( item
->IsShown() )
1707 // The stage of looking for max values in each row/column has been
1708 // made a separate function, since it's reused in AdjustForGrowables.
1709 FindWidthsAndHeights(nrows
,ncols
);
1711 return m_calculatedMinSize
;
1714 void wxFlexGridSizer::AdjustForFlexDirection()
1716 // the logic in CalcMin works when we resize flexibly in both directions
1717 // but maybe this is not the case
1718 if ( m_flexDirection
!= wxBOTH
)
1720 // select the array corresponding to the direction in which we do *not*
1722 wxArrayInt
& array
= m_flexDirection
== wxVERTICAL
? m_colWidths
1725 const size_t count
= array
.GetCount();
1727 // find the largest value in this array
1731 for ( n
= 0; n
< count
; ++n
)
1733 if ( array
[n
] > largest
)
1737 // and now fill it with the largest value
1738 for ( n
= 0; n
< count
; ++n
)
1740 // don't touch hidden rows
1741 if ( array
[n
] != -1 )
1747 // helper of AdjustForGrowables() which is called for rows/columns separately
1750 // delta: the extra space, we do nothing unless it's positive
1751 // growable: indices or growable rows/cols in sizes array
1752 // sizes: the height/widths of rows/cols to adjust
1753 // proportions: proportions of the growable rows/cols or NULL if they all
1754 // should be assumed to have proportion of 1
1756 DoAdjustForGrowables(int delta
,
1757 const wxArrayInt
& growable
,
1759 const wxArrayInt
*proportions
)
1764 // total sum of proportions of all non-hidden rows
1765 int sum_proportions
= 0;
1767 // number of currently shown growable rows
1770 const int max_idx
= sizes
.size();
1772 const size_t count
= growable
.size();
1774 for ( idx
= 0; idx
< count
; idx
++ )
1776 // Since the number of rows/columns can change as items are
1777 // inserted/deleted, we need to verify at runtime that the
1778 // requested growable rows/columns are still valid.
1779 if ( growable
[idx
] >= max_idx
)
1782 // If all items in a row/column are hidden, that row/column will
1783 // have a dimension of -1. This causes the row/column to be
1784 // hidden completely.
1785 if ( sizes
[growable
[idx
]] == -1 )
1789 sum_proportions
+= (*proportions
)[idx
];
1797 // the remaining extra free space, adjusted during each iteration
1798 for ( idx
= 0; idx
< count
; idx
++ )
1800 if ( growable
[idx
] >= max_idx
)
1803 if ( sizes
[ growable
[idx
] ] == -1 )
1807 if ( sum_proportions
== 0 )
1809 // no growable rows -- divide extra space evenly among all
1810 cur_delta
= delta
/num
;
1813 else // allocate extra space proportionally
1815 const int cur_prop
= (*proportions
)[idx
];
1816 cur_delta
= (delta
*cur_prop
)/sum_proportions
;
1817 sum_proportions
-= cur_prop
;
1820 sizes
[growable
[idx
]] += cur_delta
;
1825 void wxFlexGridSizer::AdjustForGrowables(const wxSize
& sz
)
1828 // by the time this function is called, the sizer should be already fully
1829 // initialized and hence the number of its columns and rows is known and we
1830 // can check that all indices in m_growableCols/Rows are valid (see also
1831 // comments in AddGrowableCol/Row())
1832 if ( !m_rows
|| !m_cols
)
1836 int nrows
= CalcRows();
1838 for ( size_t n
= 0; n
< m_growableRows
.size(); n
++ )
1840 wxASSERT_MSG( m_growableRows
[n
] < nrows
,
1841 "invalid growable row index" );
1847 int ncols
= CalcCols();
1849 for ( size_t n
= 0; n
< m_growableCols
.size(); n
++ )
1851 wxASSERT_MSG( m_growableCols
[n
] < ncols
,
1852 "invalid growable column index" );
1856 #endif // wxDEBUG_LEVEL
1859 if ( (m_flexDirection
& wxHORIZONTAL
) || (m_growMode
!= wxFLEX_GROWMODE_NONE
) )
1861 DoAdjustForGrowables
1863 sz
.x
- m_calculatedMinSize
.x
,
1866 m_growMode
== wxFLEX_GROWMODE_SPECIFIED
? &m_growableColsProportions
1870 // This gives nested objects that benefit from knowing one size
1871 // component in advance the chance to use that.
1872 bool didAdjustMinSize
= false;
1874 // Iterate over all items and inform about column width
1875 const int ncols
= GetEffectiveColsCount();
1877 for ( wxSizerItemList::iterator i
= m_children
.begin();
1878 i
!= m_children
.end();
1881 didAdjustMinSize
|= (*i
)->InformFirstDirection(wxHORIZONTAL
, m_colWidths
[col
], sz
.y
- m_calculatedMinSize
.y
);
1882 if ( ++col
== ncols
)
1886 // Only redo if info was actually used
1887 if( didAdjustMinSize
)
1889 DoAdjustForGrowables
1891 sz
.x
- m_calculatedMinSize
.x
,
1894 m_growMode
== wxFLEX_GROWMODE_SPECIFIED
? &m_growableColsProportions
1900 if ( (m_flexDirection
& wxVERTICAL
) || (m_growMode
!= wxFLEX_GROWMODE_NONE
) )
1902 // pass NULL instead of proportions if the grow mode is ALL as we
1903 // should treat all rows as having proportion of 1 then
1904 DoAdjustForGrowables
1906 sz
.y
- m_calculatedMinSize
.y
,
1909 m_growMode
== wxFLEX_GROWMODE_SPECIFIED
? &m_growableRowsProportions
1915 bool wxFlexGridSizer::IsRowGrowable( size_t idx
)
1917 return m_growableRows
.Index( idx
) != wxNOT_FOUND
;
1920 bool wxFlexGridSizer::IsColGrowable( size_t idx
)
1922 return m_growableCols
.Index( idx
) != wxNOT_FOUND
;
1925 void wxFlexGridSizer::AddGrowableRow( size_t idx
, int proportion
)
1927 wxASSERT_MSG( !IsRowGrowable( idx
),
1928 "AddGrowableRow() called for growable row" );
1930 // notice that we intentionally don't check the index validity here in (the
1931 // common) case when the number of rows was not specified in the ctor -- in
1932 // this case it will be computed only later, when all items are added to
1933 // the sizer, and the check will be done in AdjustForGrowables()
1934 wxCHECK_RET( !m_rows
|| idx
< (size_t)m_rows
, "invalid row index" );
1936 m_growableRows
.Add( idx
);
1937 m_growableRowsProportions
.Add( proportion
);
1940 void wxFlexGridSizer::AddGrowableCol( size_t idx
, int proportion
)
1942 wxASSERT_MSG( !IsColGrowable( idx
),
1943 "AddGrowableCol() called for growable column" );
1945 // see comment in AddGrowableRow(): although it's less common to omit the
1946 // specification of the number of columns, it still can also happen
1947 wxCHECK_RET( !m_cols
|| idx
< (size_t)m_cols
, "invalid column index" );
1949 m_growableCols
.Add( idx
);
1950 m_growableColsProportions
.Add( proportion
);
1953 // helper function for RemoveGrowableCol/Row()
1955 DoRemoveFromArrays(size_t idx
, wxArrayInt
& items
, wxArrayInt
& proportions
)
1957 const size_t count
= items
.size();
1958 for ( size_t n
= 0; n
< count
; n
++ )
1960 if ( (size_t)items
[n
] == idx
)
1963 proportions
.RemoveAt(n
);
1968 wxFAIL_MSG( wxT("column/row is already not growable") );
1971 void wxFlexGridSizer::RemoveGrowableCol( size_t idx
)
1973 DoRemoveFromArrays(idx
, m_growableCols
, m_growableColsProportions
);
1976 void wxFlexGridSizer::RemoveGrowableRow( size_t idx
)
1978 DoRemoveFromArrays(idx
, m_growableRows
, m_growableRowsProportions
);
1981 //---------------------------------------------------------------------------
1983 //---------------------------------------------------------------------------
1985 wxSizerItem
*wxBoxSizer::AddSpacer(int size
)
1987 return IsVertical() ? Add(0, size
) : Add(size
, 0);
1990 void wxBoxSizer::RecalcSizes()
1992 if ( m_children
.empty() )
1995 const wxCoord totalMinorSize
= GetSizeInMinorDir(m_size
);
1997 // the amount of free space which we should redistribute among the
1998 // stretchable items (i.e. those with non zero proportion)
1999 int delta
= GetSizeInMajorDir(m_size
) - GetSizeInMajorDir(m_minSize
);
2002 // Inform child items about the size in minor direction, that can
2003 // change how much free space we have in major dir and how to distribute it.
2004 int majorMinSum
= 0;
2005 wxSizerItemList::const_iterator i
;
2006 for ( i
= m_children
.begin();
2007 i
!= m_children
.end();
2010 wxSizerItem
* const item
= *i
;
2012 if ( !item
->IsShown() )
2015 wxSize szMinPrev
= item
->GetMinSizeWithBorder();
2016 item
->InformFirstDirection(m_orient
^wxBOTH
,totalMinorSize
,delta
);
2017 wxSize szMin
= item
->GetMinSizeWithBorder();
2018 int deltaChange
= GetSizeInMajorDir(szMin
-szMinPrev
);
2021 // Since we passed available space along to the item, it should not
2022 // take too much, so delta should not become negative.
2023 delta
-= deltaChange
;
2025 majorMinSum
+= GetSizeInMajorDir(item
->GetMinSizeWithBorder());
2027 // And update our min size
2028 SizeInMajorDir(m_minSize
) = majorMinSum
;
2031 // might have a new delta now
2032 delta
= GetSizeInMajorDir(m_size
) - GetSizeInMajorDir(m_minSize
);
2034 // the position at which we put the next child
2035 wxPoint
pt(m_position
);
2037 int totalProportion
= m_totalProportion
;
2038 for ( i
= m_children
.begin();
2039 i
!= m_children
.end();
2042 wxSizerItem
* const item
= *i
;
2044 if ( !item
->IsShown() )
2047 const wxSize
sizeThis(item
->GetMinSizeWithBorder());
2049 // adjust the size in the major direction using the proportion
2050 wxCoord majorSize
= GetSizeInMajorDir(sizeThis
);
2052 // if there is not enough space, don't try to distribute negative space
2053 // among the children, this would result in overlapping windows which
2057 const int propItem
= item
->GetProportion();
2060 const int deltaItem
= (delta
* propItem
) / totalProportion
;
2062 majorSize
+= deltaItem
;
2065 totalProportion
-= propItem
;
2070 // apply the alignment in the minor direction
2071 wxPoint
posChild(pt
);
2073 wxCoord minorSize
= GetSizeInMinorDir(sizeThis
);
2074 const int flag
= item
->GetFlag();
2075 if ( flag
& (wxEXPAND
| wxSHAPED
) )
2077 minorSize
= totalMinorSize
;
2079 else if ( flag
& (IsVertical() ? wxALIGN_RIGHT
: wxALIGN_BOTTOM
) )
2081 PosInMinorDir(posChild
) += totalMinorSize
- minorSize
;
2083 // NB: wxCENTRE is used here only for backwards compatibility,
2084 // wxALIGN_CENTRE should be used in new code
2085 else if ( flag
& (wxCENTER
| (IsVertical() ? wxALIGN_CENTRE_HORIZONTAL
: wxALIGN_CENTRE_VERTICAL
)))
2087 PosInMinorDir(posChild
) += (totalMinorSize
- minorSize
) / 2;
2091 // apply RTL adjustment for horizontal sizers:
2092 if ( !IsVertical() && m_containingWindow
)
2094 posChild
.x
= m_containingWindow
->AdjustForLayoutDirection
2102 // finally set size of this child and advance to the next one
2103 item
->SetDimension(posChild
, SizeFromMajorMinor(majorSize
, minorSize
));
2105 PosInMajorDir(pt
) += majorSize
;
2109 wxSize
wxBoxSizer::CalcMin()
2111 m_totalProportion
= 0;
2112 m_minSize
= wxSize(0, 0);
2114 // calculate the minimal sizes for all items and count sum of proportions
2115 for ( wxSizerItemList::const_iterator i
= m_children
.begin();
2116 i
!= m_children
.end();
2119 wxSizerItem
* const item
= *i
;
2121 if ( !item
->IsShown() )
2124 const wxSize sizeMinThis
= item
->CalcMin();
2125 SizeInMajorDir(m_minSize
) += GetSizeInMajorDir(sizeMinThis
);
2126 if ( GetSizeInMinorDir(sizeMinThis
) > GetSizeInMinorDir(m_minSize
) )
2127 SizeInMinorDir(m_minSize
) = GetSizeInMinorDir(sizeMinThis
);
2129 m_totalProportion
+= item
->GetProportion();
2135 //---------------------------------------------------------------------------
2137 //---------------------------------------------------------------------------
2141 wxStaticBoxSizer::wxStaticBoxSizer( wxStaticBox
*box
, int orient
)
2142 : wxBoxSizer( orient
),
2145 wxASSERT_MSG( box
, wxT("wxStaticBoxSizer needs a static box") );
2147 // do this so that our Detach() is called if the static box is destroyed
2149 m_staticBox
->SetContainingSizer(this);
2152 wxStaticBoxSizer::wxStaticBoxSizer(int orient
, wxWindow
*win
, const wxString
& s
)
2153 : wxBoxSizer(orient
),
2154 m_staticBox(new wxStaticBox(win
, wxID_ANY
, s
))
2157 m_staticBox
->SetContainingSizer(this);
2160 wxStaticBoxSizer::~wxStaticBoxSizer()
2165 void wxStaticBoxSizer::RecalcSizes()
2167 int top_border
, other_border
;
2168 m_staticBox
->GetBordersForSizer(&top_border
, &other_border
);
2170 m_staticBox
->SetSize( m_position
.x
, m_position
.y
, m_size
.x
, m_size
.y
);
2172 wxSize
old_size( m_size
);
2173 m_size
.x
-= 2*other_border
;
2174 m_size
.y
-= top_border
+ other_border
;
2176 wxPoint
old_pos( m_position
);
2177 if (m_staticBox
->GetChildren().GetCount() > 0)
2179 #if defined( __WXGTK20__ )
2180 // if the wxStaticBox has created a wxPizza to contain its children
2181 // (see wxStaticBox::AddChild) then we need to place the items it contains
2182 // in the wxBoxSizer::RecalcSizes() call below using coordinates relative
2183 // to the top-left corner of the staticbox:
2184 m_position
.x
= m_position
.y
= 0;
2186 // if the wxStaticBox has childrens, then these windows must be placed
2187 // by the wxBoxSizer::RecalcSizes() call below using coordinates relative
2188 // to the top-left corner of the staticbox (but unlike wxGTK, we need
2189 // to keep in count the static borders here!):
2190 m_position
.x
= other_border
;
2191 m_position
.y
= top_border
;
2196 // the windows contained in the staticbox have been created as siblings of the
2197 // staticbox (this is the "old" way of staticbox contents creation); in this
2198 // case we need to position them with coordinates relative to our common parent
2199 m_position
.x
+= other_border
;
2200 m_position
.y
+= top_border
;
2203 wxBoxSizer::RecalcSizes();
2205 m_position
= old_pos
;
2209 wxSize
wxStaticBoxSizer::CalcMin()
2211 int top_border
, other_border
;
2212 m_staticBox
->GetBordersForSizer(&top_border
, &other_border
);
2214 wxSize
ret( wxBoxSizer::CalcMin() );
2215 ret
.x
+= 2*other_border
;
2217 // ensure that we're wide enough to show the static box label (there is no
2218 // need to check for the static box best size in vertical direction though)
2219 const int boxWidth
= m_staticBox
->GetBestSize().x
;
2220 if ( ret
.x
< boxWidth
)
2223 ret
.y
+= other_border
+ top_border
;
2228 void wxStaticBoxSizer::ShowItems( bool show
)
2230 m_staticBox
->Show( show
);
2231 wxBoxSizer::ShowItems( show
);
2234 bool wxStaticBoxSizer::Detach( wxWindow
*window
)
2236 // avoid deleting m_staticBox in our dtor if it's being detached from the
2237 // sizer (which can happen because it's being already destroyed for
2239 if ( window
== m_staticBox
)
2245 return wxSizer::Detach( window
);
2248 #endif // wxUSE_STATBOX
2250 //---------------------------------------------------------------------------
2251 // wxStdDialogButtonSizer
2252 //---------------------------------------------------------------------------
2256 wxStdDialogButtonSizer::wxStdDialogButtonSizer()
2257 : wxBoxSizer(wxHORIZONTAL
)
2259 // Vertical buttons with lots of space on either side
2260 // looks rubbish on WinCE, so let's not do this for now.
2261 // If we are going to use vertical buttons, we should
2262 // put the sizer to the right of other controls in the dialog,
2263 // and that's beyond the scope of this sizer.
2265 bool is_pda
= (wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA
);
2266 // If we have a PDA screen, put yes/no button over
2267 // all other buttons, otherwise on the left side.
2269 m_orient
= wxVERTICAL
;
2272 m_buttonAffirmative
= NULL
;
2273 m_buttonApply
= NULL
;
2274 m_buttonNegative
= NULL
;
2275 m_buttonCancel
= NULL
;
2276 m_buttonHelp
= NULL
;
2279 void wxStdDialogButtonSizer::AddButton(wxButton
*mybutton
)
2281 switch (mybutton
->GetId())
2286 m_buttonAffirmative
= mybutton
;
2289 m_buttonApply
= mybutton
;
2292 m_buttonNegative
= mybutton
;
2296 m_buttonCancel
= mybutton
;
2299 case wxID_CONTEXT_HELP
:
2300 m_buttonHelp
= mybutton
;
2307 void wxStdDialogButtonSizer::SetAffirmativeButton( wxButton
*button
)
2309 m_buttonAffirmative
= button
;
2312 void wxStdDialogButtonSizer::SetNegativeButton( wxButton
*button
)
2314 m_buttonNegative
= button
;
2317 void wxStdDialogButtonSizer::SetCancelButton( wxButton
*button
)
2319 m_buttonCancel
= button
;
2322 void wxStdDialogButtonSizer::Realize()
2325 Add(0, 0, 0, wxLEFT
, 6);
2327 Add((wxWindow
*)m_buttonHelp
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, 6);
2329 if (m_buttonNegative
){
2330 // HIG POLICE BULLETIN - destructive buttons need extra padding
2331 // 24 pixels on either side
2332 Add((wxWindow
*)m_buttonNegative
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, 12);
2335 // extra whitespace between help/negative and cancel/ok buttons
2336 Add(0, 0, 1, wxEXPAND
, 0);
2338 if (m_buttonCancel
){
2339 Add((wxWindow
*)m_buttonCancel
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, 6);
2340 // Cancel or help should be default
2341 // m_buttonCancel->SetDefaultButton();
2344 // Ugh, Mac doesn't really have apply dialogs, so I'll just
2345 // figure the best place is between Cancel and OK
2347 Add((wxWindow
*)m_buttonApply
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, 6);
2349 if (m_buttonAffirmative
){
2350 Add((wxWindow
*)m_buttonAffirmative
, 0, wxALIGN_CENTRE
| wxLEFT
, 6);
2352 if (m_buttonAffirmative
->GetId() == wxID_SAVE
){
2353 // these buttons have set labels under Mac so we should use them
2354 m_buttonAffirmative
->SetLabel(_("Save"));
2355 if (m_buttonNegative
)
2356 m_buttonNegative
->SetLabel(_("Don't Save"));
2360 // Extra space around and at the right
2362 #elif defined(__WXGTK20__)
2363 Add(0, 0, 0, wxLEFT
, 9);
2365 Add((wxWindow
*)m_buttonHelp
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, 3);
2367 // extra whitespace between help and cancel/ok buttons
2368 Add(0, 0, 1, wxEXPAND
, 0);
2370 if (m_buttonNegative
){
2371 Add((wxWindow
*)m_buttonNegative
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, 3);
2374 // according to HIG, in explicit apply windows the order is:
2375 // [ Help Apply Cancel OK ]
2377 Add((wxWindow
*)m_buttonApply
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, 3);
2379 if (m_buttonCancel
){
2380 Add((wxWindow
*)m_buttonCancel
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, 3);
2381 // Cancel or help should be default
2382 // m_buttonCancel->SetDefaultButton();
2385 if (m_buttonAffirmative
)
2386 Add((wxWindow
*)m_buttonAffirmative
, 0, wxALIGN_CENTRE
| wxLEFT
, 6);
2387 #elif defined(__WXMSW__)
2390 // right-justify buttons
2391 Add(0, 0, 1, wxEXPAND
, 0);
2393 if (m_buttonAffirmative
){
2394 Add((wxWindow
*)m_buttonAffirmative
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, m_buttonAffirmative
->ConvertDialogToPixels(wxSize(2, 0)).x
);
2397 if (m_buttonNegative
){
2398 Add((wxWindow
*)m_buttonNegative
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, m_buttonNegative
->ConvertDialogToPixels(wxSize(2, 0)).x
);
2401 if (m_buttonCancel
){
2402 Add((wxWindow
*)m_buttonCancel
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, m_buttonCancel
->ConvertDialogToPixels(wxSize(2, 0)).x
);
2405 Add((wxWindow
*)m_buttonApply
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, m_buttonApply
->ConvertDialogToPixels(wxSize(2, 0)).x
);
2408 Add((wxWindow
*)m_buttonHelp
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, m_buttonHelp
->ConvertDialogToPixels(wxSize(2, 0)).x
);
2410 // GTK+1 and any other platform
2412 // Add(0, 0, 0, wxLEFT, 5); // Not sure what this was for but it unbalances the dialog
2414 Add((wxWindow
*)m_buttonHelp
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, m_buttonHelp
->ConvertDialogToPixels(wxSize(4, 0)).x
);
2416 // extra whitespace between help and cancel/ok buttons
2417 Add(0, 0, 1, wxEXPAND
, 0);
2420 Add((wxWindow
*)m_buttonApply
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, m_buttonApply
->ConvertDialogToPixels(wxSize(4, 0)).x
);
2422 if (m_buttonAffirmative
){
2423 Add((wxWindow
*)m_buttonAffirmative
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, m_buttonAffirmative
->ConvertDialogToPixels(wxSize(4, 0)).x
);
2426 if (m_buttonNegative
){
2427 Add((wxWindow
*)m_buttonNegative
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, m_buttonNegative
->ConvertDialogToPixels(wxSize(4, 0)).x
);
2430 if (m_buttonCancel
){
2431 Add((wxWindow
*)m_buttonCancel
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, m_buttonCancel
->ConvertDialogToPixels(wxSize(4, 0)).x
);
2432 // Cancel or help should be default
2433 // m_buttonCancel->SetDefaultButton();
2439 #endif // wxUSE_BUTTON