BsWin32Platform.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946
  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::openFolder(const Path& path)
  290. {
  291. ShellExecuteW(nullptr, L"open", path.toWString().c_str(), nullptr, nullptr, SW_SHOWNORMAL);
  292. }
  293. void Platform::_messagePump()
  294. {
  295. MSG msg;
  296. while (PeekMessage(&msg, nullptr, 0U, 0U, PM_REMOVE))
  297. {
  298. TranslateMessage(&msg);
  299. DispatchMessage(&msg);
  300. }
  301. }
  302. void Platform::_startUp()
  303. {
  304. Lock lock(mData->mSync);
  305. if (timeBeginPeriod(1) == TIMERR_NOCANDO)
  306. {
  307. LOGWRN("Unable to set timer resolution to 1ms. This can cause significant waste " \
  308. "in performance for waiting threads.");
  309. }
  310. mData->mRequiresStartUp = true;
  311. }
  312. void Platform::_update()
  313. {
  314. for (auto& dropTarget : mData->mDropTargets.dropTargetsPerWindow)
  315. {
  316. dropTarget.second->update();
  317. }
  318. }
  319. void Platform::_coreUpdate()
  320. {
  321. {
  322. Lock lock(mData->mSync);
  323. if (mData->mRequiresStartUp)
  324. {
  325. OleInitialize(nullptr);
  326. mData->mRequiresStartUp = false;
  327. }
  328. }
  329. {
  330. Lock lock(mData->mSync);
  331. for (auto& dropTargetToDestroy : mData->mDropTargets.dropTargetsToDestroy)
  332. {
  333. dropTargetToDestroy->unregisterWithOS();
  334. dropTargetToDestroy->Release();
  335. }
  336. mData->mDropTargets.dropTargetsToDestroy.clear();
  337. }
  338. {
  339. Lock lock(mData->mSync);
  340. for (auto& dropTargetToInit : mData->mDropTargets.dropTargetsToInitialize)
  341. {
  342. dropTargetToInit->registerWithOS();
  343. }
  344. mData->mDropTargets.dropTargetsToInitialize.clear();
  345. }
  346. _messagePump();
  347. {
  348. Lock lock(mData->mSync);
  349. if (mData->mRequiresShutDown)
  350. {
  351. OleUninitialize();
  352. mData->mRequiresShutDown = false;
  353. }
  354. }
  355. }
  356. void Platform::_shutDown()
  357. {
  358. Lock lock(mData->mSync);
  359. timeEndPeriod(1);
  360. mData->mRequiresShutDown = true;
  361. }
  362. bool isShiftPressed = false;
  363. bool isCtrlPressed = false;
  364. /** Translate engine non client area to win32 non client area. */
  365. LRESULT translateNonClientAreaType(NonClientAreaBorderType type)
  366. {
  367. LRESULT dir = HTCLIENT;
  368. switch(type)
  369. {
  370. case NonClientAreaBorderType::Left:
  371. dir = HTLEFT;
  372. break;
  373. case NonClientAreaBorderType::TopLeft:
  374. dir = HTTOPLEFT;
  375. break;
  376. case NonClientAreaBorderType::Top:
  377. dir = HTTOP;
  378. break;
  379. case NonClientAreaBorderType::TopRight:
  380. dir = HTTOPRIGHT;
  381. break;
  382. case NonClientAreaBorderType::Right:
  383. dir = HTRIGHT;
  384. break;
  385. case NonClientAreaBorderType::BottomRight:
  386. dir = HTBOTTOMRIGHT;
  387. break;
  388. case NonClientAreaBorderType::Bottom:
  389. dir = HTBOTTOM;
  390. break;
  391. case NonClientAreaBorderType::BottomLeft:
  392. dir = HTBOTTOMLEFT;
  393. break;
  394. }
  395. return dir;
  396. }
  397. /** Method triggered whenever a mouse event happens. */
  398. void getMouseData(HWND hWnd, WPARAM wParam, LPARAM lParam, bool nonClient, Vector2I& mousePos, OSPointerButtonStates& btnStates)
  399. {
  400. POINT clientPoint;
  401. clientPoint.x = GET_X_LPARAM(lParam);
  402. clientPoint.y = GET_Y_LPARAM(lParam);
  403. if (!nonClient)
  404. ClientToScreen(hWnd, &clientPoint);
  405. mousePos.x = clientPoint.x;
  406. mousePos.y = clientPoint.y;
  407. btnStates.mouseButtons[0] = (wParam & MK_LBUTTON) != 0;
  408. btnStates.mouseButtons[1] = (wParam & MK_MBUTTON) != 0;
  409. btnStates.mouseButtons[2] = (wParam & MK_RBUTTON) != 0;
  410. btnStates.shift = (wParam & MK_SHIFT) != 0;
  411. btnStates.ctrl = (wParam & MK_CONTROL) != 0;
  412. }
  413. /**
  414. * Converts a virtual key code into an input command, if possible. Returns true if conversion was done.
  415. *
  416. * @param[in] virtualKeyCode Virtual key code to try to translate to a command.
  417. * @param[out] command Input command. Only valid if function returns true.
  418. */
  419. bool getCommand(unsigned int virtualKeyCode, InputCommandType& command)
  420. {
  421. switch (virtualKeyCode)
  422. {
  423. case VK_LEFT:
  424. command = isShiftPressed ? InputCommandType::SelectLeft : InputCommandType::CursorMoveLeft;
  425. return true;
  426. case VK_RIGHT:
  427. command = isShiftPressed ? InputCommandType::SelectRight : InputCommandType::CursorMoveRight;
  428. return true;
  429. case VK_UP:
  430. command = isShiftPressed ? InputCommandType::SelectUp : InputCommandType::CursorMoveUp;
  431. return true;
  432. case VK_DOWN:
  433. command = isShiftPressed ? InputCommandType::SelectDown : InputCommandType::CursorMoveDown;
  434. return true;
  435. case VK_ESCAPE:
  436. command = InputCommandType::Escape;
  437. return true;
  438. case VK_RETURN:
  439. command = isShiftPressed ? InputCommandType::Return : InputCommandType::Confirm;
  440. return true;
  441. case VK_BACK:
  442. command = InputCommandType::Backspace;
  443. return true;
  444. case VK_DELETE:
  445. command = InputCommandType::Delete;
  446. return true;
  447. }
  448. return false;
  449. }
  450. LRESULT CALLBACK Win32Platform::_win32WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  451. {
  452. if (uMsg == WM_CREATE)
  453. { // Store pointer to Win32Window in user data area
  454. SetWindowLongPtr(hWnd, GWLP_USERDATA, (LONG_PTR)(((LPCREATESTRUCT)lParam)->lpCreateParams));
  455. ct::RenderWindow* newWindow = (ct::RenderWindow*)GetWindowLongPtr(hWnd, GWLP_USERDATA);
  456. if (newWindow != nullptr)
  457. {
  458. const RenderWindowProperties& props = newWindow->getProperties();
  459. if (!props.isHidden)
  460. ShowWindow(hWnd, SW_SHOWNOACTIVATE);
  461. }
  462. else
  463. ShowWindow(hWnd, SW_SHOWNOACTIVATE);
  464. return 0;
  465. }
  466. ct::RenderWindow* win = (ct::RenderWindow*)GetWindowLongPtr(hWnd, GWLP_USERDATA);
  467. if (!win)
  468. return DefWindowProc(hWnd, uMsg, wParam, lParam);
  469. switch( uMsg )
  470. {
  471. case WM_SETFOCUS:
  472. {
  473. if (!win->getProperties().hasFocus)
  474. win->_windowFocusReceived();
  475. return 0;
  476. }
  477. case WM_KILLFOCUS:
  478. {
  479. if (win->getProperties().hasFocus)
  480. win->_windowFocusLost();
  481. return 0;
  482. }
  483. case WM_SYSCHAR:
  484. if (wParam != VK_SPACE)
  485. return 0;
  486. break;
  487. case WM_MOVE:
  488. win->_windowMovedOrResized();
  489. return 0;
  490. case WM_DISPLAYCHANGE:
  491. win->_windowMovedOrResized();
  492. break;
  493. case WM_SIZE:
  494. win->_windowMovedOrResized();
  495. if (wParam == SIZE_MAXIMIZED)
  496. win->_notifyMaximized();
  497. else if (wParam == SIZE_MINIMIZED)
  498. win->_notifyMinimized();
  499. else if (wParam == SIZE_RESTORED)
  500. win->_notifyRestored();
  501. return 0;
  502. case WM_SETCURSOR:
  503. if(isCursorHidden())
  504. SetCursor(nullptr);
  505. else
  506. {
  507. switch (LOWORD(lParam))
  508. {
  509. case HTTOPLEFT:
  510. SetCursor(LoadCursor(0, IDC_SIZENWSE));
  511. return 0;
  512. case HTTOP:
  513. SetCursor(LoadCursor(0, IDC_SIZENS));
  514. return 0;
  515. case HTTOPRIGHT:
  516. SetCursor(LoadCursor(0, IDC_SIZENESW));
  517. return 0;
  518. case HTLEFT:
  519. SetCursor(LoadCursor(0, IDC_SIZEWE));
  520. return 0;
  521. case HTRIGHT:
  522. SetCursor(LoadCursor(0, IDC_SIZEWE));
  523. return 0;
  524. case HTBOTTOMLEFT:
  525. SetCursor(LoadCursor(0, IDC_SIZENESW));
  526. return 0;
  527. case HTBOTTOM:
  528. SetCursor(LoadCursor(0, IDC_SIZENS));
  529. return 0;
  530. case HTBOTTOMRIGHT:
  531. SetCursor(LoadCursor(0, IDC_SIZENWSE));
  532. return 0;
  533. }
  534. SetCursor(mData->mCursor.cursor);
  535. }
  536. return true;
  537. case WM_GETMINMAXINFO:
  538. {
  539. // Prevent the window from going smaller than some minimu size
  540. ((MINMAXINFO*)lParam)->ptMinTrackSize.x = 100;
  541. ((MINMAXINFO*)lParam)->ptMinTrackSize.y = 100;
  542. // Ensure maximizes window has proper size and doesn't cover the entire screen
  543. const POINT ptZero = { 0, 0 };
  544. HMONITOR primaryMonitor = MonitorFromPoint(ptZero, MONITOR_DEFAULTTOPRIMARY);
  545. MONITORINFO monitorInfo;
  546. monitorInfo.cbSize = sizeof(MONITORINFO);
  547. GetMonitorInfo(primaryMonitor, &monitorInfo);
  548. ((MINMAXINFO*)lParam)->ptMaxPosition.x = monitorInfo.rcWork.left - monitorInfo.rcMonitor.left;
  549. ((MINMAXINFO*)lParam)->ptMaxPosition.y = monitorInfo.rcWork.top - monitorInfo.rcMonitor.top;
  550. ((MINMAXINFO*)lParam)->ptMaxSize.x = monitorInfo.rcWork.right - monitorInfo.rcWork.left;
  551. ((MINMAXINFO*)lParam)->ptMaxSize.y = monitorInfo.rcWork.bottom - monitorInfo.rcWork.top;
  552. }
  553. break;
  554. case WM_CLOSE:
  555. {
  556. gCoreApplication().quitRequested();
  557. return 0;
  558. }
  559. case WM_NCHITTEST:
  560. {
  561. auto iterFind = mData->mNonClientAreas.find(win);
  562. if (iterFind == mData->mNonClientAreas.end())
  563. break;
  564. POINT mousePos;
  565. mousePos.x = GET_X_LPARAM(lParam);
  566. mousePos.y = GET_Y_LPARAM(lParam);
  567. ScreenToClient(hWnd, &mousePos);
  568. Vector2I mousePosInt;
  569. mousePosInt.x = mousePos.x;
  570. mousePosInt.y = mousePos.y;
  571. Vector<NonClientResizeArea>& resizeAreasPerWindow = iterFind->second.resizeAreas;
  572. for(auto area : resizeAreasPerWindow)
  573. {
  574. if (area.area.contains(mousePosInt))
  575. return translateNonClientAreaType(area.type);
  576. }
  577. Vector<Rect2I>& moveAreasPerWindow = iterFind->second.moveAreas;
  578. for(auto area : moveAreasPerWindow)
  579. {
  580. if(area.contains(mousePosInt))
  581. return HTCAPTION;
  582. }
  583. return HTCLIENT;
  584. }
  585. case WM_NCLBUTTONDBLCLK:
  586. // Maximize/Restore on double-click
  587. if (wParam == HTCAPTION)
  588. {
  589. WINDOWPLACEMENT windowPlacement;
  590. windowPlacement.length = sizeof(WINDOWPLACEMENT);
  591. GetWindowPlacement(hWnd, &windowPlacement);
  592. if (windowPlacement.showCmd == SW_MAXIMIZE)
  593. ShowWindow(hWnd, SW_RESTORE);
  594. else
  595. ShowWindow(hWnd, SW_MAXIMIZE);
  596. return 0;
  597. }
  598. break;
  599. case WM_MOUSELEAVE:
  600. {
  601. // Note: Right now I track only mouse leaving client area. So it's possible for the "mouse left window" callback
  602. // to trigger, while the mouse is still in the non-client area of the window.
  603. mData->mIsTrackingMouse = false; // TrackMouseEvent ends when this message is received and needs to be re-applied
  604. Lock lock(mData->mSync);
  605. win->_notifyMouseLeft();
  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. }