Release 20030408.
[wine/gsoc-2012-control.git] / dlls / gdi / mfdrv / graphics.c
blob591d05ca0b59ffaae087b99e06a99288aa8f710a
1 /*
2 * Metafile driver graphics functions
4 * Copyright 1993, 1994 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include <stdlib.h>
22 #include <string.h>
24 #include "gdi.h"
25 #include "mfdrv/metafiledrv.h"
26 #include "wine/debug.h"
28 WINE_DEFAULT_DEBUG_CHANNEL(metafile);
30 /**********************************************************************
31 * MFDRV_MoveTo
33 BOOL
34 MFDRV_MoveTo(PHYSDEV dev, INT x, INT y)
36 return MFDRV_MetaParam2(dev,META_MOVETO,x,y);
39 /***********************************************************************
40 * MFDRV_LineTo
42 BOOL
43 MFDRV_LineTo( PHYSDEV dev, INT x, INT y )
45 return MFDRV_MetaParam2(dev, META_LINETO, x, y);
49 /***********************************************************************
50 * MFDRV_Arc
52 BOOL
53 MFDRV_Arc( PHYSDEV dev, INT left, INT top, INT right, INT bottom,
54 INT xstart, INT ystart, INT xend, INT yend )
56 return MFDRV_MetaParam8(dev, META_ARC, left, top, right, bottom,
57 xstart, ystart, xend, yend);
61 /***********************************************************************
62 * MFDRV_Pie
64 BOOL
65 MFDRV_Pie( PHYSDEV dev, INT left, INT top, INT right, INT bottom,
66 INT xstart, INT ystart, INT xend, INT yend )
68 return MFDRV_MetaParam8(dev, META_PIE, left, top, right, bottom,
69 xstart, ystart, xend, yend);
73 /***********************************************************************
74 * MFDRV_Chord
76 BOOL
77 MFDRV_Chord( PHYSDEV dev, INT left, INT top, INT right, INT bottom,
78 INT xstart, INT ystart, INT xend, INT yend )
80 return MFDRV_MetaParam8(dev, META_CHORD, left, top, right, bottom,
81 xstart, ystart, xend, yend);
84 /***********************************************************************
85 * MFDRV_Ellipse
87 BOOL
88 MFDRV_Ellipse( PHYSDEV dev, INT left, INT top, INT right, INT bottom )
90 return MFDRV_MetaParam4(dev, META_ELLIPSE, left, top, right, bottom);
93 /***********************************************************************
94 * MFDRV_Rectangle
96 BOOL
97 MFDRV_Rectangle(PHYSDEV dev, INT left, INT top, INT right, INT bottom)
99 return MFDRV_MetaParam4(dev, META_RECTANGLE, left, top, right, bottom);
102 /***********************************************************************
103 * MFDRV_RoundRect
105 BOOL
106 MFDRV_RoundRect( PHYSDEV dev, INT left, INT top, INT right,
107 INT bottom, INT ell_width, INT ell_height )
109 return MFDRV_MetaParam6(dev, META_ROUNDRECT, left, top, right, bottom,
110 ell_width, ell_height);
113 /***********************************************************************
114 * MFDRV_SetPixel
116 COLORREF
117 MFDRV_SetPixel( PHYSDEV dev, INT x, INT y, COLORREF color )
119 return MFDRV_MetaParam4(dev, META_SETPIXEL, x, y,HIWORD(color),
120 LOWORD(color));
124 /******************************************************************
125 * MFDRV_MetaPoly - implements Polygon and Polyline
127 static BOOL MFDRV_MetaPoly(PHYSDEV dev, short func, LPPOINT16 pt, short count)
129 BOOL ret;
130 DWORD len;
131 METARECORD *mr;
133 len = sizeof(METARECORD) + (count * 4);
134 if (!(mr = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, len )))
135 return FALSE;
137 mr->rdSize = len / 2;
138 mr->rdFunction = func;
139 *(mr->rdParm) = count;
140 memcpy(mr->rdParm + 1, pt, count * 4);
141 ret = MFDRV_WriteRecord( dev, mr, mr->rdSize * 2);
142 HeapFree( GetProcessHeap(), 0, mr);
143 return ret;
147 /**********************************************************************
148 * MFDRV_Polyline
150 BOOL
151 MFDRV_Polyline( PHYSDEV dev, const POINT* pt, INT count )
153 register int i;
154 LPPOINT16 pt16;
155 BOOL16 ret;
157 pt16 = (LPPOINT16)HeapAlloc( GetProcessHeap(), 0, sizeof(POINT16)*count );
158 if(!pt16) return FALSE;
159 for (i=count;i--;) CONV_POINT32TO16(&(pt[i]),&(pt16[i]));
160 ret = MFDRV_MetaPoly(dev, META_POLYLINE, pt16, count);
162 HeapFree( GetProcessHeap(), 0, pt16 );
163 return ret;
167 /**********************************************************************
168 * MFDRV_Polygon
170 BOOL
171 MFDRV_Polygon( PHYSDEV dev, const POINT* pt, INT count )
173 register int i;
174 LPPOINT16 pt16;
175 BOOL16 ret;
177 pt16 = (LPPOINT16) HeapAlloc( GetProcessHeap(), 0, sizeof(POINT16)*count );
178 if(!pt16) return FALSE;
179 for (i=count;i--;) CONV_POINT32TO16(&(pt[i]),&(pt16[i]));
180 ret = MFDRV_MetaPoly(dev, META_POLYGON, pt16, count);
182 HeapFree( GetProcessHeap(), 0, pt16 );
183 return ret;
187 /**********************************************************************
188 * MFDRV_PolyPolygon
190 BOOL
191 MFDRV_PolyPolygon( PHYSDEV dev, const POINT* pt, const INT* counts, UINT polygons)
193 int i,j;
194 LPPOINT16 pt16;
195 const POINT* curpt=pt;
196 BOOL ret;
198 for (i=0;i<polygons;i++) {
199 pt16=(LPPOINT16)HeapAlloc( GetProcessHeap(), 0,
200 sizeof(POINT16) * counts[i] );
201 if(!pt16) return FALSE;
202 for (j=counts[i];j--;) CONV_POINT32TO16(&(curpt[j]),&(pt16[j]));
203 ret = MFDRV_MetaPoly(dev, META_POLYGON, pt16, counts[i]);
204 HeapFree( GetProcessHeap(), 0, pt16 );
205 if (!ret)
206 return FALSE;
207 curpt+=counts[i];
209 return TRUE;
213 /**********************************************************************
214 * MFDRV_ExtFloodFill
216 BOOL
217 MFDRV_ExtFloodFill( PHYSDEV dev, INT x, INT y, COLORREF color, UINT fillType )
219 return MFDRV_MetaParam4(dev,META_FLOODFILL,x,y,HIWORD(color),
220 LOWORD(color));
224 /******************************************************************
225 * MFDRV_CreateRegion
227 * For explanation of the format of the record see MF_Play_MetaCreateRegion in
228 * objects/metafile.c
230 static INT16 MFDRV_CreateRegion(PHYSDEV dev, HRGN hrgn)
232 DWORD len;
233 METARECORD *mr;
234 RGNDATA *rgndata;
235 RECT *pCurRect, *pEndRect;
236 WORD Bands = 0, MaxBands = 0;
237 WORD *Param, *StartBand;
238 BOOL ret;
240 if (!(len = GetRegionData( hrgn, 0, NULL ))) return -1;
241 if( !(rgndata = HeapAlloc( GetProcessHeap(), 0, len )) ) {
242 WARN("Can't alloc rgndata buffer\n");
243 return -1;
245 GetRegionData( hrgn, len, rgndata );
247 /* Overestimate of length:
248 * Assume every rect is a separate band -> 6 WORDs per rect
250 len = sizeof(METARECORD) + 20 + (rgndata->rdh.nCount * 12);
251 if( !(mr = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, len )) ) {
252 WARN("Can't alloc METARECORD buffer\n");
253 HeapFree( GetProcessHeap(), 0, rgndata );
254 return -1;
257 Param = mr->rdParm + 11;
258 StartBand = NULL;
260 pEndRect = (RECT *)rgndata->Buffer + rgndata->rdh.nCount;
261 for(pCurRect = (RECT *)rgndata->Buffer; pCurRect < pEndRect; pCurRect++)
263 if( StartBand && pCurRect->top == *(StartBand + 1) )
265 *Param++ = pCurRect->left;
266 *Param++ = pCurRect->right;
268 else
270 if(StartBand)
272 *StartBand = Param - StartBand - 3;
273 *Param++ = *StartBand;
274 if(*StartBand > MaxBands)
275 MaxBands = *StartBand;
276 Bands++;
278 StartBand = Param++;
279 *Param++ = pCurRect->top;
280 *Param++ = pCurRect->bottom;
281 *Param++ = pCurRect->left;
282 *Param++ = pCurRect->right;
285 len = Param - (WORD *)mr;
287 mr->rdParm[0] = 0;
288 mr->rdParm[1] = 6;
289 mr->rdParm[2] = 0x1234;
290 mr->rdParm[3] = 0;
291 mr->rdParm[4] = len * 2;
292 mr->rdParm[5] = Bands;
293 mr->rdParm[6] = MaxBands;
294 mr->rdParm[7] = rgndata->rdh.rcBound.left;
295 mr->rdParm[8] = rgndata->rdh.rcBound.top;
296 mr->rdParm[9] = rgndata->rdh.rcBound.right;
297 mr->rdParm[10] = rgndata->rdh.rcBound.bottom;
298 mr->rdFunction = META_CREATEREGION;
299 mr->rdSize = len / 2;
300 ret = MFDRV_WriteRecord( dev, mr, mr->rdSize * 2 );
301 HeapFree( GetProcessHeap(), 0, mr );
302 HeapFree( GetProcessHeap(), 0, rgndata );
303 if(!ret)
305 WARN("MFDRV_WriteRecord failed\n");
306 return -1;
308 return MFDRV_AddHandleDC( dev );
312 /**********************************************************************
313 * MFDRV_PaintRgn
315 BOOL
316 MFDRV_PaintRgn( PHYSDEV dev, HRGN hrgn )
318 INT16 index;
319 index = MFDRV_CreateRegion( dev, hrgn );
320 if(index == -1)
321 return FALSE;
322 return MFDRV_MetaParam1( dev, META_PAINTREGION, index );
326 /**********************************************************************
327 * MFDRV_InvertRgn
329 BOOL
330 MFDRV_InvertRgn( PHYSDEV dev, HRGN hrgn )
332 INT16 index;
333 index = MFDRV_CreateRegion( dev, hrgn );
334 if(index == -1)
335 return FALSE;
336 return MFDRV_MetaParam1( dev, META_INVERTREGION, index );
340 /**********************************************************************
341 * MFDRV_FillRgn
343 BOOL
344 MFDRV_FillRgn( PHYSDEV dev, HRGN hrgn, HBRUSH hbrush )
346 INT16 iRgn, iBrush;
347 iRgn = MFDRV_CreateRegion( dev, hrgn );
348 if(iRgn == -1)
349 return FALSE;
350 iBrush = MFDRV_CreateBrushIndirect( dev, hbrush );
351 if(iBrush == -1)
352 return FALSE;
353 return MFDRV_MetaParam2( dev, META_FILLREGION, iRgn, iBrush );
356 /**********************************************************************
357 * MFDRV_FrameRgn
359 BOOL
360 MFDRV_FrameRgn( PHYSDEV dev, HRGN hrgn, HBRUSH hbrush, INT x, INT y )
362 INT16 iRgn, iBrush;
363 iRgn = MFDRV_CreateRegion( dev, hrgn );
364 if(iRgn == -1)
365 return FALSE;
366 iBrush = MFDRV_CreateBrushIndirect( dev, hbrush );
367 if(iBrush == -1)
368 return FALSE;
369 return MFDRV_MetaParam4( dev, META_FRAMEREGION, iRgn, iBrush, x, y );
373 /**********************************************************************
374 * MFDRV_ExtSelectClipRgn
376 INT MFDRV_ExtSelectClipRgn( PHYSDEV dev, HRGN hrgn, INT mode )
378 INT16 iRgn;
379 INT ret;
381 if (mode != RGN_COPY) return ERROR;
382 if (!hrgn) return NULLREGION;
383 iRgn = MFDRV_CreateRegion( dev, hrgn );
384 if(iRgn == -1) return ERROR;
385 ret = MFDRV_MetaParam1( dev, META_SELECTCLIPREGION, iRgn ) ? NULLREGION : ERROR;
386 MFDRV_MetaParam1( dev, META_DELETEOBJECT, iRgn );
387 return ret;
391 /**********************************************************************
392 * MFDRV_SetBkColor
394 COLORREF
395 MFDRV_SetBkColor( PHYSDEV dev, COLORREF color )
397 return MFDRV_MetaParam2(dev, META_SETBKCOLOR, HIWORD(color),
398 LOWORD(color)) ? color : CLR_INVALID;
402 /**********************************************************************
403 * MFDRV_SetTextColor
405 COLORREF
406 MFDRV_SetTextColor( PHYSDEV dev, COLORREF color )
408 return MFDRV_MetaParam2(dev, META_SETTEXTCOLOR, HIWORD(color),
409 LOWORD(color)) ? color : CLR_INVALID;
413 /**********************************************************************
414 * MFDRV_PolyBezier
415 * Since MetaFiles don't record Beziers and they don't even record
416 * approximations to them using lines, we need this stub function.
418 BOOL
419 MFDRV_PolyBezier( PHYSDEV dev, const POINT *pts, DWORD count )
421 return FALSE;
424 /**********************************************************************
425 * MFDRV_PolyBezierTo
426 * Since MetaFiles don't record Beziers and they don't even record
427 * approximations to them using lines, we need this stub function.
429 BOOL
430 MFDRV_PolyBezierTo( PHYSDEV dev, const POINT *pts, DWORD count )
432 return FALSE;