Fix small mistake in the previous visible region patch.
[wine/testsucceed.git] / dlls / x11drv / winpos.c
blob8c1120ca773b0ff7310bc1f11102cc7b43c52c67
1 /*
2 * Window position related functions.
4 * Copyright 1993, 1994, 1995, 2001 Alexandre Julliard
5 * Copyright 1995, 1996, 1999 Alex Korobka
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "config.h"
24 #include <X11/Xlib.h>
25 #ifdef HAVE_LIBXSHAPE
26 #include <X11/IntrinsicP.h>
27 #include <X11/extensions/shape.h>
28 #endif /* HAVE_LIBXSHAPE */
29 #include <stdarg.h>
31 #include "windef.h"
32 #include "winbase.h"
33 #include "wingdi.h"
34 #include "winuser.h"
35 #include "winerror.h"
36 #include "wownt32.h"
37 #include "wine/wingdi16.h"
39 #include "x11drv.h"
40 #include "win.h"
41 #include "winpos.h"
42 #include "dce.h"
43 #include "cursoricon.h"
44 #include "nonclient.h"
46 #include "wine/server.h"
47 #include "wine/debug.h"
49 WINE_DEFAULT_DEBUG_CHANNEL(x11drv);
51 #define SWP_AGG_NOGEOMETRYCHANGE \
52 (SWP_NOSIZE | SWP_NOMOVE | SWP_NOCLIENTSIZE | SWP_NOCLIENTMOVE)
53 #define SWP_AGG_NOPOSCHANGE \
54 (SWP_AGG_NOGEOMETRYCHANGE | SWP_NOZORDER)
55 #define SWP_AGG_STATUSFLAGS \
56 (SWP_AGG_NOPOSCHANGE | SWP_FRAMECHANGED | SWP_HIDEWINDOW | SWP_SHOWWINDOW)
58 #define SWP_EX_NOCOPY 0x0001
59 #define SWP_EX_PAINTSELF 0x0002
60 #define SWP_EX_NONCLIENT 0x0004
62 #define HAS_THICKFRAME(style,exStyle) \
63 (((style) & WS_THICKFRAME) && \
64 !(((style) & (WS_DLGFRAME|WS_BORDER)) == WS_DLGFRAME))
66 #define ON_LEFT_BORDER(hit) \
67 (((hit) == HTLEFT) || ((hit) == HTTOPLEFT) || ((hit) == HTBOTTOMLEFT))
68 #define ON_RIGHT_BORDER(hit) \
69 (((hit) == HTRIGHT) || ((hit) == HTTOPRIGHT) || ((hit) == HTBOTTOMRIGHT))
70 #define ON_TOP_BORDER(hit) \
71 (((hit) == HTTOP) || ((hit) == HTTOPLEFT) || ((hit) == HTTOPRIGHT))
72 #define ON_BOTTOM_BORDER(hit) \
73 (((hit) == HTBOTTOM) || ((hit) == HTBOTTOMLEFT) || ((hit) == HTBOTTOMRIGHT))
76 /***********************************************************************
77 * clip_children
79 * Clip all children of a given window out of the visible region
81 static int clip_children( HWND parent, HWND last, HRGN hrgn, int whole_window )
83 HWND *list;
84 WND *ptr;
85 HRGN rectRgn;
86 int i, x, y, ret = SIMPLEREGION;
88 /* first check if we have anything to do */
89 if (!(list = WIN_ListChildren( parent ))) return ret;
91 if (whole_window)
93 WND *win = WIN_FindWndPtr( parent );
94 x = win->rectWindow.left - win->rectClient.left;
95 y = win->rectWindow.top - win->rectClient.top;
96 WIN_ReleaseWndPtr( win );
98 else x = y = 0;
100 rectRgn = CreateRectRgn( 0, 0, 0, 0 );
102 for (i = 0; list[i] && list[i] != last; i++)
104 if (!(ptr = WIN_FindWndPtr( list[i] ))) continue;
105 if ((ptr->dwStyle & WS_VISIBLE) && !(ptr->dwExStyle & WS_EX_TRANSPARENT))
107 SetRectRgn( rectRgn, ptr->rectWindow.left + x, ptr->rectWindow.top + y,
108 ptr->rectWindow.right + x, ptr->rectWindow.bottom + y );
109 if ((ret = CombineRgn( hrgn, hrgn, rectRgn, RGN_DIFF )) == NULLREGION)
111 WIN_ReleaseWndPtr( ptr );
112 break; /* no need to go on, region is empty */
115 WIN_ReleaseWndPtr( ptr );
117 DeleteObject( rectRgn );
118 HeapFree( GetProcessHeap(), 0, list );
119 return ret;
123 /***********************************************************************
124 * get_server_visible_region
126 static HRGN get_server_visible_region( HWND hwnd, HWND top, UINT flags )
128 RGNDATA *data;
129 HRGN ret = 0;
130 size_t size = 256;
131 BOOL retry = FALSE;
135 if (!(data = HeapAlloc( GetProcessHeap(), 0, sizeof(*data) + size - 1 ))) return 0;
136 SERVER_START_REQ( get_visible_region )
138 req->window = hwnd;
139 req->top_win = top;
140 req->flags = flags;
141 wine_server_set_reply( req, data->Buffer, size );
142 if (!wine_server_call_err( req ))
144 if (reply->total_size <= size)
146 size_t reply_size = wine_server_reply_size( reply );
147 data->rdh.dwSize = sizeof(data->rdh);
148 data->rdh.iType = RDH_RECTANGLES;
149 data->rdh.nCount = reply_size / sizeof(RECT);
150 data->rdh.nRgnSize = reply_size;
151 ret = ExtCreateRegion( NULL, size, data );
152 retry = FALSE;
154 else
156 size = reply->total_size;
157 retry = TRUE;
161 SERVER_END_REQ;
162 HeapFree( GetProcessHeap(), 0, data );
163 } while (retry);
164 return ret;
168 /***********************************************************************
169 * get_covered_region
171 * Compute the portion of 'rgn' that is covered by non-clipped siblings.
172 * This is the area that is covered from X point of view, but may still need
173 * to be exposed.
174 * 'rgn' must be relative to the client area of the parent of 'win'.
176 static int get_covered_region( WND *win, HRGN rgn )
178 HRGN tmp;
179 int ret;
180 WND *parent, *ptr = WIN_FindWndPtr( win->hwndSelf );
181 int xoffset = 0, yoffset = 0;
183 tmp = CreateRectRgn( 0, 0, 0, 0 );
184 CombineRgn( tmp, rgn, 0, RGN_COPY );
186 /* to make things easier we actually build the uncovered
187 * area by removing all siblings and then we subtract that
188 * from the total region to get the covered area */
189 for (;;)
191 if (!(ptr->dwStyle & WS_CLIPSIBLINGS))
193 if (clip_children( ptr->parent, ptr->hwndSelf, tmp, FALSE ) == NULLREGION) break;
195 if (!(parent = WIN_FindWndPtr( ptr->parent ))) break;
196 WIN_ReleaseWndPtr( ptr );
197 ptr = parent;
198 OffsetRgn( tmp, ptr->rectClient.left, ptr->rectClient.top );
199 xoffset += ptr->rectClient.left;
200 yoffset += ptr->rectClient.top;
202 WIN_ReleaseWndPtr( ptr );
203 /* make it relative to the target window again */
204 OffsetRgn( tmp, -xoffset, -yoffset );
206 /* now subtract the computed region from the original one */
207 ret = CombineRgn( rgn, rgn, tmp, RGN_DIFF );
208 DeleteObject( tmp );
209 return ret;
213 /***********************************************************************
214 * expose_window
216 * Expose a region of a given window.
218 static void expose_window( HWND hwnd, RECT *rect, HRGN rgn, int flags )
220 POINT offset;
221 HWND top = 0;
222 HWND *list;
223 int i;
225 /* find the top most parent that doesn't clip children or siblings and
226 * invalidate the area on its parent, including all children */
227 if ((list = WIN_ListParents( hwnd )))
229 HWND current = hwnd;
230 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
231 for (i = 0; list[i] && list[i] != GetDesktopWindow(); i++)
233 if (!(style & WS_CLIPSIBLINGS)) top = current;
234 style = GetWindowLongW( list[i], GWL_STYLE );
235 if (!(style & WS_CLIPCHILDREN)) top = current;
236 current = list[i];
239 if (top)
241 /* find the parent of the top window, reusing the parent list */
242 if (top == hwnd) i = 0;
243 else
245 for (i = 0; list[i]; i++) if (list[i] == top) break;
246 if (list[i] && list[i+1]) i++;
248 if (list[i] != GetDesktopWindow()) top = list[i];
249 flags &= ~RDW_FRAME; /* parent will invalidate children frame anyway */
250 flags |= RDW_ALLCHILDREN;
252 HeapFree( GetProcessHeap(), 0, list );
255 if (!top) top = hwnd;
257 /* make coords relative to top */
258 offset.x = offset.y = 0;
259 MapWindowPoints( hwnd, top, &offset, 1 );
261 if (rect)
263 OffsetRect( rect, offset.x, offset.y );
264 RedrawWindow( top, rect, 0, flags );
266 else
268 OffsetRgn( rgn, offset.x, offset.y );
269 RedrawWindow( top, NULL, rgn, flags );
274 /***********************************************************************
275 * expose_covered_parent_area
277 * Expose the parent area that has been uncovered by moving/hiding a
278 * given window, but that is still covered by other siblings (the area
279 * not covered by siblings will be exposed automatically by X).
281 static void expose_covered_parent_area( WND *win, const RECT *old_rect )
283 int ret = SIMPLEREGION;
284 HRGN hrgn = CreateRectRgnIndirect( old_rect );
286 if (win->dwStyle & WS_VISIBLE)
288 HRGN tmp = CreateRectRgnIndirect( &win->rectWindow );
289 ret = CombineRgn( hrgn, hrgn, tmp, RGN_DIFF );
290 DeleteObject( tmp );
293 if (ret != NULLREGION)
295 if (get_covered_region( win, hrgn ) != NULLREGION)
296 expose_window( win->parent, NULL, hrgn, RDW_INVALIDATE | RDW_ERASE | RDW_ALLCHILDREN );
298 DeleteObject( hrgn );
302 /***********************************************************************
303 * expose_covered_window_area
305 * Expose the area of a window that is covered by other siblings.
307 static void expose_covered_window_area( WND *win, const RECT *old_client_rect, BOOL frame )
309 HRGN hrgn;
310 int ret = SIMPLEREGION;
312 if (frame)
313 hrgn = CreateRectRgn( win->rectWindow.left - win->rectClient.left,
314 win->rectWindow.top - win->rectClient.top,
315 win->rectWindow.right - win->rectWindow.left,
316 win->rectWindow.bottom - win->rectWindow.top );
317 else
318 hrgn = CreateRectRgn( 0, 0,
319 win->rectClient.right - win->rectClient.left,
320 win->rectClient.bottom - win->rectClient.top );
322 /* if the client rect didn't move we don't need to repaint it all */
323 if (old_client_rect->left == win->rectClient.left &&
324 old_client_rect->top == win->rectClient.top)
326 RECT rc;
328 if (IntersectRect( &rc, old_client_rect, &win->rectClient ))
330 HRGN tmp;
331 /* subtract the unchanged client area from the region to expose */
332 OffsetRect( &rc, -win->rectClient.left, -win->rectClient.top );
333 if ((tmp = CreateRectRgnIndirect( &rc )))
335 ret = CombineRgn( hrgn, hrgn, tmp, RGN_DIFF );
336 DeleteObject( tmp );
341 if (ret != NULLREGION)
343 if (get_covered_region( win, hrgn ) != NULLREGION)
344 expose_window( win->hwndSelf, NULL, hrgn,
345 RDW_INVALIDATE | RDW_ERASE | RDW_FRAME | RDW_ALLCHILDREN );
348 DeleteObject( hrgn );
352 /***********************************************************************
353 * X11DRV_Expose
355 void X11DRV_Expose( HWND hwnd, XExposeEvent *event )
357 RECT rect;
358 struct x11drv_win_data *data;
359 int flags = RDW_INVALIDATE | RDW_ERASE;
360 WND *win;
362 TRACE( "win %p (%lx) %d,%d %dx%d\n",
363 hwnd, event->window, event->x, event->y, event->width, event->height );
365 rect.left = event->x;
366 rect.top = event->y;
367 rect.right = rect.left + event->width;
368 rect.bottom = rect.top + event->height;
370 if (!(win = WIN_GetPtr( hwnd ))) return;
371 data = win->pDriverData;
373 if (event->window != data->client_window) /* whole window or icon window */
375 flags |= RDW_FRAME;
376 /* make position relative to client area instead of window */
377 OffsetRect( &rect, -data->client_rect.left, -data->client_rect.top );
379 WIN_ReleasePtr( win );
381 expose_window( hwnd, &rect, 0, flags );
385 /***********************************************************************
386 * GetDC (X11DRV.@)
388 * Set the drawable, origin and dimensions for the DC associated to
389 * a given window.
391 BOOL X11DRV_GetDC( HWND hwnd, HDC hdc, HRGN hrgn, DWORD flags )
393 WND *win = WIN_GetPtr( hwnd );
394 HWND top = 0;
395 X11DRV_WND_DATA *data = win->pDriverData;
396 struct x11drv_escape_set_drawable escape;
398 escape.mode = IncludeInferiors;
399 /* don't clip siblings if using parent clip region */
400 if (flags & DCX_PARENTCLIP) flags &= ~DCX_CLIPSIBLINGS;
402 top = GetAncestor( hwnd, GA_ROOT );
403 if (top != hwnd)
405 escape.org.x = escape.org.y = 0;
406 if (flags & DCX_WINDOW)
408 escape.org.x = win->rectWindow.left - win->rectClient.left;
409 escape.org.y = win->rectWindow.top - win->rectClient.top;
411 MapWindowPoints( hwnd, top, &escape.org, 1 );
412 escape.drawable_org.x = escape.drawable_org.y = 0;
413 MapWindowPoints( top, 0, &escape.drawable_org, 1 );
414 escape.drawable = X11DRV_get_client_window( top );
416 else
418 if (IsIconic( hwnd ))
420 escape.drawable = data->icon_window ? data->icon_window : data->whole_window;
421 escape.org.x = 0;
422 escape.org.y = 0;
423 escape.drawable_org = escape.org;
425 else
427 escape.drawable = data->whole_window;
428 escape.drawable_org.x = data->whole_rect.left;
429 escape.drawable_org.y = data->whole_rect.top;
430 if (flags & DCX_WINDOW)
432 escape.org.x = win->rectWindow.left - data->whole_rect.left;
433 escape.org.y = win->rectWindow.top - data->whole_rect.top;
435 else
437 escape.org.x = win->rectClient.left - data->whole_rect.left;
438 escape.org.y = win->rectClient.top - data->whole_rect.top;
443 escape.code = X11DRV_SET_DRAWABLE;
444 ExtEscape( hdc, X11DRV_ESCAPE, sizeof(escape), (LPSTR)&escape, 0, NULL );
446 if (flags & (DCX_EXCLUDERGN | DCX_INTERSECTRGN) ||
447 SetHookFlags16( HDC_16(hdc), DCHF_VALIDATEVISRGN )) /* DC was dirty */
449 /* need to recompute the visible region */
450 HRGN visRgn = get_server_visible_region( hwnd, top, flags );
452 if (flags & (DCX_EXCLUDERGN | DCX_INTERSECTRGN))
453 CombineRgn( visRgn, visRgn, hrgn, (flags & DCX_INTERSECTRGN) ? RGN_AND : RGN_DIFF );
455 SelectVisRgn16( HDC_16(hdc), HRGN_16(visRgn) );
456 DeleteObject( visRgn );
459 WIN_ReleasePtr( win );
460 return TRUE;
464 /***********************************************************************
465 * ReleaseDC (X11DRV.@)
467 void X11DRV_ReleaseDC( HWND hwnd, HDC hdc )
469 struct x11drv_escape_set_drawable escape;
471 escape.code = X11DRV_SET_DRAWABLE;
472 escape.drawable = root_window;
473 escape.mode = IncludeInferiors;
474 escape.org.x = escape.org.y = 0;
475 escape.drawable_org.x = escape.drawable_org.y = 0;
477 ExtEscape( hdc, X11DRV_ESCAPE, sizeof(escape), (LPSTR)&escape, 0, NULL );
481 /***********************************************************************
482 * SWP_DoWinPosChanging
484 static BOOL SWP_DoWinPosChanging( WINDOWPOS* pWinpos, RECT* pNewWindowRect, RECT* pNewClientRect )
486 WND *wndPtr;
488 /* Send WM_WINDOWPOSCHANGING message */
490 if (!(pWinpos->flags & SWP_NOSENDCHANGING))
491 SendMessageW( pWinpos->hwnd, WM_WINDOWPOSCHANGING, 0, (LPARAM)pWinpos );
493 if (!(wndPtr = WIN_GetPtr( pWinpos->hwnd )) || wndPtr == WND_OTHER_PROCESS) return FALSE;
495 /* Calculate new position and size */
497 *pNewWindowRect = wndPtr->rectWindow;
498 *pNewClientRect = (wndPtr->dwStyle & WS_MINIMIZE) ? wndPtr->rectWindow
499 : wndPtr->rectClient;
501 if (!(pWinpos->flags & SWP_NOSIZE))
503 pNewWindowRect->right = pNewWindowRect->left + pWinpos->cx;
504 pNewWindowRect->bottom = pNewWindowRect->top + pWinpos->cy;
506 if (!(pWinpos->flags & SWP_NOMOVE))
508 pNewWindowRect->left = pWinpos->x;
509 pNewWindowRect->top = pWinpos->y;
510 pNewWindowRect->right += pWinpos->x - wndPtr->rectWindow.left;
511 pNewWindowRect->bottom += pWinpos->y - wndPtr->rectWindow.top;
513 OffsetRect( pNewClientRect, pWinpos->x - wndPtr->rectWindow.left,
514 pWinpos->y - wndPtr->rectWindow.top );
516 pWinpos->flags |= SWP_NOCLIENTMOVE | SWP_NOCLIENTSIZE;
517 WIN_ReleasePtr( wndPtr );
518 return TRUE;
521 /***********************************************************************
522 * SWP_DoNCCalcSize
524 static UINT SWP_DoNCCalcSize( WINDOWPOS* pWinpos, RECT* pNewWindowRect, RECT* pNewClientRect )
526 UINT wvrFlags = 0;
527 WND *wndPtr;
529 if (!(wndPtr = WIN_GetPtr( pWinpos->hwnd )) || wndPtr == WND_OTHER_PROCESS) return 0;
531 /* Send WM_NCCALCSIZE message to get new client area */
532 if( (pWinpos->flags & (SWP_FRAMECHANGED | SWP_NOSIZE)) != SWP_NOSIZE )
534 NCCALCSIZE_PARAMS params;
535 WINDOWPOS winposCopy;
537 params.rgrc[0] = *pNewWindowRect;
538 params.rgrc[1] = wndPtr->rectWindow;
539 params.rgrc[2] = wndPtr->rectClient;
540 params.lppos = &winposCopy;
541 winposCopy = *pWinpos;
542 WIN_ReleasePtr( wndPtr );
544 wvrFlags = SendMessageW( pWinpos->hwnd, WM_NCCALCSIZE, TRUE, (LPARAM)&params );
546 TRACE( "(%ld,%ld)-(%ld,%ld)\n", params.rgrc[0].left, params.rgrc[0].top,
547 params.rgrc[0].right, params.rgrc[0].bottom );
549 /* If the application send back garbage, ignore it */
550 if (params.rgrc[0].left <= params.rgrc[0].right &&
551 params.rgrc[0].top <= params.rgrc[0].bottom)
552 *pNewClientRect = params.rgrc[0];
554 /* FIXME: WVR_ALIGNxxx */
556 if (!(wndPtr = WIN_GetPtr( pWinpos->hwnd )) || wndPtr == WND_OTHER_PROCESS) return 0;
558 if( pNewClientRect->left != wndPtr->rectClient.left ||
559 pNewClientRect->top != wndPtr->rectClient.top )
560 pWinpos->flags &= ~SWP_NOCLIENTMOVE;
562 if( (pNewClientRect->right - pNewClientRect->left !=
563 wndPtr->rectClient.right - wndPtr->rectClient.left) ||
564 (pNewClientRect->bottom - pNewClientRect->top !=
565 wndPtr->rectClient.bottom - wndPtr->rectClient.top) )
566 pWinpos->flags &= ~SWP_NOCLIENTSIZE;
568 else
570 if (!(pWinpos->flags & SWP_NOMOVE) &&
571 (pNewClientRect->left != wndPtr->rectClient.left ||
572 pNewClientRect->top != wndPtr->rectClient.top))
573 pWinpos->flags &= ~SWP_NOCLIENTMOVE;
575 WIN_ReleasePtr( wndPtr );
576 return wvrFlags;
580 /***********************************************************************
581 * SWP_DoOwnedPopups
583 * fix Z order taking into account owned popups -
584 * basically we need to maintain them above the window that owns them
586 * FIXME: hide/show owned popups when owner visibility changes.
588 static HWND SWP_DoOwnedPopups(HWND hwnd, HWND hwndInsertAfter)
590 HWND *list = NULL;
591 HWND owner = GetWindow( hwnd, GW_OWNER );
592 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
594 WARN("(%p) hInsertAfter = %p\n", hwnd, hwndInsertAfter );
596 if ((style & WS_POPUP) && owner)
598 /* make sure this popup stays above the owner */
600 HWND hwndLocalPrev = HWND_TOP;
602 if( hwndInsertAfter != HWND_TOP )
604 if ((list = WIN_ListChildren( GetDesktopWindow() )))
606 int i;
607 for (i = 0; list[i]; i++)
609 if (list[i] == owner) break;
610 if (list[i] != hwnd) hwndLocalPrev = list[i];
611 if (hwndLocalPrev == hwndInsertAfter) break;
613 hwndInsertAfter = hwndLocalPrev;
617 else if (style & WS_CHILD) return hwndInsertAfter;
619 if (!list) list = WIN_ListChildren( GetDesktopWindow() );
620 if (list)
622 int i;
623 for (i = 0; list[i]; i++)
625 if (list[i] == hwnd) break;
626 if ((GetWindowLongW( list[i], GWL_STYLE ) & WS_POPUP) &&
627 GetWindow( list[i], GW_OWNER ) == hwnd)
629 SetWindowPos( list[i], hwndInsertAfter, 0, 0, 0, 0,
630 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE |
631 SWP_NOSENDCHANGING | SWP_DEFERERASE );
632 hwndInsertAfter = list[i];
635 HeapFree( GetProcessHeap(), 0, list );
638 return hwndInsertAfter;
642 /* fix redundant flags and values in the WINDOWPOS structure */
643 static BOOL fixup_flags( WINDOWPOS *winpos )
645 WND *wndPtr = WIN_GetPtr( winpos->hwnd );
646 BOOL ret = TRUE;
648 if (!wndPtr || wndPtr == WND_OTHER_PROCESS)
650 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
651 return FALSE;
653 winpos->hwnd = wndPtr->hwndSelf; /* make it a full handle */
655 /* Finally make sure that all coordinates are valid */
656 if (winpos->x < -32768) winpos->x = -32768;
657 else if (winpos->x > 32767) winpos->x = 32767;
658 if (winpos->y < -32768) winpos->y = -32768;
659 else if (winpos->y > 32767) winpos->y = 32767;
661 if (winpos->cx < 0) winpos->cx = 0;
662 else if (winpos->cx > 32767) winpos->cx = 32767;
663 if (winpos->cy < 0) winpos->cy = 0;
664 else if (winpos->cy > 32767) winpos->cy = 32767;
666 if (wndPtr->dwStyle & WS_VISIBLE) winpos->flags &= ~SWP_SHOWWINDOW;
667 else
669 winpos->flags &= ~SWP_HIDEWINDOW;
670 if (!(winpos->flags & SWP_SHOWWINDOW)) winpos->flags |= SWP_NOREDRAW;
673 if ((wndPtr->rectWindow.right - wndPtr->rectWindow.left == winpos->cx) &&
674 (wndPtr->rectWindow.bottom - wndPtr->rectWindow.top == winpos->cy))
675 winpos->flags |= SWP_NOSIZE; /* Already the right size */
677 if ((wndPtr->rectWindow.left == winpos->x) && (wndPtr->rectWindow.top == winpos->y))
678 winpos->flags |= SWP_NOMOVE; /* Already the right position */
680 if (winpos->hwnd == GetActiveWindow())
681 winpos->flags |= SWP_NOACTIVATE; /* Already active */
682 else if ((wndPtr->dwStyle & (WS_POPUP | WS_CHILD)) != WS_CHILD)
684 if (!(winpos->flags & SWP_NOACTIVATE)) /* Bring to the top when activating */
686 winpos->flags &= ~SWP_NOZORDER;
687 winpos->hwndInsertAfter = HWND_TOP;
691 /* Check hwndInsertAfter */
692 if (winpos->flags & SWP_NOZORDER) goto done;
694 /* fix sign extension */
695 if (winpos->hwndInsertAfter == (HWND)0xffff) winpos->hwndInsertAfter = HWND_TOPMOST;
696 else if (winpos->hwndInsertAfter == (HWND)0xfffe) winpos->hwndInsertAfter = HWND_NOTOPMOST;
698 /* FIXME: TOPMOST not supported yet */
699 if ((winpos->hwndInsertAfter == HWND_TOPMOST) ||
700 (winpos->hwndInsertAfter == HWND_NOTOPMOST)) winpos->hwndInsertAfter = HWND_TOP;
702 /* hwndInsertAfter must be a sibling of the window */
703 if (winpos->hwndInsertAfter == HWND_TOP)
705 if (GetWindow(winpos->hwnd, GW_HWNDFIRST) == winpos->hwnd)
706 winpos->flags |= SWP_NOZORDER;
708 else if (winpos->hwndInsertAfter == HWND_BOTTOM)
710 if (GetWindow(winpos->hwnd, GW_HWNDLAST) == winpos->hwnd)
711 winpos->flags |= SWP_NOZORDER;
713 else
715 if (GetAncestor( winpos->hwndInsertAfter, GA_PARENT ) != wndPtr->parent) ret = FALSE;
716 else
718 /* don't need to change the Zorder of hwnd if it's already inserted
719 * after hwndInsertAfter or when inserting hwnd after itself.
721 if ((winpos->hwnd == winpos->hwndInsertAfter) ||
722 (winpos->hwnd == GetWindow( winpos->hwndInsertAfter, GW_HWNDNEXT )))
723 winpos->flags |= SWP_NOZORDER;
726 done:
727 WIN_ReleasePtr( wndPtr );
728 return ret;
732 /***********************************************************************
733 * set_visible_style
735 * Set/clear the WS_VISIBLE style of a window and map/unmap the X window.
737 static void set_visible_style( HWND hwnd, BOOL set )
739 WND *win;
741 if (!(win = WIN_GetPtr( hwnd ))) return;
742 if (win == WND_OTHER_PROCESS) return;
744 TRACE( "hwnd %p (%lx) set %d visible %d empty %d\n",
745 hwnd, get_whole_window(win),
746 set, (win->dwStyle & WS_VISIBLE) != 0, IsRectEmpty(&win->rectWindow) );
748 if (set)
750 if (win->dwStyle & WS_VISIBLE) goto done;
751 WIN_SetStyle( hwnd, win->dwStyle | WS_VISIBLE );
752 if (X11DRV_is_window_rect_mapped( &win->rectWindow ) &&
753 get_whole_window(win) && is_window_top_level(win))
755 Display *display = thread_display();
756 X11DRV_sync_window_style( display, win );
757 X11DRV_set_wm_hints( display, win );
758 TRACE( "mapping win %p\n", hwnd );
759 wine_tsx11_lock();
760 XMapWindow( display, get_whole_window(win) );
761 wine_tsx11_unlock();
764 else
766 if (!(win->dwStyle & WS_VISIBLE)) goto done;
767 WIN_SetStyle( hwnd, win->dwStyle & ~WS_VISIBLE );
768 if (X11DRV_is_window_rect_mapped( &win->rectWindow ) &&
769 get_whole_window(win) && is_window_top_level(win))
771 TRACE( "unmapping win %p\n", hwnd );
772 wine_tsx11_lock();
773 XUnmapWindow( thread_display(), get_whole_window(win) );
774 wine_tsx11_unlock();
777 done:
778 WIN_ReleasePtr( win );
782 /***********************************************************************
783 * SetWindowStyle (X11DRV.@)
785 * Update the X state of a window to reflect a style change
787 void X11DRV_SetWindowStyle( HWND hwnd, LONG oldStyle )
789 Display *display = thread_display();
790 WND *wndPtr;
791 LONG changed;
793 if (hwnd == GetDesktopWindow()) return;
794 if (!(wndPtr = WIN_GetPtr( hwnd ))) return;
795 if (wndPtr == WND_OTHER_PROCESS) return;
797 changed = wndPtr->dwStyle ^ oldStyle;
799 if (changed & WS_VISIBLE)
801 if (X11DRV_is_window_rect_mapped( &wndPtr->rectWindow ))
803 if (wndPtr->dwStyle & WS_VISIBLE)
805 TRACE( "mapping win %p\n", hwnd );
806 if (is_window_top_level(wndPtr))
808 X11DRV_sync_window_style( display, wndPtr );
809 X11DRV_set_wm_hints( display, wndPtr );
811 wine_tsx11_lock();
812 XMapWindow( display, get_whole_window(wndPtr) );
813 wine_tsx11_unlock();
815 else if (!is_window_top_level(wndPtr)) /* don't unmap managed windows */
817 TRACE( "unmapping win %p\n", hwnd );
818 wine_tsx11_lock();
819 XUnmapWindow( display, get_whole_window(wndPtr) );
820 wine_tsx11_unlock();
825 if (changed & WS_DISABLED)
827 if (wndPtr->dwExStyle & WS_EX_MANAGED)
829 XWMHints *wm_hints;
830 wine_tsx11_lock();
831 if (!(wm_hints = XGetWMHints( display, get_whole_window(wndPtr) )))
832 wm_hints = XAllocWMHints();
833 if (wm_hints)
835 wm_hints->flags |= InputHint;
836 wm_hints->input = !(wndPtr->dwStyle & WS_DISABLED);
837 XSetWMHints( display, get_whole_window(wndPtr), wm_hints );
838 XFree(wm_hints);
840 wine_tsx11_unlock();
843 WIN_ReleasePtr(wndPtr);
847 /***********************************************************************
848 * SetWindowPos (X11DRV.@)
850 BOOL X11DRV_SetWindowPos( WINDOWPOS *winpos )
852 WND *wndPtr;
853 RECT newWindowRect, newClientRect;
854 RECT oldWindowRect, oldClientRect;
855 UINT wvrFlags = 0;
856 BOOL bChangePos;
858 TRACE( "hwnd %p, after %p, swp %d,%d %dx%d flags %08x\n",
859 winpos->hwnd, winpos->hwndInsertAfter, winpos->x, winpos->y,
860 winpos->cx, winpos->cy, winpos->flags);
862 bChangePos = !(winpos->flags & SWP_WINE_NOHOSTMOVE);
863 winpos->flags &= ~SWP_WINE_NOHOSTMOVE;
865 /* Check window handle */
866 if (winpos->hwnd == GetDesktopWindow()) return FALSE;
868 /* First make sure that coordinates are valid for WM_WINDOWPOSCHANGING */
869 if (!(winpos->flags & SWP_NOMOVE))
871 if (winpos->x < -32768) winpos->x = -32768;
872 else if (winpos->x > 32767) winpos->x = 32767;
873 if (winpos->y < -32768) winpos->y = -32768;
874 else if (winpos->y > 32767) winpos->y = 32767;
876 if (!(winpos->flags & SWP_NOSIZE))
878 if (winpos->cx < 0) winpos->cx = 0;
879 else if (winpos->cx > 32767) winpos->cx = 32767;
880 if (winpos->cy < 0) winpos->cy = 0;
881 else if (winpos->cy > 32767) winpos->cy = 32767;
884 if (!SWP_DoWinPosChanging( winpos, &newWindowRect, &newClientRect )) return FALSE;
886 /* Fix redundant flags */
887 if (!fixup_flags( winpos )) return FALSE;
889 if (!(wndPtr = WIN_FindWndPtr( winpos->hwnd ))) return FALSE;
891 TRACE("\tcurrent (%ld,%ld)-(%ld,%ld), style %08x\n",
892 wndPtr->rectWindow.left, wndPtr->rectWindow.top,
893 wndPtr->rectWindow.right, wndPtr->rectWindow.bottom, (unsigned)wndPtr->dwStyle );
895 if((winpos->flags & (SWP_NOZORDER | SWP_HIDEWINDOW | SWP_SHOWWINDOW)) != SWP_NOZORDER)
897 if (GetAncestor( winpos->hwnd, GA_PARENT ) == GetDesktopWindow())
898 winpos->hwndInsertAfter = SWP_DoOwnedPopups( winpos->hwnd, winpos->hwndInsertAfter );
901 /* Common operations */
903 wvrFlags = SWP_DoNCCalcSize( winpos, &newWindowRect, &newClientRect );
905 if(!(winpos->flags & SWP_NOZORDER) && winpos->hwnd != winpos->hwndInsertAfter)
907 HWND parent = GetAncestor( winpos->hwnd, GA_PARENT );
908 if (parent) WIN_LinkWindow( winpos->hwnd, parent, winpos->hwndInsertAfter );
911 /* Reset active DCEs */
913 if( (((winpos->flags & SWP_AGG_NOPOSCHANGE) != SWP_AGG_NOPOSCHANGE) &&
914 wndPtr->dwStyle & WS_VISIBLE) ||
915 (winpos->flags & (SWP_HIDEWINDOW | SWP_SHOWWINDOW)) )
917 RECT rect;
919 UnionRect(&rect, &newWindowRect, &wndPtr->rectWindow);
920 DCE_InvalidateDCE(wndPtr->hwndSelf, &rect);
923 oldWindowRect = wndPtr->rectWindow;
924 oldClientRect = wndPtr->rectClient;
926 /* Find out if we have to redraw the whole client rect */
928 if( oldClientRect.bottom - oldClientRect.top ==
929 newClientRect.bottom - newClientRect.top ) wvrFlags &= ~WVR_VREDRAW;
931 if( oldClientRect.right - oldClientRect.left ==
932 newClientRect.right - newClientRect.left ) wvrFlags &= ~WVR_HREDRAW;
934 /* FIXME: actually do something with WVR_VALIDRECTS */
936 WIN_SetRectangles( winpos->hwnd, &newWindowRect, &newClientRect );
938 if (get_whole_window(wndPtr)) /* don't do anything if X window not created yet */
940 Display *display = thread_display();
942 if (!(winpos->flags & SWP_SHOWWINDOW) && (winpos->flags & SWP_HIDEWINDOW))
944 /* clear the update region */
945 RedrawWindow( winpos->hwnd, NULL, 0, RDW_VALIDATE | RDW_NOFRAME |
946 RDW_NOERASE | RDW_NOINTERNALPAINT | RDW_ALLCHILDREN );
947 set_visible_style( winpos->hwnd, FALSE );
949 else if ((wndPtr->dwStyle & WS_VISIBLE) && bChangePos &&
950 X11DRV_is_window_rect_mapped( &oldWindowRect ) &&
951 !X11DRV_is_window_rect_mapped( &newWindowRect ))
953 /* resizing to zero size or off screen -> unmap */
954 TRACE( "unmapping zero size or off-screen win %p\n", winpos->hwnd );
955 wine_tsx11_lock();
956 XUnmapWindow( display, get_whole_window(wndPtr) );
957 wine_tsx11_unlock();
960 wine_tsx11_lock();
961 if (bChangePos)
962 X11DRV_sync_whole_window_position( display, wndPtr, !(winpos->flags & SWP_NOZORDER) );
963 else
965 struct x11drv_win_data *data = wndPtr->pDriverData;
966 data->whole_rect = wndPtr->rectWindow;
967 X11DRV_window_to_X_rect( wndPtr, &data->whole_rect );
970 if (X11DRV_sync_client_window_position( display, wndPtr ) ||
971 (winpos->flags & SWP_FRAMECHANGED))
973 /* if we moved the client area, repaint the whole non-client window */
974 XClearArea( display, get_whole_window(wndPtr), 0, 0, 0, 0, True );
975 winpos->flags |= SWP_FRAMECHANGED;
977 if (winpos->flags & SWP_SHOWWINDOW)
979 set_visible_style( winpos->hwnd, TRUE );
981 else if ((wndPtr->dwStyle & WS_VISIBLE) && bChangePos &&
982 !X11DRV_is_window_rect_mapped( &oldWindowRect ) &&
983 X11DRV_is_window_rect_mapped( &newWindowRect ))
985 /* resizing from zero size to non-zero -> map */
986 TRACE( "mapping non zero size or off-screen win %p\n", winpos->hwnd );
987 XMapWindow( display, get_whole_window(wndPtr) );
989 XFlush( display ); /* FIXME: should not be necessary */
990 wine_tsx11_unlock();
992 else /* no X window, simply toggle the window style */
994 if (winpos->flags & SWP_SHOWWINDOW)
995 set_visible_style( winpos->hwnd, TRUE );
996 else if (winpos->flags & SWP_HIDEWINDOW)
997 set_visible_style( winpos->hwnd, FALSE );
1000 /* manually expose the areas that X won't expose because they are still covered by something */
1002 if (!(winpos->flags & SWP_SHOWWINDOW))
1003 expose_covered_parent_area( wndPtr, &oldWindowRect );
1005 if (wndPtr->dwStyle & WS_VISIBLE)
1006 expose_covered_window_area( wndPtr, &oldClientRect, winpos->flags & SWP_FRAMECHANGED );
1008 WIN_ReleaseWndPtr(wndPtr);
1010 if (wvrFlags & WVR_REDRAW) RedrawWindow( winpos->hwnd, NULL, 0, RDW_INVALIDATE | RDW_ERASE );
1012 if( winpos->flags & SWP_HIDEWINDOW )
1013 HideCaret(winpos->hwnd);
1014 else if (winpos->flags & SWP_SHOWWINDOW)
1015 ShowCaret(winpos->hwnd);
1017 if (!(winpos->flags & SWP_NOACTIVATE))
1019 /* child windows get WM_CHILDACTIVATE message */
1020 if ((GetWindowLongW( winpos->hwnd, GWL_STYLE ) & (WS_CHILD | WS_POPUP)) == WS_CHILD)
1021 SendMessageA( winpos->hwnd, WM_CHILDACTIVATE, 0, 0 );
1022 else
1023 SetForegroundWindow( winpos->hwnd );
1026 /* And last, send the WM_WINDOWPOSCHANGED message */
1028 TRACE("\tstatus flags = %04x\n", winpos->flags & SWP_AGG_STATUSFLAGS);
1030 if (((winpos->flags & SWP_AGG_STATUSFLAGS) != SWP_AGG_NOPOSCHANGE))
1032 /* WM_WINDOWPOSCHANGED is sent even if SWP_NOSENDCHANGING is set
1033 and always contains final window position.
1035 winpos->x = newWindowRect.left;
1036 winpos->y = newWindowRect.top;
1037 winpos->cx = newWindowRect.right - newWindowRect.left;
1038 winpos->cy = newWindowRect.bottom - newWindowRect.top;
1039 SendMessageW( winpos->hwnd, WM_WINDOWPOSCHANGED, 0, (LPARAM)winpos );
1042 return TRUE;
1046 /***********************************************************************
1047 * WINPOS_FindIconPos
1049 * Find a suitable place for an iconic window.
1051 static POINT WINPOS_FindIconPos( WND* wndPtr, POINT pt )
1053 RECT rectParent;
1054 HWND *list;
1055 short x, y, xspacing, yspacing;
1057 GetClientRect( wndPtr->parent, &rectParent );
1058 if ((pt.x >= rectParent.left) && (pt.x + GetSystemMetrics(SM_CXICON) < rectParent.right) &&
1059 (pt.y >= rectParent.top) && (pt.y + GetSystemMetrics(SM_CYICON) < rectParent.bottom))
1060 return pt; /* The icon already has a suitable position */
1062 xspacing = GetSystemMetrics(SM_CXICONSPACING);
1063 yspacing = GetSystemMetrics(SM_CYICONSPACING);
1065 list = WIN_ListChildren( wndPtr->parent );
1066 y = rectParent.bottom;
1067 for (;;)
1069 x = rectParent.left;
1072 /* Check if another icon already occupies this spot */
1073 /* FIXME: this is completely inefficient */
1074 if (list)
1076 int i;
1077 WND *childPtr;
1079 for (i = 0; list[i]; i++)
1081 if (list[i] == wndPtr->hwndSelf) continue;
1082 if (!IsIconic( list[i] )) continue;
1083 if (!(childPtr = WIN_FindWndPtr( list[i] ))) continue;
1084 if ((childPtr->rectWindow.left < x + xspacing) &&
1085 (childPtr->rectWindow.right >= x) &&
1086 (childPtr->rectWindow.top <= y) &&
1087 (childPtr->rectWindow.bottom > y - yspacing))
1089 WIN_ReleaseWndPtr( childPtr );
1090 break; /* There's a window in there */
1092 WIN_ReleaseWndPtr( childPtr );
1094 if (list[i])
1096 /* found something here, try next spot */
1097 x += xspacing;
1098 continue;
1102 /* No window was found, so it's OK for us */
1103 pt.x = x + (xspacing - GetSystemMetrics(SM_CXICON)) / 2;
1104 pt.y = y - (yspacing + GetSystemMetrics(SM_CYICON)) / 2;
1105 HeapFree( GetProcessHeap(), 0, list );
1106 return pt;
1108 } while(x <= rectParent.right-xspacing);
1109 y -= yspacing;
1117 UINT WINPOS_MinMaximize( HWND hwnd, UINT cmd, LPRECT rect )
1119 WND *wndPtr;
1120 UINT swpFlags = 0;
1121 POINT size;
1122 LONG old_style;
1123 WINDOWPLACEMENT wpl;
1125 TRACE("%p %u\n", hwnd, cmd );
1127 wpl.length = sizeof(wpl);
1128 GetWindowPlacement( hwnd, &wpl );
1130 if (HOOK_CallHooks( WH_CBT, HCBT_MINMAX, (WPARAM)hwnd, cmd, TRUE ))
1131 return SWP_NOSIZE | SWP_NOMOVE;
1133 if (IsIconic( hwnd ))
1135 if (cmd == SW_MINIMIZE) return SWP_NOSIZE | SWP_NOMOVE;
1136 if (!SendMessageA( hwnd, WM_QUERYOPEN, 0, 0 )) return SWP_NOSIZE | SWP_NOMOVE;
1137 swpFlags |= SWP_NOCOPYBITS;
1140 if (!(wndPtr = WIN_FindWndPtr( hwnd ))) return 0;
1142 size.x = wndPtr->rectWindow.left;
1143 size.y = wndPtr->rectWindow.top;
1145 switch( cmd )
1147 case SW_MINIMIZE:
1148 if( wndPtr->dwStyle & WS_MAXIMIZE) wndPtr->flags |= WIN_RESTORE_MAX;
1149 else wndPtr->flags &= ~WIN_RESTORE_MAX;
1151 WIN_SetStyle( hwnd, (wndPtr->dwStyle & ~WS_MAXIMIZE) | WS_MINIMIZE );
1153 X11DRV_set_iconic_state( wndPtr );
1155 wpl.ptMinPosition = WINPOS_FindIconPos( wndPtr, wpl.ptMinPosition );
1157 SetRect( rect, wpl.ptMinPosition.x, wpl.ptMinPosition.y,
1158 GetSystemMetrics(SM_CXICON), GetSystemMetrics(SM_CYICON) );
1159 swpFlags |= SWP_NOCOPYBITS;
1160 break;
1162 case SW_MAXIMIZE:
1163 WINPOS_GetMinMaxInfo( hwnd, &size, &wpl.ptMaxPosition, NULL, NULL );
1165 old_style = WIN_SetStyle( hwnd, (wndPtr->dwStyle & ~WS_MINIMIZE) | WS_MAXIMIZE );
1166 if (old_style & WS_MINIMIZE)
1168 WINPOS_ShowIconTitle( hwnd, FALSE );
1169 X11DRV_set_iconic_state( wndPtr );
1171 SetRect( rect, wpl.ptMaxPosition.x, wpl.ptMaxPosition.y, size.x, size.y );
1172 break;
1174 case SW_RESTORE:
1175 old_style = WIN_SetStyle( hwnd, wndPtr->dwStyle & ~(WS_MINIMIZE|WS_MAXIMIZE) );
1176 if (old_style & WS_MINIMIZE)
1178 WINPOS_ShowIconTitle( hwnd, FALSE );
1179 X11DRV_set_iconic_state( wndPtr );
1181 if( wndPtr->flags & WIN_RESTORE_MAX)
1183 /* Restore to maximized position */
1184 WINPOS_GetMinMaxInfo( hwnd, &size, &wpl.ptMaxPosition, NULL, NULL);
1185 WIN_SetStyle( hwnd, wndPtr->dwStyle | WS_MAXIMIZE );
1186 SetRect( rect, wpl.ptMaxPosition.x, wpl.ptMaxPosition.y, size.x, size.y );
1187 break;
1190 else if (!(old_style & WS_MAXIMIZE)) break;
1192 /* Restore to normal position */
1194 *rect = wpl.rcNormalPosition;
1195 rect->right -= rect->left;
1196 rect->bottom -= rect->top;
1198 break;
1201 WIN_ReleaseWndPtr( wndPtr );
1202 return swpFlags;
1206 /***********************************************************************
1207 * ShowWindow (X11DRV.@)
1209 BOOL X11DRV_ShowWindow( HWND hwnd, INT cmd )
1211 WND* wndPtr = WIN_FindWndPtr( hwnd );
1212 BOOL wasVisible, showFlag;
1213 RECT newPos = {0, 0, 0, 0};
1214 UINT swp = 0;
1216 if (!wndPtr) return FALSE;
1217 hwnd = wndPtr->hwndSelf; /* make it a full handle */
1219 wasVisible = (wndPtr->dwStyle & WS_VISIBLE) != 0;
1221 TRACE("hwnd=%p, cmd=%d, wasVisible %d\n", hwnd, cmd, wasVisible);
1223 switch(cmd)
1225 case SW_HIDE:
1226 if (!wasVisible) goto END;
1227 swp |= SWP_HIDEWINDOW | SWP_NOSIZE | SWP_NOMOVE |
1228 SWP_NOACTIVATE | SWP_NOZORDER;
1229 break;
1231 case SW_SHOWMINNOACTIVE:
1232 swp |= SWP_NOACTIVATE | SWP_NOZORDER;
1233 /* fall through */
1234 case SW_SHOWMINIMIZED:
1235 case SW_FORCEMINIMIZE: /* FIXME: Does not work if thread is hung. */
1236 swp |= SWP_SHOWWINDOW;
1237 /* fall through */
1238 case SW_MINIMIZE:
1239 swp |= SWP_FRAMECHANGED;
1240 if( !(wndPtr->dwStyle & WS_MINIMIZE) )
1241 swp |= WINPOS_MinMaximize( hwnd, SW_MINIMIZE, &newPos );
1242 else swp |= SWP_NOSIZE | SWP_NOMOVE;
1243 break;
1245 case SW_SHOWMAXIMIZED: /* same as SW_MAXIMIZE */
1246 swp |= SWP_SHOWWINDOW | SWP_FRAMECHANGED;
1247 if( !(wndPtr->dwStyle & WS_MAXIMIZE) )
1248 swp |= WINPOS_MinMaximize( hwnd, SW_MAXIMIZE, &newPos );
1249 else swp |= SWP_NOSIZE | SWP_NOMOVE;
1250 break;
1252 case SW_SHOWNA:
1253 swp |= SWP_NOACTIVATE | SWP_NOZORDER;
1254 /* fall through */
1255 case SW_SHOW:
1256 if (wasVisible) goto END;
1258 swp |= SWP_SHOWWINDOW | SWP_NOSIZE | SWP_NOMOVE;
1259 break;
1261 case SW_SHOWNOACTIVATE:
1262 swp |= SWP_NOACTIVATE | SWP_NOZORDER;
1263 /* fall through */
1264 case SW_SHOWNORMAL: /* same as SW_NORMAL: */
1265 case SW_SHOWDEFAULT: /* FIXME: should have its own handler */
1266 case SW_RESTORE:
1267 swp |= SWP_SHOWWINDOW | SWP_FRAMECHANGED;
1269 if( wndPtr->dwStyle & (WS_MINIMIZE | WS_MAXIMIZE) )
1270 swp |= WINPOS_MinMaximize( hwnd, SW_RESTORE, &newPos );
1271 else swp |= SWP_NOSIZE | SWP_NOMOVE;
1272 break;
1275 showFlag = (cmd != SW_HIDE);
1276 if (showFlag != wasVisible)
1278 SendMessageW( hwnd, WM_SHOWWINDOW, showFlag, 0 );
1279 if (!IsWindow( hwnd )) goto END;
1282 /* We can't activate a child window */
1283 if ((wndPtr->dwStyle & WS_CHILD) &&
1284 !(wndPtr->dwExStyle & WS_EX_MDICHILD))
1285 swp |= SWP_NOACTIVATE | SWP_NOZORDER;
1287 SetWindowPos( hwnd, HWND_TOP, newPos.left, newPos.top,
1288 newPos.right, newPos.bottom, LOWORD(swp) );
1289 if (cmd == SW_HIDE)
1291 HWND hFocus;
1293 /* FIXME: This will cause the window to be activated irrespective
1294 * of whether it is owned by the same thread. Has to be done
1295 * asynchronously.
1298 if (hwnd == GetActiveWindow())
1299 WINPOS_ActivateOtherWindow(hwnd);
1301 /* Revert focus to parent */
1302 hFocus = GetFocus();
1303 if (hwnd == hFocus || IsChild(hwnd, hFocus))
1305 HWND parent = GetAncestor(hwnd, GA_PARENT);
1306 if (parent == GetDesktopWindow()) parent = 0;
1307 SetFocus(parent);
1310 if (!IsWindow( hwnd )) goto END;
1311 else if( wndPtr->dwStyle & WS_MINIMIZE ) WINPOS_ShowIconTitle( hwnd, TRUE );
1313 if (wndPtr->flags & WIN_NEED_SIZE)
1315 /* should happen only in CreateWindowEx() */
1316 int wParam = SIZE_RESTORED;
1318 wndPtr->flags &= ~WIN_NEED_SIZE;
1319 if (wndPtr->dwStyle & WS_MAXIMIZE) wParam = SIZE_MAXIMIZED;
1320 else if (wndPtr->dwStyle & WS_MINIMIZE) wParam = SIZE_MINIMIZED;
1321 SendMessageA( hwnd, WM_SIZE, wParam,
1322 MAKELONG(wndPtr->rectClient.right-wndPtr->rectClient.left,
1323 wndPtr->rectClient.bottom-wndPtr->rectClient.top));
1324 SendMessageA( hwnd, WM_MOVE, 0,
1325 MAKELONG(wndPtr->rectClient.left, wndPtr->rectClient.top) );
1328 END:
1329 WIN_ReleaseWndPtr(wndPtr);
1330 return wasVisible;
1334 /**********************************************************************
1335 * X11DRV_MapNotify
1337 void X11DRV_MapNotify( HWND hwnd, XMapEvent *event )
1339 HWND hwndFocus = GetFocus();
1340 WND *win;
1342 if (!(win = WIN_GetPtr( hwnd ))) return;
1344 if ((win->dwStyle & WS_VISIBLE) &&
1345 (win->dwStyle & WS_MINIMIZE) &&
1346 (win->dwExStyle & WS_EX_MANAGED))
1348 int x, y;
1349 unsigned int width, height, border, depth;
1350 Window root, top;
1351 RECT rect;
1352 LONG style = (win->dwStyle & ~(WS_MINIMIZE|WS_MAXIMIZE)) | WS_VISIBLE;
1354 /* FIXME: hack */
1355 wine_tsx11_lock();
1356 XGetGeometry( event->display, get_whole_window(win), &root, &x, &y, &width, &height,
1357 &border, &depth );
1358 XTranslateCoordinates( event->display, get_whole_window(win), root, 0, 0, &x, &y, &top );
1359 wine_tsx11_unlock();
1360 rect.left = x;
1361 rect.top = y;
1362 rect.right = x + width;
1363 rect.bottom = y + height;
1364 X11DRV_X_to_window_rect( win, &rect );
1366 DCE_InvalidateDCE( hwnd, &win->rectWindow );
1368 if (win->flags & WIN_RESTORE_MAX) style |= WS_MAXIMIZE;
1369 WIN_SetStyle( hwnd, style );
1370 WIN_ReleasePtr( win );
1372 SendMessageA( hwnd, WM_SHOWWINDOW, SW_RESTORE, 0 );
1373 SetWindowPos( hwnd, 0, rect.left, rect.top, rect.right-rect.left, rect.bottom-rect.top,
1374 SWP_NOZORDER | SWP_WINE_NOHOSTMOVE );
1376 else WIN_ReleasePtr( win );
1377 if (hwndFocus && IsChild( hwnd, hwndFocus )) X11DRV_SetFocus(hwndFocus); /* FIXME */
1381 /**********************************************************************
1382 * X11DRV_UnmapNotify
1384 void X11DRV_UnmapNotify( HWND hwnd, XUnmapEvent *event )
1386 WND *win;
1388 if (!(win = WIN_GetPtr( hwnd ))) return;
1390 if ((win->dwStyle & WS_VISIBLE) && (win->dwExStyle & WS_EX_MANAGED) &&
1391 X11DRV_is_window_rect_mapped( &win->rectWindow ))
1393 if (win->dwStyle & WS_MAXIMIZE)
1394 win->flags |= WIN_RESTORE_MAX;
1395 else
1396 win->flags &= ~WIN_RESTORE_MAX;
1398 WIN_SetStyle( hwnd, (win->dwStyle & ~WS_MAXIMIZE) | WS_MINIMIZE );
1399 WIN_ReleasePtr( win );
1401 EndMenu();
1402 SendMessageA( hwnd, WM_SHOWWINDOW, SW_MINIMIZE, 0 );
1403 SetWindowPos( hwnd, 0, 0, 0, GetSystemMetrics(SM_CXICON), GetSystemMetrics(SM_CYICON),
1404 SWP_NOMOVE | SWP_NOACTIVATE | SWP_NOZORDER | SWP_WINE_NOHOSTMOVE );
1406 else WIN_ReleasePtr( win );
1410 /***********************************************************************
1411 * query_zorder
1413 * Synchronize internal z-order with the window manager's.
1415 static Window __get_common_ancestor( Display *display, Window A, Window B,
1416 Window** children, unsigned* total )
1418 /* find the real root window */
1420 Window root, *childrenB;
1421 unsigned totalB;
1423 wine_tsx11_lock();
1424 while( A != B && A && B )
1426 XQueryTree( display, A, &root, &A, children, total );
1427 XQueryTree( display, B, &root, &B, &childrenB, &totalB );
1428 if( childrenB ) XFree( childrenB );
1429 if( *children ) XFree( *children ), *children = NULL;
1432 if( A && B )
1434 XQueryTree( display, A, &root, &B, children, total );
1435 wine_tsx11_unlock();
1436 return A;
1438 wine_tsx11_unlock();
1439 return 0 ;
1442 static Window __get_top_decoration( Display *display, Window w, Window ancestor )
1444 Window* children, root, prev = w, parent = w;
1445 unsigned total;
1447 wine_tsx11_lock();
1450 w = parent;
1451 XQueryTree( display, w, &root, &parent, &children, &total );
1452 if( children ) XFree( children );
1453 } while( parent && parent != ancestor );
1454 wine_tsx11_unlock();
1455 TRACE("\t%08x -> %08x\n", (unsigned)prev, (unsigned)w );
1456 return ( parent ) ? w : 0 ;
1459 static unsigned __td_lookup( Window w, Window* list, unsigned max )
1461 unsigned i;
1462 for( i = max; i > 0; i-- ) if( list[i - 1] == w ) break;
1463 return i;
1466 static HWND query_zorder( Display *display, HWND hWndCheck)
1468 HWND hwndInsertAfter = HWND_TOP;
1469 Window w, parent, *children = NULL;
1470 unsigned total, check, pos, best;
1471 HWND *list = WIN_ListChildren( GetDesktopWindow() );
1472 HWND hwndA = 0, hwndB = 0;
1473 int i;
1475 /* find at least two managed windows */
1476 if (!list) return 0;
1477 for (i = 0; list[i]; i++)
1479 if (!(GetWindowLongW( list[i], GWL_EXSTYLE ) & WS_EX_MANAGED)) continue;
1480 if (!(GetWindowLongW( list[i], GWL_STYLE ) & WS_VISIBLE)) continue;
1481 if (!hwndA) hwndA = list[i];
1482 else
1484 hwndB = list[i];
1485 break;
1488 if (!hwndA || !hwndB) goto done;
1490 parent = __get_common_ancestor( display, X11DRV_get_whole_window(hwndA),
1491 X11DRV_get_whole_window(hwndB), &children, &total );
1492 if( parent && children )
1494 /* w is the ancestor if hWndCheck that is a direct descendant of 'parent' */
1496 w = __get_top_decoration( display, X11DRV_get_whole_window(hWndCheck), parent );
1498 if( w != children[total-1] ) /* check if at the top */
1500 /* X child at index 0 is at the bottom, at index total-1 is at the top */
1501 check = __td_lookup( w, children, total );
1502 best = total;
1504 /* go through all windows in Wine z-order... */
1505 for (i = 0; list[i]; i++)
1507 if (list[i] == hWndCheck) continue;
1508 if (!(GetWindowLongW( list[i], GWL_EXSTYLE ) & WS_EX_MANAGED)) continue;
1509 if (!(w = __get_top_decoration( display, X11DRV_get_whole_window(list[i]),
1510 parent ))) continue;
1511 pos = __td_lookup( w, children, total );
1512 if( pos < best && pos > check )
1514 /* find a nearest Wine window precedes hWndCheck in the real z-order */
1515 best = pos;
1516 hwndInsertAfter = list[i];
1518 if( best - check == 1 ) break;
1522 wine_tsx11_lock();
1523 if( children ) XFree( children );
1524 wine_tsx11_unlock();
1526 done:
1527 HeapFree( GetProcessHeap(), 0, list );
1528 return hwndInsertAfter;
1532 /***********************************************************************
1533 * X11DRV_handle_desktop_resize
1535 void X11DRV_handle_desktop_resize( unsigned int width, unsigned int height )
1537 RECT rect;
1538 HWND hwnd = GetDesktopWindow();
1540 screen_width = width;
1541 screen_height = height;
1542 TRACE("desktop %p change to (%dx%d)\n", hwnd, width, height);
1543 SetRect( &rect, 0, 0, width, height );
1544 WIN_SetRectangles( hwnd, &rect, &rect );
1545 SendMessageTimeoutW( HWND_BROADCAST, WM_DISPLAYCHANGE, screen_depth,
1546 MAKELPARAM( width, height ), SMTO_ABORTIFHUNG, 2000, NULL );
1550 /***********************************************************************
1551 * X11DRV_ConfigureNotify
1553 void X11DRV_ConfigureNotify( HWND hwnd, XConfigureEvent *event )
1555 HWND oldInsertAfter;
1556 struct x11drv_win_data *data;
1557 WND *win;
1558 RECT rect;
1559 WINDOWPOS winpos;
1560 int x = event->x, y = event->y;
1562 if (!(win = WIN_GetPtr( hwnd ))) return;
1563 data = win->pDriverData;
1565 /* Get geometry */
1567 if (!event->send_event) /* normal event, need to map coordinates to the root */
1569 Window child;
1570 wine_tsx11_lock();
1571 XTranslateCoordinates( event->display, data->whole_window, root_window,
1572 0, 0, &x, &y, &child );
1573 wine_tsx11_unlock();
1575 rect.left = x;
1576 rect.top = y;
1577 rect.right = x + event->width;
1578 rect.bottom = y + event->height;
1579 TRACE( "win %p new X rect %ld,%ld,%ldx%ld (event %d,%d,%dx%d)\n",
1580 hwnd, rect.left, rect.top, rect.right-rect.left, rect.bottom-rect.top,
1581 event->x, event->y, event->width, event->height );
1582 X11DRV_X_to_window_rect( win, &rect );
1583 WIN_ReleasePtr( win );
1585 winpos.hwnd = hwnd;
1586 winpos.x = rect.left;
1587 winpos.y = rect.top;
1588 winpos.cx = rect.right - rect.left;
1589 winpos.cy = rect.bottom - rect.top;
1590 winpos.flags = SWP_NOACTIVATE;
1592 /* Get Z-order (FIXME) */
1594 winpos.hwndInsertAfter = query_zorder( event->display, hwnd );
1596 /* needs to find the first Visible Window above the current one */
1597 oldInsertAfter = hwnd;
1598 for (;;)
1600 oldInsertAfter = GetWindow( oldInsertAfter, GW_HWNDPREV );
1601 if (!oldInsertAfter)
1603 oldInsertAfter = HWND_TOP;
1604 break;
1606 if (GetWindowLongA( oldInsertAfter, GWL_STYLE ) & WS_VISIBLE) break;
1609 /* Compare what has changed */
1611 GetWindowRect( hwnd, &rect );
1612 if (rect.left == winpos.x && rect.top == winpos.y) winpos.flags |= SWP_NOMOVE;
1613 else
1614 TRACE( "%p moving from (%ld,%ld) to (%d,%d)\n",
1615 hwnd, rect.left, rect.top, winpos.x, winpos.y );
1617 if ((rect.right - rect.left == winpos.cx && rect.bottom - rect.top == winpos.cy) ||
1618 IsIconic(hwnd) ||
1619 (IsRectEmpty( &rect ) && winpos.cx == 1 && winpos.cy == 1))
1620 winpos.flags |= SWP_NOSIZE;
1621 else
1622 TRACE( "%p resizing from (%ldx%ld) to (%dx%d)\n",
1623 hwnd, rect.right - rect.left, rect.bottom - rect.top,
1624 winpos.cx, winpos.cy );
1626 if (winpos.hwndInsertAfter == oldInsertAfter) winpos.flags |= SWP_NOZORDER;
1627 else
1628 TRACE( "%p restacking from after %p to after %p\n",
1629 hwnd, oldInsertAfter, winpos.hwndInsertAfter );
1631 /* if nothing changed, don't do anything */
1632 if (winpos.flags == (SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE)) return;
1634 SetWindowPos( hwnd, winpos.hwndInsertAfter, winpos.x, winpos.y,
1635 winpos.cx, winpos.cy, winpos.flags | SWP_WINE_NOHOSTMOVE );
1639 /***********************************************************************
1640 * SetWindowRgn (X11DRV.@)
1642 * Assign specified region to window (for non-rectangular windows)
1644 int X11DRV_SetWindowRgn( HWND hwnd, HRGN hrgn, BOOL redraw )
1646 WND *wndPtr;
1648 if ((wndPtr = WIN_GetPtr( hwnd )) == WND_OTHER_PROCESS)
1650 if (IsWindow( hwnd ))
1651 FIXME( "not supported on other process window %p\n", hwnd );
1652 wndPtr = NULL;
1654 if (!wndPtr)
1656 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
1657 return FALSE;
1660 if (wndPtr->hrgnWnd == hrgn)
1662 WIN_ReleasePtr( wndPtr );
1663 return TRUE;
1666 if (wndPtr->hrgnWnd)
1668 /* delete previous region */
1669 DeleteObject(wndPtr->hrgnWnd);
1670 wndPtr->hrgnWnd = 0;
1672 wndPtr->hrgnWnd = hrgn;
1674 #ifdef HAVE_LIBXSHAPE
1676 Display *display = thread_display();
1677 X11DRV_WND_DATA *data = wndPtr->pDriverData;
1679 if (data->whole_window)
1681 if (!hrgn)
1683 wine_tsx11_lock();
1684 XShapeCombineMask( display, data->whole_window,
1685 ShapeBounding, 0, 0, None, ShapeSet );
1686 wine_tsx11_unlock();
1688 else
1690 XRectangle *aXRect;
1691 int x_offset, y_offset;
1692 DWORD size;
1693 DWORD dwBufferSize = GetRegionData(hrgn, 0, NULL);
1694 PRGNDATA pRegionData = HeapAlloc(GetProcessHeap(), 0, dwBufferSize);
1695 if (!pRegionData)
1697 WIN_ReleasePtr( wndPtr );
1698 return TRUE;
1700 GetRegionData(hrgn, dwBufferSize, pRegionData);
1701 size = pRegionData->rdh.nCount;
1702 x_offset = wndPtr->rectWindow.left - data->whole_rect.left;
1703 y_offset = wndPtr->rectWindow.top - data->whole_rect.top;
1704 /* convert region's "Windows rectangles" to XRectangles */
1705 aXRect = HeapAlloc(GetProcessHeap(), 0, size * sizeof(*aXRect) );
1706 if (aXRect)
1708 XRectangle* pCurrRect = aXRect;
1709 RECT *pRect = (RECT*) pRegionData->Buffer;
1710 for (; pRect < ((RECT*) pRegionData->Buffer) + size ; ++pRect, ++pCurrRect)
1712 pCurrRect->x = pRect->left + x_offset;
1713 pCurrRect->y = pRect->top + y_offset;
1714 pCurrRect->height = pRect->bottom - pRect->top;
1715 pCurrRect->width = pRect->right - pRect->left;
1717 TRACE("Rectangle %04d of %04ld data: X=%04d, Y=%04d, Height=%04d, Width=%04d.\n",
1718 pRect - (RECT*) pRegionData->Buffer,
1719 size,
1720 pCurrRect->x,
1721 pCurrRect->y,
1722 pCurrRect->height,
1723 pCurrRect->width);
1726 /* shape = non-rectangular windows (X11/extensions) */
1727 wine_tsx11_lock();
1728 XShapeCombineRectangles( display, data->whole_window, ShapeBounding,
1729 0, 0, aXRect,
1730 pCurrRect - aXRect, ShapeSet, YXBanded );
1731 wine_tsx11_unlock();
1732 HeapFree(GetProcessHeap(), 0, aXRect );
1734 HeapFree(GetProcessHeap(), 0, pRegionData);
1738 #endif /* HAVE_LIBXSHAPE */
1740 WIN_ReleasePtr( wndPtr );
1741 if (redraw) RedrawWindow( hwnd, NULL, 0, RDW_FRAME | RDW_INVALIDATE | RDW_ERASE );
1742 return TRUE;
1746 /***********************************************************************
1747 * draw_moving_frame
1749 * Draw the frame used when moving or resizing window.
1751 * FIXME: This causes problems in Win95 mode. (why?)
1753 static void draw_moving_frame( HDC hdc, RECT *rect, BOOL thickframe )
1755 if (thickframe)
1757 const int width = GetSystemMetrics(SM_CXFRAME);
1758 const int height = GetSystemMetrics(SM_CYFRAME);
1760 HBRUSH hbrush = SelectObject( hdc, GetStockObject( GRAY_BRUSH ) );
1761 PatBlt( hdc, rect->left, rect->top,
1762 rect->right - rect->left - width, height, PATINVERT );
1763 PatBlt( hdc, rect->left, rect->top + height, width,
1764 rect->bottom - rect->top - height, PATINVERT );
1765 PatBlt( hdc, rect->left + width, rect->bottom - 1,
1766 rect->right - rect->left - width, -height, PATINVERT );
1767 PatBlt( hdc, rect->right - 1, rect->top, -width,
1768 rect->bottom - rect->top - height, PATINVERT );
1769 SelectObject( hdc, hbrush );
1771 else DrawFocusRect( hdc, rect );
1775 /***********************************************************************
1776 * start_size_move
1778 * Initialisation of a move or resize, when initiatied from a menu choice.
1779 * Return hit test code for caption or sizing border.
1781 static LONG start_size_move( HWND hwnd, WPARAM wParam, POINT *capturePoint, LONG style )
1783 LONG hittest = 0;
1784 POINT pt;
1785 MSG msg;
1786 RECT rectWindow;
1788 GetWindowRect( hwnd, &rectWindow );
1790 if ((wParam & 0xfff0) == SC_MOVE)
1792 /* Move pointer at the center of the caption */
1793 RECT rect;
1794 NC_GetInsideRect( hwnd, &rect );
1795 if (style & WS_SYSMENU)
1796 rect.left += GetSystemMetrics(SM_CXSIZE) + 1;
1797 if (style & WS_MINIMIZEBOX)
1798 rect.right -= GetSystemMetrics(SM_CXSIZE) + 1;
1799 if (style & WS_MAXIMIZEBOX)
1800 rect.right -= GetSystemMetrics(SM_CXSIZE) + 1;
1801 pt.x = rectWindow.left + (rect.right - rect.left) / 2;
1802 pt.y = rectWindow.top + rect.top + GetSystemMetrics(SM_CYSIZE)/2;
1803 hittest = HTCAPTION;
1804 *capturePoint = pt;
1806 else /* SC_SIZE */
1808 while(!hittest)
1810 GetMessageW( &msg, 0, WM_KEYFIRST, WM_MOUSELAST );
1811 if (CallMsgFilterW( &msg, MSGF_SIZE )) continue;
1813 switch(msg.message)
1815 case WM_MOUSEMOVE:
1816 hittest = NC_HandleNCHitTest( hwnd, msg.pt );
1817 if ((hittest < HTLEFT) || (hittest > HTBOTTOMRIGHT))
1818 hittest = 0;
1819 break;
1821 case WM_LBUTTONUP:
1822 return 0;
1824 case WM_KEYDOWN:
1825 switch(msg.wParam)
1827 case VK_UP:
1828 hittest = HTTOP;
1829 pt.x =(rectWindow.left+rectWindow.right)/2;
1830 pt.y = rectWindow.top + GetSystemMetrics(SM_CYFRAME) / 2;
1831 break;
1832 case VK_DOWN:
1833 hittest = HTBOTTOM;
1834 pt.x =(rectWindow.left+rectWindow.right)/2;
1835 pt.y = rectWindow.bottom - GetSystemMetrics(SM_CYFRAME) / 2;
1836 break;
1837 case VK_LEFT:
1838 hittest = HTLEFT;
1839 pt.x = rectWindow.left + GetSystemMetrics(SM_CXFRAME) / 2;
1840 pt.y =(rectWindow.top+rectWindow.bottom)/2;
1841 break;
1842 case VK_RIGHT:
1843 hittest = HTRIGHT;
1844 pt.x = rectWindow.right - GetSystemMetrics(SM_CXFRAME) / 2;
1845 pt.y =(rectWindow.top+rectWindow.bottom)/2;
1846 break;
1847 case VK_RETURN:
1848 case VK_ESCAPE: return 0;
1852 *capturePoint = pt;
1854 SetCursorPos( pt.x, pt.y );
1855 NC_HandleSetCursor( hwnd, (WPARAM)hwnd, MAKELONG( hittest, WM_MOUSEMOVE ));
1856 return hittest;
1860 /***********************************************************************
1861 * set_movesize_capture
1863 static void set_movesize_capture( HWND hwnd )
1865 HWND previous = 0;
1867 SERVER_START_REQ( set_capture_window )
1869 req->handle = hwnd;
1870 req->flags = CAPTURE_MOVESIZE;
1871 if (!wine_server_call_err( req ))
1873 previous = reply->previous;
1874 hwnd = reply->full_handle;
1877 SERVER_END_REQ;
1878 if (previous && previous != hwnd)
1879 SendMessageW( previous, WM_CAPTURECHANGED, 0, (LPARAM)hwnd );
1883 /***********************************************************************
1884 * SysCommandSizeMove (X11DRV.@)
1886 * Perform SC_MOVE and SC_SIZE commands.
1888 void X11DRV_SysCommandSizeMove( HWND hwnd, WPARAM wParam )
1890 MSG msg;
1891 RECT sizingRect, mouseRect, origRect;
1892 HDC hdc;
1893 HWND parent;
1894 LONG hittest = (LONG)(wParam & 0x0f);
1895 HCURSOR hDragCursor = 0, hOldCursor = 0;
1896 POINT minTrack, maxTrack;
1897 POINT capturePoint, pt;
1898 LONG style = GetWindowLongA( hwnd, GWL_STYLE );
1899 LONG exstyle = GetWindowLongA( hwnd, GWL_EXSTYLE );
1900 BOOL thickframe = HAS_THICKFRAME( style, exstyle );
1901 BOOL iconic = style & WS_MINIMIZE;
1902 BOOL moved = FALSE;
1903 DWORD dwPoint = GetMessagePos ();
1904 BOOL DragFullWindows = FALSE;
1905 BOOL grab;
1906 int iWndsLocks;
1907 Display *old_gdi_display = NULL;
1908 Display *display = thread_display();
1910 SystemParametersInfoA(SPI_GETDRAGFULLWINDOWS, 0, &DragFullWindows, 0);
1912 pt.x = (short)LOWORD(dwPoint);
1913 pt.y = (short)HIWORD(dwPoint);
1914 capturePoint = pt;
1916 if (IsZoomed(hwnd) || !IsWindowVisible(hwnd) || (exstyle & WS_EX_MANAGED)) return;
1918 if ((wParam & 0xfff0) == SC_MOVE)
1920 if (!hittest) hittest = start_size_move( hwnd, wParam, &capturePoint, style );
1921 if (!hittest) return;
1923 else /* SC_SIZE */
1925 if (!thickframe) return;
1926 if ( hittest && ((wParam & 0xfff0) != SC_MOUSEMENU) )
1927 hittest += (HTLEFT - WMSZ_LEFT);
1928 else
1930 set_movesize_capture( hwnd );
1931 hittest = start_size_move( hwnd, wParam, &capturePoint, style );
1932 if (!hittest)
1934 set_movesize_capture(0);
1935 return;
1940 /* Get min/max info */
1942 WINPOS_GetMinMaxInfo( hwnd, NULL, NULL, &minTrack, &maxTrack );
1943 GetWindowRect( hwnd, &sizingRect );
1944 if (style & WS_CHILD)
1946 parent = GetParent(hwnd);
1947 /* make sizing rect relative to parent */
1948 MapWindowPoints( 0, parent, (POINT*)&sizingRect, 2 );
1949 GetClientRect( parent, &mouseRect );
1951 else
1953 parent = 0;
1954 SetRect(&mouseRect, 0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN));
1956 origRect = sizingRect;
1958 if (ON_LEFT_BORDER(hittest))
1960 mouseRect.left = max( mouseRect.left, sizingRect.right-maxTrack.x );
1961 mouseRect.right = min( mouseRect.right, sizingRect.right-minTrack.x );
1963 else if (ON_RIGHT_BORDER(hittest))
1965 mouseRect.left = max( mouseRect.left, sizingRect.left+minTrack.x );
1966 mouseRect.right = min( mouseRect.right, sizingRect.left+maxTrack.x );
1968 if (ON_TOP_BORDER(hittest))
1970 mouseRect.top = max( mouseRect.top, sizingRect.bottom-maxTrack.y );
1971 mouseRect.bottom = min( mouseRect.bottom,sizingRect.bottom-minTrack.y);
1973 else if (ON_BOTTOM_BORDER(hittest))
1975 mouseRect.top = max( mouseRect.top, sizingRect.top+minTrack.y );
1976 mouseRect.bottom = min( mouseRect.bottom, sizingRect.top+maxTrack.y );
1978 if (parent) MapWindowPoints( parent, 0, (LPPOINT)&mouseRect, 2 );
1980 /* Retrieve a default cache DC (without using the window style) */
1981 hdc = GetDCEx( parent, 0, DCX_CACHE );
1983 if( iconic ) /* create a cursor for dragging */
1985 hDragCursor = (HCURSOR)GetClassLongA( hwnd, GCL_HICON);
1986 if( !hDragCursor ) hDragCursor = (HCURSOR)SendMessageA( hwnd, WM_QUERYDRAGICON, 0, 0L);
1987 if( !hDragCursor ) iconic = FALSE;
1990 /* repaint the window before moving it around */
1991 RedrawWindow( hwnd, NULL, 0, RDW_UPDATENOW | RDW_ALLCHILDREN );
1993 SendMessageA( hwnd, WM_ENTERSIZEMOVE, 0, 0 );
1994 set_movesize_capture( hwnd );
1996 /* grab the server only when moving top-level windows without desktop */
1997 grab = (!DragFullWindows && !parent && (root_window == DefaultRootWindow(gdi_display)));
1999 wine_tsx11_lock();
2000 if (grab)
2002 XSync( gdi_display, False );
2003 XGrabServer( display );
2004 XSync( display, False );
2005 /* switch gdi display to the thread display, since the server is grabbed */
2006 old_gdi_display = gdi_display;
2007 gdi_display = display;
2009 XGrabPointer( display, X11DRV_get_whole_window(hwnd), False,
2010 PointerMotionMask | ButtonPressMask | ButtonReleaseMask,
2011 GrabModeAsync, GrabModeAsync,
2012 parent ? X11DRV_get_client_window(parent) : root_window,
2013 None, CurrentTime );
2014 wine_tsx11_unlock();
2016 while(1)
2018 int dx = 0, dy = 0;
2020 if (!GetMessageW( &msg, 0, WM_KEYFIRST, WM_MOUSELAST )) break;
2021 if (CallMsgFilterW( &msg, MSGF_SIZE )) continue;
2023 /* Exit on button-up, Return, or Esc */
2024 if ((msg.message == WM_LBUTTONUP) ||
2025 ((msg.message == WM_KEYDOWN) &&
2026 ((msg.wParam == VK_RETURN) || (msg.wParam == VK_ESCAPE)))) break;
2028 if ((msg.message != WM_KEYDOWN) && (msg.message != WM_MOUSEMOVE))
2029 continue; /* We are not interested in other messages */
2031 pt = msg.pt;
2033 if (msg.message == WM_KEYDOWN) switch(msg.wParam)
2035 case VK_UP: pt.y -= 8; break;
2036 case VK_DOWN: pt.y += 8; break;
2037 case VK_LEFT: pt.x -= 8; break;
2038 case VK_RIGHT: pt.x += 8; break;
2041 pt.x = max( pt.x, mouseRect.left );
2042 pt.x = min( pt.x, mouseRect.right );
2043 pt.y = max( pt.y, mouseRect.top );
2044 pt.y = min( pt.y, mouseRect.bottom );
2046 dx = pt.x - capturePoint.x;
2047 dy = pt.y - capturePoint.y;
2049 if (dx || dy)
2051 if( !moved )
2053 moved = TRUE;
2055 if( iconic ) /* ok, no system popup tracking */
2057 hOldCursor = SetCursor(hDragCursor);
2058 ShowCursor( TRUE );
2059 WINPOS_ShowIconTitle( hwnd, FALSE );
2061 else if(!DragFullWindows)
2062 draw_moving_frame( hdc, &sizingRect, thickframe );
2065 if (msg.message == WM_KEYDOWN) SetCursorPos( pt.x, pt.y );
2066 else
2068 RECT newRect = sizingRect;
2069 WPARAM wpSizingHit = 0;
2071 if (hittest == HTCAPTION) OffsetRect( &newRect, dx, dy );
2072 if (ON_LEFT_BORDER(hittest)) newRect.left += dx;
2073 else if (ON_RIGHT_BORDER(hittest)) newRect.right += dx;
2074 if (ON_TOP_BORDER(hittest)) newRect.top += dy;
2075 else if (ON_BOTTOM_BORDER(hittest)) newRect.bottom += dy;
2076 if(!iconic && !DragFullWindows) draw_moving_frame( hdc, &sizingRect, thickframe );
2077 capturePoint = pt;
2079 /* determine the hit location */
2080 if (hittest >= HTLEFT && hittest <= HTBOTTOMRIGHT)
2081 wpSizingHit = WMSZ_LEFT + (hittest - HTLEFT);
2082 SendMessageA( hwnd, WM_SIZING, wpSizingHit, (LPARAM)&newRect );
2084 if (!iconic)
2086 if(!DragFullWindows)
2087 draw_moving_frame( hdc, &newRect, thickframe );
2088 else {
2089 /* To avoid any deadlocks, all the locks on the windows
2090 structures must be suspended before the SetWindowPos */
2091 iWndsLocks = WIN_SuspendWndsLock();
2092 SetWindowPos( hwnd, 0, newRect.left, newRect.top,
2093 newRect.right - newRect.left,
2094 newRect.bottom - newRect.top,
2095 ( hittest == HTCAPTION ) ? SWP_NOSIZE : 0 );
2096 WIN_RestoreWndsLock(iWndsLocks);
2099 sizingRect = newRect;
2104 set_movesize_capture(0);
2105 if( iconic )
2107 if( moved ) /* restore cursors, show icon title later on */
2109 ShowCursor( FALSE );
2110 SetCursor( hOldCursor );
2113 else if (moved && !DragFullWindows)
2114 draw_moving_frame( hdc, &sizingRect, thickframe );
2116 ReleaseDC( parent, hdc );
2118 wine_tsx11_lock();
2119 XUngrabPointer( display, CurrentTime );
2120 if (grab)
2122 XSync( display, False );
2123 XUngrabServer( display );
2124 XSync( display, False );
2125 gdi_display = old_gdi_display;
2127 wine_tsx11_unlock();
2129 if (HOOK_CallHooks( WH_CBT, HCBT_MOVESIZE, (WPARAM)hwnd, (LPARAM)&sizingRect, TRUE ))
2130 moved = FALSE;
2132 SendMessageA( hwnd, WM_EXITSIZEMOVE, 0, 0 );
2133 SendMessageA( hwnd, WM_SETVISIBLE, !IsIconic(hwnd), 0L);
2135 /* window moved or resized */
2136 if (moved)
2138 /* To avoid any deadlocks, all the locks on the windows
2139 structures must be suspended before the SetWindowPos */
2140 iWndsLocks = WIN_SuspendWndsLock();
2142 /* if the moving/resizing isn't canceled call SetWindowPos
2143 * with the new position or the new size of the window
2145 if (!((msg.message == WM_KEYDOWN) && (msg.wParam == VK_ESCAPE)) )
2147 /* NOTE: SWP_NOACTIVATE prevents document window activation in Word 6 */
2148 if(!DragFullWindows)
2149 SetWindowPos( hwnd, 0, sizingRect.left, sizingRect.top,
2150 sizingRect.right - sizingRect.left,
2151 sizingRect.bottom - sizingRect.top,
2152 ( hittest == HTCAPTION ) ? SWP_NOSIZE : 0 );
2154 else
2155 { /* restore previous size/position */
2156 if(DragFullWindows)
2157 SetWindowPos( hwnd, 0, origRect.left, origRect.top,
2158 origRect.right - origRect.left,
2159 origRect.bottom - origRect.top,
2160 ( hittest == HTCAPTION ) ? SWP_NOSIZE : 0 );
2163 WIN_RestoreWndsLock(iWndsLocks);
2166 if (IsIconic(hwnd))
2168 /* Single click brings up the system menu when iconized */
2170 if( !moved )
2172 if(style & WS_SYSMENU )
2173 SendMessageA( hwnd, WM_SYSCOMMAND,
2174 SC_MOUSEMENU + HTSYSMENU, MAKELONG(pt.x,pt.y));
2176 else WINPOS_ShowIconTitle( hwnd, TRUE );
2181 /***********************************************************************
2182 * ForceWindowRaise (X11DRV.@)
2184 * Raise a window on top of the X stacking order, while preserving
2185 * the correct Windows Z order.
2187 * FIXME: this should go away.
2189 void X11DRV_ForceWindowRaise( HWND hwnd )
2191 int i = 0;
2192 HWND *list;
2193 XWindowChanges winChanges;
2194 Display *display = thread_display();
2195 WND *wndPtr = WIN_FindWndPtr( hwnd );
2197 if (!wndPtr) return;
2199 if ((wndPtr->dwExStyle & WS_EX_MANAGED) ||
2200 wndPtr->parent != GetDesktopWindow() ||
2201 IsRectEmpty( &wndPtr->rectWindow ) ||
2202 !get_whole_window(wndPtr))
2204 WIN_ReleaseWndPtr( wndPtr );
2205 return;
2207 WIN_ReleaseWndPtr( wndPtr );
2209 /* Raise all windows up to wndPtr according to their Z order.
2210 * (it would be easier with sibling-related Below but it doesn't
2211 * work very well with SGI mwm for instance)
2213 winChanges.stack_mode = Above;
2214 if (!(list = WIN_ListChildren( GetDesktopWindow() ))) return;
2215 while (list[i] && list[i] != hwnd) i++;
2216 if (list[i])
2218 for ( ; i >= 0; i--)
2220 WND *ptr = WIN_FindWndPtr( list[i] );
2221 if (!ptr) continue;
2222 if (!IsRectEmpty( &ptr->rectWindow ) && get_whole_window(ptr))
2224 wine_tsx11_lock();
2225 XReconfigureWMWindow( display, get_whole_window(ptr), 0, CWStackMode, &winChanges );
2226 wine_tsx11_unlock();
2228 WIN_ReleaseWndPtr( ptr );
2231 HeapFree( GetProcessHeap(), 0, list );