pen.c 8.27 KB
Newer Older
Alexandre Julliard's avatar
Alexandre Julliard committed
1 2 3 4
/*
 * GDI pen objects
 *
 * Copyright 1993 Alexandre Julliard
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 20
 */

21 22
#include "config.h"

23
#include <stdarg.h>
24
#include <stdlib.h>
25
#include <string.h>
26
#include <assert.h>
27 28

#include "windef.h"
29
#include "winbase.h"
30
#include "wingdi.h"
31
#include "gdi_private.h"
32
#include "wine/debug.h"
Alexandre Julliard's avatar
Alexandre Julliard committed
33

34
WINE_DEFAULT_DEBUG_CHANNEL(gdi);
35

36 37 38
  /* GDI logical pen object */
typedef struct
{
39 40
    GDIOBJHDR header;
    EXTLOGPEN logpen;
41 42
} PENOBJ;

Alexandre Julliard's avatar
Alexandre Julliard committed
43

44
static HGDIOBJ PEN_SelectObject( HGDIOBJ handle, HDC hdc );
45
static INT PEN_GetObject( HGDIOBJ handle, INT count, LPVOID buffer );
46
static BOOL PEN_DeleteObject( HGDIOBJ handle );
47 48 49 50 51 52 53

static const struct gdi_obj_funcs pen_funcs =
{
    PEN_SelectObject,  /* pSelectObject */
    PEN_GetObject,     /* pGetObjectA */
    PEN_GetObject,     /* pGetObjectW */
    NULL,              /* pUnrealizeObject */
54
    PEN_DeleteObject   /* pDeleteObject */
55
};
Alexandre Julliard's avatar
Alexandre Julliard committed
56 57


Alexandre Julliard's avatar
Alexandre Julliard committed
58
/***********************************************************************
59
 *           CreatePen    (GDI32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
60
 */
61
HPEN WINAPI CreatePen( INT style, INT width, COLORREF color )
Alexandre Julliard's avatar
Alexandre Julliard committed
62
{
63 64
    LOGPEN logpen;

65
    TRACE("%d %d %06x\n", style, width, color );
66

67
    logpen.lopnStyle = style;
68 69 70 71
    logpen.lopnWidth.x = width;
    logpen.lopnWidth.y = 0;
    logpen.lopnColor = color;

72
    return CreatePenIndirect( &logpen );
Alexandre Julliard's avatar
Alexandre Julliard committed
73 74 75 76
}


/***********************************************************************
77
 *           CreatePenIndirect    (GDI32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
78
 */
79
HPEN WINAPI CreatePenIndirect( const LOGPEN * pen )
Alexandre Julliard's avatar
Alexandre Julliard committed
80 81
{
    PENOBJ * penPtr;
82
    HPEN hpen;
Alexandre Julliard's avatar
Alexandre Julliard committed
83

84 85 86 87 88 89
    if (pen->lopnStyle == PS_NULL)
    {
        hpen = GetStockObject(NULL_PEN);
        if (hpen) return hpen;
    }

90 91
    if (!(penPtr = HeapAlloc( GetProcessHeap(), 0, sizeof(*penPtr) ))) return 0;

92
    if (pen->lopnStyle == PS_USERSTYLE || pen->lopnStyle == PS_ALTERNATE)
93
        penPtr->logpen.elpPenStyle = PS_SOLID;
94
    else
95
        penPtr->logpen.elpPenStyle = pen->lopnStyle;
96 97
    if (pen->lopnStyle == PS_NULL)
    {
98 99
        penPtr->logpen.elpWidth = 1;
        penPtr->logpen.elpColor = RGB(0, 0, 0);
100 101 102
    }
    else
    {
103 104
        penPtr->logpen.elpWidth = abs(pen->lopnWidth.x);
        penPtr->logpen.elpColor = pen->lopnColor;
105
    }
106 107 108 109 110
    penPtr->logpen.elpBrushStyle = BS_SOLID;
    penPtr->logpen.elpHatch = 0;
    penPtr->logpen.elpNumEntries = 0;
    penPtr->logpen.elpStyleEntry[0] = 0;

111 112
    if (!(hpen = alloc_gdi_handle( &penPtr->header, OBJ_PEN, &pen_funcs )))
        HeapFree( GetProcessHeap(), 0, penPtr );
Alexandre Julliard's avatar
Alexandre Julliard committed
113 114 115
    return hpen;
}

