win32Window.cpp 33 KB

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