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