Fixed a compile error on non-i386.
[wine] / dlls / gdi / painting.c
1 /*
2  * GDI drawing functions.
3  *
4  * Copyright 1993, 1994 Alexandre Julliard
5  * Copyright 1997 Bertho A. Stultiens
6  *           1999 Huw D M Davies
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
22
23 #include "config.h"
24 #include "wine/port.h"
25
26 #include <stdarg.h>
27 #include <string.h>
28 #include <stdlib.h>
29
30 #include "windef.h"
31 #include "winbase.h"
32 #include "wingdi.h"
33 #include "winerror.h"
34 #include "gdi.h"
35 #include "gdi_private.h"
36 #include "wine/debug.h"
37
38 WINE_DEFAULT_DEBUG_CHANNEL(gdi);
39
40
41 /***********************************************************************
42  *           LineTo    (GDI32.@)
43  */
44 BOOL WINAPI LineTo( HDC hdc, INT x, INT y )
45 {
46     DC * dc = DC_GetDCUpdate( hdc );
47     BOOL ret;
48
49     if(!dc) return FALSE;
50
51     if(PATH_IsPathOpen(dc->path))
52         ret = PATH_LineTo(dc, x, y);
53     else
54         ret = dc->funcs->pLineTo && dc->funcs->pLineTo(dc->physDev,x,y);
55     if(ret) {
56         dc->CursPosX = x;
57         dc->CursPosY = y;
58     }
59     GDI_ReleaseObj( hdc );
60     return ret;
61 }
62
63
64 /***********************************************************************
65  *           MoveToEx    (GDI32.@)
66  */
67 BOOL WINAPI MoveToEx( HDC hdc, INT x, INT y, LPPOINT pt )
68 {
69     BOOL ret = TRUE;
70     DC * dc = DC_GetDCPtr( hdc );
71
72     if(!dc) return FALSE;
73
74     if(pt) {
75         pt->x = dc->CursPosX;
76         pt->y = dc->CursPosY;
77     }
78     dc->CursPosX = x;
79     dc->CursPosY = y;
80
81     if(PATH_IsPathOpen(dc->path)) ret = PATH_MoveTo(dc);
82     else if (dc->funcs->pMoveTo) ret = dc->funcs->pMoveTo(dc->physDev,x,y);
83     GDI_ReleaseObj( hdc );
84     return ret;
85 }
86
87
88 /***********************************************************************
89  *           Arc    (GDI32.@)
90  */
91 BOOL WINAPI Arc( HDC hdc, INT left, INT top, INT right,
92                      INT bottom, INT xstart, INT ystart,
93                      INT xend, INT yend )
94 {
95     BOOL ret = FALSE;
96     DC * dc = DC_GetDCUpdate( hdc );
97     if (dc)
98     {
99     if(PATH_IsPathOpen(dc->path))
100             ret = PATH_Arc(dc, left, top, right, bottom, xstart, ystart, xend, yend,0);
101         else if (dc->funcs->pArc)
102             ret = dc->funcs->pArc(dc->physDev,left,top,right,bottom,xstart,ystart,xend,yend);
103         GDI_ReleaseObj( hdc );
104     }
105     return ret;
106 }
107
108 /***********************************************************************
109  *           ArcTo    (GDI32.@)
110  */
111 BOOL WINAPI ArcTo( HDC hdc,
112                      INT left,   INT top,
113                      INT right,  INT bottom,
114                      INT xstart, INT ystart,
115                      INT xend,   INT yend )
116 {
117     BOOL result;
118     DC * dc = DC_GetDCUpdate( hdc );
119     if(!dc) return FALSE;
120
121     if(dc->funcs->pArcTo)
122     {
123         result = dc->funcs->pArcTo( dc->physDev, left, top, right, bottom,
124                                   xstart, ystart, xend, yend );
125         GDI_ReleaseObj( hdc );
126         return result;
127     }
128     GDI_ReleaseObj( hdc );
129     /*
130      * Else emulate it.
131      * According to the documentation, a line is drawn from the current
132      * position to the starting point of the arc.
133      */
134     LineTo(hdc, xstart, ystart);
135     /*
136      * Then the arc is drawn.
137      */
138     result = Arc(hdc, left, top, right, bottom, xstart, ystart, xend, yend);
139     /*
140      * If no error occurred, the current position is moved to the ending
141      * point of the arc.
142      */
143     if (result) MoveToEx(hdc, xend, yend, NULL);
144     return result;
145 }
146
147
148 /***********************************************************************
149  *           Pie   (GDI32.@)
150  */
151 BOOL WINAPI Pie( HDC hdc, INT left, INT top,
152                      INT right, INT bottom, INT xstart, INT ystart,
153                      INT xend, INT yend )
154 {
155     BOOL ret = FALSE;
156     DC * dc = DC_GetDCUpdate( hdc );
157     if (!dc) return FALSE;
158
159     if(PATH_IsPathOpen(dc->path))
160         ret = PATH_Arc(dc,left,top,right,bottom,xstart,ystart,xend,yend,2);
161     else if(dc->funcs->pPie)
162         ret = dc->funcs->pPie(dc->physDev,left,top,right,bottom,xstart,ystart,xend,yend);
163
164     GDI_ReleaseObj( hdc );
165     return ret;
166 }
167
168
169 /***********************************************************************
170  *           Chord    (GDI32.@)
171  */
172 BOOL WINAPI Chord( HDC hdc, INT left, INT top,
173                        INT right, INT bottom, INT xstart, INT ystart,
174                        INT xend, INT yend )
175 {
176     BOOL ret = FALSE;
177     DC * dc = DC_GetDCUpdate( hdc );
178     if (!dc) return FALSE;
179
180     if(PATH_IsPathOpen(dc->path))
181         ret = PATH_Arc(dc,left,top,right,bottom,xstart,ystart,xend,yend,1);
182     else if(dc->funcs->pChord)
183         ret = dc->funcs->pChord(dc->physDev,left,top,right,bottom,xstart,ystart,xend,yend);
184
185     GDI_ReleaseObj( hdc );
186     return ret;
187 }
188
189
190 /***********************************************************************
191  *           Ellipse    (GDI32.@)
192  */
193 BOOL WINAPI Ellipse( HDC hdc, INT left, INT top,
194                          INT right, INT bottom )
195 {
196     BOOL ret = FALSE;
197     DC * dc = DC_GetDCUpdate( hdc );
198     if (!dc) return FALSE;
199
200     if(PATH_IsPathOpen(dc->path))
201         ret = PATH_Ellipse(dc,left,top,right,bottom);
202     else if (dc->funcs->pEllipse)
203         ret = dc->funcs->pEllipse(dc->physDev,left,top,right,bottom);
204
205     GDI_ReleaseObj( hdc );
206     return ret;
207 }
208
209
210 /***********************************************************************
211  *           Rectangle    (GDI32.@)
212  */
213 BOOL WINAPI Rectangle( HDC hdc, INT left, INT top,
214                            INT right, INT bottom )
215 {
216     BOOL ret = FALSE;
217     DC * dc = DC_GetDCUpdate( hdc );
218     if (dc)
219     {
220     if(PATH_IsPathOpen(dc->path))
221             ret = PATH_Rectangle(dc, left, top, right, bottom);
222         else if (dc->funcs->pRectangle)
223             ret = dc->funcs->pRectangle(dc->physDev,left,top,right,bottom);
224         GDI_ReleaseObj( hdc );
225     }
226     return ret;
227 }
228
229
230 /***********************************************************************
231  *           RoundRect    (GDI32.@)
232  */
233 BOOL WINAPI RoundRect( HDC hdc, INT left, INT top, INT right,
234                            INT bottom, INT ell_width, INT ell_height )
235 {
236     BOOL ret = FALSE;
237     DC *dc = DC_GetDCUpdate( hdc );
238
239     if (dc)
240     {
241         if(PATH_IsPathOpen(dc->path))
242             ret = PATH_RoundRect(dc,left,top,right,bottom,ell_width,ell_height);
243         else if (dc->funcs->pRoundRect)
244             ret = dc->funcs->pRoundRect(dc->physDev,left,top,right,bottom,ell_width,ell_height);
245         GDI_ReleaseObj( hdc );
246     }
247     return ret;
248 }
249
250 /***********************************************************************
251  *           SetPixel    (GDI32.@)
252  */
253 COLORREF WINAPI SetPixel( HDC hdc, INT x, INT y, COLORREF color )
254 {
255     COLORREF ret = 0;
256     DC * dc = DC_GetDCUpdate( hdc );
257     if (dc)
258     {
259         if (dc->funcs->pSetPixel) ret = dc->funcs->pSetPixel(dc->physDev,x,y,color);
260         GDI_ReleaseObj( hdc );
261     }
262     return ret;
263 }
264
265 /***********************************************************************
266  *           SetPixelV    (GDI32.@)
267  */
268 BOOL WINAPI SetPixelV( HDC hdc, INT x, INT y, COLORREF color )
269 {
270     BOOL ret = FALSE;
271     DC * dc = DC_GetDCUpdate( hdc );
272     if (dc)
273     {
274         if (dc->funcs->pSetPixel)
275         {
276             dc->funcs->pSetPixel(dc->physDev,x,y,color);
277             ret = TRUE;
278         }
279         GDI_ReleaseObj( hdc );
280     }
281     return ret;
282 }
283
284 /***********************************************************************
285  *           GetPixel    (GDI32.@)
286  */
287 COLORREF WINAPI GetPixel( HDC hdc, INT x, INT y )
288 {
289     COLORREF ret = CLR_INVALID;
290     DC * dc = DC_GetDCUpdate( hdc );
291
292     if (dc)
293     {
294     /* FIXME: should this be in the graphics driver? */
295         if (PtVisible( hdc, x, y ))
296         {
297             if (dc->funcs->pGetPixel) ret = dc->funcs->pGetPixel(dc->physDev,x,y);
298         }
299         GDI_ReleaseObj( hdc );
300     }
301     return ret;
302 }
303
304
305 /******************************************************************************
306  * ChoosePixelFormat [GDI32.@]
307  * Matches a pixel format to given format
308  *
309  * PARAMS
310  *    hdc  [I] Device context to search for best pixel match
311  *    ppfd [I] Pixel format for which a match is sought
312  *
313  * RETURNS
314  *    Success: Pixel format index closest to given format
315  *    Failure: 0
316  */
317 INT WINAPI ChoosePixelFormat( HDC hdc, const LPPIXELFORMATDESCRIPTOR ppfd )
318 {
319     INT ret = 0;
320     DC * dc = DC_GetDCPtr( hdc );
321
322     TRACE("(%p,%p)\n",hdc,ppfd);
323
324     if (!dc) return 0;
325
326     if (!dc->funcs->pChoosePixelFormat) FIXME(" :stub\n");
327     else ret = dc->funcs->pChoosePixelFormat(dc->physDev,ppfd);
328
329     GDI_ReleaseObj( hdc );
330     return ret;
331 }
332
333
334 /******************************************************************************
335  * SetPixelFormat [GDI32.@]
336  * Sets pixel format of device context
337  *
338  * PARAMS
339  *    hdc          [I] Device context to search for best pixel match
340  *    iPixelFormat [I] Pixel format index
341  *    ppfd         [I] Pixel format for which a match is sought
342  *
343  * RETURNS STD
344  */
345 BOOL WINAPI SetPixelFormat( HDC hdc, INT iPixelFormat,
346                             const PIXELFORMATDESCRIPTOR *ppfd)
347 {
348     INT bRet = FALSE;
349     DC * dc = DC_GetDCPtr( hdc );
350
351     TRACE("(%p,%d,%p)\n",hdc,iPixelFormat,ppfd);
352
353     if (!dc) return 0;
354
355     if (!dc->funcs->pSetPixelFormat) FIXME(" :stub\n");
356     else bRet = dc->funcs->pSetPixelFormat(dc->physDev,iPixelFormat,ppfd);
357
358     GDI_ReleaseObj( hdc );
359     return bRet;
360 }
361
362
363 /******************************************************************************
364  * GetPixelFormat [GDI32.@]
365  * Gets index of pixel format of DC
366  *
367  * PARAMETERS
368  *    hdc [I] Device context whose pixel format index is sought
369  *
370  * RETURNS
371  *    Success: Currently selected pixel format
372  *    Failure: 0
373  */
374 INT WINAPI GetPixelFormat( HDC hdc )
375 {
376     INT ret = 0;
377     DC * dc = DC_GetDCPtr( hdc );
378
379     TRACE("(%p)\n",hdc);
380
381     if (!dc) return 0;
382
383     if (!dc->funcs->pGetPixelFormat) FIXME(" :stub\n");
384     else ret = dc->funcs->pGetPixelFormat(dc->physDev);
385
386     GDI_ReleaseObj( hdc );
387     return ret;
388 }
389
390
391 /******************************************************************************
392  * DescribePixelFormat [GDI32.@]
393  * Gets info about pixel format from DC
394  *
395  * PARAMS
396  *    hdc          [I] Device context
397  *    iPixelFormat [I] Pixel format selector
398  *    nBytes       [I] Size of buffer
399  *    ppfd         [O] Pointer to structure to receive pixel format data
400  *
401  * RETURNS
402  *    Success: Maximum pixel format index of the device context
403  *    Failure: 0
404  */
405 INT WINAPI DescribePixelFormat( HDC hdc, INT iPixelFormat, UINT nBytes,
406                                 LPPIXELFORMATDESCRIPTOR ppfd )
407 {
408     INT ret = 0;
409     DC * dc = DC_GetDCPtr( hdc );
410
411     TRACE("(%p,%d,%d,%p): stub\n",hdc,iPixelFormat,nBytes,ppfd);
412
413     if (!dc) return 0;
414
415     if (!dc->funcs->pDescribePixelFormat)
416     {
417         FIXME(" :stub\n");
418         ppfd->nSize = nBytes;
419         ppfd->nVersion = 1;
420         ret = 3;
421     }
422     else ret = dc->funcs->pDescribePixelFormat(dc->physDev,iPixelFormat,nBytes,ppfd);
423
424     GDI_ReleaseObj( hdc );
425     return ret;
426 }
427
428
429 /******************************************************************************
430  * SwapBuffers [GDI32.@]
431  * Exchanges front and back buffers of window
432  *
433  * PARAMS
434  *    hdc [I] Device context whose buffers get swapped
435  *
436  * RETURNS STD
437  */
438 BOOL WINAPI SwapBuffers( HDC hdc )
439 {
440     INT bRet = FALSE;
441     DC * dc = DC_GetDCPtr( hdc );
442
443     TRACE("(%p)\n",hdc);
444
445     if (!dc) return TRUE;
446
447     if (!dc->funcs->pSwapBuffers)
448     {
449         FIXME(" :stub\n");
450         bRet = TRUE;
451     }
452     else bRet = dc->funcs->pSwapBuffers(dc->physDev);
453
454     GDI_ReleaseObj( hdc );
455     return bRet;
456 }
457
458
459 /***********************************************************************
460  *           PaintRgn    (GDI32.@)
461  */
462 BOOL WINAPI PaintRgn( HDC hdc, HRGN hrgn )
463 {
464     BOOL ret = FALSE;
465     DC * dc = DC_GetDCUpdate( hdc );
466     if (dc)
467     {
468         if (dc->funcs->pPaintRgn) ret = dc->funcs->pPaintRgn(dc->physDev,hrgn);
469         GDI_ReleaseObj( hdc );
470     }
471     return ret;
472 }
473
474
475 /***********************************************************************
476  *           FillRgn    (GDI32.@)
477  */
478 BOOL WINAPI FillRgn( HDC hdc, HRGN hrgn, HBRUSH hbrush )
479 {
480     BOOL retval = FALSE;
481     HBRUSH prevBrush;
482     DC * dc = DC_GetDCUpdate( hdc );
483
484     if (!dc) return FALSE;
485     if(dc->funcs->pFillRgn)
486         retval = dc->funcs->pFillRgn(dc->physDev, hrgn, hbrush);
487     else if ((prevBrush = SelectObject( hdc, hbrush )))
488     {
489     retval = PaintRgn( hdc, hrgn );
490     SelectObject( hdc, prevBrush );
491     }
492     GDI_ReleaseObj( hdc );
493     return retval;
494 }
495
496
497 /***********************************************************************
498  *           FrameRgn     (GDI32.@)
499  */
500 BOOL WINAPI FrameRgn( HDC hdc, HRGN hrgn, HBRUSH hbrush,
501                           INT nWidth, INT nHeight )
502 {
503     BOOL ret = FALSE;
504     DC *dc = DC_GetDCUpdate( hdc );
505
506     if (!dc) return FALSE;
507     if(dc->funcs->pFrameRgn)
508         ret = dc->funcs->pFrameRgn( dc->physDev, hrgn, hbrush, nWidth, nHeight );
509     else
510     {
511         HRGN tmp = CreateRectRgn( 0, 0, 0, 0 );
512         if (tmp)
513         {
514             if (REGION_FrameRgn( tmp, hrgn, nWidth, nHeight ))
515             {
516                 FillRgn( hdc, tmp, hbrush );
517                 ret = TRUE;
518             }
519             DeleteObject( tmp );
520         }
521     }
522     GDI_ReleaseObj( hdc );
523     return ret;
524 }
525
526
527 /***********************************************************************
528  *           InvertRgn    (GDI32.@)
529  */
530 BOOL WINAPI InvertRgn( HDC hdc, HRGN hrgn )
531 {
532     HBRUSH prevBrush;
533     INT prevROP;
534     BOOL retval;
535     DC *dc = DC_GetDCUpdate( hdc );
536     if (!dc) return FALSE;
537
538     if(dc->funcs->pInvertRgn)
539         retval = dc->funcs->pInvertRgn( dc->physDev, hrgn );
540     else
541     {
542     prevBrush = SelectObject( hdc, GetStockObject(BLACK_BRUSH) );
543     prevROP = SetROP2( hdc, R2_NOT );
544     retval = PaintRgn( hdc, hrgn );
545     SelectObject( hdc, prevBrush );
546     SetROP2( hdc, prevROP );
547     }
548     GDI_ReleaseObj( hdc );
549     return retval;
550 }
551
552
553 /**********************************************************************
554  *          Polyline   (GDI32.@)
555  */
556 BOOL WINAPI Polyline( HDC hdc, const POINT* pt, INT count )
557 {
558     BOOL ret = FALSE;
559     DC * dc = DC_GetDCUpdate( hdc );
560     if (dc)
561     {
562         if (PATH_IsPathOpen(dc->path)) ret = PATH_Polyline(dc, pt, count);
563         else if (dc->funcs->pPolyline) ret = dc->funcs->pPolyline(dc->physDev,pt,count);
564         GDI_ReleaseObj( hdc );
565     }
566     return ret;
567 }
568
569 /**********************************************************************
570  *          PolylineTo   (GDI32.@)
571  */
572 BOOL WINAPI PolylineTo( HDC hdc, const POINT* pt, DWORD cCount )
573 {
574     DC * dc = DC_GetDCUpdate( hdc );
575     BOOL ret = FALSE;
576
577     if(!dc) return FALSE;
578
579     if(PATH_IsPathOpen(dc->path))
580         ret = PATH_PolylineTo(dc, pt, cCount);
581
582     else if(dc->funcs->pPolylineTo)
583         ret = dc->funcs->pPolylineTo(dc->physDev, pt, cCount);
584
585     else { /* do it using Polyline */
586         POINT *pts = HeapAlloc( GetProcessHeap(), 0,
587                                 sizeof(POINT) * (cCount + 1) );
588         if (pts)
589         {
590         pts[0].x = dc->CursPosX;
591         pts[0].y = dc->CursPosY;
592         memcpy( pts + 1, pt, sizeof(POINT) * cCount );
593         ret = Polyline( hdc, pts, cCount + 1 );
594         HeapFree( GetProcessHeap(), 0, pts );
595     }
596     }
597     if(ret) {
598         dc->CursPosX = pt[cCount-1].x;
599         dc->CursPosY = pt[cCount-1].y;
600     }
601     GDI_ReleaseObj( hdc );
602     return ret;
603 }
604
605
606 /**********************************************************************
607  *          Polygon  (GDI32.@)
608  */
609 BOOL WINAPI Polygon( HDC hdc, const POINT* pt, INT count )
610 {
611     BOOL ret = FALSE;
612     DC * dc = DC_GetDCUpdate( hdc );
613     if (dc)
614     {
615         if (PATH_IsPathOpen(dc->path)) ret = PATH_Polygon(dc, pt, count);
616         else if (dc->funcs->pPolygon) ret = dc->funcs->pPolygon(dc->physDev,pt,count);
617         GDI_ReleaseObj( hdc );
618     }
619     return ret;
620 }
621
622
623 /**********************************************************************
624  *          PolyPolygon  (GDI32.@)
625  */
626 BOOL WINAPI PolyPolygon( HDC hdc, const POINT* pt, const INT* counts,
627                              UINT polygons )
628 {
629     BOOL ret = FALSE;
630     DC * dc = DC_GetDCUpdate( hdc );
631     if (dc)
632     {
633         if (PATH_IsPathOpen(dc->path)) ret = PATH_PolyPolygon(dc, pt, counts, polygons);
634         else if (dc->funcs->pPolyPolygon) ret = dc->funcs->pPolyPolygon(dc->physDev,pt,counts,polygons);
635         GDI_ReleaseObj( hdc );
636     }
637     return ret;
638 }
639
640 /**********************************************************************
641  *          PolyPolyline  (GDI32.@)
642  */
643 BOOL WINAPI PolyPolyline( HDC hdc, const POINT* pt, const DWORD* counts,
644                             DWORD polylines )
645 {
646     BOOL ret = FALSE;
647     DC * dc = DC_GetDCUpdate( hdc );
648     if (dc)
649     {
650         if (PATH_IsPathOpen(dc->path)) ret = PATH_PolyPolyline(dc, pt, counts, polylines);
651         else if (dc->funcs->pPolyPolyline) ret = dc->funcs->pPolyPolyline(dc->physDev,pt,counts,polylines);
652         GDI_ReleaseObj( hdc );
653     }
654     return ret;
655 }
656
657 /**********************************************************************
658  *          ExtFloodFill   (GDI32.@)
659  */
660 BOOL WINAPI ExtFloodFill( HDC hdc, INT x, INT y, COLORREF color,
661                               UINT fillType )
662 {
663     BOOL ret = FALSE;
664     DC * dc = DC_GetDCUpdate( hdc );
665     if (dc)
666     {
667         if (dc->funcs->pExtFloodFill) ret = dc->funcs->pExtFloodFill(dc->physDev,x,y,color,fillType);
668         GDI_ReleaseObj( hdc );
669     }
670     return ret;
671 }
672
673
674 /**********************************************************************
675  *          FloodFill   (GDI32.@)
676  */
677 BOOL WINAPI FloodFill( HDC hdc, INT x, INT y, COLORREF color )
678 {
679     return ExtFloodFill( hdc, x, y, color, FLOODFILLBORDER );
680 }
681
682
683 /******************************************************************************
684  * PolyBezier [GDI32.@]
685  * Draws one or more Bezier curves
686  *
687  * PARAMS
688  *    hDc     [I] Handle to device context
689  *    lppt    [I] Pointer to endpoints and control points
690  *    cPoints [I] Count of endpoints and control points
691  *
692  * RETURNS STD
693  */
694 BOOL WINAPI PolyBezier( HDC hdc, const POINT* lppt, DWORD cPoints )
695 {
696     BOOL ret = FALSE;
697     DC * dc;
698
699     /* cPoints must be 3 * n + 1 (where n>=1) */
700     if (cPoints == 1 || (cPoints % 3) != 1) return FALSE;
701
702     dc = DC_GetDCUpdate( hdc );
703     if(!dc) return FALSE;
704
705     if(PATH_IsPathOpen(dc->path))
706         ret = PATH_PolyBezier(dc, lppt, cPoints);
707     else if (dc->funcs->pPolyBezier)
708         ret = dc->funcs->pPolyBezier(dc->physDev, lppt, cPoints);
709     else  /* We'll convert it into line segments and draw them using Polyline */
710     {
711         POINT *Pts;
712         INT nOut;
713
714         if ((Pts = GDI_Bezier( lppt, cPoints, &nOut )))
715         {
716             TRACE("Pts = %p, no = %d\n", Pts, nOut);
717             ret = Polyline( dc->hSelf, Pts, nOut );
718             HeapFree( GetProcessHeap(), 0, Pts );
719         }
720     }
721
722     GDI_ReleaseObj( hdc );
723     return ret;
724 }
725
726 /******************************************************************************
727  * PolyBezierTo [GDI32.@]
728  * Draws one or more Bezier curves
729  *
730  * PARAMS
731  *    hDc     [I] Handle to device context
732  *    lppt    [I] Pointer to endpoints and control points
733  *    cPoints [I] Count of endpoints and control points
734  *
735  * RETURNS STD
736  */
737 BOOL WINAPI PolyBezierTo( HDC hdc, const POINT* lppt, DWORD cPoints )
738 {
739     DC * dc = DC_GetDCUpdate( hdc );
740     BOOL ret;
741
742     if(!dc) return FALSE;
743
744     if(PATH_IsPathOpen(dc->path))
745         ret = PATH_PolyBezierTo(dc, lppt, cPoints);
746     else if(dc->funcs->pPolyBezierTo)
747         ret = dc->funcs->pPolyBezierTo(dc->physDev, lppt, cPoints);
748     else { /* We'll do it using PolyBezier */
749         POINT *pt;
750         pt = HeapAlloc( GetProcessHeap(), 0, sizeof(POINT) * (cPoints + 1) );
751         if(!pt) return FALSE;
752         pt[0].x = dc->CursPosX;
753         pt[0].y = dc->CursPosY;
754         memcpy(pt + 1, lppt, sizeof(POINT) * cPoints);
755         ret = PolyBezier(dc->hSelf, pt, cPoints+1);
756         HeapFree( GetProcessHeap(), 0, pt );
757     }
758     if(ret) {
759         dc->CursPosX = lppt[cPoints-1].x;
760         dc->CursPosY = lppt[cPoints-1].y;
761     }
762     GDI_ReleaseObj( hdc );
763     return ret;
764 }
765
766 /***********************************************************************
767  *      AngleArc (GDI32.@)
768  */
769 BOOL WINAPI AngleArc(HDC hdc, INT x, INT y, DWORD dwRadius, FLOAT eStartAngle, FLOAT eSweepAngle)
770 {
771     INT x1,y1,x2,y2, arcdir;
772     BOOL result;
773     DC *dc;
774
775     if( (signed int)dwRadius < 0 )
776         return FALSE;
777
778     dc = DC_GetDCUpdate( hdc );
779     if(!dc) return FALSE;
780
781     if(dc->funcs->pAngleArc)
782     {
783         result = dc->funcs->pAngleArc( dc->physDev, x, y, dwRadius, eStartAngle, eSweepAngle );
784
785         GDI_ReleaseObj( hdc );
786         return result;
787     }
788     GDI_ReleaseObj( hdc );
789
790     /* AngleArc always works counterclockwise */
791     arcdir = GetArcDirection( hdc );
792     SetArcDirection( hdc, AD_COUNTERCLOCKWISE );
793
794     x1 = x + cos(eStartAngle*M_PI/180) * dwRadius;
795     y1 = y - sin(eStartAngle*M_PI/180) * dwRadius;
796     x2 = x + cos((eStartAngle+eSweepAngle)*M_PI/180) * dwRadius;
797     y2 = x - sin((eStartAngle+eSweepAngle)*M_PI/180) * dwRadius;
798
799     LineTo( hdc, x1, y1 );
800     if( eSweepAngle >= 0 )
801         result = Arc( hdc, x-dwRadius, y-dwRadius, x+dwRadius, y+dwRadius,
802                       x1, y1, x2, y2 );
803     else
804         result = Arc( hdc, x-dwRadius, y-dwRadius, x+dwRadius, y+dwRadius,
805                       x2, y2, x1, y1 );
806
807     if( result ) MoveToEx( hdc, x2, y2, NULL );
808     SetArcDirection( hdc, arcdir );
809     return result;
810 }
811
812 /***********************************************************************
813  *      PolyDraw (GDI32.@)
814  */
815 BOOL WINAPI PolyDraw(HDC hdc, const POINT *lppt, const BYTE *lpbTypes,
816                        DWORD cCount)
817 {
818     DC *dc;
819     BOOL result;
820     POINT lastmove;
821     int i;
822
823     dc = DC_GetDCUpdate( hdc );
824     if(!dc) return FALSE;
825
826     if(dc->funcs->pPolyDraw)
827     {
828         result = dc->funcs->pPolyDraw( dc->physDev, lppt, lpbTypes, cCount );
829         GDI_ReleaseObj( hdc );
830         return result;
831     }
832     GDI_ReleaseObj( hdc );
833
834     /* check for each bezierto if there are two more points */
835     for( i = 0; i < cCount; i++ )
836         if( lpbTypes[i] != PT_MOVETO &&
837             lpbTypes[i] & PT_BEZIERTO )
838         {
839             if( cCount < i+3 )
840                 return FALSE;
841             else
842                 i += 2;
843         }
844
845     /* if no moveto occurs, we will close the figure here */
846     lastmove.x = dc->CursPosX;
847     lastmove.y = dc->CursPosY;
848
849     /* now let's draw */
850     for( i = 0; i < cCount; i++ )
851     {
852         if( lpbTypes[i] == PT_MOVETO )
853         {
854             MoveToEx( hdc, lppt[i].x, lppt[i].y, NULL );
855             lastmove.x = dc->CursPosX;
856             lastmove.y = dc->CursPosY;
857         }
858         else if( lpbTypes[i] & PT_LINETO )
859             LineTo( hdc, lppt[i].x, lppt[i].y );
860         else if( lpbTypes[i] & PT_BEZIERTO )
861         {
862             PolyBezierTo( hdc, &lppt[i], 3 );
863             i += 2;
864         }
865         else
866             return FALSE;
867
868         if( lpbTypes[i] & PT_CLOSEFIGURE )
869         {
870             if( PATH_IsPathOpen( dc->path ) )
871                 CloseFigure( hdc );
872             else
873                 LineTo( hdc, lastmove.x, lastmove.y );
874         }
875     }
876
877     return TRUE;
878 }
879
880 /******************************************************************
881  *
882  *   *Very* simple bezier drawing code,
883  *
884  *   It uses a recursive algorithm to divide the curve in a series
885  *   of straight line segements. Not ideal but for me sufficient.
886  *   If you are in need for something better look for some incremental
887  *   algorithm.
888  *
889  *   7 July 1998 Rein Klazes
890  */
891
892  /*
893   * some macro definitions for bezier drawing
894   *
895   * to avoid truncation errors the coordinates are
896   * shifted upwards. When used in drawing they are
897   * shifted down again, including correct rounding
898   * and avoiding floating point arithmetic
899   * 4 bits should allow 27 bits coordinates which I saw
900   * somewhere in the win32 doc's
901   *
902   */
903
904 #define BEZIERSHIFTBITS 4
905 #define BEZIERSHIFTUP(x)    ((x)<<BEZIERSHIFTBITS)
906 #define BEZIERPIXEL        BEZIERSHIFTUP(1)
907 #define BEZIERSHIFTDOWN(x)  (((x)+(1<<(BEZIERSHIFTBITS-1)))>>BEZIERSHIFTBITS)
908 /* maximum depth of recursion */
909 #define BEZIERMAXDEPTH  8
910
911 /* size of array to store points on */
912 /* enough for one curve */
913 #define BEZIER_INITBUFSIZE    (150)
914
915 /* calculate Bezier average, in this case the middle
916  * correctly rounded...
917  * */
918
919 #define BEZIERMIDDLE(Mid, P1, P2) \
920     (Mid).x=((P1).x+(P2).x + 1)/2;\
921     (Mid).y=((P1).y+(P2).y + 1)/2;
922
923 /**********************************************************
924 * BezierCheck helper function to check
925 * that recursion can be terminated
926 *       Points[0] and Points[3] are begin and endpoint
927 *       Points[1] and Points[2] are control points
928 *       level is the recursion depth
929 *       returns true if the recusion can be terminated
930 */
931 static BOOL BezierCheck( int level, POINT *Points)
932 {
933     INT dx, dy;
934     dx=Points[3].x-Points[0].x;
935     dy=Points[3].y-Points[0].y;
936     if(abs(dy)<=abs(dx)){/* shallow line */
937         /* check that control points are between begin and end */
938         if(Points[1].x < Points[0].x){
939             if(Points[1].x < Points[3].x)
940                 return FALSE;
941         }else
942             if(Points[1].x > Points[3].x)
943                 return FALSE;
944         if(Points[2].x < Points[0].x){
945             if(Points[2].x < Points[3].x)
946                 return FALSE;
947         }else
948             if(Points[2].x > Points[3].x)
949                 return FALSE;
950         dx=BEZIERSHIFTDOWN(dx);
951         if(!dx) return TRUE;
952         if(abs(Points[1].y-Points[0].y-(dy/dx)*
953                 BEZIERSHIFTDOWN(Points[1].x-Points[0].x)) > BEZIERPIXEL ||
954            abs(Points[2].y-Points[0].y-(dy/dx)*
955                    BEZIERSHIFTDOWN(Points[2].x-Points[0].x)) > BEZIERPIXEL )
956             return FALSE;
957         else
958             return TRUE;
959     }else{ /* steep line */
960         /* check that control points are between begin and end */
961         if(Points[1].y < Points[0].y){
962             if(Points[1].y < Points[3].y)
963                 return FALSE;
964         }else
965             if(Points[1].y > Points[3].y)
966                 return FALSE;
967         if(Points[2].y < Points[0].y){
968             if(Points[2].y < Points[3].y)
969                 return FALSE;
970         }else
971             if(Points[2].y > Points[3].y)
972                 return FALSE;
973         dy=BEZIERSHIFTDOWN(dy);
974         if(!dy) return TRUE;
975         if(abs(Points[1].x-Points[0].x-(dx/dy)*
976                 BEZIERSHIFTDOWN(Points[1].y-Points[0].y)) > BEZIERPIXEL ||
977            abs(Points[2].x-Points[0].x-(dx/dy)*
978                    BEZIERSHIFTDOWN(Points[2].y-Points[0].y)) > BEZIERPIXEL )
979             return FALSE;
980         else
981             return TRUE;
982     }
983 }
984
985 /* Helper for GDI_Bezier.
986  * Just handles one Bezier, so Points should point to four POINTs
987  */
988 static void GDI_InternalBezier( POINT *Points, POINT **PtsOut, INT *dwOut,
989                                 INT *nPtsOut, INT level )
990 {
991     if(*nPtsOut == *dwOut) {
992         *dwOut *= 2;
993         *PtsOut = HeapReAlloc( GetProcessHeap(), 0, *PtsOut,
994                                *dwOut * sizeof(POINT) );
995     }
996
997     if(!level || BezierCheck(level, Points)) {
998         if(*nPtsOut == 0) {
999             (*PtsOut)[0].x = BEZIERSHIFTDOWN(Points[0].x);
1000             (*PtsOut)[0].y = BEZIERSHIFTDOWN(Points[0].y);
1001             *nPtsOut = 1;
1002         }
1003         (*PtsOut)[*nPtsOut].x = BEZIERSHIFTDOWN(Points[3].x);
1004         (*PtsOut)[*nPtsOut].y = BEZIERSHIFTDOWN(Points[3].y);
1005         (*nPtsOut) ++;
1006     } else {
1007         POINT Points2[4]; /* for the second recursive call */
1008         Points2[3]=Points[3];
1009         BEZIERMIDDLE(Points2[2], Points[2], Points[3]);
1010         BEZIERMIDDLE(Points2[0], Points[1], Points[2]);
1011         BEZIERMIDDLE(Points2[1],Points2[0],Points2[2]);
1012
1013         BEZIERMIDDLE(Points[1], Points[0],  Points[1]);
1014         BEZIERMIDDLE(Points[2], Points[1], Points2[0]);
1015         BEZIERMIDDLE(Points[3], Points[2], Points2[1]);
1016
1017         Points2[0]=Points[3];
1018
1019         /* do the two halves */
1020         GDI_InternalBezier(Points, PtsOut, dwOut, nPtsOut, level-1);
1021         GDI_InternalBezier(Points2, PtsOut, dwOut, nPtsOut, level-1);
1022     }
1023 }
1024
1025
1026
1027 /***********************************************************************
1028  *           GDI_Bezier   [INTERNAL]
1029  *   Calculate line segments that approximate -what microsoft calls- a bezier
1030  *   curve.
1031  *   The routine recursively divides the curve in two parts until a straight
1032  *   line can be drawn
1033  *
1034  *  PARAMS
1035  *
1036  *  Points  [I] Ptr to count POINTs which are the end and control points
1037  *              of the set of Bezier curves to flatten.
1038  *  count   [I] Number of Points.  Must be 3n+1.
1039  *  nPtsOut [O] Will contain no of points that have been produced (i.e. no. of
1040  *              lines+1).
1041  *
1042  *  RETURNS
1043  *
1044  *  Ptr to an array of POINTs that contain the lines that approximinate the
1045  *  Beziers.  The array is allocated on the process heap and it is the caller's
1046  *  responsibility to HeapFree it. [this is not a particularly nice interface
1047  *  but since we can't know in advance how many points will generate, the
1048  *  alternative would be to call the function twice, once to determine the size
1049  *  and a second time to do the work - I decided this was too much of a pain].
1050  */
1051 POINT *GDI_Bezier( const POINT *Points, INT count, INT *nPtsOut )
1052 {
1053     POINT *out;
1054     INT Bezier, dwOut = BEZIER_INITBUFSIZE, i;
1055
1056     if((count - 1) % 3 != 0) {
1057         ERR("Invalid no. of points\n");
1058         return NULL;
1059     }
1060     *nPtsOut = 0;
1061     out = HeapAlloc( GetProcessHeap(), 0, dwOut * sizeof(POINT));
1062     for(Bezier = 0; Bezier < (count-1)/3; Bezier++) {
1063         POINT ptBuf[4];
1064         memcpy(ptBuf, Points + Bezier * 3, sizeof(POINT) * 4);
1065         for(i = 0; i < 4; i++) {
1066             ptBuf[i].x = BEZIERSHIFTUP(ptBuf[i].x);
1067             ptBuf[i].y = BEZIERSHIFTUP(ptBuf[i].y);
1068         }
1069         GDI_InternalBezier( ptBuf, &out, &dwOut, nPtsOut, BEZIERMAXDEPTH );
1070     }
1071     TRACE("Produced %d points\n", *nPtsOut);
1072     return out;
1073 }
1074
1075 /******************************************************************************
1076  *           GradientFill   (GDI32.@)
1077  *
1078  *  FIXME: we don't support the Alpha channel properly
1079  */
1080 BOOL WINAPI GdiGradientFill( HDC hdc, TRIVERTEX *vert_array, ULONG nvert,
1081                           void * grad_array, ULONG ngrad, ULONG mode )
1082 {
1083   int i;
1084
1085   TRACE("vert_array:0x%08lx nvert:%ld grad_array:0x%08lx ngrad:%ld\n",
1086         (long)vert_array, nvert, (long)grad_array, ngrad);
1087
1088   switch(mode) 
1089     {
1090     case GRADIENT_FILL_RECT_H:
1091       for(i = 0; i < ngrad; i++) 
1092         {
1093           GRADIENT_RECT *rect = ((GRADIENT_RECT *)grad_array) + i;
1094           TRIVERTEX *v1 = vert_array + rect->UpperLeft;
1095           TRIVERTEX *v2 = vert_array + rect->LowerRight;
1096           int y1 = v1->y < v2->y ? v1->y : v2->y;
1097           int y2 = v2->y > v1->y ? v2->y : v1->y;
1098           int x, dx;
1099           if (v1->x > v2->x)
1100             {
1101               TRIVERTEX *t = v2;
1102               v2 = v1;
1103               v1 = t;
1104             }
1105           dx = v2->x - v1->x;
1106           for (x = 0; x < dx; x++)
1107             {
1108               POINT pts[2];
1109               HPEN hPen, hOldPen;
1110               
1111               hPen = CreatePen( PS_SOLID, 1, RGB(
1112                   (v1->Red   * (dx - x) + v2->Red   * x) / dx >> 8,
1113                   (v1->Green * (dx - x) + v2->Green * x) / dx >> 8,
1114                   (v1->Blue  * (dx - x) + v2->Blue  * x) / dx >> 8));
1115               hOldPen = SelectObject( hdc, hPen );
1116               pts[0].x = v1->x + x;
1117               pts[0].y = y1;
1118               pts[1].x = v1->x + x;
1119               pts[1].y = y2;
1120               Polyline( hdc, &pts[0], 2 );
1121               DeleteObject( SelectObject(hdc, hOldPen ) );
1122             }
1123         }
1124       break;
1125     case GRADIENT_FILL_RECT_V:
1126       for(i = 0; i < ngrad; i++) 
1127         {
1128           GRADIENT_RECT *rect = ((GRADIENT_RECT *)grad_array) + i;
1129           TRIVERTEX *v1 = vert_array + rect->UpperLeft;
1130           TRIVERTEX *v2 = vert_array + rect->LowerRight;
1131           int x1 = v1->x < v2->x ? v1->x : v2->x;
1132           int x2 = v2->x > v1->x ? v2->x : v1->x;
1133           int y, dy;
1134           if (v1->y > v2->y)
1135             {
1136               TRIVERTEX *t = v2;
1137               v2 = v1;
1138               v1 = t;
1139             }
1140           dy = v2->y - v1->y;
1141           for (y = 0; y < dy; y++)
1142             {
1143               POINT pts[2];
1144               HPEN hPen, hOldPen;
1145               
1146               hPen = CreatePen( PS_SOLID, 1, RGB(
1147                   (v1->Red   * (dy - y) + v2->Red   * y) / dy >> 8,
1148                   (v1->Green * (dy - y) + v2->Green * y) / dy >> 8,
1149                   (v1->Blue  * (dy - y) + v2->Blue  * y) / dy >> 8));
1150               hOldPen = SelectObject( hdc, hPen );
1151               pts[0].x = x1;
1152               pts[0].y = v1->y + y;
1153               pts[1].x = x2;
1154               pts[1].y = v1->y + y;
1155               Polyline( hdc, &pts[0], 2 );
1156               DeleteObject( SelectObject(hdc, hOldPen ) );
1157             }
1158         }
1159       break;
1160     case GRADIENT_FILL_TRIANGLE:
1161       for (i = 0; i < ngrad; i++)  
1162         {
1163           GRADIENT_TRIANGLE *tri = ((GRADIENT_TRIANGLE *)grad_array) + i;
1164           TRIVERTEX *v1 = vert_array + tri->Vertex1;
1165           TRIVERTEX *v2 = vert_array + tri->Vertex2;
1166           TRIVERTEX *v3 = vert_array + tri->Vertex3;
1167           int y, dy;
1168           
1169           if (v1->y > v2->y)
1170             { TRIVERTEX *t = v1; v1 = v2; v2 = t; }
1171           if (v2->y > v3->y)
1172             {
1173               TRIVERTEX *t = v2; v2 = v3; v3 = t;
1174               if (v1->y > v2->y)
1175                 { t = v1; v1 = v2; v2 = t; }
1176             }
1177           /* v1->y <= v2->y <= v3->y */
1178
1179           dy = v3->y - v1->y;
1180           for (y = 0; y < dy; y++)
1181             {
1182               /* v1->y <= y < v3->y */
1183               TRIVERTEX *v = y < (v2->y - v1->y) ? v1 : v3;
1184               /* (v->y <= y < v2->y) || (v2->y <= y < v->y) */
1185               int dy2 = v2->y - v->y;
1186               int y2 = y + v1->y - v->y;
1187
1188               int x1 = (v3->x     * y  + v1->x     * (dy  - y )) / dy;
1189               int x2 = (v2->x     * y2 + v-> x     * (dy2 - y2)) / dy2;
1190               int r1 = (v3->Red   * y  + v1->Red   * (dy  - y )) / dy;
1191               int r2 = (v2->Red   * y2 + v-> Red   * (dy2 - y2)) / dy2;
1192               int g1 = (v3->Green * y  + v1->Green * (dy  - y )) / dy;
1193               int g2 = (v2->Green * y2 + v-> Green * (dy2 - y2)) / dy2;
1194               int b1 = (v3->Blue  * y  + v1->Blue  * (dy  - y )) / dy;
1195               int b2 = (v2->Blue  * y2 + v-> Blue  * (dy2 - y2)) / dy2;
1196                
1197               int x;
1198               if (x1 < x2)
1199                 {
1200                   int dx = x2 - x1;
1201                   for (x = 0; x < dx; x++)
1202                     SetPixel (hdc, x + x1, y + v1->y, RGB(
1203                       (r1 * (dx - x) + r2 * x) / dx >> 8,
1204                       (g1 * (dx - x) + g2 * x) / dx >> 8,
1205                       (b1 * (dx - x) + b2 * x) / dx >> 8));
1206                 }
1207               else
1208                 {
1209                   int dx = x1 - x2;
1210                   for (x = 0; x < dx; x++)
1211                     SetPixel (hdc, x + x2, y + v1->y, RGB(
1212                       (r2 * (dx - x) + r1 * x) / dx >> 8,
1213                       (g2 * (dx - x) + g1 * x) / dx >> 8,
1214                       (b2 * (dx - x) + b1 * x) / dx >> 8));
1215                 }
1216             }
1217         }
1218       break;
1219     default:
1220       return FALSE;
1221   }
1222
1223   return TRUE;
1224 }