win32Window.cpp 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218
  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. }
  235. bool Win32Window::clearFullscreen()
  236. {
  237. return true;
  238. }
  239. bool Win32Window::isFullscreen()
  240. {
  241. return mFullscreen;
  242. }
  243. void Win32Window::_setFullscreen(const bool fullscreen)
  244. {
  245. if (fullscreen == mFullscreen)
  246. return;
  247. mFullscreen = fullscreen;
  248. if(fullscreen && !mOffscreenRender)
  249. {
  250. Con::printf("Win32Window::setFullscreen (full) enter");
  251. SetWindowLong( getHWND(), GWL_STYLE, WS_POPUP|WS_SYSMENU );
  252. SetWindowPos( getHWND(), HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_FRAMECHANGED);
  253. }
  254. else
  255. {
  256. Con::printf("Win32Window::setFullscreen (windowed) enter");
  257. if (!mOffscreenRender)
  258. {
  259. SetWindowLong( getHWND(), GWL_STYLE, mWindowedWindowStyle);
  260. SetWindowPos( getHWND(), HWND_NOTOPMOST, 0, 0, mVideoMode.resolution.x, mVideoMode.resolution.y, SWP_FRAMECHANGED | SWP_SHOWWINDOW);
  261. }
  262. setSize(mVideoMode.resolution);
  263. }
  264. Con::printf("Win32Window::setFullscreen exit");
  265. }
  266. bool Win32Window::setCaption( const char *cap )
  267. {
  268. return SetWindowTextA(mWindowHandle, cap);
  269. }
  270. const char * Win32Window::getCaption()
  271. {
  272. char buff[512];
  273. S32 strLen = GetWindowTextA(mWindowHandle, buff, 512);
  274. if(strLen==0)
  275. return NULL;
  276. return StringTable->insert(buff);
  277. }
  278. void Win32Window::setFocus()
  279. {
  280. ::SetFocus( mWindowHandle );
  281. }
  282. void Win32Window::setClientExtent( const Point2I newExtent )
  283. {
  284. Point2I oldExtent = getClientExtent();
  285. if (oldExtent == newExtent)
  286. return;
  287. RECT rtClient;
  288. DWORD Style, ExStyle;
  289. SetRect( &rtClient, 0, 0, newExtent.x, newExtent.y );
  290. Style = GetWindowLong( mWindowHandle, GWL_STYLE);
  291. ExStyle = GetWindowLong( mWindowHandle, GWL_EXSTYLE );
  292. AdjustWindowRectEx( &rtClient, Style, getMenuHandle() != NULL, ExStyle );
  293. if( Style & WS_VSCROLL )
  294. rtClient.right += GetSystemMetrics( SM_CXVSCROLL );
  295. if( Style & WS_HSCROLL )
  296. rtClient.bottom += GetSystemMetrics( SM_CYVSCROLL );
  297. SetWindowPos( mWindowHandle, NULL, 0, 0, rtClient.right - rtClient.left, rtClient.bottom - rtClient.top, SWP_NOMOVE | SWP_NOACTIVATE | SWP_NOZORDER);
  298. }
  299. const Point2I Win32Window::getClientExtent()
  300. {
  301. // Fetch Client Rect from Windows
  302. RECT clientRect;
  303. ::GetClientRect(mWindowHandle, &clientRect);
  304. // Return as a Torque Point2I - We don't care about origin as it's always 0,0
  305. return Point2I(clientRect.right - clientRect.left, clientRect.bottom - clientRect.top);
  306. }
  307. void Win32Window::setBounds( const RectI &newBounds )
  308. {
  309. RECT newRect;
  310. newRect.left = newBounds.point.x;
  311. newRect.top = newBounds.point.y;
  312. newRect.bottom = newRect.top + newBounds.extent.y;
  313. newRect.right = newRect.left + newBounds.extent.x;
  314. MoveWindow(mWindowHandle, newRect.left, newRect.top, newRect.right - newRect.left, newRect.bottom - newRect.top, true);
  315. }
  316. const RectI Win32Window::getBounds() const
  317. {
  318. // Fetch Window Rect from OS
  319. RECT windowRect;
  320. ::GetWindowRect(mWindowHandle, &windowRect);
  321. // Return as a Torque RectI
  322. return RectI(windowRect.left,windowRect.top,windowRect.right - windowRect.left, windowRect.bottom - windowRect.top);
  323. }
  324. void Win32Window::setPosition( const Point2I newPosition )
  325. {
  326. SetWindowPos( mWindowHandle, HWND_NOTOPMOST, newPosition.x, newPosition.y, 0, 0, SWP_NOSIZE | SWP_NOACTIVATE );
  327. }
  328. const Point2I Win32Window::getPosition()
  329. {
  330. RECT windowRect;
  331. GetWindowRect( mWindowHandle, &windowRect );
  332. // Return position
  333. return Point2I(windowRect.left,windowRect.top);
  334. }
  335. Point2I Win32Window::clientToScreen( const Point2I& pos )
  336. {
  337. POINT p = { pos.x, pos.y };
  338. ClientToScreen( mWindowHandle, &p );
  339. return Point2I( p.x, p.y );
  340. }
  341. Point2I Win32Window::screenToClient( const Point2I& pos )
  342. {
  343. POINT p = { pos.x, pos.y };
  344. ScreenToClient( mWindowHandle, &p );
  345. return Point2I( p.x, p.y );
  346. }
  347. void Win32Window::centerWindow()
  348. {
  349. RECT newRect;
  350. GetWindowRect(mWindowHandle,&newRect);
  351. newRect.bottom -= newRect.top;
  352. newRect.right -= newRect.left;
  353. newRect.top = 0;
  354. newRect.left = 0;
  355. HMONITOR hMon = MonitorFromWindow(mWindowHandle, MONITOR_DEFAULTTONEAREST);
  356. // Get the monitor's extents.
  357. MONITORINFO monInfo;
  358. dMemset(&monInfo, 0, sizeof(MONITORINFO));
  359. monInfo.cbSize = sizeof(MONITORINFO);
  360. GetMonitorInfo(hMon, &monInfo);
  361. // Calculate the offset to center the window in the working area
  362. S32 deltaX = ((monInfo.rcWork.right - monInfo.rcWork.left) / 2) - ((newRect.right - newRect.left) / 2);
  363. S32 deltaY = ((monInfo.rcWork.bottom - monInfo.rcWork.top) / 2) - ((newRect.bottom - newRect.top) / 2);
  364. // Calculate the new left and top position for the window
  365. S32 newLeft = newRect.left + deltaX;
  366. S32 newTop = newRect.top + deltaY;
  367. // Clamp these to be greater than 0 so that the top left corner is never offscreen
  368. newLeft = mClamp(newLeft, 0, newLeft);
  369. newTop = mClamp(newLeft, 0, newTop);
  370. // Calculate the new width and height
  371. S32 newWidth = newRect.right - newRect.left;
  372. S32 newHeight = newRect.bottom - newRect.top;
  373. // If the new width and height of the window is larger
  374. // than the working area of the monitor but is smaller
  375. // than the monitor size then have it max out at the
  376. // working area so that it will remain uncovered. We
  377. // leave it alone if it is bigger than the monitor size
  378. // (with a small fudge) to support multiple monitors.
  379. if (newLeft + newWidth > (monInfo.rcWork.right - monInfo.rcWork.left) &&
  380. newLeft + newWidth <= (monInfo.rcMonitor.right - monInfo.rcMonitor.left) + 4)
  381. newWidth = (monInfo.rcWork.right - monInfo.rcWork.left) - newLeft;
  382. if (newTop + newHeight > (monInfo.rcWork.bottom - monInfo.rcWork.top) &&
  383. newTop + newHeight <= (monInfo.rcMonitor.bottom - monInfo.rcMonitor.top) + 4)
  384. newHeight = (monInfo.rcWork.bottom - monInfo.rcWork.top) - newTop;
  385. MoveWindow( mWindowHandle, newLeft, newTop, newWidth, newHeight, true );
  386. // Make sure the resolution matches the client extent
  387. Point2I clientExt = getClientExtent();
  388. mVideoMode.resolution.set( clientExt.x, clientExt.y );
  389. // Let GFX get an update about the new resolution
  390. if (mTarget.isValid())
  391. mTarget->resetMode();
  392. }
  393. bool Win32Window::setSize( const Point2I &newSize )
  394. {
  395. // Create the window rect (screen centered if not owned by a parent)
  396. RECT newRect;
  397. newRect.left = 0;
  398. newRect.top = 0;
  399. newRect.bottom = newRect.top + newSize.y;
  400. newRect.right = newRect.left + newSize.x;
  401. // Adjust the window rect to ensure the client rectangle is the desired resolution
  402. AdjustWindowRect( &newRect, mWindowedWindowStyle, false);//(bool)(getMenuHandle() != NULL) );
  403. // Center the window on the screen if we're not a child
  404. if( !mOwningManager->mParentWindow )
  405. {
  406. HMONITOR hMon = MonitorFromWindow(mWindowHandle, MONITOR_DEFAULTTONEAREST);
  407. // Get the monitor's extents.
  408. MONITORINFO monInfo;
  409. dMemset(&monInfo, 0, sizeof(MONITORINFO));
  410. monInfo.cbSize = sizeof(MONITORINFO);
  411. GetMonitorInfo(hMon, &monInfo);
  412. // Calculate the offset to center the window in the working area
  413. S32 deltaX = ((monInfo.rcWork.right - monInfo.rcWork.left) / 2) - ((newRect.right - newRect.left) / 2);
  414. S32 deltaY = ((monInfo.rcWork.bottom - monInfo.rcWork.top) / 2) - ((newRect.bottom - newRect.top) / 2);
  415. // Calculate the new left and top position for the window
  416. S32 newLeft = newRect.left + deltaX;
  417. S32 newTop = newRect.top + deltaY;
  418. // Clamp these to be greater than 0 so that the top left corner is never offscreen
  419. newLeft = mClamp(newLeft, 0, newLeft);
  420. newTop = mClamp(newLeft, 0, newTop);
  421. // Calculate the new width and height
  422. S32 newWidth = newRect.right - newRect.left;
  423. S32 newHeight = newRect.bottom - newRect.top;
  424. // If the new width and height of the window is larger
  425. // than the working area of the monitor but is smaller
  426. // than the monitor size then have it max out at the
  427. // working area so that it will remain uncovered. We
  428. // leave it alone if it is bigger than the monitor size
  429. // (with a small fudge) to support multiple monitors.
  430. if (newLeft + newWidth > (monInfo.rcWork.right - monInfo.rcWork.left) &&
  431. newLeft + newWidth <= (monInfo.rcMonitor.right - monInfo.rcMonitor.left) + 4)
  432. newWidth = (monInfo.rcWork.right - monInfo.rcWork.left) - newLeft;
  433. if (newTop + newHeight > (monInfo.rcWork.bottom - monInfo.rcWork.top) &&
  434. newTop + newHeight <= (monInfo.rcMonitor.bottom - monInfo.rcMonitor.top) + 4)
  435. newHeight = (monInfo.rcWork.bottom - monInfo.rcWork.top) - newTop;
  436. MoveWindow( mWindowHandle, newLeft, newTop, newWidth, newHeight, true );
  437. }
  438. else // Just position it according to the mPosition plus new extent
  439. MoveWindow(mWindowHandle, newRect.left, newRect.top, newRect.right - newRect.left, newRect.bottom - newRect.top, true);
  440. // Make sure the resolution matches the client extent
  441. Point2I clientExt = getClientExtent();
  442. mVideoMode.resolution.set( clientExt.x, clientExt.y );
  443. // Let GFX get an update about the new resolution
  444. if (mTarget.isValid())
  445. mTarget->resetMode();
  446. InvalidateRect( NULL, NULL, true );
  447. return true;
  448. }
  449. bool Win32Window::isOpen()
  450. {
  451. return true;
  452. }
  453. bool Win32Window::isVisible()
  454. {
  455. // Is the window open and visible, ie. not minimized?
  456. if(!mWindowHandle)
  457. return false;
  458. if (mOffscreenRender)
  459. return true;
  460. return IsWindowVisible(mWindowHandle)
  461. && !IsIconic(mWindowHandle)
  462. && !isScreenSaverRunning();
  463. }
  464. bool Win32Window::isFocused()
  465. {
  466. if (mOffscreenRender)
  467. return true;
  468. // CodeReview This is enough to make the plugin and normal/editor scenarios
  469. // coexist but it seems brittle. I think we need a better way to detect
  470. // if we're the foreground window, maybe taking into account if any of our
  471. // window's parents are foreground? [bjg 4/30/07]
  472. if(mOwningManager->mParentWindow)
  473. return (GetFocus() == mWindowHandle || IsChild(mWindowHandle, GetFocus()));
  474. else
  475. return ((GetFocus() == mWindowHandle || IsChild(mWindowHandle, GetFocus())) && GetForegroundWindow() == mWindowHandle);
  476. }
  477. bool Win32Window::isMinimized()
  478. {
  479. if (mOffscreenRender)
  480. return false;
  481. WINDOWPLACEMENT wd;
  482. if ( GetWindowPlacement( mWindowHandle, &wd ) )
  483. {
  484. return ( wd.showCmd == SW_SHOWMINIMIZED );
  485. }
  486. return false;
  487. }
  488. bool Win32Window::isMaximized()
  489. {
  490. if (mOffscreenRender)
  491. return true;
  492. WINDOWPLACEMENT wd;
  493. if ( GetWindowPlacement( mWindowHandle, &wd ) )
  494. {
  495. return ( wd.showCmd == SW_SHOWMAXIMIZED );
  496. }
  497. return false;
  498. }
  499. WindowId Win32Window::getWindowId()
  500. {
  501. return mWindowId;
  502. }
  503. void Win32Window::minimize()
  504. {
  505. if (mOffscreenRender)
  506. return;
  507. ShowWindow( mWindowHandle, SW_MINIMIZE );
  508. }
  509. void Win32Window::maximize()
  510. {
  511. if (mOffscreenRender)
  512. return;
  513. ShowWindow( mWindowHandle, SW_MAXIMIZE );
  514. }
  515. void Win32Window::restore()
  516. {
  517. if (mOffscreenRender)
  518. return;
  519. ShowWindow( mWindowHandle, SW_RESTORE );
  520. }
  521. void Win32Window::hide()
  522. {
  523. if (mOffscreenRender)
  524. return;
  525. ShowWindow( mWindowHandle, SW_HIDE );
  526. }
  527. void Win32Window::show()
  528. {
  529. if (mOffscreenRender)
  530. return;
  531. ShowWindow( mWindowHandle, SW_SHOWNORMAL );
  532. }
  533. void Win32Window::close()
  534. {
  535. delete this;
  536. }
  537. void Win32Window::_registerWindowClass()
  538. {
  539. // Check to see if it exists already.
  540. WNDCLASSEX classInfo;
  541. if (GetClassInfoEx(GetModuleHandle(NULL),_MainWindowClassName,&classInfo))
  542. return;
  543. HMODULE appInstance = GetModuleHandle(NULL);
  544. HICON appIcon = LoadIcon(appInstance, MAKEINTRESOURCE(IDI_ICON1));
  545. // Window class shared by all MainWindow objects
  546. classInfo.lpszClassName = _MainWindowClassName;
  547. classInfo.cbSize = sizeof(WNDCLASSEX);
  548. classInfo.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
  549. classInfo.lpfnWndProc = (WNDPROC)WindowProc;
  550. classInfo.hInstance = appInstance; // Owner of this class
  551. classInfo.hIcon = appIcon; // Icon name
  552. classInfo.hIconSm = appIcon; // Icon name
  553. classInfo.hCursor = LoadCursor(NULL, IDC_ARROW); // Cursor
  554. classInfo.hbrBackground = (HBRUSH)(COLOR_WINDOW+1); // Default color
  555. classInfo.lpszMenuName = NULL;
  556. classInfo.cbClsExtra = 0;
  557. classInfo.cbWndExtra = 0;
  558. if (!RegisterClassEx(&classInfo))
  559. AssertISV(false,"Window class initialization failed");
  560. classInfo.lpfnWndProc = DefWindowProc;
  561. classInfo.hCursor = NULL;
  562. classInfo.hbrBackground = (HBRUSH) GetStockObject(BLACK_BRUSH);
  563. classInfo.lpszClassName = _CurtainWindowClassName;
  564. if (!RegisterClassEx(&classInfo))
  565. AssertISV(false,"Curtain window class initialization failed");
  566. }
  567. void Win32Window::_unregisterWindowClass()
  568. {
  569. WNDCLASSEX classInfo;
  570. if (GetClassInfoEx(GetModuleHandle(NULL),_MainWindowClassName,&classInfo))
  571. UnregisterClass(_MainWindowClassName,GetModuleHandle(NULL));
  572. if (GetClassInfoEx(GetModuleHandle(NULL),_CurtainWindowClassName,&classInfo))
  573. UnregisterClass(_CurtainWindowClassName,GetModuleHandle(NULL));
  574. }
  575. LRESULT PASCAL Win32Window::WindowProc( HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam )
  576. {
  577. // CodeReview [tom, 4/30/2007] The two casts here seem somewhat silly and redundant ?
  578. Win32Window* window = (Win32Window*)((PlatformWindow*)GetWindowLongPtr(hWnd, GWLP_USERDATA));
  579. const WindowId devId = window ? window->getWindowId() : 0;
  580. if (window && window->getOffscreenRender())
  581. return DefWindowProc(hWnd, message, wParam, lParam);
  582. switch (message)
  583. {
  584. case WM_DISPLAYCHANGE:
  585. // Update the monitor list
  586. PlatformWindowManager::get()->buildMonitorsList();
  587. if(window && window->isVisible() && !window->mSuppressReset && window->getVideoMode().bitDepth != wParam)
  588. {
  589. Con::warnf("Win32Window::WindowProc - resetting device due to display mode BPP change.");
  590. window->getGFXTarget()->resetMode();
  591. }
  592. break;
  593. case WM_MOUSEACTIVATE:
  594. SetFocus(hWnd);
  595. return MA_ACTIVATE;
  596. case WM_MOUSEMOVE:
  597. if (window && GetFocus() != hWnd && IsChild(hWnd, GetFocus()))
  598. {
  599. SetFocus(hWnd);
  600. break;
  601. }
  602. // If our foreground window is the browser and we don't have focus grab it
  603. if (Platform::getWebDeployment() && GetFocus() != hWnd)
  604. {
  605. HWND phwnd = GetParent(hWnd);
  606. while (phwnd)
  607. {
  608. if (GetForegroundWindow() == phwnd)
  609. {
  610. SetFocus(hWnd);
  611. break;
  612. }
  613. phwnd = GetParent(phwnd);
  614. }
  615. }
  616. break;
  617. // Associate the window pointer with this window
  618. case WM_CREATE:
  619. // CodeReview [tom, 4/30/2007] Why don't we just cast this to a LONG
  620. // instead of having a ton of essentially pointless casts ?
  621. SetWindowLongPtr(hWnd, GWLP_USERDATA,
  622. (LONG_PTR)((PlatformWindow*)((CREATESTRUCT*)lParam)->lpCreateParams));
  623. break;
  624. case WM_SETFOCUS:
  625. // NOTE: if wParam is NOT equal to our window handle then we are GAINING focus
  626. Dispatch(DelayedDispatch, hWnd, message, wParam, lParam);
  627. return 0;
  628. case WM_KILLFOCUS:
  629. // NOTE: if wParam is NOT equal to our window handle then we are LOSING focus
  630. Dispatch(DelayedDispatch, hWnd, message, wParam, lParam);
  631. return 0;
  632. // The window is being dragged
  633. case WM_MOVE:
  634. if(!window)
  635. break;
  636. window->mPosition.x = (int)LOWORD(lParam);
  637. window->mPosition.y = (int)HIWORD(lParam);
  638. return 0;
  639. // Update viewport when the window moves
  640. case WM_SIZE:
  641. if(window && window->mSuppressReset)
  642. break;
  643. // This is dispatched immediately to prevent a race condition with journaling and window minimizing
  644. if (wParam != SIZE_MINIMIZED && !Journal::IsPlaying())
  645. Dispatch( ImmediateDispatch, hWnd,message,wParam,lParam );
  646. if(wParam != SIZE_MINIMIZED && window != NULL )
  647. {
  648. if(!window->mVideoMode.fullScreen)
  649. {
  650. U32 width = LOWORD( lParam );
  651. U32 height = HIWORD( lParam );
  652. window->mVideoMode.resolution.set( width, height );
  653. }
  654. if(window->getGFXTarget())
  655. {
  656. Con::warnf("Win32Window::WindowProc - resetting device due to window size change.");
  657. window->getGFXTarget()->resetMode();
  658. }
  659. window->resizeEvent.trigger(window, true);
  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