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