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.

172 lines
4.4KB

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