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.

148 lines
4.6KB

  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. class NSViewComponent::Pimpl : public ComponentMovementWatcher
  19. {
  20. public:
  21. Pimpl (NSView* const view_, Component& owner_)
  22. : ComponentMovementWatcher (&owner_),
  23. view (view_),
  24. owner (owner_),
  25. currentPeer (nullptr)
  26. {
  27. [view_ retain];
  28. if (owner.isShowing())
  29. componentPeerChanged();
  30. }
  31. ~Pimpl()
  32. {
  33. removeFromParent();
  34. [view release];
  35. }
  36. void componentMovedOrResized (Component& comp, bool wasMoved, bool wasResized)
  37. {
  38. ComponentMovementWatcher::componentMovedOrResized (comp, wasMoved, wasResized);
  39. // The ComponentMovementWatcher version of this method avoids calling
  40. // us when the top-level comp is resized, but for an NSView we need to know this
  41. // because with inverted co-ords, we need to update the position even if the
  42. // top-left pos hasn't changed
  43. if (comp.isOnDesktop() && wasResized)
  44. componentMovedOrResized (wasMoved, wasResized);
  45. }
  46. void componentMovedOrResized (bool /*wasMoved*/, bool /*wasResized*/)
  47. {
  48. Component* const topComp = owner.getTopLevelComponent();
  49. if (topComp->getPeer() != nullptr)
  50. {
  51. const Point<int> pos (topComp->getLocalPoint (&owner, Point<int>()));
  52. NSRect r = NSMakeRect ((float) pos.getX(), (float) pos.getY(), (float) owner.getWidth(), (float) owner.getHeight());
  53. r.origin.y = [[view superview] frame].size.height - (r.origin.y + r.size.height);
  54. [view setFrame: r];
  55. }
  56. }
  57. void componentPeerChanged()
  58. {
  59. ComponentPeer* const peer = owner.getPeer();
  60. if (currentPeer != peer)
  61. {
  62. removeFromParent();
  63. currentPeer = peer;
  64. if (peer != nullptr)
  65. {
  66. NSView* peerView = (NSView*) peer->getNativeHandle();
  67. [peerView addSubview: view];
  68. componentMovedOrResized (false, false);
  69. }
  70. }
  71. [view setHidden: ! owner.isShowing()];
  72. }
  73. void componentVisibilityChanged()
  74. {
  75. componentPeerChanged();
  76. }
  77. Rectangle<int> getViewBounds() const
  78. {
  79. NSRect r = [view frame];
  80. return Rectangle<int> (0, 0, (int) r.size.width, (int) r.size.height);
  81. }
  82. NSView* const view;
  83. private:
  84. Component& owner;
  85. ComponentPeer* currentPeer;
  86. void removeFromParent()
  87. {
  88. if ([view superview] != nil)
  89. [view removeFromSuperview]; // Must be careful not to call this unless it's required - e.g. some Apple AU views
  90. // override the call and use it as a sign that they're being deleted, which breaks everything..
  91. }
  92. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Pimpl);
  93. };
  94. //==============================================================================
  95. NSViewComponent::NSViewComponent() {}
  96. NSViewComponent::~NSViewComponent() {}
  97. void NSViewComponent::setView (void* const view)
  98. {
  99. if (view != getView())
  100. {
  101. pimpl = nullptr;
  102. if (view != nullptr)
  103. pimpl = new Pimpl ((NSView*) view, *this);
  104. }
  105. }
  106. void* NSViewComponent::getView() const
  107. {
  108. return pimpl == nullptr ? nullptr : pimpl->view;
  109. }
  110. void NSViewComponent::resizeToFitView()
  111. {
  112. if (pimpl != nullptr)
  113. setBounds (pimpl->getViewBounds());
  114. }
  115. void NSViewComponent::paint (Graphics&) {}