2 * Window position related functions.
4 * Copyright 1993, 1994, 1995, 2001 Alexandre Julliard
5 * Copyright 1995, 1996, 1999 Alex Korobka
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 #include <X11/IntrinsicP.h>
27 #include <X11/extensions/shape.h>
28 #endif /* HAVE_LIBXSHAPE */
38 #include "wine/wingdi16.h"
44 #include "nonclient.h"
46 #include "wine/server.h"
47 #include "wine/debug.h"
49 WINE_DEFAULT_DEBUG_CHANNEL(x11drv);
51 #define SWP_AGG_NOGEOMETRYCHANGE \
52 (SWP_NOSIZE | SWP_NOMOVE | SWP_NOCLIENTSIZE | SWP_NOCLIENTMOVE)
53 #define SWP_AGG_NOPOSCHANGE \
54 (SWP_AGG_NOGEOMETRYCHANGE | SWP_NOZORDER)
55 #define SWP_AGG_STATUSFLAGS \
56 (SWP_AGG_NOPOSCHANGE | SWP_FRAMECHANGED | SWP_HIDEWINDOW | SWP_SHOWWINDOW)
58 #define SWP_EX_NOCOPY 0x0001
59 #define SWP_EX_PAINTSELF 0x0002
60 #define SWP_EX_NONCLIENT 0x0004
62 #define HAS_THICKFRAME(style,exStyle) \
63 (((style) & WS_THICKFRAME) && \
64 !(((style) & (WS_DLGFRAME|WS_BORDER)) == WS_DLGFRAME))
66 #define ON_LEFT_BORDER(hit) \
67 (((hit) == HTLEFT) || ((hit) == HTTOPLEFT) || ((hit) == HTBOTTOMLEFT))
68 #define ON_RIGHT_BORDER(hit) \
69 (((hit) == HTRIGHT) || ((hit) == HTTOPRIGHT) || ((hit) == HTBOTTOMRIGHT))
70 #define ON_TOP_BORDER(hit) \
71 (((hit) == HTTOP) || ((hit) == HTTOPLEFT) || ((hit) == HTTOPRIGHT))
72 #define ON_BOTTOM_BORDER(hit) \
73 (((hit) == HTBOTTOM) || ((hit) == HTBOTTOMLEFT) || ((hit) == HTBOTTOMRIGHT))
75 #define _NET_WM_MOVERESIZE_SIZE_TOPLEFT 0
76 #define _NET_WM_MOVERESIZE_SIZE_TOP 1
77 #define _NET_WM_MOVERESIZE_SIZE_TOPRIGHT 2
78 #define _NET_WM_MOVERESIZE_SIZE_RIGHT 3
79 #define _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT 4
80 #define _NET_WM_MOVERESIZE_SIZE_BOTTOM 5
81 #define _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT 6
82 #define _NET_WM_MOVERESIZE_SIZE_LEFT 7
83 #define _NET_WM_MOVERESIZE_MOVE 8 /* movement only */
84 #define _NET_WM_MOVERESIZE_SIZE_KEYBOARD 9 /* size via keyboard */
85 #define _NET_WM_MOVERESIZE_MOVE_KEYBOARD 10 /* move via keyboard */
88 /***********************************************************************
89 * get_server_visible_region
91 static HRGN get_server_visible_region( HWND hwnd, HWND top, UINT flags )
100 if (!(data = HeapAlloc( GetProcessHeap(), 0, sizeof(*data) + size - 1 ))) return 0;
101 SERVER_START_REQ( get_visible_region )
106 wine_server_set_reply( req, data->Buffer, size );
107 if (!(status = wine_server_call( req )))
109 size_t reply_size = wine_server_reply_size( reply );
110 data->rdh.dwSize = sizeof(data->rdh);
111 data->rdh.iType = RDH_RECTANGLES;
112 data->rdh.nCount = reply_size / sizeof(RECT);
113 data->rdh.nRgnSize = reply_size;
114 ret = ExtCreateRegion( NULL, size, data );
116 else size = reply->total_size;
119 HeapFree( GetProcessHeap(), 0, data );
120 } while (status == STATUS_BUFFER_OVERFLOW);
122 if (status) SetLastError( RtlNtStatusToDosError(status) );
127 /***********************************************************************
128 * get_top_clipping_window
130 * Get the top window to clip against (i.e. the top parent that has
131 * an associated X window).
133 static HWND get_top_clipping_window( HWND hwnd )
137 if (!using_wine_desktop) ret = GetAncestor( hwnd, GA_ROOT );
138 if (!ret) ret = GetDesktopWindow();
143 /***********************************************************************
146 * Expose a region of a given window.
148 static void expose_window( HWND hwnd, RECT *rect, HRGN rgn, int flags )
155 /* find the top most parent that doesn't clip children or siblings and
156 * invalidate the area on its parent, including all children */
157 if ((list = WIN_ListParents( hwnd )))
160 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
161 for (i = 0; list[i] && list[i] != GetDesktopWindow(); i++)
163 if (!(style & WS_CLIPSIBLINGS)) top = current;
164 style = GetWindowLongW( list[i], GWL_STYLE );
165 if (!(style & WS_CLIPCHILDREN)) top = current;
171 /* find the parent of the top window, reusing the parent list */
172 if (top == hwnd) i = 0;
175 for (i = 0; list[i]; i++) if (list[i] == top) break;
176 if (list[i] && list[i+1]) i++;
178 if (list[i] != GetDesktopWindow()) top = list[i];
179 flags &= ~RDW_FRAME; /* parent will invalidate children frame anyway */
180 flags |= RDW_ALLCHILDREN;
182 HeapFree( GetProcessHeap(), 0, list );
185 if (!top) top = hwnd;
187 /* make coords relative to top */
188 offset.x = offset.y = 0;
189 MapWindowPoints( hwnd, top, &offset, 1 );
193 OffsetRect( rect, offset.x, offset.y );
194 RedrawWindow( top, rect, 0, flags );
198 OffsetRgn( rgn, offset.x, offset.y );
199 RedrawWindow( top, NULL, rgn, flags );
204 /***********************************************************************
207 void X11DRV_Expose( HWND hwnd, XExposeEvent *event )
210 struct x11drv_win_data *data;
211 int flags = RDW_INVALIDATE | RDW_ERASE;
214 TRACE( "win %p (%lx) %d,%d %dx%d\n",
215 hwnd, event->window, event->x, event->y, event->width, event->height );
217 rect.left = event->x;
219 rect.right = rect.left + event->width;
220 rect.bottom = rect.top + event->height;
222 if (!(win = WIN_GetPtr( hwnd ))) return;
223 data = win->pDriverData;
225 if (event->window != data->client_window) /* whole window or icon window */
228 /* make position relative to client area instead of window */
229 OffsetRect( &rect, -data->client_rect.left, -data->client_rect.top );
231 WIN_ReleasePtr( win );
233 expose_window( hwnd, &rect, 0, flags );
237 /***********************************************************************
240 * Set the drawable, origin and dimensions for the DC associated to
243 BOOL X11DRV_GetDC( HWND hwnd, HDC hdc, HRGN hrgn, DWORD flags )
245 HWND top = get_top_clipping_window( hwnd );
246 WND *win = WIN_GetPtr( hwnd );
247 X11DRV_WND_DATA *data = win->pDriverData;
248 struct x11drv_escape_set_drawable escape;
250 escape.mode = IncludeInferiors;
251 /* don't clip siblings if using parent clip region */
252 if (flags & DCX_PARENTCLIP) flags &= ~DCX_CLIPSIBLINGS;
256 /* find the top most parent that doesn't clip siblings */
257 HWND clipping_parent = 0;
258 HWND *list = WIN_ListParents( hwnd );
262 for (i = 0; list[i] != top; i++)
264 LONG style = GetWindowLongW( list[i], GWL_STYLE );
265 if (!(style & WS_CLIPSIBLINGS)) clipping_parent = list[i];
267 HeapFree( GetProcessHeap(), 0, list );
270 clipping_parent = GetAncestor( clipping_parent, GA_PARENT );
271 else if (!(flags & DCX_CLIPSIBLINGS) || (flags & DCX_WINDOW))
272 clipping_parent = GetAncestor( hwnd, GA_PARENT );
274 clipping_parent = hwnd;
276 escape.org.x = escape.org.y = 0;
277 escape.drawable_org.x = escape.drawable_org.y = 0;
278 if (flags & DCX_WINDOW)
280 escape.org.x = win->rectWindow.left - win->rectClient.left;
281 escape.org.y = win->rectWindow.top - win->rectClient.top;
283 MapWindowPoints( hwnd, clipping_parent, &escape.org, 1 );
284 MapWindowPoints( clipping_parent, 0, &escape.drawable_org, 1 );
285 escape.drawable = X11DRV_get_client_window( clipping_parent );
289 if (IsIconic( hwnd ))
291 escape.drawable = data->icon_window ? data->icon_window : data->whole_window;
294 escape.drawable_org = escape.org;
296 else if (flags & DCX_WINDOW)
298 escape.drawable = data->whole_window;
299 escape.drawable_org.x = data->whole_rect.left;
300 escape.drawable_org.y = data->whole_rect.top;
301 escape.org.x = win->rectWindow.left - data->whole_rect.left;
302 escape.org.y = win->rectWindow.top - data->whole_rect.top;
306 escape.drawable = data->client_window;
307 escape.drawable_org.x = win->rectClient.left;
308 escape.drawable_org.y = win->rectClient.top;
314 escape.code = X11DRV_SET_DRAWABLE;
315 ExtEscape( hdc, X11DRV_ESCAPE, sizeof(escape), (LPSTR)&escape, 0, NULL );
317 if (flags & (DCX_EXCLUDERGN | DCX_INTERSECTRGN) ||
318 SetHookFlags16( HDC_16(hdc), DCHF_VALIDATEVISRGN )) /* DC was dirty */
320 /* need to recompute the visible region */
321 HRGN visRgn = get_server_visible_region( hwnd, top, flags );
323 if (flags & (DCX_EXCLUDERGN | DCX_INTERSECTRGN))
324 CombineRgn( visRgn, visRgn, hrgn, (flags & DCX_INTERSECTRGN) ? RGN_AND : RGN_DIFF );
326 SelectVisRgn16( HDC_16(hdc), HRGN_16(visRgn) );
327 DeleteObject( visRgn );
330 WIN_ReleasePtr( win );
335 /***********************************************************************
336 * ReleaseDC (X11DRV.@)
338 void X11DRV_ReleaseDC( HWND hwnd, HDC hdc )
340 struct x11drv_escape_set_drawable escape;
342 escape.code = X11DRV_SET_DRAWABLE;
343 escape.drawable = root_window;
344 escape.mode = IncludeInferiors;
345 escape.org.x = escape.org.y = 0;
346 escape.drawable_org.x = escape.drawable_org.y = 0;
348 ExtEscape( hdc, X11DRV_ESCAPE, sizeof(escape), (LPSTR)&escape, 0, NULL );
352 /***********************************************************************
353 * SWP_DoWinPosChanging
355 static BOOL SWP_DoWinPosChanging( WINDOWPOS* pWinpos, RECT* pNewWindowRect, RECT* pNewClientRect )
359 /* Send WM_WINDOWPOSCHANGING message */
361 if (!(pWinpos->flags & SWP_NOSENDCHANGING))
362 SendMessageW( pWinpos->hwnd, WM_WINDOWPOSCHANGING, 0, (LPARAM)pWinpos );
364 if (!(wndPtr = WIN_GetPtr( pWinpos->hwnd )) || wndPtr == WND_OTHER_PROCESS) return FALSE;
366 /* Calculate new position and size */
368 *pNewWindowRect = wndPtr->rectWindow;
369 *pNewClientRect = (wndPtr->dwStyle & WS_MINIMIZE) ? wndPtr->rectWindow
370 : wndPtr->rectClient;
372 if (!(pWinpos->flags & SWP_NOSIZE))
374 pNewWindowRect->right = pNewWindowRect->left + pWinpos->cx;
375 pNewWindowRect->bottom = pNewWindowRect->top + pWinpos->cy;
377 if (!(pWinpos->flags & SWP_NOMOVE))
379 pNewWindowRect->left = pWinpos->x;
380 pNewWindowRect->top = pWinpos->y;
381 pNewWindowRect->right += pWinpos->x - wndPtr->rectWindow.left;
382 pNewWindowRect->bottom += pWinpos->y - wndPtr->rectWindow.top;
384 OffsetRect( pNewClientRect, pWinpos->x - wndPtr->rectWindow.left,
385 pWinpos->y - wndPtr->rectWindow.top );
387 pWinpos->flags |= SWP_NOCLIENTMOVE | SWP_NOCLIENTSIZE;
389 TRACE( "hwnd %p, after %p, swp %d,%d %dx%d flags %08x\n",
390 pWinpos->hwnd, pWinpos->hwndInsertAfter, pWinpos->x, pWinpos->y,
391 pWinpos->cx, pWinpos->cy, pWinpos->flags );
392 TRACE( "current %s style %08lx new %s\n",
393 wine_dbgstr_rect( &wndPtr->rectWindow ), wndPtr->dwStyle,
394 wine_dbgstr_rect( pNewWindowRect ));
396 WIN_ReleasePtr( wndPtr );
400 /***********************************************************************
403 static UINT SWP_DoNCCalcSize( WINDOWPOS* pWinpos, const RECT* pNewWindowRect, RECT* pNewClientRect )
408 if (!(wndPtr = WIN_GetPtr( pWinpos->hwnd )) || wndPtr == WND_OTHER_PROCESS) return 0;
410 /* Send WM_NCCALCSIZE message to get new client area */
411 if( (pWinpos->flags & (SWP_FRAMECHANGED | SWP_NOSIZE)) != SWP_NOSIZE )
413 NCCALCSIZE_PARAMS params;
414 WINDOWPOS winposCopy;
416 params.rgrc[0] = *pNewWindowRect;
417 params.rgrc[1] = wndPtr->rectWindow;
418 params.rgrc[2] = wndPtr->rectClient;
419 params.lppos = &winposCopy;
420 winposCopy = *pWinpos;
421 WIN_ReleasePtr( wndPtr );
423 wvrFlags = SendMessageW( pWinpos->hwnd, WM_NCCALCSIZE, TRUE, (LPARAM)¶ms );
425 TRACE( "(%ld,%ld)-(%ld,%ld)\n", params.rgrc[0].left, params.rgrc[0].top,
426 params.rgrc[0].right, params.rgrc[0].bottom );
428 /* If the application sends back garbage, ignore it */
430 if (params.rgrc[0].left < pNewWindowRect->left) params.rgrc[0].left = pNewWindowRect->left;
431 if (params.rgrc[0].top < pNewWindowRect->top) params.rgrc[0].top = pNewWindowRect->top;
432 if (params.rgrc[0].right > pNewWindowRect->right) params.rgrc[0].right = pNewWindowRect->right;
433 if (params.rgrc[0].bottom > pNewWindowRect->bottom) params.rgrc[0].bottom = pNewWindowRect->bottom;
435 if (params.rgrc[0].left <= params.rgrc[0].right &&
436 params.rgrc[0].top <= params.rgrc[0].bottom)
437 *pNewClientRect = params.rgrc[0];
439 if (!(wndPtr = WIN_GetPtr( pWinpos->hwnd )) || wndPtr == WND_OTHER_PROCESS) return 0;
441 if( pNewClientRect->left != wndPtr->rectClient.left ||
442 pNewClientRect->top != wndPtr->rectClient.top )
443 pWinpos->flags &= ~SWP_NOCLIENTMOVE;
445 if( (pNewClientRect->right - pNewClientRect->left !=
446 wndPtr->rectClient.right - wndPtr->rectClient.left) ||
447 (pNewClientRect->bottom - pNewClientRect->top !=
448 wndPtr->rectClient.bottom - wndPtr->rectClient.top) )
449 pWinpos->flags &= ~SWP_NOCLIENTSIZE;
453 if (!(pWinpos->flags & SWP_NOMOVE) &&
454 (pNewClientRect->left != wndPtr->rectClient.left ||
455 pNewClientRect->top != wndPtr->rectClient.top))
456 pWinpos->flags &= ~SWP_NOCLIENTMOVE;
458 WIN_ReleasePtr( wndPtr );
463 /***********************************************************************
466 * fix Z order taking into account owned popups -
467 * basically we need to maintain them above the window that owns them
469 * FIXME: hide/show owned popups when owner visibility changes.
471 static HWND SWP_DoOwnedPopups(HWND hwnd, HWND hwndInsertAfter)
474 HWND owner = GetWindow( hwnd, GW_OWNER );
475 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
477 WARN("(%p) hInsertAfter = %p\n", hwnd, hwndInsertAfter );
479 if ((style & WS_POPUP) && owner)
481 /* make sure this popup stays above the owner */
483 HWND hwndLocalPrev = HWND_TOP;
485 if( hwndInsertAfter != HWND_TOP )
487 if ((list = WIN_ListChildren( GetDesktopWindow() )))
490 for (i = 0; list[i]; i++)
492 if (list[i] == owner) break;
493 if (list[i] != hwnd) hwndLocalPrev = list[i];
494 if (hwndLocalPrev == hwndInsertAfter) break;
496 hwndInsertAfter = hwndLocalPrev;
500 else if (style & WS_CHILD) return hwndInsertAfter;
502 if (!list) list = WIN_ListChildren( GetDesktopWindow() );
506 for (i = 0; list[i]; i++)
508 if (list[i] == hwnd) break;
509 if ((GetWindowLongW( list[i], GWL_STYLE ) & WS_POPUP) &&
510 GetWindow( list[i], GW_OWNER ) == hwnd)
512 SetWindowPos( list[i], hwndInsertAfter, 0, 0, 0, 0,
513 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE |
514 SWP_NOSENDCHANGING | SWP_DEFERERASE );
515 hwndInsertAfter = list[i];
518 HeapFree( GetProcessHeap(), 0, list );
521 return hwndInsertAfter;
525 /* fix redundant flags and values in the WINDOWPOS structure */
526 static BOOL fixup_flags( WINDOWPOS *winpos )
528 WND *wndPtr = WIN_GetPtr( winpos->hwnd );
531 if (!wndPtr || wndPtr == WND_OTHER_PROCESS)
533 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
536 winpos->hwnd = wndPtr->hwndSelf; /* make it a full handle */
538 /* Finally make sure that all coordinates are valid */
539 if (winpos->x < -32768) winpos->x = -32768;
540 else if (winpos->x > 32767) winpos->x = 32767;
541 if (winpos->y < -32768) winpos->y = -32768;
542 else if (winpos->y > 32767) winpos->y = 32767;
544 if (winpos->cx < 0) winpos->cx = 0;
545 else if (winpos->cx > 32767) winpos->cx = 32767;
546 if (winpos->cy < 0) winpos->cy = 0;
547 else if (winpos->cy > 32767) winpos->cy = 32767;
549 if (wndPtr->dwStyle & WS_VISIBLE) winpos->flags &= ~SWP_SHOWWINDOW;
552 winpos->flags &= ~SWP_HIDEWINDOW;
553 if (!(winpos->flags & SWP_SHOWWINDOW)) winpos->flags |= SWP_NOREDRAW;
556 if ((wndPtr->rectWindow.right - wndPtr->rectWindow.left == winpos->cx) &&
557 (wndPtr->rectWindow.bottom - wndPtr->rectWindow.top == winpos->cy))
558 winpos->flags |= SWP_NOSIZE; /* Already the right size */
560 if ((wndPtr->rectWindow.left == winpos->x) && (wndPtr->rectWindow.top == winpos->y))
561 winpos->flags |= SWP_NOMOVE; /* Already the right position */
563 if ((wndPtr->dwStyle & (WS_POPUP | WS_CHILD)) != WS_CHILD)
565 if (!(winpos->flags & SWP_NOACTIVATE)) /* Bring to the top when activating */
567 winpos->flags &= ~SWP_NOZORDER;
568 winpos->hwndInsertAfter = HWND_TOP;
572 /* Check hwndInsertAfter */
573 if (winpos->flags & SWP_NOZORDER) goto done;
575 /* fix sign extension */
576 if (winpos->hwndInsertAfter == (HWND)0xffff) winpos->hwndInsertAfter = HWND_TOPMOST;
577 else if (winpos->hwndInsertAfter == (HWND)0xfffe) winpos->hwndInsertAfter = HWND_NOTOPMOST;
579 /* FIXME: TOPMOST not supported yet */
580 if ((winpos->hwndInsertAfter == HWND_TOPMOST) ||
581 (winpos->hwndInsertAfter == HWND_NOTOPMOST)) winpos->hwndInsertAfter = HWND_TOP;
583 /* hwndInsertAfter must be a sibling of the window */
584 if (winpos->hwndInsertAfter == HWND_TOP)
586 if (GetWindow(winpos->hwnd, GW_HWNDFIRST) == winpos->hwnd)
587 winpos->flags |= SWP_NOZORDER;
589 else if (winpos->hwndInsertAfter == HWND_BOTTOM)
591 if (GetWindow(winpos->hwnd, GW_HWNDLAST) == winpos->hwnd)
592 winpos->flags |= SWP_NOZORDER;
596 if (GetAncestor( winpos->hwndInsertAfter, GA_PARENT ) != wndPtr->parent) ret = FALSE;
599 /* don't need to change the Zorder of hwnd if it's already inserted
600 * after hwndInsertAfter or when inserting hwnd after itself.
602 if ((winpos->hwnd == winpos->hwndInsertAfter) ||
603 (winpos->hwnd == GetWindow( winpos->hwndInsertAfter, GW_HWNDNEXT )))
604 winpos->flags |= SWP_NOZORDER;
608 WIN_ReleasePtr( wndPtr );
613 /***********************************************************************
614 * SetWindowStyle (X11DRV.@)
616 * Update the X state of a window to reflect a style change
618 void X11DRV_SetWindowStyle( HWND hwnd, LONG oldStyle )
620 Display *display = thread_display();
624 if (hwnd == GetDesktopWindow()) return;
625 if (!(wndPtr = WIN_GetPtr( hwnd ))) return;
626 if (wndPtr == WND_OTHER_PROCESS) return;
628 changed = wndPtr->dwStyle ^ oldStyle;
630 if (changed & WS_VISIBLE)
632 if (X11DRV_is_window_rect_mapped( &wndPtr->rectWindow ))
634 if (wndPtr->dwStyle & WS_VISIBLE)
636 TRACE( "mapping win %p\n", hwnd );
637 if (is_window_top_level(wndPtr))
639 X11DRV_sync_window_style( display, wndPtr );
640 X11DRV_set_wm_hints( display, wndPtr );
643 XMapWindow( display, get_whole_window(wndPtr) );
646 else if (!is_window_top_level(wndPtr)) /* don't unmap managed windows */
648 TRACE( "unmapping win %p\n", hwnd );
650 XUnmapWindow( display, get_whole_window(wndPtr) );
656 if (changed & WS_DISABLED)
658 if (wndPtr->dwExStyle & WS_EX_MANAGED)
662 if (!(wm_hints = XGetWMHints( display, get_whole_window(wndPtr) )))
663 wm_hints = XAllocWMHints();
666 wm_hints->flags |= InputHint;
667 wm_hints->input = !(wndPtr->dwStyle & WS_DISABLED);
668 XSetWMHints( display, get_whole_window(wndPtr), wm_hints );
674 WIN_ReleasePtr(wndPtr);
678 /***********************************************************************
679 * X11DRV_set_window_pos
681 * Set a window position and Z order.
683 BOOL X11DRV_set_window_pos( HWND hwnd, HWND insert_after, const RECT *rectWindow,
684 const RECT *rectClient, UINT swp_flags, UINT wvr_flags )
686 HWND top = get_top_clipping_window( hwnd );
687 WND *win = WIN_GetPtr( hwnd );
688 DWORD old_style, new_style;
691 if (!win) return FALSE;
692 if (win == WND_OTHER_PROCESS)
694 if (IsWindow( hwnd )) ERR( "cannot set rectangles of other process window %p\n", hwnd );
697 old_style = win->dwStyle;
698 SERVER_START_REQ( set_window_pos )
702 req->previous = insert_after;
703 req->flags = swp_flags & ~SWP_WINE_NOHOSTMOVE;
704 req->redraw_flags = wvr_flags;
705 req->window.left = rectWindow->left;
706 req->window.top = rectWindow->top;
707 req->window.right = rectWindow->right;
708 req->window.bottom = rectWindow->bottom;
709 req->client.left = rectClient->left;
710 req->client.top = rectClient->top;
711 req->client.right = rectClient->right;
712 req->client.bottom = rectClient->bottom;
713 ret = !wine_server_call( req );
714 new_style = reply->new_style;
720 struct x11drv_win_data *data = win->pDriverData;
721 Display *display = thread_display();
723 /* invalidate DCEs */
725 if ((((swp_flags & SWP_AGG_NOPOSCHANGE) != SWP_AGG_NOPOSCHANGE) && (new_style & WS_VISIBLE)) ||
726 (swp_flags & (SWP_HIDEWINDOW | SWP_SHOWWINDOW)))
729 UnionRect( &rect, rectWindow, &win->rectWindow );
730 DCE_InvalidateDCE( hwnd, &rect );
733 win->rectWindow = *rectWindow;
734 win->rectClient = *rectClient;
735 win->dwStyle = new_style;
737 TRACE( "win %p window %s client %s style %08lx\n",
738 hwnd, wine_dbgstr_rect(rectWindow), wine_dbgstr_rect(rectClient), new_style );
740 /* FIXME: copy the valid bits */
742 if (swp_flags & SWP_WINE_NOHOSTMOVE)
744 data->whole_rect = *rectWindow;
745 X11DRV_window_to_X_rect( win, &data->whole_rect );
746 if (data->client_window != data->whole_window)
747 X11DRV_sync_client_window_position( display, win );
749 else if (data->whole_window) /* don't do anything if X window not created yet */
751 if ((old_style & WS_VISIBLE) && !(new_style & WS_VISIBLE))
753 /* window got hidden, unmap it */
754 TRACE( "unmapping win %p\n", hwnd );
756 XUnmapWindow( thread_display(), data->whole_window );
759 else if ((new_style & WS_VISIBLE) && !X11DRV_is_window_rect_mapped( rectWindow ))
761 /* resizing to zero size or off screen -> unmap */
762 TRACE( "unmapping zero size or off-screen win %p\n", hwnd );
764 XUnmapWindow( display, data->whole_window );
768 X11DRV_sync_whole_window_position( display, win, !(swp_flags & SWP_NOZORDER) );
769 X11DRV_sync_client_window_position( display, win );
771 if (!(old_style & WS_VISIBLE) && (new_style & WS_VISIBLE))
773 /* window got shown, map it */
774 if (X11DRV_is_window_rect_mapped( rectWindow ))
776 TRACE( "mapping win %p\n", hwnd );
777 if (is_window_top_level(win))
779 X11DRV_sync_window_style( display, win );
780 X11DRV_set_wm_hints( display, win );
783 XMapWindow( display, data->whole_window );
787 else if ((new_style & WS_VISIBLE) && X11DRV_is_window_rect_mapped( rectWindow ))
789 /* resizing from zero size to non-zero -> map */
790 TRACE( "mapping non zero size or off-screen win %p\n", hwnd );
792 XMapWindow( display, data->whole_window );
796 XFlush( display ); /* FIXME: should not be necessary */
800 WIN_ReleasePtr( win );
805 /***********************************************************************
806 * SetWindowPos (X11DRV.@)
808 BOOL X11DRV_SetWindowPos( WINDOWPOS *winpos )
810 RECT newWindowRect, newClientRect;
811 UINT wvr_flags, orig_flags;
813 TRACE( "hwnd %p, after %p, swp %d,%d %dx%d flags %08x\n",
814 winpos->hwnd, winpos->hwndInsertAfter, winpos->x, winpos->y,
815 winpos->cx, winpos->cy, winpos->flags);
817 orig_flags = winpos->flags;
818 winpos->flags &= ~SWP_WINE_NOHOSTMOVE;
820 /* Check window handle */
821 if (winpos->hwnd == GetDesktopWindow()) return FALSE;
823 /* First make sure that coordinates are valid for WM_WINDOWPOSCHANGING */
824 if (!(winpos->flags & SWP_NOMOVE))
826 if (winpos->x < -32768) winpos->x = -32768;
827 else if (winpos->x > 32767) winpos->x = 32767;
828 if (winpos->y < -32768) winpos->y = -32768;
829 else if (winpos->y > 32767) winpos->y = 32767;
831 if (!(winpos->flags & SWP_NOSIZE))
833 if (winpos->cx < 0) winpos->cx = 0;
834 else if (winpos->cx > 32767) winpos->cx = 32767;
835 if (winpos->cy < 0) winpos->cy = 0;
836 else if (winpos->cy > 32767) winpos->cy = 32767;
839 if (!SWP_DoWinPosChanging( winpos, &newWindowRect, &newClientRect )) return FALSE;
841 /* Fix redundant flags */
842 if (!fixup_flags( winpos )) return FALSE;
844 if((winpos->flags & (SWP_NOZORDER | SWP_HIDEWINDOW | SWP_SHOWWINDOW)) != SWP_NOZORDER)
846 if (GetAncestor( winpos->hwnd, GA_PARENT ) == GetDesktopWindow())
847 winpos->hwndInsertAfter = SWP_DoOwnedPopups( winpos->hwnd, winpos->hwndInsertAfter );
850 /* Common operations */
852 wvr_flags = SWP_DoNCCalcSize( winpos, &newWindowRect, &newClientRect );
854 /* FIXME: actually do something with WVR_VALIDRECTS */
856 if (!X11DRV_set_window_pos( winpos->hwnd, winpos->hwndInsertAfter,
857 &newWindowRect, &newClientRect,
858 orig_flags, wvr_flags ))
861 if( winpos->flags & SWP_HIDEWINDOW )
862 HideCaret(winpos->hwnd);
863 else if (winpos->flags & SWP_SHOWWINDOW)
864 ShowCaret(winpos->hwnd);
866 if (!(winpos->flags & SWP_NOACTIVATE))
868 /* child windows get WM_CHILDACTIVATE message */
869 if ((GetWindowLongW( winpos->hwnd, GWL_STYLE ) & (WS_CHILD | WS_POPUP)) == WS_CHILD)
870 SendMessageA( winpos->hwnd, WM_CHILDACTIVATE, 0, 0 );
872 SetForegroundWindow( winpos->hwnd );
875 /* And last, send the WM_WINDOWPOSCHANGED message */
877 TRACE("\tstatus flags = %04x\n", winpos->flags & SWP_AGG_STATUSFLAGS);
879 if (((winpos->flags & SWP_AGG_STATUSFLAGS) != SWP_AGG_NOPOSCHANGE))
881 /* WM_WINDOWPOSCHANGED is sent even if SWP_NOSENDCHANGING is set
882 and always contains final window position.
884 winpos->x = newWindowRect.left;
885 winpos->y = newWindowRect.top;
886 winpos->cx = newWindowRect.right - newWindowRect.left;
887 winpos->cy = newWindowRect.bottom - newWindowRect.top;
888 SendMessageW( winpos->hwnd, WM_WINDOWPOSCHANGED, 0, (LPARAM)winpos );
895 /***********************************************************************
898 * Find a suitable place for an iconic window.
900 static POINT WINPOS_FindIconPos( WND* wndPtr, POINT pt )
904 short x, y, xspacing, yspacing;
906 GetClientRect( wndPtr->parent, &rectParent );
907 if ((pt.x >= rectParent.left) && (pt.x + GetSystemMetrics(SM_CXICON) < rectParent.right) &&
908 (pt.y >= rectParent.top) && (pt.y + GetSystemMetrics(SM_CYICON) < rectParent.bottom))
909 return pt; /* The icon already has a suitable position */
911 xspacing = GetSystemMetrics(SM_CXICONSPACING);
912 yspacing = GetSystemMetrics(SM_CYICONSPACING);
914 list = WIN_ListChildren( wndPtr->parent );
915 y = rectParent.bottom;
921 /* Check if another icon already occupies this spot */
922 /* FIXME: this is completely inefficient */
928 for (i = 0; list[i]; i++)
930 if (list[i] == wndPtr->hwndSelf) continue;
931 if (!IsIconic( list[i] )) continue;
932 if (!(childPtr = WIN_FindWndPtr( list[i] ))) continue;
933 if ((childPtr->rectWindow.left < x + xspacing) &&
934 (childPtr->rectWindow.right >= x) &&
935 (childPtr->rectWindow.top <= y) &&
936 (childPtr->rectWindow.bottom > y - yspacing))
938 WIN_ReleaseWndPtr( childPtr );
939 break; /* There's a window in there */
941 WIN_ReleaseWndPtr( childPtr );
945 /* found something here, try next spot */
951 /* No window was found, so it's OK for us */
952 pt.x = x + (xspacing - GetSystemMetrics(SM_CXICON)) / 2;
953 pt.y = y - (yspacing + GetSystemMetrics(SM_CYICON)) / 2;
954 HeapFree( GetProcessHeap(), 0, list );
957 } while(x <= rectParent.right-xspacing);
966 UINT WINPOS_MinMaximize( HWND hwnd, UINT cmd, LPRECT rect )
974 TRACE("%p %u\n", hwnd, cmd );
976 wpl.length = sizeof(wpl);
977 GetWindowPlacement( hwnd, &wpl );
979 if (HOOK_CallHooks( WH_CBT, HCBT_MINMAX, (WPARAM)hwnd, cmd, TRUE ))
980 return SWP_NOSIZE | SWP_NOMOVE;
982 if (IsIconic( hwnd ))
984 if (cmd == SW_MINIMIZE) return SWP_NOSIZE | SWP_NOMOVE;
985 if (!SendMessageA( hwnd, WM_QUERYOPEN, 0, 0 )) return SWP_NOSIZE | SWP_NOMOVE;
986 swpFlags |= SWP_NOCOPYBITS;
989 if (!(wndPtr = WIN_FindWndPtr( hwnd ))) return 0;
991 size.x = wndPtr->rectWindow.left;
992 size.y = wndPtr->rectWindow.top;
997 if( wndPtr->dwStyle & WS_MAXIMIZE) wndPtr->flags |= WIN_RESTORE_MAX;
998 else wndPtr->flags &= ~WIN_RESTORE_MAX;
1000 WIN_SetStyle( hwnd, (wndPtr->dwStyle & ~WS_MAXIMIZE) | WS_MINIMIZE );
1002 X11DRV_set_iconic_state( wndPtr );
1004 wpl.ptMinPosition = WINPOS_FindIconPos( wndPtr, wpl.ptMinPosition );
1006 SetRect( rect, wpl.ptMinPosition.x, wpl.ptMinPosition.y,
1007 GetSystemMetrics(SM_CXICON), GetSystemMetrics(SM_CYICON) );
1008 swpFlags |= SWP_NOCOPYBITS;
1012 WINPOS_GetMinMaxInfo( hwnd, &size, &wpl.ptMaxPosition, NULL, NULL );
1014 old_style = WIN_SetStyle( hwnd, (wndPtr->dwStyle & ~WS_MINIMIZE) | WS_MAXIMIZE );
1015 if (old_style & WS_MINIMIZE)
1017 WINPOS_ShowIconTitle( hwnd, FALSE );
1018 X11DRV_set_iconic_state( wndPtr );
1020 SetRect( rect, wpl.ptMaxPosition.x, wpl.ptMaxPosition.y, size.x, size.y );
1024 old_style = WIN_SetStyle( hwnd, wndPtr->dwStyle & ~(WS_MINIMIZE|WS_MAXIMIZE) );
1025 if (old_style & WS_MINIMIZE)
1027 WINPOS_ShowIconTitle( hwnd, FALSE );
1028 X11DRV_set_iconic_state( wndPtr );
1030 if( wndPtr->flags & WIN_RESTORE_MAX)
1032 /* Restore to maximized position */
1033 WINPOS_GetMinMaxInfo( hwnd, &size, &wpl.ptMaxPosition, NULL, NULL);
1034 WIN_SetStyle( hwnd, wndPtr->dwStyle | WS_MAXIMIZE );
1035 SetRect( rect, wpl.ptMaxPosition.x, wpl.ptMaxPosition.y, size.x, size.y );
1039 else if (!(old_style & WS_MAXIMIZE)) break;
1041 /* Restore to normal position */
1043 *rect = wpl.rcNormalPosition;
1044 rect->right -= rect->left;
1045 rect->bottom -= rect->top;
1050 WIN_ReleaseWndPtr( wndPtr );
1055 /***********************************************************************
1056 * ShowWindow (X11DRV.@)
1058 BOOL X11DRV_ShowWindow( HWND hwnd, INT cmd )
1060 WND* wndPtr = WIN_FindWndPtr( hwnd );
1061 BOOL wasVisible, showFlag;
1062 RECT newPos = {0, 0, 0, 0};
1065 if (!wndPtr) return FALSE;
1066 hwnd = wndPtr->hwndSelf; /* make it a full handle */
1068 wasVisible = (wndPtr->dwStyle & WS_VISIBLE) != 0;
1070 TRACE("hwnd=%p, cmd=%d, wasVisible %d\n", hwnd, cmd, wasVisible);
1075 if (!wasVisible) goto END;
1076 swp |= SWP_HIDEWINDOW | SWP_NOSIZE | SWP_NOMOVE |
1077 SWP_NOACTIVATE | SWP_NOZORDER;
1080 case SW_SHOWMINNOACTIVE:
1081 swp |= SWP_NOACTIVATE | SWP_NOZORDER;
1083 case SW_SHOWMINIMIZED:
1084 case SW_FORCEMINIMIZE: /* FIXME: Does not work if thread is hung. */
1085 swp |= SWP_SHOWWINDOW;
1088 swp |= SWP_FRAMECHANGED;
1089 if( !(wndPtr->dwStyle & WS_MINIMIZE) )
1090 swp |= WINPOS_MinMaximize( hwnd, SW_MINIMIZE, &newPos );
1091 else swp |= SWP_NOSIZE | SWP_NOMOVE;
1094 case SW_SHOWMAXIMIZED: /* same as SW_MAXIMIZE */
1095 swp |= SWP_SHOWWINDOW | SWP_FRAMECHANGED;
1096 if( !(wndPtr->dwStyle & WS_MAXIMIZE) )
1097 swp |= WINPOS_MinMaximize( hwnd, SW_MAXIMIZE, &newPos );
1098 else swp |= SWP_NOSIZE | SWP_NOMOVE;
1102 swp |= SWP_NOACTIVATE | SWP_NOZORDER;
1105 if (wasVisible) goto END;
1107 swp |= SWP_SHOWWINDOW | SWP_NOSIZE | SWP_NOMOVE;
1111 swp |= SWP_FRAMECHANGED;
1113 case SW_SHOWNOACTIVATE:
1114 swp |= SWP_NOACTIVATE | SWP_NOZORDER;
1116 case SW_SHOWNORMAL: /* same as SW_NORMAL: */
1117 case SW_SHOWDEFAULT: /* FIXME: should have its own handler */
1118 swp |= SWP_SHOWWINDOW;
1120 if( wndPtr->dwStyle & (WS_MINIMIZE | WS_MAXIMIZE) )
1121 swp |= WINPOS_MinMaximize( hwnd, SW_RESTORE, &newPos );
1122 else swp |= SWP_NOSIZE | SWP_NOMOVE;
1126 showFlag = (cmd != SW_HIDE);
1127 if (showFlag != wasVisible)
1129 SendMessageW( hwnd, WM_SHOWWINDOW, showFlag, 0 );
1130 if (!IsWindow( hwnd )) goto END;
1133 /* ShowWindow won't activate a not being maximized child window */
1134 if ((wndPtr->dwStyle & WS_CHILD) && cmd != SW_MAXIMIZE)
1135 swp |= SWP_NOACTIVATE | SWP_NOZORDER;
1137 SetWindowPos( hwnd, HWND_TOP, newPos.left, newPos.top,
1138 newPos.right, newPos.bottom, LOWORD(swp) );
1143 /* FIXME: This will cause the window to be activated irrespective
1144 * of whether it is owned by the same thread. Has to be done
1148 if (hwnd == GetActiveWindow())
1149 WINPOS_ActivateOtherWindow(hwnd);
1151 /* Revert focus to parent */
1152 hFocus = GetFocus();
1153 if (hwnd == hFocus || IsChild(hwnd, hFocus))
1155 HWND parent = GetAncestor(hwnd, GA_PARENT);
1156 if (parent == GetDesktopWindow()) parent = 0;
1160 if (!IsWindow( hwnd )) goto END;
1161 else if( wndPtr->dwStyle & WS_MINIMIZE ) WINPOS_ShowIconTitle( hwnd, TRUE );
1163 if (wndPtr->flags & WIN_NEED_SIZE)
1165 /* should happen only in CreateWindowEx() */
1166 int wParam = SIZE_RESTORED;
1168 wndPtr->flags &= ~WIN_NEED_SIZE;
1169 if (wndPtr->dwStyle & WS_MAXIMIZE) wParam = SIZE_MAXIMIZED;
1170 else if (wndPtr->dwStyle & WS_MINIMIZE) wParam = SIZE_MINIMIZED;
1171 SendMessageA( hwnd, WM_SIZE, wParam,
1172 MAKELONG(wndPtr->rectClient.right-wndPtr->rectClient.left,
1173 wndPtr->rectClient.bottom-wndPtr->rectClient.top));
1174 SendMessageA( hwnd, WM_MOVE, 0,
1175 MAKELONG(wndPtr->rectClient.left, wndPtr->rectClient.top) );
1179 WIN_ReleaseWndPtr(wndPtr);
1184 /**********************************************************************
1187 void X11DRV_MapNotify( HWND hwnd, XMapEvent *event )
1189 HWND hwndFocus = GetFocus();
1192 if (!(win = WIN_GetPtr( hwnd ))) return;
1194 if ((win->dwStyle & WS_VISIBLE) &&
1195 (win->dwStyle & WS_MINIMIZE) &&
1196 (win->dwExStyle & WS_EX_MANAGED))
1199 unsigned int width, height, border, depth;
1202 LONG style = (win->dwStyle & ~(WS_MINIMIZE|WS_MAXIMIZE)) | WS_VISIBLE;
1206 XGetGeometry( event->display, get_whole_window(win), &root, &x, &y, &width, &height,
1208 XTranslateCoordinates( event->display, get_whole_window(win), root, 0, 0, &x, &y, &top );
1209 wine_tsx11_unlock();
1212 rect.right = x + width;
1213 rect.bottom = y + height;
1214 X11DRV_X_to_window_rect( win, &rect );
1216 DCE_InvalidateDCE( hwnd, &win->rectWindow );
1218 if (win->flags & WIN_RESTORE_MAX) style |= WS_MAXIMIZE;
1219 WIN_SetStyle( hwnd, style );
1220 WIN_ReleasePtr( win );
1222 SendMessageA( hwnd, WM_SHOWWINDOW, SW_RESTORE, 0 );
1223 SetWindowPos( hwnd, 0, rect.left, rect.top, rect.right-rect.left, rect.bottom-rect.top,
1224 SWP_NOZORDER | SWP_WINE_NOHOSTMOVE );
1226 else WIN_ReleasePtr( win );
1227 if (hwndFocus && IsChild( hwnd, hwndFocus )) X11DRV_SetFocus(hwndFocus); /* FIXME */
1231 /**********************************************************************
1232 * X11DRV_UnmapNotify
1234 void X11DRV_UnmapNotify( HWND hwnd, XUnmapEvent *event )
1238 if (!(win = WIN_GetPtr( hwnd ))) return;
1240 if ((win->dwStyle & WS_VISIBLE) && (win->dwExStyle & WS_EX_MANAGED) &&
1241 X11DRV_is_window_rect_mapped( &win->rectWindow ))
1243 if (win->dwStyle & WS_MAXIMIZE)
1244 win->flags |= WIN_RESTORE_MAX;
1246 win->flags &= ~WIN_RESTORE_MAX;
1248 WIN_SetStyle( hwnd, (win->dwStyle & ~WS_MAXIMIZE) | WS_MINIMIZE );
1249 WIN_ReleasePtr( win );
1252 SendMessageA( hwnd, WM_SHOWWINDOW, SW_MINIMIZE, 0 );
1253 SetWindowPos( hwnd, 0, 0, 0, GetSystemMetrics(SM_CXICON), GetSystemMetrics(SM_CYICON),
1254 SWP_NOMOVE | SWP_NOACTIVATE | SWP_NOZORDER | SWP_WINE_NOHOSTMOVE );
1256 else WIN_ReleasePtr( win );
1260 /***********************************************************************
1263 * Synchronize internal z-order with the window manager's.
1265 static Window __get_common_ancestor( Display *display, Window A, Window B,
1266 Window** children, unsigned* total )
1268 /* find the real root window */
1270 Window root, *childrenB;
1274 while( A != B && A && B )
1276 XQueryTree( display, A, &root, &A, children, total );
1277 XQueryTree( display, B, &root, &B, &childrenB, &totalB );
1278 if( childrenB ) XFree( childrenB );
1279 if( *children ) XFree( *children ), *children = NULL;
1284 XQueryTree( display, A, &root, &B, children, total );
1285 wine_tsx11_unlock();
1288 wine_tsx11_unlock();
1292 static Window __get_top_decoration( Display *display, Window w, Window ancestor )
1294 Window* children, root, prev = w, parent = w;
1301 XQueryTree( display, w, &root, &parent, &children, &total );
1302 if( children ) XFree( children );
1303 } while( parent && parent != ancestor );
1304 wine_tsx11_unlock();
1305 TRACE("\t%08x -> %08x\n", (unsigned)prev, (unsigned)w );
1306 return ( parent ) ? w : 0 ;
1309 static unsigned __td_lookup( Window w, Window* list, unsigned max )
1312 for( i = max; i > 0; i-- ) if( list[i - 1] == w ) break;
1316 static HWND query_zorder( Display *display, HWND hWndCheck)
1318 HWND hwndInsertAfter = HWND_TOP;
1319 Window w, parent, *children = NULL;
1320 unsigned total, check, pos, best;
1321 HWND *list = WIN_ListChildren( GetDesktopWindow() );
1322 HWND hwndA = 0, hwndB = 0;
1325 /* find at least two managed windows */
1326 if (!list) return 0;
1327 for (i = 0; list[i]; i++)
1329 if (!(GetWindowLongW( list[i], GWL_EXSTYLE ) & WS_EX_MANAGED)) continue;
1330 if (!(GetWindowLongW( list[i], GWL_STYLE ) & WS_VISIBLE)) continue;
1331 if (!hwndA) hwndA = list[i];
1338 if (!hwndA || !hwndB) goto done;
1340 parent = __get_common_ancestor( display, X11DRV_get_whole_window(hwndA),
1341 X11DRV_get_whole_window(hwndB), &children, &total );
1342 if( parent && children )
1344 /* w is the ancestor if hWndCheck that is a direct descendant of 'parent' */
1346 w = __get_top_decoration( display, X11DRV_get_whole_window(hWndCheck), parent );
1348 if( w != children[total-1] ) /* check if at the top */
1350 /* X child at index 0 is at the bottom, at index total-1 is at the top */
1351 check = __td_lookup( w, children, total );
1354 /* go through all windows in Wine z-order... */
1355 for (i = 0; list[i]; i++)
1357 if (list[i] == hWndCheck) continue;
1358 if (!(GetWindowLongW( list[i], GWL_EXSTYLE ) & WS_EX_MANAGED)) continue;
1359 if (!(w = __get_top_decoration( display, X11DRV_get_whole_window(list[i]),
1360 parent ))) continue;
1361 pos = __td_lookup( w, children, total );
1362 if( pos < best && pos > check )
1364 /* find a nearest Wine window precedes hWndCheck in the real z-order */
1366 hwndInsertAfter = list[i];
1368 if( best - check == 1 ) break;
1373 if( children ) XFree( children );
1374 wine_tsx11_unlock();
1377 HeapFree( GetProcessHeap(), 0, list );
1378 return hwndInsertAfter;
1382 /***********************************************************************
1383 * X11DRV_handle_desktop_resize
1385 void X11DRV_handle_desktop_resize( unsigned int width, unsigned int height )
1388 HWND hwnd = GetDesktopWindow();
1390 screen_width = width;
1391 screen_height = height;
1392 TRACE("desktop %p change to (%dx%d)\n", hwnd, width, height);
1393 SetRect( &rect, 0, 0, width, height );
1394 X11DRV_set_window_pos( hwnd, 0, &rect, &rect, SWP_NOZORDER|SWP_NOMOVE|SWP_WINE_NOHOSTMOVE, 0 );
1395 SendMessageTimeoutW( HWND_BROADCAST, WM_DISPLAYCHANGE, screen_depth,
1396 MAKELPARAM( width, height ), SMTO_ABORTIFHUNG, 2000, NULL );
1400 /***********************************************************************
1401 * X11DRV_ConfigureNotify
1403 void X11DRV_ConfigureNotify( HWND hwnd, XConfigureEvent *event )
1405 HWND oldInsertAfter;
1406 struct x11drv_win_data *data;
1410 int x = event->x, y = event->y;
1412 if (!(win = WIN_GetPtr( hwnd ))) return;
1413 data = win->pDriverData;
1417 if (!event->send_event) /* normal event, need to map coordinates to the root */
1421 XTranslateCoordinates( event->display, data->whole_window, root_window,
1422 0, 0, &x, &y, &child );
1423 wine_tsx11_unlock();
1427 rect.right = x + event->width;
1428 rect.bottom = y + event->height;
1429 TRACE( "win %p new X rect %ld,%ld,%ldx%ld (event %d,%d,%dx%d)\n",
1430 hwnd, rect.left, rect.top, rect.right-rect.left, rect.bottom-rect.top,
1431 event->x, event->y, event->width, event->height );
1432 X11DRV_X_to_window_rect( win, &rect );
1433 WIN_ReleasePtr( win );
1436 winpos.x = rect.left;
1437 winpos.y = rect.top;
1438 winpos.cx = rect.right - rect.left;
1439 winpos.cy = rect.bottom - rect.top;
1440 winpos.flags = SWP_NOACTIVATE;
1442 /* Get Z-order (FIXME) */
1444 winpos.hwndInsertAfter = query_zorder( event->display, hwnd );
1446 /* needs to find the first Visible Window above the current one */
1447 oldInsertAfter = hwnd;
1450 oldInsertAfter = GetWindow( oldInsertAfter, GW_HWNDPREV );
1451 if (!oldInsertAfter)
1453 oldInsertAfter = HWND_TOP;
1456 if (GetWindowLongA( oldInsertAfter, GWL_STYLE ) & WS_VISIBLE) break;
1459 /* Compare what has changed */
1461 GetWindowRect( hwnd, &rect );
1462 if (rect.left == winpos.x && rect.top == winpos.y) winpos.flags |= SWP_NOMOVE;
1464 TRACE( "%p moving from (%ld,%ld) to (%d,%d)\n",
1465 hwnd, rect.left, rect.top, winpos.x, winpos.y );
1467 if ((rect.right - rect.left == winpos.cx && rect.bottom - rect.top == winpos.cy) ||
1469 (IsRectEmpty( &rect ) && winpos.cx == 1 && winpos.cy == 1))
1470 winpos.flags |= SWP_NOSIZE;
1472 TRACE( "%p resizing from (%ldx%ld) to (%dx%d)\n",
1473 hwnd, rect.right - rect.left, rect.bottom - rect.top,
1474 winpos.cx, winpos.cy );
1476 if (winpos.hwndInsertAfter == oldInsertAfter) winpos.flags |= SWP_NOZORDER;
1478 TRACE( "%p restacking from after %p to after %p\n",
1479 hwnd, oldInsertAfter, winpos.hwndInsertAfter );
1481 /* if nothing changed, don't do anything */
1482 if (winpos.flags == (SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE)) return;
1484 SetWindowPos( hwnd, winpos.hwndInsertAfter, winpos.x, winpos.y,
1485 winpos.cx, winpos.cy, winpos.flags | SWP_WINE_NOHOSTMOVE );
1489 /***********************************************************************
1490 * SetWindowRgn (X11DRV.@)
1492 * Assign specified region to window (for non-rectangular windows)
1494 int X11DRV_SetWindowRgn( HWND hwnd, HRGN hrgn, BOOL redraw )
1498 if ((wndPtr = WIN_GetPtr( hwnd )) == WND_OTHER_PROCESS)
1500 if (IsWindow( hwnd ))
1501 FIXME( "not supported on other process window %p\n", hwnd );
1506 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
1510 #ifdef HAVE_LIBXSHAPE
1512 Display *display = thread_display();
1513 X11DRV_WND_DATA *data = wndPtr->pDriverData;
1515 if (data->whole_window)
1520 XShapeCombineMask( display, data->whole_window,
1521 ShapeBounding, 0, 0, None, ShapeSet );
1522 wine_tsx11_unlock();
1526 RGNDATA *pRegionData = X11DRV_GetRegionData( hrgn, 0 );
1530 XShapeCombineRectangles( display, data->whole_window, ShapeBounding,
1531 wndPtr->rectWindow.left - data->whole_rect.left,
1532 wndPtr->rectWindow.top - data->whole_rect.top,
1533 (XRectangle *)pRegionData->Buffer,
1534 pRegionData->rdh.nCount,
1535 ShapeSet, YXBanded );
1536 wine_tsx11_unlock();
1537 HeapFree(GetProcessHeap(), 0, pRegionData);
1542 #endif /* HAVE_LIBXSHAPE */
1544 WIN_ReleasePtr( wndPtr );
1549 /***********************************************************************
1552 * Draw the frame used when moving or resizing window.
1554 * FIXME: This causes problems in Win95 mode. (why?)
1556 static void draw_moving_frame( HDC hdc, RECT *rect, BOOL thickframe )
1560 const int width = GetSystemMetrics(SM_CXFRAME);
1561 const int height = GetSystemMetrics(SM_CYFRAME);
1563 HBRUSH hbrush = SelectObject( hdc, GetStockObject( GRAY_BRUSH ) );
1564 PatBlt( hdc, rect->left, rect->top,
1565 rect->right - rect->left - width, height, PATINVERT );
1566 PatBlt( hdc, rect->left, rect->top + height, width,
1567 rect->bottom - rect->top - height, PATINVERT );
1568 PatBlt( hdc, rect->left + width, rect->bottom - 1,
1569 rect->right - rect->left - width, -height, PATINVERT );
1570 PatBlt( hdc, rect->right - 1, rect->top, -width,
1571 rect->bottom - rect->top - height, PATINVERT );
1572 SelectObject( hdc, hbrush );
1574 else DrawFocusRect( hdc, rect );
1578 /***********************************************************************
1581 * Initialisation of a move or resize, when initiatied from a menu choice.
1582 * Return hit test code for caption or sizing border.
1584 static LONG start_size_move( HWND hwnd, WPARAM wParam, POINT *capturePoint, LONG style )
1591 GetWindowRect( hwnd, &rectWindow );
1593 if ((wParam & 0xfff0) == SC_MOVE)
1595 /* Move pointer at the center of the caption */
1597 NC_GetInsideRect( hwnd, &rect );
1598 if (style & WS_SYSMENU)
1599 rect.left += GetSystemMetrics(SM_CXSIZE) + 1;
1600 if (style & WS_MINIMIZEBOX)
1601 rect.right -= GetSystemMetrics(SM_CXSIZE) + 1;
1602 if (style & WS_MAXIMIZEBOX)
1603 rect.right -= GetSystemMetrics(SM_CXSIZE) + 1;
1604 pt.x = rectWindow.left + (rect.right - rect.left) / 2;
1605 pt.y = rectWindow.top + rect.top + GetSystemMetrics(SM_CYSIZE)/2;
1606 hittest = HTCAPTION;
1613 GetMessageW( &msg, 0, WM_KEYFIRST, WM_MOUSELAST );
1614 if (CallMsgFilterW( &msg, MSGF_SIZE )) continue;
1619 hittest = NC_HandleNCHitTest( hwnd, msg.pt );
1620 if ((hittest < HTLEFT) || (hittest > HTBOTTOMRIGHT))
1632 pt.x =(rectWindow.left+rectWindow.right)/2;
1633 pt.y = rectWindow.top + GetSystemMetrics(SM_CYFRAME) / 2;
1637 pt.x =(rectWindow.left+rectWindow.right)/2;
1638 pt.y = rectWindow.bottom - GetSystemMetrics(SM_CYFRAME) / 2;
1642 pt.x = rectWindow.left + GetSystemMetrics(SM_CXFRAME) / 2;
1643 pt.y =(rectWindow.top+rectWindow.bottom)/2;
1647 pt.x = rectWindow.right - GetSystemMetrics(SM_CXFRAME) / 2;
1648 pt.y =(rectWindow.top+rectWindow.bottom)/2;
1651 case VK_ESCAPE: return 0;
1657 SetCursorPos( pt.x, pt.y );
1658 NC_HandleSetCursor( hwnd, (WPARAM)hwnd, MAKELONG( hittest, WM_MOUSEMOVE ));
1663 /***********************************************************************
1664 * set_movesize_capture
1666 static void set_movesize_capture( HWND hwnd )
1670 SERVER_START_REQ( set_capture_window )
1673 req->flags = CAPTURE_MOVESIZE;
1674 if (!wine_server_call_err( req ))
1676 previous = reply->previous;
1677 hwnd = reply->full_handle;
1681 if (previous && previous != hwnd)
1682 SendMessageW( previous, WM_CAPTURECHANGED, 0, (LPARAM)hwnd );
1685 /***********************************************************************
1686 * X11DRV_WMMoveResizeWindow
1688 * Tells the window manager to initiate a move or resize operation.
1691 * http://freedesktop.org/Standards/wm-spec/1.3/ar01s04.html
1692 * or search for "_NET_WM_MOVERESIZE"
1694 static void X11DRV_WMMoveResizeWindow( HWND hwnd, int x, int y, int dir )
1697 Display *display = thread_display();
1701 /* need to ungrab the pointer that may have been automatically grabbed
1702 * with a ButtonPress event */
1703 XUngrabPointer( display, CurrentTime );
1705 xev.xclient.type = ClientMessage;
1706 xev.xclient.window = X11DRV_get_whole_window(hwnd);
1707 xev.xclient.message_type = x11drv_atom(_NET_WM_MOVERESIZE);
1708 xev.xclient.serial = 0;
1709 xev.xclient.display = display;
1710 xev.xclient.send_event = True;
1711 xev.xclient.format = 32;
1712 xev.xclient.data.l[0] = x; /* x coord */
1713 xev.xclient.data.l[1] = y; /* y coord */
1714 xev.xclient.data.l[2] = dir; /* direction */
1715 xev.xclient.data.l[3] = 1; /* button */
1716 xev.xclient.data.l[4] = 0; /* unused */
1717 XSendEvent(display, root_window, False, SubstructureNotifyMask, &xev);
1719 wine_tsx11_unlock();
1722 /***********************************************************************
1723 * SysCommandSizeMove (X11DRV.@)
1725 * Perform SC_MOVE and SC_SIZE commands.
1727 void X11DRV_SysCommandSizeMove( HWND hwnd, WPARAM wParam )
1730 RECT sizingRect, mouseRect, origRect;
1733 LONG hittest = (LONG)(wParam & 0x0f);
1734 WPARAM syscommand = wParam & 0xfff0;
1735 HCURSOR hDragCursor = 0, hOldCursor = 0;
1736 POINT minTrack, maxTrack;
1737 POINT capturePoint, pt;
1738 LONG style = GetWindowLongA( hwnd, GWL_STYLE );
1739 LONG exstyle = GetWindowLongA( hwnd, GWL_EXSTYLE );
1740 BOOL thickframe = HAS_THICKFRAME( style, exstyle );
1741 BOOL iconic = style & WS_MINIMIZE;
1743 DWORD dwPoint = GetMessagePos ();
1744 BOOL DragFullWindows = FALSE;
1747 Display *old_gdi_display = NULL;
1748 Display *display = thread_display();
1750 pt.x = (short)LOWORD(dwPoint);
1751 pt.y = (short)HIWORD(dwPoint);
1754 if (IsZoomed(hwnd) || !IsWindowVisible(hwnd)) return;
1756 /* if we are managed then we let the WM do all the work */
1757 if (exstyle & WS_EX_MANAGED)
1760 if (syscommand == SC_MOVE)
1762 if (!hittest) dir = _NET_WM_MOVERESIZE_MOVE_KEYBOARD;
1763 else dir = _NET_WM_MOVERESIZE_MOVE;
1765 else if (!hittest) dir = _NET_WM_MOVERESIZE_SIZE_KEYBOARD;
1769 case WMSZ_LEFT: dir = _NET_WM_MOVERESIZE_SIZE_LEFT; break;
1770 case WMSZ_RIGHT: dir = _NET_WM_MOVERESIZE_SIZE_RIGHT; break;
1771 case WMSZ_TOP: dir = _NET_WM_MOVERESIZE_SIZE_TOP; break;
1772 case WMSZ_TOPLEFT: dir = _NET_WM_MOVERESIZE_SIZE_TOPLEFT; break;
1773 case WMSZ_TOPRIGHT: dir = _NET_WM_MOVERESIZE_SIZE_TOPRIGHT; break;
1774 case WMSZ_BOTTOM: dir = _NET_WM_MOVERESIZE_SIZE_BOTTOM; break;
1775 case WMSZ_BOTTOMLEFT: dir = _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT; break;
1776 case WMSZ_BOTTOMRIGHT: dir = _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT; break;
1778 ERR("Invalid hittest value: %ld\n", hittest);
1779 dir = _NET_WM_MOVERESIZE_SIZE_KEYBOARD;
1781 X11DRV_WMMoveResizeWindow( hwnd, pt.x, pt.y, dir );
1785 SystemParametersInfoA(SPI_GETDRAGFULLWINDOWS, 0, &DragFullWindows, 0);
1787 if (syscommand == SC_MOVE)
1789 if (!hittest) hittest = start_size_move( hwnd, wParam, &capturePoint, style );
1790 if (!hittest) return;
1794 if ( hittest && (syscommand != SC_MOUSEMENU) )
1795 hittest += (HTLEFT - WMSZ_LEFT);
1798 set_movesize_capture( hwnd );
1799 hittest = start_size_move( hwnd, wParam, &capturePoint, style );
1802 set_movesize_capture(0);
1808 /* Get min/max info */
1810 WINPOS_GetMinMaxInfo( hwnd, NULL, NULL, &minTrack, &maxTrack );
1811 GetWindowRect( hwnd, &sizingRect );
1812 if (style & WS_CHILD)
1814 parent = GetParent(hwnd);
1815 /* make sizing rect relative to parent */
1816 MapWindowPoints( 0, parent, (POINT*)&sizingRect, 2 );
1817 GetClientRect( parent, &mouseRect );
1822 SetRect(&mouseRect, 0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN));
1824 origRect = sizingRect;
1826 if (ON_LEFT_BORDER(hittest))
1828 mouseRect.left = max( mouseRect.left, sizingRect.right-maxTrack.x );
1829 mouseRect.right = min( mouseRect.right, sizingRect.right-minTrack.x );
1831 else if (ON_RIGHT_BORDER(hittest))
1833 mouseRect.left = max( mouseRect.left, sizingRect.left+minTrack.x );
1834 mouseRect.right = min( mouseRect.right, sizingRect.left+maxTrack.x );
1836 if (ON_TOP_BORDER(hittest))
1838 mouseRect.top = max( mouseRect.top, sizingRect.bottom-maxTrack.y );
1839 mouseRect.bottom = min( mouseRect.bottom,sizingRect.bottom-minTrack.y);
1841 else if (ON_BOTTOM_BORDER(hittest))
1843 mouseRect.top = max( mouseRect.top, sizingRect.top+minTrack.y );
1844 mouseRect.bottom = min( mouseRect.bottom, sizingRect.top+maxTrack.y );
1846 if (parent) MapWindowPoints( parent, 0, (LPPOINT)&mouseRect, 2 );
1848 /* Retrieve a default cache DC (without using the window style) */
1849 hdc = GetDCEx( parent, 0, DCX_CACHE );
1851 if( iconic ) /* create a cursor for dragging */
1853 hDragCursor = (HCURSOR)GetClassLongA( hwnd, GCL_HICON);
1854 if( !hDragCursor ) hDragCursor = (HCURSOR)SendMessageA( hwnd, WM_QUERYDRAGICON, 0, 0L);
1855 if( !hDragCursor ) iconic = FALSE;
1858 /* repaint the window before moving it around */
1859 RedrawWindow( hwnd, NULL, 0, RDW_UPDATENOW | RDW_ALLCHILDREN );
1861 SendMessageA( hwnd, WM_ENTERSIZEMOVE, 0, 0 );
1862 set_movesize_capture( hwnd );
1864 /* grab the server only when moving top-level windows without desktop */
1865 grab = (!DragFullWindows && !parent && (root_window == DefaultRootWindow(gdi_display)));
1870 XSync( gdi_display, False );
1871 XGrabServer( display );
1872 XSync( display, False );
1873 /* switch gdi display to the thread display, since the server is grabbed */
1874 old_gdi_display = gdi_display;
1875 gdi_display = display;
1877 XGrabPointer( display, X11DRV_get_whole_window(hwnd), False,
1878 PointerMotionMask | ButtonPressMask | ButtonReleaseMask,
1879 GrabModeAsync, GrabModeAsync,
1880 parent ? X11DRV_get_client_window(parent) : root_window,
1881 None, CurrentTime );
1882 wine_tsx11_unlock();
1888 if (!GetMessageW( &msg, 0, WM_KEYFIRST, WM_MOUSELAST )) break;
1889 if (CallMsgFilterW( &msg, MSGF_SIZE )) continue;
1891 /* Exit on button-up, Return, or Esc */
1892 if ((msg.message == WM_LBUTTONUP) ||
1893 ((msg.message == WM_KEYDOWN) &&
1894 ((msg.wParam == VK_RETURN) || (msg.wParam == VK_ESCAPE)))) break;
1896 if ((msg.message != WM_KEYDOWN) && (msg.message != WM_MOUSEMOVE))
1897 continue; /* We are not interested in other messages */
1901 if (msg.message == WM_KEYDOWN) switch(msg.wParam)
1903 case VK_UP: pt.y -= 8; break;
1904 case VK_DOWN: pt.y += 8; break;
1905 case VK_LEFT: pt.x -= 8; break;
1906 case VK_RIGHT: pt.x += 8; break;
1909 pt.x = max( pt.x, mouseRect.left );
1910 pt.x = min( pt.x, mouseRect.right );
1911 pt.y = max( pt.y, mouseRect.top );
1912 pt.y = min( pt.y, mouseRect.bottom );
1914 dx = pt.x - capturePoint.x;
1915 dy = pt.y - capturePoint.y;
1923 if( iconic ) /* ok, no system popup tracking */
1925 hOldCursor = SetCursor(hDragCursor);
1927 WINPOS_ShowIconTitle( hwnd, FALSE );
1929 else if(!DragFullWindows)
1930 draw_moving_frame( hdc, &sizingRect, thickframe );
1933 if (msg.message == WM_KEYDOWN) SetCursorPos( pt.x, pt.y );
1936 RECT newRect = sizingRect;
1937 WPARAM wpSizingHit = 0;
1939 if (hittest == HTCAPTION) OffsetRect( &newRect, dx, dy );
1940 if (ON_LEFT_BORDER(hittest)) newRect.left += dx;
1941 else if (ON_RIGHT_BORDER(hittest)) newRect.right += dx;
1942 if (ON_TOP_BORDER(hittest)) newRect.top += dy;
1943 else if (ON_BOTTOM_BORDER(hittest)) newRect.bottom += dy;
1944 if(!iconic && !DragFullWindows) draw_moving_frame( hdc, &sizingRect, thickframe );
1947 /* determine the hit location */
1948 if (hittest >= HTLEFT && hittest <= HTBOTTOMRIGHT)
1949 wpSizingHit = WMSZ_LEFT + (hittest - HTLEFT);
1950 SendMessageA( hwnd, WM_SIZING, wpSizingHit, (LPARAM)&newRect );
1954 if(!DragFullWindows)
1955 draw_moving_frame( hdc, &newRect, thickframe );
1957 /* To avoid any deadlocks, all the locks on the windows
1958 structures must be suspended before the SetWindowPos */
1959 iWndsLocks = WIN_SuspendWndsLock();
1960 SetWindowPos( hwnd, 0, newRect.left, newRect.top,
1961 newRect.right - newRect.left,
1962 newRect.bottom - newRect.top,
1963 ( hittest == HTCAPTION ) ? SWP_NOSIZE : 0 );
1964 WIN_RestoreWndsLock(iWndsLocks);
1967 sizingRect = newRect;
1972 set_movesize_capture(0);
1975 if( moved ) /* restore cursors, show icon title later on */
1977 ShowCursor( FALSE );
1978 SetCursor( hOldCursor );
1981 else if (moved && !DragFullWindows)
1982 draw_moving_frame( hdc, &sizingRect, thickframe );
1984 ReleaseDC( parent, hdc );
1987 XUngrabPointer( display, CurrentTime );
1990 XSync( display, False );
1991 XUngrabServer( display );
1992 XSync( display, False );
1993 gdi_display = old_gdi_display;
1995 wine_tsx11_unlock();
1997 if (HOOK_CallHooks( WH_CBT, HCBT_MOVESIZE, (WPARAM)hwnd, (LPARAM)&sizingRect, TRUE ))
2000 SendMessageA( hwnd, WM_EXITSIZEMOVE, 0, 0 );
2001 SendMessageA( hwnd, WM_SETVISIBLE, !IsIconic(hwnd), 0L);
2003 /* window moved or resized */
2006 /* To avoid any deadlocks, all the locks on the windows
2007 structures must be suspended before the SetWindowPos */
2008 iWndsLocks = WIN_SuspendWndsLock();
2010 /* if the moving/resizing isn't canceled call SetWindowPos
2011 * with the new position or the new size of the window
2013 if (!((msg.message == WM_KEYDOWN) && (msg.wParam == VK_ESCAPE)) )
2015 /* NOTE: SWP_NOACTIVATE prevents document window activation in Word 6 */
2016 if(!DragFullWindows)
2017 SetWindowPos( hwnd, 0, sizingRect.left, sizingRect.top,
2018 sizingRect.right - sizingRect.left,
2019 sizingRect.bottom - sizingRect.top,
2020 ( hittest == HTCAPTION ) ? SWP_NOSIZE : 0 );
2023 { /* restore previous size/position */
2025 SetWindowPos( hwnd, 0, origRect.left, origRect.top,
2026 origRect.right - origRect.left,
2027 origRect.bottom - origRect.top,
2028 ( hittest == HTCAPTION ) ? SWP_NOSIZE : 0 );
2031 WIN_RestoreWndsLock(iWndsLocks);
2036 /* Single click brings up the system menu when iconized */
2040 if(style & WS_SYSMENU )
2041 SendMessageA( hwnd, WM_SYSCOMMAND,
2042 SC_MOUSEMENU + HTSYSMENU, MAKELONG(pt.x,pt.y));
2044 else WINPOS_ShowIconTitle( hwnd, TRUE );