BsWin32DropTarget.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #pragma once
  4. #include "Shellapi.h"
  5. // This is just a helper include for BsPlatformImpl.cpp, it's not meant to be used on its own
  6. namespace BansheeEngine
  7. {
  8. /** @cond INTERNAL */
  9. /** @addtogroup Platform
  10. * @{
  11. */
  12. /**
  13. * Called by the OS when various drag and drop actions are performed over a window this control is registered for.
  14. *
  15. * @note
  16. * This class queues all messages receives by the OS (from the core thread), and then executes the queue on sim thread.
  17. * You should be wary of which methods are allowed to be called from which thread.
  18. */
  19. class Win32DropTarget : public IDropTarget
  20. {
  21. /** Type of drag and drop event. */
  22. enum class DropOpType
  23. {
  24. DragOver,
  25. Drop,
  26. Leave
  27. };
  28. /** Type of data that a drag and drop operation contains. */
  29. enum class DropOpDataType
  30. {
  31. FileList,
  32. None
  33. };
  34. /** Structure describing a drag and drop operation. */
  35. struct DropTargetOp
  36. {
  37. DropTargetOp(DropOpType _type, const Vector2I& _pos)
  38. :type(_type), position(_pos), dataType(DropOpDataType::None)
  39. { }
  40. DropOpType type;
  41. Vector2I position;
  42. DropOpDataType dataType;
  43. union
  44. {
  45. Vector<WString>* mFileList;
  46. };
  47. };
  48. public:
  49. Win32DropTarget(HWND hWnd)
  50. :mHWnd(hWnd), mRefCount(1), mAcceptDrag(false)
  51. { }
  52. ~Win32DropTarget()
  53. {
  54. BS_LOCK_MUTEX(mSync);
  55. for(auto& fileList : mFileLists)
  56. {
  57. bs_delete(fileList);
  58. }
  59. mFileLists.clear();
  60. mQueuedDropOps.clear();
  61. }
  62. /** Registers the drop target with the operating system. Monitoring for drag and drop operations starts. */
  63. void registerWithOS()
  64. {
  65. CoLockObjectExternal(this, TRUE, FALSE);
  66. HRESULT hr = RegisterDragDrop(mHWnd, this);
  67. }
  68. /** Unregisters the drop target with the operating system. Monitoring for drag and drop operations stops. */
  69. void unregisterWithOS()
  70. {
  71. RevokeDragDrop(mHWnd);
  72. CoLockObjectExternal(this, FALSE, FALSE);
  73. }
  74. /** COM requirement. Returns instance of an interface of the provided type. */
  75. HRESULT __stdcall QueryInterface(REFIID iid, void** ppvObject) override
  76. {
  77. if(iid == IID_IDropTarget || iid == IID_IUnknown)
  78. {
  79. AddRef();
  80. *ppvObject = this;
  81. return S_OK;
  82. }
  83. else
  84. {
  85. *ppvObject = nullptr;
  86. return E_NOINTERFACE;
  87. }
  88. }
  89. /** COM requirement. Increments objects reference count. */
  90. ULONG __stdcall AddRef() override
  91. {
  92. return InterlockedIncrement(&mRefCount);
  93. }
  94. /** COM requirement. Decreases the objects reference count and deletes the object if its zero. */
  95. ULONG __stdcall Release() override
  96. {
  97. LONG count = InterlockedDecrement(&mRefCount);
  98. if(count == 0)
  99. {
  100. bs_delete(this);
  101. return 0;
  102. }
  103. else
  104. {
  105. return count;
  106. }
  107. }
  108. /**
  109. * Called by the OS when user enters the drop target area while dragging an object.
  110. *
  111. * @note Called on core thread.
  112. */
  113. HRESULT __stdcall DragEnter(IDataObject* pDataObj, DWORD grfKeyState, POINTL pt, DWORD* pdwEffect) override
  114. {
  115. *pdwEffect = DROPEFFECT_LINK;
  116. mAcceptDrag = isDataValid(pDataObj);
  117. if(!mAcceptDrag)
  118. return S_OK;
  119. {
  120. BS_LOCK_MUTEX(mSync);
  121. mFileLists.push_back(getFileListFromData(pDataObj));
  122. ScreenToClient(mHWnd, (POINT *)&pt);
  123. mQueuedDropOps.push_back(DropTargetOp(DropOpType::DragOver, Vector2I((int)pt.x, (int)pt.y)));
  124. DropTargetOp& op = mQueuedDropOps.back();
  125. op.dataType = DropOpDataType::FileList;
  126. op.mFileList = mFileLists.back();
  127. }
  128. return S_OK;
  129. }
  130. /**
  131. * Called by the OS while user continues to drag an object over the drop target.
  132. *
  133. * @note Called on core thread.
  134. */
  135. HRESULT __stdcall DragOver(DWORD grfKeyState, POINTL pt, DWORD* pdwEffect) override
  136. {
  137. *pdwEffect = DROPEFFECT_LINK;
  138. if(!mAcceptDrag)
  139. return S_OK;
  140. {
  141. BS_LOCK_MUTEX(mSync);
  142. ScreenToClient(mHWnd, (POINT *)&pt);
  143. mQueuedDropOps.push_back(DropTargetOp(DropOpType::DragOver, Vector2I((int)pt.x, (int)pt.y)));
  144. DropTargetOp& op = mQueuedDropOps.back();
  145. op.dataType = DropOpDataType::FileList;
  146. op.mFileList = mFileLists.back();
  147. }
  148. return S_OK;
  149. }
  150. /**
  151. * Called by the OS when user leaves the drop target.
  152. *
  153. * @note Called on core thread.
  154. */
  155. HRESULT __stdcall DragLeave() override
  156. {
  157. {
  158. BS_LOCK_MUTEX(mSync);
  159. mQueuedDropOps.push_back(DropTargetOp(DropOpType::Leave, Vector2I()));
  160. DropTargetOp& op = mQueuedDropOps.back();
  161. op.dataType = DropOpDataType::FileList;
  162. op.mFileList = mFileLists.back();
  163. }
  164. return S_OK;
  165. }
  166. /**
  167. * Called by the OS when the user ends the drag operation while over the drop target.
  168. *
  169. * @note Called on core thread.
  170. */
  171. HRESULT __stdcall Drop(IDataObject* pDataObj, DWORD grfKeyState, POINTL pt, DWORD* pdwEffect) override
  172. {
  173. *pdwEffect = DROPEFFECT_LINK;
  174. mAcceptDrag = false;
  175. if(!isDataValid(pDataObj))
  176. return S_OK;
  177. {
  178. BS_LOCK_MUTEX(mSync);
  179. mFileLists.push_back(getFileListFromData(pDataObj));
  180. ScreenToClient(mHWnd, (POINT *)&pt);
  181. mQueuedDropOps.push_back(DropTargetOp(DropOpType::Drop, Vector2I((int)pt.x, (int)pt.y)));
  182. DropTargetOp& op = mQueuedDropOps.back();
  183. op.dataType = DropOpDataType::FileList;
  184. op.mFileList = mFileLists.back();
  185. }
  186. return S_OK;
  187. }
  188. /**
  189. * Registers a new drop target to monitor.
  190. *
  191. * @note Sim thread only.
  192. */
  193. void registerDropTarget(OSDropTarget* dropTarget)
  194. {
  195. mDropTargets.push_back(dropTarget);
  196. }
  197. /**
  198. * Unregisters an existing drop target and stops monitoring it.
  199. *
  200. * @note Sim thread only.
  201. */
  202. void unregisterDropTarget(OSDropTarget* dropTarget)
  203. {
  204. auto findIter = std::find(begin(mDropTargets), end(mDropTargets), dropTarget);
  205. if(findIter != mDropTargets.end())
  206. mDropTargets.erase(findIter);
  207. }
  208. /**
  209. * Gets the total number of monitored drop targets.
  210. *
  211. * @note Sim thread only.
  212. */
  213. unsigned int getNumDropTargets() const
  214. {
  215. return (unsigned int)mDropTargets.size();
  216. }
  217. /** Called every frame by the sim thread. Internal method. */
  218. void update()
  219. {
  220. BS_LOCK_MUTEX(mSync);
  221. for(auto& op: mQueuedDropOps)
  222. {
  223. for(auto& target : mDropTargets)
  224. {
  225. if(op.type != DropOpType::Leave)
  226. {
  227. if(target->_isInside(op.position))
  228. {
  229. if(!target->_isActive())
  230. {
  231. target->_setFileList(*op.mFileList);
  232. target->onEnter(op.position.x, op.position.y);
  233. }
  234. if(op.type == DropOpType::DragOver)
  235. target->onDragOver(op.position.x, op.position.y);
  236. else if(op.type == DropOpType::Drop)
  237. {
  238. target->_setFileList(*op.mFileList);
  239. target->onDrop(op.position.x, op.position.y);
  240. }
  241. }
  242. else
  243. {
  244. if(target->_isActive())
  245. {
  246. target->onLeave();
  247. target->_clear();
  248. target->_setActive(false);
  249. }
  250. }
  251. }
  252. else
  253. {
  254. if(target->_isActive())
  255. {
  256. target->onLeave();
  257. target->_clear();
  258. target->_setActive(false);
  259. }
  260. }
  261. }
  262. if(op.type == DropOpType::Leave || op.type == DropOpType::Drop)
  263. {
  264. while (!mFileLists.empty())
  265. {
  266. bool done = mFileLists[0] == op.mFileList;
  267. bs_delete(mFileLists[0]);
  268. mFileLists.erase(mFileLists.begin());
  269. if (done)
  270. break;
  271. }
  272. }
  273. }
  274. mQueuedDropOps.clear();
  275. }
  276. private:
  277. /** Check if we support the data in the provided drag and drop data object. */
  278. bool isDataValid(IDataObject* data)
  279. {
  280. // TODO - Currently only supports file drag and drop, so only CF_HDROP is used
  281. FORMATETC fmtetc = { CF_HDROP, 0, DVASPECT_CONTENT, -1, TYMED_HGLOBAL };
  282. return data->QueryGetData(&fmtetc) == S_OK ? true : false;
  283. }
  284. /** Gets a file list from data. Caller must ensure that the data actually contains a file list. */
  285. Vector<WString>* getFileListFromData(IDataObject* data)
  286. {
  287. FORMATETC fmtetc = { CF_HDROP, 0, DVASPECT_CONTENT, -1, TYMED_HGLOBAL };
  288. STGMEDIUM stgmed;
  289. Vector<WString>* files = bs_new<Vector<WString>>();
  290. if(data->GetData(&fmtetc, &stgmed) == S_OK)
  291. {
  292. PVOID data = GlobalLock(stgmed.hGlobal);
  293. HDROP hDrop = (HDROP)data;
  294. UINT numFiles = DragQueryFileW(hDrop, 0xFFFFFFFF, nullptr, 0);
  295. files->resize(numFiles);
  296. for(UINT i = 0; i < numFiles; i++)
  297. {
  298. UINT numChars = DragQueryFileW(hDrop, i, nullptr, 0) + 1;
  299. wchar_t* buffer = (wchar_t*)bs_alloc((UINT32)numChars * sizeof(wchar_t));
  300. DragQueryFileW(hDrop, i, buffer, numChars);
  301. (*files)[i] = WString(buffer);
  302. bs_free(buffer);
  303. }
  304. GlobalUnlock(stgmed.hGlobal);
  305. ReleaseStgMedium(&stgmed);
  306. }
  307. return files;
  308. }
  309. private:
  310. Vector<OSDropTarget*> mDropTargets;
  311. LONG mRefCount;
  312. HWND mHWnd;
  313. bool mAcceptDrag;
  314. Vector<DropTargetOp> mQueuedDropOps;
  315. Vector<Vector<WString>*> mFileLists;
  316. BS_MUTEX(mSync);
  317. };
  318. /** @} */
  319. /** @endcond */
  320. }