CmPlatformWndProc.cpp 7.3 KB

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