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