pen.c 8.35 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 23 24
#if 0
#pragma makedep unix
#endif

25
#include <stdarg.h>
26
#include <stdlib.h>
27
#include <string.h>
28
#include <assert.h>
29 30

#include "windef.h"
31
#include "winbase.h"
32
#include "wingdi.h"
33
#include "ntgdi_private.h"
34
#include "wine/debug.h"
Alexandre Julliard's avatar
Alexandre Julliard committed
35

36
WINE_DEFAULT_DEBUG_CHANNEL(gdi);
37

38 39 40
  /* GDI logical pen object */
typedef struct
{
41 42 43
    struct gdi_obj_header obj;
    struct brush_pattern  pattern;
    EXTLOGPEN             logpen;
44 45
} PENOBJ;

Alexandre Julliard's avatar
Alexandre Julliard committed
46

47
static INT PEN_GetObject( HGDIOBJ handle, INT count, LPVOID buffer );
48
static BOOL PEN_DeleteObject( HGDIOBJ handle );
49 50 51 52 53

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

57
HPEN create_pen( INT style, INT width, COLORREF color )
Alexandre Julliard's avatar
Alexandre Julliard committed
58
{
59
    PENOBJ *penPtr;
60
    HPEN hpen;
Alexandre Julliard's avatar
Alexandre Julliard committed
61

62
    TRACE( "%d %d %06x\n", style, width, color );
63

64
    switch (style)
65 66 67 68 69 70 71 72 73
    {
    case PS_SOLID:
    case PS_DASH:
    case PS_DOT:
    case PS_DASHDOT:
    case PS_DASHDOTDOT:
    case PS_INSIDEFRAME:
        break;
    case PS_NULL:
74 75
        width = 1;
        color = 0;
76 77
        break;
    default:
78
        return 0;
79 80
    }

81
    if (!(penPtr = calloc( 1, sizeof(*penPtr) ))) return 0;
82 83 84 85 86 87

    penPtr->logpen.elpPenStyle   = style;
    penPtr->logpen.elpWidth      = abs(width);
    penPtr->logpen.elpColor      = color;
    penPtr->logpen.elpBrushStyle = BS_SOLID;

88
    if (!(hpen = alloc_gdi_handle( &penPtr->obj, NTGDI_OBJ_PEN, &pen_funcs )))
89
        free( penPtr );
Alexandre Julliard's avatar
Alexandre Julliard committed
90 91 92
    return hpen;
}

93 94 95 96 97 98
/***********************************************************************
 *           NtGdiCreatePen    (win32u.@)
 */
HPEN WINAPI NtGdiCreatePen( INT style, INT width, COLORREF color, HBRUSH brush )
{
    if (brush) FIXME( "brush not supported\n" );
99
    if (style == PS_NULL) return get_stock_object( NULL_PEN );
100 101 102
    return create_pen( style, width, color );
}

Alexandre Julliard's avatar
Alexandre Julliard committed
103
/***********************************************************************
104
 *           NtGdiExtCreatePen    (win32u.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
105
 */
106 107 108 109
HPEN WINAPI NtGdiExtCreatePen( DWORD style, DWORD width, ULONG brush_style, ULONG color,
                               ULONG_PTR client_hatch, ULONG_PTR hatch, DWORD style_count,
                               const DWORD *style_bits, ULONG dib_size, BOOL old_style,
                               HBRUSH brush )
Alexandre Julliard's avatar
Alexandre Julliard committed
110
{
111
    PENOBJ *penPtr = NULL;
112
    HPEN hpen;
113
    LOGBRUSH logbrush;
Alexandre Julliard's avatar
Alexandre Julliard committed
114

115
    if ((style_count || style_bits) && (style & PS_STYLE_MASK) != PS_USERSTYLE)
116
        goto invalid;
117 118 119 120

    switch (style & PS_STYLE_MASK)
    {
    case PS_NULL:
121
        return NtGdiCreatePen( PS_NULL, 0, color, NULL );
122 123 124 125 126 127 128 129 130 131

    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;
132

133
        if ((style_count > 16) || !style_bits) goto invalid;
134

135
        if ((style & PS_TYPE_MASK) == PS_GEOMETRIC)
136 137 138 139 140 141 142 143 144 145
        {
            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);
            }

146
            if (all_zero || has_neg) goto invalid;
147
        }
148 149 150
        break;

    case PS_INSIDEFRAME:  /* applicable only for geometric pens */
151
        if ((style & PS_TYPE_MASK) != PS_GEOMETRIC) goto invalid;
152
        break;
153

154
    case PS_ALTERNATE:  /* applicable only for cosmetic pens */
155
        if ((style & PS_TYPE_MASK) == PS_GEOMETRIC) goto invalid;
156
        break;
157

158 159 160 161 162 163 164
    default:
        SetLastError(ERROR_INVALID_PARAMETER);
        return 0;
    }

    if ((style & PS_TYPE_MASK) == PS_GEOMETRIC)
    {
165
        if (brush_style == BS_NULL) return NtGdiCreatePen( PS_NULL, 0, 0, NULL );
166 167 168
    }
    else
    {
169
        if (width != 1) goto invalid;
170
        if (brush_style != BS_SOLID) goto invalid;
171
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
172

173
    if (!(penPtr = malloc( FIELD_OFFSET(PENOBJ,logpen.elpStyleEntry[style_count]) )))
174
        return 0;
175

176 177 178
    logbrush.lbStyle = brush_style;
    logbrush.lbColor = color;
    logbrush.lbHatch = hatch;
179 180 181
    if (!store_brush_pattern( &logbrush, &penPtr->pattern )) goto invalid;
    if (logbrush.lbStyle == BS_DIBPATTERN) logbrush.lbStyle = BS_DIBPATTERNPT;

182
    penPtr->logpen.elpPenStyle = style;
183
    penPtr->logpen.elpWidth = abs((int)width);
184 185
    penPtr->logpen.elpBrushStyle = logbrush.lbStyle;
    penPtr->logpen.elpColor = logbrush.lbColor;
186
    penPtr->logpen.elpHatch = client_hatch;
187 188
    penPtr->logpen.elpNumEntries = style_count;
    memcpy(penPtr->logpen.elpStyleEntry, style_bits, style_count * sizeof(DWORD));
189

190
    if (!(hpen = alloc_gdi_handle( &penPtr->obj, NTGDI_OBJ_EXTPEN, &pen_funcs )))
191 192
    {
        free_brush_pattern( &penPtr->pattern );
193
        free( penPtr );
194
    }
195
    return hpen;
196 197

invalid:
198
    free( penPtr );
199 200
    SetLastError( ERROR_INVALID_PARAMETER );
    return 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
201
}
Alexandre Julliard's avatar
Alexandre Julliard committed
202

203
/***********************************************************************
204
 *           NtGdiSelectPen (win32u.@)
205
 */
206
HGDIOBJ WINAPI NtGdiSelectPen( HDC hdc, HGDIOBJ handle )
207
{
208
    PENOBJ *pen;
209
    HGDIOBJ ret = 0;
210
    DWORD type;
211
    DC *dc;
212

213
    if (!(dc = get_dc_ptr( hdc ))) return 0;
214

215
    if ((pen = get_any_obj_ptr( handle, &type )))
216
    {
217 218
        struct brush_pattern *pattern;
        PHYSDEV physdev = GET_DC_PHYSDEV( dc, pSelectPen );
219

220
        switch (type)
221
        {
222
        case NTGDI_OBJ_PEN:
223 224
            pattern = NULL;
            break;
225
        case NTGDI_OBJ_EXTPEN:
226
            pattern = &pen->pattern;
227
            if (!pattern->info) pattern = NULL;
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247
            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 );
        }
248
    }
249
    release_dc_ptr( dc );
250 251 252 253
    return ret;
}


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

    if (!pen) return FALSE;
262
    free_brush_pattern( &pen->pattern );
263
    free( pen );
264
    return TRUE;
265 266 267
}


Alexandre Julliard's avatar
Alexandre Julliard committed
268
/***********************************************************************
269
 *           PEN_GetObject
Alexandre Julliard's avatar
Alexandre Julliard committed
270
 */
271
static INT PEN_GetObject( HGDIOBJ handle, INT count, LPVOID buffer )
Alexandre Julliard's avatar
Alexandre Julliard committed
272
{
273
    DWORD type;
274
    PENOBJ *pen = get_any_obj_ptr( handle, &type );
275 276 277
    INT ret = 0;

    if (!pen) return 0;
278

279
    switch (type)
280
    {
281
    case NTGDI_OBJ_PEN:
282 283 284
    {
        LOGPEN *lp;

285 286 287
        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))
288 289
        {
            EXTLOGPEN *elp = buffer;
290
            *elp = pen->logpen;
291
            elp->elpWidth = 0;
292
            ret = sizeof(EXTLOGPEN);
293
        }
294 295 296 297 298 299 300 301 302 303
        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;
304
    }
305

306
    case NTGDI_OBJ_EXTPEN:
307 308 309 310 311 312
        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);
        }
313 314
        break;
    }
315 316
    GDI_ReleaseObj( handle );
    return ret;
Alexandre Julliard's avatar
Alexandre Julliard committed
317
}