CmPlatformWndProc.cpp 8.0 KB

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