Audio plugin host https://kx.studio/carla
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.

128 lines
3.7KB

  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 UIViewComponent::Pimpl : public ComponentMovementWatcher
  18. {
  19. public:
  20. Pimpl (UIView* const v, Component& comp)
  21. : ComponentMovementWatcher (&comp),
  22. view (v),
  23. owner (comp),
  24. currentPeer (nullptr)
  25. {
  26. [view retain];
  27. if (owner.isShowing())
  28. componentPeerChanged();
  29. }
  30. ~Pimpl()
  31. {
  32. [view removeFromSuperview];
  33. [view release];
  34. }
  35. void componentMovedOrResized (bool /*wasMoved*/, bool /*wasResized*/) override
  36. {
  37. Component* const topComp = owner.getTopLevelComponent();
  38. if (topComp->getPeer() != nullptr)
  39. {
  40. const Point<int> pos (topComp->getLocalPoint (&owner, Point<int>()));
  41. [view setFrame: CGRectMake ((float) pos.x, (float) pos.y,
  42. (float) owner.getWidth(), (float) owner.getHeight())];
  43. }
  44. }
  45. void componentPeerChanged() override
  46. {
  47. ComponentPeer* const peer = owner.getPeer();
  48. if (currentPeer != peer)
  49. {
  50. if ([view superview] != nil)
  51. [view removeFromSuperview]; // Must be careful not to call this unless it's required - e.g. some Apple AU views
  52. // override the call and use it as a sign that they're being deleted, which breaks everything..
  53. currentPeer = peer;
  54. if (peer != nullptr)
  55. {
  56. UIView* peerView = (UIView*) peer->getNativeHandle();
  57. [peerView addSubview: view];
  58. componentMovedOrResized (false, false);
  59. }
  60. }
  61. [view setHidden: ! owner.isShowing()];
  62. }
  63. void componentVisibilityChanged() override
  64. {
  65. componentPeerChanged();
  66. }
  67. Rectangle<int> getViewBounds() const
  68. {
  69. CGRect r = [view frame];
  70. return Rectangle<int> ((int) r.size.width, (int) r.size.height);
  71. }
  72. UIView* const view;
  73. private:
  74. Component& owner;
  75. ComponentPeer* currentPeer;
  76. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Pimpl)
  77. };
  78. //==============================================================================
  79. UIViewComponent::UIViewComponent() {}
  80. UIViewComponent::~UIViewComponent() {}
  81. void UIViewComponent::setView (void* const view)
  82. {
  83. if (view != getView())
  84. {
  85. pimpl = nullptr;
  86. if (view != nullptr)
  87. pimpl = new Pimpl ((UIView*) view, *this);
  88. }
  89. }
  90. void* UIViewComponent::getView() const
  91. {
  92. return pimpl == nullptr ? nullptr : pimpl->view;
  93. }
  94. void UIViewComponent::resizeToFitView()
  95. {
  96. if (pimpl != nullptr)
  97. setBounds (pimpl->getViewBounds());
  98. }
  99. void UIViewComponent::paint (Graphics&) {}