win32Window.cpp 34 KB

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