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 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
;
66 if (!(info
= HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET( BITMAPINFO
, bmiColors
[256] ))))
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
);
79 /* release the unneeded space */
80 HeapReAlloc( GetProcessHeap(), HEAP_REALLOC_IN_PLACE_ONLY
, info
,
81 get_dib_info_size( info
, DIB_RGB_COLORS
));
84 brush
->usage
= DIB_RGB_COLORS
;
88 static BOOL
copy_bitmap( struct brush_pattern
*brush
, HBITMAP bitmap
)
91 BITMAPOBJ
*bmp
= GDI_GetObjPtr( bitmap
, OBJ_BITMAP
);
93 if (!bmp
) return FALSE
;
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
);
108 DeleteObject( brush
->bitmap
);
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
);
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
;
133 brush
->usage
= DIB_RGB_COLORS
;
136 GDI_ReleaseObj( bitmap
);
137 return brush
->info
!= NULL
;
140 BOOL
store_brush_pattern( LOGBRUSH
*brush
, struct brush_pattern
*pattern
)
145 pattern
->info
= NULL
;
146 pattern
->bits
.free
= NULL
;
148 switch (brush
->lbStyle
)
156 brush
->lbStyle
= BS_PATTERN
;
160 return copy_bitmap( pattern
, (HBITMAP
)brush
->lbHatch
);
163 hmem
= (HGLOBAL
)brush
->lbHatch
;
164 if (!(brush
->lbHatch
= (ULONG_PTR
)GlobalLock( hmem
))) return FALSE
;
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
;
176 case BS_DIBPATTERN8X8
:
180 WARN( "invalid brush style %u\n", brush
->lbStyle
);
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
)
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
);
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
;
218 GDI_ReleaseObj( handle
);
223 /***********************************************************************
224 * CreateBrushIndirect (GDI32.@)
226 * Create a logical brush with a given style, color or pattern.
229 * brush [I] Pointer to a LOGBRUSH structure describing the desired brush.
232 * A handle to the created brush, or a NULL handle if the brush cannot be
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
242 HBRUSH WINAPI
CreateBrushIndirect( const LOGBRUSH
* brush
)
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
);
258 free_brush_pattern( &ptr
->pattern
);
259 HeapFree( GetProcessHeap(), 0, ptr
);
264 /***********************************************************************
265 * CreateHatchBrush (GDI32.@)
267 * Create a logical brush with a hatched pattern.
270 * style [I] Direction of lines for the hatch pattern (HS_* values from "wingdi.h")
271 * color [I] Colour of the hatched pattern
274 * A handle to the created brush, or a NULL handle if the brush cannot
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
)
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.
302 * hbitmap [I] Bitmap containing pattern for the brush
305 * A handle to the created brush, or a NULL handle if the brush cannot
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.
329 * hbitmap [I] Global object containing BITMAPINFO structure for the pattern
330 * coloruse [I] Specifies color format, if provided
333 * A handle to the created brush, or a NULL handle if the brush cannot
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
343 HBRUSH WINAPI
CreateDIBPatternBrush( HGLOBAL hbitmap
, UINT coloruse
)
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.
364 * data [I] Pointer to a BITMAPINFO structure and image data for the pattern
365 * coloruse [I] Specifies color format, if provided
368 * A handle to the created brush, or a NULL handle if the brush cannot
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
;
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.
401 * color [I] Colour to make the solid brush
404 * A handle to the newly created brush, or a NULL handle if the brush cannot
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
)
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.
432 * hdc [I] Device context to set the brush origin for
435 * oldorg [O] If non NULL, destination for previously set brush origin.
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
;
447 oldorg
->x
= dc
->brushOrgX
;
448 oldorg
->y
= dc
->brushOrgY
;
452 release_dc_ptr( dc
);
456 /***********************************************************************
457 * FixBrushOrgEx (GDI32.@)
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 /***********************************************************************
474 static HGDIOBJ
BRUSH_SelectObject( HGDIOBJ handle
, HDC hdc
)
478 DC
*dc
= get_dc_ptr( hdc
);
482 SetLastError( ERROR_INVALID_HANDLE
);
486 if ((brush
= GDI_GetObjPtr( handle
, OBJ_BRUSH
)))
488 PHYSDEV physdev
= GET_DC_PHYSDEV( dc
, pSelectBrush
);
489 HBITMAP bitmap
= brush
->pattern
.bitmap
;
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
);
516 GDI_dec_ref_count( ret
);
519 release_dc_ptr( dc
);
524 /***********************************************************************
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 /***********************************************************************
540 static INT
BRUSH_GetObject( HGDIOBJ handle
, INT count
, LPVOID buffer
)
542 BRUSHOBJ
*brush
= GDI_GetObjPtr( handle
, OBJ_BRUSH
);
544 if (!brush
) return 0;
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
);