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.

130 lines
3.6KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 5 End-User License
  8. Agreement and JUCE 5 Privacy Policy (both updated and effective as of the
  9. 27th April 2017).
  10. End User License Agreement: www.juce.com/juce-5-licence
  11. Privacy Policy: www.juce.com/juce-5-privacy-policy
  12. Or: You may also use this code under the terms of the GPL v3 (see
  13. www.gnu.org/licenses).
  14. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  15. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  16. DISCLAIMED.
  17. ==============================================================================
  18. */
  19. class UIViewComponent::Pimpl : public ComponentMovementWatcher
  20. {
  21. public:
  22. Pimpl (UIView* const v, Component& comp)
  23. : ComponentMovementWatcher (&comp),
  24. view (v),
  25. owner (comp),
  26. currentPeer (nullptr)
  27. {
  28. [view retain];
  29. if (owner.isShowing())
  30. componentPeerChanged();
  31. }
  32. ~Pimpl()
  33. {
  34. [view removeFromSuperview];
  35. [view release];
  36. }
  37. void componentMovedOrResized (bool /*wasMoved*/, bool /*wasResized*/) override
  38. {
  39. Component* const topComp = owner.getTopLevelComponent();
  40. if (topComp->getPeer() != nullptr)
  41. {
  42. const Point<int> pos (topComp->getLocalPoint (&owner, Point<int>()));
  43. [view setFrame: CGRectMake ((float) pos.x, (float) pos.y,
  44. (float) owner.getWidth(), (float) owner.getHeight())];
  45. }
  46. }
  47. void componentPeerChanged() override
  48. {
  49. ComponentPeer* const peer = owner.getPeer();
  50. if (currentPeer != peer)
  51. {
  52. if ([view superview] != nil)
  53. [view removeFromSuperview]; // Must be careful not to call this unless it's required - e.g. some Apple AU views
  54. // override the call and use it as a sign that they're being deleted, which breaks everything..
  55. currentPeer = peer;
  56. if (peer != nullptr)
  57. {
  58. UIView* peerView = (UIView*) peer->getNativeHandle();
  59. [peerView addSubview: view];
  60. componentMovedOrResized (false, false);
  61. }
  62. }
  63. [view setHidden: ! owner.isShowing()];
  64. }
  65. void componentVisibilityChanged() override
  66. {
  67. componentPeerChanged();
  68. }
  69. Rectangle<int> getViewBounds() const
  70. {
  71. CGRect r = [view frame];
  72. return Rectangle<int> ((int) r.size.width, (int) r.size.height);
  73. }
  74. UIView* const view;
  75. private:
  76. Component& owner;
  77. ComponentPeer* currentPeer;
  78. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Pimpl)
  79. };
  80. //==============================================================================
  81. UIViewComponent::UIViewComponent() {}
  82. UIViewComponent::~UIViewComponent() {}
  83. void UIViewComponent::setView (void* const view)
  84. {
  85. if (view != getView())
  86. {
  87. pimpl = nullptr;
  88. if (view != nullptr)
  89. pimpl = new Pimpl ((UIView*) view, *this);
  90. }
  91. }
  92. void* UIViewComponent::getView() const
  93. {
  94. return pimpl == nullptr ? nullptr : pimpl->view;
  95. }
  96. void UIViewComponent::resizeToFitView()
  97. {
  98. if (pimpl != nullptr)
  99. setBounds (pimpl->getViewBounds());
  100. }
  101. void UIViewComponent::paint (Graphics&) {}