BsWin32DropTarget.h 8.8 KB

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