ShowWindow should not optimize SW_HIDE, it's the job of SetWindowPos.
[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     if (pWinpos->flags & (SWP_SHOWWINDOW | SWP_HIDEWINDOW))
557     {
558         BOOL wasVisible, showFlag;
559
560         wasVisible = (GetWindowLongW(pWinpos->hwnd, GWL_STYLE) & WS_VISIBLE) != 0;
561         showFlag = !(pWinpos->flags & SWP_HIDEWINDOW);
562         if (showFlag != wasVisible)
563             SendMessageW(pWinpos->hwnd, WM_SHOWWINDOW, showFlag, 0);
564     }
565
566     /* Send WM_WINDOWPOSCHANGING message */
567
568     if (!(pWinpos->flags & SWP_NOSENDCHANGING))
569         SendMessageW( pWinpos->hwnd, WM_WINDOWPOSCHANGING, 0, (LPARAM)pWinpos );
570
571     if (!(wndPtr = WIN_GetPtr( pWinpos->hwnd )) || wndPtr == WND_OTHER_PROCESS) return FALSE;
572
573     /* Calculate new position and size */
574
575     *pNewWindowRect = wndPtr->rectWindow;
576     *pNewClientRect = (wndPtr->dwStyle & WS_MINIMIZE) ? wndPtr->rectWindow
577                                                     : wndPtr->rectClient;
578
579     if (!(pWinpos->flags & SWP_NOSIZE))
580     {
581         pNewWindowRect->right  = pNewWindowRect->left + pWinpos->cx;
582         pNewWindowRect->bottom = pNewWindowRect->top + pWinpos->cy;
583     }
584     if (!(pWinpos->flags & SWP_NOMOVE))
585     {
586         pNewWindowRect->left    = pWinpos->x;
587         pNewWindowRect->top     = pWinpos->y;
588         pNewWindowRect->right  += pWinpos->x - wndPtr->rectWindow.left;
589         pNewWindowRect->bottom += pWinpos->y - wndPtr->rectWindow.top;
590
591         OffsetRect( pNewClientRect, pWinpos->x - wndPtr->rectWindow.left,
592                                     pWinpos->y - wndPtr->rectWindow.top );
593     }
594     pWinpos->flags |= SWP_NOCLIENTMOVE | SWP_NOCLIENTSIZE;
595     WIN_ReleasePtr( wndPtr );
596     return TRUE;
597 }
598
599 /***********************************************************************
600  *           SWP_DoNCCalcSize
601  */
602 static UINT SWP_DoNCCalcSize( WINDOWPOS* pWinpos, RECT* pNewWindowRect, RECT* pNewClientRect )
603 {
604     UINT wvrFlags = 0;
605     WND *wndPtr;
606
607     if (!(wndPtr = WIN_GetPtr( pWinpos->hwnd )) || wndPtr == WND_OTHER_PROCESS) return 0;
608
609       /* Send WM_NCCALCSIZE message to get new client area */
610     if( (pWinpos->flags & (SWP_FRAMECHANGED | SWP_NOSIZE)) != SWP_NOSIZE )
611     {
612         NCCALCSIZE_PARAMS params;
613         WINDOWPOS winposCopy;
614
615         params.rgrc[0] = *pNewWindowRect;
616         params.rgrc[1] = wndPtr->rectWindow;
617         params.rgrc[2] = wndPtr->rectClient;
618         params.lppos = &winposCopy;
619         winposCopy = *pWinpos;
620         WIN_ReleasePtr( wndPtr );
621
622         wvrFlags = SendMessageW( pWinpos->hwnd, WM_NCCALCSIZE, TRUE, (LPARAM)&params );
623
624         TRACE( "(%ld,%ld)-(%ld,%ld)\n", params.rgrc[0].left, params.rgrc[0].top,
625                params.rgrc[0].right, params.rgrc[0].bottom );
626
627         /* If the application send back garbage, ignore it */
628         if (params.rgrc[0].left <= params.rgrc[0].right &&
629             params.rgrc[0].top <= params.rgrc[0].bottom)
630             *pNewClientRect = params.rgrc[0];
631
632          /* FIXME: WVR_ALIGNxxx */
633
634         if (!(wndPtr = WIN_GetPtr( pWinpos->hwnd )) || wndPtr == WND_OTHER_PROCESS) return 0;
635
636         if( pNewClientRect->left != wndPtr->rectClient.left ||
637             pNewClientRect->top != wndPtr->rectClient.top )
638             pWinpos->flags &= ~SWP_NOCLIENTMOVE;
639
640         if( (pNewClientRect->right - pNewClientRect->left !=
641              wndPtr->rectClient.right - wndPtr->rectClient.left) ||
642             (pNewClientRect->bottom - pNewClientRect->top !=
643              wndPtr->rectClient.bottom - wndPtr->rectClient.top) )
644             pWinpos->flags &= ~SWP_NOCLIENTSIZE;
645     }
646     else
647     {
648         if (!(pWinpos->flags & SWP_NOMOVE) &&
649             (pNewClientRect->left != wndPtr->rectClient.left ||
650              pNewClientRect->top != wndPtr->rectClient.top))
651             pWinpos->flags &= ~SWP_NOCLIENTMOVE;
652     }
653     WIN_ReleasePtr( wndPtr );
654     return wvrFlags;
655 }
656
657
658 /***********************************************************************
659  *           SWP_DoOwnedPopups
660  *
661  * fix Z order taking into account owned popups -
662  * basically we need to maintain them above the window that owns them
663  *
664  * FIXME: hide/show owned popups when owner visibility changes.
665  */
666 static HWND SWP_DoOwnedPopups(HWND hwnd, HWND hwndInsertAfter)
667 {
668     HWND *list = NULL;
669     HWND owner = GetWindow( hwnd, GW_OWNER );
670     LONG style = GetWindowLongW( hwnd, GWL_STYLE );
671
672     WARN("(%p) hInsertAfter = %p\n", hwnd, hwndInsertAfter );
673
674     if ((style & WS_POPUP) && owner)
675     {
676         /* make sure this popup stays above the owner */
677
678         HWND hwndLocalPrev = HWND_TOP;
679
680         if( hwndInsertAfter != HWND_TOP )
681         {
682             if ((list = WIN_ListChildren( GetDesktopWindow() )))
683             {
684                 int i;
685                 for (i = 0; list[i]; i++)
686                 {
687                     if (list[i] == owner) break;
688                     if (list[i] != hwnd) hwndLocalPrev = list[i];
689                     if (hwndLocalPrev == hwndInsertAfter) break;
690                 }
691                 hwndInsertAfter = hwndLocalPrev;
692             }
693         }
694     }
695     else if (style & WS_CHILD) return hwndInsertAfter;
696
697     if (!list) list = WIN_ListChildren( GetDesktopWindow() );
698     if (list)
699     {
700         int i;
701         for (i = 0; list[i]; i++)
702         {
703             if (list[i] == hwnd) break;
704             if ((GetWindowLongW( list[i], GWL_STYLE ) & WS_POPUP) &&
705                 GetWindow( list[i], GW_OWNER ) == hwnd)
706             {
707                 SetWindowPos( list[i], hwndInsertAfter, 0, 0, 0, 0,
708                               SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE |
709                               SWP_NOSENDCHANGING | SWP_DEFERERASE );
710                 hwndInsertAfter = list[i];
711             }
712         }
713         HeapFree( GetProcessHeap(), 0, list );
714     }
715
716     return hwndInsertAfter;
717 }
718
719
720 /* fix redundant flags and values in the WINDOWPOS structure */
721 static BOOL fixup_flags( WINDOWPOS *winpos )
722 {
723     WND *wndPtr = WIN_GetPtr( winpos->hwnd );
724     BOOL ret = TRUE;
725
726     if (!wndPtr || wndPtr == WND_OTHER_PROCESS)
727     {
728         SetLastError( ERROR_INVALID_WINDOW_HANDLE );
729         return FALSE;
730     }
731     winpos->hwnd = wndPtr->hwndSelf;  /* make it a full handle */
732
733     /* Finally make sure that all coordinates are valid */
734     if (winpos->x < -32768) winpos->x = -32768;
735     else if (winpos->x > 32767) winpos->x = 32767;
736     if (winpos->y < -32768) winpos->y = -32768;
737     else if (winpos->y > 32767) winpos->y = 32767;
738
739     if (winpos->cx < 0) winpos->cx = 0;
740     else if (winpos->cx > 32767) winpos->cx = 32767;
741     if (winpos->cy < 0) winpos->cy = 0;
742     else if (winpos->cy > 32767) winpos->cy = 32767;
743
744     if (wndPtr->dwStyle & WS_VISIBLE) winpos->flags &= ~SWP_SHOWWINDOW;
745     else
746     {
747         winpos->flags &= ~SWP_HIDEWINDOW;
748         if (!(winpos->flags & SWP_SHOWWINDOW)) winpos->flags |= SWP_NOREDRAW;
749     }
750
751     if ((wndPtr->rectWindow.right - wndPtr->rectWindow.left == winpos->cx) &&
752         (wndPtr->rectWindow.bottom - wndPtr->rectWindow.top == winpos->cy))
753         winpos->flags |= SWP_NOSIZE;    /* Already the right size */
754
755     if ((wndPtr->rectWindow.left == winpos->x) && (wndPtr->rectWindow.top == winpos->y))
756         winpos->flags |= SWP_NOMOVE;    /* Already the right position */
757
758     if (winpos->hwnd == GetForegroundWindow())
759         winpos->flags |= SWP_NOACTIVATE;   /* Already active */
760     else if ((wndPtr->dwStyle & (WS_POPUP | WS_CHILD)) != WS_CHILD)
761     {
762         if (!(winpos->flags & SWP_NOACTIVATE)) /* Bring to the top when activating */
763         {
764             winpos->flags &= ~SWP_NOZORDER;
765             winpos->hwndInsertAfter = HWND_TOP;
766             goto done;
767         }
768     }
769
770     /* Check hwndInsertAfter */
771     if (winpos->flags & SWP_NOZORDER) goto done;
772
773     /* fix sign extension */
774     if (winpos->hwndInsertAfter == (HWND)0xffff) winpos->hwndInsertAfter = HWND_TOPMOST;
775     else if (winpos->hwndInsertAfter == (HWND)0xfffe) winpos->hwndInsertAfter = HWND_NOTOPMOST;
776
777       /* FIXME: TOPMOST not supported yet */
778     if ((winpos->hwndInsertAfter == HWND_TOPMOST) ||
779         (winpos->hwndInsertAfter == HWND_NOTOPMOST)) winpos->hwndInsertAfter = HWND_TOP;
780
781     /* hwndInsertAfter must be a sibling of the window */
782     if ((winpos->hwndInsertAfter != HWND_TOP) && (winpos->hwndInsertAfter != HWND_BOTTOM))
783     {
784         if (GetAncestor( winpos->hwndInsertAfter, GA_PARENT ) != wndPtr->parent) ret = FALSE;
785         else
786         {
787             /* don't need to change the Zorder of hwnd if it's already inserted
788              * after hwndInsertAfter or when inserting hwnd after itself.
789              */
790             if ((winpos->hwnd == winpos->hwndInsertAfter) ||
791                 (winpos->hwnd == GetWindow( winpos->hwndInsertAfter, GW_HWNDNEXT )))
792                 winpos->flags |= SWP_NOZORDER;
793         }
794     }
795  done:
796     WIN_ReleasePtr( wndPtr );
797     return ret;
798 }
799
800
801 /***********************************************************************
802  *              set_visible_style
803  *
804  * Set/clear the WS_VISIBLE style of a window and map/unmap the X window.
805  */
806 static void set_visible_style( HWND hwnd, BOOL set )
807 {
808     WND *win;
809
810     if (!(win = WIN_GetPtr( hwnd ))) return;
811     if (win == WND_OTHER_PROCESS) return;
812
813     TRACE( "hwnd %p (%lx) set %d visible %d empty %d\n",
814            hwnd, get_whole_window(win),
815            set, (win->dwStyle & WS_VISIBLE) != 0, IsRectEmpty(&win->rectWindow) );
816
817     if (set)
818     {
819         if (win->dwStyle & WS_VISIBLE) goto done;
820         WIN_SetStyle( hwnd, win->dwStyle | WS_VISIBLE );
821         if (!IsRectEmpty( &win->rectWindow ) && get_whole_window(win) && is_window_top_level(win))
822         {
823             Display *display = thread_display();
824             X11DRV_sync_window_style( display, win );
825             X11DRV_set_wm_hints( display, win );
826             TRACE( "mapping win %p\n", hwnd );
827             wine_tsx11_lock();
828             XMapWindow( display, get_whole_window(win) );
829             wine_tsx11_unlock();
830         }
831     }
832     else
833     {
834         if (!(win->dwStyle & WS_VISIBLE)) goto done;
835         WIN_SetStyle( hwnd, win->dwStyle & ~WS_VISIBLE );
836         if (!IsRectEmpty( &win->rectWindow ) && get_whole_window(win) && is_window_top_level(win))
837         {
838             TRACE( "unmapping win %p\n", hwnd );
839             wine_tsx11_lock();
840             XUnmapWindow( thread_display(), get_whole_window(win) );
841             wine_tsx11_unlock();
842         }
843     }
844  done:
845     WIN_ReleasePtr( win );
846 }
847
848
849 /***********************************************************************
850  *              SetWindowStyle   (X11DRV.@)
851  *
852  * Update the X state of a window to reflect a style change
853  */
854 void X11DRV_SetWindowStyle( HWND hwnd, LONG oldStyle )
855 {
856     Display *display = thread_display();
857     WND *wndPtr;
858     LONG changed;
859
860     if (hwnd == GetDesktopWindow()) return;
861     if (!(wndPtr = WIN_GetPtr( hwnd ))) return;
862     if (wndPtr == WND_OTHER_PROCESS) return;
863
864     changed = wndPtr->dwStyle ^ oldStyle;
865
866     if (changed & WS_VISIBLE)
867     {
868         if (!IsRectEmpty( &wndPtr->rectWindow ))
869         {
870             if (wndPtr->dwStyle & WS_VISIBLE)
871             {
872                 TRACE( "mapping win %p\n", hwnd );
873                 if (is_window_top_level(wndPtr))
874                 {
875                     X11DRV_sync_window_style( display, wndPtr );
876                     X11DRV_set_wm_hints( display, wndPtr );
877                 }
878                 wine_tsx11_lock();
879                 XMapWindow( display, get_whole_window(wndPtr) );
880                 wine_tsx11_unlock();
881             }
882             else if (!is_window_top_level(wndPtr))  /* don't unmap managed windows */
883             {
884                 TRACE( "unmapping win %p\n", hwnd );
885                 wine_tsx11_lock();
886                 XUnmapWindow( display, get_whole_window(wndPtr) );
887                 wine_tsx11_unlock();
888             }
889         }
890     }
891
892     if (changed & WS_DISABLED)
893     {
894         if (wndPtr->dwExStyle & WS_EX_MANAGED)
895         {
896             XWMHints *wm_hints;
897             wine_tsx11_lock();
898             if (!(wm_hints = XGetWMHints( display, get_whole_window(wndPtr) )))
899                 wm_hints = XAllocWMHints();
900             if (wm_hints)
901             {
902                 wm_hints->flags |= InputHint;
903                 wm_hints->input = !(wndPtr->dwStyle & WS_DISABLED);
904                 XSetWMHints( display, get_whole_window(wndPtr), wm_hints );
905                 XFree(wm_hints);
906             }
907             wine_tsx11_unlock();
908         }
909     }
910     WIN_ReleasePtr(wndPtr);
911 }
912
913
914 /***********************************************************************
915  *              SetWindowPos   (X11DRV.@)
916  */
917 BOOL X11DRV_SetWindowPos( WINDOWPOS *winpos )
918 {
919     WND *wndPtr;
920     RECT newWindowRect, newClientRect;
921     RECT oldWindowRect, oldClientRect;
922     UINT wvrFlags = 0;
923     BOOL bChangePos;
924
925     TRACE( "hwnd %p, after %p, swp %d,%d %dx%d flags %08x\n",
926            winpos->hwnd, winpos->hwndInsertAfter, winpos->x, winpos->y,
927            winpos->cx, winpos->cy, winpos->flags);
928
929     bChangePos = !(winpos->flags & SWP_WINE_NOHOSTMOVE);
930     winpos->flags &= ~SWP_WINE_NOHOSTMOVE;
931
932     /* Check window handle */
933     if (winpos->hwnd == GetDesktopWindow()) return FALSE;
934
935     /* First make sure that coordinates are valid for WM_WINDOWPOSCHANGING */
936     if (!(winpos->flags & SWP_NOMOVE))
937     {
938         if (winpos->x < -32768) winpos->x = -32768;
939         else if (winpos->x > 32767) winpos->x = 32767;
940         if (winpos->y < -32768) winpos->y = -32768;
941         else if (winpos->y > 32767) winpos->y = 32767;
942     }
943     if (!(winpos->flags & SWP_NOSIZE))
944     {
945         if (winpos->cx < 0) winpos->cx = 0;
946         else if (winpos->cx > 32767) winpos->cx = 32767;
947         if (winpos->cy < 0) winpos->cy = 0;
948         else if (winpos->cy > 32767) winpos->cy = 32767;
949     }
950
951     if (!SWP_DoWinPosChanging( winpos, &newWindowRect, &newClientRect )) return FALSE;
952
953     /* Fix redundant flags */
954     if (!fixup_flags( winpos )) return FALSE;
955
956     if (!(wndPtr = WIN_FindWndPtr( winpos->hwnd ))) return FALSE;
957
958     TRACE("\tcurrent (%ld,%ld)-(%ld,%ld), style %08x\n",
959           wndPtr->rectWindow.left, wndPtr->rectWindow.top,
960           wndPtr->rectWindow.right, wndPtr->rectWindow.bottom, (unsigned)wndPtr->dwStyle );
961
962     if((winpos->flags & (SWP_NOZORDER | SWP_HIDEWINDOW | SWP_SHOWWINDOW)) != SWP_NOZORDER)
963     {
964         if (GetAncestor( winpos->hwnd, GA_PARENT ) == GetDesktopWindow())
965             winpos->hwndInsertAfter = SWP_DoOwnedPopups( winpos->hwnd, winpos->hwndInsertAfter );
966     }
967
968     /* Common operations */
969
970     wvrFlags = SWP_DoNCCalcSize( winpos, &newWindowRect, &newClientRect );
971
972     if(!(winpos->flags & SWP_NOZORDER) && winpos->hwnd != winpos->hwndInsertAfter)
973     {
974         HWND parent = GetAncestor( winpos->hwnd, GA_PARENT );
975         if (parent) WIN_LinkWindow( winpos->hwnd, parent, winpos->hwndInsertAfter );
976     }
977
978     /* Reset active DCEs */
979
980     if( (((winpos->flags & SWP_AGG_NOPOSCHANGE) != SWP_AGG_NOPOSCHANGE) &&
981          wndPtr->dwStyle & WS_VISIBLE) ||
982         (winpos->flags & (SWP_HIDEWINDOW | SWP_SHOWWINDOW)) )
983     {
984         RECT rect;
985
986         UnionRect(&rect, &newWindowRect, &wndPtr->rectWindow);
987         DCE_InvalidateDCE(wndPtr->hwndSelf, &rect);
988     }
989
990     oldWindowRect = wndPtr->rectWindow;
991     oldClientRect = wndPtr->rectClient;
992
993     /* Find out if we have to redraw the whole client rect */
994
995     if( oldClientRect.bottom - oldClientRect.top ==
996         newClientRect.bottom - newClientRect.top ) wvrFlags &= ~WVR_VREDRAW;
997
998     if( oldClientRect.right - oldClientRect.left ==
999         newClientRect.right - newClientRect.left ) wvrFlags &= ~WVR_HREDRAW;
1000
1001     /* FIXME: actually do something with WVR_VALIDRECTS */
1002
1003     WIN_SetRectangles( winpos->hwnd, &newWindowRect, &newClientRect );
1004
1005     if (get_whole_window(wndPtr))  /* don't do anything if X window not created yet */
1006     {
1007         Display *display = thread_display();
1008
1009         if (!(winpos->flags & SWP_SHOWWINDOW) && (winpos->flags & SWP_HIDEWINDOW))
1010         {
1011             /* clear the update region */
1012             RedrawWindow( winpos->hwnd, NULL, 0, RDW_VALIDATE | RDW_NOFRAME |
1013                           RDW_NOERASE | RDW_NOINTERNALPAINT | RDW_ALLCHILDREN );
1014             set_visible_style( winpos->hwnd, FALSE );
1015         }
1016         else if ((wndPtr->dwStyle & WS_VISIBLE) &&
1017                  !IsRectEmpty( &oldWindowRect ) && IsRectEmpty( &newWindowRect ))
1018         {
1019             /* resizing to zero size -> unmap */
1020             TRACE( "unmapping zero size win %p\n", winpos->hwnd );
1021             wine_tsx11_lock();
1022             XUnmapWindow( display, get_whole_window(wndPtr) );
1023             wine_tsx11_unlock();
1024         }
1025
1026         wine_tsx11_lock();
1027         if (bChangePos)
1028             X11DRV_sync_whole_window_position( display, wndPtr, !(winpos->flags & SWP_NOZORDER) );
1029         else
1030         {
1031             struct x11drv_win_data *data = wndPtr->pDriverData;
1032             data->whole_rect = wndPtr->rectWindow;
1033             X11DRV_window_to_X_rect( wndPtr, &data->whole_rect );
1034         }
1035
1036         if (X11DRV_sync_client_window_position( display, wndPtr ) ||
1037             (winpos->flags & SWP_FRAMECHANGED))
1038         {
1039             /* if we moved the client area, repaint the whole non-client window */
1040             XClearArea( display, get_whole_window(wndPtr), 0, 0, 0, 0, True );
1041             winpos->flags |= SWP_FRAMECHANGED;
1042         }
1043         if (winpos->flags & SWP_SHOWWINDOW)
1044         {
1045             set_visible_style( winpos->hwnd, TRUE );
1046         }
1047         else if ((wndPtr->dwStyle & WS_VISIBLE) &&
1048                  IsRectEmpty( &oldWindowRect ) && !IsRectEmpty( &newWindowRect ))
1049         {
1050             /* resizing from zero size to non-zero -> map */
1051             TRACE( "mapping non zero size win %p\n", winpos->hwnd );
1052             XMapWindow( display, get_whole_window(wndPtr) );
1053         }
1054         XFlush( display );  /* FIXME: should not be necessary */
1055         wine_tsx11_unlock();
1056     }
1057     else  /* no X window, simply toggle the window style */
1058     {
1059         if (winpos->flags & SWP_SHOWWINDOW)
1060             set_visible_style( winpos->hwnd, TRUE );
1061         else if (winpos->flags & SWP_HIDEWINDOW)
1062             set_visible_style( winpos->hwnd, FALSE );
1063     }
1064
1065     /* manually expose the areas that X won't expose because they are still covered by something */
1066
1067     if (!(winpos->flags & SWP_SHOWWINDOW))
1068         expose_covered_parent_area( wndPtr, &oldWindowRect );
1069
1070     if (wndPtr->dwStyle & WS_VISIBLE)
1071         expose_covered_window_area( wndPtr, &oldClientRect, winpos->flags & SWP_FRAMECHANGED );
1072
1073     WIN_ReleaseWndPtr(wndPtr);
1074
1075     if (wvrFlags & WVR_REDRAW) RedrawWindow( winpos->hwnd, NULL, 0, RDW_INVALIDATE | RDW_ERASE );
1076
1077     if( winpos->flags & SWP_HIDEWINDOW )
1078         HideCaret(winpos->hwnd);
1079     else if (winpos->flags & SWP_SHOWWINDOW)
1080         ShowCaret(winpos->hwnd);
1081
1082     if (!(winpos->flags & SWP_NOACTIVATE))
1083     {
1084         /* child windows get WM_CHILDACTIVATE message */
1085         if ((GetWindowLongW( winpos->hwnd, GWL_STYLE ) & (WS_CHILD | WS_POPUP)) == WS_CHILD)
1086             SendMessageA( winpos->hwnd, WM_CHILDACTIVATE, 0, 0 );
1087         else
1088             SetForegroundWindow( winpos->hwnd );
1089     }
1090
1091       /* And last, send the WM_WINDOWPOSCHANGED message */
1092
1093     TRACE("\tstatus flags = %04x\n", winpos->flags & SWP_AGG_STATUSFLAGS);
1094
1095     if (((winpos->flags & SWP_AGG_STATUSFLAGS) != SWP_AGG_NOPOSCHANGE))
1096         SendMessageA( winpos->hwnd, WM_WINDOWPOSCHANGED, 0, (LPARAM)winpos );
1097         /* WM_WINDOWPOSCHANGED is send even if SWP_NOSENDCHANGING is set */
1098
1099     return TRUE;
1100 }
1101
1102
1103 /***********************************************************************
1104  *           WINPOS_FindIconPos
1105  *
1106  * Find a suitable place for an iconic window.
1107  */
1108 static POINT WINPOS_FindIconPos( WND* wndPtr, POINT pt )
1109 {
1110     RECT rectParent;
1111     HWND *list;
1112     short x, y, xspacing, yspacing;
1113
1114     GetClientRect( wndPtr->parent, &rectParent );
1115     if ((pt.x >= rectParent.left) && (pt.x + GetSystemMetrics(SM_CXICON) < rectParent.right) &&
1116         (pt.y >= rectParent.top) && (pt.y + GetSystemMetrics(SM_CYICON) < rectParent.bottom))
1117         return pt;  /* The icon already has a suitable position */
1118
1119     xspacing = GetSystemMetrics(SM_CXICONSPACING);
1120     yspacing = GetSystemMetrics(SM_CYICONSPACING);
1121
1122     list = WIN_ListChildren( wndPtr->parent );
1123     y = rectParent.bottom;
1124     for (;;)
1125     {
1126         x = rectParent.left;
1127         do
1128         {
1129             /* Check if another icon already occupies this spot */
1130             /* FIXME: this is completely inefficient */
1131             if (list)
1132             {
1133                 int i;
1134                 WND *childPtr;
1135
1136                 for (i = 0; list[i]; i++)
1137                 {
1138                     if (list[i] == wndPtr->hwndSelf) continue;
1139                     if (!IsIconic( list[i] )) continue;
1140                     if (!(childPtr = WIN_FindWndPtr( list[i] ))) continue;
1141                     if ((childPtr->rectWindow.left < x + xspacing) &&
1142                         (childPtr->rectWindow.right >= x) &&
1143                         (childPtr->rectWindow.top <= y) &&
1144                         (childPtr->rectWindow.bottom > y - yspacing))
1145                     {
1146                         WIN_ReleaseWndPtr( childPtr );
1147                         break;  /* There's a window in there */
1148                     }
1149                     WIN_ReleaseWndPtr( childPtr );
1150                 }
1151                 if (list[i])
1152                 {
1153                     /* found something here, try next spot */
1154                     x += xspacing;
1155                     continue;
1156                 }
1157             }
1158
1159             /* No window was found, so it's OK for us */
1160             pt.x = x + (xspacing - GetSystemMetrics(SM_CXICON)) / 2;
1161             pt.y = y - (yspacing + GetSystemMetrics(SM_CYICON)) / 2;
1162             HeapFree( GetProcessHeap(), 0, list );
1163             return pt;
1164
1165         } while(x <= rectParent.right-xspacing);
1166         y -= yspacing;
1167     }
1168 }
1169
1170
1171
1172
1173
1174 UINT WINPOS_MinMaximize( HWND hwnd, UINT cmd, LPRECT rect )
1175 {
1176     WND *wndPtr;
1177     UINT swpFlags = 0;
1178     POINT size;
1179     LONG old_style;
1180     WINDOWPLACEMENT wpl;
1181
1182     TRACE("%p %u\n", hwnd, cmd );
1183
1184     wpl.length = sizeof(wpl);
1185     GetWindowPlacement( hwnd, &wpl );
1186
1187     if (HOOK_CallHooks( WH_CBT, HCBT_MINMAX, (WPARAM)hwnd, cmd, TRUE ))
1188         return SWP_NOSIZE | SWP_NOMOVE;
1189
1190     if (IsIconic( hwnd ))
1191     {
1192         if (cmd == SW_MINIMIZE) return SWP_NOSIZE | SWP_NOMOVE;
1193         if (!SendMessageA( hwnd, WM_QUERYOPEN, 0, 0 )) return SWP_NOSIZE | SWP_NOMOVE;
1194         swpFlags |= SWP_NOCOPYBITS;
1195     }
1196
1197     if (!(wndPtr = WIN_FindWndPtr( hwnd ))) return 0;
1198
1199     size.x = wndPtr->rectWindow.left;
1200     size.y = wndPtr->rectWindow.top;
1201
1202     switch( cmd )
1203     {
1204     case SW_MINIMIZE:
1205         if( wndPtr->dwStyle & WS_MAXIMIZE) wndPtr->flags |= WIN_RESTORE_MAX;
1206         else wndPtr->flags &= ~WIN_RESTORE_MAX;
1207
1208         WIN_SetStyle( hwnd, (wndPtr->dwStyle & ~WS_MAXIMIZE) | WS_MINIMIZE );
1209
1210         X11DRV_set_iconic_state( wndPtr );
1211
1212         wpl.ptMinPosition = WINPOS_FindIconPos( wndPtr, wpl.ptMinPosition );
1213
1214         SetRect( rect, wpl.ptMinPosition.x, wpl.ptMinPosition.y,
1215                  GetSystemMetrics(SM_CXICON), GetSystemMetrics(SM_CYICON) );
1216         swpFlags |= SWP_NOCOPYBITS;
1217         break;
1218
1219     case SW_MAXIMIZE:
1220         WINPOS_GetMinMaxInfo( hwnd, &size, &wpl.ptMaxPosition, NULL, NULL );
1221
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         SetRect( rect, wpl.ptMaxPosition.x, wpl.ptMaxPosition.y, size.x, size.y );
1229         break;
1230
1231     case SW_RESTORE:
1232         old_style = WIN_SetStyle( hwnd, wndPtr->dwStyle & ~(WS_MINIMIZE|WS_MAXIMIZE) );
1233         if (old_style & WS_MINIMIZE)
1234         {
1235             WINPOS_ShowIconTitle( hwnd, FALSE );
1236             X11DRV_set_iconic_state( wndPtr );
1237
1238             if( wndPtr->flags & WIN_RESTORE_MAX)
1239             {
1240                 /* Restore to maximized position */
1241                 WINPOS_GetMinMaxInfo( hwnd, &size, &wpl.ptMaxPosition, NULL, NULL);
1242                 WIN_SetStyle( hwnd, wndPtr->dwStyle | WS_MAXIMIZE );
1243                 SetRect( rect, wpl.ptMaxPosition.x, wpl.ptMaxPosition.y, size.x, size.y );
1244                 break;
1245             }
1246         }
1247         else if (!(old_style & WS_MAXIMIZE)) break;
1248
1249         /* Restore to normal position */
1250
1251         *rect = wpl.rcNormalPosition;
1252         rect->right -= rect->left;
1253         rect->bottom -= rect->top;
1254
1255         break;
1256     }
1257
1258     WIN_ReleaseWndPtr( wndPtr );
1259     return swpFlags;
1260 }
1261
1262
1263 /***********************************************************************
1264  *              ShowWindow   (X11DRV.@)
1265  */
1266 BOOL X11DRV_ShowWindow( HWND hwnd, INT cmd )
1267 {
1268     WND*        wndPtr = WIN_FindWndPtr( hwnd );
1269     BOOL        wasVisible;
1270     RECT        newPos = {0, 0, 0, 0};
1271     UINT        swp = 0;
1272
1273     if (!wndPtr) return FALSE;
1274     hwnd = wndPtr->hwndSelf;  /* make it a full handle */
1275
1276     TRACE("hwnd=%p, cmd=%d\n", hwnd, cmd);
1277
1278     wasVisible = (wndPtr->dwStyle & WS_VISIBLE) != 0;
1279
1280     switch(cmd)
1281     {
1282         case SW_HIDE:
1283             swp |= SWP_HIDEWINDOW | SWP_NOSIZE | SWP_NOMOVE |
1284                         SWP_NOACTIVATE | SWP_NOZORDER;
1285             break;
1286
1287         case SW_SHOWMINNOACTIVE:
1288             swp |= SWP_NOACTIVATE | SWP_NOZORDER;
1289             /* fall through */
1290         case SW_SHOWMINIMIZED:
1291         case SW_FORCEMINIMIZE: /* FIXME: Does not work if thread is hung. */
1292             swp |= SWP_SHOWWINDOW;
1293             /* fall through */
1294         case SW_MINIMIZE:
1295             swp |= SWP_FRAMECHANGED;
1296             if( !(wndPtr->dwStyle & WS_MINIMIZE) )
1297                  swp |= WINPOS_MinMaximize( hwnd, SW_MINIMIZE, &newPos );
1298             else swp |= SWP_NOSIZE | SWP_NOMOVE;
1299             break;
1300
1301         case SW_SHOWMAXIMIZED: /* same as SW_MAXIMIZE */
1302             swp |= SWP_SHOWWINDOW | SWP_FRAMECHANGED;
1303             if( !(wndPtr->dwStyle & WS_MAXIMIZE) )
1304                  swp |= WINPOS_MinMaximize( hwnd, SW_MAXIMIZE, &newPos );
1305             else swp |= SWP_NOSIZE | SWP_NOMOVE;
1306             break;
1307
1308         case SW_SHOWNA:
1309             swp |= SWP_NOACTIVATE | SWP_NOZORDER;
1310             /* fall through */
1311         case SW_SHOW:
1312             swp |= SWP_SHOWWINDOW | SWP_NOSIZE | SWP_NOMOVE;
1313
1314             /*
1315              * ShowWindow has a little peculiar behavior that if the
1316              * window is already the topmost window, it will not
1317              * activate it.
1318              */
1319             if (GetTopWindow(NULL)==hwnd && (wasVisible || GetActiveWindow() == hwnd))
1320               swp |= SWP_NOACTIVATE;
1321
1322             break;
1323
1324         case SW_SHOWNOACTIVATE:
1325             swp |= SWP_NOACTIVATE | SWP_NOZORDER;
1326             /* fall through */
1327         case SW_SHOWNORMAL:  /* same as SW_NORMAL: */
1328         case SW_SHOWDEFAULT: /* FIXME: should have its own handler */
1329         case SW_RESTORE:
1330             swp |= SWP_SHOWWINDOW | SWP_FRAMECHANGED;
1331
1332             if( wndPtr->dwStyle & (WS_MINIMIZE | WS_MAXIMIZE) )
1333                  swp |= WINPOS_MinMaximize( hwnd, SW_RESTORE, &newPos );
1334             else swp |= SWP_NOSIZE | SWP_NOMOVE;
1335             break;
1336     }
1337
1338     /* We can't activate a child window */
1339     if ((wndPtr->dwStyle & WS_CHILD) &&
1340         !(wndPtr->dwExStyle & WS_EX_MDICHILD))
1341         swp |= SWP_NOACTIVATE | SWP_NOZORDER;
1342
1343     SetWindowPos( hwnd, HWND_TOP, newPos.left, newPos.top,
1344                   newPos.right, newPos.bottom, LOWORD(swp) );
1345     if (cmd == SW_HIDE)
1346     {
1347         /* FIXME: This will cause the window to be activated irrespective
1348          * of whether it is owned by the same thread. Has to be done
1349          * asynchronously.
1350          */
1351
1352         if (hwnd == GetActiveWindow())
1353             WINPOS_ActivateOtherWindow(hwnd);
1354
1355         /* Revert focus to parent */
1356         if (hwnd == GetFocus() || IsChild(hwnd, GetFocus()))
1357             SetFocus( GetParent(hwnd) );
1358     }
1359     if (!IsWindow( hwnd )) goto END;
1360     else if( wndPtr->dwStyle & WS_MINIMIZE ) WINPOS_ShowIconTitle( hwnd, TRUE );
1361
1362     if (wndPtr->flags & WIN_NEED_SIZE)
1363     {
1364         /* should happen only in CreateWindowEx() */
1365         int wParam = SIZE_RESTORED;
1366
1367         wndPtr->flags &= ~WIN_NEED_SIZE;
1368         if (wndPtr->dwStyle & WS_MAXIMIZE) wParam = SIZE_MAXIMIZED;
1369         else if (wndPtr->dwStyle & WS_MINIMIZE) wParam = SIZE_MINIMIZED;
1370         SendMessageA( hwnd, WM_SIZE, wParam,
1371                      MAKELONG(wndPtr->rectClient.right-wndPtr->rectClient.left,
1372                             wndPtr->rectClient.bottom-wndPtr->rectClient.top));
1373         SendMessageA( hwnd, WM_MOVE, 0,
1374                    MAKELONG(wndPtr->rectClient.left, wndPtr->rectClient.top) );
1375     }
1376
1377 END:
1378     WIN_ReleaseWndPtr(wndPtr);
1379     return wasVisible;
1380 }
1381
1382
1383 /**********************************************************************
1384  *              X11DRV_MapNotify
1385  */
1386 void X11DRV_MapNotify( HWND hwnd, XMapEvent *event )
1387 {
1388     HWND hwndFocus = GetFocus();
1389     WND *win;
1390
1391     if (!(win = WIN_GetPtr( hwnd ))) return;
1392
1393     if ((win->dwStyle & WS_VISIBLE) &&
1394         (win->dwStyle & WS_MINIMIZE) &&
1395         (win->dwExStyle & WS_EX_MANAGED))
1396     {
1397         int x, y;
1398         unsigned int width, height, border, depth;
1399         Window root, top;
1400         RECT rect;
1401         LONG style = (win->dwStyle & ~(WS_MINIMIZE|WS_MAXIMIZE)) | WS_VISIBLE;
1402
1403         /* FIXME: hack */
1404         wine_tsx11_lock();
1405         XGetGeometry( event->display, get_whole_window(win), &root, &x, &y, &width, &height,
1406                         &border, &depth );
1407         XTranslateCoordinates( event->display, get_whole_window(win), root, 0, 0, &x, &y, &top );
1408         wine_tsx11_unlock();
1409         rect.left   = x;
1410         rect.top    = y;
1411         rect.right  = x + width;
1412         rect.bottom = y + height;
1413         X11DRV_X_to_window_rect( win, &rect );
1414
1415         DCE_InvalidateDCE( hwnd, &win->rectWindow );
1416
1417         if (win->flags & WIN_RESTORE_MAX) style |= WS_MAXIMIZE;
1418         WIN_SetStyle( hwnd, style );
1419         WIN_ReleasePtr( win );
1420
1421         SendMessageA( hwnd, WM_SHOWWINDOW, SW_RESTORE, 0 );
1422         SetWindowPos( hwnd, 0, rect.left, rect.top, rect.right-rect.left, rect.bottom-rect.top,
1423                       SWP_NOZORDER | SWP_WINE_NOHOSTMOVE );
1424     }
1425     else WIN_ReleasePtr( win );
1426     if (hwndFocus && IsChild( hwnd, hwndFocus )) X11DRV_SetFocus(hwndFocus);  /* FIXME */
1427 }
1428
1429
1430 /**********************************************************************
1431  *              X11DRV_UnmapNotify
1432  */
1433 void X11DRV_UnmapNotify( HWND hwnd, XUnmapEvent *event )
1434 {
1435     WND *win;
1436
1437     if (!(win = WIN_GetPtr( hwnd ))) return;
1438
1439     if ((win->dwStyle & WS_VISIBLE) && (win->dwExStyle & WS_EX_MANAGED))
1440     {
1441         if (win->dwStyle & WS_MAXIMIZE)
1442             win->flags |= WIN_RESTORE_MAX;
1443         else
1444             win->flags &= ~WIN_RESTORE_MAX;
1445
1446         WIN_SetStyle( hwnd, (win->dwStyle & ~WS_MAXIMIZE) | WS_MINIMIZE );
1447         WIN_ReleasePtr( win );
1448
1449         EndMenu();
1450         SendMessageA( hwnd, WM_SHOWWINDOW, SW_MINIMIZE, 0 );
1451         SetWindowPos( hwnd, 0, 0, 0, GetSystemMetrics(SM_CXICON), GetSystemMetrics(SM_CYICON),
1452                       SWP_NOMOVE | SWP_NOACTIVATE | SWP_NOZORDER | SWP_WINE_NOHOSTMOVE );
1453     }
1454     else WIN_ReleasePtr( win );
1455 }
1456
1457
1458 /***********************************************************************
1459  *           query_zorder
1460  *
1461  * Synchronize internal z-order with the window manager's.
1462  */
1463 static Window __get_common_ancestor( Display *display, Window A, Window B,
1464                                      Window** children, unsigned* total )
1465 {
1466     /* find the real root window */
1467
1468     Window      root, *childrenB;
1469     unsigned    totalB;
1470
1471     wine_tsx11_lock();
1472     while( A != B && A && B )
1473     {
1474       XQueryTree( display, A, &root, &A, children, total );
1475       XQueryTree( display, B, &root, &B, &childrenB, &totalB );
1476       if( childrenB ) XFree( childrenB );
1477       if( *children ) XFree( *children ), *children = NULL;
1478     }
1479
1480     if( A && B )
1481     {
1482         XQueryTree( display, A, &root, &B, children, total );
1483         wine_tsx11_unlock();
1484         return A;
1485     }
1486     wine_tsx11_unlock();
1487     return 0 ;
1488 }
1489
1490 static Window __get_top_decoration( Display *display, Window w, Window ancestor )
1491 {
1492     Window*     children, root, prev = w, parent = w;
1493     unsigned    total;
1494
1495     wine_tsx11_lock();
1496     do
1497     {
1498         w = parent;
1499         XQueryTree( display, w, &root, &parent, &children, &total );
1500         if( children ) XFree( children );
1501     } while( parent && parent != ancestor );
1502     wine_tsx11_unlock();
1503     TRACE("\t%08x -> %08x\n", (unsigned)prev, (unsigned)w );
1504     return ( parent ) ? w : 0 ;
1505 }
1506
1507 static unsigned __td_lookup( Window w, Window* list, unsigned max )
1508 {
1509     unsigned    i;
1510     for( i = max; i > 0; i-- ) if( list[i - 1] == w ) break;
1511     return i;
1512 }
1513
1514 static HWND query_zorder( Display *display, HWND hWndCheck)
1515 {
1516     HWND      hwndInsertAfter = HWND_TOP;
1517     Window      w, parent, *children = NULL;
1518     unsigned    total, check, pos, best;
1519     HWND *list = WIN_ListChildren( GetDesktopWindow() );
1520     HWND hwndA = 0, hwndB = 0;
1521     int i;
1522
1523     /* find at least two managed windows */
1524     if (!list) return 0;
1525     for (i = 0; list[i]; i++)
1526     {
1527         if (!(GetWindowLongW( list[i], GWL_EXSTYLE ) & WS_EX_MANAGED)) continue;
1528         if (!(GetWindowLongW( list[i], GWL_STYLE ) & WS_VISIBLE)) continue;
1529         if (!hwndA) hwndA = list[i];
1530         else
1531         {
1532             hwndB = list[i];
1533             break;
1534         }
1535     }
1536     if (!hwndA || !hwndB) goto done;
1537
1538     parent = __get_common_ancestor( display, X11DRV_get_whole_window(hwndA),
1539                                     X11DRV_get_whole_window(hwndB), &children, &total );
1540     if( parent && children )
1541     {
1542         /* w is the ancestor if hWndCheck that is a direct descendant of 'parent' */
1543
1544         w = __get_top_decoration( display, X11DRV_get_whole_window(hWndCheck), parent );
1545
1546         if( w != children[total-1] ) /* check if at the top */
1547         {
1548             /* X child at index 0 is at the bottom, at index total-1 is at the top */
1549             check = __td_lookup( w, children, total );
1550             best = total;
1551
1552             /* go through all windows in Wine z-order... */
1553             for (i = 0; list[i]; i++)
1554             {
1555                 if (list[i] == hWndCheck) continue;
1556                 if (!(GetWindowLongW( list[i], GWL_EXSTYLE ) & WS_EX_MANAGED)) continue;
1557                 if (!(w = __get_top_decoration( display, X11DRV_get_whole_window(list[i]),
1558                                                 parent ))) continue;
1559                 pos = __td_lookup( w, children, total );
1560                 if( pos < best && pos > check )
1561                 {
1562                     /* find a nearest Wine window precedes hWndCheck in the real z-order */
1563                     best = pos;
1564                     hwndInsertAfter = list[i];
1565                 }
1566                 if( best - check == 1 ) break;
1567             }
1568         }
1569     }
1570     wine_tsx11_lock();
1571     if( children ) XFree( children );
1572     wine_tsx11_unlock();
1573
1574  done:
1575     HeapFree( GetProcessHeap(), 0, list );
1576     return hwndInsertAfter;
1577 }
1578
1579
1580 /***********************************************************************
1581  *              X11DRV_handle_desktop_resize
1582  */
1583 void X11DRV_handle_desktop_resize( unsigned int width, unsigned int height )
1584 {
1585     RECT rect;
1586     HWND hwnd = GetDesktopWindow();
1587
1588     screen_width  = width;
1589     screen_height = height;
1590     TRACE("desktop %p change to (%dx%d)\n", hwnd, width, height);
1591     SetRect( &rect, 0, 0, width, height );
1592     WIN_SetRectangles( hwnd, &rect, &rect );
1593     SendMessageTimeoutW( HWND_BROADCAST, WM_DISPLAYCHANGE, screen_depth,
1594                          MAKELPARAM( width, height ), SMTO_ABORTIFHUNG, 2000, NULL );
1595 }
1596
1597
1598 /***********************************************************************
1599  *              X11DRV_ConfigureNotify
1600  */
1601 void X11DRV_ConfigureNotify( HWND hwnd, XConfigureEvent *event )
1602 {
1603     HWND oldInsertAfter;
1604     struct x11drv_win_data *data;
1605     WND *win;
1606     RECT rect;
1607     WINDOWPOS winpos;
1608     int x = event->x, y = event->y;
1609
1610     if (!(win = WIN_GetPtr( hwnd ))) return;
1611     data = win->pDriverData;
1612
1613     /* Get geometry */
1614
1615     if (!event->send_event)  /* normal event, need to map coordinates to the root */
1616     {
1617         Window child;
1618         wine_tsx11_lock();
1619         XTranslateCoordinates( event->display, data->whole_window, root_window,
1620                                0, 0, &x, &y, &child );
1621         wine_tsx11_unlock();
1622     }
1623     rect.left   = x;
1624     rect.top    = y;
1625     rect.right  = x + event->width;
1626     rect.bottom = y + event->height;
1627     TRACE( "win %p new X rect %ld,%ld,%ldx%ld (event %d,%d,%dx%d)\n",
1628            hwnd, rect.left, rect.top, rect.right-rect.left, rect.bottom-rect.top,
1629            event->x, event->y, event->width, event->height );
1630     X11DRV_X_to_window_rect( win, &rect );
1631     WIN_ReleasePtr( win );
1632
1633     winpos.hwnd  = hwnd;
1634     winpos.x     = rect.left;
1635     winpos.y     = rect.top;
1636     winpos.cx    = rect.right - rect.left;
1637     winpos.cy    = rect.bottom - rect.top;
1638     winpos.flags = SWP_NOACTIVATE;
1639
1640     /* Get Z-order (FIXME) */
1641
1642     winpos.hwndInsertAfter = query_zorder( event->display, hwnd );
1643
1644     /* needs to find the first Visible Window above the current one */
1645     oldInsertAfter = hwnd;
1646     for (;;)
1647     {
1648         oldInsertAfter = GetWindow( oldInsertAfter, GW_HWNDPREV );
1649         if (!oldInsertAfter)
1650         {
1651             oldInsertAfter = HWND_TOP;
1652             break;
1653         }
1654         if (GetWindowLongA( oldInsertAfter, GWL_STYLE ) & WS_VISIBLE) break;
1655     }
1656
1657     /* Compare what has changed */
1658
1659     GetWindowRect( hwnd, &rect );
1660     if (rect.left == winpos.x && rect.top == winpos.y) winpos.flags |= SWP_NOMOVE;
1661     else
1662         TRACE( "%p moving from (%ld,%ld) to (%d,%d)\n",
1663                hwnd, rect.left, rect.top, winpos.x, winpos.y );
1664
1665     if ((rect.right - rect.left == winpos.cx && rect.bottom - rect.top == winpos.cy) ||
1666         IsIconic(hwnd) ||
1667         (IsRectEmpty( &rect ) && winpos.cx == 1 && winpos.cy == 1))
1668         winpos.flags |= SWP_NOSIZE;
1669     else
1670         TRACE( "%p resizing from (%ldx%ld) to (%dx%d)\n",
1671                hwnd, rect.right - rect.left, rect.bottom - rect.top,
1672                winpos.cx, winpos.cy );
1673
1674     if (winpos.hwndInsertAfter == oldInsertAfter) winpos.flags |= SWP_NOZORDER;
1675     else
1676         TRACE( "%p restacking from after %p to after %p\n",
1677                hwnd, oldInsertAfter, winpos.hwndInsertAfter );
1678
1679     /* if nothing changed, don't do anything */
1680     if (winpos.flags == (SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE)) return;
1681
1682     SetWindowPos( hwnd, winpos.hwndInsertAfter, winpos.x, winpos.y,
1683                   winpos.cx, winpos.cy, winpos.flags | SWP_WINE_NOHOSTMOVE );
1684 }
1685
1686
1687 /***********************************************************************
1688  *              SetWindowRgn  (X11DRV.@)
1689  *
1690  * Assign specified region to window (for non-rectangular windows)
1691  */
1692 int X11DRV_SetWindowRgn( HWND hwnd, HRGN hrgn, BOOL redraw )
1693 {
1694     WND *wndPtr;
1695
1696     if ((wndPtr = WIN_GetPtr( hwnd )) == WND_OTHER_PROCESS)
1697     {
1698         if (IsWindow( hwnd ))
1699             FIXME( "not supported on other process window %p\n", hwnd );
1700         wndPtr = NULL;
1701     }
1702     if (!wndPtr)
1703     {
1704         SetLastError( ERROR_INVALID_WINDOW_HANDLE );
1705         return FALSE;
1706     }
1707
1708     if (wndPtr->hrgnWnd == hrgn)
1709     {
1710         WIN_ReleasePtr( wndPtr );
1711         return TRUE;
1712     }
1713
1714     if (wndPtr->hrgnWnd)
1715     {
1716         /* delete previous region */
1717         DeleteObject(wndPtr->hrgnWnd);
1718         wndPtr->hrgnWnd = 0;
1719     }
1720     wndPtr->hrgnWnd = hrgn;
1721
1722 #ifdef HAVE_LIBXSHAPE
1723     {
1724         Display *display = thread_display();
1725         X11DRV_WND_DATA *data = wndPtr->pDriverData;
1726
1727         if (data->whole_window)
1728         {
1729             if (!hrgn)
1730             {
1731                 wine_tsx11_lock();
1732                 XShapeCombineMask( display, data->whole_window,
1733                                    ShapeBounding, 0, 0, None, ShapeSet );
1734                 wine_tsx11_unlock();
1735             }
1736             else
1737             {
1738                 XRectangle *aXRect;
1739                 int x_offset, y_offset;
1740                 DWORD size;
1741                 DWORD dwBufferSize = GetRegionData(hrgn, 0, NULL);
1742                 PRGNDATA pRegionData = HeapAlloc(GetProcessHeap(), 0, dwBufferSize);
1743                 if (!pRegionData)
1744                 {
1745                     WIN_ReleasePtr( wndPtr );
1746                     return TRUE;
1747                 }
1748                 GetRegionData(hrgn, dwBufferSize, pRegionData);
1749                 size = pRegionData->rdh.nCount;
1750                 x_offset = wndPtr->rectWindow.left - data->whole_rect.left;
1751                 y_offset = wndPtr->rectWindow.top - data->whole_rect.top;
1752                 /* convert region's "Windows rectangles" to XRectangles */
1753                 aXRect = HeapAlloc(GetProcessHeap(), 0, size * sizeof(*aXRect) );
1754                 if (aXRect)
1755                 {
1756                     XRectangle* pCurrRect = aXRect;
1757                     RECT *pRect = (RECT*) pRegionData->Buffer;
1758                     for (; pRect < ((RECT*) pRegionData->Buffer) + size ; ++pRect, ++pCurrRect)
1759                     {
1760                         pCurrRect->x      = pRect->left + x_offset;
1761                         pCurrRect->y      = pRect->top + y_offset;
1762                         pCurrRect->height = pRect->bottom - pRect->top;
1763                         pCurrRect->width  = pRect->right  - pRect->left;
1764
1765                         TRACE("Rectangle %04d of %04ld data: X=%04d, Y=%04d, Height=%04d, Width=%04d.\n",
1766                               pRect - (RECT*) pRegionData->Buffer,
1767                               size,
1768                               pCurrRect->x,
1769                               pCurrRect->y,
1770                               pCurrRect->height,
1771                               pCurrRect->width);
1772                     }
1773
1774                     /* shape = non-rectangular windows (X11/extensions) */
1775                     wine_tsx11_lock();
1776                     XShapeCombineRectangles( display, data->whole_window, ShapeBounding,
1777                                              0, 0, aXRect,
1778                                              pCurrRect - aXRect, ShapeSet, YXBanded );
1779                     wine_tsx11_unlock();
1780                     HeapFree(GetProcessHeap(), 0, aXRect );
1781                 }
1782                 HeapFree(GetProcessHeap(), 0, pRegionData);
1783             }
1784         }
1785     }
1786 #endif  /* HAVE_LIBXSHAPE */
1787
1788     WIN_ReleasePtr( wndPtr );
1789     if (redraw) RedrawWindow( hwnd, NULL, 0, RDW_FRAME | RDW_INVALIDATE | RDW_ERASE );
1790     return TRUE;
1791 }
1792
1793
1794 /***********************************************************************
1795  *           draw_moving_frame
1796  *
1797  * Draw the frame used when moving or resizing window.
1798  *
1799  * FIXME:  This causes problems in Win95 mode.  (why?)
1800  */
1801 static void draw_moving_frame( HDC hdc, RECT *rect, BOOL thickframe )
1802 {
1803     if (thickframe)
1804     {
1805         const int width = GetSystemMetrics(SM_CXFRAME);
1806         const int height = GetSystemMetrics(SM_CYFRAME);
1807
1808         HBRUSH hbrush = SelectObject( hdc, GetStockObject( GRAY_BRUSH ) );
1809         PatBlt( hdc, rect->left, rect->top,
1810                 rect->right - rect->left - width, height, PATINVERT );
1811         PatBlt( hdc, rect->left, rect->top + height, width,
1812                 rect->bottom - rect->top - height, PATINVERT );
1813         PatBlt( hdc, rect->left + width, rect->bottom - 1,
1814                 rect->right - rect->left - width, -height, PATINVERT );
1815         PatBlt( hdc, rect->right - 1, rect->top, -width,
1816                 rect->bottom - rect->top - height, PATINVERT );
1817         SelectObject( hdc, hbrush );
1818     }
1819     else DrawFocusRect( hdc, rect );
1820 }
1821
1822
1823 /***********************************************************************
1824  *           start_size_move
1825  *
1826  * Initialisation of a move or resize, when initiatied from a menu choice.
1827  * Return hit test code for caption or sizing border.
1828  */
1829 static LONG start_size_move( HWND hwnd, WPARAM wParam, POINT *capturePoint, LONG style )
1830 {
1831     LONG hittest = 0;
1832     POINT pt;
1833     MSG msg;
1834     RECT rectWindow;
1835
1836     GetWindowRect( hwnd, &rectWindow );
1837
1838     if ((wParam & 0xfff0) == SC_MOVE)
1839     {
1840         /* Move pointer at the center of the caption */
1841         RECT rect;
1842         NC_GetInsideRect( hwnd, &rect );
1843         if (style & WS_SYSMENU)
1844             rect.left += GetSystemMetrics(SM_CXSIZE) + 1;
1845         if (style & WS_MINIMIZEBOX)
1846             rect.right -= GetSystemMetrics(SM_CXSIZE) + 1;
1847         if (style & WS_MAXIMIZEBOX)
1848             rect.right -= GetSystemMetrics(SM_CXSIZE) + 1;
1849         pt.x = rectWindow.left + (rect.right - rect.left) / 2;
1850         pt.y = rectWindow.top + rect.top + GetSystemMetrics(SM_CYSIZE)/2;
1851         hittest = HTCAPTION;
1852         *capturePoint = pt;
1853     }
1854     else  /* SC_SIZE */
1855     {
1856         while(!hittest)
1857         {
1858             GetMessageW( &msg, 0, WM_KEYFIRST, WM_MOUSELAST );
1859             if (CallMsgFilterW( &msg, MSGF_SIZE )) continue;
1860
1861             switch(msg.message)
1862             {
1863             case WM_MOUSEMOVE:
1864                 hittest = NC_HandleNCHitTest( hwnd, msg.pt );
1865                 if ((hittest < HTLEFT) || (hittest > HTBOTTOMRIGHT))
1866                     hittest = 0;
1867                 break;
1868
1869             case WM_LBUTTONUP:
1870                 return 0;
1871
1872             case WM_KEYDOWN:
1873                 switch(msg.wParam)
1874                 {
1875                 case VK_UP:
1876                     hittest = HTTOP;
1877                     pt.x =(rectWindow.left+rectWindow.right)/2;
1878                     pt.y = rectWindow.top + GetSystemMetrics(SM_CYFRAME) / 2;
1879                     break;
1880                 case VK_DOWN:
1881                     hittest = HTBOTTOM;
1882                     pt.x =(rectWindow.left+rectWindow.right)/2;
1883                     pt.y = rectWindow.bottom - GetSystemMetrics(SM_CYFRAME) / 2;
1884                     break;
1885                 case VK_LEFT:
1886                     hittest = HTLEFT;
1887                     pt.x = rectWindow.left + GetSystemMetrics(SM_CXFRAME) / 2;
1888                     pt.y =(rectWindow.top+rectWindow.bottom)/2;
1889                     break;
1890                 case VK_RIGHT:
1891                     hittest = HTRIGHT;
1892                     pt.x = rectWindow.right - GetSystemMetrics(SM_CXFRAME) / 2;
1893                     pt.y =(rectWindow.top+rectWindow.bottom)/2;
1894                     break;
1895                 case VK_RETURN:
1896                 case VK_ESCAPE: return 0;
1897                 }
1898             }
1899         }
1900         *capturePoint = pt;
1901     }
1902     SetCursorPos( pt.x, pt.y );
1903     NC_HandleSetCursor( hwnd, (WPARAM)hwnd, MAKELONG( hittest, WM_MOUSEMOVE ));
1904     return hittest;
1905 }
1906
1907
1908 /***********************************************************************
1909  *           set_movesize_capture
1910  */
1911 static void set_movesize_capture( HWND hwnd )
1912 {
1913     HWND previous = 0;
1914
1915     SERVER_START_REQ( set_capture_window )
1916     {
1917         req->handle = hwnd;
1918         req->flags  = CAPTURE_MOVESIZE;
1919         if (!wine_server_call_err( req ))
1920         {
1921             previous = reply->previous;
1922             hwnd = reply->full_handle;
1923         }
1924     }
1925     SERVER_END_REQ;
1926     if (previous && previous != hwnd)
1927         SendMessageW( previous, WM_CAPTURECHANGED, 0, (LPARAM)hwnd );
1928 }
1929
1930
1931 /***********************************************************************
1932  *           SysCommandSizeMove   (X11DRV.@)
1933  *
1934  * Perform SC_MOVE and SC_SIZE commands.
1935  */
1936 void X11DRV_SysCommandSizeMove( HWND hwnd, WPARAM wParam )
1937 {
1938     MSG msg;
1939     RECT sizingRect, mouseRect, origRect;
1940     HDC hdc;
1941     HWND parent;
1942     LONG hittest = (LONG)(wParam & 0x0f);
1943     HCURSOR hDragCursor = 0, hOldCursor = 0;
1944     POINT minTrack, maxTrack;
1945     POINT capturePoint, pt;
1946     LONG style = GetWindowLongA( hwnd, GWL_STYLE );
1947     LONG exstyle = GetWindowLongA( hwnd, GWL_EXSTYLE );
1948     BOOL    thickframe = HAS_THICKFRAME( style, exstyle );
1949     BOOL    iconic = style & WS_MINIMIZE;
1950     BOOL    moved = FALSE;
1951     DWORD     dwPoint = GetMessagePos ();
1952     BOOL DragFullWindows = FALSE;
1953     BOOL grab;
1954     int iWndsLocks;
1955     Display *old_gdi_display = NULL;
1956     Display *display = thread_display();
1957
1958     SystemParametersInfoA(SPI_GETDRAGFULLWINDOWS, 0, &DragFullWindows, 0);
1959
1960     pt.x = (short)LOWORD(dwPoint);
1961     pt.y = (short)HIWORD(dwPoint);
1962     capturePoint = pt;
1963
1964     if (IsZoomed(hwnd) || !IsWindowVisible(hwnd) || (exstyle & WS_EX_MANAGED)) return;
1965
1966     if ((wParam & 0xfff0) == SC_MOVE)
1967     {
1968         if (!hittest) hittest = start_size_move( hwnd, wParam, &capturePoint, style );
1969         if (!hittest) return;
1970     }
1971     else  /* SC_SIZE */
1972     {
1973         if (!thickframe) return;
1974         if ( hittest && ((wParam & 0xfff0) != SC_MOUSEMENU) )
1975             hittest += (HTLEFT - WMSZ_LEFT);
1976         else
1977         {
1978             set_movesize_capture( hwnd );
1979             hittest = start_size_move( hwnd, wParam, &capturePoint, style );
1980             if (!hittest)
1981             {
1982                 set_movesize_capture(0);
1983                 return;
1984             }
1985         }
1986     }
1987
1988       /* Get min/max info */
1989
1990     WINPOS_GetMinMaxInfo( hwnd, NULL, NULL, &minTrack, &maxTrack );
1991     GetWindowRect( hwnd, &sizingRect );
1992     if (style & WS_CHILD)
1993     {
1994         parent = GetParent(hwnd);
1995         /* make sizing rect relative to parent */
1996         MapWindowPoints( 0, parent, (POINT*)&sizingRect, 2 );
1997         GetClientRect( parent, &mouseRect );
1998     }
1999     else
2000     {
2001         parent = 0;
2002         SetRect(&mouseRect, 0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN));
2003     }
2004     origRect = sizingRect;
2005
2006     if (ON_LEFT_BORDER(hittest))
2007     {
2008         mouseRect.left  = max( mouseRect.left, sizingRect.right-maxTrack.x );
2009         mouseRect.right = min( mouseRect.right, sizingRect.right-minTrack.x );
2010     }
2011     else if (ON_RIGHT_BORDER(hittest))
2012     {
2013         mouseRect.left  = max( mouseRect.left, sizingRect.left+minTrack.x );
2014         mouseRect.right = min( mouseRect.right, sizingRect.left+maxTrack.x );
2015     }
2016     if (ON_TOP_BORDER(hittest))
2017     {
2018         mouseRect.top    = max( mouseRect.top, sizingRect.bottom-maxTrack.y );
2019         mouseRect.bottom = min( mouseRect.bottom,sizingRect.bottom-minTrack.y);
2020     }
2021     else if (ON_BOTTOM_BORDER(hittest))
2022     {
2023         mouseRect.top    = max( mouseRect.top, sizingRect.top+minTrack.y );
2024         mouseRect.bottom = min( mouseRect.bottom, sizingRect.top+maxTrack.y );
2025     }
2026     if (parent) MapWindowPoints( parent, 0, (LPPOINT)&mouseRect, 2 );
2027
2028     /* Retrieve a default cache DC (without using the window style) */
2029     hdc = GetDCEx( parent, 0, DCX_CACHE );
2030
2031     if( iconic ) /* create a cursor for dragging */
2032     {
2033         hDragCursor = (HCURSOR)GetClassLongA( hwnd, GCL_HICON);
2034         if( !hDragCursor ) hDragCursor = (HCURSOR)SendMessageA( hwnd, WM_QUERYDRAGICON, 0, 0L);
2035         if( !hDragCursor ) iconic = FALSE;
2036     }
2037
2038     /* repaint the window before moving it around */
2039     RedrawWindow( hwnd, NULL, 0, RDW_UPDATENOW | RDW_ALLCHILDREN );
2040
2041     SendMessageA( hwnd, WM_ENTERSIZEMOVE, 0, 0 );
2042     set_movesize_capture( hwnd );
2043
2044     /* grab the server only when moving top-level windows without desktop */
2045     grab = (!DragFullWindows && !parent && (root_window == DefaultRootWindow(gdi_display)));
2046
2047     wine_tsx11_lock();
2048     if (grab)
2049     {
2050         XSync( gdi_display, False );
2051         XGrabServer( display );
2052         XSync( display, False );
2053         /* switch gdi display to the thread display, since the server is grabbed */
2054         old_gdi_display = gdi_display;
2055         gdi_display = display;
2056     }
2057     XGrabPointer( display, X11DRV_get_whole_window(hwnd), False,
2058                   PointerMotionMask | ButtonPressMask | ButtonReleaseMask,
2059                   GrabModeAsync, GrabModeAsync,
2060                   parent ? X11DRV_get_client_window(parent) : root_window,
2061                   None, CurrentTime );
2062     wine_tsx11_unlock();
2063
2064     while(1)
2065     {
2066         int dx = 0, dy = 0;
2067
2068         if (!GetMessageW( &msg, 0, WM_KEYFIRST, WM_MOUSELAST )) break;
2069         if (CallMsgFilterW( &msg, MSGF_SIZE )) continue;
2070
2071         /* Exit on button-up, Return, or Esc */
2072         if ((msg.message == WM_LBUTTONUP) ||
2073             ((msg.message == WM_KEYDOWN) &&
2074              ((msg.wParam == VK_RETURN) || (msg.wParam == VK_ESCAPE)))) break;
2075
2076         if ((msg.message != WM_KEYDOWN) && (msg.message != WM_MOUSEMOVE))
2077             continue;  /* We are not interested in other messages */
2078
2079         pt = msg.pt;
2080
2081         if (msg.message == WM_KEYDOWN) switch(msg.wParam)
2082         {
2083         case VK_UP:    pt.y -= 8; break;
2084         case VK_DOWN:  pt.y += 8; break;
2085         case VK_LEFT:  pt.x -= 8; break;
2086         case VK_RIGHT: pt.x += 8; break;
2087         }
2088
2089         pt.x = max( pt.x, mouseRect.left );
2090         pt.x = min( pt.x, mouseRect.right );
2091         pt.y = max( pt.y, mouseRect.top );
2092         pt.y = min( pt.y, mouseRect.bottom );
2093
2094         dx = pt.x - capturePoint.x;
2095         dy = pt.y - capturePoint.y;
2096
2097         if (dx || dy)
2098         {
2099             if( !moved )
2100             {
2101                 moved = TRUE;
2102
2103                 if( iconic ) /* ok, no system popup tracking */
2104                 {
2105                     hOldCursor = SetCursor(hDragCursor);
2106                     ShowCursor( TRUE );
2107                     WINPOS_ShowIconTitle( hwnd, FALSE );
2108                 }
2109                 else if(!DragFullWindows)
2110                     draw_moving_frame( hdc, &sizingRect, thickframe );
2111             }
2112
2113             if (msg.message == WM_KEYDOWN) SetCursorPos( pt.x, pt.y );
2114             else
2115             {
2116                 RECT newRect = sizingRect;
2117                 WPARAM wpSizingHit = 0;
2118
2119                 if (hittest == HTCAPTION) OffsetRect( &newRect, dx, dy );
2120                 if (ON_LEFT_BORDER(hittest)) newRect.left += dx;
2121                 else if (ON_RIGHT_BORDER(hittest)) newRect.right += dx;
2122                 if (ON_TOP_BORDER(hittest)) newRect.top += dy;
2123                 else if (ON_BOTTOM_BORDER(hittest)) newRect.bottom += dy;
2124                 if(!iconic && !DragFullWindows) draw_moving_frame( hdc, &sizingRect, thickframe );
2125                 capturePoint = pt;
2126
2127                 /* determine the hit location */
2128                 if (hittest >= HTLEFT && hittest <= HTBOTTOMRIGHT)
2129                     wpSizingHit = WMSZ_LEFT + (hittest - HTLEFT);
2130                 SendMessageA( hwnd, WM_SIZING, wpSizingHit, (LPARAM)&newRect );
2131
2132                 if (!iconic)
2133                 {
2134                     if(!DragFullWindows)
2135                         draw_moving_frame( hdc, &newRect, thickframe );
2136                     else {
2137                         /* To avoid any deadlocks, all the locks on the windows
2138                            structures must be suspended before the SetWindowPos */
2139                         iWndsLocks = WIN_SuspendWndsLock();
2140                         SetWindowPos( hwnd, 0, newRect.left, newRect.top,
2141                                       newRect.right - newRect.left,
2142                                       newRect.bottom - newRect.top,
2143                                       ( hittest == HTCAPTION ) ? SWP_NOSIZE : 0 );
2144                         WIN_RestoreWndsLock(iWndsLocks);
2145                     }
2146                 }
2147                 sizingRect = newRect;
2148             }
2149         }
2150     }
2151
2152     set_movesize_capture(0);
2153     if( iconic )
2154     {
2155         if( moved ) /* restore cursors, show icon title later on */
2156         {
2157             ShowCursor( FALSE );
2158             SetCursor( hOldCursor );
2159         }
2160     }
2161     else if (moved && !DragFullWindows)
2162         draw_moving_frame( hdc, &sizingRect, thickframe );
2163
2164     ReleaseDC( parent, hdc );
2165
2166     wine_tsx11_lock();
2167     XUngrabPointer( display, CurrentTime );
2168     if (grab)
2169     {
2170         XSync( display, False );
2171         XUngrabServer( display );
2172         XSync( display, False );
2173         gdi_display = old_gdi_display;
2174     }
2175     wine_tsx11_unlock();
2176
2177     if (HOOK_CallHooks( WH_CBT, HCBT_MOVESIZE, (WPARAM)hwnd, (LPARAM)&sizingRect, TRUE ))
2178         moved = FALSE;
2179
2180     SendMessageA( hwnd, WM_EXITSIZEMOVE, 0, 0 );
2181     SendMessageA( hwnd, WM_SETVISIBLE, !IsIconic(hwnd), 0L);
2182
2183     /* window moved or resized */
2184     if (moved)
2185     {
2186         /* To avoid any deadlocks, all the locks on the windows
2187            structures must be suspended before the SetWindowPos */
2188         iWndsLocks = WIN_SuspendWndsLock();
2189
2190         /* if the moving/resizing isn't canceled call SetWindowPos
2191          * with the new position or the new size of the window
2192          */
2193         if (!((msg.message == WM_KEYDOWN) && (msg.wParam == VK_ESCAPE)) )
2194         {
2195             /* NOTE: SWP_NOACTIVATE prevents document window activation in Word 6 */
2196             if(!DragFullWindows)
2197                 SetWindowPos( hwnd, 0, sizingRect.left, sizingRect.top,
2198                               sizingRect.right - sizingRect.left,
2199                               sizingRect.bottom - sizingRect.top,
2200                               ( hittest == HTCAPTION ) ? SWP_NOSIZE : 0 );
2201         }
2202         else
2203         { /* restore previous size/position */
2204             if(DragFullWindows)
2205                 SetWindowPos( hwnd, 0, origRect.left, origRect.top,
2206                               origRect.right - origRect.left,
2207                               origRect.bottom - origRect.top,
2208                               ( hittest == HTCAPTION ) ? SWP_NOSIZE : 0 );
2209         }
2210
2211         WIN_RestoreWndsLock(iWndsLocks);
2212     }
2213
2214     if (IsIconic(hwnd))
2215     {
2216         /* Single click brings up the system menu when iconized */
2217
2218         if( !moved )
2219         {
2220             if(style & WS_SYSMENU )
2221                 SendMessageA( hwnd, WM_SYSCOMMAND,
2222                               SC_MOUSEMENU + HTSYSMENU, MAKELONG(pt.x,pt.y));
2223         }
2224         else WINPOS_ShowIconTitle( hwnd, TRUE );
2225     }
2226 }
2227
2228
2229 /***********************************************************************
2230  *              ForceWindowRaise   (X11DRV.@)
2231  *
2232  * Raise a window on top of the X stacking order, while preserving
2233  * the correct Windows Z order.
2234  *
2235  * FIXME: this should go away.
2236  */
2237 void X11DRV_ForceWindowRaise( HWND hwnd )
2238 {
2239     int i = 0;
2240     HWND *list;
2241     XWindowChanges winChanges;
2242     Display *display = thread_display();
2243     WND *wndPtr = WIN_FindWndPtr( hwnd );
2244
2245     if (!wndPtr) return;
2246
2247     if ((wndPtr->dwExStyle & WS_EX_MANAGED) ||
2248         wndPtr->parent != GetDesktopWindow() ||
2249         IsRectEmpty( &wndPtr->rectWindow ) ||
2250         !get_whole_window(wndPtr))
2251     {
2252         WIN_ReleaseWndPtr( wndPtr );
2253         return;
2254     }
2255     WIN_ReleaseWndPtr( wndPtr );
2256
2257     /* Raise all windows up to wndPtr according to their Z order.
2258      * (it would be easier with sibling-related Below but it doesn't
2259      * work very well with SGI mwm for instance)
2260      */
2261     winChanges.stack_mode = Above;
2262     if (!(list = WIN_ListChildren( GetDesktopWindow() ))) return;
2263     while (list[i] && list[i] != hwnd) i++;
2264     if (list[i])
2265     {
2266         for ( ; i >= 0; i--)
2267         {
2268             WND *ptr = WIN_FindWndPtr( list[i] );
2269             if (!ptr) continue;
2270             if (!IsRectEmpty( &ptr->rectWindow ) && get_whole_window(ptr))
2271             {
2272                 wine_tsx11_lock();
2273                 XReconfigureWMWindow( display, get_whole_window(ptr), 0, CWStackMode, &winChanges );
2274                 wine_tsx11_unlock();
2275             }
2276             WIN_ReleaseWndPtr( ptr );
2277         }
2278     }
2279     HeapFree( GetProcessHeap(), 0, list );
2280 }