nonclient.c 53.6 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
 * 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
18
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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 "winnls.h"
Alexandre Julliard's avatar
Alexandre Julliard committed
29
#include "win.h"
30
#include "user_private.h"
31
#include "controls.h"
32
#include "wine/debug.h"
Alexandre Julliard's avatar
Alexandre Julliard committed
33

34
WINE_DEFAULT_DEBUG_CHANNEL(nonclient);
35

36
#define SC_ABOUTWINE            (SC_SCREENSAVE+1)
Alexandre Julliard's avatar
Alexandre Julliard committed
37

Alexandre Julliard's avatar
Alexandre Julliard committed
38 39
  /* Some useful macros */
#define HAS_DLGFRAME(style,exStyle) \
Alexandre Julliard's avatar
Alexandre Julliard committed
40
    (((exStyle) & WS_EX_DLGMODALFRAME) || \
41
     (((style) & WS_DLGFRAME) && !((style) & WS_THICKFRAME)))
Alexandre Julliard's avatar
Alexandre Julliard committed
42

43
#define HAS_THICKFRAME(style,exStyle) \
Alexandre Julliard's avatar
Alexandre Julliard committed
44
    (((style) & WS_THICKFRAME) && \
45
     !(((style) & (WS_DLGFRAME|WS_BORDER)) == WS_DLGFRAME))
Alexandre Julliard's avatar
Alexandre Julliard committed
46

47 48
#define HAS_THINFRAME(style) \
    (((style) & WS_BORDER) || !((style) & (WS_CHILD | WS_POPUP)))
Alexandre Julliard's avatar
Alexandre Julliard committed
49

50 51 52
#define HAS_BIGFRAME(style,exStyle) \
    (((style) & (WS_THICKFRAME | WS_DLGFRAME)) || \
     ((exStyle) & WS_EX_DLGMODALFRAME))
53

54 55 56 57
#define HAS_STATICOUTERFRAME(style,exStyle) \
    (((exStyle) & (WS_EX_STATICEDGE|WS_EX_DLGMODALFRAME)) == \
     WS_EX_STATICEDGE)

58
#define HAS_MENU(hwnd,style)  ((((style) & (WS_CHILD | WS_POPUP)) != WS_CHILD) && GetMenu(hwnd))
Alexandre Julliard's avatar
Alexandre Julliard committed
59

60

61
static void adjust_window_rect( RECT *rect, DWORD style, BOOL menu, DWORD exStyle, NONCLIENTMETRICSW *ncm )
Alexandre Julliard's avatar
Alexandre Julliard committed
62
{
63
    int adjust = 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
64

65
    if ((exStyle & (WS_EX_STATICEDGE|WS_EX_DLGMODALFRAME)) == WS_EX_STATICEDGE)
66
        adjust = 1; /* for the outer frame always present */
67 68 69
    else if ((exStyle & WS_EX_DLGMODALFRAME) || (style & (WS_THICKFRAME|WS_DLGFRAME)))
        adjust = 2; /* outer */

70
    if (style & WS_THICKFRAME)
71 72 73
        adjust += ncm->iBorderWidth + ncm->iPaddedBorderWidth; /* The resize border */

    if ((style & (WS_BORDER|WS_DLGFRAME)) || (exStyle & WS_EX_DLGMODALFRAME))
74 75 76
        adjust++; /* The other border */

    InflateRect (rect, adjust, adjust);
77 78

    if ((style & WS_CAPTION) == WS_CAPTION)
Alexandre Julliard's avatar
Alexandre Julliard committed
79
    {
80
        if (exStyle & WS_EX_TOOLWINDOW)
81
            rect->top -= ncm->iSmCaptionHeight + 1;
Alexandre Julliard's avatar
Alexandre Julliard committed
82
        else
83
            rect->top -= ncm->iCaptionHeight + 1;
Alexandre Julliard's avatar
Alexandre Julliard committed
84
    }
85
    if (menu) rect->top -= ncm->iMenuHeight + 1;
Alexandre Julliard's avatar
Alexandre Julliard committed
86 87

    if (exStyle & WS_EX_CLIENTEDGE)
88
        InflateRect(rect, GetSystemMetrics(SM_CXEDGE), GetSystemMetrics(SM_CYEDGE));
Alexandre Julliard's avatar
Alexandre Julliard committed
89 90 91
}


92 93
static HICON NC_IconForWindow( HWND hwnd )
{
94 95 96
    HICON hIcon = 0;
    WND *wndPtr = WIN_GetPtr( hwnd );

97
    if (wndPtr && wndPtr != WND_OTHER_PROCESS && wndPtr != WND_DESKTOP)
98 99 100 101 102
    {
        hIcon = wndPtr->hIconSmall;
        if (!hIcon) hIcon = wndPtr->hIcon;
        WIN_ReleasePtr( wndPtr );
    }
103 104
    if (!hIcon) hIcon = (HICON) GetClassLongPtrW( hwnd, GCLP_HICONSM );
    if (!hIcon) hIcon = (HICON) GetClassLongPtrW( hwnd, GCLP_HICON );
105

106
    /* If there is no icon specified and this is not a modal dialog,
107 108
     * get the default one.
     */
109
    if (!hIcon && !(GetWindowLongW( hwnd, GWL_EXSTYLE ) & WS_EX_DLGMODALFRAME))
110
        hIcon = LoadImageW(0, (LPCWSTR)IDI_WINLOGO, IMAGE_ICON, GetSystemMetrics(SM_CXSMICON),
111
                           GetSystemMetrics(SM_CYSMICON), LR_DEFAULTCOLOR | LR_SHARED);
112 113 114
    return hIcon;
}

115 116 117 118 119 120
/* 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)
    {
121
        TRIVERTEX vertices[4];
122 123 124 125 126 127
        DWORD colLeft = 
            GetSysColor (active ? COLOR_ACTIVECAPTION : COLOR_INACTIVECAPTION);
        DWORD colRight = 
            GetSysColor (active ? COLOR_GRADIENTACTIVECAPTION 
                                : COLOR_GRADIENTINACTIVECAPTION);
        int buttonsAreaSize = GetSystemMetrics(SM_CYCAPTION) - 1;
128 129 130 131 132 133 134 135 136 137 138 139
        static GRADIENT_RECT mesh[] = {{0, 1}, {1, 2}, {2, 3}};

        vertices[0].Red   = vertices[1].Red   = GetRValue (colLeft) << 8;
        vertices[0].Green = vertices[1].Green = GetGValue (colLeft) << 8;
        vertices[0].Blue  = vertices[1].Blue  = GetBValue (colLeft) << 8;
        vertices[0].Alpha = vertices[1].Alpha = 0xff00;
        vertices[2].Red   = vertices[3].Red   = GetRValue (colRight) << 8;
        vertices[2].Green = vertices[3].Green = GetGValue (colRight) << 8;
        vertices[2].Blue  = vertices[3].Blue  = GetBValue (colRight) << 8;
        vertices[2].Alpha = vertices[3].Alpha = 0xff00;

        if ((dwStyle & WS_SYSMENU)
140 141
            && ((dwStyle & WS_MAXIMIZEBOX) || (dwStyle & WS_MINIMIZEBOX)))
            buttonsAreaSize += 2 * (GetSystemMetrics(SM_CXSIZE) + 1);
142

143 144 145
        /* area behind icon; solid filled with left color */
        vertices[0].x = rect->left;
        vertices[0].y = rect->top;
146 147
        if (dwStyle & WS_SYSMENU)
            vertices[1].x = min (rect->left + GetSystemMetrics(SM_CXSMICON), rect->right);
148 149 150
        else
            vertices[1].x = vertices[0].x;
        vertices[1].y = rect->bottom;
151

152
        /* area behind text; gradient */
153
        vertices[2].x = max (vertices[1].x, rect->right - buttonsAreaSize);
154
        vertices[2].y = rect->top;
155

156
        /* area behind buttons; solid filled with right color */
157 158 159 160
        vertices[3].x = rect->right;
        vertices[3].y = rect->bottom;

        GdiGradientFill (hdc, vertices, 4, mesh, 3, GRADIENT_FILL_RECT_H);
161 162 163 164 165 166
    }
    else
        FillRect (hdc, rect, GetSysColorBrush (active ?
                  COLOR_ACTIVECAPTION : COLOR_INACTIVECAPTION));
}

167
/***********************************************************************
168
 *		DrawCaption (USER32.@) Draws a caption bar
169 170 171 172 173 174 175
 *
 * PARAMS
 *     hwnd   [I]
 *     hdc    [I]
 *     lpRect [I]
 *     uFlags [I]
 *
176 177 178
 * RETURNS
 *     Success:
 *     Failure:
179 180
 */

181 182
BOOL WINAPI
DrawCaption (HWND hwnd, HDC hdc, const RECT *lpRect, UINT uFlags)
183
{
184
    return DrawCaptionTempW (hwnd, hdc, lpRect, 0, 0, NULL, uFlags & 0x103F);
185 186 187 188
}


/***********************************************************************
189
 *		DrawCaptionTempA (USER32.@)
190
 */
191 192 193 194 195 196
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;
197

