msvcr100: The msvcr100 debug channel is unused so remove it.
[wine/testsucceed.git] / dlls / winex11.drv / graphics.c
blob53cbab40f60270c8d93c2a541d7a646a9a53bfb2
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 /***********************************************************************
77 * X11DRV_GetRegionData
79 * Calls GetRegionData on the given region and converts the rectangle
80 * array to XRectangle format. The returned buffer must be freed by
81 * caller using HeapFree(GetProcessHeap(),...).
82 * If hdc_lptodp is not 0, the rectangles are converted through LPtoDP.
84 RGNDATA *X11DRV_GetRegionData( HRGN hrgn, HDC hdc_lptodp )
86 RGNDATA *data;
87 DWORD size;
88 unsigned int i;
89 RECT *rect, tmp;
90 XRectangle *xrect;
92 if (!(size = GetRegionData( hrgn, 0, NULL ))) return NULL;
93 if (sizeof(XRectangle) > sizeof(RECT))
95 /* add extra size for XRectangle array */
96 int count = (size - sizeof(RGNDATAHEADER)) / sizeof(RECT);
97 size += count * (sizeof(XRectangle) - sizeof(RECT));
99 if (!(data = HeapAlloc( GetProcessHeap(), 0, size ))) return NULL;
100 if (!GetRegionData( hrgn, size, data ))
102 HeapFree( GetProcessHeap(), 0, data );
103 return NULL;
106 rect = (RECT *)data->Buffer;
107 xrect = (XRectangle *)data->Buffer;
108 if (hdc_lptodp) /* map to device coordinates */
110 LPtoDP( hdc_lptodp, (POINT *)rect, data->rdh.nCount * 2 );
111 for (i = 0; i < data->rdh.nCount; i++)
113 if (rect[i].right < rect[i].left)
115 INT tmp = rect[i].right;
116 rect[i].right = rect[i].left;
117 rect[i].left = tmp;
119 if (rect[i].bottom < rect[i].top)
121 INT tmp = rect[i].bottom;
122 rect[i].bottom = rect[i].top;
123 rect[i].top = tmp;
128 if (sizeof(XRectangle) > sizeof(RECT))
130 int j;
131 /* need to start from the end */
132 for (j = data->rdh.nCount-1; j >= 0; j--)
134 tmp = rect[j];
135 xrect[j].x = max( min( tmp.left, SHRT_MAX), SHRT_MIN);
136 xrect[j].y = max( min( tmp.top, SHRT_MAX), SHRT_MIN);
137 xrect[j].width = max( min( tmp.right - xrect[j].x, USHRT_MAX), 0);
138 xrect[j].height = max( min( tmp.bottom - xrect[j].y, USHRT_MAX), 0);
141 else
143 for (i = 0; i < data->rdh.nCount; i++)
145 tmp = rect[i];
146 xrect[i].x = max( min( tmp.left, SHRT_MAX), SHRT_MIN);
147 xrect[i].y = max( min( tmp.top, SHRT_MAX), SHRT_MIN);
148 xrect[i].width = max( min( tmp.right - xrect[i].x, USHRT_MAX), 0);
149 xrect[i].height = max( min( tmp.bottom - xrect[i].y, USHRT_MAX), 0);
152 return data;
156 /***********************************************************************
157 * X11DRV_SetDeviceClipping
159 void CDECL X11DRV_SetDeviceClipping( X11DRV_PDEVICE *physDev, HRGN vis_rgn, HRGN clip_rgn )
161 RGNDATA *data;
163 CombineRgn( physDev->region, vis_rgn, clip_rgn, clip_rgn ? RGN_AND : RGN_COPY );
164 if (!(data = X11DRV_GetRegionData( physDev->region, 0 ))) return;
166 wine_tsx11_lock();
167 XSetClipRectangles( gdi_display, physDev->gc, physDev->dc_rect.left, physDev->dc_rect.top,
168 (XRectangle *)data->Buffer, data->rdh.nCount, YXBanded );
169 wine_tsx11_unlock();
171 if (physDev->xrender) X11DRV_XRender_SetDeviceClipping(physDev, data);
173 HeapFree( GetProcessHeap(), 0, data );
177 /***********************************************************************
178 * X11DRV_SetupGCForPatBlt
180 * Setup the GC for a PatBlt operation using current brush.
181 * If fMapColors is TRUE, X pixels are mapped to Windows colors.
182 * Return FALSE if brush is BS_NULL, TRUE otherwise.
184 BOOL X11DRV_SetupGCForPatBlt( X11DRV_PDEVICE *physDev, GC gc, BOOL fMapColors )
186 XGCValues val;
187 unsigned long mask;
188 Pixmap pixmap = 0;
189 POINT pt;
191 if (physDev->brush.style == BS_NULL) return FALSE;
192 if (physDev->brush.pixel == -1)
194 /* Special case used for monochrome pattern brushes.
195 * We need to swap foreground and background because
196 * Windows does it the wrong way...
198 val.foreground = physDev->backgroundPixel;
199 val.background = physDev->textPixel;
201 else
203 val.foreground = physDev->brush.pixel;
204 val.background = physDev->backgroundPixel;
206 if (fMapColors && X11DRV_PALETTE_XPixelToPalette)
208 val.foreground = X11DRV_PALETTE_XPixelToPalette[val.foreground];
209 val.background = X11DRV_PALETTE_XPixelToPalette[val.background];
212 val.function = X11DRV_XROPfunction[GetROP2(physDev->hdc)-1];
214 ** Let's replace GXinvert by GXxor with (black xor white)
215 ** This solves the selection color and leak problems in excel
216 ** FIXME : Let's do that only if we work with X-pixels, not with Win-pixels
218 if (val.function == GXinvert)
220 val.foreground = (WhitePixel( gdi_display, DefaultScreen(gdi_display) ) ^
221 BlackPixel( gdi_display, DefaultScreen(gdi_display) ));
222 val.function = GXxor;
224 val.fill_style = physDev->brush.fillStyle;
225 switch(val.fill_style)
227 case FillStippled:
228 case FillOpaqueStippled:
229 if (GetBkMode(physDev->hdc)==OPAQUE) val.fill_style = FillOpaqueStippled;
230 val.stipple = physDev->brush.pixmap;
231 mask = GCStipple;
232 break;
234 case FillTiled:
235 if (fMapColors && X11DRV_PALETTE_XPixelToPalette)
237 register int x, y;
238 XImage *image;
239 wine_tsx11_lock();
240 pixmap = XCreatePixmap( gdi_display, root_window, 8, 8, physDev->depth );
241 image = XGetImage( gdi_display, physDev->brush.pixmap, 0, 0, 8, 8,
242 AllPlanes, ZPixmap );
243 for (y = 0; y < 8; y++)
244 for (x = 0; x < 8; x++)
245 XPutPixel( image, x, y,
246 X11DRV_PALETTE_XPixelToPalette[XGetPixel( image, x, y)] );
247 XPutImage( gdi_display, pixmap, gc, image, 0, 0, 0, 0, 8, 8 );
248 XDestroyImage( image );
249 wine_tsx11_unlock();
250 val.tile = pixmap;
252 else val.tile = physDev->brush.pixmap;
253 mask = GCTile;
254 break;
256 default:
257 mask = 0;
258 break;
260 GetBrushOrgEx( physDev->hdc, &pt );
261 val.ts_x_origin = physDev->dc_rect.left + pt.x;
262 val.ts_y_origin = physDev->dc_rect.top + pt.y;
263 val.fill_rule = (GetPolyFillMode(physDev->hdc) == WINDING) ? WindingRule : EvenOddRule;
264 wine_tsx11_lock();
265 XChangeGC( gdi_display, gc,
266 GCFunction | GCForeground | GCBackground | GCFillStyle |
267 GCFillRule | GCTileStipXOrigin | GCTileStipYOrigin | mask,
268 &val );
269 if (pixmap) XFreePixmap( gdi_display, pixmap );
270 wine_tsx11_unlock();
271 return TRUE;
275 /***********************************************************************
276 * X11DRV_SetupGCForBrush
278 * Setup physDev->gc for drawing operations using current brush.
279 * Return FALSE if brush is BS_NULL, TRUE otherwise.
281 BOOL X11DRV_SetupGCForBrush( X11DRV_PDEVICE *physDev )
283 return X11DRV_SetupGCForPatBlt( physDev, physDev->gc, FALSE );
287 /***********************************************************************
288 * X11DRV_SetupGCForPen
290 * Setup physDev->gc for drawing operations using current pen.
291 * Return FALSE if pen is PS_NULL, TRUE otherwise.
293 static BOOL X11DRV_SetupGCForPen( X11DRV_PDEVICE *physDev )
295 XGCValues val;
296 UINT rop2 = GetROP2(physDev->hdc);
298 if (physDev->pen.style == PS_NULL) return FALSE;
300 switch (rop2)
302 case R2_BLACK :
303 val.foreground = BlackPixel( gdi_display, DefaultScreen(gdi_display) );
304 val.function = GXcopy;
305 break;
306 case R2_WHITE :
307 val.foreground = WhitePixel( gdi_display, DefaultScreen(gdi_display) );
308 val.function = GXcopy;
309 break;
310 case R2_XORPEN :
311 val.foreground = physDev->pen.pixel;
312 /* It is very unlikely someone wants to XOR with 0 */
313 /* This fixes the rubber-drawings in paintbrush */
314 if (val.foreground == 0)
315 val.foreground = (WhitePixel( gdi_display, DefaultScreen(gdi_display) ) ^
316 BlackPixel( gdi_display, DefaultScreen(gdi_display) ));
317 val.function = GXxor;
318 break;
319 default :
320 val.foreground = physDev->pen.pixel;
321 val.function = X11DRV_XROPfunction[rop2-1];
323 val.background = physDev->backgroundPixel;
324 val.fill_style = FillSolid;
325 val.line_width = physDev->pen.width;
326 if (val.line_width <= 1) {
327 val.cap_style = CapNotLast;
328 } else {
329 switch (physDev->pen.endcap)
331 case PS_ENDCAP_SQUARE:
332 val.cap_style = CapProjecting;
333 break;
334 case PS_ENDCAP_FLAT:
335 val.cap_style = CapButt;
336 break;
337 case PS_ENDCAP_ROUND:
338 default:
339 val.cap_style = CapRound;
342 switch (physDev->pen.linejoin)
344 case PS_JOIN_BEVEL:
345 val.join_style = JoinBevel;
346 break;
347 case PS_JOIN_MITER:
348 val.join_style = JoinMiter;
349 break;
350 case PS_JOIN_ROUND:
351 default:
352 val.join_style = JoinRound;
355 if (physDev->pen.dash_len)
356 val.line_style = ((GetBkMode(physDev->hdc) == OPAQUE) && (!physDev->pen.ext))
357 ? LineDoubleDash : LineOnOffDash;
358 else
359 val.line_style = LineSolid;
361 wine_tsx11_lock();
362 if (physDev->pen.dash_len)
363 XSetDashes( gdi_display, physDev->gc, 0, physDev->pen.dashes, physDev->pen.dash_len );
364 XChangeGC( gdi_display, physDev->gc,
365 GCFunction | GCForeground | GCBackground | GCLineWidth |
366 GCLineStyle | GCCapStyle | GCJoinStyle | GCFillStyle, &val );
367 wine_tsx11_unlock();
368 return TRUE;
372 /***********************************************************************
373 * X11DRV_SetupGCForText
375 * Setup physDev->gc for text drawing operations.
376 * Return FALSE if the font is null, TRUE otherwise.
378 BOOL X11DRV_SetupGCForText( X11DRV_PDEVICE *physDev )
380 XFontStruct* xfs = XFONT_GetFontStruct( physDev->font );
382 if( xfs )
384 XGCValues val;
386 val.function = GXcopy; /* Text is always GXcopy */
387 val.foreground = physDev->textPixel;
388 val.background = physDev->backgroundPixel;
389 val.fill_style = FillSolid;
390 val.font = xfs->fid;
392 wine_tsx11_lock();
393 XChangeGC( gdi_display, physDev->gc,
394 GCFunction | GCForeground | GCBackground | GCFillStyle |
395 GCFont, &val );
396 wine_tsx11_unlock();
397 return TRUE;
399 WARN("Physical font failure\n" );
400 return FALSE;
403 /***********************************************************************
404 * X11DRV_XWStoDS
406 * Performs a world-to-viewport transformation on the specified width.
408 INT X11DRV_XWStoDS( X11DRV_PDEVICE *physDev, INT width )
410 POINT pt[2];
412 pt[0].x = 0;
413 pt[0].y = 0;
414 pt[1].x = width;
415 pt[1].y = 0;
416 LPtoDP( physDev->hdc, pt, 2 );
417 return pt[1].x - pt[0].x;
420 /***********************************************************************
421 * X11DRV_YWStoDS
423 * Performs a world-to-viewport transformation on the specified height.
425 INT X11DRV_YWStoDS( X11DRV_PDEVICE *physDev, INT height )
427 POINT pt[2];
429 pt[0].x = 0;
430 pt[0].y = 0;
431 pt[1].x = 0;
432 pt[1].y = height;
433 LPtoDP( physDev->hdc, pt, 2 );
434 return pt[1].y - pt[0].y;
437 /***********************************************************************
438 * X11DRV_LineTo
440 BOOL CDECL
441 X11DRV_LineTo( X11DRV_PDEVICE *physDev, INT x, INT y )
443 POINT pt[2];
445 if (X11DRV_SetupGCForPen( physDev )) {
446 /* Update the pixmap from the DIB section */
447 X11DRV_LockDIBSection(physDev, DIB_Status_GdiMod);
449 GetCurrentPositionEx( physDev->hdc, &pt[0] );
450 pt[1].x = x;
451 pt[1].y = y;
452 LPtoDP( physDev->hdc, pt, 2 );
454 wine_tsx11_lock();
455 XDrawLine(gdi_display, physDev->drawable, physDev->gc,
456 physDev->dc_rect.left + pt[0].x, physDev->dc_rect.top + pt[0].y,
457 physDev->dc_rect.left + pt[1].x, physDev->dc_rect.top + pt[1].y );
458 wine_tsx11_unlock();
460 /* Update the DIBSection from the pixmap */
461 X11DRV_UnlockDIBSection(physDev, TRUE);
463 return TRUE;
468 /***********************************************************************
469 * X11DRV_DrawArc
471 * Helper functions for Arc(), Chord() and Pie().
472 * 'lines' is the number of lines to draw: 0 for Arc, 1 for Chord, 2 for Pie.
475 static BOOL
476 X11DRV_DrawArc( X11DRV_PDEVICE *physDev, INT left, INT top, INT right,
477 INT bottom, INT xstart, INT ystart,
478 INT xend, INT yend, INT lines )
480 INT xcenter, ycenter, istart_angle, idiff_angle;
481 INT width, oldwidth;
482 double start_angle, end_angle;
483 XPoint points[4];
484 BOOL update = FALSE;
485 POINT start, end;
486 RECT rc;
488 SetRect(&rc, left, top, right, bottom);
489 start.x = xstart;
490 start.y = ystart;
491 end.x = xend;
492 end.y = yend;
493 LPtoDP(physDev->hdc, (POINT*)&rc, 2);
494 LPtoDP(physDev->hdc, &start, 1);
495 LPtoDP(physDev->hdc, &end, 1);
497 if (rc.right < rc.left) { INT tmp = rc.right; rc.right = rc.left; rc.left = tmp; }
498 if (rc.bottom < rc.top) { INT tmp = rc.bottom; rc.bottom = rc.top; rc.top = tmp; }
499 if ((rc.left == rc.right) || (rc.top == rc.bottom)
500 ||(lines && ((rc.right-rc.left==1)||(rc.bottom-rc.top==1)))) return TRUE;
502 if (GetArcDirection( physDev->hdc ) == AD_CLOCKWISE)
503 { POINT tmp = start; start = end; end = tmp; }
505 oldwidth = width = physDev->pen.width;
506 if (!width) width = 1;
507 if(physDev->pen.style == PS_NULL) width = 0;
509 if ((physDev->pen.style == PS_INSIDEFRAME))
511 if (2*width > (rc.right-rc.left)) width=(rc.right-rc.left + 1)/2;
512 if (2*width > (rc.bottom-rc.top)) width=(rc.bottom-rc.top + 1)/2;
513 rc.left += width / 2;
514 rc.right -= (width - 1) / 2;
515 rc.top += width / 2;
516 rc.bottom -= (width - 1) / 2;
518 if(width == 0) width = 1; /* more accurate */
519 physDev->pen.width = width;
521 xcenter = (rc.right + rc.left) / 2;
522 ycenter = (rc.bottom + rc.top) / 2;
523 start_angle = atan2( (double)(ycenter-start.y)*(rc.right-rc.left),
524 (double)(start.x-xcenter)*(rc.bottom-rc.top) );
525 end_angle = atan2( (double)(ycenter-end.y)*(rc.right-rc.left),
526 (double)(end.x-xcenter)*(rc.bottom-rc.top) );
527 if ((start.x==end.x)&&(start.y==end.y))
528 { /* A lazy program delivers xstart=xend=ystart=yend=0) */
529 start_angle = 0;
530 end_angle = 2* PI;
532 else /* notorious cases */
533 if ((start_angle == PI)&&( end_angle <0))
534 start_angle = - PI;
535 else
536 if ((end_angle == PI)&&( start_angle <0))
537 end_angle = - PI;
538 istart_angle = (INT)(start_angle * 180 * 64 / PI + 0.5);
539 idiff_angle = (INT)((end_angle - start_angle) * 180 * 64 / PI + 0.5);
540 if (idiff_angle <= 0) idiff_angle += 360 * 64;
542 /* Update the pixmap from the DIB section */
543 X11DRV_LockDIBSection(physDev, DIB_Status_GdiMod);
545 /* Fill arc with brush if Chord() or Pie() */
547 if ((lines > 0) && X11DRV_SetupGCForBrush( physDev )) {
548 wine_tsx11_lock();
549 XSetArcMode( gdi_display, physDev->gc, (lines==1) ? ArcChord : ArcPieSlice);
550 XFillArc( gdi_display, physDev->drawable, physDev->gc,
551 physDev->dc_rect.left + rc.left, physDev->dc_rect.top + rc.top,
552 rc.right-rc.left-1, rc.bottom-rc.top-1, istart_angle, idiff_angle );
553 wine_tsx11_unlock();
554 update = TRUE;
557 /* Draw arc and lines */
559 if (X11DRV_SetupGCForPen( physDev ))
561 wine_tsx11_lock();
562 XDrawArc( gdi_display, physDev->drawable, physDev->gc,
563 physDev->dc_rect.left + rc.left, physDev->dc_rect.top + rc.top,
564 rc.right-rc.left-1, rc.bottom-rc.top-1, istart_angle, idiff_angle );
565 if (lines) {
566 /* use the truncated values */
567 start_angle=(double)istart_angle*PI/64./180.;
568 end_angle=(double)(istart_angle+idiff_angle)*PI/64./180.;
569 /* calculate the endpoints and round correctly */
570 points[0].x = (int) floor(physDev->dc_rect.left + (rc.right+rc.left)/2.0 +
571 cos(start_angle) * (rc.right-rc.left-width*2+2) / 2. + 0.5);
572 points[0].y = (int) floor(physDev->dc_rect.top + (rc.top+rc.bottom)/2.0 -
573 sin(start_angle) * (rc.bottom-rc.top-width*2+2) / 2. + 0.5);
574 points[1].x = (int) floor(physDev->dc_rect.left + (rc.right+rc.left)/2.0 +
575 cos(end_angle) * (rc.right-rc.left-width*2+2) / 2. + 0.5);
576 points[1].y = (int) floor(physDev->dc_rect.top + (rc.top+rc.bottom)/2.0 -
577 sin(end_angle) * (rc.bottom-rc.top-width*2+2) / 2. + 0.5);
579 /* OK, this stuff is optimized for Xfree86
580 * which is probably the server most used by
581 * wine users. Other X servers will not
582 * display correctly. (eXceed for instance)
583 * so if you feel you must make changes, make sure that
584 * you either use Xfree86 or separate your changes
585 * from these (compile switch or whatever)
587 if (lines == 2) {
588 INT dx1,dy1;
589 points[3] = points[1];
590 points[1].x = physDev->dc_rect.left + xcenter;
591 points[1].y = physDev->dc_rect.top + ycenter;
592 points[2] = points[1];
593 dx1=points[1].x-points[0].x;
594 dy1=points[1].y-points[0].y;
595 if(((rc.top-rc.bottom) | -2) == -2)
596 if(dy1>0) points[1].y--;
597 if(dx1<0) {
598 if (((-dx1)*64)<=ABS(dy1)*37) points[0].x--;
599 if(((-dx1*9))<(dy1*16)) points[0].y--;
600 if( dy1<0 && ((dx1*9)) < (dy1*16)) points[0].y--;
601 } else {
602 if(dy1 < 0) points[0].y--;
603 if(((rc.right-rc.left) | -2) == -2) points[1].x--;
605 dx1=points[3].x-points[2].x;
606 dy1=points[3].y-points[2].y;
607 if(((rc.top-rc.bottom) | -2 ) == -2)
608 if(dy1 < 0) points[2].y--;
609 if( dx1<0){
610 if( dy1>0) points[3].y--;
611 if(((rc.right-rc.left) | -2) == -2 ) points[2].x--;
612 }else {
613 points[3].y--;
614 if( dx1 * 64 < dy1 * -37 ) points[3].x--;
616 lines++;
618 XDrawLines( gdi_display, physDev->drawable, physDev->gc,
619 points, lines+1, CoordModeOrigin );
621 wine_tsx11_unlock();
622 update = TRUE;
625 /* Update the DIBSection of the pixmap */
626 X11DRV_UnlockDIBSection(physDev, update);
628 physDev->pen.width = oldwidth;
629 return TRUE;
633 /***********************************************************************
634 * X11DRV_Arc
636 BOOL CDECL
637 X11DRV_Arc( X11DRV_PDEVICE *physDev, INT left, INT top, INT right, INT bottom,
638 INT xstart, INT ystart, INT xend, INT yend )
640 return X11DRV_DrawArc( physDev, left, top, right, bottom,
641 xstart, ystart, xend, yend, 0 );
645 /***********************************************************************
646 * X11DRV_Pie
648 BOOL CDECL
649 X11DRV_Pie( X11DRV_PDEVICE *physDev, INT left, INT top, INT right, INT bottom,
650 INT xstart, INT ystart, INT xend, INT yend )
652 return X11DRV_DrawArc( physDev, left, top, right, bottom,
653 xstart, ystart, xend, yend, 2 );
656 /***********************************************************************
657 * X11DRV_Chord
659 BOOL CDECL
660 X11DRV_Chord( X11DRV_PDEVICE *physDev, INT left, INT top, INT right, INT bottom,
661 INT xstart, INT ystart, INT xend, INT yend )
663 return X11DRV_DrawArc( physDev, left, top, right, bottom,
664 xstart, ystart, xend, yend, 1 );
668 /***********************************************************************
669 * X11DRV_Ellipse
671 BOOL CDECL
672 X11DRV_Ellipse( X11DRV_PDEVICE *physDev, INT left, INT top, INT right, INT bottom )
674 INT width, oldwidth;
675 BOOL update = FALSE;
676 RECT rc;
678 SetRect(&rc, left, top, right, bottom);
679 LPtoDP(physDev->hdc, (POINT*)&rc, 2);
681 if ((rc.left == rc.right) || (rc.top == rc.bottom)) return TRUE;
683 if (rc.right < rc.left) { INT tmp = rc.right; rc.right = rc.left; rc.left = tmp; }
684 if (rc.bottom < rc.top) { INT tmp = rc.bottom; rc.bottom = rc.top; rc.top = tmp; }
686 oldwidth = width = physDev->pen.width;
687 if (!width) width = 1;
688 if(physDev->pen.style == PS_NULL) width = 0;
690 if ((physDev->pen.style == PS_INSIDEFRAME))
692 if (2*width > (rc.right-rc.left)) width=(rc.right-rc.left + 1)/2;
693 if (2*width > (rc.bottom-rc.top)) width=(rc.bottom-rc.top + 1)/2;
694 rc.left += width / 2;
695 rc.right -= (width - 1) / 2;
696 rc.top += width / 2;
697 rc.bottom -= (width - 1) / 2;
699 if(width == 0) width = 1; /* more accurate */
700 physDev->pen.width = width;
702 /* Update the pixmap from the DIB section */
703 X11DRV_LockDIBSection(physDev, DIB_Status_GdiMod);
705 if (X11DRV_SetupGCForBrush( physDev ))
707 wine_tsx11_lock();
708 XFillArc( gdi_display, physDev->drawable, physDev->gc,
709 physDev->dc_rect.left + rc.left, physDev->dc_rect.top + rc.top,
710 rc.right-rc.left-1, rc.bottom-rc.top-1, 0, 360*64 );
711 wine_tsx11_unlock();
712 update = TRUE;
714 if (X11DRV_SetupGCForPen( physDev ))
716 wine_tsx11_lock();
717 XDrawArc( gdi_display, physDev->drawable, physDev->gc,
718 physDev->dc_rect.left + rc.left, physDev->dc_rect.top + rc.top,
719 rc.right-rc.left-1, rc.bottom-rc.top-1, 0, 360*64 );
720 wine_tsx11_unlock();
721 update = TRUE;
724 /* Update the DIBSection from the pixmap */
725 X11DRV_UnlockDIBSection(physDev, update);
727 physDev->pen.width = oldwidth;
728 return TRUE;
732 /***********************************************************************
733 * X11DRV_Rectangle
735 BOOL CDECL
736 X11DRV_Rectangle(X11DRV_PDEVICE *physDev, INT left, INT top, INT right, INT bottom)
738 INT width, oldwidth, oldjoinstyle;
739 BOOL update = FALSE;
740 RECT rc;
742 TRACE("(%d %d %d %d)\n", left, top, right, bottom);
744 SetRect(&rc, left, top, right, bottom);
745 LPtoDP(physDev->hdc, (POINT*)&rc, 2);
747 if ((rc.left == rc.right) || (rc.top == rc.bottom)) return TRUE;
749 if (rc.right < rc.left) { INT tmp = rc.right; rc.right = rc.left; rc.left = tmp; }
750 if (rc.bottom < rc.top) { INT tmp = rc.bottom; rc.bottom = rc.top; rc.top = tmp; }
752 oldwidth = width = physDev->pen.width;
753 if (!width) width = 1;
754 if(physDev->pen.style == PS_NULL) width = 0;
756 if ((physDev->pen.style == PS_INSIDEFRAME))
758 if (2*width > (rc.right-rc.left)) width=(rc.right-rc.left + 1)/2;
759 if (2*width > (rc.bottom-rc.top)) width=(rc.bottom-rc.top + 1)/2;
760 rc.left += width / 2;
761 rc.right -= (width - 1) / 2;
762 rc.top += width / 2;
763 rc.bottom -= (width - 1) / 2;
765 if(width == 1) width = 0;
766 physDev->pen.width = width;
767 oldjoinstyle = physDev->pen.linejoin;
768 if(physDev->pen.type != PS_GEOMETRIC)
769 physDev->pen.linejoin = PS_JOIN_MITER;
771 /* Update the pixmap from the DIB section */
772 X11DRV_LockDIBSection(physDev, DIB_Status_GdiMod);
774 if ((rc.right > rc.left + width) && (rc.bottom > rc.top + width))
776 if (X11DRV_SetupGCForBrush( physDev ))
778 wine_tsx11_lock();
779 XFillRectangle( gdi_display, physDev->drawable, physDev->gc,
780 physDev->dc_rect.left + rc.left + (width + 1) / 2,
781 physDev->dc_rect.top + rc.top + (width + 1) / 2,
782 rc.right-rc.left-width-1, rc.bottom-rc.top-width-1);
783 wine_tsx11_unlock();
784 update = TRUE;
787 if (X11DRV_SetupGCForPen( physDev ))
789 wine_tsx11_lock();
790 XDrawRectangle( gdi_display, physDev->drawable, physDev->gc,
791 physDev->dc_rect.left + rc.left, physDev->dc_rect.top + rc.top,
792 rc.right-rc.left-1, rc.bottom-rc.top-1 );
793 wine_tsx11_unlock();
794 update = TRUE;
797 /* Update the DIBSection from the pixmap */
798 X11DRV_UnlockDIBSection(physDev, update);
800 physDev->pen.width = oldwidth;
801 physDev->pen.linejoin = oldjoinstyle;
802 return TRUE;
805 /***********************************************************************
806 * X11DRV_RoundRect
808 BOOL CDECL
809 X11DRV_RoundRect( X11DRV_PDEVICE *physDev, INT left, INT top, INT right,
810 INT bottom, INT ell_width, INT ell_height )
812 INT width, oldwidth, oldendcap;
813 BOOL update = FALSE;
814 RECT rc;
815 POINT pts[2];
817 TRACE("(%d %d %d %d %d %d\n",
818 left, top, right, bottom, ell_width, ell_height);
820 SetRect(&rc, left, top, right, bottom);
821 LPtoDP(physDev->hdc, (POINT*)&rc, 2);
823 if ((rc.left == rc.right) || (rc.top == rc.bottom))
824 return TRUE;
826 /* Make sure ell_width and ell_height are >= 1 otherwise XDrawArc gets
827 called with width/height < 0 */
828 pts[0].x = pts[0].y = 0;
829 pts[1].x = ell_width;
830 pts[1].y = ell_height;
831 LPtoDP(physDev->hdc, pts, 2);
832 ell_width = max(abs( pts[1].x - pts[0].x ), 1);
833 ell_height = max(abs( pts[1].y - pts[0].y ), 1);
835 /* Fix the coordinates */
837 if (rc.right < rc.left) { INT tmp = rc.right; rc.right = rc.left; rc.left = tmp; }
838 if (rc.bottom < rc.top) { INT tmp = rc.bottom; rc.bottom = rc.top; rc.top = tmp; }
840 oldwidth = width = physDev->pen.width;
841 oldendcap = physDev->pen.endcap;
842 if (!width) width = 1;
843 if(physDev->pen.style == PS_NULL) width = 0;
845 if ((physDev->pen.style == PS_INSIDEFRAME))
847 if (2*width > (rc.right-rc.left)) width=(rc.right-rc.left + 1)/2;
848 if (2*width > (rc.bottom-rc.top)) width=(rc.bottom-rc.top + 1)/2;
849 rc.left += width / 2;
850 rc.right -= (width - 1) / 2;
851 rc.top += width / 2;
852 rc.bottom -= (width - 1) / 2;
854 if(width == 0) width = 1;
855 physDev->pen.width = width;
856 physDev->pen.endcap = PS_ENDCAP_SQUARE;
858 /* Update the pixmap from the DIB section */
859 X11DRV_LockDIBSection(physDev, DIB_Status_GdiMod);
861 if (X11DRV_SetupGCForBrush( physDev ))
863 wine_tsx11_lock();
864 if (ell_width > (rc.right-rc.left) )
865 if (ell_height > (rc.bottom-rc.top) )
866 XFillArc( gdi_display, physDev->drawable, physDev->gc,
867 physDev->dc_rect.left + rc.left, physDev->dc_rect.top + rc.top,
868 rc.right - rc.left - 1, rc.bottom - rc.top - 1,
869 0, 360 * 64 );
870 else{
871 XFillArc( gdi_display, physDev->drawable, physDev->gc,
872 physDev->dc_rect.left + rc.left, physDev->dc_rect.top + rc.top,
873 rc.right - rc.left - 1, ell_height, 0, 180 * 64 );
874 XFillArc( gdi_display, physDev->drawable, physDev->gc,
875 physDev->dc_rect.left + rc.left,
876 physDev->dc_rect.top + rc.bottom - ell_height - 1,
877 rc.right - rc.left - 1, ell_height, 180 * 64,
878 180 * 64 );
880 else if (ell_height > (rc.bottom-rc.top) ){
881 XFillArc( gdi_display, physDev->drawable, physDev->gc,
882 physDev->dc_rect.left + rc.left, physDev->dc_rect.top + rc.top,
883 ell_width, rc.bottom - rc.top - 1, 90 * 64, 180 * 64 );
884 XFillArc( gdi_display, physDev->drawable, physDev->gc,
885 physDev->dc_rect.left + rc.right - ell_width - 1, physDev->dc_rect.top + rc.top,
886 ell_width, rc.bottom - rc.top - 1, 270 * 64, 180 * 64 );
887 }else{
888 XFillArc( gdi_display, physDev->drawable, physDev->gc,
889 physDev->dc_rect.left + rc.left, physDev->dc_rect.top + rc.top,
890 ell_width, ell_height, 90 * 64, 90 * 64 );
891 XFillArc( gdi_display, physDev->drawable, physDev->gc,
892 physDev->dc_rect.left + rc.left,
893 physDev->dc_rect.top + rc.bottom - ell_height - 1,
894 ell_width, ell_height, 180 * 64, 90 * 64 );
895 XFillArc( gdi_display, physDev->drawable, physDev->gc,
896 physDev->dc_rect.left + rc.right - ell_width - 1,
897 physDev->dc_rect.top + rc.bottom - ell_height - 1,
898 ell_width, ell_height, 270 * 64, 90 * 64 );
899 XFillArc( gdi_display, physDev->drawable, physDev->gc,
900 physDev->dc_rect.left + rc.right - ell_width - 1,
901 physDev->dc_rect.top + rc.top,
902 ell_width, ell_height, 0, 90 * 64 );
904 if (ell_width < rc.right - rc.left)
906 XFillRectangle( gdi_display, physDev->drawable, physDev->gc,
907 physDev->dc_rect.left + rc.left + (ell_width + 1) / 2,
908 physDev->dc_rect.top + rc.top + 1,
909 rc.right - rc.left - ell_width - 1,
910 (ell_height + 1) / 2 - 1);
911 XFillRectangle( gdi_display, physDev->drawable, physDev->gc,
912 physDev->dc_rect.left + rc.left + (ell_width + 1) / 2,
913 physDev->dc_rect.top + rc.bottom - (ell_height) / 2 - 1,
914 rc.right - rc.left - ell_width - 1,
915 (ell_height) / 2 );
917 if (ell_height < rc.bottom - rc.top)
919 XFillRectangle( gdi_display, physDev->drawable, physDev->gc,
920 physDev->dc_rect.left + rc.left + 1,
921 physDev->dc_rect.top + rc.top + (ell_height + 1) / 2,
922 rc.right - rc.left - 2,
923 rc.bottom - rc.top - ell_height - 1);
925 wine_tsx11_unlock();
926 update = TRUE;
928 /* FIXME: this could be done with on X call
929 * more efficient and probably more correct
930 * on any X server: XDrawArcs will draw
931 * straight horizontal and vertical lines
932 * if width or height are zero.
934 * BTW this stuff is optimized for an Xfree86 server
935 * read the comments inside the X11DRV_DrawArc function
937 if (X11DRV_SetupGCForPen( physDev ))
939 wine_tsx11_lock();
940 if (ell_width > (rc.right-rc.left) )
941 if (ell_height > (rc.bottom-rc.top) )
942 XDrawArc( gdi_display, physDev->drawable, physDev->gc,
943 physDev->dc_rect.left + rc.left, physDev->dc_rect.top + rc.top,
944 rc.right - rc.left - 1, rc.bottom - rc.top - 1, 0 , 360 * 64 );
945 else{
946 XDrawArc( gdi_display, physDev->drawable, physDev->gc,
947 physDev->dc_rect.left + rc.left, physDev->dc_rect.top + rc.top,
948 rc.right - rc.left - 1, ell_height - 1, 0 , 180 * 64 );
949 XDrawArc( gdi_display, physDev->drawable, physDev->gc,
950 physDev->dc_rect.left + rc.left,
951 physDev->dc_rect.top + rc.bottom - ell_height,
952 rc.right - rc.left - 1, ell_height - 1, 180 * 64 , 180 * 64 );
954 else if (ell_height > (rc.bottom-rc.top) ){
955 XDrawArc( gdi_display, physDev->drawable, physDev->gc,
956 physDev->dc_rect.left + rc.left, physDev->dc_rect.top + rc.top,
957 ell_width - 1 , rc.bottom - rc.top - 1, 90 * 64 , 180 * 64 );
958 XDrawArc( gdi_display, physDev->drawable, physDev->gc,
959 physDev->dc_rect.left + rc.right - ell_width,
960 physDev->dc_rect.top + rc.top,
961 ell_width - 1 , rc.bottom - rc.top - 1, 270 * 64 , 180 * 64 );
962 }else{
963 XDrawArc( gdi_display, physDev->drawable, physDev->gc,
964 physDev->dc_rect.left + rc.left, physDev->dc_rect.top + rc.top,
965 ell_width - 1, ell_height - 1, 90 * 64, 90 * 64 );
966 XDrawArc( gdi_display, physDev->drawable, physDev->gc,
967 physDev->dc_rect.left + rc.left, physDev->dc_rect.top + rc.bottom - ell_height,
968 ell_width - 1, ell_height - 1, 180 * 64, 90 * 64 );
969 XDrawArc( gdi_display, physDev->drawable, physDev->gc,
970 physDev->dc_rect.left + rc.right - ell_width,
971 physDev->dc_rect.top + rc.bottom - ell_height,
972 ell_width - 1, ell_height - 1, 270 * 64, 90 * 64 );
973 XDrawArc( gdi_display, physDev->drawable, physDev->gc,
974 physDev->dc_rect.left + rc.right - ell_width, physDev->dc_rect.top + rc.top,
975 ell_width - 1, ell_height - 1, 0, 90 * 64 );
977 if (ell_width < rc.right - rc.left)
979 XDrawLine( gdi_display, physDev->drawable, physDev->gc,
980 physDev->dc_rect.left + rc.left + ell_width / 2,
981 physDev->dc_rect.top + rc.top,
982 physDev->dc_rect.left + rc.right - (ell_width+1) / 2,
983 physDev->dc_rect.top + rc.top);
984 XDrawLine( gdi_display, physDev->drawable, physDev->gc,
985 physDev->dc_rect.left + rc.left + ell_width / 2 ,
986 physDev->dc_rect.top + rc.bottom - 1,
987 physDev->dc_rect.left + rc.right - (ell_width+1)/ 2,
988 physDev->dc_rect.top + rc.bottom - 1);
990 if (ell_height < rc.bottom - rc.top)
992 XDrawLine( gdi_display, physDev->drawable, physDev->gc,
993 physDev->dc_rect.left + rc.right - 1,
994 physDev->dc_rect.top + rc.top + ell_height / 2,
995 physDev->dc_rect.left + rc.right - 1,
996 physDev->dc_rect.top + rc.bottom - (ell_height+1) / 2);
997 XDrawLine( gdi_display, physDev->drawable, physDev->gc,
998 physDev->dc_rect.left + rc.left,
999 physDev->dc_rect.top + rc.top + ell_height / 2,
1000 physDev->dc_rect.left + rc.left,
1001 physDev->dc_rect.top + rc.bottom - (ell_height+1) / 2);
1003 wine_tsx11_unlock();
1004 update = TRUE;
1006 /* Update the DIBSection from the pixmap */
1007 X11DRV_UnlockDIBSection(physDev, update);
1009 physDev->pen.width = oldwidth;
1010 physDev->pen.endcap = oldendcap;
1011 return TRUE;
1015 /***********************************************************************
1016 * X11DRV_SetPixel
1018 COLORREF CDECL
1019 X11DRV_SetPixel( X11DRV_PDEVICE *physDev, INT x, INT y, COLORREF color )
1021 unsigned long pixel;
1022 POINT pt;
1024 pt.x = x;
1025 pt.y = y;
1026 LPtoDP( physDev->hdc, &pt, 1 );
1027 pixel = X11DRV_PALETTE_ToPhysical( physDev, color );
1029 /* Update the pixmap from the DIB section */
1030 X11DRV_LockDIBSection(physDev, DIB_Status_GdiMod);
1032 /* inefficient but simple... */
1033 wine_tsx11_lock();
1034 XSetForeground( gdi_display, physDev->gc, pixel );
1035 XSetFunction( gdi_display, physDev->gc, GXcopy );
1036 XDrawPoint( gdi_display, physDev->drawable, physDev->gc,
1037 physDev->dc_rect.left + pt.x, physDev->dc_rect.top + pt.y );
1038 wine_tsx11_unlock();
1040 /* Update the DIBSection from the pixmap */
1041 X11DRV_UnlockDIBSection(physDev, TRUE);
1043 return X11DRV_PALETTE_ToLogical(physDev, pixel);
1047 /***********************************************************************
1048 * X11DRV_GetPixel
1050 COLORREF CDECL
1051 X11DRV_GetPixel( X11DRV_PDEVICE *physDev, INT x, INT y )
1053 static Pixmap pixmap = 0;
1054 XImage * image;
1055 int pixel;
1056 POINT pt;
1057 BOOL memdc = (GetObjectType(physDev->hdc) == OBJ_MEMDC);
1059 pt.x = x;
1060 pt.y = y;
1061 LPtoDP( physDev->hdc, &pt, 1 );
1063 /* Update the pixmap from the DIB section */
1064 X11DRV_LockDIBSection(physDev, DIB_Status_GdiMod);
1066 wine_tsx11_lock();
1067 if (memdc)
1069 image = XGetImage( gdi_display, physDev->drawable,
1070 physDev->dc_rect.left + pt.x, physDev->dc_rect.top + pt.y,
1071 1, 1, AllPlanes, ZPixmap );
1073 else
1075 /* If we are reading from the screen, use a temporary copy */
1076 /* to avoid a BadMatch error */
1077 if (!pixmap) pixmap = XCreatePixmap( gdi_display, root_window,
1078 1, 1, physDev->depth );
1079 XCopyArea( gdi_display, physDev->drawable, pixmap, get_bitmap_gc(physDev->depth),
1080 physDev->dc_rect.left + pt.x, physDev->dc_rect.top + pt.y, 1, 1, 0, 0 );
1081 image = XGetImage( gdi_display, pixmap, 0, 0, 1, 1, AllPlanes, ZPixmap );
1083 pixel = XGetPixel( image, 0, 0 );
1084 XDestroyImage( image );
1085 wine_tsx11_unlock();
1087 /* Update the DIBSection from the pixmap */
1088 X11DRV_UnlockDIBSection(physDev, FALSE);
1089 if( physDev->depth > 1)
1090 pixel = X11DRV_PALETTE_ToLogical(physDev, pixel);
1091 else
1092 /* monochrome bitmaps return black or white */
1093 if( pixel) pixel = 0xffffff;
1094 return pixel;
1099 /***********************************************************************
1100 * X11DRV_PaintRgn
1102 BOOL CDECL
1103 X11DRV_PaintRgn( X11DRV_PDEVICE *physDev, HRGN hrgn )
1105 if (X11DRV_SetupGCForBrush( physDev ))
1107 unsigned int i;
1108 XRectangle *rect;
1109 RGNDATA *data = X11DRV_GetRegionData( hrgn, physDev->hdc );
1111 if (!data) return FALSE;
1112 rect = (XRectangle *)data->Buffer;
1113 for (i = 0; i < data->rdh.nCount; i++)
1115 rect[i].x += physDev->dc_rect.left;
1116 rect[i].y += physDev->dc_rect.top;
1119 X11DRV_LockDIBSection(physDev, DIB_Status_GdiMod);
1120 wine_tsx11_lock();
1121 XFillRectangles( gdi_display, physDev->drawable, physDev->gc, rect, data->rdh.nCount );
1122 wine_tsx11_unlock();
1123 X11DRV_UnlockDIBSection(physDev, TRUE);
1124 HeapFree( GetProcessHeap(), 0, data );
1126 return TRUE;
1129 /**********************************************************************
1130 * X11DRV_Polyline
1132 BOOL CDECL
1133 X11DRV_Polyline( X11DRV_PDEVICE *physDev, const POINT* pt, INT count )
1135 int i;
1136 XPoint *points;
1138 if (!(points = HeapAlloc( GetProcessHeap(), 0, sizeof(XPoint) * count )))
1140 WARN("No memory to convert POINTs to XPoints!\n");
1141 return FALSE;
1143 for (i = 0; i < count; i++)
1145 POINT tmp = pt[i];
1146 LPtoDP(physDev->hdc, &tmp, 1);
1147 points[i].x = physDev->dc_rect.left + tmp.x;
1148 points[i].y = physDev->dc_rect.top + tmp.y;
1151 if (X11DRV_SetupGCForPen ( physDev ))
1153 X11DRV_LockDIBSection(physDev, DIB_Status_GdiMod);
1154 wine_tsx11_lock();
1155 XDrawLines( gdi_display, physDev->drawable, physDev->gc,
1156 points, count, CoordModeOrigin );
1157 wine_tsx11_unlock();
1158 X11DRV_UnlockDIBSection(physDev, TRUE);
1161 HeapFree( GetProcessHeap(), 0, points );
1162 return TRUE;
1166 /**********************************************************************
1167 * X11DRV_Polygon
1169 BOOL CDECL
1170 X11DRV_Polygon( X11DRV_PDEVICE *physDev, const POINT* pt, INT count )
1172 register int i;
1173 XPoint *points;
1174 BOOL update = FALSE;
1176 if (!(points = HeapAlloc( GetProcessHeap(), 0, sizeof(XPoint) * (count+1) )))
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(physDev->hdc, &tmp, 1);
1185 points[i].x = physDev->dc_rect.left + tmp.x;
1186 points[i].y = physDev->dc_rect.top + tmp.y;
1188 points[count] = points[0];
1190 /* Update the pixmap from the DIB section */
1191 X11DRV_LockDIBSection(physDev, DIB_Status_GdiMod);
1193 if (X11DRV_SetupGCForBrush( physDev ))
1195 wine_tsx11_lock();
1196 XFillPolygon( gdi_display, physDev->drawable, physDev->gc,
1197 points, count+1, Complex, CoordModeOrigin);
1198 wine_tsx11_unlock();
1199 update = TRUE;
1201 if (X11DRV_SetupGCForPen ( physDev ))
1203 wine_tsx11_lock();
1204 XDrawLines( gdi_display, physDev->drawable, physDev->gc,
1205 points, count+1, CoordModeOrigin );
1206 wine_tsx11_unlock();
1207 update = TRUE;
1210 /* Update the DIBSection from the pixmap */
1211 X11DRV_UnlockDIBSection(physDev, update);
1213 HeapFree( GetProcessHeap(), 0, points );
1214 return TRUE;
1218 /**********************************************************************
1219 * X11DRV_PolyPolygon
1221 BOOL CDECL
1222 X11DRV_PolyPolygon( X11DRV_PDEVICE *physDev, const POINT* pt, const INT* counts, UINT polygons)
1224 HRGN hrgn;
1226 /* FIXME: The points should be converted to device coords before */
1227 /* creating the region. */
1229 hrgn = CreatePolyPolygonRgn( pt, counts, polygons, GetPolyFillMode( physDev->hdc ) );
1230 X11DRV_PaintRgn( physDev, hrgn );
1231 DeleteObject( hrgn );
1233 /* Draw the outline of the polygons */
1235 if (X11DRV_SetupGCForPen ( physDev ))
1237 unsigned int i;
1238 int j, max = 0;
1239 XPoint *points;
1241 /* Update the pixmap from the DIB section */
1242 X11DRV_LockDIBSection(physDev, DIB_Status_GdiMod);
1244 for (i = 0; i < polygons; i++) if (counts[i] > max) max = counts[i];
1245 if (!(points = HeapAlloc( GetProcessHeap(), 0, sizeof(XPoint) * (max+1) )))
1247 WARN("No memory to convert POINTs to XPoints!\n");
1248 return FALSE;
1250 for (i = 0; i < polygons; i++)
1252 for (j = 0; j < counts[i]; j++)
1254 POINT tmp = *pt;
1255 LPtoDP(physDev->hdc, &tmp, 1);
1256 points[j].x = physDev->dc_rect.left + tmp.x;
1257 points[j].y = physDev->dc_rect.top + tmp.y;
1258 pt++;
1260 points[j] = points[0];
1261 wine_tsx11_lock();
1262 XDrawLines( gdi_display, physDev->drawable, physDev->gc,
1263 points, j + 1, CoordModeOrigin );
1264 wine_tsx11_unlock();
1267 /* Update the DIBSection of the dc's bitmap */
1268 X11DRV_UnlockDIBSection(physDev, TRUE);
1270 HeapFree( GetProcessHeap(), 0, points );
1272 return TRUE;
1276 /**********************************************************************
1277 * X11DRV_PolyPolyline
1279 BOOL CDECL
1280 X11DRV_PolyPolyline( X11DRV_PDEVICE *physDev, const POINT* pt, const DWORD* counts, DWORD polylines )
1282 if (X11DRV_SetupGCForPen ( physDev ))
1284 unsigned int i, j, max = 0;
1285 XPoint *points;
1287 /* Update the pixmap from the DIB section */
1288 X11DRV_LockDIBSection(physDev, DIB_Status_GdiMod);
1290 for (i = 0; i < polylines; i++) if (counts[i] > max) max = counts[i];
1291 if (!(points = HeapAlloc( GetProcessHeap(), 0, sizeof(XPoint) * max )))
1293 WARN("No memory to convert POINTs to XPoints!\n");
1294 return FALSE;
1296 for (i = 0; i < polylines; i++)
1298 for (j = 0; j < counts[i]; j++)
1300 POINT tmp = *pt;
1301 LPtoDP(physDev->hdc, &tmp, 1);
1302 points[j].x = physDev->dc_rect.left + tmp.x;
1303 points[j].y = physDev->dc_rect.top + tmp.y;
1304 pt++;
1306 wine_tsx11_lock();
1307 XDrawLines( gdi_display, physDev->drawable, physDev->gc,
1308 points, j, CoordModeOrigin );
1309 wine_tsx11_unlock();
1312 /* Update the DIBSection of the dc's bitmap */
1313 X11DRV_UnlockDIBSection(physDev, TRUE);
1315 HeapFree( GetProcessHeap(), 0, points );
1317 return TRUE;
1321 /**********************************************************************
1322 * X11DRV_InternalFloodFill
1324 * Internal helper function for flood fill.
1325 * (xorg,yorg) is the origin of the X image relative to the drawable.
1326 * (x,y) is relative to the origin of the X image.
1328 static void X11DRV_InternalFloodFill(XImage *image, X11DRV_PDEVICE *physDev,
1329 int x, int y,
1330 int xOrg, int yOrg,
1331 unsigned long pixel, WORD fillType )
1333 int left, right;
1335 #define TO_FLOOD(x,y) ((fillType == FLOODFILLBORDER) ? \
1336 (XGetPixel(image,x,y) != pixel) : \
1337 (XGetPixel(image,x,y) == pixel))
1339 if (!TO_FLOOD(x,y)) return;
1341 /* Find left and right boundaries */
1343 left = right = x;
1344 while ((left > 0) && TO_FLOOD( left-1, y )) left--;
1345 while ((right < image->width) && TO_FLOOD( right, y )) right++;
1346 XFillRectangle( gdi_display, physDev->drawable, physDev->gc,
1347 xOrg + left, yOrg + y, right-left, 1 );
1349 /* Set the pixels of this line so we don't fill it again */
1351 for (x = left; x < right; x++)
1353 if (fillType == FLOODFILLBORDER) XPutPixel( image, x, y, pixel );
1354 else XPutPixel( image, x, y, ~pixel );
1357 /* Fill the line above */
1359 if (--y >= 0)
1361 x = left;
1362 while (x < right)
1364 while ((x < right) && !TO_FLOOD(x,y)) x++;
1365 if (x >= right) break;
1366 while ((x < right) && TO_FLOOD(x,y)) x++;
1367 X11DRV_InternalFloodFill(image, physDev, x-1, y,
1368 xOrg, yOrg, pixel, fillType );
1372 /* Fill the line below */
1374 if ((y += 2) < image->height)
1376 x = left;
1377 while (x < right)
1379 while ((x < right) && !TO_FLOOD(x,y)) x++;
1380 if (x >= right) break;
1381 while ((x < right) && TO_FLOOD(x,y)) x++;
1382 X11DRV_InternalFloodFill(image, physDev, x-1, y,
1383 xOrg, yOrg, pixel, fillType );
1386 #undef TO_FLOOD
1390 static int ExtFloodFillXGetImageErrorHandler( Display *dpy, XErrorEvent *event, void *arg )
1392 return (event->request_code == X_GetImage && event->error_code == BadMatch);
1395 /**********************************************************************
1396 * X11DRV_ExtFloodFill
1398 BOOL CDECL
1399 X11DRV_ExtFloodFill( X11DRV_PDEVICE *physDev, INT x, INT y, COLORREF color,
1400 UINT fillType )
1402 XImage *image;
1403 RECT rect;
1404 POINT pt;
1406 TRACE("X11DRV_ExtFloodFill %d,%d %06x %d\n", x, y, color, fillType );
1408 pt.x = x;
1409 pt.y = y;
1410 LPtoDP( physDev->hdc, &pt, 1 );
1411 if (!PtInRegion( physDev->region, pt.x, pt.y )) return FALSE;
1412 GetRgnBox( physDev->region, &rect );
1414 X11DRV_expect_error( gdi_display, ExtFloodFillXGetImageErrorHandler, NULL );
1415 image = XGetImage( gdi_display, physDev->drawable,
1416 physDev->dc_rect.left + rect.left, physDev->dc_rect.top + rect.top,
1417 rect.right - rect.left, rect.bottom - rect.top,
1418 AllPlanes, ZPixmap );
1419 if(X11DRV_check_error()) image = NULL;
1420 if (!image) return FALSE;
1422 if (X11DRV_SetupGCForBrush( physDev ))
1424 unsigned long pixel = X11DRV_PALETTE_ToPhysical( physDev, color );
1426 /* Update the pixmap from the DIB section */
1427 X11DRV_LockDIBSection(physDev, DIB_Status_GdiMod);
1429 /* ROP mode is always GXcopy for flood-fill */
1430 wine_tsx11_lock();
1431 XSetFunction( gdi_display, physDev->gc, GXcopy );
1432 X11DRV_InternalFloodFill(image, physDev,
1433 pt.x - rect.left,
1434 pt.y - rect.top,
1435 physDev->dc_rect.left + rect.left,
1436 physDev->dc_rect.top + rect.top,
1437 pixel, fillType );
1438 wine_tsx11_unlock();
1439 /* Update the DIBSection of the dc's bitmap */
1440 X11DRV_UnlockDIBSection(physDev, TRUE);
1443 wine_tsx11_lock();
1444 XDestroyImage( image );
1445 wine_tsx11_unlock();
1446 return TRUE;
1449 /**********************************************************************
1450 * X11DRV_SetBkColor
1452 COLORREF CDECL
1453 X11DRV_SetBkColor( X11DRV_PDEVICE *physDev, COLORREF color )
1455 physDev->backgroundPixel = X11DRV_PALETTE_ToPhysical( physDev, color );
1456 return color;
1459 /**********************************************************************
1460 * X11DRV_SetTextColor
1462 COLORREF CDECL
1463 X11DRV_SetTextColor( X11DRV_PDEVICE *physDev, COLORREF color )
1465 physDev->textPixel = X11DRV_PALETTE_ToPhysical( physDev, color );
1466 return color;
1469 /***********************************************************************
1470 * GetDCOrgEx (X11DRV.@)
1472 BOOL CDECL X11DRV_GetDCOrgEx( X11DRV_PDEVICE *physDev, LPPOINT lpp )
1474 lpp->x = physDev->dc_rect.left + physDev->drawable_rect.left;
1475 lpp->y = physDev->dc_rect.top + physDev->drawable_rect.top;
1476 return TRUE;
1480 static unsigned char *get_icm_profile( unsigned long *size )
1482 Atom type;
1483 int format;
1484 unsigned long count, remaining;
1485 unsigned char *profile, *ret = NULL;
1487 wine_tsx11_lock();
1488 XGetWindowProperty( gdi_display, DefaultRootWindow(gdi_display),
1489 x11drv_atom(_ICC_PROFILE), 0, ~0UL, False, AnyPropertyType,
1490 &type, &format, &count, &remaining, &profile );
1491 *size = get_property_size( format, count );
1492 if (format && count)
1494 if ((ret = HeapAlloc( GetProcessHeap(), 0, *size ))) memcpy( ret, profile, *size );
1495 XFree( profile );
1497 wine_tsx11_unlock();
1498 return ret;
1501 typedef struct
1503 unsigned int unknown[6];
1504 unsigned int state[5];
1505 unsigned int count[2];
1506 unsigned char buffer[64];
1507 } sha_ctx;
1509 extern void WINAPI A_SHAInit( sha_ctx * );
1510 extern void WINAPI A_SHAUpdate( sha_ctx *, const unsigned char *, unsigned int );
1511 extern void WINAPI A_SHAFinal( sha_ctx *, unsigned char * );
1513 /***********************************************************************
1514 * GetICMProfile (X11DRV.@)
1516 BOOL CDECL X11DRV_GetICMProfile( X11DRV_PDEVICE *physDev, LPDWORD size, LPWSTR filename )
1518 static const WCHAR path[] =
1519 {'\\','s','p','o','o','l','\\','d','r','i','v','e','r','s',
1520 '\\','c','o','l','o','r','\\',0};
1521 static const WCHAR srgb[] =
1522 {'s','R','G','B',' ','C','o','l','o','r',' ','S','p','a','c','e',' ',
1523 'P','r','o','f','i','l','e','.','i','c','m',0};
1524 static const WCHAR mntr[] =
1525 {'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
1526 'W','i','n','d','o','w','s',' ','N','T','\\','C','u','r','r','e','n','t',
1527 'V','e','r','s','i','o','n','\\','I','C','M','\\','m','n','t','r',0};
1529 HKEY hkey;
1530 DWORD required, len;
1531 WCHAR profile[MAX_PATH], fullname[2*MAX_PATH + sizeof(path)/sizeof(WCHAR)];
1532 unsigned char *buffer;
1533 unsigned long buflen;
1535 if (!size) return FALSE;
1537 GetSystemDirectoryW( fullname, MAX_PATH );
1538 strcatW( fullname, path );
1540 len = sizeof(profile)/sizeof(WCHAR);
1541 if (!RegCreateKeyExW( HKEY_LOCAL_MACHINE, mntr, 0, NULL, 0, KEY_ALL_ACCESS, NULL, &hkey, NULL ) &&
1542 !RegEnumValueW( hkey, 0, profile, &len, NULL, NULL, NULL, NULL )) /* FIXME handle multiple values */
1544 strcatW( fullname, profile );
1545 RegCloseKey( hkey );
1547 else if ((buffer = get_icm_profile( &buflen )))
1549 static const WCHAR fmt[] = {'%','0','2','x',0};
1550 static const WCHAR icm[] = {'.','i','c','m',0};
1552 unsigned char sha1sum[20];
1553 unsigned int i;
1554 sha_ctx ctx;
1555 HANDLE file;
1557 A_SHAInit( &ctx );
1558 A_SHAUpdate( &ctx, buffer, buflen );
1559 A_SHAFinal( &ctx, sha1sum );
1561 for (i = 0; i < sizeof(sha1sum); i++) sprintfW( &profile[i * 2], fmt, sha1sum[i] );
1562 memcpy( &profile[i * 2], icm, sizeof(icm) );
1564 strcatW( fullname, profile );
1565 file = CreateFileW( fullname, GENERIC_WRITE, 0, NULL, CREATE_NEW, 0, 0 );
1566 if (file != INVALID_HANDLE_VALUE)
1568 DWORD written;
1570 if (!WriteFile( file, buffer, buflen, &written, NULL ) || written != buflen)
1571 ERR( "Unable to write color profile\n" );
1572 CloseHandle( file );
1574 HeapFree( GetProcessHeap(), 0, buffer );
1576 else strcatW( fullname, srgb );
1578 required = strlenW( fullname ) + 1;
1579 if (*size < required)
1581 *size = required;
1582 SetLastError( ERROR_INSUFFICIENT_BUFFER );
1583 return FALSE;
1585 if (filename)
1587 strcpyW( filename, fullname );
1588 if (GetFileAttributesW( filename ) == INVALID_FILE_ATTRIBUTES)
1589 WARN( "color profile not found\n" );
1591 *size = required;
1592 return TRUE;