gdi32: Add some brush tests.
[wine] / dlls / gdi32 / brush.c
1 /*
2  * GDI brush objects
3  *
4  * Copyright 1993, 1994  Alexandre Julliard
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include "config.h"
22
23 #include <stdarg.h>
24 #include <string.h>
25
26 #include "windef.h"
27 #include "winbase.h"
28 #include "wingdi.h"
29 #include "gdi_private.h"
30 #include "wine/debug.h"
31
32 WINE_DEFAULT_DEBUG_CHANNEL(gdi);
33
34 /* GDI logical brush object */
35 typedef struct
36 {
37     GDIOBJHDR header;
38     LOGBRUSH  logbrush;
39 } BRUSHOBJ;
40
41 #define NB_HATCH_STYLES  6
42
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 );
46
47 static const struct gdi_obj_funcs brush_funcs =
48 {
49     BRUSH_SelectObject,  /* pSelectObject */
50     BRUSH_GetObject,     /* pGetObjectA */
51     BRUSH_GetObject,     /* pGetObjectW */
52     NULL,                /* pUnrealizeObject */
53     BRUSH_DeleteObject   /* pDeleteObject */
54 };
55
56 static HGLOBAL dib_copy(const BITMAPINFO *info, UINT coloruse)
57 {
58     BITMAPINFO  *newInfo;
59     HGLOBAL     hmem;
60     INT         size;
61
62     if (info->bmiHeader.biCompression != BI_RGB && info->bmiHeader.biCompression != BI_BITFIELDS)
63         size = info->bmiHeader.biSizeImage;
64     else
65         size = get_dib_image_size(info);
66     size += bitmap_info_size( info, coloruse );
67
68     if (!(hmem = GlobalAlloc( GMEM_MOVEABLE, size )))
69     {
70         return 0;
71     }
72     newInfo = GlobalLock( hmem );
73     memcpy( newInfo, info, size );
74     GlobalUnlock( hmem );
75     return hmem;
76 }
77
78
79 /***********************************************************************
80  *           CreateBrushIndirect    (GDI32.@)
81  *
82  * Create a logical brush with a given style, color or pattern.
83  *
84  * PARAMS
85  *  brush [I] Pointer to a LOGBRUSH structure describing the desired brush.
86  *
87  * RETURNS
88  *  A handle to the created brush, or a NULL handle if the brush cannot be 
89  *  created.
90  *
91  * NOTES
92  * - The brush returned should be freed by the caller using DeleteObject()
93  *   when it is no longer required.
94  * - Windows 95 and earlier cannot create brushes from bitmaps or DIBs larger
95  *   than 8x8 pixels. If a larger bitmap is given, only a portion of the bitmap
96  *   is used.
97  */
98 HBRUSH WINAPI CreateBrushIndirect( const LOGBRUSH * brush )
99 {
100     BRUSHOBJ * ptr;
101     HBRUSH hbrush;
102
103     if (!(ptr = HeapAlloc( GetProcessHeap(), 0, sizeof(*ptr) ))) return 0;
104
105     ptr->logbrush.lbStyle = brush->lbStyle;
106     ptr->logbrush.lbColor = brush->lbColor;
107     ptr->logbrush.lbHatch = brush->lbHatch;
108
109     switch (ptr->logbrush.lbStyle)
110     {
111     case BS_SOLID:
112     case BS_HOLLOW:
113     case BS_HATCHED:
114         break;
115
116     case BS_PATTERN8X8:
117         ptr->logbrush.lbStyle = BS_PATTERN;
118         /* fall through */
119     case BS_PATTERN:
120         ptr->logbrush.lbHatch = (ULONG_PTR)BITMAP_CopyBitmap( (HBITMAP) ptr->logbrush.lbHatch );
121         ptr->logbrush.lbColor = 0;
122         if (!ptr->logbrush.lbHatch) goto error;
123         break;
124
125     case BS_DIBPATTERNPT:
126         ptr->logbrush.lbStyle = BS_DIBPATTERN;
127         ptr->logbrush.lbHatch = (ULONG_PTR)dib_copy( (BITMAPINFO *) ptr->logbrush.lbHatch,
128                                                      ptr->logbrush.lbColor);
129         if (!ptr->logbrush.lbHatch) goto error;
130         break;
131
132     case BS_DIBPATTERN:
133        {
134             BITMAPINFO* bmi;
135             HGLOBAL h = (HGLOBAL)ptr->logbrush.lbHatch;
136
137             ptr->logbrush.lbStyle = BS_DIBPATTERN;
138             if (!(bmi = GlobalLock( h ))) goto error;
139             ptr->logbrush.lbHatch = (ULONG_PTR)dib_copy( bmi, ptr->logbrush.lbColor);
140             GlobalUnlock( h );
141             if (!ptr->logbrush.lbHatch) goto error;
142             break;
143        }
144
145     case BS_DIBPATTERN8X8:
146     case BS_MONOPATTERN:
147     case BS_INDEXED:
148     default:
149         WARN( "invalid brush style %u\n", ptr->logbrush.lbStyle );
150         goto error;
151     }
152
153     if ((hbrush = alloc_gdi_handle( &ptr->header, OBJ_BRUSH, &brush_funcs )))
154     {
155         TRACE("%p\n", hbrush);
156         return hbrush;
157     }
158
159  error:
160     if (ptr->logbrush.lbHatch)
161     {
162         if (ptr->logbrush.lbStyle == BS_PATTERN)
163             DeleteObject( (HGDIOBJ)ptr->logbrush.lbHatch );
164         else if (ptr->logbrush.lbStyle == BS_DIBPATTERN)
165             GlobalFree( (HGLOBAL)ptr->logbrush.lbHatch );
166     }
167     HeapFree( GetProcessHeap(), 0, ptr );
168     return 0;
169 }
170
171
172 /***********************************************************************
173  *           CreateHatchBrush    (GDI32.@)
174  *
175  * Create a logical brush with a hatched pattern.
176  *
177  * PARAMS
178  *  style [I] Direction of lines for the hatch pattern (HS_* values from "wingdi.h")
179  *  color [I] Colour of the hatched pattern
180  *
181  * RETURNS
182  *  A handle to the created brush, or a NULL handle if the brush cannot
183  *  be created.
184  *
185  * NOTES
186  * - This function uses CreateBrushIndirect() to create the brush.
187  * - The brush returned should be freed by the caller using DeleteObject()
188  *   when it is no longer required.
189  */
190 HBRUSH WINAPI CreateHatchBrush( INT style, COLORREF color )
191 {
192     LOGBRUSH logbrush;
193
194     TRACE("%d %06x\n", style, color );
195
196     logbrush.lbStyle = BS_HATCHED;
197     logbrush.lbColor = color;
198     logbrush.lbHatch = style;
199
200     return CreateBrushIndirect( &logbrush );
201 }
202
203
204 /***********************************************************************
205  *           CreatePatternBrush    (GDI32.@)
206  *
207  * Create a logical brush with a pattern from a bitmap.
208  *
209  * PARAMS
210  *  hbitmap  [I] Bitmap containing pattern for the brush
211  *
212  * RETURNS
213  *  A handle to the created brush, or a NULL handle if the brush cannot 
214  *  be created.
215  *
216  * NOTES
217  * - This function uses CreateBrushIndirect() to create the brush.
218  * - The brush returned should be freed by the caller using DeleteObject()
219  *   when it is no longer required.
220  */
221 HBRUSH WINAPI CreatePatternBrush( HBITMAP hbitmap )
222 {
223     LOGBRUSH logbrush = { BS_PATTERN, 0, 0 };
224     TRACE("%p\n", hbitmap );
225
226     logbrush.lbHatch = (ULONG_PTR)hbitmap;
227     return CreateBrushIndirect( &logbrush );
228 }
229
230
231 /***********************************************************************
232  *           CreateDIBPatternBrush    (GDI32.@)
233  *
234  * Create a logical brush with a pattern from a DIB.
235  *
236  * PARAMS
237  *  hbitmap  [I] Global object containing BITMAPINFO structure for the pattern
238  *  coloruse [I] Specifies color format, if provided
239  *
240  * RETURNS
241  *  A handle to the created brush, or a NULL handle if the brush cannot 
242  *  be created.
243  *
244  * NOTES
245  * - This function uses CreateBrushIndirect() to create the brush.
246  * - The brush returned should be freed by the caller using DeleteObject()
247  *   when it is no longer required.
248  * - This function is for compatibility only. CreateDIBPatternBrushPt() should 
249  *   be used instead.
250  */
251 HBRUSH WINAPI CreateDIBPatternBrush( HGLOBAL hbitmap, UINT coloruse )
252 {
253     LOGBRUSH logbrush;
254
255     TRACE("%p\n", hbitmap );
256
257     logbrush.lbStyle = BS_DIBPATTERN;
258     logbrush.lbColor = coloruse;
259
260     logbrush.lbHatch = (ULONG_PTR)hbitmap;
261
262     return CreateBrushIndirect( &logbrush );
263 }
264
265
266 /***********************************************************************
267  *           CreateDIBPatternBrushPt    (GDI32.@)
268  *
269  * Create a logical brush with a pattern from a DIB.
270  *
271  * PARAMS
272  *  data     [I] Pointer to a BITMAPINFO structure and image data  for the pattern
273  *  coloruse [I] Specifies color format, if provided
274  *
275  * RETURNS
276  *  A handle to the created brush, or a NULL handle if the brush cannot
277  *  be created.
278  *
279  * NOTES
280  * - This function uses CreateBrushIndirect() to create the brush.
281  * - The brush returned should be freed by the caller using DeleteObject()
282  *   when it is no longer required.
283  */
284 HBRUSH WINAPI CreateDIBPatternBrushPt( const void* data, UINT coloruse )
285 {
286     const BITMAPINFO *info=data;
287     LOGBRUSH logbrush;
288
289     if (!data)
290         return NULL;
291
292     TRACE("%p %dx%d %dbpp\n", info, info->bmiHeader.biWidth,
293           info->bmiHeader.biHeight,  info->bmiHeader.biBitCount);
294
295     logbrush.lbStyle = BS_DIBPATTERNPT;
296     logbrush.lbColor = coloruse;
297     logbrush.lbHatch = (ULONG_PTR)data;
298
299     return CreateBrushIndirect( &logbrush );
300 }
301
302
303 /***********************************************************************
304  *           CreateSolidBrush    (GDI32.@)
305  *
306  * Create a logical brush consisting of a single colour.
307  *
308  * PARAMS
309  *  color [I] Colour to make the solid brush
310  *
311  * RETURNS
312  *  A handle to the newly created brush, or a NULL handle if the brush cannot
313  *  be created.
314  *
315  * NOTES
316  * - This function uses CreateBrushIndirect() to create the brush.
317  * - The brush returned should be freed by the caller using DeleteObject()
318  *   when it is no longer required.
319  */
320 HBRUSH WINAPI CreateSolidBrush( COLORREF color )
321 {
322     LOGBRUSH logbrush;
323
324     TRACE("%06x\n", color );
325
326     logbrush.lbStyle = BS_SOLID;
327     logbrush.lbColor = color;
328     logbrush.lbHatch = 0;
329
330     return CreateBrushIndirect( &logbrush );
331 }
332
333
334 /***********************************************************************
335  *           SetBrushOrgEx    (GDI32.@)
336  *
337  * Set the brush origin for a device context.
338  *
339  * PARAMS
340  *  hdc    [I] Device context to set the brush origin for
341  *  x      [I] New x origin
342  *  y      [I] New y origin
343  *  oldorg [O] If non NULL, destination for previously set brush origin.
344  *
345  * RETURNS
346  *  Success: TRUE. The origin is set to (x,y), and oldorg is updated if given.
347  */
348 BOOL WINAPI SetBrushOrgEx( HDC hdc, INT x, INT y, LPPOINT oldorg )
349 {
350     DC *dc = get_dc_ptr( hdc );
351
352     if (!dc) return FALSE;
353     if (oldorg)
354     {
355         oldorg->x = dc->brushOrgX;
356         oldorg->y = dc->brushOrgY;
357     }
358     dc->brushOrgX = x;
359     dc->brushOrgY = y;
360     release_dc_ptr( dc );
361     return TRUE;
362 }
363
364 /***********************************************************************
365  *           FixBrushOrgEx    (GDI32.@)
366  *
367  * See SetBrushOrgEx.
368  *
369  * NOTES
370  *  This function is no longer documented by MSDN, but in Win95 GDI32 it
371  *  is the same as SetBrushOrgEx().
372  */
373 BOOL WINAPI FixBrushOrgEx( HDC hdc, INT x, INT y, LPPOINT oldorg )
374 {
375     return SetBrushOrgEx(hdc,x,y,oldorg);
376 }
377
378
379 /***********************************************************************
380  *           BRUSH_SelectObject
381  */
382 static HGDIOBJ BRUSH_SelectObject( HGDIOBJ handle, HDC hdc )
383 {
384     BRUSHOBJ *brush;
385     HGDIOBJ ret = 0;
386     DC *dc = get_dc_ptr( hdc );
387
388     if (!dc)
389     {
390         SetLastError( ERROR_INVALID_HANDLE );
391         return 0;
392     }
393
394     if ((brush = GDI_GetObjPtr( handle, OBJ_BRUSH )))
395     {
396         PHYSDEV physdev = GET_DC_PHYSDEV( dc, pSelectBrush );
397
398         if (brush->logbrush.lbStyle == BS_PATTERN)
399         {
400             PHYSDEV pattern_dev = physdev;
401             /* FIXME: This will go away once the dib driver implements
402                pattern brushes */
403             if(pattern_dev == dc->dibdrv)
404                 pattern_dev = GET_NEXT_PHYSDEV( physdev, pSelectBrush );
405
406             BITMAP_SetOwnerDC( (HBITMAP)brush->logbrush.lbHatch, pattern_dev );
407         }
408
409         GDI_inc_ref_count( handle );
410         GDI_ReleaseObj( handle );
411
412         if (!physdev->funcs->pSelectBrush( physdev, handle ))
413         {
414             GDI_dec_ref_count( handle );
415         }
416         else
417         {
418             ret = dc->hBrush;
419             dc->hBrush = handle;
420             GDI_dec_ref_count( ret );
421         }
422     }
423     release_dc_ptr( dc );
424     return ret;
425 }
426
427
428 /***********************************************************************
429  *           BRUSH_DeleteObject
430  */
431 static BOOL BRUSH_DeleteObject( HGDIOBJ handle )
432 {
433     BRUSHOBJ *brush = free_gdi_handle( handle );
434
435     if (!brush) return FALSE;
436     switch(brush->logbrush.lbStyle)
437     {
438       case BS_PATTERN:
439           DeleteObject( (HGDIOBJ)brush->logbrush.lbHatch );
440           break;
441       case BS_DIBPATTERN:
442           GlobalFree( (HGLOBAL)brush->logbrush.lbHatch );
443           break;
444     }
445     return HeapFree( GetProcessHeap(), 0, brush );
446 }
447
448
449 /***********************************************************************
450  *           BRUSH_GetObject
451  */
452 static INT BRUSH_GetObject( HGDIOBJ handle, INT count, LPVOID buffer )
453 {
454     BRUSHOBJ *brush = GDI_GetObjPtr( handle, OBJ_BRUSH );
455
456     if (!brush) return 0;
457     if (buffer)
458     {
459         if (count > sizeof(brush->logbrush)) count = sizeof(brush->logbrush);
460         memcpy( buffer, &brush->logbrush, count );
461     }
462     else count = sizeof(brush->logbrush);
463     GDI_ReleaseObj( handle );
464     return count;
465 }