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.

149 lines
4.3KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-10 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. #include "../../../core/juce_StandardHeader.h"
  19. BEGIN_JUCE_NAMESPACE
  20. #include "juce_ComponentMovementWatcher.h"
  21. #include "../../../containers/juce_ScopedValueSetter.h"
  22. //==============================================================================
  23. ComponentMovementWatcher::ComponentMovementWatcher (Component* const component_)
  24. : component (component_),
  25. lastPeer (0),
  26. reentrant (false),
  27. wasShowing (component_->isShowing())
  28. {
  29. jassert (component != 0); // can't use this with a null pointer..
  30. component->addComponentListener (this);
  31. registerWithParentComps();
  32. }
  33. ComponentMovementWatcher::~ComponentMovementWatcher()
  34. {
  35. if (component != 0)
  36. component->removeComponentListener (this);
  37. unregister();
  38. }
  39. //==============================================================================
  40. void ComponentMovementWatcher::componentParentHierarchyChanged (Component&)
  41. {
  42. if (component != 0 && ! reentrant)
  43. {
  44. const ScopedValueSetter<bool> setter (reentrant, true);
  45. ComponentPeer* const peer = component->getPeer();
  46. if (peer != lastPeer)
  47. {
  48. componentPeerChanged();
  49. if (component == 0)
  50. return;
  51. lastPeer = peer;
  52. }
  53. unregister();
  54. registerWithParentComps();
  55. componentMovedOrResized (*component, true, true);
  56. if (component != 0)
  57. componentVisibilityChanged (*component);
  58. }
  59. }
  60. void ComponentMovementWatcher::componentMovedOrResized (Component&, bool wasMoved, bool wasResized)
  61. {
  62. if (component != 0)
  63. {
  64. if (wasMoved)
  65. {
  66. const Point<int> pos (component->getTopLevelComponent()->getLocalPoint (component, Point<int>()));
  67. wasMoved = lastBounds.getPosition() != pos;
  68. lastBounds.setPosition (pos);
  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.removeValue (&comp);
  79. if (component == &comp)
  80. unregister();
  81. }
  82. void ComponentMovementWatcher::componentVisibilityChanged (Component&)
  83. {
  84. if (component != 0)
  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. Component* p = component->getParentComponent();
  97. while (p != 0)
  98. {
  99. p->addComponentListener (this);
  100. registeredParentComps.add (p);
  101. p = p->getParentComponent();
  102. }
  103. }
  104. void ComponentMovementWatcher::unregister()
  105. {
  106. for (int i = registeredParentComps.size(); --i >= 0;)
  107. registeredParentComps.getUnchecked(i)->removeComponentListener (this);
  108. registeredParentComps.clear();
  109. }
  110. END_JUCE_NAMESPACE