vbscript: Implemented VarType.
[wine.git] / dlls / gdi32 / brush.c
blob4f0c17d734e6ca9faf2a7ebfe7edd48ab6e5d516
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 LOGBRUSH logbrush;
38 struct brush_pattern pattern;
39 } BRUSHOBJ;
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;
73 brush->bits = bits;
74 if (!bits.free)
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 );
84 goto done;
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;
90 done:
91 GDI_ReleaseObj( bitmap );
92 return brush->info != NULL;
95 BOOL store_brush_pattern( LOGBRUSH *brush, struct brush_pattern *pattern )
97 HGLOBAL hmem = 0;
99 pattern->info = NULL;
100 pattern->bits.free = NULL;
102 switch (brush->lbStyle)
104 case BS_SOLID:
105 case BS_HOLLOW:
106 return TRUE;
108 case BS_HATCHED:
109 if (brush->lbHatch > HS_DIAGCROSS)
111 if (brush->lbHatch >= HS_API_MAX) return FALSE;
112 brush->lbStyle = BS_SOLID;
113 brush->lbHatch = 0;
115 return TRUE;
117 case BS_PATTERN8X8:
118 brush->lbStyle = BS_PATTERN;
119 /* fall through */
120 case BS_PATTERN:
121 brush->lbColor = 0;
122 return copy_bitmap( pattern, (HBITMAP)brush->lbHatch );
124 case BS_DIBPATTERN:
125 hmem = (HGLOBAL)brush->lbHatch;
126 if (!(brush->lbHatch = (ULONG_PTR)GlobalLock( hmem ))) return FALSE;
127 /* fall through */
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;
135 brush->lbColor = 0;
136 return TRUE;
138 case BS_DIBPATTERN8X8:
139 case BS_MONOPATTERN:
140 case BS_INDEXED:
141 default:
142 WARN( "invalid brush style %u\n", brush->lbStyle );
143 return FALSE;
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 )
155 BRUSHOBJ *brush;
156 BOOL ret = FALSE;
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;
167 ret = TRUE;
169 GDI_ReleaseObj( handle );
170 return ret;
174 /***********************************************************************
175 * CreateBrushIndirect (GDI32.@)
177 * Create a logical brush with a given style, color or pattern.
179 * PARAMS
180 * brush [I] Pointer to a LOGBRUSH structure describing the desired brush.
182 * RETURNS
183 * A handle to the created brush, or a NULL handle if the brush cannot be
184 * created.
186 * NOTES
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
191 * is used.
193 HBRUSH WINAPI CreateBrushIndirect( const LOGBRUSH * brush )
195 BRUSHOBJ * ptr;
196 HBRUSH hbrush;
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);
206 return hbrush;
209 free_brush_pattern( &ptr->pattern );
210 HeapFree( GetProcessHeap(), 0, ptr );
211 return 0;
215 /***********************************************************************
216 * CreateHatchBrush (GDI32.@)
218 * Create a logical brush with a hatched pattern.
220 * PARAMS
221 * style [I] Direction of lines for the hatch pattern (HS_* values from "wingdi.h")
222 * color [I] Colour of the hatched pattern
224 * RETURNS
225 * A handle to the created brush, or a NULL handle if the brush cannot
226 * be created.
228 * NOTES
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 )
235 LOGBRUSH logbrush;
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.
252 * PARAMS
253 * hbitmap [I] Bitmap containing pattern for the brush
255 * RETURNS
256 * A handle to the created brush, or a NULL handle if the brush cannot
257 * be created.
259 * NOTES
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.
279 * PARAMS
280 * hbitmap [I] Global object containing BITMAPINFO structure for the pattern
281 * coloruse [I] Specifies color format, if provided
283 * RETURNS
284 * A handle to the created brush, or a NULL handle if the brush cannot
285 * be created.
287 * NOTES
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
292 * be used instead.
294 HBRUSH WINAPI CreateDIBPatternBrush( HGLOBAL hbitmap, UINT coloruse )
296 LOGBRUSH logbrush;
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.
314 * PARAMS
315 * data [I] Pointer to a BITMAPINFO structure and image data for the pattern
316 * coloruse [I] Specifies color format, if provided
318 * RETURNS
319 * A handle to the created brush, or a NULL handle if the brush cannot
320 * be created.
322 * NOTES
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;
330 LOGBRUSH logbrush;
332 if (!data)
333 return NULL;
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.
351 * PARAMS
352 * color [I] Colour to make the solid brush
354 * RETURNS
355 * A handle to the newly created brush, or a NULL handle if the brush cannot
356 * be created.
358 * NOTES
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 )
365 LOGBRUSH logbrush;
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.
382 * PARAMS
383 * hdc [I] Device context to set the brush origin for
384 * x [I] New x origin
385 * y [I] New y origin
386 * oldorg [O] If non NULL, destination for previously set brush origin.
388 * RETURNS
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;
396 if (oldorg)
398 oldorg->x = dc->brushOrgX;
399 oldorg->y = dc->brushOrgY;
401 dc->brushOrgX = x;
402 dc->brushOrgY = y;
403 release_dc_ptr( dc );
404 return TRUE;
407 /***********************************************************************
408 * FixBrushOrgEx (GDI32.@)
410 * See SetBrushOrgEx.
412 * NOTES
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 /***********************************************************************
423 * BRUSH_SelectObject
425 static HGDIOBJ BRUSH_SelectObject( HGDIOBJ handle, HDC hdc )
427 BRUSHOBJ *brush;
428 HGDIOBJ ret = 0;
429 DC *dc = get_dc_ptr( hdc );
431 if (!dc)
433 SetLastError( ERROR_INVALID_HANDLE );
434 return 0;
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 );
451 else
453 ret = dc->hBrush;
454 dc->hBrush = handle;
455 GDI_dec_ref_count( ret );
458 release_dc_ptr( dc );
459 return ret;
463 /***********************************************************************
464 * BRUSH_DeleteObject
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 /***********************************************************************
477 * BRUSH_GetObject
479 static INT BRUSH_GetObject( HGDIOBJ handle, INT count, LPVOID buffer )
481 BRUSHOBJ *brush = GDI_GetObjPtr( handle, OBJ_BRUSH );
483 if (!brush) return 0;
484 if (buffer)
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 );
491 return count;