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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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