1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/sizer.cpp
3 // Purpose: provide new wxSizer class for layout
4 // Author: Robert Roebling and Robin Dunn, contributions by
5 // Dirk Holtwick, Ron Lee
6 // Modified by: Ron Lee
9 // Copyright: (c) Robin Dunn, Robert Roebling
10 // Licence: wxWindows licence
11 /////////////////////////////////////////////////////////////////////////////
13 // For compilers that support precompilation, includes "wx.h".
14 #include "wx/wxprec.h"
20 #include "wx/display.h"
24 #include "wx/string.h"
28 #include "wx/settings.h"
29 #include "wx/button.h"
30 #include "wx/statbox.h"
31 #include "wx/toplevel.h"
34 #include "wx/listimpl.cpp"
37 //---------------------------------------------------------------------------
39 IMPLEMENT_CLASS(wxSizerItem
, wxObject
)
40 IMPLEMENT_CLASS(wxSizer
, wxObject
)
41 IMPLEMENT_CLASS(wxGridSizer
, wxSizer
)
42 IMPLEMENT_CLASS(wxFlexGridSizer
, wxGridSizer
)
43 IMPLEMENT_CLASS(wxBoxSizer
, wxSizer
)
45 IMPLEMENT_CLASS(wxStaticBoxSizer
, wxBoxSizer
)
48 IMPLEMENT_CLASS(wxStdDialogButtonSizer
, wxBoxSizer
)
51 WX_DEFINE_EXPORTED_LIST( wxSizerItemList
)
86 // ----------------------------------------------------------------------------
88 // ----------------------------------------------------------------------------
90 void wxSizerItem::Init(const wxSizerFlags
& flags
)
94 m_proportion
= flags
.GetProportion();
95 m_flag
= flags
.GetFlags();
96 m_border
= flags
.GetBorderInPixels();
99 wxSizerItem::wxSizerItem()
110 void wxSizerItem::DoSetWindow(wxWindow
*window
)
112 wxCHECK_RET( window
, _T("NULL window in wxSizerItem::SetWindow()") );
114 m_kind
= Item_Window
;
117 // window doesn't become smaller than its initial size, whatever happens
118 m_minSize
= window
->GetSize();
120 if ( m_flag
& wxFIXED_MINSIZE
)
121 window
->SetMinSize(m_minSize
);
123 // aspect ratio calculated from initial size
127 wxSizerItem::wxSizerItem(wxWindow
*window
,
133 m_proportion(proportion
),
143 void wxSizerItem::DoSetSizer(wxSizer
*sizer
)
149 wxSizerItem::wxSizerItem(wxSizer
*sizer
,
156 m_proportion(proportion
),
165 // m_minSize is set later
169 void wxSizerItem::DoSetSpacer(const wxSize
& size
)
171 m_kind
= Item_Spacer
;
172 m_spacer
= new wxSizerSpacer(size
);
177 wxSizerItem::wxSizerItem(int width
,
185 m_minSize(width
, height
), // minimal size is the initial size
186 m_proportion(proportion
),
192 DoSetSpacer(wxSize(width
, height
));
195 wxSizerItem::~wxSizerItem()
201 void wxSizerItem::Free()
209 m_window
->SetContainingSizer(NULL
);
222 wxFAIL_MSG( _T("unexpected wxSizerItem::m_kind") );
228 wxSize
wxSizerItem::GetSpacer() const
231 if ( m_kind
== Item_Spacer
)
232 size
= m_spacer
->GetSize();
238 wxSize
wxSizerItem::GetSize() const
247 ret
= m_window
->GetSize();
251 ret
= m_sizer
->GetSize();
255 ret
= m_spacer
->GetSize();
260 wxFAIL_MSG( _T("unexpected wxSizerItem::m_kind") );
267 if (m_flag
& wxNORTH
)
269 if (m_flag
& wxSOUTH
)
275 bool wxSizerItem::InformFirstDirection(int direction
, int size
, int availableOtherDir
)
277 // The size that come here will be including borders. Child items should get it
281 if( direction
==wxHORIZONTAL
)
288 else if( direction
==wxVERTICAL
)
290 if (m_flag
& wxNORTH
)
292 if (m_flag
& wxSOUTH
)
298 // Pass the information along to the held object
301 didUse
= GetSizer()->InformFirstDirection(direction
,size
,availableOtherDir
);
303 m_minSize
= GetSizer()->CalcMin();
307 didUse
= GetWindow()->InformFirstDirection(direction
,size
,availableOtherDir
);
309 m_minSize
= m_window
->GetEffectiveMinSize();
311 // This information is useful for items with wxSHAPED flag, since
312 // we can request an optimal min size for such an item. Even if
313 // we overwrite the m_minSize member here, we can read it back from
314 // the owned window (happens automatically).
315 if( (m_flag
& wxSHAPED
) && (m_flag
& wxEXPAND
) && direction
)
317 if( !wxIsNullDouble(m_ratio
) )
319 wxCHECK_MSG( (m_proportion
==0), false, _T("Shaped item, non-zero proportion in wxSizerItem::InformFirstDirection()") );
320 if( direction
==wxHORIZONTAL
&& !wxIsNullDouble(m_ratio
) )
322 // Clip size so that we don't take too much
323 if( availableOtherDir
>=0 && int(size
/m_ratio
)-m_minSize
.y
>availableOtherDir
)
324 size
= int((availableOtherDir
+m_minSize
.y
)*m_ratio
);
325 m_minSize
= wxSize(size
,int(size
/m_ratio
));
327 else if( direction
==wxVERTICAL
)
329 // Clip size so that we don't take too much
330 if( availableOtherDir
>=0 && int(size
*m_ratio
)-m_minSize
.x
>availableOtherDir
)
331 size
= int((availableOtherDir
+m_minSize
.x
)/m_ratio
);
332 m_minSize
= wxSize(int(size
*m_ratio
),size
);
342 wxSize
wxSizerItem::CalcMin()
346 m_minSize
= m_sizer
->GetMinSize();
348 // if we have to preserve aspect ratio _AND_ this is
349 // the first-time calculation, consider ret to be initial size
350 if ( (m_flag
& wxSHAPED
) && wxIsNullDouble(m_ratio
) )
353 else if ( IsWindow() )
355 // Since the size of the window may change during runtime, we
356 // should use the current minimal/best size.
357 m_minSize
= m_window
->GetEffectiveMinSize();
360 return GetMinSizeWithBorder();
363 wxSize
wxSizerItem::GetMinSizeWithBorder() const
365 wxSize ret
= m_minSize
;
371 if (m_flag
& wxNORTH
)
373 if (m_flag
& wxSOUTH
)
380 void wxSizerItem::SetDimension( const wxPoint
& pos_
, const wxSize
& size_
)
384 if (m_flag
& wxSHAPED
)
386 // adjust aspect ratio
387 int rwidth
= (int) (size
.y
* m_ratio
);
391 int rheight
= (int) (size
.x
/ m_ratio
);
392 // add vertical space
393 if (m_flag
& wxALIGN_CENTER_VERTICAL
)
394 pos
.y
+= (size
.y
- rheight
) / 2;
395 else if (m_flag
& wxALIGN_BOTTOM
)
396 pos
.y
+= (size
.y
- rheight
);
397 // use reduced dimensions
400 else if (rwidth
< size
.x
)
402 // add horizontal space
403 if (m_flag
& wxALIGN_CENTER_HORIZONTAL
)
404 pos
.x
+= (size
.x
- rwidth
) / 2;
405 else if (m_flag
& wxALIGN_RIGHT
)
406 pos
.x
+= (size
.x
- rwidth
);
411 // This is what GetPosition() returns. Since we calculate
412 // borders afterwards, GetPosition() will be the left/top
413 // corner of the surrounding border.
425 if (m_flag
& wxNORTH
)
430 if (m_flag
& wxSOUTH
)
440 m_rect
= wxRect(pos
, size
);
445 wxFAIL_MSG( _T("can't set size of uninitialized sizer item") );
449 m_window
->SetSize(pos
.x
, pos
.y
, size
.x
, size
.y
,
450 wxSIZE_ALLOW_MINUS_ONE
);
454 m_sizer
->SetDimension(pos
.x
, pos
.y
, size
.x
, size
.y
);
458 m_spacer
->SetSize(size
);
463 wxFAIL_MSG( _T("unexpected wxSizerItem::m_kind") );
467 void wxSizerItem::DeleteWindows()
476 //We are deleting the window from this sizer - normally
477 //the window destroys the sizer associated with it,
478 //which might destroy this, which we don't want
479 m_window
->SetContainingSizer(NULL
);
481 //Putting this after the switch will result in a spacer
482 //not being deleted properly on destruction
487 m_sizer
->DeleteWindows();
492 wxFAIL_MSG( _T("unexpected wxSizerItem::m_kind") );
497 void wxSizerItem::Show( bool show
)
502 wxFAIL_MSG( _T("can't show uninitialized sizer item") );
506 m_window
->Show(show
);
514 m_spacer
->Show(show
);
519 wxFAIL_MSG( _T("unexpected wxSizerItem::m_kind") );
523 bool wxSizerItem::IsShown() const
528 // we may be called from CalcMin(), just return false so that we're
533 return m_window
->IsShown();
536 // arbitrarily decide that if at least one of our elements is
537 // shown, so are we (this arbitrariness is the reason for
538 // deprecating this function)
540 // Some apps (such as dialog editors) depend on an empty sizer still
541 // being laid out correctly and reporting the correct size and position.
542 if (m_sizer
->GetChildren().GetCount() == 0)
545 for ( wxSizerItemList::compatibility_iterator
546 node
= m_sizer
->GetChildren().GetFirst();
548 node
= node
->GetNext() )
550 if ( node
->GetData()->IsShown() )
557 return m_spacer
->IsShown();
561 wxFAIL_MSG( _T("unexpected wxSizerItem::m_kind") );
567 #if WXWIN_COMPATIBILITY_2_6
568 void wxSizerItem::SetOption( int option
)
570 SetProportion( option
);
573 int wxSizerItem::GetOption() const
575 return GetProportion();
577 #endif // WXWIN_COMPATIBILITY_2_6
580 //---------------------------------------------------------------------------
582 //---------------------------------------------------------------------------
586 WX_CLEAR_LIST(wxSizerItemList
, m_children
);
589 wxSizerItem
* wxSizer::Insert( size_t index
, wxSizerItem
*item
)
591 m_children
.Insert( index
, item
);
593 if ( item
->GetWindow() )
594 item
->GetWindow()->SetContainingSizer( this );
596 if ( item
->GetSizer() )
597 item
->GetSizer()->SetContainingWindow( m_containingWindow
);
602 void wxSizer::SetContainingWindow(wxWindow
*win
)
604 if ( win
== m_containingWindow
)
607 m_containingWindow
= win
;
609 // set the same window for all nested sizers as well, they also are in the
611 for ( wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
613 node
= node
->GetNext() )
615 wxSizerItem
*const item
= node
->GetData();
616 wxSizer
*const sizer
= item
->GetSizer();
620 sizer
->SetContainingWindow(win
);
625 #if WXWIN_COMPATIBILITY_2_6
626 bool wxSizer::Remove( wxWindow
*window
)
628 return Detach( window
);
630 #endif // WXWIN_COMPATIBILITY_2_6
632 bool wxSizer::Remove( wxSizer
*sizer
)
634 wxASSERT_MSG( sizer
, _T("Removing NULL sizer") );
636 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
639 wxSizerItem
*item
= node
->GetData();
641 if (item
->GetSizer() == sizer
)
644 m_children
.Erase( node
);
648 node
= node
->GetNext();
654 bool wxSizer::Remove( int index
)
656 wxCHECK_MSG( index
>= 0 && (size_t)index
< m_children
.GetCount(),
658 _T("Remove index is out of range") );
660 wxSizerItemList::compatibility_iterator node
= m_children
.Item( index
);
662 wxCHECK_MSG( node
, false, _T("Failed to find child node") );
664 delete node
->GetData();
665 m_children
.Erase( node
);
670 bool wxSizer::Detach( wxSizer
*sizer
)
672 wxASSERT_MSG( sizer
, _T("Detaching NULL sizer") );
674 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
677 wxSizerItem
*item
= node
->GetData();
679 if (item
->GetSizer() == sizer
)
683 m_children
.Erase( node
);
686 node
= node
->GetNext();
692 bool wxSizer::Detach( wxWindow
*window
)
694 wxASSERT_MSG( window
, _T("Detaching NULL window") );
696 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
699 wxSizerItem
*item
= node
->GetData();
701 if (item
->GetWindow() == window
)
704 m_children
.Erase( node
);
707 node
= node
->GetNext();
713 bool wxSizer::Detach( int index
)
715 wxCHECK_MSG( index
>= 0 && (size_t)index
< m_children
.GetCount(),
717 _T("Detach index is out of range") );
719 wxSizerItemList::compatibility_iterator node
= m_children
.Item( index
);
721 wxCHECK_MSG( node
, false, _T("Failed to find child node") );
723 wxSizerItem
*item
= node
->GetData();
725 if ( item
->IsSizer() )
729 m_children
.Erase( node
);
733 bool wxSizer::Replace( wxWindow
*oldwin
, wxWindow
*newwin
, bool recursive
)
735 wxASSERT_MSG( oldwin
, _T("Replacing NULL window") );
736 wxASSERT_MSG( newwin
, _T("Replacing with NULL window") );
738 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
741 wxSizerItem
*item
= node
->GetData();
743 if (item
->GetWindow() == oldwin
)
745 item
->AssignWindow(newwin
);
746 newwin
->SetContainingSizer( this );
749 else if (recursive
&& item
->IsSizer())
751 if (item
->GetSizer()->Replace( oldwin
, newwin
, true ))
755 node
= node
->GetNext();
761 bool wxSizer::Replace( wxSizer
*oldsz
, wxSizer
*newsz
, bool recursive
)
763 wxASSERT_MSG( oldsz
, _T("Replacing NULL sizer") );
764 wxASSERT_MSG( newsz
, _T("Replacing with NULL sizer") );
766 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
769 wxSizerItem
*item
= node
->GetData();
771 if (item
->GetSizer() == oldsz
)
773 item
->AssignSizer(newsz
);
776 else if (recursive
&& item
->IsSizer())
778 if (item
->GetSizer()->Replace( oldsz
, newsz
, true ))
782 node
= node
->GetNext();
788 bool wxSizer::Replace( size_t old
, wxSizerItem
*newitem
)
790 wxCHECK_MSG( old
< m_children
.GetCount(), false, _T("Replace index is out of range") );
791 wxASSERT_MSG( newitem
, _T("Replacing with NULL item") );
793 wxSizerItemList::compatibility_iterator node
= m_children
.Item( old
);
795 wxCHECK_MSG( node
, false, _T("Failed to find child node") );
797 wxSizerItem
*item
= node
->GetData();
798 node
->SetData(newitem
);
804 void wxSizer::Clear( bool delete_windows
)
806 // First clear the ContainingSizer pointers
807 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
810 wxSizerItem
*item
= node
->GetData();
812 if (item
->IsWindow())
813 item
->GetWindow()->SetContainingSizer( NULL
);
814 node
= node
->GetNext();
817 // Destroy the windows if needed
821 // Now empty the list
822 WX_CLEAR_LIST(wxSizerItemList
, m_children
);
825 void wxSizer::DeleteWindows()
827 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
830 wxSizerItem
*item
= node
->GetData();
832 item
->DeleteWindows();
833 node
= node
->GetNext();
837 wxSize
wxSizer::Fit( wxWindow
*window
)
839 // take the min size by default and limit it by max size
840 wxSize size
= GetMinClientSize(window
);
843 wxTopLevelWindow
*tlw
= wxDynamicCast(window
, wxTopLevelWindow
);
846 // hack for small screen devices where TLWs are always full screen
847 if ( tlw
->IsAlwaysMaximized() )
850 return tlw
->GetSize();
853 // limit the window to the size of the display it is on
854 int disp
= wxDisplay::GetFromWindow(window
);
855 if ( disp
== wxNOT_FOUND
)
857 // or, if we don't know which one it is, of the main one
861 sizeMax
= wxDisplay(disp
).GetClientArea().GetSize();
863 // space for decorations and toolbars etc.
864 wxSize tlw_client_size
= tlw
->GetClientSize();
865 wxSize tlw_size
= tlw
->GetSize();
866 sizeMax
.x
-= tlw_size
.x
- tlw_client_size
.x
;
867 sizeMax
.y
-= tlw_size
.y
- tlw_client_size
.y
;
871 sizeMax
= GetMaxClientSize(window
);
874 if ( sizeMax
.x
!= wxDefaultCoord
&& size
.x
> sizeMax
.x
)
876 if ( sizeMax
.y
!= wxDefaultCoord
&& size
.y
> sizeMax
.y
)
880 window
->SetClientSize( size
);
882 // return entire size
883 return window
->GetSize();
886 void wxSizer::FitInside( wxWindow
*window
)
889 if (window
->IsTopLevel())
890 size
= VirtualFitSize( window
);
892 size
= GetMinClientSize( window
);
894 window
->SetVirtualSize( size
);
897 void wxSizer::Layout()
899 // (re)calculates minimums needed for each item and other preparations
903 // Applies the layout and repositions/resizes the items
907 void wxSizer::SetSizeHints( wxWindow
*window
)
909 // Preserve the window's max size hints, but set the
910 // lower bound according to the sizer calculations.
912 wxSize size
= Fit( window
);
914 window
->SetSizeHints( size
.x
,
916 window
->GetMaxWidth(),
917 window
->GetMaxHeight() );
920 #if WXWIN_COMPATIBILITY_2_8
921 void wxSizer::SetVirtualSizeHints( wxWindow
*window
)
925 #endif // WXWIN_COMPATIBILITY_2_8
927 wxSize
wxSizer::GetMaxWindowSize( wxWindow
*window
) const
929 return window
->GetMaxSize();
932 wxSize
wxSizer::GetMinWindowSize( wxWindow
*window
)
934 wxSize
minSize( GetMinSize() );
935 wxSize
size( window
->GetSize() );
936 wxSize
client_size( window
->GetClientSize() );
938 return wxSize( minSize
.x
+size
.x
-client_size
.x
,
939 minSize
.y
+size
.y
-client_size
.y
);
942 // TODO on mac we need a function that determines how much free space this
943 // min size contains, in order to make sure that we have 20 pixels of free
944 // space around the controls
945 wxSize
wxSizer::GetMaxClientSize( wxWindow
*window
) const
947 wxSize
maxSize( window
->GetMaxSize() );
949 if ( maxSize
!= wxDefaultSize
)
951 wxSize
size( window
->GetSize() );
952 wxSize
client_size( window
->GetClientSize() );
954 return wxSize( maxSize
.x
+ client_size
.x
- size
.x
,
955 maxSize
.y
+ client_size
.y
- size
.y
);
958 return wxDefaultSize
;
961 wxSize
wxSizer::GetMinClientSize( wxWindow
*WXUNUSED(window
) )
963 return GetMinSize(); // Already returns client size.
966 wxSize
wxSizer::VirtualFitSize( wxWindow
*window
)
968 wxSize size
= GetMinClientSize( window
);
969 wxSize sizeMax
= GetMaxClientSize( window
);
971 // Limit the size if sizeMax != wxDefaultSize
973 if ( size
.x
> sizeMax
.x
&& sizeMax
.x
!= wxDefaultCoord
)
975 if ( size
.y
> sizeMax
.y
&& sizeMax
.y
!= wxDefaultCoord
)
981 void wxSizer::SetDimension( int x
, int y
, int width
, int height
)
990 wxSize
wxSizer::GetMinSize()
992 wxSize
ret( CalcMin() );
993 if (ret
.x
< m_minSize
.x
) ret
.x
= m_minSize
.x
;
994 if (ret
.y
< m_minSize
.y
) ret
.y
= m_minSize
.y
;
998 void wxSizer::DoSetMinSize( int width
, int height
)
1000 m_minSize
.x
= width
;
1001 m_minSize
.y
= height
;
1004 bool wxSizer::DoSetItemMinSize( wxWindow
*window
, int width
, int height
)
1006 wxASSERT_MSG( window
, _T("SetMinSize for NULL window") );
1008 // Is it our immediate child?
1010 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
1013 wxSizerItem
*item
= node
->GetData();
1015 if (item
->GetWindow() == window
)
1017 item
->SetMinSize( width
, height
);
1020 node
= node
->GetNext();
1023 // No? Search any subsizers we own then
1025 node
= m_children
.GetFirst();
1028 wxSizerItem
*item
= node
->GetData();
1030 if ( item
->GetSizer() &&
1031 item
->GetSizer()->DoSetItemMinSize( window
, width
, height
) )
1033 // A child sizer found the requested windw, exit.
1036 node
= node
->GetNext();
1042 bool wxSizer::DoSetItemMinSize( wxSizer
*sizer
, int width
, int height
)
1044 wxASSERT_MSG( sizer
, _T("SetMinSize for NULL sizer") );
1046 // Is it our immediate child?
1048 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
1051 wxSizerItem
*item
= node
->GetData();
1053 if (item
->GetSizer() == sizer
)
1055 item
->GetSizer()->DoSetMinSize( width
, height
);
1058 node
= node
->GetNext();
1061 // No? Search any subsizers we own then
1063 node
= m_children
.GetFirst();
1066 wxSizerItem
*item
= node
->GetData();
1068 if ( item
->GetSizer() &&
1069 item
->GetSizer()->DoSetItemMinSize( sizer
, width
, height
) )
1071 // A child found the requested sizer, exit.
1074 node
= node
->GetNext();
1080 bool wxSizer::DoSetItemMinSize( size_t index
, int width
, int height
)
1082 wxSizerItemList::compatibility_iterator node
= m_children
.Item( index
);
1084 wxCHECK_MSG( node
, false, _T("Failed to find child node") );
1086 wxSizerItem
*item
= node
->GetData();
1088 if (item
->GetSizer())
1090 // Sizers contains the minimal size in them, if not calculated ...
1091 item
->GetSizer()->DoSetMinSize( width
, height
);
1095 // ... but the minimal size of spacers and windows is stored via the item
1096 item
->SetMinSize( width
, height
);
1102 wxSizerItem
* wxSizer::GetItem( wxWindow
*window
, bool recursive
)
1104 wxASSERT_MSG( window
, _T("GetItem for NULL window") );
1106 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
1109 wxSizerItem
*item
= node
->GetData();
1111 if (item
->GetWindow() == window
)
1115 else if (recursive
&& item
->IsSizer())
1117 wxSizerItem
*subitem
= item
->GetSizer()->GetItem( window
, true );
1122 node
= node
->GetNext();
1128 wxSizerItem
* wxSizer::GetItem( wxSizer
*sizer
, bool recursive
)
1130 wxASSERT_MSG( sizer
, _T("GetItem for NULL sizer") );
1132 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
1135 wxSizerItem
*item
= node
->GetData();
1137 if (item
->GetSizer() == sizer
)
1141 else if (recursive
&& item
->IsSizer())
1143 wxSizerItem
*subitem
= item
->GetSizer()->GetItem( sizer
, true );
1148 node
= node
->GetNext();
1154 wxSizerItem
* wxSizer::GetItem( size_t index
)
1156 wxCHECK_MSG( index
< m_children
.GetCount(),
1158 _T("GetItem index is out of range") );
1160 return m_children
.Item( index
)->GetData();
1163 wxSizerItem
* wxSizer::GetItemById( int id
, bool recursive
)
1165 // This gets a sizer item by the id of the sizer item
1166 // and NOT the id of a window if the item is a window.
1168 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
1171 wxSizerItem
*item
= node
->GetData();
1173 if (item
->GetId() == id
)
1177 else if (recursive
&& item
->IsSizer())
1179 wxSizerItem
*subitem
= item
->GetSizer()->GetItemById( id
, true );
1184 node
= node
->GetNext();
1190 bool wxSizer::Show( wxWindow
*window
, bool show
, bool recursive
)
1192 wxSizerItem
*item
= GetItem( window
, recursive
);
1203 bool wxSizer::Show( wxSizer
*sizer
, bool show
, bool recursive
)
1205 wxSizerItem
*item
= GetItem( sizer
, recursive
);
1216 bool wxSizer::Show( size_t index
, bool show
)
1218 wxSizerItem
*item
= GetItem( index
);
1229 void wxSizer::ShowItems( bool show
)
1231 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
1234 node
->GetData()->Show( show
);
1235 node
= node
->GetNext();
1239 bool wxSizer::IsShown( wxWindow
*window
) const
1241 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
1244 wxSizerItem
*item
= node
->GetData();
1246 if (item
->GetWindow() == window
)
1248 return item
->IsShown();
1250 node
= node
->GetNext();
1253 wxFAIL_MSG( _T("IsShown failed to find sizer item") );
1258 bool wxSizer::IsShown( wxSizer
*sizer
) const
1260 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
1263 wxSizerItem
*item
= node
->GetData();
1265 if (item
->GetSizer() == sizer
)
1267 return item
->IsShown();
1269 node
= node
->GetNext();
1272 wxFAIL_MSG( _T("IsShown failed to find sizer item") );
1277 bool wxSizer::IsShown( size_t index
) const
1279 wxCHECK_MSG( index
< m_children
.GetCount(),
1281 _T("IsShown index is out of range") );
1283 return m_children
.Item( index
)->GetData()->IsShown();
1287 //---------------------------------------------------------------------------
1289 //---------------------------------------------------------------------------
1291 wxGridSizer::wxGridSizer( int rows
, int cols
, int vgap
, int hgap
)
1292 : m_rows( ( cols
== 0 && rows
== 0 ) ? 1 : rows
)
1299 wxGridSizer::wxGridSizer( int cols
, int vgap
, int hgap
)
1300 : m_rows( cols
== 0 ? 1 : 0 )
1307 int wxGridSizer::CalcRowsCols(int& nrows
, int& ncols
) const
1309 int nitems
= m_children
.GetCount();
1315 nrows
= (nitems
+ m_cols
- 1) / m_cols
;
1319 ncols
= (nitems
+ m_rows
- 1) / m_rows
;
1322 else // 0 columns, 0 rows?
1324 wxFAIL_MSG( _T("grid sizer must have either rows or columns fixed") );
1333 void wxGridSizer::RecalcSizes()
1335 int nitems
, nrows
, ncols
;
1336 if ( (nitems
= CalcRowsCols(nrows
, ncols
)) == 0 )
1339 wxSize
sz( GetSize() );
1340 wxPoint
pt( GetPosition() );
1342 int w
= (sz
.x
- (ncols
- 1) * m_hgap
) / ncols
;
1343 int h
= (sz
.y
- (nrows
- 1) * m_vgap
) / nrows
;
1346 for (int c
= 0; c
< ncols
; c
++)
1349 for (int r
= 0; r
< nrows
; r
++)
1351 int i
= r
* ncols
+ c
;
1354 wxSizerItemList::compatibility_iterator node
= m_children
.Item( i
);
1356 wxASSERT_MSG( node
, _T("Failed to find SizerItemList node") );
1358 SetItemBounds( node
->GetData(), x
, y
, w
, h
);
1366 wxSize
wxGridSizer::CalcMin()
1369 if ( CalcRowsCols(nrows
, ncols
) == 0 )
1372 // Find the max width and height for any component
1376 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
1379 wxSizerItem
*item
= node
->GetData();
1380 wxSize
sz( item
->CalcMin() );
1382 w
= wxMax( w
, sz
.x
);
1383 h
= wxMax( h
, sz
.y
);
1385 node
= node
->GetNext();
1388 // In case we have a nested sizer with a two step algo , give it
1389 // a chance to adjust to that (we give it width component)
1390 node
= m_children
.GetFirst();
1391 bool didChangeMinSize
= false;
1394 wxSizerItem
*item
= node
->GetData();
1395 didChangeMinSize
|= item
->InformFirstDirection( wxHORIZONTAL
, w
, -1 );
1397 node
= node
->GetNext();
1400 // And redo iteration in case min size changed
1401 if( didChangeMinSize
)
1403 node
= m_children
.GetFirst();
1407 wxSizerItem
*item
= node
->GetData();
1408 wxSize
sz( item
->GetMinSizeWithBorder() );
1410 w
= wxMax( w
, sz
.x
);
1411 h
= wxMax( h
, sz
.y
);
1413 node
= node
->GetNext();
1417 return wxSize( ncols
* w
+ (ncols
-1) * m_hgap
,
1418 nrows
* h
+ (nrows
-1) * m_vgap
);
1421 void wxGridSizer::SetItemBounds( wxSizerItem
*item
, int x
, int y
, int w
, int h
)
1424 wxSize
sz( item
->GetMinSizeWithBorder() );
1425 int flag
= item
->GetFlag();
1427 if ((flag
& wxEXPAND
) || (flag
& wxSHAPED
))
1433 if (flag
& wxALIGN_CENTER_HORIZONTAL
)
1435 pt
.x
= x
+ (w
- sz
.x
) / 2;
1437 else if (flag
& wxALIGN_RIGHT
)
1439 pt
.x
= x
+ (w
- sz
.x
);
1442 if (flag
& wxALIGN_CENTER_VERTICAL
)
1444 pt
.y
= y
+ (h
- sz
.y
) / 2;
1446 else if (flag
& wxALIGN_BOTTOM
)
1448 pt
.y
= y
+ (h
- sz
.y
);
1452 item
->SetDimension(pt
, sz
);
1455 //---------------------------------------------------------------------------
1457 //---------------------------------------------------------------------------
1459 wxFlexGridSizer::wxFlexGridSizer( int rows
, int cols
, int vgap
, int hgap
)
1460 : wxGridSizer( rows
, cols
, vgap
, hgap
),
1461 m_flexDirection(wxBOTH
),
1462 m_growMode(wxFLEX_GROWMODE_SPECIFIED
)
1466 wxFlexGridSizer::wxFlexGridSizer( int cols
, int vgap
, int hgap
)
1467 : wxGridSizer( cols
, vgap
, hgap
),
1468 m_flexDirection(wxBOTH
),
1469 m_growMode(wxFLEX_GROWMODE_SPECIFIED
)
1473 wxFlexGridSizer::~wxFlexGridSizer()
1477 void wxFlexGridSizer::RecalcSizes()
1480 if ( !CalcRowsCols(nrows
, ncols
) )
1483 const wxPoint
pt(GetPosition());
1484 const wxSize
sz(GetSize());
1486 AdjustForGrowables(sz
);
1488 wxSizerItemList::const_iterator i
= m_children
.begin();
1489 const wxSizerItemList::const_iterator end
= m_children
.end();
1492 for ( int r
= 0; r
< nrows
; r
++ )
1494 if ( m_rowHeights
[r
] == -1 )
1496 // this row is entirely hidden, skip it
1497 for ( int c
= 0; c
< ncols
; c
++ )
1508 const int hrow
= m_rowHeights
[r
];
1509 int h
= sz
.y
- y
; // max remaining height, don't overflow it
1514 for ( int c
= 0; c
< ncols
&& i
!= end
; c
++, ++i
)
1516 const int wcol
= m_colWidths
[c
];
1521 int w
= sz
.x
- x
; // max possible value, ensure we don't overflow
1525 SetItemBounds(*i
, pt
.x
+ x
, pt
.y
+ y
, w
, h
);
1537 // helper function used in CalcMin() to sum up the sizes of non-hidden items
1538 static int SumArraySizes(const wxArrayInt
& sizes
, int gap
)
1540 // Sum total minimum size, including gaps between rows/columns.
1541 // -1 is used as a magic number meaning empty row/column.
1544 const size_t count
= sizes
.size();
1545 for ( size_t n
= 0; n
< count
; n
++ )
1547 if ( sizes
[n
] != -1 )
1550 total
+= gap
; // separate from the previous column
1559 void wxFlexGridSizer::FindWidthsAndHeights(int nrows
, int ncols
)
1561 // We have to recalculate the sizes in case the item minimum size has
1562 // changed since the previous layout, or the item has been hidden using
1563 // wxSizer::Show(). If all the items in a row/column are hidden, the final
1564 // dimension of the row/column will be -1, indicating that the column
1565 // itself is hidden.
1566 m_rowHeights
.assign(nrows
, -1);
1567 m_colWidths
.assign(ncols
, -1);
1569 // n is the index of the item in left-to-right top-to-bottom order
1571 for ( wxSizerItemList::iterator i
= m_children
.begin();
1572 i
!= m_children
.end();
1575 wxSizerItem
* const item
= *i
;
1576 if ( item
->IsShown() )
1578 // NOTE: Not doing the calculation here, this is just
1579 // for finding max values.
1580 const wxSize
sz(item
->GetMinSizeWithBorder());
1582 const int row
= n
/ ncols
;
1583 const int col
= n
% ncols
;
1585 if ( sz
.y
> m_rowHeights
[row
] )
1586 m_rowHeights
[row
] = sz
.y
;
1587 if ( sz
.x
> m_colWidths
[col
] )
1588 m_colWidths
[col
] = sz
.x
;
1592 AdjustForFlexDirection();
1594 m_calculatedMinSize
= wxSize(SumArraySizes(m_colWidths
, m_hgap
),
1595 SumArraySizes(m_rowHeights
, m_vgap
));
1598 wxSize
wxFlexGridSizer::CalcMin()
1603 // Number of rows/columns can change as items are added or removed.
1604 if ( !CalcRowsCols(nrows
, ncols
) )
1608 // We have to recalculate the sizes in case the item minimum size has
1609 // changed since the previous layout, or the item has been hidden using
1610 // wxSizer::Show(). If all the items in a row/column are hidden, the final
1611 // dimension of the row/column will be -1, indicating that the column
1612 // itself is hidden.
1613 m_rowHeights
.assign(nrows
, -1);
1614 m_colWidths
.assign(ncols
, -1);
1616 // n is the index of the item in left-to-right top-to-bottom order
1618 for ( wxSizerItemList::iterator i
= m_children
.begin();
1619 i
!= m_children
.end();
1622 wxSizerItem
* const item
= *i
;
1623 if ( item
->IsShown() )
1629 // The stage of looking for max values in each row/column has been
1630 // made a separate function, since it's reused in AdjustForGrowables.
1631 FindWidthsAndHeights(nrows
,ncols
);
1633 return m_calculatedMinSize
;
1636 void wxFlexGridSizer::AdjustForFlexDirection()
1638 // the logic in CalcMin works when we resize flexibly in both directions
1639 // but maybe this is not the case
1640 if ( m_flexDirection
!= wxBOTH
)
1642 // select the array corresponding to the direction in which we do *not*
1644 wxArrayInt
& array
= m_flexDirection
== wxVERTICAL
? m_colWidths
1647 const size_t count
= array
.GetCount();
1649 // find the largest value in this array
1653 for ( n
= 0; n
< count
; ++n
)
1655 if ( array
[n
] > largest
)
1659 // and now fill it with the largest value
1660 for ( n
= 0; n
< count
; ++n
)
1662 // don't touch hidden rows
1663 if ( array
[n
] != -1 )
1669 // helper of AdjustForGrowables() which is called for rows/columns separately
1672 // delta: the extra space, we do nothing unless it's positive
1673 // growable: indices or growable rows/cols in sizes array
1674 // sizes: the height/widths of rows/cols to adjust
1675 // proportions: proportions of the growable rows/cols or NULL if they all
1676 // should be assumed to have proportion of 1
1678 DoAdjustForGrowables(int delta
,
1679 const wxArrayInt
& growable
,
1681 const wxArrayInt
*proportions
)
1686 // total sum of proportions of all non-hidden rows
1687 int sum_proportions
= 0;
1689 // number of currently shown growable rows
1692 const int max_idx
= sizes
.size();
1694 const size_t count
= growable
.size();
1696 for ( idx
= 0; idx
< count
; idx
++ )
1698 // Since the number of rows/columns can change as items are
1699 // inserted/deleted, we need to verify at runtime that the
1700 // requested growable rows/columns are still valid.
1701 if ( growable
[idx
] >= max_idx
)
1704 // If all items in a row/column are hidden, that row/column will
1705 // have a dimension of -1. This causes the row/column to be
1706 // hidden completely.
1707 if ( sizes
[growable
[idx
]] == -1 )
1711 sum_proportions
+= (*proportions
)[idx
];
1719 // the remaining extra free space, adjusted during each iteration
1720 for ( idx
= 0; idx
< count
; idx
++ )
1722 if ( growable
[idx
] >= max_idx
)
1725 if ( sizes
[ growable
[idx
] ] == -1 )
1729 if ( sum_proportions
== 0 )
1731 // no growable rows -- divide extra space evenly among all
1732 cur_delta
= delta
/num
;
1735 else // allocate extra space proportionally
1737 const int cur_prop
= (*proportions
)[idx
];
1738 cur_delta
= (delta
*cur_prop
)/sum_proportions
;
1739 sum_proportions
-= cur_prop
;
1742 sizes
[growable
[idx
]] += cur_delta
;
1747 void wxFlexGridSizer::AdjustForGrowables(const wxSize
& sz
)
1749 if ( (m_flexDirection
& wxHORIZONTAL
) || (m_growMode
!= wxFLEX_GROWMODE_NONE
) )
1751 DoAdjustForGrowables
1753 sz
.x
- m_calculatedMinSize
.x
,
1756 m_growMode
== wxFLEX_GROWMODE_SPECIFIED
? &m_growableColsProportions
1760 // This gives nested objects that benefit from knowing one size
1761 // component in advance the chance to use that.
1762 bool didAdjustMinSize
= false;
1764 CalcRowsCols(nrows
, ncols
);
1766 // Iterate over all items and inform about column width
1768 for ( wxSizerItemList::iterator i
= m_children
.begin();
1769 i
!= m_children
.end();
1772 const int col
= n
% ncols
;
1773 didAdjustMinSize
|= (*i
)->InformFirstDirection(wxHORIZONTAL
, m_colWidths
[col
], sz
.y
- m_calculatedMinSize
.y
);
1776 // Only redo if info was actually used
1777 if( didAdjustMinSize
)
1779 DoAdjustForGrowables
1781 sz
.x
- m_calculatedMinSize
.x
,
1784 m_growMode
== wxFLEX_GROWMODE_SPECIFIED
? &m_growableColsProportions
1790 if ( (m_flexDirection
& wxVERTICAL
) || (m_growMode
!= wxFLEX_GROWMODE_NONE
) )
1792 // pass NULL instead of proportions if the grow mode is ALL as we
1793 // should treat all rows as having proportion of 1 then
1794 DoAdjustForGrowables
1796 sz
.y
- m_calculatedMinSize
.y
,
1799 m_growMode
== wxFLEX_GROWMODE_SPECIFIED
? &m_growableRowsProportions
1806 void wxFlexGridSizer::AddGrowableRow( size_t idx
, int proportion
)
1808 m_growableRows
.Add( idx
);
1809 m_growableRowsProportions
.Add( proportion
);
1812 void wxFlexGridSizer::AddGrowableCol( size_t idx
, int proportion
)
1814 m_growableCols
.Add( idx
);
1815 m_growableColsProportions
.Add( proportion
);
1818 // helper function for RemoveGrowableCol/Row()
1820 DoRemoveFromArrays(size_t idx
, wxArrayInt
& items
, wxArrayInt
& proportions
)
1822 const size_t count
= items
.size();
1823 for ( size_t n
= 0; n
< count
; n
++ )
1825 if ( (size_t)items
[n
] == idx
)
1828 proportions
.RemoveAt(n
);
1833 wxFAIL_MSG( _T("column/row is already not growable") );
1836 void wxFlexGridSizer::RemoveGrowableCol( size_t idx
)
1838 DoRemoveFromArrays(idx
, m_growableCols
, m_growableColsProportions
);
1841 void wxFlexGridSizer::RemoveGrowableRow( size_t idx
)
1843 DoRemoveFromArrays(idx
, m_growableRows
, m_growableRowsProportions
);
1846 //---------------------------------------------------------------------------
1848 //---------------------------------------------------------------------------
1850 void wxBoxSizer::RecalcSizes()
1852 if ( m_children
.empty() )
1855 const wxCoord totalMinorSize
= GetSizeInMinorDir(m_size
);
1857 // the amount of free space which we should redistribute among the
1858 // stretchable items (i.e. those with non zero proportion)
1859 int delta
= GetSizeInMajorDir(m_size
) - GetSizeInMajorDir(m_minSize
);
1862 // Inform child items about the size in minor direction, that can
1863 // change how much free space we have in major dir and how to distribute it.
1864 int majorMinSum
= 0;
1865 wxSizerItemList::const_iterator i
;
1866 for ( i
= m_children
.begin();
1867 i
!= m_children
.end();
1870 wxSizerItem
* const item
= *i
;
1872 if ( !item
->IsShown() )
1875 wxSize szMinPrev
= item
->GetMinSizeWithBorder();
1876 item
->InformFirstDirection(m_orient
^wxBOTH
,totalMinorSize
,delta
);
1877 wxSize szMin
= item
->GetMinSizeWithBorder();
1878 int deltaChange
= GetSizeInMajorDir(szMin
-szMinPrev
);
1881 // Since we passed available space along to the item, it should not
1882 // take too much, so delta should not become negative.
1883 delta
-= deltaChange
;
1885 majorMinSum
+= GetSizeInMajorDir(item
->GetMinSizeWithBorder());
1887 // And update our min size
1888 SizeInMajorDir(m_minSize
) = majorMinSum
;
1891 // might have a new delta now
1892 delta
= GetSizeInMajorDir(m_size
) - GetSizeInMajorDir(m_minSize
);
1894 // the position at which we put the next child
1895 wxPoint
pt(m_position
);
1897 int totalProportion
= m_totalProportion
;
1898 for ( i
= m_children
.begin();
1899 i
!= m_children
.end();
1902 wxSizerItem
* const item
= *i
;
1904 if ( !item
->IsShown() )
1907 const wxSize
sizeThis(item
->GetMinSizeWithBorder());
1909 // adjust the size in the major direction using the proportion
1910 wxCoord majorSize
= GetSizeInMajorDir(sizeThis
);
1911 const int propItem
= item
->GetProportion();
1914 const int deltaItem
= (delta
* propItem
) / totalProportion
;
1916 majorSize
+= deltaItem
;
1919 totalProportion
-= propItem
;
1923 // apply the alignment in the minor direction
1924 wxPoint
posChild(pt
);
1926 wxCoord minorSize
= GetSizeInMinorDir(sizeThis
);
1927 const int flag
= item
->GetFlag();
1928 if ( flag
& (wxEXPAND
| wxSHAPED
) )
1930 minorSize
= totalMinorSize
;
1932 else if ( flag
& (IsVertical() ? wxALIGN_RIGHT
: wxALIGN_BOTTOM
) )
1934 PosInMinorDir(posChild
) += totalMinorSize
- minorSize
;
1936 // NB: wxCENTRE is used here only for backwards compatibility,
1937 // wxALIGN_CENTRE should be used in new code
1938 else if ( flag
& (wxCENTER
| wxALIGN_CENTRE
) )
1940 PosInMinorDir(posChild
) += (totalMinorSize
- minorSize
) / 2;
1944 // apply RTL adjustment for horizontal sizers:
1945 if ( !IsVertical() && m_containingWindow
)
1947 posChild
.x
= m_containingWindow
->AdjustForLayoutDirection
1955 // finally set size of this child and advance to the next one
1956 item
->SetDimension(posChild
, SizeFromMajorMinor(majorSize
, minorSize
));
1958 PosInMajorDir(pt
) += majorSize
;
1962 wxSize
wxBoxSizer::CalcMin()
1964 m_totalProportion
= 0;
1965 m_minSize
= wxSize(0, 0);
1967 // calculate the minimal sizes for all items and count sum of proportions
1968 for ( wxSizerItemList::const_iterator i
= m_children
.begin();
1969 i
!= m_children
.end();
1972 wxSizerItem
* const item
= *i
;
1974 if ( !item
->IsShown() )
1977 const wxSize sizeMinThis
= item
->CalcMin();
1978 SizeInMajorDir(m_minSize
) += GetSizeInMajorDir(sizeMinThis
);
1979 if ( GetSizeInMinorDir(sizeMinThis
) > GetSizeInMinorDir(m_minSize
) )
1980 SizeInMinorDir(m_minSize
) = GetSizeInMinorDir(sizeMinThis
);
1982 m_totalProportion
+= item
->GetProportion();
1988 //---------------------------------------------------------------------------
1990 //---------------------------------------------------------------------------
1992 #define wxDEFAULT_PROPORTION_LAST 1000000
1994 // User data to hold old proportion for last item on line
1995 // (which might be extended)
1996 struct wxPropHolder
: public wxObject
1998 wxPropHolder( ) : m_item(0), m_propOld(0) { }
1999 void Init( wxSizerItem
*item
, int propOld
) { m_item
=item
; m_propOld
=propOld
; }
2001 wxSizerItem
*m_item
;
2005 IMPLEMENT_DYNAMIC_CLASS(wxWrapSizer
, wxBoxSizer
);
2007 wxWrapSizer::wxWrapSizer( int orient
, int flags
)
2008 : wxBoxSizer(orient
),
2009 m_prim_size_last( -1 ),
2010 m_rows(orient
^wxBOTH
),
2015 wxWrapSizer::~wxWrapSizer()
2017 // Have to clear grand child items so that they're not deleted twice
2018 for( int ix
=m_rows
.GetChildren().GetCount()-1; ix
>=0; ix
-- )
2020 wxSizer
*psz
= m_rows
.GetItem((size_t)ix
)->GetSizer();
2021 wxSizerItemList
&sl
= psz
->GetChildren();
2022 while( sl
.GetLast() )
2023 sl
.Erase( sl
.GetLast() );
2028 bool wxWrapSizer::InformFirstDirection( int direction
, int size
, int WXUNUSED(availableOtherDir
) )
2032 // Better to keep value, then CalcMin will work better
2033 //m_prim_size_last = -1;
2036 if( direction
==m_orient
)
2038 // The direction is same as our primary, so we can make use of it
2039 m_prim_size_last
= size
;
2047 void wxWrapSizer::AdjustPropLastItem(wxSizer
*psz
, wxSizerItem
*itemLast
)
2049 wxSizerItem
*psi
= m_rows
.GetItem(psz
);
2051 wxPropHolder
*pph
= (wxPropHolder
*)psi
->GetUserData();
2053 psi
->SetUserData( pph
=new wxPropHolder
);
2055 pph
->Init( itemLast
, itemLast
->GetProportion() );
2056 itemLast
->SetProportion( wxDEFAULT_PROPORTION_LAST
);
2059 void wxWrapSizer::RecalcSizes()
2061 wxASSERT( m_orient
&wxBOTH
);
2062 if (m_children
.GetCount() == 0)
2065 // What we do here is to put our items into child box sizers,
2066 // as many of them as we have lines.
2068 // Empty all items in all rows in owned sizer.
2069 // We have to access the list directly, since we don't want to
2070 // destroy the wxSizerItems.
2071 for( int ix
=m_rows
.GetChildren().GetCount()-1; ix
>=0; ix
-- ){
2072 wxSizerItem
*psi
= m_rows
.GetItem( (size_t)ix
);
2074 // Restore proportion for last item on line (if item has not been deleted)
2075 wxPropHolder
*pph
= (wxPropHolder
*)psi
->GetUserData();
2076 if( pph
&& GetChildren().Find(pph
->m_item
) )
2077 pph
->m_item
->SetProportion(pph
->m_propOld
);
2079 wxSizer
*psz
= psi
->GetSizer();
2081 wxSizerItemList
&sl
= psz
->GetChildren();
2082 while( sl
.GetLast() )
2083 sl
.Erase( sl
.GetLast() );
2086 int lineSumMajor
= 0;
2087 int majorSize
= GetSizeInMajorDir(m_size
);
2089 // Make sure we have at least one child sizer
2091 if( !m_rows
.GetChildren().GetCount() )
2092 m_rows
.Add( new wxBoxSizer(GetOrientation()), 1, wxEXPAND
);
2094 // The sizer where to insert items in
2095 wxSizer
*psz
= m_rows
.GetItem((size_t)0)->GetSizer();
2098 // Now put our child items into child sizers instead
2099 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
2100 wxSizerItem
*item
= NULL
, *itemLast
=NULL
;
2103 item
= node
->GetData();
2104 if ( item
->IsShown() )
2106 wxSize minSz
= item
->GetMinSize();
2107 int minSzMajor
= GetSizeInMajorDir(minSz
);
2109 // More space on this line?
2110 if( !lineSumMajor
|| lineSumMajor
+minSzMajor
<=majorSize
)
2112 lineSumMajor
+= minSzMajor
;
2116 lineSumMajor
= minSzMajor
;
2117 // Get a new empty sizer to insert into
2118 if( (int)m_rows
.GetChildren().GetCount()<=m_n_line
)
2119 m_rows
.Add( new wxBoxSizer(GetOrientation()), 1, wxEXPAND
);
2121 // If we have extend-last-on-each-line mode, then do so now
2122 // Note: We must store old proportion value then.
2123 if( m_flags
&wxEXTEND_LAST_ON_EACH_LINE
)
2124 AdjustPropLastItem(psz
,itemLast
);
2126 // The sizer where to insert items in
2127 psz
= m_rows
.GetItem(m_n_line
++)->GetSizer();
2131 // If item is a window, it now has a pointer to the child sizer,
2132 // which is wrong. Set it to point to us.
2133 if( item
->GetWindow() )
2134 item
->GetWindow()->SetContainingSizer( this );
2136 node
= node
->GetNext();
2139 // If we have extend-last-on-each-line mode, then do so now
2140 if( m_flags
&wxEXTEND_LAST_ON_EACH_LINE
)
2141 AdjustPropLastItem(psz
,itemLast
);
2143 // If we have more sizers than lines, remove them
2144 while( (int)m_rows
.GetChildren().GetCount()>m_n_line
)
2145 m_rows
.Remove( m_n_line
);
2147 // Now do layout on row sizer
2148 m_rows
.SetDimension( m_position
.x
, m_position
.y
, m_size
.x
, m_size
.y
);
2150 // Remember this to next time (will be overridden by InformFirstDirection if used)
2151 m_prim_size_last
= GetSizeInMajorDir(m_size
);
2155 wxSize
wxWrapSizer::CalcMin()
2157 if (m_children
.GetCount() == 0)
2160 // Algorithm for calculating min size: (assuming horizontal orientation)
2161 // X: Max width of all members
2162 // Y: Based on last X, calculate how many lines needed
2163 // First time around, assume all items fits on one line
2167 int lineMaxMinor
= 0;
2168 int lineSumMajor
= 0;
2171 // precalc item minsizes and fit on lines (preliminary)
2172 wxSizerItemList::compatibility_iterator node
= m_children
.GetFirst();
2175 wxSizerItem
*item
= node
->GetData();
2176 if ( item
->IsShown() )
2178 wxSize minSz
= item
->CalcMin();
2179 int szMajor
= GetSizeInMajorDir(minSz
);
2180 int szMinor
= GetSizeInMinorDir(minSz
);
2181 if( szMajor
>maxMajor
) maxMajor
= szMajor
;
2182 // More space on this line?
2183 if( m_prim_size_last
<0 || !lineSumMajor
||
2184 lineSumMajor
+szMajor
<=m_prim_size_last
)
2186 lineSumMajor
+= szMajor
;
2187 if( szMinor
>lineMaxMinor
)
2188 lineMaxMinor
= szMinor
;
2192 minorSum
+= lineMaxMinor
; // Add height of highest item on last line
2194 lineMaxMinor
= szMinor
;
2195 lineSumMajor
= szMajor
;
2198 node
= node
->GetNext();
2200 minorSum
+= lineMaxMinor
; // Add height of highest item on last line
2202 m_minSize
= SizeFromMajorMinor(maxMajor
, minorSum
);
2206 //---------------------------------------------------------------------------
2208 //---------------------------------------------------------------------------
2212 wxStaticBoxSizer::wxStaticBoxSizer( wxStaticBox
*box
, int orient
)
2213 : wxBoxSizer( orient
),
2216 wxASSERT_MSG( box
, wxT("wxStaticBoxSizer needs a static box") );
2218 // do this so that our Detach() is called if the static box is destroyed
2220 m_staticBox
->SetContainingSizer(this);
2223 wxStaticBoxSizer::wxStaticBoxSizer(int orient
, wxWindow
*win
, const wxString
& s
)
2224 : wxBoxSizer(orient
),
2225 m_staticBox(new wxStaticBox(win
, wxID_ANY
, s
))
2228 m_staticBox
->SetContainingSizer(this);
2231 wxStaticBoxSizer::~wxStaticBoxSizer()
2236 static void GetStaticBoxBorders( wxStaticBox
*box
,
2240 // this has to be done platform by platform as there is no way to
2241 // guess the thickness of a wxStaticBox border
2242 box
->GetBordersForSizer(borderTop
, borderOther
);
2245 void wxStaticBoxSizer::RecalcSizes()
2247 int top_border
, other_border
;
2248 GetStaticBoxBorders(m_staticBox
, &top_border
, &other_border
);
2250 m_staticBox
->SetSize( m_position
.x
, m_position
.y
, m_size
.x
, m_size
.y
);
2252 wxPoint
old_pos( m_position
);
2253 m_position
.x
+= other_border
;
2254 m_position
.y
+= top_border
;
2255 wxSize
old_size( m_size
);
2256 m_size
.x
-= 2*other_border
;
2257 m_size
.y
-= top_border
+ other_border
;
2259 wxBoxSizer::RecalcSizes();
2261 m_position
= old_pos
;
2265 wxSize
wxStaticBoxSizer::CalcMin()
2267 int top_border
, other_border
;
2268 GetStaticBoxBorders(m_staticBox
, &top_border
, &other_border
);
2270 wxSize
ret( wxBoxSizer::CalcMin() );
2271 ret
.x
+= 2*other_border
;
2272 ret
.y
+= other_border
+ top_border
;
2277 void wxStaticBoxSizer::ShowItems( bool show
)
2279 m_staticBox
->Show( show
);
2280 wxBoxSizer::ShowItems( show
);
2283 bool wxStaticBoxSizer::Detach( wxWindow
*window
)
2285 // avoid deleting m_staticBox in our dtor if it's being detached from the
2286 // sizer (which can happen because it's being already destroyed for
2288 if ( window
== m_staticBox
)
2294 return wxSizer::Detach( window
);
2297 #endif // wxUSE_STATBOX
2301 wxStdDialogButtonSizer::wxStdDialogButtonSizer()
2302 : wxBoxSizer(wxHORIZONTAL
)
2304 // Vertical buttons with lots of space on either side
2305 // looks rubbish on WinCE, so let's not do this for now.
2306 // If we are going to use vertical buttons, we should
2307 // put the sizer to the right of other controls in the dialog,
2308 // and that's beyond the scope of this sizer.
2310 bool is_pda
= (wxSystemSettings::GetScreenType() <= wxSYS_SCREEN_PDA
);
2311 // If we have a PDA screen, put yes/no button over
2312 // all other buttons, otherwise on the left side.
2314 m_orient
= wxVERTICAL
;
2317 m_buttonAffirmative
= NULL
;
2318 m_buttonApply
= NULL
;
2319 m_buttonNegative
= NULL
;
2320 m_buttonCancel
= NULL
;
2321 m_buttonHelp
= NULL
;
2324 void wxStdDialogButtonSizer::AddButton(wxButton
*mybutton
)
2326 switch (mybutton
->GetId())
2331 m_buttonAffirmative
= mybutton
;
2334 m_buttonApply
= mybutton
;
2337 m_buttonNegative
= mybutton
;
2341 m_buttonCancel
= mybutton
;
2344 case wxID_CONTEXT_HELP
:
2345 m_buttonHelp
= mybutton
;
2352 void wxStdDialogButtonSizer::SetAffirmativeButton( wxButton
*button
)
2354 m_buttonAffirmative
= button
;
2357 void wxStdDialogButtonSizer::SetNegativeButton( wxButton
*button
)
2359 m_buttonNegative
= button
;
2362 void wxStdDialogButtonSizer::SetCancelButton( wxButton
*button
)
2364 m_buttonCancel
= button
;
2367 void wxStdDialogButtonSizer::Realize()
2370 Add(0, 0, 0, wxLEFT
, 6);
2372 Add((wxWindow
*)m_buttonHelp
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, 6);
2374 if (m_buttonNegative
){
2375 // HIG POLICE BULLETIN - destructive buttons need extra padding
2376 // 24 pixels on either side
2377 Add((wxWindow
*)m_buttonNegative
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, 12);
2380 // extra whitespace between help/negative and cancel/ok buttons
2381 Add(0, 0, 1, wxEXPAND
, 0);
2383 if (m_buttonCancel
){
2384 Add((wxWindow
*)m_buttonCancel
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, 6);
2385 // Cancel or help should be default
2386 // m_buttonCancel->SetDefaultButton();
2389 // Ugh, Mac doesn't really have apply dialogs, so I'll just
2390 // figure the best place is between Cancel and OK
2392 Add((wxWindow
*)m_buttonApply
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, 6);
2394 if (m_buttonAffirmative
){
2395 Add((wxWindow
*)m_buttonAffirmative
, 0, wxALIGN_CENTRE
| wxLEFT
, 6);
2397 if (m_buttonAffirmative
->GetId() == wxID_SAVE
){
2398 // these buttons have set labels under Mac so we should use them
2399 m_buttonAffirmative
->SetLabel(_("Save"));
2400 if (m_buttonNegative
)
2401 m_buttonNegative
->SetLabel(_("Don't Save"));
2405 // Extra space around and at the right
2407 #elif defined(__WXGTK20__)
2408 Add(0, 0, 0, wxLEFT
, 9);
2410 Add((wxWindow
*)m_buttonHelp
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, 3);
2412 // extra whitespace between help and cancel/ok buttons
2413 Add(0, 0, 1, wxEXPAND
, 0);
2415 if (m_buttonNegative
){
2416 Add((wxWindow
*)m_buttonNegative
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, 3);
2419 // according to HIG, in explicit apply windows the order is:
2420 // [ Help Apply Cancel OK ]
2422 Add((wxWindow
*)m_buttonApply
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, 3);
2424 if (m_buttonCancel
){
2425 Add((wxWindow
*)m_buttonCancel
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, 3);
2426 // Cancel or help should be default
2427 // m_buttonCancel->SetDefaultButton();
2430 if (m_buttonAffirmative
)
2431 Add((wxWindow
*)m_buttonAffirmative
, 0, wxALIGN_CENTRE
| wxLEFT
, 6);
2432 #elif defined(__WXMSW__)
2435 // right-justify buttons
2436 Add(0, 0, 1, wxEXPAND
, 0);
2438 if (m_buttonAffirmative
){
2439 Add((wxWindow
*)m_buttonAffirmative
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, m_buttonAffirmative
->ConvertDialogToPixels(wxSize(2, 0)).x
);
2442 if (m_buttonNegative
){
2443 Add((wxWindow
*)m_buttonNegative
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, m_buttonNegative
->ConvertDialogToPixels(wxSize(2, 0)).x
);
2446 if (m_buttonCancel
){
2447 Add((wxWindow
*)m_buttonCancel
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, m_buttonCancel
->ConvertDialogToPixels(wxSize(2, 0)).x
);
2450 Add((wxWindow
*)m_buttonApply
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, m_buttonApply
->ConvertDialogToPixels(wxSize(2, 0)).x
);
2453 Add((wxWindow
*)m_buttonHelp
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, m_buttonHelp
->ConvertDialogToPixels(wxSize(2, 0)).x
);
2455 // GTK+1 and any other platform
2457 // Add(0, 0, 0, wxLEFT, 5); // Not sure what this was for but it unbalances the dialog
2459 Add((wxWindow
*)m_buttonHelp
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, m_buttonHelp
->ConvertDialogToPixels(wxSize(4, 0)).x
);
2461 // extra whitespace between help and cancel/ok buttons
2462 Add(0, 0, 1, wxEXPAND
, 0);
2465 Add((wxWindow
*)m_buttonApply
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, m_buttonApply
->ConvertDialogToPixels(wxSize(4, 0)).x
);
2467 if (m_buttonAffirmative
){
2468 Add((wxWindow
*)m_buttonAffirmative
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, m_buttonAffirmative
->ConvertDialogToPixels(wxSize(4, 0)).x
);
2471 if (m_buttonNegative
){
2472 Add((wxWindow
*)m_buttonNegative
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, m_buttonNegative
->ConvertDialogToPixels(wxSize(4, 0)).x
);
2475 if (m_buttonCancel
){
2476 Add((wxWindow
*)m_buttonCancel
, 0, wxALIGN_CENTRE
| wxLEFT
| wxRIGHT
, m_buttonCancel
->ConvertDialogToPixels(wxSize(4, 0)).x
);
2477 // Cancel or help should be default
2478 // m_buttonCancel->SetDefaultButton();
2484 #endif // wxUSE_BUTTON