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.

126 lines
3.8KB

  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. //==============================================================================
  22. ComponentMovementWatcher::ComponentMovementWatcher (Component* const component_)
  23. : component (component_),
  24. lastPeer (0),
  25. reentrant (false)
  26. {
  27. jassert (component != 0); // can't use this with a null pointer..
  28. component->addComponentListener (this);
  29. registerWithParentComps();
  30. }
  31. ComponentMovementWatcher::~ComponentMovementWatcher()
  32. {
  33. component->removeComponentListener (this);
  34. unregister();
  35. }
  36. //==============================================================================
  37. void ComponentMovementWatcher::componentParentHierarchyChanged (Component&)
  38. {
  39. // agh! don't delete the target component without deleting this object first!
  40. jassert (component != 0);
  41. if (! reentrant)
  42. {
  43. reentrant = true;
  44. ComponentPeer* const peer = component->getPeer();
  45. if (peer != lastPeer)
  46. {
  47. componentPeerChanged();
  48. if (component == 0)
  49. return;
  50. lastPeer = peer;
  51. }
  52. unregister();
  53. registerWithParentComps();
  54. reentrant = false;
  55. componentMovedOrResized (*component, true, true);
  56. }
  57. }
  58. void ComponentMovementWatcher::componentMovedOrResized (Component&, bool wasMoved, bool wasResized)
  59. {
  60. // agh! don't delete the target component without deleting this object first!
  61. jassert (component != 0);
  62. if (wasMoved)
  63. {
  64. const Point<int> pos (component->relativePositionToOtherComponent (component->getTopLevelComponent(), 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. void ComponentMovementWatcher::registerWithParentComps()
  74. {
  75. Component* p = component->getParentComponent();
  76. while (p != 0)
  77. {
  78. p->addComponentListener (this);
  79. registeredParentComps.add (p);
  80. p = p->getParentComponent();
  81. }
  82. }
  83. void ComponentMovementWatcher::unregister()
  84. {
  85. for (int i = registeredParentComps.size(); --i >= 0;)
  86. registeredParentComps.getUnchecked(i)->removeComponentListener (this);
  87. registeredParentComps.clear();
  88. }
  89. END_JUCE_NAMESPACE