gdi32: Add get_any_obj_ptr() to retrieve the ptr and type of a GDI handle.
[wine.git] / dlls / gdi32 / pen.c
blobd6c1dc45762e63598145204edf1ea318f2ef372d
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((int)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 );
226 WORD type;
228 if (!dc)
230 SetLastError( ERROR_INVALID_HANDLE );
231 return 0;
234 if ((pen = get_any_obj_ptr( handle, &type )))
236 struct brush_pattern *pattern;
237 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pSelectPen );
239 switch (type)
241 case OBJ_PEN:
242 pattern = NULL;
243 break;
244 case OBJ_EXTPEN:
245 pattern = &pen->pattern;
246 if (!pattern->info) pattern = NULL;
247 break;
248 default:
249 GDI_ReleaseObj( handle );
250 release_dc_ptr( dc );
251 return 0;
254 GDI_inc_ref_count( handle );
255 GDI_ReleaseObj( handle );
257 if (!physdev->funcs->pSelectPen( physdev, handle, pattern ))
259 GDI_dec_ref_count( handle );
261 else
263 ret = dc->hPen;
264 dc->hPen = handle;
265 GDI_dec_ref_count( ret );
268 release_dc_ptr( dc );
269 return ret;
273 /***********************************************************************
274 * PEN_DeleteObject
276 static BOOL PEN_DeleteObject( HGDIOBJ handle )
278 PENOBJ *pen = free_gdi_handle( handle );
280 if (!pen) return FALSE;
281 free_brush_pattern( &pen->pattern );
282 return HeapFree( GetProcessHeap(), 0, pen );
286 /***********************************************************************
287 * PEN_GetObject
289 static INT PEN_GetObject( HGDIOBJ handle, INT count, LPVOID buffer )
291 WORD type;
292 PENOBJ *pen = get_any_obj_ptr( handle, &type );
293 INT ret = 0;
295 if (!pen) return 0;
297 switch (type)
299 case OBJ_PEN:
301 LOGPEN *lp;
303 if (!buffer) ret = sizeof(LOGPEN);
304 else if (count < sizeof(LOGPEN)) ret = 0;
305 else if ((pen->logpen.elpPenStyle & PS_STYLE_MASK) == PS_NULL && count == sizeof(EXTLOGPEN))
307 EXTLOGPEN *elp = buffer;
308 *elp = pen->logpen;
309 elp->elpWidth = 0;
310 ret = sizeof(EXTLOGPEN);
312 else
314 lp = buffer;
315 lp->lopnStyle = pen->logpen.elpPenStyle;
316 lp->lopnColor = pen->logpen.elpColor;
317 lp->lopnWidth.x = pen->logpen.elpWidth;
318 lp->lopnWidth.y = 0;
319 ret = sizeof(LOGPEN);
321 break;
324 case OBJ_EXTPEN:
325 ret = sizeof(EXTLOGPEN) + pen->logpen.elpNumEntries * sizeof(DWORD) - sizeof(pen->logpen.elpStyleEntry);
326 if (buffer)
328 if (count < ret) ret = 0;
329 else memcpy(buffer, &pen->logpen, ret);
331 break;
333 GDI_ReleaseObj( handle );
334 return ret;