gdi32: Avoid redundant computation of the gradient bounding rectangle.
[wine/multimedia.git] / dlls / gdi32 / pen.c
blob1d04daef1d0edd66b9f4934d8699a22c2cc5c82d
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 "config.h"
23 #include <stdarg.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <assert.h>
28 #include "windef.h"
29 #include "winbase.h"
30 #include "wingdi.h"
31 #include "gdi_private.h"
32 #include "wine/debug.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(gdi);
36 /* GDI logical pen object */
37 typedef struct
39 GDIOBJHDR header;
40 struct brush_pattern pattern;
41 EXTLOGPEN logpen;
42 } PENOBJ;
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 /***********************************************************************
60 * CreatePen (GDI32.@)
62 HPEN WINAPI CreatePen( INT style, INT width, COLORREF color )
64 LOGPEN logpen;
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 )
82 PENOBJ * penPtr;
83 HPEN hpen;
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)
100 case PS_SOLID:
101 case PS_DASH:
102 case PS_DOT:
103 case PS_DASHDOT:
104 case PS_DASHDOTDOT:
105 case PS_INSIDEFRAME:
106 break;
107 case PS_NULL:
108 penPtr->logpen.elpWidth = 1;
109 penPtr->logpen.elpColor = 0;
110 break;
111 default:
112 penPtr->logpen.elpPenStyle = PS_SOLID;
113 break;
116 if (!(hpen = alloc_gdi_handle( &penPtr->header, OBJ_PEN, &pen_funcs )))
117 HeapFree( GetProcessHeap(), 0, penPtr );
118 return hpen;
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;
130 HPEN hpen;
131 LOGBRUSH logbrush;
133 if ((style_count || style_bits) && (style & PS_STYLE_MASK) != PS_USERSTYLE)
134 goto invalid;
136 switch (style & PS_STYLE_MASK)
138 case PS_NULL:
139 return CreatePen( PS_NULL, 0, brush->lbColor );
141 case PS_SOLID:
142 case PS_DASH:
143 case PS_DOT:
144 case PS_DASHDOT:
145 case PS_DASHDOTDOT:
146 break;
148 case PS_USERSTYLE:
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)
155 UINT i;
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;
166 break;
168 case PS_INSIDEFRAME: /* applicable only for geometric pens */
169 if ((style & PS_TYPE_MASK) != PS_GEOMETRIC) goto invalid;
170 break;
172 case PS_ALTERNATE: /* applicable only for cosmetic pens */
173 if ((style & PS_TYPE_MASK) == PS_GEOMETRIC) goto invalid;
174 break;
176 default:
177 SetLastError(ERROR_INVALID_PARAMETER);
178 return 0;
181 if ((style & PS_TYPE_MASK) == PS_GEOMETRIC)
183 if (brush->lbStyle == BS_NULL) return CreatePen( PS_NULL, 0, 0 );
185 else
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]))))
192 return 0;
194 logbrush = *brush;
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 );
211 return hpen;
213 invalid:
214 HeapFree( GetProcessHeap(), 0, penPtr );
215 SetLastError( ERROR_INVALID_PARAMETER );
216 return 0;
219 /***********************************************************************
220 * PEN_SelectObject
222 static HGDIOBJ PEN_SelectObject( HGDIOBJ handle, HDC hdc )
224 PENOBJ *pen;
225 HGDIOBJ ret = 0;
226 DC *dc = get_dc_ptr( hdc );
228 if (!dc)
230 SetLastError( ERROR_INVALID_HANDLE );
231 return 0;
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)
241 case OBJ_PEN:
242 pattern = NULL;
243 break;
244 case OBJ_EXTPEN:
245 pattern = &pen->pattern;
246 if (!pattern->info)
248 if (pattern->bitmap) cache_pattern_bits( physdev, pattern );
249 else pattern = NULL;
251 break;
252 default:
253 GDI_ReleaseObj( handle );
254 release_dc_ptr( dc );
255 return 0;
258 GDI_inc_ref_count( handle );
259 GDI_ReleaseObj( handle );
261 if (!physdev->funcs->pSelectPen( physdev, handle, pattern ))
263 GDI_dec_ref_count( handle );
265 else
267 ret = dc->hPen;
268 dc->hPen = handle;
269 GDI_dec_ref_count( ret );
272 release_dc_ptr( dc );
273 return ret;
277 /***********************************************************************
278 * PEN_DeleteObject
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 /***********************************************************************
291 * PEN_GetObject
293 static INT PEN_GetObject( HGDIOBJ handle, INT count, LPVOID buffer )
295 PENOBJ *pen = GDI_GetObjPtr( handle, 0 );
296 INT ret = 0;
298 if (!pen) return 0;
300 switch (pen->header.type)
302 case OBJ_PEN:
304 LOGPEN *lp;
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;
311 *elp = pen->logpen;
312 elp->elpWidth = 0;
313 ret = sizeof(EXTLOGPEN);
315 else
317 lp = buffer;
318 lp->lopnStyle = pen->logpen.elpPenStyle;
319 lp->lopnColor = pen->logpen.elpColor;
320 lp->lopnWidth.x = pen->logpen.elpWidth;
321 lp->lopnWidth.y = 0;
322 ret = sizeof(LOGPEN);
324 break;
327 case OBJ_EXTPEN:
328 ret = sizeof(EXTLOGPEN) + pen->logpen.elpNumEntries * sizeof(DWORD) - sizeof(pen->logpen.elpStyleEntry);
329 if (buffer)
331 if (count < ret) ret = 0;
332 else memcpy(buffer, &pen->logpen, ret);
334 break;
336 GDI_ReleaseObj( handle );
337 return ret;