2
0

win32Window.cpp 33 KB

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