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