BsWin32Platform.cpp 25 KB

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