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
27 #include "gdi_private.h"
28 #include "wine/debug.h"
30 WINE_DEFAULT_DEBUG_CHANNEL(gdi
);
32 /* GDI logical brush object */
36 struct brush_pattern pattern
;
39 #define NB_HATCH_STYLES 6
41 static HGDIOBJ
BRUSH_SelectObject( HGDIOBJ handle
, HDC hdc
);
42 static INT
BRUSH_GetObject( HGDIOBJ handle
, INT count
, LPVOID buffer
);
43 static BOOL
BRUSH_DeleteObject( HGDIOBJ handle
);
45 static const struct gdi_obj_funcs brush_funcs
=
47 BRUSH_SelectObject
, /* pSelectObject */
48 BRUSH_GetObject
, /* pGetObjectA */
49 BRUSH_GetObject
, /* pGetObjectW */
50 NULL
, /* pUnrealizeObject */
51 BRUSH_DeleteObject
/* pDeleteObject */
55 static BOOL
copy_bitmap( struct brush_pattern
*brush
, HBITMAP bitmap
)
57 char buffer
[FIELD_OFFSET( BITMAPINFO
, bmiColors
[256])];
58 BITMAPINFO
*info
= (BITMAPINFO
*)buffer
;
59 struct gdi_image_bits bits
;
60 struct bitblt_coords src
;
61 BITMAPOBJ
*bmp
= GDI_GetObjPtr( bitmap
, OBJ_BITMAP
);
63 if (!bmp
) return FALSE
;
65 src
.visrect
.left
= src
.x
= 0;
66 src
.visrect
.top
= src
.y
= 0;
67 src
.visrect
.right
= src
.width
= bmp
->dib
.dsBm
.bmWidth
;
68 src
.visrect
.bottom
= src
.height
= bmp
->dib
.dsBm
.bmHeight
;
69 if (get_image_from_bitmap( bmp
, info
, &bits
, &src
)) goto done
;
74 if (!(brush
->bits
.ptr
= HeapAlloc( GetProcessHeap(), 0, info
->bmiHeader
.biSizeImage
))) goto done
;
75 memcpy( brush
->bits
.ptr
, bits
.ptr
, info
->bmiHeader
.biSizeImage
);
76 brush
->bits
.free
= free_heap_bits
;
79 if (!(brush
->info
= HeapAlloc( GetProcessHeap(), 0, get_dib_info_size( info
, DIB_RGB_COLORS
))))
81 if (brush
->bits
.free
) brush
->bits
.free( &brush
->bits
);
84 memcpy( brush
->info
, info
, get_dib_info_size( info
, DIB_RGB_COLORS
));
85 brush
->bits
.is_copy
= FALSE
; /* the bits can't be modified */
86 brush
->usage
= DIB_RGB_COLORS
;
89 GDI_ReleaseObj( bitmap
);
90 return brush
->info
!= NULL
;
93 BOOL
store_brush_pattern( LOGBRUSH
*brush
, struct brush_pattern
*pattern
)
98 pattern
->bits
.free
= NULL
;
100 switch (brush
->lbStyle
)
107 if (brush
->lbHatch
> HS_DIAGCROSS
)
109 if (brush
->lbHatch
>= HS_API_MAX
) return FALSE
;
110 brush
->lbStyle
= BS_SOLID
;
116 brush
->lbStyle
= BS_PATTERN
;
120 return copy_bitmap( pattern
, (HBITMAP
)brush
->lbHatch
);
123 hmem
= (HGLOBAL
)brush
->lbHatch
;
124 if (!(brush
->lbHatch
= (ULONG_PTR
)GlobalLock( hmem
))) return FALSE
;
126 case BS_DIBPATTERNPT
:
127 pattern
->usage
= brush
->lbColor
;
128 pattern
->info
= copy_packed_dib( (BITMAPINFO
*)brush
->lbHatch
, pattern
->usage
);
129 if (hmem
) GlobalUnlock( hmem
);
130 if (!pattern
->info
) return FALSE
;
131 pattern
->bits
.ptr
= (char *)pattern
->info
+ get_dib_info_size( pattern
->info
, pattern
->usage
);
132 brush
->lbStyle
= BS_DIBPATTERN
;
136 case BS_DIBPATTERN8X8
:
140 WARN( "invalid brush style %u\n", brush
->lbStyle
);
145 void free_brush_pattern( struct brush_pattern
*pattern
)
147 if (pattern
->bits
.free
) pattern
->bits
.free( &pattern
->bits
);
148 HeapFree( GetProcessHeap(), 0, pattern
->info
);
151 BOOL
get_brush_bitmap_info( HBRUSH handle
, BITMAPINFO
*info
, void **bits
, UINT
*usage
)
156 if (!(brush
= GDI_GetObjPtr( handle
, OBJ_BRUSH
))) return FALSE
;
158 if (brush
->pattern
.info
)
160 memcpy( info
, brush
->pattern
.info
, get_dib_info_size( brush
->pattern
.info
, brush
->pattern
.usage
));
161 if (info
->bmiHeader
.biBitCount
<= 8 && !info
->bmiHeader
.biClrUsed
)
162 fill_default_color_table( info
);
163 *bits
= brush
->pattern
.bits
.ptr
;
164 *usage
= brush
->pattern
.usage
;
167 GDI_ReleaseObj( handle
);
172 /***********************************************************************
173 * CreateBrushIndirect (GDI32.@)
175 * Create a logical brush with a given style, color or pattern.
178 * brush [I] Pointer to a LOGBRUSH structure describing the desired brush.
181 * A handle to the created brush, or a NULL handle if the brush cannot be
185 * - The brush returned should be freed by the caller using DeleteObject()
186 * when it is no longer required.
187 * - Windows 95 and earlier cannot create brushes from bitmaps or DIBs larger
188 * than 8x8 pixels. If a larger bitmap is given, only a portion of the bitmap
191 HBRUSH WINAPI
CreateBrushIndirect( const LOGBRUSH
* brush
)
196 if (!(ptr
= HeapAlloc( GetProcessHeap(), 0, sizeof(*ptr
) ))) return 0;
198 ptr
->logbrush
= *brush
;
200 if (store_brush_pattern( &ptr
->logbrush
, &ptr
->pattern
) &&
201 (hbrush
= alloc_gdi_handle( ptr
, OBJ_BRUSH
, &brush_funcs
)))
203 TRACE("%p\n", hbrush
);
207 free_brush_pattern( &ptr
->pattern
);
208 HeapFree( GetProcessHeap(), 0, ptr
);
213 /***********************************************************************
214 * CreateHatchBrush (GDI32.@)
216 * Create a logical brush with a hatched pattern.
219 * style [I] Direction of lines for the hatch pattern (HS_* values from "wingdi.h")
220 * color [I] Colour of the hatched pattern
223 * A handle to the created brush, or a NULL handle if the brush cannot
227 * - This function uses CreateBrushIndirect() to create the brush.
228 * - The brush returned should be freed by the caller using DeleteObject()
229 * when it is no longer required.
231 HBRUSH WINAPI
CreateHatchBrush( INT style
, COLORREF color
)
235 TRACE("%d %06x\n", style
, color
);
237 logbrush
.lbStyle
= BS_HATCHED
;
238 logbrush
.lbColor
= color
;
239 logbrush
.lbHatch
= style
;
241 return CreateBrushIndirect( &logbrush
);
245 /***********************************************************************
246 * CreatePatternBrush (GDI32.@)
248 * Create a logical brush with a pattern from a bitmap.
251 * hbitmap [I] Bitmap containing pattern for the brush
254 * A handle to the created brush, or a NULL handle if the brush cannot
258 * - This function uses CreateBrushIndirect() to create the brush.
259 * - The brush returned should be freed by the caller using DeleteObject()
260 * when it is no longer required.
262 HBRUSH WINAPI
CreatePatternBrush( HBITMAP hbitmap
)
264 LOGBRUSH logbrush
= { BS_PATTERN
, 0, 0 };
265 TRACE("%p\n", hbitmap
);
267 logbrush
.lbHatch
= (ULONG_PTR
)hbitmap
;
268 return CreateBrushIndirect( &logbrush
);
272 /***********************************************************************
273 * CreateDIBPatternBrush (GDI32.@)
275 * Create a logical brush with a pattern from a DIB.
278 * hbitmap [I] Global object containing BITMAPINFO structure for the pattern
279 * coloruse [I] Specifies color format, if provided
282 * A handle to the created brush, or a NULL handle if the brush cannot
286 * - This function uses CreateBrushIndirect() to create the brush.
287 * - The brush returned should be freed by the caller using DeleteObject()
288 * when it is no longer required.
289 * - This function is for compatibility only. CreateDIBPatternBrushPt() should
292 HBRUSH WINAPI
CreateDIBPatternBrush( HGLOBAL hbitmap
, UINT coloruse
)
296 TRACE("%p\n", hbitmap
);
298 logbrush
.lbStyle
= BS_DIBPATTERN
;
299 logbrush
.lbColor
= coloruse
;
301 logbrush
.lbHatch
= (ULONG_PTR
)hbitmap
;
303 return CreateBrushIndirect( &logbrush
);
307 /***********************************************************************
308 * CreateDIBPatternBrushPt (GDI32.@)
310 * Create a logical brush with a pattern from a DIB.
313 * data [I] Pointer to a BITMAPINFO structure and image data for the pattern
314 * coloruse [I] Specifies color format, if provided
317 * A handle to the created brush, or a NULL handle if the brush cannot
321 * - This function uses CreateBrushIndirect() to create the brush.
322 * - The brush returned should be freed by the caller using DeleteObject()
323 * when it is no longer required.
325 HBRUSH WINAPI
CreateDIBPatternBrushPt( const void* data
, UINT coloruse
)
327 const BITMAPINFO
*info
=data
;
333 TRACE("%p %dx%d %dbpp\n", info
, info
->bmiHeader
.biWidth
,
334 info
->bmiHeader
.biHeight
, info
->bmiHeader
.biBitCount
);
336 logbrush
.lbStyle
= BS_DIBPATTERNPT
;
337 logbrush
.lbColor
= coloruse
;
338 logbrush
.lbHatch
= (ULONG_PTR
)data
;
340 return CreateBrushIndirect( &logbrush
);
344 /***********************************************************************
345 * CreateSolidBrush (GDI32.@)
347 * Create a logical brush consisting of a single colour.
350 * color [I] Colour to make the solid brush
353 * A handle to the newly created brush, or a NULL handle if the brush cannot
357 * - This function uses CreateBrushIndirect() to create the brush.
358 * - The brush returned should be freed by the caller using DeleteObject()
359 * when it is no longer required.
361 HBRUSH WINAPI
CreateSolidBrush( COLORREF color
)
365 TRACE("%06x\n", color
);
367 logbrush
.lbStyle
= BS_SOLID
;
368 logbrush
.lbColor
= color
;
369 logbrush
.lbHatch
= 0;
371 return CreateBrushIndirect( &logbrush
);
375 /***********************************************************************
376 * SetBrushOrgEx (GDI32.@)
378 * Set the brush origin for a device context.
381 * hdc [I] Device context to set the brush origin for
384 * oldorg [O] If non NULL, destination for previously set brush origin.
387 * Success: TRUE. The origin is set to (x,y), and oldorg is updated if given.
389 BOOL WINAPI
SetBrushOrgEx( HDC hdc
, INT x
, INT y
, LPPOINT oldorg
)
391 DC
*dc
= get_dc_ptr( hdc
);
393 if (!dc
) return FALSE
;
395 *oldorg
= dc
->brush_org
;
399 release_dc_ptr( dc
);
403 /***********************************************************************
404 * FixBrushOrgEx (GDI32.@)
409 * This function is no longer documented by MSDN, but in Win95 GDI32 it
410 * is the same as SetBrushOrgEx().
412 BOOL WINAPI
FixBrushOrgEx( HDC hdc
, INT x
, INT y
, LPPOINT oldorg
)
414 return SetBrushOrgEx(hdc
,x
,y
,oldorg
);
418 /***********************************************************************
421 static HGDIOBJ
BRUSH_SelectObject( HGDIOBJ handle
, HDC hdc
)
425 DC
*dc
= get_dc_ptr( hdc
);
429 SetLastError( ERROR_INVALID_HANDLE
);
433 if ((brush
= GDI_GetObjPtr( handle
, OBJ_BRUSH
)))
435 PHYSDEV physdev
= GET_DC_PHYSDEV( dc
, pSelectBrush
);
436 struct brush_pattern
*pattern
= &brush
->pattern
;
438 if (!pattern
->info
) pattern
= NULL
;
440 GDI_inc_ref_count( handle
);
441 GDI_ReleaseObj( handle
);
443 if (!physdev
->funcs
->pSelectBrush( physdev
, handle
, pattern
))
445 GDI_dec_ref_count( handle
);
451 GDI_dec_ref_count( ret
);
454 release_dc_ptr( dc
);
459 /***********************************************************************
462 static BOOL
BRUSH_DeleteObject( HGDIOBJ handle
)
464 BRUSHOBJ
*brush
= free_gdi_handle( handle
);
466 if (!brush
) return FALSE
;
467 free_brush_pattern( &brush
->pattern
);
468 HeapFree( GetProcessHeap(), 0, brush
);
473 /***********************************************************************
476 static INT
BRUSH_GetObject( HGDIOBJ handle
, INT count
, LPVOID buffer
)
478 BRUSHOBJ
*brush
= GDI_GetObjPtr( handle
, OBJ_BRUSH
);
480 if (!brush
) return 0;
483 if (count
> sizeof(brush
->logbrush
)) count
= sizeof(brush
->logbrush
);
484 memcpy( buffer
, &brush
->logbrush
, count
);
486 else count
= sizeof(brush
->logbrush
);
487 GDI_ReleaseObj( handle
);