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>
46 DEFAULT_DEBUG_CHANNEL(graphics)
48 #define ABS(x) ((x)<0?(-(x)):(x))
50 /* ROP code to GC function conversion */
51 const int X11DRV_XROPfunction[16] =
53 GXclear, /* R2_BLACK */
54 GXnor, /* R2_NOTMERGEPEN */
55 GXandInverted, /* R2_MASKNOTPEN */
56 GXcopyInverted, /* R2_NOTCOPYPEN */
57 GXandReverse, /* R2_MASKPENNOT */
58 GXinvert, /* R2_NOT */
59 GXxor, /* R2_XORPEN */
60 GXnand, /* R2_NOTMASKPEN */
61 GXand, /* R2_MASKPEN */
62 GXequiv, /* R2_NOTXORPEN */
64 GXorInverted, /* R2_MERGENOTPEN */
65 GXcopy, /* R2_COPYPEN */
66 GXorReverse, /* R2_MERGEPENNOT */
67 GXor, /* R2_MERGEPEN */
72 /***********************************************************************
73 * X11DRV_SetupGCForPatBlt
75 * Setup the GC for a PatBlt operation using current brush.
76 * If fMapColors is TRUE, X pixels are mapped to Windows colors.
77 * Return FALSE if brush is BS_NULL, TRUE otherwise.
79 BOOL X11DRV_SetupGCForPatBlt( DC * dc, GC gc, BOOL fMapColors )
84 X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
86 if (physDev->brush.style == BS_NULL) return FALSE;
87 if (physDev->brush.pixel == -1)
89 /* Special case used for monochrome pattern brushes.
90 * We need to swap foreground and background because
91 * Windows does it the wrong way...
93 val.foreground = physDev->backgroundPixel;
94 val.background = physDev->textPixel;
98 val.foreground = physDev->brush.pixel;
99 val.background = physDev->backgroundPixel;
101 if (fMapColors && X11DRV_PALETTE_XPixelToPalette)
103 val.foreground = X11DRV_PALETTE_XPixelToPalette[val.foreground];
104 val.background = X11DRV_PALETTE_XPixelToPalette[val.background];
107 if (dc->w.flags & DC_DIRTY) CLIPPING_UpdateGCRegion(dc);
109 val.function = X11DRV_XROPfunction[dc->w.ROPmode-1];
111 ** Let's replace GXinvert by GXxor with (black xor white)
112 ** This solves the selection color and leak problems in excel
113 ** FIXME : Let's do that only if we work with X-pixels, not with Win-pixels
115 if (val.function == GXinvert)
117 val.foreground = BlackPixelOfScreen(X11DRV_GetXScreen()) ^ WhitePixelOfScreen(X11DRV_GetXScreen());
118 val.function = GXxor;
120 val.fill_style = physDev->brush.fillStyle;
121 switch(val.fill_style)
124 case FillOpaqueStippled:
125 if (dc->w.backgroundMode==OPAQUE) val.fill_style = FillOpaqueStippled;
126 val.stipple = physDev->brush.pixmap;
131 if (fMapColors && X11DRV_PALETTE_XPixelToPalette)
135 EnterCriticalSection( &X11DRV_CritSection );
136 pixmap = XCreatePixmap( display,
137 X11DRV_GetXRootWindow(),
139 MONITOR_GetDepth(&MONITOR_PrimaryMonitor) );
140 image = XGetImage( display, physDev->brush.pixmap, 0, 0, 8, 8,
141 AllPlanes, ZPixmap );
142 for (y = 0; y < 8; y++)
143 for (x = 0; x < 8; x++)
144 XPutPixel( image, x, y,
145 X11DRV_PALETTE_XPixelToPalette[XGetPixel( image, x, y)] );
146 XPutImage( display, pixmap, gc, image, 0, 0, 0, 0, 8, 8 );
147 XDestroyImage( image );
148 LeaveCriticalSection( &X11DRV_CritSection );
151 else val.tile = physDev->brush.pixmap;
159 val.ts_x_origin = dc->w.DCOrgX + dc->w.brushOrgX;
160 val.ts_y_origin = dc->w.DCOrgY + dc->w.brushOrgY;
161 val.fill_rule = (dc->w.polyFillMode==WINDING) ? WindingRule : EvenOddRule;
162 TSXChangeGC( display, gc,
163 GCFunction | GCForeground | GCBackground | GCFillStyle |
164 GCFillRule | GCTileStipXOrigin | GCTileStipYOrigin | mask,
166 if (pixmap) TSXFreePixmap( display, pixmap );
171 /***********************************************************************
172 * X11DRV_SetupGCForBrush
174 * Setup physDev->gc for drawing operations using current brush.
175 * Return FALSE if brush is BS_NULL, TRUE otherwise.
177 BOOL X11DRV_SetupGCForBrush( DC * dc )
179 X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
180 return X11DRV_SetupGCForPatBlt( dc, physDev->gc, FALSE );
184 /***********************************************************************
185 * X11DRV_SetupGCForPen
187 * Setup physDev->gc for drawing operations using current pen.
188 * Return FALSE if pen is PS_NULL, TRUE otherwise.
190 BOOL X11DRV_SetupGCForPen( DC * dc )
193 X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
195 if (physDev->pen.style == PS_NULL) return FALSE;
197 if (dc->w.flags & DC_DIRTY) CLIPPING_UpdateGCRegion(dc);
199 switch (dc->w.ROPmode)
202 val.foreground = BlackPixelOfScreen( X11DRV_GetXScreen() );
203 val.function = GXcopy;
206 val.foreground = WhitePixelOfScreen( X11DRV_GetXScreen() );
207 val.function = GXcopy;
210 val.foreground = physDev->pen.pixel;
211 /* It is very unlikely someone wants to XOR with 0 */
212 /* This fixes the rubber-drawings in paintbrush */
213 if (val.foreground == 0)
214 val.foreground = BlackPixelOfScreen( X11DRV_GetXScreen() )
215 ^ WhitePixelOfScreen( X11DRV_GetXScreen() );
216 val.function = GXxor;
219 val.foreground = physDev->pen.pixel;
220 val.function = X11DRV_XROPfunction[dc->w.ROPmode-1];
222 val.background = physDev->backgroundPixel;
223 val.fill_style = FillSolid;
224 if ((physDev->pen.width <= 1) &&
225 (physDev->pen.style != PS_SOLID) &&
226 (physDev->pen.style != PS_INSIDEFRAME))
228 TSXSetDashes( display, physDev->gc, 0, physDev->pen.dashes,
229 physDev->pen.dash_len );
230 val.line_style = (dc->w.backgroundMode == OPAQUE) ?
231 LineDoubleDash : LineOnOffDash;
233 else val.line_style = LineSolid;
234 val.line_width = physDev->pen.width;
235 if (val.line_width <= 1) {
236 val.cap_style = CapNotLast;
238 switch (physDev->pen.endcap)
240 case PS_ENDCAP_SQUARE:
241 val.cap_style = CapProjecting;
244 val.cap_style = CapButt;
246 case PS_ENDCAP_ROUND:
248 val.cap_style = CapRound;
251 switch (physDev->pen.linejoin)
254 val.join_style = JoinBevel;
257 val.join_style = JoinMiter;
261 val.join_style = JoinRound;
263 TSXChangeGC( display, physDev->gc,
264 GCFunction | GCForeground | GCBackground | GCLineWidth |
265 GCLineStyle | GCCapStyle | GCJoinStyle | GCFillStyle, &val );
270 /***********************************************************************
271 * X11DRV_SetupGCForText
273 * Setup physDev->gc for text drawing operations.
274 * Return FALSE if the font is null, TRUE otherwise.
276 BOOL X11DRV_SetupGCForText( DC * dc )
278 X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
279 XFontStruct* xfs = XFONT_GetFontStruct( physDev->font );
285 if (dc->w.flags & DC_DIRTY) CLIPPING_UpdateGCRegion(dc);
287 val.function = GXcopy; /* Text is always GXcopy */
288 val.foreground = physDev->textPixel;
289 val.background = physDev->backgroundPixel;
290 val.fill_style = FillSolid;
293 TSXChangeGC( display, physDev->gc,
294 GCFunction | GCForeground | GCBackground | GCFillStyle |
298 WARN(graphics, "Physical font failure\n" );
303 /**********************************************************************
307 X11DRV_MoveToEx(DC *dc,INT x,INT y,LPPOINT pt) {
310 pt->x = dc->w.CursPosX;
311 pt->y = dc->w.CursPosY;
318 /***********************************************************************
322 X11DRV_LineTo( DC *dc, INT x, INT y )
324 X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
326 if (X11DRV_SetupGCForPen( dc ))
327 TSXDrawLine(display, physDev->drawable, physDev->gc,
328 dc->w.DCOrgX + XLPTODP( dc, dc->w.CursPosX ),
329 dc->w.DCOrgY + YLPTODP( dc, dc->w.CursPosY ),
330 dc->w.DCOrgX + XLPTODP( dc, x ),
331 dc->w.DCOrgY + YLPTODP( dc, y ) );
339 /***********************************************************************
342 * Helper functions for Arc(), Chord() and Pie().
343 * 'lines' is the number of lines to draw: 0 for Arc, 1 for Chord, 2 for Pie.
347 X11DRV_DrawArc( DC *dc, INT left, INT top, INT right,
348 INT bottom, INT xstart, INT ystart,
349 INT xend, INT yend, INT lines )
351 INT xcenter, ycenter, istart_angle, idiff_angle;
352 INT width, oldwidth, oldendcap;
353 double start_angle, end_angle;
355 X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
357 left = XLPTODP( dc, left );
358 top = YLPTODP( dc, top );
359 right = XLPTODP( dc, right );
360 bottom = YLPTODP( dc, bottom );
361 xstart = XLPTODP( dc, xstart );
362 ystart = YLPTODP( dc, ystart );
363 xend = XLPTODP( dc, xend );
364 yend = YLPTODP( dc, yend );
366 if (right < left) { INT tmp = right; right = left; left = tmp; }
367 if (bottom < top) { INT tmp = bottom; bottom = top; top = tmp; }
368 if ((left == right) || (top == bottom)
369 ||(lines && ((right-left==1)||(bottom-top==1)))) return TRUE;
371 oldwidth = width = physDev->pen.width;
372 oldendcap = physDev->pen.endcap;
373 if (!width) width = 1;
374 if(physDev->pen.style == PS_NULL) width = 0;
376 if ((physDev->pen.style == PS_INSIDEFRAME))
378 if (2*width > (right-left)) width=(right-left + 1)/2;
379 if (2*width > (bottom-top)) width=(bottom-top + 1)/2;
381 right -= (width - 1) / 2;
383 bottom -= (width - 1) / 2;
385 if(width == 0) width = 1; /* more accurate */
386 physDev->pen.width = width;
387 physDev->pen.endcap = PS_ENDCAP_SQUARE;
389 xcenter = (right + left) / 2;
390 ycenter = (bottom + top) / 2;
391 start_angle = atan2( (double)(ycenter-ystart)*(right-left),
392 (double)(xstart-xcenter)*(bottom-top) );
393 end_angle = atan2( (double)(ycenter-yend)*(right-left),
394 (double)(xend-xcenter)*(bottom-top) );
395 if ((xstart==xend)&&(ystart==yend))
396 { /* A lazy program delivers xstart=xend=ystart=yend=0) */
400 else /* notorious cases */
401 if ((start_angle == PI)&&( end_angle <0))
404 if ((end_angle == PI)&&( start_angle <0))
406 istart_angle = (INT)(start_angle * 180 * 64 / PI + 0.5);
407 idiff_angle = (INT)((end_angle - start_angle) * 180 * 64 / PI + 0.5);
408 if (idiff_angle <= 0) idiff_angle += 360 * 64;
410 /* Fill arc with brush if Chord() or Pie() */
412 if ((lines > 0) && X11DRV_SetupGCForBrush( dc )) {
413 TSXSetArcMode( display, physDev->gc,
414 (lines==1) ? ArcChord : ArcPieSlice);
415 TSXFillArc( display, physDev->drawable, physDev->gc,
416 dc->w.DCOrgX + left, dc->w.DCOrgY + top,
417 right-left-1, bottom-top-1, istart_angle, idiff_angle );
420 /* Draw arc and lines */
422 if (X11DRV_SetupGCForPen( dc )){
423 TSXDrawArc( display, physDev->drawable, physDev->gc,
424 dc->w.DCOrgX + left, dc->w.DCOrgY + top,
425 right-left-1, bottom-top-1, istart_angle, idiff_angle );
427 /* use the truncated values */
428 start_angle=(double)istart_angle*PI/64./180.;
429 end_angle=(double)(istart_angle+idiff_angle)*PI/64./180.;
430 /* calculate the endpoints and round correctly */
431 points[0].x = (int) floor(dc->w.DCOrgX + (right+left)/2.0 +
432 cos(start_angle) * (right-left-width*2+2) / 2. + 0.5);
433 points[0].y = (int) floor(dc->w.DCOrgY + (top+bottom)/2.0 -
434 sin(start_angle) * (bottom-top-width*2+2) / 2. + 0.5);
435 points[1].x = (int) floor(dc->w.DCOrgX + (right+left)/2.0 +
436 cos(end_angle) * (right-left-width*2+2) / 2. + 0.5);
437 points[1].y = (int) floor(dc->w.DCOrgY + (top+bottom)/2.0 -
438 sin(end_angle) * (bottom-top-width*2+2) / 2. + 0.5);
440 /* OK this stuff is optimized for Xfree86
441 * which is probably the most used server by
442 * wine users. Other X servers will not
443 * display correctly. (eXceed for instance)
444 * so if you feel you must change make sure that
445 * you either use Xfree86 or seperate your changes
446 * from these (compile switch or whatever)
450 points[3] = points[1];
451 points[1].x = dc->w.DCOrgX + xcenter;
452 points[1].y = dc->w.DCOrgY + ycenter;
453 points[2] = points[1];
454 dx1=points[1].x-points[0].x;
455 dy1=points[1].y-points[0].y;
456 if(((top-bottom) | -2) == -2)
457 if(dy1>0) points[1].y--;
459 if (((-dx1)*64)<=ABS(dy1)*37) points[0].x--;
460 if(((-dx1*9))<(dy1*16)) points[0].y--;
461 if( dy1<0 && ((dx1*9)) < (dy1*16)) points[0].y--;
463 if(dy1 < 0) points[0].y--;
464 if(((right-left) | -2) == -2) points[1].x--;
466 dx1=points[3].x-points[2].x;
467 dy1=points[3].y-points[2].y;
468 if(((top-bottom) | -2 ) == -2)
469 if(dy1 < 0) points[2].y--;
471 if( dy1>0) points[3].y--;
472 if(((right-left) | -2) == -2 ) points[2].x--;
475 if( dx1 * 64 < dy1 * -37 ) points[3].x--;
479 TSXDrawLines( display, physDev->drawable, physDev->gc,
480 points, lines+1, CoordModeOrigin );
483 physDev->pen.width = oldwidth;
484 physDev->pen.endcap = oldendcap;
489 /***********************************************************************
493 X11DRV_Arc( DC *dc, INT left, INT top, INT right, INT bottom,
494 INT xstart, INT ystart, INT xend, INT yend )
496 return X11DRV_DrawArc( dc, left, top, right, bottom,
497 xstart, ystart, xend, yend, 0 );
501 /***********************************************************************
505 X11DRV_Pie( DC *dc, INT left, INT top, INT right, INT bottom,
506 INT xstart, INT ystart, INT xend, INT yend )
508 return X11DRV_DrawArc( dc, left, top, right, bottom,
509 xstart, ystart, xend, yend, 2 );
512 /***********************************************************************
516 X11DRV_Chord( DC *dc, INT left, INT top, INT right, INT bottom,
517 INT xstart, INT ystart, INT xend, INT yend )
519 return X11DRV_DrawArc( dc, left, top, right, bottom,
520 xstart, ystart, xend, yend, 1 );
524 /***********************************************************************
528 X11DRV_Ellipse( DC *dc, INT left, INT top, INT right, INT bottom )
531 X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
533 left = XLPTODP( dc, left );
534 top = YLPTODP( dc, top );
535 right = XLPTODP( dc, right );
536 bottom = YLPTODP( dc, bottom );
537 if ((left == right) || (top == bottom)) return TRUE;
539 if (right < left) { INT tmp = right; right = left; left = tmp; }
540 if (bottom < top) { INT tmp = bottom; bottom = top; top = tmp; }
542 oldwidth = width = physDev->pen.width;
543 if (!width) width = 1;
544 if(physDev->pen.style == PS_NULL) width = 0;
546 if ((physDev->pen.style == PS_INSIDEFRAME))
548 if (2*width > (right-left)) width=(right-left + 1)/2;
549 if (2*width > (bottom-top)) width=(bottom-top + 1)/2;
551 right -= (width - 1) / 2;
553 bottom -= (width - 1) / 2;
555 if(width == 0) width = 1; /* more accurate */
556 physDev->pen.width = width;
558 if (X11DRV_SetupGCForBrush( dc ))
559 TSXFillArc( 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 if (X11DRV_SetupGCForPen( dc ))
563 TSXDrawArc( display, physDev->drawable, physDev->gc,
564 dc->w.DCOrgX + left, dc->w.DCOrgY + top,
565 right-left-1, bottom-top-1, 0, 360*64 );
566 physDev->pen.width = oldwidth;
571 /***********************************************************************
575 X11DRV_Rectangle(DC *dc, INT left, INT top, INT right, INT bottom)
577 INT width, oldwidth, oldjoinstyle;
578 X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
580 TRACE(graphics, "(%d %d %d %d)\n",
581 left, top, right, bottom);
583 left = XLPTODP( dc, left );
584 top = YLPTODP( dc, top );
585 right = XLPTODP( dc, right );
586 bottom = YLPTODP( dc, bottom );
588 if ((left == right) || (top == bottom)) return TRUE;
590 if (right < left) { INT tmp = right; right = left; left = tmp; }
591 if (bottom < top) { INT tmp = bottom; bottom = top; top = tmp; }
593 oldwidth = width = physDev->pen.width;
594 if (!width) width = 1;
595 if(physDev->pen.style == PS_NULL) width = 0;
597 if ((physDev->pen.style == PS_INSIDEFRAME))
599 if (2*width > (right-left)) width=(right-left + 1)/2;
600 if (2*width > (bottom-top)) width=(bottom-top + 1)/2;
602 right -= (width - 1) / 2;
604 bottom -= (width - 1) / 2;
606 if(width == 1) width = 0;
607 physDev->pen.width = width;
608 oldjoinstyle = physDev->pen.linejoin;
609 if(physDev->pen.type != PS_GEOMETRIC)
610 physDev->pen.linejoin = PS_JOIN_MITER;
612 if ((right > left + width) && (bottom > top + width))
614 if (X11DRV_SetupGCForBrush( dc ))
615 TSXFillRectangle( display, physDev->drawable, physDev->gc,
616 dc->w.DCOrgX + left + (width + 1) / 2,
617 dc->w.DCOrgY + top + (width + 1) / 2,
618 right-left-width-1, bottom-top-width-1);
620 if (X11DRV_SetupGCForPen( dc ))
621 TSXDrawRectangle( display, physDev->drawable, physDev->gc,
622 dc->w.DCOrgX + left, dc->w.DCOrgY + top,
623 right-left-1, bottom-top-1 );
625 physDev->pen.width = oldwidth;
626 physDev->pen.linejoin = oldjoinstyle;
630 /***********************************************************************
634 X11DRV_RoundRect( DC *dc, INT left, INT top, INT right,
635 INT bottom, INT ell_width, INT ell_height )
637 INT width, oldwidth, oldendcap;
638 X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
640 TRACE(graphics, "(%d %d %d %d %d %d\n",
641 left, top, right, bottom, ell_width, ell_height);
643 left = XLPTODP( dc, left );
644 top = YLPTODP( dc, top );
645 right = XLPTODP( dc, right );
646 bottom = YLPTODP( dc, bottom );
648 if ((left == right) || (top == bottom))
651 /* Make sure ell_width and ell_height are >= 1 otherwise XDrawArc gets
652 called with width/height < 0 */
653 ell_width = MAX(abs( ell_width * dc->vportExtX / dc->wndExtX ), 1);
654 ell_height = MAX(abs( ell_height * dc->vportExtY / dc->wndExtY ), 1);
656 /* Fix the coordinates */
658 if (right < left) { INT tmp = right; right = left; left = tmp; }
659 if (bottom < top) { INT tmp = bottom; bottom = top; top = tmp; }
661 oldwidth = width = physDev->pen.width;
662 oldendcap = physDev->pen.endcap;
663 if (!width) width = 1;
664 if(physDev->pen.style == PS_NULL) width = 0;
666 if ((physDev->pen.style == PS_INSIDEFRAME))
668 if (2*width > (right-left)) width=(right-left + 1)/2;
669 if (2*width > (bottom-top)) width=(bottom-top + 1)/2;
671 right -= (width - 1) / 2;
673 bottom -= (width - 1) / 2;
675 if(width == 0) width = 1;
676 physDev->pen.width = width;
677 physDev->pen.endcap = PS_ENDCAP_SQUARE;
679 if (X11DRV_SetupGCForBrush( dc ))
681 if (ell_width > (right-left) )
682 if (ell_height > (bottom-top) )
683 TSXFillArc( display, physDev->drawable, physDev->gc,
684 dc->w.DCOrgX + left, dc->w.DCOrgY + top,
685 right - left - 1, bottom - top - 1,
688 TSXFillArc( display, physDev->drawable, physDev->gc,
689 dc->w.DCOrgX + left, dc->w.DCOrgY + top,
690 right - left - 1, ell_height, 0, 180 * 64 );
691 TSXFillArc( display, physDev->drawable, physDev->gc,
693 dc->w.DCOrgY + bottom - ell_height - 1,
694 right - left - 1, ell_height, 180 * 64,
697 else if (ell_height > (bottom-top) ){
698 TSXFillArc( display, physDev->drawable, physDev->gc,
699 dc->w.DCOrgX + left, dc->w.DCOrgY + top,
700 ell_width, bottom - top - 1, 90 * 64, 180 * 64 );
701 TSXFillArc( display, physDev->drawable, physDev->gc,
702 dc->w.DCOrgX + right - ell_width -1, dc->w.DCOrgY + top,
703 ell_width, bottom - top - 1, 270 * 64, 180 * 64 );
705 TSXFillArc( display, physDev->drawable, physDev->gc,
706 dc->w.DCOrgX + left, dc->w.DCOrgY + top,
707 ell_width, ell_height, 90 * 64, 90 * 64 );
708 TSXFillArc( display, physDev->drawable, physDev->gc,
710 dc->w.DCOrgY + bottom - ell_height - 1,
711 ell_width, ell_height, 180 * 64, 90 * 64 );
712 TSXFillArc( display, physDev->drawable, physDev->gc,
713 dc->w.DCOrgX + right - ell_width - 1,
714 dc->w.DCOrgY + bottom - ell_height - 1,
715 ell_width, ell_height, 270 * 64, 90 * 64 );
716 TSXFillArc( display, physDev->drawable, physDev->gc,
717 dc->w.DCOrgX + right - ell_width - 1,
719 ell_width, ell_height, 0, 90 * 64 );
721 if (ell_width < right - left)
723 TSXFillRectangle( display, physDev->drawable, physDev->gc,
724 dc->w.DCOrgX + left + (ell_width + 1) / 2,
725 dc->w.DCOrgY + top + 1,
726 right - left - ell_width - 1,
727 (ell_height + 1) / 2 - 1);
728 TSXFillRectangle( display, physDev->drawable, physDev->gc,
729 dc->w.DCOrgX + left + (ell_width + 1) / 2,
730 dc->w.DCOrgY + bottom - (ell_height) / 2 - 1,
731 right - left - ell_width - 1,
734 if (ell_height < bottom - top)
736 TSXFillRectangle( display, physDev->drawable, physDev->gc,
737 dc->w.DCOrgX + left + 1,
738 dc->w.DCOrgY + top + (ell_height + 1) / 2,
740 bottom - top - ell_height - 1);
743 /* FIXME: this could be done with on X call
744 * more efficient and probably more correct
745 * on any X server: XDrawArcs will draw
746 * straight horizontal and vertical lines
747 * if width or height are zero.
749 * BTW this stuff is optimized for an Xfree86 server
750 * read the comments inside the X11DRV_DrawArc function
752 if (X11DRV_SetupGCForPen(dc)) {
753 if (ell_width > (right-left) )
754 if (ell_height > (bottom-top) )
755 TSXDrawArc( display, physDev->drawable, physDev->gc,
756 dc->w.DCOrgX + left, dc->w.DCOrgY + top,
757 right - left - 1, bottom -top - 1, 0 , 360 * 64 );
759 TSXDrawArc( display, physDev->drawable, physDev->gc,
760 dc->w.DCOrgX + left, dc->w.DCOrgY + top,
761 right - left - 1, ell_height - 1, 0 , 180 * 64 );
762 TSXDrawArc( display, physDev->drawable, physDev->gc,
764 dc->w.DCOrgY + bottom - ell_height,
765 right - left - 1, ell_height - 1, 180 * 64 , 180 * 64 );
767 else if (ell_height > (bottom-top) ){
768 TSXDrawArc( display, physDev->drawable, physDev->gc,
769 dc->w.DCOrgX + left, dc->w.DCOrgY + top,
770 ell_width - 1 , bottom - top - 1, 90 * 64 , 180 * 64 );
771 TSXDrawArc( display, physDev->drawable, physDev->gc,
772 dc->w.DCOrgX + right - ell_width,
774 ell_width - 1 , bottom - top - 1, 270 * 64 , 180 * 64 );
776 TSXDrawArc( display, physDev->drawable, physDev->gc,
777 dc->w.DCOrgX + left, dc->w.DCOrgY + top,
778 ell_width - 1, ell_height - 1, 90 * 64, 90 * 64 );
779 TSXDrawArc( display, physDev->drawable, physDev->gc,
780 dc->w.DCOrgX + left, dc->w.DCOrgY + bottom - ell_height,
781 ell_width - 1, ell_height - 1, 180 * 64, 90 * 64 );
782 TSXDrawArc( display, physDev->drawable, physDev->gc,
783 dc->w.DCOrgX + right - ell_width,
784 dc->w.DCOrgY + bottom - ell_height,
785 ell_width - 1, ell_height - 1, 270 * 64, 90 * 64 );
786 TSXDrawArc( display, physDev->drawable, physDev->gc,
787 dc->w.DCOrgX + right - ell_width, dc->w.DCOrgY + top,
788 ell_width - 1, ell_height - 1, 0, 90 * 64 );
790 if (ell_width < right - left)
792 TSXDrawLine( display, physDev->drawable, physDev->gc,
793 dc->w.DCOrgX + left + ell_width / 2,
795 dc->w.DCOrgX + right - (ell_width+1) / 2,
797 TSXDrawLine( display, physDev->drawable, physDev->gc,
798 dc->w.DCOrgX + left + ell_width / 2 ,
799 dc->w.DCOrgY + bottom - 1,
800 dc->w.DCOrgX + right - (ell_width+1)/ 2,
801 dc->w.DCOrgY + bottom - 1);
803 if (ell_height < bottom - top)
805 TSXDrawLine( display, physDev->drawable, physDev->gc,
806 dc->w.DCOrgX + right - 1,
807 dc->w.DCOrgY + top + ell_height / 2,
808 dc->w.DCOrgX + right - 1,
809 dc->w.DCOrgY + bottom - (ell_height+1) / 2);
810 TSXDrawLine( display, physDev->drawable, physDev->gc,
812 dc->w.DCOrgY + top + ell_height / 2,
814 dc->w.DCOrgY + bottom - (ell_height+1) / 2);
817 physDev->pen.width = oldwidth;
818 physDev->pen.endcap = oldendcap;
823 /***********************************************************************
827 X11DRV_SetPixel( DC *dc, INT x, INT y, COLORREF color )
830 X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
832 x = dc->w.DCOrgX + XLPTODP( dc, x );
833 y = dc->w.DCOrgY + YLPTODP( dc, y );
834 pixel = X11DRV_PALETTE_ToPhysical( dc, color );
836 TSXSetForeground( display, physDev->gc, pixel );
837 TSXSetFunction( display, physDev->gc, GXcopy );
838 TSXDrawPoint( display, physDev->drawable, physDev->gc, x, y );
840 /* inefficient but simple... */
842 return X11DRV_PALETTE_ToLogical(pixel);
846 /***********************************************************************
850 X11DRV_GetPixel( DC *dc, INT x, INT y )
852 static Pixmap pixmap = 0;
855 X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
857 x = dc->w.DCOrgX + XLPTODP( dc, x );
858 y = dc->w.DCOrgY + YLPTODP( dc, y );
859 EnterCriticalSection( &X11DRV_CritSection );
860 if (dc->w.flags & DC_MEMORY)
862 image = XGetImage( display, physDev->drawable, x, y, 1, 1,
863 AllPlanes, ZPixmap );
867 /* If we are reading from the screen, use a temporary copy */
868 /* to avoid a BadMatch error */
869 if (!pixmap) pixmap = XCreatePixmap( display, X11DRV_GetXRootWindow(),
870 1, 1, dc->w.bitsPerPixel );
871 XCopyArea( display, physDev->drawable, pixmap, BITMAP_colorGC,
873 image = XGetImage( display, pixmap, 0, 0, 1, 1, AllPlanes, ZPixmap );
875 pixel = XGetPixel( image, 0, 0 );
876 XDestroyImage( image );
877 LeaveCriticalSection( &X11DRV_CritSection );
879 return X11DRV_PALETTE_ToLogical(pixel);
883 /***********************************************************************
887 X11DRV_PaintRgn( DC *dc, HRGN hrgn )
890 HRGN tmpVisRgn, prevVisRgn;
891 HDC hdc = dc->hSelf; /* FIXME: should not mix dc/hdc this way */
892 X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
894 if (!(tmpVisRgn = CreateRectRgn( 0, 0, 0, 0 ))) return FALSE;
896 /* Transform region into device co-ords */
897 if ( !REGION_LPTODP( hdc, tmpVisRgn, hrgn )
898 || OffsetRgn( tmpVisRgn, dc->w.DCOrgX, dc->w.DCOrgY ) == ERROR) {
899 DeleteObject( tmpVisRgn );
903 /* Modify visible region */
904 if (!(prevVisRgn = SaveVisRgn16( hdc ))) {
905 DeleteObject( tmpVisRgn );
908 CombineRgn( tmpVisRgn, prevVisRgn, tmpVisRgn, RGN_AND );
909 SelectVisRgn16( hdc, tmpVisRgn );
910 DeleteObject( tmpVisRgn );
912 /* Fill the region */
914 GetRgnBox( dc->w.hGCClipRgn, &box );
915 if (X11DRV_SetupGCForBrush( dc ))
916 TSXFillRectangle( display, physDev->drawable, physDev->gc,
918 box.right-box.left, box.bottom-box.top );
920 /* Restore the visible region */
922 RestoreVisRgn16( hdc );
926 /**********************************************************************
930 X11DRV_Polyline( DC *dc, const POINT* pt, INT count )
935 X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
937 if((oldwidth = physDev->pen.width) == 0) physDev->pen.width = 1;
939 points = (XPoint *) xmalloc (sizeof (XPoint) * (count));
940 for (i = 0; i < count; i++)
942 points[i].x = dc->w.DCOrgX + XLPTODP( dc, pt[i].x );
943 points[i].y = dc->w.DCOrgY + YLPTODP( dc, pt[i].y );
946 if (X11DRV_SetupGCForPen ( dc ))
947 TSXDrawLines( display, physDev->drawable, physDev->gc,
948 points, count, CoordModeOrigin );
951 physDev->pen.width = oldwidth;
956 /**********************************************************************
960 X11DRV_Polygon( DC *dc, const POINT* pt, INT count )
964 X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
966 points = (XPoint *) xmalloc (sizeof (XPoint) * (count+1));
967 for (i = 0; i < count; i++)
969 points[i].x = dc->w.DCOrgX + XLPTODP( dc, pt[i].x );
970 points[i].y = dc->w.DCOrgY + YLPTODP( dc, pt[i].y );
972 points[count] = points[0];
974 if (X11DRV_SetupGCForBrush( dc ))
975 TSXFillPolygon( display, physDev->drawable, physDev->gc,
976 points, count+1, Complex, CoordModeOrigin);
978 if (X11DRV_SetupGCForPen ( dc ))
979 TSXDrawLines( display, physDev->drawable, physDev->gc,
980 points, count+1, CoordModeOrigin );
987 /**********************************************************************
991 X11DRV_PolyPolygon( DC *dc, const POINT* pt, const INT* counts, UINT polygons)
994 X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
996 /* FIXME: The points should be converted to device coords before */
997 /* creating the region. */
999 hrgn = CreatePolyPolygonRgn( pt, counts, polygons, dc->w.polyFillMode );
1000 X11DRV_PaintRgn( dc, hrgn );
1001 DeleteObject( hrgn );
1003 /* Draw the outline of the polygons */
1005 if (X11DRV_SetupGCForPen ( dc ))
1010 for (i = 0; i < polygons; i++) if (counts[i] > max) max = counts[i];
1011 points = (XPoint *) xmalloc( sizeof(XPoint) * (max+1) );
1013 for (i = 0; i < polygons; i++)
1015 for (j = 0; j < counts[i]; j++)
1017 points[j].x = dc->w.DCOrgX + XLPTODP( dc, pt->x );
1018 points[j].y = dc->w.DCOrgY + YLPTODP( dc, pt->y );
1021 points[j] = points[0];
1022 TSXDrawLines( display, physDev->drawable, physDev->gc,
1023 points, j + 1, CoordModeOrigin );
1031 /**********************************************************************
1032 * X11DRV_PolyPolyline
1035 X11DRV_PolyPolyline( DC *dc, const POINT* pt, const DWORD* counts, DWORD polylines )
1037 X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
1038 if (X11DRV_SetupGCForPen ( dc ))
1043 for (i = 0; i < polylines; i++) if (counts[i] > max) max = counts[i];
1044 points = (XPoint *) xmalloc( sizeof(XPoint) * (max+1) );
1046 for (i = 0; i < polylines; i++)
1048 for (j = 0; j < counts[i]; j++)
1050 points[j].x = dc->w.DCOrgX + XLPTODP( dc, pt->x );
1051 points[j].y = dc->w.DCOrgY + YLPTODP( dc, pt->y );
1054 points[j] = points[0];
1055 TSXDrawLines( display, physDev->drawable, physDev->gc,
1056 points, j + 1, CoordModeOrigin );
1064 /**********************************************************************
1065 * X11DRV_InternalFloodFill
1067 * Internal helper function for flood fill.
1068 * (xorg,yorg) is the origin of the X image relative to the drawable.
1069 * (x,y) is relative to the origin of the X image.
1071 static void X11DRV_InternalFloodFill(XImage *image, DC *dc,
1074 Pixel pixel, WORD fillType )
1076 X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
1079 #define TO_FLOOD(x,y) ((fillType == FLOODFILLBORDER) ? \
1080 (XGetPixel(image,x,y) != pixel) : \
1081 (XGetPixel(image,x,y) == pixel))
1083 if (!TO_FLOOD(x,y)) return;
1085 /* Find left and right boundaries */
1088 while ((left > 0) && TO_FLOOD( left-1, y )) left--;
1089 while ((right < image->width) && TO_FLOOD( right, y )) right++;
1090 XFillRectangle( display, physDev->drawable, physDev->gc,
1091 xOrg + left, yOrg + y, right-left, 1 );
1093 /* Set the pixels of this line so we don't fill it again */
1095 for (x = left; x < right; x++)
1097 if (fillType == FLOODFILLBORDER) XPutPixel( image, x, y, pixel );
1098 else XPutPixel( image, x, y, ~pixel );
1101 /* Fill the line above */
1108 while ((x < right) && !TO_FLOOD(x,y)) x++;
1109 if (x >= right) break;
1110 while ((x < right) && TO_FLOOD(x,y)) x++;
1111 X11DRV_InternalFloodFill(image, dc, x-1, y,
1112 xOrg, yOrg, pixel, fillType );
1116 /* Fill the line below */
1118 if ((y += 2) < image->height)
1123 while ((x < right) && !TO_FLOOD(x,y)) x++;
1124 if (x >= right) break;
1125 while ((x < right) && TO_FLOOD(x,y)) x++;
1126 X11DRV_InternalFloodFill(image, dc, x-1, y,
1127 xOrg, yOrg, pixel, fillType );
1134 /**********************************************************************
1135 * X11DRV_DoFloodFill
1137 * Main flood-fill routine.
1139 * The Xlib critical section must be entered before calling this function.
1142 struct FloodFill_params
1151 static BOOL X11DRV_DoFloodFill( const struct FloodFill_params *params )
1155 DC *dc = params->dc;
1156 X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
1158 if (GetRgnBox( dc->w.hGCClipRgn, &rect ) == ERROR) return FALSE;
1160 if (!(image = XGetImage( display, physDev->drawable,
1163 rect.right - rect.left,
1164 rect.bottom - rect.top,
1165 AllPlanes, ZPixmap ))) return FALSE;
1167 if (X11DRV_SetupGCForBrush( dc ))
1169 /* ROP mode is always GXcopy for flood-fill */
1170 XSetFunction( display, physDev->gc, GXcopy );
1171 X11DRV_InternalFloodFill(image, dc,
1172 XLPTODP(dc,params->x) + dc->w.DCOrgX - rect.left,
1173 YLPTODP(dc,params->y) + dc->w.DCOrgY - rect.top,
1176 X11DRV_PALETTE_ToPhysical( dc, params->color ),
1180 XDestroyImage( image );
1185 /**********************************************************************
1186 * X11DRV_ExtFloodFill
1189 X11DRV_ExtFloodFill( DC *dc, INT x, INT y, COLORREF color,
1193 struct FloodFill_params params = { dc, x, y, color, fillType };
1195 TRACE(graphics, "X11DRV_ExtFloodFill %d,%d %06lx %d\n",
1196 x, y, color, fillType );
1198 if (!PtVisible( dc->hSelf, x, y )) return FALSE;
1199 EnterCriticalSection( &X11DRV_CritSection );
1200 result = CALL_LARGE_STACK( X11DRV_DoFloodFill, ¶ms );
1201 LeaveCriticalSection( &X11DRV_CritSection );
1205 /******************************************************************
1207 * *Very* simple bezier drawing code,
1209 * It uses a recursive algorithm to divide the curve in a series
1210 * of straight line segements. Not ideal but for me sufficient.
1211 * If you are in need for something better look for some incremental
1214 * 7 July 1998 Rein Klazes
1218 * some macro definitions for bezier drawing
1220 * to avoid trucation errors the coordinates are
1221 * shifted upwards. When used in drawing they are
1222 * shifted down again, including correct rounding
1223 * and avoiding floating point arithmatic
1224 * 4 bits should allow 27 bits coordinates which I saw
1225 * somewere in the win32 doc's
1229 #define BEZIERSHIFTBITS 4
1230 #define BEZIERSHIFTUP(x) ((x)<<BEZIERSHIFTBITS)
1231 #define BEZIERPIXEL BEZIERSHIFTUP(1)
1232 #define BEZIERSHIFTDOWN(x) (((x)+(1<<(BEZIERSHIFTBITS-1)))>>BEZIERSHIFTBITS)
1233 /* maximum depth of recursion */
1234 #define BEZIERMAXDEPTH 8
1236 /* size of array to store points on */
1237 /* enough for one curve */
1238 #define BEZMAXPOINTS (150)
1240 /* calculate Bezier average, in this case the middle
1241 * correctly rounded...
1244 #define BEZIERMIDDLE(Mid, P1, P2) \
1245 (Mid).x=((P1).x+(P2).x + 1)/2;\
1246 (Mid).y=((P1).y+(P2).y + 1)/2;
1248 /**********************************************************
1249 * BezierCheck helper function to check
1250 * that recursion can be terminated
1251 * Points[0] and Points[3] are begin and endpoint
1252 * Points[1] and Points[2] are control points
1253 * level is the recursion depth
1254 * returns true if the recusion can be terminated
1256 static BOOL BezierCheck( int level, POINT *Points)
1259 dx=Points[3].x-Points[0].x;
1260 dy=Points[3].y-Points[0].y;
1261 if(ABS(dy)<=ABS(dx)){/* shallow line */
1262 /* check that control points are between begin and end */
1263 if(Points[1].x < Points[0].x){
1264 if(Points[1].x < Points[3].x)
1267 if(Points[1].x > Points[3].x)
1269 if(Points[2].x < Points[0].x){
1270 if(Points[2].x < Points[3].x)
1273 if(Points[2].x > Points[3].x)
1275 dx=BEZIERSHIFTDOWN(dx);
1276 if(!dx) return TRUE;
1277 if(abs(Points[1].y-Points[0].y-(dy/dx)*
1278 BEZIERSHIFTDOWN(Points[1].x-Points[0].x)) > BEZIERPIXEL ||
1279 abs(Points[2].y-Points[0].y-(dy/dx)*
1280 BEZIERSHIFTDOWN(Points[2].x-Points[0].x)) > BEZIERPIXEL )
1284 }else{ /* steep line */
1285 /* check that control points are between begin and end */
1286 if(Points[1].y < Points[0].y){
1287 if(Points[1].y < Points[3].y)
1290 if(Points[1].y > Points[3].y)
1292 if(Points[2].y < Points[0].y){
1293 if(Points[2].y < Points[3].y)
1296 if(Points[2].y > Points[3].y)
1298 dy=BEZIERSHIFTDOWN(dy);
1299 if(!dy) return TRUE;
1300 if(abs(Points[1].x-Points[0].x-(dx/dy)*
1301 BEZIERSHIFTDOWN(Points[1].y-Points[0].y)) > BEZIERPIXEL ||
1302 abs(Points[2].x-Points[0].x-(dx/dy)*
1303 BEZIERSHIFTDOWN(Points[2].y-Points[0].y)) > BEZIERPIXEL )
1310 /***********************************************************************
1312 * Draw a -what microsoft calls- bezier curve
1313 * The routine recursively devides the curve
1314 * in two parts until a straight line can be drawn
1316 * level recusion depth counted backwards
1318 * Points array of begin(0), end(3) and control points(1 and 2)
1319 * XPoints array with points calculated sofar
1320 * *pIx nr points calculated sofar
1323 static void X11DRV_Bezier(int level, DC * dc, POINT *Points,
1324 XPoint* xpoints, unsigned int* pIx)
1326 X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
1328 if(*pIx == BEZMAXPOINTS){
1329 TSXDrawLines( display, physDev->drawable, physDev->gc,
1330 xpoints, *pIx, CoordModeOrigin );
1333 if(!level || BezierCheck(level, Points)) {
1335 xpoints[*pIx].x= dc->w.DCOrgX + BEZIERSHIFTDOWN(Points[0].x);
1336 xpoints[*pIx].y= dc->w.DCOrgY + BEZIERSHIFTDOWN(Points[0].y);
1339 xpoints[*pIx].x= dc->w.DCOrgX + BEZIERSHIFTDOWN(Points[3].x);
1340 xpoints[*pIx].y= dc->w.DCOrgY + BEZIERSHIFTDOWN(Points[3].y);
1343 POINT Points2[4]; /* for the second recursive call */
1344 Points2[3]=Points[3];
1345 BEZIERMIDDLE(Points2[2], Points[2], Points[3]);
1346 BEZIERMIDDLE(Points2[0], Points[1], Points[2]);
1347 BEZIERMIDDLE(Points2[1],Points2[0],Points2[2]);
1349 BEZIERMIDDLE(Points[1], Points[0], Points[1]);
1350 BEZIERMIDDLE(Points[2], Points[1], Points2[0]);
1351 BEZIERMIDDLE(Points[3], Points[2], Points2[1]);
1353 Points2[0]=Points[3];
1355 /* do the two halves */
1356 X11DRV_Bezier(level-1, dc, Points, xpoints, pIx);
1357 X11DRV_Bezier(level-1, dc, Points2, xpoints, pIx);
1361 /***********************************************************************
1363 * Implement functionality for PolyBezier and PolyBezierTo
1365 * [i] dc pointer to device context
1366 * [i] start, first point in curve
1367 * [i] BezierPoints , array of point filled with rest of the points
1368 * [i] count, number of points in BezierPoints, must be a
1372 X11DRV_PolyBezier(DC *dc, POINT start, const POINT* BezierPoints, DWORD count)
1378 X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
1380 TRACE(graphics, "dc=%p count=%ld %ld,%ld - %ld,%ld - %ld,%ld - %ld,%ld\n",
1383 (Points+0)->x, (Points+0)->y,
1384 (Points+1)->x, (Points+1)->y,
1385 (Points+2)->x, (Points+2)->y);
1386 if(!count || count % 3){/* paranoid */
1387 WARN(graphics," bad value for count : %ld\n", count);
1390 xpoints=(XPoint*) xmalloc( sizeof(XPoint)*BEZMAXPOINTS);
1391 Points[3].x=BEZIERSHIFTUP(XLPTODP(dc,start.x));
1392 Points[3].y=BEZIERSHIFTUP(YLPTODP(dc,start.y));
1394 Points[0]=Points[3];
1396 Points[i].x= BEZIERSHIFTUP(XLPTODP(dc,BezierPoints->x));
1397 Points[i].y= BEZIERSHIFTUP(YLPTODP(dc,BezierPoints->y));
1400 X11DRV_Bezier(BEZIERMAXDEPTH , dc, Points, xpoints, &ix );
1403 if( ix) TSXDrawLines( display, physDev->drawable, physDev->gc,
1404 xpoints, ix, CoordModeOrigin );
1409 /**********************************************************************
1413 X11DRV_SetBkColor( DC *dc, COLORREF color )
1415 X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
1418 oldColor = dc->w.backgroundColor;
1419 dc->w.backgroundColor = color;
1421 physDev->backgroundPixel = X11DRV_PALETTE_ToPhysical( dc, color );
1426 /**********************************************************************
1427 * X11DRV_SetTextColor
1430 X11DRV_SetTextColor( DC *dc, COLORREF color )
1432 X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
1435 oldColor = dc->w.textColor;
1436 dc->w.textColor = color;
1438 physDev->textPixel = X11DRV_PALETTE_ToPhysical( dc, color );
1443 #endif /* !defined(X_DISPLAY_MISSING) */