Replaced X11DRV_CritSection references by wine_tsx11_(un)lock.
[wine.git] / objects / brush.c
blob41c3a7c7031d27b8d6eb7547c29074972fad5756
1 /*
2 * GDI brush objects
4 * Copyright 1993, 1994 Alexandre Julliard
5 */
7 #include <string.h>
8 #include "winbase.h"
9 #include "brush.h"
10 #include "bitmap.h"
11 #include "debugtools.h"
13 DEFAULT_DEBUG_CHANNEL(gdi);
15 static HGLOBAL16 dib_copy(BITMAPINFO *info, UINT coloruse)
17 BITMAPINFO *newInfo;
18 HGLOBAL16 hmem;
19 INT size;
21 if (info->bmiHeader.biCompression)
22 size = info->bmiHeader.biSizeImage;
23 else
24 size = DIB_GetDIBImageBytes(info->bmiHeader.biWidth,
25 info->bmiHeader.biHeight,
26 info->bmiHeader.biBitCount);
27 size += DIB_BitmapInfoSize( info, coloruse );
29 if (!(hmem = GlobalAlloc16( GMEM_MOVEABLE, size )))
31 return 0;
33 newInfo = (BITMAPINFO *) GlobalLock16( hmem );
34 memcpy( newInfo, info, size );
35 GlobalUnlock16( hmem );
36 return hmem;
41 static BOOL create_brush_indirect(BRUSHOBJ *brushPtr, BOOL v16)
43 LOGBRUSH *brush = &brushPtr->logbrush;
45 switch (brush->lbStyle)
47 case BS_PATTERN8X8:
48 brush->lbStyle = BS_PATTERN;
49 case BS_PATTERN:
50 brush->lbHatch = (LONG)BITMAP_CopyBitmap( (HBITMAP) brush->lbHatch );
51 if (! brush->lbHatch)
52 break;
53 return TRUE;
55 case BS_DIBPATTERNPT:
56 brush->lbStyle = BS_DIBPATTERN;
57 brush->lbHatch = (LONG)dib_copy( (BITMAPINFO *) brush->lbHatch,
58 brush->lbColor);
59 if (! brush->lbHatch)
60 break;
61 return TRUE;
63 case BS_DIBPATTERN8X8:
64 case BS_DIBPATTERN:
66 BITMAPINFO* bmi;
67 HGLOBAL h = brush->lbHatch;
69 brush->lbStyle = BS_DIBPATTERN;
70 if (v16)
72 if (!(bmi = (BITMAPINFO *)GlobalLock16( h )))
73 break;
75 else
77 if (!(bmi = (BITMAPINFO *)GlobalLock( h )))
78 break;
81 brush->lbHatch = dib_copy( bmi, brush->lbColor);
83 if (v16) GlobalUnlock16( h );
84 else GlobalUnlock( h );
86 if (!brush->lbHatch)
87 break;
89 return TRUE;
92 default:
93 if( brush->lbStyle <= BS_MONOPATTERN)
94 return TRUE;
97 return FALSE;
101 /***********************************************************************
102 * CreateBrushIndirect16 (GDI.50)
104 HBRUSH16 WINAPI CreateBrushIndirect16( const LOGBRUSH16 * brush )
106 BOOL success;
107 BRUSHOBJ * brushPtr;
108 HBRUSH hbrush;
110 if (!(brushPtr = GDI_AllocObject( sizeof(BRUSHOBJ), BRUSH_MAGIC, &hbrush ))) return 0;
111 brushPtr->logbrush.lbStyle = brush->lbStyle;
112 brushPtr->logbrush.lbColor = brush->lbColor;
113 brushPtr->logbrush.lbHatch = brush->lbHatch;
114 success = create_brush_indirect(brushPtr, TRUE);
115 if(!success)
117 GDI_FreeObject( hbrush, brushPtr );
118 hbrush = 0;
120 else GDI_ReleaseObj( hbrush );
121 TRACE("%04x\n", hbrush);
122 return hbrush;
126 /***********************************************************************
127 * CreateBrushIndirect (GDI32.27)
129 * BUGS
130 * As for Windows 95 and Windows 98:
131 * Creating brushes from bitmaps or DIBs larger than 8x8 pixels
132 * is not supported. If a larger bitmap is given, only a portion
133 * of the bitmap is used.
135 HBRUSH WINAPI CreateBrushIndirect( const LOGBRUSH * brush )
137 BOOL success;
138 BRUSHOBJ * brushPtr;
139 HBRUSH hbrush;
140 if (!(brushPtr = GDI_AllocObject( sizeof(BRUSHOBJ), BRUSH_MAGIC, &hbrush ))) return 0;
141 brushPtr->logbrush.lbStyle = brush->lbStyle;
142 brushPtr->logbrush.lbColor = brush->lbColor;
143 brushPtr->logbrush.lbHatch = brush->lbHatch;
144 success = create_brush_indirect(brushPtr, FALSE);
145 if(!success)
147 GDI_FreeObject( hbrush, brushPtr );
148 hbrush = 0;
150 else GDI_ReleaseObj( hbrush );
151 TRACE("%08x\n", hbrush);
152 return hbrush;
156 /***********************************************************************
157 * CreateHatchBrush16 (GDI.58)
159 HBRUSH16 WINAPI CreateHatchBrush16( INT16 style, COLORREF color )
161 return CreateHatchBrush( style, color );
165 /***********************************************************************
166 * CreateHatchBrush (GDI32.48)
168 HBRUSH WINAPI CreateHatchBrush( INT style, COLORREF color )
170 LOGBRUSH logbrush;
172 TRACE("%d %06lx\n", style, color );
174 logbrush.lbStyle = BS_HATCHED;
175 logbrush.lbColor = color;
176 logbrush.lbHatch = style;
178 return CreateBrushIndirect( &logbrush );
182 /***********************************************************************
183 * CreatePatternBrush16 (GDI.60)
185 HBRUSH16 WINAPI CreatePatternBrush16( HBITMAP16 hbitmap )
187 return (HBRUSH16)CreatePatternBrush( hbitmap );
191 /***********************************************************************
192 * CreatePatternBrush (GDI32.54)
194 HBRUSH WINAPI CreatePatternBrush( HBITMAP hbitmap )
196 LOGBRUSH logbrush = { BS_PATTERN, 0, 0 };
197 TRACE("%04x\n", hbitmap );
199 logbrush.lbHatch = hbitmap;
200 return CreateBrushIndirect( &logbrush );
204 /***********************************************************************
205 * CreateDIBPatternBrush16 (GDI.445)
207 HBRUSH16 WINAPI CreateDIBPatternBrush16( HGLOBAL16 hbitmap, UINT16 coloruse )
209 LOGBRUSH16 logbrush;
211 TRACE("%04x\n", hbitmap );
213 logbrush.lbStyle = BS_DIBPATTERN;
214 logbrush.lbColor = coloruse;
215 logbrush.lbHatch = hbitmap;
217 return CreateBrushIndirect16( &logbrush );
221 /***********************************************************************
222 * CreateDIBPatternBrush (GDI32.34)
224 * Create a logical brush which has the pattern specified by the DIB
226 * Function call is for compatibility only. CreateDIBPatternBrushPt should be used.
228 * RETURNS
230 * Handle to a logical brush on success, NULL on failure.
232 * BUGS
235 HBRUSH WINAPI CreateDIBPatternBrush(
236 HGLOBAL hbitmap, /* [in] Global object containg BITMAPINFO structure */
237 UINT coloruse /* [in] Specifies color format, if provided */
240 LOGBRUSH logbrush;
242 TRACE("%04x\n", hbitmap );
244 logbrush.lbStyle = BS_DIBPATTERN;
245 logbrush.lbColor = coloruse;
247 logbrush.lbHatch = (LONG)hbitmap;
249 return CreateBrushIndirect( &logbrush );
253 /***********************************************************************
254 * CreateDIBPatternBrushPt (GDI32.35)
256 * Create a logical brush which has the pattern specified by the DIB
258 * RETURNS
260 * Handle to a logical brush on success, NULL on failure.
262 * BUGS
265 HBRUSH WINAPI CreateDIBPatternBrushPt(
266 const void* data, /* [in] Pointer to a BITMAPINFO structure followed by more data */
267 UINT coloruse /* [in] Specifies color format, if provided */
270 BITMAPINFO *info=(BITMAPINFO*)data;
271 LOGBRUSH logbrush;
273 TRACE("%p %ldx%ld %dbpp\n", info, info->bmiHeader.biWidth,
274 info->bmiHeader.biHeight, info->bmiHeader.biBitCount);
276 logbrush.lbStyle = BS_DIBPATTERNPT;
277 logbrush.lbColor = coloruse;
278 logbrush.lbHatch = (LONG) data;
280 return CreateBrushIndirect( &logbrush );
284 /***********************************************************************
285 * CreateSolidBrush (GDI.66)
287 HBRUSH16 WINAPI CreateSolidBrush16( COLORREF color )
289 return CreateSolidBrush( color );
293 /***********************************************************************
294 * CreateSolidBrush (GDI32.64)
296 HBRUSH WINAPI CreateSolidBrush( COLORREF color )
298 LOGBRUSH logbrush;
300 TRACE("%06lx\n", color );
302 logbrush.lbStyle = BS_SOLID;
303 logbrush.lbColor = color;
304 logbrush.lbHatch = 0;
306 return CreateBrushIndirect( &logbrush );
310 /***********************************************************************
311 * SetBrushOrg (GDI.148)
313 DWORD WINAPI SetBrushOrg16( HDC16 hdc, INT16 x, INT16 y )
315 DWORD retval;
316 DC *dc = DC_GetDCPtr( hdc );
317 if (!dc) return FALSE;
318 retval = dc->brushOrgX | (dc->brushOrgY << 16);
319 dc->brushOrgX = x;
320 dc->brushOrgY = y;
321 GDI_ReleaseObj( hdc );
322 return retval;
326 /***********************************************************************
327 * SetBrushOrgEx (GDI32.308)
329 BOOL WINAPI SetBrushOrgEx( HDC hdc, INT x, INT y, LPPOINT oldorg )
331 DC *dc = DC_GetDCPtr( hdc );
333 if (!dc) return FALSE;
334 if (oldorg)
336 oldorg->x = dc->brushOrgX;
337 oldorg->y = dc->brushOrgY;
339 dc->brushOrgX = x;
340 dc->brushOrgY = y;
341 GDI_ReleaseObj( hdc );
342 return TRUE;
345 /***********************************************************************
346 * FixBrushOrgEx (GDI32.102)
347 * SDK says discontinued, but in Win95 GDI32 this is the same as SetBrushOrgEx
349 BOOL WINAPI FixBrushOrgEx( HDC hdc, INT x, INT y, LPPOINT oldorg )
351 return SetBrushOrgEx(hdc,x,y,oldorg);
355 /***********************************************************************
356 * BRUSH_DeleteObject
358 BOOL BRUSH_DeleteObject( HBRUSH16 hbrush, BRUSHOBJ * brush )
360 switch(brush->logbrush.lbStyle)
362 case BS_PATTERN:
363 DeleteObject( (HGDIOBJ)brush->logbrush.lbHatch );
364 break;
365 case BS_DIBPATTERN:
366 GlobalFree16( (HGLOBAL16)brush->logbrush.lbHatch );
367 break;
369 return GDI_FreeObject( hbrush, brush );
373 /***********************************************************************
374 * BRUSH_GetObject16
376 INT16 BRUSH_GetObject16( BRUSHOBJ * brush, INT16 count, LPSTR buffer )
378 LOGBRUSH16 logbrush;
380 logbrush.lbStyle = brush->logbrush.lbStyle;
381 logbrush.lbColor = brush->logbrush.lbColor;
382 logbrush.lbHatch = brush->logbrush.lbHatch;
383 if (count > sizeof(logbrush)) count = sizeof(logbrush);
384 memcpy( buffer, &logbrush, count );
385 return count;
389 /***********************************************************************
390 * BRUSH_GetObject
392 INT BRUSH_GetObject( BRUSHOBJ * brush, INT count, LPSTR buffer )
394 if (count > sizeof(brush->logbrush)) count = sizeof(brush->logbrush);
395 memcpy( buffer, &brush->logbrush, count );
396 return count;
400 /***********************************************************************
401 * SetSolidBrush16 (GDI.604)
403 * If hBrush is a solid brush, change it's color to newColor.
405 * RETURNS
406 * TRUE on success, FALSE on failure.
407 * FIXME: not yet implemented!
409 BOOL16 WINAPI SetSolidBrush16(HBRUSH16 hBrush, COLORREF newColor )
411 FIXME("(hBrush %04x, newColor %04x): stub!\n", hBrush, (int)newColor);
413 return(FALSE);