winex11.drv: Cast-qual warnings fix.
[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 | SWP_FRAMECHANGED );
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 struct desktop_resize_data
1228 {
1229     RECT  old_screen_rect;
1230     RECT  old_virtual_rect;
1231 };
1232
1233 static BOOL CALLBACK update_windows_on_desktop_resize( HWND hwnd, LPARAM lparam )
1234 {
1235     struct x11drv_win_data *data;
1236     Display *display = thread_display();
1237     struct desktop_resize_data *resize_data = (struct desktop_resize_data *)lparam;
1238     int mask = 0;
1239
1240     if (!(data = X11DRV_get_win_data( hwnd ))) return TRUE;
1241
1242     if (GetWindowLongW( hwnd, GWL_STYLE ) & WS_VISIBLE)
1243         update_fullscreen_state( display, data, &data->client_rect, &resize_data->old_screen_rect );
1244
1245     if (resize_data->old_virtual_rect.left != virtual_screen_rect.left) mask |= CWX;
1246     if (resize_data->old_virtual_rect.top != virtual_screen_rect.top) mask |= CWY;
1247     if (mask && data->whole_window)
1248     {
1249         XWindowChanges changes;
1250
1251         wine_tsx11_lock();
1252         changes.x = data->whole_rect.left - virtual_screen_rect.left;
1253         changes.y = data->whole_rect.top - virtual_screen_rect.top;
1254         XReconfigureWMWindow( display, data->whole_window,
1255                               DefaultScreen(display), mask, &changes );
1256         wine_tsx11_unlock();
1257     }
1258     return TRUE;
1259 }
1260
1261
1262 /***********************************************************************
1263  *              X11DRV_handle_desktop_resize
1264  */
1265 void X11DRV_handle_desktop_resize( unsigned int width, unsigned int height )
1266 {
1267     HWND hwnd = GetDesktopWindow();
1268     struct x11drv_win_data *data;
1269     struct desktop_resize_data resize_data;
1270
1271     if (!(data = X11DRV_get_win_data( hwnd ))) return;
1272
1273     SetRect( &resize_data.old_screen_rect, 0, 0, screen_width, screen_height );
1274     resize_data.old_virtual_rect = virtual_screen_rect;
1275
1276     screen_width  = width;
1277     screen_height = height;
1278     xinerama_init();
1279     TRACE("desktop %p change to (%dx%d)\n", hwnd, width, height);
1280     data->lock_changes++;
1281     X11DRV_set_window_pos( hwnd, 0, &virtual_screen_rect, &virtual_screen_rect,
1282                            SWP_NOZORDER|SWP_NOMOVE, NULL );
1283     data->lock_changes--;
1284     SendMessageTimeoutW( HWND_BROADCAST, WM_DISPLAYCHANGE, screen_depth,
1285                          MAKELPARAM( width, height ), SMTO_ABORTIFHUNG, 2000, NULL );
1286
1287     EnumWindows( update_windows_on_desktop_resize, (LPARAM)&resize_data );
1288 }
1289
1290
1291 /***********************************************************************
1292  *              X11DRV_ConfigureNotify
1293  */
1294 void X11DRV_ConfigureNotify( HWND hwnd, XEvent *xev )
1295 {
1296     XConfigureEvent *event = &xev->xconfigure;
1297     struct x11drv_win_data *data;
1298     RECT rect;
1299     UINT flags;
1300     int cx, cy, x = event->x, y = event->y;
1301
1302     if (!hwnd) return;
1303     if (!(data = X11DRV_get_win_data( hwnd ))) return;
1304
1305     /* Get geometry */
1306
1307     if (!event->send_event)  /* normal event, need to map coordinates to the root */
1308     {
1309         Window child;
1310         wine_tsx11_lock();
1311         XTranslateCoordinates( event->display, data->whole_window, root_window,
1312                                0, 0, &x, &y, &child );
1313         wine_tsx11_unlock();
1314     }
1315     rect.left   = x;
1316     rect.top    = y;
1317     rect.right  = x + event->width;
1318     rect.bottom = y + event->height;
1319     OffsetRect( &rect, virtual_screen_rect.left, virtual_screen_rect.top );
1320     TRACE( "win %p new X rect %d,%d,%dx%d (event %d,%d,%dx%d)\n",
1321            hwnd, rect.left, rect.top, rect.right-rect.left, rect.bottom-rect.top,
1322            event->x, event->y, event->width, event->height );
1323     X11DRV_X_to_window_rect( data, &rect );
1324
1325     x     = rect.left;
1326     y     = rect.top;
1327     cx    = rect.right - rect.left;
1328     cy    = rect.bottom - rect.top;
1329     flags = SWP_NOACTIVATE | SWP_NOZORDER;
1330
1331     /* Compare what has changed */
1332
1333     GetWindowRect( hwnd, &rect );
1334     if (rect.left == x && rect.top == y) flags |= SWP_NOMOVE;
1335     else
1336         TRACE( "%p moving from (%d,%d) to (%d,%d)\n",
1337                hwnd, rect.left, rect.top, x, y );
1338
1339     if ((rect.right - rect.left == cx && rect.bottom - rect.top == cy) ||
1340         IsIconic(hwnd) ||
1341         (IsRectEmpty( &rect ) && event->width == 1 && event->height == 1))
1342     {
1343         if (flags & SWP_NOMOVE) return;  /* if nothing changed, don't do anything */
1344         flags |= SWP_NOSIZE;
1345     }
1346     else
1347         TRACE( "%p resizing from (%dx%d) to (%dx%d)\n",
1348                hwnd, rect.right - rect.left, rect.bottom - rect.top, cx, cy );
1349
1350     data->lock_changes++;
1351     SetWindowPos( hwnd, 0, x, y, cx, cy, flags );
1352     data->lock_changes--;
1353 }
1354
1355
1356 /***********************************************************************
1357  *              SetWindowRgn  (X11DRV.@)
1358  *
1359  * Assign specified region to window (for non-rectangular windows)
1360  */
1361 int X11DRV_SetWindowRgn( HWND hwnd, HRGN hrgn, BOOL redraw )
1362 {
1363     struct x11drv_win_data *data;
1364
1365     if (!(data = X11DRV_get_win_data( hwnd )))
1366     {
1367         if (IsWindow( hwnd ))
1368             FIXME( "not supported on other thread window %p\n", hwnd );
1369         SetLastError( ERROR_INVALID_WINDOW_HANDLE );
1370         return FALSE;
1371     }
1372
1373 #ifdef HAVE_LIBXSHAPE
1374     if (data->whole_window)
1375     {
1376         Display *display = thread_display();
1377
1378         if (!hrgn)
1379         {
1380             wine_tsx11_lock();
1381             XShapeCombineMask( display, data->whole_window,
1382                                ShapeBounding, 0, 0, None, ShapeSet );
1383             wine_tsx11_unlock();
1384         }
1385         else
1386         {
1387             RGNDATA *pRegionData = X11DRV_GetRegionData( hrgn, 0 );
1388             if (pRegionData)
1389             {
1390                 wine_tsx11_lock();
1391                 XShapeCombineRectangles( display, data->whole_window, ShapeBounding,
1392                                          data->window_rect.left - data->whole_rect.left,
1393                                          data->window_rect.top - data->whole_rect.top,
1394                                          (XRectangle *)pRegionData->Buffer,
1395                                          pRegionData->rdh.nCount,
1396                                          ShapeSet, YXBanded );
1397                 wine_tsx11_unlock();
1398                 HeapFree(GetProcessHeap(), 0, pRegionData);
1399             }
1400         }
1401     }
1402 #endif  /* HAVE_LIBXSHAPE */
1403
1404     invalidate_dce( hwnd, &data->window_rect );
1405     return TRUE;
1406 }
1407
1408
1409 /***********************************************************************
1410  *           draw_moving_frame
1411  *
1412  * Draw the frame used when moving or resizing window.
1413  *
1414  * FIXME:  This causes problems in Win95 mode.  (why?)
1415  */
1416 static void draw_moving_frame( HDC hdc, RECT *rect, BOOL thickframe )
1417 {
1418     if (thickframe)
1419     {
1420         const int width = GetSystemMetrics(SM_CXFRAME);
1421         const int height = GetSystemMetrics(SM_CYFRAME);
1422
1423         HBRUSH hbrush = SelectObject( hdc, GetStockObject( GRAY_BRUSH ) );
1424         PatBlt( hdc, rect->left, rect->top,
1425                 rect->right - rect->left - width, height, PATINVERT );
1426         PatBlt( hdc, rect->left, rect->top + height, width,
1427                 rect->bottom - rect->top - height, PATINVERT );
1428         PatBlt( hdc, rect->left + width, rect->bottom - 1,
1429                 rect->right - rect->left - width, -height, PATINVERT );
1430         PatBlt( hdc, rect->right - 1, rect->top, -width,
1431                 rect->bottom - rect->top - height, PATINVERT );
1432         SelectObject( hdc, hbrush );
1433     }
1434     else DrawFocusRect( hdc, rect );
1435 }
1436
1437
1438 /***********************************************************************
1439  *           start_size_move
1440  *
1441  * Initialisation of a move or resize, when initiatied from a menu choice.
1442  * Return hit test code for caption or sizing border.
1443  */
1444 static LONG start_size_move( HWND hwnd, WPARAM wParam, POINT *capturePoint, LONG style )
1445 {
1446     LONG hittest = 0;
1447     POINT pt;
1448     MSG msg;
1449     RECT rectWindow;
1450
1451     GetWindowRect( hwnd, &rectWindow );
1452
1453     if ((wParam & 0xfff0) == SC_MOVE)
1454     {
1455         /* Move pointer at the center of the caption */
1456         RECT rect = rectWindow;
1457         /* Note: to be exactly centered we should take the different types
1458          * of border into account, but it shouldn't make more that a few pixels
1459          * of difference so let's not bother with that */
1460         rect.top += GetSystemMetrics(SM_CYBORDER);
1461         if (style & WS_SYSMENU)
1462             rect.left += GetSystemMetrics(SM_CXSIZE) + 1;
1463         if (style & WS_MINIMIZEBOX)
1464             rect.right -= GetSystemMetrics(SM_CXSIZE) + 1;
1465         if (style & WS_MAXIMIZEBOX)
1466             rect.right -= GetSystemMetrics(SM_CXSIZE) + 1;
1467         pt.x = (rect.right + rect.left) / 2;
1468         pt.y = rect.top + GetSystemMetrics(SM_CYSIZE)/2;
1469         hittest = HTCAPTION;
1470         *capturePoint = pt;
1471     }
1472     else  /* SC_SIZE */
1473     {
1474         pt.x = pt.y = 0;
1475         while(!hittest)
1476         {
1477             GetMessageW( &msg, 0, WM_KEYFIRST, WM_MOUSELAST );
1478             if (CallMsgFilterW( &msg, MSGF_SIZE )) continue;
1479
1480             switch(msg.message)
1481             {
1482             case WM_MOUSEMOVE:
1483                 pt = msg.pt;
1484                 hittest = SendMessageW( hwnd, WM_NCHITTEST, 0, MAKELONG( pt.x, pt.y ) );
1485                 if ((hittest < HTLEFT) || (hittest > HTBOTTOMRIGHT)) hittest = 0;
1486                 break;
1487
1488             case WM_LBUTTONUP:
1489                 return 0;
1490
1491             case WM_KEYDOWN:
1492                 switch(msg.wParam)
1493                 {
1494                 case VK_UP:
1495                     hittest = HTTOP;
1496                     pt.x =(rectWindow.left+rectWindow.right)/2;
1497                     pt.y = rectWindow.top + GetSystemMetrics(SM_CYFRAME) / 2;
1498                     break;
1499                 case VK_DOWN:
1500                     hittest = HTBOTTOM;
1501                     pt.x =(rectWindow.left+rectWindow.right)/2;
1502                     pt.y = rectWindow.bottom - GetSystemMetrics(SM_CYFRAME) / 2;
1503                     break;
1504                 case VK_LEFT:
1505                     hittest = HTLEFT;
1506                     pt.x = rectWindow.left + GetSystemMetrics(SM_CXFRAME) / 2;
1507                     pt.y =(rectWindow.top+rectWindow.bottom)/2;
1508                     break;
1509                 case VK_RIGHT:
1510                     hittest = HTRIGHT;
1511                     pt.x = rectWindow.right - GetSystemMetrics(SM_CXFRAME) / 2;
1512                     pt.y =(rectWindow.top+rectWindow.bottom)/2;
1513                     break;
1514                 case VK_RETURN:
1515                 case VK_ESCAPE: return 0;
1516                 }
1517             }
1518         }
1519         *capturePoint = pt;
1520     }
1521     SetCursorPos( pt.x, pt.y );
1522     SendMessageW( hwnd, WM_SETCURSOR, (WPARAM)hwnd, MAKELONG( hittest, WM_MOUSEMOVE ));
1523     return hittest;
1524 }
1525
1526
1527 /***********************************************************************
1528  *           set_movesize_capture
1529  */
1530 static void set_movesize_capture( HWND hwnd )
1531 {
1532     HWND previous = 0;
1533
1534     SERVER_START_REQ( set_capture_window )
1535     {
1536         req->handle = hwnd;
1537         req->flags  = CAPTURE_MOVESIZE;
1538         if (!wine_server_call_err( req ))
1539         {
1540             previous = reply->previous;
1541             hwnd = reply->full_handle;
1542         }
1543     }
1544     SERVER_END_REQ;
1545     if (previous && previous != hwnd)
1546         SendMessageW( previous, WM_CAPTURECHANGED, 0, (LPARAM)hwnd );
1547 }
1548
1549 /***********************************************************************
1550  *           X11DRV_WMMoveResizeWindow
1551  *
1552  * Tells the window manager to initiate a move or resize operation.
1553  *
1554  * SEE
1555  *  http://freedesktop.org/Standards/wm-spec/1.3/ar01s04.html
1556  *  or search for "_NET_WM_MOVERESIZE"
1557  */
1558 static void X11DRV_WMMoveResizeWindow( HWND hwnd, int x, int y, int dir )
1559 {
1560     XEvent xev;
1561     Display *display = thread_display();
1562
1563     TRACE("hwnd %p, x %d, y %d, dir %d\n", hwnd, x, y, dir);
1564
1565     xev.xclient.type = ClientMessage;
1566     xev.xclient.window = X11DRV_get_whole_window(hwnd);
1567     xev.xclient.message_type = x11drv_atom(_NET_WM_MOVERESIZE);
1568     xev.xclient.serial = 0;
1569     xev.xclient.display = display;
1570     xev.xclient.send_event = True;
1571     xev.xclient.format = 32;
1572     xev.xclient.data.l[0] = x; /* x coord */
1573     xev.xclient.data.l[1] = y; /* y coord */
1574     xev.xclient.data.l[2] = dir; /* direction */
1575     xev.xclient.data.l[3] = 1; /* button */
1576     xev.xclient.data.l[4] = 0; /* unused */
1577
1578     /* need to ungrab the pointer that may have been automatically grabbed
1579      * with a ButtonPress event */
1580     wine_tsx11_lock();
1581     XUngrabPointer( display, CurrentTime );
1582     XSendEvent(display, root_window, False, SubstructureNotifyMask, &xev);
1583     wine_tsx11_unlock();
1584 }
1585
1586 /***********************************************************************
1587  *           SysCommandSizeMove   (X11DRV.@)
1588  *
1589  * Perform SC_MOVE and SC_SIZE commands.
1590  */
1591 void X11DRV_SysCommandSizeMove( HWND hwnd, WPARAM wParam )
1592 {
1593     MSG msg;
1594     RECT sizingRect, mouseRect, origRect;
1595     HDC hdc;
1596     HWND parent;
1597     LONG hittest = (LONG)(wParam & 0x0f);
1598     WPARAM syscommand = wParam & 0xfff0;
1599     HCURSOR hDragCursor = 0, hOldCursor = 0;
1600     POINT minTrack, maxTrack;
1601     POINT capturePoint, pt;
1602     LONG style = GetWindowLongA( hwnd, GWL_STYLE );
1603     BOOL    thickframe = HAS_THICKFRAME( style );
1604     BOOL    iconic = style & WS_MINIMIZE;
1605     BOOL    moved = FALSE;
1606     DWORD     dwPoint = GetMessagePos ();
1607     BOOL DragFullWindows = FALSE;
1608     BOOL grab;
1609     Window parent_win, whole_win;
1610     Display *old_gdi_display = NULL;
1611     struct x11drv_thread_data *thread_data = x11drv_thread_data();
1612     struct x11drv_win_data *data;
1613
1614     pt.x = (short)LOWORD(dwPoint);
1615     pt.y = (short)HIWORD(dwPoint);
1616     capturePoint = pt;
1617
1618     if (IsZoomed(hwnd) || !IsWindowVisible(hwnd)) return;
1619
1620     if (!(data = X11DRV_get_win_data( hwnd ))) return;
1621
1622     TRACE("hwnd %p (%smanaged), command %04x, hittest %d, pos %d,%d\n",
1623           hwnd, data->managed ? "" : "NOT ", syscommand, hittest, pt.x, pt.y);
1624
1625     /* if we are managed then we let the WM do all the work */
1626     if (data->managed)
1627     {
1628         int dir;
1629         if (syscommand == SC_MOVE)
1630         {
1631             if (!hittest) dir = _NET_WM_MOVERESIZE_MOVE_KEYBOARD;
1632             else dir = _NET_WM_MOVERESIZE_MOVE;
1633         }
1634         else if (!hittest) dir = _NET_WM_MOVERESIZE_SIZE_KEYBOARD;
1635         else
1636             switch (hittest)
1637             {
1638             case WMSZ_LEFT:        dir = _NET_WM_MOVERESIZE_SIZE_LEFT; break;
1639             case WMSZ_RIGHT:       dir = _NET_WM_MOVERESIZE_SIZE_RIGHT; break;
1640             case WMSZ_TOP:         dir = _NET_WM_MOVERESIZE_SIZE_TOP; break;
1641             case WMSZ_TOPLEFT:     dir = _NET_WM_MOVERESIZE_SIZE_TOPLEFT; break;
1642             case WMSZ_TOPRIGHT:    dir = _NET_WM_MOVERESIZE_SIZE_TOPRIGHT; break;
1643             case WMSZ_BOTTOM:      dir = _NET_WM_MOVERESIZE_SIZE_BOTTOM; break;
1644             case WMSZ_BOTTOMLEFT:  dir = _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT; break;
1645             case WMSZ_BOTTOMRIGHT: dir = _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT; break;
1646             default:
1647                 ERR("Invalid hittest value: %d\n", hittest);
1648                 dir = _NET_WM_MOVERESIZE_SIZE_KEYBOARD;
1649             }
1650         X11DRV_WMMoveResizeWindow( hwnd, pt.x, pt.y, dir );
1651         return;
1652     }
1653
1654     SystemParametersInfoA(SPI_GETDRAGFULLWINDOWS, 0, &DragFullWindows, 0);
1655
1656     if (syscommand == SC_MOVE)
1657     {
1658         if (!hittest) hittest = start_size_move( hwnd, wParam, &capturePoint, style );
1659         if (!hittest) return;
1660     }
1661     else  /* SC_SIZE */
1662     {
1663         if ( hittest && (syscommand != SC_MOUSEMENU) )
1664             hittest += (HTLEFT - WMSZ_LEFT);
1665         else
1666         {
1667             set_movesize_capture( hwnd );
1668             hittest = start_size_move( hwnd, wParam, &capturePoint, style );
1669             if (!hittest)
1670             {
1671                 set_movesize_capture(0);
1672                 return;
1673             }
1674         }
1675     }
1676
1677       /* Get min/max info */
1678
1679     WINPOS_GetMinMaxInfo( hwnd, NULL, NULL, &minTrack, &maxTrack );
1680     GetWindowRect( hwnd, &sizingRect );
1681     if (style & WS_CHILD)
1682     {
1683         parent = GetParent(hwnd);
1684         /* make sizing rect relative to parent */
1685         MapWindowPoints( 0, parent, (POINT*)&sizingRect, 2 );
1686         GetClientRect( parent, &mouseRect );
1687     }
1688     else
1689     {
1690         parent = 0;
1691         mouseRect = virtual_screen_rect;
1692     }
1693     origRect = sizingRect;
1694
1695     if (ON_LEFT_BORDER(hittest))
1696     {
1697         mouseRect.left  = max( mouseRect.left, sizingRect.right-maxTrack.x );
1698         mouseRect.right = min( mouseRect.right, sizingRect.right-minTrack.x );
1699     }
1700     else if (ON_RIGHT_BORDER(hittest))
1701     {
1702         mouseRect.left  = max( mouseRect.left, sizingRect.left+minTrack.x );
1703         mouseRect.right = min( mouseRect.right, sizingRect.left+maxTrack.x );
1704     }
1705     if (ON_TOP_BORDER(hittest))
1706     {
1707         mouseRect.top    = max( mouseRect.top, sizingRect.bottom-maxTrack.y );
1708         mouseRect.bottom = min( mouseRect.bottom,sizingRect.bottom-minTrack.y);
1709     }
1710     else if (ON_BOTTOM_BORDER(hittest))
1711     {
1712         mouseRect.top    = max( mouseRect.top, sizingRect.top+minTrack.y );
1713         mouseRect.bottom = min( mouseRect.bottom, sizingRect.top+maxTrack.y );
1714     }
1715     if (parent) MapWindowPoints( parent, 0, (LPPOINT)&mouseRect, 2 );
1716
1717     /* Retrieve a default cache DC (without using the window style) */
1718     hdc = GetDCEx( parent, 0, DCX_CACHE );
1719
1720     if( iconic ) /* create a cursor for dragging */
1721     {
1722         hDragCursor = (HCURSOR)GetClassLongPtrW( hwnd, GCLP_HICON);
1723         if( !hDragCursor ) hDragCursor = (HCURSOR)SendMessageW( hwnd, WM_QUERYDRAGICON, 0, 0L);
1724         if( !hDragCursor ) iconic = FALSE;
1725     }
1726
1727     /* repaint the window before moving it around */
1728     RedrawWindow( hwnd, NULL, 0, RDW_UPDATENOW | RDW_ALLCHILDREN );
1729
1730     SendMessageW( hwnd, WM_ENTERSIZEMOVE, 0, 0 );
1731     set_movesize_capture( hwnd );
1732
1733     /* grab the server only when moving top-level windows without desktop */
1734     grab = (!DragFullWindows && !parent && (root_window == DefaultRootWindow(gdi_display)));
1735
1736     if (grab)
1737     {
1738         wine_tsx11_lock();
1739         XSync( gdi_display, False );
1740         XGrabServer( thread_data->display );
1741         XSync( thread_data->display, False );
1742         /* switch gdi display to the thread display, since the server is grabbed */
1743         old_gdi_display = gdi_display;
1744         gdi_display = thread_data->display;
1745         wine_tsx11_unlock();
1746     }
1747     whole_win = X11DRV_get_whole_window( GetAncestor(hwnd,GA_ROOT) );
1748     parent_win = parent ? X11DRV_get_whole_window( GetAncestor(parent,GA_ROOT) ) : root_window;
1749
1750     wine_tsx11_lock();
1751     XGrabPointer( thread_data->display, whole_win, False,
1752                   PointerMotionMask | ButtonPressMask | ButtonReleaseMask,
1753                   GrabModeAsync, GrabModeAsync, parent_win, None, CurrentTime );
1754     wine_tsx11_unlock();
1755     thread_data->grab_window = whole_win;
1756
1757     while(1)
1758     {
1759         int dx = 0, dy = 0;
1760
1761         if (!GetMessageW( &msg, 0, WM_KEYFIRST, WM_MOUSELAST )) break;
1762         if (CallMsgFilterW( &msg, MSGF_SIZE )) continue;
1763
1764         /* Exit on button-up, Return, or Esc */
1765         if ((msg.message == WM_LBUTTONUP) ||
1766             ((msg.message == WM_KEYDOWN) &&
1767              ((msg.wParam == VK_RETURN) || (msg.wParam == VK_ESCAPE)))) break;
1768
1769         if ((msg.message != WM_KEYDOWN) && (msg.message != WM_MOUSEMOVE))
1770             continue;  /* We are not interested in other messages */
1771
1772         pt = msg.pt;
1773
1774         if (msg.message == WM_KEYDOWN) switch(msg.wParam)
1775         {
1776         case VK_UP:    pt.y -= 8; break;
1777         case VK_DOWN:  pt.y += 8; break;
1778         case VK_LEFT:  pt.x -= 8; break;
1779         case VK_RIGHT: pt.x += 8; break;
1780         }
1781
1782         pt.x = max( pt.x, mouseRect.left );
1783         pt.x = min( pt.x, mouseRect.right );
1784         pt.y = max( pt.y, mouseRect.top );
1785         pt.y = min( pt.y, mouseRect.bottom );
1786
1787         dx = pt.x - capturePoint.x;
1788         dy = pt.y - capturePoint.y;
1789
1790         if (dx || dy)
1791         {
1792             if( !moved )
1793             {
1794                 moved = TRUE;
1795
1796                 if( iconic ) /* ok, no system popup tracking */
1797                 {
1798                     hOldCursor = SetCursor(hDragCursor);
1799                     ShowCursor( TRUE );
1800                     WINPOS_ShowIconTitle( hwnd, FALSE );
1801                 }
1802                 else if(!DragFullWindows)
1803                     draw_moving_frame( hdc, &sizingRect, thickframe );
1804             }
1805
1806             if (msg.message == WM_KEYDOWN) SetCursorPos( pt.x, pt.y );
1807             else
1808             {
1809                 RECT newRect = sizingRect;
1810                 WPARAM wpSizingHit = 0;
1811
1812                 if (hittest == HTCAPTION) OffsetRect( &newRect, dx, dy );
1813                 if (ON_LEFT_BORDER(hittest)) newRect.left += dx;
1814                 else if (ON_RIGHT_BORDER(hittest)) newRect.right += dx;
1815                 if (ON_TOP_BORDER(hittest)) newRect.top += dy;
1816                 else if (ON_BOTTOM_BORDER(hittest)) newRect.bottom += dy;
1817                 if(!iconic && !DragFullWindows) draw_moving_frame( hdc, &sizingRect, thickframe );
1818                 capturePoint = pt;
1819
1820                 /* determine the hit location */
1821                 if (hittest >= HTLEFT && hittest <= HTBOTTOMRIGHT)
1822                     wpSizingHit = WMSZ_LEFT + (hittest - HTLEFT);
1823                 SendMessageW( hwnd, WM_SIZING, wpSizingHit, (LPARAM)&newRect );
1824
1825                 if (!iconic)
1826                 {
1827                     if(!DragFullWindows)
1828                         draw_moving_frame( hdc, &newRect, thickframe );
1829                     else
1830                         SetWindowPos( hwnd, 0, newRect.left, newRect.top,
1831                                       newRect.right - newRect.left,
1832                                       newRect.bottom - newRect.top,
1833                                       ( hittest == HTCAPTION ) ? SWP_NOSIZE : 0 );
1834                 }
1835                 sizingRect = newRect;
1836             }
1837         }
1838     }
1839
1840     set_movesize_capture(0);
1841     if( iconic )
1842     {
1843         if( moved ) /* restore cursors, show icon title later on */
1844         {
1845             ShowCursor( FALSE );
1846             SetCursor( hOldCursor );
1847         }
1848     }
1849     else if (moved && !DragFullWindows)
1850         draw_moving_frame( hdc, &sizingRect, thickframe );
1851
1852     ReleaseDC( parent, hdc );
1853
1854     wine_tsx11_lock();
1855     XUngrabPointer( thread_data->display, CurrentTime );
1856     if (grab)
1857     {
1858         XSync( thread_data->display, False );
1859         XUngrabServer( thread_data->display );
1860         XSync( thread_data->display, False );
1861         gdi_display = old_gdi_display;
1862     }
1863     wine_tsx11_unlock();
1864     thread_data->grab_window = None;
1865
1866     if (HOOK_CallHooks( WH_CBT, HCBT_MOVESIZE, (WPARAM)hwnd, (LPARAM)&sizingRect, TRUE ))
1867         moved = FALSE;
1868
1869     SendMessageW( hwnd, WM_EXITSIZEMOVE, 0, 0 );
1870     SendMessageW( hwnd, WM_SETVISIBLE, !IsIconic(hwnd), 0L);
1871
1872     /* window moved or resized */
1873     if (moved)
1874     {
1875         /* if the moving/resizing isn't canceled call SetWindowPos
1876          * with the new position or the new size of the window
1877          */
1878         if (!((msg.message == WM_KEYDOWN) && (msg.wParam == VK_ESCAPE)) )
1879         {
1880             /* NOTE: SWP_NOACTIVATE prevents document window activation in Word 6 */
1881             if(!DragFullWindows)
1882                 SetWindowPos( hwnd, 0, sizingRect.left, sizingRect.top,
1883                               sizingRect.right - sizingRect.left,
1884                               sizingRect.bottom - sizingRect.top,
1885                               ( hittest == HTCAPTION ) ? SWP_NOSIZE : 0 );
1886         }
1887         else
1888         { /* restore previous size/position */
1889             if(DragFullWindows)
1890                 SetWindowPos( hwnd, 0, origRect.left, origRect.top,
1891                               origRect.right - origRect.left,
1892                               origRect.bottom - origRect.top,
1893                               ( hittest == HTCAPTION ) ? SWP_NOSIZE : 0 );
1894         }
1895     }
1896
1897     if (IsIconic(hwnd))
1898     {
1899         /* Single click brings up the system menu when iconized */
1900
1901         if( !moved )
1902         {
1903             if(style & WS_SYSMENU )
1904                 SendMessageW( hwnd, WM_SYSCOMMAND,
1905                               SC_MOUSEMENU + HTSYSMENU, MAKELONG(pt.x,pt.y));
1906         }
1907         else WINPOS_ShowIconTitle( hwnd, TRUE );
1908     }
1909 }