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
33 #include <X11/Xresource.h>
34 #include <X11/Xutil.h>
36 #include <X11/extensions/shape.h>
37 #endif /* HAVE_LIBXSHAPE */
43 #include "wine/unicode.h"
46 #include "wine/debug.h"
47 #include "wine/server.h"
50 WINE_DEFAULT_DEBUG_CHANNEL(x11drv);
52 #define _NET_WM_MOVERESIZE_SIZE_TOPLEFT 0
53 #define _NET_WM_MOVERESIZE_SIZE_TOP 1
54 #define _NET_WM_MOVERESIZE_SIZE_TOPRIGHT 2
55 #define _NET_WM_MOVERESIZE_SIZE_RIGHT 3
56 #define _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT 4
57 #define _NET_WM_MOVERESIZE_SIZE_BOTTOM 5
58 #define _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT 6
59 #define _NET_WM_MOVERESIZE_SIZE_LEFT 7
60 #define _NET_WM_MOVERESIZE_MOVE 8
61 #define _NET_WM_MOVERESIZE_SIZE_KEYBOARD 9
62 #define _NET_WM_MOVERESIZE_MOVE_KEYBOARD 10
64 #define _NET_WM_STATE_REMOVE 0
65 #define _NET_WM_STATE_ADD 1
66 #define _NET_WM_STATE_TOGGLE 2
68 #define SWP_AGG_NOPOSCHANGE (SWP_NOSIZE | SWP_NOMOVE | SWP_NOCLIENTSIZE | SWP_NOCLIENTMOVE | SWP_NOZORDER)
70 /* is cursor clipping active? */
71 int clipping_cursor = 0;
73 /* X context to associate a hwnd to an X window */
74 XContext winContext = 0;
76 /* X context to associate a struct x11drv_win_data to an hwnd */
77 XContext win_data_context = 0;
79 /* time of last user event and window where it's stored */
80 static Time last_user_time;
81 static Window user_time_window;
83 static const char foreign_window_prop[] = "__wine_x11_foreign_window";
84 static const char whole_window_prop[] = "__wine_x11_whole_window";
85 static const char clip_window_prop[] = "__wine_x11_clip_window";
86 static const char managed_prop[] = "__wine_x11_managed";
88 static CRITICAL_SECTION win_data_section;
89 static CRITICAL_SECTION_DEBUG critsect_debug =
91 0, 0, &win_data_section,
92 { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
93 0, 0, { (DWORD_PTR)(__FILE__ ": win_data_section") }
95 static CRITICAL_SECTION win_data_section = { &critsect_debug, -1, 0, 0, 0, 0 };
98 /***********************************************************************
99 * http://standards.freedesktop.org/startup-notification-spec
101 static void remove_startup_notification(Display *display, Window window)
103 static LONG startup_notification_removed = 0;
112 if (InterlockedCompareExchange(&startup_notification_removed, 1, 0) != 0)
115 if (GetEnvironmentVariableA("DESKTOP_STARTUP_ID", id, sizeof(id)) == 0)
117 SetEnvironmentVariableA("DESKTOP_STARTUP_ID", NULL);
119 if ((src = strstr( id, "_TIME" ))) update_user_time( atol( src + 5 ));
121 pos = snprintf(message, sizeof(message), "remove: ID=");
122 message[pos++] = '"';
123 for (i = 0; id[i] && pos < sizeof(message) - 2; i++)
125 if (id[i] == '"' || id[i] == '\\')
126 message[pos++] = '\\';
127 message[pos++] = id[i];
129 message[pos++] = '"';
130 message[pos++] = '\0';
132 xevent.xclient.type = ClientMessage;
133 xevent.xclient.message_type = x11drv_atom(_NET_STARTUP_INFO_BEGIN);
134 xevent.xclient.display = display;
135 xevent.xclient.window = window;
136 xevent.xclient.format = 8;
139 srclen = strlen(src) + 1;
146 memset(&xevent.xclient.data.b[0], 0, 20);
147 memcpy(&xevent.xclient.data.b[0], src, msglen);
151 XSendEvent( display, DefaultRootWindow( display ), False, PropertyChangeMask, &xevent );
152 xevent.xclient.message_type = x11drv_atom(_NET_STARTUP_INFO);
157 struct has_popup_result
163 static BOOL is_managed( HWND hwnd )
165 struct x11drv_win_data *data = get_win_data( hwnd );
166 BOOL ret = data && data->managed;
167 release_win_data( data );
171 static BOOL CALLBACK has_managed_popup( HWND hwnd, LPARAM lparam )
173 struct has_popup_result *result = (struct has_popup_result *)lparam;
175 if (hwnd == result->hwnd) return FALSE; /* popups are always above owner */
176 if (GetWindow( hwnd, GW_OWNER ) != result->hwnd) return TRUE;
177 result->found = is_managed( hwnd );
178 return !result->found;
181 static BOOL has_owned_popups( HWND hwnd )
183 struct has_popup_result result;
186 result.found = FALSE;
187 EnumWindows( has_managed_popup, (LPARAM)&result );
191 /***********************************************************************
194 * Check if a given window should be managed
196 static BOOL is_window_managed( HWND hwnd, UINT swp_flags, const RECT *window_rect )
198 DWORD style, ex_style;
200 if (!managed_mode) return FALSE;
202 /* child windows are not managed */
203 style = GetWindowLongW( hwnd, GWL_STYLE );
204 if ((style & (WS_CHILD|WS_POPUP)) == WS_CHILD) return FALSE;
205 /* activated windows are managed */
206 if (!(swp_flags & (SWP_NOACTIVATE|SWP_HIDEWINDOW))) return TRUE;
207 if (hwnd == GetActiveWindow()) return TRUE;
208 /* windows with caption are managed */
209 if ((style & WS_CAPTION) == WS_CAPTION) return TRUE;
210 /* windows with thick frame are managed */
211 if (style & WS_THICKFRAME) return TRUE;
212 if (style & WS_POPUP)
217 /* popup with sysmenu == caption are managed */
218 if (style & WS_SYSMENU) return TRUE;
219 /* full-screen popup windows are managed */
220 hmon = MonitorFromWindow( hwnd, MONITOR_DEFAULTTOPRIMARY );
221 mi.cbSize = sizeof( mi );
222 GetMonitorInfoW( hmon, &mi );
223 if (window_rect->left <= mi.rcWork.left && window_rect->right >= mi.rcWork.right &&
224 window_rect->top <= mi.rcWork.top && window_rect->bottom >= mi.rcWork.bottom)
227 /* application windows are managed */
228 ex_style = GetWindowLongW( hwnd, GWL_EXSTYLE );
229 if (ex_style & WS_EX_APPWINDOW) return TRUE;
230 /* windows that own popups are managed */
231 if (has_owned_popups( hwnd )) return TRUE;
232 /* default: not managed */
237 /***********************************************************************
238 * is_window_resizable
240 * Check if window should be made resizable by the window manager
242 static inline BOOL is_window_resizable( struct x11drv_win_data *data, DWORD style )
244 if (style & WS_THICKFRAME) return TRUE;
245 /* Metacity needs the window to be resizable to make it fullscreen */
246 return is_window_rect_fullscreen( &data->whole_rect );
250 /***********************************************************************
251 * get_mwm_decorations
253 static unsigned long get_mwm_decorations( struct x11drv_win_data *data,
254 DWORD style, DWORD ex_style )
256 unsigned long ret = 0;
258 if (!decorated_mode) return 0;
260 if (IsRectEmpty( &data->window_rect )) return 0;
261 if (data->shaped) return 0;
263 if (ex_style & WS_EX_TOOLWINDOW) return 0;
265 if ((style & WS_CAPTION) == WS_CAPTION)
267 ret |= MWM_DECOR_TITLE | MWM_DECOR_BORDER;
268 if (style & WS_SYSMENU) ret |= MWM_DECOR_MENU;
269 if (style & WS_MINIMIZEBOX) ret |= MWM_DECOR_MINIMIZE;
270 if (style & WS_MAXIMIZEBOX) ret |= MWM_DECOR_MAXIMIZE;
272 if (ex_style & WS_EX_DLGMODALFRAME) ret |= MWM_DECOR_BORDER;
273 else if (style & WS_THICKFRAME) ret |= MWM_DECOR_BORDER | MWM_DECOR_RESIZEH;
274 else if ((style & (WS_DLGFRAME|WS_BORDER)) == WS_DLGFRAME) ret |= MWM_DECOR_BORDER;
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;
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 = 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 /***********************************************************************
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 /***********************************************************************
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 );
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 );
381 XShapeCombineMask( data->display, data->whole_window, ShapeBounding, 0, 0, None, ShapeSet );
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);
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) );
417 XChangeProperty( display, win, x11drv_atom(_NET_WM_WINDOW_OPACITY),
418 XA_CARDINAL, 32, PropModeReplace, (unsigned char *)&opacity, 1 );
422 /***********************************************************************
425 static void sync_window_text( Display *display, Window win, const WCHAR *text )
428 char *buffer, *utf8_buffer;
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 );
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 );
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 /***********************************************************************
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;
473 unsigned int *ptr, *bits = NULL;
474 unsigned char *mask_bits = NULL;
475 int i, j, has_alpha = 0;
477 if (!GetObjectW( color, sizeof(bm), &bm )) return NULL;
478 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
479 info->bmiHeader.biWidth = bm.bmWidth;
480 info->bmiHeader.biHeight = -bm.bmHeight;
481 info->bmiHeader.biPlanes = 1;
482 info->bmiHeader.biBitCount = 32;
483 info->bmiHeader.biCompression = BI_RGB;
484 info->bmiHeader.biSizeImage = bm.bmWidth * bm.bmHeight * 4;
485 info->bmiHeader.biXPelsPerMeter = 0;
486 info->bmiHeader.biYPelsPerMeter = 0;
487 info->bmiHeader.biClrUsed = 0;
488 info->bmiHeader.biClrImportant = 0;
489 *size = bm.bmWidth * bm.bmHeight + 2;
490 if (!(bits = HeapAlloc( GetProcessHeap(), 0, *size * sizeof(long) ))) goto failed;
491 if (!GetDIBits( hdc, color, 0, bm.bmHeight, bits + 2, info, DIB_RGB_COLORS )) goto failed;
493 bits[0] = bm.bmWidth;
494 bits[1] = bm.bmHeight;
496 for (i = 0; i < bm.bmWidth * bm.bmHeight; i++)
497 if ((has_alpha = (bits[i + 2] & 0xff000000) != 0)) break;
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;
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;
521 HeapFree( GetProcessHeap(), 0, bits );
522 HeapFree( GetProcessHeap(), 0, mask_bits );
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;
543 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
544 info->bmiHeader.biBitCount = 0;
545 if (!(lines = GetDIBits( hdc, icon->hbmColor, 0, 0, NULL, info, DIB_RGB_COLORS ))) goto failed;
546 if (!(bits.ptr = HeapAlloc( GetProcessHeap(), 0, info->bmiHeader.biSizeImage ))) goto failed;
547 if (!GetDIBits( hdc, icon->hbmColor, 0, lines, bits.ptr, info, DIB_RGB_COLORS )) goto failed;
549 color_pixmap = create_pixmap_from_image( hdc, &vis, info, &bits, DIB_RGB_COLORS );
550 HeapFree( GetProcessHeap(), 0, bits.ptr );
552 if (!color_pixmap) goto failed;
554 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
555 info->bmiHeader.biBitCount = 0;
556 if (!(lines = GetDIBits( hdc, icon->hbmMask, 0, 0, NULL, info, DIB_RGB_COLORS ))) goto failed;
557 if (!(bits.ptr = HeapAlloc( GetProcessHeap(), 0, info->bmiHeader.biSizeImage ))) goto failed;
558 if (!GetDIBits( hdc, icon->hbmMask, 0, lines, bits.ptr, info, DIB_RGB_COLORS )) goto failed;
560 /* invert the mask */
561 for (i = 0; i < info->bmiHeader.biSizeImage / sizeof(DWORD); i++) ((DWORD *)bits.ptr)[i] ^= ~0u;
564 mask_pixmap = create_pixmap_from_image( hdc, &vis, info, &bits, DIB_RGB_COLORS );
565 HeapFree( GetProcessHeap(), 0, bits.ptr );
567 if (!mask_pixmap) goto failed;
569 *icon_ret = color_pixmap;
570 *mask_ret = mask_pixmap;
574 if (color_pixmap) XFreePixmap( gdi_display, color_pixmap );
575 if (mask_pixmap) XFreePixmap( gdi_display, mask_pixmap );
576 HeapFree( GetProcessHeap(), 0, bits.ptr );
581 /***********************************************************************
584 static void fetch_icon_data( HWND hwnd, HICON icon_big, HICON icon_small )
586 struct x11drv_win_data *data;
587 ICONINFO ii, ii_small;
591 Pixmap icon_pixmap, mask_pixmap;
595 icon_big = (HICON)SendMessageW( hwnd, WM_GETICON, ICON_BIG, 0 );
596 if (!icon_big) icon_big = (HICON)GetClassLongPtrW( hwnd, GCLP_HICON );
597 if (!icon_big) icon_big = LoadIconW( 0, (LPWSTR)IDI_WINLOGO );
601 icon_small = (HICON)SendMessageW( hwnd, WM_GETICON, ICON_SMALL, 0 );
602 if (!icon_small) icon_small = (HICON)GetClassLongPtrW( hwnd, GCLP_HICONSM );
605 if (!GetIconInfo(icon_big, &ii)) return;
607 hDC = CreateCompatibleDC(0);
608 bits = get_bitmap_argb( hDC, ii.hbmColor, ii.hbmMask, &size );
609 if (bits && GetIconInfo( icon_small, &ii_small ))
611 unsigned int size_small;
612 unsigned long *bits_small, *new;
614 if ((bits_small = get_bitmap_argb( hDC, ii_small.hbmColor, ii_small.hbmMask, &size_small )) &&
615 (bits_small[0] != bits[0] || bits_small[1] != bits[1])) /* size must be different */
617 if ((new = HeapReAlloc( GetProcessHeap(), 0, bits,
618 (size + size_small) * sizeof(unsigned long) )))
621 memcpy( bits + size, bits_small, size_small * sizeof(unsigned long) );
625 HeapFree( GetProcessHeap(), 0, bits_small );
626 DeleteObject( ii_small.hbmColor );
627 DeleteObject( ii_small.hbmMask );
630 if (!create_icon_pixmaps( hDC, &ii, &icon_pixmap, &mask_pixmap )) icon_pixmap = mask_pixmap = 0;
632 DeleteObject( ii.hbmColor );
633 DeleteObject( ii.hbmMask );
636 if ((data = get_win_data( hwnd )))
638 if (data->icon_pixmap) XFreePixmap( gdi_display, data->icon_pixmap );
639 if (data->icon_mask) XFreePixmap( gdi_display, data->icon_mask );
640 HeapFree( GetProcessHeap(), 0, data->icon_bits );
641 data->icon_pixmap = icon_pixmap;
642 data->icon_mask = mask_pixmap;
643 data->icon_bits = bits;
644 data->icon_size = size;
645 release_win_data( data );
649 if (icon_pixmap) XFreePixmap( gdi_display, icon_pixmap );
650 if (mask_pixmap) XFreePixmap( gdi_display, mask_pixmap );
651 HeapFree( GetProcessHeap(), 0, bits );
656 /***********************************************************************
659 * set the window size hints
661 static void set_size_hints( struct x11drv_win_data *data, DWORD style )
663 XSizeHints* size_hints;
665 if (!(size_hints = XAllocSizeHints())) return;
667 size_hints->win_gravity = StaticGravity;
668 size_hints->flags |= PWinGravity;
670 /* don't update size hints if window is not in normal state */
671 if (!(style & (WS_MINIMIZE | WS_MAXIMIZE)))
673 if (data->hwnd != GetDesktopWindow()) /* don't force position of desktop */
675 size_hints->x = data->whole_rect.left;
676 size_hints->y = data->whole_rect.top;
677 size_hints->flags |= PPosition;
679 else size_hints->win_gravity = NorthWestGravity;
681 if (!is_window_resizable( data, style ))
683 size_hints->max_width = data->whole_rect.right - data->whole_rect.left;
684 size_hints->max_height = data->whole_rect.bottom - data->whole_rect.top;
685 if (size_hints->max_width <= 0 ||size_hints->max_height <= 0)
686 size_hints->max_width = size_hints->max_height = 1;
687 size_hints->min_width = size_hints->max_width;
688 size_hints->min_height = size_hints->max_height;
689 size_hints->flags |= PMinSize | PMaxSize;
692 XSetWMNormalHints( data->display, data->whole_window, size_hints );
697 /***********************************************************************
700 static void set_mwm_hints( struct x11drv_win_data *data, DWORD style, DWORD ex_style )
704 if (data->hwnd == GetDesktopWindow())
706 if (is_desktop_fullscreen()) mwm_hints.decorations = 0;
707 else mwm_hints.decorations = MWM_DECOR_TITLE | MWM_DECOR_BORDER | MWM_DECOR_MENU | MWM_DECOR_MINIMIZE;
708 mwm_hints.functions = MWM_FUNC_MOVE | MWM_FUNC_MINIMIZE | MWM_FUNC_CLOSE;
712 mwm_hints.decorations = get_mwm_decorations( data, style, ex_style );
713 mwm_hints.functions = MWM_FUNC_MOVE;
714 if (is_window_resizable( data, style )) mwm_hints.functions |= MWM_FUNC_RESIZE;
715 if (!(style & WS_DISABLED))
717 if (style & WS_MINIMIZEBOX) mwm_hints.functions |= MWM_FUNC_MINIMIZE;
718 if (style & WS_MAXIMIZEBOX) mwm_hints.functions |= MWM_FUNC_MAXIMIZE;
719 if (style & WS_SYSMENU) mwm_hints.functions |= MWM_FUNC_CLOSE;
723 TRACE( "%p setting mwm hints to %lx,%lx (style %x exstyle %x)\n",
724 data->hwnd, mwm_hints.decorations, mwm_hints.functions, style, ex_style );
726 mwm_hints.flags = MWM_HINTS_FUNCTIONS | MWM_HINTS_DECORATIONS;
727 XChangeProperty( data->display, data->whole_window, x11drv_atom(_MOTIF_WM_HINTS),
728 x11drv_atom(_MOTIF_WM_HINTS), 32, PropModeReplace,
729 (unsigned char*)&mwm_hints, sizeof(mwm_hints)/sizeof(long) );
733 /***********************************************************************
736 static void set_style_hints( struct x11drv_win_data *data, DWORD style, DWORD ex_style )
738 Window group_leader = data->whole_window;
739 HWND owner = GetWindow( data->hwnd, GW_OWNER );
740 Window owner_win = X11DRV_get_whole_window( owner );
746 XSetTransientForHint( data->display, data->whole_window, owner_win );
747 group_leader = owner_win;
750 /* Only use dialog type for owned popups. Metacity allows making fullscreen
751 * only normal windows, and doesn't handle correctly TRANSIENT_FOR hint for
752 * dialogs owned by fullscreen windows.
754 if (((style & WS_POPUP) || (ex_style & WS_EX_DLGMODALFRAME)) && owner)
755 window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_DIALOG);
757 window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_NORMAL);
759 XChangeProperty(data->display, data->whole_window, x11drv_atom(_NET_WM_WINDOW_TYPE),
760 XA_ATOM, 32, PropModeReplace, (unsigned char*)&window_type, 1);
762 if ((wm_hints = XAllocWMHints()))
764 wm_hints->flags = InputHint | StateHint | WindowGroupHint;
765 wm_hints->input = !use_take_focus && !(style & WS_DISABLED);
766 wm_hints->initial_state = (style & WS_MINIMIZE) ? IconicState : NormalState;
767 wm_hints->window_group = group_leader;
768 if (data->icon_pixmap)
770 wm_hints->icon_pixmap = data->icon_pixmap;
771 wm_hints->icon_mask = data->icon_mask;
772 wm_hints->flags |= IconPixmapHint | IconMaskHint;
774 XSetWMHints( data->display, data->whole_window, wm_hints );
779 XChangeProperty( data->display, data->whole_window, x11drv_atom(_NET_WM_ICON),
780 XA_CARDINAL, 32, PropModeReplace,
781 (unsigned char *)data->icon_bits, data->icon_size );
783 XDeleteProperty( data->display, data->whole_window, x11drv_atom(_NET_WM_ICON) );
788 /***********************************************************************
791 * get the name of the current process for setting class hints
793 static char *get_process_name(void)
799 WCHAR module[MAX_PATH];
800 DWORD len = GetModuleFileNameW( 0, module, MAX_PATH );
801 if (len && len < MAX_PATH)
804 WCHAR *p, *appname = module;
806 if ((p = strrchrW( appname, '/' ))) appname = p + 1;
807 if ((p = strrchrW( appname, '\\' ))) appname = p + 1;
808 len = WideCharToMultiByte( CP_UNIXCP, 0, appname, -1, NULL, 0, NULL, NULL );
809 if ((ptr = HeapAlloc( GetProcessHeap(), 0, len )))
811 WideCharToMultiByte( CP_UNIXCP, 0, appname, -1, ptr, len, NULL, NULL );
820 /***********************************************************************
821 * set_initial_wm_hints
823 * Set the window manager hints that don't change over the lifetime of a window.
825 static void set_initial_wm_hints( Display *display, Window window )
829 Atom dndVersion = WINE_XDND_VERSION;
830 XClassHint *class_hints;
831 char *process_name = get_process_name();
835 protocols[i++] = x11drv_atom(WM_DELETE_WINDOW);
836 protocols[i++] = x11drv_atom(_NET_WM_PING);
837 if (use_take_focus) protocols[i++] = x11drv_atom(WM_TAKE_FOCUS);
838 XChangeProperty( display, window, x11drv_atom(WM_PROTOCOLS),
839 XA_ATOM, 32, PropModeReplace, (unsigned char *)protocols, i );
842 if ((class_hints = XAllocClassHint()))
844 static char wine[] = "Wine";
846 class_hints->res_name = process_name;
847 class_hints->res_class = wine;
848 XSetClassHint( display, window, class_hints );
849 XFree( class_hints );
852 /* set the WM_CLIENT_MACHINE and WM_LOCALE_NAME properties */
853 XSetWMProperties(display, window, NULL, NULL, NULL, 0, NULL, NULL, NULL);
854 /* set the pid. together, these properties are needed so the window manager can kill us if we freeze */
856 XChangeProperty(display, window, x11drv_atom(_NET_WM_PID),
857 XA_CARDINAL, 32, PropModeReplace, (unsigned char *)&i, 1);
859 XChangeProperty( display, window, x11drv_atom(XdndAware),
860 XA_ATOM, 32, PropModeReplace, (unsigned char*)&dndVersion, 1 );
862 update_user_time( 0 ); /* make sure that the user time window exists */
863 if (user_time_window)
864 XChangeProperty( display, window, x11drv_atom(_NET_WM_USER_TIME_WINDOW),
865 XA_WINDOW, 32, PropModeReplace, (unsigned char *)&user_time_window, 1 );
869 /***********************************************************************
872 * If the window is managed, make sure its owner window is too.
874 static void make_owner_managed( HWND hwnd )
878 if (!(owner = GetWindow( hwnd, GW_OWNER ))) return;
879 if (is_managed( owner )) return;
880 if (!is_managed( hwnd )) return;
882 SetWindowPos( owner, 0, 0, 0, 0, 0,
883 SWP_NOACTIVATE | SWP_NOZORDER | SWP_NOSIZE | SWP_NOMOVE |
884 SWP_NOREDRAW | SWP_DEFERERASE | SWP_NOSENDCHANGING | SWP_STATECHANGED );
888 /***********************************************************************
891 * Set all the window manager hints for a window.
893 static void set_wm_hints( struct x11drv_win_data *data )
895 DWORD style, ex_style;
897 if (data->hwnd == GetDesktopWindow())
899 /* force some styles for the desktop to get the correct decorations */
900 style = WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX;
901 ex_style = WS_EX_APPWINDOW;
905 style = GetWindowLongW( data->hwnd, GWL_STYLE );
906 ex_style = GetWindowLongW( data->hwnd, GWL_EXSTYLE );
909 set_size_hints( data, style );
910 set_mwm_hints( data, style, ex_style );
911 set_style_hints( data, style, ex_style );
915 /***********************************************************************
918 Window init_clip_window(void)
920 struct x11drv_thread_data *data = x11drv_init_thread_data();
922 if (!data->clip_window &&
923 (data->clip_window = (Window)GetPropA( GetDesktopWindow(), clip_window_prop )))
925 XSelectInput( data->display, data->clip_window, StructureNotifyMask );
927 return data->clip_window;
931 /***********************************************************************
934 void update_user_time( Time time )
936 if (!user_time_window)
938 Window win = XCreateWindow( gdi_display, root_window, -1, -1, 1, 1, 0, CopyFromParent,
939 InputOnly, CopyFromParent, 0, NULL );
940 if (InterlockedCompareExchangePointer( (void **)&user_time_window, (void *)win, 0 ))
941 XDestroyWindow( gdi_display, win );
942 TRACE( "user time window %lx\n", user_time_window );
946 XLockDisplay( gdi_display );
947 if (!last_user_time || (long)(time - last_user_time) > 0)
949 last_user_time = time;
950 XChangeProperty( gdi_display, user_time_window, x11drv_atom(_NET_WM_USER_TIME),
951 XA_CARDINAL, 32, PropModeReplace, (unsigned char *)&time, 1 );
953 XUnlockDisplay( gdi_display );
956 /***********************************************************************
957 * update_net_wm_states
959 void update_net_wm_states( struct x11drv_win_data *data )
961 static const unsigned int state_atoms[NB_NET_WM_STATES] =
963 XATOM__NET_WM_STATE_FULLSCREEN,
964 XATOM__NET_WM_STATE_ABOVE,
965 XATOM__NET_WM_STATE_MAXIMIZED_VERT,
966 XATOM__NET_WM_STATE_SKIP_PAGER,
967 XATOM__NET_WM_STATE_SKIP_TASKBAR
970 DWORD i, style, ex_style, new_state = 0;
972 if (!data->managed) return;
973 if (data->whole_window == root_window) return;
975 style = GetWindowLongW( data->hwnd, GWL_STYLE );
976 if (is_window_rect_fullscreen( &data->whole_rect ))
978 if ((style & WS_MAXIMIZE) && (style & WS_CAPTION) == WS_CAPTION)
979 new_state |= (1 << NET_WM_STATE_MAXIMIZED);
980 else if (!(style & WS_MINIMIZE))
981 new_state |= (1 << NET_WM_STATE_FULLSCREEN);
983 else if (style & WS_MAXIMIZE)
984 new_state |= (1 << NET_WM_STATE_MAXIMIZED);
986 ex_style = GetWindowLongW( data->hwnd, GWL_EXSTYLE );
987 if (ex_style & WS_EX_TOPMOST)
988 new_state |= (1 << NET_WM_STATE_ABOVE);
989 if (ex_style & (WS_EX_TOOLWINDOW | WS_EX_NOACTIVATE))
990 new_state |= (1 << NET_WM_STATE_SKIP_TASKBAR) | (1 << NET_WM_STATE_SKIP_PAGER);
991 if (!(ex_style & WS_EX_APPWINDOW) && GetWindow( data->hwnd, GW_OWNER ))
992 new_state |= (1 << NET_WM_STATE_SKIP_TASKBAR);
994 if (!data->mapped) /* set the _NET_WM_STATE atom directly */
996 Atom atoms[NB_NET_WM_STATES+1];
999 for (i = count = 0; i < NB_NET_WM_STATES; i++)
1001 if (!(new_state & (1 << i))) continue;
1002 TRACE( "setting wm state %u for unmapped window %p/%lx\n",
1003 i, data->hwnd, data->whole_window );
1004 atoms[count++] = X11DRV_Atoms[state_atoms[i] - FIRST_XATOM];
1005 if (state_atoms[i] == XATOM__NET_WM_STATE_MAXIMIZED_VERT)
1006 atoms[count++] = x11drv_atom(_NET_WM_STATE_MAXIMIZED_HORZ);
1008 XChangeProperty( data->display, data->whole_window, x11drv_atom(_NET_WM_STATE), XA_ATOM,
1009 32, PropModeReplace, (unsigned char *)atoms, count );
1011 else /* ask the window manager to do it for us */
1015 xev.xclient.type = ClientMessage;
1016 xev.xclient.window = data->whole_window;
1017 xev.xclient.message_type = x11drv_atom(_NET_WM_STATE);
1018 xev.xclient.serial = 0;
1019 xev.xclient.display = data->display;
1020 xev.xclient.send_event = True;
1021 xev.xclient.format = 32;
1022 xev.xclient.data.l[3] = 1;
1024 for (i = 0; i < NB_NET_WM_STATES; i++)
1026 if (!((data->net_wm_state ^ new_state) & (1 << i))) continue; /* unchanged */
1028 TRACE( "setting wm state %u for window %p/%lx to %u prev %u\n",
1029 i, data->hwnd, data->whole_window,
1030 (new_state & (1 << i)) != 0, (data->net_wm_state & (1 << i)) != 0 );
1032 xev.xclient.data.l[0] = (new_state & (1 << i)) ? _NET_WM_STATE_ADD : _NET_WM_STATE_REMOVE;
1033 xev.xclient.data.l[1] = X11DRV_Atoms[state_atoms[i] - FIRST_XATOM];
1034 xev.xclient.data.l[2] = ((state_atoms[i] == XATOM__NET_WM_STATE_MAXIMIZED_VERT) ?
1035 x11drv_atom(_NET_WM_STATE_MAXIMIZED_HORZ) : 0);
1036 XSendEvent( data->display, root_window, False,
1037 SubstructureRedirectMask | SubstructureNotifyMask, &xev );
1040 data->net_wm_state = new_state;
1044 /***********************************************************************
1047 static void set_xembed_flags( struct x11drv_win_data *data, unsigned long flags )
1049 unsigned long info[2];
1051 if (!data->whole_window) return;
1053 info[0] = 0; /* protocol version */
1055 XChangeProperty( data->display, data->whole_window, x11drv_atom(_XEMBED_INFO),
1056 x11drv_atom(_XEMBED_INFO), 32, PropModeReplace, (unsigned char*)info, 2 );
1060 /***********************************************************************
1063 static void map_window( HWND hwnd, DWORD new_style )
1065 struct x11drv_win_data *data;
1067 make_owner_managed( hwnd );
1068 wait_for_withdrawn_state( hwnd, TRUE );
1070 if (!(data = get_win_data( hwnd ))) return;
1072 if (data->whole_window && !data->mapped)
1074 TRACE( "win %p/%lx\n", data->hwnd, data->whole_window );
1076 remove_startup_notification( data->display, data->whole_window );
1077 set_wm_hints( data );
1079 if (!data->embedded)
1081 update_net_wm_states( data );
1082 sync_window_style( data );
1083 XMapWindow( data->display, data->whole_window );
1084 XFlush( data->display );
1086 else set_xembed_flags( data, XEMBED_MAPPED );
1088 data->mapped = TRUE;
1089 data->iconic = (new_style & WS_MINIMIZE) != 0;
1091 release_win_data( data );
1095 /***********************************************************************
1098 static void unmap_window( HWND hwnd )
1100 struct x11drv_win_data *data;
1102 wait_for_withdrawn_state( hwnd, FALSE );
1104 if (!(data = get_win_data( hwnd ))) return;
1108 TRACE( "win %p/%lx\n", data->hwnd, data->whole_window );
1110 if (data->embedded) set_xembed_flags( data, 0 );
1111 else if (!data->managed) XUnmapWindow( data->display, data->whole_window );
1112 else XWithdrawWindow( data->display, data->whole_window, DefaultScreen(data->display) );
1114 data->mapped = FALSE;
1115 data->net_wm_state = 0;
1117 release_win_data( data );
1121 /***********************************************************************
1122 * make_window_embedded
1124 void make_window_embedded( HWND hwnd )
1126 struct x11drv_win_data *data = get_win_data( hwnd );
1130 /* the window cannot be mapped before being embedded */
1133 if (data->managed) XUnmapWindow( data->display, data->whole_window );
1134 else XWithdrawWindow( data->display, data->whole_window, DefaultScreen(data->display) );
1135 data->net_wm_state = 0;
1137 data->embedded = TRUE;
1138 data->managed = TRUE;
1139 SetPropA( hwnd, managed_prop, (HANDLE)1 );
1140 sync_window_style( data );
1141 set_xembed_flags( data, data->mapped ? XEMBED_MAPPED : 0 );
1142 release_win_data( data );
1146 /***********************************************************************
1147 * X11DRV_window_to_X_rect
1149 * Convert a rect from client to X window coordinates
1151 static void X11DRV_window_to_X_rect( struct x11drv_win_data *data, RECT *rect )
1155 if (!data->managed) return;
1156 if (IsRectEmpty( rect )) return;
1158 get_x11_rect_offset( data, &rc );
1160 rect->left -= rc.left;
1161 rect->right -= rc.right;
1162 rect->top -= rc.top;
1163 rect->bottom -= rc.bottom;
1164 if (rect->top >= rect->bottom) rect->bottom = rect->top + 1;
1165 if (rect->left >= rect->right) rect->right = rect->left + 1;
1169 /***********************************************************************
1170 * X11DRV_X_to_window_rect
1172 * Opposite of X11DRV_window_to_X_rect
1174 void X11DRV_X_to_window_rect( struct x11drv_win_data *data, RECT *rect )
1178 if (!data->managed) return;
1179 if (IsRectEmpty( rect )) return;
1181 get_x11_rect_offset( data, &rc );
1183 rect->left += rc.left;
1184 rect->right += rc.right;
1185 rect->top += rc.top;
1186 rect->bottom += rc.bottom;
1187 if (rect->top >= rect->bottom) rect->bottom = rect->top + 1;
1188 if (rect->left >= rect->right) rect->right = rect->left + 1;
1192 /***********************************************************************
1193 * sync_window_position
1195 * Synchronize the X window position with the Windows one
1197 static void sync_window_position( struct x11drv_win_data *data,
1198 UINT swp_flags, const RECT *old_window_rect,
1199 const RECT *old_whole_rect, const RECT *old_client_rect )
1201 DWORD style = GetWindowLongW( data->hwnd, GWL_STYLE );
1202 DWORD ex_style = GetWindowLongW( data->hwnd, GWL_EXSTYLE );
1203 XWindowChanges changes;
1204 unsigned int mask = 0;
1206 if (data->managed && data->iconic) return;
1208 /* resizing a managed maximized window is not allowed */
1209 if (!(style & WS_MAXIMIZE) || !data->managed)
1211 changes.width = data->whole_rect.right - data->whole_rect.left;
1212 changes.height = data->whole_rect.bottom - data->whole_rect.top;
1213 /* if window rect is empty force size to 1x1 */
1214 if (changes.width <= 0 || changes.height <= 0) changes.width = changes.height = 1;
1215 if (changes.width > 65535) changes.width = 65535;
1216 if (changes.height > 65535) changes.height = 65535;
1217 mask |= CWWidth | CWHeight;
1220 /* only the size is allowed to change for the desktop window */
1221 if (data->whole_window != root_window)
1223 changes.x = data->whole_rect.left - virtual_screen_rect.left;
1224 changes.y = data->whole_rect.top - virtual_screen_rect.top;
1228 if (!(swp_flags & SWP_NOZORDER) || (swp_flags & SWP_SHOWWINDOW))
1230 /* find window that this one must be after */
1231 HWND prev = GetWindow( data->hwnd, GW_HWNDPREV );
1232 while (prev && !(GetWindowLongW( prev, GWL_STYLE ) & WS_VISIBLE))
1233 prev = GetWindow( prev, GW_HWNDPREV );
1234 if (!prev) /* top child */
1236 changes.stack_mode = Above;
1237 mask |= CWStackMode;
1239 /* should use stack_mode Below but most window managers don't get it right */
1240 /* and Above with a sibling doesn't work so well either, so we ignore it */
1243 set_size_hints( data, style );
1244 set_mwm_hints( data, style, ex_style );
1245 data->configure_serial = NextRequest( data->display );
1246 XReconfigureWMWindow( data->display, data->whole_window,
1247 DefaultScreen(data->display), mask, &changes );
1248 #ifdef HAVE_LIBXSHAPE
1249 if (IsRectEmpty( old_window_rect ) != IsRectEmpty( &data->window_rect ))
1250 sync_window_region( data, (HRGN)1 );
1253 int old_x_offset = old_window_rect->left - old_whole_rect->left;
1254 int old_y_offset = old_window_rect->top - old_whole_rect->top;
1255 int new_x_offset = data->window_rect.left - data->whole_rect.left;
1256 int new_y_offset = data->window_rect.top - data->whole_rect.top;
1257 if (old_x_offset != new_x_offset || old_y_offset != new_y_offset)
1258 XShapeOffsetShape( data->display, data->whole_window, ShapeBounding,
1259 new_x_offset - old_x_offset, new_y_offset - old_y_offset );
1263 TRACE( "win %p/%lx pos %d,%d,%dx%d after %lx changes=%x serial=%lu\n",
1264 data->hwnd, data->whole_window, data->whole_rect.left, data->whole_rect.top,
1265 data->whole_rect.right - data->whole_rect.left,
1266 data->whole_rect.bottom - data->whole_rect.top,
1267 changes.sibling, mask, data->configure_serial );
1271 /***********************************************************************
1274 * Move the window bits when a window is moved.
1276 static void move_window_bits( HWND hwnd, Window window, const RECT *old_rect, const RECT *new_rect,
1277 const RECT *old_client_rect, const RECT *new_client_rect,
1278 const RECT *new_window_rect )
1280 RECT src_rect = *old_rect;
1281 RECT dst_rect = *new_rect;
1282 HDC hdc_src, hdc_dst;
1289 OffsetRect( &dst_rect, -new_window_rect->left, -new_window_rect->top );
1290 parent = GetAncestor( hwnd, GA_PARENT );
1291 hdc_src = GetDCEx( parent, 0, DCX_CACHE );
1292 hdc_dst = GetDCEx( hwnd, 0, DCX_CACHE | DCX_WINDOW );
1296 OffsetRect( &dst_rect, -new_client_rect->left, -new_client_rect->top );
1297 /* make src rect relative to the old position of the window */
1298 OffsetRect( &src_rect, -old_client_rect->left, -old_client_rect->top );
1299 if (dst_rect.left == src_rect.left && dst_rect.top == src_rect.top) return;
1300 hdc_src = hdc_dst = GetDCEx( hwnd, 0, DCX_CACHE );
1303 rgn = CreateRectRgnIndirect( &dst_rect );
1304 SelectClipRgn( hdc_dst, rgn );
1305 DeleteObject( rgn );
1306 ExcludeUpdateRgn( hdc_dst, hwnd );
1308 code = X11DRV_START_EXPOSURES;
1309 ExtEscape( hdc_dst, X11DRV_ESCAPE, sizeof(code), (LPSTR)&code, 0, NULL );
1311 TRACE( "copying bits for win %p/%lx %s -> %s\n",
1312 hwnd, window, wine_dbgstr_rect(&src_rect), wine_dbgstr_rect(&dst_rect) );
1313 BitBlt( hdc_dst, dst_rect.left, dst_rect.top,
1314 dst_rect.right - dst_rect.left, dst_rect.bottom - dst_rect.top,
1315 hdc_src, src_rect.left, src_rect.top, SRCCOPY );
1318 code = X11DRV_END_EXPOSURES;
1319 ExtEscape( hdc_dst, X11DRV_ESCAPE, sizeof(code), (LPSTR)&code, sizeof(rgn), (LPSTR)&rgn );
1321 ReleaseDC( hwnd, hdc_dst );
1322 if (hdc_src != hdc_dst) ReleaseDC( parent, hdc_src );
1328 /* map region to client rect since we are using DCX_WINDOW */
1329 OffsetRgn( rgn, new_window_rect->left - new_client_rect->left,
1330 new_window_rect->top - new_client_rect->top );
1331 RedrawWindow( hwnd, NULL, rgn,
1332 RDW_INVALIDATE | RDW_FRAME | RDW_ERASE | RDW_ALLCHILDREN );
1334 else RedrawWindow( hwnd, NULL, rgn, RDW_INVALIDATE | RDW_ERASE | RDW_ALLCHILDREN );
1335 DeleteObject( rgn );
1340 /**********************************************************************
1341 * create_whole_window
1343 * Create the whole X window for a given window
1345 static void create_whole_window( struct x11drv_win_data *data )
1348 XSetWindowAttributes attr;
1352 DWORD layered_flags;
1355 if (!data->managed && is_window_managed( data->hwnd, SWP_NOACTIVATE, &data->window_rect ))
1357 TRACE( "making win %p/%lx managed\n", data->hwnd, data->whole_window );
1358 data->managed = TRUE;
1359 SetPropA( data->hwnd, managed_prop, (HANDLE)1 );
1362 if ((win_rgn = CreateRectRgn( 0, 0, 0, 0 )) && GetWindowRgn( data->hwnd, win_rgn ) == ERROR)
1364 DeleteObject( win_rgn );
1367 data->shaped = (win_rgn != 0);
1369 mask = get_window_attributes( data, &attr );
1371 data->whole_rect = data->window_rect;
1372 X11DRV_window_to_X_rect( data, &data->whole_rect );
1373 if (!(cx = data->whole_rect.right - data->whole_rect.left)) cx = 1;
1374 else if (cx > 65535) cx = 65535;
1375 if (!(cy = data->whole_rect.bottom - data->whole_rect.top)) cy = 1;
1376 else if (cy > 65535) cy = 65535;
1378 data->whole_window = XCreateWindow( data->display, root_window,
1379 data->whole_rect.left - virtual_screen_rect.left,
1380 data->whole_rect.top - virtual_screen_rect.top,
1381 cx, cy, 0, default_visual.depth, InputOutput,
1382 default_visual.visual, mask, &attr );
1383 if (!data->whole_window) goto done;
1385 set_initial_wm_hints( data->display, data->whole_window );
1386 set_wm_hints( data );
1388 XSaveContext( data->display, data->whole_window, winContext, (char *)data->hwnd );
1389 SetPropA( data->hwnd, whole_window_prop, (HANDLE)data->whole_window );
1391 /* set the window text */
1392 if (!InternalGetWindowText( data->hwnd, text, sizeof(text)/sizeof(WCHAR) )) text[0] = 0;
1393 sync_window_text( data->display, data->whole_window, text );
1395 /* set the window region */
1396 if (win_rgn || IsRectEmpty( &data->window_rect )) sync_window_region( data, win_rgn );
1398 /* set the window opacity */
1399 if (!GetLayeredWindowAttributes( data->hwnd, &key, &alpha, &layered_flags )) layered_flags = 0;
1400 sync_window_opacity( data->display, data->whole_window, key, alpha, layered_flags );
1402 XFlush( data->display ); /* make sure the window exists before we start painting to it */
1404 sync_window_cursor( data->whole_window );
1407 if (win_rgn) DeleteObject( win_rgn );
1411 /**********************************************************************
1412 * destroy_whole_window
1414 * Destroy the whole X window for a given window.
1416 static void destroy_whole_window( struct x11drv_win_data *data, BOOL already_destroyed )
1418 if (!data->whole_window)
1422 Window xwin = (Window)GetPropA( data->hwnd, foreign_window_prop );
1425 if (!already_destroyed) XSelectInput( data->display, xwin, 0 );
1426 XDeleteContext( data->display, xwin, winContext );
1427 RemovePropA( data->hwnd, foreign_window_prop );
1434 TRACE( "win %p xwin %lx\n", data->hwnd, data->whole_window );
1435 XDeleteContext( data->display, data->whole_window, winContext );
1436 if (!already_destroyed) XDestroyWindow( data->display, data->whole_window );
1437 data->whole_window = 0;
1438 data->wm_state = WithdrawnState;
1439 data->net_wm_state = 0;
1440 data->mapped = FALSE;
1443 XUnsetICFocus( data->xic );
1444 XDestroyIC( data->xic );
1447 /* Outlook stops processing messages after destroying a dialog, so we need an explicit flush */
1448 XFlush( data->display );
1449 if (data->surface) window_surface_release( data->surface );
1450 data->surface = NULL;
1451 RemovePropA( data->hwnd, whole_window_prop );
1455 /*****************************************************************
1456 * SetWindowText (X11DRV.@)
1458 void CDECL X11DRV_SetWindowText( HWND hwnd, LPCWSTR text )
1462 if ((win = X11DRV_get_whole_window( hwnd )) && win != DefaultRootWindow(gdi_display))
1464 Display *display = thread_init_display();
1465 sync_window_text( display, win, text );
1470 /***********************************************************************
1471 * SetWindowStyle (X11DRV.@)
1473 * Update the X state of a window to reflect a style change
1475 void CDECL X11DRV_SetWindowStyle( HWND hwnd, INT offset, STYLESTRUCT *style )
1477 struct x11drv_win_data *data;
1478 DWORD changed = style->styleNew ^ style->styleOld;
1480 if (hwnd == GetDesktopWindow()) return;
1481 if (!(data = get_win_data( hwnd ))) return;
1482 if (!data->whole_window) goto done;
1484 if (offset == GWL_STYLE && (changed & WS_DISABLED)) set_wm_hints( data );
1486 if (offset == GWL_EXSTYLE && (changed & WS_EX_LAYERED)) /* changing WS_EX_LAYERED resets attributes */
1488 sync_window_opacity( data->display, data->whole_window, 0, 0, 0 );
1489 if (data->surface) set_surface_color_key( data->surface, CLR_INVALID );
1492 release_win_data( data );
1496 /***********************************************************************
1497 * DestroyWindow (X11DRV.@)
1499 void CDECL X11DRV_DestroyWindow( HWND hwnd )
1501 struct x11drv_thread_data *thread_data = x11drv_thread_data();
1502 struct x11drv_win_data *data;
1504 if (!(data = get_win_data( hwnd ))) return;
1506 destroy_whole_window( data, FALSE );
1507 if (thread_data->last_focus == hwnd) thread_data->last_focus = 0;
1508 if (thread_data->last_xic_hwnd == hwnd) thread_data->last_xic_hwnd = 0;
1509 if (data->icon_pixmap) XFreePixmap( gdi_display, data->icon_pixmap );
1510 if (data->icon_mask) XFreePixmap( gdi_display, data->icon_mask );
1511 HeapFree( GetProcessHeap(), 0, data->icon_bits );
1512 XDeleteContext( gdi_display, (XID)hwnd, win_data_context );
1513 release_win_data( data );
1514 HeapFree( GetProcessHeap(), 0, data );
1515 destroy_gl_drawable( hwnd );
1519 /***********************************************************************
1520 * X11DRV_DestroyNotify
1522 void X11DRV_DestroyNotify( HWND hwnd, XEvent *event )
1524 struct x11drv_win_data *data;
1527 if (!(data = get_win_data( hwnd ))) return;
1528 embedded = data->embedded;
1529 if (!embedded) FIXME( "window %p/%lx destroyed from the outside\n", hwnd, data->whole_window );
1531 destroy_whole_window( data, TRUE );
1532 release_win_data( data );
1533 if (embedded) SendMessageW( hwnd, WM_CLOSE, 0, 0 );
1537 static struct x11drv_win_data *alloc_win_data( Display *display, HWND hwnd )
1539 struct x11drv_win_data *data;
1541 if ((data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*data))))
1543 data->display = display;
1545 EnterCriticalSection( &win_data_section );
1546 XSaveContext( gdi_display, (XID)hwnd, win_data_context, (char *)data );
1552 /* initialize the desktop window id in the desktop manager process */
1553 static BOOL create_desktop_win_data( Display *display, HWND hwnd )
1555 struct x11drv_win_data *data;
1557 if (!(data = alloc_win_data( display, hwnd ))) return FALSE;
1558 data->whole_window = root_window;
1559 data->managed = TRUE;
1560 SetPropA( data->hwnd, managed_prop, (HANDLE)1 );
1561 SetPropA( data->hwnd, whole_window_prop, (HANDLE)root_window );
1562 set_initial_wm_hints( display, root_window );
1563 release_win_data( data );
1567 /**********************************************************************
1568 * CreateDesktopWindow (X11DRV.@)
1570 BOOL CDECL X11DRV_CreateDesktopWindow( HWND hwnd )
1572 unsigned int width, height;
1574 /* retrieve the real size of the desktop */
1575 SERVER_START_REQ( get_window_rectangles )
1577 req->handle = wine_server_user_handle( hwnd );
1578 req->relative = COORDS_CLIENT;
1579 wine_server_call( req );
1580 width = reply->window.right;
1581 height = reply->window.bottom;
1585 if (!width && !height) /* not initialized yet */
1587 SERVER_START_REQ( set_window_pos )
1589 req->handle = wine_server_user_handle( hwnd );
1591 req->flags = SWP_NOZORDER;
1592 req->window.left = virtual_screen_rect.left;
1593 req->window.top = virtual_screen_rect.top;
1594 req->window.right = virtual_screen_rect.right;
1595 req->window.bottom = virtual_screen_rect.bottom;
1596 req->client = req->window;
1597 wine_server_call( req );
1603 Window win = (Window)GetPropA( hwnd, whole_window_prop );
1604 if (win && win != root_window) X11DRV_init_desktop( win, width, height );
1610 /**********************************************************************
1611 * CreateWindow (X11DRV.@)
1613 BOOL CDECL X11DRV_CreateWindow( HWND hwnd )
1615 if (hwnd == GetDesktopWindow())
1617 struct x11drv_thread_data *data = x11drv_init_thread_data();
1618 XSetWindowAttributes attr;
1620 if (root_window != DefaultRootWindow( gdi_display ))
1622 /* the desktop win data can't be created lazily */
1623 if (!create_desktop_win_data( data->display, hwnd )) return FALSE;
1626 /* create the cursor clipping window */
1627 attr.override_redirect = TRUE;
1628 attr.event_mask = StructureNotifyMask | FocusChangeMask;
1629 data->clip_window = XCreateWindow( data->display, root_window, 0, 0, 1, 1, 0, 0,
1630 InputOnly, default_visual.visual,
1631 CWOverrideRedirect | CWEventMask, &attr );
1632 XFlush( data->display );
1633 SetPropA( hwnd, clip_window_prop, (HANDLE)data->clip_window );
1639 /***********************************************************************
1642 * Lock and return the X11 data structure associated with a window.
1644 struct x11drv_win_data *get_win_data( HWND hwnd )
1648 if (!hwnd) return NULL;
1649 EnterCriticalSection( &win_data_section );
1650 if (!XFindContext( gdi_display, (XID)hwnd, win_data_context, &data ))
1651 return (struct x11drv_win_data *)data;
1652 LeaveCriticalSection( &win_data_section );
1657 /***********************************************************************
1660 * Release the data returned by get_win_data.
1662 void release_win_data( struct x11drv_win_data *data )
1664 if (data) LeaveCriticalSection( &win_data_section );
1668 /***********************************************************************
1669 * X11DRV_create_win_data
1671 * Create an X11 data window structure for an existing window.
1673 static struct x11drv_win_data *X11DRV_create_win_data( HWND hwnd, const RECT *window_rect,
1674 const RECT *client_rect )
1677 struct x11drv_win_data *data;
1680 if (!(parent = GetAncestor( hwnd, GA_PARENT ))) return NULL; /* desktop */
1682 /* don't create win data for HWND_MESSAGE windows */
1683 if (parent != GetDesktopWindow() && !GetAncestor( parent, GA_PARENT )) return NULL;
1685 if (GetWindowThreadProcessId( hwnd, NULL ) != GetCurrentThreadId()) return NULL;
1687 display = thread_init_display();
1688 init_clip_window(); /* make sure the clip window is initialized in this thread */
1690 if (!(data = alloc_win_data( display, hwnd ))) return NULL;
1692 data->whole_rect = data->window_rect = *window_rect;
1693 data->client_rect = *client_rect;
1694 if (parent == GetDesktopWindow())
1696 create_whole_window( data );
1697 TRACE( "win %p/%lx window %s whole %s client %s\n",
1698 hwnd, data->whole_window, wine_dbgstr_rect( &data->window_rect ),
1699 wine_dbgstr_rect( &data->whole_rect ), wine_dbgstr_rect( &data->client_rect ));
1705 /* window procedure for foreign windows */
1706 static LRESULT WINAPI foreign_window_proc( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
1710 case WM_WINDOWPOSCHANGED:
1711 update_systray_balloon_position();
1713 case WM_PARENTNOTIFY:
1714 if (LOWORD(wparam) == WM_DESTROY)
1716 TRACE( "%p: got parent notify destroy for win %lx\n", hwnd, lparam );
1717 PostMessageW( hwnd, WM_CLOSE, 0, 0 ); /* so that we come back here once the child is gone */
1721 if (GetWindow( hwnd, GW_CHILD )) return 0; /* refuse to die if we still have children */
1724 return DefWindowProcW( hwnd, msg, wparam, lparam );
1728 /***********************************************************************
1729 * create_foreign_window
1731 * Create a foreign window for the specified X window and its ancestors
1733 HWND create_foreign_window( Display *display, Window xwin )
1735 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};
1736 static BOOL class_registered;
1737 struct x11drv_win_data *data;
1739 Window xparent, xroot;
1741 unsigned int nchildren;
1742 XWindowAttributes attr;
1743 DWORD style = WS_CLIPCHILDREN;
1745 if (!class_registered)
1749 memset( &class, 0, sizeof(class) );
1750 class.cbSize = sizeof(class);
1751 class.lpfnWndProc = foreign_window_proc;
1752 class.lpszClassName = classW;
1753 if (!RegisterClassExW( &class ) && GetLastError() != ERROR_CLASS_ALREADY_EXISTS)
1755 ERR( "Could not register foreign window class\n" );
1758 class_registered = TRUE;
1761 if (XFindContext( display, xwin, winContext, (char **)&hwnd )) hwnd = 0;
1762 if (hwnd) return hwnd; /* already created */
1764 XSelectInput( display, xwin, StructureNotifyMask );
1765 if (!XGetWindowAttributes( display, xwin, &attr ) ||
1766 !XQueryTree( display, xwin, &xroot, &xparent, &xchildren, &nchildren ))
1768 XSelectInput( display, xwin, 0 );
1773 if (xparent == xroot)
1775 parent = GetDesktopWindow();
1777 attr.x += virtual_screen_rect.left;
1778 attr.y += virtual_screen_rect.top;
1782 parent = create_foreign_window( display, xparent );
1786 hwnd = CreateWindowW( classW, NULL, style, attr.x, attr.y, attr.width, attr.height,
1787 parent, 0, 0, NULL );
1789 if (!(data = alloc_win_data( display, hwnd )))
1791 DestroyWindow( hwnd );
1794 SetRect( &data->window_rect, attr.x, attr.y, attr.x + attr.width, attr.y + attr.height );
1795 data->whole_rect = data->client_rect = data->window_rect;
1796 data->whole_window = 0;
1797 data->embedded = TRUE;
1798 data->mapped = TRUE;
1800 SetPropA( hwnd, foreign_window_prop, (HANDLE)xwin );
1801 XSaveContext( display, xwin, winContext, (char *)data->hwnd );
1803 ShowWindow( hwnd, SW_SHOW );
1805 TRACE( "win %lx parent %p style %08x %s -> hwnd %p\n",
1806 xwin, parent, style, wine_dbgstr_rect(&data->window_rect), hwnd );
1812 /***********************************************************************
1813 * X11DRV_get_whole_window
1815 * Return the X window associated with the full area of a window
1817 Window X11DRV_get_whole_window( HWND hwnd )
1819 struct x11drv_win_data *data = get_win_data( hwnd );
1824 if (hwnd == GetDesktopWindow()) return root_window;
1825 return (Window)GetPropA( hwnd, whole_window_prop );
1827 ret = data->whole_window;
1828 release_win_data( data );
1833 /***********************************************************************
1836 * Return the X input context associated with a window
1838 XIC X11DRV_get_ic( HWND hwnd )
1840 struct x11drv_win_data *data = get_win_data( hwnd );
1846 x11drv_thread_data()->last_xic_hwnd = hwnd;
1848 if (!ret && (xim = x11drv_thread_data()->xim)) ret = X11DRV_CreateIC( xim, data );
1849 release_win_data( data );
1855 /***********************************************************************
1856 * X11DRV_GetDC (X11DRV.@)
1858 void CDECL X11DRV_GetDC( HDC hdc, HWND hwnd, HWND top, const RECT *win_rect,
1859 const RECT *top_rect, DWORD flags )
1861 struct x11drv_escape_set_drawable escape;
1864 escape.code = X11DRV_SET_DRAWABLE;
1866 escape.mode = IncludeInferiors;
1867 escape.fbconfig_id = 0;
1869 escape.dc_rect.left = win_rect->left - top_rect->left;
1870 escape.dc_rect.top = win_rect->top - top_rect->top;
1871 escape.dc_rect.right = win_rect->right - top_rect->left;
1872 escape.dc_rect.bottom = win_rect->bottom - top_rect->top;
1876 struct x11drv_win_data *data = get_win_data( hwnd );
1878 escape.drawable = data ? data->whole_window : X11DRV_get_whole_window( hwnd );
1880 /* special case: when repainting the root window, clip out top-level windows */
1881 if (data && data->whole_window == root_window) escape.mode = ClipByChildren;
1882 release_win_data( data );
1886 /* find the first ancestor that has a drawable */
1887 for (parent = hwnd; parent && parent != top; parent = GetAncestor( parent, GA_PARENT ))
1888 if ((escape.drawable = X11DRV_get_whole_window( parent ))) break;
1890 if (escape.drawable)
1892 POINT pt = { 0, 0 };
1893 MapWindowPoints( 0, parent, &pt, 1 );
1894 escape.dc_rect = *win_rect;
1895 OffsetRect( &escape.dc_rect, pt.x, pt.y );
1896 if (flags & DCX_CLIPCHILDREN) escape.mode = ClipByChildren;
1898 else escape.drawable = X11DRV_get_whole_window( top );
1901 ExtEscape( hdc, X11DRV_ESCAPE, sizeof(escape), (LPSTR)&escape, 0, NULL );
1905 /***********************************************************************
1906 * X11DRV_ReleaseDC (X11DRV.@)
1908 void CDECL X11DRV_ReleaseDC( HWND hwnd, HDC hdc )
1910 struct x11drv_escape_set_drawable escape;
1912 escape.code = X11DRV_SET_DRAWABLE;
1913 escape.hwnd = GetDesktopWindow();
1914 escape.drawable = root_window;
1915 escape.mode = IncludeInferiors;
1916 SetRect( &escape.dc_rect, 0, 0, virtual_screen_rect.right - virtual_screen_rect.left,
1917 virtual_screen_rect.bottom - virtual_screen_rect.top );
1918 OffsetRect( &escape.dc_rect, -virtual_screen_rect.left, -virtual_screen_rect.top );
1919 escape.fbconfig_id = 0;
1920 ExtEscape( hdc, X11DRV_ESCAPE, sizeof(escape), (LPSTR)&escape, 0, NULL );
1924 /***********************************************************************
1925 * SetCapture (X11DRV.@)
1927 void CDECL X11DRV_SetCapture( HWND hwnd, UINT flags )
1929 struct x11drv_thread_data *thread_data = x11drv_thread_data();
1931 if (!thread_data) return;
1932 if (!(flags & (GUI_INMOVESIZE | GUI_INMENUMODE))) return;
1936 Window grab_win = X11DRV_get_whole_window( GetAncestor( hwnd, GA_ROOT ) );
1938 if (!grab_win) return;
1939 XFlush( gdi_display );
1940 XGrabPointer( thread_data->display, grab_win, False,
1941 PointerMotionMask | ButtonPressMask | ButtonReleaseMask,
1942 GrabModeAsync, GrabModeAsync, None, None, CurrentTime );
1943 thread_data->grab_window = grab_win;
1945 else /* release capture */
1947 XFlush( gdi_display );
1948 XUngrabPointer( thread_data->display, CurrentTime );
1949 XFlush( thread_data->display );
1950 thread_data->grab_window = None;
1955 /*****************************************************************
1956 * SetParent (X11DRV.@)
1958 void CDECL X11DRV_SetParent( HWND hwnd, HWND parent, HWND old_parent )
1960 struct x11drv_win_data *data;
1962 if (parent == old_parent) return;
1963 if (!(data = get_win_data( hwnd ))) return;
1964 if (data->embedded) goto done;
1966 if (parent != GetDesktopWindow()) /* a child window */
1968 if (old_parent == GetDesktopWindow())
1970 /* destroy the old X windows */
1971 destroy_whole_window( data, FALSE );
1974 data->managed = FALSE;
1975 RemovePropA( data->hwnd, managed_prop );
1979 else /* new top level window */
1981 create_whole_window( data );
1984 release_win_data( data );
1985 fetch_icon_data( hwnd, 0, 0 );
1989 static inline RECT get_surface_rect( const RECT *visible_rect )
1993 IntersectRect( &rect, visible_rect, &virtual_screen_rect );
1994 OffsetRect( &rect, -visible_rect->left, -visible_rect->top );
1997 rect.right = max( rect.left + 32, (rect.right + 31) & ~31 );
1998 rect.bottom = max( rect.top + 32, (rect.bottom + 31) & ~31 );
2003 /***********************************************************************
2004 * WindowPosChanging (X11DRV.@)
2006 void CDECL X11DRV_WindowPosChanging( HWND hwnd, HWND insert_after, UINT swp_flags,
2007 const RECT *window_rect, const RECT *client_rect, RECT *visible_rect,
2008 struct window_surface **surface )
2010 struct x11drv_win_data *data = get_win_data( hwnd );
2014 BOOL layered = GetWindowLongW( hwnd, GWL_EXSTYLE ) & WS_EX_LAYERED;
2016 if (!data && !(data = X11DRV_create_win_data( hwnd, window_rect, client_rect ))) return;
2018 /* check if we need to switch the window to managed */
2019 if (!data->managed && data->whole_window && is_window_managed( hwnd, swp_flags, window_rect ))
2021 TRACE( "making win %p/%lx managed\n", hwnd, data->whole_window );
2022 release_win_data( data );
2023 unmap_window( hwnd );
2024 if (!(data = get_win_data( hwnd ))) return;
2025 data->managed = TRUE;
2026 SetPropA( hwnd, managed_prop, (HANDLE)1 );
2029 *visible_rect = *window_rect;
2030 X11DRV_window_to_X_rect( data, visible_rect );
2032 /* create the window surface if necessary */
2033 if (!data->whole_window) goto done;
2034 if (data->embedded) goto done;
2035 if (swp_flags & SWP_HIDEWINDOW) goto done;
2036 if (data->whole_window == root_window) goto done;
2037 if (has_gl_drawable( hwnd )) goto done;
2038 if (!client_side_graphics && !layered) goto done;
2040 surface_rect = get_surface_rect( visible_rect );
2043 if (!memcmp( &data->surface->rect, &surface_rect, sizeof(surface_rect) ))
2045 /* existing surface is good enough */
2046 window_surface_add_ref( data->surface );
2047 *surface = data->surface;
2051 else if (!(swp_flags & SWP_SHOWWINDOW) && !(GetWindowLongW( hwnd, GWL_STYLE ) & WS_VISIBLE)) goto done;
2053 if (!layered || !GetLayeredWindowAttributes( hwnd, &key, NULL, &flags ) || !(flags & LWA_COLORKEY))
2056 *surface = create_surface( data->whole_window, &default_visual, &surface_rect, key );
2059 release_win_data( data );
2063 /***********************************************************************
2064 * WindowPosChanged (X11DRV.@)
2066 void CDECL X11DRV_WindowPosChanged( HWND hwnd, HWND insert_after, UINT swp_flags,
2067 const RECT *rectWindow, const RECT *rectClient,
2068 const RECT *visible_rect, const RECT *valid_rects,
2069 struct window_surface *surface )
2071 struct x11drv_thread_data *thread_data;
2072 struct x11drv_win_data *data;
2073 DWORD new_style = GetWindowLongW( hwnd, GWL_STYLE );
2074 RECT old_window_rect, old_whole_rect, old_client_rect;
2077 if (!(data = get_win_data( hwnd ))) return;
2079 thread_data = x11drv_thread_data();
2081 old_window_rect = data->window_rect;
2082 old_whole_rect = data->whole_rect;
2083 old_client_rect = data->client_rect;
2084 data->window_rect = *rectWindow;
2085 data->whole_rect = *visible_rect;
2086 data->client_rect = *rectClient;
2087 if (surface) window_surface_add_ref( surface );
2088 if (data->surface) window_surface_release( data->surface );
2089 data->surface = surface;
2091 TRACE( "win %p window %s client %s style %08x flags %08x\n",
2092 hwnd, wine_dbgstr_rect(rectWindow), wine_dbgstr_rect(rectClient), new_style, swp_flags );
2094 if (!IsRectEmpty( &valid_rects[0] ))
2096 Window window = data->whole_window;
2097 int x_offset = old_whole_rect.left - data->whole_rect.left;
2098 int y_offset = old_whole_rect.top - data->whole_rect.top;
2100 /* if all that happened is that the whole window moved, copy everything */
2101 if (!(swp_flags & SWP_FRAMECHANGED) &&
2102 old_whole_rect.right - data->whole_rect.right == x_offset &&
2103 old_whole_rect.bottom - data->whole_rect.bottom == y_offset &&
2104 old_client_rect.left - data->client_rect.left == x_offset &&
2105 old_client_rect.right - data->client_rect.right == x_offset &&
2106 old_client_rect.top - data->client_rect.top == y_offset &&
2107 old_client_rect.bottom - data->client_rect.bottom == y_offset &&
2108 !memcmp( &valid_rects[0], &data->client_rect, sizeof(RECT) ))
2110 /* if we have an X window the bits will be moved by the X server */
2111 if (!window && (x_offset != 0 || y_offset != 0))
2113 release_win_data( data );
2114 move_window_bits( hwnd, window, &old_whole_rect, visible_rect,
2115 &old_client_rect, rectClient, rectWindow );
2116 if (!(data = get_win_data( hwnd ))) return;
2121 release_win_data( data );
2122 move_window_bits( hwnd, window, &valid_rects[1], &valid_rects[0],
2123 &old_client_rect, rectClient, rectWindow );
2124 if (!(data = get_win_data( hwnd ))) return;
2128 XFlush( gdi_display ); /* make sure painting is done before we move the window */
2130 sync_gl_drawable( data->hwnd, visible_rect, rectClient );
2132 if (!data->whole_window) goto done;
2134 /* check if we are currently processing an event relevant to this window */
2137 thread_data->current_event &&
2138 thread_data->current_event->xany.window == data->whole_window)
2140 event_type = thread_data->current_event->type;
2141 if (event_type != ConfigureNotify && event_type != PropertyNotify &&
2142 event_type != GravityNotify && event_type != ReparentNotify)
2143 event_type = 0; /* ignore other events */
2146 if (data->mapped && event_type != ReparentNotify)
2148 if (((swp_flags & SWP_HIDEWINDOW) && !(new_style & WS_VISIBLE)) ||
2150 !is_window_rect_mapped( rectWindow ) && is_window_rect_mapped( &old_window_rect )))
2152 release_win_data( data );
2153 unmap_window( hwnd );
2154 if (is_window_rect_fullscreen( &old_window_rect )) reset_clipping_window();
2155 if (!(data = get_win_data( hwnd ))) return;
2159 /* don't change position if we are about to minimize or maximize a managed window */
2161 !(data->managed && (swp_flags & SWP_STATECHANGED) && (new_style & (WS_MINIMIZE|WS_MAXIMIZE))))
2162 sync_window_position( data, swp_flags, &old_window_rect, &old_whole_rect, &old_client_rect );
2164 if ((new_style & WS_VISIBLE) &&
2165 ((new_style & WS_MINIMIZE) || is_window_rect_mapped( rectWindow )))
2169 BOOL needs_icon = !data->icon_pixmap;
2171 release_win_data( data );
2172 if (needs_icon) fetch_icon_data( hwnd, 0, 0 );
2173 map_window( hwnd, new_style );
2176 else if ((swp_flags & SWP_STATECHANGED) && (!data->iconic != !(new_style & WS_MINIMIZE)))
2178 set_wm_hints( data );
2179 data->iconic = (new_style & WS_MINIMIZE) != 0;
2180 TRACE( "changing win %p iconic state to %u\n", data->hwnd, data->iconic );
2182 XIconifyWindow( data->display, data->whole_window, DefaultScreen(data->display) );
2183 else if (is_window_rect_mapped( rectWindow ))
2184 XMapWindow( data->display, data->whole_window );
2185 update_net_wm_states( data );
2189 if (swp_flags & (SWP_FRAMECHANGED|SWP_STATECHANGED)) set_wm_hints( data );
2190 if (!event_type) update_net_wm_states( data );
2194 XFlush( data->display ); /* make sure changes are done before we start painting again */
2196 release_win_data( data );
2200 /***********************************************************************
2201 * ShowWindow (X11DRV.@)
2203 UINT CDECL X11DRV_ShowWindow( HWND hwnd, INT cmd, RECT *rect, UINT swp )
2206 unsigned int width, height, border, depth;
2208 DWORD style = GetWindowLongW( hwnd, GWL_STYLE );
2209 struct x11drv_thread_data *thread_data = x11drv_thread_data();
2210 struct x11drv_win_data *data = get_win_data( hwnd );
2212 if (!data || !data->whole_window || !data->managed || !data->mapped || data->iconic) goto done;
2213 if (style & WS_MINIMIZE) goto done;
2214 if (IsRectEmpty( rect )) goto done;
2216 /* only fetch the new rectangle if the ShowWindow was a result of a window manager event */
2218 if (!thread_data->current_event || thread_data->current_event->xany.window != data->whole_window)
2221 if (thread_data->current_event->type != ConfigureNotify &&
2222 thread_data->current_event->type != PropertyNotify)
2225 TRACE( "win %p/%lx cmd %d at %s flags %08x\n",
2226 hwnd, data->whole_window, cmd, wine_dbgstr_rect(rect), swp );
2228 XGetGeometry( thread_data->display, data->whole_window,
2229 &root, &x, &y, &width, &height, &border, &depth );
2230 XTranslateCoordinates( thread_data->display, data->whole_window, root, 0, 0, &x, &y, &top );
2233 rect->right = x + width;
2234 rect->bottom = y + height;
2235 OffsetRect( rect, virtual_screen_rect.left, virtual_screen_rect.top );
2236 X11DRV_X_to_window_rect( data, rect );
2237 swp &= ~(SWP_NOMOVE | SWP_NOCLIENTMOVE | SWP_NOSIZE | SWP_NOCLIENTSIZE);
2240 release_win_data( data );
2245 /**********************************************************************
2246 * SetWindowIcon (X11DRV.@)
2248 * hIcon or hIconSm has changed (or is being initialised for the
2249 * first time). Complete the X11 driver-specific initialisation
2250 * and set the window hints.
2252 void CDECL X11DRV_SetWindowIcon( HWND hwnd, UINT type, HICON icon )
2254 struct x11drv_win_data *data;
2256 if (!(data = get_win_data( hwnd ))) return;
2257 if (!data->whole_window) goto done;
2258 release_win_data( data ); /* release the lock, fetching the icon requires sending messages */
2260 if (type == ICON_BIG) fetch_icon_data( hwnd, icon, 0 );
2261 else fetch_icon_data( hwnd, 0, icon );
2263 if (!(data = get_win_data( hwnd ))) return;
2264 set_wm_hints( data );
2266 release_win_data( data );
2270 /***********************************************************************
2271 * SetWindowRgn (X11DRV.@)
2273 * Assign specified region to window (for non-rectangular windows)
2275 int CDECL X11DRV_SetWindowRgn( HWND hwnd, HRGN hrgn, BOOL redraw )
2277 struct x11drv_win_data *data;
2279 if ((data = get_win_data( hwnd )))
2281 sync_window_region( data, hrgn );
2282 release_win_data( data );
2284 else if (X11DRV_get_whole_window( hwnd ))
2286 SendMessageW( hwnd, WM_X11DRV_SET_WIN_REGION, 0, 0 );
2292 /***********************************************************************
2293 * SetLayeredWindowAttributes (X11DRV.@)
2295 * Set transparency attributes for a layered window.
2297 void CDECL X11DRV_SetLayeredWindowAttributes( HWND hwnd, COLORREF key, BYTE alpha, DWORD flags )
2299 struct x11drv_win_data *data = get_win_data( hwnd );
2303 if (data->whole_window)
2304 sync_window_opacity( data->display, data->whole_window, key, alpha, flags );
2306 set_surface_color_key( data->surface, (flags & LWA_COLORKEY) ? key : CLR_INVALID );
2307 release_win_data( data );
2311 Window win = X11DRV_get_whole_window( hwnd );
2314 sync_window_opacity( gdi_display, win, key, alpha, flags );
2315 if (flags & LWA_COLORKEY)
2316 FIXME( "LWA_COLORKEY not supported on foreign process window %p\n", hwnd );
2322 /**********************************************************************
2323 * X11DRV_WindowMessage (X11DRV.@)
2325 LRESULT CDECL X11DRV_WindowMessage( HWND hwnd, UINT msg, WPARAM wp, LPARAM lp )
2327 struct x11drv_win_data *data;
2331 case WM_X11DRV_ACQUIRE_SELECTION:
2332 return X11DRV_AcquireClipboard( hwnd );
2333 case WM_X11DRV_SET_WIN_FORMAT:
2334 return set_win_format( hwnd, (XID)wp );
2335 case WM_X11DRV_SET_WIN_REGION:
2336 if ((data = get_win_data( hwnd )))
2338 sync_window_region( data, (HRGN)1 );
2339 release_win_data( data );
2342 case WM_X11DRV_RESIZE_DESKTOP:
2343 X11DRV_resize_desktop( LOWORD(lp), HIWORD(lp) );
2345 case WM_X11DRV_SET_CURSOR:
2346 if ((data = get_win_data( hwnd )))
2348 if (data->whole_window) set_window_cursor( data->whole_window, (HCURSOR)lp );
2349 release_win_data( data );
2351 else if (hwnd == x11drv_thread_data()->clip_hwnd)
2352 set_window_cursor( x11drv_thread_data()->clip_window, (HCURSOR)lp );
2354 case WM_X11DRV_CLIP_CURSOR:
2355 return clip_cursor_notify( hwnd, (HWND)lp );
2357 FIXME( "got window msg %x hwnd %p wp %lx lp %lx\n", msg, hwnd, wp, lp );
2363 /***********************************************************************
2364 * is_netwm_supported
2366 static BOOL is_netwm_supported( Display *display, Atom atom )
2368 static Atom *net_supported;
2369 static int net_supported_count = -1;
2372 if (net_supported_count == -1)
2376 unsigned long count, remaining;
2378 if (!XGetWindowProperty( display, DefaultRootWindow(display), x11drv_atom(_NET_SUPPORTED), 0,
2379 ~0UL, False, XA_ATOM, &type, &format, &count,
2380 &remaining, (unsigned char **)&net_supported ))
2381 net_supported_count = get_property_size( format, count ) / sizeof(Atom);
2383 net_supported_count = 0;
2386 for (i = 0; i < net_supported_count; i++)
2387 if (net_supported[i] == atom) return TRUE;
2392 /***********************************************************************
2393 * SysCommand (X11DRV.@)
2395 * Perform WM_SYSCOMMAND handling.
2397 LRESULT CDECL X11DRV_SysCommand( HWND hwnd, WPARAM wparam, LPARAM lparam )
2399 WPARAM hittest = wparam & 0x0f;
2401 struct x11drv_win_data *data;
2403 if (!(data = get_win_data( hwnd ))) return -1;
2404 if (!data->whole_window || !data->managed || !data->mapped) goto failed;
2406 switch (wparam & 0xfff0)
2409 if (!hittest) dir = _NET_WM_MOVERESIZE_MOVE_KEYBOARD;
2410 else dir = _NET_WM_MOVERESIZE_MOVE;
2413 /* windows without WS_THICKFRAME are not resizable through the window manager */
2414 if (!(GetWindowLongW( hwnd, GWL_STYLE ) & WS_THICKFRAME)) goto failed;
2418 case WMSZ_LEFT: dir = _NET_WM_MOVERESIZE_SIZE_LEFT; break;
2419 case WMSZ_RIGHT: dir = _NET_WM_MOVERESIZE_SIZE_RIGHT; break;
2420 case WMSZ_TOP: dir = _NET_WM_MOVERESIZE_SIZE_TOP; break;
2421 case WMSZ_TOPLEFT: dir = _NET_WM_MOVERESIZE_SIZE_TOPLEFT; break;
2422 case WMSZ_TOPRIGHT: dir = _NET_WM_MOVERESIZE_SIZE_TOPRIGHT; break;
2423 case WMSZ_BOTTOM: dir = _NET_WM_MOVERESIZE_SIZE_BOTTOM; break;
2424 case WMSZ_BOTTOMLEFT: dir = _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT; break;
2425 case WMSZ_BOTTOMRIGHT: dir = _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT; break;
2426 default: dir = _NET_WM_MOVERESIZE_SIZE_KEYBOARD; break;
2431 /* prevent a simple ALT press+release from activating the system menu,
2432 * as that can get confusing on managed windows */
2433 if ((WCHAR)lparam) goto failed; /* got an explicit char */
2434 if (GetMenu( hwnd )) goto failed; /* window has a real menu */
2435 if (!(GetWindowLongW( hwnd, GWL_STYLE ) & WS_SYSMENU)) goto failed; /* no system menu */
2436 TRACE( "ignoring SC_KEYMENU wp %lx lp %lx\n", wparam, lparam );
2437 release_win_data( data );
2444 if (IsZoomed(hwnd)) goto failed;
2446 if (!is_netwm_supported( data->display, x11drv_atom(_NET_WM_MOVERESIZE) ))
2448 TRACE( "_NET_WM_MOVERESIZE not supported\n" );
2452 release_win_data( data );
2453 move_resize_window( hwnd, dir );
2457 release_win_data( data );