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 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. Point<int> newPos;
  62. Component* const top = component->getTopLevelComponent();
  63. if (top != component)
  64. newPos = top->getLocalPoint (component, Point<int>());
  65. else
  66. newPos = top->getPosition();
  67. wasMoved = lastBounds.getPosition() != newPos;
  68. lastBounds.setPosition (newPos);
  69. }
  70. wasResized = (lastBounds.getWidth() != component->getWidth() || lastBounds.getHeight() != component->getHeight());
  71. lastBounds.setSize (component->getWidth(), component->getHeight());
  72. if (wasMoved || wasResized)
  73. componentMovedOrResized (wasMoved, wasResized);
  74. }
  75. }
  76. void ComponentMovementWatcher::componentBeingDeleted (Component& comp)
  77. {
  78. registeredParentComps.removeFirstMatchingValue (&comp);
  79. if (component == &comp)
  80. unregister();
  81. }
  82. void ComponentMovementWatcher::componentVisibilityChanged (Component&)
  83. {
  84. if (component != nullptr)
  85. {
  86. const bool isShowingNow = component->isShowing();
  87. if (wasShowing != isShowingNow)
  88. {
  89. wasShowing = isShowingNow;
  90. componentVisibilityChanged();
  91. }
  92. }
  93. }
  94. void ComponentMovementWatcher::registerWithParentComps()
  95. {
  96. for (Component* p = component->getParentComponent(); p != nullptr; p = p->getParentComponent())
  97. {
  98. p->addComponentListener (this);
  99. registeredParentComps.add (p);
  100. }
  101. }
  102. void ComponentMovementWatcher::unregister()
  103. {
  104. for (int i = registeredParentComps.size(); --i >= 0;)
  105. registeredParentComps.getUnchecked(i)->removeComponentListener (this);
  106. registeredParentComps.clear();
  107. }