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