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.

484 lines
19KB

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