x11drv: Renamed the x11drv directory to winex11.drv.
[wine/multimedia.git] / dlls / winex11.drv / winpos.c
blob1da69d3453cd199873386cf56530acc2d93b8fb2
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "config.h"
24 #include <X11/Xlib.h>
25 #ifdef HAVE_LIBXSHAPE
26 #include <X11/extensions/shape.h>
27 #endif /* HAVE_LIBXSHAPE */
28 #include <stdarg.h>
30 #include "windef.h"
31 #include "winbase.h"
32 #include "wingdi.h"
33 #include "winuser.h"
34 #include "winerror.h"
35 #include "wownt32.h"
36 #include "wine/wingdi16.h"
38 #include "x11drv.h"
39 #include "win.h"
40 #include "winpos.h"
42 #include "wine/server.h"
43 #include "wine/debug.h"
45 WINE_DEFAULT_DEBUG_CHANNEL(x11drv);
47 #define SWP_AGG_NOPOSCHANGE \
48 (SWP_NOSIZE | SWP_NOMOVE | SWP_NOCLIENTSIZE | SWP_NOCLIENTMOVE | SWP_NOZORDER)
49 #define SWP_AGG_STATUSFLAGS \
50 (SWP_AGG_NOPOSCHANGE | SWP_FRAMECHANGED | SWP_HIDEWINDOW | SWP_SHOWWINDOW)
52 #define SWP_EX_NOCOPY 0x0001
53 #define SWP_EX_PAINTSELF 0x0002
54 #define SWP_EX_NONCLIENT 0x0004
56 #define HAS_THICKFRAME(style) \
57 (((style) & WS_THICKFRAME) && \
58 !(((style) & (WS_DLGFRAME|WS_BORDER)) == WS_DLGFRAME))
60 #define ON_LEFT_BORDER(hit) \
61 (((hit) == HTLEFT) || ((hit) == HTTOPLEFT) || ((hit) == HTBOTTOMLEFT))
62 #define ON_RIGHT_BORDER(hit) \
63 (((hit) == HTRIGHT) || ((hit) == HTTOPRIGHT) || ((hit) == HTBOTTOMRIGHT))
64 #define ON_TOP_BORDER(hit) \
65 (((hit) == HTTOP) || ((hit) == HTTOPLEFT) || ((hit) == HTTOPRIGHT))
66 #define ON_BOTTOM_BORDER(hit) \
67 (((hit) == HTBOTTOM) || ((hit) == HTBOTTOMLEFT) || ((hit) == HTBOTTOMRIGHT))
69 #define _NET_WM_MOVERESIZE_SIZE_TOPLEFT 0
70 #define _NET_WM_MOVERESIZE_SIZE_TOP 1
71 #define _NET_WM_MOVERESIZE_SIZE_TOPRIGHT 2
72 #define _NET_WM_MOVERESIZE_SIZE_RIGHT 3
73 #define _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT 4
74 #define _NET_WM_MOVERESIZE_SIZE_BOTTOM 5
75 #define _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT 6
76 #define _NET_WM_MOVERESIZE_SIZE_LEFT 7
77 #define _NET_WM_MOVERESIZE_MOVE 8 /* movement only */
78 #define _NET_WM_MOVERESIZE_SIZE_KEYBOARD 9 /* size via keyboard */
79 #define _NET_WM_MOVERESIZE_MOVE_KEYBOARD 10 /* move via keyboard */
82 /***********************************************************************
83 * X11DRV_Expose
85 void X11DRV_Expose( HWND hwnd, XEvent *xev )
87 XExposeEvent *event = &xev->xexpose;
88 RECT rect;
89 struct x11drv_win_data *data;
90 int flags = RDW_INVALIDATE | RDW_ERASE | RDW_ALLCHILDREN;
92 TRACE( "win %p (%lx) %d,%d %dx%d\n",
93 hwnd, event->window, event->x, event->y, event->width, event->height );
95 if (!(data = X11DRV_get_win_data( hwnd ))) return;
97 rect.left = event->x;
98 rect.top = event->y;
99 rect.right = rect.left + event->width;
100 rect.bottom = rect.top + event->height;
102 if (rect.left < data->client_rect.left ||
103 rect.top < data->client_rect.top ||
104 rect.right > data->client_rect.right ||
105 rect.bottom > data->client_rect.bottom) flags |= RDW_FRAME;
107 SERVER_START_REQ( update_window_zorder )
109 req->window = hwnd;
110 req->rect.left = rect.left + data->whole_rect.left;
111 req->rect.top = rect.top + data->whole_rect.top;
112 req->rect.right = rect.right + data->whole_rect.left;
113 req->rect.bottom = rect.bottom + data->whole_rect.top;
114 wine_server_call( req );
116 SERVER_END_REQ;
118 /* make position relative to client area instead of window */
119 OffsetRect( &rect, -data->client_rect.left, -data->client_rect.top );
120 RedrawWindow( hwnd, &rect, 0, flags );
124 /***********************************************************************
125 * SWP_DoWinPosChanging
127 static BOOL SWP_DoWinPosChanging( WINDOWPOS* pWinpos, RECT* pNewWindowRect, RECT* pNewClientRect )
129 WND *wndPtr;
131 /* Send WM_WINDOWPOSCHANGING message */
133 if (!(pWinpos->flags & SWP_NOSENDCHANGING))
134 SendMessageW( pWinpos->hwnd, WM_WINDOWPOSCHANGING, 0, (LPARAM)pWinpos );
136 if (!(wndPtr = WIN_GetPtr( pWinpos->hwnd )) ||
137 wndPtr == WND_OTHER_PROCESS || wndPtr == WND_DESKTOP) return FALSE;
139 /* Calculate new position and size */
141 *pNewWindowRect = wndPtr->rectWindow;
142 *pNewClientRect = (wndPtr->dwStyle & WS_MINIMIZE) ? wndPtr->rectWindow
143 : wndPtr->rectClient;
145 if (!(pWinpos->flags & SWP_NOSIZE))
147 pNewWindowRect->right = pNewWindowRect->left + pWinpos->cx;
148 pNewWindowRect->bottom = pNewWindowRect->top + pWinpos->cy;
150 if (!(pWinpos->flags & SWP_NOMOVE))
152 pNewWindowRect->left = pWinpos->x;
153 pNewWindowRect->top = pWinpos->y;
154 pNewWindowRect->right += pWinpos->x - wndPtr->rectWindow.left;
155 pNewWindowRect->bottom += pWinpos->y - wndPtr->rectWindow.top;
157 OffsetRect( pNewClientRect, pWinpos->x - wndPtr->rectWindow.left,
158 pWinpos->y - wndPtr->rectWindow.top );
160 pWinpos->flags |= SWP_NOCLIENTMOVE | SWP_NOCLIENTSIZE;
162 TRACE( "hwnd %p, after %p, swp %d,%d %dx%d flags %08x\n",
163 pWinpos->hwnd, pWinpos->hwndInsertAfter, pWinpos->x, pWinpos->y,
164 pWinpos->cx, pWinpos->cy, pWinpos->flags );
165 TRACE( "current %s style %08lx new %s\n",
166 wine_dbgstr_rect( &wndPtr->rectWindow ), wndPtr->dwStyle,
167 wine_dbgstr_rect( pNewWindowRect ));
169 WIN_ReleasePtr( wndPtr );
170 return TRUE;
174 /***********************************************************************
175 * get_valid_rects
177 * Compute the valid rects from the old and new client rect and WVR_* flags.
178 * Helper for WM_NCCALCSIZE handling.
180 static inline void get_valid_rects( const RECT *old_client, const RECT *new_client, UINT flags,
181 RECT *valid )
183 int cx, cy;
185 if (flags & WVR_REDRAW)
187 SetRectEmpty( &valid[0] );
188 SetRectEmpty( &valid[1] );
189 return;
192 if (flags & WVR_VALIDRECTS)
194 if (!IntersectRect( &valid[0], &valid[0], new_client ) ||
195 !IntersectRect( &valid[1], &valid[1], old_client ))
197 SetRectEmpty( &valid[0] );
198 SetRectEmpty( &valid[1] );
199 return;
201 flags = WVR_ALIGNLEFT | WVR_ALIGNTOP;
203 else
205 valid[0] = *new_client;
206 valid[1] = *old_client;
209 /* make sure the rectangles have the same size */
210 cx = min( valid[0].right - valid[0].left, valid[1].right - valid[1].left );
211 cy = min( valid[0].bottom - valid[0].top, valid[1].bottom - valid[1].top );
213 if (flags & WVR_ALIGNBOTTOM)
215 valid[0].top = valid[0].bottom - cy;
216 valid[1].top = valid[1].bottom - cy;
218 else
220 valid[0].bottom = valid[0].top + cy;
221 valid[1].bottom = valid[1].top + cy;
223 if (flags & WVR_ALIGNRIGHT)
225 valid[0].left = valid[0].right - cx;
226 valid[1].left = valid[1].right - cx;
228 else
230 valid[0].right = valid[0].left + cx;
231 valid[1].right = valid[1].left + cx;
236 /***********************************************************************
237 * SWP_DoNCCalcSize
239 static UINT SWP_DoNCCalcSize( WINDOWPOS* pWinpos, const RECT* pNewWindowRect, RECT* pNewClientRect,
240 RECT *validRects )
242 UINT wvrFlags = 0;
243 WND *wndPtr;
245 if (!(wndPtr = WIN_GetPtr( pWinpos->hwnd )) || wndPtr == WND_OTHER_PROCESS) return 0;
247 /* Send WM_NCCALCSIZE message to get new client area */
248 if( (pWinpos->flags & (SWP_FRAMECHANGED | SWP_NOSIZE)) != SWP_NOSIZE )
250 NCCALCSIZE_PARAMS params;
251 WINDOWPOS winposCopy;
253 params.rgrc[0] = *pNewWindowRect;
254 params.rgrc[1] = wndPtr->rectWindow;
255 params.rgrc[2] = wndPtr->rectClient;
256 params.lppos = &winposCopy;
257 winposCopy = *pWinpos;
258 WIN_ReleasePtr( wndPtr );
260 wvrFlags = SendMessageW( pWinpos->hwnd, WM_NCCALCSIZE, TRUE, (LPARAM)&params );
262 *pNewClientRect = params.rgrc[0];
264 if (!(wndPtr = WIN_GetPtr( pWinpos->hwnd )) || wndPtr == WND_OTHER_PROCESS) return 0;
266 TRACE( "hwnd %p old win %s old client %s new win %s new client %s\n", pWinpos->hwnd,
267 wine_dbgstr_rect(&wndPtr->rectWindow), wine_dbgstr_rect(&wndPtr->rectClient),
268 wine_dbgstr_rect(pNewWindowRect), wine_dbgstr_rect(pNewClientRect) );
270 if( pNewClientRect->left != wndPtr->rectClient.left ||
271 pNewClientRect->top != wndPtr->rectClient.top )
272 pWinpos->flags &= ~SWP_NOCLIENTMOVE;
274 if( (pNewClientRect->right - pNewClientRect->left !=
275 wndPtr->rectClient.right - wndPtr->rectClient.left))
276 pWinpos->flags &= ~SWP_NOCLIENTSIZE;
277 else
278 wvrFlags &= ~WVR_HREDRAW;
280 if (pNewClientRect->bottom - pNewClientRect->top !=
281 wndPtr->rectClient.bottom - wndPtr->rectClient.top)
282 pWinpos->flags &= ~SWP_NOCLIENTSIZE;
283 else
284 wvrFlags &= ~WVR_VREDRAW;
286 validRects[0] = params.rgrc[1];
287 validRects[1] = params.rgrc[2];
289 else
291 if (!(pWinpos->flags & SWP_NOMOVE) &&
292 (pNewClientRect->left != wndPtr->rectClient.left ||
293 pNewClientRect->top != wndPtr->rectClient.top))
294 pWinpos->flags &= ~SWP_NOCLIENTMOVE;
297 if (pWinpos->flags & (SWP_NOCOPYBITS | SWP_NOREDRAW | SWP_SHOWWINDOW | SWP_HIDEWINDOW))
299 SetRectEmpty( &validRects[0] );
300 SetRectEmpty( &validRects[1] );
302 else get_valid_rects( &wndPtr->rectClient, pNewClientRect, wvrFlags, validRects );
304 WIN_ReleasePtr( wndPtr );
305 return wvrFlags;
309 struct move_owned_info
311 HWND owner;
312 HWND insert_after;
315 static BOOL CALLBACK move_owned_popups( HWND hwnd, LPARAM lparam )
317 struct move_owned_info *info = (struct move_owned_info *)lparam;
319 if (hwnd == info->owner) return FALSE;
320 if ((GetWindowLongW( hwnd, GWL_STYLE ) & WS_POPUP) &&
321 GetWindow( hwnd, GW_OWNER ) == info->owner)
323 SetWindowPos( hwnd, info->insert_after, 0, 0, 0, 0,
324 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE |
325 SWP_NOSENDCHANGING | SWP_DEFERERASE );
326 info->insert_after = hwnd;
328 return TRUE;
331 /***********************************************************************
332 * SWP_DoOwnedPopups
334 * fix Z order taking into account owned popups -
335 * basically we need to maintain them above the window that owns them
337 * FIXME: hide/show owned popups when owner visibility changes.
339 static HWND SWP_DoOwnedPopups(HWND hwnd, HWND hwndInsertAfter)
341 HWND owner = GetWindow( hwnd, GW_OWNER );
342 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
343 struct move_owned_info info;
345 TRACE("(%p) hInsertAfter = %p\n", hwnd, hwndInsertAfter );
347 if ((style & WS_POPUP) && owner)
349 /* make sure this popup stays above the owner */
351 if( hwndInsertAfter != HWND_TOP )
353 HWND hwndLocalPrev = HWND_TOP;
354 HWND prev = GetWindow( owner, GW_HWNDPREV );
356 while (prev && prev != hwndInsertAfter)
358 if (hwndLocalPrev == HWND_TOP && GetWindowLongW( prev, GWL_STYLE ) & WS_VISIBLE)
359 hwndLocalPrev = prev;
360 prev = GetWindow( prev, GW_HWNDPREV );
362 if (!prev) hwndInsertAfter = hwndLocalPrev;
365 else if (style & WS_CHILD) return hwndInsertAfter;
367 info.owner = hwnd;
368 info.insert_after = hwndInsertAfter;
369 EnumWindows( move_owned_popups, (LPARAM)&info );
370 return info.insert_after;
374 /* fix redundant flags and values in the WINDOWPOS structure */
375 static BOOL fixup_flags( WINDOWPOS *winpos )
377 HWND parent;
378 WND *wndPtr = WIN_GetPtr( winpos->hwnd );
379 BOOL ret = TRUE;
381 if (!wndPtr || wndPtr == WND_OTHER_PROCESS)
383 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
384 return FALSE;
386 winpos->hwnd = wndPtr->hwndSelf; /* make it a full handle */
388 /* Finally make sure that all coordinates are valid */
389 if (winpos->x < -32768) winpos->x = -32768;
390 else if (winpos->x > 32767) winpos->x = 32767;
391 if (winpos->y < -32768) winpos->y = -32768;
392 else if (winpos->y > 32767) winpos->y = 32767;
394 if (winpos->cx < 0) winpos->cx = 0;
395 else if (winpos->cx > 32767) winpos->cx = 32767;
396 if (winpos->cy < 0) winpos->cy = 0;
397 else if (winpos->cy > 32767) winpos->cy = 32767;
399 parent = GetAncestor( winpos->hwnd, GA_PARENT );
400 if (!IsWindowVisible( parent )) winpos->flags |= SWP_NOREDRAW;
402 if (wndPtr->dwStyle & WS_VISIBLE) winpos->flags &= ~SWP_SHOWWINDOW;
403 else
405 winpos->flags &= ~SWP_HIDEWINDOW;
406 if (!(winpos->flags & SWP_SHOWWINDOW)) winpos->flags |= SWP_NOREDRAW;
409 if ((wndPtr->rectWindow.right - wndPtr->rectWindow.left == winpos->cx) &&
410 (wndPtr->rectWindow.bottom - wndPtr->rectWindow.top == winpos->cy))
411 winpos->flags |= SWP_NOSIZE; /* Already the right size */
413 if ((wndPtr->rectWindow.left == winpos->x) && (wndPtr->rectWindow.top == winpos->y))
414 winpos->flags |= SWP_NOMOVE; /* Already the right position */
416 if ((wndPtr->dwStyle & (WS_POPUP | WS_CHILD)) != WS_CHILD)
418 if (!(winpos->flags & (SWP_NOACTIVATE|SWP_HIDEWINDOW))) /* Bring to the top when activating */
420 winpos->flags &= ~SWP_NOZORDER;
421 winpos->hwndInsertAfter = HWND_TOP;
425 /* Check hwndInsertAfter */
426 if (winpos->flags & SWP_NOZORDER) goto done;
428 /* fix sign extension */
429 if (winpos->hwndInsertAfter == (HWND)0xffff) winpos->hwndInsertAfter = HWND_TOPMOST;
430 else if (winpos->hwndInsertAfter == (HWND)0xfffe) winpos->hwndInsertAfter = HWND_NOTOPMOST;
432 /* FIXME: TOPMOST not supported yet */
433 if ((winpos->hwndInsertAfter == HWND_TOPMOST) ||
434 (winpos->hwndInsertAfter == HWND_NOTOPMOST)) winpos->hwndInsertAfter = HWND_TOP;
436 /* hwndInsertAfter must be a sibling of the window */
437 if (winpos->hwndInsertAfter == HWND_TOP)
439 if (GetWindow(winpos->hwnd, GW_HWNDFIRST) == winpos->hwnd)
440 winpos->flags |= SWP_NOZORDER;
442 else if (winpos->hwndInsertAfter == HWND_BOTTOM)
444 if (GetWindow(winpos->hwnd, GW_HWNDLAST) == winpos->hwnd)
445 winpos->flags |= SWP_NOZORDER;
447 else
449 if (GetAncestor( winpos->hwndInsertAfter, GA_PARENT ) != parent) ret = FALSE;
450 else
452 /* don't need to change the Zorder of hwnd if it's already inserted
453 * after hwndInsertAfter or when inserting hwnd after itself.
455 if ((winpos->hwnd == winpos->hwndInsertAfter) ||
456 (winpos->hwnd == GetWindow( winpos->hwndInsertAfter, GW_HWNDNEXT )))
457 winpos->flags |= SWP_NOZORDER;
460 done:
461 WIN_ReleasePtr( wndPtr );
462 return ret;
466 /***********************************************************************
467 * SetWindowStyle (X11DRV.@)
469 * Update the X state of a window to reflect a style change
471 void X11DRV_SetWindowStyle( HWND hwnd, DWORD old_style )
473 Display *display = thread_display();
474 struct x11drv_win_data *data;
475 DWORD new_style, changed;
477 if (hwnd == GetDesktopWindow()) return;
478 if (!(data = X11DRV_get_win_data( hwnd ))) return;
480 new_style = GetWindowLongW( hwnd, GWL_STYLE );
481 changed = new_style ^ old_style;
483 if (changed & WS_VISIBLE)
485 if (data->whole_window && X11DRV_is_window_rect_mapped( &data->window_rect ))
487 if (new_style & WS_VISIBLE)
489 TRACE( "mapping win %p\n", hwnd );
490 X11DRV_sync_window_style( display, data );
491 X11DRV_set_wm_hints( display, data );
492 wine_tsx11_lock();
493 XMapWindow( display, data->whole_window );
494 wine_tsx11_unlock();
496 /* we don't unmap windows, that causes trouble with the window manager */
498 invalidate_dce( hwnd, &data->window_rect );
501 if (changed & WS_DISABLED)
503 if (data->whole_window && data->managed)
505 XWMHints *wm_hints;
506 wine_tsx11_lock();
507 if (!(wm_hints = XGetWMHints( display, data->whole_window )))
508 wm_hints = XAllocWMHints();
509 if (wm_hints)
511 wm_hints->flags |= InputHint;
512 wm_hints->input = !(new_style & WS_DISABLED);
513 XSetWMHints( display, data->whole_window, wm_hints );
514 XFree(wm_hints);
516 wine_tsx11_unlock();
522 /***********************************************************************
523 * X11DRV_set_window_pos
525 * Set a window position and Z order.
527 BOOL X11DRV_set_window_pos( HWND hwnd, HWND insert_after, const RECT *rectWindow,
528 const RECT *rectClient, UINT swp_flags, const RECT *valid_rects )
530 struct x11drv_win_data *data;
531 RECT new_whole_rect;
532 WND *win;
533 DWORD old_style, new_style;
534 BOOL ret;
536 if (!(data = X11DRV_get_win_data( hwnd ))) return FALSE;
538 new_whole_rect = *rectWindow;
539 X11DRV_window_to_X_rect( data, &new_whole_rect );
541 if (!IsRectEmpty( &valid_rects[0] ))
543 int x_offset = 0, y_offset = 0;
545 if (data->whole_window)
547 /* the X server will move the bits for us */
548 x_offset = data->whole_rect.left - new_whole_rect.left;
549 y_offset = data->whole_rect.top - new_whole_rect.top;
552 if (x_offset != valid_rects[1].left - valid_rects[0].left ||
553 y_offset != valid_rects[1].top - valid_rects[0].top)
555 /* FIXME: should copy the window bits here */
556 valid_rects = NULL;
560 if (!(win = WIN_GetPtr( hwnd ))) return FALSE;
561 if (win == WND_OTHER_PROCESS)
563 if (IsWindow( hwnd )) ERR( "cannot set rectangles of other process window %p\n", hwnd );
564 return FALSE;
566 SERVER_START_REQ( set_window_pos )
568 req->handle = hwnd;
569 req->previous = insert_after;
570 req->flags = swp_flags;
571 req->window.left = rectWindow->left;
572 req->window.top = rectWindow->top;
573 req->window.right = rectWindow->right;
574 req->window.bottom = rectWindow->bottom;
575 req->client.left = rectClient->left;
576 req->client.top = rectClient->top;
577 req->client.right = rectClient->right;
578 req->client.bottom = rectClient->bottom;
579 if (memcmp( rectWindow, &new_whole_rect, sizeof(RECT) ) || !IsRectEmpty( &valid_rects[0] ))
581 wine_server_add_data( req, &new_whole_rect, sizeof(new_whole_rect) );
582 if (!IsRectEmpty( &valid_rects[0] ))
583 wine_server_add_data( req, valid_rects, 2 * sizeof(*valid_rects) );
585 ret = !wine_server_call( req );
586 new_style = reply->new_style;
588 SERVER_END_REQ;
590 if (win == WND_DESKTOP || data->whole_window == DefaultRootWindow(gdi_display))
592 data->whole_rect = data->client_rect = data->window_rect = *rectWindow;
593 if (win != WND_DESKTOP)
595 win->rectWindow = *rectWindow;
596 win->rectClient = *rectClient;
597 win->dwStyle = new_style;
598 WIN_ReleasePtr( win );
600 return ret;
603 if (ret)
605 Display *display = thread_display();
607 /* invalidate DCEs */
609 if ((((swp_flags & SWP_AGG_NOPOSCHANGE) != SWP_AGG_NOPOSCHANGE) && (new_style & WS_VISIBLE)) ||
610 (swp_flags & (SWP_HIDEWINDOW | SWP_SHOWWINDOW)))
612 RECT rect;
613 UnionRect( &rect, rectWindow, &win->rectWindow );
614 invalidate_dce( hwnd, &rect );
617 win->rectWindow = *rectWindow;
618 win->rectClient = *rectClient;
619 old_style = win->dwStyle;
620 win->dwStyle = new_style;
621 data->window_rect = *rectWindow;
623 TRACE( "win %p window %s client %s style %08lx\n",
624 hwnd, wine_dbgstr_rect(rectWindow), wine_dbgstr_rect(rectClient), new_style );
626 /* FIXME: copy the valid bits */
628 if (data->whole_window && !data->lock_changes)
630 if ((old_style & WS_VISIBLE) && !(new_style & WS_VISIBLE))
632 /* window got hidden, unmap it */
633 TRACE( "unmapping win %p\n", hwnd );
634 wine_tsx11_lock();
635 XUnmapWindow( display, data->whole_window );
636 wine_tsx11_unlock();
638 else if ((new_style & WS_VISIBLE) && !X11DRV_is_window_rect_mapped( rectWindow ))
640 /* resizing to zero size or off screen -> unmap */
641 TRACE( "unmapping zero size or off-screen win %p\n", hwnd );
642 wine_tsx11_lock();
643 XUnmapWindow( display, data->whole_window );
644 wine_tsx11_unlock();
648 X11DRV_sync_window_position( display, data, swp_flags, rectClient, &new_whole_rect );
650 if (data->whole_window && !data->lock_changes)
652 if ((new_style & WS_VISIBLE) && !(new_style & WS_MINIMIZE) &&
653 X11DRV_is_window_rect_mapped( rectWindow ))
655 if (!(old_style & WS_VISIBLE))
657 /* window got shown, map it */
658 TRACE( "mapping win %p\n", hwnd );
659 X11DRV_sync_window_style( display, data );
660 X11DRV_set_wm_hints( display, data );
661 wine_tsx11_lock();
662 XMapWindow( display, data->whole_window );
663 wine_tsx11_unlock();
665 else if ((swp_flags & (SWP_NOSIZE | SWP_NOMOVE)) != (SWP_NOSIZE | SWP_NOMOVE))
667 /* resizing from zero size to non-zero -> map */
668 TRACE( "mapping non zero size or off-screen win %p\n", hwnd );
669 wine_tsx11_lock();
670 XMapWindow( display, data->whole_window );
671 wine_tsx11_unlock();
676 WIN_ReleasePtr( win );
677 return ret;
681 /***********************************************************************
682 * SetWindowPos (X11DRV.@)
684 BOOL X11DRV_SetWindowPos( WINDOWPOS *winpos )
686 RECT newWindowRect, newClientRect, valid_rects[2];
687 UINT orig_flags;
689 TRACE( "hwnd %p, after %p, swp %d,%d %dx%d flags %08x\n",
690 winpos->hwnd, winpos->hwndInsertAfter, winpos->x, winpos->y,
691 winpos->cx, winpos->cy, winpos->flags);
693 orig_flags = winpos->flags;
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 SendMessageW( 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 switch (cmd)
857 case SW_SHOWMINNOACTIVE:
858 case SW_SHOWMINIMIZED:
859 case SW_FORCEMINIMIZE:
860 case SW_MINIMIZE:
861 return SWP_NOSIZE | SWP_NOMOVE;
863 if (!SendMessageW( hwnd, WM_QUERYOPEN, 0, 0 )) return SWP_NOSIZE | SWP_NOMOVE;
864 swpFlags |= SWP_NOCOPYBITS;
867 switch( cmd )
869 case SW_SHOWMINNOACTIVE:
870 case SW_SHOWMINIMIZED:
871 case SW_FORCEMINIMIZE:
872 case SW_MINIMIZE:
873 if (!(wndPtr = WIN_GetPtr( hwnd )) || wndPtr == WND_OTHER_PROCESS) return 0;
874 if( wndPtr->dwStyle & WS_MAXIMIZE) wndPtr->flags |= WIN_RESTORE_MAX;
875 else wndPtr->flags &= ~WIN_RESTORE_MAX;
876 WIN_ReleasePtr( wndPtr );
878 WIN_SetStyle( hwnd, WS_MINIMIZE, WS_MAXIMIZE );
880 X11DRV_set_iconic_state( hwnd );
882 wpl.ptMinPosition = WINPOS_FindIconPos( hwnd, wpl.ptMinPosition );
884 SetRect( rect, wpl.ptMinPosition.x, wpl.ptMinPosition.y,
885 GetSystemMetrics(SM_CXICON), GetSystemMetrics(SM_CYICON) );
886 swpFlags |= SWP_NOCOPYBITS;
887 break;
889 case SW_MAXIMIZE:
890 old_style = GetWindowLongW( hwnd, GWL_STYLE );
891 if ((old_style & WS_MAXIMIZE) && (old_style & WS_CHILD)) return SWP_NOSIZE | SWP_NOMOVE;
893 WINPOS_GetMinMaxInfo( hwnd, &size, &wpl.ptMaxPosition, NULL, NULL );
895 old_style = WIN_SetStyle( hwnd, WS_MAXIMIZE, WS_MINIMIZE );
896 if (old_style & WS_MINIMIZE)
898 WINPOS_ShowIconTitle( hwnd, FALSE );
899 X11DRV_set_iconic_state( hwnd );
901 SetRect( rect, wpl.ptMaxPosition.x, wpl.ptMaxPosition.y, size.x, size.y );
902 break;
904 case SW_RESTORE:
905 old_style = WIN_SetStyle( hwnd, 0, WS_MINIMIZE | WS_MAXIMIZE );
906 if (old_style & WS_MINIMIZE)
908 BOOL restore_max;
910 WINPOS_ShowIconTitle( hwnd, FALSE );
911 X11DRV_set_iconic_state( hwnd );
913 if (!(wndPtr = WIN_GetPtr( hwnd )) || wndPtr == WND_OTHER_PROCESS) return 0;
914 restore_max = (wndPtr->flags & WIN_RESTORE_MAX) != 0;
915 WIN_ReleasePtr( wndPtr );
916 if (restore_max)
918 /* Restore to maximized position */
919 WINPOS_GetMinMaxInfo( hwnd, &size, &wpl.ptMaxPosition, NULL, NULL);
920 WIN_SetStyle( hwnd, WS_MAXIMIZE, 0 );
921 SetRect( rect, wpl.ptMaxPosition.x, wpl.ptMaxPosition.y, size.x, size.y );
922 break;
925 else if (!(old_style & WS_MAXIMIZE)) break;
927 /* Restore to normal position */
929 *rect = wpl.rcNormalPosition;
930 rect->right -= rect->left;
931 rect->bottom -= rect->top;
933 break;
936 return swpFlags;
940 /***********************************************************************
941 * ShowWindow (X11DRV.@)
943 BOOL X11DRV_ShowWindow( HWND hwnd, INT cmd )
945 WND *wndPtr;
946 HWND parent;
947 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
948 BOOL wasVisible = (style & WS_VISIBLE) != 0;
949 BOOL showFlag = TRUE, state_change = FALSE;
950 RECT newPos = {0, 0, 0, 0};
951 UINT swp = 0;
953 TRACE("hwnd=%p, cmd=%d, wasVisible %d\n", hwnd, cmd, wasVisible);
955 switch(cmd)
957 case SW_HIDE:
958 if (!wasVisible) return FALSE;
959 showFlag = FALSE;
960 swp |= SWP_HIDEWINDOW | SWP_NOSIZE | SWP_NOMOVE;
961 if (hwnd != GetActiveWindow())
962 swp |= SWP_NOACTIVATE | SWP_NOZORDER;
963 break;
965 case SW_SHOWMINNOACTIVE:
966 swp |= SWP_NOACTIVATE | SWP_NOZORDER;
967 /* fall through */
968 case SW_MINIMIZE:
969 case SW_FORCEMINIMIZE: /* FIXME: Does not work if thread is hung. */
970 if (style & WS_CHILD) swp |= SWP_NOACTIVATE | SWP_NOZORDER;
971 /* fall through */
972 case SW_SHOWMINIMIZED:
973 swp |= SWP_SHOWWINDOW | SWP_FRAMECHANGED;
974 swp |= WINPOS_MinMaximize( hwnd, cmd, &newPos );
975 if (style & WS_MINIMIZE) return wasVisible;
976 state_change = TRUE;
977 break;
979 case SW_SHOWMAXIMIZED: /* same as SW_MAXIMIZE */
980 swp |= SWP_SHOWWINDOW | SWP_FRAMECHANGED;
981 swp |= WINPOS_MinMaximize( hwnd, SW_MAXIMIZE, &newPos );
982 if ((style & WS_MAXIMIZE) && (style & WS_CHILD)) return wasVisible;
983 state_change = TRUE;
984 break;
986 case SW_SHOWNA:
987 swp |= SWP_NOACTIVATE | SWP_SHOWWINDOW | SWP_NOSIZE | SWP_NOMOVE;
988 if (style & WS_CHILD) swp |= SWP_NOZORDER;
989 break;
990 case SW_SHOW:
991 if (wasVisible) return TRUE;
992 swp |= SWP_SHOWWINDOW | SWP_NOSIZE | SWP_NOMOVE;
993 if (style & WS_CHILD) swp |= SWP_NOACTIVATE | SWP_NOZORDER;
994 break;
996 case SW_RESTORE:
997 swp |= SWP_FRAMECHANGED;
998 state_change = TRUE;
999 /* fall through */
1000 case SW_SHOWNOACTIVATE:
1001 swp |= SWP_NOACTIVATE | SWP_NOZORDER;
1002 /* fall through */
1003 case SW_SHOWNORMAL: /* same as SW_NORMAL: */
1004 case SW_SHOWDEFAULT: /* FIXME: should have its own handler */
1005 swp |= SWP_SHOWWINDOW;
1006 if (style & (WS_MINIMIZE | WS_MAXIMIZE))
1007 swp |= WINPOS_MinMaximize( hwnd, SW_RESTORE, &newPos );
1008 else swp |= SWP_NOSIZE | SWP_NOMOVE;
1009 if (style & WS_CHILD) swp |= SWP_NOACTIVATE | SWP_NOZORDER;
1010 break;
1013 if ((showFlag != wasVisible || cmd == SW_SHOWNA) && !state_change)
1015 SendMessageW( hwnd, WM_SHOWWINDOW, showFlag, 0 );
1016 if (!IsWindow( hwnd )) return wasVisible;
1019 parent = GetAncestor( hwnd, GA_PARENT );
1020 if (parent && !IsWindowVisible( parent ) && !state_change)
1022 /* if parent is not visible simply toggle WS_VISIBLE and return */
1023 if (showFlag) WIN_SetStyle( hwnd, WS_VISIBLE, 0 );
1024 else WIN_SetStyle( hwnd, 0, WS_VISIBLE );
1026 else
1028 if (style & WS_CHILD)
1030 if (state_change)
1032 /* it appears that Windows always adds an undocumented 0x8000
1033 * flag if the state of a window changes.
1035 swp |= SWP_STATECHANGED;
1039 SetWindowPos( hwnd, HWND_TOP, newPos.left, newPos.top,
1040 newPos.right, newPos.bottom, LOWORD(swp) );
1043 if (cmd == SW_HIDE)
1045 HWND hFocus;
1047 /* FIXME: This will cause the window to be activated irrespective
1048 * of whether it is owned by the same thread. Has to be done
1049 * asynchronously.
1052 if (hwnd == GetActiveWindow())
1053 WINPOS_ActivateOtherWindow(hwnd);
1055 /* Revert focus to parent */
1056 hFocus = GetFocus();
1057 if (hwnd == hFocus || IsChild(hwnd, hFocus))
1059 HWND parent = GetAncestor(hwnd, GA_PARENT);
1060 if (parent == GetDesktopWindow()) parent = 0;
1061 SetFocus(parent);
1065 if (IsIconic(hwnd)) WINPOS_ShowIconTitle( hwnd, TRUE );
1067 if (!(wndPtr = WIN_GetPtr( hwnd )) || wndPtr == WND_OTHER_PROCESS) return wasVisible;
1069 if (wndPtr->flags & WIN_NEED_SIZE)
1071 /* should happen only in CreateWindowEx() */
1072 int wParam = SIZE_RESTORED;
1073 RECT client = wndPtr->rectClient;
1075 wndPtr->flags &= ~WIN_NEED_SIZE;
1076 if (wndPtr->dwStyle & WS_MAXIMIZE) wParam = SIZE_MAXIMIZED;
1077 else if (wndPtr->dwStyle & WS_MINIMIZE) wParam = SIZE_MINIMIZED;
1078 WIN_ReleasePtr( wndPtr );
1080 SendMessageW( hwnd, WM_SIZE, wParam,
1081 MAKELONG( client.right - client.left, client.bottom - client.top ));
1082 SendMessageW( hwnd, WM_MOVE, 0, MAKELONG( client.left, client.top ));
1084 else WIN_ReleasePtr( wndPtr );
1086 return wasVisible;
1090 /**********************************************************************
1091 * X11DRV_MapNotify
1093 void X11DRV_MapNotify( HWND hwnd, XEvent *event )
1095 struct x11drv_win_data *data;
1096 HWND hwndFocus = GetFocus();
1097 WND *win;
1099 if (!(data = X11DRV_get_win_data( hwnd ))) return;
1101 if (!(win = WIN_GetPtr( hwnd ))) return;
1103 if (data->managed && (win->dwStyle & WS_VISIBLE) && (win->dwStyle & WS_MINIMIZE))
1105 int x, y;
1106 unsigned int width, height, border, depth;
1107 Window root, top;
1108 RECT rect;
1109 LONG style = WS_VISIBLE;
1111 /* FIXME: hack */
1112 wine_tsx11_lock();
1113 XGetGeometry( event->xmap.display, data->whole_window, &root, &x, &y, &width, &height,
1114 &border, &depth );
1115 XTranslateCoordinates( event->xmap.display, data->whole_window, root, 0, 0, &x, &y, &top );
1116 wine_tsx11_unlock();
1117 rect.left = x;
1118 rect.top = y;
1119 rect.right = x + width;
1120 rect.bottom = y + height;
1121 X11DRV_X_to_window_rect( data, &rect );
1123 invalidate_dce( hwnd, &data->window_rect );
1125 if (win->flags & WIN_RESTORE_MAX) style |= WS_MAXIMIZE;
1126 WIN_SetStyle( hwnd, style, WS_MINIMIZE );
1127 WIN_ReleasePtr( win );
1129 SendMessageW( hwnd, WM_SHOWWINDOW, SW_RESTORE, 0 );
1130 data->lock_changes++;
1131 SetWindowPos( hwnd, 0, rect.left, rect.top, rect.right-rect.left, rect.bottom-rect.top,
1132 SWP_NOZORDER );
1133 data->lock_changes--;
1135 else WIN_ReleasePtr( win );
1136 if (hwndFocus && IsChild( hwnd, hwndFocus )) X11DRV_SetFocus(hwndFocus); /* FIXME */
1140 /**********************************************************************
1141 * X11DRV_UnmapNotify
1143 void X11DRV_UnmapNotify( HWND hwnd, XEvent *event )
1145 struct x11drv_win_data *data;
1146 WND *win;
1148 if (!(data = X11DRV_get_win_data( hwnd ))) return;
1150 if (!(win = WIN_GetPtr( hwnd ))) return;
1152 if ((win->dwStyle & WS_VISIBLE) && data->managed &&
1153 X11DRV_is_window_rect_mapped( &win->rectWindow ))
1155 if (win->dwStyle & WS_MAXIMIZE)
1156 win->flags |= WIN_RESTORE_MAX;
1157 else
1158 win->flags &= ~WIN_RESTORE_MAX;
1160 WIN_SetStyle( hwnd, WS_MINIMIZE, WS_MAXIMIZE );
1161 WIN_ReleasePtr( win );
1163 EndMenu();
1164 SendMessageW( hwnd, WM_SHOWWINDOW, SW_MINIMIZE, 0 );
1165 data->lock_changes++;
1166 SetWindowPos( hwnd, 0, 0, 0, GetSystemMetrics(SM_CXICON), GetSystemMetrics(SM_CYICON),
1167 SWP_NOMOVE | SWP_NOACTIVATE | SWP_NOZORDER );
1168 data->lock_changes--;
1170 else WIN_ReleasePtr( win );
1174 /***********************************************************************
1175 * X11DRV_handle_desktop_resize
1177 void X11DRV_handle_desktop_resize( unsigned int width, unsigned int height )
1179 RECT rect;
1180 HWND hwnd = GetDesktopWindow();
1181 struct x11drv_win_data *data;
1183 if (!(data = X11DRV_get_win_data( hwnd ))) return;
1184 screen_width = width;
1185 screen_height = height;
1186 TRACE("desktop %p change to (%dx%d)\n", hwnd, width, height);
1187 SetRect( &rect, 0, 0, width, height );
1188 data->lock_changes++;
1189 X11DRV_set_window_pos( hwnd, 0, &rect, &rect, SWP_NOZORDER|SWP_NOMOVE, NULL );
1190 data->lock_changes--;
1191 SendMessageTimeoutW( HWND_BROADCAST, WM_DISPLAYCHANGE, screen_depth,
1192 MAKELPARAM( width, height ), SMTO_ABORTIFHUNG, 2000, NULL );
1196 /***********************************************************************
1197 * X11DRV_ConfigureNotify
1199 void X11DRV_ConfigureNotify( HWND hwnd, XEvent *xev )
1201 XConfigureEvent *event = &xev->xconfigure;
1202 struct x11drv_win_data *data;
1203 RECT rect;
1204 UINT flags;
1205 int cx, cy, x = event->x, y = event->y;
1207 if (!hwnd) return;
1208 if (!(data = X11DRV_get_win_data( hwnd ))) return;
1210 /* Get geometry */
1212 if (!event->send_event) /* normal event, need to map coordinates to the root */
1214 Window child;
1215 wine_tsx11_lock();
1216 XTranslateCoordinates( event->display, data->whole_window, root_window,
1217 0, 0, &x, &y, &child );
1218 wine_tsx11_unlock();
1220 rect.left = x;
1221 rect.top = y;
1222 rect.right = x + event->width;
1223 rect.bottom = y + event->height;
1224 TRACE( "win %p new X rect %ld,%ld,%ldx%ld (event %d,%d,%dx%d)\n",
1225 hwnd, rect.left, rect.top, rect.right-rect.left, rect.bottom-rect.top,
1226 event->x, event->y, event->width, event->height );
1227 X11DRV_X_to_window_rect( data, &rect );
1229 x = rect.left;
1230 y = rect.top;
1231 cx = rect.right - rect.left;
1232 cy = rect.bottom - rect.top;
1233 flags = SWP_NOACTIVATE | SWP_NOZORDER;
1235 /* Compare what has changed */
1237 GetWindowRect( hwnd, &rect );
1238 if (rect.left == x && rect.top == y) flags |= SWP_NOMOVE;
1239 else
1240 TRACE( "%p moving from (%ld,%ld) to (%d,%d)\n",
1241 hwnd, rect.left, rect.top, x, y );
1243 if ((rect.right - rect.left == cx && rect.bottom - rect.top == cy) ||
1244 IsIconic(hwnd) ||
1245 (IsRectEmpty( &rect ) && event->width == 1 && event->height == 1))
1247 if (flags & SWP_NOMOVE) return; /* if nothing changed, don't do anything */
1248 flags |= SWP_NOSIZE;
1250 else
1251 TRACE( "%p resizing from (%ldx%ld) to (%dx%d)\n",
1252 hwnd, rect.right - rect.left, rect.bottom - rect.top, cx, cy );
1254 data->lock_changes++;
1255 SetWindowPos( hwnd, 0, x, y, cx, cy, flags );
1256 data->lock_changes--;
1260 /***********************************************************************
1261 * SetWindowRgn (X11DRV.@)
1263 * Assign specified region to window (for non-rectangular windows)
1265 int X11DRV_SetWindowRgn( HWND hwnd, HRGN hrgn, BOOL redraw )
1267 struct x11drv_win_data *data;
1269 if (!(data = X11DRV_get_win_data( hwnd )))
1271 if (IsWindow( hwnd ))
1272 FIXME( "not supported on other thread window %p\n", hwnd );
1273 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
1274 return FALSE;
1277 #ifdef HAVE_LIBXSHAPE
1278 if (data->whole_window)
1280 Display *display = thread_display();
1282 if (!hrgn)
1284 wine_tsx11_lock();
1285 XShapeCombineMask( display, data->whole_window,
1286 ShapeBounding, 0, 0, None, ShapeSet );
1287 wine_tsx11_unlock();
1289 else
1291 RGNDATA *pRegionData = X11DRV_GetRegionData( hrgn, 0 );
1292 if (pRegionData)
1294 wine_tsx11_lock();
1295 XShapeCombineRectangles( display, data->whole_window, ShapeBounding,
1296 data->window_rect.left - data->whole_rect.left,
1297 data->window_rect.top - data->whole_rect.top,
1298 (XRectangle *)pRegionData->Buffer,
1299 pRegionData->rdh.nCount,
1300 ShapeSet, YXBanded );
1301 wine_tsx11_unlock();
1302 HeapFree(GetProcessHeap(), 0, pRegionData);
1306 #endif /* HAVE_LIBXSHAPE */
1308 invalidate_dce( hwnd, &data->window_rect );
1309 return TRUE;
1313 /***********************************************************************
1314 * draw_moving_frame
1316 * Draw the frame used when moving or resizing window.
1318 * FIXME: This causes problems in Win95 mode. (why?)
1320 static void draw_moving_frame( HDC hdc, RECT *rect, BOOL thickframe )
1322 if (thickframe)
1324 const int width = GetSystemMetrics(SM_CXFRAME);
1325 const int height = GetSystemMetrics(SM_CYFRAME);
1327 HBRUSH hbrush = SelectObject( hdc, GetStockObject( GRAY_BRUSH ) );
1328 PatBlt( hdc, rect->left, rect->top,
1329 rect->right - rect->left - width, height, PATINVERT );
1330 PatBlt( hdc, rect->left, rect->top + height, width,
1331 rect->bottom - rect->top - height, PATINVERT );
1332 PatBlt( hdc, rect->left + width, rect->bottom - 1,
1333 rect->right - rect->left - width, -height, PATINVERT );
1334 PatBlt( hdc, rect->right - 1, rect->top, -width,
1335 rect->bottom - rect->top - height, PATINVERT );
1336 SelectObject( hdc, hbrush );
1338 else DrawFocusRect( hdc, rect );
1342 /***********************************************************************
1343 * start_size_move
1345 * Initialisation of a move or resize, when initiatied from a menu choice.
1346 * Return hit test code for caption or sizing border.
1348 static LONG start_size_move( HWND hwnd, WPARAM wParam, POINT *capturePoint, LONG style )
1350 LONG hittest = 0;
1351 POINT pt;
1352 MSG msg;
1353 RECT rectWindow;
1355 GetWindowRect( hwnd, &rectWindow );
1357 if ((wParam & 0xfff0) == SC_MOVE)
1359 /* Move pointer at the center of the caption */
1360 RECT rect = rectWindow;
1361 /* Note: to be exactly centered we should take the different types
1362 * of border into account, but it shouldn't make more that a few pixels
1363 * of difference so let's not bother with that */
1364 rect.top += GetSystemMetrics(SM_CYBORDER);
1365 if (style & WS_SYSMENU)
1366 rect.left += GetSystemMetrics(SM_CXSIZE) + 1;
1367 if (style & WS_MINIMIZEBOX)
1368 rect.right -= GetSystemMetrics(SM_CXSIZE) + 1;
1369 if (style & WS_MAXIMIZEBOX)
1370 rect.right -= GetSystemMetrics(SM_CXSIZE) + 1;
1371 pt.x = (rect.right + rect.left) / 2;
1372 pt.y = rect.top + GetSystemMetrics(SM_CYSIZE)/2;
1373 hittest = HTCAPTION;
1374 *capturePoint = pt;
1376 else /* SC_SIZE */
1378 pt.x = pt.y = 0;
1379 while(!hittest)
1381 GetMessageW( &msg, 0, WM_KEYFIRST, WM_MOUSELAST );
1382 if (CallMsgFilterW( &msg, MSGF_SIZE )) continue;
1384 switch(msg.message)
1386 case WM_MOUSEMOVE:
1387 pt = msg.pt;
1388 hittest = SendMessageW( hwnd, WM_NCHITTEST, 0, MAKELONG( pt.x, pt.y ) );
1389 if ((hittest < HTLEFT) || (hittest > HTBOTTOMRIGHT)) hittest = 0;
1390 break;
1392 case WM_LBUTTONUP:
1393 return 0;
1395 case WM_KEYDOWN:
1396 switch(msg.wParam)
1398 case VK_UP:
1399 hittest = HTTOP;
1400 pt.x =(rectWindow.left+rectWindow.right)/2;
1401 pt.y = rectWindow.top + GetSystemMetrics(SM_CYFRAME) / 2;
1402 break;
1403 case VK_DOWN:
1404 hittest = HTBOTTOM;
1405 pt.x =(rectWindow.left+rectWindow.right)/2;
1406 pt.y = rectWindow.bottom - GetSystemMetrics(SM_CYFRAME) / 2;
1407 break;
1408 case VK_LEFT:
1409 hittest = HTLEFT;
1410 pt.x = rectWindow.left + GetSystemMetrics(SM_CXFRAME) / 2;
1411 pt.y =(rectWindow.top+rectWindow.bottom)/2;
1412 break;
1413 case VK_RIGHT:
1414 hittest = HTRIGHT;
1415 pt.x = rectWindow.right - GetSystemMetrics(SM_CXFRAME) / 2;
1416 pt.y =(rectWindow.top+rectWindow.bottom)/2;
1417 break;
1418 case VK_RETURN:
1419 case VK_ESCAPE: return 0;
1423 *capturePoint = pt;
1425 SetCursorPos( pt.x, pt.y );
1426 SendMessageW( hwnd, WM_SETCURSOR, (WPARAM)hwnd, MAKELONG( hittest, WM_MOUSEMOVE ));
1427 return hittest;
1431 /***********************************************************************
1432 * set_movesize_capture
1434 static void set_movesize_capture( HWND hwnd )
1436 HWND previous = 0;
1438 SERVER_START_REQ( set_capture_window )
1440 req->handle = hwnd;
1441 req->flags = CAPTURE_MOVESIZE;
1442 if (!wine_server_call_err( req ))
1444 previous = reply->previous;
1445 hwnd = reply->full_handle;
1448 SERVER_END_REQ;
1449 if (previous && previous != hwnd)
1450 SendMessageW( previous, WM_CAPTURECHANGED, 0, (LPARAM)hwnd );
1453 /***********************************************************************
1454 * X11DRV_WMMoveResizeWindow
1456 * Tells the window manager to initiate a move or resize operation.
1458 * SEE
1459 * http://freedesktop.org/Standards/wm-spec/1.3/ar01s04.html
1460 * or search for "_NET_WM_MOVERESIZE"
1462 static void X11DRV_WMMoveResizeWindow( HWND hwnd, int x, int y, int dir )
1464 XEvent xev;
1465 Display *display = thread_display();
1467 xev.xclient.type = ClientMessage;
1468 xev.xclient.window = X11DRV_get_whole_window(hwnd);
1469 xev.xclient.message_type = x11drv_atom(_NET_WM_MOVERESIZE);
1470 xev.xclient.serial = 0;
1471 xev.xclient.display = display;
1472 xev.xclient.send_event = True;
1473 xev.xclient.format = 32;
1474 xev.xclient.data.l[0] = x; /* x coord */
1475 xev.xclient.data.l[1] = y; /* y coord */
1476 xev.xclient.data.l[2] = dir; /* direction */
1477 xev.xclient.data.l[3] = 1; /* button */
1478 xev.xclient.data.l[4] = 0; /* unused */
1480 /* need to ungrab the pointer that may have been automatically grabbed
1481 * with a ButtonPress event */
1482 wine_tsx11_lock();
1483 XUngrabPointer( display, CurrentTime );
1484 XSendEvent(display, root_window, False, SubstructureNotifyMask, &xev);
1485 wine_tsx11_unlock();
1488 /***********************************************************************
1489 * SysCommandSizeMove (X11DRV.@)
1491 * Perform SC_MOVE and SC_SIZE commands.
1493 void X11DRV_SysCommandSizeMove( HWND hwnd, WPARAM wParam )
1495 MSG msg;
1496 RECT sizingRect, mouseRect, origRect;
1497 HDC hdc;
1498 HWND parent;
1499 LONG hittest = (LONG)(wParam & 0x0f);
1500 WPARAM syscommand = wParam & 0xfff0;
1501 HCURSOR hDragCursor = 0, hOldCursor = 0;
1502 POINT minTrack, maxTrack;
1503 POINT capturePoint, pt;
1504 LONG style = GetWindowLongA( hwnd, GWL_STYLE );
1505 BOOL thickframe = HAS_THICKFRAME( style );
1506 BOOL iconic = style & WS_MINIMIZE;
1507 BOOL moved = FALSE;
1508 DWORD dwPoint = GetMessagePos ();
1509 BOOL DragFullWindows = FALSE;
1510 BOOL grab;
1511 Window parent_win, whole_win;
1512 Display *old_gdi_display = NULL;
1513 struct x11drv_thread_data *thread_data = x11drv_thread_data();
1514 struct x11drv_win_data *data;
1516 pt.x = (short)LOWORD(dwPoint);
1517 pt.y = (short)HIWORD(dwPoint);
1518 capturePoint = pt;
1520 if (IsZoomed(hwnd) || !IsWindowVisible(hwnd)) return;
1522 if (!(data = X11DRV_get_win_data( hwnd ))) return;
1524 /* if we are managed then we let the WM do all the work */
1525 if (data->managed)
1527 int dir;
1528 if (syscommand == SC_MOVE)
1530 if (!hittest) dir = _NET_WM_MOVERESIZE_MOVE_KEYBOARD;
1531 else dir = _NET_WM_MOVERESIZE_MOVE;
1533 else if (!hittest) dir = _NET_WM_MOVERESIZE_SIZE_KEYBOARD;
1534 else
1535 switch (hittest)
1537 case WMSZ_LEFT: dir = _NET_WM_MOVERESIZE_SIZE_LEFT; break;
1538 case WMSZ_RIGHT: dir = _NET_WM_MOVERESIZE_SIZE_RIGHT; break;
1539 case WMSZ_TOP: dir = _NET_WM_MOVERESIZE_SIZE_TOP; break;
1540 case WMSZ_TOPLEFT: dir = _NET_WM_MOVERESIZE_SIZE_TOPLEFT; break;
1541 case WMSZ_TOPRIGHT: dir = _NET_WM_MOVERESIZE_SIZE_TOPRIGHT; break;
1542 case WMSZ_BOTTOM: dir = _NET_WM_MOVERESIZE_SIZE_BOTTOM; break;
1543 case WMSZ_BOTTOMLEFT: dir = _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT; break;
1544 case WMSZ_BOTTOMRIGHT: dir = _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT; break;
1545 default:
1546 ERR("Invalid hittest value: %ld\n", hittest);
1547 dir = _NET_WM_MOVERESIZE_SIZE_KEYBOARD;
1549 X11DRV_WMMoveResizeWindow( hwnd, pt.x, pt.y, dir );
1550 return;
1553 SystemParametersInfoA(SPI_GETDRAGFULLWINDOWS, 0, &DragFullWindows, 0);
1555 if (syscommand == SC_MOVE)
1557 if (!hittest) hittest = start_size_move( hwnd, wParam, &capturePoint, style );
1558 if (!hittest) return;
1560 else /* SC_SIZE */
1562 if ( hittest && (syscommand != SC_MOUSEMENU) )
1563 hittest += (HTLEFT - WMSZ_LEFT);
1564 else
1566 set_movesize_capture( hwnd );
1567 hittest = start_size_move( hwnd, wParam, &capturePoint, style );
1568 if (!hittest)
1570 set_movesize_capture(0);
1571 return;
1576 /* Get min/max info */
1578 WINPOS_GetMinMaxInfo( hwnd, NULL, NULL, &minTrack, &maxTrack );
1579 GetWindowRect( hwnd, &sizingRect );
1580 if (style & WS_CHILD)
1582 parent = GetParent(hwnd);
1583 /* make sizing rect relative to parent */
1584 MapWindowPoints( 0, parent, (POINT*)&sizingRect, 2 );
1585 GetClientRect( parent, &mouseRect );
1587 else
1589 parent = 0;
1590 SetRect(&mouseRect, 0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN));
1592 origRect = sizingRect;
1594 if (ON_LEFT_BORDER(hittest))
1596 mouseRect.left = max( mouseRect.left, sizingRect.right-maxTrack.x );
1597 mouseRect.right = min( mouseRect.right, sizingRect.right-minTrack.x );
1599 else if (ON_RIGHT_BORDER(hittest))
1601 mouseRect.left = max( mouseRect.left, sizingRect.left+minTrack.x );
1602 mouseRect.right = min( mouseRect.right, sizingRect.left+maxTrack.x );
1604 if (ON_TOP_BORDER(hittest))
1606 mouseRect.top = max( mouseRect.top, sizingRect.bottom-maxTrack.y );
1607 mouseRect.bottom = min( mouseRect.bottom,sizingRect.bottom-minTrack.y);
1609 else if (ON_BOTTOM_BORDER(hittest))
1611 mouseRect.top = max( mouseRect.top, sizingRect.top+minTrack.y );
1612 mouseRect.bottom = min( mouseRect.bottom, sizingRect.top+maxTrack.y );
1614 if (parent) MapWindowPoints( parent, 0, (LPPOINT)&mouseRect, 2 );
1616 /* Retrieve a default cache DC (without using the window style) */
1617 hdc = GetDCEx( parent, 0, DCX_CACHE );
1619 if( iconic ) /* create a cursor for dragging */
1621 hDragCursor = (HCURSOR)GetClassLongPtrW( hwnd, GCLP_HICON);
1622 if( !hDragCursor ) hDragCursor = (HCURSOR)SendMessageW( hwnd, WM_QUERYDRAGICON, 0, 0L);
1623 if( !hDragCursor ) iconic = FALSE;
1626 /* repaint the window before moving it around */
1627 RedrawWindow( hwnd, NULL, 0, RDW_UPDATENOW | RDW_ALLCHILDREN );
1629 SendMessageW( hwnd, WM_ENTERSIZEMOVE, 0, 0 );
1630 set_movesize_capture( hwnd );
1632 /* grab the server only when moving top-level windows without desktop */
1633 grab = (!DragFullWindows && !parent && (root_window == DefaultRootWindow(gdi_display)));
1635 if (grab)
1637 wine_tsx11_lock();
1638 XSync( gdi_display, False );
1639 XGrabServer( thread_data->display );
1640 XSync( thread_data->display, False );
1641 /* switch gdi display to the thread display, since the server is grabbed */
1642 old_gdi_display = gdi_display;
1643 gdi_display = thread_data->display;
1644 wine_tsx11_unlock();
1646 whole_win = X11DRV_get_whole_window( GetAncestor(hwnd,GA_ROOT) );
1647 parent_win = parent ? X11DRV_get_whole_window( GetAncestor(parent,GA_ROOT) ) : root_window;
1649 wine_tsx11_lock();
1650 XGrabPointer( thread_data->display, whole_win, False,
1651 PointerMotionMask | ButtonPressMask | ButtonReleaseMask,
1652 GrabModeAsync, GrabModeAsync, parent_win, None, CurrentTime );
1653 wine_tsx11_unlock();
1654 thread_data->grab_window = whole_win;
1656 while(1)
1658 int dx = 0, dy = 0;
1660 if (!GetMessageW( &msg, 0, WM_KEYFIRST, WM_MOUSELAST )) break;
1661 if (CallMsgFilterW( &msg, MSGF_SIZE )) continue;
1663 /* Exit on button-up, Return, or Esc */
1664 if ((msg.message == WM_LBUTTONUP) ||
1665 ((msg.message == WM_KEYDOWN) &&
1666 ((msg.wParam == VK_RETURN) || (msg.wParam == VK_ESCAPE)))) break;
1668 if ((msg.message != WM_KEYDOWN) && (msg.message != WM_MOUSEMOVE))
1669 continue; /* We are not interested in other messages */
1671 pt = msg.pt;
1673 if (msg.message == WM_KEYDOWN) switch(msg.wParam)
1675 case VK_UP: pt.y -= 8; break;
1676 case VK_DOWN: pt.y += 8; break;
1677 case VK_LEFT: pt.x -= 8; break;
1678 case VK_RIGHT: pt.x += 8; break;
1681 pt.x = max( pt.x, mouseRect.left );
1682 pt.x = min( pt.x, mouseRect.right );
1683 pt.y = max( pt.y, mouseRect.top );
1684 pt.y = min( pt.y, mouseRect.bottom );
1686 dx = pt.x - capturePoint.x;
1687 dy = pt.y - capturePoint.y;
1689 if (dx || dy)
1691 if( !moved )
1693 moved = TRUE;
1695 if( iconic ) /* ok, no system popup tracking */
1697 hOldCursor = SetCursor(hDragCursor);
1698 ShowCursor( TRUE );
1699 WINPOS_ShowIconTitle( hwnd, FALSE );
1701 else if(!DragFullWindows)
1702 draw_moving_frame( hdc, &sizingRect, thickframe );
1705 if (msg.message == WM_KEYDOWN) SetCursorPos( pt.x, pt.y );
1706 else
1708 RECT newRect = sizingRect;
1709 WPARAM wpSizingHit = 0;
1711 if (hittest == HTCAPTION) OffsetRect( &newRect, dx, dy );
1712 if (ON_LEFT_BORDER(hittest)) newRect.left += dx;
1713 else if (ON_RIGHT_BORDER(hittest)) newRect.right += dx;
1714 if (ON_TOP_BORDER(hittest)) newRect.top += dy;
1715 else if (ON_BOTTOM_BORDER(hittest)) newRect.bottom += dy;
1716 if(!iconic && !DragFullWindows) draw_moving_frame( hdc, &sizingRect, thickframe );
1717 capturePoint = pt;
1719 /* determine the hit location */
1720 if (hittest >= HTLEFT && hittest <= HTBOTTOMRIGHT)
1721 wpSizingHit = WMSZ_LEFT + (hittest - HTLEFT);
1722 SendMessageW( hwnd, WM_SIZING, wpSizingHit, (LPARAM)&newRect );
1724 if (!iconic)
1726 if(!DragFullWindows)
1727 draw_moving_frame( hdc, &newRect, thickframe );
1728 else
1729 SetWindowPos( hwnd, 0, newRect.left, newRect.top,
1730 newRect.right - newRect.left,
1731 newRect.bottom - newRect.top,
1732 ( hittest == HTCAPTION ) ? SWP_NOSIZE : 0 );
1734 sizingRect = newRect;
1739 set_movesize_capture(0);
1740 if( iconic )
1742 if( moved ) /* restore cursors, show icon title later on */
1744 ShowCursor( FALSE );
1745 SetCursor( hOldCursor );
1748 else if (moved && !DragFullWindows)
1749 draw_moving_frame( hdc, &sizingRect, thickframe );
1751 ReleaseDC( parent, hdc );
1753 wine_tsx11_lock();
1754 XUngrabPointer( thread_data->display, CurrentTime );
1755 if (grab)
1757 XSync( thread_data->display, False );
1758 XUngrabServer( thread_data->display );
1759 XSync( thread_data->display, False );
1760 gdi_display = old_gdi_display;
1762 wine_tsx11_unlock();
1763 thread_data->grab_window = None;
1765 if (HOOK_CallHooks( WH_CBT, HCBT_MOVESIZE, (WPARAM)hwnd, (LPARAM)&sizingRect, TRUE ))
1766 moved = FALSE;
1768 SendMessageW( hwnd, WM_EXITSIZEMOVE, 0, 0 );
1769 SendMessageW( hwnd, WM_SETVISIBLE, !IsIconic(hwnd), 0L);
1771 /* window moved or resized */
1772 if (moved)
1774 /* if the moving/resizing isn't canceled call SetWindowPos
1775 * with the new position or the new size of the window
1777 if (!((msg.message == WM_KEYDOWN) && (msg.wParam == VK_ESCAPE)) )
1779 /* NOTE: SWP_NOACTIVATE prevents document window activation in Word 6 */
1780 if(!DragFullWindows)
1781 SetWindowPos( hwnd, 0, sizingRect.left, sizingRect.top,
1782 sizingRect.right - sizingRect.left,
1783 sizingRect.bottom - sizingRect.top,
1784 ( hittest == HTCAPTION ) ? SWP_NOSIZE : 0 );
1786 else
1787 { /* restore previous size/position */
1788 if(DragFullWindows)
1789 SetWindowPos( hwnd, 0, origRect.left, origRect.top,
1790 origRect.right - origRect.left,
1791 origRect.bottom - origRect.top,
1792 ( hittest == HTCAPTION ) ? SWP_NOSIZE : 0 );
1796 if (IsIconic(hwnd))
1798 /* Single click brings up the system menu when iconized */
1800 if( !moved )
1802 if(style & WS_SYSMENU )
1803 SendMessageW( hwnd, WM_SYSCOMMAND,
1804 SC_MOUSEMENU + HTSYSMENU, MAKELONG(pt.x,pt.y));
1806 else WINPOS_ShowIconTitle( hwnd, TRUE );