Fixed a number of warnings concerning the matching of the printf
[wine] / graphics / x11drv / graphics.c
1 /*
2  * X11 graphics driver graphics functions
3  *
4  * Copyright 1993,1994 Alexandre Julliard
5  */
6
7 /*
8  * FIXME: none of these functions obey the GM_ADVANCED
9  * graphics mode
10  */
11
12 #include "config.h"
13
14 #ifndef X_DISPLAY_MISSING
15
16 #include <X11/Intrinsic.h>
17 #include "ts_xlib.h"
18 #include "ts_xutil.h"
19
20 #include <math.h>
21 #ifdef HAVE_FLOAT_H
22 # include <float.h>
23 #endif
24 #include <stdlib.h>
25 #ifndef PI
26 #define PI M_PI
27 #endif
28 #include <string.h>
29
30 #include "x11drv.h"
31 #include "x11font.h"
32 #include "bitmap.h"
33 #include "gdi.h"
34 #include "dc.h"
35 #include "monitor.h"
36 #include "bitmap.h"
37 #include "callback.h"
38 #include "metafile.h"
39 #include "palette.h"
40 #include "color.h"
41 #include "region.h"
42 #include "struct32.h"
43 #include "debug.h"
44 #include "xmalloc.h"
45
46 #define ABS(x)    ((x)<0?(-(x)):(x))
47
48   /* ROP code to GC function conversion */
49 const int X11DRV_XROPfunction[16] =
50 {
51     GXclear,        /* R2_BLACK */
52     GXnor,          /* R2_NOTMERGEPEN */
53     GXandInverted,  /* R2_MASKNOTPEN */
54     GXcopyInverted, /* R2_NOTCOPYPEN */
55     GXandReverse,   /* R2_MASKPENNOT */
56     GXinvert,       /* R2_NOT */
57     GXxor,          /* R2_XORPEN */
58     GXnand,         /* R2_NOTMASKPEN */
59     GXand,          /* R2_MASKPEN */
60     GXequiv,        /* R2_NOTXORPEN */
61     GXnoop,         /* R2_NOP */
62     GXorInverted,   /* R2_MERGENOTPEN */
63     GXcopy,         /* R2_COPYPEN */
64     GXorReverse,    /* R2_MERGEPENNOT */
65     GXor,           /* R2_MERGEPEN */
66     GXset           /* R2_WHITE */
67 };
68
69
70 /***********************************************************************
71  *           X11DRV_SetupGCForPatBlt
72  *
73  * Setup the GC for a PatBlt operation using current brush.
74  * If fMapColors is TRUE, X pixels are mapped to Windows colors.
75  * Return FALSE if brush is BS_NULL, TRUE otherwise.
76  */
77 BOOL32 X11DRV_SetupGCForPatBlt( DC * dc, GC gc, BOOL32 fMapColors )
78 {
79     XGCValues val;
80     unsigned long mask;
81     Pixmap pixmap = 0;
82     X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
83
84     if (physDev->brush.style == BS_NULL) return FALSE;
85     if (physDev->brush.pixel == -1)
86     {
87         /* Special case used for monochrome pattern brushes.
88          * We need to swap foreground and background because
89          * Windows does it the wrong way...
90          */
91         val.foreground = physDev->backgroundPixel;
92         val.background = physDev->textPixel;
93     }
94     else
95     {
96         val.foreground = physDev->brush.pixel;
97         val.background = physDev->backgroundPixel;
98     }
99     if (fMapColors && COLOR_PixelToPalette)
100     {
101         val.foreground = COLOR_PixelToPalette[val.foreground];
102         val.background = COLOR_PixelToPalette[val.background];
103     }
104
105     if (dc->w.flags & DC_DIRTY) CLIPPING_UpdateGCRegion(dc);
106
107     val.function = X11DRV_XROPfunction[dc->w.ROPmode-1];
108     /*
109     ** Let's replace GXinvert by GXxor with (black xor white)
110     ** This solves the selection color and leak problems in excel
111     ** FIXME : Let's do that only if we work with X-pixels, not with Win-pixels
112     */
113     if (val.function == GXinvert)
114         {
115         val.foreground = BlackPixelOfScreen(X11DRV_GetXScreen()) ^ WhitePixelOfScreen(X11DRV_GetXScreen());
116         val.function = GXxor;
117         }
118     val.fill_style = physDev->brush.fillStyle;
119     switch(val.fill_style)
120     {
121     case FillStippled:
122     case FillOpaqueStippled:
123         if (dc->w.backgroundMode==OPAQUE) val.fill_style = FillOpaqueStippled;
124         val.stipple = physDev->brush.pixmap;
125         mask = GCStipple;
126         break;
127
128     case FillTiled:
129         if (fMapColors && COLOR_PixelToPalette)
130         {
131             register int x, y;
132             XImage *image;
133             EnterCriticalSection( &X11DRV_CritSection );
134             pixmap = XCreatePixmap( display, 
135                                     X11DRV_GetXRootWindow(), 
136                                     8, 8, 
137                                     MONITOR_GetDepth(&MONITOR_PrimaryMonitor) );
138             image = XGetImage( display, physDev->brush.pixmap, 0, 0, 8, 8,
139                                AllPlanes, ZPixmap );
140             for (y = 0; y < 8; y++)
141                 for (x = 0; x < 8; x++)
142                     XPutPixel( image, x, y,
143                                COLOR_PixelToPalette[XGetPixel( image, x, y)] );
144             XPutImage( display, pixmap, gc, image, 0, 0, 0, 0, 8, 8 );
145             XDestroyImage( image );
146             LeaveCriticalSection( &X11DRV_CritSection );
147             val.tile = pixmap;
148         }
149         else val.tile = physDev->brush.pixmap;
150         mask = GCTile;
151         break;
152
153     default:
154         mask = 0;
155         break;
156     }
157     val.ts_x_origin = dc->w.DCOrgX + dc->w.brushOrgX;
158     val.ts_y_origin = dc->w.DCOrgY + dc->w.brushOrgY;
159     val.fill_rule = (dc->w.polyFillMode==WINDING) ? WindingRule : EvenOddRule;
160     TSXChangeGC( display, gc, 
161                GCFunction | GCForeground | GCBackground | GCFillStyle |
162                GCFillRule | GCTileStipXOrigin | GCTileStipYOrigin | mask,
163                &val );
164     if (pixmap) TSXFreePixmap( display, pixmap );
165     return TRUE;
166 }
167
168
169 /***********************************************************************
170  *           X11DRV_SetupGCForBrush
171  *
172  * Setup physDev->gc for drawing operations using current brush.
173  * Return FALSE if brush is BS_NULL, TRUE otherwise.
174  */
175 BOOL32 X11DRV_SetupGCForBrush( DC * dc )
176 {
177     X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
178     return X11DRV_SetupGCForPatBlt( dc, physDev->gc, FALSE );
179 }
180
181
182 /***********************************************************************
183  *           X11DRV_SetupGCForPen
184  *
185  * Setup physDev->gc for drawing operations using current pen.
186  * Return FALSE if pen is PS_NULL, TRUE otherwise.
187  */
188 BOOL32 X11DRV_SetupGCForPen( DC * dc )
189 {
190     XGCValues val;
191     X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
192
193     if (physDev->pen.style == PS_NULL) return FALSE;
194
195     if (dc->w.flags & DC_DIRTY) CLIPPING_UpdateGCRegion(dc); 
196
197     switch (dc->w.ROPmode)
198     {
199     case R2_BLACK :
200         val.foreground = BlackPixelOfScreen( X11DRV_GetXScreen() );
201         val.function = GXcopy;
202         break;
203     case R2_WHITE :
204         val.foreground = WhitePixelOfScreen( X11DRV_GetXScreen() );
205         val.function = GXcopy;
206         break;
207     case R2_XORPEN :
208         val.foreground = physDev->pen.pixel;
209         /* It is very unlikely someone wants to XOR with 0 */
210         /* This fixes the rubber-drawings in paintbrush */
211         if (val.foreground == 0)
212             val.foreground = BlackPixelOfScreen( X11DRV_GetXScreen() )
213                             ^ WhitePixelOfScreen( X11DRV_GetXScreen() );
214         val.function = GXxor;
215         break;
216     default :
217         val.foreground = physDev->pen.pixel;
218         val.function   = X11DRV_XROPfunction[dc->w.ROPmode-1];
219     }
220     val.background = physDev->backgroundPixel;
221     val.fill_style = FillSolid;
222     if ((physDev->pen.style!=PS_SOLID) && (physDev->pen.style!=PS_INSIDEFRAME))
223     {
224         TSXSetDashes( display, physDev->gc, 0, physDev->pen.dashes,
225                       physDev->pen.dash_len );
226         val.line_style = (dc->w.backgroundMode == OPAQUE) ?
227                               LineDoubleDash : LineOnOffDash;
228     }
229     else val.line_style = LineSolid;
230     val.line_width = physDev->pen.width;
231     if (val.line_width <= 1) {
232         val.cap_style = CapNotLast;
233     } else {
234         switch (physDev->pen.endcap)
235         {
236         case PS_ENDCAP_SQUARE:
237             val.cap_style = CapProjecting;
238             break;
239         case PS_ENDCAP_FLAT:
240             val.cap_style = CapButt;
241             break;
242         case PS_ENDCAP_ROUND:
243         default:
244             val.cap_style = CapRound;
245         }
246     }
247     switch (physDev->pen.linejoin)
248     {
249     case PS_JOIN_BEVEL:
250         val.join_style = JoinBevel;
251         break;
252     case PS_JOIN_MITER:
253         val.join_style = JoinMiter;
254         break;
255     case PS_JOIN_ROUND:
256     default:
257         val.join_style = JoinRound;
258     }
259     TSXChangeGC( display, physDev->gc, 
260                GCFunction | GCForeground | GCBackground | GCLineWidth |
261                GCLineStyle | GCCapStyle | GCJoinStyle | GCFillStyle, &val );
262     return TRUE;
263 }
264
265
266 /***********************************************************************
267  *           X11DRV_SetupGCForText
268  *
269  * Setup physDev->gc for text drawing operations.
270  * Return FALSE if the font is null, TRUE otherwise.
271  */
272 BOOL32 X11DRV_SetupGCForText( DC * dc )
273 {
274     X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
275     XFontStruct* xfs = XFONT_GetFontStruct( physDev->font );
276
277     if( xfs )
278     {
279         XGCValues val;
280
281         if (dc->w.flags & DC_DIRTY) CLIPPING_UpdateGCRegion(dc);
282
283         val.function   = GXcopy;  /* Text is always GXcopy */
284         val.foreground = physDev->textPixel;
285         val.background = physDev->backgroundPixel;
286         val.fill_style = FillSolid;
287         val.font       = xfs->fid;
288
289         TSXChangeGC( display, physDev->gc,
290                    GCFunction | GCForeground | GCBackground | GCFillStyle |
291                    GCFont, &val );
292         return TRUE;
293     } 
294     WARN(dc, "Physical font failure\n" );
295     return FALSE;
296 }
297
298
299 /**********************************************************************
300  *           X11DRV_MoveToEx
301  */
302 BOOL32
303 X11DRV_MoveToEx(DC *dc,INT32 x,INT32 y,LPPOINT32 pt) {
304     if (pt)
305     {
306         pt->x = dc->w.CursPosX;
307         pt->y = dc->w.CursPosY;
308     }
309     dc->w.CursPosX = x;
310     dc->w.CursPosY = y;
311     return TRUE;
312 }
313
314 /***********************************************************************
315  *           X11DRV_LineTo
316  */
317 BOOL32
318 X11DRV_LineTo( DC *dc, INT32 x, INT32 y )
319 {
320     X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
321
322     if (X11DRV_SetupGCForPen( dc ))
323         TSXDrawLine(display, physDev->drawable, physDev->gc, 
324                   dc->w.DCOrgX + XLPTODP( dc, dc->w.CursPosX ),
325                   dc->w.DCOrgY + YLPTODP( dc, dc->w.CursPosY ),
326                   dc->w.DCOrgX + XLPTODP( dc, x ),
327                   dc->w.DCOrgY + YLPTODP( dc, y ) );
328     dc->w.CursPosX = x;
329     dc->w.CursPosY = y;
330     return TRUE;
331 }
332
333
334
335 /***********************************************************************
336  *           X11DRV_DrawArc
337  *
338  * Helper functions for Arc(), Chord() and Pie().
339  * 'lines' is the number of lines to draw: 0 for Arc, 1 for Chord, 2 for Pie.
340  *
341  */
342 static BOOL32
343 X11DRV_DrawArc( DC *dc, INT32 left, INT32 top, INT32 right,
344                 INT32 bottom, INT32 xstart, INT32 ystart,
345                 INT32 xend, INT32 yend, INT32 lines )
346 {
347     INT32 xcenter, ycenter, istart_angle, idiff_angle;
348     INT32 width, oldwidth, oldendcap;
349     double start_angle, end_angle;
350     XPoint points[4];
351     X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
352
353     left   = XLPTODP( dc, left );
354     top    = YLPTODP( dc, top );
355     right  = XLPTODP( dc, right );
356     bottom = YLPTODP( dc, bottom );
357     xstart = XLPTODP( dc, xstart );
358     ystart = YLPTODP( dc, ystart );
359     xend   = XLPTODP( dc, xend );
360     yend   = YLPTODP( dc, yend );
361
362     if (right < left) { INT32 tmp = right; right = left; left = tmp; }
363     if (bottom < top) { INT32 tmp = bottom; bottom = top; top = tmp; }
364     if ((left == right) || (top == bottom)
365             ||(lines && ((right-left==1)||(bottom-top==1)))) return TRUE;
366
367     oldwidth = width = physDev->pen.width;
368     oldendcap = physDev->pen.endcap;
369     if (!width) width = 1;
370     if(physDev->pen.style == PS_NULL) width = 0;
371
372     if ((physDev->pen.style == PS_INSIDEFRAME))
373     {
374         if (2*width > (right-left)) width=(right-left + 1)/2;
375         if (2*width > (bottom-top)) width=(bottom-top + 1)/2;
376         left   += width / 2;
377         right  -= (width - 1) / 2;
378         top    += width / 2;
379         bottom -= (width - 1) / 2;
380     }
381     if(width == 0) width = 1; /* more accurate */
382     physDev->pen.width = width;
383     physDev->pen.endcap = PS_ENDCAP_SQUARE;
384
385     xcenter = (right + left) / 2;
386     ycenter = (bottom + top) / 2;
387     start_angle = atan2( (double)(ycenter-ystart)*(right-left),
388                          (double)(xstart-xcenter)*(bottom-top) );
389     end_angle   = atan2( (double)(ycenter-yend)*(right-left),
390                          (double)(xend-xcenter)*(bottom-top) );
391     if ((xstart==xend)&&(ystart==yend))
392       { /* A lazy program delivers xstart=xend=ystart=yend=0) */
393         start_angle = 0;
394         end_angle = 2* PI;
395       }
396     else /* notorious cases */
397       if ((start_angle == PI)&&( end_angle <0))
398         start_angle = - PI;
399     else
400       if ((end_angle == PI)&&( start_angle <0))
401         end_angle = - PI;
402     istart_angle = (INT32)(start_angle * 180 * 64 / PI + 0.5);
403     idiff_angle  = (INT32)((end_angle - start_angle) * 180 * 64 / PI + 0.5);
404     if (idiff_angle <= 0) idiff_angle += 360 * 64;
405
406       /* Fill arc with brush if Chord() or Pie() */
407
408     if ((lines > 0) && X11DRV_SetupGCForBrush( dc )) {
409         TSXSetArcMode( display, physDev->gc,
410                        (lines==1) ? ArcChord : ArcPieSlice);
411         TSXFillArc( display, physDev->drawable, physDev->gc,
412                  dc->w.DCOrgX + left, dc->w.DCOrgY + top,
413                  right-left-1, bottom-top-1, istart_angle, idiff_angle );
414     }
415
416       /* Draw arc and lines */
417
418     if (X11DRV_SetupGCForPen( dc )){
419     TSXDrawArc( display, physDev->drawable, physDev->gc,
420               dc->w.DCOrgX + left, dc->w.DCOrgY + top,
421               right-left-1, bottom-top-1, istart_angle, idiff_angle );
422         if (lines) {
423             /* use the truncated values */
424             start_angle=(double)istart_angle*PI/64./180.;
425             end_angle=(double)(istart_angle+idiff_angle)*PI/64./180.;
426             /* calculate the endpoints and round correctly */
427             points[0].x = (int) floor(dc->w.DCOrgX + (right+left)/2.0 +
428                     cos(start_angle) * (right-left-width*2+2) / 2. + 0.5);
429             points[0].y = (int) floor(dc->w.DCOrgY + (top+bottom)/2.0 -
430                     sin(start_angle) * (bottom-top-width*2+2) / 2. + 0.5);
431             points[1].x = (int) floor(dc->w.DCOrgX + (right+left)/2.0 +
432                     cos(end_angle) * (right-left-width*2+2) / 2. + 0.5);
433             points[1].y = (int) floor(dc->w.DCOrgY + (top+bottom)/2.0 -
434                     sin(end_angle) * (bottom-top-width*2+2) / 2. + 0.5);
435                     
436             /* OK this stuff is optimized for Xfree86 
437              * which is probably the most used server by
438              * wine users. Other X servers will not 
439              * display correctly. (eXceed for instance)
440              * so if you feel you must change make sure that
441              * you either use Xfree86 or seperate your changes 
442              * from these (compile switch or whatever)
443              */
444             if (lines == 2) {
445                 INT32 dx1,dy1;
446                 points[3] = points[1];
447         points[1].x = dc->w.DCOrgX + xcenter;
448         points[1].y = dc->w.DCOrgY + ycenter;
449                 points[2] = points[1];
450                 dx1=points[1].x-points[0].x;
451                 dy1=points[1].y-points[0].y;
452                 if(((top-bottom) | -2) == -2)
453                     if(dy1>0) points[1].y--;
454                 if(dx1<0) {
455                     if (((-dx1)*64)<=ABS(dy1)*37) points[0].x--;
456                     if(((-dx1*9))<(dy1*16)) points[0].y--;
457                     if( dy1<0 && ((dx1*9)) < (dy1*16)) points[0].y--;
458                 } else {
459                     if(dy1 < 0)  points[0].y--;
460                     if(((right-left) | -2) == -2) points[1].x--;
461                 }
462                 dx1=points[3].x-points[2].x;
463                 dy1=points[3].y-points[2].y;
464                 if(((top-bottom) | -2 ) == -2)
465                     if(dy1 < 0) points[2].y--;
466                 if( dx1<0){ 
467                     if( dy1>0) points[3].y--;
468                     if(((right-left) | -2) == -2 ) points[2].x--;
469                 }else {
470                     points[3].y--;
471                     if( dx1 * 64 < dy1 * -37 ) points[3].x--;
472                 }
473                 lines++;
474     }
475     TSXDrawLines( display, physDev->drawable, physDev->gc,
476                 points, lines+1, CoordModeOrigin );
477         }
478     }
479     physDev->pen.width = oldwidth;
480     physDev->pen.endcap = oldendcap;
481     return TRUE;
482 }
483
484
485 /***********************************************************************
486  *           X11DRV_Arc
487  */
488 BOOL32
489 X11DRV_Arc( DC *dc, INT32 left, INT32 top, INT32 right, INT32 bottom,
490             INT32 xstart, INT32 ystart, INT32 xend, INT32 yend )
491 {
492     return X11DRV_DrawArc( dc, left, top, right, bottom,
493                            xstart, ystart, xend, yend, 0 );
494 }
495
496
497 /***********************************************************************
498  *           X11DRV_Pie
499  */
500 BOOL32
501 X11DRV_Pie( DC *dc, INT32 left, INT32 top, INT32 right, INT32 bottom,
502             INT32 xstart, INT32 ystart, INT32 xend, INT32 yend )
503 {
504     return X11DRV_DrawArc( dc, left, top, right, bottom,
505                            xstart, ystart, xend, yend, 2 );
506 }
507
508 /***********************************************************************
509  *           X11DRV_Chord
510  */
511 BOOL32
512 X11DRV_Chord( DC *dc, INT32 left, INT32 top, INT32 right, INT32 bottom,
513               INT32 xstart, INT32 ystart, INT32 xend, INT32 yend )
514 {
515     return X11DRV_DrawArc( dc, left, top, right, bottom,
516                            xstart, ystart, xend, yend, 1 );
517 }
518
519
520 /***********************************************************************
521  *           X11DRV_Ellipse
522  */
523 BOOL32
524 X11DRV_Ellipse( DC *dc, INT32 left, INT32 top, INT32 right, INT32 bottom )
525 {
526     INT32 width, oldwidth;
527     X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
528
529     left   = XLPTODP( dc, left );
530     top    = YLPTODP( dc, top );
531     right  = XLPTODP( dc, right );
532     bottom = YLPTODP( dc, bottom );
533     if ((left == right) || (top == bottom)) return TRUE;
534
535     if (right < left) { INT32 tmp = right; right = left; left = tmp; }
536     if (bottom < top) { INT32 tmp = bottom; bottom = top; top = tmp; }
537     
538     oldwidth = width = physDev->pen.width;
539     if (!width) width = 1;
540     if(physDev->pen.style == PS_NULL) width = 0;
541
542     if ((physDev->pen.style == PS_INSIDEFRAME))
543     {
544         if (2*width > (right-left)) width=(right-left + 1)/2;
545         if (2*width > (bottom-top)) width=(bottom-top + 1)/2;
546         left   += width / 2;
547         right  -= (width - 1) / 2;
548         top    += width / 2;
549         bottom -= (width - 1) / 2;
550     }
551     if(width == 0) width = 1; /* more accurate */
552     physDev->pen.width = width;
553
554     if (X11DRV_SetupGCForBrush( dc ))
555         TSXFillArc( display, physDev->drawable, physDev->gc,
556                   dc->w.DCOrgX + left, dc->w.DCOrgY + top,
557                   right-left-1, bottom-top-1, 0, 360*64 );
558     if (X11DRV_SetupGCForPen( dc ))
559         TSXDrawArc( display, physDev->drawable, physDev->gc,
560                   dc->w.DCOrgX + left, dc->w.DCOrgY + top,
561                   right-left-1, bottom-top-1, 0, 360*64 );
562     physDev->pen.width = oldwidth;
563     return TRUE;
564 }
565
566
567 /***********************************************************************
568  *           X11DRV_Rectangle
569  */
570 BOOL32
571 X11DRV_Rectangle(DC *dc, INT32 left, INT32 top, INT32 right, INT32 bottom)
572 {
573     INT32 width, oldwidth, oldjoinstyle;
574     X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
575
576     TRACE(graphics, "(%d %d %d %d)\n", 
577         left, top, right, bottom);
578
579     left   = XLPTODP( dc, left );
580     top    = YLPTODP( dc, top );
581     right  = XLPTODP( dc, right );
582     bottom = YLPTODP( dc, bottom );
583
584     if ((left == right) || (top == bottom)) return TRUE;
585
586     if (right < left) { INT32 tmp = right; right = left; left = tmp; }
587     if (bottom < top) { INT32 tmp = bottom; bottom = top; top = tmp; }
588
589     oldwidth = width = physDev->pen.width;
590     if (!width) width = 1;
591     if(physDev->pen.style == PS_NULL) width = 0;
592
593     if ((physDev->pen.style == PS_INSIDEFRAME))
594     {
595         if (2*width > (right-left)) width=(right-left + 1)/2;
596         if (2*width > (bottom-top)) width=(bottom-top + 1)/2;
597         left   += width / 2;
598         right  -= (width - 1) / 2;
599         top    += width / 2;
600         bottom -= (width - 1) / 2;
601     }
602     if(width == 1) width = 0;
603     physDev->pen.width = width;
604     oldjoinstyle = physDev->pen.linejoin;
605     if(physDev->pen.type != PS_GEOMETRIC)
606         physDev->pen.linejoin = PS_JOIN_MITER;
607
608     if ((right > left + width) && (bottom > top + width))
609     {
610         if (X11DRV_SetupGCForBrush( dc ))
611             TSXFillRectangle( display, physDev->drawable, physDev->gc,
612                             dc->w.DCOrgX + left + (width + 1) / 2,
613                             dc->w.DCOrgY + top + (width + 1) / 2,
614                             right-left-width-1, bottom-top-width-1);
615     }
616     if (X11DRV_SetupGCForPen( dc ))
617         TSXDrawRectangle( display, physDev->drawable, physDev->gc,
618                         dc->w.DCOrgX + left, dc->w.DCOrgY + top,
619                         right-left-1, bottom-top-1 );
620
621     physDev->pen.width = oldwidth;
622     physDev->pen.linejoin = oldjoinstyle;
623     return TRUE;
624 }
625
626 /***********************************************************************
627  *           X11DRV_RoundRect
628  */
629 BOOL32
630 X11DRV_RoundRect( DC *dc, INT32 left, INT32 top, INT32 right,
631                   INT32 bottom, INT32 ell_width, INT32 ell_height )
632 {
633     INT32 width, oldwidth, oldendcap;
634     X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
635
636     TRACE(graphics, "(%d %d %d %d  %d %d\n", 
637         left, top, right, bottom, ell_width, ell_height);
638
639     left   = XLPTODP( dc, left );
640     top    = YLPTODP( dc, top );
641     right  = XLPTODP( dc, right );
642     bottom = YLPTODP( dc, bottom );
643
644     if ((left == right) || (top == bottom))
645         return TRUE;
646
647     /* Make sure ell_width and ell_height are >= 1 otherwise XDrawArc gets
648        called with width/height < 0 */
649     ell_width  = MAX(abs( ell_width * dc->vportExtX / dc->wndExtX ), 1);
650     ell_height = MAX(abs( ell_height * dc->vportExtY / dc->wndExtY ), 1);
651
652     /* Fix the coordinates */
653
654     if (right < left) { INT32 tmp = right; right = left; left = tmp; }
655     if (bottom < top) { INT32 tmp = bottom; bottom = top; top = tmp; }
656
657     oldwidth = width = physDev->pen.width;
658     oldendcap = physDev->pen.endcap;
659     if (!width) width = 1;
660     if(physDev->pen.style == PS_NULL) width = 0;
661
662     if ((physDev->pen.style == PS_INSIDEFRAME))
663     {
664         if (2*width > (right-left)) width=(right-left + 1)/2;
665         if (2*width > (bottom-top)) width=(bottom-top + 1)/2;
666         left   += width / 2;
667         right  -= (width - 1) / 2;
668         top    += width / 2;
669         bottom -= (width - 1) / 2;
670     }
671     if(width == 0) width = 1;
672     physDev->pen.width = width;
673     physDev->pen.endcap = PS_ENDCAP_SQUARE;
674
675     if (X11DRV_SetupGCForBrush( dc ))
676     {
677         if (ell_width > (right-left) )
678             if (ell_height > (bottom-top) )
679                     TSXFillArc( display, physDev->drawable, physDev->gc,
680                                 dc->w.DCOrgX + left, dc->w.DCOrgY + top,
681                                 right - left - 1, bottom - top - 1,
682                                 0, 360 * 64 );
683             else{
684                     TSXFillArc( display, physDev->drawable, physDev->gc,
685                                 dc->w.DCOrgX + left, dc->w.DCOrgY + top,
686                                 right - left - 1, ell_height, 0, 180 * 64 );
687                     TSXFillArc( display, physDev->drawable, physDev->gc,
688                                 dc->w.DCOrgX + left,
689                                 dc->w.DCOrgY + bottom - ell_height - 1,
690                                 right - left - 1, ell_height, 180 * 64,
691                                 180 * 64 );
692            }
693         else if (ell_height > (bottom-top) ){
694                 TSXFillArc( display, physDev->drawable, physDev->gc,
695                       dc->w.DCOrgX + left, dc->w.DCOrgY + top,
696                       ell_width, bottom - top - 1, 90 * 64, 180 * 64 );
697                 TSXFillArc( display, physDev->drawable, physDev->gc,
698                       dc->w.DCOrgX + right - ell_width -1, dc->w.DCOrgY + top,
699                       ell_width, bottom - top - 1, 270 * 64, 180 * 64 );
700         }else{
701                 TSXFillArc( display, physDev->drawable, physDev->gc,
702                       dc->w.DCOrgX + left, dc->w.DCOrgY + top,
703                       ell_width, ell_height, 90 * 64, 90 * 64 );
704                 TSXFillArc( display, physDev->drawable, physDev->gc,
705                       dc->w.DCOrgX + left,
706                       dc->w.DCOrgY + bottom - ell_height - 1,
707                       ell_width, ell_height, 180 * 64, 90 * 64 );
708                 TSXFillArc( display, physDev->drawable, physDev->gc,
709                       dc->w.DCOrgX + right - ell_width - 1,
710                       dc->w.DCOrgY + bottom - ell_height - 1,
711                       ell_width, ell_height, 270 * 64, 90 * 64 );
712                 TSXFillArc( display, physDev->drawable, physDev->gc,
713                       dc->w.DCOrgX + right - ell_width - 1,
714                       dc->w.DCOrgY + top,
715                       ell_width, ell_height, 0, 90 * 64 );
716         }
717         if (ell_width < right - left)
718         {
719             TSXFillRectangle( display, physDev->drawable, physDev->gc,
720                             dc->w.DCOrgX + left + (ell_width + 1) / 2,
721                             dc->w.DCOrgY + top + 1,
722                             right - left - ell_width - 1,
723                             (ell_height + 1) / 2 - 1);
724             TSXFillRectangle( display, physDev->drawable, physDev->gc,
725                             dc->w.DCOrgX + left + (ell_width + 1) / 2,
726                             dc->w.DCOrgY + bottom - (ell_height) / 2 - 1,
727                             right - left - ell_width - 1,
728                             (ell_height) / 2 );
729         }
730         if  (ell_height < bottom - top)
731         {
732             TSXFillRectangle( display, physDev->drawable, physDev->gc,
733                             dc->w.DCOrgX + left + 1,
734                             dc->w.DCOrgY + top + (ell_height + 1) / 2,
735                             right - left - 2,
736                             bottom - top - ell_height - 1);
737         }
738     }
739     /* FIXME: this could be done with on X call
740      * more efficient and probably more correct
741      * on any X server: XDrawArcs will draw
742      * straight horizontal and vertical lines
743      * if width or height are zero.
744      *
745      * BTW this stuff is optimized for an Xfree86 server
746      * read the comments inside the X11DRV_DrawArc function
747      */
748     if (X11DRV_SetupGCForPen(dc)) {
749         if (ell_width > (right-left) )
750             if (ell_height > (bottom-top) )
751                 TSXDrawArc( display, physDev->drawable, physDev->gc,
752                       dc->w.DCOrgX + left, dc->w.DCOrgY + top,
753                       right - left - 1, bottom -top - 1, 0 , 360 * 64 );
754             else{
755                 TSXDrawArc( display, physDev->drawable, physDev->gc,
756                       dc->w.DCOrgX + left, dc->w.DCOrgY + top,
757                       right - left - 1, ell_height - 1, 0 , 180 * 64 );
758                 TSXDrawArc( display, physDev->drawable, physDev->gc,
759                       dc->w.DCOrgX + left, 
760                       dc->w.DCOrgY + bottom - ell_height,
761                       right - left - 1, ell_height - 1, 180 * 64 , 180 * 64 );
762             }
763         else if (ell_height > (bottom-top) ){
764                 TSXDrawArc( display, physDev->drawable, physDev->gc,
765                       dc->w.DCOrgX + left, dc->w.DCOrgY + top,
766                       ell_width - 1 , bottom - top - 1, 90 * 64 , 180 * 64 );
767                 TSXDrawArc( display, physDev->drawable, physDev->gc,
768                       dc->w.DCOrgX + right - ell_width, 
769                       dc->w.DCOrgY + top,
770                       ell_width - 1 , bottom - top - 1, 270 * 64 , 180 * 64 );
771         }else{
772             TSXDrawArc( display, physDev->drawable, physDev->gc,
773                       dc->w.DCOrgX + left, dc->w.DCOrgY + top,
774                       ell_width - 1, ell_height - 1, 90 * 64, 90 * 64 );
775             TSXDrawArc( display, physDev->drawable, physDev->gc,
776                       dc->w.DCOrgX + left, dc->w.DCOrgY + bottom - ell_height,
777                       ell_width - 1, ell_height - 1, 180 * 64, 90 * 64 );
778             TSXDrawArc( display, physDev->drawable, physDev->gc,
779                       dc->w.DCOrgX + right - ell_width,
780                       dc->w.DCOrgY + bottom - ell_height,
781                       ell_width - 1, ell_height - 1, 270 * 64, 90 * 64 );
782             TSXDrawArc( display, physDev->drawable, physDev->gc,
783                       dc->w.DCOrgX + right - ell_width, dc->w.DCOrgY + top,
784                       ell_width - 1, ell_height - 1, 0, 90 * 64 );
785         }
786         if (ell_width < right - left)
787         {
788             TSXDrawLine( display, physDev->drawable, physDev->gc, 
789                dc->w.DCOrgX + left + ell_width / 2,
790                        dc->w.DCOrgY + top,
791                dc->w.DCOrgX + right - (ell_width+1) / 2,
792                        dc->w.DCOrgY + top);
793             TSXDrawLine( display, physDev->drawable, physDev->gc, 
794                dc->w.DCOrgX + left + ell_width / 2 ,
795                        dc->w.DCOrgY + bottom - 1,
796                dc->w.DCOrgX + right - (ell_width+1)/ 2,
797                        dc->w.DCOrgY + bottom - 1);
798         }
799         if (ell_height < bottom - top)
800         {
801             TSXDrawLine( display, physDev->drawable, physDev->gc, 
802                        dc->w.DCOrgX + right - 1,
803                dc->w.DCOrgY + top + ell_height / 2,
804                        dc->w.DCOrgX + right - 1,
805                dc->w.DCOrgY + bottom - (ell_height+1) / 2);
806             TSXDrawLine( display, physDev->drawable, physDev->gc, 
807                        dc->w.DCOrgX + left,
808                dc->w.DCOrgY + top + ell_height / 2,
809                        dc->w.DCOrgX + left,
810                dc->w.DCOrgY + bottom - (ell_height+1) / 2);
811         }
812     }
813     physDev->pen.width = oldwidth;
814     physDev->pen.endcap = oldendcap;
815     return TRUE;
816 }
817
818
819 /***********************************************************************
820  *           X11DRV_SetPixel
821  */
822 COLORREF
823 X11DRV_SetPixel( DC *dc, INT32 x, INT32 y, COLORREF color )
824 {
825     Pixel pixel;
826     X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
827     
828     x = dc->w.DCOrgX + XLPTODP( dc, x );
829     y = dc->w.DCOrgY + YLPTODP( dc, y );
830     pixel = COLOR_ToPhysical( dc, color );
831     
832     TSXSetForeground( display, physDev->gc, pixel );
833     TSXSetFunction( display, physDev->gc, GXcopy );
834     TSXDrawPoint( display, physDev->drawable, physDev->gc, x, y );
835
836     /* inefficient but simple... */
837
838     return COLOR_ToLogical(pixel);
839 }
840
841
842 /***********************************************************************
843  *           X11DRV_GetPixel
844  */
845 COLORREF
846 X11DRV_GetPixel( DC *dc, INT32 x, INT32 y )
847 {
848     static Pixmap pixmap = 0;
849     XImage * image;
850     int pixel;
851     X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
852
853     x = dc->w.DCOrgX + XLPTODP( dc, x );
854     y = dc->w.DCOrgY + YLPTODP( dc, y );
855     EnterCriticalSection( &X11DRV_CritSection );
856     if (dc->w.flags & DC_MEMORY)
857     {
858         image = XGetImage( display, physDev->drawable, x, y, 1, 1,
859                            AllPlanes, ZPixmap );
860     }
861     else
862     {
863         /* If we are reading from the screen, use a temporary copy */
864         /* to avoid a BadMatch error */
865         if (!pixmap) pixmap = XCreatePixmap( display, X11DRV_GetXRootWindow(),
866                                              1, 1, dc->w.bitsPerPixel );
867         XCopyArea( display, physDev->drawable, pixmap, BITMAP_colorGC,
868                    x, y, 1, 1, 0, 0 );
869         image = XGetImage( display, pixmap, 0, 0, 1, 1, AllPlanes, ZPixmap );
870     }
871     pixel = XGetPixel( image, 0, 0 );
872     XDestroyImage( image );
873     LeaveCriticalSection( &X11DRV_CritSection );
874     
875     return COLOR_ToLogical(pixel);
876 }
877
878
879 /***********************************************************************
880  *           X11DRV_PaintRgn
881  */
882 BOOL32
883 X11DRV_PaintRgn( DC *dc, HRGN32 hrgn )
884 {
885     RECT32 box;
886     HRGN32 tmpVisRgn, prevVisRgn;
887     HDC32  hdc = dc->hSelf; /* FIXME: should not mix dc/hdc this way */
888     X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
889
890     if (!(tmpVisRgn = CreateRectRgn32( 0, 0, 0, 0 ))) return FALSE;
891
892       /* Transform region into device co-ords */
893     if (  !REGION_LPTODP( hdc, tmpVisRgn, hrgn )
894         || OffsetRgn32( tmpVisRgn, dc->w.DCOrgX, dc->w.DCOrgY ) == ERROR) {
895         DeleteObject32( tmpVisRgn );
896         return FALSE;
897     }
898
899       /* Modify visible region */
900     if (!(prevVisRgn = SaveVisRgn( hdc ))) {
901         DeleteObject32( tmpVisRgn );
902         return FALSE;
903     }
904     CombineRgn32( tmpVisRgn, prevVisRgn, tmpVisRgn, RGN_AND );
905     SelectVisRgn( hdc, tmpVisRgn );
906     DeleteObject32( tmpVisRgn );
907
908       /* Fill the region */
909
910     GetRgnBox32( dc->w.hGCClipRgn, &box );
911     if (X11DRV_SetupGCForBrush( dc ))
912         TSXFillRectangle( display, physDev->drawable, physDev->gc,
913                           box.left, box.top,
914                           box.right-box.left, box.bottom-box.top );
915
916       /* Restore the visible region */
917
918     RestoreVisRgn( hdc );
919     return TRUE;
920 }
921
922 /**********************************************************************
923  *          X11DRV_Polyline
924  */
925 BOOL32
926 X11DRV_Polyline( DC *dc, const POINT32* pt, INT32 count )
927 {
928     INT32 oldwidth;
929     register int i;
930     XPoint *points;
931     X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
932
933     if((oldwidth = physDev->pen.width) == 0) physDev->pen.width = 1;
934
935     points = (XPoint *) xmalloc (sizeof (XPoint) * (count));
936     for (i = 0; i < count; i++)
937     {
938     points[i].x = dc->w.DCOrgX + XLPTODP( dc, pt[i].x );
939     points[i].y = dc->w.DCOrgY + YLPTODP( dc, pt[i].y );
940     }
941
942     if (X11DRV_SetupGCForPen ( dc ))
943     TSXDrawLines( display, physDev->drawable, physDev->gc,
944            points, count, CoordModeOrigin );
945
946     free( points );
947     physDev->pen.width = oldwidth;
948     return TRUE;
949 }
950
951
952 /**********************************************************************
953  *          X11DRV_Polygon
954  */
955 BOOL32
956 X11DRV_Polygon( DC *dc, const POINT32* pt, INT32 count )
957 {
958     register int i;
959     XPoint *points;
960     X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
961
962     points = (XPoint *) xmalloc (sizeof (XPoint) * (count+1));
963     for (i = 0; i < count; i++)
964     {
965         points[i].x = dc->w.DCOrgX + XLPTODP( dc, pt[i].x );
966         points[i].y = dc->w.DCOrgY + YLPTODP( dc, pt[i].y );
967     }
968     points[count] = points[0];
969
970     if (X11DRV_SetupGCForBrush( dc ))
971         TSXFillPolygon( display, physDev->drawable, physDev->gc,
972                      points, count+1, Complex, CoordModeOrigin);
973
974     if (X11DRV_SetupGCForPen ( dc ))
975         TSXDrawLines( display, physDev->drawable, physDev->gc,
976                    points, count+1, CoordModeOrigin );
977
978     free( points );
979     return TRUE;
980 }
981
982
983 /**********************************************************************
984  *          X11DRV_PolyPolygon
985  */
986 BOOL32 
987 X11DRV_PolyPolygon( DC *dc, const POINT32* pt, const INT32* counts, UINT32 polygons)
988 {
989     HRGN32 hrgn;
990     X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
991
992     /* FIXME: The points should be converted to device coords before */
993     /* creating the region. */
994
995     hrgn = CreatePolyPolygonRgn32( pt, counts, polygons, dc->w.polyFillMode );
996     X11DRV_PaintRgn( dc, hrgn );
997     DeleteObject32( hrgn );
998
999       /* Draw the outline of the polygons */
1000
1001     if (X11DRV_SetupGCForPen ( dc ))
1002     {
1003         int i, j, max = 0;
1004         XPoint *points;
1005
1006         for (i = 0; i < polygons; i++) if (counts[i] > max) max = counts[i];
1007         points = (XPoint *) xmalloc( sizeof(XPoint) * (max+1) );
1008
1009         for (i = 0; i < polygons; i++)
1010         {
1011             for (j = 0; j < counts[i]; j++)
1012             {
1013                 points[j].x = dc->w.DCOrgX + XLPTODP( dc, pt->x );
1014                 points[j].y = dc->w.DCOrgY + YLPTODP( dc, pt->y );
1015                 pt++;
1016             }
1017             points[j] = points[0];
1018             TSXDrawLines( display, physDev->drawable, physDev->gc,
1019                         points, j + 1, CoordModeOrigin );
1020         }
1021         free( points );
1022     }
1023     return TRUE;
1024 }
1025
1026
1027 /**********************************************************************
1028  *          X11DRV_PolyPolyline
1029  */
1030 BOOL32 
1031 X11DRV_PolyPolyline( DC *dc, const POINT32* pt, const DWORD* counts, DWORD polylines )
1032 {
1033     X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
1034     if (X11DRV_SetupGCForPen ( dc ))
1035     {
1036         int i, j, max = 0;
1037         XPoint *points;
1038
1039         for (i = 0; i < polylines; i++) if (counts[i] > max) max = counts[i];
1040         points = (XPoint *) xmalloc( sizeof(XPoint) * (max+1) );
1041
1042         for (i = 0; i < polylines; i++)
1043         {
1044             for (j = 0; j < counts[i]; j++)
1045             {
1046                 points[j].x = dc->w.DCOrgX + XLPTODP( dc, pt->x );
1047                 points[j].y = dc->w.DCOrgY + YLPTODP( dc, pt->y );
1048                 pt++;
1049             }
1050             points[j] = points[0];
1051             TSXDrawLines( display, physDev->drawable, physDev->gc,
1052                         points, j + 1, CoordModeOrigin );
1053         }
1054         free( points );
1055     }
1056     return TRUE;
1057 }
1058
1059
1060 /**********************************************************************
1061  *          X11DRV_InternalFloodFill
1062  *
1063  * Internal helper function for flood fill.
1064  * (xorg,yorg) is the origin of the X image relative to the drawable.
1065  * (x,y) is relative to the origin of the X image.
1066  */
1067 static void X11DRV_InternalFloodFill(XImage *image, DC *dc,
1068                                      int x, int y,
1069                                      int xOrg, int yOrg,
1070                                      Pixel pixel, WORD fillType )
1071 {
1072     X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
1073     int left, right;
1074
1075 #define TO_FLOOD(x,y)  ((fillType == FLOODFILLBORDER) ? \
1076                         (XGetPixel(image,x,y) != pixel) : \
1077                         (XGetPixel(image,x,y) == pixel))
1078
1079     if (!TO_FLOOD(x,y)) return;
1080
1081       /* Find left and right boundaries */
1082
1083     left = right = x;
1084     while ((left > 0) && TO_FLOOD( left-1, y )) left--;
1085     while ((right < image->width) && TO_FLOOD( right, y )) right++;
1086     XFillRectangle( display, physDev->drawable, physDev->gc,
1087                     xOrg + left, yOrg + y, right-left, 1 );
1088
1089       /* Set the pixels of this line so we don't fill it again */
1090
1091     for (x = left; x < right; x++)
1092     {
1093         if (fillType == FLOODFILLBORDER) XPutPixel( image, x, y, pixel );
1094         else XPutPixel( image, x, y, ~pixel );
1095     }
1096
1097       /* Fill the line above */
1098
1099     if (--y >= 0)
1100     {
1101         x = left;
1102         while (x < right)
1103         {
1104             while ((x < right) && !TO_FLOOD(x,y)) x++;
1105             if (x >= right) break;
1106             while ((x < right) && TO_FLOOD(x,y)) x++;
1107             X11DRV_InternalFloodFill(image, dc, x-1, y,
1108                                      xOrg, yOrg, pixel, fillType );
1109         }
1110     }
1111
1112       /* Fill the line below */
1113
1114     if ((y += 2) < image->height)
1115     {
1116         x = left;
1117         while (x < right)
1118         {
1119             while ((x < right) && !TO_FLOOD(x,y)) x++;
1120             if (x >= right) break;
1121             while ((x < right) && TO_FLOOD(x,y)) x++;
1122             X11DRV_InternalFloodFill(image, dc, x-1, y,
1123                                      xOrg, yOrg, pixel, fillType );
1124         }
1125     }
1126 #undef TO_FLOOD    
1127 }
1128
1129
1130 /**********************************************************************
1131  *          X11DRV_DoFloodFill
1132  *
1133  * Main flood-fill routine.
1134  *
1135  * The Xlib critical section must be entered before calling this function.
1136  */
1137
1138 struct FloodFill_params
1139 {
1140     DC      *dc;
1141     INT32    x;
1142     INT32    y;
1143     COLORREF color;
1144     UINT32   fillType;
1145 };
1146
1147 static BOOL32 X11DRV_DoFloodFill( const struct FloodFill_params *params )
1148 {
1149     XImage *image;
1150     RECT32 rect;
1151     DC *dc = params->dc;
1152     X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
1153
1154     if (GetRgnBox32( dc->w.hGCClipRgn, &rect ) == ERROR) return FALSE;
1155
1156     if (!(image = XGetImage( display, physDev->drawable,
1157                              rect.left,
1158                              rect.top,
1159                              rect.right - rect.left,
1160                              rect.bottom - rect.top,
1161                              AllPlanes, ZPixmap ))) return FALSE;
1162
1163     if (X11DRV_SetupGCForBrush( dc ))
1164     {
1165           /* ROP mode is always GXcopy for flood-fill */
1166         XSetFunction( display, physDev->gc, GXcopy );
1167         X11DRV_InternalFloodFill(image, dc,
1168                                  XLPTODP(dc,params->x) + dc->w.DCOrgX - rect.left,
1169                                  YLPTODP(dc,params->y) + dc->w.DCOrgY - rect.top,
1170                                  rect.left,
1171                                  rect.top,
1172                                  COLOR_ToPhysical( dc, params->color ),
1173                                  params->fillType );
1174     }
1175
1176     XDestroyImage( image );
1177     return TRUE;
1178 }
1179
1180
1181 /**********************************************************************
1182  *          X11DRV_ExtFloodFill
1183  */
1184 BOOL32
1185 X11DRV_ExtFloodFill( DC *dc, INT32 x, INT32 y, COLORREF color,
1186                      UINT32 fillType )
1187 {
1188     BOOL32 result;
1189     struct FloodFill_params params = { dc, x, y, color, fillType };
1190
1191     TRACE(graphics, "X11DRV_ExtFloodFill %d,%d %06lx %d\n",
1192                       x, y, color, fillType );
1193
1194     if (!PtVisible32( dc->hSelf, x, y )) return FALSE;
1195     EnterCriticalSection( &X11DRV_CritSection );
1196     result = CALL_LARGE_STACK( X11DRV_DoFloodFill, &params );
1197     LeaveCriticalSection( &X11DRV_CritSection );
1198     return result;
1199 }
1200
1201 /******************************************************************
1202  * 
1203  *   *Very* simple bezier drawing code, 
1204  *
1205  *   It uses a recursive algorithm to divide the curve in a series
1206  *   of straight line segements. Not ideal but for me sufficient.
1207  *   If you are in need for something better look for some incremental
1208  *   algorithm.
1209  *
1210  *   7 July 1998 Rein Klazes
1211  */
1212
1213  /* 
1214   * some macro definitions for bezier drawing
1215   *
1216   * to avoid trucation errors the coordinates are
1217   * shifted upwards. When used in drawing they are
1218   * shifted down again, including correct rounding
1219   * and avoiding floating point arithmatic
1220   * 4 bits should allow 27 bits coordinates which I saw
1221   * somewere in the win32 doc's
1222   * 
1223   */
1224
1225 #define BEZIERSHIFTBITS 4
1226 #define BEZIERSHIFTUP(x)    ((x)<<BEZIERSHIFTBITS)
1227 #define BEZIERPIXEL        BEZIERSHIFTUP(1)    
1228 #define BEZIERSHIFTDOWN(x)  (((x)+(1<<(BEZIERSHIFTBITS-1)))>>BEZIERSHIFTBITS)
1229 /* maximum depth of recursion */
1230 #define BEZIERMAXDEPTH  8
1231
1232 /* size of array to store points on */
1233 /* enough for one curve */
1234 #define BEZMAXPOINTS    (150)
1235
1236 /* calculate Bezier average, in this case the middle 
1237  * correctly rounded...
1238  * */
1239
1240 #define BEZIERMIDDLE(Mid, P1, P2) \
1241     (Mid).x=((P1).x+(P2).x + 1)/2;\
1242     (Mid).y=((P1).y+(P2).y + 1)/2;
1243     
1244 /**********************************************************
1245 * BezierCheck helper function to check
1246 * that recursion can be terminated
1247 *       Points[0] and Points[3] are begin and endpoint
1248 *       Points[1] and Points[2] are control points
1249 *       level is the recursion depth
1250 *       returns true if the recusion can be terminated
1251 */
1252 static BOOL32 BezierCheck( int level, POINT32 *Points)
1253
1254     INT32 dx, dy;
1255     dx=Points[3].x-Points[0].x;
1256     dy=Points[3].y-Points[0].y;
1257     if(ABS(dy)<=ABS(dx)){/* shallow line */
1258         /* check that control points are between begin and end */
1259         if(Points[1].x < Points[0].x){
1260             if(Points[1].x < Points[3].x)
1261                 return FALSE;
1262         }else
1263             if(Points[1].x > Points[3].x)
1264                 return FALSE;
1265         if(Points[2].x < Points[0].x){
1266             if(Points[2].x < Points[3].x)
1267                 return FALSE;
1268         }else
1269             if(Points[2].x > Points[3].x)
1270                 return FALSE;
1271         dx=BEZIERSHIFTDOWN(dx);
1272         if(!dx) return TRUE;
1273         if(abs(Points[1].y-Points[0].y-(dy/dx)*
1274                 BEZIERSHIFTDOWN(Points[1].x-Points[0].x)) > BEZIERPIXEL ||
1275            abs(Points[2].y-Points[0].y-(dy/dx)*
1276                    BEZIERSHIFTDOWN(Points[2].x-Points[0].x)) > BEZIERPIXEL )
1277             return FALSE;
1278         else
1279             return TRUE;
1280     }else{ /* steep line */
1281         /* check that control points are between begin and end */
1282         if(Points[1].y < Points[0].y){
1283             if(Points[1].y < Points[3].y)
1284                 return FALSE;
1285         }else
1286             if(Points[1].y > Points[3].y)
1287                 return FALSE;
1288         if(Points[2].y < Points[0].y){
1289             if(Points[2].y < Points[3].y)
1290                 return FALSE;
1291         }else
1292             if(Points[2].y > Points[3].y)
1293                 return FALSE;
1294         dy=BEZIERSHIFTDOWN(dy);
1295         if(!dy) return TRUE;
1296         if(abs(Points[1].x-Points[0].x-(dx/dy)*
1297                 BEZIERSHIFTDOWN(Points[1].y-Points[0].y)) > BEZIERPIXEL ||
1298            abs(Points[2].x-Points[0].x-(dx/dy)*
1299                    BEZIERSHIFTDOWN(Points[2].y-Points[0].y)) > BEZIERPIXEL )
1300             return FALSE;
1301         else
1302             return TRUE;
1303     }
1304 }
1305     
1306 /***********************************************************************
1307  *           X11DRV_Bezier
1308  *   Draw a -what microsoft calls- bezier curve
1309  *   The routine recursively devides the curve
1310  *   in two parts until a straight line can be drawn
1311  *
1312  *   level      recusion depth counted backwards
1313  *   dc         device context
1314  *   Points     array of begin(0), end(3) and control points(1 and 2)
1315  *   XPoints    array with points calculated sofar
1316  *   *pIx       nr points calculated sofar
1317  *   
1318  */
1319 static void X11DRV_Bezier(int level, DC * dc, POINT32 *Points, 
1320                           XPoint* xpoints, unsigned int* pIx)
1321 {
1322     X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
1323
1324     if(*pIx == BEZMAXPOINTS){
1325         TSXDrawLines( display, physDev->drawable, physDev->gc,
1326                     xpoints, *pIx, CoordModeOrigin );
1327         *pIx=0;
1328     }
1329     if(!level || BezierCheck(level, Points)) {
1330         if(*pIx == 0){
1331             xpoints[*pIx].x= dc->w.DCOrgX + BEZIERSHIFTDOWN(Points[0].x);
1332             xpoints[*pIx].y= dc->w.DCOrgY + BEZIERSHIFTDOWN(Points[0].y);
1333             *pIx=1;
1334         }
1335         xpoints[*pIx].x= dc->w.DCOrgX + BEZIERSHIFTDOWN(Points[3].x);
1336         xpoints[*pIx].y= dc->w.DCOrgY + BEZIERSHIFTDOWN(Points[3].y);
1337         (*pIx) ++;
1338     } else {
1339         POINT32 Points2[4]; /* for the second recursive call */
1340         Points2[3]=Points[3];
1341         BEZIERMIDDLE(Points2[2], Points[2], Points[3]);
1342         BEZIERMIDDLE(Points2[0], Points[1], Points[2]);
1343         BEZIERMIDDLE(Points2[1],Points2[0],Points2[2]);
1344
1345         BEZIERMIDDLE(Points[1], Points[0],  Points[1]);
1346         BEZIERMIDDLE(Points[2], Points[1], Points2[0]);
1347         BEZIERMIDDLE(Points[3], Points[2], Points2[1]);
1348
1349         Points2[0]=Points[3];
1350
1351         /* do the two halves */
1352         X11DRV_Bezier(level-1, dc, Points, xpoints, pIx);
1353         X11DRV_Bezier(level-1, dc, Points2, xpoints, pIx);
1354     }
1355 }
1356
1357 /***********************************************************************
1358  *           X11DRV_PolyBezier
1359  *      Implement functionality for PolyBezier and PolyBezierTo
1360  *      calls. 
1361  *      [i] dc pointer to device context
1362  *      [i] start, first point in curve
1363  *      [i] BezierPoints , array of point filled with rest of the points
1364  *      [i] count, number of points in BezierPoints, must be a 
1365  *          multiple of 3.
1366  */
1367 BOOL32
1368 X11DRV_PolyBezier(DC *dc, POINT32 start, const POINT32* BezierPoints, DWORD count)
1369 {
1370     POINT32 Points[4]; 
1371     int i;
1372     unsigned int ix=0;
1373     XPoint* xpoints;
1374     X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
1375
1376     TRACE(graphics, "dc=%p count=%ld %ld,%ld - %ld,%ld - %ld,%ld - %ld,%ld\n", 
1377             dc, count,
1378             start.x, start.y,
1379             (Points+0)->x, (Points+0)->y, 
1380             (Points+1)->x, (Points+1)->y, 
1381             (Points+2)->x, (Points+2)->y); 
1382     if(!count || count % 3){/* paranoid */
1383         WARN(graphics," bad value for count : %ld\n", count);
1384         return FALSE; 
1385     }
1386     xpoints=(XPoint*) xmalloc( sizeof(XPoint)*BEZMAXPOINTS);
1387     Points[3].x=BEZIERSHIFTUP(XLPTODP(dc,start.x));
1388     Points[3].y=BEZIERSHIFTUP(YLPTODP(dc,start.y));
1389     while(count){
1390         Points[0]=Points[3];
1391         for(i=1;i<4;i++) {
1392             Points[i].x= BEZIERSHIFTUP(XLPTODP(dc,BezierPoints->x));
1393             Points[i].y= BEZIERSHIFTUP(YLPTODP(dc,BezierPoints->y));
1394             BezierPoints++;
1395         }
1396         X11DRV_Bezier(BEZIERMAXDEPTH , dc, Points, xpoints, &ix );
1397         count -=3;
1398     }
1399     if( ix) TSXDrawLines( display, physDev->drawable, physDev->gc,
1400                           xpoints, ix, CoordModeOrigin );
1401     free(xpoints);
1402     return TRUE;
1403 }
1404
1405 /**********************************************************************
1406  *          X11DRV_SetBkColor
1407  */
1408 COLORREF
1409 X11DRV_SetBkColor( DC *dc, COLORREF color )
1410 {
1411     X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
1412     COLORREF oldColor;
1413
1414     oldColor = dc->w.backgroundColor;
1415     dc->w.backgroundColor = color;
1416
1417     physDev->backgroundPixel = COLOR_ToPhysical( dc, color );
1418
1419     return oldColor;
1420 }
1421
1422 /**********************************************************************
1423  *          X11DRV_SetTextColor
1424  */
1425 COLORREF
1426 X11DRV_SetTextColor( DC *dc, COLORREF color )
1427 {
1428     X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
1429     COLORREF oldColor;
1430
1431     oldColor = dc->w.textColor;
1432     dc->w.textColor = color;
1433
1434     physDev->textPixel = COLOR_ToPhysical( dc, color );
1435
1436     return oldColor;
1437 }
1438
1439 #endif /* !defined(X_DISPLAY_MISSING) */