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