Get rid of the WIN_SetRectangles export from user32.
[wine/multimedia.git] / dlls / x11drv / winpos.c
blobaf18c176327f4614fc59122ca28f65688d2504ef
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 /* find the top most parent that doesn't clip siblings */
420 HWND clipping_parent = 0;
421 HWND *list = WIN_ListParents( hwnd );
422 if (list)
424 int i;
425 for (i = 0; list[i] != top; i++)
427 LONG style = GetWindowLongW( list[i], GWL_STYLE );
428 if (!(style & WS_CLIPSIBLINGS)) clipping_parent = list[i];
430 HeapFree( GetProcessHeap(), 0, list );
432 if (clipping_parent)
433 clipping_parent = GetAncestor( clipping_parent, GA_PARENT );
434 else if (!(flags & DCX_CLIPSIBLINGS) || (flags & DCX_WINDOW))
435 clipping_parent = GetAncestor( hwnd, GA_PARENT );
436 else
437 clipping_parent = hwnd;
439 escape.org.x = escape.org.y = 0;
440 escape.drawable_org.x = escape.drawable_org.y = 0;
441 if (flags & DCX_WINDOW)
443 escape.org.x = win->rectWindow.left - win->rectClient.left;
444 escape.org.y = win->rectWindow.top - win->rectClient.top;
446 MapWindowPoints( hwnd, clipping_parent, &escape.org, 1 );
447 MapWindowPoints( clipping_parent, 0, &escape.drawable_org, 1 );
448 escape.drawable = X11DRV_get_client_window( clipping_parent );
450 else
452 if (IsIconic( hwnd ))
454 escape.drawable = data->icon_window ? data->icon_window : data->whole_window;
455 escape.org.x = 0;
456 escape.org.y = 0;
457 escape.drawable_org = escape.org;
459 else if (flags & DCX_WINDOW)
461 escape.drawable = data->whole_window;
462 escape.drawable_org.x = data->whole_rect.left;
463 escape.drawable_org.y = data->whole_rect.top;
464 escape.org.x = win->rectWindow.left - data->whole_rect.left;
465 escape.org.y = win->rectWindow.top - data->whole_rect.top;
467 else
469 escape.drawable = data->client_window;
470 escape.drawable_org.x = win->rectClient.left;
471 escape.drawable_org.y = win->rectClient.top;
472 escape.org.x = 0;
473 escape.org.y = 0;
477 escape.code = X11DRV_SET_DRAWABLE;
478 ExtEscape( hdc, X11DRV_ESCAPE, sizeof(escape), (LPSTR)&escape, 0, NULL );
480 if (flags & (DCX_EXCLUDERGN | DCX_INTERSECTRGN) ||
481 SetHookFlags16( HDC_16(hdc), DCHF_VALIDATEVISRGN )) /* DC was dirty */
483 /* need to recompute the visible region */
484 HRGN visRgn = get_server_visible_region( hwnd, top, flags );
486 if (flags & (DCX_EXCLUDERGN | DCX_INTERSECTRGN))
487 CombineRgn( visRgn, visRgn, hrgn, (flags & DCX_INTERSECTRGN) ? RGN_AND : RGN_DIFF );
489 SelectVisRgn16( HDC_16(hdc), HRGN_16(visRgn) );
490 DeleteObject( visRgn );
493 WIN_ReleasePtr( win );
494 return TRUE;
498 /***********************************************************************
499 * ReleaseDC (X11DRV.@)
501 void X11DRV_ReleaseDC( HWND hwnd, HDC hdc )
503 struct x11drv_escape_set_drawable escape;
505 escape.code = X11DRV_SET_DRAWABLE;
506 escape.drawable = root_window;
507 escape.mode = IncludeInferiors;
508 escape.org.x = escape.org.y = 0;
509 escape.drawable_org.x = escape.drawable_org.y = 0;
511 ExtEscape( hdc, X11DRV_ESCAPE, sizeof(escape), (LPSTR)&escape, 0, NULL );
515 /***********************************************************************
516 * SWP_DoWinPosChanging
518 static BOOL SWP_DoWinPosChanging( WINDOWPOS* pWinpos, RECT* pNewWindowRect, RECT* pNewClientRect )
520 WND *wndPtr;
522 /* Send WM_WINDOWPOSCHANGING message */
524 if (!(pWinpos->flags & SWP_NOSENDCHANGING))
525 SendMessageW( pWinpos->hwnd, WM_WINDOWPOSCHANGING, 0, (LPARAM)pWinpos );
527 if (!(wndPtr = WIN_GetPtr( pWinpos->hwnd )) || wndPtr == WND_OTHER_PROCESS) return FALSE;
529 /* Calculate new position and size */
531 *pNewWindowRect = wndPtr->rectWindow;
532 *pNewClientRect = (wndPtr->dwStyle & WS_MINIMIZE) ? wndPtr->rectWindow
533 : wndPtr->rectClient;
535 if (!(pWinpos->flags & SWP_NOSIZE))
537 pNewWindowRect->right = pNewWindowRect->left + pWinpos->cx;
538 pNewWindowRect->bottom = pNewWindowRect->top + pWinpos->cy;
540 if (!(pWinpos->flags & SWP_NOMOVE))
542 pNewWindowRect->left = pWinpos->x;
543 pNewWindowRect->top = pWinpos->y;
544 pNewWindowRect->right += pWinpos->x - wndPtr->rectWindow.left;
545 pNewWindowRect->bottom += pWinpos->y - wndPtr->rectWindow.top;
547 OffsetRect( pNewClientRect, pWinpos->x - wndPtr->rectWindow.left,
548 pWinpos->y - wndPtr->rectWindow.top );
550 pWinpos->flags |= SWP_NOCLIENTMOVE | SWP_NOCLIENTSIZE;
551 WIN_ReleasePtr( wndPtr );
552 return TRUE;
555 /***********************************************************************
556 * SWP_DoNCCalcSize
558 static UINT SWP_DoNCCalcSize( WINDOWPOS* pWinpos, RECT* pNewWindowRect, RECT* pNewClientRect )
560 UINT wvrFlags = 0;
561 WND *wndPtr;
563 if (!(wndPtr = WIN_GetPtr( pWinpos->hwnd )) || wndPtr == WND_OTHER_PROCESS) return 0;
565 /* Send WM_NCCALCSIZE message to get new client area */
566 if( (pWinpos->flags & (SWP_FRAMECHANGED | SWP_NOSIZE)) != SWP_NOSIZE )
568 NCCALCSIZE_PARAMS params;
569 WINDOWPOS winposCopy;
571 params.rgrc[0] = *pNewWindowRect;
572 params.rgrc[1] = wndPtr->rectWindow;
573 params.rgrc[2] = wndPtr->rectClient;
574 params.lppos = &winposCopy;
575 winposCopy = *pWinpos;
576 WIN_ReleasePtr( wndPtr );
578 wvrFlags = SendMessageW( pWinpos->hwnd, WM_NCCALCSIZE, TRUE, (LPARAM)&params );
580 TRACE( "(%ld,%ld)-(%ld,%ld)\n", params.rgrc[0].left, params.rgrc[0].top,
581 params.rgrc[0].right, params.rgrc[0].bottom );
583 /* If the application send back garbage, ignore it */
584 if (params.rgrc[0].left <= params.rgrc[0].right &&
585 params.rgrc[0].top <= params.rgrc[0].bottom)
586 *pNewClientRect = params.rgrc[0];
588 /* FIXME: WVR_ALIGNxxx */
590 if (!(wndPtr = WIN_GetPtr( pWinpos->hwnd )) || wndPtr == WND_OTHER_PROCESS) return 0;
592 if( pNewClientRect->left != wndPtr->rectClient.left ||
593 pNewClientRect->top != wndPtr->rectClient.top )
594 pWinpos->flags &= ~SWP_NOCLIENTMOVE;
596 if( (pNewClientRect->right - pNewClientRect->left !=
597 wndPtr->rectClient.right - wndPtr->rectClient.left) ||
598 (pNewClientRect->bottom - pNewClientRect->top !=
599 wndPtr->rectClient.bottom - wndPtr->rectClient.top) )
600 pWinpos->flags &= ~SWP_NOCLIENTSIZE;
602 else
604 if (!(pWinpos->flags & SWP_NOMOVE) &&
605 (pNewClientRect->left != wndPtr->rectClient.left ||
606 pNewClientRect->top != wndPtr->rectClient.top))
607 pWinpos->flags &= ~SWP_NOCLIENTMOVE;
609 WIN_ReleasePtr( wndPtr );
610 return wvrFlags;
614 /***********************************************************************
615 * SWP_DoOwnedPopups
617 * fix Z order taking into account owned popups -
618 * basically we need to maintain them above the window that owns them
620 * FIXME: hide/show owned popups when owner visibility changes.
622 static HWND SWP_DoOwnedPopups(HWND hwnd, HWND hwndInsertAfter)
624 HWND *list = NULL;
625 HWND owner = GetWindow( hwnd, GW_OWNER );
626 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
628 WARN("(%p) hInsertAfter = %p\n", hwnd, hwndInsertAfter );
630 if ((style & WS_POPUP) && owner)
632 /* make sure this popup stays above the owner */
634 HWND hwndLocalPrev = HWND_TOP;
636 if( hwndInsertAfter != HWND_TOP )
638 if ((list = WIN_ListChildren( GetDesktopWindow() )))
640 int i;
641 for (i = 0; list[i]; i++)
643 if (list[i] == owner) break;
644 if (list[i] != hwnd) hwndLocalPrev = list[i];
645 if (hwndLocalPrev == hwndInsertAfter) break;
647 hwndInsertAfter = hwndLocalPrev;
651 else if (style & WS_CHILD) return hwndInsertAfter;
653 if (!list) list = WIN_ListChildren( GetDesktopWindow() );
654 if (list)
656 int i;
657 for (i = 0; list[i]; i++)
659 if (list[i] == hwnd) break;
660 if ((GetWindowLongW( list[i], GWL_STYLE ) & WS_POPUP) &&
661 GetWindow( list[i], GW_OWNER ) == hwnd)
663 SetWindowPos( list[i], hwndInsertAfter, 0, 0, 0, 0,
664 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE |
665 SWP_NOSENDCHANGING | SWP_DEFERERASE );
666 hwndInsertAfter = list[i];
669 HeapFree( GetProcessHeap(), 0, list );
672 return hwndInsertAfter;
676 /* fix redundant flags and values in the WINDOWPOS structure */
677 static BOOL fixup_flags( WINDOWPOS *winpos )
679 WND *wndPtr = WIN_GetPtr( winpos->hwnd );
680 BOOL ret = TRUE;
682 if (!wndPtr || wndPtr == WND_OTHER_PROCESS)
684 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
685 return FALSE;
687 winpos->hwnd = wndPtr->hwndSelf; /* make it a full handle */
689 /* Finally make sure that all coordinates are valid */
690 if (winpos->x < -32768) winpos->x = -32768;
691 else if (winpos->x > 32767) winpos->x = 32767;
692 if (winpos->y < -32768) winpos->y = -32768;
693 else if (winpos->y > 32767) winpos->y = 32767;
695 if (winpos->cx < 0) winpos->cx = 0;
696 else if (winpos->cx > 32767) winpos->cx = 32767;
697 if (winpos->cy < 0) winpos->cy = 0;
698 else if (winpos->cy > 32767) winpos->cy = 32767;
700 if (wndPtr->dwStyle & WS_VISIBLE) winpos->flags &= ~SWP_SHOWWINDOW;
701 else
703 winpos->flags &= ~SWP_HIDEWINDOW;
704 if (!(winpos->flags & SWP_SHOWWINDOW)) winpos->flags |= SWP_NOREDRAW;
707 if ((wndPtr->rectWindow.right - wndPtr->rectWindow.left == winpos->cx) &&
708 (wndPtr->rectWindow.bottom - wndPtr->rectWindow.top == winpos->cy))
709 winpos->flags |= SWP_NOSIZE; /* Already the right size */
711 if ((wndPtr->rectWindow.left == winpos->x) && (wndPtr->rectWindow.top == winpos->y))
712 winpos->flags |= SWP_NOMOVE; /* Already the right position */
714 if (winpos->hwnd == GetActiveWindow())
715 winpos->flags |= SWP_NOACTIVATE; /* Already active */
716 else if ((wndPtr->dwStyle & (WS_POPUP | WS_CHILD)) != WS_CHILD)
718 if (!(winpos->flags & SWP_NOACTIVATE)) /* Bring to the top when activating */
720 winpos->flags &= ~SWP_NOZORDER;
721 winpos->hwndInsertAfter = HWND_TOP;
725 /* Check hwndInsertAfter */
726 if (winpos->flags & SWP_NOZORDER) goto done;
728 /* fix sign extension */
729 if (winpos->hwndInsertAfter == (HWND)0xffff) winpos->hwndInsertAfter = HWND_TOPMOST;
730 else if (winpos->hwndInsertAfter == (HWND)0xfffe) winpos->hwndInsertAfter = HWND_NOTOPMOST;
732 /* FIXME: TOPMOST not supported yet */
733 if ((winpos->hwndInsertAfter == HWND_TOPMOST) ||
734 (winpos->hwndInsertAfter == HWND_NOTOPMOST)) winpos->hwndInsertAfter = HWND_TOP;
736 /* hwndInsertAfter must be a sibling of the window */
737 if (winpos->hwndInsertAfter == HWND_TOP)
739 if (GetWindow(winpos->hwnd, GW_HWNDFIRST) == winpos->hwnd)
740 winpos->flags |= SWP_NOZORDER;
742 else if (winpos->hwndInsertAfter == HWND_BOTTOM)
744 if (GetWindow(winpos->hwnd, GW_HWNDLAST) == winpos->hwnd)
745 winpos->flags |= SWP_NOZORDER;
747 else
749 if (GetAncestor( winpos->hwndInsertAfter, GA_PARENT ) != wndPtr->parent) ret = FALSE;
750 else
752 /* don't need to change the Zorder of hwnd if it's already inserted
753 * after hwndInsertAfter or when inserting hwnd after itself.
755 if ((winpos->hwnd == winpos->hwndInsertAfter) ||
756 (winpos->hwnd == GetWindow( winpos->hwndInsertAfter, GW_HWNDNEXT )))
757 winpos->flags |= SWP_NOZORDER;
760 done:
761 WIN_ReleasePtr( wndPtr );
762 return ret;
766 /***********************************************************************
767 * set_visible_style
769 * Set/clear the WS_VISIBLE style of a window and map/unmap the X window.
771 static void set_visible_style( HWND hwnd, BOOL set )
773 WND *win;
775 if (!(win = WIN_GetPtr( hwnd ))) return;
776 if (win == WND_OTHER_PROCESS) return;
778 TRACE( "hwnd %p (%lx) set %d visible %d empty %d\n",
779 hwnd, get_whole_window(win),
780 set, (win->dwStyle & WS_VISIBLE) != 0, IsRectEmpty(&win->rectWindow) );
782 if (set)
784 if (win->dwStyle & WS_VISIBLE) goto done;
785 WIN_SetStyle( hwnd, win->dwStyle | WS_VISIBLE );
786 if (X11DRV_is_window_rect_mapped( &win->rectWindow ) &&
787 get_whole_window(win) && is_window_top_level(win))
789 Display *display = thread_display();
790 X11DRV_sync_window_style( display, win );
791 X11DRV_set_wm_hints( display, win );
792 TRACE( "mapping win %p\n", hwnd );
793 wine_tsx11_lock();
794 XMapWindow( display, get_whole_window(win) );
795 wine_tsx11_unlock();
798 else
800 if (!(win->dwStyle & WS_VISIBLE)) goto done;
801 WIN_SetStyle( hwnd, win->dwStyle & ~WS_VISIBLE );
802 if (X11DRV_is_window_rect_mapped( &win->rectWindow ) &&
803 get_whole_window(win) && is_window_top_level(win))
805 TRACE( "unmapping win %p\n", hwnd );
806 wine_tsx11_lock();
807 XUnmapWindow( thread_display(), get_whole_window(win) );
808 wine_tsx11_unlock();
811 done:
812 WIN_ReleasePtr( win );
816 /***********************************************************************
817 * SetWindowStyle (X11DRV.@)
819 * Update the X state of a window to reflect a style change
821 void X11DRV_SetWindowStyle( HWND hwnd, LONG oldStyle )
823 Display *display = thread_display();
824 WND *wndPtr;
825 LONG changed;
827 if (hwnd == GetDesktopWindow()) return;
828 if (!(wndPtr = WIN_GetPtr( hwnd ))) return;
829 if (wndPtr == WND_OTHER_PROCESS) return;
831 changed = wndPtr->dwStyle ^ oldStyle;
833 if (changed & WS_VISIBLE)
835 if (X11DRV_is_window_rect_mapped( &wndPtr->rectWindow ))
837 if (wndPtr->dwStyle & WS_VISIBLE)
839 TRACE( "mapping win %p\n", hwnd );
840 if (is_window_top_level(wndPtr))
842 X11DRV_sync_window_style( display, wndPtr );
843 X11DRV_set_wm_hints( display, wndPtr );
845 wine_tsx11_lock();
846 XMapWindow( display, get_whole_window(wndPtr) );
847 wine_tsx11_unlock();
849 else if (!is_window_top_level(wndPtr)) /* don't unmap managed windows */
851 TRACE( "unmapping win %p\n", hwnd );
852 wine_tsx11_lock();
853 XUnmapWindow( display, get_whole_window(wndPtr) );
854 wine_tsx11_unlock();
859 if (changed & WS_DISABLED)
861 if (wndPtr->dwExStyle & WS_EX_MANAGED)
863 XWMHints *wm_hints;
864 wine_tsx11_lock();
865 if (!(wm_hints = XGetWMHints( display, get_whole_window(wndPtr) )))
866 wm_hints = XAllocWMHints();
867 if (wm_hints)
869 wm_hints->flags |= InputHint;
870 wm_hints->input = !(wndPtr->dwStyle & WS_DISABLED);
871 XSetWMHints( display, get_whole_window(wndPtr), wm_hints );
872 XFree(wm_hints);
874 wine_tsx11_unlock();
877 WIN_ReleasePtr(wndPtr);
881 /***********************************************************************
882 * SetWindowPos (X11DRV.@)
884 BOOL X11DRV_SetWindowPos( WINDOWPOS *winpos )
886 WND *wndPtr;
887 RECT newWindowRect, newClientRect;
888 RECT oldWindowRect, oldClientRect;
889 UINT wvrFlags = 0;
890 BOOL bChangePos;
892 TRACE( "hwnd %p, after %p, swp %d,%d %dx%d flags %08x\n",
893 winpos->hwnd, winpos->hwndInsertAfter, winpos->x, winpos->y,
894 winpos->cx, winpos->cy, winpos->flags);
896 bChangePos = !(winpos->flags & SWP_WINE_NOHOSTMOVE);
897 winpos->flags &= ~SWP_WINE_NOHOSTMOVE;
899 /* Check window handle */
900 if (winpos->hwnd == GetDesktopWindow()) return FALSE;
902 /* First make sure that coordinates are valid for WM_WINDOWPOSCHANGING */
903 if (!(winpos->flags & SWP_NOMOVE))
905 if (winpos->x < -32768) winpos->x = -32768;
906 else if (winpos->x > 32767) winpos->x = 32767;
907 if (winpos->y < -32768) winpos->y = -32768;
908 else if (winpos->y > 32767) winpos->y = 32767;
910 if (!(winpos->flags & SWP_NOSIZE))
912 if (winpos->cx < 0) winpos->cx = 0;
913 else if (winpos->cx > 32767) winpos->cx = 32767;
914 if (winpos->cy < 0) winpos->cy = 0;
915 else if (winpos->cy > 32767) winpos->cy = 32767;
918 if (!SWP_DoWinPosChanging( winpos, &newWindowRect, &newClientRect )) return FALSE;
920 /* Fix redundant flags */
921 if (!fixup_flags( winpos )) return FALSE;
923 if (!(wndPtr = WIN_FindWndPtr( winpos->hwnd ))) return FALSE;
925 TRACE("\tcurrent (%ld,%ld)-(%ld,%ld), style %08x\n",
926 wndPtr->rectWindow.left, wndPtr->rectWindow.top,
927 wndPtr->rectWindow.right, wndPtr->rectWindow.bottom, (unsigned)wndPtr->dwStyle );
929 if((winpos->flags & (SWP_NOZORDER | SWP_HIDEWINDOW | SWP_SHOWWINDOW)) != SWP_NOZORDER)
931 if (GetAncestor( winpos->hwnd, GA_PARENT ) == GetDesktopWindow())
932 winpos->hwndInsertAfter = SWP_DoOwnedPopups( winpos->hwnd, winpos->hwndInsertAfter );
935 /* Common operations */
937 wvrFlags = SWP_DoNCCalcSize( winpos, &newWindowRect, &newClientRect );
939 if(!(winpos->flags & SWP_NOZORDER) && winpos->hwnd != winpos->hwndInsertAfter)
941 HWND parent = GetAncestor( winpos->hwnd, GA_PARENT );
942 if (parent) WIN_LinkWindow( winpos->hwnd, parent, winpos->hwndInsertAfter );
945 /* Reset active DCEs */
947 if( (((winpos->flags & SWP_AGG_NOPOSCHANGE) != SWP_AGG_NOPOSCHANGE) &&
948 wndPtr->dwStyle & WS_VISIBLE) ||
949 (winpos->flags & (SWP_HIDEWINDOW | SWP_SHOWWINDOW)) )
951 RECT rect;
953 UnionRect(&rect, &newWindowRect, &wndPtr->rectWindow);
954 DCE_InvalidateDCE(wndPtr->hwndSelf, &rect);
957 oldWindowRect = wndPtr->rectWindow;
958 oldClientRect = wndPtr->rectClient;
960 /* Find out if we have to redraw the whole client rect */
962 if( oldClientRect.bottom - oldClientRect.top ==
963 newClientRect.bottom - newClientRect.top ) wvrFlags &= ~WVR_VREDRAW;
965 if( oldClientRect.right - oldClientRect.left ==
966 newClientRect.right - newClientRect.left ) wvrFlags &= ~WVR_HREDRAW;
968 /* FIXME: actually do something with WVR_VALIDRECTS */
970 X11DRV_set_window_rectangles( winpos->hwnd, &newWindowRect, &newClientRect );
972 if (get_whole_window(wndPtr)) /* don't do anything if X window not created yet */
974 Display *display = thread_display();
976 if (!(winpos->flags & SWP_SHOWWINDOW) && (winpos->flags & SWP_HIDEWINDOW))
978 /* clear the update region */
979 RedrawWindow( winpos->hwnd, NULL, 0, RDW_VALIDATE | RDW_NOFRAME |
980 RDW_NOERASE | RDW_NOINTERNALPAINT | RDW_ALLCHILDREN );
981 set_visible_style( winpos->hwnd, FALSE );
983 else if ((wndPtr->dwStyle & WS_VISIBLE) && bChangePos &&
984 X11DRV_is_window_rect_mapped( &oldWindowRect ) &&
985 !X11DRV_is_window_rect_mapped( &newWindowRect ))
987 /* resizing to zero size or off screen -> unmap */
988 TRACE( "unmapping zero size or off-screen win %p\n", winpos->hwnd );
989 wine_tsx11_lock();
990 XUnmapWindow( display, get_whole_window(wndPtr) );
991 wine_tsx11_unlock();
994 if (bChangePos)
995 X11DRV_sync_whole_window_position( display, wndPtr, !(winpos->flags & SWP_NOZORDER) );
996 else
998 struct x11drv_win_data *data = wndPtr->pDriverData;
999 data->whole_rect = wndPtr->rectWindow;
1000 X11DRV_window_to_X_rect( wndPtr, &data->whole_rect );
1003 if (X11DRV_sync_client_window_position( display, wndPtr ) ||
1004 (winpos->flags & SWP_FRAMECHANGED))
1006 /* if we moved the client area, repaint the whole non-client window */
1007 wine_tsx11_lock();
1008 XClearArea( display, get_whole_window(wndPtr), 0, 0, 0, 0, True );
1009 wine_tsx11_unlock();
1010 winpos->flags |= SWP_FRAMECHANGED;
1012 if (winpos->flags & SWP_SHOWWINDOW)
1014 set_visible_style( winpos->hwnd, TRUE );
1016 else if ((wndPtr->dwStyle & WS_VISIBLE) && bChangePos &&
1017 !X11DRV_is_window_rect_mapped( &oldWindowRect ) &&
1018 X11DRV_is_window_rect_mapped( &newWindowRect ))
1020 /* resizing from zero size to non-zero -> map */
1021 TRACE( "mapping non zero size or off-screen win %p\n", winpos->hwnd );
1022 wine_tsx11_lock();
1023 XMapWindow( display, get_whole_window(wndPtr) );
1024 wine_tsx11_unlock();
1026 wine_tsx11_lock();
1027 XFlush( display ); /* FIXME: should not be necessary */
1028 wine_tsx11_unlock();
1030 else /* no X window, simply toggle the window style */
1032 if (winpos->flags & SWP_SHOWWINDOW)
1033 set_visible_style( winpos->hwnd, TRUE );
1034 else if (winpos->flags & SWP_HIDEWINDOW)
1035 set_visible_style( winpos->hwnd, FALSE );
1038 /* manually expose the areas that X won't expose because they are still covered by something */
1040 if (!(winpos->flags & SWP_SHOWWINDOW))
1041 expose_covered_parent_area( wndPtr, &oldWindowRect );
1043 if (wndPtr->dwStyle & WS_VISIBLE)
1044 expose_covered_window_area( wndPtr, &oldClientRect, winpos->flags & SWP_FRAMECHANGED );
1046 WIN_ReleaseWndPtr(wndPtr);
1048 if (wvrFlags & WVR_REDRAW) RedrawWindow( winpos->hwnd, NULL, 0, RDW_INVALIDATE | RDW_ERASE );
1050 if( winpos->flags & SWP_HIDEWINDOW )
1051 HideCaret(winpos->hwnd);
1052 else if (winpos->flags & SWP_SHOWWINDOW)
1053 ShowCaret(winpos->hwnd);
1055 if (!(winpos->flags & SWP_NOACTIVATE))
1057 /* child windows get WM_CHILDACTIVATE message */
1058 if ((GetWindowLongW( winpos->hwnd, GWL_STYLE ) & (WS_CHILD | WS_POPUP)) == WS_CHILD)
1059 SendMessageA( winpos->hwnd, WM_CHILDACTIVATE, 0, 0 );
1060 else
1061 SetForegroundWindow( winpos->hwnd );
1064 /* And last, send the WM_WINDOWPOSCHANGED message */
1066 TRACE("\tstatus flags = %04x\n", winpos->flags & SWP_AGG_STATUSFLAGS);
1068 if (((winpos->flags & SWP_AGG_STATUSFLAGS) != SWP_AGG_NOPOSCHANGE))
1070 /* WM_WINDOWPOSCHANGED is sent even if SWP_NOSENDCHANGING is set
1071 and always contains final window position.
1073 winpos->x = newWindowRect.left;
1074 winpos->y = newWindowRect.top;
1075 winpos->cx = newWindowRect.right - newWindowRect.left;
1076 winpos->cy = newWindowRect.bottom - newWindowRect.top;
1077 SendMessageW( winpos->hwnd, WM_WINDOWPOSCHANGED, 0, (LPARAM)winpos );
1080 return TRUE;
1084 /***********************************************************************
1085 * WINPOS_FindIconPos
1087 * Find a suitable place for an iconic window.
1089 static POINT WINPOS_FindIconPos( WND* wndPtr, POINT pt )
1091 RECT rectParent;
1092 HWND *list;
1093 short x, y, xspacing, yspacing;
1095 GetClientRect( wndPtr->parent, &rectParent );
1096 if ((pt.x >= rectParent.left) && (pt.x + GetSystemMetrics(SM_CXICON) < rectParent.right) &&
1097 (pt.y >= rectParent.top) && (pt.y + GetSystemMetrics(SM_CYICON) < rectParent.bottom))
1098 return pt; /* The icon already has a suitable position */
1100 xspacing = GetSystemMetrics(SM_CXICONSPACING);
1101 yspacing = GetSystemMetrics(SM_CYICONSPACING);
1103 list = WIN_ListChildren( wndPtr->parent );
1104 y = rectParent.bottom;
1105 for (;;)
1107 x = rectParent.left;
1110 /* Check if another icon already occupies this spot */
1111 /* FIXME: this is completely inefficient */
1112 if (list)
1114 int i;
1115 WND *childPtr;
1117 for (i = 0; list[i]; i++)
1119 if (list[i] == wndPtr->hwndSelf) continue;
1120 if (!IsIconic( list[i] )) continue;
1121 if (!(childPtr = WIN_FindWndPtr( list[i] ))) continue;
1122 if ((childPtr->rectWindow.left < x + xspacing) &&
1123 (childPtr->rectWindow.right >= x) &&
1124 (childPtr->rectWindow.top <= y) &&
1125 (childPtr->rectWindow.bottom > y - yspacing))
1127 WIN_ReleaseWndPtr( childPtr );
1128 break; /* There's a window in there */
1130 WIN_ReleaseWndPtr( childPtr );
1132 if (list[i])
1134 /* found something here, try next spot */
1135 x += xspacing;
1136 continue;
1140 /* No window was found, so it's OK for us */
1141 pt.x = x + (xspacing - GetSystemMetrics(SM_CXICON)) / 2;
1142 pt.y = y - (yspacing + GetSystemMetrics(SM_CYICON)) / 2;
1143 HeapFree( GetProcessHeap(), 0, list );
1144 return pt;
1146 } while(x <= rectParent.right-xspacing);
1147 y -= yspacing;
1155 UINT WINPOS_MinMaximize( HWND hwnd, UINT cmd, LPRECT rect )
1157 WND *wndPtr;
1158 UINT swpFlags = 0;
1159 POINT size;
1160 LONG old_style;
1161 WINDOWPLACEMENT wpl;
1163 TRACE("%p %u\n", hwnd, cmd );
1165 wpl.length = sizeof(wpl);
1166 GetWindowPlacement( hwnd, &wpl );
1168 if (HOOK_CallHooks( WH_CBT, HCBT_MINMAX, (WPARAM)hwnd, cmd, TRUE ))
1169 return SWP_NOSIZE | SWP_NOMOVE;
1171 if (IsIconic( hwnd ))
1173 if (cmd == SW_MINIMIZE) return SWP_NOSIZE | SWP_NOMOVE;
1174 if (!SendMessageA( hwnd, WM_QUERYOPEN, 0, 0 )) return SWP_NOSIZE | SWP_NOMOVE;
1175 swpFlags |= SWP_NOCOPYBITS;
1178 if (!(wndPtr = WIN_FindWndPtr( hwnd ))) return 0;
1180 size.x = wndPtr->rectWindow.left;
1181 size.y = wndPtr->rectWindow.top;
1183 switch( cmd )
1185 case SW_MINIMIZE:
1186 if( wndPtr->dwStyle & WS_MAXIMIZE) wndPtr->flags |= WIN_RESTORE_MAX;
1187 else wndPtr->flags &= ~WIN_RESTORE_MAX;
1189 WIN_SetStyle( hwnd, (wndPtr->dwStyle & ~WS_MAXIMIZE) | WS_MINIMIZE );
1191 X11DRV_set_iconic_state( wndPtr );
1193 wpl.ptMinPosition = WINPOS_FindIconPos( wndPtr, wpl.ptMinPosition );
1195 SetRect( rect, wpl.ptMinPosition.x, wpl.ptMinPosition.y,
1196 GetSystemMetrics(SM_CXICON), GetSystemMetrics(SM_CYICON) );
1197 swpFlags |= SWP_NOCOPYBITS;
1198 break;
1200 case SW_MAXIMIZE:
1201 WINPOS_GetMinMaxInfo( hwnd, &size, &wpl.ptMaxPosition, NULL, NULL );
1203 old_style = WIN_SetStyle( hwnd, (wndPtr->dwStyle & ~WS_MINIMIZE) | WS_MAXIMIZE );
1204 if (old_style & WS_MINIMIZE)
1206 WINPOS_ShowIconTitle( hwnd, FALSE );
1207 X11DRV_set_iconic_state( wndPtr );
1209 SetRect( rect, wpl.ptMaxPosition.x, wpl.ptMaxPosition.y, size.x, size.y );
1210 break;
1212 case SW_RESTORE:
1213 old_style = WIN_SetStyle( hwnd, wndPtr->dwStyle & ~(WS_MINIMIZE|WS_MAXIMIZE) );
1214 if (old_style & WS_MINIMIZE)
1216 WINPOS_ShowIconTitle( hwnd, FALSE );
1217 X11DRV_set_iconic_state( wndPtr );
1219 if( wndPtr->flags & WIN_RESTORE_MAX)
1221 /* Restore to maximized position */
1222 WINPOS_GetMinMaxInfo( hwnd, &size, &wpl.ptMaxPosition, NULL, NULL);
1223 WIN_SetStyle( hwnd, wndPtr->dwStyle | WS_MAXIMIZE );
1224 SetRect( rect, wpl.ptMaxPosition.x, wpl.ptMaxPosition.y, size.x, size.y );
1225 break;
1228 else if (!(old_style & WS_MAXIMIZE)) break;
1230 /* Restore to normal position */
1232 *rect = wpl.rcNormalPosition;
1233 rect->right -= rect->left;
1234 rect->bottom -= rect->top;
1236 break;
1239 WIN_ReleaseWndPtr( wndPtr );
1240 return swpFlags;
1244 /***********************************************************************
1245 * ShowWindow (X11DRV.@)
1247 BOOL X11DRV_ShowWindow( HWND hwnd, INT cmd )
1249 WND* wndPtr = WIN_FindWndPtr( hwnd );
1250 BOOL wasVisible, showFlag;
1251 RECT newPos = {0, 0, 0, 0};
1252 UINT swp = 0;
1254 if (!wndPtr) return FALSE;
1255 hwnd = wndPtr->hwndSelf; /* make it a full handle */
1257 wasVisible = (wndPtr->dwStyle & WS_VISIBLE) != 0;
1259 TRACE("hwnd=%p, cmd=%d, wasVisible %d\n", hwnd, cmd, wasVisible);
1261 switch(cmd)
1263 case SW_HIDE:
1264 if (!wasVisible) goto END;
1265 swp |= SWP_HIDEWINDOW | SWP_NOSIZE | SWP_NOMOVE |
1266 SWP_NOACTIVATE | SWP_NOZORDER;
1267 break;
1269 case SW_SHOWMINNOACTIVE:
1270 swp |= SWP_NOACTIVATE | SWP_NOZORDER;
1271 /* fall through */
1272 case SW_SHOWMINIMIZED:
1273 case SW_FORCEMINIMIZE: /* FIXME: Does not work if thread is hung. */
1274 swp |= SWP_SHOWWINDOW;
1275 /* fall through */
1276 case SW_MINIMIZE:
1277 swp |= SWP_FRAMECHANGED;
1278 if( !(wndPtr->dwStyle & WS_MINIMIZE) )
1279 swp |= WINPOS_MinMaximize( hwnd, SW_MINIMIZE, &newPos );
1280 else swp |= SWP_NOSIZE | SWP_NOMOVE;
1281 break;
1283 case SW_SHOWMAXIMIZED: /* same as SW_MAXIMIZE */
1284 swp |= SWP_SHOWWINDOW | SWP_FRAMECHANGED;
1285 if( !(wndPtr->dwStyle & WS_MAXIMIZE) )
1286 swp |= WINPOS_MinMaximize( hwnd, SW_MAXIMIZE, &newPos );
1287 else swp |= SWP_NOSIZE | SWP_NOMOVE;
1288 break;
1290 case SW_SHOWNA:
1291 swp |= SWP_NOACTIVATE | SWP_NOZORDER;
1292 /* fall through */
1293 case SW_SHOW:
1294 if (wasVisible) goto END;
1296 swp |= SWP_SHOWWINDOW | SWP_NOSIZE | SWP_NOMOVE;
1297 break;
1299 case SW_SHOWNOACTIVATE:
1300 swp |= SWP_NOACTIVATE | SWP_NOZORDER;
1301 /* fall through */
1302 case SW_SHOWNORMAL: /* same as SW_NORMAL: */
1303 case SW_SHOWDEFAULT: /* FIXME: should have its own handler */
1304 case SW_RESTORE:
1305 swp |= SWP_SHOWWINDOW | SWP_FRAMECHANGED;
1307 if( wndPtr->dwStyle & (WS_MINIMIZE | WS_MAXIMIZE) )
1308 swp |= WINPOS_MinMaximize( hwnd, SW_RESTORE, &newPos );
1309 else swp |= SWP_NOSIZE | SWP_NOMOVE;
1310 break;
1313 showFlag = (cmd != SW_HIDE);
1314 if (showFlag != wasVisible)
1316 SendMessageW( hwnd, WM_SHOWWINDOW, showFlag, 0 );
1317 if (!IsWindow( hwnd )) goto END;
1320 /* We can't activate a child window */
1321 if ((wndPtr->dwStyle & WS_CHILD) &&
1322 !(wndPtr->dwExStyle & WS_EX_MDICHILD))
1323 swp |= SWP_NOACTIVATE | SWP_NOZORDER;
1325 SetWindowPos( hwnd, HWND_TOP, newPos.left, newPos.top,
1326 newPos.right, newPos.bottom, LOWORD(swp) );
1327 if (cmd == SW_HIDE)
1329 HWND hFocus;
1331 /* FIXME: This will cause the window to be activated irrespective
1332 * of whether it is owned by the same thread. Has to be done
1333 * asynchronously.
1336 if (hwnd == GetActiveWindow())
1337 WINPOS_ActivateOtherWindow(hwnd);
1339 /* Revert focus to parent */
1340 hFocus = GetFocus();
1341 if (hwnd == hFocus || IsChild(hwnd, hFocus))
1343 HWND parent = GetAncestor(hwnd, GA_PARENT);
1344 if (parent == GetDesktopWindow()) parent = 0;
1345 SetFocus(parent);
1348 if (!IsWindow( hwnd )) goto END;
1349 else if( wndPtr->dwStyle & WS_MINIMIZE ) WINPOS_ShowIconTitle( hwnd, TRUE );
1351 if (wndPtr->flags & WIN_NEED_SIZE)
1353 /* should happen only in CreateWindowEx() */
1354 int wParam = SIZE_RESTORED;
1356 wndPtr->flags &= ~WIN_NEED_SIZE;
1357 if (wndPtr->dwStyle & WS_MAXIMIZE) wParam = SIZE_MAXIMIZED;
1358 else if (wndPtr->dwStyle & WS_MINIMIZE) wParam = SIZE_MINIMIZED;
1359 SendMessageA( hwnd, WM_SIZE, wParam,
1360 MAKELONG(wndPtr->rectClient.right-wndPtr->rectClient.left,
1361 wndPtr->rectClient.bottom-wndPtr->rectClient.top));
1362 SendMessageA( hwnd, WM_MOVE, 0,
1363 MAKELONG(wndPtr->rectClient.left, wndPtr->rectClient.top) );
1366 END:
1367 WIN_ReleaseWndPtr(wndPtr);
1368 return wasVisible;
1372 /**********************************************************************
1373 * X11DRV_MapNotify
1375 void X11DRV_MapNotify( HWND hwnd, XMapEvent *event )
1377 HWND hwndFocus = GetFocus();
1378 WND *win;
1380 if (!(win = WIN_GetPtr( hwnd ))) return;
1382 if ((win->dwStyle & WS_VISIBLE) &&
1383 (win->dwStyle & WS_MINIMIZE) &&
1384 (win->dwExStyle & WS_EX_MANAGED))
1386 int x, y;
1387 unsigned int width, height, border, depth;
1388 Window root, top;
1389 RECT rect;
1390 LONG style = (win->dwStyle & ~(WS_MINIMIZE|WS_MAXIMIZE)) | WS_VISIBLE;
1392 /* FIXME: hack */
1393 wine_tsx11_lock();
1394 XGetGeometry( event->display, get_whole_window(win), &root, &x, &y, &width, &height,
1395 &border, &depth );
1396 XTranslateCoordinates( event->display, get_whole_window(win), root, 0, 0, &x, &y, &top );
1397 wine_tsx11_unlock();
1398 rect.left = x;
1399 rect.top = y;
1400 rect.right = x + width;
1401 rect.bottom = y + height;
1402 X11DRV_X_to_window_rect( win, &rect );
1404 DCE_InvalidateDCE( hwnd, &win->rectWindow );
1406 if (win->flags & WIN_RESTORE_MAX) style |= WS_MAXIMIZE;
1407 WIN_SetStyle( hwnd, style );
1408 WIN_ReleasePtr( win );
1410 SendMessageA( hwnd, WM_SHOWWINDOW, SW_RESTORE, 0 );
1411 SetWindowPos( hwnd, 0, rect.left, rect.top, rect.right-rect.left, rect.bottom-rect.top,
1412 SWP_NOZORDER | SWP_WINE_NOHOSTMOVE );
1414 else WIN_ReleasePtr( win );
1415 if (hwndFocus && IsChild( hwnd, hwndFocus )) X11DRV_SetFocus(hwndFocus); /* FIXME */
1419 /**********************************************************************
1420 * X11DRV_UnmapNotify
1422 void X11DRV_UnmapNotify( HWND hwnd, XUnmapEvent *event )
1424 WND *win;
1426 if (!(win = WIN_GetPtr( hwnd ))) return;
1428 if ((win->dwStyle & WS_VISIBLE) && (win->dwExStyle & WS_EX_MANAGED) &&
1429 X11DRV_is_window_rect_mapped( &win->rectWindow ))
1431 if (win->dwStyle & WS_MAXIMIZE)
1432 win->flags |= WIN_RESTORE_MAX;
1433 else
1434 win->flags &= ~WIN_RESTORE_MAX;
1436 WIN_SetStyle( hwnd, (win->dwStyle & ~WS_MAXIMIZE) | WS_MINIMIZE );
1437 WIN_ReleasePtr( win );
1439 EndMenu();
1440 SendMessageA( hwnd, WM_SHOWWINDOW, SW_MINIMIZE, 0 );
1441 SetWindowPos( hwnd, 0, 0, 0, GetSystemMetrics(SM_CXICON), GetSystemMetrics(SM_CYICON),
1442 SWP_NOMOVE | SWP_NOACTIVATE | SWP_NOZORDER | SWP_WINE_NOHOSTMOVE );
1444 else WIN_ReleasePtr( win );
1448 /***********************************************************************
1449 * query_zorder
1451 * Synchronize internal z-order with the window manager's.
1453 static Window __get_common_ancestor( Display *display, Window A, Window B,
1454 Window** children, unsigned* total )
1456 /* find the real root window */
1458 Window root, *childrenB;
1459 unsigned totalB;
1461 wine_tsx11_lock();
1462 while( A != B && A && B )
1464 XQueryTree( display, A, &root, &A, children, total );
1465 XQueryTree( display, B, &root, &B, &childrenB, &totalB );
1466 if( childrenB ) XFree( childrenB );
1467 if( *children ) XFree( *children ), *children = NULL;
1470 if( A && B )
1472 XQueryTree( display, A, &root, &B, children, total );
1473 wine_tsx11_unlock();
1474 return A;
1476 wine_tsx11_unlock();
1477 return 0 ;
1480 static Window __get_top_decoration( Display *display, Window w, Window ancestor )
1482 Window* children, root, prev = w, parent = w;
1483 unsigned total;
1485 wine_tsx11_lock();
1488 w = parent;
1489 XQueryTree( display, w, &root, &parent, &children, &total );
1490 if( children ) XFree( children );
1491 } while( parent && parent != ancestor );
1492 wine_tsx11_unlock();
1493 TRACE("\t%08x -> %08x\n", (unsigned)prev, (unsigned)w );
1494 return ( parent ) ? w : 0 ;
1497 static unsigned __td_lookup( Window w, Window* list, unsigned max )
1499 unsigned i;
1500 for( i = max; i > 0; i-- ) if( list[i - 1] == w ) break;
1501 return i;
1504 static HWND query_zorder( Display *display, HWND hWndCheck)
1506 HWND hwndInsertAfter = HWND_TOP;
1507 Window w, parent, *children = NULL;
1508 unsigned total, check, pos, best;
1509 HWND *list = WIN_ListChildren( GetDesktopWindow() );
1510 HWND hwndA = 0, hwndB = 0;
1511 int i;
1513 /* find at least two managed windows */
1514 if (!list) return 0;
1515 for (i = 0; list[i]; i++)
1517 if (!(GetWindowLongW( list[i], GWL_EXSTYLE ) & WS_EX_MANAGED)) continue;
1518 if (!(GetWindowLongW( list[i], GWL_STYLE ) & WS_VISIBLE)) continue;
1519 if (!hwndA) hwndA = list[i];
1520 else
1522 hwndB = list[i];
1523 break;
1526 if (!hwndA || !hwndB) goto done;
1528 parent = __get_common_ancestor( display, X11DRV_get_whole_window(hwndA),
1529 X11DRV_get_whole_window(hwndB), &children, &total );
1530 if( parent && children )
1532 /* w is the ancestor if hWndCheck that is a direct descendant of 'parent' */
1534 w = __get_top_decoration( display, X11DRV_get_whole_window(hWndCheck), parent );
1536 if( w != children[total-1] ) /* check if at the top */
1538 /* X child at index 0 is at the bottom, at index total-1 is at the top */
1539 check = __td_lookup( w, children, total );
1540 best = total;
1542 /* go through all windows in Wine z-order... */
1543 for (i = 0; list[i]; i++)
1545 if (list[i] == hWndCheck) continue;
1546 if (!(GetWindowLongW( list[i], GWL_EXSTYLE ) & WS_EX_MANAGED)) continue;
1547 if (!(w = __get_top_decoration( display, X11DRV_get_whole_window(list[i]),
1548 parent ))) continue;
1549 pos = __td_lookup( w, children, total );
1550 if( pos < best && pos > check )
1552 /* find a nearest Wine window precedes hWndCheck in the real z-order */
1553 best = pos;
1554 hwndInsertAfter = list[i];
1556 if( best - check == 1 ) break;
1560 wine_tsx11_lock();
1561 if( children ) XFree( children );
1562 wine_tsx11_unlock();
1564 done:
1565 HeapFree( GetProcessHeap(), 0, list );
1566 return hwndInsertAfter;
1570 /***********************************************************************
1571 * X11DRV_handle_desktop_resize
1573 void X11DRV_handle_desktop_resize( unsigned int width, unsigned int height )
1575 RECT rect;
1576 HWND hwnd = GetDesktopWindow();
1578 screen_width = width;
1579 screen_height = height;
1580 TRACE("desktop %p change to (%dx%d)\n", hwnd, width, height);
1581 SetRect( &rect, 0, 0, width, height );
1582 X11DRV_set_window_rectangles( hwnd, &rect, &rect );
1583 SendMessageTimeoutW( HWND_BROADCAST, WM_DISPLAYCHANGE, screen_depth,
1584 MAKELPARAM( width, height ), SMTO_ABORTIFHUNG, 2000, NULL );
1588 /***********************************************************************
1589 * X11DRV_ConfigureNotify
1591 void X11DRV_ConfigureNotify( HWND hwnd, XConfigureEvent *event )
1593 HWND oldInsertAfter;
1594 struct x11drv_win_data *data;
1595 WND *win;
1596 RECT rect;
1597 WINDOWPOS winpos;
1598 int x = event->x, y = event->y;
1600 if (!(win = WIN_GetPtr( hwnd ))) return;
1601 data = win->pDriverData;
1603 /* Get geometry */
1605 if (!event->send_event) /* normal event, need to map coordinates to the root */
1607 Window child;
1608 wine_tsx11_lock();
1609 XTranslateCoordinates( event->display, data->whole_window, root_window,
1610 0, 0, &x, &y, &child );
1611 wine_tsx11_unlock();
1613 rect.left = x;
1614 rect.top = y;
1615 rect.right = x + event->width;
1616 rect.bottom = y + event->height;
1617 TRACE( "win %p new X rect %ld,%ld,%ldx%ld (event %d,%d,%dx%d)\n",
1618 hwnd, rect.left, rect.top, rect.right-rect.left, rect.bottom-rect.top,
1619 event->x, event->y, event->width, event->height );
1620 X11DRV_X_to_window_rect( win, &rect );
1621 WIN_ReleasePtr( win );
1623 winpos.hwnd = hwnd;
1624 winpos.x = rect.left;
1625 winpos.y = rect.top;
1626 winpos.cx = rect.right - rect.left;
1627 winpos.cy = rect.bottom - rect.top;
1628 winpos.flags = SWP_NOACTIVATE;
1630 /* Get Z-order (FIXME) */
1632 winpos.hwndInsertAfter = query_zorder( event->display, hwnd );
1634 /* needs to find the first Visible Window above the current one */
1635 oldInsertAfter = hwnd;
1636 for (;;)
1638 oldInsertAfter = GetWindow( oldInsertAfter, GW_HWNDPREV );
1639 if (!oldInsertAfter)
1641 oldInsertAfter = HWND_TOP;
1642 break;
1644 if (GetWindowLongA( oldInsertAfter, GWL_STYLE ) & WS_VISIBLE) break;
1647 /* Compare what has changed */
1649 GetWindowRect( hwnd, &rect );
1650 if (rect.left == winpos.x && rect.top == winpos.y) winpos.flags |= SWP_NOMOVE;
1651 else
1652 TRACE( "%p moving from (%ld,%ld) to (%d,%d)\n",
1653 hwnd, rect.left, rect.top, winpos.x, winpos.y );
1655 if ((rect.right - rect.left == winpos.cx && rect.bottom - rect.top == winpos.cy) ||
1656 IsIconic(hwnd) ||
1657 (IsRectEmpty( &rect ) && winpos.cx == 1 && winpos.cy == 1))
1658 winpos.flags |= SWP_NOSIZE;
1659 else
1660 TRACE( "%p resizing from (%ldx%ld) to (%dx%d)\n",
1661 hwnd, rect.right - rect.left, rect.bottom - rect.top,
1662 winpos.cx, winpos.cy );
1664 if (winpos.hwndInsertAfter == oldInsertAfter) winpos.flags |= SWP_NOZORDER;
1665 else
1666 TRACE( "%p restacking from after %p to after %p\n",
1667 hwnd, oldInsertAfter, winpos.hwndInsertAfter );
1669 /* if nothing changed, don't do anything */
1670 if (winpos.flags == (SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE)) return;
1672 SetWindowPos( hwnd, winpos.hwndInsertAfter, winpos.x, winpos.y,
1673 winpos.cx, winpos.cy, winpos.flags | SWP_WINE_NOHOSTMOVE );
1677 /***********************************************************************
1678 * SetWindowRgn (X11DRV.@)
1680 * Assign specified region to window (for non-rectangular windows)
1682 int X11DRV_SetWindowRgn( HWND hwnd, HRGN hrgn, BOOL redraw )
1684 WND *wndPtr;
1686 if ((wndPtr = WIN_GetPtr( hwnd )) == WND_OTHER_PROCESS)
1688 if (IsWindow( hwnd ))
1689 FIXME( "not supported on other process window %p\n", hwnd );
1690 wndPtr = NULL;
1692 if (!wndPtr)
1694 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
1695 return FALSE;
1698 #ifdef HAVE_LIBXSHAPE
1700 Display *display = thread_display();
1701 X11DRV_WND_DATA *data = wndPtr->pDriverData;
1703 if (data->whole_window)
1705 if (!hrgn)
1707 wine_tsx11_lock();
1708 XShapeCombineMask( display, data->whole_window,
1709 ShapeBounding, 0, 0, None, ShapeSet );
1710 wine_tsx11_unlock();
1712 else
1714 RGNDATA *pRegionData = X11DRV_GetRegionData( hrgn, 0 );
1715 if (pRegionData)
1717 wine_tsx11_lock();
1718 XShapeCombineRectangles( display, data->whole_window, ShapeBounding,
1719 wndPtr->rectWindow.left - data->whole_rect.left,
1720 wndPtr->rectWindow.top - data->whole_rect.top,
1721 (XRectangle *)pRegionData->Buffer,
1722 pRegionData->rdh.nCount,
1723 ShapeSet, YXBanded );
1724 wine_tsx11_unlock();
1725 HeapFree(GetProcessHeap(), 0, pRegionData);
1730 #endif /* HAVE_LIBXSHAPE */
1732 WIN_ReleasePtr( wndPtr );
1733 return TRUE;
1737 /***********************************************************************
1738 * draw_moving_frame
1740 * Draw the frame used when moving or resizing window.
1742 * FIXME: This causes problems in Win95 mode. (why?)
1744 static void draw_moving_frame( HDC hdc, RECT *rect, BOOL thickframe )
1746 if (thickframe)
1748 const int width = GetSystemMetrics(SM_CXFRAME);
1749 const int height = GetSystemMetrics(SM_CYFRAME);
1751 HBRUSH hbrush = SelectObject( hdc, GetStockObject( GRAY_BRUSH ) );
1752 PatBlt( hdc, rect->left, rect->top,
1753 rect->right - rect->left - width, height, PATINVERT );
1754 PatBlt( hdc, rect->left, rect->top + height, width,
1755 rect->bottom - rect->top - height, PATINVERT );
1756 PatBlt( hdc, rect->left + width, rect->bottom - 1,
1757 rect->right - rect->left - width, -height, PATINVERT );
1758 PatBlt( hdc, rect->right - 1, rect->top, -width,
1759 rect->bottom - rect->top - height, PATINVERT );
1760 SelectObject( hdc, hbrush );
1762 else DrawFocusRect( hdc, rect );
1766 /***********************************************************************
1767 * start_size_move
1769 * Initialisation of a move or resize, when initiatied from a menu choice.
1770 * Return hit test code for caption or sizing border.
1772 static LONG start_size_move( HWND hwnd, WPARAM wParam, POINT *capturePoint, LONG style )
1774 LONG hittest = 0;
1775 POINT pt;
1776 MSG msg;
1777 RECT rectWindow;
1779 GetWindowRect( hwnd, &rectWindow );
1781 if ((wParam & 0xfff0) == SC_MOVE)
1783 /* Move pointer at the center of the caption */
1784 RECT rect;
1785 NC_GetInsideRect( hwnd, &rect );
1786 if (style & WS_SYSMENU)
1787 rect.left += GetSystemMetrics(SM_CXSIZE) + 1;
1788 if (style & WS_MINIMIZEBOX)
1789 rect.right -= GetSystemMetrics(SM_CXSIZE) + 1;
1790 if (style & WS_MAXIMIZEBOX)
1791 rect.right -= GetSystemMetrics(SM_CXSIZE) + 1;
1792 pt.x = rectWindow.left + (rect.right - rect.left) / 2;
1793 pt.y = rectWindow.top + rect.top + GetSystemMetrics(SM_CYSIZE)/2;
1794 hittest = HTCAPTION;
1795 *capturePoint = pt;
1797 else /* SC_SIZE */
1799 while(!hittest)
1801 GetMessageW( &msg, 0, WM_KEYFIRST, WM_MOUSELAST );
1802 if (CallMsgFilterW( &msg, MSGF_SIZE )) continue;
1804 switch(msg.message)
1806 case WM_MOUSEMOVE:
1807 hittest = NC_HandleNCHitTest( hwnd, msg.pt );
1808 if ((hittest < HTLEFT) || (hittest > HTBOTTOMRIGHT))
1809 hittest = 0;
1810 break;
1812 case WM_LBUTTONUP:
1813 return 0;
1815 case WM_KEYDOWN:
1816 switch(msg.wParam)
1818 case VK_UP:
1819 hittest = HTTOP;
1820 pt.x =(rectWindow.left+rectWindow.right)/2;
1821 pt.y = rectWindow.top + GetSystemMetrics(SM_CYFRAME) / 2;
1822 break;
1823 case VK_DOWN:
1824 hittest = HTBOTTOM;
1825 pt.x =(rectWindow.left+rectWindow.right)/2;
1826 pt.y = rectWindow.bottom - GetSystemMetrics(SM_CYFRAME) / 2;
1827 break;
1828 case VK_LEFT:
1829 hittest = HTLEFT;
1830 pt.x = rectWindow.left + GetSystemMetrics(SM_CXFRAME) / 2;
1831 pt.y =(rectWindow.top+rectWindow.bottom)/2;
1832 break;
1833 case VK_RIGHT:
1834 hittest = HTRIGHT;
1835 pt.x = rectWindow.right - GetSystemMetrics(SM_CXFRAME) / 2;
1836 pt.y =(rectWindow.top+rectWindow.bottom)/2;
1837 break;
1838 case VK_RETURN:
1839 case VK_ESCAPE: return 0;
1843 *capturePoint = pt;
1845 SetCursorPos( pt.x, pt.y );
1846 NC_HandleSetCursor( hwnd, (WPARAM)hwnd, MAKELONG( hittest, WM_MOUSEMOVE ));
1847 return hittest;
1851 /***********************************************************************
1852 * set_movesize_capture
1854 static void set_movesize_capture( HWND hwnd )
1856 HWND previous = 0;
1858 SERVER_START_REQ( set_capture_window )
1860 req->handle = hwnd;
1861 req->flags = CAPTURE_MOVESIZE;
1862 if (!wine_server_call_err( req ))
1864 previous = reply->previous;
1865 hwnd = reply->full_handle;
1868 SERVER_END_REQ;
1869 if (previous && previous != hwnd)
1870 SendMessageW( previous, WM_CAPTURECHANGED, 0, (LPARAM)hwnd );
1873 /***********************************************************************
1874 * X11DRV_WMMoveResizeWindow
1876 * Tells the window manager to initiate a move or resize operation.
1878 * SEE
1879 * http://freedesktop.org/Standards/wm-spec/1.3/ar01s04.html
1880 * or search for "_NET_WM_MOVERESIZE"
1882 static void X11DRV_WMMoveResizeWindow( HWND hwnd, int x, int y, int dir )
1884 XEvent xev;
1885 Display *display = thread_display();
1887 wine_tsx11_lock();
1889 /* need to ungrab the pointer that may have been automatically grabbed
1890 * with a ButtonPress event */
1891 XUngrabPointer( display, CurrentTime );
1893 xev.xclient.type = ClientMessage;
1894 xev.xclient.window = X11DRV_get_whole_window(hwnd);
1895 xev.xclient.message_type = x11drv_atom(_NET_WM_MOVERESIZE);
1896 xev.xclient.serial = 0;
1897 xev.xclient.display = display;
1898 xev.xclient.send_event = True;
1899 xev.xclient.format = 32;
1900 xev.xclient.data.l[0] = x; /* x coord */
1901 xev.xclient.data.l[1] = y; /* y coord */
1902 xev.xclient.data.l[2] = dir; /* direction */
1903 xev.xclient.data.l[3] = 1; /* button */
1904 xev.xclient.data.l[4] = 0; /* unused */
1905 XSendEvent(display, root_window, False, SubstructureNotifyMask, &xev);
1907 wine_tsx11_unlock();
1910 /***********************************************************************
1911 * SysCommandSizeMove (X11DRV.@)
1913 * Perform SC_MOVE and SC_SIZE commands.
1915 void X11DRV_SysCommandSizeMove( HWND hwnd, WPARAM wParam )
1917 MSG msg;
1918 RECT sizingRect, mouseRect, origRect;
1919 HDC hdc;
1920 HWND parent;
1921 LONG hittest = (LONG)(wParam & 0x0f);
1922 WPARAM syscommand = wParam & 0xfff0;
1923 HCURSOR hDragCursor = 0, hOldCursor = 0;
1924 POINT minTrack, maxTrack;
1925 POINT capturePoint, pt;
1926 LONG style = GetWindowLongA( hwnd, GWL_STYLE );
1927 LONG exstyle = GetWindowLongA( hwnd, GWL_EXSTYLE );
1928 BOOL thickframe = HAS_THICKFRAME( style, exstyle );
1929 BOOL iconic = style & WS_MINIMIZE;
1930 BOOL moved = FALSE;
1931 DWORD dwPoint = GetMessagePos ();
1932 BOOL DragFullWindows = FALSE;
1933 BOOL grab;
1934 int iWndsLocks;
1935 Display *old_gdi_display = NULL;
1936 Display *display = thread_display();
1938 pt.x = (short)LOWORD(dwPoint);
1939 pt.y = (short)HIWORD(dwPoint);
1940 capturePoint = pt;
1942 if (IsZoomed(hwnd) || !IsWindowVisible(hwnd)) return;
1944 /* if we are managed then we let the WM do all the work */
1945 if (exstyle & WS_EX_MANAGED)
1947 int dir;
1948 if (syscommand == SC_MOVE)
1950 if (!hittest) dir = _NET_WM_MOVERESIZE_MOVE_KEYBOARD;
1951 else dir = _NET_WM_MOVERESIZE_MOVE;
1953 else if (!hittest) dir = _NET_WM_MOVERESIZE_SIZE_KEYBOARD;
1954 else
1955 switch (hittest)
1957 case WMSZ_LEFT: dir = _NET_WM_MOVERESIZE_SIZE_LEFT; break;
1958 case WMSZ_RIGHT: dir = _NET_WM_MOVERESIZE_SIZE_RIGHT; break;
1959 case WMSZ_TOP: dir = _NET_WM_MOVERESIZE_SIZE_TOP; break;
1960 case WMSZ_TOPLEFT: dir = _NET_WM_MOVERESIZE_SIZE_TOPLEFT; break;
1961 case WMSZ_TOPRIGHT: dir = _NET_WM_MOVERESIZE_SIZE_TOPRIGHT; break;
1962 case WMSZ_BOTTOM: dir = _NET_WM_MOVERESIZE_SIZE_BOTTOM; break;
1963 case WMSZ_BOTTOMLEFT: dir = _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT; break;
1964 case WMSZ_BOTTOMRIGHT: dir = _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT; break;
1965 default:
1966 ERR("Invalid hittest value: %ld\n", hittest);
1967 dir = _NET_WM_MOVERESIZE_SIZE_KEYBOARD;
1969 X11DRV_WMMoveResizeWindow( hwnd, pt.x, pt.y, dir );
1970 return;
1973 SystemParametersInfoA(SPI_GETDRAGFULLWINDOWS, 0, &DragFullWindows, 0);
1975 if (syscommand == SC_MOVE)
1977 if (!hittest) hittest = start_size_move( hwnd, wParam, &capturePoint, style );
1978 if (!hittest) return;
1980 else /* SC_SIZE */
1982 if ( hittest && (syscommand != SC_MOUSEMENU) )
1983 hittest += (HTLEFT - WMSZ_LEFT);
1984 else
1986 set_movesize_capture( hwnd );
1987 hittest = start_size_move( hwnd, wParam, &capturePoint, style );
1988 if (!hittest)
1990 set_movesize_capture(0);
1991 return;
1996 /* Get min/max info */
1998 WINPOS_GetMinMaxInfo( hwnd, NULL, NULL, &minTrack, &maxTrack );
1999 GetWindowRect( hwnd, &sizingRect );
2000 if (style & WS_CHILD)
2002 parent = GetParent(hwnd);
2003 /* make sizing rect relative to parent */
2004 MapWindowPoints( 0, parent, (POINT*)&sizingRect, 2 );
2005 GetClientRect( parent, &mouseRect );
2007 else
2009 parent = 0;
2010 SetRect(&mouseRect, 0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN));
2012 origRect = sizingRect;
2014 if (ON_LEFT_BORDER(hittest))
2016 mouseRect.left = max( mouseRect.left, sizingRect.right-maxTrack.x );
2017 mouseRect.right = min( mouseRect.right, sizingRect.right-minTrack.x );
2019 else if (ON_RIGHT_BORDER(hittest))
2021 mouseRect.left = max( mouseRect.left, sizingRect.left+minTrack.x );
2022 mouseRect.right = min( mouseRect.right, sizingRect.left+maxTrack.x );
2024 if (ON_TOP_BORDER(hittest))
2026 mouseRect.top = max( mouseRect.top, sizingRect.bottom-maxTrack.y );
2027 mouseRect.bottom = min( mouseRect.bottom,sizingRect.bottom-minTrack.y);
2029 else if (ON_BOTTOM_BORDER(hittest))
2031 mouseRect.top = max( mouseRect.top, sizingRect.top+minTrack.y );
2032 mouseRect.bottom = min( mouseRect.bottom, sizingRect.top+maxTrack.y );
2034 if (parent) MapWindowPoints( parent, 0, (LPPOINT)&mouseRect, 2 );
2036 /* Retrieve a default cache DC (without using the window style) */
2037 hdc = GetDCEx( parent, 0, DCX_CACHE );
2039 if( iconic ) /* create a cursor for dragging */
2041 hDragCursor = (HCURSOR)GetClassLongA( hwnd, GCL_HICON);
2042 if( !hDragCursor ) hDragCursor = (HCURSOR)SendMessageA( hwnd, WM_QUERYDRAGICON, 0, 0L);
2043 if( !hDragCursor ) iconic = FALSE;
2046 /* repaint the window before moving it around */
2047 RedrawWindow( hwnd, NULL, 0, RDW_UPDATENOW | RDW_ALLCHILDREN );
2049 SendMessageA( hwnd, WM_ENTERSIZEMOVE, 0, 0 );
2050 set_movesize_capture( hwnd );
2052 /* grab the server only when moving top-level windows without desktop */
2053 grab = (!DragFullWindows && !parent && (root_window == DefaultRootWindow(gdi_display)));
2055 wine_tsx11_lock();
2056 if (grab)
2058 XSync( gdi_display, False );
2059 XGrabServer( display );
2060 XSync( display, False );
2061 /* switch gdi display to the thread display, since the server is grabbed */
2062 old_gdi_display = gdi_display;
2063 gdi_display = display;
2065 XGrabPointer( display, X11DRV_get_whole_window(hwnd), False,
2066 PointerMotionMask | ButtonPressMask | ButtonReleaseMask,
2067 GrabModeAsync, GrabModeAsync,
2068 parent ? X11DRV_get_client_window(parent) : root_window,
2069 None, CurrentTime );
2070 wine_tsx11_unlock();
2072 while(1)
2074 int dx = 0, dy = 0;
2076 if (!GetMessageW( &msg, 0, WM_KEYFIRST, WM_MOUSELAST )) break;
2077 if (CallMsgFilterW( &msg, MSGF_SIZE )) continue;
2079 /* Exit on button-up, Return, or Esc */
2080 if ((msg.message == WM_LBUTTONUP) ||
2081 ((msg.message == WM_KEYDOWN) &&
2082 ((msg.wParam == VK_RETURN) || (msg.wParam == VK_ESCAPE)))) break;
2084 if ((msg.message != WM_KEYDOWN) && (msg.message != WM_MOUSEMOVE))
2085 continue; /* We are not interested in other messages */
2087 pt = msg.pt;
2089 if (msg.message == WM_KEYDOWN) switch(msg.wParam)
2091 case VK_UP: pt.y -= 8; break;
2092 case VK_DOWN: pt.y += 8; break;
2093 case VK_LEFT: pt.x -= 8; break;
2094 case VK_RIGHT: pt.x += 8; break;
2097 pt.x = max( pt.x, mouseRect.left );
2098 pt.x = min( pt.x, mouseRect.right );
2099 pt.y = max( pt.y, mouseRect.top );
2100 pt.y = min( pt.y, mouseRect.bottom );
2102 dx = pt.x - capturePoint.x;
2103 dy = pt.y - capturePoint.y;
2105 if (dx || dy)
2107 if( !moved )
2109 moved = TRUE;
2111 if( iconic ) /* ok, no system popup tracking */
2113 hOldCursor = SetCursor(hDragCursor);
2114 ShowCursor( TRUE );
2115 WINPOS_ShowIconTitle( hwnd, FALSE );
2117 else if(!DragFullWindows)
2118 draw_moving_frame( hdc, &sizingRect, thickframe );
2121 if (msg.message == WM_KEYDOWN) SetCursorPos( pt.x, pt.y );
2122 else
2124 RECT newRect = sizingRect;
2125 WPARAM wpSizingHit = 0;
2127 if (hittest == HTCAPTION) OffsetRect( &newRect, dx, dy );
2128 if (ON_LEFT_BORDER(hittest)) newRect.left += dx;
2129 else if (ON_RIGHT_BORDER(hittest)) newRect.right += dx;
2130 if (ON_TOP_BORDER(hittest)) newRect.top += dy;
2131 else if (ON_BOTTOM_BORDER(hittest)) newRect.bottom += dy;
2132 if(!iconic && !DragFullWindows) draw_moving_frame( hdc, &sizingRect, thickframe );
2133 capturePoint = pt;
2135 /* determine the hit location */
2136 if (hittest >= HTLEFT && hittest <= HTBOTTOMRIGHT)
2137 wpSizingHit = WMSZ_LEFT + (hittest - HTLEFT);
2138 SendMessageA( hwnd, WM_SIZING, wpSizingHit, (LPARAM)&newRect );
2140 if (!iconic)
2142 if(!DragFullWindows)
2143 draw_moving_frame( hdc, &newRect, thickframe );
2144 else {
2145 /* To avoid any deadlocks, all the locks on the windows
2146 structures must be suspended before the SetWindowPos */
2147 iWndsLocks = WIN_SuspendWndsLock();
2148 SetWindowPos( hwnd, 0, newRect.left, newRect.top,
2149 newRect.right - newRect.left,
2150 newRect.bottom - newRect.top,
2151 ( hittest == HTCAPTION ) ? SWP_NOSIZE : 0 );
2152 WIN_RestoreWndsLock(iWndsLocks);
2155 sizingRect = newRect;
2160 set_movesize_capture(0);
2161 if( iconic )
2163 if( moved ) /* restore cursors, show icon title later on */
2165 ShowCursor( FALSE );
2166 SetCursor( hOldCursor );
2169 else if (moved && !DragFullWindows)
2170 draw_moving_frame( hdc, &sizingRect, thickframe );
2172 ReleaseDC( parent, hdc );
2174 wine_tsx11_lock();
2175 XUngrabPointer( display, CurrentTime );
2176 if (grab)
2178 XSync( display, False );
2179 XUngrabServer( display );
2180 XSync( display, False );
2181 gdi_display = old_gdi_display;
2183 wine_tsx11_unlock();
2185 if (HOOK_CallHooks( WH_CBT, HCBT_MOVESIZE, (WPARAM)hwnd, (LPARAM)&sizingRect, TRUE ))
2186 moved = FALSE;
2188 SendMessageA( hwnd, WM_EXITSIZEMOVE, 0, 0 );
2189 SendMessageA( hwnd, WM_SETVISIBLE, !IsIconic(hwnd), 0L);
2191 /* window moved or resized */
2192 if (moved)
2194 /* To avoid any deadlocks, all the locks on the windows
2195 structures must be suspended before the SetWindowPos */
2196 iWndsLocks = WIN_SuspendWndsLock();
2198 /* if the moving/resizing isn't canceled call SetWindowPos
2199 * with the new position or the new size of the window
2201 if (!((msg.message == WM_KEYDOWN) && (msg.wParam == VK_ESCAPE)) )
2203 /* NOTE: SWP_NOACTIVATE prevents document window activation in Word 6 */
2204 if(!DragFullWindows)
2205 SetWindowPos( hwnd, 0, sizingRect.left, sizingRect.top,
2206 sizingRect.right - sizingRect.left,
2207 sizingRect.bottom - sizingRect.top,
2208 ( hittest == HTCAPTION ) ? SWP_NOSIZE : 0 );
2210 else
2211 { /* restore previous size/position */
2212 if(DragFullWindows)
2213 SetWindowPos( hwnd, 0, origRect.left, origRect.top,
2214 origRect.right - origRect.left,
2215 origRect.bottom - origRect.top,
2216 ( hittest == HTCAPTION ) ? SWP_NOSIZE : 0 );
2219 WIN_RestoreWndsLock(iWndsLocks);
2222 if (IsIconic(hwnd))
2224 /* Single click brings up the system menu when iconized */
2226 if( !moved )
2228 if(style & WS_SYSMENU )
2229 SendMessageA( hwnd, WM_SYSCOMMAND,
2230 SC_MOUSEMENU + HTSYSMENU, MAKELONG(pt.x,pt.y));
2232 else WINPOS_ShowIconTitle( hwnd, TRUE );
2237 /***********************************************************************
2238 * ForceWindowRaise (X11DRV.@)
2240 * Raise a window on top of the X stacking order, while preserving
2241 * the correct Windows Z order.
2243 * FIXME: this should go away.
2245 void X11DRV_ForceWindowRaise( HWND hwnd )
2247 int i = 0;
2248 HWND *list;
2249 XWindowChanges winChanges;
2250 Display *display = thread_display();
2251 WND *wndPtr = WIN_FindWndPtr( hwnd );
2253 if (!wndPtr) return;
2255 if ((wndPtr->dwExStyle & WS_EX_MANAGED) ||
2256 wndPtr->parent != GetDesktopWindow() ||
2257 IsRectEmpty( &wndPtr->rectWindow ) ||
2258 !get_whole_window(wndPtr))
2260 WIN_ReleaseWndPtr( wndPtr );
2261 return;
2263 WIN_ReleaseWndPtr( wndPtr );
2265 /* Raise all windows up to wndPtr according to their Z order.
2266 * (it would be easier with sibling-related Below but it doesn't
2267 * work very well with SGI mwm for instance)
2269 winChanges.stack_mode = Above;
2270 if (!(list = WIN_ListChildren( GetDesktopWindow() ))) return;
2271 while (list[i] && list[i] != hwnd) i++;
2272 if (list[i])
2274 for ( ; i >= 0; i--)
2276 WND *ptr = WIN_FindWndPtr( list[i] );
2277 if (!ptr) continue;
2278 if (!IsRectEmpty( &ptr->rectWindow ) && get_whole_window(ptr))
2280 wine_tsx11_lock();
2281 XReconfigureWMWindow( display, get_whole_window(ptr), 0, CWStackMode, &winChanges );
2282 wine_tsx11_unlock();
2284 WIN_ReleaseWndPtr( ptr );
2287 HeapFree( GetProcessHeap(), 0, list );