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.

296 lines
9.5KB

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