74209953169e07d059a81bb88bc4518a3dec9f1c
[reactos.git] / modules / rostests / winetests / gdi32 / brush.c
1 /*
2 * Unit test suite for brushes
3 *
4 * Copyright 2004 Kevin Koltzau
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 */
20
21 #include "precomp.h"
22
23 typedef struct _STOCK_BRUSH {
24 COLORREF color;
25 int stockobj;
26 const char *name;
27 } STOCK_BRUSH;
28
29 static void test_solidbrush(void)
30 {
31 static const STOCK_BRUSH stock[] = {
32 {RGB(255,255,255), WHITE_BRUSH, "white"},
33 {RGB(192,192,192), LTGRAY_BRUSH, "ltgray"},
34 {RGB(128,128,128), GRAY_BRUSH, "gray"},
35 {RGB(0,0,0), BLACK_BRUSH, "black"},
36 {RGB(0,0,255), -1, "blue"}
37 };
38 HBRUSH solidBrush;
39 HBRUSH stockBrush;
40 LOGBRUSH br;
41 size_t i;
42 INT ret;
43
44 for(i=0; i<sizeof(stock)/sizeof(stock[0]); i++) {
45 solidBrush = CreateSolidBrush(stock[i].color);
46
47 if(stock[i].stockobj != -1) {
48 stockBrush = GetStockObject(stock[i].stockobj);
49 ok(stockBrush!=solidBrush ||
50 broken(stockBrush==solidBrush), /* win9x does return stock object */
51 "Stock %s brush equals solid %s brush\n", stock[i].name, stock[i].name);
52 }
53 else
54 stockBrush = NULL;
55 memset(&br, 0, sizeof(br));
56 ret = GetObjectW(solidBrush, sizeof(br), &br);
57 ok( ret !=0, "GetObject on solid %s brush failed, error=%d\n", stock[i].name, GetLastError());
58 ok(br.lbStyle==BS_SOLID, "%s brush has wrong style, got %d expected %d\n", stock[i].name, br.lbStyle, BS_SOLID);
59 ok(br.lbColor==stock[i].color, "%s brush has wrong color, got 0x%08x expected 0x%08x\n", stock[i].name, br.lbColor, stock[i].color);
60
61 if(stockBrush) {
62 /* Sanity check, make sure the colors being compared do in fact have a stock brush */
63 ret = GetObjectW(stockBrush, sizeof(br), &br);
64 ok( ret !=0, "GetObject on stock %s brush failed, error=%d\n", stock[i].name, GetLastError());
65 ok(br.lbColor==stock[i].color, "stock %s brush unexpected color, got 0x%08x expected 0x%08x\n", stock[i].name, br.lbColor, stock[i].color);
66 }
67
68 DeleteObject(solidBrush);
69 ret = GetObjectW(solidBrush, sizeof(br), &br);
70 ok(ret==0 ||
71 broken(ret!=0), /* win9x */
72 "GetObject succeeded on a deleted %s brush\n", stock[i].name);
73 }
74 }
75
76 static void test_hatch_brush(void)
77 {
78 int i, size;
79 HBRUSH brush;
80 LOGBRUSH lb;
81
82 for (i = 0; i < 20; i++)
83 {
84 SetLastError( 0xdeadbeef );
85 brush = CreateHatchBrush( i, RGB(12,34,56) );
86 if (i < HS_API_MAX)
87 {
88 ok( brush != 0, "%u: CreateHatchBrush failed err %u\n", i, GetLastError() );
89 size = GetObjectW( brush, sizeof(lb), &lb );
90 ok( size == sizeof(lb), "wrong size %u\n", size );
91 ok( lb.lbColor == RGB(12,34,56), "wrong color %08x\n", lb.lbColor );
92 if (i <= HS_DIAGCROSS)
93 {
94 ok( lb.lbStyle == BS_HATCHED, "wrong style %u\n", lb.lbStyle );
95 ok( lb.lbHatch == i, "wrong hatch %lu/%u\n", lb.lbHatch, i );
96 }
97 else
98 {
99 ok( lb.lbStyle == BS_SOLID, "wrong style %u\n", lb.lbStyle );
100 ok( lb.lbHatch == 0, "wrong hatch %lu\n", lb.lbHatch );
101 }
102 DeleteObject( brush );
103 }
104 else
105 {
106 ok( !brush, "%u: CreateHatchBrush succeeded\n", i );
107 ok( GetLastError() == 0xdeadbeef, "wrong error %u\n", GetLastError() );
108 }
109 }
110 }
111
112 static void test_pattern_brush(void)
113 {
114 char buffer[sizeof(BITMAPINFOHEADER) + 2 * sizeof(RGBQUAD) + 32 * 32 / 8];
115 BITMAPINFO *info = (BITMAPINFO *)buffer;
116 HBRUSH brush;
117 HBITMAP bitmap;
118 LOGBRUSH br;
119 INT ret;
120 void *bits;
121 DIBSECTION dib;
122 HGLOBAL mem;
123
124 bitmap = CreateBitmap( 20, 20, 1, 1, NULL );
125 ok( bitmap != NULL, "CreateBitmap failed\n" );
126 brush = CreatePatternBrush( bitmap );
127 ok( brush != NULL, "CreatePatternBrush failed\n" );
128 memset( &br, 0x55, sizeof(br) );
129 ret = GetObjectW( brush, sizeof(br), &br );
130 ok( ret == sizeof(br), "wrong size %u\n", ret );
131 ok( br.lbStyle == BS_PATTERN, "wrong style %u\n", br.lbStyle );
132 ok( br.lbColor == 0, "wrong color %u\n", br.lbColor );
133 ok( (HBITMAP)br.lbHatch == bitmap, "wrong handle %p/%p\n", (HBITMAP)br.lbHatch, bitmap );
134 DeleteObject( brush );
135
136 br.lbStyle = BS_PATTERN8X8;
137 br.lbColor = 0x12345;
138 br.lbHatch = (ULONG_PTR)bitmap;
139 brush = CreateBrushIndirect( &br );
140 ok( brush != NULL, "CreatePatternBrush failed\n" );
141 memset( &br, 0x55, sizeof(br) );
142 ret = GetObjectW( brush, sizeof(br), &br );
143 ok( ret == sizeof(br), "wrong size %u\n", ret );
144 ok( br.lbStyle == BS_PATTERN, "wrong style %u\n", br.lbStyle );
145 ok( br.lbColor == 0, "wrong color %u\n", br.lbColor );
146 ok( (HBITMAP)br.lbHatch == bitmap, "wrong handle %p/%p\n", (HBITMAP)br.lbHatch, bitmap );
147 ret = GetObjectW( bitmap, sizeof(dib), &dib );
148 ok( ret == sizeof(dib.dsBm), "wrong size %u\n", ret );
149 DeleteObject( bitmap );
150 ret = GetObjectW( bitmap, sizeof(dib), &dib );
151 ok( ret == 0, "wrong size %u\n", ret );
152 DeleteObject( brush );
153
154 memset( info, 0, sizeof(buffer) );
155 info->bmiHeader.biSize = sizeof(info->bmiHeader);
156 info->bmiHeader.biHeight = 32;
157 info->bmiHeader.biWidth = 32;
158 info->bmiHeader.biBitCount = 1;
159 info->bmiHeader.biPlanes = 1;
160 info->bmiHeader.biCompression = BI_RGB;
161 bitmap = CreateDIBSection( 0, info, DIB_RGB_COLORS, (void**)&bits, NULL, 0 );
162 ok( bitmap != NULL, "CreateDIBSection failed\n" );
163
164 /* MSDN says a DIB section is not allowed, but it works fine */
165 brush = CreatePatternBrush( bitmap );
166 ok( brush != NULL, "CreatePatternBrush failed\n" );
167 memset( &br, 0x55, sizeof(br) );
168 ret = GetObjectW( brush, sizeof(br), &br );
169 ok( ret == sizeof(br), "wrong size %u\n", ret );
170 ok( br.lbStyle == BS_PATTERN, "wrong style %u\n", br.lbStyle );
171 ok( br.lbColor == 0, "wrong color %u\n", br.lbColor );
172 ok( (HBITMAP)br.lbHatch == bitmap, "wrong handle %p/%p\n", (HBITMAP)br.lbHatch, bitmap );
173 ret = GetObjectW( bitmap, sizeof(dib), &dib );
174 ok( ret == sizeof(dib), "wrong size %u\n", ret );
175 DeleteObject( brush );
176 DeleteObject( bitmap );
177
178 brush = CreateDIBPatternBrushPt( info, DIB_RGB_COLORS );
179 ok( brush != NULL, "CreatePatternBrush failed\n" );
180 memset( &br, 0x55, sizeof(br) );
181 ret = GetObjectW( brush, sizeof(br), &br );
182 ok( ret == sizeof(br), "wrong size %u\n", ret );
183 ok( br.lbStyle == BS_DIBPATTERN, "wrong style %u\n", br.lbStyle );
184 ok( br.lbColor == 0, "wrong color %u\n", br.lbColor );
185 ok( (BITMAPINFO *)br.lbHatch == info || broken(!br.lbHatch), /* nt4 */
186 "wrong handle %p/%p\n", (BITMAPINFO *)br.lbHatch, info );
187 DeleteObject( brush );
188
189 br.lbStyle = BS_DIBPATTERNPT;
190 br.lbColor = DIB_PAL_COLORS;
191 br.lbHatch = (ULONG_PTR)info;
192 brush = CreateBrushIndirect( &br );
193 ok( brush != NULL, "CreatePatternBrush failed\n" );
194 memset( &br, 0x55, sizeof(br) );
195 ret = GetObjectW( brush, sizeof(br), &br );
196 ok( ret == sizeof(br), "wrong size %u\n", ret );
197 ok( br.lbStyle == BS_DIBPATTERN, "wrong style %u\n", br.lbStyle );
198 ok( br.lbColor == 0, "wrong color %u\n", br.lbColor );
199 ok( (BITMAPINFO *)br.lbHatch == info || broken(!br.lbHatch), /* nt4 */
200 "wrong handle %p/%p\n", (BITMAPINFO *)br.lbHatch, info );
201
202 mem = GlobalAlloc( GMEM_MOVEABLE, sizeof(buffer) );
203 memcpy( GlobalLock( mem ), buffer, sizeof(buffer) );
204
205 br.lbStyle = BS_DIBPATTERN;
206 br.lbColor = DIB_PAL_COLORS;
207 br.lbHatch = (ULONG_PTR)mem;
208 brush = CreateBrushIndirect( &br );
209 ok( brush != NULL, "CreatePatternBrush failed\n" );
210 memset( &br, 0x55, sizeof(br) );
211 ret = GetObjectW( brush, sizeof(br), &br );
212 ok( ret == sizeof(br), "wrong size %u\n", ret );
213 ok( br.lbStyle == BS_DIBPATTERN, "wrong style %u\n", br.lbStyle );
214 ok( br.lbColor == 0, "wrong color %u\n", br.lbColor );
215 ok( (HGLOBAL)br.lbHatch != mem, "wrong handle %p/%p\n", (HGLOBAL)br.lbHatch, mem );
216 bits = GlobalLock( mem );
217 ok( (HGLOBAL)br.lbHatch == bits || broken(!br.lbHatch), /* nt4 */
218 "wrong handle %p/%p\n", (HGLOBAL)br.lbHatch, bits );
219 ret = GlobalFlags( mem );
220 ok( ret == 2, "wrong flags %x\n", ret );
221 DeleteObject( brush );
222 ret = GlobalFlags( mem );
223 ok( ret == 2, "wrong flags %x\n", ret );
224
225 brush = CreateDIBPatternBrushPt( info, DIB_PAL_COLORS );
226 ok( brush != 0, "CreateDIBPatternBrushPt failed\n" );
227 DeleteObject( brush );
228 brush = CreateDIBPatternBrushPt( info, DIB_PAL_COLORS + 1 );
229 ok( brush != 0, "CreateDIBPatternBrushPt failed\n" );
230 DeleteObject( brush );
231 brush = CreateDIBPatternBrushPt( info, DIB_PAL_COLORS + 2 );
232 ok( !brush, "CreateDIBPatternBrushPt succeeded\n" );
233 brush = CreateDIBPatternBrushPt( info, DIB_PAL_COLORS + 3 );
234 ok( !brush, "CreateDIBPatternBrushPt succeeded\n" );
235
236 info->bmiHeader.biBitCount = 8;
237 info->bmiHeader.biCompression = BI_RLE8;
238 brush = CreateDIBPatternBrushPt( info, DIB_RGB_COLORS );
239 ok( !brush, "CreateDIBPatternBrushPt succeeded\n" );
240
241 info->bmiHeader.biBitCount = 4;
242 info->bmiHeader.biCompression = BI_RLE4;
243 brush = CreateDIBPatternBrushPt( info, DIB_RGB_COLORS );
244 ok( !brush, "CreateDIBPatternBrushPt succeeded\n" );
245
246 br.lbStyle = BS_DIBPATTERN8X8;
247 br.lbColor = DIB_RGB_COLORS;
248 br.lbHatch = (ULONG_PTR)mem;
249 brush = CreateBrushIndirect( &br );
250 ok( !brush, "CreatePatternBrush succeeded\n" );
251
252 br.lbStyle = BS_MONOPATTERN;
253 br.lbColor = DIB_RGB_COLORS;
254 br.lbHatch = (ULONG_PTR)mem;
255 brush = CreateBrushIndirect( &br );
256 ok( !brush, "CreatePatternBrush succeeded\n" );
257
258 br.lbStyle = BS_INDEXED;
259 br.lbColor = DIB_RGB_COLORS;
260 br.lbHatch = (ULONG_PTR)mem;
261 brush = CreateBrushIndirect( &br );
262 ok( !brush, "CreatePatternBrush succeeded\n" );
263
264 GlobalFree( mem );
265 }
266
267 static void test_palette_brush(void)
268 {
269 char buffer[sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD) + 16 * 16];
270 BITMAPINFO *info = (BITMAPINFO *)buffer;
271 WORD *indices = (WORD *)info->bmiColors;
272 char pal_buffer[sizeof(LOGPALETTE) + 256 * sizeof(PALETTEENTRY)];
273 LOGPALETTE *pal = (LOGPALETTE *)pal_buffer;
274 HDC hdc = CreateCompatibleDC( 0 );
275 DWORD *dib_bits;
276 HBITMAP dib;
277 HBRUSH brush;
278 int i;
279 HPALETTE palette, palette2;
280
281 memset( info, 0, sizeof(*info) );
282 info->bmiHeader.biSize = sizeof(info->bmiHeader);
283 info->bmiHeader.biWidth = 16;
284 info->bmiHeader.biHeight = 16;
285 info->bmiHeader.biPlanes = 1;
286 info->bmiHeader.biBitCount = 32;
287 info->bmiHeader.biCompression = BI_RGB;
288 dib = CreateDIBSection( NULL, info, DIB_RGB_COLORS, (void**)&dib_bits, NULL, 0 );
289 ok( dib != NULL, "CreateDIBSection failed\n" );
290
291 info->bmiHeader.biBitCount = 8;
292 for (i = 0; i < 256; i++) indices[i] = 255 - i;
293 for (i = 0; i < 256; i++) ((BYTE *)(indices + 256))[i] = i;
294 brush = CreateDIBPatternBrushPt( info, DIB_PAL_COLORS );
295 ok( brush != NULL, "CreateDIBPatternBrushPt failed\n" );
296
297 pal->palVersion = 0x300;
298 pal->palNumEntries = 256;
299 for (i = 0; i < 256; i++)
300 {
301 pal->palPalEntry[i].peRed = i * 2;
302 pal->palPalEntry[i].peGreen = i * 2;
303 pal->palPalEntry[i].peBlue = i * 2;
304 pal->palPalEntry[i].peFlags = 0;
305 }
306 palette = CreatePalette( pal );
307
308 ok( SelectObject( hdc, dib ) != NULL, "SelectObject failed\n" );
309 ok( SelectPalette( hdc, palette, 0 ) != NULL, "SelectPalette failed\n" );
310 ok( SelectObject( hdc, brush ) != NULL, "SelectObject failed\n" );
311 memset( dib_bits, 0xaa, 16 * 16 * 4 );
312 PatBlt( hdc, 0, 0, 16, 16, PATCOPY );
313 for (i = 0; i < 256; i++)
314 {
315 DWORD expect = (pal->palPalEntry[255 - i].peRed << 16 |
316 pal->palPalEntry[255 - i].peGreen << 8 |
317 pal->palPalEntry[255 - i].peBlue);
318 ok( dib_bits[i] == expect, "wrong bits %x/%x at %u,%u\n", dib_bits[i], expect, i % 16, i / 16 );
319 }
320
321 for (i = 0; i < 256; i++) pal->palPalEntry[i].peRed = i * 3;
322 palette2 = CreatePalette( pal );
323 ok( SelectPalette( hdc, palette2, 0 ) != NULL, "SelectPalette failed\n" );
324 memset( dib_bits, 0xaa, 16 * 16 * 4 );
325 PatBlt( hdc, 0, 0, 16, 16, PATCOPY );
326 for (i = 0; i < 256; i++)
327 {
328 DWORD expect = (pal->palPalEntry[255 - i].peRed << 16 |
329 pal->palPalEntry[255 - i].peGreen << 8 |
330 pal->palPalEntry[255 - i].peBlue);
331 ok( dib_bits[i] == expect, "wrong bits %x/%x at %u,%u\n", dib_bits[i], expect, i % 16, i / 16 );
332 }
333 DeleteDC( hdc );
334 DeleteObject( dib );
335 DeleteObject( brush );
336 DeleteObject( palette );
337 DeleteObject( palette2 );
338 }
339
340 static void test_brush_org( void )
341 {
342 HDC hdc = GetDC( 0 );
343 POINT old, pt;
344
345 SetBrushOrgEx( hdc, 0, 0, &old );
346
347 SetBrushOrgEx( hdc, 1, 1, &pt );
348 ok( pt.x == 0 && pt.y == 0, "got %d,%d\n", pt.x, pt.y );
349 SetBrushOrgEx( hdc, 0x10000, -1, &pt );
350 ok( pt.x == 1 && pt.y == 1, "got %d,%d\n", pt.x, pt.y );
351 SetBrushOrgEx( hdc, old.x, old.y, &pt );
352 ok( pt.x == 0x10000 && pt.y == -1, "got %d,%d\n", pt.x, pt.y );
353
354 ReleaseDC( 0, hdc );
355 }
356
357 START_TEST(brush)
358 {
359 test_solidbrush();
360 test_hatch_brush();
361 test_pattern_brush();
362 test_palette_brush();
363 test_brush_org();
364 }