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 */
38 struct brush_pattern pattern
;
41 #define NB_HATCH_STYLES 6
43 static HGDIOBJ
BRUSH_SelectObject( HGDIOBJ handle
, HDC hdc
);
44 static INT
BRUSH_GetObject( HGDIOBJ handle
, INT count
, LPVOID buffer
);
45 static BOOL
BRUSH_DeleteObject( HGDIOBJ handle
);
47 static const struct gdi_obj_funcs brush_funcs
=
49 BRUSH_SelectObject
, /* pSelectObject */
50 BRUSH_GetObject
, /* pGetObjectA */
51 BRUSH_GetObject
, /* pGetObjectW */
52 NULL
, /* pUnrealizeObject */
53 BRUSH_DeleteObject
/* pDeleteObject */
57 static BOOL
copy_bitmap( struct brush_pattern
*brush
, HBITMAP bitmap
)
59 char buffer
[FIELD_OFFSET( BITMAPINFO
, bmiColors
[256])];
60 BITMAPINFO
*info
= (BITMAPINFO
*)buffer
;
61 struct gdi_image_bits bits
;
62 struct bitblt_coords src
;
63 BITMAPOBJ
*bmp
= GDI_GetObjPtr( bitmap
, OBJ_BITMAP
);
65 if (!bmp
) return FALSE
;
67 src
.visrect
.left
= src
.x
= 0;
68 src
.visrect
.top
= src
.y
= 0;
69 src
.visrect
.right
= src
.width
= bmp
->dib
.dsBm
.bmWidth
;
70 src
.visrect
.bottom
= src
.height
= bmp
->dib
.dsBm
.bmHeight
;
71 if (get_image_from_bitmap( bmp
, info
, &bits
, &src
)) goto done
;
76 if (!(brush
->bits
.ptr
= HeapAlloc( GetProcessHeap(), 0, info
->bmiHeader
.biSizeImage
))) goto done
;
77 memcpy( brush
->bits
.ptr
, bits
.ptr
, info
->bmiHeader
.biSizeImage
);
78 brush
->bits
.free
= free_heap_bits
;
81 if (!(brush
->info
= HeapAlloc( GetProcessHeap(), 0, get_dib_info_size( info
, DIB_RGB_COLORS
))))
83 if (brush
->bits
.free
) brush
->bits
.free( &brush
->bits
);
86 memcpy( brush
->info
, info
, get_dib_info_size( info
, DIB_RGB_COLORS
));
87 brush
->bits
.is_copy
= FALSE
; /* the bits can't be modified */
88 brush
->usage
= DIB_RGB_COLORS
;
91 GDI_ReleaseObj( bitmap
);
92 return brush
->info
!= NULL
;
95 BOOL
store_brush_pattern( LOGBRUSH
*brush
, struct brush_pattern
*pattern
)
100 pattern
->bits
.free
= NULL
;
102 switch (brush
->lbStyle
)
109 if (brush
->lbHatch
> HS_DIAGCROSS
)
111 if (brush
->lbHatch
>= HS_API_MAX
) return FALSE
;
112 brush
->lbStyle
= BS_SOLID
;
118 brush
->lbStyle
= BS_PATTERN
;
122 return copy_bitmap( pattern
, (HBITMAP
)brush
->lbHatch
);
125 hmem
= (HGLOBAL
)brush
->lbHatch
;
126 if (!(brush
->lbHatch
= (ULONG_PTR
)GlobalLock( hmem
))) return FALSE
;
128 case BS_DIBPATTERNPT
:
129 pattern
->usage
= brush
->lbColor
;
130 pattern
->info
= copy_packed_dib( (BITMAPINFO
*)brush
->lbHatch
, pattern
->usage
);
131 if (hmem
) GlobalUnlock( hmem
);
132 if (!pattern
->info
) return FALSE
;
133 pattern
->bits
.ptr
= (char *)pattern
->info
+ get_dib_info_size( pattern
->info
, pattern
->usage
);
134 brush
->lbStyle
= BS_DIBPATTERN
;
138 case BS_DIBPATTERN8X8
:
142 WARN( "invalid brush style %u\n", brush
->lbStyle
);
147 void free_brush_pattern( struct brush_pattern
*pattern
)
149 if (pattern
->bits
.free
) pattern
->bits
.free( &pattern
->bits
);
150 HeapFree( GetProcessHeap(), 0, pattern
->info
);
153 BOOL
get_brush_bitmap_info( HBRUSH handle
, BITMAPINFO
*info
, void **bits
, UINT
*usage
)
158 if (!(brush
= GDI_GetObjPtr( handle
, OBJ_BRUSH
))) return FALSE
;
160 if (brush
->pattern
.info
)
162 memcpy( info
, brush
->pattern
.info
, get_dib_info_size( brush
->pattern
.info
, brush
->pattern
.usage
));
163 if (info
->bmiHeader
.biBitCount
<= 8 && !info
->bmiHeader
.biClrUsed
)
164 fill_default_color_table( info
);
165 *bits
= brush
->pattern
.bits
.ptr
;
166 *usage
= brush
->pattern
.usage
;
169 GDI_ReleaseObj( handle
);
174 /***********************************************************************
175 * CreateBrushIndirect (GDI32.@)
177 * Create a logical brush with a given style, color or pattern.
180 * brush [I] Pointer to a LOGBRUSH structure describing the desired brush.
183 * A handle to the created brush, or a NULL handle if the brush cannot be
187 * - The brush returned should be freed by the caller using DeleteObject()
188 * when it is no longer required.
189 * - Windows 95 and earlier cannot create brushes from bitmaps or DIBs larger
190 * than 8x8 pixels. If a larger bitmap is given, only a portion of the bitmap
193 HBRUSH WINAPI
CreateBrushIndirect( const LOGBRUSH
* brush
)
198 if (!(ptr
= HeapAlloc( GetProcessHeap(), 0, sizeof(*ptr
) ))) return 0;
200 ptr
->logbrush
= *brush
;
202 if (store_brush_pattern( &ptr
->logbrush
, &ptr
->pattern
) &&
203 (hbrush
= alloc_gdi_handle( ptr
, OBJ_BRUSH
, &brush_funcs
)))
205 TRACE("%p\n", hbrush
);
209 free_brush_pattern( &ptr
->pattern
);
210 HeapFree( GetProcessHeap(), 0, ptr
);
215 /***********************************************************************
216 * CreateHatchBrush (GDI32.@)
218 * Create a logical brush with a hatched pattern.
221 * style [I] Direction of lines for the hatch pattern (HS_* values from "wingdi.h")
222 * color [I] Colour of the hatched pattern
225 * A handle to the created brush, or a NULL handle if the brush cannot
229 * - This function uses CreateBrushIndirect() to create the brush.
230 * - The brush returned should be freed by the caller using DeleteObject()
231 * when it is no longer required.
233 HBRUSH WINAPI
CreateHatchBrush( INT style
, COLORREF color
)
237 TRACE("%d %06x\n", style
, color
);
239 logbrush
.lbStyle
= BS_HATCHED
;
240 logbrush
.lbColor
= color
;
241 logbrush
.lbHatch
= style
;
243 return CreateBrushIndirect( &logbrush
);
247 /***********************************************************************
248 * CreatePatternBrush (GDI32.@)
250 * Create a logical brush with a pattern from a bitmap.
253 * hbitmap [I] Bitmap containing pattern for the brush
256 * A handle to the created brush, or a NULL handle if the brush cannot
260 * - This function uses CreateBrushIndirect() to create the brush.
261 * - The brush returned should be freed by the caller using DeleteObject()
262 * when it is no longer required.
264 HBRUSH WINAPI
CreatePatternBrush( HBITMAP hbitmap
)
266 LOGBRUSH logbrush
= { BS_PATTERN
, 0, 0 };
267 TRACE("%p\n", hbitmap
);
269 logbrush
.lbHatch
= (ULONG_PTR
)hbitmap
;
270 return CreateBrushIndirect( &logbrush
);
274 /***********************************************************************
275 * CreateDIBPatternBrush (GDI32.@)
277 * Create a logical brush with a pattern from a DIB.
280 * hbitmap [I] Global object containing BITMAPINFO structure for the pattern
281 * coloruse [I] Specifies color format, if provided
284 * A handle to the created brush, or a NULL handle if the brush cannot
288 * - This function uses CreateBrushIndirect() to create the brush.
289 * - The brush returned should be freed by the caller using DeleteObject()
290 * when it is no longer required.
291 * - This function is for compatibility only. CreateDIBPatternBrushPt() should
294 HBRUSH WINAPI
CreateDIBPatternBrush( HGLOBAL hbitmap
, UINT coloruse
)
298 TRACE("%p\n", hbitmap
);
300 logbrush
.lbStyle
= BS_DIBPATTERN
;
301 logbrush
.lbColor
= coloruse
;
303 logbrush
.lbHatch
= (ULONG_PTR
)hbitmap
;
305 return CreateBrushIndirect( &logbrush
);
309 /***********************************************************************
310 * CreateDIBPatternBrushPt (GDI32.@)
312 * Create a logical brush with a pattern from a DIB.
315 * data [I] Pointer to a BITMAPINFO structure and image data for the pattern
316 * coloruse [I] Specifies color format, if provided
319 * A handle to the created brush, or a NULL handle if the brush cannot
323 * - This function uses CreateBrushIndirect() to create the brush.
324 * - The brush returned should be freed by the caller using DeleteObject()
325 * when it is no longer required.
327 HBRUSH WINAPI
CreateDIBPatternBrushPt( const void* data
, UINT coloruse
)
329 const BITMAPINFO
*info
=data
;
335 TRACE("%p %dx%d %dbpp\n", info
, info
->bmiHeader
.biWidth
,
336 info
->bmiHeader
.biHeight
, info
->bmiHeader
.biBitCount
);
338 logbrush
.lbStyle
= BS_DIBPATTERNPT
;
339 logbrush
.lbColor
= coloruse
;
340 logbrush
.lbHatch
= (ULONG_PTR
)data
;
342 return CreateBrushIndirect( &logbrush
);
346 /***********************************************************************
347 * CreateSolidBrush (GDI32.@)
349 * Create a logical brush consisting of a single colour.
352 * color [I] Colour to make the solid brush
355 * A handle to the newly created brush, or a NULL handle if the brush cannot
359 * - This function uses CreateBrushIndirect() to create the brush.
360 * - The brush returned should be freed by the caller using DeleteObject()
361 * when it is no longer required.
363 HBRUSH WINAPI
CreateSolidBrush( COLORREF color
)
367 TRACE("%06x\n", color
);
369 logbrush
.lbStyle
= BS_SOLID
;
370 logbrush
.lbColor
= color
;
371 logbrush
.lbHatch
= 0;
373 return CreateBrushIndirect( &logbrush
);
377 /***********************************************************************
378 * SetBrushOrgEx (GDI32.@)
380 * Set the brush origin for a device context.
383 * hdc [I] Device context to set the brush origin for
386 * oldorg [O] If non NULL, destination for previously set brush origin.
389 * Success: TRUE. The origin is set to (x,y), and oldorg is updated if given.
391 BOOL WINAPI
SetBrushOrgEx( HDC hdc
, INT x
, INT y
, LPPOINT oldorg
)
393 DC
*dc
= get_dc_ptr( hdc
);
395 if (!dc
) return FALSE
;
398 oldorg
->x
= dc
->brushOrgX
;
399 oldorg
->y
= dc
->brushOrgY
;
403 release_dc_ptr( dc
);
407 /***********************************************************************
408 * FixBrushOrgEx (GDI32.@)
413 * This function is no longer documented by MSDN, but in Win95 GDI32 it
414 * is the same as SetBrushOrgEx().
416 BOOL WINAPI
FixBrushOrgEx( HDC hdc
, INT x
, INT y
, LPPOINT oldorg
)
418 return SetBrushOrgEx(hdc
,x
,y
,oldorg
);
422 /***********************************************************************
425 static HGDIOBJ
BRUSH_SelectObject( HGDIOBJ handle
, HDC hdc
)
429 DC
*dc
= get_dc_ptr( hdc
);
433 SetLastError( ERROR_INVALID_HANDLE
);
437 if ((brush
= GDI_GetObjPtr( handle
, OBJ_BRUSH
)))
439 PHYSDEV physdev
= GET_DC_PHYSDEV( dc
, pSelectBrush
);
440 struct brush_pattern
*pattern
= &brush
->pattern
;
442 if (!pattern
->info
) pattern
= NULL
;
444 GDI_inc_ref_count( handle
);
445 GDI_ReleaseObj( handle
);
447 if (!physdev
->funcs
->pSelectBrush( physdev
, handle
, pattern
))
449 GDI_dec_ref_count( handle
);
455 GDI_dec_ref_count( ret
);
458 release_dc_ptr( dc
);
463 /***********************************************************************
466 static BOOL
BRUSH_DeleteObject( HGDIOBJ handle
)
468 BRUSHOBJ
*brush
= free_gdi_handle( handle
);
470 if (!brush
) return FALSE
;
471 free_brush_pattern( &brush
->pattern
);
472 return HeapFree( GetProcessHeap(), 0, brush
);
476 /***********************************************************************
479 static INT
BRUSH_GetObject( HGDIOBJ handle
, INT count
, LPVOID buffer
)
481 BRUSHOBJ
*brush
= GDI_GetObjPtr( handle
, OBJ_BRUSH
);
483 if (!brush
) return 0;
486 if (count
> sizeof(brush
->logbrush
)) count
= sizeof(brush
->logbrush
);
487 memcpy( buffer
, &brush
->logbrush
, count
);
489 else count
= sizeof(brush
->logbrush
);
490 GDI_ReleaseObj( handle
);