win32Window.cpp 33 KB

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