Audio plugin host https://kx.studio/carla
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

juce_ComponentMovementWatcher.cpp 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. ComponentMovementWatcher::ComponentMovementWatcher (Component* const comp)
  18. : component (comp),
  19. lastPeerID (0),
  20. reentrant (false),
  21. wasShowing (comp->isShowing())
  22. {
  23. jassert (component != nullptr); // can't use this with a null pointer..
  24. component->addComponentListener (this);
  25. registerWithParentComps();
  26. }
  27. ComponentMovementWatcher::~ComponentMovementWatcher()
  28. {
  29. if (component != nullptr)
  30. component->removeComponentListener (this);
  31. unregister();
  32. }
  33. //==============================================================================
  34. void ComponentMovementWatcher::componentParentHierarchyChanged (Component&)
  35. {
  36. if (component != nullptr && ! reentrant)
  37. {
  38. const ScopedValueSetter<bool> setter (reentrant, true);
  39. ComponentPeer* const peer = component->getPeer();
  40. const uint32 peerID = peer != nullptr ? peer->getUniqueID() : 0;
  41. if (peerID != lastPeerID)
  42. {
  43. componentPeerChanged();
  44. if (component == nullptr)
  45. return;
  46. lastPeerID = peerID;
  47. }
  48. unregister();
  49. registerWithParentComps();
  50. componentMovedOrResized (*component, true, true);
  51. if (component != nullptr)
  52. componentVisibilityChanged (*component);
  53. }
  54. }
  55. void ComponentMovementWatcher::componentMovedOrResized (Component&, bool wasMoved, bool wasResized)
  56. {
  57. if (component != nullptr)
  58. {
  59. if (wasMoved)
  60. {
  61. const Point<int> pos (component->getTopLevelComponent()->getLocalPoint (component, Point<int>()));
  62. wasMoved = lastBounds.getPosition() != pos;
  63. lastBounds.setPosition (pos);
  64. }
  65. wasResized = (lastBounds.getWidth() != component->getWidth() || lastBounds.getHeight() != component->getHeight());
  66. lastBounds.setSize (component->getWidth(), component->getHeight());
  67. if (wasMoved || wasResized)
  68. componentMovedOrResized (wasMoved, wasResized);
  69. }
  70. }
  71. void ComponentMovementWatcher::componentBeingDeleted (Component& comp)
  72. {
  73. registeredParentComps.removeFirstMatchingValue (&comp);
  74. if (component == &comp)
  75. unregister();
  76. }
  77. void ComponentMovementWatcher::componentVisibilityChanged (Component&)
  78. {
  79. if (component != nullptr)
  80. {
  81. const bool isShowingNow = component->isShowing();
  82. if (wasShowing != isShowingNow)
  83. {
  84. wasShowing = isShowingNow;
  85. componentVisibilityChanged();
  86. }
  87. }
  88. }
  89. void ComponentMovementWatcher::registerWithParentComps()
  90. {
  91. for (Component* p = component->getParentComponent(); p != nullptr; p = p->getParentComponent())
  92. {
  93. p->addComponentListener (this);
  94. registeredParentComps.add (p);
  95. }
  96. }
  97. void ComponentMovementWatcher::unregister()
  98. {
  99. for (int i = registeredParentComps.size(); --i >= 0;)
  100. registeredParentComps.getUnchecked(i)->removeComponentListener (this);
  101. registeredParentComps.clear();
  102. }