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