Removed struct32.[ch].
[wine/multimedia.git] / dlls / x11drv / winpos.c
blob61beee7e65c7114c4e92c2af47ec59b90d5906d7
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))
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 * clip_children
91 * Clip all children of a given window out of the visible region
93 static int clip_children( HWND parent, HWND last, HRGN hrgn, int whole_window )
95 HWND *list;
96 WND *ptr;
97 HRGN rectRgn;
98 int i, x, y, ret = SIMPLEREGION;
100 /* first check if we have anything to do */
101 if (!(list = WIN_ListChildren( parent ))) return ret;
103 if (whole_window)
105 WND *win = WIN_FindWndPtr( parent );
106 x = win->rectWindow.left - win->rectClient.left;
107 y = win->rectWindow.top - win->rectClient.top;
108 WIN_ReleaseWndPtr( win );
110 else x = y = 0;
112 rectRgn = CreateRectRgn( 0, 0, 0, 0 );
114 for (i = 0; list[i] && list[i] != last; i++)
116 if (!(ptr = WIN_FindWndPtr( list[i] ))) continue;
117 if ((ptr->dwStyle & WS_VISIBLE) && !(ptr->dwExStyle & WS_EX_TRANSPARENT))
119 SetRectRgn( rectRgn, ptr->rectWindow.left + x, ptr->rectWindow.top + y,
120 ptr->rectWindow.right + x, ptr->rectWindow.bottom + y );
121 if ((ret = CombineRgn( hrgn, hrgn, rectRgn, RGN_DIFF )) == NULLREGION)
123 WIN_ReleaseWndPtr( ptr );
124 break; /* no need to go on, region is empty */
127 WIN_ReleaseWndPtr( ptr );
129 DeleteObject( rectRgn );
130 HeapFree( GetProcessHeap(), 0, list );
131 return ret;
135 /***********************************************************************
136 * get_server_visible_region
138 static HRGN get_server_visible_region( HWND hwnd, HWND top, UINT flags )
140 RGNDATA *data;
141 HRGN ret = 0;
142 size_t size = 256;
143 BOOL retry = FALSE;
147 if (!(data = HeapAlloc( GetProcessHeap(), 0, sizeof(*data) + size - 1 ))) return 0;
148 SERVER_START_REQ( get_visible_region )
150 req->window = hwnd;
151 req->top_win = top;
152 req->flags = flags;
153 wine_server_set_reply( req, data->Buffer, size );
154 if (!wine_server_call_err( req ))
156 if (reply->total_size <= size)
158 size_t reply_size = wine_server_reply_size( reply );
159 data->rdh.dwSize = sizeof(data->rdh);
160 data->rdh.iType = RDH_RECTANGLES;
161 data->rdh.nCount = reply_size / sizeof(RECT);
162 data->rdh.nRgnSize = reply_size;
163 ret = ExtCreateRegion( NULL, size, data );
164 retry = FALSE;
166 else
168 size = reply->total_size;
169 retry = TRUE;
173 SERVER_END_REQ;
174 HeapFree( GetProcessHeap(), 0, data );
175 } while (retry);
176 return ret;
180 /***********************************************************************
181 * get_covered_region
183 * Compute the portion of 'rgn' that is covered by non-clipped siblings.
184 * This is the area that is covered from X point of view, but may still need
185 * to be exposed.
186 * 'rgn' must be relative to the client area of the parent of 'win'.
188 static int get_covered_region( WND *win, HRGN rgn )
190 HRGN tmp;
191 int ret;
192 WND *parent, *ptr = WIN_FindWndPtr( win->hwndSelf );
193 int xoffset = 0, yoffset = 0;
195 tmp = CreateRectRgn( 0, 0, 0, 0 );
196 CombineRgn( tmp, rgn, 0, RGN_COPY );
198 /* to make things easier we actually build the uncovered
199 * area by removing all siblings and then we subtract that
200 * from the total region to get the covered area */
201 for (;;)
203 if (!(ptr->dwStyle & WS_CLIPSIBLINGS))
205 if (clip_children( ptr->parent, ptr->hwndSelf, tmp, FALSE ) == NULLREGION) break;
207 if (!(parent = WIN_FindWndPtr( ptr->parent ))) break;
208 WIN_ReleaseWndPtr( ptr );
209 ptr = parent;
210 OffsetRgn( tmp, ptr->rectClient.left, ptr->rectClient.top );
211 xoffset += ptr->rectClient.left;
212 yoffset += ptr->rectClient.top;
214 WIN_ReleaseWndPtr( ptr );
215 /* make it relative to the target window again */
216 OffsetRgn( tmp, -xoffset, -yoffset );
218 /* now subtract the computed region from the original one */
219 ret = CombineRgn( rgn, rgn, tmp, RGN_DIFF );
220 DeleteObject( tmp );
221 return ret;
225 /***********************************************************************
226 * expose_window
228 * Expose a region of a given window.
230 static void expose_window( HWND hwnd, RECT *rect, HRGN rgn, int flags )
232 POINT offset;
233 HWND top = 0;
234 HWND *list;
235 int i;
237 /* find the top most parent that doesn't clip children or siblings and
238 * invalidate the area on its parent, including all children */
239 if ((list = WIN_ListParents( hwnd )))
241 HWND current = hwnd;
242 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
243 for (i = 0; list[i] && list[i] != GetDesktopWindow(); i++)
245 if (!(style & WS_CLIPSIBLINGS)) top = current;
246 style = GetWindowLongW( list[i], GWL_STYLE );
247 if (!(style & WS_CLIPCHILDREN)) top = current;
248 current = list[i];
251 if (top)
253 /* find the parent of the top window, reusing the parent list */
254 if (top == hwnd) i = 0;
255 else
257 for (i = 0; list[i]; i++) if (list[i] == top) break;
258 if (list[i] && list[i+1]) i++;
260 if (list[i] != GetDesktopWindow()) top = list[i];
261 flags &= ~RDW_FRAME; /* parent will invalidate children frame anyway */
262 flags |= RDW_ALLCHILDREN;
264 HeapFree( GetProcessHeap(), 0, list );
267 if (!top) top = hwnd;
269 /* make coords relative to top */
270 offset.x = offset.y = 0;
271 MapWindowPoints( hwnd, top, &offset, 1 );
273 if (rect)
275 OffsetRect( rect, offset.x, offset.y );
276 RedrawWindow( top, rect, 0, flags );
278 else
280 OffsetRgn( rgn, offset.x, offset.y );
281 RedrawWindow( top, NULL, rgn, flags );
286 /***********************************************************************
287 * expose_covered_parent_area
289 * Expose the parent area that has been uncovered by moving/hiding a
290 * given window, but that is still covered by other siblings (the area
291 * not covered by siblings will be exposed automatically by X).
293 static void expose_covered_parent_area( WND *win, const RECT *old_rect )
295 int ret = SIMPLEREGION;
296 HRGN hrgn = CreateRectRgnIndirect( old_rect );
298 if (win->dwStyle & WS_VISIBLE)
300 HRGN tmp = CreateRectRgnIndirect( &win->rectWindow );
301 ret = CombineRgn( hrgn, hrgn, tmp, RGN_DIFF );
302 DeleteObject( tmp );
305 if (ret != NULLREGION)
307 if (get_covered_region( win, hrgn ) != NULLREGION)
308 expose_window( win->parent, NULL, hrgn, RDW_INVALIDATE | RDW_ERASE | RDW_ALLCHILDREN );
310 DeleteObject( hrgn );
314 /***********************************************************************
315 * expose_covered_window_area
317 * Expose the area of a window that is covered by other siblings.
319 static void expose_covered_window_area( WND *win, const RECT *old_client_rect, BOOL frame )
321 HRGN hrgn;
322 int ret = SIMPLEREGION;
324 if (frame)
325 hrgn = CreateRectRgn( win->rectWindow.left - win->rectClient.left,
326 win->rectWindow.top - win->rectClient.top,
327 win->rectWindow.right - win->rectWindow.left,
328 win->rectWindow.bottom - win->rectWindow.top );
329 else
330 hrgn = CreateRectRgn( 0, 0,
331 win->rectClient.right - win->rectClient.left,
332 win->rectClient.bottom - win->rectClient.top );
334 /* if the client rect didn't move we don't need to repaint it all */
335 if (old_client_rect->left == win->rectClient.left &&
336 old_client_rect->top == win->rectClient.top)
338 RECT rc;
340 if (IntersectRect( &rc, old_client_rect, &win->rectClient ))
342 HRGN tmp;
343 /* subtract the unchanged client area from the region to expose */
344 OffsetRect( &rc, -win->rectClient.left, -win->rectClient.top );
345 if ((tmp = CreateRectRgnIndirect( &rc )))
347 ret = CombineRgn( hrgn, hrgn, tmp, RGN_DIFF );
348 DeleteObject( tmp );
353 if (ret != NULLREGION)
355 if (get_covered_region( win, hrgn ) != NULLREGION)
356 expose_window( win->hwndSelf, NULL, hrgn,
357 RDW_INVALIDATE | RDW_ERASE | RDW_FRAME | RDW_ALLCHILDREN );
360 DeleteObject( hrgn );
364 /***********************************************************************
365 * X11DRV_Expose
367 void X11DRV_Expose( HWND hwnd, XExposeEvent *event )
369 RECT rect;
370 struct x11drv_win_data *data;
371 int flags = RDW_INVALIDATE | RDW_ERASE;
372 WND *win;
374 TRACE( "win %p (%lx) %d,%d %dx%d\n",
375 hwnd, event->window, event->x, event->y, event->width, event->height );
377 rect.left = event->x;
378 rect.top = event->y;
379 rect.right = rect.left + event->width;
380 rect.bottom = rect.top + event->height;
382 if (!(win = WIN_GetPtr( hwnd ))) return;
383 data = win->pDriverData;
385 if (event->window != data->client_window) /* whole window or icon window */
387 flags |= RDW_FRAME;
388 /* make position relative to client area instead of window */
389 OffsetRect( &rect, -data->client_rect.left, -data->client_rect.top );
391 WIN_ReleasePtr( win );
393 expose_window( hwnd, &rect, 0, flags );
397 /***********************************************************************
398 * GetDC (X11DRV.@)
400 * Set the drawable, origin and dimensions for the DC associated to
401 * a given window.
403 BOOL X11DRV_GetDC( HWND hwnd, HDC hdc, HRGN hrgn, DWORD flags )
405 WND *win = WIN_GetPtr( hwnd );
406 HWND top = 0;
407 X11DRV_WND_DATA *data = win->pDriverData;
408 struct x11drv_escape_set_drawable escape;
410 escape.mode = IncludeInferiors;
411 /* don't clip siblings if using parent clip region */
412 if (flags & DCX_PARENTCLIP) flags &= ~DCX_CLIPSIBLINGS;
414 top = GetAncestor( hwnd, GA_ROOT );
415 if (!top) top = GetDesktopWindow();
417 if (top != hwnd)
419 escape.drawable_org.x = escape.drawable_org.y = 0;
420 if (flags & (DCX_WINDOW|DCX_PARENTCLIP))
422 escape.org.x = win->rectWindow.left - win->rectClient.left;
423 escape.org.y = win->rectWindow.top - win->rectClient.top;
424 MapWindowPoints( hwnd, top, &escape.org, 1 );
425 MapWindowPoints( top, 0, &escape.drawable_org, 1 );
426 escape.drawable = X11DRV_get_client_window( top );
428 else
430 escape.org.x = escape.org.y = 0;
431 MapWindowPoints( hwnd, 0, &escape.drawable_org, 1 );
432 escape.drawable = X11DRV_get_client_window( hwnd );
435 else
437 if (IsIconic( hwnd ))
439 escape.drawable = data->icon_window ? data->icon_window : data->whole_window;
440 escape.org.x = 0;
441 escape.org.y = 0;
442 escape.drawable_org = escape.org;
444 else if (flags & DCX_WINDOW)
446 escape.drawable = data->whole_window;
447 escape.drawable_org.x = data->whole_rect.left;
448 escape.drawable_org.y = data->whole_rect.top;
449 escape.org.x = win->rectWindow.left - data->whole_rect.left;
450 escape.org.y = win->rectWindow.top - data->whole_rect.top;
452 else
454 escape.drawable = data->client_window;
455 escape.drawable_org.x = win->rectClient.left;
456 escape.drawable_org.y = win->rectClient.top;
457 escape.org.x = 0;
458 escape.org.y = 0;
462 escape.code = X11DRV_SET_DRAWABLE;
463 ExtEscape( hdc, X11DRV_ESCAPE, sizeof(escape), (LPSTR)&escape, 0, NULL );
465 if (flags & (DCX_EXCLUDERGN | DCX_INTERSECTRGN) ||
466 SetHookFlags16( HDC_16(hdc), DCHF_VALIDATEVISRGN )) /* DC was dirty */
468 /* need to recompute the visible region */
469 HRGN visRgn = get_server_visible_region( hwnd, top, flags );
471 if (flags & (DCX_EXCLUDERGN | DCX_INTERSECTRGN))
472 CombineRgn( visRgn, visRgn, hrgn, (flags & DCX_INTERSECTRGN) ? RGN_AND : RGN_DIFF );
474 SelectVisRgn16( HDC_16(hdc), HRGN_16(visRgn) );
475 DeleteObject( visRgn );
478 WIN_ReleasePtr( win );
479 return TRUE;
483 /***********************************************************************
484 * ReleaseDC (X11DRV.@)
486 void X11DRV_ReleaseDC( HWND hwnd, HDC hdc )
488 struct x11drv_escape_set_drawable escape;
490 escape.code = X11DRV_SET_DRAWABLE;
491 escape.drawable = root_window;
492 escape.mode = IncludeInferiors;
493 escape.org.x = escape.org.y = 0;
494 escape.drawable_org.x = escape.drawable_org.y = 0;
496 ExtEscape( hdc, X11DRV_ESCAPE, sizeof(escape), (LPSTR)&escape, 0, NULL );
500 /***********************************************************************
501 * SWP_DoWinPosChanging
503 static BOOL SWP_DoWinPosChanging( WINDOWPOS* pWinpos, RECT* pNewWindowRect, RECT* pNewClientRect )
505 WND *wndPtr;
507 /* Send WM_WINDOWPOSCHANGING message */
509 if (!(pWinpos->flags & SWP_NOSENDCHANGING))
510 SendMessageW( pWinpos->hwnd, WM_WINDOWPOSCHANGING, 0, (LPARAM)pWinpos );
512 if (!(wndPtr = WIN_GetPtr( pWinpos->hwnd )) || wndPtr == WND_OTHER_PROCESS) return FALSE;
514 /* Calculate new position and size */
516 *pNewWindowRect = wndPtr->rectWindow;
517 *pNewClientRect = (wndPtr->dwStyle & WS_MINIMIZE) ? wndPtr->rectWindow
518 : wndPtr->rectClient;
520 if (!(pWinpos->flags & SWP_NOSIZE))
522 pNewWindowRect->right = pNewWindowRect->left + pWinpos->cx;
523 pNewWindowRect->bottom = pNewWindowRect->top + pWinpos->cy;
525 if (!(pWinpos->flags & SWP_NOMOVE))
527 pNewWindowRect->left = pWinpos->x;
528 pNewWindowRect->top = pWinpos->y;
529 pNewWindowRect->right += pWinpos->x - wndPtr->rectWindow.left;
530 pNewWindowRect->bottom += pWinpos->y - wndPtr->rectWindow.top;
532 OffsetRect( pNewClientRect, pWinpos->x - wndPtr->rectWindow.left,
533 pWinpos->y - wndPtr->rectWindow.top );
535 pWinpos->flags |= SWP_NOCLIENTMOVE | SWP_NOCLIENTSIZE;
536 WIN_ReleasePtr( wndPtr );
537 return TRUE;
540 /***********************************************************************
541 * SWP_DoNCCalcSize
543 static UINT SWP_DoNCCalcSize( WINDOWPOS* pWinpos, RECT* pNewWindowRect, RECT* pNewClientRect )
545 UINT wvrFlags = 0;
546 WND *wndPtr;
548 if (!(wndPtr = WIN_GetPtr( pWinpos->hwnd )) || wndPtr == WND_OTHER_PROCESS) return 0;
550 /* Send WM_NCCALCSIZE message to get new client area */
551 if( (pWinpos->flags & (SWP_FRAMECHANGED | SWP_NOSIZE)) != SWP_NOSIZE )
553 NCCALCSIZE_PARAMS params;
554 WINDOWPOS winposCopy;
556 params.rgrc[0] = *pNewWindowRect;
557 params.rgrc[1] = wndPtr->rectWindow;
558 params.rgrc[2] = wndPtr->rectClient;
559 params.lppos = &winposCopy;
560 winposCopy = *pWinpos;
561 WIN_ReleasePtr( wndPtr );
563 wvrFlags = SendMessageW( pWinpos->hwnd, WM_NCCALCSIZE, TRUE, (LPARAM)&params );
565 TRACE( "(%ld,%ld)-(%ld,%ld)\n", params.rgrc[0].left, params.rgrc[0].top,
566 params.rgrc[0].right, params.rgrc[0].bottom );
568 /* If the application send back garbage, ignore it */
569 if (params.rgrc[0].left <= params.rgrc[0].right &&
570 params.rgrc[0].top <= params.rgrc[0].bottom)
571 *pNewClientRect = params.rgrc[0];
573 /* FIXME: WVR_ALIGNxxx */
575 if (!(wndPtr = WIN_GetPtr( pWinpos->hwnd )) || wndPtr == WND_OTHER_PROCESS) return 0;
577 if( pNewClientRect->left != wndPtr->rectClient.left ||
578 pNewClientRect->top != wndPtr->rectClient.top )
579 pWinpos->flags &= ~SWP_NOCLIENTMOVE;
581 if( (pNewClientRect->right - pNewClientRect->left !=
582 wndPtr->rectClient.right - wndPtr->rectClient.left) ||
583 (pNewClientRect->bottom - pNewClientRect->top !=
584 wndPtr->rectClient.bottom - wndPtr->rectClient.top) )
585 pWinpos->flags &= ~SWP_NOCLIENTSIZE;
587 else
589 if (!(pWinpos->flags & SWP_NOMOVE) &&
590 (pNewClientRect->left != wndPtr->rectClient.left ||
591 pNewClientRect->top != wndPtr->rectClient.top))
592 pWinpos->flags &= ~SWP_NOCLIENTMOVE;
594 WIN_ReleasePtr( wndPtr );
595 return wvrFlags;
599 /***********************************************************************
600 * SWP_DoOwnedPopups
602 * fix Z order taking into account owned popups -
603 * basically we need to maintain them above the window that owns them
605 * FIXME: hide/show owned popups when owner visibility changes.
607 static HWND SWP_DoOwnedPopups(HWND hwnd, HWND hwndInsertAfter)
609 HWND *list = NULL;
610 HWND owner = GetWindow( hwnd, GW_OWNER );
611 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
613 WARN("(%p) hInsertAfter = %p\n", hwnd, hwndInsertAfter );
615 if ((style & WS_POPUP) && owner)
617 /* make sure this popup stays above the owner */
619 HWND hwndLocalPrev = HWND_TOP;
621 if( hwndInsertAfter != HWND_TOP )
623 if ((list = WIN_ListChildren( GetDesktopWindow() )))
625 int i;
626 for (i = 0; list[i]; i++)
628 if (list[i] == owner) break;
629 if (list[i] != hwnd) hwndLocalPrev = list[i];
630 if (hwndLocalPrev == hwndInsertAfter) break;
632 hwndInsertAfter = hwndLocalPrev;
636 else if (style & WS_CHILD) return hwndInsertAfter;
638 if (!list) list = WIN_ListChildren( GetDesktopWindow() );
639 if (list)
641 int i;
642 for (i = 0; list[i]; i++)
644 if (list[i] == hwnd) break;
645 if ((GetWindowLongW( list[i], GWL_STYLE ) & WS_POPUP) &&
646 GetWindow( list[i], GW_OWNER ) == hwnd)
648 SetWindowPos( list[i], hwndInsertAfter, 0, 0, 0, 0,
649 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE |
650 SWP_NOSENDCHANGING | SWP_DEFERERASE );
651 hwndInsertAfter = list[i];
654 HeapFree( GetProcessHeap(), 0, list );
657 return hwndInsertAfter;
661 /* fix redundant flags and values in the WINDOWPOS structure */
662 static BOOL fixup_flags( WINDOWPOS *winpos )
664 WND *wndPtr = WIN_GetPtr( winpos->hwnd );
665 BOOL ret = TRUE;
667 if (!wndPtr || wndPtr == WND_OTHER_PROCESS)
669 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
670 return FALSE;
672 winpos->hwnd = wndPtr->hwndSelf; /* make it a full handle */
674 /* Finally make sure that all coordinates are valid */
675 if (winpos->x < -32768) winpos->x = -32768;
676 else if (winpos->x > 32767) winpos->x = 32767;
677 if (winpos->y < -32768) winpos->y = -32768;
678 else if (winpos->y > 32767) winpos->y = 32767;
680 if (winpos->cx < 0) winpos->cx = 0;
681 else if (winpos->cx > 32767) winpos->cx = 32767;
682 if (winpos->cy < 0) winpos->cy = 0;
683 else if (winpos->cy > 32767) winpos->cy = 32767;
685 if (wndPtr->dwStyle & WS_VISIBLE) winpos->flags &= ~SWP_SHOWWINDOW;
686 else
688 winpos->flags &= ~SWP_HIDEWINDOW;
689 if (!(winpos->flags & SWP_SHOWWINDOW)) winpos->flags |= SWP_NOREDRAW;
692 if ((wndPtr->rectWindow.right - wndPtr->rectWindow.left == winpos->cx) &&
693 (wndPtr->rectWindow.bottom - wndPtr->rectWindow.top == winpos->cy))
694 winpos->flags |= SWP_NOSIZE; /* Already the right size */
696 if ((wndPtr->rectWindow.left == winpos->x) && (wndPtr->rectWindow.top == winpos->y))
697 winpos->flags |= SWP_NOMOVE; /* Already the right position */
699 if (winpos->hwnd == GetActiveWindow())
700 winpos->flags |= SWP_NOACTIVATE; /* Already active */
701 else if ((wndPtr->dwStyle & (WS_POPUP | WS_CHILD)) != WS_CHILD)
703 if (!(winpos->flags & SWP_NOACTIVATE)) /* Bring to the top when activating */
705 winpos->flags &= ~SWP_NOZORDER;
706 winpos->hwndInsertAfter = HWND_TOP;
710 /* Check hwndInsertAfter */
711 if (winpos->flags & SWP_NOZORDER) goto done;
713 /* fix sign extension */
714 if (winpos->hwndInsertAfter == (HWND)0xffff) winpos->hwndInsertAfter = HWND_TOPMOST;
715 else if (winpos->hwndInsertAfter == (HWND)0xfffe) winpos->hwndInsertAfter = HWND_NOTOPMOST;
717 /* FIXME: TOPMOST not supported yet */
718 if ((winpos->hwndInsertAfter == HWND_TOPMOST) ||
719 (winpos->hwndInsertAfter == HWND_NOTOPMOST)) winpos->hwndInsertAfter = HWND_TOP;
721 /* hwndInsertAfter must be a sibling of the window */
722 if (winpos->hwndInsertAfter == HWND_TOP)
724 if (GetWindow(winpos->hwnd, GW_HWNDFIRST) == winpos->hwnd)
725 winpos->flags |= SWP_NOZORDER;
727 else if (winpos->hwndInsertAfter == HWND_BOTTOM)
729 if (GetWindow(winpos->hwnd, GW_HWNDLAST) == winpos->hwnd)
730 winpos->flags |= SWP_NOZORDER;
732 else
734 if (GetAncestor( winpos->hwndInsertAfter, GA_PARENT ) != wndPtr->parent) ret = FALSE;
735 else
737 /* don't need to change the Zorder of hwnd if it's already inserted
738 * after hwndInsertAfter or when inserting hwnd after itself.
740 if ((winpos->hwnd == winpos->hwndInsertAfter) ||
741 (winpos->hwnd == GetWindow( winpos->hwndInsertAfter, GW_HWNDNEXT )))
742 winpos->flags |= SWP_NOZORDER;
745 done:
746 WIN_ReleasePtr( wndPtr );
747 return ret;
751 /***********************************************************************
752 * set_visible_style
754 * Set/clear the WS_VISIBLE style of a window and map/unmap the X window.
756 static void set_visible_style( HWND hwnd, BOOL set )
758 WND *win;
760 if (!(win = WIN_GetPtr( hwnd ))) return;
761 if (win == WND_OTHER_PROCESS) return;
763 TRACE( "hwnd %p (%lx) set %d visible %d empty %d\n",
764 hwnd, get_whole_window(win),
765 set, (win->dwStyle & WS_VISIBLE) != 0, IsRectEmpty(&win->rectWindow) );
767 if (set)
769 if (win->dwStyle & WS_VISIBLE) goto done;
770 WIN_SetStyle( hwnd, win->dwStyle | WS_VISIBLE );
771 if (X11DRV_is_window_rect_mapped( &win->rectWindow ) &&
772 get_whole_window(win) && is_window_top_level(win))
774 Display *display = thread_display();
775 X11DRV_sync_window_style( display, win );
776 X11DRV_set_wm_hints( display, win );
777 TRACE( "mapping win %p\n", hwnd );
778 wine_tsx11_lock();
779 XMapWindow( display, get_whole_window(win) );
780 wine_tsx11_unlock();
783 else
785 if (!(win->dwStyle & WS_VISIBLE)) goto done;
786 WIN_SetStyle( hwnd, win->dwStyle & ~WS_VISIBLE );
787 if (X11DRV_is_window_rect_mapped( &win->rectWindow ) &&
788 get_whole_window(win) && is_window_top_level(win))
790 TRACE( "unmapping win %p\n", hwnd );
791 wine_tsx11_lock();
792 XUnmapWindow( thread_display(), get_whole_window(win) );
793 wine_tsx11_unlock();
796 done:
797 WIN_ReleasePtr( win );
801 /***********************************************************************
802 * SetWindowStyle (X11DRV.@)
804 * Update the X state of a window to reflect a style change
806 void X11DRV_SetWindowStyle( HWND hwnd, LONG oldStyle )
808 Display *display = thread_display();
809 WND *wndPtr;
810 LONG changed;
812 if (hwnd == GetDesktopWindow()) return;
813 if (!(wndPtr = WIN_GetPtr( hwnd ))) return;
814 if (wndPtr == WND_OTHER_PROCESS) return;
816 changed = wndPtr->dwStyle ^ oldStyle;
818 if (changed & WS_VISIBLE)
820 if (X11DRV_is_window_rect_mapped( &wndPtr->rectWindow ))
822 if (wndPtr->dwStyle & WS_VISIBLE)
824 TRACE( "mapping win %p\n", hwnd );
825 if (is_window_top_level(wndPtr))
827 X11DRV_sync_window_style( display, wndPtr );
828 X11DRV_set_wm_hints( display, wndPtr );
830 wine_tsx11_lock();
831 XMapWindow( display, get_whole_window(wndPtr) );
832 wine_tsx11_unlock();
834 else if (!is_window_top_level(wndPtr)) /* don't unmap managed windows */
836 TRACE( "unmapping win %p\n", hwnd );
837 wine_tsx11_lock();
838 XUnmapWindow( display, get_whole_window(wndPtr) );
839 wine_tsx11_unlock();
844 if (changed & WS_DISABLED)
846 if (wndPtr->dwExStyle & WS_EX_MANAGED)
848 XWMHints *wm_hints;
849 wine_tsx11_lock();
850 if (!(wm_hints = XGetWMHints( display, get_whole_window(wndPtr) )))
851 wm_hints = XAllocWMHints();
852 if (wm_hints)
854 wm_hints->flags |= InputHint;
855 wm_hints->input = !(wndPtr->dwStyle & WS_DISABLED);
856 XSetWMHints( display, get_whole_window(wndPtr), wm_hints );
857 XFree(wm_hints);
859 wine_tsx11_unlock();
862 WIN_ReleasePtr(wndPtr);
866 /***********************************************************************
867 * SetWindowPos (X11DRV.@)
869 BOOL X11DRV_SetWindowPos( WINDOWPOS *winpos )
871 WND *wndPtr;
872 RECT newWindowRect, newClientRect;
873 RECT oldWindowRect, oldClientRect;
874 UINT wvrFlags = 0;
875 BOOL bChangePos;
877 TRACE( "hwnd %p, after %p, swp %d,%d %dx%d flags %08x\n",
878 winpos->hwnd, winpos->hwndInsertAfter, winpos->x, winpos->y,
879 winpos->cx, winpos->cy, winpos->flags);
881 bChangePos = !(winpos->flags & SWP_WINE_NOHOSTMOVE);
882 winpos->flags &= ~SWP_WINE_NOHOSTMOVE;
884 /* Check window handle */
885 if (winpos->hwnd == GetDesktopWindow()) return FALSE;
887 /* First make sure that coordinates are valid for WM_WINDOWPOSCHANGING */
888 if (!(winpos->flags & SWP_NOMOVE))
890 if (winpos->x < -32768) winpos->x = -32768;
891 else if (winpos->x > 32767) winpos->x = 32767;
892 if (winpos->y < -32768) winpos->y = -32768;
893 else if (winpos->y > 32767) winpos->y = 32767;
895 if (!(winpos->flags & SWP_NOSIZE))
897 if (winpos->cx < 0) winpos->cx = 0;
898 else if (winpos->cx > 32767) winpos->cx = 32767;
899 if (winpos->cy < 0) winpos->cy = 0;
900 else if (winpos->cy > 32767) winpos->cy = 32767;
903 if (!SWP_DoWinPosChanging( winpos, &newWindowRect, &newClientRect )) return FALSE;
905 /* Fix redundant flags */
906 if (!fixup_flags( winpos )) return FALSE;
908 if (!(wndPtr = WIN_FindWndPtr( winpos->hwnd ))) return FALSE;
910 TRACE("\tcurrent (%ld,%ld)-(%ld,%ld), style %08x\n",
911 wndPtr->rectWindow.left, wndPtr->rectWindow.top,
912 wndPtr->rectWindow.right, wndPtr->rectWindow.bottom, (unsigned)wndPtr->dwStyle );
914 if((winpos->flags & (SWP_NOZORDER | SWP_HIDEWINDOW | SWP_SHOWWINDOW)) != SWP_NOZORDER)
916 if (GetAncestor( winpos->hwnd, GA_PARENT ) == GetDesktopWindow())
917 winpos->hwndInsertAfter = SWP_DoOwnedPopups( winpos->hwnd, winpos->hwndInsertAfter );
920 /* Common operations */
922 wvrFlags = SWP_DoNCCalcSize( winpos, &newWindowRect, &newClientRect );
924 if(!(winpos->flags & SWP_NOZORDER) && winpos->hwnd != winpos->hwndInsertAfter)
926 HWND parent = GetAncestor( winpos->hwnd, GA_PARENT );
927 if (parent) WIN_LinkWindow( winpos->hwnd, parent, winpos->hwndInsertAfter );
930 /* Reset active DCEs */
932 if( (((winpos->flags & SWP_AGG_NOPOSCHANGE) != SWP_AGG_NOPOSCHANGE) &&
933 wndPtr->dwStyle & WS_VISIBLE) ||
934 (winpos->flags & (SWP_HIDEWINDOW | SWP_SHOWWINDOW)) )
936 RECT rect;
938 UnionRect(&rect, &newWindowRect, &wndPtr->rectWindow);
939 DCE_InvalidateDCE(wndPtr->hwndSelf, &rect);
942 oldWindowRect = wndPtr->rectWindow;
943 oldClientRect = wndPtr->rectClient;
945 /* Find out if we have to redraw the whole client rect */
947 if( oldClientRect.bottom - oldClientRect.top ==
948 newClientRect.bottom - newClientRect.top ) wvrFlags &= ~WVR_VREDRAW;
950 if( oldClientRect.right - oldClientRect.left ==
951 newClientRect.right - newClientRect.left ) wvrFlags &= ~WVR_HREDRAW;
953 /* FIXME: actually do something with WVR_VALIDRECTS */
955 WIN_SetRectangles( winpos->hwnd, &newWindowRect, &newClientRect );
957 if (get_whole_window(wndPtr)) /* don't do anything if X window not created yet */
959 Display *display = thread_display();
961 if (!(winpos->flags & SWP_SHOWWINDOW) && (winpos->flags & SWP_HIDEWINDOW))
963 /* clear the update region */
964 RedrawWindow( winpos->hwnd, NULL, 0, RDW_VALIDATE | RDW_NOFRAME |
965 RDW_NOERASE | RDW_NOINTERNALPAINT | RDW_ALLCHILDREN );
966 set_visible_style( winpos->hwnd, FALSE );
968 else if ((wndPtr->dwStyle & WS_VISIBLE) && bChangePos &&
969 X11DRV_is_window_rect_mapped( &oldWindowRect ) &&
970 !X11DRV_is_window_rect_mapped( &newWindowRect ))
972 /* resizing to zero size or off screen -> unmap */
973 TRACE( "unmapping zero size or off-screen win %p\n", winpos->hwnd );
974 wine_tsx11_lock();
975 XUnmapWindow( display, get_whole_window(wndPtr) );
976 wine_tsx11_unlock();
979 if (bChangePos)
980 X11DRV_sync_whole_window_position( display, wndPtr, !(winpos->flags & SWP_NOZORDER) );
981 else
983 struct x11drv_win_data *data = wndPtr->pDriverData;
984 data->whole_rect = wndPtr->rectWindow;
985 X11DRV_window_to_X_rect( wndPtr, &data->whole_rect );
988 if (X11DRV_sync_client_window_position( display, wndPtr ) ||
989 (winpos->flags & SWP_FRAMECHANGED))
991 /* if we moved the client area, repaint the whole non-client window */
992 wine_tsx11_lock();
993 XClearArea( display, get_whole_window(wndPtr), 0, 0, 0, 0, True );
994 wine_tsx11_unlock();
995 winpos->flags |= SWP_FRAMECHANGED;
997 if (winpos->flags & SWP_SHOWWINDOW)
999 set_visible_style( winpos->hwnd, TRUE );
1001 else if ((wndPtr->dwStyle & WS_VISIBLE) && bChangePos &&
1002 !X11DRV_is_window_rect_mapped( &oldWindowRect ) &&
1003 X11DRV_is_window_rect_mapped( &newWindowRect ))
1005 /* resizing from zero size to non-zero -> map */
1006 TRACE( "mapping non zero size or off-screen win %p\n", winpos->hwnd );
1007 wine_tsx11_lock();
1008 XMapWindow( display, get_whole_window(wndPtr) );
1009 wine_tsx11_unlock();
1011 wine_tsx11_lock();
1012 XFlush( display ); /* FIXME: should not be necessary */
1013 wine_tsx11_unlock();
1015 else /* no X window, simply toggle the window style */
1017 if (winpos->flags & SWP_SHOWWINDOW)
1018 set_visible_style( winpos->hwnd, TRUE );
1019 else if (winpos->flags & SWP_HIDEWINDOW)
1020 set_visible_style( winpos->hwnd, FALSE );
1023 /* manually expose the areas that X won't expose because they are still covered by something */
1025 if (!(winpos->flags & SWP_SHOWWINDOW))
1026 expose_covered_parent_area( wndPtr, &oldWindowRect );
1028 if (wndPtr->dwStyle & WS_VISIBLE)
1029 expose_covered_window_area( wndPtr, &oldClientRect, winpos->flags & SWP_FRAMECHANGED );
1031 WIN_ReleaseWndPtr(wndPtr);
1033 if (wvrFlags & WVR_REDRAW) RedrawWindow( winpos->hwnd, NULL, 0, RDW_INVALIDATE | RDW_ERASE );
1035 if( winpos->flags & SWP_HIDEWINDOW )
1036 HideCaret(winpos->hwnd);
1037 else if (winpos->flags & SWP_SHOWWINDOW)
1038 ShowCaret(winpos->hwnd);
1040 if (!(winpos->flags & SWP_NOACTIVATE))
1042 /* child windows get WM_CHILDACTIVATE message */
1043 if ((GetWindowLongW( winpos->hwnd, GWL_STYLE ) & (WS_CHILD | WS_POPUP)) == WS_CHILD)
1044 SendMessageA( winpos->hwnd, WM_CHILDACTIVATE, 0, 0 );
1045 else
1046 SetForegroundWindow( winpos->hwnd );
1049 /* And last, send the WM_WINDOWPOSCHANGED message */
1051 TRACE("\tstatus flags = %04x\n", winpos->flags & SWP_AGG_STATUSFLAGS);
1053 if (((winpos->flags & SWP_AGG_STATUSFLAGS) != SWP_AGG_NOPOSCHANGE))
1055 /* WM_WINDOWPOSCHANGED is sent even if SWP_NOSENDCHANGING is set
1056 and always contains final window position.
1058 winpos->x = newWindowRect.left;
1059 winpos->y = newWindowRect.top;
1060 winpos->cx = newWindowRect.right - newWindowRect.left;
1061 winpos->cy = newWindowRect.bottom - newWindowRect.top;
1062 SendMessageW( winpos->hwnd, WM_WINDOWPOSCHANGED, 0, (LPARAM)winpos );
1065 return TRUE;
1069 /***********************************************************************
1070 * WINPOS_FindIconPos
1072 * Find a suitable place for an iconic window.
1074 static POINT WINPOS_FindIconPos( WND* wndPtr, POINT pt )
1076 RECT rectParent;
1077 HWND *list;
1078 short x, y, xspacing, yspacing;
1080 GetClientRect( wndPtr->parent, &rectParent );
1081 if ((pt.x >= rectParent.left) && (pt.x + GetSystemMetrics(SM_CXICON) < rectParent.right) &&
1082 (pt.y >= rectParent.top) && (pt.y + GetSystemMetrics(SM_CYICON) < rectParent.bottom))
1083 return pt; /* The icon already has a suitable position */
1085 xspacing = GetSystemMetrics(SM_CXICONSPACING);
1086 yspacing = GetSystemMetrics(SM_CYICONSPACING);
1088 list = WIN_ListChildren( wndPtr->parent );
1089 y = rectParent.bottom;
1090 for (;;)
1092 x = rectParent.left;
1095 /* Check if another icon already occupies this spot */
1096 /* FIXME: this is completely inefficient */
1097 if (list)
1099 int i;
1100 WND *childPtr;
1102 for (i = 0; list[i]; i++)
1104 if (list[i] == wndPtr->hwndSelf) continue;
1105 if (!IsIconic( list[i] )) continue;
1106 if (!(childPtr = WIN_FindWndPtr( list[i] ))) continue;
1107 if ((childPtr->rectWindow.left < x + xspacing) &&
1108 (childPtr->rectWindow.right >= x) &&
1109 (childPtr->rectWindow.top <= y) &&
1110 (childPtr->rectWindow.bottom > y - yspacing))
1112 WIN_ReleaseWndPtr( childPtr );
1113 break; /* There's a window in there */
1115 WIN_ReleaseWndPtr( childPtr );
1117 if (list[i])
1119 /* found something here, try next spot */
1120 x += xspacing;
1121 continue;
1125 /* No window was found, so it's OK for us */
1126 pt.x = x + (xspacing - GetSystemMetrics(SM_CXICON)) / 2;
1127 pt.y = y - (yspacing + GetSystemMetrics(SM_CYICON)) / 2;
1128 HeapFree( GetProcessHeap(), 0, list );
1129 return pt;
1131 } while(x <= rectParent.right-xspacing);
1132 y -= yspacing;
1140 UINT WINPOS_MinMaximize( HWND hwnd, UINT cmd, LPRECT rect )
1142 WND *wndPtr;
1143 UINT swpFlags = 0;
1144 POINT size;
1145 LONG old_style;
1146 WINDOWPLACEMENT wpl;
1148 TRACE("%p %u\n", hwnd, cmd );
1150 wpl.length = sizeof(wpl);
1151 GetWindowPlacement( hwnd, &wpl );
1153 if (HOOK_CallHooks( WH_CBT, HCBT_MINMAX, (WPARAM)hwnd, cmd, TRUE ))
1154 return SWP_NOSIZE | SWP_NOMOVE;
1156 if (IsIconic( hwnd ))
1158 if (cmd == SW_MINIMIZE) return SWP_NOSIZE | SWP_NOMOVE;
1159 if (!SendMessageA( hwnd, WM_QUERYOPEN, 0, 0 )) return SWP_NOSIZE | SWP_NOMOVE;
1160 swpFlags |= SWP_NOCOPYBITS;
1163 if (!(wndPtr = WIN_FindWndPtr( hwnd ))) return 0;
1165 size.x = wndPtr->rectWindow.left;
1166 size.y = wndPtr->rectWindow.top;
1168 switch( cmd )
1170 case SW_MINIMIZE:
1171 if( wndPtr->dwStyle & WS_MAXIMIZE) wndPtr->flags |= WIN_RESTORE_MAX;
1172 else wndPtr->flags &= ~WIN_RESTORE_MAX;
1174 WIN_SetStyle( hwnd, (wndPtr->dwStyle & ~WS_MAXIMIZE) | WS_MINIMIZE );
1176 X11DRV_set_iconic_state( wndPtr );
1178 wpl.ptMinPosition = WINPOS_FindIconPos( wndPtr, wpl.ptMinPosition );
1180 SetRect( rect, wpl.ptMinPosition.x, wpl.ptMinPosition.y,
1181 GetSystemMetrics(SM_CXICON), GetSystemMetrics(SM_CYICON) );
1182 swpFlags |= SWP_NOCOPYBITS;
1183 break;
1185 case SW_MAXIMIZE:
1186 WINPOS_GetMinMaxInfo( hwnd, &size, &wpl.ptMaxPosition, NULL, NULL );
1188 old_style = WIN_SetStyle( hwnd, (wndPtr->dwStyle & ~WS_MINIMIZE) | WS_MAXIMIZE );
1189 if (old_style & WS_MINIMIZE)
1191 WINPOS_ShowIconTitle( hwnd, FALSE );
1192 X11DRV_set_iconic_state( wndPtr );
1194 SetRect( rect, wpl.ptMaxPosition.x, wpl.ptMaxPosition.y, size.x, size.y );
1195 break;
1197 case SW_RESTORE:
1198 old_style = WIN_SetStyle( hwnd, wndPtr->dwStyle & ~(WS_MINIMIZE|WS_MAXIMIZE) );
1199 if (old_style & WS_MINIMIZE)
1201 WINPOS_ShowIconTitle( hwnd, FALSE );
1202 X11DRV_set_iconic_state( wndPtr );
1204 if( wndPtr->flags & WIN_RESTORE_MAX)
1206 /* Restore to maximized position */
1207 WINPOS_GetMinMaxInfo( hwnd, &size, &wpl.ptMaxPosition, NULL, NULL);
1208 WIN_SetStyle( hwnd, wndPtr->dwStyle | WS_MAXIMIZE );
1209 SetRect( rect, wpl.ptMaxPosition.x, wpl.ptMaxPosition.y, size.x, size.y );
1210 break;
1213 else if (!(old_style & WS_MAXIMIZE)) break;
1215 /* Restore to normal position */
1217 *rect = wpl.rcNormalPosition;
1218 rect->right -= rect->left;
1219 rect->bottom -= rect->top;
1221 break;
1224 WIN_ReleaseWndPtr( wndPtr );
1225 return swpFlags;
1229 /***********************************************************************
1230 * ShowWindow (X11DRV.@)
1232 BOOL X11DRV_ShowWindow( HWND hwnd, INT cmd )
1234 WND* wndPtr = WIN_FindWndPtr( hwnd );
1235 BOOL wasVisible, showFlag;
1236 RECT newPos = {0, 0, 0, 0};
1237 UINT swp = 0;
1239 if (!wndPtr) return FALSE;
1240 hwnd = wndPtr->hwndSelf; /* make it a full handle */
1242 wasVisible = (wndPtr->dwStyle & WS_VISIBLE) != 0;
1244 TRACE("hwnd=%p, cmd=%d, wasVisible %d\n", hwnd, cmd, wasVisible);
1246 switch(cmd)
1248 case SW_HIDE:
1249 if (!wasVisible) goto END;
1250 swp |= SWP_HIDEWINDOW | SWP_NOSIZE | SWP_NOMOVE |
1251 SWP_NOACTIVATE | SWP_NOZORDER;
1252 break;
1254 case SW_SHOWMINNOACTIVE:
1255 swp |= SWP_NOACTIVATE | SWP_NOZORDER;
1256 /* fall through */
1257 case SW_SHOWMINIMIZED:
1258 case SW_FORCEMINIMIZE: /* FIXME: Does not work if thread is hung. */
1259 swp |= SWP_SHOWWINDOW;
1260 /* fall through */
1261 case SW_MINIMIZE:
1262 swp |= SWP_FRAMECHANGED;
1263 if( !(wndPtr->dwStyle & WS_MINIMIZE) )
1264 swp |= WINPOS_MinMaximize( hwnd, SW_MINIMIZE, &newPos );
1265 else swp |= SWP_NOSIZE | SWP_NOMOVE;
1266 break;
1268 case SW_SHOWMAXIMIZED: /* same as SW_MAXIMIZE */
1269 swp |= SWP_SHOWWINDOW | SWP_FRAMECHANGED;
1270 if( !(wndPtr->dwStyle & WS_MAXIMIZE) )
1271 swp |= WINPOS_MinMaximize( hwnd, SW_MAXIMIZE, &newPos );
1272 else swp |= SWP_NOSIZE | SWP_NOMOVE;
1273 break;
1275 case SW_SHOWNA:
1276 swp |= SWP_NOACTIVATE | SWP_NOZORDER;
1277 /* fall through */
1278 case SW_SHOW:
1279 if (wasVisible) goto END;
1281 swp |= SWP_SHOWWINDOW | SWP_NOSIZE | SWP_NOMOVE;
1282 break;
1284 case SW_SHOWNOACTIVATE:
1285 swp |= SWP_NOACTIVATE | SWP_NOZORDER;
1286 /* fall through */
1287 case SW_SHOWNORMAL: /* same as SW_NORMAL: */
1288 case SW_SHOWDEFAULT: /* FIXME: should have its own handler */
1289 case SW_RESTORE:
1290 swp |= SWP_SHOWWINDOW | SWP_FRAMECHANGED;
1292 if( wndPtr->dwStyle & (WS_MINIMIZE | WS_MAXIMIZE) )
1293 swp |= WINPOS_MinMaximize( hwnd, SW_RESTORE, &newPos );
1294 else swp |= SWP_NOSIZE | SWP_NOMOVE;
1295 break;
1298 showFlag = (cmd != SW_HIDE);
1299 if (showFlag != wasVisible)
1301 SendMessageW( hwnd, WM_SHOWWINDOW, showFlag, 0 );
1302 if (!IsWindow( hwnd )) goto END;
1305 /* We can't activate a child window */
1306 if ((wndPtr->dwStyle & WS_CHILD) &&
1307 !(wndPtr->dwExStyle & WS_EX_MDICHILD))
1308 swp |= SWP_NOACTIVATE | SWP_NOZORDER;
1310 SetWindowPos( hwnd, HWND_TOP, newPos.left, newPos.top,
1311 newPos.right, newPos.bottom, LOWORD(swp) );
1312 if (cmd == SW_HIDE)
1314 HWND hFocus;
1316 /* FIXME: This will cause the window to be activated irrespective
1317 * of whether it is owned by the same thread. Has to be done
1318 * asynchronously.
1321 if (hwnd == GetActiveWindow())
1322 WINPOS_ActivateOtherWindow(hwnd);
1324 /* Revert focus to parent */
1325 hFocus = GetFocus();
1326 if (hwnd == hFocus || IsChild(hwnd, hFocus))
1328 HWND parent = GetAncestor(hwnd, GA_PARENT);
1329 if (parent == GetDesktopWindow()) parent = 0;
1330 SetFocus(parent);
1333 if (!IsWindow( hwnd )) goto END;
1334 else if( wndPtr->dwStyle & WS_MINIMIZE ) WINPOS_ShowIconTitle( hwnd, TRUE );
1336 if (wndPtr->flags & WIN_NEED_SIZE)
1338 /* should happen only in CreateWindowEx() */
1339 int wParam = SIZE_RESTORED;
1341 wndPtr->flags &= ~WIN_NEED_SIZE;
1342 if (wndPtr->dwStyle & WS_MAXIMIZE) wParam = SIZE_MAXIMIZED;
1343 else if (wndPtr->dwStyle & WS_MINIMIZE) wParam = SIZE_MINIMIZED;
1344 SendMessageA( hwnd, WM_SIZE, wParam,
1345 MAKELONG(wndPtr->rectClient.right-wndPtr->rectClient.left,
1346 wndPtr->rectClient.bottom-wndPtr->rectClient.top));
1347 SendMessageA( hwnd, WM_MOVE, 0,
1348 MAKELONG(wndPtr->rectClient.left, wndPtr->rectClient.top) );
1351 END:
1352 WIN_ReleaseWndPtr(wndPtr);
1353 return wasVisible;
1357 /**********************************************************************
1358 * X11DRV_MapNotify
1360 void X11DRV_MapNotify( HWND hwnd, XMapEvent *event )
1362 HWND hwndFocus = GetFocus();
1363 WND *win;
1365 if (!(win = WIN_GetPtr( hwnd ))) return;
1367 if ((win->dwStyle & WS_VISIBLE) &&
1368 (win->dwStyle & WS_MINIMIZE) &&
1369 (win->dwExStyle & WS_EX_MANAGED))
1371 int x, y;
1372 unsigned int width, height, border, depth;
1373 Window root, top;
1374 RECT rect;
1375 LONG style = (win->dwStyle & ~(WS_MINIMIZE|WS_MAXIMIZE)) | WS_VISIBLE;
1377 /* FIXME: hack */
1378 wine_tsx11_lock();
1379 XGetGeometry( event->display, get_whole_window(win), &root, &x, &y, &width, &height,
1380 &border, &depth );
1381 XTranslateCoordinates( event->display, get_whole_window(win), root, 0, 0, &x, &y, &top );
1382 wine_tsx11_unlock();
1383 rect.left = x;
1384 rect.top = y;
1385 rect.right = x + width;
1386 rect.bottom = y + height;
1387 X11DRV_X_to_window_rect( win, &rect );
1389 DCE_InvalidateDCE( hwnd, &win->rectWindow );
1391 if (win->flags & WIN_RESTORE_MAX) style |= WS_MAXIMIZE;
1392 WIN_SetStyle( hwnd, style );
1393 WIN_ReleasePtr( win );
1395 SendMessageA( hwnd, WM_SHOWWINDOW, SW_RESTORE, 0 );
1396 SetWindowPos( hwnd, 0, rect.left, rect.top, rect.right-rect.left, rect.bottom-rect.top,
1397 SWP_NOZORDER | SWP_WINE_NOHOSTMOVE );
1399 else WIN_ReleasePtr( win );
1400 if (hwndFocus && IsChild( hwnd, hwndFocus )) X11DRV_SetFocus(hwndFocus); /* FIXME */
1404 /**********************************************************************
1405 * X11DRV_UnmapNotify
1407 void X11DRV_UnmapNotify( HWND hwnd, XUnmapEvent *event )
1409 WND *win;
1411 if (!(win = WIN_GetPtr( hwnd ))) return;
1413 if ((win->dwStyle & WS_VISIBLE) && (win->dwExStyle & WS_EX_MANAGED) &&
1414 X11DRV_is_window_rect_mapped( &win->rectWindow ))
1416 if (win->dwStyle & WS_MAXIMIZE)
1417 win->flags |= WIN_RESTORE_MAX;
1418 else
1419 win->flags &= ~WIN_RESTORE_MAX;
1421 WIN_SetStyle( hwnd, (win->dwStyle & ~WS_MAXIMIZE) | WS_MINIMIZE );
1422 WIN_ReleasePtr( win );
1424 EndMenu();
1425 SendMessageA( hwnd, WM_SHOWWINDOW, SW_MINIMIZE, 0 );
1426 SetWindowPos( hwnd, 0, 0, 0, GetSystemMetrics(SM_CXICON), GetSystemMetrics(SM_CYICON),
1427 SWP_NOMOVE | SWP_NOACTIVATE | SWP_NOZORDER | SWP_WINE_NOHOSTMOVE );
1429 else WIN_ReleasePtr( win );
1433 /***********************************************************************
1434 * query_zorder
1436 * Synchronize internal z-order with the window manager's.
1438 static Window __get_common_ancestor( Display *display, Window A, Window B,
1439 Window** children, unsigned* total )
1441 /* find the real root window */
1443 Window root, *childrenB;
1444 unsigned totalB;
1446 wine_tsx11_lock();
1447 while( A != B && A && B )
1449 XQueryTree( display, A, &root, &A, children, total );
1450 XQueryTree( display, B, &root, &B, &childrenB, &totalB );
1451 if( childrenB ) XFree( childrenB );
1452 if( *children ) XFree( *children ), *children = NULL;
1455 if( A && B )
1457 XQueryTree( display, A, &root, &B, children, total );
1458 wine_tsx11_unlock();
1459 return A;
1461 wine_tsx11_unlock();
1462 return 0 ;
1465 static Window __get_top_decoration( Display *display, Window w, Window ancestor )
1467 Window* children, root, prev = w, parent = w;
1468 unsigned total;
1470 wine_tsx11_lock();
1473 w = parent;
1474 XQueryTree( display, w, &root, &parent, &children, &total );
1475 if( children ) XFree( children );
1476 } while( parent && parent != ancestor );
1477 wine_tsx11_unlock();
1478 TRACE("\t%08x -> %08x\n", (unsigned)prev, (unsigned)w );
1479 return ( parent ) ? w : 0 ;
1482 static unsigned __td_lookup( Window w, Window* list, unsigned max )
1484 unsigned i;
1485 for( i = max; i > 0; i-- ) if( list[i - 1] == w ) break;
1486 return i;
1489 static HWND query_zorder( Display *display, HWND hWndCheck)
1491 HWND hwndInsertAfter = HWND_TOP;
1492 Window w, parent, *children = NULL;
1493 unsigned total, check, pos, best;
1494 HWND *list = WIN_ListChildren( GetDesktopWindow() );
1495 HWND hwndA = 0, hwndB = 0;
1496 int i;
1498 /* find at least two managed windows */
1499 if (!list) return 0;
1500 for (i = 0; list[i]; i++)
1502 if (!(GetWindowLongW( list[i], GWL_EXSTYLE ) & WS_EX_MANAGED)) continue;
1503 if (!(GetWindowLongW( list[i], GWL_STYLE ) & WS_VISIBLE)) continue;
1504 if (!hwndA) hwndA = list[i];
1505 else
1507 hwndB = list[i];
1508 break;
1511 if (!hwndA || !hwndB) goto done;
1513 parent = __get_common_ancestor( display, X11DRV_get_whole_window(hwndA),
1514 X11DRV_get_whole_window(hwndB), &children, &total );
1515 if( parent && children )
1517 /* w is the ancestor if hWndCheck that is a direct descendant of 'parent' */
1519 w = __get_top_decoration( display, X11DRV_get_whole_window(hWndCheck), parent );
1521 if( w != children[total-1] ) /* check if at the top */
1523 /* X child at index 0 is at the bottom, at index total-1 is at the top */
1524 check = __td_lookup( w, children, total );
1525 best = total;
1527 /* go through all windows in Wine z-order... */
1528 for (i = 0; list[i]; i++)
1530 if (list[i] == hWndCheck) continue;
1531 if (!(GetWindowLongW( list[i], GWL_EXSTYLE ) & WS_EX_MANAGED)) continue;
1532 if (!(w = __get_top_decoration( display, X11DRV_get_whole_window(list[i]),
1533 parent ))) continue;
1534 pos = __td_lookup( w, children, total );
1535 if( pos < best && pos > check )
1537 /* find a nearest Wine window precedes hWndCheck in the real z-order */
1538 best = pos;
1539 hwndInsertAfter = list[i];
1541 if( best - check == 1 ) break;
1545 wine_tsx11_lock();
1546 if( children ) XFree( children );
1547 wine_tsx11_unlock();
1549 done:
1550 HeapFree( GetProcessHeap(), 0, list );
1551 return hwndInsertAfter;
1555 /***********************************************************************
1556 * X11DRV_handle_desktop_resize
1558 void X11DRV_handle_desktop_resize( unsigned int width, unsigned int height )
1560 RECT rect;
1561 HWND hwnd = GetDesktopWindow();
1563 screen_width = width;
1564 screen_height = height;
1565 TRACE("desktop %p change to (%dx%d)\n", hwnd, width, height);
1566 SetRect( &rect, 0, 0, width, height );
1567 WIN_SetRectangles( hwnd, &rect, &rect );
1568 SendMessageTimeoutW( HWND_BROADCAST, WM_DISPLAYCHANGE, screen_depth,
1569 MAKELPARAM( width, height ), SMTO_ABORTIFHUNG, 2000, NULL );
1573 /***********************************************************************
1574 * X11DRV_ConfigureNotify
1576 void X11DRV_ConfigureNotify( HWND hwnd, XConfigureEvent *event )
1578 HWND oldInsertAfter;
1579 struct x11drv_win_data *data;
1580 WND *win;
1581 RECT rect;
1582 WINDOWPOS winpos;
1583 int x = event->x, y = event->y;
1585 if (!(win = WIN_GetPtr( hwnd ))) return;
1586 data = win->pDriverData;
1588 /* Get geometry */
1590 if (!event->send_event) /* normal event, need to map coordinates to the root */
1592 Window child;
1593 wine_tsx11_lock();
1594 XTranslateCoordinates( event->display, data->whole_window, root_window,
1595 0, 0, &x, &y, &child );
1596 wine_tsx11_unlock();
1598 rect.left = x;
1599 rect.top = y;
1600 rect.right = x + event->width;
1601 rect.bottom = y + event->height;
1602 TRACE( "win %p new X rect %ld,%ld,%ldx%ld (event %d,%d,%dx%d)\n",
1603 hwnd, rect.left, rect.top, rect.right-rect.left, rect.bottom-rect.top,
1604 event->x, event->y, event->width, event->height );
1605 X11DRV_X_to_window_rect( win, &rect );
1606 WIN_ReleasePtr( win );
1608 winpos.hwnd = hwnd;
1609 winpos.x = rect.left;
1610 winpos.y = rect.top;
1611 winpos.cx = rect.right - rect.left;
1612 winpos.cy = rect.bottom - rect.top;
1613 winpos.flags = SWP_NOACTIVATE;
1615 /* Get Z-order (FIXME) */
1617 winpos.hwndInsertAfter = query_zorder( event->display, hwnd );
1619 /* needs to find the first Visible Window above the current one */
1620 oldInsertAfter = hwnd;
1621 for (;;)
1623 oldInsertAfter = GetWindow( oldInsertAfter, GW_HWNDPREV );
1624 if (!oldInsertAfter)
1626 oldInsertAfter = HWND_TOP;
1627 break;
1629 if (GetWindowLongA( oldInsertAfter, GWL_STYLE ) & WS_VISIBLE) break;
1632 /* Compare what has changed */
1634 GetWindowRect( hwnd, &rect );
1635 if (rect.left == winpos.x && rect.top == winpos.y) winpos.flags |= SWP_NOMOVE;
1636 else
1637 TRACE( "%p moving from (%ld,%ld) to (%d,%d)\n",
1638 hwnd, rect.left, rect.top, winpos.x, winpos.y );
1640 if ((rect.right - rect.left == winpos.cx && rect.bottom - rect.top == winpos.cy) ||
1641 IsIconic(hwnd) ||
1642 (IsRectEmpty( &rect ) && winpos.cx == 1 && winpos.cy == 1))
1643 winpos.flags |= SWP_NOSIZE;
1644 else
1645 TRACE( "%p resizing from (%ldx%ld) to (%dx%d)\n",
1646 hwnd, rect.right - rect.left, rect.bottom - rect.top,
1647 winpos.cx, winpos.cy );
1649 if (winpos.hwndInsertAfter == oldInsertAfter) winpos.flags |= SWP_NOZORDER;
1650 else
1651 TRACE( "%p restacking from after %p to after %p\n",
1652 hwnd, oldInsertAfter, winpos.hwndInsertAfter );
1654 /* if nothing changed, don't do anything */
1655 if (winpos.flags == (SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE)) return;
1657 SetWindowPos( hwnd, winpos.hwndInsertAfter, winpos.x, winpos.y,
1658 winpos.cx, winpos.cy, winpos.flags | SWP_WINE_NOHOSTMOVE );
1662 /***********************************************************************
1663 * SetWindowRgn (X11DRV.@)
1665 * Assign specified region to window (for non-rectangular windows)
1667 int X11DRV_SetWindowRgn( HWND hwnd, HRGN hrgn, BOOL redraw )
1669 WND *wndPtr;
1671 if ((wndPtr = WIN_GetPtr( hwnd )) == WND_OTHER_PROCESS)
1673 if (IsWindow( hwnd ))
1674 FIXME( "not supported on other process window %p\n", hwnd );
1675 wndPtr = NULL;
1677 if (!wndPtr)
1679 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
1680 return FALSE;
1683 #ifdef HAVE_LIBXSHAPE
1685 Display *display = thread_display();
1686 X11DRV_WND_DATA *data = wndPtr->pDriverData;
1688 if (data->whole_window)
1690 if (!hrgn)
1692 wine_tsx11_lock();
1693 XShapeCombineMask( display, data->whole_window,
1694 ShapeBounding, 0, 0, None, ShapeSet );
1695 wine_tsx11_unlock();
1697 else
1699 RGNDATA *pRegionData = X11DRV_GetRegionData( hrgn, 0 );
1700 if (pRegionData)
1702 wine_tsx11_lock();
1703 XShapeCombineRectangles( display, data->whole_window, ShapeBounding,
1704 wndPtr->rectWindow.left - data->whole_rect.left,
1705 wndPtr->rectWindow.top - data->whole_rect.top,
1706 (XRectangle *)pRegionData->Buffer,
1707 pRegionData->rdh.nCount,
1708 ShapeSet, YXBanded );
1709 wine_tsx11_unlock();
1710 HeapFree(GetProcessHeap(), 0, pRegionData);
1715 #endif /* HAVE_LIBXSHAPE */
1717 WIN_ReleasePtr( wndPtr );
1718 return TRUE;
1722 /***********************************************************************
1723 * draw_moving_frame
1725 * Draw the frame used when moving or resizing window.
1727 * FIXME: This causes problems in Win95 mode. (why?)
1729 static void draw_moving_frame( HDC hdc, RECT *rect, BOOL thickframe )
1731 if (thickframe)
1733 const int width = GetSystemMetrics(SM_CXFRAME);
1734 const int height = GetSystemMetrics(SM_CYFRAME);
1736 HBRUSH hbrush = SelectObject( hdc, GetStockObject( GRAY_BRUSH ) );
1737 PatBlt( hdc, rect->left, rect->top,
1738 rect->right - rect->left - width, height, PATINVERT );
1739 PatBlt( hdc, rect->left, rect->top + height, width,
1740 rect->bottom - rect->top - height, PATINVERT );
1741 PatBlt( hdc, rect->left + width, rect->bottom - 1,
1742 rect->right - rect->left - width, -height, PATINVERT );
1743 PatBlt( hdc, rect->right - 1, rect->top, -width,
1744 rect->bottom - rect->top - height, PATINVERT );
1745 SelectObject( hdc, hbrush );
1747 else DrawFocusRect( hdc, rect );
1751 /***********************************************************************
1752 * start_size_move
1754 * Initialisation of a move or resize, when initiatied from a menu choice.
1755 * Return hit test code for caption or sizing border.
1757 static LONG start_size_move( HWND hwnd, WPARAM wParam, POINT *capturePoint, LONG style )
1759 LONG hittest = 0;
1760 POINT pt;
1761 MSG msg;
1762 RECT rectWindow;
1764 GetWindowRect( hwnd, &rectWindow );
1766 if ((wParam & 0xfff0) == SC_MOVE)
1768 /* Move pointer at the center of the caption */
1769 RECT rect;
1770 NC_GetInsideRect( hwnd, &rect );
1771 if (style & WS_SYSMENU)
1772 rect.left += GetSystemMetrics(SM_CXSIZE) + 1;
1773 if (style & WS_MINIMIZEBOX)
1774 rect.right -= GetSystemMetrics(SM_CXSIZE) + 1;
1775 if (style & WS_MAXIMIZEBOX)
1776 rect.right -= GetSystemMetrics(SM_CXSIZE) + 1;
1777 pt.x = rectWindow.left + (rect.right - rect.left) / 2;
1778 pt.y = rectWindow.top + rect.top + GetSystemMetrics(SM_CYSIZE)/2;
1779 hittest = HTCAPTION;
1780 *capturePoint = pt;
1782 else /* SC_SIZE */
1784 while(!hittest)
1786 GetMessageW( &msg, 0, WM_KEYFIRST, WM_MOUSELAST );
1787 if (CallMsgFilterW( &msg, MSGF_SIZE )) continue;
1789 switch(msg.message)
1791 case WM_MOUSEMOVE:
1792 hittest = NC_HandleNCHitTest( hwnd, msg.pt );
1793 if ((hittest < HTLEFT) || (hittest > HTBOTTOMRIGHT))
1794 hittest = 0;
1795 break;
1797 case WM_LBUTTONUP:
1798 return 0;
1800 case WM_KEYDOWN:
1801 switch(msg.wParam)
1803 case VK_UP:
1804 hittest = HTTOP;
1805 pt.x =(rectWindow.left+rectWindow.right)/2;
1806 pt.y = rectWindow.top + GetSystemMetrics(SM_CYFRAME) / 2;
1807 break;
1808 case VK_DOWN:
1809 hittest = HTBOTTOM;
1810 pt.x =(rectWindow.left+rectWindow.right)/2;
1811 pt.y = rectWindow.bottom - GetSystemMetrics(SM_CYFRAME) / 2;
1812 break;
1813 case VK_LEFT:
1814 hittest = HTLEFT;
1815 pt.x = rectWindow.left + GetSystemMetrics(SM_CXFRAME) / 2;
1816 pt.y =(rectWindow.top+rectWindow.bottom)/2;
1817 break;
1818 case VK_RIGHT:
1819 hittest = HTRIGHT;
1820 pt.x = rectWindow.right - GetSystemMetrics(SM_CXFRAME) / 2;
1821 pt.y =(rectWindow.top+rectWindow.bottom)/2;
1822 break;
1823 case VK_RETURN:
1824 case VK_ESCAPE: return 0;
1828 *capturePoint = pt;
1830 SetCursorPos( pt.x, pt.y );
1831 NC_HandleSetCursor( hwnd, (WPARAM)hwnd, MAKELONG( hittest, WM_MOUSEMOVE ));
1832 return hittest;
1836 /***********************************************************************
1837 * set_movesize_capture
1839 static void set_movesize_capture( HWND hwnd )
1841 HWND previous = 0;
1843 SERVER_START_REQ( set_capture_window )
1845 req->handle = hwnd;
1846 req->flags = CAPTURE_MOVESIZE;
1847 if (!wine_server_call_err( req ))
1849 previous = reply->previous;
1850 hwnd = reply->full_handle;
1853 SERVER_END_REQ;
1854 if (previous && previous != hwnd)
1855 SendMessageW( previous, WM_CAPTURECHANGED, 0, (LPARAM)hwnd );
1858 /***********************************************************************
1859 * X11DRV_WMMoveResizeWindow
1861 * Tells the window manager to initiate a move or resize operation.
1863 * SEE
1864 * http://freedesktop.org/Standards/wm-spec/1.3/ar01s04.html
1865 * or search for "_NET_WM_MOVERESIZE"
1867 static void X11DRV_WMMoveResizeWindow( HWND hwnd, int x, int y, int dir )
1869 XEvent xev;
1870 Display *display = thread_display();
1872 wine_tsx11_lock();
1874 /* need to ungrab the pointer that may have been automatically grabbed
1875 * with a ButtonPress event */
1876 XUngrabPointer( display, CurrentTime );
1878 xev.xclient.type = ClientMessage;
1879 xev.xclient.window = X11DRV_get_whole_window(hwnd);
1880 xev.xclient.message_type = x11drv_atom(_NET_WM_MOVERESIZE);
1881 xev.xclient.serial = 0;
1882 xev.xclient.display = display;
1883 xev.xclient.send_event = True;
1884 xev.xclient.format = 32;
1885 xev.xclient.data.l[0] = x; /* x coord */
1886 xev.xclient.data.l[1] = y; /* y coord */
1887 xev.xclient.data.l[2] = dir; /* direction */
1888 xev.xclient.data.l[3] = 1; /* button */
1889 xev.xclient.data.l[4] = 0; /* unused */
1890 XSendEvent(display, root_window, False, SubstructureNotifyMask, &xev);
1892 wine_tsx11_unlock();
1895 /***********************************************************************
1896 * SysCommandSizeMove (X11DRV.@)
1898 * Perform SC_MOVE and SC_SIZE commands.
1900 void X11DRV_SysCommandSizeMove( HWND hwnd, WPARAM wParam )
1902 MSG msg;
1903 RECT sizingRect, mouseRect, origRect;
1904 HDC hdc;
1905 HWND parent;
1906 LONG hittest = (LONG)(wParam & 0x0f);
1907 WPARAM syscommand = wParam & 0xfff0;
1908 HCURSOR hDragCursor = 0, hOldCursor = 0;
1909 POINT minTrack, maxTrack;
1910 POINT capturePoint, pt;
1911 LONG style = GetWindowLongA( hwnd, GWL_STYLE );
1912 LONG exstyle = GetWindowLongA( hwnd, GWL_EXSTYLE );
1913 BOOL thickframe = HAS_THICKFRAME( style, exstyle );
1914 BOOL iconic = style & WS_MINIMIZE;
1915 BOOL moved = FALSE;
1916 DWORD dwPoint = GetMessagePos ();
1917 BOOL DragFullWindows = FALSE;
1918 BOOL grab;
1919 int iWndsLocks;
1920 Display *old_gdi_display = NULL;
1921 Display *display = thread_display();
1923 pt.x = (short)LOWORD(dwPoint);
1924 pt.y = (short)HIWORD(dwPoint);
1925 capturePoint = pt;
1927 if (IsZoomed(hwnd) || !IsWindowVisible(hwnd)) return;
1929 /* if we are managed then we let the WM do all the work */
1930 if (exstyle & WS_EX_MANAGED)
1932 int dir;
1933 if (syscommand == SC_MOVE)
1935 if (!hittest) dir = _NET_WM_MOVERESIZE_MOVE_KEYBOARD;
1936 else dir = _NET_WM_MOVERESIZE_MOVE;
1938 else if (!hittest) dir = _NET_WM_MOVERESIZE_SIZE_KEYBOARD;
1939 else
1940 switch (hittest)
1942 case WMSZ_LEFT: dir = _NET_WM_MOVERESIZE_SIZE_LEFT; break;
1943 case WMSZ_RIGHT: dir = _NET_WM_MOVERESIZE_SIZE_RIGHT; break;
1944 case WMSZ_TOP: dir = _NET_WM_MOVERESIZE_SIZE_TOP; break;
1945 case WMSZ_TOPLEFT: dir = _NET_WM_MOVERESIZE_SIZE_TOPLEFT; break;
1946 case WMSZ_TOPRIGHT: dir = _NET_WM_MOVERESIZE_SIZE_TOPRIGHT; break;
1947 case WMSZ_BOTTOM: dir = _NET_WM_MOVERESIZE_SIZE_BOTTOM; break;
1948 case WMSZ_BOTTOMLEFT: dir = _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT; break;
1949 case WMSZ_BOTTOMRIGHT: dir = _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT; break;
1950 default:
1951 ERR("Invalid hittest value: %ld\n", hittest);
1952 dir = _NET_WM_MOVERESIZE_SIZE_KEYBOARD;
1954 X11DRV_WMMoveResizeWindow( hwnd, pt.x, pt.y, dir );
1955 return;
1958 SystemParametersInfoA(SPI_GETDRAGFULLWINDOWS, 0, &DragFullWindows, 0);
1960 if (syscommand == SC_MOVE)
1962 if (!hittest) hittest = start_size_move( hwnd, wParam, &capturePoint, style );
1963 if (!hittest) return;
1965 else /* SC_SIZE */
1967 if ( hittest && (syscommand != SC_MOUSEMENU) )
1968 hittest += (HTLEFT - WMSZ_LEFT);
1969 else
1971 set_movesize_capture( hwnd );
1972 hittest = start_size_move( hwnd, wParam, &capturePoint, style );
1973 if (!hittest)
1975 set_movesize_capture(0);
1976 return;
1981 /* Get min/max info */
1983 WINPOS_GetMinMaxInfo( hwnd, NULL, NULL, &minTrack, &maxTrack );
1984 GetWindowRect( hwnd, &sizingRect );
1985 if (style & WS_CHILD)
1987 parent = GetParent(hwnd);
1988 /* make sizing rect relative to parent */
1989 MapWindowPoints( 0, parent, (POINT*)&sizingRect, 2 );
1990 GetClientRect( parent, &mouseRect );
1992 else
1994 parent = 0;
1995 SetRect(&mouseRect, 0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN));
1997 origRect = sizingRect;
1999 if (ON_LEFT_BORDER(hittest))
2001 mouseRect.left = max( mouseRect.left, sizingRect.right-maxTrack.x );
2002 mouseRect.right = min( mouseRect.right, sizingRect.right-minTrack.x );
2004 else if (ON_RIGHT_BORDER(hittest))
2006 mouseRect.left = max( mouseRect.left, sizingRect.left+minTrack.x );
2007 mouseRect.right = min( mouseRect.right, sizingRect.left+maxTrack.x );
2009 if (ON_TOP_BORDER(hittest))
2011 mouseRect.top = max( mouseRect.top, sizingRect.bottom-maxTrack.y );
2012 mouseRect.bottom = min( mouseRect.bottom,sizingRect.bottom-minTrack.y);
2014 else if (ON_BOTTOM_BORDER(hittest))
2016 mouseRect.top = max( mouseRect.top, sizingRect.top+minTrack.y );
2017 mouseRect.bottom = min( mouseRect.bottom, sizingRect.top+maxTrack.y );
2019 if (parent) MapWindowPoints( parent, 0, (LPPOINT)&mouseRect, 2 );
2021 /* Retrieve a default cache DC (without using the window style) */
2022 hdc = GetDCEx( parent, 0, DCX_CACHE );
2024 if( iconic ) /* create a cursor for dragging */
2026 hDragCursor = (HCURSOR)GetClassLongA( hwnd, GCL_HICON);
2027 if( !hDragCursor ) hDragCursor = (HCURSOR)SendMessageA( hwnd, WM_QUERYDRAGICON, 0, 0L);
2028 if( !hDragCursor ) iconic = FALSE;
2031 /* repaint the window before moving it around */
2032 RedrawWindow( hwnd, NULL, 0, RDW_UPDATENOW | RDW_ALLCHILDREN );
2034 SendMessageA( hwnd, WM_ENTERSIZEMOVE, 0, 0 );
2035 set_movesize_capture( hwnd );
2037 /* grab the server only when moving top-level windows without desktop */
2038 grab = (!DragFullWindows && !parent && (root_window == DefaultRootWindow(gdi_display)));
2040 wine_tsx11_lock();
2041 if (grab)
2043 XSync( gdi_display, False );
2044 XGrabServer( display );
2045 XSync( display, False );
2046 /* switch gdi display to the thread display, since the server is grabbed */
2047 old_gdi_display = gdi_display;
2048 gdi_display = display;
2050 XGrabPointer( display, X11DRV_get_whole_window(hwnd), False,
2051 PointerMotionMask | ButtonPressMask | ButtonReleaseMask,
2052 GrabModeAsync, GrabModeAsync,
2053 parent ? X11DRV_get_client_window(parent) : root_window,
2054 None, CurrentTime );
2055 wine_tsx11_unlock();
2057 while(1)
2059 int dx = 0, dy = 0;
2061 if (!GetMessageW( &msg, 0, WM_KEYFIRST, WM_MOUSELAST )) break;
2062 if (CallMsgFilterW( &msg, MSGF_SIZE )) continue;
2064 /* Exit on button-up, Return, or Esc */
2065 if ((msg.message == WM_LBUTTONUP) ||
2066 ((msg.message == WM_KEYDOWN) &&
2067 ((msg.wParam == VK_RETURN) || (msg.wParam == VK_ESCAPE)))) break;
2069 if ((msg.message != WM_KEYDOWN) && (msg.message != WM_MOUSEMOVE))
2070 continue; /* We are not interested in other messages */
2072 pt = msg.pt;
2074 if (msg.message == WM_KEYDOWN) switch(msg.wParam)
2076 case VK_UP: pt.y -= 8; break;
2077 case VK_DOWN: pt.y += 8; break;
2078 case VK_LEFT: pt.x -= 8; break;
2079 case VK_RIGHT: pt.x += 8; break;
2082 pt.x = max( pt.x, mouseRect.left );
2083 pt.x = min( pt.x, mouseRect.right );
2084 pt.y = max( pt.y, mouseRect.top );
2085 pt.y = min( pt.y, mouseRect.bottom );
2087 dx = pt.x - capturePoint.x;
2088 dy = pt.y - capturePoint.y;
2090 if (dx || dy)
2092 if( !moved )
2094 moved = TRUE;
2096 if( iconic ) /* ok, no system popup tracking */
2098 hOldCursor = SetCursor(hDragCursor);
2099 ShowCursor( TRUE );
2100 WINPOS_ShowIconTitle( hwnd, FALSE );
2102 else if(!DragFullWindows)
2103 draw_moving_frame( hdc, &sizingRect, thickframe );
2106 if (msg.message == WM_KEYDOWN) SetCursorPos( pt.x, pt.y );
2107 else
2109 RECT newRect = sizingRect;
2110 WPARAM wpSizingHit = 0;
2112 if (hittest == HTCAPTION) OffsetRect( &newRect, dx, dy );
2113 if (ON_LEFT_BORDER(hittest)) newRect.left += dx;
2114 else if (ON_RIGHT_BORDER(hittest)) newRect.right += dx;
2115 if (ON_TOP_BORDER(hittest)) newRect.top += dy;
2116 else if (ON_BOTTOM_BORDER(hittest)) newRect.bottom += dy;
2117 if(!iconic && !DragFullWindows) draw_moving_frame( hdc, &sizingRect, thickframe );
2118 capturePoint = pt;
2120 /* determine the hit location */
2121 if (hittest >= HTLEFT && hittest <= HTBOTTOMRIGHT)
2122 wpSizingHit = WMSZ_LEFT + (hittest - HTLEFT);
2123 SendMessageA( hwnd, WM_SIZING, wpSizingHit, (LPARAM)&newRect );
2125 if (!iconic)
2127 if(!DragFullWindows)
2128 draw_moving_frame( hdc, &newRect, thickframe );
2129 else {
2130 /* To avoid any deadlocks, all the locks on the windows
2131 structures must be suspended before the SetWindowPos */
2132 iWndsLocks = WIN_SuspendWndsLock();
2133 SetWindowPos( hwnd, 0, newRect.left, newRect.top,
2134 newRect.right - newRect.left,
2135 newRect.bottom - newRect.top,
2136 ( hittest == HTCAPTION ) ? SWP_NOSIZE : 0 );
2137 WIN_RestoreWndsLock(iWndsLocks);
2140 sizingRect = newRect;
2145 set_movesize_capture(0);
2146 if( iconic )
2148 if( moved ) /* restore cursors, show icon title later on */
2150 ShowCursor( FALSE );
2151 SetCursor( hOldCursor );
2154 else if (moved && !DragFullWindows)
2155 draw_moving_frame( hdc, &sizingRect, thickframe );
2157 ReleaseDC( parent, hdc );
2159 wine_tsx11_lock();
2160 XUngrabPointer( display, CurrentTime );
2161 if (grab)
2163 XSync( display, False );
2164 XUngrabServer( display );
2165 XSync( display, False );
2166 gdi_display = old_gdi_display;
2168 wine_tsx11_unlock();
2170 if (HOOK_CallHooks( WH_CBT, HCBT_MOVESIZE, (WPARAM)hwnd, (LPARAM)&sizingRect, TRUE ))
2171 moved = FALSE;
2173 SendMessageA( hwnd, WM_EXITSIZEMOVE, 0, 0 );
2174 SendMessageA( hwnd, WM_SETVISIBLE, !IsIconic(hwnd), 0L);
2176 /* window moved or resized */
2177 if (moved)
2179 /* To avoid any deadlocks, all the locks on the windows
2180 structures must be suspended before the SetWindowPos */
2181 iWndsLocks = WIN_SuspendWndsLock();
2183 /* if the moving/resizing isn't canceled call SetWindowPos
2184 * with the new position or the new size of the window
2186 if (!((msg.message == WM_KEYDOWN) && (msg.wParam == VK_ESCAPE)) )
2188 /* NOTE: SWP_NOACTIVATE prevents document window activation in Word 6 */
2189 if(!DragFullWindows)
2190 SetWindowPos( hwnd, 0, sizingRect.left, sizingRect.top,
2191 sizingRect.right - sizingRect.left,
2192 sizingRect.bottom - sizingRect.top,
2193 ( hittest == HTCAPTION ) ? SWP_NOSIZE : 0 );
2195 else
2196 { /* restore previous size/position */
2197 if(DragFullWindows)
2198 SetWindowPos( hwnd, 0, origRect.left, origRect.top,
2199 origRect.right - origRect.left,
2200 origRect.bottom - origRect.top,
2201 ( hittest == HTCAPTION ) ? SWP_NOSIZE : 0 );
2204 WIN_RestoreWndsLock(iWndsLocks);
2207 if (IsIconic(hwnd))
2209 /* Single click brings up the system menu when iconized */
2211 if( !moved )
2213 if(style & WS_SYSMENU )
2214 SendMessageA( hwnd, WM_SYSCOMMAND,
2215 SC_MOUSEMENU + HTSYSMENU, MAKELONG(pt.x,pt.y));
2217 else WINPOS_ShowIconTitle( hwnd, TRUE );
2222 /***********************************************************************
2223 * ForceWindowRaise (X11DRV.@)
2225 * Raise a window on top of the X stacking order, while preserving
2226 * the correct Windows Z order.
2228 * FIXME: this should go away.
2230 void X11DRV_ForceWindowRaise( HWND hwnd )
2232 int i = 0;
2233 HWND *list;
2234 XWindowChanges winChanges;
2235 Display *display = thread_display();
2236 WND *wndPtr = WIN_FindWndPtr( hwnd );
2238 if (!wndPtr) return;
2240 if ((wndPtr->dwExStyle & WS_EX_MANAGED) ||
2241 wndPtr->parent != GetDesktopWindow() ||
2242 IsRectEmpty( &wndPtr->rectWindow ) ||
2243 !get_whole_window(wndPtr))
2245 WIN_ReleaseWndPtr( wndPtr );
2246 return;
2248 WIN_ReleaseWndPtr( wndPtr );
2250 /* Raise all windows up to wndPtr according to their Z order.
2251 * (it would be easier with sibling-related Below but it doesn't
2252 * work very well with SGI mwm for instance)
2254 winChanges.stack_mode = Above;
2255 if (!(list = WIN_ListChildren( GetDesktopWindow() ))) return;
2256 while (list[i] && list[i] != hwnd) i++;
2257 if (list[i])
2259 for ( ; i >= 0; i--)
2261 WND *ptr = WIN_FindWndPtr( list[i] );
2262 if (!ptr) continue;
2263 if (!IsRectEmpty( &ptr->rectWindow ) && get_whole_window(ptr))
2265 wine_tsx11_lock();
2266 XReconfigureWMWindow( display, get_whole_window(ptr), 0, CWStackMode, &winChanges );
2267 wine_tsx11_unlock();
2269 WIN_ReleaseWndPtr( ptr );
2272 HeapFree( GetProcessHeap(), 0, list );