winex11: Separate the XIM process-wide setup from the IME creation.
[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 extern int usexcomposite;
68
69 /***********************************************************************
70  *              is_window_managed
71  *
72  * Check if a given window should be managed
73  */
74 BOOL is_window_managed( HWND hwnd, UINT swp_flags, const RECT *window_rect )
75 {
76     DWORD style, ex_style;
77
78     if (!managed_mode) return FALSE;
79
80     /* child windows are not managed */
81     style = GetWindowLongW( hwnd, GWL_STYLE );
82     if ((style & (WS_CHILD|WS_POPUP)) == WS_CHILD) return FALSE;
83     /* activated windows are managed */
84     if (!(swp_flags & (SWP_NOACTIVATE|SWP_HIDEWINDOW))) return TRUE;
85     if (hwnd == GetActiveWindow()) return TRUE;
86     /* windows with caption are managed */
87     if ((style & WS_CAPTION) == WS_CAPTION) return TRUE;
88     /* tool windows are not managed  */
89     ex_style = GetWindowLongW( hwnd, GWL_EXSTYLE );
90     if (ex_style & WS_EX_TOOLWINDOW) return FALSE;
91     /* windows with thick frame are managed */
92     if (style & WS_THICKFRAME) return TRUE;
93     /* application windows are managed */
94     if (ex_style & WS_EX_APPWINDOW) return TRUE;
95     if (style & WS_POPUP)
96     {
97         /* popup with sysmenu == caption are managed */
98         if (style & WS_SYSMENU) return TRUE;
99         /* full-screen popup windows are managed */
100         if (window_rect->left <= 0 && window_rect->right >= screen_width &&
101             window_rect->top <= 0 && window_rect->bottom >= screen_height)
102             return TRUE;
103     }
104     /* default: not managed */
105     return FALSE;
106 }
107
108
109 /***********************************************************************
110  *              X11DRV_is_window_rect_mapped
111  *
112  * Check if the X whole window should be mapped based on its rectangle
113  */
114 BOOL X11DRV_is_window_rect_mapped( const RECT *rect )
115 {
116     /* don't map if rect is empty */
117     if (IsRectEmpty( rect )) return FALSE;
118
119     /* don't map if rect is off-screen */
120     if (rect->left >= virtual_screen_rect.right ||
121         rect->top >= virtual_screen_rect.bottom ||
122         rect->right <= virtual_screen_rect.left ||
123         rect->bottom <= virtual_screen_rect.top)
124         return FALSE;
125
126     return TRUE;
127 }
128
129
130 /***********************************************************************
131  *              is_window_resizable
132  *
133  * Check if window should be made resizable by the window manager
134  */
135 static inline BOOL is_window_resizable( struct x11drv_win_data *data, DWORD style )
136 {
137     if (style & WS_THICKFRAME) return TRUE;
138     /* Metacity needs the window to be resizable to make it fullscreen */
139     return (data->whole_rect.left <= 0 && data->whole_rect.right >= screen_width &&
140             data->whole_rect.top <= 0 && data->whole_rect.bottom >= screen_height);
141 }
142
143
144 /***********************************************************************
145  *              get_mwm_decorations
146  */
147 static unsigned long get_mwm_decorations( DWORD style, DWORD ex_style )
148 {
149     unsigned long ret = 0;
150
151     if (ex_style & WS_EX_TOOLWINDOW) return 0;
152
153     if ((style & WS_CAPTION) == WS_CAPTION)
154     {
155         ret |= MWM_DECOR_TITLE | MWM_DECOR_BORDER;
156         if (style & WS_SYSMENU) ret |= MWM_DECOR_MENU;
157         if (style & WS_MINIMIZEBOX) ret |= MWM_DECOR_MINIMIZE;
158         if (style & WS_MAXIMIZEBOX) ret |= MWM_DECOR_MAXIMIZE;
159     }
160     if (ex_style & WS_EX_DLGMODALFRAME) ret |= MWM_DECOR_BORDER;
161     else if (style & WS_THICKFRAME) ret |= MWM_DECOR_BORDER | MWM_DECOR_RESIZEH;
162     else if ((style & (WS_DLGFRAME|WS_BORDER)) == WS_DLGFRAME) ret |= MWM_DECOR_BORDER;
163     return ret;
164 }
165
166
167 /***********************************************************************
168  *              get_x11_rect_offset
169  *
170  * Helper for X11DRV_window_to_X_rect and X11DRV_X_to_window_rect.
171  */
172 static void get_x11_rect_offset( struct x11drv_win_data *data, RECT *rect )
173 {
174     DWORD style, ex_style, style_mask = 0, ex_style_mask = 0;
175     unsigned long decor;
176
177     rect->top = rect->bottom = rect->left = rect->right = 0;
178
179     style = GetWindowLongW( data->hwnd, GWL_STYLE );
180     ex_style = GetWindowLongW( data->hwnd, GWL_EXSTYLE );
181     decor = get_mwm_decorations( style, ex_style );
182
183     if (decor & MWM_DECOR_TITLE) style_mask |= WS_CAPTION;
184     if (decor & MWM_DECOR_BORDER)
185     {
186         style_mask |= WS_DLGFRAME | WS_THICKFRAME;
187         ex_style_mask |= WS_EX_DLGMODALFRAME;
188     }
189
190     AdjustWindowRectEx( rect, style & style_mask, FALSE, ex_style & ex_style_mask );
191 }
192
193
194 /***********************************************************************
195  *              get_window_attributes
196  *
197  * Fill the window attributes structure for an X window.
198  */
199 static int get_window_attributes( Display *display, struct x11drv_win_data *data,
200                                   XSetWindowAttributes *attr )
201 {
202     attr->override_redirect = !data->managed;
203     attr->colormap          = X11DRV_PALETTE_PaletteXColormap;
204     attr->save_under        = ((GetClassLongW( data->hwnd, GCL_STYLE ) & CS_SAVEBITS) != 0);
205     attr->cursor            = x11drv_thread_data()->cursor;
206     attr->bit_gravity       = NorthWestGravity;
207     attr->backing_store     = NotUseful;
208     attr->event_mask        = (ExposureMask | PointerMotionMask |
209                                ButtonPressMask | ButtonReleaseMask | EnterWindowMask |
210                                KeyPressMask | KeyReleaseMask | FocusChangeMask | KeymapStateMask);
211     if (data->managed) attr->event_mask |= StructureNotifyMask | PropertyChangeMask;
212
213     return (CWOverrideRedirect | CWSaveUnder | CWColormap | CWCursor |
214             CWEventMask | CWBitGravity | CWBackingStore);
215 }
216
217
218 /***********************************************************************
219  *              create_client_window
220  */
221 static Window create_client_window( Display *display, struct x11drv_win_data *data, XVisualInfo *vis )
222 {
223     int cx, cy, mask;
224     XSetWindowAttributes attr;
225     Window client;
226
227     attr.bit_gravity = NorthWestGravity;
228     attr.win_gravity = NorthWestGravity;
229     attr.backing_store = NotUseful;
230     attr.event_mask = (ExposureMask | PointerMotionMask |
231                        ButtonPressMask | ButtonReleaseMask | EnterWindowMask);
232     mask = CWEventMask | CWBitGravity | CWWinGravity | CWBackingStore;
233
234     if ((cx = data->client_rect.right - data->client_rect.left) <= 0) cx = 1;
235     if ((cy = data->client_rect.bottom - data->client_rect.top) <= 0) cy = 1;
236
237     wine_tsx11_lock();
238
239     if (vis)
240     {
241         attr.colormap = XCreateColormap( display, root_window, vis->visual,
242                                          (vis->class == PseudoColor || vis->class == GrayScale ||
243                                           vis->class == DirectColor) ? AllocAll : AllocNone );
244         mask |= CWColormap;
245     }
246
247     client = XCreateWindow( display, data->whole_window,
248                             data->client_rect.left - data->whole_rect.left,
249                             data->client_rect.top - data->whole_rect.top,
250                             cx, cy, 0, screen_depth, InputOutput,
251                             vis ? vis->visual : visual, mask, &attr );
252     if (!client)
253     {
254         wine_tsx11_unlock();
255         return 0;
256     }
257
258     if (data->client_window)
259     {
260         struct x11drv_thread_data *thread_data = x11drv_thread_data();
261         if (thread_data->cursor_window == data->client_window) thread_data->cursor_window = None;
262         XDeleteContext( display, data->client_window, winContext );
263         XDestroyWindow( display, data->client_window );
264     }
265     data->client_window = client;
266
267     if (data->colormap) XFreeColormap( display, data->colormap );
268     data->colormap = vis ? attr.colormap : 0;
269
270     XMapWindow( display, data->client_window );
271     XSaveContext( display, data->client_window, winContext, (char *)data->hwnd );
272     wine_tsx11_unlock();
273
274     SetPropA( data->hwnd, client_window_prop, (HANDLE)data->client_window );
275     return data->client_window;
276 }
277
278
279 /***********************************************************************
280  *              X11DRV_sync_window_style
281  *
282  * Change the X window attributes when the window style has changed.
283  */
284 void X11DRV_sync_window_style( Display *display, struct x11drv_win_data *data )
285 {
286     if (data->whole_window != root_window)
287     {
288         XSetWindowAttributes attr;
289         int mask = get_window_attributes( display, data, &attr );
290
291         wine_tsx11_lock();
292         XChangeWindowAttributes( display, data->whole_window, mask, &attr );
293         wine_tsx11_unlock();
294     }
295 }
296
297
298 /***********************************************************************
299  *              sync_window_region
300  *
301  * Update the X11 window region.
302  */
303 static void sync_window_region( Display *display, struct x11drv_win_data *data, HRGN hrgn )
304 {
305 #ifdef HAVE_LIBXSHAPE
306     if (!data->whole_window) return;
307
308     if (!hrgn)
309     {
310         wine_tsx11_lock();
311         XShapeCombineMask( display, data->whole_window, ShapeBounding, 0, 0, None, ShapeSet );
312         wine_tsx11_unlock();
313     }
314     else
315     {
316         RGNDATA *pRegionData = X11DRV_GetRegionData( hrgn, 0 );
317         if (pRegionData)
318         {
319             wine_tsx11_lock();
320             XShapeCombineRectangles( display, data->whole_window, ShapeBounding,
321                                      data->window_rect.left - data->whole_rect.left,
322                                      data->window_rect.top - data->whole_rect.top,
323                                      (XRectangle *)pRegionData->Buffer,
324                                      pRegionData->rdh.nCount, ShapeSet, YXBanded );
325             wine_tsx11_unlock();
326             HeapFree(GetProcessHeap(), 0, pRegionData);
327         }
328     }
329 #endif  /* HAVE_LIBXSHAPE */
330 }
331
332
333 /***********************************************************************
334  *              sync_window_text
335  */
336 static void sync_window_text( Display *display, Window win, const WCHAR *text )
337 {
338     UINT count;
339     char *buffer, *utf8_buffer;
340     XTextProperty prop;
341
342     /* allocate new buffer for window text */
343     count = WideCharToMultiByte(CP_UNIXCP, 0, text, -1, NULL, 0, NULL, NULL);
344     if (!(buffer = HeapAlloc( GetProcessHeap(), 0, count ))) return;
345     WideCharToMultiByte(CP_UNIXCP, 0, text, -1, buffer, count, NULL, NULL);
346
347     count = WideCharToMultiByte(CP_UTF8, 0, text, strlenW(text), NULL, 0, NULL, NULL);
348     if (!(utf8_buffer = HeapAlloc( GetProcessHeap(), 0, count )))
349     {
350         HeapFree( GetProcessHeap(), 0, buffer );
351         return;
352     }
353     WideCharToMultiByte(CP_UTF8, 0, text, strlenW(text), utf8_buffer, count, NULL, NULL);
354
355     wine_tsx11_lock();
356     if (XmbTextListToTextProperty( display, &buffer, 1, XStdICCTextStyle, &prop ) == Success)
357     {
358         XSetWMName( display, win, &prop );
359         XSetWMIconName( display, win, &prop );
360         XFree( prop.value );
361     }
362     /*
363       Implements a NET_WM UTF-8 title. It should be without a trailing \0,
364       according to the standard
365       ( http://www.pps.jussieu.fr/~jch/software/UTF8_STRING/UTF8_STRING.text ).
366     */
367     XChangeProperty( display, win, x11drv_atom(_NET_WM_NAME), x11drv_atom(UTF8_STRING),
368                      8, PropModeReplace, (unsigned char *) utf8_buffer, count);
369     wine_tsx11_unlock();
370
371     HeapFree( GetProcessHeap(), 0, utf8_buffer );
372     HeapFree( GetProcessHeap(), 0, buffer );
373 }
374
375
376 /***********************************************************************
377  *              X11DRV_set_win_format
378  */
379 BOOL X11DRV_set_win_format( HWND hwnd, XID fbconfig_id )
380 {
381     Display *display = thread_display();
382     struct x11drv_win_data *data;
383     XVisualInfo *vis;
384     int w, h;
385
386     if (!(data = X11DRV_get_win_data(hwnd)) &&
387         !(data = X11DRV_create_win_data(hwnd))) return FALSE;
388
389     if (data->fbconfig_id) return FALSE;  /* can't change it twice */
390
391     wine_tsx11_lock();
392     vis = visual_from_fbconfig_id(fbconfig_id);
393     wine_tsx11_unlock();
394     if (!vis) return FALSE;
395
396     if (data->whole_window)
397     {
398         Window client = data->client_window;
399
400         if (vis->visualid != XVisualIDFromVisual(visual))
401         {
402             client = create_client_window( display, data, vis );
403             TRACE( "re-created client window %lx for %p fbconfig %lx\n", client, data->hwnd, fbconfig_id );
404         }
405         wine_tsx11_lock();
406         XFree(vis);
407         wine_tsx11_unlock();
408         if (client) goto done;
409         return FALSE;
410     }
411
412     w = data->client_rect.right - data->client_rect.left;
413     h = data->client_rect.bottom - data->client_rect.top;
414
415     if(w <= 0) w = 1;
416     if(h <= 0) h = 1;
417
418 #ifdef SONAME_LIBXCOMPOSITE
419     if(usexcomposite)
420     {
421         XSetWindowAttributes attrib;
422         Window parent = X11DRV_get_whole_window( GetAncestor( hwnd, GA_ROOT ));
423
424         if (!parent) parent = root_window;
425         wine_tsx11_lock();
426         data->colormap = XCreateColormap(display, parent, vis->visual,
427                                          (vis->class == PseudoColor ||
428                                           vis->class == GrayScale ||
429                                           vis->class == DirectColor) ?
430                                          AllocAll : AllocNone);
431         attrib.override_redirect = True;
432         attrib.colormap = data->colormap;
433         XInstallColormap(gdi_display, attrib.colormap);
434
435         data->gl_drawable = XCreateWindow(display, parent, -w, 0, w, h, 0,
436                                           vis->depth, InputOutput, vis->visual,
437                                           CWColormap | CWOverrideRedirect,
438                                           &attrib);
439         if(data->gl_drawable)
440         {
441             pXCompositeRedirectWindow(display, data->gl_drawable,
442                                       CompositeRedirectManual);
443             XMapWindow(display, data->gl_drawable);
444         }
445         XFree(vis);
446         wine_tsx11_unlock();
447     }
448     else
449 #endif
450     {
451         WARN("XComposite is not available, using GLXPixmap hack\n");
452
453         wine_tsx11_lock();
454         data->pixmap = XCreatePixmap(display, root_window, w, h, vis->depth);
455         if(!data->pixmap)
456         {
457             XFree(vis);
458             wine_tsx11_unlock();
459             return FALSE;
460         }
461
462         data->gl_drawable = create_glxpixmap(display, vis, data->pixmap);
463         if(!data->gl_drawable)
464         {
465             XFreePixmap(display, data->pixmap);
466             data->pixmap = 0;
467         }
468         XFree(vis);
469         wine_tsx11_unlock();
470         if (data->pixmap) SetPropA(hwnd, pixmap_prop, (HANDLE)data->pixmap);
471     }
472
473     if (!data->gl_drawable) return FALSE;
474
475     TRACE("Created GL drawable 0x%lx, using FBConfigID 0x%lx\n",
476           data->gl_drawable, fbconfig_id);
477     SetPropA(hwnd, gl_drawable_prop, (HANDLE)data->gl_drawable);
478
479 done:
480     data->fbconfig_id = fbconfig_id;
481     SetPropA(hwnd, fbconfig_id_prop, (HANDLE)data->fbconfig_id);
482     wine_tsx11_lock();
483     XFlush( display );
484     wine_tsx11_unlock();
485     WIN_invalidate_dce( hwnd, NULL );
486     return TRUE;
487 }
488
489 /***********************************************************************
490  *              sync_gl_drawable
491  */
492 static void sync_gl_drawable(Display *display, struct x11drv_win_data *data)
493 {
494     int w = data->client_rect.right - data->client_rect.left;
495     int h = data->client_rect.bottom - data->client_rect.top;
496     XVisualInfo *vis;
497     Drawable glxp;
498     Pixmap pix;
499
500     if (w <= 0) w = 1;
501     if (h <= 0) h = 1;
502
503     TRACE("Resizing GL drawable 0x%lx to %dx%d\n", data->gl_drawable, w, h);
504 #ifdef SONAME_LIBXCOMPOSITE
505     if(usexcomposite)
506     {
507         wine_tsx11_lock();
508         XMoveResizeWindow(display, data->gl_drawable, -w, 0, w, h);
509         wine_tsx11_unlock();
510         return;
511     }
512 #endif
513
514     wine_tsx11_lock();
515
516     vis = visual_from_fbconfig_id(data->fbconfig_id);
517     if(!vis)
518     {
519         wine_tsx11_unlock();
520         return;
521     }
522
523     pix = XCreatePixmap(display, root_window, w, h, vis->depth);
524     if(!pix)
525     {
526         ERR("Failed to create pixmap for offscreen rendering\n");
527         XFree(vis);
528         wine_tsx11_unlock();
529         return;
530     }
531
532     glxp = create_glxpixmap(display, vis, pix);
533     if(!glxp)
534     {
535         ERR("Failed to create drawable for offscreen rendering\n");
536         XFreePixmap(display, pix);
537         XFree(vis);
538         wine_tsx11_unlock();
539         return;
540     }
541
542     XFree(vis);
543
544     mark_drawable_dirty(data->gl_drawable, glxp);
545
546     XFreePixmap(display, data->pixmap);
547     destroy_glxpixmap(display, data->gl_drawable);
548
549     data->pixmap = pix;
550     data->gl_drawable = glxp;
551
552     wine_tsx11_unlock();
553
554     SetPropA(data->hwnd, gl_drawable_prop, (HANDLE)data->gl_drawable);
555     SetPropA(data->hwnd, pixmap_prop, (HANDLE)data->pixmap);
556 }
557
558
559 /***********************************************************************
560  *              get_window_changes
561  *
562  * fill the window changes structure
563  */
564 static int get_window_changes( XWindowChanges *changes, const RECT *old, const RECT *new )
565 {
566     int mask = 0;
567
568     if (old->right - old->left != new->right - new->left )
569     {
570         if ((changes->width = new->right - new->left) <= 0) changes->width = 1;
571         mask |= CWWidth;
572     }
573     if (old->bottom - old->top != new->bottom - new->top)
574     {
575         if ((changes->height = new->bottom - new->top) <= 0) changes->height = 1;
576         mask |= CWHeight;
577     }
578     if (old->left != new->left)
579     {
580         changes->x = new->left;
581         mask |= CWX;
582     }
583     if (old->top != new->top)
584     {
585         changes->y = new->top;
586         mask |= CWY;
587     }
588     return mask;
589 }
590
591
592 /***********************************************************************
593  *              create_icon_window
594  */
595 static Window create_icon_window( Display *display, struct x11drv_win_data *data )
596 {
597     XSetWindowAttributes attr;
598
599     attr.event_mask = (ExposureMask | KeyPressMask | KeyReleaseMask | PointerMotionMask |
600                        ButtonPressMask | ButtonReleaseMask | EnterWindowMask);
601     attr.bit_gravity = NorthWestGravity;
602     attr.backing_store = NotUseful/*WhenMapped*/;
603     attr.colormap      = X11DRV_PALETTE_PaletteXColormap; /* Needed due to our visual */
604
605     wine_tsx11_lock();
606     data->icon_window = XCreateWindow( display, root_window, 0, 0,
607                                        GetSystemMetrics( SM_CXICON ),
608                                        GetSystemMetrics( SM_CYICON ),
609                                        0, screen_depth,
610                                        InputOutput, visual,
611                                        CWEventMask | CWBitGravity | CWBackingStore | CWColormap, &attr );
612     XSaveContext( display, data->icon_window, winContext, (char *)data->hwnd );
613     XFlush( display );  /* make sure the window exists before we start painting to it */
614     wine_tsx11_unlock();
615
616     TRACE( "created %lx\n", data->icon_window );
617     SetPropA( data->hwnd, icon_window_prop, (HANDLE)data->icon_window );
618     return data->icon_window;
619 }
620
621
622
623 /***********************************************************************
624  *              destroy_icon_window
625  */
626 static void destroy_icon_window( Display *display, struct x11drv_win_data *data )
627 {
628     if (!data->icon_window) return;
629     if (x11drv_thread_data()->cursor_window == data->icon_window)
630         x11drv_thread_data()->cursor_window = None;
631     wine_tsx11_lock();
632     XDeleteContext( display, data->icon_window, winContext );
633     XDestroyWindow( display, data->icon_window );
634     data->icon_window = 0;
635     wine_tsx11_unlock();
636     RemovePropA( data->hwnd, icon_window_prop );
637 }
638
639
640 /***********************************************************************
641  *              set_icon_hints
642  *
643  * Set the icon wm hints
644  */
645 static void set_icon_hints( Display *display, struct x11drv_win_data *data, HICON hIcon )
646 {
647     XWMHints *hints = data->wm_hints;
648
649     if (data->hWMIconBitmap) DeleteObject( data->hWMIconBitmap );
650     if (data->hWMIconMask) DeleteObject( data->hWMIconMask);
651     data->hWMIconBitmap = 0;
652     data->hWMIconMask = 0;
653
654     if (!hIcon)
655     {
656         if (!data->icon_window) create_icon_window( display, data );
657         hints->icon_window = data->icon_window;
658         hints->flags = (hints->flags & ~(IconPixmapHint | IconMaskHint)) | IconWindowHint;
659     }
660     else
661     {
662         HBITMAP hbmOrig;
663         RECT rcMask;
664         BITMAP bmMask;
665         ICONINFO ii;
666         HDC hDC;
667
668         GetIconInfo(hIcon, &ii);
669
670         GetObjectA(ii.hbmMask, sizeof(bmMask), &bmMask);
671         rcMask.top    = 0;
672         rcMask.left   = 0;
673         rcMask.right  = bmMask.bmWidth;
674         rcMask.bottom = bmMask.bmHeight;
675
676         hDC = CreateCompatibleDC(0);
677         hbmOrig = SelectObject(hDC, ii.hbmMask);
678         InvertRect(hDC, &rcMask);
679         SelectObject(hDC, ii.hbmColor);  /* force the color bitmap to x11drv mode too */
680         SelectObject(hDC, hbmOrig);
681         DeleteDC(hDC);
682
683         data->hWMIconBitmap = ii.hbmColor;
684         data->hWMIconMask = ii.hbmMask;
685
686         hints->icon_pixmap = X11DRV_get_pixmap(data->hWMIconBitmap);
687         hints->icon_mask = X11DRV_get_pixmap(data->hWMIconMask);
688         destroy_icon_window( display, data );
689         hints->flags = (hints->flags & ~IconWindowHint) | IconPixmapHint | IconMaskHint;
690     }
691 }
692
693
694 /***********************************************************************
695  *              set_size_hints
696  *
697  * set the window size hints
698  */
699 static void set_size_hints( Display *display, struct x11drv_win_data *data, DWORD style )
700 {
701     XSizeHints* size_hints;
702
703     if (!(size_hints = XAllocSizeHints())) return;
704
705     /* don't update size hints if window is not in normal state */
706     if (!(style & (WS_MINIMIZE | WS_MAXIMIZE)))
707     {
708         if (data->hwnd != GetDesktopWindow())  /* don't force position of desktop */
709         {
710             size_hints->win_gravity = StaticGravity;
711             size_hints->x = data->whole_rect.left;
712             size_hints->y = data->whole_rect.top;
713             size_hints->flags |= PWinGravity | PPosition;
714         }
715
716         if (!is_window_resizable( data, style ))
717         {
718             size_hints->max_width = data->whole_rect.right - data->whole_rect.left;
719             size_hints->max_height = data->whole_rect.bottom - data->whole_rect.top;
720             size_hints->min_width = size_hints->max_width;
721             size_hints->min_height = size_hints->max_height;
722             size_hints->flags |= PMinSize | PMaxSize;
723         }
724     }
725     XSetWMNormalHints( display, data->whole_window, size_hints );
726     XFree( size_hints );
727 }
728
729
730 /***********************************************************************
731  *              get_process_name
732  *
733  * get the name of the current process for setting class hints
734  */
735 static char *get_process_name(void)
736 {
737     static char *name;
738
739     if (!name)
740     {
741         WCHAR module[MAX_PATH];
742         DWORD len = GetModuleFileNameW( 0, module, MAX_PATH );
743         if (len && len < MAX_PATH)
744         {
745             char *ptr;
746             WCHAR *p, *appname = module;
747
748             if ((p = strrchrW( appname, '/' ))) appname = p + 1;
749             if ((p = strrchrW( appname, '\\' ))) appname = p + 1;
750             len = WideCharToMultiByte( CP_UNIXCP, 0, appname, -1, NULL, 0, NULL, NULL );
751             if ((ptr = HeapAlloc( GetProcessHeap(), 0, len )))
752             {
753                 WideCharToMultiByte( CP_UNIXCP, 0, appname, -1, ptr, len, NULL, NULL );
754                 name = ptr;
755             }
756         }
757     }
758     return name;
759 }
760
761
762 /***********************************************************************
763  *              set_initial_wm_hints
764  *
765  * Set the window manager hints that don't change over the lifetime of a window.
766  */
767 static void set_initial_wm_hints( Display *display, struct x11drv_win_data *data )
768 {
769     long i;
770     Atom protocols[3];
771     Atom dndVersion = 4;
772     XClassHint *class_hints;
773     char *process_name = get_process_name();
774
775     wine_tsx11_lock();
776
777     /* wm protocols */
778     i = 0;
779     protocols[i++] = x11drv_atom(WM_DELETE_WINDOW);
780     protocols[i++] = x11drv_atom(_NET_WM_PING);
781     if (use_take_focus) protocols[i++] = x11drv_atom(WM_TAKE_FOCUS);
782     XChangeProperty( display, data->whole_window, x11drv_atom(WM_PROTOCOLS),
783                      XA_ATOM, 32, PropModeReplace, (unsigned char *)protocols, i );
784
785     /* class hints */
786     if ((class_hints = XAllocClassHint()))
787     {
788         static char wine[] = "Wine";
789
790         class_hints->res_name = process_name;
791         class_hints->res_class = wine;
792         XSetClassHint( display, data->whole_window, class_hints );
793         XFree( class_hints );
794     }
795
796     /* set the WM_CLIENT_MACHINE and WM_LOCALE_NAME properties */
797     XSetWMProperties(display, data->whole_window, NULL, NULL, NULL, 0, NULL, NULL, NULL);
798     /* set the pid. together, these properties are needed so the window manager can kill us if we freeze */
799     i = getpid();
800     XChangeProperty(display, data->whole_window, x11drv_atom(_NET_WM_PID),
801                     XA_CARDINAL, 32, PropModeReplace, (unsigned char *)&i, 1);
802
803     XChangeProperty( display, data->whole_window, x11drv_atom(XdndAware),
804                      XA_ATOM, 32, PropModeReplace, (unsigned char*)&dndVersion, 1 );
805
806     data->wm_hints = XAllocWMHints();
807     wine_tsx11_unlock();
808
809     if (data->wm_hints)
810     {
811         HICON icon = (HICON)SendMessageW( data->hwnd, WM_GETICON, ICON_BIG, 0 );
812         if (!icon) icon = (HICON)GetClassLongPtrW( data->hwnd, GCLP_HICON );
813         data->wm_hints->flags = 0;
814         set_icon_hints( display, data, icon );
815     }
816 }
817
818
819 /***********************************************************************
820  *              X11DRV_set_wm_hints
821  *
822  * Set the window manager hints for a newly-created window
823  */
824 void X11DRV_set_wm_hints( Display *display, struct x11drv_win_data *data )
825 {
826     Window group_leader;
827     Atom window_type;
828     MwmHints mwm_hints;
829     DWORD style = GetWindowLongW( data->hwnd, GWL_STYLE );
830     DWORD ex_style = GetWindowLongW( data->hwnd, GWL_EXSTYLE );
831     HWND owner = GetWindow( data->hwnd, GW_OWNER );
832
833     if (data->hwnd == GetDesktopWindow())
834     {
835         /* force some styles for the desktop to get the correct decorations */
836         style |= WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX;
837         owner = 0;
838     }
839
840     /* transient for hint */
841     if (owner)
842     {
843         Window owner_win = X11DRV_get_whole_window( owner );
844         wine_tsx11_lock();
845         XSetTransientForHint( display, data->whole_window, owner_win );
846         wine_tsx11_unlock();
847         group_leader = owner_win;
848     }
849     else group_leader = data->whole_window;
850
851     wine_tsx11_lock();
852
853     /* size hints */
854     set_size_hints( display, data, style );
855
856     /* set the WM_WINDOW_TYPE */
857     window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_NORMAL);
858     if (style & WS_THICKFRAME) window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_NORMAL);
859     else if (style & WS_DLGFRAME) window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_DIALOG);
860     else if (ex_style & WS_EX_DLGMODALFRAME) window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_DIALOG);
861 #if 0  /* many window managers don't handle utility windows very well */
862     else if (ex_style & WS_EX_TOOLWINDOW) window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_UTILITY);
863 #endif
864
865     XChangeProperty(display, data->whole_window, x11drv_atom(_NET_WM_WINDOW_TYPE),
866                     XA_ATOM, 32, PropModeReplace, (unsigned char*)&window_type, 1);
867
868     mwm_hints.flags = MWM_HINTS_FUNCTIONS | MWM_HINTS_DECORATIONS;
869     mwm_hints.decorations = get_mwm_decorations( style, ex_style );
870     mwm_hints.functions = MWM_FUNC_MOVE;
871     if (is_window_resizable( data, style )) mwm_hints.functions |= MWM_FUNC_RESIZE;
872     if (style & WS_MINIMIZEBOX) mwm_hints.functions |= MWM_FUNC_MINIMIZE;
873     if (style & WS_MAXIMIZEBOX) mwm_hints.functions |= MWM_FUNC_MAXIMIZE;
874     if (style & WS_SYSMENU)     mwm_hints.functions |= MWM_FUNC_CLOSE;
875
876     XChangeProperty( display, data->whole_window, x11drv_atom(_MOTIF_WM_HINTS),
877                      x11drv_atom(_MOTIF_WM_HINTS), 32, PropModeReplace,
878                      (unsigned char*)&mwm_hints, sizeof(mwm_hints)/sizeof(long) );
879
880     /* wm hints */
881     if (data->wm_hints)
882     {
883         data->wm_hints->flags |= InputHint | StateHint | WindowGroupHint;
884         data->wm_hints->input = !(style & WS_DISABLED);
885         data->wm_hints->initial_state = (style & WS_MINIMIZE) ? IconicState : NormalState;
886         data->wm_hints->window_group = group_leader;
887         XSetWMHints( display, data->whole_window, data->wm_hints );
888     }
889
890     wine_tsx11_unlock();
891 }
892
893
894 /***********************************************************************
895  *              X11DRV_window_to_X_rect
896  *
897  * Convert a rect from client to X window coordinates
898  */
899 void X11DRV_window_to_X_rect( struct x11drv_win_data *data, RECT *rect )
900 {
901     RECT rc;
902
903     if (!data->managed) return;
904     if (IsRectEmpty( rect )) return;
905
906     get_x11_rect_offset( data, &rc );
907
908     rect->left   -= rc.left;
909     rect->right  -= rc.right;
910     rect->top    -= rc.top;
911     rect->bottom -= rc.bottom;
912     if (rect->top >= rect->bottom) rect->bottom = rect->top + 1;
913     if (rect->left >= rect->right) rect->right = rect->left + 1;
914 }
915
916
917 /***********************************************************************
918  *              X11DRV_X_to_window_rect
919  *
920  * Opposite of X11DRV_window_to_X_rect
921  */
922 void X11DRV_X_to_window_rect( struct x11drv_win_data *data, RECT *rect )
923 {
924     RECT rc;
925
926     if (!data->managed) return;
927     if (IsRectEmpty( rect )) return;
928
929     get_x11_rect_offset( data, &rc );
930
931     rect->left   += rc.left;
932     rect->right  += rc.right;
933     rect->top    += rc.top;
934     rect->bottom += rc.bottom;
935     if (rect->top >= rect->bottom) rect->bottom = rect->top + 1;
936     if (rect->left >= rect->right) rect->right = rect->left + 1;
937 }
938
939
940 /***********************************************************************
941  *              X11DRV_sync_window_position
942  *
943  * Synchronize the X window position with the Windows one
944  */
945 void X11DRV_sync_window_position( Display *display, struct x11drv_win_data *data,
946                                   UINT swp_flags, const RECT *old_client_rect,
947                                   const RECT *old_whole_rect )
948 {
949     DWORD style = GetWindowLongW( data->hwnd, GWL_STYLE );
950     XWindowChanges changes;
951     int mask = CWWidth | CWHeight;
952
953     if (data->managed && data->iconic) return;
954
955     if ((changes.width = data->whole_rect.right - data->whole_rect.left) <= 0) changes.width = 1;
956     if ((changes.height = data->whole_rect.bottom - data->whole_rect.top) <= 0) changes.height = 1;
957
958     /* only the size is allowed to change for the desktop window */
959     if (data->whole_window != root_window)
960     {
961         changes.x = data->whole_rect.left - virtual_screen_rect.left;
962         changes.y = data->whole_rect.top - virtual_screen_rect.top;
963         mask |= CWX | CWY;
964     }
965
966     if (!(swp_flags & SWP_NOZORDER))
967     {
968         /* find window that this one must be after */
969         HWND prev = GetWindow( data->hwnd, GW_HWNDPREV );
970         while (prev && !(GetWindowLongW( prev, GWL_STYLE ) & WS_VISIBLE))
971             prev = GetWindow( prev, GW_HWNDPREV );
972         if (!prev)  /* top child */
973         {
974             changes.stack_mode = Above;
975             mask |= CWStackMode;
976         }
977         /* should use stack_mode Below but most window managers don't get it right */
978         /* and Above with a sibling doesn't work so well either, so we ignore it */
979     }
980
981     TRACE( "setting win %p/%lx pos %d,%d,%dx%d after %lx changes=%x\n",
982            data->hwnd, data->whole_window, data->whole_rect.left, data->whole_rect.top,
983            data->whole_rect.right - data->whole_rect.left,
984            data->whole_rect.bottom - data->whole_rect.top, changes.sibling, mask );
985
986     wine_tsx11_lock();
987     set_size_hints( display, data, style );
988     XReconfigureWMWindow( display, data->whole_window,
989                           DefaultScreen(display), mask, &changes );
990     wine_tsx11_unlock();
991 }
992
993
994 /***********************************************************************
995  *              X11DRV_sync_client_position
996  *
997  * Synchronize the X client window position with the Windows one
998  */
999 void X11DRV_sync_client_position( Display *display, struct x11drv_win_data *data,
1000                                   UINT swp_flags, const RECT *old_client_rect,
1001                                   const RECT *old_whole_rect )
1002 {
1003     int mask;
1004     XWindowChanges changes;
1005     RECT old = *old_client_rect;
1006     RECT new = data->client_rect;
1007
1008     OffsetRect( &old, -old_whole_rect->left, -old_whole_rect->top );
1009     OffsetRect( &new, -data->whole_rect.left, -data->whole_rect.top );
1010     if (!(mask = get_window_changes( &changes, &old, &new ))) return;
1011
1012     if (data->client_window)
1013     {
1014         TRACE( "setting client win %lx pos %d,%d,%dx%d changes=%x\n",
1015                data->client_window, new.left, new.top,
1016                new.right - new.left, new.bottom - new.top, mask );
1017         wine_tsx11_lock();
1018         XConfigureWindow( display, data->client_window, mask, &changes );
1019         wine_tsx11_unlock();
1020     }
1021
1022     if (data->gl_drawable && (mask & (CWWidth|CWHeight))) sync_gl_drawable( display, data );
1023
1024     /* make sure the changes get to the server before we start painting */
1025     if (data->client_window || data->gl_drawable)
1026     {
1027         wine_tsx11_lock();
1028         XFlush(display);
1029         wine_tsx11_unlock();
1030     }
1031 }
1032
1033
1034 /**********************************************************************
1035  *              create_whole_window
1036  *
1037  * Create the whole X window for a given window
1038  */
1039 static Window create_whole_window( Display *display, struct x11drv_win_data *data )
1040 {
1041     int cx, cy, mask;
1042     XSetWindowAttributes attr;
1043     XIM xim;
1044     WCHAR text[1024];
1045     HRGN hrgn;
1046
1047     if (!(cx = data->window_rect.right - data->window_rect.left)) cx = 1;
1048     if (!(cy = data->window_rect.bottom - data->window_rect.top)) cy = 1;
1049
1050     if (!data->managed && is_window_managed( data->hwnd, SWP_NOACTIVATE, &data->window_rect ))
1051     {
1052         TRACE( "making win %p/%lx managed\n", data->hwnd, data->whole_window );
1053         data->managed = TRUE;
1054         SetPropA( data->hwnd, managed_prop, (HANDLE)1 );
1055     }
1056
1057     mask = get_window_attributes( display, data, &attr );
1058
1059     wine_tsx11_lock();
1060
1061     data->whole_rect = data->window_rect;
1062     data->whole_window = XCreateWindow( display, root_window,
1063                                         data->window_rect.left - virtual_screen_rect.left,
1064                                         data->window_rect.top - virtual_screen_rect.top,
1065                                         cx, cy, 0, screen_depth, InputOutput,
1066                                         visual, mask, &attr );
1067
1068     if (data->whole_window) XSaveContext( display, data->whole_window, winContext, (char *)data->hwnd );
1069     wine_tsx11_unlock();
1070
1071     if (!data->whole_window) return 0;
1072
1073     if (!create_client_window( display, data, NULL ))
1074     {
1075         wine_tsx11_lock();
1076         XDeleteContext( display, data->whole_window, winContext );
1077         XDestroyWindow( display, data->whole_window );
1078         data->whole_window = 0;
1079         wine_tsx11_unlock();
1080         return 0;
1081     }
1082
1083     xim = x11drv_thread_data()->xim;
1084     if (xim) data->xic = X11DRV_CreateIC( xim, display, data->whole_window );
1085
1086     set_initial_wm_hints( display, data );
1087     X11DRV_set_wm_hints( display, data );
1088
1089     SetPropA( data->hwnd, whole_window_prop, (HANDLE)data->whole_window );
1090
1091     /* set the window text */
1092     if (!InternalGetWindowText( data->hwnd, text, sizeof(text)/sizeof(WCHAR) )) text[0] = 0;
1093     sync_window_text( display, data->whole_window, text );
1094
1095     /* set the window region */
1096     if ((hrgn = CreateRectRgn( 0, 0, 0, 0 )))
1097     {
1098         if (GetWindowRgn( data->hwnd, hrgn ) != ERROR) sync_window_region( display, data, hrgn );
1099         DeleteObject( hrgn );
1100     }
1101     wine_tsx11_lock();
1102     XFlush( display );  /* make sure the window exists before we start painting to it */
1103     wine_tsx11_unlock();
1104     return data->whole_window;
1105 }
1106
1107
1108 /**********************************************************************
1109  *              destroy_whole_window
1110  *
1111  * Destroy the whole X window for a given window.
1112  */
1113 static void destroy_whole_window( Display *display, struct x11drv_win_data *data, BOOL already_destroyed )
1114 {
1115     struct x11drv_thread_data *thread_data = x11drv_thread_data();
1116
1117     if (!data->whole_window) return;
1118
1119     TRACE( "win %p xwin %lx/%lx\n", data->hwnd, data->whole_window, data->client_window );
1120     if (thread_data->cursor_window == data->whole_window ||
1121         thread_data->cursor_window == data->client_window)
1122         thread_data->cursor_window = None;
1123     wine_tsx11_lock();
1124     XDeleteContext( display, data->whole_window, winContext );
1125     XDeleteContext( display, data->client_window, winContext );
1126     if (!already_destroyed) XDestroyWindow( display, data->whole_window );
1127     data->whole_window = data->client_window = 0;
1128     data->wm_state = WithdrawnState;
1129     data->net_wm_state = 0;
1130     data->mapped = FALSE;
1131     if (data->xic)
1132     {
1133         XUnsetICFocus( data->xic );
1134         XDestroyIC( data->xic );
1135         data->xic = 0;
1136     }
1137     /* Outlook stops processing messages after destroying a dialog, so we need an explicit flush */
1138     XFlush( display );
1139     XFree( data->wm_hints );
1140     data->wm_hints = NULL;
1141     wine_tsx11_unlock();
1142     RemovePropA( data->hwnd, whole_window_prop );
1143     RemovePropA( data->hwnd, client_window_prop );
1144 }
1145
1146
1147 /*****************************************************************
1148  *              SetWindowText   (X11DRV.@)
1149  */
1150 void X11DRV_SetWindowText( HWND hwnd, LPCWSTR text )
1151 {
1152     Display *display = thread_display();
1153     Window win;
1154
1155     if ((win = X11DRV_get_whole_window( hwnd )) && win != DefaultRootWindow(display))
1156         sync_window_text( display, win, text );
1157 }
1158
1159
1160 /***********************************************************************
1161  *              DestroyWindow   (X11DRV.@)
1162  */
1163 void X11DRV_DestroyWindow( HWND hwnd )
1164 {
1165     struct x11drv_thread_data *thread_data = x11drv_thread_data();
1166     Display *display = thread_data->display;
1167     struct x11drv_win_data *data;
1168
1169     if (!(data = X11DRV_get_win_data( hwnd ))) return;
1170
1171     if (data->pixmap)
1172     {
1173         destroy_glxpixmap(display, data->gl_drawable);
1174         wine_tsx11_lock();
1175         XFreePixmap(display, data->pixmap);
1176         wine_tsx11_unlock();
1177     }
1178     else if (data->gl_drawable)
1179     {
1180         wine_tsx11_lock();
1181         XDestroyWindow(display, data->gl_drawable);
1182         wine_tsx11_unlock();
1183     }
1184
1185     destroy_whole_window( display, data, FALSE );
1186     destroy_icon_window( display, data );
1187
1188     if (data->colormap)
1189     {
1190         wine_tsx11_lock();
1191         XFreeColormap( display, data->colormap );
1192         wine_tsx11_unlock();
1193     }
1194
1195     if (thread_data->last_focus == hwnd) thread_data->last_focus = 0;
1196     if (data->hWMIconBitmap) DeleteObject( data->hWMIconBitmap );
1197     if (data->hWMIconMask) DeleteObject( data->hWMIconMask);
1198     wine_tsx11_lock();
1199     XDeleteContext( display, (XID)hwnd, win_data_context );
1200     wine_tsx11_unlock();
1201     HeapFree( GetProcessHeap(), 0, data );
1202 }
1203
1204
1205 /***********************************************************************
1206  *              X11DRV_DestroyNotify
1207  */
1208 void X11DRV_DestroyNotify( HWND hwnd, XEvent *event )
1209 {
1210     Display *display = event->xdestroywindow.display;
1211     struct x11drv_win_data *data;
1212
1213     if (!(data = X11DRV_get_win_data( hwnd ))) return;
1214
1215     FIXME( "window %p/%lx destroyed from the outside\n", hwnd, data->whole_window );
1216     destroy_whole_window( display, data, TRUE );
1217 }
1218
1219
1220 static struct x11drv_win_data *alloc_win_data( Display *display, HWND hwnd )
1221 {
1222     struct x11drv_win_data *data;
1223
1224     if ((data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*data))))
1225     {
1226         data->hwnd = hwnd;
1227         wine_tsx11_lock();
1228         if (!winContext) winContext = XUniqueContext();
1229         if (!win_data_context) win_data_context = XUniqueContext();
1230         XSaveContext( display, (XID)hwnd, win_data_context, (char *)data );
1231         wine_tsx11_unlock();
1232     }
1233     return data;
1234 }
1235
1236
1237 /* initialize the desktop window id in the desktop manager process */
1238 static struct x11drv_win_data *create_desktop_win_data( Display *display, HWND hwnd )
1239 {
1240     struct x11drv_win_data *data;
1241     VisualID visualid;
1242
1243     if (!(data = alloc_win_data( display, hwnd ))) return NULL;
1244     wine_tsx11_lock();
1245     visualid = XVisualIDFromVisual(visual);
1246     wine_tsx11_unlock();
1247     data->whole_window = data->client_window = root_window;
1248     data->managed = TRUE;
1249     SetPropA( data->hwnd, managed_prop, (HANDLE)1 );
1250     SetPropA( data->hwnd, whole_window_prop, (HANDLE)root_window );
1251     SetPropA( data->hwnd, client_window_prop, (HANDLE)root_window );
1252     set_initial_wm_hints( display, data );
1253     return data;
1254 }
1255
1256 /**********************************************************************
1257  *              CreateDesktopWindow   (X11DRV.@)
1258  */
1259 BOOL X11DRV_CreateDesktopWindow( HWND hwnd )
1260 {
1261     unsigned int width, height;
1262
1263     /* retrieve the real size of the desktop */
1264     SERVER_START_REQ( get_window_rectangles )
1265     {
1266         req->handle = hwnd;
1267         wine_server_call( req );
1268         width  = reply->window.right - reply->window.left;
1269         height = reply->window.bottom - reply->window.top;
1270     }
1271     SERVER_END_REQ;
1272
1273     if (!width && !height)  /* not initialized yet */
1274     {
1275         SERVER_START_REQ( set_window_pos )
1276         {
1277             req->handle        = hwnd;
1278             req->previous      = 0;
1279             req->flags         = SWP_NOZORDER;
1280             req->window.left   = virtual_screen_rect.left;
1281             req->window.top    = virtual_screen_rect.top;
1282             req->window.right  = virtual_screen_rect.right;
1283             req->window.bottom = virtual_screen_rect.bottom;
1284             req->client        = req->window;
1285             wine_server_call( req );
1286         }
1287         SERVER_END_REQ;
1288     }
1289     else
1290     {
1291         Window win = (Window)GetPropA( hwnd, whole_window_prop );
1292         if (win && win != root_window) X11DRV_init_desktop( win, width, height );
1293     }
1294     return TRUE;
1295 }
1296
1297
1298 /**********************************************************************
1299  *              CreateWindow   (X11DRV.@)
1300  */
1301 BOOL X11DRV_CreateWindow( HWND hwnd )
1302 {
1303     Display *display = thread_display();
1304
1305     if (hwnd == GetDesktopWindow() && root_window != DefaultRootWindow( display ))
1306     {
1307         /* the desktop win data can't be created lazily */
1308         if (!create_desktop_win_data( display, hwnd )) return FALSE;
1309     }
1310     return TRUE;
1311 }
1312
1313
1314 /***********************************************************************
1315  *              X11DRV_get_win_data
1316  *
1317  * Return the X11 data structure associated with a window.
1318  */
1319 struct x11drv_win_data *X11DRV_get_win_data( HWND hwnd )
1320 {
1321     char *data;
1322
1323     if (!hwnd || XFindContext( thread_display(), (XID)hwnd, win_data_context, &data )) data = NULL;
1324     return (struct x11drv_win_data *)data;
1325 }
1326
1327
1328 /***********************************************************************
1329  *              X11DRV_create_win_data
1330  *
1331  * Create an X11 data window structure for an existing window.
1332  */
1333 struct x11drv_win_data *X11DRV_create_win_data( HWND hwnd )
1334 {
1335     Display *display = thread_display();
1336     struct x11drv_win_data *data;
1337     HWND parent;
1338
1339     if (!(parent = GetAncestor( hwnd, GA_PARENT ))) return NULL;  /* desktop */
1340     if (!(data = alloc_win_data( display, hwnd ))) return NULL;
1341
1342     GetWindowRect( hwnd, &data->window_rect );
1343     MapWindowPoints( 0, parent, (POINT *)&data->window_rect, 2 );
1344     data->whole_rect = data->window_rect;
1345     GetClientRect( hwnd, &data->client_rect );
1346     MapWindowPoints( hwnd, parent, (POINT *)&data->client_rect, 2 );
1347
1348     if (parent == GetDesktopWindow())
1349     {
1350         if (!create_whole_window( display, data ))
1351         {
1352             HeapFree( GetProcessHeap(), 0, data );
1353             return NULL;
1354         }
1355         TRACE( "win %p/%lx/%lx window %s whole %s client %s\n",
1356                hwnd, data->whole_window, data->client_window, wine_dbgstr_rect( &data->window_rect ),
1357                wine_dbgstr_rect( &data->whole_rect ), wine_dbgstr_rect( &data->client_rect ));
1358     }
1359     return data;
1360 }
1361
1362
1363 /***********************************************************************
1364  *              X11DRV_get_whole_window
1365  *
1366  * Return the X window associated with the full area of a window
1367  */
1368 Window X11DRV_get_whole_window( HWND hwnd )
1369 {
1370     struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
1371
1372     if (!data)
1373     {
1374         if (hwnd == GetDesktopWindow()) return root_window;
1375         return (Window)GetPropA( hwnd, whole_window_prop );
1376     }
1377     return data->whole_window;
1378 }
1379
1380
1381 /***********************************************************************
1382  *              X11DRV_get_client_window
1383  *
1384  * Return the X window associated with the client area of a window
1385  */
1386 Window X11DRV_get_client_window( HWND hwnd )
1387 {
1388     struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
1389
1390     if (!data)
1391     {
1392         if (hwnd == GetDesktopWindow()) return root_window;
1393         return (Window)GetPropA( hwnd, client_window_prop );
1394     }
1395     return data->client_window;
1396 }
1397
1398
1399 /***********************************************************************
1400  *              X11DRV_get_ic
1401  *
1402  * Return the X input context associated with a window
1403  */
1404 XIC X11DRV_get_ic( HWND hwnd )
1405 {
1406     struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
1407
1408     if (!data) return 0;
1409     return data->xic;
1410 }
1411
1412
1413 /***********************************************************************
1414  *              X11DRV_GetDC   (X11DRV.@)
1415  */
1416 void X11DRV_GetDC( HDC hdc, HWND hwnd, HWND top, const RECT *win_rect,
1417                    const RECT *top_rect, DWORD flags )
1418 {
1419     struct x11drv_escape_set_drawable escape;
1420     struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
1421
1422     escape.code        = X11DRV_SET_DRAWABLE;
1423     escape.mode        = IncludeInferiors;
1424     escape.fbconfig_id = 0;
1425     escape.gl_drawable = 0;
1426     escape.pixmap      = 0;
1427
1428     if (top == hwnd && data && IsIconic( hwnd ) && data->icon_window)
1429     {
1430         escape.drawable = data->icon_window;
1431     }
1432     else if (top == hwnd && (flags & DCX_WINDOW))
1433     {
1434         escape.drawable = data ? data->whole_window : X11DRV_get_whole_window( hwnd );
1435     }
1436     else
1437     {
1438         escape.drawable    = X11DRV_get_client_window( top );
1439         escape.fbconfig_id = data ? data->fbconfig_id : (XID)GetPropA( hwnd, fbconfig_id_prop );
1440         escape.gl_drawable = data ? data->gl_drawable : (Drawable)GetPropA( hwnd, gl_drawable_prop );
1441         escape.pixmap      = data ? data->pixmap : (Pixmap)GetPropA( hwnd, pixmap_prop );
1442         if (flags & DCX_CLIPCHILDREN) escape.mode = ClipByChildren;
1443     }
1444
1445     escape.dc_rect.left         = win_rect->left - top_rect->left;
1446     escape.dc_rect.top          = win_rect->top - top_rect->top;
1447     escape.dc_rect.right        = win_rect->right - top_rect->left;
1448     escape.dc_rect.bottom       = win_rect->bottom - top_rect->top;
1449     escape.drawable_rect.left   = top_rect->left;
1450     escape.drawable_rect.top    = top_rect->top;
1451     escape.drawable_rect.right  = top_rect->right;
1452     escape.drawable_rect.bottom = top_rect->bottom;
1453
1454     ExtEscape( hdc, X11DRV_ESCAPE, sizeof(escape), (LPSTR)&escape, 0, NULL );
1455 }
1456
1457
1458 /***********************************************************************
1459  *              X11DRV_ReleaseDC  (X11DRV.@)
1460  */
1461 void X11DRV_ReleaseDC( HWND hwnd, HDC hdc )
1462 {
1463     struct x11drv_escape_set_drawable escape;
1464
1465     escape.code = X11DRV_SET_DRAWABLE;
1466     escape.drawable = root_window;
1467     escape.mode = IncludeInferiors;
1468     escape.drawable_rect = virtual_screen_rect;
1469     SetRect( &escape.dc_rect, 0, 0, virtual_screen_rect.right - virtual_screen_rect.left,
1470              virtual_screen_rect.bottom - virtual_screen_rect.top );
1471     escape.fbconfig_id = 0;
1472     escape.gl_drawable = 0;
1473     escape.pixmap = 0;
1474     ExtEscape( hdc, X11DRV_ESCAPE, sizeof(escape), (LPSTR)&escape, 0, NULL );
1475 }
1476
1477
1478 /***********************************************************************
1479  *              SetCapture  (X11DRV.@)
1480  */
1481 void X11DRV_SetCapture( HWND hwnd, UINT flags )
1482 {
1483     struct x11drv_thread_data *thread_data = x11drv_thread_data();
1484
1485     if (!(flags & (GUI_INMOVESIZE | GUI_INMENUMODE))) return;
1486
1487     if (hwnd)
1488     {
1489         Window grab_win = X11DRV_get_client_window( GetAncestor( hwnd, GA_ROOT ) );
1490
1491         if (!grab_win) return;
1492         wine_tsx11_lock();
1493         XFlush( gdi_display );
1494         XGrabPointer( thread_data->display, grab_win, False,
1495                       PointerMotionMask | ButtonPressMask | ButtonReleaseMask,
1496                       GrabModeAsync, GrabModeAsync, None, None, CurrentTime );
1497         wine_tsx11_unlock();
1498         thread_data->grab_window = grab_win;
1499     }
1500     else  /* release capture */
1501     {
1502         wine_tsx11_lock();
1503         XFlush( gdi_display );
1504         XUngrabPointer( thread_data->display, CurrentTime );
1505         wine_tsx11_unlock();
1506         thread_data->grab_window = None;
1507     }
1508 }
1509
1510
1511 /*****************************************************************
1512  *              SetParent   (X11DRV.@)
1513  */
1514 void X11DRV_SetParent( HWND hwnd, HWND parent, HWND old_parent )
1515 {
1516     Display *display = thread_display();
1517     struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
1518
1519     if (!data) return;
1520     if (parent == old_parent) return;
1521
1522     if (parent != GetDesktopWindow()) /* a child window */
1523     {
1524         if (old_parent == GetDesktopWindow())
1525         {
1526             /* destroy the old X windows */
1527             destroy_whole_window( display, data, FALSE );
1528             destroy_icon_window( display, data );
1529             if (data->managed)
1530             {
1531                 data->managed = FALSE;
1532                 RemovePropA( data->hwnd, managed_prop );
1533             }
1534         }
1535     }
1536     else  /* new top level window */
1537     {
1538         /* FIXME: we ignore errors since we can't really recover anyway */
1539         create_whole_window( display, data );
1540     }
1541 }
1542
1543
1544 /*****************************************************************
1545  *              SetFocus   (X11DRV.@)
1546  *
1547  * Set the X focus.
1548  * Explicit colormap management seems to work only with OLVWM.
1549  */
1550 void X11DRV_SetFocus( HWND hwnd )
1551 {
1552     Display *display = thread_display();
1553     struct x11drv_win_data *data;
1554     XWindowChanges changes;
1555
1556     /* If setting the focus to 0, uninstall the colormap */
1557     if (!hwnd && root_window == DefaultRootWindow(display))
1558     {
1559         wine_tsx11_lock();
1560         if (X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_PRIVATE)
1561             XUninstallColormap( display, X11DRV_PALETTE_PaletteXColormap );
1562         wine_tsx11_unlock();
1563         return;
1564     }
1565
1566     hwnd = GetAncestor( hwnd, GA_ROOT );
1567
1568     if (!(data = X11DRV_get_win_data( hwnd ))) return;
1569     if (data->managed || !data->whole_window) return;
1570
1571     /* Set X focus and install colormap */
1572     wine_tsx11_lock();
1573     changes.stack_mode = Above;
1574     XConfigureWindow( display, data->whole_window, CWStackMode, &changes );
1575     if (root_window == DefaultRootWindow(display))
1576     {
1577         /* we must not use CurrentTime (ICCCM), so try to use last message time instead */
1578         /* FIXME: this is not entirely correct */
1579         XSetInputFocus( display, data->whole_window, RevertToParent,
1580                         /* CurrentTime */
1581                         GetMessageTime() - EVENT_x11_time_to_win32_time(0));
1582         if (X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_PRIVATE)
1583             XInstallColormap( display, X11DRV_PALETTE_PaletteXColormap );
1584     }
1585     wine_tsx11_unlock();
1586 }
1587
1588
1589 /**********************************************************************
1590  *              SetWindowIcon (X11DRV.@)
1591  *
1592  * hIcon or hIconSm has changed (or is being initialised for the
1593  * first time). Complete the X11 driver-specific initialisation
1594  * and set the window hints.
1595  *
1596  * This is not entirely correct, may need to create
1597  * an icon window and set the pixmap as a background
1598  */
1599 void X11DRV_SetWindowIcon( HWND hwnd, UINT type, HICON icon )
1600 {
1601     Display *display = thread_display();
1602     struct x11drv_win_data *data;
1603
1604     if (type != ICON_BIG) return;  /* nothing to do here */
1605
1606     if (!(data = X11DRV_get_win_data( hwnd ))) return;
1607     if (!data->whole_window) return;
1608     if (!data->managed) return;
1609
1610     if (data->wm_hints)
1611     {
1612         set_icon_hints( display, data, icon );
1613         wine_tsx11_lock();
1614         XSetWMHints( display, data->whole_window, data->wm_hints );
1615         wine_tsx11_unlock();
1616     }
1617 }
1618
1619
1620 /***********************************************************************
1621  *              SetWindowRgn  (X11DRV.@)
1622  *
1623  * Assign specified region to window (for non-rectangular windows)
1624  */
1625 int X11DRV_SetWindowRgn( HWND hwnd, HRGN hrgn, BOOL redraw )
1626 {
1627     struct x11drv_win_data *data;
1628
1629     if ((data = X11DRV_get_win_data( hwnd )))
1630     {
1631         sync_window_region( thread_display(), data, hrgn );
1632     }
1633     else if (GetWindowThreadProcessId( hwnd, NULL ) != GetCurrentThreadId())
1634     {
1635         FIXME( "not supported on other thread window %p\n", hwnd );
1636         SetLastError( ERROR_INVALID_WINDOW_HANDLE );
1637         return FALSE;
1638     }
1639
1640     return TRUE;
1641 }