push 014043c4937c940c54cd1214c96e33a3b3c8cf7d
[wine/hacks.git] / dlls / gdi32 / pen.c
bloba047c658817b92f019fd9743e10a195e09cf64da
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, void *obj, INT count, LPVOID buffer );
47 static const struct gdi_obj_funcs pen_funcs =
49 PEN_SelectObject, /* pSelectObject */
50 PEN_GetObject, /* pGetObjectA */
51 PEN_GetObject, /* pGetObjectW */
52 NULL, /* pUnrealizeObject */
53 GDI_FreeObject /* pDeleteObject */
57 /***********************************************************************
58 * CreatePen (GDI32.@)
60 HPEN WINAPI CreatePen( INT style, INT width, COLORREF color )
62 LOGPEN logpen;
64 TRACE("%d %d %06x\n", style, width, color );
66 logpen.lopnStyle = style;
67 logpen.lopnWidth.x = width;
68 logpen.lopnWidth.y = 0;
69 logpen.lopnColor = color;
71 return CreatePenIndirect( &logpen );
75 /***********************************************************************
76 * CreatePenIndirect (GDI32.@)
78 HPEN WINAPI CreatePenIndirect( const LOGPEN * pen )
80 PENOBJ * penPtr;
81 HPEN hpen;
83 if (!(penPtr = GDI_AllocObject( sizeof(PENOBJ), PEN_MAGIC, (HGDIOBJ *)&hpen,
84 &pen_funcs ))) return 0;
85 if (pen->lopnStyle == PS_USERSTYLE || pen->lopnStyle == PS_ALTERNATE)
86 penPtr->logpen.elpPenStyle = PS_SOLID;
87 else
88 penPtr->logpen.elpPenStyle = pen->lopnStyle;
89 if (pen->lopnStyle == PS_NULL)
91 penPtr->logpen.elpWidth = 1;
92 penPtr->logpen.elpColor = RGB(0, 0, 0);
94 else
96 penPtr->logpen.elpWidth = abs(pen->lopnWidth.x);
97 penPtr->logpen.elpColor = pen->lopnColor;
99 penPtr->logpen.elpBrushStyle = BS_SOLID;
100 penPtr->logpen.elpHatch = 0;
101 penPtr->logpen.elpNumEntries = 0;
102 penPtr->logpen.elpStyleEntry[0] = 0;
104 GDI_ReleaseObj( hpen );
105 return hpen;
108 /***********************************************************************
109 * ExtCreatePen (GDI32.@)
111 * FIXME: PS_USERSTYLE not handled
114 HPEN WINAPI ExtCreatePen( DWORD style, DWORD width,
115 const LOGBRUSH * brush, DWORD style_count,
116 const DWORD *style_bits )
118 PENOBJ * penPtr;
119 HPEN hpen;
121 if ((style & PS_STYLE_MASK) == PS_USERSTYLE)
123 if(((INT)style_count) <= 0)
124 return 0;
126 if ((style_count > 16) || !style_bits)
128 SetLastError(ERROR_INVALID_PARAMETER);
129 return 0;
132 if ((style & PS_TYPE_MASK) == PS_COSMETIC)
134 /* FIXME: PS_USERSTYLE workaround */
135 FIXME("PS_COSMETIC | PS_USERSTYLE not handled\n");
136 style = (style & ~PS_STYLE_MASK) | PS_SOLID;
138 else
140 UINT i;
141 BOOL has_neg = FALSE, all_zero = TRUE;
143 for(i = 0; (i < style_count) && !has_neg; i++)
145 has_neg = has_neg || (((INT)(style_bits[i])) < 0);
146 all_zero = all_zero && (style_bits[i] == 0);
149 if(all_zero || has_neg)
151 SetLastError(ERROR_INVALID_PARAMETER);
152 return 0;
156 else
158 if (style_count || style_bits)
160 SetLastError(ERROR_INVALID_PARAMETER);
161 return 0;
165 if ((style & PS_STYLE_MASK) == PS_NULL)
166 return CreatePen( PS_NULL, 0, brush->lbColor );
168 if ((style & PS_TYPE_MASK) == PS_GEOMETRIC)
170 /* PS_ALTERNATE is applicable only for cosmetic pens */
171 if ((style & PS_STYLE_MASK) == PS_ALTERNATE)
173 SetLastError(ERROR_INVALID_PARAMETER);
174 return 0;
177 if (brush->lbHatch && ((brush->lbStyle == BS_SOLID) || (brush->lbStyle == BS_HOLLOW)))
179 static int fixme_hatches_shown;
180 if (!fixme_hatches_shown++) FIXME("Hatches not implemented\n");
183 else
185 /* PS_INSIDEFRAME is applicable only for gemetric pens */
186 if ((style & PS_STYLE_MASK) == PS_INSIDEFRAME || width != 1)
188 SetLastError(ERROR_INVALID_PARAMETER);
189 return 0;
193 if (!(penPtr = GDI_AllocObject( sizeof(PENOBJ) +
194 style_count * sizeof(DWORD) - sizeof(penPtr->logpen.elpStyleEntry),
195 EXT_PEN_MAGIC, (HGDIOBJ *)&hpen,
196 &pen_funcs ))) return 0;
198 penPtr->logpen.elpPenStyle = style;
199 penPtr->logpen.elpWidth = abs(width);
200 penPtr->logpen.elpBrushStyle = brush->lbStyle;
201 penPtr->logpen.elpColor = brush->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 GDI_ReleaseObj( hpen );
208 return hpen;
212 /***********************************************************************
213 * PEN_SelectObject
215 static HGDIOBJ PEN_SelectObject( HGDIOBJ handle, HDC hdc )
217 HGDIOBJ ret = 0;
218 DC *dc = get_dc_ptr( hdc );
220 if (!dc)
222 SetLastError( ERROR_INVALID_HANDLE );
223 return 0;
226 if (!GDI_inc_ref_count( handle ))
228 release_dc_ptr( dc );
229 return 0;
232 if (dc->funcs->pSelectPen && !dc->funcs->pSelectPen( dc->physDev, handle ))
234 GDI_dec_ref_count( handle );
236 else
238 ret = dc->hPen;
239 dc->hPen = handle;
240 GDI_dec_ref_count( ret );
242 release_dc_ptr( dc );
243 return ret;
247 /***********************************************************************
248 * PEN_GetObject
250 static INT PEN_GetObject( HGDIOBJ handle, void *obj, INT count, LPVOID buffer )
252 PENOBJ *pen = obj;
254 switch (GDIMAGIC(pen->header.wMagic))
256 case PEN_MAGIC:
258 LOGPEN *lp;
260 if (!buffer) return sizeof(LOGPEN);
262 if (count < sizeof(LOGPEN)) return 0;
264 if ((pen->logpen.elpPenStyle & PS_STYLE_MASK) == PS_NULL &&
265 count == sizeof(EXTLOGPEN))
267 EXTLOGPEN *elp = buffer;
268 *elp = pen->logpen;
269 elp->elpWidth = 0;
270 return sizeof(EXTLOGPEN);
273 lp = buffer;
274 lp->lopnStyle = pen->logpen.elpPenStyle;
275 lp->lopnColor = pen->logpen.elpColor;
276 lp->lopnWidth.x = pen->logpen.elpWidth;
277 lp->lopnWidth.y = 0;
278 return sizeof(LOGPEN);
281 case EXT_PEN_MAGIC:
283 INT size = sizeof(EXTLOGPEN) + pen->logpen.elpNumEntries * sizeof(DWORD) - sizeof(pen->logpen.elpStyleEntry);
285 if (!buffer) return size;
287 if (count < size) return 0;
288 memcpy(buffer, &pen->logpen, size);
289 return size;
292 default:
293 break;
295 assert(0);
296 return 0;