UIDragDropWindows.cpp 10 KB

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