SDL_windowswindow.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2017 Sam Lantinga <slouken@libsdl.org>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include "../../SDL_internal.h"
  19. #if SDL_VIDEO_DRIVER_WINDOWS
  20. #include "../../core/windows/SDL_windows.h"
  21. #include "SDL_assert.h"
  22. #include "../SDL_sysvideo.h"
  23. #include "../SDL_pixels_c.h"
  24. #include "../../events/SDL_keyboard_c.h"
  25. #include "../../events/SDL_mouse_c.h"
  26. #include "SDL_windowsvideo.h"
  27. #include "SDL_windowswindow.h"
  28. #include "SDL_hints.h"
  29. /* Dropfile support */
  30. #include <shellapi.h>
  31. /* This is included after SDL_windowsvideo.h, which includes windows.h */
  32. #include "SDL_syswm.h"
  33. /* Windows CE compatibility */
  34. #ifndef SWP_NOCOPYBITS
  35. #define SWP_NOCOPYBITS 0
  36. #endif
  37. /* Fake window to help with DirectInput events. */
  38. HWND SDL_HelperWindow = NULL;
  39. static WCHAR *SDL_HelperWindowClassName = TEXT("SDLHelperWindowInputCatcher");
  40. static WCHAR *SDL_HelperWindowName = TEXT("SDLHelperWindowInputMsgWindow");
  41. static ATOM SDL_HelperWindowClass = 0;
  42. #define STYLE_BASIC (WS_CLIPSIBLINGS | WS_CLIPCHILDREN)
  43. #define STYLE_FULLSCREEN (WS_POPUP)
  44. #define STYLE_BORDERLESS (WS_POPUP)
  45. #define STYLE_NORMAL (WS_OVERLAPPED | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX)
  46. #define STYLE_RESIZABLE (WS_THICKFRAME | WS_MAXIMIZEBOX)
  47. #define STYLE_MASK (STYLE_FULLSCREEN | STYLE_BORDERLESS | STYLE_NORMAL | STYLE_RESIZABLE)
  48. static DWORD
  49. GetWindowStyle(SDL_Window * window)
  50. {
  51. DWORD style = 0;
  52. if (window->flags & SDL_WINDOW_FULLSCREEN) {
  53. style |= STYLE_FULLSCREEN;
  54. } else {
  55. if (window->flags & SDL_WINDOW_BORDERLESS) {
  56. style |= STYLE_BORDERLESS;
  57. } else {
  58. style |= STYLE_NORMAL;
  59. }
  60. if (window->flags & SDL_WINDOW_RESIZABLE) {
  61. style |= STYLE_RESIZABLE;
  62. }
  63. }
  64. return style;
  65. }
  66. static void
  67. WIN_SetWindowPositionInternal(_THIS, SDL_Window * window, UINT flags)
  68. {
  69. SDL_WindowData *data = (SDL_WindowData *)window->driverdata;
  70. HWND hwnd = data->hwnd;
  71. RECT rect;
  72. DWORD style;
  73. HWND top;
  74. BOOL menu;
  75. int x, y;
  76. int w, h;
  77. /* Figure out what the window area will be */
  78. if (SDL_ShouldAllowTopmost() && ((window->flags & (SDL_WINDOW_FULLSCREEN|SDL_WINDOW_INPUT_FOCUS)) == (SDL_WINDOW_FULLSCREEN|SDL_WINDOW_INPUT_FOCUS) || (window->flags & SDL_WINDOW_ALWAYS_ON_TOP))) {
  79. top = HWND_TOPMOST;
  80. } else {
  81. top = HWND_NOTOPMOST;
  82. }
  83. style = GetWindowLong(hwnd, GWL_STYLE);
  84. rect.left = 0;
  85. rect.top = 0;
  86. rect.right = window->w;
  87. rect.bottom = window->h;
  88. menu = (style & WS_CHILDWINDOW) ? FALSE : (GetMenu(hwnd) != NULL);
  89. AdjustWindowRectEx(&rect, style, menu, 0);
  90. w = (rect.right - rect.left);
  91. h = (rect.bottom - rect.top);
  92. x = window->x + rect.left;
  93. y = window->y + rect.top;
  94. data->expected_resize = SDL_TRUE;
  95. SetWindowPos(hwnd, top, x, y, w, h, flags);
  96. data->expected_resize = SDL_FALSE;
  97. }
  98. static int
  99. SetupWindowData(_THIS, SDL_Window * window, HWND hwnd, SDL_bool created)
  100. {
  101. SDL_VideoData *videodata = (SDL_VideoData *) _this->driverdata;
  102. SDL_WindowData *data;
  103. /* Allocate the window data */
  104. data = (SDL_WindowData *) SDL_calloc(1, sizeof(*data));
  105. if (!data) {
  106. return SDL_OutOfMemory();
  107. }
  108. data->window = window;
  109. data->hwnd = hwnd;
  110. data->hdc = GetDC(hwnd);
  111. data->hinstance = (HINSTANCE) GetWindowLongPtr(hwnd, GWLP_HINSTANCE);
  112. data->created = created;
  113. data->mouse_button_flags = 0;
  114. data->videodata = videodata;
  115. data->initializing = SDL_TRUE;
  116. window->driverdata = data;
  117. /* Associate the data with the window */
  118. if (!SetProp(hwnd, TEXT("SDL_WindowData"), data)) {
  119. ReleaseDC(hwnd, data->hdc);
  120. SDL_free(data);
  121. return WIN_SetError("SetProp() failed");
  122. }
  123. /* Set up the window proc function */
  124. #ifdef GWLP_WNDPROC
  125. data->wndproc = (WNDPROC) GetWindowLongPtr(hwnd, GWLP_WNDPROC);
  126. if (data->wndproc == WIN_WindowProc) {
  127. data->wndproc = NULL;
  128. } else {
  129. SetWindowLongPtr(hwnd, GWLP_WNDPROC, (LONG_PTR) WIN_WindowProc);
  130. }
  131. #else
  132. data->wndproc = (WNDPROC) GetWindowLong(hwnd, GWL_WNDPROC);
  133. if (data->wndproc == WIN_WindowProc) {
  134. data->wndproc = NULL;
  135. } else {
  136. SetWindowLong(hwnd, GWL_WNDPROC, (LONG_PTR) WIN_WindowProc);
  137. }
  138. #endif
  139. /* Fill in the SDL window with the window data */
  140. {
  141. RECT rect;
  142. if (GetClientRect(hwnd, &rect)) {
  143. int w = rect.right;
  144. int h = rect.bottom;
  145. if ((window->w && window->w != w) || (window->h && window->h != h)) {
  146. /* We tried to create a window larger than the desktop and Windows didn't allow it. Override! */
  147. RECT rect;
  148. DWORD style;
  149. BOOL menu;
  150. int x, y;
  151. int w, h;
  152. /* Figure out what the window area will be */
  153. style = GetWindowLong(hwnd, GWL_STYLE);
  154. rect.left = 0;
  155. rect.top = 0;
  156. rect.right = window->w;
  157. rect.bottom = window->h;
  158. menu = (style & WS_CHILDWINDOW) ? FALSE : (GetMenu(hwnd) != NULL);
  159. AdjustWindowRectEx(&rect, style, menu, 0);
  160. w = (rect.right - rect.left);
  161. h = (rect.bottom - rect.top);
  162. x = window->x + rect.left;
  163. y = window->y + rect.top;
  164. SetWindowPos(hwnd, HWND_NOTOPMOST, x, y, w, h, SWP_NOCOPYBITS | SWP_NOZORDER | SWP_NOACTIVATE);
  165. } else {
  166. window->w = w;
  167. window->h = h;
  168. }
  169. }
  170. }
  171. {
  172. POINT point;
  173. point.x = 0;
  174. point.y = 0;
  175. if (ClientToScreen(hwnd, &point)) {
  176. window->x = point.x;
  177. window->y = point.y;
  178. }
  179. }
  180. {
  181. DWORD style = GetWindowLong(hwnd, GWL_STYLE);
  182. if (style & WS_VISIBLE) {
  183. window->flags |= SDL_WINDOW_SHOWN;
  184. } else {
  185. window->flags &= ~SDL_WINDOW_SHOWN;
  186. }
  187. if (style & (WS_BORDER | WS_THICKFRAME)) {
  188. window->flags &= ~SDL_WINDOW_BORDERLESS;
  189. } else {
  190. window->flags |= SDL_WINDOW_BORDERLESS;
  191. }
  192. if (style & WS_THICKFRAME) {
  193. window->flags |= SDL_WINDOW_RESIZABLE;
  194. } else {
  195. window->flags &= ~SDL_WINDOW_RESIZABLE;
  196. }
  197. #ifdef WS_MAXIMIZE
  198. if (style & WS_MAXIMIZE) {
  199. window->flags |= SDL_WINDOW_MAXIMIZED;
  200. } else
  201. #endif
  202. {
  203. window->flags &= ~SDL_WINDOW_MAXIMIZED;
  204. }
  205. #ifdef WS_MINIMIZE
  206. if (style & WS_MINIMIZE) {
  207. window->flags |= SDL_WINDOW_MINIMIZED;
  208. } else
  209. #endif
  210. {
  211. window->flags &= ~SDL_WINDOW_MINIMIZED;
  212. }
  213. }
  214. if (GetFocus() == hwnd) {
  215. window->flags |= SDL_WINDOW_INPUT_FOCUS;
  216. SDL_SetKeyboardFocus(data->window);
  217. if (window->flags & SDL_WINDOW_INPUT_GRABBED) {
  218. RECT rect;
  219. GetClientRect(hwnd, &rect);
  220. ClientToScreen(hwnd, (LPPOINT) & rect);
  221. ClientToScreen(hwnd, (LPPOINT) & rect + 1);
  222. ClipCursor(&rect);
  223. }
  224. }
  225. /* Enable multi-touch */
  226. if (videodata->RegisterTouchWindow) {
  227. videodata->RegisterTouchWindow(hwnd, (TWF_FINETOUCH|TWF_WANTPALM));
  228. }
  229. /* Enable dropping files */
  230. DragAcceptFiles(hwnd, TRUE);
  231. data->initializing = SDL_FALSE;
  232. /* All done! */
  233. return 0;
  234. }
  235. int
  236. WIN_CreateWindow(_THIS, SDL_Window * window)
  237. {
  238. HWND hwnd, parent = NULL;
  239. RECT rect;
  240. DWORD style = STYLE_BASIC;
  241. int x, y;
  242. int w, h;
  243. if (window->flags & SDL_WINDOW_SKIP_TASKBAR) {
  244. parent = CreateWindow(SDL_Appname, TEXT(""), STYLE_BASIC, 0, 0, 32, 32, NULL, NULL, SDL_Instance, NULL);
  245. }
  246. style |= GetWindowStyle(window);
  247. /* Figure out what the window area will be */
  248. rect.left = window->x;
  249. rect.top = window->y;
  250. rect.right = window->x + window->w;
  251. rect.bottom = window->y + window->h;
  252. AdjustWindowRectEx(&rect, style, FALSE, 0);
  253. x = rect.left;
  254. y = rect.top;
  255. w = (rect.right - rect.left);
  256. h = (rect.bottom - rect.top);
  257. hwnd =
  258. CreateWindow(SDL_Appname, TEXT(""), style, x, y, w, h, parent, NULL,
  259. SDL_Instance, NULL);
  260. if (!hwnd) {
  261. return WIN_SetError("Couldn't create window");
  262. }
  263. WIN_PumpEvents(_this);
  264. if (SetupWindowData(_this, window, hwnd, SDL_TRUE) < 0) {
  265. DestroyWindow(hwnd);
  266. return -1;
  267. }
  268. if (!(window->flags & SDL_WINDOW_OPENGL)) {
  269. return 0;
  270. }
  271. /* The rest of this macro mess is for OpenGL or OpenGL ES windows */
  272. #if SDL_VIDEO_OPENGL_ES2
  273. if (_this->gl_config.profile_mask == SDL_GL_CONTEXT_PROFILE_ES
  274. #if SDL_VIDEO_OPENGL_WGL
  275. && (!_this->gl_data || WIN_GL_UseEGL(_this))
  276. #endif /* SDL_VIDEO_OPENGL_WGL */
  277. ) {
  278. #if SDL_VIDEO_OPENGL_EGL
  279. if (WIN_GLES_SetupWindow(_this, window) < 0) {
  280. WIN_DestroyWindow(_this, window);
  281. return -1;
  282. }
  283. return 0;
  284. #else
  285. return SDL_SetError("Could not create GLES window surface (EGL support not configured)");
  286. #endif /* SDL_VIDEO_OPENGL_EGL */
  287. }
  288. #endif /* SDL_VIDEO_OPENGL_ES2 */
  289. #if SDL_VIDEO_OPENGL_WGL
  290. if (WIN_GL_SetupWindow(_this, window) < 0) {
  291. WIN_DestroyWindow(_this, window);
  292. return -1;
  293. }
  294. #else
  295. return SDL_SetError("Could not create GL window (WGL support not configured)");
  296. #endif
  297. return 0;
  298. }
  299. int
  300. WIN_CreateWindowFrom(_THIS, SDL_Window * window, const void *data)
  301. {
  302. HWND hwnd = (HWND) data;
  303. LPTSTR title;
  304. int titleLen;
  305. /* Query the title from the existing window */
  306. titleLen = GetWindowTextLength(hwnd);
  307. title = SDL_stack_alloc(TCHAR, titleLen + 1);
  308. if (title) {
  309. titleLen = GetWindowText(hwnd, title, titleLen);
  310. } else {
  311. titleLen = 0;
  312. }
  313. if (titleLen > 0) {
  314. window->title = WIN_StringToUTF8(title);
  315. }
  316. if (title) {
  317. SDL_stack_free(title);
  318. }
  319. if (SetupWindowData(_this, window, hwnd, SDL_FALSE) < 0) {
  320. return -1;
  321. }
  322. #if SDL_VIDEO_OPENGL_WGL
  323. {
  324. const char *hint = SDL_GetHint(SDL_HINT_VIDEO_WINDOW_SHARE_PIXEL_FORMAT);
  325. if (hint) {
  326. /* This hint is a pointer (in string form) of the address of
  327. the window to share a pixel format with
  328. */
  329. SDL_Window *otherWindow = NULL;
  330. SDL_sscanf(hint, "%p", (void**)&otherWindow);
  331. /* Do some error checking on the pointer */
  332. if (otherWindow != NULL && otherWindow->magic == &_this->window_magic)
  333. {
  334. /* If the otherWindow has SDL_WINDOW_OPENGL set, set it for the new window as well */
  335. if (otherWindow->flags & SDL_WINDOW_OPENGL)
  336. {
  337. window->flags |= SDL_WINDOW_OPENGL;
  338. if(!WIN_GL_SetPixelFormatFrom(_this, otherWindow, window)) {
  339. return -1;
  340. }
  341. }
  342. }
  343. }
  344. }
  345. #endif
  346. return 0;
  347. }
  348. void
  349. WIN_SetWindowTitle(_THIS, SDL_Window * window)
  350. {
  351. HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
  352. LPTSTR title = WIN_UTF8ToString(window->title);
  353. SetWindowText(hwnd, title);
  354. SDL_free(title);
  355. }
  356. void
  357. WIN_SetWindowIcon(_THIS, SDL_Window * window, SDL_Surface * icon)
  358. {
  359. HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
  360. HICON hicon = NULL;
  361. BYTE *icon_bmp;
  362. int icon_len, y;
  363. SDL_RWops *dst;
  364. /* Create temporary bitmap buffer */
  365. icon_len = 40 + icon->h * icon->w * sizeof(Uint32);
  366. icon_bmp = SDL_stack_alloc(BYTE, icon_len);
  367. dst = SDL_RWFromMem(icon_bmp, icon_len);
  368. if (!dst) {
  369. SDL_stack_free(icon_bmp);
  370. return;
  371. }
  372. /* Write the BITMAPINFO header */
  373. SDL_WriteLE32(dst, 40);
  374. SDL_WriteLE32(dst, icon->w);
  375. SDL_WriteLE32(dst, icon->h * 2);
  376. SDL_WriteLE16(dst, 1);
  377. SDL_WriteLE16(dst, 32);
  378. SDL_WriteLE32(dst, BI_RGB);
  379. SDL_WriteLE32(dst, icon->h * icon->w * sizeof(Uint32));
  380. SDL_WriteLE32(dst, 0);
  381. SDL_WriteLE32(dst, 0);
  382. SDL_WriteLE32(dst, 0);
  383. SDL_WriteLE32(dst, 0);
  384. /* Write the pixels upside down into the bitmap buffer */
  385. SDL_assert(icon->format->format == SDL_PIXELFORMAT_ARGB8888);
  386. y = icon->h;
  387. while (y--) {
  388. Uint8 *src = (Uint8 *) icon->pixels + y * icon->pitch;
  389. SDL_RWwrite(dst, src, icon->w * sizeof(Uint32), 1);
  390. }
  391. hicon = CreateIconFromResource(icon_bmp, icon_len, TRUE, 0x00030000);
  392. SDL_RWclose(dst);
  393. SDL_stack_free(icon_bmp);
  394. /* Set the icon for the window */
  395. SendMessage(hwnd, WM_SETICON, ICON_SMALL, (LPARAM) hicon);
  396. /* Set the icon in the task manager (should we do this?) */
  397. SendMessage(hwnd, WM_SETICON, ICON_BIG, (LPARAM) hicon);
  398. }
  399. void
  400. WIN_SetWindowPosition(_THIS, SDL_Window * window)
  401. {
  402. WIN_SetWindowPositionInternal(_this, window, SWP_NOCOPYBITS | SWP_NOSIZE | SWP_NOACTIVATE);
  403. }
  404. void
  405. WIN_SetWindowSize(_THIS, SDL_Window * window)
  406. {
  407. WIN_SetWindowPositionInternal(_this, window, SWP_NOCOPYBITS | SWP_NOMOVE | SWP_NOACTIVATE);
  408. }
  409. void
  410. WIN_ShowWindow(_THIS, SDL_Window * window)
  411. {
  412. HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
  413. ShowWindow(hwnd, SW_SHOW);
  414. }
  415. void
  416. WIN_HideWindow(_THIS, SDL_Window * window)
  417. {
  418. HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
  419. ShowWindow(hwnd, SW_HIDE);
  420. }
  421. void
  422. WIN_RaiseWindow(_THIS, SDL_Window * window)
  423. {
  424. HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
  425. SetForegroundWindow(hwnd);
  426. }
  427. void
  428. WIN_MaximizeWindow(_THIS, SDL_Window * window)
  429. {
  430. SDL_WindowData *data = (SDL_WindowData *)window->driverdata;
  431. HWND hwnd = data->hwnd;
  432. data->expected_resize = SDL_TRUE;
  433. ShowWindow(hwnd, SW_MAXIMIZE);
  434. data->expected_resize = SDL_FALSE;
  435. }
  436. void
  437. WIN_MinimizeWindow(_THIS, SDL_Window * window)
  438. {
  439. HWND hwnd = ((SDL_WindowData *) window->driverdata)->hwnd;
  440. ShowWindow(hwnd, SW_MINIMIZE);
  441. }
  442. void
  443. WIN_SetWindowBordered(_THIS, SDL_Window * window, SDL_bool bordered)
  444. {
  445. SDL_WindowData *data = (SDL_WindowData *)window->driverdata;
  446. HWND hwnd = data->hwnd;
  447. DWORD style = GetWindowLong(hwnd, GWL_STYLE);
  448. if (bordered) {
  449. style &= ~STYLE_BORDERLESS;
  450. style |= STYLE_NORMAL;
  451. } else {
  452. style &= ~STYLE_NORMAL;
  453. style |= STYLE_BORDERLESS;
  454. }
  455. data->in_border_change = SDL_TRUE;
  456. SetWindowLong(hwnd, GWL_STYLE, style);
  457. WIN_SetWindowPositionInternal(_this, window, SWP_NOCOPYBITS | SWP_FRAMECHANGED | SWP_NOZORDER | SWP_NOACTIVATE);
  458. data->in_border_change = SDL_FALSE;
  459. }
  460. void
  461. WIN_SetWindowResizable(_THIS, SDL_Window * window, SDL_bool resizable)
  462. {
  463. SDL_WindowData *data = (SDL_WindowData *)window->driverdata;
  464. HWND hwnd = data->hwnd;
  465. DWORD style = GetWindowLong(hwnd, GWL_STYLE);
  466. if (resizable) {
  467. style |= STYLE_RESIZABLE;
  468. } else {
  469. style &= ~STYLE_RESIZABLE;
  470. }
  471. SetWindowLong(hwnd, GWL_STYLE, style);
  472. }
  473. void
  474. WIN_RestoreWindow(_THIS, SDL_Window * window)
  475. {
  476. SDL_WindowData *data = (SDL_WindowData *)window->driverdata;
  477. HWND hwnd = data->hwnd;
  478. data->expected_resize = SDL_TRUE;
  479. ShowWindow(hwnd, SW_RESTORE);
  480. data->expected_resize = SDL_FALSE;
  481. }
  482. void
  483. WIN_SetWindowFullscreen(_THIS, SDL_Window * window, SDL_VideoDisplay * display, SDL_bool fullscreen)
  484. {
  485. SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
  486. HWND hwnd = data->hwnd;
  487. RECT rect;
  488. SDL_Rect bounds;
  489. DWORD style;
  490. HWND top;
  491. BOOL menu;
  492. int x, y;
  493. int w, h;
  494. if (SDL_ShouldAllowTopmost() && ((window->flags & (SDL_WINDOW_FULLSCREEN|SDL_WINDOW_INPUT_FOCUS)) == (SDL_WINDOW_FULLSCREEN|SDL_WINDOW_INPUT_FOCUS) || window->flags & SDL_WINDOW_ALWAYS_ON_TOP)) {
  495. top = HWND_TOPMOST;
  496. } else {
  497. top = HWND_NOTOPMOST;
  498. }
  499. style = GetWindowLong(hwnd, GWL_STYLE);
  500. style &= ~STYLE_MASK;
  501. style |= GetWindowStyle(window);
  502. WIN_GetDisplayBounds(_this, display, &bounds);
  503. if (fullscreen) {
  504. x = bounds.x;
  505. y = bounds.y;
  506. w = bounds.w;
  507. h = bounds.h;
  508. /* Unset the maximized flag. This fixes
  509. https://bugzilla.libsdl.org/show_bug.cgi?id=3215
  510. */
  511. if (style & WS_MAXIMIZE) {
  512. data->windowed_mode_was_maximized = SDL_TRUE;
  513. style &= ~WS_MAXIMIZE;
  514. }
  515. } else {
  516. /* Restore window-maximization state, as applicable.
  517. Special care is taken to *not* do this if and when we're
  518. alt-tab'ing away (to some other window; as indicated by
  519. in_window_deactivation), otherwise
  520. https://bugzilla.libsdl.org/show_bug.cgi?id=3215 can reproduce!
  521. */
  522. if (data->windowed_mode_was_maximized && !data->in_window_deactivation) {
  523. style |= WS_MAXIMIZE;
  524. data->windowed_mode_was_maximized = SDL_FALSE;
  525. }
  526. rect.left = 0;
  527. rect.top = 0;
  528. rect.right = window->windowed.w;
  529. rect.bottom = window->windowed.h;
  530. menu = (style & WS_CHILDWINDOW) ? FALSE : (GetMenu(hwnd) != NULL);
  531. AdjustWindowRectEx(&rect, style, menu, 0);
  532. w = (rect.right - rect.left);
  533. h = (rect.bottom - rect.top);
  534. x = window->windowed.x + rect.left;
  535. y = window->windowed.y + rect.top;
  536. }
  537. SetWindowLong(hwnd, GWL_STYLE, style);
  538. data->expected_resize = SDL_TRUE;
  539. SetWindowPos(hwnd, top, x, y, w, h, SWP_NOCOPYBITS | SWP_NOACTIVATE);
  540. data->expected_resize = SDL_FALSE;
  541. }
  542. int
  543. WIN_SetWindowGammaRamp(_THIS, SDL_Window * window, const Uint16 * ramp)
  544. {
  545. SDL_VideoDisplay *display = SDL_GetDisplayForWindow(window);
  546. SDL_DisplayData *data = (SDL_DisplayData *) display->driverdata;
  547. HDC hdc;
  548. BOOL succeeded = FALSE;
  549. hdc = CreateDC(data->DeviceName, NULL, NULL, NULL);
  550. if (hdc) {
  551. succeeded = SetDeviceGammaRamp(hdc, (LPVOID)ramp);
  552. if (!succeeded) {
  553. WIN_SetError("SetDeviceGammaRamp()");
  554. }
  555. DeleteDC(hdc);
  556. }
  557. return succeeded ? 0 : -1;
  558. }
  559. int
  560. WIN_GetWindowGammaRamp(_THIS, SDL_Window * window, Uint16 * ramp)
  561. {
  562. SDL_VideoDisplay *display = SDL_GetDisplayForWindow(window);
  563. SDL_DisplayData *data = (SDL_DisplayData *) display->driverdata;
  564. HDC hdc;
  565. BOOL succeeded = FALSE;
  566. hdc = CreateDC(data->DeviceName, NULL, NULL, NULL);
  567. if (hdc) {
  568. succeeded = GetDeviceGammaRamp(hdc, (LPVOID)ramp);
  569. if (!succeeded) {
  570. WIN_SetError("GetDeviceGammaRamp()");
  571. }
  572. DeleteDC(hdc);
  573. }
  574. return succeeded ? 0 : -1;
  575. }
  576. void
  577. WIN_SetWindowGrab(_THIS, SDL_Window * window, SDL_bool grabbed)
  578. {
  579. WIN_UpdateClipCursor(window);
  580. if (window->flags & SDL_WINDOW_FULLSCREEN) {
  581. UINT flags = SWP_NOCOPYBITS | SWP_NOMOVE | SWP_NOSIZE;
  582. if (!(window->flags & SDL_WINDOW_SHOWN)) {
  583. flags |= SWP_NOACTIVATE;
  584. }
  585. WIN_SetWindowPositionInternal(_this, window, flags);
  586. }
  587. }
  588. void
  589. WIN_DestroyWindow(_THIS, SDL_Window * window)
  590. {
  591. SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
  592. if (data) {
  593. ReleaseDC(data->hwnd, data->hdc);
  594. RemoveProp(data->hwnd, TEXT("SDL_WindowData"));
  595. if (data->created) {
  596. DestroyWindow(data->hwnd);
  597. } else {
  598. /* Restore any original event handler... */
  599. if (data->wndproc != NULL) {
  600. #ifdef GWLP_WNDPROC
  601. SetWindowLongPtr(data->hwnd, GWLP_WNDPROC,
  602. (LONG_PTR) data->wndproc);
  603. #else
  604. SetWindowLong(data->hwnd, GWL_WNDPROC,
  605. (LONG_PTR) data->wndproc);
  606. #endif
  607. }
  608. }
  609. SDL_free(data);
  610. }
  611. window->driverdata = NULL;
  612. }
  613. SDL_bool
  614. WIN_GetWindowWMInfo(_THIS, SDL_Window * window, SDL_SysWMinfo * info)
  615. {
  616. const SDL_WindowData *data = (const SDL_WindowData *) window->driverdata;
  617. if (info->version.major <= SDL_MAJOR_VERSION) {
  618. int versionnum = SDL_VERSIONNUM(info->version.major, info->version.minor, info->version.patch);
  619. info->subsystem = SDL_SYSWM_WINDOWS;
  620. info->info.win.window = data->hwnd;
  621. if (versionnum >= SDL_VERSIONNUM(2, 0, 4)) {
  622. info->info.win.hdc = data->hdc;
  623. }
  624. if (versionnum >= SDL_VERSIONNUM(2, 0, 5)) {
  625. info->info.win.hinstance = data->hinstance;
  626. }
  627. return SDL_TRUE;
  628. } else {
  629. SDL_SetError("Application not compiled with SDL %d.%d",
  630. SDL_MAJOR_VERSION, SDL_MINOR_VERSION);
  631. return SDL_FALSE;
  632. }
  633. }
  634. /*
  635. * Creates a HelperWindow used for DirectInput events.
  636. */
  637. int
  638. SDL_HelperWindowCreate(void)
  639. {
  640. HINSTANCE hInstance = GetModuleHandle(NULL);
  641. WNDCLASS wce;
  642. /* Make sure window isn't created twice. */
  643. if (SDL_HelperWindow != NULL) {
  644. return 0;
  645. }
  646. /* Create the class. */
  647. SDL_zero(wce);
  648. wce.lpfnWndProc = DefWindowProc;
  649. wce.lpszClassName = (LPCWSTR) SDL_HelperWindowClassName;
  650. wce.hInstance = hInstance;
  651. /* Register the class. */
  652. SDL_HelperWindowClass = RegisterClass(&wce);
  653. if (SDL_HelperWindowClass == 0 && GetLastError() != ERROR_CLASS_ALREADY_EXISTS) {
  654. return WIN_SetError("Unable to create Helper Window Class");
  655. }
  656. /* Create the window. */
  657. SDL_HelperWindow = CreateWindowEx(0, SDL_HelperWindowClassName,
  658. SDL_HelperWindowName,
  659. WS_OVERLAPPED, CW_USEDEFAULT,
  660. CW_USEDEFAULT, CW_USEDEFAULT,
  661. CW_USEDEFAULT, HWND_MESSAGE, NULL,
  662. hInstance, NULL);
  663. if (SDL_HelperWindow == NULL) {
  664. UnregisterClass(SDL_HelperWindowClassName, hInstance);
  665. return WIN_SetError("Unable to create Helper Window");
  666. }
  667. return 0;
  668. }
  669. /*
  670. * Destroys the HelperWindow previously created with SDL_HelperWindowCreate.
  671. */
  672. void
  673. SDL_HelperWindowDestroy(void)
  674. {
  675. HINSTANCE hInstance = GetModuleHandle(NULL);
  676. /* Destroy the window. */
  677. if (SDL_HelperWindow != NULL) {
  678. if (DestroyWindow(SDL_HelperWindow) == 0) {
  679. WIN_SetError("Unable to destroy Helper Window");
  680. return;
  681. }
  682. SDL_HelperWindow = NULL;
  683. }
  684. /* Unregister the class. */
  685. if (SDL_HelperWindowClass != 0) {
  686. if ((UnregisterClass(SDL_HelperWindowClassName, hInstance)) == 0) {
  687. WIN_SetError("Unable to destroy Helper Window Class");
  688. return;
  689. }
  690. SDL_HelperWindowClass = 0;
  691. }
  692. }
  693. void WIN_OnWindowEnter(_THIS, SDL_Window * window)
  694. {
  695. SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
  696. if (!data || !data->hwnd) {
  697. /* The window wasn't fully initialized */
  698. return;
  699. }
  700. if (window->flags & SDL_WINDOW_ALWAYS_ON_TOP) {
  701. WIN_SetWindowPositionInternal(_this, window, SWP_NOCOPYBITS | SWP_NOSIZE | SWP_NOACTIVATE);
  702. }
  703. #ifdef WM_MOUSELEAVE
  704. {
  705. TRACKMOUSEEVENT trackMouseEvent;
  706. trackMouseEvent.cbSize = sizeof(TRACKMOUSEEVENT);
  707. trackMouseEvent.dwFlags = TME_LEAVE;
  708. trackMouseEvent.hwndTrack = data->hwnd;
  709. TrackMouseEvent(&trackMouseEvent);
  710. }
  711. #endif /* WM_MOUSELEAVE */
  712. }
  713. void
  714. WIN_UpdateClipCursor(SDL_Window *window)
  715. {
  716. SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
  717. SDL_Mouse *mouse = SDL_GetMouse();
  718. if (data->focus_click_pending) {
  719. return;
  720. }
  721. if ((mouse->relative_mode || (window->flags & SDL_WINDOW_INPUT_GRABBED)) &&
  722. (window->flags & SDL_WINDOW_INPUT_FOCUS)) {
  723. if (mouse->relative_mode && !mouse->relative_mode_warp) {
  724. LONG cx, cy;
  725. RECT rect;
  726. GetWindowRect(data->hwnd, &rect);
  727. cx = (rect.left + rect.right) / 2;
  728. cy = (rect.top + rect.bottom) / 2;
  729. /* Make an absurdly small clip rect */
  730. rect.left = cx - 1;
  731. rect.right = cx + 1;
  732. rect.top = cy - 1;
  733. rect.bottom = cy + 1;
  734. ClipCursor(&rect);
  735. } else {
  736. RECT rect;
  737. if (GetClientRect(data->hwnd, &rect) && !IsRectEmpty(&rect)) {
  738. ClientToScreen(data->hwnd, (LPPOINT) & rect);
  739. ClientToScreen(data->hwnd, (LPPOINT) & rect + 1);
  740. ClipCursor(&rect);
  741. }
  742. }
  743. } else {
  744. ClipCursor(NULL);
  745. }
  746. }
  747. int
  748. WIN_SetWindowHitTest(SDL_Window *window, SDL_bool enabled)
  749. {
  750. return 0; /* just succeed, the real work is done elsewhere. */
  751. }
  752. int
  753. WIN_SetWindowOpacity(_THIS, SDL_Window * window, float opacity)
  754. {
  755. const SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
  756. const HWND hwnd = data->hwnd;
  757. const LONG style = GetWindowLong(hwnd, GWL_EXSTYLE);
  758. SDL_assert(style != 0);
  759. if (opacity == 1.0f) {
  760. /* want it fully opaque, just mark it unlayered if necessary. */
  761. if (style & WS_EX_LAYERED) {
  762. if (SetWindowLong(hwnd, GWL_EXSTYLE, style & ~WS_EX_LAYERED) == 0) {
  763. return WIN_SetError("SetWindowLong()");
  764. }
  765. }
  766. } else {
  767. const BYTE alpha = (BYTE) ((int) (opacity * 255.0f));
  768. /* want it transparent, mark it layered if necessary. */
  769. if ((style & WS_EX_LAYERED) == 0) {
  770. if (SetWindowLong(hwnd, GWL_EXSTYLE, style | WS_EX_LAYERED) == 0) {
  771. return WIN_SetError("SetWindowLong()");
  772. }
  773. }
  774. if (SetLayeredWindowAttributes(hwnd, 0, alpha, LWA_ALPHA) == 0) {
  775. return WIN_SetError("SetLayeredWindowAttributes()");
  776. }
  777. }
  778. return 0;
  779. }
  780. #endif /* SDL_VIDEO_DRIVER_WINDOWS */
  781. /* vi: set ts=4 sw=4 expandtab: */