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.

295 lines
9.7KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. namespace DragAndDropHelpers
  19. {
  20. //==============================================================================
  21. class JuceDropSource : public ComBaseClassHelper <IDropSource>
  22. {
  23. public:
  24. JuceDropSource() {}
  25. JUCE_COMRESULT QueryContinueDrag (BOOL escapePressed, DWORD keys)
  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)
  34. {
  35. return DRAGDROP_S_USEDEFAULTCURSORS;
  36. }
  37. };
  38. //==============================================================================
  39. class JuceEnumFormatEtc : public ComBaseClassHelper <IEnumFORMATETC>
  40. {
  41. public:
  42. JuceEnumFormatEtc (const FORMATETC* const format_)
  43. : format (format_),
  44. index (0)
  45. {
  46. }
  47. JUCE_COMRESULT Clone (IEnumFORMATETC** result)
  48. {
  49. if (result == 0)
  50. return E_POINTER;
  51. JuceEnumFormatEtc* const newOne = new JuceEnumFormatEtc (format);
  52. newOne->index = index;
  53. *result = newOne;
  54. return S_OK;
  55. }
  56. JUCE_COMRESULT Next (ULONG celt, LPFORMATETC lpFormatEtc, ULONG* pceltFetched)
  57. {
  58. if (pceltFetched != nullptr)
  59. *pceltFetched = 0;
  60. else if (celt != 1)
  61. return S_FALSE;
  62. if (index == 0 && celt > 0 && lpFormatEtc != 0)
  63. {
  64. copyFormatEtc (lpFormatEtc [0], *format);
  65. ++index;
  66. if (pceltFetched != nullptr)
  67. *pceltFetched = 1;
  68. return S_OK;
  69. }
  70. return S_FALSE;
  71. }
  72. JUCE_COMRESULT Skip (ULONG celt)
  73. {
  74. if (index + (int) celt >= 1)
  75. return S_FALSE;
  76. index += celt;
  77. return S_OK;
  78. }
  79. JUCE_COMRESULT Reset()
  80. {
  81. index = 0;
  82. return S_OK;
  83. }
  84. private:
  85. const FORMATETC* const format;
  86. int index;
  87. static void copyFormatEtc (FORMATETC& dest, const FORMATETC& source)
  88. {
  89. dest = source;
  90. if (source.ptd != 0)
  91. {
  92. dest.ptd = (DVTARGETDEVICE*) CoTaskMemAlloc (sizeof (DVTARGETDEVICE));
  93. *(dest.ptd) = *(source.ptd);
  94. }
  95. }
  96. JUCE_DECLARE_NON_COPYABLE (JuceEnumFormatEtc);
  97. };
  98. //==============================================================================
  99. class JuceDataObject : public ComBaseClassHelper <IDataObject>
  100. {
  101. public:
  102. JuceDataObject (JuceDropSource* const dropSource_,
  103. const FORMATETC* const format_,
  104. const STGMEDIUM* const medium_)
  105. : dropSource (dropSource_),
  106. format (format_),
  107. medium (medium_)
  108. {
  109. }
  110. ~JuceDataObject()
  111. {
  112. jassert (refCount == 0);
  113. }
  114. JUCE_COMRESULT GetData (FORMATETC* pFormatEtc, STGMEDIUM* pMedium)
  115. {
  116. if ((pFormatEtc->tymed & format->tymed) != 0
  117. && pFormatEtc->cfFormat == format->cfFormat
  118. && pFormatEtc->dwAspect == format->dwAspect)
  119. {
  120. pMedium->tymed = format->tymed;
  121. pMedium->pUnkForRelease = 0;
  122. if (format->tymed == TYMED_HGLOBAL)
  123. {
  124. const SIZE_T len = GlobalSize (medium->hGlobal);
  125. void* const src = GlobalLock (medium->hGlobal);
  126. void* const dst = GlobalAlloc (GMEM_FIXED, len);
  127. memcpy (dst, src, len);
  128. GlobalUnlock (medium->hGlobal);
  129. pMedium->hGlobal = dst;
  130. return S_OK;
  131. }
  132. }
  133. return DV_E_FORMATETC;
  134. }
  135. JUCE_COMRESULT QueryGetData (FORMATETC* f)
  136. {
  137. if (f == 0)
  138. return E_INVALIDARG;
  139. if (f->tymed == format->tymed
  140. && f->cfFormat == format->cfFormat
  141. && f->dwAspect == format->dwAspect)
  142. return S_OK;
  143. return DV_E_FORMATETC;
  144. }
  145. JUCE_COMRESULT GetCanonicalFormatEtc (FORMATETC*, FORMATETC* pFormatEtcOut)
  146. {
  147. pFormatEtcOut->ptd = 0;
  148. return E_NOTIMPL;
  149. }
  150. JUCE_COMRESULT EnumFormatEtc (DWORD direction, IEnumFORMATETC** result)
  151. {
  152. if (result == 0)
  153. return E_POINTER;
  154. if (direction == DATADIR_GET)
  155. {
  156. *result = new JuceEnumFormatEtc (format);
  157. return S_OK;
  158. }
  159. *result = 0;
  160. return E_NOTIMPL;
  161. }
  162. JUCE_COMRESULT GetDataHere (FORMATETC*, STGMEDIUM*) { return DATA_E_FORMATETC; }
  163. JUCE_COMRESULT SetData (FORMATETC*, STGMEDIUM*, BOOL) { return E_NOTIMPL; }
  164. JUCE_COMRESULT DAdvise (FORMATETC*, DWORD, IAdviseSink*, DWORD*) { return OLE_E_ADVISENOTSUPPORTED; }
  165. JUCE_COMRESULT DUnadvise (DWORD) { return E_NOTIMPL; }
  166. JUCE_COMRESULT EnumDAdvise (IEnumSTATDATA**) { return OLE_E_ADVISENOTSUPPORTED; }
  167. private:
  168. JuceDropSource* const dropSource;
  169. const FORMATETC* const format;
  170. const STGMEDIUM* const medium;
  171. JUCE_DECLARE_NON_COPYABLE (JuceDataObject);
  172. };
  173. //==============================================================================
  174. HDROP createHDrop (const StringArray& fileNames)
  175. {
  176. int totalBytes = 0;
  177. for (int i = fileNames.size(); --i >= 0;)
  178. totalBytes += (int) CharPointer_UTF16::getBytesRequiredFor (fileNames[i].getCharPointer()) + sizeof (WCHAR);
  179. HDROP hDrop = (HDROP) GlobalAlloc (GMEM_MOVEABLE | GMEM_ZEROINIT, sizeof (DROPFILES) + totalBytes + 4);
  180. if (hDrop != 0)
  181. {
  182. LPDROPFILES pDropFiles = (LPDROPFILES) GlobalLock (hDrop);
  183. pDropFiles->pFiles = sizeof (DROPFILES);
  184. pDropFiles->fWide = true;
  185. WCHAR* fname = reinterpret_cast<WCHAR*> (addBytesToPointer (pDropFiles, sizeof (DROPFILES)));
  186. for (int i = 0; i < fileNames.size(); ++i)
  187. {
  188. const int bytesWritten = fileNames[i].copyToUTF16 (fname, 2048);
  189. fname = reinterpret_cast<WCHAR*> (addBytesToPointer (fname, bytesWritten));
  190. }
  191. *fname = 0;
  192. GlobalUnlock (hDrop);
  193. }
  194. return hDrop;
  195. }
  196. bool performDragDrop (FORMATETC* const format, STGMEDIUM* const medium, const DWORD whatToDo)
  197. {
  198. JuceDropSource* const source = new JuceDropSource();
  199. JuceDataObject* const data = new JuceDataObject (source, format, medium);
  200. DWORD effect;
  201. const HRESULT res = DoDragDrop (data, source, whatToDo, &effect);
  202. data->Release();
  203. source->Release();
  204. return res == DRAGDROP_S_DROP;
  205. }
  206. }
  207. //==============================================================================
  208. bool DragAndDropContainer::performExternalDragDropOfFiles (const StringArray& files, const bool canMove)
  209. {
  210. FORMATETC format = { CF_HDROP, 0, DVASPECT_CONTENT, -1, TYMED_HGLOBAL };
  211. STGMEDIUM medium = { TYMED_HGLOBAL, { 0 }, 0 };
  212. medium.hGlobal = DragAndDropHelpers::createHDrop (files);
  213. return DragAndDropHelpers::performDragDrop (&format, &medium, canMove ? (DWORD) (DROPEFFECT_COPY | DROPEFFECT_MOVE)
  214. : (DWORD) DROPEFFECT_COPY);
  215. }
  216. bool DragAndDropContainer::performExternalDragDropOfText (const String& text)
  217. {
  218. FORMATETC format = { CF_TEXT, 0, DVASPECT_CONTENT, -1, TYMED_HGLOBAL };
  219. STGMEDIUM medium = { TYMED_HGLOBAL, { 0 }, 0 };
  220. const size_t numBytes = CharPointer_UTF16::getBytesRequiredFor (text.getCharPointer());
  221. medium.hGlobal = GlobalAlloc (GMEM_MOVEABLE | GMEM_ZEROINIT, numBytes + 2);
  222. WCHAR* const data = static_cast <WCHAR*> (GlobalLock (medium.hGlobal));
  223. text.copyToUTF16 (data, (int) numBytes);
  224. format.cfFormat = CF_UNICODETEXT;
  225. GlobalUnlock (medium.hGlobal);
  226. return DragAndDropHelpers::performDragDrop (&format, &medium, DROPEFFECT_COPY | DROPEFFECT_MOVE);
  227. }