makefiles: Bypass the normal substitution mechanism for the makefile dependencies.
[wine/multimedia.git] / dlls / winex11.drv / graphics.c
blobbc00e854f73b30b42f927ec41d531e5185a282ff
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>
40 #include "windef.h"
41 #include "winbase.h"
42 #include "winreg.h"
44 #include "x11drv.h"
45 #include "x11font.h"
46 #include "wine/debug.h"
47 #include "wine/unicode.h"
49 WINE_DEFAULT_DEBUG_CHANNEL(graphics);
51 #define ABS(x) ((x)<0?(-(x)):(x))
53 /* ROP code to GC function conversion */
54 const int X11DRV_XROPfunction[16] =
56 GXclear, /* R2_BLACK */
57 GXnor, /* R2_NOTMERGEPEN */
58 GXandInverted, /* R2_MASKNOTPEN */
59 GXcopyInverted, /* R2_NOTCOPYPEN */
60 GXandReverse, /* R2_MASKPENNOT */
61 GXinvert, /* R2_NOT */
62 GXxor, /* R2_XORPEN */
63 GXnand, /* R2_NOTMASKPEN */
64 GXand, /* R2_MASKPEN */
65 GXequiv, /* R2_NOTXORPEN */
66 GXnoop, /* R2_NOP */
67 GXorInverted, /* R2_MERGENOTPEN */
68 GXcopy, /* R2_COPYPEN */
69 GXorReverse, /* R2_MERGEPENNOT */
70 GXor, /* R2_MERGEPEN */
71 GXset /* R2_WHITE */
75 /***********************************************************************
76 * X11DRV_GetRegionData
78 * Calls GetRegionData on the given region and converts the rectangle
79 * array to XRectangle format. The returned buffer must be freed by
80 * caller using HeapFree(GetProcessHeap(),...).
81 * If hdc_lptodp is not 0, the rectangles are converted through LPtoDP.
83 RGNDATA *X11DRV_GetRegionData( HRGN hrgn, HDC hdc_lptodp )
85 RGNDATA *data;
86 DWORD size;
87 unsigned int i;
88 RECT *rect, tmp;
89 XRectangle *xrect;
91 if (!(size = GetRegionData( hrgn, 0, NULL ))) return NULL;
92 if (sizeof(XRectangle) > sizeof(RECT))
94 /* add extra size for XRectangle array */
95 int count = (size - sizeof(RGNDATAHEADER)) / sizeof(RECT);
96 size += count * (sizeof(XRectangle) - sizeof(RECT));
98 if (!(data = HeapAlloc( GetProcessHeap(), 0, size ))) return NULL;
99 if (!GetRegionData( hrgn, size, data ))
101 HeapFree( GetProcessHeap(), 0, data );
102 return NULL;
105 rect = (RECT *)data->Buffer;
106 xrect = (XRectangle *)data->Buffer;
107 if (hdc_lptodp) /* map to device coordinates */
109 LPtoDP( hdc_lptodp, (POINT *)rect, data->rdh.nCount * 2 );
110 for (i = 0; i < data->rdh.nCount; i++)
112 if (rect[i].right < rect[i].left)
114 INT tmp = rect[i].right;
115 rect[i].right = rect[i].left;
116 rect[i].left = tmp;
118 if (rect[i].bottom < rect[i].top)
120 INT tmp = rect[i].bottom;
121 rect[i].bottom = rect[i].top;
122 rect[i].top = tmp;
127 if (sizeof(XRectangle) > sizeof(RECT))
129 int j;
130 /* need to start from the end */
131 for (j = data->rdh.nCount-1; j >= 0; j--)
133 tmp = rect[j];
134 xrect[j].x = tmp.left;
135 xrect[j].y = tmp.top;
136 xrect[j].width = tmp.right - tmp.left;
137 xrect[j].height = tmp.bottom - tmp.top;
140 else
142 for (i = 0; i < data->rdh.nCount; i++)
144 tmp = rect[i];
145 xrect[i].x = tmp.left;
146 xrect[i].y = tmp.top;
147 xrect[i].width = tmp.right - tmp.left;
148 xrect[i].height = tmp.bottom - tmp.top;
151 return data;
155 /***********************************************************************
156 * X11DRV_SetDeviceClipping
158 void CDECL X11DRV_SetDeviceClipping( X11DRV_PDEVICE *physDev, HRGN vis_rgn, HRGN clip_rgn )
160 RGNDATA *data;
162 CombineRgn( physDev->region, vis_rgn, clip_rgn, clip_rgn ? RGN_AND : RGN_COPY );
163 if (!(data = X11DRV_GetRegionData( physDev->region, 0 ))) return;
165 wine_tsx11_lock();
166 XSetClipRectangles( gdi_display, physDev->gc, physDev->dc_rect.left, physDev->dc_rect.top,
167 (XRectangle *)data->Buffer, data->rdh.nCount, YXBanded );
168 wine_tsx11_unlock();
170 if (physDev->xrender) X11DRV_XRender_SetDeviceClipping(physDev, data);
172 HeapFree( GetProcessHeap(), 0, data );
176 /***********************************************************************
177 * X11DRV_SetupGCForPatBlt
179 * Setup the GC for a PatBlt operation using current brush.
180 * If fMapColors is TRUE, X pixels are mapped to Windows colors.
181 * Return FALSE if brush is BS_NULL, TRUE otherwise.
183 BOOL X11DRV_SetupGCForPatBlt( X11DRV_PDEVICE *physDev, GC gc, BOOL fMapColors )
185 XGCValues val;
186 unsigned long mask;
187 Pixmap pixmap = 0;
188 POINT pt;
190 if (physDev->brush.style == BS_NULL) return FALSE;
191 if (physDev->brush.pixel == -1)
193 /* Special case used for monochrome pattern brushes.
194 * We need to swap foreground and background because
195 * Windows does it the wrong way...
197 val.foreground = physDev->backgroundPixel;
198 val.background = physDev->textPixel;
200 else
202 val.foreground = physDev->brush.pixel;
203 val.background = physDev->backgroundPixel;
205 if (fMapColors && X11DRV_PALETTE_XPixelToPalette)
207 val.foreground = X11DRV_PALETTE_XPixelToPalette[val.foreground];
208 val.background = X11DRV_PALETTE_XPixelToPalette[val.background];
211 val.function = X11DRV_XROPfunction[GetROP2(physDev->hdc)-1];
213 ** Let's replace GXinvert by GXxor with (black xor white)
214 ** This solves the selection color and leak problems in excel
215 ** FIXME : Let's do that only if we work with X-pixels, not with Win-pixels
217 if (val.function == GXinvert)
219 val.foreground = (WhitePixel( gdi_display, DefaultScreen(gdi_display) ) ^
220 BlackPixel( gdi_display, DefaultScreen(gdi_display) ));
221 val.function = GXxor;
223 val.fill_style = physDev->brush.fillStyle;
224 switch(val.fill_style)
226 case FillStippled:
227 case FillOpaqueStippled:
228 if (GetBkMode(physDev->hdc)==OPAQUE) val.fill_style = FillOpaqueStippled;
229 val.stipple = physDev->brush.pixmap;
230 mask = GCStipple;
231 break;
233 case FillTiled:
234 if (fMapColors && X11DRV_PALETTE_XPixelToPalette)
236 register int x, y;
237 XImage *image;
238 wine_tsx11_lock();
239 pixmap = XCreatePixmap( gdi_display, root_window, 8, 8, physDev->depth );
240 image = XGetImage( gdi_display, physDev->brush.pixmap, 0, 0, 8, 8,
241 AllPlanes, ZPixmap );
242 for (y = 0; y < 8; y++)
243 for (x = 0; x < 8; x++)
244 XPutPixel( image, x, y,
245 X11DRV_PALETTE_XPixelToPalette[XGetPixel( image, x, y)] );
246 XPutImage( gdi_display, pixmap, gc, image, 0, 0, 0, 0, 8, 8 );
247 XDestroyImage( image );
248 wine_tsx11_unlock();
249 val.tile = pixmap;
251 else val.tile = physDev->brush.pixmap;
252 mask = GCTile;
253 break;
255 default:
256 mask = 0;
257 break;
259 GetBrushOrgEx( physDev->hdc, &pt );
260 val.ts_x_origin = physDev->dc_rect.left + pt.x;
261 val.ts_y_origin = physDev->dc_rect.top + pt.y;
262 val.fill_rule = (GetPolyFillMode(physDev->hdc) == WINDING) ? WindingRule : EvenOddRule;
263 wine_tsx11_lock();
264 XChangeGC( gdi_display, gc,
265 GCFunction | GCForeground | GCBackground | GCFillStyle |
266 GCFillRule | GCTileStipXOrigin | GCTileStipYOrigin | mask,
267 &val );
268 if (pixmap) XFreePixmap( gdi_display, pixmap );
269 wine_tsx11_unlock();
270 return TRUE;
274 /***********************************************************************
275 * X11DRV_SetupGCForBrush
277 * Setup physDev->gc for drawing operations using current brush.
278 * Return FALSE if brush is BS_NULL, TRUE otherwise.
280 BOOL X11DRV_SetupGCForBrush( X11DRV_PDEVICE *physDev )
282 return X11DRV_SetupGCForPatBlt( physDev, physDev->gc, FALSE );
286 /***********************************************************************
287 * X11DRV_SetupGCForPen
289 * Setup physDev->gc for drawing operations using current pen.
290 * Return FALSE if pen is PS_NULL, TRUE otherwise.
292 static BOOL X11DRV_SetupGCForPen( X11DRV_PDEVICE *physDev )
294 XGCValues val;
295 UINT rop2 = GetROP2(physDev->hdc);
297 if (physDev->pen.style == PS_NULL) return FALSE;
299 switch (rop2)
301 case R2_BLACK :
302 val.foreground = BlackPixel( gdi_display, DefaultScreen(gdi_display) );
303 val.function = GXcopy;
304 break;
305 case R2_WHITE :
306 val.foreground = WhitePixel( gdi_display, DefaultScreen(gdi_display) );
307 val.function = GXcopy;
308 break;
309 case R2_XORPEN :
310 val.foreground = physDev->pen.pixel;
311 /* It is very unlikely someone wants to XOR with 0 */
312 /* This fixes the rubber-drawings in paintbrush */
313 if (val.foreground == 0)
314 val.foreground = (WhitePixel( gdi_display, DefaultScreen(gdi_display) ) ^
315 BlackPixel( gdi_display, DefaultScreen(gdi_display) ));
316 val.function = GXxor;
317 break;
318 default :
319 val.foreground = physDev->pen.pixel;
320 val.function = X11DRV_XROPfunction[rop2-1];
322 val.background = physDev->backgroundPixel;
323 val.fill_style = FillSolid;
324 val.line_width = physDev->pen.width;
325 if (val.line_width <= 1) {
326 val.cap_style = CapNotLast;
327 } else {
328 switch (physDev->pen.endcap)
330 case PS_ENDCAP_SQUARE:
331 val.cap_style = CapProjecting;
332 break;
333 case PS_ENDCAP_FLAT:
334 val.cap_style = CapButt;
335 break;
336 case PS_ENDCAP_ROUND:
337 default:
338 val.cap_style = CapRound;
341 switch (physDev->pen.linejoin)
343 case PS_JOIN_BEVEL:
344 val.join_style = JoinBevel;
345 break;
346 case PS_JOIN_MITER:
347 val.join_style = JoinMiter;
348 break;
349 case PS_JOIN_ROUND:
350 default:
351 val.join_style = JoinRound;
354 if (physDev->pen.dash_len)
355 val.line_style = ((GetBkMode(physDev->hdc) == OPAQUE) && (!physDev->pen.ext))
356 ? LineDoubleDash : LineOnOffDash;
357 else
358 val.line_style = LineSolid;
360 wine_tsx11_lock();
361 if (physDev->pen.dash_len)
362 XSetDashes( gdi_display, physDev->gc, 0, physDev->pen.dashes, physDev->pen.dash_len );
363 XChangeGC( gdi_display, physDev->gc,
364 GCFunction | GCForeground | GCBackground | GCLineWidth |
365 GCLineStyle | GCCapStyle | GCJoinStyle | GCFillStyle, &val );
366 wine_tsx11_unlock();
367 return TRUE;
371 /***********************************************************************
372 * X11DRV_SetupGCForText
374 * Setup physDev->gc for text drawing operations.
375 * Return FALSE if the font is null, TRUE otherwise.
377 BOOL X11DRV_SetupGCForText( X11DRV_PDEVICE *physDev )
379 XFontStruct* xfs = XFONT_GetFontStruct( physDev->font );
381 if( xfs )
383 XGCValues val;
385 val.function = GXcopy; /* Text is always GXcopy */
386 val.foreground = physDev->textPixel;
387 val.background = physDev->backgroundPixel;
388 val.fill_style = FillSolid;
389 val.font = xfs->fid;
391 wine_tsx11_lock();
392 XChangeGC( gdi_display, physDev->gc,
393 GCFunction | GCForeground | GCBackground | GCFillStyle |
394 GCFont, &val );
395 wine_tsx11_unlock();
396 return TRUE;
398 WARN("Physical font failure\n" );
399 return FALSE;
402 /***********************************************************************
403 * X11DRV_XWStoDS
405 * Performs a world-to-viewport transformation on the specified width.
407 INT X11DRV_XWStoDS( X11DRV_PDEVICE *physDev, INT width )
409 POINT pt[2];
411 pt[0].x = 0;
412 pt[0].y = 0;
413 pt[1].x = width;
414 pt[1].y = 0;
415 LPtoDP( physDev->hdc, pt, 2 );
416 return pt[1].x - pt[0].x;
419 /***********************************************************************
420 * X11DRV_YWStoDS
422 * Performs a world-to-viewport transformation on the specified height.
424 INT X11DRV_YWStoDS( X11DRV_PDEVICE *physDev, INT height )
426 POINT pt[2];
428 pt[0].x = 0;
429 pt[0].y = 0;
430 pt[1].x = 0;
431 pt[1].y = height;
432 LPtoDP( physDev->hdc, pt, 2 );
433 return pt[1].y - pt[0].y;
436 /***********************************************************************
437 * X11DRV_LineTo
439 BOOL CDECL
440 X11DRV_LineTo( X11DRV_PDEVICE *physDev, INT x, INT y )
442 POINT pt[2];
444 if (X11DRV_SetupGCForPen( physDev )) {
445 /* Update the pixmap from the DIB section */
446 X11DRV_LockDIBSection(physDev, DIB_Status_GdiMod);
448 GetCurrentPositionEx( physDev->hdc, &pt[0] );
449 pt[1].x = x;
450 pt[1].y = y;
451 LPtoDP( physDev->hdc, pt, 2 );
453 wine_tsx11_lock();
454 XDrawLine(gdi_display, physDev->drawable, physDev->gc,
455 physDev->dc_rect.left + pt[0].x, physDev->dc_rect.top + pt[0].y,
456 physDev->dc_rect.left + pt[1].x, physDev->dc_rect.top + pt[1].y );
457 wine_tsx11_unlock();
459 /* Update the DIBSection from the pixmap */
460 X11DRV_UnlockDIBSection(physDev, TRUE);
462 return TRUE;
467 /***********************************************************************
468 * X11DRV_DrawArc
470 * Helper functions for Arc(), Chord() and Pie().
471 * 'lines' is the number of lines to draw: 0 for Arc, 1 for Chord, 2 for Pie.
474 static BOOL
475 X11DRV_DrawArc( X11DRV_PDEVICE *physDev, INT left, INT top, INT right,
476 INT bottom, INT xstart, INT ystart,
477 INT xend, INT yend, INT lines )
479 INT xcenter, ycenter, istart_angle, idiff_angle;
480 INT width, oldwidth;
481 double start_angle, end_angle;
482 XPoint points[4];
483 BOOL update = FALSE;
484 POINT start, end;
485 RECT rc;
487 SetRect(&rc, left, top, right, bottom);
488 start.x = xstart;
489 start.y = ystart;
490 end.x = xend;
491 end.y = yend;
492 LPtoDP(physDev->hdc, (POINT*)&rc, 2);
493 LPtoDP(physDev->hdc, &start, 1);
494 LPtoDP(physDev->hdc, &end, 1);
496 if (rc.right < rc.left) { INT tmp = rc.right; rc.right = rc.left; rc.left = tmp; }
497 if (rc.bottom < rc.top) { INT tmp = rc.bottom; rc.bottom = rc.top; rc.top = tmp; }
498 if ((rc.left == rc.right) || (rc.top == rc.bottom)
499 ||(lines && ((rc.right-rc.left==1)||(rc.bottom-rc.top==1)))) return TRUE;
501 if (GetArcDirection( physDev->hdc ) == AD_CLOCKWISE)
502 { POINT tmp = start; start = end; end = tmp; }
504 oldwidth = width = physDev->pen.width;
505 if (!width) width = 1;
506 if(physDev->pen.style == PS_NULL) width = 0;
508 if ((physDev->pen.style == PS_INSIDEFRAME))
510 if (2*width > (rc.right-rc.left)) width=(rc.right-rc.left + 1)/2;
511 if (2*width > (rc.bottom-rc.top)) width=(rc.bottom-rc.top + 1)/2;
512 rc.left += width / 2;
513 rc.right -= (width - 1) / 2;
514 rc.top += width / 2;
515 rc.bottom -= (width - 1) / 2;
517 if(width == 0) width = 1; /* more accurate */
518 physDev->pen.width = width;
520 xcenter = (rc.right + rc.left) / 2;
521 ycenter = (rc.bottom + rc.top) / 2;
522 start_angle = atan2( (double)(ycenter-start.y)*(rc.right-rc.left),
523 (double)(start.x-xcenter)*(rc.bottom-rc.top) );
524 end_angle = atan2( (double)(ycenter-end.y)*(rc.right-rc.left),
525 (double)(end.x-xcenter)*(rc.bottom-rc.top) );
526 if ((start.x==end.x)&&(start.y==end.y))
527 { /* A lazy program delivers xstart=xend=ystart=yend=0) */
528 start_angle = 0;
529 end_angle = 2* PI;
531 else /* notorious cases */
532 if ((start_angle == PI)&&( end_angle <0))
533 start_angle = - PI;
534 else
535 if ((end_angle == PI)&&( start_angle <0))
536 end_angle = - PI;
537 istart_angle = (INT)(start_angle * 180 * 64 / PI + 0.5);
538 idiff_angle = (INT)((end_angle - start_angle) * 180 * 64 / PI + 0.5);
539 if (idiff_angle <= 0) idiff_angle += 360 * 64;
541 /* Update the pixmap from the DIB section */
542 X11DRV_LockDIBSection(physDev, DIB_Status_GdiMod);
544 /* Fill arc with brush if Chord() or Pie() */
546 if ((lines > 0) && X11DRV_SetupGCForBrush( physDev )) {
547 wine_tsx11_lock();
548 XSetArcMode( gdi_display, physDev->gc, (lines==1) ? ArcChord : ArcPieSlice);
549 XFillArc( gdi_display, physDev->drawable, physDev->gc,
550 physDev->dc_rect.left + rc.left, physDev->dc_rect.top + rc.top,
551 rc.right-rc.left-1, rc.bottom-rc.top-1, istart_angle, idiff_angle );
552 wine_tsx11_unlock();
553 update = TRUE;
556 /* Draw arc and lines */
558 if (X11DRV_SetupGCForPen( physDev ))
560 wine_tsx11_lock();
561 XDrawArc( gdi_display, physDev->drawable, physDev->gc,
562 physDev->dc_rect.left + rc.left, physDev->dc_rect.top + rc.top,
563 rc.right-rc.left-1, rc.bottom-rc.top-1, istart_angle, idiff_angle );
564 if (lines) {
565 /* use the truncated values */
566 start_angle=(double)istart_angle*PI/64./180.;
567 end_angle=(double)(istart_angle+idiff_angle)*PI/64./180.;
568 /* calculate the endpoints and round correctly */
569 points[0].x = (int) floor(physDev->dc_rect.left + (rc.right+rc.left)/2.0 +
570 cos(start_angle) * (rc.right-rc.left-width*2+2) / 2. + 0.5);
571 points[0].y = (int) floor(physDev->dc_rect.top + (rc.top+rc.bottom)/2.0 -
572 sin(start_angle) * (rc.bottom-rc.top-width*2+2) / 2. + 0.5);
573 points[1].x = (int) floor(physDev->dc_rect.left + (rc.right+rc.left)/2.0 +
574 cos(end_angle) * (rc.right-rc.left-width*2+2) / 2. + 0.5);
575 points[1].y = (int) floor(physDev->dc_rect.top + (rc.top+rc.bottom)/2.0 -
576 sin(end_angle) * (rc.bottom-rc.top-width*2+2) / 2. + 0.5);
578 /* OK, this stuff is optimized for Xfree86
579 * which is probably the server most used by
580 * wine users. Other X servers will not
581 * display correctly. (eXceed for instance)
582 * so if you feel you must make changes, make sure that
583 * you either use Xfree86 or separate your changes
584 * from these (compile switch or whatever)
586 if (lines == 2) {
587 INT dx1,dy1;
588 points[3] = points[1];
589 points[1].x = physDev->dc_rect.left + xcenter;
590 points[1].y = physDev->dc_rect.top + ycenter;
591 points[2] = points[1];
592 dx1=points[1].x-points[0].x;
593 dy1=points[1].y-points[0].y;
594 if(((rc.top-rc.bottom) | -2) == -2)
595 if(dy1>0) points[1].y--;
596 if(dx1<0) {
597 if (((-dx1)*64)<=ABS(dy1)*37) points[0].x--;
598 if(((-dx1*9))<(dy1*16)) points[0].y--;
599 if( dy1<0 && ((dx1*9)) < (dy1*16)) points[0].y--;
600 } else {
601 if(dy1 < 0) points[0].y--;
602 if(((rc.right-rc.left) | -2) == -2) points[1].x--;
604 dx1=points[3].x-points[2].x;
605 dy1=points[3].y-points[2].y;
606 if(((rc.top-rc.bottom) | -2 ) == -2)
607 if(dy1 < 0) points[2].y--;
608 if( dx1<0){
609 if( dy1>0) points[3].y--;
610 if(((rc.right-rc.left) | -2) == -2 ) points[2].x--;
611 }else {
612 points[3].y--;
613 if( dx1 * 64 < dy1 * -37 ) points[3].x--;
615 lines++;
617 XDrawLines( gdi_display, physDev->drawable, physDev->gc,
618 points, lines+1, CoordModeOrigin );
620 wine_tsx11_unlock();
621 update = TRUE;
624 /* Update the DIBSection of the pixmap */
625 X11DRV_UnlockDIBSection(physDev, update);
627 physDev->pen.width = oldwidth;
628 return TRUE;
632 /***********************************************************************
633 * X11DRV_Arc
635 BOOL CDECL
636 X11DRV_Arc( X11DRV_PDEVICE *physDev, INT left, INT top, INT right, INT bottom,
637 INT xstart, INT ystart, INT xend, INT yend )
639 return X11DRV_DrawArc( physDev, left, top, right, bottom,
640 xstart, ystart, xend, yend, 0 );
644 /***********************************************************************
645 * X11DRV_Pie
647 BOOL CDECL
648 X11DRV_Pie( X11DRV_PDEVICE *physDev, INT left, INT top, INT right, INT bottom,
649 INT xstart, INT ystart, INT xend, INT yend )
651 return X11DRV_DrawArc( physDev, left, top, right, bottom,
652 xstart, ystart, xend, yend, 2 );
655 /***********************************************************************
656 * X11DRV_Chord
658 BOOL CDECL
659 X11DRV_Chord( X11DRV_PDEVICE *physDev, INT left, INT top, INT right, INT bottom,
660 INT xstart, INT ystart, INT xend, INT yend )
662 return X11DRV_DrawArc( physDev, left, top, right, bottom,
663 xstart, ystart, xend, yend, 1 );
667 /***********************************************************************
668 * X11DRV_Ellipse
670 BOOL CDECL
671 X11DRV_Ellipse( X11DRV_PDEVICE *physDev, INT left, INT top, INT right, INT bottom )
673 INT width, oldwidth;
674 BOOL update = FALSE;
675 RECT rc;
677 SetRect(&rc, left, top, right, bottom);
678 LPtoDP(physDev->hdc, (POINT*)&rc, 2);
680 if ((rc.left == rc.right) || (rc.top == rc.bottom)) return TRUE;
682 if (rc.right < rc.left) { INT tmp = rc.right; rc.right = rc.left; rc.left = tmp; }
683 if (rc.bottom < rc.top) { INT tmp = rc.bottom; rc.bottom = rc.top; rc.top = tmp; }
685 oldwidth = width = physDev->pen.width;
686 if (!width) width = 1;
687 if(physDev->pen.style == PS_NULL) width = 0;
689 if ((physDev->pen.style == PS_INSIDEFRAME))
691 if (2*width > (rc.right-rc.left)) width=(rc.right-rc.left + 1)/2;
692 if (2*width > (rc.bottom-rc.top)) width=(rc.bottom-rc.top + 1)/2;
693 rc.left += width / 2;
694 rc.right -= (width - 1) / 2;
695 rc.top += width / 2;
696 rc.bottom -= (width - 1) / 2;
698 if(width == 0) width = 1; /* more accurate */
699 physDev->pen.width = width;
701 /* Update the pixmap from the DIB section */
702 X11DRV_LockDIBSection(physDev, DIB_Status_GdiMod);
704 if (X11DRV_SetupGCForBrush( physDev ))
706 wine_tsx11_lock();
707 XFillArc( gdi_display, physDev->drawable, physDev->gc,
708 physDev->dc_rect.left + rc.left, physDev->dc_rect.top + rc.top,
709 rc.right-rc.left-1, rc.bottom-rc.top-1, 0, 360*64 );
710 wine_tsx11_unlock();
711 update = TRUE;
713 if (X11DRV_SetupGCForPen( physDev ))
715 wine_tsx11_lock();
716 XDrawArc( gdi_display, physDev->drawable, physDev->gc,
717 physDev->dc_rect.left + rc.left, physDev->dc_rect.top + rc.top,
718 rc.right-rc.left-1, rc.bottom-rc.top-1, 0, 360*64 );
719 wine_tsx11_unlock();
720 update = TRUE;
723 /* Update the DIBSection from the pixmap */
724 X11DRV_UnlockDIBSection(physDev, update);
726 physDev->pen.width = oldwidth;
727 return TRUE;
731 /***********************************************************************
732 * X11DRV_Rectangle
734 BOOL CDECL
735 X11DRV_Rectangle(X11DRV_PDEVICE *physDev, INT left, INT top, INT right, INT bottom)
737 INT width, oldwidth, oldjoinstyle;
738 BOOL update = FALSE;
739 RECT rc;
741 TRACE("(%d %d %d %d)\n", left, top, right, bottom);
743 SetRect(&rc, left, top, right, bottom);
744 LPtoDP(physDev->hdc, (POINT*)&rc, 2);
746 if ((rc.left == rc.right) || (rc.top == rc.bottom)) return TRUE;
748 if (rc.right < rc.left) { INT tmp = rc.right; rc.right = rc.left; rc.left = tmp; }
749 if (rc.bottom < rc.top) { INT tmp = rc.bottom; rc.bottom = rc.top; rc.top = tmp; }
751 oldwidth = width = physDev->pen.width;
752 if (!width) width = 1;
753 if(physDev->pen.style == PS_NULL) width = 0;
755 if ((physDev->pen.style == PS_INSIDEFRAME))
757 if (2*width > (rc.right-rc.left)) width=(rc.right-rc.left + 1)/2;
758 if (2*width > (rc.bottom-rc.top)) width=(rc.bottom-rc.top + 1)/2;
759 rc.left += width / 2;
760 rc.right -= (width - 1) / 2;
761 rc.top += width / 2;
762 rc.bottom -= (width - 1) / 2;
764 if(width == 1) width = 0;
765 physDev->pen.width = width;
766 oldjoinstyle = physDev->pen.linejoin;
767 if(physDev->pen.type != PS_GEOMETRIC)
768 physDev->pen.linejoin = PS_JOIN_MITER;
770 /* Update the pixmap from the DIB section */
771 X11DRV_LockDIBSection(physDev, DIB_Status_GdiMod);
773 if ((rc.right > rc.left + width) && (rc.bottom > rc.top + width))
775 if (X11DRV_SetupGCForBrush( physDev ))
777 wine_tsx11_lock();
778 XFillRectangle( gdi_display, physDev->drawable, physDev->gc,
779 physDev->dc_rect.left + rc.left + (width + 1) / 2,
780 physDev->dc_rect.top + rc.top + (width + 1) / 2,
781 rc.right-rc.left-width-1, rc.bottom-rc.top-width-1);
782 wine_tsx11_unlock();
783 update = TRUE;
786 if (X11DRV_SetupGCForPen( physDev ))
788 wine_tsx11_lock();
789 XDrawRectangle( gdi_display, physDev->drawable, physDev->gc,
790 physDev->dc_rect.left + rc.left, physDev->dc_rect.top + rc.top,
791 rc.right-rc.left-1, rc.bottom-rc.top-1 );
792 wine_tsx11_unlock();
793 update = TRUE;
796 /* Update the DIBSection from the pixmap */
797 X11DRV_UnlockDIBSection(physDev, update);
799 physDev->pen.width = oldwidth;
800 physDev->pen.linejoin = oldjoinstyle;
801 return TRUE;
804 /***********************************************************************
805 * X11DRV_RoundRect
807 BOOL CDECL
808 X11DRV_RoundRect( X11DRV_PDEVICE *physDev, INT left, INT top, INT right,
809 INT bottom, INT ell_width, INT ell_height )
811 INT width, oldwidth, oldendcap;
812 BOOL update = FALSE;
813 RECT rc;
814 POINT pts[2];
816 TRACE("(%d %d %d %d %d %d\n",
817 left, top, right, bottom, ell_width, ell_height);
819 SetRect(&rc, left, top, right, bottom);
820 LPtoDP(physDev->hdc, (POINT*)&rc, 2);
822 if ((rc.left == rc.right) || (rc.top == rc.bottom))
823 return TRUE;
825 /* Make sure ell_width and ell_height are >= 1 otherwise XDrawArc gets
826 called with width/height < 0 */
827 pts[0].x = pts[0].y = 0;
828 pts[1].x = ell_width;
829 pts[1].y = ell_height;
830 LPtoDP(physDev->hdc, pts, 2);
831 ell_width = max(abs( pts[1].x - pts[0].x ), 1);
832 ell_height = max(abs( pts[1].y - pts[0].y ), 1);
834 /* Fix the coordinates */
836 if (rc.right < rc.left) { INT tmp = rc.right; rc.right = rc.left; rc.left = tmp; }
837 if (rc.bottom < rc.top) { INT tmp = rc.bottom; rc.bottom = rc.top; rc.top = tmp; }
839 oldwidth = width = physDev->pen.width;
840 oldendcap = physDev->pen.endcap;
841 if (!width) width = 1;
842 if(physDev->pen.style == PS_NULL) width = 0;
844 if ((physDev->pen.style == PS_INSIDEFRAME))
846 if (2*width > (rc.right-rc.left)) width=(rc.right-rc.left + 1)/2;
847 if (2*width > (rc.bottom-rc.top)) width=(rc.bottom-rc.top + 1)/2;
848 rc.left += width / 2;
849 rc.right -= (width - 1) / 2;
850 rc.top += width / 2;
851 rc.bottom -= (width - 1) / 2;
853 if(width == 0) width = 1;
854 physDev->pen.width = width;
855 physDev->pen.endcap = PS_ENDCAP_SQUARE;
857 /* Update the pixmap from the DIB section */
858 X11DRV_LockDIBSection(physDev, DIB_Status_GdiMod);
860 if (X11DRV_SetupGCForBrush( physDev ))
862 wine_tsx11_lock();
863 if (ell_width > (rc.right-rc.left) )
864 if (ell_height > (rc.bottom-rc.top) )
865 XFillArc( gdi_display, physDev->drawable, physDev->gc,
866 physDev->dc_rect.left + rc.left, physDev->dc_rect.top + rc.top,
867 rc.right - rc.left - 1, rc.bottom - rc.top - 1,
868 0, 360 * 64 );
869 else{
870 XFillArc( gdi_display, physDev->drawable, physDev->gc,
871 physDev->dc_rect.left + rc.left, physDev->dc_rect.top + rc.top,
872 rc.right - rc.left - 1, ell_height, 0, 180 * 64 );
873 XFillArc( gdi_display, physDev->drawable, physDev->gc,
874 physDev->dc_rect.left + rc.left,
875 physDev->dc_rect.top + rc.bottom - ell_height - 1,
876 rc.right - rc.left - 1, ell_height, 180 * 64,
877 180 * 64 );
879 else if (ell_height > (rc.bottom-rc.top) ){
880 XFillArc( gdi_display, physDev->drawable, physDev->gc,
881 physDev->dc_rect.left + rc.left, physDev->dc_rect.top + rc.top,
882 ell_width, rc.bottom - rc.top - 1, 90 * 64, 180 * 64 );
883 XFillArc( gdi_display, physDev->drawable, physDev->gc,
884 physDev->dc_rect.left + rc.right - ell_width - 1, physDev->dc_rect.top + rc.top,
885 ell_width, rc.bottom - rc.top - 1, 270 * 64, 180 * 64 );
886 }else{
887 XFillArc( gdi_display, physDev->drawable, physDev->gc,
888 physDev->dc_rect.left + rc.left, physDev->dc_rect.top + rc.top,
889 ell_width, ell_height, 90 * 64, 90 * 64 );
890 XFillArc( gdi_display, physDev->drawable, physDev->gc,
891 physDev->dc_rect.left + rc.left,
892 physDev->dc_rect.top + rc.bottom - ell_height - 1,
893 ell_width, ell_height, 180 * 64, 90 * 64 );
894 XFillArc( gdi_display, physDev->drawable, physDev->gc,
895 physDev->dc_rect.left + rc.right - ell_width - 1,
896 physDev->dc_rect.top + rc.bottom - ell_height - 1,
897 ell_width, ell_height, 270 * 64, 90 * 64 );
898 XFillArc( gdi_display, physDev->drawable, physDev->gc,
899 physDev->dc_rect.left + rc.right - ell_width - 1,
900 physDev->dc_rect.top + rc.top,
901 ell_width, ell_height, 0, 90 * 64 );
903 if (ell_width < rc.right - rc.left)
905 XFillRectangle( gdi_display, physDev->drawable, physDev->gc,
906 physDev->dc_rect.left + rc.left + (ell_width + 1) / 2,
907 physDev->dc_rect.top + rc.top + 1,
908 rc.right - rc.left - ell_width - 1,
909 (ell_height + 1) / 2 - 1);
910 XFillRectangle( gdi_display, physDev->drawable, physDev->gc,
911 physDev->dc_rect.left + rc.left + (ell_width + 1) / 2,
912 physDev->dc_rect.top + rc.bottom - (ell_height) / 2 - 1,
913 rc.right - rc.left - ell_width - 1,
914 (ell_height) / 2 );
916 if (ell_height < rc.bottom - rc.top)
918 XFillRectangle( gdi_display, physDev->drawable, physDev->gc,
919 physDev->dc_rect.left + rc.left + 1,
920 physDev->dc_rect.top + rc.top + (ell_height + 1) / 2,
921 rc.right - rc.left - 2,
922 rc.bottom - rc.top - ell_height - 1);
924 wine_tsx11_unlock();
925 update = TRUE;
927 /* FIXME: this could be done with on X call
928 * more efficient and probably more correct
929 * on any X server: XDrawArcs will draw
930 * straight horizontal and vertical lines
931 * if width or height are zero.
933 * BTW this stuff is optimized for an Xfree86 server
934 * read the comments inside the X11DRV_DrawArc function
936 if (X11DRV_SetupGCForPen( physDev ))
938 wine_tsx11_lock();
939 if (ell_width > (rc.right-rc.left) )
940 if (ell_height > (rc.bottom-rc.top) )
941 XDrawArc( gdi_display, physDev->drawable, physDev->gc,
942 physDev->dc_rect.left + rc.left, physDev->dc_rect.top + rc.top,
943 rc.right - rc.left - 1, rc.bottom - rc.top - 1, 0 , 360 * 64 );
944 else{
945 XDrawArc( gdi_display, physDev->drawable, physDev->gc,
946 physDev->dc_rect.left + rc.left, physDev->dc_rect.top + rc.top,
947 rc.right - rc.left - 1, ell_height - 1, 0 , 180 * 64 );
948 XDrawArc( gdi_display, physDev->drawable, physDev->gc,
949 physDev->dc_rect.left + rc.left,
950 physDev->dc_rect.top + rc.bottom - ell_height,
951 rc.right - rc.left - 1, ell_height - 1, 180 * 64 , 180 * 64 );
953 else if (ell_height > (rc.bottom-rc.top) ){
954 XDrawArc( gdi_display, physDev->drawable, physDev->gc,
955 physDev->dc_rect.left + rc.left, physDev->dc_rect.top + rc.top,
956 ell_width - 1 , rc.bottom - rc.top - 1, 90 * 64 , 180 * 64 );
957 XDrawArc( gdi_display, physDev->drawable, physDev->gc,
958 physDev->dc_rect.left + rc.right - ell_width,
959 physDev->dc_rect.top + rc.top,
960 ell_width - 1 , rc.bottom - rc.top - 1, 270 * 64 , 180 * 64 );
961 }else{
962 XDrawArc( gdi_display, physDev->drawable, physDev->gc,
963 physDev->dc_rect.left + rc.left, physDev->dc_rect.top + rc.top,
964 ell_width - 1, ell_height - 1, 90 * 64, 90 * 64 );
965 XDrawArc( gdi_display, physDev->drawable, physDev->gc,
966 physDev->dc_rect.left + rc.left, physDev->dc_rect.top + rc.bottom - ell_height,
967 ell_width - 1, ell_height - 1, 180 * 64, 90 * 64 );
968 XDrawArc( gdi_display, physDev->drawable, physDev->gc,
969 physDev->dc_rect.left + rc.right - ell_width,
970 physDev->dc_rect.top + rc.bottom - ell_height,
971 ell_width - 1, ell_height - 1, 270 * 64, 90 * 64 );
972 XDrawArc( gdi_display, physDev->drawable, physDev->gc,
973 physDev->dc_rect.left + rc.right - ell_width, physDev->dc_rect.top + rc.top,
974 ell_width - 1, ell_height - 1, 0, 90 * 64 );
976 if (ell_width < rc.right - rc.left)
978 XDrawLine( gdi_display, physDev->drawable, physDev->gc,
979 physDev->dc_rect.left + rc.left + ell_width / 2,
980 physDev->dc_rect.top + rc.top,
981 physDev->dc_rect.left + rc.right - (ell_width+1) / 2,
982 physDev->dc_rect.top + rc.top);
983 XDrawLine( gdi_display, physDev->drawable, physDev->gc,
984 physDev->dc_rect.left + rc.left + ell_width / 2 ,
985 physDev->dc_rect.top + rc.bottom - 1,
986 physDev->dc_rect.left + rc.right - (ell_width+1)/ 2,
987 physDev->dc_rect.top + rc.bottom - 1);
989 if (ell_height < rc.bottom - rc.top)
991 XDrawLine( gdi_display, physDev->drawable, physDev->gc,
992 physDev->dc_rect.left + rc.right - 1,
993 physDev->dc_rect.top + rc.top + ell_height / 2,
994 physDev->dc_rect.left + rc.right - 1,
995 physDev->dc_rect.top + rc.bottom - (ell_height+1) / 2);
996 XDrawLine( gdi_display, physDev->drawable, physDev->gc,
997 physDev->dc_rect.left + rc.left,
998 physDev->dc_rect.top + rc.top + ell_height / 2,
999 physDev->dc_rect.left + rc.left,
1000 physDev->dc_rect.top + rc.bottom - (ell_height+1) / 2);
1002 wine_tsx11_unlock();
1003 update = TRUE;
1005 /* Update the DIBSection from the pixmap */
1006 X11DRV_UnlockDIBSection(physDev, update);
1008 physDev->pen.width = oldwidth;
1009 physDev->pen.endcap = oldendcap;
1010 return TRUE;
1014 /***********************************************************************
1015 * X11DRV_SetPixel
1017 COLORREF CDECL
1018 X11DRV_SetPixel( X11DRV_PDEVICE *physDev, INT x, INT y, COLORREF color )
1020 unsigned long pixel;
1021 POINT pt;
1023 pt.x = x;
1024 pt.y = y;
1025 LPtoDP( physDev->hdc, &pt, 1 );
1026 pixel = X11DRV_PALETTE_ToPhysical( physDev, color );
1028 /* Update the pixmap from the DIB section */
1029 X11DRV_LockDIBSection(physDev, DIB_Status_GdiMod);
1031 /* inefficient but simple... */
1032 wine_tsx11_lock();
1033 XSetForeground( gdi_display, physDev->gc, pixel );
1034 XSetFunction( gdi_display, physDev->gc, GXcopy );
1035 XDrawPoint( gdi_display, physDev->drawable, physDev->gc,
1036 physDev->dc_rect.left + pt.x, physDev->dc_rect.top + pt.y );
1037 wine_tsx11_unlock();
1039 /* Update the DIBSection from the pixmap */
1040 X11DRV_UnlockDIBSection(physDev, TRUE);
1042 return X11DRV_PALETTE_ToLogical(physDev, pixel);
1046 /***********************************************************************
1047 * X11DRV_GetPixel
1049 COLORREF CDECL
1050 X11DRV_GetPixel( X11DRV_PDEVICE *physDev, INT x, INT y )
1052 static Pixmap pixmap = 0;
1053 XImage * image;
1054 int pixel;
1055 POINT pt;
1056 BOOL memdc = (GetObjectType(physDev->hdc) == OBJ_MEMDC);
1058 pt.x = x;
1059 pt.y = y;
1060 LPtoDP( physDev->hdc, &pt, 1 );
1062 /* Update the pixmap from the DIB section */
1063 X11DRV_LockDIBSection(physDev, DIB_Status_GdiMod);
1065 wine_tsx11_lock();
1066 if (memdc)
1068 image = XGetImage( gdi_display, physDev->drawable,
1069 physDev->dc_rect.left + pt.x, physDev->dc_rect.top + pt.y,
1070 1, 1, AllPlanes, ZPixmap );
1072 else
1074 /* If we are reading from the screen, use a temporary copy */
1075 /* to avoid a BadMatch error */
1076 if (!pixmap) pixmap = XCreatePixmap( gdi_display, root_window,
1077 1, 1, physDev->depth );
1078 XCopyArea( gdi_display, physDev->drawable, pixmap, get_bitmap_gc(physDev->depth),
1079 physDev->dc_rect.left + pt.x, physDev->dc_rect.top + pt.y, 1, 1, 0, 0 );
1080 image = XGetImage( gdi_display, pixmap, 0, 0, 1, 1, AllPlanes, ZPixmap );
1082 pixel = XGetPixel( image, 0, 0 );
1083 XDestroyImage( image );
1084 wine_tsx11_unlock();
1086 /* Update the DIBSection from the pixmap */
1087 X11DRV_UnlockDIBSection(physDev, FALSE);
1088 if( physDev->depth > 1)
1089 pixel = X11DRV_PALETTE_ToLogical(physDev, pixel);
1090 else
1091 /* monochrome bitmaps return black or white */
1092 if( pixel) pixel = 0xffffff;
1093 return pixel;
1098 /***********************************************************************
1099 * X11DRV_PaintRgn
1101 BOOL CDECL
1102 X11DRV_PaintRgn( X11DRV_PDEVICE *physDev, HRGN hrgn )
1104 if (X11DRV_SetupGCForBrush( physDev ))
1106 unsigned int i;
1107 XRectangle *rect;
1108 RGNDATA *data = X11DRV_GetRegionData( hrgn, physDev->hdc );
1110 if (!data) return FALSE;
1111 rect = (XRectangle *)data->Buffer;
1112 for (i = 0; i < data->rdh.nCount; i++)
1114 rect[i].x += physDev->dc_rect.left;
1115 rect[i].y += physDev->dc_rect.top;
1118 X11DRV_LockDIBSection(physDev, DIB_Status_GdiMod);
1119 wine_tsx11_lock();
1120 XFillRectangles( gdi_display, physDev->drawable, physDev->gc, rect, data->rdh.nCount );
1121 wine_tsx11_unlock();
1122 X11DRV_UnlockDIBSection(physDev, TRUE);
1123 HeapFree( GetProcessHeap(), 0, data );
1125 return TRUE;
1128 /**********************************************************************
1129 * X11DRV_Polyline
1131 BOOL CDECL
1132 X11DRV_Polyline( X11DRV_PDEVICE *physDev, const POINT* pt, INT count )
1134 int i;
1135 XPoint *points;
1137 if (!(points = HeapAlloc( GetProcessHeap(), 0, sizeof(XPoint) * count )))
1139 WARN("No memory to convert POINTs to XPoints!\n");
1140 return FALSE;
1142 for (i = 0; i < count; i++)
1144 POINT tmp = pt[i];
1145 LPtoDP(physDev->hdc, &tmp, 1);
1146 points[i].x = physDev->dc_rect.left + tmp.x;
1147 points[i].y = physDev->dc_rect.top + tmp.y;
1150 if (X11DRV_SetupGCForPen ( physDev ))
1152 X11DRV_LockDIBSection(physDev, DIB_Status_GdiMod);
1153 wine_tsx11_lock();
1154 XDrawLines( gdi_display, physDev->drawable, physDev->gc,
1155 points, count, CoordModeOrigin );
1156 wine_tsx11_unlock();
1157 X11DRV_UnlockDIBSection(physDev, TRUE);
1160 HeapFree( GetProcessHeap(), 0, points );
1161 return TRUE;
1165 /**********************************************************************
1166 * X11DRV_Polygon
1168 BOOL CDECL
1169 X11DRV_Polygon( X11DRV_PDEVICE *physDev, const POINT* pt, INT count )
1171 register int i;
1172 XPoint *points;
1173 BOOL update = FALSE;
1175 if (!(points = HeapAlloc( GetProcessHeap(), 0, sizeof(XPoint) * (count+1) )))
1177 WARN("No memory to convert POINTs to XPoints!\n");
1178 return FALSE;
1180 for (i = 0; i < count; i++)
1182 POINT tmp = pt[i];
1183 LPtoDP(physDev->hdc, &tmp, 1);
1184 points[i].x = physDev->dc_rect.left + tmp.x;
1185 points[i].y = physDev->dc_rect.top + tmp.y;
1187 points[count] = points[0];
1189 /* Update the pixmap from the DIB section */
1190 X11DRV_LockDIBSection(physDev, DIB_Status_GdiMod);
1192 if (X11DRV_SetupGCForBrush( physDev ))
1194 wine_tsx11_lock();
1195 XFillPolygon( gdi_display, physDev->drawable, physDev->gc,
1196 points, count+1, Complex, CoordModeOrigin);
1197 wine_tsx11_unlock();
1198 update = TRUE;
1200 if (X11DRV_SetupGCForPen ( physDev ))
1202 wine_tsx11_lock();
1203 XDrawLines( gdi_display, physDev->drawable, physDev->gc,
1204 points, count+1, CoordModeOrigin );
1205 wine_tsx11_unlock();
1206 update = TRUE;
1209 /* Update the DIBSection from the pixmap */
1210 X11DRV_UnlockDIBSection(physDev, update);
1212 HeapFree( GetProcessHeap(), 0, points );
1213 return TRUE;
1217 /**********************************************************************
1218 * X11DRV_PolyPolygon
1220 BOOL CDECL
1221 X11DRV_PolyPolygon( X11DRV_PDEVICE *physDev, const POINT* pt, const INT* counts, UINT polygons)
1223 HRGN hrgn;
1225 /* FIXME: The points should be converted to device coords before */
1226 /* creating the region. */
1228 hrgn = CreatePolyPolygonRgn( pt, counts, polygons, GetPolyFillMode( physDev->hdc ) );
1229 X11DRV_PaintRgn( physDev, hrgn );
1230 DeleteObject( hrgn );
1232 /* Draw the outline of the polygons */
1234 if (X11DRV_SetupGCForPen ( physDev ))
1236 unsigned int i;
1237 int j, max = 0;
1238 XPoint *points;
1240 /* Update the pixmap from the DIB section */
1241 X11DRV_LockDIBSection(physDev, DIB_Status_GdiMod);
1243 for (i = 0; i < polygons; i++) if (counts[i] > max) max = counts[i];
1244 if (!(points = HeapAlloc( GetProcessHeap(), 0, sizeof(XPoint) * (max+1) )))
1246 WARN("No memory to convert POINTs to XPoints!\n");
1247 return FALSE;
1249 for (i = 0; i < polygons; i++)
1251 for (j = 0; j < counts[i]; j++)
1253 POINT tmp = *pt;
1254 LPtoDP(physDev->hdc, &tmp, 1);
1255 points[j].x = physDev->dc_rect.left + tmp.x;
1256 points[j].y = physDev->dc_rect.top + tmp.y;
1257 pt++;
1259 points[j] = points[0];
1260 wine_tsx11_lock();
1261 XDrawLines( gdi_display, physDev->drawable, physDev->gc,
1262 points, j + 1, CoordModeOrigin );
1263 wine_tsx11_unlock();
1266 /* Update the DIBSection of the dc's bitmap */
1267 X11DRV_UnlockDIBSection(physDev, TRUE);
1269 HeapFree( GetProcessHeap(), 0, points );
1271 return TRUE;
1275 /**********************************************************************
1276 * X11DRV_PolyPolyline
1278 BOOL CDECL
1279 X11DRV_PolyPolyline( X11DRV_PDEVICE *physDev, const POINT* pt, const DWORD* counts, DWORD polylines )
1281 if (X11DRV_SetupGCForPen ( physDev ))
1283 unsigned int i, j, max = 0;
1284 XPoint *points;
1286 /* Update the pixmap from the DIB section */
1287 X11DRV_LockDIBSection(physDev, DIB_Status_GdiMod);
1289 for (i = 0; i < polylines; i++) if (counts[i] > max) max = counts[i];
1290 if (!(points = HeapAlloc( GetProcessHeap(), 0, sizeof(XPoint) * max )))
1292 WARN("No memory to convert POINTs to XPoints!\n");
1293 return FALSE;
1295 for (i = 0; i < polylines; i++)
1297 for (j = 0; j < counts[i]; j++)
1299 POINT tmp = *pt;
1300 LPtoDP(physDev->hdc, &tmp, 1);
1301 points[j].x = physDev->dc_rect.left + tmp.x;
1302 points[j].y = physDev->dc_rect.top + tmp.y;
1303 pt++;
1305 wine_tsx11_lock();
1306 XDrawLines( gdi_display, physDev->drawable, physDev->gc,
1307 points, j, CoordModeOrigin );
1308 wine_tsx11_unlock();
1311 /* Update the DIBSection of the dc's bitmap */
1312 X11DRV_UnlockDIBSection(physDev, TRUE);
1314 HeapFree( GetProcessHeap(), 0, points );
1316 return TRUE;
1320 /**********************************************************************
1321 * X11DRV_InternalFloodFill
1323 * Internal helper function for flood fill.
1324 * (xorg,yorg) is the origin of the X image relative to the drawable.
1325 * (x,y) is relative to the origin of the X image.
1327 static void X11DRV_InternalFloodFill(XImage *image, X11DRV_PDEVICE *physDev,
1328 int x, int y,
1329 int xOrg, int yOrg,
1330 unsigned long pixel, WORD fillType )
1332 int left, right;
1334 #define TO_FLOOD(x,y) ((fillType == FLOODFILLBORDER) ? \
1335 (XGetPixel(image,x,y) != pixel) : \
1336 (XGetPixel(image,x,y) == pixel))
1338 if (!TO_FLOOD(x,y)) return;
1340 /* Find left and right boundaries */
1342 left = right = x;
1343 while ((left > 0) && TO_FLOOD( left-1, y )) left--;
1344 while ((right < image->width) && TO_FLOOD( right, y )) right++;
1345 XFillRectangle( gdi_display, physDev->drawable, physDev->gc,
1346 xOrg + left, yOrg + y, right-left, 1 );
1348 /* Set the pixels of this line so we don't fill it again */
1350 for (x = left; x < right; x++)
1352 if (fillType == FLOODFILLBORDER) XPutPixel( image, x, y, pixel );
1353 else XPutPixel( image, x, y, ~pixel );
1356 /* Fill the line above */
1358 if (--y >= 0)
1360 x = left;
1361 while (x < right)
1363 while ((x < right) && !TO_FLOOD(x,y)) x++;
1364 if (x >= right) break;
1365 while ((x < right) && TO_FLOOD(x,y)) x++;
1366 X11DRV_InternalFloodFill(image, physDev, x-1, y,
1367 xOrg, yOrg, pixel, fillType );
1371 /* Fill the line below */
1373 if ((y += 2) < image->height)
1375 x = left;
1376 while (x < right)
1378 while ((x < right) && !TO_FLOOD(x,y)) x++;
1379 if (x >= right) break;
1380 while ((x < right) && TO_FLOOD(x,y)) x++;
1381 X11DRV_InternalFloodFill(image, physDev, x-1, y,
1382 xOrg, yOrg, pixel, fillType );
1385 #undef TO_FLOOD
1389 static int ExtFloodFillXGetImageErrorHandler( Display *dpy, XErrorEvent *event, void *arg )
1391 return (event->request_code == X_GetImage && event->error_code == BadMatch);
1394 /**********************************************************************
1395 * X11DRV_ExtFloodFill
1397 BOOL CDECL
1398 X11DRV_ExtFloodFill( X11DRV_PDEVICE *physDev, INT x, INT y, COLORREF color,
1399 UINT fillType )
1401 XImage *image;
1402 RECT rect;
1403 POINT pt;
1405 TRACE("X11DRV_ExtFloodFill %d,%d %06x %d\n", x, y, color, fillType );
1407 pt.x = x;
1408 pt.y = y;
1409 LPtoDP( physDev->hdc, &pt, 1 );
1410 if (!PtInRegion( physDev->region, pt.x, pt.y )) return FALSE;
1411 GetRgnBox( physDev->region, &rect );
1413 X11DRV_expect_error( gdi_display, ExtFloodFillXGetImageErrorHandler, NULL );
1414 image = XGetImage( gdi_display, physDev->drawable,
1415 physDev->dc_rect.left + rect.left, physDev->dc_rect.top + rect.top,
1416 rect.right - rect.left, rect.bottom - rect.top,
1417 AllPlanes, ZPixmap );
1418 if(X11DRV_check_error()) image = NULL;
1419 if (!image) return FALSE;
1421 if (X11DRV_SetupGCForBrush( physDev ))
1423 unsigned long pixel = X11DRV_PALETTE_ToPhysical( physDev, color );
1425 /* Update the pixmap from the DIB section */
1426 X11DRV_LockDIBSection(physDev, DIB_Status_GdiMod);
1428 /* ROP mode is always GXcopy for flood-fill */
1429 wine_tsx11_lock();
1430 XSetFunction( gdi_display, physDev->gc, GXcopy );
1431 X11DRV_InternalFloodFill(image, physDev,
1432 pt.x - rect.left,
1433 pt.y - rect.top,
1434 physDev->dc_rect.left + rect.left,
1435 physDev->dc_rect.top + rect.top,
1436 pixel, fillType );
1437 wine_tsx11_unlock();
1438 /* Update the DIBSection of the dc's bitmap */
1439 X11DRV_UnlockDIBSection(physDev, TRUE);
1442 wine_tsx11_lock();
1443 XDestroyImage( image );
1444 wine_tsx11_unlock();
1445 return TRUE;
1448 /**********************************************************************
1449 * X11DRV_SetBkColor
1451 COLORREF CDECL
1452 X11DRV_SetBkColor( X11DRV_PDEVICE *physDev, COLORREF color )
1454 physDev->backgroundPixel = X11DRV_PALETTE_ToPhysical( physDev, color );
1455 return color;
1458 /**********************************************************************
1459 * X11DRV_SetTextColor
1461 COLORREF CDECL
1462 X11DRV_SetTextColor( X11DRV_PDEVICE *physDev, COLORREF color )
1464 physDev->textPixel = X11DRV_PALETTE_ToPhysical( physDev, color );
1465 return color;
1468 /***********************************************************************
1469 * GetDCOrgEx (X11DRV.@)
1471 BOOL CDECL X11DRV_GetDCOrgEx( X11DRV_PDEVICE *physDev, LPPOINT lpp )
1473 lpp->x = physDev->dc_rect.left + physDev->drawable_rect.left;
1474 lpp->y = physDev->dc_rect.top + physDev->drawable_rect.top;
1475 return TRUE;
1479 static unsigned char *get_icm_profile( unsigned long *size )
1481 Atom type;
1482 int format;
1483 unsigned long count, remaining;
1484 unsigned char *profile, *ret = NULL;
1486 wine_tsx11_lock();
1487 XGetWindowProperty( gdi_display, DefaultRootWindow(gdi_display),
1488 x11drv_atom(_ICC_PROFILE), 0, ~0UL, False, AnyPropertyType,
1489 &type, &format, &count, &remaining, &profile );
1490 *size = get_property_size( format, count );
1491 if (format && count)
1493 if ((ret = HeapAlloc( GetProcessHeap(), 0, *size ))) memcpy( ret, profile, *size );
1494 XFree( profile );
1496 wine_tsx11_unlock();
1497 return ret;
1500 typedef struct
1502 unsigned int unknown[6];
1503 unsigned int state[5];
1504 unsigned int count[2];
1505 unsigned char buffer[64];
1506 } sha_ctx;
1508 extern void WINAPI A_SHAInit( sha_ctx * );
1509 extern void WINAPI A_SHAUpdate( sha_ctx *, const unsigned char *, unsigned int );
1510 extern void WINAPI A_SHAFinal( sha_ctx *, unsigned char * );
1512 /***********************************************************************
1513 * GetICMProfile (X11DRV.@)
1515 BOOL CDECL X11DRV_GetICMProfile( X11DRV_PDEVICE *physDev, LPDWORD size, LPWSTR filename )
1517 static const WCHAR path[] =
1518 {'\\','s','p','o','o','l','\\','d','r','i','v','e','r','s',
1519 '\\','c','o','l','o','r','\\',0};
1520 static const WCHAR srgb[] =
1521 {'s','R','G','B',' ','C','o','l','o','r',' ','S','p','a','c','e',' ',
1522 'P','r','o','f','i','l','e','.','i','c','m',0};
1523 static const WCHAR mntr[] =
1524 {'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
1525 'W','i','n','d','o','w','s',' ','N','T','\\','C','u','r','r','e','n','t',
1526 'V','e','r','s','i','o','n','\\','I','C','M','\\','m','n','t','r',0};
1528 HKEY hkey;
1529 DWORD required, len;
1530 WCHAR profile[MAX_PATH], fullname[2*MAX_PATH + sizeof(path)/sizeof(WCHAR)];
1531 unsigned char *buffer;
1532 unsigned long buflen;
1534 if (!size) return FALSE;
1536 GetSystemDirectoryW( fullname, MAX_PATH );
1537 strcatW( fullname, path );
1539 len = sizeof(profile)/sizeof(WCHAR);
1540 if (!RegCreateKeyExW( HKEY_LOCAL_MACHINE, mntr, 0, NULL, 0, KEY_ALL_ACCESS, NULL, &hkey, NULL ) &&
1541 !RegEnumValueW( hkey, 0, profile, &len, NULL, NULL, NULL, NULL )) /* FIXME handle multiple values */
1543 strcatW( fullname, profile );
1544 RegCloseKey( hkey );
1546 else if ((buffer = get_icm_profile( &buflen )))
1548 static const WCHAR fmt[] = {'%','0','2','x',0};
1549 static const WCHAR icm[] = {'.','i','c','m',0};
1551 unsigned char sha1sum[20];
1552 unsigned int i;
1553 sha_ctx ctx;
1554 HANDLE file;
1556 A_SHAInit( &ctx );
1557 A_SHAUpdate( &ctx, buffer, buflen );
1558 A_SHAFinal( &ctx, sha1sum );
1560 for (i = 0; i < sizeof(sha1sum); i++) sprintfW( &profile[i * 2], fmt, sha1sum[i] );
1561 memcpy( &profile[i * 2], icm, sizeof(icm) );
1563 strcatW( fullname, profile );
1564 file = CreateFileW( fullname, GENERIC_WRITE, 0, NULL, CREATE_NEW, 0, 0 );
1565 if (file != INVALID_HANDLE_VALUE)
1567 DWORD written;
1569 if (!WriteFile( file, buffer, buflen, &written, NULL ) || written != buflen)
1570 ERR( "Unable to write color profile\n" );
1571 CloseHandle( file );
1573 HeapFree( GetProcessHeap(), 0, buffer );
1575 else strcatW( fullname, srgb );
1577 required = strlenW( fullname ) + 1;
1578 if (*size < required)
1580 *size = required;
1581 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1582 return FALSE;
1584 if (filename)
1586 strcpyW( filename, fullname );
1587 if (GetFileAttributesW( filename ) == INVALID_FILE_ATTRIBUTES)
1588 WARN( "color profile not found\n" );
1590 *size = required;
1591 return TRUE;