2 * X11 graphics driver graphics functions
4 * Copyright 1993,1994 Alexandre Julliard
5 * Copyright 1998 Huw Davies
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 * FIXME: only some of these functions obey the GM_ADVANCED
47 #include "wine/debug.h"
48 #include "wine/unicode.h"
50 WINE_DEFAULT_DEBUG_CHANNEL(graphics);
52 #define ABS(x) ((x)<0?(-(x)):(x))
54 /* ROP code to GC function conversion */
55 const int X11DRV_XROPfunction[16] =
57 GXclear, /* R2_BLACK */
58 GXnor, /* R2_NOTMERGEPEN */
59 GXandInverted, /* R2_MASKNOTPEN */
60 GXcopyInverted, /* R2_NOTCOPYPEN */
61 GXandReverse, /* R2_MASKPENNOT */
62 GXinvert, /* R2_NOT */
63 GXxor, /* R2_XORPEN */
64 GXnand, /* R2_NOTMASKPEN */
65 GXand, /* R2_MASKPEN */
66 GXequiv, /* R2_NOTXORPEN */
68 GXorInverted, /* R2_MERGENOTPEN */
69 GXcopy, /* R2_COPYPEN */
70 GXorReverse, /* R2_MERGEPENNOT */
71 GXor, /* R2_MERGEPEN */
76 /* get the rectangle in device coordinates, with optional mirroring */
77 static RECT get_device_rect( HDC hdc, int left, int top, int right, int bottom )
85 if (GetLayout( hdc ) & LAYOUT_RTL)
87 /* shift the rectangle so that the right border is included after mirroring */
88 /* it would be more correct to do this after LPtoDP but that's not what Windows does */
92 LPtoDP( hdc, (POINT *)&rect, 2 );
93 if (rect.left > rect.right)
96 rect.left = rect.right;
99 if (rect.top > rect.bottom)
102 rect.top = rect.bottom;
108 /***********************************************************************
109 * X11DRV_GetRegionData
111 * Calls GetRegionData on the given region and converts the rectangle
112 * array to XRectangle format. The returned buffer must be freed by
113 * caller using HeapFree(GetProcessHeap(),...).
114 * If hdc_lptodp is not 0, the rectangles are converted through LPtoDP.
116 RGNDATA *X11DRV_GetRegionData( HRGN hrgn, HDC hdc_lptodp )
124 if (!(size = GetRegionData( hrgn, 0, NULL ))) return NULL;
125 if (sizeof(XRectangle) > sizeof(RECT))
127 /* add extra size for XRectangle array */
128 int count = (size - sizeof(RGNDATAHEADER)) / sizeof(RECT);
129 size += count * (sizeof(XRectangle) - sizeof(RECT));
131 if (!(data = HeapAlloc( GetProcessHeap(), 0, size ))) return NULL;
132 if (!GetRegionData( hrgn, size, data ))
134 HeapFree( GetProcessHeap(), 0, data );
138 rect = (RECT *)data->Buffer;
139 xrect = (XRectangle *)data->Buffer;
140 if (hdc_lptodp) /* map to device coordinates */
142 LPtoDP( hdc_lptodp, (POINT *)rect, data->rdh.nCount * 2 );
143 for (i = 0; i < data->rdh.nCount; i++)
145 if (rect[i].right < rect[i].left)
147 INT tmp = rect[i].right;
148 rect[i].right = rect[i].left;
151 if (rect[i].bottom < rect[i].top)
153 INT tmp = rect[i].bottom;
154 rect[i].bottom = rect[i].top;
160 if (sizeof(XRectangle) > sizeof(RECT))
163 /* need to start from the end */
164 xrect += data->rdh.nCount;
165 for (j = data->rdh.nCount-1; j >= 0; j--)
168 if (tmp.left > SHRT_MAX) continue;
169 if (tmp.top > SHRT_MAX) continue;
170 if (tmp.right < SHRT_MIN) continue;
171 if (tmp.bottom < SHRT_MIN) continue;
173 xrect->x = max( min( tmp.left, SHRT_MAX), SHRT_MIN);
174 xrect->y = max( min( tmp.top, SHRT_MAX), SHRT_MIN);
175 xrect->width = max( min( tmp.right, SHRT_MAX ) - xrect->x, 0);
176 xrect->height = max( min( tmp.bottom, SHRT_MAX ) - xrect->y, 0);
178 if (xrect > (XRectangle *)data->Buffer)
180 data->rdh.nCount -= xrect - (XRectangle *)data->Buffer;
181 memmove( (XRectangle *)data->Buffer, xrect, data->rdh.nCount * sizeof(*xrect) );
186 for (i = 0; i < data->rdh.nCount; i++)
189 if (tmp.left > SHRT_MAX) continue;
190 if (tmp.top > SHRT_MAX) continue;
191 if (tmp.right < SHRT_MIN) continue;
192 if (tmp.bottom < SHRT_MIN) continue;
193 xrect->x = max( min( tmp.left, SHRT_MAX), SHRT_MIN);
194 xrect->y = max( min( tmp.top, SHRT_MAX), SHRT_MIN);
195 xrect->width = max( min( tmp.right, SHRT_MAX ) - xrect->x, 0);
196 xrect->height = max( min( tmp.bottom, SHRT_MAX ) - xrect->y, 0);
199 data->rdh.nCount = xrect - (XRectangle *)data->Buffer;
205 /***********************************************************************
206 * update_x11_clipping
208 static void update_x11_clipping( X11DRV_PDEVICE *physDev, HRGN rgn )
215 XSetClipMask( gdi_display, physDev->gc, None );
218 else if ((data = X11DRV_GetRegionData( rgn, 0 )))
221 XSetClipRectangles( gdi_display, physDev->gc, physDev->dc_rect.left, physDev->dc_rect.top,
222 (XRectangle *)data->Buffer, data->rdh.nCount, YXBanded );
224 HeapFree( GetProcessHeap(), 0, data );
229 /***********************************************************************
230 * add_extra_clipping_region
232 * Temporarily add a region to the current clipping region.
233 * The region must be restored with restore_clipping_region.
235 BOOL add_extra_clipping_region( X11DRV_PDEVICE *dev, HRGN rgn )
239 if (!rgn) return FALSE;
242 if (!(clip = CreateRectRgn( 0, 0, 0, 0 ))) return FALSE;
243 CombineRgn( clip, dev->region, rgn, RGN_AND );
244 update_x11_clipping( dev, clip );
245 DeleteObject( clip );
247 else update_x11_clipping( dev, rgn );
252 /***********************************************************************
253 * restore_clipping_region
255 void restore_clipping_region( X11DRV_PDEVICE *dev )
257 update_x11_clipping( dev, dev->region );
261 /***********************************************************************
262 * X11DRV_SetDeviceClipping
264 void X11DRV_SetDeviceClipping( PHYSDEV dev, HRGN rgn )
266 X11DRV_PDEVICE *physDev = get_x11drv_dev( dev );
268 physDev->region = rgn;
269 update_x11_clipping( physDev, rgn );
273 /***********************************************************************
274 * X11DRV_SetupGCForPatBlt
276 * Setup the GC for a PatBlt operation using current brush.
277 * If fMapColors is TRUE, X pixels are mapped to Windows colors.
278 * Return FALSE if brush is BS_NULL, TRUE otherwise.
280 BOOL X11DRV_SetupGCForPatBlt( X11DRV_PDEVICE *physDev, GC gc, BOOL fMapColors )
287 if (physDev->brush.style == BS_NULL) return FALSE;
288 if (physDev->brush.pixel == -1)
290 /* Special case used for monochrome pattern brushes.
291 * We need to swap foreground and background because
292 * Windows does it the wrong way...
294 val.foreground = X11DRV_PALETTE_ToPhysical( physDev, GetBkColor(physDev->dev.hdc) );
295 val.background = X11DRV_PALETTE_ToPhysical( physDev, GetTextColor(physDev->dev.hdc) );
299 val.foreground = physDev->brush.pixel;
300 val.background = X11DRV_PALETTE_ToPhysical( physDev, GetBkColor(physDev->dev.hdc) );
302 if (fMapColors && X11DRV_PALETTE_XPixelToPalette)
304 val.foreground = X11DRV_PALETTE_XPixelToPalette[val.foreground];
305 val.background = X11DRV_PALETTE_XPixelToPalette[val.background];
308 val.function = X11DRV_XROPfunction[GetROP2(physDev->dev.hdc)-1];
310 ** Let's replace GXinvert by GXxor with (black xor white)
311 ** This solves the selection color and leak problems in excel
312 ** FIXME : Let's do that only if we work with X-pixels, not with Win-pixels
314 if (val.function == GXinvert)
316 val.foreground = (WhitePixel( gdi_display, DefaultScreen(gdi_display) ) ^
317 BlackPixel( gdi_display, DefaultScreen(gdi_display) ));
318 val.function = GXxor;
320 val.fill_style = physDev->brush.fillStyle;
321 switch(val.fill_style)
324 case FillOpaqueStippled:
325 if (GetBkMode(physDev->dev.hdc)==OPAQUE) val.fill_style = FillOpaqueStippled;
326 val.stipple = physDev->brush.pixmap;
331 if (fMapColors && X11DRV_PALETTE_XPixelToPalette)
336 pixmap = XCreatePixmap( gdi_display, root_window, 8, 8, physDev->depth );
337 image = XGetImage( gdi_display, physDev->brush.pixmap, 0, 0, 8, 8,
338 AllPlanes, ZPixmap );
339 for (y = 0; y < 8; y++)
340 for (x = 0; x < 8; x++)
341 XPutPixel( image, x, y,
342 X11DRV_PALETTE_XPixelToPalette[XGetPixel( image, x, y)] );
343 XPutImage( gdi_display, pixmap, gc, image, 0, 0, 0, 0, 8, 8 );
344 XDestroyImage( image );
348 else val.tile = physDev->brush.pixmap;
356 GetBrushOrgEx( physDev->dev.hdc, &pt );
357 val.ts_x_origin = physDev->dc_rect.left + pt.x;
358 val.ts_y_origin = physDev->dc_rect.top + pt.y;
359 val.fill_rule = (GetPolyFillMode(physDev->dev.hdc) == WINDING) ? WindingRule : EvenOddRule;
361 XChangeGC( gdi_display, gc,
362 GCFunction | GCForeground | GCBackground | GCFillStyle |
363 GCFillRule | GCTileStipXOrigin | GCTileStipYOrigin | mask,
365 if (pixmap) XFreePixmap( gdi_display, pixmap );
371 /***********************************************************************
372 * X11DRV_SetupGCForBrush
374 * Setup physDev->gc for drawing operations using current brush.
375 * Return FALSE if brush is BS_NULL, TRUE otherwise.
377 BOOL X11DRV_SetupGCForBrush( X11DRV_PDEVICE *physDev )
379 return X11DRV_SetupGCForPatBlt( physDev, physDev->gc, FALSE );
383 /***********************************************************************
384 * X11DRV_SetupGCForPen
386 * Setup physDev->gc for drawing operations using current pen.
387 * Return FALSE if pen is PS_NULL, TRUE otherwise.
389 static BOOL X11DRV_SetupGCForPen( X11DRV_PDEVICE *physDev )
392 UINT rop2 = GetROP2(physDev->dev.hdc);
394 if (physDev->pen.style == PS_NULL) return FALSE;
399 val.foreground = BlackPixel( gdi_display, DefaultScreen(gdi_display) );
400 val.function = GXcopy;
403 val.foreground = WhitePixel( gdi_display, DefaultScreen(gdi_display) );
404 val.function = GXcopy;
407 val.foreground = physDev->pen.pixel;
408 /* It is very unlikely someone wants to XOR with 0 */
409 /* This fixes the rubber-drawings in paintbrush */
410 if (val.foreground == 0)
411 val.foreground = (WhitePixel( gdi_display, DefaultScreen(gdi_display) ) ^
412 BlackPixel( gdi_display, DefaultScreen(gdi_display) ));
413 val.function = GXxor;
416 val.foreground = physDev->pen.pixel;
417 val.function = X11DRV_XROPfunction[rop2-1];
419 val.background = X11DRV_PALETTE_ToPhysical( physDev, GetBkColor(physDev->dev.hdc) );
420 val.fill_style = FillSolid;
421 val.line_width = physDev->pen.width;
422 if (val.line_width <= 1) {
423 val.cap_style = CapNotLast;
425 switch (physDev->pen.endcap)
427 case PS_ENDCAP_SQUARE:
428 val.cap_style = CapProjecting;
431 val.cap_style = CapButt;
433 case PS_ENDCAP_ROUND:
435 val.cap_style = CapRound;
438 switch (physDev->pen.linejoin)
441 val.join_style = JoinBevel;
444 val.join_style = JoinMiter;
448 val.join_style = JoinRound;
451 if (physDev->pen.dash_len)
452 val.line_style = ((GetBkMode(physDev->dev.hdc) == OPAQUE) && (!physDev->pen.ext))
453 ? LineDoubleDash : LineOnOffDash;
455 val.line_style = LineSolid;
458 if (physDev->pen.dash_len)
459 XSetDashes( gdi_display, physDev->gc, 0, physDev->pen.dashes, physDev->pen.dash_len );
460 XChangeGC( gdi_display, physDev->gc,
461 GCFunction | GCForeground | GCBackground | GCLineWidth |
462 GCLineStyle | GCCapStyle | GCJoinStyle | GCFillStyle, &val );
468 /***********************************************************************
469 * X11DRV_SetupGCForText
471 * Setup physDev->gc for text drawing operations.
472 * Return FALSE if the font is null, TRUE otherwise.
474 BOOL X11DRV_SetupGCForText( X11DRV_PDEVICE *physDev )
476 XFontStruct* xfs = XFONT_GetFontStruct( physDev->font );
482 val.function = GXcopy; /* Text is always GXcopy */
483 val.foreground = X11DRV_PALETTE_ToPhysical( physDev, GetTextColor(physDev->dev.hdc) );
484 val.background = X11DRV_PALETTE_ToPhysical( physDev, GetBkColor(physDev->dev.hdc) );
485 val.fill_style = FillSolid;
489 XChangeGC( gdi_display, physDev->gc,
490 GCFunction | GCForeground | GCBackground | GCFillStyle |
495 WARN("Physical font failure\n" );
499 /***********************************************************************
502 * Performs a world-to-viewport transformation on the specified width.
504 INT X11DRV_XWStoDS( HDC hdc, INT width )
512 LPtoDP( hdc, pt, 2 );
513 return pt[1].x - pt[0].x;
516 /***********************************************************************
519 * Performs a world-to-viewport transformation on the specified height.
521 INT X11DRV_YWStoDS( HDC hdc, INT height )
529 LPtoDP( hdc, pt, 2 );
530 return pt[1].y - pt[0].y;
533 /***********************************************************************
536 BOOL X11DRV_LineTo( PHYSDEV dev, INT x, INT y )
538 X11DRV_PDEVICE *physDev = get_x11drv_dev( dev );
541 if (X11DRV_SetupGCForPen( physDev )) {
542 GetCurrentPositionEx( dev->hdc, &pt[0] );
545 LPtoDP( dev->hdc, pt, 2 );
548 XDrawLine(gdi_display, physDev->drawable, physDev->gc,
549 physDev->dc_rect.left + pt[0].x, physDev->dc_rect.top + pt[0].y,
550 physDev->dc_rect.left + pt[1].x, physDev->dc_rect.top + pt[1].y );
558 /***********************************************************************
561 * Helper functions for Arc(), Chord() and Pie().
562 * 'lines' is the number of lines to draw: 0 for Arc, 1 for Chord, 2 for Pie.
565 static BOOL X11DRV_DrawArc( PHYSDEV dev, INT left, INT top, INT right, INT bottom,
566 INT xstart, INT ystart, INT xend, INT yend, INT lines )
568 X11DRV_PDEVICE *physDev = get_x11drv_dev( dev );
569 INT xcenter, ycenter, istart_angle, idiff_angle;
571 double start_angle, end_angle;
574 RECT rc = get_device_rect( dev->hdc, left, top, right, bottom );
580 LPtoDP(dev->hdc, &start, 1);
581 LPtoDP(dev->hdc, &end, 1);
583 if ((rc.left == rc.right) || (rc.top == rc.bottom)
584 ||(lines && ((rc.right-rc.left==1)||(rc.bottom-rc.top==1)))) return TRUE;
586 if (GetArcDirection( dev->hdc ) == AD_CLOCKWISE)
587 { POINT tmp = start; start = end; end = tmp; }
589 oldwidth = width = physDev->pen.width;
590 if (!width) width = 1;
591 if(physDev->pen.style == PS_NULL) width = 0;
593 if (physDev->pen.style == PS_INSIDEFRAME)
595 if (2*width > (rc.right-rc.left)) width=(rc.right-rc.left + 1)/2;
596 if (2*width > (rc.bottom-rc.top)) width=(rc.bottom-rc.top + 1)/2;
597 rc.left += width / 2;
598 rc.right -= (width - 1) / 2;
600 rc.bottom -= (width - 1) / 2;
602 if(width == 0) width = 1; /* more accurate */
603 physDev->pen.width = width;
605 xcenter = (rc.right + rc.left) / 2;
606 ycenter = (rc.bottom + rc.top) / 2;
607 start_angle = atan2( (double)(ycenter-start.y)*(rc.right-rc.left),
608 (double)(start.x-xcenter)*(rc.bottom-rc.top) );
609 end_angle = atan2( (double)(ycenter-end.y)*(rc.right-rc.left),
610 (double)(end.x-xcenter)*(rc.bottom-rc.top) );
611 if ((start.x==end.x)&&(start.y==end.y))
612 { /* A lazy program delivers xstart=xend=ystart=yend=0) */
616 else /* notorious cases */
617 if ((start_angle == PI)&&( end_angle <0))
620 if ((end_angle == PI)&&( start_angle <0))
622 istart_angle = (INT)(start_angle * 180 * 64 / PI + 0.5);
623 idiff_angle = (INT)((end_angle - start_angle) * 180 * 64 / PI + 0.5);
624 if (idiff_angle <= 0) idiff_angle += 360 * 64;
626 /* Fill arc with brush if Chord() or Pie() */
628 if ((lines > 0) && X11DRV_SetupGCForBrush( physDev )) {
630 XSetArcMode( gdi_display, physDev->gc, (lines==1) ? ArcChord : ArcPieSlice);
631 XFillArc( gdi_display, physDev->drawable, physDev->gc,
632 physDev->dc_rect.left + rc.left, physDev->dc_rect.top + rc.top,
633 rc.right-rc.left-1, rc.bottom-rc.top-1, istart_angle, idiff_angle );
637 /* Draw arc and lines */
639 if (X11DRV_SetupGCForPen( physDev ))
642 XDrawArc( gdi_display, physDev->drawable, physDev->gc,
643 physDev->dc_rect.left + rc.left, physDev->dc_rect.top + rc.top,
644 rc.right-rc.left-1, rc.bottom-rc.top-1, istart_angle, idiff_angle );
646 /* use the truncated values */
647 start_angle=(double)istart_angle*PI/64./180.;
648 end_angle=(double)(istart_angle+idiff_angle)*PI/64./180.;
649 /* calculate the endpoints and round correctly */
650 points[0].x = (int) floor(physDev->dc_rect.left + (rc.right+rc.left)/2.0 +
651 cos(start_angle) * (rc.right-rc.left-width*2+2) / 2. + 0.5);
652 points[0].y = (int) floor(physDev->dc_rect.top + (rc.top+rc.bottom)/2.0 -
653 sin(start_angle) * (rc.bottom-rc.top-width*2+2) / 2. + 0.5);
654 points[1].x = (int) floor(physDev->dc_rect.left + (rc.right+rc.left)/2.0 +
655 cos(end_angle) * (rc.right-rc.left-width*2+2) / 2. + 0.5);
656 points[1].y = (int) floor(physDev->dc_rect.top + (rc.top+rc.bottom)/2.0 -
657 sin(end_angle) * (rc.bottom-rc.top-width*2+2) / 2. + 0.5);
659 /* OK, this stuff is optimized for Xfree86
660 * which is probably the server most used by
661 * wine users. Other X servers will not
662 * display correctly. (eXceed for instance)
663 * so if you feel you must make changes, make sure that
664 * you either use Xfree86 or separate your changes
665 * from these (compile switch or whatever)
669 points[3] = points[1];
670 points[1].x = physDev->dc_rect.left + xcenter;
671 points[1].y = physDev->dc_rect.top + ycenter;
672 points[2] = points[1];
673 dx1=points[1].x-points[0].x;
674 dy1=points[1].y-points[0].y;
675 if(((rc.top-rc.bottom) | -2) == -2)
676 if(dy1>0) points[1].y--;
678 if (((-dx1)*64)<=ABS(dy1)*37) points[0].x--;
679 if(((-dx1*9))<(dy1*16)) points[0].y--;
680 if( dy1<0 && ((dx1*9)) < (dy1*16)) points[0].y--;
682 if(dy1 < 0) points[0].y--;
683 if(((rc.right-rc.left) | -2) == -2) points[1].x--;
685 dx1=points[3].x-points[2].x;
686 dy1=points[3].y-points[2].y;
687 if(((rc.top-rc.bottom) | -2 ) == -2)
688 if(dy1 < 0) points[2].y--;
690 if( dy1>0) points[3].y--;
691 if(((rc.right-rc.left) | -2) == -2 ) points[2].x--;
694 if( dx1 * 64 < dy1 * -37 ) points[3].x--;
698 XDrawLines( gdi_display, physDev->drawable, physDev->gc,
699 points, lines+1, CoordModeOrigin );
704 physDev->pen.width = oldwidth;
709 /***********************************************************************
712 BOOL X11DRV_Arc( PHYSDEV dev, INT left, INT top, INT right, INT bottom,
713 INT xstart, INT ystart, INT xend, INT yend )
715 return X11DRV_DrawArc( dev, left, top, right, bottom, xstart, ystart, xend, yend, 0 );
719 /***********************************************************************
722 BOOL X11DRV_Pie( PHYSDEV dev, INT left, INT top, INT right, INT bottom,
723 INT xstart, INT ystart, INT xend, INT yend )
725 return X11DRV_DrawArc( dev, left, top, right, bottom, xstart, ystart, xend, yend, 2 );
728 /***********************************************************************
731 BOOL X11DRV_Chord( PHYSDEV dev, INT left, INT top, INT right, INT bottom,
732 INT xstart, INT ystart, INT xend, INT yend )
734 return X11DRV_DrawArc( dev, left, top, right, bottom, xstart, ystart, xend, yend, 1 );
738 /***********************************************************************
741 BOOL X11DRV_Ellipse( PHYSDEV dev, INT left, INT top, INT right, INT bottom )
743 X11DRV_PDEVICE *physDev = get_x11drv_dev( dev );
745 RECT rc = get_device_rect( dev->hdc, left, top, right, bottom );
747 if ((rc.left == rc.right) || (rc.top == rc.bottom)) return TRUE;
749 oldwidth = width = physDev->pen.width;
750 if (!width) width = 1;
751 if(physDev->pen.style == PS_NULL) width = 0;
753 if (physDev->pen.style == PS_INSIDEFRAME)
755 if (2*width > (rc.right-rc.left)) width=(rc.right-rc.left + 1)/2;
756 if (2*width > (rc.bottom-rc.top)) width=(rc.bottom-rc.top + 1)/2;
757 rc.left += width / 2;
758 rc.right -= (width - 1) / 2;
760 rc.bottom -= (width - 1) / 2;
762 if(width == 0) width = 1; /* more accurate */
763 physDev->pen.width = width;
765 if (X11DRV_SetupGCForBrush( physDev ))
768 XFillArc( gdi_display, physDev->drawable, physDev->gc,
769 physDev->dc_rect.left + rc.left, physDev->dc_rect.top + rc.top,
770 rc.right-rc.left-1, rc.bottom-rc.top-1, 0, 360*64 );
773 if (X11DRV_SetupGCForPen( physDev ))
776 XDrawArc( gdi_display, physDev->drawable, physDev->gc,
777 physDev->dc_rect.left + rc.left, physDev->dc_rect.top + rc.top,
778 rc.right-rc.left-1, rc.bottom-rc.top-1, 0, 360*64 );
782 physDev->pen.width = oldwidth;
787 /***********************************************************************
790 BOOL X11DRV_Rectangle(PHYSDEV dev, INT left, INT top, INT right, INT bottom)
792 X11DRV_PDEVICE *physDev = get_x11drv_dev( dev );
793 INT width, oldwidth, oldjoinstyle;
794 RECT rc = get_device_rect( dev->hdc, left, top, right, bottom );
796 TRACE("(%d %d %d %d)\n", left, top, right, bottom);
798 if ((rc.left == rc.right) || (rc.top == rc.bottom)) return TRUE;
800 oldwidth = width = physDev->pen.width;
801 if (!width) width = 1;
802 if(physDev->pen.style == PS_NULL) width = 0;
804 if (physDev->pen.style == PS_INSIDEFRAME)
806 if (2*width > (rc.right-rc.left)) width=(rc.right-rc.left + 1)/2;
807 if (2*width > (rc.bottom-rc.top)) width=(rc.bottom-rc.top + 1)/2;
808 rc.left += width / 2;
809 rc.right -= (width - 1) / 2;
811 rc.bottom -= (width - 1) / 2;
813 if(width == 1) width = 0;
814 physDev->pen.width = width;
815 oldjoinstyle = physDev->pen.linejoin;
816 if(physDev->pen.type != PS_GEOMETRIC)
817 physDev->pen.linejoin = PS_JOIN_MITER;
819 if ((rc.right > rc.left + width) && (rc.bottom > rc.top + width))
821 if (X11DRV_SetupGCForBrush( physDev ))
824 XFillRectangle( gdi_display, physDev->drawable, physDev->gc,
825 physDev->dc_rect.left + rc.left + (width + 1) / 2,
826 physDev->dc_rect.top + rc.top + (width + 1) / 2,
827 rc.right-rc.left-width-1, rc.bottom-rc.top-width-1);
831 if (X11DRV_SetupGCForPen( physDev ))
834 XDrawRectangle( gdi_display, physDev->drawable, physDev->gc,
835 physDev->dc_rect.left + rc.left, physDev->dc_rect.top + rc.top,
836 rc.right-rc.left-1, rc.bottom-rc.top-1 );
840 physDev->pen.width = oldwidth;
841 physDev->pen.linejoin = oldjoinstyle;
845 /***********************************************************************
848 BOOL X11DRV_RoundRect( PHYSDEV dev, INT left, INT top, INT right, INT bottom,
849 INT ell_width, INT ell_height )
851 X11DRV_PDEVICE *physDev = get_x11drv_dev( dev );
852 INT width, oldwidth, oldendcap;
854 RECT rc = get_device_rect( dev->hdc, left, top, right, bottom );
856 TRACE("(%d %d %d %d %d %d\n",
857 left, top, right, bottom, ell_width, ell_height);
859 if ((rc.left == rc.right) || (rc.top == rc.bottom))
862 /* Make sure ell_width and ell_height are >= 1 otherwise XDrawArc gets
863 called with width/height < 0 */
864 pts[0].x = pts[0].y = 0;
865 pts[1].x = ell_width;
866 pts[1].y = ell_height;
867 LPtoDP(dev->hdc, pts, 2);
868 ell_width = max(abs( pts[1].x - pts[0].x ), 1);
869 ell_height = max(abs( pts[1].y - pts[0].y ), 1);
871 oldwidth = width = physDev->pen.width;
872 oldendcap = physDev->pen.endcap;
873 if (!width) width = 1;
874 if(physDev->pen.style == PS_NULL) width = 0;
876 if (physDev->pen.style == PS_INSIDEFRAME)
878 if (2*width > (rc.right-rc.left)) width=(rc.right-rc.left + 1)/2;
879 if (2*width > (rc.bottom-rc.top)) width=(rc.bottom-rc.top + 1)/2;
880 rc.left += width / 2;
881 rc.right -= (width - 1) / 2;
883 rc.bottom -= (width - 1) / 2;
885 if(width == 0) width = 1;
886 physDev->pen.width = width;
887 physDev->pen.endcap = PS_ENDCAP_SQUARE;
889 if (X11DRV_SetupGCForBrush( physDev ))
892 if (ell_width > (rc.right-rc.left) )
893 if (ell_height > (rc.bottom-rc.top) )
894 XFillArc( gdi_display, physDev->drawable, physDev->gc,
895 physDev->dc_rect.left + rc.left, physDev->dc_rect.top + rc.top,
896 rc.right - rc.left - 1, rc.bottom - rc.top - 1,
899 XFillArc( gdi_display, physDev->drawable, physDev->gc,
900 physDev->dc_rect.left + rc.left, physDev->dc_rect.top + rc.top,
901 rc.right - rc.left - 1, ell_height, 0, 180 * 64 );
902 XFillArc( gdi_display, physDev->drawable, physDev->gc,
903 physDev->dc_rect.left + rc.left,
904 physDev->dc_rect.top + rc.bottom - ell_height - 1,
905 rc.right - rc.left - 1, ell_height, 180 * 64,
908 else if (ell_height > (rc.bottom-rc.top) ){
909 XFillArc( gdi_display, physDev->drawable, physDev->gc,
910 physDev->dc_rect.left + rc.left, physDev->dc_rect.top + rc.top,
911 ell_width, rc.bottom - rc.top - 1, 90 * 64, 180 * 64 );
912 XFillArc( gdi_display, physDev->drawable, physDev->gc,
913 physDev->dc_rect.left + rc.right - ell_width - 1, physDev->dc_rect.top + rc.top,
914 ell_width, rc.bottom - rc.top - 1, 270 * 64, 180 * 64 );
916 XFillArc( gdi_display, physDev->drawable, physDev->gc,
917 physDev->dc_rect.left + rc.left, physDev->dc_rect.top + rc.top,
918 ell_width, ell_height, 90 * 64, 90 * 64 );
919 XFillArc( gdi_display, physDev->drawable, physDev->gc,
920 physDev->dc_rect.left + rc.left,
921 physDev->dc_rect.top + rc.bottom - ell_height - 1,
922 ell_width, ell_height, 180 * 64, 90 * 64 );
923 XFillArc( gdi_display, physDev->drawable, physDev->gc,
924 physDev->dc_rect.left + rc.right - ell_width - 1,
925 physDev->dc_rect.top + rc.bottom - ell_height - 1,
926 ell_width, ell_height, 270 * 64, 90 * 64 );
927 XFillArc( gdi_display, physDev->drawable, physDev->gc,
928 physDev->dc_rect.left + rc.right - ell_width - 1,
929 physDev->dc_rect.top + rc.top,
930 ell_width, ell_height, 0, 90 * 64 );
932 if (ell_width < rc.right - rc.left)
934 XFillRectangle( gdi_display, physDev->drawable, physDev->gc,
935 physDev->dc_rect.left + rc.left + (ell_width + 1) / 2,
936 physDev->dc_rect.top + rc.top + 1,
937 rc.right - rc.left - ell_width - 1,
938 (ell_height + 1) / 2 - 1);
939 XFillRectangle( gdi_display, physDev->drawable, physDev->gc,
940 physDev->dc_rect.left + rc.left + (ell_width + 1) / 2,
941 physDev->dc_rect.top + rc.bottom - (ell_height) / 2 - 1,
942 rc.right - rc.left - ell_width - 1,
945 if (ell_height < rc.bottom - rc.top)
947 XFillRectangle( gdi_display, physDev->drawable, physDev->gc,
948 physDev->dc_rect.left + rc.left + 1,
949 physDev->dc_rect.top + rc.top + (ell_height + 1) / 2,
950 rc.right - rc.left - 2,
951 rc.bottom - rc.top - ell_height - 1);
955 /* FIXME: this could be done with on X call
956 * more efficient and probably more correct
957 * on any X server: XDrawArcs will draw
958 * straight horizontal and vertical lines
959 * if width or height are zero.
961 * BTW this stuff is optimized for an Xfree86 server
962 * read the comments inside the X11DRV_DrawArc function
964 if (X11DRV_SetupGCForPen( physDev ))
967 if (ell_width > (rc.right-rc.left) )
968 if (ell_height > (rc.bottom-rc.top) )
969 XDrawArc( gdi_display, physDev->drawable, physDev->gc,
970 physDev->dc_rect.left + rc.left, physDev->dc_rect.top + rc.top,
971 rc.right - rc.left - 1, rc.bottom - rc.top - 1, 0 , 360 * 64 );
973 XDrawArc( gdi_display, physDev->drawable, physDev->gc,
974 physDev->dc_rect.left + rc.left, physDev->dc_rect.top + rc.top,
975 rc.right - rc.left - 1, ell_height - 1, 0 , 180 * 64 );
976 XDrawArc( gdi_display, physDev->drawable, physDev->gc,
977 physDev->dc_rect.left + rc.left,
978 physDev->dc_rect.top + rc.bottom - ell_height,
979 rc.right - rc.left - 1, ell_height - 1, 180 * 64 , 180 * 64 );
981 else if (ell_height > (rc.bottom-rc.top) ){
982 XDrawArc( gdi_display, physDev->drawable, physDev->gc,
983 physDev->dc_rect.left + rc.left, physDev->dc_rect.top + rc.top,
984 ell_width - 1 , rc.bottom - rc.top - 1, 90 * 64 , 180 * 64 );
985 XDrawArc( gdi_display, physDev->drawable, physDev->gc,
986 physDev->dc_rect.left + rc.right - ell_width,
987 physDev->dc_rect.top + rc.top,
988 ell_width - 1 , rc.bottom - rc.top - 1, 270 * 64 , 180 * 64 );
990 XDrawArc( gdi_display, physDev->drawable, physDev->gc,
991 physDev->dc_rect.left + rc.left, physDev->dc_rect.top + rc.top,
992 ell_width - 1, ell_height - 1, 90 * 64, 90 * 64 );
993 XDrawArc( gdi_display, physDev->drawable, physDev->gc,
994 physDev->dc_rect.left + rc.left, physDev->dc_rect.top + rc.bottom - ell_height,
995 ell_width - 1, ell_height - 1, 180 * 64, 90 * 64 );
996 XDrawArc( gdi_display, physDev->drawable, physDev->gc,
997 physDev->dc_rect.left + rc.right - ell_width,
998 physDev->dc_rect.top + rc.bottom - ell_height,
999 ell_width - 1, ell_height - 1, 270 * 64, 90 * 64 );
1000 XDrawArc( gdi_display, physDev->drawable, physDev->gc,
1001 physDev->dc_rect.left + rc.right - ell_width, physDev->dc_rect.top + rc.top,
1002 ell_width - 1, ell_height - 1, 0, 90 * 64 );
1004 if (ell_width < rc.right - rc.left)
1006 XDrawLine( gdi_display, physDev->drawable, physDev->gc,
1007 physDev->dc_rect.left + rc.left + ell_width / 2,
1008 physDev->dc_rect.top + rc.top,
1009 physDev->dc_rect.left + rc.right - (ell_width+1) / 2,
1010 physDev->dc_rect.top + rc.top);
1011 XDrawLine( gdi_display, physDev->drawable, physDev->gc,
1012 physDev->dc_rect.left + rc.left + ell_width / 2 ,
1013 physDev->dc_rect.top + rc.bottom - 1,
1014 physDev->dc_rect.left + rc.right - (ell_width+1)/ 2,
1015 physDev->dc_rect.top + rc.bottom - 1);
1017 if (ell_height < rc.bottom - rc.top)
1019 XDrawLine( gdi_display, physDev->drawable, physDev->gc,
1020 physDev->dc_rect.left + rc.right - 1,
1021 physDev->dc_rect.top + rc.top + ell_height / 2,
1022 physDev->dc_rect.left + rc.right - 1,
1023 physDev->dc_rect.top + rc.bottom - (ell_height+1) / 2);
1024 XDrawLine( gdi_display, physDev->drawable, physDev->gc,
1025 physDev->dc_rect.left + rc.left,
1026 physDev->dc_rect.top + rc.top + ell_height / 2,
1027 physDev->dc_rect.left + rc.left,
1028 physDev->dc_rect.top + rc.bottom - (ell_height+1) / 2);
1030 wine_tsx11_unlock();
1033 physDev->pen.width = oldwidth;
1034 physDev->pen.endcap = oldendcap;
1039 /***********************************************************************
1042 COLORREF X11DRV_SetPixel( PHYSDEV dev, INT x, INT y, COLORREF color )
1044 X11DRV_PDEVICE *physDev = get_x11drv_dev( dev );
1045 unsigned long pixel;
1050 LPtoDP( dev->hdc, &pt, 1 );
1051 pixel = X11DRV_PALETTE_ToPhysical( physDev, color );
1054 XSetForeground( gdi_display, physDev->gc, pixel );
1055 XSetFunction( gdi_display, physDev->gc, GXcopy );
1056 XDrawPoint( gdi_display, physDev->drawable, physDev->gc,
1057 physDev->dc_rect.left + pt.x, physDev->dc_rect.top + pt.y );
1058 wine_tsx11_unlock();
1060 return X11DRV_PALETTE_ToLogical(physDev, pixel);
1064 /***********************************************************************
1067 BOOL X11DRV_PaintRgn( PHYSDEV dev, HRGN hrgn )
1069 X11DRV_PDEVICE *physDev = get_x11drv_dev( dev );
1071 if (X11DRV_SetupGCForBrush( physDev ))
1075 RGNDATA *data = X11DRV_GetRegionData( hrgn, dev->hdc );
1077 if (!data) return FALSE;
1078 rect = (XRectangle *)data->Buffer;
1079 for (i = 0; i < data->rdh.nCount; i++)
1081 rect[i].x += physDev->dc_rect.left;
1082 rect[i].y += physDev->dc_rect.top;
1086 XFillRectangles( gdi_display, physDev->drawable, physDev->gc, rect, data->rdh.nCount );
1087 wine_tsx11_unlock();
1088 HeapFree( GetProcessHeap(), 0, data );
1093 /**********************************************************************
1096 BOOL X11DRV_Polyline( PHYSDEV dev, const POINT* pt, INT count )
1098 X11DRV_PDEVICE *physDev = get_x11drv_dev( dev );
1102 if (!(points = HeapAlloc( GetProcessHeap(), 0, sizeof(XPoint) * count )))
1104 WARN("No memory to convert POINTs to XPoints!\n");
1107 for (i = 0; i < count; i++)
1110 LPtoDP(dev->hdc, &tmp, 1);
1111 points[i].x = physDev->dc_rect.left + tmp.x;
1112 points[i].y = physDev->dc_rect.top + tmp.y;
1115 if (X11DRV_SetupGCForPen ( physDev ))
1118 XDrawLines( gdi_display, physDev->drawable, physDev->gc,
1119 points, count, CoordModeOrigin );
1120 wine_tsx11_unlock();
1123 HeapFree( GetProcessHeap(), 0, points );
1128 /**********************************************************************
1131 BOOL X11DRV_Polygon( PHYSDEV dev, const POINT* pt, INT count )
1133 X11DRV_PDEVICE *physDev = get_x11drv_dev( dev );
1137 if (!(points = HeapAlloc( GetProcessHeap(), 0, sizeof(XPoint) * (count+1) )))
1139 WARN("No memory to convert POINTs to XPoints!\n");
1142 for (i = 0; i < count; i++)
1145 LPtoDP(dev->hdc, &tmp, 1);
1146 points[i].x = physDev->dc_rect.left + tmp.x;
1147 points[i].y = physDev->dc_rect.top + tmp.y;
1149 points[count] = points[0];
1151 if (X11DRV_SetupGCForBrush( physDev ))
1154 XFillPolygon( gdi_display, physDev->drawable, physDev->gc,
1155 points, count+1, Complex, CoordModeOrigin);
1156 wine_tsx11_unlock();
1158 if (X11DRV_SetupGCForPen ( physDev ))
1161 XDrawLines( gdi_display, physDev->drawable, physDev->gc,
1162 points, count+1, CoordModeOrigin );
1163 wine_tsx11_unlock();
1166 HeapFree( GetProcessHeap(), 0, points );
1171 /**********************************************************************
1172 * X11DRV_PolyPolygon
1174 BOOL X11DRV_PolyPolygon( PHYSDEV dev, const POINT* pt, const INT* counts, UINT polygons )
1176 X11DRV_PDEVICE *physDev = get_x11drv_dev( dev );
1179 /* FIXME: The points should be converted to device coords before */
1180 /* creating the region. */
1182 hrgn = CreatePolyPolygonRgn( pt, counts, polygons, GetPolyFillMode( dev->hdc ) );
1183 X11DRV_PaintRgn( dev, hrgn );
1184 DeleteObject( hrgn );
1186 /* Draw the outline of the polygons */
1188 if (X11DRV_SetupGCForPen ( physDev ))
1194 for (i = 0; i < polygons; i++) if (counts[i] > max) max = counts[i];
1195 if (!(points = HeapAlloc( GetProcessHeap(), 0, sizeof(XPoint) * (max+1) )))
1197 WARN("No memory to convert POINTs to XPoints!\n");
1200 for (i = 0; i < polygons; i++)
1202 for (j = 0; j < counts[i]; j++)
1205 LPtoDP(dev->hdc, &tmp, 1);
1206 points[j].x = physDev->dc_rect.left + tmp.x;
1207 points[j].y = physDev->dc_rect.top + tmp.y;
1210 points[j] = points[0];
1212 XDrawLines( gdi_display, physDev->drawable, physDev->gc,
1213 points, j + 1, CoordModeOrigin );
1214 wine_tsx11_unlock();
1216 HeapFree( GetProcessHeap(), 0, points );
1222 /**********************************************************************
1223 * X11DRV_PolyPolyline
1225 BOOL X11DRV_PolyPolyline( PHYSDEV dev, const POINT* pt, const DWORD* counts, DWORD polylines )
1227 X11DRV_PDEVICE *physDev = get_x11drv_dev( dev );
1229 if (X11DRV_SetupGCForPen ( physDev ))
1231 unsigned int i, j, max = 0;
1234 for (i = 0; i < polylines; i++) if (counts[i] > max) max = counts[i];
1235 if (!(points = HeapAlloc( GetProcessHeap(), 0, sizeof(XPoint) * max )))
1237 WARN("No memory to convert POINTs to XPoints!\n");
1240 for (i = 0; i < polylines; i++)
1242 for (j = 0; j < counts[i]; j++)
1245 LPtoDP(dev->hdc, &tmp, 1);
1246 points[j].x = physDev->dc_rect.left + tmp.x;
1247 points[j].y = physDev->dc_rect.top + tmp.y;
1251 XDrawLines( gdi_display, physDev->drawable, physDev->gc,
1252 points, j, CoordModeOrigin );
1253 wine_tsx11_unlock();
1255 HeapFree( GetProcessHeap(), 0, points );
1261 /**********************************************************************
1262 * X11DRV_InternalFloodFill
1264 * Internal helper function for flood fill.
1265 * (xorg,yorg) is the origin of the X image relative to the drawable.
1266 * (x,y) is relative to the origin of the X image.
1268 static void X11DRV_InternalFloodFill(XImage *image, X11DRV_PDEVICE *physDev,
1271 unsigned long pixel, WORD fillType )
1275 #define TO_FLOOD(x,y) ((fillType == FLOODFILLBORDER) ? \
1276 (XGetPixel(image,x,y) != pixel) : \
1277 (XGetPixel(image,x,y) == pixel))
1279 if (!TO_FLOOD(x,y)) return;
1281 /* Find left and right boundaries */
1284 while ((left > 0) && TO_FLOOD( left-1, y )) left--;
1285 while ((right < image->width) && TO_FLOOD( right, y )) right++;
1286 XFillRectangle( gdi_display, physDev->drawable, physDev->gc,
1287 xOrg + left, yOrg + y, right-left, 1 );
1289 /* Set the pixels of this line so we don't fill it again */
1291 for (x = left; x < right; x++)
1293 if (fillType == FLOODFILLBORDER) XPutPixel( image, x, y, pixel );
1294 else XPutPixel( image, x, y, ~pixel );
1297 /* Fill the line above */
1304 while ((x < right) && !TO_FLOOD(x,y)) x++;
1305 if (x >= right) break;
1306 while ((x < right) && TO_FLOOD(x,y)) x++;
1307 X11DRV_InternalFloodFill(image, physDev, x-1, y,
1308 xOrg, yOrg, pixel, fillType );
1312 /* Fill the line below */
1314 if ((y += 2) < image->height)
1319 while ((x < right) && !TO_FLOOD(x,y)) x++;
1320 if (x >= right) break;
1321 while ((x < right) && TO_FLOOD(x,y)) x++;
1322 X11DRV_InternalFloodFill(image, physDev, x-1, y,
1323 xOrg, yOrg, pixel, fillType );
1330 static int ExtFloodFillXGetImageErrorHandler( Display *dpy, XErrorEvent *event, void *arg )
1332 return (event->request_code == X_GetImage && event->error_code == BadMatch);
1335 /**********************************************************************
1336 * X11DRV_ExtFloodFill
1338 BOOL X11DRV_ExtFloodFill( PHYSDEV dev, INT x, INT y, COLORREF color, UINT fillType )
1340 X11DRV_PDEVICE *physDev = get_x11drv_dev( dev );
1345 TRACE("X11DRV_ExtFloodFill %d,%d %06x %d\n", x, y, color, fillType );
1349 LPtoDP( dev->hdc, &pt, 1 );
1351 if (!physDev->region)
1355 rect.right = physDev->dc_rect.right - physDev->dc_rect.left;
1356 rect.bottom = physDev->dc_rect.bottom - physDev->dc_rect.top;
1360 if (!PtInRegion( physDev->region, pt.x, pt.y )) return FALSE;
1361 GetRgnBox( physDev->region, &rect );
1362 rect.left = max( rect.left, 0 );
1363 rect.top = max( rect.top, 0 );
1364 rect.right = min( rect.right, physDev->dc_rect.right - physDev->dc_rect.left );
1365 rect.bottom = min( rect.bottom, physDev->dc_rect.bottom - physDev->dc_rect.top );
1367 if (pt.x < rect.left || pt.x >= rect.right || pt.y < rect.top || pt.y >= rect.bottom) return FALSE;
1369 X11DRV_expect_error( gdi_display, ExtFloodFillXGetImageErrorHandler, NULL );
1370 image = XGetImage( gdi_display, physDev->drawable,
1371 physDev->dc_rect.left + rect.left, physDev->dc_rect.top + rect.top,
1372 rect.right - rect.left, rect.bottom - rect.top,
1373 AllPlanes, ZPixmap );
1374 if(X11DRV_check_error()) image = NULL;
1375 if (!image) return FALSE;
1377 if (X11DRV_SetupGCForBrush( physDev ))
1379 unsigned long pixel = X11DRV_PALETTE_ToPhysical( physDev, color );
1381 /* ROP mode is always GXcopy for flood-fill */
1383 XSetFunction( gdi_display, physDev->gc, GXcopy );
1384 X11DRV_InternalFloodFill(image, physDev,
1387 physDev->dc_rect.left + rect.left,
1388 physDev->dc_rect.top + rect.top,
1390 wine_tsx11_unlock();
1394 XDestroyImage( image );
1395 wine_tsx11_unlock();
1399 /**********************************************************************
1400 * X11DRV_GradientFill
1402 BOOL X11DRV_GradientFill( PHYSDEV dev, TRIVERTEX *vert_array, ULONG nvert,
1403 void *grad_array, ULONG ngrad, ULONG mode )
1405 X11DRV_PDEVICE *physdev = get_x11drv_dev( dev );
1406 const GRADIENT_RECT *rect = grad_array;
1412 /* <= 16-bpp use dithering */
1413 if (physdev->depth <= 16) goto fallback;
1417 case GRADIENT_FILL_RECT_H:
1418 val.function = GXcopy;
1419 val.fill_style = FillSolid;
1421 val.cap_style = CapNotLast;
1422 val.line_style = LineSolid;
1424 XChangeGC( gdi_display, physdev->gc,
1425 GCFunction | GCLineWidth | GCLineStyle | GCCapStyle | GCFillStyle, &val );
1426 wine_tsx11_unlock();
1428 for (i = 0; i < ngrad; i++, rect++)
1432 v[0] = vert_array[rect->UpperLeft];
1433 v[1] = vert_array[rect->LowerRight];
1438 LPtoDP( dev->hdc, pt, 2 );
1439 dx = pt[1].x - pt[0].x;
1441 if (dx < 0) /* swap the colors */
1443 v[0] = vert_array[rect->LowerRight];
1444 v[1] = vert_array[rect->UpperLeft];
1447 for (x = 0, pos = min( pt[0].x, pt[1].x ); x < dx; x++, pos++)
1449 COLORREF color = RGB( (v[0].Red * (dx - x) + v[1].Red * x) / dx / 256,
1450 (v[0].Green * (dx - x) + v[1].Green * x) / dx / 256,
1451 (v[0].Blue * (dx - x) + v[1].Blue * x) / dx / 256);
1453 XSetForeground( gdi_display, physdev->gc, X11DRV_PALETTE_ToPhysical( physdev, color ));
1454 XDrawLine( gdi_display, physdev->drawable, physdev->gc,
1455 physdev->dc_rect.left + pos, physdev->dc_rect.top + pt[0].y,
1456 physdev->dc_rect.left + pos, physdev->dc_rect.top + pt[1].y );
1457 wine_tsx11_unlock();
1462 case GRADIENT_FILL_RECT_V:
1463 val.function = GXcopy;
1464 val.fill_style = FillSolid;
1466 val.cap_style = CapNotLast;
1467 val.line_style = LineSolid;
1469 XChangeGC( gdi_display, physdev->gc,
1470 GCFunction | GCLineWidth | GCLineStyle | GCCapStyle | GCFillStyle, &val );
1471 wine_tsx11_unlock();
1473 for (i = 0; i < ngrad; i++, rect++)
1477 v[0] = vert_array[rect->UpperLeft];
1478 v[1] = vert_array[rect->LowerRight];
1483 LPtoDP( dev->hdc, pt, 2 );
1484 dy = pt[1].y - pt[0].y;
1486 if (dy < 0) /* swap the colors */
1488 v[0] = vert_array[rect->LowerRight];
1489 v[1] = vert_array[rect->UpperLeft];
1492 for (y = 0, pos = min( pt[0].y, pt[1].y ); y < dy; y++, pos++)
1494 COLORREF color = RGB( (v[0].Red * (dy - y) + v[1].Red * y) / dy / 256,
1495 (v[0].Green * (dy - y) + v[1].Green * y) / dy / 256,
1496 (v[0].Blue * (dy - y) + v[1].Blue * y) / dy / 256);
1498 XSetForeground( gdi_display, physdev->gc, X11DRV_PALETTE_ToPhysical( physdev, color ));
1499 XDrawLine( gdi_display, physdev->drawable, physdev->gc,
1500 physdev->dc_rect.left + pt[0].x, physdev->dc_rect.top + pos,
1501 physdev->dc_rect.left + pt[1].x, physdev->dc_rect.top + pos );
1502 wine_tsx11_unlock();
1509 dev = GET_NEXT_PHYSDEV( dev, pGradientFill );
1510 return dev->funcs->pGradientFill( dev, vert_array, nvert, grad_array, ngrad, mode );
1513 static unsigned char *get_icm_profile( unsigned long *size )
1517 unsigned long count, remaining;
1518 unsigned char *profile, *ret = NULL;
1521 XGetWindowProperty( gdi_display, DefaultRootWindow(gdi_display),
1522 x11drv_atom(_ICC_PROFILE), 0, ~0UL, False, AnyPropertyType,
1523 &type, &format, &count, &remaining, &profile );
1524 *size = get_property_size( format, count );
1525 if (format && count)
1527 if ((ret = HeapAlloc( GetProcessHeap(), 0, *size ))) memcpy( ret, profile, *size );
1530 wine_tsx11_unlock();
1536 unsigned int unknown[6];
1537 unsigned int state[5];
1538 unsigned int count[2];
1539 unsigned char buffer[64];
1542 extern void WINAPI A_SHAInit( sha_ctx * );
1543 extern void WINAPI A_SHAUpdate( sha_ctx *, const unsigned char *, unsigned int );
1544 extern void WINAPI A_SHAFinal( sha_ctx *, unsigned char * );
1546 static const WCHAR mntr_key[] =
1547 {'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
1548 'W','i','n','d','o','w','s',' ','N','T','\\','C','u','r','r','e','n','t',
1549 'V','e','r','s','i','o','n','\\','I','C','M','\\','m','n','t','r',0};
1551 static const WCHAR color_path[] =
1552 {'\\','s','p','o','o','l','\\','d','r','i','v','e','r','s','\\','c','o','l','o','r','\\',0};
1554 /***********************************************************************
1555 * GetICMProfile (X11DRV.@)
1557 BOOL X11DRV_GetICMProfile( PHYSDEV dev, LPDWORD size, LPWSTR filename )
1559 static const WCHAR srgb[] =
1560 {'s','R','G','B',' ','C','o','l','o','r',' ','S','p','a','c','e',' ',
1561 'P','r','o','f','i','l','e','.','i','c','m',0};
1563 DWORD required, len;
1564 WCHAR profile[MAX_PATH], fullname[2*MAX_PATH + sizeof(color_path)/sizeof(WCHAR)];
1565 unsigned char *buffer;
1566 unsigned long buflen;
1568 if (!size) return FALSE;
1570 GetSystemDirectoryW( fullname, MAX_PATH );
1571 strcatW( fullname, color_path );
1573 len = sizeof(profile)/sizeof(WCHAR);
1574 if (!RegCreateKeyExW( HKEY_LOCAL_MACHINE, mntr_key, 0, NULL, 0, KEY_ALL_ACCESS, NULL, &hkey, NULL ) &&
1575 !RegEnumValueW( hkey, 0, profile, &len, NULL, NULL, NULL, NULL )) /* FIXME handle multiple values */
1577 strcatW( fullname, profile );
1578 RegCloseKey( hkey );
1580 else if ((buffer = get_icm_profile( &buflen )))
1582 static const WCHAR fmt[] = {'%','0','2','x',0};
1583 static const WCHAR icm[] = {'.','i','c','m',0};
1585 unsigned char sha1sum[20];
1591 A_SHAUpdate( &ctx, buffer, buflen );
1592 A_SHAFinal( &ctx, sha1sum );
1594 for (i = 0; i < sizeof(sha1sum); i++) sprintfW( &profile[i * 2], fmt, sha1sum[i] );
1595 memcpy( &profile[i * 2], icm, sizeof(icm) );
1597 strcatW( fullname, profile );
1598 file = CreateFileW( fullname, GENERIC_WRITE, 0, NULL, CREATE_NEW, 0, 0 );
1599 if (file != INVALID_HANDLE_VALUE)
1603 if (!WriteFile( file, buffer, buflen, &written, NULL ) || written != buflen)
1604 ERR( "Unable to write color profile\n" );
1605 CloseHandle( file );
1607 HeapFree( GetProcessHeap(), 0, buffer );
1609 else strcatW( fullname, srgb );
1611 required = strlenW( fullname ) + 1;
1612 if (*size < required)
1615 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1620 strcpyW( filename, fullname );
1621 if (GetFileAttributesW( filename ) == INVALID_FILE_ATTRIBUTES)
1622 WARN( "color profile not found\n" );
1628 /***********************************************************************
1629 * EnumICMProfiles (X11DRV.@)
1631 INT X11DRV_EnumICMProfiles( PHYSDEV dev, ICMENUMPROCW proc, LPARAM lparam )
1633 X11DRV_PDEVICE *physDev = get_x11drv_dev( dev );
1635 DWORD len_sysdir, len_path, len, index = 0;
1636 WCHAR sysdir[MAX_PATH], *profile;
1640 TRACE("%p, %p, %ld\n", physDev, proc, lparam);
1642 if (RegOpenKeyExW( HKEY_LOCAL_MACHINE, mntr_key, 0, KEY_ALL_ACCESS, &hkey ))
1645 len_sysdir = GetSystemDirectoryW( sysdir, MAX_PATH );
1646 len_path = len_sysdir + sizeof(color_path) / sizeof(color_path[0]) - 1;
1650 if (!(profile = HeapAlloc( GetProcessHeap(), 0, (len_path + len) * sizeof(WCHAR) )))
1652 RegCloseKey( hkey );
1655 res = RegEnumValueW( hkey, index, profile + len_path, &len, NULL, NULL, NULL, NULL );
1656 while (res == ERROR_MORE_DATA)
1659 HeapFree( GetProcessHeap(), 0, profile );
1660 if (!(profile = HeapAlloc( GetProcessHeap(), 0, (len_path + len) * sizeof(WCHAR) )))
1662 RegCloseKey( hkey );
1665 res = RegEnumValueW( hkey, index, profile + len_path, &len, NULL, NULL, NULL, NULL );
1667 if (res != ERROR_SUCCESS)
1669 HeapFree( GetProcessHeap(), 0, profile );
1672 memcpy( profile, sysdir, len_sysdir * sizeof(WCHAR) );
1673 memcpy( profile + len_sysdir, color_path, sizeof(color_path) - sizeof(WCHAR) );
1674 ret = proc( profile, lparam );
1675 HeapFree( GetProcessHeap(), 0, profile );
1679 RegCloseKey( hkey );