BsWin32Platform.cpp 23 KB

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