UIDragDropWindows.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. #define WIN_StringToUTF8(S) SDL_iconv_string("UTF-8", "UTF-16LE", (char *)(S), (SDL_wcslen(S)+1)*sizeof(WCHAR))
  20. namespace Atomic
  21. {
  22. class UIDragDropWindows : public Object
  23. {
  24. OBJECT(UIDragDropWindows);
  25. public:
  26. /// Construct.
  27. UIDragDropWindows(Context* context);
  28. virtual ~UIDragDropWindows();
  29. void HandleUpdate(StringHash eventType, VariantMap& eventData);
  30. static WNDPROC sdlWndProc_;
  31. HWND hwnd_;
  32. private:
  33. };
  34. WNDPROC UIDragDropWindows::sdlWndProc_ = NULL;
  35. static WeakPtr<UIDragDrop> dragAndDrop_;
  36. void UpdateMousePos(HWND hwnd)
  37. {
  38. if (!hwnd || dragAndDrop_.Null())
  39. return;
  40. POINT p;
  41. GetCursorPos(&p);
  42. ScreenToClient(hwnd, &p);
  43. using namespace Atomic::MouseMove;
  44. Atomic::VariantMap mvEventData;
  45. int x = p.x;
  46. int y = p.y;
  47. Graphics* graphics = dragAndDrop_->GetSubsystem<Graphics>();
  48. if ((x >= 0 && x < graphics->GetWidth()) && y >= 0 && y < graphics->GetHeight())
  49. {
  50. mvEventData[P_X] = x;
  51. mvEventData[P_Y] = y;
  52. mvEventData[P_DX] = 0;
  53. mvEventData[P_DY] = 0;
  54. mvEventData[P_BUTTONS] = 0;
  55. mvEventData[P_QUALIFIERS] = 0;
  56. dragAndDrop_->SendEvent(E_MOUSEMOVE, mvEventData);
  57. }
  58. }
  59. LRESULT CALLBACK Atomic_WindowProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
  60. {
  61. if (dragAndDrop_.Null())
  62. {
  63. return CallWindowProc(UIDragDropWindows::sdlWndProc_, hwnd, msg, wParam, lParam);
  64. }
  65. switch (msg) {
  66. case WM_DROPFILES:
  67. {
  68. //implement the IDropTarget interface and then register the HWND using RegisterDragDrop() to start receiving notifications. Be sure to call RevokeDragDrop() before exiting the app.
  69. UINT i;
  70. HDROP drop = (HDROP)wParam;
  71. UINT count = DragQueryFile(drop, 0xFFFFFFFF, NULL, 0);
  72. if (count)
  73. {
  74. dragAndDrop_->FileDragEntered();
  75. UpdateMousePos(hwnd);
  76. for (i = 0; i < count; ++i) {
  77. UINT size = DragQueryFile(drop, i, NULL, 0) + 1;
  78. LPTSTR buffer = SDL_stack_alloc(TCHAR, size);
  79. if (buffer) {
  80. if (DragQueryFile(drop, i, buffer, size)) {
  81. char *file = WIN_StringToUTF8(buffer);
  82. dragAndDrop_->FileDragAddFile(file);
  83. SDL_free(file);
  84. }
  85. SDL_stack_free(buffer);
  86. }
  87. }
  88. dragAndDrop_->FileDragConclude();
  89. }
  90. // sdlWndProc will handle this
  91. //DragFinish(drop);
  92. break;
  93. }
  94. };
  95. return CallWindowProc(UIDragDropWindows::sdlWndProc_, hwnd, msg, wParam, lParam);
  96. }
  97. UIDragDropWindows::UIDragDropWindows(Context* context) : Object(context),
  98. hwnd_(NULL)
  99. {
  100. SDL_Window* window = (SDL_Window*)GetSubsystem<Graphics>()->GetSDLWindow();
  101. SDL_SysWMinfo info;
  102. SDL_VERSION(&info.version);
  103. if (SDL_GetWindowWMInfo(window, &info)) {
  104. hwnd_ = info.info.win.window;
  105. // hook the wnd proc so we can handle drag/drop ourselves
  106. sdlWndProc_ = (WNDPROC)GetWindowLongPtr(hwnd_, GWLP_WNDPROC);
  107. SetWindowLongPtr(hwnd_, GWLP_WNDPROC, (LONG_PTR)Atomic_WindowProc);
  108. }
  109. SubscribeToEvent(E_UPDATE, HANDLER(UIDragDropWindows, HandleUpdate));
  110. }
  111. UIDragDropWindows::~UIDragDropWindows()
  112. {
  113. }
  114. void UIDragDropWindows::HandleUpdate(StringHash eventType, VariantMap & eventData)
  115. {
  116. if (!hwnd_ || dragAndDrop_.Null())
  117. return;
  118. if (GetActiveWindow() == hwnd_)
  119. return;
  120. UpdateMousePos(hwnd_);
  121. }
  122. void InitDragAndDrop(UIDragDrop *dragAndDrop)
  123. {
  124. dragAndDrop_ = dragAndDrop;
  125. dragAndDrop->GetContext()->RegisterSubsystem(new UIDragDropWindows(dragAndDrop->GetContext()));
  126. }
  127. }