Alexandre Julliard's avatar
Alexandre Julliard committed
116
/***********************************************************************
117
 *           ExtCreatePen    (GDI32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
118 119 120 121
 *
 * FIXME: PS_USERSTYLE not handled
 */

122 123
HPEN WINAPI ExtCreatePen( DWORD style, DWORD width,
                              const LOGBRUSH * brush, DWORD style_count,
Alexandre Julliard's avatar
Alexandre Julliard committed
124 125
                              const DWORD *style_bits )
{
126 127
    PENOBJ * penPtr;
    HPEN hpen;
Alexandre Julliard's avatar
Alexandre Julliard committed
128 129

    if ((style & PS_STYLE_MASK) == PS_USERSTYLE)
130
    {
131 132 133 134
        if(((INT)style_count) <= 0)
            return 0;

        if ((style_count > 16) || !style_bits)
135 136 137 138
        {
            SetLastError(ERROR_INVALID_PARAMETER);
            return 0;
        }
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162

        if ((style & PS_TYPE_MASK) == PS_COSMETIC)
        {
            /* FIXME: PS_USERSTYLE workaround */
            FIXME("PS_COSMETIC | PS_USERSTYLE not handled\n");
            style = (style & ~PS_STYLE_MASK) | PS_SOLID;
        }
        else
        {
            UINT i;
            BOOL has_neg = FALSE, all_zero = TRUE;

            for(i = 0; (i < style_count) && !has_neg; i++)
            {
                has_neg = has_neg || (((INT)(style_bits[i])) < 0);
                all_zero = all_zero && (style_bits[i] == 0);
            }

            if(all_zero || has_neg)
            {
                SetLastError(ERROR_INVALID_PARAMETER);
                return 0;
            }
        }
163 164 165 166 167 168 169 170 171 172 173 174 175
    }
    else
    {
        if (style_count || style_bits)
        {
            SetLastError(ERROR_INVALID_PARAMETER);
            return 0;
        }
    }

    if ((style & PS_STYLE_MASK) == PS_NULL)
        return CreatePen( PS_NULL, 0, brush->lbColor );

Alexandre Julliard's avatar
Alexandre Julliard committed
176
    if ((style & PS_TYPE_MASK) == PS_GEOMETRIC)
177 178 179 180 181 182 183 184
    {
        /* PS_ALTERNATE is applicable only for cosmetic pens */
        if ((style & PS_STYLE_MASK) == PS_ALTERNATE)
        {
            SetLastError(ERROR_INVALID_PARAMETER);
            return 0;
        }

185 186 187 188 189
        if (brush->lbHatch && ((brush->lbStyle == BS_SOLID) || (brush->lbStyle == BS_HOLLOW)))
        {
            static int fixme_hatches_shown;
            if (!fixme_hatches_shown++) FIXME("Hatches not implemented\n");
        }
190 191 192
    }
    else
    {
Austin English's avatar
Austin English committed
193
        /* PS_INSIDEFRAME is applicable only for geometric pens */
194 195 196 197 198 199
        if ((style & PS_STYLE_MASK) == PS_INSIDEFRAME || width != 1)
        {
            SetLastError(ERROR_INVALID_PARAMETER);
            return 0;
        }
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
200

201 202
    if (!(penPtr = HeapAlloc(GetProcessHeap(), 0, FIELD_OFFSET(PENOBJ,logpen.elpStyleEntry[style_count]))))
        return 0;
203

204 205 206 207 208 209 210
    penPtr->logpen.elpPenStyle = style;
    penPtr->logpen.elpWidth = abs(width);
    penPtr->logpen.elpBrushStyle = brush->lbStyle;
    penPtr->logpen.elpColor = brush->lbColor;
    penPtr->logpen.elpHatch = brush->lbHatch;
    penPtr->logpen.elpNumEntries = style_count;
    memcpy(penPtr->logpen.elpStyleEntry, style_bits, style_count * sizeof(DWORD));
211

212 213
    if (!(hpen = alloc_gdi_handle( &penPtr->header, OBJ_EXTPEN, &pen_funcs )))
        HeapFree( GetProcessHeap(), 0, penPtr );
214
    return hpen;
Alexandre Julliard's avatar
Alexandre Julliard committed
215
}
Alexandre Julliard's avatar
Alexandre Julliard committed
216

217 218 219
/***********************************************************************
 *           PEN_SelectObject
 */
220
static HGDIOBJ PEN_SelectObject( HGDIOBJ handle, HDC hdc )
221
{
222
    HGDIOBJ ret = 0;
223
    DC *dc = get_dc_ptr( hdc );
224

225 226 227 228 229 230 231 232 233 234 235
    if (!dc)
    {
        SetLastError( ERROR_INVALID_HANDLE );
        return 0;
    }

    if (!GDI_inc_ref_count( handle ))
    {
        release_dc_ptr( dc );
        return 0;
    }
236

237 238 239 240 241
    if (dc->funcs->pSelectPen && !dc->funcs->pSelectPen( dc->physDev, handle ))
    {
        GDI_dec_ref_count( handle );
    }
    else
242 243 244 245 246
    {
        ret = dc->hPen;
        dc->hPen = handle;
        GDI_dec_ref_count( ret );
    }
247
    release_dc_ptr( dc );
248 249 250 251
    return ret;
}


252 253 254 255 256
/***********************************************************************
 *           PEN_DeleteObject
 */
static BOOL PEN_DeleteObject( HGDIOBJ handle )
{
257
    PENOBJ *pen = free_gdi_handle( handle );
258 259

    if (!pen) return FALSE;
260
    return HeapFree( GetProcessHeap(), 0, pen );
261 262 263
}


Alexandre Julliard's avatar
Alexandre Julliard committed
264
/***********************************************************************
265
 *           PEN_GetObject
Alexandre Julliard's avatar
Alexandre Julliard committed
266
 */
267
static INT PEN_GetObject( HGDIOBJ handle, INT count, LPVOID buffer )
Alexandre Julliard's avatar
Alexandre Julliard committed
268
{
269
    PENOBJ *pen = GDI_GetObjPtr( handle, 0 );
270 271 272
    INT ret = 0;

    if (!pen) return 0;
273

274
    switch (pen->header.type)
275
    {
276
    case OBJ_PEN:
277 278 279
    {
        LOGPEN *lp;

280 281 282
        if (!buffer) ret = sizeof(LOGPEN);
        else if (count < sizeof(LOGPEN)) ret = 0;
        else if ((pen->logpen.elpPenStyle & PS_STYLE_MASK) == PS_NULL && count == sizeof(EXTLOGPEN))
283 284
        {
            EXTLOGPEN *elp = buffer;
285
            *elp = pen->logpen;
286
            elp->elpWidth = 0;
287
            ret = sizeof(EXTLOGPEN);
288
        }
289 290 291 292 293 294 295 296 297 298
        else
        {
            lp = buffer;
            lp->lopnStyle = pen->logpen.elpPenStyle;
            lp->lopnColor = pen->logpen.elpColor;
            lp->lopnWidth.x = pen->logpen.elpWidth;
            lp->lopnWidth.y = 0;
            ret = sizeof(LOGPEN);
        }
        break;
299
    }
300

301
    case OBJ_EXTPEN:
302 303 304 305 306 307
        ret = sizeof(EXTLOGPEN) + pen->logpen.elpNumEntries * sizeof(DWORD) - sizeof(pen->logpen.elpStyleEntry);
        if (buffer)
        {
            if (count < ret) ret = 0;
            else memcpy(buffer, &pen->logpen, ret);
        }
308 309
        break;
    }
310 311
    GDI_ReleaseObj( handle );
    return ret;
Alexandre Julliard's avatar
Alexandre Julliard committed
312
}