Autosyncing with Wine HEAD
[reactos.git] / rostests / winetests / comctl32 / imagelist.c
1 /*
2 * Unit test suite for imagelist control.
3 *
4 * Copyright 2004 Michael Stefaniuc
5 * Copyright 2002 Mike McCormack for CodeWeavers
6 * Copyright 2007 Dmitry Timoshkov
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 */
22
23 #define COBJMACROS
24 #define CONST_VTABLE
25
26 #include <stdarg.h>
27 #include <stdio.h>
28 #include <assert.h>
29
30 #include "windef.h"
31 #include "winbase.h"
32 #include "wingdi.h"
33 #include "winuser.h"
34 #include "objbase.h"
35 #include "commctrl.h" /* must be included after objbase.h to get ImageList_Write */
36
37 #include "wine/test.h"
38
39 #undef VISIBLE
40
41 #ifdef VISIBLE
42 #define WAIT Sleep (1000)
43 #define REDRAW(hwnd) RedrawWindow (hwnd, NULL, 0, RDW_UPDATENOW)
44 #else
45 #define WAIT
46 #define REDRAW(hwnd)
47 #endif
48
49 #define IMAGELIST_MAGIC (('L' << 8) | 'I')
50
51 #include "pshpack2.h"
52 /* Header used by ImageList_Read() and ImageList_Write() */
53 typedef struct _ILHEAD
54 {
55 USHORT usMagic;
56 USHORT usVersion;
57 WORD cCurImage;
58 WORD cMaxImage;
59 WORD cGrow;
60 WORD cx;
61 WORD cy;
62 COLORREF bkcolor;
63 WORD flags;
64 SHORT ovls[4];
65 } ILHEAD;
66 #include "poppack.h"
67
68 static BOOL (WINAPI *pImageList_DrawIndirect)(IMAGELISTDRAWPARAMS*) = NULL;
69
70 static HDC desktopDC;
71 static HINSTANCE hinst;
72
73 /* These macros build cursor/bitmap data in 4x4 pixel blocks */
74 #define B(x,y) ((x?0xf0:0)|(y?0xf:0))
75 #define ROW1(a,b,c,d,e,f,g,h) B(a,b),B(c,d),B(e,f),B(g,h)
76 #define ROW32(a,b,c,d,e,f,g,h) ROW1(a,b,c,d,e,f,g,h), ROW1(a,b,c,d,e,f,g,h), \
77 ROW1(a,b,c,d,e,f,g,h), ROW1(a,b,c,d,e,f,g,h)
78 #define ROW2(a,b,c,d,e,f,g,h,i,j,k,l) ROW1(a,b,c,d,e,f,g,h),B(i,j),B(k,l)
79 #define ROW48(a,b,c,d,e,f,g,h,i,j,k,l) ROW2(a,b,c,d,e,f,g,h,i,j,k,l), \
80 ROW2(a,b,c,d,e,f,g,h,i,j,k,l), ROW2(a,b,c,d,e,f,g,h,i,j,k,l), \
81 ROW2(a,b,c,d,e,f,g,h,i,j,k,l)
82
83 static const BYTE empty_bits[48*48/8];
84
85 static const BYTE icon_bits[32*32/8] =
86 {
87 ROW32(0,0,0,0,0,0,0,0),
88 ROW32(0,0,1,1,1,1,0,0),
89 ROW32(0,1,1,1,1,1,1,0),
90 ROW32(0,1,1,0,0,1,1,0),
91 ROW32(0,1,1,0,0,1,1,0),
92 ROW32(0,1,1,1,1,1,1,0),
93 ROW32(0,0,1,1,1,1,0,0),
94 ROW32(0,0,0,0,0,0,0,0)
95 };
96
97 static const BYTE bitmap_bits[48*48/8] =
98 {
99 ROW48(0,0,0,0,0,0,0,0,0,0,0,0),
100 ROW48(0,1,1,1,1,1,1,1,1,1,1,0),
101 ROW48(0,1,1,0,0,0,0,0,0,1,1,0),
102 ROW48(0,1,0,0,0,0,0,0,1,0,1,0),
103 ROW48(0,1,0,0,0,0,0,1,0,0,1,0),
104 ROW48(0,1,0,0,0,0,1,0,0,0,1,0),
105 ROW48(0,1,0,0,0,1,0,0,0,0,1,0),
106 ROW48(0,1,0,0,1,0,0,0,0,0,1,0),
107 ROW48(0,1,0,1,0,0,0,0,0,0,1,0),
108 ROW48(0,1,1,0,0,0,0,0,0,1,1,0),
109 ROW48(0,1,1,1,1,1,1,1,1,1,1,0),
110 ROW48(0,0,0,0,0,0,0,0,0,0,0,0)
111 };
112
113 static HIMAGELIST createImageList(int cx, int cy)
114 {
115 /* Create an ImageList and put an image into it */
116 HIMAGELIST himl = ImageList_Create(cx, cy, ILC_COLOR, 1, 1);
117 HBITMAP hbm = CreateBitmap(48, 48, 1, 1, bitmap_bits);
118 ImageList_Add(himl, hbm, NULL);
119 return himl;
120 }
121
122 static HWND create_a_window(void)
123 {
124 char className[] = "bmwnd";
125 char winName[] = "Test Bitmap";
126 HWND hWnd;
127 static int registered = 0;
128
129 if (!registered)
130 {
131 WNDCLASSA cls;
132
133 cls.style = CS_HREDRAW | CS_VREDRAW | CS_GLOBALCLASS;
134 cls.lpfnWndProc = DefWindowProcA;
135 cls.cbClsExtra = 0;
136 cls.cbWndExtra = 0;
137 cls.hInstance = 0;
138 cls.hIcon = LoadIconA (0, (LPSTR)IDI_APPLICATION);
139 cls.hCursor = LoadCursorA (0, (LPSTR)IDC_ARROW);
140 cls.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH);
141 cls.lpszMenuName = 0;
142 cls.lpszClassName = className;
143
144 RegisterClassA (&cls);
145 registered = 1;
146 }
147
148 /* Setup window */
149 hWnd = CreateWindowA (className, winName,
150 WS_OVERLAPPEDWINDOW ,
151 CW_USEDEFAULT, CW_USEDEFAULT, 300, 300, 0,
152 0, hinst, 0);
153
154 #ifdef VISIBLE
155 ShowWindow (hWnd, SW_SHOW);
156 #endif
157 REDRAW(hWnd);
158 WAIT;
159
160 return hWnd;
161 }
162
163 static HDC show_image(HWND hwnd, HIMAGELIST himl, int idx, int size,
164 LPCSTR loc, BOOL clear)
165 {
166 HDC hdc = NULL;
167 #ifdef VISIBLE
168 if (!himl) return NULL;
169
170 SetWindowText(hwnd, loc);
171 hdc = GetDC(hwnd);
172 ImageList_Draw(himl, idx, hdc, 0, 0, ILD_TRANSPARENT);
173
174 REDRAW(hwnd);
175 WAIT;
176
177 if (clear)
178 {
179 BitBlt(hdc, 0, 0, size, size, hdc, size+1, size+1, SRCCOPY);
180 ReleaseDC(hwnd, hdc);
181 hdc = NULL;
182 }
183 #endif /* VISIBLE */
184 return hdc;
185 }
186
187 /* Useful for checking differences */
188 #if 0
189 static void dump_bits(const BYTE *p, const BYTE *q, int size)
190 {
191 int i, j;
192
193 size /= 8;
194
195 for (i = 0; i < size * 2; i++)
196 {
197 printf("|");
198 for (j = 0; j < size; j++)
199 printf("%c%c", p[j] & 0xf0 ? 'X' : ' ', p[j] & 0xf ? 'X' : ' ');
200 printf(" -- ");
201 for (j = 0; j < size; j++)
202 printf("%c%c", q[j] & 0xf0 ? 'X' : ' ', q[j] & 0xf ? 'X' : ' ');
203 printf("|\n");
204 p += size * 4;
205 q += size * 4;
206 }
207 printf("\n");
208 }
209 #endif
210
211 static void check_bits(HWND hwnd, HIMAGELIST himl, int idx, int size,
212 const BYTE *checkbits, LPCSTR loc)
213 {
214 #ifdef VISIBLE
215 BYTE bits[100*100/8];
216 COLORREF c;
217 HDC hdc;
218 int x, y, i = -1;
219
220 if (!himl) return;
221
222 memset(bits, 0, sizeof(bits));
223 hdc = show_image(hwnd, himl, idx, size, loc, FALSE);
224
225 c = GetPixel(hdc, 0, 0);
226
227 for (y = 0; y < size; y ++)
228 {
229 for (x = 0; x < size; x++)
230 {
231 if (!(x & 0x7)) i++;
232 if (GetPixel(hdc, x, y) != c) bits[i] |= (0x80 >> (x & 0x7));
233 }
234 }
235
236 BitBlt(hdc, 0, 0, size, size, hdc, size+1, size+1, SRCCOPY);
237 ReleaseDC(hwnd, hdc);
238
239 ok (memcmp(bits, checkbits, (size * size)/8) == 0,
240 "%s: bits different\n", loc);
241 if (memcmp(bits, checkbits, (size * size)/8))
242 dump_bits(bits, checkbits, size);
243 #endif /* VISIBLE */
244 }
245
246 static void testHotspot (void)
247 {
248 struct hotspot {
249 int dx;
250 int dy;
251 };
252
253 #define SIZEX1 47
254 #define SIZEY1 31
255 #define SIZEX2 11
256 #define SIZEY2 17
257 #define HOTSPOTS_MAX 4 /* Number of entries in hotspots */
258 static const struct hotspot hotspots[HOTSPOTS_MAX] = {
259 { 10, 7 },
260 { SIZEX1, SIZEY1 },
261 { -9, -8 },
262 { -7, 35 }
263 };
264 int i, j, ret;
265 HIMAGELIST himl1 = createImageList(SIZEX1, SIZEY1);
266 HIMAGELIST himl2 = createImageList(SIZEX2, SIZEY2);
267 HWND hwnd = create_a_window();
268
269
270 for (i = 0; i < HOTSPOTS_MAX; i++) {
271 for (j = 0; j < HOTSPOTS_MAX; j++) {
272 int dx1 = hotspots[i].dx;
273 int dy1 = hotspots[i].dy;
274 int dx2 = hotspots[j].dx;
275 int dy2 = hotspots[j].dy;
276 int correctx, correcty, newx, newy;
277 char loc[256];
278 HIMAGELIST himlNew;
279 POINT ppt;
280
281 ret = ImageList_BeginDrag(himl1, 0, dx1, dy1);
282 ok(ret != 0, "BeginDrag failed for { %d, %d }\n", dx1, dy1);
283 sprintf(loc, "BeginDrag (%d,%d)\n", i, j);
284 show_image(hwnd, himl1, 0, max(SIZEX1, SIZEY1), loc, TRUE);
285
286 /* check merging the dragged image with a second image */
287 ret = ImageList_SetDragCursorImage(himl2, 0, dx2, dy2);
288 ok(ret != 0, "SetDragCursorImage failed for {%d, %d}{%d, %d}\n",
289 dx1, dy1, dx2, dy2);
290 sprintf(loc, "SetDragCursorImage (%d,%d)\n", i, j);
291 show_image(hwnd, himl2, 0, max(SIZEX2, SIZEY2), loc, TRUE);
292
293 /* check new hotspot, it should be the same like the old one */
294 himlNew = ImageList_GetDragImage(NULL, &ppt);
295 ok(ppt.x == dx1 && ppt.y == dy1,
296 "Expected drag hotspot [%d,%d] got [%d,%d]\n",
297 dx1, dy1, ppt.x, ppt.y);
298 /* check size of new dragged image */
299 ImageList_GetIconSize(himlNew, &newx, &newy);
300 correctx = max(SIZEX1, max(SIZEX2 + dx2, SIZEX1 - dx2));
301 correcty = max(SIZEY1, max(SIZEY2 + dy2, SIZEY1 - dy2));
302 ok(newx == correctx && newy == correcty,
303 "Expected drag image size [%d,%d] got [%d,%d]\n",
304 correctx, correcty, newx, newy);
305 sprintf(loc, "GetDragImage (%d,%d)\n", i, j);
306 show_image(hwnd, himlNew, 0, max(correctx, correcty), loc, TRUE);
307 ImageList_EndDrag();
308 }
309 }
310 #undef SIZEX1
311 #undef SIZEY1
312 #undef SIZEX2
313 #undef SIZEY2
314 #undef HOTSPOTS_MAX
315 ImageList_Destroy(himl2);
316 ImageList_Destroy(himl1);
317 DestroyWindow(hwnd);
318 }
319
320 static BOOL DoTest1(void)
321 {
322 HIMAGELIST himl ;
323
324 HICON hicon1 ;
325 HICON hicon2 ;
326 HICON hicon3 ;
327
328 /* create an imagelist to play with */
329 himl = ImageList_Create(84,84,0x10,0,3);
330 ok(himl!=0,"failed to create imagelist\n");
331
332 /* load the icons to add to the image list */
333 hicon1 = CreateIcon(hinst, 32, 32, 1, 1, icon_bits, icon_bits);
334 ok(hicon1 != 0, "no hicon1\n");
335 hicon2 = CreateIcon(hinst, 32, 32, 1, 1, icon_bits, icon_bits);
336 ok(hicon2 != 0, "no hicon2\n");
337 hicon3 = CreateIcon(hinst, 32, 32, 1, 1, icon_bits, icon_bits);
338 ok(hicon3 != 0, "no hicon3\n");
339
340 /* remove when nothing exists */
341 ok(!ImageList_Remove(himl,0),"removed nonexistent icon\n");
342 /* removing everything from an empty imagelist should succeed */
343 ok(ImageList_RemoveAll(himl),"removed nonexistent icon\n");
344
345 /* add three */
346 ok(0==ImageList_AddIcon(himl, hicon1),"failed to add icon1\n");
347 ok(1==ImageList_AddIcon(himl, hicon2),"failed to add icon2\n");
348 ok(2==ImageList_AddIcon(himl, hicon3),"failed to add icon3\n");
349
350 /* remove an index out of range */
351 ok(!ImageList_Remove(himl,4711),"removed nonexistent icon\n");
352
353 /* remove three */
354 ok(ImageList_Remove(himl,0),"can't remove 0\n");
355 ok(ImageList_Remove(himl,0),"can't remove 0\n");
356 ok(ImageList_Remove(himl,0),"can't remove 0\n");
357
358 /* remove one extra */
359 ok(!ImageList_Remove(himl,0),"removed nonexistent icon\n");
360
361 /* check SetImageCount/GetImageCount */
362 ok(ImageList_SetImageCount(himl, 3), "couldn't increase image count\n");
363 ok(ImageList_GetImageCount(himl) == 3, "invalid image count after increase\n");
364 ok(ImageList_SetImageCount(himl, 1), "couldn't decrease image count\n");
365 ok(ImageList_GetImageCount(himl) == 1, "invalid image count after decrease to 1\n");
366 ok(ImageList_SetImageCount(himl, 0), "couldn't decrease image count\n");
367 ok(ImageList_GetImageCount(himl) == 0, "invalid image count after decrease to 0\n");
368
369 /* destroy it */
370 ok(ImageList_Destroy(himl),"destroy imagelist failed\n");
371
372 ok(DestroyIcon(hicon1),"icon 1 wasn't deleted\n");
373 ok(DestroyIcon(hicon2),"icon 2 wasn't deleted\n");
374 ok(DestroyIcon(hicon3),"icon 3 wasn't deleted\n");
375
376 return TRUE;
377 }
378
379 static BOOL DoTest2(void)
380 {
381 HIMAGELIST himl ;
382
383 HICON hicon1 ;
384 HICON hicon2 ;
385 HICON hicon3 ;
386
387 /* create an imagelist to play with */
388 himl = ImageList_Create(84,84,0x10,0,3);
389 ok(himl!=0,"failed to create imagelist\n");
390
391 /* load the icons to add to the image list */
392 hicon1 = CreateIcon(hinst, 32, 32, 1, 1, icon_bits, icon_bits);
393 ok(hicon1 != 0, "no hicon1\n");
394 hicon2 = CreateIcon(hinst, 32, 32, 1, 1, icon_bits, icon_bits);
395 ok(hicon2 != 0, "no hicon2\n");
396 hicon3 = CreateIcon(hinst, 32, 32, 1, 1, icon_bits, icon_bits);
397 ok(hicon3 != 0, "no hicon3\n");
398
399 /* add three */
400 ok(0==ImageList_AddIcon(himl, hicon1),"failed to add icon1\n");
401 ok(1==ImageList_AddIcon(himl, hicon2),"failed to add icon2\n");
402 ok(2==ImageList_AddIcon(himl, hicon3),"failed to add icon3\n");
403
404 /* destroy it */
405 ok(ImageList_Destroy(himl),"destroy imagelist failed\n");
406
407 ok(DestroyIcon(hicon1),"icon 1 wasn't deleted\n");
408 ok(DestroyIcon(hicon2),"icon 2 wasn't deleted\n");
409 ok(DestroyIcon(hicon3),"icon 3 wasn't deleted\n");
410
411 return TRUE;
412 }
413
414 static BOOL DoTest3(void)
415 {
416 HIMAGELIST himl;
417
418 HBITMAP hbm1;
419 HBITMAP hbm2;
420 HBITMAP hbm3;
421
422 IMAGELISTDRAWPARAMS imldp;
423 HDC hdc;
424 HWND hwndfortest;
425
426 if (!pImageList_DrawIndirect)
427 {
428 HMODULE hComCtl32 = LoadLibraryA("comctl32.dll");
429 pImageList_DrawIndirect = (void*)GetProcAddress(hComCtl32, "ImageList_DrawIndirect");
430 if (!pImageList_DrawIndirect)
431 {
432 trace("ImageList_DrawIndirect not available, skipping test\n");
433 return TRUE;
434 }
435 }
436
437 hwndfortest = create_a_window();
438 hdc = GetDC(hwndfortest);
439 ok(hdc!=NULL, "couldn't get DC\n");
440
441 /* create an imagelist to play with */
442 himl = ImageList_Create(48,48,0x10,0,3);
443 ok(himl!=0,"failed to create imagelist\n");
444
445 /* load the icons to add to the image list */
446 hbm1 = CreateBitmap(48, 48, 1, 1, bitmap_bits);
447 ok(hbm1 != 0, "no bitmap 1\n");
448 hbm2 = CreateBitmap(48, 48, 1, 1, bitmap_bits);
449 ok(hbm2 != 0, "no bitmap 2\n");
450 hbm3 = CreateBitmap(48, 48, 1, 1, bitmap_bits);
451 ok(hbm3 != 0, "no bitmap 3\n");
452
453 /* add three */
454 ok(0==ImageList_Add(himl, hbm1, 0),"failed to add bitmap 1\n");
455 ok(1==ImageList_Add(himl, hbm2, 0),"failed to add bitmap 2\n");
456
457 ok(ImageList_SetImageCount(himl,3),"Setimage count failed\n");
458 /*ok(2==ImageList_Add(himl, hbm3, NULL),"failed to add bitmap 3\n"); */
459 ok(ImageList_Replace(himl, 2, hbm3, 0),"failed to replace bitmap 3\n");
460
461 memset(&imldp, 0, sizeof (imldp));
462 ok(!pImageList_DrawIndirect(&imldp), "zero data succeeded!\n");
463 imldp.cbSize = sizeof (imldp);
464 ok(!pImageList_DrawIndirect(&imldp), "zero hdc succeeded!\n");
465 imldp.hdcDst = hdc;
466 ok(!pImageList_DrawIndirect(&imldp),"zero himl succeeded!\n");
467 imldp.himl = himl;
468 if (!pImageList_DrawIndirect(&imldp))
469 {
470 /* Earlier versions of native comctl32 use a smaller structure */
471 imldp.cbSize -= 3 * sizeof(DWORD);
472 ok(pImageList_DrawIndirect(&imldp),"DrawIndirect should succeed\n");
473 }
474 REDRAW(hwndfortest);
475 WAIT;
476
477 imldp.fStyle = SRCCOPY;
478 imldp.rgbBk = CLR_DEFAULT;
479 imldp.rgbFg = CLR_DEFAULT;
480 imldp.y = 100;
481 imldp.x = 100;
482 ok(pImageList_DrawIndirect(&imldp),"should succeed\n");
483 imldp.i ++;
484 ok(pImageList_DrawIndirect(&imldp),"should succeed\n");
485 imldp.i ++;
486 ok(pImageList_DrawIndirect(&imldp),"should succeed\n");
487 imldp.i ++;
488 ok(!pImageList_DrawIndirect(&imldp),"should fail\n");
489
490 /* remove three */
491 ok(ImageList_Remove(himl, 0), "removing 1st bitmap\n");
492 ok(ImageList_Remove(himl, 0), "removing 2nd bitmap\n");
493 ok(ImageList_Remove(himl, 0), "removing 3rd bitmap\n");
494
495 /* destroy it */
496 ok(ImageList_Destroy(himl),"destroy imagelist failed\n");
497
498 /* bitmaps should not be deleted by the imagelist */
499 ok(DeleteObject(hbm1),"bitmap 1 can't be deleted\n");
500 ok(DeleteObject(hbm2),"bitmap 2 can't be deleted\n");
501 ok(DeleteObject(hbm3),"bitmap 3 can't be deleted\n");
502
503 ReleaseDC(hwndfortest, hdc);
504 DestroyWindow(hwndfortest);
505
506 return TRUE;
507 }
508
509 static void testMerge(void)
510 {
511 HIMAGELIST himl1, himl2, hmerge;
512 HICON hicon1;
513 HWND hwnd = create_a_window();
514
515 himl1 = ImageList_Create(32,32,0,0,3);
516 ok(himl1 != NULL,"failed to create himl1\n");
517
518 himl2 = ImageList_Create(32,32,0,0,3);
519 ok(himl2 != NULL,"failed to create himl2\n");
520
521 hicon1 = CreateIcon(hinst, 32, 32, 1, 1, icon_bits, icon_bits);
522 ok(hicon1 != NULL, "failed to create hicon1\n");
523
524 if (!himl1 || !himl2 || !hicon1)
525 return;
526
527 ok(0==ImageList_AddIcon(himl2, hicon1),"add icon1 to himl2 failed\n");
528 check_bits(hwnd, himl2, 0, 32, icon_bits, "add icon1 to himl2");
529
530 /* If himl1 has no images, merge still succeeds */
531 hmerge = ImageList_Merge(himl1, -1, himl2, 0, 0, 0);
532 ok(hmerge != NULL, "merge himl1,-1 failed\n");
533 check_bits(hwnd, hmerge, 0, 32, empty_bits, "merge himl1,-1");
534 if (hmerge) ImageList_Destroy(hmerge);
535
536 hmerge = ImageList_Merge(himl1, 0, himl2, 0, 0, 0);
537 ok(hmerge != NULL,"merge himl1,0 failed\n");
538 check_bits(hwnd, hmerge, 0, 32, empty_bits, "merge himl1,0");
539 if (hmerge) ImageList_Destroy(hmerge);
540
541 /* Same happens if himl2 is empty */
542 ImageList_Destroy(himl2);
543 himl2 = ImageList_Create(32,32,0,0,3);
544 ok(himl2 != NULL,"failed to recreate himl2\n");
545 if (!himl2)
546 return;
547
548 hmerge = ImageList_Merge(himl1, -1, himl2, -1, 0, 0);
549 ok(hmerge != NULL, "merge himl2,-1 failed\n");
550 check_bits(hwnd, hmerge, 0, 32, empty_bits, "merge himl2,-1");
551 if (hmerge) ImageList_Destroy(hmerge);
552
553 hmerge = ImageList_Merge(himl1, -1, himl2, 0, 0, 0);
554 ok(hmerge != NULL, "merge himl2,0 failed\n");
555 check_bits(hwnd, hmerge, 0, 32, empty_bits, "merge himl2,0");
556 if (hmerge) ImageList_Destroy(hmerge);
557
558 /* Now try merging an image with itself */
559 ok(0==ImageList_AddIcon(himl2, hicon1),"re-add icon1 to himl2 failed\n");
560
561 hmerge = ImageList_Merge(himl2, 0, himl2, 0, 0, 0);
562 ok(hmerge != NULL, "merge himl2 with itself failed\n");
563 check_bits(hwnd, hmerge, 0, 32, empty_bits, "merge himl2 with itself");
564 if (hmerge) ImageList_Destroy(hmerge);
565
566 /* Try merging 2 different image lists */
567 ok(0==ImageList_AddIcon(himl1, hicon1),"add icon1 to himl1 failed\n");
568
569 hmerge = ImageList_Merge(himl1, 0, himl2, 0, 0, 0);
570 ok(hmerge != NULL, "merge himl1 with himl2 failed\n");
571 check_bits(hwnd, hmerge, 0, 32, empty_bits, "merge himl1 with himl2");
572 if (hmerge) ImageList_Destroy(hmerge);
573
574 hmerge = ImageList_Merge(himl1, 0, himl2, 0, 8, 16);
575 ok(hmerge != NULL, "merge himl1 with himl2 8,16 failed\n");
576 check_bits(hwnd, hmerge, 0, 32, empty_bits, "merge himl1 with himl2, 8,16");
577 if (hmerge) ImageList_Destroy(hmerge);
578
579 ImageList_Destroy(himl1);
580 ImageList_Destroy(himl2);
581 DestroyIcon(hicon1);
582 DestroyWindow(hwnd);
583 }
584
585 /*********************** imagelist storage test ***************************/
586
587 #define BMP_CX 48
588
589 struct my_IStream
590 {
591 IStream is;
592 char *iml_data; /* written imagelist data */
593 ULONG iml_data_size;
594 };
595
596 static HRESULT STDMETHODCALLTYPE Test_Stream_QueryInterface(
597 IStream* This,
598 REFIID riid,
599 void** ppvObject)
600 {
601 assert(0);
602 return E_NOTIMPL;
603 }
604
605 static ULONG STDMETHODCALLTYPE Test_Stream_AddRef(
606 IStream* This)
607 {
608 assert(0);
609 return 2;
610 }
611
612 static ULONG STDMETHODCALLTYPE Test_Stream_Release(
613 IStream* This)
614 {
615 assert(0);
616 return 1;
617 }
618
619 static HRESULT STDMETHODCALLTYPE Test_Stream_Read(
620 IStream* This,
621 void* pv,
622 ULONG cb,
623 ULONG* pcbRead)
624 {
625 assert(0);
626 return E_NOTIMPL;
627 }
628
629 static BOOL allocate_storage(struct my_IStream *my_is, ULONG add)
630 {
631 my_is->iml_data_size += add;
632
633 if (!my_is->iml_data)
634 my_is->iml_data = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, my_is->iml_data_size);
635 else
636 my_is->iml_data = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, my_is->iml_data, my_is->iml_data_size);
637
638 return my_is->iml_data ? TRUE : FALSE;
639 }
640
641 static HRESULT STDMETHODCALLTYPE Test_Stream_Write(
642 IStream* This,
643 const void* pv,
644 ULONG cb,
645 ULONG* pcbWritten)
646 {
647 struct my_IStream *my_is = (struct my_IStream *)This;
648 ULONG current_iml_data_size = my_is->iml_data_size;
649
650 if (!allocate_storage(my_is, cb)) return E_FAIL;
651
652 memcpy(my_is->iml_data + current_iml_data_size, pv, cb);
653 if (pcbWritten) *pcbWritten = cb;
654
655 return S_OK;
656 }
657
658 static HRESULT STDMETHODCALLTYPE Test_Stream_Seek(
659 IStream* This,
660 LARGE_INTEGER dlibMove,
661 DWORD dwOrigin,
662 ULARGE_INTEGER* plibNewPosition)
663 {
664 assert(0);
665 return E_NOTIMPL;
666 }
667
668 static HRESULT STDMETHODCALLTYPE Test_Stream_SetSize(
669 IStream* This,
670 ULARGE_INTEGER libNewSize)
671 {
672 assert(0);
673 return E_NOTIMPL;
674 }
675
676 static HRESULT STDMETHODCALLTYPE Test_Stream_CopyTo(
677 IStream* This,
678 IStream* pstm,
679 ULARGE_INTEGER cb,
680 ULARGE_INTEGER* pcbRead,
681 ULARGE_INTEGER* pcbWritten)
682 {
683 assert(0);
684 return E_NOTIMPL;
685 }
686
687 static HRESULT STDMETHODCALLTYPE Test_Stream_Commit(
688 IStream* This,
689 DWORD grfCommitFlags)
690 {
691 assert(0);
692 return E_NOTIMPL;
693 }
694
695 static HRESULT STDMETHODCALLTYPE Test_Stream_Revert(
696 IStream* This)
697 {
698 assert(0);
699 return E_NOTIMPL;
700 }
701
702 static HRESULT STDMETHODCALLTYPE Test_Stream_LockRegion(
703 IStream* This,
704 ULARGE_INTEGER libOffset,
705 ULARGE_INTEGER cb,
706 DWORD dwLockType)
707 {
708 assert(0);
709 return E_NOTIMPL;
710 }
711
712 static HRESULT STDMETHODCALLTYPE Test_Stream_UnlockRegion(
713 IStream* This,
714 ULARGE_INTEGER libOffset,
715 ULARGE_INTEGER cb,
716 DWORD dwLockType)
717 {
718 assert(0);
719 return E_NOTIMPL;
720 }
721
722 static HRESULT STDMETHODCALLTYPE Test_Stream_Stat(
723 IStream* This,
724 STATSTG* pstatstg,
725 DWORD grfStatFlag)
726 {
727 assert(0);
728 return E_NOTIMPL;
729 }
730
731 static HRESULT STDMETHODCALLTYPE Test_Stream_Clone(
732 IStream* This,
733 IStream** ppstm)
734 {
735 assert(0);
736 return E_NOTIMPL;
737 }
738
739 static const IStreamVtbl Test_Stream_Vtbl =
740 {
741 Test_Stream_QueryInterface,
742 Test_Stream_AddRef,
743 Test_Stream_Release,
744 Test_Stream_Read,
745 Test_Stream_Write,
746 Test_Stream_Seek,
747 Test_Stream_SetSize,
748 Test_Stream_CopyTo,
749 Test_Stream_Commit,
750 Test_Stream_Revert,
751 Test_Stream_LockRegion,
752 Test_Stream_UnlockRegion,
753 Test_Stream_Stat,
754 Test_Stream_Clone
755 };
756
757 static struct my_IStream Test_Stream = { { &Test_Stream_Vtbl }, 0, 0 };
758
759 static INT DIB_GetWidthBytes( int width, int bpp )
760 {
761 int words;
762
763 switch (bpp)
764 {
765 case 1: words = (width + 31) / 32; break;
766 case 4: words = (width + 7) / 8; break;
767 case 8: words = (width + 3) / 4; break;
768 case 15:
769 case 16: words = (width + 1) / 2; break;
770 case 24: words = (width * 3 + 3)/4; break;
771 case 32: words = width; break;
772
773 default:
774 words=0;
775 trace("Unknown depth %d, please report.\n", bpp );
776 assert(0);
777 break;
778 }
779 return 4 * words;
780 }
781
782 static void check_bitmap_data(const char *bm_data, ULONG bm_data_size,
783 INT width, INT height, INT bpp,
784 const char *comment)
785 {
786 const BITMAPFILEHEADER *bmfh = (const BITMAPFILEHEADER *)bm_data;
787 const BITMAPINFOHEADER *bmih = (const BITMAPINFOHEADER *)(bm_data + sizeof(*bmfh));
788 ULONG hdr_size, image_size;
789
790 hdr_size = sizeof(*bmfh) + sizeof(*bmih);
791 if (bmih->biBitCount <= 8) hdr_size += (1 << bpp) * sizeof(RGBQUAD);
792
793 ok(bmfh->bfType == (('M' << 8) | 'B'), "wrong bfType 0x%02x\n", bmfh->bfType);
794 ok(bmfh->bfSize == hdr_size, "wrong bfSize 0x%02x\n", bmfh->bfSize);
795 ok(bmfh->bfReserved1 == 0, "wrong bfReserved1 0x%02x\n", bmfh->bfReserved1);
796 ok(bmfh->bfReserved2 == 0, "wrong bfReserved2 0x%02x\n", bmfh->bfReserved2);
797 ok(bmfh->bfOffBits == hdr_size, "wrong bfOffBits 0x%02x\n", bmfh->bfOffBits);
798
799 ok(bmih->biSize == sizeof(*bmih), "wrong biSize %d\n", bmih->biSize);
800 ok(bmih->biWidth == width, "wrong biWidth %d (expected %d)\n", bmih->biWidth, width);
801 ok(bmih->biHeight == height, "wrong biHeight %d (expected %d)\n", bmih->biHeight, height);
802 ok(bmih->biPlanes == 1, "wrong biPlanes %d\n", bmih->biPlanes);
803 ok(bmih->biBitCount == bpp, "wrong biBitCount %d\n", bmih->biBitCount);
804
805 image_size = DIB_GetWidthBytes(bmih->biWidth, bmih->biBitCount) * bmih->biHeight;
806 ok(bmih->biSizeImage == image_size, "wrong biSizeImage %u\n", bmih->biSizeImage);
807 #if 0
808 {
809 char fname[256];
810 FILE *f;
811 sprintf(fname, "bmp_%s.bmp", comment);
812 f = fopen(fname, "wb");
813 fwrite(bm_data, 1, bm_data_size, f);
814 fclose(f);
815 }
816 #endif
817 }
818
819 static void check_ilhead_data(const char *ilh_data, INT cx, INT cy, INT cur, INT max)
820 {
821 ILHEAD *ilh = (ILHEAD *)ilh_data;
822
823 ok(ilh->usMagic == IMAGELIST_MAGIC, "wrong usMagic %4x (expected %02x)\n", ilh->usMagic, IMAGELIST_MAGIC);
824 ok(ilh->usVersion == 0x101, "wrong usVersion %x (expected 0x101)\n", ilh->usVersion);
825 ok(ilh->cCurImage == cur, "wrong cCurImage %d (expected %d)\n", ilh->cCurImage, cur);
826 ok(ilh->cMaxImage == max, "wrong cMaxImage %d (expected %d)\n", ilh->cMaxImage, max);
827 ok(ilh->cGrow == 4, "wrong cGrow %d (expected 4)\n", ilh->cGrow);
828 ok(ilh->cx == cx, "wrong cx %d (expected %d)\n", ilh->cx, cx);
829 ok(ilh->cy == cy, "wrong cy %d (expected %d)\n", ilh->cy, cy);
830 ok(ilh->bkcolor == CLR_NONE, "wrong bkcolor %x\n", ilh->bkcolor);
831 ok(ilh->flags == ILC_COLOR24, "wrong flags %04x\n", ilh->flags);
832 ok(ilh->ovls[0] == -1, "wrong ovls[0] %04x\n", ilh->ovls[0]);
833 ok(ilh->ovls[1] == -1, "wrong ovls[1] %04x\n", ilh->ovls[1]);
834 ok(ilh->ovls[2] == -1, "wrong ovls[2] %04x\n", ilh->ovls[2]);
835 ok(ilh->ovls[3] == -1, "wrong ovls[3] %04x\n", ilh->ovls[3]);
836 }
837
838 static HBITMAP create_bitmap(INT cx, INT cy, COLORREF color, const char *comment)
839 {
840 HDC hdc;
841 char bmibuf[sizeof(BITMAPINFO) + 256 * sizeof(RGBQUAD)];
842 BITMAPINFO *bmi = (BITMAPINFO *)bmibuf;
843 HBITMAP hbmp, hbmp_old;
844 HBRUSH hbrush;
845 RECT rc = { 0, 0, cx, cy };
846
847 hdc = CreateCompatibleDC(0);
848
849 memset(bmi, 0, sizeof(*bmi));
850 bmi->bmiHeader.biSize = sizeof(bmi->bmiHeader);
851 bmi->bmiHeader.biHeight = cx;
852 bmi->bmiHeader.biWidth = cy;
853 bmi->bmiHeader.biBitCount = 24;
854 bmi->bmiHeader.biPlanes = 1;
855 bmi->bmiHeader.biCompression = BI_RGB;
856 hbmp = CreateDIBSection(hdc, bmi, DIB_RGB_COLORS, NULL, NULL, 0);
857
858 hbmp_old = SelectObject(hdc, hbmp);
859
860 hbrush = CreateSolidBrush(color);
861 FillRect(hdc, &rc, hbrush);
862 DeleteObject(hbrush);
863
864 DrawText(hdc, comment, -1, &rc, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
865
866 SelectObject(hdc, hbmp_old);
867 DeleteDC(hdc);
868
869 return hbmp;
870 }
871
872 static void image_list_init(HIMAGELIST himl)
873 {
874 HBITMAP hbm;
875 char comment[16];
876 INT n = 1;
877
878 #define add_bitmap(grey) \
879 sprintf(comment, "%d", n++); \
880 hbm = create_bitmap(BMP_CX, BMP_CX, RGB((grey),(grey),(grey)), comment); \
881 ImageList_Add(himl, hbm, NULL);
882
883 add_bitmap(255); add_bitmap(170); add_bitmap(85); add_bitmap(0);
884 add_bitmap(0); add_bitmap(85); add_bitmap(170); add_bitmap(255);
885 add_bitmap(255); add_bitmap(170); add_bitmap(85); add_bitmap(0);
886 add_bitmap(0); add_bitmap(85); add_bitmap(170); add_bitmap(255);
887 add_bitmap(255); add_bitmap(170); add_bitmap(85); add_bitmap(0);
888 add_bitmap(0); add_bitmap(85); add_bitmap(170); add_bitmap(255);
889 #undef add_bitmap
890 }
891
892 #define iml_clear_stream_data() \
893 HeapFree(GetProcessHeap(), 0, Test_Stream.iml_data); \
894 Test_Stream.iml_data = NULL; \
895 Test_Stream.iml_data_size = 0;
896
897 static void check_iml_data(HIMAGELIST himl, INT cx, INT cy, INT cur, INT max,
898 INT width, INT height, INT bpp, const char *comment)
899 {
900 INT ret, cxx, cyy;
901
902 ret = ImageList_GetImageCount(himl);
903 ok(ret == cur, "expected cur %d got %d\n", cur, ret);
904
905 ret = ImageList_GetIconSize(himl, &cxx, &cyy);
906 ok(ret, "ImageList_GetIconSize failed\n");
907 ok(cxx == cx, "wrong cx %d (expected %d)\n", cxx, cx);
908 ok(cyy == cy, "wrong cy %d (expected %d)\n", cyy, cy);
909
910 iml_clear_stream_data();
911 ret = ImageList_Write(himl, &Test_Stream.is);
912 ok(ret, "ImageList_Write failed\n");
913
914 ok(Test_Stream.iml_data != 0, "ImageList_Write didn't write any data\n");
915 ok(Test_Stream.iml_data_size > sizeof(ILHEAD), "ImageList_Write wrote not enough data\n");
916
917 check_ilhead_data(Test_Stream.iml_data, cx, cy, cur, max);
918 check_bitmap_data(Test_Stream.iml_data + sizeof(ILHEAD),
919 Test_Stream.iml_data_size - sizeof(ILHEAD),
920 width, height, bpp, comment);
921 }
922
923 static void test_imagelist_storage(void)
924 {
925 HIMAGELIST himl;
926 BOOL ret;
927
928 himl = ImageList_Create(BMP_CX, BMP_CX, ILC_COLOR24, 1, 1);
929 ok(himl != 0, "ImageList_Create failed\n");
930
931 check_iml_data(himl, BMP_CX, BMP_CX, 0, 2, BMP_CX * 4, BMP_CX * 1, 24, "empty");
932
933 image_list_init(himl);
934 check_iml_data(himl, BMP_CX, BMP_CX, 24, 27, BMP_CX * 4, BMP_CX * 7, 24, "orig");
935
936 ret = ImageList_Remove(himl, 4);
937 ok(ret, "ImageList_Remove failed\n");
938 check_iml_data(himl, BMP_CX, BMP_CX, 23, 27, BMP_CX * 4, BMP_CX * 7, 24, "1");
939
940 ret = ImageList_Remove(himl, 5);
941 ok(ret, "ImageList_Remove failed\n");
942 check_iml_data(himl, BMP_CX, BMP_CX, 22, 27, BMP_CX * 4, BMP_CX * 7, 24, "2");
943
944 ret = ImageList_Remove(himl, 6);
945 ok(ret, "ImageList_Remove failed\n");
946 check_iml_data(himl, BMP_CX, BMP_CX, 21, 27, BMP_CX * 4, BMP_CX * 7, 24, "3");
947
948 ret = ImageList_Remove(himl, 7);
949 ok(ret, "ImageList_Remove failed\n");
950 check_iml_data(himl, BMP_CX, BMP_CX, 20, 27, BMP_CX * 4, BMP_CX * 7, 24, "4");
951
952 ret = ImageList_Remove(himl, -2);
953 ok(!ret, "ImageList_Remove(-2) should fail\n");
954 check_iml_data(himl, BMP_CX, BMP_CX, 20, 27, BMP_CX * 4, BMP_CX * 7, 24, "5");
955
956 ret = ImageList_Remove(himl, 20);
957 ok(!ret, "ImageList_Remove(20) should fail\n");
958 check_iml_data(himl, BMP_CX, BMP_CX, 20, 27, BMP_CX * 4, BMP_CX * 7, 24, "6");
959
960 ret = ImageList_Remove(himl, -1);
961 ok(ret, "ImageList_Remove(-1) failed\n");
962 check_iml_data(himl, BMP_CX, BMP_CX, 0, 4, BMP_CX * 4, BMP_CX * 1, 24, "7");
963
964 ret = ImageList_Destroy(himl);
965 ok(ret, "ImageList_Destroy failed\n");
966
967 iml_clear_stream_data();
968 }
969
970 START_TEST(imagelist)
971 {
972 desktopDC=GetDC(NULL);
973 hinst = GetModuleHandleA(NULL);
974
975 InitCommonControls();
976
977 testHotspot();
978 DoTest1();
979 DoTest2();
980 DoTest3();
981 testMerge();
982 test_imagelist_storage();
983 }