gdi32: Add a structure to store all the extra information needed for a pattern brush.
[wine/multimedia.git] / dlls / gdi32 / brush.c
blob5dc7dfebf3b7cc26ddf263360982a5724fb3394d
1 /*
2 * GDI brush objects
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
21 #include "config.h"
23 #include <stdarg.h>
24 #include <string.h>
26 #include "windef.h"
27 #include "winbase.h"
28 #include "wingdi.h"
29 #include "gdi_private.h"
30 #include "wine/debug.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(gdi);
34 /* GDI logical brush object */
35 typedef struct
37 GDIOBJHDR header;
38 LOGBRUSH logbrush;
39 struct brush_pattern pattern;
40 } BRUSHOBJ;
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 static BOOL store_bitmap_bits( struct brush_pattern *brush, BITMAPOBJ *bmp )
61 const struct gdi_dc_funcs *funcs = get_bitmap_funcs( bmp );
62 struct gdi_image_bits bits;
63 struct bitblt_coords src;
64 BITMAPINFO *info;
66 if (!(info = HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET( BITMAPINFO, bmiColors[256] ))))
67 return FALSE;
69 src.visrect.left = src.x = 0;
70 src.visrect.top = src.y = 0;
71 src.visrect.right = src.width = bmp->bitmap.bmWidth;
72 src.visrect.bottom = src.height = bmp->bitmap.bmHeight;
73 if (funcs->pGetImage( NULL, brush->bitmap, info, &bits, &src ))
75 HeapFree( GetProcessHeap(), 0, info );
76 return FALSE;
79 /* release the unneeded space */
80 HeapReAlloc( GetProcessHeap(), HEAP_REALLOC_IN_PLACE_ONLY, info,
81 get_dib_info_size( info, DIB_RGB_COLORS ));
82 brush->info = info;
83 brush->bits = bits;
84 brush->usage = DIB_RGB_COLORS;
85 return TRUE;
88 static BOOL copy_bitmap( struct brush_pattern *brush, HBITMAP bitmap )
90 BITMAPINFO *info;
91 BITMAPOBJ *bmp = GDI_GetObjPtr( bitmap, OBJ_BITMAP );
93 if (!bmp) return FALSE;
95 if (!bmp->dib)
97 if ((brush->bitmap = CreateBitmap( bmp->bitmap.bmWidth, bmp->bitmap.bmHeight,
98 bmp->bitmap.bmPlanes, bmp->bitmap.bmBitsPixel, NULL )))
100 if (bmp->funcs->pCopyBitmap( bitmap, brush->bitmap ))
102 BITMAPOBJ *copy = GDI_GetObjPtr( brush->bitmap, OBJ_BITMAP );
103 copy->funcs = bmp->funcs;
104 GDI_ReleaseObj( copy );
106 else
108 DeleteObject( brush->bitmap );
109 brush->bitmap = 0;
112 GDI_ReleaseObj( bitmap );
113 return brush->bitmap != 0;
116 info = HeapAlloc( GetProcessHeap(), 0,
117 get_dib_info_size( (BITMAPINFO *)&bmp->dib->dsBmih, DIB_RGB_COLORS ));
118 if (!info) goto done;
119 info->bmiHeader = bmp->dib->dsBmih;
120 if (info->bmiHeader.biCompression == BI_BITFIELDS)
121 memcpy( &info->bmiHeader + 1, bmp->dib->dsBitfields, sizeof(bmp->dib->dsBitfields) );
122 else if (info->bmiHeader.biClrUsed)
123 memcpy( &info->bmiHeader + 1, bmp->color_table, info->bmiHeader.biClrUsed * sizeof(RGBQUAD) );
124 if (!(brush->bits.ptr = HeapAlloc( GetProcessHeap(), 0, info->bmiHeader.biSizeImage )))
126 HeapFree( GetProcessHeap(), 0, info );
127 goto done;
129 memcpy( brush->bits.ptr, bmp->dib->dsBm.bmBits, info->bmiHeader.biSizeImage );
130 brush->bits.is_copy = TRUE;
131 brush->bits.free = free_heap_bits;
132 brush->info = info;
133 brush->usage = DIB_RGB_COLORS;
135 done:
136 GDI_ReleaseObj( bitmap );
137 return brush->info != NULL;
140 BOOL store_brush_pattern( LOGBRUSH *brush, struct brush_pattern *pattern )
142 HGLOBAL hmem = 0;
144 pattern->bitmap = 0;
145 pattern->info = NULL;
146 pattern->bits.free = NULL;
148 switch (brush->lbStyle)
150 case BS_SOLID:
151 case BS_HOLLOW:
152 case BS_HATCHED:
153 return TRUE;
155 case BS_PATTERN8X8:
156 brush->lbStyle = BS_PATTERN;
157 /* fall through */
158 case BS_PATTERN:
159 brush->lbColor = 0;
160 return copy_bitmap( pattern, (HBITMAP)brush->lbHatch );
162 case BS_DIBPATTERN:
163 hmem = (HGLOBAL)brush->lbHatch;
164 if (!(brush->lbHatch = (ULONG_PTR)GlobalLock( hmem ))) return FALSE;
165 /* fall through */
166 case BS_DIBPATTERNPT:
167 pattern->usage = brush->lbColor;
168 pattern->info = copy_packed_dib( (BITMAPINFO *)brush->lbHatch, pattern->usage );
169 if (hmem) GlobalUnlock( hmem );
170 if (!pattern->info) return FALSE;
171 pattern->bits.ptr = (char *)pattern->info + get_dib_info_size( pattern->info, pattern->usage );
172 brush->lbStyle = BS_DIBPATTERN;
173 brush->lbColor = 0;
174 return TRUE;
176 case BS_DIBPATTERN8X8:
177 case BS_MONOPATTERN:
178 case BS_INDEXED:
179 default:
180 WARN( "invalid brush style %u\n", brush->lbStyle );
181 return FALSE;
185 void free_brush_pattern( struct brush_pattern *pattern )
187 if (pattern->bits.free) pattern->bits.free( &pattern->bits );
188 if (pattern->bitmap) DeleteObject( pattern->bitmap );
189 HeapFree( GetProcessHeap(), 0, pattern->info );
192 BOOL get_brush_bitmap_info( HBRUSH handle, BITMAPINFO *info, void **bits, UINT *usage )
194 BRUSHOBJ *brush;
195 BOOL ret = FALSE;
197 if (!(brush = GDI_GetObjPtr( handle, OBJ_BRUSH ))) return FALSE;
199 if (!brush->pattern.info)
201 BITMAPOBJ *bmp = GDI_GetObjPtr( brush->pattern.bitmap, OBJ_BITMAP );
203 if (bmp)
205 store_bitmap_bits( &brush->pattern, bmp );
206 GDI_ReleaseObj( brush->pattern.bitmap );
209 if (brush->pattern.info)
211 memcpy( info, brush->pattern.info, get_dib_info_size( brush->pattern.info, brush->pattern.usage ));
212 if (info->bmiHeader.biBitCount <= 8 && !info->bmiHeader.biClrUsed)
213 fill_default_color_table( info );
214 *bits = brush->pattern.bits.ptr;
215 *usage = brush->pattern.usage;
216 ret = TRUE;
218 GDI_ReleaseObj( handle );
219 return ret;
223 /***********************************************************************
224 * CreateBrushIndirect (GDI32.@)
226 * Create a logical brush with a given style, color or pattern.
228 * PARAMS
229 * brush [I] Pointer to a LOGBRUSH structure describing the desired brush.
231 * RETURNS
232 * A handle to the created brush, or a NULL handle if the brush cannot be
233 * created.
235 * NOTES
236 * - The brush returned should be freed by the caller using DeleteObject()
237 * when it is no longer required.
238 * - Windows 95 and earlier cannot create brushes from bitmaps or DIBs larger
239 * than 8x8 pixels. If a larger bitmap is given, only a portion of the bitmap
240 * is used.
242 HBRUSH WINAPI CreateBrushIndirect( const LOGBRUSH * brush )
244 BRUSHOBJ * ptr;
245 HBRUSH hbrush;
247 if (!(ptr = HeapAlloc( GetProcessHeap(), 0, sizeof(*ptr) ))) return 0;
249 ptr->logbrush = *brush;
251 if (store_brush_pattern( &ptr->logbrush, &ptr->pattern ) &&
252 (hbrush = alloc_gdi_handle( &ptr->header, OBJ_BRUSH, &brush_funcs )))
254 TRACE("%p\n", hbrush);
255 return hbrush;
258 free_brush_pattern( &ptr->pattern );
259 HeapFree( GetProcessHeap(), 0, ptr );
260 return 0;
264 /***********************************************************************
265 * CreateHatchBrush (GDI32.@)
267 * Create a logical brush with a hatched pattern.
269 * PARAMS
270 * style [I] Direction of lines for the hatch pattern (HS_* values from "wingdi.h")
271 * color [I] Colour of the hatched pattern
273 * RETURNS
274 * A handle to the created brush, or a NULL handle if the brush cannot
275 * be created.
277 * NOTES
278 * - This function uses CreateBrushIndirect() to create the brush.
279 * - The brush returned should be freed by the caller using DeleteObject()
280 * when it is no longer required.
282 HBRUSH WINAPI CreateHatchBrush( INT style, COLORREF color )
284 LOGBRUSH logbrush;
286 TRACE("%d %06x\n", style, color );
288 logbrush.lbStyle = BS_HATCHED;
289 logbrush.lbColor = color;
290 logbrush.lbHatch = style;
292 return CreateBrushIndirect( &logbrush );
296 /***********************************************************************
297 * CreatePatternBrush (GDI32.@)
299 * Create a logical brush with a pattern from a bitmap.
301 * PARAMS
302 * hbitmap [I] Bitmap containing pattern for the brush
304 * RETURNS
305 * A handle to the created brush, or a NULL handle if the brush cannot
306 * be created.
308 * NOTES
309 * - This function uses CreateBrushIndirect() to create the brush.
310 * - The brush returned should be freed by the caller using DeleteObject()
311 * when it is no longer required.
313 HBRUSH WINAPI CreatePatternBrush( HBITMAP hbitmap )
315 LOGBRUSH logbrush = { BS_PATTERN, 0, 0 };
316 TRACE("%p\n", hbitmap );
318 logbrush.lbHatch = (ULONG_PTR)hbitmap;
319 return CreateBrushIndirect( &logbrush );
323 /***********************************************************************
324 * CreateDIBPatternBrush (GDI32.@)
326 * Create a logical brush with a pattern from a DIB.
328 * PARAMS
329 * hbitmap [I] Global object containing BITMAPINFO structure for the pattern
330 * coloruse [I] Specifies color format, if provided
332 * RETURNS
333 * A handle to the created brush, or a NULL handle if the brush cannot
334 * be created.
336 * NOTES
337 * - This function uses CreateBrushIndirect() to create the brush.
338 * - The brush returned should be freed by the caller using DeleteObject()
339 * when it is no longer required.
340 * - This function is for compatibility only. CreateDIBPatternBrushPt() should
341 * be used instead.
343 HBRUSH WINAPI CreateDIBPatternBrush( HGLOBAL hbitmap, UINT coloruse )
345 LOGBRUSH logbrush;
347 TRACE("%p\n", hbitmap );
349 logbrush.lbStyle = BS_DIBPATTERN;
350 logbrush.lbColor = coloruse;
352 logbrush.lbHatch = (ULONG_PTR)hbitmap;
354 return CreateBrushIndirect( &logbrush );
358 /***********************************************************************
359 * CreateDIBPatternBrushPt (GDI32.@)
361 * Create a logical brush with a pattern from a DIB.
363 * PARAMS
364 * data [I] Pointer to a BITMAPINFO structure and image data for the pattern
365 * coloruse [I] Specifies color format, if provided
367 * RETURNS
368 * A handle to the created brush, or a NULL handle if the brush cannot
369 * be created.
371 * NOTES
372 * - This function uses CreateBrushIndirect() to create the brush.
373 * - The brush returned should be freed by the caller using DeleteObject()
374 * when it is no longer required.
376 HBRUSH WINAPI CreateDIBPatternBrushPt( const void* data, UINT coloruse )
378 const BITMAPINFO *info=data;
379 LOGBRUSH logbrush;
381 if (!data)
382 return NULL;
384 TRACE("%p %dx%d %dbpp\n", info, info->bmiHeader.biWidth,
385 info->bmiHeader.biHeight, info->bmiHeader.biBitCount);
387 logbrush.lbStyle = BS_DIBPATTERNPT;
388 logbrush.lbColor = coloruse;
389 logbrush.lbHatch = (ULONG_PTR)data;
391 return CreateBrushIndirect( &logbrush );
395 /***********************************************************************
396 * CreateSolidBrush (GDI32.@)
398 * Create a logical brush consisting of a single colour.
400 * PARAMS
401 * color [I] Colour to make the solid brush
403 * RETURNS
404 * A handle to the newly created brush, or a NULL handle if the brush cannot
405 * be created.
407 * NOTES
408 * - This function uses CreateBrushIndirect() to create the brush.
409 * - The brush returned should be freed by the caller using DeleteObject()
410 * when it is no longer required.
412 HBRUSH WINAPI CreateSolidBrush( COLORREF color )
414 LOGBRUSH logbrush;
416 TRACE("%06x\n", color );
418 logbrush.lbStyle = BS_SOLID;
419 logbrush.lbColor = color;
420 logbrush.lbHatch = 0;
422 return CreateBrushIndirect( &logbrush );
426 /***********************************************************************
427 * SetBrushOrgEx (GDI32.@)
429 * Set the brush origin for a device context.
431 * PARAMS
432 * hdc [I] Device context to set the brush origin for
433 * x [I] New x origin
434 * y [I] New y origin
435 * oldorg [O] If non NULL, destination for previously set brush origin.
437 * RETURNS
438 * Success: TRUE. The origin is set to (x,y), and oldorg is updated if given.
440 BOOL WINAPI SetBrushOrgEx( HDC hdc, INT x, INT y, LPPOINT oldorg )
442 DC *dc = get_dc_ptr( hdc );
444 if (!dc) return FALSE;
445 if (oldorg)
447 oldorg->x = dc->brushOrgX;
448 oldorg->y = dc->brushOrgY;
450 dc->brushOrgX = x;
451 dc->brushOrgY = y;
452 release_dc_ptr( dc );
453 return TRUE;
456 /***********************************************************************
457 * FixBrushOrgEx (GDI32.@)
459 * See SetBrushOrgEx.
461 * NOTES
462 * This function is no longer documented by MSDN, but in Win95 GDI32 it
463 * is the same as SetBrushOrgEx().
465 BOOL WINAPI FixBrushOrgEx( HDC hdc, INT x, INT y, LPPOINT oldorg )
467 return SetBrushOrgEx(hdc,x,y,oldorg);
471 /***********************************************************************
472 * BRUSH_SelectObject
474 static HGDIOBJ BRUSH_SelectObject( HGDIOBJ handle, HDC hdc )
476 BRUSHOBJ *brush;
477 HGDIOBJ ret = 0;
478 DC *dc = get_dc_ptr( hdc );
480 if (!dc)
482 SetLastError( ERROR_INVALID_HANDLE );
483 return 0;
486 if ((brush = GDI_GetObjPtr( handle, OBJ_BRUSH )))
488 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pSelectBrush );
489 HBITMAP bitmap = brush->pattern.bitmap;
490 BITMAPINFO *info;
491 void *bits;
492 UINT usage;
494 if (bitmap && !brush->pattern.info)
496 BITMAPOBJ *bmp = GDI_GetObjPtr( bitmap, OBJ_BITMAP );
497 /* fetch the bitmap bits if we are selecting into a different type of DC */
498 if (bmp && bmp->funcs != physdev->funcs) store_bitmap_bits( &brush->pattern, bmp );
499 GDI_ReleaseObj( bitmap );
502 info = brush->pattern.info;
503 bits = brush->pattern.bits.ptr;
504 usage = brush->pattern.usage;
505 GDI_inc_ref_count( handle );
506 GDI_ReleaseObj( handle );
508 if (!physdev->funcs->pSelectBrush( physdev, handle, bitmap, info, bits, usage ))
510 GDI_dec_ref_count( handle );
512 else
514 ret = dc->hBrush;
515 dc->hBrush = handle;
516 GDI_dec_ref_count( ret );
519 release_dc_ptr( dc );
520 return ret;
524 /***********************************************************************
525 * BRUSH_DeleteObject
527 static BOOL BRUSH_DeleteObject( HGDIOBJ handle )
529 BRUSHOBJ *brush = free_gdi_handle( handle );
531 if (!brush) return FALSE;
532 free_brush_pattern( &brush->pattern );
533 return HeapFree( GetProcessHeap(), 0, brush );
537 /***********************************************************************
538 * BRUSH_GetObject
540 static INT BRUSH_GetObject( HGDIOBJ handle, INT count, LPVOID buffer )
542 BRUSHOBJ *brush = GDI_GetObjPtr( handle, OBJ_BRUSH );
544 if (!brush) return 0;
545 if (buffer)
547 if (count > sizeof(brush->logbrush)) count = sizeof(brush->logbrush);
548 memcpy( buffer, &brush->logbrush, count );
550 else count = sizeof(brush->logbrush);
551 GDI_ReleaseObj( handle );
552 return count;