win32Window.cpp 33 KB

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