cmd: DIR command outputs free space for the path.
[wine.git] / dlls / winex11.drv / graphics.c
blob4a0564392bdb50b72e363117bf5c42e2ee7aa365
1 /*
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
24 * graphics mode
27 #if 0
28 #pragma makedep unix
29 #endif
31 #include "config.h"
33 #include <stdarg.h>
34 #include <math.h>
35 #ifdef HAVE_FLOAT_H
36 # include <float.h>
37 #endif
38 #include <stdlib.h>
39 #ifndef PI
40 #define PI M_PI
41 #endif
42 #include <string.h>
43 #include <limits.h>
45 #include "windef.h"
46 #include "winbase.h"
47 #include "winreg.h"
49 #include "x11drv.h"
50 #include "wine/debug.h"
52 WINE_DEFAULT_DEBUG_CHANNEL(graphics);
54 #define ABS(x) ((x)<0?(-(x)):(x))
56 /* ROP code to GC function conversion */
57 static const int X11DRV_XROPfunction[16] =
59 GXclear, /* R2_BLACK */
60 GXnor, /* R2_NOTMERGEPEN */
61 GXandInverted, /* R2_MASKNOTPEN */
62 GXcopyInverted, /* R2_NOTCOPYPEN */
63 GXandReverse, /* R2_MASKPENNOT */
64 GXinvert, /* R2_NOT */
65 GXxor, /* R2_XORPEN */
66 GXnand, /* R2_NOTMASKPEN */
67 GXand, /* R2_MASKPEN */
68 GXequiv, /* R2_NOTXORPEN */
69 GXnoop, /* R2_NOP */
70 GXorInverted, /* R2_MERGENOTPEN */
71 GXcopy, /* R2_COPYPEN */
72 GXorReverse, /* R2_MERGEPENNOT */
73 GXor, /* R2_MERGEPEN */
74 GXset /* R2_WHITE */
78 /* get the rectangle in device coordinates, with optional mirroring */
79 static RECT get_device_rect( HDC hdc, int left, int top, int right, int bottom )
81 DWORD layout;
82 RECT rect;
84 NtGdiGetDCDword( hdc, NtGdiGetLayout, &layout );
86 rect.left = left;
87 rect.top = top;
88 rect.right = right;
89 rect.bottom = bottom;
90 if (layout & LAYOUT_RTL)
92 /* shift the rectangle so that the right border is included after mirroring */
93 /* it would be more correct to do this after LPtoDP but that's not what Windows does */
94 rect.left--;
95 rect.right--;
97 lp_to_dp( hdc, (POINT *)&rect, 2 );
98 if (rect.left > rect.right)
100 int tmp = rect.left;
101 rect.left = rect.right;
102 rect.right = tmp;
104 if (rect.top > rect.bottom)
106 int tmp = rect.top;
107 rect.top = rect.bottom;
108 rect.bottom = tmp;
110 return rect;
113 static void add_pen_device_bounds( X11DRV_PDEVICE *dev, const POINT *points, int count )
115 RECT bounds, rect;
116 int width = 0;
118 if (!dev->bounds) return;
119 reset_bounds( &bounds );
121 if (dev->pen.type & PS_GEOMETRIC || dev->pen.width > 1)
123 /* Windows uses some heuristics to estimate the distance from the point that will be painted */
124 width = dev->pen.width + 2;
125 if (dev->pen.linejoin == PS_JOIN_MITER)
127 width *= 5;
128 if (dev->pen.endcap == PS_ENDCAP_SQUARE) width = (width * 3 + 1) / 2;
130 else
132 if (dev->pen.endcap == PS_ENDCAP_SQUARE) width -= width / 4;
133 else width = (width + 1) / 2;
137 while (count-- > 0)
139 rect.left = points->x - width;
140 rect.top = points->y - width;
141 rect.right = points->x + width + 1;
142 rect.bottom = points->y + width + 1;
143 add_bounds_rect( &bounds, &rect );
144 points++;
147 add_device_bounds( dev, &bounds );
150 /***********************************************************************
151 * X11DRV_GetRegionData
153 * Calls GetRegionData on the given region and converts the rectangle
154 * array to XRectangle format. The returned buffer must be freed by caller.
155 * If hdc_lptodp is not 0, the rectangles are converted through LPtoDP.
157 RGNDATA *X11DRV_GetRegionData( HRGN hrgn, HDC hdc_lptodp )
159 RGNDATA *data;
160 DWORD size;
161 unsigned int i;
162 RECT *rect, tmp;
163 XRectangle *xrect;
165 if (!(size = NtGdiGetRegionData( hrgn, 0, NULL ))) return NULL;
166 if (sizeof(XRectangle) > sizeof(RECT))
168 /* add extra size for XRectangle array */
169 int count = (size - sizeof(RGNDATAHEADER)) / sizeof(RECT);
170 size += count * (sizeof(XRectangle) - sizeof(RECT));
172 if (!(data = malloc( size ))) return NULL;
173 if (!NtGdiGetRegionData( hrgn, size, data ))
175 free( data );
176 return NULL;
179 rect = (RECT *)data->Buffer;
180 xrect = (XRectangle *)data->Buffer;
181 if (hdc_lptodp) /* map to device coordinates */
183 lp_to_dp( hdc_lptodp, (POINT *)rect, data->rdh.nCount * 2 );
184 for (i = 0; i < data->rdh.nCount; i++)
186 if (rect[i].right < rect[i].left)
188 INT tmp = rect[i].right;
189 rect[i].right = rect[i].left;
190 rect[i].left = tmp;
192 if (rect[i].bottom < rect[i].top)
194 INT tmp = rect[i].bottom;
195 rect[i].bottom = rect[i].top;
196 rect[i].top = tmp;
201 if (sizeof(XRectangle) > sizeof(RECT))
203 int j;
204 /* need to start from the end */
205 xrect += data->rdh.nCount;
206 for (j = data->rdh.nCount-1; j >= 0; j--)
208 tmp = rect[j];
209 if (tmp.left > SHRT_MAX) continue;
210 if (tmp.top > SHRT_MAX) continue;
211 if (tmp.right < SHRT_MIN) continue;
212 if (tmp.bottom < SHRT_MIN) continue;
213 xrect--;
214 xrect->x = max( min( tmp.left, SHRT_MAX), SHRT_MIN);
215 xrect->y = max( min( tmp.top, SHRT_MAX), SHRT_MIN);
216 xrect->width = max( min( tmp.right, SHRT_MAX ) - xrect->x, 0);
217 xrect->height = max( min( tmp.bottom, SHRT_MAX ) - xrect->y, 0);
219 if (xrect > (XRectangle *)data->Buffer)
221 data->rdh.nCount -= xrect - (XRectangle *)data->Buffer;
222 memmove( (XRectangle *)data->Buffer, xrect, data->rdh.nCount * sizeof(*xrect) );
225 else
227 for (i = 0; i < data->rdh.nCount; i++)
229 tmp = rect[i];
230 if (tmp.left > SHRT_MAX) continue;
231 if (tmp.top > SHRT_MAX) continue;
232 if (tmp.right < SHRT_MIN) continue;
233 if (tmp.bottom < SHRT_MIN) continue;
234 xrect->x = max( min( tmp.left, SHRT_MAX), SHRT_MIN);
235 xrect->y = max( min( tmp.top, SHRT_MAX), SHRT_MIN);
236 xrect->width = max( min( tmp.right, SHRT_MAX ) - xrect->x, 0);
237 xrect->height = max( min( tmp.bottom, SHRT_MAX ) - xrect->y, 0);
238 xrect++;
240 data->rdh.nCount = xrect - (XRectangle *)data->Buffer;
242 return data;
246 /***********************************************************************
247 * update_x11_clipping
249 static void update_x11_clipping( X11DRV_PDEVICE *physDev, HRGN rgn )
251 RGNDATA *data;
253 if (!rgn)
255 XSetClipMask( gdi_display, physDev->gc, None );
257 else if ((data = X11DRV_GetRegionData( rgn, 0 )))
259 XSetClipRectangles( gdi_display, physDev->gc, physDev->dc_rect.left, physDev->dc_rect.top,
260 (XRectangle *)data->Buffer, data->rdh.nCount, YXBanded );
261 free( data );
266 /***********************************************************************
267 * add_extra_clipping_region
269 * Temporarily add a region to the current clipping region.
270 * The region must be restored with restore_clipping_region.
272 BOOL add_extra_clipping_region( X11DRV_PDEVICE *dev, HRGN rgn )
274 HRGN clip;
276 if (!rgn) return FALSE;
277 if (dev->region)
279 if (!(clip = NtGdiCreateRectRgn( 0, 0, 0, 0 ))) return FALSE;
280 NtGdiCombineRgn( clip, dev->region, rgn, RGN_AND );
281 update_x11_clipping( dev, clip );
282 NtGdiDeleteObjectApp( clip );
284 else update_x11_clipping( dev, rgn );
285 return TRUE;
289 /***********************************************************************
290 * restore_clipping_region
292 void restore_clipping_region( X11DRV_PDEVICE *dev )
294 update_x11_clipping( dev, dev->region );
298 /***********************************************************************
299 * X11DRV_SetDeviceClipping
301 void X11DRV_SetDeviceClipping( PHYSDEV dev, HRGN rgn )
303 X11DRV_PDEVICE *physDev = get_x11drv_dev( dev );
305 physDev->region = rgn;
306 update_x11_clipping( physDev, rgn );
310 /***********************************************************************
311 * X11DRV_SetupGCForPatBlt
313 * Setup the GC for a PatBlt operation using current brush.
314 * If fMapColors is TRUE, X pixels are mapped to Windows colors.
315 * Return FALSE if brush is BS_NULL, TRUE otherwise.
317 BOOL X11DRV_SetupGCForPatBlt( X11DRV_PDEVICE *physDev, GC gc, BOOL fMapColors )
319 DWORD bk_color, text_color, rop_mode, bk_mode, poly_fill_mode;
320 XGCValues val;
321 unsigned long mask;
322 Pixmap pixmap = 0;
323 POINT pt;
325 if (physDev->brush.style == BS_NULL) return FALSE;
327 NtGdiGetDCDword( physDev->dev.hdc, NtGdiGetBkColor, &bk_color );
328 NtGdiGetDCDword( physDev->dev.hdc, NtGdiGetROP2, &rop_mode );
330 if (physDev->brush.pixel == -1)
332 /* Special case used for monochrome pattern brushes.
333 * We need to swap foreground and background because
334 * Windows does it the wrong way...
336 NtGdiGetDCDword( physDev->dev.hdc, NtGdiGetTextColor, &text_color );
337 val.foreground = X11DRV_PALETTE_ToPhysical( physDev, bk_color );
338 val.background = X11DRV_PALETTE_ToPhysical( physDev, text_color );
340 else
342 val.foreground = physDev->brush.pixel;
343 val.background = X11DRV_PALETTE_ToPhysical( physDev, bk_color );
345 if (fMapColors && X11DRV_PALETTE_XPixelToPalette)
347 val.foreground = X11DRV_PALETTE_XPixelToPalette[val.foreground];
348 val.background = X11DRV_PALETTE_XPixelToPalette[val.background];
351 val.function = X11DRV_XROPfunction[rop_mode - 1];
353 ** Let's replace GXinvert by GXxor with (black xor white)
354 ** This solves the selection color and leak problems in excel
355 ** FIXME : Let's do that only if we work with X-pixels, not with Win-pixels
357 if (val.function == GXinvert)
359 val.foreground = (WhitePixel( gdi_display, DefaultScreen(gdi_display) ) ^
360 BlackPixel( gdi_display, DefaultScreen(gdi_display) ));
361 val.function = GXxor;
363 val.fill_style = physDev->brush.fillStyle;
364 switch(val.fill_style)
366 case FillStippled:
367 case FillOpaqueStippled:
368 NtGdiGetDCDword( physDev->dev.hdc, NtGdiGetBkMode, &bk_mode );
369 if (bk_mode == OPAQUE) val.fill_style = FillOpaqueStippled;
370 val.stipple = physDev->brush.pixmap;
371 mask = GCStipple;
372 break;
374 case FillTiled:
375 if (fMapColors && X11DRV_PALETTE_XPixelToPalette)
377 register int x, y;
378 XImage *image;
379 pixmap = XCreatePixmap( gdi_display, root_window, 8, 8, physDev->depth );
380 image = XGetImage( gdi_display, physDev->brush.pixmap, 0, 0, 8, 8,
381 AllPlanes, ZPixmap );
382 for (y = 0; y < 8; y++)
383 for (x = 0; x < 8; x++)
384 XPutPixel( image, x, y,
385 X11DRV_PALETTE_XPixelToPalette[XGetPixel( image, x, y)] );
386 XPutImage( gdi_display, pixmap, gc, image, 0, 0, 0, 0, 8, 8 );
387 XDestroyImage( image );
388 val.tile = pixmap;
390 else val.tile = physDev->brush.pixmap;
391 mask = GCTile;
392 break;
394 default:
395 mask = 0;
396 break;
399 NtGdiGetDCPoint( physDev->dev.hdc, NtGdiGetBrushOrgEx, &pt );
400 NtGdiGetDCDword( physDev->dev.hdc, NtGdiGetPolyFillMode, &poly_fill_mode );
402 val.ts_x_origin = physDev->dc_rect.left + pt.x;
403 val.ts_y_origin = physDev->dc_rect.top + pt.y;
404 val.fill_rule = poly_fill_mode == WINDING ? WindingRule : EvenOddRule;
405 XChangeGC( gdi_display, gc,
406 GCFunction | GCForeground | GCBackground | GCFillStyle |
407 GCFillRule | GCTileStipXOrigin | GCTileStipYOrigin | mask,
408 &val );
409 if (pixmap) XFreePixmap( gdi_display, pixmap );
410 return TRUE;
414 /***********************************************************************
415 * X11DRV_SetupGCForBrush
417 * Setup physDev->gc for drawing operations using current brush.
418 * Return FALSE if brush is BS_NULL, TRUE otherwise.
420 BOOL X11DRV_SetupGCForBrush( X11DRV_PDEVICE *physDev )
422 return X11DRV_SetupGCForPatBlt( physDev, physDev->gc, FALSE );
426 /***********************************************************************
427 * X11DRV_SetupGCForPen
429 * Setup physDev->gc for drawing operations using current pen.
430 * Return FALSE if pen is PS_NULL, TRUE otherwise.
432 static BOOL X11DRV_SetupGCForPen( X11DRV_PDEVICE *physDev )
434 DWORD rop2, bk_color, bk_mode;
435 XGCValues val;
437 NtGdiGetDCDword( physDev->dev.hdc, NtGdiGetROP2, &rop2 );
439 if (physDev->pen.style == PS_NULL) return FALSE;
441 switch (rop2)
443 case R2_BLACK :
444 val.foreground = BlackPixel( gdi_display, DefaultScreen(gdi_display) );
445 val.function = GXcopy;
446 break;
447 case R2_WHITE :
448 val.foreground = WhitePixel( gdi_display, DefaultScreen(gdi_display) );
449 val.function = GXcopy;
450 break;
451 case R2_XORPEN :
452 val.foreground = physDev->pen.pixel;
453 /* It is very unlikely someone wants to XOR with 0 */
454 /* This fixes the rubber-drawings in paintbrush */
455 if (val.foreground == 0)
456 val.foreground = (WhitePixel( gdi_display, DefaultScreen(gdi_display) ) ^
457 BlackPixel( gdi_display, DefaultScreen(gdi_display) ));
458 val.function = GXxor;
459 break;
460 default :
461 val.foreground = physDev->pen.pixel;
462 val.function = X11DRV_XROPfunction[rop2-1];
464 NtGdiGetDCDword( physDev->dev.hdc, NtGdiGetBkColor, &bk_color );
465 val.background = X11DRV_PALETTE_ToPhysical( physDev, bk_color );
466 val.fill_style = FillSolid;
467 val.line_width = physDev->pen.width;
468 if (val.line_width <= 1) {
469 val.cap_style = CapNotLast;
470 } else {
471 switch (physDev->pen.endcap)
473 case PS_ENDCAP_SQUARE:
474 val.cap_style = CapProjecting;
475 break;
476 case PS_ENDCAP_FLAT:
477 val.cap_style = CapButt;
478 break;
479 case PS_ENDCAP_ROUND:
480 default:
481 val.cap_style = CapRound;
484 switch (physDev->pen.linejoin)
486 case PS_JOIN_BEVEL:
487 val.join_style = JoinBevel;
488 break;
489 case PS_JOIN_MITER:
490 val.join_style = JoinMiter;
491 break;
492 case PS_JOIN_ROUND:
493 default:
494 val.join_style = JoinRound;
497 if (physDev->pen.dash_len)
498 val.line_style = (NtGdiGetDCDword( physDev->dev.hdc, NtGdiGetBkMode, &bk_mode) &&
499 bk_mode == OPAQUE && !physDev->pen.ext)
500 ? LineDoubleDash : LineOnOffDash;
501 else
502 val.line_style = LineSolid;
504 if (physDev->pen.dash_len)
505 XSetDashes( gdi_display, physDev->gc, 0, physDev->pen.dashes, physDev->pen.dash_len );
506 XChangeGC( gdi_display, physDev->gc,
507 GCFunction | GCForeground | GCBackground | GCLineWidth |
508 GCLineStyle | GCCapStyle | GCJoinStyle | GCFillStyle, &val );
509 return TRUE;
513 /***********************************************************************
514 * X11DRV_XWStoDS
516 * Performs a world-to-viewport transformation on the specified width.
518 INT X11DRV_XWStoDS( HDC hdc, INT width )
520 POINT pt[2];
522 pt[0].x = 0;
523 pt[0].y = 0;
524 pt[1].x = width;
525 pt[1].y = 0;
526 lp_to_dp( hdc, pt, 2 );
527 return pt[1].x - pt[0].x;
530 /***********************************************************************
531 * X11DRV_YWStoDS
533 * Performs a world-to-viewport transformation on the specified height.
535 INT X11DRV_YWStoDS( HDC hdc, INT height )
537 POINT pt[2];
539 pt[0].x = 0;
540 pt[0].y = 0;
541 pt[1].x = 0;
542 pt[1].y = height;
543 lp_to_dp( hdc, pt, 2 );
544 return pt[1].y - pt[0].y;
547 /***********************************************************************
548 * X11DRV_LineTo
550 BOOL X11DRV_LineTo( PHYSDEV dev, INT x, INT y )
552 X11DRV_PDEVICE *physDev = get_x11drv_dev( dev );
553 POINT pt[2];
555 NtGdiGetDCPoint( dev->hdc, NtGdiGetCurrentPosition, &pt[0] );
556 pt[1].x = x;
557 pt[1].y = y;
558 lp_to_dp( dev->hdc, pt, 2 );
559 add_pen_device_bounds( physDev, pt, 2 );
561 if (X11DRV_SetupGCForPen( physDev ))
562 XDrawLine(gdi_display, physDev->drawable, physDev->gc,
563 physDev->dc_rect.left + pt[0].x, physDev->dc_rect.top + pt[0].y,
564 physDev->dc_rect.left + pt[1].x, physDev->dc_rect.top + pt[1].y );
565 return TRUE;
570 /***********************************************************************
571 * X11DRV_DrawArc
573 * Helper functions for Arc(), Chord() and Pie().
574 * 'lines' is the number of lines to draw: 0 for Arc, 1 for Chord, 2 for Pie.
577 static BOOL X11DRV_DrawArc( PHYSDEV dev, INT left, INT top, INT right, INT bottom,
578 INT xstart, INT ystart, INT xend, INT yend, INT lines )
580 X11DRV_PDEVICE *physDev = get_x11drv_dev( dev );
581 INT xcenter, ycenter, istart_angle, idiff_angle;
582 INT width, oldwidth;
583 DWORD arc_dir;
584 double start_angle, end_angle;
585 XPoint points[4];
586 POINT start, end;
587 RECT rc = get_device_rect( dev->hdc, left, top, right, bottom );
589 start.x = xstart;
590 start.y = ystart;
591 end.x = xend;
592 end.y = yend;
593 lp_to_dp(dev->hdc, &start, 1);
594 lp_to_dp(dev->hdc, &end, 1);
596 if ((rc.left == rc.right) || (rc.top == rc.bottom)
597 ||(lines && ((rc.right-rc.left==1)||(rc.bottom-rc.top==1)))) return TRUE;
599 if (NtGdiGetDCDword( dev->hdc, NtGdiGetArcDirection, &arc_dir ) && arc_dir == AD_CLOCKWISE)
600 { POINT tmp = start; start = end; end = tmp; }
602 oldwidth = width = physDev->pen.width;
603 if (!width) width = 1;
604 if(physDev->pen.style == PS_NULL) width = 0;
606 if (physDev->pen.style == PS_INSIDEFRAME)
608 if (2*width > (rc.right-rc.left)) width=(rc.right-rc.left + 1)/2;
609 if (2*width > (rc.bottom-rc.top)) width=(rc.bottom-rc.top + 1)/2;
610 rc.left += width / 2;
611 rc.right -= (width - 1) / 2;
612 rc.top += width / 2;
613 rc.bottom -= (width - 1) / 2;
615 if(width == 0) width = 1; /* more accurate */
616 physDev->pen.width = width;
618 xcenter = (rc.right + rc.left) / 2;
619 ycenter = (rc.bottom + rc.top) / 2;
620 start_angle = atan2( (double)(ycenter-start.y)*(rc.right-rc.left),
621 (double)(start.x-xcenter)*(rc.bottom-rc.top) );
622 end_angle = atan2( (double)(ycenter-end.y)*(rc.right-rc.left),
623 (double)(end.x-xcenter)*(rc.bottom-rc.top) );
624 if ((start.x==end.x)&&(start.y==end.y))
625 { /* A lazy program delivers xstart=xend=ystart=yend=0) */
626 start_angle = 0;
627 end_angle = 2* PI;
629 else /* notorious cases */
630 if ((start_angle == PI)&&( end_angle <0))
631 start_angle = - PI;
632 else
633 if ((end_angle == PI)&&( start_angle <0))
634 end_angle = - PI;
635 istart_angle = (INT)(start_angle * 180 * 64 / PI + 0.5);
636 idiff_angle = (INT)((end_angle - start_angle) * 180 * 64 / PI + 0.5);
637 if (idiff_angle <= 0) idiff_angle += 360 * 64;
639 /* Fill arc with brush if Chord() or Pie() */
641 if ((lines > 0) && X11DRV_SetupGCForBrush( physDev )) {
642 XSetArcMode( gdi_display, physDev->gc, (lines==1) ? ArcChord : ArcPieSlice);
643 XFillArc( gdi_display, physDev->drawable, physDev->gc,
644 physDev->dc_rect.left + rc.left, physDev->dc_rect.top + rc.top,
645 rc.right-rc.left-1, rc.bottom-rc.top-1, istart_angle, idiff_angle );
648 /* Draw arc and lines */
650 if (X11DRV_SetupGCForPen( physDev ))
652 XDrawArc( gdi_display, physDev->drawable, physDev->gc,
653 physDev->dc_rect.left + rc.left, physDev->dc_rect.top + rc.top,
654 rc.right-rc.left-1, rc.bottom-rc.top-1, istart_angle, idiff_angle );
655 if (lines) {
656 /* use the truncated values */
657 start_angle=(double)istart_angle*PI/64./180.;
658 end_angle=(double)(istart_angle+idiff_angle)*PI/64./180.;
659 /* calculate the endpoints and round correctly */
660 points[0].x = (int) floor(physDev->dc_rect.left + (rc.right+rc.left)/2.0 +
661 cos(start_angle) * (rc.right-rc.left-width*2+2) / 2. + 0.5);
662 points[0].y = (int) floor(physDev->dc_rect.top + (rc.top+rc.bottom)/2.0 -
663 sin(start_angle) * (rc.bottom-rc.top-width*2+2) / 2. + 0.5);
664 points[1].x = (int) floor(physDev->dc_rect.left + (rc.right+rc.left)/2.0 +
665 cos(end_angle) * (rc.right-rc.left-width*2+2) / 2. + 0.5);
666 points[1].y = (int) floor(physDev->dc_rect.top + (rc.top+rc.bottom)/2.0 -
667 sin(end_angle) * (rc.bottom-rc.top-width*2+2) / 2. + 0.5);
669 /* OK, this stuff is optimized for Xfree86
670 * which is probably the server most used by
671 * wine users. Other X servers will not
672 * display correctly. (eXceed for instance)
673 * so if you feel you must make changes, make sure that
674 * you either use Xfree86 or separate your changes
675 * from these (compile switch or whatever)
677 if (lines == 2) {
678 INT dx1,dy1;
679 points[3] = points[1];
680 points[1].x = physDev->dc_rect.left + xcenter;
681 points[1].y = physDev->dc_rect.top + ycenter;
682 points[2] = points[1];
683 dx1=points[1].x-points[0].x;
684 dy1=points[1].y-points[0].y;
685 if(((rc.top-rc.bottom) | -2) == -2)
686 if(dy1>0) points[1].y--;
687 if(dx1<0) {
688 if (((-dx1)*64)<=ABS(dy1)*37) points[0].x--;
689 if(((-dx1*9))<(dy1*16)) points[0].y--;
690 if( dy1<0 && ((dx1*9)) < (dy1*16)) points[0].y--;
691 } else {
692 if(dy1 < 0) points[0].y--;
693 if(((rc.right-rc.left) | -2) == -2) points[1].x--;
695 dx1=points[3].x-points[2].x;
696 dy1=points[3].y-points[2].y;
697 if(((rc.top-rc.bottom) | -2 ) == -2)
698 if(dy1 < 0) points[2].y--;
699 if( dx1<0){
700 if( dy1>0) points[3].y--;
701 if(((rc.right-rc.left) | -2) == -2 ) points[2].x--;
702 }else {
703 points[3].y--;
704 if( dx1 * 64 < dy1 * -37 ) points[3].x--;
706 lines++;
708 XDrawLines( gdi_display, physDev->drawable, physDev->gc,
709 points, lines+1, CoordModeOrigin );
713 physDev->pen.width = oldwidth;
714 add_pen_device_bounds( physDev, (POINT *)&rc, 2 );
715 return TRUE;
719 /***********************************************************************
720 * X11DRV_Arc
722 BOOL X11DRV_Arc( 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, 0 );
729 /***********************************************************************
730 * X11DRV_Pie
732 BOOL X11DRV_Pie( PHYSDEV dev, INT left, INT top, INT right, INT bottom,
733 INT xstart, INT ystart, INT xend, INT yend )
735 return X11DRV_DrawArc( dev, left, top, right, bottom, xstart, ystart, xend, yend, 2 );
738 /***********************************************************************
739 * X11DRV_Chord
741 BOOL X11DRV_Chord( PHYSDEV dev, INT left, INT top, INT right, INT bottom,
742 INT xstart, INT ystart, INT xend, INT yend )
744 return X11DRV_DrawArc( dev, left, top, right, bottom, xstart, ystart, xend, yend, 1 );
748 /***********************************************************************
749 * X11DRV_Ellipse
751 BOOL X11DRV_Ellipse( PHYSDEV dev, INT left, INT top, INT right, INT bottom )
753 X11DRV_PDEVICE *physDev = get_x11drv_dev( dev );
754 INT width, oldwidth;
755 RECT rc = get_device_rect( dev->hdc, left, top, right, bottom );
757 if ((rc.left == rc.right) || (rc.top == rc.bottom)) return TRUE;
759 oldwidth = width = physDev->pen.width;
760 if (!width) width = 1;
761 if(physDev->pen.style == PS_NULL) width = 0;
763 if (physDev->pen.style == PS_INSIDEFRAME)
765 if (2*width > (rc.right-rc.left)) width=(rc.right-rc.left + 1)/2;
766 if (2*width > (rc.bottom-rc.top)) width=(rc.bottom-rc.top + 1)/2;
767 rc.left += width / 2;
768 rc.right -= (width - 1) / 2;
769 rc.top += width / 2;
770 rc.bottom -= (width - 1) / 2;
772 if(width == 0) width = 1; /* more accurate */
773 physDev->pen.width = width;
775 if (X11DRV_SetupGCForBrush( physDev ))
776 XFillArc( 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 );
780 if (X11DRV_SetupGCForPen( physDev ))
781 XDrawArc( gdi_display, physDev->drawable, physDev->gc,
782 physDev->dc_rect.left + rc.left, physDev->dc_rect.top + rc.top,
783 rc.right-rc.left-1, rc.bottom-rc.top-1, 0, 360*64 );
785 physDev->pen.width = oldwidth;
786 add_pen_device_bounds( physDev, (POINT *)&rc, 2 );
787 return TRUE;
791 /***********************************************************************
792 * X11DRV_Rectangle
794 BOOL X11DRV_Rectangle(PHYSDEV dev, INT left, INT top, INT right, INT bottom)
796 X11DRV_PDEVICE *physDev = get_x11drv_dev( dev );
797 INT width, oldwidth, oldjoinstyle;
798 RECT rc = get_device_rect( dev->hdc, left, top, right, bottom );
800 TRACE("(%d %d %d %d)\n", left, top, right, bottom);
802 if ((rc.left == rc.right) || (rc.top == rc.bottom)) return TRUE;
804 oldwidth = width = physDev->pen.width;
805 if (!width) width = 1;
806 if(physDev->pen.style == PS_NULL) width = 0;
808 if (physDev->pen.style == PS_INSIDEFRAME)
810 if (2*width > (rc.right-rc.left)) width=(rc.right-rc.left + 1)/2;
811 if (2*width > (rc.bottom-rc.top)) width=(rc.bottom-rc.top + 1)/2;
812 rc.left += width / 2;
813 rc.right -= (width - 1) / 2;
814 rc.top += width / 2;
815 rc.bottom -= (width - 1) / 2;
817 if(width == 1) width = 0;
818 physDev->pen.width = width;
819 oldjoinstyle = physDev->pen.linejoin;
820 if(physDev->pen.type != PS_GEOMETRIC)
821 physDev->pen.linejoin = PS_JOIN_MITER;
823 rc.right--;
824 rc.bottom--;
825 if ((rc.right >= rc.left + width) && (rc.bottom >= rc.top + width))
827 if (X11DRV_SetupGCForBrush( physDev ))
828 XFillRectangle( gdi_display, physDev->drawable, physDev->gc,
829 physDev->dc_rect.left + rc.left + (width + 1) / 2,
830 physDev->dc_rect.top + rc.top + (width + 1) / 2,
831 rc.right-rc.left-width, rc.bottom-rc.top-width);
833 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, rc.bottom-rc.top );
838 physDev->pen.width = oldwidth;
839 physDev->pen.linejoin = oldjoinstyle;
840 add_pen_device_bounds( physDev, (POINT *)&rc, 2 );
841 return TRUE;
844 /***********************************************************************
845 * X11DRV_RoundRect
847 BOOL X11DRV_RoundRect( PHYSDEV dev, INT left, INT top, INT right, INT bottom,
848 INT ell_width, INT ell_height )
850 X11DRV_PDEVICE *physDev = get_x11drv_dev( dev );
851 INT width, oldwidth, oldendcap;
852 POINT pts[2];
853 RECT rc = get_device_rect( dev->hdc, left, top, right, bottom );
855 TRACE("(%d %d %d %d %d %d\n",
856 left, top, right, bottom, ell_width, ell_height);
858 if ((rc.left == rc.right) || (rc.top == rc.bottom))
859 return TRUE;
861 /* Make sure ell_width and ell_height are >= 1 otherwise XDrawArc gets
862 called with width/height < 0 */
863 pts[0].x = pts[0].y = 0;
864 pts[1].x = ell_width;
865 pts[1].y = ell_height;
866 lp_to_dp(dev->hdc, pts, 2);
867 ell_width = max(abs( pts[1].x - pts[0].x ), 1);
868 ell_height = max(abs( pts[1].y - pts[0].y ), 1);
870 oldwidth = width = physDev->pen.width;
871 oldendcap = physDev->pen.endcap;
872 if (!width) width = 1;
873 if(physDev->pen.style == PS_NULL) width = 0;
875 if (physDev->pen.style == PS_INSIDEFRAME)
877 if (2*width > (rc.right-rc.left)) width=(rc.right-rc.left + 1)/2;
878 if (2*width > (rc.bottom-rc.top)) width=(rc.bottom-rc.top + 1)/2;
879 rc.left += width / 2;
880 rc.right -= (width - 1) / 2;
881 rc.top += width / 2;
882 rc.bottom -= (width - 1) / 2;
884 if(width == 0) width = 1;
885 physDev->pen.width = width;
886 physDev->pen.endcap = PS_ENDCAP_SQUARE;
888 if (X11DRV_SetupGCForBrush( physDev ))
890 if (ell_width > (rc.right-rc.left) )
891 if (ell_height > (rc.bottom-rc.top) )
892 XFillArc( gdi_display, physDev->drawable, physDev->gc,
893 physDev->dc_rect.left + rc.left, physDev->dc_rect.top + rc.top,
894 rc.right - rc.left - 1, rc.bottom - rc.top - 1,
895 0, 360 * 64 );
896 else{
897 XFillArc( gdi_display, physDev->drawable, physDev->gc,
898 physDev->dc_rect.left + rc.left, physDev->dc_rect.top + rc.top,
899 rc.right - rc.left - 1, ell_height, 0, 180 * 64 );
900 XFillArc( gdi_display, physDev->drawable, physDev->gc,
901 physDev->dc_rect.left + rc.left,
902 physDev->dc_rect.top + rc.bottom - ell_height - 1,
903 rc.right - rc.left - 1, ell_height, 180 * 64,
904 180 * 64 );
906 else if (ell_height > (rc.bottom-rc.top) ){
907 XFillArc( gdi_display, physDev->drawable, physDev->gc,
908 physDev->dc_rect.left + rc.left, physDev->dc_rect.top + rc.top,
909 ell_width, rc.bottom - rc.top - 1, 90 * 64, 180 * 64 );
910 XFillArc( gdi_display, physDev->drawable, physDev->gc,
911 physDev->dc_rect.left + rc.right - ell_width - 1, physDev->dc_rect.top + rc.top,
912 ell_width, rc.bottom - rc.top - 1, 270 * 64, 180 * 64 );
913 }else{
914 XFillArc( gdi_display, physDev->drawable, physDev->gc,
915 physDev->dc_rect.left + rc.left, physDev->dc_rect.top + rc.top,
916 ell_width, ell_height, 90 * 64, 90 * 64 );
917 XFillArc( gdi_display, physDev->drawable, physDev->gc,
918 physDev->dc_rect.left + rc.left,
919 physDev->dc_rect.top + rc.bottom - ell_height - 1,
920 ell_width, ell_height, 180 * 64, 90 * 64 );
921 XFillArc( gdi_display, physDev->drawable, physDev->gc,
922 physDev->dc_rect.left + rc.right - ell_width - 1,
923 physDev->dc_rect.top + rc.bottom - ell_height - 1,
924 ell_width, ell_height, 270 * 64, 90 * 64 );
925 XFillArc( gdi_display, physDev->drawable, physDev->gc,
926 physDev->dc_rect.left + rc.right - ell_width - 1,
927 physDev->dc_rect.top + rc.top,
928 ell_width, ell_height, 0, 90 * 64 );
930 if (ell_width < rc.right - rc.left)
932 XFillRectangle( gdi_display, physDev->drawable, physDev->gc,
933 physDev->dc_rect.left + rc.left + (ell_width + 1) / 2,
934 physDev->dc_rect.top + rc.top + 1,
935 rc.right - rc.left - ell_width - 1,
936 (ell_height + 1) / 2 - 1);
937 XFillRectangle( gdi_display, physDev->drawable, physDev->gc,
938 physDev->dc_rect.left + rc.left + (ell_width + 1) / 2,
939 physDev->dc_rect.top + rc.bottom - (ell_height) / 2 - 1,
940 rc.right - rc.left - ell_width - 1,
941 (ell_height) / 2 );
943 if (ell_height < rc.bottom - rc.top)
945 XFillRectangle( gdi_display, physDev->drawable, physDev->gc,
946 physDev->dc_rect.left + rc.left + 1,
947 physDev->dc_rect.top + rc.top + (ell_height + 1) / 2,
948 rc.right - rc.left - 2,
949 rc.bottom - rc.top - ell_height - 1);
952 /* FIXME: this could be done with on X call
953 * more efficient and probably more correct
954 * on any X server: XDrawArcs will draw
955 * straight horizontal and vertical lines
956 * if width or height are zero.
958 * BTW this stuff is optimized for an Xfree86 server
959 * read the comments inside the X11DRV_DrawArc function
961 if (X11DRV_SetupGCForPen( physDev ))
963 if (ell_width > (rc.right-rc.left) )
964 if (ell_height > (rc.bottom-rc.top) )
965 XDrawArc( gdi_display, physDev->drawable, physDev->gc,
966 physDev->dc_rect.left + rc.left, physDev->dc_rect.top + rc.top,
967 rc.right - rc.left - 1, rc.bottom - rc.top - 1, 0 , 360 * 64 );
968 else{
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, ell_height - 1, 0 , 180 * 64 );
972 XDrawArc( gdi_display, physDev->drawable, physDev->gc,
973 physDev->dc_rect.left + rc.left,
974 physDev->dc_rect.top + rc.bottom - ell_height,
975 rc.right - rc.left - 1, ell_height - 1, 180 * 64 , 180 * 64 );
977 else if (ell_height > (rc.bottom-rc.top) ){
978 XDrawArc( gdi_display, physDev->drawable, physDev->gc,
979 physDev->dc_rect.left + rc.left, physDev->dc_rect.top + rc.top,
980 ell_width - 1 , rc.bottom - rc.top - 1, 90 * 64 , 180 * 64 );
981 XDrawArc( gdi_display, physDev->drawable, physDev->gc,
982 physDev->dc_rect.left + rc.right - ell_width,
983 physDev->dc_rect.top + rc.top,
984 ell_width - 1 , rc.bottom - rc.top - 1, 270 * 64 , 180 * 64 );
985 }else{
986 XDrawArc( gdi_display, physDev->drawable, physDev->gc,
987 physDev->dc_rect.left + rc.left, physDev->dc_rect.top + rc.top,
988 ell_width - 1, ell_height - 1, 90 * 64, 90 * 64 );
989 XDrawArc( gdi_display, physDev->drawable, physDev->gc,
990 physDev->dc_rect.left + rc.left, physDev->dc_rect.top + rc.bottom - ell_height,
991 ell_width - 1, ell_height - 1, 180 * 64, 90 * 64 );
992 XDrawArc( gdi_display, physDev->drawable, physDev->gc,
993 physDev->dc_rect.left + rc.right - ell_width,
994 physDev->dc_rect.top + rc.bottom - ell_height,
995 ell_width - 1, ell_height - 1, 270 * 64, 90 * 64 );
996 XDrawArc( gdi_display, physDev->drawable, physDev->gc,
997 physDev->dc_rect.left + rc.right - ell_width, physDev->dc_rect.top + rc.top,
998 ell_width - 1, ell_height - 1, 0, 90 * 64 );
1000 if (ell_width < rc.right - rc.left)
1002 XDrawLine( gdi_display, physDev->drawable, physDev->gc,
1003 physDev->dc_rect.left + rc.left + ell_width / 2,
1004 physDev->dc_rect.top + rc.top,
1005 physDev->dc_rect.left + rc.right - (ell_width+1) / 2,
1006 physDev->dc_rect.top + rc.top);
1007 XDrawLine( gdi_display, physDev->drawable, physDev->gc,
1008 physDev->dc_rect.left + rc.left + ell_width / 2 ,
1009 physDev->dc_rect.top + rc.bottom - 1,
1010 physDev->dc_rect.left + rc.right - (ell_width+1)/ 2,
1011 physDev->dc_rect.top + rc.bottom - 1);
1013 if (ell_height < rc.bottom - rc.top)
1015 XDrawLine( gdi_display, physDev->drawable, physDev->gc,
1016 physDev->dc_rect.left + rc.right - 1,
1017 physDev->dc_rect.top + rc.top + ell_height / 2,
1018 physDev->dc_rect.left + rc.right - 1,
1019 physDev->dc_rect.top + rc.bottom - (ell_height+1) / 2);
1020 XDrawLine( gdi_display, physDev->drawable, physDev->gc,
1021 physDev->dc_rect.left + rc.left,
1022 physDev->dc_rect.top + rc.top + ell_height / 2,
1023 physDev->dc_rect.left + rc.left,
1024 physDev->dc_rect.top + rc.bottom - (ell_height+1) / 2);
1028 physDev->pen.width = oldwidth;
1029 physDev->pen.endcap = oldendcap;
1030 add_pen_device_bounds( physDev, (POINT *)&rc, 2 );
1031 return TRUE;
1035 /***********************************************************************
1036 * X11DRV_SetPixel
1038 COLORREF X11DRV_SetPixel( PHYSDEV dev, INT x, INT y, COLORREF color )
1040 X11DRV_PDEVICE *physDev = get_x11drv_dev( dev );
1041 unsigned long pixel;
1042 POINT pt;
1043 RECT rect;
1045 pt.x = x;
1046 pt.y = y;
1047 lp_to_dp( dev->hdc, &pt, 1 );
1048 pixel = X11DRV_PALETTE_ToPhysical( physDev, color );
1050 XSetForeground( gdi_display, physDev->gc, pixel );
1051 XSetFunction( gdi_display, physDev->gc, GXcopy );
1052 XDrawPoint( gdi_display, physDev->drawable, physDev->gc,
1053 physDev->dc_rect.left + pt.x, physDev->dc_rect.top + pt.y );
1055 SetRect( &rect, pt.x, pt.y, pt.x + 1, pt.y + 1 );
1056 add_device_bounds( physDev, &rect );
1057 return X11DRV_PALETTE_ToLogical(physDev, pixel);
1061 /***********************************************************************
1062 * X11DRV_PaintRgn
1064 BOOL X11DRV_PaintRgn( PHYSDEV dev, HRGN hrgn )
1066 X11DRV_PDEVICE *physDev = get_x11drv_dev( dev );
1067 RECT rc;
1069 if (X11DRV_SetupGCForBrush( physDev ))
1071 unsigned int i;
1072 XRectangle *rect;
1073 RGNDATA *data = X11DRV_GetRegionData( hrgn, dev->hdc );
1075 if (!data) return FALSE;
1076 rect = (XRectangle *)data->Buffer;
1077 for (i = 0; i < data->rdh.nCount; i++)
1079 rect[i].x += physDev->dc_rect.left;
1080 rect[i].y += physDev->dc_rect.top;
1083 XFillRectangles( gdi_display, physDev->drawable, physDev->gc, rect, data->rdh.nCount );
1084 free( data );
1086 if (NtGdiGetRgnBox( hrgn, &rc ))
1088 lp_to_dp( dev->hdc, (POINT *)&rc, 2 );
1089 add_device_bounds( physDev, &rc );
1091 return TRUE;
1094 /**********************************************************************
1095 * X11DRV_Polygon
1097 static BOOL X11DRV_Polygon( PHYSDEV dev, const POINT* pt, INT count )
1099 X11DRV_PDEVICE *physDev = get_x11drv_dev( dev );
1100 int i;
1101 POINT *points;
1102 XPoint *xpoints;
1104 points = malloc( count * sizeof(*pt) );
1105 if (!points) return FALSE;
1106 memcpy( points, pt, count * sizeof(*pt) );
1107 lp_to_dp( dev->hdc, points, count );
1108 add_pen_device_bounds( physDev, points, count );
1110 if (!(xpoints = malloc( sizeof(XPoint) * (count+1) )))
1112 free( points );
1113 return FALSE;
1115 for (i = 0; i < count; i++)
1117 xpoints[i].x = physDev->dc_rect.left + points[i].x;
1118 xpoints[i].y = physDev->dc_rect.top + points[i].y;
1120 xpoints[count] = xpoints[0];
1122 if (X11DRV_SetupGCForBrush( physDev ))
1123 XFillPolygon( gdi_display, physDev->drawable, physDev->gc,
1124 xpoints, count+1, Complex, CoordModeOrigin);
1126 if (X11DRV_SetupGCForPen ( physDev ))
1127 XDrawLines( gdi_display, physDev->drawable, physDev->gc,
1128 xpoints, count+1, CoordModeOrigin );
1130 free( xpoints );
1131 free( points );
1132 return TRUE;
1136 /**********************************************************************
1137 * X11DRV_PolyPolygon
1139 BOOL X11DRV_PolyPolygon( PHYSDEV dev, const POINT* pt, const INT* counts, UINT polygons )
1141 X11DRV_PDEVICE *physDev = get_x11drv_dev( dev );
1142 DWORD total = 0, max = 0, pos, i;
1143 POINT *points;
1144 BOOL ret = FALSE;
1146 if (polygons == 1) return X11DRV_Polygon( dev, pt, *counts );
1148 for (i = 0; i < polygons; i++)
1150 if (counts[i] < 2) return FALSE;
1151 if (counts[i] > max) max = counts[i];
1152 total += counts[i];
1155 points = malloc( total * sizeof(*pt) );
1156 if (!points) return FALSE;
1157 memcpy( points, pt, total * sizeof(*pt) );
1158 lp_to_dp( dev->hdc, points, total );
1159 add_pen_device_bounds( physDev, points, total );
1161 if (X11DRV_SetupGCForBrush( physDev ))
1163 DWORD poly_fill_mode;
1164 XRectangle *rect;
1165 HRGN hrgn;
1166 RGNDATA *data;
1168 NtGdiGetDCDword( dev->hdc, NtGdiGetPolyFillMode, &poly_fill_mode );
1169 hrgn = UlongToHandle( NtGdiPolyPolyDraw( UlongToHandle(poly_fill_mode), points,
1170 (const ULONG *)counts, polygons,
1171 NtGdiPolyPolygonRgn ));
1172 data = X11DRV_GetRegionData( hrgn, 0 );
1173 NtGdiDeleteObjectApp( hrgn );
1174 if (!data) goto done;
1175 rect = (XRectangle *)data->Buffer;
1176 for (i = 0; i < data->rdh.nCount; i++)
1178 rect[i].x += physDev->dc_rect.left;
1179 rect[i].y += physDev->dc_rect.top;
1182 XFillRectangles( gdi_display, physDev->drawable, physDev->gc, rect, data->rdh.nCount );
1183 free( data );
1186 if (X11DRV_SetupGCForPen ( physDev ))
1188 XPoint *xpoints;
1189 int j;
1191 if (!(xpoints = malloc( sizeof(XPoint) * (max + 1) ))) goto done;
1192 for (i = pos = 0; i < polygons; pos += counts[i++])
1194 for (j = 0; j < counts[i]; j++)
1196 xpoints[j].x = physDev->dc_rect.left + points[pos + j].x;
1197 xpoints[j].y = physDev->dc_rect.top + points[pos + j].y;
1199 xpoints[j] = xpoints[0];
1200 XDrawLines( gdi_display, physDev->drawable, physDev->gc, xpoints, j + 1, CoordModeOrigin );
1202 free( xpoints );
1204 ret = TRUE;
1206 done:
1207 free( points );
1208 return ret;
1212 /**********************************************************************
1213 * X11DRV_PolyPolyline
1215 BOOL X11DRV_PolyPolyline( PHYSDEV dev, const POINT* pt, const DWORD* counts, DWORD polylines )
1217 X11DRV_PDEVICE *physDev = get_x11drv_dev( dev );
1218 DWORD total = 0, max = 0, pos, i, j;
1219 POINT *points;
1221 for (i = 0; i < polylines; i++)
1223 if (counts[i] < 2) return FALSE;
1224 if (counts[i] > max) max = counts[i];
1225 total += counts[i];
1228 points = malloc( total * sizeof(*pt) );
1229 if (!points) return FALSE;
1230 memcpy( points, pt, total * sizeof(*pt) );
1231 lp_to_dp( dev->hdc, points, total );
1232 add_pen_device_bounds( physDev, points, total );
1234 if (X11DRV_SetupGCForPen ( physDev ))
1236 XPoint *xpoints;
1238 if (!(xpoints = malloc( sizeof(XPoint) * max )))
1240 free( points );
1241 return FALSE;
1243 for (i = pos = 0; i < polylines; pos += counts[i++])
1245 for (j = 0; j < counts[i]; j++)
1247 xpoints[j].x = physDev->dc_rect.left + points[pos + j].x;
1248 xpoints[j].y = physDev->dc_rect.top + points[pos + j].y;
1250 XDrawLines( gdi_display, physDev->drawable, physDev->gc, xpoints, j, CoordModeOrigin );
1252 free( xpoints );
1254 free( points );
1255 return TRUE;
1258 /* helper for path stroking and filling functions */
1259 static BOOL x11drv_stroke_and_fill_path( PHYSDEV dev, BOOL stroke, BOOL fill )
1261 X11DRV_PDEVICE *physDev = get_x11drv_dev( dev );
1262 POINT *points;
1263 BYTE *flags;
1264 BOOL ret = FALSE;
1265 XPoint *xpoints;
1266 int i, j, size;
1268 NtGdiFlattenPath( dev->hdc );
1269 if ((size = NtGdiGetPath( dev->hdc, NULL, NULL, 0 )) == -1) return FALSE;
1270 if (!size)
1272 NtGdiAbortPath( dev->hdc );
1273 return TRUE;
1275 xpoints = malloc( (size + 1) * sizeof(*xpoints) );
1276 points = malloc( size * sizeof(*points) );
1277 flags = malloc( size * sizeof(*flags) );
1278 if (!points || !flags || !xpoints) goto done;
1279 if (NtGdiGetPath( dev->hdc, points, flags, size ) == -1) goto done;
1280 lp_to_dp( dev->hdc, points, size );
1282 if (fill && X11DRV_SetupGCForBrush( physDev ))
1284 XRectangle *rect;
1285 HRGN hrgn = NtGdiPathToRegion( dev->hdc );
1286 RGNDATA *data = X11DRV_GetRegionData( hrgn, 0 );
1288 NtGdiDeleteObjectApp( hrgn );
1289 if (!data) goto done;
1290 rect = (XRectangle *)data->Buffer;
1291 for (i = 0; i < data->rdh.nCount; i++)
1293 rect[i].x += physDev->dc_rect.left;
1294 rect[i].y += physDev->dc_rect.top;
1297 XFillRectangles( gdi_display, physDev->drawable, physDev->gc, rect, data->rdh.nCount );
1298 free( data );
1301 if (stroke && X11DRV_SetupGCForPen ( physDev ))
1303 for (i = j = 0; i < size; i++, j++)
1305 if (flags[i] == PT_MOVETO)
1307 if (j > 1)
1309 if (fill || (flags[i - 1] & PT_CLOSEFIGURE)) xpoints[j++] = xpoints[0];
1310 XDrawLines( gdi_display, physDev->drawable, physDev->gc, xpoints, j, CoordModeOrigin );
1312 j = 0;
1314 xpoints[j].x = physDev->dc_rect.left + points[i].x;
1315 xpoints[j].y = physDev->dc_rect.top + points[i].y;
1317 if (j > 1)
1319 if (fill || (flags[i - 1] & PT_CLOSEFIGURE)) xpoints[j++] = xpoints[0];
1320 XDrawLines( gdi_display, physDev->drawable, physDev->gc, xpoints, j, CoordModeOrigin );
1324 add_pen_device_bounds( physDev, points, size );
1325 NtGdiAbortPath( dev->hdc );
1326 ret = TRUE;
1328 done:
1329 free( xpoints );
1330 free( points );
1331 free( flags );
1332 return ret;
1335 /**********************************************************************
1336 * X11DRV_FillPath
1338 BOOL X11DRV_FillPath( PHYSDEV dev )
1340 return x11drv_stroke_and_fill_path( dev, FALSE, TRUE );
1343 /**********************************************************************
1344 * X11DRV_StrokeAndFillPath
1346 BOOL X11DRV_StrokeAndFillPath( PHYSDEV dev )
1348 return x11drv_stroke_and_fill_path( dev, TRUE, TRUE );
1351 /**********************************************************************
1352 * X11DRV_StrokePath
1354 BOOL X11DRV_StrokePath( PHYSDEV dev )
1356 return x11drv_stroke_and_fill_path( dev, TRUE, FALSE );
1360 /**********************************************************************
1361 * X11DRV_InternalFloodFill
1363 * Internal helper function for flood fill.
1364 * (xorg,yorg) is the origin of the X image relative to the drawable.
1365 * (x,y) is relative to the origin of the X image.
1367 static void X11DRV_InternalFloodFill(XImage *image, X11DRV_PDEVICE *physDev,
1368 int x, int y,
1369 int xOrg, int yOrg,
1370 unsigned long pixel, WORD fillType, RECT *bounds )
1372 int left, right;
1374 #define TO_FLOOD(x,y) ((fillType == FLOODFILLBORDER) ? \
1375 (XGetPixel(image,x,y) != pixel) : \
1376 (XGetPixel(image,x,y) == pixel))
1378 if (!TO_FLOOD(x,y)) return;
1380 /* Find left and right boundaries */
1382 left = right = x;
1383 while ((left > 0) && TO_FLOOD( left-1, y )) left--;
1384 while ((right < image->width) && TO_FLOOD( right, y )) right++;
1385 bounds->left = min( bounds->left, left );
1386 bounds->top = min( bounds->top, y );
1387 bounds->right = max( bounds->right, right );
1388 bounds->bottom = max( bounds->bottom, y + 1 );
1389 XFillRectangle( gdi_display, physDev->drawable, physDev->gc,
1390 xOrg + left, yOrg + y, right-left, 1 );
1392 /* Set the pixels of this line so we don't fill it again */
1394 for (x = left; x < right; x++)
1396 if (fillType == FLOODFILLBORDER) XPutPixel( image, x, y, pixel );
1397 else XPutPixel( image, x, y, ~pixel );
1400 /* Fill the line above */
1402 if (--y >= 0)
1404 x = left;
1405 while (x < right)
1407 while ((x < right) && !TO_FLOOD(x,y)) x++;
1408 if (x >= right) break;
1409 while ((x < right) && TO_FLOOD(x,y)) x++;
1410 X11DRV_InternalFloodFill(image, physDev, x-1, y,
1411 xOrg, yOrg, pixel, fillType, bounds );
1415 /* Fill the line below */
1417 if ((y += 2) < image->height)
1419 x = left;
1420 while (x < right)
1422 while ((x < right) && !TO_FLOOD(x,y)) x++;
1423 if (x >= right) break;
1424 while ((x < right) && TO_FLOOD(x,y)) x++;
1425 X11DRV_InternalFloodFill(image, physDev, x-1, y,
1426 xOrg, yOrg, pixel, fillType, bounds );
1429 #undef TO_FLOOD
1433 static int ExtFloodFillXGetImageErrorHandler( Display *dpy, XErrorEvent *event, void *arg )
1435 return (event->request_code == X_GetImage && event->error_code == BadMatch);
1438 /**********************************************************************
1439 * X11DRV_ExtFloodFill
1441 BOOL X11DRV_ExtFloodFill( PHYSDEV dev, INT x, INT y, COLORREF color, UINT fillType )
1443 X11DRV_PDEVICE *physDev = get_x11drv_dev( dev );
1444 XImage *image;
1445 RECT rect, bounds;
1446 POINT pt;
1448 TRACE("X11DRV_ExtFloodFill %d,%d %s %d\n", x, y, debugstr_color(color), fillType );
1450 pt.x = x;
1451 pt.y = y;
1452 lp_to_dp( dev->hdc, &pt, 1 );
1454 if (!physDev->region)
1456 rect.left = 0;
1457 rect.top = 0;
1458 rect.right = physDev->dc_rect.right - physDev->dc_rect.left;
1459 rect.bottom = physDev->dc_rect.bottom - physDev->dc_rect.top;
1461 else
1463 if (!NtGdiPtInRegion( physDev->region, pt.x, pt.y )) return FALSE;
1464 NtGdiGetRgnBox( physDev->region, &rect );
1465 rect.left = max( rect.left, 0 );
1466 rect.top = max( rect.top, 0 );
1467 rect.right = min( rect.right, physDev->dc_rect.right - physDev->dc_rect.left );
1468 rect.bottom = min( rect.bottom, physDev->dc_rect.bottom - physDev->dc_rect.top );
1470 if (pt.x < rect.left || pt.x >= rect.right || pt.y < rect.top || pt.y >= rect.bottom) return FALSE;
1472 X11DRV_expect_error( gdi_display, ExtFloodFillXGetImageErrorHandler, NULL );
1473 image = XGetImage( gdi_display, physDev->drawable,
1474 physDev->dc_rect.left + rect.left, physDev->dc_rect.top + rect.top,
1475 rect.right - rect.left, rect.bottom - rect.top,
1476 AllPlanes, ZPixmap );
1477 if(X11DRV_check_error()) image = NULL;
1478 if (!image) return FALSE;
1480 if (X11DRV_SetupGCForBrush( physDev ))
1482 unsigned long pixel = X11DRV_PALETTE_ToPhysical( physDev, color );
1484 reset_bounds( &bounds );
1486 X11DRV_InternalFloodFill(image, physDev,
1487 pt.x - rect.left,
1488 pt.y - rect.top,
1489 physDev->dc_rect.left + rect.left,
1490 physDev->dc_rect.top + rect.top,
1491 pixel, fillType, &bounds );
1493 OffsetRect( &bounds, rect.left, rect.top );
1494 add_device_bounds( physDev, &bounds );
1497 XDestroyImage( image );
1498 return TRUE;
1501 /**********************************************************************
1502 * X11DRV_GradientFill
1504 BOOL X11DRV_GradientFill( PHYSDEV dev, TRIVERTEX *vert_array, ULONG nvert,
1505 void *grad_array, ULONG ngrad, ULONG mode )
1507 X11DRV_PDEVICE *physdev = get_x11drv_dev( dev );
1508 const GRADIENT_RECT *rect = grad_array;
1509 TRIVERTEX v[2];
1510 POINT pt[2];
1511 RECT rc, bounds;
1512 unsigned int i;
1513 XGCValues val;
1515 /* <= 16-bpp use dithering */
1516 if (physdev->depth <= 16) goto fallback;
1518 switch (mode)
1520 case GRADIENT_FILL_RECT_H:
1521 val.function = GXcopy;
1522 val.fill_style = FillSolid;
1523 val.line_width = 1;
1524 val.cap_style = CapNotLast;
1525 val.line_style = LineSolid;
1526 XChangeGC( gdi_display, physdev->gc,
1527 GCFunction | GCLineWidth | GCLineStyle | GCCapStyle | GCFillStyle, &val );
1528 reset_bounds( &bounds );
1530 for (i = 0; i < ngrad; i++, rect++)
1532 int x, dx;
1534 v[0] = vert_array[rect->UpperLeft];
1535 v[1] = vert_array[rect->LowerRight];
1536 pt[0].x = v[0].x;
1537 pt[0].y = v[0].y;
1538 pt[1].x = v[1].x;
1539 pt[1].y = v[1].y;
1540 lp_to_dp( dev->hdc, pt, 2 );
1541 dx = pt[1].x - pt[0].x;
1542 if (!dx) continue;
1543 if (dx < 0) /* swap the colors */
1545 v[0] = vert_array[rect->LowerRight];
1546 v[1] = vert_array[rect->UpperLeft];
1547 dx = -dx;
1549 rc.left = min( pt[0].x, pt[1].x );
1550 rc.top = min( pt[0].y, pt[1].y );
1551 rc.right = max( pt[0].x, pt[1].x );
1552 rc.bottom = max( pt[0].y, pt[1].y );
1553 add_bounds_rect( &bounds, &rc );
1554 for (x = 0; x < dx; x++)
1556 int color = X11DRV_PALETTE_ToPhysical( physdev,
1557 RGB( (v[0].Red * (dx - x) + v[1].Red * x) / dx / 256,
1558 (v[0].Green * (dx - x) + v[1].Green * x) / dx / 256,
1559 (v[0].Blue * (dx - x) + v[1].Blue * x) / dx / 256) );
1561 XSetForeground( gdi_display, physdev->gc, color );
1562 XDrawLine( gdi_display, physdev->drawable, physdev->gc,
1563 physdev->dc_rect.left + rc.left + x, physdev->dc_rect.top + rc.top,
1564 physdev->dc_rect.left + rc.left + x, physdev->dc_rect.top + rc.bottom );
1567 add_device_bounds( physdev, &bounds );
1568 return TRUE;
1570 case GRADIENT_FILL_RECT_V:
1571 val.function = GXcopy;
1572 val.fill_style = FillSolid;
1573 val.line_width = 1;
1574 val.cap_style = CapNotLast;
1575 val.line_style = LineSolid;
1576 XChangeGC( gdi_display, physdev->gc,
1577 GCFunction | GCLineWidth | GCLineStyle | GCCapStyle | GCFillStyle, &val );
1578 reset_bounds( &bounds );
1580 for (i = 0; i < ngrad; i++, rect++)
1582 int y, dy;
1584 v[0] = vert_array[rect->UpperLeft];
1585 v[1] = vert_array[rect->LowerRight];
1586 pt[0].x = v[0].x;
1587 pt[0].y = v[0].y;
1588 pt[1].x = v[1].x;
1589 pt[1].y = v[1].y;
1590 lp_to_dp( dev->hdc, pt, 2 );
1591 dy = pt[1].y - pt[0].y;
1592 if (!dy) continue;
1593 if (dy < 0) /* swap the colors */
1595 v[0] = vert_array[rect->LowerRight];
1596 v[1] = vert_array[rect->UpperLeft];
1597 dy = -dy;
1599 rc.left = min( pt[0].x, pt[1].x );
1600 rc.top = min( pt[0].y, pt[1].y );
1601 rc.right = max( pt[0].x, pt[1].x );
1602 rc.bottom = max( pt[0].y, pt[1].y );
1603 add_bounds_rect( &bounds, &rc );
1604 for (y = 0; y < dy; y++)
1606 int color = X11DRV_PALETTE_ToPhysical( physdev,
1607 RGB( (v[0].Red * (dy - y) + v[1].Red * y) / dy / 256,
1608 (v[0].Green * (dy - y) + v[1].Green * y) / dy / 256,
1609 (v[0].Blue * (dy - y) + v[1].Blue * y) / dy / 256) );
1611 XSetForeground( gdi_display, physdev->gc, color );
1612 XDrawLine( gdi_display, physdev->drawable, physdev->gc,
1613 physdev->dc_rect.left + rc.left, physdev->dc_rect.top + rc.top + y,
1614 physdev->dc_rect.left + rc.right, physdev->dc_rect.top + rc.top + y );
1617 add_device_bounds( physdev, &bounds );
1618 return TRUE;
1621 fallback:
1622 dev = GET_NEXT_PHYSDEV( dev, pGradientFill );
1623 return dev->funcs->pGradientFill( dev, vert_array, nvert, grad_array, ngrad, mode );
1626 static char *get_icm_profile( unsigned long *size )
1628 Atom type;
1629 int format;
1630 unsigned long count, remaining;
1631 unsigned char *profile;
1632 char *ret = NULL;
1634 XGetWindowProperty( gdi_display, DefaultRootWindow(gdi_display),
1635 x11drv_atom(_ICC_PROFILE), 0, ~0UL, False, AnyPropertyType,
1636 &type, &format, &count, &remaining, &profile );
1637 *size = get_property_size( format, count );
1638 if (format && count)
1640 if ((ret = malloc( *size ))) memcpy( ret, profile, *size );
1641 XFree( profile );
1643 return ret;
1646 static const WCHAR mntr_key[] =
1647 {'\\','R','e','g','i','s','t','r','y','\\','M','a','c','h','i','n','e','\\',
1648 'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
1649 'W','i','n','d','o','w','s',' ','N','T','\\','C','u','r','r','e','n','t',
1650 'V','e','r','s','i','o','n','\\','I','C','M','\\','m','n','t','r'};
1652 static const WCHAR color_path[] =
1653 {'\\','?','?','\\','c',':','\\','w','i','n','d','o','w','s','\\','s','y','s','t','e','m','3','2',
1654 '\\','s','p','o','o','l','\\','d','r','i','v','e','r','s','\\','c','o','l','o','r','\\'};
1656 /***********************************************************************
1657 * GetICMProfile (X11DRV.@)
1659 BOOL X11DRV_GetICMProfile( PHYSDEV dev, BOOL allow_default, LPDWORD size, LPWSTR filename )
1661 static const WCHAR srgb[] =
1662 {'s','R','G','B',' ','C','o','l','o','r',' ','S','p','a','c','e',' ',
1663 'P','r','o','f','i','l','e','.','i','c','m',0};
1664 HKEY hkey;
1665 DWORD required;
1666 char buf[4096];
1667 KEY_VALUE_FULL_INFORMATION *info = (void *)buf;
1668 char *buffer;
1669 unsigned long buflen, i;
1670 ULONG full_size;
1671 WCHAR fullname[MAX_PATH + ARRAY_SIZE( color_path )], *p;
1672 UNICODE_STRING name;
1673 OBJECT_ATTRIBUTES attr;
1675 if (!size) return FALSE;
1677 memcpy( fullname, color_path, sizeof(color_path) );
1678 p = fullname + ARRAYSIZE(color_path);
1680 hkey = reg_open_key( NULL, mntr_key, sizeof(mntr_key) );
1682 if (hkey && !NtEnumerateValueKey( hkey, 0, KeyValueFullInformation,
1683 info, sizeof(buf), &full_size ))
1685 /* FIXME handle multiple values */
1686 memcpy( p, info->Name, info->NameLength );
1687 p[info->NameLength / sizeof(WCHAR)] = 0;
1689 else if ((buffer = get_icm_profile( &buflen )))
1691 static const WCHAR icm[] = {'.','i','c','m',0};
1692 IO_STATUS_BLOCK io = {{0}};
1693 UINT64 hash = 0;
1694 HANDLE file;
1695 int status;
1697 for (i = 0; i < buflen; i++) hash = (hash << 16) - hash + buffer[i];
1698 for (i = 0; i < sizeof(hash) * 2; i++)
1700 int digit = hash & 0xf;
1701 p[i] = digit < 10 ? '0' + digit : 'a' - 10 + digit;
1702 hash >>= 4;
1705 memcpy( p + i, icm, sizeof(icm) );
1707 RtlInitUnicodeString( &name, fullname );
1708 InitializeObjectAttributes( &attr, &name, OBJ_CASE_INSENSITIVE, NULL, NULL );
1709 status = NtCreateFile( &file, GENERIC_WRITE, &attr, &io, NULL, 0, 0, FILE_CREATE,
1710 FILE_SYNCHRONOUS_IO_NONALERT | FILE_NON_DIRECTORY_FILE, NULL, 0 );
1711 if (!status)
1713 status = NtWriteFile( file, NULL, NULL, NULL, &io, buffer, buflen, NULL, NULL );
1714 if (status) ERR( "Unable to write color profile: %x\n", status );
1715 NtClose( file );
1717 free( buffer );
1719 else if (!allow_default) return FALSE;
1720 else lstrcpyW( p, srgb );
1722 NtClose( hkey );
1723 required = wcslen( fullname ) + 1 - 4 /* skip NT prefix */;
1724 if (*size < required)
1726 *size = required;
1727 RtlSetLastWin32Error( ERROR_INSUFFICIENT_BUFFER );
1728 return FALSE;
1730 if (filename)
1732 FILE_BASIC_INFORMATION info;
1733 wcscpy( filename, fullname + 4 );
1734 RtlInitUnicodeString( &name, fullname );
1735 InitializeObjectAttributes( &attr, &name, OBJ_CASE_INSENSITIVE, NULL, NULL );
1736 if (NtQueryAttributesFile( &attr, &info ))
1737 WARN( "color profile not found in %s\n", debugstr_w(fullname) );
1739 *size = required;
1740 return TRUE;