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