198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
    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)
217
{
218
    RECT   rc = *rect;
219

220
    TRACE("(%p,%p,%p,%p,%p,%s,%08x)\n",
221
          hwnd, hdc, rect, hFont, hIcon, debugstr_w(str), uFlags);
222 223 224

    /* drawing background */
    if (uFlags & DC_INBUTTON) {
225 226 227
        FillRect (hdc, &rc, GetSysColorBrush (COLOR_3DFACE));

        if (uFlags & DC_ACTIVE) {
228
            HBRUSH hbr = SelectObject (hdc, SYSCOLOR_Get55AABrush());
229 230 231 232
            PatBlt (hdc, rc.left, rc.top,
                      rc.right-rc.left, rc.bottom-rc.top, 0xFA0089);
            SelectObject (hdc, hbr);
        }
233 234
    }
    else {
235
        DWORD style = GetWindowLongW (hwnd, GWL_STYLE);
236
        NC_DrawCaptionBar (hdc, &rc, style, uFlags & DC_ACTIVE, uFlags & DC_GRADIENT);
237 238 239 240 241
    }


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

244 245
        pt.x = rc.left + 2;
        pt.y = (rc.bottom + rc.top - GetSystemMetrics(SM_CYSMICON)) / 2;
246

247 248 249
        if (!hIcon) hIcon = NC_IconForWindow(hwnd);
        DrawIconEx (hdc, pt.x, pt.y, hIcon, GetSystemMetrics(SM_CXSMICON),
                    GetSystemMetrics(SM_CYSMICON), 0, 0, DI_NORMAL);
250
        rc.left = pt.x + GetSystemMetrics( SM_CXSMICON );
251 252 253 254
    }

    /* drawing text */
    if (uFlags & DC_TEXT) {
255
        HFONT hOldFont;
256
        WCHAR text[128];
257

258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
        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);
        }

279 280
        if (!str)
        {
281
            if (!GetWindowTextW( hwnd, text, ARRAY_SIZE( text ))) text[0] = 0;
282
            str = text;
283
        }
284 285 286
        rc.left += 2;
        DrawTextW( hdc, str, -1, &rc, ((uFlags & 0x4000) ? DT_CENTER : DT_LEFT) |
                   DT_SINGLELINE | DT_VCENTER | DT_NOPREFIX | DT_END_ELLIPSIS );
287 288 289 290 291

        if (hFont)
            SelectObject (hdc, hOldFont);
        else
            DeleteObject (SelectObject (hdc, hOldFont));
292 293 294 295
    }

    /* drawing focus ??? */
    if (uFlags & 0x2000)
296
        FIXME("undocumented flag (0x2000)!\n");
297

298
    return FALSE;
299 300 301
}


Alexandre Julliard's avatar
Alexandre Julliard committed
302
/***********************************************************************
303
 *		AdjustWindowRect (USER32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
304
 */
305
BOOL WINAPI DECLSPEC_HOTPATCH AdjustWindowRect( LPRECT rect, DWORD style, BOOL menu )
Alexandre Julliard's avatar
Alexandre Julliard committed
306
{
307
    return AdjustWindowRectEx( rect, style, menu, 0 );
Alexandre Julliard's avatar
Alexandre Julliard committed
308 309 310
}


311 312 313
/***********************************************************************
 *		AdjustWindowRectEx (USER32.@)
 */
314
BOOL WINAPI DECLSPEC_HOTPATCH AdjustWindowRectEx( LPRECT rect, DWORD style, BOOL menu, DWORD exStyle )
315
{
316 317
    NONCLIENTMETRICSW ncm;

318
    if (style & WS_MINIMIZE) return TRUE;
Alexandre Julliard's avatar
Alexandre Julliard committed
319

320
    TRACE("(%s) %08x %d %08x\n", wine_dbgstr_rect(rect), style, menu, exStyle );
Alexandre Julliard's avatar
Alexandre Julliard committed
321

322 323 324 325
    ncm.cbSize = sizeof(ncm);
    SystemParametersInfoW( SPI_GETNONCLIENTMETRICS, 0, &ncm, 0 );

    adjust_window_rect( rect, style, menu, exStyle, &ncm );
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
    return TRUE;
}


/***********************************************************************
 *		AdjustWindowRectExForDpi (USER32.@)
 */
BOOL WINAPI DECLSPEC_HOTPATCH AdjustWindowRectExForDpi( LPRECT rect, DWORD style, BOOL menu,
                                                        DWORD exStyle, UINT dpi )
{
    NONCLIENTMETRICSW ncm;

    if (style & WS_MINIMIZE) return TRUE;

    TRACE("(%s) %08x %d %08x %u\n", wine_dbgstr_rect(rect), style, menu, exStyle, dpi );
341

342 343 344 345
    ncm.cbSize = sizeof(ncm);
    SystemParametersInfoForDpi( SPI_GETNONCLIENTMETRICS, 0, &ncm, 0, dpi );

    adjust_window_rect( rect, style, menu, exStyle, &ncm );
Alexandre Julliard's avatar
Alexandre Julliard committed
346
    return TRUE;
Alexandre Julliard's avatar
Alexandre Julliard committed
347 348 349 350 351 352 353 354
}


/***********************************************************************
 *           NC_HandleNCCalcSize
 *
 * Handle a WM_NCCALCSIZE message. Called from DefWindowProc().
 */
355
LRESULT NC_HandleNCCalcSize( HWND hwnd, WPARAM wparam, RECT *winRect )
Alexandre Julliard's avatar
Alexandre Julliard committed
356
{
357
    RECT tmpRect = { 0, 0, 0, 0 };
358
    LRESULT result = 0;
359 360 361
    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
362

363 364 365
    if (winRect == NULL)
        return 0;

366 367
    if (cls_style & CS_VREDRAW) result |= WVR_VREDRAW;
    if (cls_style & CS_HREDRAW) result |= WVR_HREDRAW;
Alexandre Julliard's avatar
Alexandre Julliard committed
368

369
    if (!(style & WS_MINIMIZE))
370
    {
371
        AdjustWindowRectEx( &tmpRect, style, FALSE, exStyle & ~WS_EX_CLIENTEDGE);
Alexandre Julliard's avatar
Alexandre Julliard committed
372

373 374 375 376
        winRect->left   -= tmpRect.left;
        winRect->top    -= tmpRect.top;
        winRect->right  -= tmpRect.right;
        winRect->bottom -= tmpRect.bottom;
Alexandre Julliard's avatar
Alexandre Julliard committed
377

378
        if (((style & (WS_CHILD | WS_POPUP)) != WS_CHILD) && GetMenu(hwnd))
379
        {
380
            TRACE("Calling GetMenuBarHeight with hwnd %p, width %d, at (%d, %d).\n",
381
                  hwnd, winRect->right - winRect->left, -tmpRect.left, -tmpRect.top );
Alexandre Julliard's avatar
Alexandre Julliard committed
382

383 384 385
            winRect->top +=
                MENU_GetMenuBarHeight( hwnd,
                                       winRect->right - winRect->left,
386
                                       -tmpRect.left, -tmpRect.top );
387
        }
Alexandre Julliard's avatar
Alexandre Julliard committed
388

389 390 391 392 393 394 395
        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)
396 397 398 399 400
            if (winRect->right - winRect->left >= GetSystemMetrics(SM_CXVSCROLL))
            {
                /* rectangle is in screen coords when wparam is false */
                if (!wparam && (exStyle & WS_EX_LAYOUTRTL)) exStyle ^= WS_EX_LEFTSCROLLBAR;

401 402 403 404 405 406 407 408 409
                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);
410 411 412 413 414 415

        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
416
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
417
    return result;
Alexandre Julliard's avatar
Alexandre Julliard committed
418 419 420 421
}


/***********************************************************************
Alexandre Julliard's avatar
Alexandre Julliard committed
422
 *           NC_GetInsideRect
Alexandre Julliard's avatar
Alexandre Julliard committed
423
 *
Alexandre Julliard's avatar
Alexandre Julliard committed
424 425
 * Get the 'inside' rectangle of a window, i.e. the whole window rectangle
 * but without the borders (if any).
Alexandre Julliard's avatar
Alexandre Julliard committed
426
 */
427 428
static void NC_GetInsideRect( HWND hwnd, enum coords_relative relative, RECT *rect,
                              DWORD style, DWORD ex_style )
