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
25 #include <X11/Xutil.h>
27 #include <X11/extensions/shape.h>
28 #endif /* HAVE_LIBXSHAPE */
37 #include "wine/wingdi16.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 */
81 #define _NET_WM_STATE_REMOVE 0
82 #define _NET_WM_STATE_ADD 1
83 #define _NET_WM_STATE_TOGGLE 2
85 /***********************************************************************
88 void X11DRV_Expose( HWND hwnd, XEvent *xev )
90 XExposeEvent *event = &xev->xexpose;
92 struct x11drv_win_data *data;
93 int flags = RDW_INVALIDATE | RDW_ERASE | RDW_ALLCHILDREN;
95 TRACE( "win %p (%lx) %d,%d %dx%d\n",
96 hwnd, event->window, event->x, event->y, event->width, event->height );
98 if (!(data = X11DRV_get_win_data( hwnd ))) return;
100 rect.left = event->x;
102 rect.right = rect.left + event->width;
103 rect.bottom = rect.top + event->height;
105 if (rect.left < data->client_rect.left ||
106 rect.top < data->client_rect.top ||
107 rect.right > data->client_rect.right ||
108 rect.bottom > data->client_rect.bottom) flags |= RDW_FRAME;
110 SERVER_START_REQ( update_window_zorder )
113 req->rect.left = rect.left + data->whole_rect.left;
114 req->rect.top = rect.top + data->whole_rect.top;
115 req->rect.right = rect.right + data->whole_rect.left;
116 req->rect.bottom = rect.bottom + data->whole_rect.top;
117 wine_server_call( req );
121 /* make position relative to client area instead of window */
122 OffsetRect( &rect, -data->client_rect.left, -data->client_rect.top );
123 RedrawWindow( hwnd, &rect, 0, flags );
127 /***********************************************************************
128 * SWP_DoWinPosChanging
130 static BOOL SWP_DoWinPosChanging( WINDOWPOS* pWinpos, RECT* pNewWindowRect, RECT* pNewClientRect )
134 /* Send WM_WINDOWPOSCHANGING message */
136 if (!(pWinpos->flags & SWP_NOSENDCHANGING))
137 SendMessageW( pWinpos->hwnd, WM_WINDOWPOSCHANGING, 0, (LPARAM)pWinpos );
139 if (!(wndPtr = WIN_GetPtr( pWinpos->hwnd )) ||
140 wndPtr == WND_OTHER_PROCESS || wndPtr == WND_DESKTOP) return FALSE;
142 /* Calculate new position and size */
144 *pNewWindowRect = wndPtr->rectWindow;
145 *pNewClientRect = (wndPtr->dwStyle & WS_MINIMIZE) ? wndPtr->rectWindow
146 : wndPtr->rectClient;
148 if (!(pWinpos->flags & SWP_NOSIZE))
150 pNewWindowRect->right = pNewWindowRect->left + pWinpos->cx;
151 pNewWindowRect->bottom = pNewWindowRect->top + pWinpos->cy;
153 if (!(pWinpos->flags & SWP_NOMOVE))
155 pNewWindowRect->left = pWinpos->x;
156 pNewWindowRect->top = pWinpos->y;
157 pNewWindowRect->right += pWinpos->x - wndPtr->rectWindow.left;
158 pNewWindowRect->bottom += pWinpos->y - wndPtr->rectWindow.top;
160 OffsetRect( pNewClientRect, pWinpos->x - wndPtr->rectWindow.left,
161 pWinpos->y - wndPtr->rectWindow.top );
163 pWinpos->flags |= SWP_NOCLIENTMOVE | SWP_NOCLIENTSIZE;
165 TRACE( "hwnd %p, after %p, swp %d,%d %dx%d flags %08x\n",
166 pWinpos->hwnd, pWinpos->hwndInsertAfter, pWinpos->x, pWinpos->y,
167 pWinpos->cx, pWinpos->cy, pWinpos->flags );
168 TRACE( "current %s style %08lx new %s\n",
169 wine_dbgstr_rect( &wndPtr->rectWindow ), wndPtr->dwStyle,
170 wine_dbgstr_rect( pNewWindowRect ));
172 WIN_ReleasePtr( wndPtr );
177 /***********************************************************************
180 * Compute the valid rects from the old and new client rect and WVR_* flags.
181 * Helper for WM_NCCALCSIZE handling.
183 static inline void get_valid_rects( const RECT *old_client, const RECT *new_client, UINT flags,
188 if (flags & WVR_REDRAW)
190 SetRectEmpty( &valid[0] );
191 SetRectEmpty( &valid[1] );
195 if (flags & WVR_VALIDRECTS)
197 if (!IntersectRect( &valid[0], &valid[0], new_client ) ||
198 !IntersectRect( &valid[1], &valid[1], old_client ))
200 SetRectEmpty( &valid[0] );
201 SetRectEmpty( &valid[1] );
204 flags = WVR_ALIGNLEFT | WVR_ALIGNTOP;
208 valid[0] = *new_client;
209 valid[1] = *old_client;
212 /* make sure the rectangles have the same size */
213 cx = min( valid[0].right - valid[0].left, valid[1].right - valid[1].left );
214 cy = min( valid[0].bottom - valid[0].top, valid[1].bottom - valid[1].top );
216 if (flags & WVR_ALIGNBOTTOM)
218 valid[0].top = valid[0].bottom - cy;
219 valid[1].top = valid[1].bottom - cy;
223 valid[0].bottom = valid[0].top + cy;
224 valid[1].bottom = valid[1].top + cy;
226 if (flags & WVR_ALIGNRIGHT)
228 valid[0].left = valid[0].right - cx;
229 valid[1].left = valid[1].right - cx;
233 valid[0].right = valid[0].left + cx;
234 valid[1].right = valid[1].left + cx;
239 /***********************************************************************
242 static UINT SWP_DoNCCalcSize( WINDOWPOS* pWinpos, const RECT* pNewWindowRect, RECT* pNewClientRect,
248 if (!(wndPtr = WIN_GetPtr( pWinpos->hwnd )) || wndPtr == WND_OTHER_PROCESS) return 0;
250 /* Send WM_NCCALCSIZE message to get new client area */
251 if( (pWinpos->flags & (SWP_FRAMECHANGED | SWP_NOSIZE)) != SWP_NOSIZE )
253 NCCALCSIZE_PARAMS params;
254 WINDOWPOS winposCopy;
256 params.rgrc[0] = *pNewWindowRect;
257 params.rgrc[1] = wndPtr->rectWindow;
258 params.rgrc[2] = wndPtr->rectClient;
259 params.lppos = &winposCopy;
260 winposCopy = *pWinpos;
261 WIN_ReleasePtr( wndPtr );
263 wvrFlags = SendMessageW( pWinpos->hwnd, WM_NCCALCSIZE, TRUE, (LPARAM)¶ms );
265 *pNewClientRect = params.rgrc[0];
267 if (!(wndPtr = WIN_GetPtr( pWinpos->hwnd )) || wndPtr == WND_OTHER_PROCESS) return 0;
269 TRACE( "hwnd %p old win %s old client %s new win %s new client %s\n", pWinpos->hwnd,
270 wine_dbgstr_rect(&wndPtr->rectWindow), wine_dbgstr_rect(&wndPtr->rectClient),
271 wine_dbgstr_rect(pNewWindowRect), wine_dbgstr_rect(pNewClientRect) );
273 if( pNewClientRect->left != wndPtr->rectClient.left ||
274 pNewClientRect->top != wndPtr->rectClient.top )
275 pWinpos->flags &= ~SWP_NOCLIENTMOVE;
277 if( (pNewClientRect->right - pNewClientRect->left !=
278 wndPtr->rectClient.right - wndPtr->rectClient.left))
279 pWinpos->flags &= ~SWP_NOCLIENTSIZE;
281 wvrFlags &= ~WVR_HREDRAW;
283 if (pNewClientRect->bottom - pNewClientRect->top !=
284 wndPtr->rectClient.bottom - wndPtr->rectClient.top)
285 pWinpos->flags &= ~SWP_NOCLIENTSIZE;
287 wvrFlags &= ~WVR_VREDRAW;
289 validRects[0] = params.rgrc[1];
290 validRects[1] = params.rgrc[2];
294 if (!(pWinpos->flags & SWP_NOMOVE) &&
295 (pNewClientRect->left != wndPtr->rectClient.left ||
296 pNewClientRect->top != wndPtr->rectClient.top))
297 pWinpos->flags &= ~SWP_NOCLIENTMOVE;
300 if (pWinpos->flags & (SWP_NOCOPYBITS | SWP_NOREDRAW | SWP_SHOWWINDOW | SWP_HIDEWINDOW))
302 SetRectEmpty( &validRects[0] );
303 SetRectEmpty( &validRects[1] );
305 else get_valid_rects( &wndPtr->rectClient, pNewClientRect, wvrFlags, validRects );
307 WIN_ReleasePtr( wndPtr );
312 struct move_owned_info
318 static BOOL CALLBACK move_owned_popups( HWND hwnd, LPARAM lparam )
320 struct move_owned_info *info = (struct move_owned_info *)lparam;
322 if (hwnd == info->owner) return FALSE;
323 if ((GetWindowLongW( hwnd, GWL_STYLE ) & WS_POPUP) &&
324 GetWindow( hwnd, GW_OWNER ) == info->owner)
326 SetWindowPos( hwnd, info->insert_after, 0, 0, 0, 0,
327 SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE |
328 SWP_NOSENDCHANGING | SWP_DEFERERASE );
329 info->insert_after = hwnd;
334 /***********************************************************************
337 * fix Z order taking into account owned popups -
338 * basically we need to maintain them above the window that owns them
340 * FIXME: hide/show owned popups when owner visibility changes.
342 static HWND SWP_DoOwnedPopups(HWND hwnd, HWND hwndInsertAfter)
344 HWND owner = GetWindow( hwnd, GW_OWNER );
345 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
346 struct move_owned_info info;
348 TRACE("(%p) hInsertAfter = %p\n", hwnd, hwndInsertAfter );
350 if ((style & WS_POPUP) && owner)
352 /* make sure this popup stays above the owner */
354 if( hwndInsertAfter != HWND_TOP )
356 HWND hwndLocalPrev = HWND_TOP;
357 HWND prev = GetWindow( owner, GW_HWNDPREV );
359 while (prev && prev != hwndInsertAfter)
361 if (hwndLocalPrev == HWND_TOP && GetWindowLongW( prev, GWL_STYLE ) & WS_VISIBLE)
362 hwndLocalPrev = prev;
363 prev = GetWindow( prev, GW_HWNDPREV );
365 if (!prev) hwndInsertAfter = hwndLocalPrev;
368 else if (style & WS_CHILD) return hwndInsertAfter;
371 info.insert_after = hwndInsertAfter;
372 EnumWindows( move_owned_popups, (LPARAM)&info );
373 return info.insert_after;
377 /* fix redundant flags and values in the WINDOWPOS structure */
378 static BOOL fixup_flags( WINDOWPOS *winpos )
381 WND *wndPtr = WIN_GetPtr( winpos->hwnd );
384 if (!wndPtr || wndPtr == WND_OTHER_PROCESS)
386 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
389 winpos->hwnd = wndPtr->hwndSelf; /* make it a full handle */
391 /* Finally make sure that all coordinates are valid */
392 if (winpos->x < -32768) winpos->x = -32768;
393 else if (winpos->x > 32767) winpos->x = 32767;
394 if (winpos->y < -32768) winpos->y = -32768;
395 else if (winpos->y > 32767) winpos->y = 32767;
397 if (winpos->cx < 0) winpos->cx = 0;
398 else if (winpos->cx > 32767) winpos->cx = 32767;
399 if (winpos->cy < 0) winpos->cy = 0;
400 else if (winpos->cy > 32767) winpos->cy = 32767;
402 parent = GetAncestor( winpos->hwnd, GA_PARENT );
403 if (!IsWindowVisible( parent )) winpos->flags |= SWP_NOREDRAW;
405 if (wndPtr->dwStyle & WS_VISIBLE) winpos->flags &= ~SWP_SHOWWINDOW;
408 winpos->flags &= ~SWP_HIDEWINDOW;
409 if (!(winpos->flags & SWP_SHOWWINDOW)) winpos->flags |= SWP_NOREDRAW;
412 if ((wndPtr->rectWindow.right - wndPtr->rectWindow.left == winpos->cx) &&
413 (wndPtr->rectWindow.bottom - wndPtr->rectWindow.top == winpos->cy))
414 winpos->flags |= SWP_NOSIZE; /* Already the right size */
416 if ((wndPtr->rectWindow.left == winpos->x) && (wndPtr->rectWindow.top == winpos->y))
417 winpos->flags |= SWP_NOMOVE; /* Already the right position */
419 if ((wndPtr->dwStyle & (WS_POPUP | WS_CHILD)) != WS_CHILD)
421 if (!(winpos->flags & (SWP_NOACTIVATE|SWP_HIDEWINDOW))) /* Bring to the top when activating */
423 winpos->flags &= ~SWP_NOZORDER;
424 winpos->hwndInsertAfter = HWND_TOP;
428 /* Check hwndInsertAfter */
429 if (winpos->flags & SWP_NOZORDER) goto done;
431 /* fix sign extension */
432 if (winpos->hwndInsertAfter == (HWND)0xffff) winpos->hwndInsertAfter = HWND_TOPMOST;
433 else if (winpos->hwndInsertAfter == (HWND)0xfffe) winpos->hwndInsertAfter = HWND_NOTOPMOST;
435 /* FIXME: TOPMOST not supported yet */
436 if ((winpos->hwndInsertAfter == HWND_TOPMOST) ||
437 (winpos->hwndInsertAfter == HWND_NOTOPMOST)) winpos->hwndInsertAfter = HWND_TOP;
439 /* hwndInsertAfter must be a sibling of the window */
440 if (winpos->hwndInsertAfter == HWND_TOP)
442 if (GetWindow(winpos->hwnd, GW_HWNDFIRST) == winpos->hwnd)
443 winpos->flags |= SWP_NOZORDER;
445 else if (winpos->hwndInsertAfter == HWND_BOTTOM)
447 if (GetWindow(winpos->hwnd, GW_HWNDLAST) == winpos->hwnd)
448 winpos->flags |= SWP_NOZORDER;
452 if (GetAncestor( winpos->hwndInsertAfter, GA_PARENT ) != parent) ret = FALSE;
455 /* don't need to change the Zorder of hwnd if it's already inserted
456 * after hwndInsertAfter or when inserting hwnd after itself.
458 if ((winpos->hwnd == winpos->hwndInsertAfter) ||
459 (winpos->hwnd == GetWindow( winpos->hwndInsertAfter, GW_HWNDNEXT )))
460 winpos->flags |= SWP_NOZORDER;
464 WIN_ReleasePtr( wndPtr );
469 /***********************************************************************
470 * SetWindowStyle (X11DRV.@)
472 * Update the X state of a window to reflect a style change
474 void X11DRV_SetWindowStyle( HWND hwnd, DWORD old_style )
476 Display *display = thread_display();
477 struct x11drv_win_data *data;
478 DWORD new_style, changed;
480 if (hwnd == GetDesktopWindow()) return;
481 if (!(data = X11DRV_get_win_data( hwnd ))) return;
483 new_style = GetWindowLongW( hwnd, GWL_STYLE );
484 changed = new_style ^ old_style;
486 if (changed & WS_VISIBLE)
488 if (data->whole_window && X11DRV_is_window_rect_mapped( &data->window_rect ))
490 if (new_style & WS_VISIBLE)
492 TRACE( "mapping win %p\n", hwnd );
493 X11DRV_sync_window_style( display, data );
494 X11DRV_set_wm_hints( display, data );
496 XMapWindow( display, data->whole_window );
499 /* we don't unmap windows, that causes trouble with the window manager */
501 invalidate_dce( hwnd, &data->window_rect );
504 if (changed & WS_DISABLED)
506 if (data->whole_window && data->managed)
510 if (!(wm_hints = XGetWMHints( display, data->whole_window )))
511 wm_hints = XAllocWMHints();
514 wm_hints->flags |= InputHint;
515 wm_hints->input = !(new_style & WS_DISABLED);
516 XSetWMHints( display, data->whole_window, wm_hints );
525 /***********************************************************************
526 * update_fullscreen_state
528 * Use the NETWM protocol to set the fullscreen state.
529 * This only works for mapped windows.
531 static void update_fullscreen_state( Display *display, struct x11drv_win_data *data,
532 const RECT *old_client_rect, const RECT *old_screen_rect )
535 BOOL old_fs_state = FALSE, new_fs_state = FALSE;
537 if (old_client_rect->left <= 0 && old_client_rect->right >= old_screen_rect->right &&
538 old_client_rect->top <= 0 && old_client_rect->bottom >= old_screen_rect->bottom)
541 if (data->client_rect.left <= 0 && data->client_rect.right >= screen_width &&
542 data->client_rect.top <= 0 && data->client_rect.bottom >= screen_height)
545 if (new_fs_state == old_fs_state) return;
547 TRACE("setting fullscreen state for hwnd %p to %s\n", data->hwnd, new_fs_state ? "true" : "false");
549 if (data->whole_window)
551 xev.xclient.type = ClientMessage;
552 xev.xclient.window = data->whole_window;
553 xev.xclient.message_type = x11drv_atom(_NET_WM_STATE);
554 xev.xclient.serial = 0;
555 xev.xclient.display = display;
556 xev.xclient.send_event = True;
557 xev.xclient.format = 32;
558 xev.xclient.data.l[0] = new_fs_state ? _NET_WM_STATE_ADD : _NET_WM_STATE_REMOVE;
559 xev.xclient.data.l[1] = x11drv_atom(_NET_WM_STATE_FULLSCREEN);
560 xev.xclient.data.l[2] = 0;
562 XSendEvent(display, root_window, False, SubstructureRedirectMask | SubstructureNotifyMask, &xev);
568 /***********************************************************************
569 * X11DRV_set_window_pos
571 * Set a window position and Z order.
573 BOOL X11DRV_set_window_pos( HWND hwnd, HWND insert_after, const RECT *rectWindow,
574 const RECT *rectClient, UINT swp_flags, const RECT *valid_rects )
576 struct x11drv_win_data *data;
577 RECT new_whole_rect, old_client_rect, old_screen_rect;
579 DWORD old_style, new_style;
582 if (!(data = X11DRV_get_win_data( hwnd ))) return FALSE;
584 new_whole_rect = *rectWindow;
585 X11DRV_window_to_X_rect( data, &new_whole_rect );
587 old_client_rect = data->client_rect;
589 if (!IsRectEmpty( &valid_rects[0] ))
591 int x_offset = 0, y_offset = 0;
593 if (data->whole_window)
595 /* the X server will move the bits for us */
596 x_offset = data->whole_rect.left - new_whole_rect.left;
597 y_offset = data->whole_rect.top - new_whole_rect.top;
600 if (x_offset != valid_rects[1].left - valid_rects[0].left ||
601 y_offset != valid_rects[1].top - valid_rects[0].top)
603 /* FIXME: should copy the window bits here */
608 if (!(win = WIN_GetPtr( hwnd ))) return FALSE;
609 if (win == WND_OTHER_PROCESS)
611 if (IsWindow( hwnd )) ERR( "cannot set rectangles of other process window %p\n", hwnd );
614 SERVER_START_REQ( set_window_pos )
617 req->previous = insert_after;
618 req->flags = swp_flags;
619 req->window.left = rectWindow->left;
620 req->window.top = rectWindow->top;
621 req->window.right = rectWindow->right;
622 req->window.bottom = rectWindow->bottom;
623 req->client.left = rectClient->left;
624 req->client.top = rectClient->top;
625 req->client.right = rectClient->right;
626 req->client.bottom = rectClient->bottom;
627 if (memcmp( rectWindow, &new_whole_rect, sizeof(RECT) ) || !IsRectEmpty( &valid_rects[0] ))
629 wine_server_add_data( req, &new_whole_rect, sizeof(new_whole_rect) );
630 if (!IsRectEmpty( &valid_rects[0] ))
631 wine_server_add_data( req, valid_rects, 2 * sizeof(*valid_rects) );
633 ret = !wine_server_call( req );
634 new_style = reply->new_style;
638 if (win == WND_DESKTOP || data->whole_window == DefaultRootWindow(gdi_display))
640 data->whole_rect = data->client_rect = data->window_rect = *rectWindow;
641 if (win != WND_DESKTOP)
643 win->rectWindow = *rectWindow;
644 win->rectClient = *rectClient;
645 win->dwStyle = new_style;
646 WIN_ReleasePtr( win );
653 Display *display = thread_display();
655 /* invalidate DCEs */
657 if ((((swp_flags & SWP_AGG_NOPOSCHANGE) != SWP_AGG_NOPOSCHANGE) && (new_style & WS_VISIBLE)) ||
658 (swp_flags & (SWP_HIDEWINDOW | SWP_SHOWWINDOW)))
661 UnionRect( &rect, rectWindow, &win->rectWindow );
662 invalidate_dce( hwnd, &rect );
665 win->rectWindow = *rectWindow;
666 win->rectClient = *rectClient;
667 old_style = win->dwStyle;
668 win->dwStyle = new_style;
669 data->window_rect = *rectWindow;
671 TRACE( "win %p window %s client %s style %08lx\n",
672 hwnd, wine_dbgstr_rect(rectWindow), wine_dbgstr_rect(rectClient), new_style );
674 /* FIXME: copy the valid bits */
676 if (data->whole_window && !data->lock_changes)
678 if ((old_style & WS_VISIBLE) && !(new_style & WS_VISIBLE))
680 /* window got hidden, unmap it */
681 TRACE( "unmapping win %p\n", hwnd );
683 XUnmapWindow( display, data->whole_window );
686 else if ((new_style & WS_VISIBLE) && !X11DRV_is_window_rect_mapped( rectWindow ))
688 /* resizing to zero size or off screen -> unmap */
689 TRACE( "unmapping zero size or off-screen win %p\n", hwnd );
691 XUnmapWindow( display, data->whole_window );
696 X11DRV_sync_window_position( display, data, swp_flags, rectClient, &new_whole_rect );
698 if (data->whole_window && !data->lock_changes)
700 if ((new_style & WS_VISIBLE) && !(new_style & WS_MINIMIZE) &&
701 X11DRV_is_window_rect_mapped( rectWindow ))
703 if (!(old_style & WS_VISIBLE))
705 /* window got shown, map it */
706 TRACE( "mapping win %p\n", hwnd );
707 X11DRV_sync_window_style( display, data );
708 X11DRV_set_wm_hints( display, data );
710 XMapWindow( display, data->whole_window );
713 else if ((swp_flags & (SWP_NOSIZE | SWP_NOMOVE)) != (SWP_NOSIZE | SWP_NOMOVE))
715 /* resizing from zero size to non-zero -> map */
716 TRACE( "mapping non zero size or off-screen win %p\n", hwnd );
718 XMapWindow( display, data->whole_window );
721 SetRect( &old_screen_rect, 0, 0, screen_width, screen_height );
722 update_fullscreen_state( display, data, &old_client_rect, &old_screen_rect );
726 WIN_ReleasePtr( win );
731 /***********************************************************************
732 * SetWindowPos (X11DRV.@)
734 BOOL X11DRV_SetWindowPos( WINDOWPOS *winpos )
736 RECT newWindowRect, newClientRect, valid_rects[2];
739 TRACE( "hwnd %p, after %p, swp %d,%d %dx%d flags %08x\n",
740 winpos->hwnd, winpos->hwndInsertAfter, winpos->x, winpos->y,
741 winpos->cx, winpos->cy, winpos->flags);
743 orig_flags = winpos->flags;
745 /* First make sure that coordinates are valid for WM_WINDOWPOSCHANGING */
746 if (!(winpos->flags & SWP_NOMOVE))
748 if (winpos->x < -32768) winpos->x = -32768;
749 else if (winpos->x > 32767) winpos->x = 32767;
750 if (winpos->y < -32768) winpos->y = -32768;
751 else if (winpos->y > 32767) winpos->y = 32767;
753 if (!(winpos->flags & SWP_NOSIZE))
755 if (winpos->cx < 0) winpos->cx = 0;
756 else if (winpos->cx > 32767) winpos->cx = 32767;
757 if (winpos->cy < 0) winpos->cy = 0;
758 else if (winpos->cy > 32767) winpos->cy = 32767;
761 if (!SWP_DoWinPosChanging( winpos, &newWindowRect, &newClientRect )) return FALSE;
763 /* Fix redundant flags */
764 if (!fixup_flags( winpos )) return FALSE;
766 if((winpos->flags & (SWP_NOZORDER | SWP_HIDEWINDOW | SWP_SHOWWINDOW)) != SWP_NOZORDER)
768 if (GetAncestor( winpos->hwnd, GA_PARENT ) == GetDesktopWindow())
769 winpos->hwndInsertAfter = SWP_DoOwnedPopups( winpos->hwnd, winpos->hwndInsertAfter );
772 /* Common operations */
774 SWP_DoNCCalcSize( winpos, &newWindowRect, &newClientRect, valid_rects );
776 if (!X11DRV_set_window_pos( winpos->hwnd, winpos->hwndInsertAfter,
777 &newWindowRect, &newClientRect, orig_flags, valid_rects ))
780 if (!(orig_flags & SWP_SHOWWINDOW))
782 UINT rdw_flags = RDW_FRAME | RDW_ERASE;
783 if ( !(orig_flags & SWP_DEFERERASE) ) rdw_flags |= RDW_ERASENOW;
784 RedrawWindow( winpos->hwnd, NULL, NULL, rdw_flags );
787 if( winpos->flags & SWP_HIDEWINDOW )
788 HideCaret(winpos->hwnd);
789 else if (winpos->flags & SWP_SHOWWINDOW)
790 ShowCaret(winpos->hwnd);
792 if (!(winpos->flags & (SWP_NOACTIVATE|SWP_HIDEWINDOW)))
794 /* child windows get WM_CHILDACTIVATE message */
795 if ((GetWindowLongW( winpos->hwnd, GWL_STYLE ) & (WS_CHILD | WS_POPUP)) == WS_CHILD)
796 SendMessageW( winpos->hwnd, WM_CHILDACTIVATE, 0, 0 );
798 SetForegroundWindow( winpos->hwnd );
801 /* And last, send the WM_WINDOWPOSCHANGED message */
803 TRACE("\tstatus flags = %04x\n", winpos->flags & SWP_AGG_STATUSFLAGS);
805 if (((winpos->flags & SWP_AGG_STATUSFLAGS) != SWP_AGG_NOPOSCHANGE))
807 /* WM_WINDOWPOSCHANGED is sent even if SWP_NOSENDCHANGING is set
808 and always contains final window position.
810 winpos->x = newWindowRect.left;
811 winpos->y = newWindowRect.top;
812 winpos->cx = newWindowRect.right - newWindowRect.left;
813 winpos->cy = newWindowRect.bottom - newWindowRect.top;
814 SendMessageW( winpos->hwnd, WM_WINDOWPOSCHANGED, 0, (LPARAM)winpos );
821 /***********************************************************************
824 * Find a suitable place for an iconic window.
826 static POINT WINPOS_FindIconPos( HWND hwnd, POINT pt )
828 RECT rect, rectParent;
831 int xspacing, yspacing;
833 parent = GetAncestor( hwnd, GA_PARENT );
834 GetClientRect( parent, &rectParent );
835 if ((pt.x >= rectParent.left) && (pt.x + GetSystemMetrics(SM_CXICON) < rectParent.right) &&
836 (pt.y >= rectParent.top) && (pt.y + GetSystemMetrics(SM_CYICON) < rectParent.bottom))
837 return pt; /* The icon already has a suitable position */
839 xspacing = GetSystemMetrics(SM_CXICONSPACING);
840 yspacing = GetSystemMetrics(SM_CYICONSPACING);
842 /* Check if another icon already occupies this spot */
843 /* FIXME: this is completely inefficient */
845 hrgn = CreateRectRgn( 0, 0, 0, 0 );
846 tmp = CreateRectRgn( 0, 0, 0, 0 );
847 for (child = GetWindow( parent, GW_HWNDFIRST ); child; child = GetWindow( child, GW_HWNDNEXT ))
850 if (child == hwnd) continue;
851 if ((GetWindowLongW( child, GWL_STYLE ) & (WS_VISIBLE|WS_MINIMIZE)) != (WS_VISIBLE|WS_MINIMIZE))
853 if (!(childPtr = WIN_GetPtr( child )) || childPtr == WND_OTHER_PROCESS)
855 SetRectRgn( tmp, childPtr->rectWindow.left, childPtr->rectWindow.top,
856 childPtr->rectWindow.right, childPtr->rectWindow.bottom );
857 CombineRgn( hrgn, hrgn, tmp, RGN_OR );
858 WIN_ReleasePtr( childPtr );
862 for (rect.bottom = rectParent.bottom; rect.bottom >= yspacing; rect.bottom -= yspacing)
864 for (rect.left = rectParent.left; rect.left <= rectParent.right - xspacing; rect.left += xspacing)
866 rect.right = rect.left + xspacing;
867 rect.top = rect.bottom - yspacing;
868 if (!RectInRegion( hrgn, &rect ))
870 /* No window was found, so it's OK for us */
871 pt.x = rect.left + (xspacing - GetSystemMetrics(SM_CXICON)) / 2;
872 pt.y = rect.top + (yspacing - GetSystemMetrics(SM_CYICON)) / 2;
873 DeleteObject( hrgn );
878 DeleteObject( hrgn );
887 UINT WINPOS_MinMaximize( HWND hwnd, UINT cmd, LPRECT rect )
895 TRACE("%p %u\n", hwnd, cmd );
897 wpl.length = sizeof(wpl);
898 GetWindowPlacement( hwnd, &wpl );
900 if (HOOK_CallHooks( WH_CBT, HCBT_MINMAX, (WPARAM)hwnd, cmd, TRUE ))
901 return SWP_NOSIZE | SWP_NOMOVE;
903 if (IsIconic( hwnd ))
907 case SW_SHOWMINNOACTIVE:
908 case SW_SHOWMINIMIZED:
909 case SW_FORCEMINIMIZE:
911 return SWP_NOSIZE | SWP_NOMOVE;
913 if (!SendMessageW( hwnd, WM_QUERYOPEN, 0, 0 )) return SWP_NOSIZE | SWP_NOMOVE;
914 swpFlags |= SWP_NOCOPYBITS;
919 case SW_SHOWMINNOACTIVE:
920 case SW_SHOWMINIMIZED:
921 case SW_FORCEMINIMIZE:
923 if (!(wndPtr = WIN_GetPtr( hwnd )) || wndPtr == WND_OTHER_PROCESS) return 0;
924 if( wndPtr->dwStyle & WS_MAXIMIZE) wndPtr->flags |= WIN_RESTORE_MAX;
925 else wndPtr->flags &= ~WIN_RESTORE_MAX;
926 WIN_ReleasePtr( wndPtr );
928 WIN_SetStyle( hwnd, WS_MINIMIZE, WS_MAXIMIZE );
930 X11DRV_set_iconic_state( hwnd );
932 wpl.ptMinPosition = WINPOS_FindIconPos( hwnd, wpl.ptMinPosition );
934 SetRect( rect, wpl.ptMinPosition.x, wpl.ptMinPosition.y,
935 GetSystemMetrics(SM_CXICON), GetSystemMetrics(SM_CYICON) );
936 swpFlags |= SWP_NOCOPYBITS;
940 old_style = GetWindowLongW( hwnd, GWL_STYLE );
941 if ((old_style & WS_MAXIMIZE) && (old_style & WS_CHILD)) return SWP_NOSIZE | SWP_NOMOVE;
943 WINPOS_GetMinMaxInfo( hwnd, &size, &wpl.ptMaxPosition, NULL, NULL );
945 old_style = WIN_SetStyle( hwnd, WS_MAXIMIZE, WS_MINIMIZE );
946 if (old_style & WS_MINIMIZE)
948 WINPOS_ShowIconTitle( hwnd, FALSE );
949 X11DRV_set_iconic_state( hwnd );
951 SetRect( rect, wpl.ptMaxPosition.x, wpl.ptMaxPosition.y, size.x, size.y );
955 old_style = WIN_SetStyle( hwnd, 0, WS_MINIMIZE | WS_MAXIMIZE );
956 if (old_style & WS_MINIMIZE)
960 WINPOS_ShowIconTitle( hwnd, FALSE );
961 X11DRV_set_iconic_state( hwnd );
963 if (!(wndPtr = WIN_GetPtr( hwnd )) || wndPtr == WND_OTHER_PROCESS) return 0;
964 restore_max = (wndPtr->flags & WIN_RESTORE_MAX) != 0;
965 WIN_ReleasePtr( wndPtr );
968 /* Restore to maximized position */
969 WINPOS_GetMinMaxInfo( hwnd, &size, &wpl.ptMaxPosition, NULL, NULL);
970 WIN_SetStyle( hwnd, WS_MAXIMIZE, 0 );
971 SetRect( rect, wpl.ptMaxPosition.x, wpl.ptMaxPosition.y, size.x, size.y );
975 else if (!(old_style & WS_MAXIMIZE)) break;
977 /* Restore to normal position */
979 *rect = wpl.rcNormalPosition;
980 rect->right -= rect->left;
981 rect->bottom -= rect->top;
990 /***********************************************************************
991 * ShowWindow (X11DRV.@)
993 BOOL X11DRV_ShowWindow( HWND hwnd, INT cmd )
997 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
998 BOOL wasVisible = (style & WS_VISIBLE) != 0;
999 BOOL showFlag = TRUE, state_change = FALSE;
1000 RECT newPos = {0, 0, 0, 0};
1003 TRACE("hwnd=%p, cmd=%d, wasVisible %d\n", hwnd, cmd, wasVisible);
1008 if (!wasVisible) return FALSE;
1010 swp |= SWP_HIDEWINDOW | SWP_NOSIZE | SWP_NOMOVE;
1011 if (hwnd != GetActiveWindow())
1012 swp |= SWP_NOACTIVATE | SWP_NOZORDER;
1015 case SW_SHOWMINNOACTIVE:
1016 swp |= SWP_NOACTIVATE | SWP_NOZORDER;
1019 case SW_FORCEMINIMIZE: /* FIXME: Does not work if thread is hung. */
1020 if (style & WS_CHILD) swp |= SWP_NOACTIVATE | SWP_NOZORDER;
1022 case SW_SHOWMINIMIZED:
1023 swp |= SWP_SHOWWINDOW | SWP_FRAMECHANGED;
1024 swp |= WINPOS_MinMaximize( hwnd, cmd, &newPos );
1025 if (style & WS_MINIMIZE) return wasVisible;
1026 state_change = TRUE;
1029 case SW_SHOWMAXIMIZED: /* same as SW_MAXIMIZE */
1030 swp |= SWP_SHOWWINDOW | SWP_FRAMECHANGED;
1031 swp |= WINPOS_MinMaximize( hwnd, SW_MAXIMIZE, &newPos );
1032 if ((style & WS_MAXIMIZE) && (style & WS_CHILD)) return wasVisible;
1033 state_change = TRUE;
1037 swp |= SWP_NOACTIVATE | SWP_SHOWWINDOW | SWP_NOSIZE | SWP_NOMOVE;
1038 if (style & WS_CHILD) swp |= SWP_NOZORDER;
1041 if (wasVisible) return TRUE;
1042 swp |= SWP_SHOWWINDOW | SWP_NOSIZE | SWP_NOMOVE;
1043 if (style & WS_CHILD) swp |= SWP_NOACTIVATE | SWP_NOZORDER;
1047 swp |= SWP_FRAMECHANGED;
1048 state_change = TRUE;
1050 case SW_SHOWNOACTIVATE:
1051 swp |= SWP_NOACTIVATE | SWP_NOZORDER;
1053 case SW_SHOWNORMAL: /* same as SW_NORMAL: */
1054 case SW_SHOWDEFAULT: /* FIXME: should have its own handler */
1055 swp |= SWP_SHOWWINDOW;
1056 if (style & (WS_MINIMIZE | WS_MAXIMIZE))
1057 swp |= WINPOS_MinMaximize( hwnd, SW_RESTORE, &newPos );
1058 else swp |= SWP_NOSIZE | SWP_NOMOVE;
1059 if (style & WS_CHILD) swp |= SWP_NOACTIVATE | SWP_NOZORDER;
1063 if ((showFlag != wasVisible || cmd == SW_SHOWNA) && !state_change)
1065 SendMessageW( hwnd, WM_SHOWWINDOW, showFlag, 0 );
1066 if (!IsWindow( hwnd )) return wasVisible;
1069 parent = GetAncestor( hwnd, GA_PARENT );
1070 if (parent && !IsWindowVisible( parent ) && !state_change)
1072 /* if parent is not visible simply toggle WS_VISIBLE and return */
1073 if (showFlag) WIN_SetStyle( hwnd, WS_VISIBLE, 0 );
1074 else WIN_SetStyle( hwnd, 0, WS_VISIBLE );
1078 if (style & WS_CHILD)
1082 /* it appears that Windows always adds an undocumented 0x8000
1083 * flag if the state of a window changes.
1085 swp |= SWP_STATECHANGED;
1089 SetWindowPos( hwnd, HWND_TOP, newPos.left, newPos.top,
1090 newPos.right, newPos.bottom, LOWORD(swp) );
1097 /* FIXME: This will cause the window to be activated irrespective
1098 * of whether it is owned by the same thread. Has to be done
1102 if (hwnd == GetActiveWindow())
1103 WINPOS_ActivateOtherWindow(hwnd);
1105 /* Revert focus to parent */
1106 hFocus = GetFocus();
1107 if (hwnd == hFocus || IsChild(hwnd, hFocus))
1109 HWND parent = GetAncestor(hwnd, GA_PARENT);
1110 if (parent == GetDesktopWindow()) parent = 0;
1115 if (IsIconic(hwnd)) WINPOS_ShowIconTitle( hwnd, TRUE );
1117 if (!(wndPtr = WIN_GetPtr( hwnd )) || wndPtr == WND_OTHER_PROCESS) return wasVisible;
1119 if (wndPtr->flags & WIN_NEED_SIZE)
1121 /* should happen only in CreateWindowEx() */
1122 int wParam = SIZE_RESTORED;
1123 RECT client = wndPtr->rectClient;
1125 wndPtr->flags &= ~WIN_NEED_SIZE;
1126 if (wndPtr->dwStyle & WS_MAXIMIZE) wParam = SIZE_MAXIMIZED;
1127 else if (wndPtr->dwStyle & WS_MINIMIZE) wParam = SIZE_MINIMIZED;
1128 WIN_ReleasePtr( wndPtr );
1130 SendMessageW( hwnd, WM_SIZE, wParam,
1131 MAKELONG( client.right - client.left, client.bottom - client.top ));
1132 SendMessageW( hwnd, WM_MOVE, 0, MAKELONG( client.left, client.top ));
1134 else WIN_ReleasePtr( wndPtr );
1140 /**********************************************************************
1143 void X11DRV_MapNotify( HWND hwnd, XEvent *event )
1145 struct x11drv_win_data *data;
1146 HWND hwndFocus = GetFocus();
1149 if (!(data = X11DRV_get_win_data( hwnd ))) return;
1151 if (!(win = WIN_GetPtr( hwnd ))) return;
1153 if (data->managed && (win->dwStyle & WS_VISIBLE) && (win->dwStyle & WS_MINIMIZE))
1156 unsigned int width, height, border, depth;
1159 LONG style = WS_VISIBLE;
1163 XGetGeometry( event->xmap.display, data->whole_window, &root, &x, &y, &width, &height,
1165 XTranslateCoordinates( event->xmap.display, data->whole_window, root, 0, 0, &x, &y, &top );
1166 wine_tsx11_unlock();
1169 rect.right = x + width;
1170 rect.bottom = y + height;
1171 X11DRV_X_to_window_rect( data, &rect );
1173 invalidate_dce( hwnd, &data->window_rect );
1175 if (win->flags & WIN_RESTORE_MAX) style |= WS_MAXIMIZE;
1176 WIN_SetStyle( hwnd, style, WS_MINIMIZE );
1177 WIN_ReleasePtr( win );
1179 SendMessageW( hwnd, WM_SHOWWINDOW, SW_RESTORE, 0 );
1180 data->lock_changes++;
1181 SetWindowPos( hwnd, 0, rect.left, rect.top, rect.right-rect.left, rect.bottom-rect.top,
1183 data->lock_changes--;
1185 else WIN_ReleasePtr( win );
1186 if (hwndFocus && IsChild( hwnd, hwndFocus )) X11DRV_SetFocus(hwndFocus); /* FIXME */
1190 /**********************************************************************
1191 * X11DRV_UnmapNotify
1193 void X11DRV_UnmapNotify( HWND hwnd, XEvent *event )
1195 struct x11drv_win_data *data;
1198 if (!(data = X11DRV_get_win_data( hwnd ))) return;
1200 if (!(win = WIN_GetPtr( hwnd ))) return;
1202 if ((win->dwStyle & WS_VISIBLE) && data->managed &&
1203 X11DRV_is_window_rect_mapped( &win->rectWindow ))
1205 if (win->dwStyle & WS_MAXIMIZE)
1206 win->flags |= WIN_RESTORE_MAX;
1208 win->flags &= ~WIN_RESTORE_MAX;
1210 WIN_SetStyle( hwnd, WS_MINIMIZE, WS_MAXIMIZE );
1211 WIN_ReleasePtr( win );
1214 SendMessageW( hwnd, WM_SHOWWINDOW, SW_MINIMIZE, 0 );
1215 data->lock_changes++;
1216 SetWindowPos( hwnd, 0, 0, 0, GetSystemMetrics(SM_CXICON), GetSystemMetrics(SM_CYICON),
1217 SWP_NOMOVE | SWP_NOACTIVATE | SWP_NOZORDER );
1218 data->lock_changes--;
1220 else WIN_ReleasePtr( win );
1224 static BOOL CALLBACK update_windows_fullscreen_state( HWND hwnd, LPARAM lparam )
1226 struct x11drv_win_data *data;
1227 Display *display = thread_display();
1228 RECT *old_screen_rect = (RECT *)lparam;
1230 if ((data = X11DRV_get_win_data( hwnd )) &&
1231 (GetWindowLongW( hwnd, GWL_STYLE ) & WS_VISIBLE))
1233 update_fullscreen_state( display, data, &data->client_rect, old_screen_rect );
1239 /***********************************************************************
1240 * X11DRV_handle_desktop_resize
1242 void X11DRV_handle_desktop_resize( unsigned int width, unsigned int height )
1244 unsigned int old_screen_width, old_screen_height;
1246 HWND hwnd = GetDesktopWindow();
1247 struct x11drv_win_data *data;
1249 if (!(data = X11DRV_get_win_data( hwnd ))) return;
1251 old_screen_width = screen_width;
1252 old_screen_height = screen_height;
1254 screen_width = width;
1255 screen_height = height;
1256 TRACE("desktop %p change to (%dx%d)\n", hwnd, width, height);
1257 SetRect( &rect, 0, 0, width, height );
1258 data->lock_changes++;
1259 X11DRV_set_window_pos( hwnd, 0, &rect, &rect, SWP_NOZORDER|SWP_NOMOVE, NULL );
1260 data->lock_changes--;
1261 SendMessageTimeoutW( HWND_BROADCAST, WM_DISPLAYCHANGE, screen_depth,
1262 MAKELPARAM( width, height ), SMTO_ABORTIFHUNG, 2000, NULL );
1264 SetRect( &rect, 0, 0, old_screen_width, old_screen_height );
1265 EnumWindows( update_windows_fullscreen_state, (LPARAM)&rect );
1269 /***********************************************************************
1270 * X11DRV_ConfigureNotify
1272 void X11DRV_ConfigureNotify( HWND hwnd, XEvent *xev )
1274 XConfigureEvent *event = &xev->xconfigure;
1275 struct x11drv_win_data *data;
1278 int cx, cy, x = event->x, y = event->y;
1281 if (!(data = X11DRV_get_win_data( hwnd ))) return;
1285 if (!event->send_event) /* normal event, need to map coordinates to the root */
1289 XTranslateCoordinates( event->display, data->whole_window, root_window,
1290 0, 0, &x, &y, &child );
1291 wine_tsx11_unlock();
1295 rect.right = x + event->width;
1296 rect.bottom = y + event->height;
1297 TRACE( "win %p new X rect %ld,%ld,%ldx%ld (event %d,%d,%dx%d)\n",
1298 hwnd, rect.left, rect.top, rect.right-rect.left, rect.bottom-rect.top,
1299 event->x, event->y, event->width, event->height );
1300 X11DRV_X_to_window_rect( data, &rect );
1304 cx = rect.right - rect.left;
1305 cy = rect.bottom - rect.top;
1306 flags = SWP_NOACTIVATE | SWP_NOZORDER;
1308 /* Compare what has changed */
1310 GetWindowRect( hwnd, &rect );
1311 if (rect.left == x && rect.top == y) flags |= SWP_NOMOVE;
1313 TRACE( "%p moving from (%ld,%ld) to (%d,%d)\n",
1314 hwnd, rect.left, rect.top, x, y );
1316 if ((rect.right - rect.left == cx && rect.bottom - rect.top == cy) ||
1318 (IsRectEmpty( &rect ) && event->width == 1 && event->height == 1))
1320 if (flags & SWP_NOMOVE) return; /* if nothing changed, don't do anything */
1321 flags |= SWP_NOSIZE;
1324 TRACE( "%p resizing from (%ldx%ld) to (%dx%d)\n",
1325 hwnd, rect.right - rect.left, rect.bottom - rect.top, cx, cy );
1327 data->lock_changes++;
1328 SetWindowPos( hwnd, 0, x, y, cx, cy, flags );
1329 data->lock_changes--;
1333 /***********************************************************************
1334 * SetWindowRgn (X11DRV.@)
1336 * Assign specified region to window (for non-rectangular windows)
1338 int X11DRV_SetWindowRgn( HWND hwnd, HRGN hrgn, BOOL redraw )
1340 struct x11drv_win_data *data;
1342 if (!(data = X11DRV_get_win_data( hwnd )))
1344 if (IsWindow( hwnd ))
1345 FIXME( "not supported on other thread window %p\n", hwnd );
1346 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
1350 #ifdef HAVE_LIBXSHAPE
1351 if (data->whole_window)
1353 Display *display = thread_display();
1358 XShapeCombineMask( display, data->whole_window,
1359 ShapeBounding, 0, 0, None, ShapeSet );
1360 wine_tsx11_unlock();
1364 RGNDATA *pRegionData = X11DRV_GetRegionData( hrgn, 0 );
1368 XShapeCombineRectangles( display, data->whole_window, ShapeBounding,
1369 data->window_rect.left - data->whole_rect.left,
1370 data->window_rect.top - data->whole_rect.top,
1371 (XRectangle *)pRegionData->Buffer,
1372 pRegionData->rdh.nCount,
1373 ShapeSet, YXBanded );
1374 wine_tsx11_unlock();
1375 HeapFree(GetProcessHeap(), 0, pRegionData);
1379 #endif /* HAVE_LIBXSHAPE */
1381 invalidate_dce( hwnd, &data->window_rect );
1386 /***********************************************************************
1389 * Draw the frame used when moving or resizing window.
1391 * FIXME: This causes problems in Win95 mode. (why?)
1393 static void draw_moving_frame( HDC hdc, RECT *rect, BOOL thickframe )
1397 const int width = GetSystemMetrics(SM_CXFRAME);
1398 const int height = GetSystemMetrics(SM_CYFRAME);
1400 HBRUSH hbrush = SelectObject( hdc, GetStockObject( GRAY_BRUSH ) );
1401 PatBlt( hdc, rect->left, rect->top,
1402 rect->right - rect->left - width, height, PATINVERT );
1403 PatBlt( hdc, rect->left, rect->top + height, width,
1404 rect->bottom - rect->top - height, PATINVERT );
1405 PatBlt( hdc, rect->left + width, rect->bottom - 1,
1406 rect->right - rect->left - width, -height, PATINVERT );
1407 PatBlt( hdc, rect->right - 1, rect->top, -width,
1408 rect->bottom - rect->top - height, PATINVERT );
1409 SelectObject( hdc, hbrush );
1411 else DrawFocusRect( hdc, rect );
1415 /***********************************************************************
1418 * Initialisation of a move or resize, when initiatied from a menu choice.
1419 * Return hit test code for caption or sizing border.
1421 static LONG start_size_move( HWND hwnd, WPARAM wParam, POINT *capturePoint, LONG style )
1428 GetWindowRect( hwnd, &rectWindow );
1430 if ((wParam & 0xfff0) == SC_MOVE)
1432 /* Move pointer at the center of the caption */
1433 RECT rect = rectWindow;
1434 /* Note: to be exactly centered we should take the different types
1435 * of border into account, but it shouldn't make more that a few pixels
1436 * of difference so let's not bother with that */
1437 rect.top += GetSystemMetrics(SM_CYBORDER);
1438 if (style & WS_SYSMENU)
1439 rect.left += GetSystemMetrics(SM_CXSIZE) + 1;
1440 if (style & WS_MINIMIZEBOX)
1441 rect.right -= GetSystemMetrics(SM_CXSIZE) + 1;
1442 if (style & WS_MAXIMIZEBOX)
1443 rect.right -= GetSystemMetrics(SM_CXSIZE) + 1;
1444 pt.x = (rect.right + rect.left) / 2;
1445 pt.y = rect.top + GetSystemMetrics(SM_CYSIZE)/2;
1446 hittest = HTCAPTION;
1454 GetMessageW( &msg, 0, WM_KEYFIRST, WM_MOUSELAST );
1455 if (CallMsgFilterW( &msg, MSGF_SIZE )) continue;
1461 hittest = SendMessageW( hwnd, WM_NCHITTEST, 0, MAKELONG( pt.x, pt.y ) );
1462 if ((hittest < HTLEFT) || (hittest > HTBOTTOMRIGHT)) hittest = 0;
1473 pt.x =(rectWindow.left+rectWindow.right)/2;
1474 pt.y = rectWindow.top + GetSystemMetrics(SM_CYFRAME) / 2;
1478 pt.x =(rectWindow.left+rectWindow.right)/2;
1479 pt.y = rectWindow.bottom - GetSystemMetrics(SM_CYFRAME) / 2;
1483 pt.x = rectWindow.left + GetSystemMetrics(SM_CXFRAME) / 2;
1484 pt.y =(rectWindow.top+rectWindow.bottom)/2;
1488 pt.x = rectWindow.right - GetSystemMetrics(SM_CXFRAME) / 2;
1489 pt.y =(rectWindow.top+rectWindow.bottom)/2;
1492 case VK_ESCAPE: return 0;
1498 SetCursorPos( pt.x, pt.y );
1499 SendMessageW( hwnd, WM_SETCURSOR, (WPARAM)hwnd, MAKELONG( hittest, WM_MOUSEMOVE ));
1504 /***********************************************************************
1505 * set_movesize_capture
1507 static void set_movesize_capture( HWND hwnd )
1511 SERVER_START_REQ( set_capture_window )
1514 req->flags = CAPTURE_MOVESIZE;
1515 if (!wine_server_call_err( req ))
1517 previous = reply->previous;
1518 hwnd = reply->full_handle;
1522 if (previous && previous != hwnd)
1523 SendMessageW( previous, WM_CAPTURECHANGED, 0, (LPARAM)hwnd );
1526 /***********************************************************************
1527 * X11DRV_WMMoveResizeWindow
1529 * Tells the window manager to initiate a move or resize operation.
1532 * http://freedesktop.org/Standards/wm-spec/1.3/ar01s04.html
1533 * or search for "_NET_WM_MOVERESIZE"
1535 static void X11DRV_WMMoveResizeWindow( HWND hwnd, int x, int y, int dir )
1538 Display *display = thread_display();
1540 xev.xclient.type = ClientMessage;
1541 xev.xclient.window = X11DRV_get_whole_window(hwnd);
1542 xev.xclient.message_type = x11drv_atom(_NET_WM_MOVERESIZE);
1543 xev.xclient.serial = 0;
1544 xev.xclient.display = display;
1545 xev.xclient.send_event = True;
1546 xev.xclient.format = 32;
1547 xev.xclient.data.l[0] = x; /* x coord */
1548 xev.xclient.data.l[1] = y; /* y coord */
1549 xev.xclient.data.l[2] = dir; /* direction */
1550 xev.xclient.data.l[3] = 1; /* button */
1551 xev.xclient.data.l[4] = 0; /* unused */
1553 /* need to ungrab the pointer that may have been automatically grabbed
1554 * with a ButtonPress event */
1556 XUngrabPointer( display, CurrentTime );
1557 XSendEvent(display, root_window, False, SubstructureNotifyMask, &xev);
1558 wine_tsx11_unlock();
1561 /***********************************************************************
1562 * SysCommandSizeMove (X11DRV.@)
1564 * Perform SC_MOVE and SC_SIZE commands.
1566 void X11DRV_SysCommandSizeMove( HWND hwnd, WPARAM wParam )
1569 RECT sizingRect, mouseRect, origRect;
1572 LONG hittest = (LONG)(wParam & 0x0f);
1573 WPARAM syscommand = wParam & 0xfff0;
1574 HCURSOR hDragCursor = 0, hOldCursor = 0;
1575 POINT minTrack, maxTrack;
1576 POINT capturePoint, pt;
1577 LONG style = GetWindowLongA( hwnd, GWL_STYLE );
1578 BOOL thickframe = HAS_THICKFRAME( style );
1579 BOOL iconic = style & WS_MINIMIZE;
1581 DWORD dwPoint = GetMessagePos ();
1582 BOOL DragFullWindows = FALSE;
1584 Window parent_win, whole_win;
1585 Display *old_gdi_display = NULL;
1586 struct x11drv_thread_data *thread_data = x11drv_thread_data();
1587 struct x11drv_win_data *data;
1589 pt.x = (short)LOWORD(dwPoint);
1590 pt.y = (short)HIWORD(dwPoint);
1593 if (IsZoomed(hwnd) || !IsWindowVisible(hwnd)) return;
1595 if (!(data = X11DRV_get_win_data( hwnd ))) return;
1597 /* if we are managed then we let the WM do all the work */
1601 if (syscommand == SC_MOVE)
1603 if (!hittest) dir = _NET_WM_MOVERESIZE_MOVE_KEYBOARD;
1604 else dir = _NET_WM_MOVERESIZE_MOVE;
1606 else if (!hittest) dir = _NET_WM_MOVERESIZE_SIZE_KEYBOARD;
1610 case WMSZ_LEFT: dir = _NET_WM_MOVERESIZE_SIZE_LEFT; break;
1611 case WMSZ_RIGHT: dir = _NET_WM_MOVERESIZE_SIZE_RIGHT; break;
1612 case WMSZ_TOP: dir = _NET_WM_MOVERESIZE_SIZE_TOP; break;
1613 case WMSZ_TOPLEFT: dir = _NET_WM_MOVERESIZE_SIZE_TOPLEFT; break;
1614 case WMSZ_TOPRIGHT: dir = _NET_WM_MOVERESIZE_SIZE_TOPRIGHT; break;
1615 case WMSZ_BOTTOM: dir = _NET_WM_MOVERESIZE_SIZE_BOTTOM; break;
1616 case WMSZ_BOTTOMLEFT: dir = _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT; break;
1617 case WMSZ_BOTTOMRIGHT: dir = _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT; break;
1619 ERR("Invalid hittest value: %ld\n", hittest);
1620 dir = _NET_WM_MOVERESIZE_SIZE_KEYBOARD;
1622 X11DRV_WMMoveResizeWindow( hwnd, pt.x, pt.y, dir );
1626 SystemParametersInfoA(SPI_GETDRAGFULLWINDOWS, 0, &DragFullWindows, 0);
1628 if (syscommand == SC_MOVE)
1630 if (!hittest) hittest = start_size_move( hwnd, wParam, &capturePoint, style );
1631 if (!hittest) return;
1635 if ( hittest && (syscommand != SC_MOUSEMENU) )
1636 hittest += (HTLEFT - WMSZ_LEFT);
1639 set_movesize_capture( hwnd );
1640 hittest = start_size_move( hwnd, wParam, &capturePoint, style );
1643 set_movesize_capture(0);
1649 /* Get min/max info */
1651 WINPOS_GetMinMaxInfo( hwnd, NULL, NULL, &minTrack, &maxTrack );
1652 GetWindowRect( hwnd, &sizingRect );
1653 if (style & WS_CHILD)
1655 parent = GetParent(hwnd);
1656 /* make sizing rect relative to parent */
1657 MapWindowPoints( 0, parent, (POINT*)&sizingRect, 2 );
1658 GetClientRect( parent, &mouseRect );
1663 SetRect(&mouseRect, 0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN));
1665 origRect = sizingRect;
1667 if (ON_LEFT_BORDER(hittest))
1669 mouseRect.left = max( mouseRect.left, sizingRect.right-maxTrack.x );
1670 mouseRect.right = min( mouseRect.right, sizingRect.right-minTrack.x );
1672 else if (ON_RIGHT_BORDER(hittest))
1674 mouseRect.left = max( mouseRect.left, sizingRect.left+minTrack.x );
1675 mouseRect.right = min( mouseRect.right, sizingRect.left+maxTrack.x );
1677 if (ON_TOP_BORDER(hittest))
1679 mouseRect.top = max( mouseRect.top, sizingRect.bottom-maxTrack.y );
1680 mouseRect.bottom = min( mouseRect.bottom,sizingRect.bottom-minTrack.y);
1682 else if (ON_BOTTOM_BORDER(hittest))
1684 mouseRect.top = max( mouseRect.top, sizingRect.top+minTrack.y );
1685 mouseRect.bottom = min( mouseRect.bottom, sizingRect.top+maxTrack.y );
1687 if (parent) MapWindowPoints( parent, 0, (LPPOINT)&mouseRect, 2 );
1689 /* Retrieve a default cache DC (without using the window style) */
1690 hdc = GetDCEx( parent, 0, DCX_CACHE );
1692 if( iconic ) /* create a cursor for dragging */
1694 hDragCursor = (HCURSOR)GetClassLongPtrW( hwnd, GCLP_HICON);
1695 if( !hDragCursor ) hDragCursor = (HCURSOR)SendMessageW( hwnd, WM_QUERYDRAGICON, 0, 0L);
1696 if( !hDragCursor ) iconic = FALSE;
1699 /* repaint the window before moving it around */
1700 RedrawWindow( hwnd, NULL, 0, RDW_UPDATENOW | RDW_ALLCHILDREN );
1702 SendMessageW( hwnd, WM_ENTERSIZEMOVE, 0, 0 );
1703 set_movesize_capture( hwnd );
1705 /* grab the server only when moving top-level windows without desktop */
1706 grab = (!DragFullWindows && !parent && (root_window == DefaultRootWindow(gdi_display)));
1711 XSync( gdi_display, False );
1712 XGrabServer( thread_data->display );
1713 XSync( thread_data->display, False );
1714 /* switch gdi display to the thread display, since the server is grabbed */
1715 old_gdi_display = gdi_display;
1716 gdi_display = thread_data->display;
1717 wine_tsx11_unlock();
1719 whole_win = X11DRV_get_whole_window( GetAncestor(hwnd,GA_ROOT) );
1720 parent_win = parent ? X11DRV_get_whole_window( GetAncestor(parent,GA_ROOT) ) : root_window;
1723 XGrabPointer( thread_data->display, whole_win, False,
1724 PointerMotionMask | ButtonPressMask | ButtonReleaseMask,
1725 GrabModeAsync, GrabModeAsync, parent_win, None, CurrentTime );
1726 wine_tsx11_unlock();
1727 thread_data->grab_window = whole_win;
1733 if (!GetMessageW( &msg, 0, WM_KEYFIRST, WM_MOUSELAST )) break;
1734 if (CallMsgFilterW( &msg, MSGF_SIZE )) continue;
1736 /* Exit on button-up, Return, or Esc */
1737 if ((msg.message == WM_LBUTTONUP) ||
1738 ((msg.message == WM_KEYDOWN) &&
1739 ((msg.wParam == VK_RETURN) || (msg.wParam == VK_ESCAPE)))) break;
1741 if ((msg.message != WM_KEYDOWN) && (msg.message != WM_MOUSEMOVE))
1742 continue; /* We are not interested in other messages */
1746 if (msg.message == WM_KEYDOWN) switch(msg.wParam)
1748 case VK_UP: pt.y -= 8; break;
1749 case VK_DOWN: pt.y += 8; break;
1750 case VK_LEFT: pt.x -= 8; break;
1751 case VK_RIGHT: pt.x += 8; break;
1754 pt.x = max( pt.x, mouseRect.left );
1755 pt.x = min( pt.x, mouseRect.right );
1756 pt.y = max( pt.y, mouseRect.top );
1757 pt.y = min( pt.y, mouseRect.bottom );
1759 dx = pt.x - capturePoint.x;
1760 dy = pt.y - capturePoint.y;
1768 if( iconic ) /* ok, no system popup tracking */
1770 hOldCursor = SetCursor(hDragCursor);
1772 WINPOS_ShowIconTitle( hwnd, FALSE );
1774 else if(!DragFullWindows)
1775 draw_moving_frame( hdc, &sizingRect, thickframe );
1778 if (msg.message == WM_KEYDOWN) SetCursorPos( pt.x, pt.y );
1781 RECT newRect = sizingRect;
1782 WPARAM wpSizingHit = 0;
1784 if (hittest == HTCAPTION) OffsetRect( &newRect, dx, dy );
1785 if (ON_LEFT_BORDER(hittest)) newRect.left += dx;
1786 else if (ON_RIGHT_BORDER(hittest)) newRect.right += dx;
1787 if (ON_TOP_BORDER(hittest)) newRect.top += dy;
1788 else if (ON_BOTTOM_BORDER(hittest)) newRect.bottom += dy;
1789 if(!iconic && !DragFullWindows) draw_moving_frame( hdc, &sizingRect, thickframe );
1792 /* determine the hit location */
1793 if (hittest >= HTLEFT && hittest <= HTBOTTOMRIGHT)
1794 wpSizingHit = WMSZ_LEFT + (hittest - HTLEFT);
1795 SendMessageW( hwnd, WM_SIZING, wpSizingHit, (LPARAM)&newRect );
1799 if(!DragFullWindows)
1800 draw_moving_frame( hdc, &newRect, thickframe );
1802 SetWindowPos( hwnd, 0, newRect.left, newRect.top,
1803 newRect.right - newRect.left,
1804 newRect.bottom - newRect.top,
1805 ( hittest == HTCAPTION ) ? SWP_NOSIZE : 0 );
1807 sizingRect = newRect;
1812 set_movesize_capture(0);
1815 if( moved ) /* restore cursors, show icon title later on */
1817 ShowCursor( FALSE );
1818 SetCursor( hOldCursor );
1821 else if (moved && !DragFullWindows)
1822 draw_moving_frame( hdc, &sizingRect, thickframe );
1824 ReleaseDC( parent, hdc );
1827 XUngrabPointer( thread_data->display, CurrentTime );
1830 XSync( thread_data->display, False );
1831 XUngrabServer( thread_data->display );
1832 XSync( thread_data->display, False );
1833 gdi_display = old_gdi_display;
1835 wine_tsx11_unlock();
1836 thread_data->grab_window = None;
1838 if (HOOK_CallHooks( WH_CBT, HCBT_MOVESIZE, (WPARAM)hwnd, (LPARAM)&sizingRect, TRUE ))
1841 SendMessageW( hwnd, WM_EXITSIZEMOVE, 0, 0 );
1842 SendMessageW( hwnd, WM_SETVISIBLE, !IsIconic(hwnd), 0L);
1844 /* window moved or resized */
1847 /* if the moving/resizing isn't canceled call SetWindowPos
1848 * with the new position or the new size of the window
1850 if (!((msg.message == WM_KEYDOWN) && (msg.wParam == VK_ESCAPE)) )
1852 /* NOTE: SWP_NOACTIVATE prevents document window activation in Word 6 */
1853 if(!DragFullWindows)
1854 SetWindowPos( hwnd, 0, sizingRect.left, sizingRect.top,
1855 sizingRect.right - sizingRect.left,
1856 sizingRect.bottom - sizingRect.top,
1857 ( hittest == HTCAPTION ) ? SWP_NOSIZE : 0 );
1860 { /* restore previous size/position */
1862 SetWindowPos( hwnd, 0, origRect.left, origRect.top,
1863 origRect.right - origRect.left,
1864 origRect.bottom - origRect.top,
1865 ( hittest == HTCAPTION ) ? SWP_NOSIZE : 0 );
1871 /* Single click brings up the system menu when iconized */
1875 if(style & WS_SYSMENU )
1876 SendMessageW( hwnd, WM_SYSCOMMAND,
1877 SC_MOUSEMENU + HTSYSMENU, MAKELONG(pt.x,pt.y));
1879 else WINPOS_ShowIconTitle( hwnd, TRUE );