uitools.c 54.2 KB
Newer Older
Alexandre Julliard's avatar
Alexandre Julliard committed
1 2 3 4
/*
 * User Interface Functions
 *
 * Copyright 1997 Dimitrie O. Paun
Alexandre Julliard's avatar
Alexandre Julliard committed
5
 * Copyright 1997 Bertho A. Stultiens
6 7 8 9 10 11 12 13 14 15 16 17 18
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
Alexandre Julliard's avatar
Alexandre Julliard committed
20 21
 */

22 23
#include <stdarg.h>

24
#define WINE_NO_INLINE_RECT
25
#include "windef.h"
26
#include "winbase.h"
27
#include "wingdi.h"
28
#include "winuser.h"
29
#include "user_private.h"
30
#include "controls.h"
31
#include "wine/unicode.h"
32
#include "wine/debug.h"
Alexandre Julliard's avatar
Alexandre Julliard committed
33

34
WINE_DEFAULT_DEBUG_CHANNEL(graphics);
35

Alexandre Julliard's avatar
Alexandre Julliard committed
36 37 38 39
/* These tables are used in:
 * UITOOLS_DrawDiagEdge()
 * UITOOLS_DrawRectEdge()
 */
40
static const signed char LTInnerNormal[] = {
Alexandre Julliard's avatar
Alexandre Julliard committed
41 42 43 44 45 46
    -1,           -1,                 -1,                 -1,
    -1,           COLOR_BTNHIGHLIGHT, COLOR_BTNHIGHLIGHT, -1,
    -1,           COLOR_3DDKSHADOW,   COLOR_3DDKSHADOW,   -1,
    -1,           -1,                 -1,                 -1
};

47
static const signed char LTOuterNormal[] = {
Alexandre Julliard's avatar
Alexandre Julliard committed
48
    -1,                 COLOR_3DLIGHT,     COLOR_BTNSHADOW, -1,
49
    COLOR_BTNHIGHLIGHT, COLOR_3DLIGHT,     COLOR_BTNSHADOW, -1,
Alexandre Julliard's avatar
Alexandre Julliard committed
50 51 52 53
    COLOR_3DDKSHADOW,   COLOR_3DLIGHT,     COLOR_BTNSHADOW, -1,
    -1,                 COLOR_3DLIGHT,     COLOR_BTNSHADOW, -1
};

54
static const signed char RBInnerNormal[] = {
Alexandre Julliard's avatar
Alexandre Julliard committed
55 56 57 58 59 60
    -1,           -1,                -1,              -1,
    -1,           COLOR_BTNSHADOW,   COLOR_BTNSHADOW, -1,
    -1,           COLOR_3DLIGHT,     COLOR_3DLIGHT,   -1,
    -1,           -1,                -1,              -1
};

61
static const signed char RBOuterNormal[] = {
Alexandre Julliard's avatar
Alexandre Julliard committed
62 63 64 65 66 67
    -1,              COLOR_3DDKSHADOW,  COLOR_BTNHIGHLIGHT, -1,
    COLOR_BTNSHADOW, COLOR_3DDKSHADOW,  COLOR_BTNHIGHLIGHT, -1,
    COLOR_3DLIGHT,   COLOR_3DDKSHADOW,  COLOR_BTNHIGHLIGHT, -1,
    -1,              COLOR_3DDKSHADOW,  COLOR_BTNHIGHLIGHT, -1
};

68
static const signed char LTInnerSoft[] = {
Alexandre Julliard's avatar
Alexandre Julliard committed
69 70 71 72 73 74
    -1,                  -1,                -1,              -1,
    -1,                  COLOR_3DLIGHT,     COLOR_3DLIGHT,   -1,
    -1,                  COLOR_BTNSHADOW,   COLOR_BTNSHADOW, -1,
    -1,                  -1,                -1,              -1
};

75
static const signed char LTOuterSoft[] = {
Alexandre Julliard's avatar
Alexandre Julliard committed
76 77 78 79 80 81 82 83 84
    -1,              COLOR_BTNHIGHLIGHT, COLOR_3DDKSHADOW, -1,
    COLOR_3DLIGHT,   COLOR_BTNHIGHLIGHT, COLOR_3DDKSHADOW, -1,
    COLOR_BTNSHADOW, COLOR_BTNHIGHLIGHT, COLOR_3DDKSHADOW, -1,
    -1,              COLOR_BTNHIGHLIGHT, COLOR_3DDKSHADOW, -1
};

#define RBInnerSoft RBInnerNormal   /* These are the same */
#define RBOuterSoft RBOuterNormal

85
static const signed char LTRBOuterMono[] = {
Alexandre Julliard's avatar
Alexandre Julliard committed
86 87 88 89 90 91
    -1,           COLOR_WINDOWFRAME, COLOR_WINDOWFRAME, COLOR_WINDOWFRAME,
    COLOR_WINDOW, COLOR_WINDOWFRAME, COLOR_WINDOWFRAME, COLOR_WINDOWFRAME,
    COLOR_WINDOW, COLOR_WINDOWFRAME, COLOR_WINDOWFRAME, COLOR_WINDOWFRAME,
    COLOR_WINDOW, COLOR_WINDOWFRAME, COLOR_WINDOWFRAME, COLOR_WINDOWFRAME,
};

92
static const signed char LTRBInnerMono[] = {
Alexandre Julliard's avatar
Alexandre Julliard committed
93 94 95 96 97 98
    -1, -1,           -1,           -1,
    -1, COLOR_WINDOW, COLOR_WINDOW, COLOR_WINDOW,
    -1, COLOR_WINDOW, COLOR_WINDOW, COLOR_WINDOW,
    -1, COLOR_WINDOW, COLOR_WINDOW, COLOR_WINDOW,
};

99
static const signed char LTRBOuterFlat[] = {
Alexandre Julliard's avatar
Alexandre Julliard committed
100
    -1,                COLOR_BTNSHADOW, COLOR_BTNSHADOW, COLOR_BTNSHADOW,
101 102 103
    COLOR_BTNFACE,     COLOR_BTNSHADOW, COLOR_BTNSHADOW, COLOR_BTNSHADOW,
    COLOR_BTNFACE,     COLOR_BTNSHADOW, COLOR_BTNSHADOW, COLOR_BTNSHADOW,
    COLOR_BTNFACE,     COLOR_BTNSHADOW, COLOR_BTNSHADOW, COLOR_BTNSHADOW,
Alexandre Julliard's avatar
Alexandre Julliard committed
104 105
};

106
static const signed char LTRBInnerFlat[] = {
Alexandre Julliard's avatar
Alexandre Julliard committed
107
    -1, -1,              -1,              -1,
108 109 110
    -1, COLOR_BTNFACE,     COLOR_BTNFACE,     COLOR_BTNFACE,
    -1, COLOR_BTNFACE,     COLOR_BTNFACE,     COLOR_BTNFACE,
    -1, COLOR_BTNFACE,     COLOR_BTNFACE,     COLOR_BTNFACE,
Alexandre Julliard's avatar
Alexandre Julliard committed
111 112
};

113
/* last COLOR id */
114
#define COLOR_MAX   COLOR_MENUBAR
115 116


117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
/***********************************************************************
 *           set_control_clipping
 *
 * Set clipping for a builtin control that uses CS_PARENTDC.
 * Return the previous clip region if any.
 */
HRGN set_control_clipping( HDC hdc, const RECT *rect )
{
    RECT rc = *rect;
    HRGN hrgn = CreateRectRgn( 0, 0, 0, 0 );

    if (GetClipRgn( hdc, hrgn ) != 1)
    {
        DeleteObject( hrgn );
        hrgn = 0;
    }
    DPtoLP( hdc, (POINT *)&rc, 2 );
    if (GetLayout( hdc ) & LAYOUT_RTL)  /* compensate for the shifting done by IntersectClipRect */
    {
        rc.left++;
        rc.right++;
    }
    IntersectClipRect( hdc, rc.left, rc.top, rc.right, rc.bottom );
    return hrgn;
}

Alexandre Julliard's avatar
Alexandre Julliard committed
143 144 145
/***********************************************************************
 *           UITOOLS_DrawDiagEdge
 *
Alexandre Julliard's avatar
Alexandre Julliard committed
146 147
 * Same as DrawEdge invoked with BF_DIAGONAL
 *
Alexandre Julliard's avatar
Alexandre Julliard committed
148 149 150 151
 * 03-Dec-1997: Changed by Bertho Stultiens
 *
 * See also comments with UITOOLS_DrawRectEdge()
 */
152
static BOOL UITOOLS95_DrawDiagEdge(HDC hdc, LPRECT rc,
153
				     UINT uType, UINT uFlags)
