mfplat: Implement GetScanline0AndPitch() for d3d11 buffers.
[wine/zf.git] / dlls / gdi32 / brush.c
blob517f8c17e22694aa59a2c774196f25b0c16b345d
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 <stdarg.h>
22 #include <string.h>
24 #include "windef.h"
25 #include "winbase.h"
26 #include "wingdi.h"
27 #include "gdi_private.h"
28 #include "wine/debug.h"
30 WINE_DEFAULT_DEBUG_CHANNEL(gdi);
32 /* GDI logical brush object */
33 typedef struct
35 LOGBRUSH logbrush;
36 struct brush_pattern pattern;
37 } BRUSHOBJ;
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;
71 brush->bits = bits;
72 if (!bits.free)
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 );
82 goto done;
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;
88 done:
89 GDI_ReleaseObj( bitmap );
90 return brush->info != NULL;
93 BOOL store_brush_pattern( LOGBRUSH *brush, struct brush_pattern *pattern )
95 HGLOBAL hmem = 0;
97 pattern->info = NULL;
98 pattern->bits.free = NULL;
100 switch (brush->lbStyle)
102 case BS_SOLID:
103 case BS_HOLLOW:
104 return TRUE;
106 case BS_HATCHED:
107 if (brush->lbHatch > HS_DIAGCROSS)
109 if (brush->lbHatch >= HS_API_MAX) return FALSE;
110 brush->lbStyle = BS_SOLID;
111 brush->lbHatch = 0;
113 return TRUE;
115 case BS_PATTERN8X8:
116 brush->lbStyle = BS_PATTERN;
117 /* fall through */
118 case BS_PATTERN:
119 brush->lbColor = 0;
120 return copy_bitmap( pattern, (HBITMAP)brush->lbHatch );
122 case BS_DIBPATTERN:
123 hmem = (HGLOBAL)brush->lbHatch;
124 if (!(brush->lbHatch = (ULONG_PTR)GlobalLock( hmem ))) return FALSE;
125 /* fall through */
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;
133 brush->lbColor = 0;
134 return TRUE;
136 case BS_DIBPATTERN8X8:
137 case BS_MONOPATTERN:
138 case BS_INDEXED:
139 default:
140 WARN( "invalid brush style %u\n", brush->lbStyle );
141 return FALSE;
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 )
153 BRUSHOBJ *brush;
154 BOOL ret = FALSE;
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;
165 ret = TRUE;
167 GDI_ReleaseObj( handle );
168 return ret;
172 /***********************************************************************
173 * CreateBrushIndirect (GDI32.@)
175 * Create a logical brush with a given style, color or pattern.
177 * PARAMS
178 * brush [I] Pointer to a LOGBRUSH structure describing the desired brush.
180 * RETURNS
181 * A handle to the created brush, or a NULL handle if the brush cannot be
182 * created.
184 * NOTES
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
189 * is used.
191 HBRUSH WINAPI CreateBrushIndirect( const LOGBRUSH * brush )
193 BRUSHOBJ * ptr;
194 HBRUSH hbrush;
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);
204 return hbrush;
207 free_brush_pattern( &ptr->pattern );
208 HeapFree( GetProcessHeap(), 0, ptr );
209 return 0;
213 /***********************************************************************
214 * CreateHatchBrush (GDI32.@)
216 * Create a logical brush with a hatched pattern.
218 * PARAMS
219 * style [I] Direction of lines for the hatch pattern (HS_* values from "wingdi.h")
220 * color [I] Colour of the hatched pattern
222 * RETURNS
223 * A handle to the created brush, or a NULL handle if the brush cannot
224 * be created.
226 * NOTES
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 )
233 LOGBRUSH logbrush;
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.
250 * PARAMS
251 * hbitmap [I] Bitmap containing pattern for the brush
253 * RETURNS
254 * A handle to the created brush, or a NULL handle if the brush cannot
255 * be created.
257 * NOTES
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.
277 * PARAMS
278 * hbitmap [I] Global object containing BITMAPINFO structure for the pattern
279 * coloruse [I] Specifies color format, if provided
281 * RETURNS
282 * A handle to the created brush, or a NULL handle if the brush cannot
283 * be created.
285 * NOTES
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
290 * be used instead.
292 HBRUSH WINAPI CreateDIBPatternBrush( HGLOBAL hbitmap, UINT coloruse )
294 LOGBRUSH logbrush;
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.
312 * PARAMS
313 * data [I] Pointer to a BITMAPINFO structure and image data for the pattern
314 * coloruse [I] Specifies color format, if provided
316 * RETURNS
317 * A handle to the created brush, or a NULL handle if the brush cannot
318 * be created.
320 * NOTES
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;
328 LOGBRUSH logbrush;
330 if (!data)
331 return NULL;
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.
349 * PARAMS
350 * color [I] Colour to make the solid brush
352 * RETURNS
353 * A handle to the newly created brush, or a NULL handle if the brush cannot
354 * be created.
356 * NOTES
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 )
363 LOGBRUSH logbrush;
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.
380 * PARAMS
381 * hdc [I] Device context to set the brush origin for
382 * x [I] New x origin
383 * y [I] New y origin
384 * oldorg [O] If non NULL, destination for previously set brush origin.
386 * RETURNS
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;
394 if (oldorg)
395 *oldorg = dc->brush_org;
397 dc->brush_org.x = x;
398 dc->brush_org.y = y;
399 release_dc_ptr( dc );
400 return TRUE;
403 /***********************************************************************
404 * FixBrushOrgEx (GDI32.@)
406 * See SetBrushOrgEx.
408 * NOTES
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 /***********************************************************************
419 * BRUSH_SelectObject
421 static HGDIOBJ BRUSH_SelectObject( HGDIOBJ handle, HDC hdc )
423 BRUSHOBJ *brush;
424 HGDIOBJ ret = 0;
425 DC *dc = get_dc_ptr( hdc );
427 if (!dc)
429 SetLastError( ERROR_INVALID_HANDLE );
430 return 0;
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 );
447 else
449 ret = dc->hBrush;
450 dc->hBrush = handle;
451 GDI_dec_ref_count( ret );
454 release_dc_ptr( dc );
455 return ret;
459 /***********************************************************************
460 * BRUSH_DeleteObject
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 );
469 return TRUE;
473 /***********************************************************************
474 * BRUSH_GetObject
476 static INT BRUSH_GetObject( HGDIOBJ handle, INT count, LPVOID buffer )
478 BRUSHOBJ *brush = GDI_GetObjPtr( handle, OBJ_BRUSH );
480 if (!brush) return 0;
481 if (buffer)
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 );
488 return count;