pen.c 8.71 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
    struct brush_pattern pattern;
    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
    if (!(penPtr = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*penPtr) ))) return 0;
91

92 93 94
    penPtr->logpen.elpPenStyle = pen->lopnStyle;
    penPtr->logpen.elpWidth = abs(pen->lopnWidth.x);
    penPtr->logpen.elpColor = pen->lopnColor;
95 96
    penPtr->logpen.elpBrushStyle = BS_SOLID;

97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
    switch (pen->lopnStyle)
    {
    case PS_SOLID:
    case PS_DASH:
    case PS_DOT:
    case PS_DASHDOT:
    case PS_DASHDOTDOT:
    case PS_INSIDEFRAME:
        break;
    case PS_NULL:
        penPtr->logpen.elpWidth = 1;
        penPtr->logpen.elpColor = 0;
        break;
    default:
        penPtr->logpen.elpPenStyle = PS_SOLID;
        break;
    }

115
    if (!(hpen = alloc_gdi_handle( penPtr, OBJ_PEN, &pen_funcs )))
116
        HeapFree( GetProcessHeap(), 0, penPtr );
Alexandre Julliard's avatar
Alexandre Julliard committed
117 118 119
    return hpen;
}

Alexandre Julliard's avatar
Alexandre Julliard committed
120
/***********************************************************************
121
 *           ExtCreatePen    (GDI32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
122 123
 */

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

132
    if ((style_count || style_bits) && (style & PS_STYLE_MASK) != PS_USERSTYLE)
133
        goto invalid;
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148

    switch (style & PS_STYLE_MASK)
    {
    case PS_NULL:
        return CreatePen( PS_NULL, 0, brush->lbColor );

    case PS_SOLID:
    case PS_DASH:
    case PS_DOT:
    case PS_DASHDOT:
    case PS_DASHDOTDOT:
        break;

    case PS_USERSTYLE:
        if (((INT)style_count) <= 0) return 0;
149

150
        if ((style_count > 16) || !style_bits) goto invalid;
151

152
        if ((style & PS_TYPE_MASK) == PS_GEOMETRIC)
153 154 155 156 157 158 159 160 161 162
        {
            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);
            }

163
            if (all_zero || has_neg) goto invalid;
164
        }
165 166 167
        break;

    case PS_INSIDEFRAME:  /* applicable only for geometric pens */
168
        if ((style & PS_TYPE_MASK) != PS_GEOMETRIC) goto invalid;
169
        break;
170

171
    case PS_ALTERNATE:  /* applicable only for cosmetic pens */
172
        if ((style & PS_TYPE_MASK) == PS_GEOMETRIC) goto invalid;
173
        break;
174

175 176 177 178 179 180 181
    default:
        SetLastError(ERROR_INVALID_PARAMETER);
        return 0;
    }

    if ((style & PS_TYPE_MASK) == PS_GEOMETRIC)
    {
182
        if (brush->lbStyle == BS_NULL) return CreatePen( PS_NULL, 0, 0 );
183 184 185
    }
    else
    {
186 187
        if (width != 1) goto invalid;
        if (brush->lbStyle != BS_SOLID) goto invalid;
188
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
189

190 191
    if (!(penPtr = HeapAlloc(GetProcessHeap(), 0, FIELD_OFFSET(PENOBJ,logpen.elpStyleEntry[style_count]))))
        return 0;
192

193 194 195 196
    logbrush = *brush;
    if (!store_brush_pattern( &logbrush, &penPtr->pattern )) goto invalid;
    if (logbrush.lbStyle == BS_DIBPATTERN) logbrush.lbStyle = BS_DIBPATTERNPT;

197 198
    penPtr->logpen.elpPenStyle = style;
    penPtr->logpen.elpWidth = abs(width);
199 200
    penPtr->logpen.elpBrushStyle = logbrush.lbStyle;
    penPtr->logpen.elpColor = logbrush.lbColor;
201 202 203
    penPtr->logpen.elpHatch = brush->lbHatch;
    penPtr->logpen.elpNumEntries = style_count;
    memcpy(penPtr->logpen.elpStyleEntry, style_bits, style_count * sizeof(DWORD));
204

205
    if (!(hpen = alloc_gdi_handle( penPtr, OBJ_EXTPEN, &pen_funcs )))
206 207
    {
        free_brush_pattern( &penPtr->pattern );
208
        HeapFree( GetProcessHeap(), 0, penPtr );
209
    }
210
    return hpen;
211 212 213 214 215

invalid:
    HeapFree( GetProcessHeap(), 0, penPtr );
    SetLastError( ERROR_INVALID_PARAMETER );
    return 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
216
}
Alexandre Julliard's avatar
Alexandre Julliard committed
217

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

227 228 229 230 231 232
    if (!dc)
    {
        SetLastError( ERROR_INVALID_HANDLE );
        return 0;
    }

233
    if ((pen = GDI_GetObjPtr( handle, 0 )))
234
    {
235 236
        struct brush_pattern *pattern;
        PHYSDEV physdev = GET_DC_PHYSDEV( dc, pSelectPen );
237

238
        switch (GetObjectType( handle ))
239 240 241 242 243 244
        {
        case OBJ_PEN:
            pattern = NULL;
            break;
        case OBJ_EXTPEN:
            pattern = &pen->pattern;
245
            if (!pattern->info) pattern = NULL;
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
            break;
        default:
            GDI_ReleaseObj( handle );
            release_dc_ptr( dc );
            return 0;
        }

        GDI_inc_ref_count( handle );
        GDI_ReleaseObj( handle );

        if (!physdev->funcs->pSelectPen( physdev, handle, pattern ))
        {
            GDI_dec_ref_count( handle );
        }
        else
        {
            ret = dc->hPen;
            dc->hPen = handle;
            GDI_dec_ref_count( ret );
        }
266
    }
267
    release_dc_ptr( dc );
268 269 270 271
    return ret;
}


272 273 274 275 276
/***********************************************************************
 *           PEN_DeleteObject
 */
static BOOL PEN_DeleteObject( HGDIOBJ handle )
{
277
    PENOBJ *pen = free_gdi_handle( handle );
278 279

    if (!pen) return FALSE;
280
    free_brush_pattern( &pen->pattern );
281
    return HeapFree( GetProcessHeap(), 0, pen );
282 283 284
}


Alexandre Julliard's avatar
Alexandre Julliard committed
285
/***********************************************************************
286
 *           PEN_GetObject
Alexandre Julliard's avatar
Alexandre Julliard committed
287
 */
288
static INT PEN_GetObject( HGDIOBJ handle, INT count, LPVOID buffer )
Alexandre Julliard's avatar
Alexandre Julliard committed
289
{
290
    PENOBJ *pen = GDI_GetObjPtr( handle, 0 );
291 292 293
    INT ret = 0;

    if (!pen) return 0;
294

295
    switch (GetObjectType( handle ))
296
    {
297
    case OBJ_PEN:
298 299 300
    {
        LOGPEN *lp;

301 302 303
        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))
304 305
        {
            EXTLOGPEN *elp = buffer;
306
            *elp = pen->logpen;
307
            elp->elpWidth = 0;
308
            ret = sizeof(EXTLOGPEN);
309
        }
310 311 312 313 314 315 316 317 318 319
        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;
320
    }
321

322
    case OBJ_EXTPEN:
323 324 325 326 327 328
        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);
        }
329 330
        break;
    }
331 332
    GDI_ReleaseObj( handle );
    return ret;
Alexandre Julliard's avatar
Alexandre Julliard committed
333
}