Alexandre Julliard's avatar
Alexandre Julliard committed
429
{
430
    WIN_GetRectangles( hwnd, relative, rect, NULL );
Alexandre Julliard's avatar
Alexandre Julliard committed
431

432
    if (style & WS_MINIMIZE) return;
Alexandre Julliard's avatar
Alexandre Julliard committed
433

Alexandre Julliard's avatar
Alexandre Julliard committed
434
    /* Remove frame from rectangle */
435
    if (HAS_THICKFRAME( style, ex_style ))
Alexandre Julliard's avatar
Alexandre Julliard committed
436
    {
437
        InflateRect( rect, -GetSystemMetrics(SM_CXFRAME), -GetSystemMetrics(SM_CYFRAME) );
Alexandre Julliard's avatar
Alexandre Julliard committed
438
    }
439
    else if (HAS_DLGFRAME( style, ex_style ))
Alexandre Julliard's avatar
Alexandre Julliard committed
440
    {
441
        InflateRect( rect, -GetSystemMetrics(SM_CXDLGFRAME), -GetSystemMetrics(SM_CYDLGFRAME));
Alexandre Julliard's avatar
Alexandre Julliard committed
442
    }
443
    else if (HAS_THINFRAME( style ))
Alexandre Julliard's avatar
Alexandre Julliard committed
444
    {
445
        InflateRect( rect, -GetSystemMetrics(SM_CXBORDER), -GetSystemMetrics(SM_CYBORDER) );
Alexandre Julliard's avatar
Alexandre Julliard committed
446 447
    }

448 449
    /* We have additional border information if the window
     * is a child (but not an MDI child) */
450
    if ((style & WS_CHILD) && !(ex_style & WS_EX_MDICHILD))
451
    {
452
        if (ex_style & WS_EX_CLIENTEDGE)
453
            InflateRect (rect, -GetSystemMetrics(SM_CXEDGE), -GetSystemMetrics(SM_CYEDGE));
454
        if (ex_style & WS_EX_STATICEDGE)
455
            InflateRect (rect, -GetSystemMetrics(SM_CXBORDER), -GetSystemMetrics(SM_CYBORDER));
Alexandre Julliard's avatar
Alexandre Julliard committed
456 457 458 459
    }
}


Alexandre Julliard's avatar
Alexandre Julliard committed
460
/***********************************************************************
461
 * NC_HandleNCHitTest
Alexandre Julliard's avatar
Alexandre Julliard committed
462
 *
463
 * Handle a WM_NCHITTEST message. Called from DefWindowProc().
Alexandre Julliard's avatar
Alexandre Julliard committed
464
 */
