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.

498 lines
19KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - 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 7 End-User License
  8. Agreement and JUCE Privacy Policy.
  9. End User License Agreement: www.juce.com/juce-7-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. JUCE_BEGIN_IGNORE_WARNINGS_GCC_LIKE ("-Wlanguage-extension-token")
  127. if (type == __uuidof (IOleInPlaceSite))
  128. {
  129. inplaceSite->AddRef();
  130. *result = static_cast<IOleInPlaceSite*> (inplaceSite);
  131. return S_OK;
  132. }
  133. return ComBaseClassHelper <IOleClientSite>::QueryInterface (type, result);
  134. JUCE_END_IGNORE_WARNINGS_GCC_LIKE
  135. }
  136. JUCE_COMRESULT SaveObject() { return E_NOTIMPL; }
  137. JUCE_COMRESULT GetMoniker (DWORD, DWORD, IMoniker**) { return E_NOTIMPL; }
  138. JUCE_COMRESULT GetContainer (LPOLECONTAINER* ppContainer) { *ppContainer = nullptr; return E_NOINTERFACE; }
  139. JUCE_COMRESULT ShowObject() { return S_OK; }
  140. JUCE_COMRESULT OnShowWindow (BOOL) { return E_NOTIMPL; }
  141. JUCE_COMRESULT RequestNewObjectLayout() { return E_NOTIMPL; }
  142. LRESULT offerEventToActiveXControl (::MSG& msg)
  143. {
  144. if (inplaceSite != nullptr)
  145. return inplaceSite->offerEventToActiveXControl (msg);
  146. return S_FALSE;
  147. }
  148. JuceIOleInPlaceSite* inplaceSite;
  149. };
  150. //==============================================================================
  151. static Array<ActiveXControlComponent*> activeXComps;
  152. static HWND getHWND (const ActiveXControlComponent* const component)
  153. {
  154. JUCE_BEGIN_IGNORE_WARNINGS_GCC_LIKE ("-Wlanguage-extension-token")
  155. HWND hwnd = {};
  156. const IID iid = __uuidof (IOleWindow);
  157. if (auto* window = (IOleWindow*) component->queryInterface (&iid))
  158. {
  159. window->GetWindow (&hwnd);
  160. window->Release();
  161. }
  162. return hwnd;
  163. JUCE_END_IGNORE_WARNINGS_GCC_LIKE
  164. }
  165. static void offerActiveXMouseEventToPeer (ComponentPeer* peer, HWND hwnd, UINT message, LPARAM lParam)
  166. {
  167. switch (message)
  168. {
  169. case WM_MOUSEMOVE:
  170. case WM_LBUTTONDOWN:
  171. case WM_MBUTTONDOWN:
  172. case WM_RBUTTONDOWN:
  173. case WM_LBUTTONUP:
  174. case WM_MBUTTONUP:
  175. case WM_RBUTTONUP:
  176. {
  177. RECT activeXRect, peerRect;
  178. GetWindowRect (hwnd, &activeXRect);
  179. GetWindowRect ((HWND) peer->getNativeHandle(), &peerRect);
  180. peer->handleMouseEvent (MouseInputSource::InputSourceType::mouse,
  181. { (float) (GET_X_LPARAM (lParam) + activeXRect.left - peerRect.left),
  182. (float) (GET_Y_LPARAM (lParam) + activeXRect.top - peerRect.top) },
  183. ComponentPeer::getCurrentModifiersRealtime(),
  184. MouseInputSource::defaultPressure,
  185. MouseInputSource::defaultOrientation,
  186. getMouseEventTime());
  187. break;
  188. }
  189. default:
  190. break;
  191. }
  192. }
  193. }
  194. //==============================================================================
  195. class ActiveXControlComponent::Pimpl : public ComponentMovementWatcher,
  196. public ComponentPeer::ScaleFactorListener
  197. {
  198. public:
  199. Pimpl (HWND hwnd, ActiveXControlComponent& activeXComp)
  200. : ComponentMovementWatcher (&activeXComp),
  201. owner (activeXComp),
  202. storage (new ActiveXHelpers::JuceIStorage()),
  203. clientSite (new ActiveXHelpers::JuceIOleClientSite (hwnd))
  204. {
  205. }
  206. ~Pimpl() override
  207. {
  208. if (control != nullptr)
  209. {
  210. control->Close (OLECLOSE_NOSAVE);
  211. control->Release();
  212. }
  213. clientSite->Release();
  214. storage->Release();
  215. if (currentPeer != nullptr)
  216. currentPeer->removeScaleFactorListener (this);
  217. }
  218. void setControlBounds (Rectangle<int> newBounds) const
  219. {
  220. if (controlHWND != nullptr)
  221. {
  222. if (auto* peer = owner.getTopLevelComponent()->getPeer())
  223. newBounds = (newBounds.toDouble() * peer->getPlatformScaleFactor()).toNearestInt();
  224. MoveWindow (controlHWND, newBounds.getX(), newBounds.getY(), newBounds.getWidth(), newBounds.getHeight(), TRUE);
  225. }
  226. }
  227. void setControlVisible (bool shouldBeVisible) const
  228. {
  229. if (controlHWND != nullptr)
  230. ShowWindow (controlHWND, shouldBeVisible ? SW_SHOWNA : SW_HIDE);
  231. }
  232. //==============================================================================
  233. using ComponentMovementWatcher::componentMovedOrResized;
  234. void componentMovedOrResized (bool /*wasMoved*/, bool /*wasResized*/) override
  235. {
  236. if (auto* peer = owner.getTopLevelComponent()->getPeer())
  237. setControlBounds (peer->getAreaCoveredBy (owner));
  238. }
  239. void componentPeerChanged() override
  240. {
  241. if (currentPeer != nullptr)
  242. currentPeer->removeScaleFactorListener (this);
  243. componentMovedOrResized (true, true);
  244. currentPeer = owner.getTopLevelComponent()->getPeer();
  245. if (currentPeer != nullptr)
  246. currentPeer->addScaleFactorListener (this);
  247. }
  248. using ComponentMovementWatcher::componentVisibilityChanged;
  249. void componentVisibilityChanged() override
  250. {
  251. setControlVisible (owner.isShowing());
  252. componentPeerChanged();
  253. }
  254. void nativeScaleFactorChanged (double /*newScaleFactor*/) override
  255. {
  256. componentMovedOrResized (true, true);
  257. }
  258. // intercepts events going to an activeX control, so we can sneakily use the mouse events
  259. static LRESULT CALLBACK activeXHookWndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  260. {
  261. for (auto* ax : ActiveXHelpers::activeXComps)
  262. {
  263. if (ax->control != nullptr && ax->control->controlHWND == hwnd)
  264. {
  265. switch (message)
  266. {
  267. case WM_MOUSEMOVE:
  268. case WM_LBUTTONDOWN:
  269. case WM_MBUTTONDOWN:
  270. case WM_RBUTTONDOWN:
  271. case WM_LBUTTONUP:
  272. case WM_MBUTTONUP:
  273. case WM_RBUTTONUP:
  274. case WM_LBUTTONDBLCLK:
  275. case WM_MBUTTONDBLCLK:
  276. case WM_RBUTTONDBLCLK:
  277. if (ax->isShowing())
  278. {
  279. if (auto* peer = ax->getPeer())
  280. {
  281. ActiveXHelpers::offerActiveXMouseEventToPeer (peer, hwnd, message, lParam);
  282. if (! ax->areMouseEventsAllowed())
  283. return 0;
  284. }
  285. }
  286. break;
  287. default:
  288. break;
  289. }
  290. return CallWindowProc (ax->control->originalWndProc, hwnd, message, wParam, lParam);
  291. }
  292. }
  293. return DefWindowProc (hwnd, message, wParam, lParam);
  294. }
  295. ActiveXControlComponent& owner;
  296. ComponentPeer* currentPeer = nullptr;
  297. HWND controlHWND = {};
  298. IStorage* storage = nullptr;
  299. ActiveXHelpers::JuceIOleClientSite* clientSite = nullptr;
  300. IOleObject* control = nullptr;
  301. WNDPROC originalWndProc = nullptr;
  302. };
  303. //==============================================================================
  304. ActiveXControlComponent::ActiveXControlComponent()
  305. {
  306. ActiveXHelpers::activeXComps.add (this);
  307. }
  308. ActiveXControlComponent::~ActiveXControlComponent()
  309. {
  310. deleteControl();
  311. ActiveXHelpers::activeXComps.removeFirstMatchingValue (this);
  312. }
  313. void ActiveXControlComponent::paint (Graphics& g)
  314. {
  315. if (control == nullptr)
  316. g.fillAll (Colours::lightgrey);
  317. }
  318. bool ActiveXControlComponent::createControl (const void* controlIID)
  319. {
  320. deleteControl();
  321. if (auto* peer = getPeer())
  322. {
  323. auto controlBounds = peer->getAreaCoveredBy (*this);
  324. auto hwnd = (HWND) peer->getNativeHandle();
  325. std::unique_ptr<Pimpl> newControl (new Pimpl (hwnd, *this));
  326. JUCE_BEGIN_IGNORE_WARNINGS_GCC_LIKE ("-Wlanguage-extension-token")
  327. HRESULT hr = OleCreate (*(const IID*) controlIID, __uuidof (IOleObject), 1 /*OLERENDER_DRAW*/, nullptr,
  328. newControl->clientSite, newControl->storage,
  329. (void**) &(newControl->control));
  330. JUCE_END_IGNORE_WARNINGS_GCC_LIKE
  331. if (hr == S_OK)
  332. {
  333. newControl->control->SetHostNames (L"JUCE", nullptr);
  334. if (OleSetContainedObject (newControl->control, TRUE) == S_OK)
  335. {
  336. RECT rect;
  337. rect.left = controlBounds.getX();
  338. rect.top = controlBounds.getY();
  339. rect.right = controlBounds.getRight();
  340. rect.bottom = controlBounds.getBottom();
  341. if (newControl->control->DoVerb (OLEIVERB_SHOW, nullptr, newControl->clientSite, 0, hwnd, &rect) == S_OK)
  342. {
  343. control.reset (newControl.release());
  344. control->controlHWND = ActiveXHelpers::getHWND (this);
  345. if (control->controlHWND != nullptr)
  346. {
  347. control->setControlBounds (controlBounds);
  348. control->originalWndProc = (WNDPROC) GetWindowLongPtr ((HWND) control->controlHWND, GWLP_WNDPROC);
  349. SetWindowLongPtr ((HWND) control->controlHWND, GWLP_WNDPROC, (LONG_PTR) Pimpl::activeXHookWndProc);
  350. }
  351. return true;
  352. }
  353. }
  354. }
  355. }
  356. else
  357. {
  358. // the component must have already been added to a real window when you call this!
  359. jassertfalse;
  360. }
  361. return false;
  362. }
  363. void ActiveXControlComponent::deleteControl()
  364. {
  365. control = nullptr;
  366. }
  367. void* ActiveXControlComponent::queryInterface (const void* iid) const
  368. {
  369. void* result = nullptr;
  370. if (control != nullptr && control->control != nullptr
  371. && SUCCEEDED (control->control->QueryInterface (*(const IID*) iid, &result)))
  372. return result;
  373. return nullptr;
  374. }
  375. void ActiveXControlComponent::setMouseEventsAllowed (const bool eventsCanReachControl)
  376. {
  377. mouseEventsAllowed = eventsCanReachControl;
  378. }
  379. intptr_t ActiveXControlComponent::offerEventToActiveXControl (void* ptr)
  380. {
  381. if (control != nullptr && control->clientSite != nullptr)
  382. return (intptr_t) control->clientSite->offerEventToActiveXControl (*reinterpret_cast<::MSG*> (ptr));
  383. return S_FALSE;
  384. }
  385. intptr_t ActiveXControlComponent::offerEventToActiveXControlStatic (void* ptr)
  386. {
  387. for (auto* ax : ActiveXHelpers::activeXComps)
  388. {
  389. auto result = ax->offerEventToActiveXControl (ptr);
  390. if (result != S_FALSE)
  391. return result;
  392. }
  393. return S_FALSE;
  394. }
  395. LRESULT juce_offerEventToActiveXControl (::MSG& msg);
  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