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"
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_
),
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);
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)
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)
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
)
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();
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();