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