A few patches to help Wine to compile on OS/2.
[wine] / objects / brush.c
1 /*
2  * GDI brush objects
3  *
4  * Copyright 1993, 1994  Alexandre Julliard
5  */
6
7 #include <string.h>
8 #include "winbase.h"
9 #include "brush.h"
10 #include "bitmap.h"
11 #include "debug.h"
12
13
14 /***********************************************************************
15  *           CreateBrushIndirect16    (GDI.50)
16  */
17 HBRUSH16 WINAPI CreateBrushIndirect16( const LOGBRUSH16 * brush )
18 {
19     BRUSHOBJ * brushPtr;
20     HBRUSH16 hbrush = GDI_AllocObject( sizeof(BRUSHOBJ), BRUSH_MAGIC );
21     if (!hbrush) return 0;
22     brushPtr = (BRUSHOBJ *) GDI_HEAP_LOCK( hbrush );
23     brushPtr->logbrush.lbStyle = brush->lbStyle;
24     brushPtr->logbrush.lbColor = brush->lbColor;
25     brushPtr->logbrush.lbHatch = brush->lbHatch;
26     GDI_HEAP_UNLOCK( hbrush );
27     TRACE(gdi, "%04x\n", hbrush);
28     return hbrush;
29 }
30
31
32 /***********************************************************************
33  *           CreateBrushIndirect32    (GDI32.27)
34  */
35 HBRUSH WINAPI CreateBrushIndirect( const LOGBRUSH * brush )
36 {
37     BRUSHOBJ * brushPtr;
38     HBRUSH 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     TRACE(gdi, "%08x\n", hbrush);
46     return hbrush;
47 }
48
49
50 /***********************************************************************
51  *           CreateHatchBrush16    (GDI.58)
52  */
53 HBRUSH16 WINAPI CreateHatchBrush16( INT16 style, COLORREF color )
54 {
55     LOGBRUSH logbrush = { BS_HATCHED, color, style };
56     TRACE(gdi, "%d %06lx\n", style, color );
57     if ((style < 0) || (style >= NB_HATCH_STYLES)) return 0;
58     return CreateBrushIndirect( &logbrush );
59 }
60
61
62 /***********************************************************************
63  *           CreateHatchBrush32    (GDI32.48)
64  */
65 HBRUSH WINAPI CreateHatchBrush( INT style, COLORREF color )
66 {
67     LOGBRUSH logbrush = { BS_HATCHED, color, style };
68     TRACE(gdi, "%d %06lx\n", style, color );
69     if ((style < 0) || (style >= NB_HATCH_STYLES)) return 0;
70     return CreateBrushIndirect( &logbrush );
71 }
72
73
74 /***********************************************************************
75  *           CreatePatternBrush16    (GDI.60)
76  */
77 HBRUSH16 WINAPI CreatePatternBrush16( HBITMAP16 hbitmap )
78 {
79     return (HBRUSH16)CreatePatternBrush( hbitmap );
80 }
81
82
83 /***********************************************************************
84  *           CreatePatternBrush32    (GDI32.54)
85  */
86 HBRUSH WINAPI CreatePatternBrush( HBITMAP hbitmap )
87 {
88     LOGBRUSH logbrush = { BS_PATTERN, 0, 0 };
89     TRACE(gdi, "%04x\n", hbitmap );
90
91     logbrush.lbHatch = (INT)BITMAP_CopyBitmap( hbitmap );
92     if(!logbrush.lbHatch)
93         return 0;
94     else
95         return CreateBrushIndirect( &logbrush );
96 }
97
98
99 /***********************************************************************
100  *           CreateDIBPatternBrush16    (GDI.445)
101  */
102 HBRUSH16 WINAPI CreateDIBPatternBrush16( HGLOBAL16 hbitmap, UINT16 coloruse )
103 {
104     LOGBRUSH logbrush = { BS_DIBPATTERN, coloruse, 0 };
105     BITMAPINFO *info, *newInfo;
106     INT size;
107     
108     TRACE(gdi, "%04x\n", hbitmap );
109
110       /* Make a copy of the bitmap */
111
112     if (!(info = (BITMAPINFO *)GlobalLock16( hbitmap ))) return 0;
113
114     if (info->bmiHeader.biCompression)
115         size = info->bmiHeader.biSizeImage;
116     else
117         size = DIB_GetDIBWidthBytes(info->bmiHeader.biWidth,
118                                     info->bmiHeader.biBitCount) *
119                info->bmiHeader.biHeight;
120     size += DIB_BitmapInfoSize( info, coloruse );
121
122     if (!(logbrush.lbHatch = (INT16)GlobalAlloc16( GMEM_MOVEABLE, size )))
123     {
124         GlobalUnlock16( hbitmap );
125         return 0;
126     }
127     newInfo = (BITMAPINFO *) GlobalLock16( (HGLOBAL16)logbrush.lbHatch );
128     memcpy( newInfo, info, size );
129     GlobalUnlock16( (HGLOBAL16)logbrush.lbHatch );
130     GlobalUnlock16( hbitmap );
131     return CreateBrushIndirect( &logbrush );
132 }
133
134
135 /***********************************************************************
136  *           CreateDIBPatternBrush32    (GDI32.34)
137  *
138  *      Create a logical brush which has the pattern specified by the DIB
139  *
140  *      Function call is for compatability only.  CreateDIBPatternBrushPt should be used.   
141  *
142  * RETURNS
143  *
144  *      Handle to a logical brush on success, NULL on failure.
145  *
146  * BUGS
147  *      
148  */
149 HBRUSH WINAPI CreateDIBPatternBrush( 
150                 HGLOBAL hbitmap,                /* Global object containg BITMAPINFO structure */
151                 UINT coloruse           /* Specifies color format, if provided */
152 )
153 {
154     LOGBRUSH logbrush = { BS_DIBPATTERN, coloruse, 0 };
155     BITMAPINFO *info, *newInfo;
156     INT size;
157     
158     TRACE(gdi, "%04x\n", hbitmap );
159
160       /* Make a copy of the bitmap */
161
162     if (!(info = (BITMAPINFO *)GlobalLock( hbitmap ))) return 0;
163
164     if (info->bmiHeader.biCompression)
165         size = info->bmiHeader.biSizeImage;
166     else
167         size = DIB_GetDIBWidthBytes(info->bmiHeader.biWidth,
168                                     info->bmiHeader.biBitCount) *
169                info->bmiHeader.biHeight;
170     size += DIB_BitmapInfoSize( info, coloruse );
171
172     if (!(logbrush.lbHatch = (INT)GlobalAlloc16( GMEM_MOVEABLE, size )))
173     {
174         GlobalUnlock16( hbitmap );
175         return 0;
176     }
177     newInfo = (BITMAPINFO *) GlobalLock16( (HGLOBAL16)logbrush.lbHatch );
178     memcpy( newInfo, info, size );
179     GlobalUnlock16( (HGLOBAL16)logbrush.lbHatch );
180     GlobalUnlock( hbitmap );
181     return CreateBrushIndirect( &logbrush );
182 }
183
184
185 /***********************************************************************
186  *           CreateDIBPatternBrushPt    (GDI32.35)
187  *
188  *      Create a logical brush which has the pattern specified by the DIB
189  *
190  * RETURNS
191  *
192  *      Handle to a logical brush on success, NULL on failure.
193  *
194  * BUGS
195  *      
196  */
197 HBRUSH WINAPI CreateDIBPatternBrushPt(
198                 const void* data,               /* Pointer to a BITMAPINFO structure followed by more data */ 
199                 UINT coloruse           /* Specifies color format, if provided */
200 )
201 {
202     BITMAPINFO *info=(BITMAPINFO*)data;
203     LOGBRUSH logbrush = { BS_DIBPATTERN, coloruse, 0 };
204     BITMAPINFO  *newInfo;
205     INT size;
206     
207     TRACE(gdi, "%p\n", info );
208
209       /* Make a copy of the bitmap */
210
211
212     if (info->bmiHeader.biCompression)
213         size = info->bmiHeader.biSizeImage;
214     else
215         size = DIB_GetDIBWidthBytes(info->bmiHeader.biWidth,
216                                     info->bmiHeader.biBitCount) *
217                info->bmiHeader.biHeight;
218     size += DIB_BitmapInfoSize( info, coloruse );
219
220     if (!(logbrush.lbHatch = (INT)GlobalAlloc16( GMEM_MOVEABLE, size )))
221     {
222         return 0;
223     }
224     newInfo = (BITMAPINFO *) GlobalLock16( (HGLOBAL16)logbrush.lbHatch );
225     memcpy( newInfo, info, size );
226     GlobalUnlock16( (HGLOBAL16)logbrush.lbHatch );
227     return CreateBrushIndirect( &logbrush );
228 }
229
230
231 /***********************************************************************
232  *           CreateSolidBrush    (GDI.66)
233  */
234 HBRUSH16 WINAPI CreateSolidBrush16( COLORREF color )
235 {
236     LOGBRUSH logbrush = { BS_SOLID, color, 0 };
237     TRACE(gdi, "%06lx\n", color );
238     return CreateBrushIndirect( &logbrush );
239 }
240
241
242 /***********************************************************************
243  *           CreateSolidBrush32    (GDI32.64)
244  */
245 HBRUSH WINAPI CreateSolidBrush( COLORREF color )
246 {
247     LOGBRUSH logbrush = { BS_SOLID, color, 0 };
248     TRACE(gdi, "%06lx\n", color );
249     return CreateBrushIndirect( &logbrush );
250 }
251
252
253 /***********************************************************************
254  *           SetBrushOrg    (GDI.148)
255  */
256 DWORD WINAPI SetBrushOrg16( HDC16 hdc, INT16 x, INT16 y )
257 {
258     DWORD retval;
259     DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
260     if (!dc) return FALSE;
261     retval = dc->w.brushOrgX | (dc->w.brushOrgY << 16);
262     dc->w.brushOrgX = x;
263     dc->w.brushOrgY = y;
264     return retval;
265 }
266
267
268 /***********************************************************************
269  *           SetBrushOrgEx    (GDI32.308)
270  */
271 BOOL WINAPI SetBrushOrgEx( HDC hdc, INT x, INT y, LPPOINT oldorg )
272 {
273     DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
274
275     if (!dc) return FALSE;
276     if (oldorg)
277     {
278         oldorg->x = dc->w.brushOrgX;
279         oldorg->y = dc->w.brushOrgY;
280     }
281     dc->w.brushOrgX = x;
282     dc->w.brushOrgY = y;
283     return TRUE;
284 }
285
286 /***********************************************************************
287  *           FixBrushOrgEx    (GDI32.102)
288  * SDK says discontinued, but in Win95 GDI32 this is the same as SetBrushOrgEx
289  */
290 BOOL WINAPI FixBrushOrgEx( HDC hdc, INT x, INT y, LPPOINT oldorg )
291 {
292     return SetBrushOrgEx(hdc,x,y,oldorg);
293 }
294
295
296 /***********************************************************************
297  *           BRUSH_DeleteObject
298  */
299 BOOL BRUSH_DeleteObject( HBRUSH16 hbrush, BRUSHOBJ * brush )
300 {
301     switch(brush->logbrush.lbStyle)
302     {
303       case BS_PATTERN:
304           DeleteObject( (HGDIOBJ)brush->logbrush.lbHatch );
305           break;
306       case BS_DIBPATTERN:
307           GlobalFree16( (HGLOBAL16)brush->logbrush.lbHatch );
308           break;
309     }
310     return GDI_FreeObject( hbrush );
311 }
312
313
314 /***********************************************************************
315  *           BRUSH_GetObject16
316  */
317 INT16 BRUSH_GetObject16( BRUSHOBJ * brush, INT16 count, LPSTR buffer )
318 {
319     LOGBRUSH16 logbrush;
320
321     logbrush.lbStyle = brush->logbrush.lbStyle;
322     logbrush.lbColor = brush->logbrush.lbColor;
323     logbrush.lbHatch = brush->logbrush.lbHatch;
324     if (count > sizeof(logbrush)) count = sizeof(logbrush);
325     memcpy( buffer, &logbrush, count );
326     return count;
327 }
328
329
330 /***********************************************************************
331  *           BRUSH_GetObject32
332  */
333 INT BRUSH_GetObject( BRUSHOBJ * brush, INT count, LPSTR buffer )
334 {
335     if (count > sizeof(brush->logbrush)) count = sizeof(brush->logbrush);
336     memcpy( buffer, &brush->logbrush, count );
337     return count;
338 }
339
340
341 /***********************************************************************
342  *           SetSolidBrush16   (GDI.604)
343  *
344  *  If hBrush is a solid brush, change it's color to newColor.
345  *
346  *  RETURNS
347  *           TRUE on success, FALSE on failure.
348  *   FIXME: not yet implemented!
349  */
350 BOOL16 WINAPI SetSolidBrush16(HBRUSH16 hBrush, COLORREF newColor )
351 {
352      FIXME(gdi, "(hBrush %04x, newColor %04x): stub!\n", hBrush, (int)newColor);
353
354      return(FALSE);
355 }
356