Release 940405
[wine.git] / objects / brush.c
blob94641842cc272698531efbe29f7779183bc3a221
1 /*
2 * GDI brush objects
4 * Copyright 1993 Alexandre Julliard
5 */
7 static char Copyright[] = "Copyright Alexandre Julliard, 1993";
9 #include "gdi.h"
10 #include "bitmap.h"
11 #include "prototypes.h"
14 #define NB_HATCH_STYLES 6
16 static char HatchBrushes[NB_HATCH_STYLES][8] =
18 { 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00 }, /* HS_HORIZONTAL */
19 { 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08 }, /* HS_VERTICAL */
20 { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 }, /* HS_FDIAGONAL */
21 { 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01 }, /* HS_BDIAGONAL */
22 { 0x08, 0x08, 0x08, 0xff, 0x08, 0x08, 0x08, 0x08 }, /* HS_CROSS */
23 { 0x81, 0x42, 0x24, 0x18, 0x18, 0x24, 0x42, 0x81 } /* HS_DIAGCROSS */
26 extern WORD COLOR_ToPhysical( DC *dc, COLORREF color );
29 /***********************************************************************
30 * CreateBrushIndirect (GDI.50)
32 HBRUSH CreateBrushIndirect( LOGBRUSH * brush )
34 BRUSHOBJ * brushPtr;
35 HBRUSH hbrush = GDI_AllocObject( sizeof(BRUSHOBJ), BRUSH_MAGIC );
36 if (!hbrush) return 0;
37 brushPtr = (BRUSHOBJ *) GDI_HEAP_ADDR( hbrush );
38 memcpy( &brushPtr->logbrush, brush, sizeof(LOGBRUSH) );
39 return hbrush;
43 /***********************************************************************
44 * CreateHatchBrush (GDI.58)
46 HBRUSH CreateHatchBrush( short style, COLORREF color )
48 LOGBRUSH logbrush = { BS_HATCHED, color, style };
49 #ifdef DEBUG_GDI
50 printf( "CreateHatchBrush: %d %06x\n", style, color );
51 #endif
52 if ((style < 0) || (style >= NB_HATCH_STYLES)) return 0;
53 return CreateBrushIndirect( &logbrush );
57 /***********************************************************************
58 * CreatePatternBrush (GDI.60)
60 HBRUSH CreatePatternBrush( HBITMAP hbitmap )
62 LOGBRUSH logbrush = { BS_PATTERN, 0, 0 };
63 BITMAPOBJ *bmp, *newbmp;
65 #ifdef DEBUG_GDI
66 printf( "CreatePatternBrush: %d\n", hbitmap );
67 #endif
69 /* Make a copy of the bitmap */
71 if (!(bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC )))
72 return 0;
73 logbrush.lbHatch = CreateBitmapIndirect( &bmp->bitmap );
74 newbmp = (BITMAPOBJ *) GDI_GetObjPtr( logbrush.lbHatch, BITMAP_MAGIC );
75 if (!newbmp) return 0;
76 XCopyArea( display, bmp->pixmap, newbmp->pixmap, BITMAP_GC(bmp),
77 0, 0, bmp->bitmap.bmWidth, bmp->bitmap.bmHeight, 0, 0 );
78 return CreateBrushIndirect( &logbrush );
82 /***********************************************************************
83 * CreateDIBPatternBrush (GDI.445)
85 HBRUSH CreateDIBPatternBrush( HANDLE hbitmap, WORD coloruse )
87 LOGBRUSH logbrush = { BS_DIBPATTERN, coloruse, 0 };
88 BITMAPINFO *info, *newInfo;
89 int size;
91 #ifdef DEBUG_GDI
92 printf( "CreateDIBPatternBrush: %d\n", hbitmap );
93 #endif
95 /* Make a copy of the bitmap */
97 if (!(info = (BITMAPINFO *) GlobalLock( hbitmap ))) return 0;
99 size = info->bmiHeader.biSizeImage;
100 if (!size)
101 size = (info->bmiHeader.biWidth * info->bmiHeader.biBitCount + 31) / 32
102 * 8 * info->bmiHeader.biHeight;
103 size += DIB_BitmapInfoSize( info, coloruse );
105 if (!(logbrush.lbHatch = GlobalAlloc( GMEM_MOVEABLE, size )))
107 GlobalUnlock( hbitmap );
108 return 0;
110 newInfo = (BITMAPINFO *) GlobalLock( logbrush.lbHatch );
111 memcpy( newInfo, info, size );
112 GlobalUnlock( logbrush.lbHatch );
113 GlobalUnlock( hbitmap );
114 return CreateBrushIndirect( &logbrush );
118 /***********************************************************************
119 * CreateSolidBrush (GDI.66)
121 HBRUSH CreateSolidBrush( COLORREF color )
123 LOGBRUSH logbrush = { BS_SOLID, color, 0 };
124 #ifdef DEBUG_GDI
125 printf( "CreateSolidBrush: %06x\n", color );
126 #endif
127 return CreateBrushIndirect( &logbrush );
131 /***********************************************************************
132 * SetBrushOrg (GDI.148)
134 DWORD SetBrushOrg( HDC hdc, short x, short y )
136 DWORD retval;
137 DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
138 if (!dc) return FALSE;
139 retval = dc->w.brushOrgX | (dc->w.brushOrgY << 16);
140 dc->w.brushOrgX = x;
141 dc->w.brushOrgY = y;
142 return retval;
146 /***********************************************************************
147 * BRUSH_DeleteObject
149 BOOL BRUSH_DeleteObject( HBRUSH hbrush, BRUSHOBJ * brush )
151 switch(brush->logbrush.lbStyle)
153 case BS_PATTERN:
154 DeleteObject( brush->logbrush.lbHatch );
155 break;
156 case BS_DIBPATTERN:
157 GlobalFree( brush->logbrush.lbHatch );
158 break;
160 return GDI_FreeObject( hbrush );
164 /***********************************************************************
165 * BRUSH_GetObject
167 int BRUSH_GetObject( BRUSHOBJ * brush, int count, LPSTR buffer )
169 if (count > sizeof(LOGBRUSH)) count = sizeof(LOGBRUSH);
170 memcpy( buffer, &brush->logbrush, count );
171 return count;
175 /***********************************************************************
176 * BRUSH_MakeSolidBrush
178 static void BRUSH_SelectSolidBrush( DC *dc, COLORREF color )
180 if ((dc->w.bitsPerPixel > 1) && !COLOR_IsSolid( color ))
182 /* Dithered brush */
183 dc->u.x.brush.pixmap = DITHER_DitherColor( dc, color );
184 dc->u.x.brush.fillStyle = FillTiled;
185 dc->u.x.brush.pixel = 0;
187 else
189 /* Solid brush */
190 dc->u.x.brush.pixel = COLOR_ToPhysical( dc, color );
191 dc->u.x.brush.fillStyle = FillSolid;
196 /***********************************************************************
197 * BRUSH_SelectPatternBrush
199 static BOOL BRUSH_SelectPatternBrush( DC * dc, HBITMAP hbitmap )
201 BITMAPOBJ * bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
202 if (!bmp) return FALSE;
203 dc->u.x.brush.pixmap = XCreatePixmap( display, rootWindow,
204 8, 8, bmp->bitmap.bmBitsPixel );
205 XCopyArea( display, bmp->pixmap, dc->u.x.brush.pixmap,
206 BITMAP_GC(bmp), 0, 0, 8, 8, 0, 0 );
208 if (bmp->bitmap.bmBitsPixel > 1)
210 dc->u.x.brush.fillStyle = FillTiled;
211 dc->u.x.brush.pixel = 0; /* Ignored */
213 else
215 dc->u.x.brush.fillStyle = FillOpaqueStippled;
216 dc->u.x.brush.pixel = -1; /* Special case (see DC_SetupGCForBrush) */
218 return TRUE;
223 /***********************************************************************
224 * BRUSH_SelectObject
226 HBRUSH BRUSH_SelectObject( HDC hdc, DC * dc, HBRUSH hbrush, BRUSHOBJ * brush )
228 HBITMAP hBitmap;
229 BITMAPINFO * bmpInfo;
230 HBRUSH prevHandle = dc->w.hBrush;
232 dc->w.hBrush = hbrush;
234 if (dc->u.x.brush.pixmap)
236 XFreePixmap( display, dc->u.x.brush.pixmap );
237 dc->u.x.brush.pixmap = 0;
239 dc->u.x.brush.style = brush->logbrush.lbStyle;
241 switch(brush->logbrush.lbStyle)
243 case BS_NULL:
244 break;
246 case BS_SOLID:
247 BRUSH_SelectSolidBrush( dc, brush->logbrush.lbColor );
248 break;
250 case BS_HATCHED:
251 dc->u.x.brush.pixel = COLOR_ToPhysical( dc, brush->logbrush.lbColor );
252 dc->u.x.brush.pixmap = XCreateBitmapFromData( display, rootWindow,
253 HatchBrushes[brush->logbrush.lbHatch], 8, 8 );
254 dc->u.x.brush.fillStyle = FillStippled;
255 break;
257 case BS_PATTERN:
258 BRUSH_SelectPatternBrush( dc, brush->logbrush.lbHatch );
259 break;
261 case BS_DIBPATTERN:
262 if ((bmpInfo = (BITMAPINFO *) GlobalLock( brush->logbrush.lbHatch )))
264 int size = DIB_BitmapInfoSize( bmpInfo, brush->logbrush.lbColor );
265 hBitmap = CreateDIBitmap( hdc, &bmpInfo->bmiHeader, CBM_INIT,
266 ((char *)bmpInfo) + size, bmpInfo,
267 (WORD) brush->logbrush.lbColor );
268 BRUSH_SelectPatternBrush( dc, hBitmap );
269 DeleteObject( hBitmap );
270 GlobalUnlock( brush->logbrush.lbHatch );
273 break;
276 return prevHandle;