2 * Window position related functions.
4 * Copyright 1993, 1994, 1995, 2001 Alexandre Julliard
5 * Copyright 1995, 1996, 1999 Alex Korobka
25 #include "cursoricon.h"
26 #include "nonclient.h"
29 #include "debugtools.h"
31 DEFAULT_DEBUG_CHANNEL(x11drv);
33 #define SWP_AGG_NOGEOMETRYCHANGE \
34 (SWP_NOSIZE | SWP_NOMOVE | SWP_NOCLIENTSIZE | SWP_NOCLIENTMOVE)
35 #define SWP_AGG_NOPOSCHANGE \
36 (SWP_AGG_NOGEOMETRYCHANGE | SWP_NOZORDER)
37 #define SWP_AGG_STATUSFLAGS \
38 (SWP_AGG_NOPOSCHANGE | SWP_FRAMECHANGED | SWP_HIDEWINDOW | SWP_SHOWWINDOW)
40 #define SWP_EX_NOCOPY 0x0001
41 #define SWP_EX_PAINTSELF 0x0002
42 #define SWP_EX_NONCLIENT 0x0004
44 #define HAS_THICKFRAME(style,exStyle) \
45 (((style) & WS_THICKFRAME) && \
46 !(((style) & (WS_DLGFRAME|WS_BORDER)) == WS_DLGFRAME))
48 #define ON_LEFT_BORDER(hit) \
49 (((hit) == HTLEFT) || ((hit) == HTTOPLEFT) || ((hit) == HTBOTTOMLEFT))
50 #define ON_RIGHT_BORDER(hit) \
51 (((hit) == HTRIGHT) || ((hit) == HTTOPRIGHT) || ((hit) == HTBOTTOMRIGHT))
52 #define ON_TOP_BORDER(hit) \
53 (((hit) == HTTOP) || ((hit) == HTTOPLEFT) || ((hit) == HTTOPRIGHT))
54 #define ON_BOTTOM_BORDER(hit) \
55 (((hit) == HTBOTTOM) || ((hit) == HTBOTTOMLEFT) || ((hit) == HTBOTTOMRIGHT))
58 /***********************************************************************
61 * Clip all children of a given window out of the visible region
63 static int clip_children( HWND parent, HWND last, HRGN hrgn, int whole_window )
68 int i, x, y, ret = SIMPLEREGION;
70 /* first check if we have anything to do */
71 if (!(list = WIN_ListChildren( parent ))) return ret;
72 for (i = 0; list[i] && list[i] != last; i++)
73 if (GetWindowLongW( list[i], GWL_STYLE ) & WS_VISIBLE) break;
74 if (!list[i] || list[i] == last) goto done; /* no children to clip */
78 WND *win = WIN_FindWndPtr( parent );
79 x = win->rectWindow.left - win->rectClient.left;
80 y = win->rectWindow.top - win->rectClient.top;
81 WIN_ReleaseWndPtr( win );
85 rectRgn = CreateRectRgn( 0, 0, 0, 0 );
87 for ( ; list[i] && list[i] != last; i++)
89 if (!(ptr = WIN_FindWndPtr( list[i] ))) continue;
90 if (ptr->dwStyle & WS_VISIBLE)
92 SetRectRgn( rectRgn, ptr->rectWindow.left + x, ptr->rectWindow.top + y,
93 ptr->rectWindow.right + x, ptr->rectWindow.bottom + y );
94 if ((ret = CombineRgn( hrgn, hrgn, rectRgn, RGN_DIFF )) == NULLREGION)
96 WIN_ReleaseWndPtr( ptr );
97 break; /* no need to go on, region is empty */
100 WIN_ReleaseWndPtr( ptr );
102 DeleteObject( rectRgn );
104 HeapFree( GetProcessHeap(), 0, list );
109 /***********************************************************************
112 * Compute the visible region of a window
114 static HRGN get_visible_region( WND *win, HWND top, UINT flags, int mode )
118 int xoffset, yoffset;
119 X11DRV_WND_DATA *data = win->pDriverData;
121 if (flags & DCX_WINDOW)
123 xoffset = win->rectWindow.left;
124 yoffset = win->rectWindow.top;
128 xoffset = win->rectClient.left;
129 yoffset = win->rectClient.top;
132 if (flags & DCX_PARENTCLIP)
133 GetClientRect( win->parent, &rect );
134 else if (flags & DCX_WINDOW)
135 rect = data->whole_rect;
137 rect = win->rectClient;
139 /* vis region is relative to the start of the client/window area */
140 OffsetRect( &rect, -xoffset, -yoffset );
142 if (!(rgn = CreateRectRgn( rect.left, rect.top, rect.right, rect.bottom ))) return 0;
144 if ((flags & DCX_CLIPCHILDREN) && (mode != ClipByChildren))
146 /* we need to clip children by hand */
147 if (clip_children( win->hwndSelf, 0, rgn, (flags & DCX_WINDOW) ) == NULLREGION) return rgn;
150 if (top && top != win->hwndSelf) /* need to clip siblings of ancestors */
152 WND *parent, *ptr = WIN_FindWndPtr( win->hwndSelf );
155 OffsetRgn( rgn, xoffset, yoffset );
158 if (ptr->dwStyle & WS_CLIPSIBLINGS)
160 if (clip_children( ptr->parent, ptr->hwndSelf, rgn, FALSE ) == NULLREGION) break;
162 if (ptr->hwndSelf == top) break;
163 if (!(parent = WIN_FindWndPtr( ptr->parent ))) break;
164 WIN_ReleaseWndPtr( ptr );
166 /* clip to parent client area */
167 if (tmp) SetRectRgn( tmp, 0, 0, ptr->rectClient.right - ptr->rectClient.left,
168 ptr->rectClient.bottom - ptr->rectClient.top );
169 else tmp = CreateRectRgn( 0, 0, ptr->rectClient.right - ptr->rectClient.left,
170 ptr->rectClient.bottom - ptr->rectClient.top );
171 CombineRgn( rgn, rgn, tmp, RGN_AND );
172 OffsetRgn( rgn, ptr->rectClient.left, ptr->rectClient.top );
173 xoffset += ptr->rectClient.left;
174 yoffset += ptr->rectClient.top;
176 WIN_ReleaseWndPtr( ptr );
177 /* make it relative to the target window again */
178 OffsetRgn( rgn, -xoffset, -yoffset );
179 if (tmp) DeleteObject( tmp );
186 /***********************************************************************
189 * Compute the portion of 'rgn' that is covered by non-clipped siblings.
190 * This is the area that is covered from X point of view, but may still need
192 * 'rgn' must be relative to the client area of the parent of 'win'.
194 static int get_covered_region( WND *win, HRGN rgn )
198 WND *parent, *ptr = WIN_FindWndPtr( win->hwndSelf );
199 int xoffset = 0, yoffset = 0;
201 tmp = CreateRectRgn( 0, 0, 0, 0 );
202 CombineRgn( tmp, rgn, 0, RGN_COPY );
204 /* to make things easier we actually build the uncovered
205 * area by removing all siblings and then we subtract that
206 * from the total region to get the covered area */
209 if (!(ptr->dwStyle & WS_CLIPSIBLINGS))
211 if (clip_children( ptr->parent, ptr->hwndSelf, tmp, FALSE ) == NULLREGION) break;
213 if (!(parent = WIN_FindWndPtr( ptr->parent ))) break;
214 WIN_ReleaseWndPtr( ptr );
216 OffsetRgn( tmp, ptr->rectClient.left, ptr->rectClient.top );
217 xoffset += ptr->rectClient.left;
218 yoffset += ptr->rectClient.top;
220 WIN_ReleaseWndPtr( ptr );
221 /* make it relative to the target window again */
222 OffsetRgn( tmp, -xoffset, -yoffset );
224 /* now subtract the computed region from the original one */
225 ret = CombineRgn( rgn, rgn, tmp, RGN_DIFF );
231 /***********************************************************************
234 * Expose a region of a given window.
236 static void expose_window( HWND hwnd, RECT *rect, HRGN rgn, int flags )
243 /* find the top most parent that doesn't clip children or siblings and
244 * invalidate the area on its parent, including all children */
245 if ((list = WIN_ListParents( hwnd )))
248 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
249 for (i = 0; list[i] && list[i] != GetDesktopWindow(); i++)
251 if (!(style & WS_CLIPSIBLINGS)) top = current;
252 style = GetWindowLongW( list[i], GWL_STYLE );
253 if (!(style & WS_CLIPCHILDREN)) top = current;
259 /* find the parent of the top window, reusing the parent list */
260 if (top == hwnd) i = 0;
263 for (i = 0; list[i]; i++) if (list[i] == top) break;
264 if (list[i] && list[i+1]) i++;
266 if (list[i] != GetDesktopWindow()) top = list[i];
267 flags &= ~RDW_FRAME; /* parent will invalidate children frame anyway */
268 flags |= RDW_ALLCHILDREN;
270 HeapFree( GetProcessHeap(), 0, list );
273 if (!top) top = hwnd;
275 /* make coords relative to top */
276 offset.x = offset.y = 0;
277 MapWindowPoints( hwnd, top, &offset, 1 );
281 OffsetRect( rect, offset.x, offset.y );
282 RedrawWindow( top, rect, 0, flags );
286 OffsetRgn( rgn, offset.x, offset.y );
287 RedrawWindow( top, NULL, rgn, flags );
292 /***********************************************************************
293 * expose_covered_parent_area
295 * Expose the parent area that has been uncovered by moving/hiding a
296 * given window, but that is still covered by other siblings (the area
297 * not covered by siblings will be exposed automatically by X).
299 static void expose_covered_parent_area( WND *win, const RECT *old_rect )
301 int ret = SIMPLEREGION;
302 HRGN hrgn = CreateRectRgnIndirect( old_rect );
304 if (win->dwStyle & WS_VISIBLE)
306 HRGN tmp = CreateRectRgnIndirect( &win->rectWindow );
307 ret = CombineRgn( hrgn, hrgn, tmp, RGN_DIFF );
311 if (ret != NULLREGION)
313 if (get_covered_region( win, hrgn ) != NULLREGION)
314 expose_window( win->parent, NULL, hrgn, RDW_INVALIDATE | RDW_ERASE | RDW_ALLCHILDREN );
316 DeleteObject( hrgn );
320 /***********************************************************************
321 * expose_covered_window_area
323 * Expose the area of a window that is covered by other siblings.
325 static void expose_covered_window_area( WND *win, const RECT *old_client_rect, BOOL frame )
328 int ret = SIMPLEREGION;
331 hrgn = CreateRectRgn( win->rectWindow.left - win->rectClient.left,
332 win->rectWindow.top - win->rectClient.top,
333 win->rectWindow.right - win->rectWindow.left,
334 win->rectWindow.bottom - win->rectWindow.top );
336 hrgn = CreateRectRgn( 0, 0,
337 win->rectClient.right - win->rectClient.left,
338 win->rectClient.bottom - win->rectClient.top );
340 /* if the client rect didn't move we don't need to repaint it all */
341 if (old_client_rect->left == win->rectClient.left &&
342 old_client_rect->top == win->rectClient.top)
346 if (IntersectRect( &rc, old_client_rect, &win->rectClient ))
349 /* subtract the unchanged client area from the region to expose */
350 OffsetRect( &rc, -win->rectClient.left, -win->rectClient.top );
351 if ((tmp = CreateRectRgnIndirect( &rc )))
353 ret = CombineRgn( hrgn, hrgn, tmp, RGN_DIFF );
359 if (ret != NULLREGION)
361 if (get_covered_region( win, hrgn ) != NULLREGION)
362 expose_window( win->hwndSelf, NULL, hrgn,
363 RDW_INVALIDATE | RDW_ERASE | RDW_FRAME | RDW_ALLCHILDREN );
366 DeleteObject( hrgn );
370 /***********************************************************************
373 void X11DRV_Expose( HWND hwnd, XExposeEvent *event )
376 struct x11drv_win_data *data;
377 int flags = RDW_INVALIDATE | RDW_ERASE;
380 TRACE( "win %x (%lx) %d,%d %dx%d\n",
381 hwnd, event->window, event->x, event->y, event->width, event->height );
383 rect.left = event->x;
385 rect.right = rect.left + event->width;
386 rect.bottom = rect.top + event->height;
388 if (!(win = WIN_GetPtr( hwnd ))) return;
389 data = win->pDriverData;
391 if (event->window != data->client_window) /* whole window or icon window */
394 /* make position relative to client area instead of window */
395 OffsetRect( &rect, -data->client_rect.left, -data->client_rect.top );
397 WIN_ReleasePtr( win );
399 expose_window( hwnd, &rect, 0, flags );
403 /***********************************************************************
406 * Set the drawable, origin and dimensions for the DC associated to
409 BOOL X11DRV_GetDC( HWND hwnd, HDC hdc, HRGN hrgn, DWORD flags )
411 WND *win = WIN_GetPtr( hwnd );
413 X11DRV_WND_DATA *data = win->pDriverData;
417 int mode = IncludeInferiors;
419 /* don't clip siblings if using parent clip region */
420 if (flags & DCX_PARENTCLIP) flags &= ~DCX_CLIPSIBLINGS;
422 /* find the top parent in the hierarchy that isn't clipping siblings */
423 visible = (win->dwStyle & WS_VISIBLE) != 0;
427 HWND *list = WIN_ListParents( hwnd );
431 for (i = 0; list[i] != GetDesktopWindow(); i++)
433 LONG style = GetWindowLongW( list[i], GWL_STYLE );
434 if (!(style & WS_VISIBLE))
440 if (!(style & WS_CLIPSIBLINGS)) top = list[i];
442 HeapFree( GetProcessHeap(), 0, list );
444 if (!top && visible && !(flags & DCX_CLIPSIBLINGS)) top = hwnd;
449 HWND parent = GetAncestor( top, GA_PARENT );
451 if (flags & DCX_WINDOW)
453 org.x = win->rectWindow.left - win->rectClient.left;
454 org.y = win->rectWindow.top - win->rectClient.top;
456 MapWindowPoints( hwnd, parent, &org, 1 );
457 /* have to use the parent so that we include siblings */
458 if (parent) drawable = X11DRV_get_client_window( parent );
459 else drawable = root_window;
463 if (IsIconic( hwnd ))
465 drawable = data->icon_window ? data->icon_window : data->whole_window;
469 else if (flags & DCX_WINDOW)
471 drawable = data->whole_window;
472 org.x = win->rectWindow.left - data->whole_rect.left;
473 org.y = win->rectWindow.top - data->whole_rect.top;
477 drawable = data->client_window;
480 if (flags & DCX_CLIPCHILDREN) mode = ClipByChildren; /* can use X11 clipping */
484 X11DRV_SetDrawable( hdc, drawable, mode, org.x, org.y );
486 if (flags & (DCX_EXCLUDERGN | DCX_INTERSECTRGN) ||
487 SetHookFlags16( hdc, DCHF_VALIDATEVISRGN )) /* DC was dirty */
489 /* need to recompute the visible region */
494 visRgn = get_visible_region( win, top, flags, mode );
496 if (flags & (DCX_EXCLUDERGN | DCX_INTERSECTRGN))
497 CombineRgn( visRgn, visRgn, hrgn,
498 (flags & DCX_INTERSECTRGN) ? RGN_AND : RGN_DIFF );
500 /* make it relative to the drawable origin */
501 OffsetRgn( visRgn, org.x, org.y );
503 else visRgn = CreateRectRgn( 0, 0, 0, 0 );
505 SelectVisRgn16( hdc, visRgn );
506 DeleteObject( visRgn );
509 WIN_ReleasePtr( win );
515 /***********************************************************************
516 * SWP_DoWinPosChanging
518 static BOOL SWP_DoWinPosChanging( WINDOWPOS* pWinpos, RECT* pNewWindowRect, RECT* pNewClientRect )
522 /* Send WM_WINDOWPOSCHANGING message */
524 if (!(pWinpos->flags & SWP_NOSENDCHANGING))
525 SendMessageA( pWinpos->hwnd, WM_WINDOWPOSCHANGING, 0, (LPARAM)pWinpos );
527 if (!(wndPtr = WIN_GetPtr( pWinpos->hwnd )) || wndPtr == WND_OTHER_PROCESS) return FALSE;
529 /* Calculate new position and size */
531 *pNewWindowRect = wndPtr->rectWindow;
532 *pNewClientRect = (wndPtr->dwStyle & WS_MINIMIZE) ? wndPtr->rectWindow
533 : wndPtr->rectClient;
535 if (!(pWinpos->flags & SWP_NOSIZE))
537 pNewWindowRect->right = pNewWindowRect->left + pWinpos->cx;
538 pNewWindowRect->bottom = pNewWindowRect->top + pWinpos->cy;
540 if (!(pWinpos->flags & SWP_NOMOVE))
542 pNewWindowRect->left = pWinpos->x;
543 pNewWindowRect->top = pWinpos->y;
544 pNewWindowRect->right += pWinpos->x - wndPtr->rectWindow.left;
545 pNewWindowRect->bottom += pWinpos->y - wndPtr->rectWindow.top;
547 OffsetRect( pNewClientRect, pWinpos->x - wndPtr->rectWindow.left,
548 pWinpos->y - wndPtr->rectWindow.top );
550 pWinpos->flags |= SWP_NOCLIENTMOVE | SWP_NOCLIENTSIZE;
551 WIN_ReleasePtr( wndPtr );
555 /***********************************************************************
558 static UINT SWP_DoNCCalcSize( WINDOWPOS* pWinpos, RECT* pNewWindowRect, RECT* pNewClientRect )
563 if (!(wndPtr = WIN_GetPtr( pWinpos->hwnd )) || wndPtr == WND_OTHER_PROCESS) return 0;
565 /* Send WM_NCCALCSIZE message to get new client area */
566 if( (pWinpos->flags & (SWP_FRAMECHANGED | SWP_NOSIZE)) != SWP_NOSIZE )
568 NCCALCSIZE_PARAMS params;
569 WINDOWPOS winposCopy;
571 params.rgrc[0] = *pNewWindowRect;
572 params.rgrc[1] = wndPtr->rectWindow;
573 params.rgrc[2] = wndPtr->rectClient;
574 params.lppos = &winposCopy;
575 winposCopy = *pWinpos;
576 WIN_ReleasePtr( wndPtr );
578 wvrFlags = SendMessageW( pWinpos->hwnd, WM_NCCALCSIZE, TRUE, (LPARAM)¶ms );
580 TRACE( "%d,%d-%d,%d\n", params.rgrc[0].left, params.rgrc[0].top,
581 params.rgrc[0].right, params.rgrc[0].bottom );
583 /* If the application send back garbage, ignore it */
584 if (params.rgrc[0].left <= params.rgrc[0].right &&
585 params.rgrc[0].top <= params.rgrc[0].bottom)
586 *pNewClientRect = params.rgrc[0];
588 /* FIXME: WVR_ALIGNxxx */
590 if (!(wndPtr = WIN_GetPtr( pWinpos->hwnd )) || wndPtr == WND_OTHER_PROCESS) return 0;
592 if( pNewClientRect->left != wndPtr->rectClient.left ||
593 pNewClientRect->top != wndPtr->rectClient.top )
594 pWinpos->flags &= ~SWP_NOCLIENTMOVE;
596 if( (pNewClientRect->right - pNewClientRect->left !=
597 wndPtr->rectClient.right - wndPtr->rectClient.left) ||
598 (pNewClientRect->bottom - pNewClientRect->top !=
599 wndPtr->rectClient.bottom - wndPtr->rectClient.top) )
600 pWinpos->flags &= ~SWP_NOCLIENTSIZE;
604 if (!(pWinpos->flags & SWP_NOMOVE) &&
605 (pNewClientRect->left != wndPtr->rectClient.left ||
606 pNewClientRect->top != wndPtr->rectClient.top))
607 pWinpos->flags &= ~SWP_NOCLIENTMOVE;
609 WIN_ReleasePtr( wndPtr );
614 /***********************************************************************
617 * fix Z order taking into account owned popups -
618 * basically we need to maintain them above the window that owns them
620 * FIXME: hide/show owned popups when owner visibility changes.
622 static HWND SWP_DoOwnedPopups(HWND hwnd, HWND hwndInsertAfter)
625 HWND owner = GetWindow( hwnd, GW_OWNER );
626 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
628 WARN("(%04x) hInsertAfter = %04x\n", hwnd, hwndInsertAfter );
630 if ((style & WS_POPUP) && owner)
632 /* make sure this popup stays above the owner */
634 HWND hwndLocalPrev = HWND_TOP;
636 if( hwndInsertAfter != HWND_TOP )
638 if ((list = WIN_ListChildren( GetDesktopWindow() )))
641 for (i = 0; list[i]; i++)
643 if (list[i] == owner) break;
644 if (list[i] != hwnd) hwndLocalPrev = list[i];
645 if (hwndLocalPrev == hwndInsertAfter) break;
647 hwndInsertAfter = hwndLocalPrev;
651 else if (style & WS_CHILD) return hwndInsertAfter;
653 if (!list) list = WIN_ListChildren( GetDesktopWindow() );
657 for (i = 0; list[i]; i++)
659 if (list[i] == hwnd) break;
660 if ((GetWindowLongW( list[i], GWL_STYLE ) & WS_POPUP) &&
661 GetWindow( list[i], GW_OWNER ) == hwnd)
663 SetWindowPos( list[i], hwndInsertAfter, 0, 0, 0, 0,
664 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE |
665 SWP_NOSENDCHANGING | SWP_DEFERERASE );
666 hwndInsertAfter = list[i];
669 HeapFree( GetProcessHeap(), 0, list );
672 return hwndInsertAfter;
676 /* fix redundant flags and values in the WINDOWPOS structure */
677 static BOOL fixup_flags( WINDOWPOS *winpos )
679 WND *wndPtr = WIN_GetPtr( winpos->hwnd );
682 if (!wndPtr || wndPtr == WND_OTHER_PROCESS)
684 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
687 winpos->hwnd = wndPtr->hwndSelf; /* make it a full handle */
689 if (wndPtr->dwStyle & WS_VISIBLE) winpos->flags &= ~SWP_SHOWWINDOW;
692 winpos->flags &= ~SWP_HIDEWINDOW;
693 if (!(winpos->flags & SWP_SHOWWINDOW)) winpos->flags |= SWP_NOREDRAW;
696 if (winpos->cx < 0) winpos->cx = 0;
697 if (winpos->cy < 0) winpos->cy = 0;
699 if ((wndPtr->rectWindow.right - wndPtr->rectWindow.left == winpos->cx) &&
700 (wndPtr->rectWindow.bottom - wndPtr->rectWindow.top == winpos->cy))
701 winpos->flags |= SWP_NOSIZE; /* Already the right size */
703 if ((wndPtr->rectWindow.left == winpos->x) && (wndPtr->rectWindow.top == winpos->y))
704 winpos->flags |= SWP_NOMOVE; /* Already the right position */
706 if (winpos->hwnd == GetForegroundWindow())
707 winpos->flags |= SWP_NOACTIVATE; /* Already active */
708 else if ((wndPtr->dwStyle & (WS_POPUP | WS_CHILD)) != WS_CHILD)
710 if (!(winpos->flags & SWP_NOACTIVATE)) /* Bring to the top when activating */
712 winpos->flags &= ~SWP_NOZORDER;
713 winpos->hwndInsertAfter = HWND_TOP;
718 /* Check hwndInsertAfter */
720 /* fix sign extension */
721 if (winpos->hwndInsertAfter == (HWND)0xffff) winpos->hwndInsertAfter = HWND_TOPMOST;
722 else if (winpos->hwndInsertAfter == (HWND)0xfffe) winpos->hwndInsertAfter = HWND_NOTOPMOST;
724 /* FIXME: TOPMOST not supported yet */
725 if ((winpos->hwndInsertAfter == HWND_TOPMOST) ||
726 (winpos->hwndInsertAfter == HWND_NOTOPMOST)) winpos->hwndInsertAfter = HWND_TOP;
728 /* hwndInsertAfter must be a sibling of the window */
729 if ((winpos->hwndInsertAfter != HWND_TOP) && (winpos->hwndInsertAfter != HWND_BOTTOM))
731 winpos->hwndInsertAfter = WIN_GetFullHandle( winpos->hwndInsertAfter );
732 if (GetAncestor( winpos->hwndInsertAfter, GA_PARENT ) != wndPtr->parent) ret = FALSE;
735 /* don't need to change the Zorder of hwnd if it's already inserted
736 * after hwndInsertAfter or when inserting hwnd after itself.
738 if ((winpos->hwnd == winpos->hwndInsertAfter) ||
739 (winpos->hwnd == GetWindow( winpos->hwndInsertAfter, GW_HWNDNEXT )))
740 winpos->flags |= SWP_NOZORDER;
744 WIN_ReleasePtr( wndPtr );
749 /***********************************************************************
750 * SetWindowStyle (X11DRV.@)
752 * Update the X state of a window to reflect a style change
754 void X11DRV_SetWindowStyle( HWND hwnd, LONG oldStyle )
756 Display *display = thread_display();
760 if (hwnd == GetDesktopWindow()) return;
761 if (!(wndPtr = WIN_GetPtr( hwnd ))) return;
762 if (wndPtr == WND_OTHER_PROCESS) return;
764 changed = wndPtr->dwStyle ^ oldStyle;
766 if (changed & WS_VISIBLE)
768 if (!IsRectEmpty( &wndPtr->rectWindow ))
770 if (wndPtr->dwStyle & WS_VISIBLE)
772 TRACE( "mapping win %x\n", hwnd );
773 TSXMapWindow( display, get_whole_window(wndPtr) );
777 TRACE( "unmapping win %x\n", hwnd );
778 TSXUnmapWindow( display, get_whole_window(wndPtr) );
783 if (changed & WS_DISABLED)
785 if (wndPtr->dwExStyle & WS_EX_MANAGED)
789 if (!(wm_hints = XGetWMHints( display, get_whole_window(wndPtr) )))
790 wm_hints = XAllocWMHints();
793 wm_hints->flags |= InputHint;
794 wm_hints->input = !(wndPtr->dwStyle & WS_DISABLED);
795 XSetWMHints( display, get_whole_window(wndPtr), wm_hints );
801 WIN_ReleasePtr(wndPtr);
805 /***********************************************************************
806 * SetWindowPos (X11DRV.@)
808 BOOL X11DRV_SetWindowPos( WINDOWPOS *winpos )
811 RECT newWindowRect, newClientRect;
812 RECT oldWindowRect, oldClientRect;
816 TRACE( "hwnd %04x, swp (%i,%i)-(%i,%i) flags %08x\n",
817 winpos->hwnd, winpos->x, winpos->y,
818 winpos->x + winpos->cx, winpos->y + winpos->cy, winpos->flags);
820 bChangePos = !(winpos->flags & SWP_WINE_NOHOSTMOVE);
821 winpos->flags &= ~SWP_WINE_NOHOSTMOVE;
823 /* Fix redundant flags */
824 if (!fixup_flags( winpos )) return FALSE;
826 /* Check window handle */
827 if (winpos->hwnd == GetDesktopWindow()) return FALSE;
829 SWP_DoWinPosChanging( winpos, &newWindowRect, &newClientRect );
831 if (!(wndPtr = WIN_FindWndPtr( winpos->hwnd ))) return FALSE;
833 TRACE("\tcurrent (%i,%i)-(%i,%i), style %08x\n",
834 wndPtr->rectWindow.left, wndPtr->rectWindow.top,
835 wndPtr->rectWindow.right, wndPtr->rectWindow.bottom, (unsigned)wndPtr->dwStyle );
837 if((winpos->flags & (SWP_NOZORDER | SWP_HIDEWINDOW | SWP_SHOWWINDOW)) != SWP_NOZORDER)
839 if (GetAncestor( winpos->hwnd, GA_PARENT ) == GetDesktopWindow())
840 winpos->hwndInsertAfter = SWP_DoOwnedPopups( winpos->hwnd, winpos->hwndInsertAfter );
843 /* Common operations */
845 wvrFlags = SWP_DoNCCalcSize( winpos, &newWindowRect, &newClientRect );
847 if(!(winpos->flags & SWP_NOZORDER) && winpos->hwnd != winpos->hwndInsertAfter)
849 HWND parent = GetAncestor( winpos->hwnd, GA_PARENT );
850 if (parent) WIN_LinkWindow( winpos->hwnd, parent, winpos->hwndInsertAfter );
853 /* Reset active DCEs */
855 if( (((winpos->flags & SWP_AGG_NOPOSCHANGE) != SWP_AGG_NOPOSCHANGE) &&
856 wndPtr->dwStyle & WS_VISIBLE) ||
857 (winpos->flags & (SWP_HIDEWINDOW | SWP_SHOWWINDOW)) )
861 UnionRect(&rect, &newWindowRect, &wndPtr->rectWindow);
862 DCE_InvalidateDCE(wndPtr->hwndSelf, &rect);
865 oldWindowRect = wndPtr->rectWindow;
866 oldClientRect = wndPtr->rectClient;
868 /* Find out if we have to redraw the whole client rect */
870 if( oldClientRect.bottom - oldClientRect.top ==
871 newClientRect.bottom - newClientRect.top ) wvrFlags &= ~WVR_VREDRAW;
873 if( oldClientRect.right - oldClientRect.left ==
874 newClientRect.right - newClientRect.left ) wvrFlags &= ~WVR_HREDRAW;
876 /* FIXME: actually do something with WVR_VALIDRECTS */
878 WIN_SetRectangles( winpos->hwnd, &newWindowRect, &newClientRect );
880 if (get_whole_window(wndPtr)) /* don't do anything if X window not created yet */
882 Display *display = thread_display();
884 if (!(winpos->flags & SWP_SHOWWINDOW) && (winpos->flags & SWP_HIDEWINDOW))
886 /* clear the update region */
887 RedrawWindow( winpos->hwnd, NULL, 0, RDW_VALIDATE | RDW_NOFRAME |
888 RDW_NOERASE | RDW_NOINTERNALPAINT | RDW_ALLCHILDREN );
889 WIN_SetStyle( winpos->hwnd, wndPtr->dwStyle & ~WS_VISIBLE );
891 else if ((wndPtr->dwStyle & WS_VISIBLE) &&
892 !IsRectEmpty( &oldWindowRect ) && IsRectEmpty( &newWindowRect ))
894 /* resizing to zero size -> unmap */
895 TRACE( "unmapping zero size win %x\n", winpos->hwnd );
896 TSXUnmapWindow( display, get_whole_window(wndPtr) );
901 X11DRV_sync_whole_window_position( display, wndPtr, !(winpos->flags & SWP_NOZORDER) );
904 struct x11drv_win_data *data = wndPtr->pDriverData;
905 data->whole_rect = wndPtr->rectWindow;
906 X11DRV_window_to_X_rect( wndPtr, &data->whole_rect );
909 if (X11DRV_sync_client_window_position( display, wndPtr ) ||
910 (winpos->flags & SWP_FRAMECHANGED))
912 /* if we moved the client area, repaint the whole non-client window */
913 XClearArea( display, get_whole_window(wndPtr), 0, 0, 0, 0, True );
914 winpos->flags |= SWP_FRAMECHANGED;
916 if (winpos->flags & SWP_SHOWWINDOW)
918 WIN_SetStyle( winpos->hwnd, wndPtr->dwStyle | WS_VISIBLE );
920 else if ((wndPtr->dwStyle & WS_VISIBLE) &&
921 IsRectEmpty( &oldWindowRect ) && !IsRectEmpty( &newWindowRect ))
923 /* resizing from zero size to non-zero -> map */
924 TRACE( "mapping non zero size win %x\n", winpos->hwnd );
925 XMapWindow( display, get_whole_window(wndPtr) );
927 XFlush( display ); /* FIXME: should not be necessary */
930 else /* no X window, simply toggle the window style */
932 if (winpos->flags & SWP_SHOWWINDOW)
933 WIN_SetStyle( winpos->hwnd, wndPtr->dwStyle | WS_VISIBLE );
934 else if (winpos->flags & SWP_HIDEWINDOW)
935 WIN_SetStyle( winpos->hwnd, wndPtr->dwStyle & ~WS_VISIBLE );
938 /* manually expose the areas that X won't expose because they are still covered by something */
940 if (!(winpos->flags & SWP_SHOWWINDOW))
941 expose_covered_parent_area( wndPtr, &oldWindowRect );
943 if (wndPtr->dwStyle & WS_VISIBLE)
944 expose_covered_window_area( wndPtr, &oldClientRect, winpos->flags & SWP_FRAMECHANGED );
946 WIN_ReleaseWndPtr(wndPtr);
948 if (wvrFlags & WVR_REDRAW) RedrawWindow( winpos->hwnd, NULL, 0, RDW_INVALIDATE | RDW_ERASE );
950 if (winpos->hwnd == CARET_GetHwnd())
952 if( winpos->flags & SWP_HIDEWINDOW )
953 HideCaret(winpos->hwnd);
954 else if (winpos->flags & SWP_SHOWWINDOW)
955 ShowCaret(winpos->hwnd);
958 if (!(winpos->flags & SWP_NOACTIVATE))
960 /* child windows get WM_CHILDACTIVATE message */
961 if ((GetWindowLongW( winpos->hwnd, GWL_STYLE ) & (WS_CHILD | WS_POPUP)) == WS_CHILD)
962 SendMessageA( winpos->hwnd, WM_CHILDACTIVATE, 0, 0 );
964 SetActiveWindow( winpos->hwnd );
967 /* And last, send the WM_WINDOWPOSCHANGED message */
969 TRACE("\tstatus flags = %04x\n", winpos->flags & SWP_AGG_STATUSFLAGS);
971 if (((winpos->flags & SWP_AGG_STATUSFLAGS) != SWP_AGG_NOPOSCHANGE) &&
972 !(winpos->flags & SWP_NOSENDCHANGING))
973 SendMessageA( winpos->hwnd, WM_WINDOWPOSCHANGED, 0, (LPARAM)winpos );
979 /***********************************************************************
982 * Find a suitable place for an iconic window.
984 static POINT WINPOS_FindIconPos( WND* wndPtr, POINT pt )
988 short x, y, xspacing, yspacing;
990 GetClientRect( wndPtr->parent, &rectParent );
991 if ((pt.x >= rectParent.left) && (pt.x + GetSystemMetrics(SM_CXICON) < rectParent.right) &&
992 (pt.y >= rectParent.top) && (pt.y + GetSystemMetrics(SM_CYICON) < rectParent.bottom))
993 return pt; /* The icon already has a suitable position */
995 xspacing = GetSystemMetrics(SM_CXICONSPACING);
996 yspacing = GetSystemMetrics(SM_CYICONSPACING);
998 list = WIN_ListChildren( wndPtr->parent );
999 y = rectParent.bottom;
1002 x = rectParent.left;
1005 /* Check if another icon already occupies this spot */
1006 /* FIXME: this is completely inefficient */
1012 for (i = 0; list[i]; i++)
1014 if (list[i] == wndPtr->hwndSelf) continue;
1015 if (!IsIconic( list[i] )) continue;
1016 if (!(childPtr = WIN_FindWndPtr( list[i] ))) continue;
1017 if ((childPtr->rectWindow.left < x + xspacing) &&
1018 (childPtr->rectWindow.right >= x) &&
1019 (childPtr->rectWindow.top <= y) &&
1020 (childPtr->rectWindow.bottom > y - yspacing))
1022 WIN_ReleaseWndPtr( childPtr );
1023 break; /* There's a window in there */
1025 WIN_ReleaseWndPtr( childPtr );
1029 /* found something here, try next spot */
1035 /* No window was found, so it's OK for us */
1036 pt.x = x + (xspacing - GetSystemMetrics(SM_CXICON)) / 2;
1037 pt.y = y - (yspacing + GetSystemMetrics(SM_CYICON)) / 2;
1040 } while(x <= rectParent.right-xspacing);
1049 UINT WINPOS_MinMaximize( HWND hwnd, UINT cmd, LPRECT rect )
1055 WINDOWPLACEMENT wpl;
1057 TRACE("0x%04x %u\n", hwnd, cmd );
1059 wpl.length = sizeof(wpl);
1060 GetWindowPlacement( hwnd, &wpl );
1062 if (HOOK_CallHooksA( WH_CBT, HCBT_MINMAX, (WPARAM)hwnd, cmd ))
1063 return SWP_NOSIZE | SWP_NOMOVE;
1065 if (IsIconic( hwnd ))
1067 if (cmd == SW_MINIMIZE) return SWP_NOSIZE | SWP_NOMOVE;
1068 if (!SendMessageA( hwnd, WM_QUERYOPEN, 0, 0 )) return SWP_NOSIZE | SWP_NOMOVE;
1069 swpFlags |= SWP_NOCOPYBITS;
1072 if (!(wndPtr = WIN_FindWndPtr( hwnd ))) return 0;
1074 size.x = wndPtr->rectWindow.left;
1075 size.y = wndPtr->rectWindow.top;
1080 if( wndPtr->dwStyle & WS_MAXIMIZE) wndPtr->flags |= WIN_RESTORE_MAX;
1081 else wndPtr->flags &= ~WIN_RESTORE_MAX;
1083 WIN_SetStyle( hwnd, (wndPtr->dwStyle & ~WS_MAXIMIZE) | WS_MINIMIZE );
1085 X11DRV_set_iconic_state( wndPtr );
1087 wpl.ptMinPosition = WINPOS_FindIconPos( wndPtr, wpl.ptMinPosition );
1089 SetRect( rect, wpl.ptMinPosition.x, wpl.ptMinPosition.y,
1090 GetSystemMetrics(SM_CXICON), GetSystemMetrics(SM_CYICON) );
1091 swpFlags |= SWP_NOCOPYBITS;
1095 WINPOS_GetMinMaxInfo( hwnd, &size, &wpl.ptMaxPosition, NULL, NULL );
1097 old_style = WIN_SetStyle( hwnd, (wndPtr->dwStyle & ~WS_MINIMIZE) | WS_MAXIMIZE );
1098 if (old_style & WS_MINIMIZE)
1100 WINPOS_ShowIconTitle( hwnd, FALSE );
1101 X11DRV_set_iconic_state( wndPtr );
1103 SetRect( rect, wpl.ptMaxPosition.x, wpl.ptMaxPosition.y, size.x, size.y );
1107 old_style = WIN_SetStyle( hwnd, wndPtr->dwStyle & ~(WS_MINIMIZE|WS_MAXIMIZE) );
1108 if (old_style & WS_MINIMIZE)
1110 WINPOS_ShowIconTitle( hwnd, FALSE );
1111 X11DRV_set_iconic_state( wndPtr );
1113 if( wndPtr->flags & WIN_RESTORE_MAX)
1115 /* Restore to maximized position */
1116 WINPOS_GetMinMaxInfo( hwnd, &size, &wpl.ptMaxPosition, NULL, NULL);
1117 WIN_SetStyle( hwnd, wndPtr->dwStyle | WS_MAXIMIZE );
1118 SetRect( rect, wpl.ptMaxPosition.x, wpl.ptMaxPosition.y, size.x, size.y );
1122 else if (!(old_style & WS_MAXIMIZE)) break;
1124 /* Restore to normal position */
1126 *rect = wpl.rcNormalPosition;
1127 rect->right -= rect->left;
1128 rect->bottom -= rect->top;
1133 WIN_ReleaseWndPtr( wndPtr );
1138 /***********************************************************************
1139 * ShowWindow (X11DRV.@)
1141 BOOL X11DRV_ShowWindow( HWND hwnd, INT cmd )
1143 WND* wndPtr = WIN_FindWndPtr( hwnd );
1144 BOOL wasVisible, showFlag;
1145 RECT newPos = {0, 0, 0, 0};
1148 if (!wndPtr) return FALSE;
1149 hwnd = wndPtr->hwndSelf; /* make it a full handle */
1151 TRACE("hwnd=%04x, cmd=%d\n", hwnd, cmd);
1153 wasVisible = (wndPtr->dwStyle & WS_VISIBLE) != 0;
1158 if (!wasVisible) goto END;;
1159 swp |= SWP_HIDEWINDOW | SWP_NOSIZE | SWP_NOMOVE |
1160 SWP_NOACTIVATE | SWP_NOZORDER;
1163 case SW_SHOWMINNOACTIVE:
1164 swp |= SWP_NOACTIVATE | SWP_NOZORDER;
1166 case SW_SHOWMINIMIZED:
1167 swp |= SWP_SHOWWINDOW;
1170 swp |= SWP_FRAMECHANGED;
1171 if( !(wndPtr->dwStyle & WS_MINIMIZE) )
1172 swp |= WINPOS_MinMaximize( hwnd, SW_MINIMIZE, &newPos );
1173 else swp |= SWP_NOSIZE | SWP_NOMOVE;
1176 case SW_SHOWMAXIMIZED: /* same as SW_MAXIMIZE */
1177 swp |= SWP_SHOWWINDOW | SWP_FRAMECHANGED;
1178 if( !(wndPtr->dwStyle & WS_MAXIMIZE) )
1179 swp |= WINPOS_MinMaximize( hwnd, SW_MAXIMIZE, &newPos );
1180 else swp |= SWP_NOSIZE | SWP_NOMOVE;
1184 swp |= SWP_NOACTIVATE | SWP_NOZORDER;
1187 swp |= SWP_SHOWWINDOW | SWP_NOSIZE | SWP_NOMOVE;
1190 * ShowWindow has a little peculiar behavior that if the
1191 * window is already the topmost window, it will not
1194 if (GetTopWindow((HWND)0)==hwnd && (wasVisible || GetActiveWindow() == hwnd))
1195 swp |= SWP_NOACTIVATE;
1199 case SW_SHOWNOACTIVATE:
1200 swp |= SWP_NOZORDER;
1201 if (GetActiveWindow()) swp |= SWP_NOACTIVATE;
1203 case SW_SHOWNORMAL: /* same as SW_NORMAL: */
1204 case SW_SHOWDEFAULT: /* FIXME: should have its own handler */
1206 swp |= SWP_SHOWWINDOW | SWP_FRAMECHANGED;
1208 if( wndPtr->dwStyle & (WS_MINIMIZE | WS_MAXIMIZE) )
1209 swp |= WINPOS_MinMaximize( hwnd, SW_RESTORE, &newPos );
1210 else swp |= SWP_NOSIZE | SWP_NOMOVE;
1214 showFlag = (cmd != SW_HIDE);
1215 if (showFlag != wasVisible)
1217 SendMessageA( hwnd, WM_SHOWWINDOW, showFlag, 0 );
1218 if (!IsWindow( hwnd )) goto END;
1221 /* We can't activate a child window */
1222 if ((wndPtr->dwStyle & WS_CHILD) &&
1223 !(wndPtr->dwExStyle & WS_EX_MDICHILD))
1224 swp |= SWP_NOACTIVATE | SWP_NOZORDER;
1226 SetWindowPos( hwnd, HWND_TOP, newPos.left, newPos.top,
1227 newPos.right, newPos.bottom, LOWORD(swp) );
1230 /* FIXME: This will cause the window to be activated irrespective
1231 * of whether it is owned by the same thread. Has to be done
1235 if (hwnd == GetActiveWindow())
1236 WINPOS_ActivateOtherWindow(hwnd);
1238 /* Revert focus to parent */
1239 if (hwnd == GetFocus() || IsChild(hwnd, GetFocus()))
1240 SetFocus( GetParent(hwnd) );
1242 if (!IsWindow( hwnd )) goto END;
1243 else if( wndPtr->dwStyle & WS_MINIMIZE ) WINPOS_ShowIconTitle( hwnd, TRUE );
1245 if (wndPtr->flags & WIN_NEED_SIZE)
1247 /* should happen only in CreateWindowEx() */
1248 int wParam = SIZE_RESTORED;
1250 wndPtr->flags &= ~WIN_NEED_SIZE;
1251 if (wndPtr->dwStyle & WS_MAXIMIZE) wParam = SIZE_MAXIMIZED;
1252 else if (wndPtr->dwStyle & WS_MINIMIZE) wParam = SIZE_MINIMIZED;
1253 SendMessageA( hwnd, WM_SIZE, wParam,
1254 MAKELONG(wndPtr->rectClient.right-wndPtr->rectClient.left,
1255 wndPtr->rectClient.bottom-wndPtr->rectClient.top));
1256 SendMessageA( hwnd, WM_MOVE, 0,
1257 MAKELONG(wndPtr->rectClient.left, wndPtr->rectClient.top) );
1261 WIN_ReleaseWndPtr(wndPtr);
1266 /**********************************************************************
1269 void X11DRV_MapNotify( HWND hwnd, XMapEvent *event )
1271 HWND hwndFocus = GetFocus();
1274 if (!(win = WIN_GetPtr( hwnd ))) return;
1276 if ((win->dwStyle & WS_VISIBLE) &&
1277 (win->dwStyle & WS_MINIMIZE) &&
1278 (win->dwExStyle & WS_EX_MANAGED))
1281 unsigned int width, height, border, depth;
1284 LONG style = (win->dwStyle & ~(WS_MINIMIZE|WS_MAXIMIZE)) | WS_VISIBLE;
1288 XGetGeometry( event->display, get_whole_window(win), &root, &x, &y, &width, &height,
1290 XTranslateCoordinates( event->display, get_whole_window(win), root, 0, 0, &x, &y, &top );
1291 wine_tsx11_unlock();
1294 rect.right = x + width;
1295 rect.bottom = y + height;
1296 X11DRV_X_to_window_rect( win, &rect );
1298 DCE_InvalidateDCE( hwnd, &win->rectWindow );
1300 if (win->flags & WIN_RESTORE_MAX) style |= WS_MAXIMIZE;
1301 WIN_SetStyle( hwnd, style );
1302 WIN_ReleasePtr( win );
1304 WIN_InternalShowOwnedPopups( hwnd, TRUE, TRUE );
1305 SendMessageA( hwnd, WM_SHOWWINDOW, SW_RESTORE, 0 );
1306 SetWindowPos( hwnd, 0, rect.left, rect.top, rect.right-rect.left, rect.bottom-rect.top,
1307 SWP_NOZORDER | SWP_WINE_NOHOSTMOVE );
1309 else WIN_ReleasePtr( win );
1310 if (hwndFocus && IsChild( hwnd, hwndFocus )) X11DRV_SetFocus(hwndFocus); /* FIXME */
1314 /**********************************************************************
1315 * X11DRV_UnmapNotify
1317 void X11DRV_UnmapNotify( HWND hwnd, XUnmapEvent *event )
1321 if (!(win = WIN_GetPtr( hwnd ))) return;
1323 if ((win->dwStyle & WS_VISIBLE) && (win->dwExStyle & WS_EX_MANAGED))
1325 if (win->dwStyle & WS_MAXIMIZE)
1326 win->flags |= WIN_RESTORE_MAX;
1328 win->flags &= ~WIN_RESTORE_MAX;
1330 WIN_SetStyle( hwnd, (win->dwStyle & ~WS_MAXIMIZE) | WS_MINIMIZE );
1331 WIN_ReleasePtr( win );
1334 SendMessageA( hwnd, WM_SHOWWINDOW, SW_MINIMIZE, 0 );
1335 SetWindowPos( hwnd, 0, 0, 0, GetSystemMetrics(SM_CXICON), GetSystemMetrics(SM_CYICON),
1336 SWP_NOMOVE | SWP_NOACTIVATE | SWP_NOZORDER | SWP_WINE_NOHOSTMOVE );
1338 WIN_InternalShowOwnedPopups( hwnd, FALSE, TRUE );
1340 else WIN_ReleasePtr( win );
1344 /***********************************************************************
1347 * Synchronize internal z-order with the window manager's.
1349 static Window __get_common_ancestor( Display *display, Window A, Window B,
1350 Window** children, unsigned* total )
1352 /* find the real root window */
1354 Window root, *childrenB;
1357 while( A != B && A && B )
1359 TSXQueryTree( display, A, &root, &A, children, total );
1360 TSXQueryTree( display, B, &root, &B, &childrenB, &totalB );
1361 if( childrenB ) TSXFree( childrenB );
1362 if( *children ) TSXFree( *children ), *children = NULL;
1367 TSXQueryTree( display, A, &root, &B, children, total );
1373 static Window __get_top_decoration( Display *display, Window w, Window ancestor )
1375 Window* children, root, prev = w, parent = w;
1381 TSXQueryTree( display, w, &root, &parent, &children, &total );
1382 if( children ) TSXFree( children );
1383 } while( parent && parent != ancestor );
1384 TRACE("\t%08x -> %08x\n", (unsigned)prev, (unsigned)w );
1385 return ( parent ) ? w : 0 ;
1388 static unsigned __td_lookup( Window w, Window* list, unsigned max )
1391 for( i = max - 1; i >= 0; i-- ) if( list[i] == w ) break;
1395 static HWND query_zorder( Display *display, HWND hWndCheck)
1397 HWND hwndInsertAfter = HWND_TOP;
1398 Window w, parent, *children = NULL;
1399 unsigned total, check, pos, best;
1400 HWND *list = WIN_ListChildren( GetDesktopWindow() );
1401 HWND hwndA = 0, hwndB = 0;
1404 /* find at least two managed windows */
1405 if (!list) return 0;
1406 for (i = 0; list[i]; i++)
1408 if (!(GetWindowLongW( list[i], GWL_EXSTYLE ) & WS_EX_MANAGED)) continue;
1409 if (!(GetWindowLongW( list[i], GWL_STYLE ) & WS_VISIBLE)) continue;
1410 if (!hwndA) hwndA = list[i];
1417 if (!hwndA || !hwndB) goto done;
1419 parent = __get_common_ancestor( display, X11DRV_get_whole_window(hwndA),
1420 X11DRV_get_whole_window(hwndB), &children, &total );
1421 if( parent && children )
1423 /* w is the ancestor if hWndCheck that is a direct descendant of 'parent' */
1425 w = __get_top_decoration( display, X11DRV_get_whole_window(hWndCheck), parent );
1427 if( w != children[total-1] ) /* check if at the top */
1429 /* X child at index 0 is at the bottom, at index total-1 is at the top */
1430 check = __td_lookup( w, children, total );
1433 /* go through all windows in Wine z-order... */
1434 for (i = 0; list[i]; i++)
1436 if (list[i] == hWndCheck) continue;
1437 if (!(GetWindowLongW( list[i], GWL_EXSTYLE ) & WS_EX_MANAGED)) continue;
1438 if (!(w = __get_top_decoration( display, X11DRV_get_whole_window(list[i]),
1439 parent ))) continue;
1440 pos = __td_lookup( w, children, total );
1441 if( pos < best && pos > check )
1443 /* find a nearest Wine window precedes hWndCheck in the real z-order */
1445 hwndInsertAfter = list[i];
1447 if( best - check == 1 ) break;
1451 if( children ) TSXFree( children );
1454 HeapFree( GetProcessHeap(), 0, list );
1455 return hwndInsertAfter;
1459 /***********************************************************************
1460 * X11DRV_ConfigureNotify
1462 void X11DRV_ConfigureNotify( HWND hwnd, XConfigureEvent *event )
1464 HWND oldInsertAfter;
1465 struct x11drv_win_data *data;
1469 int x = event->x, y = event->y;
1471 if (!(win = WIN_GetPtr( hwnd ))) return;
1472 data = win->pDriverData;
1476 if (!event->send_event) /* normal event, need to map coordinates to the root */
1480 XTranslateCoordinates( event->display, data->whole_window, root_window,
1481 0, 0, &x, &y, &child );
1482 wine_tsx11_unlock();
1486 rect.right = x + event->width;
1487 rect.bottom = y + event->height;
1488 TRACE( "win %x new X rect %d,%d,%dx%d (event %d,%d,%dx%d)\n",
1489 hwnd, rect.left, rect.top, rect.right-rect.left, rect.bottom-rect.top,
1490 event->x, event->y, event->width, event->height );
1491 X11DRV_X_to_window_rect( win, &rect );
1492 WIN_ReleasePtr( win );
1495 winpos.x = rect.left;
1496 winpos.y = rect.top;
1497 winpos.cx = rect.right - rect.left;
1498 winpos.cy = rect.bottom - rect.top;
1499 winpos.flags = SWP_NOACTIVATE;
1501 /* Get Z-order (FIXME) */
1503 winpos.hwndInsertAfter = query_zorder( event->display, hwnd );
1505 /* needs to find the first Visible Window above the current one */
1506 oldInsertAfter = hwnd;
1509 oldInsertAfter = GetWindow( oldInsertAfter, GW_HWNDPREV );
1510 if (!oldInsertAfter)
1512 oldInsertAfter = HWND_TOP;
1515 if (GetWindowLongA( oldInsertAfter, GWL_STYLE ) & WS_VISIBLE) break;
1518 /* Compare what has changed */
1520 GetWindowRect( hwnd, &rect );
1521 if (rect.left == winpos.x && rect.top == winpos.y) winpos.flags |= SWP_NOMOVE;
1523 TRACE( "%04x moving from (%d,%d) to (%d,%d)\n",
1524 hwnd, rect.left, rect.top, winpos.x, winpos.y );
1526 if ((rect.right - rect.left == winpos.cx && rect.bottom - rect.top == winpos.cy) ||
1528 (IsRectEmpty( &rect ) && winpos.cx == 1 && winpos.cy == 1))
1529 winpos.flags |= SWP_NOSIZE;
1531 TRACE( "%04x resizing from (%dx%d) to (%dx%d)\n",
1532 hwnd, rect.right - rect.left, rect.bottom - rect.top,
1533 winpos.cx, winpos.cy );
1535 if (winpos.hwndInsertAfter == oldInsertAfter) winpos.flags |= SWP_NOZORDER;
1537 TRACE( "%04x restacking from after %04x to after %04x\n",
1538 hwnd, oldInsertAfter, winpos.hwndInsertAfter );
1540 /* if nothing changed, don't do anything */
1541 if (winpos.flags == (SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE)) return;
1543 SetWindowPos( hwnd, winpos.hwndInsertAfter, winpos.x, winpos.y,
1544 winpos.cx, winpos.cy, winpos.flags | SWP_WINE_NOHOSTMOVE );
1548 /***********************************************************************
1549 * SetWindowRgn (X11DRV.@)
1551 * Assign specified region to window (for non-rectangular windows)
1553 int X11DRV_SetWindowRgn( HWND hwnd, HRGN hrgn, BOOL redraw )
1557 if ((wndPtr = WIN_GetPtr( hwnd )) == WND_OTHER_PROCESS)
1559 if (IsWindow( hwnd ))
1560 FIXME( "not supported on other process window %x\n", hwnd );
1565 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
1569 if (wndPtr->hrgnWnd == hrgn)
1571 WIN_ReleasePtr( wndPtr );
1575 if (wndPtr->hrgnWnd)
1577 /* delete previous region */
1578 DeleteObject(wndPtr->hrgnWnd);
1579 wndPtr->hrgnWnd = 0;
1581 wndPtr->hrgnWnd = hrgn;
1583 #ifdef HAVE_LIBXSHAPE
1585 Display *display = thread_display();
1586 X11DRV_WND_DATA *data = wndPtr->pDriverData;
1588 if (data->whole_window)
1592 TSXShapeCombineMask( display, data->whole_window,
1593 ShapeBounding, 0, 0, None, ShapeSet );
1598 int x_offset, y_offset;
1600 DWORD dwBufferSize = GetRegionData(hrgn, 0, NULL);
1601 PRGNDATA pRegionData = HeapAlloc(GetProcessHeap(), 0, dwBufferSize);
1604 WIN_ReleasePtr( wndPtr );
1607 GetRegionData(hrgn, dwBufferSize, pRegionData);
1608 size = pRegionData->rdh.nCount;
1609 x_offset = wndPtr->rectWindow.left - data->whole_rect.left;
1610 y_offset = wndPtr->rectWindow.top - data->whole_rect.top;
1611 /* convert region's "Windows rectangles" to XRectangles */
1612 aXRect = HeapAlloc(GetProcessHeap(), 0, size * sizeof(*aXRect) );
1615 XRectangle* pCurrRect = aXRect;
1616 RECT *pRect = (RECT*) pRegionData->Buffer;
1617 for (; pRect < ((RECT*) pRegionData->Buffer) + size ; ++pRect, ++pCurrRect)
1619 pCurrRect->x = pRect->left + x_offset;
1620 pCurrRect->y = pRect->top + y_offset;
1621 pCurrRect->height = pRect->bottom - pRect->top;
1622 pCurrRect->width = pRect->right - pRect->left;
1624 TRACE("Rectangle %04d of %04ld data: X=%04d, Y=%04d, Height=%04d, Width=%04d.\n",
1625 pRect - (RECT*) pRegionData->Buffer,
1633 /* shape = non-rectangular windows (X11/extensions) */
1634 TSXShapeCombineRectangles( display, data->whole_window, ShapeBounding,
1636 pCurrRect - aXRect, ShapeSet, YXBanded );
1637 HeapFree(GetProcessHeap(), 0, aXRect );
1639 HeapFree(GetProcessHeap(), 0, pRegionData);
1643 #endif /* HAVE_LIBXSHAPE */
1645 WIN_ReleasePtr( wndPtr );
1646 if (redraw) RedrawWindow( hwnd, NULL, 0, RDW_FRAME | RDW_INVALIDATE | RDW_ERASE );
1651 /***********************************************************************
1654 * Draw the frame used when moving or resizing window.
1656 * FIXME: This causes problems in Win95 mode. (why?)
1658 static void draw_moving_frame( HDC hdc, RECT *rect, BOOL thickframe )
1662 const int width = GetSystemMetrics(SM_CXFRAME);
1663 const int height = GetSystemMetrics(SM_CYFRAME);
1665 HBRUSH hbrush = SelectObject( hdc, GetStockObject( GRAY_BRUSH ) );
1666 PatBlt( hdc, rect->left, rect->top,
1667 rect->right - rect->left - width, height, PATINVERT );
1668 PatBlt( hdc, rect->left, rect->top + height, width,
1669 rect->bottom - rect->top - height, PATINVERT );
1670 PatBlt( hdc, rect->left + width, rect->bottom - 1,
1671 rect->right - rect->left - width, -height, PATINVERT );
1672 PatBlt( hdc, rect->right - 1, rect->top, -width,
1673 rect->bottom - rect->top - height, PATINVERT );
1674 SelectObject( hdc, hbrush );
1676 else DrawFocusRect( hdc, rect );
1680 /***********************************************************************
1683 * Initialisation of a move or resize, when initiatied from a menu choice.
1684 * Return hit test code for caption or sizing border.
1686 static LONG start_size_move( HWND hwnd, WPARAM wParam, POINT *capturePoint, LONG style )
1693 GetWindowRect( hwnd, &rectWindow );
1695 if ((wParam & 0xfff0) == SC_MOVE)
1697 /* Move pointer at the center of the caption */
1699 NC_GetInsideRect( hwnd, &rect );
1700 if (style & WS_SYSMENU)
1701 rect.left += GetSystemMetrics(SM_CXSIZE) + 1;
1702 if (style & WS_MINIMIZEBOX)
1703 rect.right -= GetSystemMetrics(SM_CXSIZE) + 1;
1704 if (style & WS_MAXIMIZEBOX)
1705 rect.right -= GetSystemMetrics(SM_CXSIZE) + 1;
1706 pt.x = rectWindow.left + (rect.right - rect.left) / 2;
1707 pt.y = rectWindow.top + rect.top + GetSystemMetrics(SM_CYSIZE)/2;
1708 hittest = HTCAPTION;
1715 GetMessageW( &msg, 0, WM_KEYFIRST, WM_MOUSELAST );
1716 if (CallMsgFilterW( &msg, MSGF_SIZE )) continue;
1721 hittest = NC_HandleNCHitTest( hwnd, msg.pt );
1722 if ((hittest < HTLEFT) || (hittest > HTBOTTOMRIGHT))
1734 pt.x =(rectWindow.left+rectWindow.right)/2;
1735 pt.y = rectWindow.top + GetSystemMetrics(SM_CYFRAME) / 2;
1739 pt.x =(rectWindow.left+rectWindow.right)/2;
1740 pt.y = rectWindow.bottom - GetSystemMetrics(SM_CYFRAME) / 2;
1744 pt.x = rectWindow.left + GetSystemMetrics(SM_CXFRAME) / 2;
1745 pt.y =(rectWindow.top+rectWindow.bottom)/2;
1749 pt.x = rectWindow.right - GetSystemMetrics(SM_CXFRAME) / 2;
1750 pt.y =(rectWindow.top+rectWindow.bottom)/2;
1753 case VK_ESCAPE: return 0;
1759 SetCursorPos( pt.x, pt.y );
1760 NC_HandleSetCursor( hwnd, (WPARAM)hwnd, MAKELONG( hittest, WM_MOUSEMOVE ));
1765 /***********************************************************************
1766 * SysCommandSizeMove (X11DRV.@)
1768 * Perform SC_MOVE and SC_SIZE commands.
1770 void X11DRV_SysCommandSizeMove( HWND hwnd, WPARAM wParam )
1773 RECT sizingRect, mouseRect, origRect;
1776 LONG hittest = (LONG)(wParam & 0x0f);
1777 HCURSOR16 hDragCursor = 0, hOldCursor = 0;
1778 POINT minTrack, maxTrack;
1779 POINT capturePoint, pt;
1780 LONG style = GetWindowLongA( hwnd, GWL_STYLE );
1781 LONG exstyle = GetWindowLongA( hwnd, GWL_EXSTYLE );
1782 BOOL thickframe = HAS_THICKFRAME( style, exstyle );
1783 BOOL iconic = style & WS_MINIMIZE;
1785 DWORD dwPoint = GetMessagePos ();
1786 BOOL DragFullWindows = FALSE;
1789 Display *old_gdi_display = NULL;
1790 Display *display = thread_display();
1792 SystemParametersInfoA(SPI_GETDRAGFULLWINDOWS, 0, &DragFullWindows, 0);
1794 pt.x = SLOWORD(dwPoint);
1795 pt.y = SHIWORD(dwPoint);
1798 if (IsZoomed(hwnd) || !IsWindowVisible(hwnd) || (exstyle & WS_EX_MANAGED)) return;
1800 if ((wParam & 0xfff0) == SC_MOVE)
1802 if (!hittest) hittest = start_size_move( hwnd, wParam, &capturePoint, style );
1803 if (!hittest) return;
1807 if (!thickframe) return;
1808 if ( hittest && hittest != HTSYSMENU ) hittest += 2;
1812 hittest = start_size_move( hwnd, wParam, &capturePoint, style );
1821 /* Get min/max info */
1823 WINPOS_GetMinMaxInfo( hwnd, NULL, NULL, &minTrack, &maxTrack );
1824 GetWindowRect( hwnd, &sizingRect );
1825 if (style & WS_CHILD)
1827 parent = GetParent(hwnd);
1828 /* make sizing rect relative to parent */
1829 MapWindowPoints( 0, parent, (POINT*)&sizingRect, 2 );
1830 GetClientRect( parent, &mouseRect );
1835 SetRect(&mouseRect, 0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN));
1837 origRect = sizingRect;
1839 if (ON_LEFT_BORDER(hittest))
1841 mouseRect.left = max( mouseRect.left, sizingRect.right-maxTrack.x );
1842 mouseRect.right = min( mouseRect.right, sizingRect.right-minTrack.x );
1844 else if (ON_RIGHT_BORDER(hittest))
1846 mouseRect.left = max( mouseRect.left, sizingRect.left+minTrack.x );
1847 mouseRect.right = min( mouseRect.right, sizingRect.left+maxTrack.x );
1849 if (ON_TOP_BORDER(hittest))
1851 mouseRect.top = max( mouseRect.top, sizingRect.bottom-maxTrack.y );
1852 mouseRect.bottom = min( mouseRect.bottom,sizingRect.bottom-minTrack.y);
1854 else if (ON_BOTTOM_BORDER(hittest))
1856 mouseRect.top = max( mouseRect.top, sizingRect.top+minTrack.y );
1857 mouseRect.bottom = min( mouseRect.bottom, sizingRect.top+maxTrack.y );
1859 if (parent) MapWindowPoints( parent, 0, (LPPOINT)&mouseRect, 2 );
1861 /* Retrieve a default cache DC (without using the window style) */
1862 hdc = GetDCEx( parent, 0, DCX_CACHE );
1864 if( iconic ) /* create a cursor for dragging */
1866 HICON hIcon = GetClassLongA( hwnd, GCL_HICON);
1867 if(!hIcon) hIcon = (HICON)SendMessageA( hwnd, WM_QUERYDRAGICON, 0, 0L);
1868 if( hIcon ) hDragCursor = CURSORICON_IconToCursor( hIcon, TRUE );
1869 if( !hDragCursor ) iconic = FALSE;
1872 /* repaint the window before moving it around */
1873 RedrawWindow( hwnd, NULL, 0, RDW_UPDATENOW | RDW_ALLCHILDREN );
1875 SendMessageA( hwnd, WM_ENTERSIZEMOVE, 0, 0 );
1878 /* grab the server only when moving top-level windows without desktop */
1879 grab = (!DragFullWindows && !parent && (root_window == DefaultRootWindow(gdi_display)));
1884 XSync( gdi_display, False );
1885 XGrabServer( display );
1886 XSync( display, False );
1887 /* switch gdi display to the thread display, since the server is grabbed */
1888 old_gdi_display = gdi_display;
1889 gdi_display = display;
1891 XGrabPointer( display, X11DRV_get_whole_window(hwnd), False,
1892 PointerMotionMask | ButtonPressMask | ButtonReleaseMask,
1893 GrabModeAsync, GrabModeAsync,
1894 parent ? X11DRV_get_client_window(parent) : root_window,
1895 None, CurrentTime );
1896 wine_tsx11_unlock();
1902 if (!GetMessageW( &msg, 0, WM_KEYFIRST, WM_MOUSELAST )) break;
1903 if (CallMsgFilterW( &msg, MSGF_SIZE )) continue;
1905 /* Exit on button-up, Return, or Esc */
1906 if ((msg.message == WM_LBUTTONUP) ||
1907 ((msg.message == WM_KEYDOWN) &&
1908 ((msg.wParam == VK_RETURN) || (msg.wParam == VK_ESCAPE)))) break;
1910 if ((msg.message != WM_KEYDOWN) && (msg.message != WM_MOUSEMOVE))
1911 continue; /* We are not interested in other messages */
1915 if (msg.message == WM_KEYDOWN) switch(msg.wParam)
1917 case VK_UP: pt.y -= 8; break;
1918 case VK_DOWN: pt.y += 8; break;
1919 case VK_LEFT: pt.x -= 8; break;
1920 case VK_RIGHT: pt.x += 8; break;
1923 pt.x = max( pt.x, mouseRect.left );
1924 pt.x = min( pt.x, mouseRect.right );
1925 pt.y = max( pt.y, mouseRect.top );
1926 pt.y = min( pt.y, mouseRect.bottom );
1928 dx = pt.x - capturePoint.x;
1929 dy = pt.y - capturePoint.y;
1937 if( iconic ) /* ok, no system popup tracking */
1939 hOldCursor = SetCursor(hDragCursor);
1941 WINPOS_ShowIconTitle( hwnd, FALSE );
1943 else if(!DragFullWindows)
1944 draw_moving_frame( hdc, &sizingRect, thickframe );
1947 if (msg.message == WM_KEYDOWN) SetCursorPos( pt.x, pt.y );
1950 RECT newRect = sizingRect;
1951 WPARAM wpSizingHit = 0;
1953 if (hittest == HTCAPTION) OffsetRect( &newRect, dx, dy );
1954 if (ON_LEFT_BORDER(hittest)) newRect.left += dx;
1955 else if (ON_RIGHT_BORDER(hittest)) newRect.right += dx;
1956 if (ON_TOP_BORDER(hittest)) newRect.top += dy;
1957 else if (ON_BOTTOM_BORDER(hittest)) newRect.bottom += dy;
1958 if(!iconic && !DragFullWindows) draw_moving_frame( hdc, &sizingRect, thickframe );
1961 /* determine the hit location */
1962 if (hittest >= HTLEFT && hittest <= HTBOTTOMRIGHT)
1963 wpSizingHit = WMSZ_LEFT + (hittest - HTLEFT);
1964 SendMessageA( hwnd, WM_SIZING, wpSizingHit, (LPARAM)&newRect );
1968 if(!DragFullWindows)
1969 draw_moving_frame( hdc, &newRect, thickframe );
1971 /* To avoid any deadlocks, all the locks on the windows
1972 structures must be suspended before the SetWindowPos */
1973 iWndsLocks = WIN_SuspendWndsLock();
1974 SetWindowPos( hwnd, 0, newRect.left, newRect.top,
1975 newRect.right - newRect.left,
1976 newRect.bottom - newRect.top,
1977 ( hittest == HTCAPTION ) ? SWP_NOSIZE : 0 );
1978 WIN_RestoreWndsLock(iWndsLocks);
1981 sizingRect = newRect;
1989 if( moved ) /* restore cursors, show icon title later on */
1991 ShowCursor( FALSE );
1992 SetCursor( hOldCursor );
1994 DestroyCursor( hDragCursor );
1996 else if (moved && !DragFullWindows)
1997 draw_moving_frame( hdc, &sizingRect, thickframe );
1999 ReleaseDC( parent, hdc );
2002 XUngrabPointer( display, CurrentTime );
2005 XSync( display, False );
2006 XUngrabServer( display );
2007 XSync( display, False );
2008 gdi_display = old_gdi_display;
2010 wine_tsx11_unlock();
2012 if (HOOK_CallHooksA( WH_CBT, HCBT_MOVESIZE, (WPARAM)hwnd, (LPARAM)&sizingRect )) moved = FALSE;
2014 SendMessageA( hwnd, WM_EXITSIZEMOVE, 0, 0 );
2015 SendMessageA( hwnd, WM_SETVISIBLE, !IsIconic(hwnd), 0L);
2017 /* window moved or resized */
2020 /* To avoid any deadlocks, all the locks on the windows
2021 structures must be suspended before the SetWindowPos */
2022 iWndsLocks = WIN_SuspendWndsLock();
2024 /* if the moving/resizing isn't canceled call SetWindowPos
2025 * with the new position or the new size of the window
2027 if (!((msg.message == WM_KEYDOWN) && (msg.wParam == VK_ESCAPE)) )
2029 /* NOTE: SWP_NOACTIVATE prevents document window activation in Word 6 */
2030 if(!DragFullWindows)
2031 SetWindowPos( hwnd, 0, sizingRect.left, sizingRect.top,
2032 sizingRect.right - sizingRect.left,
2033 sizingRect.bottom - sizingRect.top,
2034 ( hittest == HTCAPTION ) ? SWP_NOSIZE : 0 );
2037 { /* restore previous size/position */
2039 SetWindowPos( hwnd, 0, origRect.left, origRect.top,
2040 origRect.right - origRect.left,
2041 origRect.bottom - origRect.top,
2042 ( hittest == HTCAPTION ) ? SWP_NOSIZE : 0 );
2045 WIN_RestoreWndsLock(iWndsLocks);
2050 /* Single click brings up the system menu when iconized */
2054 if(style & WS_SYSMENU )
2055 SendMessageA( hwnd, WM_SYSCOMMAND,
2056 SC_MOUSEMENU + HTSYSMENU, MAKELONG(pt.x,pt.y));
2058 else WINPOS_ShowIconTitle( hwnd, TRUE );
2063 /***********************************************************************
2064 * ForceWindowRaise (X11DRV.@)
2066 * Raise a window on top of the X stacking order, while preserving
2067 * the correct Windows Z order.
2069 * FIXME: this should go away.
2071 void X11DRV_ForceWindowRaise( HWND hwnd )
2075 XWindowChanges winChanges;
2076 Display *display = thread_display();
2077 WND *wndPtr = WIN_FindWndPtr( hwnd );
2079 if (!wndPtr) return;
2081 if ((wndPtr->dwExStyle & WS_EX_MANAGED) ||
2082 wndPtr->parent != GetDesktopWindow() ||
2083 IsRectEmpty( &wndPtr->rectWindow ) ||
2084 !get_whole_window(wndPtr))
2086 WIN_ReleaseWndPtr( wndPtr );
2089 WIN_ReleaseWndPtr( wndPtr );
2091 /* Raise all windows up to wndPtr according to their Z order.
2092 * (it would be easier with sibling-related Below but it doesn't
2093 * work very well with SGI mwm for instance)
2095 winChanges.stack_mode = Above;
2096 if (!(list = WIN_ListChildren( GetDesktopWindow() ))) return;
2097 while (list[i] && list[i] != hwnd) i++;
2100 for ( ; i >= 0; i--)
2102 WND *ptr = WIN_FindWndPtr( list[i] );
2104 if (!IsRectEmpty( &ptr->rectWindow ) && get_whole_window(ptr))
2105 TSXReconfigureWMWindow( display, get_whole_window(ptr), 0,
2106 CWStackMode, &winChanges );
2107 WIN_ReleaseWndPtr( ptr );
2110 HeapFree( GetProcessHeap(), 0, list );