CmWin32Window.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716
  1. #ifndef _WIN32_WINNT
  2. #define _WIN32_WINNT 0x0500
  3. #endif
  4. #include "CmWin32Window.h"
  5. #include "CmInput.h"
  6. #include "CmRenderSystem.h"
  7. #include "CmCoreThread.h"
  8. #include "CmException.h"
  9. #include "CmWin32GLSupport.h"
  10. #include "CmWin32Context.h"
  11. #include "Win32/CmPlatformWndProc.h"
  12. #include "CmWin32VideoModeInfo.h"
  13. #include "CmGLPixelFormat.h"
  14. GLenum GLEWAPIENTRY wglewContextInit(BansheeEngine::GLSupport *glSupport);
  15. namespace BansheeEngine
  16. {
  17. #define _MAX_CLASS_NAME_ 128
  18. Win32Window::Win32Window(const RENDER_WINDOW_DESC& desc, Win32GLSupport &glsupport):
  19. RenderWindow(desc),
  20. mGLSupport(glsupport),
  21. mContext(0)
  22. {
  23. mIsFullScreen = false;
  24. mHWnd = 0;
  25. mIsExternal = false;
  26. mIsExternalGLControl = false;
  27. mSizing = false;
  28. mClosed = false;
  29. mDisplayFrequency = 0;
  30. mActive = false;
  31. mDeviceName = NULL;
  32. }
  33. Win32Window::~Win32Window()
  34. {
  35. }
  36. void Win32Window::initialize_internal()
  37. {
  38. #ifdef CM_STATIC_LIB
  39. HINSTANCE hInst = GetModuleHandle(NULL);
  40. #else
  41. HINSTANCE hInst = GetModuleHandle(MODULE_NAME.c_str());
  42. #endif
  43. mHWnd = 0;
  44. mName = mDesc.title;
  45. mIsFullScreen = mDesc.fullscreen;
  46. mClosed = false;
  47. mIsChild = false;
  48. mDisplayFrequency = mDesc.displayFrequency;
  49. mColorDepth = mDesc.colorDepth;
  50. HWND parent = 0;
  51. HMONITOR hMonitor = NULL;
  52. int monitorIndex = mDesc.monitorIndex;
  53. // Get variable-length params
  54. NameValuePairList::const_iterator opt;
  55. NameValuePairList::const_iterator end = mDesc.platformSpecific.end();
  56. if ((opt = mDesc.platformSpecific.find("externalWindowHandle")) != end)
  57. {
  58. mHWnd = (HWND)parseUnsignedInt(opt->second);
  59. if (mHWnd)
  60. {
  61. mIsExternal = true;
  62. mIsChild = true;
  63. mIsFullScreen = false;
  64. }
  65. if ((opt = mDesc.platformSpecific.find("externalGLControl")) != end) {
  66. mIsExternalGLControl = parseBool(opt->second);
  67. }
  68. }
  69. HGLRC glrc = 0;
  70. if ((opt = mDesc.platformSpecific.find("externalGLContext")) != end)
  71. {
  72. glrc = (HGLRC)parseUnsignedLong(opt->second);
  73. }
  74. // incompatible with fullscreen
  75. if ((opt = mDesc.platformSpecific.find("parentWindowHandle")) != end)
  76. parent = (HWND)parseUnsignedInt(opt->second);
  77. // monitor handle
  78. if ((opt = mDesc.platformSpecific.find("monitorHandle")) != end)
  79. hMonitor = (HMONITOR)parseInt(opt->second);
  80. if (!mIsFullScreen)
  81. {
  82. // make sure we don't exceed desktop colour depth
  83. if ((int)mColorDepth > GetDeviceCaps(GetDC(0), BITSPIXEL))
  84. mColorDepth = GetDeviceCaps(GetDC(0), BITSPIXEL);
  85. }
  86. if (!mIsExternal)
  87. {
  88. DWORD dwStyle = WS_VISIBLE | WS_CLIPCHILDREN;
  89. DWORD dwStyleEx = 0;
  90. MONITORINFOEX monitorInfoEx;
  91. RECT rc;
  92. // If we didn't specified the adapter index, or if it didn't find it
  93. if (hMonitor == NULL)
  94. {
  95. POINT windowAnchorPoint;
  96. // Fill in anchor point.
  97. windowAnchorPoint.x = mDesc.left;
  98. windowAnchorPoint.y = mDesc.top;
  99. // Get the nearest monitor to this window.
  100. hMonitor = MonitorFromPoint(windowAnchorPoint, MONITOR_DEFAULTTONEAREST);
  101. }
  102. // Get the target monitor info
  103. memset(&monitorInfoEx, 0, sizeof(MONITORINFOEX));
  104. monitorInfoEx.cbSize = sizeof(MONITORINFOEX);
  105. GetMonitorInfo(hMonitor, &monitorInfoEx);
  106. size_t devNameLen = strlen(monitorInfoEx.szDevice);
  107. mDeviceName = (char*)cm_alloc<ScratchAlloc>((UINT32)(devNameLen + 1));
  108. strcpy_s(mDeviceName, devNameLen + 1, monitorInfoEx.szDevice);
  109. UINT32 left = mDesc.left;
  110. UINT32 top = mDesc.top;
  111. // No specified top left -> Center the window in the middle of the monitor
  112. if (left == -1 || top == -1)
  113. {
  114. int screenw = monitorInfoEx.rcWork.right - monitorInfoEx.rcWork.left;
  115. int screenh = monitorInfoEx.rcWork.bottom - monitorInfoEx.rcWork.top;
  116. unsigned int winWidth, winHeight;
  117. _adjustWindow(mDesc.width, mDesc.height, &winWidth, &winHeight);
  118. // clamp window dimensions to screen size
  119. int outerw = (int(winWidth) < screenw)? int(winWidth) : screenw;
  120. int outerh = (int(winHeight) < screenh)? int(winHeight) : screenh;
  121. if (left == -1)
  122. left = monitorInfoEx.rcWork.left + (screenw - outerw) / 2;
  123. else if (mDesc.monitorIndex != -1)
  124. left += monitorInfoEx.rcWork.left;
  125. if (top == -1)
  126. top = monitorInfoEx.rcWork.top + (screenh - outerh) / 2;
  127. else if (mDesc.monitorIndex != -1)
  128. top += monitorInfoEx.rcWork.top;
  129. }
  130. else if (mDesc.monitorIndex != -1)
  131. {
  132. left += monitorInfoEx.rcWork.left;
  133. top += monitorInfoEx.rcWork.top;
  134. }
  135. mWidth = mDesc.width;
  136. mHeight = mDesc.height;
  137. mTop = top;
  138. mLeft = left;
  139. if (mIsFullScreen)
  140. {
  141. dwStyle |= WS_POPUP;
  142. dwStyleEx |= WS_EX_TOPMOST;
  143. mTop = monitorInfoEx.rcMonitor.top;
  144. mLeft = monitorInfoEx.rcMonitor.left;
  145. }
  146. else
  147. {
  148. if (parent)
  149. {
  150. if(mDesc.toolWindow)
  151. dwStyleEx = WS_EX_TOOLWINDOW;
  152. else
  153. dwStyle |= WS_CHILD;
  154. }
  155. if (!parent || mDesc.toolWindow)
  156. {
  157. if (mDesc.border == WindowBorder::None)
  158. dwStyle |= WS_POPUP;
  159. else if (mDesc.border == WindowBorder::Fixed)
  160. dwStyle |= WS_OVERLAPPED | WS_BORDER | WS_CAPTION |
  161. WS_SYSMENU | WS_MINIMIZEBOX;
  162. else
  163. dwStyle |= WS_OVERLAPPEDWINDOW;
  164. }
  165. int screenw = GetSystemMetrics(SM_CXSCREEN);
  166. int screenh = GetSystemMetrics(SM_CYSCREEN);
  167. if (!mDesc.outerDimensions)
  168. {
  169. // Calculate window dimensions required
  170. // to get the requested client area
  171. SetRect(&rc, 0, 0, mWidth, mHeight);
  172. AdjustWindowRect(&rc, dwStyle, false);
  173. mWidth = rc.right - rc.left;
  174. mHeight = rc.bottom - rc.top;
  175. // Clamp window rect to the nearest display monitor.
  176. if (mLeft < monitorInfoEx.rcWork.left)
  177. mLeft = monitorInfoEx.rcWork.left;
  178. if (mTop < monitorInfoEx.rcWork.top)
  179. mTop = monitorInfoEx.rcWork.top;
  180. if ((int)mWidth > monitorInfoEx.rcWork.right - mLeft)
  181. mWidth = monitorInfoEx.rcWork.right - mLeft;
  182. if ((int)mHeight > monitorInfoEx.rcWork.bottom - mTop)
  183. mHeight = monitorInfoEx.rcWork.bottom - mTop;
  184. }
  185. }
  186. // register class and create window
  187. WNDCLASS wc = { CS_OWNDC, PlatformWndProc::_win32WndProc, 0, 0, hInst,
  188. LoadIcon(NULL, IDI_APPLICATION), LoadCursor(NULL, IDC_ARROW),
  189. (HBRUSH)GetStockObject(BLACK_BRUSH), NULL, "GLWindow" };
  190. RegisterClass(&wc);
  191. if (mIsFullScreen)
  192. {
  193. DEVMODE displayDeviceMode;
  194. memset(&displayDeviceMode, 0, sizeof(displayDeviceMode));
  195. displayDeviceMode.dmSize = sizeof(DEVMODE);
  196. displayDeviceMode.dmBitsPerPel = mColorDepth;
  197. displayDeviceMode.dmPelsWidth = mWidth;
  198. displayDeviceMode.dmPelsHeight = mHeight;
  199. displayDeviceMode.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
  200. if (mDisplayFrequency)
  201. {
  202. displayDeviceMode.dmDisplayFrequency = mDisplayFrequency;
  203. displayDeviceMode.dmFields |= DM_DISPLAYFREQUENCY;
  204. if (ChangeDisplaySettingsEx(mDeviceName, &displayDeviceMode, NULL, CDS_FULLSCREEN | CDS_TEST, NULL) != DISP_CHANGE_SUCCESSFUL)
  205. {
  206. // TODO LOG PORT - Log this somewhere
  207. //LogManager::getSingleton().logMessage(LML_NORMAL, "ChangeDisplaySettings with user display frequency failed");
  208. //displayDeviceMode.dmFields ^= DM_DISPLAYFREQUENCY;
  209. }
  210. }
  211. if (ChangeDisplaySettingsEx(mDeviceName, &displayDeviceMode, NULL, CDS_FULLSCREEN, NULL) != DISP_CHANGE_SUCCESSFUL)
  212. {
  213. // TODO LOG PORT - Log this somewhere
  214. //LogManager::getSingleton().logMessage(LML_CRITICAL, "ChangeDisplaySettings failed");
  215. }
  216. }
  217. // Pass pointer to self as WM_CREATE parameter
  218. mHWnd = CreateWindowEx(dwStyleEx, "GLWindow", mDesc.title.c_str(),
  219. dwStyle, mLeft, mTop, mWidth, mHeight, parent, 0, hInst, this);
  220. }
  221. RECT rc;
  222. // top and left represent outer window position
  223. GetWindowRect(mHWnd, &rc);
  224. mTop = rc.top;
  225. mLeft = rc.left;
  226. // width and height represent drawable area only
  227. GetClientRect(mHWnd, &rc);
  228. mWidth = rc.right;
  229. mHeight = rc.bottom;
  230. mHDC = GetDC(mHWnd);
  231. if (!mIsExternalGLControl)
  232. {
  233. int testMultisample = mMultisampleCount;
  234. bool testHwGamma = mDesc.gamma;
  235. bool formatOk = mGLSupport.selectPixelFormat(mHDC, mColorDepth, testMultisample, testHwGamma);
  236. if (!formatOk)
  237. {
  238. if (mMultisampleCount > 0)
  239. {
  240. // try without multisampling
  241. testMultisample = 0;
  242. formatOk = mGLSupport.selectPixelFormat(mHDC, mColorDepth, testMultisample, testHwGamma);
  243. }
  244. if (!formatOk && mDesc.gamma)
  245. {
  246. // try without sRGB
  247. testHwGamma = false;
  248. testMultisample = mMultisampleCount;
  249. formatOk = mGLSupport.selectPixelFormat(mHDC, mColorDepth, testMultisample, testHwGamma);
  250. }
  251. if (!formatOk && mDesc.gamma && (mMultisampleCount > 0))
  252. {
  253. // try without both
  254. testHwGamma = false;
  255. testMultisample = 0;
  256. formatOk = mGLSupport.selectPixelFormat(mHDC, mColorDepth, testMultisample, testHwGamma);
  257. }
  258. if (!formatOk)
  259. CM_EXCEPT(RenderingAPIException, "selectPixelFormat failed");
  260. }
  261. // record what gamma option we used in the end
  262. // this will control enabling of sRGB state flags when used
  263. mHwGamma = testHwGamma;
  264. mMultisampleCount = testMultisample;
  265. }
  266. mActive = true;
  267. mContext = mGLSupport.createContext(mHDC, glrc);
  268. RenderWindow::initialize_internal();
  269. }
  270. void Win32Window::destroy_internal()
  271. {
  272. if (!mHWnd)
  273. return;
  274. // Unregister and destroy GLContext
  275. cm_delete(mContext);
  276. if (!mIsExternal)
  277. {
  278. if (mIsFullScreen)
  279. ChangeDisplaySettingsEx(mDeviceName, NULL, NULL, 0, NULL);
  280. DestroyWindow(mHWnd);
  281. }
  282. else
  283. {
  284. // just release the DC
  285. ReleaseDC(mHWnd, mHDC);
  286. }
  287. mActive = false;
  288. mClosed = true;
  289. mHDC = 0; // no release thanks to CS_OWNDC wndclass style
  290. mHWnd = 0;
  291. if (mDeviceName != NULL)
  292. {
  293. cm_free<ScratchAlloc>(mDeviceName);
  294. mDeviceName = NULL;
  295. }
  296. RenderWindow::destroy_internal();
  297. }
  298. void Win32Window::setFullscreen(UINT32 width, UINT32 height, float refreshRate, UINT32 monitorIdx)
  299. {
  300. THROW_IF_NOT_CORE_THREAD;
  301. if (mIsChild)
  302. return;
  303. const Win32VideoModeInfo& videoModeInfo = static_cast<const Win32VideoModeInfo&>(RenderSystem::instance().getVideoModeInfo());
  304. UINT32 numOutputs = videoModeInfo.getNumOutputs();
  305. if (numOutputs == 0)
  306. return;
  307. UINT32 actualMonitorIdx = std::min(monitorIdx, numOutputs - 1);
  308. const Win32VideoOutputInfo& outputInfo = static_cast<const Win32VideoOutputInfo&>(videoModeInfo.getOutputInfo(actualMonitorIdx));
  309. bool oldFullscreen = mIsFullScreen;
  310. DWORD style = WS_VISIBLE | WS_CLIPCHILDREN | WS_POPUP;
  311. mDisplayFrequency = Math::roundToInt(refreshRate);
  312. mIsFullScreen = true;
  313. DEVMODE displayDeviceMode;
  314. memset(&displayDeviceMode, 0, sizeof(displayDeviceMode));
  315. displayDeviceMode.dmSize = sizeof(DEVMODE);
  316. displayDeviceMode.dmBitsPerPel = mColorDepth;
  317. displayDeviceMode.dmPelsWidth = width;
  318. displayDeviceMode.dmPelsHeight = height;
  319. displayDeviceMode.dmDisplayFrequency = mDisplayFrequency;
  320. displayDeviceMode.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT | DM_DISPLAYFREQUENCY;
  321. HMONITOR hMonitor = outputInfo.getMonitorHandle();
  322. MONITORINFOEX monitorInfo;
  323. memset(&monitorInfo, 0, sizeof(MONITORINFOEX));
  324. monitorInfo.cbSize = sizeof(MONITORINFOEX);
  325. GetMonitorInfo(hMonitor, &monitorInfo);
  326. // Move window to 0,0 before display switch
  327. SetWindowPos(mHWnd, HWND_TOPMOST, 0, 0, mWidth, mHeight, SWP_NOACTIVATE);
  328. if (ChangeDisplaySettingsEx(monitorInfo.szDevice, &displayDeviceMode, NULL, CDS_FULLSCREEN, NULL) != DISP_CHANGE_SUCCESSFUL)
  329. {
  330. CM_EXCEPT(RenderingAPIException, "ChangeDisplaySettings failed");
  331. }
  332. mTop = monitorInfo.rcMonitor.top;
  333. mLeft = monitorInfo.rcMonitor.left;
  334. mWidth = width;
  335. mHeight = height;
  336. SetWindowLong(mHWnd, GWL_STYLE, style);
  337. SetWindowPos(mHWnd, HWND_TOPMOST, mLeft, mTop, width, height, SWP_NOACTIVATE);
  338. }
  339. void Win32Window::setFullscreen(const VideoMode& mode)
  340. {
  341. THROW_IF_NOT_CORE_THREAD;
  342. setFullscreen(mode.getWidth(), mode.getHeight(), mode.getRefreshRate(), mode.getOutputIdx());
  343. }
  344. void Win32Window::setWindowed()
  345. {
  346. THROW_IF_NOT_CORE_THREAD;
  347. if (!mIsFullScreen)
  348. return;
  349. mIsFullScreen = false;
  350. DWORD style = WS_VISIBLE | WS_CLIPCHILDREN | WS_OVERLAPPEDWINDOW;
  351. // Drop out of fullscreen
  352. ChangeDisplaySettingsEx(mDeviceName, NULL, NULL, 0, NULL);
  353. // Calculate overall dimensions for requested client area
  354. unsigned int winWidth, winHeight;
  355. _adjustWindow(mWidth, mHeight, &winWidth, &winHeight);
  356. // Deal with centering when switching down to smaller resolution
  357. HMONITOR hMonitor = MonitorFromWindow(mHWnd, MONITOR_DEFAULTTONEAREST);
  358. MONITORINFO monitorInfo;
  359. memset(&monitorInfo, 0, sizeof(MONITORINFO));
  360. monitorInfo.cbSize = sizeof(MONITORINFO);
  361. GetMonitorInfo(hMonitor, &monitorInfo);
  362. LONG screenw = monitorInfo.rcWork.right - monitorInfo.rcWork.left;
  363. LONG screenh = monitorInfo.rcWork.bottom - monitorInfo.rcWork.top;
  364. int left = screenw > int(winWidth) ? ((screenw - int(winWidth)) / 2) : 0;
  365. int top = screenh > int(winHeight) ? ((screenh - int(winHeight)) / 2) : 0;
  366. SetWindowLong(mHWnd, GWL_STYLE, style);
  367. SetWindowPos(mHWnd, HWND_NOTOPMOST, left, top, winWidth, winHeight,
  368. SWP_DRAWFRAME | SWP_FRAMECHANGED | SWP_NOACTIVATE);
  369. _windowMovedOrResized();
  370. }
  371. bool Win32Window::isActive(void) const
  372. {
  373. if (isFullScreen())
  374. return isVisible();
  375. return mActive && isVisible();
  376. }
  377. bool Win32Window::isVisible() const
  378. {
  379. return (mHWnd && !IsIconic(mHWnd));
  380. }
  381. bool Win32Window::isClosed() const
  382. {
  383. return mClosed;
  384. }
  385. void Win32Window::move(INT32 left, INT32 top)
  386. {
  387. THROW_IF_NOT_CORE_THREAD;
  388. if (mHWnd && !mIsFullScreen)
  389. {
  390. mLeft = left;
  391. mTop = top;
  392. SetWindowPos(mHWnd, 0, left, top, 0, 0,
  393. SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
  394. }
  395. }
  396. void Win32Window::resize(UINT32 width, UINT32 height)
  397. {
  398. THROW_IF_NOT_CORE_THREAD;
  399. if (mHWnd && !mIsFullScreen)
  400. {
  401. mWidth = width;
  402. mHeight = height;
  403. RECT rc = { 0, 0, width, height };
  404. AdjustWindowRect(&rc, GetWindowLong(mHWnd, GWL_STYLE), false);
  405. width = rc.right - rc.left;
  406. height = rc.bottom - rc.top;
  407. SetWindowPos(mHWnd, 0, 0, 0, width, height,
  408. SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE);
  409. }
  410. }
  411. void Win32Window::swapBuffers()
  412. {
  413. THROW_IF_NOT_CORE_THREAD;
  414. if (!mIsExternalGLControl) {
  415. SwapBuffers(mHDC);
  416. }
  417. }
  418. void Win32Window::copyToMemory(const PixelData &dst, FrameBuffer buffer)
  419. {
  420. THROW_IF_NOT_CORE_THREAD;
  421. if ((dst.getLeft() < 0) || (dst.getRight() > mWidth) ||
  422. (dst.getTop() < 0) || (dst.getBottom() > mHeight) ||
  423. (dst.getFront() != 0) || (dst.getBack() != 1))
  424. {
  425. CM_EXCEPT(InvalidParametersException, "Invalid box.");
  426. }
  427. if (buffer == FB_AUTO)
  428. {
  429. buffer = mIsFullScreen? FB_FRONT : FB_BACK;
  430. }
  431. GLenum format = BansheeEngine::GLPixelUtil::getGLOriginFormat(dst.getFormat());
  432. GLenum type = BansheeEngine::GLPixelUtil::getGLOriginDataType(dst.getFormat());
  433. if ((format == GL_NONE) || (type == 0))
  434. {
  435. CM_EXCEPT(InvalidParametersException, "Unsupported format.");
  436. }
  437. // Must change the packing to ensure no overruns!
  438. glPixelStorei(GL_PACK_ALIGNMENT, 1);
  439. glReadBuffer((buffer == FB_FRONT)? GL_FRONT : GL_BACK);
  440. glReadPixels((GLint)dst.getLeft(), (GLint)dst.getTop(),
  441. (GLsizei)dst.getWidth(), (GLsizei)dst.getHeight(),
  442. format, type, dst.getData());
  443. // restore default alignment
  444. glPixelStorei(GL_PACK_ALIGNMENT, 4);
  445. //vertical flip
  446. {
  447. size_t rowSpan = dst.getWidth() * PixelUtil::getNumElemBytes(dst.getFormat());
  448. size_t height = dst.getHeight();
  449. UINT8 *tmpData = (UINT8*)cm_alloc<ScratchAlloc>((UINT32)(rowSpan * height));
  450. UINT8 *srcRow = (UINT8 *)dst.getData(), *tmpRow = tmpData + (height - 1) * rowSpan;
  451. while (tmpRow >= tmpData)
  452. {
  453. memcpy(tmpRow, srcRow, rowSpan);
  454. srcRow += rowSpan;
  455. tmpRow -= rowSpan;
  456. }
  457. memcpy(dst.getData(), tmpData, rowSpan * height);
  458. cm_free<ScratchAlloc>(tmpData);
  459. }
  460. }
  461. Vector2I Win32Window::screenToWindowPos(const Vector2I& screenPos) const
  462. {
  463. POINT pos;
  464. pos.x = screenPos.x;
  465. pos.y = screenPos.y;
  466. ScreenToClient(mHWnd, &pos);
  467. return Vector2I(pos.x, pos.y);
  468. }
  469. Vector2I Win32Window::windowToScreenPos(const Vector2I& windowPos) const
  470. {
  471. POINT pos;
  472. pos.x = windowPos.x;
  473. pos.y = windowPos.y;
  474. ClientToScreen(mHWnd, &pos);
  475. return Vector2I(pos.x, pos.y);
  476. }
  477. void Win32Window::getCustomAttribute( const String& name, void* pData ) const
  478. {
  479. if( name == "GLCONTEXT" ) {
  480. *static_cast<GLContext**>(pData) = mContext;
  481. return;
  482. } else if( name == "WINDOW" )
  483. {
  484. HWND *pHwnd = (HWND*)pData;
  485. *pHwnd = _getWindowHandle();
  486. return;
  487. }
  488. }
  489. void Win32Window::setActive( bool state )
  490. {
  491. THROW_IF_NOT_CORE_THREAD;
  492. if (mDeviceName != NULL && state == false)
  493. {
  494. HWND hActiveWindow = GetActiveWindow();
  495. char classNameSrc[_MAX_CLASS_NAME_ + 1];
  496. char classNameDst[_MAX_CLASS_NAME_ + 1];
  497. GetClassName(mHWnd, classNameSrc, _MAX_CLASS_NAME_);
  498. GetClassName(hActiveWindow, classNameDst, _MAX_CLASS_NAME_);
  499. if (strcmp(classNameDst, classNameSrc) == 0)
  500. {
  501. state = true;
  502. }
  503. }
  504. mActive = state;
  505. if( mIsFullScreen )
  506. {
  507. if( state == false )
  508. { //Restore Desktop
  509. ChangeDisplaySettingsEx(mDeviceName, NULL, NULL, 0, NULL);
  510. ShowWindow(mHWnd, SW_SHOWMINNOACTIVE);
  511. }
  512. else
  513. { //Restore App
  514. ShowWindow(mHWnd, SW_SHOWNORMAL);
  515. DEVMODE displayDeviceMode;
  516. memset(&displayDeviceMode, 0, sizeof(displayDeviceMode));
  517. displayDeviceMode.dmSize = sizeof(DEVMODE);
  518. displayDeviceMode.dmBitsPerPel = mColorDepth;
  519. displayDeviceMode.dmPelsWidth = mWidth;
  520. displayDeviceMode.dmPelsHeight = mHeight;
  521. displayDeviceMode.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
  522. if (mDisplayFrequency)
  523. {
  524. displayDeviceMode.dmDisplayFrequency = mDisplayFrequency;
  525. displayDeviceMode.dmFields |= DM_DISPLAYFREQUENCY;
  526. }
  527. ChangeDisplaySettingsEx(mDeviceName, &displayDeviceMode, NULL, CDS_FULLSCREEN, NULL);
  528. }
  529. }
  530. }
  531. void Win32Window::setHidden(bool hidden)
  532. {
  533. THROW_IF_NOT_CORE_THREAD;
  534. mHidden = hidden;
  535. if (!mIsExternal)
  536. {
  537. if (hidden)
  538. ShowWindow(mHWnd, SW_HIDE);
  539. else
  540. ShowWindow(mHWnd, SW_SHOWNORMAL);
  541. }
  542. }
  543. void Win32Window::_windowMovedOrResized()
  544. {
  545. if (!mHWnd || IsIconic(mHWnd))
  546. return;
  547. RECT rc;
  548. // top and left represent outer window position
  549. GetWindowRect(mHWnd, &rc);
  550. mTop = rc.top;
  551. mLeft = rc.left;
  552. // width and height represent drawable area only
  553. GetClientRect(mHWnd, &rc);
  554. mWidth = rc.right - rc.left;
  555. mHeight = rc.bottom - rc.top;
  556. RenderWindow::_windowMovedOrResized();
  557. }
  558. void Win32Window::_adjustWindow(unsigned int clientWidth, unsigned int clientHeight,
  559. unsigned int* winWidth, unsigned int* winHeight)
  560. {
  561. // NB only call this for non full screen
  562. RECT rc;
  563. SetRect(&rc, 0, 0, clientWidth, clientHeight);
  564. AdjustWindowRect(&rc, WS_VISIBLE | WS_CLIPCHILDREN | WS_OVERLAPPEDWINDOW, false);
  565. *winWidth = rc.right - rc.left;
  566. *winHeight = rc.bottom - rc.top;
  567. // adjust to monitor
  568. HMONITOR hMonitor = MonitorFromWindow(mHWnd, MONITOR_DEFAULTTONEAREST);
  569. // Get monitor info
  570. MONITORINFO monitorInfo;
  571. memset(&monitorInfo, 0, sizeof(MONITORINFO));
  572. monitorInfo.cbSize = sizeof(MONITORINFO);
  573. GetMonitorInfo(hMonitor, &monitorInfo);
  574. LONG maxW = monitorInfo.rcWork.right - monitorInfo.rcWork.left;
  575. LONG maxH = monitorInfo.rcWork.bottom - monitorInfo.rcWork.top;
  576. if (*winWidth > (unsigned int)maxW)
  577. *winWidth = maxW;
  578. if (*winHeight > (unsigned int)maxH)
  579. *winHeight = maxH;
  580. }
  581. }