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.

146 lines
4.6KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. class NSViewAttachment : public ReferenceCountedObject,
  18. public ComponentMovementWatcher
  19. {
  20. public:
  21. NSViewAttachment (NSView* const v, Component& comp)
  22. : ComponentMovementWatcher (&comp),
  23. view (v),
  24. owner (comp),
  25. currentPeer (nullptr)
  26. {
  27. [view retain];
  28. if (owner.isShowing())
  29. componentPeerChanged();
  30. }
  31. ~NSViewAttachment()
  32. {
  33. removeFromParent();
  34. [view release];
  35. }
  36. void componentMovedOrResized (Component& comp, bool wasMoved, bool wasResized) override
  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 coordinates, 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*/) override
  47. {
  48. if (ComponentPeer* const peer = owner.getTopLevelComponent()->getPeer())
  49. {
  50. NSRect r = makeNSRect (peer->getAreaCoveredBy (owner));
  51. r.origin.y = [[view superview] frame].size.height - (r.origin.y + r.size.height);
  52. [view setFrame: r];
  53. }
  54. }
  55. void componentPeerChanged() override
  56. {
  57. ComponentPeer* const peer = owner.getPeer();
  58. if (currentPeer != peer)
  59. {
  60. removeFromParent();
  61. currentPeer = peer;
  62. if (peer != nullptr)
  63. {
  64. NSView* const peerView = (NSView*) peer->getNativeHandle();
  65. [peerView addSubview: view];
  66. componentMovedOrResized (false, false);
  67. }
  68. }
  69. [view setHidden: ! owner.isShowing()];
  70. }
  71. void componentVisibilityChanged() override
  72. {
  73. componentPeerChanged();
  74. }
  75. NSView* const view;
  76. private:
  77. Component& owner;
  78. ComponentPeer* currentPeer;
  79. void removeFromParent()
  80. {
  81. if ([view superview] != nil)
  82. [view removeFromSuperview]; // Must be careful not to call this unless it's required - e.g. some Apple AU views
  83. // override the call and use it as a sign that they're being deleted, which breaks everything..
  84. }
  85. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (NSViewAttachment)
  86. };
  87. //==============================================================================
  88. NSViewComponent::NSViewComponent() {}
  89. NSViewComponent::~NSViewComponent() {}
  90. void NSViewComponent::setView (void* const view)
  91. {
  92. if (view != getView())
  93. {
  94. attachment = nullptr;
  95. if (view != nullptr)
  96. attachment = attachViewToComponent (*this, view);
  97. }
  98. }
  99. void* NSViewComponent::getView() const
  100. {
  101. return attachment != nullptr ? static_cast <NSViewAttachment*> (attachment.get())->view
  102. : nullptr;
  103. }
  104. void NSViewComponent::resizeToFitView()
  105. {
  106. if (attachment != nullptr)
  107. {
  108. NSRect r = [static_cast <NSViewAttachment*> (attachment.get())->view frame];
  109. setBounds (Rectangle<int> ((int) r.size.width, (int) r.size.height));
  110. }
  111. }
  112. void NSViewComponent::paint (Graphics&) {}
  113. ReferenceCountedObject* NSViewComponent::attachViewToComponent (Component& comp, void* const view)
  114. {
  115. return new NSViewAttachment ((NSView*) view, comp);
  116. }