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