CmPlatformWndProc.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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_MOUSELEAVE:
  155. {
  156. // Note: Right now I track only mouse leaving client area. So it's possible for the "mouse left window" callback
  157. // to trigger, while the mouse is still in the non-client area of the window.
  158. mIsTrackingMouse = false; // TrackMouseEvent ends when this message is received and needs to be re-applied
  159. CM_LOCK_MUTEX(mSync);
  160. mMouseLeftWindows.push_back(win);
  161. }
  162. break;
  163. case WM_NCLBUTTONUP:
  164. case WM_LBUTTONUP:
  165. // Part of a hack that's done in WM_EXITSIZEMOVE (see there)
  166. if(mMoveResizeMouseUpState = 1)
  167. mMoveResizeMouseUpState = 2;
  168. break;
  169. case WM_NCMOUSEMOVE:
  170. case WM_MOUSEMOVE:
  171. {
  172. // Set up tracking so we get notified when mouse leaves the window
  173. if(!mIsTrackingMouse)
  174. {
  175. TRACKMOUSEEVENT tme = { sizeof(tme) };
  176. tme.dwFlags = TME_LEAVE;
  177. tme.hwndTrack = hWnd;
  178. TrackMouseEvent(&tme);
  179. mIsTrackingMouse = true;
  180. }
  181. POINT mousePos;
  182. mousePos.x = GET_X_LPARAM(lParam);
  183. mousePos.y = GET_Y_LPARAM(lParam);
  184. ClientToScreen(hWnd, &mousePos);
  185. if(!onMouseMoved.empty())
  186. onMouseMoved(Int2(mousePos.x, mousePos.y));
  187. return true;
  188. }
  189. case WM_MOUSEWHEEL:
  190. {
  191. INT16 wheelDelta = GET_WHEEL_DELTA_WPARAM(wParam);
  192. float wheelDeltaFlt = wheelDelta / (float)WHEEL_DELTA;
  193. if(!onMouseWheelScrolled.empty())
  194. onMouseWheelScrolled(wheelDeltaFlt);
  195. return true;
  196. }
  197. case WM_DEADCHAR:
  198. case WM_CHAR:
  199. {
  200. switch (wParam)
  201. {
  202. case VK_BACK:
  203. case 0x0A: // linefeed
  204. case 0x0D: // carriage return
  205. case VK_ESCAPE:
  206. case VK_TAB:
  207. break;
  208. default: // displayable character
  209. {
  210. UINT8 scanCode = (lParam >> 16) & 0xFF;
  211. BYTE keyState[256];
  212. HKL layout = GetKeyboardLayout(0);
  213. if(GetKeyboardState(keyState) == 0)
  214. return 0;
  215. unsigned int vk = MapVirtualKeyEx(scanCode, MAPVK_VSC_TO_VK_EX, layout);
  216. if(vk == 0)
  217. return 0;
  218. bool isDeadKey = (MapVirtualKeyEx(vk, MAPVK_VK_TO_CHAR, layout) & (1 << 31)) != 0;
  219. if(isDeadKey)
  220. return 0;
  221. wchar_t buff[3] = {0};
  222. int numChars = ToUnicodeEx(vk, scanCode, keyState, buff, 3, 0, layout);
  223. // TODO - I am ignoring dead keys here - primarily because I haven't found a good way of retrieving non-combined dead key
  224. // value. ToUnicodeEx and MapVirtualKeyEx only return precombined (i.e. spacing) versions, which can't be combined using other characters.
  225. // I need non-combined version so I can use it with FoldString to apply to a certain character.
  226. UINT32 finalChar = 0;
  227. if(numChars == 1)
  228. finalChar = buff[0];
  229. else
  230. return 0;
  231. if(!onCharInput.empty())
  232. onCharInput(finalChar);
  233. return 0;
  234. }
  235. }
  236. break;
  237. }
  238. case WM_CM_SETCAPTURE:
  239. SetCapture(hWnd);
  240. break;
  241. case WM_CM_RELEASECAPTURE:
  242. ReleaseCapture();
  243. break;
  244. case WM_CAPTURECHANGED:
  245. if(!onMouseCaptureChanged.empty())
  246. onMouseCaptureChanged();
  247. break;
  248. }
  249. return DefWindowProc( hWnd, uMsg, wParam, lParam );
  250. }
  251. LRESULT PlatformWndProc::translateNonClientAreaType(NonClientAreaBorderType type)
  252. {
  253. LRESULT dir = HTCLIENT;
  254. switch(type)
  255. {
  256. case NonClientAreaBorderType::Left:
  257. dir = HTLEFT;
  258. break;
  259. case NonClientAreaBorderType::TopLeft:
  260. dir = HTTOPLEFT;
  261. break;
  262. case NonClientAreaBorderType::Top:
  263. dir = HTTOP;
  264. break;
  265. case NonClientAreaBorderType::TopRight:
  266. dir = HTTOPRIGHT;
  267. break;
  268. case NonClientAreaBorderType::Right:
  269. dir = HTRIGHT;
  270. break;
  271. case NonClientAreaBorderType::BottomRight:
  272. dir = HTBOTTOMRIGHT;
  273. break;
  274. case NonClientAreaBorderType::Bottom:
  275. dir = HTBOTTOM;
  276. break;
  277. case NonClientAreaBorderType::BottomLeft:
  278. dir = HTBOTTOMLEFT;
  279. break;
  280. }
  281. return dir;
  282. }
  283. }