gdi32: Don't hold the GDI lock while calling the GetObjectA/W methods for GDI objects.
[wine.git] / dlls / gdi32 / pen.c
blob6298e3c1bc988753e2d1aad59a0085603b6319be
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 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 = GDI_AllocObject( sizeof(PENOBJ), PEN_MAGIC, (HGDIOBJ *)&hpen,
91 &pen_funcs ))) return 0;
92 if (pen->lopnStyle == PS_USERSTYLE || pen->lopnStyle == PS_ALTERNATE)
93 penPtr->logpen.elpPenStyle = PS_SOLID;
94 else
95 penPtr->logpen.elpPenStyle = pen->lopnStyle;
96 if (pen->lopnStyle == PS_NULL)
98 penPtr->logpen.elpWidth = 1;
99 penPtr->logpen.elpColor = RGB(0, 0, 0);
101 else
103 penPtr->logpen.elpWidth = abs(pen->lopnWidth.x);
104 penPtr->logpen.elpColor = pen->lopnColor;
106 penPtr->logpen.elpBrushStyle = BS_SOLID;
107 penPtr->logpen.elpHatch = 0;
108 penPtr->logpen.elpNumEntries = 0;
109 penPtr->logpen.elpStyleEntry[0] = 0;
111 GDI_ReleaseObj( hpen );
112 return hpen;
115 /***********************************************************************
116 * ExtCreatePen (GDI32.@)
118 * FIXME: PS_USERSTYLE not handled
121 HPEN WINAPI ExtCreatePen( DWORD style, DWORD width,
122 const LOGBRUSH * brush, DWORD style_count,
123 const DWORD *style_bits )
125 PENOBJ * penPtr;
126 HPEN hpen;
128 if ((style & PS_STYLE_MASK) == PS_USERSTYLE)
130 if(((INT)style_count) <= 0)
131 return 0;
133 if ((style_count > 16) || !style_bits)
135 SetLastError(ERROR_INVALID_PARAMETER);
136 return 0;
139 if ((style & PS_TYPE_MASK) == PS_COSMETIC)
141 /* FIXME: PS_USERSTYLE workaround */
142 FIXME("PS_COSMETIC | PS_USERSTYLE not handled\n");
143 style = (style & ~PS_STYLE_MASK) | PS_SOLID;
145 else
147 UINT i;
148 BOOL has_neg = FALSE, all_zero = TRUE;
150 for(i = 0; (i < style_count) && !has_neg; i++)
152 has_neg = has_neg || (((INT)(style_bits[i])) < 0);
153 all_zero = all_zero && (style_bits[i] == 0);
156 if(all_zero || has_neg)
158 SetLastError(ERROR_INVALID_PARAMETER);
159 return 0;
163 else
165 if (style_count || style_bits)
167 SetLastError(ERROR_INVALID_PARAMETER);
168 return 0;
172 if ((style & PS_STYLE_MASK) == PS_NULL)
173 return CreatePen( PS_NULL, 0, brush->lbColor );
175 if ((style & PS_TYPE_MASK) == PS_GEOMETRIC)
177 /* PS_ALTERNATE is applicable only for cosmetic pens */
178 if ((style & PS_STYLE_MASK) == PS_ALTERNATE)
180 SetLastError(ERROR_INVALID_PARAMETER);
181 return 0;
184 if (brush->lbHatch && ((brush->lbStyle == BS_SOLID) || (brush->lbStyle == BS_HOLLOW)))
186 static int fixme_hatches_shown;
187 if (!fixme_hatches_shown++) FIXME("Hatches not implemented\n");
190 else
192 /* PS_INSIDEFRAME is applicable only for geometric pens */
193 if ((style & PS_STYLE_MASK) == PS_INSIDEFRAME || width != 1)
195 SetLastError(ERROR_INVALID_PARAMETER);
196 return 0;
200 if (!(penPtr = GDI_AllocObject( sizeof(PENOBJ) +
201 style_count * sizeof(DWORD) - sizeof(penPtr->logpen.elpStyleEntry),
202 EXT_PEN_MAGIC, (HGDIOBJ *)&hpen,
203 &pen_funcs ))) return 0;
205 penPtr->logpen.elpPenStyle = style;
206 penPtr->logpen.elpWidth = abs(width);
207 penPtr->logpen.elpBrushStyle = brush->lbStyle;
208 penPtr->logpen.elpColor = brush->lbColor;
209 penPtr->logpen.elpHatch = brush->lbHatch;
210 penPtr->logpen.elpNumEntries = style_count;
211 memcpy(penPtr->logpen.elpStyleEntry, style_bits, style_count * sizeof(DWORD));
213 GDI_ReleaseObj( hpen );
215 return hpen;
219 /***********************************************************************
220 * PEN_SelectObject
222 static HGDIOBJ PEN_SelectObject( HGDIOBJ handle, HDC hdc )
224 HGDIOBJ ret = 0;
225 DC *dc = get_dc_ptr( hdc );
227 if (!dc)
229 SetLastError( ERROR_INVALID_HANDLE );
230 return 0;
233 if (!GDI_inc_ref_count( handle ))
235 release_dc_ptr( dc );
236 return 0;
239 if (dc->funcs->pSelectPen && !dc->funcs->pSelectPen( dc->physDev, handle ))
241 GDI_dec_ref_count( handle );
243 else
245 ret = dc->hPen;
246 dc->hPen = handle;
247 GDI_dec_ref_count( ret );
249 release_dc_ptr( dc );
250 return ret;
254 /***********************************************************************
255 * PEN_DeleteObject
257 static BOOL PEN_DeleteObject( HGDIOBJ handle )
259 PENOBJ *pen = GDI_GetObjPtr( handle, MAGIC_DONTCARE );
261 if (!pen) return FALSE;
262 return GDI_FreeObject( handle, pen );
266 /***********************************************************************
267 * PEN_GetObject
269 static INT PEN_GetObject( HGDIOBJ handle, INT count, LPVOID buffer )
271 PENOBJ *pen = GDI_GetObjPtr( handle, MAGIC_DONTCARE );
272 INT ret = 0;
274 if (!pen) return 0;
276 switch (GDIMAGIC(pen->header.wMagic))
278 case PEN_MAGIC:
280 LOGPEN *lp;
282 if (!buffer) ret = sizeof(LOGPEN);
283 else if (count < sizeof(LOGPEN)) ret = 0;
284 else if ((pen->logpen.elpPenStyle & PS_STYLE_MASK) == PS_NULL && count == sizeof(EXTLOGPEN))
286 EXTLOGPEN *elp = buffer;
287 *elp = pen->logpen;
288 elp->elpWidth = 0;
289 ret = sizeof(EXTLOGPEN);
291 else
293 lp = buffer;
294 lp->lopnStyle = pen->logpen.elpPenStyle;
295 lp->lopnColor = pen->logpen.elpColor;
296 lp->lopnWidth.x = pen->logpen.elpWidth;
297 lp->lopnWidth.y = 0;
298 ret = sizeof(LOGPEN);
300 break;
303 case EXT_PEN_MAGIC:
304 ret = sizeof(EXTLOGPEN) + pen->logpen.elpNumEntries * sizeof(DWORD) - sizeof(pen->logpen.elpStyleEntry);
305 if (buffer)
307 if (count < ret) ret = 0;
308 else memcpy(buffer, &pen->logpen, ret);
310 break;
312 GDI_ReleaseObj( handle );
313 return ret;