Add remaining files
[juce-lv2.git] / juce / source / src / gui / components / layout / juce_ComponentMovementWatcher.cpp
blobff0376e69a1e6c402204c98b33705420441c8f45
1 /*
2 ==============================================================================
4 This file is part of the JUCE library - "Jules' Utility Class Extensions"
5 Copyright 2004-11 by Raw Material Software Ltd.
7 ------------------------------------------------------------------------------
9 JUCE can be redistributed and/or modified under the terms of the GNU General
10 Public License (Version 2), as published by the Free Software Foundation.
11 A copy of the license is included in the JUCE distribution, or can be found
12 online at www.gnu.org/licenses.
14 JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
18 ------------------------------------------------------------------------------
20 To release a closed-source product which uses JUCE, commercial licenses are
21 available: visit www.rawmaterialsoftware.com/juce for more information.
23 ==============================================================================
26 #include "../../../core/juce_StandardHeader.h"
28 BEGIN_JUCE_NAMESPACE
30 #include "juce_ComponentMovementWatcher.h"
31 #include "../../../containers/juce_ScopedValueSetter.h"
32 #include "../windows/juce_ComponentPeer.h"
35 //==============================================================================
36 ComponentMovementWatcher::ComponentMovementWatcher (Component* const component_)
37 : component (component_),
38 lastPeerID (0),
39 reentrant (false),
40 wasShowing (component_->isShowing())
42 jassert (component != nullptr); // can't use this with a null pointer..
44 component->addComponentListener (this);
46 registerWithParentComps();
49 ComponentMovementWatcher::~ComponentMovementWatcher()
51 if (component != nullptr)
52 component->removeComponentListener (this);
54 unregister();
57 //==============================================================================
58 void ComponentMovementWatcher::componentParentHierarchyChanged (Component&)
60 if (component != nullptr && ! reentrant)
62 const ScopedValueSetter<bool> setter (reentrant, true);
64 ComponentPeer* const peer = component->getPeer();
65 const uint32 peerID = peer != nullptr ? peer->getUniqueID() : 0;
67 if (peerID != lastPeerID)
69 componentPeerChanged();
71 if (component == nullptr)
72 return;
74 lastPeerID = peerID;
77 unregister();
78 registerWithParentComps();
80 componentMovedOrResized (*component, true, true);
82 if (component != nullptr)
83 componentVisibilityChanged (*component);
87 void ComponentMovementWatcher::componentMovedOrResized (Component&, bool wasMoved, bool wasResized)
89 if (component != nullptr)
91 if (wasMoved)
93 const Point<int> pos (component->getTopLevelComponent()->getLocalPoint (component, Point<int>()));
95 wasMoved = lastBounds.getPosition() != pos;
96 lastBounds.setPosition (pos);
99 wasResized = (lastBounds.getWidth() != component->getWidth() || lastBounds.getHeight() != component->getHeight());
100 lastBounds.setSize (component->getWidth(), component->getHeight());
102 if (wasMoved || wasResized)
103 componentMovedOrResized (wasMoved, wasResized);
107 void ComponentMovementWatcher::componentBeingDeleted (Component& comp)
109 registeredParentComps.removeValue (&comp);
111 if (component == &comp)
112 unregister();
115 void ComponentMovementWatcher::componentVisibilityChanged (Component&)
117 if (component != nullptr)
119 const bool isShowingNow = component->isShowing();
121 if (wasShowing != isShowingNow)
123 wasShowing = isShowingNow;
124 componentVisibilityChanged();
129 void ComponentMovementWatcher::registerWithParentComps()
131 Component* p = component->getParentComponent();
133 while (p != nullptr)
135 p->addComponentListener (this);
136 registeredParentComps.add (p);
137 p = p->getParentComponent();
141 void ComponentMovementWatcher::unregister()
143 for (int i = registeredParentComps.size(); --i >= 0;)
144 registeredParentComps.getUnchecked(i)->removeComponentListener (this);
146 registeredParentComps.clear();
150 END_JUCE_NAMESPACE