Added a first-cut version of MapVirtualKeyExW() that has the same
[wine] / graphics / x11drv / brush.c
1 /*
2  * X11DRV brush objects
3  *
4  * Copyright 1993, 1994  Alexandre Julliard
5  */
6
7 #include "config.h"
8
9 #include "ts_xlib.h"
10
11 #include <stdlib.h>
12 #include "brush.h"
13 #include "bitmap.h"
14 #include "color.h"
15 #include "x11drv.h"
16 #include "debugtools.h"
17 #include "local.h"
18
19 DEFAULT_DEBUG_CHANNEL(gdi);
20
21 static const char HatchBrushes[NB_HATCH_STYLES + 1][8] =
22 {
23     { 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00 }, /* HS_HORIZONTAL */
24     { 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08 }, /* HS_VERTICAL   */
25     { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 }, /* HS_FDIAGONAL  */
26     { 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01 }, /* HS_BDIAGONAL  */
27     { 0x08, 0x08, 0x08, 0xff, 0x08, 0x08, 0x08, 0x08 }, /* HS_CROSS      */
28     { 0x81, 0x42, 0x24, 0x18, 0x18, 0x24, 0x42, 0x81 }, /* HS_DIAGCROSS  */
29     { 0xee, 0xbb, 0xee, 0xbb, 0xee, 0xbb, 0xee, 0xbb }  /* Hack for DKGRAY */
30 };
31
32   /* Levels of each primary for dithering */
33 #define PRIMARY_LEVELS  3  
34 #define TOTAL_LEVELS    (PRIMARY_LEVELS*PRIMARY_LEVELS*PRIMARY_LEVELS)
35
36  /* Dithering matrix size  */
37 #define MATRIX_SIZE     8
38 #define MATRIX_SIZE_2   (MATRIX_SIZE*MATRIX_SIZE)
39
40   /* Total number of possible levels for a dithered primary color */
41 #define DITHER_LEVELS   (MATRIX_SIZE_2 * (PRIMARY_LEVELS-1) + 1)
42
43   /* Dithering matrix */
44 static const int dither_matrix[MATRIX_SIZE_2] =
45 {
46      0, 32,  8, 40,  2, 34, 10, 42,
47     48, 16, 56, 24, 50, 18, 58, 26,
48     12, 44,  4, 36, 14, 46,  6, 38,
49     60, 28, 52, 20, 62, 30, 54, 22,
50      3, 35, 11, 43,  1, 33,  9, 41,
51     51, 19, 59, 27, 49, 17, 57, 25,
52     15, 47,  7, 39, 13, 45,  5, 37,
53     63, 31, 55, 23, 61, 29, 53, 21
54 };
55
56   /* Mapping between (R,G,B) triples and EGA colors */
57 static const int EGAmapping[TOTAL_LEVELS] =
58 {
59     0,  /* 000000 -> 000000 */
60     4,  /* 00007f -> 000080 */
61     12, /* 0000ff -> 0000ff */
62     2,  /* 007f00 -> 008000 */
63     6,  /* 007f7f -> 008080 */
64     6,  /* 007fff -> 008080 */
65     10, /* 00ff00 -> 00ff00 */
66     6,  /* 00ff7f -> 008080 */
67     14, /* 00ffff -> 00ffff */
68     1,  /* 7f0000 -> 800000 */
69     5,  /* 7f007f -> 800080 */
70     5,  /* 7f00ff -> 800080 */
71     3,  /* 7f7f00 -> 808000 */
72     8,  /* 7f7f7f -> 808080 */
73     7,  /* 7f7fff -> c0c0c0 */
74     3,  /* 7fff00 -> 808000 */
75     7,  /* 7fff7f -> c0c0c0 */
76     7,  /* 7fffff -> c0c0c0 */
77     9,  /* ff0000 -> ff0000 */
78     5,  /* ff007f -> 800080 */
79     13, /* ff00ff -> ff00ff */
80     3,  /* ff7f00 -> 808000 */
81     7,  /* ff7f7f -> c0c0c0 */
82     7,  /* ff7fff -> c0c0c0 */
83     11, /* ffff00 -> ffff00 */
84     7,  /* ffff7f -> c0c0c0 */
85     15  /* ffffff -> ffffff */
86 };
87
88 #define PIXEL_VALUE(r,g,b) \
89     X11DRV_PALETTE_mapEGAPixel[EGAmapping[((r)*PRIMARY_LEVELS+(g))*PRIMARY_LEVELS+(b)]]
90
91   /* X image for building dithered pixmap */
92 static XImage *ditherImage = NULL;
93
94
95 /***********************************************************************
96  *           BRUSH_Init
97  *
98  * Create the X image used for dithering.
99  */
100 BOOL X11DRV_BRUSH_Init(void)
101 {
102     XCREATEIMAGE( ditherImage, MATRIX_SIZE, MATRIX_SIZE, X11DRV_GetDepth() );
103     return (ditherImage != NULL);
104 }
105
106
107 /***********************************************************************
108  *           BRUSH_DitherColor
109  */
110 static Pixmap BRUSH_DitherColor( DC *dc, COLORREF color )
111 {
112     static COLORREF prevColor = 0xffffffff;
113     unsigned int x, y;
114     Pixmap pixmap;
115
116     EnterCriticalSection( &X11DRV_CritSection );
117     if (color != prevColor)
118     {
119         int r = GetRValue( color ) * DITHER_LEVELS;
120         int g = GetGValue( color ) * DITHER_LEVELS;
121         int b = GetBValue( color ) * DITHER_LEVELS;
122         const int *pmatrix = dither_matrix;
123
124         for (y = 0; y < MATRIX_SIZE; y++)
125         {
126             for (x = 0; x < MATRIX_SIZE; x++)
127             {
128                 int d  = *pmatrix++ * 256;
129                 int dr = ((r + d) / MATRIX_SIZE_2) / 256;
130                 int dg = ((g + d) / MATRIX_SIZE_2) / 256;
131                 int db = ((b + d) / MATRIX_SIZE_2) / 256;
132                 XPutPixel( ditherImage, x, y, PIXEL_VALUE(dr,dg,db) );
133             }
134         }
135         prevColor = color;
136     }
137     
138     pixmap = XCreatePixmap( display, X11DRV_GetXRootWindow(),
139                             MATRIX_SIZE, MATRIX_SIZE, X11DRV_GetDepth() );
140     XPutImage( display, pixmap, BITMAP_colorGC, ditherImage, 0, 0,
141                0, 0, MATRIX_SIZE, MATRIX_SIZE );
142     LeaveCriticalSection( &X11DRV_CritSection );
143     return pixmap;
144 }
145
146
147 /***********************************************************************
148  *           BRUSH_SelectSolidBrush
149  */
150 static void BRUSH_SelectSolidBrush( DC *dc, COLORREF color )
151 {
152     X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
153
154     if ((dc->bitsPerPixel > 1) && (X11DRV_GetDepth() <= 8) && !COLOR_IsSolid( color ))
155     {
156           /* Dithered brush */
157         physDev->brush.pixmap = BRUSH_DitherColor( dc, color );
158         physDev->brush.fillStyle = FillTiled;
159         physDev->brush.pixel = 0;
160     }
161     else
162     {
163           /* Solid brush */
164         physDev->brush.pixel = X11DRV_PALETTE_ToPhysical( dc, color );
165         physDev->brush.fillStyle = FillSolid;
166     }
167 }
168
169
170 /***********************************************************************
171  *           BRUSH_SelectPatternBrush
172  */
173 static BOOL BRUSH_SelectPatternBrush( DC * dc, HBITMAP hbitmap )
174 {
175     BOOL ret = FALSE;
176     X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
177     BITMAPOBJ * bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
178     if (!bmp) return FALSE;
179
180    if(!bmp->physBitmap)
181         if(!X11DRV_CreateBitmap(hbitmap))
182             goto done;
183
184     if(bmp->funcs != dc->funcs) {
185         WARN("Trying to select non-X11 DDB into an X11 dc\n");
186         goto done;
187     }
188
189     if ((dc->bitsPerPixel == 1) && (bmp->bitmap.bmBitsPixel != 1))
190     {
191         /* Special case: a color pattern on a monochrome DC */
192         physDev->brush.pixmap = TSXCreatePixmap( display, X11DRV_GetXRootWindow(), 8, 8, 1);
193         /* FIXME: should probably convert to monochrome instead */
194         TSXCopyPlane( display, (Pixmap)bmp->physBitmap, physDev->brush.pixmap,
195                     BITMAP_monoGC, 0, 0, 8, 8, 0, 0, 1 );
196     }
197     else
198     {
199         physDev->brush.pixmap = TSXCreatePixmap( display, X11DRV_GetXRootWindow(),
200                                                8, 8, bmp->bitmap.bmBitsPixel );
201         TSXCopyArea( display, (Pixmap)bmp->physBitmap, physDev->brush.pixmap,
202                    BITMAP_GC(bmp), 0, 0, 8, 8, 0, 0 );
203     }
204     
205     if (bmp->bitmap.bmBitsPixel > 1)
206     {
207         physDev->brush.fillStyle = FillTiled;
208         physDev->brush.pixel = 0;  /* Ignored */
209     }
210     else
211     {
212         physDev->brush.fillStyle = FillOpaqueStippled;
213         physDev->brush.pixel = -1;  /* Special case (see DC_SetupGCForBrush) */
214     }
215     ret = TRUE;
216  done:
217     GDI_ReleaseObj( hbitmap );
218     return ret;
219 }
220
221
222
223
224 /***********************************************************************
225  *           BRUSH_SelectObject
226  */
227 HBRUSH X11DRV_BRUSH_SelectObject( DC * dc, HBRUSH hbrush, BRUSHOBJ * brush )
228 {
229     HBITMAP16 hBitmap;
230     BITMAPINFO * bmpInfo;
231     HBRUSH16 prevHandle = dc->hBrush;
232     X11DRV_PDEVICE *physDev = (X11DRV_PDEVICE *)dc->physDev;
233     
234     TRACE("hdc=%04x hbrush=%04x\n",
235                 dc->hSelf,hbrush);
236
237     dc->hBrush = hbrush;
238
239     if (physDev->brush.pixmap)
240     {
241         TSXFreePixmap( display, physDev->brush.pixmap );
242         physDev->brush.pixmap = 0;
243     }
244     physDev->brush.style = brush->logbrush.lbStyle;
245     
246     switch(brush->logbrush.lbStyle)
247     {
248       case BS_NULL:
249         TRACE("BS_NULL\n" );
250         break;
251
252       case BS_SOLID:
253         TRACE("BS_SOLID\n" );
254         BRUSH_SelectSolidBrush( dc, brush->logbrush.lbColor );
255         break;
256         
257       case BS_HATCHED:
258         TRACE("BS_HATCHED\n" );
259         physDev->brush.pixel = X11DRV_PALETTE_ToPhysical( dc, brush->logbrush.lbColor );
260         physDev->brush.pixmap = TSXCreateBitmapFromData( display, X11DRV_GetXRootWindow(),
261                                  HatchBrushes[brush->logbrush.lbHatch], 8, 8 );
262         physDev->brush.fillStyle = FillStippled;
263         break;
264         
265       case BS_PATTERN:
266         TRACE("BS_PATTERN\n");
267         BRUSH_SelectPatternBrush( dc, (HBRUSH16)brush->logbrush.lbHatch );
268         break;
269
270       case BS_DIBPATTERN:
271         TRACE("BS_DIBPATTERN\n");
272         if ((bmpInfo = (BITMAPINFO *) GlobalLock16( (HGLOBAL16)brush->logbrush.lbHatch )))
273         {
274             int size = DIB_BitmapInfoSize( bmpInfo, brush->logbrush.lbColor );
275             hBitmap = CreateDIBitmap( dc->hSelf, &bmpInfo->bmiHeader,
276                                         CBM_INIT, ((char *)bmpInfo) + size,
277                                         bmpInfo,
278                                         (WORD)brush->logbrush.lbColor );
279             BRUSH_SelectPatternBrush( dc, hBitmap );
280             DeleteObject16( hBitmap );
281             GlobalUnlock16( (HGLOBAL16)brush->logbrush.lbHatch );           
282         }
283         
284         break;
285     }
286     
287     return prevHandle;
288 }