Well, SetParent doesn't work properly at all, but at least it must preserve
[wine] / windows / x11drv / wnd.c
1 /*
2  * X11 windows driver
3  *
4  * Copyright 1993, 1994, 1995, 1996 Alexandre Julliard
5  *                             1993 David Metcalfe
6  *                       1995, 1996 Alex Korobka
7  */
8
9 #include "config.h"
10
11 #include <X11/Xatom.h>
12
13 #include "ts_xlib.h"
14 #include "ts_xutil.h"
15
16 #include <stdlib.h>
17 #include <string.h>
18
19 #include "bitmap.h"
20 #include "color.h"
21 #include "debugtools.h"
22 #include "dce.h"
23 #include "options.h"
24 #include "message.h"
25 #include "heap.h"
26 #include "win.h"
27 #include "windef.h"
28 #include "class.h"
29 #include "x11drv.h"
30 #include "wingdi.h"
31 #include "wine/winuser16.h"
32
33 DEFAULT_DEBUG_CHANNEL(win);
34
35   /* Some useful macros */
36 #define HAS_DLGFRAME(style,exStyle) \
37 ((!((style) & WS_THICKFRAME)) && (((style) & WS_DLGFRAME) || ((exStyle) & WS_EX_DLGMODALFRAME)))
38
39 /**********************************************************************/
40
41 extern Cursor X11DRV_MOUSE_XCursor;  /* Current X cursor */
42 extern BOOL   X11DRV_CreateBitmap( HBITMAP );
43 extern Pixmap X11DRV_BITMAP_Pixmap( HBITMAP );
44
45 /**********************************************************************/
46
47 /* X context to associate a hwnd to an X window */
48 XContext winContext = 0;
49
50 Atom wmProtocols = None;
51 Atom wmDeleteWindow = None;
52 Atom dndProtocol = None;
53 Atom dndSelection = None;
54 Atom wmChangeState = None;
55
56 Atom kwmDockWindow = None;
57
58 /***********************************************************************
59  *              X11DRV_WND_GetXWindow
60  *
61  * Return the X window associated to a window.
62  */
63 Window X11DRV_WND_GetXWindow(WND *wndPtr)
64 {
65     return wndPtr && wndPtr->pDriverData ? 
66       ((X11DRV_WND_DATA *) wndPtr->pDriverData)->window : 0;
67 }
68
69 /***********************************************************************
70  *              X11DRV_WND_FindXWindow
71  *
72  * Return the the first X window associated to a window chain.
73  */
74 Window X11DRV_WND_FindXWindow(WND *wndPtr)
75 {
76     while (wndPtr && 
77            !((X11DRV_WND_DATA *) wndPtr->pDriverData)->window) 
78       wndPtr = wndPtr->parent;
79     return wndPtr ?
80       ((X11DRV_WND_DATA *) wndPtr->pDriverData)->window : 0;
81 }
82
83 /***********************************************************************
84  *              X11DRV_WND_RegisterWindow
85  *
86  * Associate an X window to a HWND.
87  */
88 static void X11DRV_WND_RegisterWindow(WND *wndPtr)
89 {
90   TSXSetWMProtocols( display, X11DRV_WND_GetXWindow(wndPtr), &wmDeleteWindow, 1 );
91   
92   if (!winContext) winContext = TSXUniqueContext();
93   TSXSaveContext( display, X11DRV_WND_GetXWindow(wndPtr), 
94                   winContext, (char *) wndPtr->hwndSelf );
95 }
96
97 /**********************************************************************
98  *              X11DRV_WND_Initialize
99  */
100 void X11DRV_WND_Initialize(WND *wndPtr)
101 {
102   X11DRV_WND_DATA *pWndDriverData = 
103     (X11DRV_WND_DATA *) HeapAlloc(SystemHeap, 0, sizeof(X11DRV_WND_DATA));
104
105   wndPtr->pDriverData = (void *) pWndDriverData;
106
107   pWndDriverData->window = 0;
108 }
109
110 /**********************************************************************
111  *              X11DRV_WND_Finalize
112  */
113 void X11DRV_WND_Finalize(WND *wndPtr)
114 {
115   X11DRV_WND_DATA *pWndDriverData =
116     (X11DRV_WND_DATA *) wndPtr->pDriverData;
117
118   if (!wndPtr->pDriverData) {
119      ERR("Trying to destroy window again. Not good.\n");
120      return;
121   }
122   if(pWndDriverData->window)
123     {
124       ERR(
125           "WND destroyed without destroying "
126           "the associated X Window (%ld)\n", 
127           pWndDriverData->window
128       );
129     }
130   HeapFree(SystemHeap, 0, wndPtr->pDriverData);
131   wndPtr->pDriverData = NULL;
132 }
133
134 /**********************************************************************
135  *              X11DRV_WND_CreateDesktopWindow
136  */
137 BOOL X11DRV_WND_CreateDesktopWindow(WND *wndPtr, CLASS *classPtr, BOOL bUnicode)
138 {
139     if (wmProtocols == None)
140         wmProtocols = TSXInternAtom( display, "WM_PROTOCOLS", True );
141     if (wmDeleteWindow == None)
142         wmDeleteWindow = TSXInternAtom( display, "WM_DELETE_WINDOW", True );
143     if( dndProtocol == None )
144         dndProtocol = TSXInternAtom( display, "DndProtocol" , False );
145     if( dndSelection == None )
146         dndSelection = TSXInternAtom( display, "DndSelection" , False );
147     if( wmChangeState == None )
148         wmChangeState = TSXInternAtom (display, "WM_CHANGE_STATE", False);
149     if (kwmDockWindow == None)
150         kwmDockWindow = TSXInternAtom( display, "KWM_DOCKWINDOW", False );
151
152     ((X11DRV_WND_DATA *) wndPtr->pDriverData)->window = X11DRV_GetXRootWindow();
153     X11DRV_WND_RegisterWindow( wndPtr );
154
155     return TRUE;
156 }
157
158 /**********************************************************************
159  *              X11DRV_WND_IconChanged
160  *
161  * hIcon or hIconSm has changed (or is being initialised for the
162  * first time). Complete the X11 driver-specific initialisation.
163  *
164  * This is not entirely correct, may need to create
165  * an icon window and set the pixmap as a background
166  */
167 static void X11DRV_WND_IconChanged(WND *wndPtr)
168 {
169
170     HICON16 hIcon = NC_IconForWindow(wndPtr); 
171
172     if( ((X11DRV_WND_DATA *) wndPtr->pDriverData)->hWMIconBitmap )
173         DeleteObject( ((X11DRV_WND_DATA *) wndPtr->pDriverData)->hWMIconBitmap );
174
175     if( ((X11DRV_WND_DATA *) wndPtr->pDriverData)->hWMIconMask )
176         DeleteObject( ((X11DRV_WND_DATA *) wndPtr->pDriverData)->hWMIconMask);
177
178     if (!hIcon)
179     {
180         ((X11DRV_WND_DATA *) wndPtr->pDriverData)->hWMIconBitmap= 0;
181         ((X11DRV_WND_DATA *) wndPtr->pDriverData)->hWMIconMask= 0;
182     }
183     else
184     {
185         HBITMAP hbmOrig;
186         RECT rcMask;
187         BITMAP bmMask;
188         ICONINFO ii;
189         HDC hDC;
190
191         GetIconInfo(hIcon, &ii);
192
193         X11DRV_CreateBitmap(ii.hbmMask);
194         X11DRV_CreateBitmap(ii.hbmColor);
195
196         GetObjectA(ii.hbmMask, sizeof(bmMask), &bmMask);
197         rcMask.top    = 0;
198         rcMask.left   = 0;
199         rcMask.right  = bmMask.bmWidth;
200         rcMask.bottom = bmMask.bmHeight;
201
202         hDC = CreateCompatibleDC(0);
203         hbmOrig = SelectObject(hDC, ii.hbmMask);
204         InvertRect(hDC, &rcMask);
205         SelectObject(hDC, hbmOrig);
206         DeleteDC(hDC);
207
208         ((X11DRV_WND_DATA *) wndPtr->pDriverData)->hWMIconBitmap = ii.hbmColor;
209         ((X11DRV_WND_DATA *) wndPtr->pDriverData)->hWMIconMask= ii.hbmMask;
210     }
211     return;
212 }
213
214 static void X11DRV_WND_SetIconHints(WND *wndPtr, XWMHints *hints)
215 {
216     if (((X11DRV_WND_DATA *) wndPtr->pDriverData)->hWMIconBitmap)
217     {
218         hints->icon_pixmap
219             = X11DRV_BITMAP_Pixmap(((X11DRV_WND_DATA *) wndPtr->pDriverData)->hWMIconBitmap);
220         hints->flags |= IconPixmapHint;
221     }
222     else
223         hints->flags &= ~IconPixmapHint;
224
225     if (((X11DRV_WND_DATA *) wndPtr->pDriverData)->hWMIconMask)
226     {
227         hints->icon_mask
228             = X11DRV_BITMAP_Pixmap(((X11DRV_WND_DATA *) wndPtr->pDriverData)->hWMIconMask);
229         hints->flags |= IconMaskHint;
230     }
231     else
232         hints->flags &= ~IconMaskHint;
233 }
234
235 /**********************************************************************
236  *              X11DRV_WND_UpdateIconHints
237  *
238  * hIcon or hIconSm has changed (or is being initialised for the
239  * first time). Complete the X11 driver-specific initialisation
240  * and set the window hints.
241  *
242  * This is not entirely correct, may need to create
243  * an icon window and set the pixmap as a background
244  */
245 static void X11DRV_WND_UpdateIconHints(WND *wndPtr)
246 {
247     XWMHints* wm_hints;
248
249     X11DRV_WND_IconChanged(wndPtr);
250
251     wm_hints = TSXGetWMHints( display, X11DRV_WND_GetXWindow(wndPtr) );
252     if (!wm_hints) wm_hints = TSXAllocWMHints();
253     if (wm_hints)
254     {
255         X11DRV_WND_SetIconHints(wndPtr, wm_hints);
256         TSXSetWMHints( display, X11DRV_WND_GetXWindow(wndPtr), wm_hints );
257         TSXFree( wm_hints );
258     }
259 }
260
261
262 /**********************************************************************
263  *              X11DRV_WND_CreateWindow
264  */
265 BOOL X11DRV_WND_CreateWindow(WND *wndPtr, CLASS *classPtr, CREATESTRUCTA *cs, BOOL bUnicode)
266 {
267   /* Create the X window (only for top-level windows, and then only */
268   /* when there's no desktop window) */
269   
270   if (!(cs->style & WS_CHILD) && (X11DRV_GetXRootWindow() == DefaultRootWindow(display)))
271   {
272       Window    wGroupLeader;
273       XWMHints* wm_hints;
274       XSetWindowAttributes win_attr;
275       
276       /* Create "managed" windows only if a title bar or resizable */
277       /* frame is required. */
278         if (WIN_WindowNeedsWMBorder(cs->style, cs->dwExStyle)) {
279           win_attr.event_mask = ExposureMask | KeyPressMask |
280             KeyReleaseMask | PointerMotionMask |
281             ButtonPressMask | ButtonReleaseMask |
282             FocusChangeMask | StructureNotifyMask;
283           win_attr.override_redirect = FALSE;
284           wndPtr->flags |= WIN_MANAGED;
285         } else {
286           win_attr.event_mask = ExposureMask | KeyPressMask |
287             KeyReleaseMask | PointerMotionMask |
288             ButtonPressMask | ButtonReleaseMask |
289             FocusChangeMask;
290           win_attr.override_redirect = TRUE;
291         }
292       wndPtr->flags |= WIN_NATIVE;
293
294       win_attr.bit_gravity   = (classPtr->style & (CS_VREDRAW | CS_HREDRAW)) ? BGForget : BGNorthWest;
295       win_attr.colormap      = X11DRV_PALETTE_PaletteXColormap;
296       win_attr.backing_store = NotUseful;
297       win_attr.save_under    = ((classPtr->style & CS_SAVEBITS) != 0);
298       win_attr.cursor        = X11DRV_MOUSE_XCursor;
299
300       ((X11DRV_WND_DATA *) wndPtr->pDriverData)->hWMIconBitmap = 0;
301       ((X11DRV_WND_DATA *) wndPtr->pDriverData)->hWMIconMask = 0;
302       ((X11DRV_WND_DATA *) wndPtr->pDriverData)->bit_gravity = win_attr.bit_gravity;
303       ((X11DRV_WND_DATA *) wndPtr->pDriverData)->window = 
304         TSXCreateWindow( display, X11DRV_GetXRootWindow(), 
305                          cs->x, cs->y, cs->cx, cs->cy, 
306                          0, CopyFromParent, 
307                          InputOutput, CopyFromParent,
308                          CWEventMask | CWOverrideRedirect |
309                          CWColormap | CWCursor | CWSaveUnder |
310                          CWBackingStore | CWBitGravity, 
311                          &win_attr );
312       
313       if(!(wGroupLeader = X11DRV_WND_GetXWindow(wndPtr)))
314         return FALSE;
315
316       /* If we are the systray, we need to be managed to be noticed by KWM */
317
318       if (wndPtr->dwExStyle & WS_EX_TRAYWINDOW)
319         X11DRV_WND_DockWindow(wndPtr);
320
321       if (wndPtr->flags & WIN_MANAGED) 
322       {
323           XClassHint *class_hints = TSXAllocClassHint();
324           XSizeHints* size_hints = TSXAllocSizeHints();
325           
326           if (class_hints) 
327           {
328               class_hints->res_name = "wineManaged";
329               class_hints->res_class = "Wine";
330               TSXSetClassHint( display, ((X11DRV_WND_DATA *) wndPtr->pDriverData)->window, class_hints );
331               TSXFree (class_hints);
332           }
333
334           if (size_hints) 
335           {
336               size_hints->win_gravity = StaticGravity;
337               size_hints->flags = PWinGravity;
338
339               if (HAS_DLGFRAME(cs->style,cs->dwExStyle))
340               {
341                   size_hints->min_width = size_hints->max_width = cs->cx;
342                   size_hints->min_height = size_hints->max_height = cs->cy;
343                   size_hints->flags |= PMinSize | PMaxSize;
344               }
345
346               TSXSetWMSizeHints( display, X11DRV_WND_GetXWindow(wndPtr), 
347                                  size_hints, XA_WM_NORMAL_HINTS );
348               TSXFree(size_hints);
349           }
350       }
351       
352       if (cs->hwndParent)  /* Get window owner */
353       {
354           Window w;
355           WND *tmpWnd = WIN_FindWndPtr(cs->hwndParent);
356
357           w = X11DRV_WND_FindXWindow( tmpWnd );
358           if (w != None)
359           {
360               TSXSetTransientForHint( display, X11DRV_WND_GetXWindow(wndPtr), w );
361               wGroupLeader = w;
362           }
363           WIN_ReleaseWndPtr(tmpWnd);
364       }
365
366       if ((wm_hints = TSXAllocWMHints()))
367       {
368           wm_hints->flags = InputHint | StateHint | WindowGroupHint;
369           wm_hints->input = True;
370
371           if( wndPtr->flags & WIN_MANAGED )
372           {
373               X11DRV_WND_IconChanged(wndPtr);
374               X11DRV_WND_SetIconHints(wndPtr, wm_hints);
375
376               wm_hints->initial_state = (wndPtr->dwStyle & WS_MINIMIZE) 
377                                         ? IconicState : NormalState;
378           }
379           else
380               wm_hints->initial_state = NormalState;
381           wm_hints->window_group = wGroupLeader;
382
383           TSXSetWMHints( display, X11DRV_WND_GetXWindow(wndPtr), wm_hints );
384           TSXFree(wm_hints);
385       }
386       X11DRV_WND_RegisterWindow( wndPtr );
387   }
388   return TRUE;
389 }
390
391 /***********************************************************************
392  *              X11DRV_WND_DestroyWindow
393  */
394 BOOL X11DRV_WND_DestroyWindow(WND *wndPtr)
395 {
396    Window w;
397    if ((w = X11DRV_WND_GetXWindow(wndPtr)))
398    {
399        XEvent xe;
400        TSXDeleteContext( display, w, winContext );
401        TSXDestroyWindow( display, w );
402        while( TSXCheckWindowEvent(display, w, NoEventMask, &xe) );
403
404        ((X11DRV_WND_DATA *) wndPtr->pDriverData)->window = None;
405        if( ((X11DRV_WND_DATA *) wndPtr->pDriverData)->hWMIconBitmap )
406        {
407            DeleteObject( ((X11DRV_WND_DATA *) wndPtr->pDriverData)->hWMIconBitmap );
408            ((X11DRV_WND_DATA *) wndPtr->pDriverData)->hWMIconBitmap = 0;
409        }
410        if( ((X11DRV_WND_DATA *) wndPtr->pDriverData)->hWMIconMask )
411        {
412            DeleteObject( ((X11DRV_WND_DATA *) wndPtr->pDriverData)->hWMIconMask);
413            ((X11DRV_WND_DATA *) wndPtr->pDriverData)->hWMIconMask= 0;
414        }
415    }
416
417    return TRUE;
418 }
419
420 /*****************************************************************
421  *              X11DRV_WND_SetParent
422  */
423 WND *X11DRV_WND_SetParent(WND *wndPtr, WND *pWndParent)
424 {
425     WND *pDesktop = WIN_GetDesktop();
426     
427     if( wndPtr && pWndParent && (wndPtr != pDesktop) )
428     {
429         WND* pWndPrev = wndPtr->parent;
430
431         if( pWndParent != pWndPrev )
432         {
433             if ( X11DRV_WND_GetXWindow(wndPtr) )
434             {
435                 /* Toplevel window needs to be reparented.  Used by Tk 8.0 */
436
437                 TSXDestroyWindow( display, X11DRV_WND_GetXWindow(wndPtr) );
438                 ((X11DRV_WND_DATA *) wndPtr->pDriverData)->window = None;
439             }
440
441             WIN_UnlinkWindow(wndPtr->hwndSelf);
442             wndPtr->parent = pWndParent;
443
444             /* Create an X counterpart for reparented top-level windows
445              * when not in the desktop mode. */
446
447             if( pWndParent == pDesktop )
448             {
449                 wndPtr->dwStyle &= ~WS_CHILD;
450                 if( X11DRV_GetXRootWindow() == DefaultRootWindow(display) )
451                 {
452                     CREATESTRUCTA cs;
453                     cs.lpCreateParams = NULL;
454                     cs.hInstance = 0; /* not used in following call */
455                     cs.hMenu = 0; /* not used in following call */
456                     cs.hwndParent = pWndParent->hwndSelf;
457                     cs.cy = wndPtr->rectWindow.bottom - wndPtr->rectWindow.top;
458                     if (!cs.cy)
459                       cs.cy = 1;
460                     cs.cx = wndPtr->rectWindow.right - wndPtr->rectWindow.left;
461                     if (!cs.cx)
462                       cs.cx = 1;
463                     cs.y = wndPtr->rectWindow.top;
464                     cs.x = wndPtr->rectWindow.left;
465                     cs.style = wndPtr->dwStyle;
466                     cs.lpszName = 0; /* not used in following call */
467                     cs.lpszClass = 0; /*not used in following call */
468                     cs.dwExStyle = wndPtr->dwExStyle;
469                     X11DRV_WND_CreateWindow(wndPtr, wndPtr->class,
470                                             &cs, FALSE);
471                 }
472             }
473             else /* a child window */
474             {
475                 if( !( wndPtr->dwStyle & WS_CHILD ) )
476                 {
477                     wndPtr->dwStyle |= WS_CHILD;
478                     if( wndPtr->wIDmenu != 0)
479                     {
480                         DestroyMenu( (HMENU) wndPtr->wIDmenu );
481                         wndPtr->wIDmenu = 0;
482                     }
483                 }
484             }
485             WIN_LinkWindow(wndPtr->hwndSelf, HWND_TOP);
486         }
487         WIN_ReleaseDesktop();
488         return pWndPrev;
489     } /* failure */
490     WIN_ReleaseDesktop();
491     return 0;
492 }
493
494 /***********************************************************************
495  *              X11DRV_WND_ForceWindowRaise
496  *
497  * Raise a window on top of the X stacking order, while preserving 
498  * the correct Windows Z order.
499  */
500 void X11DRV_WND_ForceWindowRaise(WND *wndPtr)
501 {
502   XWindowChanges winChanges;
503   WND *wndPrev,*pDesktop = WIN_GetDesktop();
504   
505   if( !wndPtr || !X11DRV_WND_GetXWindow(wndPtr) || (wndPtr->flags & WIN_MANAGED) )
506   {
507       WIN_ReleaseDesktop();
508     return;
509   }
510   
511   /* Raise all windows up to wndPtr according to their Z order.
512    * (it would be easier with sibling-related Below but it doesn't
513    * work very well with SGI mwm for instance)
514    */
515   winChanges.stack_mode = Above;
516   while (wndPtr)
517     {
518       if (X11DRV_WND_GetXWindow(wndPtr)) 
519         TSXReconfigureWMWindow( display, X11DRV_WND_GetXWindow(wndPtr), 0,
520                                 CWStackMode, &winChanges );
521       wndPrev = pDesktop->child;
522       if (wndPrev == wndPtr) break;
523       while (wndPrev && (wndPrev->next != wndPtr)) wndPrev = wndPrev->next;
524       wndPtr = wndPrev;
525     }
526   WIN_ReleaseDesktop();
527 }
528
529 /***********************************************************************
530  *              X11DRV_WND_FindDesktopXWindow   [Internal]
531  *
532  * Find the actual X window which needs be restacked.
533  * Used by X11DRV_WND_SetWindowPos().
534  */
535 static Window X11DRV_WND_FindDesktopXWindow( WND *wndPtr )
536 {
537   if (!(wndPtr->flags & WIN_MANAGED))
538     return X11DRV_WND_GetXWindow(wndPtr);
539   else
540     {
541       Window window, root, parent, *children;
542       int nchildren;
543       window = X11DRV_WND_GetXWindow(wndPtr);
544       for (;;)
545         {
546           TSXQueryTree( display, window, &root, &parent,
547                         &children, &nchildren );
548           TSXFree( children );
549           if (parent == root)
550             return window;
551           window = parent;
552         }
553     }
554 }
555
556 /***********************************************************************
557  *           WINPOS_SetXWindowPos
558  *
559  * SetWindowPos() for an X window. Used by the real SetWindowPos().
560  */
561 void X11DRV_WND_SetWindowPos(WND *wndPtr, const WINDOWPOS *winpos, BOOL bChangePos)
562 {
563     XWindowChanges winChanges;
564     int changeMask = 0;
565     WND *winposPtr = WIN_FindWndPtr( winpos->hwnd );
566     if ( !winposPtr ) return;
567
568     if(!wndPtr->hwndSelf) wndPtr = NULL; /* FIXME: WND destroyed, shouldn't happen!!! */
569   
570     if (!(winpos->flags & SWP_SHOWWINDOW) && (winpos->flags & SWP_HIDEWINDOW))
571     {
572       if(X11DRV_WND_GetXWindow(wndPtr)) 
573         TSXUnmapWindow( display, X11DRV_WND_GetXWindow(wndPtr) );
574     }
575
576     if(bChangePos)
577     {
578         if ( !(winpos->flags & SWP_NOSIZE))
579         {
580           winChanges.width     = (winpos->cx > 0 ) ? winpos->cx : 1;
581           winChanges.height    = (winpos->cy > 0 ) ? winpos->cy : 1;
582           changeMask |= CWWidth | CWHeight;
583           
584           /* Tweak dialog window size hints */
585           
586           if ((winposPtr->flags & WIN_MANAGED) &&
587                HAS_DLGFRAME(winposPtr->dwStyle,winposPtr->dwExStyle))
588             {
589               XSizeHints *size_hints = TSXAllocSizeHints();
590               
591               if (size_hints)
592                 {
593                   long supplied_return;
594                   
595                   TSXGetWMSizeHints( display, X11DRV_WND_GetXWindow(winposPtr), size_hints,
596                                      &supplied_return, XA_WM_NORMAL_HINTS);
597                   size_hints->min_width = size_hints->max_width = winpos->cx;
598                   size_hints->min_height = size_hints->max_height = winpos->cy;
599                   TSXSetWMSizeHints( display, X11DRV_WND_GetXWindow(winposPtr), size_hints,
600                                      XA_WM_NORMAL_HINTS );
601                   TSXFree(size_hints);
602                 }
603             }
604         }
605         if (!(winpos->flags & SWP_NOMOVE))
606         {
607           winChanges.x = winpos->x;
608           winChanges.y = winpos->y;
609           changeMask |= CWX | CWY;
610         }
611         if (!(winpos->flags & SWP_NOZORDER))
612         {
613           winChanges.stack_mode = Below;
614           changeMask |= CWStackMode;
615           
616           if (winpos->hwndInsertAfter == HWND_TOP) winChanges.stack_mode = Above;
617           else if (winpos->hwndInsertAfter != HWND_BOTTOM)
618             {
619               WND*   insertPtr = WIN_FindWndPtr( winpos->hwndInsertAfter );
620               Window stack[2];
621               
622               stack[0] = X11DRV_WND_FindDesktopXWindow( insertPtr );
623               stack[1] = X11DRV_WND_FindDesktopXWindow( winposPtr );
624               
625               /* for stupid window managers (i.e. all of them) */
626               
627               TSXRestackWindows(display, stack, 2); 
628               changeMask &= ~CWStackMode;
629               
630               WIN_ReleaseWndPtr(insertPtr);
631             }
632         }
633         if (changeMask && X11DRV_WND_GetXWindow(winposPtr))
634         {
635             TSXReconfigureWMWindow( display, X11DRV_WND_GetXWindow(winposPtr), 0, changeMask, &winChanges );
636             if( winposPtr->class->style & (CS_VREDRAW | CS_HREDRAW) )
637                 X11DRV_WND_SetHostAttr( winposPtr, HAK_BITGRAVITY, BGForget );
638         }
639     }
640
641     if ( winpos->flags & SWP_SHOWWINDOW )
642     {
643         if(X11DRV_WND_GetXWindow(wndPtr)) 
644            TSXMapWindow( display, X11DRV_WND_GetXWindow(wndPtr) );
645     }
646     WIN_ReleaseWndPtr(winposPtr);
647 }
648
649 /*****************************************************************
650  *              X11DRV_WND_SetText
651  */
652 void X11DRV_WND_SetText(WND *wndPtr, LPCSTR text)
653 {   
654   if (!X11DRV_WND_GetXWindow(wndPtr))
655     return;
656
657   TSXStoreName( display, X11DRV_WND_GetXWindow(wndPtr), text );
658   TSXSetIconName( display, X11DRV_WND_GetXWindow(wndPtr), text );
659 }
660
661 /*****************************************************************
662  *              X11DRV_WND_SetFocus
663  *
664  * Set the X focus.
665  * Explicit colormap management seems to work only with OLVWM.
666  */
667 void X11DRV_WND_SetFocus(WND *wndPtr)
668 {
669   HWND hwnd =  wndPtr->hwndSelf;
670   XWindowAttributes win_attr;
671   Window win;
672   WND *w = wndPtr;
673   
674   /* Only mess with the X focus if there's */
675   /* no desktop window and if the window is not managed by the WM. */
676   if ((X11DRV_GetXRootWindow() != DefaultRootWindow(display))) return;
677   while (w && !((X11DRV_WND_DATA *) w->pDriverData)->window)
678       w = w->parent;
679   if (!w) w = wndPtr;
680   if (w->flags & WIN_MANAGED) return;
681
682   if (!hwnd)    /* If setting the focus to 0, uninstall the colormap */
683     {
684       if (X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_PRIVATE)
685         TSXUninstallColormap( display, X11DRV_PALETTE_PaletteXColormap );
686       return;
687     }
688   
689   /* Set X focus and install colormap */
690   
691   if (!(win = X11DRV_WND_FindXWindow(wndPtr))) return;
692   if (!TSXGetWindowAttributes( display, win, &win_attr ) ||
693       (win_attr.map_state != IsViewable))
694     return;  /* If window is not viewable, don't change anything */
695   
696   TSXSetInputFocus( display, win, RevertToParent, CurrentTime );
697   if (X11DRV_PALETTE_PaletteFlags & X11DRV_PALETTE_PRIVATE)
698     TSXInstallColormap( display, X11DRV_PALETTE_PaletteXColormap );
699   
700   EVENT_Synchronize();
701 }
702
703 /*****************************************************************
704  *              X11DRV_WND_PreSizeMove
705  */
706 void X11DRV_WND_PreSizeMove(WND *wndPtr)
707 {
708   if (!(wndPtr->dwStyle & WS_CHILD) && (X11DRV_GetXRootWindow() == DefaultRootWindow(display)))
709     TSXGrabServer( display );
710 }
711
712 /*****************************************************************
713  *               X11DRV_WND_PostSizeMove
714  */
715 void X11DRV_WND_PostSizeMove(WND *wndPtr)
716 {
717   if (!(wndPtr->dwStyle & WS_CHILD) && 
718       (X11DRV_GetXRootWindow() == DefaultRootWindow(display)))
719     TSXUngrabServer( display );
720 }
721
722 /*****************************************************************
723  *               X11DRV_WND_SurfaceCopy
724  *
725  * Copies rect to (rect.left + dx, rect.top + dy). 
726  */
727 void X11DRV_WND_SurfaceCopy(WND* wndPtr, DC *dcPtr, INT dx, INT dy, 
728                             const RECT *rect, BOOL bUpdate)
729 {
730     X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dcPtr->physDev;
731     POINT dst, src;
732   
733     dst.x = (src.x = dcPtr->w.DCOrgX + rect->left) + dx;
734     dst.y = (src.y = dcPtr->w.DCOrgY + rect->top) + dy;
735   
736     if (bUpdate) /* handles non-Wine windows hanging over the copied area */
737         TSXSetGraphicsExposures( display, physDev->gc, True );
738     TSXSetFunction( display, physDev->gc, GXcopy );
739     TSXCopyArea( display, physDev->drawable, physDev->drawable,
740                  physDev->gc, src.x, src.y,
741                  rect->right - rect->left,
742                  rect->bottom - rect->top,
743                  dst.x, dst.y );
744     if (bUpdate)
745         TSXSetGraphicsExposures( display, physDev->gc, False );
746
747     if (bUpdate) /* Make sure exposure events have been processed */
748         EVENT_Synchronize();
749 }
750
751 /***********************************************************************
752  *              X11DRV_WND_SetDrawable
753  *
754  * Set the drawable, origin and dimensions for the DC associated to
755  * a given window.
756  */
757 void X11DRV_WND_SetDrawable(WND *wndPtr, DC *dc, WORD flags, BOOL bSetClipOrigin)
758 {
759     X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
760     INT dcOrgXCopy = 0, dcOrgYCopy = 0;
761     BOOL offsetClipRgn = FALSE;
762
763     if (!wndPtr)  /* Get a DC for the whole screen */
764     {
765         dc->w.DCOrgX = 0;
766         dc->w.DCOrgY = 0;
767         physDev->drawable = X11DRV_GetXRootWindow();
768         TSXSetSubwindowMode( display, physDev->gc, IncludeInferiors );
769     }
770     else
771     {
772         /*
773          * This function change the coordinate system (DCOrgX,DCOrgY)
774          * values. When it moves the origin, other data like the current clipping
775          * region will not be moved to that new origin. In the case of DCs that are class
776          * or window DCs that clipping region might be a valid value from a previous use
777          * of the DC and changing the origin of the DC without moving the clip region
778          * results in a clip region that is not placed properly in the DC.
779          * This code will save the dc origin, let the SetDrawable
780          * modify the origin and reset the clipping. When the clipping is set,
781          * it is moved according to the new DC origin.
782          */
783          if ( (wndPtr->class->style & (CS_OWNDC | CS_CLASSDC)) && (dc->w.hClipRgn > 0))
784          {
785              dcOrgXCopy = dc->w.DCOrgX;
786              dcOrgYCopy = dc->w.DCOrgY;
787              offsetClipRgn = TRUE;
788          }
789
790         if (flags & DCX_WINDOW)
791         {
792             dc->w.DCOrgX  = wndPtr->rectWindow.left;
793             dc->w.DCOrgY  = wndPtr->rectWindow.top;
794         }
795         else
796         {
797             dc->w.DCOrgX  = wndPtr->rectClient.left;
798             dc->w.DCOrgY  = wndPtr->rectClient.top;
799         }
800         while (!X11DRV_WND_GetXWindow(wndPtr))
801         {
802             wndPtr = wndPtr->parent;
803             dc->w.DCOrgX += wndPtr->rectClient.left;
804             dc->w.DCOrgY += wndPtr->rectClient.top;
805         }
806         dc->w.DCOrgX -= wndPtr->rectWindow.left;
807         dc->w.DCOrgY -= wndPtr->rectWindow.top;
808
809         /* reset the clip region, according to the new origin */
810         if ( offsetClipRgn )
811         {
812              OffsetRgn(dc->w.hClipRgn, dc->w.DCOrgX - dcOrgXCopy,dc->w.DCOrgY - dcOrgYCopy);
813         }
814         
815         physDev->drawable = X11DRV_WND_GetXWindow(wndPtr);
816
817 #if 0
818         /* This is needed when we reuse a cached DC because
819          * SetDCState() called by ReleaseDC() screws up DC
820          * origins for child windows.
821          */
822
823         if( bSetClipOrigin )
824             TSXSetClipOrigin( display, physDev->gc, dc->w.DCOrgX, dc->w.DCOrgY );
825 #endif
826     }
827 }
828
829 /***********************************************************************
830  *              X11DRV_SetWMHint
831  */
832 static BOOL X11DRV_SetWMHint(Display* display, WND* wndPtr, int hint, int val)
833 {
834     XWMHints* wm_hints = TSXGetWMHints( display, X11DRV_WND_GetXWindow(wndPtr) );
835     if (!wm_hints) wm_hints = TSXAllocWMHints();
836     if (wm_hints)
837     {
838         wm_hints->flags = hint;
839         switch( hint )
840         {
841             case InputHint:
842                  wm_hints->input = val;
843                  break;
844
845             case StateHint:
846                  wm_hints->initial_state = val;
847                  break;
848
849             case IconPixmapHint:
850                  wm_hints->icon_pixmap = (Pixmap)val;
851                  break;
852
853             case IconWindowHint:
854                  wm_hints->icon_window = (Window)val;
855                  break;
856         }
857
858         TSXSetWMHints( display, X11DRV_WND_GetXWindow(wndPtr), wm_hints );
859         TSXFree(wm_hints);
860         return TRUE;
861     }
862     return FALSE;
863 }
864
865
866 /***********************************************************************
867  *              X11DRV_WND_SetHostAttr
868  *
869  * This function returns TRUE if the attribute is supported and the
870  * action was successful. Otherwise it should return FALSE and Wine will try 
871  * to get by without the functionality provided by the host window system.
872  */
873 BOOL X11DRV_WND_SetHostAttr(WND* wnd, INT ha, INT value)
874 {
875     Window w;
876
877     if( (w = X11DRV_WND_GetXWindow(wnd)) )
878     {
879         XSetWindowAttributes win_attr;
880
881         switch( ha )
882         {
883         case HAK_ICONICSTATE: /* called when a window is minimized/restored */
884
885                     if( (wnd->flags & WIN_MANAGED) )
886                     {
887                         if( value )
888                         {
889                             if( wnd->dwStyle & WS_VISIBLE )
890                             {
891                                 XClientMessageEvent ev;
892
893                                 /* FIXME: set proper icon */
894
895                                 ev.type = ClientMessage;
896                                 ev.display = display;
897                                 ev.message_type = wmChangeState;
898                                 ev.format = 32;
899                                 ev.data.l[0] = IconicState;
900                                 ev.window = w;
901
902                                 if( TSXSendEvent (display,
903                 RootWindow( display, XScreenNumberOfScreen(X11DRV_GetXScreen()) ), 
904                 True, (SubstructureRedirectMask | SubstructureNotifyMask), (XEvent*)&ev))
905                                 {
906                                     XEvent xe;
907                                     TSXFlush (display);
908                                     while( !TSXCheckTypedWindowEvent( display, w, UnmapNotify, &xe) );
909                                 }
910                                 else 
911                                     break;
912                             }
913                             else
914                                 X11DRV_SetWMHint( display, wnd, StateHint, IconicState );
915                         }
916                         else
917                         {
918                             if( !(wnd->flags & WS_VISIBLE) )
919                                 X11DRV_SetWMHint( display, wnd, StateHint, NormalState );
920                             else
921                             {
922                                 XEvent xe;
923                                 TSXMapWindow(display, w );
924                                 while( !TSXCheckTypedWindowEvent( display, w, MapNotify, &xe) );
925                             }
926                         }
927                         return TRUE;
928                     }
929                     break;
930
931         case HAK_BITGRAVITY: /* called when a window is resized */
932
933                     if( ((X11DRV_WND_DATA *) wnd->pDriverData)->bit_gravity != value )
934                     {
935                         win_attr.bit_gravity = value;
936                         ((X11DRV_WND_DATA *) wnd->pDriverData)->bit_gravity = value;
937                         TSXChangeWindowAttributes( display, w, CWBitGravity, &win_attr );
938                     }
939                    return TRUE;
940
941         case HAK_ICONS: /* called when the icons change */
942             if ( (wnd->flags & WIN_MANAGED) )
943                 X11DRV_WND_UpdateIconHints(wnd);
944             return TRUE;
945
946         case HAK_ACCEPTFOCUS: /* called when a window is disabled/enabled */
947
948                 if( (wnd->flags & WIN_MANAGED) )
949                     return X11DRV_SetWMHint( display, wnd, InputHint, value );
950         }
951     }
952     return FALSE;
953 }
954
955 /***********************************************************************
956  *              X11DRV_WND_IsSelfClipping
957  */
958 BOOL X11DRV_WND_IsSelfClipping(WND *wndPtr)
959 {
960   return X11DRV_WND_GetXWindow(wndPtr) != None;
961 }
962
963 /***********************************************************************
964  *              X11DRV_WND_DockWindow
965  *
966  * Set the X Property of the window that tells the windowmanager we really
967  * want to be in the systray
968  *
969  * KDE: set "KWM_DOCKWINDOW", type "KWM_DOCKWINDOW" to 1 before a window is 
970  *      mapped.
971  *
972  * all others: to be added ;)
973  */
974 void X11DRV_WND_DockWindow(WND *wndPtr)
975
976   int data = 1;
977   Window win = X11DRV_WND_GetXWindow(wndPtr);
978   if (kwmDockWindow == None) 
979           return; /* no KDE running */
980   TSXChangeProperty(
981     display,win,kwmDockWindow,kwmDockWindow,32,PropModeReplace,(char*)&data,1
982   );
983 }
984