CmWin32Window.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833
  1. /*
  2. -----------------------------------------------------------------------------
  3. This source file is part of OGRE
  4. (Object-oriented Graphics Rendering Engine)
  5. For the latest info, see http://www.ogre3d.org/
  6. Copyright (c) 2000-2011 Torus Knot Software Ltd
  7. Permission is hereby granted, free of charge, to any person obtaining a copy
  8. of this software and associated documentation files (the "Software"), to deal
  9. in the Software without restriction, including without limitation the rights
  10. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. copies of the Software, and to permit persons to whom the Software is
  12. furnished to do so, subject to the following conditions:
  13. The above copyright notice and this permission notice shall be included in
  14. all copies or substantial portions of the Software.
  15. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. THE SOFTWARE.
  22. -----------------------------------------------------------------------------
  23. */
  24. #ifndef _WIN32_WINNT
  25. #define _WIN32_WINNT 0x0500
  26. #endif
  27. #include "CmWin32Window.h"
  28. #include "CmInput.h"
  29. #include "CmRenderSystem.h"
  30. #include "CmCoreThread.h"
  31. #include "CmException.h"
  32. #include "CmWin32GLSupport.h"
  33. #include "CmWin32Context.h"
  34. #include "CmPlatformWndProc.h"
  35. #include "CmGLPixelFormat.h"
  36. namespace CamelotFramework
  37. {
  38. // HACK: During the move/resize modal loop no mouse messages will be posted, which means we will
  39. // never receive a "mouse up" event, even though user had to release the mouse to stop the loop. But our GUI system
  40. // relies on mouse down being followed by mouse up otherwise things end start to break a bit. So here we simulate
  41. // the mouse release.
  42. // Note: This is possible because SendMessage won't return until user releases the mouse and modal loop is done.
  43. void HACK_SendLMBUpEvent()
  44. {
  45. gInput().simulateButtonUp(BC_MOUSE_LEFT);
  46. }
  47. #define _MAX_CLASS_NAME_ 128
  48. Win32Window::Win32Window(const RENDER_WINDOW_DESC& desc, Win32GLSupport &glsupport):
  49. RenderWindow(desc),
  50. mGLSupport(glsupport),
  51. mContext(0)
  52. {
  53. mIsFullScreen = false;
  54. mHWnd = 0;
  55. mGlrc = 0;
  56. mIsExternal = false;
  57. mIsExternalGLControl = false;
  58. mIsExternalGLContext = false;
  59. mSizing = false;
  60. mClosed = false;
  61. mDisplayFrequency = 0;
  62. mActive = false;
  63. mDeviceName = NULL;
  64. }
  65. Win32Window::~Win32Window()
  66. {
  67. }
  68. void Win32Window::initialize_internal()
  69. {
  70. #ifdef CM_STATIC_LIB
  71. HINSTANCE hInst = GetModuleHandle( NULL );
  72. #else
  73. HINSTANCE hInst = GetModuleHandle("CamelotGLRenderSystem.dll");
  74. #endif
  75. mHWnd = 0;
  76. mName = mDesc.title;
  77. mIsFullScreen = mDesc.fullscreen;
  78. mClosed = false;
  79. mDisplayFrequency = mDesc.displayFrequency;
  80. mColorDepth = mDesc.colorDepth;
  81. HWND parent = 0;
  82. HMONITOR hMonitor = NULL;
  83. int monitorIndex = mDesc.monitorIndex;
  84. // Get variable-length params
  85. NameValuePairList::const_iterator opt;
  86. NameValuePairList::const_iterator end = mDesc.platformSpecific.end();
  87. if ((opt = mDesc.platformSpecific.find("externalWindowHandle")) != end)
  88. {
  89. mHWnd = (HWND)parseUnsignedInt(opt->second);
  90. if (mHWnd)
  91. {
  92. mIsExternal = true;
  93. mIsFullScreen = false;
  94. }
  95. if ((opt = mDesc.platformSpecific.find("externalGLControl")) != end) {
  96. mIsExternalGLControl = parseBool(opt->second);
  97. }
  98. }
  99. if ((opt = mDesc.platformSpecific.find("externalGLContext")) != end)
  100. {
  101. mGlrc = (HGLRC)parseUnsignedLong(opt->second);
  102. if( mGlrc )
  103. mIsExternalGLContext = true;
  104. }
  105. // incompatible with fullscreen
  106. if ((opt = mDesc.platformSpecific.find("parentWindowHandle")) != end)
  107. parent = (HWND)parseUnsignedInt(opt->second);
  108. // monitor handle
  109. if ((opt = mDesc.platformSpecific.find("monitorHandle")) != end)
  110. hMonitor = (HMONITOR)parseInt(opt->second);
  111. if (!mIsFullScreen)
  112. {
  113. // make sure we don't exceed desktop colour depth
  114. if ((int)mColorDepth > GetDeviceCaps(GetDC(0), BITSPIXEL))
  115. mColorDepth = GetDeviceCaps(GetDC(0), BITSPIXEL);
  116. }
  117. if (!mIsExternal)
  118. {
  119. DWORD dwStyle = WS_VISIBLE | WS_CLIPCHILDREN;
  120. DWORD dwStyleEx = 0;
  121. MONITORINFOEX monitorInfoEx;
  122. RECT rc;
  123. // If we didn't specified the adapter index, or if it didn't find it
  124. if (hMonitor == NULL)
  125. {
  126. POINT windowAnchorPoint;
  127. // Fill in anchor point.
  128. windowAnchorPoint.x = mDesc.left;
  129. windowAnchorPoint.y = mDesc.top;
  130. // Get the nearest monitor to this window.
  131. hMonitor = MonitorFromPoint(windowAnchorPoint, MONITOR_DEFAULTTONEAREST);
  132. }
  133. // Get the target monitor info
  134. memset(&monitorInfoEx, 0, sizeof(MONITORINFOEX));
  135. monitorInfoEx.cbSize = sizeof(MONITORINFOEX);
  136. GetMonitorInfo(hMonitor, &monitorInfoEx);
  137. size_t devNameLen = strlen(monitorInfoEx.szDevice);
  138. mDeviceName = (char*)cm_alloc<ScratchAlloc>((UINT32)(devNameLen + 1));
  139. strcpy_s(mDeviceName, devNameLen + 1, monitorInfoEx.szDevice);
  140. UINT32 left = mDesc.left;
  141. UINT32 top = mDesc.top;
  142. // No specified top left -> Center the window in the middle of the monitor
  143. if (left == -1 || top == -1)
  144. {
  145. int screenw = monitorInfoEx.rcWork.right - monitorInfoEx.rcWork.left;
  146. int screenh = monitorInfoEx.rcWork.bottom - monitorInfoEx.rcWork.top;
  147. unsigned int winWidth, winHeight;
  148. _adjustWindow(mDesc.width, mDesc.height, &winWidth, &winHeight);
  149. // clamp window dimensions to screen size
  150. int outerw = (int(winWidth) < screenw)? int(winWidth) : screenw;
  151. int outerh = (int(winHeight) < screenh)? int(winHeight) : screenh;
  152. if (left == -1)
  153. left = monitorInfoEx.rcWork.left + (screenw - outerw) / 2;
  154. else if (mDesc.monitorIndex != -1)
  155. left += monitorInfoEx.rcWork.left;
  156. if (top == -1)
  157. top = monitorInfoEx.rcWork.top + (screenh - outerh) / 2;
  158. else if (mDesc.monitorIndex != -1)
  159. top += monitorInfoEx.rcWork.top;
  160. }
  161. else if (mDesc.monitorIndex != -1)
  162. {
  163. left += monitorInfoEx.rcWork.left;
  164. top += monitorInfoEx.rcWork.top;
  165. }
  166. mWidth = mDesc.width;
  167. mHeight = mDesc.height;
  168. mTop = top;
  169. mLeft = left;
  170. if (mIsFullScreen)
  171. {
  172. dwStyle |= WS_POPUP;
  173. dwStyleEx |= WS_EX_TOPMOST;
  174. mTop = monitorInfoEx.rcMonitor.top;
  175. mLeft = monitorInfoEx.rcMonitor.left;
  176. }
  177. else
  178. {
  179. if (parent)
  180. {
  181. if(mDesc.toolWindow)
  182. dwStyleEx = WS_EX_TOOLWINDOW;
  183. else
  184. dwStyle |= WS_CHILD;
  185. }
  186. if (!parent || mDesc.toolWindow)
  187. {
  188. if (mDesc.border == WindowBorder::None)
  189. dwStyle |= WS_POPUP;
  190. else if (mDesc.border == WindowBorder::Fixed)
  191. dwStyle |= WS_OVERLAPPED | WS_BORDER | WS_CAPTION |
  192. WS_SYSMENU | WS_MINIMIZEBOX;
  193. else
  194. dwStyle |= WS_OVERLAPPEDWINDOW;
  195. }
  196. int screenw = GetSystemMetrics(SM_CXSCREEN);
  197. int screenh = GetSystemMetrics(SM_CYSCREEN);
  198. if (!mDesc.outerDimensions)
  199. {
  200. // Calculate window dimensions required
  201. // to get the requested client area
  202. SetRect(&rc, 0, 0, mWidth, mHeight);
  203. AdjustWindowRect(&rc, dwStyle, false);
  204. mWidth = rc.right - rc.left;
  205. mHeight = rc.bottom - rc.top;
  206. // Clamp window rect to the nearest display monitor.
  207. if (mLeft < monitorInfoEx.rcWork.left)
  208. mLeft = monitorInfoEx.rcWork.left;
  209. if (mTop < monitorInfoEx.rcWork.top)
  210. mTop = monitorInfoEx.rcWork.top;
  211. if ((int)mWidth > monitorInfoEx.rcWork.right - mLeft)
  212. mWidth = monitorInfoEx.rcWork.right - mLeft;
  213. if ((int)mHeight > monitorInfoEx.rcWork.bottom - mTop)
  214. mHeight = monitorInfoEx.rcWork.bottom - mTop;
  215. }
  216. }
  217. // register class and create window
  218. WNDCLASS wc = { CS_OWNDC, PlatformWndProc::_win32WndProc, 0, 0, hInst,
  219. LoadIcon(NULL, IDI_APPLICATION), LoadCursor(NULL, IDC_ARROW),
  220. (HBRUSH)GetStockObject(BLACK_BRUSH), NULL, "GLWindow" };
  221. RegisterClass(&wc);
  222. if (mIsFullScreen)
  223. {
  224. DEVMODE displayDeviceMode;
  225. memset(&displayDeviceMode, 0, sizeof(displayDeviceMode));
  226. displayDeviceMode.dmSize = sizeof(DEVMODE);
  227. displayDeviceMode.dmBitsPerPel = mColorDepth;
  228. displayDeviceMode.dmPelsWidth = mWidth;
  229. displayDeviceMode.dmPelsHeight = mHeight;
  230. displayDeviceMode.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
  231. if (mDisplayFrequency)
  232. {
  233. displayDeviceMode.dmDisplayFrequency = mDisplayFrequency;
  234. displayDeviceMode.dmFields |= DM_DISPLAYFREQUENCY;
  235. if (ChangeDisplaySettingsEx(mDeviceName, &displayDeviceMode, NULL, CDS_FULLSCREEN | CDS_TEST, NULL) != DISP_CHANGE_SUCCESSFUL)
  236. {
  237. // TODO LOG PORT - Log this somewhere
  238. //LogManager::getSingleton().logMessage(LML_NORMAL, "ChangeDisplaySettings with user display frequency failed");
  239. //displayDeviceMode.dmFields ^= DM_DISPLAYFREQUENCY;
  240. }
  241. }
  242. if (ChangeDisplaySettingsEx(mDeviceName, &displayDeviceMode, NULL, CDS_FULLSCREEN, NULL) != DISP_CHANGE_SUCCESSFUL)
  243. {
  244. // TODO LOG PORT - Log this somewhere
  245. //LogManager::getSingleton().logMessage(LML_CRITICAL, "ChangeDisplaySettings failed");
  246. }
  247. }
  248. // Pass pointer to self as WM_CREATE parameter
  249. mHWnd = CreateWindowEx(dwStyleEx, "GLWindow", mDesc.title.c_str(),
  250. dwStyle, mLeft, mTop, mWidth, mHeight, parent, 0, hInst, this);
  251. }
  252. RECT rc;
  253. // top and left represent outer window position
  254. GetWindowRect(mHWnd, &rc);
  255. mTop = rc.top;
  256. mLeft = rc.left;
  257. // width and height represent drawable area only
  258. GetClientRect(mHWnd, &rc);
  259. mWidth = rc.right;
  260. mHeight = rc.bottom;
  261. mHDC = GetDC(mHWnd);
  262. if (!mIsExternalGLControl)
  263. {
  264. int testFsaa = mFSAA;
  265. bool testHwGamma = mDesc.gamma;
  266. bool formatOk = mGLSupport.selectPixelFormat(mHDC, mColorDepth, testFsaa, testHwGamma);
  267. if (!formatOk)
  268. {
  269. if (mFSAA > 0)
  270. {
  271. // try without FSAA
  272. testFsaa = 0;
  273. formatOk = mGLSupport.selectPixelFormat(mHDC, mColorDepth, testFsaa, testHwGamma);
  274. }
  275. if (!formatOk && mDesc.gamma)
  276. {
  277. // try without sRGB
  278. testHwGamma = false;
  279. testFsaa = mFSAA;
  280. formatOk = mGLSupport.selectPixelFormat(mHDC, mColorDepth, testFsaa, testHwGamma);
  281. }
  282. if (!formatOk && mDesc.gamma && (mFSAA > 0))
  283. {
  284. // try without both
  285. testHwGamma = false;
  286. testFsaa = 0;
  287. formatOk = mGLSupport.selectPixelFormat(mHDC, mColorDepth, testFsaa, testHwGamma);
  288. }
  289. if (!formatOk)
  290. CM_EXCEPT(RenderingAPIException, "selectPixelFormat failed");
  291. }
  292. // record what gamma option we used in the end
  293. // this will control enabling of sRGB state flags when used
  294. mHwGamma = testHwGamma;
  295. mFSAA = testFsaa;
  296. }
  297. mActive = true;
  298. GLRenderSystem* rs = static_cast<GLRenderSystem*>(RenderSystem::instancePtr());
  299. // If RenderSystem has initialized a context use that, otherwise we create our own
  300. if(!rs->isContextInitialized())
  301. {
  302. if (!mIsExternalGLContext)
  303. {
  304. mGlrc = wglCreateContext(mHDC);
  305. if (!mGlrc)
  306. {
  307. CM_EXCEPT(RenderingAPIException,
  308. "wglCreateContext failed: " + translateWGLError());
  309. }
  310. }
  311. }
  312. else
  313. {
  314. rs->getMainContext()->setCurrent();
  315. mGlrc = wglGetCurrentContext();
  316. }
  317. // Create RenderSystem context
  318. mContext = cm_new<Win32Context>(mHDC, mGlrc);
  319. RenderWindow::initialize_internal();
  320. }
  321. void Win32Window::destroy_internal()
  322. {
  323. if (!mHWnd)
  324. return;
  325. // Unregister and destroy GLContext
  326. cm_delete(mContext);
  327. if (!mIsExternalGLContext && mGlrc)
  328. {
  329. wglDeleteContext(mGlrc);
  330. mGlrc = 0;
  331. }
  332. if (!mIsExternal)
  333. {
  334. if (mIsFullScreen)
  335. ChangeDisplaySettingsEx(mDeviceName, NULL, NULL, 0, NULL);
  336. DestroyWindow(mHWnd);
  337. }
  338. else
  339. {
  340. // just release the DC
  341. ReleaseDC(mHWnd, mHDC);
  342. }
  343. mActive = false;
  344. mClosed = true;
  345. mHDC = 0; // no release thanks to CS_OWNDC wndclass style
  346. mHWnd = 0;
  347. if (mDeviceName != NULL)
  348. {
  349. cm_free<ScratchAlloc>(mDeviceName);
  350. mDeviceName = NULL;
  351. }
  352. RenderWindow::destroy_internal();
  353. }
  354. void Win32Window::setFullscreen(bool fullScreen, UINT32 width, UINT32 height)
  355. {
  356. THROW_IF_NOT_CORE_THREAD;
  357. if (mIsFullScreen != fullScreen || width != mWidth || height != mHeight)
  358. {
  359. mIsFullScreen = fullScreen;
  360. DWORD dwStyle = WS_VISIBLE | WS_CLIPCHILDREN;
  361. if (mIsFullScreen)
  362. {
  363. dwStyle |= WS_POPUP;
  364. DEVMODE displayDeviceMode;
  365. memset(&displayDeviceMode, 0, sizeof(displayDeviceMode));
  366. displayDeviceMode.dmSize = sizeof(DEVMODE);
  367. displayDeviceMode.dmBitsPerPel = mColorDepth;
  368. displayDeviceMode.dmPelsWidth = width;
  369. displayDeviceMode.dmPelsHeight = height;
  370. displayDeviceMode.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
  371. if (mDisplayFrequency)
  372. {
  373. displayDeviceMode.dmDisplayFrequency = mDisplayFrequency;
  374. displayDeviceMode.dmFields |= DM_DISPLAYFREQUENCY;
  375. if (ChangeDisplaySettingsEx(mDeviceName, &displayDeviceMode, NULL,
  376. CDS_FULLSCREEN | CDS_TEST, NULL) != DISP_CHANGE_SUCCESSFUL)
  377. {
  378. // TODO LOG PORT - Log this somewhere
  379. //LogManager::getSingleton().logMessage(LML_NORMAL, "ChangeDisplaySettings with user display frequency failed");
  380. displayDeviceMode.dmFields ^= DM_DISPLAYFREQUENCY;
  381. }
  382. }
  383. else
  384. {
  385. // try a few
  386. displayDeviceMode.dmDisplayFrequency = 100;
  387. displayDeviceMode.dmFields |= DM_DISPLAYFREQUENCY;
  388. if (ChangeDisplaySettingsEx(mDeviceName, &displayDeviceMode, NULL,
  389. CDS_FULLSCREEN | CDS_TEST, NULL) != DISP_CHANGE_SUCCESSFUL)
  390. {
  391. displayDeviceMode.dmDisplayFrequency = 75;
  392. if (ChangeDisplaySettingsEx(mDeviceName, &displayDeviceMode, NULL,
  393. CDS_FULLSCREEN | CDS_TEST, NULL) != DISP_CHANGE_SUCCESSFUL)
  394. {
  395. displayDeviceMode.dmFields ^= DM_DISPLAYFREQUENCY;
  396. }
  397. }
  398. }
  399. // move window to 0,0 before display switch
  400. SetWindowPos(mHWnd, HWND_TOPMOST, 0, 0, mWidth, mHeight, SWP_NOACTIVATE);
  401. if (ChangeDisplaySettingsEx(mDeviceName, &displayDeviceMode, NULL, CDS_FULLSCREEN, NULL) != DISP_CHANGE_SUCCESSFUL)
  402. {
  403. // TODO LOG PORT - Log this somewhere
  404. //LogManager::getSingleton().logMessage(LML_CRITICAL, "ChangeDisplaySettings failed");
  405. }
  406. // Get the nearest monitor to this window.
  407. HMONITOR hMonitor = MonitorFromWindow(mHWnd, MONITOR_DEFAULTTONEAREST);
  408. // Get monitor info
  409. MONITORINFO monitorInfo;
  410. memset(&monitorInfo, 0, sizeof(MONITORINFO));
  411. monitorInfo.cbSize = sizeof(MONITORINFO);
  412. GetMonitorInfo(hMonitor, &monitorInfo);
  413. mTop = monitorInfo.rcMonitor.top;
  414. mLeft = monitorInfo.rcMonitor.left;
  415. SetWindowLong(mHWnd, GWL_STYLE, dwStyle);
  416. SetWindowPos(mHWnd, HWND_TOPMOST, mLeft, mTop, width, height,
  417. SWP_NOACTIVATE);
  418. mWidth = width;
  419. mHeight = height;
  420. }
  421. else
  422. {
  423. dwStyle |= WS_OVERLAPPEDWINDOW;
  424. // drop out of fullscreen
  425. ChangeDisplaySettingsEx(mDeviceName, NULL, NULL, 0, NULL);
  426. // calculate overall dimensions for requested client area
  427. unsigned int winWidth, winHeight;
  428. _adjustWindow(width, height, &winWidth, &winHeight);
  429. // deal with centreing when switching down to smaller resolution
  430. HMONITOR hMonitor = MonitorFromWindow(mHWnd, MONITOR_DEFAULTTONEAREST);
  431. MONITORINFO monitorInfo;
  432. memset(&monitorInfo, 0, sizeof(MONITORINFO));
  433. monitorInfo.cbSize = sizeof(MONITORINFO);
  434. GetMonitorInfo(hMonitor, &monitorInfo);
  435. LONG screenw = monitorInfo.rcWork.right - monitorInfo.rcWork.left;
  436. LONG screenh = monitorInfo.rcWork.bottom - monitorInfo.rcWork.top;
  437. int left = screenw > int(winWidth) ? ((screenw - int(winWidth)) / 2) : 0;
  438. int top = screenh > int(winHeight) ? ((screenh - int(winHeight)) / 2) : 0;
  439. SetWindowLong(mHWnd, GWL_STYLE, dwStyle);
  440. SetWindowPos(mHWnd, HWND_NOTOPMOST, left, top, winWidth, winHeight,
  441. SWP_DRAWFRAME | SWP_FRAMECHANGED | SWP_NOACTIVATE);
  442. mWidth = width;
  443. mHeight = height;
  444. _windowMovedOrResized();
  445. }
  446. }
  447. }
  448. bool Win32Window::isActive(void) const
  449. {
  450. if (isFullScreen())
  451. return isVisible();
  452. return mActive && isVisible();
  453. }
  454. bool Win32Window::isVisible() const
  455. {
  456. return (mHWnd && !IsIconic(mHWnd));
  457. }
  458. bool Win32Window::isClosed() const
  459. {
  460. return mClosed;
  461. }
  462. void Win32Window::move(INT32 left, INT32 top)
  463. {
  464. THROW_IF_NOT_CORE_THREAD;
  465. if (mHWnd && !mIsFullScreen)
  466. {
  467. mLeft = left;
  468. mTop = top;
  469. SetWindowPos(mHWnd, 0, left, top, 0, 0,
  470. SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
  471. }
  472. }
  473. void Win32Window::resize(UINT32 width, UINT32 height)
  474. {
  475. THROW_IF_NOT_CORE_THREAD;
  476. if (mHWnd && !mIsFullScreen)
  477. {
  478. mWidth = width;
  479. mHeight = height;
  480. RECT rc = { 0, 0, width, height };
  481. AdjustWindowRect(&rc, GetWindowLong(mHWnd, GWL_STYLE), false);
  482. width = rc.right - rc.left;
  483. height = rc.bottom - rc.top;
  484. SetWindowPos(mHWnd, 0, 0, 0, width, height,
  485. SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE);
  486. }
  487. }
  488. void Win32Window::swapBuffers()
  489. {
  490. THROW_IF_NOT_CORE_THREAD;
  491. if (!mIsExternalGLControl) {
  492. SwapBuffers(mHDC);
  493. }
  494. }
  495. void Win32Window::copyToMemory(const PixelData &dst, FrameBuffer buffer)
  496. {
  497. THROW_IF_NOT_CORE_THREAD;
  498. if ((dst.getLeft() < 0) || (dst.getRight() > mWidth) ||
  499. (dst.getTop() < 0) || (dst.getBottom() > mHeight) ||
  500. (dst.getFront() != 0) || (dst.getBack() != 1))
  501. {
  502. CM_EXCEPT(InvalidParametersException, "Invalid box.");
  503. }
  504. if (buffer == FB_AUTO)
  505. {
  506. buffer = mIsFullScreen? FB_FRONT : FB_BACK;
  507. }
  508. GLenum format = CamelotFramework::GLPixelUtil::getGLOriginFormat(dst.getFormat());
  509. GLenum type = CamelotFramework::GLPixelUtil::getGLOriginDataType(dst.getFormat());
  510. if ((format == GL_NONE) || (type == 0))
  511. {
  512. CM_EXCEPT(InvalidParametersException, "Unsupported format.");
  513. }
  514. // Must change the packing to ensure no overruns!
  515. glPixelStorei(GL_PACK_ALIGNMENT, 1);
  516. glReadBuffer((buffer == FB_FRONT)? GL_FRONT : GL_BACK);
  517. glReadPixels((GLint)dst.getLeft(), (GLint)dst.getTop(),
  518. (GLsizei)dst.getWidth(), (GLsizei)dst.getHeight(),
  519. format, type, dst.getData());
  520. // restore default alignment
  521. glPixelStorei(GL_PACK_ALIGNMENT, 4);
  522. //vertical flip
  523. {
  524. size_t rowSpan = dst.getWidth() * PixelUtil::getNumElemBytes(dst.getFormat());
  525. size_t height = dst.getHeight();
  526. UINT8 *tmpData = (UINT8*)cm_alloc<ScratchAlloc>((UINT32)(rowSpan * height));
  527. UINT8 *srcRow = (UINT8 *)dst.getData(), *tmpRow = tmpData + (height - 1) * rowSpan;
  528. while (tmpRow >= tmpData)
  529. {
  530. memcpy(tmpRow, srcRow, rowSpan);
  531. srcRow += rowSpan;
  532. tmpRow -= rowSpan;
  533. }
  534. memcpy(dst.getData(), tmpData, rowSpan * height);
  535. cm_free<ScratchAlloc>(tmpData);
  536. }
  537. }
  538. Int2 Win32Window::screenToWindowPos(const Int2& screenPos) const
  539. {
  540. POINT pos;
  541. pos.x = screenPos.x;
  542. pos.y = screenPos.y;
  543. ScreenToClient(mHWnd, &pos);
  544. return Int2(pos.x, pos.y);
  545. }
  546. Int2 Win32Window::windowToScreenPos(const Int2& windowPos) const
  547. {
  548. POINT pos;
  549. pos.x = windowPos.x;
  550. pos.y = windowPos.y;
  551. ClientToScreen(mHWnd, &pos);
  552. return Int2(pos.x, pos.y);
  553. }
  554. void Win32Window::getCustomAttribute( const String& name, void* pData ) const
  555. {
  556. if( name == "GLCONTEXT" ) {
  557. *static_cast<GLContext**>(pData) = mContext;
  558. return;
  559. } else if( name == "WINDOW" )
  560. {
  561. HWND *pHwnd = (HWND*)pData;
  562. *pHwnd = _getWindowHandle();
  563. return;
  564. }
  565. }
  566. void Win32Window::setActive( bool state )
  567. {
  568. THROW_IF_NOT_CORE_THREAD;
  569. if (mDeviceName != NULL && state == false)
  570. {
  571. HWND hActiveWindow = GetActiveWindow();
  572. char classNameSrc[_MAX_CLASS_NAME_ + 1];
  573. char classNameDst[_MAX_CLASS_NAME_ + 1];
  574. GetClassName(mHWnd, classNameSrc, _MAX_CLASS_NAME_);
  575. GetClassName(hActiveWindow, classNameDst, _MAX_CLASS_NAME_);
  576. if (strcmp(classNameDst, classNameSrc) == 0)
  577. {
  578. state = true;
  579. }
  580. }
  581. mActive = state;
  582. if( mIsFullScreen )
  583. {
  584. if( state == false )
  585. { //Restore Desktop
  586. ChangeDisplaySettingsEx(mDeviceName, NULL, NULL, 0, NULL);
  587. ShowWindow(mHWnd, SW_SHOWMINNOACTIVE);
  588. }
  589. else
  590. { //Restore App
  591. ShowWindow(mHWnd, SW_SHOWNORMAL);
  592. DEVMODE displayDeviceMode;
  593. memset(&displayDeviceMode, 0, sizeof(displayDeviceMode));
  594. displayDeviceMode.dmSize = sizeof(DEVMODE);
  595. displayDeviceMode.dmBitsPerPel = mColorDepth;
  596. displayDeviceMode.dmPelsWidth = mWidth;
  597. displayDeviceMode.dmPelsHeight = mHeight;
  598. displayDeviceMode.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
  599. if (mDisplayFrequency)
  600. {
  601. displayDeviceMode.dmDisplayFrequency = mDisplayFrequency;
  602. displayDeviceMode.dmFields |= DM_DISPLAYFREQUENCY;
  603. }
  604. ChangeDisplaySettingsEx(mDeviceName, &displayDeviceMode, NULL, CDS_FULLSCREEN, NULL);
  605. }
  606. }
  607. }
  608. void Win32Window::startResize(WindowResizeDirection direction)
  609. {
  610. WPARAM dir = HTLEFT;
  611. switch(direction)
  612. {
  613. case WindowResizeDirection::Left:
  614. dir = HTLEFT;
  615. break;
  616. case WindowResizeDirection::TopLeft:
  617. dir = HTTOPLEFT;
  618. break;
  619. case WindowResizeDirection::Top:
  620. dir = HTTOP;
  621. break;
  622. case WindowResizeDirection::TopRight:
  623. dir = HTTOPRIGHT;
  624. break;
  625. case WindowResizeDirection::Right:
  626. dir = HTRIGHT;
  627. break;
  628. case WindowResizeDirection::BottomRight:
  629. dir = HTBOTTOMRIGHT;
  630. break;
  631. case WindowResizeDirection::Bottom:
  632. dir = HTBOTTOM;
  633. break;
  634. case WindowResizeDirection::BottomLeft:
  635. dir = HTBOTTOMLEFT;
  636. break;
  637. }
  638. SendMessage(mHWnd, WM_NCLBUTTONDOWN, dir, 0);
  639. HACK_SendLMBUpEvent();
  640. }
  641. void Win32Window::endResize()
  642. {
  643. }
  644. void Win32Window::startMove()
  645. {
  646. SendMessage(mHWnd, WM_NCLBUTTONDOWN, HTCAPTION, 0);
  647. HACK_SendLMBUpEvent();
  648. }
  649. void Win32Window::endMove()
  650. {
  651. }
  652. void Win32Window::_windowMovedOrResized()
  653. {
  654. if (!mHWnd || IsIconic(mHWnd))
  655. return;
  656. RECT rc;
  657. // top and left represent outer window position
  658. GetWindowRect(mHWnd, &rc);
  659. mTop = rc.top;
  660. mLeft = rc.left;
  661. // width and height represent drawable area only
  662. GetClientRect(mHWnd, &rc);
  663. mWidth = rc.right - rc.left;
  664. mHeight = rc.bottom - rc.top;
  665. RenderWindow::_windowMovedOrResized();
  666. }
  667. void Win32Window::_adjustWindow(unsigned int clientWidth, unsigned int clientHeight,
  668. unsigned int* winWidth, unsigned int* winHeight)
  669. {
  670. // NB only call this for non full screen
  671. RECT rc;
  672. SetRect(&rc, 0, 0, clientWidth, clientHeight);
  673. AdjustWindowRect(&rc, WS_VISIBLE | WS_CLIPCHILDREN | WS_OVERLAPPEDWINDOW, false);
  674. *winWidth = rc.right - rc.left;
  675. *winHeight = rc.bottom - rc.top;
  676. // adjust to monitor
  677. HMONITOR hMonitor = MonitorFromWindow(mHWnd, MONITOR_DEFAULTTONEAREST);
  678. // Get monitor info
  679. MONITORINFO monitorInfo;
  680. memset(&monitorInfo, 0, sizeof(MONITORINFO));
  681. monitorInfo.cbSize = sizeof(MONITORINFO);
  682. GetMonitorInfo(hMonitor, &monitorInfo);
  683. LONG maxW = monitorInfo.rcWork.right - monitorInfo.rcWork.left;
  684. LONG maxH = monitorInfo.rcWork.bottom - monitorInfo.rcWork.top;
  685. if (*winWidth > (unsigned int)maxW)
  686. *winWidth = maxW;
  687. if (*winHeight > (unsigned int)maxH)
  688. *winHeight = maxH;
  689. }
  690. }