combase: Add stub for RoGetApartmentIdentifier.
[wine.git] / dlls / winex11.drv / window.c
blob5fecd9a17b8500aca214e2b0534221be701d9e02
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 static const unsigned int net_wm_state_atoms[NB_NET_WM_STATES] =
70 XATOM__NET_WM_STATE_FULLSCREEN,
71 XATOM__NET_WM_STATE_ABOVE,
72 XATOM__NET_WM_STATE_MAXIMIZED_VERT,
73 XATOM__NET_WM_STATE_SKIP_PAGER,
74 XATOM__NET_WM_STATE_SKIP_TASKBAR
77 #define SWP_AGG_NOPOSCHANGE (SWP_NOSIZE | SWP_NOMOVE | SWP_NOCLIENTSIZE | SWP_NOCLIENTMOVE | SWP_NOZORDER)
79 /* is cursor clipping active? */
80 BOOL clipping_cursor = FALSE;
82 /* X context to associate a hwnd to an X window */
83 XContext winContext = 0;
85 /* X context to associate a struct x11drv_win_data to an hwnd */
86 XContext win_data_context = 0;
88 /* time of last user event and window where it's stored */
89 static Time last_user_time;
90 static Window user_time_window;
92 static const char foreign_window_prop[] = "__wine_x11_foreign_window";
93 static const char whole_window_prop[] = "__wine_x11_whole_window";
94 static const char clip_window_prop[] = "__wine_x11_clip_window";
96 static CRITICAL_SECTION win_data_section;
97 static CRITICAL_SECTION_DEBUG critsect_debug =
99 0, 0, &win_data_section,
100 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
101 0, 0, { (DWORD_PTR)(__FILE__ ": win_data_section") }
103 static CRITICAL_SECTION win_data_section = { &critsect_debug, -1, 0, 0, 0, 0 };
106 /***********************************************************************
107 * http://standards.freedesktop.org/startup-notification-spec
109 static void remove_startup_notification(Display *display, Window window)
111 static LONG startup_notification_removed = 0;
112 char id[1024];
113 char message[1024];
114 int i;
115 int pos;
116 XEvent xevent;
117 const char *src;
118 int srclen;
120 if (InterlockedCompareExchange(&startup_notification_removed, 1, 0) != 0)
121 return;
123 if (GetEnvironmentVariableA("DESKTOP_STARTUP_ID", id, sizeof(id)) == 0)
124 return;
125 SetEnvironmentVariableA("DESKTOP_STARTUP_ID", NULL);
127 if ((src = strstr( id, "_TIME" ))) update_user_time( atol( src + 5 ));
129 pos = snprintf(message, sizeof(message), "remove: ID=");
130 message[pos++] = '"';
131 for (i = 0; id[i] && pos < sizeof(message) - 3; i++)
133 if (id[i] == '"' || id[i] == '\\')
134 message[pos++] = '\\';
135 message[pos++] = id[i];
137 message[pos++] = '"';
138 message[pos++] = '\0';
140 xevent.xclient.type = ClientMessage;
141 xevent.xclient.message_type = x11drv_atom(_NET_STARTUP_INFO_BEGIN);
142 xevent.xclient.display = display;
143 xevent.xclient.window = window;
144 xevent.xclient.format = 8;
146 src = message;
147 srclen = strlen(src) + 1;
149 while (srclen > 0)
151 int msglen = srclen;
152 if (msglen > 20)
153 msglen = 20;
154 memset(&xevent.xclient.data.b[0], 0, 20);
155 memcpy(&xevent.xclient.data.b[0], src, msglen);
156 src += msglen;
157 srclen -= msglen;
159 XSendEvent( display, DefaultRootWindow( display ), False, PropertyChangeMask, &xevent );
160 xevent.xclient.message_type = x11drv_atom(_NET_STARTUP_INFO);
165 struct has_popup_result
167 HWND hwnd;
168 BOOL found;
171 static BOOL is_managed( HWND hwnd )
173 struct x11drv_win_data *data = get_win_data( hwnd );
174 BOOL ret = data && data->managed;
175 release_win_data( data );
176 return ret;
179 static BOOL CALLBACK has_managed_popup( HWND hwnd, LPARAM lparam )
181 struct has_popup_result *result = (struct has_popup_result *)lparam;
183 if (hwnd == result->hwnd) return FALSE; /* popups are always above owner */
184 if (GetWindow( hwnd, GW_OWNER ) != result->hwnd) return TRUE;
185 result->found = is_managed( hwnd );
186 return !result->found;
189 static BOOL has_owned_popups( HWND hwnd )
191 struct has_popup_result result;
193 result.hwnd = hwnd;
194 result.found = FALSE;
195 EnumWindows( has_managed_popup, (LPARAM)&result );
196 return result.found;
200 /***********************************************************************
201 * alloc_win_data
203 static struct x11drv_win_data *alloc_win_data( Display *display, HWND hwnd )
205 struct x11drv_win_data *data;
207 if ((data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*data))))
209 data->display = display;
210 data->vis = default_visual;
211 data->hwnd = hwnd;
212 EnterCriticalSection( &win_data_section );
213 XSaveContext( gdi_display, (XID)hwnd, win_data_context, (char *)data );
215 return data;
219 /***********************************************************************
220 * is_window_managed
222 * Check if a given window should be managed
224 static BOOL is_window_managed( HWND hwnd, UINT swp_flags, const RECT *window_rect )
226 DWORD style, ex_style;
228 if (!managed_mode) return FALSE;
230 /* child windows are not managed */
231 style = GetWindowLongW( hwnd, GWL_STYLE );
232 if ((style & (WS_CHILD|WS_POPUP)) == WS_CHILD) return FALSE;
233 /* activated windows are managed */
234 if (!(swp_flags & (SWP_NOACTIVATE|SWP_HIDEWINDOW))) return TRUE;
235 if (hwnd == GetActiveWindow()) return TRUE;
236 /* windows with caption are managed */
237 if ((style & WS_CAPTION) == WS_CAPTION) return TRUE;
238 /* windows with thick frame are managed */
239 if (style & WS_THICKFRAME) return TRUE;
240 if (style & WS_POPUP)
242 HMONITOR hmon;
243 MONITORINFO mi;
245 /* popup with sysmenu == caption are managed */
246 if (style & WS_SYSMENU) return TRUE;
247 /* full-screen popup windows are managed */
248 hmon = MonitorFromWindow( hwnd, MONITOR_DEFAULTTOPRIMARY );
249 mi.cbSize = sizeof( mi );
250 GetMonitorInfoW( hmon, &mi );
251 if (window_rect->left <= mi.rcWork.left && window_rect->right >= mi.rcWork.right &&
252 window_rect->top <= mi.rcWork.top && window_rect->bottom >= mi.rcWork.bottom)
253 return TRUE;
255 /* application windows are managed */
256 ex_style = GetWindowLongW( hwnd, GWL_EXSTYLE );
257 if (ex_style & WS_EX_APPWINDOW) return TRUE;
258 /* windows that own popups are managed */
259 if (has_owned_popups( hwnd )) return TRUE;
260 /* default: not managed */
261 return FALSE;
265 /***********************************************************************
266 * is_window_resizable
268 * Check if window should be made resizable by the window manager
270 static inline BOOL is_window_resizable( struct x11drv_win_data *data, DWORD style )
272 if (style & WS_THICKFRAME) return TRUE;
273 /* Metacity needs the window to be resizable to make it fullscreen */
274 return is_window_rect_fullscreen( &data->whole_rect );
278 /***********************************************************************
279 * get_mwm_decorations
281 static unsigned long get_mwm_decorations( struct x11drv_win_data *data,
282 DWORD style, DWORD ex_style )
284 unsigned long ret = 0;
286 if (!decorated_mode) return 0;
288 if (IsRectEmpty( &data->window_rect )) return 0;
289 if (data->shaped) return 0;
291 if (ex_style & WS_EX_TOOLWINDOW) return 0;
292 if (ex_style & WS_EX_LAYERED) return 0;
294 if ((style & WS_CAPTION) == WS_CAPTION)
296 ret |= MWM_DECOR_TITLE | MWM_DECOR_BORDER;
297 if (style & WS_SYSMENU) ret |= MWM_DECOR_MENU;
298 if (style & WS_MINIMIZEBOX) ret |= MWM_DECOR_MINIMIZE;
299 if (style & WS_MAXIMIZEBOX) ret |= MWM_DECOR_MAXIMIZE;
301 if (ex_style & WS_EX_DLGMODALFRAME) ret |= MWM_DECOR_BORDER;
302 else if (style & WS_THICKFRAME) ret |= MWM_DECOR_BORDER | MWM_DECOR_RESIZEH;
303 else if ((style & (WS_DLGFRAME|WS_BORDER)) == WS_DLGFRAME) ret |= MWM_DECOR_BORDER;
304 return ret;
308 /***********************************************************************
309 * get_x11_rect_offset
311 * Helper for X11DRV_window_to_X_rect and X11DRV_X_to_window_rect.
313 static void get_x11_rect_offset( struct x11drv_win_data *data, RECT *rect )
315 DWORD style, ex_style, style_mask = 0, ex_style_mask = 0;
316 unsigned long decor;
318 rect->top = rect->bottom = rect->left = rect->right = 0;
320 style = GetWindowLongW( data->hwnd, GWL_STYLE );
321 ex_style = GetWindowLongW( data->hwnd, GWL_EXSTYLE );
322 decor = get_mwm_decorations( data, style, ex_style );
324 if (decor & MWM_DECOR_TITLE) style_mask |= WS_CAPTION;
325 if (decor & MWM_DECOR_BORDER)
327 style_mask |= WS_DLGFRAME | WS_THICKFRAME;
328 ex_style_mask |= WS_EX_DLGMODALFRAME;
331 AdjustWindowRectEx( rect, style & style_mask, FALSE, ex_style & ex_style_mask );
335 /***********************************************************************
336 * get_window_attributes
338 * Fill the window attributes structure for an X window.
340 static int get_window_attributes( struct x11drv_win_data *data, XSetWindowAttributes *attr )
342 attr->override_redirect = !data->managed;
343 attr->colormap = data->colormap ? data->colormap : default_colormap;
344 attr->save_under = ((GetClassLongW( data->hwnd, GCL_STYLE ) & CS_SAVEBITS) != 0);
345 attr->bit_gravity = NorthWestGravity;
346 attr->backing_store = NotUseful;
347 attr->border_pixel = 0;
348 attr->event_mask = (ExposureMask | PointerMotionMask |
349 ButtonPressMask | ButtonReleaseMask | EnterWindowMask |
350 KeyPressMask | KeyReleaseMask | FocusChangeMask |
351 KeymapStateMask | StructureNotifyMask);
352 if (data->managed) attr->event_mask |= PropertyChangeMask;
354 return (CWOverrideRedirect | CWSaveUnder | CWColormap | CWBorderPixel |
355 CWEventMask | CWBitGravity | CWBackingStore);
359 /***********************************************************************
360 * sync_window_style
362 * Change the X window attributes when the window style has changed.
364 static void sync_window_style( struct x11drv_win_data *data )
366 if (data->whole_window != root_window)
368 XSetWindowAttributes attr;
369 int mask = get_window_attributes( data, &attr );
371 XChangeWindowAttributes( data->display, data->whole_window, mask, &attr );
376 /***********************************************************************
377 * sync_window_region
379 * Update the X11 window region.
381 static void sync_window_region( struct x11drv_win_data *data, HRGN win_region )
383 #ifdef HAVE_LIBXSHAPE
384 HRGN hrgn = win_region;
386 if (!data->whole_window) return;
387 data->shaped = FALSE;
389 if (IsRectEmpty( &data->window_rect )) /* set an empty shape */
391 static XRectangle empty_rect;
392 XShapeCombineRectangles( data->display, data->whole_window, ShapeBounding, 0, 0,
393 &empty_rect, 1, ShapeSet, YXBanded );
394 return;
397 if (hrgn == (HRGN)1) /* hack: win_region == 1 means retrieve region from server */
399 if (!(hrgn = CreateRectRgn( 0, 0, 0, 0 ))) return;
400 if (GetWindowRgn( data->hwnd, hrgn ) == ERROR)
402 DeleteObject( hrgn );
403 hrgn = 0;
407 if (!hrgn)
409 XShapeCombineMask( data->display, data->whole_window, ShapeBounding, 0, 0, None, ShapeSet );
411 else
413 RGNDATA *pRegionData;
415 if (GetWindowLongW( data->hwnd, GWL_EXSTYLE ) & WS_EX_LAYOUTRTL) MirrorRgn( data->hwnd, hrgn );
416 if ((pRegionData = X11DRV_GetRegionData( hrgn, 0 )))
418 XShapeCombineRectangles( data->display, data->whole_window, ShapeBounding,
419 data->window_rect.left - data->whole_rect.left,
420 data->window_rect.top - data->whole_rect.top,
421 (XRectangle *)pRegionData->Buffer,
422 pRegionData->rdh.nCount, ShapeSet, YXBanded );
423 HeapFree(GetProcessHeap(), 0, pRegionData);
424 data->shaped = TRUE;
427 if (hrgn && hrgn != win_region) DeleteObject( hrgn );
428 #endif /* HAVE_LIBXSHAPE */
432 /***********************************************************************
433 * sync_window_opacity
435 static void sync_window_opacity( Display *display, Window win,
436 COLORREF key, BYTE alpha, DWORD flags )
438 unsigned long opacity = 0xffffffff;
440 if (flags & LWA_ALPHA) opacity = (0xffffffff / 0xff) * alpha;
442 if (opacity == 0xffffffff)
443 XDeleteProperty( display, win, x11drv_atom(_NET_WM_WINDOW_OPACITY) );
444 else
445 XChangeProperty( display, win, x11drv_atom(_NET_WM_WINDOW_OPACITY),
446 XA_CARDINAL, 32, PropModeReplace, (unsigned char *)&opacity, 1 );
450 /***********************************************************************
451 * sync_window_text
453 static void sync_window_text( Display *display, Window win, const WCHAR *text )
455 UINT count;
456 char *buffer, *utf8_buffer;
457 XTextProperty prop;
459 /* allocate new buffer for window text */
460 count = WideCharToMultiByte(CP_UNIXCP, 0, text, -1, NULL, 0, NULL, NULL);
461 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, count ))) return;
462 WideCharToMultiByte(CP_UNIXCP, 0, text, -1, buffer, count, NULL, NULL);
464 count = WideCharToMultiByte(CP_UTF8, 0, text, strlenW(text), NULL, 0, NULL, NULL);
465 if (!(utf8_buffer = HeapAlloc( GetProcessHeap(), 0, count )))
467 HeapFree( GetProcessHeap(), 0, buffer );
468 return;
470 WideCharToMultiByte(CP_UTF8, 0, text, strlenW(text), utf8_buffer, count, NULL, NULL);
472 if (XmbTextListToTextProperty( display, &buffer, 1, XStdICCTextStyle, &prop ) == Success)
474 XSetWMName( display, win, &prop );
475 XSetWMIconName( display, win, &prop );
476 XFree( prop.value );
479 Implements a NET_WM UTF-8 title. It should be without a trailing \0,
480 according to the standard
481 ( http://www.pps.jussieu.fr/~jch/software/UTF8_STRING/UTF8_STRING.text ).
483 XChangeProperty( display, win, x11drv_atom(_NET_WM_NAME), x11drv_atom(UTF8_STRING),
484 8, PropModeReplace, (unsigned char *) utf8_buffer, count);
486 HeapFree( GetProcessHeap(), 0, utf8_buffer );
487 HeapFree( GetProcessHeap(), 0, buffer );
491 /***********************************************************************
492 * get_bitmap_argb
494 * Return the bitmap bits in ARGB format. Helper for setting icon hints.
496 static unsigned long *get_bitmap_argb( HDC hdc, HBITMAP color, HBITMAP mask, unsigned int *size )
498 char buffer[FIELD_OFFSET( BITMAPINFO, bmiColors[256] )];
499 BITMAPINFO *info = (BITMAPINFO *)buffer;
500 BITMAP bm;
501 unsigned int *ptr, *bits = NULL;
502 unsigned char *mask_bits = NULL;
503 int i, j;
504 BOOL has_alpha = FALSE;
506 if (!GetObjectW( color, sizeof(bm), &bm )) return NULL;
507 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
508 info->bmiHeader.biWidth = bm.bmWidth;
509 info->bmiHeader.biHeight = -bm.bmHeight;
510 info->bmiHeader.biPlanes = 1;
511 info->bmiHeader.biBitCount = 32;
512 info->bmiHeader.biCompression = BI_RGB;
513 info->bmiHeader.biSizeImage = bm.bmWidth * bm.bmHeight * 4;
514 info->bmiHeader.biXPelsPerMeter = 0;
515 info->bmiHeader.biYPelsPerMeter = 0;
516 info->bmiHeader.biClrUsed = 0;
517 info->bmiHeader.biClrImportant = 0;
518 *size = bm.bmWidth * bm.bmHeight + 2;
519 if (!(bits = HeapAlloc( GetProcessHeap(), 0, *size * sizeof(long) ))) goto failed;
520 if (!GetDIBits( hdc, color, 0, bm.bmHeight, bits + 2, info, DIB_RGB_COLORS )) goto failed;
522 bits[0] = bm.bmWidth;
523 bits[1] = bm.bmHeight;
525 for (i = 0; i < bm.bmWidth * bm.bmHeight; i++)
526 if ((has_alpha = (bits[i + 2] & 0xff000000) != 0)) break;
528 if (!has_alpha)
530 unsigned int width_bytes = (bm.bmWidth + 31) / 32 * 4;
531 /* generate alpha channel from the mask */
532 info->bmiHeader.biBitCount = 1;
533 info->bmiHeader.biSizeImage = width_bytes * bm.bmHeight;
534 if (!(mask_bits = HeapAlloc( GetProcessHeap(), 0, info->bmiHeader.biSizeImage ))) goto failed;
535 if (!GetDIBits( hdc, mask, 0, bm.bmHeight, mask_bits, info, DIB_RGB_COLORS )) goto failed;
536 ptr = bits + 2;
537 for (i = 0; i < bm.bmHeight; i++)
538 for (j = 0; j < bm.bmWidth; j++, ptr++)
539 if (!((mask_bits[i * width_bytes + j / 8] << (j % 8)) & 0x80)) *ptr |= 0xff000000;
540 HeapFree( GetProcessHeap(), 0, mask_bits );
543 /* convert to array of longs */
544 if (bits && sizeof(long) > sizeof(int))
545 for (i = *size - 1; i >= 0; i--) ((unsigned long *)bits)[i] = bits[i];
547 return (unsigned long *)bits;
549 failed:
550 HeapFree( GetProcessHeap(), 0, bits );
551 HeapFree( GetProcessHeap(), 0, mask_bits );
552 return NULL;
556 /***********************************************************************
557 * create_icon_pixmaps
559 static BOOL create_icon_pixmaps( HDC hdc, const ICONINFO *icon, Pixmap *icon_ret, Pixmap *mask_ret )
561 char buffer[FIELD_OFFSET( BITMAPINFO, bmiColors[256] )];
562 BITMAPINFO *info = (BITMAPINFO *)buffer;
563 XVisualInfo vis = default_visual;
564 struct gdi_image_bits bits;
565 Pixmap color_pixmap = 0, mask_pixmap = 0;
566 int lines;
567 unsigned int i;
569 bits.ptr = NULL;
570 bits.free = NULL;
571 bits.is_copy = TRUE;
573 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
574 info->bmiHeader.biBitCount = 0;
575 if (!(lines = GetDIBits( hdc, icon->hbmColor, 0, 0, NULL, info, DIB_RGB_COLORS ))) goto failed;
576 if (!(bits.ptr = HeapAlloc( GetProcessHeap(), 0, info->bmiHeader.biSizeImage ))) goto failed;
577 if (!GetDIBits( hdc, icon->hbmColor, 0, lines, bits.ptr, info, DIB_RGB_COLORS )) goto failed;
579 color_pixmap = create_pixmap_from_image( hdc, &vis, info, &bits, DIB_RGB_COLORS );
580 HeapFree( GetProcessHeap(), 0, bits.ptr );
581 bits.ptr = NULL;
582 if (!color_pixmap) goto failed;
584 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
585 info->bmiHeader.biBitCount = 0;
586 if (!(lines = GetDIBits( hdc, icon->hbmMask, 0, 0, NULL, info, DIB_RGB_COLORS ))) goto failed;
587 if (!(bits.ptr = HeapAlloc( GetProcessHeap(), 0, info->bmiHeader.biSizeImage ))) goto failed;
588 if (!GetDIBits( hdc, icon->hbmMask, 0, lines, bits.ptr, info, DIB_RGB_COLORS )) goto failed;
590 /* invert the mask */
591 for (i = 0; i < info->bmiHeader.biSizeImage / sizeof(DWORD); i++) ((DWORD *)bits.ptr)[i] ^= ~0u;
593 vis.depth = 1;
594 mask_pixmap = create_pixmap_from_image( hdc, &vis, info, &bits, DIB_RGB_COLORS );
595 HeapFree( GetProcessHeap(), 0, bits.ptr );
596 bits.ptr = NULL;
597 if (!mask_pixmap) goto failed;
599 *icon_ret = color_pixmap;
600 *mask_ret = mask_pixmap;
601 return TRUE;
603 failed:
604 if (color_pixmap) XFreePixmap( gdi_display, color_pixmap );
605 HeapFree( GetProcessHeap(), 0, bits.ptr );
606 return FALSE;
610 /***********************************************************************
611 * fetch_icon_data
613 static void fetch_icon_data( HWND hwnd, HICON icon_big, HICON icon_small )
615 struct x11drv_win_data *data;
616 ICONINFO ii, ii_small;
617 HDC hDC;
618 unsigned int size;
619 unsigned long *bits;
620 Pixmap icon_pixmap, mask_pixmap;
622 if (!icon_big)
624 icon_big = (HICON)SendMessageW( hwnd, WM_GETICON, ICON_BIG, 0 );
625 if (!icon_big) icon_big = (HICON)GetClassLongPtrW( hwnd, GCLP_HICON );
626 if (!icon_big) icon_big = LoadIconW( 0, (LPWSTR)IDI_WINLOGO );
628 if (!icon_small)
630 icon_small = (HICON)SendMessageW( hwnd, WM_GETICON, ICON_SMALL, 0 );
631 if (!icon_small) icon_small = (HICON)GetClassLongPtrW( hwnd, GCLP_HICONSM );
634 if (!GetIconInfo(icon_big, &ii)) return;
636 hDC = CreateCompatibleDC(0);
637 bits = get_bitmap_argb( hDC, ii.hbmColor, ii.hbmMask, &size );
638 if (bits && GetIconInfo( icon_small, &ii_small ))
640 unsigned int size_small;
641 unsigned long *bits_small, *new;
643 if ((bits_small = get_bitmap_argb( hDC, ii_small.hbmColor, ii_small.hbmMask, &size_small )) &&
644 (bits_small[0] != bits[0] || bits_small[1] != bits[1])) /* size must be different */
646 if ((new = HeapReAlloc( GetProcessHeap(), 0, bits,
647 (size + size_small) * sizeof(unsigned long) )))
649 bits = new;
650 memcpy( bits + size, bits_small, size_small * sizeof(unsigned long) );
651 size += size_small;
654 HeapFree( GetProcessHeap(), 0, bits_small );
655 DeleteObject( ii_small.hbmColor );
656 DeleteObject( ii_small.hbmMask );
659 if (!create_icon_pixmaps( hDC, &ii, &icon_pixmap, &mask_pixmap )) icon_pixmap = mask_pixmap = 0;
661 DeleteObject( ii.hbmColor );
662 DeleteObject( ii.hbmMask );
663 DeleteDC(hDC);
665 if ((data = get_win_data( hwnd )))
667 if (data->icon_pixmap) XFreePixmap( gdi_display, data->icon_pixmap );
668 if (data->icon_mask) XFreePixmap( gdi_display, data->icon_mask );
669 HeapFree( GetProcessHeap(), 0, data->icon_bits );
670 data->icon_pixmap = icon_pixmap;
671 data->icon_mask = mask_pixmap;
672 data->icon_bits = bits;
673 data->icon_size = size;
674 release_win_data( data );
676 else
678 if (icon_pixmap) XFreePixmap( gdi_display, icon_pixmap );
679 if (mask_pixmap) XFreePixmap( gdi_display, mask_pixmap );
680 HeapFree( GetProcessHeap(), 0, bits );
685 /***********************************************************************
686 * set_size_hints
688 * set the window size hints
690 static void set_size_hints( struct x11drv_win_data *data, DWORD style )
692 XSizeHints* size_hints;
694 if (!(size_hints = XAllocSizeHints())) return;
696 size_hints->win_gravity = StaticGravity;
697 size_hints->flags |= PWinGravity;
699 /* don't update size hints if window is not in normal state */
700 if (!(style & (WS_MINIMIZE | WS_MAXIMIZE)))
702 if (data->hwnd != GetDesktopWindow()) /* don't force position of desktop */
704 size_hints->x = data->whole_rect.left;
705 size_hints->y = data->whole_rect.top;
706 size_hints->flags |= PPosition;
708 else size_hints->win_gravity = NorthWestGravity;
710 if (!is_window_resizable( data, style ))
712 size_hints->max_width = data->whole_rect.right - data->whole_rect.left;
713 size_hints->max_height = data->whole_rect.bottom - data->whole_rect.top;
714 if (size_hints->max_width <= 0 ||size_hints->max_height <= 0)
715 size_hints->max_width = size_hints->max_height = 1;
716 size_hints->min_width = size_hints->max_width;
717 size_hints->min_height = size_hints->max_height;
718 size_hints->flags |= PMinSize | PMaxSize;
721 XSetWMNormalHints( data->display, data->whole_window, size_hints );
722 XFree( size_hints );
726 /***********************************************************************
727 * set_mwm_hints
729 static void set_mwm_hints( struct x11drv_win_data *data, DWORD style, DWORD ex_style )
731 MwmHints mwm_hints;
733 if (data->hwnd == GetDesktopWindow())
735 if (is_desktop_fullscreen()) mwm_hints.decorations = 0;
736 else mwm_hints.decorations = MWM_DECOR_TITLE | MWM_DECOR_BORDER | MWM_DECOR_MENU | MWM_DECOR_MINIMIZE;
737 mwm_hints.functions = MWM_FUNC_MOVE | MWM_FUNC_MINIMIZE | MWM_FUNC_CLOSE;
739 else
741 mwm_hints.decorations = get_mwm_decorations( data, style, ex_style );
742 mwm_hints.functions = MWM_FUNC_MOVE;
743 if (is_window_resizable( data, style )) mwm_hints.functions |= MWM_FUNC_RESIZE;
744 if (!(style & WS_DISABLED))
746 if (style & WS_MINIMIZEBOX) mwm_hints.functions |= MWM_FUNC_MINIMIZE;
747 if (style & WS_MAXIMIZEBOX) mwm_hints.functions |= MWM_FUNC_MAXIMIZE;
748 if (style & WS_SYSMENU) mwm_hints.functions |= MWM_FUNC_CLOSE;
752 TRACE( "%p setting mwm hints to %lx,%lx (style %x exstyle %x)\n",
753 data->hwnd, mwm_hints.decorations, mwm_hints.functions, style, ex_style );
755 mwm_hints.flags = MWM_HINTS_FUNCTIONS | MWM_HINTS_DECORATIONS;
756 mwm_hints.input_mode = 0;
757 mwm_hints.status = 0;
758 XChangeProperty( data->display, data->whole_window, x11drv_atom(_MOTIF_WM_HINTS),
759 x11drv_atom(_MOTIF_WM_HINTS), 32, PropModeReplace,
760 (unsigned char*)&mwm_hints, sizeof(mwm_hints)/sizeof(long) );
764 /***********************************************************************
765 * set_style_hints
767 static void set_style_hints( struct x11drv_win_data *data, DWORD style, DWORD ex_style )
769 Window group_leader = data->whole_window;
770 HWND owner = GetWindow( data->hwnd, GW_OWNER );
771 Window owner_win = 0;
772 XWMHints *wm_hints;
773 Atom window_type;
775 if (owner)
777 owner = GetAncestor( owner, GA_ROOT );
778 owner_win = X11DRV_get_whole_window( owner );
781 if (owner_win)
783 XSetTransientForHint( data->display, data->whole_window, owner_win );
784 group_leader = owner_win;
787 /* Only use dialog type for owned popups. Metacity allows making fullscreen
788 * only normal windows, and doesn't handle correctly TRANSIENT_FOR hint for
789 * dialogs owned by fullscreen windows.
791 if (((style & WS_POPUP) || (ex_style & WS_EX_DLGMODALFRAME)) && owner)
792 window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_DIALOG);
793 else
794 window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_NORMAL);
796 XChangeProperty(data->display, data->whole_window, x11drv_atom(_NET_WM_WINDOW_TYPE),
797 XA_ATOM, 32, PropModeReplace, (unsigned char*)&window_type, 1);
799 if ((wm_hints = XAllocWMHints()))
801 wm_hints->flags = InputHint | StateHint | WindowGroupHint;
802 wm_hints->input = !use_take_focus && !(style & WS_DISABLED);
803 wm_hints->initial_state = (style & WS_MINIMIZE) ? IconicState : NormalState;
804 wm_hints->window_group = group_leader;
805 if (data->icon_pixmap)
807 wm_hints->icon_pixmap = data->icon_pixmap;
808 wm_hints->icon_mask = data->icon_mask;
809 wm_hints->flags |= IconPixmapHint | IconMaskHint;
811 XSetWMHints( data->display, data->whole_window, wm_hints );
812 XFree( wm_hints );
815 if (data->icon_bits)
816 XChangeProperty( data->display, data->whole_window, x11drv_atom(_NET_WM_ICON),
817 XA_CARDINAL, 32, PropModeReplace,
818 (unsigned char *)data->icon_bits, data->icon_size );
819 else
820 XDeleteProperty( data->display, data->whole_window, x11drv_atom(_NET_WM_ICON) );
825 /***********************************************************************
826 * set_initial_wm_hints
828 * Set the window manager hints that don't change over the lifetime of a window.
830 static void set_initial_wm_hints( Display *display, Window window )
832 long i;
833 Atom protocols[3];
834 Atom dndVersion = WINE_XDND_VERSION;
835 XClassHint *class_hints;
837 /* wm protocols */
838 i = 0;
839 protocols[i++] = x11drv_atom(WM_DELETE_WINDOW);
840 protocols[i++] = x11drv_atom(_NET_WM_PING);
841 if (use_take_focus) protocols[i++] = x11drv_atom(WM_TAKE_FOCUS);
842 XChangeProperty( display, window, x11drv_atom(WM_PROTOCOLS),
843 XA_ATOM, 32, PropModeReplace, (unsigned char *)protocols, i );
845 /* class hints */
846 if ((class_hints = XAllocClassHint()))
848 static char wine[] = "Wine";
850 class_hints->res_name = process_name;
851 class_hints->res_class = wine;
852 XSetClassHint( display, window, class_hints );
853 XFree( class_hints );
856 /* set the WM_CLIENT_MACHINE and WM_LOCALE_NAME properties */
857 XSetWMProperties(display, window, NULL, NULL, NULL, 0, NULL, NULL, NULL);
858 /* set the pid. together, these properties are needed so the window manager can kill us if we freeze */
859 i = getpid();
860 XChangeProperty(display, window, x11drv_atom(_NET_WM_PID),
861 XA_CARDINAL, 32, PropModeReplace, (unsigned char *)&i, 1);
863 XChangeProperty( display, window, x11drv_atom(XdndAware),
864 XA_ATOM, 32, PropModeReplace, (unsigned char*)&dndVersion, 1 );
866 update_user_time( 0 ); /* make sure that the user time window exists */
867 if (user_time_window)
868 XChangeProperty( display, window, x11drv_atom(_NET_WM_USER_TIME_WINDOW),
869 XA_WINDOW, 32, PropModeReplace, (unsigned char *)&user_time_window, 1 );
873 /***********************************************************************
874 * make_owner_managed
876 * If the window is managed, make sure its owner window is too.
878 static void make_owner_managed( HWND hwnd )
880 HWND owner;
882 if (!(owner = GetWindow( hwnd, GW_OWNER ))) return;
883 if (is_managed( owner )) return;
884 if (!is_managed( hwnd )) return;
886 SetWindowPos( owner, 0, 0, 0, 0, 0,
887 SWP_NOACTIVATE | SWP_NOZORDER | SWP_NOSIZE | SWP_NOMOVE |
888 SWP_NOREDRAW | SWP_DEFERERASE | SWP_NOSENDCHANGING | SWP_STATECHANGED );
892 /***********************************************************************
893 * set_wm_hints
895 * Set all the window manager hints for a window.
897 static void set_wm_hints( struct x11drv_win_data *data )
899 DWORD style, ex_style;
901 if (data->hwnd == GetDesktopWindow())
903 /* force some styles for the desktop to get the correct decorations */
904 style = WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX;
905 ex_style = WS_EX_APPWINDOW;
907 else
909 style = GetWindowLongW( data->hwnd, GWL_STYLE );
910 ex_style = GetWindowLongW( data->hwnd, GWL_EXSTYLE );
913 set_size_hints( data, style );
914 set_mwm_hints( data, style, ex_style );
915 set_style_hints( data, style, ex_style );
919 /***********************************************************************
920 * init_clip_window
922 Window init_clip_window(void)
924 struct x11drv_thread_data *data = x11drv_init_thread_data();
926 if (!data->clip_window &&
927 (data->clip_window = (Window)GetPropA( GetDesktopWindow(), clip_window_prop )))
929 XSelectInput( data->display, data->clip_window, StructureNotifyMask );
931 return data->clip_window;
935 /***********************************************************************
936 * update_user_time
938 void update_user_time( Time time )
940 if (!user_time_window)
942 Window win = XCreateWindow( gdi_display, root_window, -1, -1, 1, 1, 0, CopyFromParent,
943 InputOnly, CopyFromParent, 0, NULL );
944 if (InterlockedCompareExchangePointer( (void **)&user_time_window, (void *)win, 0 ))
945 XDestroyWindow( gdi_display, win );
946 TRACE( "user time window %lx\n", user_time_window );
949 if (!time) return;
950 XLockDisplay( gdi_display );
951 if (!last_user_time || (long)(time - last_user_time) > 0)
953 last_user_time = time;
954 XChangeProperty( gdi_display, user_time_window, x11drv_atom(_NET_WM_USER_TIME),
955 XA_CARDINAL, 32, PropModeReplace, (unsigned char *)&time, 1 );
957 XUnlockDisplay( gdi_display );
960 /***********************************************************************
961 * update_net_wm_states
963 void update_net_wm_states( struct x11drv_win_data *data )
965 DWORD i, style, ex_style, new_state = 0;
967 if (!data->managed) return;
968 if (data->whole_window == root_window) return;
970 style = GetWindowLongW( data->hwnd, GWL_STYLE );
971 if (style & WS_MINIMIZE)
972 new_state |= data->net_wm_state & ((1 << NET_WM_STATE_FULLSCREEN)|(1 << NET_WM_STATE_MAXIMIZED));
973 if (is_window_rect_fullscreen( &data->whole_rect ))
975 if ((style & WS_MAXIMIZE) && (style & WS_CAPTION) == WS_CAPTION)
976 new_state |= (1 << NET_WM_STATE_MAXIMIZED);
977 else if (!(style & WS_MINIMIZE))
978 new_state |= (1 << NET_WM_STATE_FULLSCREEN);
980 else if (style & WS_MAXIMIZE)
981 new_state |= (1 << NET_WM_STATE_MAXIMIZED);
983 ex_style = GetWindowLongW( data->hwnd, GWL_EXSTYLE );
984 if (ex_style & WS_EX_TOPMOST)
985 new_state |= (1 << NET_WM_STATE_ABOVE);
986 if (ex_style & (WS_EX_TOOLWINDOW | WS_EX_NOACTIVATE))
987 new_state |= (1 << NET_WM_STATE_SKIP_TASKBAR) | (1 << NET_WM_STATE_SKIP_PAGER);
988 if (!(ex_style & WS_EX_APPWINDOW) && GetWindow( data->hwnd, GW_OWNER ))
989 new_state |= (1 << NET_WM_STATE_SKIP_TASKBAR);
991 if (!data->mapped) /* set the _NET_WM_STATE atom directly */
993 Atom atoms[NB_NET_WM_STATES+1];
994 DWORD count;
996 for (i = count = 0; i < NB_NET_WM_STATES; i++)
998 if (!(new_state & (1 << i))) continue;
999 TRACE( "setting wm state %u for unmapped window %p/%lx\n",
1000 i, data->hwnd, data->whole_window );
1001 atoms[count++] = X11DRV_Atoms[net_wm_state_atoms[i] - FIRST_XATOM];
1002 if (net_wm_state_atoms[i] == XATOM__NET_WM_STATE_MAXIMIZED_VERT)
1003 atoms[count++] = x11drv_atom(_NET_WM_STATE_MAXIMIZED_HORZ);
1005 XChangeProperty( data->display, data->whole_window, x11drv_atom(_NET_WM_STATE), XA_ATOM,
1006 32, PropModeReplace, (unsigned char *)atoms, count );
1008 else /* ask the window manager to do it for us */
1010 XEvent xev;
1012 xev.xclient.type = ClientMessage;
1013 xev.xclient.window = data->whole_window;
1014 xev.xclient.message_type = x11drv_atom(_NET_WM_STATE);
1015 xev.xclient.serial = 0;
1016 xev.xclient.display = data->display;
1017 xev.xclient.send_event = True;
1018 xev.xclient.format = 32;
1019 xev.xclient.data.l[3] = 1;
1020 xev.xclient.data.l[4] = 0;
1022 for (i = 0; i < NB_NET_WM_STATES; i++)
1024 if (!((data->net_wm_state ^ new_state) & (1 << i))) continue; /* unchanged */
1026 TRACE( "setting wm state %u for window %p/%lx to %u prev %u\n",
1027 i, data->hwnd, data->whole_window,
1028 (new_state & (1 << i)) != 0, (data->net_wm_state & (1 << i)) != 0 );
1030 xev.xclient.data.l[0] = (new_state & (1 << i)) ? _NET_WM_STATE_ADD : _NET_WM_STATE_REMOVE;
1031 xev.xclient.data.l[1] = X11DRV_Atoms[net_wm_state_atoms[i] - FIRST_XATOM];
1032 xev.xclient.data.l[2] = ((net_wm_state_atoms[i] == XATOM__NET_WM_STATE_MAXIMIZED_VERT) ?
1033 x11drv_atom(_NET_WM_STATE_MAXIMIZED_HORZ) : 0);
1034 XSendEvent( data->display, root_window, False,
1035 SubstructureRedirectMask | SubstructureNotifyMask, &xev );
1038 data->net_wm_state = new_state;
1041 /***********************************************************************
1042 * read_net_wm_states
1044 void read_net_wm_states( Display* display, struct x11drv_win_data *data )
1046 Atom type, *state;
1047 int format;
1048 unsigned long i, j, count, remaining;
1049 DWORD new_state = 0;
1050 BOOL maximized_horz = FALSE;
1052 if (!data->whole_window) return;
1054 if (!XGetWindowProperty( display, data->whole_window, x11drv_atom(_NET_WM_STATE), 0,
1055 65536/sizeof(CARD32), False, XA_ATOM, &type, &format, &count,
1056 &remaining, (unsigned char **)&state ))
1058 if (type == XA_ATOM && format == 32)
1060 for (i = 0; i < count; i++)
1062 if (state[i] == x11drv_atom(_NET_WM_STATE_MAXIMIZED_HORZ))
1063 maximized_horz = TRUE;
1064 for (j=0; j < NB_NET_WM_STATES; j++)
1066 if (state[i] == X11DRV_Atoms[net_wm_state_atoms[j] - FIRST_XATOM])
1068 new_state |= 1 << j;
1073 XFree( state );
1076 if (!maximized_horz)
1077 new_state &= ~(1 << NET_WM_STATE_MAXIMIZED);
1079 data->net_wm_state = new_state;
1083 /***********************************************************************
1084 * set_xembed_flags
1086 static void set_xembed_flags( struct x11drv_win_data *data, unsigned long flags )
1088 unsigned long info[2];
1090 if (!data->whole_window) return;
1092 info[0] = 0; /* protocol version */
1093 info[1] = flags;
1094 XChangeProperty( data->display, data->whole_window, x11drv_atom(_XEMBED_INFO),
1095 x11drv_atom(_XEMBED_INFO), 32, PropModeReplace, (unsigned char*)info, 2 );
1099 /***********************************************************************
1100 * map_window
1102 static void map_window( HWND hwnd, DWORD new_style )
1104 struct x11drv_win_data *data;
1106 make_owner_managed( hwnd );
1107 wait_for_withdrawn_state( hwnd, TRUE );
1109 if (!(data = get_win_data( hwnd ))) return;
1111 if (data->whole_window && !data->mapped)
1113 TRACE( "win %p/%lx\n", data->hwnd, data->whole_window );
1115 remove_startup_notification( data->display, data->whole_window );
1116 set_wm_hints( data );
1118 if (!data->embedded)
1120 update_net_wm_states( data );
1121 sync_window_style( data );
1122 XMapWindow( data->display, data->whole_window );
1123 XFlush( data->display );
1124 if (data->surface && data->vis.visualid != default_visual.visualid)
1125 data->surface->funcs->flush( data->surface );
1127 else set_xembed_flags( data, XEMBED_MAPPED );
1129 data->mapped = TRUE;
1130 data->iconic = (new_style & WS_MINIMIZE) != 0;
1132 release_win_data( data );
1136 /***********************************************************************
1137 * unmap_window
1139 static void unmap_window( HWND hwnd )
1141 struct x11drv_win_data *data;
1143 wait_for_withdrawn_state( hwnd, FALSE );
1145 if (!(data = get_win_data( hwnd ))) return;
1147 if (data->mapped)
1149 TRACE( "win %p/%lx\n", data->hwnd, data->whole_window );
1151 if (data->embedded) set_xembed_flags( data, 0 );
1152 else if (!data->managed) XUnmapWindow( data->display, data->whole_window );
1153 else XWithdrawWindow( data->display, data->whole_window, data->vis.screen );
1155 data->mapped = FALSE;
1156 data->net_wm_state = 0;
1158 release_win_data( data );
1162 /***********************************************************************
1163 * make_window_embedded
1165 void make_window_embedded( struct x11drv_win_data *data )
1167 /* the window cannot be mapped before being embedded */
1168 if (data->mapped)
1170 if (!data->managed) XUnmapWindow( data->display, data->whole_window );
1171 else XWithdrawWindow( data->display, data->whole_window, data->vis.screen );
1172 data->net_wm_state = 0;
1174 data->embedded = TRUE;
1175 data->managed = TRUE;
1176 sync_window_style( data );
1177 set_xembed_flags( data, (data->mapped || data->embedder) ? XEMBED_MAPPED : 0 );
1181 /***********************************************************************
1182 * X11DRV_window_to_X_rect
1184 * Convert a rect from client to X window coordinates
1186 static void X11DRV_window_to_X_rect( struct x11drv_win_data *data, RECT *rect )
1188 RECT rc;
1190 if (!data->managed) return;
1191 if (IsRectEmpty( rect )) return;
1193 get_x11_rect_offset( data, &rc );
1195 rect->left -= rc.left;
1196 rect->right -= rc.right;
1197 rect->top -= rc.top;
1198 rect->bottom -= rc.bottom;
1199 if (rect->top >= rect->bottom) rect->bottom = rect->top + 1;
1200 if (rect->left >= rect->right) rect->right = rect->left + 1;
1204 /***********************************************************************
1205 * X11DRV_X_to_window_rect
1207 * Opposite of X11DRV_window_to_X_rect
1209 void X11DRV_X_to_window_rect( struct x11drv_win_data *data, RECT *rect )
1211 RECT rc;
1213 if (!data->managed) return;
1214 if (IsRectEmpty( rect )) return;
1216 get_x11_rect_offset( data, &rc );
1218 rect->left += rc.left;
1219 rect->right += rc.right;
1220 rect->top += rc.top;
1221 rect->bottom += rc.bottom;
1222 if (rect->top >= rect->bottom) rect->bottom = rect->top + 1;
1223 if (rect->left >= rect->right) rect->right = rect->left + 1;
1227 /***********************************************************************
1228 * sync_window_position
1230 * Synchronize the X window position with the Windows one
1232 static void sync_window_position( struct x11drv_win_data *data,
1233 UINT swp_flags, const RECT *old_window_rect,
1234 const RECT *old_whole_rect, const RECT *old_client_rect )
1236 DWORD style = GetWindowLongW( data->hwnd, GWL_STYLE );
1237 DWORD ex_style = GetWindowLongW( data->hwnd, GWL_EXSTYLE );
1238 XWindowChanges changes;
1239 unsigned int mask = 0;
1241 if (data->managed && data->iconic) return;
1243 /* resizing a managed maximized window is not allowed */
1244 if (!(style & WS_MAXIMIZE) || !data->managed)
1246 changes.width = data->whole_rect.right - data->whole_rect.left;
1247 changes.height = data->whole_rect.bottom - data->whole_rect.top;
1248 /* if window rect is empty force size to 1x1 */
1249 if (changes.width <= 0 || changes.height <= 0) changes.width = changes.height = 1;
1250 if (changes.width > 65535) changes.width = 65535;
1251 if (changes.height > 65535) changes.height = 65535;
1252 mask |= CWWidth | CWHeight;
1255 /* only the size is allowed to change for the desktop window */
1256 if (data->whole_window != root_window)
1258 POINT pt = virtual_screen_to_root( data->whole_rect.left, data->whole_rect.top );
1259 changes.x = pt.x;
1260 changes.y = pt.y;
1261 mask |= CWX | CWY;
1264 if (!(swp_flags & SWP_NOZORDER) || (swp_flags & SWP_SHOWWINDOW))
1266 /* find window that this one must be after */
1267 HWND prev = GetWindow( data->hwnd, GW_HWNDPREV );
1268 while (prev && !(GetWindowLongW( prev, GWL_STYLE ) & WS_VISIBLE))
1269 prev = GetWindow( prev, GW_HWNDPREV );
1270 if (!prev) /* top child */
1272 changes.stack_mode = Above;
1273 mask |= CWStackMode;
1275 /* should use stack_mode Below but most window managers don't get it right */
1276 /* and Above with a sibling doesn't work so well either, so we ignore it */
1279 set_size_hints( data, style );
1280 set_mwm_hints( data, style, ex_style );
1281 data->configure_serial = NextRequest( data->display );
1282 XReconfigureWMWindow( data->display, data->whole_window, data->vis.screen, mask, &changes );
1283 #ifdef HAVE_LIBXSHAPE
1284 if (IsRectEmpty( old_window_rect ) != IsRectEmpty( &data->window_rect ))
1285 sync_window_region( data, (HRGN)1 );
1286 if (data->shaped)
1288 int old_x_offset = old_window_rect->left - old_whole_rect->left;
1289 int old_y_offset = old_window_rect->top - old_whole_rect->top;
1290 int new_x_offset = data->window_rect.left - data->whole_rect.left;
1291 int new_y_offset = data->window_rect.top - data->whole_rect.top;
1292 if (old_x_offset != new_x_offset || old_y_offset != new_y_offset)
1293 XShapeOffsetShape( data->display, data->whole_window, ShapeBounding,
1294 new_x_offset - old_x_offset, new_y_offset - old_y_offset );
1296 #endif
1298 TRACE( "win %p/%lx pos %d,%d,%dx%d after %lx changes=%x serial=%lu\n",
1299 data->hwnd, data->whole_window, data->whole_rect.left, data->whole_rect.top,
1300 data->whole_rect.right - data->whole_rect.left,
1301 data->whole_rect.bottom - data->whole_rect.top,
1302 changes.sibling, mask, data->configure_serial );
1306 /***********************************************************************
1307 * sync_client_position
1309 * Synchronize the X client window position with the Windows one
1311 static void sync_client_position( struct x11drv_win_data *data,
1312 const RECT *old_client_rect, const RECT *old_whole_rect )
1314 int mask = 0;
1315 XWindowChanges changes;
1317 if (!data->client_window) return;
1319 changes.x = data->client_rect.left - data->whole_rect.left;
1320 changes.y = data->client_rect.top - data->whole_rect.top;
1321 changes.width = min( max( 1, data->client_rect.right - data->client_rect.left ), 65535 );
1322 changes.height = min( max( 1, data->client_rect.bottom - data->client_rect.top ), 65535 );
1324 if (changes.x != old_client_rect->left - old_whole_rect->left) mask |= CWX;
1325 if (changes.y != old_client_rect->top - old_whole_rect->top) mask |= CWY;
1326 if (changes.width != old_client_rect->right - old_client_rect->left) mask |= CWWidth;
1327 if (changes.height != old_client_rect->bottom - old_client_rect->top) mask |= CWHeight;
1329 if (mask)
1331 TRACE( "setting client win %lx pos %d,%d,%dx%d changes=%x\n",
1332 data->client_window, changes.x, changes.y, changes.width, changes.height, mask );
1333 XConfigureWindow( data->display, data->client_window, mask, &changes );
1338 /***********************************************************************
1339 * move_window_bits
1341 * Move the window bits when a window is moved.
1343 static void move_window_bits( HWND hwnd, Window window, const RECT *old_rect, const RECT *new_rect,
1344 const RECT *old_client_rect, const RECT *new_client_rect,
1345 const RECT *new_window_rect )
1347 RECT src_rect = *old_rect;
1348 RECT dst_rect = *new_rect;
1349 HDC hdc_src, hdc_dst;
1350 INT code;
1351 HRGN rgn;
1352 HWND parent = 0;
1354 if (!window)
1356 OffsetRect( &dst_rect, -new_window_rect->left, -new_window_rect->top );
1357 parent = GetAncestor( hwnd, GA_PARENT );
1358 hdc_src = GetDCEx( parent, 0, DCX_CACHE );
1359 hdc_dst = GetDCEx( hwnd, 0, DCX_CACHE | DCX_WINDOW );
1361 else
1363 OffsetRect( &dst_rect, -new_client_rect->left, -new_client_rect->top );
1364 /* make src rect relative to the old position of the window */
1365 OffsetRect( &src_rect, -old_client_rect->left, -old_client_rect->top );
1366 if (dst_rect.left == src_rect.left && dst_rect.top == src_rect.top) return;
1367 hdc_src = hdc_dst = GetDCEx( hwnd, 0, DCX_CACHE );
1370 rgn = CreateRectRgnIndirect( &dst_rect );
1371 SelectClipRgn( hdc_dst, rgn );
1372 DeleteObject( rgn );
1373 /* WS_CLIPCHILDREN doesn't exclude children from the window update
1374 * region, and ExcludeUpdateRgn call may inappropriately clip valid
1375 * child window contents from the copied parent window bits, but we
1376 * still want to avoid copying invalid window bits when possible.
1378 if (!(GetWindowLongW( hwnd, GWL_STYLE ) & WS_CLIPCHILDREN ))
1379 ExcludeUpdateRgn( hdc_dst, hwnd );
1381 code = X11DRV_START_EXPOSURES;
1382 ExtEscape( hdc_dst, X11DRV_ESCAPE, sizeof(code), (LPSTR)&code, 0, NULL );
1384 TRACE( "copying bits for win %p/%lx %s -> %s\n",
1385 hwnd, window, wine_dbgstr_rect(&src_rect), wine_dbgstr_rect(&dst_rect) );
1386 BitBlt( hdc_dst, dst_rect.left, dst_rect.top,
1387 dst_rect.right - dst_rect.left, dst_rect.bottom - dst_rect.top,
1388 hdc_src, src_rect.left, src_rect.top, SRCCOPY );
1390 rgn = 0;
1391 code = X11DRV_END_EXPOSURES;
1392 ExtEscape( hdc_dst, X11DRV_ESCAPE, sizeof(code), (LPSTR)&code, sizeof(rgn), (LPSTR)&rgn );
1394 ReleaseDC( hwnd, hdc_dst );
1395 if (hdc_src != hdc_dst) ReleaseDC( parent, hdc_src );
1397 if (rgn)
1399 if (!window)
1401 /* map region to client rect since we are using DCX_WINDOW */
1402 OffsetRgn( rgn, new_window_rect->left - new_client_rect->left,
1403 new_window_rect->top - new_client_rect->top );
1404 RedrawWindow( hwnd, NULL, rgn,
1405 RDW_INVALIDATE | RDW_FRAME | RDW_ERASE | RDW_ALLCHILDREN );
1407 else RedrawWindow( hwnd, NULL, rgn, RDW_INVALIDATE | RDW_ERASE | RDW_ALLCHILDREN );
1408 DeleteObject( rgn );
1413 /***********************************************************************
1414 * get_dummy_parent
1416 * Create a dummy parent window for child windows that don't have a true X11 parent.
1418 static Window get_dummy_parent(void)
1420 static Window dummy_parent;
1422 if (!dummy_parent)
1424 XSetWindowAttributes attrib;
1426 attrib.override_redirect = True;
1427 attrib.border_pixel = 0;
1428 attrib.colormap = default_colormap;
1429 dummy_parent = XCreateWindow( gdi_display, root_window, -1, -1, 1, 1, 0, default_visual.depth,
1430 InputOutput, default_visual.visual,
1431 CWColormap | CWBorderPixel | CWOverrideRedirect, &attrib );
1432 XMapWindow( gdi_display, dummy_parent );
1434 return dummy_parent;
1438 /**********************************************************************
1439 * create_client_window
1441 Window create_client_window( HWND hwnd, const XVisualInfo *visual )
1443 Window dummy_parent = get_dummy_parent();
1444 struct x11drv_win_data *data = get_win_data( hwnd );
1445 XSetWindowAttributes attr;
1446 Window ret;
1447 int x, y, cx, cy;
1449 if (!data)
1451 /* explicitly create data for HWND_MESSAGE windows since they can be used for OpenGL */
1452 HWND parent = GetAncestor( hwnd, GA_PARENT );
1453 if (parent == GetDesktopWindow() || GetAncestor( parent, GA_PARENT )) return 0;
1454 if (!(data = alloc_win_data( thread_init_display(), hwnd ))) return 0;
1455 GetClientRect( hwnd, &data->client_rect );
1456 data->window_rect = data->whole_rect = data->client_rect;
1459 if (data->client_window)
1461 XDeleteContext( data->display, data->client_window, winContext );
1462 XReparentWindow( gdi_display, data->client_window, dummy_parent, 0, 0 );
1463 TRACE( "%p reparent xwin %lx/%lx\n", data->hwnd, data->whole_window, data->client_window );
1466 if (data->colormap) XFreeColormap( gdi_display, data->colormap );
1467 data->colormap = XCreateColormap( gdi_display, dummy_parent, visual->visual,
1468 (visual->class == PseudoColor ||
1469 visual->class == GrayScale ||
1470 visual->class == DirectColor) ? AllocAll : AllocNone );
1471 attr.colormap = data->colormap;
1472 attr.bit_gravity = NorthWestGravity;
1473 attr.win_gravity = NorthWestGravity;
1474 attr.backing_store = NotUseful;
1475 attr.border_pixel = 0;
1477 x = data->client_rect.left - data->whole_rect.left;
1478 y = data->client_rect.top - data->whole_rect.top;
1479 cx = min( max( 1, data->client_rect.right - data->client_rect.left ), 65535 );
1480 cy = min( max( 1, data->client_rect.bottom - data->client_rect.top ), 65535 );
1482 ret = data->client_window = XCreateWindow( gdi_display,
1483 data->whole_window ? data->whole_window : dummy_parent,
1484 x, y, cx, cy, 0, default_visual.depth, InputOutput,
1485 visual->visual, CWBitGravity | CWWinGravity |
1486 CWBackingStore | CWColormap | CWBorderPixel, &attr );
1487 if (data->client_window)
1489 XSaveContext( data->display, data->client_window, winContext, (char *)data->hwnd );
1490 XMapWindow( gdi_display, data->client_window );
1491 XSync( gdi_display, False );
1492 if (data->whole_window) XSelectInput( data->display, data->client_window, ExposureMask );
1493 TRACE( "%p xwin %lx/%lx\n", data->hwnd, data->whole_window, data->client_window );
1495 release_win_data( data );
1496 return ret;
1500 /**********************************************************************
1501 * create_whole_window
1503 * Create the whole X window for a given window
1505 static void create_whole_window( struct x11drv_win_data *data )
1507 int cx, cy, mask;
1508 XSetWindowAttributes attr;
1509 WCHAR text[1024];
1510 COLORREF key;
1511 BYTE alpha;
1512 DWORD layered_flags;
1513 HRGN win_rgn;
1514 POINT pos;
1516 if (!data->managed && is_window_managed( data->hwnd, SWP_NOACTIVATE, &data->window_rect ))
1518 TRACE( "making win %p/%lx managed\n", data->hwnd, data->whole_window );
1519 data->managed = TRUE;
1522 if ((win_rgn = CreateRectRgn( 0, 0, 0, 0 )) && GetWindowRgn( data->hwnd, win_rgn ) == ERROR)
1524 DeleteObject( win_rgn );
1525 win_rgn = 0;
1527 data->shaped = (win_rgn != 0);
1529 if (data->vis.visualid != default_visual.visualid)
1530 data->colormap = XCreateColormap( data->display, root_window, data->vis.visual, AllocNone );
1532 mask = get_window_attributes( data, &attr );
1534 data->whole_rect = data->window_rect;
1535 X11DRV_window_to_X_rect( data, &data->whole_rect );
1536 if (!(cx = data->whole_rect.right - data->whole_rect.left)) cx = 1;
1537 else if (cx > 65535) cx = 65535;
1538 if (!(cy = data->whole_rect.bottom - data->whole_rect.top)) cy = 1;
1539 else if (cy > 65535) cy = 65535;
1541 pos = virtual_screen_to_root( data->whole_rect.left, data->whole_rect.top );
1542 data->whole_window = XCreateWindow( data->display, root_window, pos.x, pos.y,
1543 cx, cy, 0, data->vis.depth, InputOutput,
1544 data->vis.visual, mask, &attr );
1545 if (!data->whole_window) goto done;
1547 set_initial_wm_hints( data->display, data->whole_window );
1548 set_wm_hints( data );
1550 XSaveContext( data->display, data->whole_window, winContext, (char *)data->hwnd );
1551 SetPropA( data->hwnd, whole_window_prop, (HANDLE)data->whole_window );
1553 /* set the window text */
1554 if (!InternalGetWindowText( data->hwnd, text, sizeof(text)/sizeof(WCHAR) )) text[0] = 0;
1555 sync_window_text( data->display, data->whole_window, text );
1557 /* set the window region */
1558 if (win_rgn || IsRectEmpty( &data->window_rect )) sync_window_region( data, win_rgn );
1560 /* set the window opacity */
1561 if (!GetLayeredWindowAttributes( data->hwnd, &key, &alpha, &layered_flags )) layered_flags = 0;
1562 sync_window_opacity( data->display, data->whole_window, key, alpha, layered_flags );
1564 XFlush( data->display ); /* make sure the window exists before we start painting to it */
1566 sync_window_cursor( data->whole_window );
1568 done:
1569 if (win_rgn) DeleteObject( win_rgn );
1573 /**********************************************************************
1574 * destroy_whole_window
1576 * Destroy the whole X window for a given window.
1578 static void destroy_whole_window( struct x11drv_win_data *data, BOOL already_destroyed )
1580 TRACE( "win %p xwin %lx/%lx\n", data->hwnd, data->whole_window, data->client_window );
1582 if (data->client_window) XDeleteContext( data->display, data->client_window, winContext );
1584 if (!data->whole_window)
1586 if (data->embedded)
1588 Window xwin = (Window)GetPropA( data->hwnd, foreign_window_prop );
1589 if (xwin)
1591 if (!already_destroyed) XSelectInput( data->display, xwin, 0 );
1592 XDeleteContext( data->display, xwin, winContext );
1593 RemovePropA( data->hwnd, foreign_window_prop );
1595 return;
1598 else
1600 if (data->client_window && !already_destroyed)
1602 XSelectInput( data->display, data->client_window, 0 );
1603 XReparentWindow( data->display, data->client_window, get_dummy_parent(), 0, 0 );
1604 XSync( data->display, False );
1606 XDeleteContext( data->display, data->whole_window, winContext );
1607 if (!already_destroyed) XDestroyWindow( data->display, data->whole_window );
1609 if (data->colormap) XFreeColormap( data->display, data->colormap );
1610 data->whole_window = data->client_window = 0;
1611 data->colormap = 0;
1612 data->wm_state = WithdrawnState;
1613 data->net_wm_state = 0;
1614 data->mapped = FALSE;
1615 if (data->xic)
1617 XUnsetICFocus( data->xic );
1618 XDestroyIC( data->xic );
1619 data->xic = 0;
1621 /* Outlook stops processing messages after destroying a dialog, so we need an explicit flush */
1622 XFlush( data->display );
1623 if (data->surface) window_surface_release( data->surface );
1624 data->surface = NULL;
1625 RemovePropA( data->hwnd, whole_window_prop );
1629 /**********************************************************************
1630 * set_window_visual
1632 * Change the visual by destroying and recreating the X window if needed.
1634 void set_window_visual( struct x11drv_win_data *data, const XVisualInfo *vis, BOOL use_alpha )
1636 Window client_window = data->client_window;
1637 Window whole_window = data->whole_window;
1639 if (!data->use_alpha == !use_alpha) return;
1640 if (data->surface) window_surface_release( data->surface );
1641 data->surface = NULL;
1642 data->use_alpha = use_alpha;
1644 if (data->vis.visualid == vis->visualid) return;
1645 data->client_window = 0;
1646 destroy_whole_window( data, client_window != 0 /* don't destroy whole_window until reparented */ );
1647 data->vis = *vis;
1648 create_whole_window( data );
1649 if (!client_window) return;
1650 /* move the client to the new parent */
1651 XReparentWindow( data->display, client_window, data->whole_window,
1652 data->client_rect.left - data->whole_rect.left,
1653 data->client_rect.top - data->whole_rect.top );
1654 data->client_window = client_window;
1655 XDestroyWindow( data->display, whole_window );
1659 /*****************************************************************
1660 * SetWindowText (X11DRV.@)
1662 void CDECL X11DRV_SetWindowText( HWND hwnd, LPCWSTR text )
1664 Window win;
1666 if ((win = X11DRV_get_whole_window( hwnd )) && win != DefaultRootWindow(gdi_display))
1668 Display *display = thread_init_display();
1669 sync_window_text( display, win, text );
1674 /***********************************************************************
1675 * SetWindowStyle (X11DRV.@)
1677 * Update the X state of a window to reflect a style change
1679 void CDECL X11DRV_SetWindowStyle( HWND hwnd, INT offset, STYLESTRUCT *style )
1681 struct x11drv_win_data *data;
1682 DWORD changed = style->styleNew ^ style->styleOld;
1684 if (hwnd == GetDesktopWindow()) return;
1685 if (!(data = get_win_data( hwnd ))) return;
1686 if (!data->whole_window) goto done;
1688 if (offset == GWL_STYLE && (changed & WS_DISABLED)) set_wm_hints( data );
1690 if (offset == GWL_EXSTYLE && (changed & WS_EX_LAYERED)) /* changing WS_EX_LAYERED resets attributes */
1692 data->layered = FALSE;
1693 set_window_visual( data, &default_visual, FALSE );
1694 sync_window_opacity( data->display, data->whole_window, 0, 0, 0 );
1695 if (data->surface) set_surface_color_key( data->surface, CLR_INVALID );
1697 done:
1698 release_win_data( data );
1702 /***********************************************************************
1703 * DestroyWindow (X11DRV.@)
1705 void CDECL X11DRV_DestroyWindow( HWND hwnd )
1707 struct x11drv_thread_data *thread_data = x11drv_thread_data();
1708 struct x11drv_win_data *data;
1710 if (!(data = get_win_data( hwnd ))) return;
1712 destroy_whole_window( data, FALSE );
1713 if (thread_data->last_focus == hwnd) thread_data->last_focus = 0;
1714 if (thread_data->last_xic_hwnd == hwnd) thread_data->last_xic_hwnd = 0;
1715 if (data->icon_pixmap) XFreePixmap( gdi_display, data->icon_pixmap );
1716 if (data->icon_mask) XFreePixmap( gdi_display, data->icon_mask );
1717 HeapFree( GetProcessHeap(), 0, data->icon_bits );
1718 XDeleteContext( gdi_display, (XID)hwnd, win_data_context );
1719 release_win_data( data );
1720 HeapFree( GetProcessHeap(), 0, data );
1721 destroy_gl_drawable( hwnd );
1725 /***********************************************************************
1726 * X11DRV_DestroyNotify
1728 BOOL X11DRV_DestroyNotify( HWND hwnd, XEvent *event )
1730 struct x11drv_win_data *data;
1731 BOOL embedded;
1733 if (!(data = get_win_data( hwnd ))) return FALSE;
1734 embedded = data->embedded;
1735 if (!embedded) FIXME( "window %p/%lx destroyed from the outside\n", hwnd, data->whole_window );
1737 destroy_whole_window( data, TRUE );
1738 release_win_data( data );
1739 if (embedded) SendMessageW( hwnd, WM_CLOSE, 0, 0 );
1740 return TRUE;
1744 /* initialize the desktop window id in the desktop manager process */
1745 BOOL create_desktop_win_data( Window win )
1747 struct x11drv_thread_data *thread_data = x11drv_thread_data();
1748 Display *display = thread_data->display;
1749 struct x11drv_win_data *data;
1751 if (!(data = alloc_win_data( display, GetDesktopWindow() ))) return FALSE;
1752 data->whole_window = win;
1753 data->managed = TRUE;
1754 SetPropA( data->hwnd, whole_window_prop, (HANDLE)win );
1755 set_initial_wm_hints( display, win );
1756 release_win_data( data );
1757 if (thread_data->clip_window) XReparentWindow( display, thread_data->clip_window, win, 0, 0 );
1758 return TRUE;
1761 /**********************************************************************
1762 * CreateDesktopWindow (X11DRV.@)
1764 BOOL CDECL X11DRV_CreateDesktopWindow( HWND hwnd )
1766 unsigned int width, height;
1768 /* retrieve the real size of the desktop */
1769 SERVER_START_REQ( get_window_rectangles )
1771 req->handle = wine_server_user_handle( hwnd );
1772 req->relative = COORDS_CLIENT;
1773 wine_server_call( req );
1774 width = reply->window.right;
1775 height = reply->window.bottom;
1777 SERVER_END_REQ;
1779 if (!width && !height) /* not initialized yet */
1781 RECT rect = get_virtual_screen_rect();
1783 SERVER_START_REQ( set_window_pos )
1785 req->handle = wine_server_user_handle( hwnd );
1786 req->previous = 0;
1787 req->swp_flags = SWP_NOZORDER;
1788 req->window.left = rect.left;
1789 req->window.top = rect.top;
1790 req->window.right = rect.right;
1791 req->window.bottom = rect.bottom;
1792 req->client = req->window;
1793 wine_server_call( req );
1795 SERVER_END_REQ;
1797 else
1799 Window win = (Window)GetPropA( hwnd, whole_window_prop );
1800 if (win && win != root_window) X11DRV_init_desktop( win, width, height );
1802 return TRUE;
1806 /**********************************************************************
1807 * CreateWindow (X11DRV.@)
1809 BOOL CDECL X11DRV_CreateWindow( HWND hwnd )
1811 if (hwnd == GetDesktopWindow())
1813 struct x11drv_thread_data *data = x11drv_init_thread_data();
1814 XSetWindowAttributes attr;
1816 /* create the cursor clipping window */
1817 attr.override_redirect = TRUE;
1818 attr.event_mask = StructureNotifyMask | FocusChangeMask;
1819 data->clip_window = XCreateWindow( data->display, root_window, 0, 0, 1, 1, 0, 0,
1820 InputOnly, default_visual.visual,
1821 CWOverrideRedirect | CWEventMask, &attr );
1822 XFlush( data->display );
1823 SetPropA( hwnd, clip_window_prop, (HANDLE)data->clip_window );
1824 X11DRV_InitClipboard();
1826 return TRUE;
1830 /***********************************************************************
1831 * get_win_data
1833 * Lock and return the X11 data structure associated with a window.
1835 struct x11drv_win_data *get_win_data( HWND hwnd )
1837 char *data;
1839 if (!hwnd) return NULL;
1840 EnterCriticalSection( &win_data_section );
1841 if (!XFindContext( gdi_display, (XID)hwnd, win_data_context, &data ))
1842 return (struct x11drv_win_data *)data;
1843 LeaveCriticalSection( &win_data_section );
1844 return NULL;
1848 /***********************************************************************
1849 * release_win_data
1851 * Release the data returned by get_win_data.
1853 void release_win_data( struct x11drv_win_data *data )
1855 if (data) LeaveCriticalSection( &win_data_section );
1859 /***********************************************************************
1860 * X11DRV_create_win_data
1862 * Create an X11 data window structure for an existing window.
1864 static struct x11drv_win_data *X11DRV_create_win_data( HWND hwnd, const RECT *window_rect,
1865 const RECT *client_rect )
1867 Display *display;
1868 struct x11drv_win_data *data;
1869 HWND parent;
1871 if (!(parent = GetAncestor( hwnd, GA_PARENT ))) return NULL; /* desktop */
1873 /* don't create win data for HWND_MESSAGE windows */
1874 if (parent != GetDesktopWindow() && !GetAncestor( parent, GA_PARENT )) return NULL;
1876 if (GetWindowThreadProcessId( hwnd, NULL ) != GetCurrentThreadId()) return NULL;
1878 display = thread_init_display();
1879 init_clip_window(); /* make sure the clip window is initialized in this thread */
1881 if (!(data = alloc_win_data( display, hwnd ))) return NULL;
1883 data->whole_rect = data->window_rect = *window_rect;
1884 data->client_rect = *client_rect;
1885 if (parent == GetDesktopWindow())
1887 create_whole_window( data );
1888 TRACE( "win %p/%lx window %s whole %s client %s\n",
1889 hwnd, data->whole_window, wine_dbgstr_rect( &data->window_rect ),
1890 wine_dbgstr_rect( &data->whole_rect ), wine_dbgstr_rect( &data->client_rect ));
1892 return data;
1896 /* window procedure for foreign windows */
1897 static LRESULT WINAPI foreign_window_proc( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
1899 switch(msg)
1901 case WM_WINDOWPOSCHANGED:
1902 update_systray_balloon_position();
1903 break;
1904 case WM_PARENTNOTIFY:
1905 if (LOWORD(wparam) == WM_DESTROY)
1907 TRACE( "%p: got parent notify destroy for win %lx\n", hwnd, lparam );
1908 PostMessageW( hwnd, WM_CLOSE, 0, 0 ); /* so that we come back here once the child is gone */
1910 return 0;
1911 case WM_CLOSE:
1912 if (GetWindow( hwnd, GW_CHILD )) return 0; /* refuse to die if we still have children */
1913 break;
1915 return DefWindowProcW( hwnd, msg, wparam, lparam );
1919 /***********************************************************************
1920 * create_foreign_window
1922 * Create a foreign window for the specified X window and its ancestors
1924 HWND create_foreign_window( Display *display, Window xwin )
1926 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};
1927 static BOOL class_registered;
1928 struct x11drv_win_data *data;
1929 HWND hwnd, parent;
1930 POINT pos;
1931 Window xparent, xroot;
1932 Window *xchildren;
1933 unsigned int nchildren;
1934 XWindowAttributes attr;
1935 DWORD style = WS_CLIPCHILDREN;
1937 if (!class_registered)
1939 WNDCLASSEXW class;
1941 memset( &class, 0, sizeof(class) );
1942 class.cbSize = sizeof(class);
1943 class.lpfnWndProc = foreign_window_proc;
1944 class.lpszClassName = classW;
1945 if (!RegisterClassExW( &class ) && GetLastError() != ERROR_CLASS_ALREADY_EXISTS)
1947 ERR( "Could not register foreign window class\n" );
1948 return FALSE;
1950 class_registered = TRUE;
1953 if (XFindContext( display, xwin, winContext, (char **)&hwnd )) hwnd = 0;
1954 if (hwnd) return hwnd; /* already created */
1956 XSelectInput( display, xwin, StructureNotifyMask );
1957 if (!XGetWindowAttributes( display, xwin, &attr ) ||
1958 !XQueryTree( display, xwin, &xroot, &xparent, &xchildren, &nchildren ))
1960 XSelectInput( display, xwin, 0 );
1961 return 0;
1963 XFree( xchildren );
1965 if (xparent == xroot)
1967 parent = GetDesktopWindow();
1968 style |= WS_POPUP;
1969 pos = root_to_virtual_screen( attr.x, attr.y );
1971 else
1973 parent = create_foreign_window( display, xparent );
1974 style |= WS_CHILD;
1975 pos.x = attr.x;
1976 pos.y = attr.y;
1979 hwnd = CreateWindowW( classW, NULL, style, pos.x, pos.y, attr.width, attr.height,
1980 parent, 0, 0, NULL );
1982 if (!(data = alloc_win_data( display, hwnd )))
1984 DestroyWindow( hwnd );
1985 return 0;
1987 SetRect( &data->window_rect, pos.x, pos.y, pos.x + attr.width, pos.y + attr.height );
1988 data->whole_rect = data->client_rect = data->window_rect;
1989 data->whole_window = data->client_window = 0;
1990 data->embedded = TRUE;
1991 data->mapped = TRUE;
1993 SetPropA( hwnd, foreign_window_prop, (HANDLE)xwin );
1994 XSaveContext( display, xwin, winContext, (char *)data->hwnd );
1996 TRACE( "win %lx parent %p style %08x %s -> hwnd %p\n",
1997 xwin, parent, style, wine_dbgstr_rect(&data->window_rect), hwnd );
1999 release_win_data( data );
2001 ShowWindow( hwnd, SW_SHOW );
2002 return hwnd;
2006 /***********************************************************************
2007 * X11DRV_get_whole_window
2009 * Return the X window associated with the full area of a window
2011 Window X11DRV_get_whole_window( HWND hwnd )
2013 struct x11drv_win_data *data = get_win_data( hwnd );
2014 Window ret;
2016 if (!data)
2018 if (hwnd == GetDesktopWindow()) return root_window;
2019 return (Window)GetPropA( hwnd, whole_window_prop );
2021 ret = data->whole_window;
2022 release_win_data( data );
2023 return ret;
2027 /***********************************************************************
2028 * X11DRV_get_ic
2030 * Return the X input context associated with a window
2032 XIC X11DRV_get_ic( HWND hwnd )
2034 struct x11drv_win_data *data = get_win_data( hwnd );
2035 XIM xim;
2036 XIC ret = 0;
2038 if (data)
2040 x11drv_thread_data()->last_xic_hwnd = hwnd;
2041 ret = data->xic;
2042 if (!ret && (xim = x11drv_thread_data()->xim)) ret = X11DRV_CreateIC( xim, data );
2043 release_win_data( data );
2045 return ret;
2049 /***********************************************************************
2050 * X11DRV_GetDC (X11DRV.@)
2052 void CDECL X11DRV_GetDC( HDC hdc, HWND hwnd, HWND top, const RECT *win_rect,
2053 const RECT *top_rect, DWORD flags )
2055 struct x11drv_escape_set_drawable escape;
2056 HWND parent;
2058 escape.code = X11DRV_SET_DRAWABLE;
2059 escape.mode = IncludeInferiors;
2061 escape.dc_rect.left = win_rect->left - top_rect->left;
2062 escape.dc_rect.top = win_rect->top - top_rect->top;
2063 escape.dc_rect.right = win_rect->right - top_rect->left;
2064 escape.dc_rect.bottom = win_rect->bottom - top_rect->top;
2066 if (top == hwnd)
2068 struct x11drv_win_data *data = get_win_data( hwnd );
2070 escape.drawable = data ? data->whole_window : X11DRV_get_whole_window( hwnd );
2072 /* special case: when repainting the root window, clip out top-level windows */
2073 if (data && data->whole_window == root_window) escape.mode = ClipByChildren;
2074 release_win_data( data );
2076 else
2078 /* find the first ancestor that has a drawable */
2079 for (parent = hwnd; parent && parent != top; parent = GetAncestor( parent, GA_PARENT ))
2080 if ((escape.drawable = X11DRV_get_whole_window( parent ))) break;
2082 if (escape.drawable)
2084 POINT pt = { 0, 0 };
2085 MapWindowPoints( 0, parent, &pt, 1 );
2086 escape.dc_rect = *win_rect;
2087 OffsetRect( &escape.dc_rect, pt.x, pt.y );
2088 if (flags & DCX_CLIPCHILDREN) escape.mode = ClipByChildren;
2090 else escape.drawable = X11DRV_get_whole_window( top );
2093 ExtEscape( hdc, X11DRV_ESCAPE, sizeof(escape), (LPSTR)&escape, 0, NULL );
2097 /***********************************************************************
2098 * X11DRV_ReleaseDC (X11DRV.@)
2100 void CDECL X11DRV_ReleaseDC( HWND hwnd, HDC hdc )
2102 struct x11drv_escape_set_drawable escape;
2104 escape.code = X11DRV_SET_DRAWABLE;
2105 escape.drawable = root_window;
2106 escape.mode = IncludeInferiors;
2107 escape.dc_rect = get_virtual_screen_rect();
2108 OffsetRect( &escape.dc_rect, -2 * escape.dc_rect.left, -2 * escape.dc_rect.top );
2109 ExtEscape( hdc, X11DRV_ESCAPE, sizeof(escape), (LPSTR)&escape, 0, NULL );
2113 /*************************************************************************
2114 * ScrollDC (X11DRV.@)
2116 BOOL CDECL X11DRV_ScrollDC( HDC hdc, INT dx, INT dy, HRGN update )
2118 RECT rect;
2119 BOOL ret;
2120 HRGN expose_rgn = 0;
2122 GetClipBox( hdc, &rect );
2124 if (update)
2126 INT code = X11DRV_START_EXPOSURES;
2127 ExtEscape( hdc, X11DRV_ESCAPE, sizeof(code), (LPSTR)&code, 0, NULL );
2129 ret = BitBlt( hdc, rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top,
2130 hdc, rect.left - dx, rect.top - dy, SRCCOPY );
2132 code = X11DRV_END_EXPOSURES;
2133 ExtEscape( hdc, X11DRV_ESCAPE, sizeof(code), (LPSTR)&code,
2134 sizeof(expose_rgn), (LPSTR)&expose_rgn );
2135 if (expose_rgn)
2137 CombineRgn( update, update, expose_rgn, RGN_OR );
2138 DeleteObject( expose_rgn );
2141 else ret = BitBlt( hdc, rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top,
2142 hdc, rect.left - dx, rect.top - dy, SRCCOPY );
2144 return ret;
2148 /***********************************************************************
2149 * SetCapture (X11DRV.@)
2151 void CDECL X11DRV_SetCapture( HWND hwnd, UINT flags )
2153 struct x11drv_thread_data *thread_data = x11drv_thread_data();
2154 struct x11drv_win_data *data;
2156 if (!(flags & (GUI_INMOVESIZE | GUI_INMENUMODE))) return;
2158 if (hwnd)
2160 if (!(data = get_win_data( GetAncestor( hwnd, GA_ROOT )))) return;
2161 if (data->whole_window)
2163 XFlush( gdi_display );
2164 XGrabPointer( data->display, data->whole_window, False,
2165 PointerMotionMask | ButtonPressMask | ButtonReleaseMask,
2166 GrabModeAsync, GrabModeAsync, None, None, CurrentTime );
2167 thread_data->grab_hwnd = data->hwnd;
2169 release_win_data( data );
2171 else /* release capture */
2173 if (!(data = get_win_data( thread_data->grab_hwnd ))) return;
2174 XFlush( gdi_display );
2175 XUngrabPointer( data->display, CurrentTime );
2176 XFlush( data->display );
2177 thread_data->grab_hwnd = NULL;
2178 release_win_data( data );
2183 /*****************************************************************
2184 * SetParent (X11DRV.@)
2186 void CDECL X11DRV_SetParent( HWND hwnd, HWND parent, HWND old_parent )
2188 struct x11drv_win_data *data;
2190 if (parent == old_parent) return;
2191 if (!(data = get_win_data( hwnd ))) return;
2192 if (data->embedded) goto done;
2194 if (parent != GetDesktopWindow()) /* a child window */
2196 if (old_parent == GetDesktopWindow())
2198 /* destroy the old X windows */
2199 destroy_whole_window( data, FALSE );
2200 data->managed = FALSE;
2203 else /* new top level window */
2205 create_whole_window( data );
2207 done:
2208 release_win_data( data );
2209 set_gl_drawable_parent( hwnd, parent );
2210 fetch_icon_data( hwnd, 0, 0 );
2214 static inline BOOL get_surface_rect( const RECT *visible_rect, RECT *surface_rect )
2216 *surface_rect = get_virtual_screen_rect();
2218 if (!IntersectRect( surface_rect, surface_rect, visible_rect )) return FALSE;
2219 OffsetRect( surface_rect, -visible_rect->left, -visible_rect->top );
2220 surface_rect->left &= ~31;
2221 surface_rect->top &= ~31;
2222 surface_rect->right = max( surface_rect->left + 32, (surface_rect->right + 31) & ~31 );
2223 surface_rect->bottom = max( surface_rect->top + 32, (surface_rect->bottom + 31) & ~31 );
2224 return TRUE;
2228 /***********************************************************************
2229 * WindowPosChanging (X11DRV.@)
2231 void CDECL X11DRV_WindowPosChanging( HWND hwnd, HWND insert_after, UINT swp_flags,
2232 const RECT *window_rect, const RECT *client_rect, RECT *visible_rect,
2233 struct window_surface **surface )
2235 struct x11drv_win_data *data = get_win_data( hwnd );
2236 RECT surface_rect;
2237 DWORD flags;
2238 COLORREF key;
2239 BOOL layered = GetWindowLongW( hwnd, GWL_EXSTYLE ) & WS_EX_LAYERED;
2241 if (!data && !(data = X11DRV_create_win_data( hwnd, window_rect, client_rect ))) return;
2243 /* check if we need to switch the window to managed */
2244 if (!data->managed && data->whole_window && is_window_managed( hwnd, swp_flags, window_rect ))
2246 TRACE( "making win %p/%lx managed\n", hwnd, data->whole_window );
2247 release_win_data( data );
2248 unmap_window( hwnd );
2249 if (!(data = get_win_data( hwnd ))) return;
2250 data->managed = TRUE;
2253 *visible_rect = *window_rect;
2254 X11DRV_window_to_X_rect( data, visible_rect );
2256 /* create the window surface if necessary */
2258 if (!data->whole_window && !data->embedded) goto done;
2259 if (swp_flags & SWP_HIDEWINDOW) goto done;
2260 if (data->use_alpha) goto done;
2261 if (!get_surface_rect( visible_rect, &surface_rect )) goto done;
2263 if (*surface) window_surface_release( *surface );
2264 *surface = NULL; /* indicate that we want to draw directly to the window */
2266 if (data->embedded) goto done;
2267 if (data->whole_window == root_window) goto done;
2268 if (data->client_window) goto done;
2269 if (!client_side_graphics && !layered) goto done;
2271 if (data->surface)
2273 if (EqualRect( &data->surface->rect, &surface_rect ))
2275 /* existing surface is good enough */
2276 window_surface_add_ref( data->surface );
2277 *surface = data->surface;
2278 goto done;
2281 else if (!(swp_flags & SWP_SHOWWINDOW) && !(GetWindowLongW( hwnd, GWL_STYLE ) & WS_VISIBLE)) goto done;
2283 if (!layered || !GetLayeredWindowAttributes( hwnd, &key, NULL, &flags ) || !(flags & LWA_COLORKEY))
2284 key = CLR_INVALID;
2286 *surface = create_surface( data->whole_window, &data->vis, &surface_rect, key, FALSE );
2288 done:
2289 release_win_data( data );
2293 /***********************************************************************
2294 * WindowPosChanged (X11DRV.@)
2296 void CDECL X11DRV_WindowPosChanged( HWND hwnd, HWND insert_after, UINT swp_flags,
2297 const RECT *rectWindow, const RECT *rectClient,
2298 const RECT *visible_rect, const RECT *valid_rects,
2299 struct window_surface *surface )
2301 struct x11drv_thread_data *thread_data;
2302 struct x11drv_win_data *data;
2303 DWORD new_style = GetWindowLongW( hwnd, GWL_STYLE );
2304 RECT old_window_rect, old_whole_rect, old_client_rect;
2305 int event_type;
2307 if (!(data = get_win_data( hwnd ))) return;
2309 thread_data = x11drv_thread_data();
2311 old_window_rect = data->window_rect;
2312 old_whole_rect = data->whole_rect;
2313 old_client_rect = data->client_rect;
2314 data->window_rect = *rectWindow;
2315 data->whole_rect = *visible_rect;
2316 data->client_rect = *rectClient;
2317 if (data->vis.visualid == default_visual.visualid)
2319 if (surface) window_surface_add_ref( surface );
2320 if (data->surface) window_surface_release( data->surface );
2321 data->surface = surface;
2324 TRACE( "win %p window %s client %s style %08x flags %08x\n",
2325 hwnd, wine_dbgstr_rect(rectWindow), wine_dbgstr_rect(rectClient), new_style, swp_flags );
2327 if (!IsRectEmpty( &valid_rects[0] ))
2329 Window window = data->whole_window;
2330 int x_offset = old_whole_rect.left - data->whole_rect.left;
2331 int y_offset = old_whole_rect.top - data->whole_rect.top;
2333 /* if all that happened is that the whole window moved, copy everything */
2334 if (!(swp_flags & SWP_FRAMECHANGED) &&
2335 old_whole_rect.right - data->whole_rect.right == x_offset &&
2336 old_whole_rect.bottom - data->whole_rect.bottom == y_offset &&
2337 old_client_rect.left - data->client_rect.left == x_offset &&
2338 old_client_rect.right - data->client_rect.right == x_offset &&
2339 old_client_rect.top - data->client_rect.top == y_offset &&
2340 old_client_rect.bottom - data->client_rect.bottom == y_offset &&
2341 EqualRect( &valid_rects[0], &data->client_rect ))
2343 /* if we have an X window the bits will be moved by the X server */
2344 if (!window && (x_offset != 0 || y_offset != 0))
2346 release_win_data( data );
2347 move_window_bits( hwnd, window, &old_whole_rect, visible_rect,
2348 &old_client_rect, rectClient, rectWindow );
2349 if (!(data = get_win_data( hwnd ))) return;
2352 else
2354 release_win_data( data );
2355 move_window_bits( hwnd, window, &valid_rects[1], &valid_rects[0],
2356 &old_client_rect, rectClient, rectWindow );
2357 if (!(data = get_win_data( hwnd ))) return;
2361 XFlush( gdi_display ); /* make sure painting is done before we move the window */
2363 sync_client_position( data, &old_client_rect, &old_whole_rect );
2365 if (!data->whole_window)
2367 BOOL needs_resize = (!data->client_window &&
2368 (data->client_rect.right - data->client_rect.left !=
2369 old_client_rect.right - old_client_rect.left ||
2370 data->client_rect.bottom - data->client_rect.top !=
2371 old_client_rect.bottom - old_client_rect.top));
2372 release_win_data( data );
2373 if (needs_resize) sync_gl_drawable( hwnd );
2374 return;
2377 /* check if we are currently processing an event relevant to this window */
2378 event_type = 0;
2379 if (thread_data &&
2380 thread_data->current_event &&
2381 thread_data->current_event->xany.window == data->whole_window)
2383 event_type = thread_data->current_event->type;
2384 if (event_type != ConfigureNotify && event_type != PropertyNotify &&
2385 event_type != GravityNotify && event_type != ReparentNotify)
2386 event_type = 0; /* ignore other events */
2389 if (data->mapped && event_type != ReparentNotify)
2391 if (((swp_flags & SWP_HIDEWINDOW) && !(new_style & WS_VISIBLE)) ||
2392 (!event_type && !(new_style & WS_MINIMIZE) &&
2393 !is_window_rect_mapped( rectWindow ) && is_window_rect_mapped( &old_window_rect )))
2395 release_win_data( data );
2396 unmap_window( hwnd );
2397 if (is_window_rect_fullscreen( &old_window_rect )) reset_clipping_window();
2398 if (!(data = get_win_data( hwnd ))) return;
2402 /* don't change position if we are about to minimize or maximize a managed window */
2403 if (!event_type &&
2404 !(data->managed && (swp_flags & SWP_STATECHANGED) && (new_style & (WS_MINIMIZE|WS_MAXIMIZE))))
2405 sync_window_position( data, swp_flags, &old_window_rect, &old_whole_rect, &old_client_rect );
2407 if ((new_style & WS_VISIBLE) &&
2408 ((new_style & WS_MINIMIZE) || is_window_rect_mapped( rectWindow )))
2410 if (!data->mapped)
2412 BOOL needs_icon = !data->icon_pixmap;
2413 BOOL needs_map = TRUE;
2415 /* layered windows are mapped only once their attributes are set */
2416 if (GetWindowLongW( hwnd, GWL_EXSTYLE ) & WS_EX_LAYERED) needs_map = data->layered;
2417 release_win_data( data );
2418 if (needs_icon) fetch_icon_data( hwnd, 0, 0 );
2419 if (needs_map) map_window( hwnd, new_style );
2420 return;
2422 else if ((swp_flags & SWP_STATECHANGED) && (!data->iconic != !(new_style & WS_MINIMIZE)))
2424 set_wm_hints( data );
2425 data->iconic = (new_style & WS_MINIMIZE) != 0;
2426 TRACE( "changing win %p iconic state to %u\n", data->hwnd, data->iconic );
2427 if (data->iconic)
2428 XIconifyWindow( data->display, data->whole_window, data->vis.screen );
2429 else if (is_window_rect_mapped( rectWindow ))
2430 XMapWindow( data->display, data->whole_window );
2431 update_net_wm_states( data );
2433 else
2435 if (swp_flags & (SWP_FRAMECHANGED|SWP_STATECHANGED)) set_wm_hints( data );
2436 if (!event_type) update_net_wm_states( data );
2440 XFlush( data->display ); /* make sure changes are done before we start painting again */
2441 if (data->surface && data->vis.visualid != default_visual.visualid)
2442 data->surface->funcs->flush( data->surface );
2444 release_win_data( data );
2447 /* check if the window icon should be hidden (i.e. moved off-screen) */
2448 static BOOL hide_icon( struct x11drv_win_data *data )
2450 static const WCHAR trayW[] = {'S','h','e','l','l','_','T','r','a','y','W','n','d',0};
2452 if (data->managed) return TRUE;
2453 /* hide icons in desktop mode when the taskbar is active */
2454 if (root_window == DefaultRootWindow( gdi_display )) return FALSE;
2455 return IsWindowVisible( FindWindowW( trayW, NULL ));
2458 /***********************************************************************
2459 * ShowWindow (X11DRV.@)
2461 UINT CDECL X11DRV_ShowWindow( HWND hwnd, INT cmd, RECT *rect, UINT swp )
2463 int x, y;
2464 unsigned int width, height, border, depth;
2465 Window root, top;
2466 POINT pos;
2467 DWORD style = GetWindowLongW( hwnd, GWL_STYLE );
2468 struct x11drv_thread_data *thread_data = x11drv_thread_data();
2469 struct x11drv_win_data *data = get_win_data( hwnd );
2471 if (!data || !data->whole_window) goto done;
2472 if (IsRectEmpty( rect )) goto done;
2473 if (style & WS_MINIMIZE)
2475 if (((rect->left != -32000 || rect->top != -32000)) && hide_icon( data ))
2477 OffsetRect( rect, -32000 - rect->left, -32000 - rect->top );
2478 swp &= ~(SWP_NOMOVE | SWP_NOCLIENTMOVE);
2480 goto done;
2482 if (!data->managed || !data->mapped || data->iconic) goto done;
2484 /* only fetch the new rectangle if the ShowWindow was a result of a window manager event */
2486 if (!thread_data->current_event || thread_data->current_event->xany.window != data->whole_window)
2487 goto done;
2489 if (thread_data->current_event->type != ConfigureNotify &&
2490 thread_data->current_event->type != PropertyNotify)
2491 goto done;
2493 TRACE( "win %p/%lx cmd %d at %s flags %08x\n",
2494 hwnd, data->whole_window, cmd, wine_dbgstr_rect(rect), swp );
2496 XGetGeometry( thread_data->display, data->whole_window,
2497 &root, &x, &y, &width, &height, &border, &depth );
2498 XTranslateCoordinates( thread_data->display, data->whole_window, root, 0, 0, &x, &y, &top );
2499 pos = root_to_virtual_screen( x, y );
2500 rect->left = pos.x;
2501 rect->top = pos.y;
2502 rect->right = pos.x + width;
2503 rect->bottom = pos.y + height;
2504 X11DRV_X_to_window_rect( data, rect );
2505 swp &= ~(SWP_NOMOVE | SWP_NOCLIENTMOVE | SWP_NOSIZE | SWP_NOCLIENTSIZE);
2507 done:
2508 release_win_data( data );
2509 return swp;
2513 /**********************************************************************
2514 * SetWindowIcon (X11DRV.@)
2516 * hIcon or hIconSm has changed (or is being initialised for the
2517 * first time). Complete the X11 driver-specific initialisation
2518 * and set the window hints.
2520 void CDECL X11DRV_SetWindowIcon( HWND hwnd, UINT type, HICON icon )
2522 struct x11drv_win_data *data;
2524 if (!(data = get_win_data( hwnd ))) return;
2525 if (!data->whole_window) goto done;
2526 release_win_data( data ); /* release the lock, fetching the icon requires sending messages */
2528 if (type == ICON_BIG) fetch_icon_data( hwnd, icon, 0 );
2529 else fetch_icon_data( hwnd, 0, icon );
2531 if (!(data = get_win_data( hwnd ))) return;
2532 set_wm_hints( data );
2533 done:
2534 release_win_data( data );
2538 /***********************************************************************
2539 * SetWindowRgn (X11DRV.@)
2541 * Assign specified region to window (for non-rectangular windows)
2543 void CDECL X11DRV_SetWindowRgn( HWND hwnd, HRGN hrgn, BOOL redraw )
2545 struct x11drv_win_data *data;
2547 if ((data = get_win_data( hwnd )))
2549 sync_window_region( data, hrgn );
2550 release_win_data( data );
2552 else if (X11DRV_get_whole_window( hwnd ))
2554 SendMessageW( hwnd, WM_X11DRV_SET_WIN_REGION, 0, 0 );
2559 /***********************************************************************
2560 * SetLayeredWindowAttributes (X11DRV.@)
2562 * Set transparency attributes for a layered window.
2564 void CDECL X11DRV_SetLayeredWindowAttributes( HWND hwnd, COLORREF key, BYTE alpha, DWORD flags )
2566 struct x11drv_win_data *data = get_win_data( hwnd );
2568 if (data)
2570 set_window_visual( data, &default_visual, FALSE );
2572 if (data->whole_window)
2573 sync_window_opacity( data->display, data->whole_window, key, alpha, flags );
2574 if (data->surface)
2575 set_surface_color_key( data->surface, (flags & LWA_COLORKEY) ? key : CLR_INVALID );
2577 data->layered = TRUE;
2578 if (!data->mapped) /* mapping is delayed until attributes are set */
2580 DWORD style = GetWindowLongW( data->hwnd, GWL_STYLE );
2582 if ((style & WS_VISIBLE) &&
2583 ((style & WS_MINIMIZE) || is_window_rect_mapped( &data->window_rect )))
2585 release_win_data( data );
2586 map_window( hwnd, style );
2587 return;
2590 release_win_data( data );
2592 else
2594 Window win = X11DRV_get_whole_window( hwnd );
2595 if (win)
2597 sync_window_opacity( gdi_display, win, key, alpha, flags );
2598 if (flags & LWA_COLORKEY)
2599 FIXME( "LWA_COLORKEY not supported on foreign process window %p\n", hwnd );
2605 /*****************************************************************************
2606 * UpdateLayeredWindow (X11DRV.@)
2608 BOOL CDECL X11DRV_UpdateLayeredWindow( HWND hwnd, const UPDATELAYEREDWINDOWINFO *info,
2609 const RECT *window_rect )
2611 struct window_surface *surface;
2612 struct x11drv_win_data *data;
2613 BLENDFUNCTION blend = { AC_SRC_OVER, 0, 255, 0 };
2614 COLORREF color_key = (info->dwFlags & ULW_COLORKEY) ? info->crKey : CLR_INVALID;
2615 char buffer[FIELD_OFFSET( BITMAPINFO, bmiColors[256] )];
2616 BITMAPINFO *bmi = (BITMAPINFO *)buffer;
2617 void *src_bits, *dst_bits;
2618 RECT rect, src_rect;
2619 HDC hdc = 0;
2620 HBITMAP dib;
2621 BOOL ret = FALSE;
2623 if (!(data = get_win_data( hwnd ))) return FALSE;
2625 data->layered = TRUE;
2626 if (!data->embedded && argb_visual.visualid) set_window_visual( data, &argb_visual, TRUE );
2628 rect = *window_rect;
2629 OffsetRect( &rect, -window_rect->left, -window_rect->top );
2631 surface = data->surface;
2632 if (!surface || !EqualRect( &surface->rect, &rect ))
2634 data->surface = create_surface( data->whole_window, &data->vis, &rect,
2635 color_key, data->use_alpha );
2636 if (surface) window_surface_release( surface );
2637 surface = data->surface;
2639 else set_surface_color_key( surface, color_key );
2641 if (surface) window_surface_add_ref( surface );
2642 release_win_data( data );
2644 if (!surface) return FALSE;
2645 if (!info->hdcSrc)
2647 window_surface_release( surface );
2648 return TRUE;
2651 dst_bits = surface->funcs->get_info( surface, bmi );
2653 if (!(dib = CreateDIBSection( info->hdcDst, bmi, DIB_RGB_COLORS, &src_bits, NULL, 0 ))) goto done;
2654 if (!(hdc = CreateCompatibleDC( 0 ))) goto done;
2656 SelectObject( hdc, dib );
2658 surface->funcs->lock( surface );
2660 if (info->prcDirty)
2662 IntersectRect( &rect, &rect, info->prcDirty );
2663 memcpy( src_bits, dst_bits, bmi->bmiHeader.biSizeImage );
2664 PatBlt( hdc, rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top, BLACKNESS );
2666 src_rect = rect;
2667 if (info->pptSrc) OffsetRect( &src_rect, info->pptSrc->x, info->pptSrc->y );
2668 DPtoLP( info->hdcSrc, (POINT *)&src_rect, 2 );
2670 ret = GdiAlphaBlend( hdc, rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top,
2671 info->hdcSrc, src_rect.left, src_rect.top,
2672 src_rect.right - src_rect.left, src_rect.bottom - src_rect.top,
2673 (info->dwFlags & ULW_ALPHA) ? *info->pblend : blend );
2674 if (ret)
2676 memcpy( dst_bits, src_bits, bmi->bmiHeader.biSizeImage );
2677 add_bounds_rect( surface->funcs->get_bounds( surface ), &rect );
2680 surface->funcs->unlock( surface );
2681 surface->funcs->flush( surface );
2683 done:
2684 window_surface_release( surface );
2685 if (hdc) DeleteDC( hdc );
2686 if (dib) DeleteObject( dib );
2687 return ret;
2691 /**********************************************************************
2692 * X11DRV_WindowMessage (X11DRV.@)
2694 LRESULT CDECL X11DRV_WindowMessage( HWND hwnd, UINT msg, WPARAM wp, LPARAM lp )
2696 struct x11drv_win_data *data;
2698 switch(msg)
2700 case WM_X11DRV_UPDATE_CLIPBOARD:
2701 return update_clipboard( hwnd );
2702 case WM_X11DRV_SET_WIN_REGION:
2703 if ((data = get_win_data( hwnd )))
2705 sync_window_region( data, (HRGN)1 );
2706 release_win_data( data );
2708 return 0;
2709 case WM_X11DRV_RESIZE_DESKTOP:
2710 X11DRV_resize_desktop( LOWORD(lp), HIWORD(lp) );
2711 return 0;
2712 case WM_X11DRV_SET_CURSOR:
2713 if ((data = get_win_data( hwnd )))
2715 Window win = data->whole_window;
2716 release_win_data( data );
2717 if (win) set_window_cursor( win, (HCURSOR)lp );
2719 else if (hwnd == x11drv_thread_data()->clip_hwnd)
2720 set_window_cursor( x11drv_thread_data()->clip_window, (HCURSOR)lp );
2721 return 0;
2722 case WM_X11DRV_CLIP_CURSOR:
2723 return clip_cursor_notify( hwnd, (HWND)lp );
2724 default:
2725 FIXME( "got window msg %x hwnd %p wp %lx lp %lx\n", msg, hwnd, wp, lp );
2726 return 0;
2731 /***********************************************************************
2732 * is_netwm_supported
2734 static BOOL is_netwm_supported( Display *display, Atom atom )
2736 static Atom *net_supported;
2737 static int net_supported_count = -1;
2738 int i;
2740 if (net_supported_count == -1)
2742 Atom type;
2743 int format;
2744 unsigned long count, remaining;
2746 if (!XGetWindowProperty( display, DefaultRootWindow(display), x11drv_atom(_NET_SUPPORTED), 0,
2747 ~0UL, False, XA_ATOM, &type, &format, &count,
2748 &remaining, (unsigned char **)&net_supported ))
2749 net_supported_count = get_property_size( format, count ) / sizeof(Atom);
2750 else
2751 net_supported_count = 0;
2754 for (i = 0; i < net_supported_count; i++)
2755 if (net_supported[i] == atom) return TRUE;
2756 return FALSE;
2760 /***********************************************************************
2761 * SysCommand (X11DRV.@)
2763 * Perform WM_SYSCOMMAND handling.
2765 LRESULT CDECL X11DRV_SysCommand( HWND hwnd, WPARAM wparam, LPARAM lparam )
2767 WPARAM hittest = wparam & 0x0f;
2768 int dir;
2769 struct x11drv_win_data *data;
2771 if (!(data = get_win_data( hwnd ))) return -1;
2772 if (!data->whole_window || !data->managed || !data->mapped) goto failed;
2774 switch (wparam & 0xfff0)
2776 case SC_MOVE:
2777 if (!hittest) dir = _NET_WM_MOVERESIZE_MOVE_KEYBOARD;
2778 else dir = _NET_WM_MOVERESIZE_MOVE;
2779 break;
2780 case SC_SIZE:
2781 /* windows without WS_THICKFRAME are not resizable through the window manager */
2782 if (!(GetWindowLongW( hwnd, GWL_STYLE ) & WS_THICKFRAME)) goto failed;
2784 switch (hittest)
2786 case WMSZ_LEFT: dir = _NET_WM_MOVERESIZE_SIZE_LEFT; break;
2787 case WMSZ_RIGHT: dir = _NET_WM_MOVERESIZE_SIZE_RIGHT; break;
2788 case WMSZ_TOP: dir = _NET_WM_MOVERESIZE_SIZE_TOP; break;
2789 case WMSZ_TOPLEFT: dir = _NET_WM_MOVERESIZE_SIZE_TOPLEFT; break;
2790 case WMSZ_TOPRIGHT: dir = _NET_WM_MOVERESIZE_SIZE_TOPRIGHT; break;
2791 case WMSZ_BOTTOM: dir = _NET_WM_MOVERESIZE_SIZE_BOTTOM; break;
2792 case WMSZ_BOTTOMLEFT: dir = _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT; break;
2793 case WMSZ_BOTTOMRIGHT: dir = _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT; break;
2794 default: dir = _NET_WM_MOVERESIZE_SIZE_KEYBOARD; break;
2796 break;
2798 case SC_KEYMENU:
2799 /* prevent a simple ALT press+release from activating the system menu,
2800 * as that can get confusing on managed windows */
2801 if ((WCHAR)lparam) goto failed; /* got an explicit char */
2802 if (GetMenu( hwnd )) goto failed; /* window has a real menu */
2803 if (!(GetWindowLongW( hwnd, GWL_STYLE ) & WS_SYSMENU)) goto failed; /* no system menu */
2804 TRACE( "ignoring SC_KEYMENU wp %lx lp %lx\n", wparam, lparam );
2805 release_win_data( data );
2806 return 0;
2808 default:
2809 goto failed;
2812 if (IsZoomed(hwnd)) goto failed;
2814 if (!is_netwm_supported( data->display, x11drv_atom(_NET_WM_MOVERESIZE) ))
2816 TRACE( "_NET_WM_MOVERESIZE not supported\n" );
2817 goto failed;
2820 release_win_data( data );
2821 move_resize_window( hwnd, dir );
2822 return 0;
2824 failed:
2825 release_win_data( data );
2826 return -1;
2829 void CDECL X11DRV_FlashWindowEx( PFLASHWINFO pfinfo )
2831 struct x11drv_win_data *data = get_win_data( pfinfo->hwnd );
2832 XEvent xev;
2834 if (!data)
2835 return;
2837 if (data->mapped)
2839 xev.type = ClientMessage;
2840 xev.xclient.window = data->whole_window;
2841 xev.xclient.message_type = x11drv_atom( _NET_WM_STATE );
2842 xev.xclient.serial = 0;
2843 xev.xclient.display = data->display;
2844 xev.xclient.send_event = True;
2845 xev.xclient.format = 32;
2846 xev.xclient.data.l[0] = pfinfo->dwFlags ? _NET_WM_STATE_ADD : _NET_WM_STATE_REMOVE;
2847 xev.xclient.data.l[1] = x11drv_atom( _NET_WM_STATE_DEMANDS_ATTENTION );
2848 xev.xclient.data.l[2] = 0;
2849 xev.xclient.data.l[3] = 1;
2850 xev.xclient.data.l[4] = 0;
2852 XSendEvent( data->display, DefaultRootWindow( data->display ), False,
2853 SubstructureNotifyMask, &xev );
2855 release_win_data( data );