d3dx9: Use float function in D3DXPlaneNormalize().
[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     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) pattern = NULL;
247             break;
248         default:
249             GDI_ReleaseObj( handle );
250             release_dc_ptr( dc );
251             return 0;
252         }
253
254         GDI_inc_ref_count( handle );
255         GDI_ReleaseObj( handle );
256
257         if (!physdev->funcs->pSelectPen( physdev, handle, pattern ))
258         {
259             GDI_dec_ref_count( handle );
260         }
261         else
262         {
263             ret = dc->hPen;
264             dc->hPen = handle;
265             GDI_dec_ref_count( ret );
266         }
267     }
268     release_dc_ptr( dc );
269     return ret;
270 }
271
272
273 /***********************************************************************
274  *           PEN_DeleteObject
275  */
276 static BOOL PEN_DeleteObject( HGDIOBJ handle )
277 {
278     PENOBJ *pen = free_gdi_handle( handle );
279
280     if (!pen) return FALSE;
281     free_brush_pattern( &pen->pattern );
282     return HeapFree( GetProcessHeap(), 0, pen );
283 }
284
285
286 /***********************************************************************
287  *           PEN_GetObject
288  */
289 static INT PEN_GetObject( HGDIOBJ handle, INT count, LPVOID buffer )
290 {
291     PENOBJ *pen = GDI_GetObjPtr( handle, 0 );
292     INT ret = 0;
293
294     if (!pen) return 0;
295
296     switch (pen->header.type)
297     {
298     case OBJ_PEN:
299     {
300         LOGPEN *lp;
301
302         if (!buffer) ret = sizeof(LOGPEN);
303         else if (count < sizeof(LOGPEN)) ret = 0;
304         else if ((pen->logpen.elpPenStyle & PS_STYLE_MASK) == PS_NULL && count == sizeof(EXTLOGPEN))
305         {
306             EXTLOGPEN *elp = buffer;
307             *elp = pen->logpen;
308             elp->elpWidth = 0;
309             ret = sizeof(EXTLOGPEN);
310         }
311         else
312         {
313             lp = buffer;
314             lp->lopnStyle = pen->logpen.elpPenStyle;
315             lp->lopnColor = pen->logpen.elpColor;
316             lp->lopnWidth.x = pen->logpen.elpWidth;
317             lp->lopnWidth.y = 0;
318             ret = sizeof(LOGPEN);
319         }
320         break;
321     }
322
323     case OBJ_EXTPEN:
324         ret = sizeof(EXTLOGPEN) + pen->logpen.elpNumEntries * sizeof(DWORD) - sizeof(pen->logpen.elpStyleEntry);
325         if (buffer)
326         {
327             if (count < ret) ret = 0;
328             else memcpy(buffer, &pen->logpen, ret);
329         }
330         break;
331     }
332     GDI_ReleaseObj( handle );
333     return ret;
334 }