jscript: Moved instr_off from statement_t to source_elements_t.
[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     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(), 0, 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     penPtr->logpen.elpHatch = 0;
97     penPtr->logpen.elpNumEntries = 0;
98     penPtr->logpen.elpStyleEntry[0] = 0;
99
100     switch (pen->lopnStyle)
101     {
102     case PS_SOLID:
103     case PS_DASH:
104     case PS_DOT:
105     case PS_DASHDOT:
106     case PS_DASHDOTDOT:
107     case PS_INSIDEFRAME:
108         break;
109     case PS_NULL:
110         penPtr->logpen.elpWidth = 1;
111         penPtr->logpen.elpColor = 0;
112         break;
113     default:
114         penPtr->logpen.elpPenStyle = PS_SOLID;
115         break;
116     }
117
118     if (!(hpen = alloc_gdi_handle( &penPtr->header, OBJ_PEN, &pen_funcs )))
119         HeapFree( GetProcessHeap(), 0, penPtr );
120     return hpen;
121 }
122
123 /***********************************************************************
124  *           ExtCreatePen    (GDI32.@)
125  */
126
127 HPEN WINAPI ExtCreatePen( DWORD style, DWORD width,
128                               const LOGBRUSH * brush, DWORD style_count,
129                               const DWORD *style_bits )
130 {
131     PENOBJ * penPtr;
132     HPEN hpen;
133
134     if ((style_count || style_bits) && (style & PS_STYLE_MASK) != PS_USERSTYLE)
135     {
136         SetLastError(ERROR_INVALID_PARAMETER);
137         return 0;
138     }
139
140     switch (style & PS_STYLE_MASK)
141     {
142     case PS_NULL:
143         return CreatePen( PS_NULL, 0, brush->lbColor );
144
145     case PS_SOLID:
146     case PS_DASH:
147     case PS_DOT:
148     case PS_DASHDOT:
149     case PS_DASHDOTDOT:
150         break;
151
152     case PS_USERSTYLE:
153         if (((INT)style_count) <= 0) return 0;
154
155         if ((style_count > 16) || !style_bits)
156         {
157             SetLastError(ERROR_INVALID_PARAMETER);
158             return 0;
159         }
160
161         if ((style & PS_TYPE_MASK) == PS_GEOMETRIC)
162         {
163             UINT i;
164             BOOL has_neg = FALSE, all_zero = TRUE;
165
166             for(i = 0; (i < style_count) && !has_neg; i++)
167             {
168                 has_neg = has_neg || (((INT)(style_bits[i])) < 0);
169                 all_zero = all_zero && (style_bits[i] == 0);
170             }
171
172             if(all_zero || has_neg)
173             {
174                 SetLastError(ERROR_INVALID_PARAMETER);
175                 return 0;
176             }
177         }
178         break;
179
180     case PS_INSIDEFRAME:  /* applicable only for geometric pens */
181         if ((style & PS_TYPE_MASK) != PS_GEOMETRIC)
182         {
183             SetLastError(ERROR_INVALID_PARAMETER);
184             return 0;
185         }
186         break;
187
188     case PS_ALTERNATE:  /* applicable only for cosmetic pens */
189         if ((style & PS_TYPE_MASK) == PS_GEOMETRIC)
190         {
191             SetLastError(ERROR_INVALID_PARAMETER);
192             return 0;
193         }
194         break;
195
196     default:
197         SetLastError(ERROR_INVALID_PARAMETER);
198         return 0;
199     }
200
201     if ((style & PS_TYPE_MASK) == PS_GEOMETRIC)
202     {
203         if (brush->lbHatch && ((brush->lbStyle != BS_SOLID) && (brush->lbStyle != BS_HOLLOW)))
204         {
205             static int fixme_hatches_shown;
206             if (!fixme_hatches_shown++) FIXME("Hatches not implemented\n");
207         }
208     }
209     else
210     {
211         if (width != 1)
212         {
213             SetLastError(ERROR_INVALID_PARAMETER);
214             return 0;
215         }
216     }
217
218     if (!(penPtr = HeapAlloc(GetProcessHeap(), 0, FIELD_OFFSET(PENOBJ,logpen.elpStyleEntry[style_count]))))
219         return 0;
220
221     penPtr->logpen.elpPenStyle = style;
222     penPtr->logpen.elpWidth = abs(width);
223     penPtr->logpen.elpBrushStyle = brush->lbStyle;
224     penPtr->logpen.elpColor = brush->lbColor;
225     penPtr->logpen.elpHatch = brush->lbHatch;
226     penPtr->logpen.elpNumEntries = style_count;
227     memcpy(penPtr->logpen.elpStyleEntry, style_bits, style_count * sizeof(DWORD));
228
229     if (!(hpen = alloc_gdi_handle( &penPtr->header, OBJ_EXTPEN, &pen_funcs )))
230         HeapFree( GetProcessHeap(), 0, penPtr );
231     return hpen;
232 }
233
234 /***********************************************************************
235  *           PEN_SelectObject
236  */
237 static HGDIOBJ PEN_SelectObject( HGDIOBJ handle, HDC hdc )
238 {
239     PHYSDEV physdev;
240     HGDIOBJ ret = 0;
241     DC *dc = get_dc_ptr( hdc );
242
243     if (!dc)
244     {
245         SetLastError( ERROR_INVALID_HANDLE );
246         return 0;
247     }
248
249     if (!GDI_inc_ref_count( handle ))
250     {
251         release_dc_ptr( dc );
252         return 0;
253     }
254
255     physdev = GET_DC_PHYSDEV( dc, pSelectPen );
256     if (!physdev->funcs->pSelectPen( physdev, handle ))
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     release_dc_ptr( dc );
267     return ret;
268 }
269
270
271 /***********************************************************************
272  *           PEN_DeleteObject
273  */
274 static BOOL PEN_DeleteObject( HGDIOBJ handle )
275 {
276     PENOBJ *pen = free_gdi_handle( handle );
277
278     if (!pen) return FALSE;
279     return HeapFree( GetProcessHeap(), 0, pen );
280 }
281
282
283 /***********************************************************************
284  *           PEN_GetObject
285  */
286 static INT PEN_GetObject( HGDIOBJ handle, INT count, LPVOID buffer )
287 {
288     PENOBJ *pen = GDI_GetObjPtr( handle, 0 );
289     INT ret = 0;
290
291     if (!pen) return 0;
292
293     switch (pen->header.type)
294     {
295     case OBJ_PEN:
296     {
297         LOGPEN *lp;
298
299         if (!buffer) ret = sizeof(LOGPEN);
300         else if (count < sizeof(LOGPEN)) ret = 0;
301         else if ((pen->logpen.elpPenStyle & PS_STYLE_MASK) == PS_NULL && count == sizeof(EXTLOGPEN))
302         {
303             EXTLOGPEN *elp = buffer;
304             *elp = pen->logpen;
305             elp->elpWidth = 0;
306             ret = sizeof(EXTLOGPEN);
307         }
308         else
309         {
310             lp = buffer;
311             lp->lopnStyle = pen->logpen.elpPenStyle;
312             lp->lopnColor = pen->logpen.elpColor;
313             lp->lopnWidth.x = pen->logpen.elpWidth;
314             lp->lopnWidth.y = 0;
315             ret = sizeof(LOGPEN);
316         }
317         break;
318     }
319
320     case OBJ_EXTPEN:
321         ret = sizeof(EXTLOGPEN) + pen->logpen.elpNumEntries * sizeof(DWORD) - sizeof(pen->logpen.elpStyleEntry);
322         if (buffer)
323         {
324             if (count < ret) ret = 0;
325             else memcpy(buffer, &pen->logpen, ret);
326         }
327         break;
328     }
329     GDI_ReleaseObj( handle );
330     return ret;
331 }