Release the OpenGL context when the device is released.
[wine/testsucceed.git] / dlls / gdi / painting.c
blob61643c1945acb16bdb072845895fccc7b189794a
1 /*
2 * GDI drawing functions.
4 * Copyright 1993, 1994 Alexandre Julliard
5 * Copyright 1997 Bertho A. Stultiens
6 * 1999 Huw D M Davies
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.
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.
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
23 #include "config.h"
24 #include "wine/port.h"
26 #include <stdarg.h>
27 #include <string.h>
28 #include <stdlib.h>
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"
38 WINE_DEFAULT_DEBUG_CHANNEL(gdi);
41 /***********************************************************************
42 * LineTo (GDI32.@)
44 BOOL WINAPI LineTo( HDC hdc, INT x, INT y )
46 DC * dc = DC_GetDCUpdate( hdc );
47 BOOL ret;
49 if(!dc) return FALSE;
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;
59 GDI_ReleaseObj( hdc );
60 return ret;
64 /***********************************************************************
65 * MoveToEx (GDI32.@)
67 BOOL WINAPI MoveToEx( HDC hdc, INT x, INT y, LPPOINT pt )
69 BOOL ret = TRUE;
70 DC * dc = DC_GetDCPtr( hdc );
72 if(!dc) return FALSE;
74 if(pt) {
75 pt->x = dc->CursPosX;
76 pt->y = dc->CursPosY;
78 dc->CursPosX = x;
79 dc->CursPosY = y;
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;
88 /***********************************************************************
89 * Arc (GDI32.@)
91 BOOL WINAPI Arc( HDC hdc, INT left, INT top, INT right,
92 INT bottom, INT xstart, INT ystart,
93 INT xend, INT yend )
95 BOOL ret = FALSE;
96 DC * dc = DC_GetDCUpdate( hdc );
97 if (dc)
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 );
105 return ret;
108 /***********************************************************************
109 * ArcTo (GDI32.@)
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 )
117 BOOL result;
118 DC * dc = DC_GetDCUpdate( hdc );
119 if(!dc) return FALSE;
121 if(dc->funcs->pArcTo)
123 result = dc->funcs->pArcTo( dc->physDev, left, top, right, bottom,
124 xstart, ystart, xend, yend );
125 GDI_ReleaseObj( hdc );
126 return result;
128 GDI_ReleaseObj( hdc );
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.
134 LineTo(hdc, xstart, ystart);
136 * Then the arc is drawn.
138 result = Arc(hdc, left, top, right, bottom, xstart, ystart, xend, yend);
140 * If no error occurred, the current position is moved to the ending
141 * point of the arc.
143 if (result) MoveToEx(hdc, xend, yend, NULL);
144 return result;
148 /***********************************************************************
149 * Pie (GDI32.@)
151 BOOL WINAPI Pie( HDC hdc, INT left, INT top,
152 INT right, INT bottom, INT xstart, INT ystart,
153 INT xend, INT yend )
155 BOOL ret = FALSE;
156 DC * dc = DC_GetDCUpdate( hdc );
157 if (!dc) return FALSE;
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);
164 GDI_ReleaseObj( hdc );
165 return ret;
169 /***********************************************************************
170 * Chord (GDI32.@)
172 BOOL WINAPI Chord( HDC hdc, INT left, INT top,
173 INT right, INT bottom, INT xstart, INT ystart,
174 INT xend, INT yend )
176 BOOL ret = FALSE;
177 DC * dc = DC_GetDCUpdate( hdc );
178 if (!dc) return FALSE;
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);
185 GDI_ReleaseObj( hdc );
186 return ret;
190 /***********************************************************************
191 * Ellipse (GDI32.@)
193 BOOL WINAPI Ellipse( HDC hdc, INT left, INT top,
194 INT right, INT bottom )
196 BOOL ret = FALSE;
197 DC * dc = DC_GetDCUpdate( hdc );
198 if (!dc) return FALSE;
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);
205 GDI_ReleaseObj( hdc );
206 return ret;
210 /***********************************************************************
211 * Rectangle (GDI32.@)
213 BOOL WINAPI Rectangle( HDC hdc, INT left, INT top,
214 INT right, INT bottom )
216 BOOL ret = FALSE;
217 DC * dc = DC_GetDCUpdate( hdc );
218 if (dc)
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 );
226 return ret;
230 /***********************************************************************
231 * RoundRect (GDI32.@)
233 BOOL WINAPI RoundRect( HDC hdc, INT left, INT top, INT right,
234 INT bottom, INT ell_width, INT ell_height )
236 BOOL ret = FALSE;
237 DC *dc = DC_GetDCUpdate( hdc );
239 if (dc)
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 );
247 return ret;
250 /***********************************************************************
251 * SetPixel (GDI32.@)
253 COLORREF WINAPI SetPixel( HDC hdc, INT x, INT y, COLORREF color )
255 COLORREF ret = 0;
256 DC * dc = DC_GetDCUpdate( hdc );
257 if (dc)
259 if (dc->funcs->pSetPixel) ret = dc->funcs->pSetPixel(dc->physDev,x,y,color);
260 GDI_ReleaseObj( hdc );
262 return ret;
265 /***********************************************************************
266 * SetPixelV (GDI32.@)
268 BOOL WINAPI SetPixelV( HDC hdc, INT x, INT y, COLORREF color )
270 BOOL ret = FALSE;
271 DC * dc = DC_GetDCUpdate( hdc );
272 if (dc)
274 if (dc->funcs->pSetPixel)
276 dc->funcs->pSetPixel(dc->physDev,x,y,color);
277 ret = TRUE;
279 GDI_ReleaseObj( hdc );
281 return ret;
284 /***********************************************************************
285 * GetPixel (GDI32.@)
287 COLORREF WINAPI GetPixel( HDC hdc, INT x, INT y )
289 COLORREF ret = CLR_INVALID;
290 DC * dc = DC_GetDCUpdate( hdc );
292 if (dc)
294 /* FIXME: should this be in the graphics driver? */
295 if (PtVisible( hdc, x, y ))
297 if (dc->funcs->pGetPixel) ret = dc->funcs->pGetPixel(dc->physDev,x,y);
299 GDI_ReleaseObj( hdc );
301 return ret;
305 /******************************************************************************
306 * ChoosePixelFormat [GDI32.@]
307 * Matches a pixel format to given format
309 * PARAMS
310 * hdc [I] Device context to search for best pixel match
311 * ppfd [I] Pixel format for which a match is sought
313 * RETURNS
314 * Success: Pixel format index closest to given format
315 * Failure: 0
317 INT WINAPI ChoosePixelFormat( HDC hdc, const PIXELFORMATDESCRIPTOR* ppfd )
319 INT ret = 0;
320 DC * dc = DC_GetDCPtr( hdc );
322 TRACE("(%p,%p)\n",hdc,ppfd);
324 if (!dc) return 0;
326 if (!dc->funcs->pChoosePixelFormat) FIXME(" :stub\n");
327 else ret = dc->funcs->pChoosePixelFormat(dc->physDev,ppfd);
329 GDI_ReleaseObj( hdc );
330 return ret;
334 /******************************************************************************
335 * SetPixelFormat [GDI32.@]
336 * Sets pixel format of device context
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
343 * RETURNS STD
345 BOOL WINAPI SetPixelFormat( HDC hdc, INT iPixelFormat,
346 const PIXELFORMATDESCRIPTOR *ppfd)
348 INT bRet = FALSE;
349 DC * dc = DC_GetDCPtr( hdc );
351 TRACE("(%p,%d,%p)\n",hdc,iPixelFormat,ppfd);
353 if (!dc) return 0;
355 if (!dc->funcs->pSetPixelFormat) FIXME(" :stub\n");
356 else bRet = dc->funcs->pSetPixelFormat(dc->physDev,iPixelFormat,ppfd);
358 GDI_ReleaseObj( hdc );
359 return bRet;
363 /******************************************************************************
364 * GetPixelFormat [GDI32.@]
365 * Gets index of pixel format of DC
367 * PARAMETERS
368 * hdc [I] Device context whose pixel format index is sought
370 * RETURNS
371 * Success: Currently selected pixel format
372 * Failure: 0
374 INT WINAPI GetPixelFormat( HDC hdc )
376 INT ret = 0;
377 DC * dc = DC_GetDCPtr( hdc );
379 TRACE("(%p)\n",hdc);
381 if (!dc) return 0;
383 if (!dc->funcs->pGetPixelFormat) FIXME(" :stub\n");
384 else ret = dc->funcs->pGetPixelFormat(dc->physDev);
386 GDI_ReleaseObj( hdc );
387 return ret;
391 /******************************************************************************
392 * DescribePixelFormat [GDI32.@]
393 * Gets info about pixel format from DC
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
401 * RETURNS
402 * Success: Maximum pixel format index of the device context
403 * Failure: 0
405 INT WINAPI DescribePixelFormat( HDC hdc, INT iPixelFormat, UINT nBytes,
406 LPPIXELFORMATDESCRIPTOR ppfd )
408 INT ret = 0;
409 DC * dc = DC_GetDCPtr( hdc );
411 TRACE("(%p,%d,%d,%p): stub\n",hdc,iPixelFormat,nBytes,ppfd);
413 if (!dc) return 0;
415 if (!dc->funcs->pDescribePixelFormat)
417 FIXME(" :stub\n");
418 ppfd->nSize = nBytes;
419 ppfd->nVersion = 1;
420 ret = 3;
422 else ret = dc->funcs->pDescribePixelFormat(dc->physDev,iPixelFormat,nBytes,ppfd);
424 GDI_ReleaseObj( hdc );
425 return ret;
429 /******************************************************************************
430 * SwapBuffers [GDI32.@]
431 * Exchanges front and back buffers of window
433 * PARAMS
434 * hdc [I] Device context whose buffers get swapped
436 * RETURNS STD
438 BOOL WINAPI SwapBuffers( HDC hdc )
440 INT bRet = FALSE;
441 DC * dc = DC_GetDCPtr( hdc );
443 TRACE("(%p)\n",hdc);
445 if (!dc) return TRUE;
447 if (!dc->funcs->pSwapBuffers)
449 FIXME(" :stub\n");
450 bRet = TRUE;
452 else bRet = dc->funcs->pSwapBuffers(dc->physDev);
454 GDI_ReleaseObj( hdc );
455 return bRet;
459 /***********************************************************************
460 * PaintRgn (GDI32.@)
462 BOOL WINAPI PaintRgn( HDC hdc, HRGN hrgn )
464 BOOL ret = FALSE;
465 DC * dc = DC_GetDCUpdate( hdc );
466 if (dc)
468 if (dc->funcs->pPaintRgn) ret = dc->funcs->pPaintRgn(dc->physDev,hrgn);
469 GDI_ReleaseObj( hdc );
471 return ret;
475 /***********************************************************************
476 * FillRgn (GDI32.@)
478 BOOL WINAPI FillRgn( HDC hdc, HRGN hrgn, HBRUSH hbrush )
480 BOOL retval = FALSE;
481 HBRUSH prevBrush;
482 DC * dc = DC_GetDCUpdate( hdc );
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 )))
489 retval = PaintRgn( hdc, hrgn );
490 SelectObject( hdc, prevBrush );
492 GDI_ReleaseObj( hdc );
493 return retval;
497 /***********************************************************************
498 * FrameRgn (GDI32.@)
500 BOOL WINAPI FrameRgn( HDC hdc, HRGN hrgn, HBRUSH hbrush,
501 INT nWidth, INT nHeight )
503 BOOL ret = FALSE;
504 DC *dc = DC_GetDCUpdate( hdc );
506 if (!dc) return FALSE;
507 if(dc->funcs->pFrameRgn)
508 ret = dc->funcs->pFrameRgn( dc->physDev, hrgn, hbrush, nWidth, nHeight );
509 else
511 HRGN tmp = CreateRectRgn( 0, 0, 0, 0 );
512 if (tmp)
514 if (REGION_FrameRgn( tmp, hrgn, nWidth, nHeight ))
516 FillRgn( hdc, tmp, hbrush );
517 ret = TRUE;
519 DeleteObject( tmp );
522 GDI_ReleaseObj( hdc );
523 return ret;
527 /***********************************************************************
528 * InvertRgn (GDI32.@)
530 BOOL WINAPI InvertRgn( HDC hdc, HRGN hrgn )
532 HBRUSH prevBrush;
533 INT prevROP;
534 BOOL retval;
535 DC *dc = DC_GetDCUpdate( hdc );
536 if (!dc) return FALSE;
538 if(dc->funcs->pInvertRgn)
539 retval = dc->funcs->pInvertRgn( dc->physDev, hrgn );
540 else
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 );
548 GDI_ReleaseObj( hdc );
549 return retval;
553 /**********************************************************************
554 * Polyline (GDI32.@)
556 BOOL WINAPI Polyline( HDC hdc, const POINT* pt, INT count )
558 BOOL ret = FALSE;
559 DC * dc = DC_GetDCUpdate( hdc );
560 if (dc)
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 );
566 return ret;
569 /**********************************************************************
570 * PolylineTo (GDI32.@)
572 BOOL WINAPI PolylineTo( HDC hdc, const POINT* pt, DWORD cCount )
574 DC * dc = DC_GetDCUpdate( hdc );
575 BOOL ret = FALSE;
577 if(!dc) return FALSE;
579 if(PATH_IsPathOpen(dc->path))
580 ret = PATH_PolylineTo(dc, pt, cCount);
582 else if(dc->funcs->pPolylineTo)
583 ret = dc->funcs->pPolylineTo(dc->physDev, pt, cCount);
585 else { /* do it using Polyline */
586 POINT *pts = HeapAlloc( GetProcessHeap(), 0,
587 sizeof(POINT) * (cCount + 1) );
588 if (pts)
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 );
597 if(ret) {
598 dc->CursPosX = pt[cCount-1].x;
599 dc->CursPosY = pt[cCount-1].y;
601 GDI_ReleaseObj( hdc );
602 return ret;
606 /**********************************************************************
607 * Polygon (GDI32.@)
609 BOOL WINAPI Polygon( HDC hdc, const POINT* pt, INT count )
611 BOOL ret = FALSE;
612 DC * dc = DC_GetDCUpdate( hdc );
613 if (dc)
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 );
619 return ret;
623 /**********************************************************************
624 * PolyPolygon (GDI32.@)
626 BOOL WINAPI PolyPolygon( HDC hdc, const POINT* pt, const INT* counts,
627 UINT polygons )
629 BOOL ret = FALSE;
630 DC * dc = DC_GetDCUpdate( hdc );
631 if (dc)
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 );
637 return ret;
640 /**********************************************************************
641 * PolyPolyline (GDI32.@)
643 BOOL WINAPI PolyPolyline( HDC hdc, const POINT* pt, const DWORD* counts,
644 DWORD polylines )
646 BOOL ret = FALSE;
647 DC * dc = DC_GetDCUpdate( hdc );
648 if (dc)
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 );
654 return ret;
657 /**********************************************************************
658 * ExtFloodFill (GDI32.@)
660 BOOL WINAPI ExtFloodFill( HDC hdc, INT x, INT y, COLORREF color,
661 UINT fillType )
663 BOOL ret = FALSE;
664 DC * dc = DC_GetDCUpdate( hdc );
665 if (dc)
667 if (dc->funcs->pExtFloodFill) ret = dc->funcs->pExtFloodFill(dc->physDev,x,y,color,fillType);
668 GDI_ReleaseObj( hdc );
670 return ret;
674 /**********************************************************************
675 * FloodFill (GDI32.@)
677 BOOL WINAPI FloodFill( HDC hdc, INT x, INT y, COLORREF color )
679 return ExtFloodFill( hdc, x, y, color, FLOODFILLBORDER );
683 /******************************************************************************
684 * PolyBezier [GDI32.@]
685 * Draws one or more Bezier curves
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
692 * RETURNS STD
694 BOOL WINAPI PolyBezier( HDC hdc, const POINT* lppt, DWORD cPoints )
696 BOOL ret = FALSE;
697 DC * dc;
699 /* cPoints must be 3 * n + 1 (where n>=1) */
700 if (cPoints == 1 || (cPoints % 3) != 1) return FALSE;
702 dc = DC_GetDCUpdate( hdc );
703 if(!dc) return FALSE;
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 */
711 POINT *Pts;
712 INT nOut;
714 if ((Pts = GDI_Bezier( lppt, cPoints, &nOut )))
716 TRACE("Pts = %p, no = %d\n", Pts, nOut);
717 ret = Polyline( dc->hSelf, Pts, nOut );
718 HeapFree( GetProcessHeap(), 0, Pts );
722 GDI_ReleaseObj( hdc );
723 return ret;
726 /******************************************************************************
727 * PolyBezierTo [GDI32.@]
728 * Draws one or more Bezier curves
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
735 * RETURNS STD
737 BOOL WINAPI PolyBezierTo( HDC hdc, const POINT* lppt, DWORD cPoints )
739 DC * dc;
740 BOOL ret;
742 /* cbPoints must be 3 * n (where n>=1) */
743 if (!cPoints || (cPoints % 3) != 0) return FALSE;
745 dc = DC_GetDCUpdate( hdc );
746 if(!dc) return FALSE;
748 if(PATH_IsPathOpen(dc->path))
749 ret = PATH_PolyBezierTo(dc, lppt, cPoints);
750 else if(dc->funcs->pPolyBezierTo)
751 ret = dc->funcs->pPolyBezierTo(dc->physDev, lppt, cPoints);
752 else { /* We'll do it using PolyBezier */
753 POINT *pt;
754 pt = HeapAlloc( GetProcessHeap(), 0, sizeof(POINT) * (cPoints + 1) );
755 if(!pt) return FALSE;
756 pt[0].x = dc->CursPosX;
757 pt[0].y = dc->CursPosY;
758 memcpy(pt + 1, lppt, sizeof(POINT) * cPoints);
759 ret = PolyBezier(dc->hSelf, pt, cPoints+1);
760 HeapFree( GetProcessHeap(), 0, pt );
762 if(ret) {
763 dc->CursPosX = lppt[cPoints-1].x;
764 dc->CursPosY = lppt[cPoints-1].y;
766 GDI_ReleaseObj( hdc );
767 return ret;
770 /***********************************************************************
771 * AngleArc (GDI32.@)
773 BOOL WINAPI AngleArc(HDC hdc, INT x, INT y, DWORD dwRadius, FLOAT eStartAngle, FLOAT eSweepAngle)
775 INT x1,y1,x2,y2, arcdir;
776 BOOL result;
777 DC *dc;
779 if( (signed int)dwRadius < 0 )
780 return FALSE;
782 dc = DC_GetDCUpdate( hdc );
783 if(!dc) return FALSE;
785 if(dc->funcs->pAngleArc)
787 result = dc->funcs->pAngleArc( dc->physDev, x, y, dwRadius, eStartAngle, eSweepAngle );
789 GDI_ReleaseObj( hdc );
790 return result;
792 GDI_ReleaseObj( hdc );
794 /* AngleArc always works counterclockwise */
795 arcdir = GetArcDirection( hdc );
796 SetArcDirection( hdc, AD_COUNTERCLOCKWISE );
798 x1 = x + cos(eStartAngle*M_PI/180) * dwRadius;
799 y1 = y - sin(eStartAngle*M_PI/180) * dwRadius;
800 x2 = x + cos((eStartAngle+eSweepAngle)*M_PI/180) * dwRadius;
801 y2 = x - sin((eStartAngle+eSweepAngle)*M_PI/180) * dwRadius;
803 LineTo( hdc, x1, y1 );
804 if( eSweepAngle >= 0 )
805 result = Arc( hdc, x-dwRadius, y-dwRadius, x+dwRadius, y+dwRadius,
806 x1, y1, x2, y2 );
807 else
808 result = Arc( hdc, x-dwRadius, y-dwRadius, x+dwRadius, y+dwRadius,
809 x2, y2, x1, y1 );
811 if( result ) MoveToEx( hdc, x2, y2, NULL );
812 SetArcDirection( hdc, arcdir );
813 return result;
816 /***********************************************************************
817 * PolyDraw (GDI32.@)
819 BOOL WINAPI PolyDraw(HDC hdc, const POINT *lppt, const BYTE *lpbTypes,
820 DWORD cCount)
822 DC *dc;
823 BOOL result;
824 POINT lastmove;
825 unsigned int i;
827 dc = DC_GetDCUpdate( hdc );
828 if(!dc) return FALSE;
830 if(dc->funcs->pPolyDraw)
832 result = dc->funcs->pPolyDraw( dc->physDev, lppt, lpbTypes, cCount );
833 GDI_ReleaseObj( hdc );
834 return result;
836 GDI_ReleaseObj( hdc );
838 /* check for each bezierto if there are two more points */
839 for( i = 0; i < cCount; i++ )
840 if( lpbTypes[i] != PT_MOVETO &&
841 lpbTypes[i] & PT_BEZIERTO )
843 if( cCount < i+3 )
844 return FALSE;
845 else
846 i += 2;
849 /* if no moveto occurs, we will close the figure here */
850 lastmove.x = dc->CursPosX;
851 lastmove.y = dc->CursPosY;
853 /* now let's draw */
854 for( i = 0; i < cCount; i++ )
856 if( lpbTypes[i] == PT_MOVETO )
858 MoveToEx( hdc, lppt[i].x, lppt[i].y, NULL );
859 lastmove.x = dc->CursPosX;
860 lastmove.y = dc->CursPosY;
862 else if( lpbTypes[i] & PT_LINETO )
863 LineTo( hdc, lppt[i].x, lppt[i].y );
864 else if( lpbTypes[i] & PT_BEZIERTO )
866 PolyBezierTo( hdc, &lppt[i], 3 );
867 i += 2;
869 else
870 return FALSE;
872 if( lpbTypes[i] & PT_CLOSEFIGURE )
874 if( PATH_IsPathOpen( dc->path ) )
875 CloseFigure( hdc );
876 else
877 LineTo( hdc, lastmove.x, lastmove.y );
881 return TRUE;
885 /**********************************************************************
886 * LineDDA (GDI32.@)
888 BOOL WINAPI LineDDA(INT nXStart, INT nYStart, INT nXEnd, INT nYEnd,
889 LINEDDAPROC callback, LPARAM lParam )
891 INT xadd = 1, yadd = 1;
892 INT err,erradd;
893 INT cnt;
894 INT dx = nXEnd - nXStart;
895 INT dy = nYEnd - nYStart;
897 if (dx < 0)
899 dx = -dx;
900 xadd = -1;
902 if (dy < 0)
904 dy = -dy;
905 yadd = -1;
907 if (dx > dy) /* line is "more horizontal" */
909 err = 2*dy - dx; erradd = 2*dy - 2*dx;
910 for(cnt = 0;cnt <= dx; cnt++)
912 callback(nXStart,nYStart,lParam);
913 if (err > 0)
915 nYStart += yadd;
916 err += erradd;
918 else err += 2*dy;
919 nXStart += xadd;
922 else /* line is "more vertical" */
924 err = 2*dx - dy; erradd = 2*dx - 2*dy;
925 for(cnt = 0;cnt <= dy; cnt++)
927 callback(nXStart,nYStart,lParam);
928 if (err > 0)
930 nXStart += xadd;
931 err += erradd;
933 else err += 2*dx;
934 nYStart += yadd;
937 return TRUE;
941 /******************************************************************
943 * *Very* simple bezier drawing code,
945 * It uses a recursive algorithm to divide the curve in a series
946 * of straight line segements. Not ideal but for me sufficient.
947 * If you are in need for something better look for some incremental
948 * algorithm.
950 * 7 July 1998 Rein Klazes
954 * some macro definitions for bezier drawing
956 * to avoid truncation errors the coordinates are
957 * shifted upwards. When used in drawing they are
958 * shifted down again, including correct rounding
959 * and avoiding floating point arithmetic
960 * 4 bits should allow 27 bits coordinates which I saw
961 * somewhere in the win32 doc's
965 #define BEZIERSHIFTBITS 4
966 #define BEZIERSHIFTUP(x) ((x)<<BEZIERSHIFTBITS)
967 #define BEZIERPIXEL BEZIERSHIFTUP(1)
968 #define BEZIERSHIFTDOWN(x) (((x)+(1<<(BEZIERSHIFTBITS-1)))>>BEZIERSHIFTBITS)
969 /* maximum depth of recursion */
970 #define BEZIERMAXDEPTH 8
972 /* size of array to store points on */
973 /* enough for one curve */
974 #define BEZIER_INITBUFSIZE (150)
976 /* calculate Bezier average, in this case the middle
977 * correctly rounded...
978 * */
980 #define BEZIERMIDDLE(Mid, P1, P2) \
981 (Mid).x=((P1).x+(P2).x + 1)/2;\
982 (Mid).y=((P1).y+(P2).y + 1)/2;
984 /**********************************************************
985 * BezierCheck helper function to check
986 * that recursion can be terminated
987 * Points[0] and Points[3] are begin and endpoint
988 * Points[1] and Points[2] are control points
989 * level is the recursion depth
990 * returns true if the recusion can be terminated
992 static BOOL BezierCheck( int level, POINT *Points)
994 INT dx, dy;
995 dx=Points[3].x-Points[0].x;
996 dy=Points[3].y-Points[0].y;
997 if(abs(dy)<=abs(dx)){/* shallow line */
998 /* check that control points are between begin and end */
999 if(Points[1].x < Points[0].x){
1000 if(Points[1].x < Points[3].x)
1001 return FALSE;
1002 }else
1003 if(Points[1].x > Points[3].x)
1004 return FALSE;
1005 if(Points[2].x < Points[0].x){
1006 if(Points[2].x < Points[3].x)
1007 return FALSE;
1008 }else
1009 if(Points[2].x > Points[3].x)
1010 return FALSE;
1011 dx=BEZIERSHIFTDOWN(dx);
1012 if(!dx) return TRUE;
1013 if(abs(Points[1].y-Points[0].y-(dy/dx)*
1014 BEZIERSHIFTDOWN(Points[1].x-Points[0].x)) > BEZIERPIXEL ||
1015 abs(Points[2].y-Points[0].y-(dy/dx)*
1016 BEZIERSHIFTDOWN(Points[2].x-Points[0].x)) > BEZIERPIXEL )
1017 return FALSE;
1018 else
1019 return TRUE;
1020 }else{ /* steep line */
1021 /* check that control points are between begin and end */
1022 if(Points[1].y < Points[0].y){
1023 if(Points[1].y < Points[3].y)
1024 return FALSE;
1025 }else
1026 if(Points[1].y > Points[3].y)
1027 return FALSE;
1028 if(Points[2].y < Points[0].y){
1029 if(Points[2].y < Points[3].y)
1030 return FALSE;
1031 }else
1032 if(Points[2].y > Points[3].y)
1033 return FALSE;
1034 dy=BEZIERSHIFTDOWN(dy);
1035 if(!dy) return TRUE;
1036 if(abs(Points[1].x-Points[0].x-(dx/dy)*
1037 BEZIERSHIFTDOWN(Points[1].y-Points[0].y)) > BEZIERPIXEL ||
1038 abs(Points[2].x-Points[0].x-(dx/dy)*
1039 BEZIERSHIFTDOWN(Points[2].y-Points[0].y)) > BEZIERPIXEL )
1040 return FALSE;
1041 else
1042 return TRUE;
1046 /* Helper for GDI_Bezier.
1047 * Just handles one Bezier, so Points should point to four POINTs
1049 static void GDI_InternalBezier( POINT *Points, POINT **PtsOut, INT *dwOut,
1050 INT *nPtsOut, INT level )
1052 if(*nPtsOut == *dwOut) {
1053 *dwOut *= 2;
1054 *PtsOut = HeapReAlloc( GetProcessHeap(), 0, *PtsOut,
1055 *dwOut * sizeof(POINT) );
1058 if(!level || BezierCheck(level, Points)) {
1059 if(*nPtsOut == 0) {
1060 (*PtsOut)[0].x = BEZIERSHIFTDOWN(Points[0].x);
1061 (*PtsOut)[0].y = BEZIERSHIFTDOWN(Points[0].y);
1062 *nPtsOut = 1;
1064 (*PtsOut)[*nPtsOut].x = BEZIERSHIFTDOWN(Points[3].x);
1065 (*PtsOut)[*nPtsOut].y = BEZIERSHIFTDOWN(Points[3].y);
1066 (*nPtsOut) ++;
1067 } else {
1068 POINT Points2[4]; /* for the second recursive call */
1069 Points2[3]=Points[3];
1070 BEZIERMIDDLE(Points2[2], Points[2], Points[3]);
1071 BEZIERMIDDLE(Points2[0], Points[1], Points[2]);
1072 BEZIERMIDDLE(Points2[1],Points2[0],Points2[2]);
1074 BEZIERMIDDLE(Points[1], Points[0], Points[1]);
1075 BEZIERMIDDLE(Points[2], Points[1], Points2[0]);
1076 BEZIERMIDDLE(Points[3], Points[2], Points2[1]);
1078 Points2[0]=Points[3];
1080 /* do the two halves */
1081 GDI_InternalBezier(Points, PtsOut, dwOut, nPtsOut, level-1);
1082 GDI_InternalBezier(Points2, PtsOut, dwOut, nPtsOut, level-1);
1088 /***********************************************************************
1089 * GDI_Bezier [INTERNAL]
1090 * Calculate line segments that approximate -what microsoft calls- a bezier
1091 * curve.
1092 * The routine recursively divides the curve in two parts until a straight
1093 * line can be drawn
1095 * PARAMS
1097 * Points [I] Ptr to count POINTs which are the end and control points
1098 * of the set of Bezier curves to flatten.
1099 * count [I] Number of Points. Must be 3n+1.
1100 * nPtsOut [O] Will contain no of points that have been produced (i.e. no. of
1101 * lines+1).
1103 * RETURNS
1105 * Ptr to an array of POINTs that contain the lines that approximinate the
1106 * Beziers. The array is allocated on the process heap and it is the caller's
1107 * responsibility to HeapFree it. [this is not a particularly nice interface
1108 * but since we can't know in advance how many points will generate, the
1109 * alternative would be to call the function twice, once to determine the size
1110 * and a second time to do the work - I decided this was too much of a pain].
1112 POINT *GDI_Bezier( const POINT *Points, INT count, INT *nPtsOut )
1114 POINT *out;
1115 INT Bezier, dwOut = BEZIER_INITBUFSIZE, i;
1117 if (count == 1 || (count - 1) % 3 != 0) {
1118 ERR("Invalid no. of points %d\n", count);
1119 return NULL;
1121 *nPtsOut = 0;
1122 out = HeapAlloc( GetProcessHeap(), 0, dwOut * sizeof(POINT));
1123 for(Bezier = 0; Bezier < (count-1)/3; Bezier++) {
1124 POINT ptBuf[4];
1125 memcpy(ptBuf, Points + Bezier * 3, sizeof(POINT) * 4);
1126 for(i = 0; i < 4; i++) {
1127 ptBuf[i].x = BEZIERSHIFTUP(ptBuf[i].x);
1128 ptBuf[i].y = BEZIERSHIFTUP(ptBuf[i].y);
1130 GDI_InternalBezier( ptBuf, &out, &dwOut, nPtsOut, BEZIERMAXDEPTH );
1132 TRACE("Produced %d points\n", *nPtsOut);
1133 return out;
1136 /******************************************************************************
1137 * GradientFill (GDI32.@)
1139 * FIXME: we don't support the Alpha channel properly
1141 BOOL WINAPI GdiGradientFill( HDC hdc, TRIVERTEX *vert_array, ULONG nvert,
1142 void * grad_array, ULONG ngrad, ULONG mode )
1144 unsigned int i;
1146 TRACE("vert_array:0x%08lx nvert:%ld grad_array:0x%08lx ngrad:%ld\n",
1147 (long)vert_array, nvert, (long)grad_array, ngrad);
1149 switch(mode)
1151 case GRADIENT_FILL_RECT_H:
1152 for(i = 0; i < ngrad; i++)
1154 GRADIENT_RECT *rect = ((GRADIENT_RECT *)grad_array) + i;
1155 TRIVERTEX *v1 = vert_array + rect->UpperLeft;
1156 TRIVERTEX *v2 = vert_array + rect->LowerRight;
1157 int y1 = v1->y < v2->y ? v1->y : v2->y;
1158 int y2 = v2->y > v1->y ? v2->y : v1->y;
1159 int x, dx;
1160 if (v1->x > v2->x)
1162 TRIVERTEX *t = v2;
1163 v2 = v1;
1164 v1 = t;
1166 dx = v2->x - v1->x;
1167 for (x = 0; x < dx; x++)
1169 POINT pts[2];
1170 HPEN hPen, hOldPen;
1172 hPen = CreatePen( PS_SOLID, 1, RGB(
1173 (v1->Red * (dx - x) + v2->Red * x) / dx >> 8,
1174 (v1->Green * (dx - x) + v2->Green * x) / dx >> 8,
1175 (v1->Blue * (dx - x) + v2->Blue * x) / dx >> 8));
1176 hOldPen = SelectObject( hdc, hPen );
1177 pts[0].x = v1->x + x;
1178 pts[0].y = y1;
1179 pts[1].x = v1->x + x;
1180 pts[1].y = y2;
1181 Polyline( hdc, &pts[0], 2 );
1182 DeleteObject( SelectObject(hdc, hOldPen ) );
1185 break;
1186 case GRADIENT_FILL_RECT_V:
1187 for(i = 0; i < ngrad; i++)
1189 GRADIENT_RECT *rect = ((GRADIENT_RECT *)grad_array) + i;
1190 TRIVERTEX *v1 = vert_array + rect->UpperLeft;
1191 TRIVERTEX *v2 = vert_array + rect->LowerRight;
1192 int x1 = v1->x < v2->x ? v1->x : v2->x;
1193 int x2 = v2->x > v1->x ? v2->x : v1->x;
1194 int y, dy;
1195 if (v1->y > v2->y)
1197 TRIVERTEX *t = v2;
1198 v2 = v1;
1199 v1 = t;
1201 dy = v2->y - v1->y;
1202 for (y = 0; y < dy; y++)
1204 POINT pts[2];
1205 HPEN hPen, hOldPen;
1207 hPen = CreatePen( PS_SOLID, 1, RGB(
1208 (v1->Red * (dy - y) + v2->Red * y) / dy >> 8,
1209 (v1->Green * (dy - y) + v2->Green * y) / dy >> 8,
1210 (v1->Blue * (dy - y) + v2->Blue * y) / dy >> 8));
1211 hOldPen = SelectObject( hdc, hPen );
1212 pts[0].x = x1;
1213 pts[0].y = v1->y + y;
1214 pts[1].x = x2;
1215 pts[1].y = v1->y + y;
1216 Polyline( hdc, &pts[0], 2 );
1217 DeleteObject( SelectObject(hdc, hOldPen ) );
1220 break;
1221 case GRADIENT_FILL_TRIANGLE:
1222 for (i = 0; i < ngrad; i++)
1224 GRADIENT_TRIANGLE *tri = ((GRADIENT_TRIANGLE *)grad_array) + i;
1225 TRIVERTEX *v1 = vert_array + tri->Vertex1;
1226 TRIVERTEX *v2 = vert_array + tri->Vertex2;
1227 TRIVERTEX *v3 = vert_array + tri->Vertex3;
1228 int y, dy;
1230 if (v1->y > v2->y)
1231 { TRIVERTEX *t = v1; v1 = v2; v2 = t; }
1232 if (v2->y > v3->y)
1234 TRIVERTEX *t = v2; v2 = v3; v3 = t;
1235 if (v1->y > v2->y)
1236 { t = v1; v1 = v2; v2 = t; }
1238 /* v1->y <= v2->y <= v3->y */
1240 dy = v3->y - v1->y;
1241 for (y = 0; y < dy; y++)
1243 /* v1->y <= y < v3->y */
1244 TRIVERTEX *v = y < (v2->y - v1->y) ? v1 : v3;
1245 /* (v->y <= y < v2->y) || (v2->y <= y < v->y) */
1246 int dy2 = v2->y - v->y;
1247 int y2 = y + v1->y - v->y;
1249 int x1 = (v3->x * y + v1->x * (dy - y )) / dy;
1250 int x2 = (v2->x * y2 + v-> x * (dy2 - y2)) / dy2;
1251 int r1 = (v3->Red * y + v1->Red * (dy - y )) / dy;
1252 int r2 = (v2->Red * y2 + v-> Red * (dy2 - y2)) / dy2;
1253 int g1 = (v3->Green * y + v1->Green * (dy - y )) / dy;
1254 int g2 = (v2->Green * y2 + v-> Green * (dy2 - y2)) / dy2;
1255 int b1 = (v3->Blue * y + v1->Blue * (dy - y )) / dy;
1256 int b2 = (v2->Blue * y2 + v-> Blue * (dy2 - y2)) / dy2;
1258 int x;
1259 if (x1 < x2)
1261 int dx = x2 - x1;
1262 for (x = 0; x < dx; x++)
1263 SetPixel (hdc, x + x1, y + v1->y, RGB(
1264 (r1 * (dx - x) + r2 * x) / dx >> 8,
1265 (g1 * (dx - x) + g2 * x) / dx >> 8,
1266 (b1 * (dx - x) + b2 * x) / dx >> 8));
1268 else
1270 int dx = x1 - x2;
1271 for (x = 0; x < dx; x++)
1272 SetPixel (hdc, x + x2, y + v1->y, RGB(
1273 (r2 * (dx - x) + r1 * x) / dx >> 8,
1274 (g2 * (dx - x) + g1 * x) / dx >> 8,
1275 (b2 * (dx - x) + b1 * x) / dx >> 8));
1279 break;
1280 default:
1281 return FALSE;
1284 return TRUE;