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.

491 lines
19KB

  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. extern int64 getMouseEventTime();
  21. JUCE_DECLARE_UUID_GETTER (IOleObject, "00000112-0000-0000-C000-000000000046")
  22. JUCE_DECLARE_UUID_GETTER (IOleWindow, "00000114-0000-0000-C000-000000000046")
  23. JUCE_DECLARE_UUID_GETTER (IOleInPlaceSite, "00000119-0000-0000-C000-000000000046")
  24. namespace ActiveXHelpers
  25. {
  26. //==============================================================================
  27. struct JuceIStorage : public ComBaseClassHelper<IStorage>
  28. {
  29. JuceIStorage() {}
  30. JUCE_COMRESULT CreateStream (const WCHAR*, DWORD, DWORD, DWORD, IStream**) { return E_NOTIMPL; }
  31. JUCE_COMRESULT OpenStream (const WCHAR*, void*, DWORD, DWORD, IStream**) { return E_NOTIMPL; }
  32. JUCE_COMRESULT CreateStorage (const WCHAR*, DWORD, DWORD, DWORD, IStorage**) { return E_NOTIMPL; }
  33. JUCE_COMRESULT OpenStorage (const WCHAR*, IStorage*, DWORD, SNB, DWORD, IStorage**) { return E_NOTIMPL; }
  34. JUCE_COMRESULT CopyTo (DWORD, IID const*, SNB, IStorage*) { return E_NOTIMPL; }
  35. JUCE_COMRESULT MoveElementTo (const OLECHAR*,IStorage*, const OLECHAR*, DWORD) { return E_NOTIMPL; }
  36. JUCE_COMRESULT Commit (DWORD) { return E_NOTIMPL; }
  37. JUCE_COMRESULT Revert() { return E_NOTIMPL; }
  38. JUCE_COMRESULT EnumElements (DWORD, void*, DWORD, IEnumSTATSTG**) { return E_NOTIMPL; }
  39. JUCE_COMRESULT DestroyElement (const OLECHAR*) { return E_NOTIMPL; }
  40. JUCE_COMRESULT RenameElement (const WCHAR*, const WCHAR*) { return E_NOTIMPL; }
  41. JUCE_COMRESULT SetElementTimes (const WCHAR*, FILETIME const*, FILETIME const*, FILETIME const*) { return E_NOTIMPL; }
  42. JUCE_COMRESULT SetClass (REFCLSID) { return S_OK; }
  43. JUCE_COMRESULT SetStateBits (DWORD, DWORD) { return E_NOTIMPL; }
  44. JUCE_COMRESULT Stat (STATSTG*, DWORD) { return E_NOTIMPL; }
  45. };
  46. //==============================================================================
  47. struct JuceOleInPlaceFrame : public ComBaseClassHelper<IOleInPlaceFrame>
  48. {
  49. JuceOleInPlaceFrame (HWND hwnd) : window (hwnd) {}
  50. JUCE_COMRESULT GetWindow (HWND* lphwnd) { *lphwnd = window; return S_OK; }
  51. JUCE_COMRESULT ContextSensitiveHelp (BOOL) { return E_NOTIMPL; }
  52. JUCE_COMRESULT GetBorder (LPRECT) { return E_NOTIMPL; }
  53. JUCE_COMRESULT RequestBorderSpace (LPCBORDERWIDTHS) { return E_NOTIMPL; }
  54. JUCE_COMRESULT SetBorderSpace (LPCBORDERWIDTHS) { return E_NOTIMPL; }
  55. JUCE_COMRESULT SetActiveObject (IOleInPlaceActiveObject* a, LPCOLESTR) { activeObject = a; return S_OK; }
  56. JUCE_COMRESULT InsertMenus (HMENU, LPOLEMENUGROUPWIDTHS) { return E_NOTIMPL; }
  57. JUCE_COMRESULT SetMenu (HMENU, HOLEMENU, HWND) { return S_OK; }
  58. JUCE_COMRESULT RemoveMenus (HMENU) { return E_NOTIMPL; }
  59. JUCE_COMRESULT SetStatusText (LPCOLESTR) { return S_OK; }
  60. JUCE_COMRESULT EnableModeless (BOOL) { return S_OK; }
  61. JUCE_COMRESULT TranslateAccelerator (LPMSG, WORD) { return E_NOTIMPL; }
  62. HRESULT OfferKeyTranslation (LPMSG lpmsg)
  63. {
  64. if (activeObject != nullptr)
  65. return activeObject->TranslateAcceleratorW (lpmsg);
  66. return S_FALSE;
  67. }
  68. HWND window;
  69. ComSmartPtr<IOleInPlaceActiveObject> activeObject;
  70. };
  71. //==============================================================================
  72. struct JuceIOleInPlaceSite : public ComBaseClassHelper<IOleInPlaceSite>
  73. {
  74. JuceIOleInPlaceSite (HWND hwnd)
  75. : window (hwnd),
  76. frame (new JuceOleInPlaceFrame (window))
  77. {}
  78. ~JuceIOleInPlaceSite()
  79. {
  80. frame->Release();
  81. }
  82. JUCE_COMRESULT GetWindow (HWND* lphwnd) { *lphwnd = window; return S_OK; }
  83. JUCE_COMRESULT ContextSensitiveHelp (BOOL) { return E_NOTIMPL; }
  84. JUCE_COMRESULT CanInPlaceActivate() { return S_OK; }
  85. JUCE_COMRESULT OnInPlaceActivate() { return S_OK; }
  86. JUCE_COMRESULT OnUIActivate() { return S_OK; }
  87. JUCE_COMRESULT GetWindowContext (LPOLEINPLACEFRAME* lplpFrame, LPOLEINPLACEUIWINDOW* lplpDoc, LPRECT, LPRECT, LPOLEINPLACEFRAMEINFO lpFrameInfo)
  88. {
  89. /* Note: If you call AddRef on the frame here, then some types of object (e.g. web browser control) cause leaks..
  90. If you don't call AddRef then others crash (e.g. QuickTime).. Bit of a catch-22, so letting it leak is probably preferable.
  91. */
  92. if (lplpFrame != nullptr) { frame->AddRef(); *lplpFrame = frame; }
  93. if (lplpDoc != nullptr) *lplpDoc = nullptr;
  94. lpFrameInfo->fMDIApp = FALSE;
  95. lpFrameInfo->hwndFrame = window;
  96. lpFrameInfo->haccel = nullptr;
  97. lpFrameInfo->cAccelEntries = 0;
  98. return S_OK;
  99. }
  100. JUCE_COMRESULT Scroll (SIZE) { return E_NOTIMPL; }
  101. JUCE_COMRESULT OnUIDeactivate (BOOL) { return S_OK; }
  102. JUCE_COMRESULT OnInPlaceDeactivate() { return S_OK; }
  103. JUCE_COMRESULT DiscardUndoState() { return E_NOTIMPL; }
  104. JUCE_COMRESULT DeactivateAndUndo() { return E_NOTIMPL; }
  105. JUCE_COMRESULT OnPosRectChange (LPCRECT) { return S_OK; }
  106. LRESULT offerEventToActiveXControl (::MSG& msg)
  107. {
  108. if (frame != nullptr)
  109. return frame->OfferKeyTranslation (&msg);
  110. return S_FALSE;
  111. }
  112. HWND window;
  113. JuceOleInPlaceFrame* frame;
  114. };
  115. //==============================================================================
  116. struct JuceIOleClientSite : public ComBaseClassHelper<IOleClientSite>
  117. {
  118. JuceIOleClientSite (HWND window) : inplaceSite (new JuceIOleInPlaceSite (window))
  119. {}
  120. ~JuceIOleClientSite()
  121. {
  122. inplaceSite->Release();
  123. }
  124. JUCE_COMRESULT QueryInterface (REFIID type, void** result)
  125. {
  126. if (type == __uuidof (IOleInPlaceSite))
  127. {
  128. inplaceSite->AddRef();
  129. *result = static_cast<IOleInPlaceSite*> (inplaceSite);
  130. return S_OK;
  131. }
  132. return ComBaseClassHelper <IOleClientSite>::QueryInterface (type, result);
  133. }
  134. JUCE_COMRESULT SaveObject() { return E_NOTIMPL; }
  135. JUCE_COMRESULT GetMoniker (DWORD, DWORD, IMoniker**) { return E_NOTIMPL; }
  136. JUCE_COMRESULT GetContainer (LPOLECONTAINER* ppContainer) { *ppContainer = nullptr; return E_NOINTERFACE; }
  137. JUCE_COMRESULT ShowObject() { return S_OK; }
  138. JUCE_COMRESULT OnShowWindow (BOOL) { return E_NOTIMPL; }
  139. JUCE_COMRESULT RequestNewObjectLayout() { return E_NOTIMPL; }
  140. LRESULT offerEventToActiveXControl (::MSG& msg)
  141. {
  142. if (inplaceSite != nullptr)
  143. return inplaceSite->offerEventToActiveXControl (msg);
  144. return S_FALSE;
  145. }
  146. JuceIOleInPlaceSite* inplaceSite;
  147. };
  148. //==============================================================================
  149. static Array<ActiveXControlComponent*> activeXComps;
  150. static HWND getHWND (const ActiveXControlComponent* const component)
  151. {
  152. HWND hwnd = {};
  153. const IID iid = __uuidof (IOleWindow);
  154. if (auto* window = (IOleWindow*) component->queryInterface (&iid))
  155. {
  156. window->GetWindow (&hwnd);
  157. window->Release();
  158. }
  159. return hwnd;
  160. }
  161. static void offerActiveXMouseEventToPeer (ComponentPeer* peer, HWND hwnd, UINT message, LPARAM lParam)
  162. {
  163. switch (message)
  164. {
  165. case WM_MOUSEMOVE:
  166. case WM_LBUTTONDOWN:
  167. case WM_MBUTTONDOWN:
  168. case WM_RBUTTONDOWN:
  169. case WM_LBUTTONUP:
  170. case WM_MBUTTONUP:
  171. case WM_RBUTTONUP:
  172. {
  173. RECT activeXRect, peerRect;
  174. GetWindowRect (hwnd, &activeXRect);
  175. GetWindowRect ((HWND) peer->getNativeHandle(), &peerRect);
  176. peer->handleMouseEvent (MouseInputSource::InputSourceType::mouse,
  177. { (float) (GET_X_LPARAM (lParam) + activeXRect.left - peerRect.left),
  178. (float) (GET_Y_LPARAM (lParam) + activeXRect.top - peerRect.top) },
  179. ComponentPeer::getCurrentModifiersRealtime(),
  180. MouseInputSource::invalidPressure,
  181. MouseInputSource::invalidOrientation,
  182. getMouseEventTime());
  183. break;
  184. }
  185. default:
  186. break;
  187. }
  188. }
  189. }
  190. //==============================================================================
  191. class ActiveXControlComponent::Pimpl : public ComponentMovementWatcher
  192. #if JUCE_WIN_PER_MONITOR_DPI_AWARE
  193. , public ComponentPeer::ScaleFactorListener
  194. #endif
  195. {
  196. public:
  197. Pimpl (HWND hwnd, ActiveXControlComponent& activeXComp)
  198. : ComponentMovementWatcher (&activeXComp),
  199. owner (activeXComp),
  200. storage (new ActiveXHelpers::JuceIStorage()),
  201. clientSite (new ActiveXHelpers::JuceIOleClientSite (hwnd))
  202. {
  203. }
  204. ~Pimpl()
  205. {
  206. if (control != nullptr)
  207. {
  208. control->Close (OLECLOSE_NOSAVE);
  209. control->Release();
  210. }
  211. clientSite->Release();
  212. storage->Release();
  213. #if JUCE_WIN_PER_MONITOR_DPI_AWARE
  214. for (int i = 0; i < ComponentPeer::getNumPeers(); ++i)
  215. if (auto* peer = ComponentPeer::getPeer (i))
  216. peer->removeScaleFactorListener (this);
  217. #endif
  218. }
  219. void setControlBounds (Rectangle<int> newBounds) const
  220. {
  221. if (controlHWND != nullptr)
  222. {
  223. #if JUCE_WIN_PER_MONITOR_DPI_AWARE
  224. if (auto* peer = owner.getTopLevelComponent()->getPeer())
  225. newBounds = (newBounds.toDouble() * peer->getPlatformScaleFactor()).toNearestInt();
  226. #endif
  227. MoveWindow (controlHWND, newBounds.getX(), newBounds.getY(), newBounds.getWidth(), newBounds.getHeight(), TRUE);
  228. }
  229. }
  230. void setControlVisible (bool shouldBeVisible) const
  231. {
  232. if (controlHWND != nullptr)
  233. ShowWindow (controlHWND, shouldBeVisible ? SW_SHOWNA : SW_HIDE);
  234. }
  235. //==============================================================================
  236. using ComponentMovementWatcher::componentMovedOrResized;
  237. void componentMovedOrResized (bool /*wasMoved*/, bool /*wasResized*/) override
  238. {
  239. if (auto* peer = owner.getTopLevelComponent()->getPeer())
  240. setControlBounds (peer->getAreaCoveredBy (owner));
  241. }
  242. void componentPeerChanged() override
  243. {
  244. componentMovedOrResized (true, true);
  245. #if JUCE_WIN_PER_MONITOR_DPI_AWARE
  246. if (auto* peer = owner.getTopLevelComponent()->getPeer())
  247. peer->addScaleFactorListener (this);
  248. #endif
  249. }
  250. using ComponentMovementWatcher::componentVisibilityChanged;
  251. void componentVisibilityChanged() override
  252. {
  253. setControlVisible (owner.isShowing());
  254. componentPeerChanged();
  255. }
  256. #if JUCE_WIN_PER_MONITOR_DPI_AWARE
  257. void nativeScaleFactorChanged (double /*newScaleFactor*/) override
  258. {
  259. componentMovedOrResized (true, true);
  260. }
  261. #endif
  262. // intercepts events going to an activeX control, so we can sneakily use the mouse events
  263. static LRESULT CALLBACK activeXHookWndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  264. {
  265. for (auto* ax : ActiveXHelpers::activeXComps)
  266. {
  267. if (ax->control != nullptr && ax->control->controlHWND == hwnd)
  268. {
  269. switch (message)
  270. {
  271. case WM_MOUSEMOVE:
  272. case WM_LBUTTONDOWN:
  273. case WM_MBUTTONDOWN:
  274. case WM_RBUTTONDOWN:
  275. case WM_LBUTTONUP:
  276. case WM_MBUTTONUP:
  277. case WM_RBUTTONUP:
  278. case WM_LBUTTONDBLCLK:
  279. case WM_MBUTTONDBLCLK:
  280. case WM_RBUTTONDBLCLK:
  281. if (ax->isShowing())
  282. {
  283. if (auto* peer = ax->getPeer())
  284. {
  285. ActiveXHelpers::offerActiveXMouseEventToPeer (peer, hwnd, message, lParam);
  286. if (! ax->areMouseEventsAllowed())
  287. return 0;
  288. }
  289. }
  290. break;
  291. default:
  292. break;
  293. }
  294. return CallWindowProc (ax->control->originalWndProc, hwnd, message, wParam, lParam);
  295. }
  296. }
  297. return DefWindowProc (hwnd, message, wParam, lParam);
  298. }
  299. ActiveXControlComponent& owner;
  300. HWND controlHWND = {};
  301. IStorage* storage = nullptr;
  302. ActiveXHelpers::JuceIOleClientSite* clientSite = nullptr;
  303. IOleObject* control = nullptr;
  304. WNDPROC originalWndProc = nullptr;
  305. };
  306. //==============================================================================
  307. ActiveXControlComponent::ActiveXControlComponent()
  308. {
  309. ActiveXHelpers::activeXComps.add (this);
  310. }
  311. ActiveXControlComponent::~ActiveXControlComponent()
  312. {
  313. deleteControl();
  314. ActiveXHelpers::activeXComps.removeFirstMatchingValue (this);
  315. }
  316. void ActiveXControlComponent::paint (Graphics& g)
  317. {
  318. if (control == nullptr)
  319. g.fillAll (Colours::lightgrey);
  320. }
  321. bool ActiveXControlComponent::createControl (const void* controlIID)
  322. {
  323. deleteControl();
  324. if (auto* peer = getPeer())
  325. {
  326. auto controlBounds = peer->getAreaCoveredBy (*this);
  327. auto hwnd = (HWND) peer->getNativeHandle();
  328. std::unique_ptr<Pimpl> newControl (new Pimpl (hwnd, *this));
  329. HRESULT hr = OleCreate (*(const IID*) controlIID, __uuidof (IOleObject), 1 /*OLERENDER_DRAW*/, nullptr,
  330. newControl->clientSite, newControl->storage,
  331. (void**) &(newControl->control));
  332. if (hr == S_OK)
  333. {
  334. newControl->control->SetHostNames (L"JUCE", nullptr);
  335. if (OleSetContainedObject (newControl->control, TRUE) == S_OK)
  336. {
  337. RECT rect;
  338. rect.left = controlBounds.getX();
  339. rect.top = controlBounds.getY();
  340. rect.right = controlBounds.getRight();
  341. rect.bottom = controlBounds.getBottom();
  342. if (newControl->control->DoVerb (OLEIVERB_SHOW, nullptr, newControl->clientSite, 0, hwnd, &rect) == S_OK)
  343. {
  344. control.reset (newControl.release());
  345. control->controlHWND = ActiveXHelpers::getHWND (this);
  346. if (control->controlHWND != nullptr)
  347. {
  348. control->setControlBounds (controlBounds);
  349. control->originalWndProc = (WNDPROC) GetWindowLongPtr ((HWND) control->controlHWND, GWLP_WNDPROC);
  350. SetWindowLongPtr ((HWND) control->controlHWND, GWLP_WNDPROC, (LONG_PTR) Pimpl::activeXHookWndProc);
  351. }
  352. return true;
  353. }
  354. }
  355. }
  356. }
  357. else
  358. {
  359. // the component must have already been added to a real window when you call this!
  360. jassertfalse;
  361. }
  362. return false;
  363. }
  364. void ActiveXControlComponent::deleteControl()
  365. {
  366. control = nullptr;
  367. }
  368. void* ActiveXControlComponent::queryInterface (const void* iid) const
  369. {
  370. void* result = nullptr;
  371. if (control != nullptr && control->control != nullptr
  372. && SUCCEEDED (control->control->QueryInterface (*(const IID*) iid, &result)))
  373. return result;
  374. return nullptr;
  375. }
  376. void ActiveXControlComponent::setMouseEventsAllowed (const bool eventsCanReachControl)
  377. {
  378. mouseEventsAllowed = eventsCanReachControl;
  379. }
  380. intptr_t ActiveXControlComponent::offerEventToActiveXControl (void* ptr)
  381. {
  382. if (control != nullptr && control->clientSite != nullptr)
  383. return (intptr_t) control->clientSite->offerEventToActiveXControl (*reinterpret_cast<::MSG*> (ptr));
  384. return S_FALSE;
  385. }
  386. intptr_t ActiveXControlComponent::offerEventToActiveXControlStatic (void* ptr)
  387. {
  388. for (auto* ax : ActiveXHelpers::activeXComps)
  389. {
  390. auto result = ax->offerEventToActiveXControl (ptr);
  391. if (result != S_FALSE)
  392. return result;
  393. }
  394. return S_FALSE;
  395. }
  396. LRESULT juce_offerEventToActiveXControl (::MSG& msg)
  397. {
  398. if (msg.message >= WM_KEYFIRST && msg.message <= WM_KEYLAST)
  399. return ActiveXControlComponent::offerEventToActiveXControlStatic (&msg);
  400. return S_FALSE;
  401. }
  402. } // namespace juce