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.

346 lines
11KB

  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. namespace DragAndDropHelpers
  16. {
  17. //==============================================================================
  18. struct JuceDropSource : public ComBaseClassHelper<IDropSource>
  19. {
  20. JuceDropSource() {}
  21. JUCE_COMRESULT QueryContinueDrag (BOOL escapePressed, DWORD keys) override
  22. {
  23. if (escapePressed)
  24. return DRAGDROP_S_CANCEL;
  25. if ((keys & (MK_LBUTTON | MK_RBUTTON)) == 0)
  26. return DRAGDROP_S_DROP;
  27. return S_OK;
  28. }
  29. JUCE_COMRESULT GiveFeedback (DWORD) override
  30. {
  31. return DRAGDROP_S_USEDEFAULTCURSORS;
  32. }
  33. };
  34. //==============================================================================
  35. struct JuceEnumFormatEtc : public ComBaseClassHelper<IEnumFORMATETC>
  36. {
  37. JuceEnumFormatEtc (const FORMATETC* f) : format (f) {}
  38. JUCE_COMRESULT Clone (IEnumFORMATETC** result) override
  39. {
  40. if (result == nullptr)
  41. return E_POINTER;
  42. auto newOne = new JuceEnumFormatEtc (format);
  43. newOne->index = index;
  44. *result = newOne;
  45. return S_OK;
  46. }
  47. JUCE_COMRESULT Next (ULONG celt, LPFORMATETC lpFormatEtc, ULONG* pceltFetched) override
  48. {
  49. if (pceltFetched != nullptr)
  50. *pceltFetched = 0;
  51. else if (celt != 1)
  52. return S_FALSE;
  53. if (index == 0 && celt > 0 && lpFormatEtc != nullptr)
  54. {
  55. copyFormatEtc (lpFormatEtc [0], *format);
  56. ++index;
  57. if (pceltFetched != nullptr)
  58. *pceltFetched = 1;
  59. return S_OK;
  60. }
  61. return S_FALSE;
  62. }
  63. JUCE_COMRESULT Skip (ULONG celt) override
  64. {
  65. if (index + (int) celt >= 1)
  66. return S_FALSE;
  67. index += (int) celt;
  68. return S_OK;
  69. }
  70. JUCE_COMRESULT Reset() override
  71. {
  72. index = 0;
  73. return S_OK;
  74. }
  75. private:
  76. const FORMATETC* const format;
  77. int index = 0;
  78. static void copyFormatEtc (FORMATETC& dest, const FORMATETC& source)
  79. {
  80. dest = source;
  81. if (source.ptd != nullptr)
  82. {
  83. dest.ptd = (DVTARGETDEVICE*) CoTaskMemAlloc (sizeof (DVTARGETDEVICE));
  84. *(dest.ptd) = *(source.ptd);
  85. }
  86. }
  87. JUCE_DECLARE_NON_COPYABLE (JuceEnumFormatEtc)
  88. };
  89. //==============================================================================
  90. class JuceDataObject : public ComBaseClassHelper <IDataObject>
  91. {
  92. public:
  93. JuceDataObject (const FORMATETC* f, const STGMEDIUM* m)
  94. : format (f), medium (m)
  95. {
  96. }
  97. ~JuceDataObject()
  98. {
  99. jassert (refCount == 0);
  100. }
  101. JUCE_COMRESULT GetData (FORMATETC* pFormatEtc, STGMEDIUM* pMedium)
  102. {
  103. if ((pFormatEtc->tymed & format->tymed) != 0
  104. && pFormatEtc->cfFormat == format->cfFormat
  105. && pFormatEtc->dwAspect == format->dwAspect)
  106. {
  107. pMedium->tymed = format->tymed;
  108. pMedium->pUnkForRelease = nullptr;
  109. if (format->tymed == TYMED_HGLOBAL)
  110. {
  111. auto len = GlobalSize (medium->hGlobal);
  112. void* const src = GlobalLock (medium->hGlobal);
  113. void* const dst = GlobalAlloc (GMEM_FIXED, len);
  114. memcpy (dst, src, len);
  115. GlobalUnlock (medium->hGlobal);
  116. pMedium->hGlobal = dst;
  117. return S_OK;
  118. }
  119. }
  120. return DV_E_FORMATETC;
  121. }
  122. JUCE_COMRESULT QueryGetData (FORMATETC* f)
  123. {
  124. if (f == nullptr)
  125. return E_INVALIDARG;
  126. if (f->tymed == format->tymed
  127. && f->cfFormat == format->cfFormat
  128. && f->dwAspect == format->dwAspect)
  129. return S_OK;
  130. return DV_E_FORMATETC;
  131. }
  132. JUCE_COMRESULT GetCanonicalFormatEtc (FORMATETC*, FORMATETC* pFormatEtcOut)
  133. {
  134. pFormatEtcOut->ptd = nullptr;
  135. return E_NOTIMPL;
  136. }
  137. JUCE_COMRESULT EnumFormatEtc (DWORD direction, IEnumFORMATETC** result)
  138. {
  139. if (result == nullptr)
  140. return E_POINTER;
  141. if (direction == DATADIR_GET)
  142. {
  143. *result = new JuceEnumFormatEtc (format);
  144. return S_OK;
  145. }
  146. *result = nullptr;
  147. return E_NOTIMPL;
  148. }
  149. JUCE_COMRESULT GetDataHere (FORMATETC*, STGMEDIUM*) { return DATA_E_FORMATETC; }
  150. JUCE_COMRESULT SetData (FORMATETC*, STGMEDIUM*, BOOL) { return E_NOTIMPL; }
  151. JUCE_COMRESULT DAdvise (FORMATETC*, DWORD, IAdviseSink*, DWORD*) { return OLE_E_ADVISENOTSUPPORTED; }
  152. JUCE_COMRESULT DUnadvise (DWORD) { return E_NOTIMPL; }
  153. JUCE_COMRESULT EnumDAdvise (IEnumSTATDATA**) { return OLE_E_ADVISENOTSUPPORTED; }
  154. private:
  155. const FORMATETC* const format;
  156. const STGMEDIUM* const medium;
  157. JUCE_DECLARE_NON_COPYABLE (JuceDataObject)
  158. };
  159. //==============================================================================
  160. HDROP createHDrop (const StringArray& fileNames)
  161. {
  162. size_t totalBytes = 0;
  163. for (int i = fileNames.size(); --i >= 0;)
  164. totalBytes += CharPointer_UTF16::getBytesRequiredFor (fileNames[i].getCharPointer()) + sizeof (WCHAR);
  165. HDROP hDrop = (HDROP) GlobalAlloc (GMEM_MOVEABLE | GMEM_ZEROINIT, sizeof (DROPFILES) + totalBytes + 4);
  166. if (hDrop != nullptr)
  167. {
  168. auto pDropFiles = (LPDROPFILES) GlobalLock (hDrop);
  169. pDropFiles->pFiles = sizeof (DROPFILES);
  170. pDropFiles->fWide = true;
  171. auto* fname = reinterpret_cast<WCHAR*> (addBytesToPointer (pDropFiles, sizeof (DROPFILES)));
  172. for (int i = 0; i < fileNames.size(); ++i)
  173. {
  174. auto bytesWritten = fileNames[i].copyToUTF16 (fname, 2048);
  175. fname = reinterpret_cast<WCHAR*> (addBytesToPointer (fname, bytesWritten));
  176. }
  177. *fname = 0;
  178. GlobalUnlock (hDrop);
  179. }
  180. return hDrop;
  181. }
  182. struct DragAndDropJob : public ThreadPoolJob
  183. {
  184. DragAndDropJob (FORMATETC f, STGMEDIUM m, DWORD d, std::function<void()>&& cb)
  185. : ThreadPoolJob ("DragAndDrop"),
  186. format (f), medium (m), whatToDo (d),
  187. completionCallback (std::move (cb))
  188. {
  189. }
  190. JobStatus runJob() override
  191. {
  192. OleInitialize (nullptr);
  193. auto source = new JuceDropSource();
  194. auto data = new JuceDataObject (&format, &medium);
  195. DWORD effect;
  196. DoDragDrop (data, source, whatToDo, &effect);
  197. data->Release();
  198. source->Release();
  199. OleUninitialize();
  200. if (completionCallback != nullptr)
  201. MessageManager::callAsync (std::move (completionCallback));
  202. return jobHasFinished;
  203. }
  204. FORMATETC format;
  205. STGMEDIUM medium;
  206. DWORD whatToDo;
  207. std::function<void()> completionCallback;
  208. };
  209. class ThreadPoolHolder : private DeletedAtShutdown
  210. {
  211. public:
  212. ThreadPoolHolder() = default;
  213. ~ThreadPoolHolder()
  214. {
  215. // Wait forever if there's a job running. The user needs to cancel the transfer
  216. // in the GUI.
  217. pool.removeAllJobs (true, -1);
  218. clearSingletonInstance();
  219. }
  220. JUCE_DECLARE_SINGLETON_SINGLETHREADED (ThreadPoolHolder, false)
  221. // We need to make sure we don't do simultaneous text and file drag and drops,
  222. // so use a pool that can only run a single job.
  223. ThreadPool pool { 1 };
  224. };
  225. JUCE_IMPLEMENT_SINGLETON (ThreadPoolHolder)
  226. }
  227. //==============================================================================
  228. bool DragAndDropContainer::performExternalDragDropOfFiles (const StringArray& files, const bool canMove,
  229. Component*, std::function<void()> callback)
  230. {
  231. if (files.isEmpty())
  232. return false;
  233. FORMATETC format = { CF_HDROP, nullptr, DVASPECT_CONTENT, -1, TYMED_HGLOBAL };
  234. STGMEDIUM medium = { TYMED_HGLOBAL, { nullptr }, nullptr };
  235. medium.hGlobal = DragAndDropHelpers::createHDrop (files);
  236. auto& pool = DragAndDropHelpers::ThreadPoolHolder::getInstance()->pool;
  237. pool.addJob (new DragAndDropHelpers::DragAndDropJob (format, medium,
  238. canMove ? (DROPEFFECT_COPY | DROPEFFECT_MOVE) : DROPEFFECT_COPY,
  239. std::move (callback)),
  240. true);
  241. return true;
  242. }
  243. bool DragAndDropContainer::performExternalDragDropOfText (const String& text, Component*, std::function<void()> callback)
  244. {
  245. if (text.isEmpty())
  246. return false;
  247. FORMATETC format = { CF_TEXT, nullptr, DVASPECT_CONTENT, -1, TYMED_HGLOBAL };
  248. STGMEDIUM medium = { TYMED_HGLOBAL, { nullptr }, nullptr };
  249. auto numBytes = CharPointer_UTF16::getBytesRequiredFor (text.getCharPointer());
  250. medium.hGlobal = GlobalAlloc (GMEM_MOVEABLE | GMEM_ZEROINIT, numBytes + 2);
  251. auto* data = static_cast<WCHAR*> (GlobalLock (medium.hGlobal));
  252. text.copyToUTF16 (data, numBytes + 2);
  253. format.cfFormat = CF_UNICODETEXT;
  254. GlobalUnlock (medium.hGlobal);
  255. auto& pool = DragAndDropHelpers::ThreadPoolHolder::getInstance()->pool;
  256. pool.addJob (new DragAndDropHelpers::DragAndDropJob (format,
  257. medium,
  258. DROPEFFECT_COPY | DROPEFFECT_MOVE,
  259. std::move (callback)),
  260. true);
  261. return true;
  262. }
  263. } // namespace juce