winex11: Add a function for retrieving the bitmap GC.
[wine] / dlls / winex11.drv / graphics.c
1 /*
2  * X11 graphics driver graphics functions
3  *
4  * Copyright 1993,1994 Alexandre Julliard
5  * Copyright 1998 Huw Davies
6  *
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.
11  *
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.
16  *
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
20  */
21
22 /*
23  * FIXME: only some of these functions obey the GM_ADVANCED
24  * graphics mode
25  */
26
27 #include "config.h"
28
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
40 #include "windef.h"
41 #include "winbase.h"
42 #include "winreg.h"
43
44 #include "x11drv.h"
45 #include "x11font.h"
46 #include "wine/debug.h"
47 #include "wine/unicode.h"
48
49 WINE_DEFAULT_DEBUG_CHANNEL(graphics);
50
51 #define ABS(x)    ((x)<0?(-(x)):(x))
52
53   /* ROP code to GC function conversion */
54 const int X11DRV_XROPfunction[16] =
55 {
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 */
72 };
73
74
75 /***********************************************************************
76  *           X11DRV_GetRegionData
77  *
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.
82  */
83 RGNDATA *X11DRV_GetRegionData( HRGN hrgn, HDC hdc_lptodp )
84 {
85     RGNDATA *data;
86     DWORD size;
87     unsigned int i;
88     RECT *rect, tmp;
89     XRectangle *xrect;
90
91     if (!(size = GetRegionData( hrgn, 0, NULL ))) return NULL;
92     if (sizeof(XRectangle) > sizeof(RECT))
93     {
94         /* add extra size for XRectangle array */
95         int count = (size - sizeof(RGNDATAHEADER)) / sizeof(RECT);
96         size += count * (sizeof(XRectangle) - sizeof(RECT));
97     }
98     if (!(data = HeapAlloc( GetProcessHeap(), 0, size ))) return NULL;
99     if (!GetRegionData( hrgn, size, data ))
100     {
101         HeapFree( GetProcessHeap(), 0, data );
102         return NULL;
103     }
104
105     rect = (RECT *)data->Buffer;
106     xrect = (XRectangle *)data->Buffer;
107     if (hdc_lptodp)  /* map to device coordinates */
108     {
109         LPtoDP( hdc_lptodp, (POINT *)rect, data->rdh.nCount * 2 );
110         for (i = 0; i < data->rdh.nCount; i++)
111         {
112             if (rect[i].right < rect[i].left)
113             {
114                 INT tmp = rect[i].right;
115                 rect[i].right = rect[i].left;
116                 rect[i].left = tmp;
117             }
118             if (rect[i].bottom < rect[i].top)
119             {
120                 INT tmp = rect[i].bottom;
121                 rect[i].bottom = rect[i].top;
122                 rect[i].top = tmp;
123             }
124         }
125     }
126
127     if (sizeof(XRectangle) > sizeof(RECT))
128     {
129         int j;
130         /* need to start from the end */
131         for (j = data->rdh.nCount-1; j >= 0; j--)
132         {
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;
138         }
139     }
140     else
141     {
142         for (i = 0; i < data->rdh.nCount; i++)
143         {
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;
149         }
150     }
151     return data;
152 }
153
154
155 /***********************************************************************
156  *           X11DRV_SetDeviceClipping
157  */
158 void CDECL X11DRV_SetDeviceClipping( X11DRV_PDEVICE *physDev, HRGN vis_rgn, HRGN clip_rgn )
159 {
160     RGNDATA *data;
161
162     CombineRgn( physDev->region, vis_rgn, clip_rgn, clip_rgn ? RGN_AND : RGN_COPY );
163     if (!(data = X11DRV_GetRegionData( physDev->region, 0 ))) return;
164
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();
169     HeapFree( GetProcessHeap(), 0, data );
170 }
171
172
173 /***********************************************************************
174  *           X11DRV_SetupGCForPatBlt
175  *
176  * Setup the GC for a PatBlt operation using current brush.
177  * If fMapColors is TRUE, X pixels are mapped to Windows colors.
178  * Return FALSE if brush is BS_NULL, TRUE otherwise.
179  */
180 BOOL X11DRV_SetupGCForPatBlt( X11DRV_PDEVICE *physDev, GC gc, BOOL fMapColors )
181 {
182     XGCValues val;
183     unsigned long mask;
184     Pixmap pixmap = 0;
185     POINT pt;
186
187     if (physDev->brush.style == BS_NULL) return FALSE;
188     if (physDev->brush.pixel == -1)
189     {
190         /* Special case used for monochrome pattern brushes.
191          * We need to swap foreground and background because
192          * Windows does it the wrong way...
193          */
194         val.foreground = physDev->backgroundPixel;
195         val.background = physDev->textPixel;
196     }
197     else
198     {
199         val.foreground = physDev->brush.pixel;
200         val.background = physDev->backgroundPixel;
201     }
202     if (fMapColors && X11DRV_PALETTE_XPixelToPalette)
203     {
204         val.foreground = X11DRV_PALETTE_XPixelToPalette[val.foreground];
205         val.background = X11DRV_PALETTE_XPixelToPalette[val.background];
206     }
207
208     val.function = X11DRV_XROPfunction[GetROP2(physDev->hdc)-1];
209     /*
210     ** Let's replace GXinvert by GXxor with (black xor white)
211     ** This solves the selection color and leak problems in excel
212     ** FIXME : Let's do that only if we work with X-pixels, not with Win-pixels
213     */
214     if (val.function == GXinvert)
215     {
216         val.foreground = (WhitePixel( gdi_display, DefaultScreen(gdi_display) ) ^
217                           BlackPixel( gdi_display, DefaultScreen(gdi_display) ));
218         val.function = GXxor;
219     }
220     val.fill_style = physDev->brush.fillStyle;
221     switch(val.fill_style)
222     {
223     case FillStippled:
224     case FillOpaqueStippled:
225         if (GetBkMode(physDev->hdc)==OPAQUE) val.fill_style = FillOpaqueStippled;
226         val.stipple = physDev->brush.pixmap;
227         mask = GCStipple;
228         break;
229
230     case FillTiled:
231         if (fMapColors && X11DRV_PALETTE_XPixelToPalette)
232         {
233             register int x, y;
234             XImage *image;
235             wine_tsx11_lock();
236             pixmap = XCreatePixmap( gdi_display, root_window, 8, 8, screen_depth );
237             image = XGetImage( gdi_display, physDev->brush.pixmap, 0, 0, 8, 8,
238                                AllPlanes, ZPixmap );
239             for (y = 0; y < 8; y++)
240                 for (x = 0; x < 8; x++)
241                     XPutPixel( image, x, y,
242                                X11DRV_PALETTE_XPixelToPalette[XGetPixel( image, x, y)] );
243             XPutImage( gdi_display, pixmap, gc, image, 0, 0, 0, 0, 8, 8 );
244             XDestroyImage( image );
245             wine_tsx11_unlock();
246             val.tile = pixmap;
247         }
248         else val.tile = physDev->brush.pixmap;
249         mask = GCTile;
250         break;
251
252     default:
253         mask = 0;
254         break;
255     }
256     GetBrushOrgEx( physDev->hdc, &pt );
257     val.ts_x_origin = physDev->dc_rect.left + pt.x;
258     val.ts_y_origin = physDev->dc_rect.top + pt.y;
259     val.fill_rule = (GetPolyFillMode(physDev->hdc) == WINDING) ? WindingRule : EvenOddRule;
260     wine_tsx11_lock();
261     XChangeGC( gdi_display, gc,
262                GCFunction | GCForeground | GCBackground | GCFillStyle |
263                GCFillRule | GCTileStipXOrigin | GCTileStipYOrigin | mask,
264                &val );
265     if (pixmap) XFreePixmap( gdi_display, pixmap );
266     wine_tsx11_unlock();
267     return TRUE;
268 }
269
270
271 /***********************************************************************
272  *           X11DRV_SetupGCForBrush
273  *
274  * Setup physDev->gc for drawing operations using current brush.
275  * Return FALSE if brush is BS_NULL, TRUE otherwise.
276  */
277 BOOL X11DRV_SetupGCForBrush( X11DRV_PDEVICE *physDev )
278 {
279     return X11DRV_SetupGCForPatBlt( physDev, physDev->gc, FALSE );
280 }
281
282
283 /***********************************************************************
284  *           X11DRV_SetupGCForPen
285  *
286  * Setup physDev->gc for drawing operations using current pen.
287  * Return FALSE if pen is PS_NULL, TRUE otherwise.
288  */
289 static BOOL X11DRV_SetupGCForPen( X11DRV_PDEVICE *physDev )
290 {
291     XGCValues val;
292     UINT rop2 = GetROP2(physDev->hdc);
293
294     if (physDev->pen.style == PS_NULL) return FALSE;
295
296     switch (rop2)
297     {
298     case R2_BLACK :
299         val.foreground = BlackPixel( gdi_display, DefaultScreen(gdi_display) );
300         val.function = GXcopy;
301         break;
302     case R2_WHITE :
303         val.foreground = WhitePixel( gdi_display, DefaultScreen(gdi_display) );
304         val.function = GXcopy;
305         break;
306     case R2_XORPEN :
307         val.foreground = physDev->pen.pixel;
308         /* It is very unlikely someone wants to XOR with 0 */
309         /* This fixes the rubber-drawings in paintbrush */
310         if (val.foreground == 0)
311             val.foreground = (WhitePixel( gdi_display, DefaultScreen(gdi_display) ) ^
312                               BlackPixel( gdi_display, DefaultScreen(gdi_display) ));
313         val.function = GXxor;
314         break;
315     default :
316         val.foreground = physDev->pen.pixel;
317         val.function   = X11DRV_XROPfunction[rop2-1];
318     }
319     val.background = physDev->backgroundPixel;
320     val.fill_style = FillSolid;
321     val.line_width = physDev->pen.width;
322     if (val.line_width <= 1) {
323         val.cap_style = CapNotLast;
324     } else {
325         switch (physDev->pen.endcap)
326         {
327         case PS_ENDCAP_SQUARE:
328             val.cap_style = CapProjecting;
329             break;
330         case PS_ENDCAP_FLAT:
331             val.cap_style = CapButt;
332             break;
333         case PS_ENDCAP_ROUND:
334         default:
335             val.cap_style = CapRound;
336         }
337     }
338     switch (physDev->pen.linejoin)
339     {
340     case PS_JOIN_BEVEL:
341         val.join_style = JoinBevel;
342         break;
343     case PS_JOIN_MITER:
344         val.join_style = JoinMiter;
345         break;
346     case PS_JOIN_ROUND:
347     default:
348         val.join_style = JoinRound;
349     }
350
351     if (physDev->pen.dash_len)
352         val.line_style = ((GetBkMode(physDev->hdc) == OPAQUE) && (!physDev->pen.ext))
353                          ? LineDoubleDash : LineOnOffDash;
354     else
355         val.line_style = LineSolid;
356
357     wine_tsx11_lock();
358     if (physDev->pen.dash_len)
359         XSetDashes( gdi_display, physDev->gc, 0, physDev->pen.dashes, physDev->pen.dash_len );
360     XChangeGC( gdi_display, physDev->gc,
361                GCFunction | GCForeground | GCBackground | GCLineWidth |
362                GCLineStyle | GCCapStyle | GCJoinStyle | GCFillStyle, &val );
363     wine_tsx11_unlock();
364     return TRUE;
365 }
366
367
368 /***********************************************************************
369  *           X11DRV_SetupGCForText
370  *
371  * Setup physDev->gc for text drawing operations.
372  * Return FALSE if the font is null, TRUE otherwise.
373  */
374 BOOL X11DRV_SetupGCForText( X11DRV_PDEVICE *physDev )
375 {
376     XFontStruct* xfs = XFONT_GetFontStruct( physDev->font );
377
378     if( xfs )
379     {
380         XGCValues val;
381
382         val.function   = GXcopy;  /* Text is always GXcopy */
383         val.foreground = physDev->textPixel;
384         val.background = physDev->backgroundPixel;
385         val.fill_style = FillSolid;
386         val.font       = xfs->fid;
387
388         wine_tsx11_lock();
389         XChangeGC( gdi_display, physDev->gc,
390                    GCFunction | GCForeground | GCBackground | GCFillStyle |
391                    GCFont, &val );
392         wine_tsx11_unlock();
393         return TRUE;
394     }
395     WARN("Physical font failure\n" );
396     return FALSE;
397 }
398
399 /***********************************************************************
400  *           X11DRV_XWStoDS
401  *
402  * Performs a world-to-viewport transformation on the specified width.
403  */
404 INT X11DRV_XWStoDS( X11DRV_PDEVICE *physDev, INT width )
405 {
406     POINT pt[2];
407
408     pt[0].x = 0;
409     pt[0].y = 0;
410     pt[1].x = width;
411     pt[1].y = 0;
412     LPtoDP( physDev->hdc, pt, 2 );
413     return pt[1].x - pt[0].x;
414 }
415
416 /***********************************************************************
417  *           X11DRV_YWStoDS
418  *
419  * Performs a world-to-viewport transformation on the specified height.
420  */
421 INT X11DRV_YWStoDS( X11DRV_PDEVICE *physDev, INT height )
422 {
423     POINT pt[2];
424
425     pt[0].x = 0;
426     pt[0].y = 0;
427     pt[1].x = 0;
428     pt[1].y = height;
429     LPtoDP( physDev->hdc, pt, 2 );
430     return pt[1].y - pt[0].y;
431 }
432
433 /***********************************************************************
434  *           X11DRV_LineTo
435  */
436 BOOL CDECL
437 X11DRV_LineTo( X11DRV_PDEVICE *physDev, INT x, INT y )
438 {
439     POINT pt[2];
440
441     if (X11DRV_SetupGCForPen( physDev )) {
442         /* Update the pixmap from the DIB section */
443         X11DRV_LockDIBSection(physDev, DIB_Status_GdiMod);
444
445         GetCurrentPositionEx( physDev->hdc, &pt[0] );
446         pt[1].x = x;
447         pt[1].y = y;
448         LPtoDP( physDev->hdc, pt, 2 );
449
450         wine_tsx11_lock();
451         XDrawLine(gdi_display, physDev->drawable, physDev->gc,
452                   physDev->dc_rect.left + pt[0].x, physDev->dc_rect.top + pt[0].y,
453                   physDev->dc_rect.left + pt[1].x, physDev->dc_rect.top + pt[1].y );
454         wine_tsx11_unlock();
455
456         /* Update the DIBSection from the pixmap */
457         X11DRV_UnlockDIBSection(physDev, TRUE);
458     }
459     return TRUE;
460 }
461
462
463
464 /***********************************************************************
465  *           X11DRV_DrawArc
466  *
467  * Helper functions for Arc(), Chord() and Pie().
468  * 'lines' is the number of lines to draw: 0 for Arc, 1 for Chord, 2 for Pie.
469  *
470  */
471 static BOOL
472 X11DRV_DrawArc( X11DRV_PDEVICE *physDev, INT left, INT top, INT right,
473                 INT bottom, INT xstart, INT ystart,
474                 INT xend, INT yend, INT lines )
475 {
476     INT xcenter, ycenter, istart_angle, idiff_angle;
477     INT width, oldwidth;
478     double start_angle, end_angle;
479     XPoint points[4];
480     BOOL update = FALSE;
481     POINT start, end;
482     RECT rc;
483
484     SetRect(&rc, left, top, right, bottom);
485     start.x = xstart;
486     start.y = ystart;
487     end.x = xend;
488     end.y = yend;
489     LPtoDP(physDev->hdc, (POINT*)&rc, 2);
490     LPtoDP(physDev->hdc, &start, 1);
491     LPtoDP(physDev->hdc, &end, 1);
492
493     if (rc.right < rc.left) { INT tmp = rc.right; rc.right = rc.left; rc.left = tmp; }
494     if (rc.bottom < rc.top) { INT tmp = rc.bottom; rc.bottom = rc.top; rc.top = tmp; }
495     if ((rc.left == rc.right) || (rc.top == rc.bottom)
496             ||(lines && ((rc.right-rc.left==1)||(rc.bottom-rc.top==1)))) return TRUE;
497
498     if (GetArcDirection( physDev->hdc ) == AD_CLOCKWISE)
499       { POINT tmp = start; start = end; end = tmp; }
500
501     oldwidth = width = physDev->pen.width;
502     if (!width) width = 1;
503     if(physDev->pen.style == PS_NULL) width = 0;
504
505     if ((physDev->pen.style == PS_INSIDEFRAME))
506     {
507         if (2*width > (rc.right-rc.left)) width=(rc.right-rc.left + 1)/2;
508         if (2*width > (rc.bottom-rc.top)) width=(rc.bottom-rc.top + 1)/2;
509         rc.left   += width / 2;
510         rc.right  -= (width - 1) / 2;
511         rc.top    += width / 2;
512         rc.bottom -= (width - 1) / 2;
513     }
514     if(width == 0) width = 1; /* more accurate */
515     physDev->pen.width = width;
516
517     xcenter = (rc.right + rc.left) / 2;
518     ycenter = (rc.bottom + rc.top) / 2;
519     start_angle = atan2( (double)(ycenter-start.y)*(rc.right-rc.left),
520                          (double)(start.x-xcenter)*(rc.bottom-rc.top) );
521     end_angle   = atan2( (double)(ycenter-end.y)*(rc.right-rc.left),
522                          (double)(end.x-xcenter)*(rc.bottom-rc.top) );
523     if ((start.x==end.x)&&(start.y==end.y))
524       { /* A lazy program delivers xstart=xend=ystart=yend=0) */
525         start_angle = 0;
526         end_angle = 2* PI;
527       }
528     else /* notorious cases */
529       if ((start_angle == PI)&&( end_angle <0))
530         start_angle = - PI;
531     else
532       if ((end_angle == PI)&&( start_angle <0))
533         end_angle = - PI;
534     istart_angle = (INT)(start_angle * 180 * 64 / PI + 0.5);
535     idiff_angle  = (INT)((end_angle - start_angle) * 180 * 64 / PI + 0.5);
536     if (idiff_angle <= 0) idiff_angle += 360 * 64;
537
538     /* Update the pixmap from the DIB section */
539     X11DRV_LockDIBSection(physDev, DIB_Status_GdiMod);
540
541       /* Fill arc with brush if Chord() or Pie() */
542
543     if ((lines > 0) && X11DRV_SetupGCForBrush( physDev )) {
544         wine_tsx11_lock();
545         XSetArcMode( gdi_display, physDev->gc, (lines==1) ? ArcChord : ArcPieSlice);
546         XFillArc( gdi_display, physDev->drawable, physDev->gc,
547                   physDev->dc_rect.left + rc.left, physDev->dc_rect.top + rc.top,
548                   rc.right-rc.left-1, rc.bottom-rc.top-1, istart_angle, idiff_angle );
549         wine_tsx11_unlock();
550         update = TRUE;
551     }
552
553       /* Draw arc and lines */
554
555     if (X11DRV_SetupGCForPen( physDev ))
556     {
557         wine_tsx11_lock();
558         XDrawArc( gdi_display, physDev->drawable, physDev->gc,
559                   physDev->dc_rect.left + rc.left, physDev->dc_rect.top + rc.top,
560                   rc.right-rc.left-1, rc.bottom-rc.top-1, istart_angle, idiff_angle );
561         if (lines) {
562             /* use the truncated values */
563             start_angle=(double)istart_angle*PI/64./180.;
564             end_angle=(double)(istart_angle+idiff_angle)*PI/64./180.;
565             /* calculate the endpoints and round correctly */
566             points[0].x = (int) floor(physDev->dc_rect.left + (rc.right+rc.left)/2.0 +
567                     cos(start_angle) * (rc.right-rc.left-width*2+2) / 2. + 0.5);
568             points[0].y = (int) floor(physDev->dc_rect.top + (rc.top+rc.bottom)/2.0 -
569                     sin(start_angle) * (rc.bottom-rc.top-width*2+2) / 2. + 0.5);
570             points[1].x = (int) floor(physDev->dc_rect.left + (rc.right+rc.left)/2.0 +
571                     cos(end_angle) * (rc.right-rc.left-width*2+2) / 2. + 0.5);
572             points[1].y = (int) floor(physDev->dc_rect.top + (rc.top+rc.bottom)/2.0 -
573                     sin(end_angle) * (rc.bottom-rc.top-width*2+2) / 2. + 0.5);
574
575             /* OK, this stuff is optimized for Xfree86
576              * which is probably the server most used by
577              * wine users. Other X servers will not
578              * display correctly. (eXceed for instance)
579              * so if you feel you must make changes, make sure that
580              * you either use Xfree86 or separate your changes
581              * from these (compile switch or whatever)
582              */
583             if (lines == 2) {
584                 INT dx1,dy1;
585                 points[3] = points[1];
586                 points[1].x = physDev->dc_rect.left + xcenter;
587                 points[1].y = physDev->dc_rect.top + ycenter;
588                 points[2] = points[1];
589                 dx1=points[1].x-points[0].x;
590                 dy1=points[1].y-points[0].y;
591                 if(((rc.top-rc.bottom) | -2) == -2)
592                     if(dy1>0) points[1].y--;
593                 if(dx1<0) {
594                     if (((-dx1)*64)<=ABS(dy1)*37) points[0].x--;
595                     if(((-dx1*9))<(dy1*16)) points[0].y--;
596                     if( dy1<0 && ((dx1*9)) < (dy1*16)) points[0].y--;
597                 } else {
598                     if(dy1 < 0)  points[0].y--;
599                     if(((rc.right-rc.left) | -2) == -2) points[1].x--;
600                 }
601                 dx1=points[3].x-points[2].x;
602                 dy1=points[3].y-points[2].y;
603                 if(((rc.top-rc.bottom) | -2 ) == -2)
604                     if(dy1 < 0) points[2].y--;
605                 if( dx1<0){
606                     if( dy1>0) points[3].y--;
607                     if(((rc.right-rc.left) | -2) == -2 ) points[2].x--;
608                 }else {
609                     points[3].y--;
610                     if( dx1 * 64 < dy1 * -37 ) points[3].x--;
611                 }
612                 lines++;
613             }
614             XDrawLines( gdi_display, physDev->drawable, physDev->gc,
615                         points, lines+1, CoordModeOrigin );
616         }
617         wine_tsx11_unlock();
618         update = TRUE;
619     }
620
621     /* Update the DIBSection of the pixmap */
622     X11DRV_UnlockDIBSection(physDev, update);
623
624     physDev->pen.width = oldwidth;
625     return TRUE;
626 }
627
628
629 /***********************************************************************
630  *           X11DRV_Arc
631  */
632 BOOL CDECL
633 X11DRV_Arc( X11DRV_PDEVICE *physDev, INT left, INT top, INT right, INT bottom,
634             INT xstart, INT ystart, INT xend, INT yend )
635 {
636     return X11DRV_DrawArc( physDev, left, top, right, bottom,
637                            xstart, ystart, xend, yend, 0 );
638 }
639
640
641 /***********************************************************************
642  *           X11DRV_Pie
643  */
644 BOOL CDECL
645 X11DRV_Pie( X11DRV_PDEVICE *physDev, INT left, INT top, INT right, INT bottom,
646             INT xstart, INT ystart, INT xend, INT yend )
647 {
648     return X11DRV_DrawArc( physDev, left, top, right, bottom,
649                            xstart, ystart, xend, yend, 2 );
650 }
651
652 /***********************************************************************
653  *           X11DRV_Chord
654  */
655 BOOL CDECL
656 X11DRV_Chord( X11DRV_PDEVICE *physDev, INT left, INT top, INT right, INT bottom,
657               INT xstart, INT ystart, INT xend, INT yend )
658 {
659     return X11DRV_DrawArc( physDev, left, top, right, bottom,
660                            xstart, ystart, xend, yend, 1 );
661 }
662
663
664 /***********************************************************************
665  *           X11DRV_Ellipse
666  */
667 BOOL CDECL
668 X11DRV_Ellipse( X11DRV_PDEVICE *physDev, INT left, INT top, INT right, INT bottom )
669 {
670     INT width, oldwidth;
671     BOOL update = FALSE;
672     RECT rc;
673
674     SetRect(&rc, left, top, right, bottom);
675     LPtoDP(physDev->hdc, (POINT*)&rc, 2);
676
677     if ((rc.left == rc.right) || (rc.top == rc.bottom)) return TRUE;
678
679     if (rc.right < rc.left) { INT tmp = rc.right; rc.right = rc.left; rc.left = tmp; }
680     if (rc.bottom < rc.top) { INT tmp = rc.bottom; rc.bottom = rc.top; rc.top = tmp; }
681
682     oldwidth = width = physDev->pen.width;
683     if (!width) width = 1;
684     if(physDev->pen.style == PS_NULL) width = 0;
685
686     if ((physDev->pen.style == PS_INSIDEFRAME))
687     {
688         if (2*width > (rc.right-rc.left)) width=(rc.right-rc.left + 1)/2;
689         if (2*width > (rc.bottom-rc.top)) width=(rc.bottom-rc.top + 1)/2;
690         rc.left   += width / 2;
691         rc.right  -= (width - 1) / 2;
692         rc.top    += width / 2;
693         rc.bottom -= (width - 1) / 2;
694     }
695     if(width == 0) width = 1; /* more accurate */
696     physDev->pen.width = width;
697
698     /* Update the pixmap from the DIB section */
699     X11DRV_LockDIBSection(physDev, DIB_Status_GdiMod);
700
701     if (X11DRV_SetupGCForBrush( physDev ))
702     {
703         wine_tsx11_lock();
704         XFillArc( gdi_display, physDev->drawable, physDev->gc,
705                   physDev->dc_rect.left + rc.left, physDev->dc_rect.top + rc.top,
706                   rc.right-rc.left-1, rc.bottom-rc.top-1, 0, 360*64 );
707         wine_tsx11_unlock();
708         update = TRUE;
709     }
710     if (X11DRV_SetupGCForPen( physDev ))
711     {
712         wine_tsx11_lock();
713         XDrawArc( gdi_display, physDev->drawable, physDev->gc,
714                   physDev->dc_rect.left + rc.left, physDev->dc_rect.top + rc.top,
715                   rc.right-rc.left-1, rc.bottom-rc.top-1, 0, 360*64 );
716         wine_tsx11_unlock();
717         update = TRUE;
718     }
719
720     /* Update the DIBSection from the pixmap */
721     X11DRV_UnlockDIBSection(physDev, update);
722
723     physDev->pen.width = oldwidth;
724     return TRUE;
725 }
726
727
728 /***********************************************************************
729  *           X11DRV_Rectangle
730  */
731 BOOL CDECL
732 X11DRV_Rectangle(X11DRV_PDEVICE *physDev, INT left, INT top, INT right, INT bottom)
733 {
734     INT width, oldwidth, oldjoinstyle;
735     BOOL update = FALSE;
736     RECT rc;
737
738     TRACE("(%d %d %d %d)\n", left, top, right, bottom);
739
740     SetRect(&rc, left, top, right, bottom);
741     LPtoDP(physDev->hdc, (POINT*)&rc, 2);
742
743     if ((rc.left == rc.right) || (rc.top == rc.bottom)) return TRUE;
744
745     if (rc.right < rc.left) { INT tmp = rc.right; rc.right = rc.left; rc.left = tmp; }
746     if (rc.bottom < rc.top) { INT tmp = rc.bottom; rc.bottom = rc.top; rc.top = tmp; }
747
748     oldwidth = width = physDev->pen.width;
749     if (!width) width = 1;
750     if(physDev->pen.style == PS_NULL) width = 0;
751
752     if ((physDev->pen.style == PS_INSIDEFRAME))
753     {
754         if (2*width > (rc.right-rc.left)) width=(rc.right-rc.left + 1)/2;
755         if (2*width > (rc.bottom-rc.top)) width=(rc.bottom-rc.top + 1)/2;
756         rc.left   += width / 2;
757         rc.right  -= (width - 1) / 2;
758         rc.top    += width / 2;
759         rc.bottom -= (width - 1) / 2;
760     }
761     if(width == 1) width = 0;
762     physDev->pen.width = width;
763     oldjoinstyle = physDev->pen.linejoin;
764     if(physDev->pen.type != PS_GEOMETRIC)
765         physDev->pen.linejoin = PS_JOIN_MITER;
766
767     /* Update the pixmap from the DIB section */
768     X11DRV_LockDIBSection(physDev, DIB_Status_GdiMod);
769
770     if ((rc.right > rc.left + width) && (rc.bottom > rc.top + width))
771     {
772         if (X11DRV_SetupGCForBrush( physDev ))
773         {
774             wine_tsx11_lock();
775             XFillRectangle( gdi_display, physDev->drawable, physDev->gc,
776                             physDev->dc_rect.left + rc.left + (width + 1) / 2,
777                             physDev->dc_rect.top + rc.top + (width + 1) / 2,
778                             rc.right-rc.left-width-1, rc.bottom-rc.top-width-1);
779             wine_tsx11_unlock();
780             update = TRUE;
781         }
782     }
783     if (X11DRV_SetupGCForPen( physDev ))
784     {
785         wine_tsx11_lock();
786         XDrawRectangle( gdi_display, physDev->drawable, physDev->gc,
787                         physDev->dc_rect.left + rc.left, physDev->dc_rect.top + rc.top,
788                         rc.right-rc.left-1, rc.bottom-rc.top-1 );
789         wine_tsx11_unlock();
790         update = TRUE;
791     }
792
793     /* Update the DIBSection from the pixmap */
794     X11DRV_UnlockDIBSection(physDev, update);
795
796     physDev->pen.width = oldwidth;
797     physDev->pen.linejoin = oldjoinstyle;
798     return TRUE;
799 }
800
801 /***********************************************************************
802  *           X11DRV_RoundRect
803  */
804 BOOL CDECL
805 X11DRV_RoundRect( X11DRV_PDEVICE *physDev, INT left, INT top, INT right,
806                   INT bottom, INT ell_width, INT ell_height )
807 {
808     INT width, oldwidth, oldendcap;
809     BOOL update = FALSE;
810     RECT rc;
811     POINT pts[2];
812
813     TRACE("(%d %d %d %d  %d %d\n",
814         left, top, right, bottom, ell_width, ell_height);
815
816     SetRect(&rc, left, top, right, bottom);
817     LPtoDP(physDev->hdc, (POINT*)&rc, 2);
818
819     if ((rc.left == rc.right) || (rc.top == rc.bottom))
820         return TRUE;
821
822     /* Make sure ell_width and ell_height are >= 1 otherwise XDrawArc gets
823        called with width/height < 0 */
824     pts[0].x = pts[0].y = 0;
825     pts[1].x = ell_width;
826     pts[1].y = ell_height;
827     LPtoDP(physDev->hdc, pts, 2);
828     ell_width  = max(abs( pts[1].x - pts[0].x ), 1);
829     ell_height = max(abs( pts[1].y - pts[0].y ), 1);
830
831     /* Fix the coordinates */
832
833     if (rc.right < rc.left) { INT tmp = rc.right; rc.right = rc.left; rc.left = tmp; }
834     if (rc.bottom < rc.top) { INT tmp = rc.bottom; rc.bottom = rc.top; rc.top = tmp; }
835
836     oldwidth = width = physDev->pen.width;
837     oldendcap = physDev->pen.endcap;
838     if (!width) width = 1;
839     if(physDev->pen.style == PS_NULL) width = 0;
840
841     if ((physDev->pen.style == PS_INSIDEFRAME))
842     {
843         if (2*width > (rc.right-rc.left)) width=(rc.right-rc.left + 1)/2;
844         if (2*width > (rc.bottom-rc.top)) width=(rc.bottom-rc.top + 1)/2;
845         rc.left   += width / 2;
846         rc.right  -= (width - 1) / 2;
847         rc.top    += width / 2;
848         rc.bottom -= (width - 1) / 2;
849     }
850     if(width == 0) width = 1;
851     physDev->pen.width = width;
852     physDev->pen.endcap = PS_ENDCAP_SQUARE;
853
854     /* Update the pixmap from the DIB section */
855     X11DRV_LockDIBSection(physDev, DIB_Status_GdiMod);
856
857     if (X11DRV_SetupGCForBrush( physDev ))
858     {
859         wine_tsx11_lock();
860         if (ell_width > (rc.right-rc.left) )
861             if (ell_height > (rc.bottom-rc.top) )
862                 XFillArc( gdi_display, physDev->drawable, physDev->gc,
863                           physDev->dc_rect.left + rc.left, physDev->dc_rect.top + rc.top,
864                           rc.right - rc.left - 1, rc.bottom - rc.top - 1,
865                           0, 360 * 64 );
866             else{
867                 XFillArc( gdi_display, physDev->drawable, physDev->gc,
868                           physDev->dc_rect.left + rc.left, physDev->dc_rect.top + rc.top,
869                           rc.right - rc.left - 1, ell_height, 0, 180 * 64 );
870                 XFillArc( gdi_display, physDev->drawable, physDev->gc,
871                           physDev->dc_rect.left + rc.left,
872                           physDev->dc_rect.top + rc.bottom - ell_height - 1,
873                           rc.right - rc.left - 1, ell_height, 180 * 64,
874                           180 * 64 );
875             }
876         else if (ell_height > (rc.bottom-rc.top) ){
877             XFillArc( gdi_display, physDev->drawable, physDev->gc,
878                       physDev->dc_rect.left + rc.left, physDev->dc_rect.top + rc.top,
879                       ell_width, rc.bottom - rc.top - 1, 90 * 64, 180 * 64 );
880             XFillArc( gdi_display, physDev->drawable, physDev->gc,
881                       physDev->dc_rect.left + rc.right - ell_width - 1, physDev->dc_rect.top + rc.top,
882                       ell_width, rc.bottom - rc.top - 1, 270 * 64, 180 * 64 );
883         }else{
884             XFillArc( gdi_display, physDev->drawable, physDev->gc,
885                       physDev->dc_rect.left + rc.left, physDev->dc_rect.top + rc.top,
886                       ell_width, ell_height, 90 * 64, 90 * 64 );
887             XFillArc( gdi_display, physDev->drawable, physDev->gc,
888                       physDev->dc_rect.left + rc.left,
889                       physDev->dc_rect.top + rc.bottom - ell_height - 1,
890                       ell_width, ell_height, 180 * 64, 90 * 64 );
891             XFillArc( gdi_display, physDev->drawable, physDev->gc,
892                       physDev->dc_rect.left + rc.right - ell_width - 1,
893                       physDev->dc_rect.top + rc.bottom - ell_height - 1,
894                       ell_width, ell_height, 270 * 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.top,
898                       ell_width, ell_height, 0, 90 * 64 );
899         }
900         if (ell_width < rc.right - rc.left)
901         {
902             XFillRectangle( gdi_display, physDev->drawable, physDev->gc,
903                             physDev->dc_rect.left + rc.left + (ell_width + 1) / 2,
904                             physDev->dc_rect.top + rc.top + 1,
905                             rc.right - rc.left - ell_width - 1,
906                             (ell_height + 1) / 2 - 1);
907             XFillRectangle( gdi_display, physDev->drawable, physDev->gc,
908                             physDev->dc_rect.left + rc.left + (ell_width + 1) / 2,
909                             physDev->dc_rect.top + rc.bottom - (ell_height) / 2 - 1,
910                             rc.right - rc.left - ell_width - 1,
911                             (ell_height) / 2 );
912         }
913         if  (ell_height < rc.bottom - rc.top)
914         {
915             XFillRectangle( gdi_display, physDev->drawable, physDev->gc,
916                             physDev->dc_rect.left + rc.left + 1,
917                             physDev->dc_rect.top + rc.top + (ell_height + 1) / 2,
918                             rc.right - rc.left - 2,
919                             rc.bottom - rc.top - ell_height - 1);
920         }
921         wine_tsx11_unlock();
922         update = TRUE;
923     }
924     /* FIXME: this could be done with on X call
925      * more efficient and probably more correct
926      * on any X server: XDrawArcs will draw
927      * straight horizontal and vertical lines
928      * if width or height are zero.
929      *
930      * BTW this stuff is optimized for an Xfree86 server
931      * read the comments inside the X11DRV_DrawArc function
932      */
933     if (X11DRV_SetupGCForPen( physDev ))
934     {
935         wine_tsx11_lock();
936         if (ell_width > (rc.right-rc.left) )
937             if (ell_height > (rc.bottom-rc.top) )
938                 XDrawArc( gdi_display, physDev->drawable, physDev->gc,
939                           physDev->dc_rect.left + rc.left, physDev->dc_rect.top + rc.top,
940                           rc.right - rc.left - 1, rc.bottom - rc.top - 1, 0 , 360 * 64 );
941             else{
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, ell_height - 1, 0 , 180 * 64 );
945                 XDrawArc( gdi_display, physDev->drawable, physDev->gc,
946                           physDev->dc_rect.left + rc.left,
947                           physDev->dc_rect.top + rc.bottom - ell_height,
948                           rc.right - rc.left - 1, ell_height - 1, 180 * 64 , 180 * 64 );
949             }
950         else if (ell_height > (rc.bottom-rc.top) ){
951             XDrawArc( gdi_display, physDev->drawable, physDev->gc,
952                       physDev->dc_rect.left + rc.left, physDev->dc_rect.top + rc.top,
953                       ell_width - 1 , rc.bottom - rc.top - 1, 90 * 64 , 180 * 64 );
954             XDrawArc( gdi_display, physDev->drawable, physDev->gc,
955                       physDev->dc_rect.left + rc.right - ell_width,
956                       physDev->dc_rect.top + rc.top,
957                       ell_width - 1 , rc.bottom - rc.top - 1, 270 * 64 , 180 * 64 );
958         }else{
959             XDrawArc( gdi_display, physDev->drawable, physDev->gc,
960                       physDev->dc_rect.left + rc.left, physDev->dc_rect.top + rc.top,
961                       ell_width - 1, ell_height - 1, 90 * 64, 90 * 64 );
962             XDrawArc( gdi_display, physDev->drawable, physDev->gc,
963                       physDev->dc_rect.left + rc.left, physDev->dc_rect.top + rc.bottom - ell_height,
964                       ell_width - 1, ell_height - 1, 180 * 64, 90 * 64 );
965             XDrawArc( gdi_display, physDev->drawable, physDev->gc,
966                       physDev->dc_rect.left + rc.right - ell_width,
967                       physDev->dc_rect.top + rc.bottom - ell_height,
968                       ell_width - 1, ell_height - 1, 270 * 64, 90 * 64 );
969             XDrawArc( gdi_display, physDev->drawable, physDev->gc,
970                       physDev->dc_rect.left + rc.right - ell_width, physDev->dc_rect.top + rc.top,
971                       ell_width - 1, ell_height - 1, 0, 90 * 64 );
972         }
973         if (ell_width < rc.right - rc.left)
974         {
975             XDrawLine( gdi_display, physDev->drawable, physDev->gc,
976                        physDev->dc_rect.left + rc.left + ell_width / 2,
977                        physDev->dc_rect.top + rc.top,
978                        physDev->dc_rect.left + rc.right - (ell_width+1) / 2,
979                        physDev->dc_rect.top + rc.top);
980             XDrawLine( gdi_display, physDev->drawable, physDev->gc,
981                        physDev->dc_rect.left + rc.left + ell_width / 2 ,
982                        physDev->dc_rect.top + rc.bottom - 1,
983                        physDev->dc_rect.left + rc.right - (ell_width+1)/ 2,
984                        physDev->dc_rect.top + rc.bottom - 1);
985         }
986         if (ell_height < rc.bottom - rc.top)
987         {
988             XDrawLine( gdi_display, physDev->drawable, physDev->gc,
989                        physDev->dc_rect.left + rc.right - 1,
990                        physDev->dc_rect.top + rc.top + ell_height / 2,
991                        physDev->dc_rect.left + rc.right - 1,
992                        physDev->dc_rect.top + rc.bottom - (ell_height+1) / 2);
993             XDrawLine( gdi_display, physDev->drawable, physDev->gc,
994                        physDev->dc_rect.left + rc.left,
995                        physDev->dc_rect.top + rc.top + ell_height / 2,
996                        physDev->dc_rect.left + rc.left,
997                        physDev->dc_rect.top + rc.bottom - (ell_height+1) / 2);
998         }
999         wine_tsx11_unlock();
1000         update = TRUE;
1001     }
1002     /* Update the DIBSection from the pixmap */
1003     X11DRV_UnlockDIBSection(physDev, update);
1004
1005     physDev->pen.width = oldwidth;
1006     physDev->pen.endcap = oldendcap;
1007     return TRUE;
1008 }
1009
1010
1011 /***********************************************************************
1012  *           X11DRV_SetPixel
1013  */
1014 COLORREF CDECL
1015 X11DRV_SetPixel( X11DRV_PDEVICE *physDev, INT x, INT y, COLORREF color )
1016 {
1017     unsigned long pixel;
1018     POINT pt;
1019
1020     pt.x = x;
1021     pt.y = y;
1022     LPtoDP( physDev->hdc, &pt, 1 );
1023     pixel = X11DRV_PALETTE_ToPhysical( physDev, color );
1024
1025     /* Update the pixmap from the DIB section */
1026     X11DRV_LockDIBSection(physDev, DIB_Status_GdiMod);
1027
1028     /* inefficient but simple... */
1029     wine_tsx11_lock();
1030     XSetForeground( gdi_display, physDev->gc, pixel );
1031     XSetFunction( gdi_display, physDev->gc, GXcopy );
1032     XDrawPoint( gdi_display, physDev->drawable, physDev->gc,
1033                 physDev->dc_rect.left + pt.x, physDev->dc_rect.top + pt.y );
1034     wine_tsx11_unlock();
1035
1036     /* Update the DIBSection from the pixmap */
1037     X11DRV_UnlockDIBSection(physDev, TRUE);
1038
1039     return X11DRV_PALETTE_ToLogical(pixel);
1040 }
1041
1042
1043 /***********************************************************************
1044  *           X11DRV_GetPixel
1045  */
1046 COLORREF CDECL
1047 X11DRV_GetPixel( X11DRV_PDEVICE *physDev, INT x, INT y )
1048 {
1049     static Pixmap pixmap = 0;
1050     XImage * image;
1051     int pixel;
1052     POINT pt;
1053     BOOL memdc = (GetObjectType(physDev->hdc) == OBJ_MEMDC);
1054
1055     pt.x = x;
1056     pt.y = y;
1057     LPtoDP( physDev->hdc, &pt, 1 );
1058
1059     /* Update the pixmap from the DIB section */
1060     X11DRV_LockDIBSection(physDev, DIB_Status_GdiMod);
1061
1062     wine_tsx11_lock();
1063     if (memdc)
1064     {
1065         image = XGetImage( gdi_display, physDev->drawable,
1066                            physDev->dc_rect.left + pt.x, physDev->dc_rect.top + pt.y,
1067                            1, 1, AllPlanes, ZPixmap );
1068     }
1069     else
1070     {
1071         /* If we are reading from the screen, use a temporary copy */
1072         /* to avoid a BadMatch error */
1073         if (!pixmap) pixmap = XCreatePixmap( gdi_display, root_window,
1074                                              1, 1, physDev->depth );
1075         XCopyArea( gdi_display, physDev->drawable, pixmap, get_bitmap_gc(physDev->depth),
1076                    physDev->dc_rect.left + pt.x, physDev->dc_rect.top + pt.y, 1, 1, 0, 0 );
1077         image = XGetImage( gdi_display, pixmap, 0, 0, 1, 1, AllPlanes, ZPixmap );
1078     }
1079     pixel = XGetPixel( image, 0, 0 );
1080     XDestroyImage( image );
1081     wine_tsx11_unlock();
1082
1083     /* Update the DIBSection from the pixmap */
1084     X11DRV_UnlockDIBSection(physDev, FALSE);
1085     if( physDev->depth > 1)
1086         pixel = X11DRV_PALETTE_ToLogical(pixel);
1087     else
1088         /* monochrome bitmaps return black or white */
1089         if( pixel) pixel = 0xffffff;
1090     return pixel;
1091
1092 }
1093
1094
1095 /***********************************************************************
1096  *           X11DRV_PaintRgn
1097  */
1098 BOOL CDECL
1099 X11DRV_PaintRgn( X11DRV_PDEVICE *physDev, HRGN hrgn )
1100 {
1101     if (X11DRV_SetupGCForBrush( physDev ))
1102     {
1103         unsigned int i;
1104         XRectangle *rect;
1105         RGNDATA *data = X11DRV_GetRegionData( hrgn, physDev->hdc );
1106
1107         if (!data) return FALSE;
1108         rect = (XRectangle *)data->Buffer;
1109         for (i = 0; i < data->rdh.nCount; i++)
1110         {
1111             rect[i].x += physDev->dc_rect.left;
1112             rect[i].y += physDev->dc_rect.top;
1113         }
1114
1115         X11DRV_LockDIBSection(physDev, DIB_Status_GdiMod);
1116         wine_tsx11_lock();
1117         XFillRectangles( gdi_display, physDev->drawable, physDev->gc, rect, data->rdh.nCount );
1118         wine_tsx11_unlock();
1119         X11DRV_UnlockDIBSection(physDev, TRUE);
1120         HeapFree( GetProcessHeap(), 0, data );
1121     }
1122     return TRUE;
1123 }
1124
1125 /**********************************************************************
1126  *          X11DRV_Polyline
1127  */
1128 BOOL CDECL
1129 X11DRV_Polyline( X11DRV_PDEVICE *physDev, const POINT* pt, INT count )
1130 {
1131     int i;
1132     XPoint *points;
1133
1134     if (!(points = HeapAlloc( GetProcessHeap(), 0, sizeof(XPoint) * count )))
1135     {
1136         WARN("No memory to convert POINTs to XPoints!\n");
1137         return FALSE;
1138     }
1139     for (i = 0; i < count; i++)
1140     {
1141         POINT tmp = pt[i];
1142         LPtoDP(physDev->hdc, &tmp, 1);
1143         points[i].x = physDev->dc_rect.left + tmp.x;
1144         points[i].y = physDev->dc_rect.top + tmp.y;
1145     }
1146
1147     if (X11DRV_SetupGCForPen ( physDev ))
1148     {
1149         X11DRV_LockDIBSection(physDev, DIB_Status_GdiMod);
1150         wine_tsx11_lock();
1151         XDrawLines( gdi_display, physDev->drawable, physDev->gc,
1152                     points, count, CoordModeOrigin );
1153         wine_tsx11_unlock();
1154         X11DRV_UnlockDIBSection(physDev, TRUE);
1155     }
1156
1157     HeapFree( GetProcessHeap(), 0, points );
1158     return TRUE;
1159 }
1160
1161
1162 /**********************************************************************
1163  *          X11DRV_Polygon
1164  */
1165 BOOL CDECL
1166 X11DRV_Polygon( X11DRV_PDEVICE *physDev, const POINT* pt, INT count )
1167 {
1168     register int i;
1169     XPoint *points;
1170     BOOL update = FALSE;
1171
1172     if (!(points = HeapAlloc( GetProcessHeap(), 0, sizeof(XPoint) * (count+1) )))
1173     {
1174         WARN("No memory to convert POINTs to XPoints!\n");
1175         return FALSE;
1176     }
1177     for (i = 0; i < count; i++)
1178     {
1179         POINT tmp = pt[i];
1180         LPtoDP(physDev->hdc, &tmp, 1);
1181         points[i].x = physDev->dc_rect.left + tmp.x;
1182         points[i].y = physDev->dc_rect.top + tmp.y;
1183     }
1184     points[count] = points[0];
1185
1186     /* Update the pixmap from the DIB section */
1187     X11DRV_LockDIBSection(physDev, DIB_Status_GdiMod);
1188
1189     if (X11DRV_SetupGCForBrush( physDev ))
1190     {
1191         wine_tsx11_lock();
1192         XFillPolygon( gdi_display, physDev->drawable, physDev->gc,
1193                       points, count+1, Complex, CoordModeOrigin);
1194         wine_tsx11_unlock();
1195         update = TRUE;
1196     }
1197     if (X11DRV_SetupGCForPen ( physDev ))
1198     {
1199         wine_tsx11_lock();
1200         XDrawLines( gdi_display, physDev->drawable, physDev->gc,
1201                     points, count+1, CoordModeOrigin );
1202         wine_tsx11_unlock();
1203         update = TRUE;
1204     }
1205
1206     /* Update the DIBSection from the pixmap */
1207     X11DRV_UnlockDIBSection(physDev, update);
1208
1209     HeapFree( GetProcessHeap(), 0, points );
1210     return TRUE;
1211 }
1212
1213
1214 /**********************************************************************
1215  *          X11DRV_PolyPolygon
1216  */
1217 BOOL CDECL
1218 X11DRV_PolyPolygon( X11DRV_PDEVICE *physDev, const POINT* pt, const INT* counts, UINT polygons)
1219 {
1220     HRGN hrgn;
1221
1222     /* FIXME: The points should be converted to device coords before */
1223     /* creating the region. */
1224
1225     hrgn = CreatePolyPolygonRgn( pt, counts, polygons, GetPolyFillMode( physDev->hdc ) );
1226     X11DRV_PaintRgn( physDev, hrgn );
1227     DeleteObject( hrgn );
1228
1229       /* Draw the outline of the polygons */
1230
1231     if (X11DRV_SetupGCForPen ( physDev ))
1232     {
1233         unsigned int i;
1234         int j, max = 0;
1235         XPoint *points;
1236
1237         /* Update the pixmap from the DIB section */
1238         X11DRV_LockDIBSection(physDev, DIB_Status_GdiMod);
1239
1240         for (i = 0; i < polygons; i++) if (counts[i] > max) max = counts[i];
1241         if (!(points = HeapAlloc( GetProcessHeap(), 0, sizeof(XPoint) * (max+1) )))
1242         {
1243             WARN("No memory to convert POINTs to XPoints!\n");
1244             return FALSE;
1245         }
1246         for (i = 0; i < polygons; i++)
1247         {
1248             for (j = 0; j < counts[i]; j++)
1249             {
1250                 POINT tmp = *pt;
1251                 LPtoDP(physDev->hdc, &tmp, 1);
1252                 points[j].x = physDev->dc_rect.left + tmp.x;
1253                 points[j].y = physDev->dc_rect.top + tmp.y;
1254                 pt++;
1255             }
1256             points[j] = points[0];
1257             wine_tsx11_lock();
1258             XDrawLines( gdi_display, physDev->drawable, physDev->gc,
1259                         points, j + 1, CoordModeOrigin );
1260             wine_tsx11_unlock();
1261         }
1262
1263         /* Update the DIBSection of the dc's bitmap */
1264         X11DRV_UnlockDIBSection(physDev, TRUE);
1265
1266         HeapFree( GetProcessHeap(), 0, points );
1267     }
1268     return TRUE;
1269 }
1270
1271
1272 /**********************************************************************
1273  *          X11DRV_PolyPolyline
1274  */
1275 BOOL CDECL
1276 X11DRV_PolyPolyline( X11DRV_PDEVICE *physDev, const POINT* pt, const DWORD* counts, DWORD polylines )
1277 {
1278     if (X11DRV_SetupGCForPen ( physDev ))
1279     {
1280         unsigned int i, j, max = 0;
1281         XPoint *points;
1282
1283         /* Update the pixmap from the DIB section */
1284         X11DRV_LockDIBSection(physDev, DIB_Status_GdiMod);
1285
1286         for (i = 0; i < polylines; i++) if (counts[i] > max) max = counts[i];
1287         if (!(points = HeapAlloc( GetProcessHeap(), 0, sizeof(XPoint) * max )))
1288         {
1289             WARN("No memory to convert POINTs to XPoints!\n");
1290             return FALSE;
1291         }
1292         for (i = 0; i < polylines; i++)
1293         {
1294             for (j = 0; j < counts[i]; j++)
1295             {
1296                 POINT tmp = *pt;
1297                 LPtoDP(physDev->hdc, &tmp, 1);
1298                 points[j].x = physDev->dc_rect.left + tmp.x;
1299                 points[j].y = physDev->dc_rect.top + tmp.y;
1300                 pt++;
1301             }
1302             wine_tsx11_lock();
1303             XDrawLines( gdi_display, physDev->drawable, physDev->gc,
1304                         points, j, CoordModeOrigin );
1305             wine_tsx11_unlock();
1306         }
1307
1308         /* Update the DIBSection of the dc's bitmap */
1309         X11DRV_UnlockDIBSection(physDev, TRUE);
1310
1311         HeapFree( GetProcessHeap(), 0, points );
1312     }
1313     return TRUE;
1314 }
1315
1316
1317 /**********************************************************************
1318  *          X11DRV_InternalFloodFill
1319  *
1320  * Internal helper function for flood fill.
1321  * (xorg,yorg) is the origin of the X image relative to the drawable.
1322  * (x,y) is relative to the origin of the X image.
1323  */
1324 static void X11DRV_InternalFloodFill(XImage *image, X11DRV_PDEVICE *physDev,
1325                                      int x, int y,
1326                                      int xOrg, int yOrg,
1327                                      unsigned long pixel, WORD fillType )
1328 {
1329     int left, right;
1330
1331 #define TO_FLOOD(x,y)  ((fillType == FLOODFILLBORDER) ? \
1332                         (XGetPixel(image,x,y) != pixel) : \
1333                         (XGetPixel(image,x,y) == pixel))
1334
1335     if (!TO_FLOOD(x,y)) return;
1336
1337       /* Find left and right boundaries */
1338
1339     left = right = x;
1340     while ((left > 0) && TO_FLOOD( left-1, y )) left--;
1341     while ((right < image->width) && TO_FLOOD( right, y )) right++;
1342     XFillRectangle( gdi_display, physDev->drawable, physDev->gc,
1343                     xOrg + left, yOrg + y, right-left, 1 );
1344
1345       /* Set the pixels of this line so we don't fill it again */
1346
1347     for (x = left; x < right; x++)
1348     {
1349         if (fillType == FLOODFILLBORDER) XPutPixel( image, x, y, pixel );
1350         else XPutPixel( image, x, y, ~pixel );
1351     }
1352
1353       /* Fill the line above */
1354
1355     if (--y >= 0)
1356     {
1357         x = left;
1358         while (x < right)
1359         {
1360             while ((x < right) && !TO_FLOOD(x,y)) x++;
1361             if (x >= right) break;
1362             while ((x < right) && TO_FLOOD(x,y)) x++;
1363             X11DRV_InternalFloodFill(image, physDev, x-1, y,
1364                                      xOrg, yOrg, pixel, fillType );
1365         }
1366     }
1367
1368       /* Fill the line below */
1369
1370     if ((y += 2) < image->height)
1371     {
1372         x = left;
1373         while (x < right)
1374         {
1375             while ((x < right) && !TO_FLOOD(x,y)) x++;
1376             if (x >= right) break;
1377             while ((x < right) && TO_FLOOD(x,y)) x++;
1378             X11DRV_InternalFloodFill(image, physDev, x-1, y,
1379                                      xOrg, yOrg, pixel, fillType );
1380         }
1381     }
1382 #undef TO_FLOOD
1383 }
1384
1385
1386 static int ExtFloodFillXGetImageErrorHandler( Display *dpy, XErrorEvent *event, void *arg )
1387 {
1388     return (event->request_code == X_GetImage && event->error_code == BadMatch);
1389 }
1390
1391 /**********************************************************************
1392  *          X11DRV_ExtFloodFill
1393  */
1394 BOOL CDECL
1395 X11DRV_ExtFloodFill( X11DRV_PDEVICE *physDev, INT x, INT y, COLORREF color,
1396                      UINT fillType )
1397 {
1398     XImage *image;
1399     RECT rect;
1400     POINT pt;
1401
1402     TRACE("X11DRV_ExtFloodFill %d,%d %06x %d\n", x, y, color, fillType );
1403
1404     pt.x = x;
1405     pt.y = y;
1406     LPtoDP( physDev->hdc, &pt, 1 );
1407     if (!PtInRegion( physDev->region, pt.x, pt.y )) return FALSE;
1408     GetRgnBox( physDev->region, &rect );
1409
1410     X11DRV_expect_error( gdi_display, ExtFloodFillXGetImageErrorHandler, NULL );
1411     image = XGetImage( gdi_display, physDev->drawable,
1412                        physDev->dc_rect.left + rect.left, physDev->dc_rect.top + rect.top,
1413                        rect.right - rect.left, rect.bottom - rect.top,
1414                        AllPlanes, ZPixmap );
1415     if(X11DRV_check_error()) image = NULL;
1416     if (!image) return FALSE;
1417
1418     if (X11DRV_SetupGCForBrush( physDev ))
1419     {
1420         unsigned long pixel = X11DRV_PALETTE_ToPhysical( physDev, color );
1421
1422         /* Update the pixmap from the DIB section */
1423         X11DRV_LockDIBSection(physDev, DIB_Status_GdiMod);
1424
1425           /* ROP mode is always GXcopy for flood-fill */
1426         wine_tsx11_lock();
1427         XSetFunction( gdi_display, physDev->gc, GXcopy );
1428         X11DRV_InternalFloodFill(image, physDev,
1429                                  pt.x - rect.left,
1430                                  pt.y - rect.top,
1431                                  physDev->dc_rect.left + rect.left,
1432                                  physDev->dc_rect.top + rect.top,
1433                                  pixel, fillType );
1434         wine_tsx11_unlock();
1435         /* Update the DIBSection of the dc's bitmap */
1436         X11DRV_UnlockDIBSection(physDev, TRUE);
1437     }
1438
1439     wine_tsx11_lock();
1440     XDestroyImage( image );
1441     wine_tsx11_unlock();
1442     return TRUE;
1443 }
1444
1445 /**********************************************************************
1446  *          X11DRV_SetBkColor
1447  */
1448 COLORREF CDECL
1449 X11DRV_SetBkColor( X11DRV_PDEVICE *physDev, COLORREF color )
1450 {
1451     physDev->backgroundPixel = X11DRV_PALETTE_ToPhysical( physDev, color );
1452     return color;
1453 }
1454
1455 /**********************************************************************
1456  *          X11DRV_SetTextColor
1457  */
1458 COLORREF CDECL
1459 X11DRV_SetTextColor( X11DRV_PDEVICE *physDev, COLORREF color )
1460 {
1461     physDev->textPixel = X11DRV_PALETTE_ToPhysical( physDev, color );
1462     return color;
1463 }
1464
1465 /***********************************************************************
1466  *           GetDCOrgEx   (X11DRV.@)
1467  */
1468 BOOL CDECL X11DRV_GetDCOrgEx( X11DRV_PDEVICE *physDev, LPPOINT lpp )
1469 {
1470     lpp->x = physDev->dc_rect.left + physDev->drawable_rect.left;
1471     lpp->y = physDev->dc_rect.top + physDev->drawable_rect.top;
1472     return TRUE;
1473 }
1474
1475
1476 /***********************************************************************
1477  *           SetDCOrg   (X11DRV.@)
1478  */
1479 DWORD CDECL X11DRV_SetDCOrg( X11DRV_PDEVICE *physDev, INT x, INT y )
1480 {
1481     DWORD ret = MAKELONG( physDev->dc_rect.left + physDev->drawable_rect.left,
1482                           physDev->dc_rect.top + physDev->drawable_rect.top );
1483     physDev->dc_rect.left = x - physDev->drawable_rect.left;
1484     physDev->dc_rect.top = y - physDev->drawable_rect.top;
1485     return ret;
1486 }
1487
1488 static unsigned char *get_icm_profile( unsigned long *size )
1489 {
1490     Atom type;
1491     int format;
1492     unsigned long count, remaining;
1493     unsigned char *profile, *ret = NULL;
1494
1495     wine_tsx11_lock();
1496     XGetWindowProperty( gdi_display, DefaultRootWindow(gdi_display),
1497                         x11drv_atom(_ICC_PROFILE), 0, ~0UL, False, AnyPropertyType,
1498                         &type, &format, &count, &remaining, &profile );
1499     *size = get_property_size( format, count );
1500     if (format && count)
1501     {
1502         if ((ret = HeapAlloc( GetProcessHeap(), 0, *size ))) memcpy( ret, profile, *size );
1503         XFree( profile );
1504     }
1505     wine_tsx11_unlock();
1506     return ret;
1507 }
1508
1509 typedef struct
1510 {
1511     unsigned int unknown[6];
1512     unsigned int state[5];
1513     unsigned int count[2];
1514     unsigned char buffer[64];
1515 } sha_ctx;
1516
1517 extern void WINAPI A_SHAInit( sha_ctx * );
1518 extern void WINAPI A_SHAUpdate( sha_ctx *, const unsigned char *, unsigned int );
1519 extern void WINAPI A_SHAFinal( sha_ctx *, unsigned char * );
1520
1521 /***********************************************************************
1522  *              GetICMProfile (X11DRV.@)
1523  */
1524 BOOL CDECL X11DRV_GetICMProfile( X11DRV_PDEVICE *physDev, LPDWORD size, LPWSTR filename )
1525 {
1526     static const WCHAR path[] =
1527         {'\\','s','p','o','o','l','\\','d','r','i','v','e','r','s',
1528          '\\','c','o','l','o','r','\\',0};
1529     static const WCHAR srgb[] =
1530         {'s','R','G','B',' ','C','o','l','o','r',' ','S','p','a','c','e',' ',
1531          'P','r','o','f','i','l','e','.','i','c','m',0};
1532     static const WCHAR mntr[] =
1533         {'S','o','f','t','w','a','r','e','\\','M','i','c','r','o','s','o','f','t','\\',
1534          'W','i','n','d','o','w','s',' ','N','T','\\','C','u','r','r','e','n','t',
1535          'V','e','r','s','i','o','n','\\','I','C','M','\\','m','n','t','r',0};
1536
1537     HKEY hkey;
1538     DWORD required, len;
1539     WCHAR profile[MAX_PATH], fullname[2*MAX_PATH + sizeof(path)/sizeof(WCHAR)];
1540     unsigned char *buffer;
1541     unsigned long buflen;
1542
1543     if (!size) return FALSE;
1544
1545     GetSystemDirectoryW( fullname, MAX_PATH );
1546     strcatW( fullname, path );
1547
1548     len = sizeof(profile)/sizeof(WCHAR);
1549     if (!RegCreateKeyExW( HKEY_LOCAL_MACHINE, mntr, 0, NULL, 0, KEY_ALL_ACCESS, NULL, &hkey, NULL ) &&
1550         !RegEnumValueW( hkey, 0, profile, &len, NULL, NULL, NULL, NULL )) /* FIXME handle multiple values */
1551     {
1552         strcatW( fullname, profile );
1553         RegCloseKey( hkey );
1554     }
1555     else if ((buffer = get_icm_profile( &buflen )))
1556     {
1557         static const WCHAR fmt[] = {'%','0','2','x',0};
1558         static const WCHAR icm[] = {'.','i','c','m',0};
1559
1560         unsigned char sha1sum[20];
1561         unsigned int i;
1562         sha_ctx ctx;
1563         HANDLE file;
1564
1565         A_SHAInit( &ctx );
1566         A_SHAUpdate( &ctx, buffer, buflen );
1567         A_SHAFinal( &ctx, sha1sum );
1568
1569         for (i = 0; i < sizeof(sha1sum); i++) sprintfW( &profile[i * 2], fmt, sha1sum[i] );
1570         memcpy( &profile[i * 2], icm, sizeof(icm) );
1571
1572         strcatW( fullname, profile );
1573         file = CreateFileW( fullname, GENERIC_WRITE, 0, NULL, CREATE_NEW, 0, 0 );
1574         if (file != INVALID_HANDLE_VALUE)
1575         {
1576             DWORD written;
1577
1578             if (!WriteFile( file, buffer, buflen, &written, NULL ) || written != buflen)
1579                 ERR( "Unable to write color profile\n" );
1580             CloseHandle( file );
1581         }
1582         HeapFree( GetProcessHeap(), 0, buffer );
1583     }
1584     else strcatW( fullname, srgb );
1585
1586     required = strlenW( fullname ) + 1;
1587     if (*size < required)
1588     {
1589         *size = required;
1590         SetLastError( ERROR_INSUFFICIENT_BUFFER );
1591         return FALSE;
1592     }
1593     if (filename)
1594     {
1595         strcpyW( filename, fullname );
1596         if (GetFileAttributesW( filename ) == INVALID_FILE_ATTRIBUTES)
1597             WARN( "color profile not found\n" );
1598     }
1599     *size = required;
1600     return TRUE;
1601 }