Implement ResetDC and PHYSICALOFFSET[X|Y] devcaps.
[wine] / dlls / gdi / enhmfdrv / graphics.c
1 /*
2  * Enhanced MetaFile driver graphics functions
3  *
4  * Copyright 1999 Huw D M Davies
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include <stdlib.h>
22 #include <string.h>
23
24 #include "gdi.h"
25 #include "enhmfdrv/enhmetafiledrv.h"
26 #include "wine/debug.h"
27
28 WINE_DEFAULT_DEBUG_CHANNEL(enhmetafile);
29
30 /**********************************************************************
31  *           EMFDRV_MoveTo
32  */
33 BOOL
34 EMFDRV_MoveTo(PHYSDEV dev, INT x, INT y)
35 {
36     EMRMOVETOEX emr;
37
38     emr.emr.iType = EMR_MOVETOEX;
39     emr.emr.nSize = sizeof(emr);
40     emr.ptl.x = x;
41     emr.ptl.y = y;
42
43     return EMFDRV_WriteRecord( dev, &emr.emr );
44 }
45
46 /***********************************************************************
47  *           EMFDRV_LineTo
48  */
49 BOOL
50 EMFDRV_LineTo( PHYSDEV dev, INT x, INT y )
51 {
52     EMRLINETO emr;
53     RECTL bounds;
54     EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE *)dev;
55     DC *dc = physDev->dc;
56
57     emr.emr.iType = EMR_LINETO;
58     emr.emr.nSize = sizeof(emr);
59     emr.ptl.x = x;
60     emr.ptl.y = y;
61
62     if(!EMFDRV_WriteRecord( dev, &emr.emr ))
63         return FALSE;
64
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);
69
70     EMFDRV_UpdateBBox( dev, &bounds );
71
72     return TRUE;
73 }
74
75
76 /***********************************************************************
77  *           EMFDRV_ArcChordPie
78  */
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 )
82 {
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;
90
91     if(left == right || top == bottom) return FALSE;
92
93     if(left > right) {temp = left; left = right; right = temp;}
94     if(top > bottom) {temp = top; top = bottom; bottom = temp;}
95
96     if(dc->GraphicsMode == GM_COMPATIBLE) {
97         right--;
98         bottom--;
99     }
100
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;
111
112
113     /* Now calculate the BBox */
114     xCentre = (left + right + 1) / 2;
115     yCentre = (top + bottom + 1) / 2;
116
117     xstart -= xCentre;
118     ystart -= yCentre;
119     xend   -= xCentre;
120     yend   -= yCentre;
121
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);
125
126     /* These are the intercepts of the start/end lines with the arc */
127
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;
132
133     if(angleStart < 0) angleStart += 2 * M_PI;
134     if(angleEnd < 0) angleEnd += 2 * M_PI;
135     if(angleEnd < angleStart) angleEnd += 2 * M_PI;
136
137     bounds.left   = min(xinterStart, xinterEnd);
138     bounds.top    = min(yinterStart, yinterEnd);
139     bounds.right  = max(xinterStart, xinterEnd);
140     bounds.bottom = max(yinterStart, yinterEnd);
141     
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;
147
148         /* the arc touches the rectangle at the start of quadrant i, so adjust
149            BBox to reflect this. */
150
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;
164         }
165     }
166
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;
173     }
174     if(!EMFDRV_WriteRecord( dev, &emr.emr ))
175         return FALSE;
176     EMFDRV_UpdateBBox( dev, &bounds );
177     return TRUE;
178 }
179
180
181 /***********************************************************************
182  *           EMFDRV_Arc
183  */
184 BOOL
185 EMFDRV_Arc( PHYSDEV dev, INT left, INT top, INT right, INT bottom,
186             INT xstart, INT ystart, INT xend, INT yend )
187 {
188     return EMFDRV_ArcChordPie( dev, left, top, right, bottom, xstart, ystart,
189                                xend, yend, EMR_ARC );
190 }
191
192 /***********************************************************************
193  *           EMFDRV_Pie
194  */
195 BOOL
196 EMFDRV_Pie( PHYSDEV dev, INT left, INT top, INT right, INT bottom,
197             INT xstart, INT ystart, INT xend, INT yend )
198 {
199     return EMFDRV_ArcChordPie( dev, left, top, right, bottom, xstart, ystart,
200                                xend, yend, EMR_PIE );
201 }
202
203
204 /***********************************************************************
205  *           EMFDRV_Chord
206  */
207 BOOL
208 EMFDRV_Chord( PHYSDEV dev, INT left, INT top, INT right, INT bottom,
209              INT xstart, INT ystart, INT xend, INT yend )
210 {
211     return EMFDRV_ArcChordPie( dev, left, top, right, bottom, xstart, ystart,
212                                xend, yend, EMR_CHORD );
213 }
214
215 /***********************************************************************
216  *           EMFDRV_Ellipse
217  */
218 BOOL
219 EMFDRV_Ellipse( PHYSDEV dev, INT left, INT top, INT right, INT bottom )
220 {
221     EMRELLIPSE emr;
222     INT temp;
223     EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE *)dev;
224     DC *dc = physDev->dc;
225
226     TRACE("%d,%d - %d,%d\n", left, top, right, bottom);
227
228     if(left == right || top == bottom) return FALSE;
229
230     if(left > right) {temp = left; left = right; right = temp;}
231     if(top > bottom) {temp = top; top = bottom; bottom = temp;}
232
233     if(dc->GraphicsMode == GM_COMPATIBLE) {
234         right--;
235         bottom--;
236     }
237
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;
244
245     EMFDRV_UpdateBBox( dev, &emr.rclBox );
246     return EMFDRV_WriteRecord( dev, &emr.emr );
247 }
248
249 /***********************************************************************
250  *           EMFDRV_Rectangle
251  */
252 BOOL
253 EMFDRV_Rectangle(PHYSDEV dev, INT left, INT top, INT right, INT bottom)
254 {
255     EMRRECTANGLE emr;
256     INT temp;
257     EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE *)dev;
258     DC *dc = physDev->dc;
259
260     TRACE("%d,%d - %d,%d\n", left, top, right, bottom);
261
262     if(left == right || top == bottom) return FALSE;
263
264     if(left > right) {temp = left; left = right; right = temp;}
265     if(top > bottom) {temp = top; top = bottom; bottom = temp;}
266
267     if(dc->GraphicsMode == GM_COMPATIBLE) {
268         right--;
269         bottom--;
270     }
271
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;
278
279     EMFDRV_UpdateBBox( dev, &emr.rclBox );
280     return EMFDRV_WriteRecord( dev, &emr.emr );
281 }
282
283 /***********************************************************************
284  *           EMFDRV_RoundRect
285  */
286 BOOL 
287 EMFDRV_RoundRect( PHYSDEV dev, INT left, INT top, INT right,
288                   INT bottom, INT ell_width, INT ell_height )
289 {
290     EMRROUNDRECT emr;
291     INT temp;
292     EMFDRV_PDEVICE *physDev = (EMFDRV_PDEVICE *)dev;
293     DC *dc = physDev->dc;
294
295     if(left == right || top == bottom) return FALSE;
296
297     if(left > right) {temp = left; left = right; right = temp;}
298     if(top > bottom) {temp = top; top = bottom; bottom = temp;}
299
300     if(dc->GraphicsMode == GM_COMPATIBLE) {
301         right--;
302         bottom--;
303     }
304
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;
313
314     EMFDRV_UpdateBBox( dev, &emr.rclBox );
315     return EMFDRV_WriteRecord( dev, &emr.emr );
316 }
317
318 /***********************************************************************
319  *           EMFDRV_SetPixel
320  */
321 COLORREF
322 EMFDRV_SetPixel( PHYSDEV dev, INT x, INT y, COLORREF color )
323 {
324   return TRUE;
325 }
326
327
328 /**********************************************************************
329  *          EMFDRV_Polylinegon
330  *
331  * Helper for EMFDRV_Poly{line|gon}
332  */
333 static BOOL
334 EMFDRV_Polylinegon( PHYSDEV dev, const POINT* pt, INT count, DWORD iType )
335 {
336     EMRPOLYLINE *emr;
337     DWORD size;
338     INT i;
339     BOOL ret;
340
341     size = sizeof(EMRPOLYLINE) + sizeof(POINTL) * (count - 1);
342
343     emr = HeapAlloc( GetProcessHeap(), 0, size );
344     emr->emr.iType = iType;
345     emr->emr.nSize = size;
346     
347     emr->rclBounds.left = emr->rclBounds.right = pt[0].x;
348     emr->rclBounds.top = emr->rclBounds.bottom = pt[0].y;
349
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;
359     }
360
361     emr->cptl = count;
362     memcpy(emr->aptl, pt, count * sizeof(POINTL));
363
364     ret = EMFDRV_WriteRecord( dev, &emr->emr );
365     if(ret)
366         EMFDRV_UpdateBBox( dev, &emr->rclBounds );
367     HeapFree( GetProcessHeap(), 0, emr );
368     return ret;
369 }
370
371
372 /**********************************************************************
373  *          EMFDRV_Polyline
374  */
375 BOOL
376 EMFDRV_Polyline( PHYSDEV dev, const POINT* pt, INT count )
377 {
378     return EMFDRV_Polylinegon( dev, pt, count, EMR_POLYLINE );
379 }
380
381 /**********************************************************************
382  *          EMFDRV_Polygon
383  */
384 BOOL
385 EMFDRV_Polygon( PHYSDEV dev, const POINT* pt, INT count )
386 {
387     if(count < 2) return FALSE;
388     return EMFDRV_Polylinegon( dev, pt, count, EMR_POLYGON );
389 }
390
391
392 /**********************************************************************
393  *          EMFDRV_PolyPolylinegon
394  *
395  * Helper for EMFDRV_PolyPoly{line|gon}
396  */
397 static BOOL 
398 EMFDRV_PolyPolylinegon( PHYSDEV dev, const POINT* pt, const INT* counts, UINT polys,
399                         DWORD iType)
400 {
401     EMRPOLYPOLYLINE *emr;
402     DWORD cptl = 0, poly, size, point;
403     RECTL bounds;
404     const POINT *pts;
405     BOOL ret;
406
407     bounds.left = bounds.right = pt[0].x;
408     bounds.top = bounds.bottom = pt[0].y;
409
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++;
419         }
420     }
421     
422     size = sizeof(EMRPOLYPOLYLINE) + (polys - 1) * sizeof(DWORD) + 
423       (cptl - 1) * sizeof(POINTL);
424
425     emr = HeapAlloc( GetProcessHeap(), 0, size );
426
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;
439 }
440
441 /**********************************************************************
442  *          EMFDRV_PolyPolyline
443  */
444 BOOL 
445 EMFDRV_PolyPolyline(PHYSDEV dev, const POINT* pt, const DWORD* counts, DWORD polys)
446 {
447     return EMFDRV_PolyPolylinegon( dev, pt, (INT *)counts, polys,
448                                    EMR_POLYPOLYLINE );
449 }
450
451 /**********************************************************************
452  *          EMFDRV_PolyPolygon
453  */
454 BOOL 
455 EMFDRV_PolyPolygon( PHYSDEV dev, const POINT* pt, const INT* counts, UINT polys )
456 {
457     return EMFDRV_PolyPolylinegon( dev, pt, counts, polys, EMR_POLYPOLYGON );
458 }
459
460
461 /**********************************************************************
462  *          EMFDRV_ExtFloodFill
463  */
464 BOOL 
465 EMFDRV_ExtFloodFill( PHYSDEV dev, INT x, INT y, COLORREF color, UINT fillType )
466 {
467     EMREXTFLOODFILL emr;
468
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;
475
476     return EMFDRV_WriteRecord( dev, &emr.emr );
477 }
478
479
480 /*********************************************************************
481  *          EMFDRV_FillRgn
482  */
483 BOOL EMFDRV_FillRgn( PHYSDEV dev, HRGN hrgn, HBRUSH hbrush )
484 {
485     EMRFILLRGN *emr;
486     DWORD size, rgnsize, index;
487     BOOL ret;
488
489     index = EMFDRV_CreateBrushIndirect( dev, hbrush );
490     if(!index) return FALSE;
491
492     rgnsize = GetRegionData( hrgn, 0, NULL );
493     size = rgnsize + sizeof(EMRFILLRGN) - 1;
494     emr = HeapAlloc( GetProcessHeap(), 0, size );
495
496     GetRegionData( hrgn, rgnsize, (RGNDATA *)&emr->RgnData );
497
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;
506     
507     ret = EMFDRV_WriteRecord( dev, &emr->emr );
508     if(ret)
509         EMFDRV_UpdateBBox( dev, &emr->rclBounds );
510     HeapFree( GetProcessHeap(), 0, emr );
511     return ret;
512 }
513 /*********************************************************************
514  *          EMFDRV_FrameRgn
515  */
516 BOOL EMFDRV_FrameRgn( PHYSDEV dev, HRGN hrgn, HBRUSH hbrush, INT width, INT height )
517 {
518     EMRFRAMERGN *emr;
519     DWORD size, rgnsize, index;
520     BOOL ret;
521
522     index = EMFDRV_CreateBrushIndirect( dev, hbrush );
523     if(!index) return FALSE;
524
525     rgnsize = GetRegionData( hrgn, 0, NULL );
526     size = rgnsize + sizeof(EMRFRAMERGN) - 1;
527     emr = HeapAlloc( GetProcessHeap(), 0, size );
528
529     GetRegionData( hrgn, rgnsize, (RGNDATA *)&emr->RgnData );
530
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;
541
542     ret = EMFDRV_WriteRecord( dev, &emr->emr );
543     if(ret)
544         EMFDRV_UpdateBBox( dev, &emr->rclBounds );
545     HeapFree( GetProcessHeap(), 0, emr );
546     return ret;
547 }
548
549 /*********************************************************************
550  *          EMFDRV_PaintInvertRgn
551  *
552  * Helper for EMFDRV_{Paint|Invert}Rgn
553  */
554 static BOOL EMFDRV_PaintInvertRgn( PHYSDEV dev, HRGN hrgn, DWORD iType )
555 {
556     EMRINVERTRGN *emr;
557     DWORD size, rgnsize;
558     BOOL ret;
559
560
561     rgnsize = GetRegionData( hrgn, 0, NULL );
562     size = rgnsize + sizeof(EMRINVERTRGN) - 1;
563     emr = HeapAlloc( GetProcessHeap(), 0, size );
564
565     GetRegionData( hrgn, rgnsize, (RGNDATA *)&emr->RgnData );
566
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;
574     
575     ret = EMFDRV_WriteRecord( dev, &emr->emr );
576     if(ret)
577         EMFDRV_UpdateBBox( dev, &emr->rclBounds );
578     HeapFree( GetProcessHeap(), 0, emr );
579     return ret;
580 }
581
582 /**********************************************************************
583  *          EMFDRV_PaintRgn
584  */
585 BOOL
586 EMFDRV_PaintRgn( PHYSDEV dev, HRGN hrgn )
587 {
588     return EMFDRV_PaintInvertRgn( dev, hrgn, EMR_PAINTRGN );
589 }
590
591 /**********************************************************************
592  *          EMFDRV_InvertRgn
593  */
594 BOOL
595 EMFDRV_InvertRgn( PHYSDEV dev, HRGN hrgn )
596 {
597     return EMFDRV_PaintInvertRgn( dev, hrgn, EMR_INVERTRGN );
598 }
599
600 /**********************************************************************
601  *          EMFDRV_SetBkColor
602  */
603 COLORREF
604 EMFDRV_SetBkColor( PHYSDEV dev, COLORREF color )
605 {
606     EMRSETBKCOLOR emr;
607
608     emr.emr.iType = EMR_SETBKCOLOR;
609     emr.emr.nSize = sizeof(emr);
610     emr.crColor = color;
611
612     return EMFDRV_WriteRecord( dev, &emr.emr ) ? color : CLR_INVALID;
613 }
614
615
616 /**********************************************************************
617  *          EMFDRV_SetTextColor
618  */
619 COLORREF
620 EMFDRV_SetTextColor( PHYSDEV dev, COLORREF color )
621 {
622     EMRSETTEXTCOLOR emr;
623
624     emr.emr.iType = EMR_SETTEXTCOLOR;
625     emr.emr.nSize = sizeof(emr);
626     emr.crColor = color;
627
628     return EMFDRV_WriteRecord( dev, &emr.emr ) ? color : CLR_INVALID;
629 }