BsWin32DropTarget.h 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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. /** @addtogroup Platform-Internal
  9. * @{
  10. */
  11. /**
  12. * Called by the OS when various drag and drop actions are performed over a window this control is registered for.
  13. *
  14. * @note
  15. * This class queues all messages receives by the OS (from the core thread), and then executes the queue on sim thread.
  16. * You should be wary of which methods are allowed to be called from which thread.
  17. */
  18. class Win32DropTarget : public IDropTarget
  19. {
  20. /** Type of drag and drop event. */
  21. enum class DropOpType
  22. {
  23. DragOver,
  24. Drop,
  25. Leave
  26. };
  27. /** Type of data that a drag and drop operation contains. */
  28. enum class DropOpDataType
  29. {
  30. FileList,
  31. None
  32. };
  33. /** Structure describing a drag and drop operation. */
  34. struct DropTargetOp
  35. {
  36. DropTargetOp(DropOpType _type, const Vector2I& _pos)
  37. :type(_type), position(_pos), dataType(DropOpDataType::None)
  38. { }
  39. DropOpType type;
  40. Vector2I position;
  41. DropOpDataType dataType;
  42. union
  43. {
  44. Vector<WString>* mFileList;
  45. };
  46. };
  47. public:
  48. Win32DropTarget(HWND hWnd)
  49. :mRefCount(1), mHWnd(hWnd), mAcceptDrag(false)
  50. { }
  51. ~Win32DropTarget()
  52. {
  53. Lock lock(mSync);
  54. for(auto& fileList : mFileLists)
  55. {
  56. bs_delete(fileList);
  57. }
  58. mFileLists.clear();
  59. mQueuedDropOps.clear();
  60. }
  61. /** Registers the drop target with the operating system. Monitoring for drag and drop operations starts. */
  62. void registerWithOS()
  63. {
  64. CoLockObjectExternal(this, TRUE, FALSE);
  65. RegisterDragDrop(mHWnd, this);
  66. }
  67. /** Unregisters the drop target with the operating system. Monitoring for drag and drop operations stops. */
  68. void unregisterWithOS()
  69. {
  70. RevokeDragDrop(mHWnd);
  71. CoLockObjectExternal(this, FALSE, FALSE);
  72. }
  73. /** COM requirement. Returns instance of an interface of the provided type. */
  74. HRESULT __stdcall QueryInterface(REFIID iid, void** ppvObject) override
  75. {
  76. if(iid == IID_IDropTarget || iid == IID_IUnknown)
  77. {
  78. AddRef();
  79. *ppvObject = this;
  80. return S_OK;
  81. }
  82. else
  83. {
  84. *ppvObject = nullptr;
  85. return E_NOINTERFACE;
  86. }
  87. }
  88. /** COM requirement. Increments objects reference count. */
  89. ULONG __stdcall AddRef() override
  90. {
  91. return InterlockedIncrement(&mRefCount);
  92. }
  93. /** COM requirement. Decreases the objects reference count and deletes the object if its zero. */
  94. ULONG __stdcall Release() override
  95. {
  96. LONG count = InterlockedDecrement(&mRefCount);
  97. if(count == 0)
  98. {
  99. bs_delete(this);
  100. return 0;
  101. }
  102. else
  103. {
  104. return count;
  105. }
  106. }
  107. /**
  108. * Called by the OS when user enters the drop target area while dragging an object.
  109. *
  110. * @note Called on core thread.
  111. */
  112. HRESULT __stdcall DragEnter(IDataObject* pDataObj, DWORD grfKeyState, POINTL pt, DWORD* pdwEffect) override
  113. {
  114. *pdwEffect = DROPEFFECT_LINK;
  115. mAcceptDrag = isDataValid(pDataObj);
  116. if(!mAcceptDrag)
  117. return S_OK;
  118. {
  119. Lock lock(mSync);
  120. mFileLists.push_back(getFileListFromData(pDataObj));
  121. ScreenToClient(mHWnd, (POINT *)&pt);
  122. mQueuedDropOps.push_back(DropTargetOp(DropOpType::DragOver, Vector2I((int)pt.x, (int)pt.y)));
  123. DropTargetOp& op = mQueuedDropOps.back();
  124. op.dataType = DropOpDataType::FileList;
  125. op.mFileList = mFileLists.back();
  126. }
  127. return S_OK;
  128. }
  129. /**
  130. * Called by the OS while user continues to drag an object over the drop target.
  131. *
  132. * @note Called on core thread.
  133. */
  134. HRESULT __stdcall DragOver(DWORD grfKeyState, POINTL pt, DWORD* pdwEffect) override
  135. {
  136. *pdwEffect = DROPEFFECT_LINK;
  137. if(!mAcceptDrag)
  138. return S_OK;
  139. {
  140. Lock lock(mSync);
  141. ScreenToClient(mHWnd, (POINT *)&pt);
  142. mQueuedDropOps.push_back(DropTargetOp(DropOpType::DragOver, Vector2I((int)pt.x, (int)pt.y)));
  143. DropTargetOp& op = mQueuedDropOps.back();
  144. op.dataType = DropOpDataType::FileList;
  145. op.mFileList = mFileLists.back();
  146. }
  147. return S_OK;
  148. }
  149. /**
  150. * Called by the OS when user leaves the drop target.
  151. *
  152. * @note Called on core thread.
  153. */
  154. HRESULT __stdcall DragLeave() override
  155. {
  156. {
  157. Lock lock(mSync);
  158. mQueuedDropOps.push_back(DropTargetOp(DropOpType::Leave, Vector2I()));
  159. DropTargetOp& op = mQueuedDropOps.back();
  160. op.dataType = DropOpDataType::FileList;
  161. op.mFileList = mFileLists.back();
  162. }
  163. return S_OK;
  164. }
  165. /**
  166. * Called by the OS when the user ends the drag operation while over the drop target.
  167. *
  168. * @note Called on core thread.
  169. */
  170. HRESULT __stdcall Drop(IDataObject* pDataObj, DWORD grfKeyState, POINTL pt, DWORD* pdwEffect) override
  171. {
  172. *pdwEffect = DROPEFFECT_LINK;
  173. mAcceptDrag = false;
  174. if(!isDataValid(pDataObj))
  175. return S_OK;
  176. {
  177. Lock lock(mSync);
  178. mFileLists.push_back(getFileListFromData(pDataObj));
  179. ScreenToClient(mHWnd, (POINT *)&pt);
  180. mQueuedDropOps.push_back(DropTargetOp(DropOpType::Drop, Vector2I((int)pt.x, (int)pt.y)));
  181. DropTargetOp& op = mQueuedDropOps.back();
  182. op.dataType = DropOpDataType::FileList;
  183. op.mFileList = mFileLists.back();
  184. }
  185. return S_OK;
  186. }
  187. /**
  188. * Registers a new drop target to monitor.
  189. *
  190. * @note Sim thread only.
  191. */
  192. void registerDropTarget(OSDropTarget* dropTarget)
  193. {
  194. mDropTargets.push_back(dropTarget);
  195. }
  196. /**
  197. * Unregisters an existing drop target and stops monitoring it.
  198. *
  199. * @note Sim thread only.
  200. */
  201. void unregisterDropTarget(OSDropTarget* dropTarget)
  202. {
  203. auto findIter = std::find(begin(mDropTargets), end(mDropTargets), dropTarget);
  204. if(findIter != mDropTargets.end())
  205. mDropTargets.erase(findIter);
  206. }
  207. /**
  208. * Gets the total number of monitored drop targets.
  209. *
  210. * @note Sim thread only.
  211. */
  212. unsigned int getNumDropTargets() const
  213. {
  214. return (unsigned int)mDropTargets.size();
  215. }
  216. /** Called every frame by the sim thread. Internal method. */
  217. void update()
  218. {
  219. Lock lock(mSync);
  220. for(auto& op: mQueuedDropOps)
  221. {
  222. for(auto& target : mDropTargets)
  223. {
  224. if(op.type != DropOpType::Leave)
  225. {
  226. if(target->_isInside(op.position))
  227. {
  228. if(!target->_isActive())
  229. {
  230. target->_setFileList(*op.mFileList);
  231. target->_setActive(true);
  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. Mutex mSync;
  317. };
  318. /** @} */
  319. }