win32Window.cpp 33 KB

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