BsPlatformWndProc.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  1. #include "Win32/BsPlatformWndProc.h"
  2. #include "BsRenderWindow.h"
  3. #include "BsCoreApplication.h"
  4. #include "BsInput.h"
  5. #include "BsDebug.h"
  6. #include "BsRenderWindowManager.h"
  7. namespace BansheeEngine
  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. RenderWindowCore* newWindow = (RenderWindowCore*)GetWindowLongPtr(hWnd, GWLP_USERDATA);
  17. if(newWindow->getProperties().isModal())
  18. {
  19. if(!mModalWindowStack.empty())
  20. {
  21. RenderWindowCore* curModalWindow = mModalWindowStack.top();
  22. UINT64 curHwnd;
  23. curModalWindow->getCustomAttribute("WINDOW", &curHwnd);
  24. EnableWindow((HWND)curHwnd, FALSE);
  25. }
  26. else
  27. {
  28. Vector<RenderWindowCore*> renderWindows = RenderWindowCoreManager::instance().getRenderWindows();
  29. for(auto& renderWindow : renderWindows)
  30. {
  31. if(renderWindow == newWindow)
  32. continue;
  33. UINT64 curHwnd;
  34. renderWindow->getCustomAttribute("WINDOW", &curHwnd);
  35. EnableWindow((HWND)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. RenderWindowCore* win = (RenderWindowCore*)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<RenderWindowCore*> newStack;
  68. while(!mModalWindowStack.empty())
  69. {
  70. RenderWindowCore* 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. RenderWindowCore* curModalWindow = mModalWindowStack.top();
  81. UINT64 curHwnd;
  82. curModalWindow->getCustomAttribute("WINDOW", &curHwnd);
  83. EnableWindow((HWND)curHwnd, TRUE);
  84. }
  85. else
  86. reenableWindows = true; // No more modal windows, re-enable any remaining window
  87. }
  88. if(reenableWindows)
  89. {
  90. Vector<RenderWindowCore*> renderWindows = RenderWindowCoreManager::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->getProperties().hasFocus())
  103. win->_windowFocusReceived();
  104. break;
  105. }
  106. case WM_KILLFOCUS:
  107. {
  108. if (win->getProperties().hasFocus())
  109. win->_windowFocusLost();
  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. win->_windowMovedOrResized();
  119. break;
  120. case WM_DISPLAYCHANGE:
  121. win->_windowMovedOrResized();
  122. break;
  123. case WM_SIZE:
  124. win->_windowMovedOrResized();
  125. if (wParam == SIZE_MAXIMIZED)
  126. win->_notifyMaximized();
  127. else if (wParam == SIZE_MINIMIZED)
  128. win->_notifyMinimized();
  129. else if (wParam == SIZE_RESTORED)
  130. win->_notifyRestored();
  131. break;
  132. case WM_SETCURSOR:
  133. if(isCursorHidden())
  134. win32HideCursor();
  135. else
  136. {
  137. switch (LOWORD(lParam))
  138. {
  139. case HTTOPLEFT:
  140. SetCursor(LoadCursor(0, IDC_SIZENWSE));
  141. return 0;
  142. case HTTOP:
  143. SetCursor(LoadCursor(0, IDC_SIZENS));
  144. return 0;
  145. case HTTOPRIGHT:
  146. SetCursor(LoadCursor(0, IDC_SIZENESW));
  147. return 0;
  148. case HTLEFT:
  149. SetCursor(LoadCursor(0, IDC_SIZEWE));
  150. return 0;
  151. case HTRIGHT:
  152. SetCursor(LoadCursor(0, IDC_SIZEWE));
  153. return 0;
  154. case HTBOTTOMLEFT:
  155. SetCursor(LoadCursor(0, IDC_SIZENESW));
  156. return 0;
  157. case HTBOTTOM:
  158. SetCursor(LoadCursor(0, IDC_SIZENS));
  159. return 0;
  160. case HTBOTTOMRIGHT:
  161. SetCursor(LoadCursor(0, IDC_SIZENWSE));
  162. return 0;
  163. }
  164. win32ShowCursor();
  165. }
  166. return true;
  167. case WM_GETMINMAXINFO:
  168. {
  169. // Prevent the window from going smaller than some minimu size
  170. ((MINMAXINFO*)lParam)->ptMinTrackSize.x = 100;
  171. ((MINMAXINFO*)lParam)->ptMinTrackSize.y = 100;
  172. // Ensure maximizes window has proper size and doesn't cover the entire screen
  173. const POINT ptZero = { 0, 0 };
  174. HMONITOR primaryMonitor = MonitorFromPoint(ptZero, MONITOR_DEFAULTTOPRIMARY);
  175. MONITORINFO monitorInfo;
  176. monitorInfo.cbSize = sizeof(MONITORINFO);
  177. GetMonitorInfo(primaryMonitor, &monitorInfo);
  178. ((MINMAXINFO*)lParam)->ptMaxPosition.x = monitorInfo.rcWork.left - monitorInfo.rcMonitor.left;
  179. ((MINMAXINFO*)lParam)->ptMaxPosition.y = monitorInfo.rcWork.top - monitorInfo.rcMonitor.top;
  180. ((MINMAXINFO*)lParam)->ptMaxSize.x = monitorInfo.rcWork.right - monitorInfo.rcWork.left;
  181. ((MINMAXINFO*)lParam)->ptMaxSize.y = monitorInfo.rcWork.bottom - monitorInfo.rcWork.top;
  182. }
  183. break;
  184. case WM_CLOSE:
  185. {
  186. gCoreApplication().stopMainLoop();
  187. return 0;
  188. }
  189. case WM_NCHITTEST:
  190. {
  191. auto iterFind = mNonClientAreas.find(win);
  192. if(iterFind == mNonClientAreas.end())
  193. break;
  194. POINT mousePos;
  195. mousePos.x = GET_X_LPARAM(lParam);
  196. mousePos.y = GET_Y_LPARAM(lParam);
  197. ScreenToClient(hWnd, &mousePos);
  198. Vector2I mousePosInt;
  199. mousePosInt.x = mousePos.x;
  200. mousePosInt.y = mousePos.y;
  201. Vector<NonClientResizeArea>& resizeAreasPerWindow = iterFind->second.resizeAreas;
  202. for(auto area : resizeAreasPerWindow)
  203. {
  204. if(area.area.contains(mousePosInt))
  205. return translateNonClientAreaType(area.type);
  206. }
  207. Vector<Rect2I>& moveAreasPerWindow = iterFind->second.moveAreas;
  208. for(auto area : moveAreasPerWindow)
  209. {
  210. if(area.contains(mousePosInt))
  211. return HTCAPTION;
  212. }
  213. return HTCLIENT;
  214. }
  215. case WM_NCLBUTTONDBLCLK:
  216. // Maximize/Restore on double-click
  217. if (wParam == HTCAPTION)
  218. {
  219. WINDOWPLACEMENT windowPlacement;
  220. windowPlacement.length = sizeof(WINDOWPLACEMENT);
  221. GetWindowPlacement(hWnd, &windowPlacement);
  222. if (windowPlacement.showCmd == SW_MAXIMIZE)
  223. ShowWindow(hWnd, SW_RESTORE);
  224. else
  225. ShowWindow(hWnd, SW_MAXIMIZE);
  226. return 0;
  227. }
  228. break;
  229. case WM_MOUSELEAVE:
  230. {
  231. // Note: Right now I track only mouse leaving client area. So it's possible for the "mouse left window" callback
  232. // to trigger, while the mouse is still in the non-client area of the window.
  233. mIsTrackingMouse = false; // TrackMouseEvent ends when this message is received and needs to be re-applied
  234. BS_LOCK_MUTEX(mSync);
  235. if (!onMouseLeftWindow.empty())
  236. onMouseLeftWindow(win);
  237. }
  238. break;
  239. case WM_LBUTTONUP:
  240. {
  241. ReleaseCapture();
  242. Vector2I intMousePos;
  243. OSPointerButtonStates btnStates;
  244. getMouseData(hWnd, wParam, lParam, intMousePos, btnStates);
  245. if(!onCursorButtonReleased.empty())
  246. onCursorButtonReleased(intMousePos, OSMouseButton::Left, btnStates);
  247. }
  248. break;
  249. case WM_MBUTTONUP:
  250. {
  251. ReleaseCapture();
  252. Vector2I intMousePos;
  253. OSPointerButtonStates btnStates;
  254. getMouseData(hWnd, wParam, lParam, intMousePos, btnStates);
  255. if(!onCursorButtonReleased.empty())
  256. onCursorButtonReleased(intMousePos, OSMouseButton::Middle, btnStates);
  257. }
  258. break;
  259. case WM_RBUTTONUP:
  260. {
  261. ReleaseCapture();
  262. Vector2I intMousePos;
  263. OSPointerButtonStates btnStates;
  264. getMouseData(hWnd, wParam, lParam, intMousePos, btnStates);
  265. if(!onCursorButtonReleased.empty())
  266. onCursorButtonReleased(intMousePos, OSMouseButton::Right, btnStates);
  267. }
  268. break;
  269. case WM_LBUTTONDOWN:
  270. {
  271. SetCapture(hWnd);
  272. Vector2I intMousePos;
  273. OSPointerButtonStates btnStates;
  274. getMouseData(hWnd, wParam, lParam, intMousePos, btnStates);
  275. if(!onCursorButtonPressed.empty())
  276. onCursorButtonPressed(intMousePos, OSMouseButton::Left, btnStates);
  277. }
  278. break;
  279. case WM_MBUTTONDOWN:
  280. {
  281. SetCapture(hWnd);
  282. Vector2I intMousePos;
  283. OSPointerButtonStates btnStates;
  284. getMouseData(hWnd, wParam, lParam, intMousePos, btnStates);
  285. if(!onCursorButtonPressed.empty())
  286. onCursorButtonPressed(intMousePos, OSMouseButton::Middle, btnStates);
  287. }
  288. break;
  289. case WM_RBUTTONDOWN:
  290. {
  291. SetCapture(hWnd);
  292. Vector2I intMousePos;
  293. OSPointerButtonStates btnStates;
  294. getMouseData(hWnd, wParam, lParam, intMousePos, btnStates);
  295. if(!onCursorButtonPressed.empty())
  296. onCursorButtonPressed(intMousePos, OSMouseButton::Right, btnStates);
  297. }
  298. break;
  299. case WM_LBUTTONDBLCLK:
  300. {
  301. Vector2I intMousePos;
  302. OSPointerButtonStates btnStates;
  303. getMouseData(hWnd, wParam, lParam, intMousePos, btnStates);
  304. if(!onCursorDoubleClick.empty())
  305. onCursorDoubleClick(intMousePos, btnStates);
  306. }
  307. break;
  308. case WM_NCMOUSEMOVE:
  309. case WM_MOUSEMOVE:
  310. {
  311. // Set up tracking so we get notified when mouse leaves the window
  312. if(!mIsTrackingMouse)
  313. {
  314. TRACKMOUSEEVENT tme = { sizeof(tme) };
  315. tme.dwFlags = TME_LEAVE;
  316. tme.hwndTrack = hWnd;
  317. TrackMouseEvent(&tme);
  318. mIsTrackingMouse = true;
  319. }
  320. if(uMsg == WM_NCMOUSEMOVE)
  321. return true;
  322. Vector2I intMousePos;
  323. OSPointerButtonStates btnStates;
  324. getMouseData(hWnd, wParam, lParam, intMousePos, btnStates);
  325. if(!onCursorMoved.empty())
  326. onCursorMoved(intMousePos, btnStates);
  327. return true;
  328. }
  329. case WM_MOUSEWHEEL:
  330. {
  331. INT16 wheelDelta = GET_WHEEL_DELTA_WPARAM(wParam);
  332. float wheelDeltaFlt = wheelDelta / (float)WHEEL_DELTA;
  333. if(!onMouseWheelScrolled.empty())
  334. onMouseWheelScrolled(wheelDeltaFlt);
  335. return true;
  336. }
  337. case WM_SYSKEYDOWN:
  338. case WM_KEYDOWN:
  339. {
  340. if(wParam == VK_SHIFT)
  341. {
  342. isShiftPressed = true;
  343. break;
  344. }
  345. if(wParam == VK_CONTROL)
  346. {
  347. isCtrlPressed = true;
  348. break;
  349. }
  350. InputCommandType command = InputCommandType::Backspace;
  351. if(getCommand((unsigned int)wParam, command))
  352. {
  353. if(!onInputCommand.empty())
  354. onInputCommand(command);
  355. return 0;
  356. }
  357. break;
  358. }
  359. case WM_SYSKEYUP:
  360. case WM_KEYUP:
  361. {
  362. if(wParam == VK_SHIFT)
  363. {
  364. isShiftPressed = false;
  365. }
  366. if(wParam == VK_CONTROL)
  367. {
  368. isCtrlPressed = false;
  369. }
  370. break;
  371. }
  372. case WM_CHAR:
  373. {
  374. // TODO - Not handling IME input
  375. switch (wParam)
  376. {
  377. case VK_BACK:
  378. case 0x0A: // linefeed
  379. case 0x0D: // carriage return
  380. case VK_ESCAPE:
  381. case VK_TAB:
  382. break;
  383. default: // displayable character
  384. {
  385. UINT8 scanCode = (lParam >> 16) & 0xFF;
  386. BYTE keyState[256];
  387. HKL layout = GetKeyboardLayout(0);
  388. if(GetKeyboardState(keyState) == 0)
  389. return 0;
  390. unsigned int vk = MapVirtualKeyEx(scanCode, MAPVK_VSC_TO_VK_EX, layout);
  391. if(vk == 0)
  392. return 0;
  393. InputCommandType command = InputCommandType::Backspace;
  394. if(getCommand(vk, command)) // We ignore character combinations that are special commands
  395. return 0;
  396. UINT32 finalChar = (UINT32)wParam;
  397. if(!onCharInput.empty())
  398. onCharInput(finalChar);
  399. return 0;
  400. }
  401. }
  402. break;
  403. }
  404. case WM_BS_SETCAPTURE:
  405. SetCapture(hWnd);
  406. break;
  407. case WM_BS_RELEASECAPTURE:
  408. ReleaseCapture();
  409. break;
  410. case WM_CAPTURECHANGED:
  411. if(!onMouseCaptureChanged.empty())
  412. onMouseCaptureChanged();
  413. break;
  414. }
  415. return DefWindowProc( hWnd, uMsg, wParam, lParam );
  416. }
  417. LRESULT PlatformWndProc::translateNonClientAreaType(NonClientAreaBorderType type)
  418. {
  419. LRESULT dir = HTCLIENT;
  420. switch(type)
  421. {
  422. case NonClientAreaBorderType::Left:
  423. dir = HTLEFT;
  424. break;
  425. case NonClientAreaBorderType::TopLeft:
  426. dir = HTTOPLEFT;
  427. break;
  428. case NonClientAreaBorderType::Top:
  429. dir = HTTOP;
  430. break;
  431. case NonClientAreaBorderType::TopRight:
  432. dir = HTTOPRIGHT;
  433. break;
  434. case NonClientAreaBorderType::Right:
  435. dir = HTRIGHT;
  436. break;
  437. case NonClientAreaBorderType::BottomRight:
  438. dir = HTBOTTOMRIGHT;
  439. break;
  440. case NonClientAreaBorderType::Bottom:
  441. dir = HTBOTTOM;
  442. break;
  443. case NonClientAreaBorderType::BottomLeft:
  444. dir = HTBOTTOMLEFT;
  445. break;
  446. }
  447. return dir;
  448. }
  449. void PlatformWndProc::getMouseData(HWND hWnd, WPARAM wParam, LPARAM lParam, Vector2I& mousePos, OSPointerButtonStates& btnStates)
  450. {
  451. POINT clientPoint;
  452. clientPoint.x = GET_X_LPARAM(lParam);
  453. clientPoint.y = GET_Y_LPARAM(lParam);
  454. ClientToScreen(hWnd, &clientPoint);
  455. mousePos.x = clientPoint.x;
  456. mousePos.y = clientPoint.y;
  457. btnStates.mouseButtons[0] = (wParam & MK_LBUTTON) != 0;
  458. btnStates.mouseButtons[1] = (wParam & MK_MBUTTON) != 0;
  459. btnStates.mouseButtons[2] = (wParam & MK_RBUTTON) != 0;
  460. btnStates.shift = (wParam & MK_SHIFT) != 0;
  461. btnStates.ctrl = (wParam & MK_CONTROL) != 0;
  462. }
  463. bool PlatformWndProc::getCommand(unsigned int virtualKeyCode, InputCommandType& command)
  464. {
  465. switch (virtualKeyCode)
  466. {
  467. case VK_LEFT:
  468. command = isShiftPressed ? InputCommandType::SelectLeft : InputCommandType::CursorMoveLeft;
  469. return true;
  470. case VK_RIGHT:
  471. command = isShiftPressed ? InputCommandType::SelectRight : InputCommandType::CursorMoveRight;
  472. return true;
  473. case VK_UP:
  474. command = isShiftPressed ? InputCommandType::SelectUp : InputCommandType::CursorMoveUp;
  475. return true;
  476. case VK_DOWN:
  477. command = isShiftPressed ? InputCommandType::SelectDown : InputCommandType::CursorMoveDown;
  478. return true;
  479. case VK_ESCAPE:
  480. command = InputCommandType::Escape;
  481. return true;
  482. case VK_RETURN:
  483. command = InputCommandType::Return;
  484. return true;
  485. case VK_BACK:
  486. command = InputCommandType::Backspace;
  487. return true;
  488. case VK_DELETE:
  489. command = InputCommandType::Delete;
  490. return true;
  491. }
  492. return false;
  493. }
  494. }