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