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