Keep track of per-column information inside the listview.
[wine] / dlls / x11drv / window.c
1 /*
2  * Window related functions
3  *
4  * Copyright 1993, 1994, 1995, 1996, 2001 Alexandre Julliard
5  * Copyright 1993 David Metcalfe
6  * Copyright 1995, 1996 Alex Korobka
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
22
23 #include "config.h"
24
25 #include <stdlib.h>
26
27 #include "ts_xlib.h"
28 #include <X11/Xresource.h>
29 #include <X11/Xutil.h>
30
31 #include "winbase.h"
32 #include "wingdi.h"
33 #include "winreg.h"
34 #include "winuser.h"
35 #include "wine/unicode.h"
36
37 #include "wine/debug.h"
38 #include "x11drv.h"
39 #include "win.h"
40 #include "winpos.h"
41 #include "dce.h"
42 #include "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 (!managed_mode) return FALSE;
78     /* tray window is always managed */
79     if (win->dwExStyle & WS_EX_TRAYWINDOW) return TRUE;
80     /* child windows are not managed */
81     if (win->dwStyle & WS_CHILD) return FALSE;
82     /* tool windows are not managed */
83     if (win->dwExStyle & WS_EX_TOOLWINDOW) return FALSE;
84     /* windows with caption or thick frame are managed */
85     if ((win->dwStyle & WS_CAPTION) == WS_CAPTION) return TRUE;
86     if (win->dwStyle & WS_THICKFRAME) return TRUE;
87     /* default: not managed */
88     return FALSE;
89 }
90
91
92 /***********************************************************************
93  *              is_client_window_mapped
94  *
95  * Check if the X client window should be mapped
96  */
97 inline static BOOL is_client_window_mapped( WND *win )
98 {
99     struct x11drv_win_data *data = win->pDriverData;
100     return !(win->dwStyle & WS_MINIMIZE) && !IsRectEmpty( &data->client_rect );
101 }
102
103
104 /***********************************************************************
105  *              get_window_attributes
106  *
107  * Fill the window attributes structure for an X window.
108  */
109 static int get_window_attributes( Display *display, WND *win, XSetWindowAttributes *attr )
110 {
111     BOOL is_top_level = is_window_top_level( win );
112     BOOL managed = is_top_level && is_window_managed( win );
113
114     if (managed) WIN_SetExStyle( win->hwndSelf, win->dwExStyle | WS_EX_MANAGED );
115     else WIN_SetExStyle( win->hwndSelf, win->dwExStyle & ~WS_EX_MANAGED );
116
117     attr->override_redirect = !managed;
118     attr->colormap          = X11DRV_PALETTE_PaletteXColormap;
119     attr->save_under        = ((win->clsStyle & CS_SAVEBITS) != 0);
120     attr->cursor            = x11drv_thread_data()->cursor;
121     attr->event_mask        = (ExposureMask | KeyPressMask | KeyReleaseMask | PointerMotionMask |
122                                ButtonPressMask | ButtonReleaseMask | EnterWindowMask);
123
124     if (is_window_top_level( win ))
125         attr->event_mask |= StructureNotifyMask | FocusChangeMask | KeymapStateMask;
126
127     return (CWOverrideRedirect | CWSaveUnder | CWEventMask | CWColormap | CWCursor);
128 }
129
130
131 /***********************************************************************
132  *              X11DRV_sync_window_style
133  *
134  * Change the X window attributes when the window style has changed.
135  */
136 void X11DRV_sync_window_style( Display *display, WND *win )
137 {
138     XSetWindowAttributes attr;
139     int mask;
140
141     wine_tsx11_lock();
142     mask = get_window_attributes( display, win, &attr );
143     XChangeWindowAttributes( display, get_whole_window(win), mask, &attr );
144     wine_tsx11_unlock();
145 }
146
147
148 /***********************************************************************
149  *              get_window_changes
150  *
151  * fill the window changes structure
152  */
153 static int get_window_changes( XWindowChanges *changes, const RECT *old, const RECT *new )
154 {
155     int mask = 0;
156
157     if (old->right - old->left != new->right - new->left )
158     {
159         if (!(changes->width = new->right - new->left)) changes->width = 1;
160         mask |= CWWidth;
161     }
162     if (old->bottom - old->top != new->bottom - new->top)
163     {
164         if (!(changes->height = new->bottom - new->top)) changes->height = 1;
165         mask |= CWHeight;
166     }
167     if (old->left != new->left)
168     {
169         changes->x = new->left;
170         mask |= CWX;
171     }
172     if (old->top != new->top)
173     {
174         changes->y = new->top;
175         mask |= CWY;
176     }
177     return mask;
178 }
179
180
181 /***********************************************************************
182  *              create_icon_window
183  */
184 static Window create_icon_window( Display *display, WND *win )
185 {
186     struct x11drv_win_data *data = win->pDriverData;
187     XSetWindowAttributes attr;
188
189     attr.event_mask = (ExposureMask | KeyPressMask | KeyReleaseMask | PointerMotionMask |
190                        ButtonPressMask | ButtonReleaseMask | EnterWindowMask);
191     attr.bit_gravity = NorthWestGravity;
192     attr.backing_store = NotUseful/*WhenMapped*/;
193     attr.colormap      = X11DRV_PALETTE_PaletteXColormap; /* Needed due to our visual */
194
195     wine_tsx11_lock();
196     data->icon_window = XCreateWindow( display, root_window, 0, 0,
197                                        GetSystemMetrics( SM_CXICON ),
198                                        GetSystemMetrics( SM_CYICON ),
199                                        0, screen_depth,
200                                        InputOutput, visual,
201                                        CWEventMask | CWBitGravity | CWBackingStore | CWColormap, &attr );
202     XSaveContext( display, data->icon_window, winContext, (char *)win->hwndSelf );
203     wine_tsx11_unlock();
204
205     TRACE( "created %lx\n", data->icon_window );
206     SetPropA( win->hwndSelf, icon_window_atom, (HANDLE)data->icon_window );
207     return data->icon_window;
208 }
209
210
211
212 /***********************************************************************
213  *              destroy_icon_window
214  */
215 inline static void destroy_icon_window( Display *display, WND *win )
216 {
217     struct x11drv_win_data *data = win->pDriverData;
218
219     if (!data->icon_window) return;
220     if (x11drv_thread_data()->cursor_window == data->icon_window)
221         x11drv_thread_data()->cursor_window = None;
222     wine_tsx11_lock();
223     XDeleteContext( display, data->icon_window, winContext );
224     XDestroyWindow( display, data->icon_window );
225     data->icon_window = 0;
226     wine_tsx11_unlock();
227     RemovePropA( win->hwndSelf, icon_window_atom );
228 }
229
230
231 /***********************************************************************
232  *              set_icon_hints
233  *
234  * Set the icon wm hints
235  */
236 static void set_icon_hints( Display *display, WND *wndPtr, XWMHints *hints )
237 {
238     X11DRV_WND_DATA *data = wndPtr->pDriverData;
239     HICON hIcon = (HICON)GetClassLongA( wndPtr->hwndSelf, GCL_HICON );
240
241     if (data->hWMIconBitmap) DeleteObject( data->hWMIconBitmap );
242     if (data->hWMIconMask) DeleteObject( data->hWMIconMask);
243     data->hWMIconBitmap = 0;
244     data->hWMIconMask = 0;
245
246     if (!(wndPtr->dwExStyle & WS_EX_MANAGED))
247     {
248         destroy_icon_window( display, wndPtr );
249         hints->flags &= ~(IconPixmapHint | IconMaskHint | IconWindowHint);
250     }
251     else if (!hIcon)
252     {
253         if (!data->icon_window) create_icon_window( display, wndPtr );
254         hints->icon_window = data->icon_window;
255         hints->flags = (hints->flags & ~(IconPixmapHint | IconMaskHint)) | IconWindowHint;
256     }
257     else
258     {
259         HBITMAP hbmOrig;
260         RECT rcMask;
261         BITMAP bmMask;
262         ICONINFO ii;
263         HDC hDC;
264
265         GetIconInfo(hIcon, &ii);
266
267         GetObjectA(ii.hbmMask, sizeof(bmMask), &bmMask);
268         rcMask.top    = 0;
269         rcMask.left   = 0;
270         rcMask.right  = bmMask.bmWidth;
271         rcMask.bottom = bmMask.bmHeight;
272
273         hDC = CreateCompatibleDC(0);
274         hbmOrig = SelectObject(hDC, ii.hbmMask);
275         InvertRect(hDC, &rcMask);
276         SelectObject(hDC, ii.hbmColor);  /* force the color bitmap to x11drv mode too */
277         SelectObject(hDC, hbmOrig);
278         DeleteDC(hDC);
279
280         data->hWMIconBitmap = ii.hbmColor;
281         data->hWMIconMask = ii.hbmMask;
282
283         hints->icon_pixmap = X11DRV_BITMAP_Pixmap(data->hWMIconBitmap);
284         hints->icon_mask = X11DRV_BITMAP_Pixmap(data->hWMIconMask);
285         destroy_icon_window( display, wndPtr );
286         hints->flags = (hints->flags & ~IconWindowHint) | IconPixmapHint | IconMaskHint;
287     }
288 }
289
290
291 /***********************************************************************
292  *              set_size_hints
293  *
294  * set the window size hints
295  */
296 static void set_size_hints( Display *display, WND *win )
297 {
298     XSizeHints* size_hints;
299     struct x11drv_win_data *data = win->pDriverData;
300
301     if ((size_hints = XAllocSizeHints()))
302     {
303         size_hints->win_gravity = StaticGravity;
304         size_hints->x = data->whole_rect.left;
305         size_hints->y = data->whole_rect.top;
306         size_hints->flags = PWinGravity | PPosition;
307
308         if (HAS_DLGFRAME( win->dwStyle, win->dwExStyle ))
309         {
310             size_hints->max_width = data->whole_rect.right - data->whole_rect.left;
311             size_hints->max_height = data->whole_rect.bottom - data->whole_rect.top;
312             size_hints->min_width = size_hints->max_width;
313             size_hints->min_height = size_hints->max_height;
314             size_hints->flags |= PMinSize | PMaxSize;
315         }
316         XSetWMNormalHints( display, data->whole_window, size_hints );
317         XFree( size_hints );
318     }
319 }
320
321
322 /***********************************************************************
323  *              X11DRV_set_wm_hints
324  *
325  * Set the window manager hints for a newly-created window
326  */
327 void X11DRV_set_wm_hints( Display *display, WND *win )
328 {
329     struct x11drv_win_data *data = win->pDriverData;
330     Window group_leader;
331     XClassHint *class_hints;
332     XWMHints* wm_hints;
333     Atom protocols[2];
334     int i;
335
336     wine_tsx11_lock();
337
338     /* wm protocols */
339     i = 0;
340     protocols[i++] = wmDeleteWindow;
341     if (wmTakeFocus) protocols[i++] = wmTakeFocus;
342     XSetWMProtocols( display, data->whole_window, protocols, i );
343
344     /* class hints */
345     if ((class_hints = XAllocClassHint()))
346     {
347         class_hints->res_name = "wine";
348         class_hints->res_class = "Wine";
349         XSetClassHint( display, data->whole_window, class_hints );
350         XFree( class_hints );
351     }
352
353     /* transient for hint */
354     if (win->owner)
355     {
356         Window owner_win = X11DRV_get_whole_window( win->owner );
357         XSetTransientForHint( display, data->whole_window, owner_win );
358         group_leader = owner_win;
359     }
360     else group_leader = data->whole_window;
361
362     /* size hints */
363     set_size_hints( display, win );
364
365     /* systray properties (KDE only for now) */
366     if (win->dwExStyle & WS_EX_TRAYWINDOW)
367     {
368         int val = 1;
369         if (kwmDockWindow != None)
370             XChangeProperty( display, data->whole_window, kwmDockWindow, kwmDockWindow,
371                              32, PropModeReplace, (char*)&val, 1 );
372         if (_kde_net_wm_system_tray_window_for != None)
373             XChangeProperty( display, data->whole_window, _kde_net_wm_system_tray_window_for,
374                              XA_WINDOW, 32, PropModeReplace, (char*)&data->whole_window, 1 );
375     }
376
377     if (mwmHints != None)
378     {
379         MwmHints mwm_hints;
380         mwm_hints.flags = MWM_HINTS_FUNCTIONS | MWM_HINTS_DECORATIONS;
381         mwm_hints.functions = 0;
382         if ((win->dwStyle & WS_CAPTION) == WS_CAPTION) mwm_hints.functions |= MWM_FUNC_MOVE;
383         if (win->dwStyle & WS_THICKFRAME) mwm_hints.functions |= MWM_FUNC_MOVE | MWM_FUNC_RESIZE;
384         if (win->dwStyle & WS_MINIMIZEBOX) mwm_hints.functions |= MWM_FUNC_MINIMIZE;
385         if (win->dwStyle & WS_MAXIMIZEBOX) mwm_hints.functions |= MWM_FUNC_MAXIMIZE;
386         if (win->dwStyle & WS_SYSMENU)    mwm_hints.functions |= MWM_FUNC_CLOSE;
387         mwm_hints.decorations = 0;
388         if ((win->dwStyle & WS_CAPTION) == WS_CAPTION) mwm_hints.decorations |= MWM_DECOR_TITLE;
389         if (win->dwExStyle & WS_EX_DLGMODALFRAME) mwm_hints.decorations |= MWM_DECOR_BORDER;
390         else if (win->dwStyle & WS_THICKFRAME) mwm_hints.decorations |= MWM_DECOR_BORDER | MWM_DECOR_RESIZEH;
391         else if ((win->dwStyle & (WS_DLGFRAME|WS_BORDER)) == WS_DLGFRAME) mwm_hints.decorations |= MWM_DECOR_BORDER;
392         else if (win->dwStyle & WS_BORDER) mwm_hints.decorations |= MWM_DECOR_BORDER;
393         else if (!(win->dwStyle & (WS_CHILD|WS_POPUP))) mwm_hints.decorations |= MWM_DECOR_BORDER;
394         if (win->dwStyle & WS_SYSMENU)  mwm_hints.decorations |= MWM_DECOR_MENU;
395         if (win->dwStyle & WS_MINIMIZEBOX) mwm_hints.decorations |= MWM_DECOR_MINIMIZE;
396         if (win->dwStyle & WS_MAXIMIZEBOX) mwm_hints.decorations |= MWM_DECOR_MAXIMIZE;
397
398         XChangeProperty( display, data->whole_window, mwmHints, mwmHints, 32,
399                          PropModeReplace, (char*)&mwm_hints, sizeof(mwm_hints)/sizeof(long) );
400     }
401
402     wm_hints = XAllocWMHints();
403     wine_tsx11_unlock();
404
405     /* wm hints */
406     if (wm_hints)
407     {
408         wm_hints->flags = InputHint | StateHint | WindowGroupHint;
409         wm_hints->input = !(win->dwStyle & WS_DISABLED);
410
411         set_icon_hints( display, win, wm_hints );
412
413         wm_hints->initial_state = (win->dwStyle & WS_MINIMIZE) ? IconicState : NormalState;
414         wm_hints->window_group = group_leader;
415
416         wine_tsx11_lock();
417         XSetWMHints( display, data->whole_window, wm_hints );
418         XFree(wm_hints);
419         wine_tsx11_unlock();
420     }
421 }
422
423
424 /***********************************************************************
425  *              X11DRV_set_iconic_state
426  *
427  * Set the X11 iconic state according to the window style.
428  */
429 void X11DRV_set_iconic_state( WND *win )
430 {
431     Display *display = thread_display();
432     struct x11drv_win_data *data = win->pDriverData;
433     XWMHints* wm_hints;
434     BOOL iconic = IsIconic( win->hwndSelf );
435
436     wine_tsx11_lock();
437
438     if (iconic) XUnmapWindow( display, data->client_window );
439     else if (is_client_window_mapped( win )) XMapWindow( display, data->client_window );
440
441     if (!(wm_hints = XGetWMHints( display, data->whole_window ))) wm_hints = XAllocWMHints();
442     wm_hints->flags |= StateHint | IconPositionHint;
443     wm_hints->initial_state = iconic ? IconicState : NormalState;
444     wm_hints->icon_x = win->rectWindow.left;
445     wm_hints->icon_y = win->rectWindow.top;
446     XSetWMHints( display, data->whole_window, wm_hints );
447
448     if (win->dwStyle & WS_VISIBLE)
449     {
450         if (iconic)
451             XIconifyWindow( display, data->whole_window, DefaultScreen(display) );
452         else
453             if (!IsRectEmpty( &win->rectWindow )) XMapWindow( display, data->whole_window );
454     }
455
456     XFree(wm_hints);
457     wine_tsx11_unlock();
458 }
459
460
461 /***********************************************************************
462  *              X11DRV_window_to_X_rect
463  *
464  * Convert a rect from client to X window coordinates
465  */
466 void X11DRV_window_to_X_rect( WND *win, RECT *rect )
467 {
468     RECT rc;
469
470     if (!(win->dwExStyle & WS_EX_MANAGED)) return;
471     if (IsRectEmpty( rect )) return;
472
473     rc.top = rc.bottom = rc.left = rc.right = 0;
474
475     AdjustWindowRectEx( &rc, win->dwStyle & ~(WS_HSCROLL|WS_VSCROLL), FALSE, win->dwExStyle );
476
477     rect->left   -= rc.left;
478     rect->right  -= rc.right;
479     rect->top    -= rc.top;
480     rect->bottom -= rc.bottom;
481     if (rect->top >= rect->bottom) rect->bottom = rect->top + 1;
482     if (rect->left >= rect->right) rect->right = rect->left + 1;
483 }
484
485
486 /***********************************************************************
487  *              X11DRV_X_to_window_rect
488  *
489  * Opposite of X11DRV_window_to_X_rect
490  */
491 void X11DRV_X_to_window_rect( WND *win, RECT *rect )
492 {
493     if (!(win->dwExStyle & WS_EX_MANAGED)) return;
494     if (IsRectEmpty( rect )) return;
495
496     AdjustWindowRectEx( rect, win->dwStyle & ~(WS_HSCROLL|WS_VSCROLL), FALSE, win->dwExStyle );
497
498     if (rect->top >= rect->bottom) rect->bottom = rect->top + 1;
499     if (rect->left >= rect->right) rect->right = rect->left + 1;
500 }
501
502
503 /***********************************************************************
504  *              X11DRV_sync_whole_window_position
505  *
506  * Synchronize the X whole window position with the Windows one
507  */
508 int X11DRV_sync_whole_window_position( Display *display, WND *win, int zorder )
509 {
510     XWindowChanges changes;
511     int mask;
512     struct x11drv_win_data *data = win->pDriverData;
513     RECT whole_rect = win->rectWindow;
514
515     X11DRV_window_to_X_rect( win, &whole_rect );
516     mask = get_window_changes( &changes, &data->whole_rect, &whole_rect );
517
518     if (zorder)
519     {
520         /* find window that this one must be after */
521         HWND prev = GetWindow( win->hwndSelf, GW_HWNDPREV );
522         while (prev && !(GetWindowLongW( prev, GWL_STYLE ) & WS_VISIBLE))
523             prev = GetWindow( prev, GW_HWNDPREV );
524         if (!prev)  /* top child */
525         {
526             changes.stack_mode = Above;
527             mask |= CWStackMode;
528         }
529         else
530         {
531             changes.stack_mode = Below;
532             changes.sibling = X11DRV_get_whole_window(prev);
533             mask |= CWStackMode | CWSibling;
534         }
535     }
536
537     data->whole_rect = whole_rect;
538
539     if (mask)
540     {
541         TRACE( "setting win %lx pos %d,%d,%dx%d after %lx changes=%x\n",
542                data->whole_window, whole_rect.left, whole_rect.top,
543                whole_rect.right - whole_rect.left, whole_rect.bottom - whole_rect.top,
544                changes.sibling, mask );
545         wine_tsx11_lock();
546         XSync( gdi_display, False );  /* flush graphics operations before moving the window */
547         if (is_window_top_level( win ))
548         {
549             if (mask & (CWWidth|CWHeight)) set_size_hints( display, win );
550             XReconfigureWMWindow( display, data->whole_window,
551                                   DefaultScreen(display), mask, &changes );
552         }
553         else XConfigureWindow( display, data->whole_window, mask, &changes );
554         wine_tsx11_unlock();
555     }
556     return mask;
557 }
558
559
560 /***********************************************************************
561  *              X11DRV_sync_client_window_position
562  *
563  * Synchronize the X client window position with the Windows one
564  */
565 int X11DRV_sync_client_window_position( Display *display, WND *win )
566 {
567     XWindowChanges changes;
568     int mask;
569     struct x11drv_win_data *data = win->pDriverData;
570     RECT client_rect = win->rectClient;
571
572     OffsetRect( &client_rect, -data->whole_rect.left, -data->whole_rect.top );
573
574     if ((mask = get_window_changes( &changes, &data->client_rect, &client_rect )))
575     {
576         BOOL was_mapped = is_client_window_mapped( win );
577
578         TRACE( "setting win %lx pos %d,%d,%dx%d (was %d,%d,%dx%d) after %lx changes=%x\n",
579                data->client_window, client_rect.left, client_rect.top,
580                client_rect.right - client_rect.left, client_rect.bottom - client_rect.top,
581                data->client_rect.left, data->client_rect.top,
582                data->client_rect.right - data->client_rect.left,
583                data->client_rect.bottom - data->client_rect.top,
584                changes.sibling, mask );
585         data->client_rect = client_rect;
586         wine_tsx11_lock();
587         XSync( gdi_display, False );  /* flush graphics operations before moving the window */
588         if (was_mapped && !is_client_window_mapped( win ))
589             XUnmapWindow( display, data->client_window );
590         XConfigureWindow( display, data->client_window, mask, &changes );
591         if (!was_mapped && is_client_window_mapped( win ))
592             XMapWindow( display, data->client_window );
593         wine_tsx11_unlock();
594     }
595     return mask;
596 }
597
598
599 /***********************************************************************
600  *              X11DRV_register_window
601  *
602  * Associate an X window to a HWND.
603  */
604 void X11DRV_register_window( Display *display, HWND hwnd, struct x11drv_win_data *data )
605 {
606     wine_tsx11_lock();
607     XSaveContext( display, data->whole_window, winContext, (char *)hwnd );
608     XSaveContext( display, data->client_window, winContext, (char *)hwnd );
609     wine_tsx11_unlock();
610 }
611
612
613 /**********************************************************************
614  *              create_desktop
615  */
616 static void create_desktop( Display *display, WND *wndPtr, CREATESTRUCTA *cs )
617 {
618     X11DRV_WND_DATA *data = wndPtr->pDriverData;
619
620     wine_tsx11_lock();
621     winContext     = XUniqueContext();
622     wmProtocols    = XInternAtom( display, "WM_PROTOCOLS", False );
623     wmDeleteWindow = XInternAtom( display, "WM_DELETE_WINDOW", False );
624     if (use_take_focus) wmTakeFocus = XInternAtom( display, "WM_TAKE_FOCUS", False );
625     dndProtocol = XInternAtom( display, "DndProtocol" , False );
626     dndSelection = XInternAtom( display, "DndSelection" , False );
627     wmChangeState = XInternAtom( display, "WM_CHANGE_STATE", False );
628     mwmHints = XInternAtom( display, _XA_MWM_HINTS, False );
629     kwmDockWindow = XInternAtom( display, "KWM_DOCKWINDOW", False );
630     _kde_net_wm_system_tray_window_for = XInternAtom( display, "_KDE_NET_WM_SYSTEM_TRAY_WINDOW_FOR", False );
631     wine_tsx11_unlock();
632
633     whole_window_atom  = MAKEINTATOMA( GlobalAddAtomA( "__wine_x11_whole_window" ));
634     client_window_atom = MAKEINTATOMA( GlobalAddAtomA( "__wine_x11_client_window" ));
635     icon_window_atom   = MAKEINTATOMA( GlobalAddAtomA( "__wine_x11_icon_window" ));
636
637     data->whole_window = data->client_window = root_window;
638     data->whole_rect = data->client_rect = wndPtr->rectWindow;
639
640     SetPropA( wndPtr->hwndSelf, whole_window_atom, (HANDLE)root_window );
641     SetPropA( wndPtr->hwndSelf, client_window_atom, (HANDLE)root_window );
642     SetPropA( wndPtr->hwndSelf, "__wine_x11_visual_id", (HANDLE)XVisualIDFromVisual(visual) );
643
644     SendMessageW( wndPtr->hwndSelf, WM_NCCREATE, 0, (LPARAM)cs );
645     if (root_window != DefaultRootWindow(display)) X11DRV_create_desktop_thread();
646 }
647
648
649 /**********************************************************************
650  *              create_whole_window
651  *
652  * Create the whole X window for a given window
653  */
654 static Window create_whole_window( Display *display, WND *win )
655 {
656     struct x11drv_win_data *data = win->pDriverData;
657     int cx, cy, mask;
658     XSetWindowAttributes attr;
659     Window parent;
660     RECT rect;
661     BOOL is_top_level = is_window_top_level( win );
662
663     rect = win->rectWindow;
664     X11DRV_window_to_X_rect( win, &rect );
665
666     if (!(cx = rect.right - rect.left)) cx = 1;
667     if (!(cy = rect.bottom - rect.top)) cy = 1;
668
669     parent = X11DRV_get_client_window( win->parent );
670
671     wine_tsx11_lock();
672
673     mask = get_window_attributes( display, win, &attr );
674
675     /* set the attributes that don't change over the lifetime of the window */
676     attr.bit_gravity       = ForgetGravity;
677     attr.win_gravity       = NorthWestGravity;
678     attr.backing_store     = NotUseful/*WhenMapped*/;
679     mask |= CWBitGravity | CWWinGravity | CWBackingStore;
680
681     data->whole_rect = rect;
682     data->whole_window = XCreateWindow( display, parent, rect.left, rect.top, cx, cy,
683                                         0, screen_depth, InputOutput, visual,
684                                         mask, &attr );
685
686     if (!data->whole_window)
687     {
688         wine_tsx11_unlock();
689         return 0;
690     }
691
692     /* non-maximized child must be at bottom of Z order */
693     if ((win->dwStyle & (WS_CHILD|WS_MAXIMIZE)) == WS_CHILD)
694     {
695         XWindowChanges changes;
696         changes.stack_mode = Below;
697         XConfigureWindow( display, data->whole_window, CWStackMode, &changes );
698     }
699
700     wine_tsx11_unlock();
701
702     if (is_top_level) X11DRV_set_wm_hints( display, win );
703
704     return data->whole_window;
705 }
706
707
708 /**********************************************************************
709  *              create_client_window
710  *
711  * Create the client window for a given window
712  */
713 static Window create_client_window( Display *display, WND *win )
714 {
715     struct x11drv_win_data *data = win->pDriverData;
716     RECT rect = data->whole_rect;
717     XSetWindowAttributes attr;
718
719     OffsetRect( &rect, -data->whole_rect.left, -data->whole_rect.top );
720     data->client_rect = rect;
721
722     attr.event_mask = (ExposureMask | KeyPressMask | KeyReleaseMask | PointerMotionMask |
723                        ButtonPressMask | ButtonReleaseMask | EnterWindowMask);
724     attr.bit_gravity = (win->clsStyle & (CS_VREDRAW | CS_HREDRAW)) ?
725                        ForgetGravity : NorthWestGravity;
726     attr.backing_store = NotUseful/*WhenMapped*/;
727
728     wine_tsx11_lock();
729     data->client_window = XCreateWindow( display, data->whole_window, 0, 0,
730                                          max( rect.right - rect.left, 1 ),
731                                          max( rect.bottom - rect.top, 1 ),
732                                          0, screen_depth,
733                                          InputOutput, visual,
734                                          CWEventMask | CWBitGravity | CWBackingStore, &attr );
735     if (data->client_window && is_client_window_mapped( win ))
736         XMapWindow( display, data->client_window );
737     wine_tsx11_unlock();
738     return data->client_window;
739 }
740
741
742 /*****************************************************************
743  *              SetWindowText   (X11DRV.@)
744  */
745 BOOL X11DRV_SetWindowText( HWND hwnd, LPCWSTR text )
746 {
747     Display *display = thread_display();
748     UINT count;
749     char *buffer;
750     char *utf8_buffer;
751     static UINT text_cp = (UINT)-1;
752     Window win;
753
754     if ((win = X11DRV_get_whole_window( hwnd )))
755     {
756         if (text_cp == (UINT)-1)
757         {
758             HKEY hkey;
759             /* default value */
760             text_cp = CP_ACP;
761             if(!RegOpenKeyA(HKEY_LOCAL_MACHINE, "Software\\Wine\\Wine\\Config\\x11drv", &hkey))
762             {
763                 char buffer[20];
764                 DWORD type, count = sizeof(buffer);
765                 if(!RegQueryValueExA(hkey, "TextCP", 0, &type, buffer, &count))
766                     text_cp = atoi(buffer);
767                 RegCloseKey(hkey);
768             }
769             TRACE("text_cp = %u\n", text_cp);
770         }
771
772         /* allocate new buffer for window text */
773         count = WideCharToMultiByte(text_cp, 0, text, -1, NULL, 0, NULL, NULL);
774         if (!(buffer = HeapAlloc( GetProcessHeap(), 0, count )))
775         {
776             ERR("Not enough memory for window text\n");
777             return FALSE;
778         }
779         WideCharToMultiByte(text_cp, 0, text, -1, buffer, count, NULL, NULL);
780
781         count = WideCharToMultiByte(CP_UTF8, 0, text, strlenW(text), NULL, 0, NULL, NULL);
782         if (!(utf8_buffer = HeapAlloc( GetProcessHeap(), 0, count )))
783         {
784             ERR("Not enough memory for window text in UTF-8\n");
785             return FALSE;
786         }
787         WideCharToMultiByte(CP_UTF8, 0, text, strlenW(text), utf8_buffer, count, NULL, NULL);
788
789         wine_tsx11_lock();
790         XStoreName( display, win, buffer );
791         XSetIconName( display, win, buffer );
792         /*
793         Implements a NET_WM UTF-8 title. It should be without a trailing \0,
794         according to the standard
795         ( http://www.pps.jussieu.fr/~jch/software/UTF8_STRING/UTF8_STRING.text ).
796         */
797         XChangeProperty( display, win,
798             XInternAtom(display, "_NET_WM_NAME", False),
799             XInternAtom(display, "UTF8_STRING", False),
800             8, PropModeReplace, (unsigned char *) utf8_buffer,
801             count);
802         wine_tsx11_unlock();
803
804         HeapFree( GetProcessHeap(), 0, utf8_buffer );
805         HeapFree( GetProcessHeap(), 0, buffer );
806     }
807     return TRUE;
808 }
809
810
811 /***********************************************************************
812  *              DestroyWindow   (X11DRV.@)
813  */
814 BOOL X11DRV_DestroyWindow( HWND hwnd )
815 {
816     struct x11drv_thread_data *thread_data = x11drv_thread_data();
817     Display *display = thread_data->display;
818     WND *wndPtr = WIN_GetPtr( hwnd );
819     X11DRV_WND_DATA *data = wndPtr->pDriverData;
820
821     if (!data) goto done;
822
823     if (data->whole_window)
824     {
825         TRACE( "win %x xwin %lx/%lx\n", hwnd, data->whole_window, data->client_window );
826         if (thread_data->cursor_window == data->whole_window) thread_data->cursor_window = None;
827         if (thread_data->last_focus == hwnd) thread_data->last_focus = 0;
828         wine_tsx11_lock();
829         XSync( gdi_display, False );  /* flush any reference to this drawable in GDI queue */
830         XDeleteContext( display, data->whole_window, winContext );
831         XDeleteContext( display, data->client_window, winContext );
832         XDestroyWindow( display, data->whole_window );  /* this destroys client too */
833         destroy_icon_window( display, wndPtr );
834         wine_tsx11_unlock();
835     }
836
837     if (data->hWMIconBitmap) DeleteObject( data->hWMIconBitmap );
838     if (data->hWMIconMask) DeleteObject( data->hWMIconMask);
839     HeapFree( GetProcessHeap(), 0, data );
840     wndPtr->pDriverData = NULL;
841  done:
842     WIN_ReleasePtr( wndPtr );
843     return TRUE;
844 }
845
846
847 /**********************************************************************
848  *              CreateWindow   (X11DRV.@)
849  */
850 BOOL X11DRV_CreateWindow( HWND hwnd, CREATESTRUCTA *cs, BOOL unicode )
851 {
852     HWND hwndLinkAfter;
853     Display *display = thread_display();
854     WND *wndPtr;
855     struct x11drv_win_data *data;
856     RECT rect;
857     BOOL ret = FALSE;
858
859     if (cs->cx > 65535)
860     {
861         ERR( "invalid window width %d\n", cs->cx );
862         cs->cx = 65535;
863     }
864     if (cs->cy > 65535)
865     {
866         ERR( "invalid window height %d\n", cs->cx );
867         cs->cy = 65535;
868     }
869
870     if (!(data = HeapAlloc(GetProcessHeap(), 0, sizeof(*data)))) return FALSE;
871     data->whole_window  = 0;
872     data->client_window = 0;
873     data->icon_window   = 0;
874     data->hWMIconBitmap = 0;
875     data->hWMIconMask   = 0;
876
877     wndPtr = WIN_GetPtr( hwnd );
878     wndPtr->pDriverData = data;
879
880     /* initialize the dimensions before sending WM_GETMINMAXINFO */
881     SetRect( &rect, cs->x, cs->y, cs->x + cs->cx, cs->y + cs->cy );
882     WIN_SetRectangles( hwnd, &rect, &rect );
883
884     if (!wndPtr->parent)
885     {
886         create_desktop( display, wndPtr, cs );
887         WIN_ReleasePtr( wndPtr );
888         return TRUE;
889     }
890
891     if (!create_whole_window( display, wndPtr )) goto failed;
892     if (!create_client_window( display, wndPtr )) goto failed;
893     TSXSync( display, False );
894
895     SetPropA( hwnd, whole_window_atom, (HANDLE)data->whole_window );
896     SetPropA( hwnd, client_window_atom, (HANDLE)data->client_window );
897
898     /* Call the WH_CBT hook */
899
900     hwndLinkAfter = ((cs->style & (WS_CHILD|WS_MAXIMIZE)) == WS_CHILD)
901  ? HWND_BOTTOM : HWND_TOP;
902
903     if (HOOK_IsHooked( WH_CBT ))
904     {
905         CBT_CREATEWNDA cbtc;
906         LRESULT lret;
907
908         cbtc.lpcs = cs;
909         cbtc.hwndInsertAfter = hwndLinkAfter;
910         lret = (unicode) ? HOOK_CallHooksW(WH_CBT, HCBT_CREATEWND,
911                                                        (WPARAM)hwnd, (LPARAM)&cbtc)
912                         : HOOK_CallHooksA(WH_CBT, HCBT_CREATEWND,
913                                                        (WPARAM)hwnd, (LPARAM)&cbtc);
914         if (lret)
915         {
916             TRACE("CBT-hook returned !0\n");
917             goto failed;
918         }
919     }
920
921
922
923     /* Send the WM_GETMINMAXINFO message and fix the size if needed */
924     if ((cs->style & WS_THICKFRAME) || !(cs->style & (WS_POPUP | WS_CHILD)))
925     {
926         POINT maxSize, maxPos, minTrack, maxTrack;
927
928         WIN_ReleasePtr( wndPtr );
929         WINPOS_GetMinMaxInfo( hwnd, &maxSize, &maxPos, &minTrack, &maxTrack);
930         if (maxSize.x < cs->cx) cs->cx = maxSize.x;
931         if (maxSize.y < cs->cy) cs->cy = maxSize.y;
932         if (cs->cx < minTrack.x ) cs->cx = minTrack.x;
933         if (cs->cy < minTrack.y ) cs->cy = minTrack.y;
934         if (cs->cx < 0) cs->cx = 0;
935         if (cs->cy < 0) cs->cy = 0;
936
937         if (!(wndPtr = WIN_GetPtr( hwnd ))) return FALSE;
938         SetRect( &rect, cs->x, cs->y, cs->x + cs->cx, cs->y + cs->cy );
939         WIN_SetRectangles( hwnd, &rect, &rect );
940         X11DRV_sync_whole_window_position( display, wndPtr, 0 );
941     }
942     WIN_ReleasePtr( wndPtr );
943
944     /* send WM_NCCREATE */
945     TRACE( "hwnd %x cs %d,%d %dx%d\n", hwnd, cs->x, cs->y, cs->cx, cs->cy );
946     if (unicode)
947         ret = SendMessageW( hwnd, WM_NCCREATE, 0, (LPARAM)cs );
948     else
949         ret = SendMessageA( hwnd, WM_NCCREATE, 0, (LPARAM)cs );
950     if (!ret)
951     {
952         WARN("aborted by WM_xxCREATE!\n");
953         return FALSE;
954     }
955
956     if (!(wndPtr = WIN_GetPtr(hwnd))) return FALSE;
957
958     X11DRV_sync_window_style( display, wndPtr );
959
960     /* send WM_NCCALCSIZE */
961     rect = wndPtr->rectWindow;
962     WIN_ReleasePtr( wndPtr );
963     SendMessageW( hwnd, WM_NCCALCSIZE, FALSE, (LPARAM)&rect );
964
965     if (!(wndPtr = WIN_GetPtr(hwnd))) return FALSE;
966     if (rect.left > rect.right || rect.top > rect.bottom) rect = wndPtr->rectWindow;
967     WIN_SetRectangles( hwnd, &wndPtr->rectWindow, &rect );
968     X11DRV_sync_client_window_position( display, wndPtr );
969     X11DRV_register_window( display, hwnd, data );
970
971     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",
972            hwnd, wndPtr->rectWindow.left, wndPtr->rectWindow.top,
973            wndPtr->rectWindow.right, wndPtr->rectWindow.bottom,
974            wndPtr->rectClient.left, wndPtr->rectClient.top,
975            wndPtr->rectClient.right, wndPtr->rectClient.bottom,
976            data->whole_rect.left, data->whole_rect.top,
977            data->whole_rect.right, data->whole_rect.bottom,
978            data->client_rect.left, data->client_rect.top,
979            data->client_rect.right, data->client_rect.bottom,
980            (unsigned int)data->whole_window, (unsigned int)data->client_window );
981
982     if ((wndPtr->dwStyle & (WS_CHILD|WS_MAXIMIZE)) == WS_CHILD)
983         WIN_LinkWindow( hwnd, wndPtr->parent, HWND_BOTTOM );
984     else
985         WIN_LinkWindow( hwnd, wndPtr->parent, HWND_TOP );
986
987     WIN_ReleasePtr( wndPtr );
988
989     if (unicode)
990         ret = (SendMessageW( hwnd, WM_CREATE, 0, (LPARAM)cs ) != -1);
991     else
992         ret = (SendMessageA( hwnd, WM_CREATE, 0, (LPARAM)cs ) != -1);
993
994     if (!ret)
995     {
996         WIN_UnlinkWindow( hwnd );
997         return FALSE;
998     }
999
1000     /* Send the size messages */
1001
1002     if (!(wndPtr = WIN_FindWndPtr(hwnd))) return FALSE;
1003     if (!(wndPtr->flags & WIN_NEED_SIZE))
1004     {
1005         /* send it anyway */
1006         if (((wndPtr->rectClient.right-wndPtr->rectClient.left) <0)
1007             ||((wndPtr->rectClient.bottom-wndPtr->rectClient.top)<0))
1008             WARN("sending bogus WM_SIZE message 0x%08lx\n",
1009                  MAKELONG(wndPtr->rectClient.right-wndPtr->rectClient.left,
1010                           wndPtr->rectClient.bottom-wndPtr->rectClient.top));
1011         SendMessageW( hwnd, WM_SIZE, SIZE_RESTORED,
1012                       MAKELONG(wndPtr->rectClient.right-wndPtr->rectClient.left,
1013                                wndPtr->rectClient.bottom-wndPtr->rectClient.top));
1014         SendMessageW( hwnd, WM_MOVE, 0,
1015                       MAKELONG( wndPtr->rectClient.left, wndPtr->rectClient.top ) );
1016     }
1017
1018     /* Show the window, maximizing or minimizing if needed */
1019
1020     if (wndPtr->dwStyle & (WS_MINIMIZE | WS_MAXIMIZE))
1021     {
1022         extern UINT WINPOS_MinMaximize( HWND hwnd, UINT cmd, LPRECT rect ); /*FIXME*/
1023
1024         RECT newPos;
1025         UINT swFlag = (wndPtr->dwStyle & WS_MINIMIZE) ? SW_MINIMIZE : SW_MAXIMIZE;
1026         WIN_SetStyle( hwnd, wndPtr->dwStyle & ~(WS_MAXIMIZE | WS_MINIMIZE) );
1027         WINPOS_MinMaximize( hwnd, swFlag, &newPos );
1028         swFlag = ((wndPtr->dwStyle & WS_CHILD) || GetActiveWindow())
1029             ? SWP_NOACTIVATE | SWP_NOZORDER | SWP_FRAMECHANGED
1030             : SWP_NOZORDER | SWP_FRAMECHANGED;
1031         SetWindowPos( hwnd, 0, newPos.left, newPos.top,
1032                       newPos.right, newPos.bottom, swFlag );
1033     }
1034
1035     /* if the window was made visible set create struct flag so that
1036      * we do a proper ShowWindow later on */
1037     if (wndPtr->dwStyle & WS_VISIBLE) cs->style |= WS_VISIBLE;
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 )) X11DRV_set_wm_hints( display, wndPtr );
1129         wine_tsx11_lock();
1130         X11DRV_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 = (HICON)SetClassLongW(hwnd, small ? GCL_HICONSM : GCL_HICON, (LONG)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;
1220
1221         wine_tsx11_lock();
1222         if (!(wm_hints = XGetWMHints( display, win ))) wm_hints = XAllocWMHints();
1223         wine_tsx11_unlock();
1224         if (wm_hints)
1225         {
1226             set_icon_hints( display, wndPtr, wm_hints );
1227             wine_tsx11_lock();
1228             XSetWMHints( display, win, wm_hints );
1229             XFree( wm_hints );
1230             wine_tsx11_unlock();
1231         }
1232     }
1233     WIN_ReleasePtr( wndPtr );
1234     return old;
1235 }