wpp: Free the include filename on error (Valgrind).
[wine.git] / dlls / winex11.drv / window.c
blob77cf304c6d1d940348a904d656b765fbf00589fe
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 BOOL clipping_cursor = FALSE;
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->backing_store = NotUseful;
318 attr->border_pixel = 0;
319 attr->event_mask = (ExposureMask | PointerMotionMask |
320 ButtonPressMask | ButtonReleaseMask | EnterWindowMask |
321 KeyPressMask | KeyReleaseMask | FocusChangeMask |
322 KeymapStateMask | StructureNotifyMask);
323 if (data->managed) attr->event_mask |= PropertyChangeMask;
325 return (CWOverrideRedirect | CWSaveUnder | CWColormap | CWBorderPixel |
326 CWEventMask | CWBitGravity | CWBackingStore);
330 /***********************************************************************
331 * sync_window_style
333 * Change the X window attributes when the window style has changed.
335 static void sync_window_style( struct x11drv_win_data *data )
337 if (data->whole_window != root_window)
339 XSetWindowAttributes attr;
340 int mask = get_window_attributes( data, &attr );
342 XChangeWindowAttributes( data->display, data->whole_window, mask, &attr );
347 /***********************************************************************
348 * sync_window_region
350 * Update the X11 window region.
352 static void sync_window_region( struct x11drv_win_data *data, HRGN win_region )
354 #ifdef HAVE_LIBXSHAPE
355 HRGN hrgn = win_region;
357 if (!data->whole_window) return;
358 data->shaped = FALSE;
360 if (IsRectEmpty( &data->window_rect )) /* set an empty shape */
362 static XRectangle empty_rect;
363 XShapeCombineRectangles( data->display, data->whole_window, ShapeBounding, 0, 0,
364 &empty_rect, 1, ShapeSet, YXBanded );
365 return;
368 if (hrgn == (HRGN)1) /* hack: win_region == 1 means retrieve region from server */
370 if (!(hrgn = CreateRectRgn( 0, 0, 0, 0 ))) return;
371 if (GetWindowRgn( data->hwnd, hrgn ) == ERROR)
373 DeleteObject( hrgn );
374 hrgn = 0;
378 if (!hrgn)
380 XShapeCombineMask( data->display, data->whole_window, ShapeBounding, 0, 0, None, ShapeSet );
382 else
384 RGNDATA *pRegionData;
386 if (GetWindowLongW( data->hwnd, GWL_EXSTYLE ) & WS_EX_LAYOUTRTL) MirrorRgn( data->hwnd, hrgn );
387 if ((pRegionData = X11DRV_GetRegionData( hrgn, 0 )))
389 XShapeCombineRectangles( data->display, data->whole_window, ShapeBounding,
390 data->window_rect.left - data->whole_rect.left,
391 data->window_rect.top - data->whole_rect.top,
392 (XRectangle *)pRegionData->Buffer,
393 pRegionData->rdh.nCount, ShapeSet, YXBanded );
394 HeapFree(GetProcessHeap(), 0, pRegionData);
395 data->shaped = TRUE;
398 if (hrgn && hrgn != win_region) DeleteObject( hrgn );
399 #endif /* HAVE_LIBXSHAPE */
403 /***********************************************************************
404 * sync_window_opacity
406 static void sync_window_opacity( Display *display, Window win,
407 COLORREF key, BYTE alpha, DWORD flags )
409 unsigned long opacity = 0xffffffff;
411 if (flags & LWA_ALPHA) opacity = (0xffffffff / 0xff) * alpha;
413 if (opacity == 0xffffffff)
414 XDeleteProperty( display, win, x11drv_atom(_NET_WM_WINDOW_OPACITY) );
415 else
416 XChangeProperty( display, win, x11drv_atom(_NET_WM_WINDOW_OPACITY),
417 XA_CARDINAL, 32, PropModeReplace, (unsigned char *)&opacity, 1 );
421 /***********************************************************************
422 * sync_window_text
424 static void sync_window_text( Display *display, Window win, const WCHAR *text )
426 UINT count;
427 char *buffer, *utf8_buffer;
428 XTextProperty prop;
430 /* allocate new buffer for window text */
431 count = WideCharToMultiByte(CP_UNIXCP, 0, text, -1, NULL, 0, NULL, NULL);
432 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, count ))) return;
433 WideCharToMultiByte(CP_UNIXCP, 0, text, -1, buffer, count, NULL, NULL);
435 count = WideCharToMultiByte(CP_UTF8, 0, text, strlenW(text), NULL, 0, NULL, NULL);
436 if (!(utf8_buffer = HeapAlloc( GetProcessHeap(), 0, count )))
438 HeapFree( GetProcessHeap(), 0, buffer );
439 return;
441 WideCharToMultiByte(CP_UTF8, 0, text, strlenW(text), utf8_buffer, count, NULL, NULL);
443 if (XmbTextListToTextProperty( display, &buffer, 1, XStdICCTextStyle, &prop ) == Success)
445 XSetWMName( display, win, &prop );
446 XSetWMIconName( display, win, &prop );
447 XFree( prop.value );
450 Implements a NET_WM UTF-8 title. It should be without a trailing \0,
451 according to the standard
452 ( http://www.pps.jussieu.fr/~jch/software/UTF8_STRING/UTF8_STRING.text ).
454 XChangeProperty( display, win, x11drv_atom(_NET_WM_NAME), x11drv_atom(UTF8_STRING),
455 8, PropModeReplace, (unsigned char *) utf8_buffer, count);
457 HeapFree( GetProcessHeap(), 0, utf8_buffer );
458 HeapFree( GetProcessHeap(), 0, buffer );
462 /***********************************************************************
463 * get_bitmap_argb
465 * Return the bitmap bits in ARGB format. Helper for setting icon hints.
467 static unsigned long *get_bitmap_argb( HDC hdc, HBITMAP color, HBITMAP mask, unsigned int *size )
469 char buffer[FIELD_OFFSET( BITMAPINFO, bmiColors[256] )];
470 BITMAPINFO *info = (BITMAPINFO *)buffer;
471 BITMAP bm;
472 unsigned int *ptr, *bits = NULL;
473 unsigned char *mask_bits = NULL;
474 int i, j;
475 BOOL has_alpha = FALSE;
477 if (!GetObjectW( color, sizeof(bm), &bm )) return NULL;
478 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
479 info->bmiHeader.biWidth = bm.bmWidth;
480 info->bmiHeader.biHeight = -bm.bmHeight;
481 info->bmiHeader.biPlanes = 1;
482 info->bmiHeader.biBitCount = 32;
483 info->bmiHeader.biCompression = BI_RGB;
484 info->bmiHeader.biSizeImage = bm.bmWidth * bm.bmHeight * 4;
485 info->bmiHeader.biXPelsPerMeter = 0;
486 info->bmiHeader.biYPelsPerMeter = 0;
487 info->bmiHeader.biClrUsed = 0;
488 info->bmiHeader.biClrImportant = 0;
489 *size = bm.bmWidth * bm.bmHeight + 2;
490 if (!(bits = HeapAlloc( GetProcessHeap(), 0, *size * sizeof(long) ))) goto failed;
491 if (!GetDIBits( hdc, color, 0, bm.bmHeight, bits + 2, info, DIB_RGB_COLORS )) goto failed;
493 bits[0] = bm.bmWidth;
494 bits[1] = bm.bmHeight;
496 for (i = 0; i < bm.bmWidth * bm.bmHeight; i++)
497 if ((has_alpha = (bits[i + 2] & 0xff000000) != 0)) break;
499 if (!has_alpha)
501 unsigned int width_bytes = (bm.bmWidth + 31) / 32 * 4;
502 /* generate alpha channel from the mask */
503 info->bmiHeader.biBitCount = 1;
504 info->bmiHeader.biSizeImage = width_bytes * bm.bmHeight;
505 if (!(mask_bits = HeapAlloc( GetProcessHeap(), 0, info->bmiHeader.biSizeImage ))) goto failed;
506 if (!GetDIBits( hdc, mask, 0, bm.bmHeight, mask_bits, info, DIB_RGB_COLORS )) goto failed;
507 ptr = bits + 2;
508 for (i = 0; i < bm.bmHeight; i++)
509 for (j = 0; j < bm.bmWidth; j++, ptr++)
510 if (!((mask_bits[i * width_bytes + j / 8] << (j % 8)) & 0x80)) *ptr |= 0xff000000;
511 HeapFree( GetProcessHeap(), 0, mask_bits );
514 /* convert to array of longs */
515 if (bits && sizeof(long) > sizeof(int))
516 for (i = *size - 1; i >= 0; i--) ((unsigned long *)bits)[i] = bits[i];
518 return (unsigned long *)bits;
520 failed:
521 HeapFree( GetProcessHeap(), 0, bits );
522 HeapFree( GetProcessHeap(), 0, mask_bits );
523 return NULL;
527 /***********************************************************************
528 * create_icon_pixmaps
530 static BOOL create_icon_pixmaps( HDC hdc, const ICONINFO *icon, Pixmap *icon_ret, Pixmap *mask_ret )
532 char buffer[FIELD_OFFSET( BITMAPINFO, bmiColors[256] )];
533 BITMAPINFO *info = (BITMAPINFO *)buffer;
534 XVisualInfo vis = default_visual;
535 struct gdi_image_bits bits;
536 Pixmap color_pixmap = 0, mask_pixmap = 0;
537 int lines;
538 unsigned int i;
540 bits.ptr = NULL;
541 bits.free = NULL;
542 bits.is_copy = TRUE;
544 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
545 info->bmiHeader.biBitCount = 0;
546 if (!(lines = GetDIBits( hdc, icon->hbmColor, 0, 0, NULL, info, DIB_RGB_COLORS ))) goto failed;
547 if (!(bits.ptr = HeapAlloc( GetProcessHeap(), 0, info->bmiHeader.biSizeImage ))) goto failed;
548 if (!GetDIBits( hdc, icon->hbmColor, 0, lines, bits.ptr, info, DIB_RGB_COLORS )) goto failed;
550 color_pixmap = create_pixmap_from_image( hdc, &vis, info, &bits, DIB_RGB_COLORS );
551 HeapFree( GetProcessHeap(), 0, bits.ptr );
552 bits.ptr = NULL;
553 if (!color_pixmap) goto failed;
555 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
556 info->bmiHeader.biBitCount = 0;
557 if (!(lines = GetDIBits( hdc, icon->hbmMask, 0, 0, NULL, info, DIB_RGB_COLORS ))) goto failed;
558 if (!(bits.ptr = HeapAlloc( GetProcessHeap(), 0, info->bmiHeader.biSizeImage ))) goto failed;
559 if (!GetDIBits( hdc, icon->hbmMask, 0, lines, bits.ptr, info, DIB_RGB_COLORS )) goto failed;
561 /* invert the mask */
562 for (i = 0; i < info->bmiHeader.biSizeImage / sizeof(DWORD); i++) ((DWORD *)bits.ptr)[i] ^= ~0u;
564 vis.depth = 1;
565 mask_pixmap = create_pixmap_from_image( hdc, &vis, info, &bits, DIB_RGB_COLORS );
566 HeapFree( GetProcessHeap(), 0, bits.ptr );
567 bits.ptr = NULL;
568 if (!mask_pixmap) goto failed;
570 *icon_ret = color_pixmap;
571 *mask_ret = mask_pixmap;
572 return TRUE;
574 failed:
575 if (color_pixmap) XFreePixmap( gdi_display, color_pixmap );
576 if (mask_pixmap) XFreePixmap( gdi_display, mask_pixmap );
577 HeapFree( GetProcessHeap(), 0, bits.ptr );
578 return FALSE;
582 /***********************************************************************
583 * fetch_icon_data
585 static void fetch_icon_data( HWND hwnd, HICON icon_big, HICON icon_small )
587 struct x11drv_win_data *data;
588 ICONINFO ii, ii_small;
589 HDC hDC;
590 unsigned int size;
591 unsigned long *bits;
592 Pixmap icon_pixmap, mask_pixmap;
594 if (!icon_big)
596 icon_big = (HICON)SendMessageW( hwnd, WM_GETICON, ICON_BIG, 0 );
597 if (!icon_big) icon_big = (HICON)GetClassLongPtrW( hwnd, GCLP_HICON );
598 if (!icon_big) icon_big = LoadIconW( 0, (LPWSTR)IDI_WINLOGO );
600 if (!icon_small)
602 icon_small = (HICON)SendMessageW( hwnd, WM_GETICON, ICON_SMALL, 0 );
603 if (!icon_small) icon_small = (HICON)GetClassLongPtrW( hwnd, GCLP_HICONSM );
606 if (!GetIconInfo(icon_big, &ii)) return;
608 hDC = CreateCompatibleDC(0);
609 bits = get_bitmap_argb( hDC, ii.hbmColor, ii.hbmMask, &size );
610 if (bits && GetIconInfo( icon_small, &ii_small ))
612 unsigned int size_small;
613 unsigned long *bits_small, *new;
615 if ((bits_small = get_bitmap_argb( hDC, ii_small.hbmColor, ii_small.hbmMask, &size_small )) &&
616 (bits_small[0] != bits[0] || bits_small[1] != bits[1])) /* size must be different */
618 if ((new = HeapReAlloc( GetProcessHeap(), 0, bits,
619 (size + size_small) * sizeof(unsigned long) )))
621 bits = new;
622 memcpy( bits + size, bits_small, size_small * sizeof(unsigned long) );
623 size += size_small;
626 HeapFree( GetProcessHeap(), 0, bits_small );
627 DeleteObject( ii_small.hbmColor );
628 DeleteObject( ii_small.hbmMask );
631 if (!create_icon_pixmaps( hDC, &ii, &icon_pixmap, &mask_pixmap )) icon_pixmap = mask_pixmap = 0;
633 DeleteObject( ii.hbmColor );
634 DeleteObject( ii.hbmMask );
635 DeleteDC(hDC);
637 if ((data = get_win_data( hwnd )))
639 if (data->icon_pixmap) XFreePixmap( gdi_display, data->icon_pixmap );
640 if (data->icon_mask) XFreePixmap( gdi_display, data->icon_mask );
641 HeapFree( GetProcessHeap(), 0, data->icon_bits );
642 data->icon_pixmap = icon_pixmap;
643 data->icon_mask = mask_pixmap;
644 data->icon_bits = bits;
645 data->icon_size = size;
646 release_win_data( data );
648 else
650 if (icon_pixmap) XFreePixmap( gdi_display, icon_pixmap );
651 if (mask_pixmap) XFreePixmap( gdi_display, mask_pixmap );
652 HeapFree( GetProcessHeap(), 0, bits );
657 /***********************************************************************
658 * set_size_hints
660 * set the window size hints
662 static void set_size_hints( struct x11drv_win_data *data, DWORD style )
664 XSizeHints* size_hints;
666 if (!(size_hints = XAllocSizeHints())) return;
668 size_hints->win_gravity = StaticGravity;
669 size_hints->flags |= PWinGravity;
671 /* don't update size hints if window is not in normal state */
672 if (!(style & (WS_MINIMIZE | WS_MAXIMIZE)))
674 if (data->hwnd != GetDesktopWindow()) /* don't force position of desktop */
676 size_hints->x = data->whole_rect.left;
677 size_hints->y = data->whole_rect.top;
678 size_hints->flags |= PPosition;
680 else size_hints->win_gravity = NorthWestGravity;
682 if (!is_window_resizable( data, style ))
684 size_hints->max_width = data->whole_rect.right - data->whole_rect.left;
685 size_hints->max_height = data->whole_rect.bottom - data->whole_rect.top;
686 if (size_hints->max_width <= 0 ||size_hints->max_height <= 0)
687 size_hints->max_width = size_hints->max_height = 1;
688 size_hints->min_width = size_hints->max_width;
689 size_hints->min_height = size_hints->max_height;
690 size_hints->flags |= PMinSize | PMaxSize;
693 XSetWMNormalHints( data->display, data->whole_window, size_hints );
694 XFree( size_hints );
698 /***********************************************************************
699 * set_mwm_hints
701 static void set_mwm_hints( struct x11drv_win_data *data, DWORD style, DWORD ex_style )
703 MwmHints mwm_hints;
705 if (data->hwnd == GetDesktopWindow())
707 if (is_desktop_fullscreen()) mwm_hints.decorations = 0;
708 else mwm_hints.decorations = MWM_DECOR_TITLE | MWM_DECOR_BORDER | MWM_DECOR_MENU | MWM_DECOR_MINIMIZE;
709 mwm_hints.functions = MWM_FUNC_MOVE | MWM_FUNC_MINIMIZE | MWM_FUNC_CLOSE;
711 else
713 mwm_hints.decorations = get_mwm_decorations( data, style, ex_style );
714 mwm_hints.functions = MWM_FUNC_MOVE;
715 if (is_window_resizable( data, style )) mwm_hints.functions |= MWM_FUNC_RESIZE;
716 if (!(style & WS_DISABLED))
718 if (style & WS_MINIMIZEBOX) mwm_hints.functions |= MWM_FUNC_MINIMIZE;
719 if (style & WS_MAXIMIZEBOX) mwm_hints.functions |= MWM_FUNC_MAXIMIZE;
720 if (style & WS_SYSMENU) mwm_hints.functions |= MWM_FUNC_CLOSE;
724 TRACE( "%p setting mwm hints to %lx,%lx (style %x exstyle %x)\n",
725 data->hwnd, mwm_hints.decorations, mwm_hints.functions, style, ex_style );
727 mwm_hints.flags = MWM_HINTS_FUNCTIONS | MWM_HINTS_DECORATIONS;
728 XChangeProperty( data->display, data->whole_window, x11drv_atom(_MOTIF_WM_HINTS),
729 x11drv_atom(_MOTIF_WM_HINTS), 32, PropModeReplace,
730 (unsigned char*)&mwm_hints, sizeof(mwm_hints)/sizeof(long) );
734 /***********************************************************************
735 * set_style_hints
737 static void set_style_hints( struct x11drv_win_data *data, DWORD style, DWORD ex_style )
739 Window group_leader = data->whole_window;
740 HWND owner = GetWindow( data->hwnd, GW_OWNER );
741 Window owner_win = X11DRV_get_whole_window( owner );
742 XWMHints *wm_hints;
743 Atom window_type;
745 if (owner_win)
747 XSetTransientForHint( data->display, data->whole_window, owner_win );
748 group_leader = owner_win;
751 /* Only use dialog type for owned popups. Metacity allows making fullscreen
752 * only normal windows, and doesn't handle correctly TRANSIENT_FOR hint for
753 * dialogs owned by fullscreen windows.
755 if (((style & WS_POPUP) || (ex_style & WS_EX_DLGMODALFRAME)) && owner)
756 window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_DIALOG);
757 else
758 window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_NORMAL);
760 XChangeProperty(data->display, data->whole_window, x11drv_atom(_NET_WM_WINDOW_TYPE),
761 XA_ATOM, 32, PropModeReplace, (unsigned char*)&window_type, 1);
763 if ((wm_hints = XAllocWMHints()))
765 wm_hints->flags = InputHint | StateHint | WindowGroupHint;
766 wm_hints->input = !use_take_focus && !(style & WS_DISABLED);
767 wm_hints->initial_state = (style & WS_MINIMIZE) ? IconicState : NormalState;
768 wm_hints->window_group = group_leader;
769 if (data->icon_pixmap)
771 wm_hints->icon_pixmap = data->icon_pixmap;
772 wm_hints->icon_mask = data->icon_mask;
773 wm_hints->flags |= IconPixmapHint | IconMaskHint;
775 XSetWMHints( data->display, data->whole_window, wm_hints );
776 XFree( wm_hints );
779 if (data->icon_bits)
780 XChangeProperty( data->display, data->whole_window, x11drv_atom(_NET_WM_ICON),
781 XA_CARDINAL, 32, PropModeReplace,
782 (unsigned char *)data->icon_bits, data->icon_size );
783 else
784 XDeleteProperty( data->display, data->whole_window, x11drv_atom(_NET_WM_ICON) );
789 /***********************************************************************
790 * get_process_name
792 * get the name of the current process for setting class hints
794 static char *get_process_name(void)
796 static char *name;
798 if (!name)
800 WCHAR module[MAX_PATH];
801 DWORD len = GetModuleFileNameW( 0, module, MAX_PATH );
802 if (len && len < MAX_PATH)
804 char *ptr;
805 WCHAR *p, *appname = module;
807 if ((p = strrchrW( appname, '/' ))) appname = p + 1;
808 if ((p = strrchrW( appname, '\\' ))) appname = p + 1;
809 len = WideCharToMultiByte( CP_UNIXCP, 0, appname, -1, NULL, 0, NULL, NULL );
810 if ((ptr = HeapAlloc( GetProcessHeap(), 0, len )))
812 WideCharToMultiByte( CP_UNIXCP, 0, appname, -1, ptr, len, NULL, NULL );
813 name = ptr;
817 return name;
821 /***********************************************************************
822 * set_initial_wm_hints
824 * Set the window manager hints that don't change over the lifetime of a window.
826 static void set_initial_wm_hints( Display *display, Window window )
828 long i;
829 Atom protocols[3];
830 Atom dndVersion = WINE_XDND_VERSION;
831 XClassHint *class_hints;
832 char *process_name = get_process_name();
834 /* wm protocols */
835 i = 0;
836 protocols[i++] = x11drv_atom(WM_DELETE_WINDOW);
837 protocols[i++] = x11drv_atom(_NET_WM_PING);
838 if (use_take_focus) protocols[i++] = x11drv_atom(WM_TAKE_FOCUS);
839 XChangeProperty( display, window, x11drv_atom(WM_PROTOCOLS),
840 XA_ATOM, 32, PropModeReplace, (unsigned char *)protocols, i );
842 /* class hints */
843 if ((class_hints = XAllocClassHint()))
845 static char wine[] = "Wine";
847 class_hints->res_name = process_name;
848 class_hints->res_class = wine;
849 XSetClassHint( display, window, class_hints );
850 XFree( class_hints );
853 /* set the WM_CLIENT_MACHINE and WM_LOCALE_NAME properties */
854 XSetWMProperties(display, window, NULL, NULL, NULL, 0, NULL, NULL, NULL);
855 /* set the pid. together, these properties are needed so the window manager can kill us if we freeze */
856 i = getpid();
857 XChangeProperty(display, window, x11drv_atom(_NET_WM_PID),
858 XA_CARDINAL, 32, PropModeReplace, (unsigned char *)&i, 1);
860 XChangeProperty( display, window, x11drv_atom(XdndAware),
861 XA_ATOM, 32, PropModeReplace, (unsigned char*)&dndVersion, 1 );
863 update_user_time( 0 ); /* make sure that the user time window exists */
864 if (user_time_window)
865 XChangeProperty( display, window, x11drv_atom(_NET_WM_USER_TIME_WINDOW),
866 XA_WINDOW, 32, PropModeReplace, (unsigned char *)&user_time_window, 1 );
870 /***********************************************************************
871 * make_owner_managed
873 * If the window is managed, make sure its owner window is too.
875 static void make_owner_managed( HWND hwnd )
877 HWND owner;
879 if (!(owner = GetWindow( hwnd, GW_OWNER ))) return;
880 if (is_managed( owner )) return;
881 if (!is_managed( hwnd )) return;
883 SetWindowPos( owner, 0, 0, 0, 0, 0,
884 SWP_NOACTIVATE | SWP_NOZORDER | SWP_NOSIZE | SWP_NOMOVE |
885 SWP_NOREDRAW | SWP_DEFERERASE | SWP_NOSENDCHANGING | SWP_STATECHANGED );
889 /***********************************************************************
890 * set_wm_hints
892 * Set all the window manager hints for a window.
894 static void set_wm_hints( struct x11drv_win_data *data )
896 DWORD style, ex_style;
898 if (data->hwnd == GetDesktopWindow())
900 /* force some styles for the desktop to get the correct decorations */
901 style = WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX;
902 ex_style = WS_EX_APPWINDOW;
904 else
906 style = GetWindowLongW( data->hwnd, GWL_STYLE );
907 ex_style = GetWindowLongW( data->hwnd, GWL_EXSTYLE );
910 set_size_hints( data, style );
911 set_mwm_hints( data, style, ex_style );
912 set_style_hints( data, style, ex_style );
916 /***********************************************************************
917 * init_clip_window
919 Window init_clip_window(void)
921 struct x11drv_thread_data *data = x11drv_init_thread_data();
923 if (!data->clip_window &&
924 (data->clip_window = (Window)GetPropA( GetDesktopWindow(), clip_window_prop )))
926 XSelectInput( data->display, data->clip_window, StructureNotifyMask );
928 return data->clip_window;
932 /***********************************************************************
933 * update_user_time
935 void update_user_time( Time time )
937 if (!user_time_window)
939 Window win = XCreateWindow( gdi_display, root_window, -1, -1, 1, 1, 0, CopyFromParent,
940 InputOnly, CopyFromParent, 0, NULL );
941 if (InterlockedCompareExchangePointer( (void **)&user_time_window, (void *)win, 0 ))
942 XDestroyWindow( gdi_display, win );
943 TRACE( "user time window %lx\n", user_time_window );
946 if (!time) return;
947 XLockDisplay( gdi_display );
948 if (!last_user_time || (long)(time - last_user_time) > 0)
950 last_user_time = time;
951 XChangeProperty( gdi_display, user_time_window, x11drv_atom(_NET_WM_USER_TIME),
952 XA_CARDINAL, 32, PropModeReplace, (unsigned char *)&time, 1 );
954 XUnlockDisplay( gdi_display );
957 /***********************************************************************
958 * update_net_wm_states
960 void update_net_wm_states( struct x11drv_win_data *data )
962 static const unsigned int state_atoms[NB_NET_WM_STATES] =
964 XATOM__NET_WM_STATE_FULLSCREEN,
965 XATOM__NET_WM_STATE_ABOVE,
966 XATOM__NET_WM_STATE_MAXIMIZED_VERT,
967 XATOM__NET_WM_STATE_SKIP_PAGER,
968 XATOM__NET_WM_STATE_SKIP_TASKBAR
971 DWORD i, style, ex_style, new_state = 0;
973 if (!data->managed) return;
974 if (data->whole_window == root_window) return;
976 style = GetWindowLongW( data->hwnd, GWL_STYLE );
977 if (is_window_rect_fullscreen( &data->whole_rect ))
979 if ((style & WS_MAXIMIZE) && (style & WS_CAPTION) == WS_CAPTION)
980 new_state |= (1 << NET_WM_STATE_MAXIMIZED);
981 else if (!(style & WS_MINIMIZE))
982 new_state |= (1 << NET_WM_STATE_FULLSCREEN);
984 else if (style & WS_MAXIMIZE)
985 new_state |= (1 << NET_WM_STATE_MAXIMIZED);
987 ex_style = GetWindowLongW( data->hwnd, GWL_EXSTYLE );
988 if (ex_style & WS_EX_TOPMOST)
989 new_state |= (1 << NET_WM_STATE_ABOVE);
990 if (ex_style & (WS_EX_TOOLWINDOW | WS_EX_NOACTIVATE))
991 new_state |= (1 << NET_WM_STATE_SKIP_TASKBAR) | (1 << NET_WM_STATE_SKIP_PAGER);
992 if (!(ex_style & WS_EX_APPWINDOW) && GetWindow( data->hwnd, GW_OWNER ))
993 new_state |= (1 << NET_WM_STATE_SKIP_TASKBAR);
995 if (!data->mapped) /* set the _NET_WM_STATE atom directly */
997 Atom atoms[NB_NET_WM_STATES+1];
998 DWORD count;
1000 for (i = count = 0; i < NB_NET_WM_STATES; i++)
1002 if (!(new_state & (1 << i))) continue;
1003 TRACE( "setting wm state %u for unmapped window %p/%lx\n",
1004 i, data->hwnd, data->whole_window );
1005 atoms[count++] = X11DRV_Atoms[state_atoms[i] - FIRST_XATOM];
1006 if (state_atoms[i] == XATOM__NET_WM_STATE_MAXIMIZED_VERT)
1007 atoms[count++] = x11drv_atom(_NET_WM_STATE_MAXIMIZED_HORZ);
1009 XChangeProperty( data->display, data->whole_window, x11drv_atom(_NET_WM_STATE), XA_ATOM,
1010 32, PropModeReplace, (unsigned char *)atoms, count );
1012 else /* ask the window manager to do it for us */
1014 XEvent xev;
1016 xev.xclient.type = ClientMessage;
1017 xev.xclient.window = data->whole_window;
1018 xev.xclient.message_type = x11drv_atom(_NET_WM_STATE);
1019 xev.xclient.serial = 0;
1020 xev.xclient.display = data->display;
1021 xev.xclient.send_event = True;
1022 xev.xclient.format = 32;
1023 xev.xclient.data.l[3] = 1;
1025 for (i = 0; i < NB_NET_WM_STATES; i++)
1027 if (!((data->net_wm_state ^ new_state) & (1 << i))) continue; /* unchanged */
1029 TRACE( "setting wm state %u for window %p/%lx to %u prev %u\n",
1030 i, data->hwnd, data->whole_window,
1031 (new_state & (1 << i)) != 0, (data->net_wm_state & (1 << i)) != 0 );
1033 xev.xclient.data.l[0] = (new_state & (1 << i)) ? _NET_WM_STATE_ADD : _NET_WM_STATE_REMOVE;
1034 xev.xclient.data.l[1] = X11DRV_Atoms[state_atoms[i] - FIRST_XATOM];
1035 xev.xclient.data.l[2] = ((state_atoms[i] == XATOM__NET_WM_STATE_MAXIMIZED_VERT) ?
1036 x11drv_atom(_NET_WM_STATE_MAXIMIZED_HORZ) : 0);
1037 XSendEvent( data->display, root_window, False,
1038 SubstructureRedirectMask | SubstructureNotifyMask, &xev );
1041 data->net_wm_state = new_state;
1045 /***********************************************************************
1046 * set_xembed_flags
1048 static void set_xembed_flags( struct x11drv_win_data *data, unsigned long flags )
1050 unsigned long info[2];
1052 if (!data->whole_window) return;
1054 info[0] = 0; /* protocol version */
1055 info[1] = flags;
1056 XChangeProperty( data->display, data->whole_window, x11drv_atom(_XEMBED_INFO),
1057 x11drv_atom(_XEMBED_INFO), 32, PropModeReplace, (unsigned char*)info, 2 );
1061 /***********************************************************************
1062 * map_window
1064 static void map_window( HWND hwnd, DWORD new_style )
1066 struct x11drv_win_data *data;
1068 make_owner_managed( hwnd );
1069 wait_for_withdrawn_state( hwnd, TRUE );
1071 if (!(data = get_win_data( hwnd ))) return;
1073 if (data->whole_window && !data->mapped)
1075 TRACE( "win %p/%lx\n", data->hwnd, data->whole_window );
1077 remove_startup_notification( data->display, data->whole_window );
1078 set_wm_hints( data );
1080 if (!data->embedded)
1082 update_net_wm_states( data );
1083 sync_window_style( data );
1084 XMapWindow( data->display, data->whole_window );
1085 XFlush( data->display );
1086 if (data->surface && data->vis.visualid != default_visual.visualid)
1087 data->surface->funcs->flush( data->surface );
1089 else set_xembed_flags( data, XEMBED_MAPPED );
1091 data->mapped = TRUE;
1092 data->iconic = (new_style & WS_MINIMIZE) != 0;
1094 release_win_data( data );
1098 /***********************************************************************
1099 * unmap_window
1101 static void unmap_window( HWND hwnd )
1103 struct x11drv_win_data *data;
1105 wait_for_withdrawn_state( hwnd, FALSE );
1107 if (!(data = get_win_data( hwnd ))) return;
1109 if (data->mapped)
1111 TRACE( "win %p/%lx\n", data->hwnd, data->whole_window );
1113 if (data->embedded) set_xembed_flags( data, 0 );
1114 else if (!data->managed) XUnmapWindow( data->display, data->whole_window );
1115 else XWithdrawWindow( data->display, data->whole_window, data->vis.screen );
1117 data->mapped = FALSE;
1118 data->net_wm_state = 0;
1120 release_win_data( data );
1124 /***********************************************************************
1125 * make_window_embedded
1127 void make_window_embedded( struct x11drv_win_data *data )
1129 /* the window cannot be mapped before being embedded */
1130 if (data->mapped)
1132 if (data->managed) XUnmapWindow( data->display, data->whole_window );
1133 else XWithdrawWindow( data->display, data->whole_window, data->vis.screen );
1134 data->net_wm_state = 0;
1136 data->embedded = TRUE;
1137 data->managed = TRUE;
1138 sync_window_style( data );
1139 set_xembed_flags( data, (data->mapped || data->embedder) ? XEMBED_MAPPED : 0 );
1143 /***********************************************************************
1144 * X11DRV_window_to_X_rect
1146 * Convert a rect from client to X window coordinates
1148 static void X11DRV_window_to_X_rect( struct x11drv_win_data *data, RECT *rect )
1150 RECT rc;
1152 if (!data->managed) return;
1153 if (IsRectEmpty( rect )) return;
1155 get_x11_rect_offset( data, &rc );
1157 rect->left -= rc.left;
1158 rect->right -= rc.right;
1159 rect->top -= rc.top;
1160 rect->bottom -= rc.bottom;
1161 if (rect->top >= rect->bottom) rect->bottom = rect->top + 1;
1162 if (rect->left >= rect->right) rect->right = rect->left + 1;
1166 /***********************************************************************
1167 * X11DRV_X_to_window_rect
1169 * Opposite of X11DRV_window_to_X_rect
1171 void X11DRV_X_to_window_rect( struct x11drv_win_data *data, RECT *rect )
1173 RECT rc;
1175 if (!data->managed) return;
1176 if (IsRectEmpty( rect )) return;
1178 get_x11_rect_offset( data, &rc );
1180 rect->left += rc.left;
1181 rect->right += rc.right;
1182 rect->top += rc.top;
1183 rect->bottom += rc.bottom;
1184 if (rect->top >= rect->bottom) rect->bottom = rect->top + 1;
1185 if (rect->left >= rect->right) rect->right = rect->left + 1;
1189 /***********************************************************************
1190 * sync_window_position
1192 * Synchronize the X window position with the Windows one
1194 static void sync_window_position( struct x11drv_win_data *data,
1195 UINT swp_flags, const RECT *old_window_rect,
1196 const RECT *old_whole_rect, const RECT *old_client_rect )
1198 DWORD style = GetWindowLongW( data->hwnd, GWL_STYLE );
1199 DWORD ex_style = GetWindowLongW( data->hwnd, GWL_EXSTYLE );
1200 XWindowChanges changes;
1201 unsigned int mask = 0;
1203 if (data->managed && data->iconic) return;
1205 /* resizing a managed maximized window is not allowed */
1206 if (!(style & WS_MAXIMIZE) || !data->managed)
1208 changes.width = data->whole_rect.right - data->whole_rect.left;
1209 changes.height = data->whole_rect.bottom - data->whole_rect.top;
1210 /* if window rect is empty force size to 1x1 */
1211 if (changes.width <= 0 || changes.height <= 0) changes.width = changes.height = 1;
1212 if (changes.width > 65535) changes.width = 65535;
1213 if (changes.height > 65535) changes.height = 65535;
1214 mask |= CWWidth | CWHeight;
1217 /* only the size is allowed to change for the desktop window */
1218 if (data->whole_window != root_window)
1220 POINT pt = virtual_screen_to_root( data->whole_rect.left, data->whole_rect.top );
1221 changes.x = pt.x;
1222 changes.y = pt.y;
1223 mask |= CWX | CWY;
1226 if (!(swp_flags & SWP_NOZORDER) || (swp_flags & SWP_SHOWWINDOW))
1228 /* find window that this one must be after */
1229 HWND prev = GetWindow( data->hwnd, GW_HWNDPREV );
1230 while (prev && !(GetWindowLongW( prev, GWL_STYLE ) & WS_VISIBLE))
1231 prev = GetWindow( prev, GW_HWNDPREV );
1232 if (!prev) /* top child */
1234 changes.stack_mode = Above;
1235 mask |= CWStackMode;
1237 /* should use stack_mode Below but most window managers don't get it right */
1238 /* and Above with a sibling doesn't work so well either, so we ignore it */
1241 set_size_hints( data, style );
1242 set_mwm_hints( data, style, ex_style );
1243 data->configure_serial = NextRequest( data->display );
1244 XReconfigureWMWindow( data->display, data->whole_window, data->vis.screen, mask, &changes );
1245 #ifdef HAVE_LIBXSHAPE
1246 if (IsRectEmpty( old_window_rect ) != IsRectEmpty( &data->window_rect ))
1247 sync_window_region( data, (HRGN)1 );
1248 if (data->shaped)
1250 int old_x_offset = old_window_rect->left - old_whole_rect->left;
1251 int old_y_offset = old_window_rect->top - old_whole_rect->top;
1252 int new_x_offset = data->window_rect.left - data->whole_rect.left;
1253 int new_y_offset = data->window_rect.top - data->whole_rect.top;
1254 if (old_x_offset != new_x_offset || old_y_offset != new_y_offset)
1255 XShapeOffsetShape( data->display, data->whole_window, ShapeBounding,
1256 new_x_offset - old_x_offset, new_y_offset - old_y_offset );
1258 #endif
1260 TRACE( "win %p/%lx pos %d,%d,%dx%d after %lx changes=%x serial=%lu\n",
1261 data->hwnd, data->whole_window, data->whole_rect.left, data->whole_rect.top,
1262 data->whole_rect.right - data->whole_rect.left,
1263 data->whole_rect.bottom - data->whole_rect.top,
1264 changes.sibling, mask, data->configure_serial );
1268 /***********************************************************************
1269 * sync_client_position
1271 * Synchronize the X client window position with the Windows one
1273 static void sync_client_position( struct x11drv_win_data *data,
1274 const RECT *old_client_rect, const RECT *old_whole_rect )
1276 int mask = 0;
1277 XWindowChanges changes;
1279 if (!data->client_window) return;
1281 changes.x = data->client_rect.left - data->whole_rect.left;
1282 changes.y = data->client_rect.top - data->whole_rect.top;
1283 changes.width = min( max( 1, data->client_rect.right - data->client_rect.left ), 65535 );
1284 changes.height = min( max( 1, data->client_rect.bottom - data->client_rect.top ), 65535 );
1286 if (changes.x != old_client_rect->left - old_whole_rect->left) mask |= CWX;
1287 if (changes.y != old_client_rect->top - old_whole_rect->top) mask |= CWY;
1288 if (changes.width != old_client_rect->right - old_client_rect->left) mask |= CWWidth;
1289 if (changes.height != old_client_rect->bottom - old_client_rect->top) mask |= CWHeight;
1291 if (mask)
1293 TRACE( "setting client win %lx pos %d,%d,%dx%d changes=%x\n",
1294 data->client_window, changes.x, changes.y, changes.width, changes.height, mask );
1295 XConfigureWindow( data->display, data->client_window, mask, &changes );
1300 /***********************************************************************
1301 * move_window_bits
1303 * Move the window bits when a window is moved.
1305 static void move_window_bits( HWND hwnd, Window window, const RECT *old_rect, const RECT *new_rect,
1306 const RECT *old_client_rect, const RECT *new_client_rect,
1307 const RECT *new_window_rect )
1309 RECT src_rect = *old_rect;
1310 RECT dst_rect = *new_rect;
1311 HDC hdc_src, hdc_dst;
1312 INT code;
1313 HRGN rgn;
1314 HWND parent = 0;
1316 if (!window)
1318 OffsetRect( &dst_rect, -new_window_rect->left, -new_window_rect->top );
1319 parent = GetAncestor( hwnd, GA_PARENT );
1320 hdc_src = GetDCEx( parent, 0, DCX_CACHE );
1321 hdc_dst = GetDCEx( hwnd, 0, DCX_CACHE | DCX_WINDOW );
1323 else
1325 OffsetRect( &dst_rect, -new_client_rect->left, -new_client_rect->top );
1326 /* make src rect relative to the old position of the window */
1327 OffsetRect( &src_rect, -old_client_rect->left, -old_client_rect->top );
1328 if (dst_rect.left == src_rect.left && dst_rect.top == src_rect.top) return;
1329 hdc_src = hdc_dst = GetDCEx( hwnd, 0, DCX_CACHE );
1332 rgn = CreateRectRgnIndirect( &dst_rect );
1333 SelectClipRgn( hdc_dst, rgn );
1334 DeleteObject( rgn );
1335 ExcludeUpdateRgn( hdc_dst, hwnd );
1337 code = X11DRV_START_EXPOSURES;
1338 ExtEscape( hdc_dst, X11DRV_ESCAPE, sizeof(code), (LPSTR)&code, 0, NULL );
1340 TRACE( "copying bits for win %p/%lx %s -> %s\n",
1341 hwnd, window, wine_dbgstr_rect(&src_rect), wine_dbgstr_rect(&dst_rect) );
1342 BitBlt( hdc_dst, dst_rect.left, dst_rect.top,
1343 dst_rect.right - dst_rect.left, dst_rect.bottom - dst_rect.top,
1344 hdc_src, src_rect.left, src_rect.top, SRCCOPY );
1346 rgn = 0;
1347 code = X11DRV_END_EXPOSURES;
1348 ExtEscape( hdc_dst, X11DRV_ESCAPE, sizeof(code), (LPSTR)&code, sizeof(rgn), (LPSTR)&rgn );
1350 ReleaseDC( hwnd, hdc_dst );
1351 if (hdc_src != hdc_dst) ReleaseDC( parent, hdc_src );
1353 if (rgn)
1355 if (!window)
1357 /* map region to client rect since we are using DCX_WINDOW */
1358 OffsetRgn( rgn, new_window_rect->left - new_client_rect->left,
1359 new_window_rect->top - new_client_rect->top );
1360 RedrawWindow( hwnd, NULL, rgn,
1361 RDW_INVALIDATE | RDW_FRAME | RDW_ERASE | RDW_ALLCHILDREN );
1363 else RedrawWindow( hwnd, NULL, rgn, RDW_INVALIDATE | RDW_ERASE | RDW_ALLCHILDREN );
1364 DeleteObject( rgn );
1369 /**********************************************************************
1370 * create_client_window
1372 Window create_client_window( struct x11drv_win_data *data, const XVisualInfo *visual )
1374 XSetWindowAttributes attr;
1375 int x = data->client_rect.left - data->whole_rect.left;
1376 int y = data->client_rect.top - data->whole_rect.top;
1377 int cx = min( max( 1, data->client_rect.right - data->client_rect.left ), 65535 );
1378 int cy = min( max( 1, data->client_rect.bottom - data->client_rect.top ), 65535 );
1380 if (data->client_window)
1382 XDeleteContext( data->display, data->client_window, winContext );
1383 XDestroyWindow( data->display, data->client_window );
1386 if (data->colormap) XFreeColormap( data->display, data->colormap );
1387 data->colormap = XCreateColormap( data->display, root_window, visual->visual,
1388 (visual->class == PseudoColor ||
1389 visual->class == GrayScale ||
1390 visual->class == DirectColor) ? AllocAll : AllocNone );
1391 attr.colormap = data->colormap;
1392 attr.bit_gravity = NorthWestGravity;
1393 attr.win_gravity = NorthWestGravity;
1394 attr.backing_store = NotUseful;
1395 attr.event_mask = ExposureMask;
1396 attr.border_pixel = 0;
1398 data->client_window = XCreateWindow( data->display, data->whole_window, x, y, cx, cy,
1399 0, default_visual.depth, InputOutput, visual->visual,
1400 CWBitGravity | CWWinGravity | CWBackingStore |
1401 CWColormap | CWEventMask | CWBorderPixel, &attr );
1402 if (!data->client_window) return 0;
1404 XSaveContext( data->display, data->client_window, winContext, (char *)data->hwnd );
1405 XMapWindow( data->display, data->client_window );
1406 XSync( data->display, False );
1407 return data->client_window;
1411 /**********************************************************************
1412 * create_whole_window
1414 * Create the whole X window for a given window
1416 static void create_whole_window( struct x11drv_win_data *data )
1418 int cx, cy, mask;
1419 XSetWindowAttributes attr;
1420 WCHAR text[1024];
1421 COLORREF key;
1422 BYTE alpha;
1423 DWORD layered_flags;
1424 HRGN win_rgn;
1425 POINT pos;
1427 if (!data->managed && is_window_managed( data->hwnd, SWP_NOACTIVATE, &data->window_rect ))
1429 TRACE( "making win %p/%lx managed\n", data->hwnd, data->whole_window );
1430 data->managed = TRUE;
1433 if ((win_rgn = CreateRectRgn( 0, 0, 0, 0 )) && GetWindowRgn( data->hwnd, win_rgn ) == ERROR)
1435 DeleteObject( win_rgn );
1436 win_rgn = 0;
1438 data->shaped = (win_rgn != 0);
1440 if (data->vis.visualid != default_visual.visualid)
1441 data->colormap = XCreateColormap( data->display, root_window, data->vis.visual, AllocNone );
1443 mask = get_window_attributes( data, &attr );
1445 data->whole_rect = data->window_rect;
1446 X11DRV_window_to_X_rect( data, &data->whole_rect );
1447 if (!(cx = data->whole_rect.right - data->whole_rect.left)) cx = 1;
1448 else if (cx > 65535) cx = 65535;
1449 if (!(cy = data->whole_rect.bottom - data->whole_rect.top)) cy = 1;
1450 else if (cy > 65535) cy = 65535;
1452 pos = virtual_screen_to_root( data->whole_rect.left, data->whole_rect.top );
1453 data->whole_window = XCreateWindow( data->display, root_window, pos.y, pos.y,
1454 cx, cy, 0, data->vis.depth, InputOutput,
1455 data->vis.visual, mask, &attr );
1456 if (!data->whole_window) goto done;
1458 set_initial_wm_hints( data->display, data->whole_window );
1459 set_wm_hints( data );
1461 XSaveContext( data->display, data->whole_window, winContext, (char *)data->hwnd );
1462 SetPropA( data->hwnd, whole_window_prop, (HANDLE)data->whole_window );
1464 /* set the window text */
1465 if (!InternalGetWindowText( data->hwnd, text, sizeof(text)/sizeof(WCHAR) )) text[0] = 0;
1466 sync_window_text( data->display, data->whole_window, text );
1468 /* set the window region */
1469 if (win_rgn || IsRectEmpty( &data->window_rect )) sync_window_region( data, win_rgn );
1471 /* set the window opacity */
1472 if (!GetLayeredWindowAttributes( data->hwnd, &key, &alpha, &layered_flags )) layered_flags = 0;
1473 sync_window_opacity( data->display, data->whole_window, key, alpha, layered_flags );
1475 XFlush( data->display ); /* make sure the window exists before we start painting to it */
1477 sync_window_cursor( data->whole_window );
1479 done:
1480 if (win_rgn) DeleteObject( win_rgn );
1484 /**********************************************************************
1485 * destroy_whole_window
1487 * Destroy the whole X window for a given window.
1489 static void destroy_whole_window( struct x11drv_win_data *data, BOOL already_destroyed )
1491 if (!data->whole_window)
1493 if (data->embedded)
1495 Window xwin = (Window)GetPropA( data->hwnd, foreign_window_prop );
1496 if (xwin)
1498 if (!already_destroyed) XSelectInput( data->display, xwin, 0 );
1499 XDeleteContext( data->display, xwin, winContext );
1500 RemovePropA( data->hwnd, foreign_window_prop );
1503 return;
1507 TRACE( "win %p xwin %lx\n", data->hwnd, data->whole_window );
1508 XDeleteContext( data->display, data->whole_window, winContext );
1509 if (data->client_window) XDeleteContext( data->display, data->client_window, winContext );
1510 if (!already_destroyed) XDestroyWindow( data->display, data->whole_window );
1511 if (data->colormap) XFreeColormap( data->display, data->colormap );
1512 data->whole_window = data->client_window = 0;
1513 data->colormap = 0;
1514 data->wm_state = WithdrawnState;
1515 data->net_wm_state = 0;
1516 data->mapped = FALSE;
1517 if (data->xic)
1519 XUnsetICFocus( data->xic );
1520 XDestroyIC( data->xic );
1521 data->xic = 0;
1523 /* Outlook stops processing messages after destroying a dialog, so we need an explicit flush */
1524 XFlush( data->display );
1525 if (data->surface) window_surface_release( data->surface );
1526 data->surface = NULL;
1527 RemovePropA( data->hwnd, whole_window_prop );
1531 /**********************************************************************
1532 * set_window_visual
1534 * Change the visual by destroying and recreating the X window if needed.
1536 void set_window_visual( struct x11drv_win_data *data, const XVisualInfo *vis )
1538 Window client_window = data->client_window;
1539 Window whole_window = data->whole_window;
1541 if (data->vis.visualid == vis->visualid) return;
1542 data->client_window = 0;
1543 destroy_whole_window( data, client_window != 0 /* don't destroy whole_window until reparented */ );
1544 if (data->surface) window_surface_release( data->surface );
1545 data->surface = NULL;
1546 data->vis = *vis;
1547 create_whole_window( data );
1548 if (!client_window) return;
1549 /* move the client to the new parent */
1550 XReparentWindow( data->display, client_window, data->whole_window,
1551 data->client_rect.left - data->whole_rect.left,
1552 data->client_rect.top - data->whole_rect.top );
1553 data->client_window = client_window;
1554 XDestroyWindow( data->display, whole_window );
1558 /*****************************************************************
1559 * SetWindowText (X11DRV.@)
1561 void CDECL X11DRV_SetWindowText( HWND hwnd, LPCWSTR text )
1563 Window win;
1565 if ((win = X11DRV_get_whole_window( hwnd )) && win != DefaultRootWindow(gdi_display))
1567 Display *display = thread_init_display();
1568 sync_window_text( display, win, text );
1573 /***********************************************************************
1574 * SetWindowStyle (X11DRV.@)
1576 * Update the X state of a window to reflect a style change
1578 void CDECL X11DRV_SetWindowStyle( HWND hwnd, INT offset, STYLESTRUCT *style )
1580 struct x11drv_win_data *data;
1581 DWORD changed = style->styleNew ^ style->styleOld;
1583 if (hwnd == GetDesktopWindow()) return;
1584 if (!(data = get_win_data( hwnd ))) return;
1585 if (!data->whole_window) goto done;
1587 if (offset == GWL_STYLE && (changed & WS_DISABLED)) set_wm_hints( data );
1589 if (offset == GWL_EXSTYLE && (changed & WS_EX_LAYERED)) /* changing WS_EX_LAYERED resets attributes */
1591 data->layered = FALSE;
1592 set_window_visual( data, &default_visual );
1593 sync_window_opacity( data->display, data->whole_window, 0, 0, 0 );
1594 if (data->surface) set_surface_color_key( data->surface, CLR_INVALID );
1596 done:
1597 release_win_data( data );
1601 /***********************************************************************
1602 * DestroyWindow (X11DRV.@)
1604 void CDECL X11DRV_DestroyWindow( HWND hwnd )
1606 struct x11drv_thread_data *thread_data = x11drv_thread_data();
1607 struct x11drv_win_data *data;
1609 destroy_gl_drawable( hwnd );
1611 if (!(data = get_win_data( hwnd ))) return;
1613 destroy_whole_window( data, FALSE );
1614 if (thread_data->last_focus == hwnd) thread_data->last_focus = 0;
1615 if (thread_data->last_xic_hwnd == hwnd) thread_data->last_xic_hwnd = 0;
1616 if (data->icon_pixmap) XFreePixmap( gdi_display, data->icon_pixmap );
1617 if (data->icon_mask) XFreePixmap( gdi_display, data->icon_mask );
1618 HeapFree( GetProcessHeap(), 0, data->icon_bits );
1619 XDeleteContext( gdi_display, (XID)hwnd, win_data_context );
1620 release_win_data( data );
1621 HeapFree( GetProcessHeap(), 0, data );
1625 /***********************************************************************
1626 * X11DRV_DestroyNotify
1628 void X11DRV_DestroyNotify( HWND hwnd, XEvent *event )
1630 struct x11drv_win_data *data;
1631 BOOL embedded;
1633 if (!(data = get_win_data( hwnd ))) return;
1634 embedded = data->embedded;
1635 if (!embedded) FIXME( "window %p/%lx destroyed from the outside\n", hwnd, data->whole_window );
1637 destroy_whole_window( data, TRUE );
1638 release_win_data( data );
1639 if (embedded) SendMessageW( hwnd, WM_CLOSE, 0, 0 );
1643 static struct x11drv_win_data *alloc_win_data( Display *display, HWND hwnd )
1645 struct x11drv_win_data *data;
1647 if ((data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*data))))
1649 data->display = display;
1650 data->vis = default_visual;
1651 data->hwnd = hwnd;
1652 EnterCriticalSection( &win_data_section );
1653 XSaveContext( gdi_display, (XID)hwnd, win_data_context, (char *)data );
1655 return data;
1659 /* initialize the desktop window id in the desktop manager process */
1660 BOOL create_desktop_win_data( Window win )
1662 struct x11drv_thread_data *thread_data = x11drv_thread_data();
1663 Display *display = thread_data->display;
1664 struct x11drv_win_data *data;
1666 if (!(data = alloc_win_data( display, GetDesktopWindow() ))) return FALSE;
1667 data->whole_window = win;
1668 data->managed = TRUE;
1669 SetPropA( data->hwnd, whole_window_prop, (HANDLE)win );
1670 set_initial_wm_hints( display, win );
1671 release_win_data( data );
1672 if (thread_data->clip_window) XReparentWindow( display, thread_data->clip_window, win, 0, 0 );
1673 return TRUE;
1676 /**********************************************************************
1677 * CreateDesktopWindow (X11DRV.@)
1679 BOOL CDECL X11DRV_CreateDesktopWindow( HWND hwnd )
1681 unsigned int width, height;
1683 /* retrieve the real size of the desktop */
1684 SERVER_START_REQ( get_window_rectangles )
1686 req->handle = wine_server_user_handle( hwnd );
1687 req->relative = COORDS_CLIENT;
1688 wine_server_call( req );
1689 width = reply->window.right;
1690 height = reply->window.bottom;
1692 SERVER_END_REQ;
1694 if (!width && !height) /* not initialized yet */
1696 RECT rect = get_virtual_screen_rect();
1698 SERVER_START_REQ( set_window_pos )
1700 req->handle = wine_server_user_handle( hwnd );
1701 req->previous = 0;
1702 req->swp_flags = SWP_NOZORDER;
1703 req->window.left = rect.left;
1704 req->window.top = rect.top;
1705 req->window.right = rect.right;
1706 req->window.bottom = rect.bottom;
1707 req->client = req->window;
1708 wine_server_call( req );
1710 SERVER_END_REQ;
1712 else
1714 Window win = (Window)GetPropA( hwnd, whole_window_prop );
1715 if (win && win != root_window) X11DRV_init_desktop( win, width, height );
1717 return TRUE;
1721 /**********************************************************************
1722 * CreateWindow (X11DRV.@)
1724 BOOL CDECL X11DRV_CreateWindow( HWND hwnd )
1726 if (hwnd == GetDesktopWindow())
1728 struct x11drv_thread_data *data = x11drv_init_thread_data();
1729 XSetWindowAttributes attr;
1731 /* create the cursor clipping window */
1732 attr.override_redirect = TRUE;
1733 attr.event_mask = StructureNotifyMask | FocusChangeMask;
1734 data->clip_window = XCreateWindow( data->display, root_window, 0, 0, 1, 1, 0, 0,
1735 InputOnly, default_visual.visual,
1736 CWOverrideRedirect | CWEventMask, &attr );
1737 XFlush( data->display );
1738 SetPropA( hwnd, clip_window_prop, (HANDLE)data->clip_window );
1740 return TRUE;
1744 /***********************************************************************
1745 * get_win_data
1747 * Lock and return the X11 data structure associated with a window.
1749 struct x11drv_win_data *get_win_data( HWND hwnd )
1751 char *data;
1753 if (!hwnd) return NULL;
1754 EnterCriticalSection( &win_data_section );
1755 if (!XFindContext( gdi_display, (XID)hwnd, win_data_context, &data ))
1756 return (struct x11drv_win_data *)data;
1757 LeaveCriticalSection( &win_data_section );
1758 return NULL;
1762 /***********************************************************************
1763 * release_win_data
1765 * Release the data returned by get_win_data.
1767 void release_win_data( struct x11drv_win_data *data )
1769 if (data) LeaveCriticalSection( &win_data_section );
1773 /***********************************************************************
1774 * X11DRV_create_win_data
1776 * Create an X11 data window structure for an existing window.
1778 static struct x11drv_win_data *X11DRV_create_win_data( HWND hwnd, const RECT *window_rect,
1779 const RECT *client_rect )
1781 Display *display;
1782 struct x11drv_win_data *data;
1783 HWND parent;
1785 if (!(parent = GetAncestor( hwnd, GA_PARENT ))) return NULL; /* desktop */
1787 /* don't create win data for HWND_MESSAGE windows */
1788 if (parent != GetDesktopWindow() && !GetAncestor( parent, GA_PARENT )) return NULL;
1790 if (GetWindowThreadProcessId( hwnd, NULL ) != GetCurrentThreadId()) return NULL;
1792 display = thread_init_display();
1793 init_clip_window(); /* make sure the clip window is initialized in this thread */
1795 if (!(data = alloc_win_data( display, hwnd ))) return NULL;
1797 data->whole_rect = data->window_rect = *window_rect;
1798 data->client_rect = *client_rect;
1799 if (parent == GetDesktopWindow())
1801 create_whole_window( data );
1802 TRACE( "win %p/%lx window %s whole %s client %s\n",
1803 hwnd, data->whole_window, wine_dbgstr_rect( &data->window_rect ),
1804 wine_dbgstr_rect( &data->whole_rect ), wine_dbgstr_rect( &data->client_rect ));
1806 return data;
1810 /* window procedure for foreign windows */
1811 static LRESULT WINAPI foreign_window_proc( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
1813 switch(msg)
1815 case WM_WINDOWPOSCHANGED:
1816 update_systray_balloon_position();
1817 break;
1818 case WM_PARENTNOTIFY:
1819 if (LOWORD(wparam) == WM_DESTROY)
1821 TRACE( "%p: got parent notify destroy for win %lx\n", hwnd, lparam );
1822 PostMessageW( hwnd, WM_CLOSE, 0, 0 ); /* so that we come back here once the child is gone */
1824 return 0;
1825 case WM_CLOSE:
1826 if (GetWindow( hwnd, GW_CHILD )) return 0; /* refuse to die if we still have children */
1827 break;
1829 return DefWindowProcW( hwnd, msg, wparam, lparam );
1833 /***********************************************************************
1834 * create_foreign_window
1836 * Create a foreign window for the specified X window and its ancestors
1838 HWND create_foreign_window( Display *display, Window xwin )
1840 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};
1841 static BOOL class_registered;
1842 struct x11drv_win_data *data;
1843 HWND hwnd, parent;
1844 POINT pos;
1845 Window xparent, xroot;
1846 Window *xchildren;
1847 unsigned int nchildren;
1848 XWindowAttributes attr;
1849 DWORD style = WS_CLIPCHILDREN;
1851 if (!class_registered)
1853 WNDCLASSEXW class;
1855 memset( &class, 0, sizeof(class) );
1856 class.cbSize = sizeof(class);
1857 class.lpfnWndProc = foreign_window_proc;
1858 class.lpszClassName = classW;
1859 if (!RegisterClassExW( &class ) && GetLastError() != ERROR_CLASS_ALREADY_EXISTS)
1861 ERR( "Could not register foreign window class\n" );
1862 return FALSE;
1864 class_registered = TRUE;
1867 if (XFindContext( display, xwin, winContext, (char **)&hwnd )) hwnd = 0;
1868 if (hwnd) return hwnd; /* already created */
1870 XSelectInput( display, xwin, StructureNotifyMask );
1871 if (!XGetWindowAttributes( display, xwin, &attr ) ||
1872 !XQueryTree( display, xwin, &xroot, &xparent, &xchildren, &nchildren ))
1874 XSelectInput( display, xwin, 0 );
1875 return 0;
1877 XFree( xchildren );
1879 if (xparent == xroot)
1881 parent = GetDesktopWindow();
1882 style |= WS_POPUP;
1883 pos = root_to_virtual_screen( attr.x, attr.y );
1885 else
1887 parent = create_foreign_window( display, xparent );
1888 style |= WS_CHILD;
1889 pos.x = attr.x;
1890 pos.y = attr.y;
1893 hwnd = CreateWindowW( classW, NULL, style, pos.x, pos.y, attr.width, attr.height,
1894 parent, 0, 0, NULL );
1896 if (!(data = alloc_win_data( display, hwnd )))
1898 DestroyWindow( hwnd );
1899 return 0;
1901 SetRect( &data->window_rect, pos.x, pos.y, pos.x + attr.width, pos.y + attr.height );
1902 data->whole_rect = data->client_rect = data->window_rect;
1903 data->whole_window = data->client_window = 0;
1904 data->embedded = TRUE;
1905 data->mapped = TRUE;
1907 SetPropA( hwnd, foreign_window_prop, (HANDLE)xwin );
1908 XSaveContext( display, xwin, winContext, (char *)data->hwnd );
1910 TRACE( "win %lx parent %p style %08x %s -> hwnd %p\n",
1911 xwin, parent, style, wine_dbgstr_rect(&data->window_rect), hwnd );
1913 release_win_data( data );
1915 ShowWindow( hwnd, SW_SHOW );
1916 return hwnd;
1920 /***********************************************************************
1921 * X11DRV_get_whole_window
1923 * Return the X window associated with the full area of a window
1925 Window X11DRV_get_whole_window( HWND hwnd )
1927 struct x11drv_win_data *data = get_win_data( hwnd );
1928 Window ret;
1930 if (!data)
1932 if (hwnd == GetDesktopWindow()) return root_window;
1933 return (Window)GetPropA( hwnd, whole_window_prop );
1935 ret = data->whole_window;
1936 release_win_data( data );
1937 return ret;
1941 /***********************************************************************
1942 * X11DRV_get_ic
1944 * Return the X input context associated with a window
1946 XIC X11DRV_get_ic( HWND hwnd )
1948 struct x11drv_win_data *data = get_win_data( hwnd );
1949 XIM xim;
1950 XIC ret = 0;
1952 if (data)
1954 x11drv_thread_data()->last_xic_hwnd = hwnd;
1955 ret = data->xic;
1956 if (!ret && (xim = x11drv_thread_data()->xim)) ret = X11DRV_CreateIC( xim, data );
1957 release_win_data( data );
1959 return ret;
1963 /***********************************************************************
1964 * X11DRV_GetDC (X11DRV.@)
1966 void CDECL X11DRV_GetDC( HDC hdc, HWND hwnd, HWND top, const RECT *win_rect,
1967 const RECT *top_rect, DWORD flags )
1969 struct x11drv_escape_set_drawable escape;
1970 HWND parent;
1972 escape.code = X11DRV_SET_DRAWABLE;
1973 escape.mode = IncludeInferiors;
1975 escape.dc_rect.left = win_rect->left - top_rect->left;
1976 escape.dc_rect.top = win_rect->top - top_rect->top;
1977 escape.dc_rect.right = win_rect->right - top_rect->left;
1978 escape.dc_rect.bottom = win_rect->bottom - top_rect->top;
1980 if (top == hwnd)
1982 struct x11drv_win_data *data = get_win_data( hwnd );
1984 escape.drawable = data ? data->whole_window : X11DRV_get_whole_window( hwnd );
1986 /* special case: when repainting the root window, clip out top-level windows */
1987 if (data && data->whole_window == root_window) escape.mode = ClipByChildren;
1988 release_win_data( data );
1990 else
1992 /* find the first ancestor that has a drawable */
1993 for (parent = hwnd; parent && parent != top; parent = GetAncestor( parent, GA_PARENT ))
1994 if ((escape.drawable = X11DRV_get_whole_window( parent ))) break;
1996 if (escape.drawable)
1998 POINT pt = { 0, 0 };
1999 MapWindowPoints( 0, parent, &pt, 1 );
2000 escape.dc_rect = *win_rect;
2001 OffsetRect( &escape.dc_rect, pt.x, pt.y );
2002 if (flags & DCX_CLIPCHILDREN) escape.mode = ClipByChildren;
2004 else escape.drawable = X11DRV_get_whole_window( top );
2007 ExtEscape( hdc, X11DRV_ESCAPE, sizeof(escape), (LPSTR)&escape, 0, NULL );
2011 /***********************************************************************
2012 * X11DRV_ReleaseDC (X11DRV.@)
2014 void CDECL X11DRV_ReleaseDC( HWND hwnd, HDC hdc )
2016 struct x11drv_escape_set_drawable escape;
2018 escape.code = X11DRV_SET_DRAWABLE;
2019 escape.drawable = root_window;
2020 escape.mode = IncludeInferiors;
2021 escape.dc_rect = get_virtual_screen_rect();
2022 OffsetRect( &escape.dc_rect, -2 * escape.dc_rect.left, -2 * escape.dc_rect.top );
2023 ExtEscape( hdc, X11DRV_ESCAPE, sizeof(escape), (LPSTR)&escape, 0, NULL );
2027 /*************************************************************************
2028 * ScrollDC (X11DRV.@)
2030 BOOL CDECL X11DRV_ScrollDC( HDC hdc, INT dx, INT dy, HRGN update )
2032 RECT rect;
2033 BOOL ret;
2034 HRGN expose_rgn = 0;
2036 GetClipBox( hdc, &rect );
2038 if (update)
2040 INT code = X11DRV_START_EXPOSURES;
2041 ExtEscape( hdc, X11DRV_ESCAPE, sizeof(code), (LPSTR)&code, 0, NULL );
2043 ret = BitBlt( hdc, rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top,
2044 hdc, rect.left - dx, rect.top - dy, SRCCOPY );
2046 code = X11DRV_END_EXPOSURES;
2047 ExtEscape( hdc, X11DRV_ESCAPE, sizeof(code), (LPSTR)&code,
2048 sizeof(expose_rgn), (LPSTR)&expose_rgn );
2049 if (expose_rgn)
2051 CombineRgn( update, update, expose_rgn, RGN_OR );
2052 DeleteObject( expose_rgn );
2055 else ret = BitBlt( hdc, rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top,
2056 hdc, rect.left - dx, rect.top - dy, SRCCOPY );
2058 return ret;
2062 /***********************************************************************
2063 * SetCapture (X11DRV.@)
2065 void CDECL X11DRV_SetCapture( HWND hwnd, UINT flags )
2067 struct x11drv_thread_data *thread_data = x11drv_thread_data();
2069 if (!thread_data) return;
2070 if (!(flags & (GUI_INMOVESIZE | GUI_INMENUMODE))) return;
2072 if (hwnd)
2074 Window grab_win = X11DRV_get_whole_window( GetAncestor( hwnd, GA_ROOT ) );
2076 if (!grab_win) return;
2077 XFlush( gdi_display );
2078 XGrabPointer( thread_data->display, grab_win, False,
2079 PointerMotionMask | ButtonPressMask | ButtonReleaseMask,
2080 GrabModeAsync, GrabModeAsync, None, None, CurrentTime );
2081 thread_data->grab_window = grab_win;
2083 else /* release capture */
2085 XFlush( gdi_display );
2086 XUngrabPointer( thread_data->display, CurrentTime );
2087 XFlush( thread_data->display );
2088 thread_data->grab_window = None;
2093 /*****************************************************************
2094 * SetParent (X11DRV.@)
2096 void CDECL X11DRV_SetParent( HWND hwnd, HWND parent, HWND old_parent )
2098 struct x11drv_win_data *data;
2100 if (parent == old_parent) return;
2101 if (!(data = get_win_data( hwnd ))) return;
2102 if (data->embedded) goto done;
2104 if (parent != GetDesktopWindow()) /* a child window */
2106 if (old_parent == GetDesktopWindow())
2108 /* destroy the old X windows */
2109 destroy_whole_window( data, FALSE );
2110 data->managed = FALSE;
2113 else /* new top level window */
2115 create_whole_window( data );
2117 done:
2118 release_win_data( data );
2119 set_gl_drawable_parent( hwnd, parent );
2120 fetch_icon_data( hwnd, 0, 0 );
2124 static inline RECT get_surface_rect( const RECT *visible_rect )
2126 RECT rect = get_virtual_screen_rect();
2128 IntersectRect( &rect, &rect, visible_rect );
2129 OffsetRect( &rect, -visible_rect->left, -visible_rect->top );
2130 rect.left &= ~31;
2131 rect.top &= ~31;
2132 rect.right = max( rect.left + 32, (rect.right + 31) & ~31 );
2133 rect.bottom = max( rect.top + 32, (rect.bottom + 31) & ~31 );
2134 return rect;
2138 /***********************************************************************
2139 * WindowPosChanging (X11DRV.@)
2141 void CDECL X11DRV_WindowPosChanging( HWND hwnd, HWND insert_after, UINT swp_flags,
2142 const RECT *window_rect, const RECT *client_rect, RECT *visible_rect,
2143 struct window_surface **surface )
2145 struct x11drv_win_data *data = get_win_data( hwnd );
2146 RECT surface_rect;
2147 DWORD flags;
2148 COLORREF key;
2149 BOOL layered = GetWindowLongW( hwnd, GWL_EXSTYLE ) & WS_EX_LAYERED;
2151 if (!data && !(data = X11DRV_create_win_data( hwnd, window_rect, client_rect ))) return;
2153 /* check if we need to switch the window to managed */
2154 if (!data->managed && data->whole_window && is_window_managed( hwnd, swp_flags, window_rect ))
2156 TRACE( "making win %p/%lx managed\n", hwnd, data->whole_window );
2157 release_win_data( data );
2158 unmap_window( hwnd );
2159 if (!(data = get_win_data( hwnd ))) return;
2160 data->managed = TRUE;
2163 *visible_rect = *window_rect;
2164 X11DRV_window_to_X_rect( data, visible_rect );
2166 /* create the window surface if necessary */
2168 if (!data->whole_window && !data->embedded) goto done;
2169 if (swp_flags & SWP_HIDEWINDOW) goto done;
2170 if (data->vis.visualid != default_visual.visualid) goto done;
2172 if (*surface) window_surface_release( *surface );
2173 *surface = NULL; /* indicate that we want to draw directly to the window */
2175 if (data->embedded) goto done;
2176 if (data->whole_window == root_window) goto done;
2177 if (data->client_window) goto done;
2178 if (!client_side_graphics && !layered) goto done;
2180 surface_rect = get_surface_rect( visible_rect );
2181 if (data->surface)
2183 if (!memcmp( &data->surface->rect, &surface_rect, sizeof(surface_rect) ))
2185 /* existing surface is good enough */
2186 window_surface_add_ref( data->surface );
2187 *surface = data->surface;
2188 goto done;
2191 else if (!(swp_flags & SWP_SHOWWINDOW) && !(GetWindowLongW( hwnd, GWL_STYLE ) & WS_VISIBLE)) goto done;
2193 if (!layered || !GetLayeredWindowAttributes( hwnd, &key, NULL, &flags ) || !(flags & LWA_COLORKEY))
2194 key = CLR_INVALID;
2196 *surface = create_surface( data->whole_window, &data->vis, &surface_rect, key, FALSE );
2198 done:
2199 release_win_data( data );
2203 /***********************************************************************
2204 * WindowPosChanged (X11DRV.@)
2206 void CDECL X11DRV_WindowPosChanged( HWND hwnd, HWND insert_after, UINT swp_flags,
2207 const RECT *rectWindow, const RECT *rectClient,
2208 const RECT *visible_rect, const RECT *valid_rects,
2209 struct window_surface *surface )
2211 struct x11drv_thread_data *thread_data;
2212 struct x11drv_win_data *data;
2213 DWORD new_style = GetWindowLongW( hwnd, GWL_STYLE );
2214 RECT old_window_rect, old_whole_rect, old_client_rect;
2215 int event_type;
2217 if (!(data = get_win_data( hwnd ))) return;
2219 thread_data = x11drv_thread_data();
2221 old_window_rect = data->window_rect;
2222 old_whole_rect = data->whole_rect;
2223 old_client_rect = data->client_rect;
2224 data->window_rect = *rectWindow;
2225 data->whole_rect = *visible_rect;
2226 data->client_rect = *rectClient;
2227 if (data->vis.visualid == default_visual.visualid)
2229 if (surface) window_surface_add_ref( surface );
2230 if (data->surface) window_surface_release( data->surface );
2231 data->surface = surface;
2234 TRACE( "win %p window %s client %s style %08x flags %08x\n",
2235 hwnd, wine_dbgstr_rect(rectWindow), wine_dbgstr_rect(rectClient), new_style, swp_flags );
2237 if (!IsRectEmpty( &valid_rects[0] ))
2239 Window window = data->whole_window;
2240 int x_offset = old_whole_rect.left - data->whole_rect.left;
2241 int y_offset = old_whole_rect.top - data->whole_rect.top;
2243 /* if all that happened is that the whole window moved, copy everything */
2244 if (!(swp_flags & SWP_FRAMECHANGED) &&
2245 old_whole_rect.right - data->whole_rect.right == x_offset &&
2246 old_whole_rect.bottom - data->whole_rect.bottom == y_offset &&
2247 old_client_rect.left - data->client_rect.left == x_offset &&
2248 old_client_rect.right - data->client_rect.right == x_offset &&
2249 old_client_rect.top - data->client_rect.top == y_offset &&
2250 old_client_rect.bottom - data->client_rect.bottom == y_offset &&
2251 !memcmp( &valid_rects[0], &data->client_rect, sizeof(RECT) ))
2253 /* if we have an X window the bits will be moved by the X server */
2254 if (!window && (x_offset != 0 || y_offset != 0))
2256 release_win_data( data );
2257 move_window_bits( hwnd, window, &old_whole_rect, visible_rect,
2258 &old_client_rect, rectClient, rectWindow );
2259 if (!(data = get_win_data( hwnd ))) return;
2262 else
2264 release_win_data( data );
2265 move_window_bits( hwnd, window, &valid_rects[1], &valid_rects[0],
2266 &old_client_rect, rectClient, rectWindow );
2267 if (!(data = get_win_data( hwnd ))) return;
2271 XFlush( gdi_display ); /* make sure painting is done before we move the window */
2273 sync_client_position( data, &old_client_rect, &old_whole_rect );
2275 if (!data->whole_window)
2277 release_win_data( data );
2278 sync_gl_drawable( hwnd, visible_rect, rectClient );
2279 return;
2282 /* check if we are currently processing an event relevant to this window */
2283 event_type = 0;
2284 if (thread_data &&
2285 thread_data->current_event &&
2286 thread_data->current_event->xany.window == data->whole_window)
2288 event_type = thread_data->current_event->type;
2289 if (event_type != ConfigureNotify && event_type != PropertyNotify &&
2290 event_type != GravityNotify && event_type != ReparentNotify)
2291 event_type = 0; /* ignore other events */
2294 if (data->mapped && event_type != ReparentNotify)
2296 if (((swp_flags & SWP_HIDEWINDOW) && !(new_style & WS_VISIBLE)) ||
2297 (!event_type && !(new_style & WS_MINIMIZE) &&
2298 !is_window_rect_mapped( rectWindow ) && is_window_rect_mapped( &old_window_rect )))
2300 release_win_data( data );
2301 unmap_window( hwnd );
2302 if (is_window_rect_fullscreen( &old_window_rect )) reset_clipping_window();
2303 if (!(data = get_win_data( hwnd ))) return;
2307 /* don't change position if we are about to minimize or maximize a managed window */
2308 if (!event_type &&
2309 !(data->managed && (swp_flags & SWP_STATECHANGED) && (new_style & (WS_MINIMIZE|WS_MAXIMIZE))))
2310 sync_window_position( data, swp_flags, &old_window_rect, &old_whole_rect, &old_client_rect );
2312 if ((new_style & WS_VISIBLE) &&
2313 ((new_style & WS_MINIMIZE) || is_window_rect_mapped( rectWindow )))
2315 if (!data->mapped)
2317 BOOL needs_icon = !data->icon_pixmap;
2318 BOOL needs_map = TRUE;
2320 /* layered windows are mapped only once their attributes are set */
2321 if (GetWindowLongW( hwnd, GWL_EXSTYLE ) & WS_EX_LAYERED) needs_map = data->layered;
2322 release_win_data( data );
2323 if (needs_icon) fetch_icon_data( hwnd, 0, 0 );
2324 if (needs_map) map_window( hwnd, new_style );
2325 return;
2327 else if ((swp_flags & SWP_STATECHANGED) && (!data->iconic != !(new_style & WS_MINIMIZE)))
2329 set_wm_hints( data );
2330 data->iconic = (new_style & WS_MINIMIZE) != 0;
2331 TRACE( "changing win %p iconic state to %u\n", data->hwnd, data->iconic );
2332 if (data->iconic)
2333 XIconifyWindow( data->display, data->whole_window, data->vis.screen );
2334 else if (is_window_rect_mapped( rectWindow ))
2335 XMapWindow( data->display, data->whole_window );
2336 update_net_wm_states( data );
2338 else
2340 if (swp_flags & (SWP_FRAMECHANGED|SWP_STATECHANGED)) set_wm_hints( data );
2341 if (!event_type) update_net_wm_states( data );
2345 XFlush( data->display ); /* make sure changes are done before we start painting again */
2346 if (data->surface && data->vis.visualid != default_visual.visualid)
2347 data->surface->funcs->flush( data->surface );
2349 release_win_data( data );
2353 /***********************************************************************
2354 * ShowWindow (X11DRV.@)
2356 UINT CDECL X11DRV_ShowWindow( HWND hwnd, INT cmd, RECT *rect, UINT swp )
2358 int x, y;
2359 unsigned int width, height, border, depth;
2360 Window root, top;
2361 POINT pos;
2362 DWORD style = GetWindowLongW( hwnd, GWL_STYLE );
2363 struct x11drv_thread_data *thread_data = x11drv_thread_data();
2364 struct x11drv_win_data *data = get_win_data( hwnd );
2366 if (!data || !data->whole_window || !data->managed) goto done;
2367 if (IsRectEmpty( rect )) goto done;
2368 if (style & WS_MINIMIZE)
2370 if (rect->left != -32000 || rect->top != -32000)
2372 OffsetRect( rect, -32000 - rect->left, -32000 - rect->top );
2373 swp &= ~(SWP_NOMOVE | SWP_NOCLIENTMOVE);
2375 goto done;
2377 if (!data->mapped || data->iconic) goto done;
2379 /* only fetch the new rectangle if the ShowWindow was a result of a window manager event */
2381 if (!thread_data->current_event || thread_data->current_event->xany.window != data->whole_window)
2382 goto done;
2384 if (thread_data->current_event->type != ConfigureNotify &&
2385 thread_data->current_event->type != PropertyNotify)
2386 goto done;
2388 TRACE( "win %p/%lx cmd %d at %s flags %08x\n",
2389 hwnd, data->whole_window, cmd, wine_dbgstr_rect(rect), swp );
2391 XGetGeometry( thread_data->display, data->whole_window,
2392 &root, &x, &y, &width, &height, &border, &depth );
2393 XTranslateCoordinates( thread_data->display, data->whole_window, root, 0, 0, &x, &y, &top );
2394 pos = root_to_virtual_screen( x, y );
2395 rect->left = pos.x;
2396 rect->top = pos.y;
2397 rect->right = pos.x + width;
2398 rect->bottom = pos.y + height;
2399 X11DRV_X_to_window_rect( data, rect );
2400 swp &= ~(SWP_NOMOVE | SWP_NOCLIENTMOVE | SWP_NOSIZE | SWP_NOCLIENTSIZE);
2402 done:
2403 release_win_data( data );
2404 return swp;
2408 /**********************************************************************
2409 * SetWindowIcon (X11DRV.@)
2411 * hIcon or hIconSm has changed (or is being initialised for the
2412 * first time). Complete the X11 driver-specific initialisation
2413 * and set the window hints.
2415 void CDECL X11DRV_SetWindowIcon( HWND hwnd, UINT type, HICON icon )
2417 struct x11drv_win_data *data;
2419 if (!(data = get_win_data( hwnd ))) return;
2420 if (!data->whole_window) goto done;
2421 release_win_data( data ); /* release the lock, fetching the icon requires sending messages */
2423 if (type == ICON_BIG) fetch_icon_data( hwnd, icon, 0 );
2424 else fetch_icon_data( hwnd, 0, icon );
2426 if (!(data = get_win_data( hwnd ))) return;
2427 set_wm_hints( data );
2428 done:
2429 release_win_data( data );
2433 /***********************************************************************
2434 * SetWindowRgn (X11DRV.@)
2436 * Assign specified region to window (for non-rectangular windows)
2438 void CDECL X11DRV_SetWindowRgn( HWND hwnd, HRGN hrgn, BOOL redraw )
2440 struct x11drv_win_data *data;
2442 if ((data = get_win_data( hwnd )))
2444 sync_window_region( data, hrgn );
2445 release_win_data( data );
2447 else if (X11DRV_get_whole_window( hwnd ))
2449 SendMessageW( hwnd, WM_X11DRV_SET_WIN_REGION, 0, 0 );
2454 /***********************************************************************
2455 * SetLayeredWindowAttributes (X11DRV.@)
2457 * Set transparency attributes for a layered window.
2459 void CDECL X11DRV_SetLayeredWindowAttributes( HWND hwnd, COLORREF key, BYTE alpha, DWORD flags )
2461 struct x11drv_win_data *data = get_win_data( hwnd );
2463 if (data)
2465 if (data->whole_window)
2466 sync_window_opacity( data->display, data->whole_window, key, alpha, flags );
2467 if (data->surface)
2468 set_surface_color_key( data->surface, (flags & LWA_COLORKEY) ? key : CLR_INVALID );
2470 data->layered = TRUE;
2471 if (!data->mapped) /* mapping is delayed until attributes are set */
2473 DWORD style = GetWindowLongW( data->hwnd, GWL_STYLE );
2475 if ((style & WS_VISIBLE) &&
2476 ((style & WS_MINIMIZE) || is_window_rect_mapped( &data->window_rect )))
2478 release_win_data( data );
2479 map_window( hwnd, style );
2480 return;
2483 release_win_data( data );
2485 else
2487 Window win = X11DRV_get_whole_window( hwnd );
2488 if (win)
2490 sync_window_opacity( gdi_display, win, key, alpha, flags );
2491 if (flags & LWA_COLORKEY)
2492 FIXME( "LWA_COLORKEY not supported on foreign process window %p\n", hwnd );
2498 /*****************************************************************************
2499 * UpdateLayeredWindow (X11DRV.@)
2501 BOOL CDECL X11DRV_UpdateLayeredWindow( HWND hwnd, const UPDATELAYEREDWINDOWINFO *info,
2502 const RECT *window_rect )
2504 struct window_surface *surface;
2505 struct x11drv_win_data *data;
2506 BLENDFUNCTION blend = { AC_SRC_OVER, 0, 255, 0 };
2507 COLORREF color_key = (info->dwFlags & ULW_COLORKEY) ? info->crKey : CLR_INVALID;
2508 char buffer[FIELD_OFFSET( BITMAPINFO, bmiColors[256] )];
2509 BITMAPINFO *bmi = (BITMAPINFO *)buffer;
2510 void *src_bits, *dst_bits;
2511 RECT rect;
2512 HDC hdc = 0;
2513 HBITMAP dib;
2514 BOOL ret = FALSE;
2516 if (!(data = get_win_data( hwnd ))) return FALSE;
2518 data->layered = TRUE;
2519 if (!data->embedded && argb_visual.visualid) set_window_visual( data, &argb_visual );
2521 rect = *window_rect;
2522 OffsetRect( &rect, -window_rect->left, -window_rect->top );
2524 surface = data->surface;
2525 if (!surface || memcmp( &surface->rect, &rect, sizeof(RECT) ))
2527 data->surface = create_surface( data->whole_window, &data->vis, &rect,
2528 color_key, !data->embedded );
2529 if (surface) window_surface_release( surface );
2530 surface = data->surface;
2532 else set_surface_color_key( surface, color_key );
2534 if (surface) window_surface_add_ref( surface );
2535 release_win_data( data );
2537 if (!surface) return FALSE;
2538 if (!info->hdcSrc)
2540 window_surface_release( surface );
2541 return TRUE;
2544 dst_bits = surface->funcs->get_info( surface, bmi );
2546 if (!(dib = CreateDIBSection( info->hdcDst, bmi, DIB_RGB_COLORS, &src_bits, NULL, 0 ))) goto done;
2547 if (!(hdc = CreateCompatibleDC( 0 ))) goto done;
2549 SelectObject( hdc, dib );
2551 surface->funcs->lock( surface );
2553 if (info->prcDirty)
2555 IntersectRect( &rect, &rect, info->prcDirty );
2556 memcpy( src_bits, dst_bits, bmi->bmiHeader.biSizeImage );
2557 PatBlt( hdc, rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top, BLACKNESS );
2559 ret = GdiAlphaBlend( hdc, rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top,
2560 info->hdcSrc,
2561 rect.left + (info->pptSrc ? info->pptSrc->x : 0),
2562 rect.top + (info->pptSrc ? info->pptSrc->y : 0),
2563 rect.right - rect.left, rect.bottom - rect.top,
2564 (info->dwFlags & ULW_ALPHA) ? *info->pblend : blend );
2565 if (ret)
2567 memcpy( dst_bits, src_bits, bmi->bmiHeader.biSizeImage );
2568 add_bounds_rect( surface->funcs->get_bounds( surface ), &rect );
2571 surface->funcs->unlock( surface );
2572 surface->funcs->flush( surface );
2574 done:
2575 window_surface_release( surface );
2576 if (hdc) DeleteDC( hdc );
2577 if (dib) DeleteObject( dib );
2578 return ret;
2582 /**********************************************************************
2583 * X11DRV_WindowMessage (X11DRV.@)
2585 LRESULT CDECL X11DRV_WindowMessage( HWND hwnd, UINT msg, WPARAM wp, LPARAM lp )
2587 struct x11drv_win_data *data;
2589 switch(msg)
2591 case WM_X11DRV_ACQUIRE_SELECTION:
2592 return X11DRV_AcquireClipboard( hwnd );
2593 case WM_X11DRV_SET_WIN_REGION:
2594 if ((data = get_win_data( hwnd )))
2596 sync_window_region( data, (HRGN)1 );
2597 release_win_data( data );
2599 return 0;
2600 case WM_X11DRV_RESIZE_DESKTOP:
2601 X11DRV_resize_desktop( LOWORD(lp), HIWORD(lp) );
2602 return 0;
2603 case WM_X11DRV_SET_CURSOR:
2604 if ((data = get_win_data( hwnd )))
2606 if (data->whole_window) set_window_cursor( data->whole_window, (HCURSOR)lp );
2607 release_win_data( data );
2609 else if (hwnd == x11drv_thread_data()->clip_hwnd)
2610 set_window_cursor( x11drv_thread_data()->clip_window, (HCURSOR)lp );
2611 return 0;
2612 case WM_X11DRV_CLIP_CURSOR:
2613 return clip_cursor_notify( hwnd, (HWND)lp );
2614 default:
2615 FIXME( "got window msg %x hwnd %p wp %lx lp %lx\n", msg, hwnd, wp, lp );
2616 return 0;
2621 /***********************************************************************
2622 * is_netwm_supported
2624 static BOOL is_netwm_supported( Display *display, Atom atom )
2626 static Atom *net_supported;
2627 static int net_supported_count = -1;
2628 int i;
2630 if (net_supported_count == -1)
2632 Atom type;
2633 int format;
2634 unsigned long count, remaining;
2636 if (!XGetWindowProperty( display, DefaultRootWindow(display), x11drv_atom(_NET_SUPPORTED), 0,
2637 ~0UL, False, XA_ATOM, &type, &format, &count,
2638 &remaining, (unsigned char **)&net_supported ))
2639 net_supported_count = get_property_size( format, count ) / sizeof(Atom);
2640 else
2641 net_supported_count = 0;
2644 for (i = 0; i < net_supported_count; i++)
2645 if (net_supported[i] == atom) return TRUE;
2646 return FALSE;
2650 /***********************************************************************
2651 * SysCommand (X11DRV.@)
2653 * Perform WM_SYSCOMMAND handling.
2655 LRESULT CDECL X11DRV_SysCommand( HWND hwnd, WPARAM wparam, LPARAM lparam )
2657 WPARAM hittest = wparam & 0x0f;
2658 int dir;
2659 struct x11drv_win_data *data;
2661 if (!(data = get_win_data( hwnd ))) return -1;
2662 if (!data->whole_window || !data->managed || !data->mapped) goto failed;
2664 switch (wparam & 0xfff0)
2666 case SC_MOVE:
2667 if (!hittest) dir = _NET_WM_MOVERESIZE_MOVE_KEYBOARD;
2668 else dir = _NET_WM_MOVERESIZE_MOVE;
2669 break;
2670 case SC_SIZE:
2671 /* windows without WS_THICKFRAME are not resizable through the window manager */
2672 if (!(GetWindowLongW( hwnd, GWL_STYLE ) & WS_THICKFRAME)) goto failed;
2674 switch (hittest)
2676 case WMSZ_LEFT: dir = _NET_WM_MOVERESIZE_SIZE_LEFT; break;
2677 case WMSZ_RIGHT: dir = _NET_WM_MOVERESIZE_SIZE_RIGHT; break;
2678 case WMSZ_TOP: dir = _NET_WM_MOVERESIZE_SIZE_TOP; break;
2679 case WMSZ_TOPLEFT: dir = _NET_WM_MOVERESIZE_SIZE_TOPLEFT; break;
2680 case WMSZ_TOPRIGHT: dir = _NET_WM_MOVERESIZE_SIZE_TOPRIGHT; break;
2681 case WMSZ_BOTTOM: dir = _NET_WM_MOVERESIZE_SIZE_BOTTOM; break;
2682 case WMSZ_BOTTOMLEFT: dir = _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT; break;
2683 case WMSZ_BOTTOMRIGHT: dir = _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT; break;
2684 default: dir = _NET_WM_MOVERESIZE_SIZE_KEYBOARD; break;
2686 break;
2688 case SC_KEYMENU:
2689 /* prevent a simple ALT press+release from activating the system menu,
2690 * as that can get confusing on managed windows */
2691 if ((WCHAR)lparam) goto failed; /* got an explicit char */
2692 if (GetMenu( hwnd )) goto failed; /* window has a real menu */
2693 if (!(GetWindowLongW( hwnd, GWL_STYLE ) & WS_SYSMENU)) goto failed; /* no system menu */
2694 TRACE( "ignoring SC_KEYMENU wp %lx lp %lx\n", wparam, lparam );
2695 release_win_data( data );
2696 return 0;
2698 default:
2699 goto failed;
2702 if (IsZoomed(hwnd)) goto failed;
2704 if (!is_netwm_supported( data->display, x11drv_atom(_NET_WM_MOVERESIZE) ))
2706 TRACE( "_NET_WM_MOVERESIZE not supported\n" );
2707 goto failed;
2710 release_win_data( data );
2711 move_resize_window( hwnd, dir );
2712 return 0;
2714 failed:
2715 release_win_data( data );
2716 return -1;