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