rpcrt4: Try a lot harder to resuse existing connections by comparing inside the RpcQu...
[wine] / dlls / gdiplus / graphics.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 #include <math.h>
21
22 #include "windef.h"
23 #include "winbase.h"
24 #include "winuser.h"
25 #include "wingdi.h"
26 #include "gdiplus.h"
27 #include "gdiplus_private.h"
28 #include "wine/debug.h"
29
30 /* looks-right constant */
31 #define TENSION_CONST (0.3)
32
33 static inline INT roundr(REAL x)
34 {
35     return (INT) floor(x+0.5);
36 }
37
38 static inline REAL deg2rad(REAL degrees)
39 {
40     return (M_PI*2.0) * degrees / 360.0;
41 }
42
43 /* Converts angle (in degrees) to x/y coordinates */
44 static void deg2xy(REAL angle, REAL x_0, REAL y_0, REAL *x, REAL *y)
45 {
46     REAL radAngle, hypotenuse;
47
48     radAngle = deg2rad(angle);
49     hypotenuse = 50.0; /* arbitrary */
50
51     *x = x_0 + cos(radAngle) * hypotenuse;
52     *y = y_0 + sin(radAngle) * hypotenuse;
53 }
54
55 /* GdipDrawPie/GdipFillPie helper function */
56 static GpStatus draw_pie(GpGraphics *graphics, HBRUSH gdibrush, HPEN gdipen,
57     REAL x, REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
58 {
59     HGDIOBJ old_pen, old_brush;
60     REAL x_0, y_0, x_1, y_1, x_2, y_2;
61
62     if(!graphics)
63         return InvalidParameter;
64
65     old_pen = SelectObject(graphics->hdc, gdipen);
66     old_brush = SelectObject(graphics->hdc, gdibrush);
67
68     x_0 = x + (width/2.0);
69     y_0 = y + (height/2.0);
70
71     deg2xy(startAngle+sweepAngle, x_0, y_0, &x_1, &y_1);
72     deg2xy(startAngle, x_0, y_0, &x_2, &y_2);
73
74     Pie(graphics->hdc, roundr(x), roundr(y), roundr(x+width), roundr(y+height),
75         roundr(x_1), roundr(y_1), roundr(x_2), roundr(y_2));
76
77     SelectObject(graphics->hdc, old_pen);
78     SelectObject(graphics->hdc, old_brush);
79
80     return Ok;
81 }
82
83 /* GdipDrawCurve helper function.
84  * Calculates Bezier points from cardinal spline points. */
85 static void calc_curve_bezier(CONST GpPointF *pts, REAL tension, REAL *x1,
86     REAL *y1, REAL *x2, REAL *y2)
87 {
88     REAL xdiff, ydiff;
89
90     /* calculate tangent */
91     xdiff = pts[2].X - pts[0].X;
92     ydiff = pts[2].Y - pts[0].Y;
93
94     /* apply tangent to get control points */
95     *x1 = pts[1].X - tension * xdiff;
96     *y1 = pts[1].Y - tension * ydiff;
97     *x2 = pts[1].X + tension * xdiff;
98     *y2 = pts[1].Y + tension * ydiff;
99 }
100
101 /* GdipDrawCurve helper function.
102  * Calculates Bezier points from cardinal spline endpoints. */
103 static void calc_curve_bezier_endp(REAL xend, REAL yend, REAL xadj, REAL yadj,
104     REAL tension, REAL *x, REAL *y)
105 {
106     /* tangent at endpoints is the line from the endpoint to the adjacent point */
107     *x = roundr(tension * (xadj - xend) + xend);
108     *y = roundr(tension * (yadj - yend) + yend);
109 }
110
111 GpStatus WINGDIPAPI GdipCreateFromHDC(HDC hdc, GpGraphics **graphics)
112 {
113     if(hdc == NULL)
114         return OutOfMemory;
115
116     if(graphics == NULL)
117         return InvalidParameter;
118
119     *graphics = GdipAlloc(sizeof(GpGraphics));
120     if(!*graphics)  return OutOfMemory;
121
122     (*graphics)->hdc = hdc;
123     (*graphics)->hwnd = NULL;
124
125     return Ok;
126 }
127
128 GpStatus WINGDIPAPI GdipCreateFromHWND(HWND hwnd, GpGraphics **graphics)
129 {
130     GpStatus ret;
131
132     if((ret = GdipCreateFromHDC(GetDC(hwnd), graphics)) != Ok)
133         return ret;
134
135     (*graphics)->hwnd = hwnd;
136
137     return Ok;
138 }
139
140 GpStatus WINGDIPAPI GdipDeleteGraphics(GpGraphics *graphics)
141 {
142     if(!graphics) return InvalidParameter;
143     if(graphics->hwnd)
144         ReleaseDC(graphics->hwnd, graphics->hdc);
145
146     HeapFree(GetProcessHeap(), 0, graphics);
147
148     return Ok;
149 }
150
151 GpStatus WINGDIPAPI GdipDrawArc(GpGraphics *graphics, GpPen *pen, REAL x,
152     REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
153 {
154     HGDIOBJ old_pen;
155     REAL x_0, y_0, x_1, y_1, x_2, y_2;
156
157     if(!graphics || !pen)
158         return InvalidParameter;
159
160     old_pen = SelectObject(graphics->hdc, pen->gdipen);
161
162     x_0 = x + (width/2.0);
163     y_0 = y + (height/2.0);
164
165     deg2xy(startAngle+sweepAngle, x_0, y_0, &x_1, &y_1);
166     deg2xy(startAngle, x_0, y_0, &x_2, &y_2);
167
168     Arc(graphics->hdc, roundr(x), roundr(y), roundr(x+width), roundr(y+height),
169         roundr(x_1), roundr(y_1), roundr(x_2), roundr(y_2));
170
171     SelectObject(graphics->hdc, old_pen);
172
173     return Ok;
174 }
175
176 GpStatus WINGDIPAPI GdipDrawBezier(GpGraphics *graphics, GpPen *pen, REAL x1,
177     REAL y1, REAL x2, REAL y2, REAL x3, REAL y3, REAL x4, REAL y4)
178 {
179     HGDIOBJ old_pen;
180     POINT pt[4] = {{roundr(x1), roundr(y1)}, {roundr(x2), roundr(y2)},
181                    {roundr(x3), roundr(y3)}, {roundr(x4), roundr(y4)}};
182
183     if(!graphics || !pen)
184         return InvalidParameter;
185
186     old_pen = SelectObject(graphics->hdc, pen->gdipen);
187
188     PolyBezier(graphics->hdc, pt, 4);
189
190     SelectObject(graphics->hdc, old_pen);
191
192     return Ok;
193 }
194
195 /* Approximates cardinal spline with Bezier curves. */
196 GpStatus WINGDIPAPI GdipDrawCurve2(GpGraphics *graphics, GpPen *pen,
197     GDIPCONST GpPointF *points, INT count, REAL tension)
198 {
199     HGDIOBJ old_pen;
200
201     /* PolyBezier expects count*3-2 points. */
202     int i, len_pt = count*3-2;
203     POINT pt[len_pt];
204     REAL x1, x2, y1, y2;
205
206     if(!graphics || !pen)
207         return InvalidParameter;
208
209     tension = tension * TENSION_CONST;
210
211     calc_curve_bezier_endp(points[0].X, points[0].Y, points[1].X, points[1].Y,
212         tension, &x1, &y1);
213
214     pt[0].x = roundr(points[0].X);
215     pt[0].y = roundr(points[0].Y);
216     pt[1].x = roundr(x1);
217     pt[1].y = roundr(y1);
218
219     for(i = 0; i < count-2; i++){
220         calc_curve_bezier(&(points[i]), tension, &x1, &y1, &x2, &y2);
221
222         pt[3*i+2].x = roundr(x1);
223         pt[3*i+2].y = roundr(y1);
224         pt[3*i+3].x = roundr(points[i+1].X);
225         pt[3*i+3].y = roundr(points[i+1].Y);
226         pt[3*i+4].x = roundr(x2);
227         pt[3*i+4].y = roundr(y2);
228     }
229
230     calc_curve_bezier_endp(points[count-1].X, points[count-1].Y,
231         points[count-2].X, points[count-2].Y, tension, &x1, &y1);
232
233     pt[len_pt-2].x = x1;
234     pt[len_pt-2].y = y1;
235     pt[len_pt-1].x = roundr(points[count-1].X);
236     pt[len_pt-1].y = roundr(points[count-1].Y);
237
238     old_pen = SelectObject(graphics->hdc, pen->gdipen);
239
240     PolyBezier(graphics->hdc, pt, len_pt);
241
242     SelectObject(graphics->hdc, old_pen);
243
244     return Ok;
245 }
246
247 GpStatus WINGDIPAPI GdipDrawLineI(GpGraphics *graphics, GpPen *pen, INT x1,
248     INT y1, INT x2, INT y2)
249 {
250     HGDIOBJ old_obj;
251
252     if(!pen || !graphics)
253         return InvalidParameter;
254
255     old_obj = SelectObject(graphics->hdc, pen->gdipen);
256     MoveToEx(graphics->hdc, x1, y1, NULL);
257     LineTo(graphics->hdc, x2, y2);
258     SelectObject(graphics->hdc, old_obj);
259
260     return Ok;
261 }
262
263 GpStatus WINGDIPAPI GdipDrawLines(GpGraphics *graphics, GpPen *pen, GDIPCONST
264     GpPointF *points, INT count)
265 {
266     HGDIOBJ old_obj;
267     INT i;
268
269     if(!pen || !graphics || (count < 2))
270         return InvalidParameter;
271
272     old_obj = SelectObject(graphics->hdc, pen->gdipen);
273     MoveToEx(graphics->hdc, roundr(points[0].X), roundr(points[0].Y), NULL);
274
275     for(i = 1; i < count; i++){
276         LineTo(graphics->hdc, roundr(points[i].X), roundr(points[i].Y));
277     }
278
279     SelectObject(graphics->hdc, old_obj);
280
281     return Ok;
282 }
283
284 GpStatus WINGDIPAPI GdipDrawPie(GpGraphics *graphics, GpPen *pen, REAL x,
285     REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
286 {
287     if(!pen)
288         return InvalidParameter;
289
290     return draw_pie(graphics, GetStockObject(NULL_BRUSH), pen->gdipen, x, y,
291         width, height, startAngle, sweepAngle);
292 }
293
294 GpStatus WINGDIPAPI GdipDrawRectangleI(GpGraphics *graphics, GpPen *pen, INT x,
295     INT y, INT width, INT height)
296 {
297     LOGBRUSH lb;
298     HPEN hpen;
299     HGDIOBJ old_obj;
300
301     if(!pen || !graphics)
302         return InvalidParameter;
303
304     lb.lbStyle = BS_SOLID;
305     lb.lbColor = pen->color;
306     lb.lbHatch = 0;
307
308     hpen = ExtCreatePen(PS_GEOMETRIC | PS_ENDCAP_SQUARE, (INT) pen->width,
309         &lb, 0, NULL);
310
311     old_obj = SelectObject(graphics->hdc, hpen);
312
313     /* assume pen aligment centered */
314     MoveToEx(graphics->hdc, x, y, NULL);
315     LineTo(graphics->hdc, x+width, y);
316     LineTo(graphics->hdc, x+width, y+height);
317     LineTo(graphics->hdc, x, y+height);
318     LineTo(graphics->hdc, x, y);
319
320     SelectObject(graphics->hdc, old_obj);
321     DeleteObject(hpen);
322
323     return Ok;
324 }
325
326 GpStatus WINGDIPAPI GdipFillPie(GpGraphics *graphics, GpBrush *brush, REAL x,
327     REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
328 {
329     if(!brush)
330         return InvalidParameter;
331
332     return draw_pie(graphics, brush->gdibrush, GetStockObject(NULL_PEN), x, y,
333         width, height, startAngle, sweepAngle);
334 }