Release 940714
[wine/multimedia.git] / objects / brush.c
blob397983646302cea36f7d71791df15e5072f4ad98
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;
146 /***********************************************************************
147 * GetSysColorBrush (USER.281)
149 WORD GetSysColorBrush(WORD x)
151 return GetStockObject(GRAY_BRUSH);
154 /***********************************************************************
155 * BRUSH_DeleteObject
157 BOOL BRUSH_DeleteObject( HBRUSH hbrush, BRUSHOBJ * brush )
159 switch(brush->logbrush.lbStyle)
161 case BS_PATTERN:
162 DeleteObject( brush->logbrush.lbHatch );
163 break;
164 case BS_DIBPATTERN:
165 GlobalFree( brush->logbrush.lbHatch );
166 break;
168 return GDI_FreeObject( hbrush );
172 /***********************************************************************
173 * BRUSH_GetObject
175 int BRUSH_GetObject( BRUSHOBJ * brush, int count, LPSTR buffer )
177 if (count > sizeof(LOGBRUSH)) count = sizeof(LOGBRUSH);
178 memcpy( buffer, &brush->logbrush, count );
179 return count;
183 /***********************************************************************
184 * BRUSH_MakeSolidBrush
186 static void BRUSH_SelectSolidBrush( DC *dc, COLORREF color )
188 if ((dc->w.bitsPerPixel > 1) && !COLOR_IsSolid( color ))
190 /* Dithered brush */
191 dc->u.x.brush.pixmap = DITHER_DitherColor( dc, color );
192 dc->u.x.brush.fillStyle = FillTiled;
193 dc->u.x.brush.pixel = 0;
195 else
197 /* Solid brush */
198 dc->u.x.brush.pixel = COLOR_ToPhysical( dc, color );
199 dc->u.x.brush.fillStyle = FillSolid;
204 /***********************************************************************
205 * BRUSH_SelectPatternBrush
207 static BOOL BRUSH_SelectPatternBrush( DC * dc, HBITMAP hbitmap )
209 BITMAPOBJ * bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC );
210 if (!bmp) return FALSE;
211 dc->u.x.brush.pixmap = XCreatePixmap( display, rootWindow,
212 8, 8, bmp->bitmap.bmBitsPixel );
213 XCopyArea( display, bmp->pixmap, dc->u.x.brush.pixmap,
214 BITMAP_GC(bmp), 0, 0, 8, 8, 0, 0 );
216 if (bmp->bitmap.bmBitsPixel > 1)
218 dc->u.x.brush.fillStyle = FillTiled;
219 dc->u.x.brush.pixel = 0; /* Ignored */
221 else
223 dc->u.x.brush.fillStyle = FillOpaqueStippled;
224 dc->u.x.brush.pixel = -1; /* Special case (see DC_SetupGCForBrush) */
226 return TRUE;
231 /***********************************************************************
232 * BRUSH_SelectObject
234 HBRUSH BRUSH_SelectObject( HDC hdc, DC * dc, HBRUSH hbrush, BRUSHOBJ * brush )
236 HBITMAP hBitmap;
237 BITMAPINFO * bmpInfo;
238 HBRUSH prevHandle = dc->w.hBrush;
240 if (dc->header.wMagic == METAFILE_DC_MAGIC)
242 switch (brush->logbrush.lbStyle)
244 case BS_SOLID:
245 case BS_HATCHED:
246 case BS_HOLLOW:
247 if (!MF_CreateBrushIndirect(dc, hbrush, &(brush->logbrush)))
248 return 0;
249 break;
251 case BS_PATTERN:
252 case BS_DIBPATTERN:
253 if (!MF_CreatePatternBrush(dc, hbrush, &(brush->logbrush)))
254 return 0;
255 break;
257 return 1;
260 dc->w.hBrush = hbrush;
262 if (dc->u.x.brush.pixmap)
264 XFreePixmap( display, dc->u.x.brush.pixmap );
265 dc->u.x.brush.pixmap = 0;
267 dc->u.x.brush.style = brush->logbrush.lbStyle;
269 switch(brush->logbrush.lbStyle)
271 case BS_NULL:
272 break;
274 case BS_SOLID:
275 BRUSH_SelectSolidBrush( dc, brush->logbrush.lbColor );
276 break;
278 case BS_HATCHED:
279 dc->u.x.brush.pixel = COLOR_ToPhysical( dc, brush->logbrush.lbColor );
280 dc->u.x.brush.pixmap = XCreateBitmapFromData( display, rootWindow,
281 HatchBrushes[brush->logbrush.lbHatch], 8, 8 );
282 dc->u.x.brush.fillStyle = FillStippled;
283 break;
285 case BS_PATTERN:
286 BRUSH_SelectPatternBrush( dc, brush->logbrush.lbHatch );
287 break;
289 case BS_DIBPATTERN:
290 if ((bmpInfo = (BITMAPINFO *) GlobalLock( brush->logbrush.lbHatch )))
292 int size = DIB_BitmapInfoSize( bmpInfo, brush->logbrush.lbColor );
293 hBitmap = CreateDIBitmap( hdc, &bmpInfo->bmiHeader, CBM_INIT,
294 ((char *)bmpInfo) + size, bmpInfo,
295 (WORD) brush->logbrush.lbColor );
296 BRUSH_SelectPatternBrush( dc, hBitmap );
297 DeleteObject( hBitmap );
298 GlobalUnlock( brush->logbrush.lbHatch );
301 break;
304 return prevHandle;