win32Window.cpp 33 KB

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