2 * Window position related functions.
4 * Copyright 1993, 1994, 1995, 2001 Alexandre Julliard
5 * Copyright 1995, 1996, 1999 Alex Korobka
24 #include "cursoricon.h"
25 #include "nonclient.h"
28 #include "debugtools.h"
30 DEFAULT_DEBUG_CHANNEL(x11drv);
32 #define SWP_AGG_NOGEOMETRYCHANGE \
33 (SWP_NOSIZE | SWP_NOMOVE | SWP_NOCLIENTSIZE | SWP_NOCLIENTMOVE)
34 #define SWP_AGG_NOPOSCHANGE \
35 (SWP_AGG_NOGEOMETRYCHANGE | SWP_NOZORDER)
36 #define SWP_AGG_STATUSFLAGS \
37 (SWP_AGG_NOPOSCHANGE | SWP_FRAMECHANGED | SWP_HIDEWINDOW | SWP_SHOWWINDOW)
39 #define SWP_EX_NOCOPY 0x0001
40 #define SWP_EX_PAINTSELF 0x0002
41 #define SWP_EX_NONCLIENT 0x0004
43 #define HAS_THICKFRAME(style,exStyle) \
44 (((style) & WS_THICKFRAME) && \
45 !(((style) & (WS_DLGFRAME|WS_BORDER)) == WS_DLGFRAME))
47 #define ON_LEFT_BORDER(hit) \
48 (((hit) == HTLEFT) || ((hit) == HTTOPLEFT) || ((hit) == HTBOTTOMLEFT))
49 #define ON_RIGHT_BORDER(hit) \
50 (((hit) == HTRIGHT) || ((hit) == HTTOPRIGHT) || ((hit) == HTBOTTOMRIGHT))
51 #define ON_TOP_BORDER(hit) \
52 (((hit) == HTTOP) || ((hit) == HTTOPLEFT) || ((hit) == HTTOPRIGHT))
53 #define ON_BOTTOM_BORDER(hit) \
54 (((hit) == HTBOTTOM) || ((hit) == HTBOTTOMLEFT) || ((hit) == HTBOTTOMRIGHT))
57 /***********************************************************************
60 * Clip all children of a given window out of the visible region
62 static int clip_children( HWND parent, HWND last, HRGN hrgn, int whole_window )
67 int i, x, y, ret = SIMPLEREGION;
69 /* first check if we have anything to do */
70 if (!(list = WIN_ListChildren( parent ))) return ret;
71 for (i = 0; list[i] && list[i] != last; i++)
72 if (GetWindowLongW( list[i], GWL_STYLE ) & WS_VISIBLE) break;
73 if (!list[i] || list[i] == last) goto done; /* no children to clip */
77 WND *win = WIN_FindWndPtr( parent );
78 x = win->rectWindow.left - win->rectClient.left;
79 y = win->rectWindow.top - win->rectClient.top;
80 WIN_ReleaseWndPtr( win );
84 rectRgn = CreateRectRgn( 0, 0, 0, 0 );
86 for ( ; list[i] && list[i] != last; i++)
88 if (!(ptr = WIN_FindWndPtr( list[i] ))) continue;
89 if (ptr->dwStyle & WS_VISIBLE)
91 SetRectRgn( rectRgn, ptr->rectWindow.left + x, ptr->rectWindow.top + y,
92 ptr->rectWindow.right + x, ptr->rectWindow.bottom + y );
93 if ((ret = CombineRgn( hrgn, hrgn, rectRgn, RGN_DIFF )) == NULLREGION)
95 WIN_ReleaseWndPtr( ptr );
96 break; /* no need to go on, region is empty */
99 WIN_ReleaseWndPtr( ptr );
101 DeleteObject( rectRgn );
103 HeapFree( GetProcessHeap(), 0, list );
108 /***********************************************************************
111 * Compute the visible region of a window
113 static HRGN get_visible_region( WND *win, HWND top, UINT flags, int mode )
117 int xoffset, yoffset;
118 X11DRV_WND_DATA *data = win->pDriverData;
120 if (flags & DCX_WINDOW)
122 xoffset = win->rectWindow.left;
123 yoffset = win->rectWindow.top;
127 xoffset = win->rectClient.left;
128 yoffset = win->rectClient.top;
131 if (flags & DCX_PARENTCLIP)
132 GetClientRect( win->parent, &rect );
133 else if (flags & DCX_WINDOW)
134 rect = data->whole_rect;
136 rect = win->rectClient;
138 /* vis region is relative to the start of the client/window area */
139 OffsetRect( &rect, -xoffset, -yoffset );
141 if (!(rgn = CreateRectRgn( rect.left, rect.top, rect.right, rect.bottom ))) return 0;
143 if ((flags & DCX_CLIPCHILDREN) && (mode != ClipByChildren))
145 /* we need to clip children by hand */
146 if (clip_children( win->hwndSelf, 0, rgn, (flags & DCX_WINDOW) ) == NULLREGION) return rgn;
149 if (top && top != win->hwndSelf) /* need to clip siblings of ancestors */
151 WND *parent, *ptr = WIN_LockWndPtr( win );
154 OffsetRgn( rgn, xoffset, yoffset );
157 if (ptr->dwStyle & WS_CLIPSIBLINGS)
159 if (clip_children( ptr->parent, ptr->hwndSelf, rgn, FALSE ) == NULLREGION) break;
161 if (ptr->hwndSelf == top) break;
162 if (!(parent = WIN_FindWndPtr( ptr->parent ))) break;
163 WIN_ReleaseWndPtr( ptr );
165 /* clip to parent client area */
166 if (tmp) SetRectRgn( tmp, 0, 0, ptr->rectClient.right - ptr->rectClient.left,
167 ptr->rectClient.bottom - ptr->rectClient.top );
168 else tmp = CreateRectRgn( 0, 0, ptr->rectClient.right - ptr->rectClient.left,
169 ptr->rectClient.bottom - ptr->rectClient.top );
170 CombineRgn( rgn, rgn, tmp, RGN_AND );
171 OffsetRgn( rgn, ptr->rectClient.left, ptr->rectClient.top );
172 xoffset += ptr->rectClient.left;
173 yoffset += ptr->rectClient.top;
175 WIN_ReleaseWndPtr( ptr );
176 /* make it relative to the target window again */
177 OffsetRgn( rgn, -xoffset, -yoffset );
178 if (tmp) DeleteObject( tmp );
185 /***********************************************************************
188 * Compute the portion of 'rgn' that is covered by non-clipped siblings.
189 * This is the area that is covered from X point of view, but may still need
191 * 'rgn' must be relative to the client area of the parent of 'win'.
193 static int get_covered_region( WND *win, HRGN rgn )
197 WND *parent, *ptr = WIN_LockWndPtr( win );
198 int xoffset = 0, yoffset = 0;
200 tmp = CreateRectRgn( 0, 0, 0, 0 );
201 CombineRgn( tmp, rgn, 0, RGN_COPY );
203 /* to make things easier we actually build the uncovered
204 * area by removing all siblings and then we subtract that
205 * from the total region to get the covered area */
208 if (!(ptr->dwStyle & WS_CLIPSIBLINGS))
210 if (clip_children( ptr->parent, ptr->hwndSelf, tmp, FALSE ) == NULLREGION) break;
212 if (!(parent = WIN_FindWndPtr( ptr->parent ))) break;
213 WIN_ReleaseWndPtr( ptr );
215 OffsetRgn( tmp, ptr->rectClient.left, ptr->rectClient.top );
216 xoffset += ptr->rectClient.left;
217 yoffset += ptr->rectClient.top;
219 WIN_ReleaseWndPtr( ptr );
220 /* make it relative to the target window again */
221 OffsetRgn( tmp, -xoffset, -yoffset );
223 /* now subtract the computed region from the original one */
224 ret = CombineRgn( rgn, rgn, tmp, RGN_DIFF );
230 /***********************************************************************
233 * Expose a region of a given window.
235 static void expose_window( HWND hwnd, RECT *rect, HRGN rgn, int flags )
242 /* find the top most parent that doesn't clip children or siblings and
243 * invalidate the area on its parent, including all children */
244 if ((list = WIN_ListParents( hwnd )))
247 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
248 for (i = 0; list[i] && list[i] != GetDesktopWindow(); i++)
250 if (!(style & WS_CLIPSIBLINGS)) top = current;
251 style = GetWindowLongW( list[i], GWL_STYLE );
252 if (!(style & WS_CLIPCHILDREN)) top = current;
258 /* find the parent of the top window, reusing the parent list */
259 if (top == hwnd) i = 0;
262 for (i = 0; list[i]; i++) if (list[i] == top) break;
263 if (list[i] && list[i+1]) i++;
265 if (list[i] != GetDesktopWindow()) top = list[i];
266 flags &= ~RDW_FRAME; /* parent will invalidate children frame anyway */
267 flags |= RDW_ALLCHILDREN;
269 HeapFree( GetProcessHeap(), 0, list );
272 if (!top) top = hwnd;
274 /* make coords relative to top */
275 offset.x = offset.y = 0;
276 MapWindowPoints( hwnd, top, &offset, 1 );
280 OffsetRect( rect, offset.x, offset.y );
281 RedrawWindow( top, rect, 0, flags );
285 OffsetRgn( rgn, offset.x, offset.y );
286 RedrawWindow( top, NULL, rgn, flags );
291 /***********************************************************************
292 * expose_covered_parent_area
294 * Expose the parent area that has been uncovered by moving/hiding a
295 * given window, but that is still covered by other siblings (the area
296 * not covered by siblings will be exposed automatically by X).
298 static void expose_covered_parent_area( WND *win, const RECT *old_rect )
300 int ret = SIMPLEREGION;
301 HRGN hrgn = CreateRectRgnIndirect( old_rect );
303 if (win->dwStyle & WS_VISIBLE)
305 HRGN tmp = CreateRectRgnIndirect( &win->rectWindow );
306 ret = CombineRgn( hrgn, hrgn, tmp, RGN_DIFF );
310 if (ret != NULLREGION)
312 if (get_covered_region( win, hrgn ) != NULLREGION)
313 expose_window( win->parent, NULL, hrgn, RDW_INVALIDATE | RDW_ERASE | RDW_ALLCHILDREN );
315 DeleteObject( hrgn );
319 /***********************************************************************
320 * expose_covered_window_area
322 * Expose the area of a window that is covered by other siblings.
324 static void expose_covered_window_area( WND *win, const RECT *old_client_rect, BOOL frame )
327 int ret = SIMPLEREGION;
330 hrgn = CreateRectRgn( win->rectWindow.left - win->rectClient.left,
331 win->rectWindow.top - win->rectClient.top,
332 win->rectWindow.right - win->rectWindow.left,
333 win->rectWindow.bottom - win->rectWindow.top );
335 hrgn = CreateRectRgn( 0, 0,
336 win->rectClient.right - win->rectClient.left,
337 win->rectClient.bottom - win->rectClient.top );
339 /* if the client rect didn't move we don't need to repaint it all */
340 if (old_client_rect->left == win->rectClient.left &&
341 old_client_rect->top == win->rectClient.top)
345 if (IntersectRect( &rc, old_client_rect, &win->rectClient ))
348 /* subtract the unchanged client area from the region to expose */
349 OffsetRect( &rc, -win->rectClient.left, -win->rectClient.top );
350 if ((tmp = CreateRectRgnIndirect( &rc )))
352 ret = CombineRgn( hrgn, hrgn, tmp, RGN_DIFF );
358 if (ret != NULLREGION)
360 if (get_covered_region( win, hrgn ) != NULLREGION)
361 expose_window( win->hwndSelf, NULL, hrgn,
362 RDW_INVALIDATE | RDW_ERASE | RDW_FRAME | RDW_ALLCHILDREN );
365 DeleteObject( hrgn );
369 /***********************************************************************
372 void X11DRV_Expose( HWND hwnd, XExposeEvent *event )
375 struct x11drv_win_data *data;
376 int flags = RDW_INVALIDATE | RDW_ERASE;
379 TRACE( "win %x (%lx) %d,%d %dx%d\n",
380 hwnd, event->window, event->x, event->y, event->width, event->height );
382 rect.left = event->x;
384 rect.right = rect.left + event->width;
385 rect.bottom = rect.top + event->height;
387 if (!(win = WIN_FindWndPtr(hwnd))) return;
388 data = win->pDriverData;
390 if (event->window != data->client_window) /* whole window or icon window */
393 /* make position relative to client area instead of window */
394 OffsetRect( &rect, -data->client_rect.left, -data->client_rect.top );
396 WIN_ReleaseWndPtr( win );
398 expose_window( hwnd, &rect, 0, flags );
402 /***********************************************************************
405 * Set the drawable, origin and dimensions for the DC associated to
408 BOOL X11DRV_GetDC( HWND hwnd, HDC hdc, HRGN hrgn, DWORD flags )
410 WND *win = WIN_FindWndPtr( hwnd );
412 X11DRV_WND_DATA *data = win->pDriverData;
416 int mode = IncludeInferiors;
418 /* don't clip siblings if using parent clip region */
419 if (flags & DCX_PARENTCLIP) flags &= ~DCX_CLIPSIBLINGS;
421 /* find the top parent in the hierarchy that isn't clipping siblings */
422 visible = (win->dwStyle & WS_VISIBLE) != 0;
426 HWND *list = WIN_ListParents( hwnd );
430 for (i = 0; list[i] != GetDesktopWindow(); i++)
432 LONG style = GetWindowLongW( list[i], GWL_STYLE );
433 if (!(style & WS_VISIBLE))
439 if (!(style & WS_CLIPSIBLINGS)) top = list[i];
441 HeapFree( GetProcessHeap(), 0, list );
443 if (!top && visible && !(flags & DCX_CLIPSIBLINGS)) top = hwnd;
448 HWND parent = GetAncestor( top, GA_PARENT );
450 if (flags & DCX_WINDOW)
452 org.x = win->rectWindow.left - win->rectClient.left;
453 org.y = win->rectWindow.top - win->rectClient.top;
455 MapWindowPoints( hwnd, parent, &org, 1 );
456 /* have to use the parent so that we include siblings */
457 if (parent) drawable = X11DRV_get_client_window( parent );
458 else drawable = root_window;
462 if (IsIconic( hwnd ))
464 drawable = data->icon_window ? data->icon_window : data->whole_window;
468 else if (flags & DCX_WINDOW)
470 drawable = data->whole_window;
471 org.x = win->rectWindow.left - data->whole_rect.left;
472 org.y = win->rectWindow.top - data->whole_rect.top;
476 drawable = data->client_window;
479 if (flags & DCX_CLIPCHILDREN) mode = ClipByChildren; /* can use X11 clipping */
483 X11DRV_SetDrawable( hdc, drawable, mode, org.x, org.y );
485 if (flags & (DCX_EXCLUDERGN | DCX_INTERSECTRGN) ||
486 SetHookFlags16( hdc, DCHF_VALIDATEVISRGN )) /* DC was dirty */
488 /* need to recompute the visible region */
493 visRgn = get_visible_region( win, top, flags, mode );
495 if (flags & (DCX_EXCLUDERGN | DCX_INTERSECTRGN))
496 CombineRgn( visRgn, visRgn, hrgn,
497 (flags & DCX_INTERSECTRGN) ? RGN_AND : RGN_DIFF );
499 /* make it relative to the drawable origin */
500 OffsetRgn( visRgn, org.x, org.y );
502 else visRgn = CreateRectRgn( 0, 0, 0, 0 );
504 SelectVisRgn16( hdc, visRgn );
505 DeleteObject( visRgn );
508 WIN_ReleaseWndPtr( win );
514 /***********************************************************************
515 * SWP_DoWinPosChanging
517 static BOOL SWP_DoWinPosChanging( WINDOWPOS* pWinpos, RECT* pNewWindowRect, RECT* pNewClientRect )
521 /* Send WM_WINDOWPOSCHANGING message */
523 if (!(pWinpos->flags & SWP_NOSENDCHANGING))
524 SendMessageA( pWinpos->hwnd, WM_WINDOWPOSCHANGING, 0, (LPARAM)pWinpos );
526 if (!(wndPtr = WIN_FindWndPtr( pWinpos->hwnd ))) return FALSE;
528 /* Calculate new position and size */
530 *pNewWindowRect = wndPtr->rectWindow;
531 *pNewClientRect = (wndPtr->dwStyle & WS_MINIMIZE) ? wndPtr->rectWindow
532 : wndPtr->rectClient;
534 if (!(pWinpos->flags & SWP_NOSIZE))
536 pNewWindowRect->right = pNewWindowRect->left + pWinpos->cx;
537 pNewWindowRect->bottom = pNewWindowRect->top + pWinpos->cy;
539 if (!(pWinpos->flags & SWP_NOMOVE))
541 pNewWindowRect->left = pWinpos->x;
542 pNewWindowRect->top = pWinpos->y;
543 pNewWindowRect->right += pWinpos->x - wndPtr->rectWindow.left;
544 pNewWindowRect->bottom += pWinpos->y - wndPtr->rectWindow.top;
546 OffsetRect( pNewClientRect, pWinpos->x - wndPtr->rectWindow.left,
547 pWinpos->y - wndPtr->rectWindow.top );
549 pWinpos->flags |= SWP_NOCLIENTMOVE | SWP_NOCLIENTSIZE;
550 WIN_ReleaseWndPtr( wndPtr );
554 /***********************************************************************
557 static UINT SWP_DoNCCalcSize( WND* wndPtr, WINDOWPOS* pWinpos,
558 RECT* pNewWindowRect, RECT* pNewClientRect )
562 /* Send WM_NCCALCSIZE message to get new client area */
563 if( (pWinpos->flags & (SWP_FRAMECHANGED | SWP_NOSIZE)) != SWP_NOSIZE )
565 wvrFlags = WINPOS_SendNCCalcSize( pWinpos->hwnd, TRUE, pNewWindowRect,
566 &wndPtr->rectWindow, &wndPtr->rectClient,
567 pWinpos, pNewClientRect );
568 /* FIXME: WVR_ALIGNxxx */
570 if( pNewClientRect->left != wndPtr->rectClient.left ||
571 pNewClientRect->top != wndPtr->rectClient.top )
572 pWinpos->flags &= ~SWP_NOCLIENTMOVE;
574 if( (pNewClientRect->right - pNewClientRect->left !=
575 wndPtr->rectClient.right - wndPtr->rectClient.left) ||
576 (pNewClientRect->bottom - pNewClientRect->top !=
577 wndPtr->rectClient.bottom - wndPtr->rectClient.top) )
578 pWinpos->flags &= ~SWP_NOCLIENTSIZE;
581 if( !(pWinpos->flags & SWP_NOMOVE) && (pNewClientRect->left != wndPtr->rectClient.left ||
582 pNewClientRect->top != wndPtr->rectClient.top) )
583 pWinpos->flags &= ~SWP_NOCLIENTMOVE;
587 /***********************************************************************
590 * fix Z order taking into account owned popups -
591 * basically we need to maintain them above the window that owns them
593 * FIXME: hide/show owned popups when owner visibility changes.
595 static HWND SWP_DoOwnedPopups(HWND hwnd, HWND hwndInsertAfter)
598 HWND owner = GetWindow( hwnd, GW_OWNER );
599 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
601 WARN("(%04x) hInsertAfter = %04x\n", hwnd, hwndInsertAfter );
603 if ((style & WS_POPUP) && owner)
605 /* make sure this popup stays above the owner */
607 HWND hwndLocalPrev = HWND_TOP;
609 if( hwndInsertAfter != HWND_TOP )
611 if ((list = WIN_ListChildren( GetDesktopWindow() )))
614 for (i = 0; list[i]; i++)
616 if (list[i] == owner) break;
617 if (list[i] != hwnd) hwndLocalPrev = list[i];
618 if (hwndLocalPrev == hwndInsertAfter) break;
620 hwndInsertAfter = hwndLocalPrev;
624 else if (style & WS_CHILD) return hwndInsertAfter;
626 if (!list) list = WIN_ListChildren( GetDesktopWindow() );
630 for (i = 0; list[i]; i++)
632 if (list[i] == hwnd) break;
633 if ((GetWindowLongW( list[i], GWL_STYLE ) & WS_POPUP) &&
634 GetWindow( list[i], GW_OWNER ) == hwnd)
636 SetWindowPos( list[i], hwndInsertAfter, 0, 0, 0, 0,
637 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE |
638 SWP_NOSENDCHANGING | SWP_DEFERERASE );
639 hwndInsertAfter = list[i];
642 HeapFree( GetProcessHeap(), 0, list );
645 return hwndInsertAfter;
649 /* fix redundant flags and values in the WINDOWPOS structure */
650 static BOOL fixup_flags( WINDOWPOS *winpos )
652 WND *wndPtr = WIN_FindWndPtr( winpos->hwnd );
655 if (!wndPtr) return FALSE;
656 winpos->hwnd = wndPtr->hwndSelf; /* make it a full handle */
658 if (wndPtr->dwStyle & WS_VISIBLE) winpos->flags &= ~SWP_SHOWWINDOW;
661 winpos->flags &= ~SWP_HIDEWINDOW;
662 if (!(winpos->flags & SWP_SHOWWINDOW)) winpos->flags |= SWP_NOREDRAW;
665 if (winpos->cx < 0) winpos->cx = 0;
666 if (winpos->cy < 0) winpos->cy = 0;
668 if ((wndPtr->rectWindow.right - wndPtr->rectWindow.left == winpos->cx) &&
669 (wndPtr->rectWindow.bottom - wndPtr->rectWindow.top == winpos->cy))
670 winpos->flags |= SWP_NOSIZE; /* Already the right size */
672 if ((wndPtr->rectWindow.left == winpos->x) && (wndPtr->rectWindow.top == winpos->y))
673 winpos->flags |= SWP_NOMOVE; /* Already the right position */
675 if (winpos->hwnd == GetForegroundWindow())
676 winpos->flags |= SWP_NOACTIVATE; /* Already active */
677 else if ((wndPtr->dwStyle & (WS_POPUP | WS_CHILD)) != WS_CHILD)
679 if (!(winpos->flags & SWP_NOACTIVATE)) /* Bring to the top when activating */
681 winpos->flags &= ~SWP_NOZORDER;
682 winpos->hwndInsertAfter = HWND_TOP;
687 /* Check hwndInsertAfter */
689 /* fix sign extension */
690 if (winpos->hwndInsertAfter == (HWND)0xffff) winpos->hwndInsertAfter = HWND_TOPMOST;
691 else if (winpos->hwndInsertAfter == (HWND)0xfffe) winpos->hwndInsertAfter = HWND_NOTOPMOST;
693 /* FIXME: TOPMOST not supported yet */
694 if ((winpos->hwndInsertAfter == HWND_TOPMOST) ||
695 (winpos->hwndInsertAfter == HWND_NOTOPMOST)) winpos->hwndInsertAfter = HWND_TOP;
697 /* hwndInsertAfter must be a sibling of the window */
698 if ((winpos->hwndInsertAfter != HWND_TOP) && (winpos->hwndInsertAfter != HWND_BOTTOM))
700 WND* wnd = WIN_FindWndPtr(winpos->hwndInsertAfter);
703 winpos->hwndInsertAfter = wnd->hwndSelf; /* make it a full handle */
704 if (wnd->parent != wndPtr->parent) ret = FALSE;
707 /* don't need to change the Zorder of hwnd if it's already inserted
708 * after hwndInsertAfter or when inserting hwnd after itself.
710 if ((winpos->hwnd == winpos->hwndInsertAfter) ||
711 (winpos->hwnd == GetWindow( winpos->hwndInsertAfter, GW_HWNDNEXT )))
712 winpos->flags |= SWP_NOZORDER;
714 WIN_ReleaseWndPtr(wnd);
718 WIN_ReleaseWndPtr(wndPtr);
723 /***********************************************************************
724 * SetWindowStyle (X11DRV.@)
726 * Update the X state of a window to reflect a style change
728 void X11DRV_SetWindowStyle( HWND hwnd, LONG oldStyle )
730 Display *display = thread_display();
731 WND *wndPtr = WIN_FindWndPtr( hwnd );
734 if ((wndPtr->dwStyle & WS_VISIBLE) && (!(oldStyle & WS_VISIBLE)))
736 if (!IsRectEmpty( &wndPtr->rectWindow ))
738 TRACE( "mapping win %x\n", hwnd );
739 TSXMapWindow( display, get_whole_window(wndPtr) );
742 WIN_ReleaseWndPtr(wndPtr);
746 /***********************************************************************
747 * SetWindowPos (X11DRV.@)
749 BOOL X11DRV_SetWindowPos( WINDOWPOS *winpos )
752 RECT newWindowRect, newClientRect;
753 RECT oldWindowRect, oldClientRect;
757 TRACE( "hwnd %04x, swp (%i,%i)-(%i,%i) flags %08x\n",
758 winpos->hwnd, winpos->x, winpos->y,
759 winpos->x + winpos->cx, winpos->y + winpos->cy, winpos->flags);
761 bChangePos = !(winpos->flags & SWP_WINE_NOHOSTMOVE);
762 winpos->flags &= ~SWP_WINE_NOHOSTMOVE;
764 /* Fix redundant flags */
765 if (!fixup_flags( winpos )) return FALSE;
767 /* Check window handle */
768 if (winpos->hwnd == GetDesktopWindow()) return FALSE;
770 SWP_DoWinPosChanging( winpos, &newWindowRect, &newClientRect );
772 if (!(wndPtr = WIN_FindWndPtr( winpos->hwnd ))) return FALSE;
774 TRACE("\tcurrent (%i,%i)-(%i,%i), style %08x\n",
775 wndPtr->rectWindow.left, wndPtr->rectWindow.top,
776 wndPtr->rectWindow.right, wndPtr->rectWindow.bottom, (unsigned)wndPtr->dwStyle );
778 if((winpos->flags & (SWP_NOZORDER | SWP_HIDEWINDOW | SWP_SHOWWINDOW)) != SWP_NOZORDER)
780 if (GetAncestor( winpos->hwnd, GA_PARENT ) == GetDesktopWindow())
781 winpos->hwndInsertAfter = SWP_DoOwnedPopups( winpos->hwnd, winpos->hwndInsertAfter );
784 /* Common operations */
786 wvrFlags = SWP_DoNCCalcSize( wndPtr, winpos, &newWindowRect, &newClientRect );
788 if(!(winpos->flags & SWP_NOZORDER) && winpos->hwnd != winpos->hwndInsertAfter)
790 HWND parent = GetAncestor( winpos->hwnd, GA_PARENT );
791 if (parent) WIN_LinkWindow( winpos->hwnd, parent, winpos->hwndInsertAfter );
794 /* Reset active DCEs */
796 if( (((winpos->flags & SWP_AGG_NOPOSCHANGE) != SWP_AGG_NOPOSCHANGE) &&
797 wndPtr->dwStyle & WS_VISIBLE) ||
798 (winpos->flags & (SWP_HIDEWINDOW | SWP_SHOWWINDOW)) )
802 UnionRect(&rect, &newWindowRect, &wndPtr->rectWindow);
803 DCE_InvalidateDCE(wndPtr->hwndSelf, &rect);
806 oldWindowRect = wndPtr->rectWindow;
807 oldClientRect = wndPtr->rectClient;
809 /* Find out if we have to redraw the whole client rect */
811 if( oldClientRect.bottom - oldClientRect.top ==
812 newClientRect.bottom - newClientRect.top ) wvrFlags &= ~WVR_VREDRAW;
814 if( oldClientRect.right - oldClientRect.left ==
815 newClientRect.right - newClientRect.left ) wvrFlags &= ~WVR_HREDRAW;
817 /* FIXME: actually do something with WVR_VALIDRECTS */
819 wndPtr->rectWindow = newWindowRect;
820 wndPtr->rectClient = newClientRect;
822 if (winpos->flags & SWP_SHOWWINDOW) wndPtr->dwStyle |= WS_VISIBLE;
823 else if (winpos->flags & SWP_HIDEWINDOW)
825 /* clear the update region */
826 RedrawWindow( winpos->hwnd, NULL, 0, RDW_VALIDATE | RDW_NOFRAME | RDW_NOERASE |
827 RDW_NOINTERNALPAINT | RDW_ALLCHILDREN );
828 wndPtr->dwStyle &= ~WS_VISIBLE;
831 if (get_whole_window(wndPtr)) /* don't do anything if X window not created yet */
833 Display *display = thread_display();
836 if (!(winpos->flags & SWP_SHOWWINDOW) && (winpos->flags & SWP_HIDEWINDOW))
838 if (!IsRectEmpty( &oldWindowRect ))
840 XUnmapWindow( display, get_whole_window(wndPtr) );
841 TRACE( "unmapping win %x\n", winpos->hwnd );
843 else TRACE( "not unmapping zero size win %x\n", winpos->hwnd );
845 else if ((wndPtr->dwStyle & WS_VISIBLE) &&
846 !IsRectEmpty( &oldWindowRect ) && IsRectEmpty( &newWindowRect ))
848 /* resizing to zero size -> unmap */
849 TRACE( "unmapping zero size win %x\n", winpos->hwnd );
850 XUnmapWindow( display, get_whole_window(wndPtr) );
854 X11DRV_sync_whole_window_position( display, wndPtr, !(winpos->flags & SWP_NOZORDER) );
857 struct x11drv_win_data *data = wndPtr->pDriverData;
858 data->whole_rect = wndPtr->rectWindow;
859 X11DRV_window_to_X_rect( wndPtr, &data->whole_rect );
862 if (X11DRV_sync_client_window_position( display, wndPtr ) ||
863 (winpos->flags & SWP_FRAMECHANGED))
865 /* if we moved the client area, repaint the whole non-client window */
866 XClearArea( display, get_whole_window(wndPtr), 0, 0, 0, 0, True );
867 winpos->flags |= SWP_FRAMECHANGED;
869 if (winpos->flags & SWP_SHOWWINDOW)
871 if (!IsRectEmpty( &newWindowRect ))
873 XMapWindow( display, get_whole_window(wndPtr) );
874 TRACE( "mapping win %x\n", winpos->hwnd );
876 else TRACE( "not mapping win %x, size is zero\n", winpos->hwnd );
878 else if ((wndPtr->dwStyle & WS_VISIBLE) &&
879 IsRectEmpty( &oldWindowRect ) && !IsRectEmpty( &newWindowRect ))
881 /* resizing from zero size to non-zero -> map */
882 TRACE( "mapping non zero size win %x\n", winpos->hwnd );
883 XMapWindow( display, get_whole_window(wndPtr) );
885 XFlush( display ); /* FIXME: should not be necessary */
889 /* manually expose the areas that X won't expose because they are still covered by something */
891 if (!(winpos->flags & SWP_SHOWWINDOW))
892 expose_covered_parent_area( wndPtr, &oldWindowRect );
894 if (wndPtr->dwStyle & WS_VISIBLE)
895 expose_covered_window_area( wndPtr, &oldClientRect, winpos->flags & SWP_FRAMECHANGED );
897 WIN_ReleaseWndPtr(wndPtr);
899 if (wvrFlags & WVR_REDRAW) RedrawWindow( winpos->hwnd, NULL, 0, RDW_INVALIDATE | RDW_ERASE );
901 if (winpos->hwnd == CARET_GetHwnd())
903 if( winpos->flags & SWP_HIDEWINDOW )
904 HideCaret(winpos->hwnd);
905 else if (winpos->flags & SWP_SHOWWINDOW)
906 ShowCaret(winpos->hwnd);
909 if (!(winpos->flags & SWP_NOACTIVATE))
910 WINPOS_ChangeActiveWindow( winpos->hwnd, FALSE );
912 /* And last, send the WM_WINDOWPOSCHANGED message */
914 TRACE("\tstatus flags = %04x\n", winpos->flags & SWP_AGG_STATUSFLAGS);
916 if (((winpos->flags & SWP_AGG_STATUSFLAGS) != SWP_AGG_NOPOSCHANGE) &&
917 !(winpos->flags & SWP_NOSENDCHANGING))
918 SendMessageA( winpos->hwnd, WM_WINDOWPOSCHANGED, 0, (LPARAM)winpos );
924 /***********************************************************************
927 * Find a suitable place for an iconic window.
929 static POINT WINPOS_FindIconPos( WND* wndPtr, POINT pt )
933 short x, y, xspacing, yspacing;
935 GetClientRect( wndPtr->parent, &rectParent );
936 if ((pt.x >= rectParent.left) && (pt.x + GetSystemMetrics(SM_CXICON) < rectParent.right) &&
937 (pt.y >= rectParent.top) && (pt.y + GetSystemMetrics(SM_CYICON) < rectParent.bottom))
938 return pt; /* The icon already has a suitable position */
940 xspacing = GetSystemMetrics(SM_CXICONSPACING);
941 yspacing = GetSystemMetrics(SM_CYICONSPACING);
943 list = WIN_ListChildren( wndPtr->parent );
944 y = rectParent.bottom;
950 /* Check if another icon already occupies this spot */
951 /* FIXME: this is completely inefficient */
957 for (i = 0; list[i]; i++)
959 if (list[i] == wndPtr->hwndSelf) continue;
960 if (!IsIconic( list[i] )) continue;
961 if (!(childPtr = WIN_FindWndPtr( list[i] ))) continue;
962 if ((childPtr->rectWindow.left < x + xspacing) &&
963 (childPtr->rectWindow.right >= x) &&
964 (childPtr->rectWindow.top <= y) &&
965 (childPtr->rectWindow.bottom > y - yspacing))
967 WIN_ReleaseWndPtr( childPtr );
968 break; /* There's a window in there */
970 WIN_ReleaseWndPtr( childPtr );
974 /* found something here, try next spot */
980 /* No window was found, so it's OK for us */
981 pt.x = x + (xspacing - GetSystemMetrics(SM_CXICON)) / 2;
982 pt.y = y - (yspacing + GetSystemMetrics(SM_CYICON)) / 2;
985 } while(x <= rectParent.right-xspacing);
994 UINT WINPOS_MinMaximize( HWND hwnd, UINT cmd, LPRECT rect )
996 WND *wndPtr = WIN_FindWndPtr( hwnd );
1001 TRACE("0x%04x %u\n", hwnd, cmd );
1003 wpl.length = sizeof(wpl);
1004 GetWindowPlacement( hwnd, &wpl );
1006 size.x = wndPtr->rectWindow.left;
1007 size.y = wndPtr->rectWindow.top;
1009 if (!HOOK_CallHooksA(WH_CBT, HCBT_MINMAX, (WPARAM)wndPtr->hwndSelf, cmd))
1011 if( wndPtr->dwStyle & WS_MINIMIZE )
1013 if( !SendMessageA( wndPtr->hwndSelf, WM_QUERYOPEN, 0, 0L ) )
1015 swpFlags = SWP_NOSIZE | SWP_NOMOVE;
1018 swpFlags |= SWP_NOCOPYBITS;
1023 if( wndPtr->dwStyle & WS_MAXIMIZE)
1025 wndPtr->flags |= WIN_RESTORE_MAX;
1026 wndPtr->dwStyle &= ~WS_MAXIMIZE;
1029 wndPtr->flags &= ~WIN_RESTORE_MAX;
1030 wndPtr->dwStyle |= WS_MINIMIZE;
1032 X11DRV_set_iconic_state( wndPtr );
1034 wpl.ptMinPosition = WINPOS_FindIconPos( wndPtr, wpl.ptMinPosition );
1036 SetRect( rect, wpl.ptMinPosition.x, wpl.ptMinPosition.y,
1037 GetSystemMetrics(SM_CXICON), GetSystemMetrics(SM_CYICON) );
1038 swpFlags |= SWP_NOCOPYBITS;
1042 WINPOS_GetMinMaxInfo( hwnd, &size, &wpl.ptMaxPosition, NULL, NULL );
1044 if( wndPtr->dwStyle & WS_MINIMIZE )
1046 wndPtr->dwStyle &= ~WS_MINIMIZE;
1047 WINPOS_ShowIconTitle( hwnd, FALSE );
1048 X11DRV_set_iconic_state( wndPtr );
1050 wndPtr->dwStyle |= WS_MAXIMIZE;
1052 SetRect( rect, wpl.ptMaxPosition.x, wpl.ptMaxPosition.y, size.x, size.y );
1056 if( wndPtr->dwStyle & WS_MINIMIZE )
1058 wndPtr->dwStyle &= ~WS_MINIMIZE;
1059 WINPOS_ShowIconTitle( hwnd, FALSE );
1060 X11DRV_set_iconic_state( wndPtr );
1062 if( wndPtr->flags & WIN_RESTORE_MAX)
1064 /* Restore to maximized position */
1065 WINPOS_GetMinMaxInfo( hwnd, &size, &wpl.ptMaxPosition, NULL, NULL);
1066 wndPtr->dwStyle |= WS_MAXIMIZE;
1067 SetRect( rect, wpl.ptMaxPosition.x, wpl.ptMaxPosition.y, size.x, size.y );
1072 if( !(wndPtr->dwStyle & WS_MAXIMIZE) )
1074 swpFlags = (UINT16)(-1);
1077 else wndPtr->dwStyle &= ~WS_MAXIMIZE;
1079 /* Restore to normal position */
1081 *rect = wpl.rcNormalPosition;
1082 rect->right -= rect->left;
1083 rect->bottom -= rect->top;
1087 } else swpFlags |= SWP_NOSIZE | SWP_NOMOVE;
1090 WIN_ReleaseWndPtr( wndPtr );
1095 /***********************************************************************
1096 * ShowWindow (X11DRV.@)
1098 BOOL X11DRV_ShowWindow( HWND hwnd, INT cmd )
1100 WND* wndPtr = WIN_FindWndPtr( hwnd );
1101 BOOL wasVisible, showFlag;
1102 RECT newPos = {0, 0, 0, 0};
1105 if (!wndPtr) return FALSE;
1106 hwnd = wndPtr->hwndSelf; /* make it a full handle */
1108 TRACE("hwnd=%04x, cmd=%d\n", hwnd, cmd);
1110 wasVisible = (wndPtr->dwStyle & WS_VISIBLE) != 0;
1115 if (!wasVisible) goto END;;
1116 swp |= SWP_HIDEWINDOW | SWP_NOSIZE | SWP_NOMOVE |
1117 SWP_NOACTIVATE | SWP_NOZORDER;
1120 case SW_SHOWMINNOACTIVE:
1121 swp |= SWP_NOACTIVATE | SWP_NOZORDER;
1123 case SW_SHOWMINIMIZED:
1124 swp |= SWP_SHOWWINDOW;
1127 swp |= SWP_FRAMECHANGED;
1128 if( !(wndPtr->dwStyle & WS_MINIMIZE) )
1129 swp |= WINPOS_MinMaximize( hwnd, SW_MINIMIZE, &newPos );
1130 else swp |= SWP_NOSIZE | SWP_NOMOVE;
1133 case SW_SHOWMAXIMIZED: /* same as SW_MAXIMIZE */
1134 swp |= SWP_SHOWWINDOW | SWP_FRAMECHANGED;
1135 if( !(wndPtr->dwStyle & WS_MAXIMIZE) )
1136 swp |= WINPOS_MinMaximize( hwnd, SW_MAXIMIZE, &newPos );
1137 else swp |= SWP_NOSIZE | SWP_NOMOVE;
1141 swp |= SWP_NOACTIVATE | SWP_NOZORDER;
1144 swp |= SWP_SHOWWINDOW | SWP_NOSIZE | SWP_NOMOVE;
1147 * ShowWindow has a little peculiar behavior that if the
1148 * window is already the topmost window, it will not
1151 if (GetTopWindow((HWND)0)==hwnd && (wasVisible || GetActiveWindow() == hwnd))
1152 swp |= SWP_NOACTIVATE;
1156 case SW_SHOWNOACTIVATE:
1157 swp |= SWP_NOZORDER;
1158 if (GetActiveWindow()) swp |= SWP_NOACTIVATE;
1160 case SW_SHOWNORMAL: /* same as SW_NORMAL: */
1161 case SW_SHOWDEFAULT: /* FIXME: should have its own handler */
1163 swp |= SWP_SHOWWINDOW | SWP_FRAMECHANGED;
1165 if( wndPtr->dwStyle & (WS_MINIMIZE | WS_MAXIMIZE) )
1166 swp |= WINPOS_MinMaximize( hwnd, SW_RESTORE, &newPos );
1167 else swp |= SWP_NOSIZE | SWP_NOMOVE;
1171 showFlag = (cmd != SW_HIDE);
1172 if (showFlag != wasVisible)
1174 SendMessageA( hwnd, WM_SHOWWINDOW, showFlag, 0 );
1175 if (!IsWindow( hwnd )) goto END;
1178 /* We can't activate a child window */
1179 if ((wndPtr->dwStyle & WS_CHILD) &&
1180 !(wndPtr->dwExStyle & WS_EX_MDICHILD))
1181 swp |= SWP_NOACTIVATE | SWP_NOZORDER;
1183 SetWindowPos( hwnd, HWND_TOP, newPos.left, newPos.top,
1184 newPos.right, newPos.bottom, LOWORD(swp) );
1187 /* FIXME: This will cause the window to be activated irrespective
1188 * of whether it is owned by the same thread. Has to be done
1192 if (hwnd == GetActiveWindow())
1193 WINPOS_ActivateOtherWindow(hwnd);
1195 /* Revert focus to parent */
1196 if (hwnd == GetFocus() || IsChild(hwnd, GetFocus()))
1197 SetFocus( GetParent(hwnd) );
1199 if (!IsWindow( hwnd )) goto END;
1200 else if( wndPtr->dwStyle & WS_MINIMIZE ) WINPOS_ShowIconTitle( hwnd, TRUE );
1202 if (wndPtr->flags & WIN_NEED_SIZE)
1204 /* should happen only in CreateWindowEx() */
1205 int wParam = SIZE_RESTORED;
1207 wndPtr->flags &= ~WIN_NEED_SIZE;
1208 if (wndPtr->dwStyle & WS_MAXIMIZE) wParam = SIZE_MAXIMIZED;
1209 else if (wndPtr->dwStyle & WS_MINIMIZE) wParam = SIZE_MINIMIZED;
1210 SendMessageA( hwnd, WM_SIZE, wParam,
1211 MAKELONG(wndPtr->rectClient.right-wndPtr->rectClient.left,
1212 wndPtr->rectClient.bottom-wndPtr->rectClient.top));
1213 SendMessageA( hwnd, WM_MOVE, 0,
1214 MAKELONG(wndPtr->rectClient.left, wndPtr->rectClient.top) );
1218 WIN_ReleaseWndPtr(wndPtr);
1223 /**********************************************************************
1226 void X11DRV_MapNotify( HWND hwnd, XMapEvent *event )
1228 HWND hwndFocus = GetFocus();
1231 if (!(win = WIN_FindWndPtr( hwnd ))) return;
1233 if ((win->dwStyle & WS_VISIBLE) &&
1234 (win->dwStyle & WS_MINIMIZE) &&
1235 (win->dwExStyle & WS_EX_MANAGED))
1238 unsigned int width, height, border, depth;
1242 DCE_InvalidateDCE( hwnd, &win->rectWindow );
1243 win->dwStyle &= ~WS_MINIMIZE;
1244 win->dwStyle |= WS_VISIBLE;
1245 WIN_InternalShowOwnedPopups( hwnd, TRUE, TRUE );
1247 if (win->flags & WIN_RESTORE_MAX)
1248 win->dwStyle |= WS_MAXIMIZE;
1250 win->dwStyle &= ~WS_MAXIMIZE;
1254 XGetGeometry( event->display, get_whole_window(win), &root, &x, &y, &width, &height,
1256 XTranslateCoordinates( event->display, get_whole_window(win), root, 0, 0, &x, &y, &top );
1257 wine_tsx11_unlock();
1260 rect.right = x + width;
1261 rect.bottom = y + height;
1262 X11DRV_X_to_window_rect( win, &rect );
1264 SendMessageA( hwnd, WM_SHOWWINDOW, SW_RESTORE, 0 );
1265 SetWindowPos( hwnd, 0, rect.left, rect.top, rect.right-rect.left, rect.bottom-rect.top,
1266 SWP_NOZORDER | SWP_WINE_NOHOSTMOVE );
1268 if (hwndFocus && IsChild( hwnd, hwndFocus )) X11DRV_SetFocus(hwndFocus); /* FIXME */
1269 WIN_ReleaseWndPtr( win );
1273 /**********************************************************************
1274 * X11DRV_UnmapNotify
1276 void X11DRV_UnmapNotify( HWND hwnd, XUnmapEvent *event )
1280 if (!(win = WIN_FindWndPtr( hwnd ))) return;
1282 if ((win->dwStyle & WS_VISIBLE) && (win->dwExStyle & WS_EX_MANAGED))
1285 SendMessageA( hwnd, WM_SHOWWINDOW, SW_MINIMIZE, 0 );
1287 win->flags &= ~WIN_RESTORE_MAX;
1288 win->dwStyle |= WS_MINIMIZE;
1290 if (win->dwStyle & WS_MAXIMIZE)
1292 win->flags |= WIN_RESTORE_MAX;
1293 win->dwStyle &= ~WS_MAXIMIZE;
1296 SetWindowPos( hwnd, 0, 0, 0, GetSystemMetrics(SM_CXICON), GetSystemMetrics(SM_CYICON),
1297 SWP_NOMOVE | SWP_NOACTIVATE | SWP_NOZORDER | SWP_WINE_NOHOSTMOVE );
1299 WIN_InternalShowOwnedPopups( hwnd, FALSE, TRUE );
1301 WIN_ReleaseWndPtr( win );
1305 /***********************************************************************
1308 * Synchronize internal z-order with the window manager's.
1310 static Window __get_common_ancestor( Display *display, Window A, Window B,
1311 Window** children, unsigned* total )
1313 /* find the real root window */
1315 Window root, *childrenB;
1318 while( A != B && A && B )
1320 TSXQueryTree( display, A, &root, &A, children, total );
1321 TSXQueryTree( display, B, &root, &B, &childrenB, &totalB );
1322 if( childrenB ) TSXFree( childrenB );
1323 if( *children ) TSXFree( *children ), *children = NULL;
1328 TSXQueryTree( display, A, &root, &B, children, total );
1334 static Window __get_top_decoration( Display *display, Window w, Window ancestor )
1336 Window* children, root, prev = w, parent = w;
1342 TSXQueryTree( display, w, &root, &parent, &children, &total );
1343 if( children ) TSXFree( children );
1344 } while( parent && parent != ancestor );
1345 TRACE("\t%08x -> %08x\n", (unsigned)prev, (unsigned)w );
1346 return ( parent ) ? w : 0 ;
1349 static unsigned __td_lookup( Window w, Window* list, unsigned max )
1352 for( i = max - 1; i >= 0; i-- ) if( list[i] == w ) break;
1356 static HWND query_zorder( Display *display, HWND hWndCheck)
1358 HWND hwndInsertAfter = HWND_TOP;
1359 Window w, parent, *children = NULL;
1360 unsigned total, check, pos, best;
1361 HWND *list = WIN_ListChildren( GetDesktopWindow() );
1362 HWND hwndA = 0, hwndB = 0;
1366 /* find at least two managed windows */
1367 if (!list) return 0;
1368 for (i = 0; list[i]; i++)
1370 if (!(win = WIN_FindWndPtr( list[i] ))) continue;
1371 if ((win->dwExStyle & WS_EX_MANAGED) && (win->dwStyle & WS_VISIBLE))
1373 if (!hwndA) hwndA = list[i];
1377 WIN_ReleaseWndPtr( win );
1381 WIN_ReleaseWndPtr( win );
1383 if (!hwndA || !hwndB) goto done;
1385 parent = __get_common_ancestor( display, X11DRV_get_whole_window(hwndA),
1386 X11DRV_get_whole_window(hwndB), &children, &total );
1387 if( parent && children )
1389 /* w is the ancestor if hWndCheck that is a direct descendant of 'parent' */
1391 w = __get_top_decoration( display, X11DRV_get_whole_window(hWndCheck), parent );
1393 if( w != children[total-1] ) /* check if at the top */
1395 /* X child at index 0 is at the bottom, at index total-1 is at the top */
1396 check = __td_lookup( w, children, total );
1399 /* go through all windows in Wine z-order... */
1400 for (i = 0; list[i]; i++)
1402 if (list[i] == hWndCheck) continue;
1403 if (!(GetWindowLongW( list[i], GWL_EXSTYLE ) & WS_EX_MANAGED)) continue;
1404 if (!(w = __get_top_decoration( display, X11DRV_get_whole_window(list[i]),
1405 parent ))) continue;
1406 pos = __td_lookup( w, children, total );
1407 if( pos < best && pos > check )
1409 /* find a nearest Wine window precedes hWndCheck in the real z-order */
1411 hwndInsertAfter = list[i];
1413 if( best - check == 1 ) break;
1417 if( children ) TSXFree( children );
1420 HeapFree( GetProcessHeap(), 0, list );
1421 return hwndInsertAfter;
1425 /***********************************************************************
1426 * X11DRV_ConfigureNotify
1428 void X11DRV_ConfigureNotify( HWND hwnd, XConfigureEvent *event )
1430 HWND oldInsertAfter;
1431 struct x11drv_win_data *data;
1435 int x = event->x, y = event->y;
1437 if (!(win = WIN_FindWndPtr( hwnd ))) return;
1438 data = win->pDriverData;
1442 if (!event->send_event) /* normal event, need to map coordinates to the root */
1446 XTranslateCoordinates( event->display, data->whole_window, root_window,
1447 0, 0, &x, &y, &child );
1448 wine_tsx11_unlock();
1452 rect.right = x + event->width;
1453 rect.bottom = y + event->height;
1454 TRACE( "win %x new X rect %d,%d,%dx%d (event %d,%d,%dx%d)\n",
1455 hwnd, rect.left, rect.top, rect.right-rect.left, rect.bottom-rect.top,
1456 event->x, event->y, event->width, event->height );
1457 X11DRV_X_to_window_rect( win, &rect );
1458 WIN_ReleaseWndPtr( win );
1461 winpos.x = rect.left;
1462 winpos.y = rect.top;
1463 winpos.cx = rect.right - rect.left;
1464 winpos.cy = rect.bottom - rect.top;
1465 winpos.flags = SWP_NOACTIVATE;
1467 /* Get Z-order (FIXME) */
1469 winpos.hwndInsertAfter = query_zorder( event->display, hwnd );
1471 /* needs to find the first Visible Window above the current one */
1472 oldInsertAfter = hwnd;
1475 oldInsertAfter = GetWindow( oldInsertAfter, GW_HWNDPREV );
1476 if (!oldInsertAfter)
1478 oldInsertAfter = HWND_TOP;
1481 if (GetWindowLongA( oldInsertAfter, GWL_STYLE ) & WS_VISIBLE) break;
1484 /* Compare what has changed */
1486 GetWindowRect( hwnd, &rect );
1487 if (rect.left == winpos.x && rect.top == winpos.y) winpos.flags |= SWP_NOMOVE;
1489 TRACE( "%04x moving from (%d,%d) to (%d,%d)\n",
1490 hwnd, rect.left, rect.top, winpos.x, winpos.y );
1492 if ((rect.right - rect.left == winpos.cx && rect.bottom - rect.top == winpos.cy) ||
1494 (IsRectEmpty( &rect ) && winpos.cx == 1 && winpos.cy == 1))
1495 winpos.flags |= SWP_NOSIZE;
1497 TRACE( "%04x resizing from (%dx%d) to (%dx%d)\n",
1498 hwnd, rect.right - rect.left, rect.bottom - rect.top,
1499 winpos.cx, winpos.cy );
1501 if (winpos.hwndInsertAfter == oldInsertAfter) winpos.flags |= SWP_NOZORDER;
1503 TRACE( "%04x restacking from after %04x to after %04x\n",
1504 hwnd, oldInsertAfter, winpos.hwndInsertAfter );
1506 /* if nothing changed, don't do anything */
1507 if (winpos.flags == (SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE)) return;
1509 SetWindowPos( hwnd, winpos.hwndInsertAfter, winpos.x, winpos.y,
1510 winpos.cx, winpos.cy, winpos.flags | SWP_WINE_NOHOSTMOVE );
1514 /***********************************************************************
1515 * SetWindowRgn (X11DRV.@)
1517 * Assign specified region to window (for non-rectangular windows)
1519 BOOL X11DRV_SetWindowRgn( HWND hwnd, HRGN hrgn, BOOL redraw )
1522 WND *wndPtr = WIN_FindWndPtr(hwnd);
1525 if (!wndPtr) return FALSE;
1527 if (wndPtr->hrgnWnd == hrgn)
1533 if (hrgn) /* verify that region really exists */
1535 if (GetRgnBox( hrgn, &rect ) == ERROR) goto done;
1538 if (wndPtr->hrgnWnd)
1540 /* delete previous region */
1541 DeleteObject(wndPtr->hrgnWnd);
1542 wndPtr->hrgnWnd = 0;
1544 wndPtr->hrgnWnd = hrgn;
1546 #ifdef HAVE_LIBXSHAPE
1548 Display *display = thread_display();
1549 X11DRV_WND_DATA *data = wndPtr->pDriverData;
1551 if (data->whole_window)
1555 TSXShapeCombineMask( display, data->whole_window,
1556 ShapeBounding, 0, 0, None, ShapeSet );
1561 int x_offset, y_offset;
1563 DWORD dwBufferSize = GetRegionData(hrgn, 0, NULL);
1564 PRGNDATA pRegionData = HeapAlloc(GetProcessHeap(), 0, dwBufferSize);
1565 if (!pRegionData) goto done;
1567 GetRegionData(hrgn, dwBufferSize, pRegionData);
1568 size = pRegionData->rdh.nCount;
1569 x_offset = wndPtr->rectWindow.left - data->whole_rect.left;
1570 y_offset = wndPtr->rectWindow.top - data->whole_rect.top;
1571 /* convert region's "Windows rectangles" to XRectangles */
1572 aXRect = HeapAlloc(GetProcessHeap(), 0, size * sizeof(*aXRect) );
1575 XRectangle* pCurrRect = aXRect;
1576 RECT *pRect = (RECT*) pRegionData->Buffer;
1577 for (; pRect < ((RECT*) pRegionData->Buffer) + size ; ++pRect, ++pCurrRect)
1579 pCurrRect->x = pRect->left + x_offset;
1580 pCurrRect->y = pRect->top + y_offset;
1581 pCurrRect->height = pRect->bottom - pRect->top;
1582 pCurrRect->width = pRect->right - pRect->left;
1584 TRACE("Rectangle %04d of %04ld data: X=%04d, Y=%04d, Height=%04d, Width=%04d.\n",
1585 pRect - (RECT*) pRegionData->Buffer,
1593 /* shape = non-rectangular windows (X11/extensions) */
1594 TSXShapeCombineRectangles( display, data->whole_window, ShapeBounding,
1596 pCurrRect - aXRect, ShapeSet, YXBanded );
1597 HeapFree(GetProcessHeap(), 0, aXRect );
1599 HeapFree(GetProcessHeap(), 0, pRegionData);
1603 #endif /* HAVE_LIBXSHAPE */
1605 if (redraw) RedrawWindow( hwnd, NULL, 0, RDW_FRAME | RDW_INVALIDATE | RDW_ERASE );
1609 WIN_ReleaseWndPtr(wndPtr);
1614 /***********************************************************************
1617 * Draw the frame used when moving or resizing window.
1619 * FIXME: This causes problems in Win95 mode. (why?)
1621 static void draw_moving_frame( HDC hdc, RECT *rect, BOOL thickframe )
1625 const int width = GetSystemMetrics(SM_CXFRAME);
1626 const int height = GetSystemMetrics(SM_CYFRAME);
1628 HBRUSH hbrush = SelectObject( hdc, GetStockObject( GRAY_BRUSH ) );
1629 PatBlt( hdc, rect->left, rect->top,
1630 rect->right - rect->left - width, height, PATINVERT );
1631 PatBlt( hdc, rect->left, rect->top + height, width,
1632 rect->bottom - rect->top - height, PATINVERT );
1633 PatBlt( hdc, rect->left + width, rect->bottom - 1,
1634 rect->right - rect->left - width, -height, PATINVERT );
1635 PatBlt( hdc, rect->right - 1, rect->top, -width,
1636 rect->bottom - rect->top - height, PATINVERT );
1637 SelectObject( hdc, hbrush );
1639 else DrawFocusRect( hdc, rect );
1643 /***********************************************************************
1646 * Initialisation of a move or resize, when initiatied from a menu choice.
1647 * Return hit test code for caption or sizing border.
1649 static LONG start_size_move( HWND hwnd, WPARAM wParam, POINT *capturePoint, LONG style )
1656 GetWindowRect( hwnd, &rectWindow );
1658 if ((wParam & 0xfff0) == SC_MOVE)
1660 /* Move pointer at the center of the caption */
1662 NC_GetInsideRect( hwnd, &rect );
1663 if (style & WS_SYSMENU)
1664 rect.left += GetSystemMetrics(SM_CXSIZE) + 1;
1665 if (style & WS_MINIMIZEBOX)
1666 rect.right -= GetSystemMetrics(SM_CXSIZE) + 1;
1667 if (style & WS_MAXIMIZEBOX)
1668 rect.right -= GetSystemMetrics(SM_CXSIZE) + 1;
1669 pt.x = rectWindow.left + (rect.right - rect.left) / 2;
1670 pt.y = rectWindow.top + rect.top + GetSystemMetrics(SM_CYSIZE)/2;
1671 hittest = HTCAPTION;
1678 GetMessageW( &msg, 0, WM_KEYFIRST, WM_MOUSELAST );
1679 if (CallMsgFilterW( &msg, MSGF_SIZE )) continue;
1684 hittest = NC_HandleNCHitTest( hwnd, msg.pt );
1685 if ((hittest < HTLEFT) || (hittest > HTBOTTOMRIGHT))
1697 pt.x =(rectWindow.left+rectWindow.right)/2;
1698 pt.y = rectWindow.top + GetSystemMetrics(SM_CYFRAME) / 2;
1702 pt.x =(rectWindow.left+rectWindow.right)/2;
1703 pt.y = rectWindow.bottom - GetSystemMetrics(SM_CYFRAME) / 2;
1707 pt.x = rectWindow.left + GetSystemMetrics(SM_CXFRAME) / 2;
1708 pt.y =(rectWindow.top+rectWindow.bottom)/2;
1712 pt.x = rectWindow.right - GetSystemMetrics(SM_CXFRAME) / 2;
1713 pt.y =(rectWindow.top+rectWindow.bottom)/2;
1716 case VK_ESCAPE: return 0;
1722 SetCursorPos( pt.x, pt.y );
1723 NC_HandleSetCursor( hwnd, (WPARAM)hwnd, MAKELONG( hittest, WM_MOUSEMOVE ));
1728 /***********************************************************************
1729 * SysCommandSizeMove (X11DRV.@)
1731 * Perform SC_MOVE and SC_SIZE commands.
1733 void X11DRV_SysCommandSizeMove( HWND hwnd, WPARAM wParam )
1736 RECT sizingRect, mouseRect, origRect;
1739 LONG hittest = (LONG)(wParam & 0x0f);
1740 HCURSOR16 hDragCursor = 0, hOldCursor = 0;
1741 POINT minTrack, maxTrack;
1742 POINT capturePoint, pt;
1743 LONG style = GetWindowLongA( hwnd, GWL_STYLE );
1744 LONG exstyle = GetWindowLongA( hwnd, GWL_EXSTYLE );
1745 BOOL thickframe = HAS_THICKFRAME( style, exstyle );
1746 BOOL iconic = style & WS_MINIMIZE;
1748 DWORD dwPoint = GetMessagePos ();
1749 BOOL DragFullWindows = FALSE;
1752 Display *old_gdi_display = NULL;
1753 Display *display = thread_display();
1755 SystemParametersInfoA(SPI_GETDRAGFULLWINDOWS, 0, &DragFullWindows, 0);
1757 pt.x = SLOWORD(dwPoint);
1758 pt.y = SHIWORD(dwPoint);
1761 if (IsZoomed(hwnd) || !IsWindowVisible(hwnd) || (exstyle & WS_EX_MANAGED)) return;
1763 if ((wParam & 0xfff0) == SC_MOVE)
1765 if (!hittest) hittest = start_size_move( hwnd, wParam, &capturePoint, style );
1766 if (!hittest) return;
1770 if (!thickframe) return;
1771 if ( hittest && hittest != HTSYSMENU ) hittest += 2;
1775 hittest = start_size_move( hwnd, wParam, &capturePoint, style );
1784 /* Get min/max info */
1786 WINPOS_GetMinMaxInfo( hwnd, NULL, NULL, &minTrack, &maxTrack );
1787 GetWindowRect( hwnd, &sizingRect );
1788 if (style & WS_CHILD)
1790 parent = GetParent(hwnd);
1791 /* make sizing rect relative to parent */
1792 MapWindowPoints( 0, parent, (POINT*)&sizingRect, 2 );
1793 GetClientRect( parent, &mouseRect );
1798 SetRect(&mouseRect, 0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN));
1800 origRect = sizingRect;
1802 if (ON_LEFT_BORDER(hittest))
1804 mouseRect.left = max( mouseRect.left, sizingRect.right-maxTrack.x );
1805 mouseRect.right = min( mouseRect.right, sizingRect.right-minTrack.x );
1807 else if (ON_RIGHT_BORDER(hittest))
1809 mouseRect.left = max( mouseRect.left, sizingRect.left+minTrack.x );
1810 mouseRect.right = min( mouseRect.right, sizingRect.left+maxTrack.x );
1812 if (ON_TOP_BORDER(hittest))
1814 mouseRect.top = max( mouseRect.top, sizingRect.bottom-maxTrack.y );
1815 mouseRect.bottom = min( mouseRect.bottom,sizingRect.bottom-minTrack.y);
1817 else if (ON_BOTTOM_BORDER(hittest))
1819 mouseRect.top = max( mouseRect.top, sizingRect.top+minTrack.y );
1820 mouseRect.bottom = min( mouseRect.bottom, sizingRect.top+maxTrack.y );
1822 if (parent) MapWindowPoints( parent, 0, (LPPOINT)&mouseRect, 2 );
1824 /* Retrieve a default cache DC (without using the window style) */
1825 hdc = GetDCEx( parent, 0, DCX_CACHE );
1827 if( iconic ) /* create a cursor for dragging */
1829 HICON hIcon = GetClassLongA( hwnd, GCL_HICON);
1830 if(!hIcon) hIcon = (HICON)SendMessageA( hwnd, WM_QUERYDRAGICON, 0, 0L);
1831 if( hIcon ) hDragCursor = CURSORICON_IconToCursor( hIcon, TRUE );
1832 if( !hDragCursor ) iconic = FALSE;
1835 /* repaint the window before moving it around */
1836 RedrawWindow( hwnd, NULL, 0, RDW_UPDATENOW | RDW_ALLCHILDREN );
1838 SendMessageA( hwnd, WM_ENTERSIZEMOVE, 0, 0 );
1841 /* grab the server only when moving top-level windows without desktop */
1842 grab = (!DragFullWindows && !parent && (root_window == DefaultRootWindow(gdi_display)));
1847 XSync( gdi_display, False );
1848 XGrabServer( display );
1849 XSync( display, False );
1850 /* switch gdi display to the thread display, since the server is grabbed */
1851 old_gdi_display = gdi_display;
1852 gdi_display = display;
1854 XGrabPointer( display, X11DRV_get_whole_window(hwnd), False,
1855 PointerMotionMask | ButtonPressMask | ButtonReleaseMask,
1856 GrabModeAsync, GrabModeAsync,
1857 parent ? X11DRV_get_client_window(parent) : root_window,
1858 None, CurrentTime );
1859 wine_tsx11_unlock();
1865 if (!GetMessageW( &msg, 0, WM_KEYFIRST, WM_MOUSELAST )) break;
1866 if (CallMsgFilterW( &msg, MSGF_SIZE )) continue;
1868 /* Exit on button-up, Return, or Esc */
1869 if ((msg.message == WM_LBUTTONUP) ||
1870 ((msg.message == WM_KEYDOWN) &&
1871 ((msg.wParam == VK_RETURN) || (msg.wParam == VK_ESCAPE)))) break;
1873 if ((msg.message != WM_KEYDOWN) && (msg.message != WM_MOUSEMOVE))
1874 continue; /* We are not interested in other messages */
1878 if (msg.message == WM_KEYDOWN) switch(msg.wParam)
1880 case VK_UP: pt.y -= 8; break;
1881 case VK_DOWN: pt.y += 8; break;
1882 case VK_LEFT: pt.x -= 8; break;
1883 case VK_RIGHT: pt.x += 8; break;
1886 pt.x = max( pt.x, mouseRect.left );
1887 pt.x = min( pt.x, mouseRect.right );
1888 pt.y = max( pt.y, mouseRect.top );
1889 pt.y = min( pt.y, mouseRect.bottom );
1891 dx = pt.x - capturePoint.x;
1892 dy = pt.y - capturePoint.y;
1900 if( iconic ) /* ok, no system popup tracking */
1902 hOldCursor = SetCursor(hDragCursor);
1904 WINPOS_ShowIconTitle( hwnd, FALSE );
1906 else if(!DragFullWindows)
1907 draw_moving_frame( hdc, &sizingRect, thickframe );
1910 if (msg.message == WM_KEYDOWN) SetCursorPos( pt.x, pt.y );
1913 RECT newRect = sizingRect;
1914 WPARAM wpSizingHit = 0;
1916 if (hittest == HTCAPTION) OffsetRect( &newRect, dx, dy );
1917 if (ON_LEFT_BORDER(hittest)) newRect.left += dx;
1918 else if (ON_RIGHT_BORDER(hittest)) newRect.right += dx;
1919 if (ON_TOP_BORDER(hittest)) newRect.top += dy;
1920 else if (ON_BOTTOM_BORDER(hittest)) newRect.bottom += dy;
1921 if(!iconic && !DragFullWindows) draw_moving_frame( hdc, &sizingRect, thickframe );
1924 /* determine the hit location */
1925 if (hittest >= HTLEFT && hittest <= HTBOTTOMRIGHT)
1926 wpSizingHit = WMSZ_LEFT + (hittest - HTLEFT);
1927 SendMessageA( hwnd, WM_SIZING, wpSizingHit, (LPARAM)&newRect );
1931 if(!DragFullWindows)
1932 draw_moving_frame( hdc, &newRect, thickframe );
1934 /* To avoid any deadlocks, all the locks on the windows
1935 structures must be suspended before the SetWindowPos */
1936 iWndsLocks = WIN_SuspendWndsLock();
1937 SetWindowPos( hwnd, 0, newRect.left, newRect.top,
1938 newRect.right - newRect.left,
1939 newRect.bottom - newRect.top,
1940 ( hittest == HTCAPTION ) ? SWP_NOSIZE : 0 );
1941 WIN_RestoreWndsLock(iWndsLocks);
1944 sizingRect = newRect;
1952 if( moved ) /* restore cursors, show icon title later on */
1954 ShowCursor( FALSE );
1955 SetCursor( hOldCursor );
1957 DestroyCursor( hDragCursor );
1959 else if (moved && !DragFullWindows)
1960 draw_moving_frame( hdc, &sizingRect, thickframe );
1962 ReleaseDC( parent, hdc );
1965 XUngrabPointer( display, CurrentTime );
1968 XSync( display, False );
1969 XUngrabServer( display );
1970 XSync( display, False );
1971 gdi_display = old_gdi_display;
1973 wine_tsx11_unlock();
1975 if (HOOK_CallHooksA( WH_CBT, HCBT_MOVESIZE, (WPARAM)hwnd, (LPARAM)&sizingRect )) moved = FALSE;
1977 SendMessageA( hwnd, WM_EXITSIZEMOVE, 0, 0 );
1978 SendMessageA( hwnd, WM_SETVISIBLE, !IsIconic(hwnd), 0L);
1980 /* window moved or resized */
1983 /* To avoid any deadlocks, all the locks on the windows
1984 structures must be suspended before the SetWindowPos */
1985 iWndsLocks = WIN_SuspendWndsLock();
1987 /* if the moving/resizing isn't canceled call SetWindowPos
1988 * with the new position or the new size of the window
1990 if (!((msg.message == WM_KEYDOWN) && (msg.wParam == VK_ESCAPE)) )
1992 /* NOTE: SWP_NOACTIVATE prevents document window activation in Word 6 */
1993 if(!DragFullWindows)
1994 SetWindowPos( hwnd, 0, sizingRect.left, sizingRect.top,
1995 sizingRect.right - sizingRect.left,
1996 sizingRect.bottom - sizingRect.top,
1997 ( hittest == HTCAPTION ) ? SWP_NOSIZE : 0 );
2000 { /* restore previous size/position */
2002 SetWindowPos( hwnd, 0, origRect.left, origRect.top,
2003 origRect.right - origRect.left,
2004 origRect.bottom - origRect.top,
2005 ( hittest == HTCAPTION ) ? SWP_NOSIZE : 0 );
2008 WIN_RestoreWndsLock(iWndsLocks);
2013 /* Single click brings up the system menu when iconized */
2017 if(style & WS_SYSMENU )
2018 SendMessageA( hwnd, WM_SYSCOMMAND,
2019 SC_MOUSEMENU + HTSYSMENU, MAKELONG(pt.x,pt.y));
2021 else WINPOS_ShowIconTitle( hwnd, TRUE );
2026 /***********************************************************************
2027 * ForceWindowRaise (X11DRV.@)
2029 * Raise a window on top of the X stacking order, while preserving
2030 * the correct Windows Z order.
2032 * FIXME: this should go away.
2034 void X11DRV_ForceWindowRaise( HWND hwnd )
2038 XWindowChanges winChanges;
2039 Display *display = thread_display();
2040 WND *wndPtr = WIN_FindWndPtr( hwnd );
2042 if (!wndPtr) return;
2044 if ((wndPtr->dwExStyle & WS_EX_MANAGED) ||
2045 wndPtr->parent != GetDesktopWindow() ||
2046 IsRectEmpty( &wndPtr->rectWindow ) ||
2047 !get_whole_window(wndPtr))
2049 WIN_ReleaseWndPtr( wndPtr );
2052 WIN_ReleaseWndPtr( wndPtr );
2054 /* Raise all windows up to wndPtr according to their Z order.
2055 * (it would be easier with sibling-related Below but it doesn't
2056 * work very well with SGI mwm for instance)
2058 winChanges.stack_mode = Above;
2059 if (!(list = WIN_ListChildren( GetDesktopWindow() ))) return;
2060 while (list[i] && list[i] != hwnd) i++;
2063 for ( ; i >= 0; i--)
2065 WND *ptr = WIN_FindWndPtr( list[i] );
2067 if (!IsRectEmpty( &ptr->rectWindow ) && get_whole_window(ptr))
2068 TSXReconfigureWMWindow( display, get_whole_window(ptr), 0,
2069 CWStackMode, &winChanges );
2070 WIN_ReleaseWndPtr( ptr );
2073 HeapFree( GetProcessHeap(), 0, list );