From: Ron Lee Date: Mon, 30 Dec 2002 13:02:15 +0000 (+0000) Subject: Add wxSizer::Detach so we can detach child sizers without deletion. X-Git-Url: https://git.saurik.com/wxWidgets.git/commitdiff_plain/00976fe56baabfe05676ace71a9fea803fb5bb2e Add wxSizer::Detach so we can detach child sizers without deletion. Updated wxSizer docs for this and a couple of other missing/outdated things. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@18460 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- diff --git a/docs/latex/wx/sizer.tex b/docs/latex/wx/sizer.tex index b7d1b8ee4e..d6b90c9c4e 100644 --- a/docs/latex/wx/sizer.tex +++ b/docs/latex/wx/sizer.tex @@ -60,10 +60,9 @@ The destructor. \func{void}{Add}{\param{int }{width}, \param{int }{height}, \param{int }{proportion = 0}, \param{int }{flag = 0}, \param{int }{border = 0}, \param{wxObject* }{userData = NULL}} -Adds the {\it window} to the sizer. As wxSizer itself is an abstract class, the parameters -have no meaning in the wxSizer class itself, but as there currently is only one class -deriving directly from wxSizer and this class does not override these methods, the meaning -of the parameters is described here: +Appends a child to the sizer. wxSizer itself is an abstract class, but the parameters are +equivalent in the derived classes that you will instantiate to use it so they are described +here: \docparam{window}{The window to be added to the sizer. Its initial size (either set explicitly by the user or calculated internally when using wxDefaultSize) is interpreted as the minimal and in many @@ -123,6 +122,31 @@ complex than the {\it proportion} and {\it flag} will allow for.} This method is abstract and has to be overwritten by any derived class. Here, the sizer will do the actual calculation of its children minimal sizes. +\membersection{wxSizer::Detach}\label{wxsizerdetach} + +\func{bool}{Detach}{\param{wxWindow* }{window}} + +\func{bool}{Detach}{\param{wxSizer* }{sizer}} + +\func{bool}{Detach}{\param{int }{nth}} + +Detach a child from the sizer without destroying it. {\it window} is the window to be +detached, {\it sizer} is the equivalent sizer and {\it nth} is the position of +the child in the sizer, typically 0 for the first item. This method does not +cause any layout or resizing to take place, call \helpref{wxSizer::Layout}{wxsizerlayout} +to update the layout "on screen" after detaching a child from the sizer. + +{\bf NB:} Detaching a wxWindow from a wxSizer is equivalent to Removing it. There is +currently no wxSizer method that will detach and destroy a window automatically. +You must either act to destroy it yourself, or allow its parent to destroy it in the +normal course of events. + +Returns TRUE if the child item was found and detached, FALSE otherwise. + +\wxheading{See also} + +\helpref{wxSizer::Remove}{wxsizerremove} + \membersection{wxSizer::Fit}\label{wxsizerfit} \func{wxSize}{Fit}{\param{wxWindow* }{window}} @@ -165,6 +189,20 @@ Returns the minimal size of the sizer. This is either the combined minimal size of all the children and their borders or the minimal size set by \helpref{SetMinSize}{wxsizersetminsize}, depending on which is bigger. +\membersection{wxSizer::Insert}\label{wxsizerinsert} + +\func{void}{Insert}{\param{int }{before}, \param{wxWindow* }{window}, \param{int }{proportion = 0},\param{int }{flag = 0}, \param{int }{border = 0}, \param{wxObject* }{userData = NULL}} + +\func{void}{Insert}{\param{int }{before}, \param{wxSizer* }{sizer}, \param{int }{proportion = 0}, \param{int }{flag = 0}, \param{int }{border = 0}, \param{wxObject* }{userData = NULL}} + +\func{void}{Insert}{\param{int }{before}, \param{int }{width}, \param{int }{height}, \param{int }{proportion = 0}, \param{int }{flag = 0}, \param{int }{border = 0}, \param{wxObject* }{userData = NULL}} + +Insert a child into the sizer. + +\docparam{before}{The position this child should assume in the sizer.} + +See \helpref{wxSizer::Add}{wxsizeradd} for the meaning of the other parameters. + \membersection{wxSizer::Layout}\label{wxsizerlayout} \func{void}{Layout}{\void} @@ -202,9 +240,12 @@ and sizes. Removes a child from the sizer. {\it window} is the window to be removed, {\it sizer} is the equivalent sizer and {\it nth} is the position of the child in the sizer, typically 0 for -the first item. This method does not cause any layout or resizing to take place and does -not delete the window itself. Call \helpref{wxSizer::Layout}{wxsizerlayout} to update -the layout "on screen" after removing a child from the sizer. +the first item. This method does not cause any layout or resizing to take place, call +\helpref{wxSizer::Layout}{wxsizerlayout} to update the layout "on screen" after removing a +child from the sizer. + +{\bf NB:} wxWindows are not deleted by Remove, but wxSizers are. To remove a sizer without +deleting it, use \helpref{wxSizer::Detach}{wxsizerdetach} instead. Returns TRUE if the child item was found and removed, FALSE otherwise. diff --git a/include/wx/sizer.h b/include/wx/sizer.h index ece3be312e..c3741cfedc 100644 --- a/include/wx/sizer.h +++ b/include/wx/sizer.h @@ -82,6 +82,8 @@ public: { m_border = border; } void Show ( bool show ) { m_show = show; } + void SetDeleteItem( bool deleteItem = TRUE ) + { m_deleteItem = deleteItem; } wxWindow *GetWindow() const { return m_window; } @@ -122,6 +124,11 @@ protected: // is shrinked. it is safer to preserve initial value. float m_ratio; + // If TRUE, and the item is a sizer, delete it when the + // sizeritem is destroyed. Not used for any other type + // of item right now. + bool m_deleteItem; + wxObject *m_userData; private: @@ -151,10 +158,17 @@ public: virtual void Prepend( wxSizer *sizer, int option = 0, int flag = 0, int border = 0, wxObject* userData = NULL ); virtual void Prepend( int width, int height, int option = 0, int flag = 0, int border = 0, wxObject* userData = NULL ); + // Remove will delete a sizer, but not a window. virtual bool Remove( wxWindow *window ); virtual bool Remove( wxSizer *sizer ); virtual bool Remove( int pos ); - + + // Detach will never destroy a sizer or window. + virtual bool Detach( wxWindow *window ) + { return Remove( window ); } + virtual bool Detach( wxSizer *sizer ); + virtual bool Detach( int pos ); + virtual void Clear( bool delete_windows=FALSE ); virtual void DeleteWindows(); diff --git a/src/common/sizer.cpp b/src/common/sizer.cpp index 36849b8cd1..f227e41b17 100644 --- a/src/common/sizer.cpp +++ b/src/common/sizer.cpp @@ -44,68 +44,60 @@ IMPLEMENT_ABSTRACT_CLASS(wxNotebookSizer, wxSizer) //--------------------------------------------------------------------------- wxSizerItem::wxSizerItem( int width, int height, int option, int flag, int border, wxObject* userData ) + : m_window( 0 ) + , m_sizer( 0 ) + , m_size( wxSize( width, height ) ) // size is set directly + , m_minSize( m_size ) // minimal size is the initial size + , m_option( option ) + , m_border( border ) + , m_flag( flag ) + , m_show( TRUE ) // Cannot be changed + , m_deleteItem( FALSE ) // unused for spacer + , m_userData( userData ) { - m_window = (wxWindow *) NULL; - m_sizer = (wxSizer *) NULL; - m_option = option; - m_border = border; - m_flag = flag; - m_show = TRUE; // Cannot be changed - m_userData = userData; - - // minimal size is the initial size - m_minSize.x = width; - m_minSize.y = height; - - SetRatio(width, height); - - // size is set directly - m_size = m_minSize; + SetRatio( m_size ); } wxSizerItem::wxSizerItem( wxWindow *window, int option, int flag, int border, wxObject* userData ) + : m_window( window ) + , m_sizer( 0 ) + , m_minSize( window->GetSize() ) // minimal size is the initial size + , m_option( option ) + , m_border( border ) + , m_flag( flag ) + , m_show( TRUE ) + , m_deleteItem( FALSE ) // currently unused for window + , m_userData( userData ) { - m_window = window; - m_sizer = (wxSizer *) NULL; - m_option = option; - m_border = border; - m_flag = flag; - m_show = TRUE; - m_userData = userData; - - // minimal size is the initial size - m_minSize = window->GetSize(); - // aspect ratio calculated from initial size - SetRatio(m_minSize); + SetRatio( m_minSize ); - // size is calculated later - // m_size = ... + // m_size is calculated later } wxSizerItem::wxSizerItem( wxSizer *sizer, int option, int flag, int border, wxObject* userData ) + : m_window( 0 ) + , m_sizer( sizer ) + , m_option( option ) + , m_border( border ) + , m_flag( flag ) + , m_show( TRUE ) + , m_ratio( 0 ) + , m_deleteItem( TRUE ) // we delete sizer items by default. + , m_userData( userData ) { - m_window = (wxWindow *) NULL; - m_sizer = sizer; - m_option = option; - m_border = border; - m_flag = flag; - m_show = TRUE; - m_userData = userData; - - // minimal size is calculated later - // m_minSize = ... - m_ratio = 0; - - // size is calculated later - // m_size = ... + // m_minSize is calculated later + // m_size is calculated later } wxSizerItem::~wxSizerItem() { + // User data is bound to the sizeritem, always delete it. if (m_userData) delete m_userData; - if (m_sizer) + + // To be able to Detach a sizer, we must be able to veto its deletion here. + if (m_deleteItem && m_sizer) delete m_sizer; } @@ -371,6 +363,39 @@ bool wxSizer::Remove( int pos ) return TRUE; } +bool wxSizer::Detach( wxSizer *sizer ) +{ + wxASSERT( sizer ); + + wxNode *node = m_children.First(); + while (node) + { + wxSizerItem *item = (wxSizerItem*)node->Data(); + if (item->GetSizer() == sizer) + { + item->SetDeleteItem( FALSE ); + m_children.DeleteNode( node ); + return TRUE; + } + node = node->Next(); + } + + return FALSE; +} + +bool wxSizer::Detach( int pos ) +{ + if ((size_t)pos >= m_children.GetCount()) + return FALSE; + wxNode *node = m_children.Nth( pos ); + if (!node) return FALSE; + + ( (wxSizerItem*)node->Data() )->SetDeleteItem( FALSE ); + m_children.DeleteNode( node ); + + return TRUE; +} + void wxSizer::Clear( bool delete_windows ) { // First clear the ContainingSizer pointers