nonclient.c 53 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"
Alexandre Julliard's avatar
Alexandre Julliard committed
28
#include "win.h"
29
#include "user_private.h"
30
#include "controls.h"
31
#include "wine/debug.h"
Alexandre Julliard's avatar
Alexandre Julliard committed
32

33
WINE_DEFAULT_DEBUG_CHANNEL(nonclient);
34

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

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

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

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

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

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

57 58 59 60
#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
61

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

64

Alexandre Julliard's avatar
Alexandre Julliard committed
65
/******************************************************************************
66
 * NC_AdjustRectOuter
Alexandre Julliard's avatar
Alexandre Julliard committed
67
 *
Alexandre Julliard's avatar
Alexandre Julliard committed
68 69
 * 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
70
 *
71 72
 * PARAMS
 *     LPRECT  rect
Alexandre Julliard's avatar
Alexandre Julliard committed
73
 *     DWORD  style
74
 *     BOOL  menu
Alexandre Julliard's avatar
Alexandre Julliard committed
75
 *     DWORD  exStyle
Alexandre Julliard's avatar
Alexandre Julliard committed
76
 *
Alexandre Julliard's avatar
Alexandre Julliard committed
77 78 79 80
 * 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
81 82 83
 *
 *****************************************************************************/

Alexandre Julliard's avatar
Alexandre Julliard committed
84
static void
85
NC_AdjustRectOuter (LPRECT rect, DWORD style, BOOL menu, DWORD exStyle)
Alexandre Julliard's avatar
Alexandre Julliard committed
86
{
87
    int adjust;
Alexandre Julliard's avatar
Alexandre Julliard committed
88 89
    if(style & WS_ICONIC) return;

90
    if ((exStyle & (WS_EX_STATICEDGE|WS_EX_DLGMODALFRAME)) ==
91 92 93 94 95 96 97 98 99 100 101 102 103
        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 */
104
    if ((style & (WS_BORDER|WS_DLGFRAME)) ||
105 106 107 108
        (exStyle & WS_EX_DLGMODALFRAME))
        adjust++; /* The other border */

    InflateRect (rect, adjust, adjust);
109 110

    if ((style & WS_CAPTION) == WS_CAPTION)
Alexandre Julliard's avatar
Alexandre Julliard committed
111
    {
112 113
        if (exStyle & WS_EX_TOOLWINDOW)
            rect->top -= GetSystemMetrics(SM_CYSMCAPTION);
Alexandre Julliard's avatar
Alexandre Julliard committed
114
        else
115
            rect->top -= GetSystemMetrics(SM_CYCAPTION);
Alexandre Julliard's avatar
Alexandre Julliard committed
116
    }
117
    if (menu) rect->top -= GetSystemMetrics(SM_CYMENU);
Alexandre Julliard's avatar
Alexandre Julliard committed
118 119 120 121
}


/******************************************************************************
122
 * NC_AdjustRectInner
Alexandre Julliard's avatar
Alexandre Julliard committed
123 124 125 126
 *
 * Computes the size of the "inside" part of the window based on the
 * parameters of the client area.
 *
127 128
 * PARAMS
 *     LPRECT   rect
Alexandre Julliard's avatar
Alexandre Julliard committed
129 130 131 132 133 134 135 136 137 138 139
 *     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
140
NC_AdjustRectInner (LPRECT rect, DWORD style, DWORD exStyle)
Alexandre Julliard's avatar
Alexandre Julliard committed
141 142 143 144
{
    if(style & WS_ICONIC) return;

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

147 148 149 150 151 152 153
    if (style & WS_VSCROLL)
    {
        if((exStyle & WS_EX_LEFTSCROLLBAR) != 0)
            rect->left  -= GetSystemMetrics(SM_CXVSCROLL);
        else
            rect->right += GetSystemMetrics(SM_CXVSCROLL);
    }
154
    if (style & WS_HSCROLL) rect->bottom += GetSystemMetrics(SM_CYHSCROLL);
Alexandre Julliard's avatar
Alexandre Julliard committed
155 156 157
}


158 159 160

static HICON NC_IconForWindow( HWND hwnd )
{
161 162 163
    HICON hIcon = 0;
    WND *wndPtr = WIN_GetPtr( hwnd );

164
    if (wndPtr && wndPtr != WND_OTHER_PROCESS && wndPtr != WND_DESKTOP)
165 166 167 168 169
    {
        hIcon = wndPtr->hIconSmall;
        if (!hIcon) hIcon = wndPtr->hIcon;
        WIN_ReleasePtr( wndPtr );
    }
170 171
    if (!hIcon) hIcon = (HICON) GetClassLongPtrW( hwnd, GCLP_HICONSM );
    if (!hIcon) hIcon = (HICON) GetClassLongPtrW( hwnd, GCLP_HICON );
172 173 174 175

    /* If there is no hIcon specified and this is a modal dialog,
     * get the default one.
     */
176 177
    if (!hIcon && (GetWindowLongW( hwnd, GWL_STYLE ) & DS_MODALFRAME))
        hIcon = LoadImageW(0, (LPCWSTR)IDI_WINLOGO, IMAGE_ICON, 0, 0, LR_DEFAULTCOLOR);
178 179 180
    return hIcon;
}

181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 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
/* 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));
}

242
/***********************************************************************
243
 *		DrawCaption (USER32.@) Draws a caption bar
244 245 246 247 248 249 250
 *
 * PARAMS
 *     hwnd   [I]
 *     hdc    [I]
 *     lpRect [I]
 *     uFlags [I]
 *
251 252 253
 * RETURNS
 *     Success:
 *     Failure:
254 255
 */

256 257
BOOL WINAPI
DrawCaption (HWND hwnd, HDC hdc, const RECT *lpRect, UINT uFlags)
258
{
259
    return DrawCaptionTempW (hwnd, hdc, lpRect, 0, 0, NULL, uFlags & 0x103F);
260 261 262 263
}


/***********************************************************************
264
 *		DrawCaptionTempA (USER32.@)
265
 */
266 267 268 269 270 271
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;
272

273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
    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)
292
{
293
    RECT   rc = *rect;
294

295
    TRACE("(%p,%p,%p,%p,%p,%s,%08x)\n",
296
          hwnd, hdc, rect, hFont, hIcon, debugstr_w(str), uFlags);
297 298 299

    /* drawing background */
    if (uFlags & DC_INBUTTON) {
300 301 302 303 304 305 306 307
        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);
        }
308 309
    }
    else {
310
        DWORD style = GetWindowLongW (hwnd, GWL_STYLE);
311
        NC_DrawCaptionBar (hdc, &rc, style, uFlags & DC_ACTIVE, uFlags & DC_GRADIENT);
312 313 314 315 316
    }


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

319 320
        pt.x = rc.left + 2;
        pt.y = (rc.bottom + rc.top - GetSystemMetrics(SM_CYSMICON)) / 2;
321

322 323 324
        if (!hIcon) hIcon = NC_IconForWindow(hwnd);
        DrawIconEx (hdc, pt.x, pt.y, hIcon, GetSystemMetrics(SM_CXSMICON),
                    GetSystemMetrics(SM_CYSMICON), 0, 0, DI_NORMAL);
325
        rc.left += (rc.bottom - rc.top);
326 327 328 329
    }

    /* drawing text */
    if (uFlags & DC_TEXT) {
330
        HFONT hOldFont;
331

332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
        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));
368 369 370 371
    }

    /* drawing focus ??? */
    if (uFlags & 0x2000)
372
        FIXME("undocumented flag (0x2000)!\n");
373 374 375 376 377

    return 0;
}


