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.

290 lines
9.3KB

  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 (JuceDropSource* s, const FORMATETC* f, const STGMEDIUM* m)
  100. : dropSource (s), 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. JuceDropSource* const dropSource;
  162. const FORMATETC* const format;
  163. const STGMEDIUM* const medium;
  164. JUCE_DECLARE_NON_COPYABLE (JuceDataObject)
  165. };
  166. //==============================================================================
  167. HDROP createHDrop (const StringArray& fileNames)
  168. {
  169. int totalBytes = 0;
  170. for (int i = fileNames.size(); --i >= 0;)
  171. totalBytes += (int) CharPointer_UTF16::getBytesRequiredFor (fileNames[i].getCharPointer()) + sizeof (WCHAR);
  172. HDROP hDrop = (HDROP) GlobalAlloc (GMEM_MOVEABLE | GMEM_ZEROINIT, sizeof (DROPFILES) + totalBytes + 4);
  173. if (hDrop != 0)
  174. {
  175. auto pDropFiles = (LPDROPFILES) GlobalLock (hDrop);
  176. pDropFiles->pFiles = sizeof (DROPFILES);
  177. pDropFiles->fWide = true;
  178. auto* fname = reinterpret_cast<WCHAR*> (addBytesToPointer (pDropFiles, sizeof (DROPFILES)));
  179. for (int i = 0; i < fileNames.size(); ++i)
  180. {
  181. auto bytesWritten = fileNames[i].copyToUTF16 (fname, 2048);
  182. fname = reinterpret_cast<WCHAR*> (addBytesToPointer (fname, bytesWritten));
  183. }
  184. *fname = 0;
  185. GlobalUnlock (hDrop);
  186. }
  187. return hDrop;
  188. }
  189. bool performDragDrop (FORMATETC* const format, STGMEDIUM* const medium, const DWORD whatToDo)
  190. {
  191. auto source = new JuceDropSource();
  192. auto data = new JuceDataObject (source, format, medium);
  193. DWORD effect;
  194. auto res = DoDragDrop (data, source, whatToDo, &effect);
  195. data->Release();
  196. source->Release();
  197. return res == DRAGDROP_S_DROP;
  198. }
  199. }
  200. //==============================================================================
  201. bool DragAndDropContainer::performExternalDragDropOfFiles (const StringArray& files, const bool canMove, Component*)
  202. {
  203. FORMATETC format = { CF_HDROP, 0, DVASPECT_CONTENT, -1, TYMED_HGLOBAL };
  204. STGMEDIUM medium = { TYMED_HGLOBAL, { 0 }, 0 };
  205. medium.hGlobal = DragAndDropHelpers::createHDrop (files);
  206. return DragAndDropHelpers::performDragDrop (&format, &medium, canMove ? (DWORD) (DROPEFFECT_COPY | DROPEFFECT_MOVE)
  207. : (DWORD) DROPEFFECT_COPY);
  208. }
  209. bool DragAndDropContainer::performExternalDragDropOfText (const String& text, Component*)
  210. {
  211. FORMATETC format = { CF_TEXT, 0, DVASPECT_CONTENT, -1, TYMED_HGLOBAL };
  212. STGMEDIUM medium = { TYMED_HGLOBAL, { 0 }, 0 };
  213. auto numBytes = CharPointer_UTF16::getBytesRequiredFor (text.getCharPointer());
  214. medium.hGlobal = GlobalAlloc (GMEM_MOVEABLE | GMEM_ZEROINIT, numBytes + 2);
  215. WCHAR* const data = static_cast<WCHAR*> (GlobalLock (medium.hGlobal));
  216. text.copyToUTF16 (data, numBytes);
  217. format.cfFormat = CF_UNICODETEXT;
  218. GlobalUnlock (medium.hGlobal);
  219. return DragAndDropHelpers::performDragDrop (&format, &medium, DROPEFFECT_COPY | DROPEFFECT_MOVE);
  220. }
  221. } // namespace juce