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