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.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. 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. using ComponentMovementWatcher::componentMovedOrResized;
  37. void componentMovedOrResized (bool wasMoved, bool wasResized) override
  38. {
  39. auto* topComponent = owner.getTopLevelComponent();
  40. if (auto* peer = owner.getPeer())
  41. {
  42. auto pos = topComponent->getLocalPoint (&owner, Point<int>());
  43. auto scaled = (Rectangle<int> (pos.x, pos.y, owner.getWidth(), owner.getHeight()).toDouble()
  44. * peer->getPlatformScaleFactor()).getSmallestIntegerContainer();
  45. DWORD windowFlags = SWP_NOACTIVATE | SWP_NOZORDER | SWP_NOOWNERZORDER;
  46. if (! wasMoved) windowFlags |= SWP_NOMOVE;
  47. if (! wasResized) windowFlags |= SWP_NOSIZE;
  48. SetWindowPos (hwnd, nullptr, scaled.getX(), scaled.getY(), scaled.getWidth(), scaled.getHeight(), windowFlags);
  49. }
  50. }
  51. void componentPeerChanged() override
  52. {
  53. auto* peer = owner.getPeer();
  54. if (currentPeer != peer)
  55. {
  56. removeFromParent();
  57. currentPeer = peer;
  58. addToParent();
  59. }
  60. auto isShowing = owner.isShowing();
  61. ShowWindow (hwnd, isShowing ? SW_SHOWNA : SW_HIDE);
  62. if (isShowing)
  63. InvalidateRect (hwnd, nullptr, 0);
  64. }
  65. using ComponentMovementWatcher::componentVisibilityChanged;
  66. void componentVisibilityChanged() override
  67. {
  68. componentPeerChanged();
  69. }
  70. void componentBroughtToFront (Component& comp) override
  71. {
  72. ComponentMovementWatcher::componentBroughtToFront (comp);
  73. }
  74. Rectangle<int> getHWNDBounds() const
  75. {
  76. if (auto* peer = owner.getPeer())
  77. {
  78. RECT r;
  79. GetWindowRect (hwnd, &r);
  80. return (Rectangle<int>::leftTopRightBottom (r.left, r.top, r.right, r.bottom).toDouble()
  81. / peer->getPlatformScaleFactor()).getSmallestIntegerContainer();
  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 | WS_CHILD);
  93. SetWindowLongPtr (hwnd, -16, windowFlags);
  94. SetParent (hwnd, (HWND) currentPeer->getNativeHandle());
  95. componentMovedOrResized (true, true);
  96. }
  97. }
  98. void removeFromParent()
  99. {
  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. {
  109. }
  110. HWNDComponent::~HWNDComponent() {}
  111. void HWNDComponent::setHWND (void* hwnd)
  112. {
  113. if (hwnd != getHWND())
  114. {
  115. pimpl.reset();
  116. if (hwnd != nullptr)
  117. pimpl.reset (new Pimpl ((HWND) hwnd, *this));
  118. }
  119. }
  120. void* HWNDComponent::getHWND() const
  121. {
  122. return pimpl == nullptr ? nullptr : (void*) pimpl->hwnd;
  123. }
  124. void HWNDComponent::resizeToFit()
  125. {
  126. if (pimpl != nullptr)
  127. setBounds (pimpl->getHWNDBounds());
  128. }
  129. } // namespace juce