Added parser template and made AVISplitter use it.
[wine] / dlls / x11drv / 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
21
22 #include "config.h"
23
24 #include <X11/Xlib.h>
25 #ifdef HAVE_LIBXSHAPE
26 #include <X11/IntrinsicP.h>
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 "ntstatus.h"
37 #include "wownt32.h"
38 #include "wine/wingdi16.h"
39
40 #include "x11drv.h"
41 #include "win.h"
42 #include "winpos.h"
43 #include "dce.h"
44
45 #include "wine/server.h"
46 #include "wine/debug.h"
47
48 WINE_DEFAULT_DEBUG_CHANNEL(x11drv);
49
50 #define SWP_AGG_NOPOSCHANGE \
51     (SWP_NOSIZE | SWP_NOMOVE | SWP_NOCLIENTSIZE | SWP_NOCLIENTMOVE | SWP_NOZORDER)
52 #define SWP_AGG_STATUSFLAGS \
53     (SWP_AGG_NOPOSCHANGE | SWP_FRAMECHANGED | SWP_HIDEWINDOW | SWP_SHOWWINDOW)
54
55 #define SWP_EX_NOCOPY       0x0001
56 #define SWP_EX_PAINTSELF    0x0002
57 #define SWP_EX_NONCLIENT    0x0004
58
59 #define HAS_THICKFRAME(style) \
60     (((style) & WS_THICKFRAME) && \
61      !(((style) & (WS_DLGFRAME|WS_BORDER)) == WS_DLGFRAME))
62
63 #define ON_LEFT_BORDER(hit) \
64  (((hit) == HTLEFT) || ((hit) == HTTOPLEFT) || ((hit) == HTBOTTOMLEFT))
65 #define ON_RIGHT_BORDER(hit) \
66  (((hit) == HTRIGHT) || ((hit) == HTTOPRIGHT) || ((hit) == HTBOTTOMRIGHT))
67 #define ON_TOP_BORDER(hit) \
68  (((hit) == HTTOP) || ((hit) == HTTOPLEFT) || ((hit) == HTTOPRIGHT))
69 #define ON_BOTTOM_BORDER(hit) \
70  (((hit) == HTBOTTOM) || ((hit) == HTBOTTOMLEFT) || ((hit) == HTBOTTOMRIGHT))
71
72 #define _NET_WM_MOVERESIZE_SIZE_TOPLEFT      0
73 #define _NET_WM_MOVERESIZE_SIZE_TOP          1
74 #define _NET_WM_MOVERESIZE_SIZE_TOPRIGHT     2
75 #define _NET_WM_MOVERESIZE_SIZE_RIGHT        3
76 #define _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT  4
77 #define _NET_WM_MOVERESIZE_SIZE_BOTTOM       5
78 #define _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT   6
79 #define _NET_WM_MOVERESIZE_SIZE_LEFT         7
80 #define _NET_WM_MOVERESIZE_MOVE              8   /* movement only */
81 #define _NET_WM_MOVERESIZE_SIZE_KEYBOARD     9   /* size via keyboard */
82 #define _NET_WM_MOVERESIZE_MOVE_KEYBOARD    10   /* move via keyboard */
83
84
85 /***********************************************************************
86  *              get_server_visible_region
87  */
88 static HRGN get_server_visible_region( HWND hwnd, HWND top, UINT flags )
89 {
90     RGNDATA *data;
91     NTSTATUS status;
92     HRGN ret = 0;
93     size_t size = 256;
94
95     do
96     {
97         if (!(data = HeapAlloc( GetProcessHeap(), 0, sizeof(*data) + size - 1 ))) return 0;
98         SERVER_START_REQ( get_visible_region )
99         {
100             req->window  = hwnd;
101             req->top_win = top;
102             req->flags   = flags;
103             wine_server_set_reply( req, data->Buffer, size );
104             if (!(status = wine_server_call( req )))
105             {
106                 size_t reply_size = wine_server_reply_size( reply );
107                 data->rdh.dwSize   = sizeof(data->rdh);
108                 data->rdh.iType    = RDH_RECTANGLES;
109                 data->rdh.nCount   = reply_size / sizeof(RECT);
110                 data->rdh.nRgnSize = reply_size;
111                 ret = ExtCreateRegion( NULL, size, data );
112             }
113             else size = reply->total_size;
114         }
115         SERVER_END_REQ;
116         HeapFree( GetProcessHeap(), 0, data );
117     } while (status == STATUS_BUFFER_OVERFLOW);
118
119     if (status) SetLastError( RtlNtStatusToDosError(status) );
120     return ret;
121 }
122
123
124 /***********************************************************************
125  *           get_top_clipping_window
126  *
127  * Get the top window to clip against (i.e. the top parent that has
128  * an associated X window).
129  */
130 static HWND get_top_clipping_window( HWND hwnd )
131 {
132     HWND ret = GetAncestor( hwnd, GA_ROOT );
133     if (!ret) ret = GetDesktopWindow();
134     return ret;
135 }
136
137
138 /***********************************************************************
139  *           X11DRV_Expose
140  */
141 void X11DRV_Expose( HWND hwnd, XExposeEvent *event )
142 {
143     RECT rect;
144     struct x11drv_win_data *data;
145     int flags = RDW_INVALIDATE | RDW_ERASE | RDW_ALLCHILDREN;
146
147     TRACE( "win %p (%lx) %d,%d %dx%d\n",
148            hwnd, event->window, event->x, event->y, event->width, event->height );
149
150     if (!(data = X11DRV_get_win_data( hwnd ))) return;
151
152     rect.left   = event->x;
153     rect.top    = event->y;
154     rect.right  = rect.left + event->width;
155     rect.bottom = rect.top + event->height;
156
157     if (rect.left < data->client_rect.left ||
158         rect.top < data->client_rect.top ||
159         rect.right > data->client_rect.right ||
160         rect.bottom > data->client_rect.bottom) flags |= RDW_FRAME;
161
162     /* make position relative to client area instead of window */
163     OffsetRect( &rect, -data->client_rect.left, -data->client_rect.top );
164
165     RedrawWindow( hwnd, &rect, 0, flags );
166 }
167
168
169 /***********************************************************************
170  *              GetDC (X11DRV.@)
171  *
172  * Set the drawable, origin and dimensions for the DC associated to
173  * a given window.
174  */
175 BOOL X11DRV_GetDC( HWND hwnd, HDC hdc, HRGN hrgn, DWORD flags )
176 {
177     HWND top = get_top_clipping_window( hwnd );
178     struct x11drv_escape_set_drawable escape;
179
180     escape.mode = IncludeInferiors;
181     /* don't clip siblings if using parent clip region */
182     if (flags & DCX_PARENTCLIP) flags &= ~DCX_CLIPSIBLINGS;
183
184     if (top != hwnd)
185     {
186         POINT client_offset;
187
188         if (flags & DCX_WINDOW)
189         {
190             RECT rect;
191             GetWindowRect( hwnd, &rect );
192             escape.org.x = rect.left;
193             escape.org.y = rect.top;
194             MapWindowPoints( 0, top, &escape.org, 1 );
195             escape.drawable_org.x = rect.left - escape.org.x;
196             escape.drawable_org.y = rect.top - escape.org.y;
197         }
198         else
199         {
200             escape.org.x = escape.org.y = 0;
201             escape.drawable_org.x = escape.drawable_org.y = 0;
202             MapWindowPoints( hwnd, top, &escape.org, 1 );
203             MapWindowPoints( top, 0, &escape.drawable_org, 1 );
204         }
205
206         /* now make origins relative to the X window and not the client area */
207         client_offset = X11DRV_get_client_area_offset( top );
208         escape.org.x += client_offset.x;
209         escape.org.y += client_offset.y;
210         escape.drawable_org.x -= client_offset.x;
211         escape.drawable_org.y -= client_offset.y;
212         escape.drawable = X11DRV_get_whole_window( top );
213     }
214     else
215     {
216         struct x11drv_win_data *data;
217
218         if (!(data = X11DRV_get_win_data( hwnd ))) return FALSE;
219
220         if (IsIconic( hwnd ))
221         {
222             escape.drawable = data->icon_window ? data->icon_window : data->whole_window;
223             escape.org.x = 0;
224             escape.org.y = 0;
225             escape.drawable_org = escape.org;
226         }
227         else
228         {
229             escape.drawable = data->whole_window;
230             escape.drawable_org.x = data->whole_rect.left;
231             escape.drawable_org.y = data->whole_rect.top;
232             if (flags & DCX_WINDOW)
233             {
234                 escape.org.x = data->window_rect.left - data->whole_rect.left;
235                 escape.org.y = data->window_rect.top - data->whole_rect.top;
236             }
237             else
238             {
239                 escape.org.x = data->client_rect.left;
240                 escape.org.y = data->client_rect.top;
241             }
242         }
243     }
244
245     escape.code = X11DRV_SET_DRAWABLE;
246     ExtEscape( hdc, X11DRV_ESCAPE, sizeof(escape), (LPSTR)&escape, 0, NULL );
247
248     if (flags & (DCX_EXCLUDERGN | DCX_INTERSECTRGN) ||
249         SetHookFlags16( HDC_16(hdc), DCHF_VALIDATEVISRGN ))  /* DC was dirty */
250     {
251         /* need to recompute the visible region */
252         HRGN visRgn = get_server_visible_region( hwnd, top, flags );
253
254         if (flags & (DCX_EXCLUDERGN | DCX_INTERSECTRGN))
255             CombineRgn( visRgn, visRgn, hrgn, (flags & DCX_INTERSECTRGN) ? RGN_AND : RGN_DIFF );
256
257         SelectVisRgn16( HDC_16(hdc), HRGN_16(visRgn) );
258         DeleteObject( visRgn );
259     }
260     return TRUE;
261 }
262
263
264 /***********************************************************************
265  *              ReleaseDC (X11DRV.@)
266  */
267 void X11DRV_ReleaseDC( HWND hwnd, HDC hdc )
268 {
269     struct x11drv_escape_set_drawable escape;
270
271     escape.code = X11DRV_SET_DRAWABLE;
272     escape.drawable = root_window;
273     escape.mode = IncludeInferiors;
274     escape.org.x = escape.org.y = 0;
275     escape.drawable_org.x = escape.drawable_org.y = 0;
276
277     ExtEscape( hdc, X11DRV_ESCAPE, sizeof(escape), (LPSTR)&escape, 0, NULL );
278 }
279
280
281 /***********************************************************************
282  *           SWP_DoWinPosChanging
283  */
284 static BOOL SWP_DoWinPosChanging( WINDOWPOS* pWinpos, RECT* pNewWindowRect, RECT* pNewClientRect )
285 {
286     WND *wndPtr;
287
288     /* Send WM_WINDOWPOSCHANGING message */
289
290     if (!(pWinpos->flags & SWP_NOSENDCHANGING))
291         SendMessageW( pWinpos->hwnd, WM_WINDOWPOSCHANGING, 0, (LPARAM)pWinpos );
292
293     if (!(wndPtr = WIN_GetPtr( pWinpos->hwnd )) || wndPtr == WND_OTHER_PROCESS) return FALSE;
294
295     /* Calculate new position and size */
296
297     *pNewWindowRect = wndPtr->rectWindow;
298     *pNewClientRect = (wndPtr->dwStyle & WS_MINIMIZE) ? wndPtr->rectWindow
299                                                     : wndPtr->rectClient;
300
301     if (!(pWinpos->flags & SWP_NOSIZE))
302     {
303         pNewWindowRect->right  = pNewWindowRect->left + pWinpos->cx;
304         pNewWindowRect->bottom = pNewWindowRect->top + pWinpos->cy;
305     }
306     if (!(pWinpos->flags & SWP_NOMOVE))
307     {
308         pNewWindowRect->left    = pWinpos->x;
309         pNewWindowRect->top     = pWinpos->y;
310         pNewWindowRect->right  += pWinpos->x - wndPtr->rectWindow.left;
311         pNewWindowRect->bottom += pWinpos->y - wndPtr->rectWindow.top;
312
313         OffsetRect( pNewClientRect, pWinpos->x - wndPtr->rectWindow.left,
314                                     pWinpos->y - wndPtr->rectWindow.top );
315     }
316     pWinpos->flags |= SWP_NOCLIENTMOVE | SWP_NOCLIENTSIZE;
317
318     TRACE( "hwnd %p, after %p, swp %d,%d %dx%d flags %08x\n",
319            pWinpos->hwnd, pWinpos->hwndInsertAfter, pWinpos->x, pWinpos->y,
320            pWinpos->cx, pWinpos->cy, pWinpos->flags );
321     TRACE( "current %s style %08lx new %s\n",
322            wine_dbgstr_rect( &wndPtr->rectWindow ), wndPtr->dwStyle,
323            wine_dbgstr_rect( pNewWindowRect ));
324
325     WIN_ReleasePtr( wndPtr );
326     return TRUE;
327 }
328
329
330 /***********************************************************************
331  *           get_valid_rects
332  *
333  * Compute the valid rects from the old and new client rect and WVR_* flags.
334  * Helper for WM_NCCALCSIZE handling.
335  */
336 static inline void get_valid_rects( const RECT *old_client, const RECT *new_client, UINT flags,
337                                     RECT *valid )
338 {
339     int cx, cy;
340
341     if (flags & WVR_REDRAW)
342     {
343         SetRectEmpty( &valid[0] );
344         SetRectEmpty( &valid[1] );
345         return;
346     }
347
348     if (flags & WVR_VALIDRECTS)
349     {
350         if (!IntersectRect( &valid[0], &valid[0], new_client ) ||
351             !IntersectRect( &valid[1], &valid[1], old_client ))
352         {
353             SetRectEmpty( &valid[0] );
354             SetRectEmpty( &valid[1] );
355             return;
356         }
357         flags = WVR_ALIGNLEFT | WVR_ALIGNTOP;
358     }
359     else
360     {
361         valid[0] = *new_client;
362         valid[1] = *old_client;
363     }
364
365     /* make sure the rectangles have the same size */
366     cx = min( valid[0].right - valid[0].left, valid[1].right - valid[1].left );
367     cy = min( valid[0].bottom - valid[0].top, valid[1].bottom - valid[1].top );
368
369     if (flags & WVR_ALIGNBOTTOM)
370     {
371         valid[0].top = valid[0].bottom - cy;
372         valid[1].top = valid[1].bottom - cy;
373     }
374     else
375     {
376         valid[0].bottom = valid[0].top + cy;
377         valid[1].bottom = valid[1].top + cy;
378     }
379     if (flags & WVR_ALIGNRIGHT)
380     {
381         valid[0].left = valid[0].right - cx;
382         valid[1].left = valid[1].right - cx;
383     }
384     else
385     {
386         valid[0].right = valid[0].left + cx;
387         valid[1].right = valid[1].left + cx;
388     }
389 }
390
391
392 /***********************************************************************
393  *           SWP_DoNCCalcSize
394  */
395 static UINT SWP_DoNCCalcSize( WINDOWPOS* pWinpos, const RECT* pNewWindowRect, RECT* pNewClientRect,
396                               RECT *validRects )
397 {
398     UINT wvrFlags = 0;
399     WND *wndPtr;
400
401     if (!(wndPtr = WIN_GetPtr( pWinpos->hwnd )) || wndPtr == WND_OTHER_PROCESS) return 0;
402
403       /* Send WM_NCCALCSIZE message to get new client area */
404     if( (pWinpos->flags & (SWP_FRAMECHANGED | SWP_NOSIZE)) != SWP_NOSIZE )
405     {
406         NCCALCSIZE_PARAMS params;
407         WINDOWPOS winposCopy;
408
409         params.rgrc[0] = *pNewWindowRect;
410         params.rgrc[1] = wndPtr->rectWindow;
411         params.rgrc[2] = wndPtr->rectClient;
412         params.lppos = &winposCopy;
413         winposCopy = *pWinpos;
414         WIN_ReleasePtr( wndPtr );
415
416         wvrFlags = SendMessageW( pWinpos->hwnd, WM_NCCALCSIZE, TRUE, (LPARAM)&params );
417
418         TRACE( "(%ld,%ld)-(%ld,%ld)\n", params.rgrc[0].left, params.rgrc[0].top,
419                params.rgrc[0].right, params.rgrc[0].bottom );
420
421         /* If the application sends back garbage, ignore it */
422
423         if (params.rgrc[0].left < pNewWindowRect->left) params.rgrc[0].left = pNewWindowRect->left;
424         if (params.rgrc[0].top < pNewWindowRect->top) params.rgrc[0].top = pNewWindowRect->top;
425         if (params.rgrc[0].right > pNewWindowRect->right) params.rgrc[0].right = pNewWindowRect->right;
426         if (params.rgrc[0].bottom > pNewWindowRect->bottom) params.rgrc[0].bottom = pNewWindowRect->bottom;
427
428         if (params.rgrc[0].left <= params.rgrc[0].right &&
429             params.rgrc[0].top <= params.rgrc[0].bottom)
430             *pNewClientRect = params.rgrc[0];
431
432         if (!(wndPtr = WIN_GetPtr( pWinpos->hwnd )) || wndPtr == WND_OTHER_PROCESS) return 0;
433
434         if( pNewClientRect->left != wndPtr->rectClient.left ||
435             pNewClientRect->top != wndPtr->rectClient.top )
436             pWinpos->flags &= ~SWP_NOCLIENTMOVE;
437
438         if( (pNewClientRect->right - pNewClientRect->left !=
439              wndPtr->rectClient.right - wndPtr->rectClient.left))
440             pWinpos->flags &= ~SWP_NOCLIENTSIZE;
441         else
442             wvrFlags &= ~WVR_HREDRAW;
443
444         if (pNewClientRect->bottom - pNewClientRect->top !=
445              wndPtr->rectClient.bottom - wndPtr->rectClient.top)
446             pWinpos->flags &= ~SWP_NOCLIENTSIZE;
447         else
448             wvrFlags &= ~WVR_VREDRAW;
449
450         validRects[0] = params.rgrc[1];
451         validRects[1] = params.rgrc[2];
452     }
453     else
454     {
455         if (!(pWinpos->flags & SWP_NOMOVE) &&
456             (pNewClientRect->left != wndPtr->rectClient.left ||
457              pNewClientRect->top != wndPtr->rectClient.top))
458             pWinpos->flags &= ~SWP_NOCLIENTMOVE;
459     }
460
461     if (pWinpos->flags & (SWP_NOCOPYBITS | SWP_NOREDRAW | SWP_SHOWWINDOW | SWP_HIDEWINDOW))
462     {
463         SetRectEmpty( &validRects[0] );
464         SetRectEmpty( &validRects[1] );
465     }
466     else get_valid_rects( &wndPtr->rectClient, pNewClientRect, wvrFlags, validRects );
467
468     WIN_ReleasePtr( wndPtr );
469     return wvrFlags;
470 }
471
472
473 /***********************************************************************
474  *           SWP_DoOwnedPopups
475  *
476  * fix Z order taking into account owned popups -
477  * basically we need to maintain them above the window that owns them
478  *
479  * FIXME: hide/show owned popups when owner visibility changes.
480  */
481 static HWND SWP_DoOwnedPopups(HWND hwnd, HWND hwndInsertAfter)
482 {
483     HWND *list = NULL;
484     HWND owner = GetWindow( hwnd, GW_OWNER );
485     LONG style = GetWindowLongW( hwnd, GWL_STYLE );
486
487     WARN("(%p) hInsertAfter = %p\n", hwnd, hwndInsertAfter );
488
489     if ((style & WS_POPUP) && owner)
490     {
491         /* make sure this popup stays above the owner */
492
493         HWND hwndLocalPrev = HWND_TOP;
494
495         if( hwndInsertAfter != HWND_TOP )
496         {
497             if ((list = WIN_ListChildren( GetDesktopWindow() )))
498             {
499                 int i;
500                 for (i = 0; list[i]; i++)
501                 {
502                     if (list[i] == owner) break;
503                     if (list[i] != hwnd) hwndLocalPrev = list[i];
504                     if (hwndLocalPrev == hwndInsertAfter) break;
505                 }
506                 hwndInsertAfter = hwndLocalPrev;
507             }
508         }
509     }
510     else if (style & WS_CHILD) return hwndInsertAfter;
511
512     if (!list) list = WIN_ListChildren( GetDesktopWindow() );
513     if (list)
514     {
515         int i;
516         for (i = 0; list[i]; i++)
517         {
518             if (list[i] == hwnd) break;
519             if ((GetWindowLongW( list[i], GWL_STYLE ) & WS_POPUP) &&
520                 GetWindow( list[i], GW_OWNER ) == hwnd)
521             {
522                 SetWindowPos( list[i], hwndInsertAfter, 0, 0, 0, 0,
523                               SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE |
524                               SWP_NOSENDCHANGING | SWP_DEFERERASE );
525                 hwndInsertAfter = list[i];
526             }
527         }
528         HeapFree( GetProcessHeap(), 0, list );
529     }
530
531     return hwndInsertAfter;
532 }
533
534
535 /* fix redundant flags and values in the WINDOWPOS structure */
536 static BOOL fixup_flags( WINDOWPOS *winpos )
537 {
538     HWND parent;
539     WND *wndPtr = WIN_GetPtr( winpos->hwnd );
540     BOOL ret = TRUE;
541
542     if (!wndPtr || wndPtr == WND_OTHER_PROCESS)
543     {
544         SetLastError( ERROR_INVALID_WINDOW_HANDLE );
545         return FALSE;
546     }
547     winpos->hwnd = wndPtr->hwndSelf;  /* make it a full handle */
548
549     /* Finally make sure that all coordinates are valid */
550     if (winpos->x < -32768) winpos->x = -32768;
551     else if (winpos->x > 32767) winpos->x = 32767;
552     if (winpos->y < -32768) winpos->y = -32768;
553     else if (winpos->y > 32767) winpos->y = 32767;
554
555     if (winpos->cx < 0) winpos->cx = 0;
556     else if (winpos->cx > 32767) winpos->cx = 32767;
557     if (winpos->cy < 0) winpos->cy = 0;
558     else if (winpos->cy > 32767) winpos->cy = 32767;
559
560     parent = GetAncestor( winpos->hwnd, GA_PARENT );
561     if (!IsWindowVisible( parent )) winpos->flags |= SWP_NOREDRAW;
562
563     if (wndPtr->dwStyle & WS_VISIBLE) winpos->flags &= ~SWP_SHOWWINDOW;
564     else
565     {
566         winpos->flags &= ~SWP_HIDEWINDOW;
567         if (!(winpos->flags & SWP_SHOWWINDOW)) winpos->flags |= SWP_NOREDRAW;
568     }
569
570     if ((wndPtr->rectWindow.right - wndPtr->rectWindow.left == winpos->cx) &&
571         (wndPtr->rectWindow.bottom - wndPtr->rectWindow.top == winpos->cy))
572         winpos->flags |= SWP_NOSIZE;    /* Already the right size */
573
574     if ((wndPtr->rectWindow.left == winpos->x) && (wndPtr->rectWindow.top == winpos->y))
575         winpos->flags |= SWP_NOMOVE;    /* Already the right position */
576
577     if ((wndPtr->dwStyle & (WS_POPUP | WS_CHILD)) != WS_CHILD)
578     {
579         if (!(winpos->flags & (SWP_NOACTIVATE|SWP_HIDEWINDOW))) /* Bring to the top when activating */
580         {
581             winpos->flags &= ~SWP_NOZORDER;
582             winpos->hwndInsertAfter = HWND_TOP;
583         }
584     }
585
586     /* Check hwndInsertAfter */
587     if (winpos->flags & SWP_NOZORDER) goto done;
588
589     /* fix sign extension */
590     if (winpos->hwndInsertAfter == (HWND)0xffff) winpos->hwndInsertAfter = HWND_TOPMOST;
591     else if (winpos->hwndInsertAfter == (HWND)0xfffe) winpos->hwndInsertAfter = HWND_NOTOPMOST;
592
593       /* FIXME: TOPMOST not supported yet */
594     if ((winpos->hwndInsertAfter == HWND_TOPMOST) ||
595         (winpos->hwndInsertAfter == HWND_NOTOPMOST)) winpos->hwndInsertAfter = HWND_TOP;
596
597     /* hwndInsertAfter must be a sibling of the window */
598     if (winpos->hwndInsertAfter == HWND_TOP)
599     {
600         if (GetWindow(winpos->hwnd, GW_HWNDFIRST) == winpos->hwnd)
601             winpos->flags |= SWP_NOZORDER;
602     }
603     else if (winpos->hwndInsertAfter == HWND_BOTTOM)
604     {
605         if (GetWindow(winpos->hwnd, GW_HWNDLAST) == winpos->hwnd)
606             winpos->flags |= SWP_NOZORDER;
607     }
608     else
609     {
610         if (GetAncestor( winpos->hwndInsertAfter, GA_PARENT ) != parent) ret = FALSE;
611         else
612         {
613             /* don't need to change the Zorder of hwnd if it's already inserted
614              * after hwndInsertAfter or when inserting hwnd after itself.
615              */
616             if ((winpos->hwnd == winpos->hwndInsertAfter) ||
617                 (winpos->hwnd == GetWindow( winpos->hwndInsertAfter, GW_HWNDNEXT )))
618                 winpos->flags |= SWP_NOZORDER;
619         }
620     }
621  done:
622     WIN_ReleasePtr( wndPtr );
623     return ret;
624 }
625
626
627 /***********************************************************************
628  *              SetWindowStyle   (X11DRV.@)
629  *
630  * Update the X state of a window to reflect a style change
631  */
632 void X11DRV_SetWindowStyle( HWND hwnd, DWORD old_style )
633 {
634     Display *display = thread_display();
635     struct x11drv_win_data *data;
636     DWORD new_style, changed;
637
638     if (hwnd == GetDesktopWindow()) return;
639     if (!(data = X11DRV_get_win_data( hwnd ))) return;
640
641     new_style = GetWindowLongW( hwnd, GWL_STYLE );
642     changed = new_style ^ old_style;
643
644     if (changed & WS_VISIBLE)
645     {
646         if (data->whole_window && X11DRV_is_window_rect_mapped( &data->window_rect ))
647         {
648             if (new_style & WS_VISIBLE)
649             {
650                 TRACE( "mapping win %p\n", hwnd );
651                 X11DRV_sync_window_style( display, data );
652                 X11DRV_set_wm_hints( display, data );
653                 wine_tsx11_lock();
654                 XMapWindow( display, data->whole_window );
655                 wine_tsx11_unlock();
656             }
657             /* we don't unmap windows, that causes trouble with the window manager */
658         }
659         DCE_InvalidateDCE( hwnd, &data->window_rect );
660     }
661
662     if (changed & WS_DISABLED)
663     {
664         if (data->whole_window && data->managed)
665         {
666             XWMHints *wm_hints;
667             wine_tsx11_lock();
668             if (!(wm_hints = XGetWMHints( display, data->whole_window )))
669                 wm_hints = XAllocWMHints();
670             if (wm_hints)
671             {
672                 wm_hints->flags |= InputHint;
673                 wm_hints->input = !(new_style & WS_DISABLED);
674                 XSetWMHints( display, data->whole_window, wm_hints );
675                 XFree(wm_hints);
676             }
677             wine_tsx11_unlock();
678         }
679     }
680 }
681
682
683 /***********************************************************************
684  *           X11DRV_set_window_pos
685  *
686  * Set a window position and Z order.
687  */
688 BOOL X11DRV_set_window_pos( HWND hwnd, HWND insert_after, const RECT *rectWindow,
689                             const RECT *rectClient, UINT swp_flags, const RECT *valid_rects )
690 {
691     struct x11drv_win_data *data;
692     HWND top = get_top_clipping_window( hwnd );
693     RECT new_whole_rect;
694     WND *win;
695     DWORD old_style, new_style;
696     BOOL ret;
697
698     if (!(data = X11DRV_get_win_data( hwnd ))) return FALSE;
699
700     new_whole_rect = *rectWindow;
701     X11DRV_window_to_X_rect( data, &new_whole_rect );
702
703     if (!IsRectEmpty( &valid_rects[0] ))
704     {
705         int x_offset = 0, y_offset = 0;
706
707         if (data->whole_window)
708         {
709             /* the X server will move the bits for us */
710             x_offset = data->whole_rect.left - new_whole_rect.left;
711             y_offset = data->whole_rect.top - new_whole_rect.top;
712         }
713
714         if (x_offset != valid_rects[1].left - valid_rects[0].left ||
715             y_offset != valid_rects[1].top - valid_rects[0].top)
716         {
717             /* FIXME: should copy the window bits here */
718             valid_rects = NULL;
719         }
720     }
721
722     if (!(win = WIN_GetPtr( hwnd ))) return FALSE;
723     if (win == WND_OTHER_PROCESS)
724     {
725         if (IsWindow( hwnd )) ERR( "cannot set rectangles of other process window %p\n", hwnd );
726         return FALSE;
727     }
728     old_style = win->dwStyle;
729     SERVER_START_REQ( set_window_pos )
730     {
731         req->handle        = hwnd;
732         req->top_win       = top;
733         req->previous      = insert_after;
734         req->flags         = swp_flags & ~SWP_WINE_NOHOSTMOVE;
735         req->window.left   = rectWindow->left;
736         req->window.top    = rectWindow->top;
737         req->window.right  = rectWindow->right;
738         req->window.bottom = rectWindow->bottom;
739         req->client.left   = rectClient->left;
740         req->client.top    = rectClient->top;
741         req->client.right  = rectClient->right;
742         req->client.bottom = rectClient->bottom;
743         if (!IsRectEmpty( &valid_rects[0] ))
744             wine_server_add_data( req, valid_rects, 2 * sizeof(*valid_rects) );
745         ret = !wine_server_call( req );
746         new_style = reply->new_style;
747     }
748     SERVER_END_REQ;
749
750     if (ret)
751     {
752         Display *display = thread_display();
753
754         /* invalidate DCEs */
755
756         if ((((swp_flags & SWP_AGG_NOPOSCHANGE) != SWP_AGG_NOPOSCHANGE) && (new_style & WS_VISIBLE)) ||
757              (swp_flags & (SWP_HIDEWINDOW | SWP_SHOWWINDOW)))
758         {
759             RECT rect;
760             UnionRect( &rect, rectWindow, &win->rectWindow );
761             DCE_InvalidateDCE( hwnd, &rect );
762         }
763
764         win->rectWindow   = *rectWindow;
765         win->rectClient   = *rectClient;
766         win->dwStyle      = new_style;
767         data->window_rect = *rectWindow;
768
769         TRACE( "win %p window %s client %s style %08lx\n",
770                hwnd, wine_dbgstr_rect(rectWindow), wine_dbgstr_rect(rectClient), new_style );
771
772         /* FIXME: copy the valid bits */
773
774         if (data->whole_window)
775         {
776             if ((old_style & WS_VISIBLE) && !(new_style & WS_VISIBLE))
777             {
778                 /* window got hidden, unmap it */
779                 TRACE( "unmapping win %p\n", hwnd );
780                 wine_tsx11_lock();
781                 XUnmapWindow( display, data->whole_window );
782                 wine_tsx11_unlock();
783             }
784             else if ((new_style & WS_VISIBLE) && !X11DRV_is_window_rect_mapped( rectWindow ))
785             {
786                 /* resizing to zero size or off screen -> unmap */
787                 TRACE( "unmapping zero size or off-screen win %p\n", hwnd );
788                 wine_tsx11_lock();
789                 XUnmapWindow( display, data->whole_window );
790                 wine_tsx11_unlock();
791             }
792         }
793
794         X11DRV_sync_window_position( display, data, swp_flags, rectClient, &new_whole_rect );
795
796         if (data->whole_window)
797         {
798             if (!(old_style & WS_VISIBLE) && (new_style & WS_VISIBLE))
799             {
800                 /* window got shown, map it */
801                 if (X11DRV_is_window_rect_mapped( rectWindow ))
802                 {
803                     TRACE( "mapping win %p\n", hwnd );
804                     X11DRV_sync_window_style( display, data );
805                     X11DRV_set_wm_hints( display, data );
806                     wine_tsx11_lock();
807                     XMapWindow( display, data->whole_window );
808                     wine_tsx11_unlock();
809                 }
810             }
811             else if ((new_style & WS_VISIBLE) && X11DRV_is_window_rect_mapped( rectWindow ))
812             {
813                 /* resizing from zero size to non-zero -> map */
814                 TRACE( "mapping non zero size or off-screen win %p\n", hwnd );
815                 wine_tsx11_lock();
816                 XMapWindow( display, data->whole_window );
817                 wine_tsx11_unlock();
818             }
819             wine_tsx11_lock();
820             XFlush( display );  /* FIXME: should not be necessary */
821             wine_tsx11_unlock();
822         }
823     }
824     WIN_ReleasePtr( win );
825     return ret;
826 }
827
828
829 /***********************************************************************
830  *              SetWindowPos   (X11DRV.@)
831  */
832 BOOL X11DRV_SetWindowPos( WINDOWPOS *winpos )
833 {
834     RECT newWindowRect, newClientRect, valid_rects[2];
835     UINT orig_flags;
836
837     TRACE( "hwnd %p, after %p, swp %d,%d %dx%d flags %08x\n",
838            winpos->hwnd, winpos->hwndInsertAfter, winpos->x, winpos->y,
839            winpos->cx, winpos->cy, winpos->flags);
840
841     orig_flags = winpos->flags;
842     winpos->flags &= ~SWP_WINE_NOHOSTMOVE;
843
844     /* Check window handle */
845     if (winpos->hwnd == GetDesktopWindow()) return FALSE;
846
847     /* First make sure that coordinates are valid for WM_WINDOWPOSCHANGING */
848     if (!(winpos->flags & SWP_NOMOVE))
849     {
850         if (winpos->x < -32768) winpos->x = -32768;
851         else if (winpos->x > 32767) winpos->x = 32767;
852         if (winpos->y < -32768) winpos->y = -32768;
853         else if (winpos->y > 32767) winpos->y = 32767;
854     }
855     if (!(winpos->flags & SWP_NOSIZE))
856     {
857         if (winpos->cx < 0) winpos->cx = 0;
858         else if (winpos->cx > 32767) winpos->cx = 32767;
859         if (winpos->cy < 0) winpos->cy = 0;
860         else if (winpos->cy > 32767) winpos->cy = 32767;
861     }
862
863     if (!SWP_DoWinPosChanging( winpos, &newWindowRect, &newClientRect )) return FALSE;
864
865     /* Fix redundant flags */
866     if (!fixup_flags( winpos )) return FALSE;
867
868     if((winpos->flags & (SWP_NOZORDER | SWP_HIDEWINDOW | SWP_SHOWWINDOW)) != SWP_NOZORDER)
869     {
870         if (GetAncestor( winpos->hwnd, GA_PARENT ) == GetDesktopWindow())
871             winpos->hwndInsertAfter = SWP_DoOwnedPopups( winpos->hwnd, winpos->hwndInsertAfter );
872     }
873
874     /* Common operations */
875
876     SWP_DoNCCalcSize( winpos, &newWindowRect, &newClientRect, valid_rects );
877
878     if (!X11DRV_set_window_pos( winpos->hwnd, winpos->hwndInsertAfter,
879                                 &newWindowRect, &newClientRect, orig_flags, valid_rects ))
880         return FALSE;
881
882     if( winpos->flags & SWP_HIDEWINDOW )
883         HideCaret(winpos->hwnd);
884     else if (winpos->flags & SWP_SHOWWINDOW)
885         ShowCaret(winpos->hwnd);
886
887     if (!(winpos->flags & (SWP_NOACTIVATE|SWP_HIDEWINDOW)))
888     {
889         /* child windows get WM_CHILDACTIVATE message */
890         if ((GetWindowLongW( winpos->hwnd, GWL_STYLE ) & (WS_CHILD | WS_POPUP)) == WS_CHILD)
891             SendMessageA( winpos->hwnd, WM_CHILDACTIVATE, 0, 0 );
892         else
893             SetForegroundWindow( winpos->hwnd );
894     }
895
896       /* And last, send the WM_WINDOWPOSCHANGED message */
897
898     TRACE("\tstatus flags = %04x\n", winpos->flags & SWP_AGG_STATUSFLAGS);
899
900     if (((winpos->flags & SWP_AGG_STATUSFLAGS) != SWP_AGG_NOPOSCHANGE))
901     {
902         /* WM_WINDOWPOSCHANGED is sent even if SWP_NOSENDCHANGING is set
903            and always contains final window position.
904          */
905         winpos->x = newWindowRect.left;
906         winpos->y = newWindowRect.top;
907         winpos->cx = newWindowRect.right - newWindowRect.left;
908         winpos->cy = newWindowRect.bottom - newWindowRect.top;
909         SendMessageW( winpos->hwnd, WM_WINDOWPOSCHANGED, 0, (LPARAM)winpos );
910     }
911
912     return TRUE;
913 }
914
915
916 /***********************************************************************
917  *           WINPOS_FindIconPos
918  *
919  * Find a suitable place for an iconic window.
920  */
921 static POINT WINPOS_FindIconPos( WND* wndPtr, POINT pt )
922 {
923     RECT rectParent;
924     HWND *list;
925     short x, y, xspacing, yspacing;
926
927     GetClientRect( wndPtr->parent, &rectParent );
928     if ((pt.x >= rectParent.left) && (pt.x + GetSystemMetrics(SM_CXICON) < rectParent.right) &&
929         (pt.y >= rectParent.top) && (pt.y + GetSystemMetrics(SM_CYICON) < rectParent.bottom))
930         return pt;  /* The icon already has a suitable position */
931
932     xspacing = GetSystemMetrics(SM_CXICONSPACING);
933     yspacing = GetSystemMetrics(SM_CYICONSPACING);
934
935     list = WIN_ListChildren( wndPtr->parent );
936     y = rectParent.bottom;
937     for (;;)
938     {
939         x = rectParent.left;
940         do
941         {
942             /* Check if another icon already occupies this spot */
943             /* FIXME: this is completely inefficient */
944             if (list)
945             {
946                 int i;
947                 WND *childPtr;
948
949                 for (i = 0; list[i]; i++)
950                 {
951                     if (list[i] == wndPtr->hwndSelf) continue;
952                     if (!IsIconic( list[i] )) continue;
953                     if (!(childPtr = WIN_GetPtr( list[i] )) || childPtr == WND_OTHER_PROCESS)
954                         continue;
955                     if ((childPtr->rectWindow.left < x + xspacing) &&
956                         (childPtr->rectWindow.right >= x) &&
957                         (childPtr->rectWindow.top <= y) &&
958                         (childPtr->rectWindow.bottom > y - yspacing))
959                     {
960                         WIN_ReleasePtr( childPtr );
961                         break;  /* There's a window in there */
962                     }
963                     WIN_ReleasePtr( childPtr );
964                 }
965                 if (list[i])
966                 {
967                     /* found something here, try next spot */
968                     x += xspacing;
969                     continue;
970                 }
971             }
972
973             /* No window was found, so it's OK for us */
974             pt.x = x + (xspacing - GetSystemMetrics(SM_CXICON)) / 2;
975             pt.y = y - (yspacing + GetSystemMetrics(SM_CYICON)) / 2;
976             HeapFree( GetProcessHeap(), 0, list );
977             return pt;
978
979         } while(x <= rectParent.right-xspacing);
980         y -= yspacing;
981     }
982 }
983
984
985
986
987
988 UINT WINPOS_MinMaximize( HWND hwnd, UINT cmd, LPRECT rect )
989 {
990     WND *wndPtr;
991     UINT swpFlags = 0;
992     POINT size;
993     LONG old_style;
994     WINDOWPLACEMENT wpl;
995
996     TRACE("%p %u\n", hwnd, cmd );
997
998     wpl.length = sizeof(wpl);
999     GetWindowPlacement( hwnd, &wpl );
1000
1001     if (HOOK_CallHooks( WH_CBT, HCBT_MINMAX, (WPARAM)hwnd, cmd, TRUE ))
1002         return SWP_NOSIZE | SWP_NOMOVE;
1003
1004     if (IsIconic( hwnd ))
1005     {
1006         if (cmd == SW_MINIMIZE) return SWP_NOSIZE | SWP_NOMOVE;
1007         if (!SendMessageA( hwnd, WM_QUERYOPEN, 0, 0 )) return SWP_NOSIZE | SWP_NOMOVE;
1008         swpFlags |= SWP_NOCOPYBITS;
1009     }
1010
1011     if (!(wndPtr = WIN_FindWndPtr( hwnd ))) return 0;
1012
1013     size.x = wndPtr->rectWindow.left;
1014     size.y = wndPtr->rectWindow.top;
1015
1016     switch( cmd )
1017     {
1018     case SW_MINIMIZE:
1019         if( wndPtr->dwStyle & WS_MAXIMIZE) wndPtr->flags |= WIN_RESTORE_MAX;
1020         else wndPtr->flags &= ~WIN_RESTORE_MAX;
1021
1022         WIN_SetStyle( hwnd, WS_MINIMIZE, WS_MAXIMIZE );
1023
1024         X11DRV_set_iconic_state( hwnd );
1025
1026         wpl.ptMinPosition = WINPOS_FindIconPos( wndPtr, wpl.ptMinPosition );
1027
1028         SetRect( rect, wpl.ptMinPosition.x, wpl.ptMinPosition.y,
1029                  GetSystemMetrics(SM_CXICON), GetSystemMetrics(SM_CYICON) );
1030         swpFlags |= SWP_NOCOPYBITS;
1031         break;
1032
1033     case SW_MAXIMIZE:
1034         WINPOS_GetMinMaxInfo( hwnd, &size, &wpl.ptMaxPosition, NULL, NULL );
1035
1036         old_style = WIN_SetStyle( hwnd, WS_MAXIMIZE, WS_MINIMIZE );
1037         if (old_style & WS_MINIMIZE)
1038         {
1039             WINPOS_ShowIconTitle( hwnd, FALSE );
1040             X11DRV_set_iconic_state( hwnd );
1041         }
1042         SetRect( rect, wpl.ptMaxPosition.x, wpl.ptMaxPosition.y, size.x, size.y );
1043         break;
1044
1045     case SW_RESTORE:
1046         old_style = WIN_SetStyle( hwnd, 0, WS_MINIMIZE | WS_MAXIMIZE );
1047         if (old_style & WS_MINIMIZE)
1048         {
1049             WINPOS_ShowIconTitle( hwnd, FALSE );
1050             X11DRV_set_iconic_state( hwnd );
1051
1052             if( wndPtr->flags & WIN_RESTORE_MAX)
1053             {
1054                 /* Restore to maximized position */
1055                 WINPOS_GetMinMaxInfo( hwnd, &size, &wpl.ptMaxPosition, NULL, NULL);
1056                 WIN_SetStyle( hwnd, WS_MAXIMIZE, 0 );
1057                 SetRect( rect, wpl.ptMaxPosition.x, wpl.ptMaxPosition.y, size.x, size.y );
1058                 break;
1059             }
1060         }
1061         else if (!(old_style & WS_MAXIMIZE)) break;
1062
1063         /* Restore to normal position */
1064
1065         *rect = wpl.rcNormalPosition;
1066         rect->right -= rect->left;
1067         rect->bottom -= rect->top;
1068
1069         break;
1070     }
1071
1072     WIN_ReleaseWndPtr( wndPtr );
1073     return swpFlags;
1074 }
1075
1076
1077 /***********************************************************************
1078  *              ShowWindow   (X11DRV.@)
1079  */
1080 BOOL X11DRV_ShowWindow( HWND hwnd, INT cmd )
1081 {
1082     WND*        wndPtr = WIN_FindWndPtr( hwnd );
1083     BOOL        wasVisible, showFlag;
1084     RECT        newPos = {0, 0, 0, 0};
1085     UINT        swp = 0;
1086
1087     if (!wndPtr) return FALSE;
1088     hwnd = wndPtr->hwndSelf;  /* make it a full handle */
1089
1090     wasVisible = (wndPtr->dwStyle & WS_VISIBLE) != 0;
1091
1092     TRACE("hwnd=%p, cmd=%d, wasVisible %d\n", hwnd, cmd, wasVisible);
1093
1094     switch(cmd)
1095     {
1096         case SW_HIDE:
1097             if (!wasVisible) goto END;
1098             swp |= SWP_HIDEWINDOW | SWP_NOSIZE | SWP_NOMOVE | SWP_NOZORDER;
1099             break;
1100
1101         case SW_SHOWMINNOACTIVE:
1102             swp |= SWP_NOACTIVATE | SWP_NOZORDER;
1103             /* fall through */
1104         case SW_SHOWMINIMIZED:
1105         case SW_FORCEMINIMIZE: /* FIXME: Does not work if thread is hung. */
1106             swp |= SWP_SHOWWINDOW;
1107             /* fall through */
1108         case SW_MINIMIZE:
1109             swp |= SWP_FRAMECHANGED;
1110             if( !(wndPtr->dwStyle & WS_MINIMIZE) )
1111                  swp |= WINPOS_MinMaximize( hwnd, SW_MINIMIZE, &newPos );
1112             else swp |= SWP_NOSIZE | SWP_NOMOVE;
1113             break;
1114
1115         case SW_SHOWMAXIMIZED: /* same as SW_MAXIMIZE */
1116             swp |= SWP_SHOWWINDOW | SWP_FRAMECHANGED;
1117             if( !(wndPtr->dwStyle & WS_MAXIMIZE) )
1118                  swp |= WINPOS_MinMaximize( hwnd, SW_MAXIMIZE, &newPos );
1119             else swp |= SWP_NOSIZE | SWP_NOMOVE;
1120             break;
1121
1122         case SW_SHOWNA:
1123             swp |= SWP_NOACTIVATE | SWP_SHOWWINDOW | SWP_NOSIZE | SWP_NOMOVE;
1124             if( wndPtr->dwStyle & WS_CHILD) swp |= SWP_NOZORDER;
1125             break;
1126         case SW_SHOW:
1127             if (wasVisible) goto END;
1128
1129             swp |= SWP_SHOWWINDOW | SWP_NOSIZE | SWP_NOMOVE;
1130             break;
1131
1132         case SW_RESTORE:
1133             swp |= SWP_FRAMECHANGED;
1134             /* fall through */
1135         case SW_SHOWNOACTIVATE:
1136             swp |= SWP_NOACTIVATE | SWP_NOZORDER;
1137             /* fall through */
1138         case SW_SHOWNORMAL:  /* same as SW_NORMAL: */
1139         case SW_SHOWDEFAULT: /* FIXME: should have its own handler */
1140             swp |= SWP_SHOWWINDOW;
1141
1142             if( wndPtr->dwStyle & (WS_MINIMIZE | WS_MAXIMIZE) )
1143                  swp |= WINPOS_MinMaximize( hwnd, SW_RESTORE, &newPos );
1144             else swp |= SWP_NOSIZE | SWP_NOMOVE;
1145             break;
1146     }
1147
1148     showFlag = (cmd != SW_HIDE);
1149     if (showFlag != wasVisible || cmd == SW_SHOWNA)
1150     {
1151         SendMessageW( hwnd, WM_SHOWWINDOW, showFlag, 0 );
1152         if (!IsWindow( hwnd )) goto END;
1153     }
1154
1155     /* ShowWindow won't activate a not being maximized child window */
1156     if ((wndPtr->dwStyle & WS_CHILD) && cmd != SW_MAXIMIZE)
1157         swp |= SWP_NOACTIVATE | SWP_NOZORDER;
1158
1159     SetWindowPos( hwnd, HWND_TOP, newPos.left, newPos.top,
1160                   newPos.right, newPos.bottom, LOWORD(swp) );
1161     if (cmd == SW_HIDE)
1162     {
1163         HWND hFocus;
1164
1165         /* FIXME: This will cause the window to be activated irrespective
1166          * of whether it is owned by the same thread. Has to be done
1167          * asynchronously.
1168          */
1169
1170         if (hwnd == GetActiveWindow())
1171             WINPOS_ActivateOtherWindow(hwnd);
1172
1173         /* Revert focus to parent */
1174         hFocus = GetFocus();
1175         if (hwnd == hFocus || IsChild(hwnd, hFocus))
1176         {
1177             HWND parent = GetAncestor(hwnd, GA_PARENT);
1178             if (parent == GetDesktopWindow()) parent = 0;
1179             SetFocus(parent);
1180         }
1181     }
1182     if (!IsWindow( hwnd )) goto END;
1183     else if( wndPtr->dwStyle & WS_MINIMIZE ) WINPOS_ShowIconTitle( hwnd, TRUE );
1184
1185     if (wndPtr->flags & WIN_NEED_SIZE)
1186     {
1187         /* should happen only in CreateWindowEx() */
1188         int wParam = SIZE_RESTORED;
1189
1190         wndPtr->flags &= ~WIN_NEED_SIZE;
1191         if (wndPtr->dwStyle & WS_MAXIMIZE) wParam = SIZE_MAXIMIZED;
1192         else if (wndPtr->dwStyle & WS_MINIMIZE) wParam = SIZE_MINIMIZED;
1193         SendMessageA( hwnd, WM_SIZE, wParam,
1194                      MAKELONG(wndPtr->rectClient.right-wndPtr->rectClient.left,
1195                             wndPtr->rectClient.bottom-wndPtr->rectClient.top));
1196         SendMessageA( hwnd, WM_MOVE, 0,
1197                    MAKELONG(wndPtr->rectClient.left, wndPtr->rectClient.top) );
1198     }
1199
1200 END:
1201     WIN_ReleaseWndPtr(wndPtr);
1202     return wasVisible;
1203 }
1204
1205
1206 /**********************************************************************
1207  *              X11DRV_MapNotify
1208  */
1209 void X11DRV_MapNotify( HWND hwnd, XMapEvent *event )
1210 {
1211     struct x11drv_win_data *data;
1212     HWND hwndFocus = GetFocus();
1213     WND *win;
1214
1215     if (!(data = X11DRV_get_win_data( hwnd ))) return;
1216
1217     if (!(win = WIN_GetPtr( hwnd ))) return;
1218
1219     if (data->managed && (win->dwStyle & WS_VISIBLE) && (win->dwStyle & WS_MINIMIZE))
1220     {
1221         int x, y;
1222         unsigned int width, height, border, depth;
1223         Window root, top;
1224         RECT rect;
1225         LONG style = WS_VISIBLE;
1226
1227         /* FIXME: hack */
1228         wine_tsx11_lock();
1229         XGetGeometry( event->display, data->whole_window, &root, &x, &y, &width, &height,
1230                         &border, &depth );
1231         XTranslateCoordinates( event->display, data->whole_window, root, 0, 0, &x, &y, &top );
1232         wine_tsx11_unlock();
1233         rect.left   = x;
1234         rect.top    = y;
1235         rect.right  = x + width;
1236         rect.bottom = y + height;
1237         X11DRV_X_to_window_rect( data, &rect );
1238
1239         DCE_InvalidateDCE( hwnd, &data->window_rect );
1240
1241         if (win->flags & WIN_RESTORE_MAX) style |= WS_MAXIMIZE;
1242         WIN_SetStyle( hwnd, style, WS_MINIMIZE );
1243         WIN_ReleasePtr( win );
1244
1245         SendMessageA( hwnd, WM_SHOWWINDOW, SW_RESTORE, 0 );
1246         SetWindowPos( hwnd, 0, rect.left, rect.top, rect.right-rect.left, rect.bottom-rect.top,
1247                       SWP_NOZORDER | SWP_WINE_NOHOSTMOVE );
1248     }
1249     else WIN_ReleasePtr( win );
1250     if (hwndFocus && IsChild( hwnd, hwndFocus )) X11DRV_SetFocus(hwndFocus);  /* FIXME */
1251 }
1252
1253
1254 /**********************************************************************
1255  *              X11DRV_UnmapNotify
1256  */
1257 void X11DRV_UnmapNotify( HWND hwnd, XUnmapEvent *event )
1258 {
1259     struct x11drv_win_data *data;
1260     WND *win;
1261
1262     if (!(data = X11DRV_get_win_data( hwnd ))) return;
1263
1264     if (!(win = WIN_GetPtr( hwnd ))) return;
1265
1266     if ((win->dwStyle & WS_VISIBLE) && data->managed &&
1267         X11DRV_is_window_rect_mapped( &win->rectWindow ))
1268     {
1269         if (win->dwStyle & WS_MAXIMIZE)
1270             win->flags |= WIN_RESTORE_MAX;
1271         else
1272             win->flags &= ~WIN_RESTORE_MAX;
1273
1274         WIN_SetStyle( hwnd, WS_MINIMIZE, WS_MAXIMIZE );
1275         WIN_ReleasePtr( win );
1276
1277         EndMenu();
1278         SendMessageA( hwnd, WM_SHOWWINDOW, SW_MINIMIZE, 0 );
1279         SetWindowPos( hwnd, 0, 0, 0, GetSystemMetrics(SM_CXICON), GetSystemMetrics(SM_CYICON),
1280                       SWP_NOMOVE | SWP_NOACTIVATE | SWP_NOZORDER | SWP_WINE_NOHOSTMOVE );
1281     }
1282     else WIN_ReleasePtr( win );
1283 }
1284
1285
1286 /***********************************************************************
1287  *           query_zorder
1288  *
1289  * Synchronize internal z-order with the window manager's.
1290  */
1291 static Window __get_common_ancestor( Display *display, Window A, Window B,
1292                                      Window** children, unsigned* total )
1293 {
1294     /* find the real root window */
1295
1296     Window      root, *childrenB;
1297     unsigned    totalB;
1298
1299     wine_tsx11_lock();
1300     while( A != B && A && B )
1301     {
1302       XQueryTree( display, A, &root, &A, children, total );
1303       XQueryTree( display, B, &root, &B, &childrenB, &totalB );
1304       if( childrenB ) XFree( childrenB );
1305       if( *children ) XFree( *children ), *children = NULL;
1306     }
1307
1308     if( A && B )
1309     {
1310         XQueryTree( display, A, &root, &B, children, total );
1311         wine_tsx11_unlock();
1312         return A;
1313     }
1314     wine_tsx11_unlock();
1315     return 0 ;
1316 }
1317
1318 static Window __get_top_decoration( Display *display, Window w, Window ancestor )
1319 {
1320     Window*     children, root, prev = w, parent = w;
1321     unsigned    total;
1322
1323     wine_tsx11_lock();
1324     do
1325     {
1326         w = parent;
1327         XQueryTree( display, w, &root, &parent, &children, &total );
1328         if( children ) XFree( children );
1329     } while( parent && parent != ancestor );
1330     wine_tsx11_unlock();
1331     TRACE("\t%08x -> %08x\n", (unsigned)prev, (unsigned)w );
1332     return ( parent ) ? w : 0 ;
1333 }
1334
1335 static unsigned __td_lookup( Window w, Window* list, unsigned max )
1336 {
1337     unsigned    i;
1338     for( i = max; i > 0; i-- ) if( list[i - 1] == w ) break;
1339     return i;
1340 }
1341
1342 static HWND query_zorder( Display *display, HWND hWndCheck)
1343 {
1344     HWND      hwndInsertAfter = HWND_TOP;
1345     Window      w, parent, *children = NULL;
1346     unsigned    total, check, pos, best;
1347     HWND *list = WIN_ListChildren( GetDesktopWindow() );
1348     HWND hwndA = 0, hwndB = 0;
1349     int i;
1350
1351     /* find at least two managed windows */
1352     if (!list) return 0;
1353     for (i = 0; list[i]; i++)
1354     {
1355         if (!(GetWindowLongW( list[i], GWL_STYLE ) & WS_VISIBLE)) continue;
1356         if (!GetPropA( list[i], "__wine_x11_managed" )) continue;
1357         if (!hwndA) hwndA = list[i];
1358         else
1359         {
1360             hwndB = list[i];
1361             break;
1362         }
1363     }
1364     if (!hwndA || !hwndB) goto done;
1365
1366     parent = __get_common_ancestor( display, X11DRV_get_whole_window(hwndA),
1367                                     X11DRV_get_whole_window(hwndB), &children, &total );
1368     if( parent && children )
1369     {
1370         /* w is the ancestor if hWndCheck that is a direct descendant of 'parent' */
1371
1372         w = __get_top_decoration( display, X11DRV_get_whole_window(hWndCheck), parent );
1373
1374         if( w != children[total-1] ) /* check if at the top */
1375         {
1376             /* X child at index 0 is at the bottom, at index total-1 is at the top */
1377             check = __td_lookup( w, children, total );
1378             best = total;
1379
1380             /* go through all windows in Wine z-order... */
1381             for (i = 0; list[i]; i++)
1382             {
1383                 if (list[i] == hWndCheck) continue;
1384                 if (!GetPropA( list[i], "__wine_x11_managed" )) continue;
1385                 if (!(w = __get_top_decoration( display, X11DRV_get_whole_window(list[i]),
1386                                                 parent ))) continue;
1387                 pos = __td_lookup( w, children, total );
1388                 if( pos < best && pos > check )
1389                 {
1390                     /* find a nearest Wine window precedes hWndCheck in the real z-order */
1391                     best = pos;
1392                     hwndInsertAfter = list[i];
1393                 }
1394                 if( best - check == 1 ) break;
1395             }
1396         }
1397     }
1398     wine_tsx11_lock();
1399     if( children ) XFree( children );
1400     wine_tsx11_unlock();
1401
1402  done:
1403     HeapFree( GetProcessHeap(), 0, list );
1404     return hwndInsertAfter;
1405 }
1406
1407
1408 /***********************************************************************
1409  *              X11DRV_handle_desktop_resize
1410  */
1411 void X11DRV_handle_desktop_resize( unsigned int width, unsigned int height )
1412 {
1413     RECT rect;
1414     HWND hwnd = GetDesktopWindow();
1415
1416     screen_width  = width;
1417     screen_height = height;
1418     TRACE("desktop %p change to (%dx%d)\n", hwnd, width, height);
1419     SetRect( &rect, 0, 0, width, height );
1420     X11DRV_set_window_pos( hwnd, 0, &rect, &rect, SWP_NOZORDER|SWP_NOMOVE|SWP_WINE_NOHOSTMOVE, NULL );
1421     SendMessageTimeoutW( HWND_BROADCAST, WM_DISPLAYCHANGE, screen_depth,
1422                          MAKELPARAM( width, height ), SMTO_ABORTIFHUNG, 2000, NULL );
1423 }
1424
1425
1426 /***********************************************************************
1427  *              X11DRV_ConfigureNotify
1428  */
1429 void X11DRV_ConfigureNotify( HWND hwnd, XConfigureEvent *event )
1430 {
1431     HWND oldInsertAfter;
1432     struct x11drv_win_data *data;
1433     RECT rect;
1434     WINDOWPOS winpos;
1435     int x = event->x, y = event->y;
1436
1437     if (!(data = X11DRV_get_win_data( hwnd ))) return;
1438
1439     /* Get geometry */
1440
1441     if (!event->send_event)  /* normal event, need to map coordinates to the root */
1442     {
1443         Window child;
1444         wine_tsx11_lock();
1445         XTranslateCoordinates( event->display, data->whole_window, root_window,
1446                                0, 0, &x, &y, &child );
1447         wine_tsx11_unlock();
1448     }
1449     rect.left   = x;
1450     rect.top    = y;
1451     rect.right  = x + event->width;
1452     rect.bottom = y + event->height;
1453     TRACE( "win %p new X rect %ld,%ld,%ldx%ld (event %d,%d,%dx%d)\n",
1454            hwnd, rect.left, rect.top, rect.right-rect.left, rect.bottom-rect.top,
1455            event->x, event->y, event->width, event->height );
1456     X11DRV_X_to_window_rect( data, &rect );
1457
1458     winpos.hwnd  = hwnd;
1459     winpos.x     = rect.left;
1460     winpos.y     = rect.top;
1461     winpos.cx    = rect.right - rect.left;
1462     winpos.cy    = rect.bottom - rect.top;
1463     winpos.flags = SWP_NOACTIVATE;
1464
1465     /* Get Z-order (FIXME) */
1466
1467     winpos.hwndInsertAfter = query_zorder( event->display, hwnd );
1468
1469     /* needs to find the first Visible Window above the current one */
1470     oldInsertAfter = hwnd;
1471     for (;;)
1472     {
1473         oldInsertAfter = GetWindow( oldInsertAfter, GW_HWNDPREV );
1474         if (!oldInsertAfter)
1475         {
1476             oldInsertAfter = HWND_TOP;
1477             break;
1478         }
1479         if (GetWindowLongA( oldInsertAfter, GWL_STYLE ) & WS_VISIBLE) break;
1480     }
1481
1482     /* Compare what has changed */
1483
1484     GetWindowRect( hwnd, &rect );
1485     if (rect.left == winpos.x && rect.top == winpos.y) winpos.flags |= SWP_NOMOVE;
1486     else
1487         TRACE( "%p moving from (%ld,%ld) to (%d,%d)\n",
1488                hwnd, rect.left, rect.top, winpos.x, winpos.y );
1489
1490     if ((rect.right - rect.left == winpos.cx && rect.bottom - rect.top == winpos.cy) ||
1491         IsIconic(hwnd) ||
1492         (IsRectEmpty( &rect ) && winpos.cx == 1 && winpos.cy == 1))
1493         winpos.flags |= SWP_NOSIZE;
1494     else
1495         TRACE( "%p resizing from (%ldx%ld) to (%dx%d)\n",
1496                hwnd, rect.right - rect.left, rect.bottom - rect.top,
1497                winpos.cx, winpos.cy );
1498
1499     if (winpos.hwndInsertAfter == oldInsertAfter) winpos.flags |= SWP_NOZORDER;
1500     else
1501         TRACE( "%p restacking from after %p to after %p\n",
1502                hwnd, oldInsertAfter, winpos.hwndInsertAfter );
1503
1504     /* if nothing changed, don't do anything */
1505     if (winpos.flags == (SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE)) return;
1506
1507     SetWindowPos( hwnd, winpos.hwndInsertAfter, winpos.x, winpos.y,
1508                   winpos.cx, winpos.cy, winpos.flags | SWP_WINE_NOHOSTMOVE );
1509 }
1510
1511
1512 /***********************************************************************
1513  *              SetWindowRgn  (X11DRV.@)
1514  *
1515  * Assign specified region to window (for non-rectangular windows)
1516  */
1517 int X11DRV_SetWindowRgn( HWND hwnd, HRGN hrgn, BOOL redraw )
1518 {
1519     struct x11drv_win_data *data;
1520
1521     if (!(data = X11DRV_get_win_data( hwnd )))
1522     {
1523         if (IsWindow( hwnd ))
1524             FIXME( "not supported on other process window %p\n", hwnd );
1525         SetLastError( ERROR_INVALID_WINDOW_HANDLE );
1526         return FALSE;
1527     }
1528
1529 #ifdef HAVE_LIBXSHAPE
1530     if (data->whole_window)
1531     {
1532         Display *display = thread_display();
1533
1534         if (!hrgn)
1535         {
1536             wine_tsx11_lock();
1537             XShapeCombineMask( display, data->whole_window,
1538                                ShapeBounding, 0, 0, None, ShapeSet );
1539             wine_tsx11_unlock();
1540         }
1541         else
1542         {
1543             RGNDATA *pRegionData = X11DRV_GetRegionData( hrgn, 0 );
1544             if (pRegionData)
1545             {
1546                 wine_tsx11_lock();
1547                 XShapeCombineRectangles( display, data->whole_window, ShapeBounding,
1548                                          data->window_rect.left - data->whole_rect.left,
1549                                          data->window_rect.top - data->whole_rect.top,
1550                                          (XRectangle *)pRegionData->Buffer,
1551                                          pRegionData->rdh.nCount,
1552                                          ShapeSet, YXBanded );
1553                 wine_tsx11_unlock();
1554                 HeapFree(GetProcessHeap(), 0, pRegionData);
1555             }
1556         }
1557     }
1558 #endif  /* HAVE_LIBXSHAPE */
1559
1560     return TRUE;
1561 }
1562
1563
1564 /***********************************************************************
1565  *           draw_moving_frame
1566  *
1567  * Draw the frame used when moving or resizing window.
1568  *
1569  * FIXME:  This causes problems in Win95 mode.  (why?)
1570  */
1571 static void draw_moving_frame( HDC hdc, RECT *rect, BOOL thickframe )
1572 {
1573     if (thickframe)
1574     {
1575         const int width = GetSystemMetrics(SM_CXFRAME);
1576         const int height = GetSystemMetrics(SM_CYFRAME);
1577
1578         HBRUSH hbrush = SelectObject( hdc, GetStockObject( GRAY_BRUSH ) );
1579         PatBlt( hdc, rect->left, rect->top,
1580                 rect->right - rect->left - width, height, PATINVERT );
1581         PatBlt( hdc, rect->left, rect->top + height, width,
1582                 rect->bottom - rect->top - height, PATINVERT );
1583         PatBlt( hdc, rect->left + width, rect->bottom - 1,
1584                 rect->right - rect->left - width, -height, PATINVERT );
1585         PatBlt( hdc, rect->right - 1, rect->top, -width,
1586                 rect->bottom - rect->top - height, PATINVERT );
1587         SelectObject( hdc, hbrush );
1588     }
1589     else DrawFocusRect( hdc, rect );
1590 }
1591
1592
1593 /***********************************************************************
1594  *           start_size_move
1595  *
1596  * Initialisation of a move or resize, when initiatied from a menu choice.
1597  * Return hit test code for caption or sizing border.
1598  */
1599 static LONG start_size_move( HWND hwnd, WPARAM wParam, POINT *capturePoint, LONG style )
1600 {
1601     LONG hittest = 0;
1602     POINT pt;
1603     MSG msg;
1604     RECT rectWindow;
1605
1606     GetWindowRect( hwnd, &rectWindow );
1607
1608     if ((wParam & 0xfff0) == SC_MOVE)
1609     {
1610         /* Move pointer at the center of the caption */
1611         RECT rect = rectWindow;
1612         /* Note: to be exactly centered we should take the different types
1613          * of border into account, but it shouldn't make more that a few pixels
1614          * of difference so let's not bother with that */
1615         rect.top += GetSystemMetrics(SM_CYBORDER);
1616         if (style & WS_SYSMENU)
1617             rect.left += GetSystemMetrics(SM_CXSIZE) + 1;
1618         if (style & WS_MINIMIZEBOX)
1619             rect.right -= GetSystemMetrics(SM_CXSIZE) + 1;
1620         if (style & WS_MAXIMIZEBOX)
1621             rect.right -= GetSystemMetrics(SM_CXSIZE) + 1;
1622         pt.x = (rect.right + rect.left) / 2;
1623         pt.y = rect.top + GetSystemMetrics(SM_CYSIZE)/2;
1624         hittest = HTCAPTION;
1625         *capturePoint = pt;
1626     }
1627     else  /* SC_SIZE */
1628     {
1629         while(!hittest)
1630         {
1631             GetMessageW( &msg, 0, WM_KEYFIRST, WM_MOUSELAST );
1632             if (CallMsgFilterW( &msg, MSGF_SIZE )) continue;
1633
1634             switch(msg.message)
1635             {
1636             case WM_MOUSEMOVE:
1637                 pt = msg.pt;
1638                 hittest = SendMessageW( hwnd, WM_NCHITTEST, 0, MAKELONG( pt.x, pt.y ) );
1639                 if ((hittest < HTLEFT) || (hittest > HTBOTTOMRIGHT)) hittest = 0;
1640                 break;
1641
1642             case WM_LBUTTONUP:
1643                 return 0;
1644
1645             case WM_KEYDOWN:
1646                 switch(msg.wParam)
1647                 {
1648                 case VK_UP:
1649                     hittest = HTTOP;
1650                     pt.x =(rectWindow.left+rectWindow.right)/2;
1651                     pt.y = rectWindow.top + GetSystemMetrics(SM_CYFRAME) / 2;
1652                     break;
1653                 case VK_DOWN:
1654                     hittest = HTBOTTOM;
1655                     pt.x =(rectWindow.left+rectWindow.right)/2;
1656                     pt.y = rectWindow.bottom - GetSystemMetrics(SM_CYFRAME) / 2;
1657                     break;
1658                 case VK_LEFT:
1659                     hittest = HTLEFT;
1660                     pt.x = rectWindow.left + GetSystemMetrics(SM_CXFRAME) / 2;
1661                     pt.y =(rectWindow.top+rectWindow.bottom)/2;
1662                     break;
1663                 case VK_RIGHT:
1664                     hittest = HTRIGHT;
1665                     pt.x = rectWindow.right - GetSystemMetrics(SM_CXFRAME) / 2;
1666                     pt.y =(rectWindow.top+rectWindow.bottom)/2;
1667                     break;
1668                 case VK_RETURN:
1669                 case VK_ESCAPE: return 0;
1670                 }
1671             }
1672         }
1673         *capturePoint = pt;
1674     }
1675     SetCursorPos( pt.x, pt.y );
1676     SendMessageW( hwnd, WM_SETCURSOR, (WPARAM)hwnd, MAKELONG( hittest, WM_MOUSEMOVE ));
1677     return hittest;
1678 }
1679
1680
1681 /***********************************************************************
1682  *           set_movesize_capture
1683  */
1684 static void set_movesize_capture( HWND hwnd )
1685 {
1686     HWND previous = 0;
1687
1688     SERVER_START_REQ( set_capture_window )
1689     {
1690         req->handle = hwnd;
1691         req->flags  = CAPTURE_MOVESIZE;
1692         if (!wine_server_call_err( req ))
1693         {
1694             previous = reply->previous;
1695             hwnd = reply->full_handle;
1696         }
1697     }
1698     SERVER_END_REQ;
1699     if (previous && previous != hwnd)
1700         SendMessageW( previous, WM_CAPTURECHANGED, 0, (LPARAM)hwnd );
1701 }
1702
1703 /***********************************************************************
1704  *           X11DRV_WMMoveResizeWindow
1705  *
1706  * Tells the window manager to initiate a move or resize operation.
1707  *
1708  * SEE
1709  *  http://freedesktop.org/Standards/wm-spec/1.3/ar01s04.html
1710  *  or search for "_NET_WM_MOVERESIZE"
1711  */
1712 static void X11DRV_WMMoveResizeWindow( HWND hwnd, int x, int y, int dir )
1713 {
1714     XEvent xev;
1715     Display *display = thread_display();
1716
1717     xev.xclient.type = ClientMessage;
1718     xev.xclient.window = X11DRV_get_whole_window(hwnd);
1719     xev.xclient.message_type = x11drv_atom(_NET_WM_MOVERESIZE);
1720     xev.xclient.serial = 0;
1721     xev.xclient.display = display;
1722     xev.xclient.send_event = True;
1723     xev.xclient.format = 32;
1724     xev.xclient.data.l[0] = x; /* x coord */
1725     xev.xclient.data.l[1] = y; /* y coord */
1726     xev.xclient.data.l[2] = dir; /* direction */
1727     xev.xclient.data.l[3] = 1; /* button */
1728     xev.xclient.data.l[4] = 0; /* unused */
1729
1730     /* need to ungrab the pointer that may have been automatically grabbed
1731      * with a ButtonPress event */
1732     wine_tsx11_lock();
1733     XUngrabPointer( display, CurrentTime );
1734     XSendEvent(display, root_window, False, SubstructureNotifyMask, &xev);
1735     wine_tsx11_unlock();
1736 }
1737
1738 /***********************************************************************
1739  *           SysCommandSizeMove   (X11DRV.@)
1740  *
1741  * Perform SC_MOVE and SC_SIZE commands.
1742  */
1743 void X11DRV_SysCommandSizeMove( HWND hwnd, WPARAM wParam )
1744 {
1745     MSG msg;
1746     RECT sizingRect, mouseRect, origRect;
1747     HDC hdc;
1748     HWND parent;
1749     LONG hittest = (LONG)(wParam & 0x0f);
1750     WPARAM syscommand = wParam & 0xfff0;
1751     HCURSOR hDragCursor = 0, hOldCursor = 0;
1752     POINT minTrack, maxTrack;
1753     POINT capturePoint, pt;
1754     LONG style = GetWindowLongA( hwnd, GWL_STYLE );
1755     BOOL    thickframe = HAS_THICKFRAME( style );
1756     BOOL    iconic = style & WS_MINIMIZE;
1757     BOOL    moved = FALSE;
1758     DWORD     dwPoint = GetMessagePos ();
1759     BOOL DragFullWindows = FALSE;
1760     BOOL grab;
1761     Window parent_win, whole_win;
1762     Display *old_gdi_display = NULL;
1763     Display *display = thread_display();
1764     struct x11drv_win_data *data;
1765
1766     pt.x = (short)LOWORD(dwPoint);
1767     pt.y = (short)HIWORD(dwPoint);
1768     capturePoint = pt;
1769
1770     if (IsZoomed(hwnd) || !IsWindowVisible(hwnd)) return;
1771
1772     if (!(data = X11DRV_get_win_data( hwnd ))) return;
1773
1774     /* if we are managed then we let the WM do all the work */
1775     if (data->managed)
1776     {
1777         int dir;
1778         if (syscommand == SC_MOVE)
1779         {
1780             if (!hittest) dir = _NET_WM_MOVERESIZE_MOVE_KEYBOARD;
1781             else dir = _NET_WM_MOVERESIZE_MOVE;
1782         }
1783         else if (!hittest) dir = _NET_WM_MOVERESIZE_SIZE_KEYBOARD;
1784         else
1785             switch (hittest)
1786             {
1787             case WMSZ_LEFT:        dir = _NET_WM_MOVERESIZE_SIZE_LEFT; break;
1788             case WMSZ_RIGHT:       dir = _NET_WM_MOVERESIZE_SIZE_RIGHT; break;
1789             case WMSZ_TOP:         dir = _NET_WM_MOVERESIZE_SIZE_TOP; break;
1790             case WMSZ_TOPLEFT:     dir = _NET_WM_MOVERESIZE_SIZE_TOPLEFT; break;
1791             case WMSZ_TOPRIGHT:    dir = _NET_WM_MOVERESIZE_SIZE_TOPRIGHT; break;
1792             case WMSZ_BOTTOM:      dir = _NET_WM_MOVERESIZE_SIZE_BOTTOM; break;
1793             case WMSZ_BOTTOMLEFT:  dir = _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT; break;
1794             case WMSZ_BOTTOMRIGHT: dir = _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT; break;
1795             default:
1796                 ERR("Invalid hittest value: %ld\n", hittest);
1797                 dir = _NET_WM_MOVERESIZE_SIZE_KEYBOARD;
1798             }
1799         X11DRV_WMMoveResizeWindow( hwnd, pt.x, pt.y, dir );
1800         return;
1801     }
1802
1803     SystemParametersInfoA(SPI_GETDRAGFULLWINDOWS, 0, &DragFullWindows, 0);
1804
1805     if (syscommand == SC_MOVE)
1806     {
1807         if (!hittest) hittest = start_size_move( hwnd, wParam, &capturePoint, style );
1808         if (!hittest) return;
1809     }
1810     else  /* SC_SIZE */
1811     {
1812         if ( hittest && (syscommand != SC_MOUSEMENU) )
1813             hittest += (HTLEFT - WMSZ_LEFT);
1814         else
1815         {
1816             set_movesize_capture( hwnd );
1817             hittest = start_size_move( hwnd, wParam, &capturePoint, style );
1818             if (!hittest)
1819             {
1820                 set_movesize_capture(0);
1821                 return;
1822             }
1823         }
1824     }
1825
1826       /* Get min/max info */
1827
1828     WINPOS_GetMinMaxInfo( hwnd, NULL, NULL, &minTrack, &maxTrack );
1829     GetWindowRect( hwnd, &sizingRect );
1830     if (style & WS_CHILD)
1831     {
1832         parent = GetParent(hwnd);
1833         /* make sizing rect relative to parent */
1834         MapWindowPoints( 0, parent, (POINT*)&sizingRect, 2 );
1835         GetClientRect( parent, &mouseRect );
1836     }
1837     else
1838     {
1839         parent = 0;
1840         SetRect(&mouseRect, 0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN));
1841     }
1842     origRect = sizingRect;
1843
1844     if (ON_LEFT_BORDER(hittest))
1845     {
1846         mouseRect.left  = max( mouseRect.left, sizingRect.right-maxTrack.x );
1847         mouseRect.right = min( mouseRect.right, sizingRect.right-minTrack.x );
1848     }
1849     else if (ON_RIGHT_BORDER(hittest))
1850     {
1851         mouseRect.left  = max( mouseRect.left, sizingRect.left+minTrack.x );
1852         mouseRect.right = min( mouseRect.right, sizingRect.left+maxTrack.x );
1853     }
1854     if (ON_TOP_BORDER(hittest))
1855     {
1856         mouseRect.top    = max( mouseRect.top, sizingRect.bottom-maxTrack.y );
1857         mouseRect.bottom = min( mouseRect.bottom,sizingRect.bottom-minTrack.y);
1858     }
1859     else if (ON_BOTTOM_BORDER(hittest))
1860     {
1861         mouseRect.top    = max( mouseRect.top, sizingRect.top+minTrack.y );
1862         mouseRect.bottom = min( mouseRect.bottom, sizingRect.top+maxTrack.y );
1863     }
1864     if (parent) MapWindowPoints( parent, 0, (LPPOINT)&mouseRect, 2 );
1865
1866     /* Retrieve a default cache DC (without using the window style) */
1867     hdc = GetDCEx( parent, 0, DCX_CACHE );
1868
1869     if( iconic ) /* create a cursor for dragging */
1870     {
1871         hDragCursor = (HCURSOR)GetClassLongA( hwnd, GCL_HICON);
1872         if( !hDragCursor ) hDragCursor = (HCURSOR)SendMessageA( hwnd, WM_QUERYDRAGICON, 0, 0L);
1873         if( !hDragCursor ) iconic = FALSE;
1874     }
1875
1876     /* repaint the window before moving it around */
1877     RedrawWindow( hwnd, NULL, 0, RDW_UPDATENOW | RDW_ALLCHILDREN );
1878
1879     SendMessageA( hwnd, WM_ENTERSIZEMOVE, 0, 0 );
1880     set_movesize_capture( hwnd );
1881
1882     /* grab the server only when moving top-level windows without desktop */
1883     grab = (!DragFullWindows && !parent && (root_window == DefaultRootWindow(gdi_display)));
1884
1885     if (grab)
1886     {
1887         wine_tsx11_lock();
1888         XSync( gdi_display, False );
1889         XGrabServer( display );
1890         XSync( display, False );
1891         /* switch gdi display to the thread display, since the server is grabbed */
1892         old_gdi_display = gdi_display;
1893         gdi_display = display;
1894         wine_tsx11_unlock();
1895     }
1896     whole_win = X11DRV_get_whole_window( GetAncestor(hwnd,GA_ROOT) );
1897     parent_win = parent ? X11DRV_get_whole_window( GetAncestor(parent,GA_ROOT) ) : root_window;
1898
1899     wine_tsx11_lock();
1900     XGrabPointer( display, whole_win, False,
1901                   PointerMotionMask | ButtonPressMask | ButtonReleaseMask,
1902                   GrabModeAsync, GrabModeAsync, parent_win, None, CurrentTime );
1903     wine_tsx11_unlock();
1904
1905     while(1)
1906     {
1907         int dx = 0, dy = 0;
1908
1909         if (!GetMessageW( &msg, 0, WM_KEYFIRST, WM_MOUSELAST )) break;
1910         if (CallMsgFilterW( &msg, MSGF_SIZE )) continue;
1911
1912         /* Exit on button-up, Return, or Esc */
1913         if ((msg.message == WM_LBUTTONUP) ||
1914             ((msg.message == WM_KEYDOWN) &&
1915              ((msg.wParam == VK_RETURN) || (msg.wParam == VK_ESCAPE)))) break;
1916
1917         if ((msg.message != WM_KEYDOWN) && (msg.message != WM_MOUSEMOVE))
1918             continue;  /* We are not interested in other messages */
1919
1920         pt = msg.pt;
1921
1922         if (msg.message == WM_KEYDOWN) switch(msg.wParam)
1923         {
1924         case VK_UP:    pt.y -= 8; break;
1925         case VK_DOWN:  pt.y += 8; break;
1926         case VK_LEFT:  pt.x -= 8; break;
1927         case VK_RIGHT: pt.x += 8; break;
1928         }
1929
1930         pt.x = max( pt.x, mouseRect.left );
1931         pt.x = min( pt.x, mouseRect.right );
1932         pt.y = max( pt.y, mouseRect.top );
1933         pt.y = min( pt.y, mouseRect.bottom );
1934
1935         dx = pt.x - capturePoint.x;
1936         dy = pt.y - capturePoint.y;
1937
1938         if (dx || dy)
1939         {
1940             if( !moved )
1941             {
1942                 moved = TRUE;
1943
1944                 if( iconic ) /* ok, no system popup tracking */
1945                 {
1946                     hOldCursor = SetCursor(hDragCursor);
1947                     ShowCursor( TRUE );
1948                     WINPOS_ShowIconTitle( hwnd, FALSE );
1949                 }
1950                 else if(!DragFullWindows)
1951                     draw_moving_frame( hdc, &sizingRect, thickframe );
1952             }
1953
1954             if (msg.message == WM_KEYDOWN) SetCursorPos( pt.x, pt.y );
1955             else
1956             {
1957                 RECT newRect = sizingRect;
1958                 WPARAM wpSizingHit = 0;
1959
1960                 if (hittest == HTCAPTION) OffsetRect( &newRect, dx, dy );
1961                 if (ON_LEFT_BORDER(hittest)) newRect.left += dx;
1962                 else if (ON_RIGHT_BORDER(hittest)) newRect.right += dx;
1963                 if (ON_TOP_BORDER(hittest)) newRect.top += dy;
1964                 else if (ON_BOTTOM_BORDER(hittest)) newRect.bottom += dy;
1965                 if(!iconic && !DragFullWindows) draw_moving_frame( hdc, &sizingRect, thickframe );
1966                 capturePoint = pt;
1967
1968                 /* determine the hit location */
1969                 if (hittest >= HTLEFT && hittest <= HTBOTTOMRIGHT)
1970                     wpSizingHit = WMSZ_LEFT + (hittest - HTLEFT);
1971                 SendMessageA( hwnd, WM_SIZING, wpSizingHit, (LPARAM)&newRect );
1972
1973                 if (!iconic)
1974                 {
1975                     if(!DragFullWindows)
1976                         draw_moving_frame( hdc, &newRect, thickframe );
1977                     else
1978                         SetWindowPos( hwnd, 0, newRect.left, newRect.top,
1979                                       newRect.right - newRect.left,
1980                                       newRect.bottom - newRect.top,
1981                                       ( hittest == HTCAPTION ) ? SWP_NOSIZE : 0 );
1982                 }
1983                 sizingRect = newRect;
1984             }
1985         }
1986     }
1987
1988     set_movesize_capture(0);
1989     if( iconic )
1990     {
1991         if( moved ) /* restore cursors, show icon title later on */
1992         {
1993             ShowCursor( FALSE );
1994             SetCursor( hOldCursor );
1995         }
1996     }
1997     else if (moved && !DragFullWindows)
1998         draw_moving_frame( hdc, &sizingRect, thickframe );
1999
2000     ReleaseDC( parent, hdc );
2001
2002     wine_tsx11_lock();
2003     XUngrabPointer( display, CurrentTime );
2004     if (grab)
2005     {
2006         XSync( display, False );
2007         XUngrabServer( display );
2008         XSync( display, False );
2009         gdi_display = old_gdi_display;
2010     }
2011     wine_tsx11_unlock();
2012
2013     if (HOOK_CallHooks( WH_CBT, HCBT_MOVESIZE, (WPARAM)hwnd, (LPARAM)&sizingRect, TRUE ))
2014         moved = FALSE;
2015
2016     SendMessageA( hwnd, WM_EXITSIZEMOVE, 0, 0 );
2017     SendMessageA( hwnd, WM_SETVISIBLE, !IsIconic(hwnd), 0L);
2018
2019     /* window moved or resized */
2020     if (moved)
2021     {
2022         /* if the moving/resizing isn't canceled call SetWindowPos
2023          * with the new position or the new size of the window
2024          */
2025         if (!((msg.message == WM_KEYDOWN) && (msg.wParam == VK_ESCAPE)) )
2026         {
2027             /* NOTE: SWP_NOACTIVATE prevents document window activation in Word 6 */
2028             if(!DragFullWindows)
2029                 SetWindowPos( hwnd, 0, sizingRect.left, sizingRect.top,
2030                               sizingRect.right - sizingRect.left,
2031                               sizingRect.bottom - sizingRect.top,
2032                               ( hittest == HTCAPTION ) ? SWP_NOSIZE : 0 );
2033         }
2034         else
2035         { /* restore previous size/position */
2036             if(DragFullWindows)
2037                 SetWindowPos( hwnd, 0, origRect.left, origRect.top,
2038                               origRect.right - origRect.left,
2039                               origRect.bottom - origRect.top,
2040                               ( hittest == HTCAPTION ) ? SWP_NOSIZE : 0 );
2041         }
2042     }
2043
2044     if (IsIconic(hwnd))
2045     {
2046         /* Single click brings up the system menu when iconized */
2047
2048         if( !moved )
2049         {
2050             if(style & WS_SYSMENU )
2051                 SendMessageA( hwnd, WM_SYSCOMMAND,
2052                               SC_MOUSEMENU + HTSYSMENU, MAKELONG(pt.x,pt.y));
2053         }
2054         else WINPOS_ShowIconTitle( hwnd, TRUE );
2055     }
2056 }