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