Alexandre Julliard's avatar
Alexandre Julliard committed
378
/***********************************************************************
379
 *		AdjustWindowRect (USER32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
380
 */
381
BOOL WINAPI AdjustWindowRect( LPRECT rect, DWORD style, BOOL menu )
Alexandre Julliard's avatar
Alexandre Julliard committed
382
{
383
    return AdjustWindowRectEx( rect, style, menu, 0 );
Alexandre Julliard's avatar
Alexandre Julliard committed
384 385 386
}


387 388 389 390 391 392
/***********************************************************************
 *		AdjustWindowRectEx (USER32.@)
 */
BOOL WINAPI AdjustWindowRectEx( LPRECT rect, DWORD style, BOOL menu, DWORD exStyle )
{
    /* Correct the window style */
Alexandre Julliard's avatar
Alexandre Julliard committed
393
    style &= (WS_DLGFRAME | WS_BORDER | WS_THICKFRAME | WS_CHILD);
Alexandre Julliard's avatar
Alexandre Julliard committed
394
    exStyle &= (WS_EX_DLGMODALFRAME | WS_EX_CLIENTEDGE |
395
                WS_EX_STATICEDGE | WS_EX_TOOLWINDOW);
Alexandre Julliard's avatar
Alexandre Julliard committed
396 397
    if (exStyle & WS_EX_DLGMODALFRAME) style &= ~WS_THICKFRAME;

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

400 401 402
    NC_AdjustRectOuter( rect, style, menu, exStyle );
    NC_AdjustRectInner( rect, style, exStyle );

Alexandre Julliard's avatar
Alexandre Julliard committed
403
    return TRUE;
Alexandre Julliard's avatar
Alexandre Julliard committed
404 405 406 407 408 409 410 411
}


/***********************************************************************
 *           NC_HandleNCCalcSize
 *
 * Handle a WM_NCCALCSIZE message. Called from DefWindowProc().
 */
412
LRESULT NC_HandleNCCalcSize( HWND hwnd, RECT *winRect )
Alexandre Julliard's avatar
Alexandre Julliard committed
413
{
414
    RECT tmpRect = { 0, 0, 0, 0 };
415
    LRESULT result = 0;
416 417 418
    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
419

420 421 422
    if (winRect == NULL)
        return 0;

423 424
    if (cls_style & CS_VREDRAW) result |= WVR_VREDRAW;
    if (cls_style & CS_HREDRAW) result |= WVR_HREDRAW;
Alexandre Julliard's avatar
Alexandre Julliard committed
425

426 427
    if (!IsIconic(hwnd))
    {
428
        NC_AdjustRectOuter( &tmpRect, style, FALSE, exStyle );
Alexandre Julliard's avatar
Alexandre Julliard committed
429

430 431 432 433
        winRect->left   -= tmpRect.left;
        winRect->top    -= tmpRect.top;
        winRect->right  -= tmpRect.right;
        winRect->bottom -= tmpRect.bottom;
Alexandre Julliard's avatar
Alexandre Julliard committed
434

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

440 441 442
            winRect->top +=
                MENU_GetMenuBarHeight( hwnd,
                                       winRect->right - winRect->left,
443
                                       -tmpRect.left, -tmpRect.top );
444
        }
Alexandre Julliard's avatar
Alexandre Julliard committed
445

446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462
        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);
463 464 465 466 467 468

        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
469
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
470
    return result;
Alexandre Julliard's avatar
Alexandre Julliard committed
471 472 473 474
}


/***********************************************************************
Alexandre Julliard's avatar
Alexandre Julliard committed
475
 *           NC_GetInsideRect
Alexandre Julliard's avatar
Alexandre Julliard committed
476
 *
Alexandre Julliard's avatar
Alexandre Julliard committed
477 478 479
 * 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
480
 */
481
static void NC_GetInsideRect( HWND hwnd, RECT *rect )
Alexandre Julliard's avatar
Alexandre Julliard committed
482
{
483 484
    WND *wndPtr = WIN_GetPtr( hwnd );

485
    if (!wndPtr || wndPtr == WND_OTHER_PROCESS || wndPtr == WND_DESKTOP) return;
Alexandre Julliard's avatar
Alexandre Julliard committed
486 487 488 489 490

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

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

Alexandre Julliard's avatar
Alexandre Julliard committed
493
    /* Remove frame from rectangle */
494
    if (HAS_THICKFRAME( wndPtr->dwStyle, wndPtr->dwExStyle ))
Alexandre Julliard's avatar
Alexandre Julliard committed
495
    {
496
        InflateRect( rect, -GetSystemMetrics(SM_CXFRAME), -GetSystemMetrics(SM_CYFRAME) );
Alexandre Julliard's avatar
Alexandre Julliard committed
497
    }
498
    else if (HAS_DLGFRAME( wndPtr->dwStyle, wndPtr->dwExStyle ))
Alexandre Julliard's avatar
Alexandre Julliard committed
499
    {
500
        InflateRect( rect, -GetSystemMetrics(SM_CXDLGFRAME), -GetSystemMetrics(SM_CYDLGFRAME));
Alexandre Julliard's avatar
Alexandre Julliard committed
501
    }
502
    else if (HAS_THINFRAME( wndPtr->dwStyle ))
Alexandre Julliard's avatar
Alexandre Julliard committed
503
    {
504
        InflateRect( rect, -GetSystemMetrics(SM_CXBORDER), -GetSystemMetrics(SM_CYBORDER) );
Alexandre Julliard's avatar
Alexandre Julliard committed
505 506
    }

507 508
    /* We have additional border information if the window
     * is a child (but not an MDI child) */
509 510
    if ( (wndPtr->dwStyle & WS_CHILD)  &&
         ( (wndPtr->dwExStyle & WS_EX_MDICHILD) == 0 ) )
511
    {
512 513 514 515
        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
516
    }
517

518
END:
519
    WIN_ReleasePtr( wndPtr );
Alexandre Julliard's avatar
Alexandre Julliard committed
520 521 522
}


Alexandre Julliard's avatar
Alexandre Julliard committed
523
/***********************************************************************
Alexandre Julliard's avatar
Alexandre Julliard committed
524
 * NC_DoNCHitTest
Alexandre Julliard's avatar
Alexandre Julliard committed
525
 *
Andreas Mohr's avatar
Andreas Mohr committed
526
 * Handle a WM_NCHITTEST message. Called from NC_HandleNCHitTest().
Alexandre Julliard's avatar
Alexandre Julliard committed
527
 *
Alexandre Julliard's avatar
Alexandre Julliard committed
528
 * FIXME:  Just a modified copy of the Win 3.1 version.
Alexandre Julliard's avatar
Alexandre Julliard committed
529 530
 */

531
static LRESULT NC_DoNCHitTest (WND *wndPtr, POINT pt )
Alexandre Julliard's avatar
Alexandre Julliard committed
532
{
533 534
    RECT rect, rcClient;
    POINT ptClient;
Alexandre Julliard's avatar
Alexandre Julliard committed
535

536
    TRACE("hwnd=%p pt=%d,%d\n", wndPtr->obj.handle, pt.x, pt.y );
Alexandre Julliard's avatar
Alexandre Julliard committed
537

538
    GetWindowRect(wndPtr->obj.handle, &rect );
539
    if (!PtInRect( &rect, pt )) return HTNOWHERE;
Alexandre Julliard's avatar
Alexandre Julliard committed
540 541 542

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

543 544
    /* Check client area */
    ptClient = pt;
545 546
    ScreenToClient( wndPtr->obj.handle, &ptClient );
    GetClientRect( wndPtr->obj.handle, &rcClient );
547 548
    if (PtInRect( &rcClient, ptClient )) return HTCLIENT;

549 550
    /* Check borders */
    if (HAS_THICKFRAME( wndPtr->dwStyle, wndPtr->dwExStyle ))
Alexandre Julliard's avatar
Alexandre Julliard committed
551
    {
552 553
        InflateRect( &rect, -GetSystemMetrics(SM_CXFRAME), -GetSystemMetrics(SM_CYFRAME) );
        if (!PtInRect( &rect, pt ))
Alexandre Julliard's avatar
Alexandre Julliard committed
554
        {
555 556
            /* Check top sizing border */
            if (pt.y < rect.top)
Alexandre Julliard's avatar
Alexandre Julliard committed
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
                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
582 583
            }
        }
584 585 586 587 588 589 590 591 592
    }
    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
