UIDragDropWindows.cpp 10 KB

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