BsWin32Platform.cpp 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005
  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)
  419. {
  420. const RenderWindowProperties& props = newWindow->getProperties();
  421. if (!props.isHidden())
  422. ShowWindow(hWnd, SW_SHOWNOACTIVATE);
  423. if (props.isModal())
  424. {
  425. if (!mData->mModalWindowStack.empty())
  426. {
  427. RenderWindowCore* curModalWindow = mData->mModalWindowStack.back();
  428. UINT64 curHwnd;
  429. curModalWindow->getCustomAttribute("WINDOW", &curHwnd);
  430. EnableWindow((HWND)curHwnd, FALSE);
  431. }
  432. else
  433. {
  434. Vector<RenderWindowCore*> renderWindows = RenderWindowCoreManager::instance().getRenderWindows();
  435. for (auto& renderWindow : renderWindows)
  436. {
  437. if (renderWindow == newWindow)
  438. continue;
  439. UINT64 curHwnd;
  440. renderWindow->getCustomAttribute("WINDOW", &curHwnd);
  441. EnableWindow((HWND)curHwnd, FALSE);
  442. }
  443. }
  444. mData->mModalWindowStack.push_back(newWindow);
  445. }
  446. else
  447. {
  448. // A non-modal window was opened while another modal one is open:
  449. // immediately deactivate it and make sure the modal windows stay on top
  450. if (!mData->mModalWindowStack.empty())
  451. {
  452. EnableWindow((HWND)hWnd, FALSE);
  453. for (auto window : mData->mModalWindowStack)
  454. {
  455. UINT64 curHwnd;
  456. window->getCustomAttribute("WINDOW", &curHwnd);
  457. BringWindowToTop((HWND)curHwnd);
  458. }
  459. }
  460. }
  461. }
  462. else
  463. ShowWindow(hWnd, SW_SHOWNOACTIVATE);
  464. return 0;
  465. }
  466. // look up window instance
  467. // note: it is possible to get a WM_SIZE before WM_CREATE
  468. RenderWindowCore* win = (RenderWindowCore*)GetWindowLongPtr(hWnd, GWLP_USERDATA);
  469. if (!win)
  470. return DefWindowProc(hWnd, uMsg, wParam, lParam);
  471. switch( uMsg )
  472. {
  473. case WM_DESTROY:
  474. {
  475. bool reenableWindows = false;
  476. if (!mData->mModalWindowStack.empty())
  477. {
  478. // Start from back because the most common case is closing the top-most modal window
  479. for (auto iter = mData->mModalWindowStack.rbegin(); iter != mData->mModalWindowStack.rend(); ++iter)
  480. {
  481. if (*iter == win)
  482. {
  483. auto iterFwd = std::next(iter).base(); // erase doesn't accept reverse iter, so convert
  484. mData->mModalWindowStack.erase(iterFwd);
  485. break;
  486. }
  487. }
  488. if (!mData->mModalWindowStack.empty()) // Enable next modal window
  489. {
  490. RenderWindowCore* curModalWindow = mData->mModalWindowStack.back();
  491. UINT64 curHwnd;
  492. curModalWindow->getCustomAttribute("WINDOW", &curHwnd);
  493. EnableWindow((HWND)curHwnd, TRUE);
  494. }
  495. else
  496. reenableWindows = true; // No more modal windows, re-enable any remaining window
  497. }
  498. if(reenableWindows)
  499. {
  500. Vector<RenderWindowCore*> renderWindows = RenderWindowCoreManager::instance().getRenderWindows();
  501. for(auto& renderWindow : renderWindows)
  502. {
  503. HWND curHwnd;
  504. renderWindow->getCustomAttribute("WINDOW", &curHwnd);
  505. EnableWindow(curHwnd, TRUE);
  506. }
  507. }
  508. return 0;
  509. }
  510. case WM_SETFOCUS:
  511. {
  512. if (!win->getProperties().hasFocus())
  513. win->_windowFocusReceived();
  514. return 0;
  515. }
  516. case WM_KILLFOCUS:
  517. {
  518. if (win->getProperties().hasFocus())
  519. win->_windowFocusLost();
  520. return 0;
  521. }
  522. case WM_SYSCHAR:
  523. // return zero to bypass defProc and signal we processed the message, unless it's an ALT-space
  524. if (wParam != VK_SPACE)
  525. return 0;
  526. break;
  527. case WM_MOVE:
  528. win->_windowMovedOrResized();
  529. return 0;
  530. case WM_DISPLAYCHANGE:
  531. win->_windowMovedOrResized();
  532. break;
  533. case WM_SIZE:
  534. win->_windowMovedOrResized();
  535. if (wParam == SIZE_MAXIMIZED)
  536. win->_notifyMaximized();
  537. else if (wParam == SIZE_MINIMIZED)
  538. win->_notifyMinimized();
  539. else if (wParam == SIZE_RESTORED)
  540. win->_notifyRestored();
  541. return 0;
  542. case WM_SETCURSOR:
  543. if(isCursorHidden())
  544. SetCursor(nullptr);
  545. else
  546. {
  547. switch (LOWORD(lParam))
  548. {
  549. case HTTOPLEFT:
  550. SetCursor(LoadCursor(0, IDC_SIZENWSE));
  551. return 0;
  552. case HTTOP:
  553. SetCursor(LoadCursor(0, IDC_SIZENS));
  554. return 0;
  555. case HTTOPRIGHT:
  556. SetCursor(LoadCursor(0, IDC_SIZENESW));
  557. return 0;
  558. case HTLEFT:
  559. SetCursor(LoadCursor(0, IDC_SIZEWE));
  560. return 0;
  561. case HTRIGHT:
  562. SetCursor(LoadCursor(0, IDC_SIZEWE));
  563. return 0;
  564. case HTBOTTOMLEFT:
  565. SetCursor(LoadCursor(0, IDC_SIZENESW));
  566. return 0;
  567. case HTBOTTOM:
  568. SetCursor(LoadCursor(0, IDC_SIZENS));
  569. return 0;
  570. case HTBOTTOMRIGHT:
  571. SetCursor(LoadCursor(0, IDC_SIZENWSE));
  572. return 0;
  573. }
  574. SetCursor(mData->mCursor.cursor);
  575. }
  576. return true;
  577. case WM_GETMINMAXINFO:
  578. {
  579. // Prevent the window from going smaller than some minimu size
  580. ((MINMAXINFO*)lParam)->ptMinTrackSize.x = 100;
  581. ((MINMAXINFO*)lParam)->ptMinTrackSize.y = 100;
  582. // Ensure maximizes window has proper size and doesn't cover the entire screen
  583. const POINT ptZero = { 0, 0 };
  584. HMONITOR primaryMonitor = MonitorFromPoint(ptZero, MONITOR_DEFAULTTOPRIMARY);
  585. MONITORINFO monitorInfo;
  586. monitorInfo.cbSize = sizeof(MONITORINFO);
  587. GetMonitorInfo(primaryMonitor, &monitorInfo);
  588. ((MINMAXINFO*)lParam)->ptMaxPosition.x = monitorInfo.rcWork.left - monitorInfo.rcMonitor.left;
  589. ((MINMAXINFO*)lParam)->ptMaxPosition.y = monitorInfo.rcWork.top - monitorInfo.rcMonitor.top;
  590. ((MINMAXINFO*)lParam)->ptMaxSize.x = monitorInfo.rcWork.right - monitorInfo.rcWork.left;
  591. ((MINMAXINFO*)lParam)->ptMaxSize.y = monitorInfo.rcWork.bottom - monitorInfo.rcWork.top;
  592. }
  593. break;
  594. case WM_CLOSE:
  595. {
  596. gCoreApplication().quitRequested();
  597. return 0;
  598. }
  599. case WM_NCHITTEST:
  600. {
  601. auto iterFind = mData->mNonClientAreas.find(win);
  602. if (iterFind == mData->mNonClientAreas.end())
  603. break;
  604. POINT mousePos;
  605. mousePos.x = GET_X_LPARAM(lParam);
  606. mousePos.y = GET_Y_LPARAM(lParam);
  607. ScreenToClient(hWnd, &mousePos);
  608. Vector2I mousePosInt;
  609. mousePosInt.x = mousePos.x;
  610. mousePosInt.y = mousePos.y;
  611. Vector<NonClientResizeArea>& resizeAreasPerWindow = iterFind->second.resizeAreas;
  612. for(auto area : resizeAreasPerWindow)
  613. {
  614. if(area.area.contains(mousePosInt))
  615. return translateNonClientAreaType(area.type);
  616. }
  617. Vector<Rect2I>& moveAreasPerWindow = iterFind->second.moveAreas;
  618. for(auto area : moveAreasPerWindow)
  619. {
  620. if(area.contains(mousePosInt))
  621. return HTCAPTION;
  622. }
  623. return HTCLIENT;
  624. }
  625. case WM_NCLBUTTONDBLCLK:
  626. // Maximize/Restore on double-click
  627. if (wParam == HTCAPTION)
  628. {
  629. WINDOWPLACEMENT windowPlacement;
  630. windowPlacement.length = sizeof(WINDOWPLACEMENT);
  631. GetWindowPlacement(hWnd, &windowPlacement);
  632. if (windowPlacement.showCmd == SW_MAXIMIZE)
  633. ShowWindow(hWnd, SW_RESTORE);
  634. else
  635. ShowWindow(hWnd, SW_MAXIMIZE);
  636. return 0;
  637. }
  638. break;
  639. case WM_MOUSELEAVE:
  640. {
  641. // Note: Right now I track only mouse leaving client area. So it's possible for the "mouse left window" callback
  642. // to trigger, while the mouse is still in the non-client area of the window.
  643. mData->mIsTrackingMouse = false; // TrackMouseEvent ends when this message is received and needs to be re-applied
  644. BS_LOCK_MUTEX(mData->mSync);
  645. if (!onMouseLeftWindow.empty())
  646. onMouseLeftWindow(win);
  647. }
  648. return 0;
  649. case WM_LBUTTONUP:
  650. {
  651. ReleaseCapture();
  652. Vector2I intMousePos;
  653. OSPointerButtonStates btnStates;
  654. getMouseData(hWnd, wParam, lParam, false, intMousePos, btnStates);
  655. if(!onCursorButtonReleased.empty())
  656. onCursorButtonReleased(intMousePos, OSMouseButton::Left, btnStates);
  657. return 0;
  658. }
  659. case WM_MBUTTONUP:
  660. {
  661. ReleaseCapture();
  662. Vector2I intMousePos;
  663. OSPointerButtonStates btnStates;
  664. getMouseData(hWnd, wParam, lParam, false, intMousePos, btnStates);
  665. if(!onCursorButtonReleased.empty())
  666. onCursorButtonReleased(intMousePos, OSMouseButton::Middle, btnStates);
  667. return 0;
  668. }
  669. case WM_RBUTTONUP:
  670. {
  671. ReleaseCapture();
  672. Vector2I intMousePos;
  673. OSPointerButtonStates btnStates;
  674. getMouseData(hWnd, wParam, lParam, false, intMousePos, btnStates);
  675. if(!onCursorButtonReleased.empty())
  676. onCursorButtonReleased(intMousePos, OSMouseButton::Right, btnStates);
  677. return 0;
  678. }
  679. case WM_LBUTTONDOWN:
  680. {
  681. SetCapture(hWnd);
  682. Vector2I intMousePos;
  683. OSPointerButtonStates btnStates;
  684. getMouseData(hWnd, wParam, lParam, false, intMousePos, btnStates);
  685. if(!onCursorButtonPressed.empty())
  686. onCursorButtonPressed(intMousePos, OSMouseButton::Left, btnStates);
  687. }
  688. return 0;
  689. case WM_MBUTTONDOWN:
  690. {
  691. SetCapture(hWnd);
  692. Vector2I intMousePos;
  693. OSPointerButtonStates btnStates;
  694. getMouseData(hWnd, wParam, lParam, false, intMousePos, btnStates);
  695. if(!onCursorButtonPressed.empty())
  696. onCursorButtonPressed(intMousePos, OSMouseButton::Middle, btnStates);
  697. }
  698. return 0;
  699. case WM_RBUTTONDOWN:
  700. {
  701. SetCapture(hWnd);
  702. Vector2I intMousePos;
  703. OSPointerButtonStates btnStates;
  704. getMouseData(hWnd, wParam, lParam, false, intMousePos, btnStates);
  705. if(!onCursorButtonPressed.empty())
  706. onCursorButtonPressed(intMousePos, OSMouseButton::Right, btnStates);
  707. }
  708. return 0;
  709. case WM_LBUTTONDBLCLK:
  710. {
  711. Vector2I intMousePos;
  712. OSPointerButtonStates btnStates;
  713. getMouseData(hWnd, wParam, lParam, false, intMousePos, btnStates);
  714. if(!onCursorDoubleClick.empty())
  715. onCursorDoubleClick(intMousePos, btnStates);
  716. }
  717. return 0;
  718. case WM_NCMOUSEMOVE:
  719. case WM_MOUSEMOVE:
  720. {
  721. // Set up tracking so we get notified when mouse leaves the window
  722. if(!mData->mIsTrackingMouse)
  723. {
  724. TRACKMOUSEEVENT tme = { sizeof(tme) };
  725. tme.dwFlags = TME_LEAVE;
  726. tme.hwndTrack = hWnd;
  727. TrackMouseEvent(&tme);
  728. mData->mIsTrackingMouse = true;
  729. }
  730. Vector2I intMousePos;
  731. OSPointerButtonStates btnStates;
  732. getMouseData(hWnd, wParam, lParam, uMsg == WM_NCMOUSEMOVE, intMousePos, btnStates);
  733. if(!onCursorMoved.empty())
  734. onCursorMoved(intMousePos, btnStates);
  735. return 0;
  736. }
  737. case WM_MOUSEWHEEL:
  738. {
  739. INT16 wheelDelta = GET_WHEEL_DELTA_WPARAM(wParam);
  740. float wheelDeltaFlt = wheelDelta / (float)WHEEL_DELTA;
  741. if(!onMouseWheelScrolled.empty())
  742. onMouseWheelScrolled(wheelDeltaFlt);
  743. return true;
  744. }
  745. case WM_SYSKEYDOWN:
  746. case WM_KEYDOWN:
  747. {
  748. if(wParam == VK_SHIFT)
  749. {
  750. isShiftPressed = true;
  751. break;
  752. }
  753. if(wParam == VK_CONTROL)
  754. {
  755. isCtrlPressed = true;
  756. break;
  757. }
  758. InputCommandType command = InputCommandType::Backspace;
  759. if(getCommand((unsigned int)wParam, command))
  760. {
  761. if(!onInputCommand.empty())
  762. onInputCommand(command);
  763. return 0;
  764. }
  765. break;
  766. }
  767. case WM_SYSKEYUP:
  768. case WM_KEYUP:
  769. {
  770. if(wParam == VK_SHIFT)
  771. {
  772. isShiftPressed = false;
  773. }
  774. if(wParam == VK_CONTROL)
  775. {
  776. isCtrlPressed = false;
  777. }
  778. return 0;
  779. }
  780. case WM_CHAR:
  781. {
  782. // TODO - Not handling IME input
  783. // Ignore rarely used special command characters, usually triggered by ctrl+key
  784. // combinations. (We want to keep ctrl+key free for shortcuts instead)
  785. if (wParam <= 23)
  786. break;
  787. switch (wParam)
  788. {
  789. case VK_ESCAPE:
  790. break;
  791. default: // displayable character
  792. {
  793. UINT8 scanCode = (lParam >> 16) & 0xFF;
  794. BYTE keyState[256];
  795. HKL layout = GetKeyboardLayout(0);
  796. if(GetKeyboardState(keyState) == 0)
  797. return 0;
  798. unsigned int vk = MapVirtualKeyEx(scanCode, MAPVK_VSC_TO_VK_EX, layout);
  799. if(vk == 0)
  800. return 0;
  801. InputCommandType command = InputCommandType::Backspace;
  802. if(getCommand(vk, command)) // We ignore character combinations that are special commands
  803. return 0;
  804. UINT32 finalChar = (UINT32)wParam;
  805. if(!onCharInput.empty())
  806. onCharInput(finalChar);
  807. return 0;
  808. }
  809. }
  810. break;
  811. }
  812. case WM_BS_SETCAPTURE:
  813. SetCapture(hWnd);
  814. break;
  815. case WM_BS_RELEASECAPTURE:
  816. ReleaseCapture();
  817. break;
  818. case WM_CAPTURECHANGED:
  819. if(!onMouseCaptureChanged.empty())
  820. onMouseCaptureChanged();
  821. return 0;
  822. }
  823. return DefWindowProc( hWnd, uMsg, wParam, lParam );
  824. }
  825. }