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 "xcomposite.h"
47 #include "wine/debug.h"
48 #include "wine/server.h"
51 WINE_DEFAULT_DEBUG_CHANNEL(x11drv);
53 #define _NET_WM_MOVERESIZE_SIZE_TOPLEFT 0
54 #define _NET_WM_MOVERESIZE_SIZE_TOP 1
55 #define _NET_WM_MOVERESIZE_SIZE_TOPRIGHT 2
56 #define _NET_WM_MOVERESIZE_SIZE_RIGHT 3
57 #define _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT 4
58 #define _NET_WM_MOVERESIZE_SIZE_BOTTOM 5
59 #define _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT 6
60 #define _NET_WM_MOVERESIZE_SIZE_LEFT 7
61 #define _NET_WM_MOVERESIZE_MOVE 8
62 #define _NET_WM_MOVERESIZE_SIZE_KEYBOARD 9
63 #define _NET_WM_MOVERESIZE_MOVE_KEYBOARD 10
65 #define _NET_WM_STATE_REMOVE 0
66 #define _NET_WM_STATE_ADD 1
67 #define _NET_WM_STATE_TOGGLE 2
69 #define SWP_AGG_NOPOSCHANGE (SWP_NOSIZE | SWP_NOMOVE | SWP_NOCLIENTSIZE | SWP_NOCLIENTMOVE | SWP_NOZORDER)
71 /* X context to associate a hwnd to an X window */
72 XContext winContext = 0;
74 /* X context to associate a struct x11drv_win_data to an hwnd */
75 static XContext win_data_context;
77 /* time of last user event and window where it's stored */
78 static Time last_user_time;
79 static Window user_time_window;
81 static const char foreign_window_prop[] = "__wine_x11_foreign_window";
82 static const char whole_window_prop[] = "__wine_x11_whole_window";
83 static const char client_window_prop[]= "__wine_x11_client_window";
84 static const char icon_window_prop[] = "__wine_x11_icon_window";
85 static const char fbconfig_id_prop[] = "__wine_x11_fbconfig_id";
86 static const char gl_drawable_prop[] = "__wine_x11_gl_drawable";
87 static const char pixmap_prop[] = "__wine_x11_pixmap";
88 static const char managed_prop[] = "__wine_x11_managed";
91 /***********************************************************************
92 * http://standards.freedesktop.org/startup-notification-spec
94 static void remove_startup_notification(Display *display, Window window)
96 static LONG startup_notification_removed = 0;
105 if (InterlockedCompareExchange(&startup_notification_removed, 1, 0) != 0)
108 if (GetEnvironmentVariableA("DESKTOP_STARTUP_ID", id, sizeof(id)) == 0)
110 SetEnvironmentVariableA("DESKTOP_STARTUP_ID", NULL);
112 if ((src = strstr( id, "_TIME" ))) update_user_time( atol( src + 5 ));
114 pos = snprintf(message, sizeof(message), "remove: ID=");
115 message[pos++] = '"';
116 for (i = 0; id[i] && pos < sizeof(message) - 2; i++)
118 if (id[i] == '"' || id[i] == '\\')
119 message[pos++] = '\\';
120 message[pos++] = id[i];
122 message[pos++] = '"';
123 message[pos++] = '\0';
125 xevent.xclient.type = ClientMessage;
126 xevent.xclient.message_type = x11drv_atom(_NET_STARTUP_INFO_BEGIN);
127 xevent.xclient.display = display;
128 xevent.xclient.window = window;
129 xevent.xclient.format = 8;
132 srclen = strlen(src) + 1;
140 memset(&xevent.xclient.data.b[0], 0, 20);
141 memcpy(&xevent.xclient.data.b[0], src, msglen);
145 XSendEvent( display, DefaultRootWindow( display ), False, PropertyChangeMask, &xevent );
146 xevent.xclient.message_type = x11drv_atom(_NET_STARTUP_INFO);
152 struct has_popup_result
158 static BOOL CALLBACK has_popup( HWND hwnd, LPARAM lparam )
160 struct has_popup_result *result = (struct has_popup_result *)lparam;
162 if (hwnd == result->hwnd) return FALSE; /* popups are always above owner */
163 result->found = (GetWindow( hwnd, GW_OWNER ) == result->hwnd);
164 return !result->found;
167 static BOOL has_owned_popups( HWND hwnd )
169 struct has_popup_result result;
172 result.found = FALSE;
173 EnumWindows( has_popup, (LPARAM)&result );
177 /***********************************************************************
180 * Check if a given window should be managed
182 static BOOL is_window_managed( HWND hwnd, UINT swp_flags, const RECT *window_rect )
184 DWORD style, ex_style;
186 if (!managed_mode) return FALSE;
188 /* child windows are not managed */
189 style = GetWindowLongW( hwnd, GWL_STYLE );
190 if ((style & (WS_CHILD|WS_POPUP)) == WS_CHILD) return FALSE;
191 /* activated windows are managed */
192 if (!(swp_flags & (SWP_NOACTIVATE|SWP_HIDEWINDOW))) return TRUE;
193 if (hwnd == GetActiveWindow()) return TRUE;
194 /* windows with caption are managed */
195 if ((style & WS_CAPTION) == WS_CAPTION) return TRUE;
196 /* windows with thick frame are managed */
197 if (style & WS_THICKFRAME) return TRUE;
198 if (style & WS_POPUP)
203 /* popup with sysmenu == caption are managed */
204 if (style & WS_SYSMENU) return TRUE;
205 /* full-screen popup windows are managed */
206 hmon = MonitorFromWindow( hwnd, MONITOR_DEFAULTTOPRIMARY );
207 mi.cbSize = sizeof( mi );
208 GetMonitorInfoW( hmon, &mi );
209 if (window_rect->left <= mi.rcWork.left && window_rect->right >= mi.rcWork.right &&
210 window_rect->top <= mi.rcWork.top && window_rect->bottom >= mi.rcWork.bottom)
213 /* application windows are managed */
214 ex_style = GetWindowLongW( hwnd, GWL_EXSTYLE );
215 if (ex_style & WS_EX_APPWINDOW) return TRUE;
216 /* windows that own popups are managed */
217 if (has_owned_popups( hwnd )) return TRUE;
218 /* default: not managed */
223 /***********************************************************************
224 * is_window_rect_mapped
226 * Check if the X whole window should be mapped based on its rectangle
228 static BOOL is_window_rect_mapped( const RECT *rect )
230 /* don't map if rect is off-screen */
231 if (rect->left >= virtual_screen_rect.right ||
232 rect->top >= virtual_screen_rect.bottom ||
233 rect->right <= virtual_screen_rect.left ||
234 rect->bottom <= virtual_screen_rect.top)
241 /***********************************************************************
242 * is_window_resizable
244 * Check if window should be made resizable by the window manager
246 static inline BOOL is_window_resizable( struct x11drv_win_data *data, DWORD style )
248 if (style & WS_THICKFRAME) return TRUE;
249 /* Metacity needs the window to be resizable to make it fullscreen */
250 return (data->whole_rect.left <= 0 && data->whole_rect.right >= screen_width &&
251 data->whole_rect.top <= 0 && data->whole_rect.bottom >= screen_height);
255 /***********************************************************************
256 * get_mwm_decorations
258 static unsigned long get_mwm_decorations( struct x11drv_win_data *data,
259 DWORD style, DWORD ex_style )
261 unsigned long ret = 0;
263 if (!decorated_mode) return 0;
265 if (IsRectEmpty( &data->window_rect )) return 0;
266 if (data->shaped) return 0;
268 if (ex_style & WS_EX_TOOLWINDOW) return 0;
270 if ((style & WS_CAPTION) == WS_CAPTION)
272 ret |= MWM_DECOR_TITLE | MWM_DECOR_BORDER;
273 if (style & WS_SYSMENU) ret |= MWM_DECOR_MENU;
274 if (style & WS_MINIMIZEBOX) ret |= MWM_DECOR_MINIMIZE;
275 if (style & WS_MAXIMIZEBOX) ret |= MWM_DECOR_MAXIMIZE;
277 if (ex_style & WS_EX_DLGMODALFRAME) ret |= MWM_DECOR_BORDER;
278 else if (style & WS_THICKFRAME) ret |= MWM_DECOR_BORDER | MWM_DECOR_RESIZEH;
279 else if ((style & (WS_DLGFRAME|WS_BORDER)) == WS_DLGFRAME) ret |= MWM_DECOR_BORDER;
284 /***********************************************************************
285 * get_x11_rect_offset
287 * Helper for X11DRV_window_to_X_rect and X11DRV_X_to_window_rect.
289 static void get_x11_rect_offset( struct x11drv_win_data *data, RECT *rect )
291 DWORD style, ex_style, style_mask = 0, ex_style_mask = 0;
294 rect->top = rect->bottom = rect->left = rect->right = 0;
296 style = GetWindowLongW( data->hwnd, GWL_STYLE );
297 ex_style = GetWindowLongW( data->hwnd, GWL_EXSTYLE );
298 decor = get_mwm_decorations( data, style, ex_style );
300 if (decor & MWM_DECOR_TITLE) style_mask |= WS_CAPTION;
301 if (decor & MWM_DECOR_BORDER)
303 style_mask |= WS_DLGFRAME | WS_THICKFRAME;
304 ex_style_mask |= WS_EX_DLGMODALFRAME;
307 AdjustWindowRectEx( rect, style & style_mask, FALSE, ex_style & ex_style_mask );
311 /***********************************************************************
312 * get_window_attributes
314 * Fill the window attributes structure for an X window.
316 static int get_window_attributes( Display *display, struct x11drv_win_data *data,
317 XSetWindowAttributes *attr )
319 attr->override_redirect = !data->managed;
320 attr->colormap = X11DRV_PALETTE_PaletteXColormap;
321 attr->save_under = ((GetClassLongW( data->hwnd, GCL_STYLE ) & CS_SAVEBITS) != 0);
322 attr->bit_gravity = NorthWestGravity;
323 attr->win_gravity = StaticGravity;
324 attr->backing_store = NotUseful;
325 attr->event_mask = (ExposureMask | PointerMotionMask |
326 ButtonPressMask | ButtonReleaseMask | EnterWindowMask |
327 KeyPressMask | KeyReleaseMask | FocusChangeMask |
328 KeymapStateMask | StructureNotifyMask);
329 if (data->managed) attr->event_mask |= PropertyChangeMask;
331 return (CWOverrideRedirect | CWSaveUnder | CWColormap |
332 CWEventMask | CWBitGravity | CWBackingStore);
336 /***********************************************************************
337 * create_client_window
339 static Window create_client_window( Display *display, struct x11drv_win_data *data, XVisualInfo *vis )
342 XSetWindowAttributes attr;
344 Visual *client_visual = vis ? vis->visual : visual;
346 attr.bit_gravity = NorthWestGravity;
347 attr.win_gravity = NorthWestGravity;
348 attr.backing_store = NotUseful;
349 attr.event_mask = (ExposureMask | PointerMotionMask |
350 ButtonPressMask | ButtonReleaseMask | EnterWindowMask);
351 mask = CWEventMask | CWBitGravity | CWWinGravity | CWBackingStore;
353 if ((cx = data->client_rect.right - data->client_rect.left) <= 0) cx = 1;
354 else if (cx > 65535) cx = 65535;
355 if ((cy = data->client_rect.bottom - data->client_rect.top) <= 0) cy = 1;
356 else if (cy > 65535) cy = 65535;
362 attr.colormap = XCreateColormap( display, root_window, vis->visual,
363 (vis->class == PseudoColor || vis->class == GrayScale ||
364 vis->class == DirectColor) ? AllocAll : AllocNone );
368 client = XCreateWindow( display, data->whole_window,
369 data->client_rect.left - data->whole_rect.left,
370 data->client_rect.top - data->whole_rect.top,
371 cx, cy, 0, screen_depth, InputOutput,
372 client_visual, mask, &attr );
379 if (data->client_window)
381 XDeleteContext( display, data->client_window, winContext );
382 XDestroyWindow( display, data->client_window );
384 data->client_window = client;
385 data->visualid = XVisualIDFromVisual( client_visual );
387 if (data->colormap) XFreeColormap( display, data->colormap );
388 data->colormap = vis ? attr.colormap : 0;
390 XMapWindow( display, data->client_window );
391 XSaveContext( display, data->client_window, winContext, (char *)data->hwnd );
394 SetPropA( data->hwnd, client_window_prop, (HANDLE)data->client_window );
395 return data->client_window;
399 /***********************************************************************
402 * Change the X window attributes when the window style has changed.
404 static void sync_window_style( Display *display, struct x11drv_win_data *data )
406 if (data->whole_window != root_window)
408 XSetWindowAttributes attr;
409 int mask = get_window_attributes( display, data, &attr );
412 XChangeWindowAttributes( display, data->whole_window, mask, &attr );
418 /***********************************************************************
421 static void sync_window_cursor( struct x11drv_win_data *data )
425 SERVER_START_REQ( set_cursor )
428 wine_server_call( req );
429 cursor = reply->prev_count >= 0 ? wine_server_ptr_handle( reply->prev_handle ) : 0;
433 set_window_cursor( data->hwnd, cursor );
435 /* setting the cursor can fail if the window isn't created yet */
436 /* so make sure that we try again once we receive a mouse event */
437 data->cursor = (HANDLE)~0u;
441 /***********************************************************************
444 * Update the X11 window region.
446 static void sync_window_region( Display *display, struct x11drv_win_data *data, HRGN win_region )
448 #ifdef HAVE_LIBXSHAPE
449 HRGN hrgn = win_region;
451 if (!data->whole_window) return;
452 data->shaped = FALSE;
454 if (IsRectEmpty( &data->window_rect )) /* set an empty shape */
456 static XRectangle empty_rect;
458 XShapeCombineRectangles( display, data->whole_window, ShapeBounding, 0, 0,
459 &empty_rect, 1, ShapeSet, YXBanded );
464 if (hrgn == (HRGN)1) /* hack: win_region == 1 means retrieve region from server */
466 if (!(hrgn = CreateRectRgn( 0, 0, 0, 0 ))) return;
467 if (GetWindowRgn( data->hwnd, hrgn ) == ERROR)
469 DeleteObject( hrgn );
477 XShapeCombineMask( display, data->whole_window, ShapeBounding, 0, 0, None, ShapeSet );
482 RGNDATA *pRegionData;
484 if (GetWindowLongW( data->hwnd, GWL_EXSTYLE ) & WS_EX_LAYOUTRTL) MirrorRgn( data->hwnd, hrgn );
485 if ((pRegionData = X11DRV_GetRegionData( hrgn, 0 )))
488 XShapeCombineRectangles( display, data->whole_window, ShapeBounding,
489 data->window_rect.left - data->whole_rect.left,
490 data->window_rect.top - data->whole_rect.top,
491 (XRectangle *)pRegionData->Buffer,
492 pRegionData->rdh.nCount, ShapeSet, YXBanded );
494 HeapFree(GetProcessHeap(), 0, pRegionData);
498 if (hrgn && hrgn != win_region) DeleteObject( hrgn );
499 #endif /* HAVE_LIBXSHAPE */
503 /***********************************************************************
504 * sync_window_opacity
506 static void sync_window_opacity( Display *display, Window win,
507 COLORREF key, BYTE alpha, DWORD flags )
509 unsigned long opacity = 0xffffffff;
511 if (flags & LWA_ALPHA) opacity = (0xffffffff / 0xff) * alpha;
513 if (flags & LWA_COLORKEY) FIXME("LWA_COLORKEY not supported\n");
516 if (opacity == 0xffffffff)
517 XDeleteProperty( display, win, x11drv_atom(_NET_WM_WINDOW_OPACITY) );
519 XChangeProperty( display, win, x11drv_atom(_NET_WM_WINDOW_OPACITY),
520 XA_CARDINAL, 32, PropModeReplace, (unsigned char *)&opacity, 1 );
525 /***********************************************************************
528 static void sync_window_text( Display *display, Window win, const WCHAR *text )
531 char *buffer, *utf8_buffer;
534 /* allocate new buffer for window text */
535 count = WideCharToMultiByte(CP_UNIXCP, 0, text, -1, NULL, 0, NULL, NULL);
536 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, count ))) return;
537 WideCharToMultiByte(CP_UNIXCP, 0, text, -1, buffer, count, NULL, NULL);
539 count = WideCharToMultiByte(CP_UTF8, 0, text, strlenW(text), NULL, 0, NULL, NULL);
540 if (!(utf8_buffer = HeapAlloc( GetProcessHeap(), 0, count )))
542 HeapFree( GetProcessHeap(), 0, buffer );
545 WideCharToMultiByte(CP_UTF8, 0, text, strlenW(text), utf8_buffer, count, NULL, NULL);
548 if (XmbTextListToTextProperty( display, &buffer, 1, XStdICCTextStyle, &prop ) == Success)
550 XSetWMName( display, win, &prop );
551 XSetWMIconName( display, win, &prop );
555 Implements a NET_WM UTF-8 title. It should be without a trailing \0,
556 according to the standard
557 ( http://www.pps.jussieu.fr/~jch/software/UTF8_STRING/UTF8_STRING.text ).
559 XChangeProperty( display, win, x11drv_atom(_NET_WM_NAME), x11drv_atom(UTF8_STRING),
560 8, PropModeReplace, (unsigned char *) utf8_buffer, count);
563 HeapFree( GetProcessHeap(), 0, utf8_buffer );
564 HeapFree( GetProcessHeap(), 0, buffer );
568 /***********************************************************************
571 static BOOL set_win_format( HWND hwnd, XID fbconfig_id )
573 struct x11drv_win_data *data;
577 if (!(data = X11DRV_get_win_data(hwnd)) &&
578 !(data = X11DRV_create_win_data(hwnd))) return FALSE;
580 if (!(vis = visual_from_fbconfig_id(fbconfig_id))) return FALSE;
582 if (data->whole_window)
584 Display *display = thread_display();
585 Window client = data->client_window;
587 if (vis->visualid != data->visualid)
589 client = create_client_window( display, data, vis );
590 TRACE( "re-created client window %lx for %p fbconfig %lx\n", client, data->hwnd, fbconfig_id );
596 if (client) goto done;
600 w = data->client_rect.right - data->client_rect.left;
601 h = data->client_rect.bottom - data->client_rect.top;
606 #ifdef SONAME_LIBXCOMPOSITE
609 XSetWindowAttributes attrib;
610 static Window dummy_parent;
613 attrib.override_redirect = True;
616 dummy_parent = XCreateWindow( gdi_display, root_window, -1, -1, 1, 1, 0, screen_depth,
617 InputOutput, visual, CWOverrideRedirect, &attrib );
618 XMapWindow( gdi_display, dummy_parent );
620 data->colormap = XCreateColormap(gdi_display, dummy_parent, vis->visual,
621 (vis->class == PseudoColor ||
622 vis->class == GrayScale ||
623 vis->class == DirectColor) ?
624 AllocAll : AllocNone);
625 attrib.colormap = data->colormap;
626 XInstallColormap(gdi_display, attrib.colormap);
628 if(data->gl_drawable) XDestroyWindow(gdi_display, data->gl_drawable);
629 data->gl_drawable = XCreateWindow(gdi_display, dummy_parent, -w, 0, w, h, 0,
630 vis->depth, InputOutput, vis->visual,
631 CWColormap | CWOverrideRedirect,
633 if(data->gl_drawable)
635 pXCompositeRedirectWindow(gdi_display, data->gl_drawable,
636 CompositeRedirectManual);
637 XMapWindow(gdi_display, data->gl_drawable);
640 XFlush( gdi_display );
646 WARN("XComposite is not available, using GLXPixmap hack\n");
650 if(data->pixmap) XFreePixmap(gdi_display, data->pixmap);
651 data->pixmap = XCreatePixmap(gdi_display, root_window, w, h, vis->depth);
659 if(data->gl_drawable) destroy_glxpixmap(gdi_display, data->gl_drawable);
660 data->gl_drawable = create_glxpixmap(gdi_display, vis, data->pixmap);
661 if(!data->gl_drawable)
663 XFreePixmap(gdi_display, data->pixmap);
667 XFlush( gdi_display );
669 if (data->pixmap) SetPropA(hwnd, pixmap_prop, (HANDLE)data->pixmap);
672 if (!data->gl_drawable) return FALSE;
674 TRACE("Created GL drawable 0x%lx, using FBConfigID 0x%lx\n",
675 data->gl_drawable, fbconfig_id);
676 SetPropA(hwnd, gl_drawable_prop, (HANDLE)data->gl_drawable);
679 data->fbconfig_id = fbconfig_id;
680 SetPropA(hwnd, fbconfig_id_prop, (HANDLE)data->fbconfig_id);
681 /* force DCE invalidation */
682 SetWindowPos( hwnd, 0, 0, 0, 0, 0,
683 SWP_NOACTIVATE | SWP_NOZORDER | SWP_NOSIZE | SWP_NOMOVE |
684 SWP_NOREDRAW | SWP_DEFERERASE | SWP_NOSENDCHANGING | SWP_STATECHANGED);
688 /***********************************************************************
691 static void sync_gl_drawable(struct x11drv_win_data *data)
693 int w = data->client_rect.right - data->client_rect.left;
694 int h = data->client_rect.bottom - data->client_rect.top;
702 TRACE("Resizing GL drawable 0x%lx to %dx%d\n", data->gl_drawable, w, h);
703 #ifdef SONAME_LIBXCOMPOSITE
707 XMoveResizeWindow(gdi_display, data->gl_drawable, -w, 0, w, h);
713 if (!(vis = visual_from_fbconfig_id(data->fbconfig_id))) return;
716 pix = XCreatePixmap(gdi_display, root_window, w, h, vis->depth);
719 ERR("Failed to create pixmap for offscreen rendering\n");
725 glxp = create_glxpixmap(gdi_display, vis, pix);
728 ERR("Failed to create drawable for offscreen rendering\n");
729 XFreePixmap(gdi_display, pix);
737 mark_drawable_dirty(data->gl_drawable, glxp);
739 XFreePixmap(gdi_display, data->pixmap);
740 destroy_glxpixmap(gdi_display, data->gl_drawable);
741 TRACE( "Recreated GL drawable %lx to replace %lx\n", glxp, data->gl_drawable );
744 data->gl_drawable = glxp;
746 XFlush( gdi_display );
749 SetPropA(data->hwnd, gl_drawable_prop, (HANDLE)data->gl_drawable);
750 SetPropA(data->hwnd, pixmap_prop, (HANDLE)data->pixmap);
754 /***********************************************************************
757 * fill the window changes structure
759 static int get_window_changes( XWindowChanges *changes, const RECT *old, const RECT *new )
763 if (old->right - old->left != new->right - new->left )
765 if ((changes->width = new->right - new->left) <= 0) changes->width = 1;
766 else if (changes->width > 65535) changes->width = 65535;
769 if (old->bottom - old->top != new->bottom - new->top)
771 if ((changes->height = new->bottom - new->top) <= 0) changes->height = 1;
772 else if (changes->height > 65535) changes->height = 65535;
775 if (old->left != new->left)
777 changes->x = new->left;
780 if (old->top != new->top)
782 changes->y = new->top;
789 /***********************************************************************
792 static Window create_icon_window( Display *display, struct x11drv_win_data *data )
794 XSetWindowAttributes attr;
796 attr.event_mask = (ExposureMask | KeyPressMask | KeyReleaseMask | PointerMotionMask |
797 ButtonPressMask | ButtonReleaseMask | EnterWindowMask);
798 attr.bit_gravity = NorthWestGravity;
799 attr.backing_store = NotUseful/*WhenMapped*/;
800 attr.colormap = X11DRV_PALETTE_PaletteXColormap; /* Needed due to our visual */
803 data->icon_window = XCreateWindow( display, root_window, 0, 0,
804 GetSystemMetrics( SM_CXICON ),
805 GetSystemMetrics( SM_CYICON ),
808 CWEventMask | CWBitGravity | CWBackingStore | CWColormap, &attr );
809 XSaveContext( display, data->icon_window, winContext, (char *)data->hwnd );
810 XFlush( display ); /* make sure the window exists before we start painting to it */
813 TRACE( "created %lx\n", data->icon_window );
814 SetPropA( data->hwnd, icon_window_prop, (HANDLE)data->icon_window );
815 return data->icon_window;
820 /***********************************************************************
821 * destroy_icon_window
823 static void destroy_icon_window( Display *display, struct x11drv_win_data *data )
825 if (!data->icon_window) return;
827 XDeleteContext( display, data->icon_window, winContext );
828 XDestroyWindow( display, data->icon_window );
829 data->icon_window = 0;
831 RemovePropA( data->hwnd, icon_window_prop );
835 /***********************************************************************
838 * Return the bitmap bits in ARGB format. Helper for setting icon hints.
840 static unsigned long *get_bitmap_argb( HDC hdc, HBITMAP color, HBITMAP mask, unsigned int *size )
844 unsigned int *ptr, *bits = NULL;
845 unsigned char *mask_bits = NULL;
846 int i, j, has_alpha = 0;
848 if (!GetObjectW( color, sizeof(bm), &bm )) return NULL;
849 if (!(info = HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET( BITMAPINFO, bmiColors[256] )))) return NULL;
850 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
851 info->bmiHeader.biWidth = bm.bmWidth;
852 info->bmiHeader.biHeight = -bm.bmHeight;
853 info->bmiHeader.biPlanes = 1;
854 info->bmiHeader.biBitCount = 32;
855 info->bmiHeader.biCompression = BI_RGB;
856 info->bmiHeader.biSizeImage = bm.bmWidth * bm.bmHeight * 4;
857 info->bmiHeader.biXPelsPerMeter = 0;
858 info->bmiHeader.biYPelsPerMeter = 0;
859 info->bmiHeader.biClrUsed = 0;
860 info->bmiHeader.biClrImportant = 0;
861 *size = bm.bmWidth * bm.bmHeight + 2;
862 if (!(bits = HeapAlloc( GetProcessHeap(), 0, *size * sizeof(long) ))) goto failed;
863 if (!GetDIBits( hdc, color, 0, bm.bmHeight, bits + 2, info, DIB_RGB_COLORS )) goto failed;
865 bits[0] = bm.bmWidth;
866 bits[1] = bm.bmHeight;
868 for (i = 0; i < bm.bmWidth * bm.bmHeight; i++)
869 if ((has_alpha = (bits[i + 2] & 0xff000000) != 0)) break;
873 unsigned int width_bytes = (bm.bmWidth + 31) / 32 * 4;
874 /* generate alpha channel from the mask */
875 info->bmiHeader.biBitCount = 1;
876 info->bmiHeader.biSizeImage = width_bytes * bm.bmHeight;
877 if (!(mask_bits = HeapAlloc( GetProcessHeap(), 0, info->bmiHeader.biSizeImage ))) goto failed;
878 if (!GetDIBits( hdc, mask, 0, bm.bmHeight, mask_bits, info, DIB_RGB_COLORS )) goto failed;
880 for (i = 0; i < bm.bmHeight; i++)
881 for (j = 0; j < bm.bmWidth; j++, ptr++)
882 if (!((mask_bits[i * width_bytes + j / 8] << (j % 8)) & 0x80)) *ptr |= 0xff000000;
883 HeapFree( GetProcessHeap(), 0, mask_bits );
885 HeapFree( GetProcessHeap(), 0, info );
887 /* convert to array of longs */
888 if (bits && sizeof(long) > sizeof(int))
889 for (i = *size - 1; i >= 0; i--) ((unsigned long *)bits)[i] = bits[i];
891 return (unsigned long *)bits;
894 HeapFree( GetProcessHeap(), 0, info );
895 HeapFree( GetProcessHeap(), 0, bits );
896 HeapFree( GetProcessHeap(), 0, mask_bits );
901 /***********************************************************************
904 * Set the icon wm hints
906 static void set_icon_hints( Display *display, struct x11drv_win_data *data,
907 HICON icon_big, HICON icon_small )
909 XWMHints *hints = data->wm_hints;
913 icon_big = (HICON)SendMessageW( data->hwnd, WM_GETICON, ICON_BIG, 0 );
914 if (!icon_big) icon_big = (HICON)GetClassLongPtrW( data->hwnd, GCLP_HICON );
918 icon_small = (HICON)SendMessageW( data->hwnd, WM_GETICON, ICON_SMALL, 0 );
919 if (!icon_small) icon_small = (HICON)GetClassLongPtrW( data->hwnd, GCLP_HICONSM );
922 if (data->hWMIconBitmap) DeleteObject( data->hWMIconBitmap );
923 if (data->hWMIconMask) DeleteObject( data->hWMIconMask);
924 data->hWMIconBitmap = 0;
925 data->hWMIconMask = 0;
929 if (!data->icon_window) create_icon_window( display, data );
930 hints->icon_window = data->icon_window;
931 hints->flags = (hints->flags & ~(IconPixmapHint | IconMaskHint)) | IconWindowHint;
938 ICONINFO ii, ii_small;
943 if (!GetIconInfo(icon_big, &ii)) return;
945 GetObjectW(ii.hbmMask, sizeof(bm), &bm);
948 rcMask.right = bm.bmWidth;
949 rcMask.bottom = bm.bmHeight;
951 hDC = CreateCompatibleDC(0);
952 bits = get_bitmap_argb( hDC, ii.hbmColor, ii.hbmMask, &size );
953 if (bits && GetIconInfo( icon_small, &ii_small ))
955 unsigned int size_small;
956 unsigned long *bits_small, *new;
958 if ((bits_small = get_bitmap_argb( hDC, ii_small.hbmColor, ii_small.hbmMask, &size_small )) &&
959 (bits_small[0] != bits[0] || bits_small[1] != bits[1])) /* size must be different */
961 if ((new = HeapReAlloc( GetProcessHeap(), 0, bits,
962 (size + size_small) * sizeof(unsigned long) )))
965 memcpy( bits + size, bits_small, size_small * sizeof(unsigned long) );
969 HeapFree( GetProcessHeap(), 0, bits_small );
970 DeleteObject( ii_small.hbmColor );
971 DeleteObject( ii_small.hbmMask );
975 XChangeProperty( display, data->whole_window, x11drv_atom(_NET_WM_ICON),
976 XA_CARDINAL, 32, PropModeReplace, (unsigned char *)bits, size );
978 XDeleteProperty( display, data->whole_window, x11drv_atom(_NET_WM_ICON) );
980 HeapFree( GetProcessHeap(), 0, bits );
982 hbmOrig = SelectObject(hDC, ii.hbmMask);
983 InvertRect(hDC, &rcMask);
984 SelectObject(hDC, ii.hbmColor); /* force the color bitmap to x11drv mode too */
985 SelectObject(hDC, hbmOrig);
987 data->hWMIconBitmap = ii.hbmColor;
988 data->hWMIconMask = ii.hbmMask;
990 hints->icon_pixmap = X11DRV_get_pixmap(data->hWMIconBitmap);
991 hints->icon_mask = X11DRV_get_pixmap(data->hWMIconMask);
992 destroy_icon_window( display, data );
993 hints->flags = (hints->flags & ~IconWindowHint) | IconPixmapHint | IconMaskHint;
1000 /***********************************************************************
1003 * set the window size hints
1005 static void set_size_hints( Display *display, struct x11drv_win_data *data, DWORD style )
1007 XSizeHints* size_hints;
1009 if (!(size_hints = XAllocSizeHints())) return;
1011 size_hints->win_gravity = StaticGravity;
1012 size_hints->flags |= PWinGravity;
1014 /* don't update size hints if window is not in normal state */
1015 if (!(style & (WS_MINIMIZE | WS_MAXIMIZE)))
1017 if (data->hwnd != GetDesktopWindow()) /* don't force position of desktop */
1019 size_hints->x = data->whole_rect.left;
1020 size_hints->y = data->whole_rect.top;
1021 size_hints->flags |= PPosition;
1024 if (!is_window_resizable( data, style ))
1026 size_hints->max_width = data->whole_rect.right - data->whole_rect.left;
1027 size_hints->max_height = data->whole_rect.bottom - data->whole_rect.top;
1028 if (size_hints->max_width <= 0 ||size_hints->max_height <= 0)
1029 size_hints->max_width = size_hints->max_height = 1;
1030 size_hints->min_width = size_hints->max_width;
1031 size_hints->min_height = size_hints->max_height;
1032 size_hints->flags |= PMinSize | PMaxSize;
1035 XSetWMNormalHints( display, data->whole_window, size_hints );
1036 XFree( size_hints );
1040 /***********************************************************************
1043 * get the name of the current process for setting class hints
1045 static char *get_process_name(void)
1051 WCHAR module[MAX_PATH];
1052 DWORD len = GetModuleFileNameW( 0, module, MAX_PATH );
1053 if (len && len < MAX_PATH)
1056 WCHAR *p, *appname = module;
1058 if ((p = strrchrW( appname, '/' ))) appname = p + 1;
1059 if ((p = strrchrW( appname, '\\' ))) appname = p + 1;
1060 len = WideCharToMultiByte( CP_UNIXCP, 0, appname, -1, NULL, 0, NULL, NULL );
1061 if ((ptr = HeapAlloc( GetProcessHeap(), 0, len )))
1063 WideCharToMultiByte( CP_UNIXCP, 0, appname, -1, ptr, len, NULL, NULL );
1072 /***********************************************************************
1073 * set_initial_wm_hints
1075 * Set the window manager hints that don't change over the lifetime of a window.
1077 static void set_initial_wm_hints( Display *display, struct x11drv_win_data *data )
1081 Atom dndVersion = WINE_XDND_VERSION;
1082 XClassHint *class_hints;
1083 char *process_name = get_process_name();
1089 protocols[i++] = x11drv_atom(WM_DELETE_WINDOW);
1090 protocols[i++] = x11drv_atom(_NET_WM_PING);
1091 if (use_take_focus) protocols[i++] = x11drv_atom(WM_TAKE_FOCUS);
1092 XChangeProperty( display, data->whole_window, x11drv_atom(WM_PROTOCOLS),
1093 XA_ATOM, 32, PropModeReplace, (unsigned char *)protocols, i );
1096 if ((class_hints = XAllocClassHint()))
1098 static char wine[] = "Wine";
1100 class_hints->res_name = process_name;
1101 class_hints->res_class = wine;
1102 XSetClassHint( display, data->whole_window, class_hints );
1103 XFree( class_hints );
1106 /* set the WM_CLIENT_MACHINE and WM_LOCALE_NAME properties */
1107 XSetWMProperties(display, data->whole_window, NULL, NULL, NULL, 0, NULL, NULL, NULL);
1108 /* set the pid. together, these properties are needed so the window manager can kill us if we freeze */
1110 XChangeProperty(display, data->whole_window, x11drv_atom(_NET_WM_PID),
1111 XA_CARDINAL, 32, PropModeReplace, (unsigned char *)&i, 1);
1113 XChangeProperty( display, data->whole_window, x11drv_atom(XdndAware),
1114 XA_ATOM, 32, PropModeReplace, (unsigned char*)&dndVersion, 1 );
1116 update_user_time( 0 ); /* make sure that the user time window exists */
1117 if (user_time_window)
1118 XChangeProperty( display, data->whole_window, x11drv_atom(_NET_WM_USER_TIME_WINDOW),
1119 XA_WINDOW, 32, PropModeReplace, (unsigned char *)&user_time_window, 1 );
1121 data->wm_hints = XAllocWMHints();
1122 wine_tsx11_unlock();
1126 data->wm_hints->flags = 0;
1127 set_icon_hints( display, data, 0, 0 );
1132 /***********************************************************************
1133 * get_owner_whole_window
1135 * Retrieve an owner's window, creating it if necessary.
1137 static Window get_owner_whole_window( HWND owner, BOOL force_managed )
1139 struct x11drv_win_data *data;
1141 if (!owner) return 0;
1143 if (!(data = X11DRV_get_win_data( owner )))
1145 if (GetWindowThreadProcessId( owner, NULL ) != GetCurrentThreadId() ||
1146 !(data = X11DRV_create_win_data( owner )))
1147 return (Window)GetPropA( owner, whole_window_prop );
1149 else if (!data->managed && force_managed) /* make it managed */
1151 SetWindowPos( owner, 0, 0, 0, 0, 0,
1152 SWP_NOACTIVATE | SWP_NOZORDER | SWP_NOSIZE | SWP_NOMOVE |
1153 SWP_NOREDRAW | SWP_DEFERERASE | SWP_NOSENDCHANGING | SWP_STATECHANGED );
1155 return data->whole_window;
1159 /***********************************************************************
1162 * Set the window manager hints for a newly-created window
1164 static void set_wm_hints( Display *display, struct x11drv_win_data *data )
1166 Window group_leader = data->whole_window;
1167 Window owner_win = 0;
1170 DWORD style, ex_style;
1173 if (data->hwnd == GetDesktopWindow())
1175 /* force some styles for the desktop to get the correct decorations */
1176 style = WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX;
1177 ex_style = WS_EX_APPWINDOW;
1182 style = GetWindowLongW( data->hwnd, GWL_STYLE );
1183 ex_style = GetWindowLongW( data->hwnd, GWL_EXSTYLE );
1184 owner = GetWindow( data->hwnd, GW_OWNER );
1185 if ((owner_win = get_owner_whole_window( owner, data->managed ))) group_leader = owner_win;
1190 if (owner_win) XSetTransientForHint( display, data->whole_window, owner_win );
1193 set_size_hints( display, data, style );
1195 /* set the WM_WINDOW_TYPE */
1196 if (style & WS_THICKFRAME) window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_NORMAL);
1197 else if (ex_style & WS_EX_APPWINDOW) window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_NORMAL);
1198 else if (style & WS_MINIMIZEBOX) window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_NORMAL);
1199 else if (style & WS_DLGFRAME) window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_DIALOG);
1200 else if (ex_style & WS_EX_DLGMODALFRAME) window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_DIALOG);
1201 else if ((style & WS_POPUP) && owner) window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_DIALOG);
1202 #if 0 /* many window managers don't handle utility windows very well */
1203 else if (ex_style & WS_EX_TOOLWINDOW) window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_UTILITY);
1205 else window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_NORMAL);
1207 XChangeProperty(display, data->whole_window, x11drv_atom(_NET_WM_WINDOW_TYPE),
1208 XA_ATOM, 32, PropModeReplace, (unsigned char*)&window_type, 1);
1210 mwm_hints.flags = MWM_HINTS_FUNCTIONS | MWM_HINTS_DECORATIONS;
1211 mwm_hints.decorations = get_mwm_decorations( data, style, ex_style );
1212 mwm_hints.functions = MWM_FUNC_MOVE;
1213 if (is_window_resizable( data, style )) mwm_hints.functions |= MWM_FUNC_RESIZE;
1214 if (!(style & WS_DISABLED))
1216 if (style & WS_MINIMIZEBOX) mwm_hints.functions |= MWM_FUNC_MINIMIZE;
1217 if (style & WS_MAXIMIZEBOX) mwm_hints.functions |= MWM_FUNC_MAXIMIZE;
1218 if (style & WS_SYSMENU) mwm_hints.functions |= MWM_FUNC_CLOSE;
1221 XChangeProperty( display, data->whole_window, x11drv_atom(_MOTIF_WM_HINTS),
1222 x11drv_atom(_MOTIF_WM_HINTS), 32, PropModeReplace,
1223 (unsigned char*)&mwm_hints, sizeof(mwm_hints)/sizeof(long) );
1228 data->wm_hints->flags |= InputHint | StateHint | WindowGroupHint;
1229 data->wm_hints->input = !use_take_focus && !(style & WS_DISABLED);
1230 data->wm_hints->initial_state = (style & WS_MINIMIZE) ? IconicState : NormalState;
1231 data->wm_hints->window_group = group_leader;
1232 XSetWMHints( display, data->whole_window, data->wm_hints );
1235 wine_tsx11_unlock();
1239 /***********************************************************************
1242 void update_user_time( Time time )
1245 if (!user_time_window)
1247 user_time_window = XCreateWindow( gdi_display, root_window, -1, -1, 1, 1, 0, 0, InputOnly,
1248 DefaultVisual(gdi_display,DefaultScreen(gdi_display)), 0, NULL );
1249 TRACE( "user time window %lx\n", user_time_window );
1251 if (time && (!last_user_time || (long)(time - last_user_time) > 0))
1253 last_user_time = time;
1254 XChangeProperty( gdi_display, user_time_window, x11drv_atom(_NET_WM_USER_TIME),
1255 XA_CARDINAL, 32, PropModeReplace, (unsigned char *)&time, 1 );
1257 wine_tsx11_unlock();
1260 /***********************************************************************
1261 * update_net_wm_states
1263 void update_net_wm_states( Display *display, struct x11drv_win_data *data )
1265 static const unsigned int state_atoms[NB_NET_WM_STATES] =
1267 XATOM__NET_WM_STATE_FULLSCREEN,
1268 XATOM__NET_WM_STATE_ABOVE,
1269 XATOM__NET_WM_STATE_MAXIMIZED_VERT,
1270 XATOM__NET_WM_STATE_SKIP_PAGER,
1271 XATOM__NET_WM_STATE_SKIP_TASKBAR
1274 DWORD i, style, ex_style, new_state = 0;
1276 if (!data->managed) return;
1277 if (data->whole_window == root_window) return;
1279 style = GetWindowLongW( data->hwnd, GWL_STYLE );
1280 if (data->whole_rect.left <= 0 && data->whole_rect.right >= screen_width &&
1281 data->whole_rect.top <= 0 && data->whole_rect.bottom >= screen_height)
1283 if ((style & WS_MAXIMIZE) && (style & WS_CAPTION) == WS_CAPTION)
1284 new_state |= (1 << NET_WM_STATE_MAXIMIZED);
1285 else if (!(style & WS_MINIMIZE))
1286 new_state |= (1 << NET_WM_STATE_FULLSCREEN);
1288 else if (style & WS_MAXIMIZE)
1289 new_state |= (1 << NET_WM_STATE_MAXIMIZED);
1291 ex_style = GetWindowLongW( data->hwnd, GWL_EXSTYLE );
1292 if (ex_style & WS_EX_TOPMOST)
1293 new_state |= (1 << NET_WM_STATE_ABOVE);
1294 if (ex_style & (WS_EX_TOOLWINDOW | WS_EX_NOACTIVATE))
1295 new_state |= (1 << NET_WM_STATE_SKIP_TASKBAR) | (1 << NET_WM_STATE_SKIP_PAGER);
1296 if (!(ex_style & WS_EX_APPWINDOW) && GetWindow( data->hwnd, GW_OWNER ))
1297 new_state |= (1 << NET_WM_STATE_SKIP_TASKBAR);
1299 if (!data->mapped) /* set the _NET_WM_STATE atom directly */
1301 Atom atoms[NB_NET_WM_STATES+1];
1304 for (i = count = 0; i < NB_NET_WM_STATES; i++)
1306 if (!(new_state & (1 << i))) continue;
1307 TRACE( "setting wm state %u for unmapped window %p/%lx\n",
1308 i, data->hwnd, data->whole_window );
1309 atoms[count++] = X11DRV_Atoms[state_atoms[i] - FIRST_XATOM];
1310 if (state_atoms[i] == XATOM__NET_WM_STATE_MAXIMIZED_VERT)
1311 atoms[count++] = x11drv_atom(_NET_WM_STATE_MAXIMIZED_HORZ);
1314 XChangeProperty( display, data->whole_window, x11drv_atom(_NET_WM_STATE), XA_ATOM,
1315 32, PropModeReplace, (unsigned char *)atoms, count );
1316 wine_tsx11_unlock();
1318 else /* ask the window manager to do it for us */
1322 xev.xclient.type = ClientMessage;
1323 xev.xclient.window = data->whole_window;
1324 xev.xclient.message_type = x11drv_atom(_NET_WM_STATE);
1325 xev.xclient.serial = 0;
1326 xev.xclient.display = display;
1327 xev.xclient.send_event = True;
1328 xev.xclient.format = 32;
1329 xev.xclient.data.l[3] = 1;
1331 for (i = 0; i < NB_NET_WM_STATES; i++)
1333 if (!((data->net_wm_state ^ new_state) & (1 << i))) continue; /* unchanged */
1335 TRACE( "setting wm state %u for window %p/%lx to %u prev %u\n",
1336 i, data->hwnd, data->whole_window,
1337 (new_state & (1 << i)) != 0, (data->net_wm_state & (1 << i)) != 0 );
1339 xev.xclient.data.l[0] = (new_state & (1 << i)) ? _NET_WM_STATE_ADD : _NET_WM_STATE_REMOVE;
1340 xev.xclient.data.l[1] = X11DRV_Atoms[state_atoms[i] - FIRST_XATOM];
1341 xev.xclient.data.l[2] = ((state_atoms[i] == XATOM__NET_WM_STATE_MAXIMIZED_VERT) ?
1342 x11drv_atom(_NET_WM_STATE_MAXIMIZED_HORZ) : 0);
1344 XSendEvent( display, root_window, False,
1345 SubstructureRedirectMask | SubstructureNotifyMask, &xev );
1346 wine_tsx11_unlock();
1349 data->net_wm_state = new_state;
1353 /***********************************************************************
1356 static void set_xembed_flags( Display *display, struct x11drv_win_data *data, unsigned long flags )
1358 unsigned long info[2];
1360 if (!data->whole_window) return;
1362 info[0] = 0; /* protocol version */
1365 XChangeProperty( display, data->whole_window, x11drv_atom(_XEMBED_INFO),
1366 x11drv_atom(_XEMBED_INFO), 32, PropModeReplace, (unsigned char*)info, 2 );
1367 wine_tsx11_unlock();
1371 /***********************************************************************
1374 static void map_window( Display *display, struct x11drv_win_data *data, DWORD new_style )
1376 TRACE( "win %p/%lx\n", data->hwnd, data->whole_window );
1378 remove_startup_notification( display, data->whole_window );
1380 wait_for_withdrawn_state( display, data, TRUE );
1382 if (!data->embedded)
1384 update_net_wm_states( display, data );
1385 sync_window_style( display, data );
1387 XMapWindow( display, data->whole_window );
1388 wine_tsx11_unlock();
1390 else set_xembed_flags( display, data, XEMBED_MAPPED );
1392 data->mapped = TRUE;
1393 data->iconic = (new_style & WS_MINIMIZE) != 0;
1397 /***********************************************************************
1400 static void unmap_window( Display *display, struct x11drv_win_data *data )
1402 TRACE( "win %p/%lx\n", data->hwnd, data->whole_window );
1404 if (!data->embedded)
1406 wait_for_withdrawn_state( display, data, FALSE );
1408 if (data->managed) XWithdrawWindow( display, data->whole_window, DefaultScreen(display) );
1409 else XUnmapWindow( display, data->whole_window );
1410 wine_tsx11_unlock();
1412 else set_xembed_flags( display, data, 0 );
1414 data->mapped = FALSE;
1415 data->net_wm_state = 0;
1419 /***********************************************************************
1420 * make_window_embedded
1422 void make_window_embedded( Display *display, struct x11drv_win_data *data )
1424 BOOL was_mapped = data->mapped;
1425 /* the window cannot be mapped before being embedded */
1426 if (data->mapped) unmap_window( display, data );
1428 data->embedded = TRUE;
1429 data->managed = TRUE;
1430 SetPropA( data->hwnd, managed_prop, (HANDLE)1 );
1431 sync_window_style( display, data );
1434 map_window( display, data, 0 );
1436 set_xembed_flags( display, data, 0 );
1440 /***********************************************************************
1441 * X11DRV_window_to_X_rect
1443 * Convert a rect from client to X window coordinates
1445 static void X11DRV_window_to_X_rect( struct x11drv_win_data *data, RECT *rect )
1449 if (!data->managed) return;
1450 if (IsRectEmpty( rect )) return;
1452 get_x11_rect_offset( data, &rc );
1454 rect->left -= rc.left;
1455 rect->right -= rc.right;
1456 rect->top -= rc.top;
1457 rect->bottom -= rc.bottom;
1458 if (rect->top >= rect->bottom) rect->bottom = rect->top + 1;
1459 if (rect->left >= rect->right) rect->right = rect->left + 1;
1463 /***********************************************************************
1464 * X11DRV_X_to_window_rect
1466 * Opposite of X11DRV_window_to_X_rect
1468 void X11DRV_X_to_window_rect( struct x11drv_win_data *data, RECT *rect )
1472 if (!data->managed) return;
1473 if (IsRectEmpty( rect )) return;
1475 get_x11_rect_offset( data, &rc );
1477 rect->left += rc.left;
1478 rect->right += rc.right;
1479 rect->top += rc.top;
1480 rect->bottom += rc.bottom;
1481 if (rect->top >= rect->bottom) rect->bottom = rect->top + 1;
1482 if (rect->left >= rect->right) rect->right = rect->left + 1;
1486 /***********************************************************************
1487 * sync_window_position
1489 * Synchronize the X window position with the Windows one
1491 static void sync_window_position( Display *display, struct x11drv_win_data *data,
1492 UINT swp_flags, const RECT *old_window_rect,
1493 const RECT *old_whole_rect, const RECT *old_client_rect )
1495 DWORD style = GetWindowLongW( data->hwnd, GWL_STYLE );
1496 XWindowChanges changes;
1497 unsigned int mask = 0;
1499 if (data->managed && data->iconic) return;
1501 /* resizing a managed maximized window is not allowed */
1502 if (!(style & WS_MAXIMIZE) || !data->managed)
1504 changes.width = data->whole_rect.right - data->whole_rect.left;
1505 changes.height = data->whole_rect.bottom - data->whole_rect.top;
1506 /* if window rect is empty force size to 1x1 */
1507 if (changes.width <= 0 || changes.height <= 0) changes.width = changes.height = 1;
1508 if (changes.width > 65535) changes.width = 65535;
1509 if (changes.height > 65535) changes.height = 65535;
1510 mask |= CWWidth | CWHeight;
1513 /* only the size is allowed to change for the desktop window */
1514 if (data->whole_window != root_window)
1516 changes.x = data->whole_rect.left - virtual_screen_rect.left;
1517 changes.y = data->whole_rect.top - virtual_screen_rect.top;
1521 if (!(swp_flags & SWP_NOZORDER) || (swp_flags & SWP_SHOWWINDOW))
1523 /* find window that this one must be after */
1524 HWND prev = GetWindow( data->hwnd, GW_HWNDPREV );
1525 while (prev && !(GetWindowLongW( prev, GWL_STYLE ) & WS_VISIBLE))
1526 prev = GetWindow( prev, GW_HWNDPREV );
1527 if (!prev) /* top child */
1529 changes.stack_mode = Above;
1530 mask |= CWStackMode;
1532 /* should use stack_mode Below but most window managers don't get it right */
1533 /* and Above with a sibling doesn't work so well either, so we ignore it */
1537 set_size_hints( display, data, style );
1538 data->configure_serial = NextRequest( display );
1539 XReconfigureWMWindow( display, data->whole_window,
1540 DefaultScreen(display), mask, &changes );
1541 #ifdef HAVE_LIBXSHAPE
1542 if (IsRectEmpty( old_window_rect ) != IsRectEmpty( &data->window_rect ))
1543 sync_window_region( display, data, (HRGN)1 );
1546 int old_x_offset = old_window_rect->left - old_whole_rect->left;
1547 int old_y_offset = old_window_rect->top - old_whole_rect->top;
1548 int new_x_offset = data->window_rect.left - data->whole_rect.left;
1549 int new_y_offset = data->window_rect.top - data->whole_rect.top;
1550 if (old_x_offset != new_x_offset || old_y_offset != new_y_offset)
1551 XShapeOffsetShape( display, data->whole_window, ShapeBounding,
1552 new_x_offset - old_x_offset, new_y_offset - old_y_offset );
1555 wine_tsx11_unlock();
1557 TRACE( "win %p/%lx pos %d,%d,%dx%d after %lx changes=%x serial=%lu\n",
1558 data->hwnd, data->whole_window, data->whole_rect.left, data->whole_rect.top,
1559 data->whole_rect.right - data->whole_rect.left,
1560 data->whole_rect.bottom - data->whole_rect.top,
1561 changes.sibling, mask, data->configure_serial );
1565 /***********************************************************************
1566 * sync_client_position
1568 * Synchronize the X client window position with the Windows one
1570 static void sync_client_position( Display *display, struct x11drv_win_data *data,
1571 UINT swp_flags, const RECT *old_client_rect,
1572 const RECT *old_whole_rect )
1575 XWindowChanges changes;
1576 RECT old = *old_client_rect;
1577 RECT new = data->client_rect;
1579 OffsetRect( &old, -old_whole_rect->left, -old_whole_rect->top );
1580 OffsetRect( &new, -data->whole_rect.left, -data->whole_rect.top );
1581 if (!(mask = get_window_changes( &changes, &old, &new ))) return;
1583 if (data->client_window)
1585 TRACE( "setting client win %lx pos %d,%d,%dx%d changes=%x\n",
1586 data->client_window, new.left, new.top,
1587 new.right - new.left, new.bottom - new.top, mask );
1589 XConfigureWindow( display, data->client_window, mask, &changes );
1590 wine_tsx11_unlock();
1593 if (data->gl_drawable && (mask & (CWWidth|CWHeight))) sync_gl_drawable( data );
1597 /***********************************************************************
1600 * Move the window bits when a window is moved.
1602 static void move_window_bits( struct x11drv_win_data *data, const RECT *old_rect, const RECT *new_rect,
1603 const RECT *old_client_rect )
1605 RECT src_rect = *old_rect;
1606 RECT dst_rect = *new_rect;
1607 HDC hdc_src, hdc_dst;
1612 if (!data->whole_window)
1614 OffsetRect( &dst_rect, -data->window_rect.left, -data->window_rect.top );
1615 parent = GetAncestor( data->hwnd, GA_PARENT );
1616 hdc_src = GetDCEx( parent, 0, DCX_CACHE );
1617 hdc_dst = GetDCEx( data->hwnd, 0, DCX_CACHE | DCX_WINDOW );
1621 OffsetRect( &dst_rect, -data->client_rect.left, -data->client_rect.top );
1622 /* make src rect relative to the old position of the window */
1623 OffsetRect( &src_rect, -old_client_rect->left, -old_client_rect->top );
1624 if (dst_rect.left == src_rect.left && dst_rect.top == src_rect.top) return;
1625 hdc_src = hdc_dst = GetDCEx( data->hwnd, 0, DCX_CACHE );
1628 ExcludeUpdateRgn( hdc_dst, data->hwnd );
1630 code = X11DRV_START_EXPOSURES;
1631 ExtEscape( hdc_dst, X11DRV_ESCAPE, sizeof(code), (LPSTR)&code, 0, NULL );
1633 TRACE( "copying bits for win %p/%lx/%lx %s -> %s\n",
1634 data->hwnd, data->whole_window, data->client_window,
1635 wine_dbgstr_rect(&src_rect), wine_dbgstr_rect(&dst_rect) );
1636 BitBlt( hdc_dst, dst_rect.left, dst_rect.top,
1637 dst_rect.right - dst_rect.left, dst_rect.bottom - dst_rect.top,
1638 hdc_src, src_rect.left, src_rect.top, SRCCOPY );
1640 code = X11DRV_END_EXPOSURES;
1641 ExtEscape( hdc_dst, X11DRV_ESCAPE, sizeof(code), (LPSTR)&code, sizeof(rgn), (LPSTR)&rgn );
1643 ReleaseDC( data->hwnd, hdc_dst );
1644 if (hdc_src != hdc_dst) ReleaseDC( parent, hdc_src );
1648 if (!data->whole_window)
1650 /* map region to client rect since we are using DCX_WINDOW */
1651 OffsetRgn( rgn, data->window_rect.left - data->client_rect.left,
1652 data->window_rect.top - data->client_rect.top );
1653 RedrawWindow( data->hwnd, NULL, rgn,
1654 RDW_INVALIDATE | RDW_FRAME | RDW_ERASE | RDW_ALLCHILDREN );
1656 else RedrawWindow( data->hwnd, NULL, rgn, RDW_INVALIDATE | RDW_ERASE | RDW_ALLCHILDREN );
1657 DeleteObject( rgn );
1662 /**********************************************************************
1663 * create_whole_window
1665 * Create the whole X window for a given window
1667 static Window create_whole_window( Display *display, struct x11drv_win_data *data )
1670 XSetWindowAttributes attr;
1674 DWORD layered_flags;
1677 if (!data->managed && is_window_managed( data->hwnd, SWP_NOACTIVATE, &data->window_rect ))
1679 TRACE( "making win %p/%lx managed\n", data->hwnd, data->whole_window );
1680 data->managed = TRUE;
1681 SetPropA( data->hwnd, managed_prop, (HANDLE)1 );
1684 if ((win_rgn = CreateRectRgn( 0, 0, 0, 0 )) &&
1685 GetWindowRgn( data->hwnd, win_rgn ) == ERROR)
1687 DeleteObject( win_rgn );
1690 data->shaped = (win_rgn != 0);
1692 mask = get_window_attributes( display, data, &attr );
1694 data->whole_rect = data->window_rect;
1695 X11DRV_window_to_X_rect( data, &data->whole_rect );
1696 if (!(cx = data->whole_rect.right - data->whole_rect.left)) cx = 1;
1697 else if (cx > 65535) cx = 65535;
1698 if (!(cy = data->whole_rect.bottom - data->whole_rect.top)) cy = 1;
1699 else if (cy > 65535) cy = 65535;
1702 data->whole_window = XCreateWindow( display, root_window,
1703 data->whole_rect.left - virtual_screen_rect.left,
1704 data->whole_rect.top - virtual_screen_rect.top,
1705 cx, cy, 0, screen_depth, InputOutput,
1706 visual, mask, &attr );
1708 if (data->whole_window) XSaveContext( display, data->whole_window, winContext, (char *)data->hwnd );
1709 wine_tsx11_unlock();
1711 if (!data->whole_window) goto done;
1713 if (!create_client_window( display, data, NULL ))
1716 XDeleteContext( display, data->whole_window, winContext );
1717 XDestroyWindow( display, data->whole_window );
1718 data->whole_window = 0;
1719 wine_tsx11_unlock();
1723 set_initial_wm_hints( display, data );
1724 set_wm_hints( display, data );
1726 SetPropA( data->hwnd, whole_window_prop, (HANDLE)data->whole_window );
1728 /* set the window text */
1729 if (!InternalGetWindowText( data->hwnd, text, sizeof(text)/sizeof(WCHAR) )) text[0] = 0;
1730 sync_window_text( display, data->whole_window, text );
1732 /* set the window region */
1733 if (win_rgn || IsRectEmpty( &data->window_rect )) sync_window_region( display, data, win_rgn );
1735 /* set the window opacity */
1736 if (!GetLayeredWindowAttributes( data->hwnd, &key, &alpha, &layered_flags )) layered_flags = 0;
1737 sync_window_opacity( display, data->whole_window, key, alpha, layered_flags );
1740 XFlush( display ); /* make sure the window exists before we start painting to it */
1741 wine_tsx11_unlock();
1743 sync_window_cursor( data );
1745 if (win_rgn) DeleteObject( win_rgn );
1746 return data->whole_window;
1750 /**********************************************************************
1751 * destroy_whole_window
1753 * Destroy the whole X window for a given window.
1755 static void destroy_whole_window( Display *display, struct x11drv_win_data *data, BOOL already_destroyed )
1757 if (!data->whole_window)
1761 Window xwin = (Window)GetPropA( data->hwnd, foreign_window_prop );
1765 if (!already_destroyed) XSelectInput( display, xwin, 0 );
1766 XDeleteContext( display, xwin, winContext );
1767 wine_tsx11_unlock();
1768 RemovePropA( data->hwnd, foreign_window_prop );
1775 TRACE( "win %p xwin %lx/%lx\n", data->hwnd, data->whole_window, data->client_window );
1777 XDeleteContext( display, data->whole_window, winContext );
1778 XDeleteContext( display, data->client_window, winContext );
1779 if (!already_destroyed) XDestroyWindow( display, data->whole_window );
1780 data->whole_window = data->client_window = 0;
1781 data->wm_state = WithdrawnState;
1782 data->net_wm_state = 0;
1783 data->mapped = FALSE;
1786 XUnsetICFocus( data->xic );
1787 XDestroyIC( data->xic );
1790 /* Outlook stops processing messages after destroying a dialog, so we need an explicit flush */
1792 XFree( data->wm_hints );
1793 data->wm_hints = NULL;
1794 wine_tsx11_unlock();
1795 RemovePropA( data->hwnd, whole_window_prop );
1796 RemovePropA( data->hwnd, client_window_prop );
1800 /*****************************************************************
1801 * SetWindowText (X11DRV.@)
1803 void CDECL X11DRV_SetWindowText( HWND hwnd, LPCWSTR text )
1807 if ((win = X11DRV_get_whole_window( hwnd )) && win != DefaultRootWindow(gdi_display))
1809 Display *display = thread_init_display();
1810 sync_window_text( display, win, text );
1815 /***********************************************************************
1816 * SetWindowStyle (X11DRV.@)
1818 * Update the X state of a window to reflect a style change
1820 void CDECL X11DRV_SetWindowStyle( HWND hwnd, INT offset, STYLESTRUCT *style )
1822 struct x11drv_win_data *data;
1825 if (hwnd == GetDesktopWindow()) return;
1826 changed = style->styleNew ^ style->styleOld;
1828 if (offset == GWL_STYLE && (changed & WS_VISIBLE) && (style->styleNew & WS_VISIBLE))
1830 /* we don't unmap windows, that causes trouble with the window manager */
1831 if (!(data = X11DRV_get_win_data( hwnd )) &&
1832 !(data = X11DRV_create_win_data( hwnd ))) return;
1834 if (data->whole_window && is_window_rect_mapped( &data->window_rect ))
1836 Display *display = thread_display();
1837 set_wm_hints( display, data );
1838 if (!data->mapped) map_window( display, data, style->styleNew );
1842 if (offset == GWL_STYLE && (changed & WS_DISABLED))
1844 data = X11DRV_get_win_data( hwnd );
1845 if (data && data->whole_window)
1846 set_wm_hints( thread_display(), data );
1849 if (offset == GWL_EXSTYLE && (changed & WS_EX_LAYERED))
1851 /* changing WS_EX_LAYERED resets attributes */
1852 if ((data = X11DRV_get_win_data( hwnd )) && data->whole_window)
1853 sync_window_opacity( thread_display(), data->whole_window, 0, 0, 0 );
1858 /***********************************************************************
1859 * DestroyWindow (X11DRV.@)
1861 void CDECL X11DRV_DestroyWindow( HWND hwnd )
1863 struct x11drv_thread_data *thread_data = x11drv_thread_data();
1864 struct x11drv_win_data *data;
1866 if (!(data = X11DRV_get_win_data( hwnd ))) return;
1871 destroy_glxpixmap(gdi_display, data->gl_drawable);
1872 XFreePixmap(gdi_display, data->pixmap);
1873 wine_tsx11_unlock();
1875 else if (data->gl_drawable)
1878 XDestroyWindow(gdi_display, data->gl_drawable);
1879 wine_tsx11_unlock();
1882 destroy_whole_window( thread_data->display, data, FALSE );
1883 destroy_icon_window( thread_data->display, data );
1888 XFreeColormap( thread_data->display, data->colormap );
1889 wine_tsx11_unlock();
1892 if (thread_data->last_focus == hwnd) thread_data->last_focus = 0;
1893 if (thread_data->last_xic_hwnd == hwnd) thread_data->last_xic_hwnd = 0;
1894 if (data->hWMIconBitmap) DeleteObject( data->hWMIconBitmap );
1895 if (data->hWMIconMask) DeleteObject( data->hWMIconMask);
1897 XDeleteContext( thread_data->display, (XID)hwnd, win_data_context );
1898 wine_tsx11_unlock();
1899 HeapFree( GetProcessHeap(), 0, data );
1903 /***********************************************************************
1904 * X11DRV_DestroyNotify
1906 void X11DRV_DestroyNotify( HWND hwnd, XEvent *event )
1908 Display *display = event->xdestroywindow.display;
1909 struct x11drv_win_data *data;
1911 if (!(data = X11DRV_get_win_data( hwnd ))) return;
1912 if (!data->embedded) FIXME( "window %p/%lx destroyed from the outside\n", hwnd, data->whole_window );
1914 destroy_whole_window( display, data, TRUE );
1915 if (data->embedded) SendMessageW( hwnd, WM_CLOSE, 0, 0 );
1919 static struct x11drv_win_data *alloc_win_data( Display *display, HWND hwnd )
1921 struct x11drv_win_data *data;
1923 if ((data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*data))))
1927 if (!winContext) winContext = XUniqueContext();
1928 if (!win_data_context) win_data_context = XUniqueContext();
1929 XSaveContext( display, (XID)hwnd, win_data_context, (char *)data );
1930 wine_tsx11_unlock();
1936 /* initialize the desktop window id in the desktop manager process */
1937 static struct x11drv_win_data *create_desktop_win_data( Display *display, HWND hwnd )
1939 struct x11drv_win_data *data;
1941 if (!(data = alloc_win_data( display, hwnd ))) return NULL;
1942 data->whole_window = data->client_window = root_window;
1943 data->managed = TRUE;
1944 SetPropA( data->hwnd, managed_prop, (HANDLE)1 );
1945 SetPropA( data->hwnd, whole_window_prop, (HANDLE)root_window );
1946 SetPropA( data->hwnd, client_window_prop, (HANDLE)root_window );
1947 set_initial_wm_hints( display, data );
1951 /**********************************************************************
1952 * CreateDesktopWindow (X11DRV.@)
1954 BOOL CDECL X11DRV_CreateDesktopWindow( HWND hwnd )
1956 unsigned int width, height;
1958 /* retrieve the real size of the desktop */
1959 SERVER_START_REQ( get_window_rectangles )
1961 req->handle = wine_server_user_handle( hwnd );
1962 req->relative = COORDS_CLIENT;
1963 wine_server_call( req );
1964 width = reply->window.right;
1965 height = reply->window.bottom;
1969 if (!width && !height) /* not initialized yet */
1971 SERVER_START_REQ( set_window_pos )
1973 req->handle = wine_server_user_handle( hwnd );
1975 req->flags = SWP_NOZORDER;
1976 req->window.left = virtual_screen_rect.left;
1977 req->window.top = virtual_screen_rect.top;
1978 req->window.right = virtual_screen_rect.right;
1979 req->window.bottom = virtual_screen_rect.bottom;
1980 req->client = req->window;
1981 wine_server_call( req );
1987 Window win = (Window)GetPropA( hwnd, whole_window_prop );
1988 if (win && win != root_window) X11DRV_init_desktop( win, width, height );
1994 /**********************************************************************
1995 * CreateWindow (X11DRV.@)
1997 BOOL CDECL X11DRV_CreateWindow( HWND hwnd )
1999 if (hwnd == GetDesktopWindow() && root_window != DefaultRootWindow( gdi_display ))
2001 Display *display = thread_init_display();
2003 /* the desktop win data can't be created lazily */
2004 if (!create_desktop_win_data( display, hwnd )) return FALSE;
2010 /***********************************************************************
2011 * X11DRV_get_win_data
2013 * Return the X11 data structure associated with a window.
2015 struct x11drv_win_data *X11DRV_get_win_data( HWND hwnd )
2017 struct x11drv_thread_data *thread_data = x11drv_thread_data();
2020 if (!thread_data) return NULL;
2021 if (!hwnd) return NULL;
2022 if (XFindContext( thread_data->display, (XID)hwnd, win_data_context, &data )) data = NULL;
2023 return (struct x11drv_win_data *)data;
2027 /***********************************************************************
2028 * X11DRV_create_win_data
2030 * Create an X11 data window structure for an existing window.
2032 struct x11drv_win_data *X11DRV_create_win_data( HWND hwnd )
2035 struct x11drv_win_data *data;
2038 if (!(parent = GetAncestor( hwnd, GA_PARENT ))) return NULL; /* desktop */
2040 /* don't create win data for HWND_MESSAGE windows */
2041 if (parent != GetDesktopWindow() && !GetAncestor( parent, GA_PARENT )) return NULL;
2043 display = thread_init_display();
2044 if (!(data = alloc_win_data( display, hwnd ))) return NULL;
2046 GetWindowRect( hwnd, &data->window_rect );
2047 MapWindowPoints( 0, parent, (POINT *)&data->window_rect, 2 );
2048 data->whole_rect = data->window_rect;
2049 GetClientRect( hwnd, &data->client_rect );
2050 MapWindowPoints( hwnd, parent, (POINT *)&data->client_rect, 2 );
2052 if (parent == GetDesktopWindow())
2054 if (!create_whole_window( display, data ))
2056 HeapFree( GetProcessHeap(), 0, data );
2059 TRACE( "win %p/%lx/%lx window %s whole %s client %s\n",
2060 hwnd, data->whole_window, data->client_window, wine_dbgstr_rect( &data->window_rect ),
2061 wine_dbgstr_rect( &data->whole_rect ), wine_dbgstr_rect( &data->client_rect ));
2067 /* window procedure for foreign windows */
2068 static LRESULT WINAPI foreign_window_proc( HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam )
2072 case WM_PARENTNOTIFY:
2073 if (LOWORD(wparam) == WM_DESTROY)
2075 TRACE( "%p: got parent notify destroy for win %lx\n", hwnd, lparam );
2076 PostMessageW( hwnd, WM_CLOSE, 0, 0 ); /* so that we come back here once the child is gone */
2080 if (GetWindow( hwnd, GW_CHILD )) return 0; /* refuse to die if we still have children */
2083 return DefWindowProcW( hwnd, msg, wparam, lparam );
2087 /***********************************************************************
2088 * create_foreign_window
2090 * Create a foreign window for the specified X window and its ancestors
2092 HWND create_foreign_window( Display *display, Window xwin )
2094 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};
2095 static BOOL class_registered;
2096 struct x11drv_win_data *data;
2098 Window xparent, xroot;
2100 unsigned int nchildren;
2101 XWindowAttributes attr;
2102 DWORD style = WS_CLIPCHILDREN;
2104 if (!class_registered)
2108 memset( &class, 0, sizeof(class) );
2109 class.cbSize = sizeof(class);
2110 class.lpfnWndProc = foreign_window_proc;
2111 class.lpszClassName = classW;
2112 if (!RegisterClassExW( &class ) && GetLastError() != ERROR_CLASS_ALREADY_EXISTS)
2114 ERR( "Could not register foreign window class\n" );
2117 class_registered = TRUE;
2121 if (XFindContext( display, xwin, winContext, (char **)&hwnd )) hwnd = 0;
2122 if (hwnd) /* already created */
2124 wine_tsx11_unlock();
2128 XSelectInput( display, xwin, StructureNotifyMask );
2129 if (!XGetWindowAttributes( display, xwin, &attr ) ||
2130 !XQueryTree( display, xwin, &xroot, &xparent, &xchildren, &nchildren ))
2132 XSelectInput( display, xwin, 0 );
2133 wine_tsx11_unlock();
2137 wine_tsx11_unlock();
2139 if (xparent == xroot)
2141 parent = GetDesktopWindow();
2143 attr.x += virtual_screen_rect.left;
2144 attr.y += virtual_screen_rect.top;
2148 parent = create_foreign_window( display, xparent );
2152 hwnd = CreateWindowW( classW, NULL, style, attr.x, attr.y, attr.width, attr.height,
2153 parent, 0, 0, NULL );
2155 if (!(data = alloc_win_data( display, hwnd )))
2157 DestroyWindow( hwnd );
2160 SetRect( &data->window_rect, attr.x, attr.y, attr.x + attr.width, attr.y + attr.height );
2161 data->whole_rect = data->client_rect = data->window_rect;
2162 data->whole_window = data->client_window = 0;
2163 data->embedded = TRUE;
2164 data->mapped = TRUE;
2166 SetPropA( hwnd, foreign_window_prop, (HANDLE)xwin );
2168 XSaveContext( display, xwin, winContext, (char *)data->hwnd );
2169 wine_tsx11_unlock();
2171 ShowWindow( hwnd, SW_SHOW );
2173 TRACE( "win %lx parent %p style %08x %s -> hwnd %p\n",
2174 xwin, parent, style, wine_dbgstr_rect(&data->window_rect), hwnd );
2180 /***********************************************************************
2181 * X11DRV_get_whole_window
2183 * Return the X window associated with the full area of a window
2185 Window X11DRV_get_whole_window( HWND hwnd )
2187 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
2191 if (hwnd == GetDesktopWindow()) return root_window;
2192 return (Window)GetPropA( hwnd, whole_window_prop );
2194 return data->whole_window;
2198 /***********************************************************************
2199 * X11DRV_get_client_window
2201 * Return the X window associated with the client area of a window
2203 static Window X11DRV_get_client_window( HWND hwnd )
2205 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
2209 if (hwnd == GetDesktopWindow()) return root_window;
2210 return (Window)GetPropA( hwnd, client_window_prop );
2212 return data->client_window;
2216 /***********************************************************************
2219 * Return the X input context associated with a window
2221 XIC X11DRV_get_ic( HWND hwnd )
2223 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
2226 if (!data) return 0;
2228 x11drv_thread_data()->last_xic_hwnd = hwnd;
2229 if (data->xic) return data->xic;
2230 if (!(xim = x11drv_thread_data()->xim)) return 0;
2231 return X11DRV_CreateIC( xim, data );
2235 /***********************************************************************
2236 * X11DRV_GetDC (X11DRV.@)
2238 void CDECL X11DRV_GetDC( HDC hdc, HWND hwnd, HWND top, const RECT *win_rect,
2239 const RECT *top_rect, DWORD flags )
2241 struct x11drv_escape_set_drawable escape;
2242 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
2245 escape.code = X11DRV_SET_DRAWABLE;
2246 escape.mode = IncludeInferiors;
2247 escape.fbconfig_id = 0;
2248 escape.gl_drawable = 0;
2250 escape.gl_copy = FALSE;
2252 escape.dc_rect.left = win_rect->left - top_rect->left;
2253 escape.dc_rect.top = win_rect->top - top_rect->top;
2254 escape.dc_rect.right = win_rect->right - top_rect->left;
2255 escape.dc_rect.bottom = win_rect->bottom - top_rect->top;
2256 escape.drawable_rect.left = top_rect->left;
2257 escape.drawable_rect.top = top_rect->top;
2258 escape.drawable_rect.right = top_rect->right;
2259 escape.drawable_rect.bottom = top_rect->bottom;
2263 escape.fbconfig_id = data ? data->fbconfig_id : (XID)GetPropA( hwnd, fbconfig_id_prop );
2264 /* GL draws to the client area even for window DCs */
2265 escape.gl_drawable = data ? data->client_window : X11DRV_get_client_window( hwnd );
2266 if (data && IsIconic( hwnd ) && data->icon_window)
2268 escape.drawable = data->icon_window;
2270 else if (flags & DCX_WINDOW)
2271 escape.drawable = data ? data->whole_window : X11DRV_get_whole_window( hwnd );
2273 escape.drawable = escape.gl_drawable;
2277 /* find the first ancestor that has a drawable */
2278 for (parent = hwnd; parent && parent != top; parent = GetAncestor( parent, GA_PARENT ))
2279 if ((escape.drawable = X11DRV_get_client_window( parent ))) break;
2281 if (escape.drawable)
2283 POINT pt = { 0, 0 };
2284 MapWindowPoints( top, parent, &pt, 1 );
2285 OffsetRect( &escape.dc_rect, pt.x, pt.y );
2286 OffsetRect( &escape.drawable_rect, -pt.x, -pt.y );
2288 else escape.drawable = X11DRV_get_client_window( top );
2290 escape.fbconfig_id = data ? data->fbconfig_id : (XID)GetPropA( hwnd, fbconfig_id_prop );
2291 escape.gl_drawable = data ? data->gl_drawable : (Drawable)GetPropA( hwnd, gl_drawable_prop );
2292 escape.pixmap = data ? data->pixmap : (Pixmap)GetPropA( hwnd, pixmap_prop );
2293 escape.gl_copy = (escape.gl_drawable != 0);
2294 if (flags & DCX_CLIPCHILDREN) escape.mode = ClipByChildren;
2297 ExtEscape( hdc, X11DRV_ESCAPE, sizeof(escape), (LPSTR)&escape, 0, NULL );
2301 /***********************************************************************
2302 * X11DRV_ReleaseDC (X11DRV.@)
2304 void CDECL X11DRV_ReleaseDC( HWND hwnd, HDC hdc )
2306 struct x11drv_escape_set_drawable escape;
2308 escape.code = X11DRV_SET_DRAWABLE;
2309 escape.drawable = root_window;
2310 escape.mode = IncludeInferiors;
2311 escape.drawable_rect = virtual_screen_rect;
2312 SetRect( &escape.dc_rect, 0, 0, virtual_screen_rect.right - virtual_screen_rect.left,
2313 virtual_screen_rect.bottom - virtual_screen_rect.top );
2314 OffsetRect( &escape.dc_rect, -escape.drawable_rect.left, -escape.drawable_rect.top );
2315 escape.fbconfig_id = 0;
2316 escape.gl_drawable = 0;
2318 ExtEscape( hdc, X11DRV_ESCAPE, sizeof(escape), (LPSTR)&escape, 0, NULL );
2322 /***********************************************************************
2323 * SetCapture (X11DRV.@)
2325 void CDECL X11DRV_SetCapture( HWND hwnd, UINT flags )
2327 struct x11drv_thread_data *thread_data = x11drv_thread_data();
2329 if (!thread_data) return;
2330 if (!(flags & (GUI_INMOVESIZE | GUI_INMENUMODE))) return;
2334 Window grab_win = X11DRV_get_client_window( GetAncestor( hwnd, GA_ROOT ) );
2336 if (!grab_win) return;
2338 XFlush( gdi_display );
2339 XGrabPointer( thread_data->display, grab_win, False,
2340 PointerMotionMask | ButtonPressMask | ButtonReleaseMask,
2341 GrabModeAsync, GrabModeAsync, None, None, CurrentTime );
2342 wine_tsx11_unlock();
2343 thread_data->grab_window = grab_win;
2345 else /* release capture */
2348 XFlush( gdi_display );
2349 XUngrabPointer( thread_data->display, CurrentTime );
2350 XFlush( thread_data->display );
2351 wine_tsx11_unlock();
2352 thread_data->grab_window = None;
2357 /*****************************************************************
2358 * SetParent (X11DRV.@)
2360 void CDECL X11DRV_SetParent( HWND hwnd, HWND parent, HWND old_parent )
2362 Display *display = thread_display();
2363 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
2366 if (parent == old_parent) return;
2367 if (data->embedded) return;
2369 if (parent != GetDesktopWindow()) /* a child window */
2371 if (old_parent == GetDesktopWindow())
2373 /* destroy the old X windows */
2374 destroy_whole_window( display, data, FALSE );
2375 destroy_icon_window( display, data );
2378 data->managed = FALSE;
2379 RemovePropA( data->hwnd, managed_prop );
2383 else /* new top level window */
2385 /* FIXME: we ignore errors since we can't really recover anyway */
2386 create_whole_window( display, data );
2391 /*****************************************************************
2392 * SetFocus (X11DRV.@)
2396 void CDECL X11DRV_SetFocus( HWND hwnd )
2398 Display *display = thread_display();
2399 struct x11drv_win_data *data;
2400 XWindowChanges changes;
2403 if (!(hwnd = GetAncestor( hwnd, GA_ROOT ))) return;
2404 if (!(data = X11DRV_get_win_data( hwnd ))) return;
2405 if (data->managed || !data->whole_window) return;
2407 if (EVENT_x11_time_to_win32_time(0))
2408 /* ICCCM says don't use CurrentTime, so try to use last message time if possible */
2409 /* FIXME: this is not entirely correct */
2410 timestamp = GetMessageTime() - EVENT_x11_time_to_win32_time(0);
2412 timestamp = CurrentTime;
2414 /* Set X focus and install colormap */
2416 changes.stack_mode = Above;
2417 XConfigureWindow( display, data->whole_window, CWStackMode, &changes );
2418 XSetInputFocus( display, data->whole_window, RevertToParent, timestamp );
2419 wine_tsx11_unlock();
2423 /***********************************************************************
2424 * WindowPosChanging (X11DRV.@)
2426 void CDECL X11DRV_WindowPosChanging( HWND hwnd, HWND insert_after, UINT swp_flags,
2427 const RECT *window_rect, const RECT *client_rect, RECT *visible_rect )
2429 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
2430 DWORD style = GetWindowLongW( hwnd, GWL_STYLE );
2434 /* create the win data if the window is being made visible */
2435 if (!(style & WS_VISIBLE) && !(swp_flags & SWP_SHOWWINDOW)) return;
2436 if (!(data = X11DRV_create_win_data( hwnd ))) return;
2439 /* check if we need to switch the window to managed */
2440 if (!data->managed && data->whole_window && is_window_managed( hwnd, swp_flags, window_rect ))
2442 TRACE( "making win %p/%lx managed\n", hwnd, data->whole_window );
2443 if (data->mapped) unmap_window( thread_display(), data );
2444 data->managed = TRUE;
2445 SetPropA( hwnd, managed_prop, (HANDLE)1 );
2448 *visible_rect = *window_rect;
2449 X11DRV_window_to_X_rect( data, visible_rect );
2453 /***********************************************************************
2454 * WindowPosChanged (X11DRV.@)
2456 void CDECL X11DRV_WindowPosChanged( HWND hwnd, HWND insert_after, UINT swp_flags,
2457 const RECT *rectWindow, const RECT *rectClient,
2458 const RECT *visible_rect, const RECT *valid_rects )
2460 struct x11drv_thread_data *thread_data;
2462 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
2463 DWORD new_style = GetWindowLongW( hwnd, GWL_STYLE );
2464 RECT old_window_rect, old_whole_rect, old_client_rect;
2469 thread_data = x11drv_thread_data();
2470 display = thread_data->display;
2472 old_window_rect = data->window_rect;
2473 old_whole_rect = data->whole_rect;
2474 old_client_rect = data->client_rect;
2475 data->window_rect = *rectWindow;
2476 data->whole_rect = *visible_rect;
2477 data->client_rect = *rectClient;
2479 TRACE( "win %p window %s client %s style %08x flags %08x\n",
2480 hwnd, wine_dbgstr_rect(rectWindow), wine_dbgstr_rect(rectClient), new_style, swp_flags );
2482 if (!IsRectEmpty( &valid_rects[0] ))
2484 int x_offset = old_whole_rect.left - data->whole_rect.left;
2485 int y_offset = old_whole_rect.top - data->whole_rect.top;
2487 /* if all that happened is that the whole window moved, copy everything */
2488 if (!(swp_flags & SWP_FRAMECHANGED) &&
2489 old_whole_rect.right - data->whole_rect.right == x_offset &&
2490 old_whole_rect.bottom - data->whole_rect.bottom == y_offset &&
2491 old_client_rect.left - data->client_rect.left == x_offset &&
2492 old_client_rect.right - data->client_rect.right == x_offset &&
2493 old_client_rect.top - data->client_rect.top == y_offset &&
2494 old_client_rect.bottom - data->client_rect.bottom == y_offset &&
2495 !memcmp( &valid_rects[0], &data->client_rect, sizeof(RECT) ))
2497 /* if we have an X window the bits will be moved by the X server */
2498 if (!data->whole_window && (x_offset != 0 || y_offset != 0))
2499 move_window_bits( data, &old_whole_rect, &data->whole_rect, &old_client_rect );
2502 move_window_bits( data, &valid_rects[1], &valid_rects[0], &old_client_rect );
2506 XFlush( gdi_display ); /* make sure painting is done before we move the window */
2507 wine_tsx11_unlock();
2509 sync_client_position( display, data, swp_flags, &old_client_rect, &old_whole_rect );
2511 if (!data->whole_window) return;
2513 /* check if we are currently processing an event relevant to this window */
2515 if (thread_data->current_event && thread_data->current_event->xany.window == data->whole_window)
2516 event_type = thread_data->current_event->type;
2518 if (event_type != ConfigureNotify && event_type != PropertyNotify &&
2519 event_type != GravityNotify && event_type != ReparentNotify)
2520 event_type = 0; /* ignore other events */
2522 if (data->mapped && event_type != ReparentNotify)
2524 if (((swp_flags & SWP_HIDEWINDOW) && !(new_style & WS_VISIBLE)) ||
2525 (event_type != ConfigureNotify &&
2526 !is_window_rect_mapped( rectWindow ) && is_window_rect_mapped( &old_window_rect )))
2527 unmap_window( display, data );
2530 /* don't change position if we are about to minimize or maximize a managed window */
2532 !(data->managed && (swp_flags & SWP_STATECHANGED) && (new_style & (WS_MINIMIZE|WS_MAXIMIZE))))
2533 sync_window_position( display, data, swp_flags,
2534 &old_window_rect, &old_whole_rect, &old_client_rect );
2536 if ((new_style & WS_VISIBLE) &&
2537 ((new_style & WS_MINIMIZE) || is_window_rect_mapped( rectWindow )))
2539 if (!data->mapped || (swp_flags & (SWP_FRAMECHANGED|SWP_STATECHANGED)))
2540 set_wm_hints( display, data );
2544 map_window( display, data, new_style );
2546 else if ((swp_flags & SWP_STATECHANGED) && (!data->iconic != !(new_style & WS_MINIMIZE)))
2548 data->iconic = (new_style & WS_MINIMIZE) != 0;
2549 TRACE( "changing win %p iconic state to %u\n", data->hwnd, data->iconic );
2552 XIconifyWindow( display, data->whole_window, DefaultScreen(display) );
2553 else if (is_window_rect_mapped( rectWindow ))
2554 XMapWindow( display, data->whole_window );
2555 wine_tsx11_unlock();
2556 update_net_wm_states( display, data );
2558 else if (!event_type)
2560 update_net_wm_states( display, data );
2565 XFlush( display ); /* make sure changes are done before we start painting again */
2566 wine_tsx11_unlock();
2570 /***********************************************************************
2571 * ShowWindow (X11DRV.@)
2573 UINT CDECL X11DRV_ShowWindow( HWND hwnd, INT cmd, RECT *rect, UINT swp )
2576 unsigned int width, height, border, depth;
2578 DWORD style = GetWindowLongW( hwnd, GWL_STYLE );
2579 struct x11drv_thread_data *thread_data = x11drv_thread_data();
2580 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
2582 if (!data || !data->whole_window || !data->managed || !data->mapped || data->iconic) return swp;
2583 if (style & WS_MINIMIZE) return swp;
2584 if (IsRectEmpty( rect )) return swp;
2586 /* only fetch the new rectangle if the ShowWindow was a result of a window manager event */
2588 if (!thread_data->current_event || thread_data->current_event->xany.window != data->whole_window)
2591 if (thread_data->current_event->type != ConfigureNotify &&
2592 thread_data->current_event->type != PropertyNotify)
2595 TRACE( "win %p/%lx cmd %d at %s flags %08x\n",
2596 hwnd, data->whole_window, cmd, wine_dbgstr_rect(rect), swp );
2599 XGetGeometry( thread_data->display, data->whole_window,
2600 &root, &x, &y, &width, &height, &border, &depth );
2601 XTranslateCoordinates( thread_data->display, data->whole_window, root, 0, 0, &x, &y, &top );
2602 wine_tsx11_unlock();
2605 rect->right = x + width;
2606 rect->bottom = y + height;
2607 OffsetRect( rect, virtual_screen_rect.left, virtual_screen_rect.top );
2608 X11DRV_X_to_window_rect( data, rect );
2609 return swp & ~(SWP_NOMOVE | SWP_NOCLIENTMOVE | SWP_NOSIZE | SWP_NOCLIENTSIZE);
2613 /**********************************************************************
2614 * SetWindowIcon (X11DRV.@)
2616 * hIcon or hIconSm has changed (or is being initialised for the
2617 * first time). Complete the X11 driver-specific initialisation
2618 * and set the window hints.
2620 * This is not entirely correct, may need to create
2621 * an icon window and set the pixmap as a background
2623 void CDECL X11DRV_SetWindowIcon( HWND hwnd, UINT type, HICON icon )
2625 Display *display = thread_display();
2626 struct x11drv_win_data *data;
2629 if (!(data = X11DRV_get_win_data( hwnd ))) return;
2630 if (!data->whole_window) return;
2631 if (!data->managed) return;
2635 if (type == ICON_BIG) set_icon_hints( display, data, icon, 0 );
2636 else set_icon_hints( display, data, 0, icon );
2638 XSetWMHints( display, data->whole_window, data->wm_hints );
2639 wine_tsx11_unlock();
2644 /***********************************************************************
2645 * SetWindowRgn (X11DRV.@)
2647 * Assign specified region to window (for non-rectangular windows)
2649 int CDECL X11DRV_SetWindowRgn( HWND hwnd, HRGN hrgn, BOOL redraw )
2651 struct x11drv_win_data *data;
2653 if ((data = X11DRV_get_win_data( hwnd )))
2655 sync_window_region( thread_display(), data, hrgn );
2657 else if (X11DRV_get_whole_window( hwnd ))
2659 SendMessageW( hwnd, WM_X11DRV_SET_WIN_REGION, 0, 0 );
2665 /***********************************************************************
2666 * SetLayeredWindowAttributes (X11DRV.@)
2668 * Set transparency attributes for a layered window.
2670 void CDECL X11DRV_SetLayeredWindowAttributes( HWND hwnd, COLORREF key, BYTE alpha, DWORD flags )
2672 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
2676 if (data->whole_window)
2677 sync_window_opacity( thread_display(), data->whole_window, key, alpha, flags );
2681 Window win = X11DRV_get_whole_window( hwnd );
2682 if (win) sync_window_opacity( gdi_display, win, key, alpha, flags );
2687 /**********************************************************************
2688 * X11DRV_WindowMessage (X11DRV.@)
2690 LRESULT CDECL X11DRV_WindowMessage( HWND hwnd, UINT msg, WPARAM wp, LPARAM lp )
2692 struct x11drv_win_data *data;
2696 case WM_X11DRV_ACQUIRE_SELECTION:
2697 return X11DRV_AcquireClipboard( hwnd );
2698 case WM_X11DRV_SET_WIN_FORMAT:
2699 return set_win_format( hwnd, (XID)wp );
2700 case WM_X11DRV_SET_WIN_REGION:
2701 if ((data = X11DRV_get_win_data( hwnd ))) sync_window_region( thread_display(), data, (HRGN)1 );
2703 case WM_X11DRV_RESIZE_DESKTOP:
2704 X11DRV_resize_desktop( LOWORD(lp), HIWORD(lp) );
2706 case WM_X11DRV_SET_CURSOR:
2707 set_window_cursor( hwnd, (HCURSOR)lp );
2710 FIXME( "got window msg %x hwnd %p wp %lx lp %lx\n", msg, hwnd, wp, lp );
2716 /***********************************************************************
2717 * is_netwm_supported
2719 static BOOL is_netwm_supported( Display *display, Atom atom )
2721 static Atom *net_supported;
2722 static int net_supported_count = -1;
2726 if (net_supported_count == -1)
2730 unsigned long count, remaining;
2732 if (!XGetWindowProperty( display, DefaultRootWindow(display), x11drv_atom(_NET_SUPPORTED), 0,
2733 ~0UL, False, XA_ATOM, &type, &format, &count,
2734 &remaining, (unsigned char **)&net_supported ))
2735 net_supported_count = get_property_size( format, count ) / sizeof(Atom);
2737 net_supported_count = 0;
2739 wine_tsx11_unlock();
2741 for (i = 0; i < net_supported_count; i++)
2742 if (net_supported[i] == atom) return TRUE;
2747 /***********************************************************************
2748 * SysCommand (X11DRV.@)
2750 * Perform WM_SYSCOMMAND handling.
2752 LRESULT CDECL X11DRV_SysCommand( HWND hwnd, WPARAM wparam, LPARAM lparam )
2754 WPARAM hittest = wparam & 0x0f;
2758 Display *display = thread_display();
2759 struct x11drv_win_data *data;
2761 if (!(data = X11DRV_get_win_data( hwnd ))) return -1;
2762 if (!data->whole_window || !data->managed || !data->mapped) return -1;
2764 switch (wparam & 0xfff0)
2767 if (!hittest) dir = _NET_WM_MOVERESIZE_MOVE_KEYBOARD;
2768 else dir = _NET_WM_MOVERESIZE_MOVE;
2771 /* windows without WS_THICKFRAME are not resizable through the window manager */
2772 if (!(GetWindowLongW( hwnd, GWL_STYLE ) & WS_THICKFRAME)) return -1;
2776 case WMSZ_LEFT: dir = _NET_WM_MOVERESIZE_SIZE_LEFT; break;
2777 case WMSZ_RIGHT: dir = _NET_WM_MOVERESIZE_SIZE_RIGHT; break;
2778 case WMSZ_TOP: dir = _NET_WM_MOVERESIZE_SIZE_TOP; break;
2779 case WMSZ_TOPLEFT: dir = _NET_WM_MOVERESIZE_SIZE_TOPLEFT; break;
2780 case WMSZ_TOPRIGHT: dir = _NET_WM_MOVERESIZE_SIZE_TOPRIGHT; break;
2781 case WMSZ_BOTTOM: dir = _NET_WM_MOVERESIZE_SIZE_BOTTOM; break;
2782 case WMSZ_BOTTOMLEFT: dir = _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT; break;
2783 case WMSZ_BOTTOMRIGHT: dir = _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT; break;
2784 default: dir = _NET_WM_MOVERESIZE_SIZE_KEYBOARD; break;
2789 /* prevent a simple ALT press+release from activating the system menu,
2790 * as that can get confusing on managed windows */
2791 if ((WCHAR)lparam) return -1; /* got an explicit char */
2792 if (GetMenu( hwnd )) return -1; /* window has a real menu */
2793 if (!(GetWindowLongW( hwnd, GWL_STYLE ) & WS_SYSMENU)) return -1; /* no system menu */
2794 TRACE( "ignoring SC_KEYMENU wp %lx lp %lx\n", wparam, lparam );
2801 if (IsZoomed(hwnd)) return -1;
2803 if (!is_netwm_supported( display, x11drv_atom(_NET_WM_MOVERESIZE) ))
2805 TRACE( "_NET_WM_MOVERESIZE not supported\n" );
2809 dwPoint = GetMessagePos();
2810 x = (short)LOWORD(dwPoint);
2811 y = (short)HIWORD(dwPoint);
2813 TRACE("hwnd %p, x %d, y %d, dir %d\n", hwnd, x, y, dir);
2815 xev.xclient.type = ClientMessage;
2816 xev.xclient.window = X11DRV_get_whole_window(hwnd);
2817 xev.xclient.message_type = x11drv_atom(_NET_WM_MOVERESIZE);
2818 xev.xclient.serial = 0;
2819 xev.xclient.display = display;
2820 xev.xclient.send_event = True;
2821 xev.xclient.format = 32;
2822 xev.xclient.data.l[0] = x - virtual_screen_rect.left; /* x coord */
2823 xev.xclient.data.l[1] = y - virtual_screen_rect.top; /* y coord */
2824 xev.xclient.data.l[2] = dir; /* direction */
2825 xev.xclient.data.l[3] = 1; /* button */
2826 xev.xclient.data.l[4] = 0; /* unused */
2828 /* need to ungrab the pointer that may have been automatically grabbed
2829 * with a ButtonPress event */
2831 XUngrabPointer( display, CurrentTime );
2832 XSendEvent(display, root_window, False, SubstructureNotifyMask | SubstructureRedirectMask, &xev);
2833 wine_tsx11_unlock();