winpos.c 83 KB
Newer Older
Alexandre Julliard's avatar
Alexandre Julliard committed
1 2 3
/*
 * Window position related functions.
 *
Alexandre Julliard's avatar
Alexandre Julliard committed
4
 * Copyright 1993, 1994, 1995 Alexandre Julliard
5
 *                       1995, 1996, 1999 Alex Korobka
6 7 8 9 10 11 12 13 14 15 16 17 18
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * 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
Alexandre Julliard's avatar
Alexandre Julliard committed
20 21
 */

Steven Edwards's avatar
Steven Edwards committed
22 23 24
#include "config.h"
#include "wine/port.h"

25
#include <stdarg.h>
Alexandre Julliard's avatar
Alexandre Julliard committed
26
#include <string.h>
27 28
#include "ntstatus.h"
#define WIN32_NO_STATUS
29
#include "windef.h"
30
#include "winbase.h"
31
#include "wingdi.h"
32
#include "winerror.h"
33
#include "wine/server.h"
34
#include "controls.h"
35
#include "user_private.h"
Alexandre Julliard's avatar
Alexandre Julliard committed
36
#include "win.h"
37
#include "wine/debug.h"
Alexandre Julliard's avatar
Alexandre Julliard committed
38

39
WINE_DEFAULT_DEBUG_CHANNEL(win);
40

41 42
#define SWP_AGG_NOGEOMETRYCHANGE \
    (SWP_NOSIZE | SWP_NOCLIENTSIZE | SWP_NOZORDER)
43 44 45 46 47
#define SWP_AGG_NOPOSCHANGE \
    (SWP_NOSIZE | SWP_NOMOVE | SWP_NOCLIENTSIZE | SWP_NOCLIENTMOVE | SWP_NOZORDER)
#define SWP_AGG_STATUSFLAGS \
    (SWP_AGG_NOPOSCHANGE | SWP_FRAMECHANGED | SWP_HIDEWINDOW | SWP_SHOWWINDOW)

Alexandre Julliard's avatar
Alexandre Julliard committed
48 49 50 51 52 53 54 55
#define HAS_DLGFRAME(style,exStyle) \
    (((exStyle) & WS_EX_DLGMODALFRAME) || \
     (((style) & WS_DLGFRAME) && !((style) & WS_BORDER)))

#define HAS_THICKFRAME(style) \
    (((style) & WS_THICKFRAME) && \
     !(((style) & (WS_DLGFRAME|WS_BORDER)) == WS_DLGFRAME))

56
#define EMPTYPOINT(pt) ((pt).x == -1 && (pt).y == -1)
Alexandre Julliard's avatar
Alexandre Julliard committed
57

58 59 60 61 62 63 64 65 66
#define ON_LEFT_BORDER(hit) \
 (((hit) == HTLEFT) || ((hit) == HTTOPLEFT) || ((hit) == HTBOTTOMLEFT))
#define ON_RIGHT_BORDER(hit) \
 (((hit) == HTRIGHT) || ((hit) == HTTOPRIGHT) || ((hit) == HTBOTTOMRIGHT))
#define ON_TOP_BORDER(hit) \
 (((hit) == HTTOP) || ((hit) == HTTOPLEFT) || ((hit) == HTTOPRIGHT))
#define ON_BOTTOM_BORDER(hit) \
 (((hit) == HTBOTTOM) || ((hit) == HTBOTTOMLEFT) || ((hit) == HTBOTTOMRIGHT))

Alexandre Julliard's avatar
Alexandre Julliard committed
67 68 69 70
#define PLACE_MIN		0x0001
#define PLACE_MAX		0x0002
#define PLACE_RECT		0x0004

71 72 73 74 75 76 77 78 79 80 81 82 83

#define DWP_MAGIC  ((INT)('W' | ('P' << 8) | ('O' << 16) | ('S' << 24)))

typedef struct
{
    INT       actualCount;
    INT       suggestedCount;
    BOOL      valid;
    INT       wMagic;
    HWND      hwndParent;
    WINDOWPOS winPos[1];
} DWP;

Alexandre Julliard's avatar
Alexandre Julliard committed
84

Alexandre Julliard's avatar
Alexandre Julliard committed
85
/***********************************************************************
86
 *		SwitchToThisWindow (USER32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
87
 */
88
void WINAPI SwitchToThisWindow( HWND hwnd, BOOL alt_tab )
Alexandre Julliard's avatar
Alexandre Julliard committed
89
{
90 91
    if (IsIconic( hwnd )) ShowWindow( hwnd, SW_RESTORE );
    else BringWindowToTop( hwnd );
Alexandre Julliard's avatar
Alexandre Julliard committed
92 93 94
}


Alexandre Julliard's avatar
Alexandre Julliard committed
95
/***********************************************************************
96
 *		GetWindowRect (USER32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
97
 */
98
BOOL WINAPI GetWindowRect( HWND hwnd, LPRECT rect )
Alexandre Julliard's avatar
Alexandre Julliard committed
99
{
100
    BOOL ret = WIN_GetRectangles( hwnd, rect, NULL );
101 102 103
    if (ret)
    {
        MapWindowPoints( GetAncestor( hwnd, GA_PARENT ), 0, (POINT *)rect, 2 );
104
        TRACE( "hwnd %p (%s)\n", hwnd, wine_dbgstr_rect(rect) );
105 106
    }
    return ret;
Alexandre Julliard's avatar
Alexandre Julliard committed
107 108 109
}


Alexandre Julliard's avatar
Alexandre Julliard committed
110
/***********************************************************************
111
 *		GetWindowRgn (USER32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
112
 */
113 114 115
int WINAPI GetWindowRgn ( HWND hwnd, HRGN hrgn )
{
    int nRet = ERROR;
116
    NTSTATUS status;
117 118 119
    HRGN win_rgn = 0;
    RGNDATA *data;
    size_t size = 256;
120

121
    do
122
    {
123 124 125 126 127 128 129
        if (!(data = HeapAlloc( GetProcessHeap(), 0, sizeof(*data) + size - 1 )))
        {
            SetLastError( ERROR_OUTOFMEMORY );
            return ERROR;
        }
        SERVER_START_REQ( get_window_region )
        {
130
            req->window = wine_server_user_handle( hwnd );
131
            wine_server_set_reply( req, data->Buffer, size );
132
            if (!(status = wine_server_call( req )))
133
            {
134 135
                size_t reply_size = wine_server_reply_size( reply );
                if (reply_size)
136 137 138 139 140 141 142 143
                {
                    data->rdh.dwSize   = sizeof(data->rdh);
                    data->rdh.iType    = RDH_RECTANGLES;
                    data->rdh.nCount   = reply_size / sizeof(RECT);
                    data->rdh.nRgnSize = reply_size;
                    win_rgn = ExtCreateRegion( NULL, size, data );
                }
            }
144
            else size = reply->total_size;
145 146 147
        }
        SERVER_END_REQ;
        HeapFree( GetProcessHeap(), 0, data );
148
    } while (status == STATUS_BUFFER_OVERFLOW);
149

150 151
    if (status) SetLastError( RtlNtStatusToDosError(status) );
    else if (win_rgn)
152
    {
153 154
        nRet = CombineRgn( hrgn, win_rgn, 0, RGN_COPY );
        DeleteObject( win_rgn );
155 156 157
    }
    return nRet;
}
Alexandre Julliard's avatar
Alexandre Julliard committed
158

159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
/***********************************************************************
 *		GetWindowRgnBox (USER32.@)
 */
int WINAPI GetWindowRgnBox( HWND hwnd, LPRECT prect )
{
    int ret = ERROR;
    HRGN hrgn;

    if (!prect)
        return ERROR;

    if ((hrgn = CreateRectRgn(0, 0, 0, 0)))
    {
        if ((ret = GetWindowRgn( hwnd, hrgn )) != ERROR )
            ret = GetRgnBox( hrgn, prect );
        DeleteObject(hrgn);
    }

    return ret;
}
179

180
/***********************************************************************
181
 *		SetWindowRgn (USER32.@)
182 183
 */
