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