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