Alexandre Julliard's avatar
Alexandre Julliard committed
154
{
155
    POINT Points[4];
156
    signed char InnerI, OuterI;
157 158 159
    HPEN InnerPen, OuterPen;
    POINT SavePoint;
    HPEN SavePen;
Alexandre Julliard's avatar
Alexandre Julliard committed
160 161 162 163 164
    int spx, spy;
    int epx, epy;
    int Width = rc->right - rc->left;
    int Height= rc->bottom - rc->top;
    int SmallDiam = Width > Height ? Height : Width;
165
    BOOL retval = !(   ((uType & BDR_INNER) == BDR_INNER
Alexandre Julliard's avatar
Alexandre Julliard committed
166 167 168 169 170 171
                       || (uType & BDR_OUTER) == BDR_OUTER)
                      && !(uFlags & (BF_FLAT|BF_MONO)) );
    int add = (LTRBInnerMono[uType & (BDR_INNER|BDR_OUTER)] != -1 ? 1 : 0)
            + (LTRBOuterMono[uType & (BDR_INNER|BDR_OUTER)] != -1 ? 1 : 0);

    /* Init some vars */
172 173
    OuterPen = InnerPen = GetStockObject(NULL_PEN);
    SavePen = SelectObject(hdc, InnerPen);
Alexandre Julliard's avatar
Alexandre Julliard committed
174
    spx = spy = epx = epy = 0; /* Satisfy the compiler... */
175

Alexandre Julliard's avatar
Alexandre Julliard committed
176 177 178 179 180 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
    /* Determine the colors of the edges */
    if(uFlags & BF_MONO)
    {
        InnerI = LTRBInnerMono[uType & (BDR_INNER|BDR_OUTER)];
        OuterI = LTRBOuterMono[uType & (BDR_INNER|BDR_OUTER)];
    }
    else if(uFlags & BF_FLAT)
    {
        InnerI = LTRBInnerFlat[uType & (BDR_INNER|BDR_OUTER)];
        OuterI = LTRBOuterFlat[uType & (BDR_INNER|BDR_OUTER)];
    }
    else if(uFlags & BF_SOFT)
    {
        if(uFlags & BF_BOTTOM)
        {
            InnerI = RBInnerSoft[uType & (BDR_INNER|BDR_OUTER)];
            OuterI = RBOuterSoft[uType & (BDR_INNER|BDR_OUTER)];
        }
        else
        {
            InnerI = LTInnerSoft[uType & (BDR_INNER|BDR_OUTER)];
            OuterI = LTOuterSoft[uType & (BDR_INNER|BDR_OUTER)];
        }
    }
    else
    {
        if(uFlags & BF_BOTTOM)
        {
            InnerI = RBInnerNormal[uType & (BDR_INNER|BDR_OUTER)];
            OuterI = RBOuterNormal[uType & (BDR_INNER|BDR_OUTER)];
        }
        else
        {
            InnerI = LTInnerNormal[uType & (BDR_INNER|BDR_OUTER)];
            OuterI = LTOuterNormal[uType & (BDR_INNER|BDR_OUTER)];
        }
    }

214 215
    if(InnerI != -1) InnerPen = SYSCOLOR_GetPen(InnerI);
    if(OuterI != -1) OuterPen = SYSCOLOR_GetPen(OuterI);
Alexandre Julliard's avatar
Alexandre Julliard committed
216

217
    MoveToEx(hdc, 0, 0, &SavePoint);
Alexandre Julliard's avatar
Alexandre Julliard committed
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264

    /* Don't ask me why, but this is what is visible... */
    /* This must be possible to do much simpler, but I fail to */
    /* see the logic in the MS implementation (sigh...). */
    /* So, this might look a bit brute force here (and it is), but */
    /* it gets the job done;) */

    switch(uFlags & BF_RECT)
    {
    case 0:
    case BF_LEFT:
    case BF_BOTTOM:
    case BF_BOTTOMLEFT:
        /* Left bottom endpoint */
        epx = rc->left-1;
        spx = epx + SmallDiam;
        epy = rc->bottom;
        spy = epy - SmallDiam;
        break;

    case BF_TOPLEFT:
    case BF_BOTTOMRIGHT:
        /* Left top endpoint */
        epx = rc->left-1;
        spx = epx + SmallDiam;
        epy = rc->top-1;
        spy = epy + SmallDiam;
        break;

    case BF_TOP:
    case BF_RIGHT:
    case BF_TOPRIGHT:
    case BF_RIGHT|BF_LEFT:
    case BF_RIGHT|BF_LEFT|BF_TOP:
    case BF_BOTTOM|BF_TOP:
    case BF_BOTTOM|BF_TOP|BF_LEFT:
    case BF_BOTTOMRIGHT|BF_LEFT:
    case BF_BOTTOMRIGHT|BF_TOP:
    case BF_RECT:
        /* Right top endpoint */
        spx = rc->left;
        epx = spx + SmallDiam;
        spy = rc->bottom-1;
        epy = spy - SmallDiam;
        break;
    }

265 266 267
    MoveToEx(hdc, spx, spy, NULL);
    SelectObject(hdc, OuterPen);
    LineTo(hdc, epx, epy);
Alexandre Julliard's avatar
Alexandre Julliard committed
268

269
    SelectObject(hdc, InnerPen);
Alexandre Julliard's avatar
Alexandre Julliard committed
270 271 272 273 274 275 276

    switch(uFlags & (BF_RECT|BF_DIAGONAL))
    {
    case BF_DIAGONAL_ENDBOTTOMLEFT:
    case (BF_DIAGONAL|BF_BOTTOM):
    case BF_DIAGONAL:
    case (BF_DIAGONAL|BF_LEFT):
277 278
        MoveToEx(hdc, spx-1, spy, NULL);
        LineTo(hdc, epx, epy-1);
Alexandre Julliard's avatar
Alexandre Julliard committed
279 280 281 282 283 284 285 286 287 288
        Points[0].x = spx-add;
        Points[0].y = spy;
        Points[1].x = rc->left;
        Points[1].y = rc->top;
        Points[2].x = epx+1;
        Points[2].y = epy-1-add;
        Points[3] = Points[2];
        break;

    case BF_DIAGONAL_ENDBOTTOMRIGHT:
289 290
        MoveToEx(hdc, spx-1, spy, NULL);
        LineTo(hdc, epx, epy+1);
Alexandre Julliard's avatar
Alexandre Julliard committed
291 292 293 294 295 296 297 298 299 300 301 302 303
        Points[0].x = spx-add;
        Points[0].y = spy;
        Points[1].x = rc->left;
        Points[1].y = rc->bottom-1;
        Points[2].x = epx+1;
        Points[2].y = epy+1+add;
        Points[3] = Points[2];
        break;

    case (BF_DIAGONAL|BF_BOTTOM|BF_RIGHT|BF_TOP):
    case (BF_DIAGONAL|BF_BOTTOM|BF_RIGHT|BF_TOP|BF_LEFT):
    case BF_DIAGONAL_ENDTOPRIGHT:
    case (BF_DIAGONAL|BF_RIGHT|BF_TOP|BF_LEFT):
304 305
        MoveToEx(hdc, spx+1, spy, NULL);
        LineTo(hdc, epx, epy+1);
Alexandre Julliard's avatar
Alexandre Julliard committed
306 307 308 309 310 311 312 313 314 315 316
        Points[0].x = epx-1;
        Points[0].y = epy+1+add;
        Points[1].x = rc->right-1;
        Points[1].y = rc->top+add;
        Points[2].x = rc->right-1;
        Points[2].y = rc->bottom-1;
        Points[3].x = spx+add;
        Points[3].y = spy;
        break;

    case BF_DIAGONAL_ENDTOPLEFT:
317 318
        MoveToEx(hdc, spx, spy-1, NULL);
        LineTo(hdc, epx+1, epy);
Alexandre Julliard's avatar
Alexandre Julliard committed
319 320 321 322 323 324 325 326 327 328 329 330 331
        Points[0].x = epx+1+add;
        Points[0].y = epy+1;
        Points[1].x = rc->right-1;
        Points[1].y = rc->top;
        Points[2].x = rc->right-1;
        Points[2].y = rc->bottom-1-add;
        Points[3].x = spx;
        Points[3].y = spy-add;
        break;

    case (BF_DIAGONAL|BF_TOP):
    case (BF_DIAGONAL|BF_BOTTOM|BF_TOP):
    case (BF_DIAGONAL|BF_BOTTOM|BF_TOP|BF_LEFT):
332 333
        MoveToEx(hdc, spx+1, spy-1, NULL);
        LineTo(hdc, epx, epy);
Alexandre Julliard's avatar
Alexandre Julliard committed
334 335 336 337 338 339 340 341 342 343 344 345 346
        Points[0].x = epx-1;
        Points[0].y = epy+1;
        Points[1].x = rc->right-1;
        Points[1].y = rc->top;
        Points[2].x = rc->right-1;
        Points[2].y = rc->bottom-1-add;
        Points[3].x = spx+add;
        Points[3].y = spy-add;
        break;

    case (BF_DIAGONAL|BF_RIGHT):
    case (BF_DIAGONAL|BF_RIGHT|BF_LEFT):
    case (BF_DIAGONAL|BF_RIGHT|BF_LEFT|BF_BOTTOM):
347 348
        MoveToEx(hdc, spx, spy, NULL);
        LineTo(hdc, epx-1, epy+1);
Alexandre Julliard's avatar
Alexandre Julliard committed
349 350 351 352 353 354 355 356 357 358 359 360 361
        Points[0].x = spx;
        Points[0].y = spy;
        Points[1].x = rc->left;
        Points[1].y = rc->top+add;
        Points[2].x = epx-1-add;
        Points[2].y = epy+1+add;
        Points[3] = Points[2];
        break;
    }

    /* Fill the interior if asked */
    if((uFlags & BF_MIDDLE) && retval)
    {
362 363
        HBRUSH hbsave;
        HBRUSH hb = GetSysColorBrush(uFlags & BF_MONO ? COLOR_WINDOW
Alexandre Julliard's avatar
Alexandre Julliard committed
364
					 :COLOR_BTNFACE);
365
        HPEN hpsave;
366
        HPEN hp = SYSCOLOR_GetPen(uFlags & BF_MONO ? COLOR_WINDOW
Alexandre Julliard's avatar
Alexandre Julliard committed
367
				     : COLOR_BTNFACE);
368 369
        hbsave = SelectObject(hdc, hb);
        hpsave = SelectObject(hdc, hp);
370 371 372
        Polygon(hdc, Points, 4);
        SelectObject(hdc, hbsave);
        SelectObject(hdc, hpsave);
Alexandre Julliard's avatar
Alexandre Julliard committed
373 374 375 376 377 378 379 380 381 382 383 384
    }

    /* Adjust rectangle if asked */
    if(uFlags & BF_ADJUST)
    {
        if(uFlags & BF_LEFT)   rc->left   += add;
        if(uFlags & BF_RIGHT)  rc->right  -= add;
        if(uFlags & BF_TOP)    rc->top    += add;
        if(uFlags & BF_BOTTOM) rc->bottom -= add;
    }

    /* Cleanup */
385 386
    SelectObject(hdc, SavePen);
    MoveToEx(hdc, SavePoint.x, SavePoint.y, NULL);
Alexandre Julliard's avatar
Alexandre Julliard committed
387 388 389

    return retval;
}
Alexandre Julliard's avatar
Alexandre Julliard committed
390 391 392 393

/***********************************************************************
 *           UITOOLS_DrawRectEdge
 *
Alexandre Julliard's avatar
Alexandre Julliard committed
394
 * Same as DrawEdge invoked without BF_DIAGONAL
395
 * The width parameter sets the width of each outer and inner edge.
Alexandre Julliard's avatar
Alexandre Julliard committed
396
 *
Alexandre Julliard's avatar
Alexandre Julliard committed
397 398 399 400 401
 * 23-Nov-1997: Changed by Bertho Stultiens
 *
 * Well, I started testing this and found out that there are a few things
 * that weren't quite as win95. The following rewrite should reproduce
 * win95 results completely.
Austin English's avatar
Austin English committed
402
 * The colorselection is table-driven to avoid awful if-statements.
Alexandre Julliard's avatar
Alexandre Julliard committed
403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465
 * The table below show the color settings.
 *
 * Pen selection table for uFlags = 0
 *
 * uType |  LTI  |  LTO  |  RBI  |  RBO
 * ------+-------+-------+-------+-------
 *  0000 |   x   |   x   |   x   |   x
 *  0001 |   x   |  22   |   x   |  21
 *  0010 |   x   |  16   |   x   |  20
 *  0011 |   x   |   x   |   x   |   x
 * ------+-------+-------+-------+-------
 *  0100 |   x   |  20   |   x   |  16
 *  0101 |  20   |  22   |  16   |  21
 *  0110 |  20   |  16   |  16   |  20
 *  0111 |   x   |   x   |   x   |   x
 * ------+-------+-------+-------+-------
 *  1000 |   x   |  21   |   x   |  22
 *  1001 |  21   |  22   |  22   |  21
 *  1010 |  21   |  16   |  22   |  20
 *  1011 |   x   |   x   |   x   |   x
 * ------+-------+-------+-------+-------
 *  1100 |   x   |   x   |   x   |   x
 *  1101 |   x   | x (22)|   x   | x (21)
 *  1110 |   x   | x (16)|   x   | x (20)
 *  1111 |   x   |   x   |   x   |   x
 *
 * Pen selection table for uFlags = BF_SOFT
 *
 * uType |  LTI  |  LTO  |  RBI  |  RBO
 * ------+-------+-------+-------+-------
 *  0000 |   x   |   x   |   x   |   x
 *  0001 |   x   |  20   |   x   |  21
 *  0010 |   x   |  21   |   x   |  20
 *  0011 |   x   |   x   |   x   |   x
 * ------+-------+-------+-------+-------
 *  0100 |   x   |  22   |   x   |  16
 *  0101 |  22   |  20   |  16   |  21
 *  0110 |  22   |  21   |  16   |  20
 *  0111 |   x   |   x   |   x   |   x
 * ------+-------+-------+-------+-------
 *  1000 |   x   |  16   |   x   |  22
 *  1001 |  16   |  20   |  22   |  21
 *  1010 |  16   |  21   |  22   |  20
 *  1011 |   x   |   x   |   x   |   x
 * ------+-------+-------+-------+-------
 *  1100 |   x   |   x   |   x   |   x
 *  1101 |   x   | x (20)|   x   | x (21)
 *  1110 |   x   | x (21)|   x   | x (20)
 *  1111 |   x   |   x   |   x   |   x
 *
 * x = don't care; (n) = is what win95 actually uses
 * LTI = left Top Inner line
 * LTO = left Top Outer line
 * RBI = Right Bottom Inner line
 * RBO = Right Bottom Outer line
 * 15 = COLOR_BTNFACE
 * 16 = COLOR_BTNSHADOW
 * 20 = COLOR_BTNHIGHLIGHT
 * 21 = COLOR_3DDKSHADOW
 * 22 = COLOR_3DLIGHT
 */


