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.

129 lines
3.9KB

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