1 /*
2 * GDI pen objects
3 *
4 * Copyright 1993 Alexandre Julliard
5 *
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.
10 *
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.
15 *
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
19 */
20
21 #include "config.h"
22
23 #include <stdarg.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <assert.h>
27
28 #include "windef.h"
29 #include "winbase.h"
30 #include "wingdi.h"
31 #include "gdi_private.h"
32 #include "wine/debug.h"
33
34 WINE_DEFAULT_DEBUG_CHANNEL(gdi);
35
36 /* GDI logical pen object */
37 typedef struct
38 {
39 GDIOBJHDR header;
40 struct brush_pattern pattern;
41 EXTLOGPEN logpen;
42 } PENOBJ;
43
44
45 static HGDIOBJ PEN_SelectObject( HGDIOBJ handle, HDC hdc );
46 static INT PEN_GetObject( HGDIOBJ handle, INT count, LPVOID buffer );
47 static BOOL PEN_DeleteObject( HGDIOBJ handle );
48
49 static const struct gdi_obj_funcs pen_funcs =
50 {
51 PEN_SelectObject, /* pSelectObject */
52 PEN_GetObject, /* pGetObjectA */
53 PEN_GetObject, /* pGetObjectW */
54 NULL, /* pUnrealizeObject */
55 PEN_DeleteObject /* pDeleteObject */
56 };
57
58
59 /***********************************************************************
60 * CreatePen (GDI32.@)
61 */
62 HPEN WINAPI CreatePen( INT style, INT width, COLORREF color )
63 {
64 LOGPEN logpen;
65
66 TRACE("%d %d %06x\n", style, width, color );
67
68 logpen.lopnStyle = style;
69 logpen.lopnWidth.x = width;
70 logpen.lopnWidth.y = 0;
71 logpen.lopnColor = color;
72
73 return CreatePenIndirect( &logpen );
74 }
75
76
77 /***********************************************************************
78 * CreatePenIndirect (GDI32.@)
79 */
80 HPEN WINAPI CreatePenIndirect( const LOGPEN * pen )
81 {
82 PENOBJ * penPtr;
83 HPEN hpen;
84
85 if (pen->lopnStyle == PS_NULL)
86 {
87 hpen = GetStockObject(NULL_PEN);
88 if (hpen) return hpen;
89 }
90
91 if (!(penPtr = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*penPtr) ))) return 0;
92
93 penPtr->logpen.elpPenStyle = pen->lopnStyle;
94 penPtr->logpen.elpWidth = abs(pen->lopnWidth.x);
95 penPtr->logpen.elpColor = pen->lopnColor;
96 penPtr->logpen.elpBrushStyle = BS_SOLID;
97
98 switch (pen->lopnStyle)
99 {
100 case PS_SOLID:
101 case PS_DASH:
102 case PS_DOT:
103 case PS_DASHDOT:
104 case PS_DASHDOTDOT:
105 case PS_INSIDEFRAME:
106 break;
107 case PS_NULL:
108 penPtr->logpen.elpWidth = 1;
109 penPtr->logpen.elpColor = 0;
110 break;
111 default:
112 penPtr->logpen.elpPenStyle = PS_SOLID;
113 break;
114 }
115
116 if (!(hpen = alloc_gdi_handle( &penPtr->header, OBJ_PEN, &pen_funcs )))
117 HeapFree( GetProcessHeap(), 0, penPtr );
118 return hpen;
119 }
120
121 /***********************************************************************
122 * ExtCreatePen (GDI32.@)
123 */
124
125 HPEN WINAPI ExtCreatePen( DWORD style, DWORD width,
126 const LOGBRUSH * brush, DWORD style_count,
127 const DWORD *style_bits )
128 {
129 PENOBJ *penPtr = NULL;
130 HPEN hpen;
131 LOGBRUSH logbrush;
132
133 if ((style_count || style_bits) && (style & PS_STYLE_MASK) != PS_USERSTYLE)
134 goto invalid;
135
136 switch (style & PS_STYLE_MASK)
137 {
138 case PS_NULL:
139 return CreatePen( PS_NULL, 0, brush->lbColor );
140
141 case PS_SOLID:
142 case PS_DASH:
143 case PS_DOT:
144 case PS_DASHDOT:
145 case PS_DASHDOTDOT:
146 break;
147
148 case PS_USERSTYLE:
149 if (((INT)style_count) <= 0) return 0;
150
151 if ((style_count > 16) || !style_bits) goto invalid;
152
153 if ((style & PS_TYPE_MASK) == PS_GEOMETRIC)
154 {
155 UINT i;
156 BOOL has_neg = FALSE, all_zero = TRUE;
157
158 for(i = 0; (i < style_count) && !has_neg; i++)
159 {
160 has_neg = has_neg || (((INT)(style_bits[i])) < 0);
161 all_zero = all_zero && (style_bits[i] == 0);
162 }
163
164 if (all_zero || has_neg) goto invalid;
165 }
166 break;
167
168 case PS_INSIDEFRAME: /* applicable only for geometric pens */
169 if ((style & PS_TYPE_MASK) != PS_GEOMETRIC) goto invalid;
170 break;
171
172 case PS_ALTERNATE: /* applicable only for cosmetic pens */
173 if ((style & PS_TYPE_MASK) == PS_GEOMETRIC) goto invalid;
174 break;
175
176 default:
177 SetLastError(ERROR_INVALID_PARAMETER);
178 return 0;
179 }
180
181 if ((style & PS_TYPE_MASK) == PS_GEOMETRIC)
182 {
183 if (brush->lbStyle == BS_NULL) return CreatePen( PS_NULL, 0, 0 );
184 }
185 else
186 {
187 if (width != 1) goto invalid;
188 if (brush->lbStyle != BS_SOLID) goto invalid;
189 }
190
191 if (!(penPtr = HeapAlloc(GetProcessHeap(), 0, FIELD_OFFSET(PENOBJ,logpen.elpStyleEntry[style_count]))))
192 return 0;
193
194 logbrush = *brush;
195 if (!store_brush_pattern( &logbrush, &penPtr->pattern )) goto invalid;
196 if (logbrush.lbStyle == BS_DIBPATTERN) logbrush.lbStyle = BS_DIBPATTERNPT;
197
198 penPtr->logpen.elpPenStyle = style;
199 penPtr->logpen.elpWidth = abs(width);
200 penPtr->logpen.elpBrushStyle = logbrush.lbStyle;
201 penPtr->logpen.elpColor = logbrush.lbColor;
202 penPtr->logpen.elpHatch = brush->lbHatch;
203 penPtr->logpen.elpNumEntries = style_count;
204 memcpy(penPtr->logpen.elpStyleEntry, style_bits, style_count * sizeof(DWORD));
205
206 if (!(hpen = alloc_gdi_handle( &penPtr->header, OBJ_EXTPEN, &pen_funcs )))
207 {
208 free_brush_pattern( &penPtr->pattern );
209 HeapFree( GetProcessHeap(), 0, penPtr );
210 }
211 return hpen;
212
213 invalid:
214 HeapFree( GetProcessHeap(), 0, penPtr );
215 SetLastError( ERROR_INVALID_PARAMETER );
216 return 0;
217 }
218
219 /***********************************************************************
220 * PEN_SelectObject
221 */
222 static HGDIOBJ PEN_SelectObject( HGDIOBJ handle, HDC hdc )
223 {
224 PENOBJ *pen;
225 HGDIOBJ ret = 0;
226 DC *dc = get_dc_ptr( hdc );
227
228 if (!dc)
229 {
230 SetLastError( ERROR_INVALID_HANDLE );
231 return 0;
232 }
233
234 if ((pen = GDI_GetObjPtr( handle, 0 )))
235 {
236 struct brush_pattern *pattern;
237 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pSelectPen );
238
239 switch (pen->header.type)
240 {
241 case OBJ_PEN:
242 pattern = NULL;
243 break;
244 case OBJ_EXTPEN:
245 pattern = &pen->pattern;
246 if (!pattern->info)
247 {
248 if (pattern->bitmap) cache_pattern_bits( physdev, pattern );
249 else pattern = NULL;
250 }
251 break;
252 default:
253 GDI_ReleaseObj( handle );
254 release_dc_ptr( dc );
255 return 0;
256 }
257
258 GDI_inc_ref_count( handle );
259 GDI_ReleaseObj( handle );
260
261 if (!physdev->funcs->pSelectPen( physdev, handle, pattern ))
262 {
263 GDI_dec_ref_count( handle );
264 }
265 else
266 {
267 ret = dc->hPen;
268 dc->hPen = handle;
269 GDI_dec_ref_count( ret );
270 }
271 }
272 release_dc_ptr( dc );
273 return ret;
274 }
275
276
277 /***********************************************************************
278 * PEN_DeleteObject
279 */
280 static BOOL PEN_DeleteObject( HGDIOBJ handle )
281 {
282 PENOBJ *pen = free_gdi_handle( handle );
283
284 if (!pen) return FALSE;
285 free_brush_pattern( &pen->pattern );
286 return HeapFree( GetProcessHeap(), 0, pen );
287 }
288
289
290 /***********************************************************************
291 * PEN_GetObject
292 */
293 static INT PEN_GetObject( HGDIOBJ handle, INT count, LPVOID buffer )
294 {
295 PENOBJ *pen = GDI_GetObjPtr( handle, 0 );
296 INT ret = 0;
297
298 if (!pen) return 0;
299
300 switch (pen->header.type)
301 {
302 case OBJ_PEN:
303 {
304 LOGPEN *lp;
305
306 if (!buffer) ret = sizeof(LOGPEN);
307 else if (count < sizeof(LOGPEN)) ret = 0;
308 else if ((pen->logpen.elpPenStyle & PS_STYLE_MASK) == PS_NULL && count == sizeof(EXTLOGPEN))
309 {
310 EXTLOGPEN *elp = buffer;
311 *elp = pen->logpen;
312 elp->elpWidth = 0;
313 ret = sizeof(EXTLOGPEN);
314 }
315 else
316 {
317 lp = buffer;
318 lp->lopnStyle = pen->logpen.elpPenStyle;
319 lp->lopnColor = pen->logpen.elpColor;
320 lp->lopnWidth.x = pen->logpen.elpWidth;
321 lp->lopnWidth.y = 0;
322 ret = sizeof(LOGPEN);
323 }
324 break;
325 }
326
327 case OBJ_EXTPEN:
328 ret = sizeof(EXTLOGPEN) + pen->logpen.elpNumEntries * sizeof(DWORD) - sizeof(pen->logpen.elpStyleEntry);
329 if (buffer)
330 {
331 if (count < ret) ret = 0;
332 else memcpy(buffer, &pen->logpen, ret);
333 }
334 break;
335 }
336 GDI_ReleaseObj( handle );
337 return ret;
338 }
339
This page was automatically generated by the
LXR engine.
Visit the LXR main site for more
information.