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