winetest: Include language settings in OS info.
[wine/multimedia.git] / dlls / winex11.drv / graphics.c
blob7c4e0d588d2dcf1e138634ae9b7f2b617aef51f0
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 static void update_x11_clipping( X11DRV_PDEVICE *physDev )
190 RGNDATA *data;
192 if (!(data = X11DRV_GetRegionData( physDev->region, 0 ))) return;
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();
198 if (physDev->xrender) X11DRV_XRender_SetDeviceClipping(physDev, data);
199 HeapFree( GetProcessHeap(), 0, data );
202 /***********************************************************************
203 * add_extra_clipping_region
205 * Temporarily add a region to the current clipping region.
206 * The returned region must be restored with restore_clipping_region.
208 HRGN add_extra_clipping_region( X11DRV_PDEVICE *dev, HRGN rgn )
210 HRGN ret, clip;
212 if (!(clip = CreateRectRgn( 0, 0, 0, 0 ))) return 0;
213 CombineRgn( clip, dev->region, rgn, RGN_AND );
214 ret = dev->region;
215 dev->region = clip;
216 update_x11_clipping( dev );
217 return ret;
221 /***********************************************************************
222 * restore_clipping_region
224 void restore_clipping_region( X11DRV_PDEVICE *dev, HRGN rgn )
226 if (!rgn) return;
227 DeleteObject( dev->region );
228 dev->region = rgn;
229 update_x11_clipping( dev );
233 /***********************************************************************
234 * X11DRV_SetDeviceClipping
236 void X11DRV_SetDeviceClipping( PHYSDEV dev, HRGN vis_rgn, HRGN clip_rgn )
238 X11DRV_PDEVICE *physDev = get_x11drv_dev( dev );
240 CombineRgn( physDev->region, vis_rgn, clip_rgn, clip_rgn ? RGN_AND : RGN_COPY );
241 update_x11_clipping( physDev );
245 /***********************************************************************
246 * X11DRV_SetupGCForPatBlt
248 * Setup the GC for a PatBlt operation using current brush.
249 * If fMapColors is TRUE, X pixels are mapped to Windows colors.
250 * Return FALSE if brush is BS_NULL, TRUE otherwise.
252 BOOL X11DRV_SetupGCForPatBlt( X11DRV_PDEVICE *physDev, GC gc, BOOL fMapColors )
254 XGCValues val;
255 unsigned long mask;
256 Pixmap pixmap = 0;
257 POINT pt;
259 if (physDev->brush.style == BS_NULL) return FALSE;
260 if (physDev->brush.pixel == -1)
262 /* Special case used for monochrome pattern brushes.
263 * We need to swap foreground and background because
264 * Windows does it the wrong way...
266 val.foreground = physDev->backgroundPixel;
267 val.background = physDev->textPixel;
269 else
271 val.foreground = physDev->brush.pixel;
272 val.background = physDev->backgroundPixel;
274 if (fMapColors && X11DRV_PALETTE_XPixelToPalette)
276 val.foreground = X11DRV_PALETTE_XPixelToPalette[val.foreground];
277 val.background = X11DRV_PALETTE_XPixelToPalette[val.background];
280 val.function = X11DRV_XROPfunction[GetROP2(physDev->dev.hdc)-1];
282 ** Let's replace GXinvert by GXxor with (black xor white)
283 ** This solves the selection color and leak problems in excel
284 ** FIXME : Let's do that only if we work with X-pixels, not with Win-pixels
286 if (val.function == GXinvert)
288 val.foreground = (WhitePixel( gdi_display, DefaultScreen(gdi_display) ) ^
289 BlackPixel( gdi_display, DefaultScreen(gdi_display) ));
290 val.function = GXxor;
292 val.fill_style = physDev->brush.fillStyle;
293 switch(val.fill_style)
295 case FillStippled:
296 case FillOpaqueStippled:
297 if (GetBkMode(physDev->dev.hdc)==OPAQUE) val.fill_style = FillOpaqueStippled;
298 val.stipple = physDev->brush.pixmap;
299 mask = GCStipple;
300 break;
302 case FillTiled:
303 if (fMapColors && X11DRV_PALETTE_XPixelToPalette)
305 register int x, y;
306 XImage *image;
307 wine_tsx11_lock();
308 pixmap = XCreatePixmap( gdi_display, root_window, 8, 8, physDev->depth );
309 image = XGetImage( gdi_display, physDev->brush.pixmap, 0, 0, 8, 8,
310 AllPlanes, ZPixmap );
311 for (y = 0; y < 8; y++)
312 for (x = 0; x < 8; x++)
313 XPutPixel( image, x, y,
314 X11DRV_PALETTE_XPixelToPalette[XGetPixel( image, x, y)] );
315 XPutImage( gdi_display, pixmap, gc, image, 0, 0, 0, 0, 8, 8 );
316 XDestroyImage( image );
317 wine_tsx11_unlock();
318 val.tile = pixmap;
320 else val.tile = physDev->brush.pixmap;
321 mask = GCTile;
322 break;
324 default:
325 mask = 0;
326 break;
328 GetBrushOrgEx( physDev->dev.hdc, &pt );
329 val.ts_x_origin = physDev->dc_rect.left + pt.x;
330 val.ts_y_origin = physDev->dc_rect.top + pt.y;
331 val.fill_rule = (GetPolyFillMode(physDev->dev.hdc) == WINDING) ? WindingRule : EvenOddRule;
332 wine_tsx11_lock();
333 XChangeGC( gdi_display, gc,
334 GCFunction | GCForeground | GCBackground | GCFillStyle |
335 GCFillRule | GCTileStipXOrigin | GCTileStipYOrigin | mask,
336 &val );
337 if (pixmap) XFreePixmap( gdi_display, pixmap );
338 wine_tsx11_unlock();
339 return TRUE;
343 /***********************************************************************
344 * X11DRV_SetupGCForBrush
346 * Setup physDev->gc for drawing operations using current brush.
347 * Return FALSE if brush is BS_NULL, TRUE otherwise.
349 BOOL X11DRV_SetupGCForBrush( X11DRV_PDEVICE *physDev )
351 return X11DRV_SetupGCForPatBlt( physDev, physDev->gc, FALSE );
355 /***********************************************************************
356 * X11DRV_SetupGCForPen
358 * Setup physDev->gc for drawing operations using current pen.
359 * Return FALSE if pen is PS_NULL, TRUE otherwise.
361 static BOOL X11DRV_SetupGCForPen( X11DRV_PDEVICE *physDev )
363 XGCValues val;
364 UINT rop2 = GetROP2(physDev->dev.hdc);
366 if (physDev->pen.style == PS_NULL) return FALSE;
368 switch (rop2)
370 case R2_BLACK :
371 val.foreground = BlackPixel( gdi_display, DefaultScreen(gdi_display) );
372 val.function = GXcopy;
373 break;
374 case R2_WHITE :
375 val.foreground = WhitePixel( gdi_display, DefaultScreen(gdi_display) );
376 val.function = GXcopy;
377 break;
378 case R2_XORPEN :
379 val.foreground = physDev->pen.pixel;
380 /* It is very unlikely someone wants to XOR with 0 */
381 /* This fixes the rubber-drawings in paintbrush */
382 if (val.foreground == 0)
383 val.foreground = (WhitePixel( gdi_display, DefaultScreen(gdi_display) ) ^
384 BlackPixel( gdi_display, DefaultScreen(gdi_display) ));
385 val.function = GXxor;
386 break;
387 default :
388 val.foreground = physDev->pen.pixel;
389 val.function = X11DRV_XROPfunction[rop2-1];
391 val.background = physDev->backgroundPixel;
392 val.fill_style = FillSolid;
393 val.line_width = physDev->pen.width;
394 if (val.line_width <= 1) {
395 val.cap_style = CapNotLast;
396 } else {
397 switch (physDev->pen.endcap)
399 case PS_ENDCAP_SQUARE:
400 val.cap_style = CapProjecting;
401 break;
402 case PS_ENDCAP_FLAT:
403 val.cap_style = CapButt;
404 break;
405 case PS_ENDCAP_ROUND:
406 default:
407 val.cap_style = CapRound;
410 switch (physDev->pen.linejoin)
412 case PS_JOIN_BEVEL:
413 val.join_style = JoinBevel;
414 break;
415 case PS_JOIN_MITER:
416 val.join_style = JoinMiter;
417 break;
418 case PS_JOIN_ROUND:
419 default:
420 val.join_style = JoinRound;
423 if (physDev->pen.dash_len)
424 val.line_style = ((GetBkMode(physDev->dev.hdc) == OPAQUE) && (!physDev->pen.ext))
425 ? LineDoubleDash : LineOnOffDash;
426 else
427 val.line_style = LineSolid;
429 wine_tsx11_lock();
430 if (physDev->pen.dash_len)
431 XSetDashes( gdi_display, physDev->gc, 0, physDev->pen.dashes, physDev->pen.dash_len );
432 XChangeGC( gdi_display, physDev->gc,
433 GCFunction | GCForeground | GCBackground | GCLineWidth |
434 GCLineStyle | GCCapStyle | GCJoinStyle | GCFillStyle, &val );
435 wine_tsx11_unlock();
436 return TRUE;
440 /***********************************************************************
441 * X11DRV_SetupGCForText
443 * Setup physDev->gc for text drawing operations.
444 * Return FALSE if the font is null, TRUE otherwise.
446 BOOL X11DRV_SetupGCForText( X11DRV_PDEVICE *physDev )
448 XFontStruct* xfs = XFONT_GetFontStruct( physDev->font );
450 if( xfs )
452 XGCValues val;
454 val.function = GXcopy; /* Text is always GXcopy */
455 val.foreground = physDev->textPixel;
456 val.background = physDev->backgroundPixel;
457 val.fill_style = FillSolid;
458 val.font = xfs->fid;
460 wine_tsx11_lock();
461 XChangeGC( gdi_display, physDev->gc,
462 GCFunction | GCForeground | GCBackground | GCFillStyle |
463 GCFont, &val );
464 wine_tsx11_unlock();
465 return TRUE;
467 WARN("Physical font failure\n" );
468 return FALSE;
471 /***********************************************************************
472 * X11DRV_XWStoDS
474 * Performs a world-to-viewport transformation on the specified width.
476 INT X11DRV_XWStoDS( X11DRV_PDEVICE *physDev, INT width )
478 POINT pt[2];
480 pt[0].x = 0;
481 pt[0].y = 0;
482 pt[1].x = width;
483 pt[1].y = 0;
484 LPtoDP( physDev->dev.hdc, pt, 2 );
485 return pt[1].x - pt[0].x;
488 /***********************************************************************
489 * X11DRV_YWStoDS
491 * Performs a world-to-viewport transformation on the specified height.
493 INT X11DRV_YWStoDS( X11DRV_PDEVICE *physDev, INT height )
495 POINT pt[2];
497 pt[0].x = 0;
498 pt[0].y = 0;
499 pt[1].x = 0;
500 pt[1].y = height;
501 LPtoDP( physDev->dev.hdc, pt, 2 );
502 return pt[1].y - pt[0].y;
505 /***********************************************************************
506 * X11DRV_LineTo
508 BOOL X11DRV_LineTo( PHYSDEV dev, INT x, INT y )
510 X11DRV_PDEVICE *physDev = get_x11drv_dev( dev );
511 POINT pt[2];
513 if (X11DRV_SetupGCForPen( physDev )) {
514 /* Update the pixmap from the DIB section */
515 X11DRV_LockDIBSection(physDev, DIB_Status_GdiMod);
517 GetCurrentPositionEx( dev->hdc, &pt[0] );
518 pt[1].x = x;
519 pt[1].y = y;
520 LPtoDP( dev->hdc, pt, 2 );
522 wine_tsx11_lock();
523 XDrawLine(gdi_display, physDev->drawable, physDev->gc,
524 physDev->dc_rect.left + pt[0].x, physDev->dc_rect.top + pt[0].y,
525 physDev->dc_rect.left + pt[1].x, physDev->dc_rect.top + pt[1].y );
526 wine_tsx11_unlock();
528 /* Update the DIBSection from the pixmap */
529 X11DRV_UnlockDIBSection(physDev, TRUE);
531 return TRUE;
536 /***********************************************************************
537 * X11DRV_DrawArc
539 * Helper functions for Arc(), Chord() and Pie().
540 * 'lines' is the number of lines to draw: 0 for Arc, 1 for Chord, 2 for Pie.
543 static BOOL X11DRV_DrawArc( PHYSDEV dev, INT left, INT top, INT right, INT bottom,
544 INT xstart, INT ystart, INT xend, INT yend, INT lines )
546 X11DRV_PDEVICE *physDev = get_x11drv_dev( dev );
547 INT xcenter, ycenter, istart_angle, idiff_angle;
548 INT width, oldwidth;
549 double start_angle, end_angle;
550 XPoint points[4];
551 BOOL update = FALSE;
552 POINT start, end;
553 RECT rc = get_device_rect( dev->hdc, left, top, right, bottom );
555 start.x = xstart;
556 start.y = ystart;
557 end.x = xend;
558 end.y = yend;
559 LPtoDP(dev->hdc, &start, 1);
560 LPtoDP(dev->hdc, &end, 1);
562 if ((rc.left == rc.right) || (rc.top == rc.bottom)
563 ||(lines && ((rc.right-rc.left==1)||(rc.bottom-rc.top==1)))) return TRUE;
565 if (GetArcDirection( dev->hdc ) == AD_CLOCKWISE)
566 { POINT tmp = start; start = end; end = tmp; }
568 oldwidth = width = physDev->pen.width;
569 if (!width) width = 1;
570 if(physDev->pen.style == PS_NULL) width = 0;
572 if (physDev->pen.style == PS_INSIDEFRAME)
574 if (2*width > (rc.right-rc.left)) width=(rc.right-rc.left + 1)/2;
575 if (2*width > (rc.bottom-rc.top)) width=(rc.bottom-rc.top + 1)/2;
576 rc.left += width / 2;
577 rc.right -= (width - 1) / 2;
578 rc.top += width / 2;
579 rc.bottom -= (width - 1) / 2;
581 if(width == 0) width = 1; /* more accurate */
582 physDev->pen.width = width;
584 xcenter = (rc.right + rc.left) / 2;
585 ycenter = (rc.bottom + rc.top) / 2;
586 start_angle = atan2( (double)(ycenter-start.y)*(rc.right-rc.left),
587 (double)(start.x-xcenter)*(rc.bottom-rc.top) );
588 end_angle = atan2( (double)(ycenter-end.y)*(rc.right-rc.left),
589 (double)(end.x-xcenter)*(rc.bottom-rc.top) );
590 if ((start.x==end.x)&&(start.y==end.y))
591 { /* A lazy program delivers xstart=xend=ystart=yend=0) */
592 start_angle = 0;
593 end_angle = 2* PI;
595 else /* notorious cases */
596 if ((start_angle == PI)&&( end_angle <0))
597 start_angle = - PI;
598 else
599 if ((end_angle == PI)&&( start_angle <0))
600 end_angle = - PI;
601 istart_angle = (INT)(start_angle * 180 * 64 / PI + 0.5);
602 idiff_angle = (INT)((end_angle - start_angle) * 180 * 64 / PI + 0.5);
603 if (idiff_angle <= 0) idiff_angle += 360 * 64;
605 /* Update the pixmap from the DIB section */
606 X11DRV_LockDIBSection(physDev, DIB_Status_GdiMod);
608 /* Fill arc with brush if Chord() or Pie() */
610 if ((lines > 0) && X11DRV_SetupGCForBrush( physDev )) {
611 wine_tsx11_lock();
612 XSetArcMode( gdi_display, physDev->gc, (lines==1) ? ArcChord : ArcPieSlice);
613 XFillArc( gdi_display, physDev->drawable, physDev->gc,
614 physDev->dc_rect.left + rc.left, physDev->dc_rect.top + rc.top,
615 rc.right-rc.left-1, rc.bottom-rc.top-1, istart_angle, idiff_angle );
616 wine_tsx11_unlock();
617 update = TRUE;
620 /* Draw arc and lines */
622 if (X11DRV_SetupGCForPen( physDev ))
624 wine_tsx11_lock();
625 XDrawArc( gdi_display, physDev->drawable, physDev->gc,
626 physDev->dc_rect.left + rc.left, physDev->dc_rect.top + rc.top,
627 rc.right-rc.left-1, rc.bottom-rc.top-1, istart_angle, idiff_angle );
628 if (lines) {
629 /* use the truncated values */
630 start_angle=(double)istart_angle*PI/64./180.;
631 end_angle=(double)(istart_angle+idiff_angle)*PI/64./180.;
632 /* calculate the endpoints and round correctly */
633 points[0].x = (int) floor(physDev->dc_rect.left + (rc.right+rc.left)/2.0 +
634 cos(start_angle) * (rc.right-rc.left-width*2+2) / 2. + 0.5);
635 points[0].y = (int) floor(physDev->dc_rect.top + (rc.top+rc.bottom)/2.0 -
636 sin(start_angle) * (rc.bottom-rc.top-width*2+2) / 2. + 0.5);
637 points[1].x = (int) floor(physDev->dc_rect.left + (rc.right+rc.left)/2.0 +
638 cos(end_angle) * (rc.right-rc.left-width*2+2) / 2. + 0.5);
639 points[1].y = (int) floor(physDev->dc_rect.top + (rc.top+rc.bottom)/2.0 -
640 sin(end_angle) * (rc.bottom-rc.top-width*2+2) / 2. + 0.5);
642 /* OK, this stuff is optimized for Xfree86
643 * which is probably the server most used by
644 * wine users. Other X servers will not
645 * display correctly. (eXceed for instance)
646 * so if you feel you must make changes, make sure that
647 * you either use Xfree86 or separate your changes
648 * from these (compile switch or whatever)
650 if (lines == 2) {
651 INT dx1,dy1;
652 points[3] = points[1];
653 points[1].x = physDev->dc_rect.left + xcenter;
654 points[1].y = physDev->dc_rect.top + ycenter;
655 points[2] = points[1];
656 dx1=points[1].x-points[0].x;
657 dy1=points[1].y-points[0].y;
658 if(((rc.top-rc.bottom) | -2) == -2)
659 if(dy1>0) points[1].y--;
660 if(dx1<0) {
661 if (((-dx1)*64)<=ABS(dy1)*37) points[0].x--;
662 if(((-dx1*9))<(dy1*16)) points[0].y--;
663 if( dy1<0 && ((dx1*9)) < (dy1*16)) points[0].y--;
664 } else {
665 if(dy1 < 0) points[0].y--;
666 if(((rc.right-rc.left) | -2) == -2) points[1].x--;
668 dx1=points[3].x-points[2].x;
669 dy1=points[3].y-points[2].y;
670 if(((rc.top-rc.bottom) | -2 ) == -2)
671 if(dy1 < 0) points[2].y--;
672 if( dx1<0){
673 if( dy1>0) points[3].y--;
674 if(((rc.right-rc.left) | -2) == -2 ) points[2].x--;
675 }else {
676 points[3].y--;
677 if( dx1 * 64 < dy1 * -37 ) points[3].x--;
679 lines++;
681 XDrawLines( gdi_display, physDev->drawable, physDev->gc,
682 points, lines+1, CoordModeOrigin );
684 wine_tsx11_unlock();
685 update = TRUE;
688 /* Update the DIBSection of the pixmap */
689 X11DRV_UnlockDIBSection(physDev, update);
691 physDev->pen.width = oldwidth;
692 return TRUE;
696 /***********************************************************************
697 * X11DRV_Arc
699 BOOL X11DRV_Arc( PHYSDEV dev, INT left, INT top, INT right, INT bottom,
700 INT xstart, INT ystart, INT xend, INT yend )
702 return X11DRV_DrawArc( dev, left, top, right, bottom, xstart, ystart, xend, yend, 0 );
706 /***********************************************************************
707 * X11DRV_Pie
709 BOOL X11DRV_Pie( PHYSDEV dev, INT left, INT top, INT right, INT bottom,
710 INT xstart, INT ystart, INT xend, INT yend )
712 return X11DRV_DrawArc( dev, left, top, right, bottom, xstart, ystart, xend, yend, 2 );
715 /***********************************************************************
716 * X11DRV_Chord
718 BOOL X11DRV_Chord( 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, 1 );
725 /***********************************************************************
726 * X11DRV_Ellipse
728 BOOL X11DRV_Ellipse( PHYSDEV dev, INT left, INT top, INT right, INT bottom )
730 X11DRV_PDEVICE *physDev = get_x11drv_dev( dev );
731 INT width, oldwidth;
732 BOOL update = FALSE;
733 RECT rc = get_device_rect( dev->hdc, left, top, right, bottom );
735 if ((rc.left == rc.right) || (rc.top == rc.bottom)) return TRUE;
737 oldwidth = width = physDev->pen.width;
738 if (!width) width = 1;
739 if(physDev->pen.style == PS_NULL) width = 0;
741 if (physDev->pen.style == PS_INSIDEFRAME)
743 if (2*width > (rc.right-rc.left)) width=(rc.right-rc.left + 1)/2;
744 if (2*width > (rc.bottom-rc.top)) width=(rc.bottom-rc.top + 1)/2;
745 rc.left += width / 2;
746 rc.right -= (width - 1) / 2;
747 rc.top += width / 2;
748 rc.bottom -= (width - 1) / 2;
750 if(width == 0) width = 1; /* more accurate */
751 physDev->pen.width = width;
753 /* Update the pixmap from the DIB section */
754 X11DRV_LockDIBSection(physDev, DIB_Status_GdiMod);
756 if (X11DRV_SetupGCForBrush( physDev ))
758 wine_tsx11_lock();
759 XFillArc( gdi_display, physDev->drawable, physDev->gc,
760 physDev->dc_rect.left + rc.left, physDev->dc_rect.top + rc.top,
761 rc.right-rc.left-1, rc.bottom-rc.top-1, 0, 360*64 );
762 wine_tsx11_unlock();
763 update = TRUE;
765 if (X11DRV_SetupGCForPen( physDev ))
767 wine_tsx11_lock();
768 XDrawArc( 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;
775 /* Update the DIBSection from the pixmap */
776 X11DRV_UnlockDIBSection(physDev, update);
778 physDev->pen.width = oldwidth;
779 return TRUE;
783 /***********************************************************************
784 * X11DRV_Rectangle
786 BOOL X11DRV_Rectangle(PHYSDEV dev, INT left, INT top, INT right, INT bottom)
788 X11DRV_PDEVICE *physDev = get_x11drv_dev( dev );
789 INT width, oldwidth, oldjoinstyle;
790 BOOL update = FALSE;
791 RECT rc = get_device_rect( dev->hdc, left, top, right, bottom );
793 TRACE("(%d %d %d %d)\n", left, top, right, bottom);
795 if ((rc.left == rc.right) || (rc.top == rc.bottom)) return TRUE;
797 oldwidth = width = physDev->pen.width;
798 if (!width) width = 1;
799 if(physDev->pen.style == PS_NULL) width = 0;
801 if (physDev->pen.style == PS_INSIDEFRAME)
803 if (2*width > (rc.right-rc.left)) width=(rc.right-rc.left + 1)/2;
804 if (2*width > (rc.bottom-rc.top)) width=(rc.bottom-rc.top + 1)/2;
805 rc.left += width / 2;
806 rc.right -= (width - 1) / 2;
807 rc.top += width / 2;
808 rc.bottom -= (width - 1) / 2;
810 if(width == 1) width = 0;
811 physDev->pen.width = width;
812 oldjoinstyle = physDev->pen.linejoin;
813 if(physDev->pen.type != PS_GEOMETRIC)
814 physDev->pen.linejoin = PS_JOIN_MITER;
816 /* Update the pixmap from the DIB section */
817 X11DRV_LockDIBSection(physDev, DIB_Status_GdiMod);
819 if ((rc.right > rc.left + width) && (rc.bottom > rc.top + width))
821 if (X11DRV_SetupGCForBrush( physDev ))
823 wine_tsx11_lock();
824 XFillRectangle( gdi_display, physDev->drawable, physDev->gc,
825 physDev->dc_rect.left + rc.left + (width + 1) / 2,
826 physDev->dc_rect.top + rc.top + (width + 1) / 2,
827 rc.right-rc.left-width-1, rc.bottom-rc.top-width-1);
828 wine_tsx11_unlock();
829 update = TRUE;
832 if (X11DRV_SetupGCForPen( physDev ))
834 wine_tsx11_lock();
835 XDrawRectangle( gdi_display, physDev->drawable, physDev->gc,
836 physDev->dc_rect.left + rc.left, physDev->dc_rect.top + rc.top,
837 rc.right-rc.left-1, rc.bottom-rc.top-1 );
838 wine_tsx11_unlock();
839 update = TRUE;
842 /* Update the DIBSection from the pixmap */
843 X11DRV_UnlockDIBSection(physDev, update);
845 physDev->pen.width = oldwidth;
846 physDev->pen.linejoin = oldjoinstyle;
847 return TRUE;
850 /***********************************************************************
851 * X11DRV_RoundRect
853 BOOL X11DRV_RoundRect( PHYSDEV dev, INT left, INT top, INT right, INT bottom,
854 INT ell_width, INT ell_height )
856 X11DRV_PDEVICE *physDev = get_x11drv_dev( dev );
857 INT width, oldwidth, oldendcap;
858 BOOL update = FALSE;
859 POINT pts[2];
860 RECT rc = get_device_rect( dev->hdc, left, top, right, bottom );
862 TRACE("(%d %d %d %d %d %d\n",
863 left, top, right, bottom, ell_width, ell_height);
865 if ((rc.left == rc.right) || (rc.top == rc.bottom))
866 return TRUE;
868 /* Make sure ell_width and ell_height are >= 1 otherwise XDrawArc gets
869 called with width/height < 0 */
870 pts[0].x = pts[0].y = 0;
871 pts[1].x = ell_width;
872 pts[1].y = ell_height;
873 LPtoDP(dev->hdc, pts, 2);
874 ell_width = max(abs( pts[1].x - pts[0].x ), 1);
875 ell_height = max(abs( pts[1].y - pts[0].y ), 1);
877 oldwidth = width = physDev->pen.width;
878 oldendcap = physDev->pen.endcap;
879 if (!width) width = 1;
880 if(physDev->pen.style == PS_NULL) width = 0;
882 if (physDev->pen.style == PS_INSIDEFRAME)
884 if (2*width > (rc.right-rc.left)) width=(rc.right-rc.left + 1)/2;
885 if (2*width > (rc.bottom-rc.top)) width=(rc.bottom-rc.top + 1)/2;
886 rc.left += width / 2;
887 rc.right -= (width - 1) / 2;
888 rc.top += width / 2;
889 rc.bottom -= (width - 1) / 2;
891 if(width == 0) width = 1;
892 physDev->pen.width = width;
893 physDev->pen.endcap = PS_ENDCAP_SQUARE;
895 /* Update the pixmap from the DIB section */
896 X11DRV_LockDIBSection(physDev, DIB_Status_GdiMod);
898 if (X11DRV_SetupGCForBrush( physDev ))
900 wine_tsx11_lock();
901 if (ell_width > (rc.right-rc.left) )
902 if (ell_height > (rc.bottom-rc.top) )
903 XFillArc( gdi_display, physDev->drawable, physDev->gc,
904 physDev->dc_rect.left + rc.left, physDev->dc_rect.top + rc.top,
905 rc.right - rc.left - 1, rc.bottom - rc.top - 1,
906 0, 360 * 64 );
907 else{
908 XFillArc( gdi_display, physDev->drawable, physDev->gc,
909 physDev->dc_rect.left + rc.left, physDev->dc_rect.top + rc.top,
910 rc.right - rc.left - 1, ell_height, 0, 180 * 64 );
911 XFillArc( gdi_display, physDev->drawable, physDev->gc,
912 physDev->dc_rect.left + rc.left,
913 physDev->dc_rect.top + rc.bottom - ell_height - 1,
914 rc.right - rc.left - 1, ell_height, 180 * 64,
915 180 * 64 );
917 else if (ell_height > (rc.bottom-rc.top) ){
918 XFillArc( gdi_display, physDev->drawable, physDev->gc,
919 physDev->dc_rect.left + rc.left, physDev->dc_rect.top + rc.top,
920 ell_width, rc.bottom - rc.top - 1, 90 * 64, 180 * 64 );
921 XFillArc( gdi_display, physDev->drawable, physDev->gc,
922 physDev->dc_rect.left + rc.right - ell_width - 1, physDev->dc_rect.top + rc.top,
923 ell_width, rc.bottom - rc.top - 1, 270 * 64, 180 * 64 );
924 }else{
925 XFillArc( gdi_display, physDev->drawable, physDev->gc,
926 physDev->dc_rect.left + rc.left, physDev->dc_rect.top + rc.top,
927 ell_width, ell_height, 90 * 64, 90 * 64 );
928 XFillArc( gdi_display, physDev->drawable, physDev->gc,
929 physDev->dc_rect.left + rc.left,
930 physDev->dc_rect.top + rc.bottom - ell_height - 1,
931 ell_width, ell_height, 180 * 64, 90 * 64 );
932 XFillArc( gdi_display, physDev->drawable, physDev->gc,
933 physDev->dc_rect.left + rc.right - ell_width - 1,
934 physDev->dc_rect.top + rc.bottom - ell_height - 1,
935 ell_width, ell_height, 270 * 64, 90 * 64 );
936 XFillArc( gdi_display, physDev->drawable, physDev->gc,
937 physDev->dc_rect.left + rc.right - ell_width - 1,
938 physDev->dc_rect.top + rc.top,
939 ell_width, ell_height, 0, 90 * 64 );
941 if (ell_width < rc.right - rc.left)
943 XFillRectangle( gdi_display, physDev->drawable, physDev->gc,
944 physDev->dc_rect.left + rc.left + (ell_width + 1) / 2,
945 physDev->dc_rect.top + rc.top + 1,
946 rc.right - rc.left - ell_width - 1,
947 (ell_height + 1) / 2 - 1);
948 XFillRectangle( gdi_display, physDev->drawable, physDev->gc,
949 physDev->dc_rect.left + rc.left + (ell_width + 1) / 2,
950 physDev->dc_rect.top + rc.bottom - (ell_height) / 2 - 1,
951 rc.right - rc.left - ell_width - 1,
952 (ell_height) / 2 );
954 if (ell_height < rc.bottom - rc.top)
956 XFillRectangle( gdi_display, physDev->drawable, physDev->gc,
957 physDev->dc_rect.left + rc.left + 1,
958 physDev->dc_rect.top + rc.top + (ell_height + 1) / 2,
959 rc.right - rc.left - 2,
960 rc.bottom - rc.top - ell_height - 1);
962 wine_tsx11_unlock();
963 update = TRUE;
965 /* FIXME: this could be done with on X call
966 * more efficient and probably more correct
967 * on any X server: XDrawArcs will draw
968 * straight horizontal and vertical lines
969 * if width or height are zero.
971 * BTW this stuff is optimized for an Xfree86 server
972 * read the comments inside the X11DRV_DrawArc function
974 if (X11DRV_SetupGCForPen( physDev ))
976 wine_tsx11_lock();
977 if (ell_width > (rc.right-rc.left) )
978 if (ell_height > (rc.bottom-rc.top) )
979 XDrawArc( gdi_display, physDev->drawable, physDev->gc,
980 physDev->dc_rect.left + rc.left, physDev->dc_rect.top + rc.top,
981 rc.right - rc.left - 1, rc.bottom - rc.top - 1, 0 , 360 * 64 );
982 else{
983 XDrawArc( gdi_display, physDev->drawable, physDev->gc,
984 physDev->dc_rect.left + rc.left, physDev->dc_rect.top + rc.top,
985 rc.right - rc.left - 1, ell_height - 1, 0 , 180 * 64 );
986 XDrawArc( gdi_display, physDev->drawable, physDev->gc,
987 physDev->dc_rect.left + rc.left,
988 physDev->dc_rect.top + rc.bottom - ell_height,
989 rc.right - rc.left - 1, ell_height - 1, 180 * 64 , 180 * 64 );
991 else if (ell_height > (rc.bottom-rc.top) ){
992 XDrawArc( gdi_display, physDev->drawable, physDev->gc,
993 physDev->dc_rect.left + rc.left, physDev->dc_rect.top + rc.top,
994 ell_width - 1 , rc.bottom - rc.top - 1, 90 * 64 , 180 * 64 );
995 XDrawArc( gdi_display, physDev->drawable, physDev->gc,
996 physDev->dc_rect.left + rc.right - ell_width,
997 physDev->dc_rect.top + rc.top,
998 ell_width - 1 , rc.bottom - rc.top - 1, 270 * 64 , 180 * 64 );
999 }else{
1000 XDrawArc( gdi_display, physDev->drawable, physDev->gc,
1001 physDev->dc_rect.left + rc.left, physDev->dc_rect.top + rc.top,
1002 ell_width - 1, ell_height - 1, 90 * 64, 90 * 64 );
1003 XDrawArc( gdi_display, physDev->drawable, physDev->gc,
1004 physDev->dc_rect.left + rc.left, physDev->dc_rect.top + rc.bottom - ell_height,
1005 ell_width - 1, ell_height - 1, 180 * 64, 90 * 64 );
1006 XDrawArc( gdi_display, physDev->drawable, physDev->gc,
1007 physDev->dc_rect.left + rc.right - ell_width,
1008 physDev->dc_rect.top + rc.bottom - ell_height,
1009 ell_width - 1, ell_height - 1, 270 * 64, 90 * 64 );
1010 XDrawArc( gdi_display, physDev->drawable, physDev->gc,
1011 physDev->dc_rect.left + rc.right - ell_width, physDev->dc_rect.top + rc.top,
1012 ell_width - 1, ell_height - 1, 0, 90 * 64 );
1014 if (ell_width < rc.right - rc.left)
1016 XDrawLine( gdi_display, physDev->drawable, physDev->gc,
1017 physDev->dc_rect.left + rc.left + ell_width / 2,
1018 physDev->dc_rect.top + rc.top,
1019 physDev->dc_rect.left + rc.right - (ell_width+1) / 2,
1020 physDev->dc_rect.top + rc.top);
1021 XDrawLine( gdi_display, physDev->drawable, physDev->gc,
1022 physDev->dc_rect.left + rc.left + ell_width / 2 ,
1023 physDev->dc_rect.top + rc.bottom - 1,
1024 physDev->dc_rect.left + rc.right - (ell_width+1)/ 2,
1025 physDev->dc_rect.top + rc.bottom - 1);
1027 if (ell_height < rc.bottom - rc.top)
1029 XDrawLine( gdi_display, physDev->drawable, physDev->gc,
1030 physDev->dc_rect.left + rc.right - 1,
1031 physDev->dc_rect.top + rc.top + ell_height / 2,
1032 physDev->dc_rect.left + rc.right - 1,
1033 physDev->dc_rect.top + rc.bottom - (ell_height+1) / 2);
1034 XDrawLine( gdi_display, physDev->drawable, physDev->gc,
1035 physDev->dc_rect.left + rc.left,
1036 physDev->dc_rect.top + rc.top + ell_height / 2,
1037 physDev->dc_rect.left + rc.left,
1038 physDev->dc_rect.top + rc.bottom - (ell_height+1) / 2);
1040 wine_tsx11_unlock();
1041 update = TRUE;
1043 /* Update the DIBSection from the pixmap */
1044 X11DRV_UnlockDIBSection(physDev, update);
1046 physDev->pen.width = oldwidth;
1047 physDev->pen.endcap = oldendcap;
1048 return TRUE;
1052 /***********************************************************************
1053 * X11DRV_SetPixel
1055 COLORREF X11DRV_SetPixel( PHYSDEV dev, INT x, INT y, COLORREF color )
1057 X11DRV_PDEVICE *physDev = get_x11drv_dev( dev );
1058 unsigned long pixel;
1059 POINT pt;
1061 pt.x = x;
1062 pt.y = y;
1063 LPtoDP( dev->hdc, &pt, 1 );
1064 pixel = X11DRV_PALETTE_ToPhysical( physDev, color );
1066 /* Update the pixmap from the DIB section */
1067 X11DRV_LockDIBSection(physDev, DIB_Status_GdiMod);
1069 /* inefficient but simple... */
1070 wine_tsx11_lock();
1071 XSetForeground( gdi_display, physDev->gc, pixel );
1072 XSetFunction( gdi_display, physDev->gc, GXcopy );
1073 XDrawPoint( gdi_display, physDev->drawable, physDev->gc,
1074 physDev->dc_rect.left + pt.x, physDev->dc_rect.top + pt.y );
1075 wine_tsx11_unlock();
1077 /* Update the DIBSection from the pixmap */
1078 X11DRV_UnlockDIBSection(physDev, TRUE);
1080 return X11DRV_PALETTE_ToLogical(physDev, pixel);
1084 /***********************************************************************
1085 * X11DRV_GetPixel
1087 COLORREF X11DRV_GetPixel( PHYSDEV dev, INT x, INT y )
1089 static Pixmap pixmap = 0;
1090 X11DRV_PDEVICE *physDev = get_x11drv_dev( dev );
1091 XImage * image;
1092 int pixel;
1093 POINT pt;
1094 BOOL memdc = (GetObjectType(dev->hdc) == OBJ_MEMDC);
1096 pt.x = x;
1097 pt.y = y;
1098 LPtoDP( dev->hdc, &pt, 1 );
1100 /* Update the pixmap from the DIB section */
1101 X11DRV_LockDIBSection(physDev, DIB_Status_GdiMod);
1103 wine_tsx11_lock();
1104 if (memdc)
1106 image = XGetImage( gdi_display, physDev->drawable,
1107 physDev->dc_rect.left + pt.x, physDev->dc_rect.top + pt.y,
1108 1, 1, AllPlanes, ZPixmap );
1110 else
1112 /* If we are reading from the screen, use a temporary copy */
1113 /* to avoid a BadMatch error */
1114 if (!pixmap) pixmap = XCreatePixmap( gdi_display, root_window,
1115 1, 1, physDev->depth );
1116 XCopyArea( gdi_display, physDev->drawable, pixmap, get_bitmap_gc(physDev->depth),
1117 physDev->dc_rect.left + pt.x, physDev->dc_rect.top + pt.y, 1, 1, 0, 0 );
1118 image = XGetImage( gdi_display, pixmap, 0, 0, 1, 1, AllPlanes, ZPixmap );
1120 pixel = XGetPixel( image, 0, 0 );
1121 XDestroyImage( image );
1122 wine_tsx11_unlock();
1124 /* Update the DIBSection from the pixmap */
1125 X11DRV_UnlockDIBSection(physDev, FALSE);
1126 if( physDev->depth > 1)
1127 pixel = X11DRV_PALETTE_ToLogical(physDev, pixel);
1128 else
1129 /* monochrome bitmaps return black or white */
1130 if( pixel) pixel = 0xffffff;
1131 return pixel;
1136 /***********************************************************************
1137 * X11DRV_PaintRgn
1139 BOOL X11DRV_PaintRgn( PHYSDEV dev, HRGN hrgn )
1141 X11DRV_PDEVICE *physDev = get_x11drv_dev( dev );
1143 if (X11DRV_SetupGCForBrush( physDev ))
1145 unsigned int i;
1146 XRectangle *rect;
1147 RGNDATA *data = X11DRV_GetRegionData( hrgn, dev->hdc );
1149 if (!data) return FALSE;
1150 rect = (XRectangle *)data->Buffer;
1151 for (i = 0; i < data->rdh.nCount; i++)
1153 rect[i].x += physDev->dc_rect.left;
1154 rect[i].y += physDev->dc_rect.top;
1157 X11DRV_LockDIBSection(physDev, DIB_Status_GdiMod);
1158 wine_tsx11_lock();
1159 XFillRectangles( gdi_display, physDev->drawable, physDev->gc, rect, data->rdh.nCount );
1160 wine_tsx11_unlock();
1161 X11DRV_UnlockDIBSection(physDev, TRUE);
1162 HeapFree( GetProcessHeap(), 0, data );
1164 return TRUE;
1167 /**********************************************************************
1168 * X11DRV_Polyline
1170 BOOL X11DRV_Polyline( PHYSDEV dev, const POINT* pt, INT count )
1172 X11DRV_PDEVICE *physDev = get_x11drv_dev( dev );
1173 int i;
1174 XPoint *points;
1176 if (!(points = HeapAlloc( GetProcessHeap(), 0, sizeof(XPoint) * count )))
1178 WARN("No memory to convert POINTs to XPoints!\n");
1179 return FALSE;
1181 for (i = 0; i < count; i++)
1183 POINT tmp = pt[i];
1184 LPtoDP(dev->hdc, &tmp, 1);
1185 points[i].x = physDev->dc_rect.left + tmp.x;
1186 points[i].y = physDev->dc_rect.top + tmp.y;
1189 if (X11DRV_SetupGCForPen ( physDev ))
1191 X11DRV_LockDIBSection(physDev, DIB_Status_GdiMod);
1192 wine_tsx11_lock();
1193 XDrawLines( gdi_display, physDev->drawable, physDev->gc,
1194 points, count, CoordModeOrigin );
1195 wine_tsx11_unlock();
1196 X11DRV_UnlockDIBSection(physDev, TRUE);
1199 HeapFree( GetProcessHeap(), 0, points );
1200 return TRUE;
1204 /**********************************************************************
1205 * X11DRV_Polygon
1207 BOOL X11DRV_Polygon( PHYSDEV dev, const POINT* pt, INT count )
1209 X11DRV_PDEVICE *physDev = get_x11drv_dev( dev );
1210 int i;
1211 XPoint *points;
1212 BOOL update = FALSE;
1214 if (!(points = HeapAlloc( GetProcessHeap(), 0, sizeof(XPoint) * (count+1) )))
1216 WARN("No memory to convert POINTs to XPoints!\n");
1217 return FALSE;
1219 for (i = 0; i < count; i++)
1221 POINT tmp = pt[i];
1222 LPtoDP(dev->hdc, &tmp, 1);
1223 points[i].x = physDev->dc_rect.left + tmp.x;
1224 points[i].y = physDev->dc_rect.top + tmp.y;
1226 points[count] = points[0];
1228 /* Update the pixmap from the DIB section */
1229 X11DRV_LockDIBSection(physDev, DIB_Status_GdiMod);
1231 if (X11DRV_SetupGCForBrush( physDev ))
1233 wine_tsx11_lock();
1234 XFillPolygon( gdi_display, physDev->drawable, physDev->gc,
1235 points, count+1, Complex, CoordModeOrigin);
1236 wine_tsx11_unlock();
1237 update = TRUE;
1239 if (X11DRV_SetupGCForPen ( physDev ))
1241 wine_tsx11_lock();
1242 XDrawLines( gdi_display, physDev->drawable, physDev->gc,
1243 points, count+1, CoordModeOrigin );
1244 wine_tsx11_unlock();
1245 update = TRUE;
1248 /* Update the DIBSection from the pixmap */
1249 X11DRV_UnlockDIBSection(physDev, update);
1251 HeapFree( GetProcessHeap(), 0, points );
1252 return TRUE;
1256 /**********************************************************************
1257 * X11DRV_PolyPolygon
1259 BOOL X11DRV_PolyPolygon( PHYSDEV dev, const POINT* pt, const INT* counts, UINT polygons )
1261 X11DRV_PDEVICE *physDev = get_x11drv_dev( dev );
1262 HRGN hrgn;
1264 /* FIXME: The points should be converted to device coords before */
1265 /* creating the region. */
1267 hrgn = CreatePolyPolygonRgn( pt, counts, polygons, GetPolyFillMode( dev->hdc ) );
1268 X11DRV_PaintRgn( dev, hrgn );
1269 DeleteObject( hrgn );
1271 /* Draw the outline of the polygons */
1273 if (X11DRV_SetupGCForPen ( physDev ))
1275 unsigned int i;
1276 int j, max = 0;
1277 XPoint *points;
1279 /* Update the pixmap from the DIB section */
1280 X11DRV_LockDIBSection(physDev, DIB_Status_GdiMod);
1282 for (i = 0; i < polygons; i++) if (counts[i] > max) max = counts[i];
1283 if (!(points = HeapAlloc( GetProcessHeap(), 0, sizeof(XPoint) * (max+1) )))
1285 WARN("No memory to convert POINTs to XPoints!\n");
1286 return FALSE;
1288 for (i = 0; i < polygons; i++)
1290 for (j = 0; j < counts[i]; j++)
1292 POINT tmp = *pt;
1293 LPtoDP(dev->hdc, &tmp, 1);
1294 points[j].x = physDev->dc_rect.left + tmp.x;
1295 points[j].y = physDev->dc_rect.top + tmp.y;
1296 pt++;
1298 points[j] = points[0];
1299 wine_tsx11_lock();
1300 XDrawLines( gdi_display, physDev->drawable, physDev->gc,
1301 points, j + 1, CoordModeOrigin );
1302 wine_tsx11_unlock();
1305 /* Update the DIBSection of the dc's bitmap */
1306 X11DRV_UnlockDIBSection(physDev, TRUE);
1308 HeapFree( GetProcessHeap(), 0, points );
1310 return TRUE;
1314 /**********************************************************************
1315 * X11DRV_PolyPolyline
1317 BOOL X11DRV_PolyPolyline( PHYSDEV dev, const POINT* pt, const DWORD* counts, DWORD polylines )
1319 X11DRV_PDEVICE *physDev = get_x11drv_dev( dev );
1321 if (X11DRV_SetupGCForPen ( physDev ))
1323 unsigned int i, j, max = 0;
1324 XPoint *points;
1326 /* Update the pixmap from the DIB section */
1327 X11DRV_LockDIBSection(physDev, DIB_Status_GdiMod);
1329 for (i = 0; i < polylines; i++) if (counts[i] > max) max = counts[i];
1330 if (!(points = HeapAlloc( GetProcessHeap(), 0, sizeof(XPoint) * max )))
1332 WARN("No memory to convert POINTs to XPoints!\n");
1333 return FALSE;
1335 for (i = 0; i < polylines; i++)
1337 for (j = 0; j < counts[i]; j++)
1339 POINT tmp = *pt;
1340 LPtoDP(dev->hdc, &tmp, 1);
1341 points[j].x = physDev->dc_rect.left + tmp.x;
1342 points[j].y = physDev->dc_rect.top + tmp.y;
1343 pt++;
1345 wine_tsx11_lock();
1346 XDrawLines( gdi_display, physDev->drawable, physDev->gc,
1347 points, j, CoordModeOrigin );
1348 wine_tsx11_unlock();
1351 /* Update the DIBSection of the dc's bitmap */
1352 X11DRV_UnlockDIBSection(physDev, TRUE);
1354 HeapFree( GetProcessHeap(), 0, points );
1356 return TRUE;
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 )
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 XFillRectangle( gdi_display, physDev->drawable, physDev->gc,
1386 xOrg + left, yOrg + y, right-left, 1 );
1388 /* Set the pixels of this line so we don't fill it again */
1390 for (x = left; x < right; x++)
1392 if (fillType == FLOODFILLBORDER) XPutPixel( image, x, y, pixel );
1393 else XPutPixel( image, x, y, ~pixel );
1396 /* Fill the line above */
1398 if (--y >= 0)
1400 x = left;
1401 while (x < right)
1403 while ((x < right) && !TO_FLOOD(x,y)) x++;
1404 if (x >= right) break;
1405 while ((x < right) && TO_FLOOD(x,y)) x++;
1406 X11DRV_InternalFloodFill(image, physDev, x-1, y,
1407 xOrg, yOrg, pixel, fillType );
1411 /* Fill the line below */
1413 if ((y += 2) < image->height)
1415 x = left;
1416 while (x < right)
1418 while ((x < right) && !TO_FLOOD(x,y)) x++;
1419 if (x >= right) break;
1420 while ((x < right) && TO_FLOOD(x,y)) x++;
1421 X11DRV_InternalFloodFill(image, physDev, x-1, y,
1422 xOrg, yOrg, pixel, fillType );
1425 #undef TO_FLOOD
1429 static int ExtFloodFillXGetImageErrorHandler( Display *dpy, XErrorEvent *event, void *arg )
1431 return (event->request_code == X_GetImage && event->error_code == BadMatch);
1434 /**********************************************************************
1435 * X11DRV_ExtFloodFill
1437 BOOL X11DRV_ExtFloodFill( PHYSDEV dev, INT x, INT y, COLORREF color, UINT fillType )
1439 X11DRV_PDEVICE *physDev = get_x11drv_dev( dev );
1440 XImage *image;
1441 RECT rect;
1442 POINT pt;
1444 TRACE("X11DRV_ExtFloodFill %d,%d %06x %d\n", x, y, color, fillType );
1446 pt.x = x;
1447 pt.y = y;
1448 LPtoDP( dev->hdc, &pt, 1 );
1449 if (!PtInRegion( physDev->region, pt.x, pt.y )) return FALSE;
1450 GetRgnBox( physDev->region, &rect );
1452 X11DRV_expect_error( gdi_display, ExtFloodFillXGetImageErrorHandler, NULL );
1453 image = XGetImage( gdi_display, physDev->drawable,
1454 physDev->dc_rect.left + rect.left, physDev->dc_rect.top + rect.top,
1455 rect.right - rect.left, rect.bottom - rect.top,
1456 AllPlanes, ZPixmap );
1457 if(X11DRV_check_error()) image = NULL;
1458 if (!image) return FALSE;
1460 if (X11DRV_SetupGCForBrush( physDev ))
1462 unsigned long pixel = X11DRV_PALETTE_ToPhysical( physDev, color );
1464 /* Update the pixmap from the DIB section */
1465 X11DRV_LockDIBSection(physDev, DIB_Status_GdiMod);
1467 /* ROP mode is always GXcopy for flood-fill */
1468 wine_tsx11_lock();
1469 XSetFunction( gdi_display, physDev->gc, GXcopy );
1470 X11DRV_InternalFloodFill(image, physDev,
1471 pt.x - rect.left,
1472 pt.y - rect.top,
1473 physDev->dc_rect.left + rect.left,
1474 physDev->dc_rect.top + rect.top,
1475 pixel, fillType );
1476 wine_tsx11_unlock();
1477 /* Update the DIBSection of the dc's bitmap */
1478 X11DRV_UnlockDIBSection(physDev, TRUE);
1481 wine_tsx11_lock();
1482 XDestroyImage( image );
1483 wine_tsx11_unlock();
1484 return TRUE;
1487 /**********************************************************************
1488 * X11DRV_SetBkColor
1490 COLORREF X11DRV_SetBkColor( PHYSDEV dev, COLORREF color )
1492 X11DRV_PDEVICE *physDev = get_x11drv_dev( dev );
1494 physDev->backgroundPixel = X11DRV_PALETTE_ToPhysical( physDev, color );
1495 return color;
1498 /**********************************************************************
1499 * X11DRV_SetTextColor
1501 COLORREF X11DRV_SetTextColor( PHYSDEV dev, COLORREF color )
1503 X11DRV_PDEVICE *physDev = get_x11drv_dev( dev );
1505 physDev->textPixel = X11DRV_PALETTE_ToPhysical( physDev, color );
1506 return color;
1510 static unsigned char *get_icm_profile( unsigned long *size )
1512 Atom type;
1513 int format;
1514 unsigned long count, remaining;
1515 unsigned char *profile, *ret = NULL;
1517 wine_tsx11_lock();
1518 XGetWindowProperty( gdi_display, DefaultRootWindow(gdi_display),
1519 x11drv_atom(_ICC_PROFILE), 0, ~0UL, False, AnyPropertyType,
1520 &type, &format, &count, &remaining, &profile );
1521 *size = get_property_size( format, count );
1522 if (format && count)
1524 if ((ret = HeapAlloc( GetProcessHeap(), 0, *size ))) memcpy( ret, profile, *size );
1525 XFree( profile );
1527 wine_tsx11_unlock();
1528 return ret;
1531 typedef struct
1533 unsigned int unknown[6];
1534 unsigned int state[5];
1535 unsigned int count[2];
1536 unsigned char buffer[64];
1537 } sha_ctx;
1539 extern void WINAPI A_SHAInit( sha_ctx * );
1540 extern void WINAPI A_SHAUpdate( sha_ctx *, const unsigned char *, unsigned int );
1541 extern void WINAPI A_SHAFinal( sha_ctx *, unsigned char * );
1543 static const WCHAR mntr_key[] =
1544 {'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
1545 'W','i','n','d','o','w','s',' ','N','T','\\','C','u','r','r','e','n','t',
1546 'V','e','r','s','i','o','n','\\','I','C','M','\\','m','n','t','r',0};
1548 static const WCHAR color_path[] =
1549 {'\\','s','p','o','o','l','\\','d','r','i','v','e','r','s','\\','c','o','l','o','r','\\',0};
1551 /***********************************************************************
1552 * GetICMProfile (X11DRV.@)
1554 BOOL X11DRV_GetICMProfile( PHYSDEV dev, LPDWORD size, LPWSTR filename )
1556 static const WCHAR srgb[] =
1557 {'s','R','G','B',' ','C','o','l','o','r',' ','S','p','a','c','e',' ',
1558 'P','r','o','f','i','l','e','.','i','c','m',0};
1559 HKEY hkey;
1560 DWORD required, len;
1561 WCHAR profile[MAX_PATH], fullname[2*MAX_PATH + sizeof(color_path)/sizeof(WCHAR)];
1562 unsigned char *buffer;
1563 unsigned long buflen;
1565 if (!size) return FALSE;
1567 GetSystemDirectoryW( fullname, MAX_PATH );
1568 strcatW( fullname, color_path );
1570 len = sizeof(profile)/sizeof(WCHAR);
1571 if (!RegCreateKeyExW( HKEY_LOCAL_MACHINE, mntr_key, 0, NULL, 0, KEY_ALL_ACCESS, NULL, &hkey, NULL ) &&
1572 !RegEnumValueW( hkey, 0, profile, &len, NULL, NULL, NULL, NULL )) /* FIXME handle multiple values */
1574 strcatW( fullname, profile );
1575 RegCloseKey( hkey );
1577 else if ((buffer = get_icm_profile( &buflen )))
1579 static const WCHAR fmt[] = {'%','0','2','x',0};
1580 static const WCHAR icm[] = {'.','i','c','m',0};
1582 unsigned char sha1sum[20];
1583 unsigned int i;
1584 sha_ctx ctx;
1585 HANDLE file;
1587 A_SHAInit( &ctx );
1588 A_SHAUpdate( &ctx, buffer, buflen );
1589 A_SHAFinal( &ctx, sha1sum );
1591 for (i = 0; i < sizeof(sha1sum); i++) sprintfW( &profile[i * 2], fmt, sha1sum[i] );
1592 memcpy( &profile[i * 2], icm, sizeof(icm) );
1594 strcatW( fullname, profile );
1595 file = CreateFileW( fullname, GENERIC_WRITE, 0, NULL, CREATE_NEW, 0, 0 );
1596 if (file != INVALID_HANDLE_VALUE)
1598 DWORD written;
1600 if (!WriteFile( file, buffer, buflen, &written, NULL ) || written != buflen)
1601 ERR( "Unable to write color profile\n" );
1602 CloseHandle( file );
1604 HeapFree( GetProcessHeap(), 0, buffer );
1606 else strcatW( fullname, srgb );
1608 required = strlenW( fullname ) + 1;
1609 if (*size < required)
1611 *size = required;
1612 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1613 return FALSE;
1615 if (filename)
1617 strcpyW( filename, fullname );
1618 if (GetFileAttributesW( filename ) == INVALID_FILE_ATTRIBUTES)
1619 WARN( "color profile not found\n" );
1621 *size = required;
1622 return TRUE;
1625 /***********************************************************************
1626 * EnumICMProfiles (X11DRV.@)
1628 INT X11DRV_EnumICMProfiles( PHYSDEV dev, ICMENUMPROCW proc, LPARAM lparam )
1630 X11DRV_PDEVICE *physDev = get_x11drv_dev( dev );
1631 HKEY hkey;
1632 DWORD len_sysdir, len_path, len, index = 0;
1633 WCHAR sysdir[MAX_PATH], *profile;
1634 LONG res;
1635 INT ret;
1637 TRACE("%p, %p, %ld\n", physDev, proc, lparam);
1639 if (RegOpenKeyExW( HKEY_LOCAL_MACHINE, mntr_key, 0, KEY_ALL_ACCESS, &hkey ))
1640 return -1;
1642 len_sysdir = GetSystemDirectoryW( sysdir, MAX_PATH );
1643 len_path = len_sysdir + sizeof(color_path) / sizeof(color_path[0]) - 1;
1644 len = 64;
1645 for (;;)
1647 if (!(profile = HeapAlloc( GetProcessHeap(), 0, (len_path + len) * sizeof(WCHAR) )))
1649 RegCloseKey( hkey );
1650 return -1;
1652 res = RegEnumValueW( hkey, index, profile + len_path, &len, NULL, NULL, NULL, NULL );
1653 while (res == ERROR_MORE_DATA)
1655 len *= 2;
1656 HeapFree( GetProcessHeap(), 0, profile );
1657 if (!(profile = HeapAlloc( GetProcessHeap(), 0, (len_path + len) * sizeof(WCHAR) )))
1659 RegCloseKey( hkey );
1660 return -1;
1662 res = RegEnumValueW( hkey, index, profile + len_path, &len, NULL, NULL, NULL, NULL );
1664 if (res != ERROR_SUCCESS)
1666 HeapFree( GetProcessHeap(), 0, profile );
1667 break;
1669 memcpy( profile, sysdir, len_sysdir * sizeof(WCHAR) );
1670 memcpy( profile + len_sysdir, color_path, sizeof(color_path) - sizeof(WCHAR) );
1671 ret = proc( profile, lparam );
1672 HeapFree( GetProcessHeap(), 0, profile );
1673 if (!ret) break;
1674 index++;
1676 RegCloseKey( hkey );
1677 return -1;