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