466
static BOOL UITOOLS95_DrawRectEdge(HDC hdc, LPRECT rc,
467
                                   UINT uType, UINT uFlags, UINT width)
Alexandre Julliard's avatar
Alexandre Julliard committed
468
{
469 470
    signed char LTInnerI, LTOuterI;
    signed char RBInnerI, RBOuterI;
471 472 473
    HBRUSH lti_brush, lto_brush, rbi_brush, rbo_brush;
    RECT InnerRect = *rc, fill_rect;
    int lbi_offset = 0, lti_offset = 0, rti_offset = 0, rbi_offset = 0;
474
    BOOL retval = !(   ((uType & BDR_INNER) == BDR_INNER
Alexandre Julliard's avatar
Alexandre Julliard committed
475 476
                       || (uType & BDR_OUTER) == BDR_OUTER)
                      && !(uFlags & (BF_FLAT|BF_MONO)) );
477

478
    lti_brush = lto_brush = rbi_brush = rbo_brush = GetStockObject(NULL_BRUSH);
Alexandre Julliard's avatar
Alexandre Julliard committed
479 480 481 482 483 484 485 486 487 488 489

    /* Determine the colors of the edges */
    if(uFlags & BF_MONO)
    {
        LTInnerI = RBInnerI = LTRBInnerMono[uType & (BDR_INNER|BDR_OUTER)];
        LTOuterI = RBOuterI = LTRBOuterMono[uType & (BDR_INNER|BDR_OUTER)];
    }
    else if(uFlags & BF_FLAT)
    {
        LTInnerI = RBInnerI = LTRBInnerFlat[uType & (BDR_INNER|BDR_OUTER)];
        LTOuterI = RBOuterI = LTRBOuterFlat[uType & (BDR_INNER|BDR_OUTER)];
490

491
	if( LTInnerI != -1 ) LTInnerI = RBInnerI = COLOR_BTNFACE;
Alexandre Julliard's avatar
Alexandre Julliard committed
492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507
    }
    else if(uFlags & BF_SOFT)
    {
        LTInnerI = LTInnerSoft[uType & (BDR_INNER|BDR_OUTER)];
        LTOuterI = LTOuterSoft[uType & (BDR_INNER|BDR_OUTER)];
        RBInnerI = RBInnerSoft[uType & (BDR_INNER|BDR_OUTER)];
        RBOuterI = RBOuterSoft[uType & (BDR_INNER|BDR_OUTER)];
    }
    else
    {
        LTInnerI = LTInnerNormal[uType & (BDR_INNER|BDR_OUTER)];
        LTOuterI = LTOuterNormal[uType & (BDR_INNER|BDR_OUTER)];
        RBInnerI = RBInnerNormal[uType & (BDR_INNER|BDR_OUTER)];
        RBOuterI = RBOuterNormal[uType & (BDR_INNER|BDR_OUTER)];
    }

508 509 510 511
    if((uFlags & BF_BOTTOMLEFT) == BF_BOTTOMLEFT)   lbi_offset = width;
    if((uFlags & BF_TOPRIGHT) == BF_TOPRIGHT)       rti_offset = width;
    if((uFlags & BF_BOTTOMRIGHT) == BF_BOTTOMRIGHT) rbi_offset = width;
    if((uFlags & BF_TOPLEFT) == BF_TOPLEFT)         lti_offset = width;
Alexandre Julliard's avatar
Alexandre Julliard committed
512

513 514 515 516
    if(LTInnerI != -1) lti_brush = GetSysColorBrush(LTInnerI);
    if(LTOuterI != -1) lto_brush = GetSysColorBrush(LTOuterI);
    if(RBInnerI != -1) rbi_brush = GetSysColorBrush(RBInnerI);
    if(RBOuterI != -1) rbo_brush = GetSysColorBrush(RBOuterI);
Alexandre Julliard's avatar
Alexandre Julliard committed
517 518 519 520

    /* Draw the outer edge */
    if(uFlags & BF_TOP)
    {
521 522 523
        fill_rect = InnerRect;
        fill_rect.bottom = fill_rect.top + width;
        FillRect( hdc, &fill_rect, lto_brush );
Alexandre Julliard's avatar
Alexandre Julliard committed
524 525 526
    }
    if(uFlags & BF_LEFT)
    {
527 528 529
        fill_rect = InnerRect;
        fill_rect.right = fill_rect.left + width;
        FillRect( hdc, &fill_rect, lto_brush );
Alexandre Julliard's avatar
Alexandre Julliard committed
530 531 532
    }
    if(uFlags & BF_BOTTOM)
    {
533 534 535
        fill_rect = InnerRect;
        fill_rect.top = fill_rect.bottom - width;
        FillRect( hdc, &fill_rect, rbo_brush );
Alexandre Julliard's avatar
Alexandre Julliard committed
536 537 538
    }
    if(uFlags & BF_RIGHT)
    {
539 540 541
        fill_rect = InnerRect;
        fill_rect.left = fill_rect.right - width;
        FillRect( hdc, &fill_rect, rbo_brush );
Alexandre Julliard's avatar
Alexandre Julliard committed
542 543 544 545 546
    }

    /* Draw the inner edge */
    if(uFlags & BF_TOP)
    {
547 548 549
        SetRect( &fill_rect, InnerRect.left + lti_offset, InnerRect.top + width,
                 InnerRect.right - rti_offset, InnerRect.top + 2 * width );
        FillRect( hdc, &fill_rect, lti_brush );
Alexandre Julliard's avatar
Alexandre Julliard committed
550 551 552
    }
    if(uFlags & BF_LEFT)
    {
553 554 555
        SetRect( &fill_rect, InnerRect.left + width, InnerRect.top + lti_offset,
                 InnerRect.left + 2 * width, InnerRect.bottom - lbi_offset );
        FillRect( hdc, &fill_rect, lti_brush );
Alexandre Julliard's avatar
Alexandre Julliard committed
556 557 558
    }
    if(uFlags & BF_BOTTOM)
    {
559 560 561
        SetRect( &fill_rect, InnerRect.left + lbi_offset, InnerRect.bottom - 2 * width,
                 InnerRect.right - rbi_offset, InnerRect.bottom - width );
        FillRect( hdc, &fill_rect, rbi_brush );
Alexandre Julliard's avatar
Alexandre Julliard committed
562 563 564
    }
    if(uFlags & BF_RIGHT)
    {
565 566 567
        SetRect( &fill_rect, InnerRect.right - 2 * width, InnerRect.top + rti_offset,
                 InnerRect.right - width, InnerRect.bottom - rbi_offset );
        FillRect( hdc, &fill_rect, rbi_brush );
Alexandre Julliard's avatar
Alexandre Julliard committed
568 569
    }

570
    if( ((uFlags & BF_MIDDLE) && retval) || (uFlags & BF_ADJUST) )
Alexandre Julliard's avatar
Alexandre Julliard committed
571
    {
572 573
        int add = (LTRBInnerMono[uType & (BDR_INNER|BDR_OUTER)] != -1 ? width : 0)
                + (LTRBOuterMono[uType & (BDR_INNER|BDR_OUTER)] != -1 ? width : 0);
574 575 576 577 578 579 580

        if(uFlags & BF_LEFT)   InnerRect.left   += add;
        if(uFlags & BF_RIGHT)  InnerRect.right  -= add;
        if(uFlags & BF_TOP)    InnerRect.top    += add;
        if(uFlags & BF_BOTTOM) InnerRect.bottom -= add;

        if((uFlags & BF_MIDDLE) && retval)
581 582
	{
            FillRect(hdc, &InnerRect, GetSysColorBrush(uFlags & BF_MONO ?
583 584 585 586 587
						       COLOR_WINDOW : COLOR_BTNFACE));
	}

	if(uFlags & BF_ADJUST)
	    *rc = InnerRect;
Alexandre Julliard's avatar
Alexandre Julliard committed
588 589 590
    }

    return retval;
Alexandre Julliard's avatar
Alexandre Julliard committed
591 592 593 594
}


/**********************************************************************
595
 *          DrawEdge   (USER32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
596
 */
597
BOOL WINAPI DrawEdge( HDC hdc, LPRECT rc, UINT edge, UINT flags )
Alexandre Julliard's avatar
Alexandre Julliard committed
598
{
599
    TRACE("%p %s %04x %04x\n", hdc, wine_dbgstr_rect(rc), edge, flags );
Alexandre Julliard's avatar
Alexandre Julliard committed
600 601

    if(flags & BF_DIAGONAL)
Alexandre Julliard's avatar
Alexandre Julliard committed
602
      return UITOOLS95_DrawDiagEdge(hdc, rc, edge, flags);
Alexandre Julliard's avatar
Alexandre Julliard committed
603
    else
604
      return UITOOLS95_DrawRectEdge(hdc, rc, edge, flags, 1);
Alexandre Julliard's avatar
Alexandre Julliard committed
605 606 607
}


Alexandre Julliard's avatar
Alexandre Julliard committed
608 609 610 611 612
/************************************************************************
 *	UITOOLS_MakeSquareRect
 *
 * Utility to create a square rectangle and returning the width
 */
