shdocvw/tests: Need to save .url before committing extra props for IE6.
[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 #include <limits.h>
22
23 #include "windef.h"
24 #include "winbase.h"
25 #include "winuser.h"
26 #include "wingdi.h"
27 #include "wine/unicode.h"
28
29 #define COBJMACROS
30 #include "objbase.h"
31 #include "ocidl.h"
32 #include "olectl.h"
33 #include "ole2.h"
34
35 #include "winreg.h"
36 #include "shlwapi.h"
37
38 #include "gdiplus.h"
39 #include "gdiplus_private.h"
40 #include "wine/debug.h"
41 #include "wine/list.h"
42
43 WINE_DEFAULT_DEBUG_CHANNEL(gdiplus);
44
45 /* looks-right constants */
46 #define ANCHOR_WIDTH (2.0)
47 #define MAX_ITERS (50)
48
49 /* Converts angle (in degrees) to x/y coordinates */
50 static void deg2xy(REAL angle, REAL x_0, REAL y_0, REAL *x, REAL *y)
51 {
52     REAL radAngle, hypotenuse;
53
54     radAngle = deg2rad(angle);
55     hypotenuse = 50.0; /* arbitrary */
56
57     *x = x_0 + cos(radAngle) * hypotenuse;
58     *y = y_0 + sin(radAngle) * hypotenuse;
59 }
60
61 /* Converts from gdiplus path point type to gdi path point type. */
62 static BYTE convert_path_point_type(BYTE type)
63 {
64     BYTE ret;
65
66     switch(type & PathPointTypePathTypeMask){
67         case PathPointTypeBezier:
68             ret = PT_BEZIERTO;
69             break;
70         case PathPointTypeLine:
71             ret = PT_LINETO;
72             break;
73         case PathPointTypeStart:
74             ret = PT_MOVETO;
75             break;
76         default:
77             ERR("Bad point type\n");
78             return 0;
79     }
80
81     if(type & PathPointTypeCloseSubpath)
82         ret |= PT_CLOSEFIGURE;
83
84     return ret;
85 }
86
87 static REAL graphics_res(GpGraphics *graphics)
88 {
89     if (graphics->image) return graphics->image->xres;
90     else return (REAL)GetDeviceCaps(graphics->hdc, LOGPIXELSX);
91 }
92
93 static INT prepare_dc(GpGraphics *graphics, GpPen *pen)
94 {
95     HPEN gdipen;
96     REAL width;
97     INT save_state, i, numdashes;
98     GpPointF pt[2];
99     DWORD dash_array[MAX_DASHLEN];
100
101     save_state = SaveDC(graphics->hdc);
102
103     EndPath(graphics->hdc);
104
105     if(pen->unit == UnitPixel){
106         width = pen->width;
107     }
108     else{
109         /* Get an estimate for the amount the pen width is affected by the world
110          * transform. (This is similar to what some of the wine drivers do.) */
111         pt[0].X = 0.0;
112         pt[0].Y = 0.0;
113         pt[1].X = 1.0;
114         pt[1].Y = 1.0;
115         GdipTransformMatrixPoints(graphics->worldtrans, pt, 2);
116         width = sqrt((pt[1].X - pt[0].X) * (pt[1].X - pt[0].X) +
117                      (pt[1].Y - pt[0].Y) * (pt[1].Y - pt[0].Y)) / sqrt(2.0);
118
119         width *= pen->width * convert_unit(graphics_res(graphics),
120                               pen->unit == UnitWorld ? graphics->unit : pen->unit);
121     }
122
123     if(pen->dash == DashStyleCustom){
124         numdashes = min(pen->numdashes, MAX_DASHLEN);
125
126         TRACE("dashes are: ");
127         for(i = 0; i < numdashes; i++){
128             dash_array[i] = roundr(width * pen->dashes[i]);
129             TRACE("%d, ", dash_array[i]);
130         }
131         TRACE("\n and the pen style is %x\n", pen->style);
132
133         gdipen = ExtCreatePen(pen->style, roundr(width), &pen->brush->lb,
134                               numdashes, dash_array);
135     }
136     else
137         gdipen = ExtCreatePen(pen->style, roundr(width), &pen->brush->lb, 0, NULL);
138
139     SelectObject(graphics->hdc, gdipen);
140
141     return save_state;
142 }
143
144 static void restore_dc(GpGraphics *graphics, INT state)
145 {
146     DeleteObject(SelectObject(graphics->hdc, GetStockObject(NULL_PEN)));
147     RestoreDC(graphics->hdc, state);
148 }
149
150 /* This helper applies all the changes that the points listed in ptf need in
151  * order to be drawn on the device context.  In the end, this should include at
152  * least:
153  *  -scaling by page unit
154  *  -applying world transformation
155  *  -converting from float to int
156  * Native gdiplus uses gdi32 to do all this (via SetMapMode, SetViewportExtEx,
157  * SetWindowExtEx, SetWorldTransform, etc.) but we cannot because we are using
158  * gdi to draw, and these functions would irreparably mess with line widths.
159  */
160 static void transform_and_round_points(GpGraphics *graphics, POINT *pti,
161     GpPointF *ptf, INT count)
162 {
163     REAL unitscale;
164     GpMatrix *matrix;
165     int i;
166
167     unitscale = convert_unit(graphics_res(graphics), graphics->unit);
168
169     /* apply page scale */
170     if(graphics->unit != UnitDisplay)
171         unitscale *= graphics->scale;
172
173     GdipCloneMatrix(graphics->worldtrans, &matrix);
174     GdipScaleMatrix(matrix, unitscale, unitscale, MatrixOrderAppend);
175     GdipTransformMatrixPoints(matrix, ptf, count);
176     GdipDeleteMatrix(matrix);
177
178     for(i = 0; i < count; i++){
179         pti[i].x = roundr(ptf[i].X);
180         pti[i].y = roundr(ptf[i].Y);
181     }
182 }
183
184 /* Draw non-premultiplied ARGB data to the given graphics object */
185 static GpStatus alpha_blend_pixels(GpGraphics *graphics, INT dst_x, INT dst_y,
186     const BYTE *src, INT src_width, INT src_height, INT src_stride)
187 {
188     if (graphics->image && graphics->image->type == ImageTypeBitmap)
189     {
190         GpBitmap *dst_bitmap = (GpBitmap*)graphics->image;
191         INT x, y;
192
193         for (x=0; x<src_width; x++)
194         {
195             for (y=0; y<src_height; y++)
196             {
197                 ARGB dst_color, src_color;
198                 GdipBitmapGetPixel(dst_bitmap, x+dst_x, y+dst_y, &dst_color);
199                 src_color = ((ARGB*)(src + src_stride * y))[x];
200                 GdipBitmapSetPixel(dst_bitmap, x+dst_x, y+dst_y, color_over(dst_color, src_color));
201             }
202         }
203
204         return Ok;
205     }
206     else
207     {
208         HDC hdc;
209         HBITMAP hbitmap, old_hbm=NULL;
210         BITMAPINFOHEADER bih;
211         BYTE *temp_bits;
212         BLENDFUNCTION bf;
213
214         hdc = CreateCompatibleDC(0);
215
216         bih.biSize = sizeof(BITMAPINFOHEADER);
217         bih.biWidth = src_width;
218         bih.biHeight = -src_height;
219         bih.biPlanes = 1;
220         bih.biBitCount = 32;
221         bih.biCompression = BI_RGB;
222         bih.biSizeImage = 0;
223         bih.biXPelsPerMeter = 0;
224         bih.biYPelsPerMeter = 0;
225         bih.biClrUsed = 0;
226         bih.biClrImportant = 0;
227
228         hbitmap = CreateDIBSection(hdc, (BITMAPINFO*)&bih, DIB_RGB_COLORS,
229             (void**)&temp_bits, NULL, 0);
230
231         convert_32bppARGB_to_32bppPARGB(src_width, src_height, temp_bits,
232             4 * src_width, src, src_stride);
233
234         old_hbm = SelectObject(hdc, hbitmap);
235
236         bf.BlendOp = AC_SRC_OVER;
237         bf.BlendFlags = 0;
238         bf.SourceConstantAlpha = 255;
239         bf.AlphaFormat = AC_SRC_ALPHA;
240
241         GdiAlphaBlend(graphics->hdc, dst_x, dst_y, src_width, src_height,
242             hdc, 0, 0, src_width, src_height, bf);
243
244         SelectObject(hdc, old_hbm);
245         DeleteDC(hdc);
246         DeleteObject(hbitmap);
247
248         return Ok;
249     }
250 }
251
252 static ARGB blend_colors(ARGB start, ARGB end, REAL position)
253 {
254     ARGB result=0;
255     ARGB i;
256     for (i=0xff; i<=0xff0000; i = i << 8)
257         result |= (int)((start&i)*(1.0f - position)+(end&i)*(position))&i;
258     return result;
259 }
260
261 static ARGB blend_line_gradient(GpLineGradient* brush, REAL position)
262 {
263     REAL blendfac;
264
265     /* clamp to between 0.0 and 1.0, using the wrap mode */
266     if (brush->wrap == WrapModeTile)
267     {
268         position = fmodf(position, 1.0f);
269         if (position < 0.0f) position += 1.0f;
270     }
271     else /* WrapModeFlip* */
272     {
273         position = fmodf(position, 2.0f);
274         if (position < 0.0f) position += 2.0f;
275         if (position > 1.0f) position = 2.0f - position;
276     }
277
278     if (brush->blendcount == 1)
279         blendfac = position;
280     else
281     {
282         int i=1;
283         REAL left_blendpos, left_blendfac, right_blendpos, right_blendfac;
284         REAL range;
285
286         /* locate the blend positions surrounding this position */
287         while (position > brush->blendpos[i])
288             i++;
289
290         /* interpolate between the blend positions */
291         left_blendpos = brush->blendpos[i-1];
292         left_blendfac = brush->blendfac[i-1];
293         right_blendpos = brush->blendpos[i];
294         right_blendfac = brush->blendfac[i];
295         range = right_blendpos - left_blendpos;
296         blendfac = (left_blendfac * (right_blendpos - position) +
297                     right_blendfac * (position - left_blendpos)) / range;
298     }
299
300     if (brush->pblendcount == 0)
301         return blend_colors(brush->startcolor, brush->endcolor, blendfac);
302     else
303     {
304         int i=1;
305         ARGB left_blendcolor, right_blendcolor;
306         REAL left_blendpos, right_blendpos;
307
308         /* locate the blend colors surrounding this position */
309         while (blendfac > brush->pblendpos[i])
310             i++;
311
312         /* interpolate between the blend colors */
313         left_blendpos = brush->pblendpos[i-1];
314         left_blendcolor = brush->pblendcolor[i-1];
315         right_blendpos = brush->pblendpos[i];
316         right_blendcolor = brush->pblendcolor[i];
317         blendfac = (blendfac - left_blendpos) / (right_blendpos - left_blendpos);
318         return blend_colors(left_blendcolor, right_blendcolor, blendfac);
319     }
320 }
321
322 static void brush_fill_path(GpGraphics *graphics, GpBrush* brush)
323 {
324     switch (brush->bt)
325     {
326     case BrushTypeLinearGradient:
327     {
328         GpLineGradient *line = (GpLineGradient*)brush;
329         RECT rc;
330
331         SelectClipPath(graphics->hdc, RGN_AND);
332         if (GetClipBox(graphics->hdc, &rc) != NULLREGION)
333         {
334             GpPointF endpointsf[2];
335             POINT endpointsi[2];
336             POINT poly[4];
337
338             SelectObject(graphics->hdc, GetStockObject(NULL_PEN));
339
340             endpointsf[0] = line->startpoint;
341             endpointsf[1] = line->endpoint;
342             transform_and_round_points(graphics, endpointsi, endpointsf, 2);
343
344             if (abs(endpointsi[0].x-endpointsi[1].x) > abs(endpointsi[0].y-endpointsi[1].y))
345             {
346                 /* vertical-ish gradient */
347                 int startx, endx; /* x co-ordinates of endpoints shifted to intersect the top of the visible rectangle */
348                 int startbottomx; /* x co-ordinate of start point shifted to intersect the bottom of the visible rectangle */
349                 int width;
350                 COLORREF col;
351                 HBRUSH hbrush, hprevbrush;
352                 int leftx, rightx; /* x co-ordinates where the leftmost and rightmost gradient lines hit the top of the visible rectangle */
353                 int x;
354                 int tilt; /* horizontal distance covered by a gradient line */
355
356                 startx = roundr((rc.top - endpointsf[0].Y) * (endpointsf[1].Y - endpointsf[0].Y) / (endpointsf[0].X - endpointsf[1].X) + endpointsf[0].X);
357                 endx = roundr((rc.top - endpointsf[1].Y) * (endpointsf[1].Y - endpointsf[0].Y) / (endpointsf[0].X - endpointsf[1].X) + endpointsf[1].X);
358                 width = endx - startx;
359                 startbottomx = roundr((rc.bottom - endpointsf[0].Y) * (endpointsf[1].Y - endpointsf[0].Y) / (endpointsf[0].X - endpointsf[1].X) + endpointsf[0].X);
360                 tilt = startx - startbottomx;
361
362                 if (startx >= startbottomx)
363                 {
364                     leftx = rc.left;
365                     rightx = rc.right + tilt;
366                 }
367                 else
368                 {
369                     leftx = rc.left + tilt;
370                     rightx = rc.right;
371                 }
372
373                 poly[0].y = rc.bottom;
374                 poly[1].y = rc.top;
375                 poly[2].y = rc.top;
376                 poly[3].y = rc.bottom;
377
378                 for (x=leftx; x<=rightx; x++)
379                 {
380                     ARGB argb = blend_line_gradient(line, (x-startx)/(REAL)width);
381                     col = ARGB2COLORREF(argb);
382                     hbrush = CreateSolidBrush(col);
383                     hprevbrush = SelectObject(graphics->hdc, hbrush);
384                     poly[0].x = x - tilt - 1;
385                     poly[1].x = x - 1;
386                     poly[2].x = x;
387                     poly[3].x = x - tilt;
388                     Polygon(graphics->hdc, poly, 4);
389                     SelectObject(graphics->hdc, hprevbrush);
390                     DeleteObject(hbrush);
391                 }
392             }
393             else if (endpointsi[0].y != endpointsi[1].y)
394             {
395                 /* horizontal-ish gradient */
396                 int starty, endy; /* y co-ordinates of endpoints shifted to intersect the left of the visible rectangle */
397                 int startrighty; /* y co-ordinate of start point shifted to intersect the right of the visible rectangle */
398                 int height;
399                 COLORREF col;
400                 HBRUSH hbrush, hprevbrush;
401                 int topy, bottomy; /* y co-ordinates where the topmost and bottommost gradient lines hit the left of the visible rectangle */
402                 int y;
403                 int tilt; /* vertical distance covered by a gradient line */
404
405                 starty = roundr((rc.left - endpointsf[0].X) * (endpointsf[0].X - endpointsf[1].X) / (endpointsf[1].Y - endpointsf[0].Y) + endpointsf[0].Y);
406                 endy = roundr((rc.left - endpointsf[1].X) * (endpointsf[0].X - endpointsf[1].X) / (endpointsf[1].Y - endpointsf[0].Y) + endpointsf[1].Y);
407                 height = endy - starty;
408                 startrighty = roundr((rc.right - endpointsf[0].X) * (endpointsf[0].X - endpointsf[1].X) / (endpointsf[1].Y - endpointsf[0].Y) + endpointsf[0].Y);
409                 tilt = starty - startrighty;
410
411                 if (starty >= startrighty)
412                 {
413                     topy = rc.top;
414                     bottomy = rc.bottom + tilt;
415                 }
416                 else
417                 {
418                     topy = rc.top + tilt;
419                     bottomy = rc.bottom;
420                 }
421
422                 poly[0].x = rc.right;
423                 poly[1].x = rc.left;
424                 poly[2].x = rc.left;
425                 poly[3].x = rc.right;
426
427                 for (y=topy; y<=bottomy; y++)
428                 {
429                     ARGB argb = blend_line_gradient(line, (y-starty)/(REAL)height);
430                     col = ARGB2COLORREF(argb);
431                     hbrush = CreateSolidBrush(col);
432                     hprevbrush = SelectObject(graphics->hdc, hbrush);
433                     poly[0].y = y - tilt - 1;
434                     poly[1].y = y - 1;
435                     poly[2].y = y;
436                     poly[3].y = y - tilt;
437                     Polygon(graphics->hdc, poly, 4);
438                     SelectObject(graphics->hdc, hprevbrush);
439                     DeleteObject(hbrush);
440                 }
441             }
442             /* else startpoint == endpoint */
443         }
444         break;
445     }
446     case BrushTypeSolidColor:
447     {
448         GpSolidFill *fill = (GpSolidFill*)brush;
449         if (fill->bmp)
450         {
451             RECT rc;
452             /* partially transparent fill */
453
454             SelectClipPath(graphics->hdc, RGN_AND);
455             if (GetClipBox(graphics->hdc, &rc) != NULLREGION)
456             {
457                 HDC hdc = CreateCompatibleDC(NULL);
458                 HBITMAP oldbmp;
459                 BLENDFUNCTION bf;
460
461                 if (!hdc) break;
462
463                 oldbmp = SelectObject(hdc, fill->bmp);
464
465                 bf.BlendOp = AC_SRC_OVER;
466                 bf.BlendFlags = 0;
467                 bf.SourceConstantAlpha = 255;
468                 bf.AlphaFormat = AC_SRC_ALPHA;
469
470                 GdiAlphaBlend(graphics->hdc, rc.left, rc.top, rc.right-rc.left, rc.bottom-rc.top, hdc, 0, 0, 1, 1, bf);
471
472                 SelectObject(hdc, oldbmp);
473                 DeleteDC(hdc);
474             }
475
476             break;
477         }
478         /* else fall through */
479     }
480     default:
481         SelectObject(graphics->hdc, brush->gdibrush);
482         FillPath(graphics->hdc);
483         break;
484     }
485 }
486
487 /* GdipDrawPie/GdipFillPie helper function */
488 static void draw_pie(GpGraphics *graphics, REAL x, REAL y, REAL width,
489     REAL height, REAL startAngle, REAL sweepAngle)
490 {
491     GpPointF ptf[4];
492     POINT pti[4];
493
494     ptf[0].X = x;
495     ptf[0].Y = y;
496     ptf[1].X = x + width;
497     ptf[1].Y = y + height;
498
499     deg2xy(startAngle+sweepAngle, x + width / 2.0, y + width / 2.0, &ptf[2].X, &ptf[2].Y);
500     deg2xy(startAngle, x + width / 2.0, y + width / 2.0, &ptf[3].X, &ptf[3].Y);
501
502     transform_and_round_points(graphics, pti, ptf, 4);
503
504     Pie(graphics->hdc, pti[0].x, pti[0].y, pti[1].x, pti[1].y, pti[2].x,
505         pti[2].y, pti[3].x, pti[3].y);
506 }
507
508 /* Draws the linecap the specified color and size on the hdc.  The linecap is in
509  * direction of the line from x1, y1 to x2, y2 and is anchored on x2, y2. Probably
510  * should not be called on an hdc that has a path you care about. */
511 static void draw_cap(GpGraphics *graphics, COLORREF color, GpLineCap cap, REAL size,
512     const GpCustomLineCap *custom, REAL x1, REAL y1, REAL x2, REAL y2)
513 {
514     HGDIOBJ oldbrush = NULL, oldpen = NULL;
515     GpMatrix *matrix = NULL;
516     HBRUSH brush = NULL;
517     HPEN pen = NULL;
518     PointF ptf[4], *custptf = NULL;
519     POINT pt[4], *custpt = NULL;
520     BYTE *tp = NULL;
521     REAL theta, dsmall, dbig, dx, dy = 0.0;
522     INT i, count;
523     LOGBRUSH lb;
524     BOOL customstroke;
525
526     if((x1 == x2) && (y1 == y2))
527         return;
528
529     theta = gdiplus_atan2(y2 - y1, x2 - x1);
530
531     customstroke = (cap == LineCapCustom) && custom && (!custom->fill);
532     if(!customstroke){
533         brush = CreateSolidBrush(color);
534         lb.lbStyle = BS_SOLID;
535         lb.lbColor = color;
536         lb.lbHatch = 0;
537         pen = ExtCreatePen(PS_GEOMETRIC | PS_SOLID | PS_ENDCAP_FLAT |
538                            PS_JOIN_MITER, 1, &lb, 0,
539                            NULL);
540         oldbrush = SelectObject(graphics->hdc, brush);
541         oldpen = SelectObject(graphics->hdc, pen);
542     }
543
544     switch(cap){
545         case LineCapFlat:
546             break;
547         case LineCapSquare:
548         case LineCapSquareAnchor:
549         case LineCapDiamondAnchor:
550             size = size * (cap & LineCapNoAnchor ? ANCHOR_WIDTH : 1.0) / 2.0;
551             if(cap == LineCapDiamondAnchor){
552                 dsmall = cos(theta + M_PI_2) * size;
553                 dbig = sin(theta + M_PI_2) * size;
554             }
555             else{
556                 dsmall = cos(theta + M_PI_4) * size;
557                 dbig = sin(theta + M_PI_4) * size;
558             }
559
560             ptf[0].X = x2 - dsmall;
561             ptf[1].X = x2 + dbig;
562
563             ptf[0].Y = y2 - dbig;
564             ptf[3].Y = y2 + dsmall;
565
566             ptf[1].Y = y2 - dsmall;
567             ptf[2].Y = y2 + dbig;
568
569             ptf[3].X = x2 - dbig;
570             ptf[2].X = x2 + dsmall;
571
572             transform_and_round_points(graphics, pt, ptf, 4);
573             Polygon(graphics->hdc, pt, 4);
574
575             break;
576         case LineCapArrowAnchor:
577             size = size * 4.0 / sqrt(3.0);
578
579             dx = cos(M_PI / 6.0 + theta) * size;
580             dy = sin(M_PI / 6.0 + theta) * size;
581
582             ptf[0].X = x2 - dx;
583             ptf[0].Y = y2 - dy;
584
585             dx = cos(- M_PI / 6.0 + theta) * size;
586             dy = sin(- M_PI / 6.0 + theta) * size;
587
588             ptf[1].X = x2 - dx;
589             ptf[1].Y = y2 - dy;
590
591             ptf[2].X = x2;
592             ptf[2].Y = y2;
593
594             transform_and_round_points(graphics, pt, ptf, 3);
595             Polygon(graphics->hdc, pt, 3);
596
597             break;
598         case LineCapRoundAnchor:
599             dx = dy = ANCHOR_WIDTH * size / 2.0;
600
601             ptf[0].X = x2 - dx;
602             ptf[0].Y = y2 - dy;
603             ptf[1].X = x2 + dx;
604             ptf[1].Y = y2 + dy;
605
606             transform_and_round_points(graphics, pt, ptf, 2);
607             Ellipse(graphics->hdc, pt[0].x, pt[0].y, pt[1].x, pt[1].y);
608
609             break;
610         case LineCapTriangle:
611             size = size / 2.0;
612             dx = cos(M_PI_2 + theta) * size;
613             dy = sin(M_PI_2 + theta) * size;
614
615             ptf[0].X = x2 - dx;
616             ptf[0].Y = y2 - dy;
617             ptf[1].X = x2 + dx;
618             ptf[1].Y = y2 + dy;
619
620             dx = cos(theta) * size;
621             dy = sin(theta) * size;
622
623             ptf[2].X = x2 + dx;
624             ptf[2].Y = y2 + dy;
625
626             transform_and_round_points(graphics, pt, ptf, 3);
627             Polygon(graphics->hdc, pt, 3);
628
629             break;
630         case LineCapRound:
631             dx = dy = size / 2.0;
632
633             ptf[0].X = x2 - dx;
634             ptf[0].Y = y2 - dy;
635             ptf[1].X = x2 + dx;
636             ptf[1].Y = y2 + dy;
637
638             dx = -cos(M_PI_2 + theta) * size;
639             dy = -sin(M_PI_2 + theta) * size;
640
641             ptf[2].X = x2 - dx;
642             ptf[2].Y = y2 - dy;
643             ptf[3].X = x2 + dx;
644             ptf[3].Y = y2 + dy;
645
646             transform_and_round_points(graphics, pt, ptf, 4);
647             Pie(graphics->hdc, pt[0].x, pt[0].y, pt[1].x, pt[1].y, pt[2].x,
648                 pt[2].y, pt[3].x, pt[3].y);
649
650             break;
651         case LineCapCustom:
652             if(!custom)
653                 break;
654
655             count = custom->pathdata.Count;
656             custptf = GdipAlloc(count * sizeof(PointF));
657             custpt = GdipAlloc(count * sizeof(POINT));
658             tp = GdipAlloc(count);
659
660             if(!custptf || !custpt || !tp || (GdipCreateMatrix(&matrix) != Ok))
661                 goto custend;
662
663             memcpy(custptf, custom->pathdata.Points, count * sizeof(PointF));
664
665             GdipScaleMatrix(matrix, size, size, MatrixOrderAppend);
666             GdipRotateMatrix(matrix, (180.0 / M_PI) * (theta - M_PI_2),
667                              MatrixOrderAppend);
668             GdipTranslateMatrix(matrix, x2, y2, MatrixOrderAppend);
669             GdipTransformMatrixPoints(matrix, custptf, count);
670
671             transform_and_round_points(graphics, custpt, custptf, count);
672
673             for(i = 0; i < count; i++)
674                 tp[i] = convert_path_point_type(custom->pathdata.Types[i]);
675
676             if(custom->fill){
677                 BeginPath(graphics->hdc);
678                 PolyDraw(graphics->hdc, custpt, tp, count);
679                 EndPath(graphics->hdc);
680                 StrokeAndFillPath(graphics->hdc);
681             }
682             else
683                 PolyDraw(graphics->hdc, custpt, tp, count);
684
685 custend:
686             GdipFree(custptf);
687             GdipFree(custpt);
688             GdipFree(tp);
689             GdipDeleteMatrix(matrix);
690             break;
691         default:
692             break;
693     }
694
695     if(!customstroke){
696         SelectObject(graphics->hdc, oldbrush);
697         SelectObject(graphics->hdc, oldpen);
698         DeleteObject(brush);
699         DeleteObject(pen);
700     }
701 }
702
703 /* Shortens the line by the given percent by changing x2, y2.
704  * If percent is > 1.0 then the line will change direction.
705  * If percent is negative it can lengthen the line. */
706 static void shorten_line_percent(REAL x1, REAL  y1, REAL *x2, REAL *y2, REAL percent)
707 {
708     REAL dist, theta, dx, dy;
709
710     if((y1 == *y2) && (x1 == *x2))
711         return;
712
713     dist = sqrt((*x2 - x1) * (*x2 - x1) + (*y2 - y1) * (*y2 - y1)) * -percent;
714     theta = gdiplus_atan2((*y2 - y1), (*x2 - x1));
715     dx = cos(theta) * dist;
716     dy = sin(theta) * dist;
717
718     *x2 = *x2 + dx;
719     *y2 = *y2 + dy;
720 }
721
722 /* Shortens the line by the given amount by changing x2, y2.
723  * If the amount is greater than the distance, the line will become length 0.
724  * If the amount is negative, it can lengthen the line. */
725 static void shorten_line_amt(REAL x1, REAL y1, REAL *x2, REAL *y2, REAL amt)
726 {
727     REAL dx, dy, percent;
728
729     dx = *x2 - x1;
730     dy = *y2 - y1;
731     if(dx == 0 && dy == 0)
732         return;
733
734     percent = amt / sqrt(dx * dx + dy * dy);
735     if(percent >= 1.0){
736         *x2 = x1;
737         *y2 = y1;
738         return;
739     }
740
741     shorten_line_percent(x1, y1, x2, y2, percent);
742 }
743
744 /* Draws lines between the given points, and if caps is true then draws an endcap
745  * at the end of the last line. */
746 static GpStatus draw_polyline(GpGraphics *graphics, GpPen *pen,
747     GDIPCONST GpPointF * pt, INT count, BOOL caps)
748 {
749     POINT *pti = NULL;
750     GpPointF *ptcopy = NULL;
751     GpStatus status = GenericError;
752
753     if(!count)
754         return Ok;
755
756     pti = GdipAlloc(count * sizeof(POINT));
757     ptcopy = GdipAlloc(count * sizeof(GpPointF));
758
759     if(!pti || !ptcopy){
760         status = OutOfMemory;
761         goto end;
762     }
763
764     memcpy(ptcopy, pt, count * sizeof(GpPointF));
765
766     if(caps){
767         if(pen->endcap == LineCapArrowAnchor)
768             shorten_line_amt(ptcopy[count-2].X, ptcopy[count-2].Y,
769                              &ptcopy[count-1].X, &ptcopy[count-1].Y, pen->width);
770         else if((pen->endcap == LineCapCustom) && pen->customend)
771             shorten_line_amt(ptcopy[count-2].X, ptcopy[count-2].Y,
772                              &ptcopy[count-1].X, &ptcopy[count-1].Y,
773                              pen->customend->inset * pen->width);
774
775         if(pen->startcap == LineCapArrowAnchor)
776             shorten_line_amt(ptcopy[1].X, ptcopy[1].Y,
777                              &ptcopy[0].X, &ptcopy[0].Y, pen->width);
778         else if((pen->startcap == LineCapCustom) && pen->customstart)
779             shorten_line_amt(ptcopy[1].X, ptcopy[1].Y,
780                              &ptcopy[0].X, &ptcopy[0].Y,
781                              pen->customstart->inset * pen->width);
782
783         draw_cap(graphics, pen->brush->lb.lbColor, pen->endcap, pen->width, pen->customend,
784                  pt[count - 2].X, pt[count - 2].Y, pt[count - 1].X, pt[count - 1].Y);
785         draw_cap(graphics, pen->brush->lb.lbColor, pen->startcap, pen->width, pen->customstart,
786                          pt[1].X, pt[1].Y, pt[0].X, pt[0].Y);
787     }
788
789     transform_and_round_points(graphics, pti, ptcopy, count);
790
791     if(Polyline(graphics->hdc, pti, count))
792         status = Ok;
793
794 end:
795     GdipFree(pti);
796     GdipFree(ptcopy);
797
798     return status;
799 }
800
801 /* Conducts a linear search to find the bezier points that will back off
802  * the endpoint of the curve by a distance of amt. Linear search works
803  * better than binary in this case because there are multiple solutions,
804  * and binary searches often find a bad one. I don't think this is what
805  * Windows does but short of rendering the bezier without GDI's help it's
806  * the best we can do. If rev then work from the start of the passed points
807  * instead of the end. */
808 static void shorten_bezier_amt(GpPointF * pt, REAL amt, BOOL rev)
809 {
810     GpPointF origpt[4];
811     REAL percent = 0.00, dx, dy, origx, origy, diff = -1.0;
812     INT i, first = 0, second = 1, third = 2, fourth = 3;
813
814     if(rev){
815         first = 3;
816         second = 2;
817         third = 1;
818         fourth = 0;
819     }
820
821     origx = pt[fourth].X;
822     origy = pt[fourth].Y;
823     memcpy(origpt, pt, sizeof(GpPointF) * 4);
824
825     for(i = 0; (i < MAX_ITERS) && (diff < amt); i++){
826         /* reset bezier points to original values */
827         memcpy(pt, origpt, sizeof(GpPointF) * 4);
828         /* Perform magic on bezier points. Order is important here.*/
829         shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
830         shorten_line_percent(pt[second].X, pt[second].Y, &pt[third].X, &pt[third].Y, percent);
831         shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
832         shorten_line_percent(pt[first].X, pt[first].Y, &pt[second].X, &pt[second].Y, percent);
833         shorten_line_percent(pt[second].X, pt[second].Y, &pt[third].X, &pt[third].Y, percent);
834         shorten_line_percent(pt[third].X, pt[third].Y, &pt[fourth].X, &pt[fourth].Y, percent);
835
836         dx = pt[fourth].X - origx;
837         dy = pt[fourth].Y - origy;
838
839         diff = sqrt(dx * dx + dy * dy);
840         percent += 0.0005 * amt;
841     }
842 }
843
844 /* Draws bezier curves between given points, and if caps is true then draws an
845  * endcap at the end of the last line. */
846 static GpStatus draw_polybezier(GpGraphics *graphics, GpPen *pen,
847     GDIPCONST GpPointF * pt, INT count, BOOL caps)
848 {
849     POINT *pti;
850     GpPointF *ptcopy;
851     GpStatus status = GenericError;
852
853     if(!count)
854         return Ok;
855
856     pti = GdipAlloc(count * sizeof(POINT));
857     ptcopy = GdipAlloc(count * sizeof(GpPointF));
858
859     if(!pti || !ptcopy){
860         status = OutOfMemory;
861         goto end;
862     }
863
864     memcpy(ptcopy, pt, count * sizeof(GpPointF));
865
866     if(caps){
867         if(pen->endcap == LineCapArrowAnchor)
868             shorten_bezier_amt(&ptcopy[count-4], pen->width, FALSE);
869         else if((pen->endcap == LineCapCustom) && pen->customend)
870             shorten_bezier_amt(&ptcopy[count-4], pen->width * pen->customend->inset,
871                                FALSE);
872
873         if(pen->startcap == LineCapArrowAnchor)
874             shorten_bezier_amt(ptcopy, pen->width, TRUE);
875         else if((pen->startcap == LineCapCustom) && pen->customstart)
876             shorten_bezier_amt(ptcopy, pen->width * pen->customstart->inset, TRUE);
877
878         /* the direction of the line cap is parallel to the direction at the
879          * end of the bezier (which, if it has been shortened, is not the same
880          * as the direction from pt[count-2] to pt[count-1]) */
881         draw_cap(graphics, pen->brush->lb.lbColor, pen->endcap, pen->width, pen->customend,
882             pt[count - 1].X - (ptcopy[count - 1].X - ptcopy[count - 2].X),
883             pt[count - 1].Y - (ptcopy[count - 1].Y - ptcopy[count - 2].Y),
884             pt[count - 1].X, pt[count - 1].Y);
885
886         draw_cap(graphics, pen->brush->lb.lbColor, pen->startcap, pen->width, pen->customstart,
887             pt[0].X - (ptcopy[0].X - ptcopy[1].X),
888             pt[0].Y - (ptcopy[0].Y - ptcopy[1].Y), pt[0].X, pt[0].Y);
889     }
890
891     transform_and_round_points(graphics, pti, ptcopy, count);
892
893     PolyBezier(graphics->hdc, pti, count);
894
895     status = Ok;
896
897 end:
898     GdipFree(pti);
899     GdipFree(ptcopy);
900
901     return status;
902 }
903
904 /* Draws a combination of bezier curves and lines between points. */
905 static GpStatus draw_poly(GpGraphics *graphics, GpPen *pen, GDIPCONST GpPointF * pt,
906     GDIPCONST BYTE * types, INT count, BOOL caps)
907 {
908     POINT *pti = GdipAlloc(count * sizeof(POINT));
909     BYTE *tp = GdipAlloc(count);
910     GpPointF *ptcopy = GdipAlloc(count * sizeof(GpPointF));
911     INT i, j;
912     GpStatus status = GenericError;
913
914     if(!count){
915         status = Ok;
916         goto end;
917     }
918     if(!pti || !tp || !ptcopy){
919         status = OutOfMemory;
920         goto end;
921     }
922
923     for(i = 1; i < count; i++){
924         if((types[i] & PathPointTypePathTypeMask) == PathPointTypeBezier){
925             if((i + 2 >= count) || !(types[i + 1] & PathPointTypeBezier)
926                 || !(types[i + 1] & PathPointTypeBezier)){
927                 ERR("Bad bezier points\n");
928                 goto end;
929             }
930             i += 2;
931         }
932     }
933
934     memcpy(ptcopy, pt, count * sizeof(GpPointF));
935
936     /* If we are drawing caps, go through the points and adjust them accordingly,
937      * and draw the caps. */
938     if(caps){
939         switch(types[count - 1] & PathPointTypePathTypeMask){
940             case PathPointTypeBezier:
941                 if(pen->endcap == LineCapArrowAnchor)
942                     shorten_bezier_amt(&ptcopy[count - 4], pen->width, FALSE);
943                 else if((pen->endcap == LineCapCustom) && pen->customend)
944                     shorten_bezier_amt(&ptcopy[count - 4],
945                                        pen->width * pen->customend->inset, FALSE);
946
947                 draw_cap(graphics, pen->brush->lb.lbColor, pen->endcap, pen->width, pen->customend,
948                     pt[count - 1].X - (ptcopy[count - 1].X - ptcopy[count - 2].X),
949                     pt[count - 1].Y - (ptcopy[count - 1].Y - ptcopy[count - 2].Y),
950                     pt[count - 1].X, pt[count - 1].Y);
951
952                 break;
953             case PathPointTypeLine:
954                 if(pen->endcap == LineCapArrowAnchor)
955                     shorten_line_amt(ptcopy[count - 2].X, ptcopy[count - 2].Y,
956                                      &ptcopy[count - 1].X, &ptcopy[count - 1].Y,
957                                      pen->width);
958                 else if((pen->endcap == LineCapCustom) && pen->customend)
959                     shorten_line_amt(ptcopy[count - 2].X, ptcopy[count - 2].Y,
960                                      &ptcopy[count - 1].X, &ptcopy[count - 1].Y,
961                                      pen->customend->inset * pen->width);
962
963                 draw_cap(graphics, pen->brush->lb.lbColor, pen->endcap, pen->width, pen->customend,
964                          pt[count - 2].X, pt[count - 2].Y, pt[count - 1].X,
965                          pt[count - 1].Y);
966
967                 break;
968             default:
969                 ERR("Bad path last point\n");
970                 goto end;
971         }
972
973         /* Find start of points */
974         for(j = 1; j < count && ((types[j] & PathPointTypePathTypeMask)
975             == PathPointTypeStart); j++);
976
977         switch(types[j] & PathPointTypePathTypeMask){
978             case PathPointTypeBezier:
979                 if(pen->startcap == LineCapArrowAnchor)
980                     shorten_bezier_amt(&ptcopy[j - 1], pen->width, TRUE);
981                 else if((pen->startcap == LineCapCustom) && pen->customstart)
982                     shorten_bezier_amt(&ptcopy[j - 1],
983                                        pen->width * pen->customstart->inset, TRUE);
984
985                 draw_cap(graphics, pen->brush->lb.lbColor, pen->startcap, pen->width, pen->customstart,
986                     pt[j - 1].X - (ptcopy[j - 1].X - ptcopy[j].X),
987                     pt[j - 1].Y - (ptcopy[j - 1].Y - ptcopy[j].Y),
988                     pt[j - 1].X, pt[j - 1].Y);
989
990                 break;
991             case PathPointTypeLine:
992                 if(pen->startcap == LineCapArrowAnchor)
993                     shorten_line_amt(ptcopy[j].X, ptcopy[j].Y,
994                                      &ptcopy[j - 1].X, &ptcopy[j - 1].Y,
995                                      pen->width);
996                 else if((pen->startcap == LineCapCustom) && pen->customstart)
997                     shorten_line_amt(ptcopy[j].X, ptcopy[j].Y,
998                                      &ptcopy[j - 1].X, &ptcopy[j - 1].Y,
999                                      pen->customstart->inset * pen->width);
1000
1001                 draw_cap(graphics, pen->brush->lb.lbColor, pen->startcap, pen->width, pen->customstart,
1002                          pt[j].X, pt[j].Y, pt[j - 1].X,
1003                          pt[j - 1].Y);
1004
1005                 break;
1006             default:
1007                 ERR("Bad path points\n");
1008                 goto end;
1009         }
1010     }
1011
1012     transform_and_round_points(graphics, pti, ptcopy, count);
1013
1014     for(i = 0; i < count; i++){
1015         tp[i] = convert_path_point_type(types[i]);
1016     }
1017
1018     PolyDraw(graphics->hdc, pti, tp, count);
1019
1020     status = Ok;
1021
1022 end:
1023     GdipFree(pti);
1024     GdipFree(ptcopy);
1025     GdipFree(tp);
1026
1027     return status;
1028 }
1029
1030 GpStatus trace_path(GpGraphics *graphics, GpPath *path)
1031 {
1032     GpStatus result;
1033
1034     BeginPath(graphics->hdc);
1035     result = draw_poly(graphics, NULL, path->pathdata.Points,
1036                        path->pathdata.Types, path->pathdata.Count, FALSE);
1037     EndPath(graphics->hdc);
1038     return result;
1039 }
1040
1041 typedef struct _GraphicsContainerItem {
1042     struct list entry;
1043     GraphicsContainer contid;
1044
1045     SmoothingMode smoothing;
1046     CompositingQuality compqual;
1047     InterpolationMode interpolation;
1048     CompositingMode compmode;
1049     TextRenderingHint texthint;
1050     REAL scale;
1051     GpUnit unit;
1052     PixelOffsetMode pixeloffset;
1053     UINT textcontrast;
1054     GpMatrix* worldtrans;
1055     GpRegion* clip;
1056 } GraphicsContainerItem;
1057
1058 static GpStatus init_container(GraphicsContainerItem** container,
1059         GDIPCONST GpGraphics* graphics){
1060     GpStatus sts;
1061
1062     *container = GdipAlloc(sizeof(GraphicsContainerItem));
1063     if(!(*container))
1064         return OutOfMemory;
1065
1066     (*container)->contid = graphics->contid + 1;
1067
1068     (*container)->smoothing = graphics->smoothing;
1069     (*container)->compqual = graphics->compqual;
1070     (*container)->interpolation = graphics->interpolation;
1071     (*container)->compmode = graphics->compmode;
1072     (*container)->texthint = graphics->texthint;
1073     (*container)->scale = graphics->scale;
1074     (*container)->unit = graphics->unit;
1075     (*container)->textcontrast = graphics->textcontrast;
1076     (*container)->pixeloffset = graphics->pixeloffset;
1077
1078     sts = GdipCloneMatrix(graphics->worldtrans, &(*container)->worldtrans);
1079     if(sts != Ok){
1080         GdipFree(*container);
1081         *container = NULL;
1082         return sts;
1083     }
1084
1085     sts = GdipCloneRegion(graphics->clip, &(*container)->clip);
1086     if(sts != Ok){
1087         GdipDeleteMatrix((*container)->worldtrans);
1088         GdipFree(*container);
1089         *container = NULL;
1090         return sts;
1091     }
1092
1093     return Ok;
1094 }
1095
1096 static void delete_container(GraphicsContainerItem* container){
1097     GdipDeleteMatrix(container->worldtrans);
1098     GdipDeleteRegion(container->clip);
1099     GdipFree(container);
1100 }
1101
1102 static GpStatus restore_container(GpGraphics* graphics,
1103         GDIPCONST GraphicsContainerItem* container){
1104     GpStatus sts;
1105     GpMatrix *newTrans;
1106     GpRegion *newClip;
1107
1108     sts = GdipCloneMatrix(container->worldtrans, &newTrans);
1109     if(sts != Ok)
1110         return sts;
1111
1112     sts = GdipCloneRegion(container->clip, &newClip);
1113     if(sts != Ok){
1114         GdipDeleteMatrix(newTrans);
1115         return sts;
1116     }
1117
1118     GdipDeleteMatrix(graphics->worldtrans);
1119     graphics->worldtrans = newTrans;
1120
1121     GdipDeleteRegion(graphics->clip);
1122     graphics->clip = newClip;
1123
1124     graphics->contid = container->contid - 1;
1125
1126     graphics->smoothing = container->smoothing;
1127     graphics->compqual = container->compqual;
1128     graphics->interpolation = container->interpolation;
1129     graphics->compmode = container->compmode;
1130     graphics->texthint = container->texthint;
1131     graphics->scale = container->scale;
1132     graphics->unit = container->unit;
1133     graphics->textcontrast = container->textcontrast;
1134     graphics->pixeloffset = container->pixeloffset;
1135
1136     return Ok;
1137 }
1138
1139 static GpStatus get_graphics_bounds(GpGraphics* graphics, GpRectF* rect)
1140 {
1141     RECT wnd_rect;
1142     GpStatus stat=Ok;
1143     GpUnit unit;
1144
1145     if(graphics->hwnd) {
1146         if(!GetClientRect(graphics->hwnd, &wnd_rect))
1147             return GenericError;
1148
1149         rect->X = wnd_rect.left;
1150         rect->Y = wnd_rect.top;
1151         rect->Width = wnd_rect.right - wnd_rect.left;
1152         rect->Height = wnd_rect.bottom - wnd_rect.top;
1153     }else if (graphics->image){
1154         stat = GdipGetImageBounds(graphics->image, rect, &unit);
1155         if (stat == Ok && unit != UnitPixel)
1156             FIXME("need to convert from unit %i\n", unit);
1157     }else{
1158         rect->X = 0;
1159         rect->Y = 0;
1160         rect->Width = GetDeviceCaps(graphics->hdc, HORZRES);
1161         rect->Height = GetDeviceCaps(graphics->hdc, VERTRES);
1162     }
1163
1164     return stat;
1165 }
1166
1167 /* on success, rgn will contain the region of the graphics object which
1168  * is visible after clipping has been applied */
1169 static GpStatus get_visible_clip_region(GpGraphics *graphics, GpRegion *rgn)
1170 {
1171     GpStatus stat;
1172     GpRectF rectf;
1173     GpRegion* tmp;
1174
1175     if((stat = get_graphics_bounds(graphics, &rectf)) != Ok)
1176         return stat;
1177
1178     if((stat = GdipCreateRegion(&tmp)) != Ok)
1179         return stat;
1180
1181     if((stat = GdipCombineRegionRect(tmp, &rectf, CombineModeReplace)) != Ok)
1182         goto end;
1183
1184     if((stat = GdipCombineRegionRegion(tmp, graphics->clip, CombineModeIntersect)) != Ok)
1185         goto end;
1186
1187     stat = GdipCombineRegionRegion(rgn, tmp, CombineModeReplace);
1188
1189 end:
1190     GdipDeleteRegion(tmp);
1191     return stat;
1192 }
1193
1194 GpStatus WINGDIPAPI GdipCreateFromHDC(HDC hdc, GpGraphics **graphics)
1195 {
1196     TRACE("(%p, %p)\n", hdc, graphics);
1197
1198     return GdipCreateFromHDC2(hdc, NULL, graphics);
1199 }
1200
1201 GpStatus WINGDIPAPI GdipCreateFromHDC2(HDC hdc, HANDLE hDevice, GpGraphics **graphics)
1202 {
1203     GpStatus retval;
1204
1205     TRACE("(%p, %p, %p)\n", hdc, hDevice, graphics);
1206
1207     if(hDevice != NULL) {
1208         FIXME("Don't know how to handle parameter hDevice\n");
1209         return NotImplemented;
1210     }
1211
1212     if(hdc == NULL)
1213         return OutOfMemory;
1214
1215     if(graphics == NULL)
1216         return InvalidParameter;
1217
1218     *graphics = GdipAlloc(sizeof(GpGraphics));
1219     if(!*graphics)  return OutOfMemory;
1220
1221     if((retval = GdipCreateMatrix(&(*graphics)->worldtrans)) != Ok){
1222         GdipFree(*graphics);
1223         return retval;
1224     }
1225
1226     if((retval = GdipCreateRegion(&(*graphics)->clip)) != Ok){
1227         GdipFree((*graphics)->worldtrans);
1228         GdipFree(*graphics);
1229         return retval;
1230     }
1231
1232     (*graphics)->hdc = hdc;
1233     (*graphics)->hwnd = WindowFromDC(hdc);
1234     (*graphics)->owndc = FALSE;
1235     (*graphics)->smoothing = SmoothingModeDefault;
1236     (*graphics)->compqual = CompositingQualityDefault;
1237     (*graphics)->interpolation = InterpolationModeDefault;
1238     (*graphics)->pixeloffset = PixelOffsetModeDefault;
1239     (*graphics)->compmode = CompositingModeSourceOver;
1240     (*graphics)->unit = UnitDisplay;
1241     (*graphics)->scale = 1.0;
1242     (*graphics)->busy = FALSE;
1243     (*graphics)->textcontrast = 4;
1244     list_init(&(*graphics)->containers);
1245     (*graphics)->contid = 0;
1246
1247     TRACE("<-- %p\n", *graphics);
1248
1249     return Ok;
1250 }
1251
1252 GpStatus graphics_from_image(GpImage *image, GpGraphics **graphics)
1253 {
1254     GpStatus retval;
1255
1256     *graphics = GdipAlloc(sizeof(GpGraphics));
1257     if(!*graphics)  return OutOfMemory;
1258
1259     if((retval = GdipCreateMatrix(&(*graphics)->worldtrans)) != Ok){
1260         GdipFree(*graphics);
1261         return retval;
1262     }
1263
1264     if((retval = GdipCreateRegion(&(*graphics)->clip)) != Ok){
1265         GdipFree((*graphics)->worldtrans);
1266         GdipFree(*graphics);
1267         return retval;
1268     }
1269
1270     (*graphics)->hdc = NULL;
1271     (*graphics)->hwnd = NULL;
1272     (*graphics)->owndc = FALSE;
1273     (*graphics)->image = image;
1274     (*graphics)->smoothing = SmoothingModeDefault;
1275     (*graphics)->compqual = CompositingQualityDefault;
1276     (*graphics)->interpolation = InterpolationModeDefault;
1277     (*graphics)->pixeloffset = PixelOffsetModeDefault;
1278     (*graphics)->compmode = CompositingModeSourceOver;
1279     (*graphics)->unit = UnitDisplay;
1280     (*graphics)->scale = 1.0;
1281     (*graphics)->busy = FALSE;
1282     (*graphics)->textcontrast = 4;
1283     list_init(&(*graphics)->containers);
1284     (*graphics)->contid = 0;
1285
1286     TRACE("<-- %p\n", *graphics);
1287
1288     return Ok;
1289 }
1290
1291 GpStatus WINGDIPAPI GdipCreateFromHWND(HWND hwnd, GpGraphics **graphics)
1292 {
1293     GpStatus ret;
1294     HDC hdc;
1295
1296     TRACE("(%p, %p)\n", hwnd, graphics);
1297
1298     hdc = GetDC(hwnd);
1299
1300     if((ret = GdipCreateFromHDC(hdc, graphics)) != Ok)
1301     {
1302         ReleaseDC(hwnd, hdc);
1303         return ret;
1304     }
1305
1306     (*graphics)->hwnd = hwnd;
1307     (*graphics)->owndc = TRUE;
1308
1309     return Ok;
1310 }
1311
1312 /* FIXME: no icm handling */
1313 GpStatus WINGDIPAPI GdipCreateFromHWNDICM(HWND hwnd, GpGraphics **graphics)
1314 {
1315     TRACE("(%p, %p)\n", hwnd, graphics);
1316
1317     return GdipCreateFromHWND(hwnd, graphics);
1318 }
1319
1320 GpStatus WINGDIPAPI GdipCreateMetafileFromEmf(HENHMETAFILE hemf, BOOL delete,
1321     GpMetafile **metafile)
1322 {
1323     static int calls;
1324
1325     TRACE("(%p,%i,%p)\n", hemf, delete, metafile);
1326
1327     if(!hemf || !metafile)
1328         return InvalidParameter;
1329
1330     if(!(calls++))
1331         FIXME("not implemented\n");
1332
1333     return NotImplemented;
1334 }
1335
1336 GpStatus WINGDIPAPI GdipCreateMetafileFromWmf(HMETAFILE hwmf, BOOL delete,
1337     GDIPCONST WmfPlaceableFileHeader * placeable, GpMetafile **metafile)
1338 {
1339     IStream *stream = NULL;
1340     UINT read;
1341     BYTE* copy;
1342     HENHMETAFILE hemf;
1343     GpStatus retval = Ok;
1344
1345     TRACE("(%p, %d, %p, %p)\n", hwmf, delete, placeable, metafile);
1346
1347     if(!hwmf || !metafile || !placeable)
1348         return InvalidParameter;
1349
1350     *metafile = NULL;
1351     read = GetMetaFileBitsEx(hwmf, 0, NULL);
1352     if(!read)
1353         return GenericError;
1354     copy = GdipAlloc(read);
1355     GetMetaFileBitsEx(hwmf, read, copy);
1356
1357     hemf = SetWinMetaFileBits(read, copy, NULL, NULL);
1358     GdipFree(copy);
1359
1360     read = GetEnhMetaFileBits(hemf, 0, NULL);
1361     copy = GdipAlloc(read);
1362     GetEnhMetaFileBits(hemf, read, copy);
1363     DeleteEnhMetaFile(hemf);
1364
1365     if(CreateStreamOnHGlobal(copy, TRUE, &stream) != S_OK){
1366         ERR("could not make stream\n");
1367         GdipFree(copy);
1368         retval = GenericError;
1369         goto err;
1370     }
1371
1372     *metafile = GdipAlloc(sizeof(GpMetafile));
1373     if(!*metafile){
1374         retval = OutOfMemory;
1375         goto err;
1376     }
1377
1378     if(OleLoadPicture(stream, 0, FALSE, &IID_IPicture,
1379         (LPVOID*) &((*metafile)->image.picture)) != S_OK)
1380     {
1381         retval = GenericError;
1382         goto err;
1383     }
1384
1385
1386     (*metafile)->image.type = ImageTypeMetafile;
1387     memcpy(&(*metafile)->image.format, &ImageFormatWMF, sizeof(GUID));
1388     (*metafile)->image.palette_flags = 0;
1389     (*metafile)->image.palette_count = 0;
1390     (*metafile)->image.palette_size = 0;
1391     (*metafile)->image.palette_entries = NULL;
1392     (*metafile)->image.xres = (REAL)placeable->Inch;
1393     (*metafile)->image.yres = (REAL)placeable->Inch;
1394     (*metafile)->bounds.X = ((REAL) placeable->BoundingBox.Left) / ((REAL) placeable->Inch);
1395     (*metafile)->bounds.Y = ((REAL) placeable->BoundingBox.Top) / ((REAL) placeable->Inch);
1396     (*metafile)->bounds.Width = ((REAL) (placeable->BoundingBox.Right
1397                     - placeable->BoundingBox.Left));
1398     (*metafile)->bounds.Height = ((REAL) (placeable->BoundingBox.Bottom
1399                    - placeable->BoundingBox.Top));
1400     (*metafile)->unit = UnitPixel;
1401
1402     if(delete)
1403         DeleteMetaFile(hwmf);
1404
1405     TRACE("<-- %p\n", *metafile);
1406
1407 err:
1408     if (retval != Ok)
1409         GdipFree(*metafile);
1410     IStream_Release(stream);
1411     return retval;
1412 }
1413
1414 GpStatus WINGDIPAPI GdipCreateMetafileFromWmfFile(GDIPCONST WCHAR *file,
1415     GDIPCONST WmfPlaceableFileHeader * placeable, GpMetafile **metafile)
1416 {
1417     HMETAFILE hmf = GetMetaFileW(file);
1418
1419     TRACE("(%s, %p, %p)\n", debugstr_w(file), placeable, metafile);
1420
1421     if(!hmf) return InvalidParameter;
1422
1423     return GdipCreateMetafileFromWmf(hmf, TRUE, placeable, metafile);
1424 }
1425
1426 GpStatus WINGDIPAPI GdipCreateMetafileFromFile(GDIPCONST WCHAR *file,
1427     GpMetafile **metafile)
1428 {
1429     FIXME("(%p, %p): stub\n", file, metafile);
1430     return NotImplemented;
1431 }
1432
1433 GpStatus WINGDIPAPI GdipCreateMetafileFromStream(IStream *stream,
1434     GpMetafile **metafile)
1435 {
1436     FIXME("(%p, %p): stub\n", stream, metafile);
1437     return NotImplemented;
1438 }
1439
1440 GpStatus WINGDIPAPI GdipCreateStreamOnFile(GDIPCONST WCHAR * filename,
1441     UINT access, IStream **stream)
1442 {
1443     DWORD dwMode;
1444     HRESULT ret;
1445
1446     TRACE("(%s, %u, %p)\n", debugstr_w(filename), access, stream);
1447
1448     if(!stream || !filename)
1449         return InvalidParameter;
1450
1451     if(access & GENERIC_WRITE)
1452         dwMode = STGM_SHARE_DENY_WRITE | STGM_WRITE | STGM_CREATE;
1453     else if(access & GENERIC_READ)
1454         dwMode = STGM_SHARE_DENY_WRITE | STGM_READ | STGM_FAILIFTHERE;
1455     else
1456         return InvalidParameter;
1457
1458     ret = SHCreateStreamOnFileW(filename, dwMode, stream);
1459
1460     return hresult_to_status(ret);
1461 }
1462
1463 GpStatus WINGDIPAPI GdipDeleteGraphics(GpGraphics *graphics)
1464 {
1465     GraphicsContainerItem *cont, *next;
1466     TRACE("(%p)\n", graphics);
1467
1468     if(!graphics) return InvalidParameter;
1469     if(graphics->busy) return ObjectBusy;
1470
1471     if(graphics->owndc)
1472         ReleaseDC(graphics->hwnd, graphics->hdc);
1473
1474     LIST_FOR_EACH_ENTRY_SAFE(cont, next, &graphics->containers, GraphicsContainerItem, entry){
1475         list_remove(&cont->entry);
1476         delete_container(cont);
1477     }
1478
1479     GdipDeleteRegion(graphics->clip);
1480     GdipDeleteMatrix(graphics->worldtrans);
1481     GdipFree(graphics);
1482
1483     return Ok;
1484 }
1485
1486 GpStatus WINGDIPAPI GdipDrawArc(GpGraphics *graphics, GpPen *pen, REAL x,
1487     REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
1488 {
1489     INT save_state, num_pts;
1490     GpPointF points[MAX_ARC_PTS];
1491     GpStatus retval;
1492
1493     TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y,
1494           width, height, startAngle, sweepAngle);
1495
1496     if(!graphics || !pen || width <= 0 || height <= 0)
1497         return InvalidParameter;
1498
1499     if(graphics->busy)
1500         return ObjectBusy;
1501
1502     if (!graphics->hdc)
1503     {
1504         FIXME("graphics object has no HDC\n");
1505         return Ok;
1506     }
1507
1508     num_pts = arc2polybezier(points, x, y, width, height, startAngle, sweepAngle);
1509
1510     save_state = prepare_dc(graphics, pen);
1511
1512     retval = draw_polybezier(graphics, pen, points, num_pts, TRUE);
1513
1514     restore_dc(graphics, save_state);
1515
1516     return retval;
1517 }
1518
1519 GpStatus WINGDIPAPI GdipDrawArcI(GpGraphics *graphics, GpPen *pen, INT x,
1520     INT y, INT width, INT height, REAL startAngle, REAL sweepAngle)
1521 {
1522     TRACE("(%p, %p, %d, %d, %d, %d, %.2f, %.2f)\n", graphics, pen, x, y,
1523           width, height, startAngle, sweepAngle);
1524
1525     return GdipDrawArc(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height,startAngle,sweepAngle);
1526 }
1527
1528 GpStatus WINGDIPAPI GdipDrawBezier(GpGraphics *graphics, GpPen *pen, REAL x1,
1529     REAL y1, REAL x2, REAL y2, REAL x3, REAL y3, REAL x4, REAL y4)
1530 {
1531     INT save_state;
1532     GpPointF pt[4];
1533     GpStatus retval;
1534
1535     TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x1, y1,
1536           x2, y2, x3, y3, x4, y4);
1537
1538     if(!graphics || !pen)
1539         return InvalidParameter;
1540
1541     if(graphics->busy)
1542         return ObjectBusy;
1543
1544     if (!graphics->hdc)
1545     {
1546         FIXME("graphics object has no HDC\n");
1547         return Ok;
1548     }
1549
1550     pt[0].X = x1;
1551     pt[0].Y = y1;
1552     pt[1].X = x2;
1553     pt[1].Y = y2;
1554     pt[2].X = x3;
1555     pt[2].Y = y3;
1556     pt[3].X = x4;
1557     pt[3].Y = y4;
1558
1559     save_state = prepare_dc(graphics, pen);
1560
1561     retval = draw_polybezier(graphics, pen, pt, 4, TRUE);
1562
1563     restore_dc(graphics, save_state);
1564
1565     return retval;
1566 }
1567
1568 GpStatus WINGDIPAPI GdipDrawBezierI(GpGraphics *graphics, GpPen *pen, INT x1,
1569     INT y1, INT x2, INT y2, INT x3, INT y3, INT x4, INT y4)
1570 {
1571     INT save_state;
1572     GpPointF pt[4];
1573     GpStatus retval;
1574
1575     TRACE("(%p, %p, %d, %d, %d, %d, %d, %d, %d, %d)\n", graphics, pen, x1, y1,
1576           x2, y2, x3, y3, x4, y4);
1577
1578     if(!graphics || !pen)
1579         return InvalidParameter;
1580
1581     if(graphics->busy)
1582         return ObjectBusy;
1583
1584     if (!graphics->hdc)
1585     {
1586         FIXME("graphics object has no HDC\n");
1587         return Ok;
1588     }
1589
1590     pt[0].X = x1;
1591     pt[0].Y = y1;
1592     pt[1].X = x2;
1593     pt[1].Y = y2;
1594     pt[2].X = x3;
1595     pt[2].Y = y3;
1596     pt[3].X = x4;
1597     pt[3].Y = y4;
1598
1599     save_state = prepare_dc(graphics, pen);
1600
1601     retval = draw_polybezier(graphics, pen, pt, 4, TRUE);
1602
1603     restore_dc(graphics, save_state);
1604
1605     return retval;
1606 }
1607
1608 GpStatus WINGDIPAPI GdipDrawBeziers(GpGraphics *graphics, GpPen *pen,
1609     GDIPCONST GpPointF *points, INT count)
1610 {
1611     INT i;
1612     GpStatus ret;
1613
1614     TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
1615
1616     if(!graphics || !pen || !points || (count <= 0))
1617         return InvalidParameter;
1618
1619     if(graphics->busy)
1620         return ObjectBusy;
1621
1622     for(i = 0; i < floor(count / 4); i++){
1623         ret = GdipDrawBezier(graphics, pen,
1624                              points[4*i].X, points[4*i].Y,
1625                              points[4*i + 1].X, points[4*i + 1].Y,
1626                              points[4*i + 2].X, points[4*i + 2].Y,
1627                              points[4*i + 3].X, points[4*i + 3].Y);
1628         if(ret != Ok)
1629             return ret;
1630     }
1631
1632     return Ok;
1633 }
1634
1635 GpStatus WINGDIPAPI GdipDrawBeziersI(GpGraphics *graphics, GpPen *pen,
1636     GDIPCONST GpPoint *points, INT count)
1637 {
1638     GpPointF *pts;
1639     GpStatus ret;
1640     INT i;
1641
1642     TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
1643
1644     if(!graphics || !pen || !points || (count <= 0))
1645         return InvalidParameter;
1646
1647     if(graphics->busy)
1648         return ObjectBusy;
1649
1650     pts = GdipAlloc(sizeof(GpPointF) * count);
1651     if(!pts)
1652         return OutOfMemory;
1653
1654     for(i = 0; i < count; i++){
1655         pts[i].X = (REAL)points[i].X;
1656         pts[i].Y = (REAL)points[i].Y;
1657     }
1658
1659     ret = GdipDrawBeziers(graphics,pen,pts,count);
1660
1661     GdipFree(pts);
1662
1663     return ret;
1664 }
1665
1666 GpStatus WINGDIPAPI GdipDrawClosedCurve(GpGraphics *graphics, GpPen *pen,
1667     GDIPCONST GpPointF *points, INT count)
1668 {
1669     TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
1670
1671     return GdipDrawClosedCurve2(graphics, pen, points, count, 1.0);
1672 }
1673
1674 GpStatus WINGDIPAPI GdipDrawClosedCurveI(GpGraphics *graphics, GpPen *pen,
1675     GDIPCONST GpPoint *points, INT count)
1676 {
1677     TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
1678
1679     return GdipDrawClosedCurve2I(graphics, pen, points, count, 1.0);
1680 }
1681
1682 GpStatus WINGDIPAPI GdipDrawClosedCurve2(GpGraphics *graphics, GpPen *pen,
1683     GDIPCONST GpPointF *points, INT count, REAL tension)
1684 {
1685     GpPath *path;
1686     GpStatus stat;
1687
1688     TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
1689
1690     if(!graphics || !pen || !points || count <= 0)
1691         return InvalidParameter;
1692
1693     if(graphics->busy)
1694         return ObjectBusy;
1695
1696     if((stat = GdipCreatePath(FillModeAlternate, &path)) != Ok)
1697         return stat;
1698
1699     stat = GdipAddPathClosedCurve2(path, points, count, tension);
1700     if(stat != Ok){
1701         GdipDeletePath(path);
1702         return stat;
1703     }
1704
1705     stat = GdipDrawPath(graphics, pen, path);
1706
1707     GdipDeletePath(path);
1708
1709     return stat;
1710 }
1711
1712 GpStatus WINGDIPAPI GdipDrawClosedCurve2I(GpGraphics *graphics, GpPen *pen,
1713     GDIPCONST GpPoint *points, INT count, REAL tension)
1714 {
1715     GpPointF *ptf;
1716     GpStatus stat;
1717     INT i;
1718
1719     TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
1720
1721     if(!points || count <= 0)
1722         return InvalidParameter;
1723
1724     ptf = GdipAlloc(sizeof(GpPointF)*count);
1725     if(!ptf)
1726         return OutOfMemory;
1727
1728     for(i = 0; i < count; i++){
1729         ptf[i].X = (REAL)points[i].X;
1730         ptf[i].Y = (REAL)points[i].Y;
1731     }
1732
1733     stat = GdipDrawClosedCurve2(graphics, pen, ptf, count, tension);
1734
1735     GdipFree(ptf);
1736
1737     return stat;
1738 }
1739
1740 GpStatus WINGDIPAPI GdipDrawCurve(GpGraphics *graphics, GpPen *pen,
1741     GDIPCONST GpPointF *points, INT count)
1742 {
1743     TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
1744
1745     return GdipDrawCurve2(graphics,pen,points,count,1.0);
1746 }
1747
1748 GpStatus WINGDIPAPI GdipDrawCurveI(GpGraphics *graphics, GpPen *pen,
1749     GDIPCONST GpPoint *points, INT count)
1750 {
1751     GpPointF *pointsF;
1752     GpStatus ret;
1753     INT i;
1754
1755     TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
1756
1757     if(!points)
1758         return InvalidParameter;
1759
1760     pointsF = GdipAlloc(sizeof(GpPointF)*count);
1761     if(!pointsF)
1762         return OutOfMemory;
1763
1764     for(i = 0; i < count; i++){
1765         pointsF[i].X = (REAL)points[i].X;
1766         pointsF[i].Y = (REAL)points[i].Y;
1767     }
1768
1769     ret = GdipDrawCurve(graphics,pen,pointsF,count);
1770     GdipFree(pointsF);
1771
1772     return ret;
1773 }
1774
1775 /* Approximates cardinal spline with Bezier curves. */
1776 GpStatus WINGDIPAPI GdipDrawCurve2(GpGraphics *graphics, GpPen *pen,
1777     GDIPCONST GpPointF *points, INT count, REAL tension)
1778 {
1779     /* PolyBezier expects count*3-2 points. */
1780     INT i, len_pt = count*3-2, save_state;
1781     GpPointF *pt;
1782     REAL x1, x2, y1, y2;
1783     GpStatus retval;
1784
1785     TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
1786
1787     if(!graphics || !pen)
1788         return InvalidParameter;
1789
1790     if(graphics->busy)
1791         return ObjectBusy;
1792
1793     if(count < 2)
1794         return InvalidParameter;
1795
1796     if (!graphics->hdc)
1797     {
1798         FIXME("graphics object has no HDC\n");
1799         return Ok;
1800     }
1801
1802     pt = GdipAlloc(len_pt * sizeof(GpPointF));
1803     if(!pt)
1804         return OutOfMemory;
1805
1806     tension = tension * TENSION_CONST;
1807
1808     calc_curve_bezier_endp(points[0].X, points[0].Y, points[1].X, points[1].Y,
1809         tension, &x1, &y1);
1810
1811     pt[0].X = points[0].X;
1812     pt[0].Y = points[0].Y;
1813     pt[1].X = x1;
1814     pt[1].Y = y1;
1815
1816     for(i = 0; i < count-2; i++){
1817         calc_curve_bezier(&(points[i]), tension, &x1, &y1, &x2, &y2);
1818
1819         pt[3*i+2].X = x1;
1820         pt[3*i+2].Y = y1;
1821         pt[3*i+3].X = points[i+1].X;
1822         pt[3*i+3].Y = points[i+1].Y;
1823         pt[3*i+4].X = x2;
1824         pt[3*i+4].Y = y2;
1825     }
1826
1827     calc_curve_bezier_endp(points[count-1].X, points[count-1].Y,
1828         points[count-2].X, points[count-2].Y, tension, &x1, &y1);
1829
1830     pt[len_pt-2].X = x1;
1831     pt[len_pt-2].Y = y1;
1832     pt[len_pt-1].X = points[count-1].X;
1833     pt[len_pt-1].Y = points[count-1].Y;
1834
1835     save_state = prepare_dc(graphics, pen);
1836
1837     retval = draw_polybezier(graphics, pen, pt, len_pt, TRUE);
1838
1839     GdipFree(pt);
1840     restore_dc(graphics, save_state);
1841
1842     return retval;
1843 }
1844
1845 GpStatus WINGDIPAPI GdipDrawCurve2I(GpGraphics *graphics, GpPen *pen,
1846     GDIPCONST GpPoint *points, INT count, REAL tension)
1847 {
1848     GpPointF *pointsF;
1849     GpStatus ret;
1850     INT i;
1851
1852     TRACE("(%p, %p, %p, %d, %.2f)\n", graphics, pen, points, count, tension);
1853
1854     if(!points)
1855         return InvalidParameter;
1856
1857     pointsF = GdipAlloc(sizeof(GpPointF)*count);
1858     if(!pointsF)
1859         return OutOfMemory;
1860
1861     for(i = 0; i < count; i++){
1862         pointsF[i].X = (REAL)points[i].X;
1863         pointsF[i].Y = (REAL)points[i].Y;
1864     }
1865
1866     ret = GdipDrawCurve2(graphics,pen,pointsF,count,tension);
1867     GdipFree(pointsF);
1868
1869     return ret;
1870 }
1871
1872 GpStatus WINGDIPAPI GdipDrawCurve3(GpGraphics *graphics, GpPen *pen,
1873     GDIPCONST GpPointF *points, INT count, INT offset, INT numberOfSegments,
1874     REAL tension)
1875 {
1876     TRACE("(%p, %p, %p, %d, %d, %d, %.2f)\n", graphics, pen, points, count, offset, numberOfSegments, tension);
1877
1878     if(offset >= count || numberOfSegments > count - offset - 1 || numberOfSegments <= 0){
1879         return InvalidParameter;
1880     }
1881
1882     return GdipDrawCurve2(graphics, pen, points + offset, numberOfSegments + 1, tension);
1883 }
1884
1885 GpStatus WINGDIPAPI GdipDrawCurve3I(GpGraphics *graphics, GpPen *pen,
1886     GDIPCONST GpPoint *points, INT count, INT offset, INT numberOfSegments,
1887     REAL tension)
1888 {
1889     TRACE("(%p, %p, %p, %d, %d, %d, %.2f)\n", graphics, pen, points, count, offset, numberOfSegments, tension);
1890
1891     if(count < 0){
1892         return OutOfMemory;
1893     }
1894
1895     if(offset >= count || numberOfSegments > count - offset - 1 || numberOfSegments <= 0){
1896         return InvalidParameter;
1897     }
1898
1899     return GdipDrawCurve2I(graphics, pen, points + offset, numberOfSegments + 1, tension);
1900 }
1901
1902 GpStatus WINGDIPAPI GdipDrawEllipse(GpGraphics *graphics, GpPen *pen, REAL x,
1903     REAL y, REAL width, REAL height)
1904 {
1905     INT save_state;
1906     GpPointF ptf[2];
1907     POINT pti[2];
1908
1909     TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y, width, height);
1910
1911     if(!graphics || !pen)
1912         return InvalidParameter;
1913
1914     if(graphics->busy)
1915         return ObjectBusy;
1916
1917     if (!graphics->hdc)
1918     {
1919         FIXME("graphics object has no HDC\n");
1920         return Ok;
1921     }
1922
1923     ptf[0].X = x;
1924     ptf[0].Y = y;
1925     ptf[1].X = x + width;
1926     ptf[1].Y = y + height;
1927
1928     save_state = prepare_dc(graphics, pen);
1929     SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
1930
1931     transform_and_round_points(graphics, pti, ptf, 2);
1932
1933     Ellipse(graphics->hdc, pti[0].x, pti[0].y, pti[1].x, pti[1].y);
1934
1935     restore_dc(graphics, save_state);
1936
1937     return Ok;
1938 }
1939
1940 GpStatus WINGDIPAPI GdipDrawEllipseI(GpGraphics *graphics, GpPen *pen, INT x,
1941     INT y, INT width, INT height)
1942 {
1943     TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, pen, x, y, width, height);
1944
1945     return GdipDrawEllipse(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
1946 }
1947
1948
1949 GpStatus WINGDIPAPI GdipDrawImage(GpGraphics *graphics, GpImage *image, REAL x, REAL y)
1950 {
1951     UINT width, height;
1952     GpPointF points[3];
1953
1954     TRACE("(%p, %p, %.2f, %.2f)\n", graphics, image, x, y);
1955
1956     if(!graphics || !image)
1957         return InvalidParameter;
1958
1959     GdipGetImageWidth(image, &width);
1960     GdipGetImageHeight(image, &height);
1961
1962     /* FIXME: we should use the graphics and image dpi, somehow */
1963
1964     points[0].X = points[2].X = x;
1965     points[0].Y = points[1].Y = y;
1966     points[1].X = x + width;
1967     points[2].Y = y + height;
1968
1969     return GdipDrawImagePointsRect(graphics, image, points, 3, 0, 0, width, height,
1970         UnitPixel, NULL, NULL, NULL);
1971 }
1972
1973 GpStatus WINGDIPAPI GdipDrawImageI(GpGraphics *graphics, GpImage *image, INT x,
1974     INT y)
1975 {
1976     TRACE("(%p, %p, %d, %d)\n", graphics, image, x, y);
1977
1978     return GdipDrawImage(graphics, image, (REAL)x, (REAL)y);
1979 }
1980
1981 GpStatus WINGDIPAPI GdipDrawImagePointRect(GpGraphics *graphics, GpImage *image,
1982     REAL x, REAL y, REAL srcx, REAL srcy, REAL srcwidth, REAL srcheight,
1983     GpUnit srcUnit)
1984 {
1985     GpPointF points[3];
1986     TRACE("(%p, %p, %f, %f, %f, %f, %f, %f, %d)\n", graphics, image, x, y, srcx, srcy, srcwidth, srcheight, srcUnit);
1987
1988     points[0].X = points[2].X = x;
1989     points[0].Y = points[1].Y = y;
1990
1991     /* FIXME: convert image coordinates to Graphics coordinates? */
1992     points[1].X = x + srcwidth;
1993     points[2].Y = y + srcheight;
1994
1995     return GdipDrawImagePointsRect(graphics, image, points, 3, srcx, srcy,
1996         srcwidth, srcheight, srcUnit, NULL, NULL, NULL);
1997 }
1998
1999 GpStatus WINGDIPAPI GdipDrawImagePointRectI(GpGraphics *graphics, GpImage *image,
2000     INT x, INT y, INT srcx, INT srcy, INT srcwidth, INT srcheight,
2001     GpUnit srcUnit)
2002 {
2003     return GdipDrawImagePointRect(graphics, image, x, y, srcx, srcy, srcwidth, srcheight, srcUnit);
2004 }
2005
2006 GpStatus WINGDIPAPI GdipDrawImagePoints(GpGraphics *graphics, GpImage *image,
2007     GDIPCONST GpPointF *dstpoints, INT count)
2008 {
2009     FIXME("(%p, %p, %p, %d): stub\n", graphics, image, dstpoints, count);
2010     return NotImplemented;
2011 }
2012
2013 GpStatus WINGDIPAPI GdipDrawImagePointsI(GpGraphics *graphics, GpImage *image,
2014     GDIPCONST GpPoint *dstpoints, INT count)
2015 {
2016     FIXME("(%p, %p, %p, %d): stub\n", graphics, image, dstpoints, count);
2017     return NotImplemented;
2018 }
2019
2020 GpStatus WINGDIPAPI GdipDrawImagePointsRect(GpGraphics *graphics, GpImage *image,
2021      GDIPCONST GpPointF *points, INT count, REAL srcx, REAL srcy, REAL srcwidth,
2022      REAL srcheight, GpUnit srcUnit, GDIPCONST GpImageAttributes* imageAttributes,
2023      DrawImageAbort callback, VOID * callbackData)
2024 {
2025     GpPointF ptf[4];
2026     POINT pti[4];
2027     REAL dx, dy;
2028     GpStatus stat;
2029
2030     TRACE("(%p, %p, %p, %d, %f, %f, %f, %f, %d, %p, %p, %p)\n", graphics, image, points,
2031           count, srcx, srcy, srcwidth, srcheight, srcUnit, imageAttributes, callback,
2032           callbackData);
2033
2034     if(!graphics || !image || !points || count != 3)
2035          return InvalidParameter;
2036
2037     TRACE("%s %s %s\n", debugstr_pointf(&points[0]), debugstr_pointf(&points[1]),
2038         debugstr_pointf(&points[2]));
2039
2040     memcpy(ptf, points, 3 * sizeof(GpPointF));
2041     ptf[3].X = ptf[2].X + ptf[1].X - ptf[0].X;
2042     ptf[3].Y = ptf[2].Y + ptf[1].Y - ptf[0].Y;
2043     transform_and_round_points(graphics, pti, ptf, 4);
2044
2045     if (image->picture)
2046     {
2047         if (!graphics->hdc)
2048         {
2049             FIXME("graphics object has no HDC\n");
2050         }
2051
2052         /* FIXME: partially implemented (only works for rectangular parallelograms) */
2053         if(srcUnit == UnitInch)
2054             dx = dy = (REAL) INCH_HIMETRIC;
2055         else if(srcUnit == UnitPixel){
2056             dx = ((REAL) INCH_HIMETRIC) /
2057                  ((REAL) GetDeviceCaps(graphics->hdc, LOGPIXELSX));
2058             dy = ((REAL) INCH_HIMETRIC) /
2059                  ((REAL) GetDeviceCaps(graphics->hdc, LOGPIXELSY));
2060         }
2061         else
2062             return NotImplemented;
2063
2064         if(IPicture_Render(image->picture, graphics->hdc,
2065             pti[0].x, pti[0].y, pti[1].x - pti[0].x, pti[2].y - pti[0].y,
2066             srcx * dx, srcy * dy,
2067             srcwidth * dx, srcheight * dy,
2068             NULL) != S_OK){
2069             if(callback)
2070                 callback(callbackData);
2071             return GenericError;
2072         }
2073     }
2074     else if (image->type == ImageTypeBitmap)
2075     {
2076         GpBitmap* bitmap = (GpBitmap*)image;
2077         int use_software=0;
2078
2079         if (srcUnit == UnitInch)
2080             dx = dy = 96.0; /* FIXME: use the image resolution */
2081         else if (srcUnit == UnitPixel)
2082             dx = dy = 1.0;
2083         else
2084             return NotImplemented;
2085
2086         if (imageAttributes ||
2087             (graphics->image && graphics->image->type == ImageTypeBitmap) ||
2088             !((GpBitmap*)image)->hbitmap ||
2089             ptf[1].Y != ptf[0].Y || ptf[2].X != ptf[0].X)
2090             use_software = 1;
2091
2092         if (use_software)
2093         {
2094             RECT src_area, dst_area;
2095             int i, x, y, stride;
2096             GpMatrix *dst_to_src;
2097             REAL m11, m12, m21, m22, mdx, mdy;
2098             LPBYTE data;
2099
2100             src_area.left = srcx*dx;
2101             src_area.top = srcy*dy;
2102             src_area.right = (srcx+srcwidth)*dx;
2103             src_area.bottom = (srcy+srcheight)*dy;
2104
2105             dst_area.left = dst_area.right = pti[0].x;
2106             dst_area.top = dst_area.bottom = pti[0].y;
2107             for (i=1; i<4; i++)
2108             {
2109                 if (dst_area.left > pti[i].x) dst_area.left = pti[i].x;
2110                 if (dst_area.right < pti[i].x) dst_area.right = pti[i].x;
2111                 if (dst_area.top > pti[i].y) dst_area.top = pti[i].y;
2112                 if (dst_area.bottom < pti[i].y) dst_area.bottom = pti[i].y;
2113             }
2114
2115             m11 = (ptf[1].X - ptf[0].X) / srcwidth;
2116             m21 = (ptf[2].X - ptf[0].X) / srcheight;
2117             mdx = ptf[0].X - m11 * srcx - m21 * srcy;
2118             m12 = (ptf[1].Y - ptf[0].Y) / srcwidth;
2119             m22 = (ptf[2].Y - ptf[0].Y) / srcheight;
2120             mdy = ptf[0].Y - m12 * srcx - m22 * srcy;
2121
2122             stat = GdipCreateMatrix2(m11, m12, m21, m22, mdx, mdy, &dst_to_src);
2123             if (stat != Ok) return stat;
2124
2125             stat = GdipInvertMatrix(dst_to_src);
2126             if (stat != Ok)
2127             {
2128                 GdipDeleteMatrix(dst_to_src);
2129                 return stat;
2130             }
2131
2132             data = GdipAlloc(sizeof(ARGB) * (dst_area.right - dst_area.left) * (dst_area.bottom - dst_area.top));
2133             if (!data)
2134             {
2135                 GdipDeleteMatrix(dst_to_src);
2136                 return OutOfMemory;
2137             }
2138
2139             stride = sizeof(ARGB) * (dst_area.right - dst_area.left);
2140
2141             if (imageAttributes &&
2142                 (imageAttributes->wrap != WrapModeClamp ||
2143                  imageAttributes->outside_color != 0x00000000 ||
2144                  imageAttributes->clamp))
2145             {
2146                 static int fixme;
2147                 if (!fixme++)
2148                     FIXME("Image wrap mode not implemented\n");
2149             }
2150
2151             for (x=dst_area.left; x<dst_area.right; x++)
2152             {
2153                 for (y=dst_area.top; y<dst_area.bottom; y++)
2154                 {
2155                     GpPointF src_pointf;
2156                     int src_x, src_y;
2157                     ARGB *src_color;
2158
2159                     src_pointf.X = x;
2160                     src_pointf.Y = y;
2161
2162                     GdipTransformMatrixPoints(dst_to_src, &src_pointf, 1);
2163
2164                     src_x = roundr(src_pointf.X);
2165                     src_y = roundr(src_pointf.Y);
2166
2167                     src_color = (ARGB*)(data + stride * (y - dst_area.top) + sizeof(ARGB) * (x - dst_area.left));
2168
2169                     if (src_x < src_area.left || src_x >= src_area.right ||
2170                         src_y < src_area.top || src_y >= src_area.bottom)
2171                         *src_color = 0;
2172                     else
2173                         GdipBitmapGetPixel(bitmap, src_x, src_y, src_color);
2174                 }
2175             }
2176
2177             GdipDeleteMatrix(dst_to_src);
2178
2179             if (imageAttributes)
2180             {
2181                 if (imageAttributes->colorkeys[ColorAdjustTypeBitmap].enabled ||
2182                     imageAttributes->colorkeys[ColorAdjustTypeDefault].enabled)
2183                 {
2184                     const struct color_key *key;
2185                     BYTE min_blue, min_green, min_red;
2186                     BYTE max_blue, max_green, max_red;
2187
2188                     if (imageAttributes->colorkeys[ColorAdjustTypeBitmap].enabled)
2189                         key = &imageAttributes->colorkeys[ColorAdjustTypeBitmap];
2190                     else
2191                         key = &imageAttributes->colorkeys[ColorAdjustTypeDefault];
2192
2193                     min_blue = key->low&0xff;
2194                     min_green = (key->low>>8)&0xff;
2195                     min_red = (key->low>>16)&0xff;
2196
2197                     max_blue = key->high&0xff;
2198                     max_green = (key->high>>8)&0xff;
2199                     max_red = (key->high>>16)&0xff;
2200
2201                     for (x=dst_area.left; x<dst_area.right; x++)
2202                         for (y=dst_area.top; y<dst_area.bottom; y++)
2203                         {
2204                             ARGB *src_color;
2205                             BYTE blue, green, red;
2206                             src_color = (ARGB*)(data + stride * (y - dst_area.top) + sizeof(ARGB) * (x - dst_area.left));
2207                             blue = *src_color&0xff;
2208                             green = (*src_color>>8)&0xff;
2209                             red = (*src_color>>16)&0xff;
2210                             if (blue >= min_blue && green >= min_green && red >= min_red &&
2211                                 blue <= max_blue && green <= max_green && red <= max_red)
2212                                 *src_color = 0x00000000;
2213                         }
2214                 }
2215
2216                 if (imageAttributes->colorremaptables[ColorAdjustTypeBitmap].enabled ||
2217                     imageAttributes->colorremaptables[ColorAdjustTypeDefault].enabled)
2218                 {
2219                     const struct color_remap_table *table;
2220
2221                     if (imageAttributes->colorremaptables[ColorAdjustTypeBitmap].enabled)
2222                         table = &imageAttributes->colorremaptables[ColorAdjustTypeBitmap];
2223                     else
2224                         table = &imageAttributes->colorremaptables[ColorAdjustTypeDefault];
2225
2226                     for (x=dst_area.left; x<dst_area.right; x++)
2227                         for (y=dst_area.top; y<dst_area.bottom; y++)
2228                         {
2229                             ARGB *src_color;
2230                             src_color = (ARGB*)(data + stride * (y - dst_area.top) + sizeof(ARGB) * (x - dst_area.left));
2231                             for (i=0; i<table->mapsize; i++)
2232                             {
2233                                 if (*src_color == table->colormap[i].oldColor.Argb)
2234                                 {
2235                                     *src_color = table->colormap[i].newColor.Argb;
2236                                     break;
2237                                 }
2238                             }
2239                         }
2240                 }
2241
2242                 if (imageAttributes->colormatrices[ColorAdjustTypeBitmap].enabled ||
2243                     imageAttributes->colormatrices[ColorAdjustTypeDefault].enabled)
2244                 {
2245                     static int fixme;
2246                     if (!fixme++)
2247                         FIXME("Color transforms not implemented\n");
2248                 }
2249
2250                 if (imageAttributes->gamma_enabled[ColorAdjustTypeBitmap] ||
2251                     imageAttributes->gamma_enabled[ColorAdjustTypeDefault])
2252                 {
2253                     static int fixme;
2254                     if (!fixme++)
2255                         FIXME("Gamma adjustment not implemented\n");
2256                 }
2257             }
2258
2259             stat = alpha_blend_pixels(graphics, dst_area.left, dst_area.top,
2260                 data, dst_area.right - dst_area.left, dst_area.bottom - dst_area.top, stride);
2261
2262             GdipFree(data);
2263
2264             return stat;
2265         }
2266         else
2267         {
2268             HDC hdc;
2269             int temp_hdc=0, temp_bitmap=0;
2270             HBITMAP hbitmap, old_hbm=NULL;
2271
2272             if (!(bitmap->format == PixelFormat16bppRGB555 ||
2273                   bitmap->format == PixelFormat24bppRGB ||
2274                   bitmap->format == PixelFormat32bppRGB ||
2275                   bitmap->format == PixelFormat32bppPARGB))
2276             {
2277                 BITMAPINFOHEADER bih;
2278                 BYTE *temp_bits;
2279                 PixelFormat dst_format;
2280
2281                 /* we can't draw a bitmap of this format directly */
2282                 hdc = CreateCompatibleDC(0);
2283                 temp_hdc = 1;
2284                 temp_bitmap = 1;
2285
2286                 bih.biSize = sizeof(BITMAPINFOHEADER);
2287                 bih.biWidth = bitmap->width;
2288                 bih.biHeight = -bitmap->height;
2289                 bih.biPlanes = 1;
2290                 bih.biBitCount = 32;
2291                 bih.biCompression = BI_RGB;
2292                 bih.biSizeImage = 0;
2293                 bih.biXPelsPerMeter = 0;
2294                 bih.biYPelsPerMeter = 0;
2295                 bih.biClrUsed = 0;
2296                 bih.biClrImportant = 0;
2297
2298                 hbitmap = CreateDIBSection(hdc, (BITMAPINFO*)&bih, DIB_RGB_COLORS,
2299                     (void**)&temp_bits, NULL, 0);
2300
2301                 if (bitmap->format & (PixelFormatAlpha|PixelFormatPAlpha))
2302                     dst_format = PixelFormat32bppPARGB;
2303                 else
2304                     dst_format = PixelFormat32bppRGB;
2305
2306                 convert_pixels(bitmap->width, bitmap->height,
2307                     bitmap->width*4, temp_bits, dst_format,
2308                     bitmap->stride, bitmap->bits, bitmap->format, bitmap->image.palette_entries);
2309             }
2310             else
2311             {
2312                 hbitmap = bitmap->hbitmap;
2313                 hdc = bitmap->hdc;
2314                 temp_hdc = (hdc == 0);
2315             }
2316
2317             if (temp_hdc)
2318             {
2319                 if (!hdc) hdc = CreateCompatibleDC(0);
2320                 old_hbm = SelectObject(hdc, hbitmap);
2321             }
2322
2323             if (bitmap->format & (PixelFormatAlpha|PixelFormatPAlpha))
2324             {
2325                 BLENDFUNCTION bf;
2326
2327                 bf.BlendOp = AC_SRC_OVER;
2328                 bf.BlendFlags = 0;
2329                 bf.SourceConstantAlpha = 255;
2330                 bf.AlphaFormat = AC_SRC_ALPHA;
2331
2332                 GdiAlphaBlend(graphics->hdc, pti[0].x, pti[0].y, pti[1].x-pti[0].x, pti[2].y-pti[0].y,
2333                     hdc, srcx*dx, srcy*dy, srcwidth*dx, srcheight*dy, bf);
2334             }
2335             else
2336             {
2337                 StretchBlt(graphics->hdc, pti[0].x, pti[0].y, pti[1].x-pti[0].x, pti[2].y-pti[0].y,
2338                     hdc, srcx*dx, srcy*dy, srcwidth*dx, srcheight*dy, SRCCOPY);
2339             }
2340
2341             if (temp_hdc)
2342             {
2343                 SelectObject(hdc, old_hbm);
2344                 DeleteDC(hdc);
2345             }
2346
2347             if (temp_bitmap)
2348                 DeleteObject(hbitmap);
2349         }
2350     }
2351     else
2352     {
2353         ERR("GpImage with no IPicture or HBITMAP?!\n");
2354         return NotImplemented;
2355     }
2356
2357     return Ok;
2358 }
2359
2360 GpStatus WINGDIPAPI GdipDrawImagePointsRectI(GpGraphics *graphics, GpImage *image,
2361      GDIPCONST GpPoint *points, INT count, INT srcx, INT srcy, INT srcwidth,
2362      INT srcheight, GpUnit srcUnit, GDIPCONST GpImageAttributes* imageAttributes,
2363      DrawImageAbort callback, VOID * callbackData)
2364 {
2365     GpPointF pointsF[3];
2366     INT i;
2367
2368     TRACE("(%p, %p, %p, %d, %d, %d, %d, %d, %d, %p, %p, %p)\n", graphics, image, points, count,
2369           srcx, srcy, srcwidth, srcheight, srcUnit, imageAttributes, callback,
2370           callbackData);
2371
2372     if(!points || count!=3)
2373         return InvalidParameter;
2374
2375     for(i = 0; i < count; i++){
2376         pointsF[i].X = (REAL)points[i].X;
2377         pointsF[i].Y = (REAL)points[i].Y;
2378     }
2379
2380     return GdipDrawImagePointsRect(graphics, image, pointsF, count, (REAL)srcx, (REAL)srcy,
2381                                    (REAL)srcwidth, (REAL)srcheight, srcUnit, imageAttributes,
2382                                    callback, callbackData);
2383 }
2384
2385 GpStatus WINGDIPAPI GdipDrawImageRectRect(GpGraphics *graphics, GpImage *image,
2386     REAL dstx, REAL dsty, REAL dstwidth, REAL dstheight, REAL srcx, REAL srcy,
2387     REAL srcwidth, REAL srcheight, GpUnit srcUnit,
2388     GDIPCONST GpImageAttributes* imageattr, DrawImageAbort callback,
2389     VOID * callbackData)
2390 {
2391     GpPointF points[3];
2392
2393     TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f, %d, %p, %p, %p)\n",
2394           graphics, image, dstx, dsty, dstwidth, dstheight, srcx, srcy,
2395           srcwidth, srcheight, srcUnit, imageattr, callback, callbackData);
2396
2397     points[0].X = dstx;
2398     points[0].Y = dsty;
2399     points[1].X = dstx + dstwidth;
2400     points[1].Y = dsty;
2401     points[2].X = dstx;
2402     points[2].Y = dsty + dstheight;
2403
2404     return GdipDrawImagePointsRect(graphics, image, points, 3, srcx, srcy,
2405                srcwidth, srcheight, srcUnit, imageattr, callback, callbackData);
2406 }
2407
2408 GpStatus WINGDIPAPI GdipDrawImageRectRectI(GpGraphics *graphics, GpImage *image,
2409         INT dstx, INT dsty, INT dstwidth, INT dstheight, INT srcx, INT srcy,
2410         INT srcwidth, INT srcheight, GpUnit srcUnit,
2411         GDIPCONST GpImageAttributes* imageAttributes, DrawImageAbort callback,
2412         VOID * callbackData)
2413 {
2414     GpPointF points[3];
2415
2416     TRACE("(%p, %p, %d, %d, %d, %d, %d, %d, %d, %d, %d, %p, %p, %p)\n",
2417           graphics, image, dstx, dsty, dstwidth, dstheight, srcx, srcy,
2418           srcwidth, srcheight, srcUnit, imageAttributes, callback, callbackData);
2419
2420     points[0].X = dstx;
2421     points[0].Y = dsty;
2422     points[1].X = dstx + dstwidth;
2423     points[1].Y = dsty;
2424     points[2].X = dstx;
2425     points[2].Y = dsty + dstheight;
2426
2427     return GdipDrawImagePointsRect(graphics, image, points, 3, srcx, srcy,
2428                srcwidth, srcheight, srcUnit, imageAttributes, callback, callbackData);
2429 }
2430
2431 GpStatus WINGDIPAPI GdipDrawImageRect(GpGraphics *graphics, GpImage *image,
2432     REAL x, REAL y, REAL width, REAL height)
2433 {
2434     RectF bounds;
2435     GpUnit unit;
2436     GpStatus ret;
2437
2438     TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, image, x, y, width, height);
2439
2440     if(!graphics || !image)
2441         return InvalidParameter;
2442
2443     ret = GdipGetImageBounds(image, &bounds, &unit);
2444     if(ret != Ok)
2445         return ret;
2446
2447     return GdipDrawImageRectRect(graphics, image, x, y, width, height,
2448                                  bounds.X, bounds.Y, bounds.Width, bounds.Height,
2449                                  unit, NULL, NULL, NULL);
2450 }
2451
2452 GpStatus WINGDIPAPI GdipDrawImageRectI(GpGraphics *graphics, GpImage *image,
2453     INT x, INT y, INT width, INT height)
2454 {
2455     TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, image, x, y, width, height);
2456
2457     return GdipDrawImageRect(graphics, image, (REAL)x, (REAL)y, (REAL)width, (REAL)height);
2458 }
2459
2460 GpStatus WINGDIPAPI GdipDrawLine(GpGraphics *graphics, GpPen *pen, REAL x1,
2461     REAL y1, REAL x2, REAL y2)
2462 {
2463     INT save_state;
2464     GpPointF pt[2];
2465     GpStatus retval;
2466
2467     TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x1, y1, x2, y2);
2468
2469     if(!pen || !graphics)
2470         return InvalidParameter;
2471
2472     if(graphics->busy)
2473         return ObjectBusy;
2474
2475     if (!graphics->hdc)
2476     {
2477         FIXME("graphics object has no HDC\n");
2478         return Ok;
2479     }
2480
2481     pt[0].X = x1;
2482     pt[0].Y = y1;
2483     pt[1].X = x2;
2484     pt[1].Y = y2;
2485
2486     save_state = prepare_dc(graphics, pen);
2487
2488     retval = draw_polyline(graphics, pen, pt, 2, TRUE);
2489
2490     restore_dc(graphics, save_state);
2491
2492     return retval;
2493 }
2494
2495 GpStatus WINGDIPAPI GdipDrawLineI(GpGraphics *graphics, GpPen *pen, INT x1,
2496     INT y1, INT x2, INT y2)
2497 {
2498     INT save_state;
2499     GpPointF pt[2];
2500     GpStatus retval;
2501
2502     TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, pen, x1, y1, x2, y2);
2503
2504     if(!pen || !graphics)
2505         return InvalidParameter;
2506
2507     if(graphics->busy)
2508         return ObjectBusy;
2509
2510     if (!graphics->hdc)
2511     {
2512         FIXME("graphics object has no HDC\n");
2513         return Ok;
2514     }
2515
2516     pt[0].X = (REAL)x1;
2517     pt[0].Y = (REAL)y1;
2518     pt[1].X = (REAL)x2;
2519     pt[1].Y = (REAL)y2;
2520
2521     save_state = prepare_dc(graphics, pen);
2522
2523     retval = draw_polyline(graphics, pen, pt, 2, TRUE);
2524
2525     restore_dc(graphics, save_state);
2526
2527     return retval;
2528 }
2529
2530 GpStatus WINGDIPAPI GdipDrawLines(GpGraphics *graphics, GpPen *pen, GDIPCONST
2531     GpPointF *points, INT count)
2532 {
2533     INT save_state;
2534     GpStatus retval;
2535
2536     TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2537
2538     if(!pen || !graphics || (count < 2))
2539         return InvalidParameter;
2540
2541     if(graphics->busy)
2542         return ObjectBusy;
2543
2544     if (!graphics->hdc)
2545     {
2546         FIXME("graphics object has no HDC\n");
2547         return Ok;
2548     }
2549
2550     save_state = prepare_dc(graphics, pen);
2551
2552     retval = draw_polyline(graphics, pen, points, count, TRUE);
2553
2554     restore_dc(graphics, save_state);
2555
2556     return retval;
2557 }
2558
2559 GpStatus WINGDIPAPI GdipDrawLinesI(GpGraphics *graphics, GpPen *pen, GDIPCONST
2560     GpPoint *points, INT count)
2561 {
2562     INT save_state;
2563     GpStatus retval;
2564     GpPointF *ptf = NULL;
2565     int i;
2566
2567     TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
2568
2569     if(!pen || !graphics || (count < 2))
2570         return InvalidParameter;
2571
2572     if(graphics->busy)
2573         return ObjectBusy;
2574
2575     if (!graphics->hdc)
2576     {
2577         FIXME("graphics object has no HDC\n");
2578         return Ok;
2579     }
2580
2581     ptf = GdipAlloc(count * sizeof(GpPointF));
2582     if(!ptf) return OutOfMemory;
2583
2584     for(i = 0; i < count; i ++){
2585         ptf[i].X = (REAL) points[i].X;
2586         ptf[i].Y = (REAL) points[i].Y;
2587     }
2588
2589     save_state = prepare_dc(graphics, pen);
2590
2591     retval = draw_polyline(graphics, pen, ptf, count, TRUE);
2592
2593     restore_dc(graphics, save_state);
2594
2595     GdipFree(ptf);
2596     return retval;
2597 }
2598
2599 GpStatus WINGDIPAPI GdipDrawPath(GpGraphics *graphics, GpPen *pen, GpPath *path)
2600 {
2601     INT save_state;
2602     GpStatus retval;
2603
2604     TRACE("(%p, %p, %p)\n", graphics, pen, path);
2605
2606     if(!pen || !graphics)
2607         return InvalidParameter;
2608
2609     if(graphics->busy)
2610         return ObjectBusy;
2611
2612     if (!graphics->hdc)
2613     {
2614         FIXME("graphics object has no HDC\n");
2615         return Ok;
2616     }
2617
2618     save_state = prepare_dc(graphics, pen);
2619
2620     retval = draw_poly(graphics, pen, path->pathdata.Points,
2621                        path->pathdata.Types, path->pathdata.Count, TRUE);
2622
2623     restore_dc(graphics, save_state);
2624
2625     return retval;
2626 }
2627
2628 GpStatus WINGDIPAPI GdipDrawPie(GpGraphics *graphics, GpPen *pen, REAL x,
2629     REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
2630 {
2631     INT save_state;
2632
2633     TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y,
2634             width, height, startAngle, sweepAngle);
2635
2636     if(!graphics || !pen)
2637         return InvalidParameter;
2638
2639     if(graphics->busy)
2640         return ObjectBusy;
2641
2642     if (!graphics->hdc)
2643     {
2644         FIXME("graphics object has no HDC\n");
2645         return Ok;
2646     }
2647
2648     save_state = prepare_dc(graphics, pen);
2649     SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
2650
2651     draw_pie(graphics, x, y, width, height, startAngle, sweepAngle);
2652
2653     restore_dc(graphics, save_state);
2654
2655     return Ok;
2656 }
2657
2658 GpStatus WINGDIPAPI GdipDrawPieI(GpGraphics *graphics, GpPen *pen, INT x,
2659     INT y, INT width, INT height, REAL startAngle, REAL sweepAngle)
2660 {
2661     TRACE("(%p, %p, %d, %d, %d, %d, %.2f, %.2f)\n", graphics, pen, x, y,
2662             width, height, startAngle, sweepAngle);
2663
2664     return GdipDrawPie(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height,startAngle,sweepAngle);
2665 }
2666
2667 GpStatus WINGDIPAPI GdipDrawRectangle(GpGraphics *graphics, GpPen *pen, REAL x,
2668     REAL y, REAL width, REAL height)
2669 {
2670     INT save_state;
2671     GpPointF ptf[4];
2672     POINT pti[4];
2673
2674     TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, pen, x, y, width, height);
2675
2676     if(!pen || !graphics)
2677         return InvalidParameter;
2678
2679     if(graphics->busy)
2680         return ObjectBusy;
2681
2682     if (!graphics->hdc)
2683     {
2684         FIXME("graphics object has no HDC\n");
2685         return Ok;
2686     }
2687
2688     ptf[0].X = x;
2689     ptf[0].Y = y;
2690     ptf[1].X = x + width;
2691     ptf[1].Y = y;
2692     ptf[2].X = x + width;
2693     ptf[2].Y = y + height;
2694     ptf[3].X = x;
2695     ptf[3].Y = y + height;
2696
2697     save_state = prepare_dc(graphics, pen);
2698     SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
2699
2700     transform_and_round_points(graphics, pti, ptf, 4);
2701     Polygon(graphics->hdc, pti, 4);
2702
2703     restore_dc(graphics, save_state);
2704
2705     return Ok;
2706 }
2707
2708 GpStatus WINGDIPAPI GdipDrawRectangleI(GpGraphics *graphics, GpPen *pen, INT x,
2709     INT y, INT width, INT height)
2710 {
2711     TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, pen, x, y, width, height);
2712
2713     return GdipDrawRectangle(graphics,pen,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
2714 }
2715
2716 GpStatus WINGDIPAPI GdipDrawRectangles(GpGraphics *graphics, GpPen *pen,
2717     GDIPCONST GpRectF* rects, INT count)
2718 {
2719     GpPointF *ptf;
2720     POINT *pti;
2721     INT save_state, i;
2722
2723     TRACE("(%p, %p, %p, %d)\n", graphics, pen, rects, count);
2724
2725     if(!graphics || !pen || !rects || count < 1)
2726         return InvalidParameter;
2727
2728     if(graphics->busy)
2729         return ObjectBusy;
2730
2731     if (!graphics->hdc)
2732     {
2733         FIXME("graphics object has no HDC\n");
2734         return Ok;
2735     }
2736
2737     ptf = GdipAlloc(4 * count * sizeof(GpPointF));
2738     pti = GdipAlloc(4 * count * sizeof(POINT));
2739
2740     if(!ptf || !pti){
2741         GdipFree(ptf);
2742         GdipFree(pti);
2743         return OutOfMemory;
2744     }
2745
2746     for(i = 0; i < count; i++){
2747         ptf[4 * i + 3].X = ptf[4 * i].X = rects[i].X;
2748         ptf[4 * i + 1].Y = ptf[4 * i].Y = rects[i].Y;
2749         ptf[4 * i + 2].X = ptf[4 * i + 1].X = rects[i].X + rects[i].Width;
2750         ptf[4 * i + 3].Y = ptf[4 * i + 2].Y = rects[i].Y + rects[i].Height;
2751     }
2752
2753     save_state = prepare_dc(graphics, pen);
2754     SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
2755
2756     transform_and_round_points(graphics, pti, ptf, 4 * count);
2757
2758     for(i = 0; i < count; i++)
2759         Polygon(graphics->hdc, &pti[4 * i], 4);
2760
2761     restore_dc(graphics, save_state);
2762
2763     GdipFree(ptf);
2764     GdipFree(pti);
2765
2766     return Ok;
2767 }
2768
2769 GpStatus WINGDIPAPI GdipDrawRectanglesI(GpGraphics *graphics, GpPen *pen,
2770     GDIPCONST GpRect* rects, INT count)
2771 {
2772     GpRectF *rectsF;
2773     GpStatus ret;
2774     INT i;
2775
2776     TRACE("(%p, %p, %p, %d)\n", graphics, pen, rects, count);
2777
2778     if(!rects || count<=0)
2779         return InvalidParameter;
2780
2781     rectsF = GdipAlloc(sizeof(GpRectF) * count);
2782     if(!rectsF)
2783         return OutOfMemory;
2784
2785     for(i = 0;i < count;i++){
2786         rectsF[i].X      = (REAL)rects[i].X;
2787         rectsF[i].Y      = (REAL)rects[i].Y;
2788         rectsF[i].Width  = (REAL)rects[i].Width;
2789         rectsF[i].Height = (REAL)rects[i].Height;
2790     }
2791
2792     ret = GdipDrawRectangles(graphics, pen, rectsF, count);
2793     GdipFree(rectsF);
2794
2795     return ret;
2796 }
2797
2798 GpStatus WINGDIPAPI GdipFillClosedCurve2(GpGraphics *graphics, GpBrush *brush,
2799     GDIPCONST GpPointF *points, INT count, REAL tension, GpFillMode fill)
2800 {
2801     GpPath *path;
2802     GpStatus stat;
2803
2804     TRACE("(%p, %p, %p, %d, %.2f, %d)\n", graphics, brush, points,
2805             count, tension, fill);
2806
2807     if(!graphics || !brush || !points)
2808         return InvalidParameter;
2809
2810     if(graphics->busy)
2811         return ObjectBusy;
2812
2813     if(count == 1)    /* Do nothing */
2814         return Ok;
2815
2816     stat = GdipCreatePath(fill, &path);
2817     if(stat != Ok)
2818         return stat;
2819
2820     stat = GdipAddPathClosedCurve2(path, points, count, tension);
2821     if(stat != Ok){
2822         GdipDeletePath(path);
2823         return stat;
2824     }
2825
2826     stat = GdipFillPath(graphics, brush, path);
2827     if(stat != Ok){
2828         GdipDeletePath(path);
2829         return stat;
2830     }
2831
2832     GdipDeletePath(path);
2833
2834     return Ok;
2835 }
2836
2837 GpStatus WINGDIPAPI GdipFillClosedCurve2I(GpGraphics *graphics, GpBrush *brush,
2838     GDIPCONST GpPoint *points, INT count, REAL tension, GpFillMode fill)
2839 {
2840     GpPointF *ptf;
2841     GpStatus stat;
2842     INT i;
2843
2844     TRACE("(%p, %p, %p, %d, %.2f, %d)\n", graphics, brush, points,
2845             count, tension, fill);
2846
2847     if(!points || count == 0)
2848         return InvalidParameter;
2849
2850     if(count == 1)    /* Do nothing */
2851         return Ok;
2852
2853     ptf = GdipAlloc(sizeof(GpPointF)*count);
2854     if(!ptf)
2855         return OutOfMemory;
2856
2857     for(i = 0;i < count;i++){
2858         ptf[i].X = (REAL)points[i].X;
2859         ptf[i].Y = (REAL)points[i].Y;
2860     }
2861
2862     stat = GdipFillClosedCurve2(graphics, brush, ptf, count, tension, fill);
2863
2864     GdipFree(ptf);
2865
2866     return stat;
2867 }
2868
2869 GpStatus WINGDIPAPI GdipFillClosedCurve(GpGraphics *graphics, GpBrush *brush,
2870     GDIPCONST GpPointF *points, INT count)
2871 {
2872     TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);
2873     return GdipFillClosedCurve2(graphics, brush, points, count,
2874                0.5f, FillModeAlternate);
2875 }
2876
2877 GpStatus WINGDIPAPI GdipFillClosedCurveI(GpGraphics *graphics, GpBrush *brush,
2878     GDIPCONST GpPoint *points, INT count)
2879 {
2880     TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);
2881     return GdipFillClosedCurve2I(graphics, brush, points, count,
2882                0.5f, FillModeAlternate);
2883 }
2884
2885 GpStatus WINGDIPAPI GdipFillEllipse(GpGraphics *graphics, GpBrush *brush, REAL x,
2886     REAL y, REAL width, REAL height)
2887 {
2888     INT save_state;
2889     GpPointF ptf[2];
2890     POINT pti[2];
2891
2892     TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, brush, x, y, width, height);
2893
2894     if(!graphics || !brush)
2895         return InvalidParameter;
2896
2897     if(graphics->busy)
2898         return ObjectBusy;
2899
2900     if(!graphics->hdc)
2901     {
2902         FIXME("graphics object has no HDC\n");
2903         return Ok;
2904     }
2905
2906     ptf[0].X = x;
2907     ptf[0].Y = y;
2908     ptf[1].X = x + width;
2909     ptf[1].Y = y + height;
2910
2911     save_state = SaveDC(graphics->hdc);
2912     EndPath(graphics->hdc);
2913
2914     transform_and_round_points(graphics, pti, ptf, 2);
2915
2916     BeginPath(graphics->hdc);
2917     Ellipse(graphics->hdc, pti[0].x, pti[0].y, pti[1].x, pti[1].y);
2918     EndPath(graphics->hdc);
2919
2920     brush_fill_path(graphics, brush);
2921
2922     RestoreDC(graphics->hdc, save_state);
2923
2924     return Ok;
2925 }
2926
2927 GpStatus WINGDIPAPI GdipFillEllipseI(GpGraphics *graphics, GpBrush *brush, INT x,
2928     INT y, INT width, INT height)
2929 {
2930     TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, brush, x, y, width, height);
2931
2932     return GdipFillEllipse(graphics,brush,(REAL)x,(REAL)y,(REAL)width,(REAL)height);
2933 }
2934
2935 GpStatus WINGDIPAPI GdipFillPath(GpGraphics *graphics, GpBrush *brush, GpPath *path)
2936 {
2937     INT save_state;
2938     GpStatus retval;
2939
2940     TRACE("(%p, %p, %p)\n", graphics, brush, path);
2941
2942     if(!brush || !graphics || !path)
2943         return InvalidParameter;
2944
2945     if(graphics->busy)
2946         return ObjectBusy;
2947
2948     if(!graphics->hdc)
2949     {
2950         FIXME("graphics object has no HDC\n");
2951         return Ok;
2952     }
2953
2954     save_state = SaveDC(graphics->hdc);
2955     EndPath(graphics->hdc);
2956     SetPolyFillMode(graphics->hdc, (path->fill == FillModeAlternate ? ALTERNATE
2957                                                                     : WINDING));
2958
2959     BeginPath(graphics->hdc);
2960     retval = draw_poly(graphics, NULL, path->pathdata.Points,
2961                        path->pathdata.Types, path->pathdata.Count, FALSE);
2962
2963     if(retval != Ok)
2964         goto end;
2965
2966     EndPath(graphics->hdc);
2967     brush_fill_path(graphics, brush);
2968
2969     retval = Ok;
2970
2971 end:
2972     RestoreDC(graphics->hdc, save_state);
2973
2974     return retval;
2975 }
2976
2977 GpStatus WINGDIPAPI GdipFillPie(GpGraphics *graphics, GpBrush *brush, REAL x,
2978     REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)
2979 {
2980     INT save_state;
2981
2982     TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f, %.2f, %.2f)\n",
2983             graphics, brush, x, y, width, height, startAngle, sweepAngle);
2984
2985     if(!graphics || !brush)
2986         return InvalidParameter;
2987
2988     if(graphics->busy)
2989         return ObjectBusy;
2990
2991     if(!graphics->hdc)
2992     {
2993         FIXME("graphics object has no HDC\n");
2994         return Ok;
2995     }
2996
2997     save_state = SaveDC(graphics->hdc);
2998     EndPath(graphics->hdc);
2999
3000     BeginPath(graphics->hdc);
3001     draw_pie(graphics, x, y, width, height, startAngle, sweepAngle);
3002     EndPath(graphics->hdc);
3003
3004     brush_fill_path(graphics, brush);
3005
3006     RestoreDC(graphics->hdc, save_state);
3007
3008     return Ok;
3009 }
3010
3011 GpStatus WINGDIPAPI GdipFillPieI(GpGraphics *graphics, GpBrush *brush, INT x,
3012     INT y, INT width, INT height, REAL startAngle, REAL sweepAngle)
3013 {
3014     TRACE("(%p, %p, %d, %d, %d, %d, %.2f, %.2f)\n",
3015             graphics, brush, x, y, width, height, startAngle, sweepAngle);
3016
3017     return GdipFillPie(graphics,brush,(REAL)x,(REAL)y,(REAL)width,(REAL)height,startAngle,sweepAngle);
3018 }
3019
3020 GpStatus WINGDIPAPI GdipFillPolygon(GpGraphics *graphics, GpBrush *brush,
3021     GDIPCONST GpPointF *points, INT count, GpFillMode fillMode)
3022 {
3023     INT save_state;
3024     GpPointF *ptf = NULL;
3025     POINT *pti = NULL;
3026     GpStatus retval = Ok;
3027
3028     TRACE("(%p, %p, %p, %d, %d)\n", graphics, brush, points, count, fillMode);
3029
3030     if(!graphics || !brush || !points || !count)
3031         return InvalidParameter;
3032
3033     if(graphics->busy)
3034         return ObjectBusy;
3035
3036     if(!graphics->hdc)
3037     {
3038         FIXME("graphics object has no HDC\n");
3039         return Ok;
3040     }
3041
3042     ptf = GdipAlloc(count * sizeof(GpPointF));
3043     pti = GdipAlloc(count * sizeof(POINT));
3044     if(!ptf || !pti){
3045         retval = OutOfMemory;
3046         goto end;
3047     }
3048
3049     memcpy(ptf, points, count * sizeof(GpPointF));
3050
3051     save_state = SaveDC(graphics->hdc);
3052     EndPath(graphics->hdc);
3053     SetPolyFillMode(graphics->hdc, (fillMode == FillModeAlternate ? ALTERNATE
3054                                                                   : WINDING));
3055
3056     transform_and_round_points(graphics, pti, ptf, count);
3057
3058     BeginPath(graphics->hdc);
3059     Polygon(graphics->hdc, pti, count);
3060     EndPath(graphics->hdc);
3061
3062     brush_fill_path(graphics, brush);
3063
3064     RestoreDC(graphics->hdc, save_state);
3065
3066 end:
3067     GdipFree(ptf);
3068     GdipFree(pti);
3069
3070     return retval;
3071 }
3072
3073 GpStatus WINGDIPAPI GdipFillPolygonI(GpGraphics *graphics, GpBrush *brush,
3074     GDIPCONST GpPoint *points, INT count, GpFillMode fillMode)
3075 {
3076     INT save_state, i;
3077     GpPointF *ptf = NULL;
3078     POINT *pti = NULL;
3079     GpStatus retval = Ok;
3080
3081     TRACE("(%p, %p, %p, %d, %d)\n", graphics, brush, points, count, fillMode);
3082
3083     if(!graphics || !brush || !points || !count)
3084         return InvalidParameter;
3085
3086     if(graphics->busy)
3087         return ObjectBusy;
3088
3089     if(!graphics->hdc)
3090     {
3091         FIXME("graphics object has no HDC\n");
3092         return Ok;
3093     }
3094
3095     ptf = GdipAlloc(count * sizeof(GpPointF));
3096     pti = GdipAlloc(count * sizeof(POINT));
3097     if(!ptf || !pti){
3098         retval = OutOfMemory;
3099         goto end;
3100     }
3101
3102     for(i = 0; i < count; i ++){
3103         ptf[i].X = (REAL) points[i].X;
3104         ptf[i].Y = (REAL) points[i].Y;
3105     }
3106
3107     save_state = SaveDC(graphics->hdc);
3108     EndPath(graphics->hdc);
3109     SetPolyFillMode(graphics->hdc, (fillMode == FillModeAlternate ? ALTERNATE
3110                                                                   : WINDING));
3111
3112     transform_and_round_points(graphics, pti, ptf, count);
3113
3114     BeginPath(graphics->hdc);
3115     Polygon(graphics->hdc, pti, count);
3116     EndPath(graphics->hdc);
3117
3118     brush_fill_path(graphics, brush);
3119
3120     RestoreDC(graphics->hdc, save_state);
3121
3122 end:
3123     GdipFree(ptf);
3124     GdipFree(pti);
3125
3126     return retval;
3127 }
3128
3129 GpStatus WINGDIPAPI GdipFillPolygon2(GpGraphics *graphics, GpBrush *brush,
3130     GDIPCONST GpPointF *points, INT count)
3131 {
3132     TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);
3133
3134     return GdipFillPolygon(graphics, brush, points, count, FillModeAlternate);
3135 }
3136
3137 GpStatus WINGDIPAPI GdipFillPolygon2I(GpGraphics *graphics, GpBrush *brush,
3138     GDIPCONST GpPoint *points, INT count)
3139 {
3140     TRACE("(%p, %p, %p, %d)\n", graphics, brush, points, count);
3141
3142     return GdipFillPolygonI(graphics, brush, points, count, FillModeAlternate);
3143 }
3144
3145 GpStatus WINGDIPAPI GdipFillRectangle(GpGraphics *graphics, GpBrush *brush,
3146     REAL x, REAL y, REAL width, REAL height)
3147 {
3148     INT save_state;
3149     GpPointF ptf[4];
3150     POINT pti[4];
3151
3152     TRACE("(%p, %p, %.2f, %.2f, %.2f, %.2f)\n", graphics, brush, x, y, width, height);
3153
3154     if(!graphics || !brush)
3155         return InvalidParameter;
3156
3157     if(graphics->busy)
3158         return ObjectBusy;
3159
3160     if(!graphics->hdc)
3161     {
3162         FIXME("graphics object has no HDC\n");
3163         return Ok;
3164     }
3165
3166     ptf[0].X = x;
3167     ptf[0].Y = y;
3168     ptf[1].X = x + width;
3169     ptf[1].Y = y;
3170     ptf[2].X = x + width;
3171     ptf[2].Y = y + height;
3172     ptf[3].X = x;
3173     ptf[3].Y = y + height;
3174
3175     save_state = SaveDC(graphics->hdc);
3176     EndPath(graphics->hdc);
3177
3178     transform_and_round_points(graphics, pti, ptf, 4);
3179
3180     BeginPath(graphics->hdc);
3181     Polygon(graphics->hdc, pti, 4);
3182     EndPath(graphics->hdc);
3183
3184     brush_fill_path(graphics, brush);
3185
3186     RestoreDC(graphics->hdc, save_state);
3187
3188     return Ok;
3189 }
3190
3191 GpStatus WINGDIPAPI GdipFillRectangleI(GpGraphics *graphics, GpBrush *brush,
3192     INT x, INT y, INT width, INT height)
3193 {
3194     INT save_state;
3195     GpPointF ptf[4];
3196     POINT pti[4];
3197
3198     TRACE("(%p, %p, %d, %d, %d, %d)\n", graphics, brush, x, y, width, height);
3199
3200     if(!graphics || !brush)
3201         return InvalidParameter;
3202
3203     if(graphics->busy)
3204         return ObjectBusy;
3205
3206     if(!graphics->hdc)
3207     {
3208         FIXME("graphics object has no HDC\n");
3209         return Ok;
3210     }
3211
3212     ptf[0].X = x;
3213     ptf[0].Y = y;
3214     ptf[1].X = x + width;
3215     ptf[1].Y = y;
3216     ptf[2].X = x + width;
3217     ptf[2].Y = y + height;
3218     ptf[3].X = x;
3219     ptf[3].Y = y + height;
3220
3221     save_state = SaveDC(graphics->hdc);
3222     EndPath(graphics->hdc);
3223
3224     transform_and_round_points(graphics, pti, ptf, 4);
3225
3226     BeginPath(graphics->hdc);
3227     Polygon(graphics->hdc, pti, 4);
3228     EndPath(graphics->hdc);
3229
3230     brush_fill_path(graphics, brush);
3231
3232     RestoreDC(graphics->hdc, save_state);
3233
3234     return Ok;
3235 }
3236
3237 GpStatus WINGDIPAPI GdipFillRectangles(GpGraphics *graphics, GpBrush *brush, GDIPCONST GpRectF *rects,
3238     INT count)
3239 {
3240     GpStatus ret;
3241     INT i;
3242
3243     TRACE("(%p, %p, %p, %d)\n", graphics, brush, rects, count);
3244
3245     if(!rects)
3246         return InvalidParameter;
3247
3248     for(i = 0; i < count; i++){
3249         ret = GdipFillRectangle(graphics, brush, rects[i].X, rects[i].Y, rects[i].Width, rects[i].Height);
3250         if(ret != Ok)   return ret;
3251     }
3252
3253     return Ok;
3254 }
3255
3256 GpStatus WINGDIPAPI GdipFillRectanglesI(GpGraphics *graphics, GpBrush *brush, GDIPCONST GpRect *rects,
3257     INT count)
3258 {
3259     GpRectF *rectsF;
3260     GpStatus ret;
3261     INT i;
3262
3263     TRACE("(%p, %p, %p, %d)\n", graphics, brush, rects, count);
3264
3265     if(!rects || count <= 0)
3266         return InvalidParameter;
3267
3268     rectsF = GdipAlloc(sizeof(GpRectF)*count);
3269     if(!rectsF)
3270         return OutOfMemory;
3271
3272     for(i = 0; i < count; i++){
3273         rectsF[i].X      = (REAL)rects[i].X;
3274         rectsF[i].Y      = (REAL)rects[i].Y;
3275         rectsF[i].X      = (REAL)rects[i].Width;
3276         rectsF[i].Height = (REAL)rects[i].Height;
3277     }
3278
3279     ret = GdipFillRectangles(graphics,brush,rectsF,count);
3280     GdipFree(rectsF);
3281
3282     return ret;
3283 }
3284
3285 /*****************************************************************************
3286  * GdipFillRegion [GDIPLUS.@]
3287  */
3288 GpStatus WINGDIPAPI GdipFillRegion(GpGraphics* graphics, GpBrush* brush,
3289         GpRegion* region)
3290 {
3291     INT save_state;
3292     GpStatus status;
3293     HRGN hrgn;
3294     RECT rc;
3295
3296     TRACE("(%p, %p, %p)\n", graphics, brush, region);
3297
3298     if (!(graphics && brush && region))
3299         return InvalidParameter;
3300
3301     if(graphics->busy)
3302         return ObjectBusy;
3303
3304     if(!graphics->hdc)
3305     {
3306         FIXME("graphics object has no HDC\n");
3307         return Ok;
3308     }
3309
3310     status = GdipGetRegionHRgn(region, graphics, &hrgn);
3311     if(status != Ok)
3312         return status;
3313
3314     save_state = SaveDC(graphics->hdc);
3315     EndPath(graphics->hdc);
3316
3317     ExtSelectClipRgn(graphics->hdc, hrgn, RGN_AND);
3318
3319     if (GetClipBox(graphics->hdc, &rc) != NULLREGION)
3320     {
3321         BeginPath(graphics->hdc);
3322         Rectangle(graphics->hdc, rc.left, rc.top, rc.right, rc.bottom);
3323         EndPath(graphics->hdc);
3324
3325         brush_fill_path(graphics, brush);
3326     }
3327
3328     RestoreDC(graphics->hdc, save_state);
3329
3330     DeleteObject(hrgn);
3331
3332     return Ok;
3333 }
3334
3335 GpStatus WINGDIPAPI GdipFlush(GpGraphics *graphics, GpFlushIntention intention)
3336 {
3337     TRACE("(%p,%u)\n", graphics, intention);
3338
3339     if(!graphics)
3340         return InvalidParameter;
3341
3342     if(graphics->busy)
3343         return ObjectBusy;
3344
3345     /* We have no internal operation queue, so there's no need to clear it. */
3346
3347     if (graphics->hdc)
3348         GdiFlush();
3349
3350     return Ok;
3351 }
3352
3353 /*****************************************************************************
3354  * GdipGetClipBounds [GDIPLUS.@]
3355  */
3356 GpStatus WINGDIPAPI GdipGetClipBounds(GpGraphics *graphics, GpRectF *rect)
3357 {
3358     TRACE("(%p, %p)\n", graphics, rect);
3359
3360     if(!graphics)
3361         return InvalidParameter;
3362
3363     if(graphics->busy)
3364         return ObjectBusy;
3365
3366     return GdipGetRegionBounds(graphics->clip, graphics, rect);
3367 }
3368
3369 /*****************************************************************************
3370  * GdipGetClipBoundsI [GDIPLUS.@]
3371  */
3372 GpStatus WINGDIPAPI GdipGetClipBoundsI(GpGraphics *graphics, GpRect *rect)
3373 {
3374     TRACE("(%p, %p)\n", graphics, rect);
3375
3376     if(!graphics)
3377         return InvalidParameter;
3378
3379     if(graphics->busy)
3380         return ObjectBusy;
3381
3382     return GdipGetRegionBoundsI(graphics->clip, graphics, rect);
3383 }
3384
3385 /* FIXME: Compositing mode is not used anywhere except the getter/setter. */
3386 GpStatus WINGDIPAPI GdipGetCompositingMode(GpGraphics *graphics,
3387     CompositingMode *mode)
3388 {
3389     TRACE("(%p, %p)\n", graphics, mode);
3390
3391     if(!graphics || !mode)
3392         return InvalidParameter;
3393
3394     if(graphics->busy)
3395         return ObjectBusy;
3396
3397     *mode = graphics->compmode;
3398
3399     return Ok;
3400 }
3401
3402 /* FIXME: Compositing quality is not used anywhere except the getter/setter. */
3403 GpStatus WINGDIPAPI GdipGetCompositingQuality(GpGraphics *graphics,
3404     CompositingQuality *quality)
3405 {
3406     TRACE("(%p, %p)\n", graphics, quality);
3407
3408     if(!graphics || !quality)
3409         return InvalidParameter;
3410
3411     if(graphics->busy)
3412         return ObjectBusy;
3413
3414     *quality = graphics->compqual;
3415
3416     return Ok;
3417 }
3418
3419 /* FIXME: Interpolation mode is not used anywhere except the getter/setter. */
3420 GpStatus WINGDIPAPI GdipGetInterpolationMode(GpGraphics *graphics,
3421     InterpolationMode *mode)
3422 {
3423     TRACE("(%p, %p)\n", graphics, mode);
3424
3425     if(!graphics || !mode)
3426         return InvalidParameter;
3427
3428     if(graphics->busy)
3429         return ObjectBusy;
3430
3431     *mode = graphics->interpolation;
3432
3433     return Ok;
3434 }
3435
3436 /* FIXME: Need to handle color depths less than 24bpp */
3437 GpStatus WINGDIPAPI GdipGetNearestColor(GpGraphics *graphics, ARGB* argb)
3438 {
3439     FIXME("(%p, %p): Passing color unmodified\n", graphics, argb);
3440
3441     if(!graphics || !argb)
3442         return InvalidParameter;
3443
3444     if(graphics->busy)
3445         return ObjectBusy;
3446
3447     return Ok;
3448 }
3449
3450 GpStatus WINGDIPAPI GdipGetPageScale(GpGraphics *graphics, REAL *scale)
3451 {
3452     TRACE("(%p, %p)\n", graphics, scale);
3453
3454     if(!graphics || !scale)
3455         return InvalidParameter;
3456
3457     if(graphics->busy)
3458         return ObjectBusy;
3459
3460     *scale = graphics->scale;
3461
3462     return Ok;
3463 }
3464
3465 GpStatus WINGDIPAPI GdipGetPageUnit(GpGraphics *graphics, GpUnit *unit)
3466 {
3467     TRACE("(%p, %p)\n", graphics, unit);
3468
3469     if(!graphics || !unit)
3470         return InvalidParameter;
3471
3472     if(graphics->busy)
3473         return ObjectBusy;
3474
3475     *unit = graphics->unit;
3476
3477     return Ok;
3478 }
3479
3480 /* FIXME: Pixel offset mode is not used anywhere except the getter/setter. */
3481 GpStatus WINGDIPAPI GdipGetPixelOffsetMode(GpGraphics *graphics, PixelOffsetMode
3482     *mode)
3483 {
3484     TRACE("(%p, %p)\n", graphics, mode);
3485
3486     if(!graphics || !mode)
3487         return InvalidParameter;
3488
3489     if(graphics->busy)
3490         return ObjectBusy;
3491
3492     *mode = graphics->pixeloffset;
3493
3494     return Ok;
3495 }
3496
3497 /* FIXME: Smoothing mode is not used anywhere except the getter/setter. */
3498 GpStatus WINGDIPAPI GdipGetSmoothingMode(GpGraphics *graphics, SmoothingMode *mode)
3499 {
3500     TRACE("(%p, %p)\n", graphics, mode);
3501
3502     if(!graphics || !mode)
3503         return InvalidParameter;
3504
3505     if(graphics->busy)
3506         return ObjectBusy;
3507
3508     *mode = graphics->smoothing;
3509
3510     return Ok;
3511 }
3512
3513 GpStatus WINGDIPAPI GdipGetTextContrast(GpGraphics *graphics, UINT *contrast)
3514 {
3515     TRACE("(%p, %p)\n", graphics, contrast);
3516
3517     if(!graphics || !contrast)
3518         return InvalidParameter;
3519
3520     *contrast = graphics->textcontrast;
3521
3522     return Ok;
3523 }
3524
3525 /* FIXME: Text rendering hint is not used anywhere except the getter/setter. */
3526 GpStatus WINGDIPAPI GdipGetTextRenderingHint(GpGraphics *graphics,
3527     TextRenderingHint *hint)
3528 {
3529     TRACE("(%p, %p)\n", graphics, hint);
3530
3531     if(!graphics || !hint)
3532         return InvalidParameter;
3533
3534     if(graphics->busy)
3535         return ObjectBusy;
3536
3537     *hint = graphics->texthint;
3538
3539     return Ok;
3540 }
3541
3542 GpStatus WINGDIPAPI GdipGetVisibleClipBounds(GpGraphics *graphics, GpRectF *rect)
3543 {
3544     GpRegion *clip_rgn;
3545     GpStatus stat;
3546
3547     TRACE("(%p, %p)\n", graphics, rect);
3548
3549     if(!graphics || !rect)
3550         return InvalidParameter;
3551
3552     if(graphics->busy)
3553         return ObjectBusy;
3554
3555     /* intersect window and graphics clipping regions */
3556     if((stat = GdipCreateRegion(&clip_rgn)) != Ok)
3557         return stat;
3558
3559     if((stat = get_visible_clip_region(graphics, clip_rgn)) != Ok)
3560         goto cleanup;
3561
3562     /* get bounds of the region */
3563     stat = GdipGetRegionBounds(clip_rgn, graphics, rect);
3564
3565 cleanup:
3566     GdipDeleteRegion(clip_rgn);
3567
3568     return stat;
3569 }
3570
3571 GpStatus WINGDIPAPI GdipGetVisibleClipBoundsI(GpGraphics *graphics, GpRect *rect)
3572 {
3573     GpRectF rectf;
3574     GpStatus stat;
3575
3576     TRACE("(%p, %p)\n", graphics, rect);
3577
3578     if(!graphics || !rect)
3579         return InvalidParameter;
3580
3581     if((stat = GdipGetVisibleClipBounds(graphics, &rectf)) == Ok)
3582     {
3583         rect->X = roundr(rectf.X);
3584         rect->Y = roundr(rectf.Y);
3585         rect->Width  = roundr(rectf.Width);
3586         rect->Height = roundr(rectf.Height);
3587     }
3588
3589     return stat;
3590 }
3591
3592 GpStatus WINGDIPAPI GdipGetWorldTransform(GpGraphics *graphics, GpMatrix *matrix)
3593 {
3594     TRACE("(%p, %p)\n", graphics, matrix);
3595
3596     if(!graphics || !matrix)
3597         return InvalidParameter;
3598
3599     if(graphics->busy)
3600         return ObjectBusy;
3601
3602     *matrix = *graphics->worldtrans;
3603     return Ok;
3604 }
3605
3606 GpStatus WINGDIPAPI GdipGraphicsClear(GpGraphics *graphics, ARGB color)
3607 {
3608     GpSolidFill *brush;
3609     GpStatus stat;
3610     GpRectF wnd_rect;
3611
3612     TRACE("(%p, %x)\n", graphics, color);
3613
3614     if(!graphics)
3615         return InvalidParameter;
3616
3617     if(graphics->busy)
3618         return ObjectBusy;
3619
3620     if((stat = GdipCreateSolidFill(color, &brush)) != Ok)
3621         return stat;
3622
3623     if((stat = get_graphics_bounds(graphics, &wnd_rect)) != Ok){
3624         GdipDeleteBrush((GpBrush*)brush);
3625         return stat;
3626     }
3627
3628     GdipFillRectangle(graphics, (GpBrush*)brush, wnd_rect.X, wnd_rect.Y,
3629                                                  wnd_rect.Width, wnd_rect.Height);
3630
3631     GdipDeleteBrush((GpBrush*)brush);
3632
3633     return Ok;
3634 }
3635
3636 GpStatus WINGDIPAPI GdipIsClipEmpty(GpGraphics *graphics, BOOL *res)
3637 {
3638     TRACE("(%p, %p)\n", graphics, res);
3639
3640     if(!graphics || !res)
3641         return InvalidParameter;
3642
3643     return GdipIsEmptyRegion(graphics->clip, graphics, res);
3644 }
3645
3646 GpStatus WINGDIPAPI GdipIsVisiblePoint(GpGraphics *graphics, REAL x, REAL y, BOOL *result)
3647 {
3648     GpStatus stat;
3649     GpRegion* rgn;
3650     GpPointF pt;
3651
3652     TRACE("(%p, %.2f, %.2f, %p)\n", graphics, x, y, result);
3653
3654     if(!graphics || !result)
3655         return InvalidParameter;
3656
3657     if(graphics->busy)
3658         return ObjectBusy;
3659
3660     pt.X = x;
3661     pt.Y = y;
3662     if((stat = GdipTransformPoints(graphics, CoordinateSpaceDevice,
3663                    CoordinateSpaceWorld, &pt, 1)) != Ok)
3664         return stat;
3665
3666     if((stat = GdipCreateRegion(&rgn)) != Ok)
3667         return stat;
3668
3669     if((stat = get_visible_clip_region(graphics, rgn)) != Ok)
3670         goto cleanup;
3671
3672     stat = GdipIsVisibleRegionPoint(rgn, pt.X, pt.Y, graphics, result);
3673
3674 cleanup:
3675     GdipDeleteRegion(rgn);
3676     return stat;
3677 }
3678
3679 GpStatus WINGDIPAPI GdipIsVisiblePointI(GpGraphics *graphics, INT x, INT y, BOOL *result)
3680 {
3681     return GdipIsVisiblePoint(graphics, (REAL)x, (REAL)y, result);
3682 }
3683
3684 GpStatus WINGDIPAPI GdipIsVisibleRect(GpGraphics *graphics, REAL x, REAL y, REAL width, REAL height, BOOL *result)
3685 {
3686     GpStatus stat;
3687     GpRegion* rgn;
3688     GpPointF pts[2];
3689
3690     TRACE("(%p %.2f %.2f %.2f %.2f %p)\n", graphics, x, y, width, height, result);
3691
3692     if(!graphics || !result)
3693         return InvalidParameter;
3694
3695     if(graphics->busy)
3696         return ObjectBusy;
3697
3698     pts[0].X = x;
3699     pts[0].Y = y;
3700     pts[1].X = x + width;
3701     pts[1].Y = y + height;
3702
3703     if((stat = GdipTransformPoints(graphics, CoordinateSpaceDevice,
3704                     CoordinateSpaceWorld, pts, 2)) != Ok)
3705         return stat;
3706
3707     pts[1].X -= pts[0].X;
3708     pts[1].Y -= pts[0].Y;
3709
3710     if((stat = GdipCreateRegion(&rgn)) != Ok)
3711         return stat;
3712
3713     if((stat = get_visible_clip_region(graphics, rgn)) != Ok)
3714         goto cleanup;
3715
3716     stat = GdipIsVisibleRegionRect(rgn, pts[0].X, pts[0].Y, pts[1].X, pts[1].Y, graphics, result);
3717
3718 cleanup:
3719     GdipDeleteRegion(rgn);
3720     return stat;
3721 }
3722
3723 GpStatus WINGDIPAPI GdipIsVisibleRectI(GpGraphics *graphics, INT x, INT y, INT width, INT height, BOOL *result)
3724 {
3725     return GdipIsVisibleRect(graphics, (REAL)x, (REAL)y, (REAL)width, (REAL)height, result);
3726 }
3727
3728 typedef GpStatus (*gdip_format_string_callback)(GpGraphics *graphics,
3729     GDIPCONST WCHAR *string, INT index, INT length, GDIPCONST GpFont *font,
3730     GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format,
3731     INT lineno, const RectF *bounds, void *user_data);
3732
3733 static GpStatus gdip_format_string(GpGraphics *graphics,
3734     GDIPCONST WCHAR *string, INT length, GDIPCONST GpFont *font,
3735     GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format,
3736     gdip_format_string_callback callback, void *user_data)
3737 {
3738     WCHAR* stringdup;
3739     int sum = 0, height = 0, fit, fitcpy, i, j, lret, nwidth,
3740         nheight, lineend, lineno = 0;
3741     RectF bounds;
3742     StringAlignment halign;
3743     GpStatus stat = Ok;
3744     SIZE size;
3745
3746     if(length == -1) length = lstrlenW(string);
3747
3748     stringdup = GdipAlloc((length + 1) * sizeof(WCHAR));
3749     if(!stringdup) return OutOfMemory;
3750
3751     nwidth = roundr(rect->Width);
3752     nheight = roundr(rect->Height);
3753
3754     if (rect->Width >= INT_MAX || rect->Width < 0.5) nwidth = INT_MAX;
3755     if (rect->Height >= INT_MAX || rect->Width < 0.5) nheight = INT_MAX;
3756
3757     for(i = 0, j = 0; i < length; i++){
3758         /* FIXME: This makes the indexes passed to callback inaccurate. */
3759         if(!isprintW(string[i]) && (string[i] != '\n'))
3760             continue;
3761
3762         stringdup[j] = string[i];
3763         j++;
3764     }
3765
3766     length = j;
3767
3768     if (format) halign = format->align;
3769     else halign = StringAlignmentNear;
3770
3771     while(sum < length){
3772         GetTextExtentExPointW(graphics->hdc, stringdup + sum, length - sum,
3773                               nwidth, &fit, NULL, &size);
3774         fitcpy = fit;
3775
3776         if(fit == 0)
3777             break;
3778
3779         for(lret = 0; lret < fit; lret++)
3780             if(*(stringdup + sum + lret) == '\n')
3781                 break;
3782
3783         /* Line break code (may look strange, but it imitates windows). */
3784         if(lret < fit)
3785             lineend = fit = lret;    /* this is not an off-by-one error */
3786         else if(fit < (length - sum)){
3787             if(*(stringdup + sum + fit) == ' ')
3788                 while(*(stringdup + sum + fit) == ' ')
3789                     fit++;
3790             else
3791                 while(*(stringdup + sum + fit - 1) != ' '){
3792                     fit--;
3793
3794                     if(*(stringdup + sum + fit) == '\t')
3795                         break;
3796
3797                     if(fit == 0){
3798                         fit = fitcpy;
3799                         break;
3800                     }
3801                 }
3802             lineend = fit;
3803             while(*(stringdup + sum + lineend - 1) == ' ' ||
3804                   *(stringdup + sum + lineend - 1) == '\t')
3805                 lineend--;
3806         }
3807         else
3808             lineend = fit;
3809
3810         GetTextExtentExPointW(graphics->hdc, stringdup + sum, lineend,
3811                               nwidth, &j, NULL, &size);
3812
3813         bounds.Width = size.cx;
3814
3815         if(height + size.cy > nheight)
3816             bounds.Height = nheight - (height + size.cy);
3817         else
3818             bounds.Height = size.cy;
3819
3820         bounds.Y = rect->Y + height;
3821
3822         switch (halign)
3823         {
3824         case StringAlignmentNear:
3825         default:
3826             bounds.X = rect->X;
3827             break;
3828         case StringAlignmentCenter:
3829             bounds.X = rect->X + (rect->Width/2) - (bounds.Width/2);
3830             break;
3831         case StringAlignmentFar:
3832             bounds.X = rect->X + rect->Width - bounds.Width;
3833             break;
3834         }
3835
3836         stat = callback(graphics, stringdup, sum, lineend,
3837             font, rect, format, lineno, &bounds, user_data);
3838
3839         if (stat != Ok)
3840             break;
3841
3842         sum += fit + (lret < fitcpy ? 1 : 0);
3843         height += size.cy;
3844         lineno++;
3845
3846         if(height > nheight)
3847             break;
3848
3849         /* Stop if this was a linewrap (but not if it was a linebreak). */
3850         if((lret == fitcpy) && format && (format->attr & StringFormatFlagsNoWrap))
3851             break;
3852     }
3853
3854     GdipFree(stringdup);
3855
3856     return stat;
3857 }
3858
3859 struct measure_ranges_args {
3860     GpRegion **regions;
3861 };
3862
3863 static GpStatus measure_ranges_callback(GpGraphics *graphics,
3864     GDIPCONST WCHAR *string, INT index, INT length, GDIPCONST GpFont *font,
3865     GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format,
3866     INT lineno, const RectF *bounds, void *user_data)
3867 {
3868     int i;
3869     GpStatus stat = Ok;
3870     struct measure_ranges_args *args = user_data;
3871
3872     for (i=0; i<format->range_count; i++)
3873     {
3874         INT range_start = max(index, format->character_ranges[i].First);
3875         INT range_end = min(index+length, format->character_ranges[i].First+format->character_ranges[i].Length);
3876         if (range_start < range_end)
3877         {
3878             GpRectF range_rect;
3879             SIZE range_size;
3880
3881             range_rect.Y = bounds->Y;
3882             range_rect.Height = bounds->Height;
3883
3884             GetTextExtentExPointW(graphics->hdc, string + index, range_start - index,
3885                                   INT_MAX, NULL, NULL, &range_size);
3886             range_rect.X = bounds->X + range_size.cx;
3887
3888             GetTextExtentExPointW(graphics->hdc, string + index, range_end - index,
3889                                   INT_MAX, NULL, NULL, &range_size);
3890             range_rect.Width = (bounds->X + range_size.cx) - range_rect.X;
3891
3892             stat = GdipCombineRegionRect(args->regions[i], &range_rect, CombineModeUnion);
3893             if (stat != Ok)
3894                 break;
3895         }
3896     }
3897
3898     return stat;
3899 }
3900
3901 GpStatus WINGDIPAPI GdipMeasureCharacterRanges(GpGraphics* graphics,
3902         GDIPCONST WCHAR* string, INT length, GDIPCONST GpFont* font,
3903         GDIPCONST RectF* layoutRect, GDIPCONST GpStringFormat *stringFormat,
3904         INT regionCount, GpRegion** regions)
3905 {
3906     GpStatus stat;
3907     int i;
3908     HFONT oldfont;
3909     struct measure_ranges_args args;
3910     HDC temp_hdc=NULL;
3911
3912     TRACE("(%p %s %d %p %s %p %d %p)\n", graphics, debugstr_w(string),
3913             length, font, debugstr_rectf(layoutRect), stringFormat, regionCount, regions);
3914
3915     if (!(graphics && string && font && layoutRect && stringFormat && regions))
3916         return InvalidParameter;
3917
3918     if (regionCount < stringFormat->range_count)
3919         return InvalidParameter;
3920
3921     if(!graphics->hdc)
3922     {
3923         temp_hdc = graphics->hdc = CreateCompatibleDC(0);
3924         if (!temp_hdc) return OutOfMemory;
3925     }
3926
3927     if (stringFormat->attr)
3928         TRACE("may be ignoring some format flags: attr %x\n", stringFormat->attr);
3929
3930     oldfont = SelectObject(graphics->hdc, CreateFontIndirectW(&font->lfw));
3931
3932     for (i=0; i<stringFormat->range_count; i++)
3933     {
3934         stat = GdipSetEmpty(regions[i]);
3935         if (stat != Ok)
3936             return stat;
3937     }
3938
3939     args.regions = regions;
3940
3941     stat = gdip_format_string(graphics, string, length, font, layoutRect, stringFormat,
3942         measure_ranges_callback, &args);
3943
3944     DeleteObject(SelectObject(graphics->hdc, oldfont));
3945
3946     if (temp_hdc)
3947     {
3948         graphics->hdc = NULL;
3949         DeleteDC(temp_hdc);
3950     }
3951
3952     return stat;
3953 }
3954
3955 struct measure_string_args {
3956     RectF *bounds;
3957     INT *codepointsfitted;
3958     INT *linesfilled;
3959 };
3960
3961 static GpStatus measure_string_callback(GpGraphics *graphics,
3962     GDIPCONST WCHAR *string, INT index, INT length, GDIPCONST GpFont *font,
3963     GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format,
3964     INT lineno, const RectF *bounds, void *user_data)
3965 {
3966     struct measure_string_args *args = user_data;
3967
3968     if (bounds->Width > args->bounds->Width)
3969         args->bounds->Width = bounds->Width;
3970
3971     if (bounds->Height + bounds->Y > args->bounds->Height + args->bounds->Y)
3972         args->bounds->Height = bounds->Height + bounds->Y - args->bounds->Y;
3973
3974     if (args->codepointsfitted)
3975         *args->codepointsfitted = index + length;
3976
3977     if (args->linesfilled)
3978         (*args->linesfilled)++;
3979
3980     return Ok;
3981 }
3982
3983 /* Find the smallest rectangle that bounds the text when it is printed in rect
3984  * according to the format options listed in format. If rect has 0 width and
3985  * height, then just find the smallest rectangle that bounds the text when it's
3986  * printed at location (rect->X, rect-Y). */
3987 GpStatus WINGDIPAPI GdipMeasureString(GpGraphics *graphics,
3988     GDIPCONST WCHAR *string, INT length, GDIPCONST GpFont *font,
3989     GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format, RectF *bounds,
3990     INT *codepointsfitted, INT *linesfilled)
3991 {
3992     HFONT oldfont;
3993     struct measure_string_args args;
3994     HDC temp_hdc=NULL;
3995
3996     TRACE("(%p, %s, %i, %p, %s, %p, %p, %p, %p)\n", graphics,
3997         debugstr_wn(string, length), length, font, debugstr_rectf(rect), format,
3998         bounds, codepointsfitted, linesfilled);
3999
4000     if(!graphics || !string || !font || !rect || !bounds)
4001         return InvalidParameter;
4002
4003     if(!graphics->hdc)
4004     {
4005         temp_hdc = graphics->hdc = CreateCompatibleDC(0);
4006         if (!temp_hdc) return OutOfMemory;
4007     }
4008
4009     if(linesfilled) *linesfilled = 0;
4010     if(codepointsfitted) *codepointsfitted = 0;
4011
4012     if(format)
4013         TRACE("may be ignoring some format flags: attr %x\n", format->attr);
4014
4015     oldfont = SelectObject(graphics->hdc, CreateFontIndirectW(&font->lfw));
4016
4017     bounds->X = rect->X;
4018     bounds->Y = rect->Y;
4019     bounds->Width = 0.0;
4020     bounds->Height = 0.0;
4021
4022     args.bounds = bounds;
4023     args.codepointsfitted = codepointsfitted;
4024     args.linesfilled = linesfilled;
4025
4026     gdip_format_string(graphics, string, length, font, rect, format,
4027         measure_string_callback, &args);
4028
4029     DeleteObject(SelectObject(graphics->hdc, oldfont));
4030
4031     if (temp_hdc)
4032     {
4033         graphics->hdc = NULL;
4034         DeleteDC(temp_hdc);
4035     }
4036
4037     return Ok;
4038 }
4039
4040 struct draw_string_args {
4041     POINT drawbase;
4042     UINT drawflags;
4043     REAL ang_cos, ang_sin;
4044 };
4045
4046 static GpStatus draw_string_callback(GpGraphics *graphics,
4047     GDIPCONST WCHAR *string, INT index, INT length, GDIPCONST GpFont *font,
4048     GDIPCONST RectF *rect, GDIPCONST GpStringFormat *format,
4049     INT lineno, const RectF *bounds, void *user_data)
4050 {
4051     struct draw_string_args *args = user_data;
4052     RECT drawcoord;
4053
4054     drawcoord.left = drawcoord.right = args->drawbase.x + roundr(args->ang_sin * bounds->Y);
4055     drawcoord.top = drawcoord.bottom = args->drawbase.y + roundr(args->ang_cos * bounds->Y);
4056
4057     DrawTextW(graphics->hdc, string + index, length, &drawcoord, args->drawflags);
4058
4059     return Ok;
4060 }
4061
4062 GpStatus WINGDIPAPI GdipDrawString(GpGraphics *graphics, GDIPCONST WCHAR *string,
4063     INT length, GDIPCONST GpFont *font, GDIPCONST RectF *rect,
4064     GDIPCONST GpStringFormat *format, GDIPCONST GpBrush *brush)
4065 {
4066     HRGN rgn = NULL;
4067     HFONT gdifont;
4068     LOGFONTW lfw;
4069     TEXTMETRICW textmet;
4070     GpPointF pt[3], rectcpy[4];
4071     POINT corners[4];
4072     REAL angle, rel_width, rel_height;
4073     INT offsety = 0, save_state;
4074     struct draw_string_args args;
4075     RectF scaled_rect;
4076
4077     TRACE("(%p, %s, %i, %p, %s, %p, %p)\n", graphics, debugstr_wn(string, length),
4078         length, font, debugstr_rectf(rect), format, brush);
4079
4080     if(!graphics || !string || !font || !brush || !rect)
4081         return InvalidParameter;
4082
4083     if((brush->bt != BrushTypeSolidColor)){
4084         FIXME("not implemented for given parameters\n");
4085         return NotImplemented;
4086     }
4087
4088     if(!graphics->hdc)
4089     {
4090         FIXME("graphics object has no HDC\n");
4091         return Ok;
4092     }
4093
4094     if(format){
4095         TRACE("may be ignoring some format flags: attr %x\n", format->attr);
4096
4097         /* Should be no need to explicitly test for StringAlignmentNear as
4098          * that is default behavior if no alignment is passed. */
4099         if(format->vertalign != StringAlignmentNear){
4100             RectF bounds;
4101             GdipMeasureString(graphics, string, length, font, rect, format, &bounds, 0, 0);
4102
4103             if(format->vertalign == StringAlignmentCenter)
4104                 offsety = (rect->Height - bounds.Height) / 2;
4105             else if(format->vertalign == StringAlignmentFar)
4106                 offsety = (rect->Height - bounds.Height);
4107         }
4108     }
4109
4110     save_state = SaveDC(graphics->hdc);
4111     SetBkMode(graphics->hdc, TRANSPARENT);
4112     SetTextColor(graphics->hdc, brush->lb.lbColor);
4113
4114     pt[0].X = 0.0;
4115     pt[0].Y = 0.0;
4116     pt[1].X = 1.0;
4117     pt[1].Y = 0.0;
4118     pt[2].X = 0.0;
4119     pt[2].Y = 1.0;
4120     GdipTransformPoints(graphics, CoordinateSpaceDevice, CoordinateSpaceWorld, pt, 3);
4121     angle = -gdiplus_atan2((pt[1].Y - pt[0].Y), (pt[1].X - pt[0].X));
4122     args.ang_cos = cos(angle);
4123     args.ang_sin = sin(angle);
4124     rel_width = sqrt((pt[1].Y-pt[0].Y)*(pt[1].Y-pt[0].Y)+
4125                      (pt[1].X-pt[0].X)*(pt[1].X-pt[0].X));
4126     rel_height = sqrt((pt[2].Y-pt[0].Y)*(pt[2].Y-pt[0].Y)+
4127                       (pt[2].X-pt[0].X)*(pt[2].X-pt[0].X));
4128
4129     rectcpy[3].X = rectcpy[0].X = rect->X;
4130     rectcpy[1].Y = rectcpy[0].Y = rect->Y + offsety;
4131     rectcpy[2].X = rectcpy[1].X = rect->X + rect->Width;
4132     rectcpy[3].Y = rectcpy[2].Y = rect->Y + offsety + rect->Height;
4133     transform_and_round_points(graphics, corners, rectcpy, 4);
4134
4135     scaled_rect.X = 0.0;
4136     scaled_rect.Y = 0.0;
4137     scaled_rect.Width = rel_width * rect->Width;
4138     scaled_rect.Height = rel_height * rect->Height;
4139
4140     if (roundr(scaled_rect.Width) != 0 && roundr(scaled_rect.Height) != 0)
4141     {
4142         /* FIXME: If only the width or only the height is 0, we should probably still clip */
4143         rgn = CreatePolygonRgn(corners, 4, ALTERNATE);
4144         SelectClipRgn(graphics->hdc, rgn);
4145     }
4146
4147     /* Use gdi to find the font, then perform transformations on it (height,
4148      * width, angle). */
4149     SelectObject(graphics->hdc, CreateFontIndirectW(&font->lfw));
4150     GetTextMetricsW(graphics->hdc, &textmet);
4151     lfw = font->lfw;
4152
4153     lfw.lfHeight = roundr(((REAL)lfw.lfHeight) * rel_height);
4154     lfw.lfWidth = roundr(textmet.tmAveCharWidth * rel_width);
4155
4156     lfw.lfEscapement = lfw.lfOrientation = roundr((angle / M_PI) * 1800.0);
4157
4158     gdifont = CreateFontIndirectW(&lfw);
4159     DeleteObject(SelectObject(graphics->hdc, CreateFontIndirectW(&lfw)));
4160
4161     if (!format || format->align == StringAlignmentNear)
4162     {
4163         args.drawbase.x = corners[0].x;
4164         args.drawbase.y = corners[0].y;
4165         args.drawflags = DT_NOCLIP | DT_EXPANDTABS;
4166     }
4167     else if (format->align == StringAlignmentCenter)
4168     {
4169         args.drawbase.x = (corners[0].x + corners[1].x)/2;
4170         args.drawbase.y = (corners[0].y + corners[1].y)/2;
4171         args.drawflags = DT_NOCLIP | DT_EXPANDTABS | DT_CENTER;
4172     }
4173     else /* (format->align == StringAlignmentFar) */
4174     {
4175         args.drawbase.x = corners[1].x;
4176         args.drawbase.y = corners[1].y;
4177         args.drawflags = DT_NOCLIP | DT_EXPANDTABS | DT_RIGHT;
4178     }
4179
4180     gdip_format_string(graphics, string, length, font, &scaled_rect, format,
4181         draw_string_callback, &args);
4182
4183     DeleteObject(rgn);
4184     DeleteObject(gdifont);
4185
4186     RestoreDC(graphics->hdc, save_state);
4187
4188     return Ok;
4189 }
4190
4191 GpStatus WINGDIPAPI GdipResetClip(GpGraphics *graphics)
4192 {
4193     TRACE("(%p)\n", graphics);
4194
4195     if(!graphics)
4196         return InvalidParameter;
4197
4198     if(graphics->busy)
4199         return ObjectBusy;
4200
4201     return GdipSetInfinite(graphics->clip);
4202 }
4203
4204 GpStatus WINGDIPAPI GdipResetWorldTransform(GpGraphics *graphics)
4205 {
4206     TRACE("(%p)\n", graphics);
4207
4208     if(!graphics)
4209         return InvalidParameter;
4210
4211     if(graphics->busy)
4212         return ObjectBusy;
4213
4214     graphics->worldtrans->matrix[0] = 1.0;
4215     graphics->worldtrans->matrix[1] = 0.0;
4216     graphics->worldtrans->matrix[2] = 0.0;
4217     graphics->worldtrans->matrix[3] = 1.0;
4218     graphics->worldtrans->matrix[4] = 0.0;
4219     graphics->worldtrans->matrix[5] = 0.0;
4220
4221     return Ok;
4222 }
4223
4224 GpStatus WINGDIPAPI GdipRestoreGraphics(GpGraphics *graphics, GraphicsState state)
4225 {
4226     return GdipEndContainer(graphics, state);
4227 }
4228
4229 GpStatus WINGDIPAPI GdipRotateWorldTransform(GpGraphics *graphics, REAL angle,
4230     GpMatrixOrder order)
4231 {
4232     TRACE("(%p, %.2f, %d)\n", graphics, angle, order);
4233
4234     if(!graphics)
4235         return InvalidParameter;
4236
4237     if(graphics->busy)
4238         return ObjectBusy;
4239
4240     return GdipRotateMatrix(graphics->worldtrans, angle, order);
4241 }
4242
4243 GpStatus WINGDIPAPI GdipSaveGraphics(GpGraphics *graphics, GraphicsState *state)
4244 {
4245     return GdipBeginContainer2(graphics, state);
4246 }
4247
4248 GpStatus WINGDIPAPI GdipBeginContainer2(GpGraphics *graphics,
4249         GraphicsContainer *state)
4250 {
4251     GraphicsContainerItem *container;
4252     GpStatus sts;
4253
4254     TRACE("(%p, %p)\n", graphics, state);
4255
4256     if(!graphics || !state)
4257         return InvalidParameter;
4258
4259     sts = init_container(&container, graphics);
4260     if(sts != Ok)
4261         return sts;
4262
4263     list_add_head(&graphics->containers, &container->entry);
4264     *state = graphics->contid = container->contid;
4265
4266     return Ok;
4267 }
4268
4269 GpStatus WINGDIPAPI GdipBeginContainer(GpGraphics *graphics, GDIPCONST GpRectF *dstrect, GDIPCONST GpRectF *srcrect, GpUnit unit, GraphicsContainer *state)
4270 {
4271     FIXME("(%p, %p, %p, %d, %p): stub\n", graphics, dstrect, srcrect, unit, state);
4272     return NotImplemented;
4273 }
4274
4275 GpStatus WINGDIPAPI GdipBeginContainerI(GpGraphics *graphics, GDIPCONST GpRect *dstrect, GDIPCONST GpRect *srcrect, GpUnit unit, GraphicsContainer *state)
4276 {
4277     FIXME("(%p, %p, %p, %d, %p): stub\n", graphics, dstrect, srcrect, unit, state);
4278     return NotImplemented;
4279 }
4280
4281 GpStatus WINGDIPAPI GdipComment(GpGraphics *graphics, UINT sizeData, GDIPCONST BYTE *data)
4282 {
4283     FIXME("(%p, %d, %p): stub\n", graphics, sizeData, data);
4284     return NotImplemented;
4285 }
4286
4287 GpStatus WINGDIPAPI GdipEndContainer(GpGraphics *graphics, GraphicsContainer state)
4288 {
4289     GpStatus sts;
4290     GraphicsContainerItem *container, *container2;
4291
4292     TRACE("(%p, %x)\n", graphics, state);
4293
4294     if(!graphics)
4295         return InvalidParameter;
4296
4297     LIST_FOR_EACH_ENTRY(container, &graphics->containers, GraphicsContainerItem, entry){
4298         if(container->contid == state)
4299             break;
4300     }
4301
4302     /* did not find a matching container */
4303     if(&container->entry == &graphics->containers)
4304         return Ok;
4305
4306     sts = restore_container(graphics, container);
4307     if(sts != Ok)
4308         return sts;
4309
4310     /* remove all of the containers on top of the found container */
4311     LIST_FOR_EACH_ENTRY_SAFE(container, container2, &graphics->containers, GraphicsContainerItem, entry){
4312         if(container->contid == state)
4313             break;
4314         list_remove(&container->entry);
4315         delete_container(container);
4316     }
4317
4318     list_remove(&container->entry);
4319     delete_container(container);
4320
4321     return Ok;
4322 }
4323
4324 GpStatus WINGDIPAPI GdipScaleWorldTransform(GpGraphics *graphics, REAL sx,
4325     REAL sy, GpMatrixOrder order)
4326 {
4327     TRACE("(%p, %.2f, %.2f, %d)\n", graphics, sx, sy, order);
4328
4329     if(!graphics)
4330         return InvalidParameter;
4331
4332     if(graphics->busy)
4333         return ObjectBusy;
4334
4335     return GdipScaleMatrix(graphics->worldtrans, sx, sy, order);
4336 }
4337
4338 GpStatus WINGDIPAPI GdipSetClipGraphics(GpGraphics *graphics, GpGraphics *srcgraphics,
4339     CombineMode mode)
4340 {
4341     TRACE("(%p, %p, %d)\n", graphics, srcgraphics, mode);
4342
4343     if(!graphics || !srcgraphics)
4344         return InvalidParameter;
4345
4346     return GdipCombineRegionRegion(graphics->clip, srcgraphics->clip, mode);
4347 }
4348
4349 GpStatus WINGDIPAPI GdipSetCompositingMode(GpGraphics *graphics,
4350     CompositingMode mode)
4351 {
4352     TRACE("(%p, %d)\n", graphics, mode);
4353
4354     if(!graphics)
4355         return InvalidParameter;
4356
4357     if(graphics->busy)
4358         return ObjectBusy;
4359
4360     graphics->compmode = mode;
4361
4362     return Ok;
4363 }
4364
4365 GpStatus WINGDIPAPI GdipSetCompositingQuality(GpGraphics *graphics,
4366     CompositingQuality quality)
4367 {
4368     TRACE("(%p, %d)\n", graphics, quality);
4369
4370     if(!graphics)
4371         return InvalidParameter;
4372
4373     if(graphics->busy)
4374         return ObjectBusy;
4375
4376     graphics->compqual = quality;
4377
4378     return Ok;
4379 }
4380
4381 GpStatus WINGDIPAPI GdipSetInterpolationMode(GpGraphics *graphics,
4382     InterpolationMode mode)
4383 {
4384     TRACE("(%p, %d)\n", graphics, mode);
4385
4386     if(!graphics)
4387         return InvalidParameter;
4388
4389     if(graphics->busy)
4390         return ObjectBusy;
4391
4392     graphics->interpolation = mode;
4393
4394     return Ok;
4395 }
4396
4397 GpStatus WINGDIPAPI GdipSetPageScale(GpGraphics *graphics, REAL scale)
4398 {
4399     TRACE("(%p, %.2f)\n", graphics, scale);
4400
4401     if(!graphics || (scale <= 0.0))
4402         return InvalidParameter;
4403
4404     if(graphics->busy)
4405         return ObjectBusy;
4406
4407     graphics->scale = scale;
4408
4409     return Ok;
4410 }
4411
4412 GpStatus WINGDIPAPI GdipSetPageUnit(GpGraphics *graphics, GpUnit unit)
4413 {
4414     TRACE("(%p, %d)\n", graphics, unit);
4415
4416     if(!graphics)
4417         return InvalidParameter;
4418
4419     if(graphics->busy)
4420         return ObjectBusy;
4421
4422     if(unit == UnitWorld)
4423         return InvalidParameter;
4424
4425     graphics->unit = unit;
4426
4427     return Ok;
4428 }
4429
4430 GpStatus WINGDIPAPI GdipSetPixelOffsetMode(GpGraphics *graphics, PixelOffsetMode
4431     mode)
4432 {
4433     TRACE("(%p, %d)\n", graphics, mode);
4434
4435     if(!graphics)
4436         return InvalidParameter;
4437
4438     if(graphics->busy)
4439         return ObjectBusy;
4440
4441     graphics->pixeloffset = mode;
4442
4443     return Ok;
4444 }
4445
4446 GpStatus WINGDIPAPI GdipSetRenderingOrigin(GpGraphics *graphics, INT x, INT y)
4447 {
4448     static int calls;
4449
4450     TRACE("(%p,%i,%i)\n", graphics, x, y);
4451
4452     if (!(calls++))
4453         FIXME("not implemented\n");
4454
4455     return NotImplemented;
4456 }
4457
4458 GpStatus WINGDIPAPI GdipGetRenderingOrigin(GpGraphics *graphics, INT *x, INT *y)
4459 {
4460     static int calls;
4461
4462     TRACE("(%p,%p,%p)\n", graphics, x, y);
4463
4464     if (!(calls++))
4465         FIXME("not implemented\n");
4466
4467     *x = *y = 0;
4468
4469     return NotImplemented;
4470 }
4471
4472 GpStatus WINGDIPAPI GdipSetSmoothingMode(GpGraphics *graphics, SmoothingMode mode)
4473 {
4474     TRACE("(%p, %d)\n", graphics, mode);
4475
4476     if(!graphics)
4477         return InvalidParameter;
4478
4479     if(graphics->busy)
4480         return ObjectBusy;
4481
4482     graphics->smoothing = mode;
4483
4484     return Ok;
4485 }
4486
4487 GpStatus WINGDIPAPI GdipSetTextContrast(GpGraphics *graphics, UINT contrast)
4488 {
4489     TRACE("(%p, %d)\n", graphics, contrast);
4490
4491     if(!graphics)
4492         return InvalidParameter;
4493
4494     graphics->textcontrast = contrast;
4495
4496     return Ok;
4497 }
4498
4499 GpStatus WINGDIPAPI GdipSetTextRenderingHint(GpGraphics *graphics,
4500     TextRenderingHint hint)
4501 {
4502     TRACE("(%p, %d)\n", graphics, hint);
4503
4504     if(!graphics)
4505         return InvalidParameter;
4506
4507     if(graphics->busy)
4508         return ObjectBusy;
4509
4510     graphics->texthint = hint;
4511
4512     return Ok;
4513 }
4514
4515 GpStatus WINGDIPAPI GdipSetWorldTransform(GpGraphics *graphics, GpMatrix *matrix)
4516 {
4517     TRACE("(%p, %p)\n", graphics, matrix);
4518
4519     if(!graphics || !matrix)
4520         return InvalidParameter;
4521
4522     if(graphics->busy)
4523         return ObjectBusy;
4524
4525     GdipDeleteMatrix(graphics->worldtrans);
4526     return GdipCloneMatrix(matrix, &graphics->worldtrans);
4527 }
4528
4529 GpStatus WINGDIPAPI GdipTranslateWorldTransform(GpGraphics *graphics, REAL dx,
4530     REAL dy, GpMatrixOrder order)
4531 {
4532     TRACE("(%p, %.2f, %.2f, %d)\n", graphics, dx, dy, order);
4533
4534     if(!graphics)
4535         return InvalidParameter;
4536
4537     if(graphics->busy)
4538         return ObjectBusy;
4539
4540     return GdipTranslateMatrix(graphics->worldtrans, dx, dy, order);
4541 }
4542
4543 /*****************************************************************************
4544  * GdipSetClipHrgn [GDIPLUS.@]
4545  */
4546 GpStatus WINGDIPAPI GdipSetClipHrgn(GpGraphics *graphics, HRGN hrgn, CombineMode mode)
4547 {
4548     GpRegion *region;
4549     GpStatus status;
4550
4551     TRACE("(%p, %p, %d)\n", graphics, hrgn, mode);
4552
4553     if(!graphics)
4554         return InvalidParameter;
4555
4556     status = GdipCreateRegionHrgn(hrgn, &region);
4557     if(status != Ok)
4558         return status;
4559
4560     status = GdipSetClipRegion(graphics, region, mode);
4561
4562     GdipDeleteRegion(region);
4563     return status;
4564 }
4565
4566 GpStatus WINGDIPAPI GdipSetClipPath(GpGraphics *graphics, GpPath *path, CombineMode mode)
4567 {
4568     TRACE("(%p, %p, %d)\n", graphics, path, mode);
4569
4570     if(!graphics)
4571         return InvalidParameter;
4572
4573     if(graphics->busy)
4574         return ObjectBusy;
4575
4576     return GdipCombineRegionPath(graphics->clip, path, mode);
4577 }
4578
4579 GpStatus WINGDIPAPI GdipSetClipRect(GpGraphics *graphics, REAL x, REAL y,
4580                                     REAL width, REAL height,
4581                                     CombineMode mode)
4582 {
4583     GpRectF rect;
4584
4585     TRACE("(%p, %.2f, %.2f, %.2f, %.2f, %d)\n", graphics, x, y, width, height, mode);
4586
4587     if(!graphics)
4588         return InvalidParameter;
4589
4590     if(graphics->busy)
4591         return ObjectBusy;
4592
4593     rect.X = x;
4594     rect.Y = y;
4595     rect.Width  = width;
4596     rect.Height = height;
4597
4598     return GdipCombineRegionRect(graphics->clip, &rect, mode);
4599 }
4600
4601 GpStatus WINGDIPAPI GdipSetClipRectI(GpGraphics *graphics, INT x, INT y,
4602                                      INT width, INT height,
4603                                      CombineMode mode)
4604 {
4605     TRACE("(%p, %d, %d, %d, %d, %d)\n", graphics, x, y, width, height, mode);
4606
4607     if(!graphics)
4608         return InvalidParameter;
4609
4610     if(graphics->busy)
4611         return ObjectBusy;
4612
4613     return GdipSetClipRect(graphics, (REAL)x, (REAL)y, (REAL)width, (REAL)height, mode);
4614 }
4615
4616 GpStatus WINGDIPAPI GdipSetClipRegion(GpGraphics *graphics, GpRegion *region,
4617                                       CombineMode mode)
4618 {
4619     TRACE("(%p, %p, %d)\n", graphics, region, mode);
4620
4621     if(!graphics || !region)
4622         return InvalidParameter;
4623
4624     if(graphics->busy)
4625         return ObjectBusy;
4626
4627     return GdipCombineRegionRegion(graphics->clip, region, mode);
4628 }
4629
4630 GpStatus WINGDIPAPI GdipSetMetafileDownLevelRasterizationLimit(GpMetafile *metafile,
4631     UINT limitDpi)
4632 {
4633     static int calls;
4634
4635     TRACE("(%p,%u)\n", metafile, limitDpi);
4636
4637     if(!(calls++))
4638         FIXME("not implemented\n");
4639
4640     return NotImplemented;
4641 }
4642
4643 GpStatus WINGDIPAPI GdipDrawPolygon(GpGraphics *graphics,GpPen *pen,GDIPCONST GpPointF *points,
4644     INT count)
4645 {
4646     INT save_state;
4647     POINT *pti;
4648
4649     TRACE("(%p, %p, %d)\n", graphics, points, count);
4650
4651     if(!graphics || !pen || count<=0)
4652         return InvalidParameter;
4653
4654     if(graphics->busy)
4655         return ObjectBusy;
4656
4657     if (!graphics->hdc)
4658     {
4659         FIXME("graphics object has no HDC\n");
4660         return Ok;
4661     }
4662
4663     pti = GdipAlloc(sizeof(POINT) * count);
4664
4665     save_state = prepare_dc(graphics, pen);
4666     SelectObject(graphics->hdc, GetStockObject(NULL_BRUSH));
4667
4668     transform_and_round_points(graphics, pti, (GpPointF*)points, count);
4669     Polygon(graphics->hdc, pti, count);
4670
4671     restore_dc(graphics, save_state);
4672     GdipFree(pti);
4673
4674     return Ok;
4675 }
4676
4677 GpStatus WINGDIPAPI GdipDrawPolygonI(GpGraphics *graphics,GpPen *pen,GDIPCONST GpPoint *points,
4678     INT count)
4679 {
4680     GpStatus ret;
4681     GpPointF *ptf;
4682     INT i;
4683
4684     TRACE("(%p, %p, %p, %d)\n", graphics, pen, points, count);
4685
4686     if(count<=0)    return InvalidParameter;
4687     ptf = GdipAlloc(sizeof(GpPointF) * count);
4688
4689     for(i = 0;i < count; i++){
4690         ptf[i].X = (REAL)points[i].X;
4691         ptf[i].Y = (REAL)points[i].Y;
4692     }
4693
4694     ret = GdipDrawPolygon(graphics,pen,ptf,count);
4695     GdipFree(ptf);
4696
4697     return ret;
4698 }
4699
4700 GpStatus WINGDIPAPI GdipGetDpiX(GpGraphics *graphics, REAL* dpi)
4701 {
4702     TRACE("(%p, %p)\n", graphics, dpi);
4703
4704     if(!graphics || !dpi)
4705         return InvalidParameter;
4706
4707     if(graphics->busy)
4708         return ObjectBusy;
4709
4710     if (graphics->image)
4711         *dpi = graphics->image->xres;
4712     else
4713         *dpi = (REAL)GetDeviceCaps(graphics->hdc, LOGPIXELSX);
4714
4715     return Ok;
4716 }
4717
4718 GpStatus WINGDIPAPI GdipGetDpiY(GpGraphics *graphics, REAL* dpi)
4719 {
4720     TRACE("(%p, %p)\n", graphics, dpi);
4721
4722     if(!graphics || !dpi)
4723         return InvalidParameter;
4724
4725     if(graphics->busy)
4726         return ObjectBusy;
4727
4728     if (graphics->image)
4729         *dpi = graphics->image->yres;
4730     else
4731         *dpi = (REAL)GetDeviceCaps(graphics->hdc, LOGPIXELSY);
4732
4733     return Ok;
4734 }
4735
4736 GpStatus WINGDIPAPI GdipMultiplyWorldTransform(GpGraphics *graphics, GDIPCONST GpMatrix *matrix,
4737     GpMatrixOrder order)
4738 {
4739     GpMatrix m;
4740     GpStatus ret;
4741
4742     TRACE("(%p, %p, %d)\n", graphics, matrix, order);
4743
4744     if(!graphics || !matrix)
4745         return InvalidParameter;
4746
4747     if(graphics->busy)
4748         return ObjectBusy;
4749
4750     m = *(graphics->worldtrans);
4751
4752     ret = GdipMultiplyMatrix(&m, matrix, order);
4753     if(ret == Ok)
4754         *(graphics->worldtrans) = m;
4755
4756     return ret;
4757 }
4758
4759 /* Color used to fill bitmaps so we can tell which parts have been drawn over by gdi32. */
4760 static const COLORREF DC_BACKGROUND_KEY = 0x0c0b0d;
4761
4762 GpStatus WINGDIPAPI GdipGetDC(GpGraphics *graphics, HDC *hdc)
4763 {
4764     TRACE("(%p, %p)\n", graphics, hdc);
4765
4766     if(!graphics || !hdc)
4767         return InvalidParameter;
4768
4769     if(graphics->busy)
4770         return ObjectBusy;
4771
4772     if (!graphics->hdc ||
4773         (graphics->image && graphics->image->type == ImageTypeBitmap && ((GpBitmap*)graphics->image)->format & PixelFormatAlpha))
4774     {
4775         /* Create a fake HDC and fill it with a constant color. */
4776         HDC temp_hdc;
4777         HBITMAP hbitmap;
4778         GpStatus stat;
4779         GpRectF bounds;
4780         BITMAPINFOHEADER bmih;
4781         int i;
4782
4783         stat = get_graphics_bounds(graphics, &bounds);
4784         if (stat != Ok)
4785             return stat;
4786
4787         graphics->temp_hbitmap_width = bounds.Width;
4788         graphics->temp_hbitmap_height = bounds.Height;
4789
4790         bmih.biSize = sizeof(bmih);
4791         bmih.biWidth = graphics->temp_hbitmap_width;
4792         bmih.biHeight = -graphics->temp_hbitmap_height;
4793         bmih.biPlanes = 1;
4794         bmih.biBitCount = 32;
4795         bmih.biCompression = BI_RGB;
4796         bmih.biSizeImage = 0;
4797         bmih.biXPelsPerMeter = 0;
4798         bmih.biYPelsPerMeter = 0;
4799         bmih.biClrUsed = 0;
4800         bmih.biClrImportant = 0;
4801
4802         hbitmap = CreateDIBSection(NULL, (BITMAPINFO*)&bmih, DIB_RGB_COLORS,
4803             (void**)&graphics->temp_bits, NULL, 0);
4804         if (!hbitmap)
4805             return GenericError;
4806
4807         temp_hdc = CreateCompatibleDC(0);
4808         if (!temp_hdc)
4809         {
4810             DeleteObject(hbitmap);
4811             return GenericError;
4812         }
4813
4814         for (i=0; i<(graphics->temp_hbitmap_width * graphics->temp_hbitmap_height); i++)
4815             ((DWORD*)graphics->temp_bits)[i] = DC_BACKGROUND_KEY;
4816
4817         SelectObject(temp_hdc, hbitmap);
4818
4819         graphics->temp_hbitmap = hbitmap;
4820         *hdc = graphics->temp_hdc = temp_hdc;
4821     }
4822     else
4823     {
4824         *hdc = graphics->hdc;
4825     }
4826
4827     graphics->busy = TRUE;
4828
4829     return Ok;
4830 }
4831
4832 GpStatus WINGDIPAPI GdipReleaseDC(GpGraphics *graphics, HDC hdc)
4833 {
4834     TRACE("(%p, %p)\n", graphics, hdc);
4835
4836     if(!graphics || !hdc)
4837         return InvalidParameter;
4838
4839     if((graphics->hdc != hdc && graphics->temp_hdc != hdc) || !(graphics->busy))
4840         return InvalidParameter;
4841
4842     if (graphics->temp_hdc == hdc)
4843     {
4844         DWORD* pos;
4845         int i;
4846
4847         /* Find the pixels that have changed, and mark them as opaque. */
4848         pos = (DWORD*)graphics->temp_bits;
4849         for (i=0; i<(graphics->temp_hbitmap_width * graphics->temp_hbitmap_height); i++)
4850         {
4851             if (*pos != DC_BACKGROUND_KEY)
4852             {
4853                 *pos |= 0xff000000;
4854             }
4855             pos++;
4856         }
4857
4858         /* Write the changed pixels to the real target. */
4859         alpha_blend_pixels(graphics, 0, 0, graphics->temp_bits,
4860             graphics->temp_hbitmap_width, graphics->temp_hbitmap_height,
4861             graphics->temp_hbitmap_width * 4);
4862
4863         /* Clean up. */
4864         DeleteDC(graphics->temp_hdc);
4865         DeleteObject(graphics->temp_hbitmap);
4866         graphics->temp_hdc = NULL;
4867         graphics->temp_hbitmap = NULL;
4868     }
4869
4870     graphics->busy = FALSE;
4871
4872     return Ok;
4873 }
4874
4875 GpStatus WINGDIPAPI GdipGetClip(GpGraphics *graphics, GpRegion *region)
4876 {
4877     GpRegion *clip;
4878     GpStatus status;
4879
4880     TRACE("(%p, %p)\n", graphics, region);
4881
4882     if(!graphics || !region)
4883         return InvalidParameter;
4884
4885     if(graphics->busy)
4886         return ObjectBusy;
4887
4888     if((status = GdipCloneRegion(graphics->clip, &clip)) != Ok)
4889         return status;
4890
4891     /* free everything except root node and header */
4892     delete_element(&region->node);
4893     memcpy(region, clip, sizeof(GpRegion));
4894     GdipFree(clip);
4895
4896     return Ok;
4897 }
4898
4899 GpStatus WINGDIPAPI GdipTransformPoints(GpGraphics *graphics, GpCoordinateSpace dst_space,
4900                                         GpCoordinateSpace src_space, GpPointF *points, INT count)
4901 {
4902     GpMatrix *matrix;
4903     GpStatus stat;
4904     REAL unitscale;
4905
4906     if(!graphics || !points || count <= 0)
4907         return InvalidParameter;
4908
4909     if(graphics->busy)
4910         return ObjectBusy;
4911
4912     TRACE("(%p, %d, %d, %p, %d)\n", graphics, dst_space, src_space, points, count);
4913
4914     if (src_space == dst_space) return Ok;
4915
4916     stat = GdipCreateMatrix(&matrix);
4917     if (stat == Ok)
4918     {
4919         unitscale = convert_unit(graphics_res(graphics), graphics->unit);
4920
4921         if(graphics->unit != UnitDisplay)
4922             unitscale *= graphics->scale;
4923
4924         /* transform from src_space to CoordinateSpacePage */
4925         switch (src_space)
4926         {
4927         case CoordinateSpaceWorld:
4928             GdipMultiplyMatrix(matrix, graphics->worldtrans, MatrixOrderAppend);
4929             break;
4930         case CoordinateSpacePage:
4931             break;
4932         case CoordinateSpaceDevice:
4933             GdipScaleMatrix(matrix, 1.0/unitscale, 1.0/unitscale, MatrixOrderAppend);
4934             break;
4935         }
4936
4937         /* transform from CoordinateSpacePage to dst_space */
4938         switch (dst_space)
4939         {
4940         case CoordinateSpaceWorld:
4941             {
4942                 GpMatrix *inverted_transform;
4943                 stat = GdipCloneMatrix(graphics->worldtrans, &inverted_transform);
4944                 if (stat == Ok)
4945                 {
4946                     stat = GdipInvertMatrix(inverted_transform);
4947                     if (stat == Ok)
4948                         GdipMultiplyMatrix(matrix, inverted_transform, MatrixOrderAppend);
4949                     GdipDeleteMatrix(inverted_transform);
4950                 }
4951                 break;
4952             }
4953         case CoordinateSpacePage:
4954             break;
4955         case CoordinateSpaceDevice:
4956             GdipScaleMatrix(matrix, unitscale, unitscale, MatrixOrderAppend);
4957             break;
4958         }
4959
4960         if (stat == Ok)
4961             stat = GdipTransformMatrixPoints(matrix, points, count);
4962
4963         GdipDeleteMatrix(matrix);
4964     }
4965
4966     return stat;
4967 }
4968
4969 GpStatus WINGDIPAPI GdipTransformPointsI(GpGraphics *graphics, GpCoordinateSpace dst_space,
4970                                          GpCoordinateSpace src_space, GpPoint *points, INT count)
4971 {
4972     GpPointF *pointsF;
4973     GpStatus ret;
4974     INT i;
4975
4976     TRACE("(%p, %d, %d, %p, %d)\n", graphics, dst_space, src_space, points, count);
4977
4978     if(count <= 0)
4979         return InvalidParameter;
4980
4981     pointsF = GdipAlloc(sizeof(GpPointF) * count);
4982     if(!pointsF)
4983         return OutOfMemory;
4984
4985     for(i = 0; i < count; i++){
4986         pointsF[i].X = (REAL)points[i].X;
4987         pointsF[i].Y = (REAL)points[i].Y;
4988     }
4989
4990     ret = GdipTransformPoints(graphics, dst_space, src_space, pointsF, count);
4991
4992     if(ret == Ok)
4993         for(i = 0; i < count; i++){
4994             points[i].X = roundr(pointsF[i].X);
4995             points[i].Y = roundr(pointsF[i].Y);
4996         }
4997     GdipFree(pointsF);
4998
4999     return ret;
5000 }
5001
5002 HPALETTE WINGDIPAPI GdipCreateHalftonePalette(void)
5003 {
5004     static int calls;
5005
5006     TRACE("\n");
5007
5008     if (!calls++)
5009       FIXME("stub\n");
5010
5011     return NULL;
5012 }
5013
5014 /*****************************************************************************
5015  * GdipTranslateClip [GDIPLUS.@]
5016  */
5017 GpStatus WINGDIPAPI GdipTranslateClip(GpGraphics *graphics, REAL dx, REAL dy)
5018 {
5019     TRACE("(%p, %.2f, %.2f)\n", graphics, dx, dy);
5020
5021     if(!graphics)
5022         return InvalidParameter;
5023
5024     if(graphics->busy)
5025         return ObjectBusy;
5026
5027     return GdipTranslateRegion(graphics->clip, dx, dy);
5028 }
5029
5030 /*****************************************************************************
5031  * GdipTranslateClipI [GDIPLUS.@]
5032  */
5033 GpStatus WINGDIPAPI GdipTranslateClipI(GpGraphics *graphics, INT dx, INT dy)
5034 {
5035     TRACE("(%p, %d, %d)\n", graphics, dx, dy);
5036
5037     if(!graphics)
5038         return InvalidParameter;
5039
5040     if(graphics->busy)
5041         return ObjectBusy;
5042
5043     return GdipTranslateRegion(graphics->clip, (REAL)dx, (REAL)dy);
5044 }
5045
5046
5047 /*****************************************************************************
5048  * GdipMeasureDriverString [GDIPLUS.@]
5049  */
5050 GpStatus WINGDIPAPI GdipMeasureDriverString(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
5051                                             GDIPCONST GpFont *font, GDIPCONST PointF *positions,
5052                                             INT flags, GDIPCONST GpMatrix *matrix, RectF *boundingBox)
5053 {
5054     FIXME("(%p %p %d %p %p %d %p %p): stub\n", graphics, text, length, font, positions, flags, matrix, boundingBox);
5055     return NotImplemented;
5056 }
5057
5058 /*****************************************************************************
5059  * GdipDrawDriverString [GDIPLUS.@]
5060  */
5061 GpStatus WINGDIPAPI GdipDrawDriverString(GpGraphics *graphics, GDIPCONST UINT16 *text, INT length,
5062                                          GDIPCONST GpFont *font, GDIPCONST GpBrush *brush,
5063                                          GDIPCONST PointF *positions, INT flags,
5064                                          GDIPCONST GpMatrix *matrix )
5065 {
5066     FIXME("(%p %p %d %p %p %p %d %p): stub\n", graphics, text, length, font, brush, positions, flags, matrix);
5067     return NotImplemented;
5068 }
5069
5070 GpStatus WINGDIPAPI GdipRecordMetafile(HDC hdc, EmfType type, GDIPCONST GpRectF *frameRect,
5071                                        MetafileFrameUnit frameUnit, GDIPCONST WCHAR *desc, GpMetafile **metafile)
5072 {
5073     FIXME("(%p %d %p %d %p %p): stub\n", hdc, type, frameRect, frameUnit, desc, metafile);
5074     return NotImplemented;
5075 }
5076
5077 /*****************************************************************************
5078  * GdipRecordMetafileI [GDIPLUS.@]
5079  */
5080 GpStatus WINGDIPAPI GdipRecordMetafileI(HDC hdc, EmfType type, GDIPCONST GpRect *frameRect,
5081                                         MetafileFrameUnit frameUnit, GDIPCONST WCHAR *desc, GpMetafile **metafile)
5082 {
5083     FIXME("(%p %d %p %d %p %p): stub\n", hdc, type, frameRect, frameUnit, desc, metafile);
5084     return NotImplemented;
5085 }
5086
5087 GpStatus WINGDIPAPI GdipRecordMetafileStream(IStream *stream, HDC hdc, EmfType type, GDIPCONST GpRect *frameRect,
5088                                         MetafileFrameUnit frameUnit, GDIPCONST WCHAR *desc, GpMetafile **metafile)
5089 {
5090     FIXME("(%p %p %d %p %d %p %p): stub\n", stream, hdc, type, frameRect, frameUnit, desc, metafile);
5091     return NotImplemented;
5092 }
5093
5094 /*****************************************************************************
5095  * GdipIsVisibleClipEmpty [GDIPLUS.@]
5096  */
5097 GpStatus WINGDIPAPI GdipIsVisibleClipEmpty(GpGraphics *graphics, BOOL *res)
5098 {
5099     GpStatus stat;
5100     GpRegion* rgn;
5101
5102     TRACE("(%p, %p)\n", graphics, res);
5103
5104     if((stat = GdipCreateRegion(&rgn)) != Ok)
5105         return stat;
5106
5107     if((stat = get_visible_clip_region(graphics, rgn)) != Ok)
5108         goto cleanup;
5109
5110     stat = GdipIsEmptyRegion(rgn, graphics, res);
5111
5112 cleanup:
5113     GdipDeleteRegion(rgn);
5114     return stat;
5115 }