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 */
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 */
56 static HGLOBAL dib_copy(const BITMAPINFO *info, UINT coloruse)
62 if (info->bmiHeader.biCompression != BI_RGB && info->bmiHeader.biCompression != BI_BITFIELDS)
63 size = info->bmiHeader.biSizeImage;
65 size = DIB_GetDIBImageBytes(info->bmiHeader.biWidth,
66 info->bmiHeader.biHeight,
67 info->bmiHeader.biBitCount);
68 size += bitmap_info_size( info, coloruse );
70 if (!(hmem = GlobalAlloc( GMEM_MOVEABLE, size )))
74 newInfo = GlobalLock( hmem );
75 memcpy( newInfo, info, size );
81 /***********************************************************************
82 * CreateBrushIndirect (GDI32.@)
84 * Create a logical brush with a given style, color or pattern.
87 * brush [I] Pointer to a LOGBRUSH structure describing the desired brush.
90 * A handle to the created brush, or a NULL handle if the brush cannot be
94 * - The brush returned should be freed by the caller using DeleteObject()
95 * when it is no longer required.
96 * - Windows 95 and earlier cannot create brushes from bitmaps or DIBs larger
97 * than 8x8 pixels. If a larger bitmap is given, only a portion of the bitmap
100 HBRUSH WINAPI CreateBrushIndirect( const LOGBRUSH * brush )
105 if (!(ptr = HeapAlloc( GetProcessHeap(), 0, sizeof(*ptr) ))) return 0;
107 ptr->logbrush.lbStyle = brush->lbStyle;
108 ptr->logbrush.lbColor = brush->lbColor;
109 ptr->logbrush.lbHatch = brush->lbHatch;
111 switch (ptr->logbrush.lbStyle)
114 ptr->logbrush.lbStyle = BS_PATTERN;
117 ptr->logbrush.lbHatch = (ULONG_PTR)BITMAP_CopyBitmap( (HBITMAP) ptr->logbrush.lbHatch );
118 if (!ptr->logbrush.lbHatch) goto error;
121 case BS_DIBPATTERNPT:
122 ptr->logbrush.lbStyle = BS_DIBPATTERN;
123 ptr->logbrush.lbHatch = (ULONG_PTR)dib_copy( (BITMAPINFO *) ptr->logbrush.lbHatch,
124 ptr->logbrush.lbColor);
125 if (!ptr->logbrush.lbHatch) goto error;
128 case BS_DIBPATTERN8X8:
132 HGLOBAL h = (HGLOBAL)ptr->logbrush.lbHatch;
134 ptr->logbrush.lbStyle = BS_DIBPATTERN;
135 if (!(bmi = GlobalLock( h ))) goto error;
136 ptr->logbrush.lbHatch = (ULONG_PTR)dib_copy( bmi, ptr->logbrush.lbColor);
138 if (!ptr->logbrush.lbHatch) goto error;
143 if(ptr->logbrush.lbStyle > BS_MONOPATTERN) goto error;
147 if ((hbrush = alloc_gdi_handle( &ptr->header, OBJ_BRUSH, &brush_funcs )))
149 TRACE("%p\n", hbrush);
154 if (ptr->logbrush.lbHatch)
156 if (ptr->logbrush.lbStyle == BS_PATTERN)
157 DeleteObject( (HGDIOBJ)ptr->logbrush.lbHatch );
158 else if (ptr->logbrush.lbStyle == BS_DIBPATTERN)
159 GlobalFree( (HGLOBAL)ptr->logbrush.lbHatch );
161 HeapFree( GetProcessHeap(), 0, ptr );
166 /***********************************************************************
167 * CreateHatchBrush (GDI32.@)
169 * Create a logical brush with a hatched pattern.
172 * style [I] Direction of lines for the hatch pattern (HS_* values from "wingdi.h")
173 * color [I] Colour of the hatched pattern
176 * A handle to the created brush, or a NULL handle if the brush cannot
180 * - This function uses CreateBrushIndirect() to create the brush.
181 * - The brush returned should be freed by the caller using DeleteObject()
182 * when it is no longer required.
184 HBRUSH WINAPI CreateHatchBrush( INT style, COLORREF color )
188 TRACE("%d %06x\n", style, color );
190 logbrush.lbStyle = BS_HATCHED;
191 logbrush.lbColor = color;
192 logbrush.lbHatch = style;
194 return CreateBrushIndirect( &logbrush );
198 /***********************************************************************
199 * CreatePatternBrush (GDI32.@)
201 * Create a logical brush with a pattern from a bitmap.
204 * hbitmap [I] Bitmap containing pattern for the brush
207 * A handle to the created brush, or a NULL handle if the brush cannot
211 * - This function uses CreateBrushIndirect() to create the brush.
212 * - The brush returned should be freed by the caller using DeleteObject()
213 * when it is no longer required.
215 HBRUSH WINAPI CreatePatternBrush( HBITMAP hbitmap )
217 LOGBRUSH logbrush = { BS_PATTERN, 0, 0 };
218 TRACE("%p\n", hbitmap );
220 logbrush.lbHatch = (ULONG_PTR)hbitmap;
221 return CreateBrushIndirect( &logbrush );
225 /***********************************************************************
226 * CreateDIBPatternBrush (GDI32.@)
228 * Create a logical brush with a pattern from a DIB.
231 * hbitmap [I] Global object containing BITMAPINFO structure for the pattern
232 * coloruse [I] Specifies color format, if provided
235 * A handle to the created brush, or a NULL handle if the brush cannot
239 * - This function uses CreateBrushIndirect() to create the brush.
240 * - The brush returned should be freed by the caller using DeleteObject()
241 * when it is no longer required.
242 * - This function is for compatibility only. CreateDIBPatternBrushPt() should
245 HBRUSH WINAPI CreateDIBPatternBrush( HGLOBAL hbitmap, UINT coloruse )
249 TRACE("%p\n", hbitmap );
251 logbrush.lbStyle = BS_DIBPATTERN;
252 logbrush.lbColor = coloruse;
254 logbrush.lbHatch = (ULONG_PTR)hbitmap;
256 return CreateBrushIndirect( &logbrush );
260 /***********************************************************************
261 * CreateDIBPatternBrushPt (GDI32.@)
263 * Create a logical brush with a pattern from a DIB.
266 * data [I] Pointer to a BITMAPINFO structure and image data for the pattern
267 * coloruse [I] Specifies color format, if provided
270 * A handle to the created brush, or a NULL handle if the brush cannot
274 * - This function uses CreateBrushIndirect() to create the brush.
275 * - The brush returned should be freed by the caller using DeleteObject()
276 * when it is no longer required.
278 HBRUSH WINAPI CreateDIBPatternBrushPt( const void* data, UINT coloruse )
280 const BITMAPINFO *info=data;
286 TRACE("%p %dx%d %dbpp\n", info, info->bmiHeader.biWidth,
287 info->bmiHeader.biHeight, info->bmiHeader.biBitCount);
289 logbrush.lbStyle = BS_DIBPATTERNPT;
290 logbrush.lbColor = coloruse;
291 logbrush.lbHatch = (ULONG_PTR)data;
293 return CreateBrushIndirect( &logbrush );
297 /***********************************************************************
298 * CreateSolidBrush (GDI32.@)
300 * Create a logical brush consisting of a single colour.
303 * color [I] Colour to make the solid brush
306 * A handle to the newly created brush, or a NULL handle if the brush cannot
310 * - This function uses CreateBrushIndirect() to create the brush.
311 * - The brush returned should be freed by the caller using DeleteObject()
312 * when it is no longer required.
314 HBRUSH WINAPI CreateSolidBrush( COLORREF color )
318 TRACE("%06x\n", color );
320 logbrush.lbStyle = BS_SOLID;
321 logbrush.lbColor = color;
322 logbrush.lbHatch = 0;
324 return CreateBrushIndirect( &logbrush );
328 /***********************************************************************
329 * SetBrushOrgEx (GDI32.@)
331 * Set the brush origin for a device context.
334 * hdc [I] Device context to set the brush origin for
337 * oldorg [O] If non NULL, destination for previously set brush origin.
340 * Success: TRUE. The origin is set to (x,y), and oldorg is updated if given.
342 BOOL WINAPI SetBrushOrgEx( HDC hdc, INT x, INT y, LPPOINT oldorg )
344 DC *dc = get_dc_ptr( hdc );
346 if (!dc) return FALSE;
349 oldorg->x = dc->brushOrgX;
350 oldorg->y = dc->brushOrgY;
354 release_dc_ptr( dc );
358 /***********************************************************************
359 * FixBrushOrgEx (GDI32.@)
364 * This function is no longer documented by MSDN, but in Win95 GDI32 it
365 * is the same as SetBrushOrgEx().
367 BOOL WINAPI FixBrushOrgEx( HDC hdc, INT x, INT y, LPPOINT oldorg )
369 return SetBrushOrgEx(hdc,x,y,oldorg);
373 /***********************************************************************
376 static HGDIOBJ BRUSH_SelectObject( HGDIOBJ handle, HDC hdc )
380 DC *dc = get_dc_ptr( hdc );
384 SetLastError( ERROR_INVALID_HANDLE );
388 if ((brush = GDI_GetObjPtr( handle, OBJ_BRUSH )))
390 if (brush->logbrush.lbStyle == BS_PATTERN)
391 BITMAP_SetOwnerDC( (HBITMAP)brush->logbrush.lbHatch, dc );
393 GDI_inc_ref_count( handle );
394 GDI_ReleaseObj( handle );
396 if (dc->funcs->pSelectBrush && !dc->funcs->pSelectBrush( dc->physDev, handle ))
398 GDI_dec_ref_count( handle );
404 GDI_dec_ref_count( ret );
407 release_dc_ptr( dc );
412 /***********************************************************************
415 static BOOL BRUSH_DeleteObject( HGDIOBJ handle )
417 BRUSHOBJ *brush = free_gdi_handle( handle );
419 if (!brush) return FALSE;
420 switch(brush->logbrush.lbStyle)
423 DeleteObject( (HGDIOBJ)brush->logbrush.lbHatch );
426 GlobalFree( (HGLOBAL)brush->logbrush.lbHatch );
429 return HeapFree( GetProcessHeap(), 0, brush );
433 /***********************************************************************
436 static INT BRUSH_GetObject( HGDIOBJ handle, INT count, LPVOID buffer )
438 BRUSHOBJ *brush = GDI_GetObjPtr( handle, OBJ_BRUSH );
440 if (!brush) return 0;
443 if (count > sizeof(brush->logbrush)) count = sizeof(brush->logbrush);
444 memcpy( buffer, &brush->logbrush, count );
446 else count = sizeof(brush->logbrush);
447 GDI_ReleaseObj( handle );