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