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