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 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software Ltd.
  5. Permission is granted to use this software under the terms of either:
  6. a) the GPL v2 (or any later version)
  7. b) the Affero GPL v3
  8. Details of these licenses can be found at: www.gnu.org/licenses
  9. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  10. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  11. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  12. ------------------------------------------------------------------------------
  13. To release a closed-source product which uses JUCE, commercial licenses are
  14. available: visit www.juce.com for more information.
  15. ==============================================================================
  16. */
  17. extern int64 getMouseEventTime();
  18. namespace ActiveXHelpers
  19. {
  20. //==============================================================================
  21. class JuceIStorage : public ComBaseClassHelper <IStorage>
  22. {
  23. public:
  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. class JuceOleInPlaceFrame : public ComBaseClassHelper <IOleInPlaceFrame>
  43. {
  44. public:
  45. JuceOleInPlaceFrame (HWND hwnd) : window (hwnd) {}
  46. JUCE_COMRESULT GetWindow (HWND* lphwnd) { *lphwnd = window; return S_OK; }
  47. JUCE_COMRESULT ContextSensitiveHelp (BOOL) { return E_NOTIMPL; }
  48. JUCE_COMRESULT GetBorder (LPRECT) { return E_NOTIMPL; }
  49. JUCE_COMRESULT RequestBorderSpace (LPCBORDERWIDTHS) { return E_NOTIMPL; }
  50. JUCE_COMRESULT SetBorderSpace (LPCBORDERWIDTHS) { return E_NOTIMPL; }
  51. JUCE_COMRESULT SetActiveObject (IOleInPlaceActiveObject*, LPCOLESTR) { return S_OK; }
  52. JUCE_COMRESULT InsertMenus (HMENU, LPOLEMENUGROUPWIDTHS) { return E_NOTIMPL; }
  53. JUCE_COMRESULT SetMenu (HMENU, HOLEMENU, HWND) { return S_OK; }
  54. JUCE_COMRESULT RemoveMenus (HMENU) { return E_NOTIMPL; }
  55. JUCE_COMRESULT SetStatusText (LPCOLESTR) { return S_OK; }
  56. JUCE_COMRESULT EnableModeless (BOOL) { return S_OK; }
  57. JUCE_COMRESULT TranslateAccelerator (LPMSG, WORD) { return E_NOTIMPL; }
  58. private:
  59. HWND window;
  60. };
  61. //==============================================================================
  62. class JuceIOleInPlaceSite : public ComBaseClassHelper <IOleInPlaceSite>
  63. {
  64. public:
  65. JuceIOleInPlaceSite (HWND hwnd)
  66. : window (hwnd),
  67. frame (new JuceOleInPlaceFrame (window))
  68. {}
  69. ~JuceIOleInPlaceSite()
  70. {
  71. frame->Release();
  72. }
  73. JUCE_COMRESULT GetWindow (HWND* lphwnd) { *lphwnd = window; return S_OK; }
  74. JUCE_COMRESULT ContextSensitiveHelp (BOOL) { return E_NOTIMPL; }
  75. JUCE_COMRESULT CanInPlaceActivate() { return S_OK; }
  76. JUCE_COMRESULT OnInPlaceActivate() { return S_OK; }
  77. JUCE_COMRESULT OnUIActivate() { return S_OK; }
  78. JUCE_COMRESULT GetWindowContext (LPOLEINPLACEFRAME* lplpFrame, LPOLEINPLACEUIWINDOW* lplpDoc, LPRECT, LPRECT, LPOLEINPLACEFRAMEINFO lpFrameInfo)
  79. {
  80. /* Note: if you call AddRef on the frame here, then some types of object (e.g. web browser control) cause leaks..
  81. If you don't call AddRef then others crash (e.g. QuickTime).. Bit of a catch-22, so letting it leak is probably preferable.
  82. */
  83. if (lplpFrame != nullptr) { frame->AddRef(); *lplpFrame = frame; }
  84. if (lplpDoc != nullptr) *lplpDoc = nullptr;
  85. lpFrameInfo->fMDIApp = FALSE;
  86. lpFrameInfo->hwndFrame = window;
  87. lpFrameInfo->haccel = 0;
  88. lpFrameInfo->cAccelEntries = 0;
  89. return S_OK;
  90. }
  91. JUCE_COMRESULT Scroll (SIZE) { return E_NOTIMPL; }
  92. JUCE_COMRESULT OnUIDeactivate (BOOL) { return S_OK; }
  93. JUCE_COMRESULT OnInPlaceDeactivate() { return S_OK; }
  94. JUCE_COMRESULT DiscardUndoState() { return E_NOTIMPL; }
  95. JUCE_COMRESULT DeactivateAndUndo() { return E_NOTIMPL; }
  96. JUCE_COMRESULT OnPosRectChange (LPCRECT) { return S_OK; }
  97. private:
  98. HWND window;
  99. JuceOleInPlaceFrame* frame;
  100. };
  101. //==============================================================================
  102. class JuceIOleClientSite : public ComBaseClassHelper <IOleClientSite>
  103. {
  104. public:
  105. JuceIOleClientSite (HWND window)
  106. : inplaceSite (new JuceIOleInPlaceSite (window))
  107. {}
  108. ~JuceIOleClientSite()
  109. {
  110. inplaceSite->Release();
  111. }
  112. JUCE_COMRESULT QueryInterface (REFIID type, void** result)
  113. {
  114. if (type == IID_IOleInPlaceSite)
  115. {
  116. inplaceSite->AddRef();
  117. *result = static_cast <IOleInPlaceSite*> (inplaceSite);
  118. return S_OK;
  119. }
  120. return ComBaseClassHelper <IOleClientSite>::QueryInterface (type, result);
  121. }
  122. JUCE_COMRESULT SaveObject() { return E_NOTIMPL; }
  123. JUCE_COMRESULT GetMoniker (DWORD, DWORD, IMoniker**) { return E_NOTIMPL; }
  124. JUCE_COMRESULT GetContainer (LPOLECONTAINER* ppContainer) { *ppContainer = nullptr; return E_NOINTERFACE; }
  125. JUCE_COMRESULT ShowObject() { return S_OK; }
  126. JUCE_COMRESULT OnShowWindow (BOOL) { return E_NOTIMPL; }
  127. JUCE_COMRESULT RequestNewObjectLayout() { return E_NOTIMPL; }
  128. private:
  129. JuceIOleInPlaceSite* inplaceSite;
  130. };
  131. //==============================================================================
  132. static Array<ActiveXControlComponent*> activeXComps;
  133. HWND getHWND (const ActiveXControlComponent* const component)
  134. {
  135. HWND hwnd = 0;
  136. const IID iid = IID_IOleWindow;
  137. if (IOleWindow* const window = (IOleWindow*) component->queryInterface (&iid))
  138. {
  139. window->GetWindow (&hwnd);
  140. window->Release();
  141. }
  142. return hwnd;
  143. }
  144. void offerActiveXMouseEventToPeer (ComponentPeer* const peer, HWND hwnd, UINT message, LPARAM lParam)
  145. {
  146. RECT activeXRect, peerRect;
  147. GetWindowRect (hwnd, &activeXRect);
  148. GetWindowRect ((HWND) peer->getNativeHandle(), &peerRect);
  149. switch (message)
  150. {
  151. case WM_MOUSEMOVE:
  152. case WM_LBUTTONDOWN:
  153. case WM_MBUTTONDOWN:
  154. case WM_RBUTTONDOWN:
  155. case WM_LBUTTONUP:
  156. case WM_MBUTTONUP:
  157. case WM_RBUTTONUP:
  158. peer->handleMouseEvent (0, Point<int> (GET_X_LPARAM (lParam) + activeXRect.left - peerRect.left,
  159. GET_Y_LPARAM (lParam) + activeXRect.top - peerRect.top),
  160. ModifierKeys::getCurrentModifiersRealtime(),
  161. getMouseEventTime());
  162. break;
  163. default:
  164. break;
  165. }
  166. }
  167. }
  168. //==============================================================================
  169. class ActiveXControlComponent::Pimpl : public ComponentMovementWatcher
  170. {
  171. public:
  172. Pimpl (HWND hwnd, ActiveXControlComponent& activeXComp)
  173. : ComponentMovementWatcher (&activeXComp),
  174. owner (activeXComp),
  175. controlHWND (0),
  176. storage (new ActiveXHelpers::JuceIStorage()),
  177. clientSite (new ActiveXHelpers::JuceIOleClientSite (hwnd)),
  178. control (nullptr),
  179. originalWndProc (0)
  180. {
  181. }
  182. ~Pimpl()
  183. {
  184. if (control != nullptr)
  185. {
  186. control->Close (OLECLOSE_NOSAVE);
  187. control->Release();
  188. }
  189. clientSite->Release();
  190. storage->Release();
  191. }
  192. void setControlBounds (const Rectangle<int>& bounds) const
  193. {
  194. if (controlHWND != 0)
  195. MoveWindow (controlHWND, bounds.getX(), bounds.getY(), bounds.getWidth(), bounds.getHeight(), TRUE);
  196. }
  197. void setControlVisible (bool shouldBeVisible) const
  198. {
  199. if (controlHWND != 0)
  200. ShowWindow (controlHWND, shouldBeVisible ? SW_SHOWNA : SW_HIDE);
  201. }
  202. //==============================================================================
  203. void componentMovedOrResized (bool /*wasMoved*/, bool /*wasResized*/) override
  204. {
  205. if (ComponentPeer* const peer = owner.getTopLevelComponent()->getPeer())
  206. setControlBounds (peer->getAreaCoveredBy (owner));
  207. }
  208. void componentPeerChanged() override
  209. {
  210. componentMovedOrResized (true, true);
  211. }
  212. void componentVisibilityChanged() override
  213. {
  214. setControlVisible (owner.isShowing());
  215. componentPeerChanged();
  216. }
  217. // intercepts events going to an activeX control, so we can sneakily use the mouse events
  218. static LRESULT CALLBACK activeXHookWndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  219. {
  220. for (int i = ActiveXHelpers::activeXComps.size(); --i >= 0;)
  221. {
  222. const ActiveXControlComponent* const ax = ActiveXHelpers::activeXComps.getUnchecked(i);
  223. if (ax->control != nullptr && ax->control->controlHWND == hwnd)
  224. {
  225. switch (message)
  226. {
  227. case WM_MOUSEMOVE:
  228. case WM_LBUTTONDOWN:
  229. case WM_MBUTTONDOWN:
  230. case WM_RBUTTONDOWN:
  231. case WM_LBUTTONUP:
  232. case WM_MBUTTONUP:
  233. case WM_RBUTTONUP:
  234. case WM_LBUTTONDBLCLK:
  235. case WM_MBUTTONDBLCLK:
  236. case WM_RBUTTONDBLCLK:
  237. if (ax->isShowing())
  238. {
  239. if (ComponentPeer* const peer = ax->getPeer())
  240. {
  241. ActiveXHelpers::offerActiveXMouseEventToPeer (peer, hwnd, message, lParam);
  242. if (! ax->areMouseEventsAllowed())
  243. return 0;
  244. }
  245. }
  246. break;
  247. default:
  248. break;
  249. }
  250. return CallWindowProc (ax->control->originalWndProc, hwnd, message, wParam, lParam);
  251. }
  252. }
  253. return DefWindowProc (hwnd, message, wParam, lParam);
  254. }
  255. ActiveXControlComponent& owner;
  256. HWND controlHWND;
  257. IStorage* storage;
  258. IOleClientSite* clientSite;
  259. IOleObject* control;
  260. WNDPROC originalWndProc;
  261. };
  262. //==============================================================================
  263. ActiveXControlComponent::ActiveXControlComponent()
  264. : mouseEventsAllowed (true)
  265. {
  266. ActiveXHelpers::activeXComps.add (this);
  267. }
  268. ActiveXControlComponent::~ActiveXControlComponent()
  269. {
  270. deleteControl();
  271. ActiveXHelpers::activeXComps.removeFirstMatchingValue (this);
  272. }
  273. void ActiveXControlComponent::paint (Graphics& g)
  274. {
  275. if (control == nullptr)
  276. g.fillAll (Colours::lightgrey);
  277. }
  278. bool ActiveXControlComponent::createControl (const void* controlIID)
  279. {
  280. deleteControl();
  281. if (ComponentPeer* const peer = getPeer())
  282. {
  283. const Rectangle<int> bounds (peer->getAreaCoveredBy (*this));
  284. HWND hwnd = (HWND) peer->getNativeHandle();
  285. ScopedPointer<Pimpl> newControl (new Pimpl (hwnd, *this));
  286. HRESULT hr;
  287. if ((hr = OleCreate (*(const IID*) controlIID, IID_IOleObject, 1 /*OLERENDER_DRAW*/, 0,
  288. newControl->clientSite, newControl->storage,
  289. (void**) &(newControl->control))) == S_OK)
  290. {
  291. newControl->control->SetHostNames (L"JUCE", 0);
  292. if (OleSetContainedObject (newControl->control, TRUE) == S_OK)
  293. {
  294. RECT rect;
  295. rect.left = bounds.getX();
  296. rect.top = bounds.getY();
  297. rect.right = bounds.getRight();
  298. rect.bottom = bounds.getBottom();
  299. if (newControl->control->DoVerb (OLEIVERB_SHOW, 0, newControl->clientSite, 0, hwnd, &rect) == S_OK)
  300. {
  301. control = newControl;
  302. control->controlHWND = ActiveXHelpers::getHWND (this);
  303. if (control->controlHWND != 0)
  304. {
  305. control->setControlBounds (bounds);
  306. control->originalWndProc = (WNDPROC) GetWindowLongPtr ((HWND) control->controlHWND, GWLP_WNDPROC);
  307. SetWindowLongPtr ((HWND) control->controlHWND, GWLP_WNDPROC, (LONG_PTR) Pimpl::activeXHookWndProc);
  308. }
  309. return true;
  310. }
  311. }
  312. }
  313. }
  314. else
  315. {
  316. // the component must have already been added to a real window when you call this!
  317. jassertfalse;
  318. }
  319. return false;
  320. }
  321. void ActiveXControlComponent::deleteControl()
  322. {
  323. control = nullptr;
  324. }
  325. void* ActiveXControlComponent::queryInterface (const void* iid) const
  326. {
  327. void* result = nullptr;
  328. if (control != nullptr && control->control != nullptr
  329. && SUCCEEDED (control->control->QueryInterface (*(const IID*) iid, &result)))
  330. return result;
  331. return nullptr;
  332. }
  333. void ActiveXControlComponent::setMouseEventsAllowed (const bool eventsCanReachControl)
  334. {
  335. mouseEventsAllowed = eventsCanReachControl;
  336. }