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.

166 lines
4.2KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 6 technical preview.
  4. Copyright (c) 2020 - Raw Material Software Limited
  5. You may use this code under the terms of the GPL v3
  6. (see www.gnu.org/licenses).
  7. For this technical preview, this file is not subject to commercial licensing.
  8. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  9. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  10. DISCLAIMED.
  11. ==============================================================================
  12. */
  13. namespace juce
  14. {
  15. class HWNDComponent::Pimpl : public ComponentMovementWatcher
  16. {
  17. public:
  18. Pimpl (HWND h, Component& comp)
  19. : ComponentMovementWatcher (&comp),
  20. hwnd (h),
  21. owner (comp)
  22. {
  23. if (owner.isShowing())
  24. componentPeerChanged();
  25. }
  26. ~Pimpl() override
  27. {
  28. removeFromParent();
  29. DestroyWindow (hwnd);
  30. }
  31. using ComponentMovementWatcher::componentMovedOrResized;
  32. void componentMovedOrResized (bool wasMoved, bool wasResized) override
  33. {
  34. auto* topComponent = owner.getTopLevelComponent();
  35. if (auto* peer = owner.getPeer())
  36. {
  37. auto pos = topComponent->getLocalPoint (&owner, Point<int>());
  38. auto scaled = (Rectangle<int> (pos.x, pos.y, owner.getWidth(), owner.getHeight()).toDouble()
  39. * peer->getPlatformScaleFactor()).getSmallestIntegerContainer();
  40. DWORD windowFlags = SWP_NOACTIVATE | SWP_NOZORDER | SWP_NOOWNERZORDER;
  41. if (! wasMoved) windowFlags |= SWP_NOMOVE;
  42. if (! wasResized) windowFlags |= SWP_NOSIZE;
  43. SetWindowPos (hwnd, nullptr, scaled.getX(), scaled.getY(), scaled.getWidth(), scaled.getHeight(), windowFlags);
  44. }
  45. }
  46. void componentPeerChanged() override
  47. {
  48. auto* peer = owner.getPeer();
  49. if (currentPeer != peer)
  50. {
  51. removeFromParent();
  52. currentPeer = peer;
  53. addToParent();
  54. }
  55. auto isShowing = owner.isShowing();
  56. ShowWindow (hwnd, isShowing ? SW_SHOWNA : SW_HIDE);
  57. if (isShowing)
  58. InvalidateRect (hwnd, nullptr, 0);
  59. }
  60. using ComponentMovementWatcher::componentVisibilityChanged;
  61. void componentVisibilityChanged() override
  62. {
  63. componentPeerChanged();
  64. }
  65. void componentBroughtToFront (Component& comp) override
  66. {
  67. ComponentMovementWatcher::componentBroughtToFront (comp);
  68. }
  69. Rectangle<int> getHWNDBounds() const
  70. {
  71. if (auto* peer = owner.getPeer())
  72. {
  73. RECT r;
  74. GetWindowRect (hwnd, &r);
  75. return (Rectangle<int>::leftTopRightBottom (r.left, r.top, r.right, r.bottom).toDouble()
  76. / peer->getPlatformScaleFactor()).getSmallestIntegerContainer();
  77. }
  78. return {};
  79. }
  80. HWND hwnd;
  81. private:
  82. void addToParent()
  83. {
  84. if (currentPeer != nullptr)
  85. {
  86. auto windowFlags = GetWindowLongPtr (hwnd, -16);
  87. windowFlags &= ~(WS_POPUP | WS_CHILD);
  88. SetWindowLongPtr (hwnd, -16, windowFlags);
  89. SetParent (hwnd, (HWND) currentPeer->getNativeHandle());
  90. componentMovedOrResized (true, true);
  91. }
  92. }
  93. void removeFromParent()
  94. {
  95. SetParent (hwnd, NULL);
  96. }
  97. Component& owner;
  98. ComponentPeer* currentPeer = nullptr;
  99. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Pimpl)
  100. };
  101. //==============================================================================
  102. HWNDComponent::HWNDComponent()
  103. {
  104. }
  105. HWNDComponent::~HWNDComponent() {}
  106. void HWNDComponent::setHWND (void* hwnd)
  107. {
  108. if (hwnd != getHWND())
  109. {
  110. pimpl.reset();
  111. if (hwnd != nullptr)
  112. pimpl.reset (new Pimpl ((HWND) hwnd, *this));
  113. }
  114. }
  115. void* HWNDComponent::getHWND() const
  116. {
  117. return pimpl == nullptr ? nullptr : (void*) pimpl->hwnd;
  118. }
  119. void HWNDComponent::resizeToFit()
  120. {
  121. if (pimpl != nullptr)
  122. setBounds (pimpl->getHWNDBounds());
  123. }
  124. } // namespace juce