BsWin32Platform.cpp 24 KB

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