BsWin32Platform.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "Win32/BsWin32Platform.h"
  4. #include "RenderAPI/BsRenderWindow.h"
  5. #include "Image/BsPixelUtil.h"
  6. #include "BsCoreApplication.h"
  7. #include "Debug/BsDebug.h"
  8. #include "Managers/BsRenderWindowManager.h"
  9. #include "Win32/BsWin32Defs.h"
  10. #include "Win32/BsWin32DropTarget.h"
  11. #include "Win32/BsWin32PlatformData.h"
  12. #include "Win32/BsWin32PlatformUtility.h"
  13. #include "TimeAPI.h"
  14. namespace bs
  15. {
  16. Event<void(const Vector2I&, const OSPointerButtonStates&)> Platform::onCursorMoved;
  17. Event<void(const Vector2I&, OSMouseButton button, const OSPointerButtonStates&)> Platform::onCursorButtonPressed;
  18. Event<void(const Vector2I&, OSMouseButton button, const OSPointerButtonStates&)> Platform::onCursorButtonReleased;
  19. Event<void(const Vector2I&, const OSPointerButtonStates&)> Platform::onCursorDoubleClick;
  20. Event<void(InputCommandType)> Platform::onInputCommand;
  21. Event<void(float)> Platform::onMouseWheelScrolled;
  22. Event<void(UINT32)> Platform::onCharInput;
  23. Event<void(ct::RenderWindow*)> Platform::onMouseLeftWindow;
  24. Event<void()> Platform::onMouseCaptureChanged;
  25. Platform::Pimpl* Platform::mData = bs_new<Platform::Pimpl>();
  26. Platform::~Platform()
  27. {
  28. bs_delete(mData);
  29. mData = nullptr;
  30. }
  31. Vector2I Platform::getCursorPosition()
  32. {
  33. Vector2I screenPos;
  34. POINT cursorPos;
  35. GetCursorPos(&cursorPos);
  36. screenPos.x = cursorPos.x;
  37. screenPos.y = cursorPos.y;
  38. return screenPos;
  39. }
  40. void Platform::setCursorPosition(const Vector2I& screenPos)
  41. {
  42. SetCursorPos(screenPos.x, screenPos.y);
  43. }
  44. void Platform::captureMouse(const RenderWindow& window)
  45. {
  46. SPtr<RenderWindow> primaryWindow = gCoreApplication().getPrimaryWindow();
  47. UINT64 hwnd;
  48. primaryWindow->getCustomAttribute("WINDOW", &hwnd);
  49. PostMessage((HWND)hwnd, WM_BS_SETCAPTURE, WPARAM((HWND)hwnd), 0);
  50. }
  51. void Platform::releaseMouseCapture()
  52. {
  53. SPtr<RenderWindow> primaryWindow = gCoreApplication().getPrimaryWindow();
  54. UINT64 hwnd;
  55. primaryWindow->getCustomAttribute("WINDOW", &hwnd);
  56. PostMessage((HWND)hwnd, WM_BS_RELEASECAPTURE, WPARAM((HWND)hwnd), 0);
  57. }
  58. bool Platform::isPointOverWindow(const RenderWindow& window, const Vector2I& screenPos)
  59. {
  60. SPtr<RenderWindow> primaryWindow = gCoreApplication().getPrimaryWindow();
  61. POINT point;
  62. point.x = screenPos.x;
  63. point.y = screenPos.y;
  64. UINT64 hwndToCheck;
  65. window.getCustomAttribute("WINDOW", &hwndToCheck);
  66. HWND hwndUnderPos = WindowFromPoint(point);
  67. return hwndUnderPos == (HWND)hwndToCheck;
  68. }
  69. void Platform::hideCursor()
  70. {
  71. if (mData->mIsCursorHidden)
  72. return;
  73. mData->mIsCursorHidden = true;
  74. // ShowCursor(FALSE) doesn't work. Presumably because we're in the wrong thread, and using
  75. // WM_SETCURSOR in message loop to hide the cursor is smarter solution anyway.
  76. SPtr<RenderWindow> primaryWindow = gCoreApplication().getPrimaryWindow();
  77. UINT64 hwnd;
  78. primaryWindow->getCustomAttribute("WINDOW", &hwnd);
  79. PostMessage((HWND)hwnd, WM_SETCURSOR, WPARAM((HWND)hwnd), (LPARAM)MAKELONG(HTCLIENT, WM_MOUSEMOVE));
  80. }
  81. void Platform::showCursor()
  82. {
  83. if (!mData->mIsCursorHidden)
  84. return;
  85. mData->mIsCursorHidden = false;
  86. // ShowCursor(FALSE) doesn't work. Presumably because we're in the wrong thread, and using
  87. // WM_SETCURSOR in message loop to hide the cursor is smarter solution anyway.
  88. SPtr<RenderWindow> primaryWindow = gCoreApplication().getPrimaryWindow();
  89. UINT64 hwnd;
  90. primaryWindow->getCustomAttribute("WINDOW", &hwnd);
  91. PostMessage((HWND)hwnd, WM_SETCURSOR, WPARAM((HWND)hwnd), (LPARAM)MAKELONG(HTCLIENT, WM_MOUSEMOVE));
  92. }
  93. bool Platform::isCursorHidden()
  94. {
  95. return mData->mIsCursorHidden;
  96. }
  97. void Platform::clipCursorToWindow(const RenderWindow& window)
  98. {
  99. UINT64 hwnd;
  100. window.getCustomAttribute("WINDOW", &hwnd);
  101. // Clip cursor to the window
  102. RECT clipWindowRect;
  103. if(GetWindowRect((HWND)hwnd, &clipWindowRect))
  104. {
  105. ClipCursor(&clipWindowRect);
  106. }
  107. }
  108. void Platform::clipCursorToRect(const Rect2I& screenRect)
  109. {
  110. RECT clipWindowRect;
  111. clipWindowRect.left = screenRect.x;
  112. clipWindowRect.top = screenRect.y;
  113. clipWindowRect.right = screenRect.x + screenRect.width;
  114. clipWindowRect.bottom = screenRect.y + screenRect.height;
  115. ClipCursor(&clipWindowRect);
  116. }
  117. void Platform::clipCursorDisable()
  118. {
  119. ClipCursor(NULL);
  120. }
  121. // TODO - Add support for animated custom cursor
  122. void Platform::setCursor(PixelData& pixelData, const Vector2I& hotSpot)
  123. {
  124. if (mData->mUsingCustomCursor)
  125. {
  126. SetCursor(0);
  127. DestroyIcon(mData->mCursor.cursor);
  128. }
  129. mData->mUsingCustomCursor = true;
  130. Vector<Color> pixels = pixelData.getColors();
  131. UINT32 width = pixelData.getWidth();
  132. UINT32 height = pixelData.getHeight();
  133. HBITMAP hBitmap = Win32PlatformUtility::createBitmap((Color*)pixels.data(), width, height, false);
  134. HBITMAP hMonoBitmap = CreateBitmap(width, height, 1, 1, nullptr);
  135. ICONINFO iconinfo = {0};
  136. iconinfo.fIcon = FALSE;
  137. iconinfo.xHotspot = (DWORD)hotSpot.x;
  138. iconinfo.yHotspot = (DWORD)hotSpot.y;
  139. iconinfo.hbmMask = hMonoBitmap;
  140. iconinfo.hbmColor = hBitmap;
  141. mData->mCursor.cursor = CreateIconIndirect(&iconinfo);
  142. DeleteObject(hBitmap);
  143. DeleteObject(hMonoBitmap);
  144. // Make sure we notify the message loop to perform the actual cursor update
  145. SPtr<RenderWindow> primaryWindow = gCoreApplication().getPrimaryWindow();
  146. UINT64 hwnd;
  147. primaryWindow->getCustomAttribute("WINDOW", &hwnd);
  148. PostMessage((HWND)hwnd, WM_SETCURSOR, WPARAM((HWND)hwnd), (LPARAM)MAKELONG(HTCLIENT, WM_MOUSEMOVE));
  149. }
  150. void Platform::setIcon(const PixelData& pixelData)
  151. {
  152. SPtr<PixelData> resizedData = PixelData::create(32, 32, 1, PF_RGBA8);
  153. PixelUtil::scale(pixelData, *resizedData);
  154. Vector<Color> pixels = pixelData.getColors();
  155. UINT32 width = pixelData.getWidth();
  156. UINT32 height = pixelData.getHeight();
  157. HBITMAP hBitmap = Win32PlatformUtility::createBitmap((Color*)pixels.data(), width, height, false);
  158. HBITMAP hMonoBitmap = CreateBitmap(width, height, 1, 1, nullptr);
  159. ICONINFO iconinfo = { 0 };
  160. iconinfo.fIcon = TRUE;
  161. iconinfo.xHotspot = 0;
  162. iconinfo.yHotspot = 0;
  163. iconinfo.hbmMask = hMonoBitmap;
  164. iconinfo.hbmColor = hBitmap;
  165. HICON icon = CreateIconIndirect(&iconinfo);
  166. DeleteObject(hBitmap);
  167. DeleteObject(hMonoBitmap);
  168. // Make sure we notify the message loop to perform the actual cursor update
  169. SPtr<RenderWindow> primaryWindow = gCoreApplication().getPrimaryWindow();
  170. UINT64 hwnd;
  171. primaryWindow->getCustomAttribute("WINDOW", &hwnd);
  172. PostMessage((HWND)hwnd, WM_SETICON, WPARAM(ICON_BIG), (LPARAM)icon);
  173. }
  174. void Platform::setCaptionNonClientAreas(const ct::RenderWindow& window, const Vector<Rect2I>& nonClientAreas)
  175. {
  176. Lock lock(mData->mSync);
  177. mData->mNonClientAreas[&window].moveAreas = nonClientAreas;
  178. }
  179. void Platform::setResizeNonClientAreas(const ct::RenderWindow& window, const Vector<NonClientResizeArea>& nonClientAreas)
  180. {
  181. Lock lock(mData->mSync);
  182. mData->mNonClientAreas[&window].resizeAreas = nonClientAreas;
  183. }
  184. void Platform::resetNonClientAreas(const ct::RenderWindow& window)
  185. {
  186. Lock lock(mData->mSync);
  187. auto iterFind = mData->mNonClientAreas.find(&window);
  188. if (iterFind != end(mData->mNonClientAreas))
  189. mData->mNonClientAreas.erase(iterFind);
  190. }
  191. void Platform::sleep(UINT32 duration)
  192. {
  193. Sleep((DWORD)duration);
  194. }
  195. OSDropTarget& Platform::createDropTarget(const RenderWindow* window, int x, int y, unsigned int width, unsigned int height)
  196. {
  197. Win32DropTarget* win32DropTarget = nullptr;
  198. auto iterFind = mData->mDropTargets.dropTargetsPerWindow.find(window);
  199. if (iterFind == mData->mDropTargets.dropTargetsPerWindow.end())
  200. {
  201. UINT64 hwnd;
  202. window->getCustomAttribute("WINDOW", &hwnd);
  203. win32DropTarget = bs_new<Win32DropTarget>((HWND)hwnd);
  204. mData->mDropTargets.dropTargetsPerWindow[window] = win32DropTarget;
  205. {
  206. Lock lock(mData->mSync);
  207. mData->mDropTargets.dropTargetsToInitialize.push_back(win32DropTarget);
  208. }
  209. }
  210. else
  211. win32DropTarget = iterFind->second;
  212. OSDropTarget* newDropTarget = new (bs_alloc<OSDropTarget>()) OSDropTarget(window, x, y, width, height);
  213. win32DropTarget->registerDropTarget(newDropTarget);
  214. return *newDropTarget;
  215. }
  216. void Platform::destroyDropTarget(OSDropTarget& target)
  217. {
  218. auto iterFind = mData->mDropTargets.dropTargetsPerWindow.find(target.getOwnerWindow());
  219. if (iterFind == mData->mDropTargets.dropTargetsPerWindow.end())
  220. {
  221. LOGWRN("Attempting to destroy a drop target but cannot find its parent window.");
  222. }
  223. else
  224. {
  225. Win32DropTarget* win32DropTarget = iterFind->second;
  226. win32DropTarget->unregisterDropTarget(&target);
  227. if(win32DropTarget->getNumDropTargets() == 0)
  228. {
  229. mData->mDropTargets.dropTargetsPerWindow.erase(iterFind);
  230. {
  231. Lock lock(mData->mSync);
  232. mData->mDropTargets.dropTargetsToDestroy.push_back(win32DropTarget);
  233. }
  234. }
  235. }
  236. BS_PVT_DELETE(OSDropTarget, &target);
  237. }
  238. void Platform::_messagePump()
  239. {
  240. MSG msg;
  241. while (PeekMessage(&msg, nullptr, 0U, 0U, PM_REMOVE))
  242. {
  243. TranslateMessage(&msg);
  244. DispatchMessage(&msg);
  245. }
  246. }
  247. void Platform::_startUp()
  248. {
  249. Lock lock(mData->mSync);
  250. if (timeBeginPeriod(1) == TIMERR_NOCANDO)
  251. {
  252. LOGWRN("Unable to set timer resolution to 1ms. This can cause significant waste " \
  253. "in performance for waiting threads.");
  254. }
  255. mData->mRequiresStartUp = true;
  256. }
  257. void Platform::_update()
  258. {
  259. for (auto& dropTarget : mData->mDropTargets.dropTargetsPerWindow)
  260. {
  261. dropTarget.second->update();
  262. }
  263. }
  264. void Platform::_coreUpdate()
  265. {
  266. {
  267. Lock lock(mData->mSync);
  268. if (mData->mRequiresStartUp)
  269. {
  270. OleInitialize(nullptr);
  271. mData->mRequiresStartUp = false;
  272. }
  273. }
  274. {
  275. Lock lock(mData->mSync);
  276. for (auto& dropTargetToDestroy : mData->mDropTargets.dropTargetsToDestroy)
  277. {
  278. dropTargetToDestroy->unregisterWithOS();
  279. dropTargetToDestroy->Release();
  280. }
  281. mData->mDropTargets.dropTargetsToDestroy.clear();
  282. }
  283. {
  284. Lock lock(mData->mSync);
  285. for (auto& dropTargetToInit : mData->mDropTargets.dropTargetsToInitialize)
  286. {
  287. dropTargetToInit->registerWithOS();
  288. }
  289. mData->mDropTargets.dropTargetsToInitialize.clear();
  290. }
  291. _messagePump();
  292. {
  293. Lock lock(mData->mSync);
  294. if (mData->mRequiresShutDown)
  295. {
  296. OleUninitialize();
  297. mData->mRequiresShutDown = false;
  298. }
  299. }
  300. }
  301. void Platform::_shutDown()
  302. {
  303. Lock lock(mData->mSync);
  304. timeEndPeriod(1);
  305. mData->mRequiresShutDown = true;
  306. }
  307. bool isShiftPressed = false;
  308. bool isCtrlPressed = false;
  309. /** Translate engine non client area to win32 non client area. */
  310. LRESULT translateNonClientAreaType(NonClientAreaBorderType type)
  311. {
  312. LRESULT dir = HTCLIENT;
  313. switch(type)
  314. {
  315. case NonClientAreaBorderType::Left:
  316. dir = HTLEFT;
  317. break;
  318. case NonClientAreaBorderType::TopLeft:
  319. dir = HTTOPLEFT;
  320. break;
  321. case NonClientAreaBorderType::Top:
  322. dir = HTTOP;
  323. break;
  324. case NonClientAreaBorderType::TopRight:
  325. dir = HTTOPRIGHT;
  326. break;
  327. case NonClientAreaBorderType::Right:
  328. dir = HTRIGHT;
  329. break;
  330. case NonClientAreaBorderType::BottomRight:
  331. dir = HTBOTTOMRIGHT;
  332. break;
  333. case NonClientAreaBorderType::Bottom:
  334. dir = HTBOTTOM;
  335. break;
  336. case NonClientAreaBorderType::BottomLeft:
  337. dir = HTBOTTOMLEFT;
  338. break;
  339. }
  340. return dir;
  341. }
  342. /** Method triggered whenever a mouse event happens. */
  343. void getMouseData(HWND hWnd, WPARAM wParam, LPARAM lParam, bool nonClient, Vector2I& mousePos, OSPointerButtonStates& btnStates)
  344. {
  345. POINT clientPoint;
  346. clientPoint.x = GET_X_LPARAM(lParam);
  347. clientPoint.y = GET_Y_LPARAM(lParam);
  348. if (!nonClient)
  349. ClientToScreen(hWnd, &clientPoint);
  350. mousePos.x = clientPoint.x;
  351. mousePos.y = clientPoint.y;
  352. btnStates.mouseButtons[0] = (wParam & MK_LBUTTON) != 0;
  353. btnStates.mouseButtons[1] = (wParam & MK_MBUTTON) != 0;
  354. btnStates.mouseButtons[2] = (wParam & MK_RBUTTON) != 0;
  355. btnStates.shift = (wParam & MK_SHIFT) != 0;
  356. btnStates.ctrl = (wParam & MK_CONTROL) != 0;
  357. }
  358. /**
  359. * Converts a virtual key code into an input command, if possible. Returns true if conversion was done.
  360. *
  361. * @param[in] virtualKeyCode Virtual key code to try to translate to a command.
  362. * @param[out] command Input command. Only valid if function returns true.
  363. * @param[in] ignoreMovement If true, then movement keys (up/down/left/right) will be ignored and not considered
  364. * as input commands (useful if you need to parse num keys as numbers and not movement).
  365. */
  366. bool getCommand(unsigned int virtualKeyCode, InputCommandType& command)
  367. {
  368. switch (virtualKeyCode)
  369. {
  370. case VK_LEFT:
  371. command = isShiftPressed ? InputCommandType::SelectLeft : InputCommandType::CursorMoveLeft;
  372. return true;
  373. case VK_RIGHT:
  374. command = isShiftPressed ? InputCommandType::SelectRight : InputCommandType::CursorMoveRight;
  375. return true;
  376. case VK_UP:
  377. command = isShiftPressed ? InputCommandType::SelectUp : InputCommandType::CursorMoveUp;
  378. return true;
  379. case VK_DOWN:
  380. command = isShiftPressed ? InputCommandType::SelectDown : InputCommandType::CursorMoveDown;
  381. return true;
  382. case VK_ESCAPE:
  383. command = InputCommandType::Escape;
  384. return true;
  385. case VK_RETURN:
  386. command = isShiftPressed ? InputCommandType::Return : InputCommandType::Confirm;
  387. return true;
  388. case VK_BACK:
  389. command = InputCommandType::Backspace;
  390. return true;
  391. case VK_DELETE:
  392. command = InputCommandType::Delete;
  393. return true;
  394. }
  395. return false;
  396. }
  397. LRESULT CALLBACK Win32Platform::_win32WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  398. {
  399. if (uMsg == WM_CREATE)
  400. { // Store pointer to Win32Window in user data area
  401. SetWindowLongPtr(hWnd, GWLP_USERDATA, (LONG_PTR)(((LPCREATESTRUCT)lParam)->lpCreateParams));
  402. ct::RenderWindow* newWindow = (ct::RenderWindow*)GetWindowLongPtr(hWnd, GWLP_USERDATA);
  403. if (newWindow != nullptr)
  404. {
  405. const RenderWindowProperties& props = newWindow->getProperties();
  406. if (!props.isHidden())
  407. ShowWindow(hWnd, SW_SHOWNOACTIVATE);
  408. }
  409. else
  410. ShowWindow(hWnd, SW_SHOWNOACTIVATE);
  411. return 0;
  412. }
  413. ct::RenderWindow* win = (ct::RenderWindow*)GetWindowLongPtr(hWnd, GWLP_USERDATA);
  414. if (!win)
  415. return DefWindowProc(hWnd, uMsg, wParam, lParam);
  416. switch( uMsg )
  417. {
  418. case WM_SETFOCUS:
  419. {
  420. if (!win->getProperties().hasFocus())
  421. win->_windowFocusReceived();
  422. return 0;
  423. }
  424. case WM_KILLFOCUS:
  425. {
  426. if (win->getProperties().hasFocus())
  427. win->_windowFocusLost();
  428. return 0;
  429. }
  430. case WM_SYSCHAR:
  431. if (wParam != VK_SPACE)
  432. return 0;
  433. break;
  434. case WM_MOVE:
  435. win->_windowMovedOrResized();
  436. return 0;
  437. case WM_DISPLAYCHANGE:
  438. win->_windowMovedOrResized();
  439. break;
  440. case WM_SIZE:
  441. win->_windowMovedOrResized();
  442. if (wParam == SIZE_MAXIMIZED)
  443. win->_notifyMaximized();
  444. else if (wParam == SIZE_MINIMIZED)
  445. win->_notifyMinimized();
  446. else if (wParam == SIZE_RESTORED)
  447. win->_notifyRestored();
  448. return 0;
  449. case WM_SETCURSOR:
  450. if(isCursorHidden())
  451. SetCursor(nullptr);
  452. else
  453. {
  454. switch (LOWORD(lParam))
  455. {
  456. case HTTOPLEFT:
  457. SetCursor(LoadCursor(0, IDC_SIZENWSE));
  458. return 0;
  459. case HTTOP:
  460. SetCursor(LoadCursor(0, IDC_SIZENS));
  461. return 0;
  462. case HTTOPRIGHT:
  463. SetCursor(LoadCursor(0, IDC_SIZENESW));
  464. return 0;
  465. case HTLEFT:
  466. SetCursor(LoadCursor(0, IDC_SIZEWE));
  467. return 0;
  468. case HTRIGHT:
  469. SetCursor(LoadCursor(0, IDC_SIZEWE));
  470. return 0;
  471. case HTBOTTOMLEFT:
  472. SetCursor(LoadCursor(0, IDC_SIZENESW));
  473. return 0;
  474. case HTBOTTOM:
  475. SetCursor(LoadCursor(0, IDC_SIZENS));
  476. return 0;
  477. case HTBOTTOMRIGHT:
  478. SetCursor(LoadCursor(0, IDC_SIZENWSE));
  479. return 0;
  480. }
  481. SetCursor(mData->mCursor.cursor);
  482. }
  483. return true;
  484. case WM_GETMINMAXINFO:
  485. {
  486. // Prevent the window from going smaller than some minimu size
  487. ((MINMAXINFO*)lParam)->ptMinTrackSize.x = 100;
  488. ((MINMAXINFO*)lParam)->ptMinTrackSize.y = 100;
  489. // Ensure maximizes window has proper size and doesn't cover the entire screen
  490. const POINT ptZero = { 0, 0 };
  491. HMONITOR primaryMonitor = MonitorFromPoint(ptZero, MONITOR_DEFAULTTOPRIMARY);
  492. MONITORINFO monitorInfo;
  493. monitorInfo.cbSize = sizeof(MONITORINFO);
  494. GetMonitorInfo(primaryMonitor, &monitorInfo);
  495. ((MINMAXINFO*)lParam)->ptMaxPosition.x = monitorInfo.rcWork.left - monitorInfo.rcMonitor.left;
  496. ((MINMAXINFO*)lParam)->ptMaxPosition.y = monitorInfo.rcWork.top - monitorInfo.rcMonitor.top;
  497. ((MINMAXINFO*)lParam)->ptMaxSize.x = monitorInfo.rcWork.right - monitorInfo.rcWork.left;
  498. ((MINMAXINFO*)lParam)->ptMaxSize.y = monitorInfo.rcWork.bottom - monitorInfo.rcWork.top;
  499. }
  500. break;
  501. case WM_CLOSE:
  502. {
  503. gCoreApplication().quitRequested();
  504. return 0;
  505. }
  506. case WM_NCHITTEST:
  507. {
  508. auto iterFind = mData->mNonClientAreas.find(win);
  509. if (iterFind == mData->mNonClientAreas.end())
  510. break;
  511. POINT mousePos;
  512. mousePos.x = GET_X_LPARAM(lParam);
  513. mousePos.y = GET_Y_LPARAM(lParam);
  514. ScreenToClient(hWnd, &mousePos);
  515. Vector2I mousePosInt;
  516. mousePosInt.x = mousePos.x;
  517. mousePosInt.y = mousePos.y;
  518. Vector<NonClientResizeArea>& resizeAreasPerWindow = iterFind->second.resizeAreas;
  519. for(auto area : resizeAreasPerWindow)
  520. {
  521. if (area.area.contains(mousePosInt))
  522. return translateNonClientAreaType(area.type);
  523. }
  524. Vector<Rect2I>& moveAreasPerWindow = iterFind->second.moveAreas;
  525. for(auto area : moveAreasPerWindow)
  526. {
  527. if(area.contains(mousePosInt))
  528. return HTCAPTION;
  529. }
  530. return HTCLIENT;
  531. }
  532. case WM_NCLBUTTONDBLCLK:
  533. // Maximize/Restore on double-click
  534. if (wParam == HTCAPTION)
  535. {
  536. WINDOWPLACEMENT windowPlacement;
  537. windowPlacement.length = sizeof(WINDOWPLACEMENT);
  538. GetWindowPlacement(hWnd, &windowPlacement);
  539. if (windowPlacement.showCmd == SW_MAXIMIZE)
  540. ShowWindow(hWnd, SW_RESTORE);
  541. else
  542. ShowWindow(hWnd, SW_MAXIMIZE);
  543. return 0;
  544. }
  545. break;
  546. case WM_MOUSELEAVE:
  547. {
  548. // Note: Right now I track only mouse leaving client area. So it's possible for the "mouse left window" callback
  549. // to trigger, while the mouse is still in the non-client area of the window.
  550. mData->mIsTrackingMouse = false; // TrackMouseEvent ends when this message is received and needs to be re-applied
  551. Lock lock(mData->mSync);
  552. if (!onMouseLeftWindow.empty())
  553. onMouseLeftWindow(win);
  554. }
  555. return 0;
  556. case WM_LBUTTONUP:
  557. {
  558. ReleaseCapture();
  559. Vector2I intMousePos;
  560. OSPointerButtonStates btnStates;
  561. getMouseData(hWnd, wParam, lParam, false, intMousePos, btnStates);
  562. if(!onCursorButtonReleased.empty())
  563. onCursorButtonReleased(intMousePos, OSMouseButton::Left, btnStates);
  564. return 0;
  565. }
  566. case WM_MBUTTONUP:
  567. {
  568. ReleaseCapture();
  569. Vector2I intMousePos;
  570. OSPointerButtonStates btnStates;
  571. getMouseData(hWnd, wParam, lParam, false, intMousePos, btnStates);
  572. if(!onCursorButtonReleased.empty())
  573. onCursorButtonReleased(intMousePos, OSMouseButton::Middle, btnStates);
  574. return 0;
  575. }
  576. case WM_RBUTTONUP:
  577. {
  578. ReleaseCapture();
  579. Vector2I intMousePos;
  580. OSPointerButtonStates btnStates;
  581. getMouseData(hWnd, wParam, lParam, false, intMousePos, btnStates);
  582. if(!onCursorButtonReleased.empty())
  583. onCursorButtonReleased(intMousePos, OSMouseButton::Right, btnStates);
  584. return 0;
  585. }
  586. case WM_LBUTTONDOWN:
  587. {
  588. SetCapture(hWnd);
  589. Vector2I intMousePos;
  590. OSPointerButtonStates btnStates;
  591. getMouseData(hWnd, wParam, lParam, false, intMousePos, btnStates);
  592. if(!onCursorButtonPressed.empty())
  593. onCursorButtonPressed(intMousePos, OSMouseButton::Left, btnStates);
  594. }
  595. return 0;
  596. case WM_MBUTTONDOWN:
  597. {
  598. SetCapture(hWnd);
  599. Vector2I intMousePos;
  600. OSPointerButtonStates btnStates;
  601. getMouseData(hWnd, wParam, lParam, false, intMousePos, btnStates);
  602. if(!onCursorButtonPressed.empty())
  603. onCursorButtonPressed(intMousePos, OSMouseButton::Middle, btnStates);
  604. }
  605. return 0;
  606. case WM_RBUTTONDOWN:
  607. {
  608. SetCapture(hWnd);
  609. Vector2I intMousePos;
  610. OSPointerButtonStates btnStates;
  611. getMouseData(hWnd, wParam, lParam, false, intMousePos, btnStates);
  612. if(!onCursorButtonPressed.empty())
  613. onCursorButtonPressed(intMousePos, OSMouseButton::Right, btnStates);
  614. }
  615. return 0;
  616. case WM_LBUTTONDBLCLK:
  617. {
  618. Vector2I intMousePos;
  619. OSPointerButtonStates btnStates;
  620. getMouseData(hWnd, wParam, lParam, false, intMousePos, btnStates);
  621. if(!onCursorDoubleClick.empty())
  622. onCursorDoubleClick(intMousePos, btnStates);
  623. }
  624. return 0;
  625. case WM_NCMOUSEMOVE:
  626. case WM_MOUSEMOVE:
  627. {
  628. // Set up tracking so we get notified when mouse leaves the window
  629. if(!mData->mIsTrackingMouse)
  630. {
  631. TRACKMOUSEEVENT tme = { sizeof(tme) };
  632. tme.dwFlags = TME_LEAVE;
  633. tme.hwndTrack = hWnd;
  634. TrackMouseEvent(&tme);
  635. mData->mIsTrackingMouse = true;
  636. }
  637. Vector2I intMousePos;
  638. OSPointerButtonStates btnStates;
  639. getMouseData(hWnd, wParam, lParam, uMsg == WM_NCMOUSEMOVE, intMousePos, btnStates);
  640. if(!onCursorMoved.empty())
  641. onCursorMoved(intMousePos, btnStates);
  642. return 0;
  643. }
  644. case WM_MOUSEWHEEL:
  645. {
  646. INT16 wheelDelta = GET_WHEEL_DELTA_WPARAM(wParam);
  647. float wheelDeltaFlt = wheelDelta / (float)WHEEL_DELTA;
  648. if(!onMouseWheelScrolled.empty())
  649. onMouseWheelScrolled(wheelDeltaFlt);
  650. return true;
  651. }
  652. case WM_SYSKEYDOWN:
  653. case WM_KEYDOWN:
  654. {
  655. if(wParam == VK_SHIFT)
  656. {
  657. isShiftPressed = true;
  658. break;
  659. }
  660. if(wParam == VK_CONTROL)
  661. {
  662. isCtrlPressed = true;
  663. break;
  664. }
  665. InputCommandType command = InputCommandType::Backspace;
  666. if(getCommand((unsigned int)wParam, command))
  667. {
  668. if(!onInputCommand.empty())
  669. onInputCommand(command);
  670. return 0;
  671. }
  672. break;
  673. }
  674. case WM_SYSKEYUP:
  675. case WM_KEYUP:
  676. {
  677. if(wParam == VK_SHIFT)
  678. {
  679. isShiftPressed = false;
  680. }
  681. if(wParam == VK_CONTROL)
  682. {
  683. isCtrlPressed = false;
  684. }
  685. return 0;
  686. }
  687. case WM_CHAR:
  688. {
  689. // TODO - Not handling IME input
  690. // Ignore rarely used special command characters, usually triggered by ctrl+key
  691. // combinations. (We want to keep ctrl+key free for shortcuts instead)
  692. if (wParam <= 23)
  693. break;
  694. switch (wParam)
  695. {
  696. case VK_ESCAPE:
  697. break;
  698. default: // displayable character
  699. {
  700. UINT32 finalChar = (UINT32)wParam;
  701. if(!onCharInput.empty())
  702. onCharInput(finalChar);
  703. return 0;
  704. }
  705. }
  706. break;
  707. }
  708. case WM_BS_SETCAPTURE:
  709. SetCapture(hWnd);
  710. break;
  711. case WM_BS_RELEASECAPTURE:
  712. ReleaseCapture();
  713. break;
  714. case WM_CAPTURECHANGED:
  715. if(!onMouseCaptureChanged.empty())
  716. onMouseCaptureChanged();
  717. return 0;
  718. }
  719. return DefWindowProc( hWnd, uMsg, wParam, lParam );
  720. }
  721. }