CmPlatformWndProc.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  1. #include "CmPlatformWndProc.h"
  2. #include "CmRenderWindow.h"
  3. #include "CmApplication.h"
  4. #include "CmInput.h"
  5. #include "CmDebug.h"
  6. #include "CmRenderWindowManager.h"
  7. namespace CamelotFramework
  8. {
  9. bool PlatformWndProc::isShiftPressed = false;
  10. bool PlatformWndProc::isCtrlPressed = false;
  11. LRESULT CALLBACK PlatformWndProc::_win32WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  12. {
  13. if (uMsg == WM_CREATE)
  14. { // Store pointer to Win32Window in user data area
  15. SetWindowLongPtr(hWnd, GWLP_USERDATA, (LONG_PTR)(((LPCREATESTRUCT)lParam)->lpCreateParams));
  16. RenderWindow* newWindow = (RenderWindow*)GetWindowLongPtr(hWnd, GWLP_USERDATA);
  17. if(newWindow->isModal())
  18. {
  19. if(!mModalWindowStack.empty())
  20. {
  21. RenderWindow* curModalWindow = mModalWindowStack.top();
  22. HWND curHwnd;
  23. curModalWindow->getCustomAttribute("WINDOW", &curHwnd);
  24. EnableWindow(curHwnd, FALSE);
  25. }
  26. else
  27. {
  28. Vector<RenderWindow*>::type renderWindows = RenderWindowManager::instance().getRenderWindows();
  29. for(auto& renderWindow : renderWindows)
  30. {
  31. if(renderWindow == newWindow)
  32. continue;
  33. HWND curHwnd;
  34. renderWindow->getCustomAttribute("WINDOW", &curHwnd);
  35. EnableWindow(curHwnd, FALSE);
  36. }
  37. }
  38. mModalWindowStack.push(newWindow);
  39. }
  40. return 0;
  41. }
  42. // look up window instance
  43. // note: it is possible to get a WM_SIZE before WM_CREATE
  44. RenderWindow* win = (RenderWindow*)GetWindowLongPtr(hWnd, GWLP_USERDATA);
  45. if (!win)
  46. return DefWindowProc(hWnd, uMsg, wParam, lParam);
  47. switch( uMsg )
  48. {
  49. case WM_ACTIVATE:
  50. {
  51. bool active = (LOWORD(wParam) != WA_INACTIVE);
  52. if( active )
  53. win->setActive(true);
  54. break;
  55. }
  56. case WM_DESTROY:
  57. {
  58. bool reenableWindows = false;
  59. if(!mModalWindowStack.empty())
  60. {
  61. if(mModalWindowStack.top() == win) // This is the most common case, top-most modal was closed
  62. {
  63. mModalWindowStack.pop();
  64. }
  65. else // Possibly some other window was closed somehow, see if it was modal and remove from stack if it is
  66. {
  67. Stack<RenderWindow*>::type newStack;
  68. while(!mModalWindowStack.empty())
  69. {
  70. RenderWindow* curWindow = mModalWindowStack.top();
  71. mModalWindowStack.pop();
  72. if(curWindow == win)
  73. continue;
  74. newStack.push(curWindow);
  75. }
  76. mModalWindowStack = newStack;
  77. }
  78. if(!mModalWindowStack.empty()) // Enable next modal window
  79. {
  80. RenderWindow* curModalWindow = mModalWindowStack.top();
  81. HWND curHwnd;
  82. curModalWindow->getCustomAttribute("WINDOW", &curHwnd);
  83. EnableWindow(curHwnd, TRUE);
  84. }
  85. else
  86. reenableWindows = true; // No more modal windows, re-enable any remaining window
  87. }
  88. if(reenableWindows)
  89. {
  90. Vector<RenderWindow*>::type renderWindows = RenderWindowManager::instance().getRenderWindows();
  91. for(auto& renderWindow : renderWindows)
  92. {
  93. HWND curHwnd;
  94. renderWindow->getCustomAttribute("WINDOW", &curHwnd);
  95. EnableWindow(curHwnd, TRUE);
  96. }
  97. }
  98. break;
  99. }
  100. case WM_SETFOCUS:
  101. {
  102. if(!win->hasFocus())
  103. windowFocusReceived(win);
  104. break;
  105. }
  106. case WM_KILLFOCUS:
  107. {
  108. if(win->hasFocus())
  109. windowFocusLost(win);
  110. break;
  111. }
  112. case WM_SYSCHAR:
  113. // return zero to bypass defProc and signal we processed the message, unless it's an ALT-space
  114. if (wParam != VK_SPACE)
  115. return 0;
  116. break;
  117. case WM_MOVE:
  118. windowMovedOrResized(win);
  119. break;
  120. case WM_DISPLAYCHANGE:
  121. windowMovedOrResized(win);
  122. break;
  123. case WM_SIZE:
  124. windowMovedOrResized(win);
  125. break;
  126. case WM_SETCURSOR:
  127. if(isCursorHidden())
  128. win32HideCursor();
  129. else
  130. {
  131. switch (LOWORD(lParam))
  132. {
  133. case HTTOPLEFT:
  134. SetCursor(LoadCursor(0, IDC_SIZENWSE));
  135. return 0;
  136. case HTTOP:
  137. SetCursor(LoadCursor(0, IDC_SIZENS));
  138. return 0;
  139. case HTTOPRIGHT:
  140. SetCursor(LoadCursor(0, IDC_SIZENESW));
  141. return 0;
  142. case HTLEFT:
  143. SetCursor(LoadCursor(0, IDC_SIZEWE));
  144. return 0;
  145. case HTRIGHT:
  146. SetCursor(LoadCursor(0, IDC_SIZEWE));
  147. return 0;
  148. case HTBOTTOMLEFT:
  149. SetCursor(LoadCursor(0, IDC_SIZENESW));
  150. return 0;
  151. case HTBOTTOM:
  152. SetCursor(LoadCursor(0, IDC_SIZENS));
  153. return 0;
  154. case HTBOTTOMRIGHT:
  155. SetCursor(LoadCursor(0, IDC_SIZENWSE));
  156. return 0;
  157. }
  158. win32ShowCursor();
  159. }
  160. return true;
  161. case WM_GETMINMAXINFO:
  162. // Prevent the window from going smaller than some minimu size
  163. ((MINMAXINFO*)lParam)->ptMinTrackSize.x = 100;
  164. ((MINMAXINFO*)lParam)->ptMinTrackSize.y = 100;
  165. break;
  166. case WM_CLOSE:
  167. {
  168. // TODO - Only stop main loop if primary window is closed!!
  169. gApplication().stopMainLoop();
  170. return 0;
  171. }
  172. case WM_NCHITTEST:
  173. {
  174. auto iterFind = mNonClientAreas.find(win);
  175. if(iterFind == mNonClientAreas.end())
  176. break;
  177. POINT mousePos;
  178. mousePos.x = GET_X_LPARAM(lParam);
  179. mousePos.y = GET_Y_LPARAM(lParam);
  180. ScreenToClient(hWnd, &mousePos);
  181. Vector2I mousePosInt;
  182. mousePosInt.x = mousePos.x;
  183. mousePosInt.y = mousePos.y;
  184. Vector<NonClientResizeArea>::type& resizeAreasPerWindow = iterFind->second.resizeAreas;
  185. for(auto area : resizeAreasPerWindow)
  186. {
  187. if(area.area.contains(mousePosInt))
  188. return translateNonClientAreaType(area.type);
  189. }
  190. Vector<RectI>::type& moveAreasPerWindow = iterFind->second.moveAreas;
  191. for(auto area : moveAreasPerWindow)
  192. {
  193. if(area.contains(mousePosInt))
  194. return HTCAPTION;
  195. }
  196. return HTCLIENT;
  197. }
  198. case WM_MOUSELEAVE:
  199. {
  200. // Note: Right now I track only mouse leaving client area. So it's possible for the "mouse left window" callback
  201. // to trigger, while the mouse is still in the non-client area of the window.
  202. mIsTrackingMouse = false; // TrackMouseEvent ends when this message is received and needs to be re-applied
  203. CM_LOCK_MUTEX(mSync);
  204. mMouseLeftWindows.push_back(win);
  205. }
  206. break;
  207. case WM_LBUTTONUP:
  208. {
  209. ReleaseCapture();
  210. Vector2I intMousePos;
  211. OSPositionalInputButtonStates btnStates;
  212. getMouseData(hWnd, wParam, lParam, intMousePos, btnStates);
  213. if(!onCursorButtonReleased.empty())
  214. onCursorButtonReleased(intMousePos, OSMouseButton::Left, btnStates);
  215. }
  216. break;
  217. case WM_MBUTTONUP:
  218. {
  219. ReleaseCapture();
  220. Vector2I intMousePos;
  221. OSPositionalInputButtonStates btnStates;
  222. getMouseData(hWnd, wParam, lParam, intMousePos, btnStates);
  223. if(!onCursorButtonReleased.empty())
  224. onCursorButtonReleased(intMousePos, OSMouseButton::Middle, btnStates);
  225. }
  226. break;
  227. case WM_RBUTTONUP:
  228. {
  229. ReleaseCapture();
  230. Vector2I intMousePos;
  231. OSPositionalInputButtonStates btnStates;
  232. getMouseData(hWnd, wParam, lParam, intMousePos, btnStates);
  233. if(!onCursorButtonReleased.empty())
  234. onCursorButtonReleased(intMousePos, OSMouseButton::Right, btnStates);
  235. }
  236. break;
  237. case WM_LBUTTONDOWN:
  238. {
  239. SetCapture(hWnd);
  240. Vector2I intMousePos;
  241. OSPositionalInputButtonStates btnStates;
  242. getMouseData(hWnd, wParam, lParam, intMousePos, btnStates);
  243. if(!onCursorButtonPressed.empty())
  244. onCursorButtonPressed(intMousePos, OSMouseButton::Left, btnStates);
  245. }
  246. break;
  247. case WM_MBUTTONDOWN:
  248. {
  249. SetCapture(hWnd);
  250. Vector2I intMousePos;
  251. OSPositionalInputButtonStates btnStates;
  252. getMouseData(hWnd, wParam, lParam, intMousePos, btnStates);
  253. if(!onCursorButtonPressed.empty())
  254. onCursorButtonPressed(intMousePos, OSMouseButton::Middle, btnStates);
  255. }
  256. break;
  257. case WM_RBUTTONDOWN:
  258. {
  259. SetCapture(hWnd);
  260. Vector2I intMousePos;
  261. OSPositionalInputButtonStates btnStates;
  262. getMouseData(hWnd, wParam, lParam, intMousePos, btnStates);
  263. if(!onCursorButtonPressed.empty())
  264. onCursorButtonPressed(intMousePos, OSMouseButton::Right, btnStates);
  265. }
  266. break;
  267. case WM_LBUTTONDBLCLK:
  268. {
  269. Vector2I intMousePos;
  270. OSPositionalInputButtonStates btnStates;
  271. getMouseData(hWnd, wParam, lParam, intMousePos, btnStates);
  272. if(!onCursorDoubleClick.empty())
  273. onCursorDoubleClick(intMousePos, btnStates);
  274. }
  275. break;
  276. case WM_NCMOUSEMOVE:
  277. case WM_MOUSEMOVE:
  278. {
  279. // Set up tracking so we get notified when mouse leaves the window
  280. if(!mIsTrackingMouse)
  281. {
  282. TRACKMOUSEEVENT tme = { sizeof(tme) };
  283. tme.dwFlags = TME_LEAVE;
  284. tme.hwndTrack = hWnd;
  285. TrackMouseEvent(&tme);
  286. mIsTrackingMouse = true;
  287. }
  288. if(uMsg == WM_NCMOUSEMOVE)
  289. return true;
  290. Vector2I intMousePos;
  291. OSPositionalInputButtonStates btnStates;
  292. getMouseData(hWnd, wParam, lParam, intMousePos, btnStates);
  293. if(!onCursorMoved.empty())
  294. onCursorMoved(intMousePos, btnStates);
  295. return true;
  296. }
  297. case WM_MOUSEWHEEL:
  298. {
  299. INT16 wheelDelta = GET_WHEEL_DELTA_WPARAM(wParam);
  300. float wheelDeltaFlt = wheelDelta / (float)WHEEL_DELTA;
  301. if(!onMouseWheelScrolled.empty())
  302. onMouseWheelScrolled(wheelDeltaFlt);
  303. return true;
  304. }
  305. case WM_SYSKEYDOWN:
  306. case WM_KEYDOWN:
  307. {
  308. if(wParam == VK_SHIFT)
  309. {
  310. isShiftPressed = true;
  311. break;
  312. }
  313. if(wParam == VK_CONTROL)
  314. {
  315. isCtrlPressed = true;
  316. break;
  317. }
  318. InputCommandType command = InputCommandType::Backspace;
  319. if(getCommand((unsigned int)wParam, command))
  320. {
  321. if(!onInputCommand.empty())
  322. onInputCommand(command);
  323. return 0;
  324. }
  325. break;
  326. }
  327. case WM_SYSKEYUP:
  328. case WM_KEYUP:
  329. {
  330. if(wParam == VK_SHIFT)
  331. {
  332. isShiftPressed = false;
  333. }
  334. if(wParam == VK_CONTROL)
  335. {
  336. isCtrlPressed = false;
  337. }
  338. break;
  339. }
  340. case WM_CHAR:
  341. {
  342. // TODO - Not handling IME input
  343. switch (wParam)
  344. {
  345. case VK_BACK:
  346. case 0x0A: // linefeed
  347. case 0x0D: // carriage return
  348. case VK_ESCAPE:
  349. case VK_TAB:
  350. break;
  351. default: // displayable character
  352. {
  353. UINT8 scanCode = (lParam >> 16) & 0xFF;
  354. BYTE keyState[256];
  355. HKL layout = GetKeyboardLayout(0);
  356. if(GetKeyboardState(keyState) == 0)
  357. return 0;
  358. unsigned int vk = MapVirtualKeyEx(scanCode, MAPVK_VSC_TO_VK_EX, layout);
  359. if(vk == 0)
  360. return 0;
  361. InputCommandType command = InputCommandType::Backspace;
  362. if(getCommand(vk, command)) // We ignore character combinations that are special commands
  363. return 0;
  364. UINT32 finalChar = (UINT32)wParam;
  365. if(!onCharInput.empty())
  366. onCharInput(finalChar);
  367. return 0;
  368. }
  369. }
  370. break;
  371. }
  372. case WM_CM_SETCAPTURE:
  373. SetCapture(hWnd);
  374. break;
  375. case WM_CM_RELEASECAPTURE:
  376. ReleaseCapture();
  377. break;
  378. case WM_CAPTURECHANGED:
  379. if(!onMouseCaptureChanged.empty())
  380. onMouseCaptureChanged();
  381. break;
  382. }
  383. return DefWindowProc( hWnd, uMsg, wParam, lParam );
  384. }
  385. LRESULT PlatformWndProc::translateNonClientAreaType(NonClientAreaBorderType type)
  386. {
  387. LRESULT dir = HTCLIENT;
  388. switch(type)
  389. {
  390. case NonClientAreaBorderType::Left:
  391. dir = HTLEFT;
  392. break;
  393. case NonClientAreaBorderType::TopLeft:
  394. dir = HTTOPLEFT;
  395. break;
  396. case NonClientAreaBorderType::Top:
  397. dir = HTTOP;
  398. break;
  399. case NonClientAreaBorderType::TopRight:
  400. dir = HTTOPRIGHT;
  401. break;
  402. case NonClientAreaBorderType::Right:
  403. dir = HTRIGHT;
  404. break;
  405. case NonClientAreaBorderType::BottomRight:
  406. dir = HTBOTTOMRIGHT;
  407. break;
  408. case NonClientAreaBorderType::Bottom:
  409. dir = HTBOTTOM;
  410. break;
  411. case NonClientAreaBorderType::BottomLeft:
  412. dir = HTBOTTOMLEFT;
  413. break;
  414. }
  415. return dir;
  416. }
  417. void PlatformWndProc::getMouseData(HWND hWnd, WPARAM wParam, LPARAM lParam, Vector2I& mousePos, OSPositionalInputButtonStates& btnStates)
  418. {
  419. POINT clientPoint;
  420. clientPoint.x = GET_X_LPARAM(lParam);
  421. clientPoint.y = GET_Y_LPARAM(lParam);
  422. ClientToScreen(hWnd, &clientPoint);
  423. mousePos.x = clientPoint.x;
  424. mousePos.y = clientPoint.y;
  425. btnStates.mouseButtons[0] = (wParam & MK_LBUTTON) != 0;
  426. btnStates.mouseButtons[1] = (wParam & MK_MBUTTON) != 0;
  427. btnStates.mouseButtons[2] = (wParam & MK_RBUTTON) != 0;
  428. btnStates.shift = (wParam & MK_SHIFT) != 0;
  429. btnStates.ctrl = (wParam & MK_CONTROL) != 0;
  430. }
  431. bool PlatformWndProc::getCommand(unsigned int virtualKeyCode, InputCommandType& command)
  432. {
  433. switch (virtualKeyCode)
  434. {
  435. case VK_LEFT:
  436. command = isShiftPressed ? InputCommandType::SelectLeft : InputCommandType::CursorMoveLeft;
  437. return true;
  438. case VK_RIGHT:
  439. command = isShiftPressed ? InputCommandType::SelectRight : InputCommandType::CursorMoveRight;
  440. return true;
  441. case VK_UP:
  442. command = isShiftPressed ? InputCommandType::SelectUp : InputCommandType::CursorMoveUp;
  443. return true;
  444. case VK_DOWN:
  445. command = isShiftPressed ? InputCommandType::SelectDown : InputCommandType::CursorMoveDown;
  446. return true;
  447. case VK_ESCAPE:
  448. command = InputCommandType::Escape;
  449. return true;
  450. case VK_RETURN:
  451. command = InputCommandType::Return;
  452. return true;
  453. case VK_BACK:
  454. command = InputCommandType::Backspace;
  455. return true;
  456. case VK_DELETE:
  457. command = InputCommandType::Delete;
  458. return true;
  459. }
  460. return false;
  461. }
  462. }