593

594
    /* Check caption */
Alexandre Julliard's avatar
Alexandre Julliard committed
595

596 597 598 599 600 601 602
    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
603
        {
604 605
            BOOL min_or_max_box = (wndPtr->dwStyle & WS_MAXIMIZEBOX) ||
                                  (wndPtr->dwStyle & WS_MINIMIZEBOX);
606 607
            /* Check system menu */
            if ((wndPtr->dwStyle & WS_SYSMENU) && !(wndPtr->dwExStyle & WS_EX_TOOLWINDOW))
Alexandre Julliard's avatar
Alexandre Julliard committed
608
            {
609
                if (NC_IconForWindow(wndPtr->obj.handle))
610
                    rect.left += GetSystemMetrics(SM_CYCAPTION) - 1;
Alexandre Julliard's avatar
Alexandre Julliard committed
611
            }
612 613 614 615
            if (pt.x < rect.left) return HTSYSMENU;

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

            /* Check maximize box */
            /* In win95 there is automatically a Maximize button when there is a minimize one*/
621
            if (min_or_max_box && !(wndPtr->dwExStyle & WS_EX_TOOLWINDOW))
622
                rect.right -= GetSystemMetrics(SM_CXSIZE);
623 624 625 626
            if (pt.x > rect.right) return HTMAXBUTTON;

            /* Check minimize box */
            /* In win95 there is automatically a Maximize button when there is a Maximize one*/
627
            if (min_or_max_box && !(wndPtr->dwExStyle & WS_EX_TOOLWINDOW))
628
                rect.right -= GetSystemMetrics(SM_CXSIZE);
629 630 631

            if (pt.x > rect.right) return HTMINBUTTON;
            return HTCAPTION;
Alexandre Julliard's avatar
Alexandre Julliard committed
632 633 634
        }
    }

Alexandre Julliard's avatar
Alexandre Julliard committed
635 636 637
      /* Check vertical scroll bar */

    if (wndPtr->dwStyle & WS_VSCROLL)
Alexandre Julliard's avatar
Alexandre Julliard committed
638
    {
639
        if((wndPtr->dwExStyle & WS_EX_LEFTSCROLLBAR) != 0)
640
            rcClient.left -= GetSystemMetrics(SM_CXVSCROLL);
641
        else
642 643
            rcClient.right += GetSystemMetrics(SM_CXVSCROLL);
        if (PtInRect( &rcClient, ptClient )) return HTVSCROLL;
Alexandre Julliard's avatar
Alexandre Julliard committed
644
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
645 646 647 648

      /* Check horizontal scroll bar */

    if (wndPtr->dwStyle & WS_HSCROLL)
Alexandre Julliard's avatar
Alexandre Julliard committed
649
    {
650 651 652 653 654 655 656 657 658 659
        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
660 661
    }

Alexandre Julliard's avatar
Alexandre Julliard committed
662 663 664
      /* Check menu bar */

    if (HAS_MENU(wndPtr))
Alexandre Julliard's avatar
Alexandre Julliard committed
665
    {
666 667
        if ((ptClient.y < 0) && (ptClient.x >= 0) && (ptClient.x < rcClient.right))
            return HTMENU;
Alexandre Julliard's avatar
Alexandre Julliard committed
668 669
    }

670
    /* Has to return HTNOWHERE if nothing was found
671 672
       Could happen when a window has a customized non client area */
    return HTNOWHERE;
Alexandre Julliard's avatar
Alexandre Julliard committed
673 674 675
}


Alexandre Julliard's avatar
Alexandre Julliard committed
676 677 678 679 680
/***********************************************************************
 * NC_HandleNCHitTest
 *
 * Handle a WM_NCHITTEST message. Called from DefWindowProc().
 */
681
LRESULT NC_HandleNCHitTest (HWND hwnd , POINT pt)
Alexandre Julliard's avatar
Alexandre Julliard committed
682
{
683
    LRESULT retvalue;
684
    WND *wndPtr = WIN_GetPtr( hwnd );
Alexandre Julliard's avatar
Alexandre Julliard committed
685

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

688
    retvalue = NC_DoNCHitTest (wndPtr, pt);
689
    WIN_ReleasePtr( wndPtr );
690
    return retvalue;
Alexandre Julliard's avatar
Alexandre Julliard committed
691 692 693
}


Alexandre Julliard's avatar
Alexandre Julliard committed
694 695
/******************************************************************************
 *
696
 *   NC_DrawSysButton
Alexandre Julliard's avatar
Alexandre Julliard committed
697
 *
698
 *   Draws the system icon.
Alexandre Julliard's avatar
Alexandre Julliard committed
699 700
 *
 *****************************************************************************/
701
BOOL NC_DrawSysButton (HWND hwnd, HDC hdc, BOOL down)
Alexandre Julliard's avatar
Alexandre Julliard committed
702
{
703
    HICON hIcon = NC_IconForWindow( hwnd );
Alexandre Julliard's avatar
Alexandre Julliard committed
704

705
    if (hIcon)
Alexandre Julliard's avatar
Alexandre Julliard committed
706
    {
707 708
        RECT rect;
        NC_GetInsideRect( hwnd, &rect );
709 710 711
        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
712
    }
713
    return (hIcon != 0);
Alexandre Julliard's avatar
Alexandre Julliard committed
714 715 716 717 718
}


/******************************************************************************
 *
719
 *   NC_DrawCloseButton
Alexandre Julliard's avatar
Alexandre Julliard committed
720
 *
721
 *   Draws the close button.
Alexandre Julliard's avatar
Alexandre Julliard committed
722
 *
723 724
 *   If bGrayed is true, then draw a disabled Close button
 *
Alexandre Julliard's avatar
Alexandre Julliard committed
725 726
 *****************************************************************************/

