Release 940518
[wine/multimedia.git] / objects / brush.c
blob563617fbfb597045e35551964bb654bc85f10640
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"
12 #include "metafile.h"
15 #define NB_HATCH_STYLES 6
17 static char HatchBrushes[NB_HATCH_STYLES][8] =
19 { 0x00, 0x00, 0x00, 0xff, 0x00, 0x00, 0x00, 0x00 }, /* HS_HORIZONTAL */
20 { 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08, 0x08 }, /* HS_VERTICAL */
21 { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 }, /* HS_FDIAGONAL */
22 { 0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01 }, /* HS_BDIAGONAL */
23 { 0x08, 0x08, 0x08, 0xff, 0x08, 0x08, 0x08, 0x08 }, /* HS_CROSS */
24 { 0x81, 0x42, 0x24, 0x18, 0x18, 0x24, 0x42, 0x81 } /* HS_DIAGCROSS */
27 extern WORD COLOR_ToPhysical( DC *dc, COLORREF color );
30 /***********************************************************************
31 * CreateBrushIndirect (GDI.50)
33 HBRUSH CreateBrushIndirect( LOGBRUSH * brush )
35 BRUSHOBJ * brushPtr;
36 HBRUSH hbrush = GDI_AllocObject( sizeof(BRUSHOBJ), BRUSH_MAGIC );
37 if (!hbrush) return 0;
38 brushPtr = (BRUSHOBJ *) GDI_HEAP_ADDR( hbrush );
39 memcpy( &brushPtr->logbrush, brush, sizeof(LOGBRUSH) );
40 return hbrush;
44 /***********************************************************************
45 * CreateHatchBrush (GDI.58)
47 HBRUSH CreateHatchBrush( short style, COLORREF color )
49 LOGBRUSH logbrush = { BS_HATCHED, color, style };
50 #ifdef DEBUG_GDI
51 printf( "CreateHatchBrush: %d %06x\n", style, color );
52 #endif
53 if ((style < 0) || (style >= NB_HATCH_STYLES)) return 0;
54 return CreateBrushIndirect( &logbrush );
58 /***********************************************************************
59 * CreatePatternBrush (GDI.60)
61 HBRUSH CreatePatternBrush( HBITMAP hbitmap )
63 LOGBRUSH logbrush = { BS_PATTERN, 0, 0 };
64 BITMAPOBJ *bmp, *newbmp;
66 #ifdef DEBUG_GDI
67 printf( "CreatePatternBrush: %d\n", hbitmap );
68 #endif
70 /* Make a copy of the bitmap */
72 if (!(bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC )))
73 return 0;
74 logbrush.lbHatch = CreateBitmapIndirect( &bmp->bitmap );
75 newbmp = (BITMAPOBJ *) GDI_GetObjPtr( logbrush.lbHatch, BITMAP_MAGIC );
76 if (!newbmp) return 0;
77 XCopyArea( display, bmp->pixmap, newbmp->pixmap, BITMAP_GC(bmp),
78 0, 0, bmp->bitmap.bmWidth, bmp->bitmap.bmHeight, 0, 0 );
79 return CreateBrushIndirect( &logbrush );
83 /***********************************************************************
84 * CreateDIBPatternBrush (GDI.445)
86 HBRUSH CreateDIBPatternBrush( HANDLE hbitmap, WORD coloruse )
88 LOGBRUSH logbrush = { BS_DIBPATTERN, coloruse, 0 };
89 BITMAPINFO *info, *newInfo;
90 int size;
92 #ifdef DEBUG_GDI
93 printf( "CreateDIBPatternBrush: %d\n", hbitmap );
94 #endif
96 /* Make a copy of the bitmap */
98 if (!(info = (BITMAPINFO *) GlobalLock( hbitmap ))) return 0;
100 size = info->bmiHeader.biSizeImage;
101 if (!size)
102 size = (info->bmiHeader.biWidth * info->bmiHeader.biBitCount + 31) / 32
103 * 8 * info->bmiHeader.biHeight;
104 size += DIB_BitmapInfoSize( info, coloruse );
106 if (!(logbrush.lbHatch = GlobalAlloc( GMEM_MOVEABLE, size )))
108 GlobalUnlock( hbitmap );
109 return 0;
111 newInfo = (BITMAPINFO *) GlobalLock( logbrush.lbHatch );
112 memcpy( newInfo, info, size );
113 GlobalUnlock( logbrush.lbHatch );
114 GlobalUnlock( hbitmap );
115 return CreateBrushIndirect( &logbrush );
119 /***********************************************************************
120 * CreateSolidBrush (GDI.66)
122 HBRUSH CreateSolidBrush( COLORREF color )
124 LOGBRUSH logbrush = { BS_SOLID, color, 0 };
125 #ifdef DEBUG_GDI
126 printf( "CreateSolidBrush: %06x\n", color );
127 #endif
128 return CreateBrushIndirect( &logbrush );
132 /***********************************************************************
133 * SetBrushOrg (GDI.148)
135 DWORD SetBrushOrg( HDC hdc, short x, short y )
137 DWORD retval;
138 DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
139 if (!dc) return FALSE;
140 retval = dc->w.brushOrgX | (dc->w.brushOrgY << 16);
141 dc->w.brushOrgX = x;
142 dc->w.brushOrgY = y;
143 return retval;
147 /***********************************************************************
148 * BRUSH_DeleteObject
150 BOOL BRUSH_DeleteObject( HBRUSH hbrush, BRUSHOBJ * brush )
152 switch(brush->logbrush.lbStyle)
154 case BS_PATTERN:
155 DeleteObject( brush->logbrush.lbHatch );
156 break;
157 case BS_DIBPATTERN:
158 GlobalFree( brush->logbrush.lbHatch );
159 break;
161 return GDI_FreeObject( hbrush );
165 /***********************************************************************
166 * BRUSH_GetObject
168 int BRUSH_GetObject( BRUSHOBJ * brush, int count, LPSTR buffer )
170 if (count > sizeof(LOGBRUSH)) count = sizeof(LOGBRUSH);
171 memcpy( buffer, &brush->logbrush, count );
172 return count;
176 /***********************************************************************
177 * BRUSH_MakeSolidBrush
179 static void BRUSH_SelectSolidBrush( DC *dc, COLORREF color )
181 if ((dc->w.bitsPerPixel > 1) && !COLOR_IsSolid( color ))
183 /* Dithered brush */
184 dc->u.x.brush.pixmap = DITHER_DitherColor( dc, color );
185 dc->u.x.brush.fillStyle = FillTiled;
186 dc->u.x.brush.pixel = 0;
188 else
190 /* Solid brush */
191 dc->u.x.brush.pixel = COLOR_ToPhysical( dc, color );
192 dc->u.x.brush.fillStyle = FillSolid;
197 /***********************************************************************
198 * BRUSH_SelectPatternBrush
200 static BOOL BRUSH_SelectPatternBrush( DC * dc, HBITMAP hbitmap )
202 BITMAPOBJ * bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
203 if (!bmp) return FALSE;
204 dc->u.x.brush.pixmap = XCreatePixmap( display, rootWindow,
205 8, 8, bmp->bitmap.bmBitsPixel );
206 XCopyArea( display, bmp->pixmap, dc->u.x.brush.pixmap,
207 BITMAP_GC(bmp), 0, 0, 8, 8, 0, 0 );
209 if (bmp->bitmap.bmBitsPixel > 1)
211 dc->u.x.brush.fillStyle = FillTiled;
212 dc->u.x.brush.pixel = 0; /* Ignored */
214 else
216 dc->u.x.brush.fillStyle = FillOpaqueStippled;
217 dc->u.x.brush.pixel = -1; /* Special case (see DC_SetupGCForBrush) */
219 return TRUE;
224 /***********************************************************************
225 * BRUSH_SelectObject
227 HBRUSH BRUSH_SelectObject( HDC hdc, DC * dc, HBRUSH hbrush, BRUSHOBJ * brush )
229 HBITMAP hBitmap;
230 BITMAPINFO * bmpInfo;
231 HBRUSH prevHandle = dc->w.hBrush;
233 if (dc->header.wMagic == METAFILE_DC_MAGIC)
235 switch (brush->logbrush.lbStyle)
237 case BS_SOLID:
238 case BS_HATCHED:
239 case BS_HOLLOW:
240 if (!MF_CreateBrushIndirect(dc, &(brush->logbrush)))
241 return 0;
242 break;
244 case BS_PATTERN:
245 case BS_DIBPATTERN:
246 if (!MF_CreatePatternBrush(dc, &(brush->logbrush)))
247 return 0;
248 break;
250 return 1;
253 dc->w.hBrush = hbrush;
255 if (dc->u.x.brush.pixmap)
257 XFreePixmap( display, dc->u.x.brush.pixmap );
258 dc->u.x.brush.pixmap = 0;
260 dc->u.x.brush.style = brush->logbrush.lbStyle;
262 switch(brush->logbrush.lbStyle)
264 case BS_NULL:
265 break;
267 case BS_SOLID:
268 BRUSH_SelectSolidBrush( dc, brush->logbrush.lbColor );
269 break;
271 case BS_HATCHED:
272 dc->u.x.brush.pixel = COLOR_ToPhysical( dc, brush->logbrush.lbColor );
273 dc->u.x.brush.pixmap = XCreateBitmapFromData( display, rootWindow,
274 HatchBrushes[brush->logbrush.lbHatch], 8, 8 );
275 dc->u.x.brush.fillStyle = FillStippled;
276 break;
278 case BS_PATTERN:
279 BRUSH_SelectPatternBrush( dc, brush->logbrush.lbHatch );
280 break;
282 case BS_DIBPATTERN:
283 if ((bmpInfo = (BITMAPINFO *) GlobalLock( brush->logbrush.lbHatch )))
285 int size = DIB_BitmapInfoSize( bmpInfo, brush->logbrush.lbColor );
286 hBitmap = CreateDIBitmap( hdc, &bmpInfo->bmiHeader, CBM_INIT,
287 ((char *)bmpInfo) + size, bmpInfo,
288 (WORD) brush->logbrush.lbColor );
289 BRUSH_SelectPatternBrush( dc, hBitmap );
290 DeleteObject( hBitmap );
291 GlobalUnlock( brush->logbrush.lbHatch );
294 break;
297 return prevHandle;