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