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