- Added f8 (history retrieval from partial command) support
[wine/multimedia.git] / dlls / gdi / enhmfdrv / graphics.c
blob402beaad4e9d1e5d43c9311cae05abc839f021d8
1 /*
2 * Enhanced MetaFile driver graphics functions
4 * Copyright 1999 Huw D M Davies
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 "enhmfdrv/enhmetafiledrv.h"
26 #include "wine/debug.h"
28 WINE_DEFAULT_DEBUG_CHANNEL(enhmetafile);
30 /**********************************************************************
31 * EMFDRV_MoveTo
33 BOOL
34 EMFDRV_MoveTo(PHYSDEV dev, INT x, INT y)
36 EMRMOVETOEX emr;
38 emr.emr.iType = EMR_MOVETOEX;
39 emr.emr.nSize = sizeof(emr);
40 emr.ptl.x = x;
41 emr.ptl.y = y;
43 return EMFDRV_WriteRecord( dev, &emr.emr );
46 /***********************************************************************
47 * EMFDRV_LineTo
49 BOOL
50 EMFDRV_LineTo( PHYSDEV dev, INT x, INT y )
52 EMRLINETO emr;
53 RECTL bounds;
54 EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE *)dev;
55 DC *dc = physDev->dc;
57 emr.emr.iType = EMR_LINETO;
58 emr.emr.nSize = sizeof(emr);
59 emr.ptl.x = x;
60 emr.ptl.y = y;
62 if(!EMFDRV_WriteRecord( dev, &emr.emr ))
63 return FALSE;
65 bounds.left = min(x, dc->CursPosX);
66 bounds.top = min(y, dc->CursPosY);
67 bounds.right = max(x, dc->CursPosX);
68 bounds.bottom = max(y, dc->CursPosY);
70 EMFDRV_UpdateBBox( dev, &bounds );
72 return TRUE;
76 /***********************************************************************
77 * EMFDRV_ArcChordPie
79 static BOOL
80 EMFDRV_ArcChordPie( PHYSDEV dev, INT left, INT top, INT right, INT bottom,
81 INT xstart, INT ystart, INT xend, INT yend, DWORD iType )
83 INT temp, xCentre, yCentre, i;
84 double angleStart, angleEnd;
85 double xinterStart, yinterStart, xinterEnd, yinterEnd;
86 EMRARC emr;
87 RECTL bounds;
88 EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE *)dev;
89 DC *dc = physDev->dc;
91 if(left == right || top == bottom) return FALSE;
93 if(left > right) {temp = left; left = right; right = temp;}
94 if(top > bottom) {temp = top; top = bottom; bottom = temp;}
96 if(dc->GraphicsMode == GM_COMPATIBLE) {
97 right--;
98 bottom--;
101 emr.emr.iType = iType;
102 emr.emr.nSize = sizeof(emr);
103 emr.rclBox.left = left;
104 emr.rclBox.top = top;
105 emr.rclBox.right = right;
106 emr.rclBox.bottom = bottom;
107 emr.ptlStart.x = xstart;
108 emr.ptlStart.y = ystart;
109 emr.ptlEnd.x = xend;
110 emr.ptlEnd.x = yend;
113 /* Now calculate the BBox */
114 xCentre = (left + right + 1) / 2;
115 yCentre = (top + bottom + 1) / 2;
117 xstart -= xCentre;
118 ystart -= yCentre;
119 xend -= xCentre;
120 yend -= yCentre;
122 /* invert y co-ords to get angle anti-clockwise from x-axis */
123 angleStart = atan2( -(double)ystart, (double)xstart);
124 angleEnd = atan2( -(double)yend, (double)xend);
126 /* These are the intercepts of the start/end lines with the arc */
128 xinterStart = (right - left + 1)/2 * cos(angleStart) + xCentre;
129 yinterStart = -(bottom - top + 1)/2 * sin(angleStart) + yCentre;
130 xinterEnd = (right - left + 1)/2 * cos(angleEnd) + xCentre;
131 yinterEnd = -(bottom - top + 1)/2 * sin(angleEnd) + yCentre;
133 if(angleStart < 0) angleStart += 2 * M_PI;
134 if(angleEnd < 0) angleEnd += 2 * M_PI;
135 if(angleEnd < angleStart) angleEnd += 2 * M_PI;
137 bounds.left = min(xinterStart, xinterEnd);
138 bounds.top = min(yinterStart, yinterEnd);
139 bounds.right = max(xinterStart, xinterEnd);
140 bounds.bottom = max(yinterStart, yinterEnd);
142 for(i = 0; i <= 8; i++) {
143 if(i * M_PI / 2 < angleStart) /* loop until we're past start */
144 continue;
145 if(i * M_PI / 2 > angleEnd) /* if we're past end we're finished */
146 break;
148 /* the arc touches the rectangle at the start of quadrant i, so adjust
149 BBox to reflect this. */
151 switch(i % 4) {
152 case 0:
153 bounds.right = right;
154 break;
155 case 1:
156 bounds.top = top;
157 break;
158 case 2:
159 bounds.left = left;
160 break;
161 case 3:
162 bounds.bottom = bottom;
163 break;
167 /* If we're drawing a pie then make sure we include the centre */
168 if(iType == EMR_PIE) {
169 if(bounds.left > xCentre) bounds.left = xCentre;
170 else if(bounds.right < xCentre) bounds.right = xCentre;
171 if(bounds.top > yCentre) bounds.top = yCentre;
172 else if(bounds.bottom < yCentre) bounds.right = yCentre;
174 if(!EMFDRV_WriteRecord( dev, &emr.emr ))
175 return FALSE;
176 EMFDRV_UpdateBBox( dev, &bounds );
177 return TRUE;
181 /***********************************************************************
182 * EMFDRV_Arc
184 BOOL
185 EMFDRV_Arc( PHYSDEV dev, INT left, INT top, INT right, INT bottom,
186 INT xstart, INT ystart, INT xend, INT yend )
188 return EMFDRV_ArcChordPie( dev, left, top, right, bottom, xstart, ystart,
189 xend, yend, EMR_ARC );
192 /***********************************************************************
193 * EMFDRV_Pie
195 BOOL
196 EMFDRV_Pie( PHYSDEV dev, INT left, INT top, INT right, INT bottom,
197 INT xstart, INT ystart, INT xend, INT yend )
199 return EMFDRV_ArcChordPie( dev, left, top, right, bottom, xstart, ystart,
200 xend, yend, EMR_PIE );
204 /***********************************************************************
205 * EMFDRV_Chord
207 BOOL
208 EMFDRV_Chord( PHYSDEV dev, INT left, INT top, INT right, INT bottom,
209 INT xstart, INT ystart, INT xend, INT yend )
211 return EMFDRV_ArcChordPie( dev, left, top, right, bottom, xstart, ystart,
212 xend, yend, EMR_CHORD );
215 /***********************************************************************
216 * EMFDRV_Ellipse
218 BOOL
219 EMFDRV_Ellipse( PHYSDEV dev, INT left, INT top, INT right, INT bottom )
221 EMRELLIPSE emr;
222 INT temp;
223 EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE *)dev;
224 DC *dc = physDev->dc;
226 TRACE("%d,%d - %d,%d\n", left, top, right, bottom);
228 if(left == right || top == bottom) return FALSE;
230 if(left > right) {temp = left; left = right; right = temp;}
231 if(top > bottom) {temp = top; top = bottom; bottom = temp;}
233 if(dc->GraphicsMode == GM_COMPATIBLE) {
234 right--;
235 bottom--;
238 emr.emr.iType = EMR_ELLIPSE;
239 emr.emr.nSize = sizeof(emr);
240 emr.rclBox.left = left;
241 emr.rclBox.top = top;
242 emr.rclBox.right = right;
243 emr.rclBox.bottom = bottom;
245 EMFDRV_UpdateBBox( dev, &emr.rclBox );
246 return EMFDRV_WriteRecord( dev, &emr.emr );
249 /***********************************************************************
250 * EMFDRV_Rectangle
252 BOOL
253 EMFDRV_Rectangle(PHYSDEV dev, INT left, INT top, INT right, INT bottom)
255 EMRRECTANGLE emr;
256 INT temp;
257 EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE *)dev;
258 DC *dc = physDev->dc;
260 TRACE("%d,%d - %d,%d\n", left, top, right, bottom);
262 if(left == right || top == bottom) return FALSE;
264 if(left > right) {temp = left; left = right; right = temp;}
265 if(top > bottom) {temp = top; top = bottom; bottom = temp;}
267 if(dc->GraphicsMode == GM_COMPATIBLE) {
268 right--;
269 bottom--;
272 emr.emr.iType = EMR_RECTANGLE;
273 emr.emr.nSize = sizeof(emr);
274 emr.rclBox.left = left;
275 emr.rclBox.top = top;
276 emr.rclBox.right = right;
277 emr.rclBox.bottom = bottom;
279 EMFDRV_UpdateBBox( dev, &emr.rclBox );
280 return EMFDRV_WriteRecord( dev, &emr.emr );
283 /***********************************************************************
284 * EMFDRV_RoundRect
286 BOOL
287 EMFDRV_RoundRect( PHYSDEV dev, INT left, INT top, INT right,
288 INT bottom, INT ell_width, INT ell_height )
290 EMRROUNDRECT emr;
291 INT temp;
292 EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE *)dev;
293 DC *dc = physDev->dc;
295 if(left == right || top == bottom) return FALSE;
297 if(left > right) {temp = left; left = right; right = temp;}
298 if(top > bottom) {temp = top; top = bottom; bottom = temp;}
300 if(dc->GraphicsMode == GM_COMPATIBLE) {
301 right--;
302 bottom--;
305 emr.emr.iType = EMR_ROUNDRECT;
306 emr.emr.nSize = sizeof(emr);
307 emr.rclBox.left = left;
308 emr.rclBox.top = top;
309 emr.rclBox.right = right;
310 emr.rclBox.bottom = bottom;
311 emr.szlCorner.cx = ell_width;
312 emr.szlCorner.cy = ell_height;
314 EMFDRV_UpdateBBox( dev, &emr.rclBox );
315 return EMFDRV_WriteRecord( dev, &emr.emr );
318 /***********************************************************************
319 * EMFDRV_SetPixel
321 COLORREF
322 EMFDRV_SetPixel( PHYSDEV dev, INT x, INT y, COLORREF color )
324 return TRUE;
328 /**********************************************************************
329 * EMFDRV_Polylinegon
331 * Helper for EMFDRV_Poly{line|gon}
333 static BOOL
334 EMFDRV_Polylinegon( PHYSDEV dev, const POINT* pt, INT count, DWORD iType )
336 EMRPOLYLINE *emr;
337 DWORD size;
338 INT i;
339 BOOL ret;
341 size = sizeof(EMRPOLYLINE) + sizeof(POINTL) * (count - 1);
343 emr = HeapAlloc( GetProcessHeap(), 0, size );
344 emr->emr.iType = iType;
345 emr->emr.nSize = size;
347 emr->rclBounds.left = emr->rclBounds.right = pt[0].x;
348 emr->rclBounds.top = emr->rclBounds.bottom = pt[0].y;
350 for(i = 1; i < count; i++) {
351 if(pt[i].x < emr->rclBounds.left)
352 emr->rclBounds.left = pt[i].x;
353 else if(pt[i].x > emr->rclBounds.right)
354 emr->rclBounds.right = pt[i].x;
355 if(pt[i].y < emr->rclBounds.top)
356 emr->rclBounds.top = pt[i].y;
357 else if(pt[i].y > emr->rclBounds.bottom)
358 emr->rclBounds.bottom = pt[i].y;
361 emr->cptl = count;
362 memcpy(emr->aptl, pt, count * sizeof(POINTL));
364 ret = EMFDRV_WriteRecord( dev, &emr->emr );
365 if(ret)
366 EMFDRV_UpdateBBox( dev, &emr->rclBounds );
367 HeapFree( GetProcessHeap(), 0, emr );
368 return ret;
372 /**********************************************************************
373 * EMFDRV_Polyline
375 BOOL
376 EMFDRV_Polyline( PHYSDEV dev, const POINT* pt, INT count )
378 return EMFDRV_Polylinegon( dev, pt, count, EMR_POLYLINE );
381 /**********************************************************************
382 * EMFDRV_Polygon
384 BOOL
385 EMFDRV_Polygon( PHYSDEV dev, const POINT* pt, INT count )
387 if(count < 2) return FALSE;
388 return EMFDRV_Polylinegon( dev, pt, count, EMR_POLYGON );
392 /**********************************************************************
393 * EMFDRV_PolyPolylinegon
395 * Helper for EMFDRV_PolyPoly{line|gon}
397 static BOOL
398 EMFDRV_PolyPolylinegon( PHYSDEV dev, const POINT* pt, const INT* counts, UINT polys,
399 DWORD iType)
401 EMRPOLYPOLYLINE *emr;
402 DWORD cptl = 0, poly, size, point;
403 RECTL bounds;
404 const POINT *pts;
405 BOOL ret;
407 bounds.left = bounds.right = pt[0].x;
408 bounds.top = bounds.bottom = pt[0].y;
410 pts = pt;
411 for(poly = 0; poly < polys; poly++) {
412 cptl += counts[poly];
413 for(point = 0; point < counts[poly]; point++) {
414 if(bounds.left > pts->x) bounds.left = pts->x;
415 else if(bounds.right < pts->x) bounds.right = pts->x;
416 if(bounds.top > pts->y) bounds.top = pts->y;
417 else if(bounds.bottom < pts->y) bounds.bottom = pts->y;
418 pts++;
422 size = sizeof(EMRPOLYPOLYLINE) + (polys - 1) * sizeof(DWORD) +
423 (cptl - 1) * sizeof(POINTL);
425 emr = HeapAlloc( GetProcessHeap(), 0, size );
427 emr->emr.iType = iType;
428 emr->emr.nSize = size;
429 emr->rclBounds = bounds;
430 emr->nPolys = polys;
431 emr->cptl = cptl;
432 memcpy(emr->aPolyCounts, counts, polys * sizeof(DWORD));
433 memcpy(emr->aPolyCounts + polys, pt, cptl * sizeof(POINTL));
434 ret = EMFDRV_WriteRecord( dev, &emr->emr );
435 if(ret)
436 EMFDRV_UpdateBBox( dev, &emr->rclBounds );
437 HeapFree( GetProcessHeap(), 0, emr );
438 return ret;
441 /**********************************************************************
442 * EMFDRV_PolyPolyline
444 BOOL
445 EMFDRV_PolyPolyline(PHYSDEV dev, const POINT* pt, const DWORD* counts, DWORD polys)
447 return EMFDRV_PolyPolylinegon( dev, pt, (INT *)counts, polys,
448 EMR_POLYPOLYLINE );
451 /**********************************************************************
452 * EMFDRV_PolyPolygon
454 BOOL
455 EMFDRV_PolyPolygon( PHYSDEV dev, const POINT* pt, const INT* counts, UINT polys )
457 return EMFDRV_PolyPolylinegon( dev, pt, counts, polys, EMR_POLYPOLYGON );
461 /**********************************************************************
462 * EMFDRV_ExtFloodFill
464 BOOL
465 EMFDRV_ExtFloodFill( PHYSDEV dev, INT x, INT y, COLORREF color, UINT fillType )
467 EMREXTFLOODFILL emr;
469 emr.emr.iType = EMR_EXTFLOODFILL;
470 emr.emr.nSize = sizeof(emr);
471 emr.ptlStart.x = x;
472 emr.ptlStart.y = y;
473 emr.crColor = color;
474 emr.iMode = fillType;
476 return EMFDRV_WriteRecord( dev, &emr.emr );
480 /*********************************************************************
481 * EMFDRV_FillRgn
483 BOOL EMFDRV_FillRgn( PHYSDEV dev, HRGN hrgn, HBRUSH hbrush )
485 EMRFILLRGN *emr;
486 DWORD size, rgnsize, index;
487 BOOL ret;
489 index = EMFDRV_CreateBrushIndirect( dev, hbrush );
490 if(!index) return FALSE;
492 rgnsize = GetRegionData( hrgn, 0, NULL );
493 size = rgnsize + sizeof(EMRFILLRGN) - 1;
494 emr = HeapAlloc( GetProcessHeap(), 0, size );
496 GetRegionData( hrgn, rgnsize, (RGNDATA *)&emr->RgnData );
498 emr->emr.iType = EMR_FILLRGN;
499 emr->emr.nSize = size;
500 emr->rclBounds.left = ((RGNDATA *)&emr->RgnData)->rdh.rcBound.left;
501 emr->rclBounds.top = ((RGNDATA *)&emr->RgnData)->rdh.rcBound.top;
502 emr->rclBounds.right = ((RGNDATA *)&emr->RgnData)->rdh.rcBound.right - 1;
503 emr->rclBounds.bottom = ((RGNDATA *)&emr->RgnData)->rdh.rcBound.bottom - 1;
504 emr->cbRgnData = rgnsize;
505 emr->ihBrush = index;
507 ret = EMFDRV_WriteRecord( dev, &emr->emr );
508 if(ret)
509 EMFDRV_UpdateBBox( dev, &emr->rclBounds );
510 HeapFree( GetProcessHeap(), 0, emr );
511 return ret;
513 /*********************************************************************
514 * EMFDRV_FrameRgn
516 BOOL EMFDRV_FrameRgn( PHYSDEV dev, HRGN hrgn, HBRUSH hbrush, INT width, INT height )
518 EMRFRAMERGN *emr;
519 DWORD size, rgnsize, index;
520 BOOL ret;
522 index = EMFDRV_CreateBrushIndirect( dev, hbrush );
523 if(!index) return FALSE;
525 rgnsize = GetRegionData( hrgn, 0, NULL );
526 size = rgnsize + sizeof(EMRFRAMERGN) - 1;
527 emr = HeapAlloc( GetProcessHeap(), 0, size );
529 GetRegionData( hrgn, rgnsize, (RGNDATA *)&emr->RgnData );
531 emr->emr.iType = EMR_FRAMERGN;
532 emr->emr.nSize = size;
533 emr->rclBounds.left = ((RGNDATA *)&emr->RgnData)->rdh.rcBound.left;
534 emr->rclBounds.top = ((RGNDATA *)&emr->RgnData)->rdh.rcBound.top;
535 emr->rclBounds.right = ((RGNDATA *)&emr->RgnData)->rdh.rcBound.right - 1;
536 emr->rclBounds.bottom = ((RGNDATA *)&emr->RgnData)->rdh.rcBound.bottom - 1;
537 emr->cbRgnData = rgnsize;
538 emr->ihBrush = index;
539 emr->szlStroke.cx = width;
540 emr->szlStroke.cy = height;
542 ret = EMFDRV_WriteRecord( dev, &emr->emr );
543 if(ret)
544 EMFDRV_UpdateBBox( dev, &emr->rclBounds );
545 HeapFree( GetProcessHeap(), 0, emr );
546 return ret;
549 /*********************************************************************
550 * EMFDRV_PaintInvertRgn
552 * Helper for EMFDRV_{Paint|Invert}Rgn
554 static BOOL EMFDRV_PaintInvertRgn( PHYSDEV dev, HRGN hrgn, DWORD iType )
556 EMRINVERTRGN *emr;
557 DWORD size, rgnsize;
558 BOOL ret;
561 rgnsize = GetRegionData( hrgn, 0, NULL );
562 size = rgnsize + sizeof(EMRINVERTRGN) - 1;
563 emr = HeapAlloc( GetProcessHeap(), 0, size );
565 GetRegionData( hrgn, rgnsize, (RGNDATA *)&emr->RgnData );
567 emr->emr.iType = iType;
568 emr->emr.nSize = size;
569 emr->rclBounds.left = ((RGNDATA *)&emr->RgnData)->rdh.rcBound.left;
570 emr->rclBounds.top = ((RGNDATA *)&emr->RgnData)->rdh.rcBound.top;
571 emr->rclBounds.right = ((RGNDATA *)&emr->RgnData)->rdh.rcBound.right - 1;
572 emr->rclBounds.bottom = ((RGNDATA *)&emr->RgnData)->rdh.rcBound.bottom - 1;
573 emr->cbRgnData = rgnsize;
575 ret = EMFDRV_WriteRecord( dev, &emr->emr );
576 if(ret)
577 EMFDRV_UpdateBBox( dev, &emr->rclBounds );
578 HeapFree( GetProcessHeap(), 0, emr );
579 return ret;
582 /**********************************************************************
583 * EMFDRV_PaintRgn
585 BOOL
586 EMFDRV_PaintRgn( PHYSDEV dev, HRGN hrgn )
588 return EMFDRV_PaintInvertRgn( dev, hrgn, EMR_PAINTRGN );
591 /**********************************************************************
592 * EMFDRV_InvertRgn
594 BOOL
595 EMFDRV_InvertRgn( PHYSDEV dev, HRGN hrgn )
597 return EMFDRV_PaintInvertRgn( dev, hrgn, EMR_INVERTRGN );
600 /**********************************************************************
601 * EMFDRV_SetBkColor
603 COLORREF
604 EMFDRV_SetBkColor( PHYSDEV dev, COLORREF color )
606 EMRSETBKCOLOR emr;
608 emr.emr.iType = EMR_SETBKCOLOR;
609 emr.emr.nSize = sizeof(emr);
610 emr.crColor = color;
612 return EMFDRV_WriteRecord( dev, &emr.emr ) ? color : CLR_INVALID;
616 /**********************************************************************
617 * EMFDRV_SetTextColor
619 COLORREF
620 EMFDRV_SetTextColor( PHYSDEV dev, COLORREF color )
622 EMRSETTEXTCOLOR emr;
624 emr.emr.iType = EMR_SETTEXTCOLOR;
625 emr.emr.nSize = sizeof(emr);
626 emr.crColor = color;
628 return EMFDRV_WriteRecord( dev, &emr.emr ) ? color : CLR_INVALID;