winspool: Implement EnumMonitorsA on top of EnumMonitorsW.
[wine/multimedia.git] / dlls / x11drv / winpos.c
blobf0fa3b8b9bc15f8f24eb9859e4201f2bafc990dd
1 /*
2 * Window position related functions.
4 * Copyright 1993, 1994, 1995, 2001 Alexandre Julliard
5 * Copyright 1995, 1996, 1999 Alex Korobka
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "config.h"
24 #include <X11/Xlib.h>
25 #ifdef HAVE_LIBXSHAPE
26 #include <X11/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 SendMessageA( winpos->hwnd, WM_CHILDACTIVATE, 0, 0 );
747 else
748 SetForegroundWindow( winpos->hwnd );
751 /* And last, send the WM_WINDOWPOSCHANGED message */
753 TRACE("\tstatus flags = %04x\n", winpos->flags & SWP_AGG_STATUSFLAGS);
755 if (((winpos->flags & SWP_AGG_STATUSFLAGS) != SWP_AGG_NOPOSCHANGE))
757 /* WM_WINDOWPOSCHANGED is sent even if SWP_NOSENDCHANGING is set
758 and always contains final window position.
760 winpos->x = newWindowRect.left;
761 winpos->y = newWindowRect.top;
762 winpos->cx = newWindowRect.right - newWindowRect.left;
763 winpos->cy = newWindowRect.bottom - newWindowRect.top;
764 SendMessageW( winpos->hwnd, WM_WINDOWPOSCHANGED, 0, (LPARAM)winpos );
767 return TRUE;
771 /***********************************************************************
772 * WINPOS_FindIconPos
774 * Find a suitable place for an iconic window.
776 static POINT WINPOS_FindIconPos( HWND hwnd, POINT pt )
778 RECT rect, rectParent;
779 HWND parent, child;
780 HRGN hrgn, tmp;
781 int xspacing, yspacing;
783 parent = GetAncestor( hwnd, GA_PARENT );
784 GetClientRect( parent, &rectParent );
785 if ((pt.x >= rectParent.left) && (pt.x + GetSystemMetrics(SM_CXICON) < rectParent.right) &&
786 (pt.y >= rectParent.top) && (pt.y + GetSystemMetrics(SM_CYICON) < rectParent.bottom))
787 return pt; /* The icon already has a suitable position */
789 xspacing = GetSystemMetrics(SM_CXICONSPACING);
790 yspacing = GetSystemMetrics(SM_CYICONSPACING);
792 /* Check if another icon already occupies this spot */
793 /* FIXME: this is completely inefficient */
795 hrgn = CreateRectRgn( 0, 0, 0, 0 );
796 tmp = CreateRectRgn( 0, 0, 0, 0 );
797 for (child = GetWindow( parent, GW_HWNDFIRST ); child; child = GetWindow( child, GW_HWNDNEXT ))
799 WND *childPtr;
800 if (child == hwnd) continue;
801 if ((GetWindowLongW( child, GWL_STYLE ) & (WS_VISIBLE|WS_MINIMIZE)) != (WS_VISIBLE|WS_MINIMIZE))
802 continue;
803 if (!(childPtr = WIN_GetPtr( child )) || childPtr == WND_OTHER_PROCESS)
804 continue;
805 SetRectRgn( tmp, childPtr->rectWindow.left, childPtr->rectWindow.top,
806 childPtr->rectWindow.right, childPtr->rectWindow.bottom );
807 CombineRgn( hrgn, hrgn, tmp, RGN_OR );
808 WIN_ReleasePtr( childPtr );
810 DeleteObject( tmp );
812 for (rect.bottom = rectParent.bottom; rect.bottom >= yspacing; rect.bottom -= yspacing)
814 for (rect.left = rectParent.left; rect.left <= rectParent.right - xspacing; rect.left += xspacing)
816 rect.right = rect.left + xspacing;
817 rect.top = rect.bottom - yspacing;
818 if (!RectInRegion( hrgn, &rect ))
820 /* No window was found, so it's OK for us */
821 pt.x = rect.left + (xspacing - GetSystemMetrics(SM_CXICON)) / 2;
822 pt.y = rect.top + (yspacing - GetSystemMetrics(SM_CYICON)) / 2;
823 DeleteObject( hrgn );
824 return pt;
828 DeleteObject( hrgn );
829 pt.x = pt.y = 0;
830 return pt;
837 UINT WINPOS_MinMaximize( HWND hwnd, UINT cmd, LPRECT rect )
839 WND *wndPtr;
840 UINT swpFlags = 0;
841 POINT size;
842 LONG old_style;
843 WINDOWPLACEMENT wpl;
845 TRACE("%p %u\n", hwnd, cmd );
847 wpl.length = sizeof(wpl);
848 GetWindowPlacement( hwnd, &wpl );
850 if (HOOK_CallHooks( WH_CBT, HCBT_MINMAX, (WPARAM)hwnd, cmd, TRUE ))
851 return SWP_NOSIZE | SWP_NOMOVE;
853 if (IsIconic( hwnd ))
855 if (cmd == SW_MINIMIZE) return SWP_NOSIZE | SWP_NOMOVE;
856 if (!SendMessageW( hwnd, WM_QUERYOPEN, 0, 0 )) return SWP_NOSIZE | SWP_NOMOVE;
857 swpFlags |= SWP_NOCOPYBITS;
860 switch( cmd )
862 case SW_MINIMIZE:
863 if (!(wndPtr = WIN_GetPtr( hwnd )) || wndPtr == WND_OTHER_PROCESS) return 0;
864 if( wndPtr->dwStyle & WS_MAXIMIZE) wndPtr->flags |= WIN_RESTORE_MAX;
865 else wndPtr->flags &= ~WIN_RESTORE_MAX;
866 WIN_ReleasePtr( wndPtr );
868 WIN_SetStyle( hwnd, WS_MINIMIZE, WS_MAXIMIZE );
870 X11DRV_set_iconic_state( hwnd );
872 wpl.ptMinPosition = WINPOS_FindIconPos( hwnd, wpl.ptMinPosition );
874 SetRect( rect, wpl.ptMinPosition.x, wpl.ptMinPosition.y,
875 GetSystemMetrics(SM_CXICON), GetSystemMetrics(SM_CYICON) );
876 swpFlags |= SWP_NOCOPYBITS;
877 break;
879 case SW_MAXIMIZE:
880 WINPOS_GetMinMaxInfo( hwnd, &size, &wpl.ptMaxPosition, NULL, NULL );
882 old_style = WIN_SetStyle( hwnd, WS_MAXIMIZE, WS_MINIMIZE );
883 if (old_style & WS_MINIMIZE)
885 WINPOS_ShowIconTitle( hwnd, FALSE );
886 X11DRV_set_iconic_state( hwnd );
888 SetRect( rect, wpl.ptMaxPosition.x, wpl.ptMaxPosition.y, size.x, size.y );
889 break;
891 case SW_RESTORE:
892 old_style = WIN_SetStyle( hwnd, 0, WS_MINIMIZE | WS_MAXIMIZE );
893 if (old_style & WS_MINIMIZE)
895 BOOL restore_max;
897 WINPOS_ShowIconTitle( hwnd, FALSE );
898 X11DRV_set_iconic_state( hwnd );
900 if (!(wndPtr = WIN_GetPtr( hwnd )) || wndPtr == WND_OTHER_PROCESS) return 0;
901 restore_max = (wndPtr->flags & WIN_RESTORE_MAX) != 0;
902 WIN_ReleasePtr( wndPtr );
903 if (restore_max)
905 /* Restore to maximized position */
906 WINPOS_GetMinMaxInfo( hwnd, &size, &wpl.ptMaxPosition, NULL, NULL);
907 WIN_SetStyle( hwnd, WS_MAXIMIZE, 0 );
908 SetRect( rect, wpl.ptMaxPosition.x, wpl.ptMaxPosition.y, size.x, size.y );
909 break;
912 else if (!(old_style & WS_MAXIMIZE)) break;
914 /* Restore to normal position */
916 *rect = wpl.rcNormalPosition;
917 rect->right -= rect->left;
918 rect->bottom -= rect->top;
920 break;
923 return swpFlags;
927 /***********************************************************************
928 * ShowWindow (X11DRV.@)
930 BOOL X11DRV_ShowWindow( HWND hwnd, INT cmd )
932 WND *wndPtr;
933 HWND parent;
934 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
935 BOOL wasVisible = (style & WS_VISIBLE) != 0;
936 BOOL showFlag = TRUE;
937 RECT newPos = {0, 0, 0, 0};
938 UINT swp = 0;
940 TRACE("hwnd=%p, cmd=%d, wasVisible %d\n", hwnd, cmd, wasVisible);
942 switch(cmd)
944 case SW_HIDE:
945 if (!wasVisible) return FALSE;
946 showFlag = FALSE;
947 swp |= SWP_HIDEWINDOW | SWP_NOSIZE | SWP_NOMOVE;
948 if (hwnd != GetActiveWindow())
949 swp |= SWP_NOACTIVATE | SWP_NOZORDER;
950 break;
952 case SW_SHOWMINNOACTIVE:
953 swp |= SWP_NOACTIVATE | SWP_NOZORDER;
954 /* fall through */
955 case SW_SHOWMINIMIZED:
956 case SW_FORCEMINIMIZE: /* FIXME: Does not work if thread is hung. */
957 swp |= SWP_SHOWWINDOW;
958 /* fall through */
959 case SW_MINIMIZE:
960 swp |= SWP_FRAMECHANGED;
961 if( !(style & WS_MINIMIZE) )
962 swp |= WINPOS_MinMaximize( hwnd, SW_MINIMIZE, &newPos );
963 else swp |= SWP_NOSIZE | SWP_NOMOVE;
964 break;
966 case SW_SHOWMAXIMIZED: /* same as SW_MAXIMIZE */
967 swp |= SWP_SHOWWINDOW | SWP_FRAMECHANGED;
968 swp |= WINPOS_MinMaximize( hwnd, SW_MAXIMIZE, &newPos );
969 break;
971 case SW_SHOWNA:
972 swp |= SWP_NOACTIVATE | SWP_SHOWWINDOW | SWP_NOSIZE | SWP_NOMOVE;
973 if (style & WS_CHILD) swp |= SWP_NOZORDER;
974 break;
975 case SW_SHOW:
976 if (wasVisible) return TRUE;
977 swp |= SWP_SHOWWINDOW | SWP_NOSIZE | SWP_NOMOVE;
978 break;
980 case SW_RESTORE:
981 swp |= SWP_FRAMECHANGED;
982 /* fall through */
983 case SW_SHOWNOACTIVATE:
984 swp |= SWP_NOACTIVATE | SWP_NOZORDER;
985 /* fall through */
986 case SW_SHOWNORMAL: /* same as SW_NORMAL: */
987 case SW_SHOWDEFAULT: /* FIXME: should have its own handler */
988 swp |= SWP_SHOWWINDOW;
990 if (style & (WS_MINIMIZE | WS_MAXIMIZE))
991 swp |= WINPOS_MinMaximize( hwnd, SW_RESTORE, &newPos );
992 else swp |= SWP_NOSIZE | SWP_NOMOVE;
993 break;
996 if ((showFlag != wasVisible || cmd == SW_SHOWNA) && cmd != SW_SHOWMAXIMIZED)
998 SendMessageW( hwnd, WM_SHOWWINDOW, showFlag, 0 );
999 if (!IsWindow( hwnd )) return wasVisible;
1002 parent = GetAncestor( hwnd, GA_PARENT );
1003 if (parent && !IsWindowVisible( parent ))
1005 /* if parent is not visible simply toggle WS_VISIBLE and return */
1006 if (showFlag) WIN_SetStyle( hwnd, WS_VISIBLE, 0 );
1007 else WIN_SetStyle( hwnd, 0, WS_VISIBLE );
1009 else
1011 /* ShowWindow won't activate a not being maximized child window */
1012 if ((style & WS_CHILD) && cmd != SW_MAXIMIZE)
1013 swp |= SWP_NOACTIVATE | SWP_NOZORDER;
1015 SetWindowPos( hwnd, HWND_TOP, newPos.left, newPos.top,
1016 newPos.right, newPos.bottom, LOWORD(swp) );
1019 if (cmd == SW_HIDE)
1021 HWND hFocus;
1023 /* FIXME: This will cause the window to be activated irrespective
1024 * of whether it is owned by the same thread. Has to be done
1025 * asynchronously.
1028 if (hwnd == GetActiveWindow())
1029 WINPOS_ActivateOtherWindow(hwnd);
1031 /* Revert focus to parent */
1032 hFocus = GetFocus();
1033 if (hwnd == hFocus || IsChild(hwnd, hFocus))
1035 HWND parent = GetAncestor(hwnd, GA_PARENT);
1036 if (parent == GetDesktopWindow()) parent = 0;
1037 SetFocus(parent);
1041 if (IsIconic(hwnd)) WINPOS_ShowIconTitle( hwnd, TRUE );
1043 if (!(wndPtr = WIN_GetPtr( hwnd )) || wndPtr == WND_OTHER_PROCESS) return wasVisible;
1045 if (wndPtr->flags & WIN_NEED_SIZE)
1047 /* should happen only in CreateWindowEx() */
1048 int wParam = SIZE_RESTORED;
1049 RECT client = wndPtr->rectClient;
1051 wndPtr->flags &= ~WIN_NEED_SIZE;
1052 if (wndPtr->dwStyle & WS_MAXIMIZE) wParam = SIZE_MAXIMIZED;
1053 else if (wndPtr->dwStyle & WS_MINIMIZE) wParam = SIZE_MINIMIZED;
1054 WIN_ReleasePtr( wndPtr );
1056 SendMessageW( hwnd, WM_SIZE, wParam,
1057 MAKELONG( client.right - client.left, client.bottom - client.top ));
1058 SendMessageW( hwnd, WM_MOVE, 0, MAKELONG( client.left, client.top ));
1060 else WIN_ReleasePtr( wndPtr );
1062 return wasVisible;
1066 /**********************************************************************
1067 * X11DRV_MapNotify
1069 void X11DRV_MapNotify( HWND hwnd, XEvent *event )
1071 struct x11drv_win_data *data;
1072 HWND hwndFocus = GetFocus();
1073 WND *win;
1075 if (!(data = X11DRV_get_win_data( hwnd ))) return;
1077 if (!(win = WIN_GetPtr( hwnd ))) return;
1079 if (data->managed && (win->dwStyle & WS_VISIBLE) && (win->dwStyle & WS_MINIMIZE))
1081 int x, y;
1082 unsigned int width, height, border, depth;
1083 Window root, top;
1084 RECT rect;
1085 LONG style = WS_VISIBLE;
1087 /* FIXME: hack */
1088 wine_tsx11_lock();
1089 XGetGeometry( event->xmap.display, data->whole_window, &root, &x, &y, &width, &height,
1090 &border, &depth );
1091 XTranslateCoordinates( event->xmap.display, data->whole_window, root, 0, 0, &x, &y, &top );
1092 wine_tsx11_unlock();
1093 rect.left = x;
1094 rect.top = y;
1095 rect.right = x + width;
1096 rect.bottom = y + height;
1097 X11DRV_X_to_window_rect( data, &rect );
1099 invalidate_dce( hwnd, &data->window_rect );
1101 if (win->flags & WIN_RESTORE_MAX) style |= WS_MAXIMIZE;
1102 WIN_SetStyle( hwnd, style, WS_MINIMIZE );
1103 WIN_ReleasePtr( win );
1105 SendMessageA( hwnd, WM_SHOWWINDOW, SW_RESTORE, 0 );
1106 data->lock_changes++;
1107 SetWindowPos( hwnd, 0, rect.left, rect.top, rect.right-rect.left, rect.bottom-rect.top,
1108 SWP_NOZORDER );
1109 data->lock_changes--;
1111 else WIN_ReleasePtr( win );
1112 if (hwndFocus && IsChild( hwnd, hwndFocus )) X11DRV_SetFocus(hwndFocus); /* FIXME */
1116 /**********************************************************************
1117 * X11DRV_UnmapNotify
1119 void X11DRV_UnmapNotify( HWND hwnd, XEvent *event )
1121 struct x11drv_win_data *data;
1122 WND *win;
1124 if (!(data = X11DRV_get_win_data( hwnd ))) return;
1126 if (!(win = WIN_GetPtr( hwnd ))) return;
1128 if ((win->dwStyle & WS_VISIBLE) && data->managed &&
1129 X11DRV_is_window_rect_mapped( &win->rectWindow ))
1131 if (win->dwStyle & WS_MAXIMIZE)
1132 win->flags |= WIN_RESTORE_MAX;
1133 else
1134 win->flags &= ~WIN_RESTORE_MAX;
1136 WIN_SetStyle( hwnd, WS_MINIMIZE, WS_MAXIMIZE );
1137 WIN_ReleasePtr( win );
1139 EndMenu();
1140 SendMessageA( hwnd, WM_SHOWWINDOW, SW_MINIMIZE, 0 );
1141 data->lock_changes++;
1142 SetWindowPos( hwnd, 0, 0, 0, GetSystemMetrics(SM_CXICON), GetSystemMetrics(SM_CYICON),
1143 SWP_NOMOVE | SWP_NOACTIVATE | SWP_NOZORDER );
1144 data->lock_changes--;
1146 else WIN_ReleasePtr( win );
1150 /***********************************************************************
1151 * X11DRV_handle_desktop_resize
1153 void X11DRV_handle_desktop_resize( unsigned int width, unsigned int height )
1155 RECT rect;
1156 HWND hwnd = GetDesktopWindow();
1157 struct x11drv_win_data *data;
1159 if (!(data = X11DRV_get_win_data( hwnd ))) return;
1160 screen_width = width;
1161 screen_height = height;
1162 TRACE("desktop %p change to (%dx%d)\n", hwnd, width, height);
1163 SetRect( &rect, 0, 0, width, height );
1164 data->lock_changes++;
1165 X11DRV_set_window_pos( hwnd, 0, &rect, &rect, SWP_NOZORDER|SWP_NOMOVE, NULL );
1166 data->lock_changes--;
1167 SendMessageTimeoutW( HWND_BROADCAST, WM_DISPLAYCHANGE, screen_depth,
1168 MAKELPARAM( width, height ), SMTO_ABORTIFHUNG, 2000, NULL );
1172 /***********************************************************************
1173 * X11DRV_ConfigureNotify
1175 void X11DRV_ConfigureNotify( HWND hwnd, XEvent *xev )
1177 XConfigureEvent *event = &xev->xconfigure;
1178 struct x11drv_win_data *data;
1179 RECT rect;
1180 UINT flags;
1181 int cx, cy, x = event->x, y = event->y;
1183 if (!hwnd) return;
1184 if (!(data = X11DRV_get_win_data( hwnd ))) return;
1186 /* Get geometry */
1188 if (!event->send_event) /* normal event, need to map coordinates to the root */
1190 Window child;
1191 wine_tsx11_lock();
1192 XTranslateCoordinates( event->display, data->whole_window, root_window,
1193 0, 0, &x, &y, &child );
1194 wine_tsx11_unlock();
1196 rect.left = x;
1197 rect.top = y;
1198 rect.right = x + event->width;
1199 rect.bottom = y + event->height;
1200 TRACE( "win %p new X rect %ld,%ld,%ldx%ld (event %d,%d,%dx%d)\n",
1201 hwnd, rect.left, rect.top, rect.right-rect.left, rect.bottom-rect.top,
1202 event->x, event->y, event->width, event->height );
1203 X11DRV_X_to_window_rect( data, &rect );
1205 x = rect.left;
1206 y = rect.top;
1207 cx = rect.right - rect.left;
1208 cy = rect.bottom - rect.top;
1209 flags = SWP_NOACTIVATE | SWP_NOZORDER;
1211 /* Compare what has changed */
1213 GetWindowRect( hwnd, &rect );
1214 if (rect.left == x && rect.top == y) flags |= SWP_NOMOVE;
1215 else
1216 TRACE( "%p moving from (%ld,%ld) to (%d,%d)\n",
1217 hwnd, rect.left, rect.top, x, y );
1219 if ((rect.right - rect.left == cx && rect.bottom - rect.top == cy) ||
1220 IsIconic(hwnd) ||
1221 (IsRectEmpty( &rect ) && event->width == 1 && event->height == 1))
1223 if (flags & SWP_NOMOVE) return; /* if nothing changed, don't do anything */
1224 flags |= SWP_NOSIZE;
1226 else
1227 TRACE( "%p resizing from (%ldx%ld) to (%dx%d)\n",
1228 hwnd, rect.right - rect.left, rect.bottom - rect.top, cx, cy );
1230 data->lock_changes++;
1231 SetWindowPos( hwnd, 0, x, y, cx, cy, flags );
1232 data->lock_changes--;
1236 /***********************************************************************
1237 * SetWindowRgn (X11DRV.@)
1239 * Assign specified region to window (for non-rectangular windows)
1241 int X11DRV_SetWindowRgn( HWND hwnd, HRGN hrgn, BOOL redraw )
1243 struct x11drv_win_data *data;
1245 if (!(data = X11DRV_get_win_data( hwnd )))
1247 if (IsWindow( hwnd ))
1248 FIXME( "not supported on other thread window %p\n", hwnd );
1249 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
1250 return FALSE;
1253 #ifdef HAVE_LIBXSHAPE
1254 if (data->whole_window)
1256 Display *display = thread_display();
1258 if (!hrgn)
1260 wine_tsx11_lock();
1261 XShapeCombineMask( display, data->whole_window,
1262 ShapeBounding, 0, 0, None, ShapeSet );
1263 wine_tsx11_unlock();
1265 else
1267 RGNDATA *pRegionData = X11DRV_GetRegionData( hrgn, 0 );
1268 if (pRegionData)
1270 wine_tsx11_lock();
1271 XShapeCombineRectangles( display, data->whole_window, ShapeBounding,
1272 data->window_rect.left - data->whole_rect.left,
1273 data->window_rect.top - data->whole_rect.top,
1274 (XRectangle *)pRegionData->Buffer,
1275 pRegionData->rdh.nCount,
1276 ShapeSet, YXBanded );
1277 wine_tsx11_unlock();
1278 HeapFree(GetProcessHeap(), 0, pRegionData);
1282 #endif /* HAVE_LIBXSHAPE */
1284 invalidate_dce( hwnd, &data->window_rect );
1285 return TRUE;
1289 /***********************************************************************
1290 * draw_moving_frame
1292 * Draw the frame used when moving or resizing window.
1294 * FIXME: This causes problems in Win95 mode. (why?)
1296 static void draw_moving_frame( HDC hdc, RECT *rect, BOOL thickframe )
1298 if (thickframe)
1300 const int width = GetSystemMetrics(SM_CXFRAME);
1301 const int height = GetSystemMetrics(SM_CYFRAME);
1303 HBRUSH hbrush = SelectObject( hdc, GetStockObject( GRAY_BRUSH ) );
1304 PatBlt( hdc, rect->left, rect->top,
1305 rect->right - rect->left - width, height, PATINVERT );
1306 PatBlt( hdc, rect->left, rect->top + height, width,
1307 rect->bottom - rect->top - height, PATINVERT );
1308 PatBlt( hdc, rect->left + width, rect->bottom - 1,
1309 rect->right - rect->left - width, -height, PATINVERT );
1310 PatBlt( hdc, rect->right - 1, rect->top, -width,
1311 rect->bottom - rect->top - height, PATINVERT );
1312 SelectObject( hdc, hbrush );
1314 else DrawFocusRect( hdc, rect );
1318 /***********************************************************************
1319 * start_size_move
1321 * Initialisation of a move or resize, when initiatied from a menu choice.
1322 * Return hit test code for caption or sizing border.
1324 static LONG start_size_move( HWND hwnd, WPARAM wParam, POINT *capturePoint, LONG style )
1326 LONG hittest = 0;
1327 POINT pt;
1328 MSG msg;
1329 RECT rectWindow;
1331 GetWindowRect( hwnd, &rectWindow );
1333 if ((wParam & 0xfff0) == SC_MOVE)
1335 /* Move pointer at the center of the caption */
1336 RECT rect = rectWindow;
1337 /* Note: to be exactly centered we should take the different types
1338 * of border into account, but it shouldn't make more that a few pixels
1339 * of difference so let's not bother with that */
1340 rect.top += GetSystemMetrics(SM_CYBORDER);
1341 if (style & WS_SYSMENU)
1342 rect.left += GetSystemMetrics(SM_CXSIZE) + 1;
1343 if (style & WS_MINIMIZEBOX)
1344 rect.right -= GetSystemMetrics(SM_CXSIZE) + 1;
1345 if (style & WS_MAXIMIZEBOX)
1346 rect.right -= GetSystemMetrics(SM_CXSIZE) + 1;
1347 pt.x = (rect.right + rect.left) / 2;
1348 pt.y = rect.top + GetSystemMetrics(SM_CYSIZE)/2;
1349 hittest = HTCAPTION;
1350 *capturePoint = pt;
1352 else /* SC_SIZE */
1354 pt.x = pt.y = 0;
1355 while(!hittest)
1357 GetMessageW( &msg, 0, WM_KEYFIRST, WM_MOUSELAST );
1358 if (CallMsgFilterW( &msg, MSGF_SIZE )) continue;
1360 switch(msg.message)
1362 case WM_MOUSEMOVE:
1363 pt = msg.pt;
1364 hittest = SendMessageW( hwnd, WM_NCHITTEST, 0, MAKELONG( pt.x, pt.y ) );
1365 if ((hittest < HTLEFT) || (hittest > HTBOTTOMRIGHT)) hittest = 0;
1366 break;
1368 case WM_LBUTTONUP:
1369 return 0;
1371 case WM_KEYDOWN:
1372 switch(msg.wParam)
1374 case VK_UP:
1375 hittest = HTTOP;
1376 pt.x =(rectWindow.left+rectWindow.right)/2;
1377 pt.y = rectWindow.top + GetSystemMetrics(SM_CYFRAME) / 2;
1378 break;
1379 case VK_DOWN:
1380 hittest = HTBOTTOM;
1381 pt.x =(rectWindow.left+rectWindow.right)/2;
1382 pt.y = rectWindow.bottom - GetSystemMetrics(SM_CYFRAME) / 2;
1383 break;
1384 case VK_LEFT:
1385 hittest = HTLEFT;
1386 pt.x = rectWindow.left + GetSystemMetrics(SM_CXFRAME) / 2;
1387 pt.y =(rectWindow.top+rectWindow.bottom)/2;
1388 break;
1389 case VK_RIGHT:
1390 hittest = HTRIGHT;
1391 pt.x = rectWindow.right - GetSystemMetrics(SM_CXFRAME) / 2;
1392 pt.y =(rectWindow.top+rectWindow.bottom)/2;
1393 break;
1394 case VK_RETURN:
1395 case VK_ESCAPE: return 0;
1399 *capturePoint = pt;
1401 SetCursorPos( pt.x, pt.y );
1402 SendMessageW( hwnd, WM_SETCURSOR, (WPARAM)hwnd, MAKELONG( hittest, WM_MOUSEMOVE ));
1403 return hittest;
1407 /***********************************************************************
1408 * set_movesize_capture
1410 static void set_movesize_capture( HWND hwnd )
1412 HWND previous = 0;
1414 SERVER_START_REQ( set_capture_window )
1416 req->handle = hwnd;
1417 req->flags = CAPTURE_MOVESIZE;
1418 if (!wine_server_call_err( req ))
1420 previous = reply->previous;
1421 hwnd = reply->full_handle;
1424 SERVER_END_REQ;
1425 if (previous && previous != hwnd)
1426 SendMessageW( previous, WM_CAPTURECHANGED, 0, (LPARAM)hwnd );
1429 /***********************************************************************
1430 * X11DRV_WMMoveResizeWindow
1432 * Tells the window manager to initiate a move or resize operation.
1434 * SEE
1435 * http://freedesktop.org/Standards/wm-spec/1.3/ar01s04.html
1436 * or search for "_NET_WM_MOVERESIZE"
1438 static void X11DRV_WMMoveResizeWindow( HWND hwnd, int x, int y, int dir )
1440 XEvent xev;
1441 Display *display = thread_display();
1443 xev.xclient.type = ClientMessage;
1444 xev.xclient.window = X11DRV_get_whole_window(hwnd);
1445 xev.xclient.message_type = x11drv_atom(_NET_WM_MOVERESIZE);
1446 xev.xclient.serial = 0;
1447 xev.xclient.display = display;
1448 xev.xclient.send_event = True;
1449 xev.xclient.format = 32;
1450 xev.xclient.data.l[0] = x; /* x coord */
1451 xev.xclient.data.l[1] = y; /* y coord */
1452 xev.xclient.data.l[2] = dir; /* direction */
1453 xev.xclient.data.l[3] = 1; /* button */
1454 xev.xclient.data.l[4] = 0; /* unused */
1456 /* need to ungrab the pointer that may have been automatically grabbed
1457 * with a ButtonPress event */
1458 wine_tsx11_lock();
1459 XUngrabPointer( display, CurrentTime );
1460 XSendEvent(display, root_window, False, SubstructureNotifyMask, &xev);
1461 wine_tsx11_unlock();
1464 /***********************************************************************
1465 * SysCommandSizeMove (X11DRV.@)
1467 * Perform SC_MOVE and SC_SIZE commands.
1469 void X11DRV_SysCommandSizeMove( HWND hwnd, WPARAM wParam )
1471 MSG msg;
1472 RECT sizingRect, mouseRect, origRect;
1473 HDC hdc;
1474 HWND parent;
1475 LONG hittest = (LONG)(wParam & 0x0f);
1476 WPARAM syscommand = wParam & 0xfff0;
1477 HCURSOR hDragCursor = 0, hOldCursor = 0;
1478 POINT minTrack, maxTrack;
1479 POINT capturePoint, pt;
1480 LONG style = GetWindowLongA( hwnd, GWL_STYLE );
1481 BOOL thickframe = HAS_THICKFRAME( style );
1482 BOOL iconic = style & WS_MINIMIZE;
1483 BOOL moved = FALSE;
1484 DWORD dwPoint = GetMessagePos ();
1485 BOOL DragFullWindows = FALSE;
1486 BOOL grab;
1487 Window parent_win, whole_win;
1488 Display *old_gdi_display = NULL;
1489 struct x11drv_thread_data *thread_data = x11drv_thread_data();
1490 struct x11drv_win_data *data;
1492 pt.x = (short)LOWORD(dwPoint);
1493 pt.y = (short)HIWORD(dwPoint);
1494 capturePoint = pt;
1496 if (IsZoomed(hwnd) || !IsWindowVisible(hwnd)) return;
1498 if (!(data = X11DRV_get_win_data( hwnd ))) return;
1500 /* if we are managed then we let the WM do all the work */
1501 if (data->managed)
1503 int dir;
1504 if (syscommand == SC_MOVE)
1506 if (!hittest) dir = _NET_WM_MOVERESIZE_MOVE_KEYBOARD;
1507 else dir = _NET_WM_MOVERESIZE_MOVE;
1509 else if (!hittest) dir = _NET_WM_MOVERESIZE_SIZE_KEYBOARD;
1510 else
1511 switch (hittest)
1513 case WMSZ_LEFT: dir = _NET_WM_MOVERESIZE_SIZE_LEFT; break;
1514 case WMSZ_RIGHT: dir = _NET_WM_MOVERESIZE_SIZE_RIGHT; break;
1515 case WMSZ_TOP: dir = _NET_WM_MOVERESIZE_SIZE_TOP; break;
1516 case WMSZ_TOPLEFT: dir = _NET_WM_MOVERESIZE_SIZE_TOPLEFT; break;
1517 case WMSZ_TOPRIGHT: dir = _NET_WM_MOVERESIZE_SIZE_TOPRIGHT; break;
1518 case WMSZ_BOTTOM: dir = _NET_WM_MOVERESIZE_SIZE_BOTTOM; break;
1519 case WMSZ_BOTTOMLEFT: dir = _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT; break;
1520 case WMSZ_BOTTOMRIGHT: dir = _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT; break;
1521 default:
1522 ERR("Invalid hittest value: %ld\n", hittest);
1523 dir = _NET_WM_MOVERESIZE_SIZE_KEYBOARD;
1525 X11DRV_WMMoveResizeWindow( hwnd, pt.x, pt.y, dir );
1526 return;
1529 SystemParametersInfoA(SPI_GETDRAGFULLWINDOWS, 0, &DragFullWindows, 0);
1531 if (syscommand == SC_MOVE)
1533 if (!hittest) hittest = start_size_move( hwnd, wParam, &capturePoint, style );
1534 if (!hittest) return;
1536 else /* SC_SIZE */
1538 if ( hittest && (syscommand != SC_MOUSEMENU) )
1539 hittest += (HTLEFT - WMSZ_LEFT);
1540 else
1542 set_movesize_capture( hwnd );
1543 hittest = start_size_move( hwnd, wParam, &capturePoint, style );
1544 if (!hittest)
1546 set_movesize_capture(0);
1547 return;
1552 /* Get min/max info */
1554 WINPOS_GetMinMaxInfo( hwnd, NULL, NULL, &minTrack, &maxTrack );
1555 GetWindowRect( hwnd, &sizingRect );
1556 if (style & WS_CHILD)
1558 parent = GetParent(hwnd);
1559 /* make sizing rect relative to parent */
1560 MapWindowPoints( 0, parent, (POINT*)&sizingRect, 2 );
1561 GetClientRect( parent, &mouseRect );
1563 else
1565 parent = 0;
1566 SetRect(&mouseRect, 0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN));
1568 origRect = sizingRect;
1570 if (ON_LEFT_BORDER(hittest))
1572 mouseRect.left = max( mouseRect.left, sizingRect.right-maxTrack.x );
1573 mouseRect.right = min( mouseRect.right, sizingRect.right-minTrack.x );
1575 else if (ON_RIGHT_BORDER(hittest))
1577 mouseRect.left = max( mouseRect.left, sizingRect.left+minTrack.x );
1578 mouseRect.right = min( mouseRect.right, sizingRect.left+maxTrack.x );
1580 if (ON_TOP_BORDER(hittest))
1582 mouseRect.top = max( mouseRect.top, sizingRect.bottom-maxTrack.y );
1583 mouseRect.bottom = min( mouseRect.bottom,sizingRect.bottom-minTrack.y);
1585 else if (ON_BOTTOM_BORDER(hittest))
1587 mouseRect.top = max( mouseRect.top, sizingRect.top+minTrack.y );
1588 mouseRect.bottom = min( mouseRect.bottom, sizingRect.top+maxTrack.y );
1590 if (parent) MapWindowPoints( parent, 0, (LPPOINT)&mouseRect, 2 );
1592 /* Retrieve a default cache DC (without using the window style) */
1593 hdc = GetDCEx( parent, 0, DCX_CACHE );
1595 if( iconic ) /* create a cursor for dragging */
1597 hDragCursor = (HCURSOR)GetClassLongPtrW( hwnd, GCLP_HICON);
1598 if( !hDragCursor ) hDragCursor = (HCURSOR)SendMessageW( hwnd, WM_QUERYDRAGICON, 0, 0L);
1599 if( !hDragCursor ) iconic = FALSE;
1602 /* repaint the window before moving it around */
1603 RedrawWindow( hwnd, NULL, 0, RDW_UPDATENOW | RDW_ALLCHILDREN );
1605 SendMessageA( hwnd, WM_ENTERSIZEMOVE, 0, 0 );
1606 set_movesize_capture( hwnd );
1608 /* grab the server only when moving top-level windows without desktop */
1609 grab = (!DragFullWindows && !parent && (root_window == DefaultRootWindow(gdi_display)));
1611 if (grab)
1613 wine_tsx11_lock();
1614 XSync( gdi_display, False );
1615 XGrabServer( thread_data->display );
1616 XSync( thread_data->display, False );
1617 /* switch gdi display to the thread display, since the server is grabbed */
1618 old_gdi_display = gdi_display;
1619 gdi_display = thread_data->display;
1620 wine_tsx11_unlock();
1622 whole_win = X11DRV_get_whole_window( GetAncestor(hwnd,GA_ROOT) );
1623 parent_win = parent ? X11DRV_get_whole_window( GetAncestor(parent,GA_ROOT) ) : root_window;
1625 wine_tsx11_lock();
1626 XGrabPointer( thread_data->display, whole_win, False,
1627 PointerMotionMask | ButtonPressMask | ButtonReleaseMask,
1628 GrabModeAsync, GrabModeAsync, parent_win, None, CurrentTime );
1629 wine_tsx11_unlock();
1630 thread_data->grab_window = whole_win;
1632 while(1)
1634 int dx = 0, dy = 0;
1636 if (!GetMessageW( &msg, 0, WM_KEYFIRST, WM_MOUSELAST )) break;
1637 if (CallMsgFilterW( &msg, MSGF_SIZE )) continue;
1639 /* Exit on button-up, Return, or Esc */
1640 if ((msg.message == WM_LBUTTONUP) ||
1641 ((msg.message == WM_KEYDOWN) &&
1642 ((msg.wParam == VK_RETURN) || (msg.wParam == VK_ESCAPE)))) break;
1644 if ((msg.message != WM_KEYDOWN) && (msg.message != WM_MOUSEMOVE))
1645 continue; /* We are not interested in other messages */
1647 pt = msg.pt;
1649 if (msg.message == WM_KEYDOWN) switch(msg.wParam)
1651 case VK_UP: pt.y -= 8; break;
1652 case VK_DOWN: pt.y += 8; break;
1653 case VK_LEFT: pt.x -= 8; break;
1654 case VK_RIGHT: pt.x += 8; break;
1657 pt.x = max( pt.x, mouseRect.left );
1658 pt.x = min( pt.x, mouseRect.right );
1659 pt.y = max( pt.y, mouseRect.top );
1660 pt.y = min( pt.y, mouseRect.bottom );
1662 dx = pt.x - capturePoint.x;
1663 dy = pt.y - capturePoint.y;
1665 if (dx || dy)
1667 if( !moved )
1669 moved = TRUE;
1671 if( iconic ) /* ok, no system popup tracking */
1673 hOldCursor = SetCursor(hDragCursor);
1674 ShowCursor( TRUE );
1675 WINPOS_ShowIconTitle( hwnd, FALSE );
1677 else if(!DragFullWindows)
1678 draw_moving_frame( hdc, &sizingRect, thickframe );
1681 if (msg.message == WM_KEYDOWN) SetCursorPos( pt.x, pt.y );
1682 else
1684 RECT newRect = sizingRect;
1685 WPARAM wpSizingHit = 0;
1687 if (hittest == HTCAPTION) OffsetRect( &newRect, dx, dy );
1688 if (ON_LEFT_BORDER(hittest)) newRect.left += dx;
1689 else if (ON_RIGHT_BORDER(hittest)) newRect.right += dx;
1690 if (ON_TOP_BORDER(hittest)) newRect.top += dy;
1691 else if (ON_BOTTOM_BORDER(hittest)) newRect.bottom += dy;
1692 if(!iconic && !DragFullWindows) draw_moving_frame( hdc, &sizingRect, thickframe );
1693 capturePoint = pt;
1695 /* determine the hit location */
1696 if (hittest >= HTLEFT && hittest <= HTBOTTOMRIGHT)
1697 wpSizingHit = WMSZ_LEFT + (hittest - HTLEFT);
1698 SendMessageA( hwnd, WM_SIZING, wpSizingHit, (LPARAM)&newRect );
1700 if (!iconic)
1702 if(!DragFullWindows)
1703 draw_moving_frame( hdc, &newRect, thickframe );
1704 else
1705 SetWindowPos( hwnd, 0, newRect.left, newRect.top,
1706 newRect.right - newRect.left,
1707 newRect.bottom - newRect.top,
1708 ( hittest == HTCAPTION ) ? SWP_NOSIZE : 0 );
1710 sizingRect = newRect;
1715 set_movesize_capture(0);
1716 if( iconic )
1718 if( moved ) /* restore cursors, show icon title later on */
1720 ShowCursor( FALSE );
1721 SetCursor( hOldCursor );
1724 else if (moved && !DragFullWindows)
1725 draw_moving_frame( hdc, &sizingRect, thickframe );
1727 ReleaseDC( parent, hdc );
1729 wine_tsx11_lock();
1730 XUngrabPointer( thread_data->display, CurrentTime );
1731 if (grab)
1733 XSync( thread_data->display, False );
1734 XUngrabServer( thread_data->display );
1735 XSync( thread_data->display, False );
1736 gdi_display = old_gdi_display;
1738 wine_tsx11_unlock();
1739 thread_data->grab_window = None;
1741 if (HOOK_CallHooks( WH_CBT, HCBT_MOVESIZE, (WPARAM)hwnd, (LPARAM)&sizingRect, TRUE ))
1742 moved = FALSE;
1744 SendMessageA( hwnd, WM_EXITSIZEMOVE, 0, 0 );
1745 SendMessageA( hwnd, WM_SETVISIBLE, !IsIconic(hwnd), 0L);
1747 /* window moved or resized */
1748 if (moved)
1750 /* if the moving/resizing isn't canceled call SetWindowPos
1751 * with the new position or the new size of the window
1753 if (!((msg.message == WM_KEYDOWN) && (msg.wParam == VK_ESCAPE)) )
1755 /* NOTE: SWP_NOACTIVATE prevents document window activation in Word 6 */
1756 if(!DragFullWindows)
1757 SetWindowPos( hwnd, 0, sizingRect.left, sizingRect.top,
1758 sizingRect.right - sizingRect.left,
1759 sizingRect.bottom - sizingRect.top,
1760 ( hittest == HTCAPTION ) ? SWP_NOSIZE : 0 );
1762 else
1763 { /* restore previous size/position */
1764 if(DragFullWindows)
1765 SetWindowPos( hwnd, 0, origRect.left, origRect.top,
1766 origRect.right - origRect.left,
1767 origRect.bottom - origRect.top,
1768 ( hittest == HTCAPTION ) ? SWP_NOSIZE : 0 );
1772 if (IsIconic(hwnd))
1774 /* Single click brings up the system menu when iconized */
1776 if( !moved )
1778 if(style & WS_SYSMENU )
1779 SendMessageA( hwnd, WM_SYSCOMMAND,
1780 SC_MOUSEMENU + HTSYSMENU, MAKELONG(pt.x,pt.y));
1782 else WINPOS_ShowIconTitle( hwnd, TRUE );