CmWin32Window.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837
  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 "CmWindowEventUtilities.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, WindowEventUtilities::_WndProc, 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. WindowEventUtilities::_addRenderWindow(this);
  252. }
  253. RECT rc;
  254. // top and left represent outer window position
  255. GetWindowRect(mHWnd, &rc);
  256. mTop = rc.top;
  257. mLeft = rc.left;
  258. // width and height represent drawable area only
  259. GetClientRect(mHWnd, &rc);
  260. mWidth = rc.right;
  261. mHeight = rc.bottom;
  262. mHDC = GetDC(mHWnd);
  263. if (!mIsExternalGLControl)
  264. {
  265. int testFsaa = mFSAA;
  266. bool testHwGamma = mDesc.gamma;
  267. bool formatOk = mGLSupport.selectPixelFormat(mHDC, mColorDepth, testFsaa, testHwGamma);
  268. if (!formatOk)
  269. {
  270. if (mFSAA > 0)
  271. {
  272. // try without FSAA
  273. testFsaa = 0;
  274. formatOk = mGLSupport.selectPixelFormat(mHDC, mColorDepth, testFsaa, testHwGamma);
  275. }
  276. if (!formatOk && mDesc.gamma)
  277. {
  278. // try without sRGB
  279. testHwGamma = false;
  280. testFsaa = mFSAA;
  281. formatOk = mGLSupport.selectPixelFormat(mHDC, mColorDepth, testFsaa, testHwGamma);
  282. }
  283. if (!formatOk && mDesc.gamma && (mFSAA > 0))
  284. {
  285. // try without both
  286. testHwGamma = false;
  287. testFsaa = 0;
  288. formatOk = mGLSupport.selectPixelFormat(mHDC, mColorDepth, testFsaa, testHwGamma);
  289. }
  290. if (!formatOk)
  291. CM_EXCEPT(RenderingAPIException, "selectPixelFormat failed");
  292. }
  293. // record what gamma option we used in the end
  294. // this will control enabling of sRGB state flags when used
  295. mHwGamma = testHwGamma;
  296. mFSAA = testFsaa;
  297. }
  298. mActive = true;
  299. GLRenderSystem* rs = static_cast<GLRenderSystem*>(RenderSystem::instancePtr());
  300. // If RenderSystem has initialized a context use that, otherwise we create our own
  301. if(!rs->isContextInitialized())
  302. {
  303. if (!mIsExternalGLContext)
  304. {
  305. mGlrc = wglCreateContext(mHDC);
  306. if (!mGlrc)
  307. {
  308. CM_EXCEPT(RenderingAPIException,
  309. "wglCreateContext failed: " + translateWGLError());
  310. }
  311. }
  312. }
  313. else
  314. {
  315. rs->getMainContext()->setCurrent();
  316. mGlrc = wglGetCurrentContext();
  317. }
  318. // Create RenderSystem context
  319. mContext = cm_new<Win32Context>(mHDC, mGlrc);
  320. RenderWindow::initialize_internal();
  321. }
  322. void Win32Window::destroy_internal()
  323. {
  324. if (!mHWnd)
  325. return;
  326. // Unregister and destroy GLContext
  327. cm_delete(mContext);
  328. if (!mIsExternalGLContext && mGlrc)
  329. {
  330. wglDeleteContext(mGlrc);
  331. mGlrc = 0;
  332. }
  333. if (!mIsExternal)
  334. {
  335. WindowEventUtilities::_removeRenderWindow(this);
  336. if (mIsFullScreen)
  337. ChangeDisplaySettingsEx(mDeviceName, NULL, NULL, 0, NULL);
  338. DestroyWindow(mHWnd);
  339. }
  340. else
  341. {
  342. // just release the DC
  343. ReleaseDC(mHWnd, mHDC);
  344. }
  345. mActive = false;
  346. mClosed = true;
  347. mHDC = 0; // no release thanks to CS_OWNDC wndclass style
  348. mHWnd = 0;
  349. if (mDeviceName != NULL)
  350. {
  351. cm_free<ScratchAlloc>(mDeviceName);
  352. mDeviceName = NULL;
  353. }
  354. RenderWindow::destroy_internal();
  355. }
  356. void Win32Window::setFullscreen(bool fullScreen, UINT32 width, UINT32 height)
  357. {
  358. THROW_IF_NOT_CORE_THREAD;
  359. if (mIsFullScreen != fullScreen || width != mWidth || height != mHeight)
  360. {
  361. mIsFullScreen = fullScreen;
  362. DWORD dwStyle = WS_VISIBLE | WS_CLIPCHILDREN;
  363. if (mIsFullScreen)
  364. {
  365. dwStyle |= WS_POPUP;
  366. DEVMODE displayDeviceMode;
  367. memset(&displayDeviceMode, 0, sizeof(displayDeviceMode));
  368. displayDeviceMode.dmSize = sizeof(DEVMODE);
  369. displayDeviceMode.dmBitsPerPel = mColorDepth;
  370. displayDeviceMode.dmPelsWidth = width;
  371. displayDeviceMode.dmPelsHeight = height;
  372. displayDeviceMode.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
  373. if (mDisplayFrequency)
  374. {
  375. displayDeviceMode.dmDisplayFrequency = mDisplayFrequency;
  376. displayDeviceMode.dmFields |= DM_DISPLAYFREQUENCY;
  377. if (ChangeDisplaySettingsEx(mDeviceName, &displayDeviceMode, NULL,
  378. CDS_FULLSCREEN | CDS_TEST, NULL) != DISP_CHANGE_SUCCESSFUL)
  379. {
  380. // TODO LOG PORT - Log this somewhere
  381. //LogManager::getSingleton().logMessage(LML_NORMAL, "ChangeDisplaySettings with user display frequency failed");
  382. displayDeviceMode.dmFields ^= DM_DISPLAYFREQUENCY;
  383. }
  384. }
  385. else
  386. {
  387. // try a few
  388. displayDeviceMode.dmDisplayFrequency = 100;
  389. displayDeviceMode.dmFields |= DM_DISPLAYFREQUENCY;
  390. if (ChangeDisplaySettingsEx(mDeviceName, &displayDeviceMode, NULL,
  391. CDS_FULLSCREEN | CDS_TEST, NULL) != DISP_CHANGE_SUCCESSFUL)
  392. {
  393. displayDeviceMode.dmDisplayFrequency = 75;
  394. if (ChangeDisplaySettingsEx(mDeviceName, &displayDeviceMode, NULL,
  395. CDS_FULLSCREEN | CDS_TEST, NULL) != DISP_CHANGE_SUCCESSFUL)
  396. {
  397. displayDeviceMode.dmFields ^= DM_DISPLAYFREQUENCY;
  398. }
  399. }
  400. }
  401. // move window to 0,0 before display switch
  402. SetWindowPos(mHWnd, HWND_TOPMOST, 0, 0, mWidth, mHeight, SWP_NOACTIVATE);
  403. if (ChangeDisplaySettingsEx(mDeviceName, &displayDeviceMode, NULL, CDS_FULLSCREEN, NULL) != DISP_CHANGE_SUCCESSFUL)
  404. {
  405. // TODO LOG PORT - Log this somewhere
  406. //LogManager::getSingleton().logMessage(LML_CRITICAL, "ChangeDisplaySettings failed");
  407. }
  408. // Get the nearest monitor to this window.
  409. HMONITOR hMonitor = MonitorFromWindow(mHWnd, MONITOR_DEFAULTTONEAREST);
  410. // Get monitor info
  411. MONITORINFO monitorInfo;
  412. memset(&monitorInfo, 0, sizeof(MONITORINFO));
  413. monitorInfo.cbSize = sizeof(MONITORINFO);
  414. GetMonitorInfo(hMonitor, &monitorInfo);
  415. mTop = monitorInfo.rcMonitor.top;
  416. mLeft = monitorInfo.rcMonitor.left;
  417. SetWindowLong(mHWnd, GWL_STYLE, dwStyle);
  418. SetWindowPos(mHWnd, HWND_TOPMOST, mLeft, mTop, width, height,
  419. SWP_NOACTIVATE);
  420. mWidth = width;
  421. mHeight = height;
  422. }
  423. else
  424. {
  425. dwStyle |= WS_OVERLAPPEDWINDOW;
  426. // drop out of fullscreen
  427. ChangeDisplaySettingsEx(mDeviceName, NULL, NULL, 0, NULL);
  428. // calculate overall dimensions for requested client area
  429. unsigned int winWidth, winHeight;
  430. _adjustWindow(width, height, &winWidth, &winHeight);
  431. // deal with centreing when switching down to smaller resolution
  432. HMONITOR hMonitor = MonitorFromWindow(mHWnd, MONITOR_DEFAULTTONEAREST);
  433. MONITORINFO monitorInfo;
  434. memset(&monitorInfo, 0, sizeof(MONITORINFO));
  435. monitorInfo.cbSize = sizeof(MONITORINFO);
  436. GetMonitorInfo(hMonitor, &monitorInfo);
  437. LONG screenw = monitorInfo.rcWork.right - monitorInfo.rcWork.left;
  438. LONG screenh = monitorInfo.rcWork.bottom - monitorInfo.rcWork.top;
  439. int left = screenw > int(winWidth) ? ((screenw - int(winWidth)) / 2) : 0;
  440. int top = screenh > int(winHeight) ? ((screenh - int(winHeight)) / 2) : 0;
  441. SetWindowLong(mHWnd, GWL_STYLE, dwStyle);
  442. SetWindowPos(mHWnd, HWND_NOTOPMOST, left, top, winWidth, winHeight,
  443. SWP_DRAWFRAME | SWP_FRAMECHANGED | SWP_NOACTIVATE);
  444. mWidth = width;
  445. mHeight = height;
  446. _windowMovedOrResized();
  447. }
  448. }
  449. }
  450. bool Win32Window::isActive(void) const
  451. {
  452. if (isFullScreen())
  453. return isVisible();
  454. return mActive && isVisible();
  455. }
  456. bool Win32Window::isVisible() const
  457. {
  458. return (mHWnd && !IsIconic(mHWnd));
  459. }
  460. bool Win32Window::isClosed() const
  461. {
  462. return mClosed;
  463. }
  464. void Win32Window::move(INT32 left, INT32 top)
  465. {
  466. THROW_IF_NOT_CORE_THREAD;
  467. if (mHWnd && !mIsFullScreen)
  468. {
  469. mLeft = left;
  470. mTop = top;
  471. SetWindowPos(mHWnd, 0, left, top, 0, 0,
  472. SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
  473. }
  474. }
  475. void Win32Window::resize(UINT32 width, UINT32 height)
  476. {
  477. THROW_IF_NOT_CORE_THREAD;
  478. if (mHWnd && !mIsFullScreen)
  479. {
  480. mWidth = width;
  481. mHeight = height;
  482. RECT rc = { 0, 0, width, height };
  483. AdjustWindowRect(&rc, GetWindowLong(mHWnd, GWL_STYLE), false);
  484. width = rc.right - rc.left;
  485. height = rc.bottom - rc.top;
  486. SetWindowPos(mHWnd, 0, 0, 0, width, height,
  487. SWP_NOMOVE | SWP_NOZORDER | SWP_NOACTIVATE);
  488. }
  489. }
  490. void Win32Window::swapBuffers()
  491. {
  492. THROW_IF_NOT_CORE_THREAD;
  493. if (!mIsExternalGLControl) {
  494. SwapBuffers(mHDC);
  495. }
  496. }
  497. void Win32Window::copyToMemory(const PixelData &dst, FrameBuffer buffer)
  498. {
  499. THROW_IF_NOT_CORE_THREAD;
  500. if ((dst.getLeft() < 0) || (dst.getRight() > mWidth) ||
  501. (dst.getTop() < 0) || (dst.getBottom() > mHeight) ||
  502. (dst.getFront() != 0) || (dst.getBack() != 1))
  503. {
  504. CM_EXCEPT(InvalidParametersException, "Invalid box.");
  505. }
  506. if (buffer == FB_AUTO)
  507. {
  508. buffer = mIsFullScreen? FB_FRONT : FB_BACK;
  509. }
  510. GLenum format = CamelotFramework::GLPixelUtil::getGLOriginFormat(dst.getFormat());
  511. GLenum type = CamelotFramework::GLPixelUtil::getGLOriginDataType(dst.getFormat());
  512. if ((format == GL_NONE) || (type == 0))
  513. {
  514. CM_EXCEPT(InvalidParametersException, "Unsupported format.");
  515. }
  516. // Must change the packing to ensure no overruns!
  517. glPixelStorei(GL_PACK_ALIGNMENT, 1);
  518. glReadBuffer((buffer == FB_FRONT)? GL_FRONT : GL_BACK);
  519. glReadPixels((GLint)dst.getLeft(), (GLint)dst.getTop(),
  520. (GLsizei)dst.getWidth(), (GLsizei)dst.getHeight(),
  521. format, type, dst.getData());
  522. // restore default alignment
  523. glPixelStorei(GL_PACK_ALIGNMENT, 4);
  524. //vertical flip
  525. {
  526. size_t rowSpan = dst.getWidth() * PixelUtil::getNumElemBytes(dst.getFormat());
  527. size_t height = dst.getHeight();
  528. UINT8 *tmpData = (UINT8*)cm_alloc<ScratchAlloc>((UINT32)(rowSpan * height));
  529. UINT8 *srcRow = (UINT8 *)dst.getData(), *tmpRow = tmpData + (height - 1) * rowSpan;
  530. while (tmpRow >= tmpData)
  531. {
  532. memcpy(tmpRow, srcRow, rowSpan);
  533. srcRow += rowSpan;
  534. tmpRow -= rowSpan;
  535. }
  536. memcpy(dst.getData(), tmpData, rowSpan * height);
  537. cm_free<ScratchAlloc>(tmpData);
  538. }
  539. }
  540. Int2 Win32Window::screenToWindowPos(const Int2& screenPos) const
  541. {
  542. POINT pos;
  543. pos.x = screenPos.x;
  544. pos.y = screenPos.y;
  545. ScreenToClient(mHWnd, &pos);
  546. return Int2(pos.x, pos.y);
  547. }
  548. Int2 Win32Window::windowToScreenPos(const Int2& windowPos) const
  549. {
  550. POINT pos;
  551. pos.x = windowPos.x;
  552. pos.y = windowPos.y;
  553. ClientToScreen(mHWnd, &pos);
  554. return Int2(pos.x, pos.y);
  555. }
  556. void Win32Window::getCustomAttribute( const String& name, void* pData ) const
  557. {
  558. if( name == "GLCONTEXT" ) {
  559. *static_cast<GLContext**>(pData) = mContext;
  560. return;
  561. } else if( name == "WINDOW" )
  562. {
  563. HWND *pHwnd = (HWND*)pData;
  564. *pHwnd = _getWindowHandle();
  565. return;
  566. }
  567. }
  568. void Win32Window::setActive( bool state )
  569. {
  570. THROW_IF_NOT_CORE_THREAD;
  571. if (mDeviceName != NULL && state == false)
  572. {
  573. HWND hActiveWindow = GetActiveWindow();
  574. char classNameSrc[_MAX_CLASS_NAME_ + 1];
  575. char classNameDst[_MAX_CLASS_NAME_ + 1];
  576. GetClassName(mHWnd, classNameSrc, _MAX_CLASS_NAME_);
  577. GetClassName(hActiveWindow, classNameDst, _MAX_CLASS_NAME_);
  578. if (strcmp(classNameDst, classNameSrc) == 0)
  579. {
  580. state = true;
  581. }
  582. }
  583. mActive = state;
  584. if( mIsFullScreen )
  585. {
  586. if( state == false )
  587. { //Restore Desktop
  588. ChangeDisplaySettingsEx(mDeviceName, NULL, NULL, 0, NULL);
  589. ShowWindow(mHWnd, SW_SHOWMINNOACTIVE);
  590. }
  591. else
  592. { //Restore App
  593. ShowWindow(mHWnd, SW_SHOWNORMAL);
  594. DEVMODE displayDeviceMode;
  595. memset(&displayDeviceMode, 0, sizeof(displayDeviceMode));
  596. displayDeviceMode.dmSize = sizeof(DEVMODE);
  597. displayDeviceMode.dmBitsPerPel = mColorDepth;
  598. displayDeviceMode.dmPelsWidth = mWidth;
  599. displayDeviceMode.dmPelsHeight = mHeight;
  600. displayDeviceMode.dmFields = DM_BITSPERPEL | DM_PELSWIDTH | DM_PELSHEIGHT;
  601. if (mDisplayFrequency)
  602. {
  603. displayDeviceMode.dmDisplayFrequency = mDisplayFrequency;
  604. displayDeviceMode.dmFields |= DM_DISPLAYFREQUENCY;
  605. }
  606. ChangeDisplaySettingsEx(mDeviceName, &displayDeviceMode, NULL, CDS_FULLSCREEN, NULL);
  607. }
  608. }
  609. }
  610. void Win32Window::startResize(WindowResizeDirection direction)
  611. {
  612. WPARAM dir = HTLEFT;
  613. switch(direction)
  614. {
  615. case WindowResizeDirection::Left:
  616. dir = HTLEFT;
  617. break;
  618. case WindowResizeDirection::TopLeft:
  619. dir = HTTOPLEFT;
  620. break;
  621. case WindowResizeDirection::Top:
  622. dir = HTTOP;
  623. break;
  624. case WindowResizeDirection::TopRight:
  625. dir = HTTOPRIGHT;
  626. break;
  627. case WindowResizeDirection::Right:
  628. dir = HTRIGHT;
  629. break;
  630. case WindowResizeDirection::BottomRight:
  631. dir = HTBOTTOMRIGHT;
  632. break;
  633. case WindowResizeDirection::Bottom:
  634. dir = HTBOTTOM;
  635. break;
  636. case WindowResizeDirection::BottomLeft:
  637. dir = HTBOTTOMLEFT;
  638. break;
  639. }
  640. SendMessage(mHWnd, WM_NCLBUTTONDOWN, dir, 0);
  641. HACK_SendLMBUpEvent();
  642. }
  643. void Win32Window::endResize()
  644. {
  645. }
  646. void Win32Window::startMove()
  647. {
  648. SendMessage(mHWnd, WM_NCLBUTTONDOWN, HTCAPTION, 0);
  649. HACK_SendLMBUpEvent();
  650. }
  651. void Win32Window::endMove()
  652. {
  653. }
  654. void Win32Window::_windowMovedOrResized()
  655. {
  656. if (!mHWnd || IsIconic(mHWnd))
  657. return;
  658. RECT rc;
  659. // top and left represent outer window position
  660. GetWindowRect(mHWnd, &rc);
  661. mTop = rc.top;
  662. mLeft = rc.left;
  663. // width and height represent drawable area only
  664. GetClientRect(mHWnd, &rc);
  665. mWidth = rc.right - rc.left;
  666. mHeight = rc.bottom - rc.top;
  667. RenderWindow::_windowMovedOrResized();
  668. }
  669. void Win32Window::_adjustWindow(unsigned int clientWidth, unsigned int clientHeight,
  670. unsigned int* winWidth, unsigned int* winHeight)
  671. {
  672. // NB only call this for non full screen
  673. RECT rc;
  674. SetRect(&rc, 0, 0, clientWidth, clientHeight);
  675. AdjustWindowRect(&rc, WS_VISIBLE | WS_CLIPCHILDREN | WS_OVERLAPPEDWINDOW, false);
  676. *winWidth = rc.right - rc.left;
  677. *winHeight = rc.bottom - rc.top;
  678. // adjust to monitor
  679. HMONITOR hMonitor = MonitorFromWindow(mHWnd, MONITOR_DEFAULTTONEAREST);
  680. // Get monitor info
  681. MONITORINFO monitorInfo;
  682. memset(&monitorInfo, 0, sizeof(MONITORINFO));
  683. monitorInfo.cbSize = sizeof(MONITORINFO);
  684. GetMonitorInfo(hMonitor, &monitorInfo);
  685. LONG maxW = monitorInfo.rcWork.right - monitorInfo.rcWork.left;
  686. LONG maxH = monitorInfo.rcWork.bottom - monitorInfo.rcWork.top;
  687. if (*winWidth > (unsigned int)maxW)
  688. *winWidth = maxW;
  689. if (*winHeight > (unsigned int)maxH)
  690. *winHeight = maxH;
  691. }
  692. }