The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

144 lines
4.3KB

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