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