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