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