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