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