pen.c 7.63 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 31
#include "wingdi.h"
#include "wine/wingdi16.h"
32
#include "gdi.h"
33
#include "gdi_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
    GDIOBJHDR header;
    EXTLOGPEN logpen;
43 44
} PENOBJ;

Alexandre Julliard's avatar
Alexandre Julliard committed
45

46 47 48 49 50 51 52 53 54 55 56 57 58
static HGDIOBJ PEN_SelectObject( HGDIOBJ handle, void *obj, HDC hdc );
static INT PEN_GetObject16( HGDIOBJ handle, void *obj, INT count, LPVOID buffer );
static INT PEN_GetObject( HGDIOBJ handle, void *obj, INT count, LPVOID buffer );

static const struct gdi_obj_funcs pen_funcs =
{
    PEN_SelectObject,  /* pSelectObject */
    PEN_GetObject16,   /* pGetObject16 */
    PEN_GetObject,     /* pGetObjectA */
    PEN_GetObject,     /* pGetObjectW */
    NULL,              /* pUnrealizeObject */
    GDI_FreeObject     /* pDeleteObject */
};
Alexandre Julliard's avatar
Alexandre Julliard committed
59 60


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

68
    TRACE("%d %d %06lx\n", style, width, color );
69

70
    logpen.lopnStyle = style;
71 72 73 74
    logpen.lopnWidth.x = width;
    logpen.lopnWidth.y = 0;
    logpen.lopnColor = color;

75
    return CreatePenIndirect( &logpen );
Alexandre Julliard's avatar
Alexandre Julliard committed
76 77 78 79
}


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

Michael Stefaniuc's avatar
Michael Stefaniuc committed
87 88
    if (!(penPtr = GDI_AllocObject( sizeof(PENOBJ), PEN_MAGIC, (HGDIOBJ *)&hpen,
				    &pen_funcs ))) return 0;
89
    if (pen->lopnStyle == PS_USERSTYLE || pen->lopnStyle == PS_ALTERNATE)
90
        penPtr->logpen.elpPenStyle = PS_SOLID;
91
    else
92
        penPtr->logpen.elpPenStyle = pen->lopnStyle;
93 94
    if (pen->lopnStyle == PS_NULL)
    {
95 96
        penPtr->logpen.elpWidth = 1;
        penPtr->logpen.elpColor = RGB(0, 0, 0);
97 98 99
    }
    else
    {
100 101
        penPtr->logpen.elpWidth = abs(pen->lopnWidth.x);
        penPtr->logpen.elpColor = pen->lopnColor;
102
    }
103 104 105 106 107
    penPtr->logpen.elpBrushStyle = BS_SOLID;
    penPtr->logpen.elpHatch = 0;
    penPtr->logpen.elpNumEntries = 0;
    penPtr->logpen.elpStyleEntry[0] = 0;

108
    GDI_ReleaseObj( hpen );
Alexandre Julliard's avatar
Alexandre Julliard committed
109 110 111
    return hpen;
}

Alexandre Julliard's avatar
Alexandre Julliard committed
112
/***********************************************************************
113
 *           ExtCreatePen    (GDI32.@)
Alexandre Julliard's avatar
Alexandre Julliard committed
114 115 116 117
 *
 * FIXME: PS_USERSTYLE not handled
 */

118 119
HPEN WINAPI ExtCreatePen( DWORD style, DWORD width,
                              const LOGBRUSH * brush, DWORD style_count,
Alexandre Julliard's avatar
Alexandre Julliard committed
120 121
                              const DWORD *style_bits )
{
122 123
    PENOBJ * penPtr;
    HPEN hpen;
Alexandre Julliard's avatar
Alexandre Julliard committed
124 125

    if ((style & PS_STYLE_MASK) == PS_USERSTYLE)
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
    {
        if (!style_count || !style_bits)
        {
            SetLastError(ERROR_INVALID_PARAMETER);
            return 0;
        }
        /* FIXME: PS_USERSTYLE workaround */
        FIXME("PS_USERSTYLE not handled\n");
        style = (style & ~PS_STYLE_MASK) | PS_SOLID;
    }
    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
148
    if ((style & PS_TYPE_MASK) == PS_GEOMETRIC)
149 150 151 152 153 154 155 156
    {
        /* PS_ALTERNATE is applicable only for cosmetic pens */
        if ((style & PS_STYLE_MASK) == PS_ALTERNATE)
        {
            SetLastError(ERROR_INVALID_PARAMETER);
            return 0;
        }

157 158 159 160 161
        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");
        }
162 163 164 165 166 167 168 169 170 171
    }
    else
    {
        /* PS_INSIDEFRAME is applicable only for gemetric pens */
        if ((style & PS_STYLE_MASK) == PS_INSIDEFRAME || width != 1)
        {
            SetLastError(ERROR_INVALID_PARAMETER);
            return 0;
        }
    }
