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