From 8b2f64c6c12f3f110480040b3d8b5328f156c295 Mon Sep 17 00:00:00 2001 From: Vadim Zeitlin Date: Tue, 27 Mar 2012 11:58:02 +0000 Subject: [PATCH] Added wxWindow::IsDescendant() helper. This function checks if another window is a direct or indirect child of this one, which can be needed in a number of situations. See #3063. git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@71024 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775 --- include/wx/window.h | 4 ++++ interface/wx/window.h | 17 +++++++++++++++++ src/common/wincmn.cpp | 21 ++++++++++++++++++++- 3 files changed, 41 insertions(+), 1 deletion(-) diff --git a/include/wx/window.h b/include/wx/window.h index 774be3539d..7a15b0bee7 100644 --- a/include/wx/window.h +++ b/include/wx/window.h @@ -740,6 +740,10 @@ public: // is this window a top level one? virtual bool IsTopLevel() const; + // is this window a child or grand child of this one (inside the same + // TLW)? + bool IsDescendant(wxWindowBase* win) const; + // it doesn't really change parent, use Reparent() instead void SetParent( wxWindowBase *parent ) { m_parent = (wxWindow *)parent; } // change the real parent of this window, return true if the parent diff --git a/interface/wx/window.h b/interface/wx/window.h index 83725acb33..89d34bd2fd 100644 --- a/interface/wx/window.h +++ b/interface/wx/window.h @@ -493,6 +493,23 @@ public: @see GetNextSibling() */ wxWindow* GetPrevSibling() const; + + /** + Check if the specified window is a descendant of this one. + + Returns @true if the window is a descendant (i.e. a child or + grand-child or grand-grand-child or ...) of this one. + + Notice that a window can never be a descendant of another one if they + are in different top level windows, i.e. a child of a wxDialog is not + considered to be a descendant of dialogs parent wxFrame. + + @param win Any window, possible @NULL (@false is always returned then). + + @since 2.9.4 + */ + bool IsDescendant(wxWindowBase* win) const; + /** Reparents the window, i.e. the window will be removed from its current parent window (e.g. a non-standard toolbar in a wxFrame) diff --git a/src/common/wincmn.cpp b/src/common/wincmn.cpp index 068a15bc40..17d8f40a31 100644 --- a/src/common/wincmn.cpp +++ b/src/common/wincmn.cpp @@ -1242,9 +1242,28 @@ void wxWindowBase::Thaw() } // ---------------------------------------------------------------------------- -// reparenting the window +// Dealing with parents and children. // ---------------------------------------------------------------------------- +bool wxWindowBase::IsDescendant(wxWindowBase* win) const +{ + // Iterate until we find this window in the parent chain or exhaust it. + while ( win ) + { + wxWindow* const parent = win->GetParent(); + if ( parent == this ) + return true; + + // Stop iterating on reaching the top level window boundary. + if ( parent->IsTopLevel() ) + break; + + win = parent; + } + + return false; +} + void wxWindowBase::AddChild(wxWindowBase *child) { wxCHECK_RET( child, wxT("can't add a NULL child") );