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