user32: Respect rev for painting selections.
[wine.git] / dlls / winex11.drv / graphics.c
blob0abd5935d377c035004fa8c68165a1e068a3f784
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 #include "config.h"
29 #include <stdarg.h>
30 #include <math.h>
31 #ifdef HAVE_FLOAT_H
32 # include <float.h>
33 #endif
34 #include <stdlib.h>
35 #ifndef PI
36 #define PI M_PI
37 #endif
38 #include <string.h>
39 #include <limits.h>
41 #include "windef.h"
42 #include "winbase.h"
43 #include "winreg.h"
45 #include "x11drv.h"
46 #include "x11font.h"
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 */
67 GXnoop, /* R2_NOP */
68 GXorInverted, /* R2_MERGENOTPEN */
69 GXcopy, /* R2_COPYPEN */
70 GXorReverse, /* R2_MERGEPENNOT */
71 GXor, /* R2_MERGEPEN */
72 GXset /* R2_WHITE */
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 )
79 RECT rect;
81 rect.left = left;
82 rect.top = top;
83 rect.right = right;
84 rect.bottom = 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 */
89 rect.left--;
90 rect.right--;
92 LPtoDP( hdc, (POINT *)&rect, 2 );
93 if (rect.left > rect.right)
95 int tmp = rect.left;
96 rect.left = rect.right;
97 rect.right = tmp;
99 if (rect.top > rect.bottom)
101 int tmp = rect.top;
102 rect.top = rect.bottom;
103 rect.bottom = tmp;
105 return rect;
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 )
118 RGNDATA *data;
119 DWORD size;
120 unsigned int i;
121 RECT *rect, tmp;
122 XRectangle *xrect;
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 );
135 return NULL;
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;
149 rect[i].left = tmp;
151 if (rect[i].bottom < rect[i].top)
153 INT tmp = rect[i].bottom;
154 rect[i].bottom = rect[i].top;
155 rect[i].top = tmp;
160 if (sizeof(XRectangle) > sizeof(RECT))
162 int j;
163 /* need to start from the end */
164 for (j = data->rdh.nCount-1; j >= 0; j--)
166 tmp = rect[j];
167 xrect[j].x = max( min( tmp.left, SHRT_MAX), SHRT_MIN);
168 xrect[j].y = max( min( tmp.top, SHRT_MAX), SHRT_MIN);
169 xrect[j].width = max( min( tmp.right - xrect[j].x, USHRT_MAX), 0);
170 xrect[j].height = max( min( tmp.bottom - xrect[j].y, USHRT_MAX), 0);
173 else
175 for (i = 0; i < data->rdh.nCount; i++)
177 tmp = rect[i];
178 xrect[i].x = max( min( tmp.left, SHRT_MAX), SHRT_MIN);
179 xrect[i].y = max( min( tmp.top, SHRT_MAX), SHRT_MIN);
180 xrect[i].width = max( min( tmp.right - xrect[i].x, USHRT_MAX), 0);
181 xrect[i].height = max( min( tmp.bottom - xrect[i].y, USHRT_MAX), 0);
184 return data;
188 /***********************************************************************
189 * update_x11_clipping
191 static void update_x11_clipping( X11DRV_PDEVICE *physDev, const RGNDATA *data )
193 wine_tsx11_lock();
194 XSetClipRectangles( gdi_display, physDev->gc, physDev->dc_rect.left, physDev->dc_rect.top,
195 (XRectangle *)data->Buffer, data->rdh.nCount, YXBanded );
196 wine_tsx11_unlock();
200 /***********************************************************************
201 * add_extra_clipping_region
203 * Temporarily add a region to the current clipping region.
204 * The returned region must be restored with restore_clipping_region.
206 RGNDATA *add_extra_clipping_region( X11DRV_PDEVICE *dev, HRGN rgn )
208 RGNDATA *ret, *data;
209 HRGN clip;
211 if (!(ret = X11DRV_GetRegionData( dev->region, 0 ))) return NULL;
212 if (!(clip = CreateRectRgn( 0, 0, 0, 0 )))
214 HeapFree( GetProcessHeap(), 0, ret );
215 return NULL;
217 CombineRgn( clip, dev->region, rgn, RGN_AND );
218 if ((data = X11DRV_GetRegionData( clip, 0 )))
220 update_x11_clipping( dev, data );
221 HeapFree( GetProcessHeap(), 0, data );
223 DeleteObject( clip );
224 return ret;
228 /***********************************************************************
229 * restore_clipping_region
231 void restore_clipping_region( X11DRV_PDEVICE *dev, RGNDATA *data )
233 if (!data) return;
234 update_x11_clipping( dev, data );
235 HeapFree( GetProcessHeap(), 0, data );
239 /***********************************************************************
240 * X11DRV_SetDeviceClipping
242 void X11DRV_SetDeviceClipping( PHYSDEV dev, HRGN vis_rgn, HRGN clip_rgn )
244 RGNDATA *data;
245 X11DRV_PDEVICE *physDev = get_x11drv_dev( dev );
247 CombineRgn( physDev->region, vis_rgn, clip_rgn, clip_rgn ? RGN_AND : RGN_COPY );
249 if ((data = X11DRV_GetRegionData( physDev->region, 0 ))) update_x11_clipping( physDev, data );
250 HeapFree( GetProcessHeap(), 0, data );
254 /***********************************************************************
255 * X11DRV_SetupGCForPatBlt
257 * Setup the GC for a PatBlt operation using current brush.
258 * If fMapColors is TRUE, X pixels are mapped to Windows colors.
259 * Return FALSE if brush is BS_NULL, TRUE otherwise.
261 BOOL X11DRV_SetupGCForPatBlt( X11DRV_PDEVICE *physDev, GC gc, BOOL fMapColors )
263 XGCValues val;
264 unsigned long mask;
265 Pixmap pixmap = 0;
266 POINT pt;
268 if (physDev->brush.style == BS_NULL) return FALSE;
269 if (physDev->brush.pixel == -1)
271 /* Special case used for monochrome pattern brushes.
272 * We need to swap foreground and background because
273 * Windows does it the wrong way...
275 val.foreground = physDev->backgroundPixel;
276 val.background = physDev->textPixel;
278 else
280 val.foreground = physDev->brush.pixel;
281 val.background = physDev->backgroundPixel;
283 if (fMapColors && X11DRV_PALETTE_XPixelToPalette)
285 val.foreground = X11DRV_PALETTE_XPixelToPalette[val.foreground];
286 val.background = X11DRV_PALETTE_XPixelToPalette[val.background];
289 val.function = X11DRV_XROPfunction[GetROP2(physDev->dev.hdc)-1];
291 ** Let's replace GXinvert by GXxor with (black xor white)
292 ** This solves the selection color and leak problems in excel
293 ** FIXME : Let's do that only if we work with X-pixels, not with Win-pixels
295 if (val.function == GXinvert)
297 val.foreground = (WhitePixel( gdi_display, DefaultScreen(gdi_display) ) ^
298 BlackPixel( gdi_display, DefaultScreen(gdi_display) ));
299 val.function = GXxor;
301 val.fill_style = physDev->brush.fillStyle;
302 switch(val.fill_style)
304 case FillStippled:
305 case FillOpaqueStippled:
306 if (GetBkMode(physDev->dev.hdc)==OPAQUE) val.fill_style = FillOpaqueStippled;
307 val.stipple = physDev->brush.pixmap;
308 mask = GCStipple;
309 break;
311 case FillTiled:
312 if (fMapColors && X11DRV_PALETTE_XPixelToPalette)
314 register int x, y;
315 XImage *image;
316 wine_tsx11_lock();
317 pixmap = XCreatePixmap( gdi_display, root_window, 8, 8, physDev->depth );
318 image = XGetImage( gdi_display, physDev->brush.pixmap, 0, 0, 8, 8,
319 AllPlanes, ZPixmap );
320 for (y = 0; y < 8; y++)
321 for (x = 0; x < 8; x++)
322 XPutPixel( image, x, y,
323 X11DRV_PALETTE_XPixelToPalette[XGetPixel( image, x, y)] );
324 XPutImage( gdi_display, pixmap, gc, image, 0, 0, 0, 0, 8, 8 );
325 XDestroyImage( image );
326 wine_tsx11_unlock();
327 val.tile = pixmap;
329 else val.tile = physDev->brush.pixmap;
330 mask = GCTile;
331 break;
333 default:
334 mask = 0;
335 break;
337 GetBrushOrgEx( physDev->dev.hdc, &pt );
338 val.ts_x_origin = physDev->dc_rect.left + pt.x;
339 val.ts_y_origin = physDev->dc_rect.top + pt.y;
340 val.fill_rule = (GetPolyFillMode(physDev->dev.hdc) == WINDING) ? WindingRule : EvenOddRule;
341 wine_tsx11_lock();
342 XChangeGC( gdi_display, gc,
343 GCFunction | GCForeground | GCBackground | GCFillStyle |
344 GCFillRule | GCTileStipXOrigin | GCTileStipYOrigin | mask,
345 &val );
346 if (pixmap) XFreePixmap( gdi_display, pixmap );
347 wine_tsx11_unlock();
348 return TRUE;
352 /***********************************************************************
353 * X11DRV_SetupGCForBrush
355 * Setup physDev->gc for drawing operations using current brush.
356 * Return FALSE if brush is BS_NULL, TRUE otherwise.
358 BOOL X11DRV_SetupGCForBrush( X11DRV_PDEVICE *physDev )
360 return X11DRV_SetupGCForPatBlt( physDev, physDev->gc, FALSE );
364 /***********************************************************************
365 * X11DRV_SetupGCForPen
367 * Setup physDev->gc for drawing operations using current pen.
368 * Return FALSE if pen is PS_NULL, TRUE otherwise.
370 static BOOL X11DRV_SetupGCForPen( X11DRV_PDEVICE *physDev )
372 XGCValues val;
373 UINT rop2 = GetROP2(physDev->dev.hdc);
375 if (physDev->pen.style == PS_NULL) return FALSE;
377 switch (rop2)
379 case R2_BLACK :
380 val.foreground = BlackPixel( gdi_display, DefaultScreen(gdi_display) );
381 val.function = GXcopy;
382 break;
383 case R2_WHITE :
384 val.foreground = WhitePixel( gdi_display, DefaultScreen(gdi_display) );
385 val.function = GXcopy;
386 break;
387 case R2_XORPEN :
388 val.foreground = physDev->pen.pixel;
389 /* It is very unlikely someone wants to XOR with 0 */
390 /* This fixes the rubber-drawings in paintbrush */
391 if (val.foreground == 0)
392 val.foreground = (WhitePixel( gdi_display, DefaultScreen(gdi_display) ) ^
393 BlackPixel( gdi_display, DefaultScreen(gdi_display) ));
394 val.function = GXxor;
395 break;
396 default :
397 val.foreground = physDev->pen.pixel;
398 val.function = X11DRV_XROPfunction[rop2-1];
400 val.background = physDev->backgroundPixel;
401 val.fill_style = FillSolid;
402 val.line_width = physDev->pen.width;
403 if (val.line_width <= 1) {
404 val.cap_style = CapNotLast;
405 } else {
406 switch (physDev->pen.endcap)
408 case PS_ENDCAP_SQUARE:
409 val.cap_style = CapProjecting;
410 break;
411 case PS_ENDCAP_FLAT:
412 val.cap_style = CapButt;
413 break;
414 case PS_ENDCAP_ROUND:
415 default:
416 val.cap_style = CapRound;
419 switch (physDev->pen.linejoin)
421 case PS_JOIN_BEVEL:
422 val.join_style = JoinBevel;
423 break;
424 case PS_JOIN_MITER:
425 val.join_style = JoinMiter;
426 break;
427 case PS_JOIN_ROUND:
428 default:
429 val.join_style = JoinRound;
432 if (physDev->pen.dash_len)
433 val.line_style = ((GetBkMode(physDev->dev.hdc) == OPAQUE) && (!physDev->pen.ext))
434 ? LineDoubleDash : LineOnOffDash;
435 else
436 val.line_style = LineSolid;
438 wine_tsx11_lock();
439 if (physDev->pen.dash_len)
440 XSetDashes( gdi_display, physDev->gc, 0, physDev->pen.dashes, physDev->pen.dash_len );
441 XChangeGC( gdi_display, physDev->gc,
442 GCFunction | GCForeground | GCBackground | GCLineWidth |
443 GCLineStyle | GCCapStyle | GCJoinStyle | GCFillStyle, &val );
444 wine_tsx11_unlock();
445 return TRUE;
449 /***********************************************************************
450 * X11DRV_SetupGCForText
452 * Setup physDev->gc for text drawing operations.
453 * Return FALSE if the font is null, TRUE otherwise.
455 BOOL X11DRV_SetupGCForText( X11DRV_PDEVICE *physDev )
457 XFontStruct* xfs = XFONT_GetFontStruct( physDev->font );
459 if( xfs )
461 XGCValues val;
463 val.function = GXcopy; /* Text is always GXcopy */
464 val.foreground = physDev->textPixel;
465 val.background = physDev->backgroundPixel;
466 val.fill_style = FillSolid;
467 val.font = xfs->fid;
469 wine_tsx11_lock();
470 XChangeGC( gdi_display, physDev->gc,
471 GCFunction | GCForeground | GCBackground | GCFillStyle |
472 GCFont, &val );
473 wine_tsx11_unlock();
474 return TRUE;
476 WARN("Physical font failure\n" );
477 return FALSE;
480 /***********************************************************************
481 * X11DRV_XWStoDS
483 * Performs a world-to-viewport transformation on the specified width.
485 INT X11DRV_XWStoDS( HDC hdc, INT width )
487 POINT pt[2];
489 pt[0].x = 0;
490 pt[0].y = 0;
491 pt[1].x = width;
492 pt[1].y = 0;
493 LPtoDP( hdc, pt, 2 );
494 return pt[1].x - pt[0].x;
497 /***********************************************************************
498 * X11DRV_YWStoDS
500 * Performs a world-to-viewport transformation on the specified height.
502 INT X11DRV_YWStoDS( HDC hdc, INT height )
504 POINT pt[2];
506 pt[0].x = 0;
507 pt[0].y = 0;
508 pt[1].x = 0;
509 pt[1].y = height;
510 LPtoDP( hdc, pt, 2 );
511 return pt[1].y - pt[0].y;
514 /***********************************************************************
515 * X11DRV_LineTo
517 BOOL X11DRV_LineTo( PHYSDEV dev, INT x, INT y )
519 X11DRV_PDEVICE *physDev = get_x11drv_dev( dev );
520 POINT pt[2];
522 if (X11DRV_SetupGCForPen( physDev )) {
523 /* Update the pixmap from the DIB section */
524 X11DRV_LockDIBSection(physDev, DIB_Status_GdiMod);
526 GetCurrentPositionEx( dev->hdc, &pt[0] );
527 pt[1].x = x;
528 pt[1].y = y;
529 LPtoDP( dev->hdc, pt, 2 );
531 wine_tsx11_lock();
532 XDrawLine(gdi_display, physDev->drawable, physDev->gc,
533 physDev->dc_rect.left + pt[0].x, physDev->dc_rect.top + pt[0].y,
534 physDev->dc_rect.left + pt[1].x, physDev->dc_rect.top + pt[1].y );
535 wine_tsx11_unlock();
537 /* Update the DIBSection from the pixmap */
538 X11DRV_UnlockDIBSection(physDev, TRUE);
540 return TRUE;
545 /***********************************************************************
546 * X11DRV_DrawArc
548 * Helper functions for Arc(), Chord() and Pie().
549 * 'lines' is the number of lines to draw: 0 for Arc, 1 for Chord, 2 for Pie.
552 static BOOL X11DRV_DrawArc( PHYSDEV dev, INT left, INT top, INT right, INT bottom,
553 INT xstart, INT ystart, INT xend, INT yend, INT lines )
555 X11DRV_PDEVICE *physDev = get_x11drv_dev( dev );
556 INT xcenter, ycenter, istart_angle, idiff_angle;
557 INT width, oldwidth;
558 double start_angle, end_angle;
559 XPoint points[4];
560 BOOL update = FALSE;
561 POINT start, end;
562 RECT rc = get_device_rect( dev->hdc, left, top, right, bottom );
564 start.x = xstart;
565 start.y = ystart;
566 end.x = xend;
567 end.y = yend;
568 LPtoDP(dev->hdc, &start, 1);
569 LPtoDP(dev->hdc, &end, 1);
571 if ((rc.left == rc.right) || (rc.top == rc.bottom)
572 ||(lines && ((rc.right-rc.left==1)||(rc.bottom-rc.top==1)))) return TRUE;
574 if (GetArcDirection( dev->hdc ) == AD_CLOCKWISE)
575 { POINT tmp = start; start = end; end = tmp; }
577 oldwidth = width = physDev->pen.width;
578 if (!width) width = 1;
579 if(physDev->pen.style == PS_NULL) width = 0;
581 if (physDev->pen.style == PS_INSIDEFRAME)
583 if (2*width > (rc.right-rc.left)) width=(rc.right-rc.left + 1)/2;
584 if (2*width > (rc.bottom-rc.top)) width=(rc.bottom-rc.top + 1)/2;
585 rc.left += width / 2;
586 rc.right -= (width - 1) / 2;
587 rc.top += width / 2;
588 rc.bottom -= (width - 1) / 2;
590 if(width == 0) width = 1; /* more accurate */
591 physDev->pen.width = width;
593 xcenter = (rc.right + rc.left) / 2;
594 ycenter = (rc.bottom + rc.top) / 2;
595 start_angle = atan2( (double)(ycenter-start.y)*(rc.right-rc.left),
596 (double)(start.x-xcenter)*(rc.bottom-rc.top) );
597 end_angle = atan2( (double)(ycenter-end.y)*(rc.right-rc.left),
598 (double)(end.x-xcenter)*(rc.bottom-rc.top) );
599 if ((start.x==end.x)&&(start.y==end.y))
600 { /* A lazy program delivers xstart=xend=ystart=yend=0) */
601 start_angle = 0;
602 end_angle = 2* PI;
604 else /* notorious cases */
605 if ((start_angle == PI)&&( end_angle <0))
606 start_angle = - PI;
607 else
608 if ((end_angle == PI)&&( start_angle <0))
609 end_angle = - PI;
610 istart_angle = (INT)(start_angle * 180 * 64 / PI + 0.5);
611 idiff_angle = (INT)((end_angle - start_angle) * 180 * 64 / PI + 0.5);
612 if (idiff_angle <= 0) idiff_angle += 360 * 64;
614 /* Update the pixmap from the DIB section */
615 X11DRV_LockDIBSection(physDev, DIB_Status_GdiMod);
617 /* Fill arc with brush if Chord() or Pie() */
619 if ((lines > 0) && X11DRV_SetupGCForBrush( physDev )) {
620 wine_tsx11_lock();
621 XSetArcMode( gdi_display, physDev->gc, (lines==1) ? ArcChord : ArcPieSlice);
622 XFillArc( gdi_display, physDev->drawable, physDev->gc,
623 physDev->dc_rect.left + rc.left, physDev->dc_rect.top + rc.top,
624 rc.right-rc.left-1, rc.bottom-rc.top-1, istart_angle, idiff_angle );
625 wine_tsx11_unlock();
626 update = TRUE;
629 /* Draw arc and lines */
631 if (X11DRV_SetupGCForPen( physDev ))
633 wine_tsx11_lock();
634 XDrawArc( gdi_display, physDev->drawable, physDev->gc,
635 physDev->dc_rect.left + rc.left, physDev->dc_rect.top + rc.top,
636 rc.right-rc.left-1, rc.bottom-rc.top-1, istart_angle, idiff_angle );
637 if (lines) {
638 /* use the truncated values */
639 start_angle=(double)istart_angle*PI/64./180.;
640 end_angle=(double)(istart_angle+idiff_angle)*PI/64./180.;
641 /* calculate the endpoints and round correctly */
642 points[0].x = (int) floor(physDev->dc_rect.left + (rc.right+rc.left)/2.0 +
643 cos(start_angle) * (rc.right-rc.left-width*2+2) / 2. + 0.5);
644 points[0].y = (int) floor(physDev->dc_rect.top + (rc.top+rc.bottom)/2.0 -
645 sin(start_angle) * (rc.bottom-rc.top-width*2+2) / 2. + 0.5);
646 points[1].x = (int) floor(physDev->dc_rect.left + (rc.right+rc.left)/2.0 +
647 cos(end_angle) * (rc.right-rc.left-width*2+2) / 2. + 0.5);
648 points[1].y = (int) floor(physDev->dc_rect.top + (rc.top+rc.bottom)/2.0 -
649 sin(end_angle) * (rc.bottom-rc.top-width*2+2) / 2. + 0.5);
651 /* OK, this stuff is optimized for Xfree86
652 * which is probably the server most used by
653 * wine users. Other X servers will not
654 * display correctly. (eXceed for instance)
655 * so if you feel you must make changes, make sure that
656 * you either use Xfree86 or separate your changes
657 * from these (compile switch or whatever)
659 if (lines == 2) {
660 INT dx1,dy1;
661 points[3] = points[1];
662 points[1].x = physDev->dc_rect.left + xcenter;
663 points[1].y = physDev->dc_rect.top + ycenter;
664 points[2] = points[1];
665 dx1=points[1].x-points[0].x;
666 dy1=points[1].y-points[0].y;
667 if(((rc.top-rc.bottom) | -2) == -2)
668 if(dy1>0) points[1].y--;
669 if(dx1<0) {
670 if (((-dx1)*64)<=ABS(dy1)*37) points[0].x--;
671 if(((-dx1*9))<(dy1*16)) points[0].y--;
672 if( dy1<0 && ((dx1*9)) < (dy1*16)) points[0].y--;
673 } else {
674 if(dy1 < 0) points[0].y--;
675 if(((rc.right-rc.left) | -2) == -2) points[1].x--;
677 dx1=points[3].x-points[2].x;
678 dy1=points[3].y-points[2].y;
679 if(((rc.top-rc.bottom) | -2 ) == -2)
680 if(dy1 < 0) points[2].y--;
681 if( dx1<0){
682 if( dy1>0) points[3].y--;
683 if(((rc.right-rc.left) | -2) == -2 ) points[2].x--;
684 }else {
685 points[3].y--;
686 if( dx1 * 64 < dy1 * -37 ) points[3].x--;
688 lines++;
690 XDrawLines( gdi_display, physDev->drawable, physDev->gc,
691 points, lines+1, CoordModeOrigin );
693 wine_tsx11_unlock();
694 update = TRUE;
697 /* Update the DIBSection of the pixmap */
698 X11DRV_UnlockDIBSection(physDev, update);
700 physDev->pen.width = oldwidth;
701 return TRUE;
705 /***********************************************************************
706 * X11DRV_Arc
708 BOOL X11DRV_Arc( PHYSDEV dev, INT left, INT top, INT right, INT bottom,
709 INT xstart, INT ystart, INT xend, INT yend )
711 return X11DRV_DrawArc( dev, left, top, right, bottom, xstart, ystart, xend, yend, 0 );
715 /***********************************************************************
716 * X11DRV_Pie
718 BOOL X11DRV_Pie( PHYSDEV dev, INT left, INT top, INT right, INT bottom,
719 INT xstart, INT ystart, INT xend, INT yend )
721 return X11DRV_DrawArc( dev, left, top, right, bottom, xstart, ystart, xend, yend, 2 );
724 /***********************************************************************
725 * X11DRV_Chord
727 BOOL X11DRV_Chord( PHYSDEV dev, INT left, INT top, INT right, INT bottom,
728 INT xstart, INT ystart, INT xend, INT yend )
730 return X11DRV_DrawArc( dev, left, top, right, bottom, xstart, ystart, xend, yend, 1 );
734 /***********************************************************************
735 * X11DRV_Ellipse
737 BOOL X11DRV_Ellipse( PHYSDEV dev, INT left, INT top, INT right, INT bottom )
739 X11DRV_PDEVICE *physDev = get_x11drv_dev( dev );
740 INT width, oldwidth;
741 BOOL update = FALSE;
742 RECT rc = get_device_rect( dev->hdc, left, top, right, bottom );
744 if ((rc.left == rc.right) || (rc.top == rc.bottom)) return TRUE;
746 oldwidth = width = physDev->pen.width;
747 if (!width) width = 1;
748 if(physDev->pen.style == PS_NULL) width = 0;
750 if (physDev->pen.style == PS_INSIDEFRAME)
752 if (2*width > (rc.right-rc.left)) width=(rc.right-rc.left + 1)/2;
753 if (2*width > (rc.bottom-rc.top)) width=(rc.bottom-rc.top + 1)/2;
754 rc.left += width / 2;
755 rc.right -= (width - 1) / 2;
756 rc.top += width / 2;
757 rc.bottom -= (width - 1) / 2;
759 if(width == 0) width = 1; /* more accurate */
760 physDev->pen.width = width;
762 /* Update the pixmap from the DIB section */
763 X11DRV_LockDIBSection(physDev, DIB_Status_GdiMod);
765 if (X11DRV_SetupGCForBrush( physDev ))
767 wine_tsx11_lock();
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 );
771 wine_tsx11_unlock();
772 update = TRUE;
774 if (X11DRV_SetupGCForPen( physDev ))
776 wine_tsx11_lock();
777 XDrawArc( gdi_display, physDev->drawable, physDev->gc,
778 physDev->dc_rect.left + rc.left, physDev->dc_rect.top + rc.top,
779 rc.right-rc.left-1, rc.bottom-rc.top-1, 0, 360*64 );
780 wine_tsx11_unlock();
781 update = TRUE;
784 /* Update the DIBSection from the pixmap */
785 X11DRV_UnlockDIBSection(physDev, update);
787 physDev->pen.width = oldwidth;
788 return TRUE;
792 /***********************************************************************
793 * X11DRV_Rectangle
795 BOOL X11DRV_Rectangle(PHYSDEV dev, INT left, INT top, INT right, INT bottom)
797 X11DRV_PDEVICE *physDev = get_x11drv_dev( dev );
798 INT width, oldwidth, oldjoinstyle;
799 BOOL update = FALSE;
800 RECT rc = get_device_rect( dev->hdc, left, top, right, bottom );
802 TRACE("(%d %d %d %d)\n", left, top, right, bottom);
804 if ((rc.left == rc.right) || (rc.top == rc.bottom)) return TRUE;
806 oldwidth = width = physDev->pen.width;
807 if (!width) width = 1;
808 if(physDev->pen.style == PS_NULL) width = 0;
810 if (physDev->pen.style == PS_INSIDEFRAME)
812 if (2*width > (rc.right-rc.left)) width=(rc.right-rc.left + 1)/2;
813 if (2*width > (rc.bottom-rc.top)) width=(rc.bottom-rc.top + 1)/2;
814 rc.left += width / 2;
815 rc.right -= (width - 1) / 2;
816 rc.top += width / 2;
817 rc.bottom -= (width - 1) / 2;
819 if(width == 1) width = 0;
820 physDev->pen.width = width;
821 oldjoinstyle = physDev->pen.linejoin;
822 if(physDev->pen.type != PS_GEOMETRIC)
823 physDev->pen.linejoin = PS_JOIN_MITER;
825 /* Update the pixmap from the DIB section */
826 X11DRV_LockDIBSection(physDev, DIB_Status_GdiMod);
828 if ((rc.right > rc.left + width) && (rc.bottom > rc.top + width))
830 if (X11DRV_SetupGCForBrush( physDev ))
832 wine_tsx11_lock();
833 XFillRectangle( gdi_display, physDev->drawable, physDev->gc,
834 physDev->dc_rect.left + rc.left + (width + 1) / 2,
835 physDev->dc_rect.top + rc.top + (width + 1) / 2,
836 rc.right-rc.left-width-1, rc.bottom-rc.top-width-1);
837 wine_tsx11_unlock();
838 update = TRUE;
841 if (X11DRV_SetupGCForPen( physDev ))
843 wine_tsx11_lock();
844 XDrawRectangle( gdi_display, physDev->drawable, physDev->gc,
845 physDev->dc_rect.left + rc.left, physDev->dc_rect.top + rc.top,
846 rc.right-rc.left-1, rc.bottom-rc.top-1 );
847 wine_tsx11_unlock();
848 update = TRUE;
851 /* Update the DIBSection from the pixmap */
852 X11DRV_UnlockDIBSection(physDev, update);
854 physDev->pen.width = oldwidth;
855 physDev->pen.linejoin = oldjoinstyle;
856 return TRUE;
859 /***********************************************************************
860 * X11DRV_RoundRect
862 BOOL X11DRV_RoundRect( PHYSDEV dev, INT left, INT top, INT right, INT bottom,
863 INT ell_width, INT ell_height )
865 X11DRV_PDEVICE *physDev = get_x11drv_dev( dev );
866 INT width, oldwidth, oldendcap;
867 BOOL update = FALSE;
868 POINT pts[2];
869 RECT rc = get_device_rect( dev->hdc, left, top, right, bottom );
871 TRACE("(%d %d %d %d %d %d\n",
872 left, top, right, bottom, ell_width, ell_height);
874 if ((rc.left == rc.right) || (rc.top == rc.bottom))
875 return TRUE;
877 /* Make sure ell_width and ell_height are >= 1 otherwise XDrawArc gets
878 called with width/height < 0 */
879 pts[0].x = pts[0].y = 0;
880 pts[1].x = ell_width;
881 pts[1].y = ell_height;
882 LPtoDP(dev->hdc, pts, 2);
883 ell_width = max(abs( pts[1].x - pts[0].x ), 1);
884 ell_height = max(abs( pts[1].y - pts[0].y ), 1);
886 oldwidth = width = physDev->pen.width;
887 oldendcap = physDev->pen.endcap;
888 if (!width) width = 1;
889 if(physDev->pen.style == PS_NULL) width = 0;
891 if (physDev->pen.style == PS_INSIDEFRAME)
893 if (2*width > (rc.right-rc.left)) width=(rc.right-rc.left + 1)/2;
894 if (2*width > (rc.bottom-rc.top)) width=(rc.bottom-rc.top + 1)/2;
895 rc.left += width / 2;
896 rc.right -= (width - 1) / 2;
897 rc.top += width / 2;
898 rc.bottom -= (width - 1) / 2;
900 if(width == 0) width = 1;
901 physDev->pen.width = width;
902 physDev->pen.endcap = PS_ENDCAP_SQUARE;
904 /* Update the pixmap from the DIB section */
905 X11DRV_LockDIBSection(physDev, DIB_Status_GdiMod);
907 if (X11DRV_SetupGCForBrush( physDev ))
909 wine_tsx11_lock();
910 if (ell_width > (rc.right-rc.left) )
911 if (ell_height > (rc.bottom-rc.top) )
912 XFillArc( gdi_display, physDev->drawable, physDev->gc,
913 physDev->dc_rect.left + rc.left, physDev->dc_rect.top + rc.top,
914 rc.right - rc.left - 1, rc.bottom - rc.top - 1,
915 0, 360 * 64 );
916 else{
917 XFillArc( gdi_display, physDev->drawable, physDev->gc,
918 physDev->dc_rect.left + rc.left, physDev->dc_rect.top + rc.top,
919 rc.right - rc.left - 1, ell_height, 0, 180 * 64 );
920 XFillArc( gdi_display, physDev->drawable, physDev->gc,
921 physDev->dc_rect.left + rc.left,
922 physDev->dc_rect.top + rc.bottom - ell_height - 1,
923 rc.right - rc.left - 1, ell_height, 180 * 64,
924 180 * 64 );
926 else if (ell_height > (rc.bottom-rc.top) ){
927 XFillArc( gdi_display, physDev->drawable, physDev->gc,
928 physDev->dc_rect.left + rc.left, physDev->dc_rect.top + rc.top,
929 ell_width, rc.bottom - rc.top - 1, 90 * 64, 180 * 64 );
930 XFillArc( gdi_display, physDev->drawable, physDev->gc,
931 physDev->dc_rect.left + rc.right - ell_width - 1, physDev->dc_rect.top + rc.top,
932 ell_width, rc.bottom - rc.top - 1, 270 * 64, 180 * 64 );
933 }else{
934 XFillArc( gdi_display, physDev->drawable, physDev->gc,
935 physDev->dc_rect.left + rc.left, physDev->dc_rect.top + rc.top,
936 ell_width, ell_height, 90 * 64, 90 * 64 );
937 XFillArc( gdi_display, physDev->drawable, physDev->gc,
938 physDev->dc_rect.left + rc.left,
939 physDev->dc_rect.top + rc.bottom - ell_height - 1,
940 ell_width, ell_height, 180 * 64, 90 * 64 );
941 XFillArc( gdi_display, physDev->drawable, physDev->gc,
942 physDev->dc_rect.left + rc.right - ell_width - 1,
943 physDev->dc_rect.top + rc.bottom - ell_height - 1,
944 ell_width, ell_height, 270 * 64, 90 * 64 );
945 XFillArc( gdi_display, physDev->drawable, physDev->gc,
946 physDev->dc_rect.left + rc.right - ell_width - 1,
947 physDev->dc_rect.top + rc.top,
948 ell_width, ell_height, 0, 90 * 64 );
950 if (ell_width < rc.right - rc.left)
952 XFillRectangle( gdi_display, physDev->drawable, physDev->gc,
953 physDev->dc_rect.left + rc.left + (ell_width + 1) / 2,
954 physDev->dc_rect.top + rc.top + 1,
955 rc.right - rc.left - ell_width - 1,
956 (ell_height + 1) / 2 - 1);
957 XFillRectangle( gdi_display, physDev->drawable, physDev->gc,
958 physDev->dc_rect.left + rc.left + (ell_width + 1) / 2,
959 physDev->dc_rect.top + rc.bottom - (ell_height) / 2 - 1,
960 rc.right - rc.left - ell_width - 1,
961 (ell_height) / 2 );
963 if (ell_height < rc.bottom - rc.top)
965 XFillRectangle( gdi_display, physDev->drawable, physDev->gc,
966 physDev->dc_rect.left + rc.left + 1,
967 physDev->dc_rect.top + rc.top + (ell_height + 1) / 2,
968 rc.right - rc.left - 2,
969 rc.bottom - rc.top - ell_height - 1);
971 wine_tsx11_unlock();
972 update = TRUE;
974 /* FIXME: this could be done with on X call
975 * more efficient and probably more correct
976 * on any X server: XDrawArcs will draw
977 * straight horizontal and vertical lines
978 * if width or height are zero.
980 * BTW this stuff is optimized for an Xfree86 server
981 * read the comments inside the X11DRV_DrawArc function
983 if (X11DRV_SetupGCForPen( physDev ))
985 wine_tsx11_lock();
986 if (ell_width > (rc.right-rc.left) )
987 if (ell_height > (rc.bottom-rc.top) )
988 XDrawArc( gdi_display, physDev->drawable, physDev->gc,
989 physDev->dc_rect.left + rc.left, physDev->dc_rect.top + rc.top,
990 rc.right - rc.left - 1, rc.bottom - rc.top - 1, 0 , 360 * 64 );
991 else{
992 XDrawArc( gdi_display, physDev->drawable, physDev->gc,
993 physDev->dc_rect.left + rc.left, physDev->dc_rect.top + rc.top,
994 rc.right - rc.left - 1, ell_height - 1, 0 , 180 * 64 );
995 XDrawArc( gdi_display, physDev->drawable, physDev->gc,
996 physDev->dc_rect.left + rc.left,
997 physDev->dc_rect.top + rc.bottom - ell_height,
998 rc.right - rc.left - 1, ell_height - 1, 180 * 64 , 180 * 64 );
1000 else if (ell_height > (rc.bottom-rc.top) ){
1001 XDrawArc( gdi_display, physDev->drawable, physDev->gc,
1002 physDev->dc_rect.left + rc.left, physDev->dc_rect.top + rc.top,
1003 ell_width - 1 , rc.bottom - rc.top - 1, 90 * 64 , 180 * 64 );
1004 XDrawArc( gdi_display, physDev->drawable, physDev->gc,
1005 physDev->dc_rect.left + rc.right - ell_width,
1006 physDev->dc_rect.top + rc.top,
1007 ell_width - 1 , rc.bottom - rc.top - 1, 270 * 64 , 180 * 64 );
1008 }else{
1009 XDrawArc( gdi_display, physDev->drawable, physDev->gc,
1010 physDev->dc_rect.left + rc.left, physDev->dc_rect.top + rc.top,
1011 ell_width - 1, ell_height - 1, 90 * 64, 90 * 64 );
1012 XDrawArc( gdi_display, physDev->drawable, physDev->gc,
1013 physDev->dc_rect.left + rc.left, physDev->dc_rect.top + rc.bottom - ell_height,
1014 ell_width - 1, ell_height - 1, 180 * 64, 90 * 64 );
1015 XDrawArc( gdi_display, physDev->drawable, physDev->gc,
1016 physDev->dc_rect.left + rc.right - ell_width,
1017 physDev->dc_rect.top + rc.bottom - ell_height,
1018 ell_width - 1, ell_height - 1, 270 * 64, 90 * 64 );
1019 XDrawArc( gdi_display, physDev->drawable, physDev->gc,
1020 physDev->dc_rect.left + rc.right - ell_width, physDev->dc_rect.top + rc.top,
1021 ell_width - 1, ell_height - 1, 0, 90 * 64 );
1023 if (ell_width < rc.right - rc.left)
1025 XDrawLine( gdi_display, physDev->drawable, physDev->gc,
1026 physDev->dc_rect.left + rc.left + ell_width / 2,
1027 physDev->dc_rect.top + rc.top,
1028 physDev->dc_rect.left + rc.right - (ell_width+1) / 2,
1029 physDev->dc_rect.top + rc.top);
1030 XDrawLine( gdi_display, physDev->drawable, physDev->gc,
1031 physDev->dc_rect.left + rc.left + ell_width / 2 ,
1032 physDev->dc_rect.top + rc.bottom - 1,
1033 physDev->dc_rect.left + rc.right - (ell_width+1)/ 2,
1034 physDev->dc_rect.top + rc.bottom - 1);
1036 if (ell_height < rc.bottom - rc.top)
1038 XDrawLine( gdi_display, physDev->drawable, physDev->gc,
1039 physDev->dc_rect.left + rc.right - 1,
1040 physDev->dc_rect.top + rc.top + ell_height / 2,
1041 physDev->dc_rect.left + rc.right - 1,
1042 physDev->dc_rect.top + rc.bottom - (ell_height+1) / 2);
1043 XDrawLine( gdi_display, physDev->drawable, physDev->gc,
1044 physDev->dc_rect.left + rc.left,
1045 physDev->dc_rect.top + rc.top + ell_height / 2,
1046 physDev->dc_rect.left + rc.left,
1047 physDev->dc_rect.top + rc.bottom - (ell_height+1) / 2);
1049 wine_tsx11_unlock();
1050 update = TRUE;
1052 /* Update the DIBSection from the pixmap */
1053 X11DRV_UnlockDIBSection(physDev, update);
1055 physDev->pen.width = oldwidth;
1056 physDev->pen.endcap = oldendcap;
1057 return TRUE;
1061 /***********************************************************************
1062 * X11DRV_SetPixel
1064 COLORREF X11DRV_SetPixel( PHYSDEV dev, INT x, INT y, COLORREF color )
1066 X11DRV_PDEVICE *physDev = get_x11drv_dev( dev );
1067 unsigned long pixel;
1068 POINT pt;
1070 pt.x = x;
1071 pt.y = y;
1072 LPtoDP( dev->hdc, &pt, 1 );
1073 pixel = X11DRV_PALETTE_ToPhysical( physDev, color );
1075 /* Update the pixmap from the DIB section */
1076 X11DRV_LockDIBSection(physDev, DIB_Status_GdiMod);
1078 /* inefficient but simple... */
1079 wine_tsx11_lock();
1080 XSetForeground( gdi_display, physDev->gc, pixel );
1081 XSetFunction( gdi_display, physDev->gc, GXcopy );
1082 XDrawPoint( gdi_display, physDev->drawable, physDev->gc,
1083 physDev->dc_rect.left + pt.x, physDev->dc_rect.top + pt.y );
1084 wine_tsx11_unlock();
1086 /* Update the DIBSection from the pixmap */
1087 X11DRV_UnlockDIBSection(physDev, TRUE);
1089 return X11DRV_PALETTE_ToLogical(physDev, pixel);
1093 /***********************************************************************
1094 * X11DRV_GetPixel
1096 COLORREF X11DRV_GetPixel( PHYSDEV dev, INT x, INT y )
1098 static Pixmap pixmap = 0;
1099 X11DRV_PDEVICE *physDev = get_x11drv_dev( dev );
1100 XImage * image;
1101 int pixel;
1102 POINT pt;
1103 BOOL memdc = (GetObjectType(dev->hdc) == OBJ_MEMDC);
1105 pt.x = x;
1106 pt.y = y;
1107 LPtoDP( dev->hdc, &pt, 1 );
1109 /* Update the pixmap from the DIB section */
1110 X11DRV_LockDIBSection(physDev, DIB_Status_GdiMod);
1112 wine_tsx11_lock();
1113 if (memdc)
1115 image = XGetImage( gdi_display, physDev->drawable,
1116 physDev->dc_rect.left + pt.x, physDev->dc_rect.top + pt.y,
1117 1, 1, AllPlanes, ZPixmap );
1119 else
1121 /* If we are reading from the screen, use a temporary copy */
1122 /* to avoid a BadMatch error */
1123 if (!pixmap) pixmap = XCreatePixmap( gdi_display, root_window,
1124 1, 1, physDev->depth );
1125 XCopyArea( gdi_display, physDev->drawable, pixmap, get_bitmap_gc(physDev->depth),
1126 physDev->dc_rect.left + pt.x, physDev->dc_rect.top + pt.y, 1, 1, 0, 0 );
1127 image = XGetImage( gdi_display, pixmap, 0, 0, 1, 1, AllPlanes, ZPixmap );
1129 pixel = XGetPixel( image, 0, 0 );
1130 XDestroyImage( image );
1131 wine_tsx11_unlock();
1133 /* Update the DIBSection from the pixmap */
1134 X11DRV_UnlockDIBSection(physDev, FALSE);
1135 if( physDev->depth > 1)
1136 pixel = X11DRV_PALETTE_ToLogical(physDev, pixel);
1137 else
1138 /* monochrome bitmaps return black or white */
1139 if( pixel) pixel = 0xffffff;
1140 return pixel;
1145 /***********************************************************************
1146 * X11DRV_PaintRgn
1148 BOOL X11DRV_PaintRgn( PHYSDEV dev, HRGN hrgn )
1150 X11DRV_PDEVICE *physDev = get_x11drv_dev( dev );
1152 if (X11DRV_SetupGCForBrush( physDev ))
1154 unsigned int i;
1155 XRectangle *rect;
1156 RGNDATA *data = X11DRV_GetRegionData( hrgn, dev->hdc );
1158 if (!data) return FALSE;
1159 rect = (XRectangle *)data->Buffer;
1160 for (i = 0; i < data->rdh.nCount; i++)
1162 rect[i].x += physDev->dc_rect.left;
1163 rect[i].y += physDev->dc_rect.top;
1166 X11DRV_LockDIBSection(physDev, DIB_Status_GdiMod);
1167 wine_tsx11_lock();
1168 XFillRectangles( gdi_display, physDev->drawable, physDev->gc, rect, data->rdh.nCount );
1169 wine_tsx11_unlock();
1170 X11DRV_UnlockDIBSection(physDev, TRUE);
1171 HeapFree( GetProcessHeap(), 0, data );
1173 return TRUE;
1176 /**********************************************************************
1177 * X11DRV_Polyline
1179 BOOL X11DRV_Polyline( PHYSDEV dev, const POINT* pt, INT count )
1181 X11DRV_PDEVICE *physDev = get_x11drv_dev( dev );
1182 int i;
1183 XPoint *points;
1185 if (!(points = HeapAlloc( GetProcessHeap(), 0, sizeof(XPoint) * count )))
1187 WARN("No memory to convert POINTs to XPoints!\n");
1188 return FALSE;
1190 for (i = 0; i < count; i++)
1192 POINT tmp = pt[i];
1193 LPtoDP(dev->hdc, &tmp, 1);
1194 points[i].x = physDev->dc_rect.left + tmp.x;
1195 points[i].y = physDev->dc_rect.top + tmp.y;
1198 if (X11DRV_SetupGCForPen ( physDev ))
1200 X11DRV_LockDIBSection(physDev, DIB_Status_GdiMod);
1201 wine_tsx11_lock();
1202 XDrawLines( gdi_display, physDev->drawable, physDev->gc,
1203 points, count, CoordModeOrigin );
1204 wine_tsx11_unlock();
1205 X11DRV_UnlockDIBSection(physDev, TRUE);
1208 HeapFree( GetProcessHeap(), 0, points );
1209 return TRUE;
1213 /**********************************************************************
1214 * X11DRV_Polygon
1216 BOOL X11DRV_Polygon( PHYSDEV dev, const POINT* pt, INT count )
1218 X11DRV_PDEVICE *physDev = get_x11drv_dev( dev );
1219 int i;
1220 XPoint *points;
1221 BOOL update = FALSE;
1223 if (!(points = HeapAlloc( GetProcessHeap(), 0, sizeof(XPoint) * (count+1) )))
1225 WARN("No memory to convert POINTs to XPoints!\n");
1226 return FALSE;
1228 for (i = 0; i < count; i++)
1230 POINT tmp = pt[i];
1231 LPtoDP(dev->hdc, &tmp, 1);
1232 points[i].x = physDev->dc_rect.left + tmp.x;
1233 points[i].y = physDev->dc_rect.top + tmp.y;
1235 points[count] = points[0];
1237 /* Update the pixmap from the DIB section */
1238 X11DRV_LockDIBSection(physDev, DIB_Status_GdiMod);
1240 if (X11DRV_SetupGCForBrush( physDev ))
1242 wine_tsx11_lock();
1243 XFillPolygon( gdi_display, physDev->drawable, physDev->gc,
1244 points, count+1, Complex, CoordModeOrigin);
1245 wine_tsx11_unlock();
1246 update = TRUE;
1248 if (X11DRV_SetupGCForPen ( physDev ))
1250 wine_tsx11_lock();
1251 XDrawLines( gdi_display, physDev->drawable, physDev->gc,
1252 points, count+1, CoordModeOrigin );
1253 wine_tsx11_unlock();
1254 update = TRUE;
1257 /* Update the DIBSection from the pixmap */
1258 X11DRV_UnlockDIBSection(physDev, update);
1260 HeapFree( GetProcessHeap(), 0, points );
1261 return TRUE;
1265 /**********************************************************************
1266 * X11DRV_PolyPolygon
1268 BOOL X11DRV_PolyPolygon( PHYSDEV dev, const POINT* pt, const INT* counts, UINT polygons )
1270 X11DRV_PDEVICE *physDev = get_x11drv_dev( dev );
1271 HRGN hrgn;
1273 /* FIXME: The points should be converted to device coords before */
1274 /* creating the region. */
1276 hrgn = CreatePolyPolygonRgn( pt, counts, polygons, GetPolyFillMode( dev->hdc ) );
1277 X11DRV_PaintRgn( dev, hrgn );
1278 DeleteObject( hrgn );
1280 /* Draw the outline of the polygons */
1282 if (X11DRV_SetupGCForPen ( physDev ))
1284 unsigned int i;
1285 int j, max = 0;
1286 XPoint *points;
1288 /* Update the pixmap from the DIB section */
1289 X11DRV_LockDIBSection(physDev, DIB_Status_GdiMod);
1291 for (i = 0; i < polygons; i++) if (counts[i] > max) max = counts[i];
1292 if (!(points = HeapAlloc( GetProcessHeap(), 0, sizeof(XPoint) * (max+1) )))
1294 WARN("No memory to convert POINTs to XPoints!\n");
1295 return FALSE;
1297 for (i = 0; i < polygons; i++)
1299 for (j = 0; j < counts[i]; j++)
1301 POINT tmp = *pt;
1302 LPtoDP(dev->hdc, &tmp, 1);
1303 points[j].x = physDev->dc_rect.left + tmp.x;
1304 points[j].y = physDev->dc_rect.top + tmp.y;
1305 pt++;
1307 points[j] = points[0];
1308 wine_tsx11_lock();
1309 XDrawLines( gdi_display, physDev->drawable, physDev->gc,
1310 points, j + 1, CoordModeOrigin );
1311 wine_tsx11_unlock();
1314 /* Update the DIBSection of the dc's bitmap */
1315 X11DRV_UnlockDIBSection(physDev, TRUE);
1317 HeapFree( GetProcessHeap(), 0, points );
1319 return TRUE;
1323 /**********************************************************************
1324 * X11DRV_PolyPolyline
1326 BOOL X11DRV_PolyPolyline( PHYSDEV dev, const POINT* pt, const DWORD* counts, DWORD polylines )
1328 X11DRV_PDEVICE *physDev = get_x11drv_dev( dev );
1330 if (X11DRV_SetupGCForPen ( physDev ))
1332 unsigned int i, j, max = 0;
1333 XPoint *points;
1335 /* Update the pixmap from the DIB section */
1336 X11DRV_LockDIBSection(physDev, DIB_Status_GdiMod);
1338 for (i = 0; i < polylines; i++) if (counts[i] > max) max = counts[i];
1339 if (!(points = HeapAlloc( GetProcessHeap(), 0, sizeof(XPoint) * max )))
1341 WARN("No memory to convert POINTs to XPoints!\n");
1342 return FALSE;
1344 for (i = 0; i < polylines; i++)
1346 for (j = 0; j < counts[i]; j++)
1348 POINT tmp = *pt;
1349 LPtoDP(dev->hdc, &tmp, 1);
1350 points[j].x = physDev->dc_rect.left + tmp.x;
1351 points[j].y = physDev->dc_rect.top + tmp.y;
1352 pt++;
1354 wine_tsx11_lock();
1355 XDrawLines( gdi_display, physDev->drawable, physDev->gc,
1356 points, j, CoordModeOrigin );
1357 wine_tsx11_unlock();
1360 /* Update the DIBSection of the dc's bitmap */
1361 X11DRV_UnlockDIBSection(physDev, TRUE);
1363 HeapFree( GetProcessHeap(), 0, points );
1365 return TRUE;
1369 /**********************************************************************
1370 * X11DRV_InternalFloodFill
1372 * Internal helper function for flood fill.
1373 * (xorg,yorg) is the origin of the X image relative to the drawable.
1374 * (x,y) is relative to the origin of the X image.
1376 static void X11DRV_InternalFloodFill(XImage *image, X11DRV_PDEVICE *physDev,
1377 int x, int y,
1378 int xOrg, int yOrg,
1379 unsigned long pixel, WORD fillType )
1381 int left, right;
1383 #define TO_FLOOD(x,y) ((fillType == FLOODFILLBORDER) ? \
1384 (XGetPixel(image,x,y) != pixel) : \
1385 (XGetPixel(image,x,y) == pixel))
1387 if (!TO_FLOOD(x,y)) return;
1389 /* Find left and right boundaries */
1391 left = right = x;
1392 while ((left > 0) && TO_FLOOD( left-1, y )) left--;
1393 while ((right < image->width) && TO_FLOOD( right, y )) right++;
1394 XFillRectangle( gdi_display, physDev->drawable, physDev->gc,
1395 xOrg + left, yOrg + y, right-left, 1 );
1397 /* Set the pixels of this line so we don't fill it again */
1399 for (x = left; x < right; x++)
1401 if (fillType == FLOODFILLBORDER) XPutPixel( image, x, y, pixel );
1402 else XPutPixel( image, x, y, ~pixel );
1405 /* Fill the line above */
1407 if (--y >= 0)
1409 x = left;
1410 while (x < right)
1412 while ((x < right) && !TO_FLOOD(x,y)) x++;
1413 if (x >= right) break;
1414 while ((x < right) && TO_FLOOD(x,y)) x++;
1415 X11DRV_InternalFloodFill(image, physDev, x-1, y,
1416 xOrg, yOrg, pixel, fillType );
1420 /* Fill the line below */
1422 if ((y += 2) < image->height)
1424 x = left;
1425 while (x < right)
1427 while ((x < right) && !TO_FLOOD(x,y)) x++;
1428 if (x >= right) break;
1429 while ((x < right) && TO_FLOOD(x,y)) x++;
1430 X11DRV_InternalFloodFill(image, physDev, x-1, y,
1431 xOrg, yOrg, pixel, fillType );
1434 #undef TO_FLOOD
1438 static int ExtFloodFillXGetImageErrorHandler( Display *dpy, XErrorEvent *event, void *arg )
1440 return (event->request_code == X_GetImage && event->error_code == BadMatch);
1443 /**********************************************************************
1444 * X11DRV_ExtFloodFill
1446 BOOL X11DRV_ExtFloodFill( PHYSDEV dev, INT x, INT y, COLORREF color, UINT fillType )
1448 X11DRV_PDEVICE *physDev = get_x11drv_dev( dev );
1449 XImage *image;
1450 RECT rect;
1451 POINT pt;
1453 TRACE("X11DRV_ExtFloodFill %d,%d %06x %d\n", x, y, color, fillType );
1455 pt.x = x;
1456 pt.y = y;
1457 LPtoDP( dev->hdc, &pt, 1 );
1458 if (!PtInRegion( physDev->region, pt.x, pt.y )) return FALSE;
1459 GetRgnBox( physDev->region, &rect );
1461 X11DRV_expect_error( gdi_display, ExtFloodFillXGetImageErrorHandler, NULL );
1462 image = XGetImage( gdi_display, physDev->drawable,
1463 physDev->dc_rect.left + rect.left, physDev->dc_rect.top + rect.top,
1464 rect.right - rect.left, rect.bottom - rect.top,
1465 AllPlanes, ZPixmap );
1466 if(X11DRV_check_error()) image = NULL;
1467 if (!image) return FALSE;
1469 if (X11DRV_SetupGCForBrush( physDev ))
1471 unsigned long pixel = X11DRV_PALETTE_ToPhysical( physDev, color );
1473 /* Update the pixmap from the DIB section */
1474 X11DRV_LockDIBSection(physDev, DIB_Status_GdiMod);
1476 /* ROP mode is always GXcopy for flood-fill */
1477 wine_tsx11_lock();
1478 XSetFunction( gdi_display, physDev->gc, GXcopy );
1479 X11DRV_InternalFloodFill(image, physDev,
1480 pt.x - rect.left,
1481 pt.y - rect.top,
1482 physDev->dc_rect.left + rect.left,
1483 physDev->dc_rect.top + rect.top,
1484 pixel, fillType );
1485 wine_tsx11_unlock();
1486 /* Update the DIBSection of the dc's bitmap */
1487 X11DRV_UnlockDIBSection(physDev, TRUE);
1490 wine_tsx11_lock();
1491 XDestroyImage( image );
1492 wine_tsx11_unlock();
1493 return TRUE;
1496 /**********************************************************************
1497 * X11DRV_SetBkColor
1499 COLORREF X11DRV_SetBkColor( PHYSDEV dev, COLORREF color )
1501 X11DRV_PDEVICE *physDev = get_x11drv_dev( dev );
1503 physDev->backgroundPixel = X11DRV_PALETTE_ToPhysical( physDev, color );
1504 return color;
1507 /**********************************************************************
1508 * X11DRV_SetTextColor
1510 COLORREF X11DRV_SetTextColor( PHYSDEV dev, COLORREF color )
1512 X11DRV_PDEVICE *physDev = get_x11drv_dev( dev );
1514 physDev->textPixel = X11DRV_PALETTE_ToPhysical( physDev, color );
1515 return color;
1519 static unsigned char *get_icm_profile( unsigned long *size )
1521 Atom type;
1522 int format;
1523 unsigned long count, remaining;
1524 unsigned char *profile, *ret = NULL;
1526 wine_tsx11_lock();
1527 XGetWindowProperty( gdi_display, DefaultRootWindow(gdi_display),
1528 x11drv_atom(_ICC_PROFILE), 0, ~0UL, False, AnyPropertyType,
1529 &type, &format, &count, &remaining, &profile );
1530 *size = get_property_size( format, count );
1531 if (format && count)
1533 if ((ret = HeapAlloc( GetProcessHeap(), 0, *size ))) memcpy( ret, profile, *size );
1534 XFree( profile );
1536 wine_tsx11_unlock();
1537 return ret;
1540 typedef struct
1542 unsigned int unknown[6];
1543 unsigned int state[5];
1544 unsigned int count[2];
1545 unsigned char buffer[64];
1546 } sha_ctx;
1548 extern void WINAPI A_SHAInit( sha_ctx * );
1549 extern void WINAPI A_SHAUpdate( sha_ctx *, const unsigned char *, unsigned int );
1550 extern void WINAPI A_SHAFinal( sha_ctx *, unsigned char * );
1552 static const WCHAR mntr_key[] =
1553 {'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
1554 'W','i','n','d','o','w','s',' ','N','T','\\','C','u','r','r','e','n','t',
1555 'V','e','r','s','i','o','n','\\','I','C','M','\\','m','n','t','r',0};
1557 static const WCHAR color_path[] =
1558 {'\\','s','p','o','o','l','\\','d','r','i','v','e','r','s','\\','c','o','l','o','r','\\',0};
1560 /***********************************************************************
1561 * GetICMProfile (X11DRV.@)
1563 BOOL X11DRV_GetICMProfile( PHYSDEV dev, LPDWORD size, LPWSTR filename )
1565 static const WCHAR srgb[] =
1566 {'s','R','G','B',' ','C','o','l','o','r',' ','S','p','a','c','e',' ',
1567 'P','r','o','f','i','l','e','.','i','c','m',0};
1568 HKEY hkey;
1569 DWORD required, len;
1570 WCHAR profile[MAX_PATH], fullname[2*MAX_PATH + sizeof(color_path)/sizeof(WCHAR)];
1571 unsigned char *buffer;
1572 unsigned long buflen;
1574 if (!size) return FALSE;
1576 GetSystemDirectoryW( fullname, MAX_PATH );
1577 strcatW( fullname, color_path );
1579 len = sizeof(profile)/sizeof(WCHAR);
1580 if (!RegCreateKeyExW( HKEY_LOCAL_MACHINE, mntr_key, 0, NULL, 0, KEY_ALL_ACCESS, NULL, &hkey, NULL ) &&
1581 !RegEnumValueW( hkey, 0, profile, &len, NULL, NULL, NULL, NULL )) /* FIXME handle multiple values */
1583 strcatW( fullname, profile );
1584 RegCloseKey( hkey );
1586 else if ((buffer = get_icm_profile( &buflen )))
1588 static const WCHAR fmt[] = {'%','0','2','x',0};
1589 static const WCHAR icm[] = {'.','i','c','m',0};
1591 unsigned char sha1sum[20];
1592 unsigned int i;
1593 sha_ctx ctx;
1594 HANDLE file;
1596 A_SHAInit( &ctx );
1597 A_SHAUpdate( &ctx, buffer, buflen );
1598 A_SHAFinal( &ctx, sha1sum );
1600 for (i = 0; i < sizeof(sha1sum); i++) sprintfW( &profile[i * 2], fmt, sha1sum[i] );
1601 memcpy( &profile[i * 2], icm, sizeof(icm) );
1603 strcatW( fullname, profile );
1604 file = CreateFileW( fullname, GENERIC_WRITE, 0, NULL, CREATE_NEW, 0, 0 );
1605 if (file != INVALID_HANDLE_VALUE)
1607 DWORD written;
1609 if (!WriteFile( file, buffer, buflen, &written, NULL ) || written != buflen)
1610 ERR( "Unable to write color profile\n" );
1611 CloseHandle( file );
1613 HeapFree( GetProcessHeap(), 0, buffer );
1615 else strcatW( fullname, srgb );
1617 required = strlenW( fullname ) + 1;
1618 if (*size < required)
1620 *size = required;
1621 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1622 return FALSE;
1624 if (filename)
1626 strcpyW( filename, fullname );
1627 if (GetFileAttributesW( filename ) == INVALID_FILE_ATTRIBUTES)
1628 WARN( "color profile not found\n" );
1630 *size = required;
1631 return TRUE;
1634 /***********************************************************************
1635 * EnumICMProfiles (X11DRV.@)
1637 INT X11DRV_EnumICMProfiles( PHYSDEV dev, ICMENUMPROCW proc, LPARAM lparam )
1639 X11DRV_PDEVICE *physDev = get_x11drv_dev( dev );
1640 HKEY hkey;
1641 DWORD len_sysdir, len_path, len, index = 0;
1642 WCHAR sysdir[MAX_PATH], *profile;
1643 LONG res;
1644 INT ret;
1646 TRACE("%p, %p, %ld\n", physDev, proc, lparam);
1648 if (RegOpenKeyExW( HKEY_LOCAL_MACHINE, mntr_key, 0, KEY_ALL_ACCESS, &hkey ))
1649 return -1;
1651 len_sysdir = GetSystemDirectoryW( sysdir, MAX_PATH );
1652 len_path = len_sysdir + sizeof(color_path) / sizeof(color_path[0]) - 1;
1653 len = 64;
1654 for (;;)
1656 if (!(profile = HeapAlloc( GetProcessHeap(), 0, (len_path + len) * sizeof(WCHAR) )))
1658 RegCloseKey( hkey );
1659 return -1;
1661 res = RegEnumValueW( hkey, index, profile + len_path, &len, NULL, NULL, NULL, NULL );
1662 while (res == ERROR_MORE_DATA)
1664 len *= 2;
1665 HeapFree( GetProcessHeap(), 0, profile );
1666 if (!(profile = HeapAlloc( GetProcessHeap(), 0, (len_path + len) * sizeof(WCHAR) )))
1668 RegCloseKey( hkey );
1669 return -1;
1671 res = RegEnumValueW( hkey, index, profile + len_path, &len, NULL, NULL, NULL, NULL );
1673 if (res != ERROR_SUCCESS)
1675 HeapFree( GetProcessHeap(), 0, profile );
1676 break;
1678 memcpy( profile, sysdir, len_sysdir * sizeof(WCHAR) );
1679 memcpy( profile + len_sysdir, color_path, sizeof(color_path) - sizeof(WCHAR) );
1680 ret = proc( profile, lparam );
1681 HeapFree( GetProcessHeap(), 0, profile );
1682 if (!ret) break;
1683 index++;
1685 RegCloseKey( hkey );
1686 return -1;