GetSystemPaletteEntries returns palette size if entries==NULL.
[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 "debug.h"
11
12
13 /***********************************************************************
14  *           CreateBrushIndirect16    (GDI.50)
15  */
16 HBRUSH16 WINAPI CreateBrushIndirect16( const LOGBRUSH16 * brush )
17 {
18     BRUSHOBJ * brushPtr;
19     HBRUSH16 hbrush = GDI_AllocObject( sizeof(BRUSHOBJ), BRUSH_MAGIC );
20     if (!hbrush) return 0;
21     brushPtr = (BRUSHOBJ *) GDI_HEAP_LOCK( hbrush );
22     brushPtr->logbrush.lbStyle = brush->lbStyle;
23     brushPtr->logbrush.lbColor = brush->lbColor;
24     brushPtr->logbrush.lbHatch = brush->lbHatch;
25     GDI_HEAP_UNLOCK( hbrush );
26     TRACE(gdi, "%04x\n", hbrush);
27     return hbrush;
28 }
29
30
31 /***********************************************************************
32  *           CreateBrushIndirect32    (GDI32.27)
33  */
34 HBRUSH32 WINAPI CreateBrushIndirect32( const LOGBRUSH32 * brush )
35 {
36     BRUSHOBJ * brushPtr;
37     HBRUSH32 hbrush = GDI_AllocObject( sizeof(BRUSHOBJ), BRUSH_MAGIC );
38     if (!hbrush) return 0;
39     brushPtr = (BRUSHOBJ *) GDI_HEAP_LOCK( hbrush );
40     brushPtr->logbrush.lbStyle = brush->lbStyle;
41     brushPtr->logbrush.lbColor = brush->lbColor;
42     brushPtr->logbrush.lbHatch = brush->lbHatch;
43     GDI_HEAP_UNLOCK( hbrush );
44     TRACE(gdi, "%08x\n", 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 = DIB_GetDIBWidthBytes(info->bmiHeader.biWidth,
117                                     info->bmiHeader.biBitCount) *
118                info->bmiHeader.biHeight;
119     size += DIB_BitmapInfoSize( info, coloruse );
120
121     if (!(logbrush.lbHatch = (INT16)GlobalAlloc16( GMEM_MOVEABLE, size )))
122     {
123         GlobalUnlock16( hbitmap );
124         return 0;
125     }
126     newInfo = (BITMAPINFO *) GlobalLock16( (HGLOBAL16)logbrush.lbHatch );
127     memcpy( newInfo, info, size );
128     GlobalUnlock16( (HGLOBAL16)logbrush.lbHatch );
129     GlobalUnlock16( hbitmap );
130     return CreateBrushIndirect32( &logbrush );
131 }
132
133
134 /***********************************************************************
135  *           CreateDIBPatternBrush32    (GDI32.34)
136  *
137  *      Create a logical brush which has the pattern specified by the DIB
138  *
139  *      Function call is for compatability only.  CreateDIBPatternBrushPt should be used.   
140  *
141  * RETURNS
142  *
143  *      Handle to a logical brush on success, NULL on failure.
144  *
145  * BUGS
146  *      
147  */
148 HBRUSH32 WINAPI CreateDIBPatternBrush32( 
149                 HGLOBAL32 hbitmap,              /* Global object containg BITMAPINFO structure */
150                 UINT32 coloruse                 /* Specifies color format, if provided */
151 )
152 {
153     LOGBRUSH32 logbrush = { BS_DIBPATTERN, coloruse, 0 };
154     BITMAPINFO *info, *newInfo;
155     INT32 size;
156     
157     TRACE(gdi, "%04x\n", hbitmap );
158
159       /* Make a copy of the bitmap */
160
161     if (!(info = (BITMAPINFO *)GlobalLock32( hbitmap ))) return 0;
162
163     if (info->bmiHeader.biCompression)
164         size = info->bmiHeader.biSizeImage;
165     else
166         size = DIB_GetDIBWidthBytes(info->bmiHeader.biWidth,
167                                     info->bmiHeader.biBitCount) *
168                info->bmiHeader.biHeight;
169     size += DIB_BitmapInfoSize( info, coloruse );
170
171     if (!(logbrush.lbHatch = (INT32)GlobalAlloc16( GMEM_MOVEABLE, size )))
172     {
173         GlobalUnlock16( hbitmap );
174         return 0;
175     }
176     newInfo = (BITMAPINFO *) GlobalLock16( (HGLOBAL16)logbrush.lbHatch );
177     memcpy( newInfo, info, size );
178     GlobalUnlock16( (HGLOBAL16)logbrush.lbHatch );
179     GlobalUnlock32( hbitmap );
180     return CreateBrushIndirect32( &logbrush );
181 }
182
183
184 /***********************************************************************
185  *           CreateDIBPatternBrushPt32    (GDI32.35)
186  *
187  *      Create a logical brush which has the pattern specified by the DIB
188  *
189  * RETURNS
190  *
191  *      Handle to a logical brush on success, NULL on failure.
192  *
193  * BUGS
194  *      
195  */
196 HBRUSH32 WINAPI CreateDIBPatternBrushPt32(      
197                 BITMAPINFO *info,               /* Pointer to a BITMAPINFO structure */ 
198                 UINT32 coloruse                 /* Specifies color format, if provided */
199 )
200 {
201     LOGBRUSH32 logbrush = { BS_DIBPATTERN, coloruse, 0 };
202     BITMAPINFO  *newInfo;
203     INT32 size;
204     
205     TRACE(gdi, "%p\n", info );
206
207       /* Make a copy of the bitmap */
208
209
210     if (info->bmiHeader.biCompression)
211         size = info->bmiHeader.biSizeImage;
212     else
213         size = DIB_GetDIBWidthBytes(info->bmiHeader.biWidth,
214                                     info->bmiHeader.biBitCount) *
215                info->bmiHeader.biHeight;
216     size += DIB_BitmapInfoSize( info, coloruse );
217
218     if (!(logbrush.lbHatch = (INT32)GlobalAlloc16( GMEM_MOVEABLE, size )))
219     {
220         return 0;
221     }
222     newInfo = (BITMAPINFO *) GlobalLock16( (HGLOBAL16)logbrush.lbHatch );
223     memcpy( newInfo, info, size );
224     GlobalUnlock16( (HGLOBAL16)logbrush.lbHatch );
225     return CreateBrushIndirect32( &logbrush );
226 }
227
228
229 /***********************************************************************
230  *           CreateSolidBrush    (GDI.66)
231  */
232 HBRUSH16 WINAPI CreateSolidBrush16( COLORREF color )
233 {
234     LOGBRUSH32 logbrush = { BS_SOLID, color, 0 };
235     TRACE(gdi, "%06lx\n", color );
236     return CreateBrushIndirect32( &logbrush );
237 }
238
239
240 /***********************************************************************
241  *           CreateSolidBrush32    (GDI32.64)
242  */
243 HBRUSH32 WINAPI CreateSolidBrush32( COLORREF color )
244 {
245     LOGBRUSH32 logbrush = { BS_SOLID, color, 0 };
246     TRACE(gdi, "%06lx\n", color );
247     return CreateBrushIndirect32( &logbrush );
248 }
249
250
251 /***********************************************************************
252  *           SetBrushOrg    (GDI.148)
253  */
254 DWORD WINAPI SetBrushOrg( HDC16 hdc, INT16 x, INT16 y )
255 {
256     DWORD retval;
257     DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
258     if (!dc) return FALSE;
259     retval = dc->w.brushOrgX | (dc->w.brushOrgY << 16);
260     dc->w.brushOrgX = x;
261     dc->w.brushOrgY = y;
262     return retval;
263 }
264
265
266 /***********************************************************************
267  *           SetBrushOrgEx    (GDI32.308)
268  */
269 BOOL32 WINAPI SetBrushOrgEx( HDC32 hdc, INT32 x, INT32 y, LPPOINT32 oldorg )
270 {
271     DC * dc = (DC *) GDI_GetObjPtr( hdc, DC_MAGIC );
272
273     if (!dc) return FALSE;
274     if (oldorg)
275     {
276         oldorg->x = dc->w.brushOrgX;
277         oldorg->y = dc->w.brushOrgY;
278     }
279     dc->w.brushOrgX = x;
280     dc->w.brushOrgY = y;
281     return TRUE;
282 }
283
284 /***********************************************************************
285  *           FixBrushOrgEx    (GDI32.102)
286  * SDK says discontinued, but in Win95 GDI32 this is the same as SetBrushOrgEx
287  */
288 BOOL32 WINAPI FixBrushOrgEx( HDC32 hdc, INT32 x, INT32 y, LPPOINT32 oldorg )
289 {
290     return SetBrushOrgEx(hdc,x,y,oldorg);
291 }
292
293
294 /***********************************************************************
295  *           BRUSH_DeleteObject
296  */
297 BOOL32 BRUSH_DeleteObject( HBRUSH16 hbrush, BRUSHOBJ * brush )
298 {
299     switch(brush->logbrush.lbStyle)
300     {
301       case BS_PATTERN:
302           DeleteObject32( (HGDIOBJ32)brush->logbrush.lbHatch );
303           break;
304       case BS_DIBPATTERN:
305           GlobalFree16( (HGLOBAL16)brush->logbrush.lbHatch );
306           break;
307     }
308     return GDI_FreeObject( hbrush );
309 }
310
311
312 /***********************************************************************
313  *           BRUSH_GetObject16
314  */
315 INT16 BRUSH_GetObject16( BRUSHOBJ * brush, INT16 count, LPSTR buffer )
316 {
317     LOGBRUSH16 logbrush;
318
319     logbrush.lbStyle = brush->logbrush.lbStyle;
320     logbrush.lbColor = brush->logbrush.lbColor;
321     logbrush.lbHatch = brush->logbrush.lbHatch;
322     if (count > sizeof(logbrush)) count = sizeof(logbrush);
323     memcpy( buffer, &logbrush, count );
324     return count;
325 }
326
327
328 /***********************************************************************
329  *           BRUSH_GetObject32
330  */
331 INT32 BRUSH_GetObject32( BRUSHOBJ * brush, INT32 count, LPSTR buffer )
332 {
333     if (count > sizeof(brush->logbrush)) count = sizeof(brush->logbrush);
334     memcpy( buffer, &brush->logbrush, count );
335     return count;
336 }
337
338
339 /***********************************************************************
340  *           SetSolidBrush16   (GDI.604)
341  *
342  *  If hBrush is a solid brush, change it's color to newColor.
343  *
344  *  RETURNS
345  *           TRUE on success, FALSE on failure.
346  *   FIXME: not yet implemented!
347  */
348 BOOL16 WINAPI SetSolidBrush16(HBRUSH16 hBrush, COLORREF newColor )
349 {
350      FIXME(gdi, "(hBrush %04x, newColor %04x): stub!\n", hBrush, (int)newColor);
351
352      return(FALSE);
353 }
354