mshtml: Use heap_alloc_zero in HTMLElementCollection_Create.
[wine/gsoc_dplay.git] / dlls / gdiplus / pen.c
blob9ef14827cbf4c8c9cc4d8f7bbe6f000efe5df72b
1 /*
2 * Copyright (C) 2007 Google (Evan Stade)
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 #include <stdarg.h>
21 #include "windef.h"
22 #include "winbase.h"
23 #include "wingdi.h"
25 #include "objbase.h"
27 #include "gdiplus.h"
28 #include "gdiplus_private.h"
29 #include "wine/debug.h"
31 WINE_DEFAULT_DEBUG_CHANNEL(gdiplus);
33 static DWORD gdip_to_gdi_dash(GpDashStyle dash)
35 switch(dash){
36 case DashStyleSolid:
37 return PS_SOLID;
38 case DashStyleDash:
39 return PS_DASH;
40 case DashStyleDot:
41 return PS_DOT;
42 case DashStyleDashDot:
43 return PS_DASHDOT;
44 case DashStyleDashDotDot:
45 return PS_DASHDOTDOT;
46 case DashStyleCustom:
47 return PS_USERSTYLE;
48 default:
49 ERR("Not a member of GpDashStyle enumeration\n");
50 return 0;
54 static DWORD gdip_to_gdi_join(GpLineJoin join)
56 switch(join){
57 case LineJoinRound:
58 return PS_JOIN_ROUND;
59 case LineJoinBevel:
60 return PS_JOIN_BEVEL;
61 case LineJoinMiter:
62 case LineJoinMiterClipped:
63 return PS_JOIN_MITER;
64 default:
65 ERR("Not a member of GpLineJoin enumeration\n");
66 return 0;
70 GpStatus WINGDIPAPI GdipClonePen(GpPen *pen, GpPen **clonepen)
72 if(!pen || !clonepen)
73 return InvalidParameter;
75 *clonepen = GdipAlloc(sizeof(GpPen));
76 if(!*clonepen) return OutOfMemory;
78 **clonepen = *pen;
80 GdipCloneCustomLineCap(pen->customstart, &(*clonepen)->customstart);
81 GdipCloneCustomLineCap(pen->customend, &(*clonepen)->customend);
82 GdipCloneBrush(pen->brush, &(*clonepen)->brush);
84 return Ok;
87 GpStatus WINGDIPAPI GdipCreatePen1(ARGB color, REAL width, GpUnit unit,
88 GpPen **pen)
90 GpBrush *brush;
91 GdipCreateSolidFill(color, (GpSolidFill **)(&brush));
92 return GdipCreatePen2(brush, width, unit, pen);
95 GpStatus WINGDIPAPI GdipCreatePen2(GpBrush *brush, REAL width, GpUnit unit,
96 GpPen **pen)
98 GpPen *gp_pen;
100 if(!pen || !brush)
101 return InvalidParameter;
103 gp_pen = GdipAlloc(sizeof(GpPen));
104 if(!gp_pen) return OutOfMemory;
106 gp_pen->style = GP_DEFAULT_PENSTYLE;
107 gp_pen->width = width;
108 gp_pen->unit = unit;
109 gp_pen->endcap = LineCapFlat;
110 gp_pen->join = LineJoinMiter;
111 gp_pen->miterlimit = 10.0;
112 gp_pen->dash = DashStyleSolid;
113 gp_pen->offset = 0.0;
114 gp_pen->brush = brush;
116 if(!((gp_pen->unit == UnitWorld) || (gp_pen->unit == UnitPixel))) {
117 FIXME("UnitWorld, UnitPixel only supported units\n");
118 GdipFree(gp_pen);
119 return NotImplemented;
122 *pen = gp_pen;
124 return Ok;
127 GpStatus WINGDIPAPI GdipDeletePen(GpPen *pen)
129 if(!pen) return InvalidParameter;
131 GdipDeleteBrush(pen->brush);
132 GdipDeleteCustomLineCap(pen->customstart);
133 GdipDeleteCustomLineCap(pen->customend);
134 GdipFree(pen->dashes);
135 GdipFree(pen);
137 return Ok;
140 GpStatus WINGDIPAPI GdipGetPenBrushFill(GpPen *pen, GpBrush **brush)
142 if(!pen || !brush)
143 return InvalidParameter;
145 return GdipCloneBrush(pen->brush, brush);
148 GpStatus WINGDIPAPI GdipGetPenColor(GpPen *pen, ARGB *argb)
150 if(!pen || !argb)
151 return InvalidParameter;
153 if(pen->brush->bt != BrushTypeSolidColor)
154 return NotImplemented;
156 return GdipGetSolidFillColor(((GpSolidFill*)pen->brush), argb);
159 GpStatus WINGDIPAPI GdipGetPenDashArray(GpPen *pen, REAL *dash, INT count)
161 if(!pen || !dash || count > pen->numdashes)
162 return InvalidParameter;
164 /* note: if you pass a negative value for count, it crashes native gdiplus. */
165 if(count < 0)
166 return GenericError;
168 memcpy(dash, pen->dashes, count * sizeof(REAL));
170 return Ok;
173 GpStatus WINGDIPAPI GdipGetPenDashOffset(GpPen *pen, REAL *offset)
175 if(!pen || !offset)
176 return InvalidParameter;
178 *offset = pen->offset;
180 return Ok;
183 GpStatus WINGDIPAPI GdipGetPenDashStyle(GpPen *pen, GpDashStyle *dash)
185 if(!pen || !dash)
186 return InvalidParameter;
188 *dash = pen->dash;
190 return Ok;
193 GpStatus WINGDIPAPI GdipSetPenBrushFill(GpPen *pen, GpBrush *brush)
195 if(!pen || !brush)
196 return InvalidParameter;
198 GdipDeleteBrush(pen->brush);
199 return GdipCloneBrush(brush, &pen->brush);
202 GpStatus WINGDIPAPI GdipSetPenColor(GpPen *pen, ARGB argb)
204 if(!pen)
205 return InvalidParameter;
207 if(pen->brush->bt != BrushTypeSolidColor)
208 return NotImplemented;
210 return GdipSetSolidFillColor(((GpSolidFill*)pen->brush), argb);
213 GpStatus WINGDIPAPI GdipSetPenCustomEndCap(GpPen *pen, GpCustomLineCap* customCap)
215 GpCustomLineCap * cap;
216 GpStatus ret;
218 if(!pen || !customCap) return InvalidParameter;
220 if((ret = GdipCloneCustomLineCap(customCap, &cap)) == Ok){
221 GdipDeleteCustomLineCap(pen->customend);
222 pen->endcap = LineCapCustom;
223 pen->customend = cap;
226 return ret;
229 GpStatus WINGDIPAPI GdipSetPenCustomStartCap(GpPen *pen, GpCustomLineCap* customCap)
231 GpCustomLineCap * cap;
232 GpStatus ret;
234 if(!pen || !customCap) return InvalidParameter;
236 if((ret = GdipCloneCustomLineCap(customCap, &cap)) == Ok){
237 GdipDeleteCustomLineCap(pen->customstart);
238 pen->startcap = LineCapCustom;
239 pen->customstart = cap;
242 return ret;
245 GpStatus WINGDIPAPI GdipSetPenDashArray(GpPen *pen, GDIPCONST REAL *dash,
246 INT count)
248 INT i;
249 REAL sum = 0;
251 if(!pen || !dash)
252 return InvalidParameter;
254 if(count <= 0)
255 return OutOfMemory;
257 for(i = 0; i < count; i++){
258 sum += dash[i];
259 if(dash[i] < 0.0)
260 return InvalidParameter;
263 if(sum == 0.0 && count)
264 return InvalidParameter;
266 GdipFree(pen->dashes);
267 pen->dashes = NULL;
269 if(count > 0)
270 pen->dashes = GdipAlloc(count * sizeof(REAL));
271 if(!pen->dashes){
272 pen->numdashes = 0;
273 return OutOfMemory;
276 GdipSetPenDashStyle(pen, DashStyleCustom);
277 memcpy(pen->dashes, dash, count * sizeof(REAL));
278 pen->numdashes = count;
280 return Ok;
283 /* FIXME: dash offset not used */
284 GpStatus WINGDIPAPI GdipSetPenDashOffset(GpPen *pen, REAL offset)
286 if(!pen)
287 return InvalidParameter;
289 pen->offset = offset;
291 return Ok;
294 GpStatus WINGDIPAPI GdipSetPenDashStyle(GpPen *pen, GpDashStyle dash)
296 if(!pen)
297 return InvalidParameter;
299 if(dash != DashStyleCustom){
300 GdipFree(pen->dashes);
301 pen->dashes = NULL;
302 pen->numdashes = 0;
305 pen->dash = dash;
306 pen->style &= ~(PS_ALTERNATE | PS_SOLID | PS_DASH | PS_DOT | PS_DASHDOT |
307 PS_DASHDOTDOT | PS_NULL | PS_USERSTYLE | PS_INSIDEFRAME);
308 pen->style |= gdip_to_gdi_dash(dash);
310 return Ok;
313 GpStatus WINGDIPAPI GdipSetPenEndCap(GpPen *pen, GpLineCap cap)
315 if(!pen) return InvalidParameter;
317 /* The old custom cap gets deleted even if the new style is LineCapCustom. */
318 GdipDeleteCustomLineCap(pen->customend);
319 pen->customend = NULL;
320 pen->endcap = cap;
322 return Ok;
325 /* FIXME: startcap, dashcap not used. */
326 GpStatus WINGDIPAPI GdipSetPenLineCap197819(GpPen *pen, GpLineCap start,
327 GpLineCap end, GpDashCap dash)
329 if(!pen)
330 return InvalidParameter;
332 GdipDeleteCustomLineCap(pen->customend);
333 GdipDeleteCustomLineCap(pen->customstart);
334 pen->customend = NULL;
335 pen->customstart = NULL;
337 pen->startcap = start;
338 pen->endcap = end;
339 pen->dashcap = dash;
341 return Ok;
344 /* FIXME: Miter line joins behave a bit differently than they do in windows.
345 * Both kinds of miter joins clip if the angle is less than 11 degrees. */
346 GpStatus WINGDIPAPI GdipSetPenLineJoin(GpPen *pen, GpLineJoin join)
348 if(!pen) return InvalidParameter;
350 pen->join = join;
351 pen->style &= ~(PS_JOIN_ROUND | PS_JOIN_BEVEL | PS_JOIN_MITER);
352 pen->style |= gdip_to_gdi_join(join);
354 return Ok;
357 GpStatus WINGDIPAPI GdipSetPenMiterLimit(GpPen *pen, REAL limit)
359 if(!pen)
360 return InvalidParameter;
362 pen->miterlimit = limit;
364 return Ok;
367 GpStatus WINGDIPAPI GdipSetPenStartCap(GpPen *pen, GpLineCap cap)
369 if(!pen) return InvalidParameter;
371 GdipDeleteCustomLineCap(pen->customstart);
372 pen->customstart = NULL;
373 pen->startcap = cap;
375 return Ok;
378 GpStatus WINGDIPAPI GdipSetPenWidth(GpPen *pen, REAL width)
380 if(!pen) return InvalidParameter;
382 pen->width = width;
384 return Ok;
388 GpStatus WINGDIPAPI GdipSetPenMode(GpPen *pen, GpPenAlignment penAlignment)
390 if(!pen) return InvalidParameter;
392 FIXME("stub (%d)\n", penAlignment);
394 return Ok;