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