gdi32: Make the PS_NULL test pass under Wine.
[wine/wine64.git] / dlls / gdi32 / pen.c
bloba308f2be7d2ff02f8a17d17f680e71f52d9d4b8c
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 (pen->lopnStyle == PS_NULL)
85 hpen = GetStockObject(NULL_PEN);
86 if (hpen) return hpen;
89 if (!(penPtr = GDI_AllocObject( sizeof(PENOBJ), PEN_MAGIC, (HGDIOBJ *)&hpen,
90 &pen_funcs ))) return 0;
91 if (pen->lopnStyle == PS_USERSTYLE || pen->lopnStyle == PS_ALTERNATE)
92 penPtr->logpen.elpPenStyle = PS_SOLID;
93 else
94 penPtr->logpen.elpPenStyle = pen->lopnStyle;
95 if (pen->lopnStyle == PS_NULL)
97 penPtr->logpen.elpWidth = 1;
98 penPtr->logpen.elpColor = RGB(0, 0, 0);
100 else
102 penPtr->logpen.elpWidth = abs(pen->lopnWidth.x);
103 penPtr->logpen.elpColor = pen->lopnColor;
105 penPtr->logpen.elpBrushStyle = BS_SOLID;
106 penPtr->logpen.elpHatch = 0;
107 penPtr->logpen.elpNumEntries = 0;
108 penPtr->logpen.elpStyleEntry[0] = 0;
110 GDI_ReleaseObj( hpen );
111 return hpen;
114 /***********************************************************************
115 * ExtCreatePen (GDI32.@)
117 * FIXME: PS_USERSTYLE not handled
120 HPEN WINAPI ExtCreatePen( DWORD style, DWORD width,
121 const LOGBRUSH * brush, DWORD style_count,
122 const DWORD *style_bits )
124 PENOBJ * penPtr;
125 HPEN hpen;
127 if ((style & PS_STYLE_MASK) == PS_USERSTYLE)
129 if(((INT)style_count) <= 0)
130 return 0;
132 if ((style_count > 16) || !style_bits)
134 SetLastError(ERROR_INVALID_PARAMETER);
135 return 0;
138 if ((style & PS_TYPE_MASK) == PS_COSMETIC)
140 /* FIXME: PS_USERSTYLE workaround */
141 FIXME("PS_COSMETIC | PS_USERSTYLE not handled\n");
142 style = (style & ~PS_STYLE_MASK) | PS_SOLID;
144 else
146 UINT i;
147 BOOL has_neg = FALSE, all_zero = TRUE;
149 for(i = 0; (i < style_count) && !has_neg; i++)
151 has_neg = has_neg || (((INT)(style_bits[i])) < 0);
152 all_zero = all_zero && (style_bits[i] == 0);
155 if(all_zero || has_neg)
157 SetLastError(ERROR_INVALID_PARAMETER);
158 return 0;
162 else
164 if (style_count || style_bits)
166 SetLastError(ERROR_INVALID_PARAMETER);
167 return 0;
171 if ((style & PS_STYLE_MASK) == PS_NULL)
172 return CreatePen( PS_NULL, 0, brush->lbColor );
174 if ((style & PS_TYPE_MASK) == PS_GEOMETRIC)
176 /* PS_ALTERNATE is applicable only for cosmetic pens */
177 if ((style & PS_STYLE_MASK) == PS_ALTERNATE)
179 SetLastError(ERROR_INVALID_PARAMETER);
180 return 0;
183 if (brush->lbHatch && ((brush->lbStyle == BS_SOLID) || (brush->lbStyle == BS_HOLLOW)))
185 static int fixme_hatches_shown;
186 if (!fixme_hatches_shown++) FIXME("Hatches not implemented\n");
189 else
191 /* PS_INSIDEFRAME is applicable only for geometric pens */
192 if ((style & PS_STYLE_MASK) == PS_INSIDEFRAME || width != 1)
194 SetLastError(ERROR_INVALID_PARAMETER);
195 return 0;
199 if (!(penPtr = GDI_AllocObject( sizeof(PENOBJ) +
200 style_count * sizeof(DWORD) - sizeof(penPtr->logpen.elpStyleEntry),
201 EXT_PEN_MAGIC, (HGDIOBJ *)&hpen,
202 &pen_funcs ))) return 0;
204 penPtr->logpen.elpPenStyle = style;
205 penPtr->logpen.elpWidth = abs(width);
206 penPtr->logpen.elpBrushStyle = brush->lbStyle;
207 penPtr->logpen.elpColor = brush->lbColor;
208 penPtr->logpen.elpHatch = brush->lbHatch;
209 penPtr->logpen.elpNumEntries = style_count;
210 memcpy(penPtr->logpen.elpStyleEntry, style_bits, style_count * sizeof(DWORD));
212 GDI_ReleaseObj( hpen );
214 return hpen;
218 /***********************************************************************
219 * PEN_SelectObject
221 static HGDIOBJ PEN_SelectObject( HGDIOBJ handle, HDC hdc )
223 HGDIOBJ ret = 0;
224 DC *dc = get_dc_ptr( hdc );
226 if (!dc)
228 SetLastError( ERROR_INVALID_HANDLE );
229 return 0;
232 if (!GDI_inc_ref_count( handle ))
234 release_dc_ptr( dc );
235 return 0;
238 if (dc->funcs->pSelectPen && !dc->funcs->pSelectPen( dc->physDev, handle ))
240 GDI_dec_ref_count( handle );
242 else
244 ret = dc->hPen;
245 dc->hPen = handle;
246 GDI_dec_ref_count( ret );
248 release_dc_ptr( dc );
249 return ret;
253 /***********************************************************************
254 * PEN_GetObject
256 static INT PEN_GetObject( HGDIOBJ handle, void *obj, INT count, LPVOID buffer )
258 PENOBJ *pen = obj;
260 switch (GDIMAGIC(pen->header.wMagic))
262 case PEN_MAGIC:
264 LOGPEN *lp;
266 if (!buffer) return sizeof(LOGPEN);
268 if (count < sizeof(LOGPEN)) return 0;
270 if ((pen->logpen.elpPenStyle & PS_STYLE_MASK) == PS_NULL &&
271 count == sizeof(EXTLOGPEN))
273 EXTLOGPEN *elp = buffer;
274 *elp = pen->logpen;
275 elp->elpWidth = 0;
276 return sizeof(EXTLOGPEN);
279 lp = buffer;
280 lp->lopnStyle = pen->logpen.elpPenStyle;
281 lp->lopnColor = pen->logpen.elpColor;
282 lp->lopnWidth.x = pen->logpen.elpWidth;
283 lp->lopnWidth.y = 0;
284 return sizeof(LOGPEN);
287 case EXT_PEN_MAGIC:
289 INT size = sizeof(EXTLOGPEN) + pen->logpen.elpNumEntries * sizeof(DWORD) - sizeof(pen->logpen.elpStyleEntry);
291 if (!buffer) return size;
293 if (count < size) return 0;
294 memcpy(buffer, &pen->logpen, size);
295 return size;
298 default:
299 break;
301 assert(0);
302 return 0;