win32Window.cpp 33 KB

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