2 * X11 graphics driver graphics functions
4 * Copyright 1993,1994 Alexandre Julliard
14 #include <X11/Intrinsic.h>
36 /**********************************************************************
40 X11DRV_MoveToEx(DC *dc,INT32 x,INT32 y,LPPOINT32 pt) {
43 pt->x = dc->w.CursPosX;
44 pt->y = dc->w.CursPosY;
51 /***********************************************************************
55 X11DRV_LineTo( DC *dc, INT32 x, INT32 y )
57 if (DC_SetupGCForPen( dc ))
58 TSXDrawLine(display, dc->u.x.drawable, dc->u.x.gc,
59 dc->w.DCOrgX + XLPTODP( dc, dc->w.CursPosX ),
60 dc->w.DCOrgY + YLPTODP( dc, dc->w.CursPosY ),
61 dc->w.DCOrgX + XLPTODP( dc, x ),
62 dc->w.DCOrgY + YLPTODP( dc, y ) );
70 /***********************************************************************
73 * Helper functions for Arc(), Chord() and Pie().
74 * 'lines' is the number of lines to draw: 0 for Arc, 1 for Chord, 2 for Pie.
77 X11DRV_DrawArc( DC *dc, INT32 left, INT32 top, INT32 right,
78 INT32 bottom, INT32 xstart, INT32 ystart,
79 INT32 xend, INT32 yend, INT32 lines )
81 INT32 xcenter, ycenter, istart_angle, idiff_angle, tmp;
82 double start_angle, end_angle;
85 left = XLPTODP( dc, left );
86 top = YLPTODP( dc, top );
87 right = XLPTODP( dc, right );
88 bottom = YLPTODP( dc, bottom );
89 xstart = XLPTODP( dc, xstart );
90 ystart = YLPTODP( dc, ystart );
91 xend = XLPTODP( dc, xend );
92 yend = YLPTODP( dc, yend );
94 if ((left == right) || (top == bottom)) return FALSE;
96 if (left > right) { tmp=left; left=right; right=tmp; }
97 if (top > bottom) { tmp=top; top=bottom; bottom=tmp; }
98 xcenter = (right + left) / 2;
99 ycenter = (bottom + top) / 2;
100 start_angle = atan2( (double)(ycenter-ystart)*(right-left),
101 (double)(xstart-xcenter)*(bottom-top) );
102 end_angle = atan2( (double)(ycenter-yend)*(right-left),
103 (double)(xend-xcenter)*(bottom-top) );
104 if ((xstart==xend)&&(ystart==yend))
105 { /* A lazy program delivers xstart=xend=ystart=yend=0) */
109 else /* notorious cases */
110 if ((start_angle == PI)&&( end_angle <0))
113 if ((end_angle == PI)&&( start_angle <0))
115 istart_angle = (INT32)(start_angle * 180 * 64 / PI);
116 idiff_angle = (INT32)((end_angle - start_angle) * 180 * 64 / PI );
117 if (idiff_angle <= 0) idiff_angle += 360 * 64;
119 /* Fill arc with brush if Chord() or Pie() */
121 if ((lines > 0) && DC_SetupGCForBrush( dc ))
123 TSXSetArcMode( display, dc->u.x.gc, (lines==1) ? ArcChord : ArcPieSlice);
124 TSXFillArc( display, dc->u.x.drawable, dc->u.x.gc,
125 dc->w.DCOrgX + left, dc->w.DCOrgY + top,
126 right-left-1, bottom-top-1, istart_angle, idiff_angle );
129 /* Draw arc and lines */
131 if (!DC_SetupGCForPen( dc )) return TRUE;
132 TSXDrawArc( display, dc->u.x.drawable, dc->u.x.gc,
133 dc->w.DCOrgX + left, dc->w.DCOrgY + top,
134 right-left-1, bottom-top-1, istart_angle, idiff_angle );
135 if (!lines) return TRUE;
137 points[0].x = dc->w.DCOrgX + xcenter + (int)(cos(start_angle) * (right-left) / 2);
138 points[0].y = dc->w.DCOrgY + ycenter - (int)(sin(start_angle) * (bottom-top) / 2);
139 points[1].x = dc->w.DCOrgX + xcenter + (int)(cos(end_angle) * (right-left) / 2);
140 points[1].y = dc->w.DCOrgY + ycenter - (int)(sin(end_angle) * (bottom-top) / 2);
143 points[2] = points[1];
144 points[1].x = dc->w.DCOrgX + xcenter;
145 points[1].y = dc->w.DCOrgY + ycenter;
147 TSXDrawLines( display, dc->u.x.drawable, dc->u.x.gc,
148 points, lines+1, CoordModeOrigin );
153 /***********************************************************************
157 X11DRV_Arc( DC *dc, INT32 left, INT32 top, INT32 right, INT32 bottom,
158 INT32 xstart, INT32 ystart, INT32 xend, INT32 yend )
160 return X11DRV_DrawArc( dc, left, top, right, bottom,
161 xstart, ystart, xend, yend, 0 );
165 /***********************************************************************
169 X11DRV_Pie( DC *dc, INT32 left, INT32 top, INT32 right, INT32 bottom,
170 INT32 xstart, INT32 ystart, INT32 xend, INT32 yend )
172 return X11DRV_DrawArc( dc, left, top, right, bottom,
173 xstart, ystart, xend, yend, 2 );
176 /***********************************************************************
180 X11DRV_Chord( DC *dc, INT32 left, INT32 top, INT32 right, INT32 bottom,
181 INT32 xstart, INT32 ystart, INT32 xend, INT32 yend )
183 return X11DRV_DrawArc( dc, left, top, right, bottom,
184 xstart, ystart, xend, yend, 1 );
188 /***********************************************************************
192 X11DRV_Ellipse( DC *dc, INT32 left, INT32 top, INT32 right, INT32 bottom )
194 left = XLPTODP( dc, left );
195 top = YLPTODP( dc, top );
196 right = XLPTODP( dc, right );
197 bottom = YLPTODP( dc, bottom );
198 if ((left == right) || (top == bottom)) return FALSE;
200 if (right < left) { INT32 tmp = right; right = left; left = tmp; }
201 if (bottom < top) { INT32 tmp = bottom; bottom = top; top = tmp; }
203 if ((dc->u.x.pen.style == PS_INSIDEFRAME) &&
204 (dc->u.x.pen.width < right-left-1) &&
205 (dc->u.x.pen.width < bottom-top-1))
207 left += dc->u.x.pen.width / 2;
208 right -= (dc->u.x.pen.width + 1) / 2;
209 top += dc->u.x.pen.width / 2;
210 bottom -= (dc->u.x.pen.width + 1) / 2;
213 if (DC_SetupGCForBrush( dc ))
214 TSXFillArc( display, dc->u.x.drawable, dc->u.x.gc,
215 dc->w.DCOrgX + left, dc->w.DCOrgY + top,
216 right-left-1, bottom-top-1, 0, 360*64 );
217 if (DC_SetupGCForPen( dc ))
218 TSXDrawArc( display, dc->u.x.drawable, dc->u.x.gc,
219 dc->w.DCOrgX + left, dc->w.DCOrgY + top,
220 right-left-1, bottom-top-1, 0, 360*64 );
225 /***********************************************************************
229 X11DRV_Rectangle(DC *dc, INT32 left, INT32 top, INT32 right, INT32 bottom)
232 left = XLPTODP( dc, left );
233 top = YLPTODP( dc, top );
234 right = XLPTODP( dc, right );
235 bottom = YLPTODP( dc, bottom );
237 if (right < left) { INT32 tmp = right; right = left; left = tmp; }
238 if (bottom < top) { INT32 tmp = bottom; bottom = top; top = tmp; }
240 if ((left == right) || (top == bottom))
243 if (DC_SetupGCForPen( dc ))
244 TSXDrawLine(display, dc->u.x.drawable, dc->u.x.gc,
247 dc->w.DCOrgX + right,
248 dc->w.DCOrgY + bottom);
252 width = dc->u.x.pen.width;
253 if (!width) width = 1;
254 if(dc->u.x.pen.style == PS_NULL) width = 0;
256 if ((dc->u.x.pen.style == PS_INSIDEFRAME) &&
257 (width < right-left) && (width < bottom-top))
260 right -= (width + 1) / 2;
262 bottom -= (width + 1) / 2;
265 if ((right > left + width) && (bottom > top + width))
267 if (DC_SetupGCForBrush( dc ))
268 TSXFillRectangle( display, dc->u.x.drawable, dc->u.x.gc,
269 dc->w.DCOrgX + left + (width + 1) / 2,
270 dc->w.DCOrgY + top + (width + 1) / 2,
271 right-left-width-1, bottom-top-width-1);
273 if (DC_SetupGCForPen( dc ))
274 TSXDrawRectangle( display, dc->u.x.drawable, dc->u.x.gc,
275 dc->w.DCOrgX + left, dc->w.DCOrgY + top,
276 right-left-1, bottom-top-1 );
280 /***********************************************************************
284 X11DRV_RoundRect( DC *dc, INT32 left, INT32 top, INT32 right,
285 INT32 bottom, INT32 ell_width, INT32 ell_height )
287 TRACE(graphics, "(%d %d %d %d %d %d\n",
288 left, top, right, bottom, ell_width, ell_height);
290 left = XLPTODP( dc, left );
291 top = YLPTODP( dc, top );
292 right = XLPTODP( dc, right );
293 bottom = YLPTODP( dc, bottom );
294 ell_width = abs( ell_width * dc->vportExtX / dc->wndExtX );
295 ell_height = abs( ell_height * dc->vportExtY / dc->wndExtY );
297 /* Fix the coordinates */
299 if (right < left) { INT32 tmp = right; right = left; left = tmp; }
300 if (bottom < top) { INT32 tmp = bottom; bottom = top; top = tmp; }
301 if (ell_width > right - left) ell_width = right - left;
302 if (ell_height > bottom - top) ell_height = bottom - top;
304 if (DC_SetupGCForBrush( dc ))
306 if (ell_width && ell_height)
308 TSXFillArc( display, dc->u.x.drawable, dc->u.x.gc,
309 dc->w.DCOrgX + left, dc->w.DCOrgY + top,
310 ell_width, ell_height, 90 * 64, 90 * 64 );
311 TSXFillArc( display, dc->u.x.drawable, dc->u.x.gc,
312 dc->w.DCOrgX + left, dc->w.DCOrgY + bottom - ell_height,
313 ell_width, ell_height, 180 * 64, 90 * 64 );
314 TSXFillArc( display, dc->u.x.drawable, dc->u.x.gc,
315 dc->w.DCOrgX + right - ell_width,
316 dc->w.DCOrgY + bottom - ell_height,
317 ell_width, ell_height, 270 * 64, 90 * 64 );
318 TSXFillArc( display, dc->u.x.drawable, dc->u.x.gc,
319 dc->w.DCOrgX + right - ell_width, dc->w.DCOrgY + top,
320 ell_width, ell_height, 0, 90 * 64 );
322 if (ell_width < right - left)
324 TSXFillRectangle( display, dc->u.x.drawable, dc->u.x.gc,
325 dc->w.DCOrgX + left + ell_width / 2,
327 right - left - ell_width, ell_height / 2 );
328 TSXFillRectangle( display, dc->u.x.drawable, dc->u.x.gc,
329 dc->w.DCOrgX + left + ell_width / 2,
330 dc->w.DCOrgY + bottom - (ell_height+1) / 2,
331 right - left - ell_width, (ell_height+1) / 2 );
333 if (ell_height < bottom - top)
335 TSXFillRectangle( display, dc->u.x.drawable, dc->u.x.gc,
337 dc->w.DCOrgY + top + ell_height / 2,
338 right - left, bottom - top - ell_height );
341 if (DC_SetupGCForPen(dc))
343 if (ell_width && ell_height)
345 TSXDrawArc( display, dc->u.x.drawable, dc->u.x.gc,
346 dc->w.DCOrgX + left, dc->w.DCOrgY + top,
347 ell_width, ell_height, 90 * 64, 90 * 64 );
348 TSXDrawArc( display, dc->u.x.drawable, dc->u.x.gc,
349 dc->w.DCOrgX + left, dc->w.DCOrgY + bottom - ell_height,
350 ell_width, ell_height, 180 * 64, 90 * 64 );
351 TSXDrawArc( display, dc->u.x.drawable, dc->u.x.gc,
352 dc->w.DCOrgX + right - ell_width,
353 dc->w.DCOrgY + bottom - ell_height,
354 ell_width, ell_height, 270 * 64, 90 * 64 );
355 TSXDrawArc( display, dc->u.x.drawable, dc->u.x.gc,
356 dc->w.DCOrgX + right - ell_width, dc->w.DCOrgY + top,
357 ell_width, ell_height, 0, 90 * 64 );
359 if (ell_width < right - left)
361 TSXDrawLine( display, dc->u.x.drawable, dc->u.x.gc,
362 dc->w.DCOrgX + left + ell_width / 2,
364 dc->w.DCOrgX + right - ell_width / 2,
365 dc->w.DCOrgY + top );
366 TSXDrawLine( display, dc->u.x.drawable, dc->u.x.gc,
367 dc->w.DCOrgX + left + ell_width / 2,
368 dc->w.DCOrgY + bottom,
369 dc->w.DCOrgX + right - ell_width / 2,
370 dc->w.DCOrgY + bottom );
372 if (ell_height < bottom - top)
374 TSXDrawLine( display, dc->u.x.drawable, dc->u.x.gc,
375 dc->w.DCOrgX + right,
376 dc->w.DCOrgY + top + ell_height / 2,
377 dc->w.DCOrgX + right,
378 dc->w.DCOrgY + bottom - ell_height / 2 );
379 TSXDrawLine( display, dc->u.x.drawable, dc->u.x.gc,
381 dc->w.DCOrgY + top + ell_height / 2,
383 dc->w.DCOrgY + bottom - ell_height / 2 );
390 /***********************************************************************
394 X11DRV_SetPixel( DC *dc, INT32 x, INT32 y, COLORREF color )
398 x = dc->w.DCOrgX + XLPTODP( dc, x );
399 y = dc->w.DCOrgY + YLPTODP( dc, y );
400 pixel = COLOR_ToPhysical( dc, color );
402 TSXSetForeground( display, dc->u.x.gc, pixel );
403 TSXSetFunction( display, dc->u.x.gc, GXcopy );
404 TSXDrawPoint( display, dc->u.x.drawable, dc->u.x.gc, x, y );
406 /* inefficient but simple... */
408 return COLOR_ToLogical(pixel);
412 /***********************************************************************
416 X11DRV_GetPixel( DC *dc, INT32 x, INT32 y )
418 static Pixmap pixmap = 0;
422 x = dc->w.DCOrgX + XLPTODP( dc, x );
423 y = dc->w.DCOrgY + YLPTODP( dc, y );
424 EnterCriticalSection( &X11DRV_CritSection );
425 if (dc->w.flags & DC_MEMORY)
427 image = XGetImage( display, dc->u.x.drawable, x, y, 1, 1,
428 AllPlanes, ZPixmap );
432 /* If we are reading from the screen, use a temporary copy */
433 /* to avoid a BadMatch error */
434 if (!pixmap) pixmap = XCreatePixmap( display, rootWindow,
435 1, 1, dc->w.bitsPerPixel );
436 XCopyArea( display, dc->u.x.drawable, pixmap, BITMAP_colorGC,
438 image = XGetImage( display, pixmap, 0, 0, 1, 1, AllPlanes, ZPixmap );
440 pixel = XGetPixel( image, 0, 0 );
441 XDestroyImage( image );
442 LeaveCriticalSection( &X11DRV_CritSection );
444 return COLOR_ToLogical(pixel);
448 /***********************************************************************
452 X11DRV_PaintRgn( DC *dc, HRGN32 hrgn )
455 HRGN32 tmpVisRgn, prevVisRgn;
456 HDC32 hdc = dc->hSelf; /* FIXME: should not mix dc/hdc this way */
458 if (!(tmpVisRgn = CreateRectRgn32( 0, 0, 0, 0 ))) return FALSE;
460 /* Transform region into device co-ords */
461 if (!REGION_LPTODP( hdc, tmpVisRgn, hrgn )) {
462 DeleteObject32( tmpVisRgn );
466 /* Modify visible region */
467 if (!(prevVisRgn = SaveVisRgn( hdc ))) {
468 DeleteObject32( tmpVisRgn );
471 CombineRgn32( tmpVisRgn, prevVisRgn, tmpVisRgn, RGN_AND );
472 SelectVisRgn( hdc, tmpVisRgn );
473 DeleteObject32( tmpVisRgn );
475 /* Fill the region */
477 GetRgnBox32( dc->w.hGCClipRgn, &box );
478 if (DC_SetupGCForBrush( dc ))
479 TSXFillRectangle( display, dc->u.x.drawable, dc->u.x.gc,
480 dc->w.DCOrgX + box.left, dc->w.DCOrgY + box.top,
481 box.right-box.left, box.bottom-box.top );
483 /* Restore the visible region */
485 RestoreVisRgn( hdc );
489 /**********************************************************************
493 X11DRV_Polyline( DC *dc, const LPPOINT32 pt, INT32 count )
497 if (DC_SetupGCForPen( dc ))
498 for (i = 0; i < count-1; i ++)
499 TSXDrawLine (display, dc->u.x.drawable, dc->u.x.gc,
500 dc->w.DCOrgX + XLPTODP(dc, pt [i].x),
501 dc->w.DCOrgY + YLPTODP(dc, pt [i].y),
502 dc->w.DCOrgX + XLPTODP(dc, pt [i+1].x),
503 dc->w.DCOrgY + YLPTODP(dc, pt [i+1].y));
508 /**********************************************************************
512 X11DRV_Polygon( DC *dc, LPPOINT32 pt, INT32 count )
517 points = (XPoint *) xmalloc (sizeof (XPoint) * (count+1));
518 for (i = 0; i < count; i++)
520 points[i].x = dc->w.DCOrgX + XLPTODP( dc, pt[i].x );
521 points[i].y = dc->w.DCOrgY + YLPTODP( dc, pt[i].y );
523 points[count] = points[0];
525 if (DC_SetupGCForBrush( dc ))
526 TSXFillPolygon( display, dc->u.x.drawable, dc->u.x.gc,
527 points, count+1, Complex, CoordModeOrigin);
529 if (DC_SetupGCForPen ( dc ))
530 TSXDrawLines( display, dc->u.x.drawable, dc->u.x.gc,
531 points, count+1, CoordModeOrigin );
538 /**********************************************************************
542 X11DRV_PolyPolygon( DC *dc, LPPOINT32 pt, LPINT32 counts, UINT32 polygons)
546 /* FIXME: The points should be converted to device coords before */
547 /* creating the region. But as CreatePolyPolygonRgn is not */
548 /* really correct either, it doesn't matter much... */
549 /* At least the outline will be correct :-) */
550 hrgn = CreatePolyPolygonRgn32( pt, counts, polygons, dc->w.polyFillMode );
551 X11DRV_PaintRgn( dc, hrgn );
552 DeleteObject32( hrgn );
554 /* Draw the outline of the polygons */
556 if (DC_SetupGCForPen ( dc ))
561 for (i = 0; i < polygons; i++) if (counts[i] > max) max = counts[i];
562 points = (XPoint *) xmalloc( sizeof(XPoint) * (max+1) );
564 for (i = 0; i < polygons; i++)
566 for (j = 0; j < counts[i]; j++)
568 points[j].x = dc->w.DCOrgX + XLPTODP( dc, pt->x );
569 points[j].y = dc->w.DCOrgY + YLPTODP( dc, pt->y );
572 points[j] = points[0];
573 TSXDrawLines( display, dc->u.x.drawable, dc->u.x.gc,
574 points, j + 1, CoordModeOrigin );
582 /**********************************************************************
583 * X11DRV_PolyPolyline
586 X11DRV_PolyPolyline( DC *dc, LPPOINT32 pt, LPINT32 counts, UINT32 polylines )
588 if (DC_SetupGCForPen ( dc ))
593 for (i = 0; i < polylines; i++) if (counts[i] > max) max = counts[i];
594 points = (XPoint *) xmalloc( sizeof(XPoint) * (max+1) );
596 for (i = 0; i < polylines; i++)
598 for (j = 0; j < counts[i]; j++)
600 points[j].x = dc->w.DCOrgX + XLPTODP( dc, pt->x );
601 points[j].y = dc->w.DCOrgY + YLPTODP( dc, pt->y );
604 points[j] = points[0];
605 TSXDrawLines( display, dc->u.x.drawable, dc->u.x.gc,
606 points, j + 1, CoordModeOrigin );
614 /**********************************************************************
615 * X11DRV_InternalFloodFill
617 * Internal helper function for flood fill.
618 * (xorg,yorg) is the origin of the X image relative to the drawable.
619 * (x,y) is relative to the origin of the X image.
621 static void X11DRV_InternalFloodFill(XImage *image, DC *dc,
624 Pixel pixel, WORD fillType )
628 #define TO_FLOOD(x,y) ((fillType == FLOODFILLBORDER) ? \
629 (XGetPixel(image,x,y) != pixel) : \
630 (XGetPixel(image,x,y) == pixel))
632 if (!TO_FLOOD(x,y)) return;
634 /* Find left and right boundaries */
637 while ((left > 0) && TO_FLOOD( left-1, y )) left--;
638 while ((right < image->width) && TO_FLOOD( right, y )) right++;
639 XFillRectangle( display, dc->u.x.drawable, dc->u.x.gc,
640 xOrg + left, yOrg + y, right-left, 1 );
642 /* Set the pixels of this line so we don't fill it again */
644 for (x = left; x < right; x++)
646 if (fillType == FLOODFILLBORDER) XPutPixel( image, x, y, pixel );
647 else XPutPixel( image, x, y, ~pixel );
650 /* Fill the line above */
657 while ((x < right) && !TO_FLOOD(x,y)) x++;
658 if (x >= right) break;
659 while ((x < right) && TO_FLOOD(x,y)) x++;
660 X11DRV_InternalFloodFill(image, dc, x-1, y,
661 xOrg, yOrg, pixel, fillType );
665 /* Fill the line below */
667 if ((y += 2) < image->height)
672 while ((x < right) && !TO_FLOOD(x,y)) x++;
673 if (x >= right) break;
674 while ((x < right) && TO_FLOOD(x,y)) x++;
675 X11DRV_InternalFloodFill(image, dc, x-1, y,
676 xOrg, yOrg, pixel, fillType );
683 /**********************************************************************
686 * Main flood-fill routine.
688 * The Xlib critical section must be entered before calling this function.
691 struct FloodFill_params
700 static BOOL32 X11DRV_DoFloodFill( const struct FloodFill_params *params )
706 if (GetRgnBox32( dc->w.hGCClipRgn, &rect ) == ERROR) return FALSE;
708 if (!(image = XGetImage( display, dc->u.x.drawable,
709 dc->w.DCOrgX + rect.left,
710 dc->w.DCOrgY + rect.top,
711 rect.right - rect.left,
712 rect.bottom - rect.top,
713 AllPlanes, ZPixmap ))) return FALSE;
715 if (DC_SetupGCForBrush( dc ))
717 /* ROP mode is always GXcopy for flood-fill */
718 XSetFunction( display, dc->u.x.gc, GXcopy );
719 X11DRV_InternalFloodFill(image, dc,
720 XLPTODP(dc,params->x) - rect.left,
721 YLPTODP(dc,params->y) - rect.top,
722 dc->w.DCOrgX + rect.left,
723 dc->w.DCOrgY + rect.top,
724 COLOR_ToPhysical( dc, params->color ),
728 XDestroyImage( image );
733 /**********************************************************************
734 * X11DRV_ExtFloodFill
737 X11DRV_ExtFloodFill( DC *dc, INT32 x, INT32 y, COLORREF color,
741 struct FloodFill_params params = { dc, x, y, color, fillType };
743 TRACE(graphics, "X11DRV_ExtFloodFill %d,%d %06lx %d\n",
744 x, y, color, fillType );
746 if (!PtVisible32( dc->hSelf, x, y )) return FALSE;
747 EnterCriticalSection( &X11DRV_CritSection );
748 result = CALL_LARGE_STACK( X11DRV_DoFloodFill, ¶ms );
749 LeaveCriticalSection( &X11DRV_CritSection );