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.

175 lines
4.5KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 7 technical preview.
  4. Copyright (c) 2022 - 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 the technical preview this file cannot be licensed commercially.
  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. void componentMovedOrResized (bool wasMoved, bool wasResized) override
  32. {
  33. if (auto* peer = owner.getTopLevelComponent()->getPeer())
  34. {
  35. auto area = (peer->getAreaCoveredBy (owner).toFloat() * peer->getPlatformScaleFactor()).getSmallestIntegerContainer();
  36. UINT flagsToSend = SWP_NOACTIVATE | SWP_NOZORDER | SWP_NOOWNERZORDER;
  37. if (! wasMoved) flagsToSend |= SWP_NOMOVE;
  38. if (! wasResized) flagsToSend |= SWP_NOSIZE;
  39. ScopedThreadDPIAwarenessSetter threadDpiAwarenessSetter { hwnd };
  40. SetWindowPos (hwnd, nullptr, area.getX(), area.getY(), area.getWidth(), area.getHeight(), flagsToSend);
  41. }
  42. }
  43. using ComponentMovementWatcher::componentMovedOrResized;
  44. void componentPeerChanged() override
  45. {
  46. auto* peer = owner.getPeer();
  47. if (currentPeer != peer)
  48. {
  49. removeFromParent();
  50. currentPeer = peer;
  51. addToParent();
  52. }
  53. auto isShowing = owner.isShowing();
  54. ShowWindow (hwnd, isShowing ? SW_SHOWNA : SW_HIDE);
  55. if (isShowing)
  56. InvalidateRect (hwnd, nullptr, 0);
  57. }
  58. void componentVisibilityChanged() override
  59. {
  60. componentPeerChanged();
  61. }
  62. using ComponentMovementWatcher::componentVisibilityChanged;
  63. void componentBroughtToFront (Component& comp) override
  64. {
  65. ComponentMovementWatcher::componentBroughtToFront (comp);
  66. }
  67. Rectangle<int> getHWNDBounds() const
  68. {
  69. if (auto* peer = owner.getPeer())
  70. {
  71. ScopedThreadDPIAwarenessSetter threadDpiAwarenessSetter { hwnd };
  72. RECT r;
  73. GetWindowRect (hwnd, &r);
  74. Rectangle<int> windowRectangle (r.right - r.left, r.bottom - r.top);
  75. return (windowRectangle.toFloat() / peer->getPlatformScaleFactor()).toNearestInt();
  76. }
  77. return {};
  78. }
  79. HWND hwnd;
  80. private:
  81. void addToParent()
  82. {
  83. if (currentPeer != nullptr)
  84. {
  85. auto windowFlags = GetWindowLongPtr (hwnd, -16);
  86. using FlagType = decltype (windowFlags);
  87. windowFlags &= ~(FlagType) WS_POPUP;
  88. windowFlags |= (FlagType) WS_CHILD;
  89. SetWindowLongPtr (hwnd, -16, windowFlags);
  90. SetParent (hwnd, (HWND) currentPeer->getNativeHandle());
  91. componentMovedOrResized (true, true);
  92. }
  93. }
  94. void removeFromParent()
  95. {
  96. ShowWindow (hwnd, SW_HIDE);
  97. SetParent (hwnd, nullptr);
  98. }
  99. Component& owner;
  100. ComponentPeer* currentPeer = nullptr;
  101. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Pimpl)
  102. };
  103. //==============================================================================
  104. HWNDComponent::HWNDComponent() {}
  105. HWNDComponent::~HWNDComponent() {}
  106. void HWNDComponent::paint (Graphics&) {}
  107. void HWNDComponent::setHWND (void* hwnd)
  108. {
  109. if (hwnd != getHWND())
  110. {
  111. pimpl.reset();
  112. if (hwnd != nullptr)
  113. pimpl.reset (new Pimpl ((HWND) hwnd, *this));
  114. }
  115. }
  116. void* HWNDComponent::getHWND() const
  117. {
  118. return pimpl == nullptr ? nullptr : (void*) pimpl->hwnd;
  119. }
  120. void HWNDComponent::resizeToFit()
  121. {
  122. if (pimpl != nullptr)
  123. setBounds (pimpl->getHWNDBounds());
  124. }
  125. void HWNDComponent::updateHWNDBounds()
  126. {
  127. if (pimpl != nullptr)
  128. pimpl->componentMovedOrResized (true, true);
  129. }
  130. } // namespace juce