int WINAPI SetWindowRgn( HWND hwnd, HRGN hrgn, BOOL bRedraw )
Alexandre Julliard's avatar
Alexandre Julliard committed
184
{
185 186
    static const RECT empty_rect;
    BOOL ret;
187

188
    if (hrgn)
189
    {
190 191 192 193 194 195 196 197 198 199 200 201
        RGNDATA *data;
        DWORD size;

        if (!(size = GetRegionData( hrgn, 0, NULL ))) return FALSE;
        if (!(data = HeapAlloc( GetProcessHeap(), 0, size ))) return FALSE;
        if (!GetRegionData( hrgn, size, data ))
        {
            HeapFree( GetProcessHeap(), 0, data );
            return FALSE;
        }
        SERVER_START_REQ( set_window_region )
        {
202
            req->window = wine_server_user_handle( hwnd );
203
            req->redraw = (bRedraw != 0);
204 205 206 207 208 209 210
            if (data->rdh.nCount)
                wine_server_add_data( req, data->Buffer, data->rdh.nCount * sizeof(RECT) );
            else
                wine_server_add_data( req, &empty_rect, sizeof(empty_rect) );
            ret = !wine_server_call_err( req );
        }
        SERVER_END_REQ;
211
        HeapFree( GetProcessHeap(), 0, data );
212 213 214 215 216
    }
    else  /* clear existing region */
    {
        SERVER_START_REQ( set_window_region )
        {
217
            req->window = wine_server_user_handle( hwnd );
218
            req->redraw = (bRedraw != 0);
219 220 221
            ret = !wine_server_call_err( req );
        }
        SERVER_END_REQ;
222
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
223

224
    if (ret) ret = USER_Driver->pSetWindowRgn( hwnd, hrgn, bRedraw );
225

226 227
    if (ret)
    {
228
        UINT swp_flags = SWP_NOSIZE|SWP_NOMOVE|SWP_NOZORDER|SWP_NOACTIVATE|SWP_FRAMECHANGED|SWP_NOCLIENTSIZE|SWP_NOCLIENTMOVE;
229 230
        if (!bRedraw) swp_flags |= SWP_NOREDRAW;
        SetWindowPos( hwnd, 0, 0, 0, 0, 0, swp_flags );
231
        invalidate_dce( hwnd, NULL );
232
    }
233
    return ret;
Alexandre Julliard's avatar
Alexandre Julliard committed
234 235
}

Alexandre Julliard's avatar
Alexandre Julliard committed
236 237

/***********************************************************************
238
 *		GetClientRect (USER32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
239
 */
240
BOOL WINAPI GetClientRect( HWND hwnd, LPRECT rect )
Alexandre Julliard's avatar
Alexandre Julliard committed
241
{
242
    BOOL ret;
243

244
    if ((ret = WIN_GetRectangles( hwnd, NULL, rect )))
245
    {
246 247
        rect->right -= rect->left;
        rect->bottom -= rect->top;
248
        rect->left = rect->top = 0;
249 250
    }
    return ret;
Alexandre Julliard's avatar
Alexandre Julliard committed
251 252 253 254
}


/*******************************************************************
255
 *		ClientToScreen (USER32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
256
 */
257
BOOL WINAPI ClientToScreen( HWND hwnd, LPPOINT lppnt )
Alexandre Julliard's avatar
Alexandre Julliard committed
258
{
259
    MapWindowPoints( hwnd, 0, lppnt, 1 );
Alexandre Julliard's avatar
Alexandre Julliard committed
260 261 262 263 264
    return TRUE;
}


/*******************************************************************
265
 *		ScreenToClient (USER32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
266
 */
267
BOOL WINAPI ScreenToClient( HWND hwnd, LPPOINT lppnt )
Alexandre Julliard's avatar
Alexandre Julliard committed
268
{
269
    MapWindowPoints( 0, hwnd, lppnt, 1 );
270
    return TRUE;
Alexandre Julliard's avatar
Alexandre Julliard committed
271 272 273
}


274
/***********************************************************************
275
 *           list_children_from_point
276
 *
277 278 279
 * Get the list of children that can contain point from the server.
 * Point is in screen coordinates.
 * Returned list must be freed by caller.
280
 */
281
static HWND *list_children_from_point( HWND hwnd, POINT pt )
282
{
283
    HWND *list;
284
    int i, size = 128;
285

286
    for (;;)
287
    {
288
        int count = 0;
289

290
        if (!(list = HeapAlloc( GetProcessHeap(), 0, size * sizeof(HWND) ))) break;
291

292
        SERVER_START_REQ( get_window_children_from_point )
293
        {
294
            req->parent = wine_server_user_handle( hwnd );
295 296
            req->x = pt.x;
            req->y = pt.y;
297
            wine_server_set_reply( req, list, (size-1) * sizeof(user_handle_t) );
298
            if (!wine_server_call( req )) count = reply->count;
299
        }
300 301
        SERVER_END_REQ;
        if (count && count < size)
302
        {
303 304 305
            /* start from the end since HWND is potentially larger than user_handle_t */
            for (i = count - 1; i >= 0; i--)
                list[i] = wine_server_ptr_handle( ((user_handle_t *)list)[i] );
306 307
            list[count] = 0;
            return list;
308
        }
309 310 311
        HeapFree( GetProcessHeap(), 0, list );
        if (!count) break;
        size = count + 1;  /* restart with a large enough buffer */
312
    }
313
    return NULL;
314 315 316
}


Alexandre Julliard's avatar
Alexandre Julliard committed
317 318
/***********************************************************************
 *           WINPOS_WindowFromPoint
Alexandre Julliard's avatar
Alexandre Julliard committed
319
 *
Alexandre Julliard's avatar
Alexandre Julliard committed
320
 * Find the window and hittest for a given point.
Alexandre Julliard's avatar
Alexandre Julliard committed
321
 */
322
HWND WINPOS_WindowFromPoint( HWND hwndScope, POINT pt, INT *hittest )
Alexandre Julliard's avatar
Alexandre Julliard committed
323
{
324 325
    int i, res;
    HWND ret, *list;
326 327 328

    if (!hwndScope) hwndScope = GetDesktopWindow();

329
    *hittest = HTNOWHERE;
330

331
    if (!(list = list_children_from_point( hwndScope, pt ))) return 0;
332

333 334 335
    /* now determine the hittest */

    for (i = 0; list[i]; i++)
Alexandre Julliard's avatar
Alexandre Julliard committed
336
    {
337
        LONG style = GetWindowLongW( list[i], GWL_STYLE );
338

339 340 341 342 343
        /* If window is minimized or disabled, return at once */
        if (style & WS_MINIMIZE)
        {
            *hittest = HTCAPTION;
            break;
344
        }
345 346 347 348 349 350 351 352 353 354 355
        if (style & WS_DISABLED)
        {
            *hittest = HTERROR;
            break;
        }
        /* Send WM_NCCHITTEST (if same thread) */
        if (!WIN_IsCurrentThread( list[i] ))
        {
            *hittest = HTCLIENT;
            break;
        }
356
        res = SendMessageW( list[i], WM_NCHITTEST, 0, MAKELONG(pt.x,pt.y) );
357 358 359 360 361 362
        if (res != HTTRANSPARENT)
        {
            *hittest = res;  /* Found the window */
            break;
        }
        /* continue search with next window in z-order */
Alexandre Julliard's avatar
Alexandre Julliard committed
363
    }
364 365
    ret = list[i];
    HeapFree( GetProcessHeap(), 0, list );
366
    TRACE( "scope %p (%d,%d) returning %p\n", hwndScope, pt.x, pt.y, ret );
367
    return ret;
Alexandre Julliard's avatar
Alexandre Julliard committed
368 369 370
}


Alexandre Julliard's avatar
Alexandre Julliard committed
371
/*******************************************************************
372
 *		WindowFromPoint (USER32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
373
 */
374
HWND WINAPI WindowFromPoint( POINT pt )
Alexandre Julliard's avatar
Alexandre Julliard committed
375
{
376 377
    INT hittest;
    return WINPOS_WindowFromPoint( 0, pt, &hittest );
Alexandre Julliard's avatar
Alexandre Julliard committed
378 379 380
}


Alexandre Julliard's avatar
Alexandre Julliard committed
381
/*******************************************************************
382
 *		ChildWindowFromPoint (USER32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
383
 */
384
HWND WINAPI ChildWindowFromPoint( HWND hwndParent, POINT pt )
Alexandre Julliard's avatar
Alexandre Julliard committed
385
{
386
    return ChildWindowFromPointEx( hwndParent, pt, CWP_ALL );
Alexandre Julliard's avatar
Alexandre Julliard committed
387 388
}

389 390 391 392 393 394 395 396
/*******************************************************************
 *		RealChildWindowFromPoint (USER32.@)
 */
HWND WINAPI RealChildWindowFromPoint( HWND hwndParent, POINT pt )
{
    return ChildWindowFromPointEx( hwndParent, pt, CWP_SKIPTRANSPARENT );
}

397
/*******************************************************************
398
 *		ChildWindowFromPointEx (USER32.@)
399
 */
400
HWND WINAPI ChildWindowFromPointEx( HWND hwndParent, POINT pt, UINT uFlags)
401 402
{
    /* pt is in the client coordinates */
403 404
    HWND *list;
    int i;
405
    RECT rect;
406
    HWND retvalue;
407

408 409
    GetClientRect( hwndParent, &rect );
    if (!PtInRect( &rect, pt )) return 0;
410
    if (!(list = WIN_ListChildren( hwndParent ))) return hwndParent;
411

412
    for (i = 0; list[i]; i++)
413
    {
414 415 416 417 418 419 420 421 422
        if (!WIN_GetRectangles( list[i], &rect, NULL )) continue;
        if (!PtInRect( &rect, pt )) continue;
        if (uFlags & (CWP_SKIPINVISIBLE|CWP_SKIPDISABLED))
        {
            LONG style = GetWindowLongW( list[i], GWL_STYLE );
            if ((uFlags & CWP_SKIPINVISIBLE) && !(style & WS_VISIBLE)) continue;
            if ((uFlags & CWP_SKIPDISABLED) && (style & WS_DISABLED)) continue;
        }
        if (uFlags & CWP_SKIPTRANSPARENT)
423
        {
424
            if (GetWindowLongW( list[i], GWL_EXSTYLE ) & WS_EX_TRANSPARENT) continue;
425
        }
426
        break;
427
    }
428
    retvalue = list[i];
429
    HeapFree( GetProcessHeap(), 0, list );
430
    if (!retvalue) retvalue = hwndParent;
431
    return retvalue;
432 433
}

Alexandre Julliard's avatar
Alexandre Julliard committed
434 435 436 437 438 439 440

/*******************************************************************
 *         WINPOS_GetWinOffset
 *
 * Calculate the offset between the origin of the two windows. Used
 * to implement MapWindowPoints.
 */
441
static void WINPOS_GetWinOffset( HWND hwndFrom, HWND hwndTo, POINT *offset )
Alexandre Julliard's avatar
Alexandre Julliard committed
442
{
443
    WND * wndPtr;
Alexandre Julliard's avatar
Alexandre Julliard committed
444

Alexandre Julliard's avatar
Alexandre Julliard committed
445
    offset->x = offset->y = 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
446

447
    /* Translate source window origin to screen coords */
Alexandre Julliard's avatar
Alexandre Julliard committed
448
    if (hwndFrom)
Alexandre Julliard's avatar
Alexandre Julliard committed
449
    {
450 451
        HWND hwnd = hwndFrom;

452
        while (hwnd)
Alexandre Julliard's avatar
Alexandre Julliard committed
453
        {
454 455
            if (hwnd == hwndTo) return;
            if (!(wndPtr = WIN_GetPtr( hwnd )))
456
            {
457
                ERR( "bad hwndFrom = %p\n", hwnd );
458
                return;
459
            }
460
            if (wndPtr == WND_DESKTOP) break;
461
            if (wndPtr == WND_OTHER_PROCESS) goto other_process;
462 463 464 465 466
            if (wndPtr->parent)
            {
                offset->x += wndPtr->rectClient.left;
                offset->y += wndPtr->rectClient.top;
            }
467 468
            hwnd = wndPtr->parent;
            WIN_ReleasePtr( wndPtr );
Alexandre Julliard's avatar
Alexandre Julliard committed
469
        }
Alexandre Julliard's avatar
Alexandre Julliard committed
470 471
    }

472
    /* Translate origin to destination window coords */
Alexandre Julliard's avatar
Alexandre Julliard committed
473
    if (hwndTo)
Alexandre Julliard's avatar
Alexandre Julliard committed
474
    {
475 476
        HWND hwnd = hwndTo;

477
        while (hwnd)
Alexandre Julliard's avatar
Alexandre Julliard committed
478
        {
479
            if (!(wndPtr = WIN_GetPtr( hwnd )))
480
            {
481
                ERR( "bad hwndTo = %p\n", hwnd );
482
                return;
483
            }
484
            if (wndPtr == WND_DESKTOP) break;
485
            if (wndPtr == WND_OTHER_PROCESS) goto other_process;
486 487 488 489 490
            if (wndPtr->parent)
            {
                offset->x -= wndPtr->rectClient.left;
                offset->y -= wndPtr->rectClient.top;
            }
491 492 493 494 495 496 497 498 499 500
            hwnd = wndPtr->parent;
            WIN_ReleasePtr( wndPtr );
        }
    }
    return;

 other_process:  /* one of the parents may belong to another process, do it the hard way */
    offset->x = offset->y = 0;
    SERVER_START_REQ( get_windows_offset )
    {
501 502
        req->from = wine_server_user_handle( hwndFrom );
        req->to   = wine_server_user_handle( hwndTo );
503
        if (!wine_server_call( req ))
504
        {
505 506
            offset->x = reply->x;
            offset->y = reply->y;
507
        }
Alexandre Julliard's avatar
Alexandre Julliard committed
508
    }
509
    SERVER_END_REQ;
Alexandre Julliard's avatar
Alexandre Julliard committed
510 511 512 513
}


/*******************************************************************
514
 *		MapWindowPoints (USER.258)
Alexandre Julliard's avatar
Alexandre Julliard committed
515
 */
Alexandre Julliard's avatar
Alexandre Julliard committed
516 517
void WINAPI MapWindowPoints16( HWND16 hwndFrom, HWND16 hwndTo,
                               LPPOINT16 lppt, UINT16 count )
Alexandre Julliard's avatar
Alexandre Julliard committed
518
{
519
    POINT offset;
Alexandre Julliard's avatar
Alexandre Julliard committed
520

521
    WINPOS_GetWinOffset( WIN_Handle32(hwndFrom), WIN_Handle32(hwndTo), &offset );
Alexandre Julliard's avatar
Alexandre Julliard committed
522 523 524 525 526 527 528 529 530 531
    while (count--)
    {
	lppt->x += offset.x;
	lppt->y += offset.y;
        lppt++;
    }
}


/*******************************************************************
532
 *		MapWindowPoints (USER32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
533
 */
534
INT WINAPI MapWindowPoints( HWND hwndFrom, HWND hwndTo, LPPOINT lppt, UINT count )
Alexandre Julliard's avatar
Alexandre Julliard committed
535
{
536
    POINT offset;
Alexandre Julliard's avatar
Alexandre Julliard committed
537

538
    WINPOS_GetWinOffset( hwndFrom, hwndTo, &offset );
Alexandre Julliard's avatar
Alexandre Julliard committed
539
    while (count--)
Alexandre Julliard's avatar
Alexandre Julliard committed
540
    {
Alexandre Julliard's avatar
Alexandre Julliard committed
541 542 543
	lppt->x += offset.x;
	lppt->y += offset.y;
        lppt++;
Alexandre Julliard's avatar
Alexandre Julliard committed
544
    }
545
    return MAKELONG( LOWORD(offset.x), LOWORD(offset.y) );
Alexandre Julliard's avatar
Alexandre Julliard committed
546 547 548
}


Alexandre Julliard's avatar
Alexandre Julliard committed
549
/***********************************************************************
550
 *		IsIconic (USER32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
551
 */
552
BOOL WINAPI IsIconic(HWND hWnd)
Alexandre Julliard's avatar
Alexandre Julliard committed
553
{
554
    return (GetWindowLongW( hWnd, GWL_STYLE ) & WS_MINIMIZE) != 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
555
}
Alexandre Julliard's avatar
Alexandre Julliard committed
556 557


Alexandre Julliard's avatar
Alexandre Julliard committed
558
/***********************************************************************
559
 *		IsZoomed (USER32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
560
 */
561
BOOL WINAPI IsZoomed(HWND hWnd)
Alexandre Julliard's avatar
Alexandre Julliard committed
562
{
563
    return (GetWindowLongW( hWnd, GWL_STYLE ) & WS_MAXIMIZE) != 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
564 565 566
}


567
/*******************************************************************
568
 *		AllowSetForegroundWindow (USER32.@)
569 570 571 572 573 574 575 576 577 578
 */
BOOL WINAPI AllowSetForegroundWindow( DWORD procid )
{
    /* FIXME: If Win98/2000 style SetForegroundWindow behavior is
     * implemented, then fix this function. */
    return TRUE;
}


/*******************************************************************
579
 *		LockSetForegroundWindow (USER32.@)
580 581 582 583 584 585 586 587 588
 */
BOOL WINAPI LockSetForegroundWindow( UINT lockcode )
{
    /* FIXME: If Win98/2000 style SetForegroundWindow behavior is
     * implemented, then fix this function. */
    return TRUE;
}


Alexandre Julliard's avatar
Alexandre Julliard committed
589
/***********************************************************************
590
 *		BringWindowToTop (USER32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
591
 */
592
BOOL WINAPI BringWindowToTop( HWND hwnd )
Alexandre Julliard's avatar
Alexandre Julliard committed
593
{
594
    return SetWindowPos( hwnd, HWND_TOP, 0, 0, 0, 0, SWP_NOMOVE|SWP_NOSIZE );
Alexandre Julliard's avatar
Alexandre Julliard committed
595 596 597
}


Alexandre Julliard's avatar
Alexandre Julliard committed
598
/***********************************************************************
599
 *		MoveWindow (USER32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
600
 */
601 602
BOOL WINAPI MoveWindow( HWND hwnd, INT x, INT y, INT cx, INT cy,
                            BOOL repaint )
603
{
Alexandre Julliard's avatar
Alexandre Julliard committed
604 605
    int flags = SWP_NOZORDER | SWP_NOACTIVATE;
    if (!repaint) flags |= SWP_NOREDRAW;
606
    TRACE("%p %d,%d %dx%d %d\n", hwnd, x, y, cx, cy, repaint );
607
    return SetWindowPos( hwnd, 0, x, y, cx, cy, flags );
Alexandre Julliard's avatar
Alexandre Julliard committed
608 609
}

Alexandre Julliard's avatar
Alexandre Julliard committed
610 611 612 613

/***********************************************************************
 *           WINPOS_RedrawIconTitle
 */
614
BOOL WINPOS_RedrawIconTitle( HWND hWnd )
Alexandre Julliard's avatar
Alexandre Julliard committed
615
{
616 617 618 619
    HWND icon_title = 0;
    WND *win = WIN_GetPtr( hWnd );

    if (win && win != WND_OTHER_PROCESS && win != WND_DESKTOP)
Alexandre Julliard's avatar
Alexandre Julliard committed
620
    {
621 622
	icon_title = win->icon_title;
        WIN_ReleasePtr( win );
Alexandre Julliard's avatar
Alexandre Julliard committed
623
    }
624 625 626 627
    if (!icon_title) return FALSE;
    SendMessageW( icon_title, WM_SHOWWINDOW, TRUE, 0 );
    InvalidateRect( icon_title, NULL, TRUE );
    return TRUE;
Alexandre Julliard's avatar
Alexandre Julliard committed
628 629 630 631 632
}

/***********************************************************************
 *           WINPOS_ShowIconTitle
 */
633
static BOOL WINPOS_ShowIconTitle( HWND hwnd, BOOL bShow )
Alexandre Julliard's avatar
Alexandre Julliard committed
634
{
635
    if (!GetPropA( hwnd, "__wine_x11_managed" ))
Alexandre Julliard's avatar
Alexandre Julliard committed
636
    {
637 638
        WND *win = WIN_GetPtr( hwnd );
        HWND title = 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
639

640
	TRACE("%p %i\n", hwnd, (bShow != 0) );
Alexandre Julliard's avatar
Alexandre Julliard committed
641

642 643 644 645
        if (!win || win == WND_OTHER_PROCESS || win == WND_DESKTOP) return FALSE;
        title = win->icon_title;
        WIN_ReleasePtr( win );

Alexandre Julliard's avatar
Alexandre Julliard committed
646 647
	if( bShow )
        {
648 649 650 651 652 653 654 655 656 657 658
            if (!title)
            {
                title = ICONTITLE_Create( hwnd );
                if (!(win = WIN_GetPtr( hwnd )) || win == WND_OTHER_PROCESS)
                {
                    DestroyWindow( title );
                    return FALSE;
                }
                win->icon_title = title;
                WIN_ReleasePtr( win );
            }
659 660
            if (!IsWindowVisible(title))
            {
661
                SendMessageW( title, WM_SHOWWINDOW, TRUE, 0 );
662 663 664
                SetWindowPos( title, 0, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOMOVE |
                              SWP_NOACTIVATE | SWP_NOZORDER | SWP_SHOWWINDOW );
            }
Alexandre Julliard's avatar
Alexandre Julliard committed
665
	}
666
	else if (title) ShowWindow( title, SW_HIDE );
Alexandre Julliard's avatar
Alexandre Julliard committed
667 668 669 670 671 672 673 674 675
    }
    return FALSE;
}

/*******************************************************************
 *           WINPOS_GetMinMaxInfo
 *
 * Get the minimized and maximized information for a window.
 */
676
void WINPOS_GetMinMaxInfo( HWND hwnd, POINT *maxSize, POINT *maxPos,
677
			   POINT *minTrack, POINT *maxTrack )
Alexandre Julliard's avatar
Alexandre Julliard committed
678
{
679
    MINMAXINFO MinMax;
680
    HMONITOR monitor;
681
    INT xinc, yinc;
682
    LONG style = GetWindowLongW( hwnd, GWL_STYLE );
683
    LONG adjustedStyle;
684
    LONG exstyle = GetWindowLongW( hwnd, GWL_EXSTYLE );
685
    RECT rc;
686
    WND *win;
Alexandre Julliard's avatar
Alexandre Julliard committed
687

Alexandre Julliard's avatar
Alexandre Julliard committed
688
    /* Compute default values */
Alexandre Julliard's avatar
Alexandre Julliard committed
689

690 691 692 693
    GetWindowRect(hwnd, &rc);
    MinMax.ptReserved.x = rc.left;
    MinMax.ptReserved.y = rc.top;

694 695 696 697
    if ((style & WS_CAPTION) == WS_CAPTION)
        adjustedStyle = style & ~WS_BORDER; /* WS_CAPTION = WS_DLGFRAME | WS_BORDER */
    else
        adjustedStyle = style;
698

699 700
    GetClientRect(GetAncestor(hwnd,GA_PARENT), &rc);
    AdjustWindowRectEx(&rc, adjustedStyle, ((style & WS_POPUP) && GetMenu(hwnd)), exstyle);
701

702 703 704 705 706 707
    xinc = -rc.left;
    yinc = -rc.top;

    MinMax.ptMaxSize.x = rc.right - rc.left;
    MinMax.ptMaxSize.y = rc.bottom - rc.top;
    if (style & (WS_DLGFRAME | WS_BORDER))
708
    {
709 710
        MinMax.ptMinTrackSize.x = GetSystemMetrics(SM_CXMINTRACK);
        MinMax.ptMinTrackSize.y = GetSystemMetrics(SM_CYMINTRACK);
Alexandre Julliard's avatar
Alexandre Julliard committed
711
    }
712 713 714 715 716 717 718
    else
    {
        MinMax.ptMinTrackSize.x = 2 * xinc;
        MinMax.ptMinTrackSize.y = 2 * yinc;
    }
    MinMax.ptMaxTrackSize.x = GetSystemMetrics(SM_CXMAXTRACK);
    MinMax.ptMaxTrackSize.y = GetSystemMetrics(SM_CYMAXTRACK);
719 720
    MinMax.ptMaxPosition.x = -xinc;
    MinMax.ptMaxPosition.y = -yinc;
721

722
    if ((win = WIN_GetPtr( hwnd )) && win != WND_DESKTOP && win != WND_OTHER_PROCESS)
723
    {
724 725
        if (!EMPTYPOINT(win->max_pos)) MinMax.ptMaxPosition = win->max_pos;
        WIN_ReleasePtr( win );
Alexandre Julliard's avatar
Alexandre Julliard committed
726 727
    }

728
    SendMessageW( hwnd, WM_GETMINMAXINFO, 0, (LPARAM)&MinMax );
Alexandre Julliard's avatar
Alexandre Julliard committed
729

730 731 732 733
    /* if the app didn't change the values, adapt them for the current monitor */

    if ((monitor = MonitorFromWindow( hwnd, MONITOR_DEFAULTTOPRIMARY )))
    {
734
        RECT rc_work;
735 736 737 738 739
        MONITORINFO mon_info;

        mon_info.cbSize = sizeof(mon_info);
        GetMonitorInfoW( monitor, &mon_info );

740 741 742 743 744 745 746 747
        rc_work = mon_info.rcMonitor;

        if (style & WS_MAXIMIZEBOX)
        {
            if ((style & WS_CAPTION) == WS_CAPTION || !(style & (WS_CHILD | WS_POPUP)))
                rc_work = mon_info.rcWork;
        }

748 749
        if (MinMax.ptMaxSize.x == GetSystemMetrics(SM_CXSCREEN) + 2 * xinc &&
            MinMax.ptMaxSize.y == GetSystemMetrics(SM_CYSCREEN) + 2 * yinc)
750
        {
751 752
            MinMax.ptMaxSize.x = (rc_work.right - rc_work.left) + 2 * xinc;
            MinMax.ptMaxSize.y = (rc_work.bottom - rc_work.top) + 2 * yinc;
753 754 755
        }
        if (MinMax.ptMaxPosition.x == -xinc && MinMax.ptMaxPosition.y == -yinc)
        {
756 757
            MinMax.ptMaxPosition.x = rc_work.left - xinc;
            MinMax.ptMaxPosition.y = rc_work.top - yinc;
758 759 760
        }
    }

Alexandre Julliard's avatar
Alexandre Julliard committed
761 762
      /* Some sanity checks */

763
    TRACE("%d %d / %d %d / %d %d / %d %d\n",
Alexandre Julliard's avatar
Alexandre Julliard committed
764 765 766 767
                      MinMax.ptMaxSize.x, MinMax.ptMaxSize.y,
                      MinMax.ptMaxPosition.x, MinMax.ptMaxPosition.y,
                      MinMax.ptMaxTrackSize.x, MinMax.ptMaxTrackSize.y,
                      MinMax.ptMinTrackSize.x, MinMax.ptMinTrackSize.y);
768
    MinMax.ptMaxTrackSize.x = max( MinMax.ptMaxTrackSize.x,
Alexandre Julliard's avatar
Alexandre Julliard committed
769
                                   MinMax.ptMinTrackSize.x );
770
    MinMax.ptMaxTrackSize.y = max( MinMax.ptMaxTrackSize.y,
Alexandre Julliard's avatar
Alexandre Julliard committed
771 772 773 774 775 776
                                   MinMax.ptMinTrackSize.y );

    if (maxSize) *maxSize = MinMax.ptMaxSize;
    if (maxPos) *maxPos = MinMax.ptMaxPosition;
    if (minTrack) *minTrack = MinMax.ptMinTrackSize;
    if (maxTrack) *maxTrack = MinMax.ptMaxTrackSize;
Alexandre Julliard's avatar
Alexandre Julliard committed
777 778
}

779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892

/***********************************************************************
 *           WINPOS_FindIconPos
 *
 * Find a suitable place for an iconic window.
 */
static POINT WINPOS_FindIconPos( HWND hwnd, POINT pt )
{
    RECT rect, rectParent;
    HWND parent, child;
    HRGN hrgn, tmp;
    int xspacing, yspacing;

    parent = GetAncestor( hwnd, GA_PARENT );
    GetClientRect( parent, &rectParent );
    if ((pt.x >= rectParent.left) && (pt.x + GetSystemMetrics(SM_CXICON) < rectParent.right) &&
        (pt.y >= rectParent.top) && (pt.y + GetSystemMetrics(SM_CYICON) < rectParent.bottom))
        return pt;  /* The icon already has a suitable position */

    xspacing = GetSystemMetrics(SM_CXICONSPACING);
    yspacing = GetSystemMetrics(SM_CYICONSPACING);

    /* Check if another icon already occupies this spot */
    /* FIXME: this is completely inefficient */

    hrgn = CreateRectRgn( 0, 0, 0, 0 );
    tmp = CreateRectRgn( 0, 0, 0, 0 );
    for (child = GetWindow( parent, GW_HWNDFIRST ); child; child = GetWindow( child, GW_HWNDNEXT ))
    {
        WND *childPtr;
        if (child == hwnd) continue;
        if ((GetWindowLongW( child, GWL_STYLE ) & (WS_VISIBLE|WS_MINIMIZE)) != (WS_VISIBLE|WS_MINIMIZE))
            continue;
        if (!(childPtr = WIN_GetPtr( child )) || childPtr == WND_OTHER_PROCESS)
            continue;
        SetRectRgn( tmp, childPtr->rectWindow.left, childPtr->rectWindow.top,
                    childPtr->rectWindow.right, childPtr->rectWindow.bottom );
        CombineRgn( hrgn, hrgn, tmp, RGN_OR );
        WIN_ReleasePtr( childPtr );
    }
    DeleteObject( tmp );

    for (rect.bottom = rectParent.bottom; rect.bottom >= yspacing; rect.bottom -= yspacing)
    {
        for (rect.left = rectParent.left; rect.left <= rectParent.right - xspacing; rect.left += xspacing)
        {
            rect.right = rect.left + xspacing;
            rect.top = rect.bottom - yspacing;
            if (!RectInRegion( hrgn, &rect ))
            {
                /* No window was found, so it's OK for us */
                pt.x = rect.left + (xspacing - GetSystemMetrics(SM_CXICON)) / 2;
                pt.y = rect.top + (yspacing - GetSystemMetrics(SM_CYICON)) / 2;
                DeleteObject( hrgn );
                return pt;
            }
        }
    }
    DeleteObject( hrgn );
    pt.x = pt.y = 0;
    return pt;
}


/***********************************************************************
 *           WINPOS_MinMaximize
 */
UINT WINPOS_MinMaximize( HWND hwnd, UINT cmd, LPRECT rect )
{
    WND *wndPtr;
    UINT swpFlags = 0;
    POINT size;
    LONG old_style;
    WINDOWPLACEMENT wpl;

    TRACE("%p %u\n", hwnd, cmd );

    wpl.length = sizeof(wpl);
    GetWindowPlacement( hwnd, &wpl );

    if (HOOK_CallHooks( WH_CBT, HCBT_MINMAX, (WPARAM)hwnd, cmd, TRUE ))
        return SWP_NOSIZE | SWP_NOMOVE;

    if (IsIconic( hwnd ))
    {
        switch (cmd)
        {
        case SW_SHOWMINNOACTIVE:
        case SW_SHOWMINIMIZED:
        case SW_FORCEMINIMIZE:
        case SW_MINIMIZE:
            return SWP_NOSIZE | SWP_NOMOVE;
        }
        if (!SendMessageW( hwnd, WM_QUERYOPEN, 0, 0 )) return SWP_NOSIZE | SWP_NOMOVE;
        swpFlags |= SWP_NOCOPYBITS;
    }

    switch( cmd )
    {
    case SW_SHOWMINNOACTIVE:
    case SW_SHOWMINIMIZED:
    case SW_FORCEMINIMIZE:
    case SW_MINIMIZE:
        if (!(wndPtr = WIN_GetPtr( hwnd )) || wndPtr == WND_OTHER_PROCESS) return 0;
        if( wndPtr->dwStyle & WS_MAXIMIZE) wndPtr->flags |= WIN_RESTORE_MAX;
        else wndPtr->flags &= ~WIN_RESTORE_MAX;
        WIN_ReleasePtr( wndPtr );

        old_style = WIN_SetStyle( hwnd, WS_MINIMIZE, WS_MAXIMIZE );

        wpl.ptMinPosition = WINPOS_FindIconPos( hwnd, wpl.ptMinPosition );

        if (!(old_style & WS_MINIMIZE)) swpFlags |= SWP_STATECHANGED;
        SetRect( rect, wpl.ptMinPosition.x, wpl.ptMinPosition.y,
893 894
                 wpl.ptMinPosition.x + GetSystemMetrics(SM_CXICON),
                 wpl.ptMinPosition.y + GetSystemMetrics(SM_CYICON) );
895 896 897 898 899 900 901 902 903 904 905 906 907
        swpFlags |= SWP_NOCOPYBITS;
        break;

    case SW_MAXIMIZE:
        old_style = GetWindowLongW( hwnd, GWL_STYLE );
        if ((old_style & WS_MAXIMIZE) && (old_style & WS_VISIBLE)) return SWP_NOSIZE | SWP_NOMOVE;

        WINPOS_GetMinMaxInfo( hwnd, &size, &wpl.ptMaxPosition, NULL, NULL );

        old_style = WIN_SetStyle( hwnd, WS_MAXIMIZE, WS_MINIMIZE );
        if (old_style & WS_MINIMIZE) WINPOS_ShowIconTitle( hwnd, FALSE );

        if (!(old_style & WS_MAXIMIZE)) swpFlags |= SWP_STATECHANGED;
908 909
        SetRect( rect, wpl.ptMaxPosition.x, wpl.ptMaxPosition.y,
                 wpl.ptMaxPosition.x +  size.x, wpl.ptMaxPosition.y + size.y );
910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930
        break;

    case SW_SHOWNOACTIVATE:
    case SW_SHOWNORMAL:
    case SW_RESTORE:
        old_style = WIN_SetStyle( hwnd, 0, WS_MINIMIZE | WS_MAXIMIZE );
        if (old_style & WS_MINIMIZE)
        {
            BOOL restore_max;

            WINPOS_ShowIconTitle( hwnd, FALSE );

            if (!(wndPtr = WIN_GetPtr( hwnd )) || wndPtr == WND_OTHER_PROCESS) return 0;
            restore_max = (wndPtr->flags & WIN_RESTORE_MAX) != 0;
            WIN_ReleasePtr( wndPtr );
            if (restore_max)
            {
                /* Restore to maximized position */
                WINPOS_GetMinMaxInfo( hwnd, &size, &wpl.ptMaxPosition, NULL, NULL);
                WIN_SetStyle( hwnd, WS_MAXIMIZE, 0 );
                swpFlags |= SWP_STATECHANGED;
931 932
                SetRect( rect, wpl.ptMaxPosition.x, wpl.ptMaxPosition.y,
                         wpl.ptMaxPosition.x + size.x, wpl.ptMaxPosition.y + size.y );
933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972
                break;
            }
        }
        else if (!(old_style & WS_MAXIMIZE)) break;

        swpFlags |= SWP_STATECHANGED;

        /* Restore to normal position */

        *rect = wpl.rcNormalPosition;
        break;
    }

    return swpFlags;
}


/***********************************************************************
 *              show_window
 *
 * Implementation of ShowWindow and ShowWindowAsync.
 */
static BOOL show_window( HWND hwnd, INT cmd )
{
    WND *wndPtr;
    HWND parent;
    LONG style = GetWindowLongW( hwnd, GWL_STYLE );
    BOOL wasVisible = (style & WS_VISIBLE) != 0;
    BOOL showFlag = TRUE;
    RECT newPos = {0, 0, 0, 0};
    UINT swp = 0;

    TRACE("hwnd=%p, cmd=%d, wasVisible %d\n", hwnd, cmd, wasVisible);

    switch(cmd)
    {
        case SW_HIDE:
            if (!wasVisible) return FALSE;
            showFlag = FALSE;
            swp |= SWP_HIDEWINDOW | SWP_NOSIZE | SWP_NOMOVE;
973
            if (style & WS_CHILD) swp |= SWP_NOACTIVATE | SWP_NOZORDER;
974 975 976 977 978
	    break;

	case SW_SHOWMINNOACTIVE:
        case SW_MINIMIZE:
        case SW_FORCEMINIMIZE: /* FIXME: Does not work if thread is hung. */
979
            swp |= SWP_NOACTIVATE | SWP_NOZORDER;
980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023
            /* fall through */
	case SW_SHOWMINIMIZED:
            swp |= SWP_SHOWWINDOW | SWP_FRAMECHANGED;
            swp |= WINPOS_MinMaximize( hwnd, cmd, &newPos );
            if ((style & WS_MINIMIZE) && wasVisible) return TRUE;
	    break;

	case SW_SHOWMAXIMIZED: /* same as SW_MAXIMIZE */
            if (!wasVisible) swp |= SWP_SHOWWINDOW;
            swp |= SWP_FRAMECHANGED;
            swp |= WINPOS_MinMaximize( hwnd, SW_MAXIMIZE, &newPos );
            if ((style & WS_MAXIMIZE) && wasVisible) return TRUE;
            break;

	case SW_SHOWNA:
            swp |= SWP_NOACTIVATE | SWP_SHOWWINDOW | SWP_NOSIZE | SWP_NOMOVE;
            if (style & WS_CHILD) swp |= SWP_NOZORDER;
            break;
	case SW_SHOW:
            if (wasVisible) return TRUE;
	    swp |= SWP_SHOWWINDOW | SWP_NOSIZE | SWP_NOMOVE;
            if (style & WS_CHILD) swp |= SWP_NOACTIVATE | SWP_NOZORDER;
	    break;

	case SW_SHOWNOACTIVATE:
            swp |= SWP_NOACTIVATE | SWP_NOZORDER;
            /* fall through */
	case SW_RESTORE:
            /* fall through */
	case SW_SHOWNORMAL:  /* same as SW_NORMAL: */
	case SW_SHOWDEFAULT: /* FIXME: should have its own handler */
            if (!wasVisible) swp |= SWP_SHOWWINDOW;
            if (style & (WS_MINIMIZE | WS_MAXIMIZE))
            {
                swp |= SWP_FRAMECHANGED;
                swp |= WINPOS_MinMaximize( hwnd, cmd, &newPos );
            }
            else
            {
                if (wasVisible) return TRUE;
                swp |= SWP_NOSIZE | SWP_NOMOVE;
            }
            if (style & WS_CHILD && !(swp & SWP_STATECHANGED)) swp |= SWP_NOACTIVATE | SWP_NOZORDER;
	    break;
1024 1025
        default:
            return wasVisible;
1026 1027 1028 1029 1030 1031 1032 1033
    }

    if ((showFlag != wasVisible || cmd == SW_SHOWNA) && cmd != SW_SHOWMAXIMIZED && !(swp & SWP_STATECHANGED))
    {
        SendMessageW( hwnd, WM_SHOWWINDOW, showFlag, 0 );
        if (!IsWindow( hwnd )) return wasVisible;
    }

1034 1035
    swp = USER_Driver->pShowWindow( hwnd, cmd, &newPos, swp );

1036 1037 1038 1039 1040 1041 1042 1043 1044
    parent = GetAncestor( hwnd, GA_PARENT );
    if (parent && !IsWindowVisible( parent ) && !(swp & SWP_STATECHANGED))
    {
        /* if parent is not visible simply toggle WS_VISIBLE and return */
        if (showFlag) WIN_SetStyle( hwnd, WS_VISIBLE, 0 );
        else WIN_SetStyle( hwnd, 0, WS_VISIBLE );
    }
    else
        SetWindowPos( hwnd, HWND_TOP, newPos.left, newPos.top,
1045
                      newPos.right - newPos.left, newPos.bottom - newPos.top, swp );
1046 1047 1048 1049 1050

    if (cmd == SW_HIDE)
    {
        HWND hFocus;

1051 1052
        WINPOS_ShowIconTitle( hwnd, FALSE );

1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068
        /* FIXME: This will cause the window to be activated irrespective
         * of whether it is owned by the same thread. Has to be done
         * asynchronously.
         */

        if (hwnd == GetActiveWindow())
            WINPOS_ActivateOtherWindow(hwnd);

        /* Revert focus to parent */
        hFocus = GetFocus();
        if (hwnd == hFocus || IsChild(hwnd, hFocus))
        {
            HWND parent = GetAncestor(hwnd, GA_PARENT);
            if (parent == GetDesktopWindow()) parent = 0;
            SetFocus(parent);
        }
1069
        return wasVisible;
1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080
    }

    if (IsIconic(hwnd)) WINPOS_ShowIconTitle( hwnd, TRUE );

    if (!(wndPtr = WIN_GetPtr( hwnd )) || wndPtr == WND_OTHER_PROCESS) return wasVisible;

    if (wndPtr->flags & WIN_NEED_SIZE)
    {
        /* should happen only in CreateWindowEx() */
	int wParam = SIZE_RESTORED;
        RECT client = wndPtr->rectClient;
1081
        LPARAM lparam = MAKELONG( client.right - client.left, client.bottom - client.top );
1082 1083 1084

	wndPtr->flags &= ~WIN_NEED_SIZE;
	if (wndPtr->dwStyle & WS_MAXIMIZE) wParam = SIZE_MAXIMIZED;
1085 1086 1087 1088 1089
        else if (wndPtr->dwStyle & WS_MINIMIZE)
        {
            wParam = SIZE_MINIMIZED;
            lparam = 0;
        }
1090 1091
        WIN_ReleasePtr( wndPtr );

1092
        SendMessageW( hwnd, WM_SIZE, wParam, lparam );
1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103
        SendMessageW( hwnd, WM_MOVE, 0, MAKELONG( client.left, client.top ));
    }
    else WIN_ReleasePtr( wndPtr );

    /* if previous state was minimized Windows sets focus to the window */
    if (style & WS_MINIMIZE) SetFocus( hwnd );

    return wasVisible;
}


Alexandre Julliard's avatar
Alexandre Julliard committed
1104
/***********************************************************************
1105
 *		ShowWindowAsync (USER32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
1106 1107 1108 1109
 *
 * doesn't wait; returns immediately.
 * used by threads to toggle windows in other (possibly hanging) threads
 */
1110
BOOL WINAPI ShowWindowAsync( HWND hwnd, INT cmd )
Alexandre Julliard's avatar
Alexandre Julliard committed
1111
{
1112 1113
    HWND full_handle;

1114 1115 1116 1117 1118 1119
    if (is_broadcast(hwnd))
    {
        SetLastError( ERROR_INVALID_PARAMETER );
        return FALSE;
    }

1120
    if ((full_handle = WIN_IsCurrentThread( hwnd )))
1121
        return show_window( full_handle, cmd );
1122

1123
    return SendNotifyMessageW( hwnd, WM_WINE_SHOWWINDOW, cmd, 0 );
Alexandre Julliard's avatar
Alexandre Julliard committed
1124 1125 1126
}


Alexandre Julliard's avatar
Alexandre Julliard committed
1127
/***********************************************************************
1128
 *		ShowWindow (USER32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
1129
 */
1130 1131
BOOL WINAPI ShowWindow( HWND hwnd, INT cmd )
{
1132 1133
    HWND full_handle;

1134 1135 1136 1137 1138
    if (is_broadcast(hwnd))
    {
        SetLastError( ERROR_INVALID_PARAMETER );
        return FALSE;
    }
1139
    if ((full_handle = WIN_IsCurrentThread( hwnd )))
1140
        return show_window( full_handle, cmd );
1141

1142
    return SendMessageW( hwnd, WM_WINE_SHOWWINDOW, cmd, 0 );
Alexandre Julliard's avatar
Alexandre Julliard committed
1143 1144 1145
}


Alexandre Julliard's avatar
Alexandre Julliard committed
1146
/***********************************************************************
1147
 *		GetInternalWindowPos (USER32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
1148
 */
1149 1150
UINT WINAPI GetInternalWindowPos( HWND hwnd, LPRECT rectWnd,
                                      LPPOINT ptIcon )
Alexandre Julliard's avatar
Alexandre Julliard committed
1151
{
1152 1153
    WINDOWPLACEMENT wndpl;
    if (GetWindowPlacement( hwnd, &wndpl ))
Alexandre Julliard's avatar
Alexandre Julliard committed
1154 1155 1156 1157 1158 1159
    {
	if (rectWnd) *rectWnd = wndpl.rcNormalPosition;
	if (ptIcon)  *ptIcon = wndpl.ptMinPosition;
	return wndpl.showCmd;
    }
    return 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
1160 1161
}

Alexandre Julliard's avatar
Alexandre Julliard committed
1162 1163

/***********************************************************************
1164
 *		GetWindowPlacement (USER32.@)
1165 1166 1167
 *
 * Win95:
 * Fails if wndpl->length of Win95 (!) apps is invalid.
Alexandre Julliard's avatar
Alexandre Julliard committed
1168
 */
1169
BOOL WINAPI GetWindowPlacement( HWND hwnd, WINDOWPLACEMENT *wndpl )
Alexandre Julliard's avatar
Alexandre Julliard committed
1170
{
1171
    WND *pWnd = WIN_GetPtr( hwnd );
1172

1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186
    if (!pWnd) return FALSE;

    if (pWnd == WND_DESKTOP)
    {
        wndpl->length  = sizeof(*wndpl);
        wndpl->showCmd = SW_SHOWNORMAL;
        wndpl->flags = 0;
        wndpl->ptMinPosition.x = -1;
        wndpl->ptMinPosition.y = -1;
        wndpl->ptMaxPosition.x = -1;
        wndpl->ptMaxPosition.y = -1;
        GetWindowRect( hwnd, &wndpl->rcNormalPosition );
        return TRUE;
    }
1187 1188 1189 1190 1191
    if (pWnd == WND_OTHER_PROCESS)
    {
        if (IsWindow( hwnd )) FIXME( "not supported on other process window %p\n", hwnd );
        return FALSE;
    }
1192

1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208
    /* update the placement according to the current style */
    if (pWnd->dwStyle & WS_MINIMIZE)
    {
        pWnd->min_pos.x = pWnd->rectWindow.left;
        pWnd->min_pos.y = pWnd->rectWindow.top;
    }
    else if (pWnd->dwStyle & WS_MAXIMIZE)
    {
        pWnd->max_pos.x = pWnd->rectWindow.left;
        pWnd->max_pos.y = pWnd->rectWindow.top;
    }
    else
    {
        pWnd->normal_rect = pWnd->rectWindow;
    }

1209 1210 1211 1212 1213 1214 1215 1216 1217
    wndpl->length  = sizeof(*wndpl);
    if( pWnd->dwStyle & WS_MINIMIZE )
        wndpl->showCmd = SW_SHOWMINIMIZED;
    else
        wndpl->showCmd = ( pWnd->dwStyle & WS_MAXIMIZE ) ? SW_SHOWMAXIMIZED : SW_SHOWNORMAL ;
    if( pWnd->flags & WIN_RESTORE_MAX )
        wndpl->flags = WPF_RESTORETOMAXIMIZED;
    else
        wndpl->flags = 0;
1218 1219 1220
    wndpl->ptMinPosition    = pWnd->min_pos;
    wndpl->ptMaxPosition    = pWnd->max_pos;
    wndpl->rcNormalPosition = pWnd->normal_rect;
1221
    WIN_ReleasePtr( pWnd );
1222 1223 1224 1225 1226

    TRACE( "%p: returning min %d,%d max %d,%d normal %s\n",
           hwnd, wndpl->ptMinPosition.x, wndpl->ptMinPosition.y,
           wndpl->ptMaxPosition.x, wndpl->ptMaxPosition.y,
           wine_dbgstr_rect(&wndpl->rcNormalPosition) );
1227
    return TRUE;
Alexandre Julliard's avatar
Alexandre Julliard committed
1228 1229
}

1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271
/* make sure the specified rect is visible on screen */
static void make_rect_onscreen( RECT *rect )
{
    MONITORINFO info;
    HMONITOR monitor = MonitorFromRect( rect, MONITOR_DEFAULTTONEAREST );

    info.cbSize = sizeof(info);
    if (!monitor || !GetMonitorInfoW( monitor, &info )) return;
    /* FIXME: map coordinates from rcWork to rcMonitor */
    if (rect->right <= info.rcWork.left)
    {
        rect->right += info.rcWork.left - rect->left;
        rect->left = info.rcWork.left;
    }
    else if (rect->left >= info.rcWork.right)
    {
        rect->left += info.rcWork.right - rect->right;
        rect->right = info.rcWork.right;
    }
    if (rect->bottom <= info.rcWork.top)
    {
        rect->bottom += info.rcWork.top - rect->top;
        rect->top = info.rcWork.top;
    }
    else if (rect->top >= info.rcWork.bottom)
    {
        rect->top += info.rcWork.bottom - rect->bottom;
        rect->bottom = info.rcWork.bottom;
    }
}

/* make sure the specified point is visible on screen */
static void make_point_onscreen( POINT *pt )
{
    RECT rect;

    SetRect( &rect, pt->x, pt->y, pt->x + 1, pt->y + 1 );
    make_rect_onscreen( &rect );
    pt->x = rect.left;
    pt->y = rect.top;
}

Alexandre Julliard's avatar
Alexandre Julliard committed
1272 1273

/***********************************************************************
Alexandre Julliard's avatar
Alexandre Julliard committed
1274
 *           WINPOS_SetPlacement
Alexandre Julliard's avatar
Alexandre Julliard committed
1275
 */
1276
static BOOL WINPOS_SetPlacement( HWND hwnd, const WINDOWPLACEMENT *wndpl, UINT flags )
Alexandre Julliard's avatar
Alexandre Julliard committed
1277
{
1278 1279
    DWORD style;
    WND *pWnd = WIN_GetPtr( hwnd );
1280
    WINDOWPLACEMENT wp = *wndpl;
1281

1282 1283 1284
    if (flags & PLACE_MIN) make_point_onscreen( &wp.ptMinPosition );
    if (flags & PLACE_MAX) make_point_onscreen( &wp.ptMaxPosition );
    if (flags & PLACE_RECT) make_rect_onscreen( &wp.rcNormalPosition );
1285

1286
    TRACE( "%p: setting min %d,%d max %d,%d normal %s flags %x ajusted to min %d,%d max %d,%d normal %s\n",
1287 1288
           hwnd, wndpl->ptMinPosition.x, wndpl->ptMinPosition.y,
           wndpl->ptMaxPosition.x, wndpl->ptMaxPosition.y,
1289 1290 1291
           wine_dbgstr_rect(&wndpl->rcNormalPosition), flags,
           wp.ptMinPosition.x, wp.ptMinPosition.y, wp.ptMaxPosition.x, wp.ptMaxPosition.y,
           wine_dbgstr_rect(&wp.rcNormalPosition) );
1292

1293
    if (!pWnd || pWnd == WND_OTHER_PROCESS || pWnd == WND_DESKTOP) return FALSE;
1294

1295 1296 1297
    if( flags & PLACE_MIN ) pWnd->min_pos = wp.ptMinPosition;
    if( flags & PLACE_MAX ) pWnd->max_pos = wp.ptMaxPosition;
    if( flags & PLACE_RECT) pWnd->normal_rect = wp.rcNormalPosition;
1298

1299
    style = pWnd->dwStyle;
Alexandre Julliard's avatar
Alexandre Julliard committed
1300

1301 1302 1303 1304
    WIN_ReleasePtr( pWnd );

    if( style & WS_MINIMIZE )
    {
1305 1306 1307 1308 1309 1310
        if (flags & PLACE_MIN)
        {
            WINPOS_ShowIconTitle( hwnd, FALSE );
            SetWindowPos( hwnd, 0, wp.ptMinPosition.x, wp.ptMinPosition.y, 0, 0,
                          SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE );
        }
1311 1312 1313
    }
    else if( style & WS_MAXIMIZE )
    {
1314 1315 1316
        if (flags & PLACE_MAX)
            SetWindowPos( hwnd, 0, wp.ptMaxPosition.x, wp.ptMaxPosition.y, 0, 0,
                          SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE );
1317 1318
    }
    else if( flags & PLACE_RECT )
1319 1320 1321
        SetWindowPos( hwnd, 0, wp.rcNormalPosition.left, wp.rcNormalPosition.top,
                      wp.rcNormalPosition.right - wp.rcNormalPosition.left,
                      wp.rcNormalPosition.bottom - wp.rcNormalPosition.top,
1322 1323 1324 1325 1326 1327 1328 1329 1330 1331
                      SWP_NOZORDER | SWP_NOACTIVATE );

    ShowWindow( hwnd, wndpl->showCmd );

    if (IsIconic( hwnd ))
    {
        if (GetWindowLongW( hwnd, GWL_STYLE ) & WS_VISIBLE) WINPOS_ShowIconTitle( hwnd, TRUE );

        /* SDK: ...valid only the next time... */
        if( wndpl->flags & WPF_RESTORETOMAXIMIZED )
1332
        {
1333 1334 1335 1336 1337 1338
            pWnd = WIN_GetPtr( hwnd );
            if (pWnd && pWnd != WND_OTHER_PROCESS)
            {
                pWnd->flags |= WIN_RESTORE_MAX;
                WIN_ReleasePtr( pWnd );
            }
1339
        }
Alexandre Julliard's avatar
Alexandre Julliard committed
1340
    }
1341
    return TRUE;
Alexandre Julliard's avatar
Alexandre Julliard committed
1342 1343 1344
}


Alexandre Julliard's avatar
Alexandre Julliard committed
1345
/***********************************************************************
1346
 *		SetWindowPlacement (USER32.@)
1347 1348 1349
 *
 * Win95:
 * Fails if wndpl->length of Win95 (!) apps is invalid.
Alexandre Julliard's avatar
Alexandre Julliard committed
1350
 */
1351
BOOL WINAPI SetWindowPlacement( HWND hwnd, const WINDOWPLACEMENT *wpl )
Alexandre Julliard's avatar
Alexandre Julliard committed
1352
{
1353
    UINT flags = PLACE_MAX | PLACE_RECT;
1354
    if (!wpl) return FALSE;
1355 1356
    if (wpl->flags & WPF_SETMINPOSITION) flags |= PLACE_MIN;
    return WINPOS_SetPlacement( hwnd, wpl, flags );
Alexandre Julliard's avatar
Alexandre Julliard committed
1357 1358 1359
}


1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383
/***********************************************************************
 *		AnimateWindow (USER32.@)
 *		Shows/Hides a window with an animation
 *		NO ANIMATION YET
 */
BOOL WINAPI AnimateWindow(HWND hwnd, DWORD dwTime, DWORD dwFlags)
{
	FIXME("partial stub\n");

	/* If trying to show/hide and it's already   *
	 * shown/hidden or invalid window, fail with *
	 * invalid parameter                         */
	if(!IsWindow(hwnd) ||
	   (IsWindowVisible(hwnd) && !(dwFlags & AW_HIDE)) ||
	   (!IsWindowVisible(hwnd) && (dwFlags & AW_HIDE)))
	{
		SetLastError(ERROR_INVALID_PARAMETER);
		return FALSE;
	}

	ShowWindow(hwnd, (dwFlags & AW_HIDE) ? SW_HIDE : ((dwFlags & AW_ACTIVATE) ? SW_SHOW : SW_SHOWNA));

	return TRUE;
}
Alexandre Julliard's avatar
Alexandre Julliard committed
1384 1385

/***********************************************************************
1386
 *		SetInternalWindowPos (USER32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
1387
 */
1388 1389
void WINAPI SetInternalWindowPos( HWND hwnd, UINT showCmd,
                                    LPRECT rect, LPPOINT pt )
Alexandre Julliard's avatar
Alexandre Julliard committed
1390
{
1391 1392
    WINDOWPLACEMENT wndpl;
    UINT flags;
Alexandre Julliard's avatar
Alexandre Julliard committed
1393

1394 1395 1396
    wndpl.length  = sizeof(wndpl);
    wndpl.showCmd = showCmd;
    wndpl.flags = flags = 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
1397

1398 1399 1400 1401 1402 1403 1404 1405 1406 1407
    if( pt )
    {
        flags |= PLACE_MIN;
        wndpl.flags |= WPF_SETMINPOSITION;
        wndpl.ptMinPosition = *pt;
    }
    if( rect )
    {
        flags |= PLACE_RECT;
        wndpl.rcNormalPosition = *rect;
Alexandre Julliard's avatar
Alexandre Julliard committed
1408
    }
1409
    WINPOS_SetPlacement( hwnd, &wndpl, flags );
Alexandre Julliard's avatar
Alexandre Julliard committed
1410 1411
}

1412

Alexandre Julliard's avatar
Alexandre Julliard committed
1413
/*******************************************************************
1414
 *         can_activate_window
Alexandre Julliard's avatar
Alexandre Julliard committed
1415
 *
1416
 * Check if we can activate the specified window.
Alexandre Julliard's avatar
Alexandre Julliard committed
1417
 */
1418
static BOOL can_activate_window( HWND hwnd )
Alexandre Julliard's avatar
Alexandre Julliard committed
1419
{
1420
    LONG style;
1421

1422 1423 1424 1425 1426
    if (!hwnd) return FALSE;
    style = GetWindowLongW( hwnd, GWL_STYLE );
    if (!(style & WS_VISIBLE)) return FALSE;
    if ((style & (WS_POPUP|WS_CHILD)) == WS_CHILD) return FALSE;
    return !(style & WS_DISABLED);
Alexandre Julliard's avatar
Alexandre Julliard committed
1427 1428
}

1429

Alexandre Julliard's avatar
Alexandre Julliard committed
1430 1431 1432
/*******************************************************************
 *         WINPOS_ActivateOtherWindow
 *
Alexandre Julliard's avatar
Alexandre Julliard committed
1433
 *  Activates window other than pWnd.
Alexandre Julliard's avatar
Alexandre Julliard committed
1434
 */
1435
void WINPOS_ActivateOtherWindow(HWND hwnd)
Alexandre Julliard's avatar
Alexandre Julliard committed
1436
{
1437
    HWND hwndTo, fg;
1438

1439
    if ((GetWindowLongW( hwnd, GWL_STYLE ) & WS_POPUP) && (hwndTo = GetWindow( hwnd, GW_OWNER )))
1440
    {
1441 1442
        hwndTo = GetAncestor( hwndTo, GA_ROOT );
        if (can_activate_window( hwndTo )) goto done;
1443
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
1444

1445 1446
    hwndTo = hwnd;
    for (;;)
1447
    {
1448 1449
        if (!(hwndTo = GetWindow( hwndTo, GW_HWNDNEXT ))) break;
        if (can_activate_window( hwndTo )) break;
1450
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
1451

1452 1453
 done:
    fg = GetForegroundWindow();
1454
    TRACE("win = %p fg = %p\n", hwndTo, fg);
1455 1456 1457 1458 1459
    if (!fg || (hwnd == fg))
    {
        if (SetForegroundWindow( hwndTo )) return;
    }
    if (!SetActiveWindow( hwndTo )) SetActiveWindow(0);
Alexandre Julliard's avatar
Alexandre Julliard committed
1460 1461 1462
}


Alexandre Julliard's avatar
Alexandre Julliard committed
1463
/***********************************************************************
1464
 *           WINPOS_HandleWindowPosChanging
Alexandre Julliard's avatar
Alexandre Julliard committed
1465 1466 1467
 *
 * Default handling for a WM_WINDOWPOSCHANGING. Called from DefWindowProc().
 */
1468
LONG WINPOS_HandleWindowPosChanging( HWND hwnd, WINDOWPOS *winpos )
Alexandre Julliard's avatar
Alexandre Julliard committed
1469
{
1470 1471
    POINT minTrack, maxTrack;
    LONG style = GetWindowLongW( hwnd, GWL_STYLE );
1472

Alexandre Julliard's avatar
Alexandre Julliard committed
1473
    if (winpos->flags & SWP_NOSIZE) return 0;
1474
    if ((style & WS_THICKFRAME) || ((style & (WS_POPUP | WS_CHILD)) == 0))
Alexandre Julliard's avatar
Alexandre Julliard committed
1475
    {
1476 1477 1478
	WINPOS_GetMinMaxInfo( hwnd, NULL, NULL, &minTrack, &maxTrack );
	if (winpos->cx > maxTrack.x) winpos->cx = maxTrack.x;
	if (winpos->cy > maxTrack.y) winpos->cy = maxTrack.y;
1479
	if (!(style & WS_MINIMIZE))
1480 1481 1482 1483
	{
	    if (winpos->cx < minTrack.x ) winpos->cx = minTrack.x;
	    if (winpos->cy < minTrack.y ) winpos->cy = minTrack.y;
	}
Alexandre Julliard's avatar
Alexandre Julliard committed
1484 1485 1486 1487
    }
    return 0;
}

Alexandre Julliard's avatar
Alexandre Julliard committed
1488

1489 1490 1491 1492 1493
/***********************************************************************
 *           dump_winpos_flags
 */
static void dump_winpos_flags(UINT flags)
{
1494 1495 1496 1497 1498
    static const DWORD dumped_flags = (SWP_NOSIZE | SWP_NOMOVE | SWP_NOZORDER | SWP_NOREDRAW |
                                       SWP_NOACTIVATE | SWP_FRAMECHANGED | SWP_SHOWWINDOW |
                                       SWP_HIDEWINDOW | SWP_NOCOPYBITS | SWP_NOOWNERZORDER |
                                       SWP_NOSENDCHANGING | SWP_DEFERERASE | SWP_ASYNCWINDOWPOS |
                                       SWP_NOCLIENTSIZE | SWP_NOCLIENTMOVE | SWP_STATECHANGED);
1499
    TRACE("flags:");
1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512
    if(flags & SWP_NOSIZE) TRACE(" SWP_NOSIZE");
    if(flags & SWP_NOMOVE) TRACE(" SWP_NOMOVE");
    if(flags & SWP_NOZORDER) TRACE(" SWP_NOZORDER");
    if(flags & SWP_NOREDRAW) TRACE(" SWP_NOREDRAW");
    if(flags & SWP_NOACTIVATE) TRACE(" SWP_NOACTIVATE");
    if(flags & SWP_FRAMECHANGED) TRACE(" SWP_FRAMECHANGED");
    if(flags & SWP_SHOWWINDOW) TRACE(" SWP_SHOWWINDOW");
    if(flags & SWP_HIDEWINDOW) TRACE(" SWP_HIDEWINDOW");
    if(flags & SWP_NOCOPYBITS) TRACE(" SWP_NOCOPYBITS");
    if(flags & SWP_NOOWNERZORDER) TRACE(" SWP_NOOWNERZORDER");
    if(flags & SWP_NOSENDCHANGING) TRACE(" SWP_NOSENDCHANGING");
    if(flags & SWP_DEFERERASE) TRACE(" SWP_DEFERERASE");
    if(flags & SWP_ASYNCWINDOWPOS) TRACE(" SWP_ASYNCWINDOWPOS");
1513 1514 1515
    if(flags & SWP_NOCLIENTSIZE) TRACE(" SWP_NOCLIENTSIZE");
    if(flags & SWP_NOCLIENTMOVE) TRACE(" SWP_NOCLIENTMOVE");
    if(flags & SWP_STATECHANGED) TRACE(" SWP_STATECHANGED");
1516

1517
    if(flags & ~dumped_flags) TRACE(" %08x", flags & ~dumped_flags);
1518
    TRACE("\n");
1519 1520
}

1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642
/***********************************************************************
 *           SWP_DoWinPosChanging
 */
static BOOL SWP_DoWinPosChanging( WINDOWPOS* pWinpos, RECT* pNewWindowRect, RECT* pNewClientRect )
{
    WND *wndPtr;

    /* Send WM_WINDOWPOSCHANGING message */

    if (!(pWinpos->flags & SWP_NOSENDCHANGING))
        SendMessageW( pWinpos->hwnd, WM_WINDOWPOSCHANGING, 0, (LPARAM)pWinpos );

    if (!(wndPtr = WIN_GetPtr( pWinpos->hwnd )) ||
        wndPtr == WND_OTHER_PROCESS || wndPtr == WND_DESKTOP) return FALSE;

    /* Calculate new position and size */

    *pNewWindowRect = wndPtr->rectWindow;
    *pNewClientRect = (wndPtr->dwStyle & WS_MINIMIZE) ? wndPtr->rectWindow
                                                    : wndPtr->rectClient;

    if (!(pWinpos->flags & SWP_NOSIZE))
    {
        pNewWindowRect->right  = pNewWindowRect->left + pWinpos->cx;
        pNewWindowRect->bottom = pNewWindowRect->top + pWinpos->cy;
    }
    if (!(pWinpos->flags & SWP_NOMOVE))
    {
        pNewWindowRect->left    = pWinpos->x;
        pNewWindowRect->top     = pWinpos->y;
        pNewWindowRect->right  += pWinpos->x - wndPtr->rectWindow.left;
        pNewWindowRect->bottom += pWinpos->y - wndPtr->rectWindow.top;

        OffsetRect( pNewClientRect, pWinpos->x - wndPtr->rectWindow.left,
                                    pWinpos->y - wndPtr->rectWindow.top );
    }
    pWinpos->flags |= SWP_NOCLIENTMOVE | SWP_NOCLIENTSIZE;

    TRACE( "hwnd %p, after %p, swp %d,%d %dx%d flags %08x\n",
           pWinpos->hwnd, pWinpos->hwndInsertAfter, pWinpos->x, pWinpos->y,
           pWinpos->cx, pWinpos->cy, pWinpos->flags );
    TRACE( "current %s style %08x new %s\n",
           wine_dbgstr_rect( &wndPtr->rectWindow ), wndPtr->dwStyle,
           wine_dbgstr_rect( pNewWindowRect ));

    WIN_ReleasePtr( wndPtr );
    return TRUE;
}

/***********************************************************************
 *           get_valid_rects
 *
 * Compute the valid rects from the old and new client rect and WVR_* flags.
 * Helper for WM_NCCALCSIZE handling.
 */
static inline void get_valid_rects( const RECT *old_client, const RECT *new_client, UINT flags,
                                    RECT *valid )
{
    int cx, cy;

    if (flags & WVR_REDRAW)
    {
        SetRectEmpty( &valid[0] );
        SetRectEmpty( &valid[1] );
        return;
    }

    if (flags & WVR_VALIDRECTS)
    {
        if (!IntersectRect( &valid[0], &valid[0], new_client ) ||
            !IntersectRect( &valid[1], &valid[1], old_client ))
        {
            SetRectEmpty( &valid[0] );
            SetRectEmpty( &valid[1] );
            return;
        }
        flags = WVR_ALIGNLEFT | WVR_ALIGNTOP;
    }
    else
    {
        valid[0] = *new_client;
        valid[1] = *old_client;
    }

    /* make sure the rectangles have the same size */
    cx = min( valid[0].right - valid[0].left, valid[1].right - valid[1].left );
    cy = min( valid[0].bottom - valid[0].top, valid[1].bottom - valid[1].top );

    if (flags & WVR_ALIGNBOTTOM)
    {
        valid[0].top = valid[0].bottom - cy;
        valid[1].top = valid[1].bottom - cy;
    }
    else
    {
        valid[0].bottom = valid[0].top + cy;
        valid[1].bottom = valid[1].top + cy;
    }
    if (flags & WVR_ALIGNRIGHT)
    {
        valid[0].left = valid[0].right - cx;
        valid[1].left = valid[1].right - cx;
    }
    else
    {
        valid[0].right = valid[0].left + cx;
        valid[1].right = valid[1].left + cx;
    }
}


/***********************************************************************
 *           SWP_DoOwnedPopups
 *
 * fix Z order taking into account owned popups -
 * basically we need to maintain them above the window that owns them
 *
 * FIXME: hide/show owned popups when owner visibility changes.
 */
static HWND SWP_DoOwnedPopups(HWND hwnd, HWND hwndInsertAfter)
{
    LONG style = GetWindowLongW( hwnd, GWL_STYLE );
1643 1644
    HWND owner, *list = NULL;
    unsigned int i;
1645 1646 1647

    TRACE("(%p) hInsertAfter = %p\n", hwnd, hwndInsertAfter );

1648
    if ((style & WS_POPUP) && (owner = GetWindow( hwnd, GW_OWNER )))
1649 1650 1651
    {
        /* make sure this popup stays above the owner */

1652
        if (hwndInsertAfter != HWND_TOP && hwndInsertAfter != HWND_TOPMOST)
1653
        {
1654
            if (!(list = WIN_ListChildren( GetDesktopWindow() ))) return hwndInsertAfter;
1655

1656
            for (i = 0; list[i]; i++)
1657
            {
1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669
                if (list[i] == owner)
                {
                    if (i > 0) hwndInsertAfter = list[i-1];
                    else hwndInsertAfter = HWND_TOP;
                    break;
                }

                if (hwndInsertAfter == HWND_NOTOPMOST)
                {
                    if (!(GetWindowLongW( list[i], GWL_EXSTYLE ) & WS_EX_TOPMOST)) break;
                }
                else if (list[i] == hwndInsertAfter) break;
1670 1671 1672 1673 1674
            }
        }
    }
    else if (style & WS_CHILD) return hwndInsertAfter;

1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710
    if (hwndInsertAfter == HWND_BOTTOM) goto done;
    if (!list && !(list = WIN_ListChildren( GetDesktopWindow() ))) goto done;

    i = 0;
    if (hwndInsertAfter == HWND_TOP || hwndInsertAfter == HWND_NOTOPMOST)
    {
        if (hwndInsertAfter == HWND_NOTOPMOST || !(GetWindowLongW( hwnd, GWL_EXSTYLE ) & WS_EX_TOPMOST))
        {
            /* skip all the topmost windows */
            while (list[i] && (GetWindowLongW( list[i], GWL_EXSTYLE ) & WS_EX_TOPMOST)) i++;
        }
    }
    else if (hwndInsertAfter != HWND_TOPMOST)
    {
        /* skip windows that are already placed correctly */
        for (i = 0; list[i]; i++)
        {
            if (list[i] == hwndInsertAfter) break;
            if (list[i] == hwnd) goto done;  /* nothing to do if window is moving backwards in z-order */
        }
    }

    for ( ; list[i]; i++)
    {
        if (list[i] == hwnd) break;
        if (!(GetWindowLongW( list[i], GWL_STYLE ) & WS_POPUP)) continue;
        if (GetWindow( list[i], GW_OWNER ) != hwnd) continue;
        TRACE( "moving %p owned by %p after %p\n", list[i], hwnd, hwndInsertAfter );
        SetWindowPos( list[i], hwndInsertAfter, 0, 0, 0, 0,
                      SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE | SWP_NOSENDCHANGING | SWP_DEFERERASE );
        hwndInsertAfter = list[i];
    }

done:
    HeapFree( GetProcessHeap(), 0, list );
    return hwndInsertAfter;
1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828
}

/***********************************************************************
 *           SWP_DoNCCalcSize
 */
static UINT SWP_DoNCCalcSize( WINDOWPOS* pWinpos, const RECT* pNewWindowRect, RECT* pNewClientRect,
                              RECT *validRects )
{
    UINT wvrFlags = 0;
    WND *wndPtr;

    if (!(wndPtr = WIN_GetPtr( pWinpos->hwnd )) || wndPtr == WND_OTHER_PROCESS) return 0;

      /* Send WM_NCCALCSIZE message to get new client area */
    if( (pWinpos->flags & (SWP_FRAMECHANGED | SWP_NOSIZE)) != SWP_NOSIZE )
    {
        NCCALCSIZE_PARAMS params;
        WINDOWPOS winposCopy;

        params.rgrc[0] = *pNewWindowRect;
        params.rgrc[1] = wndPtr->rectWindow;
        params.rgrc[2] = wndPtr->rectClient;
        params.lppos = &winposCopy;
        winposCopy = *pWinpos;
        WIN_ReleasePtr( wndPtr );

        wvrFlags = SendMessageW( pWinpos->hwnd, WM_NCCALCSIZE, TRUE, (LPARAM)&params );

        *pNewClientRect = params.rgrc[0];

        if (!(wndPtr = WIN_GetPtr( pWinpos->hwnd )) || wndPtr == WND_OTHER_PROCESS) return 0;

        TRACE( "hwnd %p old win %s old client %s new win %s new client %s\n", pWinpos->hwnd,
               wine_dbgstr_rect(&wndPtr->rectWindow), wine_dbgstr_rect(&wndPtr->rectClient),
               wine_dbgstr_rect(pNewWindowRect), wine_dbgstr_rect(pNewClientRect) );

        if( pNewClientRect->left != wndPtr->rectClient.left ||
            pNewClientRect->top != wndPtr->rectClient.top )
            pWinpos->flags &= ~SWP_NOCLIENTMOVE;

        if( (pNewClientRect->right - pNewClientRect->left !=
             wndPtr->rectClient.right - wndPtr->rectClient.left))
            pWinpos->flags &= ~SWP_NOCLIENTSIZE;
        else
            wvrFlags &= ~WVR_HREDRAW;

        if (pNewClientRect->bottom - pNewClientRect->top !=
             wndPtr->rectClient.bottom - wndPtr->rectClient.top)
            pWinpos->flags &= ~SWP_NOCLIENTSIZE;
        else
            wvrFlags &= ~WVR_VREDRAW;

        validRects[0] = params.rgrc[1];
        validRects[1] = params.rgrc[2];
    }
    else
    {
        if (!(pWinpos->flags & SWP_NOMOVE) &&
            (pNewClientRect->left != wndPtr->rectClient.left ||
             pNewClientRect->top != wndPtr->rectClient.top))
            pWinpos->flags &= ~SWP_NOCLIENTMOVE;
    }

    if (pWinpos->flags & (SWP_NOCOPYBITS | SWP_NOREDRAW | SWP_SHOWWINDOW | SWP_HIDEWINDOW))
    {
        SetRectEmpty( &validRects[0] );
        SetRectEmpty( &validRects[1] );
    }
    else get_valid_rects( &wndPtr->rectClient, pNewClientRect, wvrFlags, validRects );

    WIN_ReleasePtr( wndPtr );
    return wvrFlags;
}

/* fix redundant flags and values in the WINDOWPOS structure */
static BOOL fixup_flags( WINDOWPOS *winpos )
{
    HWND parent;
    WND *wndPtr = WIN_GetPtr( winpos->hwnd );
    BOOL ret = TRUE;

    if (!wndPtr || wndPtr == WND_OTHER_PROCESS)
    {
        SetLastError( ERROR_INVALID_WINDOW_HANDLE );
        return FALSE;
    }
    winpos->hwnd = wndPtr->hwndSelf;  /* make it a full handle */

    /* Finally make sure that all coordinates are valid */
    if (winpos->x < -32768) winpos->x = -32768;
    else if (winpos->x > 32767) winpos->x = 32767;
    if (winpos->y < -32768) winpos->y = -32768;
    else if (winpos->y > 32767) winpos->y = 32767;

    if (winpos->cx < 0) winpos->cx = 0;
    else if (winpos->cx > 32767) winpos->cx = 32767;
    if (winpos->cy < 0) winpos->cy = 0;
    else if (winpos->cy > 32767) winpos->cy = 32767;

    parent = GetAncestor( winpos->hwnd, GA_PARENT );
    if (!IsWindowVisible( parent )) winpos->flags |= SWP_NOREDRAW;

    if (wndPtr->dwStyle & WS_VISIBLE) winpos->flags &= ~SWP_SHOWWINDOW;
    else
    {
        winpos->flags &= ~SWP_HIDEWINDOW;
        if (!(winpos->flags & SWP_SHOWWINDOW)) winpos->flags |= SWP_NOREDRAW;
    }

    if ((wndPtr->rectWindow.right - wndPtr->rectWindow.left == winpos->cx) &&
        (wndPtr->rectWindow.bottom - wndPtr->rectWindow.top == winpos->cy))
        winpos->flags |= SWP_NOSIZE;    /* Already the right size */

    if ((wndPtr->rectWindow.left == winpos->x) && (wndPtr->rectWindow.top == winpos->y))
        winpos->flags |= SWP_NOMOVE;    /* Already the right position */

    if ((wndPtr->dwStyle & (WS_POPUP | WS_CHILD)) != WS_CHILD)
    {
1829 1830 1831
        if (!(winpos->flags & (SWP_NOACTIVATE|SWP_HIDEWINDOW)) && /* Bring to the top when activating */
            (winpos->flags & SWP_NOZORDER ||
             (winpos->hwndInsertAfter != HWND_TOPMOST && winpos->hwndInsertAfter != HWND_NOTOPMOST)))
1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852
        {
            winpos->flags &= ~SWP_NOZORDER;
            winpos->hwndInsertAfter = HWND_TOP;
        }
    }

    /* Check hwndInsertAfter */
    if (winpos->flags & SWP_NOZORDER) goto done;

    /* fix sign extension */
    if (winpos->hwndInsertAfter == (HWND)0xffff) winpos->hwndInsertAfter = HWND_TOPMOST;
    else if (winpos->hwndInsertAfter == (HWND)0xfffe) winpos->hwndInsertAfter = HWND_NOTOPMOST;

    /* hwndInsertAfter must be a sibling of the window */
    if (winpos->hwndInsertAfter == HWND_TOP)
    {
        if (GetWindow(winpos->hwnd, GW_HWNDFIRST) == winpos->hwnd)
            winpos->flags |= SWP_NOZORDER;
    }
    else if (winpos->hwndInsertAfter == HWND_BOTTOM)
    {
1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863
        if (!(wndPtr->dwExStyle & WS_EX_TOPMOST) && GetWindow(winpos->hwnd, GW_HWNDLAST) == winpos->hwnd)
            winpos->flags |= SWP_NOZORDER;
    }
    else if (winpos->hwndInsertAfter == HWND_TOPMOST)
    {
        if ((wndPtr->dwExStyle & WS_EX_TOPMOST) && GetWindow(winpos->hwnd, GW_HWNDFIRST) == winpos->hwnd)
            winpos->flags |= SWP_NOZORDER;
    }
    else if (winpos->hwndInsertAfter == HWND_NOTOPMOST)
    {
        if (!(wndPtr->dwExStyle & WS_EX_TOPMOST))
1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883
            winpos->flags |= SWP_NOZORDER;
    }
    else
    {
        if (GetAncestor( winpos->hwndInsertAfter, GA_PARENT ) != parent) ret = FALSE;
        else
        {
            /* don't need to change the Zorder of hwnd if it's already inserted
             * after hwndInsertAfter or when inserting hwnd after itself.
             */
            if ((winpos->hwnd == winpos->hwndInsertAfter) ||
                (winpos->hwnd == GetWindow( winpos->hwndInsertAfter, GW_HWNDNEXT )))
                winpos->flags |= SWP_NOZORDER;
        }
    }
 done:
    WIN_ReleasePtr( wndPtr );
    return ret;
}

1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894

/***********************************************************************
 *		set_window_pos
 *
 * Backend implementation of SetWindowPos.
 */
BOOL set_window_pos( HWND hwnd, HWND insert_after, UINT swp_flags,
                     const RECT *window_rect, const RECT *client_rect, const RECT *valid_rects )
{
    WND *win;
    BOOL ret;
1895
    RECT visible_rect, old_window_rect;
1896 1897 1898 1899

    visible_rect = *window_rect;
    USER_Driver->pWindowPosChanging( hwnd, insert_after, swp_flags,
                                     window_rect, client_rect, &visible_rect );
1900 1901 1902 1903

    if (!(win = WIN_GetPtr( hwnd ))) return FALSE;
    if (win == WND_DESKTOP || win == WND_OTHER_PROCESS) return FALSE;

1904
    old_window_rect = win->rectWindow;
1905 1906
    SERVER_START_REQ( set_window_pos )
    {
1907 1908
        req->handle        = wine_server_user_handle( hwnd );
        req->previous      = wine_server_user_handle( insert_after );
1909 1910 1911 1912 1913 1914 1915 1916 1917
        req->flags         = swp_flags;
        req->window.left   = window_rect->left;
        req->window.top    = window_rect->top;
        req->window.right  = window_rect->right;
        req->window.bottom = window_rect->bottom;
        req->client.left   = client_rect->left;
        req->client.top    = client_rect->top;
        req->client.right  = client_rect->right;
        req->client.bottom = client_rect->bottom;
1918 1919 1920 1921 1922 1923
        if (memcmp( window_rect, &visible_rect, sizeof(RECT) ) || !IsRectEmpty( &valid_rects[0] ))
        {
            wine_server_add_data( req, &visible_rect, sizeof(visible_rect) );
            if (!IsRectEmpty( &valid_rects[0] ))
                wine_server_add_data( req, valid_rects, 2 * sizeof(*valid_rects) );
        }
1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935
        if ((ret = !wine_server_call( req )))
        {
            win->dwStyle    = reply->new_style;
            win->dwExStyle  = reply->new_ex_style;
            win->rectWindow = *window_rect;
            win->rectClient = *client_rect;
        }
    }
    SERVER_END_REQ;
    WIN_ReleasePtr( win );

    if (ret)
1936
    {
1937 1938
        if (((swp_flags & SWP_AGG_NOPOSCHANGE) != SWP_AGG_NOPOSCHANGE) ||
            (swp_flags & (SWP_HIDEWINDOW | SWP_SHOWWINDOW | SWP_STATECHANGED)))
1939
            invalidate_dce( hwnd, &old_window_rect );
1940

1941 1942
        USER_Driver->pWindowPosChanged( hwnd, insert_after, swp_flags, window_rect,
                                        client_rect, &visible_rect, valid_rects );
1943
    }
1944 1945 1946 1947
    return ret;
}


1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990
/***********************************************************************
 *		USER_SetWindowPos
 *
 *     User32 internal function
 */
BOOL USER_SetWindowPos( WINDOWPOS * winpos )
{
    RECT newWindowRect, newClientRect, valid_rects[2];
    UINT orig_flags;
    
    orig_flags = winpos->flags;
    
    /* First make sure that coordinates are valid for WM_WINDOWPOSCHANGING */
    if (!(winpos->flags & SWP_NOMOVE))
    {
        if (winpos->x < -32768) winpos->x = -32768;
        else if (winpos->x > 32767) winpos->x = 32767;
        if (winpos->y < -32768) winpos->y = -32768;
        else if (winpos->y > 32767) winpos->y = 32767;
    }
    if (!(winpos->flags & SWP_NOSIZE))
    {
        if (winpos->cx < 0) winpos->cx = 0;
        else if (winpos->cx > 32767) winpos->cx = 32767;
        if (winpos->cy < 0) winpos->cy = 0;
        else if (winpos->cy > 32767) winpos->cy = 32767;
    }

    if (!SWP_DoWinPosChanging( winpos, &newWindowRect, &newClientRect )) return FALSE;

    /* Fix redundant flags */
    if (!fixup_flags( winpos )) return FALSE;

    if((winpos->flags & (SWP_NOZORDER | SWP_HIDEWINDOW | SWP_SHOWWINDOW)) != SWP_NOZORDER)
    {
        if (GetAncestor( winpos->hwnd, GA_PARENT ) == GetDesktopWindow())
            winpos->hwndInsertAfter = SWP_DoOwnedPopups( winpos->hwnd, winpos->hwndInsertAfter );
    }

    /* Common operations */

    SWP_DoNCCalcSize( winpos, &newWindowRect, &newClientRect, valid_rects );

1991
    if (!set_window_pos( winpos->hwnd, winpos->hwndInsertAfter, winpos->flags,
1992
                         &newWindowRect, &newClientRect, valid_rects ))
1993 1994
        return FALSE;

1995 1996 1997 1998 1999 2000 2001 2002 2003
    /* erase parent when hiding or resizing child */
    if (!(orig_flags & SWP_DEFERERASE) &&
        ((orig_flags & SWP_HIDEWINDOW) ||
         (!(orig_flags & SWP_SHOWWINDOW) &&
          (winpos->flags & SWP_AGG_STATUSFLAGS) != SWP_AGG_NOGEOMETRYCHANGE)))
    {
        HWND parent = GetAncestor( winpos->hwnd, GA_PARENT );
        if (!parent || parent == GetDesktopWindow()) parent = winpos->hwnd;
        erase_now( parent, 0 );
2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037
    }

    if( winpos->flags & SWP_HIDEWINDOW )
        HideCaret(winpos->hwnd);
    else if (winpos->flags & SWP_SHOWWINDOW)
        ShowCaret(winpos->hwnd);

    if (!(winpos->flags & (SWP_NOACTIVATE|SWP_HIDEWINDOW)))
    {
        /* child windows get WM_CHILDACTIVATE message */
        if ((GetWindowLongW( winpos->hwnd, GWL_STYLE ) & (WS_CHILD | WS_POPUP)) == WS_CHILD)
            SendMessageW( winpos->hwnd, WM_CHILDACTIVATE, 0, 0 );
        else
            SetForegroundWindow( winpos->hwnd );
    }

      /* And last, send the WM_WINDOWPOSCHANGED message */

    TRACE("\tstatus flags = %04x\n", winpos->flags & SWP_AGG_STATUSFLAGS);

    if (((winpos->flags & SWP_AGG_STATUSFLAGS) != SWP_AGG_NOPOSCHANGE))
    {
        /* WM_WINDOWPOSCHANGED is sent even if SWP_NOSENDCHANGING is set
           and always contains final window position.
         */
        winpos->x = newWindowRect.left;
        winpos->y = newWindowRect.top;
        winpos->cx = newWindowRect.right - newWindowRect.left;
        winpos->cy = newWindowRect.bottom - newWindowRect.top;
        SendMessageW( winpos->hwnd, WM_WINDOWPOSCHANGED, 0, (LPARAM)winpos );
    }
    return TRUE;
}

Alexandre Julliard's avatar
Alexandre Julliard committed
2038
/***********************************************************************
2039
 *		SetWindowPos (USER32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
2040
 */
2041
BOOL WINAPI SetWindowPos( HWND hwnd, HWND hwndInsertAfter,
2042
                          INT x, INT y, INT cx, INT cy, UINT flags )
Alexandre Julliard's avatar
Alexandre Julliard committed
2043
{
2044
    WINDOWPOS winpos;
Alexandre Julliard's avatar
Alexandre Julliard committed
2045

2046 2047
    TRACE("hwnd %p, after %p, %d,%d (%dx%d), flags %08x\n",
          hwnd, hwndInsertAfter, x, y, cx, cy, flags);
2048 2049
    if(TRACE_ON(win)) dump_winpos_flags(flags);

2050 2051 2052 2053 2054 2055
    if (is_broadcast(hwnd))
    {
        SetLastError( ERROR_INVALID_PARAMETER );
        return FALSE;
    }

2056 2057
    winpos.hwnd = WIN_GetFullHandle(hwnd);
    winpos.hwndInsertAfter = WIN_GetFullHandle(hwndInsertAfter);
Alexandre Julliard's avatar
Alexandre Julliard committed
2058 2059 2060 2061 2062
    winpos.x = x;
    winpos.y = y;
    winpos.cx = cx;
    winpos.cy = cy;
    winpos.flags = flags;
2063
    
2064
    if (WIN_IsCurrentThread( hwnd ))
2065
        return USER_SetWindowPos(&winpos);
2066

2067
    return SendMessageW( winpos.hwnd, WM_WINE_SETWINDOWPOS, 0, (LPARAM)&winpos );
Alexandre Julliard's avatar
Alexandre Julliard committed
2068 2069
}

2070

Alexandre Julliard's avatar
Alexandre Julliard committed
2071
/***********************************************************************
2072
 *		BeginDeferWindowPos (USER32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
2073
 */
2074
HDWP WINAPI BeginDeferWindowPos( INT count )
Alexandre Julliard's avatar
Alexandre Julliard committed
2075
{
2076
    HDWP handle;
Alexandre Julliard's avatar
Alexandre Julliard committed
2077 2078
    DWP *pDWP;

2079 2080
    TRACE("%d\n", count);

2081
    if (count < 0)
2082 2083 2084 2085 2086 2087 2088
    {
        SetLastError(ERROR_INVALID_PARAMETER);
        return 0;
    }
    /* Windows allows zero count, in which case it allocates context for 8 moves */
    if (count == 0) count = 8;

2089
    handle = USER_HEAP_ALLOC( sizeof(DWP) + (count-1)*sizeof(WINDOWPOS) );
Alexandre Julliard's avatar
Alexandre Julliard committed
2090
    if (!handle) return 0;
2091
    pDWP = USER_HEAP_LIN_ADDR( handle );
Alexandre Julliard's avatar
Alexandre Julliard committed
2092 2093 2094 2095
    pDWP->actualCount    = 0;
    pDWP->suggestedCount = count;
    pDWP->valid          = TRUE;
    pDWP->wMagic         = DWP_MAGIC;
Alexandre Julliard's avatar
Alexandre Julliard committed
2096
    pDWP->hwndParent     = 0;
2097 2098

    TRACE("returning hdwp %p\n", handle);
Alexandre Julliard's avatar
Alexandre Julliard committed
2099 2100 2101 2102
    return handle;
}


Alexandre Julliard's avatar
Alexandre Julliard committed
2103
/***********************************************************************
2104
 *		DeferWindowPos (USER32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
2105
 */
2106 2107 2108
HDWP WINAPI DeferWindowPos( HDWP hdwp, HWND hwnd, HWND hwndAfter,
                                INT x, INT y, INT cx, INT cy,
                                UINT flags )
Alexandre Julliard's avatar
Alexandre Julliard committed
2109
{
Alexandre Julliard's avatar
Alexandre Julliard committed
2110 2111
    DWP *pDWP;
    int i;
2112
    HDWP newhdwp = hdwp,retvalue;
Alexandre Julliard's avatar
Alexandre Julliard committed
2113

2114 2115 2116
    TRACE("hdwp %p, hwnd %p, after %p, %d,%d (%dx%d), flags %08x\n",
          hdwp, hwnd, hwndAfter, x, y, cx, cy, flags);

2117
    hwnd = WIN_GetFullHandle( hwnd );
2118
    if (is_desktop_window( hwnd )) return 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
2119

2120 2121
    if (!(pDWP = USER_HEAP_LIN_ADDR( hdwp ))) return 0;

2122
    USER_Lock();
Alexandre Julliard's avatar
Alexandre Julliard committed
2123

Alexandre Julliard's avatar
Alexandre Julliard committed
2124 2125 2126 2127 2128 2129 2130
    for (i = 0; i < pDWP->actualCount; i++)
    {
        if (pDWP->winPos[i].hwnd == hwnd)
        {
              /* Merge with the other changes */
            if (!(flags & SWP_NOZORDER))
            {
2131
                pDWP->winPos[i].hwndInsertAfter = WIN_GetFullHandle(hwndAfter);
Alexandre Julliard's avatar
Alexandre Julliard committed
2132 2133 2134 2135 2136
            }
            if (!(flags & SWP_NOMOVE))
            {
                pDWP->winPos[i].x = x;
                pDWP->winPos[i].y = y;
2137
            }
Alexandre Julliard's avatar
Alexandre Julliard committed
2138 2139 2140 2141 2142
            if (!(flags & SWP_NOSIZE))
            {
                pDWP->winPos[i].cx = cx;
                pDWP->winPos[i].cy = cy;
            }
Alexandre Julliard's avatar
Alexandre Julliard committed
2143 2144 2145 2146
            pDWP->winPos[i].flags &= flags | ~(SWP_NOSIZE | SWP_NOMOVE |
                                               SWP_NOZORDER | SWP_NOREDRAW |
                                               SWP_NOACTIVATE | SWP_NOCOPYBITS|
                                               SWP_NOOWNERZORDER);
Alexandre Julliard's avatar
Alexandre Julliard committed
2147 2148
            pDWP->winPos[i].flags |= flags & (SWP_SHOWWINDOW | SWP_HIDEWINDOW |
                                              SWP_FRAMECHANGED);
2149 2150
            retvalue = hdwp;
            goto END;
Alexandre Julliard's avatar
Alexandre Julliard committed
2151 2152 2153 2154 2155
        }
    }
    if (pDWP->actualCount >= pDWP->suggestedCount)
    {
        newhdwp = USER_HEAP_REALLOC( hdwp,
2156
                      sizeof(DWP) + pDWP->suggestedCount*sizeof(WINDOWPOS) );
2157 2158 2159 2160 2161
        if (!newhdwp)
        {
            retvalue = 0;
            goto END;
        }
2162
        pDWP = USER_HEAP_LIN_ADDR( newhdwp );
Alexandre Julliard's avatar
Alexandre Julliard committed
2163 2164 2165 2166 2167 2168 2169 2170 2171 2172
        pDWP->suggestedCount++;
    }
    pDWP->winPos[pDWP->actualCount].hwnd = hwnd;
    pDWP->winPos[pDWP->actualCount].hwndInsertAfter = hwndAfter;
    pDWP->winPos[pDWP->actualCount].x = x;
    pDWP->winPos[pDWP->actualCount].y = y;
    pDWP->winPos[pDWP->actualCount].cx = cx;
    pDWP->winPos[pDWP->actualCount].cy = cy;
    pDWP->winPos[pDWP->actualCount].flags = flags;
    pDWP->actualCount++;
2173 2174
    retvalue = newhdwp;
END:
2175
    USER_Unlock();
2176
    return retvalue;
Alexandre Julliard's avatar
Alexandre Julliard committed
2177
}
Alexandre Julliard's avatar
Alexandre Julliard committed
2178

Alexandre Julliard's avatar
Alexandre Julliard committed
2179

Alexandre Julliard's avatar
Alexandre Julliard committed
2180
/***********************************************************************
2181
 *		EndDeferWindowPos (USER32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
2182
 */
2183
BOOL WINAPI EndDeferWindowPos( HDWP hdwp )
Alexandre Julliard's avatar
Alexandre Julliard committed
2184 2185
{
    DWP *pDWP;
2186 2187
    WINDOWPOS *winpos;
    BOOL res = TRUE;
Alexandre Julliard's avatar
Alexandre Julliard committed
2188
    int i;
Alexandre Julliard's avatar
Alexandre Julliard committed
2189

2190 2191
    TRACE("%p\n", hdwp);

2192
    pDWP = USER_HEAP_LIN_ADDR( hdwp );
Alexandre Julliard's avatar
Alexandre Julliard committed
2193
    if (!pDWP) return FALSE;
2194
    for (i = 0, winpos = pDWP->winPos; res && i < pDWP->actualCount; i++, winpos++)
Alexandre Julliard's avatar
Alexandre Julliard committed
2195
    {
2196 2197 2198 2199
        TRACE("hwnd %p, after %p, %d,%d (%dx%d), flags %08x\n",
               winpos->hwnd, winpos->hwndInsertAfter, winpos->x, winpos->y,
               winpos->cx, winpos->cy, winpos->flags);

2200 2201 2202 2203
        if (WIN_IsCurrentThread( winpos->hwnd ))
            res = USER_SetWindowPos( winpos );
        else
            res = SendMessageW( winpos->hwnd, WM_WINE_SETWINDOWPOS, 0, (LPARAM)winpos );
Alexandre Julliard's avatar
Alexandre Julliard committed
2204 2205
    }
    USER_HEAP_FREE( hdwp );
Alexandre Julliard's avatar
Alexandre Julliard committed
2206
    return res;
Alexandre Julliard's avatar
Alexandre Julliard committed
2207
}
2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323


/***********************************************************************
 *		ArrangeIconicWindows (USER32.@)
 */
UINT WINAPI ArrangeIconicWindows( HWND parent )
{
    RECT rectParent;
    HWND hwndChild;
    INT x, y, xspacing, yspacing;

    GetClientRect( parent, &rectParent );
    x = rectParent.left;
    y = rectParent.bottom;
    xspacing = GetSystemMetrics(SM_CXICONSPACING);
    yspacing = GetSystemMetrics(SM_CYICONSPACING);

    hwndChild = GetWindow( parent, GW_CHILD );
    while (hwndChild)
    {
        if( IsIconic( hwndChild ) )
        {
            WINPOS_ShowIconTitle( hwndChild, FALSE );

            SetWindowPos( hwndChild, 0, x + (xspacing - GetSystemMetrics(SM_CXICON)) / 2,
                            y - yspacing - GetSystemMetrics(SM_CYICON)/2, 0, 0,
                            SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE );
	    if( IsWindow(hwndChild) )
                WINPOS_ShowIconTitle(hwndChild , TRUE );

            if (x <= rectParent.right - xspacing) x += xspacing;
            else
            {
                x = rectParent.left;
                y -= yspacing;
            }
        }
        hwndChild = GetWindow( hwndChild, GW_HWNDNEXT );
    }
    return yspacing;
}


/***********************************************************************
 *           draw_moving_frame
 *
 * Draw the frame used when moving or resizing window.
 */
static void draw_moving_frame( HDC hdc, RECT *rect, BOOL thickframe )
{
    if (thickframe)
    {
        const int width = GetSystemMetrics(SM_CXFRAME);
        const int height = GetSystemMetrics(SM_CYFRAME);

        HBRUSH hbrush = SelectObject( hdc, GetStockObject( GRAY_BRUSH ) );
        PatBlt( hdc, rect->left, rect->top,
                rect->right - rect->left - width, height, PATINVERT );
        PatBlt( hdc, rect->left, rect->top + height, width,
                rect->bottom - rect->top - height, PATINVERT );
        PatBlt( hdc, rect->left + width, rect->bottom - 1,
                rect->right - rect->left - width, -height, PATINVERT );
        PatBlt( hdc, rect->right - 1, rect->top, -width,
                rect->bottom - rect->top - height, PATINVERT );
        SelectObject( hdc, hbrush );
    }
    else DrawFocusRect( hdc, rect );
}


/***********************************************************************
 *           start_size_move
 *
 * Initialization of a move or resize, when initiated from a menu choice.
 * Return hit test code for caption or sizing border.
 */
static LONG start_size_move( HWND hwnd, WPARAM wParam, POINT *capturePoint, LONG style )
{
    LONG hittest = 0;
    POINT pt;
    MSG msg;
    RECT rectWindow;

    GetWindowRect( hwnd, &rectWindow );

    if ((wParam & 0xfff0) == SC_MOVE)
    {
        /* Move pointer at the center of the caption */
        RECT rect = rectWindow;
        /* Note: to be exactly centered we should take the different types
         * of border into account, but it shouldn't make more than a few pixels
         * of difference so let's not bother with that */
        rect.top += GetSystemMetrics(SM_CYBORDER);
        if (style & WS_SYSMENU)
            rect.left += GetSystemMetrics(SM_CXSIZE) + 1;
        if (style & WS_MINIMIZEBOX)
            rect.right -= GetSystemMetrics(SM_CXSIZE) + 1;
        if (style & WS_MAXIMIZEBOX)
            rect.right -= GetSystemMetrics(SM_CXSIZE) + 1;
        pt.x = (rect.right + rect.left) / 2;
        pt.y = rect.top + GetSystemMetrics(SM_CYSIZE)/2;
        hittest = HTCAPTION;
        *capturePoint = pt;
    }
    else  /* SC_SIZE */
    {
        SetCursor( LoadCursorW( 0, (LPWSTR)IDC_SIZEALL ) );
        pt.x = pt.y = 0;
        while(!hittest)
        {
            if (!GetMessageW( &msg, 0, 0, 0 )) return 0;
            if (CallMsgFilterW( &msg, MSGF_SIZE )) continue;

            switch(msg.message)
            {
            case WM_MOUSEMOVE:
2324 2325
                pt.x = min( max( msg.pt.x, rectWindow.left ), rectWindow.right - 1 );
                pt.y = min( max( msg.pt.y, rectWindow.top ), rectWindow.bottom - 1 );
2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608
                hittest = SendMessageW( hwnd, WM_NCHITTEST, 0, MAKELONG( pt.x, pt.y ) );
                if ((hittest < HTLEFT) || (hittest > HTBOTTOMRIGHT)) hittest = 0;
                break;

            case WM_LBUTTONUP:
                return 0;

            case WM_KEYDOWN:
                switch(msg.wParam)
                {
                case VK_UP:
                    hittest = HTTOP;
                    pt.x =(rectWindow.left+rectWindow.right)/2;
                    pt.y = rectWindow.top + GetSystemMetrics(SM_CYFRAME) / 2;
                    break;
                case VK_DOWN:
                    hittest = HTBOTTOM;
                    pt.x =(rectWindow.left+rectWindow.right)/2;
                    pt.y = rectWindow.bottom - GetSystemMetrics(SM_CYFRAME) / 2;
                    break;
                case VK_LEFT:
                    hittest = HTLEFT;
                    pt.x = rectWindow.left + GetSystemMetrics(SM_CXFRAME) / 2;
                    pt.y =(rectWindow.top+rectWindow.bottom)/2;
                    break;
                case VK_RIGHT:
                    hittest = HTRIGHT;
                    pt.x = rectWindow.right - GetSystemMetrics(SM_CXFRAME) / 2;
                    pt.y =(rectWindow.top+rectWindow.bottom)/2;
                    break;
                case VK_RETURN:
                case VK_ESCAPE:
                    return 0;
                }
                break;
            default:
                TranslateMessage( &msg );
                DispatchMessageW( &msg );
                break;
            }
        }
        *capturePoint = pt;
    }
    SetCursorPos( pt.x, pt.y );
    SendMessageW( hwnd, WM_SETCURSOR, (WPARAM)hwnd, MAKELONG( hittest, WM_MOUSEMOVE ));
    return hittest;
}


/***********************************************************************
 *           WINPOS_SysCommandSizeMove
 *
 * Perform SC_MOVE and SC_SIZE commands.
 */
void WINPOS_SysCommandSizeMove( HWND hwnd, WPARAM wParam )
{
    MSG msg;
    RECT sizingRect, mouseRect, origRect;
    HDC hdc;
    HWND parent;
    LONG hittest = (LONG)(wParam & 0x0f);
    WPARAM syscommand = wParam & 0xfff0;
    HCURSOR hDragCursor = 0, hOldCursor = 0;
    POINT minTrack, maxTrack;
    POINT capturePoint, pt;
    LONG style = GetWindowLongW( hwnd, GWL_STYLE );
    BOOL    thickframe = HAS_THICKFRAME( style );
    BOOL    iconic = style & WS_MINIMIZE;
    BOOL    moved = FALSE;
    DWORD     dwPoint = GetMessagePos ();
    BOOL DragFullWindows = TRUE;

    if (IsZoomed(hwnd) || !IsWindowVisible(hwnd)) return;

    pt.x = (short)LOWORD(dwPoint);
    pt.y = (short)HIWORD(dwPoint);
    capturePoint = pt;

    TRACE("hwnd %p command %04lx, hittest %d, pos %d,%d\n",
          hwnd, syscommand, hittest, pt.x, pt.y);

    if (syscommand == SC_MOVE)
    {
        if (!hittest) hittest = start_size_move( hwnd, wParam, &capturePoint, style );
        if (!hittest) return;
    }
    else  /* SC_SIZE */
    {
        if ( hittest && (syscommand != SC_MOUSEMENU) )
            hittest += (HTLEFT - WMSZ_LEFT);
        else
        {
            set_capture_window( hwnd, GUI_INMOVESIZE, NULL );
            hittest = start_size_move( hwnd, wParam, &capturePoint, style );
            if (!hittest)
            {
                set_capture_window( 0, GUI_INMOVESIZE, NULL );
                return;
            }
        }
    }

      /* Get min/max info */

    WINPOS_GetMinMaxInfo( hwnd, NULL, NULL, &minTrack, &maxTrack );
    GetWindowRect( hwnd, &sizingRect );
    if (style & WS_CHILD)
    {
        parent = GetParent(hwnd);
        /* make sizing rect relative to parent */
        MapWindowPoints( 0, parent, (POINT*)&sizingRect, 2 );
        GetClientRect( parent, &mouseRect );
    }
    else
    {
        parent = 0;
        GetClientRect( GetDesktopWindow(), &mouseRect );
        mouseRect.left = GetSystemMetrics( SM_XVIRTUALSCREEN );
        mouseRect.top = GetSystemMetrics( SM_YVIRTUALSCREEN );
        mouseRect.right = mouseRect.left + GetSystemMetrics( SM_CXVIRTUALSCREEN );
        mouseRect.bottom = mouseRect.top + GetSystemMetrics( SM_CYVIRTUALSCREEN );
    }
    origRect = sizingRect;

    if (ON_LEFT_BORDER(hittest))
    {
        mouseRect.left  = max( mouseRect.left, sizingRect.right-maxTrack.x );
        mouseRect.right = min( mouseRect.right, sizingRect.right-minTrack.x );
    }
    else if (ON_RIGHT_BORDER(hittest))
    {
        mouseRect.left  = max( mouseRect.left, sizingRect.left+minTrack.x );
        mouseRect.right = min( mouseRect.right, sizingRect.left+maxTrack.x );
    }
    if (ON_TOP_BORDER(hittest))
    {
        mouseRect.top    = max( mouseRect.top, sizingRect.bottom-maxTrack.y );
        mouseRect.bottom = min( mouseRect.bottom,sizingRect.bottom-minTrack.y);
    }
    else if (ON_BOTTOM_BORDER(hittest))
    {
        mouseRect.top    = max( mouseRect.top, sizingRect.top+minTrack.y );
        mouseRect.bottom = min( mouseRect.bottom, sizingRect.top+maxTrack.y );
    }
    if (parent) MapWindowPoints( parent, 0, (LPPOINT)&mouseRect, 2 );

    /* Retrieve a default cache DC (without using the window style) */
    hdc = GetDCEx( parent, 0, DCX_CACHE );

    if( iconic ) /* create a cursor for dragging */
    {
        hDragCursor = (HCURSOR)GetClassLongPtrW( hwnd, GCLP_HICON);
        if( !hDragCursor ) hDragCursor = (HCURSOR)SendMessageW( hwnd, WM_QUERYDRAGICON, 0, 0L);
        if( !hDragCursor ) iconic = FALSE;
    }

    /* we only allow disabling the full window drag for child windows */
    if (parent) SystemParametersInfoW( SPI_GETDRAGFULLWINDOWS, 0, &DragFullWindows, 0 );

    /* repaint the window before moving it around */
    RedrawWindow( hwnd, NULL, 0, RDW_UPDATENOW | RDW_ALLCHILDREN );

    SendMessageW( hwnd, WM_ENTERSIZEMOVE, 0, 0 );
    set_capture_window( hwnd, GUI_INMOVESIZE, NULL );

    while(1)
    {
        int dx = 0, dy = 0;

        if (!GetMessageW( &msg, 0, 0, 0 )) break;
        if (CallMsgFilterW( &msg, MSGF_SIZE )) continue;

        /* Exit on button-up, Return, or Esc */
        if ((msg.message == WM_LBUTTONUP) ||
            ((msg.message == WM_KEYDOWN) &&
             ((msg.wParam == VK_RETURN) || (msg.wParam == VK_ESCAPE)))) break;

        if ((msg.message != WM_KEYDOWN) && (msg.message != WM_MOUSEMOVE))
        {
            TranslateMessage( &msg );
            DispatchMessageW( &msg );
            continue;  /* We are not interested in other messages */
        }

        pt = msg.pt;

        if (msg.message == WM_KEYDOWN) switch(msg.wParam)
        {
        case VK_UP:    pt.y -= 8; break;
        case VK_DOWN:  pt.y += 8; break;
        case VK_LEFT:  pt.x -= 8; break;
        case VK_RIGHT: pt.x += 8; break;
        }

        pt.x = max( pt.x, mouseRect.left );
        pt.x = min( pt.x, mouseRect.right );
        pt.y = max( pt.y, mouseRect.top );
        pt.y = min( pt.y, mouseRect.bottom );

        dx = pt.x - capturePoint.x;
        dy = pt.y - capturePoint.y;

        if (dx || dy)
        {
            if( !moved )
            {
                moved = TRUE;

                if( iconic ) /* ok, no system popup tracking */
                {
                    hOldCursor = SetCursor(hDragCursor);
                    ShowCursor( TRUE );
                    WINPOS_ShowIconTitle( hwnd, FALSE );
                }
                else if(!DragFullWindows)
                    draw_moving_frame( hdc, &sizingRect, thickframe );
            }

            if (msg.message == WM_KEYDOWN) SetCursorPos( pt.x, pt.y );
            else
            {
                RECT newRect = sizingRect;
                WPARAM wpSizingHit = 0;

                if (hittest == HTCAPTION) OffsetRect( &newRect, dx, dy );
                if (ON_LEFT_BORDER(hittest)) newRect.left += dx;
                else if (ON_RIGHT_BORDER(hittest)) newRect.right += dx;
                if (ON_TOP_BORDER(hittest)) newRect.top += dy;
                else if (ON_BOTTOM_BORDER(hittest)) newRect.bottom += dy;
                if(!iconic && !DragFullWindows) draw_moving_frame( hdc, &sizingRect, thickframe );
                capturePoint = pt;

                /* determine the hit location */
                if (hittest >= HTLEFT && hittest <= HTBOTTOMRIGHT)
                    wpSizingHit = WMSZ_LEFT + (hittest - HTLEFT);
                SendMessageW( hwnd, WM_SIZING, wpSizingHit, (LPARAM)&newRect );

                if (!iconic)
                {
                    if(!DragFullWindows)
                        draw_moving_frame( hdc, &newRect, thickframe );
                    else
                        SetWindowPos( hwnd, 0, newRect.left, newRect.top,
                                      newRect.right - newRect.left,
                                      newRect.bottom - newRect.top,
                                      ( hittest == HTCAPTION ) ? SWP_NOSIZE : 0 );
                }
                sizingRect = newRect;
            }
        }
    }

    if( iconic )
    {
        if( moved ) /* restore cursors, show icon title later on */
        {
            ShowCursor( FALSE );
            SetCursor( hOldCursor );
        }
    }
    else if (moved && !DragFullWindows)
    {
        draw_moving_frame( hdc, &sizingRect, thickframe );
    }

    set_capture_window( 0, GUI_INMOVESIZE, NULL );
    ReleaseDC( parent, hdc );

    if (HOOK_CallHooks( WH_CBT, HCBT_MOVESIZE, (WPARAM)hwnd, (LPARAM)&sizingRect, TRUE ))
        moved = FALSE;

    SendMessageW( hwnd, WM_EXITSIZEMOVE, 0, 0 );
    SendMessageW( hwnd, WM_SETVISIBLE, !IsIconic(hwnd), 0L);

    /* window moved or resized */
    if (moved)
    {
        /* if the moving/resizing isn't canceled call SetWindowPos
         * with the new position or the new size of the window
         */
        if (!((msg.message == WM_KEYDOWN) && (msg.wParam == VK_ESCAPE)) )
        {
            /* NOTE: SWP_NOACTIVATE prevents document window activation in Word 6 */
2609
            if(!DragFullWindows || iconic)
2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637
                SetWindowPos( hwnd, 0, sizingRect.left, sizingRect.top,
                              sizingRect.right - sizingRect.left,
                              sizingRect.bottom - sizingRect.top,
                              ( hittest == HTCAPTION ) ? SWP_NOSIZE : 0 );
        }
        else
        { /* restore previous size/position */
            if(DragFullWindows)
                SetWindowPos( hwnd, 0, origRect.left, origRect.top,
                              origRect.right - origRect.left,
                              origRect.bottom - origRect.top,
                              ( hittest == HTCAPTION ) ? SWP_NOSIZE : 0 );
        }
    }

    if (IsIconic(hwnd))
    {
        /* Single click brings up the system menu when iconized */

        if( !moved )
        {
            if(style & WS_SYSMENU )
                SendMessageW( hwnd, WM_SYSCOMMAND,
                              SC_MOUSEMENU + HTSYSMENU, MAKELONG(pt.x,pt.y));
        }
        else WINPOS_ShowIconTitle( hwnd, TRUE );
    }
}