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