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