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