windowscodecs: Check for empty rect in WriteSource_Proxy.
[wine/multimedia.git] / dlls / gdi32 / pen.c
blob2ae2f2b3129b047ad34ce2f349d36499a9dad6b3
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 struct brush_pattern pattern;
40 EXTLOGPEN logpen;
41 } PENOBJ;
44 static HGDIOBJ PEN_SelectObject( HGDIOBJ handle, HDC hdc );
45 static INT PEN_GetObject( HGDIOBJ handle, INT count, LPVOID buffer );
46 static BOOL PEN_DeleteObject( HGDIOBJ handle );
48 static const struct gdi_obj_funcs pen_funcs =
50 PEN_SelectObject, /* pSelectObject */
51 PEN_GetObject, /* pGetObjectA */
52 PEN_GetObject, /* pGetObjectW */
53 NULL, /* pUnrealizeObject */
54 PEN_DeleteObject /* pDeleteObject */
58 /***********************************************************************
59 * CreatePen (GDI32.@)
61 HPEN WINAPI CreatePen( INT style, INT width, COLORREF color )
63 LOGPEN logpen;
65 TRACE("%d %d %06x\n", style, width, color );
67 logpen.lopnStyle = style;
68 logpen.lopnWidth.x = width;
69 logpen.lopnWidth.y = 0;
70 logpen.lopnColor = color;
72 return CreatePenIndirect( &logpen );
76 /***********************************************************************
77 * CreatePenIndirect (GDI32.@)
79 HPEN WINAPI CreatePenIndirect( const LOGPEN * pen )
81 PENOBJ * penPtr;
82 HPEN hpen;
84 if (pen->lopnStyle == PS_NULL)
86 hpen = GetStockObject(NULL_PEN);
87 if (hpen) return hpen;
90 if (!(penPtr = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*penPtr) ))) return 0;
92 penPtr->logpen.elpPenStyle = pen->lopnStyle;
93 penPtr->logpen.elpWidth = abs(pen->lopnWidth.x);
94 penPtr->logpen.elpColor = pen->lopnColor;
95 penPtr->logpen.elpBrushStyle = BS_SOLID;
97 switch (pen->lopnStyle)
99 case PS_SOLID:
100 case PS_DASH:
101 case PS_DOT:
102 case PS_DASHDOT:
103 case PS_DASHDOTDOT:
104 case PS_INSIDEFRAME:
105 break;
106 case PS_NULL:
107 penPtr->logpen.elpWidth = 1;
108 penPtr->logpen.elpColor = 0;
109 break;
110 default:
111 penPtr->logpen.elpPenStyle = PS_SOLID;
112 break;
115 if (!(hpen = alloc_gdi_handle( penPtr, OBJ_PEN, &pen_funcs )))
116 HeapFree( GetProcessHeap(), 0, penPtr );
117 return hpen;
120 /***********************************************************************
121 * ExtCreatePen (GDI32.@)
124 HPEN WINAPI ExtCreatePen( DWORD style, DWORD width,
125 const LOGBRUSH * brush, DWORD style_count,
126 const DWORD *style_bits )
128 PENOBJ *penPtr = NULL;
129 HPEN hpen;
130 LOGBRUSH logbrush;
132 if ((style_count || style_bits) && (style & PS_STYLE_MASK) != PS_USERSTYLE)
133 goto invalid;
135 switch (style & PS_STYLE_MASK)
137 case PS_NULL:
138 return CreatePen( PS_NULL, 0, brush->lbColor );
140 case PS_SOLID:
141 case PS_DASH:
142 case PS_DOT:
143 case PS_DASHDOT:
144 case PS_DASHDOTDOT:
145 break;
147 case PS_USERSTYLE:
148 if (((INT)style_count) <= 0) return 0;
150 if ((style_count > 16) || !style_bits) goto invalid;
152 if ((style & PS_TYPE_MASK) == PS_GEOMETRIC)
154 UINT i;
155 BOOL has_neg = FALSE, all_zero = TRUE;
157 for(i = 0; (i < style_count) && !has_neg; i++)
159 has_neg = has_neg || (((INT)(style_bits[i])) < 0);
160 all_zero = all_zero && (style_bits[i] == 0);
163 if (all_zero || has_neg) goto invalid;
165 break;
167 case PS_INSIDEFRAME: /* applicable only for geometric pens */
168 if ((style & PS_TYPE_MASK) != PS_GEOMETRIC) goto invalid;
169 break;
171 case PS_ALTERNATE: /* applicable only for cosmetic pens */
172 if ((style & PS_TYPE_MASK) == PS_GEOMETRIC) goto invalid;
173 break;
175 default:
176 SetLastError(ERROR_INVALID_PARAMETER);
177 return 0;
180 if ((style & PS_TYPE_MASK) == PS_GEOMETRIC)
182 if (brush->lbStyle == BS_NULL) return CreatePen( PS_NULL, 0, 0 );
184 else
186 if (width != 1) goto invalid;
187 if (brush->lbStyle != BS_SOLID) goto invalid;
190 if (!(penPtr = HeapAlloc(GetProcessHeap(), 0, FIELD_OFFSET(PENOBJ,logpen.elpStyleEntry[style_count]))))
191 return 0;
193 logbrush = *brush;
194 if (!store_brush_pattern( &logbrush, &penPtr->pattern )) goto invalid;
195 if (logbrush.lbStyle == BS_DIBPATTERN) logbrush.lbStyle = BS_DIBPATTERNPT;
197 penPtr->logpen.elpPenStyle = style;
198 penPtr->logpen.elpWidth = abs(width);
199 penPtr->logpen.elpBrushStyle = logbrush.lbStyle;
200 penPtr->logpen.elpColor = logbrush.lbColor;
201 penPtr->logpen.elpHatch = brush->lbHatch;
202 penPtr->logpen.elpNumEntries = style_count;
203 memcpy(penPtr->logpen.elpStyleEntry, style_bits, style_count * sizeof(DWORD));
205 if (!(hpen = alloc_gdi_handle( penPtr, OBJ_EXTPEN, &pen_funcs )))
207 free_brush_pattern( &penPtr->pattern );
208 HeapFree( GetProcessHeap(), 0, penPtr );
210 return hpen;
212 invalid:
213 HeapFree( GetProcessHeap(), 0, penPtr );
214 SetLastError( ERROR_INVALID_PARAMETER );
215 return 0;
218 /***********************************************************************
219 * PEN_SelectObject
221 static HGDIOBJ PEN_SelectObject( HGDIOBJ handle, HDC hdc )
223 PENOBJ *pen;
224 HGDIOBJ ret = 0;
225 DC *dc = get_dc_ptr( hdc );
227 if (!dc)
229 SetLastError( ERROR_INVALID_HANDLE );
230 return 0;
233 if ((pen = GDI_GetObjPtr( handle, 0 )))
235 struct brush_pattern *pattern;
236 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pSelectPen );
238 switch (GetObjectType( handle ))
240 case OBJ_PEN:
241 pattern = NULL;
242 break;
243 case OBJ_EXTPEN:
244 pattern = &pen->pattern;
245 if (!pattern->info) pattern = NULL;
246 break;
247 default:
248 GDI_ReleaseObj( handle );
249 release_dc_ptr( dc );
250 return 0;
253 GDI_inc_ref_count( handle );
254 GDI_ReleaseObj( handle );
256 if (!physdev->funcs->pSelectPen( physdev, handle, pattern ))
258 GDI_dec_ref_count( handle );
260 else
262 ret = dc->hPen;
263 dc->hPen = handle;
264 GDI_dec_ref_count( ret );
267 release_dc_ptr( dc );
268 return ret;
272 /***********************************************************************
273 * PEN_DeleteObject
275 static BOOL PEN_DeleteObject( HGDIOBJ handle )
277 PENOBJ *pen = free_gdi_handle( handle );
279 if (!pen) return FALSE;
280 free_brush_pattern( &pen->pattern );
281 return HeapFree( GetProcessHeap(), 0, pen );
285 /***********************************************************************
286 * PEN_GetObject
288 static INT PEN_GetObject( HGDIOBJ handle, INT count, LPVOID buffer )
290 PENOBJ *pen = GDI_GetObjPtr( handle, 0 );
291 INT ret = 0;
293 if (!pen) return 0;
295 switch (GetObjectType( handle ))
297 case OBJ_PEN:
299 LOGPEN *lp;
301 if (!buffer) ret = sizeof(LOGPEN);
302 else if (count < sizeof(LOGPEN)) ret = 0;
303 else if ((pen->logpen.elpPenStyle & PS_STYLE_MASK) == PS_NULL && count == sizeof(EXTLOGPEN))
305 EXTLOGPEN *elp = buffer;
306 *elp = pen->logpen;
307 elp->elpWidth = 0;
308 ret = sizeof(EXTLOGPEN);
310 else
312 lp = buffer;
313 lp->lopnStyle = pen->logpen.elpPenStyle;
314 lp->lopnColor = pen->logpen.elpColor;
315 lp->lopnWidth.x = pen->logpen.elpWidth;
316 lp->lopnWidth.y = 0;
317 ret = sizeof(LOGPEN);
319 break;
322 case OBJ_EXTPEN:
323 ret = sizeof(EXTLOGPEN) + pen->logpen.elpNumEntries * sizeof(DWORD) - sizeof(pen->logpen.elpStyleEntry);
324 if (buffer)
326 if (count < ret) ret = 0;
327 else memcpy(buffer, &pen->logpen, ret);
329 break;
331 GDI_ReleaseObj( handle );
332 return ret;