explorer: Create the driver desktop window after the window handle is created.
[wine.git] / dlls / winex11.drv / window.c
bloba76e80d998e71a44209c1d40a86d85fe244553af
1 /*
2 * Window related functions
4 * Copyright 1993, 1994, 1995, 1996, 2001 Alexandre Julliard
5 * Copyright 1993 David Metcalfe
6 * Copyright 1995, 1996 Alex Korobka
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "config.h"
25 #include <stdarg.h>
26 #include <stdlib.h>
27 #include <stdio.h>
28 #ifdef HAVE_UNISTD_H
29 # include <unistd.h>
30 #endif
32 #include <X11/Xlib.h>
33 #include <X11/Xresource.h>
34 #include <X11/Xutil.h>
35 #ifdef HAVE_LIBXSHAPE
36 #include <X11/extensions/shape.h>
37 #endif /* HAVE_LIBXSHAPE */
39 #include "windef.h"
40 #include "winbase.h"
41 #include "wingdi.h"
42 #include "winuser.h"
43 #include "wine/unicode.h"
45 #include "x11drv.h"
46 #include "wine/debug.h"
47 #include "wine/server.h"
48 #include "mwm.h"
50 WINE_DEFAULT_DEBUG_CHANNEL(x11drv);
52 #define _NET_WM_MOVERESIZE_SIZE_TOPLEFT 0
53 #define _NET_WM_MOVERESIZE_SIZE_TOP 1
54 #define _NET_WM_MOVERESIZE_SIZE_TOPRIGHT 2
55 #define _NET_WM_MOVERESIZE_SIZE_RIGHT 3
56 #define _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT 4
57 #define _NET_WM_MOVERESIZE_SIZE_BOTTOM 5
58 #define _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT 6
59 #define _NET_WM_MOVERESIZE_SIZE_LEFT 7
60 #define _NET_WM_MOVERESIZE_MOVE 8
61 #define _NET_WM_MOVERESIZE_SIZE_KEYBOARD 9
62 #define _NET_WM_MOVERESIZE_MOVE_KEYBOARD 10
64 #define _NET_WM_STATE_REMOVE 0
65 #define _NET_WM_STATE_ADD 1
66 #define _NET_WM_STATE_TOGGLE 2
68 #define SWP_AGG_NOPOSCHANGE (SWP_NOSIZE | SWP_NOMOVE | SWP_NOCLIENTSIZE | SWP_NOCLIENTMOVE | SWP_NOZORDER)
70 /* is cursor clipping active? */
71 int clipping_cursor = 0;
73 /* X context to associate a hwnd to an X window */
74 XContext winContext = 0;
76 /* X context to associate a struct x11drv_win_data to an hwnd */
77 XContext win_data_context = 0;
79 /* time of last user event and window where it's stored */
80 static Time last_user_time;
81 static Window user_time_window;
83 static const char foreign_window_prop[] = "__wine_x11_foreign_window";
84 static const char whole_window_prop[] = "__wine_x11_whole_window";
85 static const char clip_window_prop[] = "__wine_x11_clip_window";
87 static CRITICAL_SECTION win_data_section;
88 static CRITICAL_SECTION_DEBUG critsect_debug =
90 0, 0, &win_data_section,
91 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
92 0, 0, { (DWORD_PTR)(__FILE__ ": win_data_section") }
94 static CRITICAL_SECTION win_data_section = { &critsect_debug, -1, 0, 0, 0, 0 };
97 /***********************************************************************
98 * http://standards.freedesktop.org/startup-notification-spec
100 static void remove_startup_notification(Display *display, Window window)
102 static LONG startup_notification_removed = 0;
103 char id[1024];
104 char message[1024];
105 int i;
106 int pos;
107 XEvent xevent;
108 const char *src;
109 int srclen;
111 if (InterlockedCompareExchange(&startup_notification_removed, 1, 0) != 0)
112 return;
114 if (GetEnvironmentVariableA("DESKTOP_STARTUP_ID", id, sizeof(id)) == 0)
115 return;
116 SetEnvironmentVariableA("DESKTOP_STARTUP_ID", NULL);
118 if ((src = strstr( id, "_TIME" ))) update_user_time( atol( src + 5 ));
120 pos = snprintf(message, sizeof(message), "remove: ID=");
121 message[pos++] = '"';
122 for (i = 0; id[i] && pos < sizeof(message) - 2; i++)
124 if (id[i] == '"' || id[i] == '\\')
125 message[pos++] = '\\';
126 message[pos++] = id[i];
128 message[pos++] = '"';
129 message[pos++] = '\0';
131 xevent.xclient.type = ClientMessage;
132 xevent.xclient.message_type = x11drv_atom(_NET_STARTUP_INFO_BEGIN);
133 xevent.xclient.display = display;
134 xevent.xclient.window = window;
135 xevent.xclient.format = 8;
137 src = message;
138 srclen = strlen(src) + 1;
140 while (srclen > 0)
142 int msglen = srclen;
143 if (msglen > 20)
144 msglen = 20;
145 memset(&xevent.xclient.data.b[0], 0, 20);
146 memcpy(&xevent.xclient.data.b[0], src, msglen);
147 src += msglen;
148 srclen -= msglen;
150 XSendEvent( display, DefaultRootWindow( display ), False, PropertyChangeMask, &xevent );
151 xevent.xclient.message_type = x11drv_atom(_NET_STARTUP_INFO);
156 struct has_popup_result
158 HWND hwnd;
159 BOOL found;
162 static BOOL is_managed( HWND hwnd )
164 struct x11drv_win_data *data = get_win_data( hwnd );
165 BOOL ret = data && data->managed;
166 release_win_data( data );
167 return ret;
170 static BOOL CALLBACK has_managed_popup( HWND hwnd, LPARAM lparam )
172 struct has_popup_result *result = (struct has_popup_result *)lparam;
174 if (hwnd == result->hwnd) return FALSE; /* popups are always above owner */
175 if (GetWindow( hwnd, GW_OWNER ) != result->hwnd) return TRUE;
176 result->found = is_managed( hwnd );
177 return !result->found;
180 static BOOL has_owned_popups( HWND hwnd )
182 struct has_popup_result result;
184 result.hwnd = hwnd;
185 result.found = FALSE;
186 EnumWindows( has_managed_popup, (LPARAM)&result );
187 return result.found;
190 /***********************************************************************
191 * is_window_managed
193 * Check if a given window should be managed
195 static BOOL is_window_managed( HWND hwnd, UINT swp_flags, const RECT *window_rect )
197 DWORD style, ex_style;
199 if (!managed_mode) return FALSE;
201 /* child windows are not managed */
202 style = GetWindowLongW( hwnd, GWL_STYLE );
203 if ((style & (WS_CHILD|WS_POPUP)) == WS_CHILD) return FALSE;
204 /* activated windows are managed */
205 if (!(swp_flags & (SWP_NOACTIVATE|SWP_HIDEWINDOW))) return TRUE;
206 if (hwnd == GetActiveWindow()) return TRUE;
207 /* windows with caption are managed */
208 if ((style & WS_CAPTION) == WS_CAPTION) return TRUE;
209 /* windows with thick frame are managed */
210 if (style & WS_THICKFRAME) return TRUE;
211 if (style & WS_POPUP)
213 HMONITOR hmon;
214 MONITORINFO mi;
216 /* popup with sysmenu == caption are managed */
217 if (style & WS_SYSMENU) return TRUE;
218 /* full-screen popup windows are managed */
219 hmon = MonitorFromWindow( hwnd, MONITOR_DEFAULTTOPRIMARY );
220 mi.cbSize = sizeof( mi );
221 GetMonitorInfoW( hmon, &mi );
222 if (window_rect->left <= mi.rcWork.left && window_rect->right >= mi.rcWork.right &&
223 window_rect->top <= mi.rcWork.top && window_rect->bottom >= mi.rcWork.bottom)
224 return TRUE;
226 /* application windows are managed */
227 ex_style = GetWindowLongW( hwnd, GWL_EXSTYLE );
228 if (ex_style & WS_EX_APPWINDOW) return TRUE;
229 /* windows that own popups are managed */
230 if (has_owned_popups( hwnd )) return TRUE;
231 /* default: not managed */
232 return FALSE;
236 /***********************************************************************
237 * is_window_resizable
239 * Check if window should be made resizable by the window manager
241 static inline BOOL is_window_resizable( struct x11drv_win_data *data, DWORD style )
243 if (style & WS_THICKFRAME) return TRUE;
244 /* Metacity needs the window to be resizable to make it fullscreen */
245 return is_window_rect_fullscreen( &data->whole_rect );
249 /***********************************************************************
250 * get_mwm_decorations
252 static unsigned long get_mwm_decorations( struct x11drv_win_data *data,
253 DWORD style, DWORD ex_style )
255 unsigned long ret = 0;
257 if (!decorated_mode) return 0;
259 if (IsRectEmpty( &data->window_rect )) return 0;
260 if (data->shaped) return 0;
262 if (ex_style & WS_EX_TOOLWINDOW) return 0;
263 if (ex_style & WS_EX_LAYERED) return 0;
265 if ((style & WS_CAPTION) == WS_CAPTION)
267 ret |= MWM_DECOR_TITLE | MWM_DECOR_BORDER;
268 if (style & WS_SYSMENU) ret |= MWM_DECOR_MENU;
269 if (style & WS_MINIMIZEBOX) ret |= MWM_DECOR_MINIMIZE;
270 if (style & WS_MAXIMIZEBOX) ret |= MWM_DECOR_MAXIMIZE;
272 if (ex_style & WS_EX_DLGMODALFRAME) ret |= MWM_DECOR_BORDER;
273 else if (style & WS_THICKFRAME) ret |= MWM_DECOR_BORDER | MWM_DECOR_RESIZEH;
274 else if ((style & (WS_DLGFRAME|WS_BORDER)) == WS_DLGFRAME) ret |= MWM_DECOR_BORDER;
275 return ret;
279 /***********************************************************************
280 * get_x11_rect_offset
282 * Helper for X11DRV_window_to_X_rect and X11DRV_X_to_window_rect.
284 static void get_x11_rect_offset( struct x11drv_win_data *data, RECT *rect )
286 DWORD style, ex_style, style_mask = 0, ex_style_mask = 0;
287 unsigned long decor;
289 rect->top = rect->bottom = rect->left = rect->right = 0;
291 style = GetWindowLongW( data->hwnd, GWL_STYLE );
292 ex_style = GetWindowLongW( data->hwnd, GWL_EXSTYLE );
293 decor = get_mwm_decorations( data, style, ex_style );
295 if (decor & MWM_DECOR_TITLE) style_mask |= WS_CAPTION;
296 if (decor & MWM_DECOR_BORDER)
298 style_mask |= WS_DLGFRAME | WS_THICKFRAME;
299 ex_style_mask |= WS_EX_DLGMODALFRAME;
302 AdjustWindowRectEx( rect, style & style_mask, FALSE, ex_style & ex_style_mask );
306 /***********************************************************************
307 * get_window_attributes
309 * Fill the window attributes structure for an X window.
311 static int get_window_attributes( struct x11drv_win_data *data, XSetWindowAttributes *attr )
313 attr->override_redirect = !data->managed;
314 attr->colormap = data->colormap ? data->colormap : default_colormap;
315 attr->save_under = ((GetClassLongW( data->hwnd, GCL_STYLE ) & CS_SAVEBITS) != 0);
316 attr->bit_gravity = NorthWestGravity;
317 attr->win_gravity = StaticGravity;
318 attr->backing_store = NotUseful;
319 attr->border_pixel = 0;
320 attr->event_mask = (ExposureMask | PointerMotionMask |
321 ButtonPressMask | ButtonReleaseMask | EnterWindowMask |
322 KeyPressMask | KeyReleaseMask | FocusChangeMask |
323 KeymapStateMask | StructureNotifyMask);
324 if (data->managed) attr->event_mask |= PropertyChangeMask;
326 return (CWOverrideRedirect | CWSaveUnder | CWColormap | CWBorderPixel |
327 CWEventMask | CWBitGravity | CWBackingStore);
331 /***********************************************************************
332 * sync_window_style
334 * Change the X window attributes when the window style has changed.
336 static void sync_window_style( struct x11drv_win_data *data )
338 if (data->whole_window != root_window)
340 XSetWindowAttributes attr;
341 int mask = get_window_attributes( data, &attr );
343 XChangeWindowAttributes( data->display, data->whole_window, mask, &attr );
348 /***********************************************************************
349 * sync_window_region
351 * Update the X11 window region.
353 static void sync_window_region( struct x11drv_win_data *data, HRGN win_region )
355 #ifdef HAVE_LIBXSHAPE
356 HRGN hrgn = win_region;
358 if (!data->whole_window) return;
359 data->shaped = FALSE;
361 if (IsRectEmpty( &data->window_rect )) /* set an empty shape */
363 static XRectangle empty_rect;
364 XShapeCombineRectangles( data->display, data->whole_window, ShapeBounding, 0, 0,
365 &empty_rect, 1, ShapeSet, YXBanded );
366 return;
369 if (hrgn == (HRGN)1) /* hack: win_region == 1 means retrieve region from server */
371 if (!(hrgn = CreateRectRgn( 0, 0, 0, 0 ))) return;
372 if (GetWindowRgn( data->hwnd, hrgn ) == ERROR)
374 DeleteObject( hrgn );
375 hrgn = 0;
379 if (!hrgn)
381 XShapeCombineMask( data->display, data->whole_window, ShapeBounding, 0, 0, None, ShapeSet );
383 else
385 RGNDATA *pRegionData;
387 if (GetWindowLongW( data->hwnd, GWL_EXSTYLE ) & WS_EX_LAYOUTRTL) MirrorRgn( data->hwnd, hrgn );
388 if ((pRegionData = X11DRV_GetRegionData( hrgn, 0 )))
390 XShapeCombineRectangles( data->display, data->whole_window, ShapeBounding,
391 data->window_rect.left - data->whole_rect.left,
392 data->window_rect.top - data->whole_rect.top,
393 (XRectangle *)pRegionData->Buffer,
394 pRegionData->rdh.nCount, ShapeSet, YXBanded );
395 HeapFree(GetProcessHeap(), 0, pRegionData);
396 data->shaped = TRUE;
399 if (hrgn && hrgn != win_region) DeleteObject( hrgn );
400 #endif /* HAVE_LIBXSHAPE */
404 /***********************************************************************
405 * sync_window_opacity
407 static void sync_window_opacity( Display *display, Window win,
408 COLORREF key, BYTE alpha, DWORD flags )
410 unsigned long opacity = 0xffffffff;
412 if (flags & LWA_ALPHA) opacity = (0xffffffff / 0xff) * alpha;
414 if (opacity == 0xffffffff)
415 XDeleteProperty( display, win, x11drv_atom(_NET_WM_WINDOW_OPACITY) );
416 else
417 XChangeProperty( display, win, x11drv_atom(_NET_WM_WINDOW_OPACITY),
418 XA_CARDINAL, 32, PropModeReplace, (unsigned char *)&opacity, 1 );
422 /***********************************************************************
423 * sync_window_text
425 static void sync_window_text( Display *display, Window win, const WCHAR *text )
427 UINT count;
428 char *buffer, *utf8_buffer;
429 XTextProperty prop;
431 /* allocate new buffer for window text */
432 count = WideCharToMultiByte(CP_UNIXCP, 0, text, -1, NULL, 0, NULL, NULL);
433 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, count ))) return;
434 WideCharToMultiByte(CP_UNIXCP, 0, text, -1, buffer, count, NULL, NULL);
436 count = WideCharToMultiByte(CP_UTF8, 0, text, strlenW(text), NULL, 0, NULL, NULL);
437 if (!(utf8_buffer = HeapAlloc( GetProcessHeap(), 0, count )))
439 HeapFree( GetProcessHeap(), 0, buffer );
440 return;
442 WideCharToMultiByte(CP_UTF8, 0, text, strlenW(text), utf8_buffer, count, NULL, NULL);
444 if (XmbTextListToTextProperty( display, &buffer, 1, XStdICCTextStyle, &prop ) == Success)
446 XSetWMName( display, win, &prop );
447 XSetWMIconName( display, win, &prop );
448 XFree( prop.value );
451 Implements a NET_WM UTF-8 title. It should be without a trailing \0,
452 according to the standard
453 ( http://www.pps.jussieu.fr/~jch/software/UTF8_STRING/UTF8_STRING.text ).
455 XChangeProperty( display, win, x11drv_atom(_NET_WM_NAME), x11drv_atom(UTF8_STRING),
456 8, PropModeReplace, (unsigned char *) utf8_buffer, count);
458 HeapFree( GetProcessHeap(), 0, utf8_buffer );
459 HeapFree( GetProcessHeap(), 0, buffer );
463 /***********************************************************************
464 * get_bitmap_argb
466 * Return the bitmap bits in ARGB format. Helper for setting icon hints.
468 static unsigned long *get_bitmap_argb( HDC hdc, HBITMAP color, HBITMAP mask, unsigned int *size )
470 char buffer[FIELD_OFFSET( BITMAPINFO, bmiColors[256] )];
471 BITMAPINFO *info = (BITMAPINFO *)buffer;
472 BITMAP bm;
473 unsigned int *ptr, *bits = NULL;
474 unsigned char *mask_bits = NULL;
475 int i, j;
476 BOOL has_alpha = FALSE;
478 if (!GetObjectW( color, sizeof(bm), &bm )) return NULL;
479 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
480 info->bmiHeader.biWidth = bm.bmWidth;
481 info->bmiHeader.biHeight = -bm.bmHeight;
482 info->bmiHeader.biPlanes = 1;
483 info->bmiHeader.biBitCount = 32;
484 info->bmiHeader.biCompression = BI_RGB;
485 info->bmiHeader.biSizeImage = bm.bmWidth * bm.bmHeight * 4;
486 info->bmiHeader.biXPelsPerMeter = 0;
487 info->bmiHeader.biYPelsPerMeter = 0;
488 info->bmiHeader.biClrUsed = 0;
489 info->bmiHeader.biClrImportant = 0;
490 *size = bm.bmWidth * bm.bmHeight + 2;
491 if (!(bits = HeapAlloc( GetProcessHeap(), 0, *size * sizeof(long) ))) goto failed;
492 if (!GetDIBits( hdc, color, 0, bm.bmHeight, bits + 2, info, DIB_RGB_COLORS )) goto failed;
494 bits[0] = bm.bmWidth;
495 bits[1] = bm.bmHeight;
497 for (i = 0; i < bm.bmWidth * bm.bmHeight; i++)
498 if ((has_alpha = (bits[i + 2] & 0xff000000) != 0)) break;
500 if (!has_alpha)
502 unsigned int width_bytes = (bm.bmWidth + 31) / 32 * 4;
503 /* generate alpha channel from the mask */
504 info->bmiHeader.biBitCount = 1;
505 info->bmiHeader.biSizeImage = width_bytes * bm.bmHeight;
506 if (!(mask_bits = HeapAlloc( GetProcessHeap(), 0, info->bmiHeader.biSizeImage ))) goto failed;
507 if (!GetDIBits( hdc, mask, 0, bm.bmHeight, mask_bits, info, DIB_RGB_COLORS )) goto failed;
508 ptr = bits + 2;
509 for (i = 0; i < bm.bmHeight; i++)
510 for (j = 0; j < bm.bmWidth; j++, ptr++)
511 if (!((mask_bits[i * width_bytes + j / 8] << (j % 8)) & 0x80)) *ptr |= 0xff000000;
512 HeapFree( GetProcessHeap(), 0, mask_bits );
515 /* convert to array of longs */
516 if (bits && sizeof(long) > sizeof(int))
517 for (i = *size - 1; i >= 0; i--) ((unsigned long *)bits)[i] = bits[i];
519 return (unsigned long *)bits;
521 failed:
522 HeapFree( GetProcessHeap(), 0, bits );
523 HeapFree( GetProcessHeap(), 0, mask_bits );
524 return NULL;
528 /***********************************************************************
529 * create_icon_pixmaps
531 static BOOL create_icon_pixmaps( HDC hdc, const ICONINFO *icon, Pixmap *icon_ret, Pixmap *mask_ret )
533 char buffer[FIELD_OFFSET( BITMAPINFO, bmiColors[256] )];
534 BITMAPINFO *info = (BITMAPINFO *)buffer;
535 XVisualInfo vis = default_visual;
536 struct gdi_image_bits bits;
537 Pixmap color_pixmap = 0, mask_pixmap = 0;
538 int lines;
539 unsigned int i;
541 bits.ptr = NULL;
542 bits.free = NULL;
543 bits.is_copy = TRUE;
545 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
546 info->bmiHeader.biBitCount = 0;
547 if (!(lines = GetDIBits( hdc, icon->hbmColor, 0, 0, NULL, info, DIB_RGB_COLORS ))) goto failed;
548 if (!(bits.ptr = HeapAlloc( GetProcessHeap(), 0, info->bmiHeader.biSizeImage ))) goto failed;
549 if (!GetDIBits( hdc, icon->hbmColor, 0, lines, bits.ptr, info, DIB_RGB_COLORS )) goto failed;
551 color_pixmap = create_pixmap_from_image( hdc, &vis, info, &bits, DIB_RGB_COLORS );
552 HeapFree( GetProcessHeap(), 0, bits.ptr );
553 bits.ptr = NULL;
554 if (!color_pixmap) goto failed;
556 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
557 info->bmiHeader.biBitCount = 0;
558 if (!(lines = GetDIBits( hdc, icon->hbmMask, 0, 0, NULL, info, DIB_RGB_COLORS ))) goto failed;
559 if (!(bits.ptr = HeapAlloc( GetProcessHeap(), 0, info->bmiHeader.biSizeImage ))) goto failed;
560 if (!GetDIBits( hdc, icon->hbmMask, 0, lines, bits.ptr, info, DIB_RGB_COLORS )) goto failed;
562 /* invert the mask */
563 for (i = 0; i < info->bmiHeader.biSizeImage / sizeof(DWORD); i++) ((DWORD *)bits.ptr)[i] ^= ~0u;
565 vis.depth = 1;
566 mask_pixmap = create_pixmap_from_image( hdc, &vis, info, &bits, DIB_RGB_COLORS );
567 HeapFree( GetProcessHeap(), 0, bits.ptr );
568 bits.ptr = NULL;
569 if (!mask_pixmap) goto failed;
571 *icon_ret = color_pixmap;
572 *mask_ret = mask_pixmap;
573 return TRUE;
575 failed:
576 if (color_pixmap) XFreePixmap( gdi_display, color_pixmap );
577 if (mask_pixmap) XFreePixmap( gdi_display, mask_pixmap );
578 HeapFree( GetProcessHeap(), 0, bits.ptr );
579 return FALSE;
583 /***********************************************************************
584 * fetch_icon_data
586 static void fetch_icon_data( HWND hwnd, HICON icon_big, HICON icon_small )
588 struct x11drv_win_data *data;
589 ICONINFO ii, ii_small;
590 HDC hDC;
591 unsigned int size;
592 unsigned long *bits;
593 Pixmap icon_pixmap, mask_pixmap;
595 if (!icon_big)
597 icon_big = (HICON)SendMessageW( hwnd, WM_GETICON, ICON_BIG, 0 );
598 if (!icon_big) icon_big = (HICON)GetClassLongPtrW( hwnd, GCLP_HICON );
599 if (!icon_big) icon_big = LoadIconW( 0, (LPWSTR)IDI_WINLOGO );
601 if (!icon_small)
603 icon_small = (HICON)SendMessageW( hwnd, WM_GETICON, ICON_SMALL, 0 );
604 if (!icon_small) icon_small = (HICON)GetClassLongPtrW( hwnd, GCLP_HICONSM );
607 if (!GetIconInfo(icon_big, &ii)) return;
609 hDC = CreateCompatibleDC(0);
610 bits = get_bitmap_argb( hDC, ii.hbmColor, ii.hbmMask, &size );
611 if (bits && GetIconInfo( icon_small, &ii_small ))
613 unsigned int size_small;
614 unsigned long *bits_small, *new;
616 if ((bits_small = get_bitmap_argb( hDC, ii_small.hbmColor, ii_small.hbmMask, &size_small )) &&
617 (bits_small[0] != bits[0] || bits_small[1] != bits[1])) /* size must be different */
619 if ((new = HeapReAlloc( GetProcessHeap(), 0, bits,
620 (size + size_small) * sizeof(unsigned long) )))
622 bits = new;
623 memcpy( bits + size, bits_small, size_small * sizeof(unsigned long) );
624 size += size_small;
627 HeapFree( GetProcessHeap(), 0, bits_small );
628 DeleteObject( ii_small.hbmColor );
629 DeleteObject( ii_small.hbmMask );
632 if (!create_icon_pixmaps( hDC, &ii, &icon_pixmap, &mask_pixmap )) icon_pixmap = mask_pixmap = 0;
634 DeleteObject( ii.hbmColor );
635 DeleteObject( ii.hbmMask );
636 DeleteDC(hDC);
638 if ((data = get_win_data( hwnd )))
640 if (data->icon_pixmap) XFreePixmap( gdi_display, data->icon_pixmap );
641 if (data->icon_mask) XFreePixmap( gdi_display, data->icon_mask );
642 HeapFree( GetProcessHeap(), 0, data->icon_bits );
643 data->icon_pixmap = icon_pixmap;
644 data->icon_mask = mask_pixmap;
645 data->icon_bits = bits;
646 data->icon_size = size;
647 release_win_data( data );
649 else
651 if (icon_pixmap) XFreePixmap( gdi_display, icon_pixmap );
652 if (mask_pixmap) XFreePixmap( gdi_display, mask_pixmap );
653 HeapFree( GetProcessHeap(), 0, bits );
658 /***********************************************************************
659 * set_size_hints
661 * set the window size hints
663 static void set_size_hints( struct x11drv_win_data *data, DWORD style )
665 XSizeHints* size_hints;
667 if (!(size_hints = XAllocSizeHints())) return;
669 size_hints->win_gravity = StaticGravity;
670 size_hints->flags |= PWinGravity;
672 /* don't update size hints if window is not in normal state */
673 if (!(style & (WS_MINIMIZE | WS_MAXIMIZE)))
675 if (data->hwnd != GetDesktopWindow()) /* don't force position of desktop */
677 size_hints->x = data->whole_rect.left;
678 size_hints->y = data->whole_rect.top;
679 size_hints->flags |= PPosition;
681 else size_hints->win_gravity = NorthWestGravity;
683 if (!is_window_resizable( data, style ))
685 size_hints->max_width = data->whole_rect.right - data->whole_rect.left;
686 size_hints->max_height = data->whole_rect.bottom - data->whole_rect.top;
687 if (size_hints->max_width <= 0 ||size_hints->max_height <= 0)
688 size_hints->max_width = size_hints->max_height = 1;
689 size_hints->min_width = size_hints->max_width;
690 size_hints->min_height = size_hints->max_height;
691 size_hints->flags |= PMinSize | PMaxSize;
694 XSetWMNormalHints( data->display, data->whole_window, size_hints );
695 XFree( size_hints );
699 /***********************************************************************
700 * set_mwm_hints
702 static void set_mwm_hints( struct x11drv_win_data *data, DWORD style, DWORD ex_style )
704 MwmHints mwm_hints;
706 if (data->hwnd == GetDesktopWindow())
708 if (is_desktop_fullscreen()) mwm_hints.decorations = 0;
709 else mwm_hints.decorations = MWM_DECOR_TITLE | MWM_DECOR_BORDER | MWM_DECOR_MENU | MWM_DECOR_MINIMIZE;
710 mwm_hints.functions = MWM_FUNC_MOVE | MWM_FUNC_MINIMIZE | MWM_FUNC_CLOSE;
712 else
714 mwm_hints.decorations = get_mwm_decorations( data, style, ex_style );
715 mwm_hints.functions = MWM_FUNC_MOVE;
716 if (is_window_resizable( data, style )) mwm_hints.functions |= MWM_FUNC_RESIZE;
717 if (!(style & WS_DISABLED))
719 if (style & WS_MINIMIZEBOX) mwm_hints.functions |= MWM_FUNC_MINIMIZE;
720 if (style & WS_MAXIMIZEBOX) mwm_hints.functions |= MWM_FUNC_MAXIMIZE;
721 if (style & WS_SYSMENU) mwm_hints.functions |= MWM_FUNC_CLOSE;
725 TRACE( "%p setting mwm hints to %lx,%lx (style %x exstyle %x)\n",
726 data->hwnd, mwm_hints.decorations, mwm_hints.functions, style, ex_style );
728 mwm_hints.flags = MWM_HINTS_FUNCTIONS | MWM_HINTS_DECORATIONS;
729 XChangeProperty( data->display, data->whole_window, x11drv_atom(_MOTIF_WM_HINTS),
730 x11drv_atom(_MOTIF_WM_HINTS), 32, PropModeReplace,
731 (unsigned char*)&mwm_hints, sizeof(mwm_hints)/sizeof(long) );
735 /***********************************************************************
736 * set_style_hints
738 static void set_style_hints( struct x11drv_win_data *data, DWORD style, DWORD ex_style )
740 Window group_leader = data->whole_window;
741 HWND owner = GetWindow( data->hwnd, GW_OWNER );
742 Window owner_win = X11DRV_get_whole_window( owner );
743 XWMHints *wm_hints;
744 Atom window_type;
746 if (owner_win)
748 XSetTransientForHint( data->display, data->whole_window, owner_win );
749 group_leader = owner_win;
752 /* Only use dialog type for owned popups. Metacity allows making fullscreen
753 * only normal windows, and doesn't handle correctly TRANSIENT_FOR hint for
754 * dialogs owned by fullscreen windows.
756 if (((style & WS_POPUP) || (ex_style & WS_EX_DLGMODALFRAME)) && owner)
757 window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_DIALOG);
758 else
759 window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_NORMAL);
761 XChangeProperty(data->display, data->whole_window, x11drv_atom(_NET_WM_WINDOW_TYPE),
762 XA_ATOM, 32, PropModeReplace, (unsigned char*)&window_type, 1);
764 if ((wm_hints = XAllocWMHints()))
766 wm_hints->flags = InputHint | StateHint | WindowGroupHint;
767 wm_hints->input = !use_take_focus && !(style & WS_DISABLED);
768 wm_hints->initial_state = (style & WS_MINIMIZE) ? IconicState : NormalState;
769 wm_hints->window_group = group_leader;
770 if (data->icon_pixmap)
772 wm_hints->icon_pixmap = data->icon_pixmap;
773 wm_hints->icon_mask = data->icon_mask;
774 wm_hints->flags |= IconPixmapHint | IconMaskHint;
776 XSetWMHints( data->display, data->whole_window, wm_hints );
777 XFree( wm_hints );
780 if (data->icon_bits)
781 XChangeProperty( data->display, data->whole_window, x11drv_atom(_NET_WM_ICON),
782 XA_CARDINAL, 32, PropModeReplace,
783 (unsigned char *)data->icon_bits, data->icon_size );
784 else
785 XDeleteProperty( data->display, data->whole_window, x11drv_atom(_NET_WM_ICON) );
790 /***********************************************************************
791 * get_process_name
793 * get the name of the current process for setting class hints
795 static char *get_process_name(void)
797 static char *name;
799 if (!name)
801 WCHAR module[MAX_PATH];
802 DWORD len = GetModuleFileNameW( 0, module, MAX_PATH );
803 if (len && len < MAX_PATH)
805 char *ptr;
806 WCHAR *p, *appname = module;
808 if ((p = strrchrW( appname, '/' ))) appname = p + 1;
809 if ((p = strrchrW( appname, '\\' ))) appname = p + 1;
810 len = WideCharToMultiByte( CP_UNIXCP, 0, appname, -1, NULL, 0, NULL, NULL );
811 if ((ptr = HeapAlloc( GetProcessHeap(), 0, len )))
813 WideCharToMultiByte( CP_UNIXCP, 0, appname, -1, ptr, len, NULL, NULL );
814 name = ptr;
818 return name;
822 /***********************************************************************
823 * set_initial_wm_hints
825 * Set the window manager hints that don't change over the lifetime of a window.
827 static void set_initial_wm_hints( Display *display, Window window )
829 long i;
830 Atom protocols[3];
831 Atom dndVersion = WINE_XDND_VERSION;
832 XClassHint *class_hints;
833 char *process_name = get_process_name();
835 /* wm protocols */
836 i = 0;
837 protocols[i++] = x11drv_atom(WM_DELETE_WINDOW);
838 protocols[i++] = x11drv_atom(_NET_WM_PING);
839 if (use_take_focus) protocols[i++] = x11drv_atom(WM_TAKE_FOCUS);
840 XChangeProperty( display, window, x11drv_atom(WM_PROTOCOLS),
841 XA_ATOM, 32, PropModeReplace, (unsigned char *)protocols, i );
843 /* class hints */
844 if ((class_hints = XAllocClassHint()))
846 static char wine[] = "Wine";
848 class_hints->res_name = process_name;
849 class_hints->res_class = wine;
850 XSetClassHint( display, window, class_hints );
851 XFree( class_hints );
854 /* set the WM_CLIENT_MACHINE and WM_LOCALE_NAME properties */
855 XSetWMProperties(display, window, NULL, NULL, NULL, 0, NULL, NULL, NULL);
856 /* set the pid. together, these properties are needed so the window manager can kill us if we freeze */
857 i = getpid();
858 XChangeProperty(display, window, x11drv_atom(_NET_WM_PID),
859 XA_CARDINAL, 32, PropModeReplace, (unsigned char *)&i, 1);
861 XChangeProperty( display, window, x11drv_atom(XdndAware),
862 XA_ATOM, 32, PropModeReplace, (unsigned char*)&dndVersion, 1 );
864 update_user_time( 0 ); /* make sure that the user time window exists */
865 if (user_time_window)
866 XChangeProperty( display, window, x11drv_atom(_NET_WM_USER_TIME_WINDOW),
867 XA_WINDOW, 32, PropModeReplace, (unsigned char *)&user_time_window, 1 );
871 /***********************************************************************
872 * make_owner_managed
874 * If the window is managed, make sure its owner window is too.
876 static void make_owner_managed( HWND hwnd )
878 HWND owner;
880 if (!(owner = GetWindow( hwnd, GW_OWNER ))) return;
881 if (is_managed( owner )) return;
882 if (!is_managed( hwnd )) return;
884 SetWindowPos( owner, 0, 0, 0, 0, 0,
885 SWP_NOACTIVATE | SWP_NOZORDER | SWP_NOSIZE | SWP_NOMOVE |
886 SWP_NOREDRAW | SWP_DEFERERASE | SWP_NOSENDCHANGING | SWP_STATECHANGED );
890 /***********************************************************************
891 * set_wm_hints
893 * Set all the window manager hints for a window.
895 static void set_wm_hints( struct x11drv_win_data *data )
897 DWORD style, ex_style;
899 if (data->hwnd == GetDesktopWindow())
901 /* force some styles for the desktop to get the correct decorations */
902 style = WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX;
903 ex_style = WS_EX_APPWINDOW;
905 else
907 style = GetWindowLongW( data->hwnd, GWL_STYLE );
908 ex_style = GetWindowLongW( data->hwnd, GWL_EXSTYLE );
911 set_size_hints( data, style );
912 set_mwm_hints( data, style, ex_style );
913 set_style_hints( data, style, ex_style );
917 /***********************************************************************
918 * init_clip_window
920 Window init_clip_window(void)
922 struct x11drv_thread_data *data = x11drv_init_thread_data();
924 if (!data->clip_window &&
925 (data->clip_window = (Window)GetPropA( GetDesktopWindow(), clip_window_prop )))
927 XSelectInput( data->display, data->clip_window, StructureNotifyMask );
929 return data->clip_window;
933 /***********************************************************************
934 * update_user_time
936 void update_user_time( Time time )
938 if (!user_time_window)
940 Window win = XCreateWindow( gdi_display, root_window, -1, -1, 1, 1, 0, CopyFromParent,
941 InputOnly, CopyFromParent, 0, NULL );
942 if (InterlockedCompareExchangePointer( (void **)&user_time_window, (void *)win, 0 ))
943 XDestroyWindow( gdi_display, win );
944 TRACE( "user time window %lx\n", user_time_window );
947 if (!time) return;
948 XLockDisplay( gdi_display );
949 if (!last_user_time || (long)(time - last_user_time) > 0)
951 last_user_time = time;
952 XChangeProperty( gdi_display, user_time_window, x11drv_atom(_NET_WM_USER_TIME),
953 XA_CARDINAL, 32, PropModeReplace, (unsigned char *)&time, 1 );
955 XUnlockDisplay( gdi_display );
958 /***********************************************************************
959 * update_net_wm_states
961 void update_net_wm_states( struct x11drv_win_data *data )
963 static const unsigned int state_atoms[NB_NET_WM_STATES] =
965 XATOM__NET_WM_STATE_FULLSCREEN,
966 XATOM__NET_WM_STATE_ABOVE,
967 XATOM__NET_WM_STATE_MAXIMIZED_VERT,
968 XATOM__NET_WM_STATE_SKIP_PAGER,
969 XATOM__NET_WM_STATE_SKIP_TASKBAR
972 DWORD i, style, ex_style, new_state = 0;
974 if (!data->managed) return;
975 if (data->whole_window == root_window) return;
977 style = GetWindowLongW( data->hwnd, GWL_STYLE );
978 if (is_window_rect_fullscreen( &data->whole_rect ))
980 if ((style & WS_MAXIMIZE) && (style & WS_CAPTION) == WS_CAPTION)
981 new_state |= (1 << NET_WM_STATE_MAXIMIZED);
982 else if (!(style & WS_MINIMIZE))
983 new_state |= (1 << NET_WM_STATE_FULLSCREEN);
985 else if (style & WS_MAXIMIZE)
986 new_state |= (1 << NET_WM_STATE_MAXIMIZED);
988 ex_style = GetWindowLongW( data->hwnd, GWL_EXSTYLE );
989 if (ex_style & WS_EX_TOPMOST)
990 new_state |= (1 << NET_WM_STATE_ABOVE);
991 if (ex_style & (WS_EX_TOOLWINDOW | WS_EX_NOACTIVATE))
992 new_state |= (1 << NET_WM_STATE_SKIP_TASKBAR) | (1 << NET_WM_STATE_SKIP_PAGER);
993 if (!(ex_style & WS_EX_APPWINDOW) && GetWindow( data->hwnd, GW_OWNER ))
994 new_state |= (1 << NET_WM_STATE_SKIP_TASKBAR);
996 if (!data->mapped) /* set the _NET_WM_STATE atom directly */
998 Atom atoms[NB_NET_WM_STATES+1];
999 DWORD count;
1001 for (i = count = 0; i < NB_NET_WM_STATES; i++)
1003 if (!(new_state & (1 << i))) continue;
1004 TRACE( "setting wm state %u for unmapped window %p/%lx\n",
1005 i, data->hwnd, data->whole_window );
1006 atoms[count++] = X11DRV_Atoms[state_atoms[i] - FIRST_XATOM];
1007 if (state_atoms[i] == XATOM__NET_WM_STATE_MAXIMIZED_VERT)
1008 atoms[count++] = x11drv_atom(_NET_WM_STATE_MAXIMIZED_HORZ);
1010 XChangeProperty( data->display, data->whole_window, x11drv_atom(_NET_WM_STATE), XA_ATOM,
1011 32, PropModeReplace, (unsigned char *)atoms, count );
1013 else /* ask the window manager to do it for us */
1015 XEvent xev;
1017 xev.xclient.type = ClientMessage;
1018 xev.xclient.window = data->whole_window;
1019 xev.xclient.message_type = x11drv_atom(_NET_WM_STATE);
1020 xev.xclient.serial = 0;
1021 xev.xclient.display = data->display;
1022 xev.xclient.send_event = True;
1023 xev.xclient.format = 32;
1024 xev.xclient.data.l[3] = 1;
1026 for (i = 0; i < NB_NET_WM_STATES; i++)
1028 if (!((data->net_wm_state ^ new_state) & (1 << i))) continue; /* unchanged */
1030 TRACE( "setting wm state %u for window %p/%lx to %u prev %u\n",
1031 i, data->hwnd, data->whole_window,
1032 (new_state & (1 << i)) != 0, (data->net_wm_state & (1 << i)) != 0 );
1034 xev.xclient.data.l[0] = (new_state & (1 << i)) ? _NET_WM_STATE_ADD : _NET_WM_STATE_REMOVE;
1035 xev.xclient.data.l[1] = X11DRV_Atoms[state_atoms[i] - FIRST_XATOM];
1036 xev.xclient.data.l[2] = ((state_atoms[i] == XATOM__NET_WM_STATE_MAXIMIZED_VERT) ?
1037 x11drv_atom(_NET_WM_STATE_MAXIMIZED_HORZ) : 0);
1038 XSendEvent( data->display, root_window, False,
1039 SubstructureRedirectMask | SubstructureNotifyMask, &xev );
1042 data->net_wm_state = new_state;
1046 /***********************************************************************
1047 * set_xembed_flags
1049 static void set_xembed_flags( struct x11drv_win_data *data, unsigned long flags )
1051 unsigned long info[2];
1053 if (!data->whole_window) return;
1055 info[0] = 0; /* protocol version */
1056 info[1] = flags;
1057 XChangeProperty( data->display, data->whole_window, x11drv_atom(_XEMBED_INFO),
1058 x11drv_atom(_XEMBED_INFO), 32, PropModeReplace, (unsigned char*)info, 2 );
1062 /***********************************************************************
1063 * map_window
1065 static void map_window( HWND hwnd, DWORD new_style )
1067 struct x11drv_win_data *data;
1069 make_owner_managed( hwnd );
1070 wait_for_withdrawn_state( hwnd, TRUE );
1072 if (!(data = get_win_data( hwnd ))) return;
1074 if (data->whole_window && !data->mapped)
1076 TRACE( "win %p/%lx\n", data->hwnd, data->whole_window );
1078 remove_startup_notification( data->display, data->whole_window );
1079 set_wm_hints( data );
1081 if (!data->embedded)
1083 update_net_wm_states( data );
1084 sync_window_style( data );
1085 XMapWindow( data->display, data->whole_window );
1086 XFlush( data->display );
1087 if (data->surface && data->vis.visualid != default_visual.visualid)
1088 data->surface->funcs->flush( data->surface );
1090 else set_xembed_flags( data, XEMBED_MAPPED );
1092 data->mapped = TRUE;
1093 data->iconic = (new_style & WS_MINIMIZE) != 0;
1095 release_win_data( data );
1099 /***********************************************************************
1100 * unmap_window
1102 static void unmap_window( HWND hwnd )
1104 struct x11drv_win_data *data;
1106 wait_for_withdrawn_state( hwnd, FALSE );
1108 if (!(data = get_win_data( hwnd ))) return;
1110 if (data->mapped)
1112 TRACE( "win %p/%lx\n", data->hwnd, data->whole_window );
1114 if (data->embedded) set_xembed_flags( data, 0 );
1115 else if (!data->managed) XUnmapWindow( data->display, data->whole_window );
1116 else XWithdrawWindow( data->display, data->whole_window, data->vis.screen );
1118 data->mapped = FALSE;
1119 data->net_wm_state = 0;
1121 release_win_data( data );
1125 /***********************************************************************
1126 * make_window_embedded
1128 void make_window_embedded( struct x11drv_win_data *data )
1130 /* the window cannot be mapped before being embedded */
1131 if (data->mapped)
1133 if (data->managed) XUnmapWindow( data->display, data->whole_window );
1134 else XWithdrawWindow( data->display, data->whole_window, data->vis.screen );
1135 data->net_wm_state = 0;
1137 data->embedded = TRUE;
1138 data->managed = TRUE;
1139 sync_window_style( data );
1140 set_xembed_flags( data, data->mapped ? XEMBED_MAPPED : 0 );
1144 /***********************************************************************
1145 * X11DRV_window_to_X_rect
1147 * Convert a rect from client to X window coordinates
1149 static void X11DRV_window_to_X_rect( struct x11drv_win_data *data, RECT *rect )
1151 RECT rc;
1153 if (!data->managed) return;
1154 if (IsRectEmpty( rect )) return;
1156 get_x11_rect_offset( data, &rc );
1158 rect->left -= rc.left;
1159 rect->right -= rc.right;
1160 rect->top -= rc.top;
1161 rect->bottom -= rc.bottom;
1162 if (rect->top >= rect->bottom) rect->bottom = rect->top + 1;
1163 if (rect->left >= rect->right) rect->right = rect->left + 1;
1167 /***********************************************************************
1168 * X11DRV_X_to_window_rect
1170 * Opposite of X11DRV_window_to_X_rect
1172 void X11DRV_X_to_window_rect( struct x11drv_win_data *data, RECT *rect )
1174 RECT rc;
1176 if (!data->managed) return;
1177 if (IsRectEmpty( rect )) return;
1179 get_x11_rect_offset( data, &rc );
1181 rect->left += rc.left;
1182 rect->right += rc.right;
1183 rect->top += rc.top;
1184 rect->bottom += rc.bottom;
1185 if (rect->top >= rect->bottom) rect->bottom = rect->top + 1;
1186 if (rect->left >= rect->right) rect->right = rect->left + 1;
1190 /***********************************************************************
1191 * sync_window_position
1193 * Synchronize the X window position with the Windows one
1195 static void sync_window_position( struct x11drv_win_data *data,
1196 UINT swp_flags, const RECT *old_window_rect,
1197 const RECT *old_whole_rect, const RECT *old_client_rect )
1199 DWORD style = GetWindowLongW( data->hwnd, GWL_STYLE );
1200 DWORD ex_style = GetWindowLongW( data->hwnd, GWL_EXSTYLE );
1201 XWindowChanges changes;
1202 unsigned int mask = 0;
1204 if (data->managed && data->iconic) return;
1206 /* resizing a managed maximized window is not allowed */
1207 if (!(style & WS_MAXIMIZE) || !data->managed)
1209 changes.width = data->whole_rect.right - data->whole_rect.left;
1210 changes.height = data->whole_rect.bottom - data->whole_rect.top;
1211 /* if window rect is empty force size to 1x1 */
1212 if (changes.width <= 0 || changes.height <= 0) changes.width = changes.height = 1;
1213 if (changes.width > 65535) changes.width = 65535;
1214 if (changes.height > 65535) changes.height = 65535;
1215 mask |= CWWidth | CWHeight;
1218 /* only the size is allowed to change for the desktop window */
1219 if (data->whole_window != root_window)
1221 POINT pt = virtual_screen_to_root( data->whole_rect.left, data->whole_rect.top );
1222 changes.x = pt.x;
1223 changes.y = pt.y;
1224 mask |= CWX | CWY;
1227 if (!(swp_flags & SWP_NOZORDER) || (swp_flags & SWP_SHOWWINDOW))
1229 /* find window that this one must be after */
1230 HWND prev = GetWindow( data->hwnd, GW_HWNDPREV );
1231 while (prev && !(GetWindowLongW( prev, GWL_STYLE ) & WS_VISIBLE))
1232 prev = GetWindow( prev, GW_HWNDPREV );
1233 if (!prev) /* top child */
1235 changes.stack_mode = Above;
1236 mask |= CWStackMode;
1238 /* should use stack_mode Below but most window managers don't get it right */
1239 /* and Above with a sibling doesn't work so well either, so we ignore it */
1242 set_size_hints( data, style );
1243 set_mwm_hints( data, style, ex_style );
1244 data->configure_serial = NextRequest( data->display );
1245 XReconfigureWMWindow( data->display, data->whole_window, data->vis.screen, mask, &changes );
1246 #ifdef HAVE_LIBXSHAPE
1247 if (IsRectEmpty( old_window_rect ) != IsRectEmpty( &data->window_rect ))
1248 sync_window_region( data, (HRGN)1 );
1249 if (data->shaped)
1251 int old_x_offset = old_window_rect->left - old_whole_rect->left;
1252 int old_y_offset = old_window_rect->top - old_whole_rect->top;
1253 int new_x_offset = data->window_rect.left - data->whole_rect.left;
1254 int new_y_offset = data->window_rect.top - data->whole_rect.top;
1255 if (old_x_offset != new_x_offset || old_y_offset != new_y_offset)
1256 XShapeOffsetShape( data->display, data->whole_window, ShapeBounding,
1257 new_x_offset - old_x_offset, new_y_offset - old_y_offset );
1259 #endif
1261 TRACE( "win %p/%lx pos %d,%d,%dx%d after %lx changes=%x serial=%lu\n",
1262 data->hwnd, data->whole_window, data->whole_rect.left, data->whole_rect.top,
1263 data->whole_rect.right - data->whole_rect.left,
1264 data->whole_rect.bottom - data->whole_rect.top,
1265 changes.sibling, mask, data->configure_serial );
1269 /***********************************************************************
1270 * sync_client_position
1272 * Synchronize the X client window position with the Windows one
1274 static void sync_client_position( struct x11drv_win_data *data,
1275 const RECT *old_client_rect, const RECT *old_whole_rect )
1277 int mask = 0;
1278 XWindowChanges changes;
1280 if (!data->client_window) return;
1282 changes.x = data->client_rect.left - data->whole_rect.left;
1283 changes.y = data->client_rect.top - data->whole_rect.top;
1284 changes.width = min( max( 1, data->client_rect.right - data->client_rect.left ), 65535 );
1285 changes.height = min( max( 1, data->client_rect.bottom - data->client_rect.top ), 65535 );
1287 if (changes.x != old_client_rect->left - old_whole_rect->left) mask |= CWX;
1288 if (changes.y != old_client_rect->top - old_whole_rect->top) mask |= CWY;
1289 if (changes.width != old_client_rect->right - old_client_rect->left) mask |= CWWidth;
1290 if (changes.height != old_client_rect->bottom - old_client_rect->top) mask |= CWHeight;
1292 if (mask)
1294 TRACE( "setting client win %lx pos %d,%d,%dx%d changes=%x\n",
1295 data->client_window, changes.x, changes.y, changes.width, changes.height, mask );
1296 XConfigureWindow( data->display, data->client_window, mask, &changes );
1301 /***********************************************************************
1302 * move_window_bits
1304 * Move the window bits when a window is moved.
1306 static void move_window_bits( HWND hwnd, Window window, const RECT *old_rect, const RECT *new_rect,
1307 const RECT *old_client_rect, const RECT *new_client_rect,
1308 const RECT *new_window_rect )
1310 RECT src_rect = *old_rect;
1311 RECT dst_rect = *new_rect;
1312 HDC hdc_src, hdc_dst;
1313 INT code;
1314 HRGN rgn;
1315 HWND parent = 0;
1317 if (!window)
1319 OffsetRect( &dst_rect, -new_window_rect->left, -new_window_rect->top );
1320 parent = GetAncestor( hwnd, GA_PARENT );
1321 hdc_src = GetDCEx( parent, 0, DCX_CACHE );
1322 hdc_dst = GetDCEx( hwnd, 0, DCX_CACHE | DCX_WINDOW );
1324 else
1326 OffsetRect( &dst_rect, -new_client_rect->left, -new_client_rect->top );
1327 /* make src rect relative to the old position of the window */
1328 OffsetRect( &src_rect, -old_client_rect->left, -old_client_rect->top );
1329 if (dst_rect.left == src_rect.left && dst_rect.top == src_rect.top) return;
1330 hdc_src = hdc_dst = GetDCEx( hwnd, 0, DCX_CACHE );
1333 rgn = CreateRectRgnIndirect( &dst_rect );
1334 SelectClipRgn( hdc_dst, rgn );
1335 DeleteObject( rgn );
1336 ExcludeUpdateRgn( hdc_dst, hwnd );
1338 code = X11DRV_START_EXPOSURES;
1339 ExtEscape( hdc_dst, X11DRV_ESCAPE, sizeof(code), (LPSTR)&code, 0, NULL );
1341 TRACE( "copying bits for win %p/%lx %s -> %s\n",
1342 hwnd, window, wine_dbgstr_rect(&src_rect), wine_dbgstr_rect(&dst_rect) );
1343 BitBlt( hdc_dst, dst_rect.left, dst_rect.top,
1344 dst_rect.right - dst_rect.left, dst_rect.bottom - dst_rect.top,
1345 hdc_src, src_rect.left, src_rect.top, SRCCOPY );
1347 rgn = 0;
1348 code = X11DRV_END_EXPOSURES;
1349 ExtEscape( hdc_dst, X11DRV_ESCAPE, sizeof(code), (LPSTR)&code, sizeof(rgn), (LPSTR)&rgn );
1351 ReleaseDC( hwnd, hdc_dst );
1352 if (hdc_src != hdc_dst) ReleaseDC( parent, hdc_src );
1354 if (rgn)
1356 if (!window)
1358 /* map region to client rect since we are using DCX_WINDOW */
1359 OffsetRgn( rgn, new_window_rect->left - new_client_rect->left,
1360 new_window_rect->top - new_client_rect->top );
1361 RedrawWindow( hwnd, NULL, rgn,
1362 RDW_INVALIDATE | RDW_FRAME | RDW_ERASE | RDW_ALLCHILDREN );
1364 else RedrawWindow( hwnd, NULL, rgn, RDW_INVALIDATE | RDW_ERASE | RDW_ALLCHILDREN );
1365 DeleteObject( rgn );
1370 /**********************************************************************
1371 * create_client_window
1373 Window create_client_window( struct x11drv_win_data *data, const XVisualInfo *visual )
1375 XSetWindowAttributes attr;
1376 int x = data->client_rect.left - data->whole_rect.left;
1377 int y = data->client_rect.top - data->whole_rect.top;
1378 int cx = min( max( 1, data->client_rect.right - data->client_rect.left ), 65535 );
1379 int cy = min( max( 1, data->client_rect.bottom - data->client_rect.top ), 65535 );
1381 data->colormap = XCreateColormap( data->display, root_window, visual->visual,
1382 (visual->class == PseudoColor ||
1383 visual->class == GrayScale ||
1384 visual->class == DirectColor) ? AllocAll : AllocNone );
1385 attr.colormap = data->colormap;
1386 attr.bit_gravity = NorthWestGravity;
1387 attr.win_gravity = NorthWestGravity;
1388 attr.backing_store = NotUseful;
1389 attr.event_mask = ExposureMask;
1390 attr.border_pixel = 0;
1392 data->client_window = XCreateWindow( data->display, data->whole_window, x, y, cx, cy,
1393 0, default_visual.depth, InputOutput, visual->visual,
1394 CWBitGravity | CWWinGravity | CWBackingStore |
1395 CWColormap | CWEventMask | CWBorderPixel, &attr );
1396 if (!data->client_window) return 0;
1398 XSaveContext( data->display, data->client_window, winContext, (char *)data->hwnd );
1399 XMapWindow( data->display, data->client_window );
1400 XSync( data->display, False );
1401 return data->client_window;
1405 /**********************************************************************
1406 * create_whole_window
1408 * Create the whole X window for a given window
1410 static void create_whole_window( struct x11drv_win_data *data )
1412 int cx, cy, mask;
1413 XSetWindowAttributes attr;
1414 WCHAR text[1024];
1415 COLORREF key;
1416 BYTE alpha;
1417 DWORD layered_flags;
1418 HRGN win_rgn;
1419 POINT pos;
1421 if (!data->managed && is_window_managed( data->hwnd, SWP_NOACTIVATE, &data->window_rect ))
1423 TRACE( "making win %p/%lx managed\n", data->hwnd, data->whole_window );
1424 data->managed = TRUE;
1427 if ((win_rgn = CreateRectRgn( 0, 0, 0, 0 )) && GetWindowRgn( data->hwnd, win_rgn ) == ERROR)
1429 DeleteObject( win_rgn );
1430 win_rgn = 0;
1432 data->shaped = (win_rgn != 0);
1434 if (data->vis.visualid != default_visual.visualid)
1435 data->colormap = XCreateColormap( data->display, root_window, data->vis.visual, AllocNone );
1437 mask = get_window_attributes( data, &attr );
1439 data->whole_rect = data->window_rect;
1440 X11DRV_window_to_X_rect( data, &data->whole_rect );
1441 if (!(cx = data->whole_rect.right - data->whole_rect.left)) cx = 1;
1442 else if (cx > 65535) cx = 65535;
1443 if (!(cy = data->whole_rect.bottom - data->whole_rect.top)) cy = 1;
1444 else if (cy > 65535) cy = 65535;
1446 pos = virtual_screen_to_root( data->whole_rect.left, data->whole_rect.top );
1447 data->whole_window = XCreateWindow( data->display, root_window, pos.y, pos.y,
1448 cx, cy, 0, data->vis.depth, InputOutput,
1449 data->vis.visual, mask, &attr );
1450 if (!data->whole_window) goto done;
1452 set_initial_wm_hints( data->display, data->whole_window );
1453 set_wm_hints( data );
1455 XSaveContext( data->display, data->whole_window, winContext, (char *)data->hwnd );
1456 SetPropA( data->hwnd, whole_window_prop, (HANDLE)data->whole_window );
1458 /* set the window text */
1459 if (!InternalGetWindowText( data->hwnd, text, sizeof(text)/sizeof(WCHAR) )) text[0] = 0;
1460 sync_window_text( data->display, data->whole_window, text );
1462 /* set the window region */
1463 if (win_rgn || IsRectEmpty( &data->window_rect )) sync_window_region( data, win_rgn );
1465 /* set the window opacity */
1466 if (!GetLayeredWindowAttributes( data->hwnd, &key, &alpha, &layered_flags )) layered_flags = 0;
1467 sync_window_opacity( data->display, data->whole_window, key, alpha, layered_flags );
1469 XFlush( data->display ); /* make sure the window exists before we start painting to it */
1471 sync_window_cursor( data->whole_window );
1473 done:
1474 if (win_rgn) DeleteObject( win_rgn );
1478 /**********************************************************************
1479 * destroy_whole_window
1481 * Destroy the whole X window for a given window.
1483 static void destroy_whole_window( struct x11drv_win_data *data, BOOL already_destroyed )
1485 if (!data->whole_window)
1487 if (data->embedded)
1489 Window xwin = (Window)GetPropA( data->hwnd, foreign_window_prop );
1490 if (xwin)
1492 if (!already_destroyed) XSelectInput( data->display, xwin, 0 );
1493 XDeleteContext( data->display, xwin, winContext );
1494 RemovePropA( data->hwnd, foreign_window_prop );
1497 return;
1501 TRACE( "win %p xwin %lx\n", data->hwnd, data->whole_window );
1502 XDeleteContext( data->display, data->whole_window, winContext );
1503 if (data->client_window) XDeleteContext( data->display, data->client_window, winContext );
1504 if (!already_destroyed) XDestroyWindow( data->display, data->whole_window );
1505 if (data->colormap) XFreeColormap( data->display, data->colormap );
1506 data->whole_window = data->client_window = 0;
1507 data->colormap = 0;
1508 data->wm_state = WithdrawnState;
1509 data->net_wm_state = 0;
1510 data->mapped = FALSE;
1511 if (data->xic)
1513 XUnsetICFocus( data->xic );
1514 XDestroyIC( data->xic );
1515 data->xic = 0;
1517 /* Outlook stops processing messages after destroying a dialog, so we need an explicit flush */
1518 XFlush( data->display );
1519 if (data->surface) window_surface_release( data->surface );
1520 data->surface = NULL;
1521 RemovePropA( data->hwnd, whole_window_prop );
1525 /**********************************************************************
1526 * set_window_visual
1528 * Change the visual by destroying and recreating the X window if needed.
1530 void set_window_visual( struct x11drv_win_data *data, const XVisualInfo *vis )
1532 Window client_window = data->client_window;
1533 Window whole_window = data->whole_window;
1535 if (data->vis.visualid == vis->visualid) return;
1536 data->client_window = 0;
1537 destroy_whole_window( data, client_window != 0 /* don't destroy whole_window until reparented */ );
1538 if (data->surface) window_surface_release( data->surface );
1539 data->surface = NULL;
1540 data->vis = *vis;
1541 create_whole_window( data );
1542 if (!client_window) return;
1543 /* move the client to the new parent */
1544 XReparentWindow( data->display, client_window, data->whole_window,
1545 data->client_rect.left - data->whole_rect.left,
1546 data->client_rect.top - data->whole_rect.top );
1547 data->client_window = client_window;
1548 XDestroyWindow( data->display, whole_window );
1552 /*****************************************************************
1553 * SetWindowText (X11DRV.@)
1555 void CDECL X11DRV_SetWindowText( HWND hwnd, LPCWSTR text )
1557 Window win;
1559 if ((win = X11DRV_get_whole_window( hwnd )) && win != DefaultRootWindow(gdi_display))
1561 Display *display = thread_init_display();
1562 sync_window_text( display, win, text );
1567 /***********************************************************************
1568 * SetWindowStyle (X11DRV.@)
1570 * Update the X state of a window to reflect a style change
1572 void CDECL X11DRV_SetWindowStyle( HWND hwnd, INT offset, STYLESTRUCT *style )
1574 struct x11drv_win_data *data;
1575 DWORD changed = style->styleNew ^ style->styleOld;
1577 if (hwnd == GetDesktopWindow()) return;
1578 if (!(data = get_win_data( hwnd ))) return;
1579 if (!data->whole_window) goto done;
1581 if (offset == GWL_STYLE && (changed & WS_DISABLED)) set_wm_hints( data );
1583 if (offset == GWL_EXSTYLE && (changed & WS_EX_LAYERED)) /* changing WS_EX_LAYERED resets attributes */
1585 data->layered = FALSE;
1586 set_window_visual( data, &default_visual );
1587 sync_window_opacity( data->display, data->whole_window, 0, 0, 0 );
1588 if (data->surface) set_surface_color_key( data->surface, CLR_INVALID );
1590 done:
1591 release_win_data( data );
1595 /***********************************************************************
1596 * DestroyWindow (X11DRV.@)
1598 void CDECL X11DRV_DestroyWindow( HWND hwnd )
1600 struct x11drv_thread_data *thread_data = x11drv_thread_data();
1601 struct x11drv_win_data *data;
1603 destroy_gl_drawable( hwnd );
1605 if (!(data = get_win_data( hwnd ))) return;
1607 destroy_whole_window( data, FALSE );
1608 if (thread_data->last_focus == hwnd) thread_data->last_focus = 0;
1609 if (thread_data->last_xic_hwnd == hwnd) thread_data->last_xic_hwnd = 0;
1610 if (data->icon_pixmap) XFreePixmap( gdi_display, data->icon_pixmap );
1611 if (data->icon_mask) XFreePixmap( gdi_display, data->icon_mask );
1612 HeapFree( GetProcessHeap(), 0, data->icon_bits );
1613 XDeleteContext( gdi_display, (XID)hwnd, win_data_context );
1614 release_win_data( data );
1615 HeapFree( GetProcessHeap(), 0, data );
1619 /***********************************************************************
1620 * X11DRV_DestroyNotify
1622 void X11DRV_DestroyNotify( HWND hwnd, XEvent *event )
1624 struct x11drv_win_data *data;
1625 BOOL embedded;
1627 if (!(data = get_win_data( hwnd ))) return;
1628 embedded = data->embedded;
1629 if (!embedded) FIXME( "window %p/%lx destroyed from the outside\n", hwnd, data->whole_window );
1631 destroy_whole_window( data, TRUE );
1632 release_win_data( data );
1633 if (embedded) SendMessageW( hwnd, WM_CLOSE, 0, 0 );
1637 static struct x11drv_win_data *alloc_win_data( Display *display, HWND hwnd )
1639 struct x11drv_win_data *data;
1641 if ((data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*data))))
1643 data->display = display;
1644 data->vis = default_visual;
1645 data->hwnd = hwnd;
1646 EnterCriticalSection( &win_data_section );
1647 XSaveContext( gdi_display, (XID)hwnd, win_data_context, (char *)data );
1649 return data;
1653 /* initialize the desktop window id in the desktop manager process */
1654 BOOL create_desktop_win_data( Window win )
1656 struct x11drv_thread_data *thread_data = x11drv_thread_data();
1657 Display *display = thread_data->display;
1658 struct x11drv_win_data *data;
1660 if (!(data = alloc_win_data( display, GetDesktopWindow() ))) return FALSE;
1661 data->whole_window = win;
1662 data->managed = TRUE;
1663 SetPropA( data->hwnd, whole_window_prop, (HANDLE)win );
1664 set_initial_wm_hints( display, win );
1665 release_win_data( data );
1666 if (thread_data->clip_window) XReparentWindow( display, thread_data->clip_window, win, 0, 0 );
1667 return TRUE;
1670 /**********************************************************************
1671 * CreateDesktopWindow (X11DRV.@)
1673 BOOL CDECL X11DRV_CreateDesktopWindow( HWND hwnd )
1675 unsigned int width, height;
1677 /* retrieve the real size of the desktop */
1678 SERVER_START_REQ( get_window_rectangles )
1680 req->handle = wine_server_user_handle( hwnd );
1681 req->relative = COORDS_CLIENT;
1682 wine_server_call( req );
1683 width = reply->window.right;
1684 height = reply->window.bottom;
1686 SERVER_END_REQ;
1688 if (!width && !height) /* not initialized yet */
1690 RECT rect = get_virtual_screen_rect();
1692 SERVER_START_REQ( set_window_pos )
1694 req->handle = wine_server_user_handle( hwnd );
1695 req->previous = 0;
1696 req->swp_flags = SWP_NOZORDER;
1697 req->window.left = rect.left;
1698 req->window.top = rect.top;
1699 req->window.right = rect.right;
1700 req->window.bottom = rect.bottom;
1701 req->client = req->window;
1702 wine_server_call( req );
1704 SERVER_END_REQ;
1706 else
1708 Window win = (Window)GetPropA( hwnd, whole_window_prop );
1709 if (win && win != root_window) X11DRV_init_desktop( win, width, height );
1711 return TRUE;
1715 /**********************************************************************
1716 * CreateWindow (X11DRV.@)
1718 BOOL CDECL X11DRV_CreateWindow( HWND hwnd )
1720 if (hwnd == GetDesktopWindow())
1722 struct x11drv_thread_data *data = x11drv_init_thread_data();
1723 XSetWindowAttributes attr;
1725 /* create the cursor clipping window */
1726 attr.override_redirect = TRUE;
1727 attr.event_mask = StructureNotifyMask | FocusChangeMask;
1728 data->clip_window = XCreateWindow( data->display, root_window, 0, 0, 1, 1, 0, 0,
1729 InputOnly, default_visual.visual,
1730 CWOverrideRedirect | CWEventMask, &attr );
1731 XFlush( data->display );
1732 SetPropA( hwnd, clip_window_prop, (HANDLE)data->clip_window );
1734 return TRUE;
1738 /***********************************************************************
1739 * get_win_data
1741 * Lock and return the X11 data structure associated with a window.
1743 struct x11drv_win_data *get_win_data( HWND hwnd )
1745 char *data;
1747 if (!hwnd) return NULL;
1748 EnterCriticalSection( &win_data_section );
1749 if (!XFindContext( gdi_display, (XID)hwnd, win_data_context, &data ))
1750 return (struct x11drv_win_data *)data;
1751 LeaveCriticalSection( &win_data_section );
1752 return NULL;
1756 /***********************************************************************
1757 * release_win_data
1759 * Release the data returned by get_win_data.
1761 void release_win_data( struct x11drv_win_data *data )
1763 if (data) LeaveCriticalSection( &win_data_section );
1767 /***********************************************************************
1768 * X11DRV_create_win_data
1770 * Create an X11 data window structure for an existing window.
1772 static struct x11drv_win_data *X11DRV_create_win_data( HWND hwnd, const RECT *window_rect,
1773 const RECT *client_rect )
1775 Display *display;
1776 struct x11drv_win_data *data;
1777 HWND parent;
1779 if (!(parent = GetAncestor( hwnd, GA_PARENT ))) return NULL; /* desktop */
1781 /* don't create win data for HWND_MESSAGE windows */
1782 if (parent != GetDesktopWindow() && !GetAncestor( parent, GA_PARENT )) return NULL;
1784 if (GetWindowThreadProcessId( hwnd, NULL ) != GetCurrentThreadId()) return NULL;
1786 display = thread_init_display();
1787 init_clip_window(); /* make sure the clip window is initialized in this thread */
1789 if (!(data = alloc_win_data( display, hwnd ))) return NULL;
1791 data->whole_rect = data->window_rect = *window_rect;
1792 data->client_rect = *client_rect;
1793 if (parent == GetDesktopWindow())
1795 create_whole_window( data );
1796 TRACE( "win %p/%lx window %s whole %s client %s\n",
1797 hwnd, data->whole_window, wine_dbgstr_rect( &data->window_rect ),
1798 wine_dbgstr_rect( &data->whole_rect ), wine_dbgstr_rect( &data->client_rect ));
1800 return data;
1804 /* window procedure for foreign windows */
1805 static LRESULT WINAPI foreign_window_proc( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
1807 switch(msg)
1809 case WM_WINDOWPOSCHANGED:
1810 update_systray_balloon_position();
1811 break;
1812 case WM_PARENTNOTIFY:
1813 if (LOWORD(wparam) == WM_DESTROY)
1815 TRACE( "%p: got parent notify destroy for win %lx\n", hwnd, lparam );
1816 PostMessageW( hwnd, WM_CLOSE, 0, 0 ); /* so that we come back here once the child is gone */
1818 return 0;
1819 case WM_CLOSE:
1820 if (GetWindow( hwnd, GW_CHILD )) return 0; /* refuse to die if we still have children */
1821 break;
1823 return DefWindowProcW( hwnd, msg, wparam, lparam );
1827 /***********************************************************************
1828 * create_foreign_window
1830 * Create a foreign window for the specified X window and its ancestors
1832 HWND create_foreign_window( Display *display, Window xwin )
1834 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};
1835 static BOOL class_registered;
1836 struct x11drv_win_data *data;
1837 HWND hwnd, parent;
1838 POINT pos;
1839 Window xparent, xroot;
1840 Window *xchildren;
1841 unsigned int nchildren;
1842 XWindowAttributes attr;
1843 DWORD style = WS_CLIPCHILDREN;
1845 if (!class_registered)
1847 WNDCLASSEXW class;
1849 memset( &class, 0, sizeof(class) );
1850 class.cbSize = sizeof(class);
1851 class.lpfnWndProc = foreign_window_proc;
1852 class.lpszClassName = classW;
1853 if (!RegisterClassExW( &class ) && GetLastError() != ERROR_CLASS_ALREADY_EXISTS)
1855 ERR( "Could not register foreign window class\n" );
1856 return FALSE;
1858 class_registered = TRUE;
1861 if (XFindContext( display, xwin, winContext, (char **)&hwnd )) hwnd = 0;
1862 if (hwnd) return hwnd; /* already created */
1864 XSelectInput( display, xwin, StructureNotifyMask );
1865 if (!XGetWindowAttributes( display, xwin, &attr ) ||
1866 !XQueryTree( display, xwin, &xroot, &xparent, &xchildren, &nchildren ))
1868 XSelectInput( display, xwin, 0 );
1869 return 0;
1871 XFree( xchildren );
1873 if (xparent == xroot)
1875 parent = GetDesktopWindow();
1876 style |= WS_POPUP;
1877 pos = root_to_virtual_screen( attr.x, attr.y );
1879 else
1881 parent = create_foreign_window( display, xparent );
1882 style |= WS_CHILD;
1883 pos.x = attr.x;
1884 pos.y = attr.y;
1887 hwnd = CreateWindowW( classW, NULL, style, pos.x, pos.y, attr.width, attr.height,
1888 parent, 0, 0, NULL );
1890 if (!(data = alloc_win_data( display, hwnd )))
1892 DestroyWindow( hwnd );
1893 return 0;
1895 SetRect( &data->window_rect, pos.x, pos.y, pos.x + attr.width, pos.y + attr.height );
1896 data->whole_rect = data->client_rect = data->window_rect;
1897 data->whole_window = data->client_window = 0;
1898 data->embedded = TRUE;
1899 data->mapped = TRUE;
1901 SetPropA( hwnd, foreign_window_prop, (HANDLE)xwin );
1902 XSaveContext( display, xwin, winContext, (char *)data->hwnd );
1904 TRACE( "win %lx parent %p style %08x %s -> hwnd %p\n",
1905 xwin, parent, style, wine_dbgstr_rect(&data->window_rect), hwnd );
1907 release_win_data( data );
1909 ShowWindow( hwnd, SW_SHOW );
1910 return hwnd;
1914 /***********************************************************************
1915 * X11DRV_get_whole_window
1917 * Return the X window associated with the full area of a window
1919 Window X11DRV_get_whole_window( HWND hwnd )
1921 struct x11drv_win_data *data = get_win_data( hwnd );
1922 Window ret;
1924 if (!data)
1926 if (hwnd == GetDesktopWindow()) return root_window;
1927 return (Window)GetPropA( hwnd, whole_window_prop );
1929 ret = data->whole_window;
1930 release_win_data( data );
1931 return ret;
1935 /***********************************************************************
1936 * X11DRV_get_ic
1938 * Return the X input context associated with a window
1940 XIC X11DRV_get_ic( HWND hwnd )
1942 struct x11drv_win_data *data = get_win_data( hwnd );
1943 XIM xim;
1944 XIC ret = 0;
1946 if (data)
1948 x11drv_thread_data()->last_xic_hwnd = hwnd;
1949 ret = data->xic;
1950 if (!ret && (xim = x11drv_thread_data()->xim)) ret = X11DRV_CreateIC( xim, data );
1951 release_win_data( data );
1953 return ret;
1957 /***********************************************************************
1958 * X11DRV_GetDC (X11DRV.@)
1960 void CDECL X11DRV_GetDC( HDC hdc, HWND hwnd, HWND top, const RECT *win_rect,
1961 const RECT *top_rect, DWORD flags )
1963 struct x11drv_escape_set_drawable escape;
1964 HWND parent;
1966 escape.code = X11DRV_SET_DRAWABLE;
1967 escape.hwnd = hwnd;
1968 escape.mode = IncludeInferiors;
1969 escape.fbconfig_id = 0;
1971 escape.dc_rect.left = win_rect->left - top_rect->left;
1972 escape.dc_rect.top = win_rect->top - top_rect->top;
1973 escape.dc_rect.right = win_rect->right - top_rect->left;
1974 escape.dc_rect.bottom = win_rect->bottom - top_rect->top;
1976 if (top == hwnd)
1978 struct x11drv_win_data *data = get_win_data( hwnd );
1980 escape.drawable = data ? data->whole_window : X11DRV_get_whole_window( hwnd );
1982 /* special case: when repainting the root window, clip out top-level windows */
1983 if (data && data->whole_window == root_window) escape.mode = ClipByChildren;
1984 release_win_data( data );
1986 else
1988 /* find the first ancestor that has a drawable */
1989 for (parent = hwnd; parent && parent != top; parent = GetAncestor( parent, GA_PARENT ))
1990 if ((escape.drawable = X11DRV_get_whole_window( parent ))) break;
1992 if (escape.drawable)
1994 POINT pt = { 0, 0 };
1995 MapWindowPoints( 0, parent, &pt, 1 );
1996 escape.dc_rect = *win_rect;
1997 OffsetRect( &escape.dc_rect, pt.x, pt.y );
1998 if (flags & DCX_CLIPCHILDREN) escape.mode = ClipByChildren;
2000 else escape.drawable = X11DRV_get_whole_window( top );
2003 ExtEscape( hdc, X11DRV_ESCAPE, sizeof(escape), (LPSTR)&escape, 0, NULL );
2007 /***********************************************************************
2008 * X11DRV_ReleaseDC (X11DRV.@)
2010 void CDECL X11DRV_ReleaseDC( HWND hwnd, HDC hdc )
2012 struct x11drv_escape_set_drawable escape;
2014 escape.code = X11DRV_SET_DRAWABLE;
2015 escape.hwnd = GetDesktopWindow();
2016 escape.drawable = root_window;
2017 escape.mode = IncludeInferiors;
2018 escape.dc_rect = get_virtual_screen_rect();
2019 OffsetRect( &escape.dc_rect, -2 * escape.dc_rect.left, -2 * escape.dc_rect.top );
2020 escape.fbconfig_id = 0;
2021 ExtEscape( hdc, X11DRV_ESCAPE, sizeof(escape), (LPSTR)&escape, 0, NULL );
2025 /*************************************************************************
2026 * ScrollDC (X11DRV.@)
2028 BOOL CDECL X11DRV_ScrollDC( HDC hdc, INT dx, INT dy, HRGN update )
2030 RECT rect;
2031 BOOL ret;
2032 HRGN expose_rgn = 0;
2034 GetClipBox( hdc, &rect );
2036 if (update)
2038 INT code = X11DRV_START_EXPOSURES;
2039 ExtEscape( hdc, X11DRV_ESCAPE, sizeof(code), (LPSTR)&code, 0, NULL );
2041 ret = BitBlt( hdc, rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top,
2042 hdc, rect.left - dx, rect.top - dy, SRCCOPY );
2044 code = X11DRV_END_EXPOSURES;
2045 ExtEscape( hdc, X11DRV_ESCAPE, sizeof(code), (LPSTR)&code,
2046 sizeof(expose_rgn), (LPSTR)&expose_rgn );
2047 if (expose_rgn)
2049 CombineRgn( update, update, expose_rgn, RGN_OR );
2050 DeleteObject( expose_rgn );
2053 else ret = BitBlt( hdc, rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top,
2054 hdc, rect.left - dx, rect.top - dy, SRCCOPY );
2056 return ret;
2060 /***********************************************************************
2061 * SetCapture (X11DRV.@)
2063 void CDECL X11DRV_SetCapture( HWND hwnd, UINT flags )
2065 struct x11drv_thread_data *thread_data = x11drv_thread_data();
2067 if (!thread_data) return;
2068 if (!(flags & (GUI_INMOVESIZE | GUI_INMENUMODE))) return;
2070 if (hwnd)
2072 Window grab_win = X11DRV_get_whole_window( GetAncestor( hwnd, GA_ROOT ) );
2074 if (!grab_win) return;
2075 XFlush( gdi_display );
2076 XGrabPointer( thread_data->display, grab_win, False,
2077 PointerMotionMask | ButtonPressMask | ButtonReleaseMask,
2078 GrabModeAsync, GrabModeAsync, None, None, CurrentTime );
2079 thread_data->grab_window = grab_win;
2081 else /* release capture */
2083 XFlush( gdi_display );
2084 XUngrabPointer( thread_data->display, CurrentTime );
2085 XFlush( thread_data->display );
2086 thread_data->grab_window = None;
2091 /*****************************************************************
2092 * SetParent (X11DRV.@)
2094 void CDECL X11DRV_SetParent( HWND hwnd, HWND parent, HWND old_parent )
2096 struct x11drv_win_data *data;
2098 if (parent == old_parent) return;
2099 if (!(data = get_win_data( hwnd ))) return;
2100 if (data->embedded) goto done;
2102 if (parent != GetDesktopWindow()) /* a child window */
2104 if (old_parent == GetDesktopWindow())
2106 /* destroy the old X windows */
2107 destroy_whole_window( data, FALSE );
2108 data->managed = FALSE;
2111 else /* new top level window */
2113 create_whole_window( data );
2115 done:
2116 release_win_data( data );
2117 set_gl_drawable_parent( hwnd, parent );
2118 fetch_icon_data( hwnd, 0, 0 );
2122 static inline RECT get_surface_rect( const RECT *visible_rect )
2124 RECT rect = get_virtual_screen_rect();
2126 IntersectRect( &rect, &rect, visible_rect );
2127 OffsetRect( &rect, -visible_rect->left, -visible_rect->top );
2128 rect.left &= ~31;
2129 rect.top &= ~31;
2130 rect.right = max( rect.left + 32, (rect.right + 31) & ~31 );
2131 rect.bottom = max( rect.top + 32, (rect.bottom + 31) & ~31 );
2132 return rect;
2136 /***********************************************************************
2137 * WindowPosChanging (X11DRV.@)
2139 void CDECL X11DRV_WindowPosChanging( HWND hwnd, HWND insert_after, UINT swp_flags,
2140 const RECT *window_rect, const RECT *client_rect, RECT *visible_rect,
2141 struct window_surface **surface )
2143 struct x11drv_win_data *data = get_win_data( hwnd );
2144 RECT surface_rect;
2145 DWORD flags;
2146 COLORREF key;
2147 BOOL layered = GetWindowLongW( hwnd, GWL_EXSTYLE ) & WS_EX_LAYERED;
2149 if (!data && !(data = X11DRV_create_win_data( hwnd, window_rect, client_rect ))) return;
2151 /* check if we need to switch the window to managed */
2152 if (!data->managed && data->whole_window && is_window_managed( hwnd, swp_flags, window_rect ))
2154 TRACE( "making win %p/%lx managed\n", hwnd, data->whole_window );
2155 release_win_data( data );
2156 unmap_window( hwnd );
2157 if (!(data = get_win_data( hwnd ))) return;
2158 data->managed = TRUE;
2161 *visible_rect = *window_rect;
2162 X11DRV_window_to_X_rect( data, visible_rect );
2164 /* create the window surface if necessary */
2166 if (!data->whole_window && !data->embedded) goto done;
2167 if (swp_flags & SWP_HIDEWINDOW) goto done;
2168 if (data->vis.visualid != default_visual.visualid) goto done;
2170 if (*surface) window_surface_release( *surface );
2171 *surface = NULL; /* indicate that we want to draw directly to the window */
2173 if (data->embedded) goto done;
2174 if (data->whole_window == root_window) goto done;
2175 if (data->client_window) goto done;
2176 if (!client_side_graphics && !layered) goto done;
2178 surface_rect = get_surface_rect( visible_rect );
2179 if (data->surface)
2181 if (!memcmp( &data->surface->rect, &surface_rect, sizeof(surface_rect) ))
2183 /* existing surface is good enough */
2184 window_surface_add_ref( data->surface );
2185 *surface = data->surface;
2186 goto done;
2189 else if (!(swp_flags & SWP_SHOWWINDOW) && !(GetWindowLongW( hwnd, GWL_STYLE ) & WS_VISIBLE)) goto done;
2191 if (!layered || !GetLayeredWindowAttributes( hwnd, &key, NULL, &flags ) || !(flags & LWA_COLORKEY))
2192 key = CLR_INVALID;
2194 *surface = create_surface( data->whole_window, &data->vis, &surface_rect, key, FALSE );
2196 done:
2197 release_win_data( data );
2201 /***********************************************************************
2202 * WindowPosChanged (X11DRV.@)
2204 void CDECL X11DRV_WindowPosChanged( HWND hwnd, HWND insert_after, UINT swp_flags,
2205 const RECT *rectWindow, const RECT *rectClient,
2206 const RECT *visible_rect, const RECT *valid_rects,
2207 struct window_surface *surface )
2209 struct x11drv_thread_data *thread_data;
2210 struct x11drv_win_data *data;
2211 DWORD new_style = GetWindowLongW( hwnd, GWL_STYLE );
2212 RECT old_window_rect, old_whole_rect, old_client_rect;
2213 int event_type;
2215 if (!(data = get_win_data( hwnd ))) return;
2217 thread_data = x11drv_thread_data();
2219 old_window_rect = data->window_rect;
2220 old_whole_rect = data->whole_rect;
2221 old_client_rect = data->client_rect;
2222 data->window_rect = *rectWindow;
2223 data->whole_rect = *visible_rect;
2224 data->client_rect = *rectClient;
2225 if (data->vis.visualid == default_visual.visualid)
2227 if (surface) window_surface_add_ref( surface );
2228 if (data->surface) window_surface_release( data->surface );
2229 data->surface = surface;
2232 TRACE( "win %p window %s client %s style %08x flags %08x\n",
2233 hwnd, wine_dbgstr_rect(rectWindow), wine_dbgstr_rect(rectClient), new_style, swp_flags );
2235 if (!IsRectEmpty( &valid_rects[0] ))
2237 Window window = data->whole_window;
2238 int x_offset = old_whole_rect.left - data->whole_rect.left;
2239 int y_offset = old_whole_rect.top - data->whole_rect.top;
2241 /* if all that happened is that the whole window moved, copy everything */
2242 if (!(swp_flags & SWP_FRAMECHANGED) &&
2243 old_whole_rect.right - data->whole_rect.right == x_offset &&
2244 old_whole_rect.bottom - data->whole_rect.bottom == y_offset &&
2245 old_client_rect.left - data->client_rect.left == x_offset &&
2246 old_client_rect.right - data->client_rect.right == x_offset &&
2247 old_client_rect.top - data->client_rect.top == y_offset &&
2248 old_client_rect.bottom - data->client_rect.bottom == y_offset &&
2249 !memcmp( &valid_rects[0], &data->client_rect, sizeof(RECT) ))
2251 /* if we have an X window the bits will be moved by the X server */
2252 if (!window && (x_offset != 0 || y_offset != 0))
2254 release_win_data( data );
2255 move_window_bits( hwnd, window, &old_whole_rect, visible_rect,
2256 &old_client_rect, rectClient, rectWindow );
2257 if (!(data = get_win_data( hwnd ))) return;
2260 else
2262 release_win_data( data );
2263 move_window_bits( hwnd, window, &valid_rects[1], &valid_rects[0],
2264 &old_client_rect, rectClient, rectWindow );
2265 if (!(data = get_win_data( hwnd ))) return;
2269 XFlush( gdi_display ); /* make sure painting is done before we move the window */
2271 sync_client_position( data, &old_client_rect, &old_whole_rect );
2273 if (!data->whole_window)
2275 release_win_data( data );
2276 sync_gl_drawable( hwnd, visible_rect, rectClient );
2277 return;
2280 /* check if we are currently processing an event relevant to this window */
2281 event_type = 0;
2282 if (thread_data &&
2283 thread_data->current_event &&
2284 thread_data->current_event->xany.window == data->whole_window)
2286 event_type = thread_data->current_event->type;
2287 if (event_type != ConfigureNotify && event_type != PropertyNotify &&
2288 event_type != GravityNotify && event_type != ReparentNotify)
2289 event_type = 0; /* ignore other events */
2292 if (data->mapped && event_type != ReparentNotify)
2294 if (((swp_flags & SWP_HIDEWINDOW) && !(new_style & WS_VISIBLE)) ||
2295 (!event_type && !(new_style & WS_MINIMIZE) &&
2296 !is_window_rect_mapped( rectWindow ) && is_window_rect_mapped( &old_window_rect )))
2298 release_win_data( data );
2299 unmap_window( hwnd );
2300 if (is_window_rect_fullscreen( &old_window_rect )) reset_clipping_window();
2301 if (!(data = get_win_data( hwnd ))) return;
2305 /* don't change position if we are about to minimize or maximize a managed window */
2306 if (!event_type &&
2307 !(data->managed && (swp_flags & SWP_STATECHANGED) && (new_style & (WS_MINIMIZE|WS_MAXIMIZE))))
2308 sync_window_position( data, swp_flags, &old_window_rect, &old_whole_rect, &old_client_rect );
2310 if ((new_style & WS_VISIBLE) &&
2311 ((new_style & WS_MINIMIZE) || is_window_rect_mapped( rectWindow )))
2313 if (!data->mapped)
2315 BOOL needs_icon = !data->icon_pixmap;
2316 BOOL needs_map = TRUE;
2318 /* layered windows are mapped only once their attributes are set */
2319 if (GetWindowLongW( hwnd, GWL_EXSTYLE ) & WS_EX_LAYERED) needs_map = data->layered;
2320 release_win_data( data );
2321 if (needs_icon) fetch_icon_data( hwnd, 0, 0 );
2322 if (needs_map) map_window( hwnd, new_style );
2323 return;
2325 else if ((swp_flags & SWP_STATECHANGED) && (!data->iconic != !(new_style & WS_MINIMIZE)))
2327 set_wm_hints( data );
2328 data->iconic = (new_style & WS_MINIMIZE) != 0;
2329 TRACE( "changing win %p iconic state to %u\n", data->hwnd, data->iconic );
2330 if (data->iconic)
2331 XIconifyWindow( data->display, data->whole_window, data->vis.screen );
2332 else if (is_window_rect_mapped( rectWindow ))
2333 XMapWindow( data->display, data->whole_window );
2334 update_net_wm_states( data );
2336 else
2338 if (swp_flags & (SWP_FRAMECHANGED|SWP_STATECHANGED)) set_wm_hints( data );
2339 if (!event_type) update_net_wm_states( data );
2343 XFlush( data->display ); /* make sure changes are done before we start painting again */
2344 if (data->surface && data->vis.visualid != default_visual.visualid)
2345 data->surface->funcs->flush( data->surface );
2347 release_win_data( data );
2351 /***********************************************************************
2352 * ShowWindow (X11DRV.@)
2354 UINT CDECL X11DRV_ShowWindow( HWND hwnd, INT cmd, RECT *rect, UINT swp )
2356 int x, y;
2357 unsigned int width, height, border, depth;
2358 Window root, top;
2359 POINT pos;
2360 DWORD style = GetWindowLongW( hwnd, GWL_STYLE );
2361 struct x11drv_thread_data *thread_data = x11drv_thread_data();
2362 struct x11drv_win_data *data = get_win_data( hwnd );
2364 if (!data || !data->whole_window || !data->managed) goto done;
2365 if (IsRectEmpty( rect )) goto done;
2366 if (style & WS_MINIMIZE)
2368 if (rect->left != -32000 || rect->top != -32000)
2370 OffsetRect( rect, -32000 - rect->left, -32000 - rect->top );
2371 swp &= ~(SWP_NOMOVE | SWP_NOCLIENTMOVE);
2373 goto done;
2375 if (!data->mapped || data->iconic) goto done;
2377 /* only fetch the new rectangle if the ShowWindow was a result of a window manager event */
2379 if (!thread_data->current_event || thread_data->current_event->xany.window != data->whole_window)
2380 goto done;
2382 if (thread_data->current_event->type != ConfigureNotify &&
2383 thread_data->current_event->type != PropertyNotify)
2384 goto done;
2386 TRACE( "win %p/%lx cmd %d at %s flags %08x\n",
2387 hwnd, data->whole_window, cmd, wine_dbgstr_rect(rect), swp );
2389 XGetGeometry( thread_data->display, data->whole_window,
2390 &root, &x, &y, &width, &height, &border, &depth );
2391 XTranslateCoordinates( thread_data->display, data->whole_window, root, 0, 0, &x, &y, &top );
2392 pos = root_to_virtual_screen( x, y );
2393 rect->left = pos.x;
2394 rect->top = pos.y;
2395 rect->right = pos.x + width;
2396 rect->bottom = pos.y + height;
2397 X11DRV_X_to_window_rect( data, rect );
2398 swp &= ~(SWP_NOMOVE | SWP_NOCLIENTMOVE | SWP_NOSIZE | SWP_NOCLIENTSIZE);
2400 done:
2401 release_win_data( data );
2402 return swp;
2406 /**********************************************************************
2407 * SetWindowIcon (X11DRV.@)
2409 * hIcon or hIconSm has changed (or is being initialised for the
2410 * first time). Complete the X11 driver-specific initialisation
2411 * and set the window hints.
2413 void CDECL X11DRV_SetWindowIcon( HWND hwnd, UINT type, HICON icon )
2415 struct x11drv_win_data *data;
2417 if (!(data = get_win_data( hwnd ))) return;
2418 if (!data->whole_window) goto done;
2419 release_win_data( data ); /* release the lock, fetching the icon requires sending messages */
2421 if (type == ICON_BIG) fetch_icon_data( hwnd, icon, 0 );
2422 else fetch_icon_data( hwnd, 0, icon );
2424 if (!(data = get_win_data( hwnd ))) return;
2425 set_wm_hints( data );
2426 done:
2427 release_win_data( data );
2431 /***********************************************************************
2432 * SetWindowRgn (X11DRV.@)
2434 * Assign specified region to window (for non-rectangular windows)
2436 int CDECL X11DRV_SetWindowRgn( HWND hwnd, HRGN hrgn, BOOL redraw )
2438 struct x11drv_win_data *data;
2440 if ((data = get_win_data( hwnd )))
2442 sync_window_region( data, hrgn );
2443 release_win_data( data );
2445 else if (X11DRV_get_whole_window( hwnd ))
2447 SendMessageW( hwnd, WM_X11DRV_SET_WIN_REGION, 0, 0 );
2449 return TRUE;
2453 /***********************************************************************
2454 * SetLayeredWindowAttributes (X11DRV.@)
2456 * Set transparency attributes for a layered window.
2458 void CDECL X11DRV_SetLayeredWindowAttributes( HWND hwnd, COLORREF key, BYTE alpha, DWORD flags )
2460 struct x11drv_win_data *data = get_win_data( hwnd );
2462 if (data)
2464 if (data->whole_window)
2465 sync_window_opacity( data->display, data->whole_window, key, alpha, flags );
2466 if (data->surface)
2467 set_surface_color_key( data->surface, (flags & LWA_COLORKEY) ? key : CLR_INVALID );
2469 data->layered = TRUE;
2470 if (!data->mapped) /* mapping is delayed until attributes are set */
2472 DWORD style = GetWindowLongW( data->hwnd, GWL_STYLE );
2474 if ((style & WS_VISIBLE) &&
2475 ((style & WS_MINIMIZE) || is_window_rect_mapped( &data->window_rect )))
2477 release_win_data( data );
2478 map_window( hwnd, style );
2479 return;
2482 release_win_data( data );
2484 else
2486 Window win = X11DRV_get_whole_window( hwnd );
2487 if (win)
2489 sync_window_opacity( gdi_display, win, key, alpha, flags );
2490 if (flags & LWA_COLORKEY)
2491 FIXME( "LWA_COLORKEY not supported on foreign process window %p\n", hwnd );
2497 /*****************************************************************************
2498 * UpdateLayeredWindow (X11DRV.@)
2500 BOOL CDECL X11DRV_UpdateLayeredWindow( HWND hwnd, const UPDATELAYEREDWINDOWINFO *info,
2501 const RECT *window_rect )
2503 struct window_surface *surface;
2504 struct x11drv_win_data *data;
2505 BLENDFUNCTION blend = { AC_SRC_OVER, 0, 255, 0 };
2506 COLORREF color_key = (info->dwFlags & ULW_COLORKEY) ? info->crKey : CLR_INVALID;
2507 char buffer[FIELD_OFFSET( BITMAPINFO, bmiColors[256] )];
2508 BITMAPINFO *bmi = (BITMAPINFO *)buffer;
2509 void *src_bits, *dst_bits;
2510 RECT rect;
2511 HDC hdc = 0;
2512 HBITMAP dib;
2513 BOOL ret = FALSE;
2515 if (!(data = get_win_data( hwnd ))) return FALSE;
2517 data->layered = TRUE;
2518 if (!data->embedded && argb_visual.visualid) set_window_visual( data, &argb_visual );
2520 rect = *window_rect;
2521 OffsetRect( &rect, -window_rect->left, -window_rect->top );
2523 surface = data->surface;
2524 if (!surface || memcmp( &surface->rect, &rect, sizeof(RECT) ))
2526 data->surface = create_surface( data->whole_window, &data->vis, &rect,
2527 color_key, !data->embedded );
2528 if (surface) window_surface_release( surface );
2529 surface = data->surface;
2531 else set_surface_color_key( surface, color_key );
2533 if (surface) window_surface_add_ref( surface );
2534 release_win_data( data );
2536 if (!surface) return FALSE;
2537 if (!info->hdcSrc)
2539 window_surface_release( surface );
2540 return TRUE;
2543 dst_bits = surface->funcs->get_info( surface, bmi );
2545 if (!(dib = CreateDIBSection( info->hdcDst, bmi, DIB_RGB_COLORS, &src_bits, NULL, 0 ))) goto done;
2546 if (!(hdc = CreateCompatibleDC( 0 ))) goto done;
2548 SelectObject( hdc, dib );
2550 surface->funcs->lock( surface );
2552 if (info->prcDirty)
2554 IntersectRect( &rect, &rect, info->prcDirty );
2555 memcpy( src_bits, dst_bits, bmi->bmiHeader.biSizeImage );
2556 PatBlt( hdc, rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top, BLACKNESS );
2558 ret = GdiAlphaBlend( hdc, rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top,
2559 info->hdcSrc,
2560 rect.left + (info->pptSrc ? info->pptSrc->x : 0),
2561 rect.top + (info->pptSrc ? info->pptSrc->y : 0),
2562 rect.right - rect.left, rect.bottom - rect.top,
2563 (info->dwFlags & ULW_ALPHA) ? *info->pblend : blend );
2564 if (ret)
2566 memcpy( dst_bits, src_bits, bmi->bmiHeader.biSizeImage );
2567 add_bounds_rect( surface->funcs->get_bounds( surface ), &rect );
2570 surface->funcs->unlock( surface );
2571 surface->funcs->flush( surface );
2573 done:
2574 window_surface_release( surface );
2575 if (hdc) DeleteDC( hdc );
2576 if (dib) DeleteObject( dib );
2577 return ret;
2581 /**********************************************************************
2582 * X11DRV_WindowMessage (X11DRV.@)
2584 LRESULT CDECL X11DRV_WindowMessage( HWND hwnd, UINT msg, WPARAM wp, LPARAM lp )
2586 struct x11drv_win_data *data;
2588 switch(msg)
2590 case WM_X11DRV_ACQUIRE_SELECTION:
2591 return X11DRV_AcquireClipboard( hwnd );
2592 case WM_X11DRV_SET_WIN_REGION:
2593 if ((data = get_win_data( hwnd )))
2595 sync_window_region( data, (HRGN)1 );
2596 release_win_data( data );
2598 return 0;
2599 case WM_X11DRV_RESIZE_DESKTOP:
2600 X11DRV_resize_desktop( LOWORD(lp), HIWORD(lp) );
2601 return 0;
2602 case WM_X11DRV_SET_CURSOR:
2603 if ((data = get_win_data( hwnd )))
2605 if (data->whole_window) set_window_cursor( data->whole_window, (HCURSOR)lp );
2606 release_win_data( data );
2608 else if (hwnd == x11drv_thread_data()->clip_hwnd)
2609 set_window_cursor( x11drv_thread_data()->clip_window, (HCURSOR)lp );
2610 return 0;
2611 case WM_X11DRV_CLIP_CURSOR:
2612 return clip_cursor_notify( hwnd, (HWND)lp );
2613 default:
2614 FIXME( "got window msg %x hwnd %p wp %lx lp %lx\n", msg, hwnd, wp, lp );
2615 return 0;
2620 /***********************************************************************
2621 * is_netwm_supported
2623 static BOOL is_netwm_supported( Display *display, Atom atom )
2625 static Atom *net_supported;
2626 static int net_supported_count = -1;
2627 int i;
2629 if (net_supported_count == -1)
2631 Atom type;
2632 int format;
2633 unsigned long count, remaining;
2635 if (!XGetWindowProperty( display, DefaultRootWindow(display), x11drv_atom(_NET_SUPPORTED), 0,
2636 ~0UL, False, XA_ATOM, &type, &format, &count,
2637 &remaining, (unsigned char **)&net_supported ))
2638 net_supported_count = get_property_size( format, count ) / sizeof(Atom);
2639 else
2640 net_supported_count = 0;
2643 for (i = 0; i < net_supported_count; i++)
2644 if (net_supported[i] == atom) return TRUE;
2645 return FALSE;
2649 /***********************************************************************
2650 * SysCommand (X11DRV.@)
2652 * Perform WM_SYSCOMMAND handling.
2654 LRESULT CDECL X11DRV_SysCommand( HWND hwnd, WPARAM wparam, LPARAM lparam )
2656 WPARAM hittest = wparam & 0x0f;
2657 int dir;
2658 struct x11drv_win_data *data;
2660 if (!(data = get_win_data( hwnd ))) return -1;
2661 if (!data->whole_window || !data->managed || !data->mapped) goto failed;
2663 switch (wparam & 0xfff0)
2665 case SC_MOVE:
2666 if (!hittest) dir = _NET_WM_MOVERESIZE_MOVE_KEYBOARD;
2667 else dir = _NET_WM_MOVERESIZE_MOVE;
2668 break;
2669 case SC_SIZE:
2670 /* windows without WS_THICKFRAME are not resizable through the window manager */
2671 if (!(GetWindowLongW( hwnd, GWL_STYLE ) & WS_THICKFRAME)) goto failed;
2673 switch (hittest)
2675 case WMSZ_LEFT: dir = _NET_WM_MOVERESIZE_SIZE_LEFT; break;
2676 case WMSZ_RIGHT: dir = _NET_WM_MOVERESIZE_SIZE_RIGHT; break;
2677 case WMSZ_TOP: dir = _NET_WM_MOVERESIZE_SIZE_TOP; break;
2678 case WMSZ_TOPLEFT: dir = _NET_WM_MOVERESIZE_SIZE_TOPLEFT; break;
2679 case WMSZ_TOPRIGHT: dir = _NET_WM_MOVERESIZE_SIZE_TOPRIGHT; break;
2680 case WMSZ_BOTTOM: dir = _NET_WM_MOVERESIZE_SIZE_BOTTOM; break;
2681 case WMSZ_BOTTOMLEFT: dir = _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT; break;
2682 case WMSZ_BOTTOMRIGHT: dir = _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT; break;
2683 default: dir = _NET_WM_MOVERESIZE_SIZE_KEYBOARD; break;
2685 break;
2687 case SC_KEYMENU:
2688 /* prevent a simple ALT press+release from activating the system menu,
2689 * as that can get confusing on managed windows */
2690 if ((WCHAR)lparam) goto failed; /* got an explicit char */
2691 if (GetMenu( hwnd )) goto failed; /* window has a real menu */
2692 if (!(GetWindowLongW( hwnd, GWL_STYLE ) & WS_SYSMENU)) goto failed; /* no system menu */
2693 TRACE( "ignoring SC_KEYMENU wp %lx lp %lx\n", wparam, lparam );
2694 release_win_data( data );
2695 return 0;
2697 default:
2698 goto failed;
2701 if (IsZoomed(hwnd)) goto failed;
2703 if (!is_netwm_supported( data->display, x11drv_atom(_NET_WM_MOVERESIZE) ))
2705 TRACE( "_NET_WM_MOVERESIZE not supported\n" );
2706 goto failed;
2709 release_win_data( data );
2710 move_resize_window( hwnd, dir );
2711 return 0;
2713 failed:
2714 release_win_data( data );
2715 return -1;