dinput: Stub IDirectInputJoyConfig8 interface.
[wine] / dlls / comctl32 / imagelist.c
1 /*
2  *  ImageList implementation
3  *
4  *  Copyright 1998 Eric Kohl
5  *  Copyright 2000 Jason Mawdsley
6  *  Copyright 2001, 2004 Michael Stefaniuc
7  *  Copyright 2001 Charles Loep for CodeWeavers
8  *  Copyright 2002 Dimitrie O. Paun
9  *  Copyright 2009 Owen Rudge for CodeWeavers
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  * Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public
22  * License along with this library; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
24  *
25  * NOTE
26  *
27  * This code was audited for completeness against the documented features
28  * of Comctl32.dll version 6.0 on Sep. 12, 2002, by Dimitrie O. Paun.
29  *
30  * Unless otherwise noted, we believe this code to be complete, as per
31  * the specification mentioned above.
32  * If you discover missing features, or bugs, please note them below.
33  *
34  *  TODO:
35  *    - Add support for ILD_PRESERVEALPHA, ILD_SCALE, ILD_DPISCALE
36  *    - Add support for ILS_GLOW, ILS_SHADOW, ILS_SATURATE
37  *    - Thread-safe locking
38  */
39
40 #include <stdarg.h>
41 #include <stdlib.h>
42 #include <string.h>
43
44 #define COBJMACROS
45
46 #include "winerror.h"
47 #include "windef.h"
48 #include "winbase.h"
49 #include "objbase.h"
50 #include "wingdi.h"
51 #include "winuser.h"
52 #include "commctrl.h"
53 #include "comctl32.h"
54 #include "commoncontrols.h"
55 #include "wine/debug.h"
56 #include "wine/exception.h"
57
58 WINE_DEFAULT_DEBUG_CHANNEL(imagelist);
59
60 #define MAX_OVERLAYIMAGE 15
61
62 struct _IMAGELIST
63 {
64     const struct IImageListVtbl *lpVtbl; /* 00: IImageList vtable */
65
66     INT         cCurImage;                 /* 04: ImageCount */
67     INT         cMaxImage;                 /* 08: maximages */
68     INT         cGrow;                     /* 0C: cGrow */
69     INT         cx;                        /* 10: cx */
70     INT         cy;                        /* 14: cy */
71     DWORD       x4;
72     UINT        flags;                     /* 1C: flags */
73     COLORREF    clrFg;                     /* 20: foreground color */
74     COLORREF    clrBk;                     /* 24: background color */
75
76
77     HBITMAP     hbmImage;                  /* 28: images Bitmap */
78     HBITMAP     hbmMask;                   /* 2C: masks  Bitmap */
79     HDC         hdcImage;                  /* 30: images MemDC  */
80     HDC         hdcMask;                   /* 34: masks  MemDC  */
81     INT         nOvlIdx[MAX_OVERLAYIMAGE]; /* 38: overlay images index */
82
83     /* not yet found out */
84     HBRUSH  hbrBlend25;
85     HBRUSH  hbrBlend50;
86     INT     cInitial;
87     UINT    uBitsPixel;
88     char   *has_alpha;
89
90     LONG        ref;                       /* reference count */
91 };
92
93 #define IMAGELIST_MAGIC 0x53414D58
94
95 /* Header used by ImageList_Read() and ImageList_Write() */
96 #include "pshpack2.h"
97 typedef struct _ILHEAD
98 {
99     USHORT      usMagic;
100     USHORT      usVersion;
101     WORD        cCurImage;
102     WORD        cMaxImage;
103     WORD        cGrow;
104     WORD        cx;
105     WORD        cy;
106     COLORREF    bkcolor;
107     WORD        flags;
108     SHORT       ovls[4];
109 } ILHEAD;
110 #include "poppack.h"
111
112 /* internal image list data used for Drag & Drop operations */
113 typedef struct
114 {
115     HWND        hwnd;
116     HIMAGELIST  himl;
117     /* position of the drag image relative to the window */
118     INT         x;
119     INT         y;
120     /* offset of the hotspot relative to the origin of the image */
121     INT         dxHotspot;
122     INT         dyHotspot;
123     /* is the drag image visible */
124     BOOL        bShow;
125     /* saved background */
126     HBITMAP     hbmBg;
127 } INTERNALDRAG;
128
129 static INTERNALDRAG InternalDrag = { 0, 0, 0, 0, 0, 0, FALSE, 0 };
130
131 static HBITMAP ImageList_CreateImage(HDC hdc, HIMAGELIST himl, UINT count);
132 static HRESULT ImageListImpl_CreateInstance(const IUnknown *pUnkOuter, REFIID iid, void** ppv);
133 static inline BOOL is_valid(HIMAGELIST himl);
134
135 /*
136  * An imagelist with N images is tiled like this:
137  *
138  *   N/4 ->
139  *
140  * 4 048C..
141  *   159D..
142  * | 26AE.N
143  * V 37BF.
144  */
145
146 #define TILE_COUNT 4
147
148 static inline UINT imagelist_height( UINT count )
149 {
150     return ((count + TILE_COUNT - 1)/TILE_COUNT);
151 }
152
153 static inline void imagelist_point_from_index( HIMAGELIST himl, UINT index, LPPOINT pt )
154 {
155     pt->x = (index%TILE_COUNT) * himl->cx;
156     pt->y = (index/TILE_COUNT) * himl->cy;
157 }
158
159 static inline void imagelist_get_bitmap_size( HIMAGELIST himl, UINT count, SIZE *sz )
160 {
161     sz->cx = himl->cx * TILE_COUNT;
162     sz->cy = imagelist_height( count ) * himl->cy;
163 }
164
165 static inline int get_dib_stride( int width, int bpp )
166 {
167     return ((width * bpp + 31) >> 3) & ~3;
168 }
169
170 static inline int get_dib_image_size( const BITMAPINFO *info )
171 {
172     return get_dib_stride( info->bmiHeader.biWidth, info->bmiHeader.biBitCount )
173         * abs( info->bmiHeader.biHeight );
174 }
175
176 /*
177  * imagelist_copy_images()
178  *
179  * Copies a block of count images from offset src in the list to offset dest.
180  * Images are copied a row at at time. Assumes hdcSrc and hdcDest are different.
181  */
182 static inline void imagelist_copy_images( HIMAGELIST himl, HDC hdcSrc, HDC hdcDest,
183                                           UINT src, UINT count, UINT dest )
184 {
185     POINT ptSrc, ptDest;
186     SIZE sz;
187     UINT i;
188
189     for ( i=0; i<TILE_COUNT; i++ )
190     {
191         imagelist_point_from_index( himl, src+i, &ptSrc );
192         imagelist_point_from_index( himl, dest+i, &ptDest );
193         sz.cx = himl->cx;
194         sz.cy = himl->cy * imagelist_height( count - i );
195
196         BitBlt( hdcDest, ptDest.x, ptDest.y, sz.cx, sz.cy,
197                 hdcSrc, ptSrc.x, ptSrc.y, SRCCOPY );
198     }
199 }
200
201 static void add_dib_bits( HIMAGELIST himl, int pos, int count, int width, int height,
202                           BITMAPINFO *info, BITMAPINFO *mask_info, DWORD *bits, BYTE *mask_bits )
203 {
204     int i, j, n;
205     POINT pt;
206     int stride = info->bmiHeader.biWidth;
207     int mask_stride = (info->bmiHeader.biWidth + 31) / 32 * 4;
208
209     for (n = 0; n < count; n++)
210     {
211         int has_alpha = 0;
212
213         imagelist_point_from_index( himl, pos + n, &pt );
214
215         /* check if bitmap has an alpha channel */
216         for (i = 0; i < height && !has_alpha; i++)
217             for (j = n * width; j < (n + 1) * width; j++)
218                 if ((has_alpha = ((bits[i * stride + j] & 0xff000000) != 0))) break;
219
220         if (!has_alpha)  /* generate alpha channel from the mask */
221         {
222             for (i = 0; i < height; i++)
223                 for (j = n * width; j < (n + 1) * width; j++)
224                     if (!mask_info || !((mask_bits[i * mask_stride + j / 8] << (j % 8)) & 0x80))
225                         bits[i * stride + j] |= 0xff000000;
226                     else
227                         bits[i * stride + j] = 0;
228         }
229         else
230         {
231             himl->has_alpha[pos + n] = 1;
232
233             if (mask_info && himl->hbmMask)  /* generate the mask from the alpha channel */
234             {
235                 for (i = 0; i < height; i++)
236                     for (j = n * width; j < (n + 1) * width; j++)
237                         if ((bits[i * stride + j] >> 24) > 25) /* more than 10% alpha */
238                             mask_bits[i * mask_stride + j / 8] &= ~(0x80 >> (j % 8));
239                         else
240                             mask_bits[i * mask_stride + j / 8] |= 0x80 >> (j % 8);
241             }
242         }
243         StretchDIBits( himl->hdcImage, pt.x, pt.y, himl->cx, himl->cy,
244                        n * width, 0, width, height, bits, info, DIB_RGB_COLORS, SRCCOPY );
245         if (mask_info)
246             StretchDIBits( himl->hdcMask, pt.x, pt.y, himl->cx, himl->cy,
247                            n * width, 0, width, height, mask_bits, mask_info, DIB_RGB_COLORS, SRCCOPY );
248     }
249 }
250
251 /* add images with an alpha channel when the image list is 32 bpp */
252 static BOOL add_with_alpha( HIMAGELIST himl, HDC hdc, int pos, int count,
253                             int width, int height, HBITMAP hbmImage, HBITMAP hbmMask )
254 {
255     BOOL ret = FALSE;
256     BITMAP bm;
257     BITMAPINFO *info, *mask_info = NULL;
258     DWORD *bits = NULL;
259     BYTE *mask_bits = NULL;
260     DWORD mask_width;
261
262     if (!GetObjectW( hbmImage, sizeof(bm), &bm )) return FALSE;
263
264     /* if either the imagelist or the source bitmap don't have an alpha channel, bail out now */
265     if (!himl->has_alpha) return FALSE;
266     if (bm.bmBitsPixel != 32) return FALSE;
267
268     SelectObject( hdc, hbmImage );
269     mask_width = (bm.bmWidth + 31) / 32 * 4;
270
271     if (!(info = HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET( BITMAPINFO, bmiColors[256] )))) goto done;
272     info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
273     info->bmiHeader.biWidth = bm.bmWidth;
274     info->bmiHeader.biHeight = -height;
275     info->bmiHeader.biPlanes = 1;
276     info->bmiHeader.biBitCount = 32;
277     info->bmiHeader.biCompression = BI_RGB;
278     info->bmiHeader.biSizeImage = bm.bmWidth * height * 4;
279     info->bmiHeader.biXPelsPerMeter = 0;
280     info->bmiHeader.biYPelsPerMeter = 0;
281     info->bmiHeader.biClrUsed = 0;
282     info->bmiHeader.biClrImportant = 0;
283     if (!(bits = HeapAlloc( GetProcessHeap(), 0, info->bmiHeader.biSizeImage ))) goto done;
284     if (!GetDIBits( hdc, hbmImage, 0, height, bits, info, DIB_RGB_COLORS )) goto done;
285
286     if (hbmMask)
287     {
288         if (!(mask_info = HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET( BITMAPINFO, bmiColors[2] ))))
289             goto done;
290         mask_info->bmiHeader = info->bmiHeader;
291         mask_info->bmiHeader.biBitCount = 1;
292         mask_info->bmiHeader.biSizeImage = mask_width * height;
293         if (!(mask_bits = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, info->bmiHeader.biSizeImage )))
294             goto done;
295         if (!GetDIBits( hdc, hbmMask, 0, height, mask_bits, mask_info, DIB_RGB_COLORS )) goto done;
296     }
297
298     add_dib_bits( himl, pos, count, width, height, info, mask_info, bits, mask_bits );
299     ret = TRUE;
300
301 done:
302     HeapFree( GetProcessHeap(), 0, info );
303     HeapFree( GetProcessHeap(), 0, mask_info );
304     HeapFree( GetProcessHeap(), 0, bits );
305     HeapFree( GetProcessHeap(), 0, mask_bits );
306     return ret;
307 }
308
309 /*************************************************************************
310  * IMAGELIST_InternalExpandBitmaps [Internal]
311  *
312  * Expands the bitmaps of an image list by the given number of images.
313  *
314  * PARAMS
315  *     himl        [I] handle to image list
316  *     nImageCount [I] number of images to add
317  *
318  * RETURNS
319  *     nothing
320  *
321  * NOTES
322  *     This function CANNOT be used to reduce the number of images.
323  */
324 static void
325 IMAGELIST_InternalExpandBitmaps(HIMAGELIST himl, INT nImageCount)
326 {
327     HDC     hdcBitmap;
328     HBITMAP hbmNewBitmap, hbmNull;
329     INT     nNewCount;
330     SIZE    sz;
331
332     TRACE("%p has allocated %d, max %d, grow %d images\n", himl, himl->cCurImage, himl->cMaxImage, himl->cGrow);
333
334     if (himl->cCurImage + nImageCount < himl->cMaxImage)
335         return;
336
337     nNewCount = himl->cMaxImage + max(nImageCount, himl->cGrow) + 1;
338
339     imagelist_get_bitmap_size(himl, nNewCount, &sz);
340
341     TRACE("Create expanded bitmaps : himl=%p x=%d y=%d count=%d\n", himl, sz.cx, sz.cy, nNewCount);
342     hdcBitmap = CreateCompatibleDC (0);
343
344     hbmNewBitmap = ImageList_CreateImage(hdcBitmap, himl, nNewCount);
345
346     if (hbmNewBitmap == 0)
347         ERR("creating new image bitmap (x=%d y=%d)!\n", sz.cx, sz.cy);
348
349     if (himl->cCurImage)
350     {
351         hbmNull = SelectObject (hdcBitmap, hbmNewBitmap);
352         BitBlt (hdcBitmap, 0, 0, sz.cx, sz.cy,
353                 himl->hdcImage, 0, 0, SRCCOPY);
354         SelectObject (hdcBitmap, hbmNull);
355     }
356     SelectObject (himl->hdcImage, hbmNewBitmap);
357     DeleteObject (himl->hbmImage);
358     himl->hbmImage = hbmNewBitmap;
359
360     if (himl->flags & ILC_MASK)
361     {
362         hbmNewBitmap = CreateBitmap (sz.cx, sz.cy, 1, 1, NULL);
363
364         if (hbmNewBitmap == 0)
365             ERR("creating new mask bitmap!\n");
366
367         if(himl->cCurImage)
368         {
369             hbmNull = SelectObject (hdcBitmap, hbmNewBitmap);
370             BitBlt (hdcBitmap, 0, 0, sz.cx, sz.cy,
371                     himl->hdcMask, 0, 0, SRCCOPY);
372             SelectObject (hdcBitmap, hbmNull);
373         }
374         SelectObject (himl->hdcMask, hbmNewBitmap);
375         DeleteObject (himl->hbmMask);
376         himl->hbmMask = hbmNewBitmap;
377     }
378
379     if (himl->has_alpha)
380     {
381         char *new_alpha = HeapReAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, himl->has_alpha, nNewCount );
382         if (new_alpha) himl->has_alpha = new_alpha;
383         else
384         {
385             HeapFree( GetProcessHeap(), 0, himl->has_alpha );
386             himl->has_alpha = NULL;
387         }
388     }
389
390     himl->cMaxImage = nNewCount;
391
392     DeleteDC (hdcBitmap);
393 }
394
395
396 /*************************************************************************
397  * ImageList_Add [COMCTL32.@]
398  *
399  * Add an image or images to an image list.
400  *
401  * PARAMS
402  *     himl     [I] handle to image list
403  *     hbmImage [I] handle to image bitmap
404  *     hbmMask  [I] handle to mask bitmap
405  *
406  * RETURNS
407  *     Success: Index of the first new image.
408  *     Failure: -1
409  */
410
411 INT WINAPI
412 ImageList_Add (HIMAGELIST himl, HBITMAP hbmImage, HBITMAP hbmMask)
413 {
414     HDC     hdcBitmap, hdcTemp = 0;
415     INT     nFirstIndex, nImageCount, i;
416     BITMAP  bmp;
417     POINT   pt;
418
419     TRACE("himl=%p hbmimage=%p hbmmask=%p\n", himl, hbmImage, hbmMask);
420     if (!is_valid(himl))
421         return -1;
422
423     if (!GetObjectW(hbmImage, sizeof(BITMAP), &bmp))
424         return -1;
425
426     TRACE("himl %p, cCurImage %d, cMaxImage %d, cGrow %d, cx %d, cy %d\n",
427           himl, himl->cCurImage, himl->cMaxImage, himl->cGrow, himl->cx, himl->cy);
428
429     nImageCount = bmp.bmWidth / himl->cx;
430
431     TRACE("%p has %d images (%d x %d)\n", hbmImage, nImageCount, bmp.bmWidth, bmp.bmHeight);
432
433     IMAGELIST_InternalExpandBitmaps(himl, nImageCount);
434
435     hdcBitmap = CreateCompatibleDC(0);
436
437     SelectObject(hdcBitmap, hbmImage);
438
439     if (add_with_alpha( himl, hdcBitmap, himl->cCurImage, nImageCount,
440                         himl->cx, min( himl->cy, bmp.bmHeight), hbmImage, hbmMask ))
441         goto done;
442
443     if (himl->hbmMask)
444     {
445         hdcTemp = CreateCompatibleDC(0);
446         SelectObject(hdcTemp, hbmMask);
447     }
448
449     for (i=0; i<nImageCount; i++)
450     {
451         imagelist_point_from_index( himl, himl->cCurImage + i, &pt );
452
453         /* Copy result to the imagelist
454         */
455         BitBlt( himl->hdcImage, pt.x, pt.y, himl->cx, bmp.bmHeight,
456                 hdcBitmap, i*himl->cx, 0, SRCCOPY );
457
458         if (!himl->hbmMask)
459              continue;
460
461         BitBlt( himl->hdcMask, pt.x, pt.y, himl->cx, bmp.bmHeight,
462                 hdcTemp, i*himl->cx, 0, SRCCOPY );
463
464         /* Remove the background from the image
465         */
466         BitBlt( himl->hdcImage, pt.x, pt.y, himl->cx, bmp.bmHeight,
467                 himl->hdcMask, pt.x, pt.y, 0x220326 ); /* NOTSRCAND */
468     }
469     if (hdcTemp) DeleteDC(hdcTemp);
470
471 done:
472     DeleteDC(hdcBitmap);
473
474     nFirstIndex = himl->cCurImage;
475     himl->cCurImage += nImageCount;
476
477     return nFirstIndex;
478 }
479
480
481 /*************************************************************************
482  * ImageList_AddIcon [COMCTL32.@]
483  *
484  * Adds an icon to an image list.
485  *
486  * PARAMS
487  *     himl  [I] handle to image list
488  *     hIcon [I] handle to icon
489  *
490  * RETURNS
491  *     Success: index of the new image
492  *     Failure: -1
493  */
494 #undef ImageList_AddIcon
495 INT WINAPI ImageList_AddIcon (HIMAGELIST himl, HICON hIcon)
496 {
497     return ImageList_ReplaceIcon (himl, -1, hIcon);
498 }
499
500
501 /*************************************************************************
502  * ImageList_AddMasked [COMCTL32.@]
503  *
504  * Adds an image or images to an image list and creates a mask from the
505  * specified bitmap using the mask color.
506  *
507  * PARAMS
508  *     himl    [I] handle to image list.
509  *     hBitmap [I] handle to bitmap
510  *     clrMask [I] mask color.
511  *
512  * RETURNS
513  *     Success: Index of the first new image.
514  *     Failure: -1
515  */
516
517 INT WINAPI
518 ImageList_AddMasked (HIMAGELIST himl, HBITMAP hBitmap, COLORREF clrMask)
519 {
520     HDC    hdcMask, hdcBitmap;
521     INT    ret;
522     BITMAP bmp;
523     HBITMAP hMaskBitmap;
524     COLORREF bkColor;
525
526     TRACE("himl=%p hbitmap=%p clrmask=%x\n", himl, hBitmap, clrMask);
527     if (!is_valid(himl))
528         return -1;
529
530     if (!GetObjectW(hBitmap, sizeof(BITMAP), &bmp))
531         return -1;
532
533     hdcBitmap = CreateCompatibleDC(0);
534     SelectObject(hdcBitmap, hBitmap);
535
536     /* Create a temp Mask so we can remove the background of the Image */
537     hdcMask = CreateCompatibleDC(0);
538     hMaskBitmap = CreateBitmap(bmp.bmWidth, bmp.bmHeight, 1, 1, NULL);
539     SelectObject(hdcMask, hMaskBitmap);
540
541     /* create monochrome image to the mask bitmap */
542     bkColor = (clrMask != CLR_DEFAULT) ? clrMask : GetPixel (hdcBitmap, 0, 0);
543     SetBkColor (hdcBitmap, bkColor);
544     BitBlt (hdcMask, 0, 0, bmp.bmWidth, bmp.bmHeight, hdcBitmap, 0, 0, SRCCOPY);
545
546     SetBkColor(hdcBitmap, RGB(255,255,255));
547
548     /*
549      * Remove the background from the image
550      *
551      * WINDOWS BUG ALERT!!!!!!
552      *  The statement below should not be done in common practice
553      *  but this is how ImageList_AddMasked works in Windows.
554      *  It overwrites the original bitmap passed, this was discovered
555      *  by using the same bitmap to iterate the different styles
556      *  on windows where it failed (BUT ImageList_Add is OK)
557      *  This is here in case some apps rely on this bug
558      *
559      *  Blt mode 0x220326 is NOTSRCAND
560      */
561     BitBlt(hdcBitmap, 0, 0, bmp.bmWidth, bmp.bmHeight, hdcMask, 0, 0, 0x220326);
562
563     DeleteDC(hdcBitmap);
564     DeleteDC(hdcMask);
565
566     ret = ImageList_Add( himl, hBitmap, hMaskBitmap );
567
568     DeleteObject(hMaskBitmap);
569     return ret;
570 }
571
572
573 /*************************************************************************
574  * ImageList_BeginDrag [COMCTL32.@]
575  *
576  * Creates a temporary image list that contains one image. It will be used
577  * as a drag image.
578  *
579  * PARAMS
580  *     himlTrack [I] handle to the source image list
581  *     iTrack    [I] index of the drag image in the source image list
582  *     dxHotspot [I] X position of the hot spot of the drag image
583  *     dyHotspot [I] Y position of the hot spot of the drag image
584  *
585  * RETURNS
586  *     Success: TRUE
587  *     Failure: FALSE
588  */
589
590 BOOL WINAPI
591 ImageList_BeginDrag (HIMAGELIST himlTrack, INT iTrack,
592                      INT dxHotspot, INT dyHotspot)
593 {
594     INT cx, cy;
595
596     TRACE("(himlTrack=%p iTrack=%d dx=%d dy=%d)\n", himlTrack, iTrack,
597           dxHotspot, dyHotspot);
598
599     if (!is_valid(himlTrack))
600         return FALSE;
601
602     if (InternalDrag.himl)
603         ImageList_EndDrag ();
604
605     cx = himlTrack->cx;
606     cy = himlTrack->cy;
607
608     InternalDrag.himl = ImageList_Create (cx, cy, himlTrack->flags, 1, 1);
609     if (InternalDrag.himl == NULL) {
610         WARN("Error creating drag image list!\n");
611         return FALSE;
612     }
613
614     InternalDrag.dxHotspot = dxHotspot;
615     InternalDrag.dyHotspot = dyHotspot;
616
617     /* copy image */
618     BitBlt (InternalDrag.himl->hdcImage, 0, 0, cx, cy, himlTrack->hdcImage, iTrack * cx, 0, SRCCOPY);
619
620     /* copy mask */
621     BitBlt (InternalDrag.himl->hdcMask, 0, 0, cx, cy, himlTrack->hdcMask, iTrack * cx, 0, SRCCOPY);
622
623     InternalDrag.himl->cCurImage = 1;
624
625     return TRUE;
626 }
627
628
629 /*************************************************************************
630  * ImageList_Copy [COMCTL32.@]
631  *
632  *  Copies an image of the source image list to an image of the
633  *  destination image list. Images can be copied or swapped.
634  *
635  * PARAMS
636  *     himlDst [I] handle to the destination image list
637  *     iDst    [I] destination image index.
638  *     himlSrc [I] handle to the source image list
639  *     iSrc    [I] source image index
640  *     uFlags  [I] flags for the copy operation
641  *
642  * RETURNS
643  *     Success: TRUE
644  *     Failure: FALSE
645  *
646  * NOTES
647  *     Copying from one image list to another is possible. The original
648  *     implementation just copies or swaps within one image list.
649  *     Could this feature become a bug??? ;-)
650  */
651
652 BOOL WINAPI
653 ImageList_Copy (HIMAGELIST himlDst, INT iDst,   HIMAGELIST himlSrc,
654                 INT iSrc, UINT uFlags)
655 {
656     POINT ptSrc, ptDst;
657
658     TRACE("himlDst=%p iDst=%d himlSrc=%p iSrc=%d\n", himlDst, iDst, himlSrc, iSrc);
659
660     if (!is_valid(himlSrc) || !is_valid(himlDst))
661         return FALSE;
662     if ((iDst < 0) || (iDst >= himlDst->cCurImage))
663         return FALSE;
664     if ((iSrc < 0) || (iSrc >= himlSrc->cCurImage))
665         return FALSE;
666
667     imagelist_point_from_index( himlDst, iDst, &ptDst );
668     imagelist_point_from_index( himlSrc, iSrc, &ptSrc );
669
670     if (uFlags & ILCF_SWAP) {
671         /* swap */
672         HDC     hdcBmp;
673         HBITMAP hbmTempImage, hbmTempMask;
674
675         hdcBmp = CreateCompatibleDC (0);
676
677         /* create temporary bitmaps */
678         hbmTempImage = CreateBitmap (himlSrc->cx, himlSrc->cy, 1,
679                                        himlSrc->uBitsPixel, NULL);
680         hbmTempMask = CreateBitmap (himlSrc->cx, himlSrc->cy, 1,
681                                       1, NULL);
682
683         /* copy (and stretch) destination to temporary bitmaps.(save) */
684         /* image */
685         SelectObject (hdcBmp, hbmTempImage);
686         StretchBlt   (hdcBmp, 0, 0, himlSrc->cx, himlSrc->cy,
687                       himlDst->hdcImage, ptDst.x, ptDst.y, himlDst->cx, himlDst->cy,
688                       SRCCOPY);
689         /* mask */
690         SelectObject (hdcBmp, hbmTempMask);
691         StretchBlt   (hdcBmp, 0, 0, himlSrc->cx, himlSrc->cy,
692                       himlDst->hdcMask, ptDst.x, ptDst.y, himlDst->cx, himlDst->cy,
693                       SRCCOPY);
694
695         /* copy (and stretch) source to destination */
696         /* image */
697         StretchBlt   (himlDst->hdcImage, ptDst.x, ptDst.y, himlDst->cx, himlDst->cy,
698                       himlSrc->hdcImage, ptSrc.x, ptSrc.y, himlSrc->cx, himlSrc->cy,
699                       SRCCOPY);
700         /* mask */
701         StretchBlt   (himlDst->hdcMask, ptDst.x, ptDst.y, himlDst->cx, himlDst->cy,
702                       himlSrc->hdcMask, ptSrc.x, ptSrc.y, himlSrc->cx, himlSrc->cy,
703                       SRCCOPY);
704
705         /* copy (without stretching) temporary bitmaps to source (restore) */
706         /* mask */
707         BitBlt       (himlSrc->hdcMask, ptSrc.x, ptSrc.y, himlSrc->cx, himlSrc->cy,
708                       hdcBmp, 0, 0, SRCCOPY);
709
710         /* image */
711         BitBlt       (himlSrc->hdcImage, ptSrc.x, ptSrc.y, himlSrc->cx, himlSrc->cy,
712                       hdcBmp, 0, 0, SRCCOPY);
713         /* delete temporary bitmaps */
714         DeleteObject (hbmTempMask);
715         DeleteObject (hbmTempImage);
716         DeleteDC(hdcBmp);
717     }
718     else {
719         /* copy image */
720         StretchBlt   (himlDst->hdcImage, ptDst.x, ptDst.y, himlDst->cx, himlDst->cy,
721                       himlSrc->hdcImage, ptSrc.x, ptSrc.y, himlSrc->cx, himlSrc->cy,
722                       SRCCOPY);
723
724         /* copy mask */
725         StretchBlt   (himlDst->hdcMask, ptDst.x, ptDst.y, himlDst->cx, himlDst->cy,
726                       himlSrc->hdcMask, ptSrc.x, ptSrc.y, himlSrc->cx, himlSrc->cy,
727                       SRCCOPY);
728     }
729
730     return TRUE;
731 }
732
733
734 /*************************************************************************
735  * ImageList_Create [COMCTL32.@]
736  *
737  * Creates a new image list.
738  *
739  * PARAMS
740  *     cx       [I] image height
741  *     cy       [I] image width
742  *     flags    [I] creation flags
743  *     cInitial [I] initial number of images in the image list
744  *     cGrow    [I] number of images by which image list grows
745  *
746  * RETURNS
747  *     Success: Handle to the created image list
748  *     Failure: NULL
749  */
750 HIMAGELIST WINAPI
751 ImageList_Create (INT cx, INT cy, UINT flags,
752                   INT cInitial, INT cGrow)
753 {
754     HIMAGELIST himl;
755     INT      nCount;
756     HBITMAP  hbmTemp;
757     UINT     ilc = (flags & 0xFE);
758     static const WORD aBitBlend25[] =
759         {0xAA, 0x00, 0x55, 0x00, 0xAA, 0x00, 0x55, 0x00};
760
761     static const WORD aBitBlend50[] =
762         {0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA};
763
764     TRACE("(%d %d 0x%x %d %d)\n", cx, cy, flags, cInitial, cGrow);
765
766     if (cx <= 0 || cy <= 0) return NULL;
767
768     /* Create the IImageList interface for the image list */
769     if (FAILED(ImageListImpl_CreateInstance(NULL, &IID_IImageList, (void **)&himl)))
770         return NULL;
771
772     cGrow = (cGrow < 4) ? 4 : (cGrow + 3) & ~3;
773
774     himl->cx        = cx;
775     himl->cy        = cy;
776     himl->flags     = flags;
777     himl->cMaxImage = cInitial + 1;
778     himl->cInitial  = cInitial;
779     himl->cGrow     = cGrow;
780     himl->clrFg     = CLR_DEFAULT;
781     himl->clrBk     = CLR_NONE;
782
783     /* initialize overlay mask indices */
784     for (nCount = 0; nCount < MAX_OVERLAYIMAGE; nCount++)
785         himl->nOvlIdx[nCount] = -1;
786
787     /* Create Image & Mask DCs */
788     himl->hdcImage = CreateCompatibleDC (0);
789     if (!himl->hdcImage)
790         goto cleanup;
791     if (himl->flags & ILC_MASK){
792         himl->hdcMask = CreateCompatibleDC(0);
793         if (!himl->hdcMask)
794             goto cleanup;
795     }
796
797     /* Default to ILC_COLOR4 if none of the ILC_COLOR* flags are specified */
798     if (ilc == ILC_COLOR)
799         ilc = ILC_COLOR4;
800
801     if (ilc >= ILC_COLOR4 && ilc <= ILC_COLOR32)
802         himl->uBitsPixel = ilc;
803     else
804         himl->uBitsPixel = (UINT)GetDeviceCaps (himl->hdcImage, BITSPIXEL);
805
806     if (himl->cMaxImage > 0) {
807         himl->hbmImage = ImageList_CreateImage(himl->hdcImage, himl, himl->cMaxImage);
808         SelectObject(himl->hdcImage, himl->hbmImage);
809     } else
810         himl->hbmImage = 0;
811
812     if ((himl->cMaxImage > 0) && (himl->flags & ILC_MASK)) {
813         SIZE sz;
814
815         imagelist_get_bitmap_size(himl, himl->cMaxImage, &sz);
816         himl->hbmMask = CreateBitmap (sz.cx, sz.cy, 1, 1, NULL);
817         if (himl->hbmMask == 0) {
818             ERR("Error creating mask bitmap!\n");
819             goto cleanup;
820         }
821         SelectObject(himl->hdcMask, himl->hbmMask);
822     }
823     else
824         himl->hbmMask = 0;
825
826     if (ilc == ILC_COLOR32)
827         himl->has_alpha = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, himl->cMaxImage );
828     else
829         himl->has_alpha = NULL;
830
831     /* create blending brushes */
832     hbmTemp = CreateBitmap (8, 8, 1, 1, aBitBlend25);
833     himl->hbrBlend25 = CreatePatternBrush (hbmTemp);
834     DeleteObject (hbmTemp);
835
836     hbmTemp = CreateBitmap (8, 8, 1, 1, aBitBlend50);
837     himl->hbrBlend50 = CreatePatternBrush (hbmTemp);
838     DeleteObject (hbmTemp);
839
840     TRACE("created imagelist %p\n", himl);
841     return himl;
842
843 cleanup:
844     ImageList_Destroy(himl);
845     return NULL;
846 }
847
848
849 /*************************************************************************
850  * ImageList_Destroy [COMCTL32.@]
851  *
852  * Destroys an image list.
853  *
854  * PARAMS
855  *     himl [I] handle to image list
856  *
857  * RETURNS
858  *     Success: TRUE
859  *     Failure: FALSE
860  */
861
862 BOOL WINAPI
863 ImageList_Destroy (HIMAGELIST himl)
864 {
865     if (!is_valid(himl))
866         return FALSE;
867
868     IImageList_Release((IImageList *) himl);
869     return TRUE;
870 }
871
872
873 /*************************************************************************
874  * ImageList_DragEnter [COMCTL32.@]
875  *
876  * Locks window update and displays the drag image at the given position.
877  *
878  * PARAMS
879  *     hwndLock [I] handle of the window that owns the drag image.
880  *     x        [I] X position of the drag image.
881  *     y        [I] Y position of the drag image.
882  *
883  * RETURNS
884  *     Success: TRUE
885  *     Failure: FALSE
886  *
887  * NOTES
888  *     The position of the drag image is relative to the window, not
889  *     the client area.
890  */
891
892 BOOL WINAPI
893 ImageList_DragEnter (HWND hwndLock, INT x, INT y)
894 {
895     TRACE("(hwnd=%p x=%d y=%d)\n", hwndLock, x, y);
896
897     if (!is_valid(InternalDrag.himl))
898         return FALSE;
899
900     if (hwndLock)
901         InternalDrag.hwnd = hwndLock;
902     else
903         InternalDrag.hwnd = GetDesktopWindow ();
904
905     InternalDrag.x = x;
906     InternalDrag.y = y;
907
908     /* draw the drag image and save the background */
909     if (!ImageList_DragShowNolock(TRUE)) {
910         return FALSE;
911     }
912
913     return TRUE;
914 }
915
916
917 /*************************************************************************
918  * ImageList_DragLeave [COMCTL32.@]
919  *
920  * Unlocks window update and hides the drag image.
921  *
922  * PARAMS
923  *     hwndLock [I] handle of the window that owns the drag image.
924  *
925  * RETURNS
926  *     Success: TRUE
927  *     Failure: FALSE
928  */
929
930 BOOL WINAPI
931 ImageList_DragLeave (HWND hwndLock)
932 {
933     /* As we don't save drag info in the window this can lead to problems if
934        an app does not supply the same window as DragEnter */
935     /* if (hwndLock)
936         InternalDrag.hwnd = hwndLock;
937     else
938         InternalDrag.hwnd = GetDesktopWindow (); */
939     if(!hwndLock)
940         hwndLock = GetDesktopWindow();
941     if(InternalDrag.hwnd != hwndLock)
942         FIXME("DragLeave hWnd != DragEnter hWnd\n");
943
944     ImageList_DragShowNolock (FALSE);
945
946     return TRUE;
947 }
948
949
950 /*************************************************************************
951  * ImageList_InternalDragDraw [Internal]
952  *
953  * Draws the drag image.
954  *
955  * PARAMS
956  *     hdc [I] device context to draw into.
957  *     x   [I] X position of the drag image.
958  *     y   [I] Y position of the drag image.
959  *
960  * RETURNS
961  *     Success: TRUE
962  *     Failure: FALSE
963  *
964  * NOTES
965  *     The position of the drag image is relative to the window, not
966  *     the client area.
967  *
968  */
969
970 static inline void
971 ImageList_InternalDragDraw (HDC hdc, INT x, INT y)
972 {
973     IMAGELISTDRAWPARAMS imldp;
974
975     ZeroMemory (&imldp, sizeof(imldp));
976     imldp.cbSize  = sizeof(imldp);
977     imldp.himl    = InternalDrag.himl;
978     imldp.i       = 0;
979     imldp.hdcDst  = hdc,
980     imldp.x       = x;
981     imldp.y       = y;
982     imldp.rgbBk   = CLR_DEFAULT;
983     imldp.rgbFg   = CLR_DEFAULT;
984     imldp.fStyle  = ILD_NORMAL;
985     imldp.fState  = ILS_ALPHA;
986     imldp.Frame   = 192;
987     ImageList_DrawIndirect (&imldp);
988 }
989
990 /*************************************************************************
991  * ImageList_DragMove [COMCTL32.@]
992  *
993  * Moves the drag image.
994  *
995  * PARAMS
996  *     x [I] X position of the drag image.
997  *     y [I] Y position of the drag image.
998  *
999  * RETURNS
1000  *     Success: TRUE
1001  *     Failure: FALSE
1002  *
1003  * NOTES
1004  *     The position of the drag image is relative to the window, not
1005  *     the client area.
1006  */
1007
1008 BOOL WINAPI
1009 ImageList_DragMove (INT x, INT y)
1010 {
1011     TRACE("(x=%d y=%d)\n", x, y);
1012
1013     if (!is_valid(InternalDrag.himl))
1014         return FALSE;
1015
1016     /* draw/update the drag image */
1017     if (InternalDrag.bShow) {
1018         HDC hdcDrag;
1019         HDC hdcOffScreen;
1020         HDC hdcBg;
1021         HBITMAP hbmOffScreen;
1022         INT origNewX, origNewY;
1023         INT origOldX, origOldY;
1024         INT origRegX, origRegY;
1025         INT sizeRegX, sizeRegY;
1026
1027
1028         /* calculate the update region */
1029         origNewX = x - InternalDrag.dxHotspot;
1030         origNewY = y - InternalDrag.dyHotspot;
1031         origOldX = InternalDrag.x - InternalDrag.dxHotspot;
1032         origOldY = InternalDrag.y - InternalDrag.dyHotspot;
1033         origRegX = min(origNewX, origOldX);
1034         origRegY = min(origNewY, origOldY);
1035         sizeRegX = InternalDrag.himl->cx + abs(x - InternalDrag.x);
1036         sizeRegY = InternalDrag.himl->cy + abs(y - InternalDrag.y);
1037
1038         hdcDrag = GetDCEx(InternalDrag.hwnd, 0,
1039                           DCX_WINDOW | DCX_CACHE | DCX_LOCKWINDOWUPDATE);
1040         hdcOffScreen = CreateCompatibleDC(hdcDrag);
1041         hdcBg = CreateCompatibleDC(hdcDrag);
1042
1043         hbmOffScreen = CreateCompatibleBitmap(hdcDrag, sizeRegX, sizeRegY);
1044         SelectObject(hdcOffScreen, hbmOffScreen);
1045         SelectObject(hdcBg, InternalDrag.hbmBg);
1046
1047         /* get the actual background of the update region */
1048         BitBlt(hdcOffScreen, 0, 0, sizeRegX, sizeRegY, hdcDrag,
1049                origRegX, origRegY, SRCCOPY);
1050         /* erase the old image */
1051         BitBlt(hdcOffScreen, origOldX - origRegX, origOldY - origRegY,
1052                InternalDrag.himl->cx, InternalDrag.himl->cy, hdcBg, 0, 0,
1053                SRCCOPY);
1054         /* save the background */
1055         BitBlt(hdcBg, 0, 0, InternalDrag.himl->cx, InternalDrag.himl->cy,
1056                hdcOffScreen, origNewX - origRegX, origNewY - origRegY, SRCCOPY);
1057         /* draw the image */
1058         ImageList_InternalDragDraw(hdcOffScreen, origNewX - origRegX, 
1059                                    origNewY - origRegY);
1060         /* draw the update region to the screen */
1061         BitBlt(hdcDrag, origRegX, origRegY, sizeRegX, sizeRegY,
1062                hdcOffScreen, 0, 0, SRCCOPY);
1063
1064         DeleteDC(hdcBg);
1065         DeleteDC(hdcOffScreen);
1066         DeleteObject(hbmOffScreen);
1067         ReleaseDC(InternalDrag.hwnd, hdcDrag);
1068     }
1069
1070     /* update the image position */
1071     InternalDrag.x = x;
1072     InternalDrag.y = y;
1073
1074     return TRUE;
1075 }
1076
1077
1078 /*************************************************************************
1079  * ImageList_DragShowNolock [COMCTL32.@]
1080  *
1081  * Shows or hides the drag image.
1082  *
1083  * PARAMS
1084  *     bShow [I] TRUE shows the drag image, FALSE hides it.
1085  *
1086  * RETURNS
1087  *     Success: TRUE
1088  *     Failure: FALSE
1089  */
1090
1091 BOOL WINAPI
1092 ImageList_DragShowNolock (BOOL bShow)
1093 {
1094     HDC hdcDrag;
1095     HDC hdcBg;
1096     INT x, y;
1097
1098     if (!is_valid(InternalDrag.himl))
1099         return FALSE;
1100     
1101     TRACE("bShow=0x%X!\n", bShow);
1102
1103     /* DragImage is already visible/hidden */
1104     if ((InternalDrag.bShow && bShow) || (!InternalDrag.bShow && !bShow)) {
1105         return FALSE;
1106     }
1107
1108     /* position of the origin of the DragImage */
1109     x = InternalDrag.x - InternalDrag.dxHotspot;
1110     y = InternalDrag.y - InternalDrag.dyHotspot;
1111
1112     hdcDrag = GetDCEx (InternalDrag.hwnd, 0,
1113                          DCX_WINDOW | DCX_CACHE | DCX_LOCKWINDOWUPDATE);
1114     if (!hdcDrag) {
1115         return FALSE;
1116     }
1117
1118     hdcBg = CreateCompatibleDC(hdcDrag);
1119     if (!InternalDrag.hbmBg) {
1120         InternalDrag.hbmBg = CreateCompatibleBitmap(hdcDrag,
1121                     InternalDrag.himl->cx, InternalDrag.himl->cy);
1122     }
1123     SelectObject(hdcBg, InternalDrag.hbmBg);
1124
1125     if (bShow) {
1126         /* save the background */
1127         BitBlt(hdcBg, 0, 0, InternalDrag.himl->cx, InternalDrag.himl->cy,
1128                hdcDrag, x, y, SRCCOPY);
1129         /* show the image */
1130         ImageList_InternalDragDraw(hdcDrag, x, y);
1131     } else {
1132         /* hide the image */
1133         BitBlt(hdcDrag, x, y, InternalDrag.himl->cx, InternalDrag.himl->cy,
1134                hdcBg, 0, 0, SRCCOPY);
1135     }
1136
1137     InternalDrag.bShow = !InternalDrag.bShow;
1138
1139     DeleteDC(hdcBg);
1140     ReleaseDC (InternalDrag.hwnd, hdcDrag);
1141     return TRUE;
1142 }
1143
1144
1145 /*************************************************************************
1146  * ImageList_Draw [COMCTL32.@]
1147  *
1148  * Draws an image.
1149  *
1150  * PARAMS
1151  *     himl   [I] handle to image list
1152  *     i      [I] image index
1153  *     hdc    [I] handle to device context
1154  *     x      [I] x position
1155  *     y      [I] y position
1156  *     fStyle [I] drawing flags
1157  *
1158  * RETURNS
1159  *     Success: TRUE
1160  *     Failure: FALSE
1161  *
1162  * SEE
1163  *     ImageList_DrawEx.
1164  */
1165
1166 BOOL WINAPI
1167 ImageList_Draw (HIMAGELIST himl, INT i, HDC hdc, INT x, INT y, UINT fStyle)
1168 {
1169     return ImageList_DrawEx (himl, i, hdc, x, y, 0, 0, 
1170                              CLR_DEFAULT, CLR_DEFAULT, fStyle);
1171 }
1172
1173
1174 /*************************************************************************
1175  * ImageList_DrawEx [COMCTL32.@]
1176  *
1177  * Draws an image and allows to use extended drawing features.
1178  *
1179  * PARAMS
1180  *     himl   [I] handle to image list
1181  *     i      [I] image index
1182  *     hdc    [I] handle to device context
1183  *     x      [I] X position
1184  *     y      [I] Y position
1185  *     dx     [I] X offset
1186  *     dy     [I] Y offset
1187  *     rgbBk  [I] background color
1188  *     rgbFg  [I] foreground color
1189  *     fStyle [I] drawing flags
1190  *
1191  * RETURNS
1192  *     Success: TRUE
1193  *     Failure: FALSE
1194  *
1195  * NOTES
1196  *     Calls ImageList_DrawIndirect.
1197  *
1198  * SEE
1199  *     ImageList_DrawIndirect.
1200  */
1201
1202 BOOL WINAPI
1203 ImageList_DrawEx (HIMAGELIST himl, INT i, HDC hdc, INT x, INT y,
1204                   INT dx, INT dy, COLORREF rgbBk, COLORREF rgbFg,
1205                   UINT fStyle)
1206 {
1207     IMAGELISTDRAWPARAMS imldp;
1208
1209     ZeroMemory (&imldp, sizeof(imldp));
1210     imldp.cbSize  = sizeof(imldp);
1211     imldp.himl    = himl;
1212     imldp.i       = i;
1213     imldp.hdcDst  = hdc,
1214     imldp.x       = x;
1215     imldp.y       = y;
1216     imldp.cx      = dx;
1217     imldp.cy      = dy;
1218     imldp.rgbBk   = rgbBk;
1219     imldp.rgbFg   = rgbFg;
1220     imldp.fStyle  = fStyle;
1221
1222     return ImageList_DrawIndirect (&imldp);
1223 }
1224
1225
1226 static BOOL alpha_blend_image( HIMAGELIST himl, HDC dest_dc, int dest_x, int dest_y,
1227                                int src_x, int src_y, int cx, int cy, BLENDFUNCTION func,
1228                                UINT style, COLORREF blend_col )
1229 {
1230     BOOL ret = FALSE;
1231     HDC hdc;
1232     HBITMAP bmp = 0, mask = 0;
1233     BITMAPINFO *info;
1234     void *bits, *mask_bits;
1235     unsigned int *ptr;
1236     int i, j;
1237
1238     if (!(hdc = CreateCompatibleDC( 0 ))) return FALSE;
1239     if (!(info = HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET( BITMAPINFO, bmiColors[256] )))) goto done;
1240     info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1241     info->bmiHeader.biWidth = cx;
1242     info->bmiHeader.biHeight = cy;
1243     info->bmiHeader.biPlanes = 1;
1244     info->bmiHeader.biBitCount = 32;
1245     info->bmiHeader.biCompression = BI_RGB;
1246     info->bmiHeader.biSizeImage = cx * cy * 4;
1247     info->bmiHeader.biXPelsPerMeter = 0;
1248     info->bmiHeader.biYPelsPerMeter = 0;
1249     info->bmiHeader.biClrUsed = 0;
1250     info->bmiHeader.biClrImportant = 0;
1251     if (!(bmp = CreateDIBSection( himl->hdcImage, info, DIB_RGB_COLORS, &bits, 0, 0 ))) goto done;
1252     SelectObject( hdc, bmp );
1253     BitBlt( hdc, 0, 0, cx, cy, himl->hdcImage, src_x, src_y, SRCCOPY );
1254
1255     if (blend_col != CLR_NONE)
1256     {
1257         BYTE r = GetRValue( blend_col );
1258         BYTE g = GetGValue( blend_col );
1259         BYTE b = GetBValue( blend_col );
1260
1261         if (style & ILD_BLEND25)
1262         {
1263             for (i = 0, ptr = bits; i < cx * cy; i++, ptr++)
1264                 *ptr = ((*ptr & 0xff000000) |
1265                         ((((*ptr & 0x00ff0000) * 3 + (r << 16)) / 4) & 0x00ff0000) |
1266                         ((((*ptr & 0x0000ff00) * 3 + (g << 8))  / 4) & 0x0000ff00) |
1267                         ((((*ptr & 0x000000ff) * 3 + (b << 0))  / 4) & 0x000000ff));
1268         }
1269         else if (style & ILD_BLEND50)
1270         {
1271             for (i = 0, ptr = bits; i < cx * cy; i++, ptr++)
1272                 *ptr = ((*ptr & 0xff000000) |
1273                         ((((*ptr & 0x00ff0000) + (r << 16)) / 2) & 0x00ff0000) |
1274                         ((((*ptr & 0x0000ff00) + (g << 8))  / 2) & 0x0000ff00) |
1275                         ((((*ptr & 0x000000ff) + (b << 0))  / 2) & 0x000000ff));
1276         }
1277     }
1278
1279     if (himl->has_alpha)  /* we already have an alpha channel in this case */
1280     {
1281         /* pre-multiply by the alpha channel */
1282         for (i = 0, ptr = bits; i < cx * cy; i++, ptr++)
1283         {
1284             DWORD alpha = *ptr >> 24;
1285             *ptr = ((*ptr & 0xff000000) |
1286                     (((*ptr & 0x00ff0000) * alpha / 255) & 0x00ff0000) |
1287                     (((*ptr & 0x0000ff00) * alpha / 255) & 0x0000ff00) |
1288                     (((*ptr & 0x000000ff) * alpha / 255)));
1289         }
1290     }
1291     else if (himl->hbmMask)
1292     {
1293         unsigned int width_bytes = (cx + 31) / 32 * 4;
1294         /* generate alpha channel from the mask */
1295         info->bmiHeader.biBitCount = 1;
1296         info->bmiHeader.biSizeImage = width_bytes * cy;
1297         info->bmiColors[0].rgbRed      = 0;
1298         info->bmiColors[0].rgbGreen    = 0;
1299         info->bmiColors[0].rgbBlue     = 0;
1300         info->bmiColors[0].rgbReserved = 0;
1301         info->bmiColors[1].rgbRed      = 0xff;
1302         info->bmiColors[1].rgbGreen    = 0xff;
1303         info->bmiColors[1].rgbBlue     = 0xff;
1304         info->bmiColors[1].rgbReserved = 0;
1305         if (!(mask = CreateDIBSection( himl->hdcMask, info, DIB_RGB_COLORS, &mask_bits, 0, 0 )))
1306             goto done;
1307         SelectObject( hdc, mask );
1308         BitBlt( hdc, 0, 0, cx, cy, himl->hdcMask, src_x, src_y, SRCCOPY );
1309         SelectObject( hdc, bmp );
1310         for (i = 0, ptr = bits; i < cy; i++)
1311             for (j = 0; j < cx; j++, ptr++)
1312                 if ((((BYTE *)mask_bits)[i * width_bytes + j / 8] << (j % 8)) & 0x80) *ptr = 0;
1313                 else *ptr |= 0xff000000;
1314     }
1315
1316     ret = GdiAlphaBlend( dest_dc, dest_x, dest_y, cx, cy, hdc, 0, 0, cx, cy, func );
1317
1318 done:
1319     DeleteDC( hdc );
1320     if (bmp) DeleteObject( bmp );
1321     if (mask) DeleteObject( mask );
1322     HeapFree( GetProcessHeap(), 0, info );
1323     return ret;
1324 }
1325
1326 /*************************************************************************
1327  * ImageList_DrawIndirect [COMCTL32.@]
1328  *
1329  * Draws an image using various parameters specified in pimldp.
1330  *
1331  * PARAMS
1332  *     pimldp [I] pointer to IMAGELISTDRAWPARAMS structure.
1333  *
1334  * RETURNS
1335  *     Success: TRUE
1336  *     Failure: FALSE
1337  */
1338
1339 BOOL WINAPI
1340 ImageList_DrawIndirect (IMAGELISTDRAWPARAMS *pimldp)
1341 {
1342     INT cx, cy, nOvlIdx;
1343     DWORD fState, dwRop;
1344     UINT fStyle;
1345     COLORREF oldImageBk, oldImageFg;
1346     HDC hImageDC, hImageListDC, hMaskListDC;
1347     HBITMAP hImageBmp, hOldImageBmp, hBlendMaskBmp;
1348     BOOL bIsTransparent, bBlend, bResult = FALSE, bMask;
1349     HIMAGELIST himl;
1350     HBRUSH hOldBrush;
1351     POINT pt;
1352     BOOL has_alpha;
1353
1354     if (!pimldp || !(himl = pimldp->himl)) return FALSE;
1355     if (!is_valid(himl)) return FALSE;
1356     if ((pimldp->i < 0) || (pimldp->i >= himl->cCurImage)) return FALSE;
1357
1358     imagelist_point_from_index( himl, pimldp->i, &pt );
1359     pt.x += pimldp->xBitmap;
1360     pt.y += pimldp->yBitmap;
1361
1362     fState = pimldp->cbSize < sizeof(IMAGELISTDRAWPARAMS) ? ILS_NORMAL : pimldp->fState;
1363     fStyle = pimldp->fStyle & ~ILD_OVERLAYMASK;
1364     cx = (pimldp->cx == 0) ? himl->cx : pimldp->cx;
1365     cy = (pimldp->cy == 0) ? himl->cy : pimldp->cy;
1366
1367     bIsTransparent = (fStyle & ILD_TRANSPARENT);
1368     if( pimldp->rgbBk == CLR_NONE )
1369         bIsTransparent = TRUE;
1370     if( ( pimldp->rgbBk == CLR_DEFAULT ) && ( himl->clrBk == CLR_NONE ) )
1371         bIsTransparent = TRUE;
1372     bMask = (himl->flags & ILC_MASK) && (fStyle & ILD_MASK) ;
1373     bBlend = (fStyle & (ILD_BLEND25 | ILD_BLEND50) ) && !bMask;
1374
1375     TRACE("himl(%p) hbmMask(%p) iImage(%d) x(%d) y(%d) cx(%d) cy(%d)\n",
1376           himl, himl->hbmMask, pimldp->i, pimldp->x, pimldp->y, cx, cy);
1377
1378     /* we will use these DCs to access the images and masks in the ImageList */
1379     hImageListDC = himl->hdcImage;
1380     hMaskListDC  = himl->hdcMask;
1381
1382     /* these will accumulate the image and mask for the image we're drawing */
1383     hImageDC = CreateCompatibleDC( pimldp->hdcDst );
1384     hImageBmp = CreateCompatibleBitmap( pimldp->hdcDst, cx, cy );
1385     hBlendMaskBmp = bBlend ? CreateBitmap(cx, cy, 1, 1, NULL) : 0;
1386
1387     /* Create a compatible DC. */
1388     if (!hImageListDC || !hImageDC || !hImageBmp ||
1389         (bBlend && !hBlendMaskBmp) || (himl->hbmMask && !hMaskListDC))
1390         goto cleanup;
1391     
1392     hOldImageBmp = SelectObject(hImageDC, hImageBmp);
1393   
1394     /*
1395      * To obtain a transparent look, background color should be set
1396      * to white and foreground color to black when blitting the
1397      * monochrome mask.
1398      */
1399     oldImageFg = SetTextColor( hImageDC, RGB( 0, 0, 0 ) );
1400     oldImageBk = SetBkColor( hImageDC, RGB( 0xff, 0xff, 0xff ) );
1401
1402     has_alpha = (himl->has_alpha && himl->has_alpha[pimldp->i]);
1403     if (!bMask && (has_alpha || (fState & ILS_ALPHA)))
1404     {
1405         COLORREF colour, blend_col = CLR_NONE;
1406         BLENDFUNCTION func;
1407
1408         if (bBlend)
1409         {
1410             blend_col = pimldp->rgbFg;
1411             if (blend_col == CLR_DEFAULT) blend_col = GetSysColor( COLOR_HIGHLIGHT );
1412             else if (blend_col == CLR_NONE) blend_col = GetTextColor( pimldp->hdcDst );
1413         }
1414
1415         func.BlendOp = AC_SRC_OVER;
1416         func.BlendFlags = 0;
1417         func.SourceConstantAlpha = (fState & ILS_ALPHA) ? pimldp->Frame : 255;
1418         func.AlphaFormat = AC_SRC_ALPHA;
1419
1420         if (bIsTransparent)
1421         {
1422             bResult = alpha_blend_image( himl, pimldp->hdcDst, pimldp->x, pimldp->y,
1423                                          pt.x, pt.y, cx, cy, func, fStyle, blend_col );
1424             goto end;
1425         }
1426         colour = pimldp->rgbBk;
1427         if (colour == CLR_DEFAULT) colour = himl->clrBk;
1428         if (colour == CLR_NONE) colour = GetBkColor( pimldp->hdcDst );
1429
1430         hOldBrush = SelectObject (hImageDC, CreateSolidBrush (colour));
1431         PatBlt( hImageDC, 0, 0, cx, cy, PATCOPY );
1432         alpha_blend_image( himl, hImageDC, 0, 0, pt.x, pt.y, cx, cy, func, fStyle, blend_col );
1433         DeleteObject (SelectObject (hImageDC, hOldBrush));
1434         bResult = BitBlt( pimldp->hdcDst, pimldp->x,  pimldp->y, cx, cy, hImageDC, 0, 0, SRCCOPY );
1435         goto end;
1436     }
1437
1438     /*
1439      * Draw the initial image
1440      */
1441     if( bMask ) {
1442         if (himl->hbmMask) {
1443             hOldBrush = SelectObject (hImageDC, CreateSolidBrush (GetTextColor(pimldp->hdcDst)));
1444             PatBlt( hImageDC, 0, 0, cx, cy, PATCOPY );
1445             BitBlt(hImageDC, 0, 0, cx, cy, hMaskListDC, pt.x, pt.y, SRCPAINT);
1446             DeleteObject (SelectObject (hImageDC, hOldBrush));
1447             if( bIsTransparent )
1448             {
1449                 BitBlt ( pimldp->hdcDst, pimldp->x,  pimldp->y, cx, cy, hImageDC, 0, 0, SRCAND);
1450                 bResult = TRUE;
1451                 goto end;
1452             }
1453         } else {
1454             hOldBrush = SelectObject (hImageDC, GetStockObject(BLACK_BRUSH));
1455             PatBlt( hImageDC, 0, 0, cx, cy, PATCOPY);
1456             SelectObject(hImageDC, hOldBrush);
1457         }
1458     } else {
1459         /* blend the image with the needed solid background */
1460         COLORREF colour = RGB(0,0,0);
1461
1462         if( !bIsTransparent )
1463         {
1464             colour = pimldp->rgbBk;
1465             if( colour == CLR_DEFAULT )
1466                 colour = himl->clrBk;
1467             if( colour == CLR_NONE )
1468                 colour = GetBkColor(pimldp->hdcDst);
1469         }
1470
1471         hOldBrush = SelectObject (hImageDC, CreateSolidBrush (colour));
1472         PatBlt( hImageDC, 0, 0, cx, cy, PATCOPY );
1473         if (himl->hbmMask)
1474         {
1475             BitBlt( hImageDC, 0, 0, cx, cy, hMaskListDC, pt.x, pt.y, SRCAND );
1476             BitBlt( hImageDC, 0, 0, cx, cy, hImageListDC, pt.x, pt.y, SRCPAINT );
1477         }
1478         else
1479             BitBlt( hImageDC, 0, 0, cx, cy, hImageListDC, pt.x, pt.y, SRCCOPY);
1480         DeleteObject (SelectObject (hImageDC, hOldBrush));
1481     }
1482
1483     /* Time for blending, if required */
1484     if (bBlend) {
1485         HBRUSH hBlendBrush;
1486         COLORREF clrBlend = pimldp->rgbFg;
1487         HDC hBlendMaskDC = hImageListDC;
1488         HBITMAP hOldBitmap;
1489
1490         /* Create the blend Mask */
1491         hOldBitmap = SelectObject(hBlendMaskDC, hBlendMaskBmp);
1492         hBlendBrush = fStyle & ILD_BLEND50 ? himl->hbrBlend50 : himl->hbrBlend25;
1493         hOldBrush = SelectObject(hBlendMaskDC, hBlendBrush);
1494         PatBlt(hBlendMaskDC, 0, 0, cx, cy, PATCOPY);
1495         SelectObject(hBlendMaskDC, hOldBrush);
1496
1497         /* Modify the blend mask if an Image Mask exist */
1498         if(himl->hbmMask) {
1499             BitBlt(hBlendMaskDC, 0, 0, cx, cy, hMaskListDC, pt.x, pt.y, 0x220326); /* NOTSRCAND */
1500             BitBlt(hBlendMaskDC, 0, 0, cx, cy, hBlendMaskDC, 0, 0, NOTSRCCOPY);
1501         }
1502
1503         /* now apply blend to the current image given the BlendMask */
1504         if (clrBlend == CLR_DEFAULT) clrBlend = GetSysColor (COLOR_HIGHLIGHT);
1505         else if (clrBlend == CLR_NONE) clrBlend = GetTextColor (pimldp->hdcDst);
1506         hOldBrush = SelectObject (hImageDC, CreateSolidBrush(clrBlend));
1507         BitBlt (hImageDC, 0, 0, cx, cy, hBlendMaskDC, 0, 0, 0xB8074A); /* PSDPxax */
1508         DeleteObject(SelectObject(hImageDC, hOldBrush));
1509         SelectObject(hBlendMaskDC, hOldBitmap);
1510     }
1511
1512     /* Now do the overlay image, if any */
1513     nOvlIdx = (pimldp->fStyle & ILD_OVERLAYMASK) >> 8;
1514     if ( (nOvlIdx >= 1) && (nOvlIdx <= MAX_OVERLAYIMAGE)) {
1515         nOvlIdx = himl->nOvlIdx[nOvlIdx - 1];
1516         if ((nOvlIdx >= 0) && (nOvlIdx < himl->cCurImage)) {
1517             POINT ptOvl;
1518             imagelist_point_from_index( himl, nOvlIdx, &ptOvl );
1519             ptOvl.x += pimldp->xBitmap;
1520             if (himl->hbmMask && !(fStyle & ILD_IMAGE))
1521                 BitBlt (hImageDC, 0, 0, cx, cy, hMaskListDC, ptOvl.x, ptOvl.y, SRCAND);
1522             BitBlt (hImageDC, 0, 0, cx, cy, hImageListDC, ptOvl.x, ptOvl.y, SRCPAINT);
1523         }
1524     }
1525
1526     if (fState & ILS_SATURATE) FIXME("ILS_SATURATE: unimplemented!\n");
1527     if (fState & ILS_GLOW) FIXME("ILS_GLOW: unimplemented!\n");
1528     if (fState & ILS_SHADOW) FIXME("ILS_SHADOW: unimplemented!\n");
1529
1530     if (fStyle & ILD_PRESERVEALPHA) FIXME("ILD_PRESERVEALPHA: unimplemented!\n");
1531     if (fStyle & ILD_SCALE) FIXME("ILD_SCALE: unimplemented!\n");
1532     if (fStyle & ILD_DPISCALE) FIXME("ILD_DPISCALE: unimplemented!\n");
1533
1534     /* now copy the image to the screen */
1535     dwRop = SRCCOPY;
1536     if (himl->hbmMask && bIsTransparent ) {
1537         COLORREF oldDstFg = SetTextColor(pimldp->hdcDst, RGB( 0, 0, 0 ) );
1538         COLORREF oldDstBk = SetBkColor(pimldp->hdcDst, RGB( 0xff, 0xff, 0xff ));
1539         BitBlt (pimldp->hdcDst, pimldp->x,  pimldp->y, cx, cy, hMaskListDC, pt.x, pt.y, SRCAND);
1540         SetBkColor(pimldp->hdcDst, oldDstBk);
1541         SetTextColor(pimldp->hdcDst, oldDstFg);
1542         dwRop = SRCPAINT;
1543     }
1544     if (fStyle & ILD_ROP) dwRop = pimldp->dwRop;
1545     BitBlt (pimldp->hdcDst, pimldp->x,  pimldp->y, cx, cy, hImageDC, 0, 0, dwRop);
1546
1547     bResult = TRUE;
1548 end:
1549     /* cleanup the mess */
1550     SetBkColor(hImageDC, oldImageBk);
1551     SetTextColor(hImageDC, oldImageFg);
1552     SelectObject(hImageDC, hOldImageBmp);
1553 cleanup:
1554     DeleteObject(hBlendMaskBmp);
1555     DeleteObject(hImageBmp);
1556     DeleteDC(hImageDC);
1557
1558     return bResult;
1559 }
1560
1561
1562 /*************************************************************************
1563  * ImageList_Duplicate [COMCTL32.@]
1564  *
1565  * Duplicates an image list.
1566  *
1567  * PARAMS
1568  *     himlSrc [I] source image list handle
1569  *
1570  * RETURNS
1571  *     Success: Handle of duplicated image list.
1572  *     Failure: NULL
1573  */
1574
1575 HIMAGELIST WINAPI
1576 ImageList_Duplicate (HIMAGELIST himlSrc)
1577 {
1578     HIMAGELIST himlDst;
1579
1580     if (!is_valid(himlSrc)) {
1581         ERR("Invalid image list handle!\n");
1582         return NULL;
1583     }
1584
1585     himlDst = ImageList_Create (himlSrc->cx, himlSrc->cy, himlSrc->flags,
1586                                 himlSrc->cCurImage, himlSrc->cGrow);
1587
1588     if (himlDst)
1589     {
1590         SIZE sz;
1591
1592         imagelist_get_bitmap_size(himlSrc, himlSrc->cCurImage, &sz);
1593         BitBlt (himlDst->hdcImage, 0, 0, sz.cx, sz.cy,
1594                 himlSrc->hdcImage, 0, 0, SRCCOPY);
1595
1596         if (himlDst->hbmMask)
1597             BitBlt (himlDst->hdcMask, 0, 0, sz.cx, sz.cy,
1598                     himlSrc->hdcMask, 0, 0, SRCCOPY);
1599
1600         himlDst->cCurImage = himlSrc->cCurImage;
1601         if (himlSrc->has_alpha && himlDst->has_alpha)
1602             memcpy( himlDst->has_alpha, himlSrc->has_alpha, himlDst->cCurImage );
1603     }
1604     return himlDst;
1605 }
1606
1607
1608 /*************************************************************************
1609  * ImageList_EndDrag [COMCTL32.@]
1610  *
1611  * Finishes a drag operation.
1612  *
1613  * PARAMS
1614  *     no Parameters
1615  *
1616  * RETURNS
1617  *     Success: TRUE
1618  *     Failure: FALSE
1619  */
1620
1621 VOID WINAPI
1622 ImageList_EndDrag (void)
1623 {
1624     /* cleanup the InternalDrag struct */
1625     InternalDrag.hwnd = 0;
1626     ImageList_Destroy (InternalDrag.himl);
1627     InternalDrag.himl = 0;
1628     InternalDrag.x= 0;
1629     InternalDrag.y= 0;
1630     InternalDrag.dxHotspot = 0;
1631     InternalDrag.dyHotspot = 0;
1632     InternalDrag.bShow = FALSE;
1633     DeleteObject(InternalDrag.hbmBg);
1634     InternalDrag.hbmBg = 0;
1635 }
1636
1637
1638 /*************************************************************************
1639  * ImageList_GetBkColor [COMCTL32.@]
1640  *
1641  * Returns the background color of an image list.
1642  *
1643  * PARAMS
1644  *     himl [I] Image list handle.
1645  *
1646  * RETURNS
1647  *     Success: background color
1648  *     Failure: CLR_NONE
1649  */
1650
1651 COLORREF WINAPI
1652 ImageList_GetBkColor (HIMAGELIST himl)
1653 {
1654     return himl ? himl->clrBk : CLR_NONE;
1655 }
1656
1657
1658 /*************************************************************************
1659  * ImageList_GetDragImage [COMCTL32.@]
1660  *
1661  * Returns the handle to the internal drag image list.
1662  *
1663  * PARAMS
1664  *     ppt        [O] Pointer to the drag position. Can be NULL.
1665  *     pptHotspot [O] Pointer to the position of the hot spot. Can be NULL.
1666  *
1667  * RETURNS
1668  *     Success: Handle of the drag image list.
1669  *     Failure: NULL.
1670  */
1671
1672 HIMAGELIST WINAPI
1673 ImageList_GetDragImage (POINT *ppt, POINT *pptHotspot)
1674 {
1675     if (is_valid(InternalDrag.himl)) {
1676         if (ppt) {
1677             ppt->x = InternalDrag.x;
1678             ppt->y = InternalDrag.y;
1679         }
1680         if (pptHotspot) {
1681             pptHotspot->x = InternalDrag.dxHotspot;
1682             pptHotspot->y = InternalDrag.dyHotspot;
1683         }
1684         return (InternalDrag.himl);
1685     }
1686
1687     return NULL;
1688 }
1689
1690
1691 /*************************************************************************
1692  * ImageList_GetFlags [COMCTL32.@]
1693  *
1694  * Gets the flags of the specified image list.
1695  *
1696  * PARAMS
1697  *     himl [I] Handle to image list
1698  *
1699  * RETURNS
1700  *     Image list flags.
1701  *
1702  * BUGS
1703  *    Stub.
1704  */
1705
1706 DWORD WINAPI
1707 ImageList_GetFlags(HIMAGELIST himl)
1708 {
1709     TRACE("%p\n", himl);
1710
1711     return is_valid(himl) ? himl->flags : 0;
1712 }
1713
1714
1715 /*************************************************************************
1716  * ImageList_GetIcon [COMCTL32.@]
1717  *
1718  * Creates an icon from a masked image of an image list.
1719  *
1720  * PARAMS
1721  *     himl  [I] handle to image list
1722  *     i     [I] image index
1723  *     flags [I] drawing style flags
1724  *
1725  * RETURNS
1726  *     Success: icon handle
1727  *     Failure: NULL
1728  */
1729
1730 HICON WINAPI
1731 ImageList_GetIcon (HIMAGELIST himl, INT i, UINT fStyle)
1732 {
1733     ICONINFO ii;
1734     HICON hIcon;
1735     HBITMAP hOldDstBitmap;
1736     HDC hdcDst;
1737     POINT pt;
1738
1739     TRACE("%p %d %d\n", himl, i, fStyle);
1740     if (!is_valid(himl) || (i < 0) || (i >= himl->cCurImage)) return NULL;
1741
1742     ii.fIcon = TRUE;
1743     ii.xHotspot = 0;
1744     ii.yHotspot = 0;
1745
1746     /* create colour bitmap */
1747     hdcDst = GetDC(0);
1748     ii.hbmColor = CreateCompatibleBitmap(hdcDst, himl->cx, himl->cy);
1749     ReleaseDC(0, hdcDst);
1750
1751     hdcDst = CreateCompatibleDC(0);
1752
1753     imagelist_point_from_index( himl, i, &pt );
1754
1755     /* draw mask*/
1756     ii.hbmMask  = CreateBitmap (himl->cx, himl->cy, 1, 1, NULL);
1757     hOldDstBitmap = SelectObject (hdcDst, ii.hbmMask);
1758     if (himl->hbmMask) {
1759         BitBlt (hdcDst, 0, 0, himl->cx, himl->cy,
1760                 himl->hdcMask, pt.x, pt.y, SRCCOPY);
1761     }
1762     else
1763         PatBlt (hdcDst, 0, 0, himl->cx, himl->cy, BLACKNESS);
1764
1765     /* draw image*/
1766     SelectObject (hdcDst, ii.hbmColor);
1767     BitBlt (hdcDst, 0, 0, himl->cx, himl->cy,
1768             himl->hdcImage, pt.x, pt.y, SRCCOPY);
1769
1770     /*
1771      * CreateIconIndirect requires us to deselect the bitmaps from
1772      * the DCs before calling
1773      */
1774     SelectObject(hdcDst, hOldDstBitmap);
1775
1776     hIcon = CreateIconIndirect (&ii);
1777
1778     DeleteObject (ii.hbmMask);
1779     DeleteObject (ii.hbmColor);
1780     DeleteDC (hdcDst);
1781
1782     return hIcon;
1783 }
1784
1785
1786 /*************************************************************************
1787  * ImageList_GetIconSize [COMCTL32.@]
1788  *
1789  * Retrieves the size of an image in an image list.
1790  *
1791  * PARAMS
1792  *     himl [I] handle to image list
1793  *     cx   [O] pointer to the image width.
1794  *     cy   [O] pointer to the image height.
1795  *
1796  * RETURNS
1797  *     Success: TRUE
1798  *     Failure: FALSE
1799  *
1800  * NOTES
1801  *     All images in an image list have the same size.
1802  */
1803
1804 BOOL WINAPI
1805 ImageList_GetIconSize (HIMAGELIST himl, INT *cx, INT *cy)
1806 {
1807     if (!is_valid(himl) || !cx || !cy)
1808         return FALSE;
1809     if ((himl->cx <= 0) || (himl->cy <= 0))
1810         return FALSE;
1811
1812     *cx = himl->cx;
1813     *cy = himl->cy;
1814
1815     return TRUE;
1816 }
1817
1818
1819 /*************************************************************************
1820  * ImageList_GetImageCount [COMCTL32.@]
1821  *
1822  * Returns the number of images in an image list.
1823  *
1824  * PARAMS
1825  *     himl [I] handle to image list
1826  *
1827  * RETURNS
1828  *     Success: Number of images.
1829  *     Failure: 0
1830  */
1831
1832 INT WINAPI
1833 ImageList_GetImageCount (HIMAGELIST himl)
1834 {
1835     if (!is_valid(himl))
1836         return 0;
1837
1838     return himl->cCurImage;
1839 }
1840
1841
1842 /*************************************************************************
1843  * ImageList_GetImageInfo [COMCTL32.@]
1844  *
1845  * Returns information about an image in an image list.
1846  *
1847  * PARAMS
1848  *     himl       [I] handle to image list
1849  *     i          [I] image index
1850  *     pImageInfo [O] pointer to the image information
1851  *
1852  * RETURNS
1853  *     Success: TRUE
1854  *     Failure: FALSE
1855  */
1856
1857 BOOL WINAPI
1858 ImageList_GetImageInfo (HIMAGELIST himl, INT i, IMAGEINFO *pImageInfo)
1859 {
1860     POINT pt;
1861
1862     if (!is_valid(himl) || (pImageInfo == NULL))
1863         return FALSE;
1864     if ((i < 0) || (i >= himl->cCurImage))
1865         return FALSE;
1866
1867     pImageInfo->hbmImage = himl->hbmImage;
1868     pImageInfo->hbmMask  = himl->hbmMask;
1869
1870     imagelist_point_from_index( himl, i, &pt );
1871     pImageInfo->rcImage.top    = pt.y;
1872     pImageInfo->rcImage.bottom = pt.y + himl->cy;
1873     pImageInfo->rcImage.left   = pt.x;
1874     pImageInfo->rcImage.right  = pt.x + himl->cx;
1875
1876     return TRUE;
1877 }
1878
1879
1880 /*************************************************************************
1881  * ImageList_GetImageRect [COMCTL32.@]
1882  *
1883  * Retrieves the rectangle of the specified image in an image list.
1884  *
1885  * PARAMS
1886  *     himl   [I] handle to image list
1887  *     i      [I] image index
1888  *     lpRect [O] pointer to the image rectangle
1889  *
1890  * RETURNS
1891  *    Success: TRUE
1892  *    Failure: FALSE
1893  *
1894  * NOTES
1895  *    This is an UNDOCUMENTED function!!!
1896  */
1897
1898 BOOL WINAPI
1899 ImageList_GetImageRect (HIMAGELIST himl, INT i, LPRECT lpRect)
1900 {
1901     POINT pt;
1902
1903     if (!is_valid(himl) || (lpRect == NULL))
1904         return FALSE;
1905     if ((i < 0) || (i >= himl->cCurImage))
1906         return FALSE;
1907
1908     imagelist_point_from_index( himl, i, &pt );
1909     lpRect->left   = pt.x;
1910     lpRect->top    = pt.y;
1911     lpRect->right  = pt.x + himl->cx;
1912     lpRect->bottom = pt.y + himl->cy;
1913
1914     return TRUE;
1915 }
1916
1917
1918 /*************************************************************************
1919  * ImageList_LoadImage  [COMCTL32.@]
1920  * ImageList_LoadImageA [COMCTL32.@]
1921  *
1922  * Creates an image list from a bitmap, icon or cursor.
1923  *
1924  * See ImageList_LoadImageW.
1925  */
1926
1927 HIMAGELIST WINAPI
1928 ImageList_LoadImageA (HINSTANCE hi, LPCSTR lpbmp, INT cx, INT cGrow,
1929                         COLORREF clrMask, UINT uType, UINT uFlags)
1930 {
1931     HIMAGELIST himl;
1932     LPWSTR lpbmpW;
1933     DWORD len;
1934
1935     if (IS_INTRESOURCE(lpbmp))
1936         return ImageList_LoadImageW(hi, (LPCWSTR)lpbmp, cx, cGrow, clrMask,
1937                                     uType, uFlags);
1938
1939     len = MultiByteToWideChar(CP_ACP, 0, lpbmp, -1, NULL, 0);
1940     lpbmpW = Alloc(len * sizeof(WCHAR));
1941     MultiByteToWideChar(CP_ACP, 0, lpbmp, -1, lpbmpW, len);
1942
1943     himl = ImageList_LoadImageW(hi, lpbmpW, cx, cGrow, clrMask, uType, uFlags);
1944     Free (lpbmpW);
1945     return himl;
1946 }
1947
1948
1949 /*************************************************************************
1950  * ImageList_LoadImageW [COMCTL32.@]
1951  *
1952  * Creates an image list from a bitmap, icon or cursor.
1953  *
1954  * PARAMS
1955  *     hi      [I] instance handle
1956  *     lpbmp   [I] name or id of the image
1957  *     cx      [I] width of each image
1958  *     cGrow   [I] number of images to expand
1959  *     clrMask [I] mask color
1960  *     uType   [I] type of image to load
1961  *     uFlags  [I] loading flags
1962  *
1963  * RETURNS
1964  *     Success: handle to the loaded image list
1965  *     Failure: NULL
1966  *
1967  * SEE
1968  *     LoadImage ()
1969  */
1970
1971 HIMAGELIST WINAPI
1972 ImageList_LoadImageW (HINSTANCE hi, LPCWSTR lpbmp, INT cx, INT cGrow,
1973                       COLORREF clrMask, UINT uType, UINT uFlags)
1974 {
1975     HIMAGELIST himl = NULL;
1976     HANDLE   handle;
1977     INT      nImageCount;
1978
1979     handle = LoadImageW (hi, lpbmp, uType, 0, 0, uFlags);
1980     if (!handle) {
1981         WARN("Couldn't load image\n");
1982         return NULL;
1983     }
1984
1985     if (uType == IMAGE_BITMAP) {
1986         DIBSECTION dib;
1987         UINT color;
1988
1989         if (GetObjectW (handle, sizeof(dib), &dib) == sizeof(BITMAP)) color = ILC_COLOR;
1990         else color = dib.dsBm.bmBitsPixel;
1991
1992         /* To match windows behavior, if cx is set to zero and
1993          the flag DI_DEFAULTSIZE is specified, cx becomes the
1994          system metric value for icons. If the flag is not specified
1995          the function sets the size to the height of the bitmap */
1996         if (cx == 0)
1997         {
1998             if (uFlags & DI_DEFAULTSIZE)
1999                 cx = GetSystemMetrics (SM_CXICON);
2000             else
2001                 cx = dib.dsBm.bmHeight;
2002         }
2003
2004         nImageCount = dib.dsBm.bmWidth / cx;
2005
2006         himl = ImageList_Create (cx, dib.dsBm.bmHeight, ILC_MASK | color, nImageCount, cGrow);
2007         if (!himl) {
2008             DeleteObject (handle);
2009             return NULL;
2010         }
2011         ImageList_AddMasked (himl, handle, clrMask);
2012     }
2013     else if ((uType == IMAGE_ICON) || (uType == IMAGE_CURSOR)) {
2014         ICONINFO ii;
2015         BITMAP bmp;
2016
2017         GetIconInfo (handle, &ii);
2018         GetObjectW (ii.hbmColor, sizeof(BITMAP), &bmp);
2019         himl = ImageList_Create (bmp.bmWidth, bmp.bmHeight,
2020                                  ILC_MASK | ILC_COLOR, 1, cGrow);
2021         if (!himl) {
2022             DeleteObject (ii.hbmColor);
2023             DeleteObject (ii.hbmMask);
2024             DeleteObject (handle);
2025             return NULL;
2026         }
2027         ImageList_Add (himl, ii.hbmColor, ii.hbmMask);
2028         DeleteObject (ii.hbmColor);
2029         DeleteObject (ii.hbmMask);
2030     }
2031
2032     DeleteObject (handle);
2033
2034     return himl;
2035 }
2036
2037
2038 /*************************************************************************
2039  * ImageList_Merge [COMCTL32.@]
2040  *
2041  * Create an image list containing a merged image from two image lists.
2042  *
2043  * PARAMS
2044  *     himl1 [I] handle to first image list
2045  *     i1    [I] first image index
2046  *     himl2 [I] handle to second image list
2047  *     i2    [I] second image index
2048  *     dx    [I] X offset of the second image relative to the first.
2049  *     dy    [I] Y offset of the second image relative to the first.
2050  *
2051  * RETURNS
2052  *     Success: The newly created image list. It contains a single image
2053  *              consisting of the second image merged with the first.
2054  *     Failure: NULL, if either himl1 or himl2 are invalid.
2055  *
2056  * NOTES
2057  *   - The returned image list should be deleted by the caller using
2058  *     ImageList_Destroy() when it is no longer required.
2059  *   - If either i1 or i2 are not valid image indices they will be treated
2060  *     as a blank image.
2061  */
2062 HIMAGELIST WINAPI
2063 ImageList_Merge (HIMAGELIST himl1, INT i1, HIMAGELIST himl2, INT i2,
2064                  INT dx, INT dy)
2065 {
2066     HIMAGELIST himlDst = NULL;
2067     INT      cxDst, cyDst;
2068     INT      xOff1, yOff1, xOff2, yOff2;
2069     POINT    pt1, pt2;
2070
2071     TRACE("(himl1=%p i1=%d himl2=%p i2=%d dx=%d dy=%d)\n", himl1, i1, himl2,
2072            i2, dx, dy);
2073
2074     if (!is_valid(himl1) || !is_valid(himl2))
2075         return NULL;
2076
2077     if (dx > 0) {
2078         cxDst = max (himl1->cx, dx + himl2->cx);
2079         xOff1 = 0;
2080         xOff2 = dx;
2081     }
2082     else if (dx < 0) {
2083         cxDst = max (himl2->cx, himl1->cx - dx);
2084         xOff1 = -dx;
2085         xOff2 = 0;
2086     }
2087     else {
2088         cxDst = max (himl1->cx, himl2->cx);
2089         xOff1 = 0;
2090         xOff2 = 0;
2091     }
2092
2093     if (dy > 0) {
2094         cyDst = max (himl1->cy, dy + himl2->cy);
2095         yOff1 = 0;
2096         yOff2 = dy;
2097     }
2098     else if (dy < 0) {
2099         cyDst = max (himl2->cy, himl1->cy - dy);
2100         yOff1 = -dy;
2101         yOff2 = 0;
2102     }
2103     else {
2104         cyDst = max (himl1->cy, himl2->cy);
2105         yOff1 = 0;
2106         yOff2 = 0;
2107     }
2108
2109     himlDst = ImageList_Create (cxDst, cyDst, ILC_MASK | ILC_COLOR, 1, 1);
2110
2111     if (himlDst)
2112     {
2113         imagelist_point_from_index( himl1, i1, &pt1 );
2114         imagelist_point_from_index( himl2, i2, &pt2 );
2115
2116         /* copy image */
2117         BitBlt (himlDst->hdcImage, 0, 0, cxDst, cyDst, himl1->hdcImage, 0, 0, BLACKNESS);
2118         if (i1 >= 0 && i1 < himl1->cCurImage)
2119             BitBlt (himlDst->hdcImage, xOff1, yOff1, himl1->cx, himl1->cy, himl1->hdcImage, pt1.x, pt1.y, SRCCOPY);
2120         if (i2 >= 0 && i2 < himl2->cCurImage)
2121         {
2122             BitBlt (himlDst->hdcImage, xOff2, yOff2, himl2->cx, himl2->cy, himl2->hdcMask , pt2.x, pt2.y, SRCAND);
2123             BitBlt (himlDst->hdcImage, xOff2, yOff2, himl2->cx, himl2->cy, himl2->hdcImage, pt2.x, pt2.y, SRCPAINT);
2124         }
2125
2126         /* copy mask */
2127         BitBlt (himlDst->hdcMask, 0, 0, cxDst, cyDst, himl1->hdcMask, 0, 0, WHITENESS);
2128         if (i1 >= 0 && i1 < himl1->cCurImage)
2129             BitBlt (himlDst->hdcMask,  xOff1, yOff1, himl1->cx, himl1->cy, himl1->hdcMask,  pt1.x, pt1.y, SRCCOPY);
2130         if (i2 >= 0 && i2 < himl2->cCurImage)
2131             BitBlt (himlDst->hdcMask,  xOff2, yOff2, himl2->cx, himl2->cy, himl2->hdcMask,  pt2.x, pt2.y, SRCAND);
2132
2133         himlDst->cCurImage = 1;
2134     }
2135
2136     return himlDst;
2137 }
2138
2139
2140 /* helper for ImageList_Read, see comments below */
2141 static void *read_bitmap(LPSTREAM pstm, BITMAPINFO *bmi)
2142 {
2143     BITMAPFILEHEADER    bmfh;
2144     int bitsperpixel, palspace;
2145     void *bits;
2146
2147     if (FAILED(IStream_Read ( pstm, &bmfh, sizeof(bmfh), NULL)))
2148         return NULL;
2149
2150     if (bmfh.bfType != (('M'<<8)|'B'))
2151         return NULL;
2152
2153     if (FAILED(IStream_Read ( pstm, &bmi->bmiHeader, sizeof(bmi->bmiHeader), NULL)))
2154         return NULL;
2155
2156     if ((bmi->bmiHeader.biSize != sizeof(bmi->bmiHeader)))
2157         return NULL;
2158
2159     TRACE("width %u, height %u, planes %u, bpp %u\n",
2160           bmi->bmiHeader.biWidth, bmi->bmiHeader.biHeight,
2161           bmi->bmiHeader.biPlanes, bmi->bmiHeader.biBitCount);
2162
2163     bitsperpixel = bmi->bmiHeader.biPlanes * bmi->bmiHeader.biBitCount;
2164     if (bitsperpixel<=8)
2165         palspace = (1<<bitsperpixel)*sizeof(RGBQUAD);
2166     else
2167         palspace = 0;
2168
2169     bmi->bmiHeader.biSizeImage = get_dib_image_size( bmi );
2170
2171     /* read the palette right after the end of the bitmapinfoheader */
2172     if (palspace && FAILED(IStream_Read(pstm, bmi->bmiColors, palspace, NULL)))
2173         return NULL;
2174
2175     bits = Alloc(bmi->bmiHeader.biSizeImage);
2176     if (!bits) return NULL;
2177
2178     if (FAILED(IStream_Read(pstm, bits, bmi->bmiHeader.biSizeImage, NULL)))
2179     {
2180         Free(bits);
2181         return NULL;
2182     }
2183     return bits;
2184 }
2185
2186 /*************************************************************************
2187  * ImageList_Read [COMCTL32.@]
2188  *
2189  * Reads an image list from a stream.
2190  *
2191  * PARAMS
2192  *     pstm [I] pointer to a stream
2193  *
2194  * RETURNS
2195  *     Success: handle to image list
2196  *     Failure: NULL
2197  *
2198  * The format is like this:
2199  *      ILHEAD                  ilheadstruct;
2200  *
2201  * for the color image part:
2202  *      BITMAPFILEHEADER        bmfh;
2203  *      BITMAPINFOHEADER        bmih;
2204  * only if it has a palette:
2205  *      RGBQUAD         rgbs[nr_of_paletted_colors];
2206  *
2207  *      BYTE                    colorbits[imagesize];
2208  *
2209  * the following only if the ILC_MASK bit is set in ILHEAD.ilFlags:
2210  *      BITMAPFILEHEADER        bmfh_mask;
2211  *      BITMAPINFOHEADER        bmih_mask;
2212  * only if it has a palette (it usually does not):
2213  *      RGBQUAD         rgbs[nr_of_paletted_colors];
2214  *
2215  *      BYTE                    maskbits[imagesize];
2216  */
2217 HIMAGELIST WINAPI ImageList_Read (LPSTREAM pstm)
2218 {
2219     char image_buf[sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * 256];
2220     char mask_buf[sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * 256];
2221     BITMAPINFO *image_info = (BITMAPINFO *)image_buf;
2222     BITMAPINFO *mask_info = (BITMAPINFO *)mask_buf;
2223     void *image_bits, *mask_bits = NULL;
2224     ILHEAD      ilHead;
2225     HIMAGELIST  himl;
2226     int         i;
2227
2228     TRACE("%p\n", pstm);
2229
2230     if (FAILED(IStream_Read (pstm, &ilHead, sizeof(ILHEAD), NULL)))
2231         return NULL;
2232     if (ilHead.usMagic != (('L' << 8) | 'I'))
2233         return NULL;
2234     if (ilHead.usVersion != 0x101) /* probably version? */
2235         return NULL;
2236
2237     TRACE("cx %u, cy %u, flags 0x%04x, cCurImage %u, cMaxImage %u\n",
2238           ilHead.cx, ilHead.cy, ilHead.flags, ilHead.cCurImage, ilHead.cMaxImage);
2239
2240     himl = ImageList_Create(ilHead.cx, ilHead.cy, ilHead.flags, ilHead.cCurImage, ilHead.cMaxImage);
2241     if (!himl)
2242         return NULL;
2243
2244     if (!(image_bits = read_bitmap(pstm, image_info)))
2245     {
2246         WARN("failed to read bitmap from stream\n");
2247         return NULL;
2248     }
2249     if (ilHead.flags & ILC_MASK)
2250     {
2251         if (!(mask_bits = read_bitmap(pstm, mask_info)))
2252         {
2253             WARN("failed to read mask bitmap from stream\n");
2254             return NULL;
2255         }
2256     }
2257     else mask_info = NULL;
2258
2259     if (himl->has_alpha && image_info->bmiHeader.biBitCount == 32)
2260     {
2261         DWORD *ptr = image_bits;
2262         BYTE *mask_ptr = mask_bits;
2263         int stride = himl->cy * image_info->bmiHeader.biWidth;
2264
2265         if (image_info->bmiHeader.biHeight > 0)  /* bottom-up */
2266         {
2267             ptr += image_info->bmiHeader.biHeight * image_info->bmiHeader.biWidth - stride;
2268             mask_ptr += (image_info->bmiHeader.biHeight * image_info->bmiHeader.biWidth - stride) / 8;
2269             stride = -stride;
2270             image_info->bmiHeader.biHeight = himl->cy;
2271         }
2272         else image_info->bmiHeader.biHeight = -himl->cy;
2273
2274         for (i = 0; i < ilHead.cCurImage; i += TILE_COUNT)
2275         {
2276             add_dib_bits( himl, i, min( ilHead.cCurImage - i, TILE_COUNT ),
2277                           himl->cx, himl->cy, image_info, mask_info, ptr, mask_ptr );
2278             ptr += stride;
2279             mask_ptr += stride / 8;
2280         }
2281     }
2282     else
2283     {
2284         StretchDIBits( himl->hdcImage, 0, 0, image_info->bmiHeader.biWidth, image_info->bmiHeader.biHeight,
2285                        0, 0, image_info->bmiHeader.biWidth, image_info->bmiHeader.biHeight,
2286                        image_bits, image_info, DIB_RGB_COLORS, SRCCOPY);
2287         if (mask_info)
2288             StretchDIBits( himl->hdcMask, 0, 0, mask_info->bmiHeader.biWidth, mask_info->bmiHeader.biHeight,
2289                            0, 0, mask_info->bmiHeader.biWidth, mask_info->bmiHeader.biHeight,
2290                            mask_bits, mask_info, DIB_RGB_COLORS, SRCCOPY);
2291     }
2292     Free( image_bits );
2293     Free( mask_bits );
2294
2295     himl->cCurImage = ilHead.cCurImage;
2296     himl->cMaxImage = ilHead.cMaxImage;
2297
2298     ImageList_SetBkColor(himl,ilHead.bkcolor);
2299     for (i=0;i<4;i++)
2300         ImageList_SetOverlayImage(himl,ilHead.ovls[i],i+1);
2301     return himl;
2302 }
2303
2304
2305 /*************************************************************************
2306  * ImageList_Remove [COMCTL32.@]
2307  *
2308  * Removes an image from an image list
2309  *
2310  * PARAMS
2311  *     himl [I] image list handle
2312  *     i    [I] image index
2313  *
2314  * RETURNS
2315  *     Success: TRUE
2316  *     Failure: FALSE
2317  *
2318  * FIXME: as the image list storage test shows, native comctl32 simply shifts
2319  * images without creating a new bitmap.
2320  */
2321 BOOL WINAPI
2322 ImageList_Remove (HIMAGELIST himl, INT i)
2323 {
2324     HBITMAP hbmNewImage, hbmNewMask;
2325     HDC     hdcBmp;
2326     SIZE    sz;
2327
2328     TRACE("(himl=%p i=%d)\n", himl, i);
2329
2330     if (!is_valid(himl)) {
2331         ERR("Invalid image list handle!\n");
2332         return FALSE;
2333     }
2334
2335     if ((i < -1) || (i >= himl->cCurImage)) {
2336         TRACE("index out of range! %d\n", i);
2337         return FALSE;
2338     }
2339
2340     if (i == -1) {
2341         INT nCount;
2342
2343         /* remove all */
2344         if (himl->cCurImage == 0) {
2345             /* remove all on empty ImageList is allowed */
2346             TRACE("remove all on empty ImageList!\n");
2347             return TRUE;
2348         }
2349
2350         himl->cMaxImage = himl->cInitial + himl->cGrow - 1;
2351         himl->cCurImage = 0;
2352         for (nCount = 0; nCount < MAX_OVERLAYIMAGE; nCount++)
2353              himl->nOvlIdx[nCount] = -1;
2354
2355         hbmNewImage = ImageList_CreateImage(himl->hdcImage, himl, himl->cMaxImage);
2356         SelectObject (himl->hdcImage, hbmNewImage);
2357         DeleteObject (himl->hbmImage);
2358         himl->hbmImage = hbmNewImage;
2359
2360         if (himl->hbmMask) {
2361
2362             imagelist_get_bitmap_size(himl, himl->cMaxImage, &sz);
2363             hbmNewMask = CreateBitmap (sz.cx, sz.cy, 1, 1, NULL);
2364             SelectObject (himl->hdcMask, hbmNewMask);
2365             DeleteObject (himl->hbmMask);
2366             himl->hbmMask = hbmNewMask;
2367         }
2368     }
2369     else {
2370         /* delete one image */
2371         TRACE("Remove single image! %d\n", i);
2372
2373         /* create new bitmap(s) */
2374         TRACE(" - Number of images: %d / %d (Old/New)\n",
2375                  himl->cCurImage, himl->cCurImage - 1);
2376
2377         hbmNewImage = ImageList_CreateImage(himl->hdcImage, himl, himl->cMaxImage);
2378
2379         imagelist_get_bitmap_size(himl, himl->cMaxImage, &sz );
2380         if (himl->hbmMask)
2381             hbmNewMask = CreateBitmap (sz.cx, sz.cy, 1, 1, NULL);
2382         else
2383             hbmNewMask = 0;  /* Just to keep compiler happy! */
2384
2385         hdcBmp = CreateCompatibleDC (0);
2386
2387         /* copy all images and masks prior to the "removed" image */
2388         if (i > 0) {
2389             TRACE("Pre image copy: Copy %d images\n", i);
2390
2391             SelectObject (hdcBmp, hbmNewImage);
2392             imagelist_copy_images( himl, himl->hdcImage, hdcBmp, 0, i, 0 );
2393
2394             if (himl->hbmMask) {
2395                 SelectObject (hdcBmp, hbmNewMask);
2396                 imagelist_copy_images( himl, himl->hdcMask, hdcBmp, 0, i, 0 );
2397             }
2398         }
2399
2400         /* copy all images and masks behind the removed image */
2401         if (i < himl->cCurImage - 1) {
2402             TRACE("Post image copy!\n");
2403
2404             SelectObject (hdcBmp, hbmNewImage);
2405             imagelist_copy_images( himl, himl->hdcImage, hdcBmp, i + 1,
2406                                    (himl->cCurImage - i), i );
2407
2408             if (himl->hbmMask) {
2409                 SelectObject (hdcBmp, hbmNewMask);
2410                 imagelist_copy_images( himl, himl->hdcMask, hdcBmp, i + 1,
2411                                        (himl->cCurImage - i), i );
2412             }
2413         }
2414
2415         DeleteDC (hdcBmp);
2416
2417         /* delete old images and insert new ones */
2418         SelectObject (himl->hdcImage, hbmNewImage);
2419         DeleteObject (himl->hbmImage);
2420         himl->hbmImage = hbmNewImage;
2421         if (himl->hbmMask) {
2422             SelectObject (himl->hdcMask, hbmNewMask);
2423             DeleteObject (himl->hbmMask);
2424             himl->hbmMask = hbmNewMask;
2425         }
2426
2427         himl->cCurImage--;
2428     }
2429
2430     return TRUE;
2431 }
2432
2433
2434 /*************************************************************************
2435  * ImageList_Replace [COMCTL32.@]
2436  *
2437  * Replaces an image in an image list with a new image.
2438  *
2439  * PARAMS
2440  *     himl     [I] handle to image list
2441  *     i        [I] image index
2442  *     hbmImage [I] handle to image bitmap
2443  *     hbmMask  [I] handle to mask bitmap. Can be NULL.
2444  *
2445  * RETURNS
2446  *     Success: TRUE
2447  *     Failure: FALSE
2448  */
2449
2450 BOOL WINAPI
2451 ImageList_Replace (HIMAGELIST himl, INT i, HBITMAP hbmImage,
2452                    HBITMAP hbmMask)
2453 {
2454     HDC hdcImage;
2455     BITMAP bmp;
2456     POINT pt;
2457
2458     TRACE("%p %d %p %p\n", himl, i, hbmImage, hbmMask);
2459
2460     if (!is_valid(himl)) {
2461         ERR("Invalid image list handle!\n");
2462         return FALSE;
2463     }
2464
2465     if ((i >= himl->cMaxImage) || (i < 0)) {
2466         ERR("Invalid image index!\n");
2467         return FALSE;
2468     }
2469
2470     if (!GetObjectW(hbmImage, sizeof(BITMAP), &bmp))
2471         return FALSE;
2472
2473     hdcImage = CreateCompatibleDC (0);
2474
2475     /* Replace Image */
2476     SelectObject (hdcImage, hbmImage);
2477
2478     if (add_with_alpha( himl, hdcImage, i, 1, bmp.bmWidth, bmp.bmHeight, hbmImage, hbmMask ))
2479         goto done;
2480
2481     imagelist_point_from_index(himl, i, &pt);
2482     StretchBlt (himl->hdcImage, pt.x, pt.y, himl->cx, himl->cy,
2483                   hdcImage, 0, 0, bmp.bmWidth, bmp.bmHeight, SRCCOPY);
2484
2485     if (himl->hbmMask)
2486     {
2487         HDC hdcTemp;
2488         HBITMAP hOldBitmapTemp;
2489
2490         hdcTemp   = CreateCompatibleDC(0);
2491         hOldBitmapTemp = SelectObject(hdcTemp, hbmMask);
2492
2493         StretchBlt (himl->hdcMask, pt.x, pt.y, himl->cx, himl->cy,
2494                       hdcTemp, 0, 0, bmp.bmWidth, bmp.bmHeight, SRCCOPY);
2495         SelectObject(hdcTemp, hOldBitmapTemp);
2496         DeleteDC(hdcTemp);
2497
2498         /* Remove the background from the image
2499         */
2500         BitBlt (himl->hdcImage, pt.x, pt.y, bmp.bmWidth, bmp.bmHeight,
2501                 himl->hdcMask, pt.x, pt.y, 0x220326); /* NOTSRCAND */
2502     }
2503
2504 done:
2505     DeleteDC (hdcImage);
2506
2507     return TRUE;
2508 }
2509
2510
2511 /*************************************************************************
2512  * ImageList_ReplaceIcon [COMCTL32.@]
2513  *
2514  * Replaces an image in an image list using an icon.
2515  *
2516  * PARAMS
2517  *     himl  [I] handle to image list
2518  *     i     [I] image index
2519  *     hIcon [I] handle to icon
2520  *
2521  * RETURNS
2522  *     Success: index of the replaced image
2523  *     Failure: -1
2524  */
2525
2526 INT WINAPI
2527 ImageList_ReplaceIcon (HIMAGELIST himl, INT nIndex, HICON hIcon)
2528 {
2529     HDC     hdcImage;
2530     HICON   hBestFitIcon;
2531     ICONINFO  ii;
2532     BITMAP  bmp;
2533     BOOL    ret;
2534     POINT   pt;
2535
2536     TRACE("(%p %d %p)\n", himl, nIndex, hIcon);
2537
2538     if (!is_valid(himl)) {
2539         ERR("invalid image list\n");
2540         return -1;
2541     }
2542     if ((nIndex >= himl->cMaxImage) || (nIndex < -1)) {
2543         ERR("invalid image index %d / %d\n", nIndex, himl->cMaxImage);
2544         return -1;
2545     }
2546
2547     hBestFitIcon = CopyImage(
2548         hIcon, IMAGE_ICON,
2549         himl->cx, himl->cy,
2550         LR_COPYFROMRESOURCE);
2551     /* the above will fail if the icon wasn't loaded from a resource, so try
2552      * again without LR_COPYFROMRESOURCE flag */
2553     if (!hBestFitIcon)
2554         hBestFitIcon = CopyImage(
2555             hIcon, IMAGE_ICON,
2556             himl->cx, himl->cy,
2557             0);
2558     if (!hBestFitIcon)
2559         return -1;
2560
2561     ret = GetIconInfo (hBestFitIcon, &ii);
2562     if (!ret) {
2563         DestroyIcon(hBestFitIcon);
2564         return -1;
2565     }
2566
2567     ret = GetObjectW (ii.hbmMask, sizeof(BITMAP), &bmp);
2568     if (!ret) {
2569         ERR("couldn't get mask bitmap info\n");
2570         if (ii.hbmColor)
2571             DeleteObject (ii.hbmColor);
2572         if (ii.hbmMask)
2573             DeleteObject (ii.hbmMask);
2574         DestroyIcon(hBestFitIcon);
2575         return -1;
2576     }
2577
2578     if (nIndex == -1) {
2579         if (himl->cCurImage + 1 > himl->cMaxImage)
2580             IMAGELIST_InternalExpandBitmaps(himl, 1);
2581
2582         nIndex = himl->cCurImage;
2583         himl->cCurImage++;
2584     }
2585
2586     hdcImage = CreateCompatibleDC (0);
2587     TRACE("hdcImage=%p\n", hdcImage);
2588     if (hdcImage == 0)
2589         ERR("invalid hdcImage!\n");
2590
2591     if (himl->has_alpha)
2592     {
2593         if (!ii.hbmColor)
2594         {
2595             UINT height = bmp.bmHeight / 2;
2596             HDC hdcMask = CreateCompatibleDC( 0 );
2597             HBITMAP color = CreateBitmap( bmp.bmWidth, height, 1, 1, NULL );
2598             SelectObject( hdcImage, color );
2599             SelectObject( hdcMask, ii.hbmMask );
2600             BitBlt( hdcImage, 0, 0, bmp.bmWidth, height, hdcMask, 0, height, SRCCOPY );
2601             ret = add_with_alpha( himl, hdcImage, nIndex, 1, bmp.bmWidth, height, color, ii.hbmMask );
2602             DeleteDC( hdcMask );
2603             DeleteObject( color );
2604             if (ret) goto done;
2605         }
2606         else if (add_with_alpha( himl, hdcImage, nIndex, 1, bmp.bmWidth, bmp.bmHeight,
2607                                  ii.hbmColor, ii.hbmMask )) goto done;
2608     }
2609
2610     imagelist_point_from_index(himl, nIndex, &pt);
2611
2612     SetTextColor(himl->hdcImage, RGB(0,0,0));
2613     SetBkColor  (himl->hdcImage, RGB(255,255,255));
2614
2615     if (ii.hbmColor)
2616     {
2617         SelectObject (hdcImage, ii.hbmColor);
2618         StretchBlt (himl->hdcImage, pt.x, pt.y, himl->cx, himl->cy,
2619                     hdcImage, 0, 0, bmp.bmWidth, bmp.bmHeight, SRCCOPY);
2620         if (himl->hbmMask)
2621         {
2622             SelectObject (hdcImage, ii.hbmMask);
2623             StretchBlt (himl->hdcMask, pt.x, pt.y, himl->cx, himl->cy,
2624                         hdcImage, 0, 0, bmp.bmWidth, bmp.bmHeight, SRCCOPY);
2625         }
2626     }
2627     else
2628     {
2629         UINT height = bmp.bmHeight / 2;
2630         SelectObject (hdcImage, ii.hbmMask);
2631         StretchBlt (himl->hdcImage, pt.x, pt.y, himl->cx, himl->cy,
2632                     hdcImage, 0, height, bmp.bmWidth, height, SRCCOPY);
2633         if (himl->hbmMask)
2634             StretchBlt (himl->hdcMask, pt.x, pt.y, himl->cx, himl->cy,
2635                         hdcImage, 0, 0, bmp.bmWidth, height, SRCCOPY);
2636     }
2637
2638 done:
2639     DestroyIcon(hBestFitIcon);
2640     if (hdcImage)
2641         DeleteDC (hdcImage);
2642     if (ii.hbmColor)
2643         DeleteObject (ii.hbmColor);
2644     if (ii.hbmMask)
2645         DeleteObject (ii.hbmMask);
2646
2647     TRACE("Insert index = %d, himl->cCurImage = %d\n", nIndex, himl->cCurImage);
2648     return nIndex;
2649 }
2650
2651
2652 /*************************************************************************
2653  * ImageList_SetBkColor [COMCTL32.@]
2654  *
2655  * Sets the background color of an image list.
2656  *
2657  * PARAMS
2658  *     himl  [I] handle to image list
2659  *     clrBk [I] background color
2660  *
2661  * RETURNS
2662  *     Success: previous background color
2663  *     Failure: CLR_NONE
2664  */
2665
2666 COLORREF WINAPI
2667 ImageList_SetBkColor (HIMAGELIST himl, COLORREF clrBk)
2668 {
2669     COLORREF clrOldBk;
2670
2671     if (!is_valid(himl))
2672         return CLR_NONE;
2673
2674     clrOldBk = himl->clrBk;
2675     himl->clrBk = clrBk;
2676     return clrOldBk;
2677 }
2678
2679
2680 /*************************************************************************
2681  * ImageList_SetDragCursorImage [COMCTL32.@]
2682  *
2683  * Combines the specified image with the current drag image
2684  *
2685  * PARAMS
2686  *     himlDrag  [I] handle to drag image list
2687  *     iDrag     [I] drag image index
2688  *     dxHotspot [I] X position of the hot spot
2689  *     dyHotspot [I] Y position of the hot spot
2690  *
2691  * RETURNS
2692  *     Success: TRUE
2693  *     Failure: FALSE
2694  *
2695  * NOTES
2696  *   - The names dxHotspot, dyHotspot are misleading because they have nothing
2697  *     to do with a hotspot but are only the offset of the origin of the new
2698  *     image relative to the origin of the old image.
2699  *
2700  *   - When this function is called and the drag image is visible, a
2701  *     short flickering occurs but this matches the Win9x behavior. It is
2702  *     possible to fix the flickering using code like in ImageList_DragMove.
2703  */
2704
2705 BOOL WINAPI
2706 ImageList_SetDragCursorImage (HIMAGELIST himlDrag, INT iDrag,
2707                               INT dxHotspot, INT dyHotspot)
2708 {
2709     HIMAGELIST himlTemp;
2710     BOOL visible;
2711
2712     if (!is_valid(InternalDrag.himl) || !is_valid(himlDrag))
2713         return FALSE;
2714
2715     TRACE(" dxH=%d dyH=%d nX=%d nY=%d\n",
2716            dxHotspot, dyHotspot, InternalDrag.dxHotspot, InternalDrag.dyHotspot);
2717
2718     visible = InternalDrag.bShow;
2719
2720     himlTemp = ImageList_Merge (InternalDrag.himl, 0, himlDrag, iDrag,
2721                                 dxHotspot, dyHotspot);
2722
2723     if (visible) {
2724         /* hide the drag image */
2725         ImageList_DragShowNolock(FALSE);
2726     }
2727     if ((InternalDrag.himl->cx != himlTemp->cx) ||
2728            (InternalDrag.himl->cy != himlTemp->cy)) {
2729         /* the size of the drag image changed, invalidate the buffer */
2730         DeleteObject(InternalDrag.hbmBg);
2731         InternalDrag.hbmBg = 0;
2732     }
2733
2734     ImageList_Destroy (InternalDrag.himl);
2735     InternalDrag.himl = himlTemp;
2736
2737     if (visible) {
2738         /* show the drag image */
2739         ImageList_DragShowNolock(TRUE);
2740     }
2741
2742     return TRUE;
2743 }
2744
2745
2746 /*************************************************************************
2747  * ImageList_SetFilter [COMCTL32.@]
2748  *
2749  * Sets a filter (or does something completely different)!!???
2750  * It removes 12 Bytes from the stack (3 Parameters).
2751  *
2752  * PARAMS
2753  *     himl     [I] SHOULD be a handle to image list
2754  *     i        [I] COULD be an index?
2755  *     dwFilter [I] ???
2756  *
2757  * RETURNS
2758  *     Success: TRUE ???
2759  *     Failure: FALSE ???
2760  *
2761  * BUGS
2762  *     This is an UNDOCUMENTED function!!!!
2763  *     empty stub.
2764  */
2765
2766 BOOL WINAPI
2767 ImageList_SetFilter (HIMAGELIST himl, INT i, DWORD dwFilter)
2768 {
2769     FIXME("(%p 0x%x 0x%x):empty stub!\n", himl, i, dwFilter);
2770
2771     return FALSE;
2772 }
2773
2774
2775 /*************************************************************************
2776  * ImageList_SetFlags [COMCTL32.@]
2777  *
2778  * Sets the image list flags.
2779  *
2780  * PARAMS
2781  *     himl  [I] Handle to image list
2782  *     flags [I] Flags to set
2783  *
2784  * RETURNS
2785  *     Old flags?
2786  *
2787  * BUGS
2788  *    Stub.
2789  */
2790
2791 DWORD WINAPI
2792 ImageList_SetFlags(HIMAGELIST himl, DWORD flags)
2793 {
2794     FIXME("(%p %08x):empty stub\n", himl, flags);
2795     return 0;
2796 }
2797
2798
2799 /*************************************************************************
2800  * ImageList_SetIconSize [COMCTL32.@]
2801  *
2802  * Sets the image size of the bitmap and deletes all images.
2803  *
2804  * PARAMS
2805  *     himl [I] handle to image list
2806  *     cx   [I] image width
2807  *     cy   [I] image height
2808  *
2809  * RETURNS
2810  *     Success: TRUE
2811  *     Failure: FALSE
2812  */
2813
2814 BOOL WINAPI
2815 ImageList_SetIconSize (HIMAGELIST himl, INT cx, INT cy)
2816 {
2817     INT nCount;
2818     HBITMAP hbmNew;
2819
2820     if (!is_valid(himl))
2821         return FALSE;
2822
2823     /* remove all images */
2824     himl->cMaxImage = himl->cInitial + 1;
2825     himl->cCurImage = 0;
2826     himl->cx        = cx;
2827     himl->cy        = cy;
2828
2829     /* initialize overlay mask indices */
2830     for (nCount = 0; nCount < MAX_OVERLAYIMAGE; nCount++)
2831         himl->nOvlIdx[nCount] = -1;
2832
2833     hbmNew = ImageList_CreateImage(himl->hdcImage, himl, himl->cMaxImage);
2834     SelectObject (himl->hdcImage, hbmNew);
2835     DeleteObject (himl->hbmImage);
2836     himl->hbmImage = hbmNew;
2837
2838     if (himl->hbmMask) {
2839         SIZE sz;
2840         imagelist_get_bitmap_size(himl, himl->cMaxImage, &sz);
2841         hbmNew = CreateBitmap (sz.cx, sz.cy, 1, 1, NULL);
2842         SelectObject (himl->hdcMask, hbmNew);
2843         DeleteObject (himl->hbmMask);
2844         himl->hbmMask = hbmNew;
2845     }
2846
2847     return TRUE;
2848 }
2849
2850
2851 /*************************************************************************
2852  * ImageList_SetImageCount [COMCTL32.@]
2853  *
2854  * Resizes an image list to the specified number of images.
2855  *
2856  * PARAMS
2857  *     himl        [I] handle to image list
2858  *     iImageCount [I] number of images in the image list
2859  *
2860  * RETURNS
2861  *     Success: TRUE
2862  *     Failure: FALSE
2863  */
2864
2865 BOOL WINAPI
2866 ImageList_SetImageCount (HIMAGELIST himl, UINT iImageCount)
2867 {
2868     HDC     hdcBitmap;
2869     HBITMAP hbmNewBitmap, hbmOld;
2870     INT     nNewCount, nCopyCount;
2871
2872     TRACE("%p %d\n",himl,iImageCount);
2873
2874     if (!is_valid(himl))
2875         return FALSE;
2876     if (himl->cMaxImage > iImageCount)
2877     {
2878         himl->cCurImage = iImageCount;
2879         /* TODO: shrink the bitmap when cMaxImage-cCurImage>cGrow ? */
2880         return TRUE;
2881     }
2882
2883     nNewCount = iImageCount + himl->cGrow;
2884     nCopyCount = min(himl->cCurImage, iImageCount);
2885
2886     hdcBitmap = CreateCompatibleDC (0);
2887
2888     hbmNewBitmap = ImageList_CreateImage(hdcBitmap, himl, nNewCount);
2889
2890     if (hbmNewBitmap != 0)
2891     {
2892         hbmOld = SelectObject (hdcBitmap, hbmNewBitmap);
2893         imagelist_copy_images( himl, himl->hdcImage, hdcBitmap, 0, nCopyCount, 0 );
2894         SelectObject (hdcBitmap, hbmOld);
2895
2896         /* FIXME: delete 'empty' image space? */
2897
2898         SelectObject (himl->hdcImage, hbmNewBitmap);
2899         DeleteObject (himl->hbmImage);
2900         himl->hbmImage = hbmNewBitmap;
2901     }
2902     else
2903         ERR("Could not create new image bitmap !\n");
2904
2905     if (himl->hbmMask)
2906     {
2907         SIZE sz;
2908         imagelist_get_bitmap_size( himl, nNewCount, &sz );
2909         hbmNewBitmap = CreateBitmap (sz.cx, sz.cy, 1, 1, NULL);
2910         if (hbmNewBitmap != 0)
2911         {
2912             hbmOld = SelectObject (hdcBitmap, hbmNewBitmap);
2913             imagelist_copy_images( himl, himl->hdcMask, hdcBitmap, 0, nCopyCount, 0 );
2914             SelectObject (hdcBitmap, hbmOld);
2915
2916             /* FIXME: delete 'empty' image space? */
2917
2918             SelectObject (himl->hdcMask, hbmNewBitmap);
2919             DeleteObject (himl->hbmMask);
2920             himl->hbmMask = hbmNewBitmap;
2921         }
2922         else
2923             ERR("Could not create new mask bitmap!\n");
2924     }
2925
2926     DeleteDC (hdcBitmap);
2927
2928     if (himl->has_alpha)
2929     {
2930         char *new_alpha = HeapReAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, himl->has_alpha, nNewCount );
2931         if (new_alpha) himl->has_alpha = new_alpha;
2932         else
2933         {
2934             HeapFree( GetProcessHeap(), 0, himl->has_alpha );
2935             himl->has_alpha = NULL;
2936         }
2937     }
2938
2939     /* Update max image count and current image count */
2940     himl->cMaxImage = nNewCount;
2941     himl->cCurImage = iImageCount;
2942
2943     return TRUE;
2944 }
2945
2946
2947 /*************************************************************************
2948  * ImageList_SetOverlayImage [COMCTL32.@]
2949  *
2950  * Assigns an overlay mask index to an existing image in an image list.
2951  *
2952  * PARAMS
2953  *     himl     [I] handle to image list
2954  *     iImage   [I] image index
2955  *     iOverlay [I] overlay mask index
2956  *
2957  * RETURNS
2958  *     Success: TRUE
2959  *     Failure: FALSE
2960  */
2961
2962 BOOL WINAPI
2963 ImageList_SetOverlayImage (HIMAGELIST himl, INT iImage, INT iOverlay)
2964 {
2965     if (!is_valid(himl))
2966         return FALSE;
2967     if ((iOverlay < 1) || (iOverlay > MAX_OVERLAYIMAGE))
2968         return FALSE;
2969     if ((iImage!=-1) && ((iImage < 0) || (iImage > himl->cCurImage)))
2970         return FALSE;
2971     himl->nOvlIdx[iOverlay - 1] = iImage;
2972     return TRUE;
2973 }
2974
2975
2976
2977 /* helper for ImageList_Write - write bitmap to pstm
2978  * currently everything is written as 24 bit RGB, except masks
2979  */
2980 static BOOL
2981 _write_bitmap(HBITMAP hBitmap, LPSTREAM pstm)
2982 {
2983     LPBITMAPFILEHEADER bmfh;
2984     LPBITMAPINFOHEADER bmih;
2985     LPBYTE data = NULL, lpBits;
2986     BITMAP bm;
2987     INT bitCount, sizeImage, offBits, totalSize;
2988     HDC xdc;
2989     BOOL result = FALSE;
2990
2991     if (!GetObjectW(hBitmap, sizeof(BITMAP), &bm))
2992         return FALSE;
2993
2994     bitCount = bm.bmBitsPixel == 1 ? 1 : 24;
2995     sizeImage = get_dib_stride(bm.bmWidth, bitCount) * bm.bmHeight;
2996
2997     totalSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
2998     if(bitCount != 24)
2999         totalSize += (1 << bitCount) * sizeof(RGBQUAD);
3000     offBits = totalSize;
3001     totalSize += sizeImage;
3002
3003     data = Alloc(totalSize);
3004     bmfh = (LPBITMAPFILEHEADER)data;
3005     bmih = (LPBITMAPINFOHEADER)(data + sizeof(BITMAPFILEHEADER));
3006     lpBits = data + offBits;
3007
3008     /* setup BITMAPFILEHEADER */
3009     bmfh->bfType      = (('M' << 8) | 'B');
3010     bmfh->bfSize      = offBits;
3011     bmfh->bfReserved1 = 0;
3012     bmfh->bfReserved2 = 0;
3013     bmfh->bfOffBits   = offBits;
3014
3015     /* setup BITMAPINFOHEADER */
3016     bmih->biSize          = sizeof(BITMAPINFOHEADER);
3017     bmih->biWidth         = bm.bmWidth;
3018     bmih->biHeight        = bm.bmHeight;
3019     bmih->biPlanes        = 1;
3020     bmih->biBitCount      = bitCount;
3021     bmih->biCompression   = BI_RGB;
3022     bmih->biSizeImage     = sizeImage;
3023     bmih->biXPelsPerMeter = 0;
3024     bmih->biYPelsPerMeter = 0;
3025     bmih->biClrUsed       = 0;
3026     bmih->biClrImportant  = 0;
3027
3028     xdc = GetDC(0);
3029     result = GetDIBits(xdc, hBitmap, 0, bm.bmHeight, lpBits, (BITMAPINFO *)bmih, DIB_RGB_COLORS) == bm.bmHeight;
3030     ReleaseDC(0, xdc);
3031     if (!result)
3032         goto failed;
3033
3034     TRACE("width %u, height %u, planes %u, bpp %u\n",
3035           bmih->biWidth, bmih->biHeight,
3036           bmih->biPlanes, bmih->biBitCount);
3037
3038     if(bitCount == 1) {
3039         /* Hack. */
3040         LPBITMAPINFO inf = (LPBITMAPINFO)bmih;
3041         inf->bmiColors[0].rgbRed = inf->bmiColors[0].rgbGreen = inf->bmiColors[0].rgbBlue = 0;
3042         inf->bmiColors[1].rgbRed = inf->bmiColors[1].rgbGreen = inf->bmiColors[1].rgbBlue = 0xff;
3043     }
3044
3045     if(FAILED(IStream_Write(pstm, data, totalSize, NULL)))
3046         goto failed;
3047
3048     result = TRUE;
3049
3050 failed:
3051     Free(data);
3052
3053     return result;
3054 }
3055
3056
3057 /*************************************************************************
3058  * ImageList_Write [COMCTL32.@]
3059  *
3060  * Writes an image list to a stream.
3061  *
3062  * PARAMS
3063  *     himl [I] handle to image list
3064  *     pstm [O] Pointer to a stream.
3065  *
3066  * RETURNS
3067  *     Success: TRUE
3068  *     Failure: FALSE
3069  *
3070  * BUGS
3071  *     probably.
3072  */
3073
3074 BOOL WINAPI
3075 ImageList_Write (HIMAGELIST himl, LPSTREAM pstm)
3076 {
3077     ILHEAD ilHead;
3078     int i;
3079
3080     TRACE("%p %p\n", himl, pstm);
3081
3082     if (!is_valid(himl))
3083         return FALSE;
3084
3085     ilHead.usMagic   = (('L' << 8) | 'I');
3086     ilHead.usVersion = 0x101;
3087     ilHead.cCurImage = himl->cCurImage;
3088     ilHead.cMaxImage = himl->cMaxImage;
3089     ilHead.cGrow     = himl->cGrow;
3090     ilHead.cx        = himl->cx;
3091     ilHead.cy        = himl->cy;
3092     ilHead.bkcolor   = himl->clrBk;
3093     ilHead.flags     = himl->flags;
3094     for(i = 0; i < 4; i++) {
3095         ilHead.ovls[i] = himl->nOvlIdx[i];
3096     }
3097
3098     TRACE("cx %u, cy %u, flags 0x04%x, cCurImage %u, cMaxImage %u\n",
3099           ilHead.cx, ilHead.cy, ilHead.flags, ilHead.cCurImage, ilHead.cMaxImage);
3100
3101     if(FAILED(IStream_Write(pstm, &ilHead, sizeof(ILHEAD), NULL)))
3102         return FALSE;
3103
3104     /* write the bitmap */
3105     if(!_write_bitmap(himl->hbmImage, pstm))
3106         return FALSE;
3107
3108     /* write the mask if we have one */
3109     if(himl->flags & ILC_MASK) {
3110         if(!_write_bitmap(himl->hbmMask, pstm))
3111             return FALSE;
3112     }
3113
3114     return TRUE;
3115 }
3116
3117
3118 static HBITMAP ImageList_CreateImage(HDC hdc, HIMAGELIST himl, UINT count)
3119 {
3120     HBITMAP hbmNewBitmap;
3121     UINT ilc = (himl->flags & 0xFE);
3122     SIZE sz;
3123
3124     imagelist_get_bitmap_size( himl, count, &sz );
3125
3126     if ((ilc >= ILC_COLOR4 && ilc <= ILC_COLOR32) || ilc == ILC_COLOR)
3127     {
3128         VOID* bits;
3129         BITMAPINFO *bmi;
3130
3131         TRACE("Creating DIBSection %d x %d, %d Bits per Pixel\n",
3132               sz.cx, sz.cy, himl->uBitsPixel);
3133
3134         if (himl->uBitsPixel <= ILC_COLOR8)
3135         {
3136             LPPALETTEENTRY pal;
3137             ULONG i, colors;
3138             BYTE temp;
3139
3140             colors = 1 << himl->uBitsPixel;
3141             bmi = Alloc(sizeof(BITMAPINFOHEADER) +
3142                             sizeof(PALETTEENTRY) * colors);
3143
3144             pal = (LPPALETTEENTRY)bmi->bmiColors;
3145             GetPaletteEntries(GetStockObject(DEFAULT_PALETTE), 0, colors, pal);
3146
3147             /* Swap colors returned by GetPaletteEntries so we can use them for
3148              * CreateDIBSection call. */
3149             for (i = 0; i < colors; i++)
3150             {
3151                 temp = pal[i].peBlue;
3152                 bmi->bmiColors[i].rgbRed = pal[i].peRed;
3153                 bmi->bmiColors[i].rgbBlue = temp;
3154             }
3155         }
3156         else
3157         {
3158             bmi = Alloc(sizeof(BITMAPINFOHEADER));
3159         }
3160
3161         bmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
3162         bmi->bmiHeader.biWidth = sz.cx;
3163         bmi->bmiHeader.biHeight = sz.cy;
3164         bmi->bmiHeader.biPlanes = 1;
3165         bmi->bmiHeader.biBitCount = himl->uBitsPixel;
3166         bmi->bmiHeader.biCompression = BI_RGB;
3167         bmi->bmiHeader.biSizeImage = 0;
3168         bmi->bmiHeader.biXPelsPerMeter = 0;
3169         bmi->bmiHeader.biYPelsPerMeter = 0;
3170         bmi->bmiHeader.biClrUsed = 0;
3171         bmi->bmiHeader.biClrImportant = 0;
3172
3173         hbmNewBitmap = CreateDIBSection(hdc, bmi, DIB_RGB_COLORS, &bits, 0, 0);
3174
3175         Free (bmi);
3176     }
3177     else /*if (ilc == ILC_COLORDDB)*/
3178     {
3179         TRACE("Creating Bitmap: %d Bits per Pixel\n", himl->uBitsPixel);
3180
3181         hbmNewBitmap = CreateBitmap (sz.cx, sz.cy, 1, himl->uBitsPixel, NULL);
3182     }
3183     TRACE("returning %p\n", hbmNewBitmap);
3184     return hbmNewBitmap;
3185 }
3186
3187 /*************************************************************************
3188  * ImageList_SetColorTable [COMCTL32.@]
3189  *
3190  * Sets the color table of an image list.
3191  *
3192  * PARAMS
3193  *     himl        [I] Handle to the image list.
3194  *     uStartIndex [I] The first index to set.
3195  *     cEntries    [I] Number of entries to set.
3196  *     prgb        [I] New color information for color table for the image list.
3197  *
3198  * RETURNS
3199  *     Success: Number of entries in the table that were set.
3200  *     Failure: Zero.
3201  *
3202  * SEE
3203  *     ImageList_Create(), SetDIBColorTable()
3204  */
3205
3206 UINT WINAPI
3207 ImageList_SetColorTable (HIMAGELIST himl, UINT uStartIndex, UINT cEntries, CONST RGBQUAD * prgb)
3208 {
3209     return SetDIBColorTable(himl->hdcImage, uStartIndex, cEntries, prgb);
3210 }
3211
3212 /*************************************************************************
3213  * ImageList_CoCreateInstance [COMCTL32.@]
3214  *
3215  * Creates a new imagelist instance and returns an interface pointer to it.
3216  *
3217  * PARAMS
3218  *     rclsid      [I] A reference to the CLSID (CLSID_ImageList).
3219  *     punkOuter   [I] Pointer to IUnknown interface for aggregation, if desired
3220  *     riid        [I] Identifier of the requested interface.
3221  *     ppv         [O] Returns the address of the pointer requested, or NULL.
3222  *
3223  * RETURNS
3224  *     Success: S_OK.
3225  *     Failure: Error value.
3226  */
3227 HRESULT WINAPI
3228 ImageList_CoCreateInstance (REFCLSID rclsid, const IUnknown *punkOuter, REFIID riid, void **ppv)
3229 {
3230     TRACE("(%s,%p,%s,%p)\n", debugstr_guid(rclsid), punkOuter, debugstr_guid(riid), ppv);
3231
3232     if (!IsEqualCLSID(&CLSID_ImageList, rclsid))
3233         return E_NOINTERFACE;
3234
3235     return ImageListImpl_CreateInstance(punkOuter, riid, ppv);
3236 }
3237
3238
3239 /*************************************************************************
3240  * IImageList implementation
3241  */
3242
3243 static HRESULT WINAPI ImageListImpl_QueryInterface(IImageList *iface,
3244     REFIID iid, void **ppv)
3245 {
3246     HIMAGELIST This = (HIMAGELIST) iface;
3247     TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
3248
3249     if (!ppv) return E_INVALIDARG;
3250
3251     if (IsEqualIID(&IID_IUnknown, iid) || IsEqualIID(&IID_IImageList, iid))
3252         *ppv = This;
3253     else
3254     {
3255         *ppv = NULL;
3256         return E_NOINTERFACE;
3257     }
3258
3259     IUnknown_AddRef((IUnknown*)*ppv);
3260     return S_OK;
3261 }
3262
3263 static ULONG WINAPI ImageListImpl_AddRef(IImageList *iface)
3264 {
3265     HIMAGELIST This = (HIMAGELIST) iface;
3266     ULONG ref = InterlockedIncrement(&This->ref);
3267
3268     TRACE("(%p) refcount=%u\n", iface, ref);
3269     return ref;
3270 }
3271
3272 static ULONG WINAPI ImageListImpl_Release(IImageList *iface)
3273 {
3274     HIMAGELIST This = (HIMAGELIST) iface;
3275     ULONG ref = InterlockedDecrement(&This->ref);
3276
3277     TRACE("(%p) refcount=%u\n", iface, ref);
3278
3279     if (ref == 0)
3280     {
3281         /* delete image bitmaps */
3282         if (This->hbmImage) DeleteObject (This->hbmImage);
3283         if (This->hbmMask)  DeleteObject (This->hbmMask);
3284
3285         /* delete image & mask DCs */
3286         if (This->hdcImage) DeleteDC (This->hdcImage);
3287         if (This->hdcMask)  DeleteDC (This->hdcMask);
3288
3289         /* delete blending brushes */
3290         if (This->hbrBlend25) DeleteObject (This->hbrBlend25);
3291         if (This->hbrBlend50) DeleteObject (This->hbrBlend50);
3292
3293         This->lpVtbl = NULL;
3294         HeapFree(GetProcessHeap(), 0, This->has_alpha);
3295         HeapFree(GetProcessHeap(), 0, This);
3296     }
3297
3298     return ref;
3299 }
3300
3301 static HRESULT WINAPI ImageListImpl_Add(IImageList *iface, HBITMAP hbmImage,
3302     HBITMAP hbmMask, int *pi)
3303 {
3304     HIMAGELIST This = (HIMAGELIST) iface;
3305     int ret;
3306
3307     if (!pi)
3308         return E_FAIL;
3309
3310     ret = ImageList_Add(This, hbmImage, hbmMask);
3311
3312     if (ret == -1)
3313         return E_FAIL;
3314
3315     *pi = ret;
3316     return S_OK;
3317 }
3318
3319 static HRESULT WINAPI ImageListImpl_ReplaceIcon(IImageList *iface, int i,
3320     HICON hicon, int *pi)
3321 {
3322     HIMAGELIST This = (HIMAGELIST) iface;
3323     int ret;
3324
3325     if (!pi)
3326         return E_FAIL;
3327
3328     ret = ImageList_ReplaceIcon(This, i, hicon);
3329
3330     if (ret == -1)
3331         return E_FAIL;
3332
3333     *pi = ret;
3334     return S_OK;
3335 }
3336
3337 static HRESULT WINAPI ImageListImpl_SetOverlayImage(IImageList *iface,
3338     int iImage, int iOverlay)
3339 {
3340     return ImageList_SetOverlayImage((HIMAGELIST) iface, iImage, iOverlay)
3341         ? S_OK : E_FAIL;
3342 }
3343
3344 static HRESULT WINAPI ImageListImpl_Replace(IImageList *iface, int i,
3345     HBITMAP hbmImage, HBITMAP hbmMask)
3346 {
3347     return ImageList_Replace((HIMAGELIST) iface, i, hbmImage, hbmMask) ? S_OK :
3348         E_FAIL;
3349 }
3350
3351 static HRESULT WINAPI ImageListImpl_AddMasked(IImageList *iface, HBITMAP hbmImage,
3352     COLORREF crMask, int *pi)
3353 {
3354     HIMAGELIST This = (HIMAGELIST) iface;
3355     int ret;
3356
3357     if (!pi)
3358         return E_FAIL;
3359
3360     ret = ImageList_AddMasked(This, hbmImage, crMask);
3361
3362     if (ret == -1)
3363         return E_FAIL;
3364
3365     *pi = ret;
3366     return S_OK;
3367 }
3368
3369 static HRESULT WINAPI ImageListImpl_Draw(IImageList *iface,
3370     IMAGELISTDRAWPARAMS *pimldp)
3371 {
3372     HIMAGELIST This = (HIMAGELIST) iface;
3373     HIMAGELIST old_himl;
3374     int ret;
3375
3376     /* As far as I can tell, Windows simply ignores the contents of pimldp->himl
3377        so we shall simulate the same */
3378     old_himl = pimldp->himl;
3379     pimldp->himl = This;
3380
3381     ret = ImageList_DrawIndirect(pimldp);
3382
3383     pimldp->himl = old_himl;
3384     return ret ? S_OK : E_INVALIDARG;
3385 }
3386
3387 static HRESULT WINAPI ImageListImpl_Remove(IImageList *iface, int i)
3388 {
3389     return (ImageList_Remove((HIMAGELIST) iface, i) == 0) ? E_INVALIDARG : S_OK;
3390 }
3391
3392 static HRESULT WINAPI ImageListImpl_GetIcon(IImageList *iface, int i, UINT flags,
3393     HICON *picon)
3394 {
3395     HICON hIcon;
3396
3397     if (!picon)
3398         return E_FAIL;
3399
3400     hIcon = ImageList_GetIcon((HIMAGELIST) iface, i, flags);
3401
3402     if (hIcon == NULL)
3403         return E_FAIL;
3404
3405     *picon = hIcon;
3406     return S_OK;
3407 }
3408
3409 static HRESULT WINAPI ImageListImpl_GetImageInfo(IImageList *iface, int i,
3410     IMAGEINFO *pImageInfo)
3411 {
3412     return ImageList_GetImageInfo((HIMAGELIST) iface, i, pImageInfo) ? S_OK : E_FAIL;
3413 }
3414
3415 static HRESULT WINAPI ImageListImpl_Copy(IImageList *iface, int iDst,
3416     IUnknown *punkSrc, int iSrc, UINT uFlags)
3417 {
3418     HIMAGELIST This = (HIMAGELIST) iface;
3419     IImageList *src = NULL;
3420     HRESULT ret;
3421
3422     if (!punkSrc)
3423         return E_FAIL;
3424
3425     /* TODO: Add test for IID_ImageList2 too */
3426     if (FAILED(IImageList_QueryInterface(punkSrc, &IID_IImageList,
3427             (void **) &src)))
3428         return E_FAIL;
3429
3430     if (ImageList_Copy(This, iDst, (HIMAGELIST) src, iSrc, uFlags))
3431         ret = S_OK;
3432     else
3433         ret = E_FAIL;
3434
3435     IImageList_Release(src);
3436     return ret;
3437 }
3438
3439 static HRESULT WINAPI ImageListImpl_Merge(IImageList *iface, int i1,
3440     IUnknown *punk2, int i2, int dx, int dy, REFIID riid, void **ppv)
3441 {
3442     HIMAGELIST This = (HIMAGELIST) iface;
3443     IImageList *iml2 = NULL;
3444     HIMAGELIST hNew;
3445     HRESULT ret = E_FAIL;
3446
3447     TRACE("(%p)->(%d %p %d %d %d %s %p)\n", iface, i1, punk2, i2, dx, dy, debugstr_guid(riid), ppv);
3448
3449     /* TODO: Add test for IID_ImageList2 too */
3450     if (FAILED(IImageList_QueryInterface(punk2, &IID_IImageList,
3451             (void **) &iml2)))
3452         return E_FAIL;
3453
3454     hNew = ImageList_Merge(This, i1, (HIMAGELIST) iml2, i2, dx, dy);
3455
3456     /* Get the interface for the new image list */
3457     if (hNew)
3458     {
3459         IImageList *imerge = (IImageList*)hNew;
3460
3461         ret = HIMAGELIST_QueryInterface(hNew, riid, ppv);
3462         IImageList_Release(imerge);
3463     }
3464
3465     IImageList_Release(iml2);
3466     return ret;
3467 }
3468
3469 static HRESULT WINAPI ImageListImpl_Clone(IImageList *iface, REFIID riid, void **ppv)
3470 {
3471     HIMAGELIST This = (HIMAGELIST) iface;
3472     HIMAGELIST clone;
3473     HRESULT ret = E_FAIL;
3474
3475     TRACE("(%p)->(%s %p)\n", iface, debugstr_guid(riid), ppv);
3476
3477     clone = ImageList_Duplicate(This);
3478
3479     /* Get the interface for the new image list */
3480     if (clone)
3481     {
3482         IImageList *iclone = (IImageList*)clone;
3483
3484         ret = HIMAGELIST_QueryInterface(clone, riid, ppv);
3485         IImageList_Release(iclone);
3486     }
3487
3488     return ret;
3489 }
3490
3491 static HRESULT WINAPI ImageListImpl_GetImageRect(IImageList *iface, int i,
3492     RECT *prc)
3493 {
3494     HIMAGELIST This = (HIMAGELIST) iface;
3495     IMAGEINFO info;
3496
3497     if (!prc)
3498         return E_FAIL;
3499
3500     if (!ImageList_GetImageInfo(This, i, &info))
3501         return E_FAIL;
3502
3503     return CopyRect(prc, &info.rcImage) ? S_OK : E_FAIL;
3504 }
3505
3506 static HRESULT WINAPI ImageListImpl_GetIconSize(IImageList *iface, int *cx,
3507     int *cy)
3508 {
3509     HIMAGELIST This = (HIMAGELIST) iface;
3510
3511     return ImageList_GetIconSize(This, cx, cy) ? S_OK : E_INVALIDARG;
3512 }
3513
3514 static HRESULT WINAPI ImageListImpl_SetIconSize(IImageList *iface, int cx,
3515     int cy)
3516 {
3517     return ImageList_SetIconSize((HIMAGELIST) iface, cx, cy) ? S_OK : E_FAIL;
3518 }
3519
3520 static HRESULT WINAPI ImageListImpl_GetImageCount(IImageList *iface, int *pi)
3521 {
3522     *pi = ImageList_GetImageCount((HIMAGELIST) iface);
3523     return S_OK;
3524 }
3525
3526 static HRESULT WINAPI ImageListImpl_SetImageCount(IImageList *iface,
3527     UINT uNewCount)
3528 {
3529     return ImageList_SetImageCount((HIMAGELIST) iface, uNewCount) ? S_OK : E_FAIL;
3530 }
3531
3532 static HRESULT WINAPI ImageListImpl_SetBkColor(IImageList *iface, COLORREF clrBk,
3533     COLORREF *pclr)
3534 {
3535     *pclr = ImageList_SetBkColor((HIMAGELIST) iface, clrBk);
3536     return S_OK;
3537 }
3538
3539 static HRESULT WINAPI ImageListImpl_GetBkColor(IImageList *iface, COLORREF *pclr)
3540 {
3541     *pclr = ImageList_GetBkColor((HIMAGELIST) iface);
3542     return S_OK;
3543 }
3544
3545 static HRESULT WINAPI ImageListImpl_BeginDrag(IImageList *iface, int iTrack,
3546     int dxHotspot, int dyHotspot)
3547 {
3548     return ImageList_BeginDrag((HIMAGELIST) iface, iTrack, dxHotspot, dyHotspot) ? S_OK : E_FAIL;
3549 }
3550
3551 static HRESULT WINAPI ImageListImpl_EndDrag(IImageList *iface)
3552 {
3553     ImageList_EndDrag();
3554     return S_OK;
3555 }
3556
3557 static HRESULT WINAPI ImageListImpl_DragEnter(IImageList *iface, HWND hwndLock,
3558     int x, int y)
3559 {
3560     return ImageList_DragEnter(hwndLock, x, y) ? S_OK : E_FAIL;
3561 }
3562
3563 static HRESULT WINAPI ImageListImpl_DragLeave(IImageList *iface, HWND hwndLock)
3564 {
3565     return ImageList_DragLeave(hwndLock) ? S_OK : E_FAIL;
3566 }
3567
3568 static HRESULT WINAPI ImageListImpl_DragMove(IImageList *iface, int x, int y)
3569 {
3570     return ImageList_DragMove(x, y) ? S_OK : E_FAIL;
3571 }
3572
3573 static HRESULT WINAPI ImageListImpl_SetDragCursorImage(IImageList *iface,
3574     IUnknown *punk, int iDrag, int dxHotspot, int dyHotspot)
3575 {
3576     IImageList *iml2 = NULL;
3577     HRESULT ret;
3578
3579     if (!punk)
3580         return E_FAIL;
3581
3582     /* TODO: Add test for IID_ImageList2 too */
3583     if (FAILED(IImageList_QueryInterface(punk, &IID_IImageList,
3584             (void **) &iml2)))
3585         return E_FAIL;
3586
3587     ret = ImageList_SetDragCursorImage((HIMAGELIST) iml2, iDrag, dxHotspot,
3588         dyHotspot);
3589
3590     IImageList_Release(iml2);
3591
3592     return ret ? S_OK : E_FAIL;
3593 }
3594
3595 static HRESULT WINAPI ImageListImpl_DragShowNolock(IImageList *iface, BOOL fShow)
3596 {
3597     return ImageList_DragShowNolock(fShow) ? S_OK : E_FAIL;
3598 }
3599
3600 static HRESULT WINAPI ImageListImpl_GetDragImage(IImageList *iface, POINT *ppt,
3601     POINT *pptHotspot, REFIID riid, PVOID *ppv)
3602 {
3603     HRESULT ret = E_FAIL;
3604     HIMAGELIST hNew;
3605
3606     if (!ppv)
3607         return E_FAIL;
3608
3609     hNew = ImageList_GetDragImage(ppt, pptHotspot);
3610
3611     /* Get the interface for the new image list */
3612     if (hNew)
3613     {
3614         IImageList *idrag = (IImageList*)hNew;
3615
3616         ret = HIMAGELIST_QueryInterface(hNew, riid, ppv);
3617         IImageList_Release(idrag);
3618     }
3619
3620     return ret;
3621 }
3622
3623 static HRESULT WINAPI ImageListImpl_GetItemFlags(IImageList *iface, int i,
3624     DWORD *dwFlags)
3625 {
3626     FIXME("STUB: %p %d %p\n", iface, i, dwFlags);
3627     return E_NOTIMPL;
3628 }
3629
3630 static HRESULT WINAPI ImageListImpl_GetOverlayImage(IImageList *iface, int iOverlay,
3631     int *piIndex)
3632 {
3633     HIMAGELIST This = (HIMAGELIST) iface;
3634     int i;
3635
3636     if ((iOverlay < 0) || (iOverlay > This->cCurImage))
3637         return E_FAIL;
3638
3639     for (i = 0; i < MAX_OVERLAYIMAGE; i++)
3640     {
3641         if (This->nOvlIdx[i] == iOverlay)
3642         {
3643             *piIndex = i + 1;
3644             return S_OK;
3645         }
3646     }
3647
3648     return E_FAIL;
3649 }
3650
3651
3652 static const IImageListVtbl ImageListImpl_Vtbl = {
3653     ImageListImpl_QueryInterface,
3654     ImageListImpl_AddRef,
3655     ImageListImpl_Release,
3656     ImageListImpl_Add,
3657     ImageListImpl_ReplaceIcon,
3658     ImageListImpl_SetOverlayImage,
3659     ImageListImpl_Replace,
3660     ImageListImpl_AddMasked,
3661     ImageListImpl_Draw,
3662     ImageListImpl_Remove,
3663     ImageListImpl_GetIcon,
3664     ImageListImpl_GetImageInfo,
3665     ImageListImpl_Copy,
3666     ImageListImpl_Merge,
3667     ImageListImpl_Clone,
3668     ImageListImpl_GetImageRect,
3669     ImageListImpl_GetIconSize,
3670     ImageListImpl_SetIconSize,
3671     ImageListImpl_GetImageCount,
3672     ImageListImpl_SetImageCount,
3673     ImageListImpl_SetBkColor,
3674     ImageListImpl_GetBkColor,
3675     ImageListImpl_BeginDrag,
3676     ImageListImpl_EndDrag,
3677     ImageListImpl_DragEnter,
3678     ImageListImpl_DragLeave,
3679     ImageListImpl_DragMove,
3680     ImageListImpl_SetDragCursorImage,
3681     ImageListImpl_DragShowNolock,
3682     ImageListImpl_GetDragImage,
3683     ImageListImpl_GetItemFlags,
3684     ImageListImpl_GetOverlayImage
3685 };
3686
3687 static BOOL is_valid(HIMAGELIST himl)
3688 {
3689     BOOL valid;
3690     __TRY
3691     {
3692         valid = himl && himl->lpVtbl == &ImageListImpl_Vtbl;
3693     }
3694     __EXCEPT_PAGE_FAULT
3695     {
3696         valid = FALSE;
3697     }
3698     __ENDTRY
3699     return valid;
3700 }
3701
3702 /*************************************************************************
3703  * HIMAGELIST_QueryInterface [COMCTL32.@]
3704  *
3705  * Returns a pointer to an IImageList or IImageList2 object for the given
3706  * HIMAGELIST.
3707  *
3708  * PARAMS
3709  *     himl        [I] Image list handle.
3710  *     riid        [I] Identifier of the requested interface.
3711  *     ppv         [O] Returns the address of the pointer requested, or NULL.
3712  *
3713  * RETURNS
3714  *     Success: S_OK.
3715  *     Failure: Error value.
3716  */
3717 HRESULT WINAPI
3718 HIMAGELIST_QueryInterface (HIMAGELIST himl, REFIID riid, void **ppv)
3719 {
3720     TRACE("(%p,%s,%p)\n", himl, debugstr_guid(riid), ppv);
3721     return IImageList_QueryInterface((IImageList *) himl, riid, ppv);
3722 }
3723
3724 static HRESULT ImageListImpl_CreateInstance(const IUnknown *pUnkOuter, REFIID iid, void** ppv)
3725 {
3726     HIMAGELIST This;
3727     HRESULT ret;
3728
3729     TRACE("(%p,%s,%p)\n", pUnkOuter, debugstr_guid(iid), ppv);
3730
3731     *ppv = NULL;
3732
3733     if (pUnkOuter) return CLASS_E_NOAGGREGATION;
3734
3735     This = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct _IMAGELIST));
3736     if (!This) return E_OUTOFMEMORY;
3737
3738     This->lpVtbl = &ImageListImpl_Vtbl;
3739     This->ref = 1;
3740
3741     ret = IUnknown_QueryInterface((IUnknown*)This, iid, ppv);
3742     IUnknown_Release((IUnknown*)This);
3743
3744     return ret;
3745 }