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_DragAndDrop.cpp 11KB

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