dinput: BuildActionMap and SetActionMap stubs for generic joystick.
[wine] / dlls / wineps.drv / graphics.c
1 /*
2  *      PostScript driver graphics functions
3  *
4  *      Copyright 1998  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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #include "config.h"
22
23 #include <stdio.h>
24 #include <string.h>
25 #include <math.h>
26 #if defined(HAVE_FLOAT_H)
27 # include <float.h>
28 #endif
29 #if !defined(PI)
30 # define PI M_PI
31 #endif
32 #include "psdrv.h"
33 #include "wine/debug.h"
34
35 WINE_DEFAULT_DEBUG_CHANNEL(psdrv);
36
37 /***********************************************************************
38  *           PSDRV_XWStoDS
39  *
40  * Performs a world-to-viewport transformation on the specified width.
41  */
42 INT PSDRV_XWStoDS( PHYSDEV dev, INT width )
43 {
44     POINT pt[2];
45
46     pt[0].x = 0;
47     pt[0].y = 0;
48     pt[1].x = width;
49     pt[1].y = 0;
50     LPtoDP( dev->hdc, pt, 2 );
51     return pt[1].x - pt[0].x;
52 }
53
54 /***********************************************************************
55  *           PSDRV_DrawLine
56  */
57 static void PSDRV_DrawLine( PHYSDEV dev )
58 {
59     PSDRV_PDEVICE *physDev = get_psdrv_dev( dev );
60
61     if(physDev->pathdepth)
62         return;
63
64     if (physDev->pen.style == PS_NULL)
65         PSDRV_WriteNewPath(dev);
66     else
67         PSDRV_WriteStroke(dev);
68 }
69
70 /***********************************************************************
71  *           PSDRV_LineTo
72  */
73 BOOL CDECL PSDRV_LineTo(PHYSDEV dev, INT x, INT y)
74 {
75     POINT pt[2];
76
77     TRACE("%d %d\n", x, y);
78
79     GetCurrentPositionEx( dev->hdc, pt );
80     pt[1].x = x;
81     pt[1].y = y;
82     LPtoDP( dev->hdc, pt, 2 );
83
84     PSDRV_SetPen(dev);
85
86     PSDRV_SetClip(dev);
87     PSDRV_WriteMoveTo(dev, pt[0].x, pt[0].y );
88     PSDRV_WriteLineTo(dev, pt[1].x, pt[1].y );
89     PSDRV_DrawLine(dev);
90     PSDRV_ResetClip(dev);
91
92     return TRUE;
93 }
94
95
96 /***********************************************************************
97  *           PSDRV_Rectangle
98  */
99 BOOL CDECL PSDRV_Rectangle( PHYSDEV dev, INT left, INT top, INT right, INT bottom )
100 {
101     PSDRV_PDEVICE *physDev = get_psdrv_dev( dev );
102     RECT rect;
103
104     TRACE("%d %d - %d %d\n", left, top, right, bottom);
105
106     rect.left = left;
107     rect.top = top;
108     rect.right = right;
109     rect.bottom = bottom;
110     LPtoDP( dev->hdc, (POINT *)&rect, 2 );
111
112     /* Windows does something truly hacky here.  If we're in passthrough mode
113        and our rop is R2_NOP, then we output the string below.  This is used in
114        Office 2k when inserting eps files */
115     if(physDev->job.in_passthrough && !physDev->job.had_passthrough_rect && GetROP2(dev->hdc) == R2_NOP) {
116       char buf[256];
117       sprintf(buf, "N %d %d %d %d B\n", rect.right - rect.left, rect.bottom - rect.top, rect.left, rect.top);
118       write_spool(dev, buf, strlen(buf));
119       physDev->job.had_passthrough_rect = TRUE;
120       return TRUE;
121     }
122
123     PSDRV_SetPen(dev);
124
125     PSDRV_SetClip(dev);
126     PSDRV_WriteRectangle(dev, rect.left, rect.top, rect.right - rect.left,
127                          rect.bottom - rect.top );
128     PSDRV_Brush(dev,0);
129     PSDRV_DrawLine(dev);
130     PSDRV_ResetClip(dev);
131     return TRUE;
132 }
133
134
135 /***********************************************************************
136  *           PSDRV_RoundRect
137  */
138 BOOL CDECL PSDRV_RoundRect( PHYSDEV dev, INT left, INT top, INT right,
139                             INT bottom, INT ell_width, INT ell_height )
140 {
141     RECT rect[2];
142
143     rect[0].left   = left;
144     rect[0].top    = top;
145     rect[0].right  = right;
146     rect[0].bottom = bottom;
147     rect[1].left   = 0;
148     rect[1].top    = 0;
149     rect[1].right  = ell_width;
150     rect[1].bottom = ell_height;
151     LPtoDP( dev->hdc, (POINT *)rect, 4 );
152
153     left   = rect[0].left;
154     top    = rect[0].top;
155     right  = rect[0].right;
156     bottom = rect[0].bottom;
157     if (left > right) { INT tmp = left; left = right; right = tmp; }
158     if (top > bottom) { INT tmp = top; top = bottom; bottom = tmp; }
159
160     ell_width  = rect[1].right - rect[1].left;
161     ell_height = rect[1].bottom - rect[1].top;
162     if (ell_width > right - left) ell_width = right - left;
163     if (ell_height > bottom - top) ell_height = bottom - top;
164
165     PSDRV_WriteSpool(dev, "%RoundRect\n",11);
166     PSDRV_SetPen(dev);
167
168     PSDRV_SetClip(dev);
169     PSDRV_WriteMoveTo( dev, left, top + ell_height/2 );
170     PSDRV_WriteArc( dev, left + ell_width/2, top + ell_height/2, ell_width,
171                     ell_height, 90.0, 180.0);
172     PSDRV_WriteLineTo( dev, right - ell_width/2, top );
173     PSDRV_WriteArc( dev, right - ell_width/2, top + ell_height/2, ell_width,
174                     ell_height, 0.0, 90.0);
175     PSDRV_WriteLineTo( dev, right, bottom - ell_height/2 );
176     PSDRV_WriteArc( dev, right - ell_width/2, bottom - ell_height/2, ell_width,
177                     ell_height, -90.0, 0.0);
178     PSDRV_WriteLineTo( dev, right - ell_width/2, bottom);
179     PSDRV_WriteArc( dev, left + ell_width/2, bottom - ell_height/2, ell_width,
180                     ell_height, 180.0, -90.0);
181     PSDRV_WriteClosePath( dev );
182
183     PSDRV_Brush(dev,0);
184     PSDRV_DrawLine(dev);
185     PSDRV_ResetClip(dev);
186     return TRUE;
187 }
188
189 /***********************************************************************
190  *           PSDRV_DrawArc
191  *
192  * Does the work of Arc, Chord and Pie. lines is 0, 1 or 2 respectively.
193  */
194 static BOOL PSDRV_DrawArc( PHYSDEV dev, INT left, INT top,
195                            INT right, INT bottom, INT xstart, INT ystart,
196                            INT xend, INT yend, int lines )
197 {
198     INT x, y, h, w;
199     double start_angle, end_angle, ratio;
200     RECT rect;
201     POINT start, end;
202
203     rect.left = left;
204     rect.top = top;
205     rect.right = right;
206     rect.bottom = bottom;
207     LPtoDP( dev->hdc, (POINT *)&rect, 2 );
208     start.x = xstart;
209     start.y = ystart;
210     end.x = xend;
211     end.y = yend;
212     LPtoDP( dev->hdc, &start, 1 );
213     LPtoDP( dev->hdc, &end, 1 );
214
215     x = (rect.left + rect.right) / 2;
216     y = (rect.top + rect.bottom) / 2;
217     w = rect.right - rect.left;
218     h = rect.bottom - rect.top;
219
220     if(w < 0) w = -w;
221     if(h < 0) h = -h;
222     ratio = ((double)w)/h;
223
224     /* angle is the angle after the rectangle is transformed to a square and is
225        measured anticlockwise from the +ve x-axis */
226
227     start_angle = atan2((double)(y - start.y) * ratio, (double)(start.x - x));
228     end_angle = atan2((double)(y - end.y) * ratio, (double)(end.x - x));
229
230     start_angle *= 180.0 / PI;
231     end_angle *= 180.0 / PI;
232
233     PSDRV_WriteSpool(dev,"%DrawArc\n", 9);
234     PSDRV_SetPen(dev);
235
236     PSDRV_SetClip(dev);
237     if(lines == 2) /* pie */
238         PSDRV_WriteMoveTo(dev, x, y);
239     else
240         PSDRV_WriteNewPath( dev );
241
242     PSDRV_WriteArc(dev, x, y, w, h, start_angle, end_angle);
243     if(lines == 1 || lines == 2) { /* chord or pie */
244         PSDRV_WriteClosePath(dev);
245         PSDRV_Brush(dev,0);
246     }
247     PSDRV_DrawLine(dev);
248     PSDRV_ResetClip(dev);
249
250     return TRUE;
251 }
252
253
254 /***********************************************************************
255  *           PSDRV_Arc
256  */
257 BOOL CDECL PSDRV_Arc( PHYSDEV dev, INT left, INT top, INT right, INT bottom,
258                       INT xstart, INT ystart, INT xend, INT yend )
259 {
260     return PSDRV_DrawArc( dev, left, top, right, bottom, xstart, ystart, xend, yend, 0 );
261 }
262
263 /***********************************************************************
264  *           PSDRV_Chord
265  */
266 BOOL CDECL PSDRV_Chord( PHYSDEV dev, INT left, INT top, INT right, INT bottom,
267                         INT xstart, INT ystart, INT xend, INT yend )
268 {
269     return PSDRV_DrawArc( dev, left, top, right, bottom, xstart, ystart, xend, yend, 1 );
270 }
271
272
273 /***********************************************************************
274  *           PSDRV_Pie
275  */
276 BOOL CDECL PSDRV_Pie( PHYSDEV dev, INT left, INT top, INT right, INT bottom,
277                       INT xstart, INT ystart, INT xend, INT yend )
278 {
279     return PSDRV_DrawArc( dev, left, top, right, bottom, xstart, ystart, xend, yend, 2 );
280 }
281
282
283 /***********************************************************************
284  *           PSDRV_Ellipse
285  */
286 BOOL CDECL PSDRV_Ellipse( PHYSDEV dev, INT left, INT top, INT right, INT bottom)
287 {
288     INT x, y, w, h;
289     RECT rect;
290
291     TRACE("%d %d - %d %d\n", left, top, right, bottom);
292
293     rect.left   = left;
294     rect.top    = top;
295     rect.right  = right;
296     rect.bottom = bottom;
297     LPtoDP( dev->hdc, (POINT *)&rect, 2 );
298
299     x = (rect.left + rect.right) / 2;
300     y = (rect.top + rect.bottom) / 2;
301     w = rect.right - rect.left;
302     h = rect.bottom - rect.top;
303
304     PSDRV_WriteSpool(dev, "%Ellipse\n", 9);
305     PSDRV_SetPen(dev);
306
307     PSDRV_SetClip(dev);
308     PSDRV_WriteNewPath(dev);
309     PSDRV_WriteArc(dev, x, y, w, h, 0.0, 360.0);
310     PSDRV_WriteClosePath(dev);
311     PSDRV_Brush(dev,0);
312     PSDRV_DrawLine(dev);
313     PSDRV_ResetClip(dev);
314     return TRUE;
315 }
316
317
318 /***********************************************************************
319  *           PSDRV_PolyPolyline
320  */
321 BOOL CDECL PSDRV_PolyPolyline( PHYSDEV dev, const POINT* pts, const DWORD* counts,
322                                DWORD polylines )
323 {
324     DWORD polyline, line, total;
325     POINT *dev_pts, *pt;
326
327     TRACE("\n");
328
329     for (polyline = total = 0; polyline < polylines; polyline++) total += counts[polyline];
330     if (!(dev_pts = HeapAlloc( GetProcessHeap(), 0, total * sizeof(*dev_pts) ))) return FALSE;
331     memcpy( dev_pts, pts, total * sizeof(*dev_pts) );
332     LPtoDP( dev->hdc, dev_pts, total );
333
334     pt = dev_pts;
335
336     PSDRV_WriteSpool(dev, "%PolyPolyline\n",14);
337     PSDRV_SetPen(dev);
338     PSDRV_SetClip(dev);
339
340     for(polyline = 0; polyline < polylines; polyline++) {
341         PSDRV_WriteMoveTo(dev, pt->x, pt->y);
342         pt++;
343         for(line = 1; line < counts[polyline]; line++, pt++)
344             PSDRV_WriteLineTo(dev, pt->x, pt->y);
345     }
346     HeapFree( GetProcessHeap(), 0, dev_pts );
347
348     PSDRV_DrawLine(dev);
349     PSDRV_ResetClip(dev);
350     return TRUE;
351 }
352
353
354 /***********************************************************************
355  *           PSDRV_Polyline
356  */
357 BOOL CDECL PSDRV_Polyline( PHYSDEV dev, const POINT* pt, INT count )
358 {
359     return PSDRV_PolyPolyline( dev, pt, (LPDWORD) &count, 1 );
360 }
361
362
363 /***********************************************************************
364  *           PSDRV_PolyPolygon
365  */
366 BOOL CDECL PSDRV_PolyPolygon( PHYSDEV dev, const POINT* pts, const INT* counts,
367                               UINT polygons )
368 {
369     DWORD polygon, total;
370     INT line;
371     POINT *dev_pts, *pt;
372
373     TRACE("\n");
374
375     for (polygon = total = 0; polygon < polygons; polygon++) total += counts[polygon];
376     if (!(dev_pts = HeapAlloc( GetProcessHeap(), 0, total * sizeof(*dev_pts) ))) return FALSE;
377     memcpy( dev_pts, pts, total * sizeof(*dev_pts) );
378     LPtoDP( dev->hdc, dev_pts, total );
379
380     pt = dev_pts;
381
382     PSDRV_WriteSpool(dev, "%PolyPolygon\n",13);
383     PSDRV_SetPen(dev);
384     PSDRV_SetClip(dev);
385     PSDRV_WriteNewPath(dev);
386
387     for(polygon = 0; polygon < polygons; polygon++) {
388         PSDRV_WriteMoveTo(dev, pt->x, pt->y);
389         pt++;
390         for(line = 1; line < counts[polygon]; line++, pt++)
391             PSDRV_WriteLineTo(dev, pt->x, pt->y);
392         PSDRV_WriteClosePath(dev);
393     }
394     HeapFree( GetProcessHeap(), 0, dev_pts );
395
396     if(GetPolyFillMode( dev->hdc ) == ALTERNATE)
397         PSDRV_Brush(dev, 1);
398     else /* WINDING */
399         PSDRV_Brush(dev, 0);
400
401     PSDRV_DrawLine(dev);
402     PSDRV_ResetClip(dev);
403     return TRUE;
404 }
405
406
407 /***********************************************************************
408  *           PSDRV_Polygon
409  */
410 BOOL CDECL PSDRV_Polygon( PHYSDEV dev, const POINT* pt, INT count )
411 {
412      return PSDRV_PolyPolygon( dev, pt, &count, 1 );
413 }
414
415
416 /***********************************************************************
417  *           PSDRV_SetPixel
418  */
419 COLORREF CDECL PSDRV_SetPixel( PHYSDEV dev, INT x, INT y, COLORREF color )
420 {
421     PSCOLOR pscolor;
422     POINT pt;
423
424     pt.x = x;
425     pt.y = y;
426     LPtoDP( dev->hdc, &pt, 1 );
427
428     PSDRV_SetClip(dev);
429     /* we bracket the setcolor in gsave/grestore so that we don't trash
430        the current pen colour */
431     PSDRV_WriteGSave(dev);
432     PSDRV_WriteRectangle( dev, pt.x, pt.y, 0, 0 );
433     PSDRV_CreateColor( dev, &pscolor, color );
434     PSDRV_WriteSetColor( dev, &pscolor );
435     PSDRV_WriteFill( dev );
436     PSDRV_WriteGRestore(dev);
437     PSDRV_ResetClip(dev);
438     return color;
439 }
440
441 /***********************************************************************
442  *           PSDRV_PaintRgn
443  */
444 BOOL CDECL PSDRV_PaintRgn( PHYSDEV dev, HRGN hrgn )
445 {
446     RGNDATA *rgndata = NULL;
447     RECT *pRect;
448     DWORD size, i;
449
450     TRACE("hdc=%p\n", dev->hdc);
451
452     size = GetRegionData(hrgn, 0, NULL);
453     rgndata = HeapAlloc( GetProcessHeap(), 0, size );
454     if(!rgndata) {
455         ERR("Can't allocate buffer\n");
456         return FALSE;
457     }
458     
459     GetRegionData(hrgn, size, rgndata);
460     if (rgndata->rdh.nCount == 0)
461         goto end;
462
463     LPtoDP(dev->hdc, (POINT*)rgndata->Buffer, rgndata->rdh.nCount * 2);
464
465     PSDRV_SetClip(dev);
466     PSDRV_WriteNewPath(dev);
467     for(i = 0, pRect = (RECT*)rgndata->Buffer; i < rgndata->rdh.nCount; i++, pRect++)
468         PSDRV_WriteRectangle(dev, pRect->left, pRect->top, pRect->right - pRect->left, pRect->bottom - pRect->top);
469
470     PSDRV_Brush(dev, 0);
471     PSDRV_ResetClip(dev);
472
473  end:
474     HeapFree(GetProcessHeap(), 0, rgndata);
475     return TRUE;
476 }