BsWin32Platform.cpp 24 KB

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