From fee33f45fdd9f68ed547b7479f53e4c716ee919d Mon Sep 17 00:00:00 2001 From: jules Date: Fri, 30 Sep 2011 17:06:50 +0100 Subject: [PATCH] Added Component::findChildWithID --- .../juce_gui_basics/components/juce_Component.cpp | 12 ++++++++++++ modules/juce_gui_basics/components/juce_Component.h | 9 +++++++-- .../juce_RelativeCoordinatePositioner.cpp | 10 +--------- 3 files changed, 20 insertions(+), 11 deletions(-) diff --git a/modules/juce_gui_basics/components/juce_Component.cpp b/modules/juce_gui_basics/components/juce_Component.cpp index a654c8e07c..f3d603e7a5 100644 --- a/modules/juce_gui_basics/components/juce_Component.cpp +++ b/modules/juce_gui_basics/components/juce_Component.cpp @@ -1440,6 +1440,18 @@ int Component::getIndexOfChildComponent (const Component* const child) const noe return childComponentList.indexOf (const_cast (child)); } +Component* Component::findChildWithID (const String& targetID) const noexcept +{ + for (int i = childComponentList.size(); --i >= 0;) + { + Component* const c = childComponentList.getUnchecked(i); + if (c->componentID == targetID) + return c; + } + + return nullptr; +} + Component* Component::getTopLevelComponent() const noexcept { const Component* comp = this; diff --git a/modules/juce_gui_basics/components/juce_Component.h b/modules/juce_gui_basics/components/juce_Component.h index 988a3ad339..d4b58d296c 100644 --- a/modules/juce_gui_basics/components/juce_Component.h +++ b/modules/juce_gui_basics/components/juce_Component.h @@ -101,13 +101,13 @@ public: virtual void setName (const String& newName); /** Returns the ID string that was set by setComponentID(). - @see setComponentID + @see setComponentID, findChildWithID */ const String& getComponentID() const noexcept { return componentID; } /** Sets the component's ID string. You can retrieve the ID using getComponentID(). - @see getComponentID + @see getComponentID, findChildWithID */ void setComponentID (const String& newID); @@ -699,6 +699,11 @@ public: */ int getIndexOfChildComponent (const Component* child) const noexcept; + /** Looks for a child component with the specified ID. + @see setComponentID, getComponentID + */ + Component* findChildWithID (const String& componentID) const noexcept; + /** Adds a child component to this one. Adding a child component does not mean that the component will own or delete the child - it's diff --git a/modules/juce_gui_basics/positioning/juce_RelativeCoordinatePositioner.cpp b/modules/juce_gui_basics/positioning/juce_RelativeCoordinatePositioner.cpp index e14b7487a8..b22eb6b5ba 100644 --- a/modules/juce_gui_basics/positioning/juce_RelativeCoordinatePositioner.cpp +++ b/modules/juce_gui_basics/positioning/juce_RelativeCoordinatePositioner.cpp @@ -80,15 +80,7 @@ Component* RelativeCoordinatePositionerBase::ComponentScope::findSiblingComponen Component* const parent = component.getParentComponent(); if (parent != nullptr) - { - for (int i = parent->getNumChildComponents(); --i >= 0;) - { - Component* const c = parent->getChildComponent(i); - - if (c->getComponentID() == componentID) - return c; - } - } + return parent->findChildWithID (componentID); return nullptr; }