- Improve LVM_GETITEMRECT values for LVS_ICON style. Now mostly
[wine] / objects / 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include "config.h"
22
23 #include <string.h>
24
25 #include "winbase.h"
26 #include "wingdi.h"
27 #include "wine/wingdi16.h"
28 #include "bitmap.h"
29 #include "wine/debug.h"
30
31 WINE_DEFAULT_DEBUG_CHANNEL(gdi);
32
33 /* GDI logical brush object */
34 typedef struct
35 {
36     GDIOBJHDR header;
37     LOGBRUSH  logbrush;
38 } BRUSHOBJ;
39
40 #define NB_HATCH_STYLES  6
41
42 static HGDIOBJ BRUSH_SelectObject( HGDIOBJ handle, void *obj, HDC hdc );
43 static INT BRUSH_GetObject16( HGDIOBJ handle, void *obj, INT count, LPVOID buffer );
44 static INT BRUSH_GetObject( HGDIOBJ handle, void *obj, INT count, LPVOID buffer );
45 static BOOL BRUSH_DeleteObject( HGDIOBJ handle, void *obj );
46
47 static const struct gdi_obj_funcs brush_funcs =
48 {
49     BRUSH_SelectObject,  /* pSelectObject */
50     BRUSH_GetObject16,   /* pGetObject16 */
51     BRUSH_GetObject,     /* pGetObjectA */
52     BRUSH_GetObject,     /* pGetObjectW */
53     NULL,                /* pUnrealizeObject */
54     BRUSH_DeleteObject   /* pDeleteObject */
55 };
56
57 static HGLOBAL16 dib_copy(BITMAPINFO *info, UINT coloruse)
58 {
59     BITMAPINFO  *newInfo;
60     HGLOBAL16   hmem;
61     INT         size;
62
63     if (info->bmiHeader.biCompression)
64         size = info->bmiHeader.biSizeImage;
65     else
66         size = DIB_GetDIBImageBytes(info->bmiHeader.biWidth,
67                                     info->bmiHeader.biHeight,
68                                     info->bmiHeader.biBitCount);
69     size += DIB_BitmapInfoSize( info, coloruse );
70
71     if (!(hmem = GlobalAlloc16( GMEM_MOVEABLE, size )))
72     {
73         return 0;
74     }
75     newInfo = (BITMAPINFO *) GlobalLock16( hmem );
76     memcpy( newInfo, info, size );
77     GlobalUnlock16( hmem );
78     return hmem;
79 }
80
81
82
83 static BOOL create_brush_indirect(BRUSHOBJ *brushPtr, BOOL v16)
84 {
85     LOGBRUSH *brush = &brushPtr->logbrush;
86
87     switch (brush->lbStyle)
88     {
89        case BS_PATTERN8X8:
90             brush->lbStyle = BS_PATTERN;
91        case BS_PATTERN:
92             brush->lbHatch = (LONG)BITMAP_CopyBitmap( (HBITMAP) brush->lbHatch );
93             if (! brush->lbHatch)
94                break;
95             return TRUE;
96
97        case BS_DIBPATTERNPT:
98             brush->lbStyle = BS_DIBPATTERN;
99             brush->lbHatch = (LONG)dib_copy( (BITMAPINFO *) brush->lbHatch,
100                                              brush->lbColor);
101             if (! brush->lbHatch)
102                break;
103             return TRUE;
104
105        case BS_DIBPATTERN8X8:
106        case BS_DIBPATTERN:
107        {
108             BITMAPINFO* bmi;
109             HGLOBAL     h = brush->lbHatch;
110
111             brush->lbStyle = BS_DIBPATTERN;
112             if (v16)
113             {
114                if (!(bmi = (BITMAPINFO *)GlobalLock16( h )))
115                   break;
116             }
117             else
118             {
119                if (!(bmi = (BITMAPINFO *)GlobalLock( h )))
120                   break;
121             }
122
123             brush->lbHatch = dib_copy( bmi, brush->lbColor);
124
125             if (v16)  GlobalUnlock16( h );
126             else      GlobalUnlock( h );
127
128             if (!brush->lbHatch)
129                break;
130
131             return TRUE;
132        }
133
134        default:
135           if( brush->lbStyle <= BS_MONOPATTERN)
136              return TRUE;
137     }
138
139     return FALSE;
140 }
141
142
143 /***********************************************************************
144  *           CreateBrushIndirect    (GDI.50)
145  */
146 HBRUSH16 WINAPI CreateBrushIndirect16( const LOGBRUSH16 * brush )
147 {
148     BOOL success;
149     BRUSHOBJ * brushPtr;
150     HBRUSH hbrush;
151
152     if (!(brushPtr = GDI_AllocObject( sizeof(BRUSHOBJ), BRUSH_MAGIC, &hbrush, &brush_funcs )))
153         return 0;
154     brushPtr->logbrush.lbStyle = brush->lbStyle;
155     brushPtr->logbrush.lbColor = brush->lbColor;
156     brushPtr->logbrush.lbHatch = brush->lbHatch;
157     success = create_brush_indirect(brushPtr, TRUE);
158     if(!success)
159     {
160        GDI_FreeObject( hbrush, brushPtr );
161        hbrush = 0;
162     }
163     else GDI_ReleaseObj( hbrush );
164     TRACE("%04x\n", hbrush);
165     return hbrush;
166 }
167
168
169 /***********************************************************************
170  *           CreateBrushIndirect    (GDI32.@)
171  *
172  * BUGS
173  *      As for Windows 95 and Windows 98:
174  *      Creating brushes from bitmaps or DIBs larger than 8x8 pixels
175  *      is not supported. If a larger bitmap is given, only a portion
176  *      of the bitmap is used.
177  */
178 HBRUSH WINAPI CreateBrushIndirect( const LOGBRUSH * brush )
179 {
180     BOOL success;
181     BRUSHOBJ * brushPtr;
182     HBRUSH hbrush;
183     if (!(brushPtr = GDI_AllocObject( sizeof(BRUSHOBJ), BRUSH_MAGIC, &hbrush, &brush_funcs )))
184         return 0;
185     brushPtr->logbrush.lbStyle = brush->lbStyle;
186     brushPtr->logbrush.lbColor = brush->lbColor;
187     brushPtr->logbrush.lbHatch = brush->lbHatch;
188     success = create_brush_indirect(brushPtr, FALSE);
189     if(!success)
190     {
191        GDI_FreeObject( hbrush, brushPtr );
192        hbrush = 0;
193     }
194     else GDI_ReleaseObj( hbrush );
195     TRACE("%08x\n", hbrush);
196     return hbrush;
197 }
198
199
200 /***********************************************************************
201  *           CreateHatchBrush    (GDI32.@)
202  */
203 HBRUSH WINAPI CreateHatchBrush( INT style, COLORREF color )
204 {
205     LOGBRUSH logbrush;
206
207     TRACE("%d %06lx\n", style, color );
208
209     logbrush.lbStyle = BS_HATCHED;
210     logbrush.lbColor = color;
211     logbrush.lbHatch = style;
212
213     return CreateBrushIndirect( &logbrush );
214 }
215
216
217 /***********************************************************************
218  *           CreatePatternBrush    (GDI32.@)
219  */
220 HBRUSH WINAPI CreatePatternBrush( HBITMAP hbitmap )
221 {
222     LOGBRUSH logbrush = { BS_PATTERN, 0, 0 };
223     TRACE("%04x\n", hbitmap );
224
225     logbrush.lbHatch = hbitmap;
226     return CreateBrushIndirect( &logbrush );
227 }
228
229
230 /***********************************************************************
231  *           CreateDIBPatternBrush    (GDI.445)
232  */
233 HBRUSH16 WINAPI CreateDIBPatternBrush16( HGLOBAL16 hbitmap, UINT16 coloruse )
234 {
235     LOGBRUSH16 logbrush;
236
237     TRACE("%04x\n", hbitmap );
238
239     logbrush.lbStyle = BS_DIBPATTERN;
240     logbrush.lbColor = coloruse;
241     logbrush.lbHatch = hbitmap;
242
243     return CreateBrushIndirect16( &logbrush );
244 }
245
246
247 /***********************************************************************
248  *           CreateDIBPatternBrush    (GDI32.@)
249  *
250  *      Create a logical brush which has the pattern specified by the DIB
251  *
252  *      Function call is for compatibility only.  CreateDIBPatternBrushPt should be used.
253  *
254  * RETURNS
255  *
256  *      Handle to a logical brush on success, NULL on failure.
257  *
258  * BUGS
259  *
260  */
261 HBRUSH WINAPI CreateDIBPatternBrush(
262                 HGLOBAL hbitmap, /* [in] Global object containg BITMAPINFO structure */
263                 UINT coloruse    /* [in] Specifies color format, if provided */
264 )
265 {
266     LOGBRUSH logbrush;
267
268     TRACE("%04x\n", hbitmap );
269
270     logbrush.lbStyle = BS_DIBPATTERN;
271     logbrush.lbColor = coloruse;
272
273     logbrush.lbHatch = (LONG)hbitmap;
274
275     return CreateBrushIndirect( &logbrush );
276 }
277
278
279 /***********************************************************************
280  *           CreateDIBPatternBrushPt    (GDI32.@)
281  *
282  *      Create a logical brush which has the pattern specified by the DIB
283  *
284  * RETURNS
285  *
286  *      Handle to a logical brush on success, NULL on failure.
287  *
288  * BUGS
289  *
290  */
291 HBRUSH WINAPI CreateDIBPatternBrushPt(
292                 const void* data, /* [in] Pointer to a BITMAPINFO structure followed by more data */
293                 UINT coloruse     /* [in] Specifies color format, if provided */
294 )
295 {
296     BITMAPINFO *info=(BITMAPINFO*)data;
297     LOGBRUSH logbrush;
298
299     TRACE("%p %ldx%ld %dbpp\n", info, info->bmiHeader.biWidth,
300           info->bmiHeader.biHeight,  info->bmiHeader.biBitCount);
301
302     logbrush.lbStyle = BS_DIBPATTERNPT;
303     logbrush.lbColor = coloruse;
304     logbrush.lbHatch = (LONG) data;
305
306     return CreateBrushIndirect( &logbrush );
307 }
308
309
310 /***********************************************************************
311  *           CreateSolidBrush    (GDI32.@)
312  */
313 HBRUSH WINAPI CreateSolidBrush( COLORREF color )
314 {
315     LOGBRUSH logbrush;
316
317     TRACE("%06lx\n", color );
318
319     logbrush.lbStyle = BS_SOLID;
320     logbrush.lbColor = color;
321     logbrush.lbHatch = 0;
322
323     return CreateBrushIndirect( &logbrush );
324 }
325
326
327 /***********************************************************************
328  *           SetBrushOrgEx    (GDI32.@)
329  */
330 BOOL WINAPI SetBrushOrgEx( HDC hdc, INT x, INT y, LPPOINT oldorg )
331 {
332     DC *dc = DC_GetDCPtr( hdc );
333
334     if (!dc) return FALSE;
335     if (oldorg)
336     {
337         oldorg->x = dc->brushOrgX;
338         oldorg->y = dc->brushOrgY;
339     }
340     dc->brushOrgX = x;
341     dc->brushOrgY = y;
342     GDI_ReleaseObj( hdc );
343     return TRUE;
344 }
345
346 /***********************************************************************
347  *           FixBrushOrgEx    (GDI32.@)
348  * SDK says discontinued, but in Win95 GDI32 this is the same as SetBrushOrgEx
349  */
350 BOOL WINAPI FixBrushOrgEx( HDC hdc, INT x, INT y, LPPOINT oldorg )
351 {
352     return SetBrushOrgEx(hdc,x,y,oldorg);
353 }
354
355
356 /***********************************************************************
357  *           BRUSH_SelectObject
358  */
359 static HGDIOBJ BRUSH_SelectObject( HGDIOBJ handle, void *obj, HDC hdc )
360 {
361     BRUSHOBJ *brush = obj;
362     HGDIOBJ ret;
363     DC *dc = DC_GetDCPtr( hdc );
364
365     if (!dc) return 0;
366
367     if (brush->logbrush.lbStyle == BS_PATTERN)
368         BITMAP_SetOwnerDC( (HBITMAP)brush->logbrush.lbHatch, dc );
369
370     ret = dc->hBrush;
371     if (dc->funcs->pSelectBrush) handle = dc->funcs->pSelectBrush( dc->physDev, handle );
372     if (handle) dc->hBrush = handle;
373     else ret = 0;
374     GDI_ReleaseObj( hdc );
375     return ret;
376 }
377
378
379 /***********************************************************************
380  *           BRUSH_DeleteObject
381  */
382 static BOOL BRUSH_DeleteObject( HGDIOBJ handle, void *obj )
383 {
384     BRUSHOBJ *brush = obj;
385
386     switch(brush->logbrush.lbStyle)
387     {
388       case BS_PATTERN:
389           DeleteObject( (HGDIOBJ)brush->logbrush.lbHatch );
390           break;
391       case BS_DIBPATTERN:
392           GlobalFree16( (HGLOBAL16)brush->logbrush.lbHatch );
393           break;
394     }
395     return GDI_FreeObject( handle, obj );
396 }
397
398
399 /***********************************************************************
400  *           BRUSH_GetObject16
401  */
402 static INT BRUSH_GetObject16( HGDIOBJ handle, void *obj, INT count, LPVOID buffer )
403 {
404     BRUSHOBJ *brush = obj;
405     LOGBRUSH16 logbrush;
406
407     logbrush.lbStyle = brush->logbrush.lbStyle;
408     logbrush.lbColor = brush->logbrush.lbColor;
409     logbrush.lbHatch = brush->logbrush.lbHatch;
410     if (count > sizeof(logbrush)) count = sizeof(logbrush);
411     memcpy( buffer, &logbrush, count );
412     return count;
413 }
414
415
416 /***********************************************************************
417  *           BRUSH_GetObject
418  */
419 static INT BRUSH_GetObject( HGDIOBJ handle, void *obj, INT count, LPVOID buffer )
420 {
421     BRUSHOBJ *brush = obj;
422
423     if (count > sizeof(brush->logbrush)) count = sizeof(brush->logbrush);
424     memcpy( buffer, &brush->logbrush, count );
425     return count;
426 }
427
428
429 /***********************************************************************
430  *           SetSolidBrush   (GDI.604)
431  *
432  *  If hBrush is a solid brush, change its color to newColor.
433  *
434  *  RETURNS
435  *           TRUE on success, FALSE on failure.
436  *
437  *  FIXME: untested, not sure if correct.
438  */
439 BOOL16 WINAPI SetSolidBrush16(HBRUSH16 hBrush, COLORREF newColor )
440 {
441     BRUSHOBJ * brushPtr;
442     BOOL16 res = FALSE;
443
444     TRACE("(hBrush %04x, newColor %08lx)\n", hBrush, (DWORD)newColor);
445     if (!(brushPtr = (BRUSHOBJ *) GDI_GetObjPtr( hBrush, BRUSH_MAGIC )))
446         return FALSE;
447
448     if (brushPtr->logbrush.lbStyle == BS_SOLID)
449     {
450         brushPtr->logbrush.lbColor = newColor;
451         res = TRUE;
452     }
453
454      GDI_ReleaseObj( hBrush );
455      return res;
456 }