gdiplus/tests: Check more return values (Coverity).
[wine.git] / dlls / comctl32 / imagelist.c
blob70457c78f538537041f239f2157a0f5b6efd5c7f
1 /*
2 * ImageList implementation
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
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.
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.
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
25 * NOTE
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.
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.
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
40 #include <stdarg.h>
41 #include <stdlib.h>
42 #include <string.h>
44 #define COBJMACROS
46 #include "winerror.h"
47 #include "windef.h"
48 #include "winbase.h"
49 #include "objbase.h"
50 #include "wingdi.h"
51 #include "winuser.h"
52 #include "commctrl.h"
53 #include "comctl32.h"
54 #include "commoncontrols.h"
55 #include "wine/debug.h"
56 #include "wine/exception.h"
58 WINE_DEFAULT_DEBUG_CHANNEL(imagelist);
60 #define MAX_OVERLAYIMAGE 15
62 struct _IMAGELIST
64 IImageList2 IImageList2_iface; /* 00: IImageList vtable */
65 INT cCurImage; /* 04: ImageCount */
66 INT cMaxImage; /* 08: maximages */
67 INT cGrow; /* 0C: cGrow */
68 INT cx; /* 10: cx */
69 INT cy; /* 14: cy */
70 DWORD x4;
71 UINT flags; /* 1C: flags */
72 COLORREF clrFg; /* 20: foreground color */
73 COLORREF clrBk; /* 24: background color */
76 HBITMAP hbmImage; /* 28: images Bitmap */
77 HBITMAP hbmMask; /* 2C: masks Bitmap */
78 HDC hdcImage; /* 30: images MemDC */
79 HDC hdcMask; /* 34: masks MemDC */
80 INT nOvlIdx[MAX_OVERLAYIMAGE]; /* 38: overlay images index */
82 /* not yet found out */
83 HBRUSH hbrBlend25;
84 HBRUSH hbrBlend50;
85 INT cInitial;
86 UINT uBitsPixel;
87 char *has_alpha;
88 BOOL color_table_set;
90 LONG ref; /* reference count */
93 #define IMAGELIST_MAGIC 0x53414D58
95 /* Header used by ImageList_Read() and ImageList_Write() */
96 #include "pshpack2.h"
97 typedef struct _ILHEAD
99 USHORT usMagic;
100 USHORT usVersion;
101 WORD cCurImage;
102 WORD cMaxImage;
103 WORD cGrow;
104 WORD cx;
105 WORD cy;
106 COLORREF bkcolor;
107 WORD flags;
108 SHORT ovls[4];
109 } ILHEAD;
110 #include "poppack.h"
112 /* internal image list data used for Drag & Drop operations */
113 typedef struct
115 HWND hwnd;
116 HIMAGELIST himl;
117 HIMAGELIST himlNoCursor;
118 /* position of the drag image relative to the window */
119 INT x;
120 INT y;
121 /* offset of the hotspot relative to the origin of the image */
122 INT dxHotspot;
123 INT dyHotspot;
124 /* is the drag image visible */
125 BOOL bShow;
126 /* saved background */
127 HBITMAP hbmBg;
128 } INTERNALDRAG;
130 static INTERNALDRAG InternalDrag = { 0, 0, 0, 0, 0, 0, 0, FALSE, 0 };
132 static inline HIMAGELIST impl_from_IImageList2(IImageList2 *iface)
134 return CONTAINING_RECORD(iface, struct _IMAGELIST, IImageList2_iface);
137 static HBITMAP ImageList_CreateImage(HDC hdc, HIMAGELIST himl, UINT count);
138 static HRESULT ImageListImpl_CreateInstance(const IUnknown *pUnkOuter, REFIID iid, void** ppv);
139 static BOOL is_valid(HIMAGELIST himl);
142 * An imagelist with N images is tiled like this:
144 * N/4 ->
146 * 4 048C..
147 * 159D..
148 * | 26AE.N
149 * V 37BF.
152 #define TILE_COUNT 4
154 static inline UINT imagelist_height( UINT count )
156 return ((count + TILE_COUNT - 1)/TILE_COUNT);
159 static inline void imagelist_point_from_index( HIMAGELIST himl, UINT index, LPPOINT pt )
161 pt->x = (index%TILE_COUNT) * himl->cx;
162 pt->y = (index/TILE_COUNT) * himl->cy;
165 static inline void imagelist_get_bitmap_size( HIMAGELIST himl, UINT count, SIZE *sz )
167 sz->cx = himl->cx * TILE_COUNT;
168 sz->cy = imagelist_height( count ) * himl->cy;
171 static inline int get_dib_stride( int width, int bpp )
173 return ((width * bpp + 31) >> 3) & ~3;
176 static inline int get_dib_image_size( const BITMAPINFO *info )
178 return get_dib_stride( info->bmiHeader.biWidth, info->bmiHeader.biBitCount )
179 * abs( info->bmiHeader.biHeight );
183 * imagelist_copy_images()
185 * Copies a block of count images from offset src in the list to offset dest.
186 * Images are copied a row at at time. Assumes hdcSrc and hdcDest are different.
188 static inline void imagelist_copy_images( HIMAGELIST himl, HDC hdcSrc, HDC hdcDest,
189 UINT src, UINT count, UINT dest )
191 POINT ptSrc, ptDest;
192 SIZE sz;
193 UINT i;
195 for ( i=0; i<TILE_COUNT; i++ )
197 imagelist_point_from_index( himl, src+i, &ptSrc );
198 imagelist_point_from_index( himl, dest+i, &ptDest );
199 sz.cx = himl->cx;
200 sz.cy = himl->cy * imagelist_height( count - i );
202 BitBlt( hdcDest, ptDest.x, ptDest.y, sz.cx, sz.cy,
203 hdcSrc, ptSrc.x, ptSrc.y, SRCCOPY );
207 static void add_dib_bits( HIMAGELIST himl, int pos, int count, int width, int height,
208 BITMAPINFO *info, BITMAPINFO *mask_info, DWORD *bits, BYTE *mask_bits )
210 int i, j, n;
211 POINT pt;
212 int stride = info->bmiHeader.biWidth;
213 int mask_stride = (info->bmiHeader.biWidth + 31) / 32 * 4;
215 for (n = 0; n < count; n++)
217 BOOL has_alpha = FALSE;
219 imagelist_point_from_index( himl, pos + n, &pt );
221 /* check if bitmap has an alpha channel */
222 for (i = 0; i < height && !has_alpha; i++)
223 for (j = n * width; j < (n + 1) * width; j++)
224 if ((has_alpha = ((bits[i * stride + j] & 0xff000000) != 0))) break;
226 if (!has_alpha) /* generate alpha channel from the mask */
228 for (i = 0; i < height; i++)
229 for (j = n * width; j < (n + 1) * width; j++)
230 if (!mask_info || !((mask_bits[i * mask_stride + j / 8] << (j % 8)) & 0x80))
231 bits[i * stride + j] |= 0xff000000;
232 else
233 bits[i * stride + j] = 0;
235 else
237 himl->has_alpha[pos + n] = 1;
239 if (mask_info && himl->hbmMask) /* generate the mask from the alpha channel */
241 for (i = 0; i < height; i++)
242 for (j = n * width; j < (n + 1) * width; j++)
243 if ((bits[i * stride + j] >> 24) > 25) /* more than 10% alpha */
244 mask_bits[i * mask_stride + j / 8] &= ~(0x80 >> (j % 8));
245 else
246 mask_bits[i * mask_stride + j / 8] |= 0x80 >> (j % 8);
249 StretchDIBits( himl->hdcImage, pt.x, pt.y, himl->cx, himl->cy,
250 n * width, 0, width, height, bits, info, DIB_RGB_COLORS, SRCCOPY );
251 if (mask_info)
252 StretchDIBits( himl->hdcMask, pt.x, pt.y, himl->cx, himl->cy,
253 n * width, 0, width, height, mask_bits, mask_info, DIB_RGB_COLORS, SRCCOPY );
257 /* add images with an alpha channel when the image list is 32 bpp */
258 static BOOL add_with_alpha( HIMAGELIST himl, HDC hdc, int pos, int count,
259 int width, int height, HBITMAP hbmImage, HBITMAP hbmMask )
261 BOOL ret = FALSE;
262 BITMAP bm;
263 BITMAPINFO *info, *mask_info = NULL;
264 DWORD *bits = NULL;
265 BYTE *mask_bits = NULL;
266 DWORD mask_width;
268 if (!GetObjectW( hbmImage, sizeof(bm), &bm )) return FALSE;
270 /* if either the imagelist or the source bitmap don't have an alpha channel, bail out now */
271 if (!himl->has_alpha) return FALSE;
272 if (bm.bmBitsPixel != 32) return FALSE;
274 SelectObject( hdc, hbmImage );
275 mask_width = (bm.bmWidth + 31) / 32 * 4;
277 if (!(info = HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET( BITMAPINFO, bmiColors[256] )))) goto done;
278 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
279 info->bmiHeader.biWidth = bm.bmWidth;
280 info->bmiHeader.biHeight = -height;
281 info->bmiHeader.biPlanes = 1;
282 info->bmiHeader.biBitCount = 32;
283 info->bmiHeader.biCompression = BI_RGB;
284 info->bmiHeader.biSizeImage = bm.bmWidth * height * 4;
285 info->bmiHeader.biXPelsPerMeter = 0;
286 info->bmiHeader.biYPelsPerMeter = 0;
287 info->bmiHeader.biClrUsed = 0;
288 info->bmiHeader.biClrImportant = 0;
289 if (!(bits = HeapAlloc( GetProcessHeap(), 0, info->bmiHeader.biSizeImage ))) goto done;
290 if (!GetDIBits( hdc, hbmImage, 0, height, bits, info, DIB_RGB_COLORS )) goto done;
292 if (hbmMask)
294 if (!(mask_info = HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET( BITMAPINFO, bmiColors[2] ))))
295 goto done;
296 mask_info->bmiHeader = info->bmiHeader;
297 mask_info->bmiHeader.biBitCount = 1;
298 mask_info->bmiHeader.biSizeImage = mask_width * height;
299 if (!(mask_bits = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, info->bmiHeader.biSizeImage )))
300 goto done;
301 if (!GetDIBits( hdc, hbmMask, 0, height, mask_bits, mask_info, DIB_RGB_COLORS )) goto done;
304 add_dib_bits( himl, pos, count, width, height, info, mask_info, bits, mask_bits );
305 ret = TRUE;
307 done:
308 HeapFree( GetProcessHeap(), 0, info );
309 HeapFree( GetProcessHeap(), 0, mask_info );
310 HeapFree( GetProcessHeap(), 0, bits );
311 HeapFree( GetProcessHeap(), 0, mask_bits );
312 return ret;
315 UINT WINAPI
316 ImageList_SetColorTable(HIMAGELIST himl, UINT uStartIndex, UINT cEntries, const RGBQUAD *prgb);
318 /*************************************************************************
319 * IMAGELIST_InternalExpandBitmaps [Internal]
321 * Expands the bitmaps of an image list by the given number of images.
323 * PARAMS
324 * himl [I] handle to image list
325 * nImageCount [I] number of images to add
327 * RETURNS
328 * nothing
330 * NOTES
331 * This function CANNOT be used to reduce the number of images.
333 static void
334 IMAGELIST_InternalExpandBitmaps(HIMAGELIST himl, INT nImageCount)
336 HDC hdcBitmap;
337 HBITMAP hbmNewBitmap, hbmNull;
338 INT nNewCount;
339 SIZE sz;
341 TRACE("%p has allocated %d, max %d, grow %d images\n", himl, himl->cCurImage, himl->cMaxImage, himl->cGrow);
343 if (himl->cCurImage + nImageCount < himl->cMaxImage)
344 return;
346 nNewCount = himl->cMaxImage + max(nImageCount, himl->cGrow) + 1;
348 imagelist_get_bitmap_size(himl, nNewCount, &sz);
350 TRACE("Create expanded bitmaps : himl=%p x=%d y=%d count=%d\n", himl, sz.cx, sz.cy, nNewCount);
351 hdcBitmap = CreateCompatibleDC (0);
353 hbmNewBitmap = ImageList_CreateImage(hdcBitmap, himl, nNewCount);
355 if (hbmNewBitmap == 0)
356 ERR("creating new image bitmap (x=%d y=%d)!\n", sz.cx, sz.cy);
358 if (himl->cCurImage)
360 hbmNull = SelectObject (hdcBitmap, hbmNewBitmap);
361 BitBlt (hdcBitmap, 0, 0, sz.cx, sz.cy,
362 himl->hdcImage, 0, 0, SRCCOPY);
363 SelectObject (hdcBitmap, hbmNull);
365 SelectObject (himl->hdcImage, hbmNewBitmap);
366 DeleteObject (himl->hbmImage);
367 himl->hbmImage = hbmNewBitmap;
369 if (himl->flags & ILC_MASK)
371 hbmNewBitmap = CreateBitmap (sz.cx, sz.cy, 1, 1, NULL);
373 if (hbmNewBitmap == 0)
374 ERR("creating new mask bitmap!\n");
376 if(himl->cCurImage)
378 hbmNull = SelectObject (hdcBitmap, hbmNewBitmap);
379 BitBlt (hdcBitmap, 0, 0, sz.cx, sz.cy,
380 himl->hdcMask, 0, 0, SRCCOPY);
381 SelectObject (hdcBitmap, hbmNull);
383 SelectObject (himl->hdcMask, hbmNewBitmap);
384 DeleteObject (himl->hbmMask);
385 himl->hbmMask = hbmNewBitmap;
388 if (himl->has_alpha)
390 char *new_alpha = HeapReAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, himl->has_alpha, nNewCount );
391 if (new_alpha) himl->has_alpha = new_alpha;
392 else
394 HeapFree( GetProcessHeap(), 0, himl->has_alpha );
395 himl->has_alpha = NULL;
399 himl->cMaxImage = nNewCount;
401 DeleteDC (hdcBitmap);
405 /*************************************************************************
406 * ImageList_Add [COMCTL32.@]
408 * Add an image or images to an image list.
410 * PARAMS
411 * himl [I] handle to image list
412 * hbmImage [I] handle to image bitmap
413 * hbmMask [I] handle to mask bitmap
415 * RETURNS
416 * Success: Index of the first new image.
417 * Failure: -1
420 INT WINAPI
421 ImageList_Add (HIMAGELIST himl, HBITMAP hbmImage, HBITMAP hbmMask)
423 HDC hdcBitmap, hdcTemp = 0;
424 INT nFirstIndex, nImageCount, i;
425 BITMAP bmp;
426 POINT pt;
428 TRACE("himl=%p hbmimage=%p hbmmask=%p\n", himl, hbmImage, hbmMask);
429 if (!is_valid(himl))
430 return -1;
432 if (!GetObjectW(hbmImage, sizeof(BITMAP), &bmp))
433 return -1;
435 TRACE("himl %p, cCurImage %d, cMaxImage %d, cGrow %d, cx %d, cy %d\n",
436 himl, himl->cCurImage, himl->cMaxImage, himl->cGrow, himl->cx, himl->cy);
438 nImageCount = bmp.bmWidth / himl->cx;
440 TRACE("%p has %d images (%d x %d) bpp %d\n", hbmImage, nImageCount, bmp.bmWidth, bmp.bmHeight,
441 bmp.bmBitsPixel);
443 IMAGELIST_InternalExpandBitmaps(himl, nImageCount);
445 hdcBitmap = CreateCompatibleDC(0);
447 SelectObject(hdcBitmap, hbmImage);
449 if (add_with_alpha( himl, hdcBitmap, himl->cCurImage, nImageCount,
450 himl->cx, min( himl->cy, bmp.bmHeight), hbmImage, hbmMask ))
451 goto done;
453 if (himl->hbmMask)
455 hdcTemp = CreateCompatibleDC(0);
456 SelectObject(hdcTemp, hbmMask);
459 if (himl->uBitsPixel <= 8 && bmp.bmBitsPixel <= 8 &&
460 !himl->color_table_set && himl->cCurImage == 0)
462 RGBQUAD colors[256];
463 UINT num = GetDIBColorTable( hdcBitmap, 0, 1 << bmp.bmBitsPixel, colors );
464 if (num) ImageList_SetColorTable( himl, 0, num, colors );
467 for (i=0; i<nImageCount; i++)
469 imagelist_point_from_index( himl, himl->cCurImage + i, &pt );
471 /* Copy result to the imagelist
473 BitBlt( himl->hdcImage, pt.x, pt.y, himl->cx, bmp.bmHeight,
474 hdcBitmap, i*himl->cx, 0, SRCCOPY );
476 if (!himl->hbmMask)
477 continue;
479 BitBlt( himl->hdcMask, pt.x, pt.y, himl->cx, bmp.bmHeight,
480 hdcTemp, i*himl->cx, 0, SRCCOPY );
482 /* Remove the background from the image
484 BitBlt( himl->hdcImage, pt.x, pt.y, himl->cx, bmp.bmHeight,
485 himl->hdcMask, pt.x, pt.y, 0x220326 ); /* NOTSRCAND */
487 if (hdcTemp) DeleteDC(hdcTemp);
489 done:
490 DeleteDC(hdcBitmap);
492 nFirstIndex = himl->cCurImage;
493 himl->cCurImage += nImageCount;
495 return nFirstIndex;
499 /*************************************************************************
500 * ImageList_AddIcon [COMCTL32.@]
502 * Adds an icon to an image list.
504 * PARAMS
505 * himl [I] handle to image list
506 * hIcon [I] handle to icon
508 * RETURNS
509 * Success: index of the new image
510 * Failure: -1
512 #undef ImageList_AddIcon
513 INT WINAPI ImageList_AddIcon (HIMAGELIST himl, HICON hIcon)
515 return ImageList_ReplaceIcon (himl, -1, hIcon);
519 /*************************************************************************
520 * ImageList_AddMasked [COMCTL32.@]
522 * Adds an image or images to an image list and creates a mask from the
523 * specified bitmap using the mask color.
525 * PARAMS
526 * himl [I] handle to image list.
527 * hBitmap [I] handle to bitmap
528 * clrMask [I] mask color.
530 * RETURNS
531 * Success: Index of the first new image.
532 * Failure: -1
535 INT WINAPI
536 ImageList_AddMasked (HIMAGELIST himl, HBITMAP hBitmap, COLORREF clrMask)
538 HDC hdcMask, hdcBitmap;
539 INT ret;
540 BITMAP bmp;
541 HBITMAP hMaskBitmap;
542 COLORREF bkColor;
544 TRACE("himl=%p hbitmap=%p clrmask=%x\n", himl, hBitmap, clrMask);
545 if (!is_valid(himl))
546 return -1;
548 if (!GetObjectW(hBitmap, sizeof(BITMAP), &bmp))
549 return -1;
551 hdcBitmap = CreateCompatibleDC(0);
552 SelectObject(hdcBitmap, hBitmap);
554 /* Create a temp Mask so we can remove the background of the Image */
555 hdcMask = CreateCompatibleDC(0);
556 hMaskBitmap = CreateBitmap(bmp.bmWidth, bmp.bmHeight, 1, 1, NULL);
557 SelectObject(hdcMask, hMaskBitmap);
559 /* create monochrome image to the mask bitmap */
560 bkColor = (clrMask != CLR_DEFAULT) ? clrMask : GetPixel (hdcBitmap, 0, 0);
561 SetBkColor (hdcBitmap, bkColor);
562 BitBlt (hdcMask, 0, 0, bmp.bmWidth, bmp.bmHeight, hdcBitmap, 0, 0, SRCCOPY);
565 * Remove the background from the image
567 * WINDOWS BUG ALERT!!!!!!
568 * The statement below should not be done in common practice
569 * but this is how ImageList_AddMasked works in Windows.
570 * It overwrites the original bitmap passed, this was discovered
571 * by using the same bitmap to iterate the different styles
572 * on windows where it failed (BUT ImageList_Add is OK)
573 * This is here in case some apps rely on this bug
575 * Blt mode 0x220326 is NOTSRCAND
577 if (bmp.bmBitsPixel > 8) /* NOTSRCAND can't work with palettes */
579 SetBkColor(hdcBitmap, RGB(255,255,255));
580 BitBlt(hdcBitmap, 0, 0, bmp.bmWidth, bmp.bmHeight, hdcMask, 0, 0, 0x220326);
583 DeleteDC(hdcBitmap);
584 DeleteDC(hdcMask);
586 ret = ImageList_Add( himl, hBitmap, hMaskBitmap );
588 DeleteObject(hMaskBitmap);
589 return ret;
593 /*************************************************************************
594 * ImageList_BeginDrag [COMCTL32.@]
596 * Creates a temporary image list that contains one image. It will be used
597 * as a drag image.
599 * PARAMS
600 * himlTrack [I] handle to the source image list
601 * iTrack [I] index of the drag image in the source image list
602 * dxHotspot [I] X position of the hot spot of the drag image
603 * dyHotspot [I] Y position of the hot spot of the drag image
605 * RETURNS
606 * Success: TRUE
607 * Failure: FALSE
610 BOOL WINAPI
611 ImageList_BeginDrag (HIMAGELIST himlTrack, INT iTrack,
612 INT dxHotspot, INT dyHotspot)
614 INT cx, cy;
615 POINT src, dst;
617 TRACE("(himlTrack=%p iTrack=%d dx=%d dy=%d)\n", himlTrack, iTrack,
618 dxHotspot, dyHotspot);
620 if (!is_valid(himlTrack))
621 return FALSE;
623 if (iTrack >= himlTrack->cCurImage)
624 return FALSE;
626 if (InternalDrag.himl)
627 return FALSE;
629 cx = himlTrack->cx;
630 cy = himlTrack->cy;
632 InternalDrag.himlNoCursor = InternalDrag.himl = ImageList_Create (cx, cy, himlTrack->flags, 1, 1);
633 if (InternalDrag.himl == NULL) {
634 WARN("Error creating drag image list!\n");
635 return FALSE;
638 InternalDrag.dxHotspot = dxHotspot;
639 InternalDrag.dyHotspot = dyHotspot;
641 /* copy image */
642 imagelist_point_from_index(InternalDrag.himl, 0, &dst);
643 imagelist_point_from_index(himlTrack, iTrack, &src);
644 BitBlt(InternalDrag.himl->hdcImage, dst.x, dst.y, cx, cy, himlTrack->hdcImage, src.x, src.y,
645 SRCCOPY);
646 BitBlt(InternalDrag.himl->hdcMask, dst.x, dst.y, cx, cy, himlTrack->hdcMask, src.x, src.y,
647 SRCCOPY);
649 InternalDrag.himl->cCurImage = 1;
651 return TRUE;
655 /*************************************************************************
656 * ImageList_Copy [COMCTL32.@]
658 * Copies an image of the source image list to an image of the
659 * destination image list. Images can be copied or swapped.
661 * PARAMS
662 * himlDst [I] handle to the destination image list
663 * iDst [I] destination image index.
664 * himlSrc [I] handle to the source image list
665 * iSrc [I] source image index
666 * uFlags [I] flags for the copy operation
668 * RETURNS
669 * Success: TRUE
670 * Failure: FALSE
672 * NOTES
673 * Copying from one image list to another is possible. The original
674 * implementation just copies or swaps within one image list.
675 * Could this feature become a bug??? ;-)
678 BOOL WINAPI
679 ImageList_Copy (HIMAGELIST himlDst, INT iDst, HIMAGELIST himlSrc,
680 INT iSrc, UINT uFlags)
682 POINT ptSrc, ptDst;
684 TRACE("himlDst=%p iDst=%d himlSrc=%p iSrc=%d\n", himlDst, iDst, himlSrc, iSrc);
686 if (!is_valid(himlSrc) || !is_valid(himlDst))
687 return FALSE;
688 if ((iDst < 0) || (iDst >= himlDst->cCurImage))
689 return FALSE;
690 if ((iSrc < 0) || (iSrc >= himlSrc->cCurImage))
691 return FALSE;
693 imagelist_point_from_index( himlDst, iDst, &ptDst );
694 imagelist_point_from_index( himlSrc, iSrc, &ptSrc );
696 if (uFlags & ILCF_SWAP) {
697 /* swap */
698 HDC hdcBmp;
699 HBITMAP hbmTempImage, hbmTempMask;
701 hdcBmp = CreateCompatibleDC (0);
703 /* create temporary bitmaps */
704 hbmTempImage = CreateBitmap (himlSrc->cx, himlSrc->cy, 1,
705 himlSrc->uBitsPixel, NULL);
706 hbmTempMask = CreateBitmap (himlSrc->cx, himlSrc->cy, 1,
707 1, NULL);
709 /* copy (and stretch) destination to temporary bitmaps.(save) */
710 /* image */
711 SelectObject (hdcBmp, hbmTempImage);
712 StretchBlt (hdcBmp, 0, 0, himlSrc->cx, himlSrc->cy,
713 himlDst->hdcImage, ptDst.x, ptDst.y, himlDst->cx, himlDst->cy,
714 SRCCOPY);
715 /* mask */
716 SelectObject (hdcBmp, hbmTempMask);
717 StretchBlt (hdcBmp, 0, 0, himlSrc->cx, himlSrc->cy,
718 himlDst->hdcMask, ptDst.x, ptDst.y, himlDst->cx, himlDst->cy,
719 SRCCOPY);
721 /* copy (and stretch) source to destination */
722 /* image */
723 StretchBlt (himlDst->hdcImage, ptDst.x, ptDst.y, himlDst->cx, himlDst->cy,
724 himlSrc->hdcImage, ptSrc.x, ptSrc.y, himlSrc->cx, himlSrc->cy,
725 SRCCOPY);
726 /* mask */
727 StretchBlt (himlDst->hdcMask, ptDst.x, ptDst.y, himlDst->cx, himlDst->cy,
728 himlSrc->hdcMask, ptSrc.x, ptSrc.y, himlSrc->cx, himlSrc->cy,
729 SRCCOPY);
731 /* copy (without stretching) temporary bitmaps to source (restore) */
732 /* mask */
733 BitBlt (himlSrc->hdcMask, ptSrc.x, ptSrc.y, himlSrc->cx, himlSrc->cy,
734 hdcBmp, 0, 0, SRCCOPY);
736 /* image */
737 BitBlt (himlSrc->hdcImage, ptSrc.x, ptSrc.y, himlSrc->cx, himlSrc->cy,
738 hdcBmp, 0, 0, SRCCOPY);
739 /* delete temporary bitmaps */
740 DeleteObject (hbmTempMask);
741 DeleteObject (hbmTempImage);
742 DeleteDC(hdcBmp);
744 else {
745 /* copy image */
746 StretchBlt (himlDst->hdcImage, ptDst.x, ptDst.y, himlDst->cx, himlDst->cy,
747 himlSrc->hdcImage, ptSrc.x, ptSrc.y, himlSrc->cx, himlSrc->cy,
748 SRCCOPY);
750 /* copy mask */
751 StretchBlt (himlDst->hdcMask, ptDst.x, ptDst.y, himlDst->cx, himlDst->cy,
752 himlSrc->hdcMask, ptSrc.x, ptSrc.y, himlSrc->cx, himlSrc->cy,
753 SRCCOPY);
756 return TRUE;
760 /*************************************************************************
761 * ImageList_Create [COMCTL32.@]
763 * Creates a new image list.
765 * PARAMS
766 * cx [I] image height
767 * cy [I] image width
768 * flags [I] creation flags
769 * cInitial [I] initial number of images in the image list
770 * cGrow [I] number of images by which image list grows
772 * RETURNS
773 * Success: Handle to the created image list
774 * Failure: NULL
776 HIMAGELIST WINAPI
777 ImageList_Create (INT cx, INT cy, UINT flags,
778 INT cInitial, INT cGrow)
780 HIMAGELIST himl;
781 INT nCount;
782 HBITMAP hbmTemp;
783 UINT ilc = (flags & 0xFE);
784 static const WORD aBitBlend25[] =
785 {0xAA, 0x00, 0x55, 0x00, 0xAA, 0x00, 0x55, 0x00};
787 static const WORD aBitBlend50[] =
788 {0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA};
790 TRACE("(%d %d 0x%x %d %d)\n", cx, cy, flags, cInitial, cGrow);
792 if (cx < 0 || cy < 0) return NULL;
793 if (!((flags&ILC_COLORDDB) == ILC_COLORDDB) && (cx == 0 || cy == 0)) return NULL;
795 /* Create the IImageList interface for the image list */
796 if (FAILED(ImageListImpl_CreateInstance(NULL, &IID_IImageList, (void **)&himl)))
797 return NULL;
799 cGrow = (WORD)((max( cGrow, 1 ) + 3) & ~3);
801 if (cGrow > 256)
803 /* Windows doesn't limit the size here, but X11 doesn't let us allocate such huge bitmaps */
804 WARN( "grow %d too large, limiting to 256\n", cGrow );
805 cGrow = 256;
808 himl->cx = cx;
809 himl->cy = cy;
810 himl->flags = flags;
811 himl->cMaxImage = cInitial + 1;
812 himl->cInitial = cInitial;
813 himl->cGrow = cGrow;
814 himl->clrFg = CLR_DEFAULT;
815 himl->clrBk = CLR_NONE;
816 himl->color_table_set = FALSE;
818 /* initialize overlay mask indices */
819 for (nCount = 0; nCount < MAX_OVERLAYIMAGE; nCount++)
820 himl->nOvlIdx[nCount] = -1;
822 /* Create Image & Mask DCs */
823 himl->hdcImage = CreateCompatibleDC (0);
824 if (!himl->hdcImage)
825 goto cleanup;
826 if (himl->flags & ILC_MASK){
827 himl->hdcMask = CreateCompatibleDC(0);
828 if (!himl->hdcMask)
829 goto cleanup;
832 /* Default to ILC_COLOR4 if none of the ILC_COLOR* flags are specified */
833 if (ilc == ILC_COLOR)
835 ilc = ILC_COLOR4;
836 himl->flags |= ILC_COLOR4;
839 if (ilc >= ILC_COLOR4 && ilc <= ILC_COLOR32)
840 himl->uBitsPixel = ilc;
841 else
842 himl->uBitsPixel = (UINT)GetDeviceCaps (himl->hdcImage, BITSPIXEL);
844 if (himl->cMaxImage > 0) {
845 himl->hbmImage = ImageList_CreateImage(himl->hdcImage, himl, himl->cMaxImage);
846 SelectObject(himl->hdcImage, himl->hbmImage);
847 } else
848 himl->hbmImage = 0;
850 if ((himl->cMaxImage > 0) && (himl->flags & ILC_MASK)) {
851 SIZE sz;
853 imagelist_get_bitmap_size(himl, himl->cMaxImage, &sz);
854 himl->hbmMask = CreateBitmap (sz.cx, sz.cy, 1, 1, NULL);
855 if (himl->hbmMask == 0) {
856 ERR("Error creating mask bitmap!\n");
857 goto cleanup;
859 SelectObject(himl->hdcMask, himl->hbmMask);
861 else
862 himl->hbmMask = 0;
864 if (ilc == ILC_COLOR32)
865 himl->has_alpha = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, himl->cMaxImage );
866 else
867 himl->has_alpha = NULL;
869 /* create blending brushes */
870 hbmTemp = CreateBitmap (8, 8, 1, 1, aBitBlend25);
871 himl->hbrBlend25 = CreatePatternBrush (hbmTemp);
872 DeleteObject (hbmTemp);
874 hbmTemp = CreateBitmap (8, 8, 1, 1, aBitBlend50);
875 himl->hbrBlend50 = CreatePatternBrush (hbmTemp);
876 DeleteObject (hbmTemp);
878 TRACE("created imagelist %p\n", himl);
879 return himl;
881 cleanup:
882 ImageList_Destroy(himl);
883 return NULL;
887 /*************************************************************************
888 * ImageList_Destroy [COMCTL32.@]
890 * Destroys an image list.
892 * PARAMS
893 * himl [I] handle to image list
895 * RETURNS
896 * Success: TRUE
897 * Failure: FALSE
900 BOOL WINAPI
901 ImageList_Destroy (HIMAGELIST himl)
903 if (!is_valid(himl))
904 return FALSE;
906 IImageList_Release((IImageList *) himl);
907 return TRUE;
911 /*************************************************************************
912 * ImageList_DragEnter [COMCTL32.@]
914 * Locks window update and displays the drag image at the given position.
916 * PARAMS
917 * hwndLock [I] handle of the window that owns the drag image.
918 * x [I] X position of the drag image.
919 * y [I] Y position of the drag image.
921 * RETURNS
922 * Success: TRUE
923 * Failure: FALSE
925 * NOTES
926 * The position of the drag image is relative to the window, not
927 * the client area.
930 BOOL WINAPI
931 ImageList_DragEnter (HWND hwndLock, INT x, INT y)
933 TRACE("(hwnd=%p x=%d y=%d)\n", hwndLock, x, y);
935 if (!is_valid(InternalDrag.himl))
936 return FALSE;
938 if (hwndLock)
939 InternalDrag.hwnd = hwndLock;
940 else
941 InternalDrag.hwnd = GetDesktopWindow ();
943 InternalDrag.x = x;
944 InternalDrag.y = y;
946 /* draw the drag image and save the background */
947 return ImageList_DragShowNolock(TRUE);
951 /*************************************************************************
952 * ImageList_DragLeave [COMCTL32.@]
954 * Unlocks window update and hides the drag image.
956 * PARAMS
957 * hwndLock [I] handle of the window that owns the drag image.
959 * RETURNS
960 * Success: TRUE
961 * Failure: FALSE
964 BOOL WINAPI
965 ImageList_DragLeave (HWND hwndLock)
967 /* As we don't save drag info in the window this can lead to problems if
968 an app does not supply the same window as DragEnter */
969 /* if (hwndLock)
970 InternalDrag.hwnd = hwndLock;
971 else
972 InternalDrag.hwnd = GetDesktopWindow (); */
973 if(!hwndLock)
974 hwndLock = GetDesktopWindow();
975 if(InternalDrag.hwnd != hwndLock)
976 FIXME("DragLeave hWnd != DragEnter hWnd\n");
978 ImageList_DragShowNolock (FALSE);
980 return TRUE;
984 /*************************************************************************
985 * ImageList_InternalDragDraw [Internal]
987 * Draws the drag image.
989 * PARAMS
990 * hdc [I] device context to draw into.
991 * x [I] X position of the drag image.
992 * y [I] Y position of the drag image.
994 * RETURNS
995 * Success: TRUE
996 * Failure: FALSE
998 * NOTES
999 * The position of the drag image is relative to the window, not
1000 * the client area.
1004 static inline void
1005 ImageList_InternalDragDraw (HDC hdc, INT x, INT y)
1007 IMAGELISTDRAWPARAMS imldp;
1009 ZeroMemory (&imldp, sizeof(imldp));
1010 imldp.cbSize = sizeof(imldp);
1011 imldp.himl = InternalDrag.himl;
1012 imldp.i = 0;
1013 imldp.hdcDst = hdc,
1014 imldp.x = x;
1015 imldp.y = y;
1016 imldp.rgbBk = CLR_DEFAULT;
1017 imldp.rgbFg = CLR_DEFAULT;
1018 imldp.fStyle = ILD_NORMAL;
1019 imldp.fState = ILS_ALPHA;
1020 imldp.Frame = 192;
1021 ImageList_DrawIndirect (&imldp);
1024 /*************************************************************************
1025 * ImageList_DragMove [COMCTL32.@]
1027 * Moves the drag image.
1029 * PARAMS
1030 * x [I] X position of the drag image.
1031 * y [I] Y position of the drag image.
1033 * RETURNS
1034 * Success: TRUE
1035 * Failure: FALSE
1037 * NOTES
1038 * The position of the drag image is relative to the window, not
1039 * the client area.
1042 BOOL WINAPI
1043 ImageList_DragMove (INT x, INT y)
1045 TRACE("(x=%d y=%d)\n", x, y);
1047 if (!is_valid(InternalDrag.himl))
1048 return FALSE;
1050 /* draw/update the drag image */
1051 if (InternalDrag.bShow) {
1052 HDC hdcDrag;
1053 HDC hdcOffScreen;
1054 HDC hdcBg;
1055 HBITMAP hbmOffScreen;
1056 INT origNewX, origNewY;
1057 INT origOldX, origOldY;
1058 INT origRegX, origRegY;
1059 INT sizeRegX, sizeRegY;
1062 /* calculate the update region */
1063 origNewX = x - InternalDrag.dxHotspot;
1064 origNewY = y - InternalDrag.dyHotspot;
1065 origOldX = InternalDrag.x - InternalDrag.dxHotspot;
1066 origOldY = InternalDrag.y - InternalDrag.dyHotspot;
1067 origRegX = min(origNewX, origOldX);
1068 origRegY = min(origNewY, origOldY);
1069 sizeRegX = InternalDrag.himl->cx + abs(x - InternalDrag.x);
1070 sizeRegY = InternalDrag.himl->cy + abs(y - InternalDrag.y);
1072 hdcDrag = GetDCEx(InternalDrag.hwnd, 0,
1073 DCX_WINDOW | DCX_CACHE | DCX_LOCKWINDOWUPDATE);
1074 hdcOffScreen = CreateCompatibleDC(hdcDrag);
1075 hdcBg = CreateCompatibleDC(hdcDrag);
1077 hbmOffScreen = CreateCompatibleBitmap(hdcDrag, sizeRegX, sizeRegY);
1078 SelectObject(hdcOffScreen, hbmOffScreen);
1079 SelectObject(hdcBg, InternalDrag.hbmBg);
1081 /* get the actual background of the update region */
1082 BitBlt(hdcOffScreen, 0, 0, sizeRegX, sizeRegY, hdcDrag,
1083 origRegX, origRegY, SRCCOPY);
1084 /* erase the old image */
1085 BitBlt(hdcOffScreen, origOldX - origRegX, origOldY - origRegY,
1086 InternalDrag.himl->cx, InternalDrag.himl->cy, hdcBg, 0, 0,
1087 SRCCOPY);
1088 /* save the background */
1089 BitBlt(hdcBg, 0, 0, InternalDrag.himl->cx, InternalDrag.himl->cy,
1090 hdcOffScreen, origNewX - origRegX, origNewY - origRegY, SRCCOPY);
1091 /* draw the image */
1092 ImageList_InternalDragDraw(hdcOffScreen, origNewX - origRegX,
1093 origNewY - origRegY);
1094 /* draw the update region to the screen */
1095 BitBlt(hdcDrag, origRegX, origRegY, sizeRegX, sizeRegY,
1096 hdcOffScreen, 0, 0, SRCCOPY);
1098 DeleteDC(hdcBg);
1099 DeleteDC(hdcOffScreen);
1100 DeleteObject(hbmOffScreen);
1101 ReleaseDC(InternalDrag.hwnd, hdcDrag);
1104 /* update the image position */
1105 InternalDrag.x = x;
1106 InternalDrag.y = y;
1108 return TRUE;
1112 /*************************************************************************
1113 * ImageList_DragShowNolock [COMCTL32.@]
1115 * Shows or hides the drag image.
1117 * PARAMS
1118 * bShow [I] TRUE shows the drag image, FALSE hides it.
1120 * RETURNS
1121 * Success: TRUE
1122 * Failure: FALSE
1125 BOOL WINAPI
1126 ImageList_DragShowNolock (BOOL bShow)
1128 HDC hdcDrag;
1129 HDC hdcBg;
1130 INT x, y;
1132 if (!is_valid(InternalDrag.himl))
1133 return FALSE;
1135 TRACE("bShow=0x%X!\n", bShow);
1137 /* DragImage is already visible/hidden */
1138 if ((InternalDrag.bShow && bShow) || (!InternalDrag.bShow && !bShow)) {
1139 return FALSE;
1142 /* position of the origin of the DragImage */
1143 x = InternalDrag.x - InternalDrag.dxHotspot;
1144 y = InternalDrag.y - InternalDrag.dyHotspot;
1146 hdcDrag = GetDCEx (InternalDrag.hwnd, 0,
1147 DCX_WINDOW | DCX_CACHE | DCX_LOCKWINDOWUPDATE);
1148 if (!hdcDrag) {
1149 return FALSE;
1152 hdcBg = CreateCompatibleDC(hdcDrag);
1153 if (!InternalDrag.hbmBg) {
1154 InternalDrag.hbmBg = CreateCompatibleBitmap(hdcDrag,
1155 InternalDrag.himl->cx, InternalDrag.himl->cy);
1157 SelectObject(hdcBg, InternalDrag.hbmBg);
1159 if (bShow) {
1160 /* save the background */
1161 BitBlt(hdcBg, 0, 0, InternalDrag.himl->cx, InternalDrag.himl->cy,
1162 hdcDrag, x, y, SRCCOPY);
1163 /* show the image */
1164 ImageList_InternalDragDraw(hdcDrag, x, y);
1165 } else {
1166 /* hide the image */
1167 BitBlt(hdcDrag, x, y, InternalDrag.himl->cx, InternalDrag.himl->cy,
1168 hdcBg, 0, 0, SRCCOPY);
1171 InternalDrag.bShow = !InternalDrag.bShow;
1173 DeleteDC(hdcBg);
1174 ReleaseDC (InternalDrag.hwnd, hdcDrag);
1175 return TRUE;
1179 /*************************************************************************
1180 * ImageList_Draw [COMCTL32.@]
1182 * Draws an image.
1184 * PARAMS
1185 * himl [I] handle to image list
1186 * i [I] image index
1187 * hdc [I] handle to device context
1188 * x [I] x position
1189 * y [I] y position
1190 * fStyle [I] drawing flags
1192 * RETURNS
1193 * Success: TRUE
1194 * Failure: FALSE
1196 * SEE
1197 * ImageList_DrawEx.
1200 BOOL WINAPI
1201 ImageList_Draw (HIMAGELIST himl, INT i, HDC hdc, INT x, INT y, UINT fStyle)
1203 return ImageList_DrawEx (himl, i, hdc, x, y, 0, 0,
1204 CLR_DEFAULT, CLR_DEFAULT, fStyle);
1208 /*************************************************************************
1209 * ImageList_DrawEx [COMCTL32.@]
1211 * Draws an image and allows using extended drawing features.
1213 * PARAMS
1214 * himl [I] handle to image list
1215 * i [I] image index
1216 * hdc [I] handle to device context
1217 * x [I] X position
1218 * y [I] Y position
1219 * dx [I] X offset
1220 * dy [I] Y offset
1221 * rgbBk [I] background color
1222 * rgbFg [I] foreground color
1223 * fStyle [I] drawing flags
1225 * RETURNS
1226 * Success: TRUE
1227 * Failure: FALSE
1229 * NOTES
1230 * Calls ImageList_DrawIndirect.
1232 * SEE
1233 * ImageList_DrawIndirect.
1236 BOOL WINAPI
1237 ImageList_DrawEx (HIMAGELIST himl, INT i, HDC hdc, INT x, INT y,
1238 INT dx, INT dy, COLORREF rgbBk, COLORREF rgbFg,
1239 UINT fStyle)
1241 IMAGELISTDRAWPARAMS imldp;
1243 ZeroMemory (&imldp, sizeof(imldp));
1244 imldp.cbSize = sizeof(imldp);
1245 imldp.himl = himl;
1246 imldp.i = i;
1247 imldp.hdcDst = hdc,
1248 imldp.x = x;
1249 imldp.y = y;
1250 imldp.cx = dx;
1251 imldp.cy = dy;
1252 imldp.rgbBk = rgbBk;
1253 imldp.rgbFg = rgbFg;
1254 imldp.fStyle = fStyle;
1256 return ImageList_DrawIndirect (&imldp);
1260 static BOOL alpha_blend_image( HIMAGELIST himl, HDC dest_dc, int dest_x, int dest_y,
1261 int src_x, int src_y, int cx, int cy, BLENDFUNCTION func,
1262 UINT style, COLORREF blend_col )
1264 BOOL ret = FALSE;
1265 HDC hdc;
1266 HBITMAP bmp = 0, mask = 0;
1267 BITMAPINFO *info;
1268 void *bits, *mask_bits;
1269 unsigned int *ptr;
1270 int i, j;
1272 if (!(hdc = CreateCompatibleDC( 0 ))) return FALSE;
1273 if (!(info = HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET( BITMAPINFO, bmiColors[256] )))) goto done;
1274 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1275 info->bmiHeader.biWidth = cx;
1276 info->bmiHeader.biHeight = cy;
1277 info->bmiHeader.biPlanes = 1;
1278 info->bmiHeader.biBitCount = 32;
1279 info->bmiHeader.biCompression = BI_RGB;
1280 info->bmiHeader.biSizeImage = cx * cy * 4;
1281 info->bmiHeader.biXPelsPerMeter = 0;
1282 info->bmiHeader.biYPelsPerMeter = 0;
1283 info->bmiHeader.biClrUsed = 0;
1284 info->bmiHeader.biClrImportant = 0;
1285 if (!(bmp = CreateDIBSection( himl->hdcImage, info, DIB_RGB_COLORS, &bits, 0, 0 ))) goto done;
1286 SelectObject( hdc, bmp );
1287 BitBlt( hdc, 0, 0, cx, cy, himl->hdcImage, src_x, src_y, SRCCOPY );
1289 if (blend_col != CLR_NONE)
1291 BYTE r = GetRValue( blend_col );
1292 BYTE g = GetGValue( blend_col );
1293 BYTE b = GetBValue( blend_col );
1295 if (style & ILD_BLEND25)
1297 for (i = 0, ptr = bits; i < cx * cy; i++, ptr++)
1298 *ptr = ((*ptr & 0xff000000) |
1299 ((((*ptr & 0x00ff0000) * 3 + (r << 16)) / 4) & 0x00ff0000) |
1300 ((((*ptr & 0x0000ff00) * 3 + (g << 8)) / 4) & 0x0000ff00) |
1301 ((((*ptr & 0x000000ff) * 3 + (b << 0)) / 4) & 0x000000ff));
1303 else if (style & ILD_BLEND50)
1305 for (i = 0, ptr = bits; i < cx * cy; i++, ptr++)
1306 *ptr = ((*ptr & 0xff000000) |
1307 ((((*ptr & 0x00ff0000) + (r << 16)) / 2) & 0x00ff0000) |
1308 ((((*ptr & 0x0000ff00) + (g << 8)) / 2) & 0x0000ff00) |
1309 ((((*ptr & 0x000000ff) + (b << 0)) / 2) & 0x000000ff));
1313 if (himl->has_alpha) /* we already have an alpha channel in this case */
1315 /* pre-multiply by the alpha channel */
1316 for (i = 0, ptr = bits; i < cx * cy; i++, ptr++)
1318 DWORD alpha = *ptr >> 24;
1319 *ptr = ((*ptr & 0xff000000) |
1320 (((*ptr & 0x00ff0000) * alpha / 255) & 0x00ff0000) |
1321 (((*ptr & 0x0000ff00) * alpha / 255) & 0x0000ff00) |
1322 (((*ptr & 0x000000ff) * alpha / 255)));
1325 else if (himl->hbmMask)
1327 unsigned int width_bytes = (cx + 31) / 32 * 4;
1328 /* generate alpha channel from the mask */
1329 info->bmiHeader.biBitCount = 1;
1330 info->bmiHeader.biSizeImage = width_bytes * cy;
1331 info->bmiColors[0].rgbRed = 0;
1332 info->bmiColors[0].rgbGreen = 0;
1333 info->bmiColors[0].rgbBlue = 0;
1334 info->bmiColors[0].rgbReserved = 0;
1335 info->bmiColors[1].rgbRed = 0xff;
1336 info->bmiColors[1].rgbGreen = 0xff;
1337 info->bmiColors[1].rgbBlue = 0xff;
1338 info->bmiColors[1].rgbReserved = 0;
1339 if (!(mask = CreateDIBSection( himl->hdcMask, info, DIB_RGB_COLORS, &mask_bits, 0, 0 )))
1340 goto done;
1341 SelectObject( hdc, mask );
1342 BitBlt( hdc, 0, 0, cx, cy, himl->hdcMask, src_x, src_y, SRCCOPY );
1343 SelectObject( hdc, bmp );
1344 for (i = 0, ptr = bits; i < cy; i++)
1345 for (j = 0; j < cx; j++, ptr++)
1346 if ((((BYTE *)mask_bits)[i * width_bytes + j / 8] << (j % 8)) & 0x80) *ptr = 0;
1347 else *ptr |= 0xff000000;
1350 ret = GdiAlphaBlend( dest_dc, dest_x, dest_y, cx, cy, hdc, 0, 0, cx, cy, func );
1352 done:
1353 DeleteDC( hdc );
1354 if (bmp) DeleteObject( bmp );
1355 if (mask) DeleteObject( mask );
1356 HeapFree( GetProcessHeap(), 0, info );
1357 return ret;
1360 /*************************************************************************
1361 * ImageList_DrawIndirect [COMCTL32.@]
1363 * Draws an image using various parameters specified in pimldp.
1365 * PARAMS
1366 * pimldp [I] pointer to IMAGELISTDRAWPARAMS structure.
1368 * RETURNS
1369 * Success: TRUE
1370 * Failure: FALSE
1373 BOOL WINAPI
1374 ImageList_DrawIndirect (IMAGELISTDRAWPARAMS *pimldp)
1376 INT cx, cy, nOvlIdx;
1377 DWORD fState, dwRop;
1378 UINT fStyle;
1379 COLORREF oldImageBk, oldImageFg;
1380 HDC hImageDC, hImageListDC, hMaskListDC;
1381 HBITMAP hImageBmp, hOldImageBmp, hBlendMaskBmp;
1382 BOOL bIsTransparent, bBlend, bResult = FALSE, bMask;
1383 HIMAGELIST himl;
1384 HBRUSH hOldBrush;
1385 POINT pt;
1386 BOOL has_alpha;
1388 if (!pimldp || !(himl = pimldp->himl)) return FALSE;
1389 if (!is_valid(himl)) return FALSE;
1390 if ((pimldp->i < 0) || (pimldp->i >= himl->cCurImage)) return FALSE;
1392 imagelist_point_from_index( himl, pimldp->i, &pt );
1393 pt.x += pimldp->xBitmap;
1394 pt.y += pimldp->yBitmap;
1396 fState = pimldp->cbSize < sizeof(IMAGELISTDRAWPARAMS) ? ILS_NORMAL : pimldp->fState;
1397 fStyle = pimldp->fStyle & ~ILD_OVERLAYMASK;
1398 cx = (pimldp->cx == 0) ? himl->cx : pimldp->cx;
1399 cy = (pimldp->cy == 0) ? himl->cy : pimldp->cy;
1401 bIsTransparent = (fStyle & ILD_TRANSPARENT);
1402 if( pimldp->rgbBk == CLR_NONE )
1403 bIsTransparent = TRUE;
1404 if( ( pimldp->rgbBk == CLR_DEFAULT ) && ( himl->clrBk == CLR_NONE ) )
1405 bIsTransparent = TRUE;
1406 bMask = (himl->flags & ILC_MASK) && (fStyle & ILD_MASK) ;
1407 bBlend = (fStyle & (ILD_BLEND25 | ILD_BLEND50) ) && !bMask;
1409 TRACE("himl(%p) hbmMask(%p) iImage(%d) x(%d) y(%d) cx(%d) cy(%d)\n",
1410 himl, himl->hbmMask, pimldp->i, pimldp->x, pimldp->y, cx, cy);
1412 /* we will use these DCs to access the images and masks in the ImageList */
1413 hImageListDC = himl->hdcImage;
1414 hMaskListDC = himl->hdcMask;
1416 /* these will accumulate the image and mask for the image we're drawing */
1417 hImageDC = CreateCompatibleDC( pimldp->hdcDst );
1418 hImageBmp = CreateCompatibleBitmap( pimldp->hdcDst, cx, cy );
1419 hBlendMaskBmp = bBlend ? CreateBitmap(cx, cy, 1, 1, NULL) : 0;
1421 /* Create a compatible DC. */
1422 if (!hImageListDC || !hImageDC || !hImageBmp ||
1423 (bBlend && !hBlendMaskBmp) || (himl->hbmMask && !hMaskListDC))
1424 goto cleanup;
1426 hOldImageBmp = SelectObject(hImageDC, hImageBmp);
1429 * To obtain a transparent look, background color should be set
1430 * to white and foreground color to black when blitting the
1431 * monochrome mask.
1433 oldImageFg = SetTextColor( hImageDC, RGB( 0, 0, 0 ) );
1434 oldImageBk = SetBkColor( hImageDC, RGB( 0xff, 0xff, 0xff ) );
1436 has_alpha = (himl->has_alpha && himl->has_alpha[pimldp->i]);
1437 if (!bMask && (has_alpha || (fState & ILS_ALPHA)))
1439 COLORREF colour, blend_col = CLR_NONE;
1440 BLENDFUNCTION func;
1442 if (bBlend)
1444 blend_col = pimldp->rgbFg;
1445 if (blend_col == CLR_DEFAULT) blend_col = GetSysColor( COLOR_HIGHLIGHT );
1446 else if (blend_col == CLR_NONE) blend_col = GetTextColor( pimldp->hdcDst );
1449 func.BlendOp = AC_SRC_OVER;
1450 func.BlendFlags = 0;
1451 func.SourceConstantAlpha = (fState & ILS_ALPHA) ? pimldp->Frame : 255;
1452 func.AlphaFormat = AC_SRC_ALPHA;
1454 if (bIsTransparent)
1456 bResult = alpha_blend_image( himl, pimldp->hdcDst, pimldp->x, pimldp->y,
1457 pt.x, pt.y, cx, cy, func, fStyle, blend_col );
1458 goto end;
1460 colour = pimldp->rgbBk;
1461 if (colour == CLR_DEFAULT) colour = himl->clrBk;
1462 if (colour == CLR_NONE) colour = GetBkColor( pimldp->hdcDst );
1464 hOldBrush = SelectObject (hImageDC, CreateSolidBrush (colour));
1465 PatBlt( hImageDC, 0, 0, cx, cy, PATCOPY );
1466 alpha_blend_image( himl, hImageDC, 0, 0, pt.x, pt.y, cx, cy, func, fStyle, blend_col );
1467 DeleteObject (SelectObject (hImageDC, hOldBrush));
1468 bResult = BitBlt( pimldp->hdcDst, pimldp->x, pimldp->y, cx, cy, hImageDC, 0, 0, SRCCOPY );
1469 goto end;
1473 * Draw the initial image
1475 if( bMask ) {
1476 if (himl->hbmMask) {
1477 hOldBrush = SelectObject (hImageDC, CreateSolidBrush (GetTextColor(pimldp->hdcDst)));
1478 PatBlt( hImageDC, 0, 0, cx, cy, PATCOPY );
1479 BitBlt(hImageDC, 0, 0, cx, cy, hMaskListDC, pt.x, pt.y, SRCPAINT);
1480 DeleteObject (SelectObject (hImageDC, hOldBrush));
1481 if( bIsTransparent )
1483 BitBlt ( pimldp->hdcDst, pimldp->x, pimldp->y, cx, cy, hImageDC, 0, 0, SRCAND);
1484 bResult = TRUE;
1485 goto end;
1487 } else {
1488 hOldBrush = SelectObject (hImageDC, GetStockObject(BLACK_BRUSH));
1489 PatBlt( hImageDC, 0, 0, cx, cy, PATCOPY);
1490 SelectObject(hImageDC, hOldBrush);
1492 } else {
1493 /* blend the image with the needed solid background */
1494 COLORREF colour = RGB(0,0,0);
1496 if( !bIsTransparent )
1498 colour = pimldp->rgbBk;
1499 if( colour == CLR_DEFAULT )
1500 colour = himl->clrBk;
1501 if( colour == CLR_NONE )
1502 colour = GetBkColor(pimldp->hdcDst);
1505 hOldBrush = SelectObject (hImageDC, CreateSolidBrush (colour));
1506 PatBlt( hImageDC, 0, 0, cx, cy, PATCOPY );
1507 if (himl->hbmMask)
1509 BitBlt( hImageDC, 0, 0, cx, cy, hMaskListDC, pt.x, pt.y, SRCAND );
1510 BitBlt( hImageDC, 0, 0, cx, cy, hImageListDC, pt.x, pt.y, SRCPAINT );
1512 else
1513 BitBlt( hImageDC, 0, 0, cx, cy, hImageListDC, pt.x, pt.y, SRCCOPY);
1514 DeleteObject (SelectObject (hImageDC, hOldBrush));
1517 /* Time for blending, if required */
1518 if (bBlend) {
1519 HBRUSH hBlendBrush;
1520 COLORREF clrBlend = pimldp->rgbFg;
1521 HDC hBlendMaskDC = hImageListDC;
1522 HBITMAP hOldBitmap;
1524 /* Create the blend Mask */
1525 hOldBitmap = SelectObject(hBlendMaskDC, hBlendMaskBmp);
1526 hBlendBrush = fStyle & ILD_BLEND50 ? himl->hbrBlend50 : himl->hbrBlend25;
1527 hOldBrush = SelectObject(hBlendMaskDC, hBlendBrush);
1528 PatBlt(hBlendMaskDC, 0, 0, cx, cy, PATCOPY);
1529 SelectObject(hBlendMaskDC, hOldBrush);
1531 /* Modify the blend mask if an Image Mask exist */
1532 if(himl->hbmMask) {
1533 BitBlt(hBlendMaskDC, 0, 0, cx, cy, hMaskListDC, pt.x, pt.y, 0x220326); /* NOTSRCAND */
1534 BitBlt(hBlendMaskDC, 0, 0, cx, cy, hBlendMaskDC, 0, 0, NOTSRCCOPY);
1537 /* now apply blend to the current image given the BlendMask */
1538 if (clrBlend == CLR_DEFAULT) clrBlend = GetSysColor (COLOR_HIGHLIGHT);
1539 else if (clrBlend == CLR_NONE) clrBlend = GetTextColor (pimldp->hdcDst);
1540 hOldBrush = SelectObject (hImageDC, CreateSolidBrush(clrBlend));
1541 BitBlt (hImageDC, 0, 0, cx, cy, hBlendMaskDC, 0, 0, 0xB8074A); /* PSDPxax */
1542 DeleteObject(SelectObject(hImageDC, hOldBrush));
1543 SelectObject(hBlendMaskDC, hOldBitmap);
1546 /* Now do the overlay image, if any */
1547 nOvlIdx = (pimldp->fStyle & ILD_OVERLAYMASK) >> 8;
1548 if ( (nOvlIdx >= 1) && (nOvlIdx <= MAX_OVERLAYIMAGE)) {
1549 nOvlIdx = himl->nOvlIdx[nOvlIdx - 1];
1550 if ((nOvlIdx >= 0) && (nOvlIdx < himl->cCurImage)) {
1551 POINT ptOvl;
1552 imagelist_point_from_index( himl, nOvlIdx, &ptOvl );
1553 ptOvl.x += pimldp->xBitmap;
1554 if (himl->hbmMask && !(fStyle & ILD_IMAGE))
1555 BitBlt (hImageDC, 0, 0, cx, cy, hMaskListDC, ptOvl.x, ptOvl.y, SRCAND);
1556 BitBlt (hImageDC, 0, 0, cx, cy, hImageListDC, ptOvl.x, ptOvl.y, SRCPAINT);
1560 if (fState & ILS_SATURATE) FIXME("ILS_SATURATE: unimplemented!\n");
1561 if (fState & ILS_GLOW) FIXME("ILS_GLOW: unimplemented!\n");
1562 if (fState & ILS_SHADOW) FIXME("ILS_SHADOW: unimplemented!\n");
1564 if (fStyle & ILD_PRESERVEALPHA) FIXME("ILD_PRESERVEALPHA: unimplemented!\n");
1565 if (fStyle & ILD_SCALE) FIXME("ILD_SCALE: unimplemented!\n");
1566 if (fStyle & ILD_DPISCALE) FIXME("ILD_DPISCALE: unimplemented!\n");
1568 /* now copy the image to the screen */
1569 dwRop = SRCCOPY;
1570 if (himl->hbmMask && bIsTransparent ) {
1571 COLORREF oldDstFg = SetTextColor(pimldp->hdcDst, RGB( 0, 0, 0 ) );
1572 COLORREF oldDstBk = SetBkColor(pimldp->hdcDst, RGB( 0xff, 0xff, 0xff ));
1573 BitBlt (pimldp->hdcDst, pimldp->x, pimldp->y, cx, cy, hMaskListDC, pt.x, pt.y, SRCAND);
1574 SetBkColor(pimldp->hdcDst, oldDstBk);
1575 SetTextColor(pimldp->hdcDst, oldDstFg);
1576 dwRop = SRCPAINT;
1578 if (fStyle & ILD_ROP) dwRop = pimldp->dwRop;
1579 BitBlt (pimldp->hdcDst, pimldp->x, pimldp->y, cx, cy, hImageDC, 0, 0, dwRop);
1581 bResult = TRUE;
1582 end:
1583 /* cleanup the mess */
1584 SetBkColor(hImageDC, oldImageBk);
1585 SetTextColor(hImageDC, oldImageFg);
1586 SelectObject(hImageDC, hOldImageBmp);
1587 cleanup:
1588 DeleteObject(hBlendMaskBmp);
1589 DeleteObject(hImageBmp);
1590 DeleteDC(hImageDC);
1592 return bResult;
1596 /*************************************************************************
1597 * ImageList_Duplicate [COMCTL32.@]
1599 * Duplicates an image list.
1601 * PARAMS
1602 * himlSrc [I] source image list handle
1604 * RETURNS
1605 * Success: Handle of duplicated image list.
1606 * Failure: NULL
1609 HIMAGELIST WINAPI
1610 ImageList_Duplicate (HIMAGELIST himlSrc)
1612 HIMAGELIST himlDst;
1614 if (!is_valid(himlSrc)) {
1615 ERR("Invalid image list handle!\n");
1616 return NULL;
1619 himlDst = ImageList_Create (himlSrc->cx, himlSrc->cy, himlSrc->flags,
1620 himlSrc->cCurImage, himlSrc->cGrow);
1622 if (himlDst)
1624 SIZE sz;
1626 imagelist_get_bitmap_size(himlSrc, himlSrc->cCurImage, &sz);
1627 BitBlt (himlDst->hdcImage, 0, 0, sz.cx, sz.cy,
1628 himlSrc->hdcImage, 0, 0, SRCCOPY);
1630 if (himlDst->hbmMask)
1631 BitBlt (himlDst->hdcMask, 0, 0, sz.cx, sz.cy,
1632 himlSrc->hdcMask, 0, 0, SRCCOPY);
1634 himlDst->cCurImage = himlSrc->cCurImage;
1635 if (himlSrc->has_alpha && himlDst->has_alpha)
1636 memcpy( himlDst->has_alpha, himlSrc->has_alpha, himlDst->cCurImage );
1638 return himlDst;
1642 /*************************************************************************
1643 * ImageList_EndDrag [COMCTL32.@]
1645 * Finishes a drag operation.
1647 * PARAMS
1648 * no Parameters
1650 * RETURNS
1651 * Success: TRUE
1652 * Failure: FALSE
1655 VOID WINAPI
1656 ImageList_EndDrag (void)
1658 /* cleanup the InternalDrag struct */
1659 InternalDrag.hwnd = 0;
1660 if (InternalDrag.himl != InternalDrag.himlNoCursor)
1661 ImageList_Destroy (InternalDrag.himlNoCursor);
1662 ImageList_Destroy (InternalDrag.himl);
1663 InternalDrag.himlNoCursor = InternalDrag.himl = 0;
1664 InternalDrag.x= 0;
1665 InternalDrag.y= 0;
1666 InternalDrag.dxHotspot = 0;
1667 InternalDrag.dyHotspot = 0;
1668 InternalDrag.bShow = FALSE;
1669 DeleteObject(InternalDrag.hbmBg);
1670 InternalDrag.hbmBg = 0;
1674 /*************************************************************************
1675 * ImageList_GetBkColor [COMCTL32.@]
1677 * Returns the background color of an image list.
1679 * PARAMS
1680 * himl [I] Image list handle.
1682 * RETURNS
1683 * Success: background color
1684 * Failure: CLR_NONE
1687 COLORREF WINAPI
1688 ImageList_GetBkColor (HIMAGELIST himl)
1690 return himl ? himl->clrBk : CLR_NONE;
1694 /*************************************************************************
1695 * ImageList_GetDragImage [COMCTL32.@]
1697 * Returns the handle to the internal drag image list.
1699 * PARAMS
1700 * ppt [O] Pointer to the drag position. Can be NULL.
1701 * pptHotspot [O] Pointer to the position of the hot spot. Can be NULL.
1703 * RETURNS
1704 * Success: Handle of the drag image list.
1705 * Failure: NULL.
1708 HIMAGELIST WINAPI
1709 ImageList_GetDragImage (POINT *ppt, POINT *pptHotspot)
1711 if (is_valid(InternalDrag.himl)) {
1712 if (ppt) {
1713 ppt->x = InternalDrag.x;
1714 ppt->y = InternalDrag.y;
1716 if (pptHotspot) {
1717 pptHotspot->x = InternalDrag.dxHotspot;
1718 pptHotspot->y = InternalDrag.dyHotspot;
1720 return (InternalDrag.himl);
1723 return NULL;
1727 /*************************************************************************
1728 * ImageList_GetFlags [COMCTL32.@]
1730 * Gets the flags of the specified image list.
1732 * PARAMS
1733 * himl [I] Handle to image list
1735 * RETURNS
1736 * Image list flags.
1738 * BUGS
1739 * Stub.
1742 DWORD WINAPI
1743 ImageList_GetFlags(HIMAGELIST himl)
1745 TRACE("%p\n", himl);
1747 return is_valid(himl) ? himl->flags : 0;
1751 /*************************************************************************
1752 * ImageList_GetIcon [COMCTL32.@]
1754 * Creates an icon from a masked image of an image list.
1756 * PARAMS
1757 * himl [I] handle to image list
1758 * i [I] image index
1759 * flags [I] drawing style flags
1761 * RETURNS
1762 * Success: icon handle
1763 * Failure: NULL
1766 HICON WINAPI
1767 ImageList_GetIcon (HIMAGELIST himl, INT i, UINT fStyle)
1769 ICONINFO ii;
1770 HICON hIcon;
1771 HBITMAP hOldDstBitmap;
1772 HDC hdcDst;
1773 POINT pt;
1775 TRACE("%p %d %d\n", himl, i, fStyle);
1776 if (!is_valid(himl) || (i < 0) || (i >= himl->cCurImage)) return NULL;
1778 ii.fIcon = TRUE;
1779 ii.xHotspot = 0;
1780 ii.yHotspot = 0;
1782 /* create colour bitmap */
1783 hdcDst = GetDC(0);
1784 ii.hbmColor = CreateCompatibleBitmap(hdcDst, himl->cx, himl->cy);
1785 ReleaseDC(0, hdcDst);
1787 hdcDst = CreateCompatibleDC(0);
1789 imagelist_point_from_index( himl, i, &pt );
1791 /* draw mask*/
1792 ii.hbmMask = CreateBitmap (himl->cx, himl->cy, 1, 1, NULL);
1793 hOldDstBitmap = SelectObject (hdcDst, ii.hbmMask);
1794 if (himl->hbmMask) {
1795 BitBlt (hdcDst, 0, 0, himl->cx, himl->cy,
1796 himl->hdcMask, pt.x, pt.y, SRCCOPY);
1798 else
1799 PatBlt (hdcDst, 0, 0, himl->cx, himl->cy, BLACKNESS);
1801 /* draw image*/
1802 SelectObject (hdcDst, ii.hbmColor);
1803 BitBlt (hdcDst, 0, 0, himl->cx, himl->cy,
1804 himl->hdcImage, pt.x, pt.y, SRCCOPY);
1807 * CreateIconIndirect requires us to deselect the bitmaps from
1808 * the DCs before calling
1810 SelectObject(hdcDst, hOldDstBitmap);
1812 hIcon = CreateIconIndirect (&ii);
1814 DeleteObject (ii.hbmMask);
1815 DeleteObject (ii.hbmColor);
1816 DeleteDC (hdcDst);
1818 return hIcon;
1822 /*************************************************************************
1823 * ImageList_GetIconSize [COMCTL32.@]
1825 * Retrieves the size of an image in an image list.
1827 * PARAMS
1828 * himl [I] handle to image list
1829 * cx [O] pointer to the image width.
1830 * cy [O] pointer to the image height.
1832 * RETURNS
1833 * Success: TRUE
1834 * Failure: FALSE
1836 * NOTES
1837 * All images in an image list have the same size.
1840 BOOL WINAPI
1841 ImageList_GetIconSize (HIMAGELIST himl, INT *cx, INT *cy)
1843 if (!is_valid(himl) || !cx || !cy)
1844 return FALSE;
1846 *cx = himl->cx;
1847 *cy = himl->cy;
1849 return TRUE;
1853 /*************************************************************************
1854 * ImageList_GetImageCount [COMCTL32.@]
1856 * Returns the number of images in an image list.
1858 * PARAMS
1859 * himl [I] handle to image list
1861 * RETURNS
1862 * Success: Number of images.
1863 * Failure: 0
1866 INT WINAPI
1867 ImageList_GetImageCount (HIMAGELIST himl)
1869 if (!is_valid(himl))
1870 return 0;
1872 return himl->cCurImage;
1876 /*************************************************************************
1877 * ImageList_GetImageInfo [COMCTL32.@]
1879 * Returns information about an image in an image list.
1881 * PARAMS
1882 * himl [I] handle to image list
1883 * i [I] image index
1884 * pImageInfo [O] pointer to the image information
1886 * RETURNS
1887 * Success: TRUE
1888 * Failure: FALSE
1891 BOOL WINAPI
1892 ImageList_GetImageInfo (HIMAGELIST himl, INT i, IMAGEINFO *pImageInfo)
1894 POINT pt;
1896 if (!is_valid(himl) || (pImageInfo == NULL))
1897 return FALSE;
1898 if ((i < 0) || (i >= himl->cCurImage))
1899 return FALSE;
1901 pImageInfo->hbmImage = himl->hbmImage;
1902 pImageInfo->hbmMask = himl->hbmMask;
1904 imagelist_point_from_index( himl, i, &pt );
1905 pImageInfo->rcImage.top = pt.y;
1906 pImageInfo->rcImage.bottom = pt.y + himl->cy;
1907 pImageInfo->rcImage.left = pt.x;
1908 pImageInfo->rcImage.right = pt.x + himl->cx;
1910 return TRUE;
1914 /*************************************************************************
1915 * ImageList_GetImageRect [COMCTL32.@]
1917 * Retrieves the rectangle of the specified image in an image list.
1919 * PARAMS
1920 * himl [I] handle to image list
1921 * i [I] image index
1922 * lpRect [O] pointer to the image rectangle
1924 * RETURNS
1925 * Success: TRUE
1926 * Failure: FALSE
1928 * NOTES
1929 * This is an UNDOCUMENTED function!!!
1932 BOOL WINAPI
1933 ImageList_GetImageRect (HIMAGELIST himl, INT i, LPRECT lpRect)
1935 POINT pt;
1937 if (!is_valid(himl) || (lpRect == NULL))
1938 return FALSE;
1939 if ((i < 0) || (i >= himl->cCurImage))
1940 return FALSE;
1942 imagelist_point_from_index( himl, i, &pt );
1943 lpRect->left = pt.x;
1944 lpRect->top = pt.y;
1945 lpRect->right = pt.x + himl->cx;
1946 lpRect->bottom = pt.y + himl->cy;
1948 return TRUE;
1952 /*************************************************************************
1953 * ImageList_LoadImage [COMCTL32.@]
1954 * ImageList_LoadImageA [COMCTL32.@]
1956 * Creates an image list from a bitmap, icon or cursor.
1958 * See ImageList_LoadImageW.
1961 HIMAGELIST WINAPI
1962 ImageList_LoadImageA (HINSTANCE hi, LPCSTR lpbmp, INT cx, INT cGrow,
1963 COLORREF clrMask, UINT uType, UINT uFlags)
1965 HIMAGELIST himl;
1966 LPWSTR lpbmpW;
1967 DWORD len;
1969 if (IS_INTRESOURCE(lpbmp))
1970 return ImageList_LoadImageW(hi, (LPCWSTR)lpbmp, cx, cGrow, clrMask,
1971 uType, uFlags);
1973 len = MultiByteToWideChar(CP_ACP, 0, lpbmp, -1, NULL, 0);
1974 lpbmpW = Alloc(len * sizeof(WCHAR));
1975 MultiByteToWideChar(CP_ACP, 0, lpbmp, -1, lpbmpW, len);
1977 himl = ImageList_LoadImageW(hi, lpbmpW, cx, cGrow, clrMask, uType, uFlags);
1978 Free (lpbmpW);
1979 return himl;
1983 /*************************************************************************
1984 * ImageList_LoadImageW [COMCTL32.@]
1986 * Creates an image list from a bitmap, icon or cursor.
1988 * PARAMS
1989 * hi [I] instance handle
1990 * lpbmp [I] name or id of the image
1991 * cx [I] width of each image
1992 * cGrow [I] number of images to expand
1993 * clrMask [I] mask color
1994 * uType [I] type of image to load
1995 * uFlags [I] loading flags
1997 * RETURNS
1998 * Success: handle to the loaded image list
1999 * Failure: NULL
2001 * SEE
2002 * LoadImage ()
2005 HIMAGELIST WINAPI
2006 ImageList_LoadImageW (HINSTANCE hi, LPCWSTR lpbmp, INT cx, INT cGrow,
2007 COLORREF clrMask, UINT uType, UINT uFlags)
2009 HIMAGELIST himl = NULL;
2010 HANDLE handle;
2011 INT nImageCount;
2013 handle = LoadImageW (hi, lpbmp, uType, 0, 0, uFlags);
2014 if (!handle) {
2015 WARN("Couldn't load image\n");
2016 return NULL;
2019 if (uType == IMAGE_BITMAP) {
2020 DIBSECTION dib;
2021 UINT color;
2023 if (GetObjectW (handle, sizeof(dib), &dib) == sizeof(BITMAP)) color = ILC_COLOR;
2024 else color = dib.dsBm.bmBitsPixel;
2026 /* To match windows behavior, if cx is set to zero and
2027 the flag DI_DEFAULTSIZE is specified, cx becomes the
2028 system metric value for icons. If the flag is not specified
2029 the function sets the size to the height of the bitmap */
2030 if (cx == 0)
2032 if (uFlags & DI_DEFAULTSIZE)
2033 cx = GetSystemMetrics (SM_CXICON);
2034 else
2035 cx = dib.dsBm.bmHeight;
2038 nImageCount = dib.dsBm.bmWidth / cx;
2040 if (clrMask != CLR_NONE) color |= ILC_MASK;
2041 himl = ImageList_Create (cx, dib.dsBm.bmHeight, color, nImageCount, cGrow);
2042 if (!himl) {
2043 DeleteObject (handle);
2044 return NULL;
2046 ImageList_AddMasked (himl, handle, clrMask);
2048 else if ((uType == IMAGE_ICON) || (uType == IMAGE_CURSOR)) {
2049 ICONINFO ii;
2050 BITMAP bmp;
2052 GetIconInfo (handle, &ii);
2053 GetObjectW (ii.hbmColor, sizeof(BITMAP), &bmp);
2054 himl = ImageList_Create (bmp.bmWidth, bmp.bmHeight,
2055 ILC_MASK | ILC_COLOR, 1, cGrow);
2056 if (!himl) {
2057 DeleteObject (ii.hbmColor);
2058 DeleteObject (ii.hbmMask);
2059 DeleteObject (handle);
2060 return NULL;
2062 ImageList_Add (himl, ii.hbmColor, ii.hbmMask);
2063 DeleteObject (ii.hbmColor);
2064 DeleteObject (ii.hbmMask);
2067 DeleteObject (handle);
2069 return himl;
2073 /*************************************************************************
2074 * ImageList_Merge [COMCTL32.@]
2076 * Create an image list containing a merged image from two image lists.
2078 * PARAMS
2079 * himl1 [I] handle to first image list
2080 * i1 [I] first image index
2081 * himl2 [I] handle to second image list
2082 * i2 [I] second image index
2083 * dx [I] X offset of the second image relative to the first.
2084 * dy [I] Y offset of the second image relative to the first.
2086 * RETURNS
2087 * Success: The newly created image list. It contains a single image
2088 * consisting of the second image merged with the first.
2089 * Failure: NULL, if either himl1 or himl2 is invalid.
2091 * NOTES
2092 * - The returned image list should be deleted by the caller using
2093 * ImageList_Destroy() when it is no longer required.
2094 * - If either i1 or i2 is not a valid image index, they will be treated
2095 * as blank images.
2097 HIMAGELIST WINAPI
2098 ImageList_Merge (HIMAGELIST himl1, INT i1, HIMAGELIST himl2, INT i2,
2099 INT dx, INT dy)
2101 HIMAGELIST himlDst = NULL;
2102 INT cxDst, cyDst;
2103 INT xOff1, yOff1, xOff2, yOff2;
2104 POINT pt1, pt2;
2105 INT newFlags;
2107 TRACE("(himl1=%p i1=%d himl2=%p i2=%d dx=%d dy=%d)\n", himl1, i1, himl2,
2108 i2, dx, dy);
2110 if (!is_valid(himl1) || !is_valid(himl2))
2111 return NULL;
2113 if (dx > 0) {
2114 cxDst = max (himl1->cx, dx + himl2->cx);
2115 xOff1 = 0;
2116 xOff2 = dx;
2118 else if (dx < 0) {
2119 cxDst = max (himl2->cx, himl1->cx - dx);
2120 xOff1 = -dx;
2121 xOff2 = 0;
2123 else {
2124 cxDst = max (himl1->cx, himl2->cx);
2125 xOff1 = 0;
2126 xOff2 = 0;
2129 if (dy > 0) {
2130 cyDst = max (himl1->cy, dy + himl2->cy);
2131 yOff1 = 0;
2132 yOff2 = dy;
2134 else if (dy < 0) {
2135 cyDst = max (himl2->cy, himl1->cy - dy);
2136 yOff1 = -dy;
2137 yOff2 = 0;
2139 else {
2140 cyDst = max (himl1->cy, himl2->cy);
2141 yOff1 = 0;
2142 yOff2 = 0;
2145 newFlags = (himl1->flags > himl2->flags ? himl1->flags : himl2->flags) & ILC_COLORDDB;
2146 if (newFlags == ILC_COLORDDB && (himl1->flags & ILC_COLORDDB) == ILC_COLOR16)
2147 newFlags = ILC_COLOR16; /* this is what native (at least v5) does, don't know why */
2148 himlDst = ImageList_Create (cxDst, cyDst, ILC_MASK | newFlags, 1, 1);
2150 if (himlDst)
2152 imagelist_point_from_index( himl1, i1, &pt1 );
2153 imagelist_point_from_index( himl2, i2, &pt2 );
2155 /* copy image */
2156 BitBlt (himlDst->hdcImage, 0, 0, cxDst, cyDst, himl1->hdcImage, 0, 0, BLACKNESS);
2157 if (i1 >= 0 && i1 < himl1->cCurImage)
2158 BitBlt (himlDst->hdcImage, xOff1, yOff1, himl1->cx, himl1->cy, himl1->hdcImage, pt1.x, pt1.y, SRCCOPY);
2159 if (i2 >= 0 && i2 < himl2->cCurImage)
2161 if (himl2->flags & ILC_MASK)
2163 BitBlt (himlDst->hdcImage, xOff2, yOff2, himl2->cx, himl2->cy, himl2->hdcMask , pt2.x, pt2.y, SRCAND);
2164 BitBlt (himlDst->hdcImage, xOff2, yOff2, himl2->cx, himl2->cy, himl2->hdcImage, pt2.x, pt2.y, SRCPAINT);
2166 else
2167 BitBlt (himlDst->hdcImage, xOff2, yOff2, himl2->cx, himl2->cy, himl2->hdcImage, pt2.x, pt2.y, SRCCOPY);
2170 /* copy mask */
2171 BitBlt (himlDst->hdcMask, 0, 0, cxDst, cyDst, himl1->hdcMask, 0, 0, WHITENESS);
2172 if (i1 >= 0 && i1 < himl1->cCurImage)
2173 BitBlt (himlDst->hdcMask, xOff1, yOff1, himl1->cx, himl1->cy, himl1->hdcMask, pt1.x, pt1.y, SRCCOPY);
2174 if (i2 >= 0 && i2 < himl2->cCurImage)
2175 BitBlt (himlDst->hdcMask, xOff2, yOff2, himl2->cx, himl2->cy, himl2->hdcMask, pt2.x, pt2.y, SRCAND);
2177 himlDst->cCurImage = 1;
2180 return himlDst;
2184 /* helper for ImageList_Read, see comments below */
2185 static void *read_bitmap(IStream *pstm, BITMAPINFO *bmi)
2187 BITMAPFILEHEADER bmfh;
2188 int bitsperpixel, palspace;
2189 void *bits;
2191 if (FAILED(IStream_Read ( pstm, &bmfh, sizeof(bmfh), NULL)))
2192 return NULL;
2194 if (bmfh.bfType != (('M'<<8)|'B'))
2195 return NULL;
2197 if (FAILED(IStream_Read ( pstm, &bmi->bmiHeader, sizeof(bmi->bmiHeader), NULL)))
2198 return NULL;
2200 if ((bmi->bmiHeader.biSize != sizeof(bmi->bmiHeader)))
2201 return NULL;
2203 TRACE("width %u, height %u, planes %u, bpp %u\n",
2204 bmi->bmiHeader.biWidth, bmi->bmiHeader.biHeight,
2205 bmi->bmiHeader.biPlanes, bmi->bmiHeader.biBitCount);
2207 bitsperpixel = bmi->bmiHeader.biPlanes * bmi->bmiHeader.biBitCount;
2208 if (bitsperpixel<=8)
2209 palspace = (1<<bitsperpixel)*sizeof(RGBQUAD);
2210 else
2211 palspace = 0;
2213 bmi->bmiHeader.biSizeImage = get_dib_image_size( bmi );
2215 /* read the palette right after the end of the bitmapinfoheader */
2216 if (palspace && FAILED(IStream_Read(pstm, bmi->bmiColors, palspace, NULL)))
2217 return NULL;
2219 bits = Alloc(bmi->bmiHeader.biSizeImage);
2220 if (!bits) return NULL;
2222 if (FAILED(IStream_Read(pstm, bits, bmi->bmiHeader.biSizeImage, NULL)))
2224 Free(bits);
2225 return NULL;
2227 return bits;
2230 /*************************************************************************
2231 * ImageList_Read [COMCTL32.@]
2233 * Reads an image list from a stream.
2235 * PARAMS
2236 * pstm [I] pointer to a stream
2238 * RETURNS
2239 * Success: handle to image list
2240 * Failure: NULL
2242 * The format is like this:
2243 * ILHEAD ilheadstruct;
2245 * for the color image part:
2246 * BITMAPFILEHEADER bmfh;
2247 * BITMAPINFOHEADER bmih;
2248 * only if it has a palette:
2249 * RGBQUAD rgbs[nr_of_paletted_colors];
2251 * BYTE colorbits[imagesize];
2253 * the following only if the ILC_MASK bit is set in ILHEAD.ilFlags:
2254 * BITMAPFILEHEADER bmfh_mask;
2255 * BITMAPINFOHEADER bmih_mask;
2256 * only if it has a palette (it usually does not):
2257 * RGBQUAD rgbs[nr_of_paletted_colors];
2259 * BYTE maskbits[imagesize];
2261 HIMAGELIST WINAPI ImageList_Read(IStream *pstm)
2263 char image_buf[sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * 256];
2264 char mask_buf[sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * 256];
2265 BITMAPINFO *image_info = (BITMAPINFO *)image_buf;
2266 BITMAPINFO *mask_info = (BITMAPINFO *)mask_buf;
2267 void *image_bits, *mask_bits = NULL;
2268 ILHEAD ilHead;
2269 HIMAGELIST himl;
2270 unsigned int i;
2272 TRACE("%p\n", pstm);
2274 if (FAILED(IStream_Read (pstm, &ilHead, sizeof(ILHEAD), NULL)))
2275 return NULL;
2276 if (ilHead.usMagic != (('L' << 8) | 'I'))
2277 return NULL;
2278 if (ilHead.usVersion != 0x101) /* probably version? */
2279 return NULL;
2281 TRACE("cx %u, cy %u, flags 0x%04x, cCurImage %u, cMaxImage %u\n",
2282 ilHead.cx, ilHead.cy, ilHead.flags, ilHead.cCurImage, ilHead.cMaxImage);
2284 himl = ImageList_Create(ilHead.cx, ilHead.cy, ilHead.flags, ilHead.cCurImage, ilHead.cMaxImage);
2285 if (!himl)
2286 return NULL;
2288 if (!(image_bits = read_bitmap(pstm, image_info)))
2290 WARN("failed to read bitmap from stream\n");
2291 return NULL;
2293 if (ilHead.flags & ILC_MASK)
2295 if (!(mask_bits = read_bitmap(pstm, mask_info)))
2297 WARN("failed to read mask bitmap from stream\n");
2298 return NULL;
2301 else mask_info = NULL;
2303 if (himl->has_alpha && image_info->bmiHeader.biBitCount == 32)
2305 DWORD *ptr = image_bits;
2306 BYTE *mask_ptr = mask_bits;
2307 int stride = himl->cy * image_info->bmiHeader.biWidth;
2309 if (image_info->bmiHeader.biHeight > 0) /* bottom-up */
2311 ptr += image_info->bmiHeader.biHeight * image_info->bmiHeader.biWidth - stride;
2312 mask_ptr += (image_info->bmiHeader.biHeight * image_info->bmiHeader.biWidth - stride) / 8;
2313 stride = -stride;
2314 image_info->bmiHeader.biHeight = himl->cy;
2316 else image_info->bmiHeader.biHeight = -himl->cy;
2318 for (i = 0; i < ilHead.cCurImage; i += TILE_COUNT)
2320 add_dib_bits( himl, i, min( ilHead.cCurImage - i, TILE_COUNT ),
2321 himl->cx, himl->cy, image_info, mask_info, ptr, mask_ptr );
2322 ptr += stride;
2323 mask_ptr += stride / 8;
2326 else
2328 StretchDIBits( himl->hdcImage, 0, 0, image_info->bmiHeader.biWidth, image_info->bmiHeader.biHeight,
2329 0, 0, image_info->bmiHeader.biWidth, image_info->bmiHeader.biHeight,
2330 image_bits, image_info, DIB_RGB_COLORS, SRCCOPY);
2331 if (mask_info)
2332 StretchDIBits( himl->hdcMask, 0, 0, mask_info->bmiHeader.biWidth, mask_info->bmiHeader.biHeight,
2333 0, 0, mask_info->bmiHeader.biWidth, mask_info->bmiHeader.biHeight,
2334 mask_bits, mask_info, DIB_RGB_COLORS, SRCCOPY);
2336 Free( image_bits );
2337 Free( mask_bits );
2339 himl->cCurImage = ilHead.cCurImage;
2340 himl->cMaxImage = ilHead.cMaxImage;
2342 ImageList_SetBkColor(himl,ilHead.bkcolor);
2343 for (i=0;i<4;i++)
2344 ImageList_SetOverlayImage(himl,ilHead.ovls[i],i+1);
2345 return himl;
2349 /*************************************************************************
2350 * ImageList_Remove [COMCTL32.@]
2352 * Removes an image from an image list
2354 * PARAMS
2355 * himl [I] image list handle
2356 * i [I] image index
2358 * RETURNS
2359 * Success: TRUE
2360 * Failure: FALSE
2362 * FIXME: as the image list storage test shows, native comctl32 simply shifts
2363 * images without creating a new bitmap.
2365 BOOL WINAPI
2366 ImageList_Remove (HIMAGELIST himl, INT i)
2368 HBITMAP hbmNewImage, hbmNewMask;
2369 HDC hdcBmp;
2370 SIZE sz;
2372 TRACE("(himl=%p i=%d)\n", himl, i);
2374 if (!is_valid(himl)) {
2375 ERR("Invalid image list handle!\n");
2376 return FALSE;
2379 if ((i < -1) || (i >= himl->cCurImage)) {
2380 TRACE("index out of range! %d\n", i);
2381 return FALSE;
2384 if (i == -1) {
2385 INT nCount;
2387 /* remove all */
2388 if (himl->cCurImage == 0) {
2389 /* remove all on empty ImageList is allowed */
2390 TRACE("remove all on empty ImageList!\n");
2391 return TRUE;
2394 himl->cMaxImage = himl->cGrow;
2395 himl->cCurImage = 0;
2396 for (nCount = 0; nCount < MAX_OVERLAYIMAGE; nCount++)
2397 himl->nOvlIdx[nCount] = -1;
2399 if (himl->has_alpha)
2401 HeapFree( GetProcessHeap(), 0, himl->has_alpha );
2402 himl->has_alpha = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, himl->cMaxImage );
2405 hbmNewImage = ImageList_CreateImage(himl->hdcImage, himl, himl->cMaxImage);
2406 SelectObject (himl->hdcImage, hbmNewImage);
2407 DeleteObject (himl->hbmImage);
2408 himl->hbmImage = hbmNewImage;
2410 if (himl->hbmMask) {
2412 imagelist_get_bitmap_size(himl, himl->cMaxImage, &sz);
2413 hbmNewMask = CreateBitmap (sz.cx, sz.cy, 1, 1, NULL);
2414 SelectObject (himl->hdcMask, hbmNewMask);
2415 DeleteObject (himl->hbmMask);
2416 himl->hbmMask = hbmNewMask;
2419 else {
2420 /* delete one image */
2421 TRACE("Remove single image! %d\n", i);
2423 /* create new bitmap(s) */
2424 TRACE(" - Number of images: %d / %d (Old/New)\n",
2425 himl->cCurImage, himl->cCurImage - 1);
2427 hbmNewImage = ImageList_CreateImage(himl->hdcImage, himl, himl->cMaxImage);
2429 imagelist_get_bitmap_size(himl, himl->cMaxImage, &sz );
2430 if (himl->hbmMask)
2431 hbmNewMask = CreateBitmap (sz.cx, sz.cy, 1, 1, NULL);
2432 else
2433 hbmNewMask = 0; /* Just to keep compiler happy! */
2435 hdcBmp = CreateCompatibleDC (0);
2437 /* copy all images and masks prior to the "removed" image */
2438 if (i > 0) {
2439 TRACE("Pre image copy: Copy %d images\n", i);
2441 SelectObject (hdcBmp, hbmNewImage);
2442 imagelist_copy_images( himl, himl->hdcImage, hdcBmp, 0, i, 0 );
2444 if (himl->hbmMask) {
2445 SelectObject (hdcBmp, hbmNewMask);
2446 imagelist_copy_images( himl, himl->hdcMask, hdcBmp, 0, i, 0 );
2450 /* copy all images and masks behind the removed image */
2451 if (i < himl->cCurImage - 1) {
2452 TRACE("Post image copy!\n");
2454 SelectObject (hdcBmp, hbmNewImage);
2455 imagelist_copy_images( himl, himl->hdcImage, hdcBmp, i + 1,
2456 (himl->cCurImage - i), i );
2458 if (himl->hbmMask) {
2459 SelectObject (hdcBmp, hbmNewMask);
2460 imagelist_copy_images( himl, himl->hdcMask, hdcBmp, i + 1,
2461 (himl->cCurImage - i), i );
2465 DeleteDC (hdcBmp);
2467 /* delete old images and insert new ones */
2468 SelectObject (himl->hdcImage, hbmNewImage);
2469 DeleteObject (himl->hbmImage);
2470 himl->hbmImage = hbmNewImage;
2471 if (himl->hbmMask) {
2472 SelectObject (himl->hdcMask, hbmNewMask);
2473 DeleteObject (himl->hbmMask);
2474 himl->hbmMask = hbmNewMask;
2477 himl->cCurImage--;
2480 return TRUE;
2484 /*************************************************************************
2485 * ImageList_Replace [COMCTL32.@]
2487 * Replaces an image in an image list with a new image.
2489 * PARAMS
2490 * himl [I] handle to image list
2491 * i [I] image index
2492 * hbmImage [I] handle to image bitmap
2493 * hbmMask [I] handle to mask bitmap. Can be NULL.
2495 * RETURNS
2496 * Success: TRUE
2497 * Failure: FALSE
2500 BOOL WINAPI
2501 ImageList_Replace (HIMAGELIST himl, INT i, HBITMAP hbmImage,
2502 HBITMAP hbmMask)
2504 HDC hdcImage;
2505 BITMAP bmp;
2506 POINT pt;
2508 TRACE("%p %d %p %p\n", himl, i, hbmImage, hbmMask);
2510 if (!is_valid(himl)) {
2511 ERR("Invalid image list handle!\n");
2512 return FALSE;
2515 if ((i >= himl->cMaxImage) || (i < 0)) {
2516 ERR("Invalid image index!\n");
2517 return FALSE;
2520 if (!GetObjectW(hbmImage, sizeof(BITMAP), &bmp))
2521 return FALSE;
2523 hdcImage = CreateCompatibleDC (0);
2525 /* Replace Image */
2526 SelectObject (hdcImage, hbmImage);
2528 if (add_with_alpha( himl, hdcImage, i, 1, bmp.bmWidth, bmp.bmHeight, hbmImage, hbmMask ))
2529 goto done;
2531 imagelist_point_from_index(himl, i, &pt);
2532 StretchBlt (himl->hdcImage, pt.x, pt.y, himl->cx, himl->cy,
2533 hdcImage, 0, 0, bmp.bmWidth, bmp.bmHeight, SRCCOPY);
2535 if (himl->hbmMask)
2537 HDC hdcTemp;
2538 HBITMAP hOldBitmapTemp;
2540 hdcTemp = CreateCompatibleDC(0);
2541 hOldBitmapTemp = SelectObject(hdcTemp, hbmMask);
2543 StretchBlt (himl->hdcMask, pt.x, pt.y, himl->cx, himl->cy,
2544 hdcTemp, 0, 0, bmp.bmWidth, bmp.bmHeight, SRCCOPY);
2545 SelectObject(hdcTemp, hOldBitmapTemp);
2546 DeleteDC(hdcTemp);
2548 /* Remove the background from the image
2550 BitBlt (himl->hdcImage, pt.x, pt.y, bmp.bmWidth, bmp.bmHeight,
2551 himl->hdcMask, pt.x, pt.y, 0x220326); /* NOTSRCAND */
2554 done:
2555 DeleteDC (hdcImage);
2557 return TRUE;
2561 /*************************************************************************
2562 * ImageList_ReplaceIcon [COMCTL32.@]
2564 * Replaces an image in an image list using an icon.
2566 * PARAMS
2567 * himl [I] handle to image list
2568 * i [I] image index
2569 * hIcon [I] handle to icon
2571 * RETURNS
2572 * Success: index of the replaced image
2573 * Failure: -1
2576 INT WINAPI
2577 ImageList_ReplaceIcon (HIMAGELIST himl, INT nIndex, HICON hIcon)
2579 HICON hBestFitIcon;
2580 ICONINFO ii;
2581 BITMAP bmp;
2582 BOOL ret;
2583 POINT pt;
2585 TRACE("(%p %d %p)\n", himl, nIndex, hIcon);
2587 if (!is_valid(himl)) {
2588 ERR("invalid image list\n");
2589 return -1;
2591 if ((nIndex >= himl->cMaxImage) || (nIndex < -1)) {
2592 ERR("invalid image index %d / %d\n", nIndex, himl->cMaxImage);
2593 return -1;
2596 hBestFitIcon = CopyImage(
2597 hIcon, IMAGE_ICON,
2598 himl->cx, himl->cy,
2599 LR_COPYFROMRESOURCE);
2600 /* the above will fail if the icon wasn't loaded from a resource, so try
2601 * again without LR_COPYFROMRESOURCE flag */
2602 if (!hBestFitIcon)
2603 hBestFitIcon = CopyImage(
2604 hIcon, IMAGE_ICON,
2605 himl->cx, himl->cy,
2607 if (!hBestFitIcon)
2608 return -1;
2610 if (nIndex == -1) {
2611 if (himl->cCurImage + 1 >= himl->cMaxImage)
2612 IMAGELIST_InternalExpandBitmaps(himl, 1);
2614 nIndex = himl->cCurImage;
2615 himl->cCurImage++;
2618 if (himl->has_alpha && GetIconInfo (hBestFitIcon, &ii))
2620 HDC hdcImage = CreateCompatibleDC( 0 );
2621 GetObjectW (ii.hbmMask, sizeof(BITMAP), &bmp);
2623 if (!ii.hbmColor)
2625 UINT height = bmp.bmHeight / 2;
2626 HDC hdcMask = CreateCompatibleDC( 0 );
2627 HBITMAP color = CreateBitmap( bmp.bmWidth, height, 1, 1, NULL );
2628 SelectObject( hdcImage, color );
2629 SelectObject( hdcMask, ii.hbmMask );
2630 BitBlt( hdcImage, 0, 0, bmp.bmWidth, height, hdcMask, 0, height, SRCCOPY );
2631 ret = add_with_alpha( himl, hdcImage, nIndex, 1, bmp.bmWidth, height, color, ii.hbmMask );
2632 DeleteDC( hdcMask );
2633 DeleteObject( color );
2635 else ret = add_with_alpha( himl, hdcImage, nIndex, 1, bmp.bmWidth, bmp.bmHeight,
2636 ii.hbmColor, ii.hbmMask );
2638 DeleteDC( hdcImage );
2639 DeleteObject (ii.hbmMask);
2640 if (ii.hbmColor) DeleteObject (ii.hbmColor);
2641 if (ret) goto done;
2644 imagelist_point_from_index(himl, nIndex, &pt);
2646 if (himl->hbmMask)
2648 DrawIconEx( himl->hdcImage, pt.x, pt.y, hBestFitIcon, himl->cx, himl->cy, 0, 0, DI_IMAGE );
2649 PatBlt( himl->hdcMask, pt.x, pt.y, himl->cx, himl->cy, WHITENESS );
2650 DrawIconEx( himl->hdcMask, pt.x, pt.y, hBestFitIcon, himl->cx, himl->cy, 0, 0, DI_MASK );
2652 else
2654 COLORREF color = himl->clrBk != CLR_NONE ? himl->clrBk : comctl32_color.clrWindow;
2655 HBRUSH brush = CreateSolidBrush( GetNearestColor( himl->hdcImage, color ));
2657 SelectObject( himl->hdcImage, brush );
2658 PatBlt( himl->hdcImage, pt.x, pt.y, himl->cx, himl->cy, PATCOPY );
2659 SelectObject( himl->hdcImage, GetStockObject(BLACK_BRUSH) );
2660 DeleteObject( brush );
2661 DrawIconEx( himl->hdcImage, pt.x, pt.y, hBestFitIcon, himl->cx, himl->cy, 0, 0, DI_NORMAL );
2664 done:
2665 DestroyIcon(hBestFitIcon);
2667 TRACE("Insert index = %d, himl->cCurImage = %d\n", nIndex, himl->cCurImage);
2668 return nIndex;
2672 /*************************************************************************
2673 * ImageList_SetBkColor [COMCTL32.@]
2675 * Sets the background color of an image list.
2677 * PARAMS
2678 * himl [I] handle to image list
2679 * clrBk [I] background color
2681 * RETURNS
2682 * Success: previous background color
2683 * Failure: CLR_NONE
2686 COLORREF WINAPI
2687 ImageList_SetBkColor (HIMAGELIST himl, COLORREF clrBk)
2689 COLORREF clrOldBk;
2691 if (!is_valid(himl))
2692 return CLR_NONE;
2694 clrOldBk = himl->clrBk;
2695 himl->clrBk = clrBk;
2696 return clrOldBk;
2700 /*************************************************************************
2701 * ImageList_SetDragCursorImage [COMCTL32.@]
2703 * Combines the specified image with the current drag image
2705 * PARAMS
2706 * himlDrag [I] handle to drag image list
2707 * iDrag [I] drag image index
2708 * dxHotspot [I] X position of the hot spot
2709 * dyHotspot [I] Y position of the hot spot
2711 * RETURNS
2712 * Success: TRUE
2713 * Failure: FALSE
2715 * NOTES
2716 * - The names dxHotspot, dyHotspot are misleading because they have nothing
2717 * to do with a hotspot but are only the offset of the origin of the new
2718 * image relative to the origin of the old image.
2720 * - When this function is called and the drag image is visible, a
2721 * short flickering occurs but this matches the Win9x behavior. It is
2722 * possible to fix the flickering using code like in ImageList_DragMove.
2725 BOOL WINAPI
2726 ImageList_SetDragCursorImage (HIMAGELIST himlDrag, INT iDrag,
2727 INT dxHotspot, INT dyHotspot)
2729 HIMAGELIST himlTemp;
2730 BOOL visible;
2732 if (!is_valid(InternalDrag.himl) || !is_valid(himlDrag))
2733 return FALSE;
2735 TRACE(" dxH=%d dyH=%d nX=%d nY=%d\n",
2736 dxHotspot, dyHotspot, InternalDrag.dxHotspot, InternalDrag.dyHotspot);
2738 visible = InternalDrag.bShow;
2740 himlTemp = ImageList_Merge (InternalDrag.himlNoCursor, 0, himlDrag, iDrag,
2741 dxHotspot, dyHotspot);
2743 if (visible) {
2744 /* hide the drag image */
2745 ImageList_DragShowNolock(FALSE);
2747 if ((InternalDrag.himl->cx != himlTemp->cx) ||
2748 (InternalDrag.himl->cy != himlTemp->cy)) {
2749 /* the size of the drag image changed, invalidate the buffer */
2750 DeleteObject(InternalDrag.hbmBg);
2751 InternalDrag.hbmBg = 0;
2754 if (InternalDrag.himl != InternalDrag.himlNoCursor)
2755 ImageList_Destroy (InternalDrag.himl);
2756 InternalDrag.himl = himlTemp;
2758 if (visible) {
2759 /* show the drag image */
2760 ImageList_DragShowNolock(TRUE);
2763 return TRUE;
2767 /*************************************************************************
2768 * ImageList_SetFilter [COMCTL32.@]
2770 * Sets a filter (or does something completely different)!!???
2771 * It removes 12 Bytes from the stack (3 Parameters).
2773 * PARAMS
2774 * himl [I] SHOULD be a handle to image list
2775 * i [I] COULD be an index?
2776 * dwFilter [I] ???
2778 * RETURNS
2779 * Success: TRUE ???
2780 * Failure: FALSE ???
2782 * BUGS
2783 * This is an UNDOCUMENTED function!!!!
2784 * empty stub.
2787 BOOL WINAPI
2788 ImageList_SetFilter (HIMAGELIST himl, INT i, DWORD dwFilter)
2790 FIXME("(%p 0x%x 0x%x):empty stub!\n", himl, i, dwFilter);
2792 return FALSE;
2796 /*************************************************************************
2797 * ImageList_SetFlags [COMCTL32.@]
2799 * Sets the image list flags.
2801 * PARAMS
2802 * himl [I] Handle to image list
2803 * flags [I] Flags to set
2805 * RETURNS
2806 * Old flags?
2808 * BUGS
2809 * Stub.
2812 DWORD WINAPI
2813 ImageList_SetFlags(HIMAGELIST himl, DWORD flags)
2815 FIXME("(%p %08x):empty stub\n", himl, flags);
2816 return 0;
2820 /*************************************************************************
2821 * ImageList_SetIconSize [COMCTL32.@]
2823 * Sets the image size of the bitmap and deletes all images.
2825 * PARAMS
2826 * himl [I] handle to image list
2827 * cx [I] image width
2828 * cy [I] image height
2830 * RETURNS
2831 * Success: TRUE
2832 * Failure: FALSE
2835 BOOL WINAPI
2836 ImageList_SetIconSize (HIMAGELIST himl, INT cx, INT cy)
2838 INT nCount;
2839 HBITMAP hbmNew;
2841 if (!is_valid(himl))
2842 return FALSE;
2844 /* remove all images */
2845 himl->cMaxImage = himl->cInitial + 1;
2846 himl->cCurImage = 0;
2847 himl->cx = cx;
2848 himl->cy = cy;
2850 /* initialize overlay mask indices */
2851 for (nCount = 0; nCount < MAX_OVERLAYIMAGE; nCount++)
2852 himl->nOvlIdx[nCount] = -1;
2854 hbmNew = ImageList_CreateImage(himl->hdcImage, himl, himl->cMaxImage);
2855 SelectObject (himl->hdcImage, hbmNew);
2856 DeleteObject (himl->hbmImage);
2857 himl->hbmImage = hbmNew;
2859 if (himl->hbmMask) {
2860 SIZE sz;
2861 imagelist_get_bitmap_size(himl, himl->cMaxImage, &sz);
2862 hbmNew = CreateBitmap (sz.cx, sz.cy, 1, 1, NULL);
2863 SelectObject (himl->hdcMask, hbmNew);
2864 DeleteObject (himl->hbmMask);
2865 himl->hbmMask = hbmNew;
2868 return TRUE;
2872 /*************************************************************************
2873 * ImageList_SetImageCount [COMCTL32.@]
2875 * Resizes an image list to the specified number of images.
2877 * PARAMS
2878 * himl [I] handle to image list
2879 * iImageCount [I] number of images in the image list
2881 * RETURNS
2882 * Success: TRUE
2883 * Failure: FALSE
2886 BOOL WINAPI
2887 ImageList_SetImageCount (HIMAGELIST himl, UINT iImageCount)
2889 HDC hdcBitmap;
2890 HBITMAP hbmNewBitmap, hbmOld;
2891 INT nNewCount, nCopyCount;
2893 TRACE("%p %d\n",himl,iImageCount);
2895 if (!is_valid(himl))
2896 return FALSE;
2898 nNewCount = iImageCount + 1;
2899 nCopyCount = min(himl->cCurImage, iImageCount);
2901 hdcBitmap = CreateCompatibleDC (0);
2903 hbmNewBitmap = ImageList_CreateImage(hdcBitmap, himl, nNewCount);
2905 if (hbmNewBitmap != 0)
2907 hbmOld = SelectObject (hdcBitmap, hbmNewBitmap);
2908 imagelist_copy_images( himl, himl->hdcImage, hdcBitmap, 0, nCopyCount, 0 );
2909 SelectObject (hdcBitmap, hbmOld);
2911 /* FIXME: delete 'empty' image space? */
2913 SelectObject (himl->hdcImage, hbmNewBitmap);
2914 DeleteObject (himl->hbmImage);
2915 himl->hbmImage = hbmNewBitmap;
2917 else
2918 ERR("Could not create new image bitmap!\n");
2920 if (himl->hbmMask)
2922 SIZE sz;
2923 imagelist_get_bitmap_size( himl, nNewCount, &sz );
2924 hbmNewBitmap = CreateBitmap (sz.cx, sz.cy, 1, 1, NULL);
2925 if (hbmNewBitmap != 0)
2927 hbmOld = SelectObject (hdcBitmap, hbmNewBitmap);
2928 imagelist_copy_images( himl, himl->hdcMask, hdcBitmap, 0, nCopyCount, 0 );
2929 SelectObject (hdcBitmap, hbmOld);
2931 /* FIXME: delete 'empty' image space? */
2933 SelectObject (himl->hdcMask, hbmNewBitmap);
2934 DeleteObject (himl->hbmMask);
2935 himl->hbmMask = hbmNewBitmap;
2937 else
2938 ERR("Could not create new mask bitmap!\n");
2941 DeleteDC (hdcBitmap);
2943 if (himl->has_alpha)
2945 char *new_alpha = HeapReAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, himl->has_alpha, nNewCount );
2946 if (new_alpha) himl->has_alpha = new_alpha;
2947 else
2949 HeapFree( GetProcessHeap(), 0, himl->has_alpha );
2950 himl->has_alpha = NULL;
2954 /* Update max image count and current image count */
2955 himl->cMaxImage = nNewCount;
2956 himl->cCurImage = iImageCount;
2958 return TRUE;
2962 /*************************************************************************
2963 * ImageList_SetOverlayImage [COMCTL32.@]
2965 * Assigns an overlay mask index to an existing image in an image list.
2967 * PARAMS
2968 * himl [I] handle to image list
2969 * iImage [I] image index
2970 * iOverlay [I] overlay mask index
2972 * RETURNS
2973 * Success: TRUE
2974 * Failure: FALSE
2977 BOOL WINAPI
2978 ImageList_SetOverlayImage (HIMAGELIST himl, INT iImage, INT iOverlay)
2980 if (!is_valid(himl))
2981 return FALSE;
2982 if ((iOverlay < 1) || (iOverlay > MAX_OVERLAYIMAGE))
2983 return FALSE;
2984 if ((iImage!=-1) && ((iImage < 0) || (iImage > himl->cCurImage)))
2985 return FALSE;
2986 himl->nOvlIdx[iOverlay - 1] = iImage;
2987 return TRUE;
2992 /* helper for ImageList_Write - write bitmap to pstm
2993 * currently everything is written as 24 bit RGB, except masks
2995 static BOOL _write_bitmap(HBITMAP hBitmap, IStream *pstm)
2997 LPBITMAPFILEHEADER bmfh;
2998 LPBITMAPINFOHEADER bmih;
2999 LPBYTE data = NULL, lpBits;
3000 BITMAP bm;
3001 INT bitCount, sizeImage, offBits, totalSize;
3002 HDC xdc;
3003 BOOL result = FALSE;
3005 if (!GetObjectW(hBitmap, sizeof(BITMAP), &bm))
3006 return FALSE;
3008 bitCount = bm.bmBitsPixel;
3009 sizeImage = get_dib_stride(bm.bmWidth, bitCount) * bm.bmHeight;
3011 totalSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
3012 if(bitCount <= 8)
3013 totalSize += (1 << bitCount) * sizeof(RGBQUAD);
3014 offBits = totalSize;
3015 totalSize += sizeImage;
3017 data = Alloc(totalSize);
3018 bmfh = (LPBITMAPFILEHEADER)data;
3019 bmih = (LPBITMAPINFOHEADER)(data + sizeof(BITMAPFILEHEADER));
3020 lpBits = data + offBits;
3022 /* setup BITMAPFILEHEADER */
3023 bmfh->bfType = (('M' << 8) | 'B');
3024 bmfh->bfSize = offBits;
3025 bmfh->bfReserved1 = 0;
3026 bmfh->bfReserved2 = 0;
3027 bmfh->bfOffBits = offBits;
3029 /* setup BITMAPINFOHEADER */
3030 bmih->biSize = sizeof(BITMAPINFOHEADER);
3031 bmih->biWidth = bm.bmWidth;
3032 bmih->biHeight = bm.bmHeight;
3033 bmih->biPlanes = 1;
3034 bmih->biBitCount = bitCount;
3035 bmih->biCompression = BI_RGB;
3036 bmih->biSizeImage = sizeImage;
3037 bmih->biXPelsPerMeter = 0;
3038 bmih->biYPelsPerMeter = 0;
3039 bmih->biClrUsed = 0;
3040 bmih->biClrImportant = 0;
3042 xdc = GetDC(0);
3043 result = GetDIBits(xdc, hBitmap, 0, bm.bmHeight, lpBits, (BITMAPINFO *)bmih, DIB_RGB_COLORS) == bm.bmHeight;
3044 ReleaseDC(0, xdc);
3045 if (!result)
3046 goto failed;
3048 TRACE("width %u, height %u, planes %u, bpp %u\n",
3049 bmih->biWidth, bmih->biHeight,
3050 bmih->biPlanes, bmih->biBitCount);
3052 if(FAILED(IStream_Write(pstm, data, totalSize, NULL)))
3053 goto failed;
3055 result = TRUE;
3057 failed:
3058 Free(data);
3060 return result;
3064 /*************************************************************************
3065 * ImageList_Write [COMCTL32.@]
3067 * Writes an image list to a stream.
3069 * PARAMS
3070 * himl [I] handle to image list
3071 * pstm [O] Pointer to a stream.
3073 * RETURNS
3074 * Success: TRUE
3075 * Failure: FALSE
3077 * BUGS
3078 * probably.
3081 BOOL WINAPI ImageList_Write(HIMAGELIST himl, IStream *pstm)
3083 ILHEAD ilHead;
3084 int i;
3086 TRACE("%p %p\n", himl, pstm);
3088 if (!is_valid(himl))
3089 return FALSE;
3091 ilHead.usMagic = (('L' << 8) | 'I');
3092 ilHead.usVersion = 0x101;
3093 ilHead.cCurImage = himl->cCurImage;
3094 ilHead.cMaxImage = himl->cMaxImage;
3095 ilHead.cGrow = himl->cGrow;
3096 ilHead.cx = himl->cx;
3097 ilHead.cy = himl->cy;
3098 ilHead.bkcolor = himl->clrBk;
3099 ilHead.flags = himl->flags;
3100 for(i = 0; i < 4; i++) {
3101 ilHead.ovls[i] = himl->nOvlIdx[i];
3104 TRACE("cx %u, cy %u, flags 0x04%x, cCurImage %u, cMaxImage %u\n",
3105 ilHead.cx, ilHead.cy, ilHead.flags, ilHead.cCurImage, ilHead.cMaxImage);
3107 if(FAILED(IStream_Write(pstm, &ilHead, sizeof(ILHEAD), NULL)))
3108 return FALSE;
3110 /* write the bitmap */
3111 if(!_write_bitmap(himl->hbmImage, pstm))
3112 return FALSE;
3114 /* write the mask if we have one */
3115 if(himl->flags & ILC_MASK) {
3116 if(!_write_bitmap(himl->hbmMask, pstm))
3117 return FALSE;
3120 return TRUE;
3124 static HBITMAP ImageList_CreateImage(HDC hdc, HIMAGELIST himl, UINT count)
3126 HBITMAP hbmNewBitmap;
3127 UINT ilc = (himl->flags & 0xFE);
3128 SIZE sz;
3130 imagelist_get_bitmap_size( himl, count, &sz );
3132 if ((ilc >= ILC_COLOR4 && ilc <= ILC_COLOR32) || ilc == ILC_COLOR)
3134 char buffer[sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD)];
3135 BITMAPINFO *bmi = (BITMAPINFO *)buffer;
3137 TRACE("Creating DIBSection %d x %d, %d Bits per Pixel\n",
3138 sz.cx, sz.cy, himl->uBitsPixel);
3140 memset( buffer, 0, sizeof(buffer) );
3141 bmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
3142 bmi->bmiHeader.biWidth = sz.cx;
3143 bmi->bmiHeader.biHeight = sz.cy;
3144 bmi->bmiHeader.biPlanes = 1;
3145 bmi->bmiHeader.biBitCount = himl->uBitsPixel;
3146 bmi->bmiHeader.biCompression = BI_RGB;
3148 if (himl->uBitsPixel <= ILC_COLOR8)
3150 if (!himl->color_table_set)
3152 /* retrieve the default color map */
3153 HBITMAP tmp = CreateBitmap( 1, 1, 1, 1, NULL );
3154 GetDIBits( hdc, tmp, 0, 0, NULL, bmi, DIB_RGB_COLORS );
3155 DeleteObject( tmp );
3156 if (ilc == ILC_COLOR4)
3158 RGBQUAD tmp;
3159 tmp = bmi->bmiColors[7];
3160 bmi->bmiColors[7] = bmi->bmiColors[8];
3161 bmi->bmiColors[8] = tmp;
3164 else
3166 GetDIBColorTable(himl->hdcImage, 0, 1 << himl->uBitsPixel, bmi->bmiColors);
3169 hbmNewBitmap = CreateDIBSection(hdc, bmi, DIB_RGB_COLORS, NULL, 0, 0);
3171 else /*if (ilc == ILC_COLORDDB)*/
3173 TRACE("Creating Bitmap: %d Bits per Pixel\n", himl->uBitsPixel);
3175 hbmNewBitmap = CreateBitmap (sz.cx, sz.cy, 1, himl->uBitsPixel, NULL);
3177 TRACE("returning %p\n", hbmNewBitmap);
3178 return hbmNewBitmap;
3181 /*************************************************************************
3182 * ImageList_SetColorTable [COMCTL32.@]
3184 * Sets the color table of an image list.
3186 * PARAMS
3187 * himl [I] Handle to the image list.
3188 * uStartIndex [I] The first index to set.
3189 * cEntries [I] Number of entries to set.
3190 * prgb [I] New color information for color table for the image list.
3192 * RETURNS
3193 * Success: Number of entries in the table that were set.
3194 * Failure: Zero.
3196 * SEE
3197 * ImageList_Create(), SetDIBColorTable()
3200 UINT WINAPI
3201 ImageList_SetColorTable(HIMAGELIST himl, UINT uStartIndex, UINT cEntries, const RGBQUAD *prgb)
3203 TRACE("(%p, %d, %d, %p)\n", himl, uStartIndex, cEntries, prgb);
3204 himl->color_table_set = TRUE;
3205 return SetDIBColorTable(himl->hdcImage, uStartIndex, cEntries, prgb);
3208 /*************************************************************************
3209 * ImageList_CoCreateInstance [COMCTL32.@]
3211 * Creates a new imagelist instance and returns an interface pointer to it.
3213 * PARAMS
3214 * rclsid [I] A reference to the CLSID (CLSID_ImageList).
3215 * punkOuter [I] Pointer to IUnknown interface for aggregation, if desired
3216 * riid [I] Identifier of the requested interface.
3217 * ppv [O] Returns the address of the pointer requested, or NULL.
3219 * RETURNS
3220 * Success: S_OK.
3221 * Failure: Error value.
3223 HRESULT WINAPI
3224 ImageList_CoCreateInstance (REFCLSID rclsid, const IUnknown *punkOuter, REFIID riid, void **ppv)
3226 TRACE("(%s,%p,%s,%p)\n", debugstr_guid(rclsid), punkOuter, debugstr_guid(riid), ppv);
3228 if (!IsEqualCLSID(&CLSID_ImageList, rclsid))
3229 return E_NOINTERFACE;
3231 return ImageListImpl_CreateInstance(punkOuter, riid, ppv);
3235 /*************************************************************************
3236 * IImageList implementation
3239 static HRESULT WINAPI ImageListImpl_QueryInterface(IImageList2 *iface,
3240 REFIID iid, void **ppv)
3242 HIMAGELIST imgl = impl_from_IImageList2(iface);
3244 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
3246 if (!ppv) return E_INVALIDARG;
3248 if (IsEqualIID(&IID_IUnknown, iid) ||
3249 IsEqualIID(&IID_IImageList, iid) ||
3250 IsEqualIID(&IID_IImageList2, iid))
3252 *ppv = &imgl->IImageList2_iface;
3254 else
3256 *ppv = NULL;
3257 return E_NOINTERFACE;
3260 IImageList2_AddRef(iface);
3261 return S_OK;
3264 static ULONG WINAPI ImageListImpl_AddRef(IImageList2 *iface)
3266 HIMAGELIST imgl = impl_from_IImageList2(iface);
3267 ULONG ref = InterlockedIncrement(&imgl->ref);
3269 TRACE("(%p) refcount=%u\n", iface, ref);
3270 return ref;
3273 static ULONG WINAPI ImageListImpl_Release(IImageList2 *iface)
3275 HIMAGELIST This = impl_from_IImageList2(iface);
3276 ULONG ref = InterlockedDecrement(&This->ref);
3278 TRACE("(%p) refcount=%u\n", iface, ref);
3280 if (ref == 0)
3282 /* delete image bitmaps */
3283 if (This->hbmImage) DeleteObject (This->hbmImage);
3284 if (This->hbmMask) DeleteObject (This->hbmMask);
3286 /* delete image & mask DCs */
3287 if (This->hdcImage) DeleteDC (This->hdcImage);
3288 if (This->hdcMask) DeleteDC (This->hdcMask);
3290 /* delete blending brushes */
3291 if (This->hbrBlend25) DeleteObject (This->hbrBlend25);
3292 if (This->hbrBlend50) DeleteObject (This->hbrBlend50);
3294 This->IImageList2_iface.lpVtbl = NULL;
3295 HeapFree(GetProcessHeap(), 0, This->has_alpha);
3296 HeapFree(GetProcessHeap(), 0, This);
3299 return ref;
3302 static HRESULT WINAPI ImageListImpl_Add(IImageList2 *iface, HBITMAP hbmImage,
3303 HBITMAP hbmMask, int *pi)
3305 HIMAGELIST imgl = impl_from_IImageList2(iface);
3306 int ret;
3308 if (!pi)
3309 return E_FAIL;
3311 ret = ImageList_Add(imgl, hbmImage, hbmMask);
3313 if (ret == -1)
3314 return E_FAIL;
3316 *pi = ret;
3317 return S_OK;
3320 static HRESULT WINAPI ImageListImpl_ReplaceIcon(IImageList2 *iface, int i,
3321 HICON hicon, int *pi)
3323 HIMAGELIST imgl = impl_from_IImageList2(iface);
3324 int ret;
3326 if (!pi)
3327 return E_FAIL;
3329 ret = ImageList_ReplaceIcon(imgl, i, hicon);
3331 if (ret == -1)
3332 return E_FAIL;
3334 *pi = ret;
3335 return S_OK;
3338 static HRESULT WINAPI ImageListImpl_SetOverlayImage(IImageList2 *iface,
3339 int iImage, int iOverlay)
3341 HIMAGELIST imgl = impl_from_IImageList2(iface);
3342 return ImageList_SetOverlayImage(imgl, iImage, iOverlay) ? S_OK : E_FAIL;
3345 static HRESULT WINAPI ImageListImpl_Replace(IImageList2 *iface, int i,
3346 HBITMAP hbmImage, HBITMAP hbmMask)
3348 HIMAGELIST imgl = impl_from_IImageList2(iface);
3349 return ImageList_Replace(imgl, i, hbmImage, hbmMask) ? S_OK : E_FAIL;
3352 static HRESULT WINAPI ImageListImpl_AddMasked(IImageList2 *iface, HBITMAP hbmImage,
3353 COLORREF crMask, int *pi)
3355 HIMAGELIST imgl = impl_from_IImageList2(iface);
3356 int ret;
3358 if (!pi)
3359 return E_FAIL;
3361 ret = ImageList_AddMasked(imgl, hbmImage, crMask);
3363 if (ret == -1)
3364 return E_FAIL;
3366 *pi = ret;
3367 return S_OK;
3370 static HRESULT WINAPI ImageListImpl_Draw(IImageList2 *iface,
3371 IMAGELISTDRAWPARAMS *pimldp)
3373 HIMAGELIST imgl = impl_from_IImageList2(iface);
3374 HIMAGELIST old_himl;
3375 int ret;
3377 /* As far as I can tell, Windows simply ignores the contents of pimldp->himl
3378 so we shall simulate the same */
3379 old_himl = pimldp->himl;
3380 pimldp->himl = imgl;
3382 ret = ImageList_DrawIndirect(pimldp);
3384 pimldp->himl = old_himl;
3385 return ret ? S_OK : E_INVALIDARG;
3388 static HRESULT WINAPI ImageListImpl_Remove(IImageList2 *iface, int i)
3390 HIMAGELIST imgl = impl_from_IImageList2(iface);
3391 return (ImageList_Remove(imgl, i) == 0) ? E_INVALIDARG : S_OK;
3394 static HRESULT WINAPI ImageListImpl_GetIcon(IImageList2 *iface, int i, UINT flags,
3395 HICON *picon)
3397 HIMAGELIST imgl = impl_from_IImageList2(iface);
3398 HICON hIcon;
3400 if (!picon)
3401 return E_FAIL;
3403 hIcon = ImageList_GetIcon(imgl, i, flags);
3405 if (hIcon == NULL)
3406 return E_FAIL;
3408 *picon = hIcon;
3409 return S_OK;
3412 static HRESULT WINAPI ImageListImpl_GetImageInfo(IImageList2 *iface, int i,
3413 IMAGEINFO *pImageInfo)
3415 HIMAGELIST imgl = impl_from_IImageList2(iface);
3416 return ImageList_GetImageInfo(imgl, i, pImageInfo) ? S_OK : E_FAIL;
3419 static HRESULT WINAPI ImageListImpl_Copy(IImageList2 *iface, int dst_index,
3420 IUnknown *unk_src, int src_index, UINT flags)
3422 HIMAGELIST imgl = impl_from_IImageList2(iface);
3423 IImageList *src = NULL;
3424 HRESULT ret;
3426 if (!unk_src)
3427 return E_FAIL;
3429 /* TODO: Add test for IID_ImageList2 too */
3430 if (FAILED(IUnknown_QueryInterface(unk_src, &IID_IImageList,
3431 (void **) &src)))
3432 return E_FAIL;
3434 if (ImageList_Copy(imgl, dst_index, (HIMAGELIST) src, src_index, flags))
3435 ret = S_OK;
3436 else
3437 ret = E_FAIL;
3439 IImageList_Release(src);
3440 return ret;
3443 static HRESULT WINAPI ImageListImpl_Merge(IImageList2 *iface, int i1,
3444 IUnknown *punk2, int i2, int dx, int dy, REFIID riid, void **ppv)
3446 HIMAGELIST imgl = impl_from_IImageList2(iface);
3447 IImageList *iml2 = NULL;
3448 HIMAGELIST merged;
3449 HRESULT ret = E_FAIL;
3451 TRACE("(%p)->(%d %p %d %d %d %s %p)\n", iface, i1, punk2, i2, dx, dy, debugstr_guid(riid), ppv);
3453 /* TODO: Add test for IID_ImageList2 too */
3454 if (FAILED(IUnknown_QueryInterface(punk2, &IID_IImageList,
3455 (void **) &iml2)))
3456 return E_FAIL;
3458 merged = ImageList_Merge(imgl, i1, (HIMAGELIST) iml2, i2, dx, dy);
3460 /* Get the interface for the new image list */
3461 if (merged)
3463 ret = HIMAGELIST_QueryInterface(merged, riid, ppv);
3464 ImageList_Destroy(merged);
3467 IImageList_Release(iml2);
3468 return ret;
3471 static HRESULT WINAPI ImageListImpl_Clone(IImageList2 *iface, REFIID riid, void **ppv)
3473 HIMAGELIST imgl = impl_from_IImageList2(iface);
3474 HIMAGELIST clone;
3475 HRESULT ret = E_FAIL;
3477 TRACE("(%p)->(%s %p)\n", iface, debugstr_guid(riid), ppv);
3479 clone = ImageList_Duplicate(imgl);
3481 /* Get the interface for the new image list */
3482 if (clone)
3484 ret = HIMAGELIST_QueryInterface(clone, riid, ppv);
3485 ImageList_Destroy(clone);
3488 return ret;
3491 static HRESULT WINAPI ImageListImpl_GetImageRect(IImageList2 *iface, int i,
3492 RECT *prc)
3494 HIMAGELIST imgl = impl_from_IImageList2(iface);
3495 IMAGEINFO info;
3497 if (!prc)
3498 return E_FAIL;
3500 if (!ImageList_GetImageInfo(imgl, i, &info))
3501 return E_FAIL;
3503 *prc = info.rcImage;
3505 return S_OK;
3508 static HRESULT WINAPI ImageListImpl_GetIconSize(IImageList2 *iface, int *cx,
3509 int *cy)
3511 HIMAGELIST imgl = impl_from_IImageList2(iface);
3512 return ImageList_GetIconSize(imgl, cx, cy) ? S_OK : E_INVALIDARG;
3515 static HRESULT WINAPI ImageListImpl_SetIconSize(IImageList2 *iface, int cx,
3516 int cy)
3518 HIMAGELIST imgl = impl_from_IImageList2(iface);
3519 return ImageList_SetIconSize(imgl, cx, cy) ? S_OK : E_FAIL;
3522 static HRESULT WINAPI ImageListImpl_GetImageCount(IImageList2 *iface, int *pi)
3524 HIMAGELIST imgl = impl_from_IImageList2(iface);
3525 *pi = ImageList_GetImageCount(imgl);
3526 return S_OK;
3529 static HRESULT WINAPI ImageListImpl_SetImageCount(IImageList2 *iface, UINT count)
3531 HIMAGELIST imgl = impl_from_IImageList2(iface);
3532 return ImageList_SetImageCount(imgl, count) ? S_OK : E_FAIL;
3535 static HRESULT WINAPI ImageListImpl_SetBkColor(IImageList2 *iface, COLORREF clrBk,
3536 COLORREF *pclr)
3538 HIMAGELIST imgl = impl_from_IImageList2(iface);
3539 *pclr = ImageList_SetBkColor(imgl, clrBk);
3540 return S_OK;
3543 static HRESULT WINAPI ImageListImpl_GetBkColor(IImageList2 *iface, COLORREF *pclr)
3545 HIMAGELIST imgl = impl_from_IImageList2(iface);
3546 *pclr = ImageList_GetBkColor(imgl);
3547 return S_OK;
3550 static HRESULT WINAPI ImageListImpl_BeginDrag(IImageList2 *iface, int iTrack,
3551 int dxHotspot, int dyHotspot)
3553 HIMAGELIST imgl = impl_from_IImageList2(iface);
3554 return ImageList_BeginDrag(imgl, iTrack, dxHotspot, dyHotspot) ? S_OK : E_FAIL;
3557 static HRESULT WINAPI ImageListImpl_EndDrag(IImageList2 *iface)
3559 ImageList_EndDrag();
3560 return S_OK;
3563 static HRESULT WINAPI ImageListImpl_DragEnter(IImageList2 *iface, HWND hwndLock,
3564 int x, int y)
3566 return ImageList_DragEnter(hwndLock, x, y) ? S_OK : E_FAIL;
3569 static HRESULT WINAPI ImageListImpl_DragLeave(IImageList2 *iface, HWND hwndLock)
3571 return ImageList_DragLeave(hwndLock) ? S_OK : E_FAIL;
3574 static HRESULT WINAPI ImageListImpl_DragMove(IImageList2 *iface, int x, int y)
3576 return ImageList_DragMove(x, y) ? S_OK : E_FAIL;
3579 static HRESULT WINAPI ImageListImpl_SetDragCursorImage(IImageList2 *iface,
3580 IUnknown *punk, int iDrag, int dxHotspot, int dyHotspot)
3582 IImageList *iml2 = NULL;
3583 BOOL ret;
3585 if (!punk)
3586 return E_FAIL;
3588 /* TODO: Add test for IID_ImageList2 too */
3589 if (FAILED(IUnknown_QueryInterface(punk, &IID_IImageList,
3590 (void **) &iml2)))
3591 return E_FAIL;
3593 ret = ImageList_SetDragCursorImage((HIMAGELIST) iml2, iDrag, dxHotspot,
3594 dyHotspot);
3596 IImageList_Release(iml2);
3598 return ret ? S_OK : E_FAIL;
3601 static HRESULT WINAPI ImageListImpl_DragShowNolock(IImageList2 *iface, BOOL fShow)
3603 return ImageList_DragShowNolock(fShow) ? S_OK : E_FAIL;
3606 static HRESULT WINAPI ImageListImpl_GetDragImage(IImageList2 *iface, POINT *ppt,
3607 POINT *pptHotspot, REFIID riid, PVOID *ppv)
3609 HRESULT ret = E_FAIL;
3610 HIMAGELIST hNew;
3612 if (!ppv)
3613 return E_FAIL;
3615 hNew = ImageList_GetDragImage(ppt, pptHotspot);
3617 /* Get the interface for the new image list */
3618 if (hNew)
3620 IImageList *idrag = (IImageList*)hNew;
3622 ret = HIMAGELIST_QueryInterface(hNew, riid, ppv);
3623 IImageList_Release(idrag);
3626 return ret;
3629 static HRESULT WINAPI ImageListImpl_GetItemFlags(IImageList2 *iface, int i,
3630 DWORD *dwFlags)
3632 FIXME("STUB: %p %d %p\n", iface, i, dwFlags);
3633 return E_NOTIMPL;
3636 static HRESULT WINAPI ImageListImpl_GetOverlayImage(IImageList2 *iface, int iOverlay,
3637 int *piIndex)
3639 HIMAGELIST This = impl_from_IImageList2(iface);
3640 int i;
3642 if ((iOverlay < 0) || (iOverlay > This->cCurImage))
3643 return E_FAIL;
3645 for (i = 0; i < MAX_OVERLAYIMAGE; i++)
3647 if (This->nOvlIdx[i] == iOverlay)
3649 *piIndex = i + 1;
3650 return S_OK;
3654 return E_FAIL;
3657 static HRESULT WINAPI ImageListImpl_Resize(IImageList2 *iface, INT cx, INT cy)
3659 FIXME("(%p)->(%d %d): stub\n", iface, cx, cy);
3660 return E_NOTIMPL;
3663 static HRESULT WINAPI ImageListImpl_GetOriginalSize(IImageList2 *iface, INT image, DWORD flags, INT *cx, INT *cy)
3665 FIXME("(%p)->(%d %x %p %p): stub\n", iface, image, flags, cx, cy);
3666 return E_NOTIMPL;
3669 static HRESULT WINAPI ImageListImpl_SetOriginalSize(IImageList2 *iface, INT image, INT cx, INT cy)
3671 FIXME("(%p)->(%d %d %d): stub\n", iface, image, cx, cy);
3672 return E_NOTIMPL;
3675 static HRESULT WINAPI ImageListImpl_SetCallback(IImageList2 *iface, IUnknown *callback)
3677 FIXME("(%p)->(%p): stub\n", iface, callback);
3678 return E_NOTIMPL;
3681 static HRESULT WINAPI ImageListImpl_GetCallback(IImageList2 *iface, REFIID riid, void **ppv)
3683 FIXME("(%p)->(%s %p): stub\n", iface, debugstr_guid(riid), ppv);
3684 return E_NOTIMPL;
3687 static HRESULT WINAPI ImageListImpl_ForceImagePresent(IImageList2 *iface, INT image, DWORD flags)
3689 FIXME("(%p)->(%d %x): stub\n", iface, image, flags);
3690 return E_NOTIMPL;
3693 static HRESULT WINAPI ImageListImpl_DiscardImages(IImageList2 *iface, INT first_image, INT last_image, DWORD flags)
3695 FIXME("(%p)->(%d %d %x): stub\n", iface, first_image, last_image, flags);
3696 return E_NOTIMPL;
3699 static HRESULT WINAPI ImageListImpl_PreloadImages(IImageList2 *iface, IMAGELISTDRAWPARAMS *params)
3701 FIXME("(%p)->(%p): stub\n", iface, params);
3702 return E_NOTIMPL;
3705 static HRESULT WINAPI ImageListImpl_GetStatistics(IImageList2 *iface, IMAGELISTSTATS *stats)
3707 FIXME("(%p)->(%p): stub\n", iface, stats);
3708 return E_NOTIMPL;
3711 static HRESULT WINAPI ImageListImpl_Initialize(IImageList2 *iface, INT cx, INT cy, UINT flags, INT initial, INT grow)
3713 FIXME("(%p)->(%d %d %d %d %d): stub\n", iface, cx, cy, flags, initial, grow);
3714 return E_NOTIMPL;
3717 static HRESULT WINAPI ImageListImpl_Replace2(IImageList2 *iface, INT i, HBITMAP image, HBITMAP mask, IUnknown *unk, DWORD flags)
3719 FIXME("(%p)->(%d %p %p %p %x): stub\n", iface, i, image, mask, unk, flags);
3720 return E_NOTIMPL;
3723 static HRESULT WINAPI ImageListImpl_ReplaceFromImageList(IImageList2 *iface, INT i, IImageList *imagelist, INT src,
3724 IUnknown *unk, DWORD flags)
3726 FIXME("(%p)->(%d %p %d %p %x): stub\n", iface, i, imagelist, src, unk, flags);
3727 return E_NOTIMPL;
3730 static const IImageList2Vtbl ImageListImpl_Vtbl = {
3731 ImageListImpl_QueryInterface,
3732 ImageListImpl_AddRef,
3733 ImageListImpl_Release,
3734 ImageListImpl_Add,
3735 ImageListImpl_ReplaceIcon,
3736 ImageListImpl_SetOverlayImage,
3737 ImageListImpl_Replace,
3738 ImageListImpl_AddMasked,
3739 ImageListImpl_Draw,
3740 ImageListImpl_Remove,
3741 ImageListImpl_GetIcon,
3742 ImageListImpl_GetImageInfo,
3743 ImageListImpl_Copy,
3744 ImageListImpl_Merge,
3745 ImageListImpl_Clone,
3746 ImageListImpl_GetImageRect,
3747 ImageListImpl_GetIconSize,
3748 ImageListImpl_SetIconSize,
3749 ImageListImpl_GetImageCount,
3750 ImageListImpl_SetImageCount,
3751 ImageListImpl_SetBkColor,
3752 ImageListImpl_GetBkColor,
3753 ImageListImpl_BeginDrag,
3754 ImageListImpl_EndDrag,
3755 ImageListImpl_DragEnter,
3756 ImageListImpl_DragLeave,
3757 ImageListImpl_DragMove,
3758 ImageListImpl_SetDragCursorImage,
3759 ImageListImpl_DragShowNolock,
3760 ImageListImpl_GetDragImage,
3761 ImageListImpl_GetItemFlags,
3762 ImageListImpl_GetOverlayImage,
3763 ImageListImpl_Resize,
3764 ImageListImpl_GetOriginalSize,
3765 ImageListImpl_SetOriginalSize,
3766 ImageListImpl_SetCallback,
3767 ImageListImpl_GetCallback,
3768 ImageListImpl_ForceImagePresent,
3769 ImageListImpl_DiscardImages,
3770 ImageListImpl_PreloadImages,
3771 ImageListImpl_GetStatistics,
3772 ImageListImpl_Initialize,
3773 ImageListImpl_Replace2,
3774 ImageListImpl_ReplaceFromImageList
3777 static BOOL is_valid(HIMAGELIST himl)
3779 BOOL valid;
3780 __TRY
3782 valid = himl && himl->IImageList2_iface.lpVtbl == &ImageListImpl_Vtbl;
3784 __EXCEPT_PAGE_FAULT
3786 valid = FALSE;
3788 __ENDTRY
3789 return valid;
3792 /*************************************************************************
3793 * HIMAGELIST_QueryInterface [COMCTL32.@]
3795 * Returns a pointer to an IImageList or IImageList2 object for the given
3796 * HIMAGELIST.
3798 * PARAMS
3799 * himl [I] Image list handle.
3800 * riid [I] Identifier of the requested interface.
3801 * ppv [O] Returns the address of the pointer requested, or NULL.
3803 * RETURNS
3804 * Success: S_OK.
3805 * Failure: Error value.
3807 HRESULT WINAPI
3808 HIMAGELIST_QueryInterface (HIMAGELIST himl, REFIID riid, void **ppv)
3810 TRACE("(%p,%s,%p)\n", himl, debugstr_guid(riid), ppv);
3811 return IImageList2_QueryInterface((IImageList2 *) himl, riid, ppv);
3814 static HRESULT ImageListImpl_CreateInstance(const IUnknown *pUnkOuter, REFIID iid, void** ppv)
3816 HIMAGELIST This;
3817 HRESULT ret;
3819 TRACE("(%p,%s,%p)\n", pUnkOuter, debugstr_guid(iid), ppv);
3821 *ppv = NULL;
3823 if (pUnkOuter) return CLASS_E_NOAGGREGATION;
3825 This = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct _IMAGELIST));
3826 if (!This) return E_OUTOFMEMORY;
3828 This->IImageList2_iface.lpVtbl = &ImageListImpl_Vtbl;
3829 This->ref = 1;
3831 ret = IImageList2_QueryInterface(&This->IImageList2_iface, iid, ppv);
3832 IImageList2_Release(&This->IImageList2_iface);
3834 return ret;