613
static int UITOOLS_MakeSquareRect(LPRECT src, LPRECT dst)
Alexandre Julliard's avatar
Alexandre Julliard committed
614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635
{
    int Width  = src->right - src->left;
    int Height = src->bottom - src->top;
    int SmallDiam = Width > Height ? Height : Width;

    *dst = *src;

    /* Make it a square box */
    if(Width < Height)      /* SmallDiam == Width */
    {
        dst->top += (Height-Width)/2;
        dst->bottom = dst->top + SmallDiam;
    }
    else if(Width > Height) /* SmallDiam == Height */
    {
        dst->left += (Width-Height)/2;
        dst->right = dst->left + SmallDiam;
    }

   return SmallDiam;
}

636 637 638 639 640 641 642 643 644
static void UITOOLS_DrawCheckedRect( HDC dc, LPRECT rect )
{
    if(GetSysColor(COLOR_BTNHIGHLIGHT) == RGB(255, 255, 255))
    {
      HBRUSH hbsave;
      COLORREF bg;

      FillRect(dc, rect, GetSysColorBrush(COLOR_BTNFACE));
      bg = SetBkColor(dc, RGB(255, 255, 255));
645
      hbsave = SelectObject(dc, SYSCOLOR_Get55AABrush());
646 647 648 649 650 651 652 653 654
      PatBlt(dc, rect->left, rect->top, rect->right-rect->left, rect->bottom-rect->top, 0x00FA0089);
      SelectObject(dc, hbsave);
      SetBkColor(dc, bg);
    }
    else
    {
        FillRect(dc, rect, GetSysColorBrush(COLOR_BTNHIGHLIGHT));
    }
}
Alexandre Julliard's avatar
Alexandre Julliard committed
655 656 657 658 659 660 661 662 663 664

/************************************************************************
 *	UITOOLS_DFC_ButtonPush
 *
 * Draw a push button coming from DrawFrameControl()
 *
 * Does a pretty good job in emulating MS behavior. Some quirks are
 * however there because MS uses a TrueType font (Marlett) to draw
 * the buttons.
 */
665
static BOOL UITOOLS95_DFC_ButtonPush(HDC dc, LPRECT r, UINT uFlags)
Alexandre Julliard's avatar
Alexandre Julliard committed
666
{
667 668
    UINT edge;
    RECT myr = *r;
Alexandre Julliard's avatar
Alexandre Julliard committed
669 670 671 672 673 674 675 676 677

    if(uFlags & (DFCS_PUSHED | DFCS_CHECKED | DFCS_FLAT))
        edge = EDGE_SUNKEN;
    else
        edge = EDGE_RAISED;

    if(uFlags & DFCS_CHECKED)
    {
        if(uFlags & DFCS_MONO)
678
            UITOOLS95_DrawRectEdge(dc, &myr, edge, BF_MONO|BF_RECT|BF_ADJUST, 1);
Alexandre Julliard's avatar
Alexandre Julliard committed
679
        else
680
            UITOOLS95_DrawRectEdge(dc, &myr, edge, (uFlags&DFCS_FLAT)|BF_RECT|BF_SOFT|BF_ADJUST, 1);
Alexandre Julliard's avatar
Alexandre Julliard committed
681

682 683 684 685 686
	if (!(uFlags & DFCS_TRANSPARENT))
	    UITOOLS_DrawCheckedRect( dc, &myr );
    }
    else
    {
Alexandre Julliard's avatar
Alexandre Julliard committed
687 688
        if(uFlags & DFCS_MONO)
        {
689
            UITOOLS95_DrawRectEdge(dc, &myr, edge, BF_MONO|BF_RECT|BF_ADJUST, 1);
690 691
	    if (!(uFlags & DFCS_TRANSPARENT))
                FillRect(dc, &myr, GetSysColorBrush(COLOR_BTNFACE));
Alexandre Julliard's avatar
Alexandre Julliard committed
692 693 694
        }
        else
        {
695 696
            UITOOLS95_DrawRectEdge(dc, r, edge, (uFlags & DFCS_FLAT) |
                                   ((uFlags & DFCS_TRANSPARENT) ? 0 : BF_MIDDLE) | BF_RECT | BF_SOFT, 1);
Alexandre Julliard's avatar
Alexandre Julliard committed
697 698 699 700 701
        }
    }

    /* Adjust rectangle if asked */
    if(uFlags & DFCS_ADJUSTRECT)
702
        InflateRect(r, -2, -2);
Alexandre Julliard's avatar
Alexandre Julliard committed
703 704 705 706 707 708

    return TRUE;
}


/************************************************************************
709
 *	UITOOLS_DFC_ButtonCheck
Alexandre Julliard's avatar
Alexandre Julliard committed
710 711 712 713 714 715 716 717
 *
 * Draw a check/3state button coming from DrawFrameControl()
 *
 * Does a pretty good job in emulating MS behavior. Some quirks are
 * however there because MS uses a TrueType font (Marlett) to draw
 * the buttons.
 */

718
static BOOL UITOOLS95_DFC_ButtonCheck(HDC dc, LPRECT r, UINT uFlags)
Alexandre Julliard's avatar
Alexandre Julliard committed
719
{
720
    RECT myr;
721
    UINT flags = BF_RECT | BF_ADJUST;
722

723
    UITOOLS_MakeSquareRect(r, &myr);
Alexandre Julliard's avatar
Alexandre Julliard committed
724

725 726
    if(uFlags & DFCS_FLAT) flags |= BF_FLAT;
    else if(uFlags & DFCS_MONO) flags |= BF_MONO;
Alexandre Julliard's avatar
Alexandre Julliard committed
727

728
    UITOOLS95_DrawRectEdge( dc, &myr, EDGE_SUNKEN, flags, max( (myr.right - myr.left + 8) / 16, 1) );
Alexandre Julliard's avatar
Alexandre Julliard committed
729

730
    if (!(uFlags & DFCS_TRANSPARENT))
Alexandre Julliard's avatar
Alexandre Julliard committed
731
    {
732 733 734 735 736 737
	if(uFlags & (DFCS_INACTIVE | DFCS_PUSHED))
            FillRect(dc, &myr, GetSysColorBrush(COLOR_BTNFACE));
	else if( (uFlags & DFCS_BUTTON3STATE) && (uFlags & DFCS_CHECKED) )
            UITOOLS_DrawCheckedRect( dc, &myr );
	else
            FillRect(dc, &myr, GetSysColorBrush(COLOR_WINDOW));
Alexandre Julliard's avatar
Alexandre Julliard committed
738 739 740 741
    }

    if(uFlags & DFCS_CHECKED)
    {
742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765
        POINT pt[6];
        HPEN old_pen;
        HBRUSH old_brush;
        int color = (uFlags & DFCS_INACTIVE) || (uFlags & 0xff) == DFCS_BUTTON3STATE ?
            COLOR_BTNSHADOW : COLOR_WINDOWTEXT;

        pt[0].x = myr.right - 1;
        pt[0].y = myr.top;
        pt[1].x = pt[0].x;
        pt[1].y = pt[0].y + (myr.bottom - myr.top) / 3;
        pt[2].x = myr.left + (myr.right - myr.left) / 3;
        pt[2].y = myr.bottom - 1;
        pt[3].x = myr.left + 1;
        pt[3].y = pt[2].y - (pt[2].x - pt[3].x);
        pt[4].x = pt[3].x;
        pt[4].y = pt[3].y - (myr.bottom - myr.top) / 3;
        pt[5].x = pt[2].x;
        pt[5].y = pt[2].y - (myr.bottom - myr.top) / 3;

        old_brush = SelectObject( dc, GetSysColorBrush( color ) );
        old_pen = SelectObject( dc, GetStockObject( NULL_PEN ));
        Polygon( dc, pt, 6 );
        SelectObject( dc, old_brush );
        SelectObject( dc, old_pen );
Alexandre Julliard's avatar
Alexandre Julliard committed
766 767 768 769 770 771 772 773 774 775 776 777 778 779
    }
    return TRUE;
}


/************************************************************************
 *	UITOOLS_DFC_ButtonRadio
 *
 * Draw a radio/radioimage/radiomask button coming from DrawFrameControl()
 *
 * Does a pretty good job in emulating MS behavior. Some quirks are
 * however there because MS uses a TrueType font (Marlett) to draw
 * the buttons.
 */
