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.

404 lines
16KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2015 - ROLI 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).toFloat(),
  160. ModifierKeys::getCurrentModifiersRealtime(),
  161. MouseInputSource::invalidPressure,
  162. getMouseEventTime());
  163. break;
  164. default:
  165. break;
  166. }
  167. }
  168. }
  169. //==============================================================================
  170. class ActiveXControlComponent::Pimpl : public ComponentMovementWatcher
  171. {
  172. public:
  173. Pimpl (HWND hwnd, ActiveXControlComponent& activeXComp)
  174. : ComponentMovementWatcher (&activeXComp),
  175. owner (activeXComp),
  176. controlHWND (0),
  177. storage (new ActiveXHelpers::JuceIStorage()),
  178. clientSite (new ActiveXHelpers::JuceIOleClientSite (hwnd)),
  179. control (nullptr),
  180. originalWndProc (0)
  181. {
  182. }
  183. ~Pimpl()
  184. {
  185. if (control != nullptr)
  186. {
  187. control->Close (OLECLOSE_NOSAVE);
  188. control->Release();
  189. }
  190. clientSite->Release();
  191. storage->Release();
  192. }
  193. void setControlBounds (const Rectangle<int>& newBounds) const
  194. {
  195. if (controlHWND != 0)
  196. MoveWindow (controlHWND, newBounds.getX(), newBounds.getY(), newBounds.getWidth(), newBounds.getHeight(), TRUE);
  197. }
  198. void setControlVisible (bool shouldBeVisible) const
  199. {
  200. if (controlHWND != 0)
  201. ShowWindow (controlHWND, shouldBeVisible ? SW_SHOWNA : SW_HIDE);
  202. }
  203. //==============================================================================
  204. void componentMovedOrResized (bool /*wasMoved*/, bool /*wasResized*/) override
  205. {
  206. if (ComponentPeer* const peer = owner.getTopLevelComponent()->getPeer())
  207. setControlBounds (peer->getAreaCoveredBy (owner));
  208. }
  209. void componentPeerChanged() override
  210. {
  211. componentMovedOrResized (true, true);
  212. }
  213. void componentVisibilityChanged() override
  214. {
  215. setControlVisible (owner.isShowing());
  216. componentPeerChanged();
  217. }
  218. // intercepts events going to an activeX control, so we can sneakily use the mouse events
  219. static LRESULT CALLBACK activeXHookWndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  220. {
  221. for (int i = ActiveXHelpers::activeXComps.size(); --i >= 0;)
  222. {
  223. const ActiveXControlComponent* const ax = ActiveXHelpers::activeXComps.getUnchecked(i);
  224. if (ax->control != nullptr && ax->control->controlHWND == hwnd)
  225. {
  226. switch (message)
  227. {
  228. case WM_MOUSEMOVE:
  229. case WM_LBUTTONDOWN:
  230. case WM_MBUTTONDOWN:
  231. case WM_RBUTTONDOWN:
  232. case WM_LBUTTONUP:
  233. case WM_MBUTTONUP:
  234. case WM_RBUTTONUP:
  235. case WM_LBUTTONDBLCLK:
  236. case WM_MBUTTONDBLCLK:
  237. case WM_RBUTTONDBLCLK:
  238. if (ax->isShowing())
  239. {
  240. if (ComponentPeer* const peer = ax->getPeer())
  241. {
  242. ActiveXHelpers::offerActiveXMouseEventToPeer (peer, hwnd, message, lParam);
  243. if (! ax->areMouseEventsAllowed())
  244. return 0;
  245. }
  246. }
  247. break;
  248. default:
  249. break;
  250. }
  251. return CallWindowProc (ax->control->originalWndProc, hwnd, message, wParam, lParam);
  252. }
  253. }
  254. return DefWindowProc (hwnd, message, wParam, lParam);
  255. }
  256. ActiveXControlComponent& owner;
  257. HWND controlHWND;
  258. IStorage* storage;
  259. IOleClientSite* clientSite;
  260. IOleObject* control;
  261. WNDPROC originalWndProc;
  262. };
  263. //==============================================================================
  264. ActiveXControlComponent::ActiveXControlComponent()
  265. : mouseEventsAllowed (true)
  266. {
  267. ActiveXHelpers::activeXComps.add (this);
  268. }
  269. ActiveXControlComponent::~ActiveXControlComponent()
  270. {
  271. deleteControl();
  272. ActiveXHelpers::activeXComps.removeFirstMatchingValue (this);
  273. }
  274. void ActiveXControlComponent::paint (Graphics& g)
  275. {
  276. if (control == nullptr)
  277. g.fillAll (Colours::lightgrey);
  278. }
  279. bool ActiveXControlComponent::createControl (const void* controlIID)
  280. {
  281. deleteControl();
  282. if (ComponentPeer* const peer = getPeer())
  283. {
  284. const Rectangle<int> controlBounds (peer->getAreaCoveredBy (*this));
  285. HWND hwnd = (HWND) peer->getNativeHandle();
  286. ScopedPointer<Pimpl> newControl (new Pimpl (hwnd, *this));
  287. HRESULT hr;
  288. if ((hr = OleCreate (*(const IID*) controlIID, IID_IOleObject, 1 /*OLERENDER_DRAW*/, 0,
  289. newControl->clientSite, newControl->storage,
  290. (void**) &(newControl->control))) == S_OK)
  291. {
  292. newControl->control->SetHostNames (L"JUCE", 0);
  293. if (OleSetContainedObject (newControl->control, TRUE) == S_OK)
  294. {
  295. RECT rect;
  296. rect.left = controlBounds.getX();
  297. rect.top = controlBounds.getY();
  298. rect.right = controlBounds.getRight();
  299. rect.bottom = controlBounds.getBottom();
  300. if (newControl->control->DoVerb (OLEIVERB_SHOW, 0, newControl->clientSite, 0, hwnd, &rect) == S_OK)
  301. {
  302. control = newControl;
  303. control->controlHWND = ActiveXHelpers::getHWND (this);
  304. if (control->controlHWND != 0)
  305. {
  306. control->setControlBounds (controlBounds);
  307. control->originalWndProc = (WNDPROC) GetWindowLongPtr ((HWND) control->controlHWND, GWLP_WNDPROC);
  308. SetWindowLongPtr ((HWND) control->controlHWND, GWLP_WNDPROC, (LONG_PTR) Pimpl::activeXHookWndProc);
  309. }
  310. return true;
  311. }
  312. }
  313. }
  314. }
  315. else
  316. {
  317. // the component must have already been added to a real window when you call this!
  318. jassertfalse;
  319. }
  320. return false;
  321. }
  322. void ActiveXControlComponent::deleteControl()
  323. {
  324. control = nullptr;
  325. }
  326. void* ActiveXControlComponent::queryInterface (const void* iid) const
  327. {
  328. void* result = nullptr;
  329. if (control != nullptr && control->control != nullptr
  330. && SUCCEEDED (control->control->QueryInterface (*(const IID*) iid, &result)))
  331. return result;
  332. return nullptr;
  333. }
  334. void ActiveXControlComponent::setMouseEventsAllowed (const bool eventsCanReachControl)
  335. {
  336. mouseEventsAllowed = eventsCanReachControl;
  337. }