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