rasapi32: Update spec file.
[wine/zf.git] / dlls / gdi32 / pen.c
blobfbac882a9c7d25ad3b7310ff8f671d356a0dfaa2
1 /*
2 * GDI pen objects
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
21 #include <stdarg.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <assert.h>
26 #include "windef.h"
27 #include "winbase.h"
28 #include "wingdi.h"
29 #include "gdi_private.h"
30 #include "wine/debug.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(gdi);
34 /* GDI logical pen object */
35 typedef struct
37 struct brush_pattern pattern;
38 EXTLOGPEN logpen;
39 } PENOBJ;
42 static HGDIOBJ PEN_SelectObject( HGDIOBJ handle, HDC hdc );
43 static INT PEN_GetObject( HGDIOBJ handle, INT count, LPVOID buffer );
44 static BOOL PEN_DeleteObject( HGDIOBJ handle );
46 static const struct gdi_obj_funcs pen_funcs =
48 PEN_SelectObject, /* pSelectObject */
49 PEN_GetObject, /* pGetObjectA */
50 PEN_GetObject, /* pGetObjectW */
51 NULL, /* pUnrealizeObject */
52 PEN_DeleteObject /* pDeleteObject */
56 /***********************************************************************
57 * CreatePen (GDI32.@)
59 HPEN WINAPI CreatePen( INT style, INT width, COLORREF color )
61 LOGPEN logpen;
63 TRACE("%d %d %06x\n", style, width, color );
65 logpen.lopnStyle = style;
66 logpen.lopnWidth.x = width;
67 logpen.lopnWidth.y = 0;
68 logpen.lopnColor = color;
70 return CreatePenIndirect( &logpen );
74 /***********************************************************************
75 * CreatePenIndirect (GDI32.@)
77 HPEN WINAPI CreatePenIndirect( const LOGPEN * pen )
79 PENOBJ * penPtr;
80 HPEN hpen;
82 if (pen->lopnStyle == PS_NULL)
84 hpen = GetStockObject(NULL_PEN);
85 if (hpen) return hpen;
88 if (!(penPtr = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*penPtr) ))) return 0;
90 penPtr->logpen.elpPenStyle = pen->lopnStyle;
91 penPtr->logpen.elpWidth = abs(pen->lopnWidth.x);
92 penPtr->logpen.elpColor = pen->lopnColor;
93 penPtr->logpen.elpBrushStyle = BS_SOLID;
95 switch (pen->lopnStyle)
97 case PS_SOLID:
98 case PS_DASH:
99 case PS_DOT:
100 case PS_DASHDOT:
101 case PS_DASHDOTDOT:
102 case PS_INSIDEFRAME:
103 break;
104 case PS_NULL:
105 penPtr->logpen.elpWidth = 1;
106 penPtr->logpen.elpColor = 0;
107 break;
108 default:
109 penPtr->logpen.elpPenStyle = PS_SOLID;
110 break;
113 if (!(hpen = alloc_gdi_handle( penPtr, OBJ_PEN, &pen_funcs )))
114 HeapFree( GetProcessHeap(), 0, penPtr );
115 return hpen;
118 /***********************************************************************
119 * ExtCreatePen (GDI32.@)
122 HPEN WINAPI ExtCreatePen( DWORD style, DWORD width,
123 const LOGBRUSH * brush, DWORD style_count,
124 const DWORD *style_bits )
126 PENOBJ *penPtr = NULL;
127 HPEN hpen;
128 LOGBRUSH logbrush;
130 if ((style_count || style_bits) && (style & PS_STYLE_MASK) != PS_USERSTYLE)
131 goto invalid;
133 switch (style & PS_STYLE_MASK)
135 case PS_NULL:
136 return CreatePen( PS_NULL, 0, brush->lbColor );
138 case PS_SOLID:
139 case PS_DASH:
140 case PS_DOT:
141 case PS_DASHDOT:
142 case PS_DASHDOTDOT:
143 break;
145 case PS_USERSTYLE:
146 if (((INT)style_count) <= 0) return 0;
148 if ((style_count > 16) || !style_bits) goto invalid;
150 if ((style & PS_TYPE_MASK) == PS_GEOMETRIC)
152 UINT i;
153 BOOL has_neg = FALSE, all_zero = TRUE;
155 for(i = 0; (i < style_count) && !has_neg; i++)
157 has_neg = has_neg || (((INT)(style_bits[i])) < 0);
158 all_zero = all_zero && (style_bits[i] == 0);
161 if (all_zero || has_neg) goto invalid;
163 break;
165 case PS_INSIDEFRAME: /* applicable only for geometric pens */
166 if ((style & PS_TYPE_MASK) != PS_GEOMETRIC) goto invalid;
167 break;
169 case PS_ALTERNATE: /* applicable only for cosmetic pens */
170 if ((style & PS_TYPE_MASK) == PS_GEOMETRIC) goto invalid;
171 break;
173 default:
174 SetLastError(ERROR_INVALID_PARAMETER);
175 return 0;
178 if ((style & PS_TYPE_MASK) == PS_GEOMETRIC)
180 if (brush->lbStyle == BS_NULL) return CreatePen( PS_NULL, 0, 0 );
182 else
184 if (width != 1) goto invalid;
185 if (brush->lbStyle != BS_SOLID) goto invalid;
188 if (!(penPtr = HeapAlloc(GetProcessHeap(), 0, FIELD_OFFSET(PENOBJ,logpen.elpStyleEntry[style_count]))))
189 return 0;
191 logbrush = *brush;
192 if (!store_brush_pattern( &logbrush, &penPtr->pattern )) goto invalid;
193 if (logbrush.lbStyle == BS_DIBPATTERN) logbrush.lbStyle = BS_DIBPATTERNPT;
195 penPtr->logpen.elpPenStyle = style;
196 penPtr->logpen.elpWidth = abs((int)width);
197 penPtr->logpen.elpBrushStyle = logbrush.lbStyle;
198 penPtr->logpen.elpColor = logbrush.lbColor;
199 penPtr->logpen.elpHatch = brush->lbHatch;
200 penPtr->logpen.elpNumEntries = style_count;
201 memcpy(penPtr->logpen.elpStyleEntry, style_bits, style_count * sizeof(DWORD));
203 if (!(hpen = alloc_gdi_handle( penPtr, OBJ_EXTPEN, &pen_funcs )))
205 free_brush_pattern( &penPtr->pattern );
206 HeapFree( GetProcessHeap(), 0, penPtr );
208 return hpen;
210 invalid:
211 HeapFree( GetProcessHeap(), 0, penPtr );
212 SetLastError( ERROR_INVALID_PARAMETER );
213 return 0;
216 /***********************************************************************
217 * PEN_SelectObject
219 static HGDIOBJ PEN_SelectObject( HGDIOBJ handle, HDC hdc )
221 PENOBJ *pen;
222 HGDIOBJ ret = 0;
223 DC *dc = get_dc_ptr( hdc );
224 WORD type;
226 if (!dc)
228 SetLastError( ERROR_INVALID_HANDLE );
229 return 0;
232 if ((pen = get_any_obj_ptr( handle, &type )))
234 struct brush_pattern *pattern;
235 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pSelectPen );
237 switch (type)
239 case OBJ_PEN:
240 pattern = NULL;
241 break;
242 case OBJ_EXTPEN:
243 pattern = &pen->pattern;
244 if (!pattern->info) pattern = NULL;
245 break;
246 default:
247 GDI_ReleaseObj( handle );
248 release_dc_ptr( dc );
249 return 0;
252 GDI_inc_ref_count( handle );
253 GDI_ReleaseObj( handle );
255 if (!physdev->funcs->pSelectPen( physdev, handle, pattern ))
257 GDI_dec_ref_count( handle );
259 else
261 ret = dc->hPen;
262 dc->hPen = handle;
263 GDI_dec_ref_count( ret );
266 release_dc_ptr( dc );
267 return ret;
271 /***********************************************************************
272 * PEN_DeleteObject
274 static BOOL PEN_DeleteObject( HGDIOBJ handle )
276 PENOBJ *pen = free_gdi_handle( handle );
278 if (!pen) return FALSE;
279 free_brush_pattern( &pen->pattern );
280 HeapFree( GetProcessHeap(), 0, pen );
281 return TRUE;
285 /***********************************************************************
286 * PEN_GetObject
288 static INT PEN_GetObject( HGDIOBJ handle, INT count, LPVOID buffer )
290 WORD type;
291 PENOBJ *pen = get_any_obj_ptr( handle, &type );
292 INT ret = 0;
294 if (!pen) return 0;
296 switch (type)
298 case OBJ_PEN:
300 LOGPEN *lp;
302 if (!buffer) ret = sizeof(LOGPEN);
303 else if (count < sizeof(LOGPEN)) ret = 0;
304 else if ((pen->logpen.elpPenStyle & PS_STYLE_MASK) == PS_NULL && count == sizeof(EXTLOGPEN))
306 EXTLOGPEN *elp = buffer;
307 *elp = pen->logpen;
308 elp->elpWidth = 0;
309 ret = sizeof(EXTLOGPEN);
311 else
313 lp = buffer;
314 lp->lopnStyle = pen->logpen.elpPenStyle;
315 lp->lopnColor = pen->logpen.elpColor;
316 lp->lopnWidth.x = pen->logpen.elpWidth;
317 lp->lopnWidth.y = 0;
318 ret = sizeof(LOGPEN);
320 break;
323 case OBJ_EXTPEN:
324 ret = sizeof(EXTLOGPEN) + pen->logpen.elpNumEntries * sizeof(DWORD) - sizeof(pen->logpen.elpStyleEntry);
325 if (buffer)
327 if (count < ret) ret = 0;
328 else memcpy(buffer, &pen->logpen, ret);
330 break;
332 GDI_ReleaseObj( handle );
333 return ret;