nonclient.c 54.3 KB
Newer Older
Alexandre Julliard's avatar
Alexandre Julliard committed
1 2 3 4
/*
 * Non-client area window functions
 *
 * Copyright 1994 Alexandre Julliard
Alexandre Julliard's avatar
Alexandre Julliard committed
5
 *
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
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
Alexandre Julliard's avatar
Alexandre Julliard committed
19
 */
Alexandre Julliard's avatar
Alexandre Julliard committed
20

21 22
#include "config.h"

23 24
#include <stdarg.h>

25
#include "windef.h"
26
#include "winbase.h"
27
#include "wingdi.h"
28
#include "wine/winuser16.h"
29
#include "wownt32.h"
Alexandre Julliard's avatar
Alexandre Julliard committed
30
#include "win.h"
31
#include "user_private.h"
32
#include "controls.h"
Alexandre Julliard's avatar
Alexandre Julliard committed
33
#include "winpos.h"
34
#include "shellapi.h"
35
#include "wine/debug.h"
Alexandre Julliard's avatar
Alexandre Julliard committed
36

37
WINE_DEFAULT_DEBUG_CHANNEL(nonclient);
38

39 40
BOOL NC_DrawGrayButton(HDC hdc, int x, int y);

41
static const BYTE lpGrayMask[] = { 0xAA, 0xA0,
42 43 44 45 46 47 48 49 50 51 52 53
                                   0x55, 0x50,
                                   0xAA, 0xA0,
                                   0x55, 0x50,
                                   0xAA, 0xA0,
                                   0x55, 0x50,
                                   0xAA, 0xA0,
                                   0x55, 0x50,
                                   0xAA, 0xA0,
                                   0x55, 0x50};

#define SC_ABOUTWINE            (SC_SCREENSAVE+1)
#define SC_PUTMARK              (SC_SCREENSAVE+2)
Alexandre Julliard's avatar
Alexandre Julliard committed
54

Alexandre Julliard's avatar
Alexandre Julliard committed
55 56
  /* Some useful macros */
#define HAS_DLGFRAME(style,exStyle) \
Alexandre Julliard's avatar
Alexandre Julliard committed
57
    (((exStyle) & WS_EX_DLGMODALFRAME) || \
58
     (((style) & WS_DLGFRAME) && !((style) & WS_THICKFRAME)))
Alexandre Julliard's avatar
Alexandre Julliard committed
59

60
#define HAS_THICKFRAME(style,exStyle) \
Alexandre Julliard's avatar
Alexandre Julliard committed
61
    (((style) & WS_THICKFRAME) && \
62
     !(((style) & (WS_DLGFRAME|WS_BORDER)) == WS_DLGFRAME))
Alexandre Julliard's avatar
Alexandre Julliard committed
63

64 65
#define HAS_THINFRAME(style) \
    (((style) & WS_BORDER) || !((style) & (WS_CHILD | WS_POPUP)))
Alexandre Julliard's avatar
Alexandre Julliard committed
66

67 68 69
#define HAS_BIGFRAME(style,exStyle) \
    (((style) & (WS_THICKFRAME | WS_DLGFRAME)) || \
     ((exStyle) & WS_EX_DLGMODALFRAME))
70

71 72 73 74
#define HAS_STATICOUTERFRAME(style,exStyle) \
    (((exStyle) & (WS_EX_STATICEDGE|WS_EX_DLGMODALFRAME)) == \
     WS_EX_STATICEDGE)

75 76 77 78
#define HAS_ANYFRAME(style,exStyle) \
    (((style) & (WS_THICKFRAME | WS_DLGFRAME | WS_BORDER)) || \
     ((exStyle) & WS_EX_DLGMODALFRAME) || \
     !((style) & (WS_CHILD | WS_POPUP)))
Alexandre Julliard's avatar
Alexandre Julliard committed
79

80
#define HAS_MENU(w)  ((((w)->dwStyle & (WS_CHILD | WS_POPUP)) != WS_CHILD) && ((w)->wIDmenu != 0))
Alexandre Julliard's avatar
Alexandre Julliard committed
81

82

Alexandre Julliard's avatar
Alexandre Julliard committed
83
/******************************************************************************
84
 * NC_AdjustRectOuter
Alexandre Julliard's avatar
Alexandre Julliard committed
85
 *
Alexandre Julliard's avatar
Alexandre Julliard committed
86 87
 * Computes the size of the "outside" parts of the window based on the
 * parameters of the client area.
Alexandre Julliard's avatar
Alexandre Julliard committed
88
 *
89 90
 * PARAMS
 *     LPRECT  rect
Alexandre Julliard's avatar
Alexandre Julliard committed
91
 *     DWORD  style
92
 *     BOOL  menu
Alexandre Julliard's avatar
Alexandre Julliard committed
93
 *     DWORD  exStyle
Alexandre Julliard's avatar
Alexandre Julliard committed
94
 *
Alexandre Julliard's avatar
Alexandre Julliard committed
95 96 97 98
 * NOTES
 *     "Outer" parts of a window means the whole window frame, caption and
 *     menu bar. It does not include "inner" parts of the frame like client
 *     edge, static edge or scroll bars.
Alexandre Julliard's avatar
Alexandre Julliard committed
99 100 101
 *
 *****************************************************************************/

Alexandre Julliard's avatar
Alexandre Julliard committed
102
static void
103
NC_AdjustRectOuter (LPRECT rect, DWORD style, BOOL menu, DWORD exStyle)
Alexandre Julliard's avatar
Alexandre Julliard committed
104
{
105
    int adjust;
Alexandre Julliard's avatar
Alexandre Julliard committed
106 107
    if(style & WS_ICONIC) return;

108
    if ((exStyle & (WS_EX_STATICEDGE|WS_EX_DLGMODALFRAME)) ==
109 110 111 112 113 114 115 116 117 118 119 120 121
        WS_EX_STATICEDGE)
    {
        adjust = 1; /* for the outer frame always present */
    }
    else
    {
        adjust = 0;
        if ((exStyle & WS_EX_DLGMODALFRAME) ||
            (style & (WS_THICKFRAME|WS_DLGFRAME))) adjust = 2; /* outer */
    }
    if (style & WS_THICKFRAME)
        adjust +=  ( GetSystemMetrics (SM_CXFRAME)
                   - GetSystemMetrics (SM_CXDLGFRAME)); /* The resize border */
122
    if ((style & (WS_BORDER|WS_DLGFRAME)) ||
123 124 125 126
        (exStyle & WS_EX_DLGMODALFRAME))
        adjust++; /* The other border */

    InflateRect (rect, adjust, adjust);
127 128

    if ((style & WS_CAPTION) == WS_CAPTION)
Alexandre Julliard's avatar
Alexandre Julliard committed
129
    {
130 131
        if (exStyle & WS_EX_TOOLWINDOW)
            rect->top -= GetSystemMetrics(SM_CYSMCAPTION);
Alexandre Julliard's avatar
Alexandre Julliard committed
132
        else
133
            rect->top -= GetSystemMetrics(SM_CYCAPTION);
Alexandre Julliard's avatar
Alexandre Julliard committed
134
    }
135
    if (menu) rect->top -= GetSystemMetrics(SM_CYMENU);
Alexandre Julliard's avatar
Alexandre Julliard committed
136 137 138 139
}


/******************************************************************************
140
 * NC_AdjustRectInner
Alexandre Julliard's avatar
Alexandre Julliard committed
141 142 143 144
 *
 * Computes the size of the "inside" part of the window based on the
 * parameters of the client area.
 *
145 146
 * PARAMS
 *     LPRECT   rect
Alexandre Julliard's avatar
Alexandre Julliard committed
147 148 149 150 151 152 153 154 155 156 157
 *     DWORD    style
 *     DWORD    exStyle
 *
 * NOTES
 *     "Inner" part of a window means the window frame inside of the flat
 *     window frame. It includes the client edge, the static edge and the
 *     scroll bars.
 *
 *****************************************************************************/

static void
158
NC_AdjustRectInner (LPRECT rect, DWORD style, DWORD exStyle)
Alexandre Julliard's avatar
Alexandre Julliard committed
159 160 161 162
{
    if(style & WS_ICONIC) return;

    if (exStyle & WS_EX_CLIENTEDGE)
163
        InflateRect(rect, GetSystemMetrics(SM_CXEDGE), GetSystemMetrics(SM_CYEDGE));
Alexandre Julliard's avatar
Alexandre Julliard committed
164

165 166 167 168 169 170 171
    if (style & WS_VSCROLL)
    {
        if((exStyle & WS_EX_LEFTSCROLLBAR) != 0)
            rect->left  -= GetSystemMetrics(SM_CXVSCROLL);
        else
            rect->right += GetSystemMetrics(SM_CXVSCROLL);
    }
172
    if (style & WS_HSCROLL) rect->bottom += GetSystemMetrics(SM_CYHSCROLL);
Alexandre Julliard's avatar
Alexandre Julliard committed
173 174 175
}


176 177 178

static HICON NC_IconForWindow( HWND hwnd )
{
179 180 181
    HICON hIcon = 0;
    WND *wndPtr = WIN_GetPtr( hwnd );

182
    if (wndPtr && wndPtr != WND_OTHER_PROCESS && wndPtr != WND_DESKTOP)
183 184 185 186 187
    {
        hIcon = wndPtr->hIconSmall;
        if (!hIcon) hIcon = wndPtr->hIcon;
        WIN_ReleasePtr( wndPtr );
    }
188 189
    if (!hIcon) hIcon = (HICON) GetClassLongPtrW( hwnd, GCLP_HICONSM );
    if (!hIcon) hIcon = (HICON) GetClassLongPtrW( hwnd, GCLP_HICON );
190 191 192 193

    /* If there is no hIcon specified and this is a modal dialog,
     * get the default one.
     */
194 195
    if (!hIcon && (GetWindowLongW( hwnd, GWL_STYLE ) & DS_MODALFRAME))
        hIcon = LoadImageW(0, (LPCWSTR)IDI_WINLOGO, IMAGE_ICON, 0, 0, LR_DEFAULTCOLOR);
196 197 198
    return hIcon;
}

199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
/* Draws the bar part(ie the big rectangle) of the caption */
static void NC_DrawCaptionBar (HDC hdc, const RECT *rect, DWORD dwStyle, 
                               BOOL active, BOOL gradient)
{
    if (gradient)
    {
        TRIVERTEX vertices[6];
        DWORD colLeft = 
            GetSysColor (active ? COLOR_ACTIVECAPTION : COLOR_INACTIVECAPTION);
        DWORD colRight = 
            GetSysColor (active ? COLOR_GRADIENTACTIVECAPTION 
                                : COLOR_GRADIENTINACTIVECAPTION);
        int v;
        int buttonsAreaSize = GetSystemMetrics(SM_CYCAPTION) - 1;
        static GRADIENT_RECT mesh[] = {{0, 1}, {2, 3}, {4, 5}};
    
        for (v = 0; v < 3; v++)
        {
            vertices[v].Red = GetRValue (colLeft) << 8;
            vertices[v].Green = GetGValue (colLeft) << 8;
            vertices[v].Blue = GetBValue (colLeft) << 8;
            vertices[v].Alpha = 0x8000;
            vertices[v+3].Red = GetRValue (colRight) << 8;
            vertices[v+3].Green = GetGValue (colRight) << 8;
            vertices[v+3].Blue = GetBValue (colRight) << 8;
            vertices[v+3].Alpha = 0x8000;
        }
    
        if ((dwStyle & WS_SYSMENU) 
            && ((dwStyle & WS_MAXIMIZEBOX) || (dwStyle & WS_MINIMIZEBOX)))
            buttonsAreaSize += 2 * (GetSystemMetrics(SM_CXSIZE) + 1);
        
        /* area behind icon; solid filled with left color */
        vertices[0].x = rect->left;
        vertices[0].y = rect->top;
        if (dwStyle & WS_SYSMENU) 
            vertices[1].x = 
                min (rect->left + GetSystemMetrics(SM_CXSMICON), rect->right);
        else
            vertices[1].x = vertices[0].x;
        vertices[1].y = rect->bottom;
        
        /* area behind text; gradient */
        vertices[2].x = vertices[1].x;
        vertices[2].y = rect->top;
        vertices[3].x = max (vertices[2].x, rect->right - buttonsAreaSize);
        vertices[3].y = rect->bottom;
        
        /* area behind buttons; solid filled with right color */
        vertices[4].x = vertices[3].x;
        vertices[4].y = rect->top;
        vertices[5].x = rect->right;
        vertices[5].y = rect->bottom;
        
        GdiGradientFill (hdc, vertices, 6, mesh, 3, GRADIENT_FILL_RECT_H);
    }
    else
        FillRect (hdc, rect, GetSysColorBrush (active ?
                  COLOR_ACTIVECAPTION : COLOR_INACTIVECAPTION));
}

