win32Window.cpp 32 KB

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