win32Window.cpp 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165
  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. // Update the monitor list
  546. PlatformWindowManager::get()->buildMonitorsList();
  547. if(window && window->isVisible() && !window->mSuppressReset && window->getVideoMode().bitDepth != wParam)
  548. {
  549. Con::warnf("Win32Window::WindowProc - resetting device due to display mode BPP change.");
  550. window->getGFXTarget()->resetMode();
  551. }
  552. break;
  553. case WM_MOUSEACTIVATE:
  554. SetFocus(hWnd);
  555. return MA_ACTIVATE;
  556. case WM_MOUSEMOVE:
  557. if (window && GetFocus() != hWnd && IsChild(hWnd, GetFocus()))
  558. {
  559. SetFocus(hWnd);
  560. break;
  561. }
  562. // If our foreground window is the browser and we don't have focus grab it
  563. if (Platform::getWebDeployment() && GetFocus() != hWnd)
  564. {
  565. HWND phwnd = GetParent(hWnd);
  566. while (phwnd)
  567. {
  568. if (GetForegroundWindow() == phwnd)
  569. {
  570. SetFocus(hWnd);
  571. break;
  572. }
  573. phwnd = GetParent(phwnd);
  574. }
  575. }
  576. break;
  577. // Associate the window pointer with this window
  578. case WM_CREATE:
  579. // CodeReview [tom, 4/30/2007] Why don't we just cast this to a LONG
  580. // instead of having a ton of essentially pointless casts ?
  581. SetWindowLong(hWnd, GWL_USERDATA,
  582. (LONG)((PlatformWindow*)((CREATESTRUCT*)lParam)->lpCreateParams));
  583. break;
  584. case WM_SETFOCUS:
  585. // NOTE: if wParam is NOT equal to our window handle then we are GAINING focus
  586. Dispatch(DelayedDispatch, hWnd, message, wParam, lParam);
  587. return 0;
  588. case WM_KILLFOCUS:
  589. // NOTE: if wParam is NOT equal to our window handle then we are LOSING focus
  590. Dispatch(DelayedDispatch, hWnd, message, wParam, lParam);
  591. return 0;
  592. // The window is being dragged
  593. case WM_MOVE:
  594. if(!window)
  595. break;
  596. window->mPosition.x = (int)LOWORD(lParam);
  597. window->mPosition.y = (int)HIWORD(lParam);
  598. return 0;
  599. // Update viewport when the window moves
  600. case WM_SIZE:
  601. if(window && window->mSuppressReset)
  602. break;
  603. // This is dispatched immediately to prevent a race condition with journaling and window minimizing
  604. if (wParam != SIZE_MINIMIZED && !Journal::IsPlaying())
  605. Dispatch( ImmediateDispatch, hWnd,message,wParam,lParam );
  606. if(wParam != SIZE_MINIMIZED && window != NULL )
  607. {
  608. if(!window->mVideoMode.fullScreen)
  609. {
  610. U32 width = LOWORD( lParam );
  611. U32 height = HIWORD( lParam );
  612. window->mVideoMode.resolution.set( width, height );
  613. }
  614. if(window->getGFXTarget())
  615. {
  616. Con::warnf("Win32Window::WindowProc - resetting device due to window size change.");
  617. window->getGFXTarget()->resetMode();
  618. }
  619. }
  620. return 0;
  621. // Limit resize to a safe minimum
  622. case WM_GETMINMAXINFO:
  623. MINMAXINFO *winfo;
  624. winfo = (MINMAXINFO*)(lParam);
  625. if(window && window->mMinimumSize.lenSquared() > 0)
  626. {
  627. winfo->ptMinTrackSize.x = window->mMinimumSize.x;
  628. winfo->ptMinTrackSize.y = window->mMinimumSize.y;
  629. }
  630. //Is the window size locked?
  631. if (window && window->isSizeLocked())
  632. {
  633. Point2I lockedSize = window->getLockedSize();
  634. winfo->ptMinTrackSize.x = lockedSize.x;
  635. winfo->ptMinTrackSize.y = lockedSize.y;
  636. winfo->ptMaxTrackSize.x = lockedSize.x;
  637. winfo->ptMaxTrackSize.y = lockedSize.y;
  638. }
  639. break;
  640. // Override background erase so window doesn't get cleared
  641. case WM_ERASEBKGND:
  642. return 1;
  643. case WM_MENUSELECT:
  644. winState.renderThreadBlocked = true;
  645. break;
  646. // Refresh the window
  647. case WM_PAINT:
  648. // Use validate instead of begin/end paint, which seem to installs
  649. // some Dx clipping state that isn't getting restored properly
  650. ValidateRect(hWnd,0);
  651. // Skip it if we're dispatching.
  652. if(Journal::IsDispatching())
  653. break;
  654. if( window == NULL )
  655. break;
  656. //// Default render if..
  657. //// 1. We have no device
  658. //// 2. We have a device but it's not allowing rendering
  659. if( !window->getGFXDevice() || !window->getGFXDevice()->allowRender() )
  660. window->defaultRender();
  661. if( winState.renderThreadBlocked )
  662. window->displayEvent.trigger(devId);
  663. break;
  664. // Power shutdown query
  665. case WM_POWERBROADCAST: {
  666. if (wParam == PBT_APMQUERYSUSPEND)
  667. if (GetForegroundWindow() == hWnd)
  668. return BROADCAST_QUERY_DENY;
  669. break;
  670. }
  671. // Screensaver activation and monitor power requests
  672. case WM_SYSCOMMAND:
  673. switch (wParam) {
  674. case SC_SCREENSAVE:
  675. case SC_MONITORPOWER:
  676. if (GetForegroundWindow() == hWnd)
  677. return SCREENSAVER_QUERY_DENY;
  678. break;
  679. }
  680. break;
  681. // Menus
  682. case WM_COMMAND:
  683. {
  684. winState.renderThreadBlocked = false;
  685. if( window == NULL )
  686. break;
  687. // [tom, 8/21/2006] Pass off to the relevant PopupMenu if it's a menu
  688. // or accelerator command. PopupMenu will in turn hand off to script.
  689. //
  690. // Note: PopupMenu::handleSelect() will not do anything if the menu
  691. // item is disabled, so we don't need to deal with that here.
  692. S32 numItems = GetMenuItemCount(window->getMenuHandle());
  693. for(S32 i = 0;i < numItems;i++)
  694. {
  695. MENUITEMINFOA mi;
  696. mi.cbSize = sizeof(mi);
  697. mi.fMask = MIIM_DATA;
  698. if(GetMenuItemInfoA(window->getMenuHandle(), i, TRUE, &mi))
  699. {
  700. if(mi.fMask & MIIM_DATA && mi.dwItemData != 0)
  701. {
  702. PopupMenu *mnu = (PopupMenu *)mi.dwItemData;
  703. PopupMenu::smSelectionEventHandled = false;
  704. PopupMenu::smPopupMenuEvent.trigger(mnu->getPopupGUID(), LOWORD(wParam));
  705. if (PopupMenu::smSelectionEventHandled)
  706. return 0;
  707. }
  708. }
  709. }
  710. }
  711. break;
  712. case WM_INITMENUPOPUP:
  713. {
  714. HMENU menu = (HMENU)wParam;
  715. MENUINFO mi;
  716. mi.cbSize = sizeof(mi);
  717. mi.fMask = MIM_MENUDATA;
  718. if(GetMenuInfo(menu, &mi) && mi.dwMenuData != 0)
  719. {
  720. PopupMenu *pm = (PopupMenu *)mi.dwMenuData;
  721. if(pm != NULL)
  722. pm->onMenuSelect();
  723. }
  724. }
  725. break;
  726. // Some events need to be consumed as well as queued up
  727. // for later dispatch.
  728. case WM_CLOSE:
  729. case WM_MOUSEWHEEL:
  730. #ifdef WM_MOUSEHWHEEL // Vista
  731. case WM_MOUSEHWHEEL:
  732. #endif
  733. // CodeReview This fixes some issues with inappropriate event handling
  734. // around device resets and in full-screen mode but feels
  735. // heavy-handed. Is it clobbering something important?
  736. // [bjg 6/13/07]
  737. case WM_KEYUP:
  738. case WM_KEYDOWN:
  739. case WM_SYSKEYUP:
  740. case WM_SYSKEYDOWN:
  741. Dispatch(DelayedDispatch,hWnd,message,wParam,lParam);
  742. return 0;
  743. }
  744. // Queue up for later and invoke the Windows default handler.
  745. Dispatch(DelayedDispatch,hWnd,message,wParam,lParam);
  746. return DefWindowProc(hWnd, message, wParam, lParam);
  747. }
  748. void Win32Window::defaultRender()
  749. {
  750. // Get Window Device Context
  751. HDC logoDC = GetDC(mWindowHandle);
  752. // Get Window Rectangle
  753. RECT lRect;
  754. GetClientRect(mWindowHandle,&lRect);
  755. // Fill with AppWorkspace color
  756. FillRect( logoDC, &lRect, (HBRUSH)GetSysColorBrush(COLOR_APPWORKSPACE) );
  757. // Release Device Context
  758. ReleaseDC(mWindowHandle,logoDC);
  759. }
  760. //-----------------------------------------------------------------------------
  761. // Accelerators
  762. //-----------------------------------------------------------------------------
  763. void Win32Window::addAccelerator(Accelerator &accel)
  764. {
  765. ACCEL winAccel;
  766. winAccel.fVirt = FVIRTKEY;
  767. winAccel.cmd = accel.mID;
  768. if(accel.mDescriptor.flags & SI_SHIFT)
  769. winAccel.fVirt |= FSHIFT;
  770. if(accel.mDescriptor.flags & SI_CTRL)
  771. winAccel.fVirt |= FCONTROL;
  772. if(accel.mDescriptor.flags & SI_ALT)
  773. winAccel.fVirt |= FALT;
  774. winAccel.key = TranslateKeyCodeToOS(accel.mDescriptor.eventCode);
  775. for(WinAccelList::iterator i = mWinAccelList.begin();i != mWinAccelList.end();++i)
  776. {
  777. if(i->cmd == winAccel.cmd)
  778. {
  779. // Already in list, just update it
  780. i->fVirt = winAccel.fVirt;
  781. i->key = winAccel.key;
  782. return;
  783. }
  784. if(i->fVirt == winAccel.fVirt && i->key == winAccel.key)
  785. {
  786. // Existing accelerator in list, don't add this one
  787. return;
  788. }
  789. }
  790. mWinAccelList.push_back(winAccel);
  791. }
  792. void Win32Window::removeAccelerator(Accelerator &accel)
  793. {
  794. for(WinAccelList::iterator i = mWinAccelList.begin();i != mWinAccelList.end();++i)
  795. {
  796. if(i->cmd == accel.mID)
  797. {
  798. mWinAccelList.erase(i);
  799. return;
  800. }
  801. }
  802. }
  803. //-----------------------------------------------------------------------------
  804. static bool isMenuItemIDEnabled(HMENU menu, U32 id)
  805. {
  806. S32 numItems = GetMenuItemCount(menu);
  807. for(S32 i = 0;i < numItems;i++)
  808. {
  809. MENUITEMINFOA mi;
  810. mi.cbSize = sizeof(mi);
  811. mi.fMask = MIIM_ID|MIIM_STATE|MIIM_SUBMENU|MIIM_DATA;
  812. if(GetMenuItemInfoA(menu, i, TRUE, &mi))
  813. {
  814. if(mi.fMask & MIIM_ID && mi.wID == id)
  815. {
  816. // This is an item on this menu
  817. return (mi.fMask & MIIM_STATE) && ! (mi.fState & MFS_DISABLED);
  818. }
  819. if((mi.fMask & MIIM_SUBMENU) && mi.hSubMenu != 0 && (mi.fMask & MIIM_DATA) && mi.dwItemData != 0)
  820. {
  821. // This is a submenu, if it can handle this ID then recurse to find correct state
  822. PopupMenu *mnu = (PopupMenu *)mi.dwItemData;
  823. if(mnu->canHandleID(id))
  824. return isMenuItemIDEnabled(mi.hSubMenu, id);
  825. }
  826. }
  827. }
  828. return false;
  829. }
  830. bool Win32Window::isAccelerator(const InputEventInfo &info)
  831. {
  832. U32 virt;
  833. virt = FVIRTKEY;
  834. if(info.modifier & SI_SHIFT)
  835. virt |= FSHIFT;
  836. if(info.modifier & SI_CTRL)
  837. virt |= FCONTROL;
  838. if(info.modifier & SI_ALT)
  839. virt |= FALT;
  840. U8 keyCode = TranslateKeyCodeToOS(info.objInst);
  841. for(S32 i = 0;i < mWinAccelList.size();++i)
  842. {
  843. const ACCEL &accel = mWinAccelList[i];
  844. if(accel.key == keyCode && accel.fVirt == virt && isMenuItemIDEnabled(getMenuHandle(), accel.cmd))
  845. return true;
  846. }
  847. return false;
  848. }
  849. //-----------------------------------------------------------------------------
  850. void Win32Window::addAccelerators(AcceleratorList &list)
  851. {
  852. if(mAccelHandle)
  853. {
  854. DestroyAcceleratorTable(mAccelHandle);
  855. mAccelHandle = NULL;
  856. }
  857. for(AcceleratorList::iterator i = list.begin();i != list.end();++i)
  858. {
  859. addAccelerator(*i);
  860. }
  861. if(mWinAccelList.size() > 0)
  862. mAccelHandle = CreateAcceleratorTable(&mWinAccelList[0], mWinAccelList.size());
  863. }
  864. void Win32Window::removeAccelerators(AcceleratorList &list)
  865. {
  866. if(mAccelHandle)
  867. {
  868. DestroyAcceleratorTable(mAccelHandle);
  869. mAccelHandle = NULL;
  870. }
  871. for(AcceleratorList::iterator i = list.begin();i != list.end();++i)
  872. {
  873. removeAccelerator(*i);
  874. }
  875. if(mWinAccelList.size() > 0)
  876. mAccelHandle = CreateAcceleratorTable(mWinAccelList.address(), mWinAccelList.size());
  877. }
  878. bool Win32Window::translateMessage(MSG &msg)
  879. {
  880. if(mAccelHandle == NULL || mWindowHandle == NULL || !mEnableAccelerators)
  881. return false;
  882. int ret = TranslateAccelerator(mWindowHandle, mAccelHandle, &msg);
  883. return ret != 0;
  884. }
  885. //-----------------------------------------------------------------------------
  886. // Mouse Locking
  887. //-----------------------------------------------------------------------------
  888. void Win32Window::setMouseLocked( bool enable )
  889. {
  890. if (mOffscreenRender)
  891. return;
  892. // Maintain a good state without unnecessary
  893. // cursor hides/modifications
  894. if( enable && mMouseLocked && mShouldLockMouse )
  895. return;
  896. else if(!enable && !mMouseLocked && !mShouldLockMouse )
  897. return;
  898. // Need to be focused to enable mouse lock
  899. // but we can disable it no problem if we're
  900. // not focused
  901. if( !isFocused() && enable )
  902. {
  903. mShouldLockMouse = enable;
  904. return;
  905. }
  906. // Set Flag
  907. mMouseLocked = enable;
  908. if( enable )
  909. {
  910. getCursorPosition( mMouseLockPosition );
  911. RECT r;
  912. GetWindowRect(getHWND(), &r);
  913. // Hide the cursor before it's moved
  914. setCursorVisible( false );
  915. // We have to nudge the cursor clip rect in a bit so we don't go out
  916. // side the bounds of the window... We'll just do it by 32 in all
  917. // directions, which will break for very small windows (< 200x200 or so)
  918. // but otherwise won't matter.
  919. RECT rCopy = r;
  920. rCopy.top += 32; rCopy.bottom -= 64;
  921. rCopy.left += 32; rCopy.right -= 64;
  922. ClipCursor(&rCopy);
  923. S32 centerX = (r.right + r.left) >> 1;
  924. S32 centerY = ((r.bottom + r.top) >> 1);
  925. // Consume all existing mouse events and those posted to our own dispatch queue
  926. MSG msg;
  927. PeekMessage( &msg, 0,WM_MOUSEFIRST,WM_MOUSELAST , PM_QS_POSTMESSAGE | PM_NOYIELD | PM_REMOVE );
  928. RemoveMessages( NULL, WM_MOUSEMOVE, WM_MOUSEMOVE );
  929. // Set the CursorPos
  930. SetCursorPos(centerX, centerY);
  931. // reset should lock flag
  932. mShouldLockMouse = true;
  933. }
  934. else
  935. {
  936. // This belongs before the unlock code
  937. mShouldLockMouse = false;
  938. ClipCursor(NULL);
  939. setCursorPosition( mMouseLockPosition.x,mMouseLockPosition.y );
  940. // Consume all existing mouse events and those posted to our own dispatch queue
  941. MSG msg;
  942. PeekMessage( &msg, NULL,WM_MOUSEFIRST,WM_MOUSELAST , PM_QS_POSTMESSAGE | PM_NOYIELD | PM_REMOVE );
  943. RemoveMessages( NULL, WM_MOUSEMOVE, WM_MOUSEMOVE );
  944. // Show the Cursor
  945. setCursorVisible( true );
  946. }
  947. }
  948. const UTF16 *Win32Window::getWindowClassName()
  949. {
  950. return _MainWindowClassName;
  951. }
  952. const UTF16 *Win32Window::getCurtainWindowClassName()
  953. {
  954. return _CurtainWindowClassName;
  955. }