winevulkan: Merge body and body_conversion.
[wine.git] / dlls / win32u / pen.c
blob551c4438f3cde5b965311ec5213467fa4f7f813b
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 #if 0
22 #pragma makedep unix
23 #endif
25 #include <stdarg.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <assert.h>
30 #include "windef.h"
31 #include "winbase.h"
32 #include "wingdi.h"
33 #include "ntgdi_private.h"
34 #include "wine/debug.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(gdi);
38 /* GDI logical pen object */
39 typedef struct
41 struct gdi_obj_header obj;
42 struct brush_pattern pattern;
43 EXTLOGPEN logpen;
44 } PENOBJ;
47 static INT PEN_GetObject( HGDIOBJ handle, INT count, LPVOID buffer );
48 static BOOL PEN_DeleteObject( HGDIOBJ handle );
50 static const struct gdi_obj_funcs pen_funcs =
52 PEN_GetObject, /* pGetObjectW */
53 NULL, /* pUnrealizeObject */
54 PEN_DeleteObject /* pDeleteObject */
57 HPEN create_pen( INT style, INT width, COLORREF color )
59 PENOBJ *penPtr;
60 HPEN hpen;
62 TRACE( "%d %d %06x\n", style, width, color );
64 switch (style)
66 case PS_SOLID:
67 case PS_DASH:
68 case PS_DOT:
69 case PS_DASHDOT:
70 case PS_DASHDOTDOT:
71 case PS_INSIDEFRAME:
72 break;
73 case PS_NULL:
74 width = 1;
75 color = 0;
76 break;
77 default:
78 return 0;
81 if (!(penPtr = calloc( 1, sizeof(*penPtr) ))) return 0;
83 penPtr->logpen.elpPenStyle = style;
84 penPtr->logpen.elpWidth = abs(width);
85 penPtr->logpen.elpColor = color;
86 penPtr->logpen.elpBrushStyle = BS_SOLID;
88 if (!(hpen = alloc_gdi_handle( &penPtr->obj, NTGDI_OBJ_PEN, &pen_funcs )))
89 free( penPtr );
90 return hpen;
93 /***********************************************************************
94 * NtGdiCreatePen (win32u.@)
96 HPEN WINAPI NtGdiCreatePen( INT style, INT width, COLORREF color, HBRUSH brush )
98 if (brush) FIXME( "brush not supported\n" );
99 if (style == PS_NULL) return GetStockObject( NULL_PEN );
100 return create_pen( style, width, color );
103 /***********************************************************************
104 * NtGdiExtCreatePen (win32u.@)
106 HPEN WINAPI NtGdiExtCreatePen( DWORD style, DWORD width, ULONG brush_style, ULONG color,
107 ULONG_PTR client_hatch, ULONG_PTR hatch, DWORD style_count,
108 const DWORD *style_bits, ULONG dib_size, BOOL old_style,
109 HBRUSH brush )
111 PENOBJ *penPtr = NULL;
112 HPEN hpen;
113 LOGBRUSH logbrush;
115 if ((style_count || style_bits) && (style & PS_STYLE_MASK) != PS_USERSTYLE)
116 goto invalid;
118 switch (style & PS_STYLE_MASK)
120 case PS_NULL:
121 return NtGdiCreatePen( PS_NULL, 0, color, NULL );
123 case PS_SOLID:
124 case PS_DASH:
125 case PS_DOT:
126 case PS_DASHDOT:
127 case PS_DASHDOTDOT:
128 break;
130 case PS_USERSTYLE:
131 if (((INT)style_count) <= 0) return 0;
133 if ((style_count > 16) || !style_bits) goto invalid;
135 if ((style & PS_TYPE_MASK) == PS_GEOMETRIC)
137 UINT i;
138 BOOL has_neg = FALSE, all_zero = TRUE;
140 for(i = 0; (i < style_count) && !has_neg; i++)
142 has_neg = has_neg || (((INT)(style_bits[i])) < 0);
143 all_zero = all_zero && (style_bits[i] == 0);
146 if (all_zero || has_neg) goto invalid;
148 break;
150 case PS_INSIDEFRAME: /* applicable only for geometric pens */
151 if ((style & PS_TYPE_MASK) != PS_GEOMETRIC) goto invalid;
152 break;
154 case PS_ALTERNATE: /* applicable only for cosmetic pens */
155 if ((style & PS_TYPE_MASK) == PS_GEOMETRIC) goto invalid;
156 break;
158 default:
159 RtlSetLastWin32Error(ERROR_INVALID_PARAMETER);
160 return 0;
163 if ((style & PS_TYPE_MASK) == PS_GEOMETRIC)
165 if (brush_style == BS_NULL) return NtGdiCreatePen( PS_NULL, 0, 0, NULL );
167 else
169 if (width != 1) goto invalid;
170 if (brush_style != BS_SOLID) goto invalid;
173 if (!(penPtr = malloc( FIELD_OFFSET(PENOBJ,logpen.elpStyleEntry[style_count]) )))
174 return 0;
176 logbrush.lbStyle = brush_style;
177 logbrush.lbColor = color;
178 logbrush.lbHatch = hatch;
179 if (!store_brush_pattern( &logbrush, &penPtr->pattern )) goto invalid;
180 if (logbrush.lbStyle == BS_DIBPATTERN) logbrush.lbStyle = BS_DIBPATTERNPT;
182 penPtr->logpen.elpPenStyle = style;
183 penPtr->logpen.elpWidth = abs((int)width);
184 penPtr->logpen.elpBrushStyle = logbrush.lbStyle;
185 penPtr->logpen.elpColor = logbrush.lbColor;
186 penPtr->logpen.elpHatch = client_hatch;
187 penPtr->logpen.elpNumEntries = style_count;
188 memcpy(penPtr->logpen.elpStyleEntry, style_bits, style_count * sizeof(DWORD));
190 if (!(hpen = alloc_gdi_handle( &penPtr->obj, NTGDI_OBJ_EXTPEN, &pen_funcs )))
192 free_brush_pattern( &penPtr->pattern );
193 free( penPtr );
195 return hpen;
197 invalid:
198 free( penPtr );
199 RtlSetLastWin32Error( ERROR_INVALID_PARAMETER );
200 return 0;
203 /***********************************************************************
204 * NtGdiSelectPen (win32u.@)
206 HGDIOBJ WINAPI NtGdiSelectPen( HDC hdc, HGDIOBJ handle )
208 PENOBJ *pen;
209 HGDIOBJ ret = 0;
210 DWORD type;
211 DC *dc;
213 if (!(dc = get_dc_ptr( hdc ))) return 0;
215 if ((pen = get_any_obj_ptr( handle, &type )))
217 struct brush_pattern *pattern;
218 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pSelectPen );
220 switch (type)
222 case NTGDI_OBJ_PEN:
223 pattern = NULL;
224 break;
225 case NTGDI_OBJ_EXTPEN:
226 pattern = &pen->pattern;
227 if (!pattern->info) pattern = NULL;
228 break;
229 default:
230 GDI_ReleaseObj( handle );
231 release_dc_ptr( dc );
232 return 0;
235 GDI_inc_ref_count( handle );
236 GDI_ReleaseObj( handle );
238 if (!physdev->funcs->pSelectPen( physdev, handle, pattern ))
240 GDI_dec_ref_count( handle );
242 else
244 ret = dc->hPen;
245 dc->hPen = handle;
246 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 = free_gdi_handle( handle );
261 if (!pen) return FALSE;
262 free_brush_pattern( &pen->pattern );
263 free( pen );
264 return TRUE;
268 /***********************************************************************
269 * PEN_GetObject
271 static INT PEN_GetObject( HGDIOBJ handle, INT count, LPVOID buffer )
273 DWORD type;
274 PENOBJ *pen = get_any_obj_ptr( handle, &type );
275 INT ret = 0;
277 if (!pen) return 0;
279 switch (type)
281 case NTGDI_OBJ_PEN:
283 LOGPEN *lp;
285 if (!buffer) ret = sizeof(LOGPEN);
286 else if (count < sizeof(LOGPEN)) ret = 0;
287 else if ((pen->logpen.elpPenStyle & PS_STYLE_MASK) == PS_NULL && count == sizeof(EXTLOGPEN))
289 EXTLOGPEN *elp = buffer;
290 *elp = pen->logpen;
291 elp->elpWidth = 0;
292 ret = sizeof(EXTLOGPEN);
294 else
296 lp = buffer;
297 lp->lopnStyle = pen->logpen.elpPenStyle;
298 lp->lopnColor = pen->logpen.elpColor;
299 lp->lopnWidth.x = pen->logpen.elpWidth;
300 lp->lopnWidth.y = 0;
301 ret = sizeof(LOGPEN);
303 break;
306 case NTGDI_OBJ_EXTPEN:
307 ret = sizeof(EXTLOGPEN) + pen->logpen.elpNumEntries * sizeof(DWORD) - sizeof(pen->logpen.elpStyleEntry);
308 if (buffer)
310 if (count < ret) ret = 0;
311 else memcpy(buffer, &pen->logpen, ret);
313 break;
315 GDI_ReleaseObj( handle );
316 return ret;