winex11: Add window data structure locking to the window region functions.
[wine.git] / dlls / winex11.drv / window.c
blob5eab698870eeac95fa7f14c6b0ea72baecdf3c82
1 /*
2 * Window related functions
4 * Copyright 1993, 1994, 1995, 1996, 2001 Alexandre Julliard
5 * Copyright 1993 David Metcalfe
6 * Copyright 1995, 1996 Alex Korobka
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "config.h"
25 #include <stdarg.h>
26 #include <stdlib.h>
27 #include <stdio.h>
28 #ifdef HAVE_UNISTD_H
29 # include <unistd.h>
30 #endif
32 #include <X11/Xlib.h>
33 #include <X11/Xresource.h>
34 #include <X11/Xutil.h>
35 #ifdef HAVE_LIBXSHAPE
36 #include <X11/extensions/shape.h>
37 #endif /* HAVE_LIBXSHAPE */
39 #include "windef.h"
40 #include "winbase.h"
41 #include "wingdi.h"
42 #include "winuser.h"
43 #include "wine/unicode.h"
45 #include "x11drv.h"
46 #include "wine/debug.h"
47 #include "wine/server.h"
48 #include "mwm.h"
50 WINE_DEFAULT_DEBUG_CHANNEL(x11drv);
52 #define _NET_WM_MOVERESIZE_SIZE_TOPLEFT 0
53 #define _NET_WM_MOVERESIZE_SIZE_TOP 1
54 #define _NET_WM_MOVERESIZE_SIZE_TOPRIGHT 2
55 #define _NET_WM_MOVERESIZE_SIZE_RIGHT 3
56 #define _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT 4
57 #define _NET_WM_MOVERESIZE_SIZE_BOTTOM 5
58 #define _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT 6
59 #define _NET_WM_MOVERESIZE_SIZE_LEFT 7
60 #define _NET_WM_MOVERESIZE_MOVE 8
61 #define _NET_WM_MOVERESIZE_SIZE_KEYBOARD 9
62 #define _NET_WM_MOVERESIZE_MOVE_KEYBOARD 10
64 #define _NET_WM_STATE_REMOVE 0
65 #define _NET_WM_STATE_ADD 1
66 #define _NET_WM_STATE_TOGGLE 2
68 #define SWP_AGG_NOPOSCHANGE (SWP_NOSIZE | SWP_NOMOVE | SWP_NOCLIENTSIZE | SWP_NOCLIENTMOVE | SWP_NOZORDER)
70 /* is cursor clipping active? */
71 int clipping_cursor = 0;
73 /* X context to associate a hwnd to an X window */
74 XContext winContext = 0;
76 /* X context to associate a struct x11drv_win_data to an hwnd */
77 XContext win_data_context = 0;
79 /* time of last user event and window where it's stored */
80 static Time last_user_time;
81 static Window user_time_window;
83 static const char foreign_window_prop[] = "__wine_x11_foreign_window";
84 static const char whole_window_prop[] = "__wine_x11_whole_window";
85 static const char clip_window_prop[] = "__wine_x11_clip_window";
86 static const char managed_prop[] = "__wine_x11_managed";
88 static CRITICAL_SECTION win_data_section;
89 static CRITICAL_SECTION_DEBUG critsect_debug =
91 0, 0, &win_data_section,
92 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
93 0, 0, { (DWORD_PTR)(__FILE__ ": win_data_section") }
95 static CRITICAL_SECTION win_data_section = { &critsect_debug, -1, 0, 0, 0, 0 };
98 /***********************************************************************
99 * http://standards.freedesktop.org/startup-notification-spec
101 static void remove_startup_notification(Display *display, Window window)
103 static LONG startup_notification_removed = 0;
104 char id[1024];
105 char message[1024];
106 int i;
107 int pos;
108 XEvent xevent;
109 const char *src;
110 int srclen;
112 if (InterlockedCompareExchange(&startup_notification_removed, 1, 0) != 0)
113 return;
115 if (GetEnvironmentVariableA("DESKTOP_STARTUP_ID", id, sizeof(id)) == 0)
116 return;
117 SetEnvironmentVariableA("DESKTOP_STARTUP_ID", NULL);
119 if ((src = strstr( id, "_TIME" ))) update_user_time( atol( src + 5 ));
121 pos = snprintf(message, sizeof(message), "remove: ID=");
122 message[pos++] = '"';
123 for (i = 0; id[i] && pos < sizeof(message) - 2; i++)
125 if (id[i] == '"' || id[i] == '\\')
126 message[pos++] = '\\';
127 message[pos++] = id[i];
129 message[pos++] = '"';
130 message[pos++] = '\0';
132 xevent.xclient.type = ClientMessage;
133 xevent.xclient.message_type = x11drv_atom(_NET_STARTUP_INFO_BEGIN);
134 xevent.xclient.display = display;
135 xevent.xclient.window = window;
136 xevent.xclient.format = 8;
138 src = message;
139 srclen = strlen(src) + 1;
141 while (srclen > 0)
143 int msglen = srclen;
144 if (msglen > 20)
145 msglen = 20;
146 memset(&xevent.xclient.data.b[0], 0, 20);
147 memcpy(&xevent.xclient.data.b[0], src, msglen);
148 src += msglen;
149 srclen -= msglen;
151 XSendEvent( display, DefaultRootWindow( display ), False, PropertyChangeMask, &xevent );
152 xevent.xclient.message_type = x11drv_atom(_NET_STARTUP_INFO);
157 struct has_popup_result
159 HWND hwnd;
160 BOOL found;
163 static BOOL is_managed( HWND hwnd )
165 struct x11drv_win_data *data = get_win_data( hwnd );
166 BOOL ret = data && data->managed;
167 release_win_data( data );
168 return ret;
171 static BOOL CALLBACK has_managed_popup( HWND hwnd, LPARAM lparam )
173 struct has_popup_result *result = (struct has_popup_result *)lparam;
175 if (hwnd == result->hwnd) return FALSE; /* popups are always above owner */
176 if (GetWindow( hwnd, GW_OWNER ) != result->hwnd) return TRUE;
177 result->found = is_managed( hwnd );
178 return !result->found;
181 static BOOL has_owned_popups( HWND hwnd )
183 struct has_popup_result result;
185 result.hwnd = hwnd;
186 result.found = FALSE;
187 EnumWindows( has_managed_popup, (LPARAM)&result );
188 return result.found;
191 /***********************************************************************
192 * is_window_managed
194 * Check if a given window should be managed
196 static BOOL is_window_managed( HWND hwnd, UINT swp_flags, const RECT *window_rect )
198 DWORD style, ex_style;
200 if (!managed_mode) return FALSE;
202 /* child windows are not managed */
203 style = GetWindowLongW( hwnd, GWL_STYLE );
204 if ((style & (WS_CHILD|WS_POPUP)) == WS_CHILD) return FALSE;
205 /* activated windows are managed */
206 if (!(swp_flags & (SWP_NOACTIVATE|SWP_HIDEWINDOW))) return TRUE;
207 if (hwnd == GetActiveWindow()) return TRUE;
208 /* windows with caption are managed */
209 if ((style & WS_CAPTION) == WS_CAPTION) return TRUE;
210 /* windows with thick frame are managed */
211 if (style & WS_THICKFRAME) return TRUE;
212 if (style & WS_POPUP)
214 HMONITOR hmon;
215 MONITORINFO mi;
217 /* popup with sysmenu == caption are managed */
218 if (style & WS_SYSMENU) return TRUE;
219 /* full-screen popup windows are managed */
220 hmon = MonitorFromWindow( hwnd, MONITOR_DEFAULTTOPRIMARY );
221 mi.cbSize = sizeof( mi );
222 GetMonitorInfoW( hmon, &mi );
223 if (window_rect->left <= mi.rcWork.left && window_rect->right >= mi.rcWork.right &&
224 window_rect->top <= mi.rcWork.top && window_rect->bottom >= mi.rcWork.bottom)
225 return TRUE;
227 /* application windows are managed */
228 ex_style = GetWindowLongW( hwnd, GWL_EXSTYLE );
229 if (ex_style & WS_EX_APPWINDOW) return TRUE;
230 /* windows that own popups are managed */
231 if (has_owned_popups( hwnd )) return TRUE;
232 /* default: not managed */
233 return FALSE;
237 /***********************************************************************
238 * is_window_resizable
240 * Check if window should be made resizable by the window manager
242 static inline BOOL is_window_resizable( struct x11drv_win_data *data, DWORD style )
244 if (style & WS_THICKFRAME) return TRUE;
245 /* Metacity needs the window to be resizable to make it fullscreen */
246 return is_window_rect_fullscreen( &data->whole_rect );
250 /***********************************************************************
251 * get_mwm_decorations
253 static unsigned long get_mwm_decorations( struct x11drv_win_data *data,
254 DWORD style, DWORD ex_style )
256 unsigned long ret = 0;
258 if (!decorated_mode) return 0;
260 if (IsRectEmpty( &data->window_rect )) return 0;
261 if (data->shaped) return 0;
263 if (ex_style & WS_EX_TOOLWINDOW) return 0;
265 if ((style & WS_CAPTION) == WS_CAPTION)
267 ret |= MWM_DECOR_TITLE | MWM_DECOR_BORDER;
268 if (style & WS_SYSMENU) ret |= MWM_DECOR_MENU;
269 if (style & WS_MINIMIZEBOX) ret |= MWM_DECOR_MINIMIZE;
270 if (style & WS_MAXIMIZEBOX) ret |= MWM_DECOR_MAXIMIZE;
272 if (ex_style & WS_EX_DLGMODALFRAME) ret |= MWM_DECOR_BORDER;
273 else if (style & WS_THICKFRAME) ret |= MWM_DECOR_BORDER | MWM_DECOR_RESIZEH;
274 else if ((style & (WS_DLGFRAME|WS_BORDER)) == WS_DLGFRAME) ret |= MWM_DECOR_BORDER;
275 return ret;
279 /***********************************************************************
280 * get_x11_rect_offset
282 * Helper for X11DRV_window_to_X_rect and X11DRV_X_to_window_rect.
284 static void get_x11_rect_offset( struct x11drv_win_data *data, RECT *rect )
286 DWORD style, ex_style, style_mask = 0, ex_style_mask = 0;
287 unsigned long decor;
289 rect->top = rect->bottom = rect->left = rect->right = 0;
291 style = GetWindowLongW( data->hwnd, GWL_STYLE );
292 ex_style = GetWindowLongW( data->hwnd, GWL_EXSTYLE );
293 decor = get_mwm_decorations( data, style, ex_style );
295 if (decor & MWM_DECOR_TITLE) style_mask |= WS_CAPTION;
296 if (decor & MWM_DECOR_BORDER)
298 style_mask |= WS_DLGFRAME | WS_THICKFRAME;
299 ex_style_mask |= WS_EX_DLGMODALFRAME;
302 AdjustWindowRectEx( rect, style & style_mask, FALSE, ex_style & ex_style_mask );
306 /***********************************************************************
307 * get_window_attributes
309 * Fill the window attributes structure for an X window.
311 static int get_window_attributes( Display *display, struct x11drv_win_data *data,
312 XSetWindowAttributes *attr )
314 attr->override_redirect = !data->managed;
315 attr->colormap = X11DRV_PALETTE_PaletteXColormap;
316 attr->save_under = ((GetClassLongW( data->hwnd, GCL_STYLE ) & CS_SAVEBITS) != 0);
317 attr->bit_gravity = NorthWestGravity;
318 attr->win_gravity = StaticGravity;
319 attr->backing_store = NotUseful;
320 attr->event_mask = (ExposureMask | PointerMotionMask |
321 ButtonPressMask | ButtonReleaseMask | EnterWindowMask |
322 KeyPressMask | KeyReleaseMask | FocusChangeMask |
323 KeymapStateMask | StructureNotifyMask);
324 if (data->managed) attr->event_mask |= PropertyChangeMask;
326 return (CWOverrideRedirect | CWSaveUnder | CWColormap |
327 CWEventMask | CWBitGravity | CWBackingStore);
331 /***********************************************************************
332 * sync_window_style
334 * Change the X window attributes when the window style has changed.
336 static void sync_window_style( Display *display, struct x11drv_win_data *data )
338 if (data->whole_window != root_window)
340 XSetWindowAttributes attr;
341 int mask = get_window_attributes( display, data, &attr );
343 XChangeWindowAttributes( display, data->whole_window, mask, &attr );
348 /***********************************************************************
349 * sync_window_region
351 * Update the X11 window region.
353 static void sync_window_region( Display *display, struct x11drv_win_data *data, HRGN win_region )
355 #ifdef HAVE_LIBXSHAPE
356 HRGN hrgn = win_region;
358 if (!data->whole_window) return;
359 data->shaped = FALSE;
361 if (IsRectEmpty( &data->window_rect )) /* set an empty shape */
363 static XRectangle empty_rect;
364 XShapeCombineRectangles( display, data->whole_window, ShapeBounding, 0, 0,
365 &empty_rect, 1, ShapeSet, YXBanded );
366 return;
369 if (hrgn == (HRGN)1) /* hack: win_region == 1 means retrieve region from server */
371 if (!(hrgn = CreateRectRgn( 0, 0, 0, 0 ))) return;
372 if (GetWindowRgn( data->hwnd, hrgn ) == ERROR)
374 DeleteObject( hrgn );
375 hrgn = 0;
379 if (!hrgn)
381 XShapeCombineMask( display, data->whole_window, ShapeBounding, 0, 0, None, ShapeSet );
383 else
385 RGNDATA *pRegionData;
387 if (GetWindowLongW( data->hwnd, GWL_EXSTYLE ) & WS_EX_LAYOUTRTL) MirrorRgn( data->hwnd, hrgn );
388 if ((pRegionData = X11DRV_GetRegionData( hrgn, 0 )))
390 XShapeCombineRectangles( display, data->whole_window, ShapeBounding,
391 data->window_rect.left - data->whole_rect.left,
392 data->window_rect.top - data->whole_rect.top,
393 (XRectangle *)pRegionData->Buffer,
394 pRegionData->rdh.nCount, ShapeSet, YXBanded );
395 HeapFree(GetProcessHeap(), 0, pRegionData);
396 data->shaped = TRUE;
399 if (hrgn && hrgn != win_region) DeleteObject( hrgn );
400 #endif /* HAVE_LIBXSHAPE */
404 /***********************************************************************
405 * sync_window_opacity
407 static void sync_window_opacity( Display *display, Window win,
408 COLORREF key, BYTE alpha, DWORD flags )
410 unsigned long opacity = 0xffffffff;
412 if (flags & LWA_ALPHA) opacity = (0xffffffff / 0xff) * alpha;
414 if (opacity == 0xffffffff)
415 XDeleteProperty( display, win, x11drv_atom(_NET_WM_WINDOW_OPACITY) );
416 else
417 XChangeProperty( display, win, x11drv_atom(_NET_WM_WINDOW_OPACITY),
418 XA_CARDINAL, 32, PropModeReplace, (unsigned char *)&opacity, 1 );
422 /***********************************************************************
423 * sync_window_text
425 static void sync_window_text( Display *display, Window win, const WCHAR *text )
427 UINT count;
428 char *buffer, *utf8_buffer;
429 XTextProperty prop;
431 /* allocate new buffer for window text */
432 count = WideCharToMultiByte(CP_UNIXCP, 0, text, -1, NULL, 0, NULL, NULL);
433 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, count ))) return;
434 WideCharToMultiByte(CP_UNIXCP, 0, text, -1, buffer, count, NULL, NULL);
436 count = WideCharToMultiByte(CP_UTF8, 0, text, strlenW(text), NULL, 0, NULL, NULL);
437 if (!(utf8_buffer = HeapAlloc( GetProcessHeap(), 0, count )))
439 HeapFree( GetProcessHeap(), 0, buffer );
440 return;
442 WideCharToMultiByte(CP_UTF8, 0, text, strlenW(text), utf8_buffer, count, NULL, NULL);
444 if (XmbTextListToTextProperty( display, &buffer, 1, XStdICCTextStyle, &prop ) == Success)
446 XSetWMName( display, win, &prop );
447 XSetWMIconName( display, win, &prop );
448 XFree( prop.value );
451 Implements a NET_WM UTF-8 title. It should be without a trailing \0,
452 according to the standard
453 ( http://www.pps.jussieu.fr/~jch/software/UTF8_STRING/UTF8_STRING.text ).
455 XChangeProperty( display, win, x11drv_atom(_NET_WM_NAME), x11drv_atom(UTF8_STRING),
456 8, PropModeReplace, (unsigned char *) utf8_buffer, count);
458 HeapFree( GetProcessHeap(), 0, utf8_buffer );
459 HeapFree( GetProcessHeap(), 0, buffer );
463 /***********************************************************************
464 * get_bitmap_argb
466 * Return the bitmap bits in ARGB format. Helper for setting icon hints.
468 static unsigned long *get_bitmap_argb( HDC hdc, HBITMAP color, HBITMAP mask, unsigned int *size )
470 char buffer[FIELD_OFFSET( BITMAPINFO, bmiColors[256] )];
471 BITMAPINFO *info = (BITMAPINFO *)buffer;
472 BITMAP bm;
473 unsigned int *ptr, *bits = NULL;
474 unsigned char *mask_bits = NULL;
475 int i, j, has_alpha = 0;
477 if (!GetObjectW( color, sizeof(bm), &bm )) return NULL;
478 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
479 info->bmiHeader.biWidth = bm.bmWidth;
480 info->bmiHeader.biHeight = -bm.bmHeight;
481 info->bmiHeader.biPlanes = 1;
482 info->bmiHeader.biBitCount = 32;
483 info->bmiHeader.biCompression = BI_RGB;
484 info->bmiHeader.biSizeImage = bm.bmWidth * bm.bmHeight * 4;
485 info->bmiHeader.biXPelsPerMeter = 0;
486 info->bmiHeader.biYPelsPerMeter = 0;
487 info->bmiHeader.biClrUsed = 0;
488 info->bmiHeader.biClrImportant = 0;
489 *size = bm.bmWidth * bm.bmHeight + 2;
490 if (!(bits = HeapAlloc( GetProcessHeap(), 0, *size * sizeof(long) ))) goto failed;
491 if (!GetDIBits( hdc, color, 0, bm.bmHeight, bits + 2, info, DIB_RGB_COLORS )) goto failed;
493 bits[0] = bm.bmWidth;
494 bits[1] = bm.bmHeight;
496 for (i = 0; i < bm.bmWidth * bm.bmHeight; i++)
497 if ((has_alpha = (bits[i + 2] & 0xff000000) != 0)) break;
499 if (!has_alpha)
501 unsigned int width_bytes = (bm.bmWidth + 31) / 32 * 4;
502 /* generate alpha channel from the mask */
503 info->bmiHeader.biBitCount = 1;
504 info->bmiHeader.biSizeImage = width_bytes * bm.bmHeight;
505 if (!(mask_bits = HeapAlloc( GetProcessHeap(), 0, info->bmiHeader.biSizeImage ))) goto failed;
506 if (!GetDIBits( hdc, mask, 0, bm.bmHeight, mask_bits, info, DIB_RGB_COLORS )) goto failed;
507 ptr = bits + 2;
508 for (i = 0; i < bm.bmHeight; i++)
509 for (j = 0; j < bm.bmWidth; j++, ptr++)
510 if (!((mask_bits[i * width_bytes + j / 8] << (j % 8)) & 0x80)) *ptr |= 0xff000000;
511 HeapFree( GetProcessHeap(), 0, mask_bits );
514 /* convert to array of longs */
515 if (bits && sizeof(long) > sizeof(int))
516 for (i = *size - 1; i >= 0; i--) ((unsigned long *)bits)[i] = bits[i];
518 return (unsigned long *)bits;
520 failed:
521 HeapFree( GetProcessHeap(), 0, bits );
522 HeapFree( GetProcessHeap(), 0, mask_bits );
523 return NULL;
527 /***********************************************************************
528 * create_icon_pixmaps
530 static BOOL create_icon_pixmaps( HDC hdc, const ICONINFO *icon, Pixmap *icon_ret, Pixmap *mask_ret )
532 char buffer[FIELD_OFFSET( BITMAPINFO, bmiColors[256] )];
533 BITMAPINFO *info = (BITMAPINFO *)buffer;
534 XVisualInfo vis;
535 struct gdi_image_bits bits;
536 Pixmap color_pixmap = 0, mask_pixmap = 0;
537 int i, lines;
539 bits.ptr = NULL;
540 bits.free = NULL;
541 bits.is_copy = TRUE;
543 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
544 info->bmiHeader.biBitCount = 0;
545 if (!(lines = GetDIBits( hdc, icon->hbmColor, 0, 0, NULL, info, DIB_RGB_COLORS ))) goto failed;
546 if (!(bits.ptr = HeapAlloc( GetProcessHeap(), 0, info->bmiHeader.biSizeImage ))) goto failed;
547 if (!GetDIBits( hdc, icon->hbmColor, 0, lines, bits.ptr, info, DIB_RGB_COLORS )) goto failed;
549 vis.visual = visual;
550 vis.depth = screen_depth;
551 vis.visualid = visual->visualid;
552 vis.class = visual->class;
553 vis.red_mask = visual->red_mask;
554 vis.green_mask = visual->green_mask;
555 vis.blue_mask = visual->blue_mask;
556 color_pixmap = create_pixmap_from_image( hdc, &vis, info, &bits, DIB_RGB_COLORS );
557 HeapFree( GetProcessHeap(), 0, bits.ptr );
558 bits.ptr = NULL;
559 if (!color_pixmap) goto failed;
561 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
562 info->bmiHeader.biBitCount = 0;
563 if (!(lines = GetDIBits( hdc, icon->hbmMask, 0, 0, NULL, info, DIB_RGB_COLORS ))) goto failed;
564 if (!(bits.ptr = HeapAlloc( GetProcessHeap(), 0, info->bmiHeader.biSizeImage ))) goto failed;
565 if (!GetDIBits( hdc, icon->hbmMask, 0, lines, bits.ptr, info, DIB_RGB_COLORS )) goto failed;
567 /* invert the mask */
568 for (i = 0; i < info->bmiHeader.biSizeImage / sizeof(DWORD); i++) ((DWORD *)bits.ptr)[i] ^= ~0u;
570 vis.depth = 1;
571 mask_pixmap = create_pixmap_from_image( hdc, &vis, info, &bits, DIB_RGB_COLORS );
572 HeapFree( GetProcessHeap(), 0, bits.ptr );
573 bits.ptr = NULL;
574 if (!mask_pixmap) goto failed;
576 *icon_ret = color_pixmap;
577 *mask_ret = mask_pixmap;
578 return TRUE;
580 failed:
581 if (color_pixmap) XFreePixmap( gdi_display, color_pixmap );
582 if (mask_pixmap) XFreePixmap( gdi_display, mask_pixmap );
583 HeapFree( GetProcessHeap(), 0, bits.ptr );
584 return FALSE;
588 /***********************************************************************
589 * fetch_icon_data
591 static void fetch_icon_data( HWND hwnd, HICON icon_big, HICON icon_small )
593 struct x11drv_win_data *data;
594 ICONINFO ii, ii_small;
595 HDC hDC;
596 unsigned int size;
597 unsigned long *bits;
598 Pixmap icon_pixmap, mask_pixmap;
600 if (!icon_big)
602 icon_big = (HICON)SendMessageW( hwnd, WM_GETICON, ICON_BIG, 0 );
603 if (!icon_big) icon_big = (HICON)GetClassLongPtrW( hwnd, GCLP_HICON );
604 if (!icon_big) icon_big = LoadIconW( 0, (LPWSTR)IDI_WINLOGO );
606 if (!icon_small)
608 icon_small = (HICON)SendMessageW( hwnd, WM_GETICON, ICON_SMALL, 0 );
609 if (!icon_small) icon_small = (HICON)GetClassLongPtrW( hwnd, GCLP_HICONSM );
612 if (!GetIconInfo(icon_big, &ii)) return;
614 hDC = CreateCompatibleDC(0);
615 bits = get_bitmap_argb( hDC, ii.hbmColor, ii.hbmMask, &size );
616 if (bits && GetIconInfo( icon_small, &ii_small ))
618 unsigned int size_small;
619 unsigned long *bits_small, *new;
621 if ((bits_small = get_bitmap_argb( hDC, ii_small.hbmColor, ii_small.hbmMask, &size_small )) &&
622 (bits_small[0] != bits[0] || bits_small[1] != bits[1])) /* size must be different */
624 if ((new = HeapReAlloc( GetProcessHeap(), 0, bits,
625 (size + size_small) * sizeof(unsigned long) )))
627 bits = new;
628 memcpy( bits + size, bits_small, size_small * sizeof(unsigned long) );
629 size += size_small;
632 HeapFree( GetProcessHeap(), 0, bits_small );
633 DeleteObject( ii_small.hbmColor );
634 DeleteObject( ii_small.hbmMask );
637 if (!create_icon_pixmaps( hDC, &ii, &icon_pixmap, &mask_pixmap )) icon_pixmap = mask_pixmap = 0;
639 DeleteObject( ii.hbmColor );
640 DeleteObject( ii.hbmMask );
641 DeleteDC(hDC);
643 if ((data = get_win_data( hwnd )))
645 if (data->icon_pixmap) XFreePixmap( gdi_display, data->icon_pixmap );
646 if (data->icon_mask) XFreePixmap( gdi_display, data->icon_mask );
647 HeapFree( GetProcessHeap(), 0, data->icon_bits );
648 data->icon_pixmap = icon_pixmap;
649 data->icon_mask = mask_pixmap;
650 data->icon_bits = bits;
651 data->icon_size = size;
652 release_win_data( data );
654 else
656 if (icon_pixmap) XFreePixmap( gdi_display, icon_pixmap );
657 if (mask_pixmap) XFreePixmap( gdi_display, mask_pixmap );
658 HeapFree( GetProcessHeap(), 0, bits );
663 /***********************************************************************
664 * set_size_hints
666 * set the window size hints
668 static void set_size_hints( Display *display, struct x11drv_win_data *data, DWORD style )
670 XSizeHints* size_hints;
672 if (!(size_hints = XAllocSizeHints())) return;
674 size_hints->win_gravity = StaticGravity;
675 size_hints->flags |= PWinGravity;
677 /* don't update size hints if window is not in normal state */
678 if (!(style & (WS_MINIMIZE | WS_MAXIMIZE)))
680 if (data->hwnd != GetDesktopWindow()) /* don't force position of desktop */
682 size_hints->x = data->whole_rect.left;
683 size_hints->y = data->whole_rect.top;
684 size_hints->flags |= PPosition;
686 else size_hints->win_gravity = NorthWestGravity;
688 if (!is_window_resizable( data, style ))
690 size_hints->max_width = data->whole_rect.right - data->whole_rect.left;
691 size_hints->max_height = data->whole_rect.bottom - data->whole_rect.top;
692 if (size_hints->max_width <= 0 ||size_hints->max_height <= 0)
693 size_hints->max_width = size_hints->max_height = 1;
694 size_hints->min_width = size_hints->max_width;
695 size_hints->min_height = size_hints->max_height;
696 size_hints->flags |= PMinSize | PMaxSize;
699 XSetWMNormalHints( display, data->whole_window, size_hints );
700 XFree( size_hints );
704 /***********************************************************************
705 * set_mwm_hints
707 static void set_mwm_hints( Display *display, struct x11drv_win_data *data, DWORD style, DWORD ex_style )
709 MwmHints mwm_hints;
711 if (data->hwnd == GetDesktopWindow())
713 if (is_desktop_fullscreen()) mwm_hints.decorations = 0;
714 else mwm_hints.decorations = MWM_DECOR_TITLE | MWM_DECOR_BORDER | MWM_DECOR_MENU | MWM_DECOR_MINIMIZE;
715 mwm_hints.functions = MWM_FUNC_MOVE | MWM_FUNC_MINIMIZE | MWM_FUNC_CLOSE;
717 else
719 mwm_hints.decorations = get_mwm_decorations( data, style, ex_style );
720 mwm_hints.functions = MWM_FUNC_MOVE;
721 if (is_window_resizable( data, style )) mwm_hints.functions |= MWM_FUNC_RESIZE;
722 if (!(style & WS_DISABLED))
724 if (style & WS_MINIMIZEBOX) mwm_hints.functions |= MWM_FUNC_MINIMIZE;
725 if (style & WS_MAXIMIZEBOX) mwm_hints.functions |= MWM_FUNC_MAXIMIZE;
726 if (style & WS_SYSMENU) mwm_hints.functions |= MWM_FUNC_CLOSE;
730 TRACE( "%p setting mwm hints to %lx,%lx (style %x exstyle %x)\n",
731 data->hwnd, mwm_hints.decorations, mwm_hints.functions, style, ex_style );
733 mwm_hints.flags = MWM_HINTS_FUNCTIONS | MWM_HINTS_DECORATIONS;
734 XChangeProperty( display, data->whole_window, x11drv_atom(_MOTIF_WM_HINTS),
735 x11drv_atom(_MOTIF_WM_HINTS), 32, PropModeReplace,
736 (unsigned char*)&mwm_hints, sizeof(mwm_hints)/sizeof(long) );
740 /***********************************************************************
741 * set_style_hints
743 static void set_style_hints( Display *display, struct x11drv_win_data *data, DWORD style, DWORD ex_style )
745 Window group_leader = data->whole_window;
746 HWND owner = GetWindow( data->hwnd, GW_OWNER );
747 Window owner_win = X11DRV_get_whole_window( owner );
748 XWMHints *wm_hints;
749 Atom window_type;
751 if (owner_win)
753 XSetTransientForHint( display, data->whole_window, owner_win );
754 group_leader = owner_win;
757 /* Only use dialog type for owned popups. Metacity allows making fullscreen
758 * only normal windows, and doesn't handle correctly TRANSIENT_FOR hint for
759 * dialogs owned by fullscreen windows.
761 if (((style & WS_POPUP) || (ex_style & WS_EX_DLGMODALFRAME)) && owner)
762 window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_DIALOG);
763 else
764 window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_NORMAL);
766 XChangeProperty(display, data->whole_window, x11drv_atom(_NET_WM_WINDOW_TYPE),
767 XA_ATOM, 32, PropModeReplace, (unsigned char*)&window_type, 1);
769 if ((wm_hints = XAllocWMHints()))
771 wm_hints->flags = InputHint | StateHint | WindowGroupHint;
772 wm_hints->input = !use_take_focus && !(style & WS_DISABLED);
773 wm_hints->initial_state = (style & WS_MINIMIZE) ? IconicState : NormalState;
774 wm_hints->window_group = group_leader;
775 if (data->icon_pixmap)
777 wm_hints->icon_pixmap = data->icon_pixmap;
778 wm_hints->icon_mask = data->icon_mask;
779 wm_hints->flags |= IconPixmapHint | IconMaskHint;
781 XSetWMHints( display, data->whole_window, wm_hints );
782 XFree( wm_hints );
785 if (data->icon_bits)
786 XChangeProperty( display, data->whole_window, x11drv_atom(_NET_WM_ICON),
787 XA_CARDINAL, 32, PropModeReplace,
788 (unsigned char *)data->icon_bits, data->icon_size );
789 else
790 XDeleteProperty( display, data->whole_window, x11drv_atom(_NET_WM_ICON) );
795 /***********************************************************************
796 * get_process_name
798 * get the name of the current process for setting class hints
800 static char *get_process_name(void)
802 static char *name;
804 if (!name)
806 WCHAR module[MAX_PATH];
807 DWORD len = GetModuleFileNameW( 0, module, MAX_PATH );
808 if (len && len < MAX_PATH)
810 char *ptr;
811 WCHAR *p, *appname = module;
813 if ((p = strrchrW( appname, '/' ))) appname = p + 1;
814 if ((p = strrchrW( appname, '\\' ))) appname = p + 1;
815 len = WideCharToMultiByte( CP_UNIXCP, 0, appname, -1, NULL, 0, NULL, NULL );
816 if ((ptr = HeapAlloc( GetProcessHeap(), 0, len )))
818 WideCharToMultiByte( CP_UNIXCP, 0, appname, -1, ptr, len, NULL, NULL );
819 name = ptr;
823 return name;
827 /***********************************************************************
828 * set_initial_wm_hints
830 * Set the window manager hints that don't change over the lifetime of a window.
832 static void set_initial_wm_hints( Display *display, Window window )
834 long i;
835 Atom protocols[3];
836 Atom dndVersion = WINE_XDND_VERSION;
837 XClassHint *class_hints;
838 char *process_name = get_process_name();
840 /* wm protocols */
841 i = 0;
842 protocols[i++] = x11drv_atom(WM_DELETE_WINDOW);
843 protocols[i++] = x11drv_atom(_NET_WM_PING);
844 if (use_take_focus) protocols[i++] = x11drv_atom(WM_TAKE_FOCUS);
845 XChangeProperty( display, window, x11drv_atom(WM_PROTOCOLS),
846 XA_ATOM, 32, PropModeReplace, (unsigned char *)protocols, i );
848 /* class hints */
849 if ((class_hints = XAllocClassHint()))
851 static char wine[] = "Wine";
853 class_hints->res_name = process_name;
854 class_hints->res_class = wine;
855 XSetClassHint( display, window, class_hints );
856 XFree( class_hints );
859 /* set the WM_CLIENT_MACHINE and WM_LOCALE_NAME properties */
860 XSetWMProperties(display, window, NULL, NULL, NULL, 0, NULL, NULL, NULL);
861 /* set the pid. together, these properties are needed so the window manager can kill us if we freeze */
862 i = getpid();
863 XChangeProperty(display, window, x11drv_atom(_NET_WM_PID),
864 XA_CARDINAL, 32, PropModeReplace, (unsigned char *)&i, 1);
866 XChangeProperty( display, window, x11drv_atom(XdndAware),
867 XA_ATOM, 32, PropModeReplace, (unsigned char*)&dndVersion, 1 );
869 update_user_time( 0 ); /* make sure that the user time window exists */
870 if (user_time_window)
871 XChangeProperty( display, window, x11drv_atom(_NET_WM_USER_TIME_WINDOW),
872 XA_WINDOW, 32, PropModeReplace, (unsigned char *)&user_time_window, 1 );
876 /***********************************************************************
877 * make_owner_managed
879 * If the window is managed, make sure its owner window is too.
881 static void make_owner_managed( HWND hwnd )
883 HWND owner;
885 if (!(owner = GetWindow( hwnd, GW_OWNER ))) return;
886 if (is_managed( owner )) return;
887 if (!is_managed( hwnd )) return;
889 SetWindowPos( owner, 0, 0, 0, 0, 0,
890 SWP_NOACTIVATE | SWP_NOZORDER | SWP_NOSIZE | SWP_NOMOVE |
891 SWP_NOREDRAW | SWP_DEFERERASE | SWP_NOSENDCHANGING | SWP_STATECHANGED );
895 /***********************************************************************
896 * set_wm_hints
898 * Set all the window manager hints for a window.
900 static void set_wm_hints( Display *display, struct x11drv_win_data *data )
902 DWORD style, ex_style;
904 if (data->hwnd == GetDesktopWindow())
906 /* force some styles for the desktop to get the correct decorations */
907 style = WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX;
908 ex_style = WS_EX_APPWINDOW;
910 else
912 style = GetWindowLongW( data->hwnd, GWL_STYLE );
913 ex_style = GetWindowLongW( data->hwnd, GWL_EXSTYLE );
916 set_size_hints( display, data, style );
917 set_mwm_hints( display, data, style, ex_style );
918 set_style_hints( display, data, style, ex_style );
922 /***********************************************************************
923 * init_clip_window
925 Window init_clip_window(void)
927 struct x11drv_thread_data *data = x11drv_init_thread_data();
929 if (!data->clip_window &&
930 (data->clip_window = (Window)GetPropA( GetDesktopWindow(), clip_window_prop )))
932 XSelectInput( data->display, data->clip_window, StructureNotifyMask );
934 return data->clip_window;
938 /***********************************************************************
939 * update_user_time
941 void update_user_time( Time time )
943 if (!user_time_window)
945 Window win = XCreateWindow( gdi_display, root_window, -1, -1, 1, 1, 0, 0, InputOnly,
946 DefaultVisual(gdi_display,DefaultScreen(gdi_display)), 0, NULL );
947 if (InterlockedCompareExchangePointer( (void **)&user_time_window, (void *)win, 0 ))
948 XDestroyWindow( gdi_display, win );
949 TRACE( "user time window %lx\n", user_time_window );
952 if (!time) return;
953 XLockDisplay( gdi_display );
954 if (!last_user_time || (long)(time - last_user_time) > 0)
956 last_user_time = time;
957 XChangeProperty( gdi_display, user_time_window, x11drv_atom(_NET_WM_USER_TIME),
958 XA_CARDINAL, 32, PropModeReplace, (unsigned char *)&time, 1 );
960 XUnlockDisplay( gdi_display );
963 /***********************************************************************
964 * update_net_wm_states
966 void update_net_wm_states( Display *display, struct x11drv_win_data *data )
968 static const unsigned int state_atoms[NB_NET_WM_STATES] =
970 XATOM__NET_WM_STATE_FULLSCREEN,
971 XATOM__NET_WM_STATE_ABOVE,
972 XATOM__NET_WM_STATE_MAXIMIZED_VERT,
973 XATOM__NET_WM_STATE_SKIP_PAGER,
974 XATOM__NET_WM_STATE_SKIP_TASKBAR
977 DWORD i, style, ex_style, new_state = 0;
979 if (!data->managed) return;
980 if (data->whole_window == root_window) return;
982 style = GetWindowLongW( data->hwnd, GWL_STYLE );
983 if (is_window_rect_fullscreen( &data->whole_rect ))
985 if ((style & WS_MAXIMIZE) && (style & WS_CAPTION) == WS_CAPTION)
986 new_state |= (1 << NET_WM_STATE_MAXIMIZED);
987 else if (!(style & WS_MINIMIZE))
988 new_state |= (1 << NET_WM_STATE_FULLSCREEN);
990 else if (style & WS_MAXIMIZE)
991 new_state |= (1 << NET_WM_STATE_MAXIMIZED);
993 ex_style = GetWindowLongW( data->hwnd, GWL_EXSTYLE );
994 if (ex_style & WS_EX_TOPMOST)
995 new_state |= (1 << NET_WM_STATE_ABOVE);
996 if (ex_style & (WS_EX_TOOLWINDOW | WS_EX_NOACTIVATE))
997 new_state |= (1 << NET_WM_STATE_SKIP_TASKBAR) | (1 << NET_WM_STATE_SKIP_PAGER);
998 if (!(ex_style & WS_EX_APPWINDOW) && GetWindow( data->hwnd, GW_OWNER ))
999 new_state |= (1 << NET_WM_STATE_SKIP_TASKBAR);
1001 if (!data->mapped) /* set the _NET_WM_STATE atom directly */
1003 Atom atoms[NB_NET_WM_STATES+1];
1004 DWORD count;
1006 for (i = count = 0; i < NB_NET_WM_STATES; i++)
1008 if (!(new_state & (1 << i))) continue;
1009 TRACE( "setting wm state %u for unmapped window %p/%lx\n",
1010 i, data->hwnd, data->whole_window );
1011 atoms[count++] = X11DRV_Atoms[state_atoms[i] - FIRST_XATOM];
1012 if (state_atoms[i] == XATOM__NET_WM_STATE_MAXIMIZED_VERT)
1013 atoms[count++] = x11drv_atom(_NET_WM_STATE_MAXIMIZED_HORZ);
1015 XChangeProperty( display, data->whole_window, x11drv_atom(_NET_WM_STATE), XA_ATOM,
1016 32, PropModeReplace, (unsigned char *)atoms, count );
1018 else /* ask the window manager to do it for us */
1020 XEvent xev;
1022 xev.xclient.type = ClientMessage;
1023 xev.xclient.window = data->whole_window;
1024 xev.xclient.message_type = x11drv_atom(_NET_WM_STATE);
1025 xev.xclient.serial = 0;
1026 xev.xclient.display = display;
1027 xev.xclient.send_event = True;
1028 xev.xclient.format = 32;
1029 xev.xclient.data.l[3] = 1;
1031 for (i = 0; i < NB_NET_WM_STATES; i++)
1033 if (!((data->net_wm_state ^ new_state) & (1 << i))) continue; /* unchanged */
1035 TRACE( "setting wm state %u for window %p/%lx to %u prev %u\n",
1036 i, data->hwnd, data->whole_window,
1037 (new_state & (1 << i)) != 0, (data->net_wm_state & (1 << i)) != 0 );
1039 xev.xclient.data.l[0] = (new_state & (1 << i)) ? _NET_WM_STATE_ADD : _NET_WM_STATE_REMOVE;
1040 xev.xclient.data.l[1] = X11DRV_Atoms[state_atoms[i] - FIRST_XATOM];
1041 xev.xclient.data.l[2] = ((state_atoms[i] == XATOM__NET_WM_STATE_MAXIMIZED_VERT) ?
1042 x11drv_atom(_NET_WM_STATE_MAXIMIZED_HORZ) : 0);
1043 XSendEvent( display, root_window, False,
1044 SubstructureRedirectMask | SubstructureNotifyMask, &xev );
1047 data->net_wm_state = new_state;
1051 /***********************************************************************
1052 * set_xembed_flags
1054 static void set_xembed_flags( Display *display, struct x11drv_win_data *data, unsigned long flags )
1056 unsigned long info[2];
1058 if (!data->whole_window) return;
1060 info[0] = 0; /* protocol version */
1061 info[1] = flags;
1062 XChangeProperty( display, data->whole_window, x11drv_atom(_XEMBED_INFO),
1063 x11drv_atom(_XEMBED_INFO), 32, PropModeReplace, (unsigned char*)info, 2 );
1067 /***********************************************************************
1068 * map_window
1070 static void map_window( HWND hwnd, DWORD new_style )
1072 Display *display = thread_display();
1073 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
1075 TRACE( "win %p/%lx\n", data->hwnd, data->whole_window );
1077 remove_startup_notification( display, data->whole_window );
1079 wait_for_withdrawn_state( hwnd, TRUE );
1081 if (!data->embedded)
1083 update_net_wm_states( display, data );
1084 sync_window_style( display, data );
1085 XMapWindow( display, data->whole_window );
1087 else set_xembed_flags( display, data, XEMBED_MAPPED );
1089 data->mapped = TRUE;
1090 data->iconic = (new_style & WS_MINIMIZE) != 0;
1094 /***********************************************************************
1095 * unmap_window
1097 static void unmap_window( HWND hwnd )
1099 Display *display = thread_display();
1100 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
1102 TRACE( "win %p/%lx\n", data->hwnd, data->whole_window );
1104 if (!data->embedded)
1106 wait_for_withdrawn_state( hwnd, FALSE );
1107 if (data->managed) XWithdrawWindow( display, data->whole_window, DefaultScreen(display) );
1108 else XUnmapWindow( display, data->whole_window );
1110 else set_xembed_flags( display, data, 0 );
1112 data->mapped = FALSE;
1113 data->net_wm_state = 0;
1117 /***********************************************************************
1118 * make_window_embedded
1120 void make_window_embedded( HWND hwnd )
1122 Display *display = thread_display();
1123 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
1124 BOOL was_mapped = data->mapped;
1126 /* the window cannot be mapped before being embedded */
1127 if (data->mapped) unmap_window( hwnd );
1129 data->embedded = TRUE;
1130 data->managed = TRUE;
1131 SetPropA( hwnd, managed_prop, (HANDLE)1 );
1132 sync_window_style( display, data );
1134 if (was_mapped)
1135 map_window( hwnd, 0 );
1136 else
1137 set_xembed_flags( display, data, 0 );
1141 /***********************************************************************
1142 * X11DRV_window_to_X_rect
1144 * Convert a rect from client to X window coordinates
1146 static void X11DRV_window_to_X_rect( struct x11drv_win_data *data, RECT *rect )
1148 RECT rc;
1150 if (!data->managed) return;
1151 if (IsRectEmpty( rect )) return;
1153 get_x11_rect_offset( data, &rc );
1155 rect->left -= rc.left;
1156 rect->right -= rc.right;
1157 rect->top -= rc.top;
1158 rect->bottom -= rc.bottom;
1159 if (rect->top >= rect->bottom) rect->bottom = rect->top + 1;
1160 if (rect->left >= rect->right) rect->right = rect->left + 1;
1164 /***********************************************************************
1165 * X11DRV_X_to_window_rect
1167 * Opposite of X11DRV_window_to_X_rect
1169 void X11DRV_X_to_window_rect( struct x11drv_win_data *data, RECT *rect )
1171 RECT rc;
1173 if (!data->managed) return;
1174 if (IsRectEmpty( rect )) return;
1176 get_x11_rect_offset( data, &rc );
1178 rect->left += rc.left;
1179 rect->right += rc.right;
1180 rect->top += rc.top;
1181 rect->bottom += rc.bottom;
1182 if (rect->top >= rect->bottom) rect->bottom = rect->top + 1;
1183 if (rect->left >= rect->right) rect->right = rect->left + 1;
1187 /***********************************************************************
1188 * sync_window_position
1190 * Synchronize the X window position with the Windows one
1192 static void sync_window_position( Display *display, struct x11drv_win_data *data,
1193 UINT swp_flags, const RECT *old_window_rect,
1194 const RECT *old_whole_rect, const RECT *old_client_rect )
1196 DWORD style = GetWindowLongW( data->hwnd, GWL_STYLE );
1197 DWORD ex_style = GetWindowLongW( data->hwnd, GWL_EXSTYLE );
1198 XWindowChanges changes;
1199 unsigned int mask = 0;
1201 if (data->managed && data->iconic) return;
1203 /* resizing a managed maximized window is not allowed */
1204 if (!(style & WS_MAXIMIZE) || !data->managed)
1206 changes.width = data->whole_rect.right - data->whole_rect.left;
1207 changes.height = data->whole_rect.bottom - data->whole_rect.top;
1208 /* if window rect is empty force size to 1x1 */
1209 if (changes.width <= 0 || changes.height <= 0) changes.width = changes.height = 1;
1210 if (changes.width > 65535) changes.width = 65535;
1211 if (changes.height > 65535) changes.height = 65535;
1212 mask |= CWWidth | CWHeight;
1215 /* only the size is allowed to change for the desktop window */
1216 if (data->whole_window != root_window)
1218 changes.x = data->whole_rect.left - virtual_screen_rect.left;
1219 changes.y = data->whole_rect.top - virtual_screen_rect.top;
1220 mask |= CWX | CWY;
1223 if (!(swp_flags & SWP_NOZORDER) || (swp_flags & SWP_SHOWWINDOW))
1225 /* find window that this one must be after */
1226 HWND prev = GetWindow( data->hwnd, GW_HWNDPREV );
1227 while (prev && !(GetWindowLongW( prev, GWL_STYLE ) & WS_VISIBLE))
1228 prev = GetWindow( prev, GW_HWNDPREV );
1229 if (!prev) /* top child */
1231 changes.stack_mode = Above;
1232 mask |= CWStackMode;
1234 /* should use stack_mode Below but most window managers don't get it right */
1235 /* and Above with a sibling doesn't work so well either, so we ignore it */
1238 set_size_hints( display, data, style );
1239 set_mwm_hints( display, data, style, ex_style );
1240 data->configure_serial = NextRequest( display );
1241 XReconfigureWMWindow( display, data->whole_window,
1242 DefaultScreen(display), mask, &changes );
1243 #ifdef HAVE_LIBXSHAPE
1244 if (IsRectEmpty( old_window_rect ) != IsRectEmpty( &data->window_rect ))
1245 sync_window_region( display, data, (HRGN)1 );
1246 if (data->shaped)
1248 int old_x_offset = old_window_rect->left - old_whole_rect->left;
1249 int old_y_offset = old_window_rect->top - old_whole_rect->top;
1250 int new_x_offset = data->window_rect.left - data->whole_rect.left;
1251 int new_y_offset = data->window_rect.top - data->whole_rect.top;
1252 if (old_x_offset != new_x_offset || old_y_offset != new_y_offset)
1253 XShapeOffsetShape( display, data->whole_window, ShapeBounding,
1254 new_x_offset - old_x_offset, new_y_offset - old_y_offset );
1256 #endif
1258 TRACE( "win %p/%lx pos %d,%d,%dx%d after %lx changes=%x serial=%lu\n",
1259 data->hwnd, data->whole_window, data->whole_rect.left, data->whole_rect.top,
1260 data->whole_rect.right - data->whole_rect.left,
1261 data->whole_rect.bottom - data->whole_rect.top,
1262 changes.sibling, mask, data->configure_serial );
1266 /***********************************************************************
1267 * move_window_bits
1269 * Move the window bits when a window is moved.
1271 static void move_window_bits( HWND hwnd, const RECT *old_rect, const RECT *new_rect,
1272 const RECT *old_client_rect )
1274 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
1275 RECT src_rect = *old_rect;
1276 RECT dst_rect = *new_rect;
1277 HDC hdc_src, hdc_dst;
1278 INT code;
1279 HRGN rgn;
1280 HWND parent = 0;
1282 if (!data->whole_window)
1284 OffsetRect( &dst_rect, -data->window_rect.left, -data->window_rect.top );
1285 parent = GetAncestor( data->hwnd, GA_PARENT );
1286 hdc_src = GetDCEx( parent, 0, DCX_CACHE );
1287 hdc_dst = GetDCEx( data->hwnd, 0, DCX_CACHE | DCX_WINDOW );
1289 else
1291 OffsetRect( &dst_rect, -data->client_rect.left, -data->client_rect.top );
1292 /* make src rect relative to the old position of the window */
1293 OffsetRect( &src_rect, -old_client_rect->left, -old_client_rect->top );
1294 if (dst_rect.left == src_rect.left && dst_rect.top == src_rect.top) return;
1295 hdc_src = hdc_dst = GetDCEx( data->hwnd, 0, DCX_CACHE );
1298 rgn = CreateRectRgnIndirect( &dst_rect );
1299 SelectClipRgn( hdc_dst, rgn );
1300 DeleteObject( rgn );
1301 ExcludeUpdateRgn( hdc_dst, data->hwnd );
1303 code = X11DRV_START_EXPOSURES;
1304 ExtEscape( hdc_dst, X11DRV_ESCAPE, sizeof(code), (LPSTR)&code, 0, NULL );
1306 TRACE( "copying bits for win %p/%lx %s -> %s\n",
1307 data->hwnd, data->whole_window, wine_dbgstr_rect(&src_rect), wine_dbgstr_rect(&dst_rect) );
1308 BitBlt( hdc_dst, dst_rect.left, dst_rect.top,
1309 dst_rect.right - dst_rect.left, dst_rect.bottom - dst_rect.top,
1310 hdc_src, src_rect.left, src_rect.top, SRCCOPY );
1312 rgn = 0;
1313 code = X11DRV_END_EXPOSURES;
1314 ExtEscape( hdc_dst, X11DRV_ESCAPE, sizeof(code), (LPSTR)&code, sizeof(rgn), (LPSTR)&rgn );
1316 ReleaseDC( data->hwnd, hdc_dst );
1317 if (hdc_src != hdc_dst) ReleaseDC( parent, hdc_src );
1319 if (rgn)
1321 if (!data->whole_window)
1323 /* map region to client rect since we are using DCX_WINDOW */
1324 OffsetRgn( rgn, data->window_rect.left - data->client_rect.left,
1325 data->window_rect.top - data->client_rect.top );
1326 RedrawWindow( data->hwnd, NULL, rgn,
1327 RDW_INVALIDATE | RDW_FRAME | RDW_ERASE | RDW_ALLCHILDREN );
1329 else RedrawWindow( data->hwnd, NULL, rgn, RDW_INVALIDATE | RDW_ERASE | RDW_ALLCHILDREN );
1330 DeleteObject( rgn );
1335 /**********************************************************************
1336 * create_whole_window
1338 * Create the whole X window for a given window
1340 static void create_whole_window( Display *display, struct x11drv_win_data *data )
1342 int cx, cy, mask;
1343 XSetWindowAttributes attr;
1344 WCHAR text[1024];
1345 COLORREF key;
1346 BYTE alpha;
1347 DWORD layered_flags;
1348 HRGN win_rgn;
1350 if (!data->managed && is_window_managed( data->hwnd, SWP_NOACTIVATE, &data->window_rect ))
1352 TRACE( "making win %p/%lx managed\n", data->hwnd, data->whole_window );
1353 data->managed = TRUE;
1354 SetPropA( data->hwnd, managed_prop, (HANDLE)1 );
1357 if ((win_rgn = CreateRectRgn( 0, 0, 0, 0 )) && GetWindowRgn( data->hwnd, win_rgn ) == ERROR)
1359 DeleteObject( win_rgn );
1360 win_rgn = 0;
1362 data->shaped = (win_rgn != 0);
1364 mask = get_window_attributes( display, data, &attr );
1366 data->whole_rect = data->window_rect;
1367 X11DRV_window_to_X_rect( data, &data->whole_rect );
1368 if (!(cx = data->whole_rect.right - data->whole_rect.left)) cx = 1;
1369 else if (cx > 65535) cx = 65535;
1370 if (!(cy = data->whole_rect.bottom - data->whole_rect.top)) cy = 1;
1371 else if (cy > 65535) cy = 65535;
1373 data->whole_window = XCreateWindow( display, root_window,
1374 data->whole_rect.left - virtual_screen_rect.left,
1375 data->whole_rect.top - virtual_screen_rect.top,
1376 cx, cy, 0, screen_depth, InputOutput,
1377 visual, mask, &attr );
1378 if (!data->whole_window) goto done;
1380 set_initial_wm_hints( display, data->whole_window );
1381 set_wm_hints( display, data );
1383 XSaveContext( display, data->whole_window, winContext, (char *)data->hwnd );
1384 SetPropA( data->hwnd, whole_window_prop, (HANDLE)data->whole_window );
1386 /* set the window text */
1387 if (!InternalGetWindowText( data->hwnd, text, sizeof(text)/sizeof(WCHAR) )) text[0] = 0;
1388 sync_window_text( display, data->whole_window, text );
1390 /* set the window region */
1391 if (win_rgn || IsRectEmpty( &data->window_rect )) sync_window_region( display, data, win_rgn );
1393 /* set the window opacity */
1394 if (!GetLayeredWindowAttributes( data->hwnd, &key, &alpha, &layered_flags )) layered_flags = 0;
1395 sync_window_opacity( display, data->whole_window, key, alpha, layered_flags );
1397 XFlush( display ); /* make sure the window exists before we start painting to it */
1399 sync_window_cursor( data->whole_window );
1401 done:
1402 if (win_rgn) DeleteObject( win_rgn );
1406 /**********************************************************************
1407 * destroy_whole_window
1409 * Destroy the whole X window for a given window.
1411 static void destroy_whole_window( Display *display, struct x11drv_win_data *data, BOOL already_destroyed )
1413 if (!data->whole_window)
1415 if (data->embedded)
1417 Window xwin = (Window)GetPropA( data->hwnd, foreign_window_prop );
1418 if (xwin)
1420 if (!already_destroyed) XSelectInput( display, xwin, 0 );
1421 XDeleteContext( display, xwin, winContext );
1422 RemovePropA( data->hwnd, foreign_window_prop );
1425 return;
1429 TRACE( "win %p xwin %lx\n", data->hwnd, data->whole_window );
1430 XDeleteContext( display, data->whole_window, winContext );
1431 if (!already_destroyed) XDestroyWindow( display, data->whole_window );
1432 data->whole_window = 0;
1433 data->wm_state = WithdrawnState;
1434 data->net_wm_state = 0;
1435 data->mapped = FALSE;
1436 if (data->xic)
1438 XUnsetICFocus( data->xic );
1439 XDestroyIC( data->xic );
1440 data->xic = 0;
1442 /* Outlook stops processing messages after destroying a dialog, so we need an explicit flush */
1443 XFlush( display );
1444 if (data->surface) window_surface_release( data->surface );
1445 data->surface = NULL;
1446 RemovePropA( data->hwnd, whole_window_prop );
1450 /*****************************************************************
1451 * SetWindowText (X11DRV.@)
1453 void CDECL X11DRV_SetWindowText( HWND hwnd, LPCWSTR text )
1455 Window win;
1457 if ((win = X11DRV_get_whole_window( hwnd )) && win != DefaultRootWindow(gdi_display))
1459 Display *display = thread_init_display();
1460 sync_window_text( display, win, text );
1465 /***********************************************************************
1466 * SetWindowStyle (X11DRV.@)
1468 * Update the X state of a window to reflect a style change
1470 void CDECL X11DRV_SetWindowStyle( HWND hwnd, INT offset, STYLESTRUCT *style )
1472 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
1473 DWORD changed;
1475 if (hwnd == GetDesktopWindow()) return;
1476 if (!data || !data->whole_window) return;
1478 changed = style->styleNew ^ style->styleOld;
1480 if (offset == GWL_STYLE && (changed & WS_DISABLED)) set_wm_hints( thread_display(), data );
1482 if (offset == GWL_EXSTYLE && (changed & WS_EX_LAYERED)) /* changing WS_EX_LAYERED resets attributes */
1484 sync_window_opacity( thread_display(), data->whole_window, 0, 0, 0 );
1485 if (data->surface) set_surface_color_key( data->surface, CLR_INVALID );
1490 /***********************************************************************
1491 * DestroyWindow (X11DRV.@)
1493 void CDECL X11DRV_DestroyWindow( HWND hwnd )
1495 struct x11drv_thread_data *thread_data = x11drv_thread_data();
1496 struct x11drv_win_data *data;
1498 if (!(data = get_win_data( hwnd ))) return;
1500 destroy_whole_window( thread_data->display, data, FALSE );
1501 if (thread_data->last_focus == hwnd) thread_data->last_focus = 0;
1502 if (thread_data->last_xic_hwnd == hwnd) thread_data->last_xic_hwnd = 0;
1503 if (data->icon_pixmap) XFreePixmap( gdi_display, data->icon_pixmap );
1504 if (data->icon_mask) XFreePixmap( gdi_display, data->icon_mask );
1505 HeapFree( GetProcessHeap(), 0, data->icon_bits );
1506 XDeleteContext( gdi_display, (XID)hwnd, win_data_context );
1507 release_win_data( data );
1508 HeapFree( GetProcessHeap(), 0, data );
1509 destroy_gl_drawable( hwnd );
1513 /***********************************************************************
1514 * X11DRV_DestroyNotify
1516 void X11DRV_DestroyNotify( HWND hwnd, XEvent *event )
1518 Display *display = event->xdestroywindow.display;
1519 struct x11drv_win_data *data;
1520 BOOL embedded;
1522 if (!(data = get_win_data( hwnd ))) return;
1523 embedded = data->embedded;
1524 if (!embedded) FIXME( "window %p/%lx destroyed from the outside\n", hwnd, data->whole_window );
1526 destroy_whole_window( display, data, TRUE );
1527 release_win_data( data );
1528 if (embedded) SendMessageW( hwnd, WM_CLOSE, 0, 0 );
1532 static struct x11drv_win_data *alloc_win_data( Display *display, HWND hwnd )
1534 struct x11drv_win_data *data;
1536 if ((data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*data))))
1538 data->hwnd = hwnd;
1539 EnterCriticalSection( &win_data_section );
1540 XSaveContext( gdi_display, (XID)hwnd, win_data_context, (char *)data );
1542 return data;
1546 /* initialize the desktop window id in the desktop manager process */
1547 static BOOL create_desktop_win_data( Display *display, HWND hwnd )
1549 struct x11drv_win_data *data;
1551 if (!(data = alloc_win_data( display, hwnd ))) return FALSE;
1552 data->whole_window = root_window;
1553 data->managed = TRUE;
1554 SetPropA( data->hwnd, managed_prop, (HANDLE)1 );
1555 SetPropA( data->hwnd, whole_window_prop, (HANDLE)root_window );
1556 set_initial_wm_hints( display, root_window );
1557 release_win_data( data );
1558 return TRUE;
1561 /**********************************************************************
1562 * CreateDesktopWindow (X11DRV.@)
1564 BOOL CDECL X11DRV_CreateDesktopWindow( HWND hwnd )
1566 unsigned int width, height;
1568 /* retrieve the real size of the desktop */
1569 SERVER_START_REQ( get_window_rectangles )
1571 req->handle = wine_server_user_handle( hwnd );
1572 req->relative = COORDS_CLIENT;
1573 wine_server_call( req );
1574 width = reply->window.right;
1575 height = reply->window.bottom;
1577 SERVER_END_REQ;
1579 if (!width && !height) /* not initialized yet */
1581 SERVER_START_REQ( set_window_pos )
1583 req->handle = wine_server_user_handle( hwnd );
1584 req->previous = 0;
1585 req->flags = SWP_NOZORDER;
1586 req->window.left = virtual_screen_rect.left;
1587 req->window.top = virtual_screen_rect.top;
1588 req->window.right = virtual_screen_rect.right;
1589 req->window.bottom = virtual_screen_rect.bottom;
1590 req->client = req->window;
1591 wine_server_call( req );
1593 SERVER_END_REQ;
1595 else
1597 Window win = (Window)GetPropA( hwnd, whole_window_prop );
1598 if (win && win != root_window) X11DRV_init_desktop( win, width, height );
1600 return TRUE;
1604 /**********************************************************************
1605 * CreateWindow (X11DRV.@)
1607 BOOL CDECL X11DRV_CreateWindow( HWND hwnd )
1609 if (hwnd == GetDesktopWindow())
1611 struct x11drv_thread_data *data = x11drv_init_thread_data();
1612 XSetWindowAttributes attr;
1614 if (root_window != DefaultRootWindow( gdi_display ))
1616 /* the desktop win data can't be created lazily */
1617 if (!create_desktop_win_data( data->display, hwnd )) return FALSE;
1620 /* create the cursor clipping window */
1621 attr.override_redirect = TRUE;
1622 attr.event_mask = StructureNotifyMask | FocusChangeMask;
1623 data->clip_window = XCreateWindow( data->display, root_window, 0, 0, 1, 1, 0, 0,
1624 InputOnly, visual, CWOverrideRedirect | CWEventMask, &attr );
1625 XFlush( data->display );
1626 SetPropA( hwnd, clip_window_prop, (HANDLE)data->clip_window );
1628 return TRUE;
1632 /***********************************************************************
1633 * get_win_data
1635 * Lock and return the X11 data structure associated with a window.
1637 struct x11drv_win_data *get_win_data( HWND hwnd )
1639 char *data;
1641 if (!hwnd) return NULL;
1642 EnterCriticalSection( &win_data_section );
1643 if (!XFindContext( gdi_display, (XID)hwnd, win_data_context, &data ))
1644 return (struct x11drv_win_data *)data;
1645 LeaveCriticalSection( &win_data_section );
1646 return NULL;
1650 /***********************************************************************
1651 * release_win_data
1653 * Release the data returned by get_win_data.
1655 void release_win_data( struct x11drv_win_data *data )
1657 if (data) LeaveCriticalSection( &win_data_section );
1661 /***********************************************************************
1662 * X11DRV_get_win_data
1664 * Return the X11 data structure associated with a window.
1666 struct x11drv_win_data *X11DRV_get_win_data( HWND hwnd )
1668 struct x11drv_win_data *data = get_win_data( hwnd );
1670 if (data)
1672 release_win_data( data );
1673 if (GetWindowThreadProcessId( hwnd, NULL ) != GetCurrentThreadId()) data = NULL;
1675 return data;
1679 /***********************************************************************
1680 * X11DRV_create_win_data
1682 * Create an X11 data window structure for an existing window.
1684 static struct x11drv_win_data *X11DRV_create_win_data( HWND hwnd, const RECT *window_rect,
1685 const RECT *client_rect )
1687 Display *display;
1688 struct x11drv_win_data *data;
1689 HWND parent;
1691 if (!(parent = GetAncestor( hwnd, GA_PARENT ))) return NULL; /* desktop */
1693 /* don't create win data for HWND_MESSAGE windows */
1694 if (parent != GetDesktopWindow() && !GetAncestor( parent, GA_PARENT )) return NULL;
1696 if (GetWindowThreadProcessId( hwnd, NULL ) != GetCurrentThreadId()) return NULL;
1698 display = thread_init_display();
1699 init_clip_window(); /* make sure the clip window is initialized in this thread */
1701 if (!(data = alloc_win_data( display, hwnd ))) return NULL;
1703 data->whole_rect = data->window_rect = *window_rect;
1704 data->client_rect = *client_rect;
1705 if (parent == GetDesktopWindow())
1707 create_whole_window( display, data );
1708 TRACE( "win %p/%lx window %s whole %s client %s\n",
1709 hwnd, data->whole_window, wine_dbgstr_rect( &data->window_rect ),
1710 wine_dbgstr_rect( &data->whole_rect ), wine_dbgstr_rect( &data->client_rect ));
1712 release_win_data( data );
1713 return data;
1717 /* window procedure for foreign windows */
1718 static LRESULT WINAPI foreign_window_proc( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
1720 switch(msg)
1722 case WM_WINDOWPOSCHANGED:
1723 update_systray_balloon_position();
1724 break;
1725 case WM_PARENTNOTIFY:
1726 if (LOWORD(wparam) == WM_DESTROY)
1728 TRACE( "%p: got parent notify destroy for win %lx\n", hwnd, lparam );
1729 PostMessageW( hwnd, WM_CLOSE, 0, 0 ); /* so that we come back here once the child is gone */
1731 return 0;
1732 case WM_CLOSE:
1733 if (GetWindow( hwnd, GW_CHILD )) return 0; /* refuse to die if we still have children */
1734 break;
1736 return DefWindowProcW( hwnd, msg, wparam, lparam );
1740 /***********************************************************************
1741 * create_foreign_window
1743 * Create a foreign window for the specified X window and its ancestors
1745 HWND create_foreign_window( Display *display, Window xwin )
1747 static const WCHAR classW[] = {'_','_','w','i','n','e','_','x','1','1','_','f','o','r','e','i','g','n','_','w','i','n','d','o','w',0};
1748 static BOOL class_registered;
1749 struct x11drv_win_data *data;
1750 HWND hwnd, parent;
1751 Window xparent, xroot;
1752 Window *xchildren;
1753 unsigned int nchildren;
1754 XWindowAttributes attr;
1755 DWORD style = WS_CLIPCHILDREN;
1757 if (!class_registered)
1759 WNDCLASSEXW class;
1761 memset( &class, 0, sizeof(class) );
1762 class.cbSize = sizeof(class);
1763 class.lpfnWndProc = foreign_window_proc;
1764 class.lpszClassName = classW;
1765 if (!RegisterClassExW( &class ) && GetLastError() != ERROR_CLASS_ALREADY_EXISTS)
1767 ERR( "Could not register foreign window class\n" );
1768 return FALSE;
1770 class_registered = TRUE;
1773 if (XFindContext( display, xwin, winContext, (char **)&hwnd )) hwnd = 0;
1774 if (hwnd) return hwnd; /* already created */
1776 XSelectInput( display, xwin, StructureNotifyMask );
1777 if (!XGetWindowAttributes( display, xwin, &attr ) ||
1778 !XQueryTree( display, xwin, &xroot, &xparent, &xchildren, &nchildren ))
1780 XSelectInput( display, xwin, 0 );
1781 return 0;
1783 XFree( xchildren );
1785 if (xparent == xroot)
1787 parent = GetDesktopWindow();
1788 style |= WS_POPUP;
1789 attr.x += virtual_screen_rect.left;
1790 attr.y += virtual_screen_rect.top;
1792 else
1794 parent = create_foreign_window( display, xparent );
1795 style |= WS_CHILD;
1798 hwnd = CreateWindowW( classW, NULL, style, attr.x, attr.y, attr.width, attr.height,
1799 parent, 0, 0, NULL );
1801 if (!(data = alloc_win_data( display, hwnd )))
1803 DestroyWindow( hwnd );
1804 return 0;
1806 SetRect( &data->window_rect, attr.x, attr.y, attr.x + attr.width, attr.y + attr.height );
1807 data->whole_rect = data->client_rect = data->window_rect;
1808 data->whole_window = 0;
1809 data->embedded = TRUE;
1810 data->mapped = TRUE;
1812 SetPropA( hwnd, foreign_window_prop, (HANDLE)xwin );
1813 XSaveContext( display, xwin, winContext, (char *)data->hwnd );
1815 ShowWindow( hwnd, SW_SHOW );
1817 TRACE( "win %lx parent %p style %08x %s -> hwnd %p\n",
1818 xwin, parent, style, wine_dbgstr_rect(&data->window_rect), hwnd );
1820 return hwnd;
1824 /***********************************************************************
1825 * X11DRV_get_whole_window
1827 * Return the X window associated with the full area of a window
1829 Window X11DRV_get_whole_window( HWND hwnd )
1831 struct x11drv_win_data *data = get_win_data( hwnd );
1832 Window ret;
1834 if (!data)
1836 if (hwnd == GetDesktopWindow()) return root_window;
1837 return (Window)GetPropA( hwnd, whole_window_prop );
1839 ret = data->whole_window;
1840 release_win_data( data );
1841 return ret;
1845 /***********************************************************************
1846 * X11DRV_get_ic
1848 * Return the X input context associated with a window
1850 XIC X11DRV_get_ic( HWND hwnd )
1852 struct x11drv_win_data *data = get_win_data( hwnd );
1853 XIM xim;
1854 XIC ret = 0;
1856 if (data)
1858 x11drv_thread_data()->last_xic_hwnd = hwnd;
1859 ret = data->xic;
1860 if (!ret && (xim = x11drv_thread_data()->xim)) ret = X11DRV_CreateIC( xim, data );
1861 release_win_data( data );
1863 return ret;
1867 /***********************************************************************
1868 * X11DRV_GetDC (X11DRV.@)
1870 void CDECL X11DRV_GetDC( HDC hdc, HWND hwnd, HWND top, const RECT *win_rect,
1871 const RECT *top_rect, DWORD flags )
1873 struct x11drv_escape_set_drawable escape;
1874 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
1875 HWND parent;
1877 escape.code = X11DRV_SET_DRAWABLE;
1878 escape.hwnd = hwnd;
1879 escape.mode = IncludeInferiors;
1880 escape.fbconfig_id = 0;
1882 escape.dc_rect.left = win_rect->left - top_rect->left;
1883 escape.dc_rect.top = win_rect->top - top_rect->top;
1884 escape.dc_rect.right = win_rect->right - top_rect->left;
1885 escape.dc_rect.bottom = win_rect->bottom - top_rect->top;
1887 if (top == hwnd)
1889 escape.drawable = data ? data->whole_window : X11DRV_get_whole_window( hwnd );
1891 /* special case: when repainting the root window, clip out top-level windows */
1892 if (data && data->whole_window == root_window) escape.mode = ClipByChildren;
1894 else
1896 /* find the first ancestor that has a drawable */
1897 for (parent = hwnd; parent && parent != top; parent = GetAncestor( parent, GA_PARENT ))
1898 if ((escape.drawable = X11DRV_get_whole_window( parent ))) break;
1900 if (escape.drawable)
1902 POINT pt = { 0, 0 };
1903 MapWindowPoints( 0, parent, &pt, 1 );
1904 escape.dc_rect = *win_rect;
1905 OffsetRect( &escape.dc_rect, pt.x, pt.y );
1906 if (flags & DCX_CLIPCHILDREN) escape.mode = ClipByChildren;
1908 else escape.drawable = X11DRV_get_whole_window( top );
1911 ExtEscape( hdc, X11DRV_ESCAPE, sizeof(escape), (LPSTR)&escape, 0, NULL );
1915 /***********************************************************************
1916 * X11DRV_ReleaseDC (X11DRV.@)
1918 void CDECL X11DRV_ReleaseDC( HWND hwnd, HDC hdc )
1920 struct x11drv_escape_set_drawable escape;
1922 escape.code = X11DRV_SET_DRAWABLE;
1923 escape.hwnd = GetDesktopWindow();
1924 escape.drawable = root_window;
1925 escape.mode = IncludeInferiors;
1926 SetRect( &escape.dc_rect, 0, 0, virtual_screen_rect.right - virtual_screen_rect.left,
1927 virtual_screen_rect.bottom - virtual_screen_rect.top );
1928 OffsetRect( &escape.dc_rect, -virtual_screen_rect.left, -virtual_screen_rect.top );
1929 escape.fbconfig_id = 0;
1930 ExtEscape( hdc, X11DRV_ESCAPE, sizeof(escape), (LPSTR)&escape, 0, NULL );
1934 /***********************************************************************
1935 * SetCapture (X11DRV.@)
1937 void CDECL X11DRV_SetCapture( HWND hwnd, UINT flags )
1939 struct x11drv_thread_data *thread_data = x11drv_thread_data();
1941 if (!thread_data) return;
1942 if (!(flags & (GUI_INMOVESIZE | GUI_INMENUMODE))) return;
1944 if (hwnd)
1946 Window grab_win = X11DRV_get_whole_window( GetAncestor( hwnd, GA_ROOT ) );
1948 if (!grab_win) return;
1949 XFlush( gdi_display );
1950 XGrabPointer( thread_data->display, grab_win, False,
1951 PointerMotionMask | ButtonPressMask | ButtonReleaseMask,
1952 GrabModeAsync, GrabModeAsync, None, None, CurrentTime );
1953 thread_data->grab_window = grab_win;
1955 else /* release capture */
1957 XFlush( gdi_display );
1958 XUngrabPointer( thread_data->display, CurrentTime );
1959 XFlush( thread_data->display );
1960 thread_data->grab_window = None;
1965 /*****************************************************************
1966 * SetParent (X11DRV.@)
1968 void CDECL X11DRV_SetParent( HWND hwnd, HWND parent, HWND old_parent )
1970 Display *display = thread_display();
1971 struct x11drv_win_data *data;
1973 if (parent == old_parent) return;
1974 if (!(data = get_win_data( hwnd ))) return;
1975 if (data->embedded) goto done;
1977 if (parent != GetDesktopWindow()) /* a child window */
1979 if (old_parent == GetDesktopWindow())
1981 /* destroy the old X windows */
1982 destroy_whole_window( display, data, FALSE );
1983 if (data->managed)
1985 data->managed = FALSE;
1986 RemovePropA( data->hwnd, managed_prop );
1990 else /* new top level window */
1992 create_whole_window( display, data );
1994 done:
1995 release_win_data( data );
1996 fetch_icon_data( hwnd, 0, 0 );
2000 static inline RECT get_surface_rect( const RECT *visible_rect )
2002 RECT rect;
2004 IntersectRect( &rect, visible_rect, &virtual_screen_rect );
2005 OffsetRect( &rect, -visible_rect->left, -visible_rect->top );
2006 rect.left &= ~31;
2007 rect.top &= ~31;
2008 rect.right = max( rect.left + 32, (rect.right + 31) & ~31 );
2009 rect.bottom = max( rect.top + 32, (rect.bottom + 31) & ~31 );
2010 return rect;
2014 /***********************************************************************
2015 * WindowPosChanging (X11DRV.@)
2017 void CDECL X11DRV_WindowPosChanging( HWND hwnd, HWND insert_after, UINT swp_flags,
2018 const RECT *window_rect, const RECT *client_rect, RECT *visible_rect,
2019 struct window_surface **surface )
2021 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
2022 RECT surface_rect;
2023 XVisualInfo vis;
2024 DWORD flags;
2025 COLORREF key;
2026 BOOL layered = GetWindowLongW( hwnd, GWL_EXSTYLE ) & WS_EX_LAYERED;
2028 if (!data && !(data = X11DRV_create_win_data( hwnd, window_rect, client_rect ))) return;
2030 /* check if we need to switch the window to managed */
2031 if (!data->managed && data->whole_window && is_window_managed( hwnd, swp_flags, window_rect ))
2033 TRACE( "making win %p/%lx managed\n", hwnd, data->whole_window );
2034 if (data->mapped) unmap_window( hwnd );
2035 data->managed = TRUE;
2036 SetPropA( hwnd, managed_prop, (HANDLE)1 );
2039 *visible_rect = *window_rect;
2040 X11DRV_window_to_X_rect( data, visible_rect );
2042 /* create the window surface if necessary */
2043 if (!data->whole_window) return;
2044 if (data->embedded) return;
2045 if (swp_flags & SWP_HIDEWINDOW) return;
2046 if (data->whole_window == root_window) return;
2047 if (has_gl_drawable( hwnd )) return;
2048 if (!client_side_graphics && !layered) return;
2050 surface_rect = get_surface_rect( visible_rect );
2051 if (data->surface)
2053 if (!memcmp( &data->surface->rect, &surface_rect, sizeof(surface_rect) ))
2055 /* existing surface is good enough */
2056 window_surface_add_ref( data->surface );
2057 *surface = data->surface;
2058 return;
2061 else if (!(swp_flags & SWP_SHOWWINDOW) && !(GetWindowLongW( hwnd, GWL_STYLE ) & WS_VISIBLE)) return;
2063 memset( &vis, 0, sizeof(vis) );
2064 vis.visual = visual;
2065 vis.visualid = visual->visualid;
2066 vis.depth = screen_depth;
2067 vis.red_mask = visual->red_mask;
2068 vis.green_mask = visual->green_mask;
2069 vis.blue_mask = visual->blue_mask;
2070 if (!layered || !GetLayeredWindowAttributes( hwnd, &key, NULL, &flags ) || !(flags & LWA_COLORKEY))
2071 key = CLR_INVALID;
2073 *surface = create_surface( data->whole_window, &vis, &surface_rect, key );
2077 /***********************************************************************
2078 * WindowPosChanged (X11DRV.@)
2080 void CDECL X11DRV_WindowPosChanged( HWND hwnd, HWND insert_after, UINT swp_flags,
2081 const RECT *rectWindow, const RECT *rectClient,
2082 const RECT *visible_rect, const RECT *valid_rects,
2083 struct window_surface *surface )
2085 struct x11drv_thread_data *thread_data;
2086 Display *display;
2087 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
2088 DWORD new_style = GetWindowLongW( hwnd, GWL_STYLE );
2089 RECT old_window_rect, old_whole_rect, old_client_rect;
2090 int event_type;
2092 if (!data) return;
2094 thread_data = x11drv_thread_data();
2095 display = thread_data->display;
2097 old_window_rect = data->window_rect;
2098 old_whole_rect = data->whole_rect;
2099 old_client_rect = data->client_rect;
2100 data->window_rect = *rectWindow;
2101 data->whole_rect = *visible_rect;
2102 data->client_rect = *rectClient;
2103 if (surface) window_surface_add_ref( surface );
2104 if (data->surface) window_surface_release( data->surface );
2105 data->surface = surface;
2107 TRACE( "win %p window %s client %s style %08x flags %08x\n",
2108 hwnd, wine_dbgstr_rect(rectWindow), wine_dbgstr_rect(rectClient), new_style, swp_flags );
2110 if (!IsRectEmpty( &valid_rects[0] ))
2112 int x_offset = old_whole_rect.left - data->whole_rect.left;
2113 int y_offset = old_whole_rect.top - data->whole_rect.top;
2115 /* if all that happened is that the whole window moved, copy everything */
2116 if (!(swp_flags & SWP_FRAMECHANGED) &&
2117 old_whole_rect.right - data->whole_rect.right == x_offset &&
2118 old_whole_rect.bottom - data->whole_rect.bottom == y_offset &&
2119 old_client_rect.left - data->client_rect.left == x_offset &&
2120 old_client_rect.right - data->client_rect.right == x_offset &&
2121 old_client_rect.top - data->client_rect.top == y_offset &&
2122 old_client_rect.bottom - data->client_rect.bottom == y_offset &&
2123 !memcmp( &valid_rects[0], &data->client_rect, sizeof(RECT) ))
2125 /* if we have an X window the bits will be moved by the X server */
2126 if (!data->whole_window && (x_offset != 0 || y_offset != 0))
2127 move_window_bits( hwnd, &old_whole_rect, &data->whole_rect, &old_client_rect );
2129 else
2130 move_window_bits( hwnd, &valid_rects[1], &valid_rects[0], &old_client_rect );
2133 XFlush( gdi_display ); /* make sure painting is done before we move the window */
2135 sync_gl_drawable( data->hwnd, visible_rect, rectClient );
2137 if (!data->whole_window) return;
2139 /* check if we are currently processing an event relevant to this window */
2140 event_type = 0;
2141 if (thread_data->current_event && thread_data->current_event->xany.window == data->whole_window)
2142 event_type = thread_data->current_event->type;
2144 if (event_type != ConfigureNotify && event_type != PropertyNotify &&
2145 event_type != GravityNotify && event_type != ReparentNotify)
2146 event_type = 0; /* ignore other events */
2148 if (data->mapped && event_type != ReparentNotify)
2150 if (((swp_flags & SWP_HIDEWINDOW) && !(new_style & WS_VISIBLE)) ||
2151 (!event_type &&
2152 !is_window_rect_mapped( rectWindow ) && is_window_rect_mapped( &old_window_rect )))
2154 unmap_window( hwnd );
2155 if (is_window_rect_fullscreen( &old_window_rect )) reset_clipping_window();
2159 /* don't change position if we are about to minimize or maximize a managed window */
2160 if (!event_type &&
2161 !(data->managed && (swp_flags & SWP_STATECHANGED) && (new_style & (WS_MINIMIZE|WS_MAXIMIZE))))
2162 sync_window_position( display, data, swp_flags,
2163 &old_window_rect, &old_whole_rect, &old_client_rect );
2165 if ((new_style & WS_VISIBLE) &&
2166 ((new_style & WS_MINIMIZE) || is_window_rect_mapped( rectWindow )))
2168 if (!data->mapped)
2170 make_owner_managed( hwnd );
2171 if (!data->icon_pixmap) fetch_icon_data( hwnd, 0, 0 );
2172 set_wm_hints( display, data );
2173 map_window( hwnd, new_style );
2175 else if ((swp_flags & SWP_STATECHANGED) && (!data->iconic != !(new_style & WS_MINIMIZE)))
2177 set_wm_hints( display, data );
2178 data->iconic = (new_style & WS_MINIMIZE) != 0;
2179 TRACE( "changing win %p iconic state to %u\n", data->hwnd, data->iconic );
2180 if (data->iconic)
2181 XIconifyWindow( display, data->whole_window, DefaultScreen(display) );
2182 else if (is_window_rect_mapped( rectWindow ))
2183 XMapWindow( display, data->whole_window );
2184 update_net_wm_states( display, data );
2186 else
2188 if (swp_flags & (SWP_FRAMECHANGED|SWP_STATECHANGED)) set_wm_hints( display, data );
2189 if (!event_type) update_net_wm_states( display, data );
2193 XFlush( display ); /* make sure changes are done before we start painting again */
2197 /***********************************************************************
2198 * ShowWindow (X11DRV.@)
2200 UINT CDECL X11DRV_ShowWindow( HWND hwnd, INT cmd, RECT *rect, UINT swp )
2202 int x, y;
2203 unsigned int width, height, border, depth;
2204 Window root, top;
2205 DWORD style = GetWindowLongW( hwnd, GWL_STYLE );
2206 struct x11drv_thread_data *thread_data = x11drv_thread_data();
2207 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
2209 if (!data || !data->whole_window || !data->managed || !data->mapped || data->iconic) return swp;
2210 if (style & WS_MINIMIZE) return swp;
2211 if (IsRectEmpty( rect )) return swp;
2213 /* only fetch the new rectangle if the ShowWindow was a result of a window manager event */
2215 if (!thread_data->current_event || thread_data->current_event->xany.window != data->whole_window)
2216 return swp;
2218 if (thread_data->current_event->type != ConfigureNotify &&
2219 thread_data->current_event->type != PropertyNotify)
2220 return swp;
2222 TRACE( "win %p/%lx cmd %d at %s flags %08x\n",
2223 hwnd, data->whole_window, cmd, wine_dbgstr_rect(rect), swp );
2225 XGetGeometry( thread_data->display, data->whole_window,
2226 &root, &x, &y, &width, &height, &border, &depth );
2227 XTranslateCoordinates( thread_data->display, data->whole_window, root, 0, 0, &x, &y, &top );
2228 rect->left = x;
2229 rect->top = y;
2230 rect->right = x + width;
2231 rect->bottom = y + height;
2232 OffsetRect( rect, virtual_screen_rect.left, virtual_screen_rect.top );
2233 X11DRV_X_to_window_rect( data, rect );
2234 return swp & ~(SWP_NOMOVE | SWP_NOCLIENTMOVE | SWP_NOSIZE | SWP_NOCLIENTSIZE);
2238 /**********************************************************************
2239 * SetWindowIcon (X11DRV.@)
2241 * hIcon or hIconSm has changed (or is being initialised for the
2242 * first time). Complete the X11 driver-specific initialisation
2243 * and set the window hints.
2245 void CDECL X11DRV_SetWindowIcon( HWND hwnd, UINT type, HICON icon )
2247 struct x11drv_win_data *data;
2249 if (!(data = get_win_data( hwnd ))) return;
2250 if (!data->whole_window) goto done;
2251 release_win_data( data ); /* release the lock, fetching the icon requires sending messages */
2253 if (type == ICON_BIG) fetch_icon_data( hwnd, icon, 0 );
2254 else fetch_icon_data( hwnd, 0, icon );
2256 if (!(data = get_win_data( hwnd ))) return;
2257 set_wm_hints( thread_display(), data );
2258 done:
2259 release_win_data( data );
2263 /***********************************************************************
2264 * SetWindowRgn (X11DRV.@)
2266 * Assign specified region to window (for non-rectangular windows)
2268 int CDECL X11DRV_SetWindowRgn( HWND hwnd, HRGN hrgn, BOOL redraw )
2270 struct x11drv_win_data *data;
2272 if ((data = get_win_data( hwnd )))
2274 sync_window_region( thread_display(), data, hrgn );
2275 release_win_data( data );
2277 else if (X11DRV_get_whole_window( hwnd ))
2279 SendMessageW( hwnd, WM_X11DRV_SET_WIN_REGION, 0, 0 );
2281 return TRUE;
2285 /***********************************************************************
2286 * SetLayeredWindowAttributes (X11DRV.@)
2288 * Set transparency attributes for a layered window.
2290 void CDECL X11DRV_SetLayeredWindowAttributes( HWND hwnd, COLORREF key, BYTE alpha, DWORD flags )
2292 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
2294 if (data)
2296 if (data->whole_window)
2297 sync_window_opacity( thread_display(), data->whole_window, key, alpha, flags );
2298 if (data->surface)
2299 set_surface_color_key( data->surface, (flags & LWA_COLORKEY) ? key : CLR_INVALID );
2301 else
2303 Window win = X11DRV_get_whole_window( hwnd );
2304 if (win)
2306 sync_window_opacity( gdi_display, win, key, alpha, flags );
2307 if (flags & LWA_COLORKEY)
2308 FIXME( "LWA_COLORKEY not supported on foreign thread window %p\n", hwnd );
2314 /**********************************************************************
2315 * X11DRV_WindowMessage (X11DRV.@)
2317 LRESULT CDECL X11DRV_WindowMessage( HWND hwnd, UINT msg, WPARAM wp, LPARAM lp )
2319 struct x11drv_win_data *data;
2321 switch(msg)
2323 case WM_X11DRV_ACQUIRE_SELECTION:
2324 return X11DRV_AcquireClipboard( hwnd );
2325 case WM_X11DRV_SET_WIN_FORMAT:
2326 return set_win_format( hwnd, (XID)wp );
2327 case WM_X11DRV_SET_WIN_REGION:
2328 if ((data = get_win_data( hwnd )))
2330 sync_window_region( thread_display(), data, (HRGN)1 );
2331 release_win_data( data );
2333 return 0;
2334 case WM_X11DRV_RESIZE_DESKTOP:
2335 X11DRV_resize_desktop( LOWORD(lp), HIWORD(lp) );
2336 return 0;
2337 case WM_X11DRV_SET_CURSOR:
2338 if ((data = X11DRV_get_win_data( hwnd )) && data->whole_window)
2339 set_window_cursor( data->whole_window, (HCURSOR)lp );
2340 else if (hwnd == x11drv_thread_data()->clip_hwnd)
2341 set_window_cursor( x11drv_thread_data()->clip_window, (HCURSOR)lp );
2342 return 0;
2343 case WM_X11DRV_CLIP_CURSOR:
2344 return clip_cursor_notify( hwnd, (HWND)lp );
2345 default:
2346 FIXME( "got window msg %x hwnd %p wp %lx lp %lx\n", msg, hwnd, wp, lp );
2347 return 0;
2352 /***********************************************************************
2353 * is_netwm_supported
2355 static BOOL is_netwm_supported( Display *display, Atom atom )
2357 static Atom *net_supported;
2358 static int net_supported_count = -1;
2359 int i;
2361 if (net_supported_count == -1)
2363 Atom type;
2364 int format;
2365 unsigned long count, remaining;
2367 if (!XGetWindowProperty( display, DefaultRootWindow(display), x11drv_atom(_NET_SUPPORTED), 0,
2368 ~0UL, False, XA_ATOM, &type, &format, &count,
2369 &remaining, (unsigned char **)&net_supported ))
2370 net_supported_count = get_property_size( format, count ) / sizeof(Atom);
2371 else
2372 net_supported_count = 0;
2375 for (i = 0; i < net_supported_count; i++)
2376 if (net_supported[i] == atom) return TRUE;
2377 return FALSE;
2381 /***********************************************************************
2382 * SysCommand (X11DRV.@)
2384 * Perform WM_SYSCOMMAND handling.
2386 LRESULT CDECL X11DRV_SysCommand( HWND hwnd, WPARAM wparam, LPARAM lparam )
2388 WPARAM hittest = wparam & 0x0f;
2389 int dir;
2390 Display *display = thread_display();
2391 struct x11drv_win_data *data;
2393 if (!(data = X11DRV_get_win_data( hwnd ))) return -1;
2394 if (!data->whole_window || !data->managed || !data->mapped) return -1;
2396 switch (wparam & 0xfff0)
2398 case SC_MOVE:
2399 if (!hittest) dir = _NET_WM_MOVERESIZE_MOVE_KEYBOARD;
2400 else dir = _NET_WM_MOVERESIZE_MOVE;
2401 break;
2402 case SC_SIZE:
2403 /* windows without WS_THICKFRAME are not resizable through the window manager */
2404 if (!(GetWindowLongW( hwnd, GWL_STYLE ) & WS_THICKFRAME)) return -1;
2406 switch (hittest)
2408 case WMSZ_LEFT: dir = _NET_WM_MOVERESIZE_SIZE_LEFT; break;
2409 case WMSZ_RIGHT: dir = _NET_WM_MOVERESIZE_SIZE_RIGHT; break;
2410 case WMSZ_TOP: dir = _NET_WM_MOVERESIZE_SIZE_TOP; break;
2411 case WMSZ_TOPLEFT: dir = _NET_WM_MOVERESIZE_SIZE_TOPLEFT; break;
2412 case WMSZ_TOPRIGHT: dir = _NET_WM_MOVERESIZE_SIZE_TOPRIGHT; break;
2413 case WMSZ_BOTTOM: dir = _NET_WM_MOVERESIZE_SIZE_BOTTOM; break;
2414 case WMSZ_BOTTOMLEFT: dir = _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT; break;
2415 case WMSZ_BOTTOMRIGHT: dir = _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT; break;
2416 default: dir = _NET_WM_MOVERESIZE_SIZE_KEYBOARD; break;
2418 break;
2420 case SC_KEYMENU:
2421 /* prevent a simple ALT press+release from activating the system menu,
2422 * as that can get confusing on managed windows */
2423 if ((WCHAR)lparam) return -1; /* got an explicit char */
2424 if (GetMenu( hwnd )) return -1; /* window has a real menu */
2425 if (!(GetWindowLongW( hwnd, GWL_STYLE ) & WS_SYSMENU)) return -1; /* no system menu */
2426 TRACE( "ignoring SC_KEYMENU wp %lx lp %lx\n", wparam, lparam );
2427 return 0;
2429 default:
2430 return -1;
2433 if (IsZoomed(hwnd)) return -1;
2435 if (!is_netwm_supported( display, x11drv_atom(_NET_WM_MOVERESIZE) ))
2437 TRACE( "_NET_WM_MOVERESIZE not supported\n" );
2438 return -1;
2441 move_resize_window( hwnd, dir );
2442 return 0;