Added CreateDIBPatternBrushPt (GDI32.35) with minor documentation
[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     TRACE(gdi, "%04x\n", hbitmap );
89
90     logbrush.lbHatch = (INT32)BITMAP_CopyBitmap( hbitmap );
91     if(!logbrush.lbHatch)
92         return 0;
93     else
94         return CreateBrushIndirect32( &logbrush );
95 }
96
97
98 /***********************************************************************
99  *           CreateDIBPatternBrush16    (GDI.445)
100  */
101 HBRUSH16 WINAPI CreateDIBPatternBrush16( HGLOBAL16 hbitmap, UINT16 coloruse )
102 {
103     LOGBRUSH32 logbrush = { BS_DIBPATTERN, coloruse, 0 };
104     BITMAPINFO *info, *newInfo;
105     INT32 size;
106     
107     TRACE(gdi, "%04x\n", hbitmap );
108
109       /* Make a copy of the bitmap */
110
111     if (!(info = (BITMAPINFO *)GlobalLock16( hbitmap ))) return 0;
112
113     if (info->bmiHeader.biCompression)
114         size = info->bmiHeader.biSizeImage;
115     else
116         size = (info->bmiHeader.biWidth * info->bmiHeader.biBitCount + 31) / 32
117                  * 8 * info->bmiHeader.biHeight;
118     size += DIB_BitmapInfoSize( info, coloruse );
119
120     if (!(logbrush.lbHatch = (INT16)GlobalAlloc16( GMEM_MOVEABLE, size )))
121     {
122         GlobalUnlock16( hbitmap );
123         return 0;
124     }
125     newInfo = (BITMAPINFO *) GlobalLock16( (HGLOBAL16)logbrush.lbHatch );
126     memcpy( newInfo, info, size );
127     GlobalUnlock16( (HGLOBAL16)logbrush.lbHatch );
128     GlobalUnlock16( hbitmap );
129     return CreateBrushIndirect32( &logbrush );
130 }
131
132
133 /***********************************************************************
134  *           CreateDIBPatternBrush32    (GDI32.34)
135  *
136  *      Create a logical brush which has the pattern specified by the DIB
137  *
138  *      Function call is for compatability only.  CreateDIBPatternBrushPt should be used.   
139  *
140  * RETURNS
141  *
142  *      Handle to a logical brush on success, NULL on failure.
143  *
144  * BUGS
145  *      
146  */
147 HBRUSH32 WINAPI CreateDIBPatternBrush32( 
148                 HGLOBAL32 hbitmap,              /* Global object containg BITMAPINFO structure */
149                 UINT32 coloruse                 /* Specifies color format, if provided */
150 )
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  *           CreateDIBPatternBrushPt32    (GDI32.35)
184  *
185  *      Create a logical brush which has the pattern specified by the DIB
186  *
187  * RETURNS
188  *
189  *      Handle to a logical brush on success, NULL on failure.
190  *
191  * BUGS
192  *      
193  */
194 HBRUSH32 WINAPI CreateDIBPatternBrushPt32(      
195                 BITMAPINFO *info,               /* Pointer to a BITMAPINFO structure */ 
196                 UINT32 coloruse                 /* Specifies color format, if provided */
197 )
198 {
199     LOGBRUSH32 logbrush = { BS_DIBPATTERN, coloruse, 0 };
200     BITMAPINFO  *newInfo;
201     INT32 size;
202     
203     TRACE(gdi, "%04x\n", info );
204
205       /* Make a copy of the bitmap */
206
207
208     if (info->bmiHeader.biCompression)
209         size = info->bmiHeader.biSizeImage;
210     else
211         size = (info->bmiHeader.biWidth * info->bmiHeader.biBitCount + 31) / 32
212                  * 8 * info->bmiHeader.biHeight;
213     size += DIB_BitmapInfoSize( info, coloruse );
214
215     if (!(logbrush.lbHatch = (INT32)GlobalAlloc16( GMEM_MOVEABLE, size )))
216     {
217         return 0;
218     }
219     newInfo = (BITMAPINFO *) GlobalLock16( (HGLOBAL16)logbrush.lbHatch );
220     memcpy( newInfo, info, size );
221     GlobalUnlock16( (HGLOBAL16)logbrush.lbHatch );
222     return CreateBrushIndirect32( &logbrush );
223 }
224
225
226 /***********************************************************************
227  *           CreateSolidBrush    (GDI.66)
228  */
229 HBRUSH16 WINAPI CreateSolidBrush16( COLORREF color )
230 {
231     LOGBRUSH32 logbrush = { BS_SOLID, color, 0 };
232     TRACE(gdi, "%06lx\n", color );
233     return CreateBrushIndirect32( &logbrush );
234 }
235
236
237 /***********************************************************************
238  *           CreateSolidBrush32    (GDI32.64)
239  */
240 HBRUSH32 WINAPI CreateSolidBrush32( COLORREF color )
241 {
242     LOGBRUSH32 logbrush = { BS_SOLID, color, 0 };
243     TRACE(gdi, "%06lx\n", color );
244     return CreateBrushIndirect32( &logbrush );
245 }
246
247
248 /***********************************************************************
249  *           SetBrushOrg    (GDI.148)
250  */
251 DWORD WINAPI SetBrushOrg( HDC16 hdc, INT16 x, INT16 y )
252 {
253     DWORD retval;
254     DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
255     if (!dc) return FALSE;
256     retval = dc->w.brushOrgX | (dc->w.brushOrgY << 16);
257     dc->w.brushOrgX = x;
258     dc->w.brushOrgY = y;
259     return retval;
260 }
261
262
263 /***********************************************************************
264  *           SetBrushOrgEx    (GDI32.308)
265  */
266 BOOL32 WINAPI SetBrushOrgEx( HDC32 hdc, INT32 x, INT32 y, LPPOINT32 oldorg )
267 {
268     DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
269
270     if (!dc) return FALSE;
271     if (oldorg)
272     {
273         oldorg->x = dc->w.brushOrgX;
274         oldorg->y = dc->w.brushOrgY;
275     }
276     dc->w.brushOrgX = x;
277     dc->w.brushOrgY = y;
278     return TRUE;
279 }
280
281 /***********************************************************************
282  *           FixBrushOrgEx    (GDI32.102)
283  * SDK says discontinued, but in Win95 GDI32 this is the same as SetBrushOrgEx
284  */
285 BOOL32 WINAPI FixBrushOrgEx( HDC32 hdc, INT32 x, INT32 y, LPPOINT32 oldorg )
286 {
287     return SetBrushOrgEx(hdc,x,y,oldorg);
288 }
289
290
291 /***********************************************************************
292  *           BRUSH_DeleteObject
293  */
294 BOOL32 BRUSH_DeleteObject( HBRUSH16 hbrush, BRUSHOBJ * brush )
295 {
296     switch(brush->logbrush.lbStyle)
297     {
298       case BS_PATTERN:
299           DeleteObject32( (HGDIOBJ32)brush->logbrush.lbHatch );
300           break;
301       case BS_DIBPATTERN:
302           GlobalFree16( (HGLOBAL16)brush->logbrush.lbHatch );
303           break;
304     }
305     return GDI_FreeObject( hbrush );
306 }
307
308
309 /***********************************************************************
310  *           BRUSH_GetObject16
311  */
312 INT16 BRUSH_GetObject16( BRUSHOBJ * brush, INT16 count, LPSTR buffer )
313 {
314     LOGBRUSH16 logbrush;
315
316     logbrush.lbStyle = brush->logbrush.lbStyle;
317     logbrush.lbColor = brush->logbrush.lbColor;
318     logbrush.lbHatch = brush->logbrush.lbHatch;
319     if (count > sizeof(logbrush)) count = sizeof(logbrush);
320     memcpy( buffer, &logbrush, count );
321     return count;
322 }
323
324
325 /***********************************************************************
326  *           BRUSH_GetObject32
327  */
328 INT32 BRUSH_GetObject32( BRUSHOBJ * brush, INT32 count, LPSTR buffer )
329 {
330     if (count > sizeof(brush->logbrush)) count = sizeof(brush->logbrush);
331     memcpy( buffer, &brush->logbrush, count );
332     return count;
333 }