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_ActiveXComponent.cpp 18KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE 6 technical preview.
  4. Copyright (c) 2017 - ROLI Ltd.
  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 = 0;
  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 inline 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 inline 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 != 0)
  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 != 0)
  228. ShowWindow (controlHWND, shouldBeVisible ? SW_SHOWNA : SW_HIDE);
  229. }
  230. //==============================================================================
  231. void componentMovedOrResized (bool /*wasMoved*/, bool /*wasResized*/) override
  232. {
  233. if (auto* peer = owner.getTopLevelComponent()->getPeer())
  234. setControlBounds (peer->getAreaCoveredBy (owner));
  235. }
  236. void componentPeerChanged() override
  237. {
  238. componentMovedOrResized (true, true);
  239. #if JUCE_WIN_PER_MONITOR_DPI_AWARE
  240. if (auto* peer = owner.getTopLevelComponent()->getPeer())
  241. peer->addScaleFactorListener (this);
  242. #endif
  243. }
  244. void componentVisibilityChanged() override
  245. {
  246. setControlVisible (owner.isShowing());
  247. componentPeerChanged();
  248. }
  249. #if JUCE_WIN_PER_MONITOR_DPI_AWARE
  250. void nativeScaleFactorChanged (double /*newScaleFactor*/) override
  251. {
  252. componentMovedOrResized (true, true);
  253. }
  254. #endif
  255. // intercepts events going to an activeX control, so we can sneakily use the mouse events
  256. static LRESULT CALLBACK activeXHookWndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  257. {
  258. for (auto* ax : ActiveXHelpers::activeXComps)
  259. {
  260. if (ax->control != nullptr && ax->control->controlHWND == hwnd)
  261. {
  262. switch (message)
  263. {
  264. case WM_MOUSEMOVE:
  265. case WM_LBUTTONDOWN:
  266. case WM_MBUTTONDOWN:
  267. case WM_RBUTTONDOWN:
  268. case WM_LBUTTONUP:
  269. case WM_MBUTTONUP:
  270. case WM_RBUTTONUP:
  271. case WM_LBUTTONDBLCLK:
  272. case WM_MBUTTONDBLCLK:
  273. case WM_RBUTTONDBLCLK:
  274. if (ax->isShowing())
  275. {
  276. if (auto* peer = ax->getPeer())
  277. {
  278. ActiveXHelpers::offerActiveXMouseEventToPeer (peer, hwnd, message, lParam);
  279. if (! ax->areMouseEventsAllowed())
  280. return 0;
  281. }
  282. }
  283. break;
  284. default:
  285. break;
  286. }
  287. return CallWindowProc (ax->control->originalWndProc, hwnd, message, wParam, lParam);
  288. }
  289. }
  290. return DefWindowProc (hwnd, message, wParam, lParam);
  291. }
  292. ActiveXControlComponent& owner;
  293. HWND controlHWND = {};
  294. IStorage* storage = nullptr;
  295. ActiveXHelpers::JuceIOleClientSite* clientSite = nullptr;
  296. IOleObject* control = nullptr;
  297. WNDPROC originalWndProc = 0;
  298. };
  299. //==============================================================================
  300. ActiveXControlComponent::ActiveXControlComponent()
  301. {
  302. ActiveXHelpers::activeXComps.add (this);
  303. }
  304. ActiveXControlComponent::~ActiveXControlComponent()
  305. {
  306. deleteControl();
  307. ActiveXHelpers::activeXComps.removeFirstMatchingValue (this);
  308. }
  309. void ActiveXControlComponent::paint (Graphics& g)
  310. {
  311. if (control == nullptr)
  312. g.fillAll (Colours::lightgrey);
  313. }
  314. bool ActiveXControlComponent::createControl (const void* controlIID)
  315. {
  316. deleteControl();
  317. if (auto* peer = getPeer())
  318. {
  319. auto controlBounds = peer->getAreaCoveredBy (*this);
  320. auto hwnd = (HWND) peer->getNativeHandle();
  321. std::unique_ptr<Pimpl> newControl (new Pimpl (hwnd, *this));
  322. HRESULT hr = OleCreate (*(const IID*) controlIID, __uuidof (IOleObject), 1 /*OLERENDER_DRAW*/, 0,
  323. newControl->clientSite, newControl->storage,
  324. (void**) &(newControl->control));
  325. if (hr == S_OK)
  326. {
  327. newControl->control->SetHostNames (L"JUCE", 0);
  328. if (OleSetContainedObject (newControl->control, TRUE) == S_OK)
  329. {
  330. RECT rect;
  331. rect.left = controlBounds.getX();
  332. rect.top = controlBounds.getY();
  333. rect.right = controlBounds.getRight();
  334. rect.bottom = controlBounds.getBottom();
  335. if (newControl->control->DoVerb (OLEIVERB_SHOW, 0, newControl->clientSite, 0, hwnd, &rect) == S_OK)
  336. {
  337. control.reset (newControl.release());
  338. control->controlHWND = ActiveXHelpers::getHWND (this);
  339. if (control->controlHWND != 0)
  340. {
  341. control->setControlBounds (controlBounds);
  342. control->originalWndProc = (WNDPROC) GetWindowLongPtr ((HWND) control->controlHWND, GWLP_WNDPROC);
  343. SetWindowLongPtr ((HWND) control->controlHWND, GWLP_WNDPROC, (LONG_PTR) Pimpl::activeXHookWndProc);
  344. }
  345. return true;
  346. }
  347. }
  348. }
  349. }
  350. else
  351. {
  352. // the component must have already been added to a real window when you call this!
  353. jassertfalse;
  354. }
  355. return false;
  356. }
  357. void ActiveXControlComponent::deleteControl()
  358. {
  359. control = nullptr;
  360. }
  361. void* ActiveXControlComponent::queryInterface (const void* iid) const
  362. {
  363. void* result = nullptr;
  364. if (control != nullptr && control->control != nullptr
  365. && SUCCEEDED (control->control->QueryInterface (*(const IID*) iid, &result)))
  366. return result;
  367. return nullptr;
  368. }
  369. void ActiveXControlComponent::setMouseEventsAllowed (const bool eventsCanReachControl)
  370. {
  371. mouseEventsAllowed = eventsCanReachControl;
  372. }
  373. intptr_t ActiveXControlComponent::offerEventToActiveXControl (void* ptr)
  374. {
  375. if (control != nullptr && control->clientSite != nullptr)
  376. return (intptr_t) control->clientSite->offerEventToActiveXControl (*reinterpret_cast<::MSG*> (ptr));
  377. return S_FALSE;
  378. }
  379. intptr_t ActiveXControlComponent::offerEventToActiveXControlStatic (void* ptr)
  380. {
  381. for (auto* ax : ActiveXHelpers::activeXComps)
  382. {
  383. auto result = ax->offerEventToActiveXControl (ptr);
  384. if (result != S_FALSE)
  385. return result;
  386. }
  387. return S_FALSE;
  388. }
  389. LRESULT juce_offerEventToActiveXControl (::MSG& msg)
  390. {
  391. if (msg.message >= WM_KEYFIRST && msg.message <= WM_KEYLAST)
  392. return ActiveXControlComponent::offerEventToActiveXControlStatic (&msg);
  393. return S_FALSE;
  394. }
  395. } // namespace juce