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 /* X context to associate a hwnd to an X window */
54 XContext winContext = 0;
56 /* X context to associate a struct x11drv_win_data to an hwnd */
57 static XContext win_data_context;
59 static const char whole_window_prop[] = "__wine_x11_whole_window";
60 static const char client_window_prop[]= "__wine_x11_client_window";
61 static const char icon_window_prop[] = "__wine_x11_icon_window";
62 static const char fbconfig_id_prop[] = "__wine_x11_fbconfig_id";
63 static const char gl_drawable_prop[] = "__wine_x11_gl_drawable";
64 static const char pixmap_prop[] = "__wine_x11_pixmap";
65 static const char managed_prop[] = "__wine_x11_managed";
67 /* for XDG systray icons */
68 #define SYSTEM_TRAY_REQUEST_DOCK 0
70 extern int usexcomposite;
72 /***********************************************************************
75 * Check if a given window should be managed
77 BOOL is_window_managed( HWND hwnd, UINT swp_flags, const RECT *window_rect )
79 DWORD style, ex_style;
81 if (!managed_mode) return FALSE;
83 /* child windows are not managed */
84 style = GetWindowLongW( hwnd, GWL_STYLE );
85 if ((style & (WS_CHILD|WS_POPUP)) == WS_CHILD) return FALSE;
86 /* activated windows are managed */
87 if (!(swp_flags & (SWP_NOACTIVATE|SWP_HIDEWINDOW))) return TRUE;
88 if (hwnd == GetActiveWindow()) return TRUE;
89 /* windows with caption are managed */
90 if ((style & WS_CAPTION) == WS_CAPTION) return TRUE;
91 /* tool windows are not managed */
92 ex_style = GetWindowLongW( hwnd, GWL_EXSTYLE );
93 if (ex_style & WS_EX_TOOLWINDOW) return FALSE;
94 /* windows with thick frame are managed */
95 if (style & WS_THICKFRAME) return TRUE;
96 /* application windows are managed */
97 if (ex_style & WS_EX_APPWINDOW) return TRUE;
100 /* popup with sysmenu == caption are managed */
101 if (style & WS_SYSMENU) return TRUE;
102 /* full-screen popup windows are managed */
103 if (window_rect->left <= 0 && window_rect->right >= screen_width &&
104 window_rect->top <= 0 && window_rect->bottom >= screen_height)
107 /* default: not managed */
112 /***********************************************************************
113 * X11DRV_is_window_rect_mapped
115 * Check if the X whole window should be mapped based on its rectangle
117 BOOL X11DRV_is_window_rect_mapped( const RECT *rect )
119 /* don't map if rect is empty */
120 if (IsRectEmpty( rect )) return FALSE;
122 /* don't map if rect is off-screen */
123 if (rect->left >= virtual_screen_rect.right ||
124 rect->top >= virtual_screen_rect.bottom ||
125 rect->right <= virtual_screen_rect.left ||
126 rect->bottom <= virtual_screen_rect.top)
133 /***********************************************************************
134 * get_mwm_decorations
136 static unsigned long get_mwm_decorations( DWORD style, DWORD ex_style )
138 unsigned long ret = 0;
140 if (ex_style & WS_EX_TOOLWINDOW) return 0;
142 if ((style & WS_CAPTION) == WS_CAPTION)
144 ret |= MWM_DECOR_TITLE | MWM_DECOR_BORDER;
145 if (style & WS_SYSMENU) ret |= MWM_DECOR_MENU;
146 if (style & WS_MINIMIZEBOX) ret |= MWM_DECOR_MINIMIZE;
147 if (style & WS_MAXIMIZEBOX) ret |= MWM_DECOR_MAXIMIZE;
149 if (ex_style & WS_EX_DLGMODALFRAME) ret |= MWM_DECOR_BORDER;
150 else if (style & WS_THICKFRAME) ret |= MWM_DECOR_BORDER | MWM_DECOR_RESIZEH;
151 else if ((style & (WS_DLGFRAME|WS_BORDER)) == WS_DLGFRAME) ret |= MWM_DECOR_BORDER;
156 /***********************************************************************
157 * get_x11_rect_offset
159 * Helper for X11DRV_window_to_X_rect and X11DRV_X_to_window_rect.
161 static void get_x11_rect_offset( struct x11drv_win_data *data, RECT *rect )
163 DWORD style, ex_style, style_mask = 0, ex_style_mask = 0;
166 rect->top = rect->bottom = rect->left = rect->right = 0;
168 style = GetWindowLongW( data->hwnd, GWL_STYLE );
169 ex_style = GetWindowLongW( data->hwnd, GWL_EXSTYLE );
170 decor = get_mwm_decorations( style, ex_style );
172 if (decor & MWM_DECOR_TITLE) style_mask |= WS_CAPTION;
173 if (decor & MWM_DECOR_BORDER)
175 style_mask |= WS_DLGFRAME | WS_THICKFRAME;
176 ex_style_mask |= WS_EX_DLGMODALFRAME;
179 AdjustWindowRectEx( rect, style & style_mask, FALSE, ex_style & ex_style_mask );
183 /***********************************************************************
184 * get_window_attributes
186 * Fill the window attributes structure for an X window.
188 static int get_window_attributes( Display *display, struct x11drv_win_data *data,
189 XSetWindowAttributes *attr )
191 attr->override_redirect = !data->managed;
192 attr->colormap = X11DRV_PALETTE_PaletteXColormap;
193 attr->save_under = ((GetClassLongW( data->hwnd, GCL_STYLE ) & CS_SAVEBITS) != 0);
194 attr->cursor = x11drv_thread_data()->cursor;
195 attr->bit_gravity = NorthWestGravity;
196 attr->backing_store = NotUseful;
197 attr->event_mask = (ExposureMask | PointerMotionMask |
198 ButtonPressMask | ButtonReleaseMask | EnterWindowMask |
199 KeyPressMask | KeyReleaseMask | FocusChangeMask | KeymapStateMask);
200 if (data->managed) attr->event_mask |= StructureNotifyMask | PropertyChangeMask;
202 return (CWOverrideRedirect | CWSaveUnder | CWColormap | CWCursor |
203 CWEventMask | CWBitGravity | CWBackingStore);
207 /***********************************************************************
208 * create_client_window
210 static Window create_client_window( Display *display, struct x11drv_win_data *data, XVisualInfo *vis )
213 XSetWindowAttributes attr;
216 attr.bit_gravity = NorthWestGravity;
217 attr.win_gravity = NorthWestGravity;
218 attr.backing_store = NotUseful;
219 attr.event_mask = (ExposureMask | PointerMotionMask |
220 ButtonPressMask | ButtonReleaseMask | EnterWindowMask);
221 mask = CWEventMask | CWBitGravity | CWWinGravity | CWBackingStore;
223 if ((cx = data->client_rect.right - data->client_rect.left) <= 0) cx = 1;
224 if ((cy = data->client_rect.bottom - data->client_rect.top) <= 0) cy = 1;
230 attr.colormap = XCreateColormap( display, root_window, vis->visual,
231 (vis->class == PseudoColor || vis->class == GrayScale ||
232 vis->class == DirectColor) ? AllocAll : AllocNone );
236 client = XCreateWindow( display, data->whole_window,
237 data->client_rect.left - data->whole_rect.left,
238 data->client_rect.top - data->whole_rect.top,
239 cx, cy, 0, screen_depth, InputOutput,
240 vis ? vis->visual : visual, mask, &attr );
247 if (data->client_window)
249 struct x11drv_thread_data *thread_data = x11drv_thread_data();
250 if (thread_data->cursor_window == data->client_window) thread_data->cursor_window = None;
251 XDeleteContext( display, data->client_window, winContext );
252 XDestroyWindow( display, data->client_window );
254 data->client_window = client;
256 if (data->colormap) XFreeColormap( display, data->colormap );
257 data->colormap = vis ? attr.colormap : 0;
259 XMapWindow( display, data->client_window );
260 XSaveContext( display, data->client_window, winContext, (char *)data->hwnd );
263 SetPropA( data->hwnd, client_window_prop, (HANDLE)data->client_window );
264 return data->client_window;
268 /***********************************************************************
269 * X11DRV_sync_window_style
271 * Change the X window attributes when the window style has changed.
273 void X11DRV_sync_window_style( Display *display, struct x11drv_win_data *data )
275 if (data->whole_window != root_window)
277 XSetWindowAttributes attr;
278 int mask = get_window_attributes( display, data, &attr );
281 XChangeWindowAttributes( display, data->whole_window, mask, &attr );
287 /***********************************************************************
290 * Update the X11 window region.
292 static void sync_window_region( Display *display, struct x11drv_win_data *data, HRGN hrgn )
294 #ifdef HAVE_LIBXSHAPE
295 if (!data->whole_window) return;
300 XShapeCombineMask( display, data->whole_window, ShapeBounding, 0, 0, None, ShapeSet );
305 RGNDATA *pRegionData = X11DRV_GetRegionData( hrgn, 0 );
309 XShapeCombineRectangles( display, data->whole_window, ShapeBounding,
310 data->window_rect.left - data->whole_rect.left,
311 data->window_rect.top - data->whole_rect.top,
312 (XRectangle *)pRegionData->Buffer,
313 pRegionData->rdh.nCount, ShapeSet, YXBanded );
315 HeapFree(GetProcessHeap(), 0, pRegionData);
318 #endif /* HAVE_LIBXSHAPE */
322 /***********************************************************************
325 static void sync_window_text( Display *display, Window win, const WCHAR *text )
328 char *buffer, *utf8_buffer;
331 /* allocate new buffer for window text */
332 count = WideCharToMultiByte(CP_UNIXCP, 0, text, -1, NULL, 0, NULL, NULL);
333 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, count ))) return;
334 WideCharToMultiByte(CP_UNIXCP, 0, text, -1, buffer, count, NULL, NULL);
336 count = WideCharToMultiByte(CP_UTF8, 0, text, strlenW(text), NULL, 0, NULL, NULL);
337 if (!(utf8_buffer = HeapAlloc( GetProcessHeap(), 0, count )))
339 HeapFree( GetProcessHeap(), 0, buffer );
342 WideCharToMultiByte(CP_UTF8, 0, text, strlenW(text), utf8_buffer, count, NULL, NULL);
345 if (XmbTextListToTextProperty( display, &buffer, 1, XStdICCTextStyle, &prop ) == Success)
347 XSetWMName( display, win, &prop );
348 XSetWMIconName( display, win, &prop );
352 Implements a NET_WM UTF-8 title. It should be without a trailing \0,
353 according to the standard
354 ( http://www.pps.jussieu.fr/~jch/software/UTF8_STRING/UTF8_STRING.text ).
356 XChangeProperty( display, win, x11drv_atom(_NET_WM_NAME), x11drv_atom(UTF8_STRING),
357 8, PropModeReplace, (unsigned char *) utf8_buffer, count);
360 HeapFree( GetProcessHeap(), 0, utf8_buffer );
361 HeapFree( GetProcessHeap(), 0, buffer );
365 /***********************************************************************
366 * X11DRV_set_win_format
368 BOOL X11DRV_set_win_format( HWND hwnd, XID fbconfig_id )
370 Display *display = thread_display();
371 struct x11drv_win_data *data;
375 if (!(data = X11DRV_get_win_data(hwnd)) &&
376 !(data = X11DRV_create_win_data(hwnd))) return FALSE;
378 if (data->fbconfig_id) return FALSE; /* can't change it twice */
381 vis = visual_from_fbconfig_id(fbconfig_id);
383 if (!vis) return FALSE;
385 if (data->whole_window)
387 Window client = data->client_window;
389 if (vis->visualid != XVisualIDFromVisual(visual))
391 client = create_client_window( display, data, vis );
392 TRACE( "re-created client window %lx for %p fbconfig %lx\n", client, data->hwnd, fbconfig_id );
397 if (client) goto done;
401 w = data->client_rect.right - data->client_rect.left;
402 h = data->client_rect.bottom - data->client_rect.top;
407 #ifdef SONAME_LIBXCOMPOSITE
410 XSetWindowAttributes attrib;
411 Window parent = X11DRV_get_whole_window( GetAncestor( hwnd, GA_ROOT ));
413 if (!parent) parent = root_window;
415 data->colormap = XCreateColormap(display, parent, vis->visual,
416 (vis->class == PseudoColor ||
417 vis->class == GrayScale ||
418 vis->class == DirectColor) ?
419 AllocAll : AllocNone);
420 attrib.override_redirect = True;
421 attrib.colormap = data->colormap;
422 XInstallColormap(gdi_display, attrib.colormap);
424 data->gl_drawable = XCreateWindow(display, parent, -w, 0, w, h, 0,
425 vis->depth, InputOutput, vis->visual,
426 CWColormap | CWOverrideRedirect,
428 if(data->gl_drawable)
430 pXCompositeRedirectWindow(display, data->gl_drawable,
431 CompositeRedirectManual);
432 XMapWindow(display, data->gl_drawable);
440 WARN("XComposite is not available, using GLXPixmap hack\n");
443 data->pixmap = XCreatePixmap(display, root_window, w, h, vis->depth);
451 data->gl_drawable = create_glxpixmap(display, vis, data->pixmap);
452 if(!data->gl_drawable)
454 XFreePixmap(display, data->pixmap);
459 if (data->pixmap) SetPropA(hwnd, pixmap_prop, (HANDLE)data->pixmap);
462 if (!data->gl_drawable) return FALSE;
464 TRACE("Created GL drawable 0x%lx, using FBConfigID 0x%lx\n",
465 data->gl_drawable, fbconfig_id);
466 SetPropA(hwnd, gl_drawable_prop, (HANDLE)data->gl_drawable);
469 data->fbconfig_id = fbconfig_id;
470 SetPropA(hwnd, fbconfig_id_prop, (HANDLE)data->fbconfig_id);
474 WIN_invalidate_dce( hwnd, NULL );
478 /***********************************************************************
481 static void sync_gl_drawable(Display *display, struct x11drv_win_data *data)
483 int w = data->client_rect.right - data->client_rect.left;
484 int h = data->client_rect.bottom - data->client_rect.top;
492 TRACE("Resizing GL drawable 0x%lx to %dx%d\n", data->gl_drawable, w, h);
493 #ifdef SONAME_LIBXCOMPOSITE
497 XMoveResizeWindow(display, data->gl_drawable, -w, 0, w, h);
505 vis = visual_from_fbconfig_id(data->fbconfig_id);
512 pix = XCreatePixmap(display, root_window, w, h, vis->depth);
515 ERR("Failed to create pixmap for offscreen rendering\n");
521 glxp = create_glxpixmap(display, vis, pix);
524 ERR("Failed to create drawable for offscreen rendering\n");
525 XFreePixmap(display, pix);
533 mark_drawable_dirty(data->gl_drawable, glxp);
535 XFreePixmap(display, data->pixmap);
536 destroy_glxpixmap(display, data->gl_drawable);
539 data->gl_drawable = glxp;
543 SetPropA(data->hwnd, gl_drawable_prop, (HANDLE)data->gl_drawable);
544 SetPropA(data->hwnd, pixmap_prop, (HANDLE)data->pixmap);
548 /***********************************************************************
551 * fill the window changes structure
553 static int get_window_changes( XWindowChanges *changes, const RECT *old, const RECT *new )
557 if (old->right - old->left != new->right - new->left )
559 if ((changes->width = new->right - new->left) <= 0) changes->width = 1;
562 if (old->bottom - old->top != new->bottom - new->top)
564 if ((changes->height = new->bottom - new->top) <= 0) changes->height = 1;
567 if (old->left != new->left)
569 changes->x = new->left;
572 if (old->top != new->top)
574 changes->y = new->top;
581 /***********************************************************************
584 static Window create_icon_window( Display *display, struct x11drv_win_data *data )
586 XSetWindowAttributes attr;
588 attr.event_mask = (ExposureMask | KeyPressMask | KeyReleaseMask | PointerMotionMask |
589 ButtonPressMask | ButtonReleaseMask | EnterWindowMask);
590 attr.bit_gravity = NorthWestGravity;
591 attr.backing_store = NotUseful/*WhenMapped*/;
592 attr.colormap = X11DRV_PALETTE_PaletteXColormap; /* Needed due to our visual */
595 data->icon_window = XCreateWindow( display, root_window, 0, 0,
596 GetSystemMetrics( SM_CXICON ),
597 GetSystemMetrics( SM_CYICON ),
600 CWEventMask | CWBitGravity | CWBackingStore | CWColormap, &attr );
601 XSaveContext( display, data->icon_window, winContext, (char *)data->hwnd );
604 TRACE( "created %lx\n", data->icon_window );
605 SetPropA( data->hwnd, icon_window_prop, (HANDLE)data->icon_window );
606 return data->icon_window;
611 /***********************************************************************
612 * destroy_icon_window
614 static void destroy_icon_window( Display *display, struct x11drv_win_data *data )
616 if (!data->icon_window) return;
617 if (x11drv_thread_data()->cursor_window == data->icon_window)
618 x11drv_thread_data()->cursor_window = None;
620 XDeleteContext( display, data->icon_window, winContext );
621 XDestroyWindow( display, data->icon_window );
622 data->icon_window = 0;
624 RemovePropA( data->hwnd, icon_window_prop );
628 /***********************************************************************
631 * Set the icon wm hints
633 static void set_icon_hints( Display *display, struct x11drv_win_data *data, HICON hIcon )
635 XWMHints *hints = data->wm_hints;
637 if (data->hWMIconBitmap) DeleteObject( data->hWMIconBitmap );
638 if (data->hWMIconMask) DeleteObject( data->hWMIconMask);
639 data->hWMIconBitmap = 0;
640 data->hWMIconMask = 0;
644 if (!data->icon_window) create_icon_window( display, data );
645 hints->icon_window = data->icon_window;
646 hints->flags = (hints->flags & ~(IconPixmapHint | IconMaskHint)) | IconWindowHint;
656 GetIconInfo(hIcon, &ii);
658 GetObjectA(ii.hbmMask, sizeof(bmMask), &bmMask);
661 rcMask.right = bmMask.bmWidth;
662 rcMask.bottom = bmMask.bmHeight;
664 hDC = CreateCompatibleDC(0);
665 hbmOrig = SelectObject(hDC, ii.hbmMask);
666 InvertRect(hDC, &rcMask);
667 SelectObject(hDC, ii.hbmColor); /* force the color bitmap to x11drv mode too */
668 SelectObject(hDC, hbmOrig);
671 data->hWMIconBitmap = ii.hbmColor;
672 data->hWMIconMask = ii.hbmMask;
674 hints->icon_pixmap = X11DRV_get_pixmap(data->hWMIconBitmap);
675 hints->icon_mask = X11DRV_get_pixmap(data->hWMIconMask);
676 destroy_icon_window( display, data );
677 hints->flags = (hints->flags & ~IconWindowHint) | IconPixmapHint | IconMaskHint;
681 /***********************************************************************
682 * wine_make_systray_window (X11DRV.@)
684 * Docks the given X window with the NETWM system tray.
686 void X11DRV_make_systray_window( HWND hwnd )
688 static Atom systray_atom;
689 Display *display = thread_display();
690 struct x11drv_win_data *data;
691 Window systray_window;
693 if (!(data = X11DRV_get_win_data( hwnd )) &&
694 !(data = X11DRV_create_win_data( hwnd ))) return;
699 if (DefaultScreen( display ) == 0)
700 systray_atom = x11drv_atom(_NET_SYSTEM_TRAY_S0);
703 char systray_buffer[29]; /* strlen(_NET_SYSTEM_TRAY_S4294967295)+1 */
704 sprintf( systray_buffer, "_NET_SYSTEM_TRAY_S%u", DefaultScreen( display ) );
705 systray_atom = XInternAtom( display, systray_buffer, False );
708 systray_window = XGetSelectionOwner( display, systray_atom );
711 TRACE("Docking tray icon %p\n", data->hwnd);
713 if (systray_window != None)
716 unsigned long info[2];
718 /* Put the window offscreen so it isn't mapped. The window _cannot_ be
719 * mapped if we intend to dock with an XEMBED tray. If the window is
720 * mapped when we dock, it may become visible as a child of the root
721 * window after it docks, which isn't the proper behavior.
723 * For more information on this problem, see
724 * http://standards.freedesktop.org/xembed-spec/latest/ar01s04.html */
726 SetWindowPos( data->hwnd, NULL, virtual_screen_rect.right + 1, virtual_screen_rect.bottom + 1,
727 0, 0, SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE );
729 /* set XEMBED protocol data on the window */
730 info[0] = 0; /* protocol version */
731 info[1] = 1; /* mapped = true */
734 XChangeProperty( display, data->whole_window,
735 x11drv_atom(_XEMBED_INFO),
736 x11drv_atom(_XEMBED_INFO), 32, PropModeReplace,
737 (unsigned char*)info, 2 );
740 /* send the docking request message */
741 ZeroMemory( &ev, sizeof(ev) );
742 ev.xclient.type = ClientMessage;
743 ev.xclient.window = systray_window;
744 ev.xclient.message_type = x11drv_atom( _NET_SYSTEM_TRAY_OPCODE );
745 ev.xclient.format = 32;
746 ev.xclient.data.l[0] = CurrentTime;
747 ev.xclient.data.l[1] = SYSTEM_TRAY_REQUEST_DOCK;
748 ev.xclient.data.l[2] = data->whole_window;
749 ev.xclient.data.l[3] = 0;
750 ev.xclient.data.l[4] = 0;
753 XSendEvent( display, systray_window, False, NoEventMask, &ev );
761 /* fall back to he KDE hints if the WM doesn't support XEMBED'ed
765 XChangeProperty( display, data->whole_window,
766 x11drv_atom(KWM_DOCKWINDOW),
767 x11drv_atom(KWM_DOCKWINDOW), 32, PropModeReplace,
768 (unsigned char*)&val, 1 );
769 XChangeProperty( display, data->whole_window,
770 x11drv_atom(_KDE_NET_WM_SYSTEM_TRAY_WINDOW_FOR),
771 XA_WINDOW, 32, PropModeReplace,
772 (unsigned char*)&data->whole_window, 1 );
778 /***********************************************************************
781 * set the window size hints
783 static void set_size_hints( Display *display, struct x11drv_win_data *data, DWORD style )
785 XSizeHints* size_hints;
787 if ((size_hints = XAllocSizeHints()))
789 size_hints->flags = 0;
791 if (data->hwnd != GetDesktopWindow()) /* don't force position of desktop */
793 size_hints->win_gravity = StaticGravity;
794 size_hints->x = data->whole_rect.left;
795 size_hints->y = data->whole_rect.top;
796 size_hints->flags |= PWinGravity | PPosition;
799 if ( !(style & WS_THICKFRAME) )
801 /* If we restrict window resizing Metacity decides that it should
802 * disable fullscreen support for this window as well.
804 if (!(data->whole_rect.left <= 0 && data->whole_rect.right >= screen_width &&
805 data->whole_rect.top <= 0 && data->whole_rect.bottom >= screen_height))
807 size_hints->max_width = data->whole_rect.right - data->whole_rect.left;
808 size_hints->max_height = data->whole_rect.bottom - data->whole_rect.top;
809 size_hints->min_width = size_hints->max_width;
810 size_hints->min_height = size_hints->max_height;
811 size_hints->flags |= PMinSize | PMaxSize;
814 XSetWMNormalHints( display, data->whole_window, size_hints );
820 /***********************************************************************
823 * get the name of the current process for setting class hints
825 static char *get_process_name(void)
831 WCHAR module[MAX_PATH];
832 DWORD len = GetModuleFileNameW( 0, module, MAX_PATH );
833 if (len && len < MAX_PATH)
836 WCHAR *p, *appname = module;
838 if ((p = strrchrW( appname, '/' ))) appname = p + 1;
839 if ((p = strrchrW( appname, '\\' ))) appname = p + 1;
840 len = WideCharToMultiByte( CP_UNIXCP, 0, appname, -1, NULL, 0, NULL, NULL );
841 if ((ptr = HeapAlloc( GetProcessHeap(), 0, len )))
843 WideCharToMultiByte( CP_UNIXCP, 0, appname, -1, ptr, len, NULL, NULL );
852 /***********************************************************************
853 * set_initial_wm_hints
855 * Set the window manager hints that don't change over the lifetime of a window.
857 static void set_initial_wm_hints( Display *display, struct x11drv_win_data *data )
862 XClassHint *class_hints;
863 char *process_name = get_process_name();
869 protocols[i++] = x11drv_atom(WM_DELETE_WINDOW);
870 protocols[i++] = x11drv_atom(_NET_WM_PING);
871 if (use_take_focus) protocols[i++] = x11drv_atom(WM_TAKE_FOCUS);
872 XChangeProperty( display, data->whole_window, x11drv_atom(WM_PROTOCOLS),
873 XA_ATOM, 32, PropModeReplace, (unsigned char *)protocols, i );
876 if ((class_hints = XAllocClassHint()))
878 static char wine[] = "Wine";
880 class_hints->res_name = process_name;
881 class_hints->res_class = wine;
882 XSetClassHint( display, data->whole_window, class_hints );
883 XFree( class_hints );
886 /* set the WM_CLIENT_MACHINE and WM_LOCALE_NAME properties */
887 XSetWMProperties(display, data->whole_window, NULL, NULL, NULL, 0, NULL, NULL, NULL);
888 /* set the pid. together, these properties are needed so the window manager can kill us if we freeze */
890 XChangeProperty(display, data->whole_window, x11drv_atom(_NET_WM_PID),
891 XA_CARDINAL, 32, PropModeReplace, (unsigned char *)&i, 1);
893 XChangeProperty( display, data->whole_window, x11drv_atom(XdndAware),
894 XA_ATOM, 32, PropModeReplace, (unsigned char*)&dndVersion, 1 );
896 data->wm_hints = XAllocWMHints();
901 HICON icon = (HICON)SendMessageW( data->hwnd, WM_GETICON, ICON_BIG, 0 );
902 if (!icon) icon = (HICON)GetClassLongPtrW( data->hwnd, GCLP_HICON );
903 data->wm_hints->flags = 0;
904 set_icon_hints( display, data, icon );
909 /***********************************************************************
910 * X11DRV_set_wm_hints
912 * Set the window manager hints for a newly-created window
914 void X11DRV_set_wm_hints( Display *display, struct x11drv_win_data *data )
919 DWORD style = GetWindowLongW( data->hwnd, GWL_STYLE );
920 DWORD ex_style = GetWindowLongW( data->hwnd, GWL_EXSTYLE );
921 HWND owner = GetWindow( data->hwnd, GW_OWNER );
923 if (data->hwnd == GetDesktopWindow())
925 /* force some styles for the desktop to get the correct decorations */
926 style |= WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX;
930 /* transient for hint */
933 Window owner_win = X11DRV_get_whole_window( owner );
935 XSetTransientForHint( display, data->whole_window, owner_win );
937 group_leader = owner_win;
939 else group_leader = data->whole_window;
944 set_size_hints( display, data, style );
946 /* set the WM_WINDOW_TYPE */
947 window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_NORMAL);
948 if (ex_style & WS_EX_TOOLWINDOW) window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_UTILITY);
949 else if (style & WS_THICKFRAME) window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_NORMAL);
950 else if (style & WS_DLGFRAME) window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_DIALOG);
951 else if (ex_style & WS_EX_DLGMODALFRAME) window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_DIALOG);
953 XChangeProperty(display, data->whole_window, x11drv_atom(_NET_WM_WINDOW_TYPE),
954 XA_ATOM, 32, PropModeReplace, (unsigned char*)&window_type, 1);
956 mwm_hints.flags = MWM_HINTS_FUNCTIONS | MWM_HINTS_DECORATIONS;
957 mwm_hints.decorations = get_mwm_decorations( style, ex_style );
958 mwm_hints.functions = MWM_FUNC_MOVE;
959 if (style & WS_THICKFRAME) mwm_hints.functions |= MWM_FUNC_RESIZE;
960 if (style & WS_MINIMIZEBOX) mwm_hints.functions |= MWM_FUNC_MINIMIZE;
961 if (style & WS_MAXIMIZEBOX) mwm_hints.functions |= MWM_FUNC_MAXIMIZE;
962 if (style & WS_SYSMENU) mwm_hints.functions |= MWM_FUNC_CLOSE;
964 XChangeProperty( display, data->whole_window, x11drv_atom(_MOTIF_WM_HINTS),
965 x11drv_atom(_MOTIF_WM_HINTS), 32, PropModeReplace,
966 (unsigned char*)&mwm_hints, sizeof(mwm_hints)/sizeof(long) );
971 data->wm_hints->flags |= InputHint | StateHint | WindowGroupHint;
972 data->wm_hints->input = !(style & WS_DISABLED);
973 data->wm_hints->initial_state = (style & WS_MINIMIZE) ? IconicState : NormalState;
974 data->wm_hints->window_group = group_leader;
975 XSetWMHints( display, data->whole_window, data->wm_hints );
982 /***********************************************************************
983 * X11DRV_window_to_X_rect
985 * Convert a rect from client to X window coordinates
987 void X11DRV_window_to_X_rect( struct x11drv_win_data *data, RECT *rect )
991 if (!data->managed) return;
992 if (IsRectEmpty( rect )) return;
994 get_x11_rect_offset( data, &rc );
996 rect->left -= rc.left;
997 rect->right -= rc.right;
999 rect->bottom -= rc.bottom;
1000 if (rect->top >= rect->bottom) rect->bottom = rect->top + 1;
1001 if (rect->left >= rect->right) rect->right = rect->left + 1;
1005 /***********************************************************************
1006 * X11DRV_X_to_window_rect
1008 * Opposite of X11DRV_window_to_X_rect
1010 void X11DRV_X_to_window_rect( struct x11drv_win_data *data, RECT *rect )
1014 if (!data->managed) return;
1015 if (IsRectEmpty( rect )) return;
1017 get_x11_rect_offset( data, &rc );
1019 rect->left += rc.left;
1020 rect->right += rc.right;
1021 rect->top += rc.top;
1022 rect->bottom += rc.bottom;
1023 if (rect->top >= rect->bottom) rect->bottom = rect->top + 1;
1024 if (rect->left >= rect->right) rect->right = rect->left + 1;
1028 /***********************************************************************
1029 * X11DRV_sync_window_position
1031 * Synchronize the X window position with the Windows one
1033 void X11DRV_sync_window_position( Display *display, struct x11drv_win_data *data,
1034 UINT swp_flags, const RECT *old_client_rect,
1035 const RECT *old_whole_rect )
1037 XWindowChanges changes;
1038 int mask = get_window_changes( &changes, old_whole_rect, &data->whole_rect );
1040 if (!(swp_flags & SWP_NOZORDER))
1042 /* find window that this one must be after */
1043 HWND prev = GetWindow( data->hwnd, GW_HWNDPREV );
1044 while (prev && !(GetWindowLongW( prev, GWL_STYLE ) & WS_VISIBLE))
1045 prev = GetWindow( prev, GW_HWNDPREV );
1046 if (!prev) /* top child */
1048 changes.stack_mode = Above;
1049 mask |= CWStackMode;
1051 /* should use stack_mode Below but most window managers don't get it right */
1052 /* and Above with a sibling doesn't work so well either, so we ignore it */
1055 /* only the size is allowed to change for the desktop window */
1056 if (data->whole_window == root_window) mask &= CWWidth | CWHeight;
1060 DWORD style = GetWindowLongW( data->hwnd, GWL_STYLE );
1062 TRACE( "setting win %p/%lx pos %d,%d,%dx%d after %lx changes=%x\n",
1063 data->hwnd, data->whole_window, data->whole_rect.left, data->whole_rect.top,
1064 data->whole_rect.right - data->whole_rect.left,
1065 data->whole_rect.bottom - data->whole_rect.top, changes.sibling, mask );
1068 if (mask & (CWWidth|CWHeight)) set_size_hints( display, data, style );
1069 if (mask & CWX) changes.x -= virtual_screen_rect.left;
1070 if (mask & CWY) changes.y -= virtual_screen_rect.top;
1071 XReconfigureWMWindow( display, data->whole_window,
1072 DefaultScreen(display), mask, &changes );
1073 wine_tsx11_unlock();
1078 /***********************************************************************
1079 * X11DRV_sync_client_position
1081 * Synchronize the X client window position with the Windows one
1083 void X11DRV_sync_client_position( Display *display, struct x11drv_win_data *data,
1084 UINT swp_flags, const RECT *old_client_rect,
1085 const RECT *old_whole_rect )
1088 XWindowChanges changes;
1089 RECT old = *old_client_rect;
1090 RECT new = data->client_rect;
1092 OffsetRect( &old, -old_whole_rect->left, -old_whole_rect->top );
1093 OffsetRect( &new, -data->whole_rect.left, -data->whole_rect.top );
1094 if (!(mask = get_window_changes( &changes, &old, &new ))) return;
1096 if (data->client_window)
1098 TRACE( "setting client win %lx pos %d,%d,%dx%d changes=%x\n",
1099 data->client_window, new.left, new.top,
1100 new.right - new.left, new.bottom - new.top, mask );
1102 XConfigureWindow( display, data->client_window, mask, &changes );
1103 wine_tsx11_unlock();
1106 if (data->gl_drawable && (mask & (CWWidth|CWHeight))) sync_gl_drawable( display, data );
1108 /* make sure the changes get to the server before we start painting */
1109 if (data->client_window || data->gl_drawable)
1113 wine_tsx11_unlock();
1118 /**********************************************************************
1119 * create_whole_window
1121 * Create the whole X window for a given window
1123 static Window create_whole_window( Display *display, struct x11drv_win_data *data )
1126 XSetWindowAttributes attr;
1131 if (!(cx = data->window_rect.right - data->window_rect.left)) cx = 1;
1132 if (!(cy = data->window_rect.bottom - data->window_rect.top)) cy = 1;
1134 if (!data->managed && is_window_managed( data->hwnd, SWP_NOACTIVATE, &data->window_rect ))
1136 TRACE( "making win %p/%lx managed\n", data->hwnd, data->whole_window );
1137 data->managed = TRUE;
1138 SetPropA( data->hwnd, managed_prop, (HANDLE)1 );
1141 mask = get_window_attributes( display, data, &attr );
1145 data->whole_rect = data->window_rect;
1146 data->whole_window = XCreateWindow( display, root_window,
1147 data->window_rect.left - virtual_screen_rect.left,
1148 data->window_rect.top - virtual_screen_rect.top,
1149 cx, cy, 0, screen_depth, InputOutput,
1150 visual, mask, &attr );
1152 if (data->whole_window) XSaveContext( display, data->whole_window, winContext, (char *)data->hwnd );
1153 wine_tsx11_unlock();
1155 if (!data->whole_window) return 0;
1157 if (!create_client_window( display, data, NULL ))
1160 XDeleteContext( display, data->whole_window, winContext );
1161 XDestroyWindow( display, data->whole_window );
1162 data->whole_window = 0;
1163 wine_tsx11_unlock();
1167 xim = x11drv_thread_data()->xim;
1168 if (xim) data->xic = X11DRV_CreateIC( xim, display, data->whole_window );
1170 set_initial_wm_hints( display, data );
1171 X11DRV_set_wm_hints( display, data );
1173 SetPropA( data->hwnd, whole_window_prop, (HANDLE)data->whole_window );
1175 /* set the window text */
1176 if (!InternalGetWindowText( data->hwnd, text, sizeof(text)/sizeof(WCHAR) )) text[0] = 0;
1177 sync_window_text( display, data->whole_window, text );
1179 /* set the window region */
1180 if ((hrgn = CreateRectRgn( 0, 0, 0, 0 )))
1182 if (GetWindowRgn( data->hwnd, hrgn ) != ERROR) sync_window_region( display, data, hrgn );
1183 DeleteObject( hrgn );
1185 return data->whole_window;
1189 /**********************************************************************
1190 * destroy_whole_window
1192 * Destroy the whole X window for a given window.
1194 static void destroy_whole_window( Display *display, struct x11drv_win_data *data, BOOL already_destroyed )
1196 struct x11drv_thread_data *thread_data = x11drv_thread_data();
1198 if (!data->whole_window) return;
1200 TRACE( "win %p xwin %lx/%lx\n", data->hwnd, data->whole_window, data->client_window );
1201 if (thread_data->cursor_window == data->whole_window ||
1202 thread_data->cursor_window == data->client_window)
1203 thread_data->cursor_window = None;
1205 XDeleteContext( display, data->whole_window, winContext );
1206 XDeleteContext( display, data->client_window, winContext );
1207 if (!already_destroyed) XDestroyWindow( display, data->whole_window );
1208 data->whole_window = data->client_window = 0;
1209 data->wm_state = WithdrawnState;
1210 data->net_wm_state = 0;
1211 data->mapped = FALSE;
1214 XUnsetICFocus( data->xic );
1215 XDestroyIC( data->xic );
1218 /* Outlook stops processing messages after destroying a dialog, so we need an explicit flush */
1220 XFree( data->wm_hints );
1221 data->wm_hints = NULL;
1222 wine_tsx11_unlock();
1223 RemovePropA( data->hwnd, whole_window_prop );
1224 RemovePropA( data->hwnd, client_window_prop );
1228 /*****************************************************************
1229 * SetWindowText (X11DRV.@)
1231 void X11DRV_SetWindowText( HWND hwnd, LPCWSTR text )
1233 Display *display = thread_display();
1236 if ((win = X11DRV_get_whole_window( hwnd )) && win != DefaultRootWindow(display))
1237 sync_window_text( display, win, text );
1241 /***********************************************************************
1242 * DestroyWindow (X11DRV.@)
1244 void X11DRV_DestroyWindow( HWND hwnd )
1246 struct x11drv_thread_data *thread_data = x11drv_thread_data();
1247 Display *display = thread_data->display;
1248 struct x11drv_win_data *data;
1250 if (!(data = X11DRV_get_win_data( hwnd ))) return;
1254 destroy_glxpixmap(display, data->gl_drawable);
1256 XFreePixmap(display, data->pixmap);
1257 wine_tsx11_unlock();
1259 else if (data->gl_drawable)
1262 XDestroyWindow(display, data->gl_drawable);
1263 wine_tsx11_unlock();
1266 destroy_whole_window( display, data, FALSE );
1267 destroy_icon_window( display, data );
1272 XFreeColormap( display, data->colormap );
1273 wine_tsx11_unlock();
1276 if (thread_data->last_focus == hwnd) thread_data->last_focus = 0;
1277 if (data->hWMIconBitmap) DeleteObject( data->hWMIconBitmap );
1278 if (data->hWMIconMask) DeleteObject( data->hWMIconMask);
1280 XDeleteContext( display, (XID)hwnd, win_data_context );
1281 wine_tsx11_unlock();
1282 HeapFree( GetProcessHeap(), 0, data );
1286 /***********************************************************************
1287 * X11DRV_DestroyNotify
1289 void X11DRV_DestroyNotify( HWND hwnd, XEvent *event )
1291 Display *display = event->xdestroywindow.display;
1292 struct x11drv_win_data *data;
1294 if (!(data = X11DRV_get_win_data( hwnd ))) return;
1296 FIXME( "window %p/%lx destroyed from the outside\n", hwnd, data->whole_window );
1297 destroy_whole_window( display, data, TRUE );
1301 static struct x11drv_win_data *alloc_win_data( Display *display, HWND hwnd )
1303 struct x11drv_win_data *data;
1305 if ((data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*data))))
1309 if (!winContext) winContext = XUniqueContext();
1310 if (!win_data_context) win_data_context = XUniqueContext();
1311 XSaveContext( display, (XID)hwnd, win_data_context, (char *)data );
1312 wine_tsx11_unlock();
1318 /* initialize the desktop window id in the desktop manager process */
1319 static struct x11drv_win_data *create_desktop_win_data( Display *display, HWND hwnd )
1321 struct x11drv_win_data *data;
1324 if (!(data = alloc_win_data( display, hwnd ))) return NULL;
1326 visualid = XVisualIDFromVisual(visual);
1327 wine_tsx11_unlock();
1328 data->whole_window = data->client_window = root_window;
1329 data->managed = TRUE;
1330 SetPropA( data->hwnd, managed_prop, (HANDLE)1 );
1331 SetPropA( data->hwnd, whole_window_prop, (HANDLE)root_window );
1332 SetPropA( data->hwnd, client_window_prop, (HANDLE)root_window );
1333 set_initial_wm_hints( display, data );
1337 /**********************************************************************
1338 * CreateDesktopWindow (X11DRV.@)
1340 BOOL X11DRV_CreateDesktopWindow( HWND hwnd )
1342 unsigned int width, height;
1344 /* retrieve the real size of the desktop */
1345 SERVER_START_REQ( get_window_rectangles )
1348 wine_server_call( req );
1349 width = reply->window.right - reply->window.left;
1350 height = reply->window.bottom - reply->window.top;
1354 if (!width && !height) /* not initialized yet */
1356 SERVER_START_REQ( set_window_pos )
1360 req->flags = SWP_NOZORDER;
1361 req->window.left = virtual_screen_rect.left;
1362 req->window.top = virtual_screen_rect.top;
1363 req->window.right = virtual_screen_rect.right;
1364 req->window.bottom = virtual_screen_rect.bottom;
1365 req->client = req->window;
1366 wine_server_call( req );
1372 Window win = (Window)GetPropA( hwnd, whole_window_prop );
1373 if (win && win != root_window) X11DRV_init_desktop( win, width, height );
1379 /**********************************************************************
1380 * CreateWindow (X11DRV.@)
1382 BOOL X11DRV_CreateWindow( HWND hwnd )
1384 Display *display = thread_display();
1386 if (hwnd == GetDesktopWindow() && root_window != DefaultRootWindow( display ))
1388 /* the desktop win data can't be created lazily */
1389 if (!create_desktop_win_data( display, hwnd )) return FALSE;
1395 /***********************************************************************
1396 * X11DRV_get_win_data
1398 * Return the X11 data structure associated with a window.
1400 struct x11drv_win_data *X11DRV_get_win_data( HWND hwnd )
1404 if (!hwnd || XFindContext( thread_display(), (XID)hwnd, win_data_context, &data )) data = NULL;
1405 return (struct x11drv_win_data *)data;
1409 /***********************************************************************
1410 * X11DRV_create_win_data
1412 * Create an X11 data window structure for an existing window.
1414 struct x11drv_win_data *X11DRV_create_win_data( HWND hwnd )
1416 Display *display = thread_display();
1417 struct x11drv_win_data *data;
1420 if (!(parent = GetAncestor( hwnd, GA_PARENT ))) return NULL; /* desktop */
1421 if (!(data = alloc_win_data( display, hwnd ))) return NULL;
1423 GetWindowRect( hwnd, &data->window_rect );
1424 MapWindowPoints( 0, parent, (POINT *)&data->window_rect, 2 );
1425 data->whole_rect = data->window_rect;
1426 GetClientRect( hwnd, &data->client_rect );
1427 MapWindowPoints( hwnd, parent, (POINT *)&data->client_rect, 2 );
1429 if (parent == GetDesktopWindow())
1431 if (!create_whole_window( display, data ))
1433 HeapFree( GetProcessHeap(), 0, data );
1436 TRACE( "win %p/%lx/%lx window %s whole %s client %s\n",
1437 hwnd, data->whole_window, data->client_window, wine_dbgstr_rect( &data->window_rect ),
1438 wine_dbgstr_rect( &data->whole_rect ), wine_dbgstr_rect( &data->client_rect ));
1444 /***********************************************************************
1445 * X11DRV_get_whole_window
1447 * Return the X window associated with the full area of a window
1449 Window X11DRV_get_whole_window( HWND hwnd )
1451 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
1455 if (hwnd == GetDesktopWindow()) return root_window;
1456 return (Window)GetPropA( hwnd, whole_window_prop );
1458 return data->whole_window;
1462 /***********************************************************************
1463 * X11DRV_get_client_window
1465 * Return the X window associated with the client area of a window
1467 Window X11DRV_get_client_window( HWND hwnd )
1469 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
1473 if (hwnd == GetDesktopWindow()) return root_window;
1474 return (Window)GetPropA( hwnd, client_window_prop );
1476 return data->client_window;
1480 /***********************************************************************
1483 * Return the X input context associated with a window
1485 XIC X11DRV_get_ic( HWND hwnd )
1487 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
1489 if (!data) return 0;
1494 /***********************************************************************
1495 * X11DRV_GetDC (X11DRV.@)
1497 void X11DRV_GetDC( HDC hdc, HWND hwnd, HWND top, const RECT *win_rect,
1498 const RECT *top_rect, DWORD flags )
1500 struct x11drv_escape_set_drawable escape;
1501 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
1503 escape.code = X11DRV_SET_DRAWABLE;
1504 escape.mode = IncludeInferiors;
1505 escape.fbconfig_id = 0;
1506 escape.gl_drawable = 0;
1509 if (top == hwnd && data && IsIconic( hwnd ) && data->icon_window)
1511 escape.drawable = data->icon_window;
1513 else if (top == hwnd && (flags & DCX_WINDOW))
1515 escape.drawable = data ? data->whole_window : X11DRV_get_whole_window( hwnd );
1519 escape.drawable = X11DRV_get_client_window( top );
1520 escape.fbconfig_id = data ? data->fbconfig_id : (XID)GetPropA( hwnd, fbconfig_id_prop );
1521 escape.gl_drawable = data ? data->gl_drawable : (Drawable)GetPropA( hwnd, gl_drawable_prop );
1522 escape.pixmap = data ? data->pixmap : (Pixmap)GetPropA( hwnd, pixmap_prop );
1523 if (flags & DCX_CLIPCHILDREN) escape.mode = ClipByChildren;
1526 escape.dc_rect.left = win_rect->left - top_rect->left;
1527 escape.dc_rect.top = win_rect->top - top_rect->top;
1528 escape.dc_rect.right = win_rect->right - top_rect->left;
1529 escape.dc_rect.bottom = win_rect->bottom - top_rect->top;
1530 escape.drawable_rect.left = top_rect->left;
1531 escape.drawable_rect.top = top_rect->top;
1532 escape.drawable_rect.right = top_rect->right;
1533 escape.drawable_rect.bottom = top_rect->bottom;
1535 ExtEscape( hdc, X11DRV_ESCAPE, sizeof(escape), (LPSTR)&escape, 0, NULL );
1539 /***********************************************************************
1540 * X11DRV_ReleaseDC (X11DRV.@)
1542 void X11DRV_ReleaseDC( HWND hwnd, HDC hdc )
1544 struct x11drv_escape_set_drawable escape;
1546 escape.code = X11DRV_SET_DRAWABLE;
1547 escape.drawable = root_window;
1548 escape.mode = IncludeInferiors;
1549 escape.drawable_rect = virtual_screen_rect;
1550 SetRect( &escape.dc_rect, 0, 0, virtual_screen_rect.right - virtual_screen_rect.left,
1551 virtual_screen_rect.bottom - virtual_screen_rect.top );
1552 escape.fbconfig_id = 0;
1553 escape.gl_drawable = 0;
1555 ExtEscape( hdc, X11DRV_ESCAPE, sizeof(escape), (LPSTR)&escape, 0, NULL );
1559 /***********************************************************************
1560 * SetCapture (X11DRV.@)
1562 void X11DRV_SetCapture( HWND hwnd, UINT flags )
1564 struct x11drv_thread_data *thread_data = x11drv_thread_data();
1566 if (!(flags & GUI_INMOVESIZE)) return;
1570 Window grab_win = X11DRV_get_client_window( GetAncestor( hwnd, GA_ROOT ) );
1572 if (!grab_win) return;
1574 XFlush( gdi_display );
1575 XGrabPointer( thread_data->display, grab_win, False,
1576 PointerMotionMask | ButtonPressMask | ButtonReleaseMask,
1577 GrabModeAsync, GrabModeAsync, root_window, None, CurrentTime );
1578 wine_tsx11_unlock();
1579 thread_data->grab_window = grab_win;
1581 else /* release capture */
1584 XFlush( gdi_display );
1585 XUngrabPointer( thread_data->display, CurrentTime );
1586 wine_tsx11_unlock();
1587 thread_data->grab_window = None;
1592 /*****************************************************************
1593 * SetParent (X11DRV.@)
1595 void X11DRV_SetParent( HWND hwnd, HWND parent, HWND old_parent )
1597 Display *display = thread_display();
1598 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
1601 if (parent == old_parent) return;
1603 if (parent != GetDesktopWindow()) /* a child window */
1605 if (old_parent == GetDesktopWindow())
1607 /* destroy the old X windows */
1608 destroy_whole_window( display, data, FALSE );
1609 destroy_icon_window( display, data );
1612 data->managed = FALSE;
1613 RemovePropA( data->hwnd, managed_prop );
1617 else /* new top level window */
1619 /* FIXME: we ignore errors since we can't really recover anyway */
1620 create_whole_window( display, data );
1625 /*****************************************************************
1626 * SetFocus (X11DRV.@)
1629 * Explicit colormap management seems to work only with OLVWM.
1631 void X11DRV_SetFocus( HWND hwnd )
1633 Display *display = thread_display();
1634 struct x11drv_win_data *data;
1635 XWindowChanges changes;
1637 /* If setting the focus to 0, uninstall the colormap */
1638 if (!hwnd && root_window == DefaultRootWindow(display))
1641 if (X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_PRIVATE)
1642 XUninstallColormap( display, X11DRV_PALETTE_PaletteXColormap );
1643 wine_tsx11_unlock();
1647 hwnd = GetAncestor( hwnd, GA_ROOT );
1649 if (!(data = X11DRV_get_win_data( hwnd ))) return;
1650 if (data->managed || !data->whole_window) return;
1652 /* Set X focus and install colormap */
1654 changes.stack_mode = Above;
1655 XConfigureWindow( display, data->whole_window, CWStackMode, &changes );
1656 if (root_window == DefaultRootWindow(display))
1658 /* we must not use CurrentTime (ICCCM), so try to use last message time instead */
1659 /* FIXME: this is not entirely correct */
1660 XSetInputFocus( display, data->whole_window, RevertToParent,
1662 GetMessageTime() - EVENT_x11_time_to_win32_time(0));
1663 if (X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_PRIVATE)
1664 XInstallColormap( display, X11DRV_PALETTE_PaletteXColormap );
1666 wine_tsx11_unlock();
1670 /**********************************************************************
1671 * SetWindowIcon (X11DRV.@)
1673 * hIcon or hIconSm has changed (or is being initialised for the
1674 * first time). Complete the X11 driver-specific initialisation
1675 * and set the window hints.
1677 * This is not entirely correct, may need to create
1678 * an icon window and set the pixmap as a background
1680 void X11DRV_SetWindowIcon( HWND hwnd, UINT type, HICON icon )
1682 Display *display = thread_display();
1683 struct x11drv_win_data *data;
1685 if (type != ICON_BIG) return; /* nothing to do here */
1687 if (!(data = X11DRV_get_win_data( hwnd ))) return;
1688 if (!data->whole_window) return;
1689 if (!data->managed) return;
1693 set_icon_hints( display, data, icon );
1695 XSetWMHints( display, data->whole_window, data->wm_hints );
1696 wine_tsx11_unlock();
1701 /***********************************************************************
1702 * SetWindowRgn (X11DRV.@)
1704 * Assign specified region to window (for non-rectangular windows)
1706 int X11DRV_SetWindowRgn( HWND hwnd, HRGN hrgn, BOOL redraw )
1708 struct x11drv_win_data *data;
1710 if ((data = X11DRV_get_win_data( hwnd )))
1712 sync_window_region( thread_display(), data, hrgn );
1714 else if (GetWindowThreadProcessId( hwnd, NULL ) != GetCurrentThreadId())
1716 FIXME( "not supported on other thread window %p\n", hwnd );
1717 SetLastError( ERROR_INVALID_WINDOW_HANDLE );