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