user32: Handle VK_RETURN WM_KEYDOWN events better in edit controls.
[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 #define _NET_WM_MOVERESIZE_SIZE_TOPLEFT      0
54 #define _NET_WM_MOVERESIZE_SIZE_TOP          1
55 #define _NET_WM_MOVERESIZE_SIZE_TOPRIGHT     2
56 #define _NET_WM_MOVERESIZE_SIZE_RIGHT        3
57 #define _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT  4
58 #define _NET_WM_MOVERESIZE_SIZE_BOTTOM       5
59 #define _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT   6
60 #define _NET_WM_MOVERESIZE_SIZE_LEFT         7
61 #define _NET_WM_MOVERESIZE_MOVE              8
62 #define _NET_WM_MOVERESIZE_SIZE_KEYBOARD     9
63 #define _NET_WM_MOVERESIZE_MOVE_KEYBOARD    10
64
65 #define _NET_WM_STATE_REMOVE  0
66 #define _NET_WM_STATE_ADD     1
67 #define _NET_WM_STATE_TOGGLE  2
68
69 #define SWP_AGG_NOPOSCHANGE (SWP_NOSIZE | SWP_NOMOVE | SWP_NOCLIENTSIZE | SWP_NOCLIENTMOVE | SWP_NOZORDER)
70
71 /* X context to associate a hwnd to an X window */
72 XContext winContext = 0;
73
74 /* X context to associate a struct x11drv_win_data to an hwnd */
75 static XContext win_data_context;
76
77 static const char whole_window_prop[] = "__wine_x11_whole_window";
78 static const char client_window_prop[]= "__wine_x11_client_window";
79 static const char icon_window_prop[]  = "__wine_x11_icon_window";
80 static const char fbconfig_id_prop[]  = "__wine_x11_fbconfig_id";
81 static const char gl_drawable_prop[]  = "__wine_x11_gl_drawable";
82 static const char pixmap_prop[]       = "__wine_x11_pixmap";
83 static const char managed_prop[]      = "__wine_x11_managed";
84
85 /***********************************************************************
86  *              is_window_managed
87  *
88  * Check if a given window should be managed
89  */
90 static BOOL is_window_managed( HWND hwnd, UINT swp_flags, const RECT *window_rect )
91 {
92     DWORD style, ex_style;
93
94     if (!managed_mode) return FALSE;
95
96     /* child windows are not managed */
97     style = GetWindowLongW( hwnd, GWL_STYLE );
98     if ((style & (WS_CHILD|WS_POPUP)) == WS_CHILD) return FALSE;
99     /* activated windows are managed */
100     if (!(swp_flags & (SWP_NOACTIVATE|SWP_HIDEWINDOW))) return TRUE;
101     if (hwnd == GetActiveWindow()) return TRUE;
102     /* windows with caption are managed */
103     if ((style & WS_CAPTION) == WS_CAPTION) return TRUE;
104     /* tool windows are not managed  */
105     ex_style = GetWindowLongW( hwnd, GWL_EXSTYLE );
106     if (ex_style & WS_EX_TOOLWINDOW) return FALSE;
107     /* windows with thick frame are managed */
108     if (style & WS_THICKFRAME) return TRUE;
109     /* application windows are managed */
110     if (ex_style & WS_EX_APPWINDOW) return TRUE;
111     if (style & WS_POPUP)
112     {
113         /* popup with sysmenu == caption are managed */
114         if (style & WS_SYSMENU) return TRUE;
115         /* full-screen popup windows are managed */
116         if (window_rect->left <= 0 && window_rect->right >= screen_width &&
117             window_rect->top <= 0 && window_rect->bottom >= screen_height)
118             return TRUE;
119     }
120     /* default: not managed */
121     return FALSE;
122 }
123
124
125 /***********************************************************************
126  *              X11DRV_is_window_rect_mapped
127  *
128  * Check if the X whole window should be mapped based on its rectangle
129  */
130 static BOOL is_window_rect_mapped( const RECT *rect )
131 {
132     /* don't map if rect is off-screen */
133     if (rect->left >= virtual_screen_rect.right ||
134         rect->top >= virtual_screen_rect.bottom ||
135         rect->right <= virtual_screen_rect.left ||
136         rect->bottom <= virtual_screen_rect.top)
137         return FALSE;
138
139     return TRUE;
140 }
141
142
143 /***********************************************************************
144  *              is_window_resizable
145  *
146  * Check if window should be made resizable by the window manager
147  */
148 static inline BOOL is_window_resizable( struct x11drv_win_data *data, DWORD style )
149 {
150     if (style & WS_THICKFRAME) return TRUE;
151     /* Metacity needs the window to be resizable to make it fullscreen */
152     return (data->whole_rect.left <= 0 && data->whole_rect.right >= screen_width &&
153             data->whole_rect.top <= 0 && data->whole_rect.bottom >= screen_height);
154 }
155
156
157 /***********************************************************************
158  *              get_mwm_decorations
159  */
160 static unsigned long get_mwm_decorations( struct x11drv_win_data *data,
161                                           DWORD style, DWORD ex_style )
162 {
163     unsigned long ret = 0;
164
165     if (!decorated_mode) return 0;
166
167     if (IsRectEmpty( &data->window_rect )) return 0;
168
169     if (ex_style & WS_EX_TOOLWINDOW) return 0;
170
171     if ((style & WS_CAPTION) == WS_CAPTION)
172     {
173         ret |= MWM_DECOR_TITLE | MWM_DECOR_BORDER;
174         if (style & WS_SYSMENU) ret |= MWM_DECOR_MENU;
175         if (style & WS_MINIMIZEBOX) ret |= MWM_DECOR_MINIMIZE;
176         if (style & WS_MAXIMIZEBOX) ret |= MWM_DECOR_MAXIMIZE;
177     }
178     if (ex_style & WS_EX_DLGMODALFRAME) ret |= MWM_DECOR_BORDER;
179     else if (style & WS_THICKFRAME) ret |= MWM_DECOR_BORDER | MWM_DECOR_RESIZEH;
180     else if ((style & (WS_DLGFRAME|WS_BORDER)) == WS_DLGFRAME) ret |= MWM_DECOR_BORDER;
181     return ret;
182 }
183
184
185 /***********************************************************************
186  *              get_x11_rect_offset
187  *
188  * Helper for X11DRV_window_to_X_rect and X11DRV_X_to_window_rect.
189  */
190 static void get_x11_rect_offset( struct x11drv_win_data *data, RECT *rect )
191 {
192     DWORD style, ex_style, style_mask = 0, ex_style_mask = 0;
193     unsigned long decor;
194
195     rect->top = rect->bottom = rect->left = rect->right = 0;
196
197     style = GetWindowLongW( data->hwnd, GWL_STYLE );
198     ex_style = GetWindowLongW( data->hwnd, GWL_EXSTYLE );
199     decor = get_mwm_decorations( data, style, ex_style );
200
201     if (decor & MWM_DECOR_TITLE) style_mask |= WS_CAPTION;
202     if (decor & MWM_DECOR_BORDER)
203     {
204         style_mask |= WS_DLGFRAME | WS_THICKFRAME;
205         ex_style_mask |= WS_EX_DLGMODALFRAME;
206     }
207
208     AdjustWindowRectEx( rect, style & style_mask, FALSE, ex_style & ex_style_mask );
209 }
210
211
212 /***********************************************************************
213  *              get_window_attributes
214  *
215  * Fill the window attributes structure for an X window.
216  */
217 static int get_window_attributes( Display *display, struct x11drv_win_data *data,
218                                   XSetWindowAttributes *attr )
219 {
220     attr->override_redirect = !data->managed;
221     attr->colormap          = X11DRV_PALETTE_PaletteXColormap;
222     attr->save_under        = ((GetClassLongW( data->hwnd, GCL_STYLE ) & CS_SAVEBITS) != 0);
223     attr->cursor            = x11drv_thread_data()->cursor;
224     attr->bit_gravity       = NorthWestGravity;
225     attr->win_gravity       = StaticGravity;
226     attr->backing_store     = NotUseful;
227     attr->event_mask        = (ExposureMask | PointerMotionMask |
228                                ButtonPressMask | ButtonReleaseMask | EnterWindowMask |
229                                KeyPressMask | KeyReleaseMask | FocusChangeMask | KeymapStateMask);
230     if (data->managed) attr->event_mask |= StructureNotifyMask | PropertyChangeMask;
231
232     return (CWOverrideRedirect | CWSaveUnder | CWColormap | CWCursor |
233             CWEventMask | CWBitGravity | CWBackingStore);
234 }
235
236
237 /***********************************************************************
238  *              create_client_window
239  */
240 static Window create_client_window( Display *display, struct x11drv_win_data *data, XVisualInfo *vis )
241 {
242     int cx, cy, mask;
243     XSetWindowAttributes attr;
244     Window client;
245
246     attr.bit_gravity = NorthWestGravity;
247     attr.win_gravity = NorthWestGravity;
248     attr.backing_store = NotUseful;
249     attr.event_mask = (ExposureMask | PointerMotionMask |
250                        ButtonPressMask | ButtonReleaseMask | EnterWindowMask);
251     mask = CWEventMask | CWBitGravity | CWWinGravity | CWBackingStore;
252
253     if ((cx = data->client_rect.right - data->client_rect.left) <= 0) cx = 1;
254     if ((cy = data->client_rect.bottom - data->client_rect.top) <= 0) cy = 1;
255
256     wine_tsx11_lock();
257
258     if (vis)
259     {
260         attr.colormap = XCreateColormap( display, root_window, vis->visual,
261                                          (vis->class == PseudoColor || vis->class == GrayScale ||
262                                           vis->class == DirectColor) ? AllocAll : AllocNone );
263         mask |= CWColormap;
264     }
265
266     client = XCreateWindow( display, data->whole_window,
267                             data->client_rect.left - data->whole_rect.left,
268                             data->client_rect.top - data->whole_rect.top,
269                             cx, cy, 0, screen_depth, InputOutput,
270                             vis ? vis->visual : visual, mask, &attr );
271     if (!client)
272     {
273         wine_tsx11_unlock();
274         return 0;
275     }
276
277     if (data->client_window)
278     {
279         struct x11drv_thread_data *thread_data = x11drv_thread_data();
280         if (thread_data->cursor_window == data->client_window) thread_data->cursor_window = None;
281         XDeleteContext( display, data->client_window, winContext );
282         XDestroyWindow( display, data->client_window );
283     }
284     data->client_window = client;
285
286     if (data->colormap) XFreeColormap( display, data->colormap );
287     data->colormap = vis ? attr.colormap : 0;
288
289     XMapWindow( display, data->client_window );
290     XSaveContext( display, data->client_window, winContext, (char *)data->hwnd );
291     wine_tsx11_unlock();
292
293     SetPropA( data->hwnd, client_window_prop, (HANDLE)data->client_window );
294     return data->client_window;
295 }
296
297
298 /***********************************************************************
299  *              sync_window_style
300  *
301  * Change the X window attributes when the window style has changed.
302  */
303 static void sync_window_style( Display *display, struct x11drv_win_data *data )
304 {
305     if (data->whole_window != root_window)
306     {
307         XSetWindowAttributes attr;
308         int mask = get_window_attributes( display, data, &attr );
309
310         wine_tsx11_lock();
311         XChangeWindowAttributes( display, data->whole_window, mask, &attr );
312         wine_tsx11_unlock();
313     }
314 }
315
316
317 /***********************************************************************
318  *              sync_window_region
319  *
320  * Update the X11 window region.
321  */
322 static void sync_window_region( Display *display, struct x11drv_win_data *data, HRGN hrgn )
323 {
324 #ifdef HAVE_LIBXSHAPE
325     if (!data->whole_window) return;
326
327     if (!hrgn)
328     {
329         wine_tsx11_lock();
330         XShapeCombineMask( display, data->whole_window, ShapeBounding, 0, 0, None, ShapeSet );
331         wine_tsx11_unlock();
332     }
333     else
334     {
335         RGNDATA *pRegionData = X11DRV_GetRegionData( hrgn, 0 );
336         if (pRegionData)
337         {
338             wine_tsx11_lock();
339             XShapeCombineRectangles( display, data->whole_window, ShapeBounding,
340                                      data->window_rect.left - data->whole_rect.left,
341                                      data->window_rect.top - data->whole_rect.top,
342                                      (XRectangle *)pRegionData->Buffer,
343                                      pRegionData->rdh.nCount, ShapeSet, YXBanded );
344             wine_tsx11_unlock();
345             HeapFree(GetProcessHeap(), 0, pRegionData);
346         }
347     }
348 #endif  /* HAVE_LIBXSHAPE */
349 }
350
351
352 /***********************************************************************
353  *              sync_window_text
354  */
355 static void sync_window_text( Display *display, Window win, const WCHAR *text )
356 {
357     UINT count;
358     char *buffer, *utf8_buffer;
359     XTextProperty prop;
360
361     /* allocate new buffer for window text */
362     count = WideCharToMultiByte(CP_UNIXCP, 0, text, -1, NULL, 0, NULL, NULL);
363     if (!(buffer = HeapAlloc( GetProcessHeap(), 0, count ))) return;
364     WideCharToMultiByte(CP_UNIXCP, 0, text, -1, buffer, count, NULL, NULL);
365
366     count = WideCharToMultiByte(CP_UTF8, 0, text, strlenW(text), NULL, 0, NULL, NULL);
367     if (!(utf8_buffer = HeapAlloc( GetProcessHeap(), 0, count )))
368     {
369         HeapFree( GetProcessHeap(), 0, buffer );
370         return;
371     }
372     WideCharToMultiByte(CP_UTF8, 0, text, strlenW(text), utf8_buffer, count, NULL, NULL);
373
374     wine_tsx11_lock();
375     if (XmbTextListToTextProperty( display, &buffer, 1, XStdICCTextStyle, &prop ) == Success)
376     {
377         XSetWMName( display, win, &prop );
378         XSetWMIconName( display, win, &prop );
379         XFree( prop.value );
380     }
381     /*
382       Implements a NET_WM UTF-8 title. It should be without a trailing \0,
383       according to the standard
384       ( http://www.pps.jussieu.fr/~jch/software/UTF8_STRING/UTF8_STRING.text ).
385     */
386     XChangeProperty( display, win, x11drv_atom(_NET_WM_NAME), x11drv_atom(UTF8_STRING),
387                      8, PropModeReplace, (unsigned char *) utf8_buffer, count);
388     wine_tsx11_unlock();
389
390     HeapFree( GetProcessHeap(), 0, utf8_buffer );
391     HeapFree( GetProcessHeap(), 0, buffer );
392 }
393
394
395 /***********************************************************************
396  *              X11DRV_set_win_format
397  */
398 BOOL X11DRV_set_win_format( HWND hwnd, XID fbconfig_id )
399 {
400     Display *display = thread_display();
401     struct x11drv_win_data *data;
402     XVisualInfo *vis;
403     int w, h;
404
405     if (!(data = X11DRV_get_win_data(hwnd)) &&
406         !(data = X11DRV_create_win_data(hwnd))) return FALSE;
407
408     if (data->fbconfig_id) return FALSE;  /* can't change it twice */
409
410     wine_tsx11_lock();
411     vis = visual_from_fbconfig_id(fbconfig_id);
412     wine_tsx11_unlock();
413     if (!vis) return FALSE;
414
415     if (data->whole_window)
416     {
417         Window client = data->client_window;
418
419         if (vis->visualid != XVisualIDFromVisual(visual))
420         {
421             client = create_client_window( display, data, vis );
422             TRACE( "re-created client window %lx for %p fbconfig %lx\n", client, data->hwnd, fbconfig_id );
423         }
424         wine_tsx11_lock();
425         XFree(vis);
426         wine_tsx11_unlock();
427         if (client) goto done;
428         return FALSE;
429     }
430
431     w = data->client_rect.right - data->client_rect.left;
432     h = data->client_rect.bottom - data->client_rect.top;
433
434     if(w <= 0) w = 1;
435     if(h <= 0) h = 1;
436
437 #ifdef SONAME_LIBXCOMPOSITE
438     if(usexcomposite)
439     {
440         XSetWindowAttributes attrib;
441         Window parent = X11DRV_get_whole_window( GetAncestor( hwnd, GA_ROOT ));
442
443         if (!parent) parent = root_window;
444         wine_tsx11_lock();
445         data->colormap = XCreateColormap(display, parent, vis->visual,
446                                          (vis->class == PseudoColor ||
447                                           vis->class == GrayScale ||
448                                           vis->class == DirectColor) ?
449                                          AllocAll : AllocNone);
450         attrib.override_redirect = True;
451         attrib.colormap = data->colormap;
452         XInstallColormap(gdi_display, attrib.colormap);
453
454         data->gl_drawable = XCreateWindow(display, parent, -w, 0, w, h, 0,
455                                           vis->depth, InputOutput, vis->visual,
456                                           CWColormap | CWOverrideRedirect,
457                                           &attrib);
458         if(data->gl_drawable)
459         {
460             pXCompositeRedirectWindow(display, data->gl_drawable,
461                                       CompositeRedirectManual);
462             XMapWindow(display, data->gl_drawable);
463         }
464         XFree(vis);
465         wine_tsx11_unlock();
466     }
467     else
468 #endif
469     {
470         WARN("XComposite is not available, using GLXPixmap hack\n");
471
472         wine_tsx11_lock();
473         data->pixmap = XCreatePixmap(display, root_window, w, h, vis->depth);
474         if(!data->pixmap)
475         {
476             XFree(vis);
477             wine_tsx11_unlock();
478             return FALSE;
479         }
480
481         data->gl_drawable = create_glxpixmap(display, vis, data->pixmap);
482         if(!data->gl_drawable)
483         {
484             XFreePixmap(display, data->pixmap);
485             data->pixmap = 0;
486         }
487         XFree(vis);
488         wine_tsx11_unlock();
489         if (data->pixmap) SetPropA(hwnd, pixmap_prop, (HANDLE)data->pixmap);
490     }
491
492     if (!data->gl_drawable) return FALSE;
493
494     TRACE("Created GL drawable 0x%lx, using FBConfigID 0x%lx\n",
495           data->gl_drawable, fbconfig_id);
496     SetPropA(hwnd, gl_drawable_prop, (HANDLE)data->gl_drawable);
497
498 done:
499     data->fbconfig_id = fbconfig_id;
500     SetPropA(hwnd, fbconfig_id_prop, (HANDLE)data->fbconfig_id);
501     wine_tsx11_lock();
502     XFlush( display );
503     wine_tsx11_unlock();
504     /* force DCE invalidation */
505     SetWindowPos( hwnd, 0, 0, 0, 0, 0,
506                   SWP_NOACTIVATE | SWP_NOZORDER | SWP_NOSIZE | SWP_NOMOVE |
507                   SWP_NOREDRAW | SWP_NOSENDCHANGING | SWP_STATECHANGED);
508     return TRUE;
509 }
510
511 /***********************************************************************
512  *              sync_gl_drawable
513  */
514 static void sync_gl_drawable(Display *display, struct x11drv_win_data *data)
515 {
516     int w = data->client_rect.right - data->client_rect.left;
517     int h = data->client_rect.bottom - data->client_rect.top;
518     XVisualInfo *vis;
519     Drawable glxp;
520     Pixmap pix;
521
522     if (w <= 0) w = 1;
523     if (h <= 0) h = 1;
524
525     TRACE("Resizing GL drawable 0x%lx to %dx%d\n", data->gl_drawable, w, h);
526 #ifdef SONAME_LIBXCOMPOSITE
527     if(usexcomposite)
528     {
529         wine_tsx11_lock();
530         XMoveResizeWindow(display, data->gl_drawable, -w, 0, w, h);
531         wine_tsx11_unlock();
532         return;
533     }
534 #endif
535
536     wine_tsx11_lock();
537
538     vis = visual_from_fbconfig_id(data->fbconfig_id);
539     if(!vis)
540     {
541         wine_tsx11_unlock();
542         return;
543     }
544
545     pix = XCreatePixmap(display, root_window, w, h, vis->depth);
546     if(!pix)
547     {
548         ERR("Failed to create pixmap for offscreen rendering\n");
549         XFree(vis);
550         wine_tsx11_unlock();
551         return;
552     }
553
554     glxp = create_glxpixmap(display, vis, pix);
555     if(!glxp)
556     {
557         ERR("Failed to create drawable for offscreen rendering\n");
558         XFreePixmap(display, pix);
559         XFree(vis);
560         wine_tsx11_unlock();
561         return;
562     }
563
564     XFree(vis);
565
566     mark_drawable_dirty(data->gl_drawable, glxp);
567
568     XFreePixmap(display, data->pixmap);
569     destroy_glxpixmap(display, data->gl_drawable);
570     TRACE( "Recreated GL drawable %lx to replace %lx\n", glxp, data->gl_drawable );
571
572     data->pixmap = pix;
573     data->gl_drawable = glxp;
574
575     XFlush( display );
576     wine_tsx11_unlock();
577
578     SetPropA(data->hwnd, gl_drawable_prop, (HANDLE)data->gl_drawable);
579     SetPropA(data->hwnd, pixmap_prop, (HANDLE)data->pixmap);
580 }
581
582
583 /***********************************************************************
584  *              get_window_changes
585  *
586  * fill the window changes structure
587  */
588 static int get_window_changes( XWindowChanges *changes, const RECT *old, const RECT *new )
589 {
590     int mask = 0;
591
592     if (old->right - old->left != new->right - new->left )
593     {
594         if ((changes->width = new->right - new->left) <= 0) changes->width = 1;
595         mask |= CWWidth;
596     }
597     if (old->bottom - old->top != new->bottom - new->top)
598     {
599         if ((changes->height = new->bottom - new->top) <= 0) changes->height = 1;
600         mask |= CWHeight;
601     }
602     if (old->left != new->left)
603     {
604         changes->x = new->left;
605         mask |= CWX;
606     }
607     if (old->top != new->top)
608     {
609         changes->y = new->top;
610         mask |= CWY;
611     }
612     return mask;
613 }
614
615
616 /***********************************************************************
617  *              create_icon_window
618  */
619 static Window create_icon_window( Display *display, struct x11drv_win_data *data )
620 {
621     XSetWindowAttributes attr;
622
623     attr.event_mask = (ExposureMask | KeyPressMask | KeyReleaseMask | PointerMotionMask |
624                        ButtonPressMask | ButtonReleaseMask | EnterWindowMask);
625     attr.bit_gravity = NorthWestGravity;
626     attr.backing_store = NotUseful/*WhenMapped*/;
627     attr.colormap      = X11DRV_PALETTE_PaletteXColormap; /* Needed due to our visual */
628
629     wine_tsx11_lock();
630     data->icon_window = XCreateWindow( display, root_window, 0, 0,
631                                        GetSystemMetrics( SM_CXICON ),
632                                        GetSystemMetrics( SM_CYICON ),
633                                        0, screen_depth,
634                                        InputOutput, visual,
635                                        CWEventMask | CWBitGravity | CWBackingStore | CWColormap, &attr );
636     XSaveContext( display, data->icon_window, winContext, (char *)data->hwnd );
637     XFlush( display );  /* make sure the window exists before we start painting to it */
638     wine_tsx11_unlock();
639
640     TRACE( "created %lx\n", data->icon_window );
641     SetPropA( data->hwnd, icon_window_prop, (HANDLE)data->icon_window );
642     return data->icon_window;
643 }
644
645
646
647 /***********************************************************************
648  *              destroy_icon_window
649  */
650 static void destroy_icon_window( Display *display, struct x11drv_win_data *data )
651 {
652     if (!data->icon_window) return;
653     if (x11drv_thread_data()->cursor_window == data->icon_window)
654         x11drv_thread_data()->cursor_window = None;
655     wine_tsx11_lock();
656     XDeleteContext( display, data->icon_window, winContext );
657     XDestroyWindow( display, data->icon_window );
658     data->icon_window = 0;
659     wine_tsx11_unlock();
660     RemovePropA( data->hwnd, icon_window_prop );
661 }
662
663
664 /***********************************************************************
665  *              set_icon_hints
666  *
667  * Set the icon wm hints
668  */
669 static void set_icon_hints( Display *display, struct x11drv_win_data *data, HICON hIcon )
670 {
671     XWMHints *hints = data->wm_hints;
672
673     if (data->hWMIconBitmap) DeleteObject( data->hWMIconBitmap );
674     if (data->hWMIconMask) DeleteObject( data->hWMIconMask);
675     data->hWMIconBitmap = 0;
676     data->hWMIconMask = 0;
677
678     if (!hIcon)
679     {
680         if (!data->icon_window) create_icon_window( display, data );
681         hints->icon_window = data->icon_window;
682         hints->flags = (hints->flags & ~(IconPixmapHint | IconMaskHint)) | IconWindowHint;
683     }
684     else
685     {
686         HBITMAP hbmOrig;
687         RECT rcMask;
688         BITMAP bmMask;
689         ICONINFO ii;
690         HDC hDC;
691
692         GetIconInfo(hIcon, &ii);
693
694         GetObjectA(ii.hbmMask, sizeof(bmMask), &bmMask);
695         rcMask.top    = 0;
696         rcMask.left   = 0;
697         rcMask.right  = bmMask.bmWidth;
698         rcMask.bottom = bmMask.bmHeight;
699
700         hDC = CreateCompatibleDC(0);
701         hbmOrig = SelectObject(hDC, ii.hbmMask);
702         InvertRect(hDC, &rcMask);
703         SelectObject(hDC, ii.hbmColor);  /* force the color bitmap to x11drv mode too */
704         SelectObject(hDC, hbmOrig);
705         DeleteDC(hDC);
706
707         data->hWMIconBitmap = ii.hbmColor;
708         data->hWMIconMask = ii.hbmMask;
709
710         hints->icon_pixmap = X11DRV_get_pixmap(data->hWMIconBitmap);
711         hints->icon_mask = X11DRV_get_pixmap(data->hWMIconMask);
712         destroy_icon_window( display, data );
713         hints->flags = (hints->flags & ~IconWindowHint) | IconPixmapHint | IconMaskHint;
714     }
715 }
716
717
718 /***********************************************************************
719  *              set_size_hints
720  *
721  * set the window size hints
722  */
723 static void set_size_hints( Display *display, struct x11drv_win_data *data, DWORD style )
724 {
725     XSizeHints* size_hints;
726
727     if (!(size_hints = XAllocSizeHints())) return;
728
729     size_hints->win_gravity = StaticGravity;
730     size_hints->flags |= PWinGravity;
731
732     /* don't update size hints if window is not in normal state */
733     if (!(style & (WS_MINIMIZE | WS_MAXIMIZE)))
734     {
735         if (data->hwnd != GetDesktopWindow())  /* don't force position of desktop */
736         {
737             size_hints->x = data->whole_rect.left;
738             size_hints->y = data->whole_rect.top;
739             size_hints->flags |= PPosition;
740         }
741
742         if (!is_window_resizable( data, style ))
743         {
744             size_hints->max_width = data->whole_rect.right - data->whole_rect.left;
745             size_hints->max_height = data->whole_rect.bottom - data->whole_rect.top;
746             size_hints->min_width = size_hints->max_width;
747             size_hints->min_height = size_hints->max_height;
748             size_hints->flags |= PMinSize | PMaxSize;
749         }
750     }
751     XSetWMNormalHints( display, data->whole_window, size_hints );
752     XFree( size_hints );
753 }
754
755
756 /***********************************************************************
757  *              get_process_name
758  *
759  * get the name of the current process for setting class hints
760  */
761 static char *get_process_name(void)
762 {
763     static char *name;
764
765     if (!name)
766     {
767         WCHAR module[MAX_PATH];
768         DWORD len = GetModuleFileNameW( 0, module, MAX_PATH );
769         if (len && len < MAX_PATH)
770         {
771             char *ptr;
772             WCHAR *p, *appname = module;
773
774             if ((p = strrchrW( appname, '/' ))) appname = p + 1;
775             if ((p = strrchrW( appname, '\\' ))) appname = p + 1;
776             len = WideCharToMultiByte( CP_UNIXCP, 0, appname, -1, NULL, 0, NULL, NULL );
777             if ((ptr = HeapAlloc( GetProcessHeap(), 0, len )))
778             {
779                 WideCharToMultiByte( CP_UNIXCP, 0, appname, -1, ptr, len, NULL, NULL );
780                 name = ptr;
781             }
782         }
783     }
784     return name;
785 }
786
787
788 /***********************************************************************
789  *              set_initial_wm_hints
790  *
791  * Set the window manager hints that don't change over the lifetime of a window.
792  */
793 static void set_initial_wm_hints( Display *display, struct x11drv_win_data *data )
794 {
795     long i;
796     Atom protocols[3];
797     Atom dndVersion = 4;
798     XClassHint *class_hints;
799     char *process_name = get_process_name();
800
801     wine_tsx11_lock();
802
803     /* wm protocols */
804     i = 0;
805     protocols[i++] = x11drv_atom(WM_DELETE_WINDOW);
806     protocols[i++] = x11drv_atom(_NET_WM_PING);
807     if (use_take_focus) protocols[i++] = x11drv_atom(WM_TAKE_FOCUS);
808     XChangeProperty( display, data->whole_window, x11drv_atom(WM_PROTOCOLS),
809                      XA_ATOM, 32, PropModeReplace, (unsigned char *)protocols, i );
810
811     /* class hints */
812     if ((class_hints = XAllocClassHint()))
813     {
814         static char wine[] = "Wine";
815
816         class_hints->res_name = process_name;
817         class_hints->res_class = wine;
818         XSetClassHint( display, data->whole_window, class_hints );
819         XFree( class_hints );
820     }
821
822     /* set the WM_CLIENT_MACHINE and WM_LOCALE_NAME properties */
823     XSetWMProperties(display, data->whole_window, NULL, NULL, NULL, 0, NULL, NULL, NULL);
824     /* set the pid. together, these properties are needed so the window manager can kill us if we freeze */
825     i = getpid();
826     XChangeProperty(display, data->whole_window, x11drv_atom(_NET_WM_PID),
827                     XA_CARDINAL, 32, PropModeReplace, (unsigned char *)&i, 1);
828
829     XChangeProperty( display, data->whole_window, x11drv_atom(XdndAware),
830                      XA_ATOM, 32, PropModeReplace, (unsigned char*)&dndVersion, 1 );
831
832     data->wm_hints = XAllocWMHints();
833     wine_tsx11_unlock();
834
835     if (data->wm_hints)
836     {
837         HICON icon = (HICON)SendMessageW( data->hwnd, WM_GETICON, ICON_BIG, 0 );
838         if (!icon) icon = (HICON)GetClassLongPtrW( data->hwnd, GCLP_HICON );
839         data->wm_hints->flags = 0;
840         set_icon_hints( display, data, icon );
841     }
842 }
843
844
845 /***********************************************************************
846  *              set_wm_hints
847  *
848  * Set the window manager hints for a newly-created window
849  */
850 static void set_wm_hints( Display *display, struct x11drv_win_data *data )
851 {
852     Window group_leader;
853     Atom window_type;
854     MwmHints mwm_hints;
855     DWORD style, ex_style;
856     HWND owner;
857
858     if (data->hwnd == GetDesktopWindow())
859     {
860         /* force some styles for the desktop to get the correct decorations */
861         style = WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU | WS_MINIMIZEBOX;
862         ex_style = WS_EX_APPWINDOW;
863         owner = 0;
864     }
865     else
866     {
867         style = GetWindowLongW( data->hwnd, GWL_STYLE );
868         ex_style = GetWindowLongW( data->hwnd, GWL_EXSTYLE );
869         owner = GetWindow( data->hwnd, GW_OWNER );
870     }
871
872     /* transient for hint */
873     if (owner)
874     {
875         Window owner_win = X11DRV_get_whole_window( owner );
876         wine_tsx11_lock();
877         XSetTransientForHint( display, data->whole_window, owner_win );
878         wine_tsx11_unlock();
879         group_leader = owner_win;
880     }
881     else group_leader = data->whole_window;
882
883     wine_tsx11_lock();
884
885     /* size hints */
886     set_size_hints( display, data, style );
887
888     /* set the WM_WINDOW_TYPE */
889     if (style & WS_THICKFRAME) window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_NORMAL);
890     else if (ex_style & WS_EX_APPWINDOW) window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_NORMAL);
891     else if (style & WS_DLGFRAME) window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_DIALOG);
892     else if (ex_style & WS_EX_DLGMODALFRAME) window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_DIALOG);
893     else if ((style & WS_POPUP) && owner) window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_DIALOG);
894 #if 0  /* many window managers don't handle utility windows very well */
895     else if (ex_style & WS_EX_TOOLWINDOW) window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_UTILITY);
896 #endif
897     else window_type = x11drv_atom(_NET_WM_WINDOW_TYPE_NORMAL);
898
899     XChangeProperty(display, data->whole_window, x11drv_atom(_NET_WM_WINDOW_TYPE),
900                     XA_ATOM, 32, PropModeReplace, (unsigned char*)&window_type, 1);
901
902     mwm_hints.flags = MWM_HINTS_FUNCTIONS | MWM_HINTS_DECORATIONS;
903     mwm_hints.decorations = get_mwm_decorations( data, style, ex_style );
904     mwm_hints.functions = MWM_FUNC_MOVE;
905     if (is_window_resizable( data, style )) mwm_hints.functions |= MWM_FUNC_RESIZE;
906     if (style & WS_MINIMIZEBOX) mwm_hints.functions |= MWM_FUNC_MINIMIZE;
907     if (style & WS_MAXIMIZEBOX) mwm_hints.functions |= MWM_FUNC_MAXIMIZE;
908     if (style & WS_SYSMENU)     mwm_hints.functions |= MWM_FUNC_CLOSE;
909
910     XChangeProperty( display, data->whole_window, x11drv_atom(_MOTIF_WM_HINTS),
911                      x11drv_atom(_MOTIF_WM_HINTS), 32, PropModeReplace,
912                      (unsigned char*)&mwm_hints, sizeof(mwm_hints)/sizeof(long) );
913
914     /* wm hints */
915     if (data->wm_hints)
916     {
917         data->wm_hints->flags |= InputHint | StateHint | WindowGroupHint;
918         data->wm_hints->input = !(style & WS_DISABLED);
919         data->wm_hints->initial_state = (style & WS_MINIMIZE) ? IconicState : NormalState;
920         data->wm_hints->window_group = group_leader;
921         XSetWMHints( display, data->whole_window, data->wm_hints );
922     }
923
924     wine_tsx11_unlock();
925 }
926
927
928 /***********************************************************************
929  *     update_net_wm_states
930  */
931 void update_net_wm_states( Display *display, struct x11drv_win_data *data )
932 {
933     static const unsigned int state_atoms[NB_NET_WM_STATES] =
934     {
935         XATOM__NET_WM_STATE_FULLSCREEN,
936         XATOM__NET_WM_STATE_ABOVE,
937         XATOM__NET_WM_STATE_MAXIMIZED_VERT,
938         XATOM__NET_WM_STATE_SKIP_PAGER,
939         XATOM__NET_WM_STATE_SKIP_TASKBAR
940     };
941
942     DWORD i, style, ex_style, new_state = 0;
943
944     if (!data->managed) return;
945     if (data->whole_window == root_window) return;
946
947     style = GetWindowLongW( data->hwnd, GWL_STYLE );
948     if (data->whole_rect.left <= 0 && data->whole_rect.right >= screen_width &&
949         data->whole_rect.top <= 0 && data->whole_rect.bottom >= screen_height)
950     {
951         if ((style & WS_MAXIMIZE) && (style & WS_CAPTION) == WS_CAPTION)
952             new_state |= (1 << NET_WM_STATE_MAXIMIZED);
953         else if (!(style & WS_MINIMIZE))
954             new_state |= (1 << NET_WM_STATE_FULLSCREEN);
955     }
956     else if (style & WS_MAXIMIZE)
957         new_state |= (1 << NET_WM_STATE_MAXIMIZED);
958
959     ex_style = GetWindowLongW( data->hwnd, GWL_EXSTYLE );
960     if (ex_style & WS_EX_TOPMOST)
961         new_state |= (1 << NET_WM_STATE_ABOVE);
962     if (ex_style & WS_EX_TOOLWINDOW)
963         new_state |= (1 << NET_WM_STATE_SKIP_TASKBAR) | (1 << NET_WM_STATE_SKIP_PAGER);
964     if (!(ex_style & WS_EX_APPWINDOW) && GetWindow( data->hwnd, GW_OWNER ))
965         new_state |= (1 << NET_WM_STATE_SKIP_TASKBAR);
966
967     if (!data->mapped)  /* set the _NET_WM_STATE atom directly */
968     {
969         Atom atoms[NB_NET_WM_STATES+1];
970         DWORD count;
971
972         for (i = count = 0; i < NB_NET_WM_STATES; i++)
973         {
974             if (!(new_state & (1 << i))) continue;
975             TRACE( "setting wm state %u for unmapped window %p/%lx\n",
976                    i, data->hwnd, data->whole_window );
977             atoms[count++] = X11DRV_Atoms[state_atoms[i] - FIRST_XATOM];
978             if (state_atoms[i] == XATOM__NET_WM_STATE_MAXIMIZED_VERT)
979                 atoms[count++] = x11drv_atom(_NET_WM_STATE_MAXIMIZED_HORZ);
980         }
981         wine_tsx11_lock();
982         XChangeProperty( display, data->whole_window, x11drv_atom(_NET_WM_STATE), XA_ATOM,
983                          32, PropModeReplace, (unsigned char *)atoms, count );
984         wine_tsx11_unlock();
985     }
986     else  /* ask the window manager to do it for us */
987     {
988         XEvent xev;
989
990         xev.xclient.type = ClientMessage;
991         xev.xclient.window = data->whole_window;
992         xev.xclient.message_type = x11drv_atom(_NET_WM_STATE);
993         xev.xclient.serial = 0;
994         xev.xclient.display = display;
995         xev.xclient.send_event = True;
996         xev.xclient.format = 32;
997         xev.xclient.data.l[3] = 1;
998
999         for (i = 0; i < NB_NET_WM_STATES; i++)
1000         {
1001             if (!((data->net_wm_state ^ new_state) & (1 << i))) continue;  /* unchanged */
1002
1003             TRACE( "setting wm state %u for window %p/%lx to %u prev %u\n",
1004                    i, data->hwnd, data->whole_window,
1005                    (new_state & (1 << i)) != 0, (data->net_wm_state & (1 << i)) != 0 );
1006
1007             xev.xclient.data.l[0] = (new_state & (1 << i)) ? _NET_WM_STATE_ADD : _NET_WM_STATE_REMOVE;
1008             xev.xclient.data.l[1] = X11DRV_Atoms[state_atoms[i] - FIRST_XATOM];
1009             xev.xclient.data.l[2] = ((state_atoms[i] == XATOM__NET_WM_STATE_MAXIMIZED_VERT) ?
1010                                      x11drv_atom(_NET_WM_STATE_MAXIMIZED_HORZ) : 0);
1011             wine_tsx11_lock();
1012             XSendEvent( display, root_window, False,
1013                         SubstructureRedirectMask | SubstructureNotifyMask, &xev );
1014             wine_tsx11_unlock();
1015         }
1016     }
1017     data->net_wm_state = new_state;
1018 }
1019
1020
1021 /***********************************************************************
1022  *     set_xembed_flags
1023  */
1024 static void set_xembed_flags( Display *display, struct x11drv_win_data *data, unsigned long flags )
1025 {
1026     unsigned long info[2];
1027
1028     info[0] = 0; /* protocol version */
1029     info[1] = flags;
1030     wine_tsx11_lock();
1031     XChangeProperty( display, data->whole_window, x11drv_atom(_XEMBED_INFO),
1032                      x11drv_atom(_XEMBED_INFO), 32, PropModeReplace, (unsigned char*)info, 2 );
1033     wine_tsx11_unlock();
1034 }
1035
1036
1037 /***********************************************************************
1038  *     map_window
1039  */
1040 static void map_window( Display *display, struct x11drv_win_data *data, DWORD new_style )
1041 {
1042     TRACE( "win %p/%lx\n", data->hwnd, data->whole_window );
1043
1044     wait_for_withdrawn_state( display, data, TRUE );
1045
1046     if (!data->embedded)
1047     {
1048         update_net_wm_states( display, data );
1049         sync_window_style( display, data );
1050         wine_tsx11_lock();
1051         XMapWindow( display, data->whole_window );
1052         wine_tsx11_unlock();
1053     }
1054     else set_xembed_flags( display, data, XEMBED_MAPPED );
1055
1056     data->mapped = TRUE;
1057     data->iconic = (new_style & WS_MINIMIZE) != 0;
1058 }
1059
1060
1061 /***********************************************************************
1062  *     unmap_window
1063  */
1064 static void unmap_window( Display *display, struct x11drv_win_data *data )
1065 {
1066     TRACE( "win %p/%lx\n", data->hwnd, data->whole_window );
1067
1068     if (!data->embedded)
1069     {
1070         wait_for_withdrawn_state( display, data, FALSE );
1071         wine_tsx11_lock();
1072         if (data->managed) XWithdrawWindow( display, data->whole_window, DefaultScreen(display) );
1073         else XUnmapWindow( display, data->whole_window );
1074         wine_tsx11_unlock();
1075     }
1076     else set_xembed_flags( display, data, 0 );
1077
1078     data->mapped = FALSE;
1079     data->net_wm_state = 0;
1080 }
1081
1082
1083 /***********************************************************************
1084  *     make_window_embedded
1085  */
1086 void make_window_embedded( Display *display, struct x11drv_win_data *data )
1087 {
1088     if (data->mapped)
1089     {
1090         /* the window cannot be mapped before being embedded */
1091         unmap_window( display, data );
1092         data->embedded = TRUE;
1093         map_window( display, data, 0 );
1094     }
1095     else
1096     {
1097         data->embedded = TRUE;
1098         set_xembed_flags( display, data, 0 );
1099     }
1100 }
1101
1102
1103 /***********************************************************************
1104  *              X11DRV_window_to_X_rect
1105  *
1106  * Convert a rect from client to X window coordinates
1107  */
1108 void X11DRV_window_to_X_rect( struct x11drv_win_data *data, RECT *rect )
1109 {
1110     RECT rc;
1111
1112     if (!data->managed) return;
1113     if (IsRectEmpty( rect )) return;
1114
1115     get_x11_rect_offset( data, &rc );
1116
1117     rect->left   -= rc.left;
1118     rect->right  -= rc.right;
1119     rect->top    -= rc.top;
1120     rect->bottom -= rc.bottom;
1121     if (rect->top >= rect->bottom) rect->bottom = rect->top + 1;
1122     if (rect->left >= rect->right) rect->right = rect->left + 1;
1123 }
1124
1125
1126 /***********************************************************************
1127  *              X11DRV_X_to_window_rect
1128  *
1129  * Opposite of X11DRV_window_to_X_rect
1130  */
1131 void X11DRV_X_to_window_rect( struct x11drv_win_data *data, RECT *rect )
1132 {
1133     RECT rc;
1134
1135     if (!data->managed) return;
1136     if (IsRectEmpty( rect )) return;
1137
1138     get_x11_rect_offset( data, &rc );
1139
1140     rect->left   += rc.left;
1141     rect->right  += rc.right;
1142     rect->top    += rc.top;
1143     rect->bottom += rc.bottom;
1144     if (rect->top >= rect->bottom) rect->bottom = rect->top + 1;
1145     if (rect->left >= rect->right) rect->right = rect->left + 1;
1146 }
1147
1148
1149 /***********************************************************************
1150  *              sync_window_position
1151  *
1152  * Synchronize the X window position with the Windows one
1153  */
1154 static void sync_window_position( Display *display, struct x11drv_win_data *data,
1155                                   UINT swp_flags, const RECT *old_client_rect,
1156                                   const RECT *old_whole_rect )
1157 {
1158     DWORD style = GetWindowLongW( data->hwnd, GWL_STYLE );
1159     XWindowChanges changes;
1160     unsigned int mask = 0;
1161
1162     if (data->managed && data->iconic) return;
1163
1164     /* resizing a managed maximized window is not allowed */
1165     if (!(style & WS_MAXIMIZE) || !data->managed)
1166     {
1167         if ((changes.width = data->whole_rect.right - data->whole_rect.left) <= 0) changes.width = 1;
1168         if ((changes.height = data->whole_rect.bottom - data->whole_rect.top) <= 0) changes.height = 1;
1169         mask |= CWWidth | CWHeight;
1170     }
1171
1172     /* only the size is allowed to change for the desktop window */
1173     if (data->whole_window != root_window)
1174     {
1175         changes.x = data->whole_rect.left - virtual_screen_rect.left;
1176         changes.y = data->whole_rect.top - virtual_screen_rect.top;
1177         mask |= CWX | CWY;
1178     }
1179
1180     if (!(swp_flags & SWP_NOZORDER))
1181     {
1182         /* find window that this one must be after */
1183         HWND prev = GetWindow( data->hwnd, GW_HWNDPREV );
1184         while (prev && !(GetWindowLongW( prev, GWL_STYLE ) & WS_VISIBLE))
1185             prev = GetWindow( prev, GW_HWNDPREV );
1186         if (!prev)  /* top child */
1187         {
1188             changes.stack_mode = Above;
1189             mask |= CWStackMode;
1190         }
1191         /* should use stack_mode Below but most window managers don't get it right */
1192         /* and Above with a sibling doesn't work so well either, so we ignore it */
1193     }
1194
1195     TRACE( "setting win %p/%lx pos %d,%d,%dx%d after %lx changes=%x\n",
1196            data->hwnd, data->whole_window, data->whole_rect.left, data->whole_rect.top,
1197            data->whole_rect.right - data->whole_rect.left,
1198            data->whole_rect.bottom - data->whole_rect.top, changes.sibling, mask );
1199
1200     wine_tsx11_lock();
1201     set_size_hints( display, data, style );
1202     XReconfigureWMWindow( display, data->whole_window,
1203                           DefaultScreen(display), mask, &changes );
1204     wine_tsx11_unlock();
1205 }
1206
1207
1208 /***********************************************************************
1209  *              sync_client_position
1210  *
1211  * Synchronize the X client window position with the Windows one
1212  */
1213 static void sync_client_position( Display *display, struct x11drv_win_data *data,
1214                                   UINT swp_flags, const RECT *old_client_rect,
1215                                   const RECT *old_whole_rect )
1216 {
1217     int mask;
1218     XWindowChanges changes;
1219     RECT old = *old_client_rect;
1220     RECT new = data->client_rect;
1221
1222     OffsetRect( &old, -old_whole_rect->left, -old_whole_rect->top );
1223     OffsetRect( &new, -data->whole_rect.left, -data->whole_rect.top );
1224     if (!(mask = get_window_changes( &changes, &old, &new ))) return;
1225
1226     if (data->client_window)
1227     {
1228         TRACE( "setting client win %lx pos %d,%d,%dx%d changes=%x\n",
1229                data->client_window, new.left, new.top,
1230                new.right - new.left, new.bottom - new.top, mask );
1231         wine_tsx11_lock();
1232         XConfigureWindow( display, data->client_window, mask, &changes );
1233         wine_tsx11_unlock();
1234     }
1235
1236     if (data->gl_drawable && (mask & (CWWidth|CWHeight))) sync_gl_drawable( display, data );
1237 }
1238
1239
1240 /***********************************************************************
1241  *              move_window_bits
1242  *
1243  * Move the window bits when a window is moved.
1244  */
1245 static void move_window_bits( struct x11drv_win_data *data, const RECT *old_rect, const RECT *new_rect,
1246                               const RECT *old_client_rect )
1247 {
1248     RECT src_rect = *old_rect;
1249     RECT dst_rect = *new_rect;
1250     HDC hdc_src, hdc_dst;
1251     INT code;
1252     HRGN rgn = 0;
1253     HWND parent = 0;
1254
1255     if (!data->whole_window)
1256     {
1257         OffsetRect( &dst_rect, -data->window_rect.left, -data->window_rect.top );
1258         parent = GetAncestor( data->hwnd, GA_PARENT );
1259         hdc_src = GetDCEx( parent, 0, DCX_CACHE );
1260         hdc_dst = GetDCEx( data->hwnd, 0, DCX_CACHE | DCX_WINDOW );
1261     }
1262     else
1263     {
1264         OffsetRect( &dst_rect, -data->client_rect.left, -data->client_rect.top );
1265         /* make src rect relative to the old position of the window */
1266         OffsetRect( &src_rect, -old_client_rect->left, -old_client_rect->top );
1267         if (dst_rect.left == src_rect.left && dst_rect.top == src_rect.top) return;
1268         hdc_src = hdc_dst = GetDCEx( data->hwnd, 0, DCX_CACHE );
1269     }
1270
1271     code = X11DRV_START_EXPOSURES;
1272     ExtEscape( hdc_dst, X11DRV_ESCAPE, sizeof(code), (LPSTR)&code, 0, NULL );
1273
1274     TRACE( "copying bits for win %p/%lx/%lx %s -> %s\n",
1275            data->hwnd, data->whole_window, data->client_window,
1276            wine_dbgstr_rect(&src_rect), wine_dbgstr_rect(&dst_rect) );
1277     BitBlt( hdc_dst, dst_rect.left, dst_rect.top,
1278             dst_rect.right - dst_rect.left, dst_rect.bottom - dst_rect.top,
1279             hdc_src, src_rect.left, src_rect.top, SRCCOPY );
1280
1281     code = X11DRV_END_EXPOSURES;
1282     ExtEscape( hdc_dst, X11DRV_ESCAPE, sizeof(code), (LPSTR)&code, sizeof(rgn), (LPSTR)&rgn );
1283
1284     ReleaseDC( data->hwnd, hdc_dst );
1285     if (hdc_src != hdc_dst) ReleaseDC( parent, hdc_src );
1286
1287     if (rgn)
1288     {
1289         if (!data->whole_window)
1290         {
1291             /* map region to client rect since we are using DCX_WINDOW */
1292             OffsetRgn( rgn, data->window_rect.left - data->client_rect.left,
1293                        data->window_rect.top - data->client_rect.top );
1294             RedrawWindow( data->hwnd, NULL, rgn,
1295                           RDW_INVALIDATE | RDW_FRAME | RDW_ERASE | RDW_ALLCHILDREN );
1296         }
1297         else RedrawWindow( data->hwnd, NULL, rgn, RDW_INVALIDATE | RDW_ERASE | RDW_ALLCHILDREN );
1298         DeleteObject( rgn );
1299     }
1300 }
1301
1302
1303 /**********************************************************************
1304  *              create_whole_window
1305  *
1306  * Create the whole X window for a given window
1307  */
1308 static Window create_whole_window( Display *display, struct x11drv_win_data *data )
1309 {
1310     int cx, cy, mask;
1311     XSetWindowAttributes attr;
1312     WCHAR text[1024];
1313     HRGN hrgn;
1314
1315     if (!(cx = data->window_rect.right - data->window_rect.left)) cx = 1;
1316     if (!(cy = data->window_rect.bottom - data->window_rect.top)) cy = 1;
1317
1318     if (!data->managed && is_window_managed( data->hwnd, SWP_NOACTIVATE, &data->window_rect ))
1319     {
1320         TRACE( "making win %p/%lx managed\n", data->hwnd, data->whole_window );
1321         data->managed = TRUE;
1322         SetPropA( data->hwnd, managed_prop, (HANDLE)1 );
1323     }
1324
1325     mask = get_window_attributes( display, data, &attr );
1326
1327     wine_tsx11_lock();
1328
1329     data->whole_rect = data->window_rect;
1330     data->whole_window = XCreateWindow( display, root_window,
1331                                         data->window_rect.left - virtual_screen_rect.left,
1332                                         data->window_rect.top - virtual_screen_rect.top,
1333                                         cx, cy, 0, screen_depth, InputOutput,
1334                                         visual, mask, &attr );
1335
1336     if (data->whole_window) XSaveContext( display, data->whole_window, winContext, (char *)data->hwnd );
1337     wine_tsx11_unlock();
1338
1339     if (!data->whole_window) return 0;
1340
1341     if (!create_client_window( display, data, NULL ))
1342     {
1343         wine_tsx11_lock();
1344         XDeleteContext( display, data->whole_window, winContext );
1345         XDestroyWindow( display, data->whole_window );
1346         data->whole_window = 0;
1347         wine_tsx11_unlock();
1348         return 0;
1349     }
1350
1351     set_initial_wm_hints( display, data );
1352     set_wm_hints( display, data );
1353
1354     SetPropA( data->hwnd, whole_window_prop, (HANDLE)data->whole_window );
1355
1356     /* set the window text */
1357     if (!InternalGetWindowText( data->hwnd, text, sizeof(text)/sizeof(WCHAR) )) text[0] = 0;
1358     sync_window_text( display, data->whole_window, text );
1359
1360     /* set the window region */
1361     if ((hrgn = CreateRectRgn( 0, 0, 0, 0 )))
1362     {
1363         if (GetWindowRgn( data->hwnd, hrgn ) != ERROR) sync_window_region( display, data, hrgn );
1364         DeleteObject( hrgn );
1365     }
1366     wine_tsx11_lock();
1367     XFlush( display );  /* make sure the window exists before we start painting to it */
1368     wine_tsx11_unlock();
1369     return data->whole_window;
1370 }
1371
1372
1373 /**********************************************************************
1374  *              destroy_whole_window
1375  *
1376  * Destroy the whole X window for a given window.
1377  */
1378 static void destroy_whole_window( Display *display, struct x11drv_win_data *data, BOOL already_destroyed )
1379 {
1380     struct x11drv_thread_data *thread_data = x11drv_thread_data();
1381
1382     if (!data->whole_window) return;
1383
1384     TRACE( "win %p xwin %lx/%lx\n", data->hwnd, data->whole_window, data->client_window );
1385     if (thread_data->cursor_window == data->whole_window ||
1386         thread_data->cursor_window == data->client_window)
1387         thread_data->cursor_window = None;
1388     wine_tsx11_lock();
1389     XDeleteContext( display, data->whole_window, winContext );
1390     XDeleteContext( display, data->client_window, winContext );
1391     if (!already_destroyed) XDestroyWindow( display, data->whole_window );
1392     data->whole_window = data->client_window = 0;
1393     data->wm_state = WithdrawnState;
1394     data->net_wm_state = 0;
1395     data->mapped = FALSE;
1396     if (data->xic)
1397     {
1398         XUnsetICFocus( data->xic );
1399         XDestroyIC( data->xic );
1400         data->xic = 0;
1401     }
1402     /* Outlook stops processing messages after destroying a dialog, so we need an explicit flush */
1403     XFlush( display );
1404     XFree( data->wm_hints );
1405     data->wm_hints = NULL;
1406     wine_tsx11_unlock();
1407     RemovePropA( data->hwnd, whole_window_prop );
1408     RemovePropA( data->hwnd, client_window_prop );
1409 }
1410
1411
1412 /*****************************************************************
1413  *              SetWindowText   (X11DRV.@)
1414  */
1415 void X11DRV_SetWindowText( HWND hwnd, LPCWSTR text )
1416 {
1417     Display *display = thread_display();
1418     Window win;
1419
1420     if ((win = X11DRV_get_whole_window( hwnd )) && win != DefaultRootWindow(display))
1421         sync_window_text( display, win, text );
1422 }
1423
1424
1425 /***********************************************************************
1426  *              SetWindowStyle   (X11DRV.@)
1427  *
1428  * Update the X state of a window to reflect a style change
1429  */
1430 void X11DRV_SetWindowStyle( HWND hwnd, DWORD old_style )
1431 {
1432     Display *display = thread_display();
1433     struct x11drv_win_data *data;
1434     DWORD new_style, changed;
1435
1436     if (hwnd == GetDesktopWindow()) return;
1437     new_style = GetWindowLongW( hwnd, GWL_STYLE );
1438     changed = new_style ^ old_style;
1439
1440     if ((changed & WS_VISIBLE) && (new_style & WS_VISIBLE))
1441     {
1442         /* we don't unmap windows, that causes trouble with the window manager */
1443         if (!(data = X11DRV_get_win_data( hwnd )) &&
1444             !(data = X11DRV_create_win_data( hwnd ))) return;
1445
1446         if (data->whole_window && is_window_rect_mapped( &data->window_rect ))
1447         {
1448             set_wm_hints( display, data );
1449             if (!data->mapped) map_window( display, data, new_style );
1450         }
1451     }
1452
1453     if (changed & WS_DISABLED)
1454     {
1455         data = X11DRV_get_win_data( hwnd );
1456         if (data && data->wm_hints)
1457         {
1458             wine_tsx11_lock();
1459             data->wm_hints->input = !(new_style & WS_DISABLED);
1460             XSetWMHints( display, data->whole_window, data->wm_hints );
1461             wine_tsx11_unlock();
1462         }
1463     }
1464 }
1465
1466
1467 /***********************************************************************
1468  *              DestroyWindow   (X11DRV.@)
1469  */
1470 void X11DRV_DestroyWindow( HWND hwnd )
1471 {
1472     struct x11drv_thread_data *thread_data = x11drv_thread_data();
1473     Display *display = thread_data->display;
1474     struct x11drv_win_data *data;
1475
1476     if (!(data = X11DRV_get_win_data( hwnd ))) return;
1477
1478     if (data->pixmap)
1479     {
1480         destroy_glxpixmap(display, data->gl_drawable);
1481         wine_tsx11_lock();
1482         XFreePixmap(display, data->pixmap);
1483         wine_tsx11_unlock();
1484     }
1485     else if (data->gl_drawable)
1486     {
1487         wine_tsx11_lock();
1488         XDestroyWindow(display, data->gl_drawable);
1489         wine_tsx11_unlock();
1490     }
1491
1492     destroy_whole_window( display, data, FALSE );
1493     destroy_icon_window( display, data );
1494
1495     if (data->colormap)
1496     {
1497         wine_tsx11_lock();
1498         XFreeColormap( display, data->colormap );
1499         wine_tsx11_unlock();
1500     }
1501
1502     if (thread_data->last_focus == hwnd) thread_data->last_focus = 0;
1503     if (data->hWMIconBitmap) DeleteObject( data->hWMIconBitmap );
1504     if (data->hWMIconMask) DeleteObject( data->hWMIconMask);
1505     wine_tsx11_lock();
1506     XDeleteContext( display, (XID)hwnd, win_data_context );
1507     wine_tsx11_unlock();
1508     HeapFree( GetProcessHeap(), 0, data );
1509 }
1510
1511
1512 /***********************************************************************
1513  *              X11DRV_DestroyNotify
1514  */
1515 void X11DRV_DestroyNotify( HWND hwnd, XEvent *event )
1516 {
1517     Display *display = event->xdestroywindow.display;
1518     struct x11drv_win_data *data;
1519
1520     if (!(data = X11DRV_get_win_data( hwnd ))) return;
1521
1522     FIXME( "window %p/%lx destroyed from the outside\n", hwnd, data->whole_window );
1523     destroy_whole_window( display, data, TRUE );
1524 }
1525
1526
1527 static struct x11drv_win_data *alloc_win_data( Display *display, HWND hwnd )
1528 {
1529     struct x11drv_win_data *data;
1530
1531     if ((data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*data))))
1532     {
1533         data->hwnd = hwnd;
1534         wine_tsx11_lock();
1535         if (!winContext) winContext = XUniqueContext();
1536         if (!win_data_context) win_data_context = XUniqueContext();
1537         XSaveContext( display, (XID)hwnd, win_data_context, (char *)data );
1538         wine_tsx11_unlock();
1539     }
1540     return data;
1541 }
1542
1543
1544 /* initialize the desktop window id in the desktop manager process */
1545 static struct x11drv_win_data *create_desktop_win_data( Display *display, HWND hwnd )
1546 {
1547     struct x11drv_win_data *data;
1548     VisualID visualid;
1549
1550     if (!(data = alloc_win_data( display, hwnd ))) return NULL;
1551     wine_tsx11_lock();
1552     visualid = XVisualIDFromVisual(visual);
1553     wine_tsx11_unlock();
1554     data->whole_window = data->client_window = root_window;
1555     data->managed = TRUE;
1556     SetPropA( data->hwnd, managed_prop, (HANDLE)1 );
1557     SetPropA( data->hwnd, whole_window_prop, (HANDLE)root_window );
1558     SetPropA( data->hwnd, client_window_prop, (HANDLE)root_window );
1559     set_initial_wm_hints( display, data );
1560     return data;
1561 }
1562
1563 /**********************************************************************
1564  *              CreateDesktopWindow   (X11DRV.@)
1565  */
1566 BOOL X11DRV_CreateDesktopWindow( HWND hwnd )
1567 {
1568     unsigned int width, height;
1569
1570     /* retrieve the real size of the desktop */
1571     SERVER_START_REQ( get_window_rectangles )
1572     {
1573         req->handle = hwnd;
1574         wine_server_call( req );
1575         width  = reply->window.right - reply->window.left;
1576         height = reply->window.bottom - reply->window.top;
1577     }
1578     SERVER_END_REQ;
1579
1580     if (!width && !height)  /* not initialized yet */
1581     {
1582         SERVER_START_REQ( set_window_pos )
1583         {
1584             req->handle        = hwnd;
1585             req->previous      = 0;
1586             req->flags         = SWP_NOZORDER;
1587             req->window.left   = virtual_screen_rect.left;
1588             req->window.top    = virtual_screen_rect.top;
1589             req->window.right  = virtual_screen_rect.right;
1590             req->window.bottom = virtual_screen_rect.bottom;
1591             req->client        = req->window;
1592             wine_server_call( req );
1593         }
1594         SERVER_END_REQ;
1595     }
1596     else
1597     {
1598         Window win = (Window)GetPropA( hwnd, whole_window_prop );
1599         if (win && win != root_window) X11DRV_init_desktop( win, width, height );
1600     }
1601     return TRUE;
1602 }
1603
1604
1605 /**********************************************************************
1606  *              CreateWindow   (X11DRV.@)
1607  */
1608 BOOL X11DRV_CreateWindow( HWND hwnd )
1609 {
1610     Display *display = thread_display();
1611
1612     if (hwnd == GetDesktopWindow() && root_window != DefaultRootWindow( display ))
1613     {
1614         /* the desktop win data can't be created lazily */
1615         if (!create_desktop_win_data( display, hwnd )) return FALSE;
1616     }
1617     return TRUE;
1618 }
1619
1620
1621 /***********************************************************************
1622  *              X11DRV_get_win_data
1623  *
1624  * Return the X11 data structure associated with a window.
1625  */
1626 struct x11drv_win_data *X11DRV_get_win_data( HWND hwnd )
1627 {
1628     char *data;
1629
1630     if (!hwnd || XFindContext( thread_display(), (XID)hwnd, win_data_context, &data )) data = NULL;
1631     return (struct x11drv_win_data *)data;
1632 }
1633
1634
1635 /***********************************************************************
1636  *              X11DRV_create_win_data
1637  *
1638  * Create an X11 data window structure for an existing window.
1639  */
1640 struct x11drv_win_data *X11DRV_create_win_data( HWND hwnd )
1641 {
1642     Display *display = thread_display();
1643     struct x11drv_win_data *data;
1644     HWND parent;
1645
1646     if (!(parent = GetAncestor( hwnd, GA_PARENT ))) return NULL;  /* desktop */
1647     if (!(data = alloc_win_data( display, hwnd ))) return NULL;
1648
1649     GetWindowRect( hwnd, &data->window_rect );
1650     MapWindowPoints( 0, parent, (POINT *)&data->window_rect, 2 );
1651     data->whole_rect = data->window_rect;
1652     GetClientRect( hwnd, &data->client_rect );
1653     MapWindowPoints( hwnd, parent, (POINT *)&data->client_rect, 2 );
1654
1655     if (parent == GetDesktopWindow())
1656     {
1657         if (!create_whole_window( display, data ))
1658         {
1659             HeapFree( GetProcessHeap(), 0, data );
1660             return NULL;
1661         }
1662         TRACE( "win %p/%lx/%lx window %s whole %s client %s\n",
1663                hwnd, data->whole_window, data->client_window, wine_dbgstr_rect( &data->window_rect ),
1664                wine_dbgstr_rect( &data->whole_rect ), wine_dbgstr_rect( &data->client_rect ));
1665     }
1666     return data;
1667 }
1668
1669
1670 /***********************************************************************
1671  *              X11DRV_get_whole_window
1672  *
1673  * Return the X window associated with the full area of a window
1674  */
1675 Window X11DRV_get_whole_window( HWND hwnd )
1676 {
1677     struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
1678
1679     if (!data)
1680     {
1681         if (hwnd == GetDesktopWindow()) return root_window;
1682         return (Window)GetPropA( hwnd, whole_window_prop );
1683     }
1684     return data->whole_window;
1685 }
1686
1687
1688 /***********************************************************************
1689  *              X11DRV_get_client_window
1690  *
1691  * Return the X window associated with the client area of a window
1692  */
1693 Window X11DRV_get_client_window( HWND hwnd )
1694 {
1695     struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
1696
1697     if (!data)
1698     {
1699         if (hwnd == GetDesktopWindow()) return root_window;
1700         return (Window)GetPropA( hwnd, client_window_prop );
1701     }
1702     return data->client_window;
1703 }
1704
1705
1706 /***********************************************************************
1707  *              X11DRV_get_ic
1708  *
1709  * Return the X input context associated with a window
1710  */
1711 XIC X11DRV_get_ic( HWND hwnd )
1712 {
1713     struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
1714     XIM xim;
1715
1716     if (!data) return 0;
1717     if (data->xic) return data->xic;
1718     if (!(xim = x11drv_thread_data()->xim)) return 0;
1719     return X11DRV_CreateIC( xim, data );
1720 }
1721
1722
1723 /***********************************************************************
1724  *              X11DRV_GetDC   (X11DRV.@)
1725  */
1726 void X11DRV_GetDC( HDC hdc, HWND hwnd, HWND top, const RECT *win_rect,
1727                    const RECT *top_rect, DWORD flags )
1728 {
1729     struct x11drv_escape_set_drawable escape;
1730     struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
1731
1732     escape.code        = X11DRV_SET_DRAWABLE;
1733     escape.mode        = IncludeInferiors;
1734     escape.fbconfig_id = 0;
1735     escape.gl_drawable = 0;
1736     escape.pixmap      = 0;
1737
1738     if (top == hwnd && data && IsIconic( hwnd ) && data->icon_window)
1739     {
1740         escape.drawable = data->icon_window;
1741     }
1742     else if (top == hwnd && (flags & DCX_WINDOW))
1743     {
1744         escape.drawable = data ? data->whole_window : X11DRV_get_whole_window( hwnd );
1745     }
1746     else
1747     {
1748         escape.drawable    = X11DRV_get_client_window( top );
1749         escape.fbconfig_id = data ? data->fbconfig_id : (XID)GetPropA( hwnd, fbconfig_id_prop );
1750         escape.gl_drawable = data ? data->gl_drawable : (Drawable)GetPropA( hwnd, gl_drawable_prop );
1751         escape.pixmap      = data ? data->pixmap : (Pixmap)GetPropA( hwnd, pixmap_prop );
1752         if (flags & DCX_CLIPCHILDREN) escape.mode = ClipByChildren;
1753     }
1754
1755     escape.dc_rect.left         = win_rect->left - top_rect->left;
1756     escape.dc_rect.top          = win_rect->top - top_rect->top;
1757     escape.dc_rect.right        = win_rect->right - top_rect->left;
1758     escape.dc_rect.bottom       = win_rect->bottom - top_rect->top;
1759     escape.drawable_rect.left   = top_rect->left;
1760     escape.drawable_rect.top    = top_rect->top;
1761     escape.drawable_rect.right  = top_rect->right;
1762     escape.drawable_rect.bottom = top_rect->bottom;
1763
1764     ExtEscape( hdc, X11DRV_ESCAPE, sizeof(escape), (LPSTR)&escape, 0, NULL );
1765 }
1766
1767
1768 /***********************************************************************
1769  *              X11DRV_ReleaseDC  (X11DRV.@)
1770  */
1771 void X11DRV_ReleaseDC( HWND hwnd, HDC hdc )
1772 {
1773     struct x11drv_escape_set_drawable escape;
1774
1775     escape.code = X11DRV_SET_DRAWABLE;
1776     escape.drawable = root_window;
1777     escape.mode = IncludeInferiors;
1778     escape.drawable_rect = virtual_screen_rect;
1779     SetRect( &escape.dc_rect, 0, 0, virtual_screen_rect.right - virtual_screen_rect.left,
1780              virtual_screen_rect.bottom - virtual_screen_rect.top );
1781     escape.fbconfig_id = 0;
1782     escape.gl_drawable = 0;
1783     escape.pixmap = 0;
1784     ExtEscape( hdc, X11DRV_ESCAPE, sizeof(escape), (LPSTR)&escape, 0, NULL );
1785 }
1786
1787
1788 /***********************************************************************
1789  *              SetCapture  (X11DRV.@)
1790  */
1791 void X11DRV_SetCapture( HWND hwnd, UINT flags )
1792 {
1793     struct x11drv_thread_data *thread_data = x11drv_thread_data();
1794
1795     if (!(flags & (GUI_INMOVESIZE | GUI_INMENUMODE))) return;
1796
1797     if (hwnd)
1798     {
1799         Window grab_win = X11DRV_get_client_window( GetAncestor( hwnd, GA_ROOT ) );
1800
1801         if (!grab_win) return;
1802         wine_tsx11_lock();
1803         XFlush( gdi_display );
1804         XGrabPointer( thread_data->display, grab_win, False,
1805                       PointerMotionMask | ButtonPressMask | ButtonReleaseMask,
1806                       GrabModeAsync, GrabModeAsync, None, None, CurrentTime );
1807         wine_tsx11_unlock();
1808         thread_data->grab_window = grab_win;
1809     }
1810     else  /* release capture */
1811     {
1812         wine_tsx11_lock();
1813         XFlush( gdi_display );
1814         XUngrabPointer( thread_data->display, CurrentTime );
1815         wine_tsx11_unlock();
1816         thread_data->grab_window = None;
1817     }
1818 }
1819
1820
1821 /*****************************************************************
1822  *              SetParent   (X11DRV.@)
1823  */
1824 void X11DRV_SetParent( HWND hwnd, HWND parent, HWND old_parent )
1825 {
1826     Display *display = thread_display();
1827     struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
1828
1829     if (!data) return;
1830     if (parent == old_parent) return;
1831
1832     if (parent != GetDesktopWindow()) /* a child window */
1833     {
1834         if (old_parent == GetDesktopWindow())
1835         {
1836             /* destroy the old X windows */
1837             destroy_whole_window( display, data, FALSE );
1838             destroy_icon_window( display, data );
1839             if (data->managed)
1840             {
1841                 data->managed = FALSE;
1842                 RemovePropA( data->hwnd, managed_prop );
1843             }
1844         }
1845     }
1846     else  /* new top level window */
1847     {
1848         /* FIXME: we ignore errors since we can't really recover anyway */
1849         create_whole_window( display, data );
1850     }
1851 }
1852
1853
1854 /*****************************************************************
1855  *              SetFocus   (X11DRV.@)
1856  *
1857  * Set the X focus.
1858  * Explicit colormap management seems to work only with OLVWM.
1859  */
1860 void X11DRV_SetFocus( HWND hwnd )
1861 {
1862     Display *display = thread_display();
1863     struct x11drv_win_data *data;
1864     XWindowChanges changes;
1865
1866     /* If setting the focus to 0, uninstall the colormap */
1867     if (!hwnd && root_window == DefaultRootWindow(display))
1868     {
1869         wine_tsx11_lock();
1870         if (X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_PRIVATE)
1871             XUninstallColormap( display, X11DRV_PALETTE_PaletteXColormap );
1872         wine_tsx11_unlock();
1873         return;
1874     }
1875
1876     hwnd = GetAncestor( hwnd, GA_ROOT );
1877
1878     if (!(data = X11DRV_get_win_data( hwnd ))) return;
1879     if (data->managed || !data->whole_window) return;
1880
1881     /* Set X focus and install colormap */
1882     wine_tsx11_lock();
1883     changes.stack_mode = Above;
1884     XConfigureWindow( display, data->whole_window, CWStackMode, &changes );
1885     if (root_window == DefaultRootWindow(display))
1886     {
1887         /* we must not use CurrentTime (ICCCM), so try to use last message time instead */
1888         /* FIXME: this is not entirely correct */
1889         XSetInputFocus( display, data->whole_window, RevertToParent,
1890                         /* CurrentTime */
1891                         GetMessageTime() - EVENT_x11_time_to_win32_time(0));
1892         if (X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_PRIVATE)
1893             XInstallColormap( display, X11DRV_PALETTE_PaletteXColormap );
1894     }
1895     wine_tsx11_unlock();
1896 }
1897
1898
1899 /***********************************************************************
1900  *              SetWindowPos   (X11DRV.@)
1901  */
1902 void X11DRV_SetWindowPos( HWND hwnd, HWND insert_after, UINT swp_flags,
1903                           const RECT *rectWindow, const RECT *rectClient,
1904                           const RECT *visible_rect, const RECT *valid_rects )
1905 {
1906     struct x11drv_thread_data *thread_data = x11drv_thread_data();
1907     Display *display = thread_data->display;
1908     struct x11drv_win_data *data = X11DRV_get_win_data( hwnd );
1909     DWORD new_style = GetWindowLongW( hwnd, GWL_STYLE );
1910     RECT old_window_rect, old_whole_rect, old_client_rect;
1911     int event_type;
1912
1913     if (!data)
1914     {
1915         /* create the win data if the window is being made visible */
1916         if (!(new_style & WS_VISIBLE)) return;
1917         if (!(data = X11DRV_create_win_data( hwnd ))) return;
1918     }
1919
1920     /* check if we need to switch the window to managed */
1921     if (!data->managed && data->whole_window && is_window_managed( hwnd, swp_flags, rectWindow ))
1922     {
1923         TRACE( "making win %p/%lx managed\n", hwnd, data->whole_window );
1924         if (data->mapped) unmap_window( display, data );
1925         data->managed = TRUE;
1926         SetPropA( hwnd, managed_prop, (HANDLE)1 );
1927     }
1928
1929     old_window_rect = data->window_rect;
1930     old_whole_rect  = data->whole_rect;
1931     old_client_rect = data->client_rect;
1932     data->window_rect = *rectWindow;
1933     data->whole_rect  = *rectWindow;
1934     data->client_rect = *rectClient;
1935     X11DRV_window_to_X_rect( data, &data->whole_rect );
1936     if (memcmp( visible_rect, &data->whole_rect, sizeof(RECT) ))
1937     {
1938         TRACE( "%p: need to update visible rect %s -> %s\n", hwnd,
1939                wine_dbgstr_rect(visible_rect), wine_dbgstr_rect(&data->whole_rect) );
1940         SERVER_START_REQ( set_window_visible_rect )
1941         {
1942             req->handle         = hwnd;
1943             req->flags          = swp_flags;
1944             req->visible.left   = data->whole_rect.left;
1945             req->visible.top    = data->whole_rect.top;
1946             req->visible.right  = data->whole_rect.right;
1947             req->visible.bottom = data->whole_rect.bottom;
1948             wine_server_call( req );
1949         }
1950         SERVER_END_REQ;
1951     }
1952
1953     TRACE( "win %p window %s client %s style %08x flags %08x\n",
1954            hwnd, wine_dbgstr_rect(rectWindow), wine_dbgstr_rect(rectClient), new_style, swp_flags );
1955
1956     if (!IsRectEmpty( &valid_rects[0] ))
1957     {
1958         int x_offset = old_whole_rect.left - data->whole_rect.left;
1959         int y_offset = old_whole_rect.top - data->whole_rect.top;
1960
1961         /* if all that happened is that the whole window moved, copy everything */
1962         if (!(swp_flags & SWP_FRAMECHANGED) &&
1963             old_whole_rect.right   - data->whole_rect.right   == x_offset &&
1964             old_whole_rect.bottom  - data->whole_rect.bottom  == y_offset &&
1965             old_client_rect.left   - data->client_rect.left   == x_offset &&
1966             old_client_rect.right  - data->client_rect.right  == x_offset &&
1967             old_client_rect.top    - data->client_rect.top    == y_offset &&
1968             old_client_rect.bottom - data->client_rect.bottom == y_offset &&
1969             !memcmp( &valid_rects[0], &data->client_rect, sizeof(RECT) ))
1970         {
1971             /* if we have an X window the bits will be moved by the X server */
1972             if (!data->whole_window)
1973                 move_window_bits( data, &old_whole_rect, &data->whole_rect, &old_client_rect );
1974         }
1975         else
1976             move_window_bits( data, &valid_rects[1], &valid_rects[0], &old_client_rect );
1977     }
1978
1979     wine_tsx11_lock();
1980     XFlush( gdi_display );  /* make sure painting is done before we move the window */
1981     wine_tsx11_unlock();
1982
1983     sync_client_position( display, data, swp_flags, &old_client_rect, &old_whole_rect );
1984
1985     if (!data->whole_window) return;
1986
1987     /* check if we are currently processing an event relevant to this window */
1988     event_type = 0;
1989     if (thread_data->current_event && thread_data->current_event->xany.window == data->whole_window)
1990         event_type = thread_data->current_event->type;
1991
1992     if (event_type != ConfigureNotify && event_type != PropertyNotify)
1993         event_type = 0;  /* ignore other events */
1994
1995     if (data->mapped && (!(new_style & WS_VISIBLE) ||
1996                          (!event_type && !is_window_rect_mapped( rectWindow ))))
1997         unmap_window( display, data );
1998
1999     /* don't change position if we are about to minimize or maximize a managed window */
2000     if (!event_type &&
2001         !(data->managed && (swp_flags & SWP_STATECHANGED) && (new_style & (WS_MINIMIZE|WS_MAXIMIZE))))
2002         sync_window_position( display, data, swp_flags, &old_client_rect, &old_whole_rect );
2003
2004     if ((new_style & WS_VISIBLE) &&
2005         ((new_style & WS_MINIMIZE) || is_window_rect_mapped( rectWindow )))
2006     {
2007         if (!data->mapped || (swp_flags & (SWP_FRAMECHANGED|SWP_STATECHANGED)))
2008             set_wm_hints( display, data );
2009
2010         if (!data->mapped)
2011         {
2012             map_window( display, data, new_style );
2013         }
2014         else if ((swp_flags & SWP_STATECHANGED) && (!data->iconic != !(new_style & WS_MINIMIZE)))
2015         {
2016             data->iconic = (new_style & WS_MINIMIZE) != 0;
2017             TRACE( "changing win %p iconic state to %u\n", data->hwnd, data->iconic );
2018             wine_tsx11_lock();
2019             if (data->iconic)
2020                 XIconifyWindow( display, data->whole_window, DefaultScreen(display) );
2021             else if (is_window_rect_mapped( rectWindow ))
2022                 XMapWindow( display, data->whole_window );
2023             wine_tsx11_unlock();
2024             update_net_wm_states( display, data );
2025         }
2026         else if (!event_type)
2027         {
2028             update_net_wm_states( display, data );
2029         }
2030     }
2031
2032     wine_tsx11_lock();
2033     XFlush( display );  /* make sure changes are done before we start painting again */
2034     wine_tsx11_unlock();
2035 }
2036
2037
2038 /**********************************************************************
2039  *              SetWindowIcon (X11DRV.@)
2040  *
2041  * hIcon or hIconSm has changed (or is being initialised for the
2042  * first time). Complete the X11 driver-specific initialisation
2043  * and set the window hints.
2044  *
2045  * This is not entirely correct, may need to create
2046  * an icon window and set the pixmap as a background
2047  */
2048 void X11DRV_SetWindowIcon( HWND hwnd, UINT type, HICON icon )
2049 {
2050     Display *display = thread_display();
2051     struct x11drv_win_data *data;
2052
2053     if (type != ICON_BIG) return;  /* nothing to do here */
2054
2055     if (!(data = X11DRV_get_win_data( hwnd ))) return;
2056     if (!data->whole_window) return;
2057     if (!data->managed) return;
2058
2059     if (data->wm_hints)
2060     {
2061         set_icon_hints( display, data, icon );
2062         wine_tsx11_lock();
2063         XSetWMHints( display, data->whole_window, data->wm_hints );
2064         wine_tsx11_unlock();
2065     }
2066 }
2067
2068
2069 /***********************************************************************
2070  *              SetWindowRgn  (X11DRV.@)
2071  *
2072  * Assign specified region to window (for non-rectangular windows)
2073  */
2074 int X11DRV_SetWindowRgn( HWND hwnd, HRGN hrgn, BOOL redraw )
2075 {
2076     struct x11drv_win_data *data;
2077
2078     if ((data = X11DRV_get_win_data( hwnd )))
2079     {
2080         sync_window_region( thread_display(), data, hrgn );
2081     }
2082     else if (GetWindowThreadProcessId( hwnd, NULL ) != GetCurrentThreadId())
2083     {
2084         FIXME( "not supported on other thread window %p\n", hwnd );
2085         SetLastError( ERROR_INVALID_WINDOW_HANDLE );
2086         return FALSE;
2087     }
2088
2089     return TRUE;
2090 }
2091
2092
2093 /***********************************************************************
2094  *              is_netwm_supported
2095  */
2096 static BOOL is_netwm_supported( Display *display, Atom atom )
2097 {
2098     static Atom *net_supported;
2099     static int net_supported_count = -1;
2100     int i;
2101
2102     wine_tsx11_lock();
2103     if (net_supported_count == -1)
2104     {
2105         Atom type;
2106         int format;
2107         unsigned long count, remaining;
2108
2109         if (!XGetWindowProperty( display, DefaultRootWindow(display), x11drv_atom(_NET_SUPPORTED), 0,
2110                                  ~0UL, False, XA_ATOM, &type, &format, &count,
2111                                  &remaining, (unsigned char **)&net_supported ))
2112             net_supported_count = get_property_size( format, count ) / sizeof(Atom);
2113         else
2114             net_supported_count = 0;
2115     }
2116     wine_tsx11_unlock();
2117
2118     for (i = 0; i < net_supported_count; i++)
2119         if (net_supported[i] == atom) return TRUE;
2120     return FALSE;
2121 }
2122
2123
2124 /***********************************************************************
2125  *           SysCommand   (X11DRV.@)
2126  *
2127  * Perform WM_SYSCOMMAND handling.
2128  */
2129 LRESULT X11DRV_SysCommand( HWND hwnd, WPARAM wparam, LPARAM lparam )
2130 {
2131     WPARAM hittest = wparam & 0x0f;
2132     DWORD dwPoint;
2133     int x, y, dir;
2134     XEvent xev;
2135     Display *display = thread_display();
2136     struct x11drv_win_data *data;
2137
2138     if (!(data = X11DRV_get_win_data( hwnd ))) return -1;
2139     if (!data->whole_window || !data->managed || !data->mapped) return -1;
2140
2141     switch (wparam & 0xfff0)
2142     {
2143     case SC_MOVE:
2144         if (!hittest) dir = _NET_WM_MOVERESIZE_MOVE_KEYBOARD;
2145         else dir = _NET_WM_MOVERESIZE_MOVE;
2146         break;
2147     case SC_SIZE:
2148         /* windows without WS_THICKFRAME are not resizable through the window manager */
2149         if (!(GetWindowLongW( hwnd, GWL_STYLE ) & WS_THICKFRAME)) return -1;
2150
2151         switch (hittest)
2152         {
2153         case WMSZ_LEFT:        dir = _NET_WM_MOVERESIZE_SIZE_LEFT; break;
2154         case WMSZ_RIGHT:       dir = _NET_WM_MOVERESIZE_SIZE_RIGHT; break;
2155         case WMSZ_TOP:         dir = _NET_WM_MOVERESIZE_SIZE_TOP; break;
2156         case WMSZ_TOPLEFT:     dir = _NET_WM_MOVERESIZE_SIZE_TOPLEFT; break;
2157         case WMSZ_TOPRIGHT:    dir = _NET_WM_MOVERESIZE_SIZE_TOPRIGHT; break;
2158         case WMSZ_BOTTOM:      dir = _NET_WM_MOVERESIZE_SIZE_BOTTOM; break;
2159         case WMSZ_BOTTOMLEFT:  dir = _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT; break;
2160         case WMSZ_BOTTOMRIGHT: dir = _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT; break;
2161         default:               dir = _NET_WM_MOVERESIZE_SIZE_KEYBOARD; break;
2162         }
2163         break;
2164
2165     case SC_KEYMENU:
2166         /* prevent a simple ALT press+release from activating the system menu,
2167          * as that can get confusing on managed windows */
2168         if ((WCHAR)lparam) return -1;  /* got an explicit char */
2169         if (GetMenu( hwnd )) return -1;  /* window has a real menu */
2170         if (!(GetWindowLongW( hwnd, GWL_STYLE ) & WS_SYSMENU)) return -1;  /* no system menu */
2171         TRACE( "ignoring SC_KEYMENU wp %lx lp %lx\n", wparam, lparam );
2172         return 0;
2173
2174     default:
2175         return -1;
2176     }
2177
2178     if (IsZoomed(hwnd)) return -1;
2179
2180     if (!is_netwm_supported( display, x11drv_atom(_NET_WM_MOVERESIZE) ))
2181     {
2182         TRACE( "_NET_WM_MOVERESIZE not supported\n" );
2183         return -1;
2184     }
2185
2186     dwPoint = GetMessagePos();
2187     x = (short)LOWORD(dwPoint);
2188     y = (short)HIWORD(dwPoint);
2189
2190     TRACE("hwnd %p, x %d, y %d, dir %d\n", hwnd, x, y, dir);
2191
2192     xev.xclient.type = ClientMessage;
2193     xev.xclient.window = X11DRV_get_whole_window(hwnd);
2194     xev.xclient.message_type = x11drv_atom(_NET_WM_MOVERESIZE);
2195     xev.xclient.serial = 0;
2196     xev.xclient.display = display;
2197     xev.xclient.send_event = True;
2198     xev.xclient.format = 32;
2199     xev.xclient.data.l[0] = x; /* x coord */
2200     xev.xclient.data.l[1] = y; /* y coord */
2201     xev.xclient.data.l[2] = dir; /* direction */
2202     xev.xclient.data.l[3] = 1; /* button */
2203     xev.xclient.data.l[4] = 0; /* unused */
2204
2205     /* need to ungrab the pointer that may have been automatically grabbed
2206      * with a ButtonPress event */
2207     wine_tsx11_lock();
2208     XUngrabPointer( display, CurrentTime );
2209     XSendEvent(display, root_window, False, SubstructureNotifyMask, &xev);
2210     wine_tsx11_unlock();
2211     return 0;
2212 }