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