BsWin32Platform.cpp 25 KB

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