727
static void NC_DrawCloseButton (HWND hwnd, HDC hdc, BOOL down, BOOL bGrayed)
Alexandre Julliard's avatar
Alexandre Julliard committed
728
{
729
    RECT rect;
Alexandre Julliard's avatar
Alexandre Julliard committed
730

731 732 733
    NC_GetInsideRect( hwnd, &rect );

    /* A tool window has a smaller Close button */
734
    if (GetWindowLongW( hwnd, GWL_EXSTYLE ) & WS_EX_TOOLWINDOW)
Alexandre Julliard's avatar
Alexandre Julliard committed
735
    {
736 737 738
        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
739

740 741 742 743
        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
744
    }
745 746
    else
    {
747 748
        rect.left = rect.right - GetSystemMetrics(SM_CXSIZE);
        rect.bottom = rect.top + GetSystemMetrics(SM_CYSIZE) - 2;
749 750 751 752 753 754 755
        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
756 757 758
}

/******************************************************************************
759
 *   NC_DrawMaxButton
Alexandre Julliard's avatar
Alexandre Julliard committed
760
 *
761
 *   Draws the maximize button for windows.
762
 *   If bGrayed is true, then draw a disabled Maximize button
763
 */
764
static void NC_DrawMaxButton(HWND hwnd,HDC hdc,BOOL down, BOOL bGrayed)
Alexandre Julliard's avatar
Alexandre Julliard committed
765
{
766
    RECT rect;
767 768 769 770 771 772 773
    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;
774 775

    NC_GetInsideRect( hwnd, &rect );
776
    if (GetWindowLongW( hwnd, GWL_STYLE) & WS_SYSMENU)
777
        rect.right -= GetSystemMetrics(SM_CXSIZE);
778
    rect.left = rect.right - GetSystemMetrics(SM_CXSIZE);
779
    rect.bottom = rect.top + GetSystemMetrics(SM_CYSIZE) - 2;
780 781 782 783 784
    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
785 786 787
}

/******************************************************************************
788
 *   NC_DrawMinButton
Alexandre Julliard's avatar
Alexandre Julliard committed
789
 *
790
 *   Draws the minimize button for windows.
791
 *   If bGrayed is true, then draw a disabled Minimize button
792
 */
793
static void  NC_DrawMinButton(HWND hwnd,HDC hdc,BOOL down, BOOL bGrayed)
Alexandre Julliard's avatar
Alexandre Julliard committed
794
{
795
    RECT rect;
796
    UINT flags = DFCS_CAPTIONMIN;
797
    DWORD style = GetWindowLongW( hwnd, GWL_STYLE );
Alexandre Julliard's avatar
Alexandre Julliard committed
798

799 800 801 802
    /* never draw minimize box when window has WS_EX_TOOLWINDOW style */
    if (GetWindowLongW( hwnd, GWL_EXSTYLE ) & WS_EX_TOOLWINDOW)
        return;

803 804
    NC_GetInsideRect( hwnd, &rect );
    if (style & WS_SYSMENU)
805
        rect.right -= GetSystemMetrics(SM_CXSIZE);
806 807 808
    if (style & (WS_MAXIMIZEBOX|WS_MINIMIZEBOX))
        rect.right -= GetSystemMetrics(SM_CXSIZE) - 2;
    rect.left = rect.right - GetSystemMetrics(SM_CXSIZE);
809
    rect.bottom = rect.top + GetSystemMetrics(SM_CYSIZE) - 2;
810 811 812 813 814
    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
815 816 817 818
}

/******************************************************************************
 *
819
 *   NC_DrawFrame
Alexandre Julliard's avatar
Alexandre Julliard committed
820 821 822 823 824
 *
 *   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
825
 *        on the outer edge is handled by NC_DoNCPaint.  As is the inner
Alexandre Julliard's avatar
Alexandre Julliard committed
826 827 828 829 830 831 832
 *        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...
833
 *
Alexandre Julliard's avatar
Alexandre Julliard committed
834 835
 *****************************************************************************/

836
static void  NC_DrawFrame( HDC  hdc, RECT  *rect, BOOL  active, DWORD style, DWORD exStyle)
Alexandre Julliard's avatar
Alexandre Julliard committed
837
{
838
    INT width, height;
Alexandre Julliard's avatar
Alexandre Julliard committed
839

840 841
    /* Firstly the "thick" frame */
    if (style & WS_THICKFRAME)
Alexandre Julliard's avatar
Alexandre Julliard committed
842
    {
843 844
        width = GetSystemMetrics(SM_CXFRAME) - GetSystemMetrics(SM_CXDLGFRAME);
        height = GetSystemMetrics(SM_CYFRAME) - GetSystemMetrics(SM_CYDLGFRAME);
845 846

        SelectObject( hdc, GetSysColorBrush(active ? COLOR_ACTIVEBORDER :
847
                                            COLOR_INACTIVEBORDER) );
848 849 850 851 852 853 854 855 856 857 858
        /* 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
859
    }
860 861 862 863

    /* Now the other bit of the frame */
    if ((style & (WS_BORDER|WS_DLGFRAME)) ||
        (exStyle & WS_EX_DLGMODALFRAME))
Alexandre Julliard's avatar
Alexandre Julliard committed
864
    {
865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889
        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
890 891 892
    }
}

Alexandre Julliard's avatar
Alexandre Julliard committed
893

Alexandre Julliard's avatar
Alexandre Julliard committed
894 895
/******************************************************************************
 *
896
 *   NC_DrawCaption
Alexandre Julliard's avatar
Alexandre Julliard committed
897
 *
898
 *   Draw the window caption for windows.
Alexandre Julliard's avatar
Alexandre Julliard committed
899 900 901 902
 *   The correct pen for the window frame must be selected in the DC.
 *
 *****************************************************************************/

903 904
static void  NC_DrawCaption( HDC  hdc, RECT *rect, HWND hwnd, DWORD  style, 
                             DWORD  exStyle, BOOL active )
Alexandre Julliard's avatar
Alexandre Julliard committed
905
{
906
    RECT  r = *rect;
907
    WCHAR buffer[256];
908
    HPEN  hPrevPen;
909
    HMENU hSysMenu;
910
    BOOL gradient = FALSE;
Alexandre Julliard's avatar
Alexandre Julliard committed
911

912
    hPrevPen = SelectObject( hdc, SYSCOLOR_GetPen(
913 914 915
                     ((exStyle & (WS_EX_STATICEDGE|WS_EX_CLIENTEDGE|
                                 WS_EX_DLGMODALFRAME)) == WS_EX_STATICEDGE) ?
                      COLOR_WINDOWFRAME : COLOR_3DFACE) );
916 917 918
    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
919 920
    r.bottom--;

921
    SystemParametersInfoW (SPI_GETGRADIENTCAPTIONS, 0, &gradient, 0);
922
    NC_DrawCaptionBar (hdc, &r, style, active, gradient);
Alexandre Julliard's avatar
Alexandre Julliard committed
923

924
    if ((style & WS_SYSMENU) && !(exStyle & WS_EX_TOOLWINDOW)) {
925 926
        if (NC_DrawSysButton (hwnd, hdc, FALSE))
            r.left += GetSystemMetrics(SM_CXSMICON) + 2;
927
    }
928

929
    if (style & WS_SYSMENU)
930
    {
931
        UINT state;
932

933 934 935
        /* Go get the sysmenu */
        hSysMenu = GetSystemMenu(hwnd, FALSE);
        state = GetMenuState(hSysMenu, SC_CLOSE, MF_BYCOMMAND);
936

937 938 939 940
        /* 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;
941

942 943 944 945
        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*/
946

947 948
            NC_DrawMaxButton( hwnd, hdc, FALSE, (!(style & WS_MAXIMIZEBOX)));
            r.right -= GetSystemMetrics(SM_CXSIZE) + 1;
949

950 951 952
            NC_DrawMinButton( hwnd, hdc, FALSE,  (!(style & WS_MINIMIZEBOX)));
            r.right -= GetSystemMetrics(SM_CXSIZE) + 1;
        }
Alexandre Julliard's avatar
Alexandre Julliard committed
953 954
    }

955
    if (GetWindowTextW( hwnd, buffer, sizeof(buffer)/sizeof(WCHAR) ))
956
    {
957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972
        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
973 974 975 976 977
    }
}


/******************************************************************************
978
 *   NC_DoNCPaint
Alexandre Julliard's avatar
Alexandre Julliard committed
979
 *
980 981
 *   Paint the non-client area for windows.
 */
982
static void  NC_DoNCPaint( HWND  hwnd, HRGN  clip, BOOL  suppress_menupaint )
Alexandre Julliard's avatar
Alexandre Julliard committed
983
{
984
    HDC hdc;
985
    RECT rfuzz, rect, rectClip;
986
    BOOL active;
987 988 989
    WND *wndPtr;
    DWORD dwStyle, dwExStyle;
    WORD flags;
990
    HRGN hrgn;
991 992 993 994 995 996
    RECT rectClient, rectWindow;
    int has_menu;

    if (!(wndPtr = WIN_GetPtr( hwnd )) || wndPtr == WND_OTHER_PROCESS) return;
    has_menu = HAS_MENU(wndPtr);
    dwStyle = wndPtr->dwStyle;
997
    dwExStyle = wndPtr->dwExStyle;
998 999 1000 1001 1002
    flags = wndPtr->flags;
    rectWindow = wndPtr->rectWindow;
    WIN_ReleasePtr( wndPtr );

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

1005
    active  = flags & WIN_NCACTIVATED;
Alexandre Julliard's avatar
Alexandre Julliard committed
1006

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

1009 1010 1011 1012 1013
    /* 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?
1014 1015
     */

1016 1017 1018 1019
    GetClientRect( hwnd, &rectClient );
    MapWindowPoints( hwnd, 0, (POINT *)&rectClient, 2 );
    hrgn = CreateRectRgnIndirect( &rectClient );

1020 1021 1022 1023 1024 1025
    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
1026
    {
1027
        hdc = GetDCEx( hwnd, hrgn, DCX_USESTYLE | DCX_WINDOW | DCX_EXCLUDERGN );
Alexandre Julliard's avatar
Alexandre Julliard committed
1028 1029
    }

1030 1031
    if (!hdc) return;

Alexandre Julliard's avatar
Alexandre Julliard committed
1032
    rect.top = rect.left = 0;
1033 1034
    rect.right  = rectWindow.right - rectWindow.left;
    rect.bottom = rectWindow.bottom - rectWindow.top;
1035
    GetClipBox( hdc, &rectClip );
1036

1037
    SelectObject( hdc, SYSCOLOR_GetPen(COLOR_WINDOWFRAME) );
Alexandre Julliard's avatar
Alexandre Julliard committed
1038

1039
    if (HAS_STATICOUTERFRAME(dwStyle, dwExStyle)) {
1040
        DrawEdge (hdc, &rect, BDR_SUNKENOUTER, BF_RECT | BF_ADJUST);
1041
    }
1042
    else if (HAS_BIGFRAME( dwStyle, dwExStyle)) {
1043
        DrawEdge (hdc, &rect, EDGE_RAISED, BF_RECT | BF_ADJUST);
1044
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
1045

1046
    NC_DrawFrame(hdc, &rect, active, dwStyle, dwExStyle );
1047

1048
    if ((dwStyle & WS_CAPTION) == WS_CAPTION)
1049 1050
    {
        RECT  r = rect;
1051
        if (dwExStyle & WS_EX_TOOLWINDOW) {
1052 1053
            r.bottom = rect.top + GetSystemMetrics(SM_CYSMCAPTION);
            rect.top += GetSystemMetrics(SM_CYSMCAPTION);
Alexandre Julliard's avatar
Alexandre Julliard committed
1054
        }
1055 1056 1057 1058
        else {
            r.bottom = rect.top + GetSystemMetrics(SM_CYCAPTION);
            rect.top += GetSystemMetrics(SM_CYCAPTION);
        }
1059
        if( IntersectRect( &rfuzz, &r, &rectClip ) )
1060
            NC_DrawCaption(hdc, &r, hwnd, dwStyle, dwExStyle, active);
Alexandre Julliard's avatar
Alexandre Julliard committed
1061 1062
    }

1063
    if (has_menu)
Alexandre Julliard's avatar
Alexandre Julliard committed
1064
    {
1065
	RECT r = rect;
1066
	r.bottom = rect.top + GetSystemMetrics(SM_CYMENU);
1067

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

Alexandre Julliard's avatar
Alexandre Julliard committed
1070
	rect.top += MENU_DrawMenuBar( hdc, &r, hwnd, suppress_menupaint ) + 1;
Alexandre Julliard's avatar
Alexandre Julliard committed
1071 1072
    }

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

1075
    if (dwExStyle & WS_EX_CLIENTEDGE)
1076
	DrawEdge (hdc, &rect, EDGE_SUNKEN, BF_RECT | BF_ADJUST);
Alexandre Julliard's avatar
Alexandre Julliard committed
1077 1078 1079

    /* Draw the scroll-bars */

1080
    if (dwStyle & WS_VSCROLL)
1081
        SCROLL_DrawScrollBar( hwnd, hdc, SB_VERT, TRUE, TRUE );
1082
    if (dwStyle & WS_HSCROLL)
1083
        SCROLL_DrawScrollBar( hwnd, hdc, SB_HORZ, TRUE, TRUE );
Alexandre Julliard's avatar
Alexandre Julliard committed
1084 1085

    /* Draw the "size-box" */
1086
    if ((dwStyle & WS_VSCROLL) && (dwStyle & WS_HSCROLL))
Alexandre Julliard's avatar
Alexandre Julliard committed
1087
    {
1088
        RECT r = rect;
1089 1090 1091 1092
        if((dwExStyle & WS_EX_LEFTSCROLLBAR) != 0)
            r.right = r.left + GetSystemMetrics(SM_CXVSCROLL) + 1;
        else
            r.left = r.right - GetSystemMetrics(SM_CXVSCROLL) + 1;
1093
        r.top  = r.bottom - GetSystemMetrics(SM_CYHSCROLL) + 1;
1094
        FillRect( hdc, &r,  GetSysColorBrush(COLOR_SCROLLBAR) );
1095
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
1096

1097
    ReleaseDC( hwnd, hdc );
Alexandre Julliard's avatar
Alexandre Julliard committed
1098 1099 1100 1101
}



Alexandre Julliard's avatar
Alexandre Julliard committed
1102

Alexandre Julliard's avatar
Alexandre Julliard committed
1103 1104 1105 1106 1107
/***********************************************************************
 *           NC_HandleNCPaint
 *
 * Handle a WM_NCPAINT message. Called from DefWindowProc().
 */
1108
LRESULT NC_HandleNCPaint( HWND hwnd , HRGN clip)
Alexandre Julliard's avatar
Alexandre Julliard committed
1109
{
1110
    DWORD dwStyle = GetWindowLongW( hwnd, GWL_STYLE );
Alexandre Julliard's avatar
Alexandre Julliard committed
1111

1112
    if( dwStyle & WS_VISIBLE )
Alexandre Julliard's avatar
Alexandre Julliard committed
1113
    {
1114
	if( dwStyle & WS_MINIMIZE )
Alexandre Julliard's avatar
Alexandre Julliard committed
1115
	    WINPOS_RedrawIconTitle( hwnd );
Alexandre Julliard's avatar
Alexandre Julliard committed
1116
	else
1117
	    NC_DoNCPaint( hwnd, clip, FALSE );
Alexandre Julliard's avatar
Alexandre Julliard committed
1118
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
1119 1120 1121 1122 1123
    return 0;
}


/***********************************************************************
Alexandre Julliard's avatar
Alexandre Julliard committed
1124
 *           NC_HandleNCActivate
Alexandre Julliard's avatar
Alexandre Julliard committed
1125
 *
Alexandre Julliard's avatar
Alexandre Julliard committed
1126
 * Handle a WM_NCACTIVATE message. Called from DefWindowProc().
Alexandre Julliard's avatar
Alexandre Julliard committed
1127
 */
1128
LRESULT NC_HandleNCActivate( HWND hwnd, WPARAM wParam, LPARAM lParam )
Alexandre Julliard's avatar
Alexandre Julliard committed
1129
{
1130 1131 1132
    WND* wndPtr = WIN_GetPtr( hwnd );

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

1134 1135 1136 1137 1138
    /* 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.
     */
1139 1140 1141 1142
    if (wParam) wndPtr->flags |= WIN_NCACTIVATED;
    else wndPtr->flags &= ~WIN_NCACTIVATED;
    WIN_ReleasePtr( wndPtr );

1143 1144 1145 1146 1147 1148 1149 1150 1151 1152
    /* 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
            NC_DoNCPaint( hwnd, (HRGN)1, FALSE );
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
1153

Alexandre Julliard's avatar
Alexandre Julliard committed
1154 1155
    return TRUE;
}
Alexandre Julliard's avatar
Alexandre Julliard committed
1156

Alexandre Julliard's avatar
Alexandre Julliard committed
1157

Alexandre Julliard's avatar
Alexandre Julliard committed
1158 1159 1160 1161 1162
/***********************************************************************
 *           NC_HandleSetCursor
 *
 * Handle a WM_SETCURSOR message. Called from DefWindowProc().
 */
1163
LRESULT NC_HandleSetCursor( HWND hwnd, WPARAM wParam, LPARAM lParam )
Alexandre Julliard's avatar
Alexandre Julliard committed
1164
{
1165
    hwnd = WIN_GetFullHandle( (HWND)wParam );
Alexandre Julliard's avatar
Alexandre Julliard committed
1166

1167
    switch((short)LOWORD(lParam))
Alexandre Julliard's avatar
Alexandre Julliard committed
1168 1169
    {
    case HTERROR:
1170 1171 1172 1173 1174 1175 1176
        {
            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
1177 1178

    case HTCLIENT:
1179 1180 1181 1182 1183 1184
        {
            HCURSOR hCursor = (HCURSOR)GetClassLongPtrW(hwnd, GCLP_HCURSOR);
            if(hCursor) {
                SetCursor(hCursor);
                return TRUE;
            }
1185
            return FALSE;
1186
        }
Alexandre Julliard's avatar
Alexandre Julliard committed
1187 1188 1189

    case HTLEFT:
    case HTRIGHT:
1190
        return (LRESULT)SetCursor( LoadCursorA( 0, (LPSTR)IDC_SIZEWE ) );
Alexandre Julliard's avatar
Alexandre Julliard committed
1191 1192 1193

    case HTTOP:
    case HTBOTTOM:
1194
        return (LRESULT)SetCursor( LoadCursorA( 0, (LPSTR)IDC_SIZENS ) );
Alexandre Julliard's avatar
Alexandre Julliard committed
1195 1196

    case HTTOPLEFT:
1197
    case HTBOTTOMRIGHT:
1198
        return (LRESULT)SetCursor( LoadCursorA( 0, (LPSTR)IDC_SIZENWSE ) );
Alexandre Julliard's avatar
Alexandre Julliard committed
1199 1200 1201

    case HTTOPRIGHT:
    case HTBOTTOMLEFT:
1202
        return (LRESULT)SetCursor( LoadCursorA( 0, (LPSTR)IDC_SIZENESW ) );
Alexandre Julliard's avatar
Alexandre Julliard committed
1203 1204 1205
    }

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

Alexandre Julliard's avatar
Alexandre Julliard committed
1209 1210 1211
/***********************************************************************
 *           NC_GetSysPopupPos
 */
1212
void NC_GetSysPopupPos( HWND hwnd, RECT* rect )
Alexandre Julliard's avatar
Alexandre Julliard committed
1213
{
1214 1215 1216
    if (IsIconic(hwnd)) GetWindowRect( hwnd, rect );
    else
    {
1217
        WND *wndPtr = WIN_GetPtr( hwnd );
1218
        if (!wndPtr || wndPtr == WND_OTHER_PROCESS || wndPtr == WND_DESKTOP) return;
1219 1220 1221 1222

        NC_GetInsideRect( hwnd, rect );
        OffsetRect( rect, wndPtr->rectWindow.left, wndPtr->rectWindow.top);
        if (wndPtr->dwStyle & WS_CHILD)
1223
            ClientToScreen( GetParent(hwnd), (POINT *)rect );
1224 1225
        rect->right = rect->left + GetSystemMetrics(SM_CYCAPTION) - 1;
        rect->bottom = rect->top + GetSystemMetrics(SM_CYCAPTION) - 1;
1226
        WIN_ReleasePtr( wndPtr );
1227
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
1228
}
Alexandre Julliard's avatar
Alexandre Julliard committed
1229

1230
/***********************************************************************
1231
 *           NC_TrackMinMaxBox
1232 1233 1234 1235 1236 1237 1238 1239
 *
 * 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.
 *
 */
1240
static void NC_TrackMinMaxBox( HWND hwnd, WORD wParam )
1241 1242 1243 1244 1245
{
    MSG msg;
    HDC hdc = GetWindowDC( hwnd );
    BOOL pressed = TRUE;
    UINT state;
1246
    DWORD wndStyle = GetWindowLongW( hwnd, GWL_STYLE);
1247 1248
    HMENU hSysMenu = GetSystemMenu(hwnd, FALSE);

1249
    void  (*paintButton)(HWND, HDC, BOOL, BOOL);
1250 1251 1252

    if (wParam == HTMINBUTTON)
    {
1253 1254 1255
        /* If the style is not present, do nothing */
        if (!(wndStyle & WS_MINIMIZEBOX))
            return;
1256

1257 1258
        /* Check if the sysmenu item for minimize is there  */
        state = GetMenuState(hSysMenu, SC_MINIMIZE, MF_BYCOMMAND);
1259

1260
        paintButton = NC_DrawMinButton;
1261 1262 1263
    }
    else
    {
1264 1265 1266
        /* If the style is not present, do nothing */
        if (!(wndStyle & WS_MAXIMIZEBOX))
            return;
1267

1268 1269
        /* Check if the sysmenu item for maximize is there  */
        state = GetMenuState(hSysMenu, SC_MAXIMIZE, MF_BYCOMMAND);
1270

1271
        paintButton = NC_DrawMaxButton;
1272 1273 1274 1275 1276 1277
    }

    SetCapture( hwnd );

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

1278
    while(1)
1279
    {
1280
        BOOL oldstate = pressed;
1281 1282 1283

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

1285 1286
        if(msg.message == WM_LBUTTONUP)
            break;
1287

1288 1289
        if(msg.message != WM_MOUSEMOVE)
            continue;
1290

1291 1292 1293
        pressed = (NC_HandleNCHitTest( hwnd, msg.pt ) == wParam);
        if (pressed != oldstate)
           (*paintButton)( hwnd, hdc, pressed, FALSE);
1294
    }
1295

1296
    if(pressed)
1297
        (*paintButton)(hwnd, hdc, FALSE, FALSE);
1298 1299 1300 1301 1302 1303 1304

    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))
1305
        return;
1306

1307
    if (wParam == HTMINBUTTON)
1308
        SendMessageW( hwnd, WM_SYSCOMMAND, SC_MINIMIZE, MAKELONG(msg.pt.x,msg.pt.y) );
1309
    else
1310
        SendMessageW( hwnd, WM_SYSCOMMAND,
1311
                      IsZoomed(hwnd) ? SC_RESTORE:SC_MAXIMIZE, MAKELONG(msg.pt.x,msg.pt.y) );
1312 1313
}

Alexandre Julliard's avatar
Alexandre Julliard committed
1314
/***********************************************************************
1315
 * NC_TrackCloseButton
Alexandre Julliard's avatar
Alexandre Julliard committed
1316 1317 1318
 *
 * Track a mouse button press on the Win95 close button.
 */
1319
static void NC_TrackCloseButton (HWND hwnd, WPARAM wParam, LPARAM lParam)
Alexandre Julliard's avatar
Alexandre Julliard committed
1320
{
1321
    MSG msg;
1322
    HDC hdc;
1323
    BOOL pressed = TRUE;
1324 1325 1326 1327
    HMENU hSysMenu = GetSystemMenu(hwnd, FALSE);
    UINT state;

    if(hSysMenu == 0)
1328
        return;
1329 1330

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

1332 1333
    /* If the item close of the sysmenu is disabled or not there do nothing */
    if((state & MF_DISABLED) || (state & MF_GRAYED) || (state == 0xFFFFFFFF))
1334
        return;
1335 1336

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

1338
    SetCapture( hwnd );
Alexandre Julliard's avatar
Alexandre Julliard committed
1339

1340
    NC_DrawCloseButton (hwnd, hdc, TRUE, FALSE);
Alexandre Julliard's avatar
Alexandre Julliard committed
1341

1342
    while(1)
Alexandre Julliard's avatar
Alexandre Julliard committed
1343
    {
1344
        BOOL oldstate = pressed;
1345 1346 1347

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

1349 1350
        if(msg.message == WM_LBUTTONUP)
            break;
1351

1352 1353
        if(msg.message != WM_MOUSEMOVE)
            continue;
1354

1355 1356 1357
        pressed = (NC_HandleNCHitTest( hwnd, msg.pt ) == wParam);
        if (pressed != oldstate)
           NC_DrawCloseButton (hwnd, hdc, pressed, FALSE);
1358
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
1359

1360
    if(pressed)
1361
        NC_DrawCloseButton (hwnd, hdc, FALSE, FALSE);
Alexandre Julliard's avatar
Alexandre Julliard committed
1362 1363

    ReleaseCapture();
1364
    ReleaseDC( hwnd, hdc );
Alexandre Julliard's avatar
Alexandre Julliard committed
1365 1366
    if (!pressed) return;

1367
    SendMessageW( hwnd, WM_SYSCOMMAND, SC_CLOSE, lParam );
Alexandre Julliard's avatar
Alexandre Julliard committed
1368 1369 1370
}


Alexandre Julliard's avatar
Alexandre Julliard committed
1371 1372 1373 1374 1375
/***********************************************************************
 *           NC_TrackScrollBar
 *
 * Track a mouse button press on the horizontal or vertical scroll-bar.
 */
1376
static void NC_TrackScrollBar( HWND hwnd, WPARAM wParam, POINT pt )
Alexandre Julliard's avatar
Alexandre Julliard committed
1377
{
1378
    INT scrollbar;
Alexandre Julliard's avatar
Alexandre Julliard committed
1379

Alexandre Julliard's avatar
Alexandre Julliard committed
1380 1381
    if ((wParam & 0xfff0) == SC_HSCROLL)
    {
1382
        if ((wParam & 0x0f) != HTHSCROLL) return;
Alexandre Julliard's avatar
Alexandre Julliard committed
1383 1384 1385 1386
	scrollbar = SB_HORZ;
    }
    else  /* SC_VSCROLL */
    {
1387
        if ((wParam & 0x0f) != HTVSCROLL) return;
Alexandre Julliard's avatar
Alexandre Julliard committed
1388 1389
	scrollbar = SB_VERT;
    }
1390
    SCROLL_TrackScrollBar( hwnd, scrollbar, pt );
Alexandre Julliard's avatar
Alexandre Julliard committed
1391 1392
}

1393

Alexandre Julliard's avatar
Alexandre Julliard committed
1394 1395 1396 1397 1398
/***********************************************************************
 *           NC_HandleNCLButtonDown
 *
 * Handle a WM_NCLBUTTONDOWN message. Called from DefWindowProc().
 */
1399
LRESULT NC_HandleNCLButtonDown( HWND hwnd, WPARAM wParam, LPARAM lParam )
Alexandre Julliard's avatar
Alexandre Julliard committed
1400
{
1401
    LONG style = GetWindowLongW( hwnd, GWL_STYLE );
Alexandre Julliard's avatar
Alexandre Julliard committed
1402

Alexandre Julliard's avatar
Alexandre Julliard committed
1403 1404
    switch(wParam)  /* Hit test */
    {
Alexandre Julliard's avatar
Alexandre Julliard committed
1405
    case HTCAPTION:
1406
        {
1407
            HWND top = GetAncestor( hwnd, GA_ROOT );
Alexandre Julliard's avatar
Alexandre Julliard committed
1408

1409
            if (FOCUS_MouseActivate( top ) || (GetActiveWindow() == top))
1410 1411 1412
                SendMessageW( hwnd, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, lParam );
            break;
        }
Alexandre Julliard's avatar
Alexandre Julliard committed
1413 1414

    case HTSYSMENU:
1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425
         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
1426 1427

    case HTMENU:
1428 1429
        SendMessageW( hwnd, WM_SYSCOMMAND, SC_MOUSEMENU, lParam );
        break;
Alexandre Julliard's avatar
Alexandre Julliard committed
1430

Alexandre Julliard's avatar
Alexandre Julliard committed
1431
    case HTHSCROLL:
1432 1433
        SendMessageW( hwnd, WM_SYSCOMMAND, SC_HSCROLL + HTHSCROLL, lParam );
        break;
Alexandre Julliard's avatar
Alexandre Julliard committed
1434

Alexandre Julliard's avatar
Alexandre Julliard committed
1435
    case HTVSCROLL:
1436 1437
        SendMessageW( hwnd, WM_SYSCOMMAND, SC_VSCROLL + HTVSCROLL, lParam );
        break;
Alexandre Julliard's avatar
Alexandre Julliard committed
1438

Alexandre Julliard's avatar
Alexandre Julliard committed
1439
    case HTMINBUTTON:
Alexandre Julliard's avatar
Alexandre Julliard committed
1440
    case HTMAXBUTTON:
1441 1442
        NC_TrackMinMaxBox( hwnd, wParam );
        break;
Alexandre Julliard's avatar
Alexandre Julliard committed
1443

Alexandre Julliard's avatar
Alexandre Julliard committed
1444
    case HTCLOSE:
1445
        NC_TrackCloseButton (hwnd, wParam, lParam);
1446
        break;
1447

Alexandre Julliard's avatar
Alexandre Julliard committed
1448 1449 1450 1451 1452 1453 1454
    case HTLEFT:
    case HTRIGHT:
    case HTTOP:
    case HTTOPLEFT:
    case HTTOPRIGHT:
    case HTBOTTOM:
    case HTBOTTOMLEFT:
Alexandre Julliard's avatar
Alexandre Julliard committed
1455
    case HTBOTTOMRIGHT:
1456 1457 1458 1459 1460 1461 1462 1463 1464
        /* 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);
1465
        break;
Alexandre Julliard's avatar
Alexandre Julliard committed
1466 1467

    case HTBORDER:
1468
        break;
Alexandre Julliard's avatar
Alexandre Julliard committed
1469 1470 1471 1472 1473 1474 1475 1476 1477 1478
    }
    return 0;
}


/***********************************************************************
 *           NC_HandleNCLButtonDblClk
 *
 * Handle a WM_NCLBUTTONDBLCLK message. Called from DefWindowProc().
 */
1479
LRESULT NC_HandleNCLButtonDblClk( HWND hwnd, WPARAM wParam, LPARAM lParam )
Alexandre Julliard's avatar
Alexandre Julliard committed
1480
{
Alexandre Julliard's avatar
Alexandre Julliard committed
1481 1482 1483 1484
    /*
     * if this is an icon, send a restore since we are handling
     * a double click
     */
1485
    if (IsIconic(hwnd))
Alexandre Julliard's avatar
Alexandre Julliard committed
1486
    {
1487
        SendMessageW( hwnd, WM_SYSCOMMAND, SC_RESTORE, lParam );
Alexandre Julliard's avatar
Alexandre Julliard committed
1488
        return 0;
1489
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
1490

Alexandre Julliard's avatar
Alexandre Julliard committed
1491 1492 1493
    switch(wParam)  /* Hit test */
    {
    case HTCAPTION:
Alexandre Julliard's avatar
Alexandre Julliard committed
1494
        /* stop processing if WS_MAXIMIZEBOX is missing */
1495
        if (GetWindowLongW( hwnd, GWL_STYLE ) & WS_MAXIMIZEBOX)
1496 1497
            SendMessageW( hwnd, WM_SYSCOMMAND,
                          IsZoomed(hwnd) ? SC_RESTORE : SC_MAXIMIZE, lParam );
1498
        break;
Alexandre Julliard's avatar
Alexandre Julliard committed
1499 1500

    case HTSYSMENU:
1501 1502 1503
        {
            HMENU hSysMenu = GetSystemMenu(hwnd, FALSE);
            UINT state = GetMenuState(hSysMenu, SC_CLOSE, MF_BYCOMMAND);
1504

1505 1506 1507 1508
            /* If the item close of the sysmenu is disabled or not there do nothing */
            if ((state & (MF_DISABLED | MF_GRAYED)) || (state == 0xFFFFFFFF))
                break;

1509
            SendMessageW( hwnd, WM_SYSCOMMAND, SC_CLOSE, lParam );
1510 1511
            break;
        }
Alexandre Julliard's avatar
Alexandre Julliard committed
1512 1513

    case HTHSCROLL:
1514
        SendMessageW( hwnd, WM_SYSCOMMAND, SC_HSCROLL + HTHSCROLL, lParam );
1515
        break;
Alexandre Julliard's avatar
Alexandre Julliard committed
1516 1517

    case HTVSCROLL:
1518
        SendMessageW( hwnd, WM_SYSCOMMAND, SC_VSCROLL + HTVSCROLL, lParam );
1519
        break;
Alexandre Julliard's avatar
Alexandre Julliard committed
1520 1521 1522 1523 1524 1525 1526 1527 1528 1529
    }
    return 0;
}


/***********************************************************************
 *           NC_HandleSysCommand
 *
 * Handle a WM_SYSCOMMAND message. Called from DefWindowProc().
 */
1530
LRESULT NC_HandleSysCommand( HWND hwnd, WPARAM wParam, LPARAM lParam )
Alexandre Julliard's avatar
Alexandre Julliard committed
1531
{
1532
    TRACE("hwnd %p WM_SYSCOMMAND %lx %lx\n", hwnd, wParam, lParam );
1533 1534

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

1536 1537 1538
    if (HOOK_CallHooks( WH_CBT, HCBT_SYSCOMMAND, wParam, lParam, TRUE ))
        return 0;

1539 1540 1541
    if (!USER_Driver->pSysCommand( hwnd, wParam, lParam ))
        return 0;

1542
    switch (wParam & 0xfff0)
Alexandre Julliard's avatar
Alexandre Julliard committed
1543 1544 1545
    {
    case SC_SIZE:
    case SC_MOVE:
1546
        WINPOS_SysCommandSizeMove( hwnd, wParam );
1547
        break;
Alexandre Julliard's avatar
Alexandre Julliard committed
1548 1549

    case SC_MINIMIZE:
1550 1551
        if (hwnd == GetForegroundWindow())
            ShowOwnedPopups(hwnd,FALSE);
1552 1553
        ShowWindow( hwnd, SW_MINIMIZE );
        break;
Alexandre Julliard's avatar
Alexandre Julliard committed
1554 1555

    case SC_MAXIMIZE:
1556 1557
        if (IsIconic(hwnd) && hwnd == GetForegroundWindow())
            ShowOwnedPopups(hwnd,TRUE);
1558 1559
        ShowWindow( hwnd, SW_MAXIMIZE );
        break;
Alexandre Julliard's avatar
Alexandre Julliard committed
1560 1561

    case SC_RESTORE:
1562 1563
        if (IsIconic(hwnd) && hwnd == GetForegroundWindow())
            ShowOwnedPopups(hwnd,TRUE);
1564 1565
        ShowWindow( hwnd, SW_RESTORE );
        break;
Alexandre Julliard's avatar
Alexandre Julliard committed
1566 1567

    case SC_CLOSE:
1568
        return SendMessageW( hwnd, WM_CLOSE, 0, 0 );
Alexandre Julliard's avatar
Alexandre Julliard committed
1569 1570 1571

    case SC_VSCROLL:
    case SC_HSCROLL:
1572 1573
        {
            POINT pt;
1574 1575
            pt.x = (short)LOWORD(lParam);
            pt.y = (short)HIWORD(lParam);
1576 1577
            NC_TrackScrollBar( hwnd, wParam, pt );
        }
1578
        break;
Alexandre Julliard's avatar
Alexandre Julliard committed
1579

Alexandre Julliard's avatar
Alexandre Julliard committed
1580
    case SC_MOUSEMENU:
1581 1582
        {
            POINT pt;
1583 1584
            pt.x = (short)LOWORD(lParam);
            pt.y = (short)HIWORD(lParam);
1585 1586
            MENU_TrackMouseMenuBar( hwnd, wParam & 0x000F, pt );
        }
1587
        break;
Alexandre Julliard's avatar
Alexandre Julliard committed
1588

Alexandre Julliard's avatar
Alexandre Julliard committed
1589
    case SC_KEYMENU:
1590
        MENU_TrackKbdMenuBar( hwnd, wParam, (WCHAR)lParam );
1591
        break;
1592

Alexandre Julliard's avatar
Alexandre Julliard committed
1593
    case SC_TASKLIST:
1594 1595
        WinExec( "taskman.exe", SW_SHOWNORMAL );
        break;
Alexandre Julliard's avatar
Alexandre Julliard committed
1596 1597

    case SC_SCREENSAVE:
1598
        if (wParam == SC_ABOUTWINE)
1599 1600 1601 1602
        {
            HMODULE hmodule = LoadLibraryA( "shell32.dll" );
            if (hmodule)
            {
1603 1604 1605
                BOOL (WINAPI *aboutproc)(HWND, LPCSTR, LPCSTR, HICON);

                aboutproc = (void *)GetProcAddress( hmodule, "ShellAboutA" );
1606
                if (aboutproc) aboutproc( hwnd, PACKAGE_STRING, NULL, 0 );
1607 1608 1609
                FreeLibrary( hmodule );
            }
        }
1610
        break;
1611

Alexandre Julliard's avatar
Alexandre Julliard committed
1612 1613 1614 1615
    case SC_HOTKEY:
    case SC_ARRANGE:
    case SC_NEXTWINDOW:
    case SC_PREVWINDOW:
1616
        FIXME("unimplemented WM_SYSCOMMAND %04lx!\n", wParam);
Alexandre Julliard's avatar
Alexandre Julliard committed
1617
        break;
Alexandre Julliard's avatar
Alexandre Julliard committed
1618 1619 1620
    }
    return 0;
}
1621

1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633
/***********************************************************************
 *		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)) {
1634
        TRACE("Invalid TITLEBARINFO size: %d\n", tbi->cbSize);
1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655
        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);
    }

1656
    ZeroMemory(tbi->rgstate, sizeof(tbi->rgstate));
1657 1658 1659 1660 1661 1662 1663 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
    /* 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;
}