advapi32: Create MachineGuid value if it doesn't exist.
[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 (root_window != DefaultRootWindow(display)) return;
694
695     if (!(data = X11DRV_get_win_data( hwnd )) &&
696         !(data = X11DRV_create_win_data( hwnd ))) return;
697
698     wine_tsx11_lock();
699     if (!systray_atom)
700     {
701         if (DefaultScreen( display ) == 0)
702             systray_atom = x11drv_atom(_NET_SYSTEM_TRAY_S0);
703         else
704         {
705             char systray_buffer[29]; /* strlen(_NET_SYSTEM_TRAY_S4294967295)+1 */
706             sprintf( systray_buffer, "_NET_SYSTEM_TRAY_S%u", DefaultScreen( display ) );
707             systray_atom = XInternAtom( display, systray_buffer, False );
708         }
709     }
710     systray_window = XGetSelectionOwner( display, systray_atom );
711     wine_tsx11_unlock();
712
713     TRACE("Docking tray icon %p\n", data->hwnd);
714
715     if (systray_window != None)
716     {
717         XEvent ev;
718         unsigned long info[2];
719                 
720         /* Put the window offscreen so it isn't mapped. The window _cannot_ be 
721          * mapped if we intend to dock with an XEMBED tray. If the window is 
722          * mapped when we dock, it may become visible as a child of the root 
723          * window after it docks, which isn't the proper behavior. 
724          *
725          * For more information on this problem, see
726          * http://standards.freedesktop.org/xembed-spec/latest/ar01s04.html */
727         
728         SetWindowPos( data->hwnd, NULL, virtual_screen_rect.right + 1, virtual_screen_rect.bottom + 1,
729                       0, 0, SWP_NOZORDER | SWP_NOSIZE | SWP_NOACTIVATE );
730
731         /* set XEMBED protocol data on the window */
732         info[0] = 0; /* protocol version */
733         info[1] = 1; /* mapped = true */
734         
735         wine_tsx11_lock();
736         XChangeProperty( display, data->whole_window,
737                          x11drv_atom(_XEMBED_INFO),
738                          x11drv_atom(_XEMBED_INFO), 32, PropModeReplace,
739                          (unsigned char*)info, 2 );
740         wine_tsx11_unlock();
741     
742         /* send the docking request message */
743         ZeroMemory( &ev, sizeof(ev) ); 
744         ev.xclient.type = ClientMessage;
745         ev.xclient.window = systray_window;
746         ev.xclient.message_type = x11drv_atom( _NET_SYSTEM_TRAY_OPCODE );
747         ev.xclient.format = 32;
748         ev.xclient.data.l[0] = CurrentTime;
749         ev.xclient.data.l[1] = SYSTEM_TRAY_REQUEST_DOCK;
750         ev.xclient.data.l[2] = data->whole_window;
751         ev.xclient.data.l[3] = 0;
752         ev.xclient.data.l[4] = 0;
753         
754         wine_tsx11_lock();
755         XSendEvent( display, systray_window, False, NoEventMask, &ev );
756         wine_tsx11_unlock();
757
758     }
759     else
760     {
761         int val = 1;
762
763         /* fall back to he KDE hints if the WM doesn't support XEMBED'ed
764          * systrays */
765         
766         wine_tsx11_lock();
767         XChangeProperty( display, data->whole_window, 
768                          x11drv_atom(KWM_DOCKWINDOW),
769                          x11drv_atom(KWM_DOCKWINDOW), 32, PropModeReplace,
770                          (unsigned char*)&val, 1 );
771         XChangeProperty( display, data->whole_window,
772                          x11drv_atom(_KDE_NET_WM_SYSTEM_TRAY_WINDOW_FOR),
773                          XA_WINDOW, 32, PropModeReplace,
774                          (unsigned char*)&data->whole_window, 1 );
775        wine_tsx11_unlock();
776     }
777 }
778
779
780 /***********************************************************************
781  *              set_size_hints
782  *
783  * set the window size hints
784  */
785 static void set_size_hints( Display *display, struct x11drv_win_data *data, DWORD style )
786 {
787     XSizeHints* size_hints;
788
789     if ((size_hints = XAllocSizeHints()))
790     {
791         size_hints->flags = 0;
792
793         if (data->hwnd != GetDesktopWindow())  /* don't force position of desktop */
794         {
795             size_hints->win_gravity = StaticGravity;
796             size_hints->x = data->whole_rect.left;
797             size_hints->y = data->whole_rect.top;
798             size_hints->flags |= PWinGravity | PPosition;
799         }
800
801         if ( !(style & WS_THICKFRAME) )
802         {
803             /* If we restrict window resizing Metacity decides that it should
804              * disable fullscreen support for this window as well.
805              */
806             if (!(data->whole_rect.left <= 0 && data->whole_rect.right >= screen_width &&
807                   data->whole_rect.top <= 0 && data->whole_rect.bottom >= screen_height))
808             {
809                 size_hints->max_width = data->whole_rect.right - data->whole_rect.left;
810                 size_hints->max_height = data->whole_rect.bottom - data->whole_rect.top;
811                 size_hints->min_width = size_hints->max_width;
812                 size_hints->min_height = size_hints->max_height;
813                 size_hints->flags |= PMinSize | PMaxSize;
814             }
815         }
816         XSetWMNormalHints( display, data->whole_window, size_hints );
817         XFree( size_hints );
818     }
819 }
820
821
822 /***********************************************************************
823  *              get_process_name
824  *
825  * get the name of the current process for setting class hints
826  */
827 static char *get_process_name(void)
828 {
829     static char *name;
830
831     if (!name)
832     {
833         WCHAR module[MAX_PATH];
834         DWORD len = GetModuleFileNameW( 0, module, MAX_PATH );
835         if (len && len < MAX_PATH)
836         {
837             char *ptr;
838             WCHAR *p, *appname = module;
839
840             if ((p = strrchrW( appname, '/' ))) appname = p + 1;
841             if ((p = strrchrW( appname, '\\' ))) appname = p + 1;
842             len = WideCharToMultiByte( CP_UNIXCP, 0, appname, -1, NULL, 0, NULL, NULL );
843             if ((ptr = HeapAlloc( GetProcessHeap(), 0, len )))
844             {
845                 WideCharToMultiByte( CP_UNIXCP, 0, appname, -1, ptr, len, NULL, NULL );
846                 name = ptr;
847             }
848         }
849     }
850     return name;
851 }
852
853
854 /***********************************************************************
855  *              set_initial_wm_hints
856  *
857  * Set the window manager hints that don't change over the lifetime of a window.
858  */
859 static void set_initial_wm_hints( Display *display, struct x11drv_win_data *data )
860 {
861     int i;
862     Atom protocols[3];
863     Atom dndVersion = 4;
864     XClassHint *class_hints;
865     char *process_name = get_process_name();
866
867     wine_tsx11_lock();
868
869     /* wm protocols */
870     i = 0;
871     protocols[i++] = x11drv_atom(WM_DELETE_WINDOW);
872     protocols[i++] = x11drv_atom(_NET_WM_PING);
873     if (use_take_focus) protocols[i++] = x11drv_atom(WM_TAKE_FOCUS);
874     XChangeProperty( display, data->whole_window, x11drv_atom(WM_PROTOCOLS),
875                      XA_ATOM, 32, PropModeReplace, (unsigned char *)protocols, i );
876
877     /* class hints */
878     if ((class_hints = XAllocClassHint()))
879     {
880         static char wine[] = "Wine";
881
882         class_hints->res_name = process_name;
883         class_hints->res_class = wine;
884         XSetClassHint( display, data->whole_window, class_hints );
885         XFree( class_hints );
886     }
887
888     /* set the WM_CLIENT_MACHINE and WM_LOCALE_NAME properties */
889     XSetWMProperties(display, data->whole_window, NULL, NULL, NULL, 0, NULL, NULL, NULL);
890     /* set the pid. together, these properties are needed so the window manager can kill us if we freeze */
891     i = getpid();
892     XChangeProperty(display, data->whole_window, x11drv_atom(_NET_WM_PID),
893                     XA_CARDINAL, 32, PropModeReplace, (unsigned char *)&i, 1);
894
895     XChangeProperty( display, data->whole_window, x11drv_atom(XdndAware),
896                      XA_ATOM, 32, PropModeReplace, (unsigned char*)&dndVersion, 1 );
897
898     data->wm_hints = XAllocWMHints();
899     wine_tsx11_unlock();
900
901     if (data->wm_hints)
902     {
903         HICON icon = (HICON)SendMessageW( data->hwnd, WM_GETICON, ICON_BIG, 0 );
904         if (!icon) icon = (HICON)GetClassLongPtrW( data->hwnd, GCLP_HICON );
905         data->wm_hints->flags = 0;
906         set_icon_hints( display, data, icon );
907     }
908 }
909
910
911 /***********************************************************************
912  *              X11DRV_set_wm_hints
913  *
914  * Set the window manager hints for a newly-created window
915  */
916 void X11DRV_set_wm_hints( Display *display, struct x11drv_win_data *data )
917 {
918     Window group_leader;
919     Atom window_type;
920     MwmHints mwm_hints;
921     DWORD style = GetWindowLongW( data->hwnd, GWL_STYLE );
922     DWORD ex_style = GetWindowLongW( data->hwnd, GWL_EXSTYLE );
923     HWND owner = GetWindow( data->hwnd, GW_OWNER );
924
925     if (data->hwnd == GetDesktopWindow())
926     {
927         /* force some styles for the desktop to get the correct decorations */
928         style |= WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX;
929         owner = 0;
930     }
931
932     /* transient for hint */
933     if (owner)
934     {
935         Window owner_win = X11DRV_get_whole_window( owner );
936         wine_tsx11_lock();
937         XSetTransientForHint( display, data->whole_window, owner_win );
938         wine_tsx11_unlock();
939         group_leader = owner_win;
940     }
941     else group_leader = data->whole_window;
942
943     wine_tsx11_lock();
944
945     /* size hints */
946     set_size_hints( display, data, style );
947
948     /* set the WM_WINDOW_TYPE */
949     window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_NORMAL);
950     if (ex_style & WS_EX_TOOLWINDOW) window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_UTILITY);
951     else if (style & WS_THICKFRAME) window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_NORMAL);
952     else if (style & WS_DLGFRAME) window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_DIALOG);
953     else if (ex_style & WS_EX_DLGMODALFRAME) window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_DIALOG);
954
955     XChangeProperty(display, data->whole_window, x11drv_atom(_NET_WM_WINDOW_TYPE),
956                     XA_ATOM, 32, PropModeReplace, (unsigned char*)&window_type, 1);
957
958     mwm_hints.flags = MWM_HINTS_FUNCTIONS | MWM_HINTS_DECORATIONS;
959     mwm_hints.decorations = get_mwm_decorations( style, ex_style );
960     mwm_hints.functions = MWM_FUNC_MOVE;
961     if (style & WS_THICKFRAME)  mwm_hints.functions |= MWM_FUNC_RESIZE;
962     if (style & WS_MINIMIZEBOX) mwm_hints.functions |= MWM_FUNC_MINIMIZE;
963     if (style & WS_MAXIMIZEBOX) mwm_hints.functions |= MWM_FUNC_MAXIMIZE;
964     if (style & WS_SYSMENU)     mwm_hints.functions |= MWM_FUNC_CLOSE;
965
966     XChangeProperty( display, data->whole_window, x11drv_atom(_MOTIF_WM_HINTS),
967                      x11drv_atom(_MOTIF_WM_HINTS), 32, PropModeReplace,
968                      (unsigned char*)&mwm_hints, sizeof(mwm_hints)/sizeof(long) );
969
970     /* wm hints */
971     if (data->wm_hints)
972     {
973         data->wm_hints->flags |= InputHint | StateHint | WindowGroupHint;
974         data->wm_hints->input = !(style & WS_DISABLED);
975         data->wm_hints->initial_state = (style & WS_MINIMIZE) ? IconicState : NormalState;
976         data->wm_hints->window_group = group_leader;
977         XSetWMHints( display, data->whole_window, data->wm_hints );
978     }
979
980     wine_tsx11_unlock();
981 }
982
983
984 /***********************************************************************
985  *              X11DRV_window_to_X_rect
986  *
987  * Convert a rect from client to X window coordinates
988  */
989 void X11DRV_window_to_X_rect( struct x11drv_win_data *data, RECT *rect )
990 {
991     RECT rc;
992
993     if (!data->managed) return;
994     if (IsRectEmpty( rect )) return;
995
996     get_x11_rect_offset( data, &rc );
997
998     rect->left   -= rc.left;
999     rect->right  -= rc.right;
1000     rect->top    -= rc.top;
1001     rect->bottom -= rc.bottom;
1002     if (rect->top >= rect->bottom) rect->bottom = rect->top + 1;
1003     if (rect->left >= rect->right) rect->right = rect->left + 1;
1004 }
1005
1006
1007 /***********************************************************************
1008  *              X11DRV_X_to_window_rect
1009  *
1010  * Opposite of X11DRV_window_to_X_rect
1011  */
1012 void X11DRV_X_to_window_rect( struct x11drv_win_data *data, RECT *rect )
1013 {
1014     RECT rc;
1015
1016     if (!data->managed) return;
1017     if (IsRectEmpty( rect )) return;
1018
1019     get_x11_rect_offset( data, &rc );
1020
1021     rect->left   += rc.left;
1022     rect->right  += rc.right;
1023     rect->top    += rc.top;
1024     rect->bottom += rc.bottom;
1025     if (rect->top >= rect->bottom) rect->bottom = rect->top + 1;
1026     if (rect->left >= rect->right) rect->right = rect->left + 1;
1027 }
1028
1029
1030 /***********************************************************************
1031  *              X11DRV_sync_window_position
1032  *
1033  * Synchronize the X window position with the Windows one
1034  */
1035 void X11DRV_sync_window_position( Display *display, struct x11drv_win_data *data,
1036                                   UINT swp_flags, const RECT *old_client_rect,
1037                                   const RECT *old_whole_rect )
1038 {
1039     XWindowChanges changes;
1040     int mask = get_window_changes( &changes, old_whole_rect, &data->whole_rect );
1041
1042     if (!(swp_flags & SWP_NOZORDER))
1043     {
1044         /* find window that this one must be after */
1045         HWND prev = GetWindow( data->hwnd, GW_HWNDPREV );
1046         while (prev && !(GetWindowLongW( prev, GWL_STYLE ) & WS_VISIBLE))
1047             prev = GetWindow( prev, GW_HWNDPREV );
1048         if (!prev)  /* top child */
1049         {
1050             changes.stack_mode = Above;
1051             mask |= CWStackMode;
1052         }
1053         /* should use stack_mode Below but most window managers don't get it right */
1054         /* and Above with a sibling doesn't work so well either, so we ignore it */
1055     }
1056
1057     /* only the size is allowed to change for the desktop window */
1058     if (data->whole_window == root_window) mask &= CWWidth | CWHeight;
1059
1060     if (mask)
1061     {
1062         DWORD style = GetWindowLongW( data->hwnd, GWL_STYLE );
1063
1064         TRACE( "setting win %p/%lx pos %d,%d,%dx%d after %lx changes=%x\n",
1065                data->hwnd, data->whole_window, data->whole_rect.left, data->whole_rect.top,
1066                data->whole_rect.right - data->whole_rect.left,
1067                data->whole_rect.bottom - data->whole_rect.top, changes.sibling, mask );
1068
1069         wine_tsx11_lock();
1070         if (mask & (CWWidth|CWHeight)) set_size_hints( display, data, style );
1071         if (mask & CWX) changes.x -= virtual_screen_rect.left;
1072         if (mask & CWY) changes.y -= virtual_screen_rect.top;
1073         XReconfigureWMWindow( display, data->whole_window,
1074                               DefaultScreen(display), mask, &changes );
1075         wine_tsx11_unlock();
1076     }
1077 }
1078
1079
1080 /***********************************************************************
1081  *              X11DRV_sync_client_position
1082  *
1083  * Synchronize the X client window position with the Windows one
1084  */
1085 void X11DRV_sync_client_position( Display *display, struct x11drv_win_data *data,
1086                                   UINT swp_flags, const RECT *old_client_rect,
1087                                   const RECT *old_whole_rect )
1088 {
1089     int mask;
1090     XWindowChanges changes;
1091     RECT old = *old_client_rect;
1092     RECT new = data->client_rect;
1093
1094     OffsetRect( &old, -old_whole_rect->left, -old_whole_rect->top );
1095     OffsetRect( &new, -data->whole_rect.left, -data->whole_rect.top );
1096     if (!(mask = get_window_changes( &changes, &old, &new ))) return;
1097
1098     if (data->client_window)
1099     {
1100         TRACE( "setting client win %lx pos %d,%d,%dx%d changes=%x\n",
1101                data->client_window, new.left, new.top,
1102                new.right - new.left, new.bottom - new.top, mask );
1103         wine_tsx11_lock();
1104         XConfigureWindow( display, data->client_window, mask, &changes );
1105         wine_tsx11_unlock();
1106     }
1107
1108     if (data->gl_drawable && (mask & (CWWidth|CWHeight))) sync_gl_drawable( display, data );
1109
1110     /* make sure the changes get to the server before we start painting */
1111     if (data->client_window || data->gl_drawable)
1112     {
1113         wine_tsx11_lock();
1114         XFlush(display);
1115         wine_tsx11_unlock();
1116     }
1117 }
1118
1119
1120 /**********************************************************************
1121  *              create_whole_window
1122  *
1123  * Create the whole X window for a given window
1124  */
1125 static Window create_whole_window( Display *display, struct x11drv_win_data *data )
1126 {
1127     int cx, cy, mask;
1128     XSetWindowAttributes attr;
1129     XIM xim;
1130     WCHAR text[1024];
1131     HRGN hrgn;
1132
1133     if (!(cx = data->window_rect.right - data->window_rect.left)) cx = 1;
1134     if (!(cy = data->window_rect.bottom - data->window_rect.top)) cy = 1;
1135
1136     if (!data->managed && is_window_managed( data->hwnd, SWP_NOACTIVATE, &data->window_rect ))
1137     {
1138         TRACE( "making win %p/%lx managed\n", data->hwnd, data->whole_window );
1139         data->managed = TRUE;
1140         SetPropA( data->hwnd, managed_prop, (HANDLE)1 );
1141     }
1142
1143     mask = get_window_attributes( display, data, &attr );
1144
1145     wine_tsx11_lock();
1146
1147     data->whole_rect = data->window_rect;
1148     data->whole_window = XCreateWindow( display, root_window,
1149                                         data->window_rect.left - virtual_screen_rect.left,
1150                                         data->window_rect.top - virtual_screen_rect.top,
1151                                         cx, cy, 0, screen_depth, InputOutput,
1152                                         visual, mask, &attr );
1153
1154     if (data->whole_window) XSaveContext( display, data->whole_window, winContext, (char *)data->hwnd );
1155     wine_tsx11_unlock();
1156
1157     if (!data->whole_window) return 0;
1158
1159     if (!create_client_window( display, data, NULL ))
1160     {
1161         wine_tsx11_lock();
1162         XDeleteContext( display, data->whole_window, winContext );
1163         XDestroyWindow( display, data->whole_window );
1164         data->whole_window = 0;
1165         wine_tsx11_unlock();
1166         return 0;
1167     }
1168
1169     xim = x11drv_thread_data()->xim;
1170     if (xim) data->xic = X11DRV_CreateIC( xim, display, data->whole_window );
1171
1172     set_initial_wm_hints( display, data );
1173     X11DRV_set_wm_hints( display, data );
1174
1175     SetPropA( data->hwnd, whole_window_prop, (HANDLE)data->whole_window );
1176
1177     /* set the window text */
1178     if (!InternalGetWindowText( data->hwnd, text, sizeof(text)/sizeof(WCHAR) )) text[0] = 0;
1179     sync_window_text( display, data->whole_window, text );
1180
1181     /* set the window region */
1182     if ((hrgn = CreateRectRgn( 0, 0, 0, 0 )))
1183     {
1184         if (GetWindowRgn( data->hwnd, hrgn ) != ERROR) sync_window_region( display, data, hrgn );
1185         DeleteObject( hrgn );
1186     }
1187     return data->whole_window;
1188 }
1189
1190
1191 /**********************************************************************
1192  *              destroy_whole_window
1193  *
1194  * Destroy the whole X window for a given window.
1195  */
1196 static void destroy_whole_window( Display *display, struct x11drv_win_data *data, BOOL already_destroyed )
1197 {
1198     struct x11drv_thread_data *thread_data = x11drv_thread_data();
1199
1200     if (!data->whole_window) return;
1201
1202     TRACE( "win %p xwin %lx/%lx\n", data->hwnd, data->whole_window, data->client_window );
1203     if (thread_data->cursor_window == data->whole_window ||
1204         thread_data->cursor_window == data->client_window)
1205         thread_data->cursor_window = None;
1206     wine_tsx11_lock();
1207     XDeleteContext( display, data->whole_window, winContext );
1208     XDeleteContext( display, data->client_window, winContext );
1209     if (!already_destroyed) XDestroyWindow( display, data->whole_window );
1210     data->whole_window = data->client_window = 0;
1211     data->wm_state = WithdrawnState;
1212     data->net_wm_state = 0;
1213     data->mapped = FALSE;
1214     if (data->xic)
1215     {
1216         XUnsetICFocus( data->xic );
1217         XDestroyIC( data->xic );
1218         data->xic = 0;
1219     }
1220     /* Outlook stops processing messages after destroying a dialog, so we need an explicit flush */
1221     XFlush( display );
1222     XFree( data->wm_hints );
1223     data->wm_hints = NULL;
1224     wine_tsx11_unlock();
1225     RemovePropA( data->hwnd, whole_window_prop );
1226     RemovePropA( data->hwnd, client_window_prop );
1227 }
1228
1229
1230 /*****************************************************************
1231  *              SetWindowText   (X11DRV.@)
1232  */
1233 void X11DRV_SetWindowText( HWND hwnd, LPCWSTR text )
1234 {
1235     Display *display = thread_display();
1236     Window win;
1237
1238     if ((win = X11DRV_get_whole_window( hwnd )) && win != DefaultRootWindow(display))
1239         sync_window_text( display, win, text );
1240 }
1241
1242
1243 /***********************************************************************
1244  *              DestroyWindow   (X11DRV.@)
1245  */
1246 void X11DRV_DestroyWindow( HWND hwnd )
1247 {
1248     struct x11drv_thread_data *thread_data = x11drv_thread_data();
1249     Display *display = thread_data->display;
1250     struct x11drv_win_data *data;
1251
1252     if (!(data = X11DRV_get_win_data( hwnd ))) return;
1253
1254     if (data->pixmap)
1255     {
1256         destroy_glxpixmap(display, data->gl_drawable);
1257         wine_tsx11_lock();
1258         XFreePixmap(display, data->pixmap);
1259         wine_tsx11_unlock();
1260     }
1261     else if (data->gl_drawable)
1262     {
1263         wine_tsx11_lock();
1264         XDestroyWindow(display, data->gl_drawable);
1265         wine_tsx11_unlock();
1266     }
1267
1268     destroy_whole_window( display, data, FALSE );
1269     destroy_icon_window( display, data );
1270
1271     if (data->colormap)
1272     {
1273         wine_tsx11_lock();
1274         XFreeColormap( display, data->colormap );
1275         wine_tsx11_unlock();
1276     }
1277
1278     if (thread_data->last_focus == hwnd) thread_data->last_focus = 0;
1279     if (data->hWMIconBitmap) DeleteObject( data->hWMIconBitmap );
1280     if (data->hWMIconMask) DeleteObject( data->hWMIconMask);
1281     wine_tsx11_lock();
1282     XDeleteContext( display, (XID)hwnd, win_data_context );
1283     wine_tsx11_unlock();
1284     HeapFree( GetProcessHeap(), 0, data );
1285 }
1286
1287
1288 /***********************************************************************
1289  *              X11DRV_DestroyNotify
1290  */
1291 void X11DRV_DestroyNotify( HWND hwnd, XEvent *event )
1292 {
1293     Display *display = event->xdestroywindow.display;
1294     struct x11drv_win_data *data;
1295
1296     if (!(data = X11DRV_get_win_data( hwnd ))) return;
1297
1298     FIXME( "window %p/%lx destroyed from the outside\n", hwnd, data->whole_window );
1299     destroy_whole_window( display, data, TRUE );
1300 }
1301
1302
1303 static struct x11drv_win_data *alloc_win_data( Display *display, HWND hwnd )
1304 {
1305     struct x11drv_win_data *data;
1306
1307     if ((data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*data))))
1308     {
1309         data->hwnd = hwnd;
1310         wine_tsx11_lock();
1311         if (!winContext) winContext = XUniqueContext();
1312         if (!win_data_context) win_data_context = XUniqueContext();
1313         XSaveContext( display, (XID)hwnd, win_data_context, (char *)data );
1314         wine_tsx11_unlock();
1315     }
1316     return data;
1317 }
1318
1319
1320 /* initialize the desktop window id in the desktop manager process */
1321 static struct x11drv_win_data *create_desktop_win_data( Display *display, HWND hwnd )
1322 {
1323     struct x11drv_win_data *data;
1324     VisualID visualid;
1325
1326     if (!(data = alloc_win_data( display, hwnd ))) return NULL;
1327     wine_tsx11_lock();
1328     visualid = XVisualIDFromVisual(visual);
1329     wine_tsx11_unlock();
1330     data->whole_window = data->client_window = root_window;
1331     data->managed = TRUE;
1332     SetPropA( data->hwnd, managed_prop, (HANDLE)1 );
1333     SetPropA( data->hwnd, whole_window_prop, (HANDLE)root_window );
1334     SetPropA( data->hwnd, client_window_prop, (HANDLE)root_window );
1335     set_initial_wm_hints( display, data );
1336     return data;
1337 }
1338
1339 /**********************************************************************
1340  *              CreateDesktopWindow   (X11DRV.@)
1341  */
1342 BOOL X11DRV_CreateDesktopWindow( HWND hwnd )
1343 {
1344     unsigned int width, height;
1345
1346     /* retrieve the real size of the desktop */
1347     SERVER_START_REQ( get_window_rectangles )
1348     {
1349         req->handle = hwnd;
1350         wine_server_call( req );
1351         width  = reply->window.right - reply->window.left;
1352         height = reply->window.bottom - reply->window.top;
1353     }
1354     SERVER_END_REQ;
1355
1356     if (!width && !height)  /* not initialized yet */
1357     {
1358         SERVER_START_REQ( set_window_pos )
1359         {
1360             req->handle        = hwnd;
1361             req->previous      = 0;
1362             req->flags         = SWP_NOZORDER;
1363             req->window.left   = virtual_screen_rect.left;
1364             req->window.top    = virtual_screen_rect.top;
1365             req->window.right  = virtual_screen_rect.right;
1366             req->window.bottom = virtual_screen_rect.bottom;
1367             req->client        = req->window;
1368             wine_server_call( req );
1369         }
1370         SERVER_END_REQ;
1371     }
1372     else
1373     {
1374         Window win = (Window)GetPropA( hwnd, whole_window_prop );
1375         if (win && win != root_window) X11DRV_init_desktop( win, width, height );
1376     }
1377     return TRUE;
1378 }
1379
1380
1381 /**********************************************************************
1382  *              CreateWindow   (X11DRV.@)
1383  */
1384 BOOL X11DRV_CreateWindow( HWND hwnd )
1385 {
1386     Display *display = thread_display();
1387
1388     if (hwnd == GetDesktopWindow() && root_window != DefaultRootWindow( display ))
1389     {
1390         /* the desktop win data can't be created lazily */
1391         if (!create_desktop_win_data( display, hwnd )) return FALSE;
1392     }
1393     return TRUE;
1394 }
1395
1396
1397 /***********************************************************************
1398  *              X11DRV_get_win_data
1399  *
1400  * Return the X11 data structure associated with a window.
1401  */
1402 struct x11drv_win_data *X11DRV_get_win_data( HWND hwnd )
1403 {
1404     char *data;
1405
1406     if (!hwnd || XFindContext( thread_display(), (XID)hwnd, win_data_context, &data )) data = NULL;
1407     return (struct x11drv_win_data *)data;
1408 }
1409
1410
1411 /***********************************************************************
1412  *              X11DRV_create_win_data
1413  *
1414  * Create an X11 data window structure for an existing window.
1415  */
1416 struct x11drv_win_data *X11DRV_create_win_data( HWND hwnd )
1417 {
1418     Display *display = thread_display();
1419     struct x11drv_win_data *data;
1420     HWND parent;
1421
1422     if (!(parent = GetAncestor( hwnd, GA_PARENT ))) return NULL;  /* desktop */
1423     if (!(data = alloc_win_data( display, hwnd ))) return NULL;
1424
1425     GetWindowRect( hwnd, &data->window_rect );
1426     MapWindowPoints( 0, parent, (POINT *)&data->window_rect, 2 );
1427     data->whole_rect = data->window_rect;
1428     GetClientRect( hwnd, &data->client_rect );
1429     MapWindowPoints( hwnd, parent, (POINT *)&data->client_rect, 2 );
1430
1431     if (parent == GetDesktopWindow())
1432     {
1433         if (!create_whole_window( display, data ))
1434         {
1435             HeapFree( GetProcessHeap(), 0, data );
1436             return NULL;
1437         }
1438         TRACE( "win %p/%lx/%lx window %s whole %s client %s\n",
1439                hwnd, data->whole_window, data->client_window, wine_dbgstr_rect( &data->window_rect ),
1440                wine_dbgstr_rect( &data->whole_rect ), wine_dbgstr_rect( &data->client_rect ));
1441     }
1442     return data;
1443 }
1444
1445
1446 /***********************************************************************
1447  *              X11DRV_get_whole_window
1448  *
1449  * Return the X window associated with the full area of a window
1450  */
1451 Window X11DRV_get_whole_window( HWND hwnd )
1452 {
1453     struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
1454
1455     if (!data)
1456     {
1457         if (hwnd == GetDesktopWindow()) return root_window;
1458         return (Window)GetPropA( hwnd, whole_window_prop );
1459     }
1460     return data->whole_window;
1461 }
1462
1463
1464 /***********************************************************************
1465  *              X11DRV_get_client_window
1466  *
1467  * Return the X window associated with the client area of a window
1468  */
1469 Window X11DRV_get_client_window( HWND hwnd )
1470 {
1471     struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
1472
1473     if (!data)
1474     {
1475         if (hwnd == GetDesktopWindow()) return root_window;
1476         return (Window)GetPropA( hwnd, client_window_prop );
1477     }
1478     return data->client_window;
1479 }
1480
1481
1482 /***********************************************************************
1483  *              X11DRV_get_ic
1484  *
1485  * Return the X input context associated with a window
1486  */
1487 XIC X11DRV_get_ic( HWND hwnd )
1488 {
1489     struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
1490
1491     if (!data) return 0;
1492     return data->xic;
1493 }
1494
1495
1496 /***********************************************************************
1497  *              X11DRV_GetDC   (X11DRV.@)
1498  */
1499 void X11DRV_GetDC( HDC hdc, HWND hwnd, HWND top, const RECT *win_rect,
1500                    const RECT *top_rect, DWORD flags )
1501 {
1502     struct x11drv_escape_set_drawable escape;
1503     struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
1504
1505     escape.code        = X11DRV_SET_DRAWABLE;
1506     escape.mode        = IncludeInferiors;
1507     escape.fbconfig_id = 0;
1508     escape.gl_drawable = 0;
1509     escape.pixmap      = 0;
1510
1511     if (top == hwnd && data && IsIconic( hwnd ) && data->icon_window)
1512     {
1513         escape.drawable = data->icon_window;
1514     }
1515     else if (top == hwnd && (flags & DCX_WINDOW))
1516     {
1517         escape.drawable = data ? data->whole_window : X11DRV_get_whole_window( hwnd );
1518     }
1519     else
1520     {
1521         escape.drawable    = X11DRV_get_client_window( top );
1522         escape.fbconfig_id = data ? data->fbconfig_id : (XID)GetPropA( hwnd, fbconfig_id_prop );
1523         escape.gl_drawable = data ? data->gl_drawable : (Drawable)GetPropA( hwnd, gl_drawable_prop );
1524         escape.pixmap      = data ? data->pixmap : (Pixmap)GetPropA( hwnd, pixmap_prop );
1525         if (flags & DCX_CLIPCHILDREN) escape.mode = ClipByChildren;
1526     }
1527
1528     escape.dc_rect.left         = win_rect->left - top_rect->left;
1529     escape.dc_rect.top          = win_rect->top - top_rect->top;
1530     escape.dc_rect.right        = win_rect->right - top_rect->left;
1531     escape.dc_rect.bottom       = win_rect->bottom - top_rect->top;
1532     escape.drawable_rect.left   = top_rect->left;
1533     escape.drawable_rect.top    = top_rect->top;
1534     escape.drawable_rect.right  = top_rect->right;
1535     escape.drawable_rect.bottom = top_rect->bottom;
1536
1537     ExtEscape( hdc, X11DRV_ESCAPE, sizeof(escape), (LPSTR)&escape, 0, NULL );
1538 }
1539
1540
1541 /***********************************************************************
1542  *              X11DRV_ReleaseDC  (X11DRV.@)
1543  */
1544 void X11DRV_ReleaseDC( HWND hwnd, HDC hdc )
1545 {
1546     struct x11drv_escape_set_drawable escape;
1547
1548     escape.code = X11DRV_SET_DRAWABLE;
1549     escape.drawable = root_window;
1550     escape.mode = IncludeInferiors;
1551     escape.drawable_rect = virtual_screen_rect;
1552     SetRect( &escape.dc_rect, 0, 0, virtual_screen_rect.right - virtual_screen_rect.left,
1553              virtual_screen_rect.bottom - virtual_screen_rect.top );
1554     escape.fbconfig_id = 0;
1555     escape.gl_drawable = 0;
1556     escape.pixmap = 0;
1557     ExtEscape( hdc, X11DRV_ESCAPE, sizeof(escape), (LPSTR)&escape, 0, NULL );
1558 }
1559
1560
1561 /***********************************************************************
1562  *              SetCapture  (X11DRV.@)
1563  */
1564 void X11DRV_SetCapture( HWND hwnd, UINT flags )
1565 {
1566     struct x11drv_thread_data *thread_data = x11drv_thread_data();
1567
1568     if (!(flags & (GUI_INMOVESIZE | GUI_INMENUMODE))) return;
1569
1570     if (hwnd)
1571     {
1572         Window grab_win = X11DRV_get_client_window( GetAncestor( hwnd, GA_ROOT ) );
1573
1574         if (!grab_win) return;
1575         wine_tsx11_lock();
1576         XFlush( gdi_display );
1577         XGrabPointer( thread_data->display, grab_win, False,
1578                       PointerMotionMask | ButtonPressMask | ButtonReleaseMask,
1579                       GrabModeAsync, GrabModeAsync, None, None, CurrentTime );
1580         wine_tsx11_unlock();
1581         thread_data->grab_window = grab_win;
1582     }
1583     else  /* release capture */
1584     {
1585         wine_tsx11_lock();
1586         XFlush( gdi_display );
1587         XUngrabPointer( thread_data->display, CurrentTime );
1588         wine_tsx11_unlock();
1589         thread_data->grab_window = None;
1590     }
1591 }
1592
1593
1594 /*****************************************************************
1595  *              SetParent   (X11DRV.@)
1596  */
1597 void X11DRV_SetParent( HWND hwnd, HWND parent, HWND old_parent )
1598 {
1599     Display *display = thread_display();
1600     struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
1601
1602     if (!data) return;
1603     if (parent == old_parent) return;
1604
1605     if (parent != GetDesktopWindow()) /* a child window */
1606     {
1607         if (old_parent == GetDesktopWindow())
1608         {
1609             /* destroy the old X windows */
1610             destroy_whole_window( display, data, FALSE );
1611             destroy_icon_window( display, data );
1612             if (data->managed)
1613             {
1614                 data->managed = FALSE;
1615                 RemovePropA( data->hwnd, managed_prop );
1616             }
1617         }
1618     }
1619     else  /* new top level window */
1620     {
1621         /* FIXME: we ignore errors since we can't really recover anyway */
1622         create_whole_window( display, data );
1623     }
1624 }
1625
1626
1627 /*****************************************************************
1628  *              SetFocus   (X11DRV.@)
1629  *
1630  * Set the X focus.
1631  * Explicit colormap management seems to work only with OLVWM.
1632  */
1633 void X11DRV_SetFocus( HWND hwnd )
1634 {
1635     Display *display = thread_display();
1636     struct x11drv_win_data *data;
1637     XWindowChanges changes;
1638
1639     /* If setting the focus to 0, uninstall the colormap */
1640     if (!hwnd && root_window == DefaultRootWindow(display))
1641     {
1642         wine_tsx11_lock();
1643         if (X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_PRIVATE)
1644             XUninstallColormap( display, X11DRV_PALETTE_PaletteXColormap );
1645         wine_tsx11_unlock();
1646         return;
1647     }
1648
1649     hwnd = GetAncestor( hwnd, GA_ROOT );
1650
1651     if (!(data = X11DRV_get_win_data( hwnd ))) return;
1652     if (data->managed || !data->whole_window) return;
1653
1654     /* Set X focus and install colormap */
1655     wine_tsx11_lock();
1656     changes.stack_mode = Above;
1657     XConfigureWindow( display, data->whole_window, CWStackMode, &changes );
1658     if (root_window == DefaultRootWindow(display))
1659     {
1660         /* we must not use CurrentTime (ICCCM), so try to use last message time instead */
1661         /* FIXME: this is not entirely correct */
1662         XSetInputFocus( display, data->whole_window, RevertToParent,
1663                         /* CurrentTime */
1664                         GetMessageTime() - EVENT_x11_time_to_win32_time(0));
1665         if (X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_PRIVATE)
1666             XInstallColormap( display, X11DRV_PALETTE_PaletteXColormap );
1667     }
1668     wine_tsx11_unlock();
1669 }
1670
1671
1672 /**********************************************************************
1673  *              SetWindowIcon (X11DRV.@)
1674  *
1675  * hIcon or hIconSm has changed (or is being initialised for the
1676  * first time). Complete the X11 driver-specific initialisation
1677  * and set the window hints.
1678  *
1679  * This is not entirely correct, may need to create
1680  * an icon window and set the pixmap as a background
1681  */
1682 void X11DRV_SetWindowIcon( HWND hwnd, UINT type, HICON icon )
1683 {
1684     Display *display = thread_display();
1685     struct x11drv_win_data *data;
1686
1687     if (type != ICON_BIG) return;  /* nothing to do here */
1688
1689     if (!(data = X11DRV_get_win_data( hwnd ))) return;
1690     if (!data->whole_window) return;
1691     if (!data->managed) return;
1692
1693     if (data->wm_hints)
1694     {
1695         set_icon_hints( display, data, icon );
1696         wine_tsx11_lock();
1697         XSetWMHints( display, data->whole_window, data->wm_hints );
1698         wine_tsx11_unlock();
1699     }
1700 }
1701
1702
1703 /***********************************************************************
1704  *              SetWindowRgn  (X11DRV.@)
1705  *
1706  * Assign specified region to window (for non-rectangular windows)
1707  */
1708 int X11DRV_SetWindowRgn( HWND hwnd, HRGN hrgn, BOOL redraw )
1709 {
1710     struct x11drv_win_data *data;
1711
1712     if ((data = X11DRV_get_win_data( hwnd )))
1713     {
1714         sync_window_region( thread_display(), data, hrgn );
1715     }
1716     else if (GetWindowThreadProcessId( hwnd, NULL ) != GetCurrentThreadId())
1717     {
1718         FIXME( "not supported on other thread window %p\n", hwnd );
1719         SetLastError( ERROR_INVALID_WINDOW_HANDLE );
1720         return FALSE;
1721     }
1722
1723     return TRUE;
1724 }