gdi32: Recompute the pen masks on every use to support PALETTEINDEX colors.
[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     HBITMAP               bitmap;   /* bitmap handle for DDB pattern brushes */
40     BITMAPINFO           *info;     /* DIB info for pattern brushes */
41     struct gdi_image_bits bits;     /* DIB bits for pattern brushes */
42     UINT                  usage;    /* color usage for DIB info */
43 } BRUSHOBJ;
44
45 #define NB_HATCH_STYLES  6
46
47 static HGDIOBJ BRUSH_SelectObject( HGDIOBJ handle, HDC hdc );
48 static INT BRUSH_GetObject( HGDIOBJ handle, INT count, LPVOID buffer );
49 static BOOL BRUSH_DeleteObject( HGDIOBJ handle );
50
51 static const struct gdi_obj_funcs brush_funcs =
52 {
53     BRUSH_SelectObject,  /* pSelectObject */
54     BRUSH_GetObject,     /* pGetObjectA */
55     BRUSH_GetObject,     /* pGetObjectW */
56     NULL,                /* pUnrealizeObject */
57     BRUSH_DeleteObject   /* pDeleteObject */
58 };
59
60
61 /* fetch the contents of the brush bitmap and cache them in the brush object */
62 static BOOL store_bitmap_bits( BRUSHOBJ *brush, BITMAPOBJ *bmp )
63 {
64     const struct gdi_dc_funcs *funcs = get_bitmap_funcs( bmp );
65     struct gdi_image_bits bits;
66     struct bitblt_coords src;
67     BITMAPINFO *info;
68
69     if (!(info = HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET( BITMAPINFO, bmiColors[256] ))))
70         return FALSE;
71
72     src.visrect.left   = src.x = 0;
73     src.visrect.top    = src.y = 0;
74     src.visrect.right  = src.width = bmp->bitmap.bmWidth;
75     src.visrect.bottom = src.height = bmp->bitmap.bmHeight;
76     if (funcs->pGetImage( NULL, brush->bitmap, info, &bits, &src ))
77     {
78         HeapFree( GetProcessHeap(), 0, info );
79         return FALSE;
80     }
81
82     /* release the unneeded space */
83     HeapReAlloc( GetProcessHeap(), HEAP_REALLOC_IN_PLACE_ONLY, info,
84                  get_dib_info_size( info, DIB_RGB_COLORS ));
85     brush->info  = info;
86     brush->bits  = bits;
87     brush->usage = DIB_RGB_COLORS;
88     return TRUE;
89 }
90
91 static BOOL copy_bitmap( BRUSHOBJ *brush, HBITMAP bitmap )
92 {
93     BITMAPINFO *info;
94     BITMAPOBJ *bmp = GDI_GetObjPtr( bitmap, OBJ_BITMAP );
95
96     if (!bmp) return FALSE;
97
98     if (!bmp->dib)
99     {
100         if ((brush->bitmap = CreateBitmap( bmp->bitmap.bmWidth, bmp->bitmap.bmHeight,
101                                            bmp->bitmap.bmPlanes, bmp->bitmap.bmBitsPixel, NULL )))
102         {
103             if (bmp->funcs->pCopyBitmap( bitmap, brush->bitmap ))
104             {
105                 BITMAPOBJ *copy = GDI_GetObjPtr( brush->bitmap, OBJ_BITMAP );
106                 copy->funcs = bmp->funcs;
107                 GDI_ReleaseObj( copy );
108             }
109             else
110             {
111                 DeleteObject( brush->bitmap );
112                 brush->bitmap = 0;
113             }
114         }
115         GDI_ReleaseObj( bitmap );
116         return brush->bitmap != 0;
117     }
118
119     info = HeapAlloc( GetProcessHeap(), 0,
120                       get_dib_info_size( (BITMAPINFO *)&bmp->dib->dsBmih, DIB_RGB_COLORS ));
121     if (!info) goto done;
122     info->bmiHeader = bmp->dib->dsBmih;
123     if (info->bmiHeader.biCompression == BI_BITFIELDS)
124         memcpy( &info->bmiHeader + 1, bmp->dib->dsBitfields, sizeof(bmp->dib->dsBitfields) );
125     else if (info->bmiHeader.biClrUsed)
126         memcpy( &info->bmiHeader + 1, bmp->color_table, info->bmiHeader.biClrUsed * sizeof(RGBQUAD) );
127     if (!(brush->bits.ptr = HeapAlloc( GetProcessHeap(), 0, info->bmiHeader.biSizeImage )))
128     {
129         HeapFree( GetProcessHeap(), 0, info );
130         goto done;
131     }
132     memcpy( brush->bits.ptr, bmp->dib->dsBm.bmBits, info->bmiHeader.biSizeImage );
133     brush->bits.is_copy = TRUE;
134     brush->bits.free = free_heap_bits;
135     brush->info = info;
136     brush->usage = DIB_RGB_COLORS;
137
138 done:
139     GDI_ReleaseObj( bitmap );
140     return brush->info != NULL;
141 }
142
143 BOOL get_brush_bitmap_info( HBRUSH handle, BITMAPINFO *info, void **bits, UINT *usage )
144 {
145     BRUSHOBJ *brush;
146     BOOL ret = FALSE;
147
148     if (!(brush = GDI_GetObjPtr( handle, OBJ_BRUSH ))) return FALSE;
149
150     if (!brush->info)
151     {
152         BITMAPOBJ *bmp = GDI_GetObjPtr( brush->bitmap, OBJ_BITMAP );
153
154         if (bmp)
155         {
156             store_bitmap_bits( brush, bmp );
157             GDI_ReleaseObj( brush->bitmap );
158         }
159     }
160     if (brush->info)
161     {
162         memcpy( info, brush->info, get_dib_info_size( brush->info, brush->usage ));
163         if (info->bmiHeader.biBitCount <= 8 && !info->bmiHeader.biClrUsed)
164             fill_default_color_table( info );
165         *bits = brush->bits.ptr;
166         *usage = brush->usage;
167         ret = TRUE;
168     }
169     GDI_ReleaseObj( handle );
170     return ret;
171 }
172
173
174 /***********************************************************************
175  *           CreateBrushIndirect    (GDI32.@)
176  *
177  * Create a logical brush with a given style, color or pattern.
178  *
179  * PARAMS
180  *  brush [I] Pointer to a LOGBRUSH structure describing the desired brush.
181  *
182  * RETURNS
183  *  A handle to the created brush, or a NULL handle if the brush cannot be 
184  *  created.
185  *
186  * NOTES
187  * - The brush returned should be freed by the caller using DeleteObject()
188  *   when it is no longer required.
189  * - Windows 95 and earlier cannot create brushes from bitmaps or DIBs larger
190  *   than 8x8 pixels. If a larger bitmap is given, only a portion of the bitmap
191  *   is used.
192  */
193 HBRUSH WINAPI CreateBrushIndirect( const LOGBRUSH * brush )
194 {
195     BRUSHOBJ * ptr;
196     HBRUSH hbrush;
197     HGLOBAL hmem = 0;
198
199     if (!(ptr = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*ptr) ))) return 0;
200
201     ptr->logbrush = *brush;
202
203     switch (ptr->logbrush.lbStyle)
204     {
205     case BS_SOLID:
206     case BS_HOLLOW:
207     case BS_HATCHED:
208         break;
209
210     case BS_PATTERN8X8:
211         ptr->logbrush.lbStyle = BS_PATTERN;
212         /* fall through */
213     case BS_PATTERN:
214         if (!copy_bitmap( ptr, (HBITMAP)ptr->logbrush.lbHatch )) goto error;
215         ptr->logbrush.lbColor = 0;
216         break;
217
218     case BS_DIBPATTERN:
219         hmem = (HGLOBAL)ptr->logbrush.lbHatch;
220         if (!(ptr->logbrush.lbHatch = (ULONG_PTR)GlobalLock( hmem ))) goto error;
221         /* fall through */
222     case BS_DIBPATTERNPT:
223         ptr->usage = ptr->logbrush.lbColor;
224         ptr->info = copy_packed_dib( (BITMAPINFO *)ptr->logbrush.lbHatch, ptr->usage );
225         if (hmem) GlobalUnlock( hmem );
226         if (!ptr->info) goto error;
227         ptr->bits.ptr = (char *)ptr->info + get_dib_info_size( ptr->info, ptr->usage );
228         ptr->logbrush.lbStyle = BS_DIBPATTERN;
229         ptr->logbrush.lbColor = 0;
230         break;
231
232     case BS_DIBPATTERN8X8:
233     case BS_MONOPATTERN:
234     case BS_INDEXED:
235     default:
236         WARN( "invalid brush style %u\n", ptr->logbrush.lbStyle );
237         goto error;
238     }
239
240     if ((hbrush = alloc_gdi_handle( &ptr->header, OBJ_BRUSH, &brush_funcs )))
241     {
242         TRACE("%p\n", hbrush);
243         return hbrush;
244     }
245
246  error:
247     if (ptr->bitmap) DeleteObject( ptr->bitmap );
248     HeapFree( GetProcessHeap(), 0, ptr->info );
249     HeapFree( GetProcessHeap(), 0, ptr );
250     return 0;
251 }
252
253
254 /***********************************************************************
255  *           CreateHatchBrush    (GDI32.@)
256  *
257  * Create a logical brush with a hatched pattern.
258  *
259  * PARAMS
260  *  style [I] Direction of lines for the hatch pattern (HS_* values from "wingdi.h")
261  *  color [I] Colour of the hatched pattern
262  *
263  * RETURNS
264  *  A handle to the created brush, or a NULL handle if the brush cannot
265  *  be created.
266  *
267  * NOTES
268  * - This function uses CreateBrushIndirect() to create the brush.
269  * - The brush returned should be freed by the caller using DeleteObject()
270  *   when it is no longer required.
271  */
272 HBRUSH WINAPI CreateHatchBrush( INT style, COLORREF color )
273 {
274     LOGBRUSH logbrush;
275
276     TRACE("%d %06x\n", style, color );
277
278     logbrush.lbStyle = BS_HATCHED;
279     logbrush.lbColor = color;
280     logbrush.lbHatch = style;
281
282     return CreateBrushIndirect( &logbrush );
283 }
284
285
286 /***********************************************************************
287  *           CreatePatternBrush    (GDI32.@)
288  *
289  * Create a logical brush with a pattern from a bitmap.
290  *
291  * PARAMS
292  *  hbitmap  [I] Bitmap containing pattern for the brush
293  *
294  * RETURNS
295  *  A handle to the created brush, or a NULL handle if the brush cannot 
296  *  be created.
297  *
298  * NOTES
299  * - This function uses CreateBrushIndirect() to create the brush.
300  * - The brush returned should be freed by the caller using DeleteObject()
301  *   when it is no longer required.
302  */
303 HBRUSH WINAPI CreatePatternBrush( HBITMAP hbitmap )
304 {
305     LOGBRUSH logbrush = { BS_PATTERN, 0, 0 };
306     TRACE("%p\n", hbitmap );
307
308     logbrush.lbHatch = (ULONG_PTR)hbitmap;
309     return CreateBrushIndirect( &logbrush );
310 }
311
312
313 /***********************************************************************
314  *           CreateDIBPatternBrush    (GDI32.@)
315  *
316  * Create a logical brush with a pattern from a DIB.
317  *
318  * PARAMS
319  *  hbitmap  [I] Global object containing BITMAPINFO structure for the pattern
320  *  coloruse [I] Specifies color format, if provided
321  *
322  * RETURNS
323  *  A handle to the created brush, or a NULL handle if the brush cannot 
324  *  be created.
325  *
326  * NOTES
327  * - This function uses CreateBrushIndirect() to create the brush.
328  * - The brush returned should be freed by the caller using DeleteObject()
329  *   when it is no longer required.
330  * - This function is for compatibility only. CreateDIBPatternBrushPt() should 
331  *   be used instead.
332  */
333 HBRUSH WINAPI CreateDIBPatternBrush( HGLOBAL hbitmap, UINT coloruse )
334 {
335     LOGBRUSH logbrush;
336
337     TRACE("%p\n", hbitmap );
338
339     logbrush.lbStyle = BS_DIBPATTERN;
340     logbrush.lbColor = coloruse;
341
342     logbrush.lbHatch = (ULONG_PTR)hbitmap;
343
344     return CreateBrushIndirect( &logbrush );
345 }
346
347
348 /***********************************************************************
349  *           CreateDIBPatternBrushPt    (GDI32.@)
350  *
351  * Create a logical brush with a pattern from a DIB.
352  *
353  * PARAMS
354  *  data     [I] Pointer to a BITMAPINFO structure and image data  for the pattern
355  *  coloruse [I] Specifies color format, if provided
356  *
357  * RETURNS
358  *  A handle to the created brush, or a NULL handle if the brush cannot
359  *  be created.
360  *
361  * NOTES
362  * - This function uses CreateBrushIndirect() to create the brush.
363  * - The brush returned should be freed by the caller using DeleteObject()
364  *   when it is no longer required.
365  */
366 HBRUSH WINAPI CreateDIBPatternBrushPt( const void* data, UINT coloruse )
367 {
368     const BITMAPINFO *info=data;
369     LOGBRUSH logbrush;
370
371     if (!data)
372         return NULL;
373
374     TRACE("%p %dx%d %dbpp\n", info, info->bmiHeader.biWidth,
375           info->bmiHeader.biHeight,  info->bmiHeader.biBitCount);
376
377     logbrush.lbStyle = BS_DIBPATTERNPT;
378     logbrush.lbColor = coloruse;
379     logbrush.lbHatch = (ULONG_PTR)data;
380
381     return CreateBrushIndirect( &logbrush );
382 }
383
384
385 /***********************************************************************
386  *           CreateSolidBrush    (GDI32.@)
387  *
388  * Create a logical brush consisting of a single colour.
389  *
390  * PARAMS
391  *  color [I] Colour to make the solid brush
392  *
393  * RETURNS
394  *  A handle to the newly created brush, or a NULL handle if the brush cannot
395  *  be created.
396  *
397  * NOTES
398  * - This function uses CreateBrushIndirect() to create the brush.
399  * - The brush returned should be freed by the caller using DeleteObject()
400  *   when it is no longer required.
401  */
402 HBRUSH WINAPI CreateSolidBrush( COLORREF color )
403 {
404     LOGBRUSH logbrush;
405
406     TRACE("%06x\n", color );
407
408     logbrush.lbStyle = BS_SOLID;
409     logbrush.lbColor = color;
410     logbrush.lbHatch = 0;
411
412     return CreateBrushIndirect( &logbrush );
413 }
414
415
416 /***********************************************************************
417  *           SetBrushOrgEx    (GDI32.@)
418  *
419  * Set the brush origin for a device context.
420  *
421  * PARAMS
422  *  hdc    [I] Device context to set the brush origin for
423  *  x      [I] New x origin
424  *  y      [I] New y origin
425  *  oldorg [O] If non NULL, destination for previously set brush origin.
426  *
427  * RETURNS
428  *  Success: TRUE. The origin is set to (x,y), and oldorg is updated if given.
429  */
430 BOOL WINAPI SetBrushOrgEx( HDC hdc, INT x, INT y, LPPOINT oldorg )
431 {
432     DC *dc = get_dc_ptr( hdc );
433
434     if (!dc) return FALSE;
435     if (oldorg)
436     {
437         oldorg->x = dc->brushOrgX;
438         oldorg->y = dc->brushOrgY;
439     }
440     dc->brushOrgX = x;
441     dc->brushOrgY = y;
442     release_dc_ptr( dc );
443     return TRUE;
444 }
445
446 /***********************************************************************
447  *           FixBrushOrgEx    (GDI32.@)
448  *
449  * See SetBrushOrgEx.
450  *
451  * NOTES
452  *  This function is no longer documented by MSDN, but in Win95 GDI32 it
453  *  is the same as SetBrushOrgEx().
454  */
455 BOOL WINAPI FixBrushOrgEx( HDC hdc, INT x, INT y, LPPOINT oldorg )
456 {
457     return SetBrushOrgEx(hdc,x,y,oldorg);
458 }
459
460
461 /***********************************************************************
462  *           BRUSH_SelectObject
463  */
464 static HGDIOBJ BRUSH_SelectObject( HGDIOBJ handle, HDC hdc )
465 {
466     BRUSHOBJ *brush;
467     HGDIOBJ ret = 0;
468     DC *dc = get_dc_ptr( hdc );
469
470     if (!dc)
471     {
472         SetLastError( ERROR_INVALID_HANDLE );
473         return 0;
474     }
475
476     if ((brush = GDI_GetObjPtr( handle, OBJ_BRUSH )))
477     {
478         PHYSDEV physdev = GET_DC_PHYSDEV( dc, pSelectBrush );
479         HBITMAP bitmap = brush->bitmap;
480         BITMAPINFO *info;
481         void *bits;
482         UINT usage;
483
484         if (bitmap && !brush->info)
485         {
486             BITMAPOBJ *bmp = GDI_GetObjPtr( bitmap, OBJ_BITMAP );
487             /* fetch the bitmap bits if we are selecting into a different type of DC */
488             if (bmp && bmp->funcs != physdev->funcs) store_bitmap_bits( brush, bmp );
489             GDI_ReleaseObj( bitmap );
490         }
491
492         info   = brush->info;
493         bits   = brush->bits.ptr;
494         usage  = brush->usage;
495         GDI_inc_ref_count( handle );
496         GDI_ReleaseObj( handle );
497
498         if (!physdev->funcs->pSelectBrush( physdev, handle, bitmap, info, bits, usage ))
499         {
500             GDI_dec_ref_count( handle );
501         }
502         else
503         {
504             ret = dc->hBrush;
505             dc->hBrush = handle;
506             GDI_dec_ref_count( ret );
507         }
508     }
509     release_dc_ptr( dc );
510     return ret;
511 }
512
513
514 /***********************************************************************
515  *           BRUSH_DeleteObject
516  */
517 static BOOL BRUSH_DeleteObject( HGDIOBJ handle )
518 {
519     BRUSHOBJ *brush = free_gdi_handle( handle );
520
521     if (!brush) return FALSE;
522     if (brush->bits.free) brush->bits.free( &brush->bits );
523     if (brush->bitmap) DeleteObject( brush->bitmap );
524     HeapFree( GetProcessHeap(), 0, brush->info );
525     return HeapFree( GetProcessHeap(), 0, brush );
526 }
527
528
529 /***********************************************************************
530  *           BRUSH_GetObject
531  */
532 static INT BRUSH_GetObject( HGDIOBJ handle, INT count, LPVOID buffer )
533 {
534     BRUSHOBJ *brush = GDI_GetObjPtr( handle, OBJ_BRUSH );
535
536     if (!brush) return 0;
537     if (buffer)
538     {
539         if (count > sizeof(brush->logbrush)) count = sizeof(brush->logbrush);
540         memcpy( buffer, &brush->logbrush, count );
541     }
542     else count = sizeof(brush->logbrush);
543     GDI_ReleaseObj( handle );
544     return count;
545 }