push 421a6d0d1068c98851feb4eb01bb0e754f615ce9
[wine/hacks.git] / dlls / winex11.drv / winpos.c
blob5499ef0ceadeb7b09545a69fb16bc771ffe6b1d0
1 /*
2 * Window position related functions.
4 * Copyright 1993, 1994, 1995, 2001 Alexandre Julliard
5 * Copyright 1995, 1996, 1999 Alex Korobka
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "config.h"
24 #include <X11/Xlib.h>
25 #include <X11/Xutil.h>
26 #ifdef HAVE_LIBXSHAPE
27 #include <X11/extensions/shape.h>
28 #endif /* HAVE_LIBXSHAPE */
29 #include <stdarg.h>
31 #include "windef.h"
32 #include "winbase.h"
33 #include "wingdi.h"
34 #include "winuser.h"
35 #include "winerror.h"
36 #include "wine/wingdi16.h"
38 #include "x11drv.h"
39 #include "win.h"
41 #include "wine/server.h"
42 #include "wine/debug.h"
44 WINE_DEFAULT_DEBUG_CHANNEL(x11drv);
46 #define SWP_AGG_NOPOSCHANGE \
47 (SWP_NOSIZE | SWP_NOMOVE | SWP_NOCLIENTSIZE | SWP_NOCLIENTMOVE | SWP_NOZORDER)
49 #define SWP_EX_NOCOPY 0x0001
50 #define SWP_EX_PAINTSELF 0x0002
51 #define SWP_EX_NONCLIENT 0x0004
53 #define HAS_THICKFRAME(style) \
54 (((style) & WS_THICKFRAME) && \
55 !(((style) & (WS_DLGFRAME|WS_BORDER)) == WS_DLGFRAME))
57 #define ON_LEFT_BORDER(hit) \
58 (((hit) == HTLEFT) || ((hit) == HTTOPLEFT) || ((hit) == HTBOTTOMLEFT))
59 #define ON_RIGHT_BORDER(hit) \
60 (((hit) == HTRIGHT) || ((hit) == HTTOPRIGHT) || ((hit) == HTBOTTOMRIGHT))
61 #define ON_TOP_BORDER(hit) \
62 (((hit) == HTTOP) || ((hit) == HTTOPLEFT) || ((hit) == HTTOPRIGHT))
63 #define ON_BOTTOM_BORDER(hit) \
64 (((hit) == HTBOTTOM) || ((hit) == HTBOTTOMLEFT) || ((hit) == HTBOTTOMRIGHT))
66 #define _NET_WM_MOVERESIZE_SIZE_TOPLEFT 0
67 #define _NET_WM_MOVERESIZE_SIZE_TOP 1
68 #define _NET_WM_MOVERESIZE_SIZE_TOPRIGHT 2
69 #define _NET_WM_MOVERESIZE_SIZE_RIGHT 3
70 #define _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT 4
71 #define _NET_WM_MOVERESIZE_SIZE_BOTTOM 5
72 #define _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT 6
73 #define _NET_WM_MOVERESIZE_SIZE_LEFT 7
74 #define _NET_WM_MOVERESIZE_MOVE 8 /* movement only */
75 #define _NET_WM_MOVERESIZE_SIZE_KEYBOARD 9 /* size via keyboard */
76 #define _NET_WM_MOVERESIZE_MOVE_KEYBOARD 10 /* move via keyboard */
78 #define _NET_WM_STATE_REMOVE 0
79 #define _NET_WM_STATE_ADD 1
80 #define _NET_WM_STATE_TOGGLE 2
82 static const char managed_prop[] = "__wine_x11_managed";
84 /***********************************************************************
85 * X11DRV_Expose
87 void X11DRV_Expose( HWND hwnd, XEvent *xev )
89 XExposeEvent *event = &xev->xexpose;
90 RECT rect;
91 struct x11drv_win_data *data;
92 int flags = RDW_INVALIDATE | RDW_ERASE | RDW_ALLCHILDREN;
94 TRACE( "win %p (%lx) %d,%d %dx%d\n",
95 hwnd, event->window, event->x, event->y, event->width, event->height );
97 if (!(data = X11DRV_get_win_data( hwnd ))) return;
99 rect.left = event->x;
100 rect.top = event->y;
101 rect.right = rect.left + event->width;
102 rect.bottom = rect.top + event->height;
104 if (event->window == root_window)
105 OffsetRect( &rect, virtual_screen_rect.left, virtual_screen_rect.top );
107 if (rect.left < data->client_rect.left ||
108 rect.top < data->client_rect.top ||
109 rect.right > data->client_rect.right ||
110 rect.bottom > data->client_rect.bottom) flags |= RDW_FRAME;
112 SERVER_START_REQ( update_window_zorder )
114 req->window = hwnd;
115 req->rect.left = rect.left + data->whole_rect.left;
116 req->rect.top = rect.top + data->whole_rect.top;
117 req->rect.right = rect.right + data->whole_rect.left;
118 req->rect.bottom = rect.bottom + data->whole_rect.top;
119 wine_server_call( req );
121 SERVER_END_REQ;
123 /* make position relative to client area instead of window */
124 OffsetRect( &rect, -data->client_rect.left, -data->client_rect.top );
125 RedrawWindow( hwnd, &rect, 0, flags );
128 /***********************************************************************
129 * SetWindowStyle (X11DRV.@)
131 * Update the X state of a window to reflect a style change
133 void X11DRV_SetWindowStyle( HWND hwnd, DWORD old_style )
135 Display *display = thread_display();
136 struct x11drv_win_data *data;
137 DWORD new_style, changed;
139 if (hwnd == GetDesktopWindow()) return;
140 if (!(data = X11DRV_get_win_data( hwnd ))) return;
142 new_style = GetWindowLongW( hwnd, GWL_STYLE );
143 changed = new_style ^ old_style;
145 if (changed & WS_VISIBLE)
147 if (data->whole_window && X11DRV_is_window_rect_mapped( &data->window_rect ))
149 if (new_style & WS_VISIBLE)
151 TRACE( "mapping win %p\n", hwnd );
152 X11DRV_sync_window_style( display, data );
153 X11DRV_set_wm_hints( display, data );
154 wine_tsx11_lock();
155 XMapWindow( display, data->whole_window );
156 wine_tsx11_unlock();
158 /* we don't unmap windows, that causes trouble with the window manager */
160 invalidate_dce( hwnd, &data->window_rect );
163 if (changed & WS_DISABLED)
165 if (data->whole_window && data->wm_hints)
167 wine_tsx11_lock();
168 data->wm_hints->input = !(new_style & WS_DISABLED);
169 XSetWMHints( display, data->whole_window, data->wm_hints );
170 wine_tsx11_unlock();
176 /***********************************************************************
177 * update_fullscreen_state
179 * Use the NETWM protocol to set the fullscreen state.
180 * This only works for mapped windows.
182 static void update_fullscreen_state( Display *display, Window win, BOOL new_fs_state )
184 XEvent xev;
186 TRACE("setting fullscreen state for window %lx to %s\n", win, new_fs_state ? "true" : "false");
188 xev.xclient.type = ClientMessage;
189 xev.xclient.window = win;
190 xev.xclient.message_type = x11drv_atom(_NET_WM_STATE);
191 xev.xclient.serial = 0;
192 xev.xclient.display = display;
193 xev.xclient.send_event = True;
194 xev.xclient.format = 32;
195 xev.xclient.data.l[0] = new_fs_state ? _NET_WM_STATE_ADD : _NET_WM_STATE_REMOVE;
196 xev.xclient.data.l[1] = x11drv_atom(_NET_WM_STATE_FULLSCREEN);
197 xev.xclient.data.l[2] = 0;
198 wine_tsx11_lock();
199 XSendEvent(display, root_window, False, SubstructureRedirectMask | SubstructureNotifyMask, &xev);
200 wine_tsx11_unlock();
203 /***********************************************************************
204 * fullscreen_state_changed
206 * Check if the fullscreen state of a given window has changed
208 static BOOL fullscreen_state_changed( const struct x11drv_win_data *data,
209 const RECT *old_client_rect, const RECT *old_screen_rect,
210 BOOL *new_fs_state )
212 BOOL old_fs_state = FALSE;
214 *new_fs_state = FALSE;
216 if (old_client_rect->left <= 0 && old_client_rect->right >= old_screen_rect->right &&
217 old_client_rect->top <= 0 && old_client_rect->bottom >= old_screen_rect->bottom)
218 old_fs_state = TRUE;
220 if (data->client_rect.left <= 0 && data->client_rect.right >= screen_width &&
221 data->client_rect.top <= 0 && data->client_rect.bottom >= screen_height)
222 *new_fs_state = TRUE;
224 #if 0 /* useful to debug fullscreen state problems */
225 TRACE("hwnd %p, old rect %s, new rect %s, old screen %s, new screen (0,0-%d,%d)\n",
226 data->hwnd,
227 wine_dbgstr_rect(old_client_rect), wine_dbgstr_rect(&data->client_rect),
228 wine_dbgstr_rect(old_screen_rect), screen_width, screen_height);
229 TRACE("old fs state %d\n, new fs state = %d\n", old_fs_state, *new_fs_state);
230 #endif
231 return *new_fs_state != old_fs_state;
235 /***********************************************************************
236 * SetWindowPos (X11DRV.@)
238 BOOL X11DRV_SetWindowPos( HWND hwnd, HWND insert_after, const RECT *rectWindow,
239 const RECT *rectClient, UINT swp_flags, const RECT *valid_rects )
241 Display *display = thread_display();
242 struct x11drv_win_data *data;
243 RECT new_whole_rect, old_client_rect, old_screen_rect;
244 WND *win;
245 DWORD old_style, new_style;
246 BOOL ret, make_managed = FALSE;
248 if (!(data = X11DRV_get_win_data( hwnd ))) return FALSE;
250 /* check if we need to switch the window to managed */
251 if (!data->managed && data->whole_window && managed_mode &&
252 root_window == DefaultRootWindow( display ) &&
253 data->whole_window != root_window)
255 if (!(swp_flags & (SWP_NOACTIVATE|SWP_HIDEWINDOW)) ||
256 is_window_managed( hwnd, rectWindow ) ||
257 hwnd == GetActiveWindow())
259 TRACE( "making win %p/%lx managed\n", hwnd, data->whole_window );
260 make_managed = TRUE;
261 data->managed = TRUE;
262 SetPropA( hwnd, managed_prop, (HANDLE)1 );
266 new_whole_rect = *rectWindow;
267 X11DRV_window_to_X_rect( data, &new_whole_rect );
269 old_client_rect = data->client_rect;
271 if (!(win = WIN_GetPtr( hwnd ))) return FALSE;
272 if (win == WND_OTHER_PROCESS)
274 if (IsWindow( hwnd )) ERR( "cannot set rectangles of other process window %p\n", hwnd );
275 return FALSE;
277 SERVER_START_REQ( set_window_pos )
279 req->handle = hwnd;
280 req->previous = insert_after;
281 req->flags = swp_flags;
282 req->window.left = rectWindow->left;
283 req->window.top = rectWindow->top;
284 req->window.right = rectWindow->right;
285 req->window.bottom = rectWindow->bottom;
286 req->client.left = rectClient->left;
287 req->client.top = rectClient->top;
288 req->client.right = rectClient->right;
289 req->client.bottom = rectClient->bottom;
290 if (memcmp( rectWindow, &new_whole_rect, sizeof(RECT) ) || !IsRectEmpty( &valid_rects[0] ))
292 wine_server_add_data( req, &new_whole_rect, sizeof(new_whole_rect) );
293 if (!IsRectEmpty( &valid_rects[0] ))
294 wine_server_add_data( req, valid_rects, 2 * sizeof(*valid_rects) );
296 ret = !wine_server_call( req );
297 new_style = reply->new_style;
299 SERVER_END_REQ;
301 if (win == WND_DESKTOP || data->whole_window == DefaultRootWindow(gdi_display))
303 data->whole_rect = data->client_rect = data->window_rect = *rectWindow;
304 if (win != WND_DESKTOP)
306 win->rectWindow = *rectWindow;
307 win->rectClient = *rectClient;
308 win->dwStyle = new_style;
309 WIN_ReleasePtr( win );
311 return ret;
314 if (ret)
316 /* invalidate DCEs */
318 if ((((swp_flags & SWP_AGG_NOPOSCHANGE) != SWP_AGG_NOPOSCHANGE) && (new_style & WS_VISIBLE)) ||
319 (swp_flags & (SWP_HIDEWINDOW | SWP_SHOWWINDOW)))
321 RECT rect;
322 UnionRect( &rect, rectWindow, &win->rectWindow );
323 invalidate_dce( hwnd, &rect );
326 win->rectWindow = *rectWindow;
327 win->rectClient = *rectClient;
328 old_style = win->dwStyle;
329 win->dwStyle = new_style;
330 data->window_rect = *rectWindow;
332 TRACE( "win %p window %s client %s style %08x\n",
333 hwnd, wine_dbgstr_rect(rectWindow), wine_dbgstr_rect(rectClient), new_style );
335 if (make_managed && (old_style & WS_VISIBLE))
337 wine_tsx11_lock();
338 XUnmapWindow( display, data->whole_window );
339 wine_tsx11_unlock();
340 old_style &= ~WS_VISIBLE; /* force it to be mapped again below */
343 if (!IsRectEmpty( &valid_rects[0] ))
345 int x_offset = 0, y_offset = 0;
347 if (data->whole_window)
349 /* the X server will move the bits for us */
350 x_offset = data->whole_rect.left - new_whole_rect.left;
351 y_offset = data->whole_rect.top - new_whole_rect.top;
354 if (x_offset != valid_rects[1].left - valid_rects[0].left ||
355 y_offset != valid_rects[1].top - valid_rects[0].top)
357 /* FIXME: should copy the window bits here */
358 RECT invalid_rect = valid_rects[0];
360 /* invalid_rects are relative to the client area */
361 OffsetRect( &invalid_rect, -rectClient->left, -rectClient->top );
362 RedrawWindow( hwnd, &invalid_rect, NULL, RDW_INVALIDATE | RDW_ERASE | RDW_ALLCHILDREN );
367 if (data->whole_window && !data->lock_changes)
369 if ((old_style & WS_VISIBLE) && !(new_style & WS_VISIBLE))
371 /* window got hidden, unmap it */
372 TRACE( "unmapping win %p\n", hwnd );
373 wine_tsx11_lock();
374 XUnmapWindow( display, data->whole_window );
375 wine_tsx11_unlock();
377 else if ((new_style & WS_VISIBLE) && !X11DRV_is_window_rect_mapped( rectWindow ))
379 /* resizing to zero size or off screen -> unmap */
380 TRACE( "unmapping zero size or off-screen win %p\n", hwnd );
381 wine_tsx11_lock();
382 XUnmapWindow( display, data->whole_window );
383 wine_tsx11_unlock();
387 X11DRV_sync_window_position( display, data, swp_flags, rectClient, &new_whole_rect );
389 if (data->whole_window && !data->lock_changes)
391 BOOL new_fs_state, mapped = FALSE;
393 if ((new_style & WS_VISIBLE) && !(new_style & WS_MINIMIZE) &&
394 X11DRV_is_window_rect_mapped( rectWindow ))
396 if (!(old_style & WS_VISIBLE))
398 /* window got shown, map it */
399 TRACE( "mapping win %p\n", hwnd );
400 mapped = TRUE;
402 else if ((swp_flags & (SWP_NOSIZE | SWP_NOMOVE)) != (SWP_NOSIZE | SWP_NOMOVE))
404 /* resizing from zero size to non-zero -> map */
405 TRACE( "mapping non zero size or off-screen win %p\n", hwnd );
406 mapped = TRUE;
408 if (mapped)
410 X11DRV_sync_window_style( display, data );
411 X11DRV_set_wm_hints( display, data );
412 wine_tsx11_lock();
413 XMapWindow( display, data->whole_window );
414 XFlush( display );
415 wine_tsx11_unlock();
417 SetRect( &old_screen_rect, 0, 0, screen_width, screen_height );
418 if (fullscreen_state_changed( data, &old_client_rect, &old_screen_rect, &new_fs_state ) || mapped)
419 update_fullscreen_state( display, data->whole_window, new_fs_state );
423 WIN_ReleasePtr( win );
424 return ret;
428 /***********************************************************************
429 * WINPOS_FindIconPos
431 * Find a suitable place for an iconic window.
433 static POINT WINPOS_FindIconPos( HWND hwnd, POINT pt )
435 RECT rect, rectParent;
436 HWND parent, child;
437 HRGN hrgn, tmp;
438 int xspacing, yspacing;
440 parent = GetAncestor( hwnd, GA_PARENT );
441 GetClientRect( parent, &rectParent );
442 if ((pt.x >= rectParent.left) && (pt.x + GetSystemMetrics(SM_CXICON) < rectParent.right) &&
443 (pt.y >= rectParent.top) && (pt.y + GetSystemMetrics(SM_CYICON) < rectParent.bottom))
444 return pt; /* The icon already has a suitable position */
446 xspacing = GetSystemMetrics(SM_CXICONSPACING);
447 yspacing = GetSystemMetrics(SM_CYICONSPACING);
449 /* Check if another icon already occupies this spot */
450 /* FIXME: this is completely inefficient */
452 hrgn = CreateRectRgn( 0, 0, 0, 0 );
453 tmp = CreateRectRgn( 0, 0, 0, 0 );
454 for (child = GetWindow( parent, GW_HWNDFIRST ); child; child = GetWindow( child, GW_HWNDNEXT ))
456 WND *childPtr;
457 if (child == hwnd) continue;
458 if ((GetWindowLongW( child, GWL_STYLE ) & (WS_VISIBLE|WS_MINIMIZE)) != (WS_VISIBLE|WS_MINIMIZE))
459 continue;
460 if (!(childPtr = WIN_GetPtr( child )) || childPtr == WND_OTHER_PROCESS)
461 continue;
462 SetRectRgn( tmp, childPtr->rectWindow.left, childPtr->rectWindow.top,
463 childPtr->rectWindow.right, childPtr->rectWindow.bottom );
464 CombineRgn( hrgn, hrgn, tmp, RGN_OR );
465 WIN_ReleasePtr( childPtr );
467 DeleteObject( tmp );
469 for (rect.bottom = rectParent.bottom; rect.bottom >= yspacing; rect.bottom -= yspacing)
471 for (rect.left = rectParent.left; rect.left <= rectParent.right - xspacing; rect.left += xspacing)
473 rect.right = rect.left + xspacing;
474 rect.top = rect.bottom - yspacing;
475 if (!RectInRegion( hrgn, &rect ))
477 /* No window was found, so it's OK for us */
478 pt.x = rect.left + (xspacing - GetSystemMetrics(SM_CXICON)) / 2;
479 pt.y = rect.top + (yspacing - GetSystemMetrics(SM_CYICON)) / 2;
480 DeleteObject( hrgn );
481 return pt;
485 DeleteObject( hrgn );
486 pt.x = pt.y = 0;
487 return pt;
491 /***********************************************************************
492 * WINPOS_MinMaximize
494 UINT WINPOS_MinMaximize( HWND hwnd, UINT cmd, LPRECT rect )
496 WND *wndPtr;
497 UINT swpFlags = 0;
498 POINT size;
499 LONG old_style;
500 WINDOWPLACEMENT wpl;
502 TRACE("%p %u\n", hwnd, cmd );
504 wpl.length = sizeof(wpl);
505 GetWindowPlacement( hwnd, &wpl );
507 if (HOOK_CallHooks( WH_CBT, HCBT_MINMAX, (WPARAM)hwnd, cmd, TRUE ))
508 return SWP_NOSIZE | SWP_NOMOVE;
510 if (IsIconic( hwnd ))
512 switch (cmd)
514 case SW_SHOWMINNOACTIVE:
515 case SW_SHOWMINIMIZED:
516 case SW_FORCEMINIMIZE:
517 case SW_MINIMIZE:
518 return SWP_NOSIZE | SWP_NOMOVE;
520 if (!SendMessageW( hwnd, WM_QUERYOPEN, 0, 0 )) return SWP_NOSIZE | SWP_NOMOVE;
521 swpFlags |= SWP_NOCOPYBITS;
524 switch( cmd )
526 case SW_SHOWMINNOACTIVE:
527 case SW_SHOWMINIMIZED:
528 case SW_FORCEMINIMIZE:
529 case SW_MINIMIZE:
530 if (!(wndPtr = WIN_GetPtr( hwnd )) || wndPtr == WND_OTHER_PROCESS) return 0;
531 if( wndPtr->dwStyle & WS_MAXIMIZE) wndPtr->flags |= WIN_RESTORE_MAX;
532 else wndPtr->flags &= ~WIN_RESTORE_MAX;
533 WIN_ReleasePtr( wndPtr );
535 old_style = WIN_SetStyle( hwnd, WS_MINIMIZE, WS_MAXIMIZE );
537 X11DRV_set_iconic_state( hwnd );
539 wpl.ptMinPosition = WINPOS_FindIconPos( hwnd, wpl.ptMinPosition );
541 if (!(old_style & WS_MINIMIZE)) swpFlags |= SWP_STATECHANGED;
542 SetRect( rect, wpl.ptMinPosition.x, wpl.ptMinPosition.y,
543 GetSystemMetrics(SM_CXICON), GetSystemMetrics(SM_CYICON) );
544 swpFlags |= SWP_NOCOPYBITS;
545 break;
547 case SW_MAXIMIZE:
548 old_style = GetWindowLongW( hwnd, GWL_STYLE );
549 if ((old_style & WS_MAXIMIZE) && (old_style & WS_VISIBLE)) return SWP_NOSIZE | SWP_NOMOVE;
551 WINPOS_GetMinMaxInfo( hwnd, &size, &wpl.ptMaxPosition, NULL, NULL );
553 old_style = WIN_SetStyle( hwnd, WS_MAXIMIZE, WS_MINIMIZE );
554 if (old_style & WS_MINIMIZE)
556 WINPOS_ShowIconTitle( hwnd, FALSE );
557 X11DRV_set_iconic_state( hwnd );
559 if (!(old_style & WS_MAXIMIZE)) swpFlags |= SWP_STATECHANGED;
560 SetRect( rect, wpl.ptMaxPosition.x, wpl.ptMaxPosition.y, size.x, size.y );
561 break;
563 case SW_SHOWNOACTIVATE:
564 case SW_SHOWNORMAL:
565 case SW_RESTORE:
566 old_style = WIN_SetStyle( hwnd, 0, WS_MINIMIZE | WS_MAXIMIZE );
567 if (old_style & WS_MINIMIZE)
569 BOOL restore_max;
571 WINPOS_ShowIconTitle( hwnd, FALSE );
572 X11DRV_set_iconic_state( hwnd );
574 if (!(wndPtr = WIN_GetPtr( hwnd )) || wndPtr == WND_OTHER_PROCESS) return 0;
575 restore_max = (wndPtr->flags & WIN_RESTORE_MAX) != 0;
576 WIN_ReleasePtr( wndPtr );
577 if (restore_max)
579 /* Restore to maximized position */
580 WINPOS_GetMinMaxInfo( hwnd, &size, &wpl.ptMaxPosition, NULL, NULL);
581 WIN_SetStyle( hwnd, WS_MAXIMIZE, 0 );
582 swpFlags |= SWP_STATECHANGED;
583 SetRect( rect, wpl.ptMaxPosition.x, wpl.ptMaxPosition.y, size.x, size.y );
584 break;
587 else if (!(old_style & WS_MAXIMIZE)) break;
589 swpFlags |= SWP_STATECHANGED;
591 /* Restore to normal position */
593 *rect = wpl.rcNormalPosition;
594 rect->right -= rect->left;
595 rect->bottom -= rect->top;
597 break;
600 return swpFlags;
604 /***********************************************************************
605 * ShowWindow (X11DRV.@)
607 BOOL X11DRV_ShowWindow( HWND hwnd, INT cmd )
609 WND *wndPtr;
610 HWND parent;
611 LONG style = GetWindowLongW( hwnd, GWL_STYLE );
612 BOOL wasVisible = (style & WS_VISIBLE) != 0;
613 BOOL showFlag = TRUE;
614 RECT newPos = {0, 0, 0, 0};
615 UINT swp = 0;
617 TRACE("hwnd=%p, cmd=%d, wasVisible %d\n", hwnd, cmd, wasVisible);
619 switch(cmd)
621 case SW_HIDE:
622 if (!wasVisible) return FALSE;
623 showFlag = FALSE;
624 swp |= SWP_HIDEWINDOW | SWP_NOSIZE | SWP_NOMOVE;
625 if (hwnd != GetActiveWindow())
626 swp |= SWP_NOACTIVATE | SWP_NOZORDER;
627 break;
629 case SW_SHOWMINNOACTIVE:
630 swp |= SWP_NOACTIVATE | SWP_NOZORDER;
631 /* fall through */
632 case SW_MINIMIZE:
633 case SW_FORCEMINIMIZE: /* FIXME: Does not work if thread is hung. */
634 if (style & WS_CHILD) swp |= SWP_NOACTIVATE | SWP_NOZORDER;
635 /* fall through */
636 case SW_SHOWMINIMIZED:
637 swp |= SWP_SHOWWINDOW | SWP_FRAMECHANGED;
638 swp |= WINPOS_MinMaximize( hwnd, cmd, &newPos );
639 if ((style & WS_MINIMIZE) && wasVisible) return TRUE;
640 break;
642 case SW_SHOWMAXIMIZED: /* same as SW_MAXIMIZE */
643 if (!wasVisible) swp |= SWP_SHOWWINDOW;
644 swp |= SWP_FRAMECHANGED;
645 swp |= WINPOS_MinMaximize( hwnd, SW_MAXIMIZE, &newPos );
646 if ((style & WS_MAXIMIZE) && wasVisible) return TRUE;
647 break;
649 case SW_SHOWNA:
650 swp |= SWP_NOACTIVATE | SWP_SHOWWINDOW | SWP_NOSIZE | SWP_NOMOVE;
651 if (style & WS_CHILD) swp |= SWP_NOZORDER;
652 break;
653 case SW_SHOW:
654 if (wasVisible) return TRUE;
655 swp |= SWP_SHOWWINDOW | SWP_NOSIZE | SWP_NOMOVE;
656 if (style & WS_CHILD) swp |= SWP_NOACTIVATE | SWP_NOZORDER;
657 break;
659 case SW_SHOWNOACTIVATE:
660 swp |= SWP_NOACTIVATE | SWP_NOZORDER;
661 /* fall through */
662 case SW_RESTORE:
663 /* fall through */
664 case SW_SHOWNORMAL: /* same as SW_NORMAL: */
665 case SW_SHOWDEFAULT: /* FIXME: should have its own handler */
666 if (!wasVisible) swp |= SWP_SHOWWINDOW;
667 if (style & (WS_MINIMIZE | WS_MAXIMIZE))
669 swp |= SWP_FRAMECHANGED;
670 swp |= WINPOS_MinMaximize( hwnd, cmd, &newPos );
672 else
674 if (wasVisible) return TRUE;
675 swp |= SWP_NOSIZE | SWP_NOMOVE;
677 if (style & WS_CHILD && !(swp & SWP_STATECHANGED)) swp |= SWP_NOACTIVATE | SWP_NOZORDER;
678 break;
681 if ((showFlag != wasVisible || cmd == SW_SHOWNA) && cmd != SW_SHOWMAXIMIZED && !(swp & SWP_STATECHANGED))
683 SendMessageW( hwnd, WM_SHOWWINDOW, showFlag, 0 );
684 if (!IsWindow( hwnd )) return wasVisible;
687 parent = GetAncestor( hwnd, GA_PARENT );
688 if (parent && !IsWindowVisible( parent ) && !(swp & SWP_STATECHANGED))
690 /* if parent is not visible simply toggle WS_VISIBLE and return */
691 if (showFlag) WIN_SetStyle( hwnd, WS_VISIBLE, 0 );
692 else WIN_SetStyle( hwnd, 0, WS_VISIBLE );
694 else
695 SetWindowPos( hwnd, HWND_TOP, newPos.left, newPos.top,
696 newPos.right, newPos.bottom, LOWORD(swp) );
698 if (cmd == SW_HIDE)
700 HWND hFocus;
702 /* FIXME: This will cause the window to be activated irrespective
703 * of whether it is owned by the same thread. Has to be done
704 * asynchronously.
707 if (hwnd == GetActiveWindow())
708 WINPOS_ActivateOtherWindow(hwnd);
710 /* Revert focus to parent */
711 hFocus = GetFocus();
712 if (hwnd == hFocus || IsChild(hwnd, hFocus))
714 HWND parent = GetAncestor(hwnd, GA_PARENT);
715 if (parent == GetDesktopWindow()) parent = 0;
716 SetFocus(parent);
720 if (IsIconic(hwnd)) WINPOS_ShowIconTitle( hwnd, TRUE );
722 if (!(wndPtr = WIN_GetPtr( hwnd )) || wndPtr == WND_OTHER_PROCESS) return wasVisible;
724 if (wndPtr->flags & WIN_NEED_SIZE)
726 /* should happen only in CreateWindowEx() */
727 int wParam = SIZE_RESTORED;
728 RECT client = wndPtr->rectClient;
730 wndPtr->flags &= ~WIN_NEED_SIZE;
731 if (wndPtr->dwStyle & WS_MAXIMIZE) wParam = SIZE_MAXIMIZED;
732 else if (wndPtr->dwStyle & WS_MINIMIZE) wParam = SIZE_MINIMIZED;
733 WIN_ReleasePtr( wndPtr );
735 SendMessageW( hwnd, WM_SIZE, wParam,
736 MAKELONG( client.right - client.left, client.bottom - client.top ));
737 SendMessageW( hwnd, WM_MOVE, 0, MAKELONG( client.left, client.top ));
739 else WIN_ReleasePtr( wndPtr );
741 /* if previous state was minimized Windows sets focus to the window */
742 if (style & WS_MINIMIZE) SetFocus( hwnd );
744 return wasVisible;
748 /**********************************************************************
749 * X11DRV_MapNotify
751 void X11DRV_MapNotify( HWND hwnd, XEvent *event )
753 struct x11drv_win_data *data;
754 HWND hwndFocus = GetFocus();
755 WND *win;
757 if (!(data = X11DRV_get_win_data( hwnd ))) return;
759 if (!(win = WIN_GetPtr( hwnd ))) return;
761 if (data->managed && (win->dwStyle & WS_VISIBLE) && (win->dwStyle & WS_MINIMIZE))
763 int x, y;
764 unsigned int width, height, border, depth;
765 Window root, top;
766 RECT rect;
767 LONG style = WS_VISIBLE;
769 /* FIXME: hack */
770 wine_tsx11_lock();
771 XGetGeometry( event->xmap.display, data->whole_window, &root, &x, &y, &width, &height,
772 &border, &depth );
773 XTranslateCoordinates( event->xmap.display, data->whole_window, root, 0, 0, &x, &y, &top );
774 wine_tsx11_unlock();
775 rect.left = x;
776 rect.top = y;
777 rect.right = x + width;
778 rect.bottom = y + height;
779 OffsetRect( &rect, virtual_screen_rect.left, virtual_screen_rect.top );
780 X11DRV_X_to_window_rect( data, &rect );
782 invalidate_dce( hwnd, &data->window_rect );
784 if (win->flags & WIN_RESTORE_MAX) style |= WS_MAXIMIZE;
785 WIN_SetStyle( hwnd, style, WS_MINIMIZE );
786 WIN_ReleasePtr( win );
788 SendMessageW( hwnd, WM_SHOWWINDOW, SW_RESTORE, 0 );
789 data->lock_changes++;
790 SetWindowPos( hwnd, 0, rect.left, rect.top, rect.right-rect.left, rect.bottom-rect.top,
791 SWP_NOZORDER | SWP_NOACTIVATE | SWP_FRAMECHANGED | SWP_STATECHANGED );
792 data->lock_changes--;
794 else WIN_ReleasePtr( win );
795 if (hwndFocus && IsChild( hwnd, hwndFocus )) X11DRV_SetFocus(hwndFocus); /* FIXME */
799 /**********************************************************************
800 * X11DRV_UnmapNotify
802 void X11DRV_UnmapNotify( HWND hwnd, XEvent *event )
804 struct x11drv_win_data *data;
805 WND *win;
807 if (!(data = X11DRV_get_win_data( hwnd ))) return;
809 if (!(win = WIN_GetPtr( hwnd ))) return;
811 if ((win->dwStyle & WS_VISIBLE) && data->managed &&
812 X11DRV_is_window_rect_mapped( &win->rectWindow ))
814 if (win->dwStyle & WS_MAXIMIZE)
815 win->flags |= WIN_RESTORE_MAX;
816 else if (!(win->dwStyle & WS_MINIMIZE))
817 win->flags &= ~WIN_RESTORE_MAX;
819 WIN_SetStyle( hwnd, WS_MINIMIZE, WS_MAXIMIZE );
820 WIN_ReleasePtr( win );
822 EndMenu();
823 SendMessageW( hwnd, WM_SHOWWINDOW, SW_MINIMIZE, 0 );
824 data->lock_changes++;
825 SetWindowPos( hwnd, 0, 0, 0, GetSystemMetrics(SM_CXICON), GetSystemMetrics(SM_CYICON),
826 SWP_NOMOVE | SWP_NOACTIVATE | SWP_NOZORDER | SWP_STATECHANGED );
827 data->lock_changes--;
829 else WIN_ReleasePtr( win );
832 struct desktop_resize_data
834 RECT old_screen_rect;
835 RECT old_virtual_rect;
838 static BOOL CALLBACK update_windows_on_desktop_resize( HWND hwnd, LPARAM lparam )
840 struct x11drv_win_data *data;
841 Display *display = thread_display();
842 struct desktop_resize_data *resize_data = (struct desktop_resize_data *)lparam;
843 int mask = 0;
845 if (!(data = X11DRV_get_win_data( hwnd ))) return TRUE;
847 if (GetWindowLongW( hwnd, GWL_STYLE ) & WS_VISIBLE)
849 BOOL new_fs_state;
851 if (fullscreen_state_changed( data, &data->client_rect, &resize_data->old_screen_rect, &new_fs_state ))
852 update_fullscreen_state( display, data->whole_window, new_fs_state );
855 if (resize_data->old_virtual_rect.left != virtual_screen_rect.left) mask |= CWX;
856 if (resize_data->old_virtual_rect.top != virtual_screen_rect.top) mask |= CWY;
857 if (mask && data->whole_window)
859 XWindowChanges changes;
861 wine_tsx11_lock();
862 changes.x = data->whole_rect.left - virtual_screen_rect.left;
863 changes.y = data->whole_rect.top - virtual_screen_rect.top;
864 XReconfigureWMWindow( display, data->whole_window,
865 DefaultScreen(display), mask, &changes );
866 wine_tsx11_unlock();
868 return TRUE;
872 /***********************************************************************
873 * X11DRV_handle_desktop_resize
875 void X11DRV_handle_desktop_resize( unsigned int width, unsigned int height )
877 HWND hwnd = GetDesktopWindow();
878 struct x11drv_win_data *data;
879 struct desktop_resize_data resize_data;
881 if (!(data = X11DRV_get_win_data( hwnd ))) return;
883 SetRect( &resize_data.old_screen_rect, 0, 0, screen_width, screen_height );
884 resize_data.old_virtual_rect = virtual_screen_rect;
886 screen_width = width;
887 screen_height = height;
888 xinerama_init();
889 TRACE("desktop %p change to (%dx%d)\n", hwnd, width, height);
890 data->lock_changes++;
891 X11DRV_SetWindowPos( hwnd, 0, &virtual_screen_rect, &virtual_screen_rect,
892 SWP_NOZORDER | SWP_NOMOVE | SWP_NOACTIVATE, NULL );
893 data->lock_changes--;
894 ClipCursor(NULL);
895 SendMessageTimeoutW( HWND_BROADCAST, WM_DISPLAYCHANGE, screen_depth,
896 MAKELPARAM( width, height ), SMTO_ABORTIFHUNG, 2000, NULL );
898 EnumWindows( update_windows_on_desktop_resize, (LPARAM)&resize_data );
902 /***********************************************************************
903 * X11DRV_ConfigureNotify
905 void X11DRV_ConfigureNotify( HWND hwnd, XEvent *xev )
907 XConfigureEvent *event = &xev->xconfigure;
908 struct x11drv_win_data *data;
909 RECT rect;
910 UINT flags;
911 int cx, cy, x = event->x, y = event->y;
913 if (!hwnd) return;
914 if (!(data = X11DRV_get_win_data( hwnd ))) return;
916 /* Get geometry */
918 if (!event->send_event) /* normal event, need to map coordinates to the root */
920 Window child;
921 wine_tsx11_lock();
922 XTranslateCoordinates( event->display, data->whole_window, root_window,
923 0, 0, &x, &y, &child );
924 wine_tsx11_unlock();
926 rect.left = x;
927 rect.top = y;
928 rect.right = x + event->width;
929 rect.bottom = y + event->height;
930 OffsetRect( &rect, virtual_screen_rect.left, virtual_screen_rect.top );
931 TRACE( "win %p new X rect %d,%d,%dx%d (event %d,%d,%dx%d)\n",
932 hwnd, rect.left, rect.top, rect.right-rect.left, rect.bottom-rect.top,
933 event->x, event->y, event->width, event->height );
934 X11DRV_X_to_window_rect( data, &rect );
936 x = rect.left;
937 y = rect.top;
938 cx = rect.right - rect.left;
939 cy = rect.bottom - rect.top;
940 flags = SWP_NOACTIVATE | SWP_NOZORDER;
942 /* Compare what has changed */
944 GetWindowRect( hwnd, &rect );
945 if (rect.left == x && rect.top == y) flags |= SWP_NOMOVE;
946 else
947 TRACE( "%p moving from (%d,%d) to (%d,%d)\n",
948 hwnd, rect.left, rect.top, x, y );
950 if ((rect.right - rect.left == cx && rect.bottom - rect.top == cy) ||
951 IsIconic(hwnd) ||
952 (IsRectEmpty( &rect ) && event->width == 1 && event->height == 1))
954 if (flags & SWP_NOMOVE) return; /* if nothing changed, don't do anything */
955 flags |= SWP_NOSIZE;
957 else
958 TRACE( "%p resizing from (%dx%d) to (%dx%d)\n",
959 hwnd, rect.right - rect.left, rect.bottom - rect.top, cx, cy );
961 data->lock_changes++;
962 SetWindowPos( hwnd, 0, x, y, cx, cy, flags );
963 data->lock_changes--;
967 /***********************************************************************
968 * SetWindowRgn (X11DRV.@)
970 * Assign specified region to window (for non-rectangular windows)
972 int X11DRV_SetWindowRgn( HWND hwnd, HRGN hrgn, BOOL redraw )
974 struct x11drv_win_data *data;
976 if (!(data = X11DRV_get_win_data( hwnd )))
978 if (IsWindow( hwnd ))
979 FIXME( "not supported on other thread window %p\n", hwnd );
980 SetLastError( ERROR_INVALID_WINDOW_HANDLE );
981 return FALSE;
984 #ifdef HAVE_LIBXSHAPE
985 if (data->whole_window)
987 Display *display = thread_display();
989 if (!hrgn)
991 wine_tsx11_lock();
992 XShapeCombineMask( display, data->whole_window,
993 ShapeBounding, 0, 0, None, ShapeSet );
994 wine_tsx11_unlock();
996 else
998 RGNDATA *pRegionData = X11DRV_GetRegionData( hrgn, 0 );
999 if (pRegionData)
1001 wine_tsx11_lock();
1002 XShapeCombineRectangles( display, data->whole_window, ShapeBounding,
1003 data->window_rect.left - data->whole_rect.left,
1004 data->window_rect.top - data->whole_rect.top,
1005 (XRectangle *)pRegionData->Buffer,
1006 pRegionData->rdh.nCount,
1007 ShapeSet, YXBanded );
1008 wine_tsx11_unlock();
1009 HeapFree(GetProcessHeap(), 0, pRegionData);
1013 #endif /* HAVE_LIBXSHAPE */
1015 invalidate_dce( hwnd, &data->window_rect );
1016 return TRUE;
1020 /***********************************************************************
1021 * draw_moving_frame
1023 * Draw the frame used when moving or resizing window.
1025 * FIXME: This causes problems in Win95 mode. (why?)
1027 static void draw_moving_frame( HDC hdc, RECT *rect, BOOL thickframe )
1029 if (thickframe)
1031 const int width = GetSystemMetrics(SM_CXFRAME);
1032 const int height = GetSystemMetrics(SM_CYFRAME);
1034 HBRUSH hbrush = SelectObject( hdc, GetStockObject( GRAY_BRUSH ) );
1035 PatBlt( hdc, rect->left, rect->top,
1036 rect->right - rect->left - width, height, PATINVERT );
1037 PatBlt( hdc, rect->left, rect->top + height, width,
1038 rect->bottom - rect->top - height, PATINVERT );
1039 PatBlt( hdc, rect->left + width, rect->bottom - 1,
1040 rect->right - rect->left - width, -height, PATINVERT );
1041 PatBlt( hdc, rect->right - 1, rect->top, -width,
1042 rect->bottom - rect->top - height, PATINVERT );
1043 SelectObject( hdc, hbrush );
1045 else DrawFocusRect( hdc, rect );
1049 /***********************************************************************
1050 * start_size_move
1052 * Initialisation of a move or resize, when initiatied from a menu choice.
1053 * Return hit test code for caption or sizing border.
1055 static LONG start_size_move( HWND hwnd, WPARAM wParam, POINT *capturePoint, LONG style )
1057 LONG hittest = 0;
1058 POINT pt;
1059 MSG msg;
1060 RECT rectWindow;
1062 GetWindowRect( hwnd, &rectWindow );
1064 if ((wParam & 0xfff0) == SC_MOVE)
1066 /* Move pointer at the center of the caption */
1067 RECT rect = rectWindow;
1068 /* Note: to be exactly centered we should take the different types
1069 * of border into account, but it shouldn't make more that a few pixels
1070 * of difference so let's not bother with that */
1071 rect.top += GetSystemMetrics(SM_CYBORDER);
1072 if (style & WS_SYSMENU)
1073 rect.left += GetSystemMetrics(SM_CXSIZE) + 1;
1074 if (style & WS_MINIMIZEBOX)
1075 rect.right -= GetSystemMetrics(SM_CXSIZE) + 1;
1076 if (style & WS_MAXIMIZEBOX)
1077 rect.right -= GetSystemMetrics(SM_CXSIZE) + 1;
1078 pt.x = (rect.right + rect.left) / 2;
1079 pt.y = rect.top + GetSystemMetrics(SM_CYSIZE)/2;
1080 hittest = HTCAPTION;
1081 *capturePoint = pt;
1083 else /* SC_SIZE */
1085 pt.x = pt.y = 0;
1086 while(!hittest)
1088 GetMessageW( &msg, 0, WM_KEYFIRST, WM_MOUSELAST );
1089 if (CallMsgFilterW( &msg, MSGF_SIZE )) continue;
1091 switch(msg.message)
1093 case WM_MOUSEMOVE:
1094 pt = msg.pt;
1095 hittest = SendMessageW( hwnd, WM_NCHITTEST, 0, MAKELONG( pt.x, pt.y ) );
1096 if ((hittest < HTLEFT) || (hittest > HTBOTTOMRIGHT)) hittest = 0;
1097 break;
1099 case WM_LBUTTONUP:
1100 return 0;
1102 case WM_KEYDOWN:
1103 switch(msg.wParam)
1105 case VK_UP:
1106 hittest = HTTOP;
1107 pt.x =(rectWindow.left+rectWindow.right)/2;
1108 pt.y = rectWindow.top + GetSystemMetrics(SM_CYFRAME) / 2;
1109 break;
1110 case VK_DOWN:
1111 hittest = HTBOTTOM;
1112 pt.x =(rectWindow.left+rectWindow.right)/2;
1113 pt.y = rectWindow.bottom - GetSystemMetrics(SM_CYFRAME) / 2;
1114 break;
1115 case VK_LEFT:
1116 hittest = HTLEFT;
1117 pt.x = rectWindow.left + GetSystemMetrics(SM_CXFRAME) / 2;
1118 pt.y =(rectWindow.top+rectWindow.bottom)/2;
1119 break;
1120 case VK_RIGHT:
1121 hittest = HTRIGHT;
1122 pt.x = rectWindow.right - GetSystemMetrics(SM_CXFRAME) / 2;
1123 pt.y =(rectWindow.top+rectWindow.bottom)/2;
1124 break;
1125 case VK_RETURN:
1126 case VK_ESCAPE: return 0;
1130 *capturePoint = pt;
1132 SetCursorPos( pt.x, pt.y );
1133 SendMessageW( hwnd, WM_SETCURSOR, (WPARAM)hwnd, MAKELONG( hittest, WM_MOUSEMOVE ));
1134 return hittest;
1138 /***********************************************************************
1139 * set_movesize_capture
1141 static void set_movesize_capture( HWND hwnd )
1143 HWND previous = 0;
1145 SERVER_START_REQ( set_capture_window )
1147 req->handle = hwnd;
1148 req->flags = CAPTURE_MOVESIZE;
1149 if (!wine_server_call_err( req ))
1151 previous = reply->previous;
1152 hwnd = reply->full_handle;
1155 SERVER_END_REQ;
1156 if (previous && previous != hwnd)
1157 SendMessageW( previous, WM_CAPTURECHANGED, 0, (LPARAM)hwnd );
1160 /***********************************************************************
1161 * X11DRV_WMMoveResizeWindow
1163 * Tells the window manager to initiate a move or resize operation.
1165 * SEE
1166 * http://freedesktop.org/Standards/wm-spec/1.3/ar01s04.html
1167 * or search for "_NET_WM_MOVERESIZE"
1169 static void X11DRV_WMMoveResizeWindow( HWND hwnd, int x, int y, int dir )
1171 XEvent xev;
1172 Display *display = thread_display();
1174 TRACE("hwnd %p, x %d, y %d, dir %d\n", hwnd, x, y, dir);
1176 xev.xclient.type = ClientMessage;
1177 xev.xclient.window = X11DRV_get_whole_window(hwnd);
1178 xev.xclient.message_type = x11drv_atom(_NET_WM_MOVERESIZE);
1179 xev.xclient.serial = 0;
1180 xev.xclient.display = display;
1181 xev.xclient.send_event = True;
1182 xev.xclient.format = 32;
1183 xev.xclient.data.l[0] = x; /* x coord */
1184 xev.xclient.data.l[1] = y; /* y coord */
1185 xev.xclient.data.l[2] = dir; /* direction */
1186 xev.xclient.data.l[3] = 1; /* button */
1187 xev.xclient.data.l[4] = 0; /* unused */
1189 /* need to ungrab the pointer that may have been automatically grabbed
1190 * with a ButtonPress event */
1191 wine_tsx11_lock();
1192 XUngrabPointer( display, CurrentTime );
1193 XSendEvent(display, root_window, False, SubstructureNotifyMask, &xev);
1194 wine_tsx11_unlock();
1197 /***********************************************************************
1198 * SysCommandSizeMove (X11DRV.@)
1200 * Perform SC_MOVE and SC_SIZE commands.
1202 void X11DRV_SysCommandSizeMove( HWND hwnd, WPARAM wParam )
1204 MSG msg;
1205 RECT sizingRect, mouseRect, origRect;
1206 HDC hdc;
1207 HWND parent;
1208 LONG hittest = (LONG)(wParam & 0x0f);
1209 WPARAM syscommand = wParam & 0xfff0;
1210 HCURSOR hDragCursor = 0, hOldCursor = 0;
1211 POINT minTrack, maxTrack;
1212 POINT capturePoint, pt;
1213 LONG style = GetWindowLongA( hwnd, GWL_STYLE );
1214 BOOL thickframe = HAS_THICKFRAME( style );
1215 BOOL iconic = style & WS_MINIMIZE;
1216 BOOL moved = FALSE;
1217 DWORD dwPoint = GetMessagePos ();
1218 BOOL DragFullWindows = FALSE;
1219 BOOL grab;
1220 Window parent_win, whole_win;
1221 Display *old_gdi_display = NULL;
1222 struct x11drv_thread_data *thread_data = x11drv_thread_data();
1223 struct x11drv_win_data *data;
1225 pt.x = (short)LOWORD(dwPoint);
1226 pt.y = (short)HIWORD(dwPoint);
1227 capturePoint = pt;
1229 if (IsZoomed(hwnd) || !IsWindowVisible(hwnd)) return;
1231 if (!(data = X11DRV_get_win_data( hwnd ))) return;
1233 TRACE("hwnd %p (%smanaged), command %04lx, hittest %d, pos %d,%d\n",
1234 hwnd, data->managed ? "" : "NOT ", syscommand, hittest, pt.x, pt.y);
1236 /* if we are managed then we let the WM do all the work */
1237 if (data->managed)
1239 int dir;
1240 if (syscommand == SC_MOVE)
1242 if (!hittest) dir = _NET_WM_MOVERESIZE_MOVE_KEYBOARD;
1243 else dir = _NET_WM_MOVERESIZE_MOVE;
1245 else if (!hittest) dir = _NET_WM_MOVERESIZE_SIZE_KEYBOARD;
1246 else
1247 switch (hittest)
1249 case WMSZ_LEFT: dir = _NET_WM_MOVERESIZE_SIZE_LEFT; break;
1250 case WMSZ_RIGHT: dir = _NET_WM_MOVERESIZE_SIZE_RIGHT; break;
1251 case WMSZ_TOP: dir = _NET_WM_MOVERESIZE_SIZE_TOP; break;
1252 case WMSZ_TOPLEFT: dir = _NET_WM_MOVERESIZE_SIZE_TOPLEFT; break;
1253 case WMSZ_TOPRIGHT: dir = _NET_WM_MOVERESIZE_SIZE_TOPRIGHT; break;
1254 case WMSZ_BOTTOM: dir = _NET_WM_MOVERESIZE_SIZE_BOTTOM; break;
1255 case WMSZ_BOTTOMLEFT: dir = _NET_WM_MOVERESIZE_SIZE_BOTTOMLEFT; break;
1256 case WMSZ_BOTTOMRIGHT: dir = _NET_WM_MOVERESIZE_SIZE_BOTTOMRIGHT; break;
1257 default:
1258 ERR("Invalid hittest value: %d\n", hittest);
1259 dir = _NET_WM_MOVERESIZE_SIZE_KEYBOARD;
1261 X11DRV_WMMoveResizeWindow( hwnd, pt.x, pt.y, dir );
1262 return;
1265 SystemParametersInfoA(SPI_GETDRAGFULLWINDOWS, 0, &DragFullWindows, 0);
1267 if (syscommand == SC_MOVE)
1269 if (!hittest) hittest = start_size_move( hwnd, wParam, &capturePoint, style );
1270 if (!hittest) return;
1272 else /* SC_SIZE */
1274 if ( hittest && (syscommand != SC_MOUSEMENU) )
1275 hittest += (HTLEFT - WMSZ_LEFT);
1276 else
1278 set_movesize_capture( hwnd );
1279 hittest = start_size_move( hwnd, wParam, &capturePoint, style );
1280 if (!hittest)
1282 set_movesize_capture(0);
1283 return;
1288 /* Get min/max info */
1290 WINPOS_GetMinMaxInfo( hwnd, NULL, NULL, &minTrack, &maxTrack );
1291 GetWindowRect( hwnd, &sizingRect );
1292 if (style & WS_CHILD)
1294 parent = GetParent(hwnd);
1295 /* make sizing rect relative to parent */
1296 MapWindowPoints( 0, parent, (POINT*)&sizingRect, 2 );
1297 GetClientRect( parent, &mouseRect );
1299 else
1301 parent = 0;
1302 mouseRect = virtual_screen_rect;
1304 origRect = sizingRect;
1306 if (ON_LEFT_BORDER(hittest))
1308 mouseRect.left = max( mouseRect.left, sizingRect.right-maxTrack.x );
1309 mouseRect.right = min( mouseRect.right, sizingRect.right-minTrack.x );
1311 else if (ON_RIGHT_BORDER(hittest))
1313 mouseRect.left = max( mouseRect.left, sizingRect.left+minTrack.x );
1314 mouseRect.right = min( mouseRect.right, sizingRect.left+maxTrack.x );
1316 if (ON_TOP_BORDER(hittest))
1318 mouseRect.top = max( mouseRect.top, sizingRect.bottom-maxTrack.y );
1319 mouseRect.bottom = min( mouseRect.bottom,sizingRect.bottom-minTrack.y);
1321 else if (ON_BOTTOM_BORDER(hittest))
1323 mouseRect.top = max( mouseRect.top, sizingRect.top+minTrack.y );
1324 mouseRect.bottom = min( mouseRect.bottom, sizingRect.top+maxTrack.y );
1326 if (parent) MapWindowPoints( parent, 0, (LPPOINT)&mouseRect, 2 );
1328 /* Retrieve a default cache DC (without using the window style) */
1329 hdc = GetDCEx( parent, 0, DCX_CACHE );
1331 if( iconic ) /* create a cursor for dragging */
1333 hDragCursor = (HCURSOR)GetClassLongPtrW( hwnd, GCLP_HICON);
1334 if( !hDragCursor ) hDragCursor = (HCURSOR)SendMessageW( hwnd, WM_QUERYDRAGICON, 0, 0L);
1335 if( !hDragCursor ) iconic = FALSE;
1338 /* repaint the window before moving it around */
1339 RedrawWindow( hwnd, NULL, 0, RDW_UPDATENOW | RDW_ALLCHILDREN );
1341 SendMessageW( hwnd, WM_ENTERSIZEMOVE, 0, 0 );
1342 set_movesize_capture( hwnd );
1344 /* grab the server only when moving top-level windows without desktop */
1345 grab = (!DragFullWindows && !parent && (root_window == DefaultRootWindow(gdi_display)));
1347 if (grab)
1349 wine_tsx11_lock();
1350 XSync( gdi_display, False );
1351 XGrabServer( thread_data->display );
1352 XSync( thread_data->display, False );
1353 /* switch gdi display to the thread display, since the server is grabbed */
1354 old_gdi_display = gdi_display;
1355 gdi_display = thread_data->display;
1356 wine_tsx11_unlock();
1358 whole_win = X11DRV_get_whole_window( GetAncestor(hwnd,GA_ROOT) );
1359 parent_win = parent ? X11DRV_get_whole_window( GetAncestor(parent,GA_ROOT) ) : root_window;
1361 wine_tsx11_lock();
1362 XGrabPointer( thread_data->display, whole_win, False,
1363 PointerMotionMask | ButtonPressMask | ButtonReleaseMask,
1364 GrabModeAsync, GrabModeAsync, parent_win, None, CurrentTime );
1365 wine_tsx11_unlock();
1366 thread_data->grab_window = whole_win;
1368 while(1)
1370 int dx = 0, dy = 0;
1372 if (!GetMessageW( &msg, 0, WM_KEYFIRST, WM_MOUSELAST )) break;
1373 if (CallMsgFilterW( &msg, MSGF_SIZE )) continue;
1375 /* Exit on button-up, Return, or Esc */
1376 if ((msg.message == WM_LBUTTONUP) ||
1377 ((msg.message == WM_KEYDOWN) &&
1378 ((msg.wParam == VK_RETURN) || (msg.wParam == VK_ESCAPE)))) break;
1380 if ((msg.message != WM_KEYDOWN) && (msg.message != WM_MOUSEMOVE))
1381 continue; /* We are not interested in other messages */
1383 pt = msg.pt;
1385 if (msg.message == WM_KEYDOWN) switch(msg.wParam)
1387 case VK_UP: pt.y -= 8; break;
1388 case VK_DOWN: pt.y += 8; break;
1389 case VK_LEFT: pt.x -= 8; break;
1390 case VK_RIGHT: pt.x += 8; break;
1393 pt.x = max( pt.x, mouseRect.left );
1394 pt.x = min( pt.x, mouseRect.right );
1395 pt.y = max( pt.y, mouseRect.top );
1396 pt.y = min( pt.y, mouseRect.bottom );
1398 dx = pt.x - capturePoint.x;
1399 dy = pt.y - capturePoint.y;
1401 if (dx || dy)
1403 if( !moved )
1405 moved = TRUE;
1407 if( iconic ) /* ok, no system popup tracking */
1409 hOldCursor = SetCursor(hDragCursor);
1410 ShowCursor( TRUE );
1411 WINPOS_ShowIconTitle( hwnd, FALSE );
1413 else if(!DragFullWindows)
1414 draw_moving_frame( hdc, &sizingRect, thickframe );
1417 if (msg.message == WM_KEYDOWN) SetCursorPos( pt.x, pt.y );
1418 else
1420 RECT newRect = sizingRect;
1421 WPARAM wpSizingHit = 0;
1423 if (hittest == HTCAPTION) OffsetRect( &newRect, dx, dy );
1424 if (ON_LEFT_BORDER(hittest)) newRect.left += dx;
1425 else if (ON_RIGHT_BORDER(hittest)) newRect.right += dx;
1426 if (ON_TOP_BORDER(hittest)) newRect.top += dy;
1427 else if (ON_BOTTOM_BORDER(hittest)) newRect.bottom += dy;
1428 if(!iconic && !DragFullWindows) draw_moving_frame( hdc, &sizingRect, thickframe );
1429 capturePoint = pt;
1431 /* determine the hit location */
1432 if (hittest >= HTLEFT && hittest <= HTBOTTOMRIGHT)
1433 wpSizingHit = WMSZ_LEFT + (hittest - HTLEFT);
1434 SendMessageW( hwnd, WM_SIZING, wpSizingHit, (LPARAM)&newRect );
1436 if (!iconic)
1438 if(!DragFullWindows)
1439 draw_moving_frame( hdc, &newRect, thickframe );
1440 else
1441 SetWindowPos( hwnd, 0, newRect.left, newRect.top,
1442 newRect.right - newRect.left,
1443 newRect.bottom - newRect.top,
1444 ( hittest == HTCAPTION ) ? SWP_NOSIZE : 0 );
1446 sizingRect = newRect;
1451 set_movesize_capture(0);
1452 if( iconic )
1454 if( moved ) /* restore cursors, show icon title later on */
1456 ShowCursor( FALSE );
1457 SetCursor( hOldCursor );
1460 else if (moved && !DragFullWindows)
1461 draw_moving_frame( hdc, &sizingRect, thickframe );
1463 ReleaseDC( parent, hdc );
1465 wine_tsx11_lock();
1466 XUngrabPointer( thread_data->display, CurrentTime );
1467 if (grab)
1469 XSync( thread_data->display, False );
1470 XUngrabServer( thread_data->display );
1471 XSync( thread_data->display, False );
1472 gdi_display = old_gdi_display;
1474 wine_tsx11_unlock();
1475 thread_data->grab_window = None;
1477 if (HOOK_CallHooks( WH_CBT, HCBT_MOVESIZE, (WPARAM)hwnd, (LPARAM)&sizingRect, TRUE ))
1478 moved = FALSE;
1480 SendMessageW( hwnd, WM_EXITSIZEMOVE, 0, 0 );
1481 SendMessageW( hwnd, WM_SETVISIBLE, !IsIconic(hwnd), 0L);
1483 /* window moved or resized */
1484 if (moved)
1486 /* if the moving/resizing isn't canceled call SetWindowPos
1487 * with the new position or the new size of the window
1489 if (!((msg.message == WM_KEYDOWN) && (msg.wParam == VK_ESCAPE)) )
1491 /* NOTE: SWP_NOACTIVATE prevents document window activation in Word 6 */
1492 if(!DragFullWindows)
1493 SetWindowPos( hwnd, 0, sizingRect.left, sizingRect.top,
1494 sizingRect.right - sizingRect.left,
1495 sizingRect.bottom - sizingRect.top,
1496 ( hittest == HTCAPTION ) ? SWP_NOSIZE : 0 );
1498 else
1499 { /* restore previous size/position */
1500 if(DragFullWindows)
1501 SetWindowPos( hwnd, 0, origRect.left, origRect.top,
1502 origRect.right - origRect.left,
1503 origRect.bottom - origRect.top,
1504 ( hittest == HTCAPTION ) ? SWP_NOSIZE : 0 );
1508 if (IsIconic(hwnd))
1510 /* Single click brings up the system menu when iconized */
1512 if( !moved )
1514 if(style & WS_SYSMENU )
1515 SendMessageW( hwnd, WM_SYSCOMMAND,
1516 SC_MOUSEMENU + HTSYSMENU, MAKELONG(pt.x,pt.y));
1518 else WINPOS_ShowIconTitle( hwnd, TRUE );