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