winex11: Setting window z-order using a sibling doesn't work with some window managers.
[wine] / dlls / winex11.drv / window.c
1 /*
2  * Window related functions
3  *
4  * Copyright 1993, 1994, 1995, 1996, 2001 Alexandre Julliard
5  * Copyright 1993 David Metcalfe
6  * Copyright 1995, 1996 Alex Korobka
7  *
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.
12  *
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.
17  *
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
21  */
22
23 #include "config.h"
24
25 #include <stdarg.h>
26 #include <stdlib.h>
27 #include <stdio.h>
28 #ifdef HAVE_UNISTD_H
29 # include <unistd.h>
30 #endif
31
32 #include <X11/Xlib.h>
33 #include <X11/Xresource.h>
34 #include <X11/Xutil.h>
35 #ifdef HAVE_LIBXSHAPE
36 #include <X11/extensions/shape.h>
37 #endif /* HAVE_LIBXSHAPE */
38
39 #include "windef.h"
40 #include "winbase.h"
41 #include "wingdi.h"
42 #include "winuser.h"
43 #include "wine/unicode.h"
44
45 #include "x11drv.h"
46 #include "xcomposite.h"
47 #include "wine/debug.h"
48 #include "wine/server.h"
49 #include "mwm.h"
50
51 WINE_DEFAULT_DEBUG_CHANNEL(x11drv);
52
53 /* X context to associate a hwnd to an X window */
54 XContext winContext = 0;
55
56 /* X context to associate a struct x11drv_win_data to an hwnd */
57 static XContext win_data_context;
58
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";
66
67 /* for XDG systray icons */
68 #define SYSTEM_TRAY_REQUEST_DOCK    0
69
70 extern int usexcomposite;
71
72 /***********************************************************************
73  *              is_window_managed
74  *
75  * Check if a given window should be managed
76  */
77 BOOL is_window_managed( HWND hwnd, UINT swp_flags, const RECT *window_rect )
78 {
79     DWORD style, ex_style;
80
81     if (!managed_mode) return FALSE;
82
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;
98     if (style & WS_POPUP)
99     {
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)
105             return TRUE;
106     }
107     /* default: not managed */
108     return FALSE;
109 }
110
111
112 /***********************************************************************
113  *              X11DRV_is_window_rect_mapped
114  *
115  * Check if the X whole window should be mapped based on its rectangle
116  */
117 BOOL X11DRV_is_window_rect_mapped( const RECT *rect )
118 {
119     /* don't map if rect is empty */
120     if (IsRectEmpty( rect )) return FALSE;
121
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)
127         return FALSE;
128
129     return TRUE;
130 }
131
132
133 /***********************************************************************
134  *              get_mwm_decorations
135  */
136 static unsigned long get_mwm_decorations( DWORD style, DWORD ex_style )
137 {
138     unsigned long ret = 0;
139
140     if (ex_style & WS_EX_TOOLWINDOW) return 0;
141
142     if ((style & WS_CAPTION) == WS_CAPTION)
143     {
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;
148     }
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;
152     return ret;
153 }
154
155
156 /***********************************************************************
157  *              get_x11_rect_offset
158  *
159  * Helper for X11DRV_window_to_X_rect and X11DRV_X_to_window_rect.
160  */
161 static void get_x11_rect_offset( struct x11drv_win_data *data, RECT *rect )
162 {
163     DWORD style, ex_style, style_mask = 0, ex_style_mask = 0;
164     unsigned long decor;
165
166     rect->top = rect->bottom = rect->left = rect->right = 0;
167
168     style = GetWindowLongW( data->hwnd, GWL_STYLE );
169     ex_style = GetWindowLongW( data->hwnd, GWL_EXSTYLE );
170     decor = get_mwm_decorations( style, ex_style );
171
172     if (decor & MWM_DECOR_TITLE) style_mask |= WS_CAPTION;
173     if (decor & MWM_DECOR_BORDER)
174     {
175         style_mask |= WS_DLGFRAME | WS_THICKFRAME;
176         ex_style_mask |= WS_EX_DLGMODALFRAME;
177     }
178
179     AdjustWindowRectEx( rect, style & style_mask, FALSE, ex_style & ex_style_mask );
180 }
181
182
183 /***********************************************************************
184  *              get_window_attributes
185  *
186  * Fill the window attributes structure for an X window.
187  */
188 static int get_window_attributes( Display *display, struct x11drv_win_data *data,
189                                   XSetWindowAttributes *attr )
190 {
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;
201
202     return (CWOverrideRedirect | CWSaveUnder | CWColormap | CWCursor |
203             CWEventMask | CWBitGravity | CWBackingStore);
204 }
205
206
207 /***********************************************************************
208  *              create_client_window
209  */
210 static Window create_client_window( Display *display, struct x11drv_win_data *data, XVisualInfo *vis )
211 {
212     int cx, cy, mask;
213     XSetWindowAttributes attr;
214     Window client;
215
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;
222
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;
225
226     wine_tsx11_lock();
227
228     if (vis)
229     {
230         attr.colormap = XCreateColormap( display, root_window, vis->visual,
231                                          (vis->class == PseudoColor || vis->class == GrayScale ||
232                                           vis->class == DirectColor) ? AllocAll : AllocNone );
233         mask |= CWColormap;
234     }
235
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 );
241     if (!client)
242     {
243         wine_tsx11_unlock();
244         return 0;
245     }
246
247     if (data->client_window)
248     {
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 );
253     }
254     data->client_window = client;
255
256     if (data->colormap) XFreeColormap( display, data->colormap );
257     data->colormap = vis ? attr.colormap : 0;
258
259     XMapWindow( display, data->client_window );
260     XSaveContext( display, data->client_window, winContext, (char *)data->hwnd );
261     wine_tsx11_unlock();
262
263     SetPropA( data->hwnd, client_window_prop, (HANDLE)data->client_window );
264     return data->client_window;
265 }
266
267
268 /***********************************************************************
269  *              X11DRV_sync_window_style
270  *
271  * Change the X window attributes when the window style has changed.
272  */
273 void X11DRV_sync_window_style( Display *display, struct x11drv_win_data *data )
274 {
275     if (data->whole_window != root_window)
276     {
277         XSetWindowAttributes attr;
278         int mask = get_window_attributes( display, data, &attr );
279
280         wine_tsx11_lock();
281         XChangeWindowAttributes( display, data->whole_window, mask, &attr );
282         wine_tsx11_unlock();
283     }
284 }
285
286
287 /***********************************************************************
288  *              sync_window_region
289  *
290  * Update the X11 window region.
291  */
292 static void sync_window_region( Display *display, struct x11drv_win_data *data, HRGN hrgn )
293 {
294 #ifdef HAVE_LIBXSHAPE
295     if (!data->whole_window) return;
296
297     if (!hrgn)
298     {
299         wine_tsx11_lock();
300         XShapeCombineMask( display, data->whole_window, ShapeBounding, 0, 0, None, ShapeSet );
301         wine_tsx11_unlock();
302     }
303     else
304     {
305         RGNDATA *pRegionData = X11DRV_GetRegionData( hrgn, 0 );
306         if (pRegionData)
307         {
308             wine_tsx11_lock();
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 );
314             wine_tsx11_unlock();
315             HeapFree(GetProcessHeap(), 0, pRegionData);
316         }
317     }
318 #endif  /* HAVE_LIBXSHAPE */
319 }
320
321
322 /***********************************************************************
323  *              sync_window_text
324  */
325 static void sync_window_text( Display *display, Window win, const WCHAR *text )
326 {
327     UINT count;
328     char *buffer, *utf8_buffer;
329     XTextProperty prop;
330
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);
335
336     count = WideCharToMultiByte(CP_UTF8, 0, text, strlenW(text), NULL, 0, NULL, NULL);
337     if (!(utf8_buffer = HeapAlloc( GetProcessHeap(), 0, count )))
338     {
339         HeapFree( GetProcessHeap(), 0, buffer );
340         return;
341     }
342     WideCharToMultiByte(CP_UTF8, 0, text, strlenW(text), utf8_buffer, count, NULL, NULL);
343
344     wine_tsx11_lock();
345     if (XmbTextListToTextProperty( display, &buffer, 1, XStdICCTextStyle, &prop ) == Success)
346     {
347         XSetWMName( display, win, &prop );
348         XSetWMIconName( display, win, &prop );
349         XFree( prop.value );
350     }
351     /*
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 ).
355     */
356     XChangeProperty( display, win, x11drv_atom(_NET_WM_NAME), x11drv_atom(UTF8_STRING),
357                      8, PropModeReplace, (unsigned char *) utf8_buffer, count);
358     wine_tsx11_unlock();
359
360     HeapFree( GetProcessHeap(), 0, utf8_buffer );
361     HeapFree( GetProcessHeap(), 0, buffer );
362 }
363
364
365 /***********************************************************************
366  *              X11DRV_set_win_format
367  */
368 BOOL X11DRV_set_win_format( HWND hwnd, XID fbconfig_id )
369 {
370     Display *display = thread_display();
371     struct x11drv_win_data *data;
372     XVisualInfo *vis;
373     int w, h;
374
375     if (!(data = X11DRV_get_win_data(hwnd)) &&
376         !(data = X11DRV_create_win_data(hwnd))) return FALSE;
377
378     if (data->fbconfig_id) return FALSE;  /* can't change it twice */
379
380     wine_tsx11_lock();
381     vis = visual_from_fbconfig_id(fbconfig_id);
382     wine_tsx11_unlock();
383     if (!vis) return FALSE;
384
385     if (data->whole_window)
386     {
387         Window client = data->client_window;
388
389         if (vis->visualid != XVisualIDFromVisual(visual))
390         {
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 );
393         }
394         wine_tsx11_lock();
395         XFree(vis);
396         wine_tsx11_unlock();
397         if (client) goto done;
398         return FALSE;
399     }
400
401     w = data->client_rect.right - data->client_rect.left;
402     h = data->client_rect.bottom - data->client_rect.top;
403
404     if(w <= 0) w = 1;
405     if(h <= 0) h = 1;
406
407 #ifdef SONAME_LIBXCOMPOSITE
408     if(usexcomposite)
409     {
410         XSetWindowAttributes attrib;
411         Window parent = X11DRV_get_whole_window( GetAncestor( hwnd, GA_ROOT ));
412
413         if (!parent) parent = root_window;
414         wine_tsx11_lock();
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);
423
424         data->gl_drawable = XCreateWindow(display, parent, -w, 0, w, h, 0,
425                                           vis->depth, InputOutput, vis->visual,
426                                           CWColormap | CWOverrideRedirect,
427                                           &attrib);
428         if(data->gl_drawable)
429         {
430             pXCompositeRedirectWindow(display, data->gl_drawable,
431                                       CompositeRedirectManual);
432             XMapWindow(display, data->gl_drawable);
433         }
434         XFree(vis);
435         wine_tsx11_unlock();
436     }
437     else
438 #endif
439     {
440         WARN("XComposite is not available, using GLXPixmap hack\n");
441
442         wine_tsx11_lock();
443         data->pixmap = XCreatePixmap(display, root_window, w, h, vis->depth);
444         if(!data->pixmap)
445         {
446             XFree(vis);
447             wine_tsx11_unlock();
448             return FALSE;
449         }
450
451         data->gl_drawable = create_glxpixmap(display, vis, data->pixmap);
452         if(!data->gl_drawable)
453         {
454             XFreePixmap(display, data->pixmap);
455             data->pixmap = 0;
456         }
457         XFree(vis);
458         wine_tsx11_unlock();
459         if (data->pixmap) SetPropA(hwnd, pixmap_prop, (HANDLE)data->pixmap);
460     }
461
462     if (!data->gl_drawable) return FALSE;
463
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);
467
468 done:
469     data->fbconfig_id = fbconfig_id;
470     SetPropA(hwnd, fbconfig_id_prop, (HANDLE)data->fbconfig_id);
471     wine_tsx11_lock();
472     XFlush( display );
473     wine_tsx11_unlock();
474     WIN_invalidate_dce( hwnd, NULL );
475     return TRUE;
476 }
477
478 /***********************************************************************
479  *              sync_gl_drawable
480  */
481 static void sync_gl_drawable(Display *display, struct x11drv_win_data *data)
482 {
483     int w = data->client_rect.right - data->client_rect.left;
484     int h = data->client_rect.bottom - data->client_rect.top;
485     XVisualInfo *vis;
486     Drawable glxp;
487     Pixmap pix;
488
489     if (w <= 0) w = 1;
490     if (h <= 0) h = 1;
491
492     TRACE("Resizing GL drawable 0x%lx to %dx%d\n", data->gl_drawable, w, h);
493 #ifdef SONAME_LIBXCOMPOSITE
494     if(usexcomposite)
495     {
496         wine_tsx11_lock();
497         XMoveResizeWindow(display, data->gl_drawable, -w, 0, w, h);
498         wine_tsx11_unlock();
499         return;
500     }
501 #endif
502
503     wine_tsx11_lock();
504
505     vis = visual_from_fbconfig_id(data->fbconfig_id);
506     if(!vis)
507     {
508         wine_tsx11_unlock();
509         return;
510     }
511
512     pix = XCreatePixmap(display, root_window, w, h, vis->depth);
513     if(!pix)
514     {
515         ERR("Failed to create pixmap for offscreen rendering\n");
516         XFree(vis);
517         wine_tsx11_unlock();
518         return;
519     }
520
521     glxp = create_glxpixmap(display, vis, pix);
522     if(!glxp)
523     {
524         ERR("Failed to create drawable for offscreen rendering\n");
525         XFreePixmap(display, pix);
526         XFree(vis);
527         wine_tsx11_unlock();
528         return;
529     }
530
531     XFree(vis);
532
533     mark_drawable_dirty(data->gl_drawable, glxp);
534
535     XFreePixmap(display, data->pixmap);
536     destroy_glxpixmap(display, data->gl_drawable);
537
538     data->pixmap = pix;
539     data->gl_drawable = glxp;
540
541     wine_tsx11_unlock();
542
543     SetPropA(data->hwnd, gl_drawable_prop, (HANDLE)data->gl_drawable);
544     SetPropA(data->hwnd, pixmap_prop, (HANDLE)data->pixmap);
545 }
546
547
548 /***********************************************************************
549  *              get_window_changes
550  *
551  * fill the window changes structure
552  */
553 static int get_window_changes( XWindowChanges *changes, const RECT *old, const RECT *new )
554 {
555     int mask = 0;
556
557     if (old->right - old->left != new->right - new->left )
558     {
559         if ((changes->width = new->right - new->left) <= 0) changes->width = 1;
560         mask |= CWWidth;
561     }
562     if (old->bottom - old->top != new->bottom - new->top)
563     {
564         if ((changes->height = new->bottom - new->top) <= 0) changes->height = 1;
565         mask |= CWHeight;
566     }
567     if (old->left != new->left)
568     {
569         changes->x = new->left;
570         mask |= CWX;
571     }
572     if (old->top != new->top)
573     {
574         changes->y = new->top;
575         mask |= CWY;
576     }
577     return mask;
578 }
579
580
581 /***********************************************************************
582  *              create_icon_window
583  */
584 static Window create_icon_window( Display *display, struct x11drv_win_data *data )
585 {
586     XSetWindowAttributes attr;
587
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 */
593
594     wine_tsx11_lock();
595     data->icon_window = XCreateWindow( display, root_window, 0, 0,
596                                        GetSystemMetrics( SM_CXICON ),
597                                        GetSystemMetrics( SM_CYICON ),
598                                        0, screen_depth,
599                                        InputOutput, visual,
600                                        CWEventMask | CWBitGravity | CWBackingStore | CWColormap, &attr );
601     XSaveContext( display, data->icon_window, winContext, (char *)data->hwnd );
602     wine_tsx11_unlock();
603
604     TRACE( "created %lx\n", data->icon_window );
605     SetPropA( data->hwnd, icon_window_prop, (HANDLE)data->icon_window );
606     return data->icon_window;
607 }
608
609
610
611 /***********************************************************************
612  *              destroy_icon_window
613  */
614 static void destroy_icon_window( Display *display, struct x11drv_win_data *data )
615 {
616     if (!data->icon_window) return;
617     if (x11drv_thread_data()->cursor_window == data->icon_window)
618         x11drv_thread_data()->cursor_window = None;
619     wine_tsx11_lock();
620     XDeleteContext( display, data->icon_window, winContext );
621     XDestroyWindow( display, data->icon_window );
622     data->icon_window = 0;
623     wine_tsx11_unlock();
624     RemovePropA( data->hwnd, icon_window_prop );
625 }
626
627
628 /***********************************************************************
629  *              set_icon_hints
630  *
631  * Set the icon wm hints
632  */
633 static void set_icon_hints( Display *display, struct x11drv_win_data *data, HICON hIcon )
634 {
635     XWMHints *hints = data->wm_hints;
636
637     if (data->hWMIconBitmap) DeleteObject( data->hWMIconBitmap );
638     if (data->hWMIconMask) DeleteObject( data->hWMIconMask);
639     data->hWMIconBitmap = 0;
640     data->hWMIconMask = 0;
641
642     if (!hIcon)
643     {
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;
647     }
648     else
649     {
650         HBITMAP hbmOrig;
651         RECT rcMask;
652         BITMAP bmMask;
653         ICONINFO ii;
654         HDC hDC;
655
656         GetIconInfo(hIcon, &ii);
657
658         GetObjectA(ii.hbmMask, sizeof(bmMask), &bmMask);
659         rcMask.top    = 0;
660         rcMask.left   = 0;
661         rcMask.right  = bmMask.bmWidth;
662         rcMask.bottom = bmMask.bmHeight;
663
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);
669         DeleteDC(hDC);
670
671         data->hWMIconBitmap = ii.hbmColor;
672         data->hWMIconMask = ii.hbmMask;
673
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;
678     }
679 }
680
681 /***********************************************************************
682  *              wine_make_systray_window   (X11DRV.@)
683  *
684  * Docks the given X window with the NETWM system tray.
685  */
686 void X11DRV_make_systray_window( HWND hwnd )
687 {
688     static Atom systray_atom;
689     Display *display = thread_display();
690     struct x11drv_win_data *data;
691     Window systray_window;
692
693     if (!(data = X11DRV_get_win_data( hwnd )) &&
694         !(data = X11DRV_create_win_data( hwnd ))) return;
695
696     wine_tsx11_lock();
697     if (!systray_atom)
698     {
699         if (DefaultScreen( display ) == 0)
700             systray_atom = x11drv_atom(_NET_SYSTEM_TRAY_S0);
701         else
702         {
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 );
706         }
707     }
708     systray_window = XGetSelectionOwner( display, systray_atom );
709     wine_tsx11_unlock();
710
711     TRACE("Docking tray icon %p\n", data->hwnd);
712
713     if (systray_window != None)
714     {
715         XEvent ev;
716         unsigned long info[2];
717                 
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. 
722          *
723          * For more information on this problem, see
724          * http://standards.freedesktop.org/xembed-spec/latest/ar01s04.html */
725         
726         SetWindowPos( data->hwnd, NULL, virtual_screen_rect.right + 1, virtual_screen_rect.bottom + 1,
727                       0, 0, SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE );
728
729         /* set XEMBED protocol data on the window */
730         info[0] = 0; /* protocol version */
731         info[1] = 1; /* mapped = true */
732         
733         wine_tsx11_lock();
734         XChangeProperty( display, data->whole_window,
735                          x11drv_atom(_XEMBED_INFO),
736                          x11drv_atom(_XEMBED_INFO), 32, PropModeReplace,
737                          (unsigned char*)info, 2 );
738         wine_tsx11_unlock();
739     
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;
751         
752         wine_tsx11_lock();
753         XSendEvent( display, systray_window, False, NoEventMask, &ev );
754         wine_tsx11_unlock();
755
756     }
757     else
758     {
759         int val = 1;
760
761         /* fall back to he KDE hints if the WM doesn't support XEMBED'ed
762          * systrays */
763         
764         wine_tsx11_lock();
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 );
773        wine_tsx11_unlock();
774     }
775 }
776
777
778 /***********************************************************************
779  *              set_size_hints
780  *
781  * set the window size hints
782  */
783 static void set_size_hints( Display *display, struct x11drv_win_data *data, DWORD style )
784 {
785     XSizeHints* size_hints;
786
787     if ((size_hints = XAllocSizeHints()))
788     {
789         size_hints->flags = 0;
790
791         if (data->hwnd != GetDesktopWindow())  /* don't force position of desktop */
792         {
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;
797         }
798
799         if ( !(style & WS_THICKFRAME) )
800         {
801             /* If we restrict window resizing Metacity decides that it should
802              * disable fullscreen support for this window as well.
803              */
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))
806             {
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;
812             }
813         }
814         XSetWMNormalHints( display, data->whole_window, size_hints );
815         XFree( size_hints );
816     }
817 }
818
819
820 /***********************************************************************
821  *              get_process_name
822  *
823  * get the name of the current process for setting class hints
824  */
825 static char *get_process_name(void)
826 {
827     static char *name;
828
829     if (!name)
830     {
831         WCHAR module[MAX_PATH];
832         DWORD len = GetModuleFileNameW( 0, module, MAX_PATH );
833         if (len && len < MAX_PATH)
834         {
835             char *ptr;
836             WCHAR *p, *appname = module;
837
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 )))
842             {
843                 WideCharToMultiByte( CP_UNIXCP, 0, appname, -1, ptr, len, NULL, NULL );
844                 name = ptr;
845             }
846         }
847     }
848     return name;
849 }
850
851
852 /***********************************************************************
853  *              set_initial_wm_hints
854  *
855  * Set the window manager hints that don't change over the lifetime of a window.
856  */
857 static void set_initial_wm_hints( Display *display, struct x11drv_win_data *data )
858 {
859     int i;
860     Atom protocols[3];
861     Atom dndVersion = 4;
862     XClassHint *class_hints;
863     char *process_name = get_process_name();
864
865     wine_tsx11_lock();
866
867     /* wm protocols */
868     i = 0;
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 );
874
875     /* class hints */
876     if ((class_hints = XAllocClassHint()))
877     {
878         static char wine[] = "Wine";
879
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 );
884     }
885
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 */
889     i = getpid();
890     XChangeProperty(display, data->whole_window, x11drv_atom(_NET_WM_PID),
891                     XA_CARDINAL, 32, PropModeReplace, (unsigned char *)&i, 1);
892
893     XChangeProperty( display, data->whole_window, x11drv_atom(XdndAware),
894                      XA_ATOM, 32, PropModeReplace, (unsigned char*)&dndVersion, 1 );
895
896     data->wm_hints = XAllocWMHints();
897     wine_tsx11_unlock();
898
899     if (data->wm_hints)
900     {
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 );
905     }
906 }
907
908
909 /***********************************************************************
910  *              X11DRV_set_wm_hints
911  *
912  * Set the window manager hints for a newly-created window
913  */
914 void X11DRV_set_wm_hints( Display *display, struct x11drv_win_data *data )
915 {
916     Window group_leader;
917     Atom window_type;
918     MwmHints mwm_hints;
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 );
922
923     if (data->hwnd == GetDesktopWindow())
924     {
925         /* force some styles for the desktop to get the correct decorations */
926         style |= WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX;
927         owner = 0;
928     }
929
930     /* transient for hint */
931     if (owner)
932     {
933         Window owner_win = X11DRV_get_whole_window( owner );
934         wine_tsx11_lock();
935         XSetTransientForHint( display, data->whole_window, owner_win );
936         wine_tsx11_unlock();
937         group_leader = owner_win;
938     }
939     else group_leader = data->whole_window;
940
941     wine_tsx11_lock();
942
943     /* size hints */
944     set_size_hints( display, data, style );
945
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);
952
953     XChangeProperty(display, data->whole_window, x11drv_atom(_NET_WM_WINDOW_TYPE),
954                     XA_ATOM, 32, PropModeReplace, (unsigned char*)&window_type, 1);
955
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;
963
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) );
967
968     /* wm hints */
969     if (data->wm_hints)
970     {
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 );
976     }
977
978     wine_tsx11_unlock();
979 }
980
981
982 /***********************************************************************
983  *              X11DRV_window_to_X_rect
984  *
985  * Convert a rect from client to X window coordinates
986  */
987 void X11DRV_window_to_X_rect( struct x11drv_win_data *data, RECT *rect )
988 {
989     RECT rc;
990
991     if (!data->managed) return;
992     if (IsRectEmpty( rect )) return;
993
994     get_x11_rect_offset( data, &rc );
995
996     rect->left   -= rc.left;
997     rect->right  -= rc.right;
998     rect->top    -= rc.top;
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;
1002 }
1003
1004
1005 /***********************************************************************
1006  *              X11DRV_X_to_window_rect
1007  *
1008  * Opposite of X11DRV_window_to_X_rect
1009  */
1010 void X11DRV_X_to_window_rect( struct x11drv_win_data *data, RECT *rect )
1011 {
1012     RECT rc;
1013
1014     if (!data->managed) return;
1015     if (IsRectEmpty( rect )) return;
1016
1017     get_x11_rect_offset( data, &rc );
1018
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;
1025 }
1026
1027
1028 /***********************************************************************
1029  *              X11DRV_sync_window_position
1030  *
1031  * Synchronize the X window position with the Windows one
1032  */
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 )
1036 {
1037     XWindowChanges changes;
1038     int mask = get_window_changes( &changes, old_whole_rect, &data->whole_rect );
1039
1040     if (!(swp_flags & SWP_NOZORDER))
1041     {
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 */
1047         {
1048             changes.stack_mode = Above;
1049             mask |= CWStackMode;
1050         }
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 */
1053     }
1054
1055     /* only the size is allowed to change for the desktop window */
1056     if (data->whole_window == root_window) mask &= CWWidth | CWHeight;
1057
1058     if (mask)
1059     {
1060         DWORD style = GetWindowLongW( data->hwnd, GWL_STYLE );
1061
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 );
1066
1067         wine_tsx11_lock();
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();
1074     }
1075 }
1076
1077
1078 /***********************************************************************
1079  *              X11DRV_sync_client_position
1080  *
1081  * Synchronize the X client window position with the Windows one
1082  */
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 )
1086 {
1087     int mask;
1088     XWindowChanges changes;
1089     RECT old = *old_client_rect;
1090     RECT new = data->client_rect;
1091
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;
1095
1096     if (data->client_window)
1097     {
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 );
1101         wine_tsx11_lock();
1102         XConfigureWindow( display, data->client_window, mask, &changes );
1103         wine_tsx11_unlock();
1104     }
1105
1106     if (data->gl_drawable && (mask & (CWWidth|CWHeight))) sync_gl_drawable( display, data );
1107
1108     /* make sure the changes get to the server before we start painting */
1109     if (data->client_window || data->gl_drawable)
1110     {
1111         wine_tsx11_lock();
1112         XFlush(display);
1113         wine_tsx11_unlock();
1114     }
1115 }
1116
1117
1118 /**********************************************************************
1119  *              create_whole_window
1120  *
1121  * Create the whole X window for a given window
1122  */
1123 static Window create_whole_window( Display *display, struct x11drv_win_data *data )
1124 {
1125     int cx, cy, mask;
1126     XSetWindowAttributes attr;
1127     XIM xim;
1128     WCHAR text[1024];
1129     HRGN hrgn;
1130
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;
1133
1134     if (!data->managed && is_window_managed( data->hwnd, SWP_NOACTIVATE, &data->window_rect ))
1135     {
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 );
1139     }
1140
1141     mask = get_window_attributes( display, data, &attr );
1142
1143     wine_tsx11_lock();
1144
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 );
1151
1152     if (data->whole_window) XSaveContext( display, data->whole_window, winContext, (char *)data->hwnd );
1153     wine_tsx11_unlock();
1154
1155     if (!data->whole_window) return 0;
1156
1157     if (!create_client_window( display, data, NULL ))
1158     {
1159         wine_tsx11_lock();
1160         XDeleteContext( display, data->whole_window, winContext );
1161         XDestroyWindow( display, data->whole_window );
1162         data->whole_window = 0;
1163         wine_tsx11_unlock();
1164         return 0;
1165     }
1166
1167     xim = x11drv_thread_data()->xim;
1168     if (xim) data->xic = X11DRV_CreateIC( xim, display, data->whole_window );
1169
1170     set_initial_wm_hints( display, data );
1171     X11DRV_set_wm_hints( display, data );
1172
1173     SetPropA( data->hwnd, whole_window_prop, (HANDLE)data->whole_window );
1174
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 );
1178
1179     /* set the window region */
1180     if ((hrgn = CreateRectRgn( 0, 0, 0, 0 )))
1181     {
1182         if (GetWindowRgn( data->hwnd, hrgn ) != ERROR) sync_window_region( display, data, hrgn );
1183         DeleteObject( hrgn );
1184     }
1185     return data->whole_window;
1186 }
1187
1188
1189 /**********************************************************************
1190  *              destroy_whole_window
1191  *
1192  * Destroy the whole X window for a given window.
1193  */
1194 static void destroy_whole_window( Display *display, struct x11drv_win_data *data, BOOL already_destroyed )
1195 {
1196     struct x11drv_thread_data *thread_data = x11drv_thread_data();
1197
1198     if (!data->whole_window) return;
1199
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;
1204     wine_tsx11_lock();
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;
1212     if (data->xic)
1213     {
1214         XUnsetICFocus( data->xic );
1215         XDestroyIC( data->xic );
1216         data->xic = 0;
1217     }
1218     /* Outlook stops processing messages after destroying a dialog, so we need an explicit flush */
1219     XFlush( display );
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 );
1225 }
1226
1227
1228 /*****************************************************************
1229  *              SetWindowText   (X11DRV.@)
1230  */
1231 void X11DRV_SetWindowText( HWND hwnd, LPCWSTR text )
1232 {
1233     Display *display = thread_display();
1234     Window win;
1235
1236     if ((win = X11DRV_get_whole_window( hwnd )) && win != DefaultRootWindow(display))
1237         sync_window_text( display, win, text );
1238 }
1239
1240
1241 /***********************************************************************
1242  *              DestroyWindow   (X11DRV.@)
1243  */
1244 void X11DRV_DestroyWindow( HWND hwnd )
1245 {
1246     struct x11drv_thread_data *thread_data = x11drv_thread_data();
1247     Display *display = thread_data->display;
1248     struct x11drv_win_data *data;
1249
1250     if (!(data = X11DRV_get_win_data( hwnd ))) return;
1251
1252     if (data->pixmap)
1253     {
1254         destroy_glxpixmap(display, data->gl_drawable);
1255         wine_tsx11_lock();
1256         XFreePixmap(display, data->pixmap);
1257         wine_tsx11_unlock();
1258     }
1259     else if (data->gl_drawable)
1260     {
1261         wine_tsx11_lock();
1262         XDestroyWindow(display, data->gl_drawable);
1263         wine_tsx11_unlock();
1264     }
1265
1266     destroy_whole_window( display, data, FALSE );
1267     destroy_icon_window( display, data );
1268
1269     if (data->colormap)
1270     {
1271         wine_tsx11_lock();
1272         XFreeColormap( display, data->colormap );
1273         wine_tsx11_unlock();
1274     }
1275
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);
1279     wine_tsx11_lock();
1280     XDeleteContext( display, (XID)hwnd, win_data_context );
1281     wine_tsx11_unlock();
1282     HeapFree( GetProcessHeap(), 0, data );
1283 }
1284
1285
1286 /***********************************************************************
1287  *              X11DRV_DestroyNotify
1288  */
1289 void X11DRV_DestroyNotify( HWND hwnd, XEvent *event )
1290 {
1291     Display *display = event->xdestroywindow.display;
1292     struct x11drv_win_data *data;
1293
1294     if (!(data = X11DRV_get_win_data( hwnd ))) return;
1295
1296     FIXME( "window %p/%lx destroyed from the outside\n", hwnd, data->whole_window );
1297     destroy_whole_window( display, data, TRUE );
1298 }
1299
1300
1301 static struct x11drv_win_data *alloc_win_data( Display *display, HWND hwnd )
1302 {
1303     struct x11drv_win_data *data;
1304
1305     if ((data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*data))))
1306     {
1307         data->hwnd = hwnd;
1308         wine_tsx11_lock();
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();
1313     }
1314     return data;
1315 }
1316
1317
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 )
1320 {
1321     struct x11drv_win_data *data;
1322     VisualID visualid;
1323
1324     if (!(data = alloc_win_data( display, hwnd ))) return NULL;
1325     wine_tsx11_lock();
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 );
1334     return data;
1335 }
1336
1337 /**********************************************************************
1338  *              CreateDesktopWindow   (X11DRV.@)
1339  */
1340 BOOL X11DRV_CreateDesktopWindow( HWND hwnd )
1341 {
1342     unsigned int width, height;
1343
1344     /* retrieve the real size of the desktop */
1345     SERVER_START_REQ( get_window_rectangles )
1346     {
1347         req->handle = hwnd;
1348         wine_server_call( req );
1349         width  = reply->window.right - reply->window.left;
1350         height = reply->window.bottom - reply->window.top;
1351     }
1352     SERVER_END_REQ;
1353
1354     if (!width && !height)  /* not initialized yet */
1355     {
1356         SERVER_START_REQ( set_window_pos )
1357         {
1358             req->handle        = hwnd;
1359             req->previous      = 0;
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 );
1367         }
1368         SERVER_END_REQ;
1369     }
1370     else
1371     {
1372         Window win = (Window)GetPropA( hwnd, whole_window_prop );
1373         if (win && win != root_window) X11DRV_init_desktop( win, width, height );
1374     }
1375     return TRUE;
1376 }
1377
1378
1379 /**********************************************************************
1380  *              CreateWindow   (X11DRV.@)
1381  */
1382 BOOL X11DRV_CreateWindow( HWND hwnd )
1383 {
1384     Display *display = thread_display();
1385
1386     if (hwnd == GetDesktopWindow() && root_window != DefaultRootWindow( display ))
1387     {
1388         /* the desktop win data can't be created lazily */
1389         if (!create_desktop_win_data( display, hwnd )) return FALSE;
1390     }
1391     return TRUE;
1392 }
1393
1394
1395 /***********************************************************************
1396  *              X11DRV_get_win_data
1397  *
1398  * Return the X11 data structure associated with a window.
1399  */
1400 struct x11drv_win_data *X11DRV_get_win_data( HWND hwnd )
1401 {
1402     char *data;
1403
1404     if (!hwnd || XFindContext( thread_display(), (XID)hwnd, win_data_context, &data )) data = NULL;
1405     return (struct x11drv_win_data *)data;
1406 }
1407
1408
1409 /***********************************************************************
1410  *              X11DRV_create_win_data
1411  *
1412  * Create an X11 data window structure for an existing window.
1413  */
1414 struct x11drv_win_data *X11DRV_create_win_data( HWND hwnd )
1415 {
1416     Display *display = thread_display();
1417     struct x11drv_win_data *data;
1418     HWND parent;
1419
1420     if (!(parent = GetAncestor( hwnd, GA_PARENT ))) return NULL;  /* desktop */
1421     if (!(data = alloc_win_data( display, hwnd ))) return NULL;
1422
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 );
1428
1429     if (parent == GetDesktopWindow())
1430     {
1431         if (!create_whole_window( display, data ))
1432         {
1433             HeapFree( GetProcessHeap(), 0, data );
1434             return NULL;
1435         }
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 ));
1439     }
1440     return data;
1441 }
1442
1443
1444 /***********************************************************************
1445  *              X11DRV_get_whole_window
1446  *
1447  * Return the X window associated with the full area of a window
1448  */
1449 Window X11DRV_get_whole_window( HWND hwnd )
1450 {
1451     struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
1452
1453     if (!data)
1454     {
1455         if (hwnd == GetDesktopWindow()) return root_window;
1456         return (Window)GetPropA( hwnd, whole_window_prop );
1457     }
1458     return data->whole_window;
1459 }
1460
1461
1462 /***********************************************************************
1463  *              X11DRV_get_client_window
1464  *
1465  * Return the X window associated with the client area of a window
1466  */
1467 Window X11DRV_get_client_window( HWND hwnd )
1468 {
1469     struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
1470
1471     if (!data)
1472     {
1473         if (hwnd == GetDesktopWindow()) return root_window;
1474         return (Window)GetPropA( hwnd, client_window_prop );
1475     }
1476     return data->client_window;
1477 }
1478
1479
1480 /***********************************************************************
1481  *              X11DRV_get_ic
1482  *
1483  * Return the X input context associated with a window
1484  */
1485 XIC X11DRV_get_ic( HWND hwnd )
1486 {
1487     struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
1488
1489     if (!data) return 0;
1490     return data->xic;
1491 }
1492
1493
1494 /***********************************************************************
1495  *              X11DRV_GetDC   (X11DRV.@)
1496  */
1497 void X11DRV_GetDC( HDC hdc, HWND hwnd, HWND top, const RECT *win_rect,
1498                    const RECT *top_rect, DWORD flags )
1499 {
1500     struct x11drv_escape_set_drawable escape;
1501     struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
1502
1503     escape.code        = X11DRV_SET_DRAWABLE;
1504     escape.mode        = IncludeInferiors;
1505     escape.fbconfig_id = 0;
1506     escape.gl_drawable = 0;
1507     escape.pixmap      = 0;
1508
1509     if (top == hwnd && data && IsIconic( hwnd ) && data->icon_window)
1510     {
1511         escape.drawable = data->icon_window;
1512     }
1513     else if (top == hwnd && (flags & DCX_WINDOW))
1514     {
1515         escape.drawable = data ? data->whole_window : X11DRV_get_whole_window( hwnd );
1516     }
1517     else
1518     {
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;
1524     }
1525
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;
1534
1535     ExtEscape( hdc, X11DRV_ESCAPE, sizeof(escape), (LPSTR)&escape, 0, NULL );
1536 }
1537
1538
1539 /***********************************************************************
1540  *              X11DRV_ReleaseDC  (X11DRV.@)
1541  */
1542 void X11DRV_ReleaseDC( HWND hwnd, HDC hdc )
1543 {
1544     struct x11drv_escape_set_drawable escape;
1545
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;
1554     escape.pixmap = 0;
1555     ExtEscape( hdc, X11DRV_ESCAPE, sizeof(escape), (LPSTR)&escape, 0, NULL );
1556 }
1557
1558
1559 /***********************************************************************
1560  *              SetCapture  (X11DRV.@)
1561  */
1562 void X11DRV_SetCapture( HWND hwnd, UINT flags )
1563 {
1564     struct x11drv_thread_data *thread_data = x11drv_thread_data();
1565
1566     if (!(flags & GUI_INMOVESIZE)) return;
1567
1568     if (hwnd)
1569     {
1570         Window grab_win = X11DRV_get_client_window( GetAncestor( hwnd, GA_ROOT ) );
1571
1572         if (!grab_win) return;
1573         wine_tsx11_lock();
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;
1580     }
1581     else  /* release capture */
1582     {
1583         wine_tsx11_lock();
1584         XFlush( gdi_display );
1585         XUngrabPointer( thread_data->display, CurrentTime );
1586         wine_tsx11_unlock();
1587         thread_data->grab_window = None;
1588     }
1589 }
1590
1591
1592 /*****************************************************************
1593  *              SetParent   (X11DRV.@)
1594  */
1595 void X11DRV_SetParent( HWND hwnd, HWND parent, HWND old_parent )
1596 {
1597     Display *display = thread_display();
1598     struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
1599
1600     if (!data) return;
1601     if (parent == old_parent) return;
1602
1603     if (parent != GetDesktopWindow()) /* a child window */
1604     {
1605         if (old_parent == GetDesktopWindow())
1606         {
1607             /* destroy the old X windows */
1608             destroy_whole_window( display, data, FALSE );
1609             destroy_icon_window( display, data );
1610             if (data->managed)
1611             {
1612                 data->managed = FALSE;
1613                 RemovePropA( data->hwnd, managed_prop );
1614             }
1615         }
1616     }
1617     else  /* new top level window */
1618     {
1619         /* FIXME: we ignore errors since we can't really recover anyway */
1620         create_whole_window( display, data );
1621     }
1622 }
1623
1624
1625 /*****************************************************************
1626  *              SetFocus   (X11DRV.@)
1627  *
1628  * Set the X focus.
1629  * Explicit colormap management seems to work only with OLVWM.
1630  */
1631 void X11DRV_SetFocus( HWND hwnd )
1632 {
1633     Display *display = thread_display();
1634     struct x11drv_win_data *data;
1635     XWindowChanges changes;
1636
1637     /* If setting the focus to 0, uninstall the colormap */
1638     if (!hwnd && root_window == DefaultRootWindow(display))
1639     {
1640         wine_tsx11_lock();
1641         if (X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_PRIVATE)
1642             XUninstallColormap( display, X11DRV_PALETTE_PaletteXColormap );
1643         wine_tsx11_unlock();
1644         return;
1645     }
1646
1647     hwnd = GetAncestor( hwnd, GA_ROOT );
1648
1649     if (!(data = X11DRV_get_win_data( hwnd ))) return;
1650     if (data->managed || !data->whole_window) return;
1651
1652     /* Set X focus and install colormap */
1653     wine_tsx11_lock();
1654     changes.stack_mode = Above;
1655     XConfigureWindow( display, data->whole_window, CWStackMode, &changes );
1656     if (root_window == DefaultRootWindow(display))
1657     {
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,
1661                         /* CurrentTime */
1662                         GetMessageTime() - EVENT_x11_time_to_win32_time(0));
1663         if (X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_PRIVATE)
1664             XInstallColormap( display, X11DRV_PALETTE_PaletteXColormap );
1665     }
1666     wine_tsx11_unlock();
1667 }
1668
1669
1670 /**********************************************************************
1671  *              SetWindowIcon (X11DRV.@)
1672  *
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.
1676  *
1677  * This is not entirely correct, may need to create
1678  * an icon window and set the pixmap as a background
1679  */
1680 void X11DRV_SetWindowIcon( HWND hwnd, UINT type, HICON icon )
1681 {
1682     Display *display = thread_display();
1683     struct x11drv_win_data *data;
1684
1685     if (type != ICON_BIG) return;  /* nothing to do here */
1686
1687     if (!(data = X11DRV_get_win_data( hwnd ))) return;
1688     if (!data->whole_window) return;
1689     if (!data->managed) return;
1690
1691     if (data->wm_hints)
1692     {
1693         set_icon_hints( display, data, icon );
1694         wine_tsx11_lock();
1695         XSetWMHints( display, data->whole_window, data->wm_hints );
1696         wine_tsx11_unlock();
1697     }
1698 }
1699
1700
1701 /***********************************************************************
1702  *              SetWindowRgn  (X11DRV.@)
1703  *
1704  * Assign specified region to window (for non-rectangular windows)
1705  */
1706 int X11DRV_SetWindowRgn( HWND hwnd, HRGN hrgn, BOOL redraw )
1707 {
1708     struct x11drv_win_data *data;
1709
1710     if ((data = X11DRV_get_win_data( hwnd )))
1711     {
1712         sync_window_region( thread_display(), data, hrgn );
1713     }
1714     else if (GetWindowThreadProcessId( hwnd, NULL ) != GetCurrentThreadId())
1715     {
1716         FIXME( "not supported on other thread window %p\n", hwnd );
1717         SetLastError( ERROR_INVALID_WINDOW_HANDLE );
1718         return FALSE;
1719     }
1720
1721     return TRUE;
1722 }