win32Window.cpp 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include <windows.h>
  23. #include <tchar.h>
  24. #include <winuser.h>
  25. #include "math/mMath.h"
  26. #include "gfx/gfxStructs.h"
  27. #include "windowManager/win32/win32Window.h"
  28. #include "windowManager/win32/win32WindowMgr.h"
  29. #include "windowManager/win32/win32CursorController.h"
  30. #include "windowManager/win32/winDispatch.h"
  31. #include "platform/menus/popupMenu.h"
  32. #include "platform/platformInput.h"
  33. // for winState structure
  34. #include "platformWin32/platformWin32.h"
  35. #include <d3d9types.h>
  36. #include "gfx/gfxDevice.h"
  37. #include <zmouse.h>
  38. const UTF16* _MainWindowClassName = L"TorqueJuggernaughtWindow";
  39. const UTF16* _CurtainWindowClassName = L"TorqueJuggernaughtCurtainWindow";
  40. #define SCREENSAVER_QUERY_DENY 0 // Disable screensaver
  41. #ifndef IDI_ICON1
  42. #define IDI_ICON1 107
  43. #endif
  44. static bool isScreenSaverRunning()
  45. {
  46. #ifndef SPI_GETSCREENSAVERRUNNING
  47. #define SPI_GETSCREENSAVERRUNNING 114
  48. #endif
  49. // Windows 2K, and higher. It might be better to hook into
  50. // the broadcast WM_SETTINGCHANGE message instead of polling for
  51. // the screen saver status.
  52. BOOL sreensaver = false;
  53. SystemParametersInfo(SPI_GETSCREENSAVERRUNNING,0,&sreensaver,0);
  54. return sreensaver;
  55. }
  56. DISPLAY_DEVICE GetPrimaryDevice()
  57. {
  58. int index = 0;
  59. DISPLAY_DEVICE dd;
  60. dd.cb = sizeof(DISPLAY_DEVICE);
  61. while (EnumDisplayDevices(NULL, index++, &dd, 0))
  62. {
  63. if (dd.StateFlags & DISPLAY_DEVICE_PRIMARY_DEVICE) return dd;
  64. }
  65. return dd;
  66. }
  67. Win32Window::Win32Window(): mMouseLockPosition(0,0),
  68. mShouldLockMouse(false),
  69. mMouseLocked(false),
  70. mOwningManager(NULL),
  71. mNextWindow(NULL),
  72. mWindowHandle(NULL),
  73. mOldParent(NULL),
  74. mTarget(NULL),
  75. mDevice(NULL),
  76. mAccelHandle(NULL),
  77. mSuppressReset(false),
  78. mMenuHandle(NULL),
  79. mWindowedWindowStyle(0),
  80. mPosition(0,0),
  81. mFullscreen(false)
  82. {
  83. mCursorController = new Win32CursorController( this );
  84. mVideoMode.bitDepth = 32;
  85. mVideoMode.fullScreen = false;
  86. mVideoMode.refreshRate = 60;
  87. mVideoMode.resolution.set(800,600);
  88. _registerWindowClass();
  89. }
  90. Win32Window::~Win32Window()
  91. {
  92. if(mAccelHandle)
  93. {
  94. DestroyAcceleratorTable(mAccelHandle);
  95. mAccelHandle = NULL;
  96. }
  97. // delete our win handle..
  98. DestroyWindow(mWindowHandle);
  99. // unlink ourselves from the window list...
  100. AssertFatal(mOwningManager, "Win32Window::~Win32Window - orphan window, cannot unlink!");
  101. mOwningManager->unlinkWindow(this);
  102. _unregisterWindowClass();
  103. }
  104. GFXDevice * Win32Window::getGFXDevice()
  105. {
  106. return mDevice;
  107. }
  108. GFXWindowTarget * Win32Window::getGFXTarget()
  109. {
  110. return mTarget;
  111. }
  112. const GFXVideoMode & Win32Window::getVideoMode()
  113. {
  114. return mVideoMode;
  115. }
  116. void Win32Window::setVideoMode( const GFXVideoMode &mode )
  117. {
  118. bool needCurtain = (mVideoMode.fullScreen != mode.fullScreen);
  119. if(needCurtain)
  120. {
  121. Con::errorf("Win32Window::setVideoMode - invoking curtain");
  122. mOwningManager->lowerCurtain();
  123. }
  124. mVideoMode = mode;
  125. mSuppressReset = true;
  126. // Can't switch to fullscreen while a child of another window
  127. if(mode.fullScreen && !Platform::getWebDeployment() && mOwningManager->getParentWindow())
  128. {
  129. mOldParent = (HWND)mOwningManager->getParentWindow();
  130. mOwningManager->setParentWindow(NULL);
  131. }
  132. else if(!mode.fullScreen && mOldParent)
  133. {
  134. mOwningManager->setParentWindow(mOldParent);
  135. mOldParent = NULL;
  136. }
  137. // Set our window to have the right style based on the mode
  138. if(mode.fullScreen && !Platform::getWebDeployment() && !mOffscreenRender)
  139. {
  140. WINDOWPLACEMENT wplacement = { sizeof(wplacement) };
  141. DWORD dwStyle = GetWindowLong(getHWND(), GWL_STYLE);
  142. MONITORINFO mi = { sizeof(mi) };
  143. if (GetWindowPlacement(getHWND(), &wplacement) && GetMonitorInfo(MonitorFromWindow(getHWND(), MONITOR_DEFAULTTOPRIMARY), &mi))
  144. {
  145. DISPLAY_DEVICE dd = GetPrimaryDevice();
  146. DEVMODE dv;
  147. ZeroMemory(&dv, sizeof(dv));
  148. dv.dmSize = sizeof(DEVMODE);
  149. EnumDisplaySettings(dd.DeviceName, ENUM_CURRENT_SETTINGS, &dv);
  150. dv.dmPelsWidth = mode.resolution.x;
  151. dv.dmPelsHeight = mode.resolution.y;
  152. dv.dmBitsPerPel = mode.bitDepth;
  153. dv.dmDisplayFrequency = mode.refreshRate;
  154. dv.dmFields = (DM_PELSWIDTH | DM_PELSHEIGHT);
  155. ChangeDisplaySettings(&dv, CDS_FULLSCREEN);
  156. SetWindowLong(getHWND(), GWL_STYLE, dwStyle & ~WS_OVERLAPPEDWINDOW);
  157. SetWindowPos(getHWND(), HWND_TOP,
  158. mi.rcMonitor.left,
  159. mi.rcMonitor.top,
  160. mi.rcMonitor.right - mi.rcMonitor.left,
  161. mi.rcMonitor.bottom - mi.rcMonitor.top,
  162. SWP_NOOWNERZORDER | SWP_FRAMECHANGED);
  163. }
  164. if(mDisplayWindow)
  165. ShowWindow(getHWND(), SW_SHOWNORMAL);
  166. // Clear the menu bar from the window for full screen
  167. HMENU menu = GetMenu(getHWND());
  168. if(menu)
  169. {
  170. SetMenu(getHWND(), NULL);
  171. }
  172. // When switching to Fullscreen, reset device after setting style
  173. if(mTarget.isValid())
  174. mTarget->resetMode();
  175. mFullscreen = true;
  176. }
  177. else
  178. {
  179. ChangeDisplaySettings(NULL, 0);
  180. // Reset device *first*, so that when we call setSize() and let it
  181. // access the monitor settings, it won't end up with our fullscreen
  182. // geometry that is just about to change.
  183. if(mTarget.isValid())
  184. mTarget->resetMode();
  185. if (!mOffscreenRender)
  186. {
  187. SetWindowLong( getHWND(), GWL_STYLE, mWindowedWindowStyle);
  188. SetWindowPos( getHWND(), HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_FRAMECHANGED);
  189. // Put back the menu bar, if any
  190. if(mMenuHandle)
  191. {
  192. SetMenu(getHWND(), mMenuHandle);
  193. }
  194. }
  195. // Make sure we're the correct resolution for web deployment
  196. if (!Platform::getWebDeployment() || !mOwningManager->getParentWindow() || mOffscreenRender)
  197. {
  198. setSize(mode.resolution);
  199. }
  200. else
  201. {
  202. HWND parentWin = (HWND)mOwningManager->getParentWindow();
  203. RECT windowRect;
  204. GetClientRect(parentWin, &windowRect);
  205. Point2I res(windowRect.right-windowRect.left, windowRect.bottom-windowRect.top);
  206. if (res.x == 0 || res.y == 0)
  207. {
  208. // Must be too early in the window set up to obtain the parent's size.
  209. setSize(mode.resolution);
  210. }
  211. else
  212. {
  213. setSize(res);
  214. }
  215. }
  216. if (!mOffscreenRender)
  217. {
  218. // We have to force Win32 to update the window frame and make the window
  219. // visible and no longer topmost - this code might be possible to simplify.
  220. SetWindowPos( getHWND(), HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_FRAMECHANGED);
  221. if(mDisplayWindow)
  222. ShowWindow( getHWND(), SW_SHOWNORMAL);
  223. }
  224. mFullscreen = false;
  225. }
  226. mSuppressReset = false;
  227. if(needCurtain)
  228. mOwningManager->raiseCurtain();
  229. SetForegroundWindow(getHWND());
  230. }
  231. bool Win32Window::clearFullscreen()
  232. {
  233. return true;
  234. }
  235. bool Win32Window::isFullscreen()
  236. {
  237. return mFullscreen;
  238. }
  239. void Win32Window::_setFullscreen(const bool fullscreen)
  240. {
  241. if (fullscreen == mFullscreen)
  242. return;
  243. mFullscreen = fullscreen;
  244. if(fullscreen && !mOffscreenRender)
  245. {
  246. Con::printf("Win32Window::setFullscreen (full) enter");
  247. SetWindowLong( getHWND(), GWL_STYLE, WS_POPUP|WS_SYSMENU );
  248. SetWindowPos( getHWND(), HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_FRAMECHANGED);
  249. }
  250. else
  251. {
  252. Con::printf("Win32Window::setFullscreen (windowed) enter");
  253. if (!mOffscreenRender)
  254. {
  255. SetWindowLong( getHWND(), GWL_STYLE, mWindowedWindowStyle);
  256. SetWindowPos( getHWND(), HWND_NOTOPMOST, 0, 0, mVideoMode.resolution.x, mVideoMode.resolution.y, SWP_FRAMECHANGED | SWP_SHOWWINDOW);
  257. }
  258. setSize(mVideoMode.resolution);
  259. }
  260. Con::printf("Win32Window::setFullscreen exit");
  261. }
  262. bool Win32Window::setCaption( const char *cap )
  263. {
  264. return SetWindowTextA(mWindowHandle, cap);
  265. }
  266. const char * Win32Window::getCaption()
  267. {
  268. char buff[512];
  269. S32 strLen = GetWindowTextA(mWindowHandle, buff, 512);
  270. if(strLen==0)
  271. return NULL;
  272. return StringTable->insert(buff);
  273. }
  274. void Win32Window::setFocus()
  275. {
  276. ::SetFocus( mWindowHandle );
  277. }
  278. void Win32Window::setClientExtent( const Point2I newExtent )
  279. {
  280. Point2I oldExtent = getClientExtent();
  281. if (oldExtent == newExtent)
  282. return;
  283. RECT rtClient;
  284. DWORD Style, ExStyle;
  285. SetRect( &rtClient, 0, 0, newExtent.x, newExtent.y );
  286. Style = GetWindowLong( mWindowHandle, GWL_STYLE);
  287. ExStyle = GetWindowLong( mWindowHandle, GWL_EXSTYLE );
  288. AdjustWindowRectEx( &rtClient, Style, getMenuHandle() != NULL, ExStyle );
  289. if( Style & WS_VSCROLL )
  290. rtClient.right += GetSystemMetrics( SM_CXVSCROLL );
  291. if( Style & WS_HSCROLL )
  292. rtClient.bottom += GetSystemMetrics( SM_CYVSCROLL );
  293. SetWindowPos( mWindowHandle, NULL, 0, 0, rtClient.right - rtClient.left, rtClient.bottom - rtClient.top, SWP_NOMOVE | SWP_NOACTIVATE | SWP_NOZORDER);
  294. }
  295. const Point2I Win32Window::getClientExtent()
  296. {
  297. // Fetch Client Rect from Windows
  298. RECT clientRect;
  299. ::GetClientRect(mWindowHandle, &clientRect);
  300. // Return as a Torque Point2I - We don't care about origin as it's always 0,0
  301. return Point2I(clientRect.right - clientRect.left, clientRect.bottom - clientRect.top);
  302. }
  303. void Win32Window::setBounds( const RectI &newBounds )
  304. {
  305. RECT newRect;
  306. newRect.left = newBounds.point.x;
  307. newRect.top = newBounds.point.y;
  308. newRect.bottom = newRect.top + newBounds.extent.y;
  309. newRect.right = newRect.left + newBounds.extent.x;
  310. MoveWindow(mWindowHandle, newRect.left, newRect.top, newRect.right - newRect.left, newRect.bottom - newRect.top, true);
  311. }
  312. const RectI Win32Window::getBounds() const
  313. {
  314. // Fetch Window Rect from OS
  315. RECT windowRect;
  316. ::GetWindowRect(mWindowHandle, &windowRect);
  317. // Return as a Torque RectI
  318. return RectI(windowRect.left,windowRect.top,windowRect.right - windowRect.left, windowRect.bottom - windowRect.top);
  319. }
  320. void Win32Window::setPosition( const Point2I newPosition )
  321. {
  322. SetWindowPos( mWindowHandle, HWND_NOTOPMOST, newPosition.x, newPosition.y, 0, 0, SWP_NOSIZE | SWP_NOACTIVATE );
  323. }
  324. const Point2I Win32Window::getPosition()
  325. {
  326. RECT windowRect;
  327. GetWindowRect( mWindowHandle, &windowRect );
  328. // Return position
  329. return Point2I(windowRect.left,windowRect.top);
  330. }
  331. Point2I Win32Window::clientToScreen( const Point2I& pos )
  332. {
  333. POINT p = { pos.x, pos.y };
  334. ClientToScreen( mWindowHandle, &p );
  335. return Point2I( p.x, p.y );
  336. }
  337. Point2I Win32Window::screenToClient( const Point2I& pos )
  338. {
  339. POINT p = { pos.x, pos.y };
  340. ScreenToClient( mWindowHandle, &p );
  341. return Point2I( p.x, p.y );
  342. }
  343. void Win32Window::centerWindow()
  344. {
  345. RECT newRect;
  346. GetWindowRect(mWindowHandle,&newRect);
  347. newRect.bottom -= newRect.top;
  348. newRect.right -= newRect.left;
  349. newRect.top = 0;
  350. newRect.left = 0;
  351. HMONITOR hMon = MonitorFromWindow(mWindowHandle, MONITOR_DEFAULTTONEAREST);
  352. // Get the monitor's extents.
  353. MONITORINFO monInfo;
  354. dMemset(&monInfo, 0, sizeof MONITORINFO);
  355. monInfo.cbSize = sizeof MONITORINFO;
  356. GetMonitorInfo(hMon, &monInfo);
  357. // Calculate the offset to center the window in the working area
  358. S32 deltaX = ((monInfo.rcWork.right - monInfo.rcWork.left) / 2) - ((newRect.right - newRect.left) / 2);
  359. S32 deltaY = ((monInfo.rcWork.bottom - monInfo.rcWork.top) / 2) - ((newRect.bottom - newRect.top) / 2);
  360. // Calculate the new left and top position for the window
  361. S32 newLeft = newRect.left + deltaX;
  362. S32 newTop = newRect.top + deltaY;
  363. // Clamp these to be greater than 0 so that the top left corner is never offscreen
  364. newLeft = mClamp(newLeft, 0, newLeft);
  365. newTop = mClamp(newLeft, 0, newTop);
  366. // Calculate the new width and height
  367. S32 newWidth = newRect.right - newRect.left;
  368. S32 newHeight = newRect.bottom - newRect.top;
  369. // If the new width and height of the window is larger
  370. // than the working area of the monitor but is smaller
  371. // than the monitor size then have it max out at the
  372. // working area so that it will remain uncovered. We
  373. // leave it alone if it is bigger than the monitor size
  374. // (with a small fudge) to support multiple monitors.
  375. if (newLeft + newWidth > (monInfo.rcWork.right - monInfo.rcWork.left) &&
  376. newLeft + newWidth <= (monInfo.rcMonitor.right - monInfo.rcMonitor.left) + 4)
  377. newWidth = (monInfo.rcWork.right - monInfo.rcWork.left) - newLeft;
  378. if (newTop + newHeight > (monInfo.rcWork.bottom - monInfo.rcWork.top) &&
  379. newTop + newHeight <= (monInfo.rcMonitor.bottom - monInfo.rcMonitor.top) + 4)
  380. newHeight = (monInfo.rcWork.bottom - monInfo.rcWork.top) - newTop;
  381. MoveWindow( mWindowHandle, newLeft, newTop, newWidth, newHeight, true );
  382. // Make sure the resolution matches the client extent
  383. Point2I clientExt = getClientExtent();
  384. mVideoMode.resolution.set( clientExt.x, clientExt.y );
  385. // Let GFX get an update about the new resolution
  386. if (mTarget.isValid())
  387. mTarget->resetMode();
  388. }
  389. bool Win32Window::setSize( const Point2I &newSize )
  390. {
  391. // Create the window rect (screen centered if not owned by a parent)
  392. RECT newRect;
  393. newRect.left = 0;
  394. newRect.top = 0;
  395. newRect.bottom = newRect.top + newSize.y;
  396. newRect.right = newRect.left + newSize.x;
  397. // Adjust the window rect to ensure the client rectangle is the desired resolution
  398. AdjustWindowRect( &newRect, mWindowedWindowStyle, false);//(bool)(getMenuHandle() != NULL) );
  399. // Center the window on the screen if we're not a child
  400. if( !mOwningManager->mParentWindow )
  401. {
  402. HMONITOR hMon = MonitorFromWindow(mWindowHandle, MONITOR_DEFAULTTONEAREST);
  403. // Get the monitor's extents.
  404. MONITORINFO monInfo;
  405. dMemset(&monInfo, 0, sizeof MONITORINFO);
  406. monInfo.cbSize = sizeof MONITORINFO;
  407. GetMonitorInfo(hMon, &monInfo);
  408. // Calculate the offset to center the window in the working area
  409. S32 deltaX = ((monInfo.rcWork.right - monInfo.rcWork.left) / 2) - ((newRect.right - newRect.left) / 2);
  410. S32 deltaY = ((monInfo.rcWork.bottom - monInfo.rcWork.top) / 2) - ((newRect.bottom - newRect.top) / 2);
  411. // Calculate the new left and top position for the window
  412. S32 newLeft = newRect.left + deltaX;
  413. S32 newTop = newRect.top + deltaY;
  414. // Clamp these to be greater than 0 so that the top left corner is never offscreen
  415. newLeft = mClamp(newLeft, 0, newLeft);
  416. newTop = mClamp(newLeft, 0, newTop);
  417. // Calculate the new width and height
  418. S32 newWidth = newRect.right - newRect.left;
  419. S32 newHeight = newRect.bottom - newRect.top;
  420. // If the new width and height of the window is larger
  421. // than the working area of the monitor but is smaller
  422. // than the monitor size then have it max out at the
  423. // working area so that it will remain uncovered. We
  424. // leave it alone if it is bigger than the monitor size
  425. // (with a small fudge) to support multiple monitors.
  426. if (newLeft + newWidth > (monInfo.rcWork.right - monInfo.rcWork.left) &&
  427. newLeft + newWidth <= (monInfo.rcMonitor.right - monInfo.rcMonitor.left) + 4)
  428. newWidth = (monInfo.rcWork.right - monInfo.rcWork.left) - newLeft;
  429. if (newTop + newHeight > (monInfo.rcWork.bottom - monInfo.rcWork.top) &&
  430. newTop + newHeight <= (monInfo.rcMonitor.bottom - monInfo.rcMonitor.top) + 4)
  431. newHeight = (monInfo.rcWork.bottom - monInfo.rcWork.top) - newTop;
  432. MoveWindow( mWindowHandle, newLeft, newTop, newWidth, newHeight, true );
  433. }
  434. else // Just position it according to the mPosition plus new extent
  435. MoveWindow(mWindowHandle, newRect.left, newRect.top, newRect.right - newRect.left, newRect.bottom - newRect.top, true);
  436. // Make sure the resolution matches the client extent
  437. Point2I clientExt = getClientExtent();
  438. mVideoMode.resolution.set( clientExt.x, clientExt.y );
  439. // Let GFX get an update about the new resolution
  440. if (mTarget.isValid())
  441. mTarget->resetMode();
  442. InvalidateRect( NULL, NULL, true );
  443. return true;
  444. }
  445. bool Win32Window::isOpen()
  446. {
  447. return true;
  448. }
  449. bool Win32Window::isVisible()
  450. {
  451. // Is the window open and visible, ie. not minimized?
  452. if(!mWindowHandle)
  453. return false;
  454. if (mOffscreenRender)
  455. return true;
  456. return IsWindowVisible(mWindowHandle)
  457. && !IsIconic(mWindowHandle)
  458. && !isScreenSaverRunning();
  459. }
  460. bool Win32Window::isFocused()
  461. {
  462. if (mOffscreenRender)
  463. return true;
  464. // CodeReview This is enough to make the plugin and normal/editor scenarios
  465. // coexist but it seems brittle. I think we need a better way to detect
  466. // if we're the foreground window, maybe taking into account if any of our
  467. // window's parents are foreground? [bjg 4/30/07]
  468. if(mOwningManager->mParentWindow)
  469. return (GetFocus() == mWindowHandle || IsChild(mWindowHandle, GetFocus()));
  470. else
  471. return ((GetFocus() == mWindowHandle || IsChild(mWindowHandle, GetFocus())) && GetForegroundWindow() == mWindowHandle);
  472. }
  473. bool Win32Window::isMinimized()
  474. {
  475. if (mOffscreenRender)
  476. return false;
  477. WINDOWPLACEMENT wd;
  478. if ( GetWindowPlacement( mWindowHandle, &wd ) )
  479. {
  480. return ( wd.showCmd == SW_SHOWMINIMIZED );
  481. }
  482. return false;
  483. }
  484. bool Win32Window::isMaximized()
  485. {
  486. if (mOffscreenRender)
  487. return true;
  488. WINDOWPLACEMENT wd;
  489. if ( GetWindowPlacement( mWindowHandle, &wd ) )
  490. {
  491. return ( wd.showCmd == SW_SHOWMAXIMIZED );
  492. }
  493. return false;
  494. }
  495. WindowId Win32Window::getWindowId()
  496. {
  497. return mWindowId;
  498. }
  499. void Win32Window::minimize()
  500. {
  501. if (mOffscreenRender)
  502. return;
  503. ShowWindow( mWindowHandle, SW_MINIMIZE );
  504. }
  505. void Win32Window::maximize()
  506. {
  507. if (mOffscreenRender)
  508. return;
  509. ShowWindow( mWindowHandle, SW_MAXIMIZE );
  510. }
  511. void Win32Window::restore()
  512. {
  513. if (mOffscreenRender)
  514. return;
  515. ShowWindow( mWindowHandle, SW_RESTORE );
  516. }
  517. void Win32Window::hide()
  518. {
  519. if (mOffscreenRender)
  520. return;
  521. ShowWindow( mWindowHandle, SW_HIDE );
  522. }
  523. void Win32Window::show()
  524. {
  525. if (mOffscreenRender)
  526. return;
  527. ShowWindow( mWindowHandle, SW_SHOWNORMAL );
  528. }
  529. void Win32Window::close()
  530. {
  531. delete this;
  532. }
  533. void Win32Window::_registerWindowClass()
  534. {
  535. // Check to see if it exists already.
  536. WNDCLASSEX classInfo;
  537. if (GetClassInfoEx(GetModuleHandle(NULL),_MainWindowClassName,&classInfo))
  538. return;
  539. HMODULE appInstance = GetModuleHandle(NULL);
  540. HICON appIcon = LoadIcon(appInstance, MAKEINTRESOURCE(IDI_ICON1));
  541. // Window class shared by all MainWindow objects
  542. classInfo.lpszClassName = _MainWindowClassName;
  543. classInfo.cbSize = sizeof(WNDCLASSEX);
  544. classInfo.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
  545. classInfo.lpfnWndProc = (WNDPROC)WindowProc;
  546. classInfo.hInstance = appInstance; // Owner of this class
  547. classInfo.hIcon = appIcon; // Icon name
  548. classInfo.hIconSm = appIcon; // Icon name
  549. classInfo.hCursor = LoadCursor(NULL, IDC_ARROW); // Cursor
  550. classInfo.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); // Default color
  551. classInfo.lpszMenuName = NULL;
  552. classInfo.cbClsExtra = 0;
  553. classInfo.cbWndExtra = 0;
  554. if (!RegisterClassEx(&classInfo))
  555. AssertISV(false,"Window class initialization failed");
  556. classInfo.lpfnWndProc = DefWindowProc;
  557. classInfo.hCursor = NULL;
  558. classInfo.hbrBackground = (HBRUSH) GetStockObject(BLACK_BRUSH);
  559. classInfo.lpszClassName = _CurtainWindowClassName;
  560. if (!RegisterClassEx(&classInfo))
  561. AssertISV(false,"Curtain window class initialization failed");
  562. }
  563. void Win32Window::_unregisterWindowClass()
  564. {
  565. WNDCLASSEX classInfo;
  566. if (GetClassInfoEx(GetModuleHandle(NULL),_MainWindowClassName,&classInfo))
  567. UnregisterClass(_MainWindowClassName,GetModuleHandle(NULL));
  568. if (GetClassInfoEx(GetModuleHandle(NULL),_CurtainWindowClassName,&classInfo))
  569. UnregisterClass(_CurtainWindowClassName,GetModuleHandle(NULL));
  570. }
  571. LRESULT PASCAL Win32Window::WindowProc( HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam )
  572. {
  573. // CodeReview [tom, 4/30/2007] The two casts here seem somewhat silly and redundant ?
  574. Win32Window* window = (Win32Window*)((PlatformWindow*)GetWindowLongPtr(hWnd, GWLP_USERDATA));
  575. const WindowId devId = window ? window->getWindowId() : 0;
  576. if (window && window->getOffscreenRender())
  577. return DefWindowProc(hWnd, message, wParam, lParam);
  578. switch (message)
  579. {
  580. case WM_DISPLAYCHANGE:
  581. // Update the monitor list
  582. PlatformWindowManager::get()->buildMonitorsList();
  583. if(window && window->isVisible() && !window->mSuppressReset && window->getVideoMode().bitDepth != wParam)
  584. {
  585. Con::warnf("Win32Window::WindowProc - resetting device due to display mode BPP change.");
  586. window->getGFXTarget()->resetMode();
  587. }
  588. break;
  589. case WM_MOUSEACTIVATE:
  590. SetFocus(hWnd);
  591. return MA_ACTIVATE;
  592. case WM_MOUSEMOVE:
  593. if (window && GetFocus() != hWnd && IsChild(hWnd, GetFocus()))
  594. {
  595. SetFocus(hWnd);
  596. break;
  597. }
  598. // If our foreground window is the browser and we don't have focus grab it
  599. if (Platform::getWebDeployment() && GetFocus() != hWnd)
  600. {
  601. HWND phwnd = GetParent(hWnd);
  602. while (phwnd)
  603. {
  604. if (GetForegroundWindow() == phwnd)
  605. {
  606. SetFocus(hWnd);
  607. break;
  608. }
  609. phwnd = GetParent(phwnd);
  610. }
  611. }
  612. break;
  613. // Associate the window pointer with this window
  614. case WM_CREATE:
  615. // CodeReview [tom, 4/30/2007] Why don't we just cast this to a LONG
  616. // instead of having a ton of essentially pointless casts ?
  617. SetWindowLongPtr(hWnd, GWLP_USERDATA,
  618. (LONG_PTR)((PlatformWindow*)((CREATESTRUCT*)lParam)->lpCreateParams));
  619. break;
  620. case WM_SETFOCUS:
  621. // NOTE: if wParam is NOT equal to our window handle then we are GAINING focus
  622. Dispatch(DelayedDispatch, hWnd, message, wParam, lParam);
  623. return 0;
  624. case WM_KILLFOCUS:
  625. // NOTE: if wParam is NOT equal to our window handle then we are LOSING focus
  626. Dispatch(DelayedDispatch, hWnd, message, wParam, lParam);
  627. return 0;
  628. // The window is being dragged
  629. case WM_MOVE:
  630. if(!window)
  631. break;
  632. window->mPosition.x = (int)LOWORD(lParam);
  633. window->mPosition.y = (int)HIWORD(lParam);
  634. return 0;
  635. // Update viewport when the window moves
  636. case WM_SIZE:
  637. if(window && window->mSuppressReset)
  638. break;
  639. // This is dispatched immediately to prevent a race condition with journaling and window minimizing
  640. if (wParam != SIZE_MINIMIZED && !Journal::IsPlaying())
  641. Dispatch( ImmediateDispatch, hWnd,message,wParam,lParam );
  642. if(wParam != SIZE_MINIMIZED && window != NULL )
  643. {
  644. if(!window->mVideoMode.fullScreen)
  645. {
  646. U32 width = LOWORD( lParam );
  647. U32 height = HIWORD( lParam );
  648. window->mVideoMode.resolution.set( width, height );
  649. }
  650. if(window->getGFXTarget())
  651. {
  652. Con::warnf("Win32Window::WindowProc - resetting device due to window size change.");
  653. window->getGFXTarget()->resetMode();
  654. }
  655. }
  656. return 0;
  657. // Limit resize to a safe minimum
  658. case WM_GETMINMAXINFO:
  659. MINMAXINFO *winfo;
  660. winfo = (MINMAXINFO*)(lParam);
  661. if(window && window->mMinimumSize.lenSquared() > 0)
  662. {
  663. winfo->ptMinTrackSize.x = window->mMinimumSize.x;
  664. winfo->ptMinTrackSize.y = window->mMinimumSize.y;
  665. }
  666. //Is the window size locked?
  667. if (window && window->isSizeLocked())
  668. {
  669. Point2I lockedSize = window->getLockedSize();
  670. winfo->ptMinTrackSize.x = lockedSize.x;
  671. winfo->ptMinTrackSize.y = lockedSize.y;
  672. winfo->ptMaxTrackSize.x = lockedSize.x;
  673. winfo->ptMaxTrackSize.y = lockedSize.y;
  674. }
  675. break;
  676. // Override background erase so window doesn't get cleared
  677. case WM_ERASEBKGND:
  678. return 1;
  679. case WM_MENUSELECT:
  680. winState.renderThreadBlocked = true;
  681. break;
  682. // Refresh the window
  683. case WM_PAINT:
  684. // Use validate instead of begin/end paint, which seem to installs
  685. // some Dx clipping state that isn't getting restored properly
  686. ValidateRect(hWnd,0);
  687. // Skip it if we're dispatching.
  688. if(Journal::IsDispatching())
  689. break;
  690. if( window == NULL )
  691. break;
  692. //// Default render if..
  693. //// 1. We have no device
  694. //// 2. We have a device but it's not allowing rendering
  695. if( !window->getGFXDevice() || !window->getGFXDevice()->allowRender() )
  696. window->defaultRender();
  697. if( winState.renderThreadBlocked )
  698. window->displayEvent.trigger(devId);
  699. break;
  700. // Power shutdown query
  701. case WM_POWERBROADCAST: {
  702. if (wParam == PBT_APMQUERYSUSPEND)
  703. if (GetForegroundWindow() == hWnd)
  704. return BROADCAST_QUERY_DENY;
  705. break;
  706. }
  707. // Screensaver activation and monitor power requests
  708. case WM_SYSCOMMAND:
  709. switch (wParam) {
  710. case SC_SCREENSAVE:
  711. case SC_MONITORPOWER:
  712. if (GetForegroundWindow() == hWnd)
  713. return SCREENSAVER_QUERY_DENY;
  714. break;
  715. }
  716. break;
  717. // Menus
  718. case WM_COMMAND:
  719. {
  720. winState.renderThreadBlocked = false;
  721. if( window == NULL )
  722. break;
  723. // [tom, 8/21/2006] Pass off to the relevant PopupMenu if it's a menu
  724. // or accelerator command. PopupMenu will in turn hand off to script.
  725. //
  726. // Note: PopupMenu::handleSelect() will not do anything if the menu
  727. // item is disabled, so we don't need to deal with that here.
  728. S32 numItems = GetMenuItemCount(window->getMenuHandle());
  729. for(S32 i = 0;i < numItems;i++)
  730. {
  731. MENUITEMINFOA mi;
  732. mi.cbSize = sizeof(mi);
  733. mi.fMask = MIIM_DATA;
  734. if(GetMenuItemInfoA(window->getMenuHandle(), i, TRUE, &mi))
  735. {
  736. if(mi.fMask & MIIM_DATA && mi.dwItemData != 0)
  737. {
  738. PopupMenu *mnu = (PopupMenu *)mi.dwItemData;
  739. PopupMenu::smSelectionEventHandled = false;
  740. PopupMenu::smPopupMenuEvent.trigger(mnu->getPopupGUID(), LOWORD(wParam));
  741. if (PopupMenu::smSelectionEventHandled)
  742. return 0;
  743. }
  744. }
  745. }
  746. }
  747. break;
  748. case WM_INITMENUPOPUP:
  749. {
  750. HMENU menu = (HMENU)wParam;
  751. MENUINFO mi;
  752. mi.cbSize = sizeof(mi);
  753. mi.fMask = MIM_MENUDATA;
  754. if(GetMenuInfo(menu, &mi) && mi.dwMenuData != 0)
  755. {
  756. PopupMenu *pm = (PopupMenu *)mi.dwMenuData;
  757. if(pm != NULL)
  758. pm->onMenuSelect();
  759. }
  760. }
  761. break;
  762. // Some events need to be consumed as well as queued up
  763. // for later dispatch.
  764. case WM_CLOSE:
  765. case WM_MOUSEWHEEL:
  766. #ifdef WM_MOUSEHWHEEL // Vista
  767. case WM_MOUSEHWHEEL:
  768. #endif
  769. // CodeReview This fixes some issues with inappropriate event handling
  770. // around device resets and in full-screen mode but feels
  771. // heavy-handed. Is it clobbering something important?
  772. // [bjg 6/13/07]
  773. case WM_KEYUP:
  774. case WM_KEYDOWN:
  775. case WM_SYSKEYUP:
  776. case WM_SYSKEYDOWN:
  777. Dispatch(DelayedDispatch,hWnd,message,wParam,lParam);
  778. return 0;
  779. }
  780. // Queue up for later and invoke the Windows default handler.
  781. Dispatch(DelayedDispatch,hWnd,message,wParam,lParam);
  782. return DefWindowProc(hWnd, message, wParam, lParam);
  783. }
  784. void Win32Window::defaultRender()
  785. {
  786. // Get Window Device Context
  787. HDC logoDC = GetDC(mWindowHandle);
  788. // Get Window Rectangle
  789. RECT lRect;
  790. GetClientRect(mWindowHandle,&lRect);
  791. // Fill with AppWorkspace color
  792. FillRect( logoDC, &lRect, (HBRUSH)GetSysColorBrush(COLOR_APPWORKSPACE) );
  793. // Release Device Context
  794. ReleaseDC(mWindowHandle,logoDC);
  795. }
  796. //-----------------------------------------------------------------------------
  797. // Accelerators
  798. //-----------------------------------------------------------------------------
  799. void Win32Window::addAccelerator(Accelerator &accel)
  800. {
  801. ACCEL winAccel;
  802. winAccel.fVirt = FVIRTKEY;
  803. winAccel.cmd = accel.mID;
  804. if(accel.mDescriptor.flags & SI_SHIFT)
  805. winAccel.fVirt |= FSHIFT;
  806. if(accel.mDescriptor.flags & SI_CTRL)
  807. winAccel.fVirt |= FCONTROL;
  808. if(accel.mDescriptor.flags & SI_ALT)
  809. winAccel.fVirt |= FALT;
  810. winAccel.key = TranslateKeyCodeToOS(accel.mDescriptor.eventCode);
  811. for(WinAccelList::iterator i = mWinAccelList.begin();i != mWinAccelList.end();++i)
  812. {
  813. if(i->cmd == winAccel.cmd)
  814. {
  815. // Already in list, just update it
  816. i->fVirt = winAccel.fVirt;
  817. i->key = winAccel.key;
  818. return;
  819. }
  820. if(i->fVirt == winAccel.fVirt && i->key == winAccel.key)
  821. {
  822. // Existing accelerator in list, don't add this one
  823. return;
  824. }
  825. }
  826. mWinAccelList.push_back(winAccel);
  827. }
  828. void Win32Window::removeAccelerator(Accelerator &accel)
  829. {
  830. for(WinAccelList::iterator i = mWinAccelList.begin();i != mWinAccelList.end();++i)
  831. {
  832. if(i->cmd == accel.mID)
  833. {
  834. mWinAccelList.erase(i);
  835. return;
  836. }
  837. }
  838. }
  839. //-----------------------------------------------------------------------------
  840. static bool isMenuItemIDEnabled(HMENU menu, U32 id)
  841. {
  842. S32 numItems = GetMenuItemCount(menu);
  843. for(S32 i = 0;i < numItems;i++)
  844. {
  845. MENUITEMINFOA mi;
  846. mi.cbSize = sizeof(mi);
  847. mi.fMask = MIIM_ID|MIIM_STATE|MIIM_SUBMENU|MIIM_DATA;
  848. if(GetMenuItemInfoA(menu, i, TRUE, &mi))
  849. {
  850. if(mi.fMask & MIIM_ID && mi.wID == id)
  851. {
  852. // This is an item on this menu
  853. return (mi.fMask & MIIM_STATE) && ! (mi.fState & MFS_DISABLED);
  854. }
  855. if((mi.fMask & MIIM_SUBMENU) && mi.hSubMenu != 0 && (mi.fMask & MIIM_DATA) && mi.dwItemData != 0)
  856. {
  857. // This is a submenu, if it can handle this ID then recurse to find correct state
  858. PopupMenu *mnu = (PopupMenu *)mi.dwItemData;
  859. if(mnu->canHandleID(id))
  860. return isMenuItemIDEnabled(mi.hSubMenu, id);
  861. }
  862. }
  863. }
  864. return false;
  865. }
  866. bool Win32Window::isAccelerator(const InputEventInfo &info)
  867. {
  868. U32 virt;
  869. virt = FVIRTKEY;
  870. if(info.modifier & SI_SHIFT)
  871. virt |= FSHIFT;
  872. if(info.modifier & SI_CTRL)
  873. virt |= FCONTROL;
  874. if(info.modifier & SI_ALT)
  875. virt |= FALT;
  876. U8 keyCode = TranslateKeyCodeToOS(info.objInst);
  877. for(S32 i = 0;i < mWinAccelList.size();++i)
  878. {
  879. const ACCEL &accel = mWinAccelList[i];
  880. if(accel.key == keyCode && accel.fVirt == virt && isMenuItemIDEnabled(getMenuHandle(), accel.cmd))
  881. return true;
  882. }
  883. return false;
  884. }
  885. //-----------------------------------------------------------------------------
  886. void Win32Window::addAccelerators(AcceleratorList &list)
  887. {
  888. if(mAccelHandle)
  889. {
  890. DestroyAcceleratorTable(mAccelHandle);
  891. mAccelHandle = NULL;
  892. }
  893. for(AcceleratorList::iterator i = list.begin();i != list.end();++i)
  894. {
  895. addAccelerator(*i);
  896. }
  897. if(mWinAccelList.size() > 0)
  898. mAccelHandle = CreateAcceleratorTable(&mWinAccelList[0], mWinAccelList.size());
  899. }
  900. void Win32Window::removeAccelerators(AcceleratorList &list)
  901. {
  902. if(mAccelHandle)
  903. {
  904. DestroyAcceleratorTable(mAccelHandle);
  905. mAccelHandle = NULL;
  906. }
  907. for(AcceleratorList::iterator i = list.begin();i != list.end();++i)
  908. {
  909. removeAccelerator(*i);
  910. }
  911. if(mWinAccelList.size() > 0)
  912. mAccelHandle = CreateAcceleratorTable(mWinAccelList.address(), mWinAccelList.size());
  913. }
  914. bool Win32Window::translateMessage(MSG &msg)
  915. {
  916. if(mAccelHandle == NULL || mWindowHandle == NULL || !mEnableAccelerators)
  917. return false;
  918. S32 ret = TranslateAccelerator(mWindowHandle, mAccelHandle, &msg);
  919. return ret != 0;
  920. }
  921. //-----------------------------------------------------------------------------
  922. // Mouse Locking
  923. //-----------------------------------------------------------------------------
  924. void Win32Window::setMouseLocked( bool enable )
  925. {
  926. if (mOffscreenRender)
  927. return;
  928. // Maintain a good state without unnecessary
  929. // cursor hides/modifications
  930. if( enable && mMouseLocked && mShouldLockMouse )
  931. return;
  932. else if(!enable && !mMouseLocked && !mShouldLockMouse )
  933. return;
  934. // Need to be focused to enable mouse lock
  935. // but we can disable it no problem if we're
  936. // not focused
  937. if( !isFocused() && enable )
  938. {
  939. mShouldLockMouse = enable;
  940. return;
  941. }
  942. // Set Flag
  943. mMouseLocked = enable;
  944. if( enable )
  945. {
  946. getCursorPosition( mMouseLockPosition );
  947. RECT r;
  948. GetWindowRect(getHWND(), &r);
  949. // Hide the cursor before it's moved
  950. setCursorVisible( false );
  951. // We have to nudge the cursor clip rect in a bit so we don't go out
  952. // side the bounds of the window... We'll just do it by 32 in all
  953. // directions, which will break for very small windows (< 200x200 or so)
  954. // but otherwise won't matter.
  955. RECT rCopy = r;
  956. rCopy.top += 32; rCopy.bottom -= 64;
  957. rCopy.left += 32; rCopy.right -= 64;
  958. ClipCursor(&rCopy);
  959. S32 centerX = (r.right + r.left) >> 1;
  960. S32 centerY = ((r.bottom + r.top) >> 1);
  961. // Consume all existing mouse events and those posted to our own dispatch queue
  962. MSG msg;
  963. PeekMessage( &msg, 0,WM_MOUSEFIRST,WM_MOUSELAST , PM_QS_POSTMESSAGE | PM_NOYIELD | PM_REMOVE );
  964. RemoveMessages( NULL, WM_MOUSEMOVE, WM_MOUSEMOVE );
  965. // Set the CursorPos
  966. SetCursorPos(centerX, centerY);
  967. // reset should lock flag
  968. mShouldLockMouse = true;
  969. }
  970. else
  971. {
  972. // This belongs before the unlock code
  973. mShouldLockMouse = false;
  974. ClipCursor(NULL);
  975. setCursorPosition( mMouseLockPosition.x,mMouseLockPosition.y );
  976. // Consume all existing mouse events and those posted to our own dispatch queue
  977. MSG msg;
  978. PeekMessage( &msg, NULL,WM_MOUSEFIRST,WM_MOUSELAST , PM_QS_POSTMESSAGE | PM_NOYIELD | PM_REMOVE );
  979. RemoveMessages( NULL, WM_MOUSEMOVE, WM_MOUSEMOVE );
  980. // Show the Cursor
  981. setCursorVisible( true );
  982. }
  983. }
  984. const UTF16 *Win32Window::getWindowClassName()
  985. {
  986. return _MainWindowClassName;
  987. }
  988. const UTF16 *Win32Window::getCurtainWindowClassName()
  989. {
  990. return _CurtainWindowClassName;
  991. }