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