4 * Copyright 1993, 1994 Alexandre Julliard
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.
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.
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
29 #include "gdi_private.h"
30 #include "wine/debug.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(gdi);
34 /* GDI logical brush object */
39 struct brush_pattern pattern;
42 #define NB_HATCH_STYLES 6
44 static HGDIOBJ BRUSH_SelectObject( HGDIOBJ handle, HDC hdc );
45 static INT BRUSH_GetObject( HGDIOBJ handle, INT count, LPVOID buffer );
46 static BOOL BRUSH_DeleteObject( HGDIOBJ handle );
48 static const struct gdi_obj_funcs brush_funcs =
50 BRUSH_SelectObject, /* pSelectObject */
51 BRUSH_GetObject, /* pGetObjectA */
52 BRUSH_GetObject, /* pGetObjectW */
53 NULL, /* pUnrealizeObject */
54 BRUSH_DeleteObject /* pDeleteObject */
58 /* fetch the contents of the brush bitmap and cache them in the brush pattern */
59 void cache_pattern_bits( PHYSDEV physdev, struct brush_pattern *pattern )
61 const struct gdi_dc_funcs *funcs;
62 struct gdi_image_bits bits;
63 struct bitblt_coords src;
67 if (pattern->info) return; /* already cached */
68 if (!(bmp = GDI_GetObjPtr( pattern->bitmap, OBJ_BITMAP ))) return;
70 /* we don't need to cache if we are selecting into the same type of DC */
71 if (physdev && bmp->funcs == physdev->funcs) goto done;
73 if (!(info = HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET( BITMAPINFO, bmiColors[256] )))) goto done;
75 src.visrect.left = src.x = 0;
76 src.visrect.top = src.y = 0;
77 src.visrect.right = src.width = bmp->bitmap.bmWidth;
78 src.visrect.bottom = src.height = bmp->bitmap.bmHeight;
79 funcs = get_bitmap_funcs( bmp );
80 if (funcs->pGetImage( NULL, pattern->bitmap, info, &bits, &src ))
82 HeapFree( GetProcessHeap(), 0, info );
86 /* release the unneeded space */
87 HeapReAlloc( GetProcessHeap(), HEAP_REALLOC_IN_PLACE_ONLY, info,
88 get_dib_info_size( info, DIB_RGB_COLORS ));
91 pattern->usage = DIB_RGB_COLORS;
94 GDI_ReleaseObj( pattern->bitmap );
97 static BOOL copy_bitmap( struct brush_pattern *brush, HBITMAP bitmap )
100 BITMAPOBJ *bmp = GDI_GetObjPtr( bitmap, OBJ_BITMAP );
102 if (!bmp) return FALSE;
106 if ((brush->bitmap = CreateBitmap( bmp->bitmap.bmWidth, bmp->bitmap.bmHeight,
107 bmp->bitmap.bmPlanes, bmp->bitmap.bmBitsPixel, NULL )))
109 if (bmp->funcs->pCopyBitmap( bitmap, brush->bitmap ))
111 BITMAPOBJ *copy = GDI_GetObjPtr( brush->bitmap, OBJ_BITMAP );
112 copy->funcs = bmp->funcs;
113 GDI_ReleaseObj( copy );
117 DeleteObject( brush->bitmap );
121 GDI_ReleaseObj( bitmap );
122 return brush->bitmap != 0;
125 info = HeapAlloc( GetProcessHeap(), 0,
126 get_dib_info_size( (BITMAPINFO *)&bmp->dib->dsBmih, DIB_RGB_COLORS ));
127 if (!info) goto done;
128 info->bmiHeader = bmp->dib->dsBmih;
129 if (info->bmiHeader.biCompression == BI_BITFIELDS)
130 memcpy( &info->bmiHeader + 1, bmp->dib->dsBitfields, sizeof(bmp->dib->dsBitfields) );
131 else if (info->bmiHeader.biClrUsed)
132 memcpy( &info->bmiHeader + 1, bmp->color_table, info->bmiHeader.biClrUsed * sizeof(RGBQUAD) );
133 if (!(brush->bits.ptr = HeapAlloc( GetProcessHeap(), 0, info->bmiHeader.biSizeImage )))
135 HeapFree( GetProcessHeap(), 0, info );
138 memcpy( brush->bits.ptr, bmp->dib->dsBm.bmBits, info->bmiHeader.biSizeImage );
139 brush->bits.is_copy = TRUE;
140 brush->bits.free = free_heap_bits;
142 brush->usage = DIB_RGB_COLORS;
145 GDI_ReleaseObj( bitmap );
146 return brush->info != NULL;
149 BOOL store_brush_pattern( LOGBRUSH *brush, struct brush_pattern *pattern )
154 pattern->info = NULL;
155 pattern->bits.free = NULL;
157 switch (brush->lbStyle)
164 if (brush->lbHatch > HS_DIAGCROSS)
166 if (brush->lbHatch >= HS_API_MAX) return FALSE;
167 brush->lbStyle = BS_SOLID;
173 brush->lbStyle = BS_PATTERN;
177 return copy_bitmap( pattern, (HBITMAP)brush->lbHatch );
180 hmem = (HGLOBAL)brush->lbHatch;
181 if (!(brush->lbHatch = (ULONG_PTR)GlobalLock( hmem ))) return FALSE;
183 case BS_DIBPATTERNPT:
184 pattern->usage = brush->lbColor;
185 pattern->info = copy_packed_dib( (BITMAPINFO *)brush->lbHatch, pattern->usage );
186 if (hmem) GlobalUnlock( hmem );
187 if (!pattern->info) return FALSE;
188 pattern->bits.ptr = (char *)pattern->info + get_dib_info_size( pattern->info, pattern->usage );
189 brush->lbStyle = BS_DIBPATTERN;
193 case BS_DIBPATTERN8X8:
197 WARN( "invalid brush style %u\n", brush->lbStyle );
202 void free_brush_pattern( struct brush_pattern *pattern )
204 if (pattern->bits.free) pattern->bits.free( &pattern->bits );
205 if (pattern->bitmap) DeleteObject( pattern->bitmap );
206 HeapFree( GetProcessHeap(), 0, pattern->info );
209 BOOL get_brush_bitmap_info( HBRUSH handle, BITMAPINFO *info, void **bits, UINT *usage )
214 if (!(brush = GDI_GetObjPtr( handle, OBJ_BRUSH ))) return FALSE;
216 if (!brush->pattern.info) cache_pattern_bits( NULL, &brush->pattern );
218 if (brush->pattern.info)
220 memcpy( info, brush->pattern.info, get_dib_info_size( brush->pattern.info, brush->pattern.usage ));
221 if (info->bmiHeader.biBitCount <= 8 && !info->bmiHeader.biClrUsed)
222 fill_default_color_table( info );
223 *bits = brush->pattern.bits.ptr;
224 *usage = brush->pattern.usage;
227 GDI_ReleaseObj( handle );
232 /***********************************************************************
233 * CreateBrushIndirect (GDI32.@)
235 * Create a logical brush with a given style, color or pattern.
238 * brush [I] Pointer to a LOGBRUSH structure describing the desired brush.
241 * A handle to the created brush, or a NULL handle if the brush cannot be
245 * - The brush returned should be freed by the caller using DeleteObject()
246 * when it is no longer required.
247 * - Windows 95 and earlier cannot create brushes from bitmaps or DIBs larger
248 * than 8x8 pixels. If a larger bitmap is given, only a portion of the bitmap
251 HBRUSH WINAPI CreateBrushIndirect( const LOGBRUSH * brush )
256 if (!(ptr = HeapAlloc( GetProcessHeap(), 0, sizeof(*ptr) ))) return 0;
258 ptr->logbrush = *brush;
260 if (store_brush_pattern( &ptr->logbrush, &ptr->pattern ) &&
261 (hbrush = alloc_gdi_handle( &ptr->header, OBJ_BRUSH, &brush_funcs )))
263 TRACE("%p\n", hbrush);
267 free_brush_pattern( &ptr->pattern );
268 HeapFree( GetProcessHeap(), 0, ptr );
273 /***********************************************************************
274 * CreateHatchBrush (GDI32.@)
276 * Create a logical brush with a hatched pattern.
279 * style [I] Direction of lines for the hatch pattern (HS_* values from "wingdi.h")
280 * color [I] Colour of the hatched pattern
283 * A handle to the created brush, or a NULL handle if the brush cannot
287 * - This function uses CreateBrushIndirect() to create the brush.
288 * - The brush returned should be freed by the caller using DeleteObject()
289 * when it is no longer required.
291 HBRUSH WINAPI CreateHatchBrush( INT style, COLORREF color )
295 TRACE("%d %06x\n", style, color );
297 logbrush.lbStyle = BS_HATCHED;
298 logbrush.lbColor = color;
299 logbrush.lbHatch = style;
301 return CreateBrushIndirect( &logbrush );
305 /***********************************************************************
306 * CreatePatternBrush (GDI32.@)
308 * Create a logical brush with a pattern from a bitmap.
311 * hbitmap [I] Bitmap containing pattern for the brush
314 * A handle to the created brush, or a NULL handle if the brush cannot
318 * - This function uses CreateBrushIndirect() to create the brush.
319 * - The brush returned should be freed by the caller using DeleteObject()
320 * when it is no longer required.
322 HBRUSH WINAPI CreatePatternBrush( HBITMAP hbitmap )
324 LOGBRUSH logbrush = { BS_PATTERN, 0, 0 };
325 TRACE("%p\n", hbitmap );
327 logbrush.lbHatch = (ULONG_PTR)hbitmap;
328 return CreateBrushIndirect( &logbrush );
332 /***********************************************************************
333 * CreateDIBPatternBrush (GDI32.@)
335 * Create a logical brush with a pattern from a DIB.
338 * hbitmap [I] Global object containing BITMAPINFO structure for the pattern
339 * coloruse [I] Specifies color format, if provided
342 * A handle to the created brush, or a NULL handle if the brush cannot
346 * - This function uses CreateBrushIndirect() to create the brush.
347 * - The brush returned should be freed by the caller using DeleteObject()
348 * when it is no longer required.
349 * - This function is for compatibility only. CreateDIBPatternBrushPt() should
352 HBRUSH WINAPI CreateDIBPatternBrush( HGLOBAL hbitmap, UINT coloruse )
356 TRACE("%p\n", hbitmap );
358 logbrush.lbStyle = BS_DIBPATTERN;
359 logbrush.lbColor = coloruse;
361 logbrush.lbHatch = (ULONG_PTR)hbitmap;
363 return CreateBrushIndirect( &logbrush );
367 /***********************************************************************
368 * CreateDIBPatternBrushPt (GDI32.@)
370 * Create a logical brush with a pattern from a DIB.
373 * data [I] Pointer to a BITMAPINFO structure and image data for the pattern
374 * coloruse [I] Specifies color format, if provided
377 * A handle to the created brush, or a NULL handle if the brush cannot
381 * - This function uses CreateBrushIndirect() to create the brush.
382 * - The brush returned should be freed by the caller using DeleteObject()
383 * when it is no longer required.
385 HBRUSH WINAPI CreateDIBPatternBrushPt( const void* data, UINT coloruse )
387 const BITMAPINFO *info=data;
393 TRACE("%p %dx%d %dbpp\n", info, info->bmiHeader.biWidth,
394 info->bmiHeader.biHeight, info->bmiHeader.biBitCount);
396 logbrush.lbStyle = BS_DIBPATTERNPT;
397 logbrush.lbColor = coloruse;
398 logbrush.lbHatch = (ULONG_PTR)data;
400 return CreateBrushIndirect( &logbrush );
404 /***********************************************************************
405 * CreateSolidBrush (GDI32.@)
407 * Create a logical brush consisting of a single colour.
410 * color [I] Colour to make the solid brush
413 * A handle to the newly created brush, or a NULL handle if the brush cannot
417 * - This function uses CreateBrushIndirect() to create the brush.
418 * - The brush returned should be freed by the caller using DeleteObject()
419 * when it is no longer required.
421 HBRUSH WINAPI CreateSolidBrush( COLORREF color )
425 TRACE("%06x\n", color );
427 logbrush.lbStyle = BS_SOLID;
428 logbrush.lbColor = color;
429 logbrush.lbHatch = 0;
431 return CreateBrushIndirect( &logbrush );
435 /***********************************************************************
436 * SetBrushOrgEx (GDI32.@)
438 * Set the brush origin for a device context.
441 * hdc [I] Device context to set the brush origin for
444 * oldorg [O] If non NULL, destination for previously set brush origin.
447 * Success: TRUE. The origin is set to (x,y), and oldorg is updated if given.
449 BOOL WINAPI SetBrushOrgEx( HDC hdc, INT x, INT y, LPPOINT oldorg )
451 DC *dc = get_dc_ptr( hdc );
453 if (!dc) return FALSE;
456 oldorg->x = dc->brushOrgX;
457 oldorg->y = dc->brushOrgY;
461 release_dc_ptr( dc );
465 /***********************************************************************
466 * FixBrushOrgEx (GDI32.@)
471 * This function is no longer documented by MSDN, but in Win95 GDI32 it
472 * is the same as SetBrushOrgEx().
474 BOOL WINAPI FixBrushOrgEx( HDC hdc, INT x, INT y, LPPOINT oldorg )
476 return SetBrushOrgEx(hdc,x,y,oldorg);
480 /***********************************************************************
483 static HGDIOBJ BRUSH_SelectObject( HGDIOBJ handle, HDC hdc )
487 DC *dc = get_dc_ptr( hdc );
491 SetLastError( ERROR_INVALID_HANDLE );
495 if ((brush = GDI_GetObjPtr( handle, OBJ_BRUSH )))
497 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pSelectBrush );
498 struct brush_pattern *pattern = &brush->pattern;
502 if (pattern->bitmap) cache_pattern_bits( physdev, pattern );
506 GDI_inc_ref_count( handle );
507 GDI_ReleaseObj( handle );
509 if (!physdev->funcs->pSelectBrush( physdev, handle, pattern ))
511 GDI_dec_ref_count( handle );
517 GDI_dec_ref_count( ret );
520 release_dc_ptr( dc );
525 /***********************************************************************
528 static BOOL BRUSH_DeleteObject( HGDIOBJ handle )
530 BRUSHOBJ *brush = free_gdi_handle( handle );
532 if (!brush) return FALSE;
533 free_brush_pattern( &brush->pattern );
534 return HeapFree( GetProcessHeap(), 0, brush );
538 /***********************************************************************
541 static INT BRUSH_GetObject( HGDIOBJ handle, INT count, LPVOID buffer )
543 BRUSHOBJ *brush = GDI_GetObjPtr( handle, OBJ_BRUSH );
545 if (!brush) return 0;
548 if (count > sizeof(brush->logbrush)) count = sizeof(brush->logbrush);
549 memcpy( buffer, &brush->logbrush, count );
551 else count = sizeof(brush->logbrush);
552 GDI_ReleaseObj( handle );