gdiplus: Added GdipGetPenFillType.
[wine] / dlls / gdiplus / pen.c
1 /*
2  * Copyright (C) 2007 Google (Evan Stade)
3  *
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.
8  *
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.
13  *
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
17  */
18
19 #include <stdarg.h>
20
21 #include "windef.h"
22 #include "winbase.h"
23 #include "wingdi.h"
24
25 #include "objbase.h"
26
27 #include "gdiplus.h"
28 #include "gdiplus_private.h"
29 #include "wine/debug.h"
30
31 WINE_DEFAULT_DEBUG_CHANNEL(gdiplus);
32
33 static DWORD gdip_to_gdi_dash(GpDashStyle dash)
34 {
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;
51     }
52 }
53
54 static DWORD gdip_to_gdi_join(GpLineJoin join)
55 {
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;
67     }
68 }
69
70 static GpPenType bt_to_pt(GpBrushType bt)
71 {
72     switch(bt){
73         case BrushTypeSolidColor:
74             return PenTypeSolidColor;
75         case BrushTypeHatchFill:
76             return PenTypeHatchFill;
77         case BrushTypeTextureFill:
78             return PenTypeTextureFill;
79         case BrushTypePathGradient:
80             return PenTypePathGradient;
81         case BrushTypeLinearGradient:
82             return PenTypeLinearGradient;
83         default:
84             return PenTypeUnknown;
85     }
86 }
87
88 GpStatus WINGDIPAPI GdipClonePen(GpPen *pen, GpPen **clonepen)
89 {
90     TRACE("(%p, %p)\n", pen, clonepen);
91
92     if(!pen || !clonepen)
93         return InvalidParameter;
94
95     *clonepen = GdipAlloc(sizeof(GpPen));
96     if(!*clonepen)  return OutOfMemory;
97
98     **clonepen = *pen;
99
100     GdipCloneCustomLineCap(pen->customstart, &(*clonepen)->customstart);
101     GdipCloneCustomLineCap(pen->customend, &(*clonepen)->customend);
102     GdipCloneBrush(pen->brush, &(*clonepen)->brush);
103
104     return Ok;
105 }
106
107 GpStatus WINGDIPAPI GdipCreatePen1(ARGB color, REAL width, GpUnit unit,
108     GpPen **pen)
109 {
110     GpBrush *brush;
111     GpStatus status;
112
113     TRACE("(%x, %.2f, %d, %p)\n", color, width, unit, pen);
114
115     GdipCreateSolidFill(color, (GpSolidFill **)(&brush));
116     status = GdipCreatePen2(brush, width, unit, pen);
117     GdipDeleteBrush(brush);
118     return status;
119 }
120
121 GpStatus WINGDIPAPI GdipCreatePen2(GpBrush *brush, REAL width, GpUnit unit,
122     GpPen **pen)
123 {
124     GpPen *gp_pen;
125     GpBrush *clone_brush;
126
127     TRACE("(%p, %.2f, %d, %p)\n", brush, width, unit, pen);
128
129     if(!pen || !brush)
130         return InvalidParameter;
131
132     gp_pen = GdipAlloc(sizeof(GpPen));
133     if(!gp_pen)    return OutOfMemory;
134
135     gp_pen->style = GP_DEFAULT_PENSTYLE;
136     gp_pen->width = width;
137     gp_pen->unit = unit;
138     gp_pen->endcap = LineCapFlat;
139     gp_pen->join = LineJoinMiter;
140     gp_pen->miterlimit = 10.0;
141     gp_pen->dash = DashStyleSolid;
142     gp_pen->offset = 0.0;
143     gp_pen->customstart = NULL;
144     gp_pen->customend = NULL;
145
146     if(!((gp_pen->unit == UnitWorld) || (gp_pen->unit == UnitPixel))) {
147         FIXME("UnitWorld, UnitPixel only supported units\n");
148         GdipFree(gp_pen);
149         return NotImplemented;
150     }
151
152     GdipCloneBrush(brush, &clone_brush);
153     gp_pen->brush = clone_brush;
154
155     *pen = gp_pen;
156
157     return Ok;
158 }
159
160 GpStatus WINGDIPAPI GdipDeletePen(GpPen *pen)
161 {
162     TRACE("(%p)\n", pen);
163
164     if(!pen)    return InvalidParameter;
165
166     GdipDeleteBrush(pen->brush);
167     GdipDeleteCustomLineCap(pen->customstart);
168     GdipDeleteCustomLineCap(pen->customend);
169     GdipFree(pen->dashes);
170     GdipFree(pen);
171
172     return Ok;
173 }
174
175 GpStatus WINGDIPAPI GdipGetPenBrushFill(GpPen *pen, GpBrush **brush)
176 {
177     TRACE("(%p, %p)\n", pen, brush);
178
179     if(!pen || !brush)
180         return InvalidParameter;
181
182     return GdipCloneBrush(pen->brush, brush);
183 }
184
185 GpStatus WINGDIPAPI GdipGetPenColor(GpPen *pen, ARGB *argb)
186 {
187     TRACE("(%p, %p)\n", pen, argb);
188
189     if(!pen || !argb)
190         return InvalidParameter;
191
192     if(pen->brush->bt != BrushTypeSolidColor)
193         return NotImplemented;
194
195     return GdipGetSolidFillColor(((GpSolidFill*)pen->brush), argb);
196 }
197
198 GpStatus WINGDIPAPI GdipGetPenCustomEndCap(GpPen *pen, GpCustomLineCap** customCap)
199 {
200     TRACE("(%p, %p)\n", pen, customCap);
201
202     if(!pen || !customCap)
203         return InvalidParameter;
204
205     if(!pen->customend){
206         *customCap = NULL;
207         return Ok;
208     }
209
210     return GdipCloneCustomLineCap(pen->customend, customCap);
211 }
212
213 GpStatus WINGDIPAPI GdipGetPenCustomStartCap(GpPen *pen, GpCustomLineCap** customCap)
214 {
215     TRACE("(%p, %p)\n", pen, customCap);
216
217     if(!pen || !customCap)
218         return InvalidParameter;
219
220     if(!pen->customstart){
221         *customCap = NULL;
222         return Ok;
223     }
224
225     return GdipCloneCustomLineCap(pen->customstart, customCap);
226 }
227
228 GpStatus WINGDIPAPI GdipGetPenDashArray(GpPen *pen, REAL *dash, INT count)
229 {
230     TRACE("(%p, %p, %d)\n", pen, dash, count);
231
232     if(!pen || !dash || count > pen->numdashes)
233         return InvalidParameter;
234
235     /* note: if you pass a negative value for count, it crashes native gdiplus. */
236     if(count < 0)
237         return GenericError;
238
239     memcpy(dash, pen->dashes, count * sizeof(REAL));
240
241     return Ok;
242 }
243
244 GpStatus WINGDIPAPI GdipGetPenDashCap197819(GpPen *pen, GpDashCap *dashCap)
245 {
246     TRACE("(%p, %p)\n", pen, dashCap);
247
248     if(!pen || !dashCap)
249         return InvalidParameter;
250
251     *dashCap = pen->dashcap;
252
253     return Ok;
254 }
255
256 GpStatus WINGDIPAPI GdipGetPenDashCount(GpPen *pen, INT *count)
257 {
258     TRACE("(%p, %p)\n", pen, count);
259
260     if(!pen || !count)
261         return InvalidParameter;
262
263     *count = pen->numdashes;
264
265     return Ok;
266 }
267
268 GpStatus WINGDIPAPI GdipGetPenDashOffset(GpPen *pen, REAL *offset)
269 {
270     TRACE("(%p, %p)\n", pen, offset);
271
272     if(!pen || !offset)
273         return InvalidParameter;
274
275     *offset = pen->offset;
276
277     return Ok;
278 }
279
280 GpStatus WINGDIPAPI GdipGetPenDashStyle(GpPen *pen, GpDashStyle *dash)
281 {
282     TRACE("(%p, %p)\n", pen, dash);
283
284     if(!pen || !dash)
285         return InvalidParameter;
286
287     *dash = pen->dash;
288
289     return Ok;
290 }
291
292 GpStatus WINGDIPAPI GdipGetPenEndCap(GpPen *pen, GpLineCap *endCap)
293 {
294     TRACE("(%p, %p)\n", pen, endCap);
295
296     if(!pen || !endCap)
297         return InvalidParameter;
298
299     *endCap = pen->endcap;
300
301     return Ok;
302 }
303
304 GpStatus WINGDIPAPI GdipGetPenFillType(GpPen *pen, GpPenType* type)
305 {
306     TRACE("(%p, %p)\n", pen, type);
307
308     if(!pen || !type)
309         return InvalidParameter;
310
311     *type = bt_to_pt(pen->brush->bt);
312
313     return Ok;
314 }
315
316 GpStatus WINGDIPAPI GdipGetPenLineJoin(GpPen *pen, GpLineJoin *lineJoin)
317 {
318     TRACE("(%p, %p)\n", pen, lineJoin);
319
320     if(!pen || !lineJoin)
321         return InvalidParameter;
322
323     *lineJoin = pen->join;
324
325     return Ok;
326 }
327
328 GpStatus WINGDIPAPI GdipGetPenMode(GpPen *pen, GpPenAlignment *mode)
329 {
330     TRACE("(%p, %p)\n", pen, mode);
331
332     if(!pen || !mode)
333         return InvalidParameter;
334
335     *mode = pen->align;
336
337     return Ok;
338 }
339
340 GpStatus WINGDIPAPI GdipGetPenMiterLimit(GpPen *pen, REAL *miterLimit)
341 {
342     TRACE("(%p, %p)\n", pen, miterLimit);
343
344     if(!pen || !miterLimit)
345         return InvalidParameter;
346
347     *miterLimit = pen->miterlimit;
348
349     return Ok;
350 }
351
352 GpStatus WINGDIPAPI GdipGetPenStartCap(GpPen *pen, GpLineCap *startCap)
353 {
354     TRACE("(%p, %p)\n", pen, startCap);
355
356     if(!pen || !startCap)
357         return InvalidParameter;
358
359     *startCap = pen->startcap;
360
361     return Ok;
362 }
363
364 GpStatus WINGDIPAPI GdipGetPenUnit(GpPen *pen, GpUnit *unit)
365 {
366     TRACE("(%p, %p)\n", pen, unit);
367
368     if(!pen || !unit)
369         return InvalidParameter;
370
371     *unit = pen->unit;
372
373     return Ok;
374 }
375
376 GpStatus WINGDIPAPI GdipGetPenWidth(GpPen *pen, REAL *width)
377 {
378     TRACE("(%p, %p)\n", pen, width);
379
380     if(!pen || !width)
381         return InvalidParameter;
382
383     *width = pen->width;
384
385     return Ok;
386 }
387
388 GpStatus WINGDIPAPI GdipSetPenBrushFill(GpPen *pen, GpBrush *brush)
389 {
390     TRACE("(%p, %p)\n", pen, brush);
391
392     if(!pen || !brush)
393         return InvalidParameter;
394
395     GdipDeleteBrush(pen->brush);
396     return GdipCloneBrush(brush, &pen->brush);
397 }
398
399 GpStatus WINGDIPAPI GdipSetPenColor(GpPen *pen, ARGB argb)
400 {
401     TRACE("(%p, %x)\n", pen, argb);
402
403     if(!pen)
404         return InvalidParameter;
405
406     if(pen->brush->bt != BrushTypeSolidColor)
407         return NotImplemented;
408
409     return GdipSetSolidFillColor(((GpSolidFill*)pen->brush), argb);
410 }
411
412 GpStatus WINGDIPAPI GdipSetPenCustomEndCap(GpPen *pen, GpCustomLineCap* customCap)
413 {
414     GpCustomLineCap * cap;
415     GpStatus ret;
416
417     TRACE("(%p, %p)\n", pen, customCap);
418
419     /* native crashes on pen == NULL, customCap != NULL */
420     if(!customCap) return InvalidParameter;
421
422     if((ret = GdipCloneCustomLineCap(customCap, &cap)) == Ok){
423         GdipDeleteCustomLineCap(pen->customend);
424         pen->endcap = LineCapCustom;
425         pen->customend = cap;
426     }
427
428     return ret;
429 }
430
431 GpStatus WINGDIPAPI GdipSetPenCustomStartCap(GpPen *pen, GpCustomLineCap* customCap)
432 {
433     GpCustomLineCap * cap;
434     GpStatus ret;
435
436     TRACE("(%p, %p)\n", pen, customCap);
437
438     /* native crashes on pen == NULL, customCap != NULL */
439     if(!customCap) return InvalidParameter;
440
441     if((ret = GdipCloneCustomLineCap(customCap, &cap)) == Ok){
442         GdipDeleteCustomLineCap(pen->customstart);
443         pen->startcap = LineCapCustom;
444         pen->customstart = cap;
445     }
446
447     return ret;
448 }
449
450 GpStatus WINGDIPAPI GdipSetPenDashArray(GpPen *pen, GDIPCONST REAL *dash,
451     INT count)
452 {
453     INT i;
454     REAL sum = 0;
455
456     TRACE("(%p, %p, %d)\n", pen, dash, count);
457
458     if(!pen || !dash)
459         return InvalidParameter;
460
461     if(count <= 0)
462         return OutOfMemory;
463
464     for(i = 0; i < count; i++){
465         sum += dash[i];
466         if(dash[i] < 0.0)
467             return InvalidParameter;
468     }
469
470     if(sum == 0.0 && count)
471         return InvalidParameter;
472
473     GdipFree(pen->dashes);
474     pen->dashes = NULL;
475
476     if(count > 0)
477         pen->dashes = GdipAlloc(count * sizeof(REAL));
478     if(!pen->dashes){
479         pen->numdashes = 0;
480         return OutOfMemory;
481     }
482
483     GdipSetPenDashStyle(pen, DashStyleCustom);
484     memcpy(pen->dashes, dash, count * sizeof(REAL));
485     pen->numdashes = count;
486
487     return Ok;
488 }
489
490 GpStatus WINGDIPAPI GdipSetPenDashCap197819(GpPen *pen, GpDashCap dashCap)
491 {
492     TRACE("(%p, %d)\n", pen, dashCap);
493
494     if(!pen)
495         return InvalidParameter;
496
497     pen->dashcap = dashCap;
498
499     return Ok;
500 }
501
502 /* FIXME: dash offset not used */
503 GpStatus WINGDIPAPI GdipSetPenDashOffset(GpPen *pen, REAL offset)
504 {
505     TRACE("(%p, %.2f)\n", pen, offset);
506
507     if(!pen)
508         return InvalidParameter;
509
510     pen->offset = offset;
511
512     return Ok;
513 }
514
515 GpStatus WINGDIPAPI GdipSetPenDashStyle(GpPen *pen, GpDashStyle dash)
516 {
517     TRACE("(%p, %d)\n", pen, dash);
518
519     if(!pen)
520         return InvalidParameter;
521
522     if(dash != DashStyleCustom){
523         GdipFree(pen->dashes);
524         pen->dashes = NULL;
525         pen->numdashes = 0;
526     }
527
528     pen->dash = dash;
529     pen->style &= ~(PS_ALTERNATE | PS_SOLID | PS_DASH | PS_DOT | PS_DASHDOT |
530                     PS_DASHDOTDOT | PS_NULL | PS_USERSTYLE | PS_INSIDEFRAME);
531     pen->style |= gdip_to_gdi_dash(dash);
532
533     return Ok;
534 }
535
536 GpStatus WINGDIPAPI GdipSetPenEndCap(GpPen *pen, GpLineCap cap)
537 {
538     TRACE("(%p, %d)\n", pen, cap);
539
540     if(!pen)    return InvalidParameter;
541
542     /* The old custom cap gets deleted even if the new style is LineCapCustom. */
543     GdipDeleteCustomLineCap(pen->customend);
544     pen->customend = NULL;
545     pen->endcap = cap;
546
547     return Ok;
548 }
549
550 /* FIXME: startcap, dashcap not used. */
551 GpStatus WINGDIPAPI GdipSetPenLineCap197819(GpPen *pen, GpLineCap start,
552     GpLineCap end, GpDashCap dash)
553 {
554     TRACE("%p, %d, %d, %d)\n", pen, start, end, dash);
555
556     if(!pen)
557         return InvalidParameter;
558
559     GdipDeleteCustomLineCap(pen->customend);
560     GdipDeleteCustomLineCap(pen->customstart);
561     pen->customend = NULL;
562     pen->customstart = NULL;
563
564     pen->startcap = start;
565     pen->endcap = end;
566     pen->dashcap = dash;
567
568     return Ok;
569 }
570
571 /* FIXME: Miter line joins behave a bit differently than they do in windows.
572  * Both kinds of miter joins clip if the angle is less than 11 degrees. */
573 GpStatus WINGDIPAPI GdipSetPenLineJoin(GpPen *pen, GpLineJoin join)
574 {
575     TRACE("(%p, %d)\n", pen, join);
576
577     if(!pen)    return InvalidParameter;
578
579     pen->join = join;
580     pen->style &= ~(PS_JOIN_ROUND | PS_JOIN_BEVEL | PS_JOIN_MITER);
581     pen->style |= gdip_to_gdi_join(join);
582
583     return Ok;
584 }
585
586 GpStatus WINGDIPAPI GdipSetPenMiterLimit(GpPen *pen, REAL limit)
587 {
588     TRACE("(%p, %.2f)\n", pen, limit);
589
590     if(!pen)
591         return InvalidParameter;
592
593     pen->miterlimit = limit;
594
595     return Ok;
596 }
597
598 GpStatus WINGDIPAPI GdipSetPenStartCap(GpPen *pen, GpLineCap cap)
599 {
600     TRACE("(%p, %d)\n", pen, cap);
601
602     if(!pen)    return InvalidParameter;
603
604     GdipDeleteCustomLineCap(pen->customstart);
605     pen->customstart = NULL;
606     pen->startcap = cap;
607
608     return Ok;
609 }
610
611 GpStatus WINGDIPAPI GdipSetPenWidth(GpPen *pen, REAL width)
612 {
613     TRACE("(%p, %.2f)\n", pen, width);
614
615     if(!pen)    return InvalidParameter;
616
617     pen->width = width;
618
619     return Ok;
620 }
621
622 GpStatus WINGDIPAPI GdipSetPenMode(GpPen *pen, GpPenAlignment mode)
623 {
624     TRACE("(%p, %d)\n", pen, mode);
625
626     if(!pen)    return InvalidParameter;
627
628     pen->align = mode;
629
630     return Ok;
631 }