|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172 |
- /*
- ==============================================================================
-
- This file is part of the JUCE library.
- Copyright (c) 2020 - Raw Material Software Limited
-
- JUCE is an open source library subject to commercial or open-source
- licensing.
-
- By using JUCE, you agree to the terms of both the JUCE 6 End-User License
- Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020).
-
- End User License Agreement: www.juce.com/juce-6-licence
- Privacy Policy: www.juce.com/juce-privacy-policy
-
- Or: You may also use this code under the terms of the GPL v3 (see
- www.gnu.org/licenses).
-
- JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
- EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
- DISCLAIMED.
-
- ==============================================================================
- */
-
- namespace juce
- {
-
- class HWNDComponent::Pimpl : public ComponentMovementWatcher
- {
- public:
- Pimpl (HWND h, Component& comp)
- : ComponentMovementWatcher (&comp),
- hwnd (h),
- owner (comp)
- {
- if (owner.isShowing())
- componentPeerChanged();
- }
-
- ~Pimpl() override
- {
- removeFromParent();
- DestroyWindow (hwnd);
- }
-
- using ComponentMovementWatcher::componentMovedOrResized;
-
- void componentMovedOrResized (bool wasMoved, bool wasResized) override
- {
- auto* topComponent = owner.getTopLevelComponent();
-
- if (auto* peer = owner.getPeer())
- {
- auto pos = topComponent->getLocalPoint (&owner, Point<int>());
-
- auto scaled = (Rectangle<int> (pos.x, pos.y, owner.getWidth(), owner.getHeight()).toDouble()
- * peer->getPlatformScaleFactor()).getSmallestIntegerContainer();
-
- DWORD windowFlags = SWP_NOACTIVATE | SWP_NOZORDER | SWP_NOOWNERZORDER;
- if (! wasMoved) windowFlags |= SWP_NOMOVE;
- if (! wasResized) windowFlags |= SWP_NOSIZE;
-
- SetWindowPos (hwnd, nullptr, scaled.getX(), scaled.getY(), scaled.getWidth(), scaled.getHeight(), windowFlags);
- }
- }
-
- void componentPeerChanged() override
- {
- auto* peer = owner.getPeer();
-
- if (currentPeer != peer)
- {
- removeFromParent();
- currentPeer = peer;
-
- addToParent();
- }
-
- auto isShowing = owner.isShowing();
-
- ShowWindow (hwnd, isShowing ? SW_SHOWNA : SW_HIDE);
-
- if (isShowing)
- InvalidateRect (hwnd, nullptr, 0);
- }
-
- using ComponentMovementWatcher::componentVisibilityChanged;
-
- void componentVisibilityChanged() override
- {
- componentPeerChanged();
- }
-
- void componentBroughtToFront (Component& comp) override
- {
- ComponentMovementWatcher::componentBroughtToFront (comp);
- }
-
- Rectangle<int> getHWNDBounds() const
- {
- if (auto* peer = owner.getPeer())
- {
- RECT r;
- GetWindowRect (hwnd, &r);
-
- return (Rectangle<int>::leftTopRightBottom (r.left, r.top, r.right, r.bottom).toDouble()
- / peer->getPlatformScaleFactor()).getSmallestIntegerContainer();
- }
-
- return {};
- }
-
- HWND hwnd;
-
- private:
- void addToParent()
- {
- if (currentPeer != nullptr)
- {
- auto windowFlags = GetWindowLongPtr (hwnd, -16);
-
- windowFlags &= ~(WS_POPUP | WS_CHILD);
-
- SetWindowLongPtr (hwnd, -16, windowFlags);
- SetParent (hwnd, (HWND) currentPeer->getNativeHandle());
-
- componentMovedOrResized (true, true);
- }
- }
-
- void removeFromParent()
- {
- SetParent (hwnd, NULL);
- }
-
- Component& owner;
- ComponentPeer* currentPeer = nullptr;
-
- JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (Pimpl)
- };
-
- //==============================================================================
- HWNDComponent::HWNDComponent()
- {
- }
-
- HWNDComponent::~HWNDComponent() {}
-
- void HWNDComponent::setHWND (void* hwnd)
- {
- if (hwnd != getHWND())
- {
- pimpl.reset();
-
- if (hwnd != nullptr)
- pimpl.reset (new Pimpl ((HWND) hwnd, *this));
- }
- }
-
- void* HWNDComponent::getHWND() const
- {
- return pimpl == nullptr ? nullptr : (void*) pimpl->hwnd;
- }
-
- void HWNDComponent::resizeToFit()
- {
- if (pimpl != nullptr)
- setBounds (pimpl->getHWNDBounds());
- }
-
- } // namespace juce
|