2 * X11 graphics driver graphics functions
4 * Copyright 1993,1994 Alexandre Julliard
10 #include <X11/Xutil.h>
11 #include <X11/Intrinsic.h>
35 /**********************************************************************
39 X11DRV_MoveToEx(DC *dc,INT32 x,INT32 y,LPPOINT32 pt) {
42 pt->x = dc->w.CursPosX;
43 pt->y = dc->w.CursPosY;
50 /***********************************************************************
54 X11DRV_LineTo( DC *dc, INT32 x, INT32 y )
56 if (DC_SetupGCForPen( dc ))
57 XDrawLine(display, dc->u.x.drawable, dc->u.x.gc,
58 dc->w.DCOrgX + XLPTODP( dc, dc->w.CursPosX ),
59 dc->w.DCOrgY + YLPTODP( dc, dc->w.CursPosY ),
60 dc->w.DCOrgX + XLPTODP( dc, x ),
61 dc->w.DCOrgY + YLPTODP( dc, y ) );
69 /***********************************************************************
72 * Helper functions for Arc(), Chord() and Pie().
73 * 'lines' is the number of lines to draw: 0 for Arc, 1 for Chord, 2 for Pie.
76 X11DRV_DrawArc( DC *dc, INT32 left, INT32 top, INT32 right,
77 INT32 bottom, INT32 xstart, INT32 ystart,
78 INT32 xend, INT32 yend, INT32 lines )
80 INT32 xcenter, ycenter, istart_angle, idiff_angle, tmp;
81 double start_angle, end_angle;
84 left = XLPTODP( dc, left );
85 top = YLPTODP( dc, top );
86 right = XLPTODP( dc, right );
87 bottom = YLPTODP( dc, bottom );
88 xstart = XLPTODP( dc, xstart );
89 ystart = YLPTODP( dc, ystart );
90 xend = XLPTODP( dc, xend );
91 yend = YLPTODP( dc, yend );
92 if ((left == right) || (top == bottom)) return FALSE;
94 xcenter = (right + left) / 2;
95 ycenter = (bottom + top) / 2;
96 start_angle = atan2( (double)(ycenter-ystart)*(right-left),
97 (double)(xstart-xcenter)*(bottom-top) );
98 end_angle = atan2( (double)(ycenter-yend)*(right-left),
99 (double)(xend-xcenter)*(bottom-top) );
100 istart_angle = (INT32)(start_angle * 180 * 64 / PI);
101 idiff_angle = (INT32)((end_angle - start_angle) * 180 * 64 / PI );
102 if (idiff_angle <= 0) idiff_angle += 360 * 64;
103 if (left > right) { tmp=left; left=right; right=tmp; }
104 if (top > bottom) { tmp=top; top=bottom; bottom=tmp; }
106 /* Fill arc with brush if Chord() or Pie() */
108 if ((lines > 0) && DC_SetupGCForBrush( dc ))
110 XSetArcMode( display, dc->u.x.gc, (lines==1) ? ArcChord : ArcPieSlice);
111 XFillArc( display, dc->u.x.drawable, dc->u.x.gc,
112 dc->w.DCOrgX + left, dc->w.DCOrgY + top,
113 right-left-1, bottom-top-1, istart_angle, idiff_angle );
116 /* Draw arc and lines */
118 if (!DC_SetupGCForPen( dc )) return TRUE;
119 XDrawArc( display, dc->u.x.drawable, dc->u.x.gc,
120 dc->w.DCOrgX + left, dc->w.DCOrgY + top,
121 right-left-1, bottom-top-1, istart_angle, idiff_angle );
122 if (!lines) return TRUE;
124 points[0].x = dc->w.DCOrgX + xcenter + (int)(cos(start_angle) * (right-left) / 2);
125 points[0].y = dc->w.DCOrgY + ycenter - (int)(sin(start_angle) * (bottom-top) / 2);
126 points[1].x = dc->w.DCOrgX + xcenter + (int)(cos(end_angle) * (right-left) / 2);
127 points[1].y = dc->w.DCOrgY + ycenter - (int)(sin(end_angle) * (bottom-top) / 2);
130 points[2] = points[1];
131 points[1].x = dc->w.DCOrgX + xcenter;
132 points[1].y = dc->w.DCOrgY + ycenter;
134 XDrawLines( display, dc->u.x.drawable, dc->u.x.gc,
135 points, lines+1, CoordModeOrigin );
140 /***********************************************************************
144 X11DRV_Arc( DC *dc, INT32 left, INT32 top, INT32 right, INT32 bottom,
145 INT32 xstart, INT32 ystart, INT32 xend, INT32 yend )
147 return X11DRV_DrawArc( dc, left, top, right, bottom,
148 xstart, ystart, xend, yend, 0 );
152 /***********************************************************************
156 X11DRV_Pie( DC *dc, INT32 left, INT32 top, INT32 right, INT32 bottom,
157 INT32 xstart, INT32 ystart, INT32 xend, INT32 yend )
159 return X11DRV_DrawArc( dc, left, top, right, bottom,
160 xstart, ystart, xend, yend, 2 );
163 /***********************************************************************
167 X11DRV_Chord( DC *dc, INT32 left, INT32 top, INT32 right, INT32 bottom,
168 INT32 xstart, INT32 ystart, INT32 xend, INT32 yend )
170 return X11DRV_DrawArc( dc, left, top, right, bottom,
171 xstart, ystart, xend, yend, 1 );
175 /***********************************************************************
179 X11DRV_Ellipse( DC *dc, INT32 left, INT32 top, INT32 right, INT32 bottom )
181 left = XLPTODP( dc, left );
182 top = YLPTODP( dc, top );
183 right = XLPTODP( dc, right );
184 bottom = YLPTODP( dc, bottom );
185 if ((left == right) || (top == bottom)) return FALSE;
187 if (right < left) { INT32 tmp = right; right = left; left = tmp; }
188 if (bottom < top) { INT32 tmp = bottom; bottom = top; top = tmp; }
190 if ((dc->u.x.pen.style == PS_INSIDEFRAME) &&
191 (dc->u.x.pen.width < right-left-1) &&
192 (dc->u.x.pen.width < bottom-top-1))
194 left += dc->u.x.pen.width / 2;
195 right -= (dc->u.x.pen.width + 1) / 2;
196 top += dc->u.x.pen.width / 2;
197 bottom -= (dc->u.x.pen.width + 1) / 2;
200 if (DC_SetupGCForBrush( dc ))
201 XFillArc( display, dc->u.x.drawable, dc->u.x.gc,
202 dc->w.DCOrgX + left, dc->w.DCOrgY + top,
203 right-left-1, bottom-top-1, 0, 360*64 );
204 if (DC_SetupGCForPen( dc ))
205 XDrawArc( display, dc->u.x.drawable, dc->u.x.gc,
206 dc->w.DCOrgX + left, dc->w.DCOrgY + top,
207 right-left-1, bottom-top-1, 0, 360*64 );
212 /***********************************************************************
216 X11DRV_Rectangle(DC *dc, INT32 left, INT32 top, INT32 right, INT32 bottom)
219 left = XLPTODP( dc, left );
220 top = YLPTODP( dc, top );
221 right = XLPTODP( dc, right );
222 bottom = YLPTODP( dc, bottom );
224 if (right < left) { INT32 tmp = right; right = left; left = tmp; }
225 if (bottom < top) { INT32 tmp = bottom; bottom = top; top = tmp; }
227 if ((left == right) || (top == bottom))
229 if (DC_SetupGCForPen( dc ))
230 XDrawLine(display, dc->u.x.drawable, dc->u.x.gc,
233 dc->w.DCOrgX + right,
234 dc->w.DCOrgY + bottom);
237 width = dc->u.x.pen.width;
238 if (!width) width = 1;
239 if(dc->u.x.pen.style == PS_NULL) width = 0;
241 if ((dc->u.x.pen.style == PS_INSIDEFRAME) &&
242 (width < right-left) && (width < bottom-top))
245 right -= (width + 1) / 2;
247 bottom -= (width + 1) / 2;
250 if (DC_SetupGCForBrush( dc ))
251 XFillRectangle( display, dc->u.x.drawable, dc->u.x.gc,
252 dc->w.DCOrgX + left + (width + 1) / 2,
253 dc->w.DCOrgY + top + (width + 1) / 2,
254 right-left-width-1, bottom-top-width-1);
255 if (DC_SetupGCForPen( dc ))
256 XDrawRectangle( display, dc->u.x.drawable, dc->u.x.gc,
257 dc->w.DCOrgX + left, dc->w.DCOrgY + top,
258 right-left-1, bottom-top-1 );
262 /***********************************************************************
266 X11DRV_RoundRect( DC *dc, INT32 left, INT32 top, INT32 right,
267 INT32 bottom, INT32 ell_width, INT32 ell_height )
269 dprintf_graphics(stddeb, "X11DRV_RoundRect(%d %d %d %d %d %d\n",
270 left, top, right, bottom, ell_width, ell_height);
272 left = XLPTODP( dc, left );
273 top = YLPTODP( dc, top );
274 right = XLPTODP( dc, right );
275 bottom = YLPTODP( dc, bottom );
276 ell_width = abs( ell_width * dc->vportExtX / dc->wndExtX );
277 ell_height = abs( ell_height * dc->vportExtY / dc->wndExtY );
279 /* Fix the coordinates */
281 if (right < left) { INT32 tmp = right; right = left; left = tmp; }
282 if (bottom < top) { INT32 tmp = bottom; bottom = top; top = tmp; }
283 if (ell_width > right - left) ell_width = right - left;
284 if (ell_height > bottom - top) ell_height = bottom - top;
286 if (DC_SetupGCForBrush( dc ))
288 if (ell_width && ell_height)
290 XFillArc( display, dc->u.x.drawable, dc->u.x.gc,
291 dc->w.DCOrgX + left, dc->w.DCOrgY + top,
292 ell_width, ell_height, 90 * 64, 90 * 64 );
293 XFillArc( display, dc->u.x.drawable, dc->u.x.gc,
294 dc->w.DCOrgX + left, dc->w.DCOrgY + bottom - ell_height,
295 ell_width, ell_height, 180 * 64, 90 * 64 );
296 XFillArc( display, dc->u.x.drawable, dc->u.x.gc,
297 dc->w.DCOrgX + right - ell_width,
298 dc->w.DCOrgY + bottom - ell_height,
299 ell_width, ell_height, 270 * 64, 90 * 64 );
300 XFillArc( display, dc->u.x.drawable, dc->u.x.gc,
301 dc->w.DCOrgX + right - ell_width, dc->w.DCOrgY + top,
302 ell_width, ell_height, 0, 90 * 64 );
304 if (ell_width < right - left)
306 XFillRectangle( display, dc->u.x.drawable, dc->u.x.gc,
307 dc->w.DCOrgX + left + ell_width / 2,
309 right - left - ell_width, ell_height / 2 );
310 XFillRectangle( display, dc->u.x.drawable, dc->u.x.gc,
311 dc->w.DCOrgX + left + ell_width / 2,
312 dc->w.DCOrgY + bottom - (ell_height+1) / 2,
313 right - left - ell_width, (ell_height+1) / 2 );
315 if (ell_height < bottom - top)
317 XFillRectangle( display, dc->u.x.drawable, dc->u.x.gc,
319 dc->w.DCOrgY + top + ell_height / 2,
320 right - left, bottom - top - ell_height );
323 if (DC_SetupGCForPen(dc))
325 if (ell_width && ell_height)
327 XDrawArc( display, dc->u.x.drawable, dc->u.x.gc,
328 dc->w.DCOrgX + left, dc->w.DCOrgY + top,
329 ell_width, ell_height, 90 * 64, 90 * 64 );
330 XDrawArc( display, dc->u.x.drawable, dc->u.x.gc,
331 dc->w.DCOrgX + left, dc->w.DCOrgY + bottom - ell_height,
332 ell_width, ell_height, 180 * 64, 90 * 64 );
333 XDrawArc( display, dc->u.x.drawable, dc->u.x.gc,
334 dc->w.DCOrgX + right - ell_width,
335 dc->w.DCOrgY + bottom - ell_height,
336 ell_width, ell_height, 270 * 64, 90 * 64 );
337 XDrawArc( display, dc->u.x.drawable, dc->u.x.gc,
338 dc->w.DCOrgX + right - ell_width, dc->w.DCOrgY + top,
339 ell_width, ell_height, 0, 90 * 64 );
341 if (ell_width < right - left)
343 XDrawLine( display, dc->u.x.drawable, dc->u.x.gc,
344 dc->w.DCOrgX + left + ell_width / 2,
346 dc->w.DCOrgX + right - ell_width / 2,
347 dc->w.DCOrgY + top );
348 XDrawLine( display, dc->u.x.drawable, dc->u.x.gc,
349 dc->w.DCOrgX + left + ell_width / 2,
350 dc->w.DCOrgY + bottom,
351 dc->w.DCOrgX + right - ell_width / 2,
352 dc->w.DCOrgY + bottom );
354 if (ell_height < bottom - top)
356 XDrawLine( display, dc->u.x.drawable, dc->u.x.gc,
357 dc->w.DCOrgX + right,
358 dc->w.DCOrgY + top + ell_height / 2,
359 dc->w.DCOrgX + right,
360 dc->w.DCOrgY + bottom - ell_height / 2 );
361 XDrawLine( display, dc->u.x.drawable, dc->u.x.gc,
363 dc->w.DCOrgY + top + ell_height / 2,
365 dc->w.DCOrgY + bottom - ell_height / 2 );
372 /***********************************************************************
376 X11DRV_SetPixel( DC *dc, INT32 x, INT32 y, COLORREF color )
380 x = dc->w.DCOrgX + XLPTODP( dc, x );
381 y = dc->w.DCOrgY + YLPTODP( dc, y );
382 pixel = COLOR_ToPhysical( dc, color );
384 XSetForeground( display, dc->u.x.gc, pixel );
385 XSetFunction( display, dc->u.x.gc, GXcopy );
386 XDrawPoint( display, dc->u.x.drawable, dc->u.x.gc, x, y );
388 /* inefficient but simple... */
390 return COLOR_ToLogical(pixel);
394 /***********************************************************************
398 X11DRV_GetPixel( DC *dc, INT32 x, INT32 y )
400 static Pixmap pixmap = 0;
404 x = dc->w.DCOrgX + XLPTODP( dc, x );
405 y = dc->w.DCOrgY + YLPTODP( dc, y );
406 if (dc->w.flags & DC_MEMORY)
408 image = XGetImage( display, dc->u.x.drawable, x, y, 1, 1,
409 AllPlanes, ZPixmap );
413 /* If we are reading from the screen, use a temporary copy */
414 /* to avoid a BadMatch error */
415 if (!pixmap) pixmap = XCreatePixmap( display, rootWindow,
416 1, 1, dc->w.bitsPerPixel );
417 XCopyArea( display, dc->u.x.drawable, pixmap, BITMAP_colorGC,
419 image = XGetImage( display, pixmap, 0, 0, 1, 1, AllPlanes, ZPixmap );
421 pixel = XGetPixel( image, 0, 0 );
422 XDestroyImage( image );
424 return COLOR_ToLogical(pixel);
428 /***********************************************************************
432 X11DRV_PaintRgn( DC *dc, HRGN32 hrgn )
435 HRGN32 tmpVisRgn, prevVisRgn;
436 HDC32 hdc = dc->hSelf; /* FIXME: should not mix dc/hdc this way */
438 /* Modify visible region */
440 if (!(prevVisRgn = SaveVisRgn( hdc ))) return FALSE;
441 if (!(tmpVisRgn = CreateRectRgn32( 0, 0, 0, 0 )))
443 RestoreVisRgn( hdc );
446 CombineRgn32( tmpVisRgn, prevVisRgn, hrgn, RGN_AND );
447 SelectVisRgn( hdc, tmpVisRgn );
448 DeleteObject32( tmpVisRgn );
450 /* Fill the region */
452 GetRgnBox32( dc->w.hGCClipRgn, &box );
453 if (DC_SetupGCForBrush( dc ))
454 XFillRectangle( display, dc->u.x.drawable, dc->u.x.gc,
455 dc->w.DCOrgX + box.left, dc->w.DCOrgY + box.top,
456 box.right-box.left, box.bottom-box.top );
458 /* Restore the visible region */
460 RestoreVisRgn( hdc );
464 /**********************************************************************
468 X11DRV_Polyline( DC *dc, const LPPOINT32 pt, INT32 count )
472 if (DC_SetupGCForPen( dc ))
473 for (i = 0; i < count-1; i ++)
474 XDrawLine (display, dc->u.x.drawable, dc->u.x.gc,
475 dc->w.DCOrgX + XLPTODP(dc, pt [i].x),
476 dc->w.DCOrgY + YLPTODP(dc, pt [i].y),
477 dc->w.DCOrgX + XLPTODP(dc, pt [i+1].x),
478 dc->w.DCOrgY + YLPTODP(dc, pt [i+1].y));
483 /**********************************************************************
487 X11DRV_Polygon( DC *dc, LPPOINT32 pt, INT32 count )
492 points = (XPoint *) xmalloc (sizeof (XPoint) * (count+1));
493 for (i = 0; i < count; i++)
495 points[i].x = dc->w.DCOrgX + XLPTODP( dc, pt[i].x );
496 points[i].y = dc->w.DCOrgY + YLPTODP( dc, pt[i].y );
498 points[count] = points[0];
500 if (DC_SetupGCForBrush( dc ))
501 XFillPolygon( display, dc->u.x.drawable, dc->u.x.gc,
502 points, count+1, Complex, CoordModeOrigin);
504 if (DC_SetupGCForPen ( dc ))
505 XDrawLines( display, dc->u.x.drawable, dc->u.x.gc,
506 points, count+1, CoordModeOrigin );
513 /**********************************************************************
517 X11DRV_PolyPolygon( DC *dc, LPPOINT32 pt, LPINT32 counts, UINT32 polygons)
521 /* FIXME: The points should be converted to device coords before */
522 /* creating the region. But as CreatePolyPolygonRgn is not */
523 /* really correct either, it doesn't matter much... */
524 /* At least the outline will be correct :-) */
525 hrgn = CreatePolyPolygonRgn32( pt, counts, polygons, dc->w.polyFillMode );
526 X11DRV_PaintRgn( dc, hrgn );
527 DeleteObject32( hrgn );
529 /* Draw the outline of the polygons */
531 if (DC_SetupGCForPen ( dc ))
536 for (i = 0; i < polygons; i++) if (counts[i] > max) max = counts[i];
537 points = (XPoint *) xmalloc( sizeof(XPoint) * (max+1) );
539 for (i = 0; i < polygons; i++)
541 for (j = 0; j < counts[i]; j++)
543 points[j].x = dc->w.DCOrgX + XLPTODP( dc, pt->x );
544 points[j].y = dc->w.DCOrgY + YLPTODP( dc, pt->y );
547 points[j] = points[0];
548 XDrawLines( display, dc->u.x.drawable, dc->u.x.gc,
549 points, j + 1, CoordModeOrigin );
557 /**********************************************************************
558 * X11DRV_InternalFloodFill
560 * Internal helper function for flood fill.
561 * (xorg,yorg) is the origin of the X image relative to the drawable.
562 * (x,y) is relative to the origin of the X image.
564 static void X11DRV_InternalFloodFill(XImage *image, DC *dc,
567 Pixel pixel, WORD fillType )
571 #define TO_FLOOD(x,y) ((fillType == FLOODFILLBORDER) ? \
572 (XGetPixel(image,x,y) != pixel) : \
573 (XGetPixel(image,x,y) == pixel))
575 if (!TO_FLOOD(x,y)) return;
577 /* Find left and right boundaries */
580 while ((left > 0) && TO_FLOOD( left-1, y )) left--;
581 while ((right < image->width) && TO_FLOOD( right, y )) right++;
582 XFillRectangle( display, dc->u.x.drawable, dc->u.x.gc,
583 xOrg + left, yOrg + y, right-left, 1 );
585 /* Set the pixels of this line so we don't fill it again */
587 for (x = left; x < right; x++)
589 if (fillType == FLOODFILLBORDER) XPutPixel( image, x, y, pixel );
590 else XPutPixel( image, x, y, ~pixel );
593 /* Fill the line above */
600 while ((x < right) && !TO_FLOOD(x,y)) x++;
601 if (x >= right) break;
602 while ((x < right) && TO_FLOOD(x,y)) x++;
603 X11DRV_InternalFloodFill(image, dc, x-1, y,
604 xOrg, yOrg, pixel, fillType );
608 /* Fill the line below */
610 if ((y += 2) < image->height)
615 while ((x < right) && !TO_FLOOD(x,y)) x++;
616 if (x >= right) break;
617 while ((x < right) && TO_FLOOD(x,y)) x++;
618 X11DRV_InternalFloodFill(image, dc, x-1, y,
619 xOrg, yOrg, pixel, fillType );
626 /**********************************************************************
629 * Main flood-fill routine.
631 static BOOL32 X11DRV_DoFloodFill( DC *dc, RECT32 *rect, INT32 x, INT32 y,
632 COLORREF color, UINT32 fillType )
636 if (!(image = XGetImage( display, dc->u.x.drawable,
637 dc->w.DCOrgX + rect->left,
638 dc->w.DCOrgY + rect->top,
639 rect->right - rect->left,
640 rect->bottom - rect->top,
641 AllPlanes, ZPixmap ))) return FALSE;
643 if (DC_SetupGCForBrush( dc ))
645 /* ROP mode is always GXcopy for flood-fill */
646 XSetFunction( display, dc->u.x.gc, GXcopy );
647 X11DRV_InternalFloodFill(image, dc,
648 XLPTODP(dc,x) - rect->left,
649 YLPTODP(dc,y) - rect->top,
650 dc->w.DCOrgX + rect->left,
651 dc->w.DCOrgY + rect->top,
652 COLOR_ToPhysical( dc, color ), fillType );
655 XDestroyImage( image );
660 /**********************************************************************
661 * X11DRV_ExtFloodFill
664 X11DRV_ExtFloodFill( DC *dc, INT32 x, INT32 y, COLORREF color,
668 HDC32 hdc = dc->hSelf; /* FIXME */
670 dprintf_graphics( stddeb, "X11DRV_ExtFloodFill %d,%d %06lx %d\n",
671 x, y, color, fillType );
673 if (!PtVisible32( hdc, x, y )) return FALSE;
674 if (GetRgnBox32( dc->w.hGCClipRgn, &rect ) == ERROR) return FALSE;
676 return CallTo32_LargeStack( (int(*)())X11DRV_DoFloodFill, 6,
677 dc, &rect, x, y, color, fillType );