iphlpapi: Don't rely on the HAVE_XXX macros having a numeric values.
[wine] / dlls / winex11.drv / brush.c
1 /*
2  * X11DRV brush objects
3  *
4  * Copyright 1993, 1994  Alexandre Julliard
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  */
20
21 #include "config.h"
22
23 #include <stdlib.h>
24
25 #include "wine/winbase16.h"
26 #include "x11drv.h"
27 #include "wine/debug.h"
28
29 WINE_DEFAULT_DEBUG_CHANNEL(gdi);
30
31 static const char HatchBrushes[][8] =
32 {
33     { 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00 }, /* HS_HORIZONTAL */
34     { 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08 }, /* HS_VERTICAL   */
35     { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 }, /* HS_FDIAGONAL  */
36     { 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01 }, /* HS_BDIAGONAL  */
37     { 0x08, 0x08, 0x08, 0xff, 0x08, 0x08, 0x08, 0x08 }, /* HS_CROSS      */
38     { 0x81, 0x42, 0x24, 0x18, 0x18, 0x24, 0x42, 0x81 }, /* HS_DIAGCROSS  */
39 };
40
41   /* Levels of each primary for dithering */
42 #define PRIMARY_LEVELS  3
43 #define TOTAL_LEVELS    (PRIMARY_LEVELS*PRIMARY_LEVELS*PRIMARY_LEVELS)
44
45  /* Dithering matrix size  */
46 #define MATRIX_SIZE     8
47 #define MATRIX_SIZE_2   (MATRIX_SIZE*MATRIX_SIZE)
48
49   /* Total number of possible levels for a dithered primary color */
50 #define DITHER_LEVELS   (MATRIX_SIZE_2 * (PRIMARY_LEVELS-1) + 1)
51
52   /* Dithering matrix */
53 static const int dither_matrix[MATRIX_SIZE_2] =
54 {
55      0, 32,  8, 40,  2, 34, 10, 42,
56     48, 16, 56, 24, 50, 18, 58, 26,
57     12, 44,  4, 36, 14, 46,  6, 38,
58     60, 28, 52, 20, 62, 30, 54, 22,
59      3, 35, 11, 43,  1, 33,  9, 41,
60     51, 19, 59, 27, 49, 17, 57, 25,
61     15, 47,  7, 39, 13, 45,  5, 37,
62     63, 31, 55, 23, 61, 29, 53, 21
63 };
64
65   /* Mapping between (R,G,B) triples and EGA colors */
66 static const int EGAmapping[TOTAL_LEVELS] =
67 {
68     0,  /* 000000 -> 000000 */
69     4,  /* 00007f -> 000080 */
70     12, /* 0000ff -> 0000ff */
71     2,  /* 007f00 -> 008000 */
72     6,  /* 007f7f -> 008080 */
73     6,  /* 007fff -> 008080 */
74     10, /* 00ff00 -> 00ff00 */
75     6,  /* 00ff7f -> 008080 */
76     14, /* 00ffff -> 00ffff */
77     1,  /* 7f0000 -> 800000 */
78     5,  /* 7f007f -> 800080 */
79     5,  /* 7f00ff -> 800080 */
80     3,  /* 7f7f00 -> 808000 */
81     8,  /* 7f7f7f -> 808080 */
82     7,  /* 7f7fff -> c0c0c0 */
83     3,  /* 7fff00 -> 808000 */
84     7,  /* 7fff7f -> c0c0c0 */
85     7,  /* 7fffff -> c0c0c0 */
86     9,  /* ff0000 -> ff0000 */
87     5,  /* ff007f -> 800080 */
88     13, /* ff00ff -> ff00ff */
89     3,  /* ff7f00 -> 808000 */
90     7,  /* ff7f7f -> c0c0c0 */
91     7,  /* ff7fff -> c0c0c0 */
92     11, /* ffff00 -> ffff00 */
93     7,  /* ffff7f -> c0c0c0 */
94     15  /* ffffff -> ffffff */
95 };
96
97 #define PIXEL_VALUE(r,g,b) \
98     X11DRV_PALETTE_mapEGAPixel[EGAmapping[((r)*PRIMARY_LEVELS+(g))*PRIMARY_LEVELS+(b)]]
99
100 static const COLORREF BLACK = RGB(0, 0, 0);
101 static const COLORREF WHITE = RGB(0xff, 0xff, 0xff);
102
103 /***********************************************************************
104  *           BRUSH_DitherColor
105  */
106 static Pixmap BRUSH_DitherColor( COLORREF color, int depth)
107 {
108     /* X image for building dithered pixmap */
109     static XImage *ditherImage = NULL;
110     static COLORREF prevColor = 0xffffffff;
111     unsigned int x, y;
112     Pixmap pixmap;
113     GC gc;
114
115     wine_tsx11_lock();
116     if (!ditherImage)
117     {
118         ditherImage = XCreateImage( gdi_display, visual, depth, ZPixmap, 0,
119                                     NULL, MATRIX_SIZE, MATRIX_SIZE, 32, 0 );
120         if (!ditherImage)
121         {
122             wine_tsx11_unlock();
123             ERR("Could not create dither image\n");
124             return 0;
125         }
126         ditherImage->data = HeapAlloc( GetProcessHeap(), 0,
127                                        ditherImage->height * ditherImage->bytes_per_line );
128     }
129
130     if (color != prevColor)
131     {
132         int r = GetRValue( color ) * DITHER_LEVELS;
133         int g = GetGValue( color ) * DITHER_LEVELS;
134         int b = GetBValue( color ) * DITHER_LEVELS;
135         const int *pmatrix = dither_matrix;
136
137         for (y = 0; y < MATRIX_SIZE; y++)
138         {
139             for (x = 0; x < MATRIX_SIZE; x++)
140             {
141                 int d  = *pmatrix++ * 256;
142                 int dr = ((r + d) / MATRIX_SIZE_2) / 256;
143                 int dg = ((g + d) / MATRIX_SIZE_2) / 256;
144                 int db = ((b + d) / MATRIX_SIZE_2) / 256;
145                 XPutPixel( ditherImage, x, y, PIXEL_VALUE(dr,dg,db) );
146             }
147         }
148         prevColor = color;
149     }
150
151     pixmap = XCreatePixmap( gdi_display, root_window, MATRIX_SIZE, MATRIX_SIZE, depth );
152     gc = XCreateGC( gdi_display, pixmap, 0, NULL );
153     XPutImage( gdi_display, pixmap, gc, ditherImage, 0, 0, 0, 0, MATRIX_SIZE, MATRIX_SIZE );
154     XFreeGC( gdi_display, gc );
155     wine_tsx11_unlock();
156
157     return pixmap;
158 }
159
160
161 /***********************************************************************
162  *           BRUSH_DitherMono
163  */
164 static Pixmap BRUSH_DitherMono( COLORREF color )
165 {
166     /* This makes the spray work in Win 3.11 pbrush.exe */
167     /* FIXME. Extend this basic selection of dither patterns */
168     static const char gray_dither[][2] = {{ 0x1, 0x0 }, /* DKGRAY */
169                                           { 0x2, 0x1 }, /* GRAY */
170                                           { 0x1, 0x3 }, /* LTGRAY */
171     };                                      
172     int gray = (30 * GetRValue(color) + 59 * GetGValue(color) + 11 * GetBValue(color)) / 100;
173     int idx = gray * (sizeof gray_dither/sizeof gray_dither[0] + 1)/256 - 1;
174     Pixmap pixmap;
175
176     TRACE("color=%06x -> gray=%x\n", color, gray);
177
178     wine_tsx11_lock();
179     pixmap = XCreateBitmapFromData( gdi_display, root_window, 
180                                     gray_dither[idx],
181                                     2, 2 );
182     wine_tsx11_unlock();
183     return pixmap;
184 }
185
186 /***********************************************************************
187  *           BRUSH_SelectSolidBrush
188  */
189 static void BRUSH_SelectSolidBrush( X11DRV_PDEVICE *physDev, COLORREF color )
190 {
191     COLORREF colorRGB = X11DRV_PALETTE_GetColor( physDev, color );
192     if ((physDev->depth > 1) && (screen_depth <= 8) && !X11DRV_IsSolidColor( color ))
193     {
194           /* Dithered brush */
195         physDev->brush.pixmap = BRUSH_DitherColor( colorRGB, physDev->depth );
196         physDev->brush.fillStyle = FillTiled;
197         physDev->brush.pixel = 0;
198     }
199     else if (physDev->depth == 1 && colorRGB != WHITE && colorRGB != BLACK)
200     {
201         physDev->brush.pixel = 0;
202         physDev->brush.pixmap = BRUSH_DitherMono( colorRGB );
203         physDev->brush.fillStyle = FillTiled;
204     }
205     else
206     {
207           /* Solid brush */
208         physDev->brush.pixel = X11DRV_PALETTE_ToPhysical( physDev, color );
209         physDev->brush.fillStyle = FillSolid;
210     }
211 }
212
213
214 static BOOL select_pattern_brush( X11DRV_PDEVICE *physdev, const struct brush_pattern *pattern )
215 {
216     XVisualInfo vis;
217     Pixmap pixmap;
218     const BITMAPINFO *info = pattern->info;
219
220     memset( &vis, 0, sizeof(vis) );
221     vis.visual     = visual;
222     vis.visualid   = visual->visualid;
223
224     if (physdev->depth > 1 && info->bmiHeader.biBitCount > 1)
225     {
226         vis.depth      = screen_depth;
227         vis.red_mask   = visual->red_mask;
228         vis.green_mask = visual->green_mask;
229         vis.blue_mask  = visual->blue_mask;
230     }
231     else vis.depth = 1;
232
233     pixmap = create_pixmap_from_image( physdev->dev.hdc, &vis, info, &pattern->bits, pattern->usage );
234     if (!pixmap) return FALSE;
235
236     wine_tsx11_lock();
237     if (physdev->brush.pixmap) XFreePixmap( gdi_display, physdev->brush.pixmap );
238     physdev->brush.pixmap = pixmap;
239
240     if (vis.depth == 1)
241     {
242         physdev->brush.fillStyle = FillOpaqueStippled;
243         physdev->brush.pixel = -1;  /* Special case (see DC_SetupGCForBrush) */
244     }
245     else
246     {
247         physdev->brush.fillStyle = FillTiled;
248         physdev->brush.pixel = 0;  /* Ignored */
249     }
250     wine_tsx11_unlock();
251     return TRUE;
252 }
253
254 /***********************************************************************
255  *           SelectBrush   (X11DRV.@)
256  */
257 HBRUSH X11DRV_SelectBrush( PHYSDEV dev, HBRUSH hbrush, const struct brush_pattern *pattern )
258 {
259     X11DRV_PDEVICE *physDev = get_x11drv_dev( dev );
260     LOGBRUSH logbrush;
261
262     if (pattern)  /* pattern brush */
263     {
264         if (!select_pattern_brush( physDev, pattern )) return 0;
265         TRACE("BS_PATTERN\n");
266         physDev->brush.style = BS_PATTERN;
267         return hbrush;
268     }
269
270     if (!GetObjectA( hbrush, sizeof(logbrush), &logbrush )) return 0;
271
272     TRACE("hdc=%p hbrush=%p\n", dev->hdc, hbrush);
273
274     if (physDev->brush.pixmap)
275     {
276         wine_tsx11_lock();
277         XFreePixmap( gdi_display, physDev->brush.pixmap );
278         wine_tsx11_unlock();
279         physDev->brush.pixmap = 0;
280     }
281     physDev->brush.style = logbrush.lbStyle;
282     if (hbrush == GetStockObject( DC_BRUSH ))
283         logbrush.lbColor = GetDCBrushColor( dev->hdc );
284
285     switch(logbrush.lbStyle)
286     {
287       case BS_NULL:
288         TRACE("BS_NULL\n" );
289         break;
290
291       case BS_SOLID:
292         TRACE("BS_SOLID\n" );
293         BRUSH_SelectSolidBrush( physDev, logbrush.lbColor );
294         break;
295
296       case BS_HATCHED:
297         TRACE("BS_HATCHED\n" );
298         physDev->brush.pixel = X11DRV_PALETTE_ToPhysical( physDev, logbrush.lbColor );
299         wine_tsx11_lock();
300         physDev->brush.pixmap = XCreateBitmapFromData( gdi_display, root_window,
301                                                        HatchBrushes[logbrush.lbHatch], 8, 8 );
302         wine_tsx11_unlock();
303         physDev->brush.fillStyle = FillStippled;
304         break;
305     }
306     return hbrush;
307 }
308
309
310 /***********************************************************************
311  *           SetDCBrushColor (X11DRV.@)
312  */
313 COLORREF X11DRV_SetDCBrushColor( PHYSDEV dev, COLORREF crColor )
314 {
315     X11DRV_PDEVICE *physDev = get_x11drv_dev( dev );
316
317     if (GetCurrentObject(dev->hdc, OBJ_BRUSH) == GetStockObject( DC_BRUSH ))
318         BRUSH_SelectSolidBrush( physDev, crColor );
319
320     return crColor;
321 }