780
static BOOL UITOOLS95_DFC_ButtonRadio(HDC dc, LPRECT r, UINT uFlags)
Alexandre Julliard's avatar
Alexandre Julliard committed
781
{
782
    RECT myr;
Alexandre Julliard's avatar
Alexandre Julliard committed
783 784 785
    int i;
    int SmallDiam = UITOOLS_MakeSquareRect(r, &myr);
    int BorderShrink = SmallDiam / 16;
786 787
    HPEN hpsave;
    HBRUSH hbsave;
Alexandre Julliard's avatar
Alexandre Julliard committed
788 789 790 791 792
    int xc, yc;

    if(BorderShrink < 1) BorderShrink = 1;

    if((uFlags & 0xff) == DFCS_BUTTONRADIOIMAGE)
793
        FillRect(dc, r, GetStockObject(BLACK_BRUSH));
794 795
    else if((uFlags & 0xff) == DFCS_BUTTONRADIOMASK)
        FillRect(dc, r, GetStockObject(WHITE_BRUSH));
Alexandre Julliard's avatar
Alexandre Julliard committed
796 797 798 799 800 801

    xc = myr.left + SmallDiam - SmallDiam/2;
    yc = myr.top  + SmallDiam - SmallDiam/2;

    /* Define bounding box */
    i = 14*SmallDiam/16;
802
    SetRect(&myr, xc - i + i / 2, yc - i + i / 2, xc + i / 2, yc + i / 2);
Alexandre Julliard's avatar
Alexandre Julliard committed
803 804 805

    if((uFlags & 0xff) == DFCS_BUTTONRADIOMASK)
    {
806
        hbsave = SelectObject(dc, GetStockObject(BLACK_BRUSH));
807
        Ellipse(dc, myr.left, myr.top, myr.right, myr.bottom);
808
        SelectObject(dc, hbsave);
Alexandre Julliard's avatar
Alexandre Julliard committed
809 810 811 812 813
    }
    else
    {
        if(uFlags & (DFCS_FLAT|DFCS_MONO))
        {
814 815
            hpsave = SelectObject(dc, SYSCOLOR_GetPen(COLOR_WINDOWFRAME));
            hbsave = SelectObject(dc, GetSysColorBrush(COLOR_WINDOWFRAME));
816
            Ellipse(dc, myr.left, myr.top, myr.right, myr.bottom);
817 818
            SelectObject(dc, hbsave);
            SelectObject(dc, hpsave);
Alexandre Julliard's avatar
Alexandre Julliard committed
819 820 821
        }
        else
        {
822 823
            hpsave = SelectObject(dc, SYSCOLOR_GetPen(COLOR_BTNHIGHLIGHT));
            hbsave = SelectObject(dc, GetSysColorBrush(COLOR_BTNHIGHLIGHT));
824
            Pie(dc, myr.left, myr.top, myr.right+1, myr.bottom+1, myr.left-1, myr.bottom, myr.right+1, myr.top);
Alexandre Julliard's avatar
Alexandre Julliard committed
825

826
            SelectObject(dc, SYSCOLOR_GetPen(COLOR_BTNSHADOW));
827
            SelectObject(dc, GetSysColorBrush(COLOR_BTNSHADOW));
828
            Pie(dc, myr.left, myr.top, myr.right+1, myr.bottom+1, myr.right+1, myr.top, myr.left-1, myr.bottom);
Alexandre Julliard's avatar
Alexandre Julliard committed
829

830
            InflateRect(&myr, -BorderShrink, -BorderShrink);
831
            SelectObject(dc, SYSCOLOR_GetPen(COLOR_3DLIGHT));
832
            SelectObject(dc, GetSysColorBrush(COLOR_3DLIGHT));
833
            Pie(dc, myr.left, myr.top, myr.right+1, myr.bottom+1, myr.left-1, myr.bottom, myr.right+1, myr.top);
Alexandre Julliard's avatar
Alexandre Julliard committed
834

835
            SelectObject(dc, SYSCOLOR_GetPen(COLOR_3DDKSHADOW));
836
            SelectObject(dc, GetSysColorBrush(COLOR_3DDKSHADOW));
837
            Pie(dc, myr.left, myr.top, myr.right+1, myr.bottom+1, myr.right+1, myr.top, myr.left-1, myr.bottom);
838 839
            SelectObject(dc, hbsave);
            SelectObject(dc, hpsave);
Alexandre Julliard's avatar
Alexandre Julliard committed
840 841 842
        }

        i = 10*SmallDiam/16;
843
        SetRect(&myr, xc - i + i / 2, yc - i + i / 2, xc + i / 2, yc + i / 2);
Alexandre Julliard's avatar
Alexandre Julliard committed
844
        i= !(uFlags & (DFCS_INACTIVE|DFCS_PUSHED)) ? COLOR_WINDOW : COLOR_BTNFACE;
845 846
        hpsave = SelectObject(dc, SYSCOLOR_GetPen(i));
        hbsave = SelectObject(dc, GetSysColorBrush(i));
847
        Ellipse(dc, myr.left, myr.top, myr.right, myr.bottom);
848 849
        SelectObject(dc, hbsave);
        SelectObject(dc, hpsave);
Alexandre Julliard's avatar
Alexandre Julliard committed
850 851 852 853 854 855
    }

    if(uFlags & DFCS_CHECKED)
    {
        i = 6*SmallDiam/16;
        i = i < 1 ? 1 : i;
856
        SetRect(&myr, xc - i + i / 2, yc - i + i / 2, xc + i / 2, yc + i / 2);
Alexandre Julliard's avatar
Alexandre Julliard committed
857 858

        i = uFlags & DFCS_INACTIVE ? COLOR_BTNSHADOW : COLOR_WINDOWTEXT;
859 860
        hbsave = SelectObject(dc, GetSysColorBrush(i));
        hpsave = SelectObject(dc, SYSCOLOR_GetPen(i));
861
        Ellipse(dc, myr.left, myr.top, myr.right, myr.bottom);
862 863
        SelectObject(dc, hpsave);
        SelectObject(dc, hbsave);
Alexandre Julliard's avatar
Alexandre Julliard committed
864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880
    }

    /* FIXME: M$ has a polygon in the center at relative points: */
    /* 0.476, 0.476 (times SmallDiam, SmallDiam) */
    /* 0.476, 0.525 */
    /* 0.500, 0.500 */
    /* 0.500, 0.499 */
    /* when the button is unchecked. The reason for it is unknown. The */
    /* color is COLOR_BTNHIGHLIGHT, although the polygon gets painted at */
    /* least 3 times (it looks like a clip-region when you see it happen). */
    /* I do not really see a reason why this should be implemented. If you */
    /* have a good reason, let me know. Maybe this is a quirk in the Marlett */
    /* font. */

    return TRUE;
}

Alexandre Julliard's avatar
Alexandre Julliard committed
881 882 883
/***********************************************************************
 *           UITOOLS_DrawFrameButton
 */
884
static BOOL UITOOLS95_DrawFrameButton(HDC hdc, LPRECT rc, UINT uState)
Alexandre Julliard's avatar
Alexandre Julliard committed
885
{
Alexandre Julliard's avatar
Alexandre Julliard committed
886 887 888
    switch(uState & 0xff)
    {
    case DFCS_BUTTONPUSH:
Alexandre Julliard's avatar
Alexandre Julliard committed
889
        return UITOOLS95_DFC_ButtonPush(hdc, rc, uState);
Alexandre Julliard's avatar
Alexandre Julliard committed
890 891 892

    case DFCS_BUTTONCHECK:
    case DFCS_BUTTON3STATE:
Alexandre Julliard's avatar
Alexandre Julliard committed
893
        return UITOOLS95_DFC_ButtonCheck(hdc, rc, uState);
Alexandre Julliard's avatar
Alexandre Julliard committed
894 895 896 897

    case DFCS_BUTTONRADIOIMAGE:
    case DFCS_BUTTONRADIOMASK:
    case DFCS_BUTTONRADIO:
Alexandre Julliard's avatar
Alexandre Julliard committed
898
        return UITOOLS95_DFC_ButtonRadio(hdc, rc, uState);
Alexandre Julliard's avatar
Alexandre Julliard committed
899 900

    default:
901
        WARN("Invalid button state=0x%04x\n", uState);
Alexandre Julliard's avatar
Alexandre Julliard committed
902 903
    }

Alexandre Julliard's avatar
Alexandre Julliard committed
904 905 906 907 908
    return FALSE;
}

/***********************************************************************
 *           UITOOLS_DrawFrameCaption
Alexandre Julliard's avatar
Alexandre Julliard committed
909 910
 *
 * Draw caption buttons (win95), coming from DrawFrameControl()
Alexandre Julliard's avatar
Alexandre Julliard committed
911
 */
Alexandre Julliard's avatar
Alexandre Julliard committed
912

913
static BOOL UITOOLS95_DrawFrameCaption(HDC dc, LPRECT r, UINT uFlags)
Alexandre Julliard's avatar
Alexandre Julliard committed
914
{
915
    RECT myr;
Alexandre Julliard's avatar
Alexandre Julliard committed
916
    int SmallDiam = UITOOLS_MakeSquareRect(r, &myr)-2;
917
    HFONT hfsave, hf;
918
    int colorIdx = uFlags & DFCS_INACTIVE ? COLOR_BTNSHADOW : COLOR_BTNTEXT;
Alexandre Julliard's avatar
Alexandre Julliard committed
919 920
    int xc = (myr.left+myr.right)/2;
    int yc = (myr.top+myr.bottom)/2;
921
    WCHAR str[] = {0, 0};
922
    static const WCHAR glyphFontName[] = { 'M','a','r','l','e','t','t',0 };
923
    UINT alignsave;
Alexandre Julliard's avatar
Alexandre Julliard committed
924 925
    int bksave;
    COLORREF clrsave;
926
    SIZE size;
Alexandre Julliard's avatar
Alexandre Julliard committed
927

Alexandre Julliard's avatar
Alexandre Julliard committed
928
    UITOOLS95_DFC_ButtonPush(dc, r, uFlags & 0xff00);
Alexandre Julliard's avatar
Alexandre Julliard committed
929

930
    switch(uFlags & 0xf)
Alexandre Julliard's avatar
Alexandre Julliard committed
931
    {
932 933 934 935 936
    case DFCS_CAPTIONCLOSE:     str[0] = 0x72; break;
    case DFCS_CAPTIONHELP:      str[0] = 0x73; break;
    case DFCS_CAPTIONMIN:       str[0] = 0x30; break;
    case DFCS_CAPTIONMAX:       str[0] = 0x31; break;
    case DFCS_CAPTIONRESTORE:   str[0] = 0x32; break;
Alexandre Julliard's avatar
Alexandre Julliard committed
937
    default:
938
        WARN("Invalid caption; flags=0x%04x\n", uFlags);
Alexandre Julliard's avatar
Alexandre Julliard committed
939 940
        return FALSE;
    }
941 942 943 944 945 946 947
    
    hf = CreateFontW(-SmallDiam, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE,
                    SYMBOL_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,
                    DEFAULT_QUALITY, FIXED_PITCH|FF_DONTCARE, glyphFontName);
    alignsave = SetTextAlign(dc, TA_TOP|TA_LEFT);
    bksave = SetBkMode(dc, TRANSPARENT);
    clrsave = GetTextColor(dc);
948
    hfsave = SelectObject(dc, hf);
949
    GetTextExtentPoint32W(dc, str, 1, &size);
Alexandre Julliard's avatar
Alexandre Julliard committed
950 951 952

    if(uFlags & DFCS_INACTIVE)
    {
953 954
        SetTextColor(dc, GetSysColor(COLOR_BTNHIGHLIGHT));
        TextOutW(dc, xc-size.cx/2+1, yc-size.cy/2+1, str, 1);
Alexandre Julliard's avatar
Alexandre Julliard committed
955
    }
956 957
    SetTextColor(dc, GetSysColor(colorIdx));
    TextOutW(dc, xc-size.cx/2, yc-size.cy/2, str, 1);
Alexandre Julliard's avatar
Alexandre Julliard committed
958

959 960 961 962 963 964
    SelectObject(dc, hfsave);
    SetTextColor(dc, clrsave);
    SetBkMode(dc, bksave);
    SetTextAlign(dc, alignsave);
    DeleteObject(hf);
  
Alexandre Julliard's avatar
Alexandre Julliard committed
965
    return TRUE;
Alexandre Julliard's avatar
Alexandre Julliard committed
966 967
}

Alexandre Julliard's avatar
Alexandre Julliard committed
968 969 970 971 972

/************************************************************************
 *      UITOOLS_DrawFrameScroll
 *
 * Draw a scroll-bar control coming from DrawFrameControl()
Alexandre Julliard's avatar
Alexandre Julliard committed
973
 */
