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

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