CmPlatformWndProc.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. #include "CmPlatformWndProc.h"
  2. #include "CmRenderWindow.h"
  3. #include "CmApplication.h"
  4. #include "CmInput.h"
  5. namespace CamelotFramework
  6. {
  7. UINT32 PlatformWndProc::mMoveResizeMouseUpState = 0;
  8. LRESULT CALLBACK PlatformWndProc::_win32WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  9. {
  10. if (uMsg == WM_CREATE)
  11. { // Store pointer to Win32Window in user data area
  12. SetWindowLongPtr(hWnd, GWLP_USERDATA, (LONG_PTR)(((LPCREATESTRUCT)lParam)->lpCreateParams));
  13. return 0;
  14. }
  15. // look up window instance
  16. // note: it is possible to get a WM_SIZE before WM_CREATE
  17. RenderWindow* win = (RenderWindow*)GetWindowLongPtr(hWnd, GWLP_USERDATA);
  18. if (!win)
  19. return DefWindowProc(hWnd, uMsg, wParam, lParam);
  20. switch( uMsg )
  21. {
  22. case WM_ACTIVATE:
  23. {
  24. bool active = (LOWORD(wParam) != WA_INACTIVE);
  25. if( active )
  26. {
  27. win->setActive(true);
  28. if(!win->hasFocus())
  29. windowFocusReceived(win);
  30. }
  31. else
  32. {
  33. if(win->hasFocus())
  34. windowFocusLost(win);
  35. }
  36. break;
  37. }
  38. case WM_SYSKEYDOWN:
  39. switch( wParam )
  40. {
  41. case VK_CONTROL:
  42. case VK_SHIFT:
  43. case VK_MENU: //ALT
  44. //return zero to bypass defProc and signal we processed the message
  45. return 0;
  46. }
  47. break;
  48. case WM_SYSKEYUP:
  49. switch( wParam )
  50. {
  51. case VK_CONTROL:
  52. case VK_SHIFT:
  53. case VK_MENU: //ALT
  54. case VK_F10:
  55. //return zero to bypass defProc and signal we processed the message
  56. return 0;
  57. }
  58. break;
  59. case WM_SYSCHAR:
  60. // return zero to bypass defProc and signal we processed the message, unless it's an ALT-space
  61. if (wParam != VK_SPACE)
  62. return 0;
  63. break;
  64. case WM_ENTERSIZEMOVE:
  65. mMoveResizeMouseUpState = 1;
  66. break;
  67. case WM_EXITSIZEMOVE:
  68. // HACK - Windows doesn't send mouseUp event after move/resize if the cursor moved out of the original window bounds
  69. if(mMoveResizeMouseUpState != 2)
  70. gInput().instance().simulateButtonUp(BC_MOUSE_LEFT);
  71. mMoveResizeMouseUpState = 0;
  72. break;
  73. case WM_MOVE:
  74. windowMovedOrResized(win);
  75. break;
  76. case WM_DISPLAYCHANGE:
  77. windowMovedOrResized(win);
  78. break;
  79. case WM_SIZE:
  80. windowMovedOrResized(win);
  81. break;
  82. case WM_SETCURSOR:
  83. if(isCursorHidden())
  84. win32HideCursor();
  85. else
  86. {
  87. switch (LOWORD(lParam))
  88. {
  89. case HTTOPLEFT:
  90. SetCursor(LoadCursor(0, IDC_SIZENWSE));
  91. return 0;
  92. case HTTOP:
  93. SetCursor(LoadCursor(0, IDC_SIZENS));
  94. return 0;
  95. case HTTOPRIGHT:
  96. SetCursor(LoadCursor(0, IDC_SIZENESW));
  97. return 0;
  98. case HTLEFT:
  99. SetCursor(LoadCursor(0, IDC_SIZEWE));
  100. return 0;
  101. case HTRIGHT:
  102. SetCursor(LoadCursor(0, IDC_SIZEWE));
  103. return 0;
  104. case HTBOTTOMLEFT:
  105. SetCursor(LoadCursor(0, IDC_SIZENESW));
  106. return 0;
  107. case HTBOTTOM:
  108. SetCursor(LoadCursor(0, IDC_SIZENS));
  109. return 0;
  110. case HTBOTTOMRIGHT:
  111. SetCursor(LoadCursor(0, IDC_SIZENWSE));
  112. return 0;
  113. }
  114. win32ShowCursor();
  115. }
  116. return true;
  117. case WM_GETMINMAXINFO:
  118. // Prevent the window from going smaller than some minimu size
  119. ((MINMAXINFO*)lParam)->ptMinTrackSize.x = 100;
  120. ((MINMAXINFO*)lParam)->ptMinTrackSize.y = 100;
  121. break;
  122. case WM_CLOSE:
  123. {
  124. // TODO - Only stop main loop if primary window is closed!!
  125. gApplication().stopMainLoop();
  126. return 0;
  127. }
  128. case WM_NCHITTEST:
  129. {
  130. auto iterFind = mNonClientAreas.find(win);
  131. if(iterFind == mNonClientAreas.end())
  132. break;
  133. POINT mousePos;
  134. mousePos.x = GET_X_LPARAM(lParam);
  135. mousePos.y = GET_Y_LPARAM(lParam);
  136. ScreenToClient(hWnd, &mousePos);
  137. Int2 mousePosInt;
  138. mousePosInt.x = mousePos.x;
  139. mousePosInt.y = mousePos.y;
  140. Vector<NonClientResizeArea>::type& resizeAreasPerWindow = iterFind->second.resizeAreas;
  141. for(auto area : resizeAreasPerWindow)
  142. {
  143. if(area.area.contains(mousePosInt))
  144. return translateNonClientAreaType(area.type);
  145. }
  146. Vector<Rect>::type& moveAreasPerWindow = iterFind->second.moveAreas;
  147. for(auto area : moveAreasPerWindow)
  148. {
  149. if(area.contains(mousePosInt))
  150. return HTCAPTION;
  151. }
  152. return HTCLIENT;
  153. }
  154. case WM_NCMOUSELEAVE:
  155. case WM_MOUSELEAVE:
  156. {
  157. mIsTrackingMouse = false; // TrackMouseEvent ends when this message is received and needs to be re-applied
  158. POINT mousePos;
  159. GetCursorPos(&mousePos);
  160. // Ensure we have actually left the window - it's possible we just moved from client to non-client area but
  161. // that's not what we're interested in
  162. if(mousePos.x < win->getLeft() || mousePos.x > (INT32)(win->getLeft() + win->getWidth()) ||
  163. mousePos.y < win->getTop() || mousePos.y > (INT32)(win->getTop() + win->getHeight()))
  164. {
  165. CM_LOCK_MUTEX(mSync);
  166. mMouseLeftWindows.push_back(win);
  167. }
  168. else
  169. {
  170. TRACKMOUSEEVENT tme = { sizeof(tme) };
  171. if(uMsg == WM_MOUSELEAVE)
  172. tme.dwFlags = TME_LEAVE | TME_NONCLIENT;
  173. else
  174. tme.dwFlags = TME_LEAVE;
  175. tme.hwndTrack = hWnd;
  176. TrackMouseEvent(&tme);
  177. mIsTrackingMouse = true;
  178. }
  179. }
  180. break;
  181. case WM_NCLBUTTONUP:
  182. case WM_LBUTTONUP:
  183. // Part of a hack that's done in WM_EXITSIZEMOVE (see there)
  184. if(mMoveResizeMouseUpState = 1)
  185. mMoveResizeMouseUpState = 2;
  186. break;
  187. case WM_NCMOUSEMOVE:
  188. case WM_MOUSEMOVE:
  189. {
  190. // Set up tracking so we get notified when mouse leaves the window
  191. if(!mIsTrackingMouse)
  192. {
  193. TRACKMOUSEEVENT tme = { sizeof(tme) };
  194. tme.dwFlags = TME_LEAVE;
  195. if(uMsg == WM_NCMOUSEMOVE)
  196. tme.dwFlags |= TME_NONCLIENT;
  197. tme.hwndTrack = hWnd;
  198. TrackMouseEvent(&tme);
  199. mIsTrackingMouse = true;
  200. }
  201. POINT mousePos;
  202. mousePos.x = GET_X_LPARAM(lParam);
  203. mousePos.y = GET_Y_LPARAM(lParam);
  204. ClientToScreen(hWnd, &mousePos);
  205. if(!onMouseMoved.empty())
  206. onMouseMoved(Int2(mousePos.x, mousePos.y));
  207. return true;
  208. }
  209. case WM_MOUSEWHEEL:
  210. {
  211. INT16 wheelDelta = GET_WHEEL_DELTA_WPARAM(wParam);
  212. float wheelDeltaFlt = wheelDelta / (float)WHEEL_DELTA;
  213. if(!onMouseWheelScrolled.empty())
  214. onMouseWheelScrolled(wheelDeltaFlt);
  215. return true;
  216. }
  217. case WM_DEADCHAR:
  218. case WM_CHAR:
  219. {
  220. switch (wParam)
  221. {
  222. case VK_BACK:
  223. case 0x0A: // linefeed
  224. case 0x0D: // carriage return
  225. case VK_ESCAPE:
  226. case VK_TAB:
  227. break;
  228. default: // displayable character
  229. {
  230. UINT8 scanCode = (lParam >> 16) & 0xFF;
  231. BYTE keyState[256];
  232. HKL layout = GetKeyboardLayout(0);
  233. if(GetKeyboardState(keyState) == 0)
  234. return 0;
  235. unsigned int vk = MapVirtualKeyEx(scanCode, MAPVK_VSC_TO_VK_EX, layout);
  236. if(vk == 0)
  237. return 0;
  238. bool isDeadKey = (MapVirtualKeyEx(vk, MAPVK_VK_TO_CHAR, layout) & (1 << 31)) != 0;
  239. if(isDeadKey)
  240. return 0;
  241. wchar_t buff[3] = {0};
  242. int numChars = ToUnicodeEx(vk, scanCode, keyState, buff, 3, 0, layout);
  243. // TODO - I am ignoring dead keys here - primarily because I haven't found a good way of retrieving non-combined dead key
  244. // value. ToUnicodeEx and MapVirtualKeyEx only return precombined (i.e. spacing) versions, which can't be combined using other characters.
  245. // I need non-combined version so I can use it with FoldString to apply to a certain character.
  246. UINT32 finalChar = 0;
  247. if(numChars == 1)
  248. finalChar = buff[0];
  249. else
  250. return 0;
  251. if(!onCharInput.empty())
  252. onCharInput(finalChar);
  253. return 0;
  254. }
  255. }
  256. break;
  257. }
  258. case WM_CM_SETCAPTURE:
  259. SetCapture(hWnd);
  260. break;
  261. case WM_CM_RELEASECAPTURE:
  262. ReleaseCapture();
  263. break;
  264. case WM_CAPTURECHANGED:
  265. if(!onMouseCaptureChanged.empty())
  266. onMouseCaptureChanged();
  267. break;
  268. }
  269. return DefWindowProc( hWnd, uMsg, wParam, lParam );
  270. }
  271. LRESULT PlatformWndProc::translateNonClientAreaType(NonClientAreaBorderType type)
  272. {
  273. LRESULT dir = HTCLIENT;
  274. switch(type)
  275. {
  276. case NonClientAreaBorderType::Left:
  277. dir = HTLEFT;
  278. break;
  279. case NonClientAreaBorderType::TopLeft:
  280. dir = HTTOPLEFT;
  281. break;
  282. case NonClientAreaBorderType::Top:
  283. dir = HTTOP;
  284. break;
  285. case NonClientAreaBorderType::TopRight:
  286. dir = HTTOPRIGHT;
  287. break;
  288. case NonClientAreaBorderType::Right:
  289. dir = HTRIGHT;
  290. break;
  291. case NonClientAreaBorderType::BottomRight:
  292. dir = HTBOTTOMRIGHT;
  293. break;
  294. case NonClientAreaBorderType::Bottom:
  295. dir = HTBOTTOM;
  296. break;
  297. case NonClientAreaBorderType::BottomLeft:
  298. dir = HTBOTTOMLEFT;
  299. break;
  300. }
  301. return dir;
  302. }
  303. }