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.

176 lines
4.5KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - 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 6 End-User License
  8. Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020).
  9. End User License Agreement: www.juce.com/juce-6-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. void setThreadDPIAwarenessForWindow (HWND);
  21. class HWNDComponent::Pimpl : public ComponentMovementWatcher
  22. {
  23. public:
  24. Pimpl (HWND h, Component& comp)
  25. : ComponentMovementWatcher (&comp),
  26. hwnd (h),
  27. owner (comp)
  28. {
  29. if (owner.isShowing())
  30. componentPeerChanged();
  31. }
  32. ~Pimpl() override
  33. {
  34. removeFromParent();
  35. DestroyWindow (hwnd);
  36. }
  37. void componentMovedOrResized (bool wasMoved, bool wasResized) override
  38. {
  39. if (auto* peer = owner.getTopLevelComponent()->getPeer())
  40. {
  41. auto area = (peer->getAreaCoveredBy (owner).toFloat() * peer->getPlatformScaleFactor()).getSmallestIntegerContainer();
  42. setThreadDPIAwarenessForWindow (hwnd);
  43. UINT flagsToSend = SWP_NOACTIVATE | SWP_NOZORDER | SWP_NOOWNERZORDER;
  44. if (! wasMoved) flagsToSend |= SWP_NOMOVE;
  45. if (! wasResized) flagsToSend |= SWP_NOSIZE;
  46. SetWindowPos (hwnd, nullptr, area.getX(), area.getY(), area.getWidth(), area.getHeight(), flagsToSend);
  47. }
  48. }
  49. using ComponentMovementWatcher::componentMovedOrResized;
  50. void componentPeerChanged() override
  51. {
  52. auto* peer = owner.getPeer();
  53. if (currentPeer != peer)
  54. {
  55. removeFromParent();
  56. currentPeer = peer;
  57. addToParent();
  58. }
  59. auto isShowing = owner.isShowing();
  60. ShowWindow (hwnd, isShowing ? SW_SHOWNA : SW_HIDE);
  61. if (isShowing)
  62. InvalidateRect (hwnd, nullptr, 0);
  63. }
  64. void componentVisibilityChanged() override
  65. {
  66. componentPeerChanged();
  67. }
  68. using ComponentMovementWatcher::componentVisibilityChanged;
  69. void componentBroughtToFront (Component& comp) override
  70. {
  71. ComponentMovementWatcher::componentBroughtToFront (comp);
  72. }
  73. Rectangle<int> getHWNDBounds() const
  74. {
  75. if (auto* peer = owner.getPeer())
  76. {
  77. setThreadDPIAwarenessForWindow (hwnd);
  78. RECT r;
  79. GetWindowRect (hwnd, &r);
  80. Rectangle<int> windowRectangle (r.right - r.left, r.bottom - r.top);
  81. return (windowRectangle.toFloat() / peer->getPlatformScaleFactor()).toNearestInt();
  82. }
  83. return {};
  84. }
  85. HWND hwnd;
  86. private:
  87. void addToParent()
  88. {
  89. if (currentPeer != nullptr)
  90. {
  91. auto windowFlags = GetWindowLongPtr (hwnd, -16);
  92. windowFlags &= ~WS_POPUP;
  93. windowFlags |= 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, NULL);
  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. } // namespace juce