|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133 |
- /*
- ==============================================================================
-
- This file is part of the JUCE library.
- Copyright (c) 2013 - Raw Material Software Ltd.
-
- Permission is granted to use this software under the terms of either:
- a) the GPL v2 (or any later version)
- b) the Affero GPL v3
-
- Details of these licenses can be found at: www.gnu.org/licenses
-
- JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
- WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
- A PARTICULAR PURPOSE. See the GNU General Public License for more details.
-
- ------------------------------------------------------------------------------
-
- To release a closed-source product which uses JUCE, commercial licenses are
- available: visit www.juce.com for more information.
-
- ==============================================================================
- */
-
- ComponentMovementWatcher::ComponentMovementWatcher (Component* const comp)
- : component (comp),
- lastPeerID (0),
- reentrant (false),
- wasShowing (comp->isShowing())
- {
- jassert (component != nullptr); // can't use this with a null pointer..
-
- component->addComponentListener (this);
-
- registerWithParentComps();
- }
-
- ComponentMovementWatcher::~ComponentMovementWatcher()
- {
- if (component != nullptr)
- component->removeComponentListener (this);
-
- unregister();
- }
-
- //==============================================================================
- void ComponentMovementWatcher::componentParentHierarchyChanged (Component&)
- {
- if (component != nullptr && ! reentrant)
- {
- const ScopedValueSetter<bool> setter (reentrant, true);
-
- ComponentPeer* const peer = component->getPeer();
- const uint32 peerID = peer != nullptr ? peer->getUniqueID() : 0;
-
- if (peerID != lastPeerID)
- {
- componentPeerChanged();
-
- if (component == nullptr)
- return;
-
- lastPeerID = peerID;
- }
-
- unregister();
- registerWithParentComps();
-
- componentMovedOrResized (*component, true, true);
-
- if (component != nullptr)
- componentVisibilityChanged (*component);
- }
- }
-
- void ComponentMovementWatcher::componentMovedOrResized (Component&, bool wasMoved, bool wasResized)
- {
- if (component != nullptr)
- {
- if (wasMoved)
- {
- const Point<int> pos (component->getTopLevelComponent()->getLocalPoint (component, Point<int>()));
-
- wasMoved = lastBounds.getPosition() != pos;
- lastBounds.setPosition (pos);
- }
-
- wasResized = (lastBounds.getWidth() != component->getWidth() || lastBounds.getHeight() != component->getHeight());
- lastBounds.setSize (component->getWidth(), component->getHeight());
-
- if (wasMoved || wasResized)
- componentMovedOrResized (wasMoved, wasResized);
- }
- }
-
- void ComponentMovementWatcher::componentBeingDeleted (Component& comp)
- {
- registeredParentComps.removeFirstMatchingValue (&comp);
-
- if (component == &comp)
- unregister();
- }
-
- void ComponentMovementWatcher::componentVisibilityChanged (Component&)
- {
- if (component != nullptr)
- {
- const bool isShowingNow = component->isShowing();
-
- if (wasShowing != isShowingNow)
- {
- wasShowing = isShowingNow;
- componentVisibilityChanged();
- }
- }
- }
-
- void ComponentMovementWatcher::registerWithParentComps()
- {
- for (Component* p = component->getParentComponent(); p != nullptr; p = p->getParentComponent())
- {
- p->addComponentListener (this);
- registeredParentComps.add (p);
- }
- }
-
- void ComponentMovementWatcher::unregister()
- {
- for (int i = registeredParentComps.size(); --i >= 0;)
- registeredParentComps.getUnchecked(i)->removeComponentListener (this);
-
- registeredParentComps.clear();
- }
|