Do nothing in ShowWindow(SW_SHOW) if a window is already visible.
[wine/multimedia.git] / dlls / x11drv / winpos.c
blob947e2ccbc83bfad921b4f23ab1bf4f2e3fd17824
1 /*
2 * Window position related functions.
4 * Copyright 1993, 1994, 1995, 2001 Alexandre Julliard
5 * Copyright 1995, 1996, 1999 Alex Korobka
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.
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.
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
22 #include "config.h"
24 #include <X11/Xlib.h>
25 #ifdef HAVE_LIBXSHAPE
26 #include <X11/IntrinsicP.h>
27 #include <X11/extensions/shape.h>
28 #endif /* HAVE_LIBXSHAPE */
29 #include <stdarg.h>
31 #include "windef.h"
32 #include "winbase.h"
33 #include "wingdi.h"
34 #include "winuser.h"
35 #include "winerror.h"
36 #include "wownt32.h"
37 #include "wine/wingdi16.h"
39 #include "x11drv.h"
40 #include "win.h"
41 #include "winpos.h"
42 #include "dce.h"
43 #include "cursoricon.h"
44 #include "nonclient.h"
46 #include "wine/server.h"
47 #include "wine/debug.h"
49 WINE_DEFAULT_DEBUG_CHANNEL(x11drv);
51 #define SWP_AGG_NOGEOMETRYCHANGE \
52 (SWP_NOSIZE | SWP_NOMOVE | SWP_NOCLIENTSIZE | SWP_NOCLIENTMOVE)
53 #define SWP_AGG_NOPOSCHANGE \
54 (SWP_AGG_NOGEOMETRYCHANGE | SWP_NOZORDER)
55 #define SWP_AGG_STATUSFLAGS \
56 (SWP_AGG_NOPOSCHANGE | SWP_FRAMECHANGED | SWP_HIDEWINDOW | SWP_SHOWWINDOW)
58 #define SWP_EX_NOCOPY 0x0001
59 #define SWP_EX_PAINTSELF 0x0002
60 #define SWP_EX_NONCLIENT 0x0004
62 #define HAS_THICKFRAME(style,exStyle) \
63 (((style) & WS_THICKFRAME) && \
64 !(((style) & (WS_DLGFRAME|WS_BORDER)) == WS_DLGFRAME))
66 #define ON_LEFT_BORDER(hit) \
67 (((hit) == HTLEFT) || ((hit) == HTTOPLEFT) || ((hit) == HTBOTTOMLEFT))
68 #define ON_RIGHT_BORDER(hit) \
69 (((hit) == HTRIGHT) || ((hit) == HTTOPRIGHT) || ((hit) == HTBOTTOMRIGHT))
70 #define ON_TOP_BORDER(hit) \
71 (((hit) == HTTOP) || ((hit) == HTTOPLEFT) || ((hit) == HTTOPRIGHT))
72 #define ON_BOTTOM_BORDER(hit) \
73 (((hit) == HTBOTTOM) || ((hit) == HTBOTTOMLEFT) || ((hit) == HTBOTTOMRIGHT))
76 /***********************************************************************
77 * clip_children
79 * Clip all children of a given window out of the visible region
81 static int clip_children( HWND parent, HWND last, HRGN hrgn, int whole_window )
83 HWND *list;
84 WND *ptr;
85 HRGN rectRgn;
86 int i, x, y, ret = SIMPLEREGION;
88 /* first check if we have anything to do */
89 if (!(list = WIN_ListChildren( parent ))) return ret;
91 if (whole_window)
93 WND *win = WIN_FindWndPtr( parent );
94 x = win->rectWindow.left - win->rectClient.left;
95 y = win->rectWindow.top - win->rectClient.top;
96 WIN_ReleaseWndPtr( win );
98 else x = y = 0;
100 rectRgn = CreateRectRgn( 0, 0, 0, 0 );
102 for (i = 0; list[i] && list[i] != last; i++)
104 if (!(ptr = WIN_FindWndPtr( list[i] ))) continue;
105 if ((ptr->dwStyle & WS_VISIBLE) && !(ptr->dwExStyle & WS_EX_TRANSPARENT))
107 SetRectRgn( rectRgn, ptr->rectWindow.left + x, ptr->rectWindow.top + y,
108 ptr->rectWindow.right + x, ptr->rectWindow.bottom + y );
109 if ((ret = CombineRgn( hrgn, hrgn, rectRgn, RGN_DIFF )) == NULLREGION)
111 WIN_ReleaseWndPtr( ptr );
112 break; /* no need to go on, region is empty */
115 WIN_ReleaseWndPtr( ptr );
117 DeleteObject( rectRgn );
118 HeapFree( GetProcessHeap(), 0, list );
119 return ret;
123 /***********************************************************************
124 * get_visible_region
126 * Compute the visible region of a window
128 static HRGN get_visible_region( WND *win, HWND top, UINT flags, int mode )
130 HRGN rgn;
131 RECT rect;
132 int xoffset, yoffset;
133 X11DRV_WND_DATA *data = win->pDriverData;
135 if (flags & DCX_WINDOW)
137 xoffset = win->rectWindow.left;
138 yoffset = win->rectWindow.top;
140 else
142 xoffset = win->rectClient.left;
143 yoffset = win->rectClient.top;
146 if (flags & DCX_PARENTCLIP)
147 GetClientRect( win->parent, &rect );
148 else if (flags & DCX_WINDOW)
149 rect = data->whole_rect;
150 else
151 rect = win->rectClient;
153 /* vis region is relative to the start of the client/window area */
154 OffsetRect( &rect, -xoffset, -yoffset );
156 if (!(rgn = CreateRectRgn( rect.left, rect.top, rect.right, rect.bottom ))) return 0;
158 if ((flags & DCX_CLIPCHILDREN) && (mode != ClipByChildren))
160 /* we need to clip children by hand */
161 if (clip_children( win->hwndSelf, 0, rgn, (flags & DCX_WINDOW) ) == NULLREGION) return rgn;
164 if (top && top != win->hwndSelf) /* need to clip siblings of ancestors */
166 WND *parent, *ptr = WIN_FindWndPtr( win->hwndSelf );
167 HRGN tmp = 0;
169 OffsetRgn( rgn, xoffset, yoffset );
170 for (;;)
172 if (ptr->dwStyle & WS_CLIPSIBLINGS)
174 if (clip_children( ptr->parent, ptr->hwndSelf, rgn, FALSE ) == NULLREGION) break;
176 if (ptr->hwndSelf == top) break;
177 if (!(parent = WIN_FindWndPtr( ptr->parent ))) break;
178 WIN_ReleaseWndPtr( ptr );
179 ptr = parent;
180 /* clip to parent client area */
181 if (tmp) SetRectRgn( tmp, 0, 0, ptr->rectClient.right - ptr->rectClient.left,
182 ptr->rectClient.bottom - ptr->rectClient.top );
183 else tmp = CreateRectRgn( 0, 0, ptr->rectClient.right - ptr->rectClient.left,
184 ptr->rectClient.bottom - ptr->rectClient.top );
185 CombineRgn( rgn, rgn, tmp, RGN_AND );
186 OffsetRgn( rgn, ptr->rectClient.left, ptr->rectClient.top );
187 xoffset += ptr->rectClient.left;
188 yoffset += ptr->rectClient.top;
190 WIN_ReleaseWndPtr( ptr );
191 /* make it relative to the target window again */
192 OffsetRgn( rgn, -xoffset, -yoffset );
193 if (tmp) DeleteObject( tmp );
196 return rgn;
200 /***********************************************************************
201 * get_covered_region
203 * Compute the portion of 'rgn' that is covered by non-clipped siblings.
204 * This is the area that is covered from X point of view, but may still need
205 * to be exposed.
206 * 'rgn' must be relative to the client area of the parent of 'win'.
208 static int get_covered_region( WND *win, HRGN rgn )
210 HRGN tmp;
211 int ret;
212 WND *parent, *ptr = WIN_FindWndPtr( win->hwndSelf );
213 int xoffset = 0, yoffset = 0;
215 tmp = CreateRectRgn( 0, 0, 0, 0 );
216 CombineRgn( tmp, rgn, 0, RGN_COPY );
218 /* to make things easier we actually build the uncovered
219 * area by removing all siblings and then we subtract that
220 * from the total region to get the covered area */
221 for (;;)
223 if (!(ptr->dwStyle & WS_CLIPSIBLINGS))
225 if (clip_children( ptr->parent, ptr->hwndSelf, tmp, FALSE ) == NULLREGION) break;
227 if (!(parent = WIN_FindWndPtr( ptr->parent ))) break;
228 WIN_ReleaseWndPtr( ptr );
229 ptr = parent;
230 OffsetRgn( tmp, ptr->rectClient.left, ptr->rectClient.top );
231 xoffset += ptr->rectClient.left;
232 yoffset += ptr->rectClient.top;
234 WIN_ReleaseWndPtr( ptr );
235 /* make it relative to the target window again */
236 OffsetRgn( tmp, -xoffset, -yoffset );
238 /* now subtract the computed region from the original one */
239 ret = CombineRgn( rgn, rgn, tmp, RGN_DIFF );
240 DeleteObject( tmp );
241 return ret;
245 /***********************************************************************
246 * expose_window
248 * Expose a region of a given window.
250 static void expose_window( HWND hwnd, RECT *rect, HRGN rgn, int flags )
252 POINT offset;
253 HWND top = 0;
254 HWND *list;
255 int i;
257 /* find the top most parent that doesn't clip children or siblings and
258 * invalidate the area on its parent, including all children */
259 if ((list = WIN_ListParents( hwnd )))
261 HWND current = hwnd;
262 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
263 for (i = 0; list[i] && list[i] != GetDesktopWindow(); i++)
265 if (!(style & WS_CLIPSIBLINGS)) top = current;
266 style = GetWindowLongW( list[i], GWL_STYLE );
267 if (!(style & WS_CLIPCHILDREN)) top = current;
268 current = list[i];
271 if (top)
273 /* find the parent of the top window, reusing the parent list */
274 if (top == hwnd) i = 0;
275 else
277 for (i = 0; list[i]; i++) if (list[i] == top) break;
278 if (list[i] && list[i+1]) i++;
280 if (list[i] != GetDesktopWindow()) top = list[i];
281 flags &= ~RDW_FRAME; /* parent will invalidate children frame anyway */
282 flags |= RDW_ALLCHILDREN;
284 HeapFree( GetProcessHeap(), 0, list );
287 if (!top) top = hwnd;
289 /* make coords relative to top */
290 offset.x = offset.y = 0;
291 MapWindowPoints( hwnd, top, &offset, 1 );
293 if (rect)
295 OffsetRect( rect, offset.x, offset.y );
296 RedrawWindow( top, rect, 0, flags );
298 else
300 OffsetRgn( rgn, offset.x, offset.y );
301 RedrawWindow( top, NULL, rgn, flags );
306 /***********************************************************************
307 * expose_covered_parent_area
309 * Expose the parent area that has been uncovered by moving/hiding a
310 * given window, but that is still covered by other siblings (the area
311 * not covered by siblings will be exposed automatically by X).
313 static void expose_covered_parent_area( WND *win, const RECT *old_rect )
315 int ret = SIMPLEREGION;
316 HRGN hrgn = CreateRectRgnIndirect( old_rect );
318 if (win->dwStyle & WS_VISIBLE)
320 HRGN tmp = CreateRectRgnIndirect( &win->rectWindow );
321 ret = CombineRgn( hrgn, hrgn, tmp, RGN_DIFF );
322 DeleteObject( tmp );
325 if (ret != NULLREGION)
327 if (get_covered_region( win, hrgn ) != NULLREGION)
328 expose_window( win->parent, NULL, hrgn, RDW_INVALIDATE | RDW_ERASE | RDW_ALLCHILDREN );
330 DeleteObject( hrgn );
334 /***********************************************************************
335 * expose_covered_window_area
337 * Expose the area of a window that is covered by other siblings.
339 static void expose_covered_window_area( WND *win, const RECT *old_client_rect, BOOL frame )
341 HRGN hrgn;
342 int ret = SIMPLEREGION;
344 if (frame)
345 hrgn = CreateRectRgn( win->rectWindow.left - win->rectClient.left,
346 win->rectWindow.top - win->rectClient.top,
347 win->rectWindow.right - win->rectWindow.left,
348 win->rectWindow.bottom - win->rectWindow.top );
349 else
350 hrgn = CreateRectRgn( 0, 0,
351 win->rectClient.right - win->rectClient.left,
352 win->rectClient.bottom - win->rectClient.top );
354 /* if the client rect didn't move we don't need to repaint it all */
355 if (old_client_rect->left == win->rectClient.left &&
356 old_client_rect->top == win->rectClient.top)
358 RECT rc;
360 if (IntersectRect( &rc, old_client_rect, &win->rectClient ))
362 HRGN tmp;
363 /* subtract the unchanged client area from the region to expose */
364 OffsetRect( &rc, -win->rectClient.left, -win->rectClient.top );
365 if ((tmp = CreateRectRgnIndirect( &rc )))
367 ret = CombineRgn( hrgn, hrgn, tmp, RGN_DIFF );
368 DeleteObject( tmp );
373 if (ret != NULLREGION)
375 if (get_covered_region( win, hrgn ) != NULLREGION)
376 expose_window( win->hwndSelf, NULL, hrgn,
377 RDW_INVALIDATE | RDW_ERASE | RDW_FRAME | RDW_ALLCHILDREN );
380 DeleteObject( hrgn );
384 /***********************************************************************
385 * X11DRV_Expose
387 void X11DRV_Expose( HWND hwnd, XExposeEvent *event )
389 RECT rect;
390 struct x11drv_win_data *data;
391 int flags = RDW_INVALIDATE | RDW_ERASE;
392 WND *win;
394 TRACE( "win %p (%lx) %d,%d %dx%d\n",
395 hwnd, event->window, event->x, event->y, event->width, event->height );
397 rect.left = event->x;
398 rect.top = event->y;
399 rect.right = rect.left + event->width;
400 rect.bottom = rect.top + event->height;
402 if (!(win = WIN_GetPtr( hwnd ))) return;
403 data = win->pDriverData;
405 if (event->window != data->client_window) /* whole window or icon window */
407 flags |= RDW_FRAME;
408 /* make position relative to client area instead of window */
409 OffsetRect( &rect, -data->client_rect.left, -data->client_rect.top );
411 WIN_ReleasePtr( win );
413 expose_window( hwnd, &rect, 0, flags );
417 /***********************************************************************
418 * GetDC (X11DRV.@)
420 * Set the drawable, origin and dimensions for the DC associated to
421 * a given window.
423 BOOL X11DRV_GetDC( HWND hwnd, HDC hdc, HRGN hrgn, DWORD flags )
425 WND *win = WIN_GetPtr( hwnd );
426 HWND top = 0;
427 X11DRV_WND_DATA *data = win->pDriverData;
428 struct x11drv_escape_set_drawable escape;
429 BOOL visible;
431 escape.mode = IncludeInferiors;
432 /* don't clip siblings if using parent clip region */
433 if (flags & DCX_PARENTCLIP) flags &= ~DCX_CLIPSIBLINGS;
435 /* find the top parent in the hierarchy that isn't clipping siblings */
436 visible = (win->dwStyle & WS_VISIBLE) != 0;
438 if (visible)
440 HWND *list = WIN_ListParents( hwnd );
441 if (list)
443 int i;
444 for (i = 0; list[i] != GetDesktopWindow(); i++)
446 LONG style = GetWindowLongW( list[i], GWL_STYLE );
447 if (!(style & WS_VISIBLE))
449 visible = FALSE;
450 top = 0;
451 break;
453 if (!(style & WS_CLIPSIBLINGS)) top = list[i];
455 HeapFree( GetProcessHeap(), 0, list );
457 if (!top && visible && !(flags & DCX_CLIPSIBLINGS)) top = hwnd;
460 if (top)
462 HWND parent = GetAncestor( top, GA_PARENT );
463 escape.org.x = escape.org.y = 0;
464 if (flags & DCX_WINDOW)
466 escape.org.x = win->rectWindow.left - win->rectClient.left;
467 escape.org.y = win->rectWindow.top - win->rectClient.top;
469 MapWindowPoints( hwnd, parent, &escape.org, 1 );
470 escape.drawable_org.x = escape.drawable_org.y = 0;
471 MapWindowPoints( parent, 0, &escape.drawable_org, 1 );
472 /* have to use the parent so that we include siblings */
473 if (parent) escape.drawable = X11DRV_get_client_window( parent );
474 else escape.drawable = root_window;
476 else
478 if (IsIconic( hwnd ))
480 escape.drawable = data->icon_window ? data->icon_window : data->whole_window;
481 escape.org.x = 0;
482 escape.org.y = 0;
483 escape.drawable_org = escape.org;
485 else if (flags & DCX_WINDOW)
487 escape.drawable = data->whole_window;
488 escape.org.x = win->rectWindow.left - data->whole_rect.left;
489 escape.org.y = win->rectWindow.top - data->whole_rect.top;
490 escape.drawable_org.x = data->whole_rect.left - win->rectClient.left;
491 escape.drawable_org.y = data->whole_rect.top - win->rectClient.top;
493 else
495 escape.drawable = data->client_window;
496 escape.org.x = 0;
497 escape.org.y = 0;
498 escape.drawable_org = escape.org;
499 if (flags & DCX_CLIPCHILDREN) escape.mode = ClipByChildren; /* can use X11 clipping */
501 MapWindowPoints( hwnd, 0, &escape.drawable_org, 1 );
504 escape.code = X11DRV_SET_DRAWABLE;
505 ExtEscape( hdc, X11DRV_ESCAPE, sizeof(escape), (LPSTR)&escape, 0, NULL );
507 if (flags & (DCX_EXCLUDERGN | DCX_INTERSECTRGN) ||
508 SetHookFlags16( HDC_16(hdc), DCHF_VALIDATEVISRGN )) /* DC was dirty */
510 /* need to recompute the visible region */
511 HRGN visRgn;
513 if (visible)
515 visRgn = get_visible_region( win, top, flags, escape.mode );
517 if (flags & (DCX_EXCLUDERGN | DCX_INTERSECTRGN))
518 CombineRgn( visRgn, visRgn, hrgn,
519 (flags & DCX_INTERSECTRGN) ? RGN_AND : RGN_DIFF );
521 else visRgn = CreateRectRgn( 0, 0, 0, 0 );
523 SelectVisRgn16( HDC_16(hdc), HRGN_16(visRgn) );
524 DeleteObject( visRgn );
527 WIN_ReleasePtr( win );
528 return TRUE;
532 /***********************************************************************
533 * ReleaseDC (X11DRV.@)
535 void X11DRV_ReleaseDC( HWND hwnd, HDC hdc )
537 struct x11drv_escape_set_drawable escape;
539 escape.code = X11DRV_SET_DRAWABLE;
540 escape.drawable = root_window;
541 escape.mode = IncludeInferiors;
542 escape.org.x = escape.org.y = 0;
543 escape.drawable_org.x = escape.drawable_org.y = 0;
545 ExtEscape( hdc, X11DRV_ESCAPE, sizeof(escape), (LPSTR)&escape, 0, NULL );
549 /***********************************************************************
550 * SWP_DoWinPosChanging
552 static BOOL SWP_DoWinPosChanging( WINDOWPOS* pWinpos, RECT* pNewWindowRect, RECT* pNewClientRect )
554 WND *wndPtr;
556 /* Send WM_WINDOWPOSCHANGING message */
558 if (!(pWinpos->flags & SWP_NOSENDCHANGING))
559 SendMessageW( pWinpos->hwnd, WM_WINDOWPOSCHANGING, 0, (LPARAM)pWinpos );
561 if (!(wndPtr = WIN_GetPtr( pWinpos->hwnd )) || wndPtr == WND_OTHER_PROCESS) return FALSE;
563 /* Calculate new position and size */
565 *pNewWindowRect = wndPtr->rectWindow;
566 *pNewClientRect = (wndPtr->dwStyle & WS_MINIMIZE) ? wndPtr->rectWindow
567 : wndPtr->rectClient;
569 if (!(pWinpos->flags & SWP_NOSIZE))
571 pNewWindowRect->right = pNewWindowRect->left + pWinpos->cx;
572 pNewWindowRect->bottom = pNewWindowRect->top + pWinpos->cy;
574 if (!(pWinpos->flags & SWP_NOMOVE))
576 pNewWindowRect->left = pWinpos->x;
577 pNewWindowRect->top = pWinpos->y;
578 pNewWindowRect->right += pWinpos->x - wndPtr->rectWindow.left;
579 pNewWindowRect->bottom += pWinpos->y - wndPtr->rectWindow.top;
581 OffsetRect( pNewClientRect, pWinpos->x - wndPtr->rectWindow.left,
582 pWinpos->y - wndPtr->rectWindow.top );
584 pWinpos->flags |= SWP_NOCLIENTMOVE | SWP_NOCLIENTSIZE;
585 WIN_ReleasePtr( wndPtr );
586 return TRUE;
589 /***********************************************************************
590 * SWP_DoNCCalcSize
592 static UINT SWP_DoNCCalcSize( WINDOWPOS* pWinpos, RECT* pNewWindowRect, RECT* pNewClientRect )
594 UINT wvrFlags = 0;
595 WND *wndPtr;
597 if (!(wndPtr = WIN_GetPtr( pWinpos->hwnd )) || wndPtr == WND_OTHER_PROCESS) return 0;
599 /* Send WM_NCCALCSIZE message to get new client area */
600 if( (pWinpos->flags & (SWP_FRAMECHANGED | SWP_NOSIZE)) != SWP_NOSIZE )
602 NCCALCSIZE_PARAMS params;
603 WINDOWPOS winposCopy;
605 params.rgrc[0] = *pNewWindowRect;
606 params.rgrc[1] = wndPtr->rectWindow;
607 params.rgrc[2] = wndPtr->rectClient;
608 params.lppos = &winposCopy;
609 winposCopy = *pWinpos;
610 WIN_ReleasePtr( wndPtr );
612 wvrFlags = SendMessageW( pWinpos->hwnd, WM_NCCALCSIZE, TRUE, (LPARAM)&params );
614 TRACE( "(%ld,%ld)-(%ld,%ld)\n", params.rgrc[0].left, params.rgrc[0].top,
615 params.rgrc[0].right, params.rgrc[0].bottom );
617 /* If the application send back garbage, ignore it */
618 if (params.rgrc[0].left <= params.rgrc[0].right &&
619 params.rgrc[0].top <= params.rgrc[0].bottom)
620 *pNewClientRect = params.rgrc[0];
622 /* FIXME: WVR_ALIGNxxx */
624 if (!(wndPtr = WIN_GetPtr( pWinpos->hwnd )) || wndPtr == WND_OTHER_PROCESS) return 0;
626 if( pNewClientRect->left != wndPtr->rectClient.left ||
627 pNewClientRect->top != wndPtr->rectClient.top )
628 pWinpos->flags &= ~SWP_NOCLIENTMOVE;
630 if( (pNewClientRect->right - pNewClientRect->left !=
631 wndPtr->rectClient.right - wndPtr->rectClient.left) ||
632 (pNewClientRect->bottom - pNewClientRect->top !=
633 wndPtr->rectClient.bottom - wndPtr->rectClient.top) )
634 pWinpos->flags &= ~SWP_NOCLIENTSIZE;
636 else
638 if (!(pWinpos->flags & SWP_NOMOVE) &&
639 (pNewClientRect->left != wndPtr->rectClient.left ||
640 pNewClientRect->top != wndPtr->rectClient.top))
641 pWinpos->flags &= ~SWP_NOCLIENTMOVE;
643 WIN_ReleasePtr( wndPtr );
644 return wvrFlags;
648 /***********************************************************************
649 * SWP_DoOwnedPopups
651 * fix Z order taking into account owned popups -
652 * basically we need to maintain them above the window that owns them
654 * FIXME: hide/show owned popups when owner visibility changes.
656 static HWND SWP_DoOwnedPopups(HWND hwnd, HWND hwndInsertAfter)
658 HWND *list = NULL;
659 HWND owner = GetWindow( hwnd, GW_OWNER );
660 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
662 WARN("(%p) hInsertAfter = %p\n", hwnd, hwndInsertAfter );
664 if ((style & WS_POPUP) && owner)
666 /* make sure this popup stays above the owner */
668 HWND hwndLocalPrev = HWND_TOP;
670 if( hwndInsertAfter != HWND_TOP )
672 if ((list = WIN_ListChildren( GetDesktopWindow() )))
674 int i;
675 for (i = 0; list[i]; i++)
677 if (list[i] == owner) break;
678 if (list[i] != hwnd) hwndLocalPrev = list[i];
679 if (hwndLocalPrev == hwndInsertAfter) break;
681 hwndInsertAfter = hwndLocalPrev;
685 else if (style & WS_CHILD) return hwndInsertAfter;
687 if (!list) list = WIN_ListChildren( GetDesktopWindow() );
688 if (list)
690 int i;
691 for (i = 0; list[i]; i++)
693 if (list[i] == hwnd) break;
694 if ((GetWindowLongW( list[i], GWL_STYLE ) & WS_POPUP) &&
695 GetWindow( list[i], GW_OWNER ) == hwnd)
697 SetWindowPos( list[i], hwndInsertAfter, 0, 0, 0, 0,
698 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE |
699 SWP_NOSENDCHANGING | SWP_DEFERERASE );
700 hwndInsertAfter = list[i];
703 HeapFree( GetProcessHeap(), 0, list );
706 return hwndInsertAfter;
710 /* fix redundant flags and values in the WINDOWPOS structure */
711 static BOOL fixup_flags( WINDOWPOS *winpos )
713 WND *wndPtr = WIN_GetPtr( winpos->hwnd );
714 BOOL ret = TRUE;
716 if (!wndPtr || wndPtr == WND_OTHER_PROCESS)
718 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
719 return FALSE;
721 winpos->hwnd = wndPtr->hwndSelf; /* make it a full handle */
723 /* Finally make sure that all coordinates are valid */
724 if (winpos->x < -32768) winpos->x = -32768;
725 else if (winpos->x > 32767) winpos->x = 32767;
726 if (winpos->y < -32768) winpos->y = -32768;
727 else if (winpos->y > 32767) winpos->y = 32767;
729 if (winpos->cx < 0) winpos->cx = 0;
730 else if (winpos->cx > 32767) winpos->cx = 32767;
731 if (winpos->cy < 0) winpos->cy = 0;
732 else if (winpos->cy > 32767) winpos->cy = 32767;
734 if (wndPtr->dwStyle & WS_VISIBLE) winpos->flags &= ~SWP_SHOWWINDOW;
735 else
737 winpos->flags &= ~SWP_HIDEWINDOW;
738 if (!(winpos->flags & SWP_SHOWWINDOW)) winpos->flags |= SWP_NOREDRAW;
741 if ((wndPtr->rectWindow.right - wndPtr->rectWindow.left == winpos->cx) &&
742 (wndPtr->rectWindow.bottom - wndPtr->rectWindow.top == winpos->cy))
743 winpos->flags |= SWP_NOSIZE; /* Already the right size */
745 if ((wndPtr->rectWindow.left == winpos->x) && (wndPtr->rectWindow.top == winpos->y))
746 winpos->flags |= SWP_NOMOVE; /* Already the right position */
748 if (winpos->hwnd == GetActiveWindow())
749 winpos->flags |= SWP_NOACTIVATE; /* Already active */
750 else if ((wndPtr->dwStyle & (WS_POPUP | WS_CHILD)) != WS_CHILD)
752 if (!(winpos->flags & SWP_NOACTIVATE)) /* Bring to the top when activating */
754 winpos->flags &= ~SWP_NOZORDER;
755 winpos->hwndInsertAfter = HWND_TOP;
759 /* Check hwndInsertAfter */
760 if (winpos->flags & SWP_NOZORDER) goto done;
762 /* fix sign extension */
763 if (winpos->hwndInsertAfter == (HWND)0xffff) winpos->hwndInsertAfter = HWND_TOPMOST;
764 else if (winpos->hwndInsertAfter == (HWND)0xfffe) winpos->hwndInsertAfter = HWND_NOTOPMOST;
766 /* FIXME: TOPMOST not supported yet */
767 if ((winpos->hwndInsertAfter == HWND_TOPMOST) ||
768 (winpos->hwndInsertAfter == HWND_NOTOPMOST)) winpos->hwndInsertAfter = HWND_TOP;
770 /* hwndInsertAfter must be a sibling of the window */
771 if (winpos->hwndInsertAfter == HWND_TOP)
773 if (GetWindow(winpos->hwnd, GW_HWNDFIRST) == winpos->hwnd)
774 winpos->flags |= SWP_NOZORDER;
776 else if (winpos->hwndInsertAfter == HWND_BOTTOM)
778 if (GetWindow(winpos->hwnd, GW_HWNDLAST) == winpos->hwnd)
779 winpos->flags |= SWP_NOZORDER;
781 else
783 if (GetAncestor( winpos->hwndInsertAfter, GA_PARENT ) != wndPtr->parent) ret = FALSE;
784 else
786 /* don't need to change the Zorder of hwnd if it's already inserted
787 * after hwndInsertAfter or when inserting hwnd after itself.
789 if ((winpos->hwnd == winpos->hwndInsertAfter) ||
790 (winpos->hwnd == GetWindow( winpos->hwndInsertAfter, GW_HWNDNEXT )))
791 winpos->flags |= SWP_NOZORDER;
794 done:
795 WIN_ReleasePtr( wndPtr );
796 return ret;
800 /***********************************************************************
801 * set_visible_style
803 * Set/clear the WS_VISIBLE style of a window and map/unmap the X window.
805 static void set_visible_style( HWND hwnd, BOOL set )
807 WND *win;
809 if (!(win = WIN_GetPtr( hwnd ))) return;
810 if (win == WND_OTHER_PROCESS) return;
812 TRACE( "hwnd %p (%lx) set %d visible %d empty %d\n",
813 hwnd, get_whole_window(win),
814 set, (win->dwStyle & WS_VISIBLE) != 0, IsRectEmpty(&win->rectWindow) );
816 if (set)
818 if (win->dwStyle & WS_VISIBLE) goto done;
819 WIN_SetStyle( hwnd, win->dwStyle | WS_VISIBLE );
820 if (X11DRV_is_window_rect_mapped( &win->rectWindow ) &&
821 get_whole_window(win) && is_window_top_level(win))
823 Display *display = thread_display();
824 X11DRV_sync_window_style( display, win );
825 X11DRV_set_wm_hints( display, win );
826 TRACE( "mapping win %p\n", hwnd );
827 wine_tsx11_lock();
828 XMapWindow( display, get_whole_window(win) );
829 wine_tsx11_unlock();
832 else
834 if (!(win->dwStyle & WS_VISIBLE)) goto done;
835 WIN_SetStyle( hwnd, win->dwStyle & ~WS_VISIBLE );
836 if (X11DRV_is_window_rect_mapped( &win->rectWindow ) &&
837 get_whole_window(win) && is_window_top_level(win))
839 TRACE( "unmapping win %p\n", hwnd );
840 wine_tsx11_lock();
841 XUnmapWindow( thread_display(), get_whole_window(win) );
842 wine_tsx11_unlock();
845 done:
846 WIN_ReleasePtr( win );
850 /***********************************************************************
851 * SetWindowStyle (X11DRV.@)
853 * Update the X state of a window to reflect a style change
855 void X11DRV_SetWindowStyle( HWND hwnd, LONG oldStyle )
857 Display *display = thread_display();
858 WND *wndPtr;
859 LONG changed;
861 if (hwnd == GetDesktopWindow()) return;
862 if (!(wndPtr = WIN_GetPtr( hwnd ))) return;
863 if (wndPtr == WND_OTHER_PROCESS) return;
865 changed = wndPtr->dwStyle ^ oldStyle;
867 if (changed & WS_VISIBLE)
869 if (X11DRV_is_window_rect_mapped( &wndPtr->rectWindow ))
871 if (wndPtr->dwStyle & WS_VISIBLE)
873 TRACE( "mapping win %p\n", hwnd );
874 if (is_window_top_level(wndPtr))
876 X11DRV_sync_window_style( display, wndPtr );
877 X11DRV_set_wm_hints( display, wndPtr );
879 wine_tsx11_lock();
880 XMapWindow( display, get_whole_window(wndPtr) );
881 wine_tsx11_unlock();
883 else if (!is_window_top_level(wndPtr)) /* don't unmap managed windows */
885 TRACE( "unmapping win %p\n", hwnd );
886 wine_tsx11_lock();
887 XUnmapWindow( display, get_whole_window(wndPtr) );
888 wine_tsx11_unlock();
893 if (changed & WS_DISABLED)
895 if (wndPtr->dwExStyle & WS_EX_MANAGED)
897 XWMHints *wm_hints;
898 wine_tsx11_lock();
899 if (!(wm_hints = XGetWMHints( display, get_whole_window(wndPtr) )))
900 wm_hints = XAllocWMHints();
901 if (wm_hints)
903 wm_hints->flags |= InputHint;
904 wm_hints->input = !(wndPtr->dwStyle & WS_DISABLED);
905 XSetWMHints( display, get_whole_window(wndPtr), wm_hints );
906 XFree(wm_hints);
908 wine_tsx11_unlock();
911 WIN_ReleasePtr(wndPtr);
915 /***********************************************************************
916 * SetWindowPos (X11DRV.@)
918 BOOL X11DRV_SetWindowPos( WINDOWPOS *winpos )
920 WND *wndPtr;
921 RECT newWindowRect, newClientRect;
922 RECT oldWindowRect, oldClientRect;
923 UINT wvrFlags = 0;
924 BOOL bChangePos;
926 TRACE( "hwnd %p, after %p, swp %d,%d %dx%d flags %08x\n",
927 winpos->hwnd, winpos->hwndInsertAfter, winpos->x, winpos->y,
928 winpos->cx, winpos->cy, winpos->flags);
930 bChangePos = !(winpos->flags & SWP_WINE_NOHOSTMOVE);
931 winpos->flags &= ~SWP_WINE_NOHOSTMOVE;
933 /* Check window handle */
934 if (winpos->hwnd == GetDesktopWindow()) return FALSE;
936 /* First make sure that coordinates are valid for WM_WINDOWPOSCHANGING */
937 if (!(winpos->flags & SWP_NOMOVE))
939 if (winpos->x < -32768) winpos->x = -32768;
940 else if (winpos->x > 32767) winpos->x = 32767;
941 if (winpos->y < -32768) winpos->y = -32768;
942 else if (winpos->y > 32767) winpos->y = 32767;
944 if (!(winpos->flags & SWP_NOSIZE))
946 if (winpos->cx < 0) winpos->cx = 0;
947 else if (winpos->cx > 32767) winpos->cx = 32767;
948 if (winpos->cy < 0) winpos->cy = 0;
949 else if (winpos->cy > 32767) winpos->cy = 32767;
952 if (!SWP_DoWinPosChanging( winpos, &newWindowRect, &newClientRect )) return FALSE;
954 /* Fix redundant flags */
955 if (!fixup_flags( winpos )) return FALSE;
957 if (!(wndPtr = WIN_FindWndPtr( winpos->hwnd ))) return FALSE;
959 TRACE("\tcurrent (%ld,%ld)-(%ld,%ld), style %08x\n",
960 wndPtr->rectWindow.left, wndPtr->rectWindow.top,
961 wndPtr->rectWindow.right, wndPtr->rectWindow.bottom, (unsigned)wndPtr->dwStyle );
963 if((winpos->flags & (SWP_NOZORDER | SWP_HIDEWINDOW | SWP_SHOWWINDOW)) != SWP_NOZORDER)
965 if (GetAncestor( winpos->hwnd, GA_PARENT ) == GetDesktopWindow())
966 winpos->hwndInsertAfter = SWP_DoOwnedPopups( winpos->hwnd, winpos->hwndInsertAfter );
969 /* Common operations */
971 wvrFlags = SWP_DoNCCalcSize( winpos, &newWindowRect, &newClientRect );
973 if(!(winpos->flags & SWP_NOZORDER) && winpos->hwnd != winpos->hwndInsertAfter)
975 HWND parent = GetAncestor( winpos->hwnd, GA_PARENT );
976 if (parent) WIN_LinkWindow( winpos->hwnd, parent, winpos->hwndInsertAfter );
979 /* Reset active DCEs */
981 if( (((winpos->flags & SWP_AGG_NOPOSCHANGE) != SWP_AGG_NOPOSCHANGE) &&
982 wndPtr->dwStyle & WS_VISIBLE) ||
983 (winpos->flags & (SWP_HIDEWINDOW | SWP_SHOWWINDOW)) )
985 RECT rect;
987 UnionRect(&rect, &newWindowRect, &wndPtr->rectWindow);
988 DCE_InvalidateDCE(wndPtr->hwndSelf, &rect);
991 oldWindowRect = wndPtr->rectWindow;
992 oldClientRect = wndPtr->rectClient;
994 /* Find out if we have to redraw the whole client rect */
996 if( oldClientRect.bottom - oldClientRect.top ==
997 newClientRect.bottom - newClientRect.top ) wvrFlags &= ~WVR_VREDRAW;
999 if( oldClientRect.right - oldClientRect.left ==
1000 newClientRect.right - newClientRect.left ) wvrFlags &= ~WVR_HREDRAW;
1002 /* FIXME: actually do something with WVR_VALIDRECTS */
1004 WIN_SetRectangles( winpos->hwnd, &newWindowRect, &newClientRect );
1006 if (get_whole_window(wndPtr)) /* don't do anything if X window not created yet */
1008 Display *display = thread_display();
1010 if (!(winpos->flags & SWP_SHOWWINDOW) && (winpos->flags & SWP_HIDEWINDOW))
1012 /* clear the update region */
1013 RedrawWindow( winpos->hwnd, NULL, 0, RDW_VALIDATE | RDW_NOFRAME |
1014 RDW_NOERASE | RDW_NOINTERNALPAINT | RDW_ALLCHILDREN );
1015 set_visible_style( winpos->hwnd, FALSE );
1017 else if ((wndPtr->dwStyle & WS_VISIBLE) &&
1018 X11DRV_is_window_rect_mapped( &oldWindowRect ) &&
1019 !X11DRV_is_window_rect_mapped( &newWindowRect ))
1021 /* resizing to zero size or off screen -> unmap */
1022 TRACE( "unmapping zero size or off-screen win %p\n", winpos->hwnd );
1023 wine_tsx11_lock();
1024 XUnmapWindow( display, get_whole_window(wndPtr) );
1025 wine_tsx11_unlock();
1028 wine_tsx11_lock();
1029 if (bChangePos)
1030 X11DRV_sync_whole_window_position( display, wndPtr, !(winpos->flags & SWP_NOZORDER) );
1031 else
1033 struct x11drv_win_data *data = wndPtr->pDriverData;
1034 data->whole_rect = wndPtr->rectWindow;
1035 X11DRV_window_to_X_rect( wndPtr, &data->whole_rect );
1038 if (X11DRV_sync_client_window_position( display, wndPtr ) ||
1039 (winpos->flags & SWP_FRAMECHANGED))
1041 /* if we moved the client area, repaint the whole non-client window */
1042 XClearArea( display, get_whole_window(wndPtr), 0, 0, 0, 0, True );
1043 winpos->flags |= SWP_FRAMECHANGED;
1045 if (winpos->flags & SWP_SHOWWINDOW)
1047 set_visible_style( winpos->hwnd, TRUE );
1049 else if ((wndPtr->dwStyle & WS_VISIBLE) &&
1050 !X11DRV_is_window_rect_mapped( &oldWindowRect ) &&
1051 X11DRV_is_window_rect_mapped( &newWindowRect ))
1053 /* resizing from zero size to non-zero -> map */
1054 TRACE( "mapping non zero size or off-screen win %p\n", winpos->hwnd );
1055 XMapWindow( display, get_whole_window(wndPtr) );
1057 XFlush( display ); /* FIXME: should not be necessary */
1058 wine_tsx11_unlock();
1060 else /* no X window, simply toggle the window style */
1062 if (winpos->flags & SWP_SHOWWINDOW)
1063 set_visible_style( winpos->hwnd, TRUE );
1064 else if (winpos->flags & SWP_HIDEWINDOW)
1065 set_visible_style( winpos->hwnd, FALSE );
1068 /* manually expose the areas that X won't expose because they are still covered by something */
1070 if (!(winpos->flags & SWP_SHOWWINDOW))
1071 expose_covered_parent_area( wndPtr, &oldWindowRect );
1073 if (wndPtr->dwStyle & WS_VISIBLE)
1074 expose_covered_window_area( wndPtr, &oldClientRect, winpos->flags & SWP_FRAMECHANGED );
1076 WIN_ReleaseWndPtr(wndPtr);
1078 if (wvrFlags & WVR_REDRAW) RedrawWindow( winpos->hwnd, NULL, 0, RDW_INVALIDATE | RDW_ERASE );
1080 if( winpos->flags & SWP_HIDEWINDOW )
1081 HideCaret(winpos->hwnd);
1082 else if (winpos->flags & SWP_SHOWWINDOW)
1083 ShowCaret(winpos->hwnd);
1085 if (!(winpos->flags & SWP_NOACTIVATE))
1087 /* child windows get WM_CHILDACTIVATE message */
1088 if ((GetWindowLongW( winpos->hwnd, GWL_STYLE ) & (WS_CHILD | WS_POPUP)) == WS_CHILD)
1089 SendMessageA( winpos->hwnd, WM_CHILDACTIVATE, 0, 0 );
1090 else
1091 SetForegroundWindow( winpos->hwnd );
1094 /* And last, send the WM_WINDOWPOSCHANGED message */
1096 TRACE("\tstatus flags = %04x\n", winpos->flags & SWP_AGG_STATUSFLAGS);
1098 if (((winpos->flags & SWP_AGG_STATUSFLAGS) != SWP_AGG_NOPOSCHANGE))
1099 SendMessageA( winpos->hwnd, WM_WINDOWPOSCHANGED, 0, (LPARAM)winpos );
1100 /* WM_WINDOWPOSCHANGED is send even if SWP_NOSENDCHANGING is set */
1102 return TRUE;
1106 /***********************************************************************
1107 * WINPOS_FindIconPos
1109 * Find a suitable place for an iconic window.
1111 static POINT WINPOS_FindIconPos( WND* wndPtr, POINT pt )
1113 RECT rectParent;
1114 HWND *list;
1115 short x, y, xspacing, yspacing;
1117 GetClientRect( wndPtr->parent, &rectParent );
1118 if ((pt.x >= rectParent.left) && (pt.x + GetSystemMetrics(SM_CXICON) < rectParent.right) &&
1119 (pt.y >= rectParent.top) && (pt.y + GetSystemMetrics(SM_CYICON) < rectParent.bottom))
1120 return pt; /* The icon already has a suitable position */
1122 xspacing = GetSystemMetrics(SM_CXICONSPACING);
1123 yspacing = GetSystemMetrics(SM_CYICONSPACING);
1125 list = WIN_ListChildren( wndPtr->parent );
1126 y = rectParent.bottom;
1127 for (;;)
1129 x = rectParent.left;
1132 /* Check if another icon already occupies this spot */
1133 /* FIXME: this is completely inefficient */
1134 if (list)
1136 int i;
1137 WND *childPtr;
1139 for (i = 0; list[i]; i++)
1141 if (list[i] == wndPtr->hwndSelf) continue;
1142 if (!IsIconic( list[i] )) continue;
1143 if (!(childPtr = WIN_FindWndPtr( list[i] ))) continue;
1144 if ((childPtr->rectWindow.left < x + xspacing) &&
1145 (childPtr->rectWindow.right >= x) &&
1146 (childPtr->rectWindow.top <= y) &&
1147 (childPtr->rectWindow.bottom > y - yspacing))
1149 WIN_ReleaseWndPtr( childPtr );
1150 break; /* There's a window in there */
1152 WIN_ReleaseWndPtr( childPtr );
1154 if (list[i])
1156 /* found something here, try next spot */
1157 x += xspacing;
1158 continue;
1162 /* No window was found, so it's OK for us */
1163 pt.x = x + (xspacing - GetSystemMetrics(SM_CXICON)) / 2;
1164 pt.y = y - (yspacing + GetSystemMetrics(SM_CYICON)) / 2;
1165 HeapFree( GetProcessHeap(), 0, list );
1166 return pt;
1168 } while(x <= rectParent.right-xspacing);
1169 y -= yspacing;
1177 UINT WINPOS_MinMaximize( HWND hwnd, UINT cmd, LPRECT rect )
1179 WND *wndPtr;
1180 UINT swpFlags = 0;
1181 POINT size;
1182 LONG old_style;
1183 WINDOWPLACEMENT wpl;
1185 TRACE("%p %u\n", hwnd, cmd );
1187 wpl.length = sizeof(wpl);
1188 GetWindowPlacement( hwnd, &wpl );
1190 if (HOOK_CallHooks( WH_CBT, HCBT_MINMAX, (WPARAM)hwnd, cmd, TRUE ))
1191 return SWP_NOSIZE | SWP_NOMOVE;
1193 if (IsIconic( hwnd ))
1195 if (cmd == SW_MINIMIZE) return SWP_NOSIZE | SWP_NOMOVE;
1196 if (!SendMessageA( hwnd, WM_QUERYOPEN, 0, 0 )) return SWP_NOSIZE | SWP_NOMOVE;
1197 swpFlags |= SWP_NOCOPYBITS;
1200 if (!(wndPtr = WIN_FindWndPtr( hwnd ))) return 0;
1202 size.x = wndPtr->rectWindow.left;
1203 size.y = wndPtr->rectWindow.top;
1205 switch( cmd )
1207 case SW_MINIMIZE:
1208 if( wndPtr->dwStyle & WS_MAXIMIZE) wndPtr->flags |= WIN_RESTORE_MAX;
1209 else wndPtr->flags &= ~WIN_RESTORE_MAX;
1211 WIN_SetStyle( hwnd, (wndPtr->dwStyle & ~WS_MAXIMIZE) | WS_MINIMIZE );
1213 X11DRV_set_iconic_state( wndPtr );
1215 wpl.ptMinPosition = WINPOS_FindIconPos( wndPtr, wpl.ptMinPosition );
1217 SetRect( rect, wpl.ptMinPosition.x, wpl.ptMinPosition.y,
1218 GetSystemMetrics(SM_CXICON), GetSystemMetrics(SM_CYICON) );
1219 swpFlags |= SWP_NOCOPYBITS;
1220 break;
1222 case SW_MAXIMIZE:
1223 WINPOS_GetMinMaxInfo( hwnd, &size, &wpl.ptMaxPosition, NULL, NULL );
1225 old_style = WIN_SetStyle( hwnd, (wndPtr->dwStyle & ~WS_MINIMIZE) | WS_MAXIMIZE );
1226 if (old_style & WS_MINIMIZE)
1228 WINPOS_ShowIconTitle( hwnd, FALSE );
1229 X11DRV_set_iconic_state( wndPtr );
1231 SetRect( rect, wpl.ptMaxPosition.x, wpl.ptMaxPosition.y, size.x, size.y );
1232 break;
1234 case SW_RESTORE:
1235 old_style = WIN_SetStyle( hwnd, wndPtr->dwStyle & ~(WS_MINIMIZE|WS_MAXIMIZE) );
1236 if (old_style & WS_MINIMIZE)
1238 WINPOS_ShowIconTitle( hwnd, FALSE );
1239 X11DRV_set_iconic_state( wndPtr );
1241 if( wndPtr->flags & WIN_RESTORE_MAX)
1243 /* Restore to maximized position */
1244 WINPOS_GetMinMaxInfo( hwnd, &size, &wpl.ptMaxPosition, NULL, NULL);
1245 WIN_SetStyle( hwnd, wndPtr->dwStyle | WS_MAXIMIZE );
1246 SetRect( rect, wpl.ptMaxPosition.x, wpl.ptMaxPosition.y, size.x, size.y );
1247 break;
1250 else if (!(old_style & WS_MAXIMIZE)) break;
1252 /* Restore to normal position */
1254 *rect = wpl.rcNormalPosition;
1255 rect->right -= rect->left;
1256 rect->bottom -= rect->top;
1258 break;
1261 WIN_ReleaseWndPtr( wndPtr );
1262 return swpFlags;
1266 /***********************************************************************
1267 * ShowWindow (X11DRV.@)
1269 BOOL X11DRV_ShowWindow( HWND hwnd, INT cmd )
1271 WND* wndPtr = WIN_FindWndPtr( hwnd );
1272 BOOL wasVisible, showFlag;
1273 RECT newPos = {0, 0, 0, 0};
1274 UINT swp = 0;
1276 if (!wndPtr) return FALSE;
1277 hwnd = wndPtr->hwndSelf; /* make it a full handle */
1279 wasVisible = (wndPtr->dwStyle & WS_VISIBLE) != 0;
1281 TRACE("hwnd=%p, cmd=%d, wasVisible %d\n", hwnd, cmd, wasVisible);
1283 switch(cmd)
1285 case SW_HIDE:
1286 if (!wasVisible) goto END;
1287 swp |= SWP_HIDEWINDOW | SWP_NOSIZE | SWP_NOMOVE |
1288 SWP_NOACTIVATE | SWP_NOZORDER;
1289 break;
1291 case SW_SHOWMINNOACTIVE:
1292 swp |= SWP_NOACTIVATE | SWP_NOZORDER;
1293 /* fall through */
1294 case SW_SHOWMINIMIZED:
1295 case SW_FORCEMINIMIZE: /* FIXME: Does not work if thread is hung. */
1296 swp |= SWP_SHOWWINDOW;
1297 /* fall through */
1298 case SW_MINIMIZE:
1299 swp |= SWP_FRAMECHANGED;
1300 if( !(wndPtr->dwStyle & WS_MINIMIZE) )
1301 swp |= WINPOS_MinMaximize( hwnd, SW_MINIMIZE, &newPos );
1302 else swp |= SWP_NOSIZE | SWP_NOMOVE;
1303 break;
1305 case SW_SHOWMAXIMIZED: /* same as SW_MAXIMIZE */
1306 swp |= SWP_SHOWWINDOW | SWP_FRAMECHANGED;
1307 if( !(wndPtr->dwStyle & WS_MAXIMIZE) )
1308 swp |= WINPOS_MinMaximize( hwnd, SW_MAXIMIZE, &newPos );
1309 else swp |= SWP_NOSIZE | SWP_NOMOVE;
1310 break;
1312 case SW_SHOWNA:
1313 swp |= SWP_NOACTIVATE | SWP_NOZORDER;
1314 /* fall through */
1315 case SW_SHOW:
1316 if (wasVisible) goto END;
1318 swp |= SWP_SHOWWINDOW | SWP_NOSIZE | SWP_NOMOVE;
1321 * ShowWindow has a little peculiar behavior that if the
1322 * window is already the topmost window, it will not
1323 * activate it.
1325 if (GetTopWindow(NULL)==hwnd && (wasVisible || GetActiveWindow() == hwnd))
1326 swp |= SWP_NOACTIVATE;
1328 break;
1330 case SW_SHOWNOACTIVATE:
1331 swp |= SWP_NOACTIVATE | SWP_NOZORDER;
1332 /* fall through */
1333 case SW_SHOWNORMAL: /* same as SW_NORMAL: */
1334 case SW_SHOWDEFAULT: /* FIXME: should have its own handler */
1335 case SW_RESTORE:
1336 swp |= SWP_SHOWWINDOW | SWP_FRAMECHANGED;
1338 if( wndPtr->dwStyle & (WS_MINIMIZE | WS_MAXIMIZE) )
1339 swp |= WINPOS_MinMaximize( hwnd, SW_RESTORE, &newPos );
1340 else swp |= SWP_NOSIZE | SWP_NOMOVE;
1341 break;
1344 showFlag = (cmd != SW_HIDE);
1345 if (showFlag != wasVisible)
1347 SendMessageW( hwnd, WM_SHOWWINDOW, showFlag, 0 );
1348 if (!IsWindow( hwnd )) goto END;
1351 /* We can't activate a child window */
1352 if ((wndPtr->dwStyle & WS_CHILD) &&
1353 !(wndPtr->dwExStyle & WS_EX_MDICHILD))
1354 swp |= SWP_NOACTIVATE | SWP_NOZORDER;
1356 SetWindowPos( hwnd, HWND_TOP, newPos.left, newPos.top,
1357 newPos.right, newPos.bottom, LOWORD(swp) );
1358 if (cmd == SW_HIDE)
1360 HWND hFocus;
1362 /* FIXME: This will cause the window to be activated irrespective
1363 * of whether it is owned by the same thread. Has to be done
1364 * asynchronously.
1367 if (hwnd == GetActiveWindow())
1368 WINPOS_ActivateOtherWindow(hwnd);
1370 /* Revert focus to parent */
1371 hFocus = GetFocus();
1372 if (hwnd == hFocus || IsChild(hwnd, hFocus))
1374 HWND parent = GetAncestor(hwnd, GA_PARENT);
1375 if (parent == GetDesktopWindow()) parent = 0;
1376 SetFocus(parent);
1379 if (!IsWindow( hwnd )) goto END;
1380 else if( wndPtr->dwStyle & WS_MINIMIZE ) WINPOS_ShowIconTitle( hwnd, TRUE );
1382 if (wndPtr->flags & WIN_NEED_SIZE)
1384 /* should happen only in CreateWindowEx() */
1385 int wParam = SIZE_RESTORED;
1387 wndPtr->flags &= ~WIN_NEED_SIZE;
1388 if (wndPtr->dwStyle & WS_MAXIMIZE) wParam = SIZE_MAXIMIZED;
1389 else if (wndPtr->dwStyle & WS_MINIMIZE) wParam = SIZE_MINIMIZED;
1390 SendMessageA( hwnd, WM_SIZE, wParam,
1391 MAKELONG(wndPtr->rectClient.right-wndPtr->rectClient.left,
1392 wndPtr->rectClient.bottom-wndPtr->rectClient.top));
1393 SendMessageA( hwnd, WM_MOVE, 0,
1394 MAKELONG(wndPtr->rectClient.left, wndPtr->rectClient.top) );
1397 END:
1398 WIN_ReleaseWndPtr(wndPtr);
1399 return wasVisible;
1403 /**********************************************************************
1404 * X11DRV_MapNotify
1406 void X11DRV_MapNotify( HWND hwnd, XMapEvent *event )
1408 HWND hwndFocus = GetFocus();
1409 WND *win;
1411 if (!(win = WIN_GetPtr( hwnd ))) return;
1413 if ((win->dwStyle & WS_VISIBLE) &&
1414 (win->dwStyle & WS_MINIMIZE) &&
1415 (win->dwExStyle & WS_EX_MANAGED))
1417 int x, y;
1418 unsigned int width, height, border, depth;
1419 Window root, top;
1420 RECT rect;
1421 LONG style = (win->dwStyle & ~(WS_MINIMIZE|WS_MAXIMIZE)) | WS_VISIBLE;
1423 /* FIXME: hack */
1424 wine_tsx11_lock();
1425 XGetGeometry( event->display, get_whole_window(win), &root, &x, &y, &width, &height,
1426 &border, &depth );
1427 XTranslateCoordinates( event->display, get_whole_window(win), root, 0, 0, &x, &y, &top );
1428 wine_tsx11_unlock();
1429 rect.left = x;
1430 rect.top = y;
1431 rect.right = x + width;
1432 rect.bottom = y + height;
1433 X11DRV_X_to_window_rect( win, &rect );
1435 DCE_InvalidateDCE( hwnd, &win->rectWindow );
1437 if (win->flags & WIN_RESTORE_MAX) style |= WS_MAXIMIZE;
1438 WIN_SetStyle( hwnd, style );
1439 WIN_ReleasePtr( win );
1441 SendMessageA( hwnd, WM_SHOWWINDOW, SW_RESTORE, 0 );
1442 SetWindowPos( hwnd, 0, rect.left, rect.top, rect.right-rect.left, rect.bottom-rect.top,
1443 SWP_NOZORDER | SWP_WINE_NOHOSTMOVE );
1445 else WIN_ReleasePtr( win );
1446 if (hwndFocus && IsChild( hwnd, hwndFocus )) X11DRV_SetFocus(hwndFocus); /* FIXME */
1450 /**********************************************************************
1451 * X11DRV_UnmapNotify
1453 void X11DRV_UnmapNotify( HWND hwnd, XUnmapEvent *event )
1455 WND *win;
1457 if (!(win = WIN_GetPtr( hwnd ))) return;
1459 if ((win->dwStyle & WS_VISIBLE) && (win->dwExStyle & WS_EX_MANAGED) &&
1460 X11DRV_is_window_rect_mapped( &win->rectWindow ))
1462 if (win->dwStyle & WS_MAXIMIZE)
1463 win->flags |= WIN_RESTORE_MAX;
1464 else
1465 win->flags &= ~WIN_RESTORE_MAX;
1467 WIN_SetStyle( hwnd, (win->dwStyle & ~WS_MAXIMIZE) | WS_MINIMIZE );
1468 WIN_ReleasePtr( win );
1470 EndMenu();
1471 SendMessageA( hwnd, WM_SHOWWINDOW, SW_MINIMIZE, 0 );
1472 SetWindowPos( hwnd, 0, 0, 0, GetSystemMetrics(SM_CXICON), GetSystemMetrics(SM_CYICON),
1473 SWP_NOMOVE | SWP_NOACTIVATE | SWP_NOZORDER | SWP_WINE_NOHOSTMOVE );
1475 else WIN_ReleasePtr( win );
1479 /***********************************************************************
1480 * query_zorder
1482 * Synchronize internal z-order with the window manager's.
1484 static Window __get_common_ancestor( Display *display, Window A, Window B,
1485 Window** children, unsigned* total )
1487 /* find the real root window */
1489 Window root, *childrenB;
1490 unsigned totalB;
1492 wine_tsx11_lock();
1493 while( A != B && A && B )
1495 XQueryTree( display, A, &root, &A, children, total );
1496 XQueryTree( display, B, &root, &B, &childrenB, &totalB );
1497 if( childrenB ) XFree( childrenB );
1498 if( *children ) XFree( *children ), *children = NULL;
1501 if( A && B )
1503 XQueryTree( display, A, &root, &B, children, total );
1504 wine_tsx11_unlock();
1505 return A;
1507 wine_tsx11_unlock();
1508 return 0 ;
1511 static Window __get_top_decoration( Display *display, Window w, Window ancestor )
1513 Window* children, root, prev = w, parent = w;
1514 unsigned total;
1516 wine_tsx11_lock();
1519 w = parent;
1520 XQueryTree( display, w, &root, &parent, &children, &total );
1521 if( children ) XFree( children );
1522 } while( parent && parent != ancestor );
1523 wine_tsx11_unlock();
1524 TRACE("\t%08x -> %08x\n", (unsigned)prev, (unsigned)w );
1525 return ( parent ) ? w : 0 ;
1528 static unsigned __td_lookup( Window w, Window* list, unsigned max )
1530 unsigned i;
1531 for( i = max; i > 0; i-- ) if( list[i - 1] == w ) break;
1532 return i;
1535 static HWND query_zorder( Display *display, HWND hWndCheck)
1537 HWND hwndInsertAfter = HWND_TOP;
1538 Window w, parent, *children = NULL;
1539 unsigned total, check, pos, best;
1540 HWND *list = WIN_ListChildren( GetDesktopWindow() );
1541 HWND hwndA = 0, hwndB = 0;
1542 int i;
1544 /* find at least two managed windows */
1545 if (!list) return 0;
1546 for (i = 0; list[i]; i++)
1548 if (!(GetWindowLongW( list[i], GWL_EXSTYLE ) & WS_EX_MANAGED)) continue;
1549 if (!(GetWindowLongW( list[i], GWL_STYLE ) & WS_VISIBLE)) continue;
1550 if (!hwndA) hwndA = list[i];
1551 else
1553 hwndB = list[i];
1554 break;
1557 if (!hwndA || !hwndB) goto done;
1559 parent = __get_common_ancestor( display, X11DRV_get_whole_window(hwndA),
1560 X11DRV_get_whole_window(hwndB), &children, &total );
1561 if( parent && children )
1563 /* w is the ancestor if hWndCheck that is a direct descendant of 'parent' */
1565 w = __get_top_decoration( display, X11DRV_get_whole_window(hWndCheck), parent );
1567 if( w != children[total-1] ) /* check if at the top */
1569 /* X child at index 0 is at the bottom, at index total-1 is at the top */
1570 check = __td_lookup( w, children, total );
1571 best = total;
1573 /* go through all windows in Wine z-order... */
1574 for (i = 0; list[i]; i++)
1576 if (list[i] == hWndCheck) continue;
1577 if (!(GetWindowLongW( list[i], GWL_EXSTYLE ) & WS_EX_MANAGED)) continue;
1578 if (!(w = __get_top_decoration( display, X11DRV_get_whole_window(list[i]),
1579 parent ))) continue;
1580 pos = __td_lookup( w, children, total );
1581 if( pos < best && pos > check )
1583 /* find a nearest Wine window precedes hWndCheck in the real z-order */
1584 best = pos;
1585 hwndInsertAfter = list[i];
1587 if( best - check == 1 ) break;
1591 wine_tsx11_lock();
1592 if( children ) XFree( children );
1593 wine_tsx11_unlock();
1595 done:
1596 HeapFree( GetProcessHeap(), 0, list );
1597 return hwndInsertAfter;
1601 /***********************************************************************
1602 * X11DRV_handle_desktop_resize
1604 void X11DRV_handle_desktop_resize( unsigned int width, unsigned int height )
1606 RECT rect;
1607 HWND hwnd = GetDesktopWindow();
1609 screen_width = width;
1610 screen_height = height;
1611 TRACE("desktop %p change to (%dx%d)\n", hwnd, width, height);
1612 SetRect( &rect, 0, 0, width, height );
1613 WIN_SetRectangles( hwnd, &rect, &rect );
1614 SendMessageTimeoutW( HWND_BROADCAST, WM_DISPLAYCHANGE, screen_depth,
1615 MAKELPARAM( width, height ), SMTO_ABORTIFHUNG, 2000, NULL );
1619 /***********************************************************************
1620 * X11DRV_ConfigureNotify
1622 void X11DRV_ConfigureNotify( HWND hwnd, XConfigureEvent *event )
1624 HWND oldInsertAfter;
1625 struct x11drv_win_data *data;
1626 WND *win;
1627 RECT rect;
1628 WINDOWPOS winpos;
1629 int x = event->x, y = event->y;
1631 if (!(win = WIN_GetPtr( hwnd ))) return;
1632 data = win->pDriverData;
1634 /* Get geometry */
1636 if (!event->send_event) /* normal event, need to map coordinates to the root */
1638 Window child;
1639 wine_tsx11_lock();
1640 XTranslateCoordinates( event->display, data->whole_window, root_window,
1641 0, 0, &x, &y, &child );
1642 wine_tsx11_unlock();
1644 rect.left = x;
1645 rect.top = y;
1646 rect.right = x + event->width;
1647 rect.bottom = y + event->height;
1648 TRACE( "win %p new X rect %ld,%ld,%ldx%ld (event %d,%d,%dx%d)\n",
1649 hwnd, rect.left, rect.top, rect.right-rect.left, rect.bottom-rect.top,
1650 event->x, event->y, event->width, event->height );
1651 X11DRV_X_to_window_rect( win, &rect );
1652 WIN_ReleasePtr( win );
1654 winpos.hwnd = hwnd;
1655 winpos.x = rect.left;
1656 winpos.y = rect.top;
1657 winpos.cx = rect.right - rect.left;
1658 winpos.cy = rect.bottom - rect.top;
1659 winpos.flags = SWP_NOACTIVATE;
1661 /* Get Z-order (FIXME) */
1663 winpos.hwndInsertAfter = query_zorder( event->display, hwnd );
1665 /* needs to find the first Visible Window above the current one */
1666 oldInsertAfter = hwnd;
1667 for (;;)
1669 oldInsertAfter = GetWindow( oldInsertAfter, GW_HWNDPREV );
1670 if (!oldInsertAfter)
1672 oldInsertAfter = HWND_TOP;
1673 break;
1675 if (GetWindowLongA( oldInsertAfter, GWL_STYLE ) & WS_VISIBLE) break;
1678 /* Compare what has changed */
1680 GetWindowRect( hwnd, &rect );
1681 if (rect.left == winpos.x && rect.top == winpos.y) winpos.flags |= SWP_NOMOVE;
1682 else
1683 TRACE( "%p moving from (%ld,%ld) to (%d,%d)\n",
1684 hwnd, rect.left, rect.top, winpos.x, winpos.y );
1686 if ((rect.right - rect.left == winpos.cx && rect.bottom - rect.top == winpos.cy) ||
1687 IsIconic(hwnd) ||
1688 (IsRectEmpty( &rect ) && winpos.cx == 1 && winpos.cy == 1))
1689 winpos.flags |= SWP_NOSIZE;
1690 else
1691 TRACE( "%p resizing from (%ldx%ld) to (%dx%d)\n",
1692 hwnd, rect.right - rect.left, rect.bottom - rect.top,
1693 winpos.cx, winpos.cy );
1695 if (winpos.hwndInsertAfter == oldInsertAfter) winpos.flags |= SWP_NOZORDER;
1696 else
1697 TRACE( "%p restacking from after %p to after %p\n",
1698 hwnd, oldInsertAfter, winpos.hwndInsertAfter );
1700 /* if nothing changed, don't do anything */
1701 if (winpos.flags == (SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE)) return;
1703 SetWindowPos( hwnd, winpos.hwndInsertAfter, winpos.x, winpos.y,
1704 winpos.cx, winpos.cy, winpos.flags | SWP_WINE_NOHOSTMOVE );
1708 /***********************************************************************
1709 * SetWindowRgn (X11DRV.@)
1711 * Assign specified region to window (for non-rectangular windows)
1713 int X11DRV_SetWindowRgn( HWND hwnd, HRGN hrgn, BOOL redraw )
1715 WND *wndPtr;
1717 if ((wndPtr = WIN_GetPtr( hwnd )) == WND_OTHER_PROCESS)
1719 if (IsWindow( hwnd ))
1720 FIXME( "not supported on other process window %p\n", hwnd );
1721 wndPtr = NULL;
1723 if (!wndPtr)
1725 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
1726 return FALSE;
1729 if (wndPtr->hrgnWnd == hrgn)
1731 WIN_ReleasePtr( wndPtr );
1732 return TRUE;
1735 if (wndPtr->hrgnWnd)
1737 /* delete previous region */
1738 DeleteObject(wndPtr->hrgnWnd);
1739 wndPtr->hrgnWnd = 0;
1741 wndPtr->hrgnWnd = hrgn;
1743 #ifdef HAVE_LIBXSHAPE
1745 Display *display = thread_display();
1746 X11DRV_WND_DATA *data = wndPtr->pDriverData;
1748 if (data->whole_window)
1750 if (!hrgn)
1752 wine_tsx11_lock();
1753 XShapeCombineMask( display, data->whole_window,
1754 ShapeBounding, 0, 0, None, ShapeSet );
1755 wine_tsx11_unlock();
1757 else
1759 XRectangle *aXRect;
1760 int x_offset, y_offset;
1761 DWORD size;
1762 DWORD dwBufferSize = GetRegionData(hrgn, 0, NULL);
1763 PRGNDATA pRegionData = HeapAlloc(GetProcessHeap(), 0, dwBufferSize);
1764 if (!pRegionData)
1766 WIN_ReleasePtr( wndPtr );
1767 return TRUE;
1769 GetRegionData(hrgn, dwBufferSize, pRegionData);
1770 size = pRegionData->rdh.nCount;
1771 x_offset = wndPtr->rectWindow.left - data->whole_rect.left;
1772 y_offset = wndPtr->rectWindow.top - data->whole_rect.top;
1773 /* convert region's "Windows rectangles" to XRectangles */
1774 aXRect = HeapAlloc(GetProcessHeap(), 0, size * sizeof(*aXRect) );
1775 if (aXRect)
1777 XRectangle* pCurrRect = aXRect;
1778 RECT *pRect = (RECT*) pRegionData->Buffer;
1779 for (; pRect < ((RECT*) pRegionData->Buffer) + size ; ++pRect, ++pCurrRect)
1781 pCurrRect->x = pRect->left + x_offset;
1782 pCurrRect->y = pRect->top + y_offset;
1783 pCurrRect->height = pRect->bottom - pRect->top;
1784 pCurrRect->width = pRect->right - pRect->left;
1786 TRACE("Rectangle %04d of %04ld data: X=%04d, Y=%04d, Height=%04d, Width=%04d.\n",
1787 pRect - (RECT*) pRegionData->Buffer,
1788 size,
1789 pCurrRect->x,
1790 pCurrRect->y,
1791 pCurrRect->height,
1792 pCurrRect->width);
1795 /* shape = non-rectangular windows (X11/extensions) */
1796 wine_tsx11_lock();
1797 XShapeCombineRectangles( display, data->whole_window, ShapeBounding,
1798 0, 0, aXRect,
1799 pCurrRect - aXRect, ShapeSet, YXBanded );
1800 wine_tsx11_unlock();
1801 HeapFree(GetProcessHeap(), 0, aXRect );
1803 HeapFree(GetProcessHeap(), 0, pRegionData);
1807 #endif /* HAVE_LIBXSHAPE */
1809 WIN_ReleasePtr( wndPtr );
1810 if (redraw) RedrawWindow( hwnd, NULL, 0, RDW_FRAME | RDW_INVALIDATE | RDW_ERASE );
1811 return TRUE;
1815 /***********************************************************************
1816 * draw_moving_frame
1818 * Draw the frame used when moving or resizing window.
1820 * FIXME: This causes problems in Win95 mode. (why?)
1822 static void draw_moving_frame( HDC hdc, RECT *rect, BOOL thickframe )
1824 if (thickframe)
1826 const int width = GetSystemMetrics(SM_CXFRAME);
1827 const int height = GetSystemMetrics(SM_CYFRAME);
1829 HBRUSH hbrush = SelectObject( hdc, GetStockObject( GRAY_BRUSH ) );
1830 PatBlt( hdc, rect->left, rect->top,
1831 rect->right - rect->left - width, height, PATINVERT );
1832 PatBlt( hdc, rect->left, rect->top + height, width,
1833 rect->bottom - rect->top - height, PATINVERT );
1834 PatBlt( hdc, rect->left + width, rect->bottom - 1,
1835 rect->right - rect->left - width, -height, PATINVERT );
1836 PatBlt( hdc, rect->right - 1, rect->top, -width,
1837 rect->bottom - rect->top - height, PATINVERT );
1838 SelectObject( hdc, hbrush );
1840 else DrawFocusRect( hdc, rect );
1844 /***********************************************************************
1845 * start_size_move
1847 * Initialisation of a move or resize, when initiatied from a menu choice.
1848 * Return hit test code for caption or sizing border.
1850 static LONG start_size_move( HWND hwnd, WPARAM wParam, POINT *capturePoint, LONG style )
1852 LONG hittest = 0;
1853 POINT pt;
1854 MSG msg;
1855 RECT rectWindow;
1857 GetWindowRect( hwnd, &rectWindow );
1859 if ((wParam & 0xfff0) == SC_MOVE)
1861 /* Move pointer at the center of the caption */
1862 RECT rect;
1863 NC_GetInsideRect( hwnd, &rect );
1864 if (style & WS_SYSMENU)
1865 rect.left += GetSystemMetrics(SM_CXSIZE) + 1;
1866 if (style & WS_MINIMIZEBOX)
1867 rect.right -= GetSystemMetrics(SM_CXSIZE) + 1;
1868 if (style & WS_MAXIMIZEBOX)
1869 rect.right -= GetSystemMetrics(SM_CXSIZE) + 1;
1870 pt.x = rectWindow.left + (rect.right - rect.left) / 2;
1871 pt.y = rectWindow.top + rect.top + GetSystemMetrics(SM_CYSIZE)/2;
1872 hittest = HTCAPTION;
1873 *capturePoint = pt;
1875 else /* SC_SIZE */
1877 while(!hittest)
1879 GetMessageW( &msg, 0, WM_KEYFIRST, WM_MOUSELAST );
1880 if (CallMsgFilterW( &msg, MSGF_SIZE )) continue;
1882 switch(msg.message)
1884 case WM_MOUSEMOVE:
1885 hittest = NC_HandleNCHitTest( hwnd, msg.pt );
1886 if ((hittest < HTLEFT) || (hittest > HTBOTTOMRIGHT))
1887 hittest = 0;
1888 break;
1890 case WM_LBUTTONUP:
1891 return 0;
1893 case WM_KEYDOWN:
1894 switch(msg.wParam)
1896 case VK_UP:
1897 hittest = HTTOP;
1898 pt.x =(rectWindow.left+rectWindow.right)/2;
1899 pt.y = rectWindow.top + GetSystemMetrics(SM_CYFRAME) / 2;
1900 break;
1901 case VK_DOWN:
1902 hittest = HTBOTTOM;
1903 pt.x =(rectWindow.left+rectWindow.right)/2;
1904 pt.y = rectWindow.bottom - GetSystemMetrics(SM_CYFRAME) / 2;
1905 break;
1906 case VK_LEFT:
1907 hittest = HTLEFT;
1908 pt.x = rectWindow.left + GetSystemMetrics(SM_CXFRAME) / 2;
1909 pt.y =(rectWindow.top+rectWindow.bottom)/2;
1910 break;
1911 case VK_RIGHT:
1912 hittest = HTRIGHT;
1913 pt.x = rectWindow.right - GetSystemMetrics(SM_CXFRAME) / 2;
1914 pt.y =(rectWindow.top+rectWindow.bottom)/2;
1915 break;
1916 case VK_RETURN:
1917 case VK_ESCAPE: return 0;
1921 *capturePoint = pt;
1923 SetCursorPos( pt.x, pt.y );
1924 NC_HandleSetCursor( hwnd, (WPARAM)hwnd, MAKELONG( hittest, WM_MOUSEMOVE ));
1925 return hittest;
1929 /***********************************************************************
1930 * set_movesize_capture
1932 static void set_movesize_capture( HWND hwnd )
1934 HWND previous = 0;
1936 SERVER_START_REQ( set_capture_window )
1938 req->handle = hwnd;
1939 req->flags = CAPTURE_MOVESIZE;
1940 if (!wine_server_call_err( req ))
1942 previous = reply->previous;
1943 hwnd = reply->full_handle;
1946 SERVER_END_REQ;
1947 if (previous && previous != hwnd)
1948 SendMessageW( previous, WM_CAPTURECHANGED, 0, (LPARAM)hwnd );
1952 /***********************************************************************
1953 * SysCommandSizeMove (X11DRV.@)
1955 * Perform SC_MOVE and SC_SIZE commands.
1957 void X11DRV_SysCommandSizeMove( HWND hwnd, WPARAM wParam )
1959 MSG msg;
1960 RECT sizingRect, mouseRect, origRect;
1961 HDC hdc;
1962 HWND parent;
1963 LONG hittest = (LONG)(wParam & 0x0f);
1964 HCURSOR hDragCursor = 0, hOldCursor = 0;
1965 POINT minTrack, maxTrack;
1966 POINT capturePoint, pt;
1967 LONG style = GetWindowLongA( hwnd, GWL_STYLE );
1968 LONG exstyle = GetWindowLongA( hwnd, GWL_EXSTYLE );
1969 BOOL thickframe = HAS_THICKFRAME( style, exstyle );
1970 BOOL iconic = style & WS_MINIMIZE;
1971 BOOL moved = FALSE;
1972 DWORD dwPoint = GetMessagePos ();
1973 BOOL DragFullWindows = FALSE;
1974 BOOL grab;
1975 int iWndsLocks;
1976 Display *old_gdi_display = NULL;
1977 Display *display = thread_display();
1979 SystemParametersInfoA(SPI_GETDRAGFULLWINDOWS, 0, &DragFullWindows, 0);
1981 pt.x = (short)LOWORD(dwPoint);
1982 pt.y = (short)HIWORD(dwPoint);
1983 capturePoint = pt;
1985 if (IsZoomed(hwnd) || !IsWindowVisible(hwnd) || (exstyle & WS_EX_MANAGED)) return;
1987 if ((wParam & 0xfff0) == SC_MOVE)
1989 if (!hittest) hittest = start_size_move( hwnd, wParam, &capturePoint, style );
1990 if (!hittest) return;
1992 else /* SC_SIZE */
1994 if (!thickframe) return;
1995 if ( hittest && ((wParam & 0xfff0) != SC_MOUSEMENU) )
1996 hittest += (HTLEFT - WMSZ_LEFT);
1997 else
1999 set_movesize_capture( hwnd );
2000 hittest = start_size_move( hwnd, wParam, &capturePoint, style );
2001 if (!hittest)
2003 set_movesize_capture(0);
2004 return;
2009 /* Get min/max info */
2011 WINPOS_GetMinMaxInfo( hwnd, NULL, NULL, &minTrack, &maxTrack );
2012 GetWindowRect( hwnd, &sizingRect );
2013 if (style & WS_CHILD)
2015 parent = GetParent(hwnd);
2016 /* make sizing rect relative to parent */
2017 MapWindowPoints( 0, parent, (POINT*)&sizingRect, 2 );
2018 GetClientRect( parent, &mouseRect );
2020 else
2022 parent = 0;
2023 SetRect(&mouseRect, 0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN));
2025 origRect = sizingRect;
2027 if (ON_LEFT_BORDER(hittest))
2029 mouseRect.left = max( mouseRect.left, sizingRect.right-maxTrack.x );
2030 mouseRect.right = min( mouseRect.right, sizingRect.right-minTrack.x );
2032 else if (ON_RIGHT_BORDER(hittest))
2034 mouseRect.left = max( mouseRect.left, sizingRect.left+minTrack.x );
2035 mouseRect.right = min( mouseRect.right, sizingRect.left+maxTrack.x );
2037 if (ON_TOP_BORDER(hittest))
2039 mouseRect.top = max( mouseRect.top, sizingRect.bottom-maxTrack.y );
2040 mouseRect.bottom = min( mouseRect.bottom,sizingRect.bottom-minTrack.y);
2042 else if (ON_BOTTOM_BORDER(hittest))
2044 mouseRect.top = max( mouseRect.top, sizingRect.top+minTrack.y );
2045 mouseRect.bottom = min( mouseRect.bottom, sizingRect.top+maxTrack.y );
2047 if (parent) MapWindowPoints( parent, 0, (LPPOINT)&mouseRect, 2 );
2049 /* Retrieve a default cache DC (without using the window style) */
2050 hdc = GetDCEx( parent, 0, DCX_CACHE );
2052 if( iconic ) /* create a cursor for dragging */
2054 hDragCursor = (HCURSOR)GetClassLongA( hwnd, GCL_HICON);
2055 if( !hDragCursor ) hDragCursor = (HCURSOR)SendMessageA( hwnd, WM_QUERYDRAGICON, 0, 0L);
2056 if( !hDragCursor ) iconic = FALSE;
2059 /* repaint the window before moving it around */
2060 RedrawWindow( hwnd, NULL, 0, RDW_UPDATENOW | RDW_ALLCHILDREN );
2062 SendMessageA( hwnd, WM_ENTERSIZEMOVE, 0, 0 );
2063 set_movesize_capture( hwnd );
2065 /* grab the server only when moving top-level windows without desktop */
2066 grab = (!DragFullWindows && !parent && (root_window == DefaultRootWindow(gdi_display)));
2068 wine_tsx11_lock();
2069 if (grab)
2071 XSync( gdi_display, False );
2072 XGrabServer( display );
2073 XSync( display, False );
2074 /* switch gdi display to the thread display, since the server is grabbed */
2075 old_gdi_display = gdi_display;
2076 gdi_display = display;
2078 XGrabPointer( display, X11DRV_get_whole_window(hwnd), False,
2079 PointerMotionMask | ButtonPressMask | ButtonReleaseMask,
2080 GrabModeAsync, GrabModeAsync,
2081 parent ? X11DRV_get_client_window(parent) : root_window,
2082 None, CurrentTime );
2083 wine_tsx11_unlock();
2085 while(1)
2087 int dx = 0, dy = 0;
2089 if (!GetMessageW( &msg, 0, WM_KEYFIRST, WM_MOUSELAST )) break;
2090 if (CallMsgFilterW( &msg, MSGF_SIZE )) continue;
2092 /* Exit on button-up, Return, or Esc */
2093 if ((msg.message == WM_LBUTTONUP) ||
2094 ((msg.message == WM_KEYDOWN) &&
2095 ((msg.wParam == VK_RETURN) || (msg.wParam == VK_ESCAPE)))) break;
2097 if ((msg.message != WM_KEYDOWN) && (msg.message != WM_MOUSEMOVE))
2098 continue; /* We are not interested in other messages */
2100 pt = msg.pt;
2102 if (msg.message == WM_KEYDOWN) switch(msg.wParam)
2104 case VK_UP: pt.y -= 8; break;
2105 case VK_DOWN: pt.y += 8; break;
2106 case VK_LEFT: pt.x -= 8; break;
2107 case VK_RIGHT: pt.x += 8; break;
2110 pt.x = max( pt.x, mouseRect.left );
2111 pt.x = min( pt.x, mouseRect.right );
2112 pt.y = max( pt.y, mouseRect.top );
2113 pt.y = min( pt.y, mouseRect.bottom );
2115 dx = pt.x - capturePoint.x;
2116 dy = pt.y - capturePoint.y;
2118 if (dx || dy)
2120 if( !moved )
2122 moved = TRUE;
2124 if( iconic ) /* ok, no system popup tracking */
2126 hOldCursor = SetCursor(hDragCursor);
2127 ShowCursor( TRUE );
2128 WINPOS_ShowIconTitle( hwnd, FALSE );
2130 else if(!DragFullWindows)
2131 draw_moving_frame( hdc, &sizingRect, thickframe );
2134 if (msg.message == WM_KEYDOWN) SetCursorPos( pt.x, pt.y );
2135 else
2137 RECT newRect = sizingRect;
2138 WPARAM wpSizingHit = 0;
2140 if (hittest == HTCAPTION) OffsetRect( &newRect, dx, dy );
2141 if (ON_LEFT_BORDER(hittest)) newRect.left += dx;
2142 else if (ON_RIGHT_BORDER(hittest)) newRect.right += dx;
2143 if (ON_TOP_BORDER(hittest)) newRect.top += dy;
2144 else if (ON_BOTTOM_BORDER(hittest)) newRect.bottom += dy;
2145 if(!iconic && !DragFullWindows) draw_moving_frame( hdc, &sizingRect, thickframe );
2146 capturePoint = pt;
2148 /* determine the hit location */
2149 if (hittest >= HTLEFT && hittest <= HTBOTTOMRIGHT)
2150 wpSizingHit = WMSZ_LEFT + (hittest - HTLEFT);
2151 SendMessageA( hwnd, WM_SIZING, wpSizingHit, (LPARAM)&newRect );
2153 if (!iconic)
2155 if(!DragFullWindows)
2156 draw_moving_frame( hdc, &newRect, thickframe );
2157 else {
2158 /* To avoid any deadlocks, all the locks on the windows
2159 structures must be suspended before the SetWindowPos */
2160 iWndsLocks = WIN_SuspendWndsLock();
2161 SetWindowPos( hwnd, 0, newRect.left, newRect.top,
2162 newRect.right - newRect.left,
2163 newRect.bottom - newRect.top,
2164 ( hittest == HTCAPTION ) ? SWP_NOSIZE : 0 );
2165 WIN_RestoreWndsLock(iWndsLocks);
2168 sizingRect = newRect;
2173 set_movesize_capture(0);
2174 if( iconic )
2176 if( moved ) /* restore cursors, show icon title later on */
2178 ShowCursor( FALSE );
2179 SetCursor( hOldCursor );
2182 else if (moved && !DragFullWindows)
2183 draw_moving_frame( hdc, &sizingRect, thickframe );
2185 ReleaseDC( parent, hdc );
2187 wine_tsx11_lock();
2188 XUngrabPointer( display, CurrentTime );
2189 if (grab)
2191 XSync( display, False );
2192 XUngrabServer( display );
2193 XSync( display, False );
2194 gdi_display = old_gdi_display;
2196 wine_tsx11_unlock();
2198 if (HOOK_CallHooks( WH_CBT, HCBT_MOVESIZE, (WPARAM)hwnd, (LPARAM)&sizingRect, TRUE ))
2199 moved = FALSE;
2201 SendMessageA( hwnd, WM_EXITSIZEMOVE, 0, 0 );
2202 SendMessageA( hwnd, WM_SETVISIBLE, !IsIconic(hwnd), 0L);
2204 /* window moved or resized */
2205 if (moved)
2207 /* To avoid any deadlocks, all the locks on the windows
2208 structures must be suspended before the SetWindowPos */
2209 iWndsLocks = WIN_SuspendWndsLock();
2211 /* if the moving/resizing isn't canceled call SetWindowPos
2212 * with the new position or the new size of the window
2214 if (!((msg.message == WM_KEYDOWN) && (msg.wParam == VK_ESCAPE)) )
2216 /* NOTE: SWP_NOACTIVATE prevents document window activation in Word 6 */
2217 if(!DragFullWindows)
2218 SetWindowPos( hwnd, 0, sizingRect.left, sizingRect.top,
2219 sizingRect.right - sizingRect.left,
2220 sizingRect.bottom - sizingRect.top,
2221 ( hittest == HTCAPTION ) ? SWP_NOSIZE : 0 );
2223 else
2224 { /* restore previous size/position */
2225 if(DragFullWindows)
2226 SetWindowPos( hwnd, 0, origRect.left, origRect.top,
2227 origRect.right - origRect.left,
2228 origRect.bottom - origRect.top,
2229 ( hittest == HTCAPTION ) ? SWP_NOSIZE : 0 );
2232 WIN_RestoreWndsLock(iWndsLocks);
2235 if (IsIconic(hwnd))
2237 /* Single click brings up the system menu when iconized */
2239 if( !moved )
2241 if(style & WS_SYSMENU )
2242 SendMessageA( hwnd, WM_SYSCOMMAND,
2243 SC_MOUSEMENU + HTSYSMENU, MAKELONG(pt.x,pt.y));
2245 else WINPOS_ShowIconTitle( hwnd, TRUE );
2250 /***********************************************************************
2251 * ForceWindowRaise (X11DRV.@)
2253 * Raise a window on top of the X stacking order, while preserving
2254 * the correct Windows Z order.
2256 * FIXME: this should go away.
2258 void X11DRV_ForceWindowRaise( HWND hwnd )
2260 int i = 0;
2261 HWND *list;
2262 XWindowChanges winChanges;
2263 Display *display = thread_display();
2264 WND *wndPtr = WIN_FindWndPtr( hwnd );
2266 if (!wndPtr) return;
2268 if ((wndPtr->dwExStyle & WS_EX_MANAGED) ||
2269 wndPtr->parent != GetDesktopWindow() ||
2270 IsRectEmpty( &wndPtr->rectWindow ) ||
2271 !get_whole_window(wndPtr))
2273 WIN_ReleaseWndPtr( wndPtr );
2274 return;
2276 WIN_ReleaseWndPtr( wndPtr );
2278 /* Raise all windows up to wndPtr according to their Z order.
2279 * (it would be easier with sibling-related Below but it doesn't
2280 * work very well with SGI mwm for instance)
2282 winChanges.stack_mode = Above;
2283 if (!(list = WIN_ListChildren( GetDesktopWindow() ))) return;
2284 while (list[i] && list[i] != hwnd) i++;
2285 if (list[i])
2287 for ( ; i >= 0; i--)
2289 WND *ptr = WIN_FindWndPtr( list[i] );
2290 if (!ptr) continue;
2291 if (!IsRectEmpty( &ptr->rectWindow ) && get_whole_window(ptr))
2293 wine_tsx11_lock();
2294 XReconfigureWMWindow( display, get_whole_window(ptr), 0, CWStackMode, &winChanges );
2295 wine_tsx11_unlock();
2297 WIN_ReleaseWndPtr( ptr );
2300 HeapFree( GetProcessHeap(), 0, list );