gdi32: Don't hold the gdi lock while calling DC driver functions.
[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 "wine/wingdi16.h"
32 #include "gdi_private.h"
33 #include "wine/debug.h"
34
35 WINE_DEFAULT_DEBUG_CHANNEL(gdi);
36
37   /* GDI logical pen object */
38 typedef struct
39 {
40     GDIOBJHDR header;
41     EXTLOGPEN logpen;
42 } PENOBJ;
43
44
45 static HGDIOBJ PEN_SelectObject( HGDIOBJ handle, void *obj, HDC hdc );
46 static INT PEN_GetObject16( HGDIOBJ handle, void *obj, INT count, LPVOID buffer );
47 static INT PEN_GetObject( HGDIOBJ handle, void *obj, INT count, LPVOID buffer );
48
49 static const struct gdi_obj_funcs pen_funcs =
50 {
51     PEN_SelectObject,  /* pSelectObject */
52     PEN_GetObject16,   /* pGetObject16 */
53     PEN_GetObject,     /* pGetObjectA */
54     PEN_GetObject,     /* pGetObjectW */
55     NULL,              /* pUnrealizeObject */
56     GDI_FreeObject     /* pDeleteObject */
57 };
58
59
60 /***********************************************************************
61  *           CreatePen    (GDI32.@)
62  */
63 HPEN WINAPI CreatePen( INT style, INT width, COLORREF color )
64 {
65     LOGPEN logpen;
66
67     TRACE("%d %d %06x\n", style, width, color );
68
69     logpen.lopnStyle = style;
70     logpen.lopnWidth.x = width;
71     logpen.lopnWidth.y = 0;
72     logpen.lopnColor = color;
73
74     return CreatePenIndirect( &logpen );
75 }
76
77
78 /***********************************************************************
79  *           CreatePenIndirect    (GDI32.@)
80  */
81 HPEN WINAPI CreatePenIndirect( const LOGPEN * pen )
82 {
83     PENOBJ * penPtr;
84     HPEN hpen;
85
86     if (!(penPtr = GDI_AllocObject( sizeof(PENOBJ), PEN_MAGIC, (HGDIOBJ *)&hpen,
87                                     &pen_funcs ))) return 0;
88     if (pen->lopnStyle == PS_USERSTYLE || pen->lopnStyle == PS_ALTERNATE)
89         penPtr->logpen.elpPenStyle = PS_SOLID;
90     else
91         penPtr->logpen.elpPenStyle = pen->lopnStyle;
92     if (pen->lopnStyle == PS_NULL)
93     {
94         penPtr->logpen.elpWidth = 1;
95         penPtr->logpen.elpColor = RGB(0, 0, 0);
96     }
97     else
98     {
99         penPtr->logpen.elpWidth = abs(pen->lopnWidth.x);
100         penPtr->logpen.elpColor = pen->lopnColor;
101     }
102     penPtr->logpen.elpBrushStyle = BS_SOLID;
103     penPtr->logpen.elpHatch = 0;
104     penPtr->logpen.elpNumEntries = 0;
105     penPtr->logpen.elpStyleEntry[0] = 0;
106
107     GDI_ReleaseObj( hpen );
108     return hpen;
109 }
110
111 /***********************************************************************
112  *           ExtCreatePen    (GDI32.@)
113  *
114  * FIXME: PS_USERSTYLE not handled
115  */
116
117 HPEN WINAPI ExtCreatePen( DWORD style, DWORD width,
118                               const LOGBRUSH * brush, DWORD style_count,
119                               const DWORD *style_bits )
120 {
121     PENOBJ * penPtr;
122     HPEN hpen;
123
124     if ((style & PS_STYLE_MASK) == PS_USERSTYLE)
125     {
126         if(((INT)style_count) <= 0)
127             return 0;
128
129         if ((style_count > 16) || !style_bits)
130         {
131             SetLastError(ERROR_INVALID_PARAMETER);
132             return 0;
133         }
134
135         if ((style & PS_TYPE_MASK) == PS_COSMETIC)
136         {
137             /* FIXME: PS_USERSTYLE workaround */
138             FIXME("PS_COSMETIC | PS_USERSTYLE not handled\n");
139             style = (style & ~PS_STYLE_MASK) | PS_SOLID;
140         }
141         else
142         {
143             UINT i;
144             BOOL has_neg = FALSE, all_zero = TRUE;
145
146             for(i = 0; (i < style_count) && !has_neg; i++)
147             {
148                 has_neg = has_neg || (((INT)(style_bits[i])) < 0);
149                 all_zero = all_zero && (style_bits[i] == 0);
150             }
151
152             if(all_zero || has_neg)
153             {
154                 SetLastError(ERROR_INVALID_PARAMETER);
155                 return 0;
156             }
157         }
158     }
159     else
160     {
161         if (style_count || style_bits)
162         {
163             SetLastError(ERROR_INVALID_PARAMETER);
164             return 0;
165         }
166     }
167
168     if ((style & PS_STYLE_MASK) == PS_NULL)
169         return CreatePen( PS_NULL, 0, brush->lbColor );
170
171     if ((style & PS_TYPE_MASK) == PS_GEOMETRIC)
172     {
173         /* PS_ALTERNATE is applicable only for cosmetic pens */
174         if ((style & PS_STYLE_MASK) == PS_ALTERNATE)
175         {
176             SetLastError(ERROR_INVALID_PARAMETER);
177             return 0;
178         }
179
180         if (brush->lbHatch && ((brush->lbStyle == BS_SOLID) || (brush->lbStyle == BS_HOLLOW)))
181         {
182             static int fixme_hatches_shown;
183             if (!fixme_hatches_shown++) FIXME("Hatches not implemented\n");
184         }
185     }
186     else
187     {
188         /* PS_INSIDEFRAME is applicable only for gemetric pens */
189         if ((style & PS_STYLE_MASK) == PS_INSIDEFRAME || width != 1)
190         {
191             SetLastError(ERROR_INVALID_PARAMETER);
192             return 0;
193         }
194     }
195
196     if (!(penPtr = GDI_AllocObject( sizeof(PENOBJ) +
197                                     style_count * sizeof(DWORD) - sizeof(penPtr->logpen.elpStyleEntry),
198                                     EXT_PEN_MAGIC, (HGDIOBJ *)&hpen,
199                                     &pen_funcs ))) return 0;
200
201     penPtr->logpen.elpPenStyle = style;
202     penPtr->logpen.elpWidth = abs(width);
203     penPtr->logpen.elpBrushStyle = brush->lbStyle;
204     penPtr->logpen.elpColor = brush->lbColor;
205     penPtr->logpen.elpHatch = brush->lbHatch;
206     penPtr->logpen.elpNumEntries = style_count;
207     memcpy(penPtr->logpen.elpStyleEntry, style_bits, style_count * sizeof(DWORD));
208     
209     GDI_ReleaseObj( hpen );
210
211     return hpen;
212 }
213
214
215 /***********************************************************************
216  *           PEN_SelectObject
217  */
218 static HGDIOBJ PEN_SelectObject( HGDIOBJ handle, void *obj, HDC hdc )
219 {
220     HGDIOBJ ret;
221     DC *dc = DC_GetDCPtr( hdc );
222
223     if (!dc) return 0;
224     ret = dc->hPen;
225     if (dc->funcs->pSelectPen) handle = dc->funcs->pSelectPen( dc->physDev, handle );
226     if (handle) dc->hPen = handle;
227     else ret = 0;
228     DC_ReleaseDCPtr( dc );
229     return ret;
230 }
231
232
233 /***********************************************************************
234  *           PEN_GetObject16
235  */
236 static INT PEN_GetObject16( HGDIOBJ handle, void *obj, INT count, LPVOID buffer )
237 {
238     PENOBJ *pen = obj;
239     LOGPEN16 *logpen;
240
241     if (!buffer) return sizeof(LOGPEN16);
242
243     if (count < sizeof(LOGPEN16)) return 0;
244
245     logpen = buffer;
246     logpen->lopnStyle = pen->logpen.elpPenStyle;
247     logpen->lopnColor = pen->logpen.elpColor;
248     logpen->lopnWidth.x = pen->logpen.elpWidth;
249     logpen->lopnWidth.y = 0;
250
251     return sizeof(LOGPEN16);
252 }
253
254
255 /***********************************************************************
256  *           PEN_GetObject
257  */
258 static INT PEN_GetObject( HGDIOBJ handle, void *obj, INT count, LPVOID buffer )
259 {
260     PENOBJ *pen = obj;
261
262     switch (GDIMAGIC(pen->header.wMagic))
263     {
264     case PEN_MAGIC:
265     {
266         LOGPEN *lp;
267
268         if (!buffer) return sizeof(LOGPEN);
269
270         if (count < sizeof(LOGPEN)) return 0;
271
272         if ((pen->logpen.elpPenStyle & PS_STYLE_MASK) == PS_NULL &&
273             count == sizeof(EXTLOGPEN))
274         {
275             EXTLOGPEN *elp = buffer;
276             memcpy(elp, &pen->logpen, sizeof(EXTLOGPEN));
277             elp->elpWidth = 0;
278             return sizeof(EXTLOGPEN);
279         }
280
281         lp = buffer;
282         lp->lopnStyle = pen->logpen.elpPenStyle;
283         lp->lopnColor = pen->logpen.elpColor;
284         lp->lopnWidth.x = pen->logpen.elpWidth;
285         lp->lopnWidth.y = 0;
286         return sizeof(LOGPEN);
287     }
288
289     case EXT_PEN_MAGIC:
290     {
291         INT size = sizeof(EXTLOGPEN) + pen->logpen.elpNumEntries * sizeof(DWORD) - sizeof(pen->logpen.elpStyleEntry);
292
293         if (!buffer) return size;
294
295         if (count < size) return 0;
296         memcpy(buffer, &pen->logpen, size);
297         return size;
298     }
299
300     default:
301         break;
302     }
303     assert(0);
304     return 0;
305 }