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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2022 - 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 7 End-User License
  8. Agreement and JUCE Privacy Policy.
  9. End User License Agreement: www.juce.com/juce-7-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. if (dest.ptd != nullptr)
  90. *(dest.ptd) = *(source.ptd);
  91. }
  92. }
  93. JUCE_DECLARE_NON_COPYABLE (JuceEnumFormatEtc)
  94. };
  95. //==============================================================================
  96. class JuceDataObject : public ComBaseClassHelper <IDataObject>
  97. {
  98. public:
  99. JuceDataObject (const FORMATETC* f, const STGMEDIUM* m)
  100. : format (f), medium (m)
  101. {
  102. }
  103. ~JuceDataObject()
  104. {
  105. jassert (refCount == 0);
  106. }
  107. JUCE_COMRESULT GetData (FORMATETC* pFormatEtc, STGMEDIUM* pMedium)
  108. {
  109. if ((pFormatEtc->tymed & format->tymed) != 0
  110. && pFormatEtc->cfFormat == format->cfFormat
  111. && pFormatEtc->dwAspect == format->dwAspect)
  112. {
  113. pMedium->tymed = format->tymed;
  114. pMedium->pUnkForRelease = nullptr;
  115. if (format->tymed == TYMED_HGLOBAL)
  116. {
  117. auto len = GlobalSize (medium->hGlobal);
  118. void* const src = GlobalLock (medium->hGlobal);
  119. void* const dst = GlobalAlloc (GMEM_FIXED, len);
  120. if (src != nullptr && dst != nullptr)
  121. memcpy (dst, src, len);
  122. GlobalUnlock (medium->hGlobal);
  123. pMedium->hGlobal = dst;
  124. return S_OK;
  125. }
  126. }
  127. return DV_E_FORMATETC;
  128. }
  129. JUCE_COMRESULT QueryGetData (FORMATETC* f)
  130. {
  131. if (f == nullptr)
  132. return E_INVALIDARG;
  133. if (f->tymed == format->tymed
  134. && f->cfFormat == format->cfFormat
  135. && f->dwAspect == format->dwAspect)
  136. return S_OK;
  137. return DV_E_FORMATETC;
  138. }
  139. JUCE_COMRESULT GetCanonicalFormatEtc (FORMATETC*, FORMATETC* pFormatEtcOut)
  140. {
  141. pFormatEtcOut->ptd = nullptr;
  142. return E_NOTIMPL;
  143. }
  144. JUCE_COMRESULT EnumFormatEtc (DWORD direction, IEnumFORMATETC** result)
  145. {
  146. if (result == nullptr)
  147. return E_POINTER;
  148. if (direction == DATADIR_GET)
  149. {
  150. *result = new JuceEnumFormatEtc (format);
  151. return S_OK;
  152. }
  153. *result = nullptr;
  154. return E_NOTIMPL;
  155. }
  156. JUCE_COMRESULT GetDataHere (FORMATETC*, STGMEDIUM*) { return DATA_E_FORMATETC; }
  157. JUCE_COMRESULT SetData (FORMATETC*, STGMEDIUM*, BOOL) { return E_NOTIMPL; }
  158. JUCE_COMRESULT DAdvise (FORMATETC*, DWORD, IAdviseSink*, DWORD*) { return OLE_E_ADVISENOTSUPPORTED; }
  159. JUCE_COMRESULT DUnadvise (DWORD) { return E_NOTIMPL; }
  160. JUCE_COMRESULT EnumDAdvise (IEnumSTATDATA**) { return OLE_E_ADVISENOTSUPPORTED; }
  161. private:
  162. const FORMATETC* const format;
  163. const STGMEDIUM* const medium;
  164. JUCE_DECLARE_NON_COPYABLE (JuceDataObject)
  165. };
  166. //==============================================================================
  167. static HDROP createHDrop (const StringArray& fileNames)
  168. {
  169. size_t totalBytes = 0;
  170. for (int i = fileNames.size(); --i >= 0;)
  171. totalBytes += CharPointer_UTF16::getBytesRequiredFor (fileNames[i].getCharPointer()) + sizeof (WCHAR);
  172. struct Deleter
  173. {
  174. void operator() (void* ptr) const noexcept { GlobalFree (ptr); }
  175. };
  176. auto hDrop = std::unique_ptr<void, Deleter> ((HDROP) GlobalAlloc (GMEM_MOVEABLE | GMEM_ZEROINIT, sizeof (DROPFILES) + totalBytes + 4));
  177. if (hDrop != nullptr)
  178. {
  179. auto pDropFiles = (LPDROPFILES) GlobalLock (hDrop.get());
  180. if (pDropFiles == nullptr)
  181. return nullptr;
  182. pDropFiles->pFiles = sizeof (DROPFILES);
  183. pDropFiles->fWide = true;
  184. auto* fname = reinterpret_cast<WCHAR*> (addBytesToPointer (pDropFiles, sizeof (DROPFILES)));
  185. for (int i = 0; i < fileNames.size(); ++i)
  186. {
  187. auto bytesWritten = fileNames[i].copyToUTF16 (fname, 2048);
  188. fname = reinterpret_cast<WCHAR*> (addBytesToPointer (fname, bytesWritten));
  189. }
  190. *fname = 0;
  191. GlobalUnlock (hDrop.get());
  192. }
  193. return static_cast<HDROP> (hDrop.release());
  194. }
  195. struct DragAndDropJob : public ThreadPoolJob
  196. {
  197. DragAndDropJob (FORMATETC f, STGMEDIUM m, DWORD d, std::function<void()>&& cb)
  198. : ThreadPoolJob ("DragAndDrop"),
  199. format (f), medium (m), whatToDo (d),
  200. completionCallback (std::move (cb))
  201. {
  202. }
  203. JobStatus runJob() override
  204. {
  205. ignoreUnused (OleInitialize (nullptr));
  206. auto* source = new JuceDropSource();
  207. auto* data = new JuceDataObject (&format, &medium);
  208. DWORD effect;
  209. DoDragDrop (data, source, whatToDo, &effect);
  210. data->Release();
  211. source->Release();
  212. OleUninitialize();
  213. if (completionCallback != nullptr)
  214. MessageManager::callAsync (std::move (completionCallback));
  215. return jobHasFinished;
  216. }
  217. FORMATETC format;
  218. STGMEDIUM medium;
  219. DWORD whatToDo;
  220. std::function<void()> completionCallback;
  221. };
  222. class ThreadPoolHolder : private DeletedAtShutdown
  223. {
  224. public:
  225. ThreadPoolHolder() = default;
  226. ~ThreadPoolHolder()
  227. {
  228. // Wait forever if there's a job running. The user needs to cancel the transfer
  229. // in the GUI.
  230. pool.removeAllJobs (true, -1);
  231. clearSingletonInstance();
  232. }
  233. JUCE_DECLARE_SINGLETON_SINGLETHREADED (ThreadPoolHolder, false)
  234. // We need to make sure we don't do simultaneous text and file drag and drops,
  235. // so use a pool that can only run a single job.
  236. ThreadPool pool { 1 };
  237. };
  238. JUCE_IMPLEMENT_SINGLETON (ThreadPoolHolder)
  239. }
  240. //==============================================================================
  241. bool DragAndDropContainer::performExternalDragDropOfFiles (const StringArray& files, const bool canMove,
  242. Component*, std::function<void()> callback)
  243. {
  244. if (files.isEmpty())
  245. return false;
  246. FORMATETC format = { CF_HDROP, nullptr, DVASPECT_CONTENT, -1, TYMED_HGLOBAL };
  247. STGMEDIUM medium = { TYMED_HGLOBAL, { nullptr }, nullptr };
  248. medium.hGlobal = DragAndDropHelpers::createHDrop (files);
  249. auto& pool = DragAndDropHelpers::ThreadPoolHolder::getInstance()->pool;
  250. pool.addJob (new DragAndDropHelpers::DragAndDropJob (format, medium,
  251. canMove ? (DROPEFFECT_COPY | DROPEFFECT_MOVE) : DROPEFFECT_COPY,
  252. std::move (callback)),
  253. true);
  254. return true;
  255. }
  256. bool DragAndDropContainer::performExternalDragDropOfText (const String& text, Component*, std::function<void()> callback)
  257. {
  258. if (text.isEmpty())
  259. return false;
  260. FORMATETC format = { CF_TEXT, nullptr, DVASPECT_CONTENT, -1, TYMED_HGLOBAL };
  261. STGMEDIUM medium = { TYMED_HGLOBAL, { nullptr }, nullptr };
  262. auto numBytes = CharPointer_UTF16::getBytesRequiredFor (text.getCharPointer());
  263. medium.hGlobal = GlobalAlloc (GMEM_MOVEABLE | GMEM_ZEROINIT, numBytes + 2);
  264. if (medium.hGlobal == nullptr)
  265. return false;
  266. auto* data = static_cast<WCHAR*> (GlobalLock (medium.hGlobal));
  267. text.copyToUTF16 (data, numBytes + 2);
  268. format.cfFormat = CF_UNICODETEXT;
  269. GlobalUnlock (medium.hGlobal);
  270. auto& pool = DragAndDropHelpers::ThreadPoolHolder::getInstance()->pool;
  271. pool.addJob (new DragAndDropHelpers::DragAndDropJob (format,
  272. medium,
  273. DROPEFFECT_COPY | DROPEFFECT_MOVE,
  274. std::move (callback)),
  275. true);
  276. return true;
  277. }
  278. } // namespace juce