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