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