UIDragDropWindows.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. //
  2. // Copyright (c) 2014-2015, THUNDERBEAST GAMES LLC All rights reserved
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #ifdef ATOMIC_PLATFORM_WINDOWS
  23. #define WIN32_LEAN_AND_MEAN
  24. #define STRICT
  25. #ifndef UNICODE
  26. #define UNICODE 1
  27. #endif
  28. #undef _WIN32_WINNT
  29. #define _WIN32_WINNT 0x501 /* Need 0x410 for AlphaBlend() and 0x500 for EnumDisplayDevices(), 0x501 for raw input */
  30. #include <Atomic/IO/Log.h>
  31. #include <Atomic/Core/Context.h>
  32. #include <Atomic/Core/CoreEvents.h>
  33. #include <Atomic/Input/InputEvents.h>
  34. #include <Atomic/Graphics/Graphics.h>
  35. #include "UIDragDrop.h"
  36. #include "UIDragDropMac.h"
  37. #include <ThirdParty/SDL/include/SDL_syswm.h>
  38. #include <windows.h>
  39. #include <windowsx.h>
  40. #include <shellapi.h>
  41. #include <OleIdl.h>
  42. namespace Atomic
  43. {
  44. // global weakptr to dragAndDrop system as it is sometimes accessed outside of an Object method
  45. static WeakPtr<UIDragDrop> dragAndDrop_;
  46. // Win32 proc hook so we can catch close and unregister drag and drop
  47. static LRESULT CALLBACK Atomic_WindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam);
  48. class CDropTarget : public IDropTarget
  49. {
  50. public:
  51. // IUnknown implementation
  52. HRESULT __stdcall QueryInterface(REFIID iid, void ** ppvObject);
  53. ULONG __stdcall AddRef(void);
  54. ULONG __stdcall Release(void);
  55. void UpdateMousePos();
  56. // IDropTarget implementation
  57. HRESULT __stdcall DragEnter(IDataObject * pDataObject, DWORD grfKeyState, POINTL pt, DWORD * pdwEffect);
  58. HRESULT __stdcall DragOver(DWORD grfKeyState, POINTL pt, DWORD * pdwEffect);
  59. HRESULT __stdcall DragLeave(void);
  60. HRESULT __stdcall Drop(IDataObject * pDataObject, DWORD grfKeyState, POINTL pt, DWORD * pdwEffect);
  61. void OpenFilesFromDataObject(IDataObject * pdto);
  62. // Constructor
  63. CDropTarget(HWND hwnd);
  64. ~CDropTarget();
  65. private:
  66. // internal helper function
  67. DWORD DropEffect(DWORD grfKeyState, POINTL pt, DWORD dwAllowed);
  68. bool QueryDataObject(IDataObject *pDataObject);
  69. // Private member variables
  70. LONG m_lRefCount;
  71. HWND m_hWnd;
  72. bool m_fAllowDrop;
  73. IDataObject *m_pDataObject;
  74. };
  75. CDropTarget::CDropTarget(HWND hwnd)
  76. {
  77. m_lRefCount = 1;
  78. m_hWnd = hwnd;
  79. m_fAllowDrop = false;
  80. }
  81. CDropTarget::~CDropTarget()
  82. {
  83. }
  84. HRESULT __stdcall CDropTarget::QueryInterface(REFIID iid, void ** ppvObject)
  85. {
  86. if (iid == IID_IDropTarget || iid == IID_IUnknown)
  87. {
  88. AddRef();
  89. *ppvObject = this;
  90. return S_OK;
  91. }
  92. else
  93. {
  94. *ppvObject = 0;
  95. return E_NOINTERFACE;
  96. }
  97. }
  98. ULONG __stdcall CDropTarget::AddRef(void)
  99. {
  100. return InterlockedIncrement(&m_lRefCount);
  101. }
  102. ULONG __stdcall CDropTarget::Release(void)
  103. {
  104. LONG count = InterlockedDecrement(&m_lRefCount);
  105. if (count == 0)
  106. {
  107. delete this;
  108. return 0;
  109. }
  110. else
  111. {
  112. return count;
  113. }
  114. }
  115. void CDropTarget::UpdateMousePos()
  116. {
  117. if (!m_hWnd || dragAndDrop_.Null())
  118. return;
  119. POINT p;
  120. GetCursorPos(&p);
  121. ScreenToClient(m_hWnd, &p);
  122. using namespace Atomic::MouseMove;
  123. Atomic::VariantMap mvEventData;
  124. int x = p.x;
  125. int y = p.y;
  126. Graphics* graphics = dragAndDrop_->GetSubsystem<Graphics>();
  127. if ((x >= 0 && x < graphics->GetWidth()) && y >= 0 && y < graphics->GetHeight())
  128. {
  129. mvEventData[P_X] = x;
  130. mvEventData[P_Y] = y;
  131. mvEventData[P_DX] = 0;
  132. mvEventData[P_DY] = 0;
  133. mvEventData[P_BUTTONS] = 0;
  134. mvEventData[P_QUALIFIERS] = 0;
  135. dragAndDrop_->SendEvent(E_MOUSEMOVE, mvEventData);
  136. }
  137. }
  138. bool CDropTarget::QueryDataObject(IDataObject *pDataObject)
  139. {
  140. FORMATETC fmte = { CF_HDROP, NULL, DVASPECT_CONTENT,
  141. -1, TYMED_HGLOBAL };
  142. // does the data object support CF_HDROP using a HGLOBAL?
  143. return pDataObject->QueryGetData(&fmte) == S_OK ? true : false;
  144. }
  145. DWORD CDropTarget::DropEffect(DWORD grfKeyState, POINTL pt, DWORD dwAllowed)
  146. {
  147. DWORD dwEffect = 0;
  148. // 1. check "pt" -> do we allow a drop at the specified coordinates?
  149. // 2. work out that the drop-effect should be based on grfKeyState
  150. if (grfKeyState & MK_CONTROL)
  151. {
  152. dwEffect = dwAllowed & DROPEFFECT_COPY;
  153. }
  154. else if (grfKeyState & MK_SHIFT)
  155. {
  156. dwEffect = dwAllowed & DROPEFFECT_MOVE;
  157. }
  158. // 3. no key-modifiers were specified (or drop effect not allowed), so
  159. // base the effect on those allowed by the dropsource
  160. if (dwEffect == 0)
  161. {
  162. if (dwAllowed & DROPEFFECT_COPY) dwEffect = DROPEFFECT_COPY;
  163. if (dwAllowed & DROPEFFECT_MOVE) dwEffect = DROPEFFECT_MOVE;
  164. }
  165. return dwEffect;
  166. }
  167. HRESULT __stdcall CDropTarget::DragEnter(IDataObject * pDataObject, DWORD grfKeyState, POINTL pt, DWORD * pdwEffect)
  168. {
  169. if (dragAndDrop_.Null())
  170. return S_OK;
  171. // does the dataobject contain data we want?
  172. m_fAllowDrop = QueryDataObject(pDataObject);
  173. if (m_fAllowDrop)
  174. {
  175. // get the dropeffect based on keyboard state
  176. *pdwEffect = DropEffect(grfKeyState, pt, *pdwEffect);
  177. SetFocus(m_hWnd);
  178. dragAndDrop_->FileDragEntered();
  179. UpdateMousePos();
  180. }
  181. else
  182. {
  183. *pdwEffect = DROPEFFECT_NONE;
  184. }
  185. return S_OK;
  186. }
  187. HRESULT __stdcall CDropTarget::DragOver(DWORD grfKeyState, POINTL pt, DWORD * pdwEffect)
  188. {
  189. if (m_fAllowDrop)
  190. {
  191. *pdwEffect = DropEffect(grfKeyState, pt, *pdwEffect);
  192. UpdateMousePos();
  193. }
  194. else
  195. {
  196. *pdwEffect = DROPEFFECT_NONE;
  197. }
  198. return S_OK;
  199. }
  200. HRESULT __stdcall CDropTarget::DragLeave(void)
  201. {
  202. return S_OK;
  203. }
  204. void CDropTarget::OpenFilesFromDataObject(IDataObject *pdto)
  205. {
  206. FORMATETC fmte = { CF_HDROP, NULL, DVASPECT_CONTENT,
  207. -1, TYMED_HGLOBAL };
  208. STGMEDIUM stgm;
  209. if (SUCCEEDED(pdto->GetData(&fmte, &stgm)))
  210. {
  211. PVOID data = GlobalLock(stgm.hGlobal);
  212. HDROP hdrop = reinterpret_cast<HDROP>(stgm.hGlobal);
  213. UINT cFiles = DragQueryFile(hdrop, 0xFFFFFFFF, NULL, 0);
  214. for (UINT i = 0; i < cFiles; i++)
  215. {
  216. TCHAR szFile[MAX_PATH];
  217. UINT cch = DragQueryFile(hdrop, i, szFile, MAX_PATH);
  218. if (cch > 0 && cch < MAX_PATH)
  219. {
  220. dragAndDrop_->FileDragAddFile(szFile);
  221. }
  222. }
  223. DragFinish(hdrop);
  224. GlobalUnlock(stgm.hGlobal);
  225. ReleaseStgMedium(&stgm);
  226. dragAndDrop_->FileDragConclude();
  227. }
  228. }
  229. HRESULT __stdcall CDropTarget::Drop(IDataObject * pDataObject, DWORD grfKeyState, POINTL pt, DWORD * pdwEffect)
  230. {
  231. if (dragAndDrop_.Null())
  232. return S_OK;
  233. UpdateMousePos();
  234. if (m_fAllowDrop)
  235. {
  236. OpenFilesFromDataObject(pDataObject);
  237. *pdwEffect = DropEffect(grfKeyState, pt, *pdwEffect);
  238. }
  239. else
  240. {
  241. *pdwEffect = DROPEFFECT_NONE;
  242. }
  243. return S_OK;
  244. }
  245. static void RegisterDropWindow(HWND hwnd, IDropTarget **ppDropTarget)
  246. {
  247. OleInitialize(0);
  248. CDropTarget *pDropTarget = new CDropTarget(hwnd);
  249. // acquire a strong lock
  250. CoLockObjectExternal(pDropTarget, TRUE, FALSE);
  251. // tell OLE that the window is a drop target
  252. HRESULT result = RegisterDragDrop(hwnd, pDropTarget);
  253. *ppDropTarget = pDropTarget;
  254. }
  255. static void UnregisterDropWindow(HWND hwnd, IDropTarget *pDropTarget)
  256. {
  257. // remove drag+drop
  258. HRESULT result = RevokeDragDrop(hwnd);
  259. // remove the strong lock
  260. CoLockObjectExternal(pDropTarget, FALSE, TRUE);
  261. // release our own reference
  262. pDropTarget->Release();
  263. OleUninitialize();
  264. }
  265. // UIDragDropWindows
  266. class UIDragDropWindows : public Object
  267. {
  268. ATOMIC_OBJECT(UIDragDropWindows, Object);
  269. public:
  270. /// Construct.
  271. UIDragDropWindows(Context* context);
  272. virtual ~UIDragDropWindows();
  273. void Shutdown();
  274. static WNDPROC sdlWndProc_;
  275. private:
  276. IDropTarget* dropTarget_;
  277. HWND hwnd_;
  278. };
  279. WNDPROC UIDragDropWindows::sdlWndProc_ = NULL;
  280. UIDragDropWindows::UIDragDropWindows(Context* context) : Object(context),
  281. dropTarget_(NULL), hwnd_(NULL)
  282. {
  283. SDL_Window* window = (SDL_Window*)GetSubsystem<Graphics>()->GetSDLWindow();
  284. SDL_SysWMinfo info;
  285. SDL_VERSION(&info.version);
  286. if (SDL_GetWindowWMInfo(window, &info))
  287. {
  288. hwnd_ = info.info.win.window;
  289. // hook the wnd proc so we can catch close and deregister drag and drop
  290. sdlWndProc_ = (WNDPROC)GetWindowLongPtr(hwnd_, GWLP_WNDPROC);
  291. SetWindowLongPtr(hwnd_, GWLP_WNDPROC, (LONG_PTR)Atomic_WindowProc);
  292. RegisterDropWindow(hwnd_, &dropTarget_);
  293. }
  294. }
  295. UIDragDropWindows::~UIDragDropWindows()
  296. {
  297. }
  298. void UIDragDropWindows::Shutdown()
  299. {
  300. if (!hwnd_ || !dropTarget_)
  301. return;
  302. UnregisterDropWindow(hwnd_, dropTarget_);
  303. // restore SDL winproc
  304. SetWindowLongPtr(hwnd_, GWLP_WNDPROC, (LONG_PTR)sdlWndProc_);
  305. }
  306. void InitDragAndDrop(UIDragDrop *dragAndDrop)
  307. {
  308. dragAndDrop_ = dragAndDrop;
  309. dragAndDrop->GetContext()->RegisterSubsystem(new UIDragDropWindows(dragAndDrop->GetContext()));
  310. }
  311. LRESULT CALLBACK Atomic_WindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
  312. {
  313. if (dragAndDrop_.Null())
  314. {
  315. return CallWindowProc(UIDragDropWindows::sdlWndProc_, hwnd, msg, wParam, lParam);
  316. }
  317. switch (msg) {
  318. case WM_CLOSE:
  319. {
  320. dragAndDrop_->GetSubsystem<UIDragDropWindows>()->Shutdown();
  321. break;
  322. }
  323. };
  324. return CallWindowProc(UIDragDropWindows::sdlWndProc_, hwnd, msg, wParam, lParam);
  325. }
  326. }
  327. #endif