4 * Copyright 1993 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * 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
31 #include "gdi_private.h"
32 #include "wine/debug.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(gdi);
36 /* GDI logical pen object */
40 struct brush_pattern pattern;
45 static HGDIOBJ PEN_SelectObject( HGDIOBJ handle, HDC hdc );
46 static INT PEN_GetObject( HGDIOBJ handle, INT count, LPVOID buffer );
47 static BOOL PEN_DeleteObject( HGDIOBJ handle );
49 static const struct gdi_obj_funcs pen_funcs =
51 PEN_SelectObject, /* pSelectObject */
52 PEN_GetObject, /* pGetObjectA */
53 PEN_GetObject, /* pGetObjectW */
54 NULL, /* pUnrealizeObject */
55 PEN_DeleteObject /* pDeleteObject */
59 /***********************************************************************
62 HPEN WINAPI CreatePen( INT style, INT width, COLORREF color )
66 TRACE("%d %d %06x\n", style, width, color );
68 logpen.lopnStyle = style;
69 logpen.lopnWidth.x = width;
70 logpen.lopnWidth.y = 0;
71 logpen.lopnColor = color;
73 return CreatePenIndirect( &logpen );
77 /***********************************************************************
78 * CreatePenIndirect (GDI32.@)
80 HPEN WINAPI CreatePenIndirect( const LOGPEN * pen )
85 if (pen->lopnStyle == PS_NULL)
87 hpen = GetStockObject(NULL_PEN);
88 if (hpen) return hpen;
91 if (!(penPtr = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*penPtr) ))) return 0;
93 penPtr->logpen.elpPenStyle = pen->lopnStyle;
94 penPtr->logpen.elpWidth = abs(pen->lopnWidth.x);
95 penPtr->logpen.elpColor = pen->lopnColor;
96 penPtr->logpen.elpBrushStyle = BS_SOLID;
98 switch (pen->lopnStyle)
108 penPtr->logpen.elpWidth = 1;
109 penPtr->logpen.elpColor = 0;
112 penPtr->logpen.elpPenStyle = PS_SOLID;
116 if (!(hpen = alloc_gdi_handle( &penPtr->header, OBJ_PEN, &pen_funcs )))
117 HeapFree( GetProcessHeap(), 0, penPtr );
121 /***********************************************************************
122 * ExtCreatePen (GDI32.@)
125 HPEN WINAPI ExtCreatePen( DWORD style, DWORD width,
126 const LOGBRUSH * brush, DWORD style_count,
127 const DWORD *style_bits )
129 PENOBJ *penPtr = NULL;
133 if ((style_count || style_bits) && (style & PS_STYLE_MASK) != PS_USERSTYLE)
136 switch (style & PS_STYLE_MASK)
139 return CreatePen( PS_NULL, 0, brush->lbColor );
149 if (((INT)style_count) <= 0) return 0;
151 if ((style_count > 16) || !style_bits) goto invalid;
153 if ((style & PS_TYPE_MASK) == PS_GEOMETRIC)
156 BOOL has_neg = FALSE, all_zero = TRUE;
158 for(i = 0; (i < style_count) && !has_neg; i++)
160 has_neg = has_neg || (((INT)(style_bits[i])) < 0);
161 all_zero = all_zero && (style_bits[i] == 0);
164 if (all_zero || has_neg) goto invalid;
168 case PS_INSIDEFRAME: /* applicable only for geometric pens */
169 if ((style & PS_TYPE_MASK) != PS_GEOMETRIC) goto invalid;
172 case PS_ALTERNATE: /* applicable only for cosmetic pens */
173 if ((style & PS_TYPE_MASK) == PS_GEOMETRIC) goto invalid;
177 SetLastError(ERROR_INVALID_PARAMETER);
181 if ((style & PS_TYPE_MASK) == PS_GEOMETRIC)
183 if (brush->lbStyle == BS_NULL) return CreatePen( PS_NULL, 0, 0 );
187 if (width != 1) goto invalid;
188 if (brush->lbStyle != BS_SOLID) goto invalid;
191 if (!(penPtr = HeapAlloc(GetProcessHeap(), 0, FIELD_OFFSET(PENOBJ,logpen.elpStyleEntry[style_count]))))
195 if (!store_brush_pattern( &logbrush, &penPtr->pattern )) goto invalid;
196 if (logbrush.lbStyle == BS_DIBPATTERN) logbrush.lbStyle = BS_DIBPATTERNPT;
198 penPtr->logpen.elpPenStyle = style;
199 penPtr->logpen.elpWidth = abs(width);
200 penPtr->logpen.elpBrushStyle = logbrush.lbStyle;
201 penPtr->logpen.elpColor = logbrush.lbColor;
202 penPtr->logpen.elpHatch = brush->lbHatch;
203 penPtr->logpen.elpNumEntries = style_count;
204 memcpy(penPtr->logpen.elpStyleEntry, style_bits, style_count * sizeof(DWORD));
206 if (!(hpen = alloc_gdi_handle( &penPtr->header, OBJ_EXTPEN, &pen_funcs )))
208 free_brush_pattern( &penPtr->pattern );
209 HeapFree( GetProcessHeap(), 0, penPtr );
214 HeapFree( GetProcessHeap(), 0, penPtr );
215 SetLastError( ERROR_INVALID_PARAMETER );
219 /***********************************************************************
222 static HGDIOBJ PEN_SelectObject( HGDIOBJ handle, HDC hdc )
226 DC *dc = get_dc_ptr( hdc );
230 SetLastError( ERROR_INVALID_HANDLE );
234 if ((pen = GDI_GetObjPtr( handle, 0 )))
236 struct brush_pattern *pattern;
237 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pSelectPen );
239 switch (pen->header.type)
245 pattern = &pen->pattern;
248 if (pattern->bitmap) cache_pattern_bits( physdev, pattern );
253 GDI_ReleaseObj( handle );
254 release_dc_ptr( dc );
258 GDI_inc_ref_count( handle );
259 GDI_ReleaseObj( handle );
261 if (!physdev->funcs->pSelectPen( physdev, handle, pattern ))
263 GDI_dec_ref_count( handle );
269 GDI_dec_ref_count( ret );
272 release_dc_ptr( dc );
277 /***********************************************************************
280 static BOOL PEN_DeleteObject( HGDIOBJ handle )
282 PENOBJ *pen = free_gdi_handle( handle );
284 if (!pen) return FALSE;
285 free_brush_pattern( &pen->pattern );
286 return HeapFree( GetProcessHeap(), 0, pen );
290 /***********************************************************************
293 static INT PEN_GetObject( HGDIOBJ handle, INT count, LPVOID buffer )
295 PENOBJ *pen = GDI_GetObjPtr( handle, 0 );
300 switch (pen->header.type)
306 if (!buffer) ret = sizeof(LOGPEN);
307 else if (count < sizeof(LOGPEN)) ret = 0;
308 else if ((pen->logpen.elpPenStyle & PS_STYLE_MASK) == PS_NULL && count == sizeof(EXTLOGPEN))
310 EXTLOGPEN *elp = buffer;
313 ret = sizeof(EXTLOGPEN);
318 lp->lopnStyle = pen->logpen.elpPenStyle;
319 lp->lopnColor = pen->logpen.elpColor;
320 lp->lopnWidth.x = pen->logpen.elpWidth;
322 ret = sizeof(LOGPEN);
328 ret = sizeof(EXTLOGPEN) + pen->logpen.elpNumEntries * sizeof(DWORD) - sizeof(pen->logpen.elpStyleEntry);
331 if (count < ret) ret = 0;
332 else memcpy(buffer, &pen->logpen, ret);
336 GDI_ReleaseObj( handle );