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