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