974
static BOOL UITOOLS95_DrawFrameScroll(HDC dc, LPRECT r, UINT uFlags)
Alexandre Julliard's avatar
Alexandre Julliard committed
975
{
976 977
    POINT Line[4];
    RECT myr;
Alexandre Julliard's avatar
Alexandre Julliard committed
978 979
    int SmallDiam = UITOOLS_MakeSquareRect(r, &myr) - 2;
    int i;
980 981
    HBRUSH hbsave, hb, hb2;
    HPEN hpsave, hp, hp2;
982
    int tri = 290*SmallDiam/1000 - 1;
Alexandre Julliard's avatar
Alexandre Julliard committed
983 984
    int d46, d93;

985 986
    /*
     * This fixes a problem with really tiny "scroll" buttons. In particular
987
     * with the updown control.
988 989
     * Making sure that the arrow is as least 3 pixels wide (or high).
     */
990
    tri = max( 2, tri );
991

Alexandre Julliard's avatar
Alexandre Julliard committed
992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004
    switch(uFlags & 0xff)
    {
    case DFCS_SCROLLCOMBOBOX:
    case DFCS_SCROLLDOWN:
        Line[2].x = myr.left + 470*SmallDiam/1000 + 2;
        Line[2].y = myr.top  + 687*SmallDiam/1000 + 1;
        Line[0].x = Line[2].x - tri;
        Line[1].x = Line[2].x + tri;
        Line[0].y = Line[1].y = Line[2].y - tri;
        break;

    case DFCS_SCROLLUP:
        Line[2].x = myr.left + 470*SmallDiam/1000 + 2;
1005
        Line[2].y = myr.bottom - (687*SmallDiam/1000 + 1);
Alexandre Julliard's avatar
Alexandre Julliard committed
1006 1007 1008 1009 1010 1011
        Line[0].x = Line[2].x - tri;
        Line[1].x = Line[2].x + tri;
        Line[0].y = Line[1].y = Line[2].y + tri;
        break;

    case DFCS_SCROLLLEFT:
1012
        Line[2].x = myr.right - (687*SmallDiam/1000 + 1);
Alexandre Julliard's avatar
Alexandre Julliard committed
1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028
        Line[2].y = myr.top  + 470*SmallDiam/1000 + 2;
        Line[0].y = Line[2].y - tri;
        Line[1].y = Line[2].y + tri;
        Line[0].x = Line[1].x = Line[2].x + tri;
        break;

    case DFCS_SCROLLRIGHT:
        Line[2].x = myr.left + 687*SmallDiam/1000 + 1;
        Line[2].y = myr.top  + 470*SmallDiam/1000 + 2;
        Line[0].y = Line[2].y - tri;
        Line[1].y = Line[2].y + tri;
        Line[0].x = Line[1].x = Line[2].x - tri;
        break;

    case DFCS_SCROLLSIZEGRIP:
        /* This one breaks the flow... */
1029
        UITOOLS95_DrawRectEdge(dc, r, EDGE_BUMP, BF_MIDDLE | ((uFlags&(DFCS_MONO|DFCS_FLAT)) ? BF_MONO : 0), 1);
1030 1031
        hpsave = SelectObject(dc, GetStockObject(NULL_PEN));
        hbsave = SelectObject(dc, GetStockObject(NULL_BRUSH));
Alexandre Julliard's avatar
Alexandre Julliard committed
1032 1033
        if(uFlags & (DFCS_MONO|DFCS_FLAT))
        {
1034
            hp = hp2 = SYSCOLOR_GetPen(COLOR_WINDOWFRAME);
1035
            hb = hb2 = GetSysColorBrush(COLOR_WINDOWFRAME);
Alexandre Julliard's avatar
Alexandre Julliard committed
1036 1037 1038
        }
        else
        {
1039 1040
            hp  = SYSCOLOR_GetPen(COLOR_BTNHIGHLIGHT);
            hp2 = SYSCOLOR_GetPen(COLOR_BTNSHADOW);
1041 1042
            hb  = GetSysColorBrush(COLOR_BTNHIGHLIGHT);
            hb2 = GetSysColorBrush(COLOR_BTNSHADOW);
Alexandre Julliard's avatar
Alexandre Julliard committed
1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053
        }
        Line[0].x = Line[1].x = r->right-1;
        Line[2].y = Line[3].y = r->bottom-1;
        d46 = 46*SmallDiam/750;
        d93 = 93*SmallDiam/750;

        i = 586*SmallDiam/750;
        Line[0].y = r->bottom - i - 1;
        Line[3].x = r->right - i - 1;
        Line[1].y = Line[0].y + d46;
        Line[2].x = Line[3].x + d46;
1054 1055 1056
        SelectObject(dc, hb);
        SelectObject(dc, hp);
        Polygon(dc, Line, 4);
Alexandre Julliard's avatar
Alexandre Julliard committed
1057 1058 1059 1060

        Line[1].y++; Line[2].x++;
        Line[0].y = Line[1].y + d93;
        Line[3].x = Line[2].x + d93;
1061 1062 1063
        SelectObject(dc, hb2);
        SelectObject(dc, hp2);
        Polygon(dc, Line, 4);
Alexandre Julliard's avatar
Alexandre Julliard committed
1064 1065 1066 1067 1068 1069

        i = 398*SmallDiam/750;
        Line[0].y = r->bottom - i - 1;
        Line[3].x = r->right - i - 1;
        Line[1].y = Line[0].y + d46;
        Line[2].x = Line[3].x + d46;
1070 1071 1072
        SelectObject(dc, hb);
        SelectObject(dc, hp);
        Polygon(dc, Line, 4);
Alexandre Julliard's avatar
Alexandre Julliard committed
1073 1074 1075 1076

        Line[1].y++; Line[2].x++;
        Line[0].y = Line[1].y + d93;
        Line[3].x = Line[2].x + d93;
1077 1078 1079
        SelectObject(dc, hb2);
        SelectObject(dc, hp2);
        Polygon(dc, Line, 4);
Alexandre Julliard's avatar
Alexandre Julliard committed
1080 1081 1082 1083 1084 1085

        i = 210*SmallDiam/750;
        Line[0].y = r->bottom - i - 1;
        Line[3].x = r->right - i - 1;
        Line[1].y = Line[0].y + d46;
        Line[2].x = Line[3].x + d46;
1086 1087 1088
        SelectObject(dc, hb);
        SelectObject(dc, hp);
        Polygon(dc, Line, 4);
Alexandre Julliard's avatar
Alexandre Julliard committed
1089 1090 1091 1092

        Line[1].y++; Line[2].x++;
        Line[0].y = Line[1].y + d93;
        Line[3].x = Line[2].x + d93;
1093 1094 1095
        SelectObject(dc, hb2);
        SelectObject(dc, hp2);
        Polygon(dc, Line, 4);
Alexandre Julliard's avatar
Alexandre Julliard committed
1096

1097 1098
        SelectObject(dc, hpsave);
        SelectObject(dc, hbsave);
Alexandre Julliard's avatar
Alexandre Julliard committed
1099 1100 1101
        return TRUE;

    default:
1102
        WARN("Invalid scroll; flags=0x%04x\n", uFlags);
Alexandre Julliard's avatar
Alexandre Julliard committed
1103 1104 1105 1106
        return FALSE;
    }

    /* Here do the real scroll-bar controls end up */
1107 1108 1109
    if( ! (uFlags & (0xff00 & ~DFCS_ADJUSTRECT)) )
      /* UITOOLS95_DFC_ButtonPush always uses BF_SOFT which we don't */
      /* want for the normal scroll-arrow button. */
1110
      UITOOLS95_DrawRectEdge( dc, r, EDGE_RAISED, (uFlags&DFCS_ADJUSTRECT) | BF_MIDDLE | BF_RECT, 1);
1111 1112
    else
      UITOOLS95_DFC_ButtonPush(dc, r, (uFlags & 0xff00) );
Alexandre Julliard's avatar
Alexandre Julliard committed
1113 1114 1115

    if(uFlags & DFCS_INACTIVE)
    {
1116 1117
        hbsave = SelectObject(dc, GetSysColorBrush(COLOR_BTNHIGHLIGHT));
        hpsave = SelectObject(dc, SYSCOLOR_GetPen(COLOR_BTNHIGHLIGHT));
1118 1119 1120
        Polygon(dc, Line, 3);
        SelectObject(dc, hpsave);
        SelectObject(dc, hbsave);
Alexandre Julliard's avatar
Alexandre Julliard committed
1121 1122
    }

1123 1124 1125
    if( (uFlags & DFCS_INACTIVE) || !(uFlags & DFCS_PUSHED) )
      for(i = 0; i < 3; i++)
      {
Alexandre Julliard's avatar
Alexandre Julliard committed
1126 1127
        Line[i].x--;
        Line[i].y--;
1128
      }
Alexandre Julliard's avatar
Alexandre Julliard committed
1129

Alexandre Julliard's avatar
Alexandre Julliard committed
1130
    i = uFlags & DFCS_INACTIVE ? COLOR_BTNSHADOW : COLOR_BTNTEXT;
1131 1132
    hbsave = SelectObject(dc, GetSysColorBrush(i));
    hpsave = SelectObject(dc, SYSCOLOR_GetPen(i));
1133 1134 1135
    Polygon(dc, Line, 3);
    SelectObject(dc, hpsave);
    SelectObject(dc, hbsave);
Alexandre Julliard's avatar
Alexandre Julliard committed
1136 1137

    return TRUE;
Alexandre Julliard's avatar
Alexandre Julliard committed
1138 1139
}

Alexandre Julliard's avatar
Alexandre Julliard committed
1140 1141 1142 1143
/************************************************************************
 *      UITOOLS_DrawFrameMenu
 *
 * Draw a menu control coming from DrawFrameControl()
Alexandre Julliard's avatar
Alexandre Julliard committed
1144
 */
1145
static BOOL UITOOLS95_DrawFrameMenu(HDC dc, LPRECT r, UINT uFlags)
Alexandre Julliard's avatar
Alexandre Julliard committed
1146
{
1147 1148
    POINT Points[6];
    RECT myr;
Alexandre Julliard's avatar
Alexandre Julliard committed
1149 1150
    int SmallDiam = UITOOLS_MakeSquareRect(r, &myr);
    int i;
1151 1152
    HBRUSH hbsave;
    HPEN hpsave;
Alexandre Julliard's avatar
Alexandre Julliard committed
1153 1154
    int xe, ye;
    int xc, yc;
1155
    BOOL retval = TRUE;
Alexandre Julliard's avatar
Alexandre Julliard committed
1156 1157 1158 1159 1160 1161

    /* Using black and white seems to be utterly wrong, but win95 doesn't */
    /* use anything else. I think I tried all sys-colors to change things */
    /* without luck. It seems as if this behavior is inherited from the */
    /* win31 DFC() implementation... (you remember, B/W menus). */

1162
    FillRect(dc, r, GetStockObject(WHITE_BRUSH));
Alexandre Julliard's avatar
Alexandre Julliard committed
1163

1164 1165
    hbsave = SelectObject(dc, GetStockObject(BLACK_BRUSH));
    hpsave = SelectObject(dc, GetStockObject(BLACK_PEN));
Alexandre Julliard's avatar
Alexandre Julliard committed
1166 1167 1168 1169 1170 1171 1172 1173 1174 1175

    switch(uFlags & 0xff)
    {
    case DFCS_MENUARROW:
        i = 187*SmallDiam/750;
        Points[2].x = myr.left + 468*SmallDiam/750;
        Points[2].y = myr.top  + 352*SmallDiam/750+1;
        Points[0].y = Points[2].y - i;
        Points[1].y = Points[2].y + i;
        Points[0].x = Points[1].x = Points[2].x - i;
1176
        Polygon(dc, Points, 3);
Alexandre Julliard's avatar
Alexandre Julliard committed
1177 1178 1179 1180 1181 1182 1183 1184 1185
        break;

    case DFCS_MENUBULLET:
        xe = myr.left;
        ye = myr.top  + SmallDiam - SmallDiam/2;
        xc = myr.left + SmallDiam - SmallDiam/2;
        yc = myr.top  + SmallDiam - SmallDiam/2;
        i = 234*SmallDiam/750;
        i = i < 1 ? 1 : i;
1186
        SetRect(&myr, xc - i + i / 2, yc - i + i / 2, xc + i / 2, yc + i / 2);
1187
        Pie(dc, myr.left, myr.top, myr.right, myr.bottom, xe, ye, xe, ye);
Alexandre Julliard's avatar
Alexandre Julliard committed
1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202
        break;

    case DFCS_MENUCHECK:
        Points[0].x = myr.left + 253*SmallDiam/1000;
        Points[0].y = myr.top  + 445*SmallDiam/1000;
        Points[1].x = myr.left + 409*SmallDiam/1000;
        Points[1].y = Points[0].y + (Points[1].x-Points[0].x);
        Points[2].x = myr.left + 690*SmallDiam/1000;
        Points[2].y = Points[1].y - (Points[2].x-Points[1].x);
        Points[3].x = Points[2].x;
        Points[3].y = Points[2].y + 3*SmallDiam/16;
        Points[4].x = Points[1].x;
        Points[4].y = Points[1].y + 3*SmallDiam/16;
        Points[5].x = Points[0].x;
        Points[5].y = Points[0].y + 3*SmallDiam/16;
1203
        Polygon(dc, Points, 6);
Alexandre Julliard's avatar
Alexandre Julliard committed
1204 1205 1206
        break;

    default:
1207
        WARN("Invalid menu; flags=0x%04x\n", uFlags);
Alexandre Julliard's avatar
Alexandre Julliard committed
1208 1209 1210 1211
        retval = FALSE;
        break;
    }

1212 1213
    SelectObject(dc, hpsave);
    SelectObject(dc, hbsave);
Alexandre Julliard's avatar
Alexandre Julliard committed
1214
    return retval;
Alexandre Julliard's avatar
Alexandre Julliard committed
1215 1216 1217 1218
}