260
/***********************************************************************
261
 *		DrawCaption (USER32.@) Draws a caption bar
262 263 264 265 266 267 268
 *
 * PARAMS
 *     hwnd   [I]
 *     hdc    [I]
 *     lpRect [I]
 *     uFlags [I]
 *
269 270 271
 * RETURNS
 *     Success:
 *     Failure:
272 273
 */

274 275
BOOL WINAPI
DrawCaption (HWND hwnd, HDC hdc, const RECT *lpRect, UINT uFlags)
276
{
277
    return DrawCaptionTempW (hwnd, hdc, lpRect, 0, 0, NULL, uFlags & 0x103F);
278 279 280 281
}


/***********************************************************************
282
 *		DrawCaptionTempA (USER32.@)
283
 */
284 285 286 287 288 289
BOOL WINAPI DrawCaptionTempA (HWND hwnd, HDC hdc, const RECT *rect, HFONT hFont,
                              HICON hIcon, LPCSTR str, UINT uFlags)
{
    LPWSTR strW;
    INT len;
    BOOL ret = FALSE;
290

291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
    if (!(uFlags & DC_TEXT) || !str)
        return DrawCaptionTempW( hwnd, hdc, rect, hFont, hIcon, NULL, uFlags );

    len = MultiByteToWideChar( CP_ACP, 0, str, -1, NULL, 0 );
    if ((strW = HeapAlloc( GetProcessHeap(), 0, len * sizeof(WCHAR) )))
    {
        MultiByteToWideChar( CP_ACP, 0, str, -1, strW, len );
        ret = DrawCaptionTempW (hwnd, hdc, rect, hFont, hIcon, strW, uFlags);
        HeapFree( GetProcessHeap (), 0, strW );
    }
    return ret;
}


/***********************************************************************
 *		DrawCaptionTempW (USER32.@)
 */
BOOL WINAPI DrawCaptionTempW (HWND hwnd, HDC hdc, const RECT *rect, HFONT hFont,
                              HICON hIcon, LPCWSTR str, UINT uFlags)
310
{
311
    RECT   rc = *rect;
312

313
    TRACE("(%p,%p,%p,%p,%p,%s,%08x)\n",
314
          hwnd, hdc, rect, hFont, hIcon, debugstr_w(str), uFlags);
315 316 317

    /* drawing background */
    if (uFlags & DC_INBUTTON) {
318 319 320 321 322 323 324 325
        FillRect (hdc, &rc, GetSysColorBrush (COLOR_3DFACE));

        if (uFlags & DC_ACTIVE) {
            HBRUSH hbr = SelectObject (hdc, SYSCOLOR_55AABrush);
            PatBlt (hdc, rc.left, rc.top,
                      rc.right-rc.left, rc.bottom-rc.top, 0xFA0089);
            SelectObject (hdc, hbr);
        }
326 327
    }
    else {
328 329
        DWORD style = GetWindowLongW (hwnd, GWL_STYLE);
        NC_DrawCaptionBar (hdc, rect, style, uFlags & DC_ACTIVE, uFlags & DC_GRADIENT);
330 331 332 333 334
    }


    /* drawing icon */
    if ((uFlags & DC_ICON) && !(uFlags & DC_SMALLCAP)) {
335
        POINT pt;
336

337 338
        pt.x = rc.left + 2;
        pt.y = (rc.bottom + rc.top - GetSystemMetrics(SM_CYSMICON)) / 2;
339

340 341 342
        if (!hIcon) hIcon = NC_IconForWindow(hwnd);
        DrawIconEx (hdc, pt.x, pt.y, hIcon, GetSystemMetrics(SM_CXSMICON),
                    GetSystemMetrics(SM_CYSMICON), 0, 0, DI_NORMAL);
343
        rc.left += (rc.bottom - rc.top);
344 345 346 347
    }

    /* drawing text */
    if (uFlags & DC_TEXT) {
348
        HFONT hOldFont;
349

350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
        if (uFlags & DC_INBUTTON)
            SetTextColor (hdc, GetSysColor (COLOR_BTNTEXT));
        else if (uFlags & DC_ACTIVE)
            SetTextColor (hdc, GetSysColor (COLOR_CAPTIONTEXT));
        else
            SetTextColor (hdc, GetSysColor (COLOR_INACTIVECAPTIONTEXT));

        SetBkMode (hdc, TRANSPARENT);

        if (hFont)
            hOldFont = SelectObject (hdc, hFont);
        else {
            NONCLIENTMETRICSW nclm;
            HFONT hNewFont;
            nclm.cbSize = sizeof(NONCLIENTMETRICSW);
            SystemParametersInfoW (SPI_GETNONCLIENTMETRICS, 0, &nclm, 0);
            hNewFont = CreateFontIndirectW ((uFlags & DC_SMALLCAP) ?
                &nclm.lfSmCaptionFont : &nclm.lfCaptionFont);
            hOldFont = SelectObject (hdc, hNewFont);
        }

        if (str)
            DrawTextW (hdc, str, -1, &rc,
                         DT_SINGLELINE | DT_VCENTER | DT_NOPREFIX | DT_LEFT);
        else {
            WCHAR szText[128];
            INT nLen;
            nLen = GetWindowTextW (hwnd, szText, 128);
            DrawTextW (hdc, szText, nLen, &rc,
                         DT_SINGLELINE | DT_VCENTER | DT_NOPREFIX | DT_LEFT);
        }

        if (hFont)
            SelectObject (hdc, hOldFont);
        else
            DeleteObject (SelectObject (hdc, hOldFont));
386 387 388 389
    }

    /* drawing focus ??? */
    if (uFlags & 0x2000)
390
        FIXME("undocumented flag (0x2000)!\n");
391 392 393 394 395

    return 0;
}


Alexandre Julliard's avatar
Alexandre Julliard committed
396
/***********************************************************************
397
 *		AdjustWindowRect (USER32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
398
 */
399
BOOL WINAPI AdjustWindowRect( LPRECT rect, DWORD style, BOOL menu )
Alexandre Julliard's avatar
Alexandre Julliard committed
400
{
401
    return AdjustWindowRectEx( rect, style, menu, 0 );
Alexandre Julliard's avatar
Alexandre Julliard committed
402 403 404
}


405 406 407 408 409 410
/***********************************************************************
 *		AdjustWindowRectEx (USER32.@)
 */
BOOL WINAPI AdjustWindowRectEx( LPRECT rect, DWORD style, BOOL menu, DWORD exStyle )
{
    /* Correct the window style */
Alexandre Julliard's avatar
Alexandre Julliard committed
411
    style &= (WS_DLGFRAME | WS_BORDER | WS_THICKFRAME | WS_CHILD);
Alexandre Julliard's avatar
Alexandre Julliard committed
412
    exStyle &= (WS_EX_DLGMODALFRAME | WS_EX_CLIENTEDGE |
413
                WS_EX_STATICEDGE | WS_EX_TOOLWINDOW);
Alexandre Julliard's avatar
Alexandre Julliard committed
414 415
    if (exStyle & WS_EX_DLGMODALFRAME) style &= ~WS_THICKFRAME;

416
    TRACE("(%ld,%ld)-(%ld,%ld) %08lx %d %08lx\n",
417 418
          rect->left, rect->top, rect->right, rect->bottom,
          style, menu, exStyle );
Alexandre Julliard's avatar
Alexandre Julliard committed
419

420 421 422
    NC_AdjustRectOuter( rect, style, menu, exStyle );
    NC_AdjustRectInner( rect, style, exStyle );

Alexandre Julliard's avatar
Alexandre Julliard committed
423
    return TRUE;
Alexandre Julliard's avatar
Alexandre Julliard committed
424 425 426 427 428 429 430 431
}


/***********************************************************************
 *           NC_HandleNCCalcSize
 *
 * Handle a WM_NCCALCSIZE message. Called from DefWindowProc().
 */
432
LRESULT NC_HandleNCCalcSize( HWND hwnd, RECT *winRect )
Alexandre Julliard's avatar
Alexandre Julliard committed
433
{
434
    RECT tmpRect = { 0, 0, 0, 0 };
435
    LRESULT result = 0;
436 437 438
    LONG cls_style = GetClassLongW(hwnd, GCL_STYLE);
    LONG style = GetWindowLongW( hwnd, GWL_STYLE );
    LONG exStyle = GetWindowLongW( hwnd, GWL_EXSTYLE );
Alexandre Julliard's avatar
Alexandre Julliard committed
439

440 441
    if (cls_style & CS_VREDRAW) result |= WVR_VREDRAW;
    if (cls_style & CS_HREDRAW) result |= WVR_HREDRAW;
Alexandre Julliard's avatar
Alexandre Julliard committed
442

443 444
    if (!IsIconic(hwnd))
    {
445
        NC_AdjustRectOuter( &tmpRect, style, FALSE, exStyle );
Alexandre Julliard's avatar
Alexandre Julliard committed
446

447 448 449 450
        winRect->left   -= tmpRect.left;
        winRect->top    -= tmpRect.top;
        winRect->right  -= tmpRect.right;
        winRect->bottom -= tmpRect.bottom;
Alexandre Julliard's avatar
Alexandre Julliard committed
451

452
        if (((style & (WS_CHILD | WS_POPUP)) != WS_CHILD) && GetMenu(hwnd))
453
        {
454
            TRACE("Calling GetMenuBarHeight with hwnd %p, width %ld, at (%ld, %ld).\n",
455
                  hwnd, winRect->right - winRect->left, -tmpRect.left, -tmpRect.top );
Alexandre Julliard's avatar
Alexandre Julliard committed
456

457 458 459
            winRect->top +=
                MENU_GetMenuBarHeight( hwnd,
                                       winRect->right - winRect->left,
460
                                       -tmpRect.left, -tmpRect.top );
461
        }
Alexandre Julliard's avatar
Alexandre Julliard committed
462

463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479
        if( exStyle & WS_EX_CLIENTEDGE)
            if( winRect->right - winRect->left > 2 * GetSystemMetrics(SM_CXEDGE) &&
                   winRect->bottom - winRect->top > 2 * GetSystemMetrics(SM_CYEDGE))
                InflateRect( winRect, - GetSystemMetrics(SM_CXEDGE),
                        - GetSystemMetrics(SM_CYEDGE));

        if (style & WS_VSCROLL)
            if( winRect->right - winRect->left >= GetSystemMetrics(SM_CXVSCROLL)){
                if((exStyle & WS_EX_LEFTSCROLLBAR) != 0)
                    winRect->left  += GetSystemMetrics(SM_CXVSCROLL);
                else
                    winRect->right -= GetSystemMetrics(SM_CXVSCROLL);
            }

        if (style & WS_HSCROLL)
            if( winRect->bottom - winRect->top > GetSystemMetrics(SM_CYHSCROLL))
                    winRect->bottom -= GetSystemMetrics(SM_CYHSCROLL);
480 481 482 483 484 485

        if (winRect->top > winRect->bottom)
            winRect->bottom = winRect->top;

        if (winRect->left > winRect->right)
            winRect->right = winRect->left;
Alexandre Julliard's avatar
Alexandre Julliard committed
486
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
487
    return result;
Alexandre Julliard's avatar
Alexandre Julliard committed
488 489 490 491
}


/***********************************************************************
Alexandre Julliard's avatar
Alexandre Julliard committed
492
 *           NC_GetInsideRect
Alexandre Julliard's avatar
Alexandre Julliard committed
493
 *
Alexandre Julliard's avatar
Alexandre Julliard committed
494 495 496
 * Get the 'inside' rectangle of a window, i.e. the whole window rectangle
 * but without the borders (if any).
 * The rectangle is in window coordinates (for drawing with GetWindowDC()).
Alexandre Julliard's avatar
Alexandre Julliard committed
497
 */
498
static void NC_GetInsideRect( HWND hwnd, RECT *rect )
Alexandre Julliard's avatar
Alexandre Julliard committed
499
{
500 501
    WND *wndPtr = WIN_GetPtr( hwnd );

502
    if (!wndPtr || wndPtr == WND_OTHER_PROCESS || wndPtr == WND_DESKTOP) return;
Alexandre Julliard's avatar
Alexandre Julliard committed
503 504 505 506 507

    rect->top    = rect->left = 0;
    rect->right  = wndPtr->rectWindow.right - wndPtr->rectWindow.left;
    rect->bottom = wndPtr->rectWindow.bottom - wndPtr->rectWindow.top;

508
    if (wndPtr->dwStyle & WS_ICONIC) goto END;
Alexandre Julliard's avatar
Alexandre Julliard committed
509

Alexandre Julliard's avatar
Alexandre Julliard committed
510
    /* Remove frame from rectangle */
511
    if (HAS_THICKFRAME( wndPtr->dwStyle, wndPtr->dwExStyle ))
Alexandre Julliard's avatar
Alexandre Julliard committed
512
    {
513
        InflateRect( rect, -GetSystemMetrics(SM_CXFRAME), -GetSystemMetrics(SM_CYFRAME) );
Alexandre Julliard's avatar
Alexandre Julliard committed
514
    }
515
    else if (HAS_DLGFRAME( wndPtr->dwStyle, wndPtr->dwExStyle ))
Alexandre Julliard's avatar
Alexandre Julliard committed
516
    {
517
        InflateRect( rect, -GetSystemMetrics(SM_CXDLGFRAME), -GetSystemMetrics(SM_CYDLGFRAME));
Alexandre Julliard's avatar
Alexandre Julliard committed
518
    }
519
    else if (HAS_THINFRAME( wndPtr->dwStyle ))
Alexandre Julliard's avatar
Alexandre Julliard committed
520
    {
521
        InflateRect( rect, -GetSystemMetrics(SM_CXBORDER), -GetSystemMetrics(SM_CYBORDER) );
Alexandre Julliard's avatar
Alexandre Julliard committed
522 523
    }

524 525
    /* We have additional border information if the window
     * is a child (but not an MDI child) */
526 527
    if ( (wndPtr->dwStyle & WS_CHILD)  &&
         ( (wndPtr->dwExStyle & WS_EX_MDICHILD) == 0 ) )
528
    {
529 530 531 532
        if (wndPtr->dwExStyle & WS_EX_CLIENTEDGE)
            InflateRect (rect, -GetSystemMetrics(SM_CXEDGE), -GetSystemMetrics(SM_CYEDGE));
        if (wndPtr->dwExStyle & WS_EX_STATICEDGE)
            InflateRect (rect, -GetSystemMetrics(SM_CXBORDER), -GetSystemMetrics(SM_CYBORDER));
Alexandre Julliard's avatar
Alexandre Julliard committed
533
    }
534

535
END:
536
    WIN_ReleasePtr( wndPtr );
Alexandre Julliard's avatar
Alexandre Julliard committed
537 538 539
}


Alexandre Julliard's avatar
Alexandre Julliard committed
540
/***********************************************************************
Alexandre Julliard's avatar
Alexandre Julliard committed
541
 * NC_DoNCHitTest
Alexandre Julliard's avatar
Alexandre Julliard committed
542
 *
Andreas Mohr's avatar
Andreas Mohr committed
543
 * Handle a WM_NCHITTEST message. Called from NC_HandleNCHitTest().
Alexandre Julliard's avatar
Alexandre Julliard committed
544
 *
Alexandre Julliard's avatar
Alexandre Julliard committed
545
 * FIXME:  Just a modified copy of the Win 3.1 version.
Alexandre Julliard's avatar
Alexandre Julliard committed
546 547
 */

548
static LRESULT NC_DoNCHitTest (WND *wndPtr, POINT pt )
Alexandre Julliard's avatar
Alexandre Julliard committed
549
{
550 551
    RECT rect, rcClient;
    POINT ptClient;
Alexandre Julliard's avatar
Alexandre Julliard committed
552

553
    TRACE("hwnd=%p pt=%ld,%ld\n", wndPtr->hwndSelf, pt.x, pt.y );
Alexandre Julliard's avatar
Alexandre Julliard committed
554

555 556
    GetWindowRect(wndPtr->hwndSelf, &rect );
    if (!PtInRect( &rect, pt )) return HTNOWHERE;
Alexandre Julliard's avatar
Alexandre Julliard committed
557 558 559

    if (wndPtr->dwStyle & WS_MINIMIZE) return HTCAPTION;

560 561 562 563 564 565
    /* Check client area */
    ptClient = pt;
    ScreenToClient( wndPtr->hwndSelf, &ptClient );
    GetClientRect( wndPtr->hwndSelf, &rcClient );
    if (PtInRect( &rcClient, ptClient )) return HTCLIENT;

566 567
    /* Check borders */
    if (HAS_THICKFRAME( wndPtr->dwStyle, wndPtr->dwExStyle ))
Alexandre Julliard's avatar
Alexandre Julliard committed
568
    {
569 570
        InflateRect( &rect, -GetSystemMetrics(SM_CXFRAME), -GetSystemMetrics(SM_CYFRAME) );
        if (!PtInRect( &rect, pt ))
Alexandre Julliard's avatar
Alexandre Julliard committed
571
        {
572 573
            /* Check top sizing border */
            if (pt.y < rect.top)
Alexandre Julliard's avatar
Alexandre Julliard committed
574
            {
575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598
                if (pt.x < rect.left+GetSystemMetrics(SM_CXSIZE)) return HTTOPLEFT;
                if (pt.x >= rect.right-GetSystemMetrics(SM_CXSIZE)) return HTTOPRIGHT;
                return HTTOP;
            }
            /* Check bottom sizing border */
            if (pt.y >= rect.bottom)
            {
                if (pt.x < rect.left+GetSystemMetrics(SM_CXSIZE)) return HTBOTTOMLEFT;
                if (pt.x >= rect.right-GetSystemMetrics(SM_CXSIZE)) return HTBOTTOMRIGHT;
                return HTBOTTOM;
            }
            /* Check left sizing border */
            if (pt.x < rect.left)
            {
                if (pt.y < rect.top+GetSystemMetrics(SM_CYSIZE)) return HTTOPLEFT;
                if (pt.y >= rect.bottom-GetSystemMetrics(SM_CYSIZE)) return HTBOTTOMLEFT;
                return HTLEFT;
            }
            /* Check right sizing border */
            if (pt.x >= rect.right)
            {
                if (pt.y < rect.top+GetSystemMetrics(SM_CYSIZE)) return HTTOPRIGHT;
                if (pt.y >= rect.bottom-GetSystemMetrics(SM_CYSIZE)) return HTBOTTOMRIGHT;
                return HTRIGHT;
Alexandre Julliard's avatar
Alexandre Julliard committed
599 600
            }
        }
601 602 603 604 605 606 607 608 609
    }
    else  /* No thick frame */
    {
        if (HAS_DLGFRAME( wndPtr->dwStyle, wndPtr->dwExStyle ))
            InflateRect(&rect, -GetSystemMetrics(SM_CXDLGFRAME), -GetSystemMetrics(SM_CYDLGFRAME));
        else if (HAS_THINFRAME( wndPtr->dwStyle ))
            InflateRect(&rect, -GetSystemMetrics(SM_CXBORDER), -GetSystemMetrics(SM_CYBORDER));
        if (!PtInRect( &rect, pt )) return HTBORDER;
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
610

611
    /* Check caption */
Alexandre Julliard's avatar
Alexandre Julliard committed
612

613 614 615 616 617 618 619
    if ((wndPtr->dwStyle & WS_CAPTION) == WS_CAPTION)
    {
        if (wndPtr->dwExStyle & WS_EX_TOOLWINDOW)
            rect.top += GetSystemMetrics(SM_CYSMCAPTION) - 1;
        else
            rect.top += GetSystemMetrics(SM_CYCAPTION) - 1;
        if (!PtInRect( &rect, pt ))
Alexandre Julliard's avatar
Alexandre Julliard committed
620
        {
621 622
            BOOL min_or_max_box = (wndPtr->dwStyle & WS_MAXIMIZEBOX) ||
                                  (wndPtr->dwStyle & WS_MINIMIZEBOX);
623 624
            /* Check system menu */
            if ((wndPtr->dwStyle & WS_SYSMENU) && !(wndPtr->dwExStyle & WS_EX_TOOLWINDOW))
Alexandre Julliard's avatar
Alexandre Julliard committed
625
            {
626 627
                if (NC_IconForWindow(wndPtr->hwndSelf))
                    rect.left += GetSystemMetrics(SM_CYCAPTION) - 1;
Alexandre Julliard's avatar
Alexandre Julliard committed
628
            }
629 630 631 632 633 634 635 636 637
            if (pt.x < rect.left) return HTSYSMENU;

            /* Check close button */
            if (wndPtr->dwStyle & WS_SYSMENU)
                rect.right -= GetSystemMetrics(SM_CYCAPTION) - 1;
            if (pt.x > rect.right) return HTCLOSE;

            /* Check maximize box */
            /* In win95 there is automatically a Maximize button when there is a minimize one*/
638
            if (min_or_max_box && !(wndPtr->dwExStyle & WS_EX_TOOLWINDOW))
639 640 641 642 643
                rect.right -= GetSystemMetrics(SM_CXSIZE) + 1;
            if (pt.x > rect.right) return HTMAXBUTTON;

            /* Check minimize box */
            /* In win95 there is automatically a Maximize button when there is a Maximize one*/
644
            if (min_or_max_box && !(wndPtr->dwExStyle & WS_EX_TOOLWINDOW))
645 646 647 648
                rect.right -= GetSystemMetrics(SM_CXSIZE) + 1;

            if (pt.x > rect.right) return HTMINBUTTON;
            return HTCAPTION;
Alexandre Julliard's avatar
Alexandre Julliard committed
649 650 651
        }
    }

Alexandre Julliard's avatar
Alexandre Julliard committed
652 653 654
      /* Check vertical scroll bar */

    if (wndPtr->dwStyle & WS_VSCROLL)
Alexandre Julliard's avatar
Alexandre Julliard committed
655
    {
656
        if((wndPtr->dwExStyle & WS_EX_LEFTSCROLLBAR) != 0)
657
            rcClient.left -= GetSystemMetrics(SM_CXVSCROLL);
658
        else
659 660
            rcClient.right += GetSystemMetrics(SM_CXVSCROLL);
        if (PtInRect( &rcClient, ptClient )) return HTVSCROLL;
Alexandre Julliard's avatar
Alexandre Julliard committed
661
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
662 663 664 665

      /* Check horizontal scroll bar */

    if (wndPtr->dwStyle & WS_HSCROLL)
Alexandre Julliard's avatar
Alexandre Julliard committed
666
    {
667 668 669 670 671 672 673 674 675 676
        rcClient.bottom += GetSystemMetrics(SM_CYHSCROLL);
        if (PtInRect( &rcClient, ptClient ))
        {
            /* Check size box */
            if ((wndPtr->dwStyle & WS_VSCROLL) &&
                ((((wndPtr->dwExStyle & WS_EX_LEFTSCROLLBAR) != 0) && (ptClient.x <= rcClient.left + GetSystemMetrics(SM_CXVSCROLL))) ||
                (((wndPtr->dwExStyle & WS_EX_LEFTSCROLLBAR) == 0) && (ptClient.x >= rcClient.right - GetSystemMetrics(SM_CXVSCROLL)))))
                return HTSIZE;
            return HTHSCROLL;
        }
Alexandre Julliard's avatar
Alexandre Julliard committed
677 678
    }

Alexandre Julliard's avatar
Alexandre Julliard committed
679 680 681
      /* Check menu bar */

    if (HAS_MENU(wndPtr))
Alexandre Julliard's avatar
Alexandre Julliard committed
682
    {
683 684
        if ((ptClient.y < 0) && (ptClient.x >= 0) && (ptClient.x < rcClient.right))
            return HTMENU;
Alexandre Julliard's avatar
Alexandre Julliard committed
685 686
    }

687
    /* Has to return HTNOWHERE if nothing was found
688 689
       Could happen when a window has a customized non client area */
    return HTNOWHERE;
Alexandre Julliard's avatar
Alexandre Julliard committed
690 691 692
}


Alexandre Julliard's avatar
Alexandre Julliard committed
693 694 695 696 697
/***********************************************************************
 * NC_HandleNCHitTest
 *
 * Handle a WM_NCHITTEST message. Called from DefWindowProc().
 */
698
LRESULT NC_HandleNCHitTest (HWND hwnd , POINT pt)
Alexandre Julliard's avatar
Alexandre Julliard committed
699
{
700
    LRESULT retvalue;
701
    WND *wndPtr = WIN_GetPtr( hwnd );
Alexandre Julliard's avatar
Alexandre Julliard committed
702

703
    if (!wndPtr || wndPtr == WND_OTHER_PROCESS || wndPtr == WND_DESKTOP) return HTERROR;
Alexandre Julliard's avatar
Alexandre Julliard committed
704

705
    retvalue = NC_DoNCHitTest (wndPtr, pt);
706
    WIN_ReleasePtr( wndPtr );
707
    return retvalue;
Alexandre Julliard's avatar
Alexandre Julliard committed
708 709 710
}


Alexandre Julliard's avatar
Alexandre Julliard committed
711 712
/******************************************************************************
 *
713
 *   NC_DrawSysButton
Alexandre Julliard's avatar
Alexandre Julliard committed
714
 *
715
 *   Draws the system icon.
Alexandre Julliard's avatar
Alexandre Julliard committed
716 717
 *
 *****************************************************************************/
718
BOOL NC_DrawSysButton (HWND hwnd, HDC hdc, BOOL down)
Alexandre Julliard's avatar
Alexandre Julliard committed
719
{
720
    HICON hIcon = NC_IconForWindow( hwnd );
Alexandre Julliard's avatar
Alexandre Julliard committed
721

722
    if (hIcon)
Alexandre Julliard's avatar
Alexandre Julliard committed
723
    {
724 725
        RECT rect;
        NC_GetInsideRect( hwnd, &rect );
726 727 728
        DrawIconEx (hdc, rect.left + 2, rect.top + 1, hIcon,
                    GetSystemMetrics(SM_CXSMICON),
                    GetSystemMetrics(SM_CYSMICON), 0, 0, DI_NORMAL);
Alexandre Julliard's avatar
Alexandre Julliard committed
729
    }
730
    return (hIcon != 0);
Alexandre Julliard's avatar
Alexandre Julliard committed
731 732 733 734 735
}


/******************************************************************************
 *
736
 *   NC_DrawCloseButton
Alexandre Julliard's avatar
Alexandre Julliard committed
737
 *
738
 *   Draws the close button.
Alexandre Julliard's avatar
Alexandre Julliard committed
739
 *
740 741
 *   If bGrayed is true, then draw a disabled Close button
 *
Alexandre Julliard's avatar
Alexandre Julliard committed
742 743
 *****************************************************************************/

744
static void NC_DrawCloseButton (HWND hwnd, HDC hdc, BOOL down, BOOL bGrayed)
Alexandre Julliard's avatar
Alexandre Julliard committed
745
{
746
    RECT rect;
Alexandre Julliard's avatar
Alexandre Julliard committed
747

748 749 750
    NC_GetInsideRect( hwnd, &rect );

    /* A tool window has a smaller Close button */
751
    if (GetWindowLongW( hwnd, GWL_EXSTYLE ) & WS_EX_TOOLWINDOW)
Alexandre Julliard's avatar
Alexandre Julliard committed
752
    {
753 754 755
        INT iBmpHeight = 11; /* Windows does not use SM_CXSMSIZE and SM_CYSMSIZE   */
        INT iBmpWidth = 11;  /* it uses 11x11 for  the close button in tool window */
        INT iCaptionHeight = GetSystemMetrics(SM_CYSMCAPTION);
Alexandre Julliard's avatar
Alexandre Julliard committed
756

757 758 759 760
        rect.top = rect.top + (iCaptionHeight - 1 - iBmpHeight) / 2;
        rect.left = rect.right - (iCaptionHeight + 1 + iBmpWidth) / 2;
        rect.bottom = rect.top + iBmpHeight;
        rect.right = rect.left + iBmpWidth;
Alexandre Julliard's avatar
Alexandre Julliard committed
761
    }
762 763 764 765 766 767 768 769 770 771 772
    else
    {
        rect.left = rect.right - GetSystemMetrics(SM_CXSIZE) - 1;
        rect.bottom = rect.top + GetSystemMetrics(SM_CYSIZE) - 1;
        rect.top += 2;
        rect.right -= 2;
    }
    DrawFrameControl( hdc, &rect, DFC_CAPTION,
                      (DFCS_CAPTIONCLOSE |
                       (down ? DFCS_PUSHED : 0) |
                       (bGrayed ? DFCS_INACTIVE : 0)) );
Alexandre Julliard's avatar
Alexandre Julliard committed
773 774 775
}

/******************************************************************************
776
 *   NC_DrawMaxButton
Alexandre Julliard's avatar
Alexandre Julliard committed
777
 *
778
 *   Draws the maximize button for windows.
779
 *   If bGrayed is true, then draw a disabled Maximize button
780
 */
781
static void NC_DrawMaxButton(HWND hwnd,HDC hdc,BOOL down, BOOL bGrayed)
Alexandre Julliard's avatar
Alexandre Julliard committed
782
{
783
    RECT rect;
784 785 786 787 788 789 790
    UINT flags;

    /* never draw maximize box when window has WS_EX_TOOLWINDOW style */
    if (GetWindowLongW( hwnd, GWL_EXSTYLE ) & WS_EX_TOOLWINDOW)
        return;

    flags = IsZoomed(hwnd) ? DFCS_CAPTIONRESTORE : DFCS_CAPTIONMAX;
791 792

    NC_GetInsideRect( hwnd, &rect );
793
    if (GetWindowLongW( hwnd, GWL_STYLE) & WS_SYSMENU)
794 795 796 797 798 799 800 801
        rect.right -= GetSystemMetrics(SM_CXSIZE) + 1;
    rect.left = rect.right - GetSystemMetrics(SM_CXSIZE);
    rect.bottom = rect.top + GetSystemMetrics(SM_CYSIZE) - 1;
    rect.top += 2;
    rect.right -= 2;
    if (down) flags |= DFCS_PUSHED;
    if (bGrayed) flags |= DFCS_INACTIVE;
    DrawFrameControl( hdc, &rect, DFC_CAPTION, flags );
Alexandre Julliard's avatar
Alexandre Julliard committed
802 803 804
}

/******************************************************************************
805
 *   NC_DrawMinButton
Alexandre Julliard's avatar
Alexandre Julliard committed
806
 *
807
 *   Draws the minimize button for windows.
808
 *   If bGrayed is true, then draw a disabled Minimize button
809
 */
810
static void  NC_DrawMinButton(HWND hwnd,HDC hdc,BOOL down, BOOL bGrayed)
Alexandre Julliard's avatar
Alexandre Julliard committed
811
{
812
    RECT rect;
813
    UINT flags = DFCS_CAPTIONMIN;
814
    DWORD style = GetWindowLongW( hwnd, GWL_STYLE );
Alexandre Julliard's avatar
Alexandre Julliard committed
815

816 817 818 819
    /* never draw minimize box when window has WS_EX_TOOLWINDOW style */
    if (GetWindowLongW( hwnd, GWL_EXSTYLE ) & WS_EX_TOOLWINDOW)
        return;

820 821 822 823 824 825 826 827 828 829 830 831
    NC_GetInsideRect( hwnd, &rect );
    if (style & WS_SYSMENU)
        rect.right -= GetSystemMetrics(SM_CXSIZE) + 1;
    if (style & (WS_MAXIMIZEBOX|WS_MINIMIZEBOX))
        rect.right -= GetSystemMetrics(SM_CXSIZE) - 2;
    rect.left = rect.right - GetSystemMetrics(SM_CXSIZE);
    rect.bottom = rect.top + GetSystemMetrics(SM_CYSIZE) - 1;
    rect.top += 2;
    rect.right -= 2;
    if (down) flags |= DFCS_PUSHED;
    if (bGrayed) flags |= DFCS_INACTIVE;
    DrawFrameControl( hdc, &rect, DFC_CAPTION, flags );
Alexandre Julliard's avatar
Alexandre Julliard committed
832 833 834 835
}

/******************************************************************************
 *
836
 *   NC_DrawFrame
Alexandre Julliard's avatar
Alexandre Julliard committed
837 838 839 840 841
 *
 *   Draw a window frame inside the given rectangle, and update the rectangle.
 *
 *   Bugs
 *        Many.  First, just what IS a frame in Win95?  Note that the 3D look
842
 *        on the outer edge is handled by NC_DoNCPaint.  As is the inner
Alexandre Julliard's avatar
Alexandre Julliard committed
843 844 845 846 847 848 849
 *        edge.  The inner rectangle just inside the frame is handled by the
 *        Caption code.
 *
 *        In short, for most people, this function should be a nop (unless
 *        you LIKE thick borders in Win95/NT4.0 -- I've been working with
 *        them lately, but just to get this code right).  Even so, it doesn't
 *        appear to be so.  It's being worked on...
850
 *
Alexandre Julliard's avatar
Alexandre Julliard committed
851 852
 *****************************************************************************/

853
static void  NC_DrawFrame( HDC  hdc, RECT  *rect, BOOL  active, DWORD style, DWORD exStyle)
Alexandre Julliard's avatar
Alexandre Julliard committed
854
{
855
    INT width, height;
Alexandre Julliard's avatar
Alexandre Julliard committed
856

857 858
    /* Firstly the "thick" frame */
    if (style & WS_THICKFRAME)
Alexandre Julliard's avatar
Alexandre Julliard committed
859
    {
860 861
        width = GetSystemMetrics(SM_CXFRAME) - GetSystemMetrics(SM_CXDLGFRAME);
        height = GetSystemMetrics(SM_CYFRAME) - GetSystemMetrics(SM_CYDLGFRAME);
862 863

        SelectObject( hdc, GetSysColorBrush(active ? COLOR_ACTIVEBORDER :
864
                                            COLOR_INACTIVEBORDER) );
865 866 867 868 869 870 871 872 873 874 875
        /* Draw frame */
        PatBlt( hdc, rect->left, rect->top,
                  rect->right - rect->left, height, PATCOPY );
        PatBlt( hdc, rect->left, rect->top,
                  width, rect->bottom - rect->top, PATCOPY );
        PatBlt( hdc, rect->left, rect->bottom - 1,
                  rect->right - rect->left, -height, PATCOPY );
        PatBlt( hdc, rect->right - 1, rect->top,
                  -width, rect->bottom - rect->top, PATCOPY );

        InflateRect( rect, -width, -height );
Alexandre Julliard's avatar
Alexandre Julliard committed
876
    }
877 878 879 880

    /* Now the other bit of the frame */
    if ((style & (WS_BORDER|WS_DLGFRAME)) ||
        (exStyle & WS_EX_DLGMODALFRAME))
Alexandre Julliard's avatar
Alexandre Julliard committed
881
    {
882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906
        width = GetSystemMetrics(SM_CXDLGFRAME) - GetSystemMetrics(SM_CXEDGE);
        height = GetSystemMetrics(SM_CYDLGFRAME) - GetSystemMetrics(SM_CYEDGE);
        /* This should give a value of 1 that should also work for a border */

        SelectObject( hdc, GetSysColorBrush(
                      (exStyle & (WS_EX_DLGMODALFRAME|WS_EX_CLIENTEDGE)) ?
                          COLOR_3DFACE :
                      (exStyle & WS_EX_STATICEDGE) ?
                          COLOR_WINDOWFRAME :
                      (style & (WS_DLGFRAME|WS_THICKFRAME)) ?
                          COLOR_3DFACE :
                      /* else */
                          COLOR_WINDOWFRAME));

        /* Draw frame */
        PatBlt( hdc, rect->left, rect->top,
                  rect->right - rect->left, height, PATCOPY );
        PatBlt( hdc, rect->left, rect->top,
                  width, rect->bottom - rect->top, PATCOPY );
        PatBlt( hdc, rect->left, rect->bottom - 1,
                  rect->right - rect->left, -height, PATCOPY );
        PatBlt( hdc, rect->right - 1, rect->top,
                  -width, rect->bottom - rect->top, PATCOPY );

        InflateRect( rect, -width, -height );
Alexandre Julliard's avatar
Alexandre Julliard committed
907 908 909
    }
}

Alexandre Julliard's avatar
Alexandre Julliard committed
910

Alexandre Julliard's avatar
Alexandre Julliard committed
911 912
/******************************************************************************
 *
913
 *   NC_DrawCaption
Alexandre Julliard's avatar
Alexandre Julliard committed
914
 *
915
 *   Draw the window caption for windows.
Alexandre Julliard's avatar
Alexandre Julliard committed
916 917 918 919
 *   The correct pen for the window frame must be selected in the DC.
 *
 *****************************************************************************/

920 921
static void  NC_DrawCaption( HDC  hdc, RECT *rect, HWND hwnd, DWORD  style, 
                             DWORD  exStyle, BOOL active )
Alexandre Julliard's avatar
Alexandre Julliard committed
922
{
923
    RECT  r = *rect;
924
    WCHAR buffer[256];
925
    HPEN  hPrevPen;
926
    HMENU hSysMenu;
927
    BOOL gradient = FALSE;
Alexandre Julliard's avatar
Alexandre Julliard committed
928

929
    hPrevPen = SelectObject( hdc, SYSCOLOR_GetPen(
930 931 932
                     ((exStyle & (WS_EX_STATICEDGE|WS_EX_CLIENTEDGE|
                                 WS_EX_DLGMODALFRAME)) == WS_EX_STATICEDGE) ?
                      COLOR_WINDOWFRAME : COLOR_3DFACE) );
933 934 935
    MoveToEx( hdc, r.left, r.bottom - 1, NULL );
    LineTo( hdc, r.right, r.bottom - 1 );
    SelectObject( hdc, hPrevPen );
Alexandre Julliard's avatar
Alexandre Julliard committed
936 937
    r.bottom--;

938 939
    SystemParametersInfoW (SPI_GETGRADIENTCAPTIONS, 0, &gradient, 0);
    NC_DrawCaptionBar (hdc, rect, style, active, gradient);
Alexandre Julliard's avatar
Alexandre Julliard committed
940

941
    if ((style & WS_SYSMENU) && !(exStyle & WS_EX_TOOLWINDOW)) {
942 943
        if (NC_DrawSysButton (hwnd, hdc, FALSE))
            r.left += GetSystemMetrics(SM_CXSMICON) + 2;
944
    }
945

946
    if (style & WS_SYSMENU)
947
    {
948
        UINT state;
949

950 951 952
        /* Go get the sysmenu */
        hSysMenu = GetSystemMenu(hwnd, FALSE);
        state = GetMenuState(hSysMenu, SC_CLOSE, MF_BYCOMMAND);
953

954 955 956 957
        /* Draw a grayed close button if disabled or if SC_CLOSE is not there */
        NC_DrawCloseButton (hwnd, hdc, FALSE,
                            (state & (MF_DISABLED | MF_GRAYED)) || (state == 0xFFFFFFFF));
        r.right -= GetSystemMetrics(SM_CYCAPTION) - 1;
958

959 960 961 962
        if ((style & WS_MAXIMIZEBOX) || (style & WS_MINIMIZEBOX))
        {
            /* In win95 the two buttons are always there */
            /* But if the menu item is not in the menu they're disabled*/
963

964 965
            NC_DrawMaxButton( hwnd, hdc, FALSE, (!(style & WS_MAXIMIZEBOX)));
            r.right -= GetSystemMetrics(SM_CXSIZE) + 1;
966

967 968 969
            NC_DrawMinButton( hwnd, hdc, FALSE,  (!(style & WS_MINIMIZEBOX)));
            r.right -= GetSystemMetrics(SM_CXSIZE) + 1;
        }
Alexandre Julliard's avatar
Alexandre Julliard committed
970 971
    }

972 973
    if (InternalGetWindowText( hwnd, buffer, sizeof(buffer)/sizeof(WCHAR) ))
    {
974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989
        NONCLIENTMETRICSW nclm;
        HFONT hFont, hOldFont;
        nclm.cbSize = sizeof(nclm);
        SystemParametersInfoW (SPI_GETNONCLIENTMETRICS, 0, &nclm, 0);
        if (exStyle & WS_EX_TOOLWINDOW)
            hFont = CreateFontIndirectW (&nclm.lfSmCaptionFont);
        else
            hFont = CreateFontIndirectW (&nclm.lfCaptionFont);
        hOldFont = SelectObject (hdc, hFont);
        if (active) SetTextColor( hdc, GetSysColor( COLOR_CAPTIONTEXT ) );
        else SetTextColor( hdc, GetSysColor( COLOR_INACTIVECAPTIONTEXT ) );
        SetBkMode( hdc, TRANSPARENT );
        r.left += 2;
        DrawTextW( hdc, buffer, -1, &r,
                     DT_SINGLELINE | DT_VCENTER | DT_NOPREFIX | DT_LEFT );
        DeleteObject (SelectObject (hdc, hOldFont));
Alexandre Julliard's avatar
Alexandre Julliard committed
990 991 992 993 994
    }
}


/******************************************************************************
995
 *   NC_DoNCPaint
Alexandre Julliard's avatar
Alexandre Julliard committed
996
 *
997 998
 *   Paint the non-client area for windows.
 */
999
static void  NC_DoNCPaint( HWND  hwnd, HRGN  clip, BOOL  suppress_menupaint )
Alexandre Julliard's avatar
Alexandre Julliard committed
1000
{
1001
    HDC hdc;
1002
    RECT rfuzz, rect, rectClip;
1003
    BOOL active;
1004 1005 1006
    WND *wndPtr;
    DWORD dwStyle, dwExStyle;
    WORD flags;
1007
    HRGN hrgn;
1008 1009 1010 1011 1012 1013
    RECT rectClient, rectWindow;
    int has_menu;

    if (!(wndPtr = WIN_GetPtr( hwnd )) || wndPtr == WND_OTHER_PROCESS) return;
    has_menu = HAS_MENU(wndPtr);
    dwStyle = wndPtr->dwStyle;
1014
    dwExStyle = wndPtr->dwExStyle;
1015 1016 1017 1018 1019
    flags = wndPtr->flags;
    rectWindow = wndPtr->rectWindow;
    WIN_ReleasePtr( wndPtr );

    if ( dwStyle & WS_MINIMIZE ||
1020
         !WIN_IsWindowDrawable( hwnd, 0 )) return; /* Nothing to do */
Alexandre Julliard's avatar
Alexandre Julliard committed
1021

1022
    active  = flags & WIN_NCACTIVATED;
Alexandre Julliard's avatar
Alexandre Julliard committed
1023

1024
    TRACE("%p %d\n", hwnd, active );
Alexandre Julliard's avatar
Alexandre Julliard committed
1025

1026 1027 1028 1029 1030
    /* MSDN docs are pretty idiotic here, they say app CAN use clipRgn in
       the call to GetDCEx implying that it is allowed not to use it either.
       However, the suggested GetDCEx(    , DCX_WINDOW | DCX_INTERSECTRGN)
       will cause clipRgn to be deleted after ReleaseDC().
       Now, how is the "system" supposed to tell what happened?
1031 1032
     */

1033 1034 1035 1036
    GetClientRect( hwnd, &rectClient );
    MapWindowPoints( hwnd, 0, (POINT *)&rectClient, 2 );
    hrgn = CreateRectRgnIndirect( &rectClient );

1037 1038 1039 1040 1041 1042
    if (clip > (HRGN)1)
    {
        CombineRgn( hrgn, clip, hrgn, RGN_DIFF );
        hdc = GetDCEx( hwnd, hrgn, DCX_USESTYLE | DCX_WINDOW | DCX_INTERSECTRGN );
    }
    else
Alexandre Julliard's avatar
Alexandre Julliard committed
1043
    {
1044
        hdc = GetDCEx( hwnd, hrgn, DCX_USESTYLE | DCX_WINDOW | DCX_EXCLUDERGN );
Alexandre Julliard's avatar
Alexandre Julliard committed
1045 1046
    }

1047 1048
    if (!hdc) return;

Alexandre Julliard's avatar
Alexandre Julliard committed
1049
    rect.top = rect.left = 0;
1050 1051
    rect.right  = rectWindow.right - rectWindow.left;
    rect.bottom = rectWindow.bottom - rectWindow.top;
1052
    GetClipBox( hdc, &rectClip );
1053

1054
    SelectObject( hdc, SYSCOLOR_GetPen(COLOR_WINDOWFRAME) );
Alexandre Julliard's avatar
Alexandre Julliard committed
1055

1056
    if (HAS_STATICOUTERFRAME(dwStyle, dwExStyle)) {
1057
        DrawEdge (hdc, &rect, BDR_SUNKENOUTER, BF_RECT | BF_ADJUST);
1058
    }
1059
    else if (HAS_BIGFRAME( dwStyle, dwExStyle)) {
1060
        DrawEdge (hdc, &rect, EDGE_RAISED, BF_RECT | BF_ADJUST);
1061
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
1062

1063
    NC_DrawFrame(hdc, &rect, active, dwStyle, dwExStyle );
1064

1065
    if ((dwStyle & WS_CAPTION) == WS_CAPTION)
1066 1067
    {
        RECT  r = rect;
1068
        if (dwExStyle & WS_EX_TOOLWINDOW) {
1069 1070
            r.bottom = rect.top + GetSystemMetrics(SM_CYSMCAPTION);
            rect.top += GetSystemMetrics(SM_CYSMCAPTION);
Alexandre Julliard's avatar
Alexandre Julliard committed
1071
        }
1072 1073 1074 1075
        else {
            r.bottom = rect.top + GetSystemMetrics(SM_CYCAPTION);
            rect.top += GetSystemMetrics(SM_CYCAPTION);
        }
1076
        if( IntersectRect( &rfuzz, &r, &rectClip ) )
1077
            NC_DrawCaption(hdc, &r, hwnd, dwStyle, dwExStyle, active);
Alexandre Julliard's avatar
Alexandre Julliard committed
1078 1079
    }

1080
    if (has_menu)
Alexandre Julliard's avatar
Alexandre Julliard committed
1081
    {
1082
	RECT r = rect;
1083
	r.bottom = rect.top + GetSystemMetrics(SM_CYMENU);
1084

1085
	TRACE("Calling DrawMenuBar with rect (%ld, %ld)-(%ld, %ld)\n",
1086
              r.left, r.top, r.right, r.bottom);
Alexandre Julliard's avatar
Alexandre Julliard committed
1087

Alexandre Julliard's avatar
Alexandre Julliard committed
1088
	rect.top += MENU_DrawMenuBar( hdc, &r, hwnd, suppress_menupaint ) + 1;
Alexandre Julliard's avatar
Alexandre Julliard committed
1089 1090
    }

1091
    TRACE("After MenuBar, rect is (%ld, %ld)-(%ld, %ld).\n",
1092
          rect.left, rect.top, rect.right, rect.bottom );
Alexandre Julliard's avatar
Alexandre Julliard committed
1093

1094
    if (dwExStyle & WS_EX_CLIENTEDGE)
1095
	DrawEdge (hdc, &rect, EDGE_SUNKEN, BF_RECT | BF_ADJUST);
Alexandre Julliard's avatar
Alexandre Julliard committed
1096 1097 1098

    /* Draw the scroll-bars */

1099
    if (dwStyle & WS_VSCROLL)
1100
        SCROLL_DrawScrollBar( hwnd, hdc, SB_VERT, TRUE, TRUE );
1101
    if (dwStyle & WS_HSCROLL)
1102
        SCROLL_DrawScrollBar( hwnd, hdc, SB_HORZ, TRUE, TRUE );
Alexandre Julliard's avatar
Alexandre Julliard committed
1103 1104

    /* Draw the "size-box" */
1105
    if ((dwStyle & WS_VSCROLL) && (dwStyle & WS_HSCROLL))
Alexandre Julliard's avatar
Alexandre Julliard committed
1106
    {
1107
        RECT r = rect;
1108 1109 1110 1111
        if((dwExStyle & WS_EX_LEFTSCROLLBAR) != 0)
            r.right = r.left + GetSystemMetrics(SM_CXVSCROLL) + 1;
        else
            r.left = r.right - GetSystemMetrics(SM_CXVSCROLL) + 1;
1112
        r.top  = r.bottom - GetSystemMetrics(SM_CYHSCROLL) + 1;
1113
        FillRect( hdc, &r,  GetSysColorBrush(COLOR_SCROLLBAR) );
1114
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
1115

1116
    ReleaseDC( hwnd, hdc );
Alexandre Julliard's avatar
Alexandre Julliard committed
1117 1118 1119 1120
}



Alexandre Julliard's avatar
Alexandre Julliard committed
1121

Alexandre Julliard's avatar
Alexandre Julliard committed
1122 1123 1124 1125 1126
/***********************************************************************
 *           NC_HandleNCPaint
 *
 * Handle a WM_NCPAINT message. Called from DefWindowProc().
 */
1127
LRESULT NC_HandleNCPaint( HWND hwnd , HRGN clip)
Alexandre Julliard's avatar
Alexandre Julliard committed
1128
{
1129
    DWORD dwStyle = GetWindowLongW( hwnd, GWL_STYLE );
Alexandre Julliard's avatar
Alexandre Julliard committed
1130

1131
    if( dwStyle & WS_VISIBLE )
Alexandre Julliard's avatar
Alexandre Julliard committed
1132
    {
1133
	if( dwStyle & WS_MINIMIZE )
Alexandre Julliard's avatar
Alexandre Julliard committed
1134
	    WINPOS_RedrawIconTitle( hwnd );
Alexandre Julliard's avatar
Alexandre Julliard committed
1135
	else
1136
	    NC_DoNCPaint( hwnd, clip, FALSE );
Alexandre Julliard's avatar
Alexandre Julliard committed
1137
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
1138 1139 1140 1141 1142
    return 0;
}


/***********************************************************************
Alexandre Julliard's avatar
Alexandre Julliard committed
1143
 *           NC_HandleNCActivate
Alexandre Julliard's avatar
Alexandre Julliard committed
1144
 *
Alexandre Julliard's avatar
Alexandre Julliard committed
1145
 * Handle a WM_NCACTIVATE message. Called from DefWindowProc().
Alexandre Julliard's avatar
Alexandre Julliard committed
1146
 */
1147
LRESULT NC_HandleNCActivate( HWND hwnd, WPARAM wParam )
Alexandre Julliard's avatar
Alexandre Julliard committed
1148
{
1149 1150 1151
    WND* wndPtr = WIN_GetPtr( hwnd );

    if (!wndPtr || wndPtr == WND_OTHER_PROCESS) return FALSE;
1152

1153 1154 1155 1156 1157
    /* Lotus Notes draws menu descriptions in the caption of its main
     * window. When it wants to restore original "system" view, it just
     * sends WM_NCACTIVATE message to itself. Any optimizations here in
     * attempt to minimize redrawings lead to a not restored caption.
     */
1158 1159 1160 1161 1162 1163 1164 1165
    if (wParam) wndPtr->flags |= WIN_NCACTIVATED;
    else wndPtr->flags &= ~WIN_NCACTIVATED;
    WIN_ReleasePtr( wndPtr );

    if (IsIconic(hwnd))
        WINPOS_RedrawIconTitle( hwnd );
    else
        NC_DoNCPaint( hwnd, (HRGN)1, FALSE );
Alexandre Julliard's avatar
Alexandre Julliard committed
1166

Alexandre Julliard's avatar
Alexandre Julliard committed
1167 1168
    return TRUE;
}
Alexandre Julliard's avatar
Alexandre Julliard committed
1169

Alexandre Julliard's avatar
Alexandre Julliard committed
1170

Alexandre Julliard's avatar
Alexandre Julliard committed
1171 1172 1173 1174 1175
/***********************************************************************
 *           NC_HandleSetCursor
 *
 * Handle a WM_SETCURSOR message. Called from DefWindowProc().
 */
1176
LRESULT NC_HandleSetCursor( HWND hwnd, WPARAM wParam, LPARAM lParam )
Alexandre Julliard's avatar
Alexandre Julliard committed
1177
{
1178
    hwnd = WIN_GetFullHandle( (HWND)wParam );
Alexandre Julliard's avatar
Alexandre Julliard committed
1179

1180
    switch((short)LOWORD(lParam))
Alexandre Julliard's avatar
Alexandre Julliard committed
1181 1182
    {
    case HTERROR:
1183 1184 1185 1186 1187 1188 1189
        {
            WORD msg = HIWORD( lParam );
            if ((msg == WM_LBUTTONDOWN) || (msg == WM_MBUTTONDOWN) ||
                (msg == WM_RBUTTONDOWN) || (msg == WM_XBUTTONDOWN))
                MessageBeep(0);
        }
        break;
Alexandre Julliard's avatar
Alexandre Julliard committed
1190 1191

    case HTCLIENT:
1192 1193 1194 1195 1196 1197
        {
            HCURSOR hCursor = (HCURSOR)GetClassLongPtrW(hwnd, GCLP_HCURSOR);
            if(hCursor) {
                SetCursor(hCursor);
                return TRUE;
            }
1198
            return FALSE;
1199
        }
Alexandre Julliard's avatar
Alexandre Julliard committed
1200 1201 1202

    case HTLEFT:
    case HTRIGHT:
1203
        return (LRESULT)SetCursor( LoadCursorA( 0, (LPSTR)IDC_SIZEWE ) );
Alexandre Julliard's avatar
Alexandre Julliard committed
1204 1205 1206

    case HTTOP:
    case HTBOTTOM:
1207
        return (LRESULT)SetCursor( LoadCursorA( 0, (LPSTR)IDC_SIZENS ) );
Alexandre Julliard's avatar
Alexandre Julliard committed
1208 1209

    case HTTOPLEFT:
1210
    case HTBOTTOMRIGHT:
1211
        return (LRESULT)SetCursor( LoadCursorA( 0, (LPSTR)IDC_SIZENWSE ) );
Alexandre Julliard's avatar
Alexandre Julliard committed
1212 1213 1214

    case HTTOPRIGHT:
    case HTBOTTOMLEFT:
1215
        return (LRESULT)SetCursor( LoadCursorA( 0, (LPSTR)IDC_SIZENESW ) );
Alexandre Julliard's avatar
Alexandre Julliard committed
1216 1217 1218
    }

    /* Default cursor: arrow */
1219
    return (LRESULT)SetCursor( LoadCursorA( 0, (LPSTR)IDC_ARROW ) );
Alexandre Julliard's avatar
Alexandre Julliard committed
1220 1221
}

Alexandre Julliard's avatar
Alexandre Julliard committed
1222 1223 1224
/***********************************************************************
 *           NC_GetSysPopupPos
 */
1225
void NC_GetSysPopupPos( HWND hwnd, RECT* rect )
Alexandre Julliard's avatar
Alexandre Julliard committed
1226
{
1227 1228 1229
    if (IsIconic(hwnd)) GetWindowRect( hwnd, rect );
    else
    {
1230
        WND *wndPtr = WIN_GetPtr( hwnd );
1231
        if (!wndPtr || wndPtr == WND_OTHER_PROCESS || wndPtr == WND_DESKTOP) return;
1232 1233 1234 1235

        NC_GetInsideRect( hwnd, rect );
        OffsetRect( rect, wndPtr->rectWindow.left, wndPtr->rectWindow.top);
        if (wndPtr->dwStyle & WS_CHILD)
1236
            ClientToScreen( GetParent(hwnd), (POINT *)rect );
1237 1238
        rect->right = rect->left + GetSystemMetrics(SM_CYCAPTION) - 1;
        rect->bottom = rect->top + GetSystemMetrics(SM_CYCAPTION) - 1;
1239
        WIN_ReleasePtr( wndPtr );
1240
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
1241
}
Alexandre Julliard's avatar
Alexandre Julliard committed
1242

1243
/***********************************************************************
1244
 *           NC_TrackMinMaxBox
1245 1246 1247 1248 1249 1250 1251 1252
 *
 * Track a mouse button press on the minimize or maximize box.
 *
 * The big difference between 3.1 and 95 is the disabled button state.
 * In win95 the system button can be disabled, so it can ignore the mouse
 * event.
 *
 */
1253
static void NC_TrackMinMaxBox( HWND hwnd, WORD wParam )
1254 1255 1256 1257 1258
{
    MSG msg;
    HDC hdc = GetWindowDC( hwnd );
    BOOL pressed = TRUE;
    UINT state;
1259
    DWORD wndStyle = GetWindowLongW( hwnd, GWL_STYLE);
1260 1261
    HMENU hSysMenu = GetSystemMenu(hwnd, FALSE);

1262
    void  (*paintButton)(HWND, HDC, BOOL, BOOL);
1263 1264 1265

    if (wParam == HTMINBUTTON)
    {
1266 1267 1268
        /* If the style is not present, do nothing */
        if (!(wndStyle & WS_MINIMIZEBOX))
            return;
1269

1270 1271
        /* Check if the sysmenu item for minimize is there  */
        state = GetMenuState(hSysMenu, SC_MINIMIZE, MF_BYCOMMAND);
1272

1273
        paintButton = &NC_DrawMinButton;
1274 1275 1276
    }
    else
    {
1277 1278 1279
        /* If the style is not present, do nothing */
        if (!(wndStyle & WS_MAXIMIZEBOX))
            return;
1280

1281 1282
        /* Check if the sysmenu item for maximize is there  */
        state = GetMenuState(hSysMenu, SC_MAXIMIZE, MF_BYCOMMAND);
1283

1284
        paintButton = &NC_DrawMaxButton;
1285 1286 1287 1288 1289 1290
    }

    SetCapture( hwnd );

    (*paintButton)( hwnd, hdc, TRUE, FALSE);

1291
    while(1)
1292
    {
1293
        BOOL oldstate = pressed;
1294 1295 1296

        if (!GetMessageW( &msg, 0, WM_MOUSEFIRST, WM_MOUSELAST )) break;
        if (CallMsgFilterW( &msg, MSGF_MAX )) continue;
1297

1298 1299
        if(msg.message == WM_LBUTTONUP)
            break;
1300

1301 1302
        if(msg.message != WM_MOUSEMOVE)
            continue;
1303

1304 1305 1306
        pressed = (NC_HandleNCHitTest( hwnd, msg.pt ) == wParam);
        if (pressed != oldstate)
           (*paintButton)( hwnd, hdc, pressed, FALSE);
1307
    }
1308

1309
    if(pressed)
1310
        (*paintButton)(hwnd, hdc, FALSE, FALSE);
1311 1312 1313 1314 1315 1316 1317

    ReleaseCapture();
    ReleaseDC( hwnd, hdc );

    /* If the item minimize or maximize of the sysmenu are not there */
    /* or if the style is not present, do nothing */
    if ((!pressed) || (state == 0xFFFFFFFF))
1318
        return;
1319

1320
    if (wParam == HTMINBUTTON)
1321
        SendMessageA( hwnd, WM_SYSCOMMAND, SC_MINIMIZE, MAKELONG(msg.pt.x,msg.pt.y) );
1322
    else
1323 1324
        SendMessageA( hwnd, WM_SYSCOMMAND,
                      IsZoomed(hwnd) ? SC_RESTORE:SC_MAXIMIZE, MAKELONG(msg.pt.x,msg.pt.y) );
1325 1326
}

Alexandre Julliard's avatar
Alexandre Julliard committed
1327
/***********************************************************************
1328
 * NC_TrackCloseButton
Alexandre Julliard's avatar
Alexandre Julliard committed
1329 1330 1331
 *
 * Track a mouse button press on the Win95 close button.
 */
1332
static void NC_TrackCloseButton (HWND hwnd, WORD wParam)
Alexandre Julliard's avatar
Alexandre Julliard committed
1333
{
1334
    MSG msg;
1335
    HDC hdc;
1336
    BOOL pressed = TRUE;
1337 1338 1339 1340
    HMENU hSysMenu = GetSystemMenu(hwnd, FALSE);
    UINT state;

    if(hSysMenu == 0)
1341
        return;
1342 1343

    state = GetMenuState(hSysMenu, SC_CLOSE, MF_BYCOMMAND);
1344

1345 1346
    /* If the item close of the sysmenu is disabled or not there do nothing */
    if((state & MF_DISABLED) || (state & MF_GRAYED) || (state == 0xFFFFFFFF))
1347
        return;
1348 1349

    hdc = GetWindowDC( hwnd );
Alexandre Julliard's avatar
Alexandre Julliard committed
1350

1351
    SetCapture( hwnd );
Alexandre Julliard's avatar
Alexandre Julliard committed
1352

1353
    NC_DrawCloseButton (hwnd, hdc, TRUE, FALSE);
Alexandre Julliard's avatar
Alexandre Julliard committed
1354

1355
    while(1)
Alexandre Julliard's avatar
Alexandre Julliard committed
1356
    {
1357
        BOOL oldstate = pressed;
1358 1359 1360

        if (!GetMessageW( &msg, 0, WM_MOUSEFIRST, WM_MOUSELAST )) break;
        if (CallMsgFilterW( &msg, MSGF_MAX )) continue;
Alexandre Julliard's avatar
Alexandre Julliard committed
1361

1362 1363
        if(msg.message == WM_LBUTTONUP)
            break;
1364

1365 1366
        if(msg.message != WM_MOUSEMOVE)
            continue;
1367

1368 1369 1370
        pressed = (NC_HandleNCHitTest( hwnd, msg.pt ) == wParam);
        if (pressed != oldstate)
           NC_DrawCloseButton (hwnd, hdc, pressed, FALSE);
1371
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
1372

1373
    if(pressed)
1374
        NC_DrawCloseButton (hwnd, hdc, FALSE, FALSE);
Alexandre Julliard's avatar
Alexandre Julliard committed
1375 1376

    ReleaseCapture();
1377
    ReleaseDC( hwnd, hdc );
Alexandre Julliard's avatar
Alexandre Julliard committed
1378 1379
    if (!pressed) return;

1380
    SendMessageA( hwnd, WM_SYSCOMMAND, SC_CLOSE, MAKELONG(msg.pt.x,msg.pt.y) );
Alexandre Julliard's avatar
Alexandre Julliard committed
1381 1382 1383
}


Alexandre Julliard's avatar
Alexandre Julliard committed
1384 1385 1386 1387 1388
/***********************************************************************
 *           NC_TrackScrollBar
 *
 * Track a mouse button press on the horizontal or vertical scroll-bar.
 */
1389
static void NC_TrackScrollBar( HWND hwnd, WPARAM wParam, POINT pt )
Alexandre Julliard's avatar
Alexandre Julliard committed
1390
{
1391
    INT scrollbar;
Alexandre Julliard's avatar
Alexandre Julliard committed
1392

Alexandre Julliard's avatar
Alexandre Julliard committed
1393 1394
    if ((wParam & 0xfff0) == SC_HSCROLL)
    {
1395
        if ((wParam & 0x0f) != HTHSCROLL) return;
Alexandre Julliard's avatar
Alexandre Julliard committed
1396 1397 1398 1399
	scrollbar = SB_HORZ;
    }
    else  /* SC_VSCROLL */
    {
1400
        if ((wParam & 0x0f) != HTVSCROLL) return;
Alexandre Julliard's avatar
Alexandre Julliard committed
1401 1402
	scrollbar = SB_VERT;
    }
1403
    SCROLL_TrackScrollBar( hwnd, scrollbar, pt );
Alexandre Julliard's avatar
Alexandre Julliard committed
1404 1405
}

1406

Alexandre Julliard's avatar
Alexandre Julliard committed
1407 1408 1409 1410 1411
/***********************************************************************
 *           NC_HandleNCLButtonDown
 *
 * Handle a WM_NCLBUTTONDOWN message. Called from DefWindowProc().
 */
1412
LRESULT NC_HandleNCLButtonDown( HWND hwnd, WPARAM wParam, LPARAM lParam )
Alexandre Julliard's avatar
Alexandre Julliard committed
1413
{
1414
    LONG style = GetWindowLongW( hwnd, GWL_STYLE );
Alexandre Julliard's avatar
Alexandre Julliard committed
1415

Alexandre Julliard's avatar
Alexandre Julliard committed
1416 1417
    switch(wParam)  /* Hit test */
    {
Alexandre Julliard's avatar
Alexandre Julliard committed
1418
    case HTCAPTION:
1419
        {
1420
            HWND top = GetAncestor( hwnd, GA_ROOT );
Alexandre Julliard's avatar
Alexandre Julliard committed
1421

1422
            if (FOCUS_MouseActivate( top ) || (GetActiveWindow() == top))
1423 1424 1425
                SendMessageW( hwnd, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, lParam );
            break;
        }
Alexandre Julliard's avatar
Alexandre Julliard committed
1426 1427

    case HTSYSMENU:
1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438
         if( style & WS_SYSMENU )
         {
             if( !(style & WS_MINIMIZE) )
             {
                HDC hDC = GetWindowDC(hwnd);
                NC_DrawSysButton( hwnd, hDC, TRUE );
                ReleaseDC( hwnd, hDC );
             }
             SendMessageW( hwnd, WM_SYSCOMMAND, SC_MOUSEMENU + HTSYSMENU, lParam );
         }
         break;
Alexandre Julliard's avatar
Alexandre Julliard committed
1439 1440

    case HTMENU:
1441 1442
        SendMessageW( hwnd, WM_SYSCOMMAND, SC_MOUSEMENU, lParam );
        break;
Alexandre Julliard's avatar
Alexandre Julliard committed
1443

Alexandre Julliard's avatar
Alexandre Julliard committed
1444
    case HTHSCROLL:
1445 1446
        SendMessageW( hwnd, WM_SYSCOMMAND, SC_HSCROLL + HTHSCROLL, lParam );
        break;
Alexandre Julliard's avatar
Alexandre Julliard committed
1447

Alexandre Julliard's avatar
Alexandre Julliard committed
1448
    case HTVSCROLL:
1449 1450
        SendMessageW( hwnd, WM_SYSCOMMAND, SC_VSCROLL + HTVSCROLL, lParam );
        break;
Alexandre Julliard's avatar
Alexandre Julliard committed
1451

Alexandre Julliard's avatar
Alexandre Julliard committed
1452
    case HTMINBUTTON:
Alexandre Julliard's avatar
Alexandre Julliard committed
1453
    case HTMAXBUTTON:
1454 1455
        NC_TrackMinMaxBox( hwnd, wParam );
        break;
Alexandre Julliard's avatar
Alexandre Julliard committed
1456

Alexandre Julliard's avatar
Alexandre Julliard committed
1457
    case HTCLOSE:
1458 1459
        NC_TrackCloseButton (hwnd, wParam);
        break;
1460

Alexandre Julliard's avatar
Alexandre Julliard committed
1461 1462 1463 1464 1465 1466 1467
    case HTLEFT:
    case HTRIGHT:
    case HTTOP:
    case HTTOPLEFT:
    case HTTOPRIGHT:
    case HTBOTTOM:
    case HTBOTTOMLEFT:
Alexandre Julliard's avatar
Alexandre Julliard committed
1468
    case HTBOTTOMRIGHT:
1469 1470 1471 1472 1473 1474 1475 1476 1477
        /* Old comment:
         * "make sure hittest fits into 0xf and doesn't overlap with HTSYSMENU"
         * This was previously done by setting wParam=SC_SIZE + wParam - 2
         */
        /* But that is not what WinNT does. Instead it sends this. This
         * is easy to differentiate from HTSYSMENU, because HTSYSMENU adds
         * SC_MOUSEMENU into wParam.
         */
        SendMessageW( hwnd, WM_SYSCOMMAND, SC_SIZE + wParam - (HTLEFT-WMSZ_LEFT), lParam);
1478
        break;
Alexandre Julliard's avatar
Alexandre Julliard committed
1479 1480

    case HTBORDER:
1481
        break;
Alexandre Julliard's avatar
Alexandre Julliard committed
1482 1483 1484 1485 1486 1487 1488 1489 1490 1491
    }
    return 0;
}


/***********************************************************************
 *           NC_HandleNCLButtonDblClk
 *
 * Handle a WM_NCLBUTTONDBLCLK message. Called from DefWindowProc().
 */
1492
LRESULT NC_HandleNCLButtonDblClk( HWND hwnd, WPARAM wParam, LPARAM lParam )
Alexandre Julliard's avatar
Alexandre Julliard committed
1493
{
Alexandre Julliard's avatar
Alexandre Julliard committed
1494 1495 1496 1497
    /*
     * if this is an icon, send a restore since we are handling
     * a double click
     */
1498
    if (IsIconic(hwnd))
Alexandre Julliard's avatar
Alexandre Julliard committed
1499
    {
1500
        SendMessageW( hwnd, WM_SYSCOMMAND, SC_RESTORE, lParam );
Alexandre Julliard's avatar
Alexandre Julliard committed
1501
        return 0;
1502
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
1503

Alexandre Julliard's avatar
Alexandre Julliard committed
1504 1505 1506
    switch(wParam)  /* Hit test */
    {
    case HTCAPTION:
Alexandre Julliard's avatar
Alexandre Julliard committed
1507
        /* stop processing if WS_MAXIMIZEBOX is missing */
1508
        if (GetWindowLongW( hwnd, GWL_STYLE ) & WS_MAXIMIZEBOX)
1509 1510
            SendMessageW( hwnd, WM_SYSCOMMAND,
                          IsZoomed(hwnd) ? SC_RESTORE : SC_MAXIMIZE, lParam );
1511
        break;
Alexandre Julliard's avatar
Alexandre Julliard committed
1512 1513

    case HTSYSMENU:
1514 1515 1516
        {
            HMENU hSysMenu = GetSystemMenu(hwnd, FALSE);
            UINT state = GetMenuState(hSysMenu, SC_CLOSE, MF_BYCOMMAND);
1517

1518 1519 1520 1521
            /* If the item close of the sysmenu is disabled or not there do nothing */
            if ((state & (MF_DISABLED | MF_GRAYED)) || (state == 0xFFFFFFFF))
                break;

1522
            SendMessageW( hwnd, WM_SYSCOMMAND, SC_CLOSE, lParam );
1523 1524
            break;
        }
Alexandre Julliard's avatar
Alexandre Julliard committed
1525 1526

    case HTHSCROLL:
1527
        SendMessageW( hwnd, WM_SYSCOMMAND, SC_HSCROLL + HTHSCROLL, lParam );
1528
        break;
Alexandre Julliard's avatar
Alexandre Julliard committed
1529 1530

    case HTVSCROLL:
1531
        SendMessageW( hwnd, WM_SYSCOMMAND, SC_VSCROLL + HTVSCROLL, lParam );
1532
        break;
Alexandre Julliard's avatar
Alexandre Julliard committed
1533 1534 1535 1536 1537 1538 1539 1540 1541 1542
    }
    return 0;
}


/***********************************************************************
 *           NC_HandleSysCommand
 *
 * Handle a WM_SYSCOMMAND message. Called from DefWindowProc().
 */
1543
LRESULT NC_HandleSysCommand( HWND hwnd, WPARAM wParam, LPARAM lParam )
Alexandre Julliard's avatar
Alexandre Julliard committed
1544
{
1545
    TRACE("Handling WM_SYSCOMMAND %x %lx\n", wParam, lParam );
Alexandre Julliard's avatar
Alexandre Julliard committed
1546

1547 1548 1549
    if (HOOK_CallHooks( WH_CBT, HCBT_SYSCOMMAND, wParam, lParam, TRUE ))
        return 0;

1550
    switch (wParam & 0xfff0)
Alexandre Julliard's avatar
Alexandre Julliard committed
1551 1552 1553
    {
    case SC_SIZE:
    case SC_MOVE:
1554
        USER_Driver->pSysCommandSizeMove( hwnd, wParam );
1555
        break;
Alexandre Julliard's avatar
Alexandre Julliard committed
1556 1557

    case SC_MINIMIZE:
1558 1559
        if (hwnd == GetForegroundWindow())
            ShowOwnedPopups(hwnd,FALSE);
1560 1561
        ShowWindow( hwnd, SW_MINIMIZE );
        break;
Alexandre Julliard's avatar
Alexandre Julliard committed
1562 1563

    case SC_MAXIMIZE:
1564 1565
        if (IsIconic(hwnd) && hwnd == GetForegroundWindow())
            ShowOwnedPopups(hwnd,TRUE);
1566 1567
        ShowWindow( hwnd, SW_MAXIMIZE );
        break;
Alexandre Julliard's avatar
Alexandre Julliard committed
1568 1569

    case SC_RESTORE:
1570 1571
        if (IsIconic(hwnd) && hwnd == GetForegroundWindow())
            ShowOwnedPopups(hwnd,TRUE);
1572 1573
        ShowWindow( hwnd, SW_RESTORE );
        break;
Alexandre Julliard's avatar
Alexandre Julliard committed
1574 1575

    case SC_CLOSE:
1576
        return SendMessageA( hwnd, WM_CLOSE, 0, 0 );
Alexandre Julliard's avatar
Alexandre Julliard committed
1577 1578 1579

    case SC_VSCROLL:
    case SC_HSCROLL:
1580 1581
        {
            POINT pt;
1582 1583
            pt.x = (short)LOWORD(lParam);
            pt.y = (short)HIWORD(lParam);
1584 1585
            NC_TrackScrollBar( hwnd, wParam, pt );
        }
1586
        break;
Alexandre Julliard's avatar
Alexandre Julliard committed
1587

Alexandre Julliard's avatar
Alexandre Julliard committed
1588
    case SC_MOUSEMENU:
1589 1590
        {
            POINT pt;
1591 1592
            pt.x = (short)LOWORD(lParam);
            pt.y = (short)HIWORD(lParam);
1593 1594
            MENU_TrackMouseMenuBar( hwnd, wParam & 0x000F, pt );
        }
1595
        break;
Alexandre Julliard's avatar
Alexandre Julliard committed
1596

Alexandre Julliard's avatar
Alexandre Julliard committed
1597
    case SC_KEYMENU:
1598
        MENU_TrackKbdMenuBar( hwnd, wParam, (WCHAR)lParam );
1599
        break;
1600

Alexandre Julliard's avatar
Alexandre Julliard committed
1601
    case SC_TASKLIST:
1602 1603
        WinExec( "taskman.exe", SW_SHOWNORMAL );
        break;
Alexandre Julliard's avatar
Alexandre Julliard committed
1604 1605

    case SC_SCREENSAVE:
1606
        if (wParam == SC_ABOUTWINE)
1607 1608 1609 1610 1611
        {
            HMODULE hmodule = LoadLibraryA( "shell32.dll" );
            if (hmodule)
            {
                FARPROC aboutproc = GetProcAddress( hmodule, "ShellAboutA" );
1612
                if (aboutproc) aboutproc( hwnd, PACKAGE_NAME, PACKAGE_STRING, 0 );
1613 1614 1615
                FreeLibrary( hmodule );
            }
        }
1616 1617
        else
          if (wParam == SC_PUTMARK)
1618
            DPRINTF("Debug mark requested by user\n");
1619
        break;
1620

Alexandre Julliard's avatar
Alexandre Julliard committed
1621 1622 1623 1624
    case SC_HOTKEY:
    case SC_ARRANGE:
    case SC_NEXTWINDOW:
    case SC_PREVWINDOW:
1625
        FIXME("unimplemented WM_SYSCOMMAND %04x!\n", wParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
1626
        break;
Alexandre Julliard's avatar
Alexandre Julliard committed
1627 1628 1629
    }
    return 0;
}
1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640

/*************************************************************
*  NC_DrawGrayButton
*
* Stub for the grayed button of the caption
*
*************************************************************/

BOOL NC_DrawGrayButton(HDC hdc, int x, int y)
{
    HBITMAP hMaskBmp;
1641
    HDC hdcMask;
1642 1643 1644
    HBRUSH hOldBrush;

    hMaskBmp = CreateBitmap (12, 10, 1, 1, lpGrayMask);
1645

1646
    if(hMaskBmp == 0)
1647
        return FALSE;
1648

1649
    hdcMask = CreateCompatibleDC (0);
1650
    SelectObject (hdcMask, hMaskBmp);
1651

1652
    /* Draw the grayed bitmap using the mask */
1653
    hOldBrush = SelectObject (hdc, (HGDIOBJ)RGB(128, 128, 128));
1654
    BitBlt (hdc, x, y, 12, 10,
1655
            hdcMask, 0, 0, 0xB8074A);
1656

1657 1658 1659 1660
    /* Clean up */
    SelectObject (hdc, hOldBrush);
    DeleteObject(hMaskBmp);
    DeleteDC (hdcMask);
1661

1662 1663
    return TRUE;
}
1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 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 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732

/***********************************************************************
 *		GetTitleBarInfo (USER32.@)
 * TODO: Handle STATE_SYSTEM_PRESSED
 */
BOOL WINAPI GetTitleBarInfo(HWND hwnd, PTITLEBARINFO tbi) {
    DWORD dwStyle;
    DWORD dwExStyle;
    RECT wndRect;

    TRACE("(%p %p)\n", hwnd, tbi);

    if(tbi->cbSize != sizeof(TITLEBARINFO)) {
        TRACE("Invalid TITLEBARINFO size: %ld\n", tbi->cbSize);
        SetLastError(ERROR_INVALID_PARAMETER);
        return FALSE;
    }
    dwStyle = GetWindowLongW(hwnd, GWL_STYLE);
    dwExStyle = GetWindowLongW(hwnd, GWL_EXSTYLE);
    NC_GetInsideRect(hwnd, &tbi->rcTitleBar);

    GetWindowRect(hwnd, &wndRect);

    tbi->rcTitleBar.top += wndRect.top;
    tbi->rcTitleBar.left += wndRect.left;
    tbi->rcTitleBar.right += wndRect.left;

    tbi->rcTitleBar.bottom = tbi->rcTitleBar.top;
    if(dwExStyle & WS_EX_TOOLWINDOW)
        tbi->rcTitleBar.bottom += GetSystemMetrics(SM_CYSMCAPTION);
    else {
        tbi->rcTitleBar.bottom += GetSystemMetrics(SM_CYCAPTION);
        tbi->rcTitleBar.left += GetSystemMetrics(SM_CXSIZE);
    }

    ZeroMemory(&tbi->rgstate, sizeof(tbi->rgstate));
    /* Does the title bar always have STATE_SYSTEM_FOCUSABLE?
     * Under XP it seems to
     */
    tbi->rgstate[0] = STATE_SYSTEM_FOCUSABLE;
    if(dwStyle & WS_CAPTION) {
        tbi->rgstate[1] = STATE_SYSTEM_INVISIBLE;
        if(dwStyle & WS_SYSMENU) {
            if(!(dwStyle & (WS_MINIMIZEBOX|WS_MAXIMIZEBOX))) {
                tbi->rgstate[2] = STATE_SYSTEM_INVISIBLE;
                tbi->rgstate[3] = STATE_SYSTEM_INVISIBLE;
            }
            else {
                if(!(dwStyle & WS_MINIMIZEBOX))
                    tbi->rgstate[2] = STATE_SYSTEM_UNAVAILABLE;
                if(!(dwStyle & WS_MAXIMIZEBOX))
                    tbi->rgstate[3] = STATE_SYSTEM_UNAVAILABLE;
            }
            if(!(dwExStyle & WS_EX_CONTEXTHELP))
                tbi->rgstate[4] = STATE_SYSTEM_INVISIBLE;
            if(GetClassLongW(hwnd, GCL_STYLE) & CS_NOCLOSE)
                tbi->rgstate[5] = STATE_SYSTEM_UNAVAILABLE;
        }
        else {
            tbi->rgstate[2] = STATE_SYSTEM_INVISIBLE;
            tbi->rgstate[3] = STATE_SYSTEM_INVISIBLE;
            tbi->rgstate[4] = STATE_SYSTEM_INVISIBLE;
            tbi->rgstate[5] = STATE_SYSTEM_INVISIBLE;
        }
    }
    else
        tbi->rgstate[0] |= STATE_SYSTEM_INVISIBLE;
    return TRUE;
}