+ // This is what GetPosition() returns. Since we calculate
+ // borders afterwards, GetPosition() will be the left/top
+ // corner of the surrounding border.
+ m_pos = pos;
+
+ if (m_flag & wxWEST)
+ {
+ pos.x += m_border;
+ size.x -= m_border;
+ }
+ if (m_flag & wxEAST)
+ {
+ size.x -= m_border;
+ }
+ if (m_flag & wxNORTH)
+ {
+ pos.y += m_border;
+ size.y -= m_border;
+ }
+ if (m_flag & wxSOUTH)
+ {
+ size.y -= m_border;
+ }
+
+ if (size.x < 0)
+ size.x = 0;
+ if (size.y < 0)
+ size.y = 0;
+
+ m_rect = wxRect(pos, size);
+
+ switch ( m_kind )
+ {
+ case Item_None:
+ wxFAIL_MSG( wxT("can't set size of uninitialized sizer item") );
+ break;
+
+ case Item_Window:
+ {
+ // Use wxSIZE_FORCE_EVENT here since a sizer item might
+ // have changed alignment or some other property which would
+ // not change the size of the window. In such a case, no
+ // wxSizeEvent would normally be generated and thus the
+ // control wouldn't get laid out correctly here.
+#if 1
+ m_window->SetSize(pos.x, pos.y, size.x, size.y,
+ wxSIZE_ALLOW_MINUS_ONE|wxSIZE_FORCE_EVENT );
+#else
+ m_window->SetSize(pos.x, pos.y, size.x, size.y,
+ wxSIZE_ALLOW_MINUS_ONE );
+#endif
+ break;
+ }
+ case Item_Sizer:
+ m_sizer->SetDimension(pos, size);
+ break;
+
+ case Item_Spacer:
+ m_spacer->SetSize(size);
+ break;
+
+ case Item_Max:
+ default:
+ wxFAIL_MSG( wxT("unexpected wxSizerItem::m_kind") );
+ }
+}
+
+void wxSizerItem::DeleteWindows()
+{
+ switch ( m_kind )
+ {
+ case Item_None:
+ case Item_Spacer:
+ break;
+
+ case Item_Window:
+ //We are deleting the window from this sizer - normally
+ //the window destroys the sizer associated with it,
+ //which might destroy this, which we don't want
+ m_window->SetContainingSizer(NULL);
+ m_window->Destroy();
+ //Putting this after the switch will result in a spacer
+ //not being deleted properly on destruction
+ m_kind = Item_None;
+ break;
+
+ case Item_Sizer:
+ m_sizer->DeleteWindows();
+ break;
+
+ case Item_Max:
+ default:
+ wxFAIL_MSG( wxT("unexpected wxSizerItem::m_kind") );
+ }
+
+}
+
+void wxSizerItem::Show( bool show )
+{
+ switch ( m_kind )
+ {
+ case Item_None:
+ wxFAIL_MSG( wxT("can't show uninitialized sizer item") );
+ break;
+
+ case Item_Window:
+ m_window->Show(show);
+ break;
+
+ case Item_Sizer:
+ m_sizer->Show(show);
+ break;
+
+ case Item_Spacer:
+ m_spacer->Show(show);
+ break;
+
+ case Item_Max:
+ default:
+ wxFAIL_MSG( wxT("unexpected wxSizerItem::m_kind") );
+ }
+}
+
+bool wxSizerItem::IsShown() const
+{
+ if ( m_flag & wxRESERVE_SPACE_EVEN_IF_HIDDEN )
+ return true;
+
+ switch ( m_kind )
+ {
+ case Item_None:
+ // we may be called from CalcMin(), just return false so that we're
+ // not used
+ break;
+
+ case Item_Window:
+ return m_window->IsShown();
+
+ case Item_Sizer:
+ {
+ // arbitrarily decide that if at least one of our elements is
+ // shown, so are we (this arbitrariness is the reason for
+ // deprecating this function)
+ for ( wxSizerItemList::compatibility_iterator
+ node = m_sizer->GetChildren().GetFirst();
+ node;
+ node = node->GetNext() )
+ {
+ if ( node->GetData()->IsShown() )
+ return true;
+ }
+ return false;
+ }
+
+ case Item_Spacer:
+ return m_spacer->IsShown();
+
+ case Item_Max:
+ default:
+ wxFAIL_MSG( wxT("unexpected wxSizerItem::m_kind") );
+ }
+
+ return false;
+}
+
+#if WXWIN_COMPATIBILITY_2_6
+void wxSizerItem::SetOption( int option )
+{
+ SetProportion( option );
+}
+
+int wxSizerItem::GetOption() const
+{
+ return GetProportion();
+}
+#endif // WXWIN_COMPATIBILITY_2_6
+
+
+//---------------------------------------------------------------------------
+// wxSizer
+//---------------------------------------------------------------------------
+
+wxSizer::~wxSizer()
+{
+ WX_CLEAR_LIST(wxSizerItemList, m_children);
+}
+
+wxSizerItem* wxSizer::DoInsert( size_t index, wxSizerItem *item )
+{
+ m_children.Insert( index, item );
+
+ if ( item->GetWindow() )
+ item->GetWindow()->SetContainingSizer( this );
+
+ if ( item->GetSizer() )
+ item->GetSizer()->SetContainingWindow( m_containingWindow );
+
+ return item;
+}
+
+void wxSizer::SetContainingWindow(wxWindow *win)
+{
+ if ( win == m_containingWindow )
+ return;
+
+ m_containingWindow = win;
+
+ // set the same window for all nested sizers as well, they also are in the
+ // same window
+ for ( wxSizerItemList::compatibility_iterator node = m_children.GetFirst();
+ node;
+ node = node->GetNext() )
+ {
+ wxSizerItem *const item = node->GetData();
+ wxSizer *const sizer = item->GetSizer();
+
+ if ( sizer )
+ {
+ sizer->SetContainingWindow(win);
+ }
+ }
+}
+
+#if WXWIN_COMPATIBILITY_2_6
+bool wxSizer::Remove( wxWindow *window )
+{
+ return Detach( window );
+}
+#endif // WXWIN_COMPATIBILITY_2_6
+
+bool wxSizer::Remove( wxSizer *sizer )
+{
+ wxASSERT_MSG( sizer, wxT("Removing NULL sizer") );
+
+ wxSizerItemList::compatibility_iterator node = m_children.GetFirst();
+ while (node)
+ {
+ wxSizerItem *item = node->GetData();
+
+ if (item->GetSizer() == sizer)
+ {
+ delete item;
+ m_children.Erase( node );
+ return true;
+ }
+
+ node = node->GetNext();
+ }
+
+ return false;
+}
+
+bool wxSizer::Remove( int index )
+{
+ wxCHECK_MSG( index >= 0 && (size_t)index < m_children.GetCount(),
+ false,
+ wxT("Remove index is out of range") );
+
+ wxSizerItemList::compatibility_iterator node = m_children.Item( index );
+
+ wxCHECK_MSG( node, false, wxT("Failed to find child node") );
+
+ delete node->GetData();
+ m_children.Erase( node );
+
+ return true;
+}
+
+bool wxSizer::Detach( wxSizer *sizer )
+{
+ wxASSERT_MSG( sizer, wxT("Detaching NULL sizer") );
+
+ wxSizerItemList::compatibility_iterator node = m_children.GetFirst();
+ while (node)
+ {
+ wxSizerItem *item = node->GetData();
+
+ if (item->GetSizer() == sizer)
+ {
+ item->DetachSizer();
+ delete item;
+ m_children.Erase( node );
+ return true;
+ }
+ node = node->GetNext();
+ }
+
+ return false;
+}
+
+bool wxSizer::Detach( wxWindow *window )
+{
+ wxASSERT_MSG( window, wxT("Detaching NULL window") );
+
+ wxSizerItemList::compatibility_iterator node = m_children.GetFirst();
+ while (node)
+ {
+ wxSizerItem *item = node->GetData();
+
+ if (item->GetWindow() == window)
+ {
+ delete item;
+ m_children.Erase( node );
+ return true;
+ }
+ node = node->GetNext();
+ }
+
+ return false;
+}
+
+bool wxSizer::Detach( int index )
+{
+ wxCHECK_MSG( index >= 0 && (size_t)index < m_children.GetCount(),
+ false,
+ wxT("Detach index is out of range") );
+
+ wxSizerItemList::compatibility_iterator node = m_children.Item( index );
+
+ wxCHECK_MSG( node, false, wxT("Failed to find child node") );
+
+ wxSizerItem *item = node->GetData();
+
+ if ( item->IsSizer() )
+ item->DetachSizer();
+
+ delete item;
+ m_children.Erase( node );
+ return true;
+}
+
+bool wxSizer::Replace( wxWindow *oldwin, wxWindow *newwin, bool recursive )
+{
+ wxASSERT_MSG( oldwin, wxT("Replacing NULL window") );
+ wxASSERT_MSG( newwin, wxT("Replacing with NULL window") );
+
+ wxSizerItemList::compatibility_iterator node = m_children.GetFirst();
+ while (node)
+ {
+ wxSizerItem *item = node->GetData();
+
+ if (item->GetWindow() == oldwin)
+ {
+ item->AssignWindow(newwin);
+ newwin->SetContainingSizer( this );
+ return true;
+ }
+ else if (recursive && item->IsSizer())
+ {
+ if (item->GetSizer()->Replace( oldwin, newwin, true ))
+ return true;
+ }
+
+ node = node->GetNext();
+ }
+
+ return false;
+}
+
+bool wxSizer::Replace( wxSizer *oldsz, wxSizer *newsz, bool recursive )
+{
+ wxASSERT_MSG( oldsz, wxT("Replacing NULL sizer") );
+ wxASSERT_MSG( newsz, wxT("Replacing with NULL sizer") );
+
+ wxSizerItemList::compatibility_iterator node = m_children.GetFirst();
+ while (node)
+ {
+ wxSizerItem *item = node->GetData();
+
+ if (item->GetSizer() == oldsz)
+ {
+ item->AssignSizer(newsz);
+ return true;
+ }
+ else if (recursive && item->IsSizer())
+ {
+ if (item->GetSizer()->Replace( oldsz, newsz, true ))
+ return true;
+ }
+
+ node = node->GetNext();
+ }
+
+ return false;
+}
+
+bool wxSizer::Replace( size_t old, wxSizerItem *newitem )
+{
+ wxCHECK_MSG( old < m_children.GetCount(), false, wxT("Replace index is out of range") );
+ wxASSERT_MSG( newitem, wxT("Replacing with NULL item") );
+
+ wxSizerItemList::compatibility_iterator node = m_children.Item( old );
+
+ wxCHECK_MSG( node, false, wxT("Failed to find child node") );
+
+ wxSizerItem *item = node->GetData();
+ node->SetData(newitem);
+
+ if (item->IsWindow() && item->GetWindow())
+ item->GetWindow()->SetContainingSizer(NULL);
+
+ delete item;
+
+ return true;
+}
+
+void wxSizer::Clear( bool delete_windows )
+{
+ // First clear the ContainingSizer pointers
+ wxSizerItemList::compatibility_iterator node = m_children.GetFirst();
+ while (node)
+ {
+ wxSizerItem *item = node->GetData();
+
+ if (item->IsWindow())
+ item->GetWindow()->SetContainingSizer( NULL );
+ node = node->GetNext();
+ }
+
+ // Destroy the windows if needed
+ if (delete_windows)
+ DeleteWindows();
+
+ // Now empty the list
+ WX_CLEAR_LIST(wxSizerItemList, m_children);
+}
+
+void wxSizer::DeleteWindows()
+{
+ wxSizerItemList::compatibility_iterator node = m_children.GetFirst();
+ while (node)
+ {
+ wxSizerItem *item = node->GetData();
+
+ item->DeleteWindows();
+ node = node->GetNext();
+ }
+}
+
+wxSize wxSizer::ComputeFittingClientSize(wxWindow *window)
+{
+ wxCHECK_MSG( window, wxDefaultSize, "window can't be NULL" );
+
+ // take the min size by default and limit it by max size
+ wxSize size = GetMinClientSize(window);
+ wxSize sizeMax;
+
+ wxTopLevelWindow *tlw = wxDynamicCast(window, wxTopLevelWindow);
+ if ( tlw )
+ {
+ // hack for small screen devices where TLWs are always full screen
+ if ( tlw->IsAlwaysMaximized() )
+ {
+ return tlw->GetClientSize();
+ }
+
+ // limit the window to the size of the display it is on
+ int disp = wxDisplay::GetFromWindow(window);
+ if ( disp == wxNOT_FOUND )
+ {
+ // or, if we don't know which one it is, of the main one
+ disp = 0;
+ }
+
+ sizeMax = wxDisplay(disp).GetClientArea().GetSize();
+
+ // If determining the display size failed, skip the max size checks as
+ // we really don't want to create windows of (0, 0) size.
+ if ( !sizeMax.x || !sizeMax.y )
+ return size;
+
+ // space for decorations and toolbars etc.
+ sizeMax = tlw->WindowToClientSize(sizeMax);
+ }
+ else
+ {
+ sizeMax = GetMaxClientSize(window);
+ }
+
+ if ( sizeMax.x != wxDefaultCoord && size.x > sizeMax.x )
+ size.x = sizeMax.x;
+ if ( sizeMax.y != wxDefaultCoord && size.y > sizeMax.y )
+ size.y = sizeMax.y;
+
+ return size;
+}
+
+wxSize wxSizer::ComputeFittingWindowSize(wxWindow *window)
+{
+ wxCHECK_MSG( window, wxDefaultSize, "window can't be NULL" );
+
+ return window->ClientToWindowSize(ComputeFittingClientSize(window));
+}
+
+wxSize wxSizer::Fit( wxWindow *window )
+{
+ wxCHECK_MSG( window, wxDefaultSize, "window can't be NULL" );
+
+ // set client size
+ window->SetClientSize(ComputeFittingClientSize(window));
+
+ // return entire size
+ return window->GetSize();
+}
+
+void wxSizer::FitInside( wxWindow *window )
+{
+ wxSize size;
+ if (window->IsTopLevel())
+ size = VirtualFitSize( window );
+ else
+ size = GetMinClientSize( window );
+
+ window->SetVirtualSize( size );
+}
+
+void wxSizer::Layout()
+{
+ // (re)calculates minimums needed for each item and other preparations
+ // for layout
+ CalcMin();
+
+ // Applies the layout and repositions/resizes the items
+ RecalcSizes();
+}
+
+void wxSizer::SetSizeHints( wxWindow *window )
+{
+ // Preserve the window's max size hints, but set the
+ // lower bound according to the sizer calculations.
+
+ // This is equivalent to calling Fit(), except that we need to set
+ // the size hints _in between_ the two steps performed by Fit
+ // (1. ComputeFittingClientSize, 2. SetClientSize). That's because
+ // otherwise SetClientSize() could have no effect if there already are
+ // size hints in effect that forbid requested client size.
+
+ const wxSize clientSize = ComputeFittingClientSize(window);
+
+ window->SetMinClientSize(clientSize);
+ window->SetClientSize(clientSize);
+}
+
+#if WXWIN_COMPATIBILITY_2_8
+void wxSizer::SetVirtualSizeHints( wxWindow *window )
+{
+ FitInside( window );
+}
+#endif // WXWIN_COMPATIBILITY_2_8
+
+// TODO on mac we need a function that determines how much free space this
+// min size contains, in order to make sure that we have 20 pixels of free
+// space around the controls
+wxSize wxSizer::GetMaxClientSize( wxWindow *window ) const
+{
+ return window->WindowToClientSize(window->GetMaxSize());
+}
+
+wxSize wxSizer::GetMinClientSize( wxWindow *WXUNUSED(window) )
+{
+ return GetMinSize(); // Already returns client size.
+}
+
+wxSize wxSizer::VirtualFitSize( wxWindow *window )
+{
+ wxSize size = GetMinClientSize( window );
+ wxSize sizeMax = GetMaxClientSize( window );
+
+ // Limit the size if sizeMax != wxDefaultSize
+
+ if ( size.x > sizeMax.x && sizeMax.x != wxDefaultCoord )
+ size.x = sizeMax.x;
+ if ( size.y > sizeMax.y && sizeMax.y != wxDefaultCoord )
+ size.y = sizeMax.y;
+
+ return size;
+}
+
+wxSize wxSizer::GetMinSize()
+{
+ wxSize ret( CalcMin() );
+ if (ret.x < m_minSize.x) ret.x = m_minSize.x;
+ if (ret.y < m_minSize.y) ret.y = m_minSize.y;
+ return ret;
+}
+
+void wxSizer::DoSetMinSize( int width, int height )
+{
+ m_minSize.x = width;
+ m_minSize.y = height;
+}
+
+bool wxSizer::DoSetItemMinSize( wxWindow *window, int width, int height )
+{
+ wxASSERT_MSG( window, wxT("SetMinSize for NULL window") );
+
+ // Is it our immediate child?
+
+ wxSizerItemList::compatibility_iterator node = m_children.GetFirst();
+ while (node)
+ {
+ wxSizerItem *item = node->GetData();
+
+ if (item->GetWindow() == window)
+ {
+ item->SetMinSize( width, height );
+ return true;
+ }
+ node = node->GetNext();
+ }
+
+ // No? Search any subsizers we own then