/**********************************************************************
1219
 *          DrawFrameControl  (USER32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
1220
 */
1221 1222
BOOL WINAPI DrawFrameControl( HDC hdc, LPRECT rc, UINT uType,
                                  UINT uState )
Alexandre Julliard's avatar
Alexandre Julliard committed
1223 1224 1225 1226
{
    switch(uType)
    {
    case DFC_BUTTON:
Alexandre Julliard's avatar
Alexandre Julliard committed
1227
      return UITOOLS95_DrawFrameButton(hdc, rc, uState);
Alexandre Julliard's avatar
Alexandre Julliard committed
1228
    case DFC_CAPTION:
Alexandre Julliard's avatar
Alexandre Julliard committed
1229
      return UITOOLS95_DrawFrameCaption(hdc, rc, uState);
Alexandre Julliard's avatar
Alexandre Julliard committed
1230
    case DFC_MENU:
Alexandre Julliard's avatar
Alexandre Julliard committed
1231
      return UITOOLS95_DrawFrameMenu(hdc, rc, uState);
1232 1233 1234
    /*case DFC_POPUPMENU:
      FIXME("DFC_POPUPMENU: not implemented\n");
      break;*/
Alexandre Julliard's avatar
Alexandre Julliard committed
1235
    case DFC_SCROLL:
Alexandre Julliard's avatar
Alexandre Julliard committed
1236
      return UITOOLS95_DrawFrameScroll(hdc, rc, uState);
Alexandre Julliard's avatar
Alexandre Julliard committed
1237
    default:
1238
      WARN("(%p,%p,%d,%x), bad type!\n", hdc,rc,uType,uState );
Alexandre Julliard's avatar
Alexandre Julliard committed
1239 1240 1241
    }
    return FALSE;
}
1242 1243


1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408
/***********************************************************************
 *		SetRect (USER32.@)
 */
BOOL WINAPI SetRect( LPRECT rect, INT left, INT top,
                       INT right, INT bottom )
{
    if (!rect) return FALSE;
    rect->left   = left;
    rect->right  = right;
    rect->top    = top;
    rect->bottom = bottom;
    return TRUE;
}


/***********************************************************************
 *		SetRectEmpty (USER32.@)
 */
BOOL WINAPI SetRectEmpty( LPRECT rect )
{
    if (!rect) return FALSE;
    rect->left = rect->right = rect->top = rect->bottom = 0;
    return TRUE;
}


/***********************************************************************
 *		CopyRect (USER32.@)
 */
BOOL WINAPI CopyRect( RECT *dest, const RECT *src )
{
    if (!dest || !src) return FALSE;
    *dest = *src;
    return TRUE;
}


/***********************************************************************
 *		IsRectEmpty (USER32.@)
 *
 * Bug compat: Windows checks for 0 or negative width/height.
 */
BOOL WINAPI IsRectEmpty( const RECT *rect )
{
    if (!rect) return TRUE;
    return ((rect->left >= rect->right) || (rect->top >= rect->bottom));
}


/***********************************************************************
 *		PtInRect (USER32.@)
 */
BOOL WINAPI PtInRect( const RECT *rect, POINT pt )
{
    if (!rect) return FALSE;
    return ((pt.x >= rect->left) && (pt.x < rect->right) &&
            (pt.y >= rect->top) && (pt.y < rect->bottom));
}


/***********************************************************************
 *		OffsetRect (USER32.@)
 */
BOOL WINAPI OffsetRect( LPRECT rect, INT x, INT y )
{
    if (!rect) return FALSE;
    rect->left   += x;
    rect->right  += x;
    rect->top    += y;
    rect->bottom += y;
    return TRUE;
}


/***********************************************************************
 *		InflateRect (USER32.@)
 */
BOOL WINAPI InflateRect( LPRECT rect, INT x, INT y )
{
    if (!rect) return FALSE;
    rect->left   -= x;
    rect->top    -= y;
    rect->right  += x;
    rect->bottom += y;
    return TRUE;
}


/***********************************************************************
 *		IntersectRect (USER32.@)
 */
BOOL WINAPI IntersectRect( LPRECT dest, const RECT *src1, const RECT *src2 )
{
    if (!dest || !src1 || !src2) return FALSE;
    if (IsRectEmpty(src1) || IsRectEmpty(src2) ||
        (src1->left >= src2->right) || (src2->left >= src1->right) ||
        (src1->top >= src2->bottom) || (src2->top >= src1->bottom))
    {
        SetRectEmpty( dest );
        return FALSE;
    }
    dest->left   = max( src1->left, src2->left );
    dest->right  = min( src1->right, src2->right );
    dest->top    = max( src1->top, src2->top );
    dest->bottom = min( src1->bottom, src2->bottom );
    return TRUE;
}


/***********************************************************************
 *		UnionRect (USER32.@)
 */
BOOL WINAPI UnionRect( LPRECT dest, const RECT *src1, const RECT *src2 )
{
    if (!dest) return FALSE;
    if (IsRectEmpty(src1))
    {
        if (IsRectEmpty(src2))
        {
            SetRectEmpty( dest );
            return FALSE;
        }
        else *dest = *src2;
    }
    else
    {
        if (IsRectEmpty(src2)) *dest = *src1;
        else
        {
            dest->left   = min( src1->left, src2->left );
            dest->right  = max( src1->right, src2->right );
            dest->top    = min( src1->top, src2->top );
            dest->bottom = max( src1->bottom, src2->bottom );
        }
    }
    return TRUE;
}


/***********************************************************************
 *		EqualRect (USER32.@)
 */
BOOL WINAPI EqualRect( const RECT* rect1, const RECT* rect2 )
{
    if (!rect1 || !rect2) return FALSE;
    return ((rect1->left == rect2->left) && (rect1->right == rect2->right) &&
            (rect1->top == rect2->top) && (rect1->bottom == rect2->bottom));
}


/***********************************************************************
 *		SubtractRect (USER32.@)
 */
BOOL WINAPI SubtractRect( LPRECT dest, const RECT *src1, const RECT *src2 )
{
    RECT tmp;

    if (!dest) return FALSE;
    if (IsRectEmpty( src1 ))
    {
        SetRectEmpty( dest );
        return FALSE;
    }
    if (IntersectRect( &tmp, src1, src2 ))
    {
1409
        if (EqualRect( &tmp, src1 ))
1410 1411 1412 1413
        {
            SetRectEmpty( dest );
            return FALSE;
        }
1414
        *dest = *src1;
1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425
        if ((tmp.top == dest->top) && (tmp.bottom == dest->bottom))
        {
            if (tmp.left == dest->left) dest->left = tmp.right;
            else if (tmp.right == dest->right) dest->right = tmp.left;
        }
        else if ((tmp.left == dest->left) && (tmp.right == dest->right))
        {
            if (tmp.top == dest->top) dest->top = tmp.bottom;
            else if (tmp.bottom == dest->bottom) dest->bottom = tmp.top;
        }
    }
1426 1427 1428 1429
    else
    {
        *dest = *src1;
    }
1430 1431 1432 1433
    return TRUE;
}


1434 1435 1436 1437 1438
/***********************************************************************
 *		FillRect (USER32.@)
 */
INT WINAPI FillRect( HDC hdc, const RECT *rect, HBRUSH hbrush )
{
1439
    HBRUSH prev_brush;
1440

1441
    if (hbrush <= (HBRUSH) (COLOR_MAX + 1)) hbrush = GetSysColorBrush( HandleToULong(hbrush) - 1 );
1442

1443
    prev_brush = SelectObject( hdc, hbrush );
1444 1445
    PatBlt( hdc, rect->left, rect->top,
              rect->right - rect->left, rect->bottom - rect->top, PATCOPY );
1446
    if (prev_brush) SelectObject( hdc, prev_brush );
1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468
    return 1;
}


/***********************************************************************
 *		InvertRect (USER32.@)
 */
BOOL WINAPI InvertRect( HDC hdc, const RECT *rect )
{
    return PatBlt( hdc, rect->left, rect->top,
                   rect->right - rect->left, rect->bottom - rect->top, DSTINVERT );
}


/***********************************************************************
 *		FrameRect (USER32.@)
 */
INT WINAPI FrameRect( HDC hdc, const RECT *rect, HBRUSH hbrush )
{
    HBRUSH prevBrush;
    RECT r = *rect;

1469
    if (IsRectEmpty(&r)) return 0;
1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491
    if (!(prevBrush = SelectObject( hdc, hbrush ))) return 0;

    PatBlt( hdc, r.left, r.top, 1, r.bottom - r.top, PATCOPY );
    PatBlt( hdc, r.right - 1, r.top, 1, r.bottom - r.top, PATCOPY );
    PatBlt( hdc, r.left, r.top, r.right - r.left, 1, PATCOPY );
    PatBlt( hdc, r.left, r.bottom - 1, r.right - r.left, 1, PATCOPY );

    SelectObject( hdc, prevBrush );
    return TRUE;
}


