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.

juce_win32_HWNDComponent.cpp 4.7KB

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