win32Window.cpp 34 KB

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