/***********************************************************************
 *		DrawFocusRect (USER32.@)
 *
 * FIXME: PatBlt(PATINVERT) with background brush.
 */
BOOL WINAPI DrawFocusRect( HDC hdc, const RECT* rc )
{
    HBRUSH hOldBrush;
    HPEN hOldPen, hNewPen;
    INT oldDrawMode, oldBkMode;
1492
    LOGBRUSH lb;
1493 1494

    hOldBrush = SelectObject(hdc, GetStockObject(NULL_BRUSH));
1495
    lb.lbStyle = BS_SOLID;
1496
    lb.lbColor = 0;
1497
    hNewPen = ExtCreatePen(PS_COSMETIC|PS_ALTERNATE, 1, &lb, 0, NULL);
1498
    hOldPen = SelectObject(hdc, hNewPen);
1499
    oldDrawMode = SetROP2(hdc, R2_NOT);
1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547
    oldBkMode = SetBkMode(hdc, TRANSPARENT);

    Rectangle(hdc, rc->left, rc->top, rc->right, rc->bottom);

    SetBkMode(hdc, oldBkMode);
    SetROP2(hdc, oldDrawMode);
    SelectObject(hdc, hOldPen);
    DeleteObject(hNewPen);
    SelectObject(hdc, hOldBrush);

    return TRUE;
}


/**********************************************************************
 *		DrawAnimatedRects (USER32.@)
 */
BOOL WINAPI DrawAnimatedRects( HWND hwnd, INT idAni, const RECT* lprcFrom, const RECT* lprcTo )
{
    FIXME("(%p,%d,%p,%p): stub\n",hwnd,idAni,lprcFrom,lprcTo);
    return TRUE;
}


/**********************************************************************
 *          UITOOLS_DrawStateJam
 *
 * Jams in the requested type in the dc
 */
static BOOL UITOOLS_DrawStateJam( HDC hdc, UINT opcode, DRAWSTATEPROC func, LPARAM lp, WPARAM wp,
                                  LPRECT rc, UINT dtflags, BOOL unicode )
{
    HDC memdc;
    HBITMAP hbmsave;
    BOOL retval;
    INT cx = rc->right - rc->left;
    INT cy = rc->bottom - rc->top;

    switch(opcode)
    {
    case DST_TEXT:
    case DST_PREFIXTEXT:
        if(unicode)
            return DrawTextW(hdc, (LPWSTR)lp, (INT)wp, rc, dtflags);
        else
            return DrawTextA(hdc, (LPSTR)lp, (INT)wp, rc, dtflags);

    case DST_ICON:
1548
        return DrawIconEx(hdc, rc->left, rc->top, (HICON)lp, 0, 0, 0, NULL, DI_NORMAL);
1549 1550 1551 1552

    case DST_BITMAP:
        memdc = CreateCompatibleDC(hdc);
        if(!memdc) return FALSE;
1553
        hbmsave = SelectObject(memdc, (HBITMAP)lp);
1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598
        if(!hbmsave)
        {
            DeleteDC(memdc);
            return FALSE;
        }
        retval = BitBlt(hdc, rc->left, rc->top, cx, cy, memdc, 0, 0, SRCCOPY);
        SelectObject(memdc, hbmsave);
        DeleteDC(memdc);
        return retval;

    case DST_COMPLEX:
        if(func) {
            BOOL bRet;
            /* DRAWSTATEPROC assumes that it draws at the center of coordinates  */

            OffsetViewportOrgEx(hdc, rc->left, rc->top, NULL);
            bRet = func(hdc, lp, wp, cx, cy);
            /* Restore origin */
            OffsetViewportOrgEx(hdc, -rc->left, -rc->top, NULL);
            return bRet;
        } else
            return FALSE;
    }
    return FALSE;
}

/**********************************************************************
 *      UITOOLS_DrawState()
 */
static BOOL UITOOLS_DrawState(HDC hdc, HBRUSH hbr, DRAWSTATEPROC func, LPARAM lp, WPARAM wp,
                              INT x, INT y, INT cx, INT cy, UINT flags, BOOL unicode )
{
    HBITMAP hbm, hbmsave;
    HFONT hfsave;
    HBRUSH hbsave, hbrtmp = 0;
    HDC memdc;
    RECT rc;
    UINT dtflags = DT_NOCLIP;
    COLORREF fg, bg;
    UINT opcode = flags & 0xf;
    INT len = wp;
    BOOL retval, tmp;

    if((opcode == DST_TEXT || opcode == DST_PREFIXTEXT) && !len)    /* The string is '\0' terminated */
    {
1599 1600
        if (!lp) return FALSE;

1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624
        if(unicode)
            len = strlenW((LPWSTR)lp);
        else
            len = strlen((LPSTR)lp);
    }

    /* Find out what size the image has if not given by caller */
    if(!cx || !cy)
    {
        SIZE s;
        BITMAP bm;

        switch(opcode)
        {
        case DST_TEXT:
        case DST_PREFIXTEXT:
            if(unicode)
                retval = GetTextExtentPoint32W(hdc, (LPWSTR)lp, len, &s);
            else
                retval = GetTextExtentPoint32A(hdc, (LPSTR)lp, len, &s);
            if(!retval) return FALSE;
            break;

        case DST_ICON:
1625
            if (!get_icon_size( (HICON)lp, &s )) return FALSE;
1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641
            break;

        case DST_BITMAP:
            if(!GetObjectA((HBITMAP)lp, sizeof(bm), &bm)) return FALSE;
            s.cx = bm.bmWidth;
            s.cy = bm.bmHeight;
            break;

        case DST_COMPLEX: /* cx and cy must be set in this mode */
            return FALSE;
        }

        if(!cx) cx = s.cx;
        if(!cy) cy = s.cy;
    }

1642
    SetRect(&rc, x, y, x + cx, y + cy);
1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667

    if(flags & DSS_RIGHT)    /* This one is not documented in the win32.hlp file */
        dtflags |= DT_RIGHT;
    if(opcode == DST_TEXT)
        dtflags |= DT_NOPREFIX;

    /* For DSS_NORMAL we just jam in the image and return */
    if((flags & 0x7ff0) == DSS_NORMAL)
    {
        return UITOOLS_DrawStateJam(hdc, opcode, func, lp, len, &rc, dtflags, unicode);
    }

    /* For all other states we need to convert the image to B/W in a local bitmap */
    /* before it is displayed */
    fg = SetTextColor(hdc, RGB(0, 0, 0));
    bg = SetBkColor(hdc, RGB(255, 255, 255));
    hbm = NULL; hbmsave = NULL;
    memdc = NULL; hbsave = NULL;
    retval = FALSE; /* assume failure */

    /* From here on we must use "goto cleanup" when something goes wrong */
    hbm     = CreateBitmap(cx, cy, 1, 1, NULL);
    if(!hbm) goto cleanup;
    memdc   = CreateCompatibleDC(hdc);
    if(!memdc) goto cleanup;
1668
    hbmsave = SelectObject(memdc, hbm);
1669
    if(!hbmsave) goto cleanup;
1670
    SetRect(&rc, 0, 0, cx, cy);
1671
    if(!FillRect(memdc, &rc, GetStockObject(WHITE_BRUSH))) goto cleanup;
1672 1673
    SetBkColor(memdc, RGB(255, 255, 255));
    SetTextColor(memdc, RGB(0, 0, 0));
1674
    hfsave  = SelectObject(memdc, GetCurrentObject(hdc, OBJ_FONT));
1675
    SetLayout( memdc, GetLayout( hdc ));
1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687

    /* DST_COMPLEX may draw text as well,
     * so we must be sure that correct font is selected
     */
    if(!hfsave && (opcode <= DST_PREFIXTEXT)) goto cleanup;
    tmp = UITOOLS_DrawStateJam(memdc, opcode, func, lp, len, &rc, dtflags, unicode);
    if(hfsave) SelectObject(memdc, hfsave);
    if(!tmp) goto cleanup;

    /* This state cause the image to be dithered */
    if(flags & DSS_UNION)
    {
1688
        hbsave = SelectObject(memdc, SYSCOLOR_Get55AABrush());
1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703
        if(!hbsave) goto cleanup;
        tmp = PatBlt(memdc, 0, 0, cx, cy, 0x00FA0089);
        SelectObject(memdc, hbsave);
        if(!tmp) goto cleanup;
    }

    if (flags & DSS_DISABLED)
       hbrtmp = CreateSolidBrush(GetSysColor(COLOR_3DHILIGHT));
    else if (flags & DSS_DEFAULT)
       hbrtmp = CreateSolidBrush(GetSysColor(COLOR_3DSHADOW));

    /* Draw light or dark shadow */
    if (flags & (DSS_DISABLED|DSS_DEFAULT))
    {
       if(!hbrtmp) goto cleanup;
1704
       hbsave = SelectObject(hdc, hbrtmp);
1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718
       if(!hbsave) goto cleanup;
       if(!BitBlt(hdc, x+1, y+1, cx, cy, memdc, 0, 0, 0x00B8074A)) goto cleanup;
       SelectObject(hdc, hbsave);
       DeleteObject(hbrtmp);
       hbrtmp = 0;
    }

    if (flags & DSS_DISABLED)
    {
       hbr = hbrtmp = CreateSolidBrush(GetSysColor(COLOR_3DSHADOW));
       if(!hbrtmp) goto cleanup;
    }
    else if (!hbr)
    {
1719
       hbr = GetStockObject(BLACK_BRUSH);
1720 1721
    }

1722
    hbsave = SelectObject(hdc, hbr);
1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759

    if(!BitBlt(hdc, x, y, cx, cy, memdc, 0, 0, 0x00B8074A)) goto cleanup;

    retval = TRUE; /* We succeeded */

cleanup:
    SetTextColor(hdc, fg);
    SetBkColor(hdc, bg);

    if(hbsave)  SelectObject(hdc, hbsave);
    if(hbmsave) SelectObject(memdc, hbmsave);
    if(hbrtmp)  DeleteObject(hbrtmp);
    if(hbm)     DeleteObject(hbm);
    if(memdc)   DeleteDC(memdc);

    return retval;
}

/**********************************************************************
 *		DrawStateA (USER32.@)
 */
BOOL WINAPI DrawStateA(HDC hdc, HBRUSH hbr,
                   DRAWSTATEPROC func, LPARAM ldata, WPARAM wdata,
                   INT x, INT y, INT cx, INT cy, UINT flags)
{
    return UITOOLS_DrawState(hdc, hbr, func, ldata, wdata, x, y, cx, cy, flags, FALSE);
}

/**********************************************************************
 *		DrawStateW (USER32.@)
 */
BOOL WINAPI DrawStateW(HDC hdc, HBRUSH hbr,
                   DRAWSTATEPROC func, LPARAM ldata, WPARAM wdata,
                   INT x, INT y, INT cx, INT cy, UINT flags)
{
    return UITOOLS_DrawState(hdc, hbr, func, ldata, wdata, x, y, cx, cy, flags, TRUE);
}