Fixed bug in DIB_SetImageBits_RLE8 (because 'color' var was WORD, all
[wine] / objects / brush.c
1 /*
2  * GDI brush objects
3  *
4  * Copyright 1993, 1994  Alexandre Julliard
5  */
6
7 #include <stdlib.h>
8 #include "brush.h"
9 #include "bitmap.h"
10 #include "metafile.h"
11 #include "color.h"
12 #include "debug.h"
13
14
15 /***********************************************************************
16  *           CreateBrushIndirect16    (GDI.50)
17  */
18 HBRUSH16 WINAPI CreateBrushIndirect16( const LOGBRUSH16 * brush )
19 {
20     BRUSHOBJ * brushPtr;
21     HBRUSH16 hbrush = GDI_AllocObject( sizeof(BRUSHOBJ), BRUSH_MAGIC );
22     if (!hbrush) return 0;
23     brushPtr = (BRUSHOBJ *) GDI_HEAP_LOCK( hbrush );
24     brushPtr->logbrush.lbStyle = brush->lbStyle;
25     brushPtr->logbrush.lbColor = brush->lbColor;
26     brushPtr->logbrush.lbHatch = brush->lbHatch;
27     GDI_HEAP_UNLOCK( hbrush );
28     return hbrush;
29 }
30
31
32 /***********************************************************************
33  *           CreateBrushIndirect32    (GDI32.27)
34  */
35 HBRUSH32 WINAPI CreateBrushIndirect32( const LOGBRUSH32 * brush )
36 {
37     BRUSHOBJ * brushPtr;
38     HBRUSH32 hbrush = GDI_AllocObject( sizeof(BRUSHOBJ), BRUSH_MAGIC );
39     if (!hbrush) return 0;
40     brushPtr = (BRUSHOBJ *) GDI_HEAP_LOCK( hbrush );
41     brushPtr->logbrush.lbStyle = brush->lbStyle;
42     brushPtr->logbrush.lbColor = brush->lbColor;
43     brushPtr->logbrush.lbHatch = brush->lbHatch;
44     GDI_HEAP_UNLOCK( hbrush );
45     return hbrush;
46 }
47
48
49 /***********************************************************************
50  *           CreateHatchBrush16    (GDI.58)
51  */
52 HBRUSH16 WINAPI CreateHatchBrush16( INT16 style, COLORREF color )
53 {
54     LOGBRUSH32 logbrush = { BS_HATCHED, color, style };
55     TRACE(gdi, "%d %06lx\n", style, color );
56     if ((style < 0) || (style >= NB_HATCH_STYLES)) return 0;
57     return CreateBrushIndirect32( &logbrush );
58 }
59
60
61 /***********************************************************************
62  *           CreateHatchBrush32    (GDI32.48)
63  */
64 HBRUSH32 WINAPI CreateHatchBrush32( INT32 style, COLORREF color )
65 {
66     LOGBRUSH32 logbrush = { BS_HATCHED, color, style };
67     TRACE(gdi, "%d %06lx\n", style, color );
68     if ((style < 0) || (style >= NB_HATCH_STYLES)) return 0;
69     return CreateBrushIndirect32( &logbrush );
70 }
71
72
73 /***********************************************************************
74  *           CreatePatternBrush16    (GDI.60)
75  */
76 HBRUSH16 WINAPI CreatePatternBrush16( HBITMAP16 hbitmap )
77 {
78     return (HBRUSH16)CreatePatternBrush32( hbitmap );
79 }
80
81
82 /***********************************************************************
83  *           CreatePatternBrush32    (GDI32.54)
84  */
85 HBRUSH32 WINAPI CreatePatternBrush32( HBITMAP32 hbitmap )
86 {
87     LOGBRUSH32 logbrush = { BS_PATTERN, 0, 0 };
88     BITMAPOBJ *bmp, *newbmp;
89
90     TRACE(gdi, "%04x\n", hbitmap );
91
92       /* Make a copy of the bitmap */
93
94     if (!(bmp = (BITMAPOBJ *) GDI_GetObjPtr( hbitmap, BITMAP_MAGIC )))
95         return 0;
96     logbrush.lbHatch = (INT32)CreateBitmapIndirect16( &bmp->bitmap );
97     newbmp = (BITMAPOBJ *) GDI_GetObjPtr( (HGDIOBJ32)logbrush.lbHatch,
98                                           BITMAP_MAGIC );
99     if (!newbmp) 
100     {
101       GDI_HEAP_UNLOCK( hbitmap );
102       return 0;
103     }
104     TSXCopyArea( display, bmp->pixmap, newbmp->pixmap, BITMAP_GC(bmp),
105                0, 0, bmp->bitmap.bmWidth, bmp->bitmap.bmHeight, 0, 0 );
106     GDI_HEAP_UNLOCK( hbitmap );
107     GDI_HEAP_UNLOCK( logbrush.lbHatch );
108     return CreateBrushIndirect32( &logbrush );
109 }
110
111
112 /***********************************************************************
113  *           CreateDIBPatternBrush16    (GDI.445)
114  */
115 HBRUSH16 WINAPI CreateDIBPatternBrush16( HGLOBAL16 hbitmap, UINT16 coloruse )
116 {
117     LOGBRUSH32 logbrush = { BS_DIBPATTERN, coloruse, 0 };
118     BITMAPINFO *info, *newInfo;
119     INT32 size;
120     
121     TRACE(gdi, "%04x\n", hbitmap );
122
123       /* Make a copy of the bitmap */
124
125     if (!(info = (BITMAPINFO *)GlobalLock16( hbitmap ))) return 0;
126
127     if (info->bmiHeader.biCompression)
128         size = info->bmiHeader.biSizeImage;
129     else
130         size = (info->bmiHeader.biWidth * info->bmiHeader.biBitCount + 31) / 32
131                  * 8 * info->bmiHeader.biHeight;
132     size += DIB_BitmapInfoSize( info, coloruse );
133
134     if (!(logbrush.lbHatch = (INT16)GlobalAlloc16( GMEM_MOVEABLE, size )))
135     {
136         GlobalUnlock16( hbitmap );
137         return 0;
138     }
139     newInfo = (BITMAPINFO *) GlobalLock16( (HGLOBAL16)logbrush.lbHatch );
140     memcpy( newInfo, info, size );
141     GlobalUnlock16( (HGLOBAL16)logbrush.lbHatch );
142     GlobalUnlock16( hbitmap );
143     return CreateBrushIndirect32( &logbrush );
144 }
145
146
147 /***********************************************************************
148  *           CreateDIBPatternBrush32    (GDI32.34)
149  */
150 HBRUSH32 WINAPI CreateDIBPatternBrush32( HGLOBAL32 hbitmap, UINT32 coloruse )
151 {
152     LOGBRUSH32 logbrush = { BS_DIBPATTERN, coloruse, 0 };
153     BITMAPINFO *info, *newInfo;
154     INT32 size;
155     
156     TRACE(gdi, "%04x\n", hbitmap );
157
158       /* Make a copy of the bitmap */
159
160     if (!(info = (BITMAPINFO *)GlobalLock32( hbitmap ))) return 0;
161
162     if (info->bmiHeader.biCompression)
163         size = info->bmiHeader.biSizeImage;
164     else
165         size = (info->bmiHeader.biWidth * info->bmiHeader.biBitCount + 31) / 32
166                  * 8 * info->bmiHeader.biHeight;
167     size += DIB_BitmapInfoSize( info, coloruse );
168
169     if (!(logbrush.lbHatch = (INT32)GlobalAlloc16( GMEM_MOVEABLE, size )))
170     {
171         GlobalUnlock16( hbitmap );
172         return 0;
173     }
174     newInfo = (BITMAPINFO *) GlobalLock16( (HGLOBAL16)logbrush.lbHatch );
175     memcpy( newInfo, info, size );
176     GlobalUnlock16( (HGLOBAL16)logbrush.lbHatch );
177     GlobalUnlock16( hbitmap );
178     return CreateBrushIndirect32( &logbrush );
179 }
180
181
182 /***********************************************************************
183  *           CreateSolidBrush    (GDI.66)
184  */
185 HBRUSH16 WINAPI CreateSolidBrush16( COLORREF color )
186 {
187     LOGBRUSH32 logbrush = { BS_SOLID, color, 0 };
188     TRACE(gdi, "%06lx\n", color );
189     return CreateBrushIndirect32( &logbrush );
190 }
191
192
193 /***********************************************************************
194  *           CreateSolidBrush32    (GDI32.64)
195  */
196 HBRUSH32 WINAPI CreateSolidBrush32( COLORREF color )
197 {
198     LOGBRUSH32 logbrush = { BS_SOLID, color, 0 };
199     TRACE(gdi, "%06lx\n", color );
200     return CreateBrushIndirect32( &logbrush );
201 }
202
203
204 /***********************************************************************
205  *           SetBrushOrg    (GDI.148)
206  */
207 DWORD WINAPI SetBrushOrg( HDC16 hdc, INT16 x, INT16 y )
208 {
209     DWORD retval;
210     DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
211     if (!dc) return FALSE;
212     retval = dc->w.brushOrgX | (dc->w.brushOrgY << 16);
213     dc->w.brushOrgX = x;
214     dc->w.brushOrgY = y;
215     return retval;
216 }
217
218
219 /***********************************************************************
220  *           SetBrushOrgEx    (GDI32.308)
221  */
222 BOOL32 WINAPI SetBrushOrgEx( HDC32 hdc, INT32 x, INT32 y, LPPOINT32 oldorg )
223 {
224     DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
225
226     if (!dc) return FALSE;
227     if (oldorg)
228     {
229         oldorg->x = dc->w.brushOrgX;
230         oldorg->y = dc->w.brushOrgY;
231     }
232     dc->w.brushOrgX = x;
233     dc->w.brushOrgY = y;
234     return TRUE;
235 }
236
237 /***********************************************************************
238  *           FixBrushOrgEx    (GDI32.102)
239  * SDK says discontinued, but in Win95 GDI32 this is the same as SetBrushOrgEx
240  */
241 BOOL32 WINAPI FixBrushOrgEx( HDC32 hdc, INT32 x, INT32 y, LPPOINT32 oldorg )
242 {
243     return SetBrushOrgEx(hdc,x,y,oldorg);
244 }
245
246
247 /***********************************************************************
248  *           BRUSH_DeleteObject
249  */
250 BOOL32 BRUSH_DeleteObject( HBRUSH16 hbrush, BRUSHOBJ * brush )
251 {
252     switch(brush->logbrush.lbStyle)
253     {
254       case BS_PATTERN:
255           DeleteObject32( (HGDIOBJ32)brush->logbrush.lbHatch );
256           break;
257       case BS_DIBPATTERN:
258           GlobalFree16( (HGLOBAL16)brush->logbrush.lbHatch );
259           break;
260     }
261     return GDI_FreeObject( hbrush );
262 }
263
264
265 /***********************************************************************
266  *           BRUSH_GetObject16
267  */
268 INT16 BRUSH_GetObject16( BRUSHOBJ * brush, INT16 count, LPSTR buffer )
269 {
270     LOGBRUSH16 logbrush;
271
272     logbrush.lbStyle = brush->logbrush.lbStyle;
273     logbrush.lbColor = brush->logbrush.lbColor;
274     logbrush.lbHatch = brush->logbrush.lbHatch;
275     if (count > sizeof(logbrush)) count = sizeof(logbrush);
276     memcpy( buffer, &logbrush, count );
277     return count;
278 }
279
280
281 /***********************************************************************
282  *           BRUSH_GetObject32
283  */
284 INT32 BRUSH_GetObject32( BRUSHOBJ * brush, INT32 count, LPSTR buffer )
285 {
286     if (count > sizeof(brush->logbrush)) count = sizeof(brush->logbrush);
287     memcpy( buffer, &brush->logbrush, count );
288     return count;
289 }