CreateDIBSection takes a const pointer to BITMAPINFO.
[wine] / dlls / x11drv / winpos.c
1 /*
2  * Window position related functions.
3  *
4  * Copyright 1993, 1994, 1995, 2001 Alexandre Julliard
5  * Copyright 1995, 1996, 1999 Alex Korobka
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
21
22 #include "config.h"
23
24 #include <X11/Xlib.h>
25 #ifdef HAVE_LIBXSHAPE
26 #include <X11/IntrinsicP.h>
27 #include <X11/extensions/shape.h>
28 #endif /* HAVE_LIBXSHAPE */
29 #include <stdarg.h>
30
31 #include "windef.h"
32 #include "winbase.h"
33 #include "wingdi.h"
34 #include "winuser.h"
35 #include "winerror.h"
36 #include "wownt32.h"
37 #include "wine/wingdi16.h"
38
39 #include "x11drv.h"
40 #include "win.h"
41 #include "winpos.h"
42 #include "dce.h"
43 #include "cursoricon.h"
44 #include "nonclient.h"
45
46 #include "wine/server.h"
47 #include "wine/debug.h"
48
49 WINE_DEFAULT_DEBUG_CHANNEL(x11drv);
50
51 #define SWP_AGG_NOGEOMETRYCHANGE \
52     (SWP_NOSIZE | SWP_NOMOVE | SWP_NOCLIENTSIZE | SWP_NOCLIENTMOVE)
53 #define SWP_AGG_NOPOSCHANGE \
54     (SWP_AGG_NOGEOMETRYCHANGE | SWP_NOZORDER)
55 #define SWP_AGG_STATUSFLAGS \
56     (SWP_AGG_NOPOSCHANGE | SWP_FRAMECHANGED | SWP_HIDEWINDOW | SWP_SHOWWINDOW)
57
58 #define SWP_EX_NOCOPY       0x0001
59 #define SWP_EX_PAINTSELF    0x0002
60 #define SWP_EX_NONCLIENT    0x0004
61
62 #define HAS_THICKFRAME(style,exStyle) \
63     (((style) & WS_THICKFRAME) && \
64      !(((style) & (WS_DLGFRAME|WS_BORDER)) == WS_DLGFRAME))
65
66 #define ON_LEFT_BORDER(hit) \
67  (((hit) == HTLEFT) || ((hit) == HTTOPLEFT) || ((hit) == HTBOTTOMLEFT))
68 #define ON_RIGHT_BORDER(hit) \
69  (((hit) == HTRIGHT) || ((hit) == HTTOPRIGHT) || ((hit) == HTBOTTOMRIGHT))
70 #define ON_TOP_BORDER(hit) \
71  (((hit) == HTTOP) || ((hit) == HTTOPLEFT) || ((hit) == HTTOPRIGHT))
72 #define ON_BOTTOM_BORDER(hit) \
73  (((hit) == HTBOTTOM) || ((hit) == HTBOTTOMLEFT) || ((hit) == HTBOTTOMRIGHT))
74
75
76 /***********************************************************************
77  *              clip_children
78  *
79  * Clip all children of a given window out of the visible region
80  */
81 static int clip_children( HWND parent, HWND last, HRGN hrgn, int whole_window )
82 {
83     HWND *list;
84     WND *ptr;
85     HRGN rectRgn;
86     int i, x, y, ret = SIMPLEREGION;
87
88     /* first check if we have anything to do */
89     if (!(list = WIN_ListChildren( parent ))) return ret;
90
91     if (whole_window)
92     {
93         WND *win = WIN_FindWndPtr( parent );
94         x = win->rectWindow.left - win->rectClient.left;
95         y = win->rectWindow.top - win->rectClient.top;
96         WIN_ReleaseWndPtr( win );
97     }
98     else x = y = 0;
99
100     rectRgn = CreateRectRgn( 0, 0, 0, 0 );
101
102     for (i = 0; list[i] && list[i] != last; i++)
103     {
104         if (!(ptr = WIN_FindWndPtr( list[i] ))) continue;
105         if ((ptr->dwStyle & WS_VISIBLE) && !(ptr->dwExStyle & WS_EX_TRANSPARENT))
106         {
107             SetRectRgn( rectRgn, ptr->rectWindow.left + x, ptr->rectWindow.top + y,
108                         ptr->rectWindow.right + x, ptr->rectWindow.bottom + y );
109             if ((ret = CombineRgn( hrgn, hrgn, rectRgn, RGN_DIFF )) == NULLREGION)
110             {
111                 WIN_ReleaseWndPtr( ptr );
112                 break;  /* no need to go on, region is empty */
113             }
114         }
115         WIN_ReleaseWndPtr( ptr );
116     }
117     DeleteObject( rectRgn );
118     HeapFree( GetProcessHeap(), 0, list );
119     return ret;
120 }
121
122
123 /***********************************************************************
124  *              get_visible_region
125  *
126  * Compute the visible region of a window
127  */
128 static HRGN get_visible_region( WND *win, HWND top, UINT flags, int mode )
129 {
130     HRGN rgn;
131     RECT rect;
132     int xoffset, yoffset;
133     X11DRV_WND_DATA *data = win->pDriverData;
134
135     if (flags & DCX_WINDOW)
136     {
137         xoffset = win->rectWindow.left;
138         yoffset = win->rectWindow.top;
139     }
140     else
141     {
142         xoffset = win->rectClient.left;
143         yoffset = win->rectClient.top;
144     }
145
146     if (flags & DCX_PARENTCLIP)
147         GetClientRect( win->parent, &rect );
148     else if (flags & DCX_WINDOW)
149         rect = data->whole_rect;
150     else
151         rect = win->rectClient;
152
153     /* vis region is relative to the start of the client/window area */
154     OffsetRect( &rect, -xoffset, -yoffset );
155
156     if (!(rgn = CreateRectRgn( rect.left, rect.top, rect.right, rect.bottom ))) return 0;
157
158     if ((flags & DCX_CLIPCHILDREN) && (mode != ClipByChildren))
159     {
160         /* we need to clip children by hand */
161         if (clip_children( win->hwndSelf, 0, rgn, (flags & DCX_WINDOW) ) == NULLREGION) return rgn;
162     }
163
164     if (top && top != win->hwndSelf)  /* need to clip siblings of ancestors */
165     {
166         WND *parent, *ptr = WIN_FindWndPtr( win->hwndSelf );
167         HRGN tmp = 0;
168
169         OffsetRgn( rgn, xoffset, yoffset );
170         for (;;)
171         {
172             if (ptr->dwStyle & WS_CLIPSIBLINGS)
173             {
174                 if (clip_children( ptr->parent, ptr->hwndSelf, rgn, FALSE ) == NULLREGION) break;
175             }
176             if (ptr->hwndSelf == top) break;
177             if (!(parent = WIN_FindWndPtr( ptr->parent ))) break;
178             WIN_ReleaseWndPtr( ptr );
179             ptr = parent;
180             /* clip to parent client area */
181             if (tmp) SetRectRgn( tmp, 0, 0, ptr->rectClient.right - ptr->rectClient.left,
182                                  ptr->rectClient.bottom - ptr->rectClient.top );
183             else tmp = CreateRectRgn( 0, 0, ptr->rectClient.right - ptr->rectClient.left,
184                                       ptr->rectClient.bottom - ptr->rectClient.top );
185             CombineRgn( rgn, rgn, tmp, RGN_AND );
186             OffsetRgn( rgn, ptr->rectClient.left, ptr->rectClient.top );
187             xoffset += ptr->rectClient.left;
188             yoffset += ptr->rectClient.top;
189         }
190         WIN_ReleaseWndPtr( ptr );
191         /* make it relative to the target window again */
192         OffsetRgn( rgn, -xoffset, -yoffset );
193         if (tmp) DeleteObject( tmp );
194     }
195
196     return rgn;
197 }
198
199
200 /***********************************************************************
201  *              get_covered_region
202  *
203  * Compute the portion of 'rgn' that is covered by non-clipped siblings.
204  * This is the area that is covered from X point of view, but may still need
205  * to be exposed.
206  * 'rgn' must be relative to the client area of the parent of 'win'.
207  */
208 static int get_covered_region( WND *win, HRGN rgn )
209 {
210     HRGN tmp;
211     int ret;
212     WND *parent, *ptr = WIN_FindWndPtr( win->hwndSelf );
213     int xoffset = 0, yoffset = 0;
214
215     tmp = CreateRectRgn( 0, 0, 0, 0 );
216     CombineRgn( tmp, rgn, 0, RGN_COPY );
217
218     /* to make things easier we actually build the uncovered
219      * area by removing all siblings and then we subtract that
220      * from the total region to get the covered area */
221     for (;;)
222     {
223         if (!(ptr->dwStyle & WS_CLIPSIBLINGS))
224         {
225             if (clip_children( ptr->parent, ptr->hwndSelf, tmp, FALSE ) == NULLREGION) break;
226         }
227         if (!(parent = WIN_FindWndPtr( ptr->parent ))) break;
228         WIN_ReleaseWndPtr( ptr );
229         ptr = parent;
230         OffsetRgn( tmp, ptr->rectClient.left, ptr->rectClient.top );
231         xoffset += ptr->rectClient.left;
232         yoffset += ptr->rectClient.top;
233     }
234     WIN_ReleaseWndPtr( ptr );
235     /* make it relative to the target window again */
236     OffsetRgn( tmp, -xoffset, -yoffset );
237
238     /* now subtract the computed region from the original one */
239     ret = CombineRgn( rgn, rgn, tmp, RGN_DIFF );
240     DeleteObject( tmp );
241     return ret;
242 }
243
244
245 /***********************************************************************
246  *              expose_window
247  *
248  * Expose a region of a given window.
249  */
250 static void expose_window( HWND hwnd, RECT *rect, HRGN rgn, int flags )
251 {
252     POINT offset;
253     HWND top = 0;
254     HWND *list;
255     int i;
256
257     /* find the top most parent that doesn't clip children or siblings and
258      * invalidate the area on its parent, including all children */
259     if ((list = WIN_ListParents( hwnd )))
260     {
261         HWND current = hwnd;
262         LONG style = GetWindowLongW( hwnd, GWL_STYLE );
263         for (i = 0; list[i] && list[i] != GetDesktopWindow(); i++)
264         {
265             if (!(style & WS_CLIPSIBLINGS)) top = current;
266             style = GetWindowLongW( list[i], GWL_STYLE );
267             if (!(style & WS_CLIPCHILDREN)) top = current;
268             current = list[i];
269         }
270
271         if (top)
272         {
273             /* find the parent of the top window, reusing the parent list */
274             if (top == hwnd) i = 0;
275             else
276             {
277                 for (i = 0; list[i]; i++) if (list[i] == top) break;
278                 if (list[i] && list[i+1]) i++;
279             }
280             if (list[i] != GetDesktopWindow()) top = list[i];
281             flags &= ~RDW_FRAME;  /* parent will invalidate children frame anyway */
282             flags |= RDW_ALLCHILDREN;
283         }
284         HeapFree( GetProcessHeap(), 0, list );
285     }
286
287     if (!top) top = hwnd;
288
289     /* make coords relative to top */
290     offset.x = offset.y = 0;
291     MapWindowPoints( hwnd, top, &offset, 1 );
292
293     if (rect)
294     {
295         OffsetRect( rect, offset.x, offset.y );
296         RedrawWindow( top, rect, 0, flags );
297     }
298     else
299     {
300         OffsetRgn( rgn, offset.x, offset.y );
301         RedrawWindow( top, NULL, rgn, flags );
302     }
303 }
304
305
306 /***********************************************************************
307  *              expose_covered_parent_area
308  *
309  * Expose the parent area that has been uncovered by moving/hiding a
310  * given window, but that is still covered by other siblings (the area
311  * not covered by siblings will be exposed automatically by X).
312  */
313 static void expose_covered_parent_area( WND *win, const RECT *old_rect )
314 {
315     int ret = SIMPLEREGION;
316     HRGN hrgn = CreateRectRgnIndirect( old_rect );
317
318     if (win->dwStyle & WS_VISIBLE)
319     {
320         HRGN tmp = CreateRectRgnIndirect( &win->rectWindow );
321         ret = CombineRgn( hrgn, hrgn, tmp, RGN_DIFF );
322         DeleteObject( tmp );
323     }
324
325     if (ret != NULLREGION)
326     {
327         if (get_covered_region( win, hrgn ) != NULLREGION)
328             expose_window( win->parent, NULL, hrgn, RDW_INVALIDATE | RDW_ERASE | RDW_ALLCHILDREN );
329     }
330     DeleteObject( hrgn );
331 }
332
333
334 /***********************************************************************
335  *              expose_covered_window_area
336  *
337  * Expose the area of a window that is covered by other siblings.
338  */
339 static void expose_covered_window_area( WND *win, const RECT *old_client_rect, BOOL frame )
340 {
341     HRGN hrgn;
342     int ret = SIMPLEREGION;
343
344     if (frame)
345         hrgn = CreateRectRgn( win->rectWindow.left - win->rectClient.left,
346                               win->rectWindow.top - win->rectClient.top,
347                               win->rectWindow.right - win->rectWindow.left,
348                               win->rectWindow.bottom - win->rectWindow.top );
349     else
350         hrgn = CreateRectRgn( 0, 0,
351                               win->rectClient.right - win->rectClient.left,
352                               win->rectClient.bottom - win->rectClient.top );
353
354     /* if the client rect didn't move we don't need to repaint it all */
355     if (old_client_rect->left == win->rectClient.left &&
356         old_client_rect->top == win->rectClient.top)
357     {
358         RECT rc;
359
360         if (IntersectRect( &rc, old_client_rect, &win->rectClient ))
361         {
362             HRGN tmp;
363             /* subtract the unchanged client area from the region to expose */
364             OffsetRect( &rc, -win->rectClient.left, -win->rectClient.top );
365             if ((tmp = CreateRectRgnIndirect( &rc )))
366             {
367                 ret = CombineRgn( hrgn, hrgn, tmp, RGN_DIFF );
368                 DeleteObject( tmp );
369             }
370         }
371     }
372
373     if (ret != NULLREGION)
374     {
375         if (get_covered_region( win, hrgn ) != NULLREGION)
376             expose_window( win->hwndSelf, NULL, hrgn,
377                            RDW_INVALIDATE | RDW_ERASE | RDW_FRAME | RDW_ALLCHILDREN );
378     }
379
380     DeleteObject( hrgn );
381 }
382
383
384 /***********************************************************************
385  *           X11DRV_Expose
386  */
387 void X11DRV_Expose( HWND hwnd, XExposeEvent *event )
388 {
389     RECT rect;
390     struct x11drv_win_data *data;
391     int flags = RDW_INVALIDATE | RDW_ERASE;
392     WND *win;
393
394     TRACE( "win %p (%lx) %d,%d %dx%d\n",
395            hwnd, event->window, event->x, event->y, event->width, event->height );
396
397     rect.left   = event->x;
398     rect.top    = event->y;
399     rect.right  = rect.left + event->width;
400     rect.bottom = rect.top + event->height;
401
402     if (!(win = WIN_GetPtr( hwnd ))) return;
403     data = win->pDriverData;
404
405     if (event->window != data->client_window)  /* whole window or icon window */
406     {
407         flags |= RDW_FRAME;
408         /* make position relative to client area instead of window */
409         OffsetRect( &rect, -data->client_rect.left, -data->client_rect.top );
410     }
411     WIN_ReleasePtr( win );
412
413     expose_window( hwnd, &rect, 0, flags );
414 }
415
416
417 /***********************************************************************
418  *              GetDC (X11DRV.@)
419  *
420  * Set the drawable, origin and dimensions for the DC associated to
421  * a given window.
422  */
423 BOOL X11DRV_GetDC( HWND hwnd, HDC hdc, HRGN hrgn, DWORD flags )
424 {
425     WND *win = WIN_GetPtr( hwnd );
426     HWND top = 0;
427     X11DRV_WND_DATA *data = win->pDriverData;
428     struct x11drv_escape_set_drawable escape;
429     BOOL visible;
430
431     escape.mode = IncludeInferiors;
432     /* don't clip siblings if using parent clip region */
433     if (flags & DCX_PARENTCLIP) flags &= ~DCX_CLIPSIBLINGS;
434
435     /* find the top parent in the hierarchy that isn't clipping siblings */
436     visible = (win->dwStyle & WS_VISIBLE) != 0;
437
438     if (visible)
439     {
440         HWND *list = WIN_ListParents( hwnd );
441         if (list)
442         {
443             int i;
444             for (i = 0; list[i] != GetDesktopWindow(); i++)
445             {
446                 LONG style = GetWindowLongW( list[i], GWL_STYLE );
447                 if (!(style & WS_VISIBLE))
448                 {
449                     visible = FALSE;
450                     top = 0;
451                     break;
452                 }
453                 if (!(style & WS_CLIPSIBLINGS)) top = list[i];
454             }
455             HeapFree( GetProcessHeap(), 0, list );
456         }
457         if (!top && visible && !(flags & DCX_CLIPSIBLINGS)) top = hwnd;
458     }
459
460     if (top)
461     {
462         HWND parent = GetAncestor( top, GA_PARENT );
463         escape.org.x = escape.org.y = 0;
464         if (flags & DCX_WINDOW)
465         {
466             escape.org.x = win->rectWindow.left - win->rectClient.left;
467             escape.org.y = win->rectWindow.top - win->rectClient.top;
468         }
469         MapWindowPoints( hwnd, parent, &escape.org, 1 );
470         escape.drawable_org.x = escape.drawable_org.y = 0;
471         MapWindowPoints( parent, 0, &escape.drawable_org, 1 );
472         /* have to use the parent so that we include siblings */
473         if (parent) escape.drawable = X11DRV_get_client_window( parent );
474         else escape.drawable = root_window;
475     }
476     else
477     {
478         if (IsIconic( hwnd ))
479         {
480             escape.drawable = data->icon_window ? data->icon_window : data->whole_window;
481             escape.org.x = 0;
482             escape.org.y = 0;
483             escape.drawable_org = escape.org;
484         }
485         else if (flags & DCX_WINDOW)
486         {
487             escape.drawable = data->whole_window;
488             escape.org.x = win->rectWindow.left - data->whole_rect.left;
489             escape.org.y = win->rectWindow.top - data->whole_rect.top;
490             escape.drawable_org.x = data->whole_rect.left - win->rectClient.left;
491             escape.drawable_org.y = data->whole_rect.top - win->rectClient.top;
492         }
493         else
494         {
495             escape.drawable = data->client_window;
496             escape.org.x = 0;
497             escape.org.y = 0;
498             escape.drawable_org = escape.org;
499             if (flags & DCX_CLIPCHILDREN) escape.mode = ClipByChildren;  /* can use X11 clipping */
500         }
501         MapWindowPoints( hwnd, 0, &escape.drawable_org, 1 );
502     }
503
504     escape.code = X11DRV_SET_DRAWABLE;
505     ExtEscape( hdc, X11DRV_ESCAPE, sizeof(escape), (LPSTR)&escape, 0, NULL );
506
507     if (flags & (DCX_EXCLUDERGN | DCX_INTERSECTRGN) ||
508         SetHookFlags16( HDC_16(hdc), DCHF_VALIDATEVISRGN ))  /* DC was dirty */
509     {
510         /* need to recompute the visible region */
511         HRGN visRgn;
512
513         if (visible)
514         {
515             visRgn = get_visible_region( win, top, flags, escape.mode );
516
517             if (flags & (DCX_EXCLUDERGN | DCX_INTERSECTRGN))
518                 CombineRgn( visRgn, visRgn, hrgn,
519                             (flags & DCX_INTERSECTRGN) ? RGN_AND : RGN_DIFF );
520         }
521         else visRgn = CreateRectRgn( 0, 0, 0, 0 );
522
523         SelectVisRgn16( HDC_16(hdc), HRGN_16(visRgn) );
524         DeleteObject( visRgn );
525     }
526
527     WIN_ReleasePtr( win );
528     return TRUE;
529 }
530
531
532 /***********************************************************************
533  *              ReleaseDC (X11DRV.@)
534  */
535 void X11DRV_ReleaseDC( HWND hwnd, HDC hdc )
536 {
537     struct x11drv_escape_set_drawable escape;
538
539     escape.code = X11DRV_SET_DRAWABLE;
540     escape.drawable = root_window;
541     escape.mode = IncludeInferiors;
542     escape.org.x = escape.org.y = 0;
543     escape.drawable_org.x = escape.drawable_org.y = 0;
544
545     ExtEscape( hdc, X11DRV_ESCAPE, sizeof(escape), (LPSTR)&escape, 0, NULL );
546 }
547
548
549 /***********************************************************************
550  *           SWP_DoWinPosChanging
551  */
552 static BOOL SWP_DoWinPosChanging( WINDOWPOS* pWinpos, RECT* pNewWindowRect, RECT* pNewClientRect )
553 {
554     WND *wndPtr;
555
556     /* Send WM_WINDOWPOSCHANGING message */
557
558     if (!(pWinpos->flags & SWP_NOSENDCHANGING))
559         SendMessageA( pWinpos->hwnd, WM_WINDOWPOSCHANGING, 0, (LPARAM)pWinpos );
560
561     if (!(wndPtr = WIN_GetPtr( pWinpos->hwnd )) || wndPtr == WND_OTHER_PROCESS) return FALSE;
562
563     /* Calculate new position and size */
564
565     *pNewWindowRect = wndPtr->rectWindow;
566     *pNewClientRect = (wndPtr->dwStyle & WS_MINIMIZE) ? wndPtr->rectWindow
567                                                     : wndPtr->rectClient;
568
569     if (!(pWinpos->flags & SWP_NOSIZE))
570     {
571         pNewWindowRect->right  = pNewWindowRect->left + pWinpos->cx;
572         pNewWindowRect->bottom = pNewWindowRect->top + pWinpos->cy;
573     }
574     if (!(pWinpos->flags & SWP_NOMOVE))
575     {
576         pNewWindowRect->left    = pWinpos->x;
577         pNewWindowRect->top     = pWinpos->y;
578         pNewWindowRect->right  += pWinpos->x - wndPtr->rectWindow.left;
579         pNewWindowRect->bottom += pWinpos->y - wndPtr->rectWindow.top;
580
581         OffsetRect( pNewClientRect, pWinpos->x - wndPtr->rectWindow.left,
582                                     pWinpos->y - wndPtr->rectWindow.top );
583     }
584     pWinpos->flags |= SWP_NOCLIENTMOVE | SWP_NOCLIENTSIZE;
585     WIN_ReleasePtr( wndPtr );
586     return TRUE;
587 }
588
589 /***********************************************************************
590  *           SWP_DoNCCalcSize
591  */
592 static UINT SWP_DoNCCalcSize( WINDOWPOS* pWinpos, RECT* pNewWindowRect, RECT* pNewClientRect )
593 {
594     UINT wvrFlags = 0;
595     WND *wndPtr;
596
597     if (!(wndPtr = WIN_GetPtr( pWinpos->hwnd )) || wndPtr == WND_OTHER_PROCESS) return 0;
598
599       /* Send WM_NCCALCSIZE message to get new client area */
600     if( (pWinpos->flags & (SWP_FRAMECHANGED | SWP_NOSIZE)) != SWP_NOSIZE )
601     {
602         NCCALCSIZE_PARAMS params;
603         WINDOWPOS winposCopy;
604
605         params.rgrc[0] = *pNewWindowRect;
606         params.rgrc[1] = wndPtr->rectWindow;
607         params.rgrc[2] = wndPtr->rectClient;
608         params.lppos = &winposCopy;
609         winposCopy = *pWinpos;
610         WIN_ReleasePtr( wndPtr );
611
612         wvrFlags = SendMessageW( pWinpos->hwnd, WM_NCCALCSIZE, TRUE, (LPARAM)&params );
613
614         TRACE( "(%ld,%ld)-(%ld,%ld)\n", params.rgrc[0].left, params.rgrc[0].top,
615                params.rgrc[0].right, params.rgrc[0].bottom );
616
617         /* If the application send back garbage, ignore it */
618         if (params.rgrc[0].left <= params.rgrc[0].right &&
619             params.rgrc[0].top <= params.rgrc[0].bottom)
620             *pNewClientRect = params.rgrc[0];
621
622          /* FIXME: WVR_ALIGNxxx */
623
624         if (!(wndPtr = WIN_GetPtr( pWinpos->hwnd )) || wndPtr == WND_OTHER_PROCESS) return 0;
625
626         if( pNewClientRect->left != wndPtr->rectClient.left ||
627             pNewClientRect->top != wndPtr->rectClient.top )
628             pWinpos->flags &= ~SWP_NOCLIENTMOVE;
629
630         if( (pNewClientRect->right - pNewClientRect->left !=
631              wndPtr->rectClient.right - wndPtr->rectClient.left) ||
632             (pNewClientRect->bottom - pNewClientRect->top !=
633              wndPtr->rectClient.bottom - wndPtr->rectClient.top) )
634             pWinpos->flags &= ~SWP_NOCLIENTSIZE;
635     }
636     else
637     {
638         if (!(pWinpos->flags & SWP_NOMOVE) &&
639             (pNewClientRect->left != wndPtr->rectClient.left ||
640              pNewClientRect->top != wndPtr->rectClient.top))
641             pWinpos->flags &= ~SWP_NOCLIENTMOVE;
642     }
643     WIN_ReleasePtr( wndPtr );
644     return wvrFlags;
645 }
646
647
648 /***********************************************************************
649  *           SWP_DoOwnedPopups
650  *
651  * fix Z order taking into account owned popups -
652  * basically we need to maintain them above the window that owns them
653  *
654  * FIXME: hide/show owned popups when owner visibility changes.
655  */
656 static HWND SWP_DoOwnedPopups(HWND hwnd, HWND hwndInsertAfter)
657 {
658     HWND *list = NULL;
659     HWND owner = GetWindow( hwnd, GW_OWNER );
660     LONG style = GetWindowLongW( hwnd, GWL_STYLE );
661
662     WARN("(%p) hInsertAfter = %p\n", hwnd, hwndInsertAfter );
663
664     if ((style & WS_POPUP) && owner)
665     {
666         /* make sure this popup stays above the owner */
667
668         HWND hwndLocalPrev = HWND_TOP;
669
670         if( hwndInsertAfter != HWND_TOP )
671         {
672             if ((list = WIN_ListChildren( GetDesktopWindow() )))
673             {
674                 int i;
675                 for (i = 0; list[i]; i++)
676                 {
677                     if (list[i] == owner) break;
678                     if (list[i] != hwnd) hwndLocalPrev = list[i];
679                     if (hwndLocalPrev == hwndInsertAfter) break;
680                 }
681                 hwndInsertAfter = hwndLocalPrev;
682             }
683         }
684     }
685     else if (style & WS_CHILD) return hwndInsertAfter;
686
687     if (!list) list = WIN_ListChildren( GetDesktopWindow() );
688     if (list)
689     {
690         int i;
691         for (i = 0; list[i]; i++)
692         {
693             if (list[i] == hwnd) break;
694             if ((GetWindowLongW( list[i], GWL_STYLE ) & WS_POPUP) &&
695                 GetWindow( list[i], GW_OWNER ) == hwnd)
696             {
697                 SetWindowPos( list[i], hwndInsertAfter, 0, 0, 0, 0,
698                               SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE |
699                               SWP_NOSENDCHANGING | SWP_DEFERERASE );
700                 hwndInsertAfter = list[i];
701             }
702         }
703         HeapFree( GetProcessHeap(), 0, list );
704     }
705
706     return hwndInsertAfter;
707 }
708
709
710 /* fix redundant flags and values in the WINDOWPOS structure */
711 static BOOL fixup_flags( WINDOWPOS *winpos )
712 {
713     WND *wndPtr = WIN_GetPtr( winpos->hwnd );
714     BOOL ret = TRUE;
715
716     if (!wndPtr || wndPtr == WND_OTHER_PROCESS)
717     {
718         SetLastError( ERROR_INVALID_WINDOW_HANDLE );
719         return FALSE;
720     }
721     winpos->hwnd = wndPtr->hwndSelf;  /* make it a full handle */
722
723     /* Finally make sure that all coordinates are valid */
724     if (winpos->x < -32768) winpos->x = -32768;
725     else if (winpos->x > 32767) winpos->x = 32767;
726     if (winpos->y < -32768) winpos->y = -32768;
727     else if (winpos->y > 32767) winpos->y = 32767;
728
729     if (winpos->cx < 0) winpos->cx = 0;
730     else if (winpos->cx > 32767) winpos->cx = 32767;
731     if (winpos->cy < 0) winpos->cy = 0;
732     else if (winpos->cy > 32767) winpos->cy = 32767;
733
734     if (wndPtr->dwStyle & WS_VISIBLE) winpos->flags &= ~SWP_SHOWWINDOW;
735     else
736     {
737         winpos->flags &= ~SWP_HIDEWINDOW;
738         if (!(winpos->flags & SWP_SHOWWINDOW)) winpos->flags |= SWP_NOREDRAW;
739     }
740
741     if ((wndPtr->rectWindow.right - wndPtr->rectWindow.left == winpos->cx) &&
742         (wndPtr->rectWindow.bottom - wndPtr->rectWindow.top == winpos->cy))
743         winpos->flags |= SWP_NOSIZE;    /* Already the right size */
744
745     if ((wndPtr->rectWindow.left == winpos->x) && (wndPtr->rectWindow.top == winpos->y))
746         winpos->flags |= SWP_NOMOVE;    /* Already the right position */
747
748     if (winpos->hwnd == GetForegroundWindow())
749         winpos->flags |= SWP_NOACTIVATE;   /* Already active */
750     else if ((wndPtr->dwStyle & (WS_POPUP | WS_CHILD)) != WS_CHILD)
751     {
752         if (!(winpos->flags & SWP_NOACTIVATE)) /* Bring to the top when activating */
753         {
754             winpos->flags &= ~SWP_NOZORDER;
755             winpos->hwndInsertAfter = HWND_TOP;
756             goto done;
757         }
758     }
759
760     /* Check hwndInsertAfter */
761     if (winpos->flags & SWP_NOZORDER) goto done;
762
763     /* fix sign extension */
764     if (winpos->hwndInsertAfter == (HWND)0xffff) winpos->hwndInsertAfter = HWND_TOPMOST;
765     else if (winpos->hwndInsertAfter == (HWND)0xfffe) winpos->hwndInsertAfter = HWND_NOTOPMOST;
766
767       /* FIXME: TOPMOST not supported yet */
768     if ((winpos->hwndInsertAfter == HWND_TOPMOST) ||
769         (winpos->hwndInsertAfter == HWND_NOTOPMOST)) winpos->hwndInsertAfter = HWND_TOP;
770
771     /* hwndInsertAfter must be a sibling of the window */
772     if ((winpos->hwndInsertAfter != HWND_TOP) && (winpos->hwndInsertAfter != HWND_BOTTOM))
773     {
774         if (GetAncestor( winpos->hwndInsertAfter, GA_PARENT ) != wndPtr->parent) ret = FALSE;
775         else
776         {
777             /* don't need to change the Zorder of hwnd if it's already inserted
778              * after hwndInsertAfter or when inserting hwnd after itself.
779              */
780             if ((winpos->hwnd == winpos->hwndInsertAfter) ||
781                 (winpos->hwnd == GetWindow( winpos->hwndInsertAfter, GW_HWNDNEXT )))
782                 winpos->flags |= SWP_NOZORDER;
783         }
784     }
785  done:
786     WIN_ReleasePtr( wndPtr );
787     return ret;
788 }
789
790
791 /***********************************************************************
792  *              set_visible_style
793  *
794  * Set/clear the WS_VISIBLE style of a window and map/unmap the X window.
795  */
796 static void set_visible_style( HWND hwnd, BOOL set )
797 {
798     WND *win;
799
800     if (!(win = WIN_GetPtr( hwnd ))) return;
801     if (win == WND_OTHER_PROCESS) return;
802
803     TRACE( "hwnd %p (%lx) set %d visible %d empty %d\n",
804            hwnd, get_whole_window(win),
805            set, (win->dwStyle & WS_VISIBLE) != 0, IsRectEmpty(&win->rectWindow) );
806
807     if (set)
808     {
809         if (win->dwStyle & WS_VISIBLE) goto done;
810         WIN_SetStyle( hwnd, win->dwStyle | WS_VISIBLE );
811         if (!IsRectEmpty( &win->rectWindow ) && get_whole_window(win) && is_window_top_level(win))
812         {
813             Display *display = thread_display();
814             X11DRV_sync_window_style( display, win );
815             X11DRV_set_wm_hints( display, win );
816             TRACE( "mapping win %p\n", hwnd );
817             wine_tsx11_lock();
818             XMapWindow( display, get_whole_window(win) );
819             wine_tsx11_unlock();
820         }
821     }
822     else
823     {
824         if (!(win->dwStyle & WS_VISIBLE)) goto done;
825         WIN_SetStyle( hwnd, win->dwStyle & ~WS_VISIBLE );
826         if (!IsRectEmpty( &win->rectWindow ) && get_whole_window(win) && is_window_top_level(win))
827         {
828             TRACE( "unmapping win %p\n", hwnd );
829             wine_tsx11_lock();
830             XUnmapWindow( thread_display(), get_whole_window(win) );
831             wine_tsx11_unlock();
832         }
833     }
834  done:
835     WIN_ReleasePtr( win );
836 }
837
838
839 /***********************************************************************
840  *              SetWindowStyle   (X11DRV.@)
841  *
842  * Update the X state of a window to reflect a style change
843  */
844 void X11DRV_SetWindowStyle( HWND hwnd, LONG oldStyle )
845 {
846     Display *display = thread_display();
847     WND *wndPtr;
848     LONG changed;
849
850     if (hwnd == GetDesktopWindow()) return;
851     if (!(wndPtr = WIN_GetPtr( hwnd ))) return;
852     if (wndPtr == WND_OTHER_PROCESS) return;
853
854     changed = wndPtr->dwStyle ^ oldStyle;
855
856     if (changed & WS_VISIBLE)
857     {
858         if (!IsRectEmpty( &wndPtr->rectWindow ))
859         {
860             if (wndPtr->dwStyle & WS_VISIBLE)
861             {
862                 TRACE( "mapping win %p\n", hwnd );
863                 if (is_window_top_level(wndPtr))
864                 {
865                     X11DRV_sync_window_style( display, wndPtr );
866                     X11DRV_set_wm_hints( display, wndPtr );
867                 }
868                 wine_tsx11_lock();
869                 XMapWindow( display, get_whole_window(wndPtr) );
870                 wine_tsx11_unlock();
871             }
872             else if (!is_window_top_level(wndPtr))  /* don't unmap managed windows */
873             {
874                 TRACE( "unmapping win %p\n", hwnd );
875                 wine_tsx11_lock();
876                 XUnmapWindow( display, get_whole_window(wndPtr) );
877                 wine_tsx11_unlock();
878             }
879         }
880     }
881
882     if (changed & WS_DISABLED)
883     {
884         if (wndPtr->dwExStyle & WS_EX_MANAGED)
885         {
886             XWMHints *wm_hints;
887             wine_tsx11_lock();
888             if (!(wm_hints = XGetWMHints( display, get_whole_window(wndPtr) )))
889                 wm_hints = XAllocWMHints();
890             if (wm_hints)
891             {
892                 wm_hints->flags |= InputHint;
893                 wm_hints->input = !(wndPtr->dwStyle & WS_DISABLED);
894                 XSetWMHints( display, get_whole_window(wndPtr), wm_hints );
895                 XFree(wm_hints);
896             }
897             wine_tsx11_unlock();
898         }
899     }
900     WIN_ReleasePtr(wndPtr);
901 }
902
903
904 /***********************************************************************
905  *              SetWindowPos   (X11DRV.@)
906  */
907 BOOL X11DRV_SetWindowPos( WINDOWPOS *winpos )
908 {
909     WND *wndPtr;
910     RECT newWindowRect, newClientRect;
911     RECT oldWindowRect, oldClientRect;
912     UINT wvrFlags = 0;
913     BOOL bChangePos;
914
915     TRACE( "hwnd %p, after %p, swp %d,%d %dx%d flags %08x\n",
916            winpos->hwnd, winpos->hwndInsertAfter, winpos->x, winpos->y,
917            winpos->cx, winpos->cy, winpos->flags);
918
919     bChangePos = !(winpos->flags & SWP_WINE_NOHOSTMOVE);
920     winpos->flags &= ~SWP_WINE_NOHOSTMOVE;
921
922     /* Check window handle */
923     if (winpos->hwnd == GetDesktopWindow()) return FALSE;
924
925     /* First make sure that coordinates are valid for WM_WINDOWPOSCHANGING */
926     if (!(winpos->flags & SWP_NOMOVE))
927     {
928         if (winpos->x < -32768) winpos->x = -32768;
929         else if (winpos->x > 32767) winpos->x = 32767;
930         if (winpos->y < -32768) winpos->y = -32768;
931         else if (winpos->y > 32767) winpos->y = 32767;
932     }
933     if (!(winpos->flags & SWP_NOSIZE))
934     {
935         if (winpos->cx < 0) winpos->cx = 0;
936         else if (winpos->cx > 32767) winpos->cx = 32767;
937         if (winpos->cy < 0) winpos->cy = 0;
938         else if (winpos->cy > 32767) winpos->cy = 32767;
939     }
940
941     if (!SWP_DoWinPosChanging( winpos, &newWindowRect, &newClientRect )) return FALSE;
942
943     /* Fix redundant flags */
944     if (!fixup_flags( winpos )) return FALSE;
945
946     if (!(wndPtr = WIN_FindWndPtr( winpos->hwnd ))) return FALSE;
947
948     TRACE("\tcurrent (%ld,%ld)-(%ld,%ld), style %08x\n",
949           wndPtr->rectWindow.left, wndPtr->rectWindow.top,
950           wndPtr->rectWindow.right, wndPtr->rectWindow.bottom, (unsigned)wndPtr->dwStyle );
951
952     if((winpos->flags & (SWP_NOZORDER | SWP_HIDEWINDOW | SWP_SHOWWINDOW)) != SWP_NOZORDER)
953     {
954         if (GetAncestor( winpos->hwnd, GA_PARENT ) == GetDesktopWindow())
955             winpos->hwndInsertAfter = SWP_DoOwnedPopups( winpos->hwnd, winpos->hwndInsertAfter );
956     }
957
958     /* Common operations */
959
960     wvrFlags = SWP_DoNCCalcSize( winpos, &newWindowRect, &newClientRect );
961
962     if(!(winpos->flags & SWP_NOZORDER) && winpos->hwnd != winpos->hwndInsertAfter)
963     {
964         HWND parent = GetAncestor( winpos->hwnd, GA_PARENT );
965         if (parent) WIN_LinkWindow( winpos->hwnd, parent, winpos->hwndInsertAfter );
966     }
967
968     /* Reset active DCEs */
969
970     if( (((winpos->flags & SWP_AGG_NOPOSCHANGE) != SWP_AGG_NOPOSCHANGE) &&
971          wndPtr->dwStyle & WS_VISIBLE) ||
972         (winpos->flags & (SWP_HIDEWINDOW | SWP_SHOWWINDOW)) )
973     {
974         RECT rect;
975
976         UnionRect(&rect, &newWindowRect, &wndPtr->rectWindow);
977         DCE_InvalidateDCE(wndPtr->hwndSelf, &rect);
978     }
979
980     oldWindowRect = wndPtr->rectWindow;
981     oldClientRect = wndPtr->rectClient;
982
983     /* Find out if we have to redraw the whole client rect */
984
985     if( oldClientRect.bottom - oldClientRect.top ==
986         newClientRect.bottom - newClientRect.top ) wvrFlags &= ~WVR_VREDRAW;
987
988     if( oldClientRect.right - oldClientRect.left ==
989         newClientRect.right - newClientRect.left ) wvrFlags &= ~WVR_HREDRAW;
990
991     /* FIXME: actually do something with WVR_VALIDRECTS */
992
993     WIN_SetRectangles( winpos->hwnd, &newWindowRect, &newClientRect );
994
995     if (get_whole_window(wndPtr))  /* don't do anything if X window not created yet */
996     {
997         Display *display = thread_display();
998
999         if (!(winpos->flags & SWP_SHOWWINDOW) && (winpos->flags & SWP_HIDEWINDOW))
1000         {
1001             /* clear the update region */
1002             RedrawWindow( winpos->hwnd, NULL, 0, RDW_VALIDATE | RDW_NOFRAME |
1003                           RDW_NOERASE | RDW_NOINTERNALPAINT | RDW_ALLCHILDREN );
1004             set_visible_style( winpos->hwnd, FALSE );
1005         }
1006         else if ((wndPtr->dwStyle & WS_VISIBLE) &&
1007                  !IsRectEmpty( &oldWindowRect ) && IsRectEmpty( &newWindowRect ))
1008         {
1009             /* resizing to zero size -> unmap */
1010             TRACE( "unmapping zero size win %p\n", winpos->hwnd );
1011             wine_tsx11_lock();
1012             XUnmapWindow( display, get_whole_window(wndPtr) );
1013             wine_tsx11_unlock();
1014         }
1015
1016         wine_tsx11_lock();
1017         if (bChangePos)
1018             X11DRV_sync_whole_window_position( display, wndPtr, !(winpos->flags & SWP_NOZORDER) );
1019         else
1020         {
1021             struct x11drv_win_data *data = wndPtr->pDriverData;
1022             data->whole_rect = wndPtr->rectWindow;
1023             X11DRV_window_to_X_rect( wndPtr, &data->whole_rect );
1024         }
1025
1026         if (X11DRV_sync_client_window_position( display, wndPtr ) ||
1027             (winpos->flags & SWP_FRAMECHANGED))
1028         {
1029             /* if we moved the client area, repaint the whole non-client window */
1030             XClearArea( display, get_whole_window(wndPtr), 0, 0, 0, 0, True );
1031             winpos->flags |= SWP_FRAMECHANGED;
1032         }
1033         if (winpos->flags & SWP_SHOWWINDOW)
1034         {
1035             set_visible_style( winpos->hwnd, TRUE );
1036         }
1037         else if ((wndPtr->dwStyle & WS_VISIBLE) &&
1038                  IsRectEmpty( &oldWindowRect ) && !IsRectEmpty( &newWindowRect ))
1039         {
1040             /* resizing from zero size to non-zero -> map */
1041             TRACE( "mapping non zero size win %p\n", winpos->hwnd );
1042             XMapWindow( display, get_whole_window(wndPtr) );
1043         }
1044         XFlush( display );  /* FIXME: should not be necessary */
1045         wine_tsx11_unlock();
1046     }
1047     else  /* no X window, simply toggle the window style */
1048     {
1049         if (winpos->flags & SWP_SHOWWINDOW)
1050             set_visible_style( winpos->hwnd, TRUE );
1051         else if (winpos->flags & SWP_HIDEWINDOW)
1052             set_visible_style( winpos->hwnd, FALSE );
1053     }
1054
1055     /* manually expose the areas that X won't expose because they are still covered by something */
1056
1057     if (!(winpos->flags & SWP_SHOWWINDOW))
1058         expose_covered_parent_area( wndPtr, &oldWindowRect );
1059
1060     if (wndPtr->dwStyle & WS_VISIBLE)
1061         expose_covered_window_area( wndPtr, &oldClientRect, winpos->flags & SWP_FRAMECHANGED );
1062
1063     WIN_ReleaseWndPtr(wndPtr);
1064
1065     if (wvrFlags & WVR_REDRAW) RedrawWindow( winpos->hwnd, NULL, 0, RDW_INVALIDATE | RDW_ERASE );
1066
1067     if( winpos->flags & SWP_HIDEWINDOW )
1068         HideCaret(winpos->hwnd);
1069     else if (winpos->flags & SWP_SHOWWINDOW)
1070         ShowCaret(winpos->hwnd);
1071
1072     if (!(winpos->flags & SWP_NOACTIVATE))
1073     {
1074         /* child windows get WM_CHILDACTIVATE message */
1075         if ((GetWindowLongW( winpos->hwnd, GWL_STYLE ) & (WS_CHILD | WS_POPUP)) == WS_CHILD)
1076             SendMessageA( winpos->hwnd, WM_CHILDACTIVATE, 0, 0 );
1077         else
1078             SetForegroundWindow( winpos->hwnd );
1079     }
1080
1081       /* And last, send the WM_WINDOWPOSCHANGED message */
1082
1083     TRACE("\tstatus flags = %04x\n", winpos->flags & SWP_AGG_STATUSFLAGS);
1084
1085     if (((winpos->flags & SWP_AGG_STATUSFLAGS) != SWP_AGG_NOPOSCHANGE))
1086         SendMessageA( winpos->hwnd, WM_WINDOWPOSCHANGED, 0, (LPARAM)winpos );
1087         /* WM_WINDOWPOSCHANGED is send even if SWP_NOSENDCHANGING is set */
1088
1089     return TRUE;
1090 }
1091
1092
1093 /***********************************************************************
1094  *           WINPOS_FindIconPos
1095  *
1096  * Find a suitable place for an iconic window.
1097  */
1098 static POINT WINPOS_FindIconPos( WND* wndPtr, POINT pt )
1099 {
1100     RECT rectParent;
1101     HWND *list;
1102     short x, y, xspacing, yspacing;
1103
1104     GetClientRect( wndPtr->parent, &rectParent );
1105     if ((pt.x >= rectParent.left) && (pt.x + GetSystemMetrics(SM_CXICON) < rectParent.right) &&
1106         (pt.y >= rectParent.top) && (pt.y + GetSystemMetrics(SM_CYICON) < rectParent.bottom))
1107         return pt;  /* The icon already has a suitable position */
1108
1109     xspacing = GetSystemMetrics(SM_CXICONSPACING);
1110     yspacing = GetSystemMetrics(SM_CYICONSPACING);
1111
1112     list = WIN_ListChildren( wndPtr->parent );
1113     y = rectParent.bottom;
1114     for (;;)
1115     {
1116         x = rectParent.left;
1117         do
1118         {
1119             /* Check if another icon already occupies this spot */
1120             /* FIXME: this is completely inefficient */
1121             if (list)
1122             {
1123                 int i;
1124                 WND *childPtr;
1125
1126                 for (i = 0; list[i]; i++)
1127                 {
1128                     if (list[i] == wndPtr->hwndSelf) continue;
1129                     if (!IsIconic( list[i] )) continue;
1130                     if (!(childPtr = WIN_FindWndPtr( list[i] ))) continue;
1131                     if ((childPtr->rectWindow.left < x + xspacing) &&
1132                         (childPtr->rectWindow.right >= x) &&
1133                         (childPtr->rectWindow.top <= y) &&
1134                         (childPtr->rectWindow.bottom > y - yspacing))
1135                     {
1136                         WIN_ReleaseWndPtr( childPtr );
1137                         break;  /* There's a window in there */
1138                     }
1139                     WIN_ReleaseWndPtr( childPtr );
1140                 }
1141                 if (list[i])
1142                 {
1143                     /* found something here, try next spot */
1144                     x += xspacing;
1145                     continue;
1146                 }
1147             }
1148
1149             /* No window was found, so it's OK for us */
1150             pt.x = x + (xspacing - GetSystemMetrics(SM_CXICON)) / 2;
1151             pt.y = y - (yspacing + GetSystemMetrics(SM_CYICON)) / 2;
1152             HeapFree( GetProcessHeap(), 0, list );
1153             return pt;
1154
1155         } while(x <= rectParent.right-xspacing);
1156         y -= yspacing;
1157     }
1158 }
1159
1160
1161
1162
1163
1164 UINT WINPOS_MinMaximize( HWND hwnd, UINT cmd, LPRECT rect )
1165 {
1166     WND *wndPtr;
1167     UINT swpFlags = 0;
1168     POINT size;
1169     LONG old_style;
1170     WINDOWPLACEMENT wpl;
1171
1172     TRACE("%p %u\n", hwnd, cmd );
1173
1174     wpl.length = sizeof(wpl);
1175     GetWindowPlacement( hwnd, &wpl );
1176
1177     if (HOOK_CallHooks( WH_CBT, HCBT_MINMAX, (WPARAM)hwnd, cmd, TRUE ))
1178         return SWP_NOSIZE | SWP_NOMOVE;
1179
1180     if (IsIconic( hwnd ))
1181     {
1182         if (cmd == SW_MINIMIZE) return SWP_NOSIZE | SWP_NOMOVE;
1183         if (!SendMessageA( hwnd, WM_QUERYOPEN, 0, 0 )) return SWP_NOSIZE | SWP_NOMOVE;
1184         swpFlags |= SWP_NOCOPYBITS;
1185     }
1186
1187     if (!(wndPtr = WIN_FindWndPtr( hwnd ))) return 0;
1188
1189     size.x = wndPtr->rectWindow.left;
1190     size.y = wndPtr->rectWindow.top;
1191
1192     switch( cmd )
1193     {
1194     case SW_MINIMIZE:
1195         if( wndPtr->dwStyle & WS_MAXIMIZE) wndPtr->flags |= WIN_RESTORE_MAX;
1196         else wndPtr->flags &= ~WIN_RESTORE_MAX;
1197
1198         WIN_SetStyle( hwnd, (wndPtr->dwStyle & ~WS_MAXIMIZE) | WS_MINIMIZE );
1199
1200         X11DRV_set_iconic_state( wndPtr );
1201
1202         wpl.ptMinPosition = WINPOS_FindIconPos( wndPtr, wpl.ptMinPosition );
1203
1204         SetRect( rect, wpl.ptMinPosition.x, wpl.ptMinPosition.y,
1205                  GetSystemMetrics(SM_CXICON), GetSystemMetrics(SM_CYICON) );
1206         swpFlags |= SWP_NOCOPYBITS;
1207         break;
1208
1209     case SW_MAXIMIZE:
1210         WINPOS_GetMinMaxInfo( hwnd, &size, &wpl.ptMaxPosition, NULL, NULL );
1211
1212         old_style = WIN_SetStyle( hwnd, (wndPtr->dwStyle & ~WS_MINIMIZE) | WS_MAXIMIZE );
1213         if (old_style & WS_MINIMIZE)
1214         {
1215             WINPOS_ShowIconTitle( hwnd, FALSE );
1216             X11DRV_set_iconic_state( wndPtr );
1217         }
1218         SetRect( rect, wpl.ptMaxPosition.x, wpl.ptMaxPosition.y, size.x, size.y );
1219         break;
1220
1221     case SW_RESTORE:
1222         old_style = WIN_SetStyle( hwnd, wndPtr->dwStyle & ~(WS_MINIMIZE|WS_MAXIMIZE) );
1223         if (old_style & WS_MINIMIZE)
1224         {
1225             WINPOS_ShowIconTitle( hwnd, FALSE );
1226             X11DRV_set_iconic_state( wndPtr );
1227
1228             if( wndPtr->flags & WIN_RESTORE_MAX)
1229             {
1230                 /* Restore to maximized position */
1231                 WINPOS_GetMinMaxInfo( hwnd, &size, &wpl.ptMaxPosition, NULL, NULL);
1232                 WIN_SetStyle( hwnd, wndPtr->dwStyle | WS_MAXIMIZE );
1233                 SetRect( rect, wpl.ptMaxPosition.x, wpl.ptMaxPosition.y, size.x, size.y );
1234                 break;
1235             }
1236         }
1237         else if (!(old_style & WS_MAXIMIZE)) break;
1238
1239         /* Restore to normal position */
1240
1241         *rect = wpl.rcNormalPosition;
1242         rect->right -= rect->left;
1243         rect->bottom -= rect->top;
1244
1245         break;
1246     }
1247
1248     WIN_ReleaseWndPtr( wndPtr );
1249     return swpFlags;
1250 }
1251
1252
1253 /***********************************************************************
1254  *              ShowWindow   (X11DRV.@)
1255  */
1256 BOOL X11DRV_ShowWindow( HWND hwnd, INT cmd )
1257 {
1258     WND*        wndPtr = WIN_FindWndPtr( hwnd );
1259     BOOL        wasVisible, showFlag;
1260     RECT        newPos = {0, 0, 0, 0};
1261     UINT        swp = 0;
1262
1263     if (!wndPtr) return FALSE;
1264     hwnd = wndPtr->hwndSelf;  /* make it a full handle */
1265
1266     TRACE("hwnd=%p, cmd=%d\n", hwnd, cmd);
1267
1268     wasVisible = (wndPtr->dwStyle & WS_VISIBLE) != 0;
1269
1270     switch(cmd)
1271     {
1272         case SW_HIDE:
1273             if (!wasVisible) goto END;
1274             swp |= SWP_HIDEWINDOW | SWP_NOSIZE | SWP_NOMOVE |
1275                         SWP_NOACTIVATE | SWP_NOZORDER;
1276             break;
1277
1278         case SW_SHOWMINNOACTIVE:
1279             swp |= SWP_NOACTIVATE | SWP_NOZORDER;
1280             /* fall through */
1281         case SW_SHOWMINIMIZED:
1282         case SW_FORCEMINIMIZE: /* FIXME: Does not work if thread is hung. */
1283             swp |= SWP_SHOWWINDOW;
1284             /* fall through */
1285         case SW_MINIMIZE:
1286             swp |= SWP_FRAMECHANGED;
1287             if( !(wndPtr->dwStyle & WS_MINIMIZE) )
1288                  swp |= WINPOS_MinMaximize( hwnd, SW_MINIMIZE, &newPos );
1289             else swp |= SWP_NOSIZE | SWP_NOMOVE;
1290             break;
1291
1292         case SW_SHOWMAXIMIZED: /* same as SW_MAXIMIZE */
1293             swp |= SWP_SHOWWINDOW | SWP_FRAMECHANGED;
1294             if( !(wndPtr->dwStyle & WS_MAXIMIZE) )
1295                  swp |= WINPOS_MinMaximize( hwnd, SW_MAXIMIZE, &newPos );
1296             else swp |= SWP_NOSIZE | SWP_NOMOVE;
1297             break;
1298
1299         case SW_SHOWNA:
1300             swp |= SWP_NOACTIVATE | SWP_NOZORDER;
1301             /* fall through */
1302         case SW_SHOW:
1303             swp |= SWP_SHOWWINDOW | SWP_NOSIZE | SWP_NOMOVE;
1304
1305             /*
1306              * ShowWindow has a little peculiar behavior that if the
1307              * window is already the topmost window, it will not
1308              * activate it.
1309              */
1310             if (GetTopWindow(NULL)==hwnd && (wasVisible || GetActiveWindow() == hwnd))
1311               swp |= SWP_NOACTIVATE;
1312
1313             break;
1314
1315         case SW_SHOWNOACTIVATE:
1316             swp |= SWP_NOACTIVATE | SWP_NOZORDER;
1317             /* fall through */
1318         case SW_SHOWNORMAL:  /* same as SW_NORMAL: */
1319         case SW_SHOWDEFAULT: /* FIXME: should have its own handler */
1320         case SW_RESTORE:
1321             swp |= SWP_SHOWWINDOW | SWP_FRAMECHANGED;
1322
1323             if( wndPtr->dwStyle & (WS_MINIMIZE | WS_MAXIMIZE) )
1324                  swp |= WINPOS_MinMaximize( hwnd, SW_RESTORE, &newPos );
1325             else swp |= SWP_NOSIZE | SWP_NOMOVE;
1326             break;
1327     }
1328
1329     showFlag = (cmd != SW_HIDE);
1330     if (showFlag != wasVisible)
1331     {
1332         SendMessageA( hwnd, WM_SHOWWINDOW, showFlag, 0 );
1333         if (!IsWindow( hwnd )) goto END;
1334     }
1335
1336     /* We can't activate a child window */
1337     if ((wndPtr->dwStyle & WS_CHILD) &&
1338         !(wndPtr->dwExStyle & WS_EX_MDICHILD))
1339         swp |= SWP_NOACTIVATE | SWP_NOZORDER;
1340
1341     SetWindowPos( hwnd, HWND_TOP, newPos.left, newPos.top,
1342                   newPos.right, newPos.bottom, LOWORD(swp) );
1343     if (cmd == SW_HIDE)
1344     {
1345         /* FIXME: This will cause the window to be activated irrespective
1346          * of whether it is owned by the same thread. Has to be done
1347          * asynchronously.
1348          */
1349
1350         if (hwnd == GetActiveWindow())
1351             WINPOS_ActivateOtherWindow(hwnd);
1352
1353         /* Revert focus to parent */
1354         if (hwnd == GetFocus() || IsChild(hwnd, GetFocus()))
1355             SetFocus( GetParent(hwnd) );
1356     }
1357     if (!IsWindow( hwnd )) goto END;
1358     else if( wndPtr->dwStyle & WS_MINIMIZE ) WINPOS_ShowIconTitle( hwnd, TRUE );
1359
1360     if (wndPtr->flags & WIN_NEED_SIZE)
1361     {
1362         /* should happen only in CreateWindowEx() */
1363         int wParam = SIZE_RESTORED;
1364
1365         wndPtr->flags &= ~WIN_NEED_SIZE;
1366         if (wndPtr->dwStyle & WS_MAXIMIZE) wParam = SIZE_MAXIMIZED;
1367         else if (wndPtr->dwStyle & WS_MINIMIZE) wParam = SIZE_MINIMIZED;
1368         SendMessageA( hwnd, WM_SIZE, wParam,
1369                      MAKELONG(wndPtr->rectClient.right-wndPtr->rectClient.left,
1370                             wndPtr->rectClient.bottom-wndPtr->rectClient.top));
1371         SendMessageA( hwnd, WM_MOVE, 0,
1372                    MAKELONG(wndPtr->rectClient.left, wndPtr->rectClient.top) );
1373     }
1374
1375 END:
1376     WIN_ReleaseWndPtr(wndPtr);
1377     return wasVisible;
1378 }
1379
1380
1381 /**********************************************************************
1382  *              X11DRV_MapNotify
1383  */
1384 void X11DRV_MapNotify( HWND hwnd, XMapEvent *event )
1385 {
1386     HWND hwndFocus = GetFocus();
1387     WND *win;
1388
1389     if (!(win = WIN_GetPtr( hwnd ))) return;
1390
1391     if ((win->dwStyle & WS_VISIBLE) &&
1392         (win->dwStyle & WS_MINIMIZE) &&
1393         (win->dwExStyle & WS_EX_MANAGED))
1394     {
1395         int x, y;
1396         unsigned int width, height, border, depth;
1397         Window root, top;
1398         RECT rect;
1399         LONG style = (win->dwStyle & ~(WS_MINIMIZE|WS_MAXIMIZE)) | WS_VISIBLE;
1400
1401         /* FIXME: hack */
1402         wine_tsx11_lock();
1403         XGetGeometry( event->display, get_whole_window(win), &root, &x, &y, &width, &height,
1404                         &border, &depth );
1405         XTranslateCoordinates( event->display, get_whole_window(win), root, 0, 0, &x, &y, &top );
1406         wine_tsx11_unlock();
1407         rect.left   = x;
1408         rect.top    = y;
1409         rect.right  = x + width;
1410         rect.bottom = y + height;
1411         X11DRV_X_to_window_rect( win, &rect );
1412
1413         DCE_InvalidateDCE( hwnd, &win->rectWindow );
1414
1415         if (win->flags & WIN_RESTORE_MAX) style |= WS_MAXIMIZE;
1416         WIN_SetStyle( hwnd, style );
1417         WIN_ReleasePtr( win );
1418
1419         SendMessageA( hwnd, WM_SHOWWINDOW, SW_RESTORE, 0 );
1420         SetWindowPos( hwnd, 0, rect.left, rect.top, rect.right-rect.left, rect.bottom-rect.top,
1421                       SWP_NOZORDER | SWP_WINE_NOHOSTMOVE );
1422     }
1423     else WIN_ReleasePtr( win );
1424     if (hwndFocus && IsChild( hwnd, hwndFocus )) X11DRV_SetFocus(hwndFocus);  /* FIXME */
1425 }
1426
1427
1428 /**********************************************************************
1429  *              X11DRV_UnmapNotify
1430  */
1431 void X11DRV_UnmapNotify( HWND hwnd, XUnmapEvent *event )
1432 {
1433     WND *win;
1434
1435     if (!(win = WIN_GetPtr( hwnd ))) return;
1436
1437     if ((win->dwStyle & WS_VISIBLE) && (win->dwExStyle & WS_EX_MANAGED))
1438     {
1439         if (win->dwStyle & WS_MAXIMIZE)
1440             win->flags |= WIN_RESTORE_MAX;
1441         else
1442             win->flags &= ~WIN_RESTORE_MAX;
1443
1444         WIN_SetStyle( hwnd, (win->dwStyle & ~WS_MAXIMIZE) | WS_MINIMIZE );
1445         WIN_ReleasePtr( win );
1446
1447         EndMenu();
1448         SendMessageA( hwnd, WM_SHOWWINDOW, SW_MINIMIZE, 0 );
1449         SetWindowPos( hwnd, 0, 0, 0, GetSystemMetrics(SM_CXICON), GetSystemMetrics(SM_CYICON),
1450                       SWP_NOMOVE | SWP_NOACTIVATE | SWP_NOZORDER | SWP_WINE_NOHOSTMOVE );
1451     }
1452     else WIN_ReleasePtr( win );
1453 }
1454
1455
1456 /***********************************************************************
1457  *           query_zorder
1458  *
1459  * Synchronize internal z-order with the window manager's.
1460  */
1461 static Window __get_common_ancestor( Display *display, Window A, Window B,
1462                                      Window** children, unsigned* total )
1463 {
1464     /* find the real root window */
1465
1466     Window      root, *childrenB;
1467     unsigned    totalB;
1468
1469     wine_tsx11_lock();
1470     while( A != B && A && B )
1471     {
1472       XQueryTree( display, A, &root, &A, children, total );
1473       XQueryTree( display, B, &root, &B, &childrenB, &totalB );
1474       if( childrenB ) XFree( childrenB );
1475       if( *children ) XFree( *children ), *children = NULL;
1476     }
1477
1478     if( A && B )
1479     {
1480         XQueryTree( display, A, &root, &B, children, total );
1481         wine_tsx11_unlock();
1482         return A;
1483     }
1484     wine_tsx11_unlock();
1485     return 0 ;
1486 }
1487
1488 static Window __get_top_decoration( Display *display, Window w, Window ancestor )
1489 {
1490     Window*     children, root, prev = w, parent = w;
1491     unsigned    total;
1492
1493     wine_tsx11_lock();
1494     do
1495     {
1496         w = parent;
1497         XQueryTree( display, w, &root, &parent, &children, &total );
1498         if( children ) XFree( children );
1499     } while( parent && parent != ancestor );
1500     wine_tsx11_unlock();
1501     TRACE("\t%08x -> %08x\n", (unsigned)prev, (unsigned)w );
1502     return ( parent ) ? w : 0 ;
1503 }
1504
1505 static unsigned __td_lookup( Window w, Window* list, unsigned max )
1506 {
1507     unsigned    i;
1508     for( i = max; i > 0; i-- ) if( list[i - 1] == w ) break;
1509     return i;
1510 }
1511
1512 static HWND query_zorder( Display *display, HWND hWndCheck)
1513 {
1514     HWND      hwndInsertAfter = HWND_TOP;
1515     Window      w, parent, *children = NULL;
1516     unsigned    total, check, pos, best;
1517     HWND *list = WIN_ListChildren( GetDesktopWindow() );
1518     HWND hwndA = 0, hwndB = 0;
1519     int i;
1520
1521     /* find at least two managed windows */
1522     if (!list) return 0;
1523     for (i = 0; list[i]; i++)
1524     {
1525         if (!(GetWindowLongW( list[i], GWL_EXSTYLE ) & WS_EX_MANAGED)) continue;
1526         if (!(GetWindowLongW( list[i], GWL_STYLE ) & WS_VISIBLE)) continue;
1527         if (!hwndA) hwndA = list[i];
1528         else
1529         {
1530             hwndB = list[i];
1531             break;
1532         }
1533     }
1534     if (!hwndA || !hwndB) goto done;
1535
1536     parent = __get_common_ancestor( display, X11DRV_get_whole_window(hwndA),
1537                                     X11DRV_get_whole_window(hwndB), &children, &total );
1538     if( parent && children )
1539     {
1540         /* w is the ancestor if hWndCheck that is a direct descendant of 'parent' */
1541
1542         w = __get_top_decoration( display, X11DRV_get_whole_window(hWndCheck), parent );
1543
1544         if( w != children[total-1] ) /* check if at the top */
1545         {
1546             /* X child at index 0 is at the bottom, at index total-1 is at the top */
1547             check = __td_lookup( w, children, total );
1548             best = total;
1549
1550             /* go through all windows in Wine z-order... */
1551             for (i = 0; list[i]; i++)
1552             {
1553                 if (list[i] == hWndCheck) continue;
1554                 if (!(GetWindowLongW( list[i], GWL_EXSTYLE ) & WS_EX_MANAGED)) continue;
1555                 if (!(w = __get_top_decoration( display, X11DRV_get_whole_window(list[i]),
1556                                                 parent ))) continue;
1557                 pos = __td_lookup( w, children, total );
1558                 if( pos < best && pos > check )
1559                 {
1560                     /* find a nearest Wine window precedes hWndCheck in the real z-order */
1561                     best = pos;
1562                     hwndInsertAfter = list[i];
1563                 }
1564                 if( best - check == 1 ) break;
1565             }
1566         }
1567     }
1568     wine_tsx11_lock();
1569     if( children ) XFree( children );
1570     wine_tsx11_unlock();
1571
1572  done:
1573     HeapFree( GetProcessHeap(), 0, list );
1574     return hwndInsertAfter;
1575 }
1576
1577
1578 /***********************************************************************
1579  *              X11DRV_handle_desktop_resize
1580  */
1581 void X11DRV_handle_desktop_resize( unsigned int width, unsigned int height )
1582 {
1583     RECT rect;
1584     HWND hwnd = GetDesktopWindow();
1585
1586     screen_width  = width;
1587     screen_height = height;
1588     TRACE("desktop %p change to (%dx%d)\n", hwnd, width, height);
1589     SetRect( &rect, 0, 0, width, height );
1590     WIN_SetRectangles( hwnd, &rect, &rect );
1591     SendMessageTimeoutW( HWND_BROADCAST, WM_DISPLAYCHANGE, screen_depth,
1592                          MAKELPARAM( width, height ), SMTO_ABORTIFHUNG, 2000, NULL );
1593 }
1594
1595
1596 /***********************************************************************
1597  *              X11DRV_ConfigureNotify
1598  */
1599 void X11DRV_ConfigureNotify( HWND hwnd, XConfigureEvent *event )
1600 {
1601     HWND oldInsertAfter;
1602     struct x11drv_win_data *data;
1603     WND *win;
1604     RECT rect;
1605     WINDOWPOS winpos;
1606     int x = event->x, y = event->y;
1607
1608     if (!(win = WIN_GetPtr( hwnd ))) return;
1609     data = win->pDriverData;
1610
1611     /* Get geometry */
1612
1613     if (!event->send_event)  /* normal event, need to map coordinates to the root */
1614     {
1615         Window child;
1616         wine_tsx11_lock();
1617         XTranslateCoordinates( event->display, data->whole_window, root_window,
1618                                0, 0, &x, &y, &child );
1619         wine_tsx11_unlock();
1620     }
1621     rect.left   = x;
1622     rect.top    = y;
1623     rect.right  = x + event->width;
1624     rect.bottom = y + event->height;
1625     TRACE( "win %p new X rect %ld,%ld,%ldx%ld (event %d,%d,%dx%d)\n",
1626            hwnd, rect.left, rect.top, rect.right-rect.left, rect.bottom-rect.top,
1627            event->x, event->y, event->width, event->height );
1628     X11DRV_X_to_window_rect( win, &rect );
1629     WIN_ReleasePtr( win );
1630
1631     winpos.hwnd  = hwnd;
1632     winpos.x     = rect.left;
1633     winpos.y     = rect.top;
1634     winpos.cx    = rect.right - rect.left;
1635     winpos.cy    = rect.bottom - rect.top;
1636     winpos.flags = SWP_NOACTIVATE;
1637
1638     /* Get Z-order (FIXME) */
1639
1640     winpos.hwndInsertAfter = query_zorder( event->display, hwnd );
1641
1642     /* needs to find the first Visible Window above the current one */
1643     oldInsertAfter = hwnd;
1644     for (;;)
1645     {
1646         oldInsertAfter = GetWindow( oldInsertAfter, GW_HWNDPREV );
1647         if (!oldInsertAfter)
1648         {
1649             oldInsertAfter = HWND_TOP;
1650             break;
1651         }
1652         if (GetWindowLongA( oldInsertAfter, GWL_STYLE ) & WS_VISIBLE) break;
1653     }
1654
1655     /* Compare what has changed */
1656
1657     GetWindowRect( hwnd, &rect );
1658     if (rect.left == winpos.x && rect.top == winpos.y) winpos.flags |= SWP_NOMOVE;
1659     else
1660         TRACE( "%p moving from (%ld,%ld) to (%d,%d)\n",
1661                hwnd, rect.left, rect.top, winpos.x, winpos.y );
1662
1663     if ((rect.right - rect.left == winpos.cx && rect.bottom - rect.top == winpos.cy) ||
1664         IsIconic(hwnd) ||
1665         (IsRectEmpty( &rect ) && winpos.cx == 1 && winpos.cy == 1))
1666         winpos.flags |= SWP_NOSIZE;
1667     else
1668         TRACE( "%p resizing from (%ldx%ld) to (%dx%d)\n",
1669                hwnd, rect.right - rect.left, rect.bottom - rect.top,
1670                winpos.cx, winpos.cy );
1671
1672     if (winpos.hwndInsertAfter == oldInsertAfter) winpos.flags |= SWP_NOZORDER;
1673     else
1674         TRACE( "%p restacking from after %p to after %p\n",
1675                hwnd, oldInsertAfter, winpos.hwndInsertAfter );
1676
1677     /* if nothing changed, don't do anything */
1678     if (winpos.flags == (SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE)) return;
1679
1680     SetWindowPos( hwnd, winpos.hwndInsertAfter, winpos.x, winpos.y,
1681                   winpos.cx, winpos.cy, winpos.flags | SWP_WINE_NOHOSTMOVE );
1682 }
1683
1684
1685 /***********************************************************************
1686  *              SetWindowRgn  (X11DRV.@)
1687  *
1688  * Assign specified region to window (for non-rectangular windows)
1689  */
1690 int X11DRV_SetWindowRgn( HWND hwnd, HRGN hrgn, BOOL redraw )
1691 {
1692     WND *wndPtr;
1693
1694     if ((wndPtr = WIN_GetPtr( hwnd )) == WND_OTHER_PROCESS)
1695     {
1696         if (IsWindow( hwnd ))
1697             FIXME( "not supported on other process window %p\n", hwnd );
1698         wndPtr = NULL;
1699     }
1700     if (!wndPtr)
1701     {
1702         SetLastError( ERROR_INVALID_WINDOW_HANDLE );
1703         return FALSE;
1704     }
1705
1706     if (wndPtr->hrgnWnd == hrgn)
1707     {
1708         WIN_ReleasePtr( wndPtr );
1709         return TRUE;
1710     }
1711
1712     if (wndPtr->hrgnWnd)
1713     {
1714         /* delete previous region */
1715         DeleteObject(wndPtr->hrgnWnd);
1716         wndPtr->hrgnWnd = 0;
1717     }
1718     wndPtr->hrgnWnd = hrgn;
1719
1720 #ifdef HAVE_LIBXSHAPE
1721     {
1722         Display *display = thread_display();
1723         X11DRV_WND_DATA *data = wndPtr->pDriverData;
1724
1725         if (data->whole_window)
1726         {
1727             if (!hrgn)
1728             {
1729                 wine_tsx11_lock();
1730                 XShapeCombineMask( display, data->whole_window,
1731                                    ShapeBounding, 0, 0, None, ShapeSet );
1732                 wine_tsx11_unlock();
1733             }
1734             else
1735             {
1736                 XRectangle *aXRect;
1737                 int x_offset, y_offset;
1738                 DWORD size;
1739                 DWORD dwBufferSize = GetRegionData(hrgn, 0, NULL);
1740                 PRGNDATA pRegionData = HeapAlloc(GetProcessHeap(), 0, dwBufferSize);
1741                 if (!pRegionData)
1742                 {
1743                     WIN_ReleasePtr( wndPtr );
1744                     return TRUE;
1745                 }
1746                 GetRegionData(hrgn, dwBufferSize, pRegionData);
1747                 size = pRegionData->rdh.nCount;
1748                 x_offset = wndPtr->rectWindow.left - data->whole_rect.left;
1749                 y_offset = wndPtr->rectWindow.top - data->whole_rect.top;
1750                 /* convert region's "Windows rectangles" to XRectangles */
1751                 aXRect = HeapAlloc(GetProcessHeap(), 0, size * sizeof(*aXRect) );
1752                 if (aXRect)
1753                 {
1754                     XRectangle* pCurrRect = aXRect;
1755                     RECT *pRect = (RECT*) pRegionData->Buffer;
1756                     for (; pRect < ((RECT*) pRegionData->Buffer) + size ; ++pRect, ++pCurrRect)
1757                     {
1758                         pCurrRect->x      = pRect->left + x_offset;
1759                         pCurrRect->y      = pRect->top + y_offset;
1760                         pCurrRect->height = pRect->bottom - pRect->top;
1761                         pCurrRect->width  = pRect->right  - pRect->left;
1762
1763                         TRACE("Rectangle %04d of %04ld data: X=%04d, Y=%04d, Height=%04d, Width=%04d.\n",
1764                               pRect - (RECT*) pRegionData->Buffer,
1765                               size,
1766                               pCurrRect->x,
1767                               pCurrRect->y,
1768                               pCurrRect->height,
1769                               pCurrRect->width);
1770                     }
1771
1772                     /* shape = non-rectangular windows (X11/extensions) */
1773                     wine_tsx11_lock();
1774                     XShapeCombineRectangles( display, data->whole_window, ShapeBounding,
1775                                              0, 0, aXRect,
1776                                              pCurrRect - aXRect, ShapeSet, YXBanded );
1777                     wine_tsx11_unlock();
1778                     HeapFree(GetProcessHeap(), 0, aXRect );
1779                 }
1780                 HeapFree(GetProcessHeap(), 0, pRegionData);
1781             }
1782         }
1783     }
1784 #endif  /* HAVE_LIBXSHAPE */
1785
1786     WIN_ReleasePtr( wndPtr );
1787     if (redraw) RedrawWindow( hwnd, NULL, 0, RDW_FRAME | RDW_INVALIDATE | RDW_ERASE );
1788     return TRUE;
1789 }
1790
1791
1792 /***********************************************************************
1793  *           draw_moving_frame
1794  *
1795  * Draw the frame used when moving or resizing window.
1796  *
1797  * FIXME:  This causes problems in Win95 mode.  (why?)
1798  */
1799 static void draw_moving_frame( HDC hdc, RECT *rect, BOOL thickframe )
1800 {
1801     if (thickframe)
1802     {
1803         const int width = GetSystemMetrics(SM_CXFRAME);
1804         const int height = GetSystemMetrics(SM_CYFRAME);
1805
1806         HBRUSH hbrush = SelectObject( hdc, GetStockObject( GRAY_BRUSH ) );
1807         PatBlt( hdc, rect->left, rect->top,
1808                 rect->right - rect->left - width, height, PATINVERT );
1809         PatBlt( hdc, rect->left, rect->top + height, width,
1810                 rect->bottom - rect->top - height, PATINVERT );
1811         PatBlt( hdc, rect->left + width, rect->bottom - 1,
1812                 rect->right - rect->left - width, -height, PATINVERT );
1813         PatBlt( hdc, rect->right - 1, rect->top, -width,
1814                 rect->bottom - rect->top - height, PATINVERT );
1815         SelectObject( hdc, hbrush );
1816     }
1817     else DrawFocusRect( hdc, rect );
1818 }
1819
1820
1821 /***********************************************************************
1822  *           start_size_move
1823  *
1824  * Initialisation of a move or resize, when initiatied from a menu choice.
1825  * Return hit test code for caption or sizing border.
1826  */
1827 static LONG start_size_move( HWND hwnd, WPARAM wParam, POINT *capturePoint, LONG style )
1828 {
1829     LONG hittest = 0;
1830     POINT pt;
1831     MSG msg;
1832     RECT rectWindow;
1833
1834     GetWindowRect( hwnd, &rectWindow );
1835
1836     if ((wParam & 0xfff0) == SC_MOVE)
1837     {
1838         /* Move pointer at the center of the caption */
1839         RECT rect;
1840         NC_GetInsideRect( hwnd, &rect );
1841         if (style & WS_SYSMENU)
1842             rect.left += GetSystemMetrics(SM_CXSIZE) + 1;
1843         if (style & WS_MINIMIZEBOX)
1844             rect.right -= GetSystemMetrics(SM_CXSIZE) + 1;
1845         if (style & WS_MAXIMIZEBOX)
1846             rect.right -= GetSystemMetrics(SM_CXSIZE) + 1;
1847         pt.x = rectWindow.left + (rect.right - rect.left) / 2;
1848         pt.y = rectWindow.top + rect.top + GetSystemMetrics(SM_CYSIZE)/2;
1849         hittest = HTCAPTION;
1850         *capturePoint = pt;
1851     }
1852     else  /* SC_SIZE */
1853     {
1854         while(!hittest)
1855         {
1856             GetMessageW( &msg, 0, WM_KEYFIRST, WM_MOUSELAST );
1857             if (CallMsgFilterW( &msg, MSGF_SIZE )) continue;
1858
1859             switch(msg.message)
1860             {
1861             case WM_MOUSEMOVE:
1862                 hittest = NC_HandleNCHitTest( hwnd, msg.pt );
1863                 if ((hittest < HTLEFT) || (hittest > HTBOTTOMRIGHT))
1864                     hittest = 0;
1865                 break;
1866
1867             case WM_LBUTTONUP:
1868                 return 0;
1869
1870             case WM_KEYDOWN:
1871                 switch(msg.wParam)
1872                 {
1873                 case VK_UP:
1874                     hittest = HTTOP;
1875                     pt.x =(rectWindow.left+rectWindow.right)/2;
1876                     pt.y = rectWindow.top + GetSystemMetrics(SM_CYFRAME) / 2;
1877                     break;
1878                 case VK_DOWN:
1879                     hittest = HTBOTTOM;
1880                     pt.x =(rectWindow.left+rectWindow.right)/2;
1881                     pt.y = rectWindow.bottom - GetSystemMetrics(SM_CYFRAME) / 2;
1882                     break;
1883                 case VK_LEFT:
1884                     hittest = HTLEFT;
1885                     pt.x = rectWindow.left + GetSystemMetrics(SM_CXFRAME) / 2;
1886                     pt.y =(rectWindow.top+rectWindow.bottom)/2;
1887                     break;
1888                 case VK_RIGHT:
1889                     hittest = HTRIGHT;
1890                     pt.x = rectWindow.right - GetSystemMetrics(SM_CXFRAME) / 2;
1891                     pt.y =(rectWindow.top+rectWindow.bottom)/2;
1892                     break;
1893                 case VK_RETURN:
1894                 case VK_ESCAPE: return 0;
1895                 }
1896             }
1897         }
1898         *capturePoint = pt;
1899     }
1900     SetCursorPos( pt.x, pt.y );
1901     NC_HandleSetCursor( hwnd, (WPARAM)hwnd, MAKELONG( hittest, WM_MOUSEMOVE ));
1902     return hittest;
1903 }
1904
1905
1906 /***********************************************************************
1907  *           set_movesize_capture
1908  */
1909 static void set_movesize_capture( HWND hwnd )
1910 {
1911     HWND previous = 0;
1912
1913     SERVER_START_REQ( set_capture_window )
1914     {
1915         req->handle = hwnd;
1916         req->flags  = CAPTURE_MOVESIZE;
1917         if (!wine_server_call_err( req ))
1918         {
1919             previous = reply->previous;
1920             hwnd = reply->full_handle;
1921         }
1922     }
1923     SERVER_END_REQ;
1924     if (previous && previous != hwnd)
1925         SendMessageW( previous, WM_CAPTURECHANGED, 0, (LPARAM)hwnd );
1926 }
1927
1928
1929 /***********************************************************************
1930  *           SysCommandSizeMove   (X11DRV.@)
1931  *
1932  * Perform SC_MOVE and SC_SIZE commands.
1933  */
1934 void X11DRV_SysCommandSizeMove( HWND hwnd, WPARAM wParam )
1935 {
1936     MSG msg;
1937     RECT sizingRect, mouseRect, origRect;
1938     HDC hdc;
1939     HWND parent;
1940     LONG hittest = (LONG)(wParam & 0x0f);
1941     HCURSOR hDragCursor = 0, hOldCursor = 0;
1942     POINT minTrack, maxTrack;
1943     POINT capturePoint, pt;
1944     LONG style = GetWindowLongA( hwnd, GWL_STYLE );
1945     LONG exstyle = GetWindowLongA( hwnd, GWL_EXSTYLE );
1946     BOOL    thickframe = HAS_THICKFRAME( style, exstyle );
1947     BOOL    iconic = style & WS_MINIMIZE;
1948     BOOL    moved = FALSE;
1949     DWORD     dwPoint = GetMessagePos ();
1950     BOOL DragFullWindows = FALSE;
1951     BOOL grab;
1952     int iWndsLocks;
1953     Display *old_gdi_display = NULL;
1954     Display *display = thread_display();
1955
1956     SystemParametersInfoA(SPI_GETDRAGFULLWINDOWS, 0, &DragFullWindows, 0);
1957
1958     pt.x = (short)LOWORD(dwPoint);
1959     pt.y = (short)HIWORD(dwPoint);
1960     capturePoint = pt;
1961
1962     if (IsZoomed(hwnd) || !IsWindowVisible(hwnd) || (exstyle & WS_EX_MANAGED)) return;
1963
1964     if ((wParam & 0xfff0) == SC_MOVE)
1965     {
1966         if (!hittest) hittest = start_size_move( hwnd, wParam, &capturePoint, style );
1967         if (!hittest) return;
1968     }
1969     else  /* SC_SIZE */
1970     {
1971         if (!thickframe) return;
1972         if ( hittest && ((wParam & 0xfff0) != SC_MOUSEMENU) )
1973             hittest += (HTLEFT - WMSZ_LEFT);
1974         else
1975         {
1976             set_movesize_capture( hwnd );
1977             hittest = start_size_move( hwnd, wParam, &capturePoint, style );
1978             if (!hittest)
1979             {
1980                 set_movesize_capture(0);
1981                 return;
1982             }
1983         }
1984     }
1985
1986       /* Get min/max info */
1987
1988     WINPOS_GetMinMaxInfo( hwnd, NULL, NULL, &minTrack, &maxTrack );
1989     GetWindowRect( hwnd, &sizingRect );
1990     if (style & WS_CHILD)
1991     {
1992         parent = GetParent(hwnd);
1993         /* make sizing rect relative to parent */
1994         MapWindowPoints( 0, parent, (POINT*)&sizingRect, 2 );
1995         GetClientRect( parent, &mouseRect );
1996     }
1997     else
1998     {
1999         parent = 0;
2000         SetRect(&mouseRect, 0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN));
2001     }
2002     origRect = sizingRect;
2003
2004     if (ON_LEFT_BORDER(hittest))
2005     {
2006         mouseRect.left  = max( mouseRect.left, sizingRect.right-maxTrack.x );
2007         mouseRect.right = min( mouseRect.right, sizingRect.right-minTrack.x );
2008     }
2009     else if (ON_RIGHT_BORDER(hittest))
2010     {
2011         mouseRect.left  = max( mouseRect.left, sizingRect.left+minTrack.x );
2012         mouseRect.right = min( mouseRect.right, sizingRect.left+maxTrack.x );
2013     }
2014     if (ON_TOP_BORDER(hittest))
2015     {
2016         mouseRect.top    = max( mouseRect.top, sizingRect.bottom-maxTrack.y );
2017         mouseRect.bottom = min( mouseRect.bottom,sizingRect.bottom-minTrack.y);
2018     }
2019     else if (ON_BOTTOM_BORDER(hittest))
2020     {
2021         mouseRect.top    = max( mouseRect.top, sizingRect.top+minTrack.y );
2022         mouseRect.bottom = min( mouseRect.bottom, sizingRect.top+maxTrack.y );
2023     }
2024     if (parent) MapWindowPoints( parent, 0, (LPPOINT)&mouseRect, 2 );
2025
2026     /* Retrieve a default cache DC (without using the window style) */
2027     hdc = GetDCEx( parent, 0, DCX_CACHE );
2028
2029     if( iconic ) /* create a cursor for dragging */
2030     {
2031         hDragCursor = (HCURSOR)GetClassLongA( hwnd, GCL_HICON);
2032         if( !hDragCursor ) hDragCursor = (HCURSOR)SendMessageA( hwnd, WM_QUERYDRAGICON, 0, 0L);
2033         if( !hDragCursor ) iconic = FALSE;
2034     }
2035
2036     /* repaint the window before moving it around */
2037     RedrawWindow( hwnd, NULL, 0, RDW_UPDATENOW | RDW_ALLCHILDREN );
2038
2039     SendMessageA( hwnd, WM_ENTERSIZEMOVE, 0, 0 );
2040     set_movesize_capture( hwnd );
2041
2042     /* grab the server only when moving top-level windows without desktop */
2043     grab = (!DragFullWindows && !parent && (root_window == DefaultRootWindow(gdi_display)));
2044
2045     wine_tsx11_lock();
2046     if (grab)
2047     {
2048         XSync( gdi_display, False );
2049         XGrabServer( display );
2050         XSync( display, False );
2051         /* switch gdi display to the thread display, since the server is grabbed */
2052         old_gdi_display = gdi_display;
2053         gdi_display = display;
2054     }
2055     XGrabPointer( display, X11DRV_get_whole_window(hwnd), False,
2056                   PointerMotionMask | ButtonPressMask | ButtonReleaseMask,
2057                   GrabModeAsync, GrabModeAsync,
2058                   parent ? X11DRV_get_client_window(parent) : root_window,
2059                   None, CurrentTime );
2060     wine_tsx11_unlock();
2061
2062     while(1)
2063     {
2064         int dx = 0, dy = 0;
2065
2066         if (!GetMessageW( &msg, 0, WM_KEYFIRST, WM_MOUSELAST )) break;
2067         if (CallMsgFilterW( &msg, MSGF_SIZE )) continue;
2068
2069         /* Exit on button-up, Return, or Esc */
2070         if ((msg.message == WM_LBUTTONUP) ||
2071             ((msg.message == WM_KEYDOWN) &&
2072              ((msg.wParam == VK_RETURN) || (msg.wParam == VK_ESCAPE)))) break;
2073
2074         if ((msg.message != WM_KEYDOWN) && (msg.message != WM_MOUSEMOVE))
2075             continue;  /* We are not interested in other messages */
2076
2077         pt = msg.pt;
2078
2079         if (msg.message == WM_KEYDOWN) switch(msg.wParam)
2080         {
2081         case VK_UP:    pt.y -= 8; break;
2082         case VK_DOWN:  pt.y += 8; break;
2083         case VK_LEFT:  pt.x -= 8; break;
2084         case VK_RIGHT: pt.x += 8; break;
2085         }
2086
2087         pt.x = max( pt.x, mouseRect.left );
2088         pt.x = min( pt.x, mouseRect.right );
2089         pt.y = max( pt.y, mouseRect.top );
2090         pt.y = min( pt.y, mouseRect.bottom );
2091
2092         dx = pt.x - capturePoint.x;
2093         dy = pt.y - capturePoint.y;
2094
2095         if (dx || dy)
2096         {
2097             if( !moved )
2098             {
2099                 moved = TRUE;
2100
2101                 if( iconic ) /* ok, no system popup tracking */
2102                 {
2103                     hOldCursor = SetCursor(hDragCursor);
2104                     ShowCursor( TRUE );
2105                     WINPOS_ShowIconTitle( hwnd, FALSE );
2106                 }
2107                 else if(!DragFullWindows)
2108                     draw_moving_frame( hdc, &sizingRect, thickframe );
2109             }
2110
2111             if (msg.message == WM_KEYDOWN) SetCursorPos( pt.x, pt.y );
2112             else
2113             {
2114                 RECT newRect = sizingRect;
2115                 WPARAM wpSizingHit = 0;
2116
2117                 if (hittest == HTCAPTION) OffsetRect( &newRect, dx, dy );
2118                 if (ON_LEFT_BORDER(hittest)) newRect.left += dx;
2119                 else if (ON_RIGHT_BORDER(hittest)) newRect.right += dx;
2120                 if (ON_TOP_BORDER(hittest)) newRect.top += dy;
2121                 else if (ON_BOTTOM_BORDER(hittest)) newRect.bottom += dy;
2122                 if(!iconic && !DragFullWindows) draw_moving_frame( hdc, &sizingRect, thickframe );
2123                 capturePoint = pt;
2124
2125                 /* determine the hit location */
2126                 if (hittest >= HTLEFT && hittest <= HTBOTTOMRIGHT)
2127                     wpSizingHit = WMSZ_LEFT + (hittest - HTLEFT);
2128                 SendMessageA( hwnd, WM_SIZING, wpSizingHit, (LPARAM)&newRect );
2129
2130                 if (!iconic)
2131                 {
2132                     if(!DragFullWindows)
2133                         draw_moving_frame( hdc, &newRect, thickframe );
2134                     else {
2135                         /* To avoid any deadlocks, all the locks on the windows
2136                            structures must be suspended before the SetWindowPos */
2137                         iWndsLocks = WIN_SuspendWndsLock();
2138                         SetWindowPos( hwnd, 0, newRect.left, newRect.top,
2139                                       newRect.right - newRect.left,
2140                                       newRect.bottom - newRect.top,
2141                                       ( hittest == HTCAPTION ) ? SWP_NOSIZE : 0 );
2142                         WIN_RestoreWndsLock(iWndsLocks);
2143                     }
2144                 }
2145                 sizingRect = newRect;
2146             }
2147         }
2148     }
2149
2150     set_movesize_capture(0);
2151     if( iconic )
2152     {
2153         if( moved ) /* restore cursors, show icon title later on */
2154         {
2155             ShowCursor( FALSE );
2156             SetCursor( hOldCursor );
2157         }
2158     }
2159     else if (moved && !DragFullWindows)
2160         draw_moving_frame( hdc, &sizingRect, thickframe );
2161
2162     ReleaseDC( parent, hdc );
2163
2164     wine_tsx11_lock();
2165     XUngrabPointer( display, CurrentTime );
2166     if (grab)
2167     {
2168         XSync( display, False );
2169         XUngrabServer( display );
2170         XSync( display, False );
2171         gdi_display = old_gdi_display;
2172     }
2173     wine_tsx11_unlock();
2174
2175     if (HOOK_CallHooks( WH_CBT, HCBT_MOVESIZE, (WPARAM)hwnd, (LPARAM)&sizingRect, TRUE ))
2176         moved = FALSE;
2177
2178     SendMessageA( hwnd, WM_EXITSIZEMOVE, 0, 0 );
2179     SendMessageA( hwnd, WM_SETVISIBLE, !IsIconic(hwnd), 0L);
2180
2181     /* window moved or resized */
2182     if (moved)
2183     {
2184         /* To avoid any deadlocks, all the locks on the windows
2185            structures must be suspended before the SetWindowPos */
2186         iWndsLocks = WIN_SuspendWndsLock();
2187
2188         /* if the moving/resizing isn't canceled call SetWindowPos
2189          * with the new position or the new size of the window
2190          */
2191         if (!((msg.message == WM_KEYDOWN) && (msg.wParam == VK_ESCAPE)) )
2192         {
2193             /* NOTE: SWP_NOACTIVATE prevents document window activation in Word 6 */
2194             if(!DragFullWindows)
2195                 SetWindowPos( hwnd, 0, sizingRect.left, sizingRect.top,
2196                               sizingRect.right - sizingRect.left,
2197                               sizingRect.bottom - sizingRect.top,
2198                               ( hittest == HTCAPTION ) ? SWP_NOSIZE : 0 );
2199         }
2200         else
2201         { /* restore previous size/position */
2202             if(DragFullWindows)
2203                 SetWindowPos( hwnd, 0, origRect.left, origRect.top,
2204                               origRect.right - origRect.left,
2205                               origRect.bottom - origRect.top,
2206                               ( hittest == HTCAPTION ) ? SWP_NOSIZE : 0 );
2207         }
2208
2209         WIN_RestoreWndsLock(iWndsLocks);
2210     }
2211
2212     if (IsIconic(hwnd))
2213     {
2214         /* Single click brings up the system menu when iconized */
2215
2216         if( !moved )
2217         {
2218             if(style & WS_SYSMENU )
2219                 SendMessageA( hwnd, WM_SYSCOMMAND,
2220                               SC_MOUSEMENU + HTSYSMENU, MAKELONG(pt.x,pt.y));
2221         }
2222         else WINPOS_ShowIconTitle( hwnd, TRUE );
2223     }
2224 }
2225
2226
2227 /***********************************************************************
2228  *              ForceWindowRaise   (X11DRV.@)
2229  *
2230  * Raise a window on top of the X stacking order, while preserving
2231  * the correct Windows Z order.
2232  *
2233  * FIXME: this should go away.
2234  */
2235 void X11DRV_ForceWindowRaise( HWND hwnd )
2236 {
2237     int i = 0;
2238     HWND *list;
2239     XWindowChanges winChanges;
2240     Display *display = thread_display();
2241     WND *wndPtr = WIN_FindWndPtr( hwnd );
2242
2243     if (!wndPtr) return;
2244
2245     if ((wndPtr->dwExStyle & WS_EX_MANAGED) ||
2246         wndPtr->parent != GetDesktopWindow() ||
2247         IsRectEmpty( &wndPtr->rectWindow ) ||
2248         !get_whole_window(wndPtr))
2249     {
2250         WIN_ReleaseWndPtr( wndPtr );
2251         return;
2252     }
2253     WIN_ReleaseWndPtr( wndPtr );
2254
2255     /* Raise all windows up to wndPtr according to their Z order.
2256      * (it would be easier with sibling-related Below but it doesn't
2257      * work very well with SGI mwm for instance)
2258      */
2259     winChanges.stack_mode = Above;
2260     if (!(list = WIN_ListChildren( GetDesktopWindow() ))) return;
2261     while (list[i] && list[i] != hwnd) i++;
2262     if (list[i])
2263     {
2264         for ( ; i >= 0; i--)
2265         {
2266             WND *ptr = WIN_FindWndPtr( list[i] );
2267             if (!ptr) continue;
2268             if (!IsRectEmpty( &ptr->rectWindow ) && get_whole_window(ptr))
2269             {
2270                 wine_tsx11_lock();
2271                 XReconfigureWMWindow( display, get_whole_window(ptr), 0, CWStackMode, &winChanges );
2272                 wine_tsx11_unlock();
2273             }
2274             WIN_ReleaseWndPtr( ptr );
2275         }
2276     }
2277     HeapFree( GetProcessHeap(), 0, list );
2278 }