BsWin32Platform.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948
  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, INT32 x, INT32 y, UINT32 width, UINT32 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::copyToClipboard(const WString& string)
  239. {
  240. HANDLE hData = GlobalAlloc(GMEM_MOVEABLE | GMEM_DDESHARE, (string.size() + 1) * sizeof(WString::value_type));
  241. WString::value_type* buffer = (WString::value_type*)GlobalLock(hData);
  242. string.copy(buffer, string.size());
  243. buffer[string.size()] = '\0';
  244. GlobalUnlock(hData);
  245. if (OpenClipboard(NULL))
  246. {
  247. EmptyClipboard();
  248. SetClipboardData(CF_UNICODETEXT, hData);
  249. CloseClipboard();
  250. }
  251. else
  252. {
  253. GlobalFree(hData);
  254. }
  255. }
  256. WString Platform::copyFromClipboard()
  257. {
  258. if (OpenClipboard(NULL))
  259. {
  260. HANDLE hData = GetClipboardData(CF_UNICODETEXT);
  261. if (hData != NULL)
  262. {
  263. WString::value_type* buffer = (WString::value_type*)GlobalLock(hData);
  264. WString string(buffer);
  265. GlobalUnlock(hData);
  266. CloseClipboard();
  267. return string;
  268. }
  269. else
  270. {
  271. CloseClipboard();
  272. return L"";
  273. }
  274. }
  275. return L"";
  276. }
  277. WString Platform::keyCodeToUnicode(UINT32 keyCode)
  278. {
  279. static HKL keyboardLayout = GetKeyboardLayout(0);
  280. static UINT8 keyboarState[256];
  281. if (GetKeyboardState(keyboarState) == FALSE)
  282. return 0;
  283. UINT virtualKey = MapVirtualKeyExW(keyCode, 1, keyboardLayout);
  284. wchar_t output[2];
  285. int count = ToUnicodeEx(virtualKey, keyCode, keyboarState, output, 2, 0, keyboardLayout);
  286. if (count > 0)
  287. return WString(output, count);
  288. return StringUtil::WBLANK;
  289. }
  290. void Platform::_messagePump()
  291. {
  292. MSG msg;
  293. while (PeekMessage(&msg, nullptr, 0U, 0U, PM_REMOVE))
  294. {
  295. TranslateMessage(&msg);
  296. DispatchMessage(&msg);
  297. }
  298. }
  299. void Platform::_startUp()
  300. {
  301. Lock lock(mData->mSync);
  302. if (timeBeginPeriod(1) == TIMERR_NOCANDO)
  303. {
  304. LOGWRN("Unable to set timer resolution to 1ms. This can cause significant waste " \
  305. "in performance for waiting threads.");
  306. }
  307. mData->mRequiresStartUp = true;
  308. }
  309. void Platform::_update()
  310. {
  311. for (auto& dropTarget : mData->mDropTargets.dropTargetsPerWindow)
  312. {
  313. dropTarget.second->update();
  314. }
  315. }
  316. void Platform::_coreUpdate()
  317. {
  318. {
  319. Lock lock(mData->mSync);
  320. if (mData->mRequiresStartUp)
  321. {
  322. OleInitialize(nullptr);
  323. mData->mRequiresStartUp = false;
  324. }
  325. }
  326. {
  327. Lock lock(mData->mSync);
  328. for (auto& dropTargetToDestroy : mData->mDropTargets.dropTargetsToDestroy)
  329. {
  330. dropTargetToDestroy->unregisterWithOS();
  331. dropTargetToDestroy->Release();
  332. }
  333. mData->mDropTargets.dropTargetsToDestroy.clear();
  334. }
  335. {
  336. Lock lock(mData->mSync);
  337. for (auto& dropTargetToInit : mData->mDropTargets.dropTargetsToInitialize)
  338. {
  339. dropTargetToInit->registerWithOS();
  340. }
  341. mData->mDropTargets.dropTargetsToInitialize.clear();
  342. }
  343. _messagePump();
  344. {
  345. Lock lock(mData->mSync);
  346. if (mData->mRequiresShutDown)
  347. {
  348. OleUninitialize();
  349. mData->mRequiresShutDown = false;
  350. }
  351. }
  352. }
  353. void Platform::_shutDown()
  354. {
  355. Lock lock(mData->mSync);
  356. timeEndPeriod(1);
  357. mData->mRequiresShutDown = true;
  358. }
  359. bool isShiftPressed = false;
  360. bool isCtrlPressed = false;
  361. /** Translate engine non client area to win32 non client area. */
  362. LRESULT translateNonClientAreaType(NonClientAreaBorderType type)
  363. {
  364. LRESULT dir = HTCLIENT;
  365. switch(type)
  366. {
  367. case NonClientAreaBorderType::Left:
  368. dir = HTLEFT;
  369. break;
  370. case NonClientAreaBorderType::TopLeft:
  371. dir = HTTOPLEFT;
  372. break;
  373. case NonClientAreaBorderType::Top:
  374. dir = HTTOP;
  375. break;
  376. case NonClientAreaBorderType::TopRight:
  377. dir = HTTOPRIGHT;
  378. break;
  379. case NonClientAreaBorderType::Right:
  380. dir = HTRIGHT;
  381. break;
  382. case NonClientAreaBorderType::BottomRight:
  383. dir = HTBOTTOMRIGHT;
  384. break;
  385. case NonClientAreaBorderType::Bottom:
  386. dir = HTBOTTOM;
  387. break;
  388. case NonClientAreaBorderType::BottomLeft:
  389. dir = HTBOTTOMLEFT;
  390. break;
  391. }
  392. return dir;
  393. }
  394. /** Method triggered whenever a mouse event happens. */
  395. void getMouseData(HWND hWnd, WPARAM wParam, LPARAM lParam, bool nonClient, Vector2I& mousePos, OSPointerButtonStates& btnStates)
  396. {
  397. POINT clientPoint;
  398. clientPoint.x = GET_X_LPARAM(lParam);
  399. clientPoint.y = GET_Y_LPARAM(lParam);
  400. if (!nonClient)
  401. ClientToScreen(hWnd, &clientPoint);
  402. mousePos.x = clientPoint.x;
  403. mousePos.y = clientPoint.y;
  404. btnStates.mouseButtons[0] = (wParam & MK_LBUTTON) != 0;
  405. btnStates.mouseButtons[1] = (wParam & MK_MBUTTON) != 0;
  406. btnStates.mouseButtons[2] = (wParam & MK_RBUTTON) != 0;
  407. btnStates.shift = (wParam & MK_SHIFT) != 0;
  408. btnStates.ctrl = (wParam & MK_CONTROL) != 0;
  409. }
  410. /**
  411. * Converts a virtual key code into an input command, if possible. Returns true if conversion was done.
  412. *
  413. * @param[in] virtualKeyCode Virtual key code to try to translate to a command.
  414. * @param[out] command Input command. Only valid if function returns true.
  415. * @param[in] ignoreMovement If true, then movement keys (up/down/left/right) will be ignored and not considered
  416. * as input commands (useful if you need to parse num keys as numbers and not movement).
  417. */
  418. bool getCommand(unsigned int virtualKeyCode, InputCommandType& command)
  419. {
  420. switch (virtualKeyCode)
  421. {
  422. case VK_LEFT:
  423. command = isShiftPressed ? InputCommandType::SelectLeft : InputCommandType::CursorMoveLeft;
  424. return true;
  425. case VK_RIGHT:
  426. command = isShiftPressed ? InputCommandType::SelectRight : InputCommandType::CursorMoveRight;
  427. return true;
  428. case VK_UP:
  429. command = isShiftPressed ? InputCommandType::SelectUp : InputCommandType::CursorMoveUp;
  430. return true;
  431. case VK_DOWN:
  432. command = isShiftPressed ? InputCommandType::SelectDown : InputCommandType::CursorMoveDown;
  433. return true;
  434. case VK_ESCAPE:
  435. command = InputCommandType::Escape;
  436. return true;
  437. case VK_RETURN:
  438. command = isShiftPressed ? InputCommandType::Return : InputCommandType::Confirm;
  439. return true;
  440. case VK_BACK:
  441. command = InputCommandType::Backspace;
  442. return true;
  443. case VK_DELETE:
  444. command = InputCommandType::Delete;
  445. return true;
  446. }
  447. return false;
  448. }
  449. LRESULT CALLBACK Win32Platform::_win32WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  450. {
  451. if (uMsg == WM_CREATE)
  452. { // Store pointer to Win32Window in user data area
  453. SetWindowLongPtr(hWnd, GWLP_USERDATA, (LONG_PTR)(((LPCREATESTRUCT)lParam)->lpCreateParams));
  454. ct::RenderWindow* newWindow = (ct::RenderWindow*)GetWindowLongPtr(hWnd, GWLP_USERDATA);
  455. if (newWindow != nullptr)
  456. {
  457. const RenderWindowProperties& props = newWindow->getProperties();
  458. if (!props.isHidden)
  459. ShowWindow(hWnd, SW_SHOWNOACTIVATE);
  460. }
  461. else
  462. ShowWindow(hWnd, SW_SHOWNOACTIVATE);
  463. return 0;
  464. }
  465. ct::RenderWindow* win = (ct::RenderWindow*)GetWindowLongPtr(hWnd, GWLP_USERDATA);
  466. if (!win)
  467. return DefWindowProc(hWnd, uMsg, wParam, lParam);
  468. switch( uMsg )
  469. {
  470. case WM_SETFOCUS:
  471. {
  472. if (!win->getProperties().hasFocus)
  473. win->_windowFocusReceived();
  474. return 0;
  475. }
  476. case WM_KILLFOCUS:
  477. {
  478. if (win->getProperties().hasFocus)
  479. win->_windowFocusLost();
  480. return 0;
  481. }
  482. case WM_SYSCHAR:
  483. if (wParam != VK_SPACE)
  484. return 0;
  485. break;
  486. case WM_MOVE:
  487. win->_windowMovedOrResized();
  488. return 0;
  489. case WM_DISPLAYCHANGE:
  490. win->_windowMovedOrResized();
  491. break;
  492. case WM_SIZE:
  493. win->_windowMovedOrResized();
  494. if (wParam == SIZE_MAXIMIZED)
  495. win->_notifyMaximized();
  496. else if (wParam == SIZE_MINIMIZED)
  497. win->_notifyMinimized();
  498. else if (wParam == SIZE_RESTORED)
  499. win->_notifyRestored();
  500. return 0;
  501. case WM_SETCURSOR:
  502. if(isCursorHidden())
  503. SetCursor(nullptr);
  504. else
  505. {
  506. switch (LOWORD(lParam))
  507. {
  508. case HTTOPLEFT:
  509. SetCursor(LoadCursor(0, IDC_SIZENWSE));
  510. return 0;
  511. case HTTOP:
  512. SetCursor(LoadCursor(0, IDC_SIZENS));
  513. return 0;
  514. case HTTOPRIGHT:
  515. SetCursor(LoadCursor(0, IDC_SIZENESW));
  516. return 0;
  517. case HTLEFT:
  518. SetCursor(LoadCursor(0, IDC_SIZEWE));
  519. return 0;
  520. case HTRIGHT:
  521. SetCursor(LoadCursor(0, IDC_SIZEWE));
  522. return 0;
  523. case HTBOTTOMLEFT:
  524. SetCursor(LoadCursor(0, IDC_SIZENESW));
  525. return 0;
  526. case HTBOTTOM:
  527. SetCursor(LoadCursor(0, IDC_SIZENS));
  528. return 0;
  529. case HTBOTTOMRIGHT:
  530. SetCursor(LoadCursor(0, IDC_SIZENWSE));
  531. return 0;
  532. }
  533. SetCursor(mData->mCursor.cursor);
  534. }
  535. return true;
  536. case WM_GETMINMAXINFO:
  537. {
  538. // Prevent the window from going smaller than some minimu size
  539. ((MINMAXINFO*)lParam)->ptMinTrackSize.x = 100;
  540. ((MINMAXINFO*)lParam)->ptMinTrackSize.y = 100;
  541. // Ensure maximizes window has proper size and doesn't cover the entire screen
  542. const POINT ptZero = { 0, 0 };
  543. HMONITOR primaryMonitor = MonitorFromPoint(ptZero, MONITOR_DEFAULTTOPRIMARY);
  544. MONITORINFO monitorInfo;
  545. monitorInfo.cbSize = sizeof(MONITORINFO);
  546. GetMonitorInfo(primaryMonitor, &monitorInfo);
  547. ((MINMAXINFO*)lParam)->ptMaxPosition.x = monitorInfo.rcWork.left - monitorInfo.rcMonitor.left;
  548. ((MINMAXINFO*)lParam)->ptMaxPosition.y = monitorInfo.rcWork.top - monitorInfo.rcMonitor.top;
  549. ((MINMAXINFO*)lParam)->ptMaxSize.x = monitorInfo.rcWork.right - monitorInfo.rcWork.left;
  550. ((MINMAXINFO*)lParam)->ptMaxSize.y = monitorInfo.rcWork.bottom - monitorInfo.rcWork.top;
  551. }
  552. break;
  553. case WM_CLOSE:
  554. {
  555. gCoreApplication().quitRequested();
  556. return 0;
  557. }
  558. case WM_NCHITTEST:
  559. {
  560. auto iterFind = mData->mNonClientAreas.find(win);
  561. if (iterFind == mData->mNonClientAreas.end())
  562. break;
  563. POINT mousePos;
  564. mousePos.x = GET_X_LPARAM(lParam);
  565. mousePos.y = GET_Y_LPARAM(lParam);
  566. ScreenToClient(hWnd, &mousePos);
  567. Vector2I mousePosInt;
  568. mousePosInt.x = mousePos.x;
  569. mousePosInt.y = mousePos.y;
  570. Vector<NonClientResizeArea>& resizeAreasPerWindow = iterFind->second.resizeAreas;
  571. for(auto area : resizeAreasPerWindow)
  572. {
  573. if (area.area.contains(mousePosInt))
  574. return translateNonClientAreaType(area.type);
  575. }
  576. Vector<Rect2I>& moveAreasPerWindow = iterFind->second.moveAreas;
  577. for(auto area : moveAreasPerWindow)
  578. {
  579. if(area.contains(mousePosInt))
  580. return HTCAPTION;
  581. }
  582. return HTCLIENT;
  583. }
  584. case WM_NCLBUTTONDBLCLK:
  585. // Maximize/Restore on double-click
  586. if (wParam == HTCAPTION)
  587. {
  588. WINDOWPLACEMENT windowPlacement;
  589. windowPlacement.length = sizeof(WINDOWPLACEMENT);
  590. GetWindowPlacement(hWnd, &windowPlacement);
  591. if (windowPlacement.showCmd == SW_MAXIMIZE)
  592. ShowWindow(hWnd, SW_RESTORE);
  593. else
  594. ShowWindow(hWnd, SW_MAXIMIZE);
  595. return 0;
  596. }
  597. break;
  598. case WM_MOUSELEAVE:
  599. {
  600. // Note: Right now I track only mouse leaving client area. So it's possible for the "mouse left window" callback
  601. // to trigger, while the mouse is still in the non-client area of the window.
  602. mData->mIsTrackingMouse = false; // TrackMouseEvent ends when this message is received and needs to be re-applied
  603. Lock lock(mData->mSync);
  604. if (!onMouseLeftWindow.empty())
  605. onMouseLeftWindow(win);
  606. }
  607. return 0;
  608. case WM_LBUTTONUP:
  609. {
  610. ReleaseCapture();
  611. Vector2I intMousePos;
  612. OSPointerButtonStates btnStates;
  613. getMouseData(hWnd, wParam, lParam, false, intMousePos, btnStates);
  614. if(!onCursorButtonReleased.empty())
  615. onCursorButtonReleased(intMousePos, OSMouseButton::Left, btnStates);
  616. return 0;
  617. }
  618. case WM_MBUTTONUP:
  619. {
  620. ReleaseCapture();
  621. Vector2I intMousePos;
  622. OSPointerButtonStates btnStates;
  623. getMouseData(hWnd, wParam, lParam, false, intMousePos, btnStates);
  624. if(!onCursorButtonReleased.empty())
  625. onCursorButtonReleased(intMousePos, OSMouseButton::Middle, btnStates);
  626. return 0;
  627. }
  628. case WM_RBUTTONUP:
  629. {
  630. ReleaseCapture();
  631. Vector2I intMousePos;
  632. OSPointerButtonStates btnStates;
  633. getMouseData(hWnd, wParam, lParam, false, intMousePos, btnStates);
  634. if(!onCursorButtonReleased.empty())
  635. onCursorButtonReleased(intMousePos, OSMouseButton::Right, btnStates);
  636. return 0;
  637. }
  638. case WM_LBUTTONDOWN:
  639. {
  640. SetCapture(hWnd);
  641. Vector2I intMousePos;
  642. OSPointerButtonStates btnStates;
  643. getMouseData(hWnd, wParam, lParam, false, intMousePos, btnStates);
  644. if(!onCursorButtonPressed.empty())
  645. onCursorButtonPressed(intMousePos, OSMouseButton::Left, btnStates);
  646. }
  647. return 0;
  648. case WM_MBUTTONDOWN:
  649. {
  650. SetCapture(hWnd);
  651. Vector2I intMousePos;
  652. OSPointerButtonStates btnStates;
  653. getMouseData(hWnd, wParam, lParam, false, intMousePos, btnStates);
  654. if(!onCursorButtonPressed.empty())
  655. onCursorButtonPressed(intMousePos, OSMouseButton::Middle, btnStates);
  656. }
  657. return 0;
  658. case WM_RBUTTONDOWN:
  659. {
  660. SetCapture(hWnd);
  661. Vector2I intMousePos;
  662. OSPointerButtonStates btnStates;
  663. getMouseData(hWnd, wParam, lParam, false, intMousePos, btnStates);
  664. if(!onCursorButtonPressed.empty())
  665. onCursorButtonPressed(intMousePos, OSMouseButton::Right, btnStates);
  666. }
  667. return 0;
  668. case WM_LBUTTONDBLCLK:
  669. {
  670. Vector2I intMousePos;
  671. OSPointerButtonStates btnStates;
  672. getMouseData(hWnd, wParam, lParam, false, intMousePos, btnStates);
  673. if(!onCursorDoubleClick.empty())
  674. onCursorDoubleClick(intMousePos, btnStates);
  675. }
  676. return 0;
  677. case WM_NCMOUSEMOVE:
  678. case WM_MOUSEMOVE:
  679. {
  680. // Set up tracking so we get notified when mouse leaves the window
  681. if(!mData->mIsTrackingMouse)
  682. {
  683. TRACKMOUSEEVENT tme = { sizeof(tme) };
  684. tme.dwFlags = TME_LEAVE;
  685. tme.hwndTrack = hWnd;
  686. TrackMouseEvent(&tme);
  687. mData->mIsTrackingMouse = true;
  688. }
  689. Vector2I intMousePos;
  690. OSPointerButtonStates btnStates;
  691. getMouseData(hWnd, wParam, lParam, uMsg == WM_NCMOUSEMOVE, intMousePos, btnStates);
  692. if(!onCursorMoved.empty())
  693. onCursorMoved(intMousePos, btnStates);
  694. return 0;
  695. }
  696. case WM_MOUSEWHEEL:
  697. {
  698. INT16 wheelDelta = GET_WHEEL_DELTA_WPARAM(wParam);
  699. float wheelDeltaFlt = wheelDelta / (float)WHEEL_DELTA;
  700. if(!onMouseWheelScrolled.empty())
  701. onMouseWheelScrolled(wheelDeltaFlt);
  702. return true;
  703. }
  704. case WM_SYSKEYDOWN:
  705. case WM_KEYDOWN:
  706. {
  707. if(wParam == VK_SHIFT)
  708. {
  709. isShiftPressed = true;
  710. break;
  711. }
  712. if(wParam == VK_CONTROL)
  713. {
  714. isCtrlPressed = true;
  715. break;
  716. }
  717. InputCommandType command = InputCommandType::Backspace;
  718. if(getCommand((unsigned int)wParam, command))
  719. {
  720. if(!onInputCommand.empty())
  721. onInputCommand(command);
  722. return 0;
  723. }
  724. break;
  725. }
  726. case WM_SYSKEYUP:
  727. case WM_KEYUP:
  728. {
  729. if(wParam == VK_SHIFT)
  730. {
  731. isShiftPressed = false;
  732. }
  733. if(wParam == VK_CONTROL)
  734. {
  735. isCtrlPressed = false;
  736. }
  737. return 0;
  738. }
  739. case WM_CHAR:
  740. {
  741. // TODO - Not handling IME input
  742. // Ignore rarely used special command characters, usually triggered by ctrl+key
  743. // combinations. (We want to keep ctrl+key free for shortcuts instead)
  744. if (wParam <= 23)
  745. break;
  746. switch (wParam)
  747. {
  748. case VK_ESCAPE:
  749. break;
  750. default: // displayable character
  751. {
  752. UINT32 finalChar = (UINT32)wParam;
  753. if(!onCharInput.empty())
  754. onCharInput(finalChar);
  755. return 0;
  756. }
  757. }
  758. break;
  759. }
  760. case WM_BS_SETCAPTURE:
  761. SetCapture(hWnd);
  762. break;
  763. case WM_BS_RELEASECAPTURE:
  764. ReleaseCapture();
  765. break;
  766. case WM_CAPTURECHANGED:
  767. if(!onMouseCaptureChanged.empty())
  768. onMouseCaptureChanged();
  769. return 0;
  770. }
  771. return DefWindowProc( hWnd, uMsg, wParam, lParam );
  772. }
  773. }