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