465
LRESULT NC_HandleNCHitTest( HWND hwnd, POINT pt )
Alexandre Julliard's avatar
Alexandre Julliard committed
466
{
467
    RECT rect, rcClient;
468
    DWORD style, ex_style;
Alexandre Julliard's avatar
Alexandre Julliard committed
469

470
    TRACE("hwnd=%p pt=%d,%d\n", hwnd, pt.x, pt.y );
Alexandre Julliard's avatar
Alexandre Julliard committed
471

472
    WIN_GetRectangles( hwnd, COORDS_SCREEN, &rect, &rcClient );
473
    if (!PtInRect( &rect, pt )) return HTNOWHERE;
Alexandre Julliard's avatar
Alexandre Julliard committed
474

475 476 477
    style = GetWindowLongW( hwnd, GWL_STYLE );
    ex_style = GetWindowLongW( hwnd, GWL_EXSTYLE );
    if (style & WS_MINIMIZE) return HTCAPTION;
Alexandre Julliard's avatar
Alexandre Julliard committed
478

479
    if (PtInRect( &rcClient, pt )) return HTCLIENT;
480

481
    /* Check borders */
482
    if (HAS_THICKFRAME( style, ex_style ))
Alexandre Julliard's avatar
Alexandre Julliard committed
483
    {
484 485
        InflateRect( &rect, -GetSystemMetrics(SM_CXFRAME), -GetSystemMetrics(SM_CYFRAME) );
        if (!PtInRect( &rect, pt ))
Alexandre Julliard's avatar
Alexandre Julliard committed
486
        {
487 488
            /* Check top sizing border */
            if (pt.y < rect.top)
Alexandre Julliard's avatar
Alexandre Julliard committed
489
            {
490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513
                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
514 515
            }
        }
516 517 518
    }
    else  /* No thick frame */
    {
519
        if (HAS_DLGFRAME( style, ex_style ))
520
            InflateRect(&rect, -GetSystemMetrics(SM_CXDLGFRAME), -GetSystemMetrics(SM_CYDLGFRAME));
521
        else if (HAS_THINFRAME( style ))
522 523 524
            InflateRect(&rect, -GetSystemMetrics(SM_CXBORDER), -GetSystemMetrics(SM_CYBORDER));
        if (!PtInRect( &rect, pt )) return HTBORDER;
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
525

526
    /* Check caption */
Alexandre Julliard's avatar
Alexandre Julliard committed
527

528
    if ((style & WS_CAPTION) == WS_CAPTION)
529
    {
530
        if (ex_style & WS_EX_TOOLWINDOW)
531 532 533 534
            rect.top += GetSystemMetrics(SM_CYSMCAPTION) - 1;
        else
            rect.top += GetSystemMetrics(SM_CYCAPTION) - 1;
        if (!PtInRect( &rect, pt ))
Alexandre Julliard's avatar
Alexandre Julliard committed
535
        {
536
            BOOL min_or_max_box = (style & WS_SYSMENU) && (style & (WS_MINIMIZEBOX|WS_MAXIMIZEBOX));
537
            if (ex_style & WS_EX_LAYOUTRTL)
Alexandre Julliard's avatar
Alexandre Julliard committed
538
            {
539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597
                /* Check system menu */
                if ((style & WS_SYSMENU) && !(ex_style & WS_EX_TOOLWINDOW) && NC_IconForWindow(hwnd))
                {
                    rect.right -= GetSystemMetrics(SM_CYCAPTION) - 1;
                    if (pt.x > rect.right) return HTSYSMENU;
                }

                /* Check close button */
                if (style & WS_SYSMENU)
                {
                    rect.left += GetSystemMetrics(SM_CYCAPTION);
                    if (pt.x < rect.left) return HTCLOSE;
                }

                /* Check maximize box */
                /* In win95 there is automatically a Maximize button when there is a minimize one*/
                if (min_or_max_box && !(ex_style & WS_EX_TOOLWINDOW))
                {
                    rect.left += GetSystemMetrics(SM_CXSIZE);
                    if (pt.x < rect.left) return HTMAXBUTTON;
                }

                /* Check minimize box */
                if (min_or_max_box && !(ex_style & WS_EX_TOOLWINDOW))
                {
                    rect.left += GetSystemMetrics(SM_CXSIZE);
                    if (pt.x < rect.left) return HTMINBUTTON;
                }
            }
            else
            {
                /* Check system menu */
                if ((style & WS_SYSMENU) && !(ex_style & WS_EX_TOOLWINDOW) && NC_IconForWindow(hwnd))
                {
                    rect.left += GetSystemMetrics(SM_CYCAPTION) - 1;
                    if (pt.x < rect.left) return HTSYSMENU;
                }

                /* Check close button */
                if (style & WS_SYSMENU)
                {
                    rect.right -= GetSystemMetrics(SM_CYCAPTION);
                    if (pt.x > rect.right) return HTCLOSE;
                }

                /* Check maximize box */
                /* In win95 there is automatically a Maximize button when there is a minimize one*/
                if (min_or_max_box && !(ex_style & WS_EX_TOOLWINDOW))
                {
                    rect.right -= GetSystemMetrics(SM_CXSIZE);
                    if (pt.x > rect.right) return HTMAXBUTTON;
                }

                /* Check minimize box */
                if (min_or_max_box && !(ex_style & WS_EX_TOOLWINDOW))
                {
                    rect.right -= GetSystemMetrics(SM_CXSIZE);
                    if (pt.x > rect.right) return HTMINBUTTON;
                }
Alexandre Julliard's avatar
Alexandre Julliard committed
598
            }
599
            return HTCAPTION;
Alexandre Julliard's avatar
Alexandre Julliard committed
600 601 602
        }
    }

603 604 605 606 607 608
      /* Check menu bar */

    if (HAS_MENU( hwnd, style ) && (pt.y < rcClient.top) &&
        (pt.x >= rcClient.left) && (pt.x < rcClient.right))
        return HTMENU;

Alexandre Julliard's avatar
Alexandre Julliard committed
609 610
      /* Check vertical scroll bar */

611
    if (ex_style & WS_EX_LAYOUTRTL) ex_style ^= WS_EX_LEFTSCROLLBAR;
612
    if (style & WS_VSCROLL)
Alexandre Julliard's avatar
Alexandre Julliard committed
613
    {
614
        if((ex_style & WS_EX_LEFTSCROLLBAR) != 0)
615
            rcClient.left -= GetSystemMetrics(SM_CXVSCROLL);
616
        else
617
            rcClient.right += GetSystemMetrics(SM_CXVSCROLL);
618
        if (PtInRect( &rcClient, pt )) return HTVSCROLL;
Alexandre Julliard's avatar
Alexandre Julliard committed
619
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
620 621 622

      /* Check horizontal scroll bar */

623
    if (style & WS_HSCROLL)
Alexandre Julliard's avatar
Alexandre Julliard committed
624
    {
625
        rcClient.bottom += GetSystemMetrics(SM_CYHSCROLL);
626
        if (PtInRect( &rcClient, pt ))
627 628
        {
            /* Check size box */
629 630 631
            if ((style & WS_VSCROLL) &&
                ((((ex_style & WS_EX_LEFTSCROLLBAR) != 0) && (pt.x <= rcClient.left + GetSystemMetrics(SM_CXVSCROLL))) ||
                (((ex_style & WS_EX_LEFTSCROLLBAR) == 0) && (pt.x >= rcClient.right - GetSystemMetrics(SM_CXVSCROLL)))))
632 633 634
                return HTSIZE;
            return HTHSCROLL;
        }
Alexandre Julliard's avatar
Alexandre Julliard committed
635 636
    }

637
    /* Has to return HTNOWHERE if nothing was found
638 639
       Could happen when a window has a customized non client area */
    return HTNOWHERE;
Alexandre Julliard's avatar
Alexandre Julliard committed
640 641 642
}


Alexandre Julliard's avatar
Alexandre Julliard committed
643 644
/******************************************************************************
 *
645
 *   NC_DrawSysButton
Alexandre Julliard's avatar
Alexandre Julliard committed
646
 *
647
 *   Draws the system icon.
Alexandre Julliard's avatar
Alexandre Julliard committed
648 649
 *
 *****************************************************************************/
650
BOOL NC_DrawSysButton (HWND hwnd, HDC hdc, BOOL down)
Alexandre Julliard's avatar
Alexandre Julliard committed
651
{
652
    HICON hIcon = NC_IconForWindow( hwnd );
Alexandre Julliard's avatar
Alexandre Julliard committed
653

654
    if (hIcon)
Alexandre Julliard's avatar
Alexandre Julliard committed
655
    {
656
        RECT rect;
657
        POINT pt;
658 659 660
        DWORD style = GetWindowLongW( hwnd, GWL_STYLE );
        DWORD ex_style = GetWindowLongW( hwnd, GWL_EXSTYLE );

661
        NC_GetInsideRect( hwnd, COORDS_WINDOW, &rect, style, ex_style );
662
        pt.x = rect.left + 2;
663
        pt.y = rect.top + (GetSystemMetrics(SM_CYCAPTION) - GetSystemMetrics(SM_CYSMICON)) / 2;
664
        DrawIconEx (hdc, pt.x, pt.y, hIcon,
665 666
                    GetSystemMetrics(SM_CXSMICON),
                    GetSystemMetrics(SM_CYSMICON), 0, 0, DI_NORMAL);
Alexandre Julliard's avatar
Alexandre Julliard committed
667
    }
668
    return (hIcon != 0);
Alexandre Julliard's avatar
Alexandre Julliard committed
669 670 671 672 673
}


/******************************************************************************
 *
674
 *   NC_DrawCloseButton
Alexandre Julliard's avatar
Alexandre Julliard committed
675
 *
676
 *   Draws the close button.
Alexandre Julliard's avatar
Alexandre Julliard committed
677
 *
678 679
 *   If bGrayed is true, then draw a disabled Close button
 *
Alexandre Julliard's avatar
Alexandre Julliard committed
680 681
 *****************************************************************************/

682
static void NC_DrawCloseButton (HWND hwnd, HDC hdc, BOOL down, BOOL bGrayed)
Alexandre Julliard's avatar
Alexandre Julliard committed
683
{
684
    RECT rect;
685 686
    DWORD style = GetWindowLongW( hwnd, GWL_STYLE );
    DWORD ex_style = GetWindowLongW( hwnd, GWL_EXSTYLE );
Alexandre Julliard's avatar
Alexandre Julliard committed
687

688
    NC_GetInsideRect( hwnd, COORDS_WINDOW, &rect, style, ex_style );
689 690

    /* A tool window has a smaller Close button */
691
    if (ex_style & WS_EX_TOOLWINDOW)
Alexandre Julliard's avatar
Alexandre Julliard committed
692
    {
693 694 695
        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
696

697 698 699 700
        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
701
    }
702 703
    else
    {
704 705
        rect.left = rect.right - GetSystemMetrics(SM_CXSIZE);
        rect.bottom = rect.top + GetSystemMetrics(SM_CYSIZE) - 2;
706 707 708 709 710 711 712
        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
713 714 715
}

/******************************************************************************
716
 *   NC_DrawMaxButton
Alexandre Julliard's avatar
Alexandre Julliard committed
717
 *
718
 *   Draws the maximize button for windows.
719
 *   If bGrayed is true, then draw a disabled Maximize button
720
 */
721
static void NC_DrawMaxButton(HWND hwnd,HDC hdc,BOOL down, BOOL bGrayed)
Alexandre Julliard's avatar
Alexandre Julliard committed
722
{
723
    RECT rect;
724
    UINT flags;
725 726
    DWORD style = GetWindowLongW( hwnd, GWL_STYLE );
    DWORD ex_style = GetWindowLongW( hwnd, GWL_EXSTYLE );
727 728

    /* never draw maximize box when window has WS_EX_TOOLWINDOW style */
729
    if (ex_style & WS_EX_TOOLWINDOW) return;
730

731
    flags = (style & WS_MAXIMIZE) ? DFCS_CAPTIONRESTORE : DFCS_CAPTIONMAX;
732

733
    NC_GetInsideRect( hwnd, COORDS_WINDOW, &rect, style, ex_style );
734
    if (style & WS_SYSMENU)
735
        rect.right -= GetSystemMetrics(SM_CXSIZE);
736
    rect.left = rect.right - GetSystemMetrics(SM_CXSIZE);
737
    rect.bottom = rect.top + GetSystemMetrics(SM_CYSIZE) - 2;
738 739 740 741 742
    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
743 744 745
}

/******************************************************************************
746
 *   NC_DrawMinButton
Alexandre Julliard's avatar
Alexandre Julliard committed
747
 *
748
 *   Draws the minimize button for windows.
749
 *   If bGrayed is true, then draw a disabled Minimize button
750
 */
751
static void  NC_DrawMinButton(HWND hwnd,HDC hdc,BOOL down, BOOL bGrayed)
Alexandre Julliard's avatar
Alexandre Julliard committed
752
{
753
    RECT rect;
754
    UINT flags = DFCS_CAPTIONMIN;
755
    DWORD style = GetWindowLongW( hwnd, GWL_STYLE );
756
    DWORD ex_style = GetWindowLongW( hwnd, GWL_EXSTYLE );
Alexandre Julliard's avatar
Alexandre Julliard committed
757

758
    /* never draw minimize box when window has WS_EX_TOOLWINDOW style */
759
    if (ex_style & WS_EX_TOOLWINDOW) return;
760

761
    NC_GetInsideRect( hwnd, COORDS_WINDOW, &rect, style, ex_style );
762
    if (style & WS_SYSMENU)
763
        rect.right -= GetSystemMetrics(SM_CXSIZE);
764 765 766
    if (style & (WS_MAXIMIZEBOX|WS_MINIMIZEBOX))
        rect.right -= GetSystemMetrics(SM_CXSIZE) - 2;
    rect.left = rect.right - GetSystemMetrics(SM_CXSIZE);
767
    rect.bottom = rect.top + GetSystemMetrics(SM_CYSIZE) - 2;
768 769 770 771 772
    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
773 774 775 776
}

/******************************************************************************
 *
777
 *   NC_DrawFrame
Alexandre Julliard's avatar
Alexandre Julliard committed
778 779 780 781 782
 *
 *   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
783
 *        on the outer edge is handled by NC_DoNCPaint.  As is the inner
Alexandre Julliard's avatar
Alexandre Julliard committed
784 785 786 787 788 789 790
 *        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...
791
 *
Alexandre Julliard's avatar
Alexandre Julliard committed
792 793
 *****************************************************************************/

794
static void  NC_DrawFrame( HDC  hdc, RECT  *rect, BOOL  active, DWORD style, DWORD exStyle)
Alexandre Julliard's avatar
Alexandre Julliard committed
795
{
796
    INT width, height;
Alexandre Julliard's avatar
Alexandre Julliard committed
797

798 799
    /* Firstly the "thick" frame */
    if (style & WS_THICKFRAME)
Alexandre Julliard's avatar
Alexandre Julliard committed
800
    {
801 802
        width = GetSystemMetrics(SM_CXFRAME) - GetSystemMetrics(SM_CXDLGFRAME);
        height = GetSystemMetrics(SM_CYFRAME) - GetSystemMetrics(SM_CYDLGFRAME);
803 804

        SelectObject( hdc, GetSysColorBrush(active ? COLOR_ACTIVEBORDER :
805
                                            COLOR_INACTIVEBORDER) );
806 807 808 809 810 811 812 813 814 815 816
        /* 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
817
    }
818 819 820 821

    /* Now the other bit of the frame */
    if ((style & (WS_BORDER|WS_DLGFRAME)) ||
        (exStyle & WS_EX_DLGMODALFRAME))
Alexandre Julliard's avatar
Alexandre Julliard committed
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
        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
848 849 850
    }
}

Alexandre Julliard's avatar
Alexandre Julliard committed
851

Alexandre Julliard's avatar
Alexandre Julliard committed
852 853
/******************************************************************************
 *
854
 *   NC_DrawCaption
Alexandre Julliard's avatar
Alexandre Julliard committed
855
 *
856
 *   Draw the window caption for windows.
Alexandre Julliard's avatar
Alexandre Julliard committed
857 858 859 860
 *   The correct pen for the window frame must be selected in the DC.
 *
 *****************************************************************************/

861 862
static void  NC_DrawCaption( HDC  hdc, RECT *rect, HWND hwnd, DWORD  style, 
                             DWORD  exStyle, BOOL active )
Alexandre Julliard's avatar
Alexandre Julliard committed
863
{
864
    RECT  r = *rect;
865
    WCHAR buffer[256];
866
    HPEN  hPrevPen;
867
    HMENU hSysMenu;
868
    BOOL gradient = FALSE;
Alexandre Julliard's avatar
Alexandre Julliard committed
869

870
    hPrevPen = SelectObject( hdc, SYSCOLOR_GetPen(
871 872 873
                     ((exStyle & (WS_EX_STATICEDGE|WS_EX_CLIENTEDGE|
                                 WS_EX_DLGMODALFRAME)) == WS_EX_STATICEDGE) ?
                      COLOR_WINDOWFRAME : COLOR_3DFACE) );
874 875 876
    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
877 878
    r.bottom--;

879
    SystemParametersInfoW (SPI_GETGRADIENTCAPTIONS, 0, &gradient, 0);
880
    NC_DrawCaptionBar (hdc, &r, style, active, gradient);
Alexandre Julliard's avatar
Alexandre Julliard committed
881

882
    if ((style & WS_SYSMENU) && !(exStyle & WS_EX_TOOLWINDOW)) {
883 884
        if (NC_DrawSysButton (hwnd, hdc, FALSE))
            r.left += GetSystemMetrics(SM_CXSMICON) + 2;
885
    }
886

887
    if (style & WS_SYSMENU)
888
    {
889
        UINT state;
890

891 892 893
        /* Go get the sysmenu */
        hSysMenu = GetSystemMenu(hwnd, FALSE);
        state = GetMenuState(hSysMenu, SC_CLOSE, MF_BYCOMMAND);
894

895 896 897 898
        /* 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;
899

900 901 902 903
        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*/
904

905 906
            NC_DrawMaxButton( hwnd, hdc, FALSE, (!(style & WS_MAXIMIZEBOX)));
            r.right -= GetSystemMetrics(SM_CXSIZE) + 1;
907

908 909 910
            NC_DrawMinButton( hwnd, hdc, FALSE,  (!(style & WS_MINIMIZEBOX)));
            r.right -= GetSystemMetrics(SM_CXSIZE) + 1;
        }
Alexandre Julliard's avatar
Alexandre Julliard committed
911 912
    }

913
    if (GetWindowTextW( hwnd, buffer, ARRAY_SIZE( buffer )))
914
    {
915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930
        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
931 932 933 934 935
    }
}


/******************************************************************************
936
 *   NC_DoNCPaint
Alexandre Julliard's avatar
Alexandre Julliard committed
937
 *
938 939
 *   Paint the non-client area for windows.
 */
940
static void  NC_DoNCPaint( HWND  hwnd, HRGN  clip )
Alexandre Julliard's avatar
Alexandre Julliard committed
941
{
942
    HDC hdc;
943
    RECT rfuzz, rect, rectClip;
944
    BOOL active;
945 946 947
    WND *wndPtr;
    DWORD dwStyle, dwExStyle;
    WORD flags;
948
    HRGN hrgn;
949
    RECT rectClient;
950 951 952

    if (!(wndPtr = WIN_GetPtr( hwnd )) || wndPtr == WND_OTHER_PROCESS) return;
    dwStyle = wndPtr->dwStyle;
953
    dwExStyle = wndPtr->dwExStyle;
954 955 956 957
    flags = wndPtr->flags;
    WIN_ReleasePtr( wndPtr );

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

960
    active  = flags & WIN_NCACTIVATED;
Alexandre Julliard's avatar
Alexandre Julliard committed
961

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

964 965 966 967 968
    /* 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?
969 970
     */

971
    WIN_GetRectangles( hwnd, COORDS_SCREEN, NULL, &rectClient );
972 973
    hrgn = CreateRectRgnIndirect( &rectClient );

974 975 976 977 978 979
    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
980
    {
981
        hdc = GetDCEx( hwnd, hrgn, DCX_USESTYLE | DCX_WINDOW | DCX_EXCLUDERGN );
Alexandre Julliard's avatar
Alexandre Julliard committed
982 983
    }

984 985
    if (!hdc) return;

986
    WIN_GetRectangles( hwnd, COORDS_WINDOW, &rect, NULL );
987
    GetClipBox( hdc, &rectClip );
988

989
    SelectObject( hdc, SYSCOLOR_GetPen(COLOR_WINDOWFRAME) );
Alexandre Julliard's avatar
Alexandre Julliard committed
990

991
    if (HAS_STATICOUTERFRAME(dwStyle, dwExStyle)) {
992
        DrawEdge (hdc, &rect, BDR_SUNKENOUTER, BF_RECT | BF_ADJUST);
993
    }
994
    else if (HAS_BIGFRAME( dwStyle, dwExStyle)) {
995
        DrawEdge (hdc, &rect, EDGE_RAISED, BF_RECT | BF_ADJUST);
996
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
997

998
    NC_DrawFrame(hdc, &rect, active, dwStyle, dwExStyle );
999

1000
    if ((dwStyle & WS_CAPTION) == WS_CAPTION)
1001 1002
    {
        RECT  r = rect;
1003
        if (dwExStyle & WS_EX_TOOLWINDOW) {
1004 1005
            r.bottom = rect.top + GetSystemMetrics(SM_CYSMCAPTION);
            rect.top += GetSystemMetrics(SM_CYSMCAPTION);
Alexandre Julliard's avatar
Alexandre Julliard committed
1006
        }
1007 1008 1009 1010
        else {
            r.bottom = rect.top + GetSystemMetrics(SM_CYCAPTION);
            rect.top += GetSystemMetrics(SM_CYCAPTION);
        }
1011
        if( IntersectRect( &rfuzz, &r, &rectClip ) )
1012
            NC_DrawCaption(hdc, &r, hwnd, dwStyle, dwExStyle, active);
Alexandre Julliard's avatar
Alexandre Julliard committed
1013 1014
    }

1015
    if (HAS_MENU( hwnd, dwStyle ))
Alexandre Julliard's avatar
Alexandre Julliard committed
1016
    {
1017
	RECT r = rect;
1018
	r.bottom = rect.top + GetSystemMetrics(SM_CYMENU);
1019

1020
	TRACE("Calling DrawMenuBar with rect (%s)\n", wine_dbgstr_rect(&r));
Alexandre Julliard's avatar
Alexandre Julliard committed
1021

1022
	rect.top += MENU_DrawMenuBar( hdc, &r, hwnd ) + 1;
Alexandre Julliard's avatar
Alexandre Julliard committed
1023 1024
    }

1025
    TRACE("After MenuBar, rect is (%s).\n", wine_dbgstr_rect(&rect));
Alexandre Julliard's avatar
Alexandre Julliard committed
1026

1027
    if (dwExStyle & WS_EX_CLIENTEDGE)
1028
	DrawEdge (hdc, &rect, EDGE_SUNKEN, BF_RECT | BF_ADJUST);
Alexandre Julliard's avatar
Alexandre Julliard committed
1029 1030 1031

    /* Draw the scroll-bars */

1032
    if (dwStyle & WS_VSCROLL)
1033
        SCROLL_DrawScrollBar( hwnd, hdc, SB_VERT, TRUE, TRUE );
1034
    if (dwStyle & WS_HSCROLL)
1035
        SCROLL_DrawScrollBar( hwnd, hdc, SB_HORZ, TRUE, TRUE );
Alexandre Julliard's avatar
Alexandre Julliard committed
1036 1037

    /* Draw the "size-box" */
1038
    if ((dwStyle & WS_VSCROLL) && (dwStyle & WS_HSCROLL))
Alexandre Julliard's avatar
Alexandre Julliard committed
1039
    {
1040
        RECT r = rect;
1041 1042 1043 1044
        if((dwExStyle & WS_EX_LEFTSCROLLBAR) != 0)
            r.right = r.left + GetSystemMetrics(SM_CXVSCROLL) + 1;
        else
            r.left = r.right - GetSystemMetrics(SM_CXVSCROLL) + 1;
1045
        r.top  = r.bottom - GetSystemMetrics(SM_CYHSCROLL) + 1;
1046
        FillRect( hdc, &r,  GetSysColorBrush(COLOR_SCROLLBAR) );
1047
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
1048

1049
    ReleaseDC( hwnd, hdc );
Alexandre Julliard's avatar
Alexandre Julliard committed
1050 1051 1052 1053
}



Alexandre Julliard's avatar
Alexandre Julliard committed
1054

Alexandre Julliard's avatar
Alexandre Julliard committed
1055 1056 1057 1058 1059
/***********************************************************************
 *           NC_HandleNCPaint
 *
 * Handle a WM_NCPAINT message. Called from DefWindowProc().
 */
1060
LRESULT NC_HandleNCPaint( HWND hwnd , HRGN clip)
Alexandre Julliard's avatar
Alexandre Julliard committed
1061
{
1062
    HWND parent = GetAncestor( hwnd, GA_PARENT );
1063
    DWORD dwStyle = GetWindowLongW( hwnd, GWL_STYLE );
Alexandre Julliard's avatar
Alexandre Julliard committed
1064

1065
    if( dwStyle & WS_VISIBLE )
Alexandre Julliard's avatar
Alexandre Julliard committed
1066
    {
1067
	if( dwStyle & WS_MINIMIZE )
Alexandre Julliard's avatar
Alexandre Julliard committed
1068
	    WINPOS_RedrawIconTitle( hwnd );
Alexandre Julliard's avatar
Alexandre Julliard committed
1069
	else
1070
	    NC_DoNCPaint( hwnd, clip );
1071 1072 1073

        if (parent == GetDesktopWindow())
            PostMessageW( parent, WM_PARENTNOTIFY, WM_NCPAINT, (LPARAM)hwnd );
Alexandre Julliard's avatar
Alexandre Julliard committed
1074
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
1075 1076 1077 1078 1079
    return 0;
}


/***********************************************************************
Alexandre Julliard's avatar
Alexandre Julliard committed
1080
 *           NC_HandleNCActivate
Alexandre Julliard's avatar
Alexandre Julliard committed
1081
 *
Alexandre Julliard's avatar
Alexandre Julliard committed
1082
 * Handle a WM_NCACTIVATE message. Called from DefWindowProc().
Alexandre Julliard's avatar
Alexandre Julliard committed
1083
 */
1084
LRESULT NC_HandleNCActivate( HWND hwnd, WPARAM wParam, LPARAM lParam )
Alexandre Julliard's avatar
Alexandre Julliard committed
1085
{
1086 1087 1088 1089 1090
    /* 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.
     */
1091 1092
    if (wParam) win_set_flags( hwnd, WIN_NCACTIVATED, 0 );
    else win_set_flags( hwnd, 0, WIN_NCACTIVATED );
1093

1094 1095 1096 1097 1098 1099 1100 1101
    /* This isn't documented but is reproducible in at least XP SP2 and
     * Outlook 2007 depends on it
     */
    if (lParam != -1)
    {
        if (IsIconic(hwnd))
            WINPOS_RedrawIconTitle( hwnd );
        else
1102
            NC_DoNCPaint( hwnd, (HRGN)1 );
1103

1104 1105
        if (GetAncestor( hwnd, GA_PARENT ) == GetDesktopWindow())
            PostMessageW( GetDesktopWindow(), WM_PARENTNOTIFY, WM_NCACTIVATE, (LPARAM)hwnd );
1106
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
1107

Alexandre Julliard's avatar
Alexandre Julliard committed
1108 1109
    return TRUE;
}
Alexandre Julliard's avatar
Alexandre Julliard committed
1110

Alexandre Julliard's avatar
Alexandre Julliard committed
1111

Alexandre Julliard's avatar
Alexandre Julliard committed
1112 1113 1114 1115 1116
/***********************************************************************
 *           NC_HandleSetCursor
 *
 * Handle a WM_SETCURSOR message. Called from DefWindowProc().
 */
1117
LRESULT NC_HandleSetCursor( HWND hwnd, WPARAM wParam, LPARAM lParam )
Alexandre Julliard's avatar
Alexandre Julliard committed
1118
{
1119
    hwnd = WIN_GetFullHandle( (HWND)wParam );
Alexandre Julliard's avatar
Alexandre Julliard committed
1120

1121
    switch((short)LOWORD(lParam))
Alexandre Julliard's avatar
Alexandre Julliard committed
1122 1123
    {
    case HTERROR:
1124 1125 1126 1127 1128 1129 1130
        {
            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
1131 1132

    case HTCLIENT:
1133 1134 1135 1136 1137 1138
        {
            HCURSOR hCursor = (HCURSOR)GetClassLongPtrW(hwnd, GCLP_HCURSOR);
            if(hCursor) {
                SetCursor(hCursor);
                return TRUE;
            }
1139
            return FALSE;
1140
        }
Alexandre Julliard's avatar
Alexandre Julliard committed
1141 1142 1143

    case HTLEFT:
    case HTRIGHT:
1144
        return (LRESULT)SetCursor( LoadCursorA( 0, (LPSTR)IDC_SIZEWE ) );
Alexandre Julliard's avatar
Alexandre Julliard committed
1145 1146 1147

    case HTTOP:
    case HTBOTTOM:
1148
        return (LRESULT)SetCursor( LoadCursorA( 0, (LPSTR)IDC_SIZENS ) );
Alexandre Julliard's avatar
Alexandre Julliard committed
1149 1150

    case HTTOPLEFT:
1151
    case HTBOTTOMRIGHT:
1152
        return (LRESULT)SetCursor( LoadCursorA( 0, (LPSTR)IDC_SIZENWSE ) );
Alexandre Julliard's avatar
Alexandre Julliard committed
1153 1154 1155

    case HTTOPRIGHT:
    case HTBOTTOMLEFT:
1156
        return (LRESULT)SetCursor( LoadCursorA( 0, (LPSTR)IDC_SIZENESW ) );
Alexandre Julliard's avatar
Alexandre Julliard committed
1157 1158 1159
    }

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

Alexandre Julliard's avatar
Alexandre Julliard committed
1163 1164 1165
/***********************************************************************
 *           NC_GetSysPopupPos
 */
1166
void NC_GetSysPopupPos( HWND hwnd, RECT* rect )
Alexandre Julliard's avatar
Alexandre Julliard committed
1167
{
1168 1169 1170
    if (IsIconic(hwnd)) GetWindowRect( hwnd, rect );
    else
    {
1171 1172
        DWORD style = GetWindowLongW( hwnd, GWL_STYLE );
        DWORD ex_style = GetWindowLongW( hwnd, GWL_EXSTYLE );
1173

1174
        NC_GetInsideRect( hwnd, COORDS_CLIENT, rect, style, ex_style );
1175 1176
        rect->right = rect->left + GetSystemMetrics(SM_CYCAPTION) - 1;
        rect->bottom = rect->top + GetSystemMetrics(SM_CYCAPTION) - 1;
1177
        MapWindowPoints( hwnd, 0, (POINT *)rect, 2 );
1178
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
1179
}
Alexandre Julliard's avatar
Alexandre Julliard committed
1180

1181
/***********************************************************************
1182
 *           NC_TrackMinMaxBox
1183 1184 1185 1186 1187 1188 1189 1190
 *
 * 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.
 *
 */
1191
static void NC_TrackMinMaxBox( HWND hwnd, WORD wParam )
1192 1193 1194 1195 1196
{
    MSG msg;
    HDC hdc = GetWindowDC( hwnd );
    BOOL pressed = TRUE;
    UINT state;
1197
    DWORD wndStyle = GetWindowLongW( hwnd, GWL_STYLE);
1198 1199
    HMENU hSysMenu = GetSystemMenu(hwnd, FALSE);

1200
    void  (*paintButton)(HWND, HDC, BOOL, BOOL);
1201 1202 1203

    if (wParam == HTMINBUTTON)
    {
1204 1205 1206
        /* If the style is not present, do nothing */
        if (!(wndStyle & WS_MINIMIZEBOX))
            return;
1207

1208 1209
        /* Check if the sysmenu item for minimize is there  */
        state = GetMenuState(hSysMenu, SC_MINIMIZE, MF_BYCOMMAND);
1210

1211
        paintButton = NC_DrawMinButton;
1212 1213 1214
    }
    else
    {
1215 1216 1217
        /* If the style is not present, do nothing */
        if (!(wndStyle & WS_MAXIMIZEBOX))
            return;
1218

1219 1220
        /* Check if the sysmenu item for maximize is there  */
        state = GetMenuState(hSysMenu, SC_MAXIMIZE, MF_BYCOMMAND);
1221

1222
        paintButton = NC_DrawMaxButton;
1223 1224 1225 1226 1227 1228
    }

    SetCapture( hwnd );

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

1229
    while(1)
1230
    {
1231
        BOOL oldstate = pressed;
1232 1233 1234

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

1236 1237
        if(msg.message == WM_LBUTTONUP)
            break;
1238

1239 1240
        if(msg.message != WM_MOUSEMOVE)
            continue;
1241

1242 1243 1244
        pressed = (NC_HandleNCHitTest( hwnd, msg.pt ) == wParam);
        if (pressed != oldstate)
           (*paintButton)( hwnd, hdc, pressed, FALSE);
1245
    }
1246

1247
    if(pressed)
1248
        (*paintButton)(hwnd, hdc, FALSE, FALSE);
1249 1250 1251 1252

    ReleaseCapture();
    ReleaseDC( hwnd, hdc );

1253
    /* If the minimize or maximize items of the sysmenu are not there */
1254 1255
    /* or if the style is not present, do nothing */
    if ((!pressed) || (state == 0xFFFFFFFF))
1256
        return;
1257

1258
    if (wParam == HTMINBUTTON)
1259
        SendMessageW( hwnd, WM_SYSCOMMAND, SC_MINIMIZE, MAKELONG(msg.pt.x,msg.pt.y) );
1260
    else
1261
        SendMessageW( hwnd, WM_SYSCOMMAND,
1262
                      IsZoomed(hwnd) ? SC_RESTORE:SC_MAXIMIZE, MAKELONG(msg.pt.x,msg.pt.y) );
1263 1264
}

Alexandre Julliard's avatar
Alexandre Julliard committed
1265
/***********************************************************************
1266
 * NC_TrackCloseButton
Alexandre Julliard's avatar
Alexandre Julliard committed
1267 1268 1269
 *
 * Track a mouse button press on the Win95 close button.
 */
1270
static void NC_TrackCloseButton (HWND hwnd, WPARAM wParam, LPARAM lParam)
Alexandre Julliard's avatar
Alexandre Julliard committed
1271
{
1272
    MSG msg;
1273
    HDC hdc;
1274
    BOOL pressed = TRUE;
1275 1276 1277 1278
    HMENU hSysMenu = GetSystemMenu(hwnd, FALSE);
    UINT state;

    if(hSysMenu == 0)
1279
        return;
1280 1281

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

1283
    /* If the close item of the sysmenu is disabled or not present do nothing */
1284
    if((state & MF_DISABLED) || (state & MF_GRAYED) || (state == 0xFFFFFFFF))
1285
        return;
1286 1287

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

1289
    SetCapture( hwnd );
Alexandre Julliard's avatar
Alexandre Julliard committed
1290

1291
    NC_DrawCloseButton (hwnd, hdc, TRUE, FALSE);
Alexandre Julliard's avatar
Alexandre Julliard committed
1292

1293
    while(1)
Alexandre Julliard's avatar
Alexandre Julliard committed
1294
    {
1295
        BOOL oldstate = pressed;
1296 1297 1298

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

1300 1301
        if(msg.message == WM_LBUTTONUP)
            break;
1302

1303 1304
        if(msg.message != WM_MOUSEMOVE)
            continue;
1305

1306 1307 1308
        pressed = (NC_HandleNCHitTest( hwnd, msg.pt ) == wParam);
        if (pressed != oldstate)
           NC_DrawCloseButton (hwnd, hdc, pressed, FALSE);
1309
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
1310

1311
    if(pressed)
1312
        NC_DrawCloseButton (hwnd, hdc, FALSE, FALSE);
Alexandre Julliard's avatar
Alexandre Julliard committed
1313 1314

    ReleaseCapture();
1315
    ReleaseDC( hwnd, hdc );
Alexandre Julliard's avatar
Alexandre Julliard committed
1316 1317
    if (!pressed) return;

1318
    SendMessageW( hwnd, WM_SYSCOMMAND, SC_CLOSE, lParam );
Alexandre Julliard's avatar
Alexandre Julliard committed
1319 1320 1321
}


Alexandre Julliard's avatar
Alexandre Julliard committed
1322 1323 1324 1325 1326
/***********************************************************************
 *           NC_TrackScrollBar
 *
 * Track a mouse button press on the horizontal or vertical scroll-bar.
 */
1327
static void NC_TrackScrollBar( HWND hwnd, WPARAM wParam, POINT pt )
Alexandre Julliard's avatar
Alexandre Julliard committed
1328
{
1329
    INT scrollbar;
Alexandre Julliard's avatar
Alexandre Julliard committed
1330

Alexandre Julliard's avatar
Alexandre Julliard committed
1331 1332
    if ((wParam & 0xfff0) == SC_HSCROLL)
    {
1333
        if ((wParam & 0x0f) != HTHSCROLL) return;
Alexandre Julliard's avatar
Alexandre Julliard committed
1334 1335 1336 1337
	scrollbar = SB_HORZ;
    }
    else  /* SC_VSCROLL */
    {
1338
        if ((wParam & 0x0f) != HTVSCROLL) return;
Alexandre Julliard's avatar
Alexandre Julliard committed
1339 1340
	scrollbar = SB_VERT;
    }
1341
    SCROLL_TrackScrollBar( hwnd, scrollbar, pt );
Alexandre Julliard's avatar
Alexandre Julliard committed
1342 1343
}

1344

Alexandre Julliard's avatar
Alexandre Julliard committed
1345 1346 1347 1348 1349
/***********************************************************************
 *           NC_HandleNCLButtonDown
 *
 * Handle a WM_NCLBUTTONDOWN message. Called from DefWindowProc().
 */
1350
LRESULT NC_HandleNCLButtonDown( HWND hwnd, WPARAM wParam, LPARAM lParam )
Alexandre Julliard's avatar
Alexandre Julliard committed
1351
{
1352
    LONG style = GetWindowLongW( hwnd, GWL_STYLE );
Alexandre Julliard's avatar
Alexandre Julliard committed
1353

Alexandre Julliard's avatar
Alexandre Julliard committed
1354 1355
    switch(wParam)  /* Hit test */
    {
Alexandre Julliard's avatar
Alexandre Julliard committed
1356
    case HTCAPTION:
1357
        {
1358 1359 1360 1361 1362 1363 1364 1365 1366
            HWND top = hwnd, parent;
            while(1)
            {
                if ((GetWindowLongW( top, GWL_STYLE ) & (WS_POPUP|WS_CHILD)) != WS_CHILD)
                    break;
                parent = GetAncestor( top, GA_PARENT );
                if (!parent || parent == GetDesktopWindow()) break;
                top = parent;
            }
Alexandre Julliard's avatar
Alexandre Julliard committed
1367

1368
            if (FOCUS_MouseActivate( top ) || (GetActiveWindow() == top))
1369 1370 1371
                SendMessageW( hwnd, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, lParam );
            break;
        }
Alexandre Julliard's avatar
Alexandre Julliard committed
1372 1373

    case HTSYSMENU:
1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384
         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
1385 1386

    case HTMENU:
1387 1388
        SendMessageW( hwnd, WM_SYSCOMMAND, SC_MOUSEMENU, lParam );
        break;
Alexandre Julliard's avatar
Alexandre Julliard committed
1389

Alexandre Julliard's avatar
Alexandre Julliard committed
1390
    case HTHSCROLL:
1391 1392
        SendMessageW( hwnd, WM_SYSCOMMAND, SC_HSCROLL + HTHSCROLL, lParam );
        break;
Alexandre Julliard's avatar
Alexandre Julliard committed
1393

Alexandre Julliard's avatar
Alexandre Julliard committed
1394
    case HTVSCROLL:
1395 1396
        SendMessageW( hwnd, WM_SYSCOMMAND, SC_VSCROLL + HTVSCROLL, lParam );
        break;
Alexandre Julliard's avatar
Alexandre Julliard committed
1397

Alexandre Julliard's avatar
Alexandre Julliard committed
1398
    case HTMINBUTTON:
Alexandre Julliard's avatar
Alexandre Julliard committed
1399
    case HTMAXBUTTON:
1400 1401
        NC_TrackMinMaxBox( hwnd, wParam );
        break;
Alexandre Julliard's avatar
Alexandre Julliard committed
1402

Alexandre Julliard's avatar
Alexandre Julliard committed
1403
    case HTCLOSE:
1404
        NC_TrackCloseButton (hwnd, wParam, lParam);
1405
        break;
1406

Alexandre Julliard's avatar
Alexandre Julliard committed
1407 1408 1409 1410 1411 1412 1413
    case HTLEFT:
    case HTRIGHT:
    case HTTOP:
    case HTTOPLEFT:
    case HTTOPRIGHT:
    case HTBOTTOM:
    case HTBOTTOMLEFT:
Alexandre Julliard's avatar
Alexandre Julliard committed
1414
    case HTBOTTOMRIGHT:
1415 1416 1417 1418 1419 1420 1421 1422 1423
        /* 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);
1424
        break;
Alexandre Julliard's avatar
Alexandre Julliard committed
1425 1426

    case HTBORDER:
1427
        break;
Alexandre Julliard's avatar
Alexandre Julliard committed
1428 1429 1430 1431 1432
    }
    return 0;
}


1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459
/***********************************************************************
 *           NC_HandleNCRButtonDown
 *
 * Handle a WM_NCRBUTTONDOWN message. Called from DefWindowProc().
 */
LRESULT NC_HandleNCRButtonDown( HWND hwnd, WPARAM wParam, LPARAM lParam )
{
    MSG msg;
    INT hittest = wParam;

    switch (hittest)
    {
    case HTCAPTION:
    case HTSYSMENU:
        SetCapture( hwnd );
        for (;;)
        {
            if (!GetMessageW( &msg, 0, WM_MOUSEFIRST, WM_MOUSELAST )) break;
            if (CallMsgFilterW( &msg, MSGF_MAX )) continue;
            if (msg.message == WM_RBUTTONUP)
            {
                hittest = NC_HandleNCHitTest( hwnd, msg.pt );
                break;
            }
        }
        ReleaseCapture();
        if (hittest == HTCAPTION || hittest == HTSYSMENU)
1460
            SendMessageW( hwnd, WM_CONTEXTMENU, (WPARAM)hwnd, MAKELPARAM(msg.pt.x, msg.pt.y));
1461 1462 1463 1464 1465 1466
        break;
    }
    return 0;
}


Alexandre Julliard's avatar
Alexandre Julliard committed
1467 1468 1469 1470 1471
/***********************************************************************
 *           NC_HandleNCLButtonDblClk
 *
 * Handle a WM_NCLBUTTONDBLCLK message. Called from DefWindowProc().
 */
1472
LRESULT NC_HandleNCLButtonDblClk( HWND hwnd, WPARAM wParam, LPARAM lParam )
Alexandre Julliard's avatar
Alexandre Julliard committed
1473
{
Alexandre Julliard's avatar
Alexandre Julliard committed
1474 1475 1476 1477
    /*
     * if this is an icon, send a restore since we are handling
     * a double click
     */
1478
    if (IsIconic(hwnd))
Alexandre Julliard's avatar
Alexandre Julliard committed
1479
    {
1480
        SendMessageW( hwnd, WM_SYSCOMMAND, SC_RESTORE, lParam );
Alexandre Julliard's avatar
Alexandre Julliard committed
1481
        return 0;
1482
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
1483

Alexandre Julliard's avatar
Alexandre Julliard committed
1484 1485 1486
    switch(wParam)  /* Hit test */
    {
    case HTCAPTION:
Alexandre Julliard's avatar
Alexandre Julliard committed
1487
        /* stop processing if WS_MAXIMIZEBOX is missing */
1488
        if (GetWindowLongW( hwnd, GWL_STYLE ) & WS_MAXIMIZEBOX)
1489 1490
            SendMessageW( hwnd, WM_SYSCOMMAND,
                          IsZoomed(hwnd) ? SC_RESTORE : SC_MAXIMIZE, lParam );
1491
        break;
Alexandre Julliard's avatar
Alexandre Julliard committed
1492 1493

    case HTSYSMENU:
1494 1495 1496
        {
            HMENU hSysMenu = GetSystemMenu(hwnd, FALSE);
            UINT state = GetMenuState(hSysMenu, SC_CLOSE, MF_BYCOMMAND);
1497

1498
            /* If the close item of the sysmenu is disabled or not present do nothing */
1499 1500 1501
            if ((state & (MF_DISABLED | MF_GRAYED)) || (state == 0xFFFFFFFF))
                break;

1502
            SendMessageW( hwnd, WM_SYSCOMMAND, SC_CLOSE, lParam );
1503 1504
            break;
        }
Alexandre Julliard's avatar
Alexandre Julliard committed
1505 1506

    case HTHSCROLL:
1507
        SendMessageW( hwnd, WM_SYSCOMMAND, SC_HSCROLL + HTHSCROLL, lParam );
1508
        break;
Alexandre Julliard's avatar
Alexandre Julliard committed
1509 1510

    case HTVSCROLL:
1511
        SendMessageW( hwnd, WM_SYSCOMMAND, SC_VSCROLL + HTVSCROLL, lParam );
1512
        break;
Alexandre Julliard's avatar
Alexandre Julliard committed
1513 1514 1515 1516 1517 1518 1519 1520 1521 1522
    }
    return 0;
}


/***********************************************************************
 *           NC_HandleSysCommand
 *
 * Handle a WM_SYSCOMMAND message. Called from DefWindowProc().
 */
1523
LRESULT NC_HandleSysCommand( HWND hwnd, WPARAM wParam, LPARAM lParam )
Alexandre Julliard's avatar
Alexandre Julliard committed
1524
{
1525
    TRACE("hwnd %p WM_SYSCOMMAND %lx %lx\n", hwnd, wParam, lParam );
1526 1527

    if (!IsWindowEnabled( hwnd )) return 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
1528

1529 1530 1531
    if (HOOK_CallHooks( WH_CBT, HCBT_SYSCOMMAND, wParam, lParam, TRUE ))
        return 0;

1532 1533 1534
    if (!USER_Driver->pSysCommand( hwnd, wParam, lParam ))
        return 0;

1535
    switch (wParam & 0xfff0)
Alexandre Julliard's avatar
Alexandre Julliard committed
1536 1537 1538
    {
    case SC_SIZE:
    case SC_MOVE:
1539
        WINPOS_SysCommandSizeMove( hwnd, wParam );
1540
        break;
Alexandre Julliard's avatar
Alexandre Julliard committed
1541 1542

    case SC_MINIMIZE:
1543
        if (hwnd == GetActiveWindow())
1544
            ShowOwnedPopups(hwnd,FALSE);
1545 1546
        ShowWindow( hwnd, SW_MINIMIZE );
        break;
Alexandre Julliard's avatar
Alexandre Julliard committed
1547 1548

    case SC_MAXIMIZE:
1549
        if (IsIconic(hwnd) && hwnd == GetActiveWindow())
1550
            ShowOwnedPopups(hwnd,TRUE);
1551 1552
        ShowWindow( hwnd, SW_MAXIMIZE );
        break;
Alexandre Julliard's avatar
Alexandre Julliard committed
1553 1554

    case SC_RESTORE:
1555
        if (IsIconic(hwnd) && hwnd == GetActiveWindow())
1556
            ShowOwnedPopups(hwnd,TRUE);
1557 1558
        ShowWindow( hwnd, SW_RESTORE );
        break;
Alexandre Julliard's avatar
Alexandre Julliard committed
1559 1560

    case SC_CLOSE:
1561
        return SendMessageW( hwnd, WM_CLOSE, 0, 0 );
Alexandre Julliard's avatar
Alexandre Julliard committed
1562 1563 1564

    case SC_VSCROLL:
    case SC_HSCROLL:
1565 1566
        {
            POINT pt;
1567 1568
            pt.x = (short)LOWORD(lParam);
            pt.y = (short)HIWORD(lParam);
1569 1570
            NC_TrackScrollBar( hwnd, wParam, pt );
        }
1571
        break;
Alexandre Julliard's avatar
Alexandre Julliard committed
1572

Alexandre Julliard's avatar
Alexandre Julliard committed
1573
    case SC_MOUSEMENU:
1574 1575
        {
            POINT pt;
1576 1577
            pt.x = (short)LOWORD(lParam);
            pt.y = (short)HIWORD(lParam);
1578 1579
            MENU_TrackMouseMenuBar( hwnd, wParam & 0x000F, pt );
        }
1580
        break;
Alexandre Julliard's avatar
Alexandre Julliard committed
1581

Alexandre Julliard's avatar
Alexandre Julliard committed
1582
    case SC_KEYMENU:
1583
        MENU_TrackKbdMenuBar( hwnd, wParam, (WCHAR)lParam );
1584
        break;
1585

Alexandre Julliard's avatar
Alexandre Julliard committed
1586
    case SC_TASKLIST:
1587 1588
        WinExec( "taskman.exe", SW_SHOWNORMAL );
        break;
Alexandre Julliard's avatar
Alexandre Julliard committed
1589 1590

    case SC_SCREENSAVE:
1591
        if (wParam == SC_ABOUTWINE)
1592 1593 1594 1595
        {
            HMODULE hmodule = LoadLibraryA( "shell32.dll" );
            if (hmodule)
            {
1596 1597 1598
                BOOL (WINAPI *aboutproc)(HWND, LPCSTR, LPCSTR, HICON);

                aboutproc = (void *)GetProcAddress( hmodule, "ShellAboutA" );
1599
                if (aboutproc) aboutproc( hwnd, PACKAGE_STRING, NULL, 0 );
1600 1601 1602
                FreeLibrary( hmodule );
            }
        }
1603
        break;
1604

Alexandre Julliard's avatar
Alexandre Julliard committed
1605 1606 1607 1608
    case SC_HOTKEY:
    case SC_ARRANGE:
    case SC_NEXTWINDOW:
    case SC_PREVWINDOW:
1609
        FIXME("unimplemented WM_SYSCOMMAND %04lx!\n", wParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
1610
        break;
Alexandre Julliard's avatar
Alexandre Julliard committed
1611 1612 1613
    }
    return 0;
}
1614

1615 1616 1617 1618 1619 1620 1621 1622 1623 1624
/***********************************************************************
 *		GetTitleBarInfo (USER32.@)
 * TODO: Handle STATE_SYSTEM_PRESSED
 */
BOOL WINAPI GetTitleBarInfo(HWND hwnd, PTITLEBARINFO tbi) {
    DWORD dwStyle;
    DWORD dwExStyle;

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

1625 1626 1627 1628 1629
    if(!tbi) {
        SetLastError(ERROR_NOACCESS);
        return FALSE;
    }

1630
    if(tbi->cbSize != sizeof(TITLEBARINFO)) {
1631
        TRACE("Invalid TITLEBARINFO size: %d\n", tbi->cbSize);
1632 1633 1634 1635 1636
        SetLastError(ERROR_INVALID_PARAMETER);
        return FALSE;
    }
    dwStyle = GetWindowLongW(hwnd, GWL_STYLE);
    dwExStyle = GetWindowLongW(hwnd, GWL_EXSTYLE);
1637
    NC_GetInsideRect(hwnd, COORDS_SCREEN, &tbi->rcTitleBar, dwStyle, dwExStyle);
1638 1639 1640 1641 1642 1643 1644 1645 1646

    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);
    }

1647
    ZeroMemory(tbi->rgstate, sizeof(tbi->rgstate));
1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680
    /* 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;
}