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