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