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