2 * X11 graphics driver graphics functions
4 * Copyright 1993,1994 Alexandre Julliard
8 * FIXME: none of these functions obey the GM_ADVANCED
14 #ifndef X_DISPLAY_MISSING
16 #include <X11/Intrinsic.h>
42 #include "debugtools.h"
45 DEFAULT_DEBUG_CHANNEL(graphics)
47 #define ABS(x) ((x)<0?(-(x)):(x))
49 /* ROP code to GC function conversion */
50 const int X11DRV_XROPfunction[16] =
52 GXclear, /* R2_BLACK */
53 GXnor, /* R2_NOTMERGEPEN */
54 GXandInverted, /* R2_MASKNOTPEN */
55 GXcopyInverted, /* R2_NOTCOPYPEN */
56 GXandReverse, /* R2_MASKPENNOT */
57 GXinvert, /* R2_NOT */
58 GXxor, /* R2_XORPEN */
59 GXnand, /* R2_NOTMASKPEN */
60 GXand, /* R2_MASKPEN */
61 GXequiv, /* R2_NOTXORPEN */
63 GXorInverted, /* R2_MERGENOTPEN */
64 GXcopy, /* R2_COPYPEN */
65 GXorReverse, /* R2_MERGEPENNOT */
66 GXor, /* R2_MERGEPEN */
71 /***********************************************************************
72 * X11DRV_SetupGCForPatBlt
74 * Setup the GC for a PatBlt operation using current brush.
75 * If fMapColors is TRUE, X pixels are mapped to Windows colors.
76 * Return FALSE if brush is BS_NULL, TRUE otherwise.
78 BOOL X11DRV_SetupGCForPatBlt( DC * dc, GC gc, BOOL fMapColors )
83 X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
85 if (physDev->brush.style == BS_NULL) return FALSE;
86 if (physDev->brush.pixel == -1)
88 /* Special case used for monochrome pattern brushes.
89 * We need to swap foreground and background because
90 * Windows does it the wrong way...
92 val.foreground = physDev->backgroundPixel;
93 val.background = physDev->textPixel;
97 val.foreground = physDev->brush.pixel;
98 val.background = physDev->backgroundPixel;
100 if (fMapColors && X11DRV_PALETTE_XPixelToPalette)
102 val.foreground = X11DRV_PALETTE_XPixelToPalette[val.foreground];
103 val.background = X11DRV_PALETTE_XPixelToPalette[val.background];
106 if (dc->w.flags & DC_DIRTY) CLIPPING_UpdateGCRegion(dc);
108 val.function = X11DRV_XROPfunction[dc->w.ROPmode-1];
110 ** Let's replace GXinvert by GXxor with (black xor white)
111 ** This solves the selection color and leak problems in excel
112 ** FIXME : Let's do that only if we work with X-pixels, not with Win-pixels
114 if (val.function == GXinvert)
116 val.foreground = BlackPixelOfScreen(X11DRV_GetXScreen()) ^ WhitePixelOfScreen(X11DRV_GetXScreen());
117 val.function = GXxor;
119 val.fill_style = physDev->brush.fillStyle;
120 switch(val.fill_style)
123 case FillOpaqueStippled:
124 if (dc->w.backgroundMode==OPAQUE) val.fill_style = FillOpaqueStippled;
125 val.stipple = physDev->brush.pixmap;
130 if (fMapColors && X11DRV_PALETTE_XPixelToPalette)
134 EnterCriticalSection( &X11DRV_CritSection );
135 pixmap = XCreatePixmap( display,
136 X11DRV_GetXRootWindow(),
138 MONITOR_GetDepth(&MONITOR_PrimaryMonitor) );
139 image = XGetImage( display, physDev->brush.pixmap, 0, 0, 8, 8,
140 AllPlanes, ZPixmap );
141 for (y = 0; y < 8; y++)
142 for (x = 0; x < 8; x++)
143 XPutPixel( image, x, y,
144 X11DRV_PALETTE_XPixelToPalette[XGetPixel( image, x, y)] );
145 XPutImage( display, pixmap, gc, image, 0, 0, 0, 0, 8, 8 );
146 XDestroyImage( image );
147 LeaveCriticalSection( &X11DRV_CritSection );
150 else val.tile = physDev->brush.pixmap;
158 val.ts_x_origin = dc->w.DCOrgX + dc->w.brushOrgX;
159 val.ts_y_origin = dc->w.DCOrgY + dc->w.brushOrgY;
160 val.fill_rule = (dc->w.polyFillMode==WINDING) ? WindingRule : EvenOddRule;
161 TSXChangeGC( display, gc,
162 GCFunction | GCForeground | GCBackground | GCFillStyle |
163 GCFillRule | GCTileStipXOrigin | GCTileStipYOrigin | mask,
165 if (pixmap) TSXFreePixmap( display, pixmap );
170 /***********************************************************************
171 * X11DRV_SetupGCForBrush
173 * Setup physDev->gc for drawing operations using current brush.
174 * Return FALSE if brush is BS_NULL, TRUE otherwise.
176 BOOL X11DRV_SetupGCForBrush( DC * dc )
178 X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
179 return X11DRV_SetupGCForPatBlt( dc, physDev->gc, FALSE );
183 /***********************************************************************
184 * X11DRV_SetupGCForPen
186 * Setup physDev->gc for drawing operations using current pen.
187 * Return FALSE if pen is PS_NULL, TRUE otherwise.
189 BOOL X11DRV_SetupGCForPen( DC * dc )
192 X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
194 if (physDev->pen.style == PS_NULL) return FALSE;
196 if (dc->w.flags & DC_DIRTY) CLIPPING_UpdateGCRegion(dc);
198 switch (dc->w.ROPmode)
201 val.foreground = BlackPixelOfScreen( X11DRV_GetXScreen() );
202 val.function = GXcopy;
205 val.foreground = WhitePixelOfScreen( X11DRV_GetXScreen() );
206 val.function = GXcopy;
209 val.foreground = physDev->pen.pixel;
210 /* It is very unlikely someone wants to XOR with 0 */
211 /* This fixes the rubber-drawings in paintbrush */
212 if (val.foreground == 0)
213 val.foreground = BlackPixelOfScreen( X11DRV_GetXScreen() )
214 ^ WhitePixelOfScreen( X11DRV_GetXScreen() );
215 val.function = GXxor;
218 val.foreground = physDev->pen.pixel;
219 val.function = X11DRV_XROPfunction[dc->w.ROPmode-1];
221 val.background = physDev->backgroundPixel;
222 val.fill_style = FillSolid;
223 if ((physDev->pen.width <= 1) &&
224 (physDev->pen.style != PS_SOLID) &&
225 (physDev->pen.style != PS_INSIDEFRAME))
227 TSXSetDashes( display, physDev->gc, 0, physDev->pen.dashes,
228 physDev->pen.dash_len );
229 val.line_style = (dc->w.backgroundMode == OPAQUE) ?
230 LineDoubleDash : LineOnOffDash;
232 else val.line_style = LineSolid;
233 val.line_width = physDev->pen.width;
234 if (val.line_width <= 1) {
235 val.cap_style = CapNotLast;
237 switch (physDev->pen.endcap)
239 case PS_ENDCAP_SQUARE:
240 val.cap_style = CapProjecting;
243 val.cap_style = CapButt;
245 case PS_ENDCAP_ROUND:
247 val.cap_style = CapRound;
250 switch (physDev->pen.linejoin)
253 val.join_style = JoinBevel;
256 val.join_style = JoinMiter;
260 val.join_style = JoinRound;
262 TSXChangeGC( display, physDev->gc,
263 GCFunction | GCForeground | GCBackground | GCLineWidth |
264 GCLineStyle | GCCapStyle | GCJoinStyle | GCFillStyle, &val );
269 /***********************************************************************
270 * X11DRV_SetupGCForText
272 * Setup physDev->gc for text drawing operations.
273 * Return FALSE if the font is null, TRUE otherwise.
275 BOOL X11DRV_SetupGCForText( DC * dc )
277 X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
278 XFontStruct* xfs = XFONT_GetFontStruct( physDev->font );
284 if (dc->w.flags & DC_DIRTY) CLIPPING_UpdateGCRegion(dc);
286 val.function = GXcopy; /* Text is always GXcopy */
287 val.foreground = physDev->textPixel;
288 val.background = physDev->backgroundPixel;
289 val.fill_style = FillSolid;
292 TSXChangeGC( display, physDev->gc,
293 GCFunction | GCForeground | GCBackground | GCFillStyle |
297 WARN("Physical font failure\n" );
302 /**********************************************************************
306 X11DRV_MoveToEx(DC *dc,INT x,INT y,LPPOINT pt) {
309 pt->x = dc->w.CursPosX;
310 pt->y = dc->w.CursPosY;
317 /***********************************************************************
321 X11DRV_LineTo( DC *dc, INT x, INT y )
323 X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
325 if (X11DRV_SetupGCForPen( dc ))
326 TSXDrawLine(display, physDev->drawable, physDev->gc,
327 dc->w.DCOrgX + XLPTODP( dc, dc->w.CursPosX ),
328 dc->w.DCOrgY + YLPTODP( dc, dc->w.CursPosY ),
329 dc->w.DCOrgX + XLPTODP( dc, x ),
330 dc->w.DCOrgY + YLPTODP( dc, y ) );
338 /***********************************************************************
341 * Helper functions for Arc(), Chord() and Pie().
342 * 'lines' is the number of lines to draw: 0 for Arc, 1 for Chord, 2 for Pie.
346 X11DRV_DrawArc( DC *dc, INT left, INT top, INT right,
347 INT bottom, INT xstart, INT ystart,
348 INT xend, INT yend, INT lines )
350 INT xcenter, ycenter, istart_angle, idiff_angle;
351 INT width, oldwidth, oldendcap;
352 double start_angle, end_angle;
354 X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
356 left = XLPTODP( dc, left );
357 top = YLPTODP( dc, top );
358 right = XLPTODP( dc, right );
359 bottom = YLPTODP( dc, bottom );
360 xstart = XLPTODP( dc, xstart );
361 ystart = YLPTODP( dc, ystart );
362 xend = XLPTODP( dc, xend );
363 yend = YLPTODP( dc, yend );
365 if (right < left) { INT tmp = right; right = left; left = tmp; }
366 if (bottom < top) { INT tmp = bottom; bottom = top; top = tmp; }
367 if ((left == right) || (top == bottom)
368 ||(lines && ((right-left==1)||(bottom-top==1)))) return TRUE;
370 oldwidth = width = physDev->pen.width;
371 oldendcap = physDev->pen.endcap;
372 if (!width) width = 1;
373 if(physDev->pen.style == PS_NULL) width = 0;
375 if ((physDev->pen.style == PS_INSIDEFRAME))
377 if (2*width > (right-left)) width=(right-left + 1)/2;
378 if (2*width > (bottom-top)) width=(bottom-top + 1)/2;
380 right -= (width - 1) / 2;
382 bottom -= (width - 1) / 2;
384 if(width == 0) width = 1; /* more accurate */
385 physDev->pen.width = width;
386 physDev->pen.endcap = PS_ENDCAP_SQUARE;
388 xcenter = (right + left) / 2;
389 ycenter = (bottom + top) / 2;
390 start_angle = atan2( (double)(ycenter-ystart)*(right-left),
391 (double)(xstart-xcenter)*(bottom-top) );
392 end_angle = atan2( (double)(ycenter-yend)*(right-left),
393 (double)(xend-xcenter)*(bottom-top) );
394 if ((xstart==xend)&&(ystart==yend))
395 { /* A lazy program delivers xstart=xend=ystart=yend=0) */
399 else /* notorious cases */
400 if ((start_angle == PI)&&( end_angle <0))
403 if ((end_angle == PI)&&( start_angle <0))
405 istart_angle = (INT)(start_angle * 180 * 64 / PI + 0.5);
406 idiff_angle = (INT)((end_angle - start_angle) * 180 * 64 / PI + 0.5);
407 if (idiff_angle <= 0) idiff_angle += 360 * 64;
409 /* Fill arc with brush if Chord() or Pie() */
411 if ((lines > 0) && X11DRV_SetupGCForBrush( dc )) {
412 TSXSetArcMode( display, physDev->gc,
413 (lines==1) ? ArcChord : ArcPieSlice);
414 TSXFillArc( display, physDev->drawable, physDev->gc,
415 dc->w.DCOrgX + left, dc->w.DCOrgY + top,
416 right-left-1, bottom-top-1, istart_angle, idiff_angle );
419 /* Draw arc and lines */
421 if (X11DRV_SetupGCForPen( dc )){
422 TSXDrawArc( display, physDev->drawable, physDev->gc,
423 dc->w.DCOrgX + left, dc->w.DCOrgY + top,
424 right-left-1, bottom-top-1, istart_angle, idiff_angle );
426 /* use the truncated values */
427 start_angle=(double)istart_angle*PI/64./180.;
428 end_angle=(double)(istart_angle+idiff_angle)*PI/64./180.;
429 /* calculate the endpoints and round correctly */
430 points[0].x = (int) floor(dc->w.DCOrgX + (right+left)/2.0 +
431 cos(start_angle) * (right-left-width*2+2) / 2. + 0.5);
432 points[0].y = (int) floor(dc->w.DCOrgY + (top+bottom)/2.0 -
433 sin(start_angle) * (bottom-top-width*2+2) / 2. + 0.5);
434 points[1].x = (int) floor(dc->w.DCOrgX + (right+left)/2.0 +
435 cos(end_angle) * (right-left-width*2+2) / 2. + 0.5);
436 points[1].y = (int) floor(dc->w.DCOrgY + (top+bottom)/2.0 -
437 sin(end_angle) * (bottom-top-width*2+2) / 2. + 0.5);
439 /* OK this stuff is optimized for Xfree86
440 * which is probably the most used server by
441 * wine users. Other X servers will not
442 * display correctly. (eXceed for instance)
443 * so if you feel you must change make sure that
444 * you either use Xfree86 or seperate your changes
445 * from these (compile switch or whatever)
449 points[3] = points[1];
450 points[1].x = dc->w.DCOrgX + xcenter;
451 points[1].y = dc->w.DCOrgY + ycenter;
452 points[2] = points[1];
453 dx1=points[1].x-points[0].x;
454 dy1=points[1].y-points[0].y;
455 if(((top-bottom) | -2) == -2)
456 if(dy1>0) points[1].y--;
458 if (((-dx1)*64)<=ABS(dy1)*37) points[0].x--;
459 if(((-dx1*9))<(dy1*16)) points[0].y--;
460 if( dy1<0 && ((dx1*9)) < (dy1*16)) points[0].y--;
462 if(dy1 < 0) points[0].y--;
463 if(((right-left) | -2) == -2) points[1].x--;
465 dx1=points[3].x-points[2].x;
466 dy1=points[3].y-points[2].y;
467 if(((top-bottom) | -2 ) == -2)
468 if(dy1 < 0) points[2].y--;
470 if( dy1>0) points[3].y--;
471 if(((right-left) | -2) == -2 ) points[2].x--;
474 if( dx1 * 64 < dy1 * -37 ) points[3].x--;
478 TSXDrawLines( display, physDev->drawable, physDev->gc,
479 points, lines+1, CoordModeOrigin );
482 physDev->pen.width = oldwidth;
483 physDev->pen.endcap = oldendcap;
488 /***********************************************************************
492 X11DRV_Arc( DC *dc, INT left, INT top, INT right, INT bottom,
493 INT xstart, INT ystart, INT xend, INT yend )
495 return X11DRV_DrawArc( dc, left, top, right, bottom,
496 xstart, ystart, xend, yend, 0 );
500 /***********************************************************************
504 X11DRV_Pie( DC *dc, INT left, INT top, INT right, INT bottom,
505 INT xstart, INT ystart, INT xend, INT yend )
507 return X11DRV_DrawArc( dc, left, top, right, bottom,
508 xstart, ystart, xend, yend, 2 );
511 /***********************************************************************
515 X11DRV_Chord( DC *dc, INT left, INT top, INT right, INT bottom,
516 INT xstart, INT ystart, INT xend, INT yend )
518 return X11DRV_DrawArc( dc, left, top, right, bottom,
519 xstart, ystart, xend, yend, 1 );
523 /***********************************************************************
527 X11DRV_Ellipse( DC *dc, INT left, INT top, INT right, INT bottom )
530 X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
532 left = XLPTODP( dc, left );
533 top = YLPTODP( dc, top );
534 right = XLPTODP( dc, right );
535 bottom = YLPTODP( dc, bottom );
536 if ((left == right) || (top == bottom)) return TRUE;
538 if (right < left) { INT tmp = right; right = left; left = tmp; }
539 if (bottom < top) { INT tmp = bottom; bottom = top; top = tmp; }
541 oldwidth = width = physDev->pen.width;
542 if (!width) width = 1;
543 if(physDev->pen.style == PS_NULL) width = 0;
545 if ((physDev->pen.style == PS_INSIDEFRAME))
547 if (2*width > (right-left)) width=(right-left + 1)/2;
548 if (2*width > (bottom-top)) width=(bottom-top + 1)/2;
550 right -= (width - 1) / 2;
552 bottom -= (width - 1) / 2;
554 if(width == 0) width = 1; /* more accurate */
555 physDev->pen.width = width;
557 if (X11DRV_SetupGCForBrush( dc ))
558 TSXFillArc( display, physDev->drawable, physDev->gc,
559 dc->w.DCOrgX + left, dc->w.DCOrgY + top,
560 right-left-1, bottom-top-1, 0, 360*64 );
561 if (X11DRV_SetupGCForPen( dc ))
562 TSXDrawArc( display, physDev->drawable, physDev->gc,
563 dc->w.DCOrgX + left, dc->w.DCOrgY + top,
564 right-left-1, bottom-top-1, 0, 360*64 );
565 physDev->pen.width = oldwidth;
570 /***********************************************************************
574 X11DRV_Rectangle(DC *dc, INT left, INT top, INT right, INT bottom)
576 INT width, oldwidth, oldjoinstyle;
577 X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
579 TRACE("(%d %d %d %d)\n",
580 left, top, right, bottom);
582 left = XLPTODP( dc, left );
583 top = YLPTODP( dc, top );
584 right = XLPTODP( dc, right );
585 bottom = YLPTODP( dc, bottom );
587 if ((left == right) || (top == bottom)) return TRUE;
589 if (right < left) { INT tmp = right; right = left; left = tmp; }
590 if (bottom < top) { INT tmp = bottom; bottom = top; top = tmp; }
592 oldwidth = width = physDev->pen.width;
593 if (!width) width = 1;
594 if(physDev->pen.style == PS_NULL) width = 0;
596 if ((physDev->pen.style == PS_INSIDEFRAME))
598 if (2*width > (right-left)) width=(right-left + 1)/2;
599 if (2*width > (bottom-top)) width=(bottom-top + 1)/2;
601 right -= (width - 1) / 2;
603 bottom -= (width - 1) / 2;
605 if(width == 1) width = 0;
606 physDev->pen.width = width;
607 oldjoinstyle = physDev->pen.linejoin;
608 if(physDev->pen.type != PS_GEOMETRIC)
609 physDev->pen.linejoin = PS_JOIN_MITER;
611 if ((right > left + width) && (bottom > top + width))
613 if (X11DRV_SetupGCForBrush( dc ))
614 TSXFillRectangle( display, physDev->drawable, physDev->gc,
615 dc->w.DCOrgX + left + (width + 1) / 2,
616 dc->w.DCOrgY + top + (width + 1) / 2,
617 right-left-width-1, bottom-top-width-1);
619 if (X11DRV_SetupGCForPen( dc ))
620 TSXDrawRectangle( display, physDev->drawable, physDev->gc,
621 dc->w.DCOrgX + left, dc->w.DCOrgY + top,
622 right-left-1, bottom-top-1 );
624 physDev->pen.width = oldwidth;
625 physDev->pen.linejoin = oldjoinstyle;
629 /***********************************************************************
633 X11DRV_RoundRect( DC *dc, INT left, INT top, INT right,
634 INT bottom, INT ell_width, INT ell_height )
636 INT width, oldwidth, oldendcap;
637 X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
639 TRACE("(%d %d %d %d %d %d\n",
640 left, top, right, bottom, ell_width, ell_height);
642 left = XLPTODP( dc, left );
643 top = YLPTODP( dc, top );
644 right = XLPTODP( dc, right );
645 bottom = YLPTODP( dc, bottom );
647 if ((left == right) || (top == bottom))
650 /* Make sure ell_width and ell_height are >= 1 otherwise XDrawArc gets
651 called with width/height < 0 */
652 ell_width = MAX(abs( ell_width * dc->vportExtX / dc->wndExtX ), 1);
653 ell_height = MAX(abs( ell_height * dc->vportExtY / dc->wndExtY ), 1);
655 /* Fix the coordinates */
657 if (right < left) { INT tmp = right; right = left; left = tmp; }
658 if (bottom < top) { INT tmp = bottom; bottom = top; top = tmp; }
660 oldwidth = width = physDev->pen.width;
661 oldendcap = physDev->pen.endcap;
662 if (!width) width = 1;
663 if(physDev->pen.style == PS_NULL) width = 0;
665 if ((physDev->pen.style == PS_INSIDEFRAME))
667 if (2*width > (right-left)) width=(right-left + 1)/2;
668 if (2*width > (bottom-top)) width=(bottom-top + 1)/2;
670 right -= (width - 1) / 2;
672 bottom -= (width - 1) / 2;
674 if(width == 0) width = 1;
675 physDev->pen.width = width;
676 physDev->pen.endcap = PS_ENDCAP_SQUARE;
678 if (X11DRV_SetupGCForBrush( dc ))
680 if (ell_width > (right-left) )
681 if (ell_height > (bottom-top) )
682 TSXFillArc( display, physDev->drawable, physDev->gc,
683 dc->w.DCOrgX + left, dc->w.DCOrgY + top,
684 right - left - 1, bottom - top - 1,
687 TSXFillArc( display, physDev->drawable, physDev->gc,
688 dc->w.DCOrgX + left, dc->w.DCOrgY + top,
689 right - left - 1, ell_height, 0, 180 * 64 );
690 TSXFillArc( display, physDev->drawable, physDev->gc,
692 dc->w.DCOrgY + bottom - ell_height - 1,
693 right - left - 1, ell_height, 180 * 64,
696 else if (ell_height > (bottom-top) ){
697 TSXFillArc( display, physDev->drawable, physDev->gc,
698 dc->w.DCOrgX + left, dc->w.DCOrgY + top,
699 ell_width, bottom - top - 1, 90 * 64, 180 * 64 );
700 TSXFillArc( display, physDev->drawable, physDev->gc,
701 dc->w.DCOrgX + right - ell_width -1, dc->w.DCOrgY + top,
702 ell_width, bottom - top - 1, 270 * 64, 180 * 64 );
704 TSXFillArc( display, physDev->drawable, physDev->gc,
705 dc->w.DCOrgX + left, dc->w.DCOrgY + top,
706 ell_width, ell_height, 90 * 64, 90 * 64 );
707 TSXFillArc( display, physDev->drawable, physDev->gc,
709 dc->w.DCOrgY + bottom - ell_height - 1,
710 ell_width, ell_height, 180 * 64, 90 * 64 );
711 TSXFillArc( display, physDev->drawable, physDev->gc,
712 dc->w.DCOrgX + right - ell_width - 1,
713 dc->w.DCOrgY + bottom - ell_height - 1,
714 ell_width, ell_height, 270 * 64, 90 * 64 );
715 TSXFillArc( display, physDev->drawable, physDev->gc,
716 dc->w.DCOrgX + right - ell_width - 1,
718 ell_width, ell_height, 0, 90 * 64 );
720 if (ell_width < right - left)
722 TSXFillRectangle( display, physDev->drawable, physDev->gc,
723 dc->w.DCOrgX + left + (ell_width + 1) / 2,
724 dc->w.DCOrgY + top + 1,
725 right - left - ell_width - 1,
726 (ell_height + 1) / 2 - 1);
727 TSXFillRectangle( display, physDev->drawable, physDev->gc,
728 dc->w.DCOrgX + left + (ell_width + 1) / 2,
729 dc->w.DCOrgY + bottom - (ell_height) / 2 - 1,
730 right - left - ell_width - 1,
733 if (ell_height < bottom - top)
735 TSXFillRectangle( display, physDev->drawable, physDev->gc,
736 dc->w.DCOrgX + left + 1,
737 dc->w.DCOrgY + top + (ell_height + 1) / 2,
739 bottom - top - ell_height - 1);
742 /* FIXME: this could be done with on X call
743 * more efficient and probably more correct
744 * on any X server: XDrawArcs will draw
745 * straight horizontal and vertical lines
746 * if width or height are zero.
748 * BTW this stuff is optimized for an Xfree86 server
749 * read the comments inside the X11DRV_DrawArc function
751 if (X11DRV_SetupGCForPen(dc)) {
752 if (ell_width > (right-left) )
753 if (ell_height > (bottom-top) )
754 TSXDrawArc( display, physDev->drawable, physDev->gc,
755 dc->w.DCOrgX + left, dc->w.DCOrgY + top,
756 right - left - 1, bottom -top - 1, 0 , 360 * 64 );
758 TSXDrawArc( display, physDev->drawable, physDev->gc,
759 dc->w.DCOrgX + left, dc->w.DCOrgY + top,
760 right - left - 1, ell_height - 1, 0 , 180 * 64 );
761 TSXDrawArc( display, physDev->drawable, physDev->gc,
763 dc->w.DCOrgY + bottom - ell_height,
764 right - left - 1, ell_height - 1, 180 * 64 , 180 * 64 );
766 else if (ell_height > (bottom-top) ){
767 TSXDrawArc( display, physDev->drawable, physDev->gc,
768 dc->w.DCOrgX + left, dc->w.DCOrgY + top,
769 ell_width - 1 , bottom - top - 1, 90 * 64 , 180 * 64 );
770 TSXDrawArc( display, physDev->drawable, physDev->gc,
771 dc->w.DCOrgX + right - ell_width,
773 ell_width - 1 , bottom - top - 1, 270 * 64 , 180 * 64 );
775 TSXDrawArc( display, physDev->drawable, physDev->gc,
776 dc->w.DCOrgX + left, dc->w.DCOrgY + top,
777 ell_width - 1, ell_height - 1, 90 * 64, 90 * 64 );
778 TSXDrawArc( display, physDev->drawable, physDev->gc,
779 dc->w.DCOrgX + left, dc->w.DCOrgY + bottom - ell_height,
780 ell_width - 1, ell_height - 1, 180 * 64, 90 * 64 );
781 TSXDrawArc( display, physDev->drawable, physDev->gc,
782 dc->w.DCOrgX + right - ell_width,
783 dc->w.DCOrgY + bottom - ell_height,
784 ell_width - 1, ell_height - 1, 270 * 64, 90 * 64 );
785 TSXDrawArc( display, physDev->drawable, physDev->gc,
786 dc->w.DCOrgX + right - ell_width, dc->w.DCOrgY + top,
787 ell_width - 1, ell_height - 1, 0, 90 * 64 );
789 if (ell_width < right - left)
791 TSXDrawLine( display, physDev->drawable, physDev->gc,
792 dc->w.DCOrgX + left + ell_width / 2,
794 dc->w.DCOrgX + right - (ell_width+1) / 2,
796 TSXDrawLine( display, physDev->drawable, physDev->gc,
797 dc->w.DCOrgX + left + ell_width / 2 ,
798 dc->w.DCOrgY + bottom - 1,
799 dc->w.DCOrgX + right - (ell_width+1)/ 2,
800 dc->w.DCOrgY + bottom - 1);
802 if (ell_height < bottom - top)
804 TSXDrawLine( display, physDev->drawable, physDev->gc,
805 dc->w.DCOrgX + right - 1,
806 dc->w.DCOrgY + top + ell_height / 2,
807 dc->w.DCOrgX + right - 1,
808 dc->w.DCOrgY + bottom - (ell_height+1) / 2);
809 TSXDrawLine( display, physDev->drawable, physDev->gc,
811 dc->w.DCOrgY + top + ell_height / 2,
813 dc->w.DCOrgY + bottom - (ell_height+1) / 2);
816 physDev->pen.width = oldwidth;
817 physDev->pen.endcap = oldendcap;
822 /***********************************************************************
826 X11DRV_SetPixel( DC *dc, INT x, INT y, COLORREF color )
829 X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
831 x = dc->w.DCOrgX + XLPTODP( dc, x );
832 y = dc->w.DCOrgY + YLPTODP( dc, y );
833 pixel = X11DRV_PALETTE_ToPhysical( dc, color );
835 TSXSetForeground( display, physDev->gc, pixel );
836 TSXSetFunction( display, physDev->gc, GXcopy );
837 TSXDrawPoint( display, physDev->drawable, physDev->gc, x, y );
839 /* inefficient but simple... */
841 return X11DRV_PALETTE_ToLogical(pixel);
845 /***********************************************************************
849 X11DRV_GetPixel( DC *dc, INT x, INT y )
851 static Pixmap pixmap = 0;
854 X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
856 x = dc->w.DCOrgX + XLPTODP( dc, x );
857 y = dc->w.DCOrgY + YLPTODP( dc, y );
858 EnterCriticalSection( &X11DRV_CritSection );
859 if (dc->w.flags & DC_MEMORY)
861 image = XGetImage( display, physDev->drawable, x, y, 1, 1,
862 AllPlanes, ZPixmap );
866 /* If we are reading from the screen, use a temporary copy */
867 /* to avoid a BadMatch error */
868 if (!pixmap) pixmap = XCreatePixmap( display, X11DRV_GetXRootWindow(),
869 1, 1, dc->w.bitsPerPixel );
870 XCopyArea( display, physDev->drawable, pixmap, BITMAP_colorGC,
872 image = XGetImage( display, pixmap, 0, 0, 1, 1, AllPlanes, ZPixmap );
874 pixel = XGetPixel( image, 0, 0 );
875 XDestroyImage( image );
876 LeaveCriticalSection( &X11DRV_CritSection );
878 return X11DRV_PALETTE_ToLogical(pixel);
882 /***********************************************************************
886 X11DRV_PaintRgn( DC *dc, HRGN hrgn )
889 HRGN tmpVisRgn, prevVisRgn;
890 HDC hdc = dc->hSelf; /* FIXME: should not mix dc/hdc this way */
891 X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
893 if (!(tmpVisRgn = CreateRectRgn( 0, 0, 0, 0 ))) return FALSE;
895 /* Transform region into device co-ords */
896 if ( !REGION_LPTODP( hdc, tmpVisRgn, hrgn )
897 || OffsetRgn( tmpVisRgn, dc->w.DCOrgX, dc->w.DCOrgY ) == ERROR) {
898 DeleteObject( tmpVisRgn );
902 /* Modify visible region */
903 if (!(prevVisRgn = SaveVisRgn16( hdc ))) {
904 DeleteObject( tmpVisRgn );
907 CombineRgn( tmpVisRgn, prevVisRgn, tmpVisRgn, RGN_AND );
908 SelectVisRgn16( hdc, tmpVisRgn );
909 DeleteObject( tmpVisRgn );
911 /* Fill the region */
913 GetRgnBox( dc->w.hGCClipRgn, &box );
914 if (X11DRV_SetupGCForBrush( dc ))
915 TSXFillRectangle( display, physDev->drawable, physDev->gc,
917 box.right-box.left, box.bottom-box.top );
919 /* Restore the visible region */
921 RestoreVisRgn16( hdc );
925 /**********************************************************************
929 X11DRV_Polyline( DC *dc, const POINT* pt, INT count )
934 X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
936 if((oldwidth = physDev->pen.width) == 0) physDev->pen.width = 1;
938 points = (XPoint *) xmalloc (sizeof (XPoint) * (count));
939 for (i = 0; i < count; i++)
941 points[i].x = dc->w.DCOrgX + XLPTODP( dc, pt[i].x );
942 points[i].y = dc->w.DCOrgY + YLPTODP( dc, pt[i].y );
945 if (X11DRV_SetupGCForPen ( dc ))
946 TSXDrawLines( display, physDev->drawable, physDev->gc,
947 points, count, CoordModeOrigin );
950 physDev->pen.width = oldwidth;
955 /**********************************************************************
959 X11DRV_Polygon( DC *dc, const POINT* pt, INT count )
963 X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
965 points = (XPoint *) xmalloc (sizeof (XPoint) * (count+1));
966 for (i = 0; i < count; i++)
968 points[i].x = dc->w.DCOrgX + XLPTODP( dc, pt[i].x );
969 points[i].y = dc->w.DCOrgY + YLPTODP( dc, pt[i].y );
971 points[count] = points[0];
973 if (X11DRV_SetupGCForBrush( dc ))
974 TSXFillPolygon( display, physDev->drawable, physDev->gc,
975 points, count+1, Complex, CoordModeOrigin);
977 if (X11DRV_SetupGCForPen ( dc ))
978 TSXDrawLines( display, physDev->drawable, physDev->gc,
979 points, count+1, CoordModeOrigin );
986 /**********************************************************************
990 X11DRV_PolyPolygon( DC *dc, const POINT* pt, const INT* counts, UINT polygons)
993 X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
995 /* FIXME: The points should be converted to device coords before */
996 /* creating the region. */
998 hrgn = CreatePolyPolygonRgn( pt, counts, polygons, dc->w.polyFillMode );
999 X11DRV_PaintRgn( dc, hrgn );
1000 DeleteObject( hrgn );
1002 /* Draw the outline of the polygons */
1004 if (X11DRV_SetupGCForPen ( dc ))
1009 for (i = 0; i < polygons; i++) if (counts[i] > max) max = counts[i];
1010 points = (XPoint *) xmalloc( sizeof(XPoint) * (max+1) );
1012 for (i = 0; i < polygons; i++)
1014 for (j = 0; j < counts[i]; j++)
1016 points[j].x = dc->w.DCOrgX + XLPTODP( dc, pt->x );
1017 points[j].y = dc->w.DCOrgY + YLPTODP( dc, pt->y );
1020 points[j] = points[0];
1021 TSXDrawLines( display, physDev->drawable, physDev->gc,
1022 points, j + 1, CoordModeOrigin );
1030 /**********************************************************************
1031 * X11DRV_PolyPolyline
1034 X11DRV_PolyPolyline( DC *dc, const POINT* pt, const DWORD* counts, DWORD polylines )
1036 X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
1037 if (X11DRV_SetupGCForPen ( dc ))
1042 for (i = 0; i < polylines; i++) if (counts[i] > max) max = counts[i];
1043 points = (XPoint *) xmalloc( sizeof(XPoint) * (max+1) );
1045 for (i = 0; i < polylines; i++)
1047 for (j = 0; j < counts[i]; j++)
1049 points[j].x = dc->w.DCOrgX + XLPTODP( dc, pt->x );
1050 points[j].y = dc->w.DCOrgY + YLPTODP( dc, pt->y );
1053 points[j] = points[0];
1054 TSXDrawLines( display, physDev->drawable, physDev->gc,
1055 points, j + 1, CoordModeOrigin );
1063 /**********************************************************************
1064 * X11DRV_InternalFloodFill
1066 * Internal helper function for flood fill.
1067 * (xorg,yorg) is the origin of the X image relative to the drawable.
1068 * (x,y) is relative to the origin of the X image.
1070 static void X11DRV_InternalFloodFill(XImage *image, DC *dc,
1073 Pixel pixel, WORD fillType )
1075 X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
1078 #define TO_FLOOD(x,y) ((fillType == FLOODFILLBORDER) ? \
1079 (XGetPixel(image,x,y) != pixel) : \
1080 (XGetPixel(image,x,y) == pixel))
1082 if (!TO_FLOOD(x,y)) return;
1084 /* Find left and right boundaries */
1087 while ((left > 0) && TO_FLOOD( left-1, y )) left--;
1088 while ((right < image->width) && TO_FLOOD( right, y )) right++;
1089 XFillRectangle( display, physDev->drawable, physDev->gc,
1090 xOrg + left, yOrg + y, right-left, 1 );
1092 /* Set the pixels of this line so we don't fill it again */
1094 for (x = left; x < right; x++)
1096 if (fillType == FLOODFILLBORDER) XPutPixel( image, x, y, pixel );
1097 else XPutPixel( image, x, y, ~pixel );
1100 /* Fill the line above */
1107 while ((x < right) && !TO_FLOOD(x,y)) x++;
1108 if (x >= right) break;
1109 while ((x < right) && TO_FLOOD(x,y)) x++;
1110 X11DRV_InternalFloodFill(image, dc, x-1, y,
1111 xOrg, yOrg, pixel, fillType );
1115 /* Fill the line below */
1117 if ((y += 2) < image->height)
1122 while ((x < right) && !TO_FLOOD(x,y)) x++;
1123 if (x >= right) break;
1124 while ((x < right) && TO_FLOOD(x,y)) x++;
1125 X11DRV_InternalFloodFill(image, dc, x-1, y,
1126 xOrg, yOrg, pixel, fillType );
1133 /**********************************************************************
1134 * X11DRV_DoFloodFill
1136 * Main flood-fill routine.
1138 * The Xlib critical section must be entered before calling this function.
1141 struct FloodFill_params
1150 static BOOL X11DRV_DoFloodFill( const struct FloodFill_params *params )
1154 DC *dc = params->dc;
1155 X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
1157 if (GetRgnBox( dc->w.hGCClipRgn, &rect ) == ERROR) return FALSE;
1159 if (!(image = XGetImage( display, physDev->drawable,
1162 rect.right - rect.left,
1163 rect.bottom - rect.top,
1164 AllPlanes, ZPixmap ))) return FALSE;
1166 if (X11DRV_SetupGCForBrush( dc ))
1168 /* ROP mode is always GXcopy for flood-fill */
1169 XSetFunction( display, physDev->gc, GXcopy );
1170 X11DRV_InternalFloodFill(image, dc,
1171 XLPTODP(dc,params->x) + dc->w.DCOrgX - rect.left,
1172 YLPTODP(dc,params->y) + dc->w.DCOrgY - rect.top,
1175 X11DRV_PALETTE_ToPhysical( dc, params->color ),
1179 XDestroyImage( image );
1184 /**********************************************************************
1185 * X11DRV_ExtFloodFill
1188 X11DRV_ExtFloodFill( DC *dc, INT x, INT y, COLORREF color,
1192 struct FloodFill_params params;
1194 TRACE("X11DRV_ExtFloodFill %d,%d %06lx %d\n",
1195 x, y, color, fillType );
1200 params.color = color;
1201 params.fillType = fillType;
1203 if (!PtVisible( dc->hSelf, x, y )) return FALSE;
1204 EnterCriticalSection( &X11DRV_CritSection );
1205 result = CALL_LARGE_STACK( X11DRV_DoFloodFill, ¶ms );
1206 LeaveCriticalSection( &X11DRV_CritSection );
1210 /******************************************************************
1212 * *Very* simple bezier drawing code,
1214 * It uses a recursive algorithm to divide the curve in a series
1215 * of straight line segements. Not ideal but for me sufficient.
1216 * If you are in need for something better look for some incremental
1219 * 7 July 1998 Rein Klazes
1223 * some macro definitions for bezier drawing
1225 * to avoid trucation errors the coordinates are
1226 * shifted upwards. When used in drawing they are
1227 * shifted down again, including correct rounding
1228 * and avoiding floating point arithmatic
1229 * 4 bits should allow 27 bits coordinates which I saw
1230 * somewere in the win32 doc's
1234 #define BEZIERSHIFTBITS 4
1235 #define BEZIERSHIFTUP(x) ((x)<<BEZIERSHIFTBITS)
1236 #define BEZIERPIXEL BEZIERSHIFTUP(1)
1237 #define BEZIERSHIFTDOWN(x) (((x)+(1<<(BEZIERSHIFTBITS-1)))>>BEZIERSHIFTBITS)
1238 /* maximum depth of recursion */
1239 #define BEZIERMAXDEPTH 8
1241 /* size of array to store points on */
1242 /* enough for one curve */
1243 #define BEZMAXPOINTS (150)
1245 /* calculate Bezier average, in this case the middle
1246 * correctly rounded...
1249 #define BEZIERMIDDLE(Mid, P1, P2) \
1250 (Mid).x=((P1).x+(P2).x + 1)/2;\
1251 (Mid).y=((P1).y+(P2).y + 1)/2;
1253 /**********************************************************
1254 * BezierCheck helper function to check
1255 * that recursion can be terminated
1256 * Points[0] and Points[3] are begin and endpoint
1257 * Points[1] and Points[2] are control points
1258 * level is the recursion depth
1259 * returns true if the recusion can be terminated
1261 static BOOL BezierCheck( int level, POINT *Points)
1264 dx=Points[3].x-Points[0].x;
1265 dy=Points[3].y-Points[0].y;
1266 if(ABS(dy)<=ABS(dx)){/* shallow line */
1267 /* check that control points are between begin and end */
1268 if(Points[1].x < Points[0].x){
1269 if(Points[1].x < Points[3].x)
1272 if(Points[1].x > Points[3].x)
1274 if(Points[2].x < Points[0].x){
1275 if(Points[2].x < Points[3].x)
1278 if(Points[2].x > Points[3].x)
1280 dx=BEZIERSHIFTDOWN(dx);
1281 if(!dx) return TRUE;
1282 if(abs(Points[1].y-Points[0].y-(dy/dx)*
1283 BEZIERSHIFTDOWN(Points[1].x-Points[0].x)) > BEZIERPIXEL ||
1284 abs(Points[2].y-Points[0].y-(dy/dx)*
1285 BEZIERSHIFTDOWN(Points[2].x-Points[0].x)) > BEZIERPIXEL )
1289 }else{ /* steep line */
1290 /* check that control points are between begin and end */
1291 if(Points[1].y < Points[0].y){
1292 if(Points[1].y < Points[3].y)
1295 if(Points[1].y > Points[3].y)
1297 if(Points[2].y < Points[0].y){
1298 if(Points[2].y < Points[3].y)
1301 if(Points[2].y > Points[3].y)
1303 dy=BEZIERSHIFTDOWN(dy);
1304 if(!dy) return TRUE;
1305 if(abs(Points[1].x-Points[0].x-(dx/dy)*
1306 BEZIERSHIFTDOWN(Points[1].y-Points[0].y)) > BEZIERPIXEL ||
1307 abs(Points[2].x-Points[0].x-(dx/dy)*
1308 BEZIERSHIFTDOWN(Points[2].y-Points[0].y)) > BEZIERPIXEL )
1315 /***********************************************************************
1317 * Draw a -what microsoft calls- bezier curve
1318 * The routine recursively devides the curve
1319 * in two parts until a straight line can be drawn
1321 * level recusion depth counted backwards
1323 * Points array of begin(0), end(3) and control points(1 and 2)
1324 * XPoints array with points calculated sofar
1325 * *pIx nr points calculated sofar
1328 static void X11DRV_Bezier(int level, DC * dc, POINT *Points,
1329 XPoint* xpoints, unsigned int* pIx)
1331 X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
1333 if(*pIx == BEZMAXPOINTS){
1334 if (X11DRV_SetupGCForPen( dc ))
1335 TSXDrawLines( display, physDev->drawable, physDev->gc,
1336 xpoints, *pIx, CoordModeOrigin );
1339 if(!level || BezierCheck(level, Points)) {
1341 xpoints[*pIx].x= dc->w.DCOrgX + BEZIERSHIFTDOWN(Points[0].x);
1342 xpoints[*pIx].y= dc->w.DCOrgY + BEZIERSHIFTDOWN(Points[0].y);
1345 xpoints[*pIx].x= dc->w.DCOrgX + BEZIERSHIFTDOWN(Points[3].x);
1346 xpoints[*pIx].y= dc->w.DCOrgY + BEZIERSHIFTDOWN(Points[3].y);
1349 POINT Points2[4]; /* for the second recursive call */
1350 Points2[3]=Points[3];
1351 BEZIERMIDDLE(Points2[2], Points[2], Points[3]);
1352 BEZIERMIDDLE(Points2[0], Points[1], Points[2]);
1353 BEZIERMIDDLE(Points2[1],Points2[0],Points2[2]);
1355 BEZIERMIDDLE(Points[1], Points[0], Points[1]);
1356 BEZIERMIDDLE(Points[2], Points[1], Points2[0]);
1357 BEZIERMIDDLE(Points[3], Points[2], Points2[1]);
1359 Points2[0]=Points[3];
1361 /* do the two halves */
1362 X11DRV_Bezier(level-1, dc, Points, xpoints, pIx);
1363 X11DRV_Bezier(level-1, dc, Points2, xpoints, pIx);
1367 /***********************************************************************
1369 * Implement functionality for PolyBezier and PolyBezierTo
1371 * [i] dc pointer to device context
1372 * [i] start, first point in curve
1373 * [i] BezierPoints , array of point filled with rest of the points
1374 * [i] count, number of points in BezierPoints, must be a
1378 X11DRV_PolyBezier(DC *dc, POINT start, const POINT* BezierPoints, DWORD count)
1384 X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
1386 TRACE("dc=%p count=%ld %ld,%ld - %ld,%ld - %ld,%ld - %ld,%ld\n",
1389 (BezierPoints+0)->x, (BezierPoints+0)->y,
1390 (BezierPoints+1)->x, (BezierPoints+1)->y,
1391 (BezierPoints+2)->x, (BezierPoints+2)->y);
1392 if(!count || count % 3){/* paranoid */
1393 WARN(" bad value for count : %ld\n", count);
1396 xpoints=(XPoint*) xmalloc( sizeof(XPoint)*BEZMAXPOINTS);
1397 start.x=BEZIERSHIFTUP(XLPTODP(dc,start.x));
1398 start.y=BEZIERSHIFTUP(YLPTODP(dc,start.y));
1402 Points[i].x= BEZIERSHIFTUP(XLPTODP(dc,BezierPoints->x));
1403 Points[i].y= BEZIERSHIFTUP(YLPTODP(dc,BezierPoints->y));
1407 X11DRV_Bezier(BEZIERMAXDEPTH , dc, Points, xpoints, &ix );
1410 if (ix && X11DRV_SetupGCForPen( dc ))
1411 TSXDrawLines( display, physDev->drawable, physDev->gc,
1412 xpoints, ix, CoordModeOrigin );
1417 /**********************************************************************
1421 X11DRV_SetBkColor( DC *dc, COLORREF color )
1423 X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
1426 oldColor = dc->w.backgroundColor;
1427 dc->w.backgroundColor = color;
1429 physDev->backgroundPixel = X11DRV_PALETTE_ToPhysical( dc, color );
1434 /**********************************************************************
1435 * X11DRV_SetTextColor
1438 X11DRV_SetTextColor( DC *dc, COLORREF color )
1440 X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
1443 oldColor = dc->w.textColor;
1444 dc->w.textColor = color;
1446 physDev->textPixel = X11DRV_PALETTE_ToPhysical( dc, color );
1451 #endif /* !defined(X_DISPLAY_MISSING) */