quartz: Silence requests for ipin on filters.
[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         else
1052         {
1053             /* should use stack_mode Below but most window managers don't get it right */
1054             /* so move it above the next one in Z order */
1055             HWND next = GetWindow( data->hwnd, GW_HWNDNEXT );
1056             while (next && !(GetWindowLongW( next, GWL_STYLE ) & WS_VISIBLE))
1057                 next = GetWindow( next, GW_HWNDNEXT );
1058             if (next)
1059             {
1060                 changes.stack_mode = Above;
1061                 changes.sibling = X11DRV_get_whole_window(next);
1062                 mask |= CWStackMode | CWSibling;
1063             }
1064         }
1065     }
1066
1067     /* only the size is allowed to change for the desktop window */
1068     if (data->whole_window == root_window) mask &= CWWidth | CWHeight;
1069
1070     if (mask)
1071     {
1072         DWORD style = GetWindowLongW( data->hwnd, GWL_STYLE );
1073
1074         TRACE( "setting win %lx pos %d,%d,%dx%d after %lx changes=%x\n",
1075                data->whole_window, data->whole_rect.left, data->whole_rect.top,
1076                data->whole_rect.right - data->whole_rect.left,
1077                data->whole_rect.bottom - data->whole_rect.top, changes.sibling, mask );
1078
1079         wine_tsx11_lock();
1080         if (mask & (CWWidth|CWHeight)) set_size_hints( display, data, style );
1081         if (mask & CWX) changes.x -= virtual_screen_rect.left;
1082         if (mask & CWY) changes.y -= virtual_screen_rect.top;
1083         XReconfigureWMWindow( display, data->whole_window,
1084                               DefaultScreen(display), mask, &changes );
1085         wine_tsx11_unlock();
1086     }
1087 }
1088
1089
1090 /***********************************************************************
1091  *              X11DRV_sync_client_position
1092  *
1093  * Synchronize the X client window position with the Windows one
1094  */
1095 void X11DRV_sync_client_position( Display *display, struct x11drv_win_data *data,
1096                                   UINT swp_flags, const RECT *old_client_rect,
1097                                   const RECT *old_whole_rect )
1098 {
1099     int mask;
1100     XWindowChanges changes;
1101     RECT old = *old_client_rect;
1102     RECT new = data->client_rect;
1103
1104     OffsetRect( &old, -old_whole_rect->left, -old_whole_rect->top );
1105     OffsetRect( &new, -data->whole_rect.left, -data->whole_rect.top );
1106     if (!(mask = get_window_changes( &changes, &old, &new ))) return;
1107
1108     if (data->client_window)
1109     {
1110         TRACE( "setting client win %lx pos %d,%d,%dx%d changes=%x\n",
1111                data->client_window, new.left, new.top,
1112                new.right - new.left, new.bottom - new.top, mask );
1113         wine_tsx11_lock();
1114         XConfigureWindow( display, data->client_window, mask, &changes );
1115         wine_tsx11_unlock();
1116     }
1117
1118     if (data->gl_drawable && (mask & (CWWidth|CWHeight))) sync_gl_drawable( display, data );
1119
1120     /* make sure the changes get to the server before we start painting */
1121     if (data->client_window || data->gl_drawable)
1122     {
1123         wine_tsx11_lock();
1124         XFlush(display);
1125         wine_tsx11_unlock();
1126     }
1127 }
1128
1129
1130 /**********************************************************************
1131  *              create_whole_window
1132  *
1133  * Create the whole X window for a given window
1134  */
1135 static Window create_whole_window( Display *display, struct x11drv_win_data *data )
1136 {
1137     int cx, cy, mask;
1138     XSetWindowAttributes attr;
1139     XIM xim;
1140     WCHAR text[1024];
1141     HRGN hrgn;
1142
1143     if (!(cx = data->window_rect.right - data->window_rect.left)) cx = 1;
1144     if (!(cy = data->window_rect.bottom - data->window_rect.top)) cy = 1;
1145
1146     if (!data->managed && is_window_managed( data->hwnd, SWP_NOACTIVATE, &data->window_rect ))
1147     {
1148         TRACE( "making win %p/%lx managed\n", data->hwnd, data->whole_window );
1149         data->managed = TRUE;
1150         SetPropA( data->hwnd, managed_prop, (HANDLE)1 );
1151     }
1152
1153     mask = get_window_attributes( display, data, &attr );
1154
1155     wine_tsx11_lock();
1156
1157     data->whole_rect = data->window_rect;
1158     data->whole_window = XCreateWindow( display, root_window,
1159                                         data->window_rect.left - virtual_screen_rect.left,
1160                                         data->window_rect.top - virtual_screen_rect.top,
1161                                         cx, cy, 0, screen_depth, InputOutput,
1162                                         visual, mask, &attr );
1163
1164     if (data->whole_window) XSaveContext( display, data->whole_window, winContext, (char *)data->hwnd );
1165     wine_tsx11_unlock();
1166
1167     if (!data->whole_window) return 0;
1168
1169     if (!create_client_window( display, data, NULL ))
1170     {
1171         wine_tsx11_lock();
1172         XDeleteContext( display, data->whole_window, winContext );
1173         XDestroyWindow( display, data->whole_window );
1174         data->whole_window = 0;
1175         wine_tsx11_unlock();
1176         return 0;
1177     }
1178
1179     xim = x11drv_thread_data()->xim;
1180     if (xim) data->xic = X11DRV_CreateIC( xim, display, data->whole_window );
1181
1182     set_initial_wm_hints( display, data );
1183     X11DRV_set_wm_hints( display, data );
1184
1185     SetPropA( data->hwnd, whole_window_prop, (HANDLE)data->whole_window );
1186
1187     /* set the window text */
1188     if (!InternalGetWindowText( data->hwnd, text, sizeof(text)/sizeof(WCHAR) )) text[0] = 0;
1189     sync_window_text( display, data->whole_window, text );
1190
1191     /* set the window region */
1192     if ((hrgn = CreateRectRgn( 0, 0, 0, 0 )))
1193     {
1194         if (GetWindowRgn( data->hwnd, hrgn ) != ERROR) sync_window_region( display, data, hrgn );
1195         DeleteObject( hrgn );
1196     }
1197     return data->whole_window;
1198 }
1199
1200
1201 /**********************************************************************
1202  *              destroy_whole_window
1203  *
1204  * Destroy the whole X window for a given window.
1205  */
1206 static void destroy_whole_window( Display *display, struct x11drv_win_data *data, BOOL already_destroyed )
1207 {
1208     struct x11drv_thread_data *thread_data = x11drv_thread_data();
1209
1210     if (!data->whole_window) return;
1211
1212     TRACE( "win %p xwin %lx/%lx\n", data->hwnd, data->whole_window, data->client_window );
1213     if (thread_data->cursor_window == data->whole_window ||
1214         thread_data->cursor_window == data->client_window)
1215         thread_data->cursor_window = None;
1216     wine_tsx11_lock();
1217     XDeleteContext( display, data->whole_window, winContext );
1218     XDeleteContext( display, data->client_window, winContext );
1219     if (!already_destroyed) XDestroyWindow( display, data->whole_window );
1220     data->whole_window = data->client_window = 0;
1221     data->wm_state = WithdrawnState;
1222     data->net_wm_state = 0;
1223     data->mapped = FALSE;
1224     if (data->xic)
1225     {
1226         XUnsetICFocus( data->xic );
1227         XDestroyIC( data->xic );
1228         data->xic = 0;
1229     }
1230     /* Outlook stops processing messages after destroying a dialog, so we need an explicit flush */
1231     XFlush( display );
1232     XFree( data->wm_hints );
1233     data->wm_hints = NULL;
1234     wine_tsx11_unlock();
1235     RemovePropA( data->hwnd, whole_window_prop );
1236     RemovePropA( data->hwnd, client_window_prop );
1237 }
1238
1239
1240 /*****************************************************************
1241  *              SetWindowText   (X11DRV.@)
1242  */
1243 void X11DRV_SetWindowText( HWND hwnd, LPCWSTR text )
1244 {
1245     Display *display = thread_display();
1246     Window win;
1247
1248     if ((win = X11DRV_get_whole_window( hwnd )) && win != DefaultRootWindow(display))
1249         sync_window_text( display, win, text );
1250 }
1251
1252
1253 /***********************************************************************
1254  *              DestroyWindow   (X11DRV.@)
1255  */
1256 void X11DRV_DestroyWindow( HWND hwnd )
1257 {
1258     struct x11drv_thread_data *thread_data = x11drv_thread_data();
1259     Display *display = thread_data->display;
1260     struct x11drv_win_data *data;
1261
1262     if (!(data = X11DRV_get_win_data( hwnd ))) return;
1263
1264     if (data->pixmap)
1265     {
1266         destroy_glxpixmap(display, data->gl_drawable);
1267         wine_tsx11_lock();
1268         XFreePixmap(display, data->pixmap);
1269         wine_tsx11_unlock();
1270     }
1271     else if (data->gl_drawable)
1272     {
1273         wine_tsx11_lock();
1274         XDestroyWindow(display, data->gl_drawable);
1275         wine_tsx11_unlock();
1276     }
1277
1278     destroy_whole_window( display, data, FALSE );
1279     destroy_icon_window( display, data );
1280
1281     if (data->colormap)
1282     {
1283         wine_tsx11_lock();
1284         XFreeColormap( display, data->colormap );
1285         wine_tsx11_unlock();
1286     }
1287
1288     if (thread_data->last_focus == hwnd) thread_data->last_focus = 0;
1289     if (data->hWMIconBitmap) DeleteObject( data->hWMIconBitmap );
1290     if (data->hWMIconMask) DeleteObject( data->hWMIconMask);
1291     wine_tsx11_lock();
1292     XDeleteContext( display, (XID)hwnd, win_data_context );
1293     wine_tsx11_unlock();
1294     HeapFree( GetProcessHeap(), 0, data );
1295 }
1296
1297
1298 /***********************************************************************
1299  *              X11DRV_DestroyNotify
1300  */
1301 void X11DRV_DestroyNotify( HWND hwnd, XEvent *event )
1302 {
1303     Display *display = event->xdestroywindow.display;
1304     struct x11drv_win_data *data;
1305
1306     if (!(data = X11DRV_get_win_data( hwnd ))) return;
1307
1308     FIXME( "window %p/%lx destroyed from the outside\n", hwnd, data->whole_window );
1309     destroy_whole_window( display, data, TRUE );
1310 }
1311
1312
1313 static struct x11drv_win_data *alloc_win_data( Display *display, HWND hwnd )
1314 {
1315     struct x11drv_win_data *data;
1316
1317     if ((data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*data))))
1318     {
1319         data->hwnd = hwnd;
1320         wine_tsx11_lock();
1321         if (!winContext) winContext = XUniqueContext();
1322         if (!win_data_context) win_data_context = XUniqueContext();
1323         XSaveContext( display, (XID)hwnd, win_data_context, (char *)data );
1324         wine_tsx11_unlock();
1325     }
1326     return data;
1327 }
1328
1329
1330 /* initialize the desktop window id in the desktop manager process */
1331 static struct x11drv_win_data *create_desktop_win_data( Display *display, HWND hwnd )
1332 {
1333     struct x11drv_win_data *data;
1334     VisualID visualid;
1335
1336     if (!(data = alloc_win_data( display, hwnd ))) return NULL;
1337     wine_tsx11_lock();
1338     visualid = XVisualIDFromVisual(visual);
1339     wine_tsx11_unlock();
1340     data->whole_window = data->client_window = root_window;
1341     data->managed = TRUE;
1342     SetPropA( data->hwnd, managed_prop, (HANDLE)1 );
1343     SetPropA( data->hwnd, whole_window_prop, (HANDLE)root_window );
1344     SetPropA( data->hwnd, client_window_prop, (HANDLE)root_window );
1345     set_initial_wm_hints( display, data );
1346     return data;
1347 }
1348
1349 /**********************************************************************
1350  *              CreateDesktopWindow   (X11DRV.@)
1351  */
1352 BOOL X11DRV_CreateDesktopWindow( HWND hwnd )
1353 {
1354     unsigned int width, height;
1355
1356     /* retrieve the real size of the desktop */
1357     SERVER_START_REQ( get_window_rectangles )
1358     {
1359         req->handle = hwnd;
1360         wine_server_call( req );
1361         width  = reply->window.right - reply->window.left;
1362         height = reply->window.bottom - reply->window.top;
1363     }
1364     SERVER_END_REQ;
1365
1366     if (!width && !height)  /* not initialized yet */
1367     {
1368         SERVER_START_REQ( set_window_pos )
1369         {
1370             req->handle        = hwnd;
1371             req->previous      = 0;
1372             req->flags         = SWP_NOZORDER;
1373             req->window.left   = virtual_screen_rect.left;
1374             req->window.top    = virtual_screen_rect.top;
1375             req->window.right  = virtual_screen_rect.right;
1376             req->window.bottom = virtual_screen_rect.bottom;
1377             req->client        = req->window;
1378             wine_server_call( req );
1379         }
1380         SERVER_END_REQ;
1381     }
1382     else
1383     {
1384         Window win = (Window)GetPropA( hwnd, whole_window_prop );
1385         if (win && win != root_window) X11DRV_init_desktop( win, width, height );
1386     }
1387     return TRUE;
1388 }
1389
1390
1391 /**********************************************************************
1392  *              CreateWindow   (X11DRV.@)
1393  */
1394 BOOL X11DRV_CreateWindow( HWND hwnd )
1395 {
1396     Display *display = thread_display();
1397
1398     if (hwnd == GetDesktopWindow() && root_window != DefaultRootWindow( display ))
1399     {
1400         /* the desktop win data can't be created lazily */
1401         if (!create_desktop_win_data( display, hwnd )) return FALSE;
1402     }
1403     return TRUE;
1404 }
1405
1406
1407 /***********************************************************************
1408  *              X11DRV_get_win_data
1409  *
1410  * Return the X11 data structure associated with a window.
1411  */
1412 struct x11drv_win_data *X11DRV_get_win_data( HWND hwnd )
1413 {
1414     char *data;
1415
1416     if (!hwnd || XFindContext( thread_display(), (XID)hwnd, win_data_context, &data )) data = NULL;
1417     return (struct x11drv_win_data *)data;
1418 }
1419
1420
1421 /***********************************************************************
1422  *              X11DRV_create_win_data
1423  *
1424  * Create an X11 data window structure for an existing window.
1425  */
1426 struct x11drv_win_data *X11DRV_create_win_data( HWND hwnd )
1427 {
1428     Display *display = thread_display();
1429     struct x11drv_win_data *data;
1430     HWND parent;
1431
1432     if (!(parent = GetAncestor( hwnd, GA_PARENT ))) return NULL;  /* desktop */
1433     if (!(data = alloc_win_data( display, hwnd ))) return NULL;
1434
1435     GetWindowRect( hwnd, &data->window_rect );
1436     MapWindowPoints( 0, parent, (POINT *)&data->window_rect, 2 );
1437     data->whole_rect = data->window_rect;
1438     GetClientRect( hwnd, &data->client_rect );
1439     MapWindowPoints( hwnd, parent, (POINT *)&data->client_rect, 2 );
1440
1441     if (parent == GetDesktopWindow())
1442     {
1443         if (!create_whole_window( display, data ))
1444         {
1445             HeapFree( GetProcessHeap(), 0, data );
1446             return NULL;
1447         }
1448         TRACE( "win %p/%lx/%lx window %s whole %s client %s\n",
1449                hwnd, data->whole_window, data->client_window, wine_dbgstr_rect( &data->window_rect ),
1450                wine_dbgstr_rect( &data->whole_rect ), wine_dbgstr_rect( &data->client_rect ));
1451     }
1452     return data;
1453 }
1454
1455
1456 /***********************************************************************
1457  *              X11DRV_get_whole_window
1458  *
1459  * Return the X window associated with the full area of a window
1460  */
1461 Window X11DRV_get_whole_window( HWND hwnd )
1462 {
1463     struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
1464
1465     if (!data)
1466     {
1467         if (hwnd == GetDesktopWindow()) return root_window;
1468         return (Window)GetPropA( hwnd, whole_window_prop );
1469     }
1470     return data->whole_window;
1471 }
1472
1473
1474 /***********************************************************************
1475  *              X11DRV_get_client_window
1476  *
1477  * Return the X window associated with the client area of a window
1478  */
1479 Window X11DRV_get_client_window( HWND hwnd )
1480 {
1481     struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
1482
1483     if (!data)
1484     {
1485         if (hwnd == GetDesktopWindow()) return root_window;
1486         return (Window)GetPropA( hwnd, client_window_prop );
1487     }
1488     return data->client_window;
1489 }
1490
1491
1492 /***********************************************************************
1493  *              X11DRV_get_ic
1494  *
1495  * Return the X input context associated with a window
1496  */
1497 XIC X11DRV_get_ic( HWND hwnd )
1498 {
1499     struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
1500
1501     if (!data) return 0;
1502     return data->xic;
1503 }
1504
1505
1506 /***********************************************************************
1507  *              X11DRV_GetDC   (X11DRV.@)
1508  */
1509 void X11DRV_GetDC( HDC hdc, HWND hwnd, HWND top, const RECT *win_rect,
1510                    const RECT *top_rect, DWORD flags )
1511 {
1512     struct x11drv_escape_set_drawable escape;
1513     struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
1514
1515     escape.code        = X11DRV_SET_DRAWABLE;
1516     escape.mode        = IncludeInferiors;
1517     escape.fbconfig_id = 0;
1518     escape.gl_drawable = 0;
1519     escape.pixmap      = 0;
1520
1521     if (top == hwnd && data && IsIconic( hwnd ) && data->icon_window)
1522     {
1523         escape.drawable = data->icon_window;
1524     }
1525     else if (top == hwnd && (flags & DCX_WINDOW))
1526     {
1527         escape.drawable = data ? data->whole_window : X11DRV_get_whole_window( hwnd );
1528     }
1529     else
1530     {
1531         escape.drawable    = X11DRV_get_client_window( top );
1532         escape.fbconfig_id = data ? data->fbconfig_id : (XID)GetPropA( hwnd, fbconfig_id_prop );
1533         escape.gl_drawable = data ? data->gl_drawable : (Drawable)GetPropA( hwnd, gl_drawable_prop );
1534         escape.pixmap      = data ? data->pixmap : (Pixmap)GetPropA( hwnd, pixmap_prop );
1535         if (flags & DCX_CLIPCHILDREN) escape.mode = ClipByChildren;
1536     }
1537
1538     escape.dc_rect.left         = win_rect->left - top_rect->left;
1539     escape.dc_rect.top          = win_rect->top - top_rect->top;
1540     escape.dc_rect.right        = win_rect->right - top_rect->left;
1541     escape.dc_rect.bottom       = win_rect->bottom - top_rect->top;
1542     escape.drawable_rect.left   = top_rect->left;
1543     escape.drawable_rect.top    = top_rect->top;
1544     escape.drawable_rect.right  = top_rect->right;
1545     escape.drawable_rect.bottom = top_rect->bottom;
1546
1547     ExtEscape( hdc, X11DRV_ESCAPE, sizeof(escape), (LPSTR)&escape, 0, NULL );
1548 }
1549
1550
1551 /***********************************************************************
1552  *              X11DRV_ReleaseDC  (X11DRV.@)
1553  */
1554 void X11DRV_ReleaseDC( HWND hwnd, HDC hdc )
1555 {
1556     struct x11drv_escape_set_drawable escape;
1557
1558     escape.code = X11DRV_SET_DRAWABLE;
1559     escape.drawable = root_window;
1560     escape.mode = IncludeInferiors;
1561     escape.drawable_rect = virtual_screen_rect;
1562     SetRect( &escape.dc_rect, 0, 0, virtual_screen_rect.right - virtual_screen_rect.left,
1563              virtual_screen_rect.bottom - virtual_screen_rect.top );
1564     escape.fbconfig_id = 0;
1565     escape.gl_drawable = 0;
1566     escape.pixmap = 0;
1567     ExtEscape( hdc, X11DRV_ESCAPE, sizeof(escape), (LPSTR)&escape, 0, NULL );
1568 }
1569
1570
1571 /***********************************************************************
1572  *              SetCapture  (X11DRV.@)
1573  */
1574 void X11DRV_SetCapture( HWND hwnd, UINT flags )
1575 {
1576     struct x11drv_thread_data *thread_data = x11drv_thread_data();
1577
1578     if (!(flags & GUI_INMOVESIZE)) return;
1579
1580     if (hwnd)
1581     {
1582         Window grab_win = X11DRV_get_client_window( GetAncestor( hwnd, GA_ROOT ) );
1583
1584         if (!grab_win) return;
1585         wine_tsx11_lock();
1586         XFlush( gdi_display );
1587         XGrabPointer( thread_data->display, grab_win, False,
1588                       PointerMotionMask | ButtonPressMask | ButtonReleaseMask,
1589                       GrabModeAsync, GrabModeAsync, root_window, None, CurrentTime );
1590         wine_tsx11_unlock();
1591         thread_data->grab_window = grab_win;
1592     }
1593     else  /* release capture */
1594     {
1595         wine_tsx11_lock();
1596         XFlush( gdi_display );
1597         XUngrabPointer( thread_data->display, CurrentTime );
1598         wine_tsx11_unlock();
1599         thread_data->grab_window = None;
1600     }
1601 }
1602
1603
1604 /*****************************************************************
1605  *              SetParent   (X11DRV.@)
1606  */
1607 void X11DRV_SetParent( HWND hwnd, HWND parent, HWND old_parent )
1608 {
1609     Display *display = thread_display();
1610     struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
1611
1612     if (!data) return;
1613     if (parent == old_parent) return;
1614
1615     if (parent != GetDesktopWindow()) /* a child window */
1616     {
1617         if (old_parent == GetDesktopWindow())
1618         {
1619             /* destroy the old X windows */
1620             destroy_whole_window( display, data, FALSE );
1621             destroy_icon_window( display, data );
1622             if (data->managed)
1623             {
1624                 data->managed = FALSE;
1625                 RemovePropA( data->hwnd, managed_prop );
1626             }
1627         }
1628     }
1629     else  /* new top level window */
1630     {
1631         /* FIXME: we ignore errors since we can't really recover anyway */
1632         create_whole_window( display, data );
1633     }
1634 }
1635
1636
1637 /*****************************************************************
1638  *              SetFocus   (X11DRV.@)
1639  *
1640  * Set the X focus.
1641  * Explicit colormap management seems to work only with OLVWM.
1642  */
1643 void X11DRV_SetFocus( HWND hwnd )
1644 {
1645     Display *display = thread_display();
1646     struct x11drv_win_data *data;
1647     XWindowChanges changes;
1648
1649     /* If setting the focus to 0, uninstall the colormap */
1650     if (!hwnd && root_window == DefaultRootWindow(display))
1651     {
1652         wine_tsx11_lock();
1653         if (X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_PRIVATE)
1654             XUninstallColormap( display, X11DRV_PALETTE_PaletteXColormap );
1655         wine_tsx11_unlock();
1656         return;
1657     }
1658
1659     hwnd = GetAncestor( hwnd, GA_ROOT );
1660
1661     if (!(data = X11DRV_get_win_data( hwnd ))) return;
1662     if (data->managed || !data->whole_window) return;
1663
1664     /* Set X focus and install colormap */
1665     wine_tsx11_lock();
1666     changes.stack_mode = Above;
1667     XConfigureWindow( display, data->whole_window, CWStackMode, &changes );
1668     if (root_window == DefaultRootWindow(display))
1669     {
1670         /* we must not use CurrentTime (ICCCM), so try to use last message time instead */
1671         /* FIXME: this is not entirely correct */
1672         XSetInputFocus( display, data->whole_window, RevertToParent,
1673                         /* CurrentTime */
1674                         GetMessageTime() - EVENT_x11_time_to_win32_time(0));
1675         if (X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_PRIVATE)
1676             XInstallColormap( display, X11DRV_PALETTE_PaletteXColormap );
1677     }
1678     wine_tsx11_unlock();
1679 }
1680
1681
1682 /**********************************************************************
1683  *              SetWindowIcon (X11DRV.@)
1684  *
1685  * hIcon or hIconSm has changed (or is being initialised for the
1686  * first time). Complete the X11 driver-specific initialisation
1687  * and set the window hints.
1688  *
1689  * This is not entirely correct, may need to create
1690  * an icon window and set the pixmap as a background
1691  */
1692 void X11DRV_SetWindowIcon( HWND hwnd, UINT type, HICON icon )
1693 {
1694     Display *display = thread_display();
1695     struct x11drv_win_data *data;
1696
1697     if (type != ICON_BIG) return;  /* nothing to do here */
1698
1699     if (!(data = X11DRV_get_win_data( hwnd ))) return;
1700     if (!data->whole_window) return;
1701     if (!data->managed) return;
1702
1703     if (data->wm_hints)
1704     {
1705         set_icon_hints( display, data, icon );
1706         wine_tsx11_lock();
1707         XSetWMHints( display, data->whole_window, data->wm_hints );
1708         wine_tsx11_unlock();
1709     }
1710 }
1711
1712
1713 /***********************************************************************
1714  *              SetWindowRgn  (X11DRV.@)
1715  *
1716  * Assign specified region to window (for non-rectangular windows)
1717  */
1718 int X11DRV_SetWindowRgn( HWND hwnd, HRGN hrgn, BOOL redraw )
1719 {
1720     struct x11drv_win_data *data;
1721
1722     if ((data = X11DRV_get_win_data( hwnd )))
1723     {
1724         sync_window_region( thread_display(), data, hrgn );
1725     }
1726     else if (GetWindowThreadProcessId( hwnd, NULL ) != GetCurrentThreadId())
1727     {
1728         FIXME( "not supported on other thread window %p\n", hwnd );
1729         SetLastError( ERROR_INVALID_WINDOW_HANDLE );
1730         return FALSE;
1731     }
1732
1733     return TRUE;
1734 }