cmd: DIR command outputs free space for the path.
[wine.git] / dlls / win32u / brush.c
blob29a96035599475022ba1d04a2655ddba709fe402
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 #if 0
22 #pragma makedep unix
23 #endif
25 #include <stdarg.h>
26 #include <string.h>
28 #include "windef.h"
29 #include "winbase.h"
30 #include "wingdi.h"
31 #include "ntgdi_private.h"
32 #include "wine/debug.h"
34 WINE_DEFAULT_DEBUG_CHANNEL(gdi);
36 /* GDI logical brush object */
37 typedef struct
39 struct gdi_obj_header obj;
40 LOGBRUSH logbrush;
41 struct brush_pattern pattern;
42 } BRUSHOBJ;
44 #define NB_HATCH_STYLES 6
46 static INT BRUSH_GetObject( HGDIOBJ handle, INT count, LPVOID buffer );
47 static BOOL BRUSH_DeleteObject( HGDIOBJ handle );
49 static const struct gdi_obj_funcs brush_funcs =
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, NTGDI_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 = malloc( 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 = malloc( 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 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_DIBPATTERNPT:
123 pattern->usage = brush->lbColor;
124 pattern->info = copy_packed_dib( (BITMAPINFO *)brush->lbHatch, pattern->usage );
125 if (!pattern->info) return FALSE;
126 pattern->bits.ptr = (char *)pattern->info + get_dib_info_size( pattern->info, pattern->usage );
127 brush->lbStyle = BS_DIBPATTERN;
128 brush->lbColor = 0;
129 return TRUE;
131 case BS_DIBPATTERN:
132 case BS_DIBPATTERN8X8:
133 case BS_MONOPATTERN:
134 case BS_INDEXED:
135 default:
136 WARN( "invalid brush style %u\n", brush->lbStyle );
137 return FALSE;
141 void free_brush_pattern( struct brush_pattern *pattern )
143 if (pattern->bits.free) pattern->bits.free( &pattern->bits );
144 free( pattern->info );
147 /**********************************************************************
148 * NtGdiIcmBrushInfo (win32u.@)
150 BOOL WINAPI NtGdiIcmBrushInfo( HDC hdc, HBRUSH handle, BITMAPINFO *info, void *bits,
151 ULONG *bits_size, UINT *usage, BOOL *unk, UINT mode )
153 BRUSHOBJ *brush;
154 BOOL ret = FALSE;
156 if (mode)
158 FIXME( "unsupported mode %u\n", mode );
159 return FALSE;
162 if (!(brush = GDI_GetObjPtr( handle, NTGDI_OBJ_BRUSH ))) return FALSE;
164 if (brush->pattern.info)
166 if (info)
168 memcpy( info, brush->pattern.info,
169 get_dib_info_size( brush->pattern.info, brush->pattern.usage ));
170 if (info->bmiHeader.biBitCount <= 8 && !info->bmiHeader.biClrUsed)
171 fill_default_color_table( info );
172 if (info->bmiHeader.biHeight < 0)
173 info->bmiHeader.biHeight = -info->bmiHeader.biHeight;
175 if (bits)
177 /* always return a bottom-up DIB */
178 if (brush->pattern.info->bmiHeader.biHeight < 0)
180 unsigned int i, width_bytes, height = -brush->pattern.info->bmiHeader.biHeight;
181 char *dst_ptr;
183 width_bytes = get_dib_stride( brush->pattern.info->bmiHeader.biWidth,
184 brush->pattern.info->bmiHeader.biBitCount );
185 dst_ptr = (char *)bits + (height - 1) * width_bytes;
186 for (i = 0; i < height; i++, dst_ptr -= width_bytes)
187 memcpy( dst_ptr, (char *)brush->pattern.bits.ptr + i * width_bytes,
188 width_bytes );
190 else memcpy( bits, brush->pattern.bits.ptr,
191 brush->pattern.info->bmiHeader.biSizeImage );
193 if (bits_size) *bits_size = brush->pattern.info->bmiHeader.biSizeImage;
194 if (usage) *usage = brush->pattern.usage;
195 ret = TRUE;
197 GDI_ReleaseObj( handle );
198 return ret;
202 HBRUSH create_brush( const LOGBRUSH *brush )
204 BRUSHOBJ * ptr;
205 HBRUSH hbrush;
207 if (!(ptr = malloc( sizeof(*ptr) ))) return 0;
209 ptr->logbrush = *brush;
211 if (store_brush_pattern( &ptr->logbrush, &ptr->pattern ) &&
212 (hbrush = alloc_gdi_handle( &ptr->obj, NTGDI_OBJ_BRUSH, &brush_funcs )))
214 TRACE("%p\n", hbrush);
215 return hbrush;
218 free_brush_pattern( &ptr->pattern );
219 free( ptr );
220 return 0;
224 /***********************************************************************
225 * NtGdiCreateHatchBrushInternal (win32u.@)
227 * Create a logical brush with a hatched pattern.
229 HBRUSH WINAPI NtGdiCreateHatchBrushInternal( INT style, COLORREF color, BOOL pen )
231 LOGBRUSH logbrush;
233 TRACE( "%d %s\n", style, debugstr_color(color) );
235 logbrush.lbStyle = BS_HATCHED;
236 logbrush.lbColor = color;
237 logbrush.lbHatch = style;
239 return create_brush( &logbrush );
243 /***********************************************************************
244 * NtGdiCreatePatternBrushInternal (win32u.@)
246 * Create a logical brush with a pattern from a bitmap.
248 HBRUSH WINAPI NtGdiCreatePatternBrushInternal( HBITMAP bitmap, BOOL pen, BOOL is_8x8 )
250 LOGBRUSH logbrush = { BS_PATTERN, 0, 0 };
252 TRACE( "%p\n", bitmap );
254 logbrush.lbHatch = (ULONG_PTR)bitmap;
255 return create_brush( &logbrush );
259 /***********************************************************************
260 * NtGdiCreateDIBBrush (win32u.@)
262 * Create a logical brush with a pattern from a DIB.
264 HBRUSH WINAPI NtGdiCreateDIBBrush( const void *data, UINT coloruse, UINT size,
265 BOOL is_8x8, BOOL pen, const void *client )
267 const BITMAPINFO *info = data;
268 LOGBRUSH logbrush;
270 if (!data)
271 return NULL;
273 TRACE( "%p %dx%d %dbpp\n", info, (int)info->bmiHeader.biWidth,
274 (int)info->bmiHeader.biHeight, (int)info->bmiHeader.biBitCount );
276 logbrush.lbStyle = BS_DIBPATTERNPT;
277 logbrush.lbColor = coloruse;
278 logbrush.lbHatch = (ULONG_PTR)data;
280 return create_brush( &logbrush );
284 /***********************************************************************
285 * NtGdiCreateSolidBrush (win32u.@)
287 * Create a logical brush consisting of a single colour.
289 HBRUSH WINAPI NtGdiCreateSolidBrush( COLORREF color, HBRUSH brush )
291 LOGBRUSH logbrush;
293 TRACE("%s\n", debugstr_color(color) );
295 logbrush.lbStyle = BS_SOLID;
296 logbrush.lbColor = color;
297 logbrush.lbHatch = 0;
299 return create_brush( &logbrush );
303 /***********************************************************************
304 * NtGdiSelectBrush (win32u.@)
306 HGDIOBJ WINAPI NtGdiSelectBrush( HDC hdc, HGDIOBJ handle )
308 BRUSHOBJ *brush;
309 HGDIOBJ ret = 0;
310 DC *dc;
312 if (!(dc = get_dc_ptr( hdc ))) return 0;
314 if ((brush = GDI_GetObjPtr( handle, NTGDI_OBJ_BRUSH )))
316 PHYSDEV physdev = GET_DC_PHYSDEV( dc, pSelectBrush );
317 struct brush_pattern *pattern = &brush->pattern;
319 if (!pattern->info) pattern = NULL;
321 GDI_inc_ref_count( handle );
322 GDI_ReleaseObj( handle );
324 if (!physdev->funcs->pSelectBrush( physdev, handle, pattern ))
326 GDI_dec_ref_count( handle );
328 else
330 ret = dc->hBrush;
331 dc->hBrush = handle;
332 GDI_dec_ref_count( ret );
335 release_dc_ptr( dc );
336 return ret;
340 /***********************************************************************
341 * BRUSH_DeleteObject
343 static BOOL BRUSH_DeleteObject( HGDIOBJ handle )
345 BRUSHOBJ *brush = free_gdi_handle( handle );
347 if (!brush) return FALSE;
348 free_brush_pattern( &brush->pattern );
349 free( brush );
350 return TRUE;
354 /***********************************************************************
355 * BRUSH_GetObject
357 static INT BRUSH_GetObject( HGDIOBJ handle, INT count, LPVOID buffer )
359 BRUSHOBJ *brush = GDI_GetObjPtr( handle, NTGDI_OBJ_BRUSH );
361 if (!brush) return 0;
362 if (buffer)
364 if (count > sizeof(brush->logbrush)) count = sizeof(brush->logbrush);
365 memcpy( buffer, &brush->logbrush, count );
367 else count = sizeof(brush->logbrush);
368 GDI_ReleaseObj( handle );
369 return count;