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