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