The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
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.

362 lines
11KB

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