Release 20050930.
[wine/gsoc-2012-control.git] / dlls / x11drv / winpos.c
blobe5fcbafb0cf3ca37734cd6c912ef76a7f164ed8b
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 "ntstatus.h"
37 #include "wownt32.h"
38 #include "wine/wingdi16.h"
40 #include "x11drv.h"
41 #include "win.h"
42 #include "winpos.h"
44 #include "wine/server.h"
45 #include "wine/debug.h"
47 WINE_DEFAULT_DEBUG_CHANNEL(x11drv);
49 #define SWP_AGG_NOPOSCHANGE \
50 (SWP_NOSIZE | SWP_NOMOVE | SWP_NOCLIENTSIZE | SWP_NOCLIENTMOVE | SWP_NOZORDER)
51 #define SWP_AGG_STATUSFLAGS \
52 (SWP_AGG_NOPOSCHANGE | SWP_FRAMECHANGED | SWP_HIDEWINDOW | SWP_SHOWWINDOW)
54 #define SWP_EX_NOCOPY 0x0001
55 #define SWP_EX_PAINTSELF 0x0002
56 #define SWP_EX_NONCLIENT 0x0004
58 #define HAS_THICKFRAME(style) \
59 (((style) & WS_THICKFRAME) && \
60 !(((style) & (WS_DLGFRAME|WS_BORDER)) == WS_DLGFRAME))
62 #define ON_LEFT_BORDER(hit) \
63 (((hit) == HTLEFT) || ((hit) == HTTOPLEFT) || ((hit) == HTBOTTOMLEFT))
64 #define ON_RIGHT_BORDER(hit) \
65 (((hit) == HTRIGHT) || ((hit) == HTTOPRIGHT) || ((hit) == HTBOTTOMRIGHT))
66 #define ON_TOP_BORDER(hit) \
67 (((hit) == HTTOP) || ((hit) == HTTOPLEFT) || ((hit) == HTTOPRIGHT))
68 #define ON_BOTTOM_BORDER(hit) \
69 (((hit) == HTBOTTOM) || ((hit) == HTBOTTOMLEFT) || ((hit) == HTBOTTOMRIGHT))
71 #define _NET_WM_MOVERESIZE_SIZE_TOPLEFT 0
72 #define _NET_WM_MOVERESIZE_SIZE_TOP 1
73 #define _NET_WM_MOVERESIZE_SIZE_TOPRIGHT 2
74 #define _NET_WM_MOVERESIZE_SIZE_RIGHT 3
75 #define _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT 4
76 #define _NET_WM_MOVERESIZE_SIZE_BOTTOM 5
77 #define _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT 6
78 #define _NET_WM_MOVERESIZE_SIZE_LEFT 7
79 #define _NET_WM_MOVERESIZE_MOVE 8 /* movement only */
80 #define _NET_WM_MOVERESIZE_SIZE_KEYBOARD 9 /* size via keyboard */
81 #define _NET_WM_MOVERESIZE_MOVE_KEYBOARD 10 /* move via keyboard */
84 /***********************************************************************
85 * X11DRV_Expose
87 void X11DRV_Expose( HWND hwnd, XEvent *xev )
89 XExposeEvent *event = &xev->xexpose;
90 RECT rect;
91 struct x11drv_win_data *data;
92 int flags = RDW_INVALIDATE | RDW_ERASE | RDW_ALLCHILDREN;
94 TRACE( "win %p (%lx) %d,%d %dx%d\n",
95 hwnd, event->window, event->x, event->y, event->width, event->height );
97 if (!(data = X11DRV_get_win_data( hwnd ))) return;
99 rect.left = event->x;
100 rect.top = event->y;
101 rect.right = rect.left + event->width;
102 rect.bottom = rect.top + event->height;
104 if (rect.left < data->client_rect.left ||
105 rect.top < data->client_rect.top ||
106 rect.right > data->client_rect.right ||
107 rect.bottom > data->client_rect.bottom) flags |= RDW_FRAME;
109 SERVER_START_REQ( update_window_zorder )
111 req->window = hwnd;
112 req->rect.left = rect.left + data->whole_rect.left;
113 req->rect.top = rect.top + data->whole_rect.top;
114 req->rect.right = rect.right + data->whole_rect.left;
115 req->rect.bottom = rect.bottom + data->whole_rect.top;
116 wine_server_call( req );
118 SERVER_END_REQ;
120 /* make position relative to client area instead of window */
121 OffsetRect( &rect, -data->client_rect.left, -data->client_rect.top );
122 RedrawWindow( hwnd, &rect, 0, flags );
126 /***********************************************************************
127 * SWP_DoWinPosChanging
129 static BOOL SWP_DoWinPosChanging( WINDOWPOS* pWinpos, RECT* pNewWindowRect, RECT* pNewClientRect )
131 WND *wndPtr;
133 /* Send WM_WINDOWPOSCHANGING message */
135 if (!(pWinpos->flags & SWP_NOSENDCHANGING))
136 SendMessageW( pWinpos->hwnd, WM_WINDOWPOSCHANGING, 0, (LPARAM)pWinpos );
138 if (!(wndPtr = WIN_GetPtr( pWinpos->hwnd )) || wndPtr == WND_OTHER_PROCESS) return FALSE;
140 /* Calculate new position and size */
142 *pNewWindowRect = wndPtr->rectWindow;
143 *pNewClientRect = (wndPtr->dwStyle & WS_MINIMIZE) ? wndPtr->rectWindow
144 : wndPtr->rectClient;
146 if (!(pWinpos->flags & SWP_NOSIZE))
148 pNewWindowRect->right = pNewWindowRect->left + pWinpos->cx;
149 pNewWindowRect->bottom = pNewWindowRect->top + pWinpos->cy;
151 if (!(pWinpos->flags & SWP_NOMOVE))
153 pNewWindowRect->left = pWinpos->x;
154 pNewWindowRect->top = pWinpos->y;
155 pNewWindowRect->right += pWinpos->x - wndPtr->rectWindow.left;
156 pNewWindowRect->bottom += pWinpos->y - wndPtr->rectWindow.top;
158 OffsetRect( pNewClientRect, pWinpos->x - wndPtr->rectWindow.left,
159 pWinpos->y - wndPtr->rectWindow.top );
161 pWinpos->flags |= SWP_NOCLIENTMOVE | SWP_NOCLIENTSIZE;
163 TRACE( "hwnd %p, after %p, swp %d,%d %dx%d flags %08x\n",
164 pWinpos->hwnd, pWinpos->hwndInsertAfter, pWinpos->x, pWinpos->y,
165 pWinpos->cx, pWinpos->cy, pWinpos->flags );
166 TRACE( "current %s style %08lx new %s\n",
167 wine_dbgstr_rect( &wndPtr->rectWindow ), wndPtr->dwStyle,
168 wine_dbgstr_rect( pNewWindowRect ));
170 WIN_ReleasePtr( wndPtr );
171 return TRUE;
175 /***********************************************************************
176 * get_valid_rects
178 * Compute the valid rects from the old and new client rect and WVR_* flags.
179 * Helper for WM_NCCALCSIZE handling.
181 static inline void get_valid_rects( const RECT *old_client, const RECT *new_client, UINT flags,
182 RECT *valid )
184 int cx, cy;
186 if (flags & WVR_REDRAW)
188 SetRectEmpty( &valid[0] );
189 SetRectEmpty( &valid[1] );
190 return;
193 if (flags & WVR_VALIDRECTS)
195 if (!IntersectRect( &valid[0], &valid[0], new_client ) ||
196 !IntersectRect( &valid[1], &valid[1], old_client ))
198 SetRectEmpty( &valid[0] );
199 SetRectEmpty( &valid[1] );
200 return;
202 flags = WVR_ALIGNLEFT | WVR_ALIGNTOP;
204 else
206 valid[0] = *new_client;
207 valid[1] = *old_client;
210 /* make sure the rectangles have the same size */
211 cx = min( valid[0].right - valid[0].left, valid[1].right - valid[1].left );
212 cy = min( valid[0].bottom - valid[0].top, valid[1].bottom - valid[1].top );
214 if (flags & WVR_ALIGNBOTTOM)
216 valid[0].top = valid[0].bottom - cy;
217 valid[1].top = valid[1].bottom - cy;
219 else
221 valid[0].bottom = valid[0].top + cy;
222 valid[1].bottom = valid[1].top + cy;
224 if (flags & WVR_ALIGNRIGHT)
226 valid[0].left = valid[0].right - cx;
227 valid[1].left = valid[1].right - cx;
229 else
231 valid[0].right = valid[0].left + cx;
232 valid[1].right = valid[1].left + cx;
237 /***********************************************************************
238 * SWP_DoNCCalcSize
240 static UINT SWP_DoNCCalcSize( WINDOWPOS* pWinpos, const RECT* pNewWindowRect, RECT* pNewClientRect,
241 RECT *validRects )
243 UINT wvrFlags = 0;
244 WND *wndPtr;
246 if (!(wndPtr = WIN_GetPtr( pWinpos->hwnd )) || wndPtr == WND_OTHER_PROCESS) return 0;
248 /* Send WM_NCCALCSIZE message to get new client area */
249 if( (pWinpos->flags & (SWP_FRAMECHANGED | SWP_NOSIZE)) != SWP_NOSIZE )
251 NCCALCSIZE_PARAMS params;
252 WINDOWPOS winposCopy;
254 params.rgrc[0] = *pNewWindowRect;
255 params.rgrc[1] = wndPtr->rectWindow;
256 params.rgrc[2] = wndPtr->rectClient;
257 params.lppos = &winposCopy;
258 winposCopy = *pWinpos;
259 WIN_ReleasePtr( wndPtr );
261 wvrFlags = SendMessageW( pWinpos->hwnd, WM_NCCALCSIZE, TRUE, (LPARAM)&params );
263 *pNewClientRect = params.rgrc[0];
265 if (!(wndPtr = WIN_GetPtr( pWinpos->hwnd )) || wndPtr == WND_OTHER_PROCESS) return 0;
267 TRACE( "hwnd %p old win %s old client %s new win %s new client %s\n", pWinpos->hwnd,
268 wine_dbgstr_rect(&wndPtr->rectWindow), wine_dbgstr_rect(&wndPtr->rectClient),
269 wine_dbgstr_rect(pNewWindowRect), wine_dbgstr_rect(pNewClientRect) );
271 if( pNewClientRect->left != wndPtr->rectClient.left ||
272 pNewClientRect->top != wndPtr->rectClient.top )
273 pWinpos->flags &= ~SWP_NOCLIENTMOVE;
275 if( (pNewClientRect->right - pNewClientRect->left !=
276 wndPtr->rectClient.right - wndPtr->rectClient.left))
277 pWinpos->flags &= ~SWP_NOCLIENTSIZE;
278 else
279 wvrFlags &= ~WVR_HREDRAW;
281 if (pNewClientRect->bottom - pNewClientRect->top !=
282 wndPtr->rectClient.bottom - wndPtr->rectClient.top)
283 pWinpos->flags &= ~SWP_NOCLIENTSIZE;
284 else
285 wvrFlags &= ~WVR_VREDRAW;
287 validRects[0] = params.rgrc[1];
288 validRects[1] = params.rgrc[2];
290 else
292 if (!(pWinpos->flags & SWP_NOMOVE) &&
293 (pNewClientRect->left != wndPtr->rectClient.left ||
294 pNewClientRect->top != wndPtr->rectClient.top))
295 pWinpos->flags &= ~SWP_NOCLIENTMOVE;
298 if (pWinpos->flags & (SWP_NOCOPYBITS | SWP_NOREDRAW | SWP_SHOWWINDOW | SWP_HIDEWINDOW))
300 SetRectEmpty( &validRects[0] );
301 SetRectEmpty( &validRects[1] );
303 else get_valid_rects( &wndPtr->rectClient, pNewClientRect, wvrFlags, validRects );
305 WIN_ReleasePtr( wndPtr );
306 return wvrFlags;
310 struct move_owned_info
312 HWND owner;
313 HWND insert_after;
316 static BOOL CALLBACK move_owned_popups( HWND hwnd, LPARAM lparam )
318 struct move_owned_info *info = (struct move_owned_info *)lparam;
320 if (hwnd == info->owner) return FALSE;
321 if ((GetWindowLongW( hwnd, GWL_STYLE ) & WS_POPUP) &&
322 GetWindow( hwnd, GW_OWNER ) == info->owner)
324 SetWindowPos( hwnd, info->insert_after, 0, 0, 0, 0,
325 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE |
326 SWP_NOSENDCHANGING | SWP_DEFERERASE );
327 info->insert_after = hwnd;
329 return TRUE;
332 /***********************************************************************
333 * SWP_DoOwnedPopups
335 * fix Z order taking into account owned popups -
336 * basically we need to maintain them above the window that owns them
338 * FIXME: hide/show owned popups when owner visibility changes.
340 static HWND SWP_DoOwnedPopups(HWND hwnd, HWND hwndInsertAfter)
342 HWND owner = GetWindow( hwnd, GW_OWNER );
343 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
344 struct move_owned_info info;
346 TRACE("(%p) hInsertAfter = %p\n", hwnd, hwndInsertAfter );
348 if ((style & WS_POPUP) && owner)
350 /* make sure this popup stays above the owner */
352 if( hwndInsertAfter != HWND_TOP )
354 HWND hwndLocalPrev = HWND_TOP;
355 HWND prev = GetWindow( owner, GW_HWNDPREV );
357 while (prev && prev != hwndInsertAfter)
359 if (hwndLocalPrev == HWND_TOP && GetWindowLongW( prev, GWL_STYLE ) & WS_VISIBLE)
360 hwndLocalPrev = prev;
361 prev = GetWindow( prev, GW_HWNDPREV );
363 if (!prev) hwndInsertAfter = hwndLocalPrev;
366 else if (style & WS_CHILD) return hwndInsertAfter;
368 info.owner = hwnd;
369 info.insert_after = hwndInsertAfter;
370 EnumWindows( move_owned_popups, (LPARAM)&info );
371 return info.insert_after;
375 /* fix redundant flags and values in the WINDOWPOS structure */
376 static BOOL fixup_flags( WINDOWPOS *winpos )
378 HWND parent;
379 WND *wndPtr = WIN_GetPtr( winpos->hwnd );
380 BOOL ret = TRUE;
382 if (!wndPtr || wndPtr == WND_OTHER_PROCESS)
384 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
385 return FALSE;
387 winpos->hwnd = wndPtr->hwndSelf; /* make it a full handle */
389 /* Finally make sure that all coordinates are valid */
390 if (winpos->x < -32768) winpos->x = -32768;
391 else if (winpos->x > 32767) winpos->x = 32767;
392 if (winpos->y < -32768) winpos->y = -32768;
393 else if (winpos->y > 32767) winpos->y = 32767;
395 if (winpos->cx < 0) winpos->cx = 0;
396 else if (winpos->cx > 32767) winpos->cx = 32767;
397 if (winpos->cy < 0) winpos->cy = 0;
398 else if (winpos->cy > 32767) winpos->cy = 32767;
400 parent = GetAncestor( winpos->hwnd, GA_PARENT );
401 if (!IsWindowVisible( parent )) winpos->flags |= SWP_NOREDRAW;
403 if (wndPtr->dwStyle & WS_VISIBLE) winpos->flags &= ~SWP_SHOWWINDOW;
404 else
406 winpos->flags &= ~SWP_HIDEWINDOW;
407 if (!(winpos->flags & SWP_SHOWWINDOW)) winpos->flags |= SWP_NOREDRAW;
410 if ((wndPtr->rectWindow.right - wndPtr->rectWindow.left == winpos->cx) &&
411 (wndPtr->rectWindow.bottom - wndPtr->rectWindow.top == winpos->cy))
412 winpos->flags |= SWP_NOSIZE; /* Already the right size */
414 if ((wndPtr->rectWindow.left == winpos->x) && (wndPtr->rectWindow.top == winpos->y))
415 winpos->flags |= SWP_NOMOVE; /* Already the right position */
417 if ((wndPtr->dwStyle & (WS_POPUP | WS_CHILD)) != WS_CHILD)
419 if (!(winpos->flags & (SWP_NOACTIVATE|SWP_HIDEWINDOW))) /* Bring to the top when activating */
421 winpos->flags &= ~SWP_NOZORDER;
422 winpos->hwndInsertAfter = HWND_TOP;
426 /* Check hwndInsertAfter */
427 if (winpos->flags & SWP_NOZORDER) goto done;
429 /* fix sign extension */
430 if (winpos->hwndInsertAfter == (HWND)0xffff) winpos->hwndInsertAfter = HWND_TOPMOST;
431 else if (winpos->hwndInsertAfter == (HWND)0xfffe) winpos->hwndInsertAfter = HWND_NOTOPMOST;
433 /* FIXME: TOPMOST not supported yet */
434 if ((winpos->hwndInsertAfter == HWND_TOPMOST) ||
435 (winpos->hwndInsertAfter == HWND_NOTOPMOST)) winpos->hwndInsertAfter = HWND_TOP;
437 /* hwndInsertAfter must be a sibling of the window */
438 if (winpos->hwndInsertAfter == HWND_TOP)
440 if (GetWindow(winpos->hwnd, GW_HWNDFIRST) == winpos->hwnd)
441 winpos->flags |= SWP_NOZORDER;
443 else if (winpos->hwndInsertAfter == HWND_BOTTOM)
445 if (GetWindow(winpos->hwnd, GW_HWNDLAST) == winpos->hwnd)
446 winpos->flags |= SWP_NOZORDER;
448 else
450 if (GetAncestor( winpos->hwndInsertAfter, GA_PARENT ) != parent) ret = FALSE;
451 else
453 /* don't need to change the Zorder of hwnd if it's already inserted
454 * after hwndInsertAfter or when inserting hwnd after itself.
456 if ((winpos->hwnd == winpos->hwndInsertAfter) ||
457 (winpos->hwnd == GetWindow( winpos->hwndInsertAfter, GW_HWNDNEXT )))
458 winpos->flags |= SWP_NOZORDER;
461 done:
462 WIN_ReleasePtr( wndPtr );
463 return ret;
467 /***********************************************************************
468 * SetWindowStyle (X11DRV.@)
470 * Update the X state of a window to reflect a style change
472 void X11DRV_SetWindowStyle( HWND hwnd, DWORD old_style )
474 Display *display = thread_display();
475 struct x11drv_win_data *data;
476 DWORD new_style, changed;
478 if (hwnd == GetDesktopWindow()) return;
479 if (!(data = X11DRV_get_win_data( hwnd ))) return;
481 new_style = GetWindowLongW( hwnd, GWL_STYLE );
482 changed = new_style ^ old_style;
484 if (changed & WS_VISIBLE)
486 if (data->whole_window && X11DRV_is_window_rect_mapped( &data->window_rect ))
488 if (new_style & WS_VISIBLE)
490 TRACE( "mapping win %p\n", hwnd );
491 X11DRV_sync_window_style( display, data );
492 X11DRV_set_wm_hints( display, data );
493 wine_tsx11_lock();
494 XMapWindow( display, data->whole_window );
495 wine_tsx11_unlock();
497 /* we don't unmap windows, that causes trouble with the window manager */
499 invalidate_dce( hwnd, &data->window_rect );
502 if (changed & WS_DISABLED)
504 if (data->whole_window && data->managed)
506 XWMHints *wm_hints;
507 wine_tsx11_lock();
508 if (!(wm_hints = XGetWMHints( display, data->whole_window )))
509 wm_hints = XAllocWMHints();
510 if (wm_hints)
512 wm_hints->flags |= InputHint;
513 wm_hints->input = !(new_style & WS_DISABLED);
514 XSetWMHints( display, data->whole_window, wm_hints );
515 XFree(wm_hints);
517 wine_tsx11_unlock();
523 /***********************************************************************
524 * X11DRV_set_window_pos
526 * Set a window position and Z order.
528 BOOL X11DRV_set_window_pos( HWND hwnd, HWND insert_after, const RECT *rectWindow,
529 const RECT *rectClient, UINT swp_flags, const RECT *valid_rects )
531 struct x11drv_win_data *data;
532 RECT new_whole_rect;
533 WND *win;
534 DWORD old_style, new_style;
535 BOOL ret;
537 if (!(data = X11DRV_get_win_data( hwnd ))) return FALSE;
539 new_whole_rect = *rectWindow;
540 X11DRV_window_to_X_rect( data, &new_whole_rect );
542 if (!IsRectEmpty( &valid_rects[0] ))
544 int x_offset = 0, y_offset = 0;
546 if (data->whole_window)
548 /* the X server will move the bits for us */
549 x_offset = data->whole_rect.left - new_whole_rect.left;
550 y_offset = data->whole_rect.top - new_whole_rect.top;
553 if (x_offset != valid_rects[1].left - valid_rects[0].left ||
554 y_offset != valid_rects[1].top - valid_rects[0].top)
556 /* FIXME: should copy the window bits here */
557 valid_rects = NULL;
561 if (!(win = WIN_GetPtr( hwnd ))) return FALSE;
562 if (win == WND_OTHER_PROCESS)
564 if (IsWindow( hwnd )) ERR( "cannot set rectangles of other process window %p\n", hwnd );
565 return FALSE;
567 SERVER_START_REQ( set_window_pos )
569 req->handle = hwnd;
570 req->previous = insert_after;
571 req->flags = swp_flags & ~SWP_WINE_NOHOSTMOVE;
572 req->window.left = rectWindow->left;
573 req->window.top = rectWindow->top;
574 req->window.right = rectWindow->right;
575 req->window.bottom = rectWindow->bottom;
576 req->client.left = rectClient->left;
577 req->client.top = rectClient->top;
578 req->client.right = rectClient->right;
579 req->client.bottom = rectClient->bottom;
580 if (memcmp( rectWindow, &new_whole_rect, sizeof(RECT) ) || !IsRectEmpty( &valid_rects[0] ))
582 wine_server_add_data( req, &new_whole_rect, sizeof(new_whole_rect) );
583 if (!IsRectEmpty( &valid_rects[0] ))
584 wine_server_add_data( req, valid_rects, 2 * sizeof(*valid_rects) );
586 ret = !wine_server_call( req );
587 new_style = reply->new_style;
589 SERVER_END_REQ;
591 if (win == WND_DESKTOP)
593 data->whole_rect = data->client_rect = data->window_rect = *rectWindow;
594 return ret;
597 if (ret)
599 Display *display = thread_display();
601 /* invalidate DCEs */
603 if ((((swp_flags & SWP_AGG_NOPOSCHANGE) != SWP_AGG_NOPOSCHANGE) && (new_style & WS_VISIBLE)) ||
604 (swp_flags & (SWP_HIDEWINDOW | SWP_SHOWWINDOW)))
606 RECT rect;
607 UnionRect( &rect, rectWindow, &win->rectWindow );
608 invalidate_dce( hwnd, &rect );
611 win->rectWindow = *rectWindow;
612 win->rectClient = *rectClient;
613 old_style = win->dwStyle;
614 win->dwStyle = new_style;
615 data->window_rect = *rectWindow;
617 TRACE( "win %p window %s client %s style %08lx\n",
618 hwnd, wine_dbgstr_rect(rectWindow), wine_dbgstr_rect(rectClient), new_style );
620 /* FIXME: copy the valid bits */
622 if (data->whole_window && !(swp_flags & SWP_WINE_NOHOSTMOVE))
624 if ((old_style & WS_VISIBLE) && !(new_style & WS_VISIBLE))
626 /* window got hidden, unmap it */
627 TRACE( "unmapping win %p\n", hwnd );
628 wine_tsx11_lock();
629 XUnmapWindow( display, data->whole_window );
630 wine_tsx11_unlock();
632 else if ((new_style & WS_VISIBLE) && !X11DRV_is_window_rect_mapped( rectWindow ))
634 /* resizing to zero size or off screen -> unmap */
635 TRACE( "unmapping zero size or off-screen win %p\n", hwnd );
636 wine_tsx11_lock();
637 XUnmapWindow( display, data->whole_window );
638 wine_tsx11_unlock();
642 X11DRV_sync_window_position( display, data, swp_flags, rectClient, &new_whole_rect );
644 if (data->whole_window && !(swp_flags & SWP_WINE_NOHOSTMOVE))
646 if (!(old_style & WS_VISIBLE) && (new_style & WS_VISIBLE))
648 /* window got shown, map it */
649 if (X11DRV_is_window_rect_mapped( rectWindow ))
651 TRACE( "mapping win %p\n", hwnd );
652 X11DRV_sync_window_style( display, data );
653 X11DRV_set_wm_hints( display, data );
654 wine_tsx11_lock();
655 XMapWindow( display, data->whole_window );
656 wine_tsx11_unlock();
659 else if ((new_style & WS_VISIBLE) && X11DRV_is_window_rect_mapped( rectWindow ))
661 /* resizing from zero size to non-zero -> map */
662 TRACE( "mapping non zero size or off-screen win %p\n", hwnd );
663 wine_tsx11_lock();
664 XMapWindow( display, data->whole_window );
665 wine_tsx11_unlock();
667 wine_tsx11_lock();
668 XFlush( display ); /* FIXME: should not be necessary */
669 wine_tsx11_unlock();
672 WIN_ReleasePtr( win );
673 return ret;
677 /***********************************************************************
678 * SetWindowPos (X11DRV.@)
680 BOOL X11DRV_SetWindowPos( WINDOWPOS *winpos )
682 RECT newWindowRect, newClientRect, valid_rects[2];
683 UINT orig_flags;
685 TRACE( "hwnd %p, after %p, swp %d,%d %dx%d flags %08x\n",
686 winpos->hwnd, winpos->hwndInsertAfter, winpos->x, winpos->y,
687 winpos->cx, winpos->cy, winpos->flags);
689 orig_flags = winpos->flags;
690 winpos->flags &= ~SWP_WINE_NOHOSTMOVE;
692 /* Check window handle */
693 if (winpos->hwnd == GetDesktopWindow()) return FALSE;
695 /* First make sure that coordinates are valid for WM_WINDOWPOSCHANGING */
696 if (!(winpos->flags & SWP_NOMOVE))
698 if (winpos->x < -32768) winpos->x = -32768;
699 else if (winpos->x > 32767) winpos->x = 32767;
700 if (winpos->y < -32768) winpos->y = -32768;
701 else if (winpos->y > 32767) winpos->y = 32767;
703 if (!(winpos->flags & SWP_NOSIZE))
705 if (winpos->cx < 0) winpos->cx = 0;
706 else if (winpos->cx > 32767) winpos->cx = 32767;
707 if (winpos->cy < 0) winpos->cy = 0;
708 else if (winpos->cy > 32767) winpos->cy = 32767;
711 if (!SWP_DoWinPosChanging( winpos, &newWindowRect, &newClientRect )) return FALSE;
713 /* Fix redundant flags */
714 if (!fixup_flags( winpos )) return FALSE;
716 if((winpos->flags & (SWP_NOZORDER | SWP_HIDEWINDOW | SWP_SHOWWINDOW)) != SWP_NOZORDER)
718 if (GetAncestor( winpos->hwnd, GA_PARENT ) == GetDesktopWindow())
719 winpos->hwndInsertAfter = SWP_DoOwnedPopups( winpos->hwnd, winpos->hwndInsertAfter );
722 /* Common operations */
724 SWP_DoNCCalcSize( winpos, &newWindowRect, &newClientRect, valid_rects );
726 if (!X11DRV_set_window_pos( winpos->hwnd, winpos->hwndInsertAfter,
727 &newWindowRect, &newClientRect, orig_flags, valid_rects ))
728 return FALSE;
730 if (!(orig_flags & SWP_SHOWWINDOW))
732 UINT rdw_flags = RDW_FRAME | RDW_ERASE;
733 if ( !(orig_flags & SWP_DEFERERASE) ) rdw_flags |= RDW_ERASENOW;
734 RedrawWindow( winpos->hwnd, NULL, NULL, rdw_flags );
737 if( winpos->flags & SWP_HIDEWINDOW )
738 HideCaret(winpos->hwnd);
739 else if (winpos->flags & SWP_SHOWWINDOW)
740 ShowCaret(winpos->hwnd);
742 if (!(winpos->flags & (SWP_NOACTIVATE|SWP_HIDEWINDOW)))
744 /* child windows get WM_CHILDACTIVATE message */
745 if ((GetWindowLongW( winpos->hwnd, GWL_STYLE ) & (WS_CHILD | WS_POPUP)) == WS_CHILD)
746 SendMessageA( winpos->hwnd, WM_CHILDACTIVATE, 0, 0 );
747 else
748 SetForegroundWindow( winpos->hwnd );
751 /* And last, send the WM_WINDOWPOSCHANGED message */
753 TRACE("\tstatus flags = %04x\n", winpos->flags & SWP_AGG_STATUSFLAGS);
755 if (((winpos->flags & SWP_AGG_STATUSFLAGS) != SWP_AGG_NOPOSCHANGE))
757 /* WM_WINDOWPOSCHANGED is sent even if SWP_NOSENDCHANGING is set
758 and always contains final window position.
760 winpos->x = newWindowRect.left;
761 winpos->y = newWindowRect.top;
762 winpos->cx = newWindowRect.right - newWindowRect.left;
763 winpos->cy = newWindowRect.bottom - newWindowRect.top;
764 SendMessageW( winpos->hwnd, WM_WINDOWPOSCHANGED, 0, (LPARAM)winpos );
767 return TRUE;
771 /***********************************************************************
772 * WINPOS_FindIconPos
774 * Find a suitable place for an iconic window.
776 static POINT WINPOS_FindIconPos( HWND hwnd, POINT pt )
778 RECT rect, rectParent;
779 HWND parent, child;
780 HRGN hrgn, tmp;
781 int xspacing, yspacing;
783 parent = GetAncestor( hwnd, GA_PARENT );
784 GetClientRect( parent, &rectParent );
785 if ((pt.x >= rectParent.left) && (pt.x + GetSystemMetrics(SM_CXICON) < rectParent.right) &&
786 (pt.y >= rectParent.top) && (pt.y + GetSystemMetrics(SM_CYICON) < rectParent.bottom))
787 return pt; /* The icon already has a suitable position */
789 xspacing = GetSystemMetrics(SM_CXICONSPACING);
790 yspacing = GetSystemMetrics(SM_CYICONSPACING);
792 /* Check if another icon already occupies this spot */
793 /* FIXME: this is completely inefficient */
795 hrgn = CreateRectRgn( 0, 0, 0, 0 );
796 tmp = CreateRectRgn( 0, 0, 0, 0 );
797 for (child = GetWindow( parent, GW_HWNDFIRST ); child; child = GetWindow( child, GW_HWNDNEXT ))
799 WND *childPtr;
800 if (child == hwnd) continue;
801 if ((GetWindowLongW( child, GWL_STYLE ) & (WS_VISIBLE|WS_MINIMIZE)) != (WS_VISIBLE|WS_MINIMIZE))
802 continue;
803 if (!(childPtr = WIN_GetPtr( child )) || childPtr == WND_OTHER_PROCESS)
804 continue;
805 SetRectRgn( tmp, childPtr->rectWindow.left, childPtr->rectWindow.top,
806 childPtr->rectWindow.right, childPtr->rectWindow.bottom );
807 CombineRgn( hrgn, hrgn, tmp, RGN_OR );
808 WIN_ReleasePtr( childPtr );
810 DeleteObject( tmp );
812 for (rect.bottom = rectParent.bottom; rect.bottom >= yspacing; rect.bottom -= yspacing)
814 for (rect.left = rectParent.left; rect.left <= rectParent.right - xspacing; rect.left += xspacing)
816 rect.right = rect.left + xspacing;
817 rect.top = rect.bottom - yspacing;
818 if (!RectInRegion( hrgn, &rect ))
820 /* No window was found, so it's OK for us */
821 pt.x = rect.left + (xspacing - GetSystemMetrics(SM_CXICON)) / 2;
822 pt.y = rect.top + (yspacing - GetSystemMetrics(SM_CYICON)) / 2;
823 DeleteObject( hrgn );
824 return pt;
828 DeleteObject( hrgn );
829 pt.x = pt.y = 0;
830 return pt;
837 UINT WINPOS_MinMaximize( HWND hwnd, UINT cmd, LPRECT rect )
839 WND *wndPtr;
840 UINT swpFlags = 0;
841 POINT size;
842 LONG old_style;
843 WINDOWPLACEMENT wpl;
845 TRACE("%p %u\n", hwnd, cmd );
847 wpl.length = sizeof(wpl);
848 GetWindowPlacement( hwnd, &wpl );
850 if (HOOK_CallHooks( WH_CBT, HCBT_MINMAX, (WPARAM)hwnd, cmd, TRUE ))
851 return SWP_NOSIZE | SWP_NOMOVE;
853 if (IsIconic( hwnd ))
855 if (cmd == SW_MINIMIZE) return SWP_NOSIZE | SWP_NOMOVE;
856 if (!SendMessageW( hwnd, WM_QUERYOPEN, 0, 0 )) return SWP_NOSIZE | SWP_NOMOVE;
857 swpFlags |= SWP_NOCOPYBITS;
860 switch( cmd )
862 case SW_MINIMIZE:
863 if (!(wndPtr = WIN_GetPtr( hwnd )) || wndPtr == WND_OTHER_PROCESS) return 0;
864 if( wndPtr->dwStyle & WS_MAXIMIZE) wndPtr->flags |= WIN_RESTORE_MAX;
865 else wndPtr->flags &= ~WIN_RESTORE_MAX;
866 WIN_ReleasePtr( wndPtr );
868 WIN_SetStyle( hwnd, WS_MINIMIZE, WS_MAXIMIZE );
870 X11DRV_set_iconic_state( hwnd );
872 wpl.ptMinPosition = WINPOS_FindIconPos( hwnd, wpl.ptMinPosition );
874 SetRect( rect, wpl.ptMinPosition.x, wpl.ptMinPosition.y,
875 GetSystemMetrics(SM_CXICON), GetSystemMetrics(SM_CYICON) );
876 swpFlags |= SWP_NOCOPYBITS;
877 break;
879 case SW_MAXIMIZE:
880 WINPOS_GetMinMaxInfo( hwnd, &size, &wpl.ptMaxPosition, NULL, NULL );
882 old_style = WIN_SetStyle( hwnd, WS_MAXIMIZE, WS_MINIMIZE );
883 if (old_style & WS_MINIMIZE)
885 WINPOS_ShowIconTitle( hwnd, FALSE );
886 X11DRV_set_iconic_state( hwnd );
888 SetRect( rect, wpl.ptMaxPosition.x, wpl.ptMaxPosition.y, size.x, size.y );
889 break;
891 case SW_RESTORE:
892 old_style = WIN_SetStyle( hwnd, 0, WS_MINIMIZE | WS_MAXIMIZE );
893 if (old_style & WS_MINIMIZE)
895 BOOL restore_max;
897 WINPOS_ShowIconTitle( hwnd, FALSE );
898 X11DRV_set_iconic_state( hwnd );
900 if (!(wndPtr = WIN_GetPtr( hwnd )) || wndPtr == WND_OTHER_PROCESS) return 0;
901 restore_max = (wndPtr->flags & WIN_RESTORE_MAX) != 0;
902 WIN_ReleasePtr( wndPtr );
903 if (restore_max)
905 /* Restore to maximized position */
906 WINPOS_GetMinMaxInfo( hwnd, &size, &wpl.ptMaxPosition, NULL, NULL);
907 WIN_SetStyle( hwnd, WS_MAXIMIZE, 0 );
908 SetRect( rect, wpl.ptMaxPosition.x, wpl.ptMaxPosition.y, size.x, size.y );
909 break;
912 else if (!(old_style & WS_MAXIMIZE)) break;
914 /* Restore to normal position */
916 *rect = wpl.rcNormalPosition;
917 rect->right -= rect->left;
918 rect->bottom -= rect->top;
920 break;
923 return swpFlags;
927 /***********************************************************************
928 * ShowWindow (X11DRV.@)
930 BOOL X11DRV_ShowWindow( HWND hwnd, INT cmd )
932 WND *wndPtr;
933 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
934 BOOL wasVisible = (style & WS_VISIBLE) != 0;
935 BOOL showFlag = TRUE;
936 RECT newPos = {0, 0, 0, 0};
937 UINT swp = 0;
939 if (hwnd == GetDesktopWindow()) return FALSE;
941 TRACE("hwnd=%p, cmd=%d, wasVisible %d\n", hwnd, cmd, wasVisible);
943 switch(cmd)
945 case SW_HIDE:
946 if (!wasVisible) return FALSE;
947 showFlag = FALSE;
948 swp |= SWP_HIDEWINDOW | SWP_NOSIZE | SWP_NOMOVE;
949 if (hwnd != GetActiveWindow())
950 swp |= SWP_NOACTIVATE | SWP_NOZORDER;
951 break;
953 case SW_SHOWMINNOACTIVE:
954 swp |= SWP_NOACTIVATE | SWP_NOZORDER;
955 /* fall through */
956 case SW_SHOWMINIMIZED:
957 case SW_FORCEMINIMIZE: /* FIXME: Does not work if thread is hung. */
958 swp |= SWP_SHOWWINDOW;
959 /* fall through */
960 case SW_MINIMIZE:
961 swp |= SWP_FRAMECHANGED;
962 if( !(style & WS_MINIMIZE) )
963 swp |= WINPOS_MinMaximize( hwnd, SW_MINIMIZE, &newPos );
964 else swp |= SWP_NOSIZE | SWP_NOMOVE;
965 break;
967 case SW_SHOWMAXIMIZED: /* same as SW_MAXIMIZE */
968 swp |= SWP_SHOWWINDOW | SWP_FRAMECHANGED;
969 if( !(style & WS_MAXIMIZE) )
970 swp |= WINPOS_MinMaximize( hwnd, SW_MAXIMIZE, &newPos );
971 else swp |= SWP_NOSIZE | SWP_NOMOVE;
972 break;
974 case SW_SHOWNA:
975 swp |= SWP_NOACTIVATE | SWP_SHOWWINDOW | SWP_NOSIZE | SWP_NOMOVE;
976 if (style & WS_CHILD) swp |= SWP_NOZORDER;
977 break;
978 case SW_SHOW:
979 if (wasVisible) return TRUE;
980 swp |= SWP_SHOWWINDOW | SWP_NOSIZE | SWP_NOMOVE;
981 break;
983 case SW_RESTORE:
984 swp |= SWP_FRAMECHANGED;
985 /* fall through */
986 case SW_SHOWNOACTIVATE:
987 swp |= SWP_NOACTIVATE | SWP_NOZORDER;
988 /* fall through */
989 case SW_SHOWNORMAL: /* same as SW_NORMAL: */
990 case SW_SHOWDEFAULT: /* FIXME: should have its own handler */
991 swp |= SWP_SHOWWINDOW;
993 if (style & (WS_MINIMIZE | WS_MAXIMIZE))
994 swp |= WINPOS_MinMaximize( hwnd, SW_RESTORE, &newPos );
995 else swp |= SWP_NOSIZE | SWP_NOMOVE;
996 break;
999 if ((showFlag != wasVisible || cmd == SW_SHOWNA) && cmd != SW_SHOWMAXIMIZED)
1001 SendMessageW( hwnd, WM_SHOWWINDOW, showFlag, 0 );
1002 if (!IsWindow( hwnd )) return wasVisible;
1005 /* ShowWindow won't activate a not being maximized child window */
1006 if ((style & WS_CHILD) && cmd != SW_MAXIMIZE)
1007 swp |= SWP_NOACTIVATE | SWP_NOZORDER;
1009 SetWindowPos( hwnd, HWND_TOP, newPos.left, newPos.top,
1010 newPos.right, newPos.bottom, LOWORD(swp) );
1011 if (cmd == SW_HIDE)
1013 HWND hFocus;
1015 /* FIXME: This will cause the window to be activated irrespective
1016 * of whether it is owned by the same thread. Has to be done
1017 * asynchronously.
1020 if (hwnd == GetActiveWindow())
1021 WINPOS_ActivateOtherWindow(hwnd);
1023 /* Revert focus to parent */
1024 hFocus = GetFocus();
1025 if (hwnd == hFocus || IsChild(hwnd, hFocus))
1027 HWND parent = GetAncestor(hwnd, GA_PARENT);
1028 if (parent == GetDesktopWindow()) parent = 0;
1029 SetFocus(parent);
1033 if (IsIconic(hwnd)) WINPOS_ShowIconTitle( hwnd, TRUE );
1035 if (!(wndPtr = WIN_GetPtr( hwnd )) || wndPtr == WND_OTHER_PROCESS) return wasVisible;
1037 if (wndPtr->flags & WIN_NEED_SIZE)
1039 /* should happen only in CreateWindowEx() */
1040 int wParam = SIZE_RESTORED;
1041 RECT client = wndPtr->rectClient;
1043 wndPtr->flags &= ~WIN_NEED_SIZE;
1044 if (wndPtr->dwStyle & WS_MAXIMIZE) wParam = SIZE_MAXIMIZED;
1045 else if (wndPtr->dwStyle & WS_MINIMIZE) wParam = SIZE_MINIMIZED;
1046 WIN_ReleasePtr( wndPtr );
1048 SendMessageW( hwnd, WM_SIZE, wParam,
1049 MAKELONG( client.right - client.left, client.bottom - client.top ));
1050 SendMessageW( hwnd, WM_MOVE, 0, MAKELONG( client.left, client.top ));
1052 else WIN_ReleasePtr( wndPtr );
1054 return wasVisible;
1058 /**********************************************************************
1059 * X11DRV_MapNotify
1061 void X11DRV_MapNotify( HWND hwnd, XEvent *event )
1063 struct x11drv_win_data *data;
1064 HWND hwndFocus = GetFocus();
1065 WND *win;
1067 if (!(data = X11DRV_get_win_data( hwnd ))) return;
1069 if (!(win = WIN_GetPtr( hwnd ))) return;
1071 if (data->managed && (win->dwStyle & WS_VISIBLE) && (win->dwStyle & WS_MINIMIZE))
1073 int x, y;
1074 unsigned int width, height, border, depth;
1075 Window root, top;
1076 RECT rect;
1077 LONG style = WS_VISIBLE;
1079 /* FIXME: hack */
1080 wine_tsx11_lock();
1081 XGetGeometry( event->xmap.display, data->whole_window, &root, &x, &y, &width, &height,
1082 &border, &depth );
1083 XTranslateCoordinates( event->xmap.display, data->whole_window, root, 0, 0, &x, &y, &top );
1084 wine_tsx11_unlock();
1085 rect.left = x;
1086 rect.top = y;
1087 rect.right = x + width;
1088 rect.bottom = y + height;
1089 X11DRV_X_to_window_rect( data, &rect );
1091 invalidate_dce( hwnd, &data->window_rect );
1093 if (win->flags & WIN_RESTORE_MAX) style |= WS_MAXIMIZE;
1094 WIN_SetStyle( hwnd, style, WS_MINIMIZE );
1095 WIN_ReleasePtr( win );
1097 SendMessageA( hwnd, WM_SHOWWINDOW, SW_RESTORE, 0 );
1098 SetWindowPos( hwnd, 0, rect.left, rect.top, rect.right-rect.left, rect.bottom-rect.top,
1099 SWP_NOZORDER | SWP_WINE_NOHOSTMOVE );
1101 else WIN_ReleasePtr( win );
1102 if (hwndFocus && IsChild( hwnd, hwndFocus )) X11DRV_SetFocus(hwndFocus); /* FIXME */
1106 /**********************************************************************
1107 * X11DRV_UnmapNotify
1109 void X11DRV_UnmapNotify( HWND hwnd, XEvent *event )
1111 struct x11drv_win_data *data;
1112 WND *win;
1114 if (!(data = X11DRV_get_win_data( hwnd ))) return;
1116 if (!(win = WIN_GetPtr( hwnd ))) return;
1118 if ((win->dwStyle & WS_VISIBLE) && data->managed &&
1119 X11DRV_is_window_rect_mapped( &win->rectWindow ))
1121 if (win->dwStyle & WS_MAXIMIZE)
1122 win->flags |= WIN_RESTORE_MAX;
1123 else
1124 win->flags &= ~WIN_RESTORE_MAX;
1126 WIN_SetStyle( hwnd, WS_MINIMIZE, WS_MAXIMIZE );
1127 WIN_ReleasePtr( win );
1129 EndMenu();
1130 SendMessageA( hwnd, WM_SHOWWINDOW, SW_MINIMIZE, 0 );
1131 SetWindowPos( hwnd, 0, 0, 0, GetSystemMetrics(SM_CXICON), GetSystemMetrics(SM_CYICON),
1132 SWP_NOMOVE | SWP_NOACTIVATE | SWP_NOZORDER | SWP_WINE_NOHOSTMOVE );
1134 else WIN_ReleasePtr( win );
1138 /***********************************************************************
1139 * X11DRV_handle_desktop_resize
1141 void X11DRV_handle_desktop_resize( unsigned int width, unsigned int height )
1143 RECT rect;
1144 HWND hwnd = GetDesktopWindow();
1146 screen_width = width;
1147 screen_height = height;
1148 TRACE("desktop %p change to (%dx%d)\n", hwnd, width, height);
1149 SetRect( &rect, 0, 0, width, height );
1150 X11DRV_set_window_pos( hwnd, 0, &rect, &rect, SWP_NOZORDER|SWP_NOMOVE|SWP_WINE_NOHOSTMOVE, NULL );
1151 SendMessageTimeoutW( HWND_BROADCAST, WM_DISPLAYCHANGE, screen_depth,
1152 MAKELPARAM( width, height ), SMTO_ABORTIFHUNG, 2000, NULL );
1156 /***********************************************************************
1157 * X11DRV_ConfigureNotify
1159 void X11DRV_ConfigureNotify( HWND hwnd, XEvent *xev )
1161 XConfigureEvent *event = &xev->xconfigure;
1162 struct x11drv_win_data *data;
1163 RECT rect;
1164 UINT flags;
1165 int cx, cy, x = event->x, y = event->y;
1167 if (!hwnd) return;
1168 if (!(data = X11DRV_get_win_data( hwnd ))) return;
1170 /* Get geometry */
1172 if (!event->send_event) /* normal event, need to map coordinates to the root */
1174 Window child;
1175 wine_tsx11_lock();
1176 XTranslateCoordinates( event->display, data->whole_window, root_window,
1177 0, 0, &x, &y, &child );
1178 wine_tsx11_unlock();
1180 rect.left = x;
1181 rect.top = y;
1182 rect.right = x + event->width;
1183 rect.bottom = y + event->height;
1184 TRACE( "win %p new X rect %ld,%ld,%ldx%ld (event %d,%d,%dx%d)\n",
1185 hwnd, rect.left, rect.top, rect.right-rect.left, rect.bottom-rect.top,
1186 event->x, event->y, event->width, event->height );
1187 X11DRV_X_to_window_rect( data, &rect );
1189 x = rect.left;
1190 y = rect.top;
1191 cx = rect.right - rect.left;
1192 cy = rect.bottom - rect.top;
1193 flags = SWP_NOACTIVATE | SWP_NOZORDER | SWP_WINE_NOHOSTMOVE;
1195 /* Compare what has changed */
1197 GetWindowRect( hwnd, &rect );
1198 if (rect.left == x && rect.top == y) flags |= SWP_NOMOVE;
1199 else
1200 TRACE( "%p moving from (%ld,%ld) to (%d,%d)\n",
1201 hwnd, rect.left, rect.top, x, y );
1203 if ((rect.right - rect.left == cx && rect.bottom - rect.top == cy) ||
1204 IsIconic(hwnd) ||
1205 (IsRectEmpty( &rect ) && event->width == 1 && event->height == 1))
1207 if (flags & SWP_NOMOVE) return; /* if nothing changed, don't do anything */
1208 flags |= SWP_NOSIZE;
1210 else
1211 TRACE( "%p resizing from (%ldx%ld) to (%dx%d)\n",
1212 hwnd, rect.right - rect.left, rect.bottom - rect.top, cx, cy );
1214 SetWindowPos( hwnd, 0, x, y, cx, cy, flags );
1218 /***********************************************************************
1219 * SetWindowRgn (X11DRV.@)
1221 * Assign specified region to window (for non-rectangular windows)
1223 int X11DRV_SetWindowRgn( HWND hwnd, HRGN hrgn, BOOL redraw )
1225 struct x11drv_win_data *data;
1227 if (!(data = X11DRV_get_win_data( hwnd )))
1229 if (IsWindow( hwnd ))
1230 FIXME( "not supported on other thread window %p\n", hwnd );
1231 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
1232 return FALSE;
1235 #ifdef HAVE_LIBXSHAPE
1236 if (data->whole_window)
1238 Display *display = thread_display();
1240 if (!hrgn)
1242 wine_tsx11_lock();
1243 XShapeCombineMask( display, data->whole_window,
1244 ShapeBounding, 0, 0, None, ShapeSet );
1245 wine_tsx11_unlock();
1247 else
1249 RGNDATA *pRegionData = X11DRV_GetRegionData( hrgn, 0 );
1250 if (pRegionData)
1252 wine_tsx11_lock();
1253 XShapeCombineRectangles( display, data->whole_window, ShapeBounding,
1254 data->window_rect.left - data->whole_rect.left,
1255 data->window_rect.top - data->whole_rect.top,
1256 (XRectangle *)pRegionData->Buffer,
1257 pRegionData->rdh.nCount,
1258 ShapeSet, YXBanded );
1259 wine_tsx11_unlock();
1260 HeapFree(GetProcessHeap(), 0, pRegionData);
1264 #endif /* HAVE_LIBXSHAPE */
1266 invalidate_dce( hwnd, &data->window_rect );
1267 return TRUE;
1271 /***********************************************************************
1272 * draw_moving_frame
1274 * Draw the frame used when moving or resizing window.
1276 * FIXME: This causes problems in Win95 mode. (why?)
1278 static void draw_moving_frame( HDC hdc, RECT *rect, BOOL thickframe )
1280 if (thickframe)
1282 const int width = GetSystemMetrics(SM_CXFRAME);
1283 const int height = GetSystemMetrics(SM_CYFRAME);
1285 HBRUSH hbrush = SelectObject( hdc, GetStockObject( GRAY_BRUSH ) );
1286 PatBlt( hdc, rect->left, rect->top,
1287 rect->right - rect->left - width, height, PATINVERT );
1288 PatBlt( hdc, rect->left, rect->top + height, width,
1289 rect->bottom - rect->top - height, PATINVERT );
1290 PatBlt( hdc, rect->left + width, rect->bottom - 1,
1291 rect->right - rect->left - width, -height, PATINVERT );
1292 PatBlt( hdc, rect->right - 1, rect->top, -width,
1293 rect->bottom - rect->top - height, PATINVERT );
1294 SelectObject( hdc, hbrush );
1296 else DrawFocusRect( hdc, rect );
1300 /***********************************************************************
1301 * start_size_move
1303 * Initialisation of a move or resize, when initiatied from a menu choice.
1304 * Return hit test code for caption or sizing border.
1306 static LONG start_size_move( HWND hwnd, WPARAM wParam, POINT *capturePoint, LONG style )
1308 LONG hittest = 0;
1309 POINT pt;
1310 MSG msg;
1311 RECT rectWindow;
1313 GetWindowRect( hwnd, &rectWindow );
1315 if ((wParam & 0xfff0) == SC_MOVE)
1317 /* Move pointer at the center of the caption */
1318 RECT rect = rectWindow;
1319 /* Note: to be exactly centered we should take the different types
1320 * of border into account, but it shouldn't make more that a few pixels
1321 * of difference so let's not bother with that */
1322 rect.top += GetSystemMetrics(SM_CYBORDER);
1323 if (style & WS_SYSMENU)
1324 rect.left += GetSystemMetrics(SM_CXSIZE) + 1;
1325 if (style & WS_MINIMIZEBOX)
1326 rect.right -= GetSystemMetrics(SM_CXSIZE) + 1;
1327 if (style & WS_MAXIMIZEBOX)
1328 rect.right -= GetSystemMetrics(SM_CXSIZE) + 1;
1329 pt.x = (rect.right + rect.left) / 2;
1330 pt.y = rect.top + GetSystemMetrics(SM_CYSIZE)/2;
1331 hittest = HTCAPTION;
1332 *capturePoint = pt;
1334 else /* SC_SIZE */
1336 pt.x = pt.y = 0;
1337 while(!hittest)
1339 GetMessageW( &msg, 0, WM_KEYFIRST, WM_MOUSELAST );
1340 if (CallMsgFilterW( &msg, MSGF_SIZE )) continue;
1342 switch(msg.message)
1344 case WM_MOUSEMOVE:
1345 pt = msg.pt;
1346 hittest = SendMessageW( hwnd, WM_NCHITTEST, 0, MAKELONG( pt.x, pt.y ) );
1347 if ((hittest < HTLEFT) || (hittest > HTBOTTOMRIGHT)) hittest = 0;
1348 break;
1350 case WM_LBUTTONUP:
1351 return 0;
1353 case WM_KEYDOWN:
1354 switch(msg.wParam)
1356 case VK_UP:
1357 hittest = HTTOP;
1358 pt.x =(rectWindow.left+rectWindow.right)/2;
1359 pt.y = rectWindow.top + GetSystemMetrics(SM_CYFRAME) / 2;
1360 break;
1361 case VK_DOWN:
1362 hittest = HTBOTTOM;
1363 pt.x =(rectWindow.left+rectWindow.right)/2;
1364 pt.y = rectWindow.bottom - GetSystemMetrics(SM_CYFRAME) / 2;
1365 break;
1366 case VK_LEFT:
1367 hittest = HTLEFT;
1368 pt.x = rectWindow.left + GetSystemMetrics(SM_CXFRAME) / 2;
1369 pt.y =(rectWindow.top+rectWindow.bottom)/2;
1370 break;
1371 case VK_RIGHT:
1372 hittest = HTRIGHT;
1373 pt.x = rectWindow.right - GetSystemMetrics(SM_CXFRAME) / 2;
1374 pt.y =(rectWindow.top+rectWindow.bottom)/2;
1375 break;
1376 case VK_RETURN:
1377 case VK_ESCAPE: return 0;
1381 *capturePoint = pt;
1383 SetCursorPos( pt.x, pt.y );
1384 SendMessageW( hwnd, WM_SETCURSOR, (WPARAM)hwnd, MAKELONG( hittest, WM_MOUSEMOVE ));
1385 return hittest;
1389 /***********************************************************************
1390 * set_movesize_capture
1392 static void set_movesize_capture( HWND hwnd )
1394 HWND previous = 0;
1396 SERVER_START_REQ( set_capture_window )
1398 req->handle = hwnd;
1399 req->flags = CAPTURE_MOVESIZE;
1400 if (!wine_server_call_err( req ))
1402 previous = reply->previous;
1403 hwnd = reply->full_handle;
1406 SERVER_END_REQ;
1407 if (previous && previous != hwnd)
1408 SendMessageW( previous, WM_CAPTURECHANGED, 0, (LPARAM)hwnd );
1411 /***********************************************************************
1412 * X11DRV_WMMoveResizeWindow
1414 * Tells the window manager to initiate a move or resize operation.
1416 * SEE
1417 * http://freedesktop.org/Standards/wm-spec/1.3/ar01s04.html
1418 * or search for "_NET_WM_MOVERESIZE"
1420 static void X11DRV_WMMoveResizeWindow( HWND hwnd, int x, int y, int dir )
1422 XEvent xev;
1423 Display *display = thread_display();
1425 xev.xclient.type = ClientMessage;
1426 xev.xclient.window = X11DRV_get_whole_window(hwnd);
1427 xev.xclient.message_type = x11drv_atom(_NET_WM_MOVERESIZE);
1428 xev.xclient.serial = 0;
1429 xev.xclient.display = display;
1430 xev.xclient.send_event = True;
1431 xev.xclient.format = 32;
1432 xev.xclient.data.l[0] = x; /* x coord */
1433 xev.xclient.data.l[1] = y; /* y coord */
1434 xev.xclient.data.l[2] = dir; /* direction */
1435 xev.xclient.data.l[3] = 1; /* button */
1436 xev.xclient.data.l[4] = 0; /* unused */
1438 /* need to ungrab the pointer that may have been automatically grabbed
1439 * with a ButtonPress event */
1440 wine_tsx11_lock();
1441 XUngrabPointer( display, CurrentTime );
1442 XSendEvent(display, root_window, False, SubstructureNotifyMask, &xev);
1443 wine_tsx11_unlock();
1446 /***********************************************************************
1447 * SysCommandSizeMove (X11DRV.@)
1449 * Perform SC_MOVE and SC_SIZE commands.
1451 void X11DRV_SysCommandSizeMove( HWND hwnd, WPARAM wParam )
1453 MSG msg;
1454 RECT sizingRect, mouseRect, origRect;
1455 HDC hdc;
1456 HWND parent;
1457 LONG hittest = (LONG)(wParam & 0x0f);
1458 WPARAM syscommand = wParam & 0xfff0;
1459 HCURSOR hDragCursor = 0, hOldCursor = 0;
1460 POINT minTrack, maxTrack;
1461 POINT capturePoint, pt;
1462 LONG style = GetWindowLongA( hwnd, GWL_STYLE );
1463 BOOL thickframe = HAS_THICKFRAME( style );
1464 BOOL iconic = style & WS_MINIMIZE;
1465 BOOL moved = FALSE;
1466 DWORD dwPoint = GetMessagePos ();
1467 BOOL DragFullWindows = FALSE;
1468 BOOL grab;
1469 Window parent_win, whole_win;
1470 Display *old_gdi_display = NULL;
1471 struct x11drv_thread_data *thread_data = x11drv_thread_data();
1472 struct x11drv_win_data *data;
1474 pt.x = (short)LOWORD(dwPoint);
1475 pt.y = (short)HIWORD(dwPoint);
1476 capturePoint = pt;
1478 if (IsZoomed(hwnd) || !IsWindowVisible(hwnd)) return;
1480 if (!(data = X11DRV_get_win_data( hwnd ))) return;
1482 /* if we are managed then we let the WM do all the work */
1483 if (data->managed)
1485 int dir;
1486 if (syscommand == SC_MOVE)
1488 if (!hittest) dir = _NET_WM_MOVERESIZE_MOVE_KEYBOARD;
1489 else dir = _NET_WM_MOVERESIZE_MOVE;
1491 else if (!hittest) dir = _NET_WM_MOVERESIZE_SIZE_KEYBOARD;
1492 else
1493 switch (hittest)
1495 case WMSZ_LEFT: dir = _NET_WM_MOVERESIZE_SIZE_LEFT; break;
1496 case WMSZ_RIGHT: dir = _NET_WM_MOVERESIZE_SIZE_RIGHT; break;
1497 case WMSZ_TOP: dir = _NET_WM_MOVERESIZE_SIZE_TOP; break;
1498 case WMSZ_TOPLEFT: dir = _NET_WM_MOVERESIZE_SIZE_TOPLEFT; break;
1499 case WMSZ_TOPRIGHT: dir = _NET_WM_MOVERESIZE_SIZE_TOPRIGHT; break;
1500 case WMSZ_BOTTOM: dir = _NET_WM_MOVERESIZE_SIZE_BOTTOM; break;
1501 case WMSZ_BOTTOMLEFT: dir = _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT; break;
1502 case WMSZ_BOTTOMRIGHT: dir = _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT; break;
1503 default:
1504 ERR("Invalid hittest value: %ld\n", hittest);
1505 dir = _NET_WM_MOVERESIZE_SIZE_KEYBOARD;
1507 X11DRV_WMMoveResizeWindow( hwnd, pt.x, pt.y, dir );
1508 return;
1511 SystemParametersInfoA(SPI_GETDRAGFULLWINDOWS, 0, &DragFullWindows, 0);
1513 if (syscommand == SC_MOVE)
1515 if (!hittest) hittest = start_size_move( hwnd, wParam, &capturePoint, style );
1516 if (!hittest) return;
1518 else /* SC_SIZE */
1520 if ( hittest && (syscommand != SC_MOUSEMENU) )
1521 hittest += (HTLEFT - WMSZ_LEFT);
1522 else
1524 set_movesize_capture( hwnd );
1525 hittest = start_size_move( hwnd, wParam, &capturePoint, style );
1526 if (!hittest)
1528 set_movesize_capture(0);
1529 return;
1534 /* Get min/max info */
1536 WINPOS_GetMinMaxInfo( hwnd, NULL, NULL, &minTrack, &maxTrack );
1537 GetWindowRect( hwnd, &sizingRect );
1538 if (style & WS_CHILD)
1540 parent = GetParent(hwnd);
1541 /* make sizing rect relative to parent */
1542 MapWindowPoints( 0, parent, (POINT*)&sizingRect, 2 );
1543 GetClientRect( parent, &mouseRect );
1545 else
1547 parent = 0;
1548 SetRect(&mouseRect, 0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN));
1550 origRect = sizingRect;
1552 if (ON_LEFT_BORDER(hittest))
1554 mouseRect.left = max( mouseRect.left, sizingRect.right-maxTrack.x );
1555 mouseRect.right = min( mouseRect.right, sizingRect.right-minTrack.x );
1557 else if (ON_RIGHT_BORDER(hittest))
1559 mouseRect.left = max( mouseRect.left, sizingRect.left+minTrack.x );
1560 mouseRect.right = min( mouseRect.right, sizingRect.left+maxTrack.x );
1562 if (ON_TOP_BORDER(hittest))
1564 mouseRect.top = max( mouseRect.top, sizingRect.bottom-maxTrack.y );
1565 mouseRect.bottom = min( mouseRect.bottom,sizingRect.bottom-minTrack.y);
1567 else if (ON_BOTTOM_BORDER(hittest))
1569 mouseRect.top = max( mouseRect.top, sizingRect.top+minTrack.y );
1570 mouseRect.bottom = min( mouseRect.bottom, sizingRect.top+maxTrack.y );
1572 if (parent) MapWindowPoints( parent, 0, (LPPOINT)&mouseRect, 2 );
1574 /* Retrieve a default cache DC (without using the window style) */
1575 hdc = GetDCEx( parent, 0, DCX_CACHE );
1577 if( iconic ) /* create a cursor for dragging */
1579 hDragCursor = (HCURSOR)GetClassLongPtrW( hwnd, GCLP_HICON);
1580 if( !hDragCursor ) hDragCursor = (HCURSOR)SendMessageW( hwnd, WM_QUERYDRAGICON, 0, 0L);
1581 if( !hDragCursor ) iconic = FALSE;
1584 /* repaint the window before moving it around */
1585 RedrawWindow( hwnd, NULL, 0, RDW_UPDATENOW | RDW_ALLCHILDREN );
1587 SendMessageA( hwnd, WM_ENTERSIZEMOVE, 0, 0 );
1588 set_movesize_capture( hwnd );
1590 /* grab the server only when moving top-level windows without desktop */
1591 grab = (!DragFullWindows && !parent && (root_window == DefaultRootWindow(gdi_display)));
1593 if (grab)
1595 wine_tsx11_lock();
1596 XSync( gdi_display, False );
1597 XGrabServer( thread_data->display );
1598 XSync( thread_data->display, False );
1599 /* switch gdi display to the thread display, since the server is grabbed */
1600 old_gdi_display = gdi_display;
1601 gdi_display = thread_data->display;
1602 wine_tsx11_unlock();
1604 whole_win = X11DRV_get_whole_window( GetAncestor(hwnd,GA_ROOT) );
1605 parent_win = parent ? X11DRV_get_whole_window( GetAncestor(parent,GA_ROOT) ) : root_window;
1607 wine_tsx11_lock();
1608 XGrabPointer( thread_data->display, whole_win, False,
1609 PointerMotionMask | ButtonPressMask | ButtonReleaseMask,
1610 GrabModeAsync, GrabModeAsync, parent_win, None, CurrentTime );
1611 wine_tsx11_unlock();
1612 thread_data->grab_window = whole_win;
1614 while(1)
1616 int dx = 0, dy = 0;
1618 if (!GetMessageW( &msg, 0, WM_KEYFIRST, WM_MOUSELAST )) break;
1619 if (CallMsgFilterW( &msg, MSGF_SIZE )) continue;
1621 /* Exit on button-up, Return, or Esc */
1622 if ((msg.message == WM_LBUTTONUP) ||
1623 ((msg.message == WM_KEYDOWN) &&
1624 ((msg.wParam == VK_RETURN) || (msg.wParam == VK_ESCAPE)))) break;
1626 if ((msg.message != WM_KEYDOWN) && (msg.message != WM_MOUSEMOVE))
1627 continue; /* We are not interested in other messages */
1629 pt = msg.pt;
1631 if (msg.message == WM_KEYDOWN) switch(msg.wParam)
1633 case VK_UP: pt.y -= 8; break;
1634 case VK_DOWN: pt.y += 8; break;
1635 case VK_LEFT: pt.x -= 8; break;
1636 case VK_RIGHT: pt.x += 8; break;
1639 pt.x = max( pt.x, mouseRect.left );
1640 pt.x = min( pt.x, mouseRect.right );
1641 pt.y = max( pt.y, mouseRect.top );
1642 pt.y = min( pt.y, mouseRect.bottom );
1644 dx = pt.x - capturePoint.x;
1645 dy = pt.y - capturePoint.y;
1647 if (dx || dy)
1649 if( !moved )
1651 moved = TRUE;
1653 if( iconic ) /* ok, no system popup tracking */
1655 hOldCursor = SetCursor(hDragCursor);
1656 ShowCursor( TRUE );
1657 WINPOS_ShowIconTitle( hwnd, FALSE );
1659 else if(!DragFullWindows)
1660 draw_moving_frame( hdc, &sizingRect, thickframe );
1663 if (msg.message == WM_KEYDOWN) SetCursorPos( pt.x, pt.y );
1664 else
1666 RECT newRect = sizingRect;
1667 WPARAM wpSizingHit = 0;
1669 if (hittest == HTCAPTION) OffsetRect( &newRect, dx, dy );
1670 if (ON_LEFT_BORDER(hittest)) newRect.left += dx;
1671 else if (ON_RIGHT_BORDER(hittest)) newRect.right += dx;
1672 if (ON_TOP_BORDER(hittest)) newRect.top += dy;
1673 else if (ON_BOTTOM_BORDER(hittest)) newRect.bottom += dy;
1674 if(!iconic && !DragFullWindows) draw_moving_frame( hdc, &sizingRect, thickframe );
1675 capturePoint = pt;
1677 /* determine the hit location */
1678 if (hittest >= HTLEFT && hittest <= HTBOTTOMRIGHT)
1679 wpSizingHit = WMSZ_LEFT + (hittest - HTLEFT);
1680 SendMessageA( hwnd, WM_SIZING, wpSizingHit, (LPARAM)&newRect );
1682 if (!iconic)
1684 if(!DragFullWindows)
1685 draw_moving_frame( hdc, &newRect, thickframe );
1686 else
1687 SetWindowPos( hwnd, 0, newRect.left, newRect.top,
1688 newRect.right - newRect.left,
1689 newRect.bottom - newRect.top,
1690 ( hittest == HTCAPTION ) ? SWP_NOSIZE : 0 );
1692 sizingRect = newRect;
1697 set_movesize_capture(0);
1698 if( iconic )
1700 if( moved ) /* restore cursors, show icon title later on */
1702 ShowCursor( FALSE );
1703 SetCursor( hOldCursor );
1706 else if (moved && !DragFullWindows)
1707 draw_moving_frame( hdc, &sizingRect, thickframe );
1709 ReleaseDC( parent, hdc );
1711 wine_tsx11_lock();
1712 XUngrabPointer( thread_data->display, CurrentTime );
1713 if (grab)
1715 XSync( thread_data->display, False );
1716 XUngrabServer( thread_data->display );
1717 XSync( thread_data->display, False );
1718 gdi_display = old_gdi_display;
1720 wine_tsx11_unlock();
1721 thread_data->grab_window = None;
1723 if (HOOK_CallHooks( WH_CBT, HCBT_MOVESIZE, (WPARAM)hwnd, (LPARAM)&sizingRect, TRUE ))
1724 moved = FALSE;
1726 SendMessageA( hwnd, WM_EXITSIZEMOVE, 0, 0 );
1727 SendMessageA( hwnd, WM_SETVISIBLE, !IsIconic(hwnd), 0L);
1729 /* window moved or resized */
1730 if (moved)
1732 /* if the moving/resizing isn't canceled call SetWindowPos
1733 * with the new position or the new size of the window
1735 if (!((msg.message == WM_KEYDOWN) && (msg.wParam == VK_ESCAPE)) )
1737 /* NOTE: SWP_NOACTIVATE prevents document window activation in Word 6 */
1738 if(!DragFullWindows)
1739 SetWindowPos( hwnd, 0, sizingRect.left, sizingRect.top,
1740 sizingRect.right - sizingRect.left,
1741 sizingRect.bottom - sizingRect.top,
1742 ( hittest == HTCAPTION ) ? SWP_NOSIZE : 0 );
1744 else
1745 { /* restore previous size/position */
1746 if(DragFullWindows)
1747 SetWindowPos( hwnd, 0, origRect.left, origRect.top,
1748 origRect.right - origRect.left,
1749 origRect.bottom - origRect.top,
1750 ( hittest == HTCAPTION ) ? SWP_NOSIZE : 0 );
1754 if (IsIconic(hwnd))
1756 /* Single click brings up the system menu when iconized */
1758 if( !moved )
1760 if(style & WS_SYSMENU )
1761 SendMessageA( hwnd, WM_SYSCOMMAND,
1762 SC_MOUSEMENU + HTSYSMENU, MAKELONG(pt.x,pt.y));
1764 else WINPOS_ShowIconTitle( hwnd, TRUE );