juce_win32_DragAndDrop.cpp 11 KB

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