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.

285 lines
9.2KB

  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 DragAndDropHelpers
  20. {
  21. //==============================================================================
  22. struct JuceDropSource : public ComBaseClassHelper<IDropSource>
  23. {
  24. JuceDropSource() {}
  25. JUCE_COMRESULT QueryContinueDrag (BOOL escapePressed, DWORD keys) override
  26. {
  27. if (escapePressed)
  28. return DRAGDROP_S_CANCEL;
  29. if ((keys & (MK_LBUTTON | MK_RBUTTON)) == 0)
  30. return DRAGDROP_S_DROP;
  31. return S_OK;
  32. }
  33. JUCE_COMRESULT GiveFeedback (DWORD) override
  34. {
  35. return DRAGDROP_S_USEDEFAULTCURSORS;
  36. }
  37. };
  38. //==============================================================================
  39. struct JuceEnumFormatEtc : public ComBaseClassHelper<IEnumFORMATETC>
  40. {
  41. JuceEnumFormatEtc (const FORMATETC* f) : format (f) {}
  42. JUCE_COMRESULT Clone (IEnumFORMATETC** result) override
  43. {
  44. if (result == 0)
  45. return E_POINTER;
  46. auto newOne = new JuceEnumFormatEtc (format);
  47. newOne->index = index;
  48. *result = newOne;
  49. return S_OK;
  50. }
  51. JUCE_COMRESULT Next (ULONG celt, LPFORMATETC lpFormatEtc, ULONG* pceltFetched) override
  52. {
  53. if (pceltFetched != nullptr)
  54. *pceltFetched = 0;
  55. else if (celt != 1)
  56. return S_FALSE;
  57. if (index == 0 && celt > 0 && lpFormatEtc != 0)
  58. {
  59. copyFormatEtc (lpFormatEtc [0], *format);
  60. ++index;
  61. if (pceltFetched != nullptr)
  62. *pceltFetched = 1;
  63. return S_OK;
  64. }
  65. return S_FALSE;
  66. }
  67. JUCE_COMRESULT Skip (ULONG celt) override
  68. {
  69. if (index + (int) celt >= 1)
  70. return S_FALSE;
  71. index += celt;
  72. return S_OK;
  73. }
  74. JUCE_COMRESULT Reset() override
  75. {
  76. index = 0;
  77. return S_OK;
  78. }
  79. private:
  80. const FORMATETC* const format;
  81. int index = 0;
  82. static void copyFormatEtc (FORMATETC& dest, const FORMATETC& source)
  83. {
  84. dest = source;
  85. if (source.ptd != 0)
  86. {
  87. dest.ptd = (DVTARGETDEVICE*) CoTaskMemAlloc (sizeof (DVTARGETDEVICE));
  88. *(dest.ptd) = *(source.ptd);
  89. }
  90. }
  91. JUCE_DECLARE_NON_COPYABLE (JuceEnumFormatEtc)
  92. };
  93. //==============================================================================
  94. class JuceDataObject : public ComBaseClassHelper <IDataObject>
  95. {
  96. public:
  97. JuceDataObject (JuceDropSource* s, const FORMATETC* f, const STGMEDIUM* m)
  98. : dropSource (s), format (f), medium (m)
  99. {
  100. }
  101. ~JuceDataObject()
  102. {
  103. jassert (refCount == 0);
  104. }
  105. JUCE_COMRESULT GetData (FORMATETC* pFormatEtc, STGMEDIUM* pMedium)
  106. {
  107. if ((pFormatEtc->tymed & format->tymed) != 0
  108. && pFormatEtc->cfFormat == format->cfFormat
  109. && pFormatEtc->dwAspect == format->dwAspect)
  110. {
  111. pMedium->tymed = format->tymed;
  112. pMedium->pUnkForRelease = 0;
  113. if (format->tymed == TYMED_HGLOBAL)
  114. {
  115. auto len = GlobalSize (medium->hGlobal);
  116. void* const src = GlobalLock (medium->hGlobal);
  117. void* const dst = GlobalAlloc (GMEM_FIXED, len);
  118. memcpy (dst, src, len);
  119. GlobalUnlock (medium->hGlobal);
  120. pMedium->hGlobal = dst;
  121. return S_OK;
  122. }
  123. }
  124. return DV_E_FORMATETC;
  125. }
  126. JUCE_COMRESULT QueryGetData (FORMATETC* f)
  127. {
  128. if (f == 0)
  129. return E_INVALIDARG;
  130. if (f->tymed == format->tymed
  131. && f->cfFormat == format->cfFormat
  132. && f->dwAspect == format->dwAspect)
  133. return S_OK;
  134. return DV_E_FORMATETC;
  135. }
  136. JUCE_COMRESULT GetCanonicalFormatEtc (FORMATETC*, FORMATETC* pFormatEtcOut)
  137. {
  138. pFormatEtcOut->ptd = 0;
  139. return E_NOTIMPL;
  140. }
  141. JUCE_COMRESULT EnumFormatEtc (DWORD direction, IEnumFORMATETC** result)
  142. {
  143. if (result == 0)
  144. return E_POINTER;
  145. if (direction == DATADIR_GET)
  146. {
  147. *result = new JuceEnumFormatEtc (format);
  148. return S_OK;
  149. }
  150. *result = 0;
  151. return E_NOTIMPL;
  152. }
  153. JUCE_COMRESULT GetDataHere (FORMATETC*, STGMEDIUM*) { return DATA_E_FORMATETC; }
  154. JUCE_COMRESULT SetData (FORMATETC*, STGMEDIUM*, BOOL) { return E_NOTIMPL; }
  155. JUCE_COMRESULT DAdvise (FORMATETC*, DWORD, IAdviseSink*, DWORD*) { return OLE_E_ADVISENOTSUPPORTED; }
  156. JUCE_COMRESULT DUnadvise (DWORD) { return E_NOTIMPL; }
  157. JUCE_COMRESULT EnumDAdvise (IEnumSTATDATA**) { return OLE_E_ADVISENOTSUPPORTED; }
  158. private:
  159. JuceDropSource* const dropSource;
  160. const FORMATETC* const format;
  161. const STGMEDIUM* const medium;
  162. JUCE_DECLARE_NON_COPYABLE (JuceDataObject)
  163. };
  164. //==============================================================================
  165. HDROP createHDrop (const StringArray& fileNames)
  166. {
  167. int totalBytes = 0;
  168. for (int i = fileNames.size(); --i >= 0;)
  169. totalBytes += (int) CharPointer_UTF16::getBytesRequiredFor (fileNames[i].getCharPointer()) + sizeof (WCHAR);
  170. HDROP hDrop = (HDROP) GlobalAlloc (GMEM_MOVEABLE | GMEM_ZEROINIT, sizeof (DROPFILES) + totalBytes + 4);
  171. if (hDrop != 0)
  172. {
  173. auto pDropFiles = (LPDROPFILES) GlobalLock (hDrop);
  174. pDropFiles->pFiles = sizeof (DROPFILES);
  175. pDropFiles->fWide = true;
  176. auto* fname = reinterpret_cast<WCHAR*> (addBytesToPointer (pDropFiles, sizeof (DROPFILES)));
  177. for (int i = 0; i < fileNames.size(); ++i)
  178. {
  179. auto bytesWritten = fileNames[i].copyToUTF16 (fname, 2048);
  180. fname = reinterpret_cast<WCHAR*> (addBytesToPointer (fname, bytesWritten));
  181. }
  182. *fname = 0;
  183. GlobalUnlock (hDrop);
  184. }
  185. return hDrop;
  186. }
  187. bool performDragDrop (FORMATETC* const format, STGMEDIUM* const medium, const DWORD whatToDo)
  188. {
  189. auto source = new JuceDropSource();
  190. auto data = new JuceDataObject (source, format, medium);
  191. DWORD effect;
  192. auto res = DoDragDrop (data, source, whatToDo, &effect);
  193. data->Release();
  194. source->Release();
  195. return res == DRAGDROP_S_DROP;
  196. }
  197. }
  198. //==============================================================================
  199. bool DragAndDropContainer::performExternalDragDropOfFiles (const StringArray& files, const bool canMove, Component*)
  200. {
  201. FORMATETC format = { CF_HDROP, 0, DVASPECT_CONTENT, -1, TYMED_HGLOBAL };
  202. STGMEDIUM medium = { TYMED_HGLOBAL, { 0 }, 0 };
  203. medium.hGlobal = DragAndDropHelpers::createHDrop (files);
  204. return DragAndDropHelpers::performDragDrop (&format, &medium, canMove ? (DWORD) (DROPEFFECT_COPY | DROPEFFECT_MOVE)
  205. : (DWORD) DROPEFFECT_COPY);
  206. }
  207. bool DragAndDropContainer::performExternalDragDropOfText (const String& text, Component*)
  208. {
  209. FORMATETC format = { CF_TEXT, 0, DVASPECT_CONTENT, -1, TYMED_HGLOBAL };
  210. STGMEDIUM medium = { TYMED_HGLOBAL, { 0 }, 0 };
  211. auto numBytes = CharPointer_UTF16::getBytesRequiredFor (text.getCharPointer());
  212. medium.hGlobal = GlobalAlloc (GMEM_MOVEABLE | GMEM_ZEROINIT, numBytes + 2);
  213. WCHAR* const data = static_cast<WCHAR*> (GlobalLock (medium.hGlobal));
  214. text.copyToUTF16 (data, numBytes);
  215. format.cfFormat = CF_UNICODETEXT;
  216. GlobalUnlock (medium.hGlobal);
  217. return DragAndDropHelpers::performDragDrop (&format, &medium, DROPEFFECT_COPY | DROPEFFECT_MOVE);
  218. }