Alexandre Julliard's avatar
Alexandre Julliard committed
172

173 174 175
    if (!(penPtr = GDI_AllocObject( sizeof(PENOBJ) +
                                    style_count * sizeof(DWORD) - sizeof(penPtr->logpen.elpStyleEntry),
                                    EXT_PEN_MAGIC, (HGDIOBJ *)&hpen,
Michael Stefaniuc's avatar
Michael Stefaniuc committed
176
				    &pen_funcs ))) return 0;
177

178 179 180 181 182 183 184 185
    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));
    
186
    GDI_ReleaseObj( hpen );
187 188

    return hpen;
Alexandre Julliard's avatar
Alexandre Julliard committed
189
}
Alexandre Julliard's avatar
Alexandre Julliard committed
190

191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209

/***********************************************************************
 *           PEN_SelectObject
 */
static HGDIOBJ PEN_SelectObject( HGDIOBJ handle, void *obj, HDC hdc )
{
    HGDIOBJ ret;
    DC *dc = DC_GetDCPtr( hdc );

    if (!dc) return 0;
    ret = dc->hPen;
    if (dc->funcs->pSelectPen) handle = dc->funcs->pSelectPen( dc->physDev, handle );
    if (handle) dc->hPen = handle;
    else ret = 0;
    GDI_ReleaseObj( hdc );
    return ret;
}


Alexandre Julliard's avatar
Alexandre Julliard committed
210 211 212
/***********************************************************************
 *           PEN_GetObject16
 */
213
static INT PEN_GetObject16( HGDIOBJ handle, void *obj, INT count, LPVOID buffer )
Alexandre Julliard's avatar
Alexandre Julliard committed
214
{
215
    PENOBJ *pen = obj;
216 217 218 219 220 221 222 223 224 225 226 227 228
    LOGPEN16 *logpen;

    if (!buffer) return sizeof(LOGPEN16);

    if (count < sizeof(LOGPEN16)) return 0;

    logpen = buffer;
    logpen->lopnStyle = pen->logpen.elpPenStyle;
    logpen->lopnColor = pen->logpen.elpColor;
    logpen->lopnWidth.x = pen->logpen.elpWidth;
    logpen->lopnWidth.y = 0;

    return sizeof(LOGPEN16);
Alexandre Julliard's avatar
Alexandre Julliard committed
229 230 231 232
}


/***********************************************************************
233
 *           PEN_GetObject
Alexandre Julliard's avatar
Alexandre Julliard committed
234
 */
235
static INT PEN_GetObject( HGDIOBJ handle, void *obj, INT count, LPVOID buffer )
Alexandre Julliard's avatar
Alexandre Julliard committed
236
{
237 238
    PENOBJ *pen = obj;

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
    switch (GDIMAGIC(pen->header.wMagic))
    {
    case PEN_MAGIC:
    {
        LOGPEN *lp;

        if (!buffer) return sizeof(LOGPEN);

        if (count < sizeof(LOGPEN)) return 0;

        if ((pen->logpen.elpPenStyle & PS_STYLE_MASK) == PS_NULL &&
            count == sizeof(EXTLOGPEN))
        {
            EXTLOGPEN *elp = buffer;
            memcpy(elp, &pen->logpen, sizeof(EXTLOGPEN));
            elp->elpWidth = 0;
            return sizeof(EXTLOGPEN);
        }

        lp = buffer;
        lp->lopnStyle = pen->logpen.elpPenStyle;
        lp->lopnColor = pen->logpen.elpColor;
        lp->lopnWidth.x = pen->logpen.elpWidth;
        lp->lopnWidth.y = 0;
        return sizeof(LOGPEN);
    }
265

266
    case EXT_PEN_MAGIC:
267
    {
268
        INT size = sizeof(EXTLOGPEN) + pen->logpen.elpNumEntries * sizeof(DWORD) - sizeof(pen->logpen.elpStyleEntry);
269

270 271 272 273 274 275
        if (!buffer) return size;

        if (count < size) return 0;
        memcpy(buffer, &pen->logpen, size);
        return size;
    }
276 277 278 279

    default:
        break;
    }
280
    assert(0);
281
    return 0;
Alexandre Julliard's avatar
Alexandre Julliard committed
282
}