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"
52 WINE_DEFAULT_DEBUG_CHANNEL(x11drv);
54 /* X context to associate a hwnd to an X window */
55 XContext winContext = 0;
57 /* X context to associate a struct x11drv_win_data to an hwnd */
58 static XContext win_data_context;
60 static const char whole_window_prop[] = "__wine_x11_whole_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";
66 static const char visual_id_prop[] = "__wine_x11_visual_id";
68 /* for XDG systray icons */
69 #define SYSTEM_TRAY_REQUEST_DOCK 0
71 extern int usexcomposite;
73 /***********************************************************************
76 * Check if a given window should be managed
78 BOOL is_window_managed( HWND hwnd, UINT swp_flags, const RECT *window_rect )
80 DWORD style, ex_style;
82 if (!managed_mode) return FALSE;
84 /* child windows are not managed */
85 style = GetWindowLongW( hwnd, GWL_STYLE );
86 if ((style & (WS_CHILD|WS_POPUP)) == WS_CHILD) return FALSE;
87 /* activated windows are managed */
88 if (!(swp_flags & (SWP_NOACTIVATE|SWP_HIDEWINDOW))) return TRUE;
89 if (hwnd == GetActiveWindow()) return TRUE;
90 /* windows with caption are managed */
91 if ((style & WS_CAPTION) == WS_CAPTION) return TRUE;
92 /* tool windows are not managed */
93 ex_style = GetWindowLongW( hwnd, GWL_EXSTYLE );
94 if (ex_style & WS_EX_TOOLWINDOW) return FALSE;
95 /* windows with thick frame are managed */
96 if (style & WS_THICKFRAME) return TRUE;
97 /* application windows are managed */
98 if (ex_style & WS_EX_APPWINDOW) return TRUE;
101 /* popup with sysmenu == caption are managed */
102 if (style & WS_SYSMENU) return TRUE;
103 /* full-screen popup windows are managed */
104 if ((window_rect->right - window_rect->left) == screen_width &&
105 (window_rect->bottom - window_rect->top) == screen_height)
108 /* default: not managed */
113 /***********************************************************************
114 * X11DRV_is_window_rect_mapped
116 * Check if the X whole window should be mapped based on its rectangle
118 BOOL X11DRV_is_window_rect_mapped( const RECT *rect )
120 /* don't map if rect is empty */
121 if (IsRectEmpty( rect )) return FALSE;
123 /* don't map if rect is off-screen */
124 if (rect->left >= virtual_screen_rect.right ||
125 rect->top >= virtual_screen_rect.bottom ||
126 rect->right <= virtual_screen_rect.left ||
127 rect->bottom <= virtual_screen_rect.top)
134 /***********************************************************************
135 * get_mwm_decorations
137 static unsigned long get_mwm_decorations( DWORD style, DWORD ex_style )
139 unsigned long ret = 0;
141 if (ex_style & WS_EX_TOOLWINDOW) return 0;
143 if ((style & WS_CAPTION) == WS_CAPTION)
145 ret |= MWM_DECOR_TITLE | MWM_DECOR_BORDER;
146 if (style & WS_SYSMENU) ret |= MWM_DECOR_MENU;
147 if (style & WS_MINIMIZEBOX) ret |= MWM_DECOR_MINIMIZE;
148 if (style & WS_MAXIMIZEBOX) ret |= MWM_DECOR_MAXIMIZE;
150 if (ex_style & WS_EX_DLGMODALFRAME) ret |= MWM_DECOR_BORDER;
151 else if (style & WS_THICKFRAME) ret |= MWM_DECOR_BORDER | MWM_DECOR_RESIZEH;
152 else if ((style & (WS_DLGFRAME|WS_BORDER)) == WS_DLGFRAME) ret |= MWM_DECOR_BORDER;
157 /***********************************************************************
158 * get_x11_rect_offset
160 * Helper for X11DRV_window_to_X_rect and X11DRV_X_to_window_rect.
162 static void get_x11_rect_offset( struct x11drv_win_data *data, RECT *rect )
164 DWORD style, ex_style, style_mask = 0, ex_style_mask = 0;
167 rect->top = rect->bottom = rect->left = rect->right = 0;
169 style = GetWindowLongW( data->hwnd, GWL_STYLE );
170 ex_style = GetWindowLongW( data->hwnd, GWL_EXSTYLE );
171 decor = get_mwm_decorations( style, ex_style );
173 if (decor & MWM_DECOR_TITLE) style_mask |= WS_CAPTION;
174 if (decor & MWM_DECOR_BORDER)
176 style_mask |= WS_DLGFRAME | WS_THICKFRAME;
177 ex_style_mask |= WS_EX_DLGMODALFRAME;
180 AdjustWindowRectEx( rect, style & style_mask, FALSE, ex_style & ex_style_mask );
184 /***********************************************************************
185 * get_window_attributes
187 * Fill the window attributes structure for an X window.
189 static int get_window_attributes( Display *display, struct x11drv_win_data *data,
190 XSetWindowAttributes *attr )
192 attr->override_redirect = !data->managed;
193 attr->colormap = X11DRV_PALETTE_PaletteXColormap;
194 attr->save_under = ((GetClassLongW( data->hwnd, GCL_STYLE ) & CS_SAVEBITS) != 0);
195 attr->cursor = x11drv_thread_data()->cursor;
196 attr->bit_gravity = NorthWestGravity;
197 attr->backing_store = NotUseful;
198 attr->event_mask = (ExposureMask | PointerMotionMask |
199 ButtonPressMask | ButtonReleaseMask | EnterWindowMask |
200 KeyPressMask | KeyReleaseMask | FocusChangeMask | KeymapStateMask);
201 if (data->managed) attr->event_mask |= StructureNotifyMask;
203 return (CWOverrideRedirect | CWSaveUnder | CWColormap | CWCursor |
204 CWEventMask | CWBitGravity | CWBackingStore);
208 /***********************************************************************
209 * X11DRV_sync_window_style
211 * Change the X window attributes when the window style has changed.
213 void X11DRV_sync_window_style( Display *display, struct x11drv_win_data *data )
215 if (data->whole_window != root_window)
217 XSetWindowAttributes attr;
218 int mask = get_window_attributes( display, data, &attr );
221 XChangeWindowAttributes( display, data->whole_window, mask, &attr );
227 /***********************************************************************
230 * Update the X11 window region.
232 static void sync_window_region( Display *display, struct x11drv_win_data *data, HRGN hrgn )
234 #ifdef HAVE_LIBXSHAPE
235 if (!data->whole_window) return;
240 XShapeCombineMask( display, data->whole_window, ShapeBounding, 0, 0, None, ShapeSet );
245 RGNDATA *pRegionData = X11DRV_GetRegionData( hrgn, 0 );
249 XShapeCombineRectangles( display, data->whole_window, ShapeBounding,
250 data->window_rect.left - data->whole_rect.left,
251 data->window_rect.top - data->whole_rect.top,
252 (XRectangle *)pRegionData->Buffer,
253 pRegionData->rdh.nCount, ShapeSet, YXBanded );
255 HeapFree(GetProcessHeap(), 0, pRegionData);
258 #endif /* HAVE_LIBXSHAPE */
262 /***********************************************************************
265 static void sync_window_text( Display *display, Window win, const WCHAR *text )
268 char *buffer, *utf8_buffer;
271 /* allocate new buffer for window text */
272 count = WideCharToMultiByte(CP_UNIXCP, 0, text, -1, NULL, 0, NULL, NULL);
273 if (!(buffer = HeapAlloc( GetProcessHeap(), 0, count ))) return;
274 WideCharToMultiByte(CP_UNIXCP, 0, text, -1, buffer, count, NULL, NULL);
276 count = WideCharToMultiByte(CP_UTF8, 0, text, strlenW(text), NULL, 0, NULL, NULL);
277 if (!(utf8_buffer = HeapAlloc( GetProcessHeap(), 0, count )))
279 HeapFree( GetProcessHeap(), 0, buffer );
282 WideCharToMultiByte(CP_UTF8, 0, text, strlenW(text), utf8_buffer, count, NULL, NULL);
285 if (XmbTextListToTextProperty( display, &buffer, 1, XStdICCTextStyle, &prop ) == Success)
287 XSetWMName( display, win, &prop );
288 XSetWMIconName( display, win, &prop );
292 Implements a NET_WM UTF-8 title. It should be without a trailing \0,
293 according to the standard
294 ( http://www.pps.jussieu.fr/~jch/software/UTF8_STRING/UTF8_STRING.text ).
296 XChangeProperty( display, win, x11drv_atom(_NET_WM_NAME), x11drv_atom(UTF8_STRING),
297 8, PropModeReplace, (unsigned char *) utf8_buffer, count);
300 HeapFree( GetProcessHeap(), 0, utf8_buffer );
301 HeapFree( GetProcessHeap(), 0, buffer );
305 /***********************************************************************
306 * X11DRV_set_win_format
308 BOOL X11DRV_set_win_format( HWND hwnd, XID fbconfig_id )
310 Display *display = thread_display();
311 struct x11drv_win_data *data;
317 if (!(data = X11DRV_get_win_data(hwnd)) &&
318 !(data = X11DRV_create_win_data(hwnd))) return FALSE;
322 vis = visual_from_fbconfig_id(fbconfig_id);
329 if(data->whole_window && vis->visualid == XVisualIDFromVisual(visual))
331 TRACE("Whole window available and visual match, rendering onscreen\n");
337 parent = data->whole_window;
341 next_hwnd = GetAncestor(next_hwnd, GA_PARENT);
344 ERR("Could not find parent HWND with a drawable!\n");
347 parent = X11DRV_get_whole_window(next_hwnd);
350 w = data->client_rect.right - data->client_rect.left;
351 h = data->client_rect.bottom - data->client_rect.top;
357 #ifdef SONAME_LIBXCOMPOSITE
360 XSetWindowAttributes attrib;
362 attrib.override_redirect = True;
363 attrib.colormap = XCreateColormap(display, parent, vis->visual,
364 (vis->class == PseudoColor ||
365 vis->class == GrayScale ||
366 vis->class == DirectColor) ?
367 AllocAll : AllocNone);
368 XInstallColormap(gdi_display, attrib.colormap);
370 data->gl_drawable = XCreateWindow(display, parent, -w, 0, w, h, 0,
371 vis->depth, InputOutput, vis->visual,
372 CWColormap | CWOverrideRedirect,
374 if(data->gl_drawable)
376 pXCompositeRedirectWindow(display, data->gl_drawable,
377 CompositeRedirectManual);
378 XMapWindow(display, data->gl_drawable);
384 WARN("XComposite is not available, using GLXPixmap hack\n");
386 data->pixmap = XCreatePixmap(display, parent, w, h, vis->depth);
389 ERR("Failed to create pixmap for offscreen rendering\n");
395 data->gl_drawable = create_glxpixmap(display, vis, data->pixmap);
396 if(!data->gl_drawable)
398 XFreePixmap(display, data->pixmap);
403 if(!data->gl_drawable)
405 ERR("Failed to create drawable for offscreen rendering\n");
417 TRACE("Created GL drawable 0x%lx, using FBConfigID 0x%lx\n",
418 data->gl_drawable, fbconfig_id);
420 data->fbconfig_id = fbconfig_id;
421 SetPropA(hwnd, fbconfig_id_prop, (HANDLE)data->fbconfig_id);
422 SetPropA(hwnd, gl_drawable_prop, (HANDLE)data->gl_drawable);
423 SetPropA(hwnd, pixmap_prop, (HANDLE)data->pixmap);
424 invalidate_dce( hwnd, &data->window_rect );
428 /***********************************************************************
429 * X11DRV_sync_gl_drawable
431 void X11DRV_sync_gl_drawable(Display *display, struct x11drv_win_data *data)
433 int w = data->client_rect.right - data->client_rect.left;
434 int h = data->client_rect.bottom - data->client_rect.top;
441 TRACE("Resizing GL drawable 0x%lx to %dx%d\n", data->gl_drawable, w, h);
442 #ifdef SONAME_LIBXCOMPOSITE
446 XMoveResizeWindow(display, data->gl_drawable, -w, 0, w, h);
452 parent = data->whole_window;
453 next_hwnd = data->hwnd;
456 next_hwnd = GetAncestor(next_hwnd, GA_PARENT);
459 ERR("Could not find parent HWND with a drawable!\n");
462 parent = X11DRV_get_whole_window(next_hwnd);
467 vis = visual_from_fbconfig_id(data->fbconfig_id);
474 pix = XCreatePixmap(display, parent, w, h, vis->depth);
477 ERR("Failed to create pixmap for offscreen rendering\n");
483 glxp = create_glxpixmap(display, vis, pix);
486 ERR("Failed to create drawable for offscreen rendering\n");
487 XFreePixmap(display, pix);
495 mark_drawable_dirty(data->gl_drawable, glxp);
497 XFreePixmap(display, data->pixmap);
498 destroy_glxpixmap(display, data->gl_drawable);
501 data->gl_drawable = glxp;
506 SetPropA(data->hwnd, gl_drawable_prop, (HANDLE)data->gl_drawable);
507 SetPropA(data->hwnd, pixmap_prop, (HANDLE)data->pixmap);
508 invalidate_dce( data->hwnd, &data->window_rect );
512 /***********************************************************************
515 * fill the window changes structure
517 static int get_window_changes( XWindowChanges *changes, const RECT *old, const RECT *new )
521 if (old->right - old->left != new->right - new->left )
523 if (!(changes->width = new->right - new->left)) changes->width = 1;
526 if (old->bottom - old->top != new->bottom - new->top)
528 if (!(changes->height = new->bottom - new->top)) changes->height = 1;
531 if (old->left != new->left)
533 changes->x = new->left;
536 if (old->top != new->top)
538 changes->y = new->top;
545 /***********************************************************************
548 static Window create_icon_window( Display *display, struct x11drv_win_data *data )
550 XSetWindowAttributes attr;
552 attr.event_mask = (ExposureMask | KeyPressMask | KeyReleaseMask | PointerMotionMask |
553 ButtonPressMask | ButtonReleaseMask | EnterWindowMask);
554 attr.bit_gravity = NorthWestGravity;
555 attr.backing_store = NotUseful/*WhenMapped*/;
556 attr.colormap = X11DRV_PALETTE_PaletteXColormap; /* Needed due to our visual */
559 data->icon_window = XCreateWindow( display, root_window, 0, 0,
560 GetSystemMetrics( SM_CXICON ),
561 GetSystemMetrics( SM_CYICON ),
564 CWEventMask | CWBitGravity | CWBackingStore | CWColormap, &attr );
565 XSaveContext( display, data->icon_window, winContext, (char *)data->hwnd );
568 TRACE( "created %lx\n", data->icon_window );
569 SetPropA( data->hwnd, icon_window_prop, (HANDLE)data->icon_window );
570 return data->icon_window;
575 /***********************************************************************
576 * destroy_icon_window
578 static void destroy_icon_window( Display *display, struct x11drv_win_data *data )
580 if (!data->icon_window) return;
581 if (x11drv_thread_data()->cursor_window == data->icon_window)
582 x11drv_thread_data()->cursor_window = None;
584 XDeleteContext( display, data->icon_window, winContext );
585 XDestroyWindow( display, data->icon_window );
586 data->icon_window = 0;
588 RemovePropA( data->hwnd, icon_window_prop );
592 /***********************************************************************
595 * Set the icon wm hints
597 static void set_icon_hints( Display *display, struct x11drv_win_data *data, HICON hIcon )
599 XWMHints *hints = data->wm_hints;
601 if (data->hWMIconBitmap) DeleteObject( data->hWMIconBitmap );
602 if (data->hWMIconMask) DeleteObject( data->hWMIconMask);
603 data->hWMIconBitmap = 0;
604 data->hWMIconMask = 0;
608 if (!data->icon_window) create_icon_window( display, data );
609 hints->icon_window = data->icon_window;
610 hints->flags = (hints->flags & ~(IconPixmapHint | IconMaskHint)) | IconWindowHint;
620 GetIconInfo(hIcon, &ii);
622 GetObjectA(ii.hbmMask, sizeof(bmMask), &bmMask);
625 rcMask.right = bmMask.bmWidth;
626 rcMask.bottom = bmMask.bmHeight;
628 hDC = CreateCompatibleDC(0);
629 hbmOrig = SelectObject(hDC, ii.hbmMask);
630 InvertRect(hDC, &rcMask);
631 SelectObject(hDC, ii.hbmColor); /* force the color bitmap to x11drv mode too */
632 SelectObject(hDC, hbmOrig);
635 data->hWMIconBitmap = ii.hbmColor;
636 data->hWMIconMask = ii.hbmMask;
638 hints->icon_pixmap = X11DRV_get_pixmap(data->hWMIconBitmap);
639 hints->icon_mask = X11DRV_get_pixmap(data->hWMIconMask);
640 destroy_icon_window( display, data );
641 hints->flags = (hints->flags & ~IconWindowHint) | IconPixmapHint | IconMaskHint;
645 /***********************************************************************
646 * wine_make_systray_window (X11DRV.@)
648 * Docks the given X window with the NETWM system tray.
650 void X11DRV_make_systray_window( HWND hwnd )
652 static Atom systray_atom;
653 Display *display = thread_display();
654 struct x11drv_win_data *data;
655 Window systray_window;
657 if (!(data = X11DRV_get_win_data( hwnd )) &&
658 !(data = X11DRV_create_win_data( hwnd ))) return;
663 if (DefaultScreen( display ) == 0)
664 systray_atom = x11drv_atom(_NET_SYSTEM_TRAY_S0);
667 char systray_buffer[29]; /* strlen(_NET_SYSTEM_TRAY_S4294967295)+1 */
668 sprintf( systray_buffer, "_NET_SYSTEM_TRAY_S%u", DefaultScreen( display ) );
669 systray_atom = XInternAtom( display, systray_buffer, False );
672 systray_window = XGetSelectionOwner( display, systray_atom );
675 TRACE("Docking tray icon %p\n", data->hwnd);
677 if (systray_window != None)
680 unsigned long info[2];
682 /* Put the window offscreen so it isn't mapped. The window _cannot_ be
683 * mapped if we intend to dock with an XEMBED tray. If the window is
684 * mapped when we dock, it may become visible as a child of the root
685 * window after it docks, which isn't the proper behavior.
687 * For more information on this problem, see
688 * http://standards.freedesktop.org/xembed-spec/latest/ar01s04.html */
690 SetWindowPos( data->hwnd, NULL, virtual_screen_rect.right + 1, virtual_screen_rect.bottom + 1,
691 0, 0, SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE );
693 /* set XEMBED protocol data on the window */
694 info[0] = 0; /* protocol version */
695 info[1] = 1; /* mapped = true */
698 XChangeProperty( display, data->whole_window,
699 x11drv_atom(_XEMBED_INFO),
700 x11drv_atom(_XEMBED_INFO), 32, PropModeReplace,
701 (unsigned char*)info, 2 );
704 /* send the docking request message */
705 ZeroMemory( &ev, sizeof(ev) );
706 ev.xclient.type = ClientMessage;
707 ev.xclient.window = systray_window;
708 ev.xclient.message_type = x11drv_atom( _NET_SYSTEM_TRAY_OPCODE );
709 ev.xclient.format = 32;
710 ev.xclient.data.l[0] = CurrentTime;
711 ev.xclient.data.l[1] = SYSTEM_TRAY_REQUEST_DOCK;
712 ev.xclient.data.l[2] = data->whole_window;
713 ev.xclient.data.l[3] = 0;
714 ev.xclient.data.l[4] = 0;
717 XSendEvent( display, systray_window, False, NoEventMask, &ev );
725 /* fall back to he KDE hints if the WM doesn't support XEMBED'ed
729 XChangeProperty( display, data->whole_window,
730 x11drv_atom(KWM_DOCKWINDOW),
731 x11drv_atom(KWM_DOCKWINDOW), 32, PropModeReplace,
732 (unsigned char*)&val, 1 );
733 XChangeProperty( display, data->whole_window,
734 x11drv_atom(_KDE_NET_WM_SYSTEM_TRAY_WINDOW_FOR),
735 XA_WINDOW, 32, PropModeReplace,
736 (unsigned char*)&data->whole_window, 1 );
742 /***********************************************************************
745 * set the window size hints
747 static void set_size_hints( Display *display, struct x11drv_win_data *data, DWORD style )
749 XSizeHints* size_hints;
751 if ((size_hints = XAllocSizeHints()))
753 size_hints->flags = 0;
755 if (data->hwnd != GetDesktopWindow()) /* don't force position of desktop */
757 size_hints->win_gravity = StaticGravity;
758 size_hints->x = data->whole_rect.left;
759 size_hints->y = data->whole_rect.top;
760 size_hints->flags |= PWinGravity | PPosition;
763 if ( !(style & WS_THICKFRAME) )
765 size_hints->max_width = data->whole_rect.right - data->whole_rect.left;
766 size_hints->max_height = data->whole_rect.bottom - data->whole_rect.top;
767 size_hints->min_width = size_hints->max_width;
768 size_hints->min_height = size_hints->max_height;
769 size_hints->flags |= PMinSize | PMaxSize;
771 XSetWMNormalHints( display, data->whole_window, size_hints );
777 /***********************************************************************
780 * get the name of the current process for setting class hints
782 static char *get_process_name(void)
788 WCHAR module[MAX_PATH];
789 DWORD len = GetModuleFileNameW( 0, module, MAX_PATH );
790 if (len && len < MAX_PATH)
793 WCHAR *p, *appname = module;
795 if ((p = strrchrW( appname, '/' ))) appname = p + 1;
796 if ((p = strrchrW( appname, '\\' ))) appname = p + 1;
797 len = WideCharToMultiByte( CP_UNIXCP, 0, appname, -1, NULL, 0, NULL, NULL );
798 if ((ptr = HeapAlloc( GetProcessHeap(), 0, len )))
800 WideCharToMultiByte( CP_UNIXCP, 0, appname, -1, ptr, len, NULL, NULL );
809 /***********************************************************************
810 * set_initial_wm_hints
812 * Set the window manager hints that don't change over the lifetime of a window.
814 static void set_initial_wm_hints( Display *display, struct x11drv_win_data *data )
819 XClassHint *class_hints;
820 char *process_name = get_process_name();
826 protocols[i++] = x11drv_atom(WM_DELETE_WINDOW);
827 protocols[i++] = x11drv_atom(_NET_WM_PING);
828 if (use_take_focus) protocols[i++] = x11drv_atom(WM_TAKE_FOCUS);
829 XChangeProperty( display, data->whole_window, x11drv_atom(WM_PROTOCOLS),
830 XA_ATOM, 32, PropModeReplace, (unsigned char *)protocols, i );
833 if ((class_hints = XAllocClassHint()))
835 static char wine[] = "Wine";
837 class_hints->res_name = process_name;
838 class_hints->res_class = wine;
839 XSetClassHint( display, data->whole_window, class_hints );
840 XFree( class_hints );
843 /* set the WM_CLIENT_MACHINE and WM_LOCALE_NAME properties */
844 XSetWMProperties(display, data->whole_window, NULL, NULL, NULL, 0, NULL, NULL, NULL);
845 /* set the pid. together, these properties are needed so the window manager can kill us if we freeze */
847 XChangeProperty(display, data->whole_window, x11drv_atom(_NET_WM_PID),
848 XA_CARDINAL, 32, PropModeReplace, (unsigned char *)&i, 1);
850 XChangeProperty( display, data->whole_window, x11drv_atom(XdndAware),
851 XA_ATOM, 32, PropModeReplace, (unsigned char*)&dndVersion, 1 );
853 data->wm_hints = XAllocWMHints();
858 HICON icon = (HICON)SendMessageW( data->hwnd, WM_GETICON, ICON_BIG, 0 );
859 if (!icon) icon = (HICON)GetClassLongPtrW( data->hwnd, GCLP_HICON );
860 data->wm_hints->flags = 0;
861 set_icon_hints( display, data, icon );
866 /***********************************************************************
867 * X11DRV_set_wm_hints
869 * Set the window manager hints for a newly-created window
871 void X11DRV_set_wm_hints( Display *display, struct x11drv_win_data *data )
876 DWORD style = GetWindowLongW( data->hwnd, GWL_STYLE );
877 DWORD ex_style = GetWindowLongW( data->hwnd, GWL_EXSTYLE );
878 HWND owner = GetWindow( data->hwnd, GW_OWNER );
880 if (data->hwnd == GetDesktopWindow())
882 /* force some styles for the desktop to get the correct decorations */
883 style |= WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX;
887 /* transient for hint */
890 Window owner_win = X11DRV_get_whole_window( owner );
892 XSetTransientForHint( display, data->whole_window, owner_win );
894 group_leader = owner_win;
896 else group_leader = data->whole_window;
901 set_size_hints( display, data, style );
903 /* set the WM_WINDOW_TYPE */
904 window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_NORMAL);
905 if (ex_style & WS_EX_TOOLWINDOW) window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_UTILITY);
906 else if (style & WS_THICKFRAME) window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_NORMAL);
907 else if (style & WS_DLGFRAME) window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_DIALOG);
908 else if (ex_style & WS_EX_DLGMODALFRAME) window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_DIALOG);
910 XChangeProperty(display, data->whole_window, x11drv_atom(_NET_WM_WINDOW_TYPE),
911 XA_ATOM, 32, PropModeReplace, (unsigned char*)&window_type, 1);
913 mwm_hints.flags = MWM_HINTS_FUNCTIONS | MWM_HINTS_DECORATIONS;
914 mwm_hints.decorations = get_mwm_decorations( style, ex_style );
915 mwm_hints.functions = MWM_FUNC_MOVE;
916 if (style & WS_THICKFRAME) mwm_hints.functions |= MWM_FUNC_RESIZE;
917 if (style & WS_MINIMIZEBOX) mwm_hints.functions |= MWM_FUNC_MINIMIZE;
918 if (style & WS_MAXIMIZEBOX) mwm_hints.functions |= MWM_FUNC_MAXIMIZE;
919 if (style & WS_SYSMENU) mwm_hints.functions |= MWM_FUNC_CLOSE;
921 XChangeProperty( display, data->whole_window, x11drv_atom(_MOTIF_WM_HINTS),
922 x11drv_atom(_MOTIF_WM_HINTS), 32, PropModeReplace,
923 (unsigned char*)&mwm_hints, sizeof(mwm_hints)/sizeof(long) );
928 data->wm_hints->flags |= InputHint | StateHint | WindowGroupHint;
929 data->wm_hints->input = !(style & WS_DISABLED);
930 data->wm_hints->initial_state = (style & WS_MINIMIZE) ? IconicState : NormalState;
931 data->wm_hints->window_group = group_leader;
932 XSetWMHints( display, data->whole_window, data->wm_hints );
939 /***********************************************************************
940 * X11DRV_set_iconic_state
942 * Set the X11 iconic state according to the window style.
944 void X11DRV_set_iconic_state( HWND hwnd )
946 Display *display = thread_display();
947 struct x11drv_win_data *data;
949 DWORD style = GetWindowLongW( hwnd, GWL_STYLE );
950 BOOL iconic = (style & WS_MINIMIZE) != 0;
952 if (!(data = X11DRV_get_win_data( hwnd ))) return;
953 if (!data->whole_window) return;
955 GetWindowRect( hwnd, &rect );
961 data->wm_hints->flags |= StateHint | IconPositionHint;
962 data->wm_hints->initial_state = iconic ? IconicState : NormalState;
963 data->wm_hints->icon_x = rect.left - virtual_screen_rect.left;
964 data->wm_hints->icon_y = rect.top - virtual_screen_rect.top;
965 XSetWMHints( display, data->whole_window, data->wm_hints );
971 XIconifyWindow( display, data->whole_window, DefaultScreen(display) );
973 if (X11DRV_is_window_rect_mapped( &rect ))
974 XMapWindow( display, data->whole_window );
981 /***********************************************************************
982 * X11DRV_window_to_X_rect
984 * Convert a rect from client to X window coordinates
986 void X11DRV_window_to_X_rect( struct x11drv_win_data *data, RECT *rect )
990 if (!data->managed) return;
991 if (IsRectEmpty( rect )) return;
993 get_x11_rect_offset( data, &rc );
995 rect->left -= rc.left;
996 rect->right -= rc.right;
998 rect->bottom -= rc.bottom;
999 if (rect->top >= rect->bottom) rect->bottom = rect->top + 1;
1000 if (rect->left >= rect->right) rect->right = rect->left + 1;
1004 /***********************************************************************
1005 * X11DRV_X_to_window_rect
1007 * Opposite of X11DRV_window_to_X_rect
1009 void X11DRV_X_to_window_rect( struct x11drv_win_data *data, RECT *rect )
1013 if (!data->managed) return;
1014 if (IsRectEmpty( rect )) return;
1016 get_x11_rect_offset( data, &rc );
1018 rect->left += rc.left;
1019 rect->right += rc.right;
1020 rect->top += rc.top;
1021 rect->bottom += rc.bottom;
1022 if (rect->top >= rect->bottom) rect->bottom = rect->top + 1;
1023 if (rect->left >= rect->right) rect->right = rect->left + 1;
1027 /***********************************************************************
1028 * X11DRV_sync_window_position
1030 * Synchronize the X window position with the Windows one
1032 void X11DRV_sync_window_position( Display *display, struct x11drv_win_data *data,
1033 UINT swp_flags, const RECT *old_client_rect,
1034 const RECT *old_whole_rect )
1036 XWindowChanges changes;
1037 int mask = get_window_changes( &changes, old_whole_rect, &data->whole_rect );
1039 if (!(swp_flags & SWP_NOZORDER))
1041 /* find window that this one must be after */
1042 HWND prev = GetWindow( data->hwnd, GW_HWNDPREV );
1043 while (prev && !(GetWindowLongW( prev, GWL_STYLE ) & WS_VISIBLE))
1044 prev = GetWindow( prev, GW_HWNDPREV );
1045 if (!prev) /* top child */
1047 changes.stack_mode = Above;
1048 mask |= CWStackMode;
1052 /* should use stack_mode Below but most window managers don't get it right */
1053 /* so move it above the next one in Z order */
1054 HWND next = GetWindow( data->hwnd, GW_HWNDNEXT );
1055 while (next && !(GetWindowLongW( next, GWL_STYLE ) & WS_VISIBLE))
1056 next = GetWindow( next, GW_HWNDNEXT );
1059 changes.stack_mode = Above;
1060 changes.sibling = X11DRV_get_whole_window(next);
1061 mask |= CWStackMode | CWSibling;
1066 /* only the size is allowed to change for the desktop window */
1067 if (data->whole_window == root_window) mask &= CWWidth | CWHeight;
1071 DWORD style = GetWindowLongW( data->hwnd, GWL_STYLE );
1073 TRACE( "setting win %lx pos %d,%d,%dx%d after %lx changes=%x\n",
1074 data->whole_window, data->whole_rect.left, data->whole_rect.top,
1075 data->whole_rect.right - data->whole_rect.left,
1076 data->whole_rect.bottom - data->whole_rect.top, changes.sibling, mask );
1079 if (mask & (CWWidth|CWHeight)) set_size_hints( display, data, style );
1080 if (mask & CWX) changes.x -= virtual_screen_rect.left;
1081 if (mask & CWY) changes.y -= virtual_screen_rect.top;
1082 XReconfigureWMWindow( display, data->whole_window,
1083 DefaultScreen(display), mask, &changes );
1084 wine_tsx11_unlock();
1089 /**********************************************************************
1090 * create_whole_window
1092 * Create the whole X window for a given window
1094 static Window create_whole_window( Display *display, struct x11drv_win_data *data )
1097 XSetWindowAttributes attr;
1102 if (!(cx = data->window_rect.right - data->window_rect.left)) cx = 1;
1103 if (!(cy = data->window_rect.bottom - data->window_rect.top)) cy = 1;
1105 if (!data->managed && is_window_managed( data->hwnd, SWP_NOACTIVATE, &data->window_rect ))
1107 TRACE( "making win %p/%lx managed\n", data->hwnd, data->whole_window );
1108 data->managed = TRUE;
1109 SetPropA( data->hwnd, managed_prop, (HANDLE)1 );
1112 mask = get_window_attributes( display, data, &attr );
1116 data->whole_rect = data->window_rect;
1117 data->whole_window = XCreateWindow( display, root_window,
1118 data->window_rect.left - virtual_screen_rect.left,
1119 data->window_rect.top - virtual_screen_rect.top,
1120 cx, cy, 0, screen_depth, InputOutput,
1121 visual, mask, &attr );
1123 if (!data->whole_window)
1125 wine_tsx11_unlock();
1128 XSaveContext( display, data->whole_window, winContext, (char *)data->hwnd );
1129 wine_tsx11_unlock();
1131 xim = x11drv_thread_data()->xim;
1132 if (xim) data->xic = X11DRV_CreateIC( xim, display, data->whole_window );
1134 set_initial_wm_hints( display, data );
1135 X11DRV_set_wm_hints( display, data );
1137 SetPropA( data->hwnd, whole_window_prop, (HANDLE)data->whole_window );
1139 /* set the window text */
1140 if (!InternalGetWindowText( data->hwnd, text, sizeof(text)/sizeof(WCHAR) )) text[0] = 0;
1141 sync_window_text( display, data->whole_window, text );
1143 /* set the window region */
1144 if ((hrgn = CreateRectRgn( 0, 0, 0, 0 )))
1146 if (GetWindowRgn( data->hwnd, hrgn ) != ERROR) sync_window_region( display, data, hrgn );
1147 DeleteObject( hrgn );
1149 return data->whole_window;
1153 /**********************************************************************
1154 * destroy_whole_window
1156 * Destroy the whole X window for a given window.
1158 static void destroy_whole_window( Display *display, struct x11drv_win_data *data )
1160 struct x11drv_thread_data *thread_data = x11drv_thread_data();
1162 if (!data->whole_window) return;
1164 TRACE( "win %p xwin %lx\n", data->hwnd, data->whole_window );
1165 if (thread_data->cursor_window == data->whole_window) thread_data->cursor_window = None;
1167 XDeleteContext( display, data->whole_window, winContext );
1168 XDestroyWindow( display, data->whole_window );
1169 data->whole_window = 0;
1172 XUnsetICFocus( data->xic );
1173 XDestroyIC( data->xic );
1175 /* Outlook stops processing messages after destroying a dialog, so we need an explicit flush */
1177 XFree( data->wm_hints );
1178 data->wm_hints = NULL;
1179 wine_tsx11_unlock();
1180 RemovePropA( data->hwnd, whole_window_prop );
1184 /*****************************************************************
1185 * SetWindowText (X11DRV.@)
1187 void X11DRV_SetWindowText( HWND hwnd, LPCWSTR text )
1189 Display *display = thread_display();
1192 if ((win = X11DRV_get_whole_window( hwnd )) && win != DefaultRootWindow(display))
1193 sync_window_text( display, win, text );
1197 /***********************************************************************
1198 * DestroyWindow (X11DRV.@)
1200 void X11DRV_DestroyWindow( HWND hwnd )
1202 struct x11drv_thread_data *thread_data = x11drv_thread_data();
1203 Display *display = thread_data->display;
1204 struct x11drv_win_data *data;
1206 if (!(data = X11DRV_get_win_data( hwnd ))) return;
1210 destroy_glxpixmap(display, data->gl_drawable);
1212 XFreePixmap(display, data->pixmap);
1213 wine_tsx11_unlock();
1215 else if (data->gl_drawable)
1218 XDestroyWindow(display, data->gl_drawable);
1219 wine_tsx11_unlock();
1222 free_window_dce( data );
1223 destroy_whole_window( display, data );
1224 destroy_icon_window( display, data );
1226 if (thread_data->last_focus == hwnd) thread_data->last_focus = 0;
1227 if (data->hWMIconBitmap) DeleteObject( data->hWMIconBitmap );
1228 if (data->hWMIconMask) DeleteObject( data->hWMIconMask);
1230 XDeleteContext( display, (XID)hwnd, win_data_context );
1231 wine_tsx11_unlock();
1232 HeapFree( GetProcessHeap(), 0, data );
1236 static struct x11drv_win_data *alloc_win_data( Display *display, HWND hwnd )
1238 struct x11drv_win_data *data;
1240 if ((data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*data))))
1244 if (!winContext) winContext = XUniqueContext();
1245 if (!win_data_context) win_data_context = XUniqueContext();
1246 XSaveContext( display, (XID)hwnd, win_data_context, (char *)data );
1247 wine_tsx11_unlock();
1253 /* initialize the desktop window id in the desktop manager process */
1254 static struct x11drv_win_data *create_desktop_win_data( Display *display, HWND hwnd )
1256 struct x11drv_win_data *data;
1259 if (!(data = alloc_win_data( display, hwnd ))) return NULL;
1261 visualid = XVisualIDFromVisual(visual);
1262 wine_tsx11_unlock();
1263 data->whole_window = root_window;
1264 data->managed = TRUE;
1265 SetPropA( data->hwnd, managed_prop, (HANDLE)1 );
1266 SetPropA( data->hwnd, whole_window_prop, (HANDLE)root_window );
1267 SetPropA( data->hwnd, visual_id_prop, (HANDLE)visualid );
1268 set_initial_wm_hints( display, data );
1272 /**********************************************************************
1273 * CreateDesktopWindow (X11DRV.@)
1275 BOOL X11DRV_CreateDesktopWindow( HWND hwnd )
1277 unsigned int width, height;
1279 /* retrieve the real size of the desktop */
1280 SERVER_START_REQ( get_window_rectangles )
1283 wine_server_call( req );
1284 width = reply->window.right - reply->window.left;
1285 height = reply->window.bottom - reply->window.top;
1289 if (!width && !height) /* not initialized yet */
1291 SERVER_START_REQ( set_window_pos )
1295 req->flags = SWP_NOZORDER;
1296 req->window.left = virtual_screen_rect.left;
1297 req->window.top = virtual_screen_rect.top;
1298 req->window.right = virtual_screen_rect.right;
1299 req->window.bottom = virtual_screen_rect.bottom;
1300 req->client = req->window;
1301 wine_server_call( req );
1307 Window win = (Window)GetPropA( hwnd, whole_window_prop );
1308 if (win && win != root_window) X11DRV_init_desktop( win, width, height );
1314 /**********************************************************************
1315 * CreateWindow (X11DRV.@)
1317 BOOL X11DRV_CreateWindow( HWND hwnd )
1319 Display *display = thread_display();
1322 if (hwnd == GetDesktopWindow() && root_window != DefaultRootWindow( display ))
1324 /* the desktop win data can't be created lazily */
1325 if (!create_desktop_win_data( display, hwnd )) return FALSE;
1328 /* Show the window, maximizing or minimizing if needed */
1330 style = GetWindowLongW( hwnd, GWL_STYLE );
1331 if (style & (WS_MINIMIZE | WS_MAXIMIZE))
1333 extern UINT WINPOS_MinMaximize( HWND hwnd, UINT cmd, LPRECT rect ); /*FIXME*/
1336 UINT swFlag = (style & WS_MINIMIZE) ? SW_MINIMIZE : SW_MAXIMIZE;
1337 WIN_SetStyle( hwnd, 0, WS_MAXIMIZE | WS_MINIMIZE );
1338 swFlag = WINPOS_MinMaximize( hwnd, swFlag, &newPos );
1340 swFlag |= SWP_FRAMECHANGED; /* Frame always gets changed */
1341 if (!(style & WS_VISIBLE) || (style & WS_CHILD) || GetActiveWindow()) swFlag |= SWP_NOACTIVATE;
1343 SetWindowPos( hwnd, 0, newPos.left, newPos.top,
1344 newPos.right, newPos.bottom, swFlag );
1351 /***********************************************************************
1352 * X11DRV_get_win_data
1354 * Return the X11 data structure associated with a window.
1356 struct x11drv_win_data *X11DRV_get_win_data( HWND hwnd )
1360 if (!hwnd || XFindContext( thread_display(), (XID)hwnd, win_data_context, &data )) data = NULL;
1361 return (struct x11drv_win_data *)data;
1365 /***********************************************************************
1366 * X11DRV_create_win_data
1368 * Create an X11 data window structure for an existing window.
1370 struct x11drv_win_data *X11DRV_create_win_data( HWND hwnd )
1372 Display *display = thread_display();
1373 struct x11drv_win_data *data;
1376 if (!(parent = GetAncestor( hwnd, GA_PARENT ))) return NULL; /* desktop */
1377 if (!(data = alloc_win_data( display, hwnd ))) return NULL;
1379 GetWindowRect( hwnd, &data->window_rect );
1380 MapWindowPoints( 0, parent, (POINT *)&data->window_rect, 2 );
1381 data->whole_rect = data->window_rect;
1382 GetClientRect( hwnd, &data->client_rect );
1383 MapWindowPoints( hwnd, parent, (POINT *)&data->client_rect, 2 );
1385 if (parent == GetDesktopWindow())
1387 if (!create_whole_window( display, data ))
1389 HeapFree( GetProcessHeap(), 0, data );
1392 TRACE( "win %p/%lx window %s whole %s client %s\n",
1393 hwnd, data->whole_window, wine_dbgstr_rect( &data->window_rect ),
1394 wine_dbgstr_rect( &data->whole_rect ), wine_dbgstr_rect( &data->client_rect ));
1397 /* get class or window DC if needed */
1398 alloc_window_dce( data );
1403 /***********************************************************************
1404 * X11DRV_get_whole_window
1406 * Return the X window associated with the full area of a window
1408 Window X11DRV_get_whole_window( HWND hwnd )
1410 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
1414 if (hwnd == GetDesktopWindow()) return root_window;
1415 return (Window)GetPropA( hwnd, whole_window_prop );
1417 return data->whole_window;
1421 /***********************************************************************
1422 * X11DRV_get_fbconfig_id
1424 * Return the GLXFBConfig ID of the drawable used by the window for
1425 * OpenGL rendering. This is 0 for windows without a pixel format set.
1427 XID X11DRV_get_fbconfig_id( HWND hwnd )
1429 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
1431 if (!data) return (XID)GetPropA( hwnd, fbconfig_id_prop );
1432 return data->fbconfig_id;
1435 /***********************************************************************
1436 * X11DRV_get_gl_drawable
1438 * Return the GL drawable for this window.
1440 Drawable X11DRV_get_gl_drawable( HWND hwnd )
1442 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
1444 if (!data) return (Drawable)GetPropA( hwnd, gl_drawable_prop );
1445 return data->gl_drawable;
1448 /***********************************************************************
1449 * X11DRV_get_gl_pixmap
1451 * Return the Pixmap associated with the GL drawable (if any) for this window.
1453 Pixmap X11DRV_get_gl_pixmap( HWND hwnd )
1455 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
1457 if (!data) return (Pixmap)GetPropA( hwnd, pixmap_prop );
1458 return data->pixmap;
1462 /***********************************************************************
1465 * Return the X input context associated with a window
1467 XIC X11DRV_get_ic( HWND hwnd )
1469 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
1471 if (!data) return 0;
1476 /*****************************************************************
1477 * SetParent (X11DRV.@)
1479 void X11DRV_SetParent( HWND hwnd, HWND parent, HWND old_parent )
1481 Display *display = thread_display();
1482 struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
1485 if (parent == old_parent) return;
1487 if (parent != GetDesktopWindow()) /* a child window */
1489 if (old_parent == GetDesktopWindow())
1491 /* destroy the old X windows */
1492 destroy_whole_window( display, data );
1493 destroy_icon_window( display, data );
1496 data->managed = FALSE;
1497 RemovePropA( data->hwnd, managed_prop );
1501 else /* new top level window */
1503 /* FIXME: we ignore errors since we can't really recover anyway */
1504 create_whole_window( display, data );
1509 /*****************************************************************
1510 * SetFocus (X11DRV.@)
1513 * Explicit colormap management seems to work only with OLVWM.
1515 void X11DRV_SetFocus( HWND hwnd )
1517 Display *display = thread_display();
1518 struct x11drv_win_data *data;
1519 XWindowAttributes win_attr;
1521 /* Only mess with the X focus if there's */
1522 /* no desktop window and if the window is not managed by the WM. */
1523 if (root_window != DefaultRootWindow(display)) return;
1525 if (!hwnd) /* If setting the focus to 0, uninstall the colormap */
1528 if (X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_PRIVATE)
1529 XUninstallColormap( display, X11DRV_PALETTE_PaletteXColormap );
1530 wine_tsx11_unlock();
1534 hwnd = GetAncestor( hwnd, GA_ROOT );
1536 if (!(data = X11DRV_get_win_data( hwnd ))) return;
1537 if (data->managed || !data->whole_window) return;
1539 /* Set X focus and install colormap */
1541 if (XGetWindowAttributes( display, data->whole_window, &win_attr ) &&
1542 (win_attr.map_state == IsViewable))
1544 /* If window is not viewable, don't change anything */
1546 /* we must not use CurrentTime (ICCCM), so try to use last message time instead */
1547 /* FIXME: this is not entirely correct */
1548 XSetInputFocus( display, data->whole_window, RevertToParent,
1550 GetMessageTime() - EVENT_x11_time_to_win32_time(0));
1551 if (X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_PRIVATE)
1552 XInstallColormap( display, X11DRV_PALETTE_PaletteXColormap );
1554 wine_tsx11_unlock();
1558 /**********************************************************************
1559 * SetWindowIcon (X11DRV.@)
1561 * hIcon or hIconSm has changed (or is being initialised for the
1562 * first time). Complete the X11 driver-specific initialisation
1563 * and set the window hints.
1565 * This is not entirely correct, may need to create
1566 * an icon window and set the pixmap as a background
1568 void X11DRV_SetWindowIcon( HWND hwnd, UINT type, HICON icon )
1570 Display *display = thread_display();
1571 struct x11drv_win_data *data;
1573 if (type != ICON_BIG) return; /* nothing to do here */
1575 if (!(data = X11DRV_get_win_data( hwnd ))) return;
1576 if (!data->whole_window) return;
1577 if (!data->managed) return;
1581 set_icon_hints( display, data, icon );
1583 XSetWMHints( display, data->whole_window, data->wm_hints );
1584 wine_tsx11_unlock();
1589 /***********************************************************************
1590 * SetWindowRgn (X11DRV.@)
1592 * Assign specified region to window (for non-rectangular windows)
1594 int X11DRV_SetWindowRgn( HWND hwnd, HRGN hrgn, BOOL redraw )
1596 struct x11drv_win_data *data;
1598 if ((data = X11DRV_get_win_data( hwnd )))
1600 sync_window_region( thread_display(), data, hrgn );
1601 invalidate_dce( hwnd, &data->window_rect );
1603 else if (GetWindowThreadProcessId( hwnd, NULL ) != GetCurrentThreadId())
1605 FIXME( "not supported on other thread window %p\n", hwnd );
1606 SetLastError( ERROR_INVALID_WINDOW_HANDLE );