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.

138 lines
4.2KB

  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. ComponentMovementWatcher::ComponentMovementWatcher (Component* const component_)
  19. : component (component_),
  20. lastPeerID (0),
  21. reentrant (false),
  22. wasShowing (component_->isShowing())
  23. {
  24. jassert (component != nullptr); // can't use this with a null pointer..
  25. component->addComponentListener (this);
  26. registerWithParentComps();
  27. }
  28. ComponentMovementWatcher::~ComponentMovementWatcher()
  29. {
  30. if (component != nullptr)
  31. component->removeComponentListener (this);
  32. unregister();
  33. }
  34. //==============================================================================
  35. void ComponentMovementWatcher::componentParentHierarchyChanged (Component&)
  36. {
  37. if (component != nullptr && ! reentrant)
  38. {
  39. const ScopedValueSetter<bool> setter (reentrant, true);
  40. ComponentPeer* const peer = component->getPeer();
  41. const uint32 peerID = peer != nullptr ? peer->getUniqueID() : 0;
  42. if (peerID != lastPeerID)
  43. {
  44. componentPeerChanged();
  45. if (component == nullptr)
  46. return;
  47. lastPeerID = peerID;
  48. }
  49. unregister();
  50. registerWithParentComps();
  51. componentMovedOrResized (*component, true, true);
  52. if (component != nullptr)
  53. componentVisibilityChanged (*component);
  54. }
  55. }
  56. void ComponentMovementWatcher::componentMovedOrResized (Component&, bool wasMoved, bool wasResized)
  57. {
  58. if (component != nullptr)
  59. {
  60. if (wasMoved)
  61. {
  62. const Point<int> pos (component->getTopLevelComponent()->getLocalPoint (component, Point<int>()));
  63. wasMoved = lastBounds.getPosition() != pos;
  64. lastBounds.setPosition (pos);
  65. }
  66. wasResized = (lastBounds.getWidth() != component->getWidth() || lastBounds.getHeight() != component->getHeight());
  67. lastBounds.setSize (component->getWidth(), component->getHeight());
  68. if (wasMoved || wasResized)
  69. componentMovedOrResized (wasMoved, wasResized);
  70. }
  71. }
  72. void ComponentMovementWatcher::componentBeingDeleted (Component& comp)
  73. {
  74. registeredParentComps.removeFirstMatchingValue (&comp);
  75. if (component == &comp)
  76. unregister();
  77. }
  78. void ComponentMovementWatcher::componentVisibilityChanged (Component&)
  79. {
  80. if (component != nullptr)
  81. {
  82. const bool isShowingNow = component->isShowing();
  83. if (wasShowing != isShowingNow)
  84. {
  85. wasShowing = isShowingNow;
  86. componentVisibilityChanged();
  87. }
  88. }
  89. }
  90. void ComponentMovementWatcher::registerWithParentComps()
  91. {
  92. Component* p = component->getParentComponent();
  93. while (p != nullptr)
  94. {
  95. p->addComponentListener (this);
  96. registeredParentComps.add (p);
  97. p = p->getParentComponent();
  98. }
  99. }
  100. void ComponentMovementWatcher::unregister()
  101. {
  102. for (int i = registeredParentComps.size(); --i >= 0;)
  103. registeredParentComps.getUnchecked(i)->removeComponentListener (this);
  104. registeredParentComps.clear();
  105. }