comctl32/imagelist: Update todo list.
[wine.git] / dlls / comctl32 / imagelist.c
blob925952351fa0919ee0a2e5ce8820428793e631b9
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 * TODO:
26 * - Add support for ILD_PRESERVEALPHA, ILD_SCALE, ILD_DPISCALE
27 * - Add support for ILS_GLOW, ILS_SHADOW
28 * - Thread-safe locking
31 #include <stdarg.h>
32 #include <stdlib.h>
33 #include <string.h>
35 #define COBJMACROS
37 #include "winerror.h"
38 #include "windef.h"
39 #include "winbase.h"
40 #include "objbase.h"
41 #include "wingdi.h"
42 #include "winuser.h"
43 #include "commctrl.h"
44 #include "comctl32.h"
45 #include "commoncontrols.h"
46 #include "wine/debug.h"
47 #include "wine/exception.h"
48 #include "wine/heap.h"
50 WINE_DEFAULT_DEBUG_CHANNEL(imagelist);
52 #define MAX_OVERLAYIMAGE 15
54 struct _IMAGELIST
56 IImageList2 IImageList2_iface; /* 00: IImageList vtable */
57 INT cCurImage; /* 04: ImageCount */
58 INT cMaxImage; /* 08: maximages */
59 INT cGrow; /* 0C: cGrow */
60 INT cx; /* 10: cx */
61 INT cy; /* 14: cy */
62 DWORD x4;
63 UINT flags; /* 1C: flags */
64 COLORREF clrFg; /* 20: foreground color */
65 COLORREF clrBk; /* 24: background color */
68 HBITMAP hbmImage; /* 28: images Bitmap */
69 HBITMAP hbmMask; /* 2C: masks Bitmap */
70 HDC hdcImage; /* 30: images MemDC */
71 HDC hdcMask; /* 34: masks MemDC */
72 INT nOvlIdx[MAX_OVERLAYIMAGE]; /* 38: overlay images index */
74 /* not yet found out */
75 HBRUSH hbrBlend25;
76 HBRUSH hbrBlend50;
77 INT cInitial;
78 UINT uBitsPixel;
79 DWORD *item_flags;
80 BOOL color_table_set;
82 LONG ref; /* reference count */
85 #define IMAGELIST_MAGIC 0x53414D58
87 /* Header used by ImageList_Read() and ImageList_Write() */
88 #include "pshpack2.h"
89 typedef struct _ILHEAD
91 USHORT usMagic;
92 USHORT usVersion;
93 WORD cCurImage;
94 WORD cMaxImage;
95 WORD cGrow;
96 WORD cx;
97 WORD cy;
98 COLORREF bkcolor;
99 WORD flags;
100 SHORT ovls[4];
101 } ILHEAD;
102 #include "poppack.h"
104 /* internal image list data used for Drag & Drop operations */
105 typedef struct
107 HWND hwnd;
108 HIMAGELIST himl;
109 HIMAGELIST himlNoCursor;
110 /* position of the drag image relative to the window */
111 INT x;
112 INT y;
113 /* offset of the hotspot relative to the origin of the image */
114 INT dxHotspot;
115 INT dyHotspot;
116 /* is the drag image visible */
117 BOOL bShow;
118 /* saved background */
119 HBITMAP hbmBg;
120 } INTERNALDRAG;
122 static INTERNALDRAG InternalDrag = { 0, 0, 0, 0, 0, 0, 0, FALSE, 0 };
124 static inline HIMAGELIST impl_from_IImageList2(IImageList2 *iface)
126 return CONTAINING_RECORD(iface, struct _IMAGELIST, IImageList2_iface);
129 static HBITMAP ImageList_CreateImage(HDC hdc, HIMAGELIST himl, UINT count);
130 static HRESULT ImageListImpl_CreateInstance(const IUnknown *pUnkOuter, REFIID iid, void** ppv);
131 static BOOL is_valid(HIMAGELIST himl);
134 * An imagelist with N images is tiled like this:
136 * N/4 ->
138 * 4 048C..
139 * 159D..
140 * | 26AE.N
141 * V 37BF.
144 #define TILE_COUNT 4
146 static inline UINT imagelist_height( UINT count )
148 return ((count + TILE_COUNT - 1)/TILE_COUNT);
151 static inline void imagelist_point_from_index( HIMAGELIST himl, UINT index, LPPOINT pt )
153 pt->x = (index%TILE_COUNT) * himl->cx;
154 pt->y = (index/TILE_COUNT) * himl->cy;
157 static inline void imagelist_get_bitmap_size( HIMAGELIST himl, UINT count, SIZE *sz )
159 sz->cx = himl->cx * TILE_COUNT;
160 sz->cy = imagelist_height( count ) * himl->cy;
163 static inline int get_dib_stride( int width, int bpp )
165 return ((width * bpp + 31) >> 3) & ~3;
168 static inline int get_dib_image_size( const BITMAPINFO *info )
170 return get_dib_stride( info->bmiHeader.biWidth, info->bmiHeader.biBitCount )
171 * abs( info->bmiHeader.biHeight );
175 * imagelist_copy_images()
177 * Copies a block of count images from offset src in the list to offset dest.
178 * Images are copied a row at at time. Assumes hdcSrc and hdcDest are different.
180 static inline void imagelist_copy_images( HIMAGELIST himl, HDC hdcSrc, HDC hdcDest,
181 UINT src, UINT count, UINT dest )
183 POINT ptSrc, ptDest;
184 SIZE sz;
185 UINT i;
187 for ( i=0; i<TILE_COUNT; i++ )
189 imagelist_point_from_index( himl, src+i, &ptSrc );
190 imagelist_point_from_index( himl, dest+i, &ptDest );
191 sz.cx = himl->cx;
192 sz.cy = himl->cy * imagelist_height( count - i );
194 BitBlt( hdcDest, ptDest.x, ptDest.y, sz.cx, sz.cy,
195 hdcSrc, ptSrc.x, ptSrc.y, SRCCOPY );
199 static void add_dib_bits( HIMAGELIST himl, int pos, int count, int width, int height,
200 BITMAPINFO *info, BITMAPINFO *mask_info, DWORD *bits, BYTE *mask_bits )
202 int i, j, n;
203 POINT pt;
204 int stride = info->bmiHeader.biWidth;
205 int mask_stride = (info->bmiHeader.biWidth + 31) / 32 * 4;
207 for (n = 0; n < count; n++)
209 BOOL has_alpha = FALSE;
211 imagelist_point_from_index( himl, pos + n, &pt );
213 /* check if bitmap has an alpha channel */
214 for (i = 0; i < height && !has_alpha; i++)
215 for (j = n * width; j < (n + 1) * width; j++)
216 if ((has_alpha = ((bits[i * stride + j] & 0xff000000) != 0))) break;
218 if (has_alpha)
220 himl->item_flags[pos + n] = ILIF_ALPHA;
222 if (mask_info && himl->hbmMask) /* generate the mask from the alpha channel */
224 for (i = 0; i < height; i++)
225 for (j = n * width; j < (n + 1) * width; j++)
226 if ((bits[i * stride + j] >> 24) > 25) /* more than 10% alpha */
227 mask_bits[i * mask_stride + j / 8] &= ~(0x80 >> (j % 8));
228 else
229 mask_bits[i * mask_stride + j / 8] |= 0x80 >> (j % 8);
232 StretchDIBits( himl->hdcImage, pt.x, pt.y, himl->cx, himl->cy,
233 n * width, 0, width, height, bits, info, DIB_RGB_COLORS, SRCCOPY );
234 if (mask_info)
235 StretchDIBits( himl->hdcMask, pt.x, pt.y, himl->cx, himl->cy,
236 n * width, 0, width, height, mask_bits, mask_info, DIB_RGB_COLORS, SRCCOPY );
240 /* add images with an alpha channel when the image list is 32 bpp */
241 static BOOL add_with_alpha( HIMAGELIST himl, HDC hdc, int pos, int count,
242 int width, int height, HBITMAP hbmImage, HBITMAP hbmMask )
244 BOOL ret = FALSE;
245 BITMAP bm;
246 BITMAPINFO *info, *mask_info = NULL;
247 DWORD *bits = NULL;
248 BYTE *mask_bits = NULL;
249 DWORD mask_width;
251 if (!GetObjectW( hbmImage, sizeof(bm), &bm )) return FALSE;
253 /* if either the imagelist or the source bitmap don't have an alpha channel, bail out now */
254 if ((himl->flags & 0xfe) != ILC_COLOR32) return FALSE;
255 if (bm.bmBitsPixel != 32) return FALSE;
257 SelectObject( hdc, hbmImage );
258 mask_width = (bm.bmWidth + 31) / 32 * 4;
260 if (!(info = heap_alloc( FIELD_OFFSET( BITMAPINFO, bmiColors[256] )))) goto done;
261 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
262 info->bmiHeader.biWidth = bm.bmWidth;
263 info->bmiHeader.biHeight = -height;
264 info->bmiHeader.biPlanes = 1;
265 info->bmiHeader.biBitCount = 32;
266 info->bmiHeader.biCompression = BI_RGB;
267 info->bmiHeader.biSizeImage = bm.bmWidth * height * 4;
268 info->bmiHeader.biXPelsPerMeter = 0;
269 info->bmiHeader.biYPelsPerMeter = 0;
270 info->bmiHeader.biClrUsed = 0;
271 info->bmiHeader.biClrImportant = 0;
272 if (!(bits = heap_alloc( info->bmiHeader.biSizeImage ))) goto done;
273 if (!GetDIBits( hdc, hbmImage, 0, height, bits, info, DIB_RGB_COLORS )) goto done;
275 if (hbmMask)
277 if (!(mask_info = heap_alloc( FIELD_OFFSET( BITMAPINFO, bmiColors[2] ))))
278 goto done;
279 mask_info->bmiHeader = info->bmiHeader;
280 mask_info->bmiHeader.biBitCount = 1;
281 mask_info->bmiHeader.biSizeImage = mask_width * height;
282 if (!(mask_bits = heap_alloc_zero( mask_info->bmiHeader.biSizeImage )))
283 goto done;
284 if (!GetDIBits( hdc, hbmMask, 0, height, mask_bits, mask_info, DIB_RGB_COLORS )) goto done;
287 add_dib_bits( himl, pos, count, width, height, info, mask_info, bits, mask_bits );
288 ret = TRUE;
290 done:
291 heap_free( info );
292 heap_free( mask_info );
293 heap_free( bits );
294 heap_free( mask_bits );
295 return ret;
298 UINT WINAPI
299 ImageList_SetColorTable(HIMAGELIST himl, UINT uStartIndex, UINT cEntries, const RGBQUAD *prgb);
301 /*************************************************************************
302 * IMAGELIST_InternalExpandBitmaps [Internal]
304 * Expands the bitmaps of an image list by the given number of images.
306 * PARAMS
307 * himl [I] handle to image list
308 * nImageCount [I] number of images to add
310 * RETURNS
311 * nothing
313 * NOTES
314 * This function CANNOT be used to reduce the number of images.
316 static void
317 IMAGELIST_InternalExpandBitmaps(HIMAGELIST himl, INT nImageCount)
319 HDC hdcBitmap;
320 HBITMAP hbmNewBitmap, hbmNull;
321 INT nNewCount;
322 SIZE sz;
324 TRACE("%p has allocated %d, max %d, grow %d images\n", himl, himl->cCurImage, himl->cMaxImage, himl->cGrow);
326 if (himl->cCurImage + nImageCount < himl->cMaxImage)
327 return;
329 nNewCount = himl->cMaxImage + max(nImageCount, himl->cGrow) + 1;
331 imagelist_get_bitmap_size(himl, nNewCount, &sz);
333 TRACE("Create expanded bitmaps : himl=%p x=%d y=%d count=%d\n", himl, sz.cx, sz.cy, nNewCount);
334 hdcBitmap = CreateCompatibleDC (0);
336 hbmNewBitmap = ImageList_CreateImage(hdcBitmap, himl, nNewCount);
338 if (hbmNewBitmap == 0)
339 ERR("creating new image bitmap (x=%d y=%d)!\n", sz.cx, sz.cy);
341 if (himl->cCurImage)
343 hbmNull = SelectObject (hdcBitmap, hbmNewBitmap);
344 BitBlt (hdcBitmap, 0, 0, sz.cx, sz.cy,
345 himl->hdcImage, 0, 0, SRCCOPY);
346 SelectObject (hdcBitmap, hbmNull);
348 SelectObject (himl->hdcImage, hbmNewBitmap);
349 DeleteObject (himl->hbmImage);
350 himl->hbmImage = hbmNewBitmap;
352 if (himl->flags & ILC_MASK)
354 hbmNewBitmap = CreateBitmap (sz.cx, sz.cy, 1, 1, NULL);
356 if (hbmNewBitmap == 0)
357 ERR("creating new mask bitmap!\n");
359 if(himl->cCurImage)
361 hbmNull = SelectObject (hdcBitmap, hbmNewBitmap);
362 BitBlt (hdcBitmap, 0, 0, sz.cx, sz.cy,
363 himl->hdcMask, 0, 0, SRCCOPY);
364 SelectObject (hdcBitmap, hbmNull);
366 SelectObject (himl->hdcMask, hbmNewBitmap);
367 DeleteObject (himl->hbmMask);
368 himl->hbmMask = hbmNewBitmap;
371 himl->item_flags = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, himl->item_flags,
372 nNewCount * sizeof(*himl->item_flags));
373 himl->cMaxImage = nNewCount;
375 DeleteDC (hdcBitmap);
379 /*************************************************************************
380 * ImageList_Add [COMCTL32.@]
382 * Add an image or images to an image list.
384 * PARAMS
385 * himl [I] handle to image list
386 * hbmImage [I] handle to image bitmap
387 * hbmMask [I] handle to mask bitmap
389 * RETURNS
390 * Success: Index of the first new image.
391 * Failure: -1
394 INT WINAPI
395 ImageList_Add (HIMAGELIST himl, HBITMAP hbmImage, HBITMAP hbmMask)
397 HDC hdcBitmap, hdcTemp = 0;
398 INT nFirstIndex, nImageCount, i;
399 BITMAP bmp;
400 POINT pt;
402 TRACE("himl=%p hbmimage=%p hbmmask=%p\n", himl, hbmImage, hbmMask);
403 if (!is_valid(himl))
404 return -1;
406 if (!GetObjectW(hbmImage, sizeof(BITMAP), &bmp))
407 return -1;
409 TRACE("himl %p, cCurImage %d, cMaxImage %d, cGrow %d, cx %d, cy %d\n",
410 himl, himl->cCurImage, himl->cMaxImage, himl->cGrow, himl->cx, himl->cy);
412 nImageCount = bmp.bmWidth / himl->cx;
414 TRACE("%p has %d images (%d x %d) bpp %d\n", hbmImage, nImageCount, bmp.bmWidth, bmp.bmHeight,
415 bmp.bmBitsPixel);
417 IMAGELIST_InternalExpandBitmaps(himl, nImageCount);
419 hdcBitmap = CreateCompatibleDC(0);
421 SelectObject(hdcBitmap, hbmImage);
423 if (add_with_alpha( himl, hdcBitmap, himl->cCurImage, nImageCount,
424 himl->cx, min( himl->cy, bmp.bmHeight), hbmImage, hbmMask ))
425 goto done;
427 if (himl->hbmMask)
429 hdcTemp = CreateCompatibleDC(0);
430 SelectObject(hdcTemp, hbmMask);
433 if (himl->uBitsPixel <= 8 && bmp.bmBitsPixel <= 8 &&
434 !himl->color_table_set && himl->cCurImage == 0)
436 RGBQUAD colors[256];
437 UINT num = GetDIBColorTable( hdcBitmap, 0, 1 << bmp.bmBitsPixel, colors );
438 if (num) ImageList_SetColorTable( himl, 0, num, colors );
441 for (i=0; i<nImageCount; i++)
443 imagelist_point_from_index( himl, himl->cCurImage + i, &pt );
445 /* Copy result to the imagelist
447 BitBlt( himl->hdcImage, pt.x, pt.y, himl->cx, bmp.bmHeight,
448 hdcBitmap, i*himl->cx, 0, SRCCOPY );
450 if (!himl->hbmMask)
451 continue;
453 BitBlt( himl->hdcMask, pt.x, pt.y, himl->cx, bmp.bmHeight,
454 hdcTemp, i*himl->cx, 0, SRCCOPY );
456 /* Remove the background from the image
458 BitBlt( himl->hdcImage, pt.x, pt.y, himl->cx, bmp.bmHeight,
459 himl->hdcMask, pt.x, pt.y, 0x220326 ); /* NOTSRCAND */
461 if (hdcTemp) DeleteDC(hdcTemp);
463 done:
464 DeleteDC(hdcBitmap);
466 nFirstIndex = himl->cCurImage;
467 himl->cCurImage += nImageCount;
469 return nFirstIndex;
473 /*************************************************************************
474 * ImageList_AddIcon [COMCTL32.@]
476 * Adds an icon to an image list.
478 * PARAMS
479 * himl [I] handle to image list
480 * hIcon [I] handle to icon
482 * RETURNS
483 * Success: index of the new image
484 * Failure: -1
486 #undef ImageList_AddIcon
487 INT WINAPI ImageList_AddIcon (HIMAGELIST himl, HICON hIcon)
489 return ImageList_ReplaceIcon (himl, -1, hIcon);
493 /*************************************************************************
494 * ImageList_AddMasked [COMCTL32.@]
496 * Adds an image or images to an image list and creates a mask from the
497 * specified bitmap using the mask color.
499 * PARAMS
500 * himl [I] handle to image list.
501 * hBitmap [I] handle to bitmap
502 * clrMask [I] mask color.
504 * RETURNS
505 * Success: Index of the first new image.
506 * Failure: -1
509 INT WINAPI
510 ImageList_AddMasked (HIMAGELIST himl, HBITMAP hBitmap, COLORREF clrMask)
512 HDC hdcMask, hdcBitmap;
513 INT ret;
514 BITMAP bmp;
515 HBITMAP hMaskBitmap;
516 COLORREF bkColor;
518 TRACE("himl=%p hbitmap=%p clrmask=%x\n", himl, hBitmap, clrMask);
519 if (!is_valid(himl))
520 return -1;
522 if (!GetObjectW(hBitmap, sizeof(BITMAP), &bmp))
523 return -1;
525 hdcBitmap = CreateCompatibleDC(0);
526 SelectObject(hdcBitmap, hBitmap);
528 /* Create a temp Mask so we can remove the background of the Image */
529 hdcMask = CreateCompatibleDC(0);
530 hMaskBitmap = CreateBitmap(bmp.bmWidth, bmp.bmHeight, 1, 1, NULL);
531 SelectObject(hdcMask, hMaskBitmap);
533 /* create monochrome image to the mask bitmap */
534 bkColor = (clrMask != CLR_DEFAULT) ? clrMask : GetPixel (hdcBitmap, 0, 0);
535 SetBkColor (hdcBitmap, bkColor);
536 BitBlt (hdcMask, 0, 0, bmp.bmWidth, bmp.bmHeight, hdcBitmap, 0, 0, SRCCOPY);
539 * Remove the background from the image
541 * WINDOWS BUG ALERT!!!!!!
542 * The statement below should not be done in common practice
543 * but this is how ImageList_AddMasked works in Windows.
544 * It overwrites the original bitmap passed, this was discovered
545 * by using the same bitmap to iterate the different styles
546 * on windows where it failed (BUT ImageList_Add is OK)
547 * This is here in case some apps rely on this bug
549 * Blt mode 0x220326 is NOTSRCAND
551 if (bmp.bmBitsPixel > 8) /* NOTSRCAND can't work with palettes */
553 SetBkColor(hdcBitmap, RGB(255,255,255));
554 BitBlt(hdcBitmap, 0, 0, bmp.bmWidth, bmp.bmHeight, hdcMask, 0, 0, 0x220326);
557 DeleteDC(hdcBitmap);
558 DeleteDC(hdcMask);
560 ret = ImageList_Add( himl, hBitmap, hMaskBitmap );
562 DeleteObject(hMaskBitmap);
563 return ret;
567 /*************************************************************************
568 * ImageList_BeginDrag [COMCTL32.@]
570 * Creates a temporary image list that contains one image. It will be used
571 * as a drag image.
573 * PARAMS
574 * himlTrack [I] handle to the source image list
575 * iTrack [I] index of the drag image in the source image list
576 * dxHotspot [I] X position of the hot spot of the drag image
577 * dyHotspot [I] Y position of the hot spot of the drag image
579 * RETURNS
580 * Success: TRUE
581 * Failure: FALSE
584 BOOL WINAPI
585 ImageList_BeginDrag (HIMAGELIST himlTrack, INT iTrack,
586 INT dxHotspot, INT dyHotspot)
588 INT cx, cy;
589 POINT src, dst;
591 TRACE("(himlTrack=%p iTrack=%d dx=%d dy=%d)\n", himlTrack, iTrack,
592 dxHotspot, dyHotspot);
594 if (!is_valid(himlTrack))
595 return FALSE;
597 if (iTrack >= himlTrack->cCurImage)
598 return FALSE;
600 if (InternalDrag.himl)
601 return FALSE;
603 cx = himlTrack->cx;
604 cy = himlTrack->cy;
606 InternalDrag.himlNoCursor = InternalDrag.himl = ImageList_Create (cx, cy, himlTrack->flags, 1, 1);
607 if (InternalDrag.himl == NULL) {
608 WARN("Error creating drag image list!\n");
609 return FALSE;
612 InternalDrag.dxHotspot = dxHotspot;
613 InternalDrag.dyHotspot = dyHotspot;
615 /* copy image */
616 imagelist_point_from_index(InternalDrag.himl, 0, &dst);
617 imagelist_point_from_index(himlTrack, iTrack, &src);
618 BitBlt(InternalDrag.himl->hdcImage, dst.x, dst.y, cx, cy, himlTrack->hdcImage, src.x, src.y,
619 SRCCOPY);
620 BitBlt(InternalDrag.himl->hdcMask, dst.x, dst.y, cx, cy, himlTrack->hdcMask, src.x, src.y,
621 SRCCOPY);
623 InternalDrag.himl->cCurImage = 1;
625 return TRUE;
629 /*************************************************************************
630 * ImageList_Copy [COMCTL32.@]
632 * Copies an image of the source image list to an image of the
633 * destination image list. Images can be copied or swapped.
635 * PARAMS
636 * himlDst [I] handle to the destination image list
637 * iDst [I] destination image index.
638 * himlSrc [I] handle to the source image list
639 * iSrc [I] source image index
640 * uFlags [I] flags for the copy operation
642 * RETURNS
643 * Success: TRUE
644 * Failure: FALSE
646 * NOTES
647 * Copying from one image list to another is possible. The original
648 * implementation just copies or swaps within one image list.
649 * Could this feature become a bug??? ;-)
652 BOOL WINAPI
653 ImageList_Copy (HIMAGELIST himlDst, INT iDst, HIMAGELIST himlSrc,
654 INT iSrc, UINT uFlags)
656 POINT ptSrc, ptDst;
658 TRACE("himlDst=%p iDst=%d himlSrc=%p iSrc=%d\n", himlDst, iDst, himlSrc, iSrc);
660 if (!is_valid(himlSrc) || !is_valid(himlDst))
661 return FALSE;
662 if ((iDst < 0) || (iDst >= himlDst->cCurImage))
663 return FALSE;
664 if ((iSrc < 0) || (iSrc >= himlSrc->cCurImage))
665 return FALSE;
667 imagelist_point_from_index( himlDst, iDst, &ptDst );
668 imagelist_point_from_index( himlSrc, iSrc, &ptSrc );
670 if (uFlags & ILCF_SWAP) {
671 /* swap */
672 HDC hdcBmp;
673 HBITMAP hbmTempImage, hbmTempMask;
675 hdcBmp = CreateCompatibleDC (0);
677 /* create temporary bitmaps */
678 hbmTempImage = CreateBitmap (himlSrc->cx, himlSrc->cy, 1,
679 himlSrc->uBitsPixel, NULL);
680 hbmTempMask = CreateBitmap (himlSrc->cx, himlSrc->cy, 1,
681 1, NULL);
683 /* copy (and stretch) destination to temporary bitmaps.(save) */
684 /* image */
685 SelectObject (hdcBmp, hbmTempImage);
686 StretchBlt (hdcBmp, 0, 0, himlSrc->cx, himlSrc->cy,
687 himlDst->hdcImage, ptDst.x, ptDst.y, himlDst->cx, himlDst->cy,
688 SRCCOPY);
689 /* mask */
690 SelectObject (hdcBmp, hbmTempMask);
691 StretchBlt (hdcBmp, 0, 0, himlSrc->cx, himlSrc->cy,
692 himlDst->hdcMask, ptDst.x, ptDst.y, himlDst->cx, himlDst->cy,
693 SRCCOPY);
695 /* copy (and stretch) source to destination */
696 /* image */
697 StretchBlt (himlDst->hdcImage, ptDst.x, ptDst.y, himlDst->cx, himlDst->cy,
698 himlSrc->hdcImage, ptSrc.x, ptSrc.y, himlSrc->cx, himlSrc->cy,
699 SRCCOPY);
700 /* mask */
701 StretchBlt (himlDst->hdcMask, ptDst.x, ptDst.y, himlDst->cx, himlDst->cy,
702 himlSrc->hdcMask, ptSrc.x, ptSrc.y, himlSrc->cx, himlSrc->cy,
703 SRCCOPY);
705 /* copy (without stretching) temporary bitmaps to source (restore) */
706 /* mask */
707 BitBlt (himlSrc->hdcMask, ptSrc.x, ptSrc.y, himlSrc->cx, himlSrc->cy,
708 hdcBmp, 0, 0, SRCCOPY);
710 /* image */
711 BitBlt (himlSrc->hdcImage, ptSrc.x, ptSrc.y, himlSrc->cx, himlSrc->cy,
712 hdcBmp, 0, 0, SRCCOPY);
713 /* delete temporary bitmaps */
714 DeleteObject (hbmTempMask);
715 DeleteObject (hbmTempImage);
716 DeleteDC(hdcBmp);
718 else {
719 /* copy image */
720 StretchBlt (himlDst->hdcImage, ptDst.x, ptDst.y, himlDst->cx, himlDst->cy,
721 himlSrc->hdcImage, ptSrc.x, ptSrc.y, himlSrc->cx, himlSrc->cy,
722 SRCCOPY);
724 /* copy mask */
725 StretchBlt (himlDst->hdcMask, ptDst.x, ptDst.y, himlDst->cx, himlDst->cy,
726 himlSrc->hdcMask, ptSrc.x, ptSrc.y, himlSrc->cx, himlSrc->cy,
727 SRCCOPY);
730 return TRUE;
734 /*************************************************************************
735 * ImageList_Create [COMCTL32.@]
737 * Creates a new image list.
739 * PARAMS
740 * cx [I] image height
741 * cy [I] image width
742 * flags [I] creation flags
743 * cInitial [I] initial number of images in the image list
744 * cGrow [I] number of images by which image list grows
746 * RETURNS
747 * Success: Handle to the created image list
748 * Failure: NULL
750 HIMAGELIST WINAPI
751 ImageList_Create (INT cx, INT cy, UINT flags,
752 INT cInitial, INT cGrow)
754 HIMAGELIST himl;
755 INT nCount;
756 HBITMAP hbmTemp;
757 UINT ilc = (flags & 0xFE);
758 static const WORD aBitBlend25[] =
759 {0xAA, 0x00, 0x55, 0x00, 0xAA, 0x00, 0x55, 0x00};
761 static const WORD aBitBlend50[] =
762 {0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA};
764 TRACE("(%d %d 0x%x %d %d)\n", cx, cy, flags, cInitial, cGrow);
766 if (cx < 0 || cy < 0) return NULL;
767 if (!((flags&ILC_COLORDDB) == ILC_COLORDDB) && (cx == 0 || cy == 0)) return NULL;
769 /* Create the IImageList interface for the image list */
770 if (FAILED(ImageListImpl_CreateInstance(NULL, &IID_IImageList, (void **)&himl)))
771 return NULL;
773 cGrow = (WORD)((max( cGrow, 1 ) + 3) & ~3);
775 if (cGrow > 256)
777 /* Windows doesn't limit the size here, but X11 doesn't let us allocate such huge bitmaps */
778 WARN( "grow %d too large, limiting to 256\n", cGrow );
779 cGrow = 256;
782 himl->cx = cx;
783 himl->cy = cy;
784 himl->flags = flags;
785 himl->cMaxImage = cInitial + 1;
786 himl->cInitial = cInitial;
787 himl->cGrow = cGrow;
788 himl->clrFg = CLR_DEFAULT;
789 himl->clrBk = CLR_NONE;
790 himl->color_table_set = FALSE;
792 /* initialize overlay mask indices */
793 for (nCount = 0; nCount < MAX_OVERLAYIMAGE; nCount++)
794 himl->nOvlIdx[nCount] = -1;
796 /* Create Image & Mask DCs */
797 himl->hdcImage = CreateCompatibleDC (0);
798 if (!himl->hdcImage)
799 goto cleanup;
800 if (himl->flags & ILC_MASK){
801 himl->hdcMask = CreateCompatibleDC(0);
802 if (!himl->hdcMask)
803 goto cleanup;
806 /* Default to ILC_COLOR4 if none of the ILC_COLOR* flags are specified */
807 if (ilc == ILC_COLOR)
809 ilc = ILC_COLOR4;
810 himl->flags |= ILC_COLOR4;
813 if (ilc >= ILC_COLOR4 && ilc <= ILC_COLOR32)
814 himl->uBitsPixel = ilc;
815 else
816 himl->uBitsPixel = (UINT)GetDeviceCaps (himl->hdcImage, BITSPIXEL);
818 if (himl->cMaxImage > 0) {
819 himl->hbmImage = ImageList_CreateImage(himl->hdcImage, himl, himl->cMaxImage);
820 SelectObject(himl->hdcImage, himl->hbmImage);
821 } else
822 himl->hbmImage = 0;
824 if ((himl->cMaxImage > 0) && (himl->flags & ILC_MASK)) {
825 SIZE sz;
827 imagelist_get_bitmap_size(himl, himl->cMaxImage, &sz);
828 himl->hbmMask = CreateBitmap (sz.cx, sz.cy, 1, 1, NULL);
829 if (himl->hbmMask == 0) {
830 ERR("Error creating mask bitmap!\n");
831 goto cleanup;
833 SelectObject(himl->hdcMask, himl->hbmMask);
835 else
836 himl->hbmMask = 0;
838 himl->item_flags = heap_alloc_zero( himl->cMaxImage * sizeof(*himl->item_flags) );
840 /* create blending brushes */
841 hbmTemp = CreateBitmap (8, 8, 1, 1, aBitBlend25);
842 himl->hbrBlend25 = CreatePatternBrush (hbmTemp);
843 DeleteObject (hbmTemp);
845 hbmTemp = CreateBitmap (8, 8, 1, 1, aBitBlend50);
846 himl->hbrBlend50 = CreatePatternBrush (hbmTemp);
847 DeleteObject (hbmTemp);
849 TRACE("created imagelist %p\n", himl);
850 return himl;
852 cleanup:
853 ImageList_Destroy(himl);
854 return NULL;
858 /*************************************************************************
859 * ImageList_Destroy [COMCTL32.@]
861 * Destroys an image list.
863 * PARAMS
864 * himl [I] handle to image list
866 * RETURNS
867 * Success: TRUE
868 * Failure: FALSE
871 BOOL WINAPI
872 ImageList_Destroy (HIMAGELIST himl)
874 if (!is_valid(himl))
875 return FALSE;
877 IImageList_Release((IImageList *) himl);
878 return TRUE;
882 /*************************************************************************
883 * ImageList_DragEnter [COMCTL32.@]
885 * Locks window update and displays the drag image at the given position.
887 * PARAMS
888 * hwndLock [I] handle of the window that owns the drag image.
889 * x [I] X position of the drag image.
890 * y [I] Y position of the drag image.
892 * RETURNS
893 * Success: TRUE
894 * Failure: FALSE
896 * NOTES
897 * The position of the drag image is relative to the window, not
898 * the client area.
901 BOOL WINAPI
902 ImageList_DragEnter (HWND hwndLock, INT x, INT y)
904 TRACE("(hwnd=%p x=%d y=%d)\n", hwndLock, x, y);
906 if (!is_valid(InternalDrag.himl))
907 return FALSE;
909 if (hwndLock)
910 InternalDrag.hwnd = hwndLock;
911 else
912 InternalDrag.hwnd = GetDesktopWindow ();
914 InternalDrag.x = x;
915 InternalDrag.y = y;
917 /* draw the drag image and save the background */
918 return ImageList_DragShowNolock(TRUE);
922 /*************************************************************************
923 * ImageList_DragLeave [COMCTL32.@]
925 * Unlocks window update and hides the drag image.
927 * PARAMS
928 * hwndLock [I] handle of the window that owns the drag image.
930 * RETURNS
931 * Success: TRUE
932 * Failure: FALSE
935 BOOL WINAPI
936 ImageList_DragLeave (HWND hwndLock)
938 /* As we don't save drag info in the window this can lead to problems if
939 an app does not supply the same window as DragEnter */
940 /* if (hwndLock)
941 InternalDrag.hwnd = hwndLock;
942 else
943 InternalDrag.hwnd = GetDesktopWindow (); */
944 if(!hwndLock)
945 hwndLock = GetDesktopWindow();
946 if(InternalDrag.hwnd != hwndLock)
947 FIXME("DragLeave hWnd != DragEnter hWnd\n");
949 ImageList_DragShowNolock (FALSE);
951 return TRUE;
955 /*************************************************************************
956 * ImageList_InternalDragDraw [Internal]
958 * Draws the drag image.
960 * PARAMS
961 * hdc [I] device context to draw into.
962 * x [I] X position of the drag image.
963 * y [I] Y position of the drag image.
965 * RETURNS
966 * Success: TRUE
967 * Failure: FALSE
969 * NOTES
970 * The position of the drag image is relative to the window, not
971 * the client area.
975 static inline void
976 ImageList_InternalDragDraw (HDC hdc, INT x, INT y)
978 IMAGELISTDRAWPARAMS imldp;
980 ZeroMemory (&imldp, sizeof(imldp));
981 imldp.cbSize = sizeof(imldp);
982 imldp.himl = InternalDrag.himl;
983 imldp.i = 0;
984 imldp.hdcDst = hdc;
985 imldp.x = x;
986 imldp.y = y;
987 imldp.rgbBk = CLR_DEFAULT;
988 imldp.rgbFg = CLR_DEFAULT;
989 imldp.fStyle = ILD_NORMAL;
990 imldp.fState = ILS_ALPHA;
991 imldp.Frame = 192;
992 ImageList_DrawIndirect (&imldp);
995 /*************************************************************************
996 * ImageList_DragMove [COMCTL32.@]
998 * Moves the drag image.
1000 * PARAMS
1001 * x [I] X position of the drag image.
1002 * y [I] Y position of the drag image.
1004 * RETURNS
1005 * Success: TRUE
1006 * Failure: FALSE
1008 * NOTES
1009 * The position of the drag image is relative to the window, not
1010 * the client area.
1013 BOOL WINAPI
1014 ImageList_DragMove (INT x, INT y)
1016 TRACE("(x=%d y=%d)\n", x, y);
1018 if (!is_valid(InternalDrag.himl))
1019 return FALSE;
1021 /* draw/update the drag image */
1022 if (InternalDrag.bShow) {
1023 HDC hdcDrag;
1024 HDC hdcOffScreen;
1025 HDC hdcBg;
1026 HBITMAP hbmOffScreen;
1027 INT origNewX, origNewY;
1028 INT origOldX, origOldY;
1029 INT origRegX, origRegY;
1030 INT sizeRegX, sizeRegY;
1033 /* calculate the update region */
1034 origNewX = x - InternalDrag.dxHotspot;
1035 origNewY = y - InternalDrag.dyHotspot;
1036 origOldX = InternalDrag.x - InternalDrag.dxHotspot;
1037 origOldY = InternalDrag.y - InternalDrag.dyHotspot;
1038 origRegX = min(origNewX, origOldX);
1039 origRegY = min(origNewY, origOldY);
1040 sizeRegX = InternalDrag.himl->cx + abs(x - InternalDrag.x);
1041 sizeRegY = InternalDrag.himl->cy + abs(y - InternalDrag.y);
1043 hdcDrag = GetDCEx(InternalDrag.hwnd, 0,
1044 DCX_WINDOW | DCX_CACHE | DCX_LOCKWINDOWUPDATE);
1045 hdcOffScreen = CreateCompatibleDC(hdcDrag);
1046 hdcBg = CreateCompatibleDC(hdcDrag);
1048 hbmOffScreen = CreateCompatibleBitmap(hdcDrag, sizeRegX, sizeRegY);
1049 SelectObject(hdcOffScreen, hbmOffScreen);
1050 SelectObject(hdcBg, InternalDrag.hbmBg);
1052 /* get the actual background of the update region */
1053 BitBlt(hdcOffScreen, 0, 0, sizeRegX, sizeRegY, hdcDrag,
1054 origRegX, origRegY, SRCCOPY);
1055 /* erase the old image */
1056 BitBlt(hdcOffScreen, origOldX - origRegX, origOldY - origRegY,
1057 InternalDrag.himl->cx, InternalDrag.himl->cy, hdcBg, 0, 0,
1058 SRCCOPY);
1059 /* save the background */
1060 BitBlt(hdcBg, 0, 0, InternalDrag.himl->cx, InternalDrag.himl->cy,
1061 hdcOffScreen, origNewX - origRegX, origNewY - origRegY, SRCCOPY);
1062 /* draw the image */
1063 ImageList_InternalDragDraw(hdcOffScreen, origNewX - origRegX,
1064 origNewY - origRegY);
1065 /* draw the update region to the screen */
1066 BitBlt(hdcDrag, origRegX, origRegY, sizeRegX, sizeRegY,
1067 hdcOffScreen, 0, 0, SRCCOPY);
1069 DeleteDC(hdcBg);
1070 DeleteDC(hdcOffScreen);
1071 DeleteObject(hbmOffScreen);
1072 ReleaseDC(InternalDrag.hwnd, hdcDrag);
1075 /* update the image position */
1076 InternalDrag.x = x;
1077 InternalDrag.y = y;
1079 return TRUE;
1083 /*************************************************************************
1084 * ImageList_DragShowNolock [COMCTL32.@]
1086 * Shows or hides the drag image.
1088 * PARAMS
1089 * bShow [I] TRUE shows the drag image, FALSE hides it.
1091 * RETURNS
1092 * Success: TRUE
1093 * Failure: FALSE
1096 BOOL WINAPI
1097 ImageList_DragShowNolock (BOOL bShow)
1099 HDC hdcDrag;
1100 HDC hdcBg;
1101 INT x, y;
1103 if (!is_valid(InternalDrag.himl))
1104 return FALSE;
1106 TRACE("bShow=0x%X!\n", bShow);
1108 /* DragImage is already visible/hidden */
1109 if ((InternalDrag.bShow && bShow) || (!InternalDrag.bShow && !bShow)) {
1110 return FALSE;
1113 /* position of the origin of the DragImage */
1114 x = InternalDrag.x - InternalDrag.dxHotspot;
1115 y = InternalDrag.y - InternalDrag.dyHotspot;
1117 hdcDrag = GetDCEx (InternalDrag.hwnd, 0,
1118 DCX_WINDOW | DCX_CACHE | DCX_LOCKWINDOWUPDATE);
1119 if (!hdcDrag) {
1120 return FALSE;
1123 hdcBg = CreateCompatibleDC(hdcDrag);
1124 if (!InternalDrag.hbmBg) {
1125 InternalDrag.hbmBg = CreateCompatibleBitmap(hdcDrag,
1126 InternalDrag.himl->cx, InternalDrag.himl->cy);
1128 SelectObject(hdcBg, InternalDrag.hbmBg);
1130 if (bShow) {
1131 /* save the background */
1132 BitBlt(hdcBg, 0, 0, InternalDrag.himl->cx, InternalDrag.himl->cy,
1133 hdcDrag, x, y, SRCCOPY);
1134 /* show the image */
1135 ImageList_InternalDragDraw(hdcDrag, x, y);
1136 } else {
1137 /* hide the image */
1138 BitBlt(hdcDrag, x, y, InternalDrag.himl->cx, InternalDrag.himl->cy,
1139 hdcBg, 0, 0, SRCCOPY);
1142 InternalDrag.bShow = !InternalDrag.bShow;
1144 DeleteDC(hdcBg);
1145 ReleaseDC (InternalDrag.hwnd, hdcDrag);
1146 return TRUE;
1150 /*************************************************************************
1151 * ImageList_Draw [COMCTL32.@]
1153 * Draws an image.
1155 * PARAMS
1156 * himl [I] handle to image list
1157 * i [I] image index
1158 * hdc [I] handle to device context
1159 * x [I] x position
1160 * y [I] y position
1161 * fStyle [I] drawing flags
1163 * RETURNS
1164 * Success: TRUE
1165 * Failure: FALSE
1167 * SEE
1168 * ImageList_DrawEx.
1171 BOOL WINAPI
1172 ImageList_Draw (HIMAGELIST himl, INT i, HDC hdc, INT x, INT y, UINT fStyle)
1174 return ImageList_DrawEx (himl, i, hdc, x, y, 0, 0,
1175 CLR_DEFAULT, CLR_DEFAULT, fStyle);
1179 /*************************************************************************
1180 * ImageList_DrawEx [COMCTL32.@]
1182 * Draws an image and allows using extended drawing features.
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 * dx [I] X offset
1191 * dy [I] Y offset
1192 * rgbBk [I] background color
1193 * rgbFg [I] foreground color
1194 * fStyle [I] drawing flags
1196 * RETURNS
1197 * Success: TRUE
1198 * Failure: FALSE
1200 * NOTES
1201 * Calls ImageList_DrawIndirect.
1203 * SEE
1204 * ImageList_DrawIndirect.
1207 BOOL WINAPI
1208 ImageList_DrawEx (HIMAGELIST himl, INT i, HDC hdc, INT x, INT y,
1209 INT dx, INT dy, COLORREF rgbBk, COLORREF rgbFg,
1210 UINT fStyle)
1212 IMAGELISTDRAWPARAMS imldp;
1214 ZeroMemory (&imldp, sizeof(imldp));
1215 imldp.cbSize = sizeof(imldp);
1216 imldp.himl = himl;
1217 imldp.i = i;
1218 imldp.hdcDst = hdc;
1219 imldp.x = x;
1220 imldp.y = y;
1221 imldp.cx = dx;
1222 imldp.cy = dy;
1223 imldp.rgbBk = rgbBk;
1224 imldp.rgbFg = rgbFg;
1225 imldp.fStyle = fStyle;
1227 return ImageList_DrawIndirect (&imldp);
1231 static BOOL alpha_blend_image( HIMAGELIST himl, HDC dest_dc, int dest_x, int dest_y,
1232 int src_x, int src_y, int cx, int cy, BLENDFUNCTION func,
1233 UINT style, COLORREF blend_col, BOOL has_alpha, BOOL grayscale )
1235 BOOL ret = FALSE;
1236 HDC hdc;
1237 HBITMAP bmp = 0, mask = 0;
1238 BITMAPINFO *info;
1239 void *bits, *mask_bits;
1240 unsigned int *ptr;
1241 int i, j;
1243 if (!(hdc = CreateCompatibleDC( 0 ))) return FALSE;
1244 if (!(info = heap_alloc( FIELD_OFFSET( BITMAPINFO, bmiColors[256] )))) goto done;
1245 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1246 info->bmiHeader.biWidth = cx;
1247 info->bmiHeader.biHeight = cy;
1248 info->bmiHeader.biPlanes = 1;
1249 info->bmiHeader.biBitCount = 32;
1250 info->bmiHeader.biCompression = BI_RGB;
1251 info->bmiHeader.biSizeImage = cx * cy * 4;
1252 info->bmiHeader.biXPelsPerMeter = 0;
1253 info->bmiHeader.biYPelsPerMeter = 0;
1254 info->bmiHeader.biClrUsed = 0;
1255 info->bmiHeader.biClrImportant = 0;
1256 if (!(bmp = CreateDIBSection( himl->hdcImage, info, DIB_RGB_COLORS, &bits, 0, 0 ))) goto done;
1257 SelectObject( hdc, bmp );
1258 BitBlt( hdc, 0, 0, cx, cy, himl->hdcImage, src_x, src_y, SRCCOPY );
1260 if (blend_col != CLR_NONE)
1262 BYTE r = GetRValue( blend_col );
1263 BYTE g = GetGValue( blend_col );
1264 BYTE b = GetBValue( blend_col );
1266 if (style & ILD_BLEND25)
1268 for (i = 0, ptr = bits; i < cx * cy; i++, ptr++)
1269 *ptr = ((*ptr & 0xff000000) |
1270 ((((*ptr & 0x00ff0000) * 3 + (r << 16)) / 4) & 0x00ff0000) |
1271 ((((*ptr & 0x0000ff00) * 3 + (g << 8)) / 4) & 0x0000ff00) |
1272 ((((*ptr & 0x000000ff) * 3 + (b << 0)) / 4) & 0x000000ff));
1274 else if (style & ILD_BLEND50)
1276 for (i = 0, ptr = bits; i < cx * cy; i++, ptr++)
1277 *ptr = ((*ptr & 0xff000000) |
1278 ((((*ptr & 0x00ff0000) + (r << 16)) / 2) & 0x00ff0000) |
1279 ((((*ptr & 0x0000ff00) + (g << 8)) / 2) & 0x0000ff00) |
1280 ((((*ptr & 0x000000ff) + (b << 0)) / 2) & 0x000000ff));
1284 if (grayscale)
1286 for (i = 0, ptr = bits; i < cx * cy; i++, ptr++)
1288 DWORD gray = (((*ptr & 0x00ff0000) >> 16) * 299 +
1289 ((*ptr & 0x0000ff00) >> 8) * 587 +
1290 ((*ptr & 0x000000ff) >> 0) * 114 + 500) / 1000;
1291 if (has_alpha) gray = gray * (*ptr >> 24) / 255;
1292 *ptr = (*ptr & 0xff000000)| (gray << 16) | (gray << 8) | gray;
1296 if (has_alpha) /* we already have an alpha channel in this case */
1298 /* pre-multiply by the alpha channel */
1299 for (i = 0, ptr = bits; i < cx * cy; i++, ptr++)
1301 DWORD alpha = *ptr >> 24;
1302 *ptr = ((*ptr & 0xff000000) |
1303 (((*ptr & 0x00ff0000) * alpha / 255) & 0x00ff0000) |
1304 (((*ptr & 0x0000ff00) * alpha / 255) & 0x0000ff00) |
1305 (((*ptr & 0x000000ff) * alpha / 255)));
1308 else if (himl->hbmMask)
1310 unsigned int width_bytes = (cx + 31) / 32 * 4;
1311 /* generate alpha channel from the mask */
1312 info->bmiHeader.biBitCount = 1;
1313 info->bmiHeader.biSizeImage = width_bytes * cy;
1314 info->bmiColors[0].rgbRed = 0;
1315 info->bmiColors[0].rgbGreen = 0;
1316 info->bmiColors[0].rgbBlue = 0;
1317 info->bmiColors[0].rgbReserved = 0;
1318 info->bmiColors[1].rgbRed = 0xff;
1319 info->bmiColors[1].rgbGreen = 0xff;
1320 info->bmiColors[1].rgbBlue = 0xff;
1321 info->bmiColors[1].rgbReserved = 0;
1322 if (!(mask = CreateDIBSection( himl->hdcMask, info, DIB_RGB_COLORS, &mask_bits, 0, 0 )))
1323 goto done;
1324 SelectObject( hdc, mask );
1325 BitBlt( hdc, 0, 0, cx, cy, himl->hdcMask, src_x, src_y, SRCCOPY );
1326 SelectObject( hdc, bmp );
1327 for (i = 0, ptr = bits; i < cy; i++)
1328 for (j = 0; j < cx; j++, ptr++)
1329 if ((((BYTE *)mask_bits)[i * width_bytes + j / 8] << (j % 8)) & 0x80) *ptr = 0;
1330 else *ptr |= 0xff000000;
1332 else
1334 for (i = 0, ptr = bits; i < cx * cy; i++, ptr++)
1335 *ptr |= 0xff000000;
1338 ret = GdiAlphaBlend( dest_dc, dest_x, dest_y, cx, cy, hdc, 0, 0, cx, cy, func );
1340 done:
1341 DeleteDC( hdc );
1342 if (bmp) DeleteObject( bmp );
1343 if (mask) DeleteObject( mask );
1344 heap_free( info );
1345 return ret;
1348 /*************************************************************************
1349 * ImageList_DrawIndirect [COMCTL32.@]
1351 * Draws an image using various parameters specified in pimldp.
1353 * PARAMS
1354 * pimldp [I] pointer to IMAGELISTDRAWPARAMS structure.
1356 * RETURNS
1357 * Success: TRUE
1358 * Failure: FALSE
1361 BOOL WINAPI
1362 ImageList_DrawIndirect (IMAGELISTDRAWPARAMS *pimldp)
1364 INT cx, cy, nOvlIdx;
1365 DWORD fState, dwRop;
1366 UINT fStyle;
1367 COLORREF oldImageBk, oldImageFg;
1368 HDC hImageDC, hImageListDC, hMaskListDC;
1369 HBITMAP hImageBmp, hOldImageBmp, hBlendMaskBmp;
1370 BOOL bIsTransparent, bBlend, bResult = FALSE, bMask;
1371 HIMAGELIST himl;
1372 HBRUSH hOldBrush;
1373 POINT pt;
1374 BOOL has_alpha;
1376 if (!pimldp || !(himl = pimldp->himl)) return FALSE;
1377 if (!is_valid(himl)) return FALSE;
1378 if ((pimldp->i < 0) || (pimldp->i >= himl->cCurImage)) return FALSE;
1380 imagelist_point_from_index( himl, pimldp->i, &pt );
1381 pt.x += pimldp->xBitmap;
1382 pt.y += pimldp->yBitmap;
1384 fState = pimldp->cbSize < sizeof(IMAGELISTDRAWPARAMS) ? ILS_NORMAL : pimldp->fState;
1385 fStyle = pimldp->fStyle & ~ILD_OVERLAYMASK;
1386 cx = (pimldp->cx == 0) ? himl->cx : pimldp->cx;
1387 cy = (pimldp->cy == 0) ? himl->cy : pimldp->cy;
1389 bIsTransparent = (fStyle & ILD_TRANSPARENT);
1390 if( pimldp->rgbBk == CLR_NONE )
1391 bIsTransparent = TRUE;
1392 if( ( pimldp->rgbBk == CLR_DEFAULT ) && ( himl->clrBk == CLR_NONE ) )
1393 bIsTransparent = TRUE;
1394 bMask = (himl->flags & ILC_MASK) && (fStyle & ILD_MASK) ;
1395 bBlend = (fStyle & (ILD_BLEND25 | ILD_BLEND50) ) && !bMask;
1397 TRACE("himl(%p) hbmMask(%p) iImage(%d) x(%d) y(%d) cx(%d) cy(%d)\n",
1398 himl, himl->hbmMask, pimldp->i, pimldp->x, pimldp->y, cx, cy);
1400 /* we will use these DCs to access the images and masks in the ImageList */
1401 hImageListDC = himl->hdcImage;
1402 hMaskListDC = himl->hdcMask;
1404 /* these will accumulate the image and mask for the image we're drawing */
1405 hImageDC = CreateCompatibleDC( pimldp->hdcDst );
1406 hImageBmp = CreateCompatibleBitmap( pimldp->hdcDst, cx, cy );
1407 hBlendMaskBmp = bBlend ? CreateBitmap(cx, cy, 1, 1, NULL) : 0;
1409 /* Create a compatible DC. */
1410 if (!hImageListDC || !hImageDC || !hImageBmp ||
1411 (bBlend && !hBlendMaskBmp) || (himl->hbmMask && !hMaskListDC))
1412 goto cleanup;
1414 hOldImageBmp = SelectObject(hImageDC, hImageBmp);
1417 * To obtain a transparent look, background color should be set
1418 * to white and foreground color to black when blitting the
1419 * monochrome mask.
1421 oldImageFg = SetTextColor( hImageDC, RGB( 0, 0, 0 ) );
1422 oldImageBk = SetBkColor( hImageDC, RGB( 0xff, 0xff, 0xff ) );
1424 has_alpha = himl->item_flags[pimldp->i] & ILIF_ALPHA;
1425 if (!bMask && (has_alpha || (fState & ILS_ALPHA) || (fState & ILS_SATURATE)))
1427 COLORREF colour, blend_col = CLR_NONE;
1428 BLENDFUNCTION func;
1430 if (bBlend)
1432 blend_col = pimldp->rgbFg;
1433 if (blend_col == CLR_DEFAULT) blend_col = GetSysColor( COLOR_HIGHLIGHT );
1434 else if (blend_col == CLR_NONE) blend_col = GetTextColor( pimldp->hdcDst );
1437 func.BlendOp = AC_SRC_OVER;
1438 func.BlendFlags = 0;
1439 func.SourceConstantAlpha = (fState & ILS_ALPHA) ? pimldp->Frame : 255;
1440 func.AlphaFormat = AC_SRC_ALPHA;
1442 if (bIsTransparent)
1444 bResult = alpha_blend_image( himl, pimldp->hdcDst, pimldp->x, pimldp->y, pt.x, pt.y, cx, cy,
1445 func, fStyle, blend_col, has_alpha, fState & ILS_SATURATE );
1446 goto end;
1448 colour = pimldp->rgbBk;
1449 if (colour == CLR_DEFAULT) colour = himl->clrBk;
1450 if (colour == CLR_NONE) colour = GetBkColor( pimldp->hdcDst );
1452 hOldBrush = SelectObject (hImageDC, CreateSolidBrush (colour));
1453 PatBlt( hImageDC, 0, 0, cx, cy, PATCOPY );
1454 alpha_blend_image( himl, hImageDC, 0, 0, pt.x, pt.y, cx, cy, func,
1455 fStyle, blend_col, has_alpha, fState & ILS_SATURATE );
1456 DeleteObject (SelectObject (hImageDC, hOldBrush));
1457 bResult = BitBlt( pimldp->hdcDst, pimldp->x, pimldp->y, cx, cy, hImageDC, 0, 0, SRCCOPY );
1458 goto end;
1462 * Draw the initial image
1464 if( bMask ) {
1465 if (himl->hbmMask) {
1466 hOldBrush = SelectObject (hImageDC, CreateSolidBrush (GetTextColor(pimldp->hdcDst)));
1467 PatBlt( hImageDC, 0, 0, cx, cy, PATCOPY );
1468 BitBlt(hImageDC, 0, 0, cx, cy, hMaskListDC, pt.x, pt.y, SRCPAINT);
1469 DeleteObject (SelectObject (hImageDC, hOldBrush));
1470 if( bIsTransparent )
1472 BitBlt ( pimldp->hdcDst, pimldp->x, pimldp->y, cx, cy, hImageDC, 0, 0, SRCAND);
1473 bResult = TRUE;
1474 goto end;
1476 } else {
1477 hOldBrush = SelectObject (hImageDC, GetStockObject(BLACK_BRUSH));
1478 PatBlt( hImageDC, 0, 0, cx, cy, PATCOPY);
1479 SelectObject(hImageDC, hOldBrush);
1481 } else {
1482 /* blend the image with the needed solid background */
1483 COLORREF colour = RGB(0,0,0);
1485 if( !bIsTransparent )
1487 colour = pimldp->rgbBk;
1488 if( colour == CLR_DEFAULT )
1489 colour = himl->clrBk;
1490 if( colour == CLR_NONE )
1491 colour = GetBkColor(pimldp->hdcDst);
1494 hOldBrush = SelectObject (hImageDC, CreateSolidBrush (colour));
1495 PatBlt( hImageDC, 0, 0, cx, cy, PATCOPY );
1496 if (himl->hbmMask)
1498 BitBlt( hImageDC, 0, 0, cx, cy, hMaskListDC, pt.x, pt.y, SRCAND );
1499 BitBlt( hImageDC, 0, 0, cx, cy, hImageListDC, pt.x, pt.y, SRCPAINT );
1501 else
1502 BitBlt( hImageDC, 0, 0, cx, cy, hImageListDC, pt.x, pt.y, SRCCOPY);
1503 DeleteObject (SelectObject (hImageDC, hOldBrush));
1506 /* Time for blending, if required */
1507 if (bBlend) {
1508 HBRUSH hBlendBrush;
1509 COLORREF clrBlend = pimldp->rgbFg;
1510 HDC hBlendMaskDC = hImageListDC;
1511 HBITMAP hOldBitmap;
1513 /* Create the blend Mask */
1514 hOldBitmap = SelectObject(hBlendMaskDC, hBlendMaskBmp);
1515 hBlendBrush = fStyle & ILD_BLEND50 ? himl->hbrBlend50 : himl->hbrBlend25;
1516 hOldBrush = SelectObject(hBlendMaskDC, hBlendBrush);
1517 PatBlt(hBlendMaskDC, 0, 0, cx, cy, PATCOPY);
1518 SelectObject(hBlendMaskDC, hOldBrush);
1520 /* Modify the blend mask if an Image Mask exist */
1521 if(himl->hbmMask) {
1522 BitBlt(hBlendMaskDC, 0, 0, cx, cy, hMaskListDC, pt.x, pt.y, 0x220326); /* NOTSRCAND */
1523 BitBlt(hBlendMaskDC, 0, 0, cx, cy, hBlendMaskDC, 0, 0, NOTSRCCOPY);
1526 /* now apply blend to the current image given the BlendMask */
1527 if (clrBlend == CLR_DEFAULT) clrBlend = GetSysColor (COLOR_HIGHLIGHT);
1528 else if (clrBlend == CLR_NONE) clrBlend = GetTextColor (pimldp->hdcDst);
1529 hOldBrush = SelectObject (hImageDC, CreateSolidBrush(clrBlend));
1530 BitBlt (hImageDC, 0, 0, cx, cy, hBlendMaskDC, 0, 0, 0xB8074A); /* PSDPxax */
1531 DeleteObject(SelectObject(hImageDC, hOldBrush));
1532 SelectObject(hBlendMaskDC, hOldBitmap);
1535 /* Now do the overlay image, if any */
1536 nOvlIdx = (pimldp->fStyle & ILD_OVERLAYMASK) >> 8;
1537 if ( (nOvlIdx >= 1) && (nOvlIdx <= MAX_OVERLAYIMAGE)) {
1538 nOvlIdx = himl->nOvlIdx[nOvlIdx - 1];
1539 if ((nOvlIdx >= 0) && (nOvlIdx < himl->cCurImage)) {
1540 POINT ptOvl;
1541 imagelist_point_from_index( himl, nOvlIdx, &ptOvl );
1542 ptOvl.x += pimldp->xBitmap;
1543 if (himl->hbmMask && !(fStyle & ILD_IMAGE))
1544 BitBlt (hImageDC, 0, 0, cx, cy, hMaskListDC, ptOvl.x, ptOvl.y, SRCAND);
1545 BitBlt (hImageDC, 0, 0, cx, cy, hImageListDC, ptOvl.x, ptOvl.y, SRCPAINT);
1549 if (fState & ILS_GLOW) FIXME("ILS_GLOW: unimplemented!\n");
1550 if (fState & ILS_SHADOW) FIXME("ILS_SHADOW: unimplemented!\n");
1552 if (fStyle & ILD_PRESERVEALPHA) FIXME("ILD_PRESERVEALPHA: unimplemented!\n");
1553 if (fStyle & ILD_SCALE) FIXME("ILD_SCALE: unimplemented!\n");
1554 if (fStyle & ILD_DPISCALE) FIXME("ILD_DPISCALE: unimplemented!\n");
1556 /* now copy the image to the screen */
1557 dwRop = SRCCOPY;
1558 if (himl->hbmMask && bIsTransparent ) {
1559 COLORREF oldDstFg = SetTextColor(pimldp->hdcDst, RGB( 0, 0, 0 ) );
1560 COLORREF oldDstBk = SetBkColor(pimldp->hdcDst, RGB( 0xff, 0xff, 0xff ));
1561 BitBlt (pimldp->hdcDst, pimldp->x, pimldp->y, cx, cy, hMaskListDC, pt.x, pt.y, SRCAND);
1562 SetBkColor(pimldp->hdcDst, oldDstBk);
1563 SetTextColor(pimldp->hdcDst, oldDstFg);
1564 dwRop = SRCPAINT;
1566 if (fStyle & ILD_ROP) dwRop = pimldp->dwRop;
1567 BitBlt (pimldp->hdcDst, pimldp->x, pimldp->y, cx, cy, hImageDC, 0, 0, dwRop);
1569 bResult = TRUE;
1570 end:
1571 /* cleanup the mess */
1572 SetBkColor(hImageDC, oldImageBk);
1573 SetTextColor(hImageDC, oldImageFg);
1574 SelectObject(hImageDC, hOldImageBmp);
1575 cleanup:
1576 DeleteObject(hBlendMaskBmp);
1577 DeleteObject(hImageBmp);
1578 DeleteDC(hImageDC);
1580 return bResult;
1584 /*************************************************************************
1585 * ImageList_Duplicate [COMCTL32.@]
1587 * Duplicates an image list.
1589 * PARAMS
1590 * himlSrc [I] source image list handle
1592 * RETURNS
1593 * Success: Handle of duplicated image list.
1594 * Failure: NULL
1597 HIMAGELIST WINAPI
1598 ImageList_Duplicate (HIMAGELIST himlSrc)
1600 HIMAGELIST himlDst;
1602 if (!is_valid(himlSrc)) {
1603 ERR("Invalid image list handle!\n");
1604 return NULL;
1607 himlDst = ImageList_Create (himlSrc->cx, himlSrc->cy, himlSrc->flags,
1608 himlSrc->cCurImage, himlSrc->cGrow);
1610 if (himlDst)
1612 SIZE sz;
1614 imagelist_get_bitmap_size(himlSrc, himlSrc->cCurImage, &sz);
1615 BitBlt (himlDst->hdcImage, 0, 0, sz.cx, sz.cy,
1616 himlSrc->hdcImage, 0, 0, SRCCOPY);
1618 if (himlDst->hbmMask)
1619 BitBlt (himlDst->hdcMask, 0, 0, sz.cx, sz.cy,
1620 himlSrc->hdcMask, 0, 0, SRCCOPY);
1622 himlDst->cCurImage = himlSrc->cCurImage;
1623 memcpy( himlDst->item_flags, himlSrc->item_flags, himlDst->cCurImage * sizeof(*himlDst->item_flags) );
1625 return himlDst;
1629 /*************************************************************************
1630 * ImageList_EndDrag [COMCTL32.@]
1632 * Finishes a drag operation.
1634 * PARAMS
1635 * no Parameters
1637 * RETURNS
1638 * Success: TRUE
1639 * Failure: FALSE
1642 VOID WINAPI
1643 ImageList_EndDrag (void)
1645 /* cleanup the InternalDrag struct */
1646 InternalDrag.hwnd = 0;
1647 if (InternalDrag.himl != InternalDrag.himlNoCursor)
1648 ImageList_Destroy (InternalDrag.himlNoCursor);
1649 ImageList_Destroy (InternalDrag.himl);
1650 InternalDrag.himlNoCursor = InternalDrag.himl = 0;
1651 InternalDrag.x= 0;
1652 InternalDrag.y= 0;
1653 InternalDrag.dxHotspot = 0;
1654 InternalDrag.dyHotspot = 0;
1655 InternalDrag.bShow = FALSE;
1656 DeleteObject(InternalDrag.hbmBg);
1657 InternalDrag.hbmBg = 0;
1661 /*************************************************************************
1662 * ImageList_GetBkColor [COMCTL32.@]
1664 * Returns the background color of an image list.
1666 * PARAMS
1667 * himl [I] Image list handle.
1669 * RETURNS
1670 * Success: background color
1671 * Failure: CLR_NONE
1674 COLORREF WINAPI
1675 ImageList_GetBkColor (HIMAGELIST himl)
1677 return himl ? himl->clrBk : CLR_NONE;
1681 /*************************************************************************
1682 * ImageList_GetDragImage [COMCTL32.@]
1684 * Returns the handle to the internal drag image list.
1686 * PARAMS
1687 * ppt [O] Pointer to the drag position. Can be NULL.
1688 * pptHotspot [O] Pointer to the position of the hot spot. Can be NULL.
1690 * RETURNS
1691 * Success: Handle of the drag image list.
1692 * Failure: NULL.
1695 HIMAGELIST WINAPI
1696 ImageList_GetDragImage (POINT *ppt, POINT *pptHotspot)
1698 if (is_valid(InternalDrag.himl)) {
1699 if (ppt) {
1700 ppt->x = InternalDrag.x;
1701 ppt->y = InternalDrag.y;
1703 if (pptHotspot) {
1704 pptHotspot->x = InternalDrag.dxHotspot;
1705 pptHotspot->y = InternalDrag.dyHotspot;
1707 return (InternalDrag.himl);
1710 return NULL;
1714 /*************************************************************************
1715 * ImageList_GetFlags [COMCTL32.@]
1717 * Gets the flags of the specified image list.
1719 * PARAMS
1720 * himl [I] Handle to image list
1722 * RETURNS
1723 * Image list flags.
1725 * BUGS
1726 * Stub.
1729 DWORD WINAPI
1730 ImageList_GetFlags(HIMAGELIST himl)
1732 TRACE("%p\n", himl);
1734 return is_valid(himl) ? himl->flags : 0;
1738 /*************************************************************************
1739 * ImageList_GetIcon [COMCTL32.@]
1741 * Creates an icon from a masked image of an image list.
1743 * PARAMS
1744 * himl [I] handle to image list
1745 * i [I] image index
1746 * flags [I] drawing style flags
1748 * RETURNS
1749 * Success: icon handle
1750 * Failure: NULL
1753 HICON WINAPI
1754 ImageList_GetIcon (HIMAGELIST himl, INT i, UINT fStyle)
1756 ICONINFO ii;
1757 HICON hIcon;
1758 HBITMAP hOldDstBitmap;
1759 HDC hdcDst;
1760 POINT pt;
1762 TRACE("%p %d %d\n", himl, i, fStyle);
1763 if (!is_valid(himl) || (i < 0) || (i >= himl->cCurImage)) return NULL;
1765 ii.fIcon = TRUE;
1766 ii.xHotspot = 0;
1767 ii.yHotspot = 0;
1769 /* create colour bitmap */
1770 hdcDst = GetDC(0);
1771 ii.hbmColor = CreateCompatibleBitmap(hdcDst, himl->cx, himl->cy);
1772 ReleaseDC(0, hdcDst);
1774 hdcDst = CreateCompatibleDC(0);
1776 imagelist_point_from_index( himl, i, &pt );
1778 /* draw mask*/
1779 ii.hbmMask = CreateBitmap (himl->cx, himl->cy, 1, 1, NULL);
1780 hOldDstBitmap = SelectObject (hdcDst, ii.hbmMask);
1781 if (himl->hbmMask) {
1782 BitBlt (hdcDst, 0, 0, himl->cx, himl->cy,
1783 himl->hdcMask, pt.x, pt.y, SRCCOPY);
1785 else
1786 PatBlt (hdcDst, 0, 0, himl->cx, himl->cy, BLACKNESS);
1788 /* draw image*/
1789 SelectObject (hdcDst, ii.hbmColor);
1790 BitBlt (hdcDst, 0, 0, himl->cx, himl->cy,
1791 himl->hdcImage, pt.x, pt.y, SRCCOPY);
1794 * CreateIconIndirect requires us to deselect the bitmaps from
1795 * the DCs before calling
1797 SelectObject(hdcDst, hOldDstBitmap);
1799 hIcon = CreateIconIndirect (&ii);
1801 DeleteObject (ii.hbmMask);
1802 DeleteObject (ii.hbmColor);
1803 DeleteDC (hdcDst);
1805 return hIcon;
1809 /*************************************************************************
1810 * ImageList_GetIconSize [COMCTL32.@]
1812 * Retrieves the size of an image in an image list.
1814 * PARAMS
1815 * himl [I] handle to image list
1816 * cx [O] pointer to the image width.
1817 * cy [O] pointer to the image height.
1819 * RETURNS
1820 * Success: TRUE
1821 * Failure: FALSE
1823 * NOTES
1824 * All images in an image list have the same size.
1827 BOOL WINAPI
1828 ImageList_GetIconSize (HIMAGELIST himl, INT *cx, INT *cy)
1830 if (!is_valid(himl) || !cx || !cy)
1831 return FALSE;
1833 *cx = himl->cx;
1834 *cy = himl->cy;
1836 return TRUE;
1840 /*************************************************************************
1841 * ImageList_GetImageCount [COMCTL32.@]
1843 * Returns the number of images in an image list.
1845 * PARAMS
1846 * himl [I] handle to image list
1848 * RETURNS
1849 * Success: Number of images.
1850 * Failure: 0
1853 INT WINAPI
1854 ImageList_GetImageCount (HIMAGELIST himl)
1856 if (!is_valid(himl))
1857 return 0;
1859 return himl->cCurImage;
1863 /*************************************************************************
1864 * ImageList_GetImageInfo [COMCTL32.@]
1866 * Returns information about an image in an image list.
1868 * PARAMS
1869 * himl [I] handle to image list
1870 * i [I] image index
1871 * pImageInfo [O] pointer to the image information
1873 * RETURNS
1874 * Success: TRUE
1875 * Failure: FALSE
1878 BOOL WINAPI
1879 ImageList_GetImageInfo (HIMAGELIST himl, INT i, IMAGEINFO *pImageInfo)
1881 POINT pt;
1883 if (!is_valid(himl) || (pImageInfo == NULL))
1884 return FALSE;
1885 if ((i < 0) || (i >= himl->cCurImage))
1886 return FALSE;
1888 pImageInfo->hbmImage = himl->hbmImage;
1889 pImageInfo->hbmMask = himl->hbmMask;
1891 imagelist_point_from_index( himl, i, &pt );
1892 pImageInfo->rcImage.top = pt.y;
1893 pImageInfo->rcImage.bottom = pt.y + himl->cy;
1894 pImageInfo->rcImage.left = pt.x;
1895 pImageInfo->rcImage.right = pt.x + himl->cx;
1897 return TRUE;
1901 /*************************************************************************
1902 * ImageList_GetImageRect [COMCTL32.@]
1904 * Retrieves the rectangle of the specified image in an image list.
1906 * PARAMS
1907 * himl [I] handle to image list
1908 * i [I] image index
1909 * lpRect [O] pointer to the image rectangle
1911 * RETURNS
1912 * Success: TRUE
1913 * Failure: FALSE
1915 * NOTES
1916 * This is an UNDOCUMENTED function!!!
1919 BOOL WINAPI
1920 ImageList_GetImageRect (HIMAGELIST himl, INT i, LPRECT lpRect)
1922 POINT pt;
1924 if (!is_valid(himl) || (lpRect == NULL))
1925 return FALSE;
1926 if ((i < 0) || (i >= himl->cCurImage))
1927 return FALSE;
1929 imagelist_point_from_index( himl, i, &pt );
1930 lpRect->left = pt.x;
1931 lpRect->top = pt.y;
1932 lpRect->right = pt.x + himl->cx;
1933 lpRect->bottom = pt.y + himl->cy;
1935 return TRUE;
1939 /*************************************************************************
1940 * ImageList_LoadImage [COMCTL32.@]
1941 * ImageList_LoadImageA [COMCTL32.@]
1943 * Creates an image list from a bitmap, icon or cursor.
1945 * See ImageList_LoadImageW.
1948 HIMAGELIST WINAPI
1949 ImageList_LoadImageA (HINSTANCE hi, LPCSTR lpbmp, INT cx, INT cGrow,
1950 COLORREF clrMask, UINT uType, UINT uFlags)
1952 HIMAGELIST himl;
1953 LPWSTR lpbmpW;
1954 DWORD len;
1956 if (IS_INTRESOURCE(lpbmp))
1957 return ImageList_LoadImageW(hi, (LPCWSTR)lpbmp, cx, cGrow, clrMask,
1958 uType, uFlags);
1960 len = MultiByteToWideChar(CP_ACP, 0, lpbmp, -1, NULL, 0);
1961 lpbmpW = heap_alloc(len * sizeof(WCHAR));
1962 MultiByteToWideChar(CP_ACP, 0, lpbmp, -1, lpbmpW, len);
1964 himl = ImageList_LoadImageW(hi, lpbmpW, cx, cGrow, clrMask, uType, uFlags);
1965 heap_free (lpbmpW);
1966 return himl;
1970 /*************************************************************************
1971 * ImageList_LoadImageW [COMCTL32.@]
1973 * Creates an image list from a bitmap, icon or cursor.
1975 * PARAMS
1976 * hi [I] instance handle
1977 * lpbmp [I] name or id of the image
1978 * cx [I] width of each image
1979 * cGrow [I] number of images to expand
1980 * clrMask [I] mask color
1981 * uType [I] type of image to load
1982 * uFlags [I] loading flags
1984 * RETURNS
1985 * Success: handle to the loaded image list
1986 * Failure: NULL
1988 * SEE
1989 * LoadImage ()
1992 HIMAGELIST WINAPI
1993 ImageList_LoadImageW (HINSTANCE hi, LPCWSTR lpbmp, INT cx, INT cGrow,
1994 COLORREF clrMask, UINT uType, UINT uFlags)
1996 HIMAGELIST himl = NULL;
1997 HANDLE handle;
1998 INT nImageCount;
2000 handle = LoadImageW (hi, lpbmp, uType, 0, 0, uFlags);
2001 if (!handle) {
2002 WARN("Couldn't load image\n");
2003 return NULL;
2006 if (uType == IMAGE_BITMAP) {
2007 DIBSECTION dib;
2008 UINT color;
2010 if (GetObjectW (handle, sizeof(dib), &dib) == sizeof(BITMAP)) color = ILC_COLOR;
2011 else color = dib.dsBm.bmBitsPixel;
2013 /* To match windows behavior, if cx is set to zero and
2014 the flag DI_DEFAULTSIZE is specified, cx becomes the
2015 system metric value for icons. If the flag is not specified
2016 the function sets the size to the height of the bitmap */
2017 if (cx == 0)
2019 if (uFlags & DI_DEFAULTSIZE)
2020 cx = GetSystemMetrics (SM_CXICON);
2021 else
2022 cx = dib.dsBm.bmHeight;
2025 nImageCount = dib.dsBm.bmWidth / cx;
2027 if (clrMask != CLR_NONE) color |= ILC_MASK;
2028 himl = ImageList_Create (cx, dib.dsBm.bmHeight, color, nImageCount, cGrow);
2029 if (!himl) {
2030 DeleteObject (handle);
2031 return NULL;
2033 ImageList_AddMasked (himl, handle, clrMask);
2035 else if ((uType == IMAGE_ICON) || (uType == IMAGE_CURSOR)) {
2036 ICONINFO ii;
2037 BITMAP bmp;
2039 GetIconInfo (handle, &ii);
2040 GetObjectW (ii.hbmColor, sizeof(BITMAP), &bmp);
2041 himl = ImageList_Create (bmp.bmWidth, bmp.bmHeight,
2042 ILC_MASK | ILC_COLOR, 1, cGrow);
2043 if (!himl) {
2044 DeleteObject (ii.hbmColor);
2045 DeleteObject (ii.hbmMask);
2046 DeleteObject (handle);
2047 return NULL;
2049 ImageList_Add (himl, ii.hbmColor, ii.hbmMask);
2050 DeleteObject (ii.hbmColor);
2051 DeleteObject (ii.hbmMask);
2054 DeleteObject (handle);
2056 return himl;
2060 /*************************************************************************
2061 * ImageList_Merge [COMCTL32.@]
2063 * Create an image list containing a merged image from two image lists.
2065 * PARAMS
2066 * himl1 [I] handle to first image list
2067 * i1 [I] first image index
2068 * himl2 [I] handle to second image list
2069 * i2 [I] second image index
2070 * dx [I] X offset of the second image relative to the first.
2071 * dy [I] Y offset of the second image relative to the first.
2073 * RETURNS
2074 * Success: The newly created image list. It contains a single image
2075 * consisting of the second image merged with the first.
2076 * Failure: NULL, if either himl1 or himl2 is invalid.
2078 * NOTES
2079 * - The returned image list should be deleted by the caller using
2080 * ImageList_Destroy() when it is no longer required.
2081 * - If either i1 or i2 is not a valid image index, they will be treated
2082 * as blank images.
2084 HIMAGELIST WINAPI
2085 ImageList_Merge (HIMAGELIST himl1, INT i1, HIMAGELIST himl2, INT i2,
2086 INT dx, INT dy)
2088 HIMAGELIST himlDst = NULL;
2089 INT cxDst, cyDst;
2090 INT xOff1, yOff1, xOff2, yOff2;
2091 POINT pt1, pt2;
2092 INT newFlags;
2094 TRACE("(himl1=%p i1=%d himl2=%p i2=%d dx=%d dy=%d)\n", himl1, i1, himl2,
2095 i2, dx, dy);
2097 if (!is_valid(himl1) || !is_valid(himl2))
2098 return NULL;
2100 if (dx > 0) {
2101 cxDst = max (himl1->cx, dx + himl2->cx);
2102 xOff1 = 0;
2103 xOff2 = dx;
2105 else if (dx < 0) {
2106 cxDst = max (himl2->cx, himl1->cx - dx);
2107 xOff1 = -dx;
2108 xOff2 = 0;
2110 else {
2111 cxDst = max (himl1->cx, himl2->cx);
2112 xOff1 = 0;
2113 xOff2 = 0;
2116 if (dy > 0) {
2117 cyDst = max (himl1->cy, dy + himl2->cy);
2118 yOff1 = 0;
2119 yOff2 = dy;
2121 else if (dy < 0) {
2122 cyDst = max (himl2->cy, himl1->cy - dy);
2123 yOff1 = -dy;
2124 yOff2 = 0;
2126 else {
2127 cyDst = max (himl1->cy, himl2->cy);
2128 yOff1 = 0;
2129 yOff2 = 0;
2132 newFlags = (himl1->flags > himl2->flags ? himl1->flags : himl2->flags) & ILC_COLORDDB;
2133 if (newFlags == ILC_COLORDDB && (himl1->flags & ILC_COLORDDB) == ILC_COLOR16)
2134 newFlags = ILC_COLOR16; /* this is what native (at least v5) does, don't know why */
2135 himlDst = ImageList_Create (cxDst, cyDst, ILC_MASK | newFlags, 1, 1);
2137 if (himlDst)
2139 imagelist_point_from_index( himl1, i1, &pt1 );
2140 imagelist_point_from_index( himl2, i2, &pt2 );
2142 /* copy image */
2143 BitBlt (himlDst->hdcImage, 0, 0, cxDst, cyDst, himl1->hdcImage, 0, 0, BLACKNESS);
2144 if (i1 >= 0 && i1 < himl1->cCurImage)
2145 BitBlt (himlDst->hdcImage, xOff1, yOff1, himl1->cx, himl1->cy, himl1->hdcImage, pt1.x, pt1.y, SRCCOPY);
2146 if (i2 >= 0 && i2 < himl2->cCurImage)
2148 if (himl2->flags & ILC_MASK)
2150 BitBlt (himlDst->hdcImage, xOff2, yOff2, himl2->cx, himl2->cy, himl2->hdcMask , pt2.x, pt2.y, SRCAND);
2151 BitBlt (himlDst->hdcImage, xOff2, yOff2, himl2->cx, himl2->cy, himl2->hdcImage, pt2.x, pt2.y, SRCPAINT);
2153 else
2154 BitBlt (himlDst->hdcImage, xOff2, yOff2, himl2->cx, himl2->cy, himl2->hdcImage, pt2.x, pt2.y, SRCCOPY);
2157 /* copy mask */
2158 BitBlt (himlDst->hdcMask, 0, 0, cxDst, cyDst, himl1->hdcMask, 0, 0, WHITENESS);
2159 if (i1 >= 0 && i1 < himl1->cCurImage)
2160 BitBlt (himlDst->hdcMask, xOff1, yOff1, himl1->cx, himl1->cy, himl1->hdcMask, pt1.x, pt1.y, SRCCOPY);
2161 if (i2 >= 0 && i2 < himl2->cCurImage)
2162 BitBlt (himlDst->hdcMask, xOff2, yOff2, himl2->cx, himl2->cy, himl2->hdcMask, pt2.x, pt2.y, SRCAND);
2164 himlDst->cCurImage = 1;
2167 return himlDst;
2171 /* helper for ImageList_Read, see comments below */
2172 static void *read_bitmap(IStream *pstm, BITMAPINFO *bmi)
2174 BITMAPFILEHEADER bmfh;
2175 int bitsperpixel, palspace;
2176 void *bits;
2178 if (FAILED(IStream_Read ( pstm, &bmfh, sizeof(bmfh), NULL)))
2179 return NULL;
2181 if (bmfh.bfType != (('M'<<8)|'B'))
2182 return NULL;
2184 if (FAILED(IStream_Read ( pstm, &bmi->bmiHeader, sizeof(bmi->bmiHeader), NULL)))
2185 return NULL;
2187 if ((bmi->bmiHeader.biSize != sizeof(bmi->bmiHeader)))
2188 return NULL;
2190 TRACE("width %u, height %u, planes %u, bpp %u\n",
2191 bmi->bmiHeader.biWidth, bmi->bmiHeader.biHeight,
2192 bmi->bmiHeader.biPlanes, bmi->bmiHeader.biBitCount);
2194 bitsperpixel = bmi->bmiHeader.biPlanes * bmi->bmiHeader.biBitCount;
2195 if (bitsperpixel<=8)
2196 palspace = (1<<bitsperpixel)*sizeof(RGBQUAD);
2197 else
2198 palspace = 0;
2200 bmi->bmiHeader.biSizeImage = get_dib_image_size( bmi );
2202 /* read the palette right after the end of the bitmapinfoheader */
2203 if (palspace && FAILED(IStream_Read(pstm, bmi->bmiColors, palspace, NULL)))
2204 return NULL;
2206 bits = heap_alloc_zero(bmi->bmiHeader.biSizeImage);
2207 if (!bits) return NULL;
2209 if (FAILED(IStream_Read(pstm, bits, bmi->bmiHeader.biSizeImage, NULL)))
2211 heap_free(bits);
2212 return NULL;
2214 return bits;
2217 /*************************************************************************
2218 * ImageList_Read [COMCTL32.@]
2220 * Reads an image list from a stream.
2222 * PARAMS
2223 * pstm [I] pointer to a stream
2225 * RETURNS
2226 * Success: handle to image list
2227 * Failure: NULL
2229 * The format is like this:
2230 * ILHEAD ilheadstruct;
2232 * for the color image part:
2233 * BITMAPFILEHEADER bmfh;
2234 * BITMAPINFOHEADER bmih;
2235 * only if it has a palette:
2236 * RGBQUAD rgbs[nr_of_paletted_colors];
2238 * BYTE colorbits[imagesize];
2240 * the following only if the ILC_MASK bit is set in ILHEAD.ilFlags:
2241 * BITMAPFILEHEADER bmfh_mask;
2242 * BITMAPINFOHEADER bmih_mask;
2243 * only if it has a palette (it usually does not):
2244 * RGBQUAD rgbs[nr_of_paletted_colors];
2246 * BYTE maskbits[imagesize];
2248 HIMAGELIST WINAPI ImageList_Read(IStream *pstm)
2250 char image_buf[sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * 256];
2251 char mask_buf[sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * 256];
2252 BITMAPINFO *image_info = (BITMAPINFO *)image_buf;
2253 BITMAPINFO *mask_info = (BITMAPINFO *)mask_buf;
2254 void *image_bits, *mask_bits = NULL;
2255 ILHEAD ilHead;
2256 HIMAGELIST himl;
2257 unsigned int i;
2259 TRACE("%p\n", pstm);
2261 if (FAILED(IStream_Read (pstm, &ilHead, sizeof(ILHEAD), NULL)))
2262 return NULL;
2263 if (ilHead.usMagic != (('L' << 8) | 'I'))
2264 return NULL;
2265 if (ilHead.usVersion != 0x101) /* probably version? */
2266 return NULL;
2268 TRACE("cx %u, cy %u, flags 0x%04x, cCurImage %u, cMaxImage %u\n",
2269 ilHead.cx, ilHead.cy, ilHead.flags, ilHead.cCurImage, ilHead.cMaxImage);
2271 himl = ImageList_Create(ilHead.cx, ilHead.cy, ilHead.flags, ilHead.cMaxImage, ilHead.cGrow);
2272 if (!himl)
2273 return NULL;
2275 if (!(image_bits = read_bitmap(pstm, image_info)))
2277 WARN("failed to read bitmap from stream\n");
2278 return NULL;
2280 if (ilHead.flags & ILC_MASK)
2282 if (!(mask_bits = read_bitmap(pstm, mask_info)))
2284 WARN("failed to read mask bitmap from stream\n");
2285 return NULL;
2288 else mask_info = NULL;
2290 if ((himl->flags & 0xfe) == ILC_COLOR32 && image_info->bmiHeader.biBitCount == 32)
2292 DWORD *ptr = image_bits;
2293 BYTE *mask_ptr = mask_bits;
2294 int stride = himl->cy * image_info->bmiHeader.biWidth;
2296 if (image_info->bmiHeader.biHeight > 0) /* bottom-up */
2298 ptr += image_info->bmiHeader.biHeight * image_info->bmiHeader.biWidth - stride;
2299 mask_ptr += (image_info->bmiHeader.biHeight * image_info->bmiHeader.biWidth - stride) / 8;
2300 stride = -stride;
2301 image_info->bmiHeader.biHeight = himl->cy;
2303 else image_info->bmiHeader.biHeight = -himl->cy;
2305 for (i = 0; i < ilHead.cCurImage; i += TILE_COUNT)
2307 add_dib_bits( himl, i, min( ilHead.cCurImage - i, TILE_COUNT ),
2308 himl->cx, himl->cy, image_info, mask_info, ptr, mask_ptr );
2309 ptr += stride;
2310 mask_ptr += stride / 8;
2313 else
2315 StretchDIBits( himl->hdcImage, 0, 0, image_info->bmiHeader.biWidth, image_info->bmiHeader.biHeight,
2316 0, 0, image_info->bmiHeader.biWidth, image_info->bmiHeader.biHeight,
2317 image_bits, image_info, DIB_RGB_COLORS, SRCCOPY);
2318 if (mask_info)
2319 StretchDIBits( himl->hdcMask, 0, 0, mask_info->bmiHeader.biWidth, mask_info->bmiHeader.biHeight,
2320 0, 0, mask_info->bmiHeader.biWidth, mask_info->bmiHeader.biHeight,
2321 mask_bits, mask_info, DIB_RGB_COLORS, SRCCOPY);
2323 heap_free( image_bits );
2324 heap_free( mask_bits );
2326 himl->cCurImage = ilHead.cCurImage;
2327 himl->cMaxImage = ilHead.cMaxImage;
2329 ImageList_SetBkColor(himl,ilHead.bkcolor);
2330 for (i=0;i<4;i++)
2331 ImageList_SetOverlayImage(himl,ilHead.ovls[i],i+1);
2332 return himl;
2336 /*************************************************************************
2337 * ImageList_Remove [COMCTL32.@]
2339 * Removes an image from an image list
2341 * PARAMS
2342 * himl [I] image list handle
2343 * i [I] image index
2345 * RETURNS
2346 * Success: TRUE
2347 * Failure: FALSE
2349 * FIXME: as the image list storage test shows, native comctl32 simply shifts
2350 * images without creating a new bitmap.
2352 BOOL WINAPI
2353 ImageList_Remove (HIMAGELIST himl, INT i)
2355 HBITMAP hbmNewImage, hbmNewMask;
2356 HDC hdcBmp;
2357 SIZE sz;
2359 TRACE("(himl=%p i=%d)\n", himl, i);
2361 if (!is_valid(himl)) {
2362 ERR("Invalid image list handle!\n");
2363 return FALSE;
2366 if ((i < -1) || (i >= himl->cCurImage)) {
2367 TRACE("index out of range! %d\n", i);
2368 return FALSE;
2371 if (i == -1) {
2372 INT nCount;
2374 /* remove all */
2375 if (himl->cCurImage == 0) {
2376 /* remove all on empty ImageList is allowed */
2377 TRACE("remove all on empty ImageList!\n");
2378 return TRUE;
2381 himl->cMaxImage = himl->cGrow;
2382 himl->cCurImage = 0;
2383 for (nCount = 0; nCount < MAX_OVERLAYIMAGE; nCount++)
2384 himl->nOvlIdx[nCount] = -1;
2386 heap_free( himl->item_flags );
2387 himl->item_flags = heap_alloc_zero( himl->cMaxImage * sizeof(*himl->item_flags) );
2389 hbmNewImage = ImageList_CreateImage(himl->hdcImage, himl, himl->cMaxImage);
2390 SelectObject (himl->hdcImage, hbmNewImage);
2391 DeleteObject (himl->hbmImage);
2392 himl->hbmImage = hbmNewImage;
2394 if (himl->hbmMask) {
2396 imagelist_get_bitmap_size(himl, himl->cMaxImage, &sz);
2397 hbmNewMask = CreateBitmap (sz.cx, sz.cy, 1, 1, NULL);
2398 SelectObject (himl->hdcMask, hbmNewMask);
2399 DeleteObject (himl->hbmMask);
2400 himl->hbmMask = hbmNewMask;
2403 else {
2404 /* delete one image */
2405 TRACE("Remove single image! %d\n", i);
2407 /* create new bitmap(s) */
2408 TRACE(" - Number of images: %d / %d (Old/New)\n",
2409 himl->cCurImage, himl->cCurImage - 1);
2411 hbmNewImage = ImageList_CreateImage(himl->hdcImage, himl, himl->cMaxImage);
2413 imagelist_get_bitmap_size(himl, himl->cMaxImage, &sz );
2414 if (himl->hbmMask)
2415 hbmNewMask = CreateBitmap (sz.cx, sz.cy, 1, 1, NULL);
2416 else
2417 hbmNewMask = 0; /* Just to keep compiler happy! */
2419 hdcBmp = CreateCompatibleDC (0);
2421 /* copy all images and masks prior to the "removed" image */
2422 if (i > 0) {
2423 TRACE("Pre image copy: Copy %d images\n", i);
2425 SelectObject (hdcBmp, hbmNewImage);
2426 imagelist_copy_images( himl, himl->hdcImage, hdcBmp, 0, i, 0 );
2428 if (himl->hbmMask) {
2429 SelectObject (hdcBmp, hbmNewMask);
2430 imagelist_copy_images( himl, himl->hdcMask, hdcBmp, 0, i, 0 );
2434 /* copy all images and masks behind the removed image */
2435 if (i < himl->cCurImage - 1) {
2436 TRACE("Post image copy!\n");
2438 SelectObject (hdcBmp, hbmNewImage);
2439 imagelist_copy_images( himl, himl->hdcImage, hdcBmp, i + 1,
2440 (himl->cCurImage - i), i );
2442 if (himl->hbmMask) {
2443 SelectObject (hdcBmp, hbmNewMask);
2444 imagelist_copy_images( himl, himl->hdcMask, hdcBmp, i + 1,
2445 (himl->cCurImage - i), i );
2449 DeleteDC (hdcBmp);
2451 /* delete old images and insert new ones */
2452 SelectObject (himl->hdcImage, hbmNewImage);
2453 DeleteObject (himl->hbmImage);
2454 himl->hbmImage = hbmNewImage;
2455 if (himl->hbmMask) {
2456 SelectObject (himl->hdcMask, hbmNewMask);
2457 DeleteObject (himl->hbmMask);
2458 himl->hbmMask = hbmNewMask;
2461 himl->cCurImage--;
2464 return TRUE;
2468 /*************************************************************************
2469 * ImageList_Replace [COMCTL32.@]
2471 * Replaces an image in an image list with a new image.
2473 * PARAMS
2474 * himl [I] handle to image list
2475 * i [I] image index
2476 * hbmImage [I] handle to image bitmap
2477 * hbmMask [I] handle to mask bitmap. Can be NULL.
2479 * RETURNS
2480 * Success: TRUE
2481 * Failure: FALSE
2484 BOOL WINAPI
2485 ImageList_Replace (HIMAGELIST himl, INT i, HBITMAP hbmImage,
2486 HBITMAP hbmMask)
2488 HDC hdcImage;
2489 BITMAP bmp;
2490 POINT pt;
2492 TRACE("%p %d %p %p\n", himl, i, hbmImage, hbmMask);
2494 if (!is_valid(himl)) {
2495 ERR("Invalid image list handle!\n");
2496 return FALSE;
2499 if ((i >= himl->cMaxImage) || (i < 0)) {
2500 ERR("Invalid image index!\n");
2501 return FALSE;
2504 if (!GetObjectW(hbmImage, sizeof(BITMAP), &bmp))
2505 return FALSE;
2507 hdcImage = CreateCompatibleDC (0);
2509 /* Replace Image */
2510 SelectObject (hdcImage, hbmImage);
2512 if (add_with_alpha( himl, hdcImage, i, 1, bmp.bmWidth, bmp.bmHeight, hbmImage, hbmMask ))
2513 goto done;
2515 imagelist_point_from_index(himl, i, &pt);
2516 StretchBlt (himl->hdcImage, pt.x, pt.y, himl->cx, himl->cy,
2517 hdcImage, 0, 0, bmp.bmWidth, bmp.bmHeight, SRCCOPY);
2519 if (himl->hbmMask)
2521 HDC hdcTemp;
2522 HBITMAP hOldBitmapTemp;
2524 hdcTemp = CreateCompatibleDC(0);
2525 hOldBitmapTemp = SelectObject(hdcTemp, hbmMask);
2527 StretchBlt (himl->hdcMask, pt.x, pt.y, himl->cx, himl->cy,
2528 hdcTemp, 0, 0, bmp.bmWidth, bmp.bmHeight, SRCCOPY);
2529 SelectObject(hdcTemp, hOldBitmapTemp);
2530 DeleteDC(hdcTemp);
2532 /* Remove the background from the image
2534 BitBlt (himl->hdcImage, pt.x, pt.y, bmp.bmWidth, bmp.bmHeight,
2535 himl->hdcMask, pt.x, pt.y, 0x220326); /* NOTSRCAND */
2538 done:
2539 DeleteDC (hdcImage);
2541 return TRUE;
2545 /*************************************************************************
2546 * ImageList_ReplaceIcon [COMCTL32.@]
2548 * Replaces an image in an image list using an icon.
2550 * PARAMS
2551 * himl [I] handle to image list
2552 * i [I] image index
2553 * hIcon [I] handle to icon
2555 * RETURNS
2556 * Success: index of the replaced image
2557 * Failure: -1
2560 INT WINAPI
2561 ImageList_ReplaceIcon (HIMAGELIST himl, INT nIndex, HICON hIcon)
2563 HICON hBestFitIcon;
2564 ICONINFO ii;
2565 BITMAP bmp;
2566 BOOL ret;
2567 POINT pt;
2569 TRACE("(%p %d %p)\n", himl, nIndex, hIcon);
2571 if (!is_valid(himl)) {
2572 ERR("invalid image list\n");
2573 return -1;
2575 if ((nIndex >= himl->cMaxImage) || (nIndex < -1)) {
2576 ERR("invalid image index %d / %d\n", nIndex, himl->cMaxImage);
2577 return -1;
2580 hBestFitIcon = CopyImage(
2581 hIcon, IMAGE_ICON,
2582 himl->cx, himl->cy,
2583 LR_COPYFROMRESOURCE);
2584 /* the above will fail if the icon wasn't loaded from a resource, so try
2585 * again without LR_COPYFROMRESOURCE flag */
2586 if (!hBestFitIcon)
2587 hBestFitIcon = CopyImage(
2588 hIcon, IMAGE_ICON,
2589 himl->cx, himl->cy,
2591 if (!hBestFitIcon)
2592 return -1;
2594 if (nIndex == -1) {
2595 if (himl->cCurImage + 1 >= himl->cMaxImage)
2596 IMAGELIST_InternalExpandBitmaps(himl, 1);
2598 nIndex = himl->cCurImage;
2599 himl->cCurImage++;
2602 if ((himl->flags & 0xfe) == ILC_COLOR32 && GetIconInfo (hBestFitIcon, &ii))
2604 HDC hdcImage = CreateCompatibleDC( 0 );
2605 GetObjectW (ii.hbmMask, sizeof(BITMAP), &bmp);
2607 if (!ii.hbmColor)
2609 UINT height = bmp.bmHeight / 2;
2610 HDC hdcMask = CreateCompatibleDC( 0 );
2611 HBITMAP color = CreateBitmap( bmp.bmWidth, height, 1, 1, NULL );
2612 SelectObject( hdcImage, color );
2613 SelectObject( hdcMask, ii.hbmMask );
2614 BitBlt( hdcImage, 0, 0, bmp.bmWidth, height, hdcMask, 0, height, SRCCOPY );
2615 ret = add_with_alpha( himl, hdcImage, nIndex, 1, bmp.bmWidth, height, color, ii.hbmMask );
2616 DeleteDC( hdcMask );
2617 DeleteObject( color );
2619 else ret = add_with_alpha( himl, hdcImage, nIndex, 1, bmp.bmWidth, bmp.bmHeight,
2620 ii.hbmColor, ii.hbmMask );
2622 DeleteDC( hdcImage );
2623 DeleteObject (ii.hbmMask);
2624 if (ii.hbmColor) DeleteObject (ii.hbmColor);
2625 if (ret) goto done;
2628 imagelist_point_from_index(himl, nIndex, &pt);
2630 if (himl->hbmMask)
2632 DrawIconEx( himl->hdcImage, pt.x, pt.y, hBestFitIcon, himl->cx, himl->cy, 0, 0, DI_IMAGE );
2633 PatBlt( himl->hdcMask, pt.x, pt.y, himl->cx, himl->cy, WHITENESS );
2634 DrawIconEx( himl->hdcMask, pt.x, pt.y, hBestFitIcon, himl->cx, himl->cy, 0, 0, DI_MASK );
2636 else
2638 COLORREF color = himl->clrBk != CLR_NONE ? himl->clrBk : comctl32_color.clrWindow;
2639 HBRUSH brush = CreateSolidBrush( GetNearestColor( himl->hdcImage, color ));
2641 SelectObject( himl->hdcImage, brush );
2642 PatBlt( himl->hdcImage, pt.x, pt.y, himl->cx, himl->cy, PATCOPY );
2643 SelectObject( himl->hdcImage, GetStockObject(BLACK_BRUSH) );
2644 DeleteObject( brush );
2645 DrawIconEx( himl->hdcImage, pt.x, pt.y, hBestFitIcon, himl->cx, himl->cy, 0, 0, DI_NORMAL );
2648 done:
2649 DestroyIcon(hBestFitIcon);
2651 TRACE("Insert index = %d, himl->cCurImage = %d\n", nIndex, himl->cCurImage);
2652 return nIndex;
2656 /*************************************************************************
2657 * ImageList_SetBkColor [COMCTL32.@]
2659 * Sets the background color of an image list.
2661 * PARAMS
2662 * himl [I] handle to image list
2663 * clrBk [I] background color
2665 * RETURNS
2666 * Success: previous background color
2667 * Failure: CLR_NONE
2670 COLORREF WINAPI
2671 ImageList_SetBkColor (HIMAGELIST himl, COLORREF clrBk)
2673 COLORREF clrOldBk;
2675 if (!is_valid(himl))
2676 return CLR_NONE;
2678 clrOldBk = himl->clrBk;
2679 himl->clrBk = clrBk;
2680 return clrOldBk;
2684 /*************************************************************************
2685 * ImageList_SetDragCursorImage [COMCTL32.@]
2687 * Combines the specified image with the current drag image
2689 * PARAMS
2690 * himlDrag [I] handle to drag image list
2691 * iDrag [I] drag image index
2692 * dxHotspot [I] X position of the hot spot
2693 * dyHotspot [I] Y position of the hot spot
2695 * RETURNS
2696 * Success: TRUE
2697 * Failure: FALSE
2699 * NOTES
2700 * - The names dxHotspot, dyHotspot are misleading because they have nothing
2701 * to do with a hotspot but are only the offset of the origin of the new
2702 * image relative to the origin of the old image.
2704 * - When this function is called and the drag image is visible, a
2705 * short flickering occurs but this matches the Win9x behavior. It is
2706 * possible to fix the flickering using code like in ImageList_DragMove.
2709 BOOL WINAPI
2710 ImageList_SetDragCursorImage (HIMAGELIST himlDrag, INT iDrag,
2711 INT dxHotspot, INT dyHotspot)
2713 HIMAGELIST himlTemp;
2714 BOOL visible;
2716 if (!is_valid(InternalDrag.himl) || !is_valid(himlDrag))
2717 return FALSE;
2719 TRACE(" dxH=%d dyH=%d nX=%d nY=%d\n",
2720 dxHotspot, dyHotspot, InternalDrag.dxHotspot, InternalDrag.dyHotspot);
2722 visible = InternalDrag.bShow;
2724 himlTemp = ImageList_Merge (InternalDrag.himlNoCursor, 0, himlDrag, iDrag,
2725 dxHotspot, dyHotspot);
2727 if (visible) {
2728 /* hide the drag image */
2729 ImageList_DragShowNolock(FALSE);
2731 if ((InternalDrag.himl->cx != himlTemp->cx) ||
2732 (InternalDrag.himl->cy != himlTemp->cy)) {
2733 /* the size of the drag image changed, invalidate the buffer */
2734 DeleteObject(InternalDrag.hbmBg);
2735 InternalDrag.hbmBg = 0;
2738 if (InternalDrag.himl != InternalDrag.himlNoCursor)
2739 ImageList_Destroy (InternalDrag.himl);
2740 InternalDrag.himl = himlTemp;
2742 if (visible) {
2743 /* show the drag image */
2744 ImageList_DragShowNolock(TRUE);
2747 return TRUE;
2751 /*************************************************************************
2752 * ImageList_SetFilter [COMCTL32.@]
2754 * Sets a filter (or does something completely different)!!???
2755 * It removes 12 Bytes from the stack (3 Parameters).
2757 * PARAMS
2758 * himl [I] SHOULD be a handle to image list
2759 * i [I] COULD be an index?
2760 * dwFilter [I] ???
2762 * RETURNS
2763 * Success: TRUE ???
2764 * Failure: FALSE ???
2766 * BUGS
2767 * This is an UNDOCUMENTED function!!!!
2768 * empty stub.
2771 BOOL WINAPI
2772 ImageList_SetFilter (HIMAGELIST himl, INT i, DWORD dwFilter)
2774 FIXME("(%p 0x%x 0x%x):empty stub!\n", himl, i, dwFilter);
2776 return FALSE;
2780 /*************************************************************************
2781 * ImageList_SetFlags [COMCTL32.@]
2783 * Sets the image list flags.
2785 * PARAMS
2786 * himl [I] Handle to image list
2787 * flags [I] Flags to set
2789 * RETURNS
2790 * Old flags?
2792 * BUGS
2793 * Stub.
2796 DWORD WINAPI
2797 ImageList_SetFlags(HIMAGELIST himl, DWORD flags)
2799 FIXME("(%p %08x):empty stub\n", himl, flags);
2800 return 0;
2804 /*************************************************************************
2805 * ImageList_SetIconSize [COMCTL32.@]
2807 * Sets the image size of the bitmap and deletes all images.
2809 * PARAMS
2810 * himl [I] handle to image list
2811 * cx [I] image width
2812 * cy [I] image height
2814 * RETURNS
2815 * Success: TRUE
2816 * Failure: FALSE
2819 BOOL WINAPI
2820 ImageList_SetIconSize (HIMAGELIST himl, INT cx, INT cy)
2822 INT nCount;
2823 HBITMAP hbmNew;
2825 if (!is_valid(himl))
2826 return FALSE;
2828 /* remove all images */
2829 himl->cMaxImage = himl->cInitial + 1;
2830 himl->cCurImage = 0;
2831 himl->cx = cx;
2832 himl->cy = cy;
2834 /* initialize overlay mask indices */
2835 for (nCount = 0; nCount < MAX_OVERLAYIMAGE; nCount++)
2836 himl->nOvlIdx[nCount] = -1;
2838 hbmNew = ImageList_CreateImage(himl->hdcImage, himl, himl->cMaxImage);
2839 SelectObject (himl->hdcImage, hbmNew);
2840 DeleteObject (himl->hbmImage);
2841 himl->hbmImage = hbmNew;
2843 if (himl->hbmMask) {
2844 SIZE sz;
2845 imagelist_get_bitmap_size(himl, himl->cMaxImage, &sz);
2846 hbmNew = CreateBitmap (sz.cx, sz.cy, 1, 1, NULL);
2847 SelectObject (himl->hdcMask, hbmNew);
2848 DeleteObject (himl->hbmMask);
2849 himl->hbmMask = hbmNew;
2852 return TRUE;
2856 /*************************************************************************
2857 * ImageList_SetImageCount [COMCTL32.@]
2859 * Resizes an image list to the specified number of images.
2861 * PARAMS
2862 * himl [I] handle to image list
2863 * iImageCount [I] number of images in the image list
2865 * RETURNS
2866 * Success: TRUE
2867 * Failure: FALSE
2870 BOOL WINAPI
2871 ImageList_SetImageCount (HIMAGELIST himl, UINT iImageCount)
2873 HDC hdcBitmap;
2874 HBITMAP hbmNewBitmap, hbmOld;
2875 INT nNewCount, nCopyCount;
2877 TRACE("%p %d\n",himl,iImageCount);
2879 if (!is_valid(himl))
2880 return FALSE;
2882 nNewCount = iImageCount + 1;
2883 nCopyCount = min(himl->cCurImage, iImageCount);
2885 hdcBitmap = CreateCompatibleDC (0);
2887 hbmNewBitmap = ImageList_CreateImage(hdcBitmap, himl, nNewCount);
2889 if (hbmNewBitmap != 0)
2891 hbmOld = SelectObject (hdcBitmap, hbmNewBitmap);
2892 imagelist_copy_images( himl, himl->hdcImage, hdcBitmap, 0, nCopyCount, 0 );
2893 SelectObject (hdcBitmap, hbmOld);
2895 /* FIXME: delete 'empty' image space? */
2897 SelectObject (himl->hdcImage, hbmNewBitmap);
2898 DeleteObject (himl->hbmImage);
2899 himl->hbmImage = hbmNewBitmap;
2901 else
2902 ERR("Could not create new image bitmap!\n");
2904 if (himl->hbmMask)
2906 SIZE sz;
2907 imagelist_get_bitmap_size( himl, nNewCount, &sz );
2908 hbmNewBitmap = CreateBitmap (sz.cx, sz.cy, 1, 1, NULL);
2909 if (hbmNewBitmap != 0)
2911 hbmOld = SelectObject (hdcBitmap, hbmNewBitmap);
2912 imagelist_copy_images( himl, himl->hdcMask, hdcBitmap, 0, nCopyCount, 0 );
2913 SelectObject (hdcBitmap, hbmOld);
2915 /* FIXME: delete 'empty' image space? */
2917 SelectObject (himl->hdcMask, hbmNewBitmap);
2918 DeleteObject (himl->hbmMask);
2919 himl->hbmMask = hbmNewBitmap;
2921 else
2922 ERR("Could not create new mask bitmap!\n");
2925 DeleteDC (hdcBitmap);
2927 himl->item_flags = HeapReAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, himl->item_flags,
2928 nNewCount * sizeof(*himl->item_flags));
2930 /* Update max image count and current image count */
2931 himl->cMaxImage = nNewCount;
2932 himl->cCurImage = iImageCount;
2934 return TRUE;
2938 /*************************************************************************
2939 * ImageList_SetOverlayImage [COMCTL32.@]
2941 * Assigns an overlay mask index to an existing image in an image list.
2943 * PARAMS
2944 * himl [I] handle to image list
2945 * iImage [I] image index
2946 * iOverlay [I] overlay mask index
2948 * RETURNS
2949 * Success: TRUE
2950 * Failure: FALSE
2953 BOOL WINAPI
2954 ImageList_SetOverlayImage (HIMAGELIST himl, INT iImage, INT iOverlay)
2956 if (!is_valid(himl))
2957 return FALSE;
2958 if ((iOverlay < 1) || (iOverlay > MAX_OVERLAYIMAGE))
2959 return FALSE;
2960 if ((iImage!=-1) && ((iImage < 0) || (iImage > himl->cCurImage)))
2961 return FALSE;
2962 himl->nOvlIdx[iOverlay - 1] = iImage;
2963 return TRUE;
2968 /* helper for ImageList_Write - write bitmap to pstm
2969 * currently everything is written as 24 bit RGB, except masks
2971 static BOOL _write_bitmap(HBITMAP hBitmap, IStream *pstm)
2973 LPBITMAPFILEHEADER bmfh;
2974 LPBITMAPINFOHEADER bmih;
2975 LPBYTE data = NULL, lpBits;
2976 BITMAP bm;
2977 INT bitCount, sizeImage, offBits, totalSize;
2978 HDC xdc;
2979 BOOL result = FALSE;
2981 if (!GetObjectW(hBitmap, sizeof(BITMAP), &bm))
2982 return FALSE;
2984 bitCount = bm.bmBitsPixel;
2985 sizeImage = get_dib_stride(bm.bmWidth, bitCount) * bm.bmHeight;
2987 totalSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
2988 if(bitCount <= 8)
2989 totalSize += (1 << bitCount) * sizeof(RGBQUAD);
2990 offBits = totalSize;
2991 totalSize += sizeImage;
2993 data = heap_alloc_zero(totalSize);
2994 bmfh = (LPBITMAPFILEHEADER)data;
2995 bmih = (LPBITMAPINFOHEADER)(data + sizeof(BITMAPFILEHEADER));
2996 lpBits = data + offBits;
2998 /* setup BITMAPFILEHEADER */
2999 bmfh->bfType = (('M' << 8) | 'B');
3000 bmfh->bfSize = offBits;
3001 bmfh->bfReserved1 = 0;
3002 bmfh->bfReserved2 = 0;
3003 bmfh->bfOffBits = offBits;
3005 /* setup BITMAPINFOHEADER */
3006 bmih->biSize = sizeof(BITMAPINFOHEADER);
3007 bmih->biWidth = bm.bmWidth;
3008 bmih->biHeight = bm.bmHeight;
3009 bmih->biPlanes = 1;
3010 bmih->biBitCount = bitCount;
3011 bmih->biCompression = BI_RGB;
3012 bmih->biSizeImage = sizeImage;
3013 bmih->biXPelsPerMeter = 0;
3014 bmih->biYPelsPerMeter = 0;
3015 bmih->biClrUsed = 0;
3016 bmih->biClrImportant = 0;
3018 xdc = GetDC(0);
3019 result = GetDIBits(xdc, hBitmap, 0, bm.bmHeight, lpBits, (BITMAPINFO *)bmih, DIB_RGB_COLORS) == bm.bmHeight;
3020 ReleaseDC(0, xdc);
3021 if (!result)
3022 goto failed;
3024 TRACE("width %u, height %u, planes %u, bpp %u\n",
3025 bmih->biWidth, bmih->biHeight,
3026 bmih->biPlanes, bmih->biBitCount);
3028 if(FAILED(IStream_Write(pstm, data, totalSize, NULL)))
3029 goto failed;
3031 result = TRUE;
3033 failed:
3034 heap_free(data);
3036 return result;
3040 /*************************************************************************
3041 * ImageList_Write [COMCTL32.@]
3043 * Writes an image list to a stream.
3045 * PARAMS
3046 * himl [I] handle to image list
3047 * pstm [O] Pointer to a stream.
3049 * RETURNS
3050 * Success: TRUE
3051 * Failure: FALSE
3053 * BUGS
3054 * probably.
3057 BOOL WINAPI ImageList_Write(HIMAGELIST himl, IStream *pstm)
3059 ILHEAD ilHead;
3060 int i;
3062 TRACE("%p %p\n", himl, pstm);
3064 if (!is_valid(himl))
3065 return FALSE;
3067 ilHead.usMagic = (('L' << 8) | 'I');
3068 ilHead.usVersion = 0x101;
3069 ilHead.cCurImage = himl->cCurImage;
3070 ilHead.cMaxImage = himl->cMaxImage;
3071 ilHead.cGrow = himl->cGrow;
3072 ilHead.cx = himl->cx;
3073 ilHead.cy = himl->cy;
3074 ilHead.bkcolor = himl->clrBk;
3075 ilHead.flags = himl->flags;
3076 for(i = 0; i < 4; i++) {
3077 ilHead.ovls[i] = himl->nOvlIdx[i];
3080 TRACE("cx %u, cy %u, flags 0x04%x, cCurImage %u, cMaxImage %u\n",
3081 ilHead.cx, ilHead.cy, ilHead.flags, ilHead.cCurImage, ilHead.cMaxImage);
3083 if(FAILED(IStream_Write(pstm, &ilHead, sizeof(ILHEAD), NULL)))
3084 return FALSE;
3086 /* write the bitmap */
3087 if(!_write_bitmap(himl->hbmImage, pstm))
3088 return FALSE;
3090 /* write the mask if we have one */
3091 if(himl->flags & ILC_MASK) {
3092 if(!_write_bitmap(himl->hbmMask, pstm))
3093 return FALSE;
3096 return TRUE;
3100 static HBITMAP ImageList_CreateImage(HDC hdc, HIMAGELIST himl, UINT count)
3102 HBITMAP hbmNewBitmap;
3103 UINT ilc = (himl->flags & 0xFE);
3104 SIZE sz;
3106 imagelist_get_bitmap_size( himl, count, &sz );
3108 if ((ilc >= ILC_COLOR4 && ilc <= ILC_COLOR32) || ilc == ILC_COLOR)
3110 char buffer[sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD)];
3111 BITMAPINFO *bmi = (BITMAPINFO *)buffer;
3113 TRACE("Creating DIBSection %d x %d, %d Bits per Pixel\n",
3114 sz.cx, sz.cy, himl->uBitsPixel);
3116 memset( buffer, 0, sizeof(buffer) );
3117 bmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
3118 bmi->bmiHeader.biWidth = sz.cx;
3119 bmi->bmiHeader.biHeight = sz.cy;
3120 bmi->bmiHeader.biPlanes = 1;
3121 bmi->bmiHeader.biBitCount = himl->uBitsPixel;
3122 bmi->bmiHeader.biCompression = BI_RGB;
3124 if (himl->uBitsPixel <= ILC_COLOR8)
3126 if (!himl->color_table_set)
3128 /* retrieve the default color map */
3129 HBITMAP tmp = CreateBitmap( 1, 1, 1, 1, NULL );
3130 GetDIBits( hdc, tmp, 0, 0, NULL, bmi, DIB_RGB_COLORS );
3131 DeleteObject( tmp );
3132 if (ilc == ILC_COLOR4)
3134 RGBQUAD tmp;
3135 tmp = bmi->bmiColors[7];
3136 bmi->bmiColors[7] = bmi->bmiColors[8];
3137 bmi->bmiColors[8] = tmp;
3140 else
3142 GetDIBColorTable(himl->hdcImage, 0, 1 << himl->uBitsPixel, bmi->bmiColors);
3145 hbmNewBitmap = CreateDIBSection(hdc, bmi, DIB_RGB_COLORS, NULL, 0, 0);
3147 else /*if (ilc == ILC_COLORDDB)*/
3149 TRACE("Creating Bitmap: %d Bits per Pixel\n", himl->uBitsPixel);
3151 hbmNewBitmap = CreateBitmap (sz.cx, sz.cy, 1, himl->uBitsPixel, NULL);
3153 TRACE("returning %p\n", hbmNewBitmap);
3154 return hbmNewBitmap;
3157 /*************************************************************************
3158 * ImageList_SetColorTable [COMCTL32.@]
3160 * Sets the color table of an image list.
3162 * PARAMS
3163 * himl [I] Handle to the image list.
3164 * uStartIndex [I] The first index to set.
3165 * cEntries [I] Number of entries to set.
3166 * prgb [I] New color information for color table for the image list.
3168 * RETURNS
3169 * Success: Number of entries in the table that were set.
3170 * Failure: Zero.
3172 * SEE
3173 * ImageList_Create(), SetDIBColorTable()
3176 UINT WINAPI
3177 ImageList_SetColorTable(HIMAGELIST himl, UINT uStartIndex, UINT cEntries, const RGBQUAD *prgb)
3179 TRACE("(%p, %d, %d, %p)\n", himl, uStartIndex, cEntries, prgb);
3180 himl->color_table_set = TRUE;
3181 return SetDIBColorTable(himl->hdcImage, uStartIndex, cEntries, prgb);
3184 /*************************************************************************
3185 * ImageList_CoCreateInstance [COMCTL32.@]
3187 * Creates a new imagelist instance and returns an interface pointer to it.
3189 * PARAMS
3190 * rclsid [I] A reference to the CLSID (CLSID_ImageList).
3191 * punkOuter [I] Pointer to IUnknown interface for aggregation, if desired
3192 * riid [I] Identifier of the requested interface.
3193 * ppv [O] Returns the address of the pointer requested, or NULL.
3195 * RETURNS
3196 * Success: S_OK.
3197 * Failure: Error value.
3199 HRESULT WINAPI
3200 ImageList_CoCreateInstance (REFCLSID rclsid, const IUnknown *punkOuter, REFIID riid, void **ppv)
3202 TRACE("(%s,%p,%s,%p)\n", debugstr_guid(rclsid), punkOuter, debugstr_guid(riid), ppv);
3204 if (!IsEqualCLSID(&CLSID_ImageList, rclsid))
3205 return E_NOINTERFACE;
3207 return ImageListImpl_CreateInstance(punkOuter, riid, ppv);
3211 /*************************************************************************
3212 * IImageList implementation
3215 static HRESULT WINAPI ImageListImpl_QueryInterface(IImageList2 *iface,
3216 REFIID iid, void **ppv)
3218 HIMAGELIST imgl = impl_from_IImageList2(iface);
3220 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
3222 if (!ppv) return E_INVALIDARG;
3224 if (IsEqualIID(&IID_IUnknown, iid) ||
3225 IsEqualIID(&IID_IImageList, iid) ||
3226 IsEqualIID(&IID_IImageList2, iid))
3228 *ppv = &imgl->IImageList2_iface;
3230 else
3232 *ppv = NULL;
3233 return E_NOINTERFACE;
3236 IImageList2_AddRef(iface);
3237 return S_OK;
3240 static ULONG WINAPI ImageListImpl_AddRef(IImageList2 *iface)
3242 HIMAGELIST imgl = impl_from_IImageList2(iface);
3243 ULONG ref = InterlockedIncrement(&imgl->ref);
3245 TRACE("(%p) refcount=%u\n", iface, ref);
3246 return ref;
3249 static ULONG WINAPI ImageListImpl_Release(IImageList2 *iface)
3251 HIMAGELIST This = impl_from_IImageList2(iface);
3252 ULONG ref = InterlockedDecrement(&This->ref);
3254 TRACE("(%p) refcount=%u\n", iface, ref);
3256 if (ref == 0)
3258 /* delete image bitmaps */
3259 if (This->hbmImage) DeleteObject (This->hbmImage);
3260 if (This->hbmMask) DeleteObject (This->hbmMask);
3262 /* delete image & mask DCs */
3263 if (This->hdcImage) DeleteDC (This->hdcImage);
3264 if (This->hdcMask) DeleteDC (This->hdcMask);
3266 /* delete blending brushes */
3267 if (This->hbrBlend25) DeleteObject (This->hbrBlend25);
3268 if (This->hbrBlend50) DeleteObject (This->hbrBlend50);
3270 This->IImageList2_iface.lpVtbl = NULL;
3271 heap_free(This->item_flags);
3272 heap_free(This);
3275 return ref;
3278 static HRESULT WINAPI ImageListImpl_Add(IImageList2 *iface, HBITMAP hbmImage,
3279 HBITMAP hbmMask, int *pi)
3281 HIMAGELIST imgl = impl_from_IImageList2(iface);
3282 int ret;
3284 if (!pi)
3285 return E_FAIL;
3287 ret = ImageList_Add(imgl, hbmImage, hbmMask);
3289 if (ret == -1)
3290 return E_FAIL;
3292 *pi = ret;
3293 return S_OK;
3296 static HRESULT WINAPI ImageListImpl_ReplaceIcon(IImageList2 *iface, int i,
3297 HICON hicon, int *pi)
3299 HIMAGELIST imgl = impl_from_IImageList2(iface);
3300 int ret;
3302 if (!pi)
3303 return E_FAIL;
3305 ret = ImageList_ReplaceIcon(imgl, i, hicon);
3307 if (ret == -1)
3308 return E_FAIL;
3310 *pi = ret;
3311 return S_OK;
3314 static HRESULT WINAPI ImageListImpl_SetOverlayImage(IImageList2 *iface,
3315 int iImage, int iOverlay)
3317 HIMAGELIST imgl = impl_from_IImageList2(iface);
3318 return ImageList_SetOverlayImage(imgl, iImage, iOverlay) ? S_OK : E_FAIL;
3321 static HRESULT WINAPI ImageListImpl_Replace(IImageList2 *iface, int i,
3322 HBITMAP hbmImage, HBITMAP hbmMask)
3324 HIMAGELIST imgl = impl_from_IImageList2(iface);
3325 return ImageList_Replace(imgl, i, hbmImage, hbmMask) ? S_OK : E_FAIL;
3328 static HRESULT WINAPI ImageListImpl_AddMasked(IImageList2 *iface, HBITMAP hbmImage,
3329 COLORREF crMask, int *pi)
3331 HIMAGELIST imgl = impl_from_IImageList2(iface);
3332 int ret;
3334 if (!pi)
3335 return E_FAIL;
3337 ret = ImageList_AddMasked(imgl, hbmImage, crMask);
3339 if (ret == -1)
3340 return E_FAIL;
3342 *pi = ret;
3343 return S_OK;
3346 static HRESULT WINAPI ImageListImpl_Draw(IImageList2 *iface,
3347 IMAGELISTDRAWPARAMS *pimldp)
3349 HIMAGELIST imgl = impl_from_IImageList2(iface);
3350 HIMAGELIST old_himl;
3351 int ret;
3353 /* As far as I can tell, Windows simply ignores the contents of pimldp->himl
3354 so we shall simulate the same */
3355 old_himl = pimldp->himl;
3356 pimldp->himl = imgl;
3358 ret = ImageList_DrawIndirect(pimldp);
3360 pimldp->himl = old_himl;
3361 return ret ? S_OK : E_INVALIDARG;
3364 static HRESULT WINAPI ImageListImpl_Remove(IImageList2 *iface, int i)
3366 HIMAGELIST imgl = impl_from_IImageList2(iface);
3367 return (ImageList_Remove(imgl, i) == 0) ? E_INVALIDARG : S_OK;
3370 static HRESULT WINAPI ImageListImpl_GetIcon(IImageList2 *iface, int i, UINT flags,
3371 HICON *picon)
3373 HIMAGELIST imgl = impl_from_IImageList2(iface);
3374 HICON hIcon;
3376 if (!picon)
3377 return E_FAIL;
3379 hIcon = ImageList_GetIcon(imgl, i, flags);
3381 if (hIcon == NULL)
3382 return E_FAIL;
3384 *picon = hIcon;
3385 return S_OK;
3388 static HRESULT WINAPI ImageListImpl_GetImageInfo(IImageList2 *iface, int i,
3389 IMAGEINFO *pImageInfo)
3391 HIMAGELIST imgl = impl_from_IImageList2(iface);
3392 return ImageList_GetImageInfo(imgl, i, pImageInfo) ? S_OK : E_FAIL;
3395 static HRESULT WINAPI ImageListImpl_Copy(IImageList2 *iface, int dst_index,
3396 IUnknown *unk_src, int src_index, UINT flags)
3398 HIMAGELIST imgl = impl_from_IImageList2(iface);
3399 IImageList *src = NULL;
3400 HRESULT ret;
3402 if (!unk_src)
3403 return E_FAIL;
3405 /* TODO: Add test for IID_ImageList2 too */
3406 if (FAILED(IUnknown_QueryInterface(unk_src, &IID_IImageList,
3407 (void **) &src)))
3408 return E_FAIL;
3410 if (ImageList_Copy(imgl, dst_index, (HIMAGELIST) src, src_index, flags))
3411 ret = S_OK;
3412 else
3413 ret = E_FAIL;
3415 IImageList_Release(src);
3416 return ret;
3419 static HRESULT WINAPI ImageListImpl_Merge(IImageList2 *iface, int i1,
3420 IUnknown *punk2, int i2, int dx, int dy, REFIID riid, void **ppv)
3422 HIMAGELIST imgl = impl_from_IImageList2(iface);
3423 IImageList *iml2 = NULL;
3424 HIMAGELIST merged;
3425 HRESULT ret = E_FAIL;
3427 TRACE("(%p)->(%d %p %d %d %d %s %p)\n", iface, i1, punk2, i2, dx, dy, debugstr_guid(riid), ppv);
3429 /* TODO: Add test for IID_ImageList2 too */
3430 if (FAILED(IUnknown_QueryInterface(punk2, &IID_IImageList,
3431 (void **) &iml2)))
3432 return E_FAIL;
3434 merged = ImageList_Merge(imgl, i1, (HIMAGELIST) iml2, i2, dx, dy);
3436 /* Get the interface for the new image list */
3437 if (merged)
3439 ret = HIMAGELIST_QueryInterface(merged, riid, ppv);
3440 ImageList_Destroy(merged);
3443 IImageList_Release(iml2);
3444 return ret;
3447 static HRESULT WINAPI ImageListImpl_Clone(IImageList2 *iface, REFIID riid, void **ppv)
3449 HIMAGELIST imgl = impl_from_IImageList2(iface);
3450 HIMAGELIST clone;
3451 HRESULT ret = E_FAIL;
3453 TRACE("(%p)->(%s %p)\n", iface, debugstr_guid(riid), ppv);
3455 clone = ImageList_Duplicate(imgl);
3457 /* Get the interface for the new image list */
3458 if (clone)
3460 ret = HIMAGELIST_QueryInterface(clone, riid, ppv);
3461 ImageList_Destroy(clone);
3464 return ret;
3467 static HRESULT WINAPI ImageListImpl_GetImageRect(IImageList2 *iface, int i,
3468 RECT *prc)
3470 HIMAGELIST imgl = impl_from_IImageList2(iface);
3471 IMAGEINFO info;
3473 if (!prc)
3474 return E_FAIL;
3476 if (!ImageList_GetImageInfo(imgl, i, &info))
3477 return E_FAIL;
3479 *prc = info.rcImage;
3481 return S_OK;
3484 static HRESULT WINAPI ImageListImpl_GetIconSize(IImageList2 *iface, int *cx,
3485 int *cy)
3487 HIMAGELIST imgl = impl_from_IImageList2(iface);
3488 return ImageList_GetIconSize(imgl, cx, cy) ? S_OK : E_INVALIDARG;
3491 static HRESULT WINAPI ImageListImpl_SetIconSize(IImageList2 *iface, int cx,
3492 int cy)
3494 HIMAGELIST imgl = impl_from_IImageList2(iface);
3495 return ImageList_SetIconSize(imgl, cx, cy) ? S_OK : E_FAIL;
3498 static HRESULT WINAPI ImageListImpl_GetImageCount(IImageList2 *iface, int *pi)
3500 HIMAGELIST imgl = impl_from_IImageList2(iface);
3501 *pi = ImageList_GetImageCount(imgl);
3502 return S_OK;
3505 static HRESULT WINAPI ImageListImpl_SetImageCount(IImageList2 *iface, UINT count)
3507 HIMAGELIST imgl = impl_from_IImageList2(iface);
3508 return ImageList_SetImageCount(imgl, count) ? S_OK : E_FAIL;
3511 static HRESULT WINAPI ImageListImpl_SetBkColor(IImageList2 *iface, COLORREF clrBk,
3512 COLORREF *pclr)
3514 HIMAGELIST imgl = impl_from_IImageList2(iface);
3515 *pclr = ImageList_SetBkColor(imgl, clrBk);
3516 return S_OK;
3519 static HRESULT WINAPI ImageListImpl_GetBkColor(IImageList2 *iface, COLORREF *pclr)
3521 HIMAGELIST imgl = impl_from_IImageList2(iface);
3522 *pclr = ImageList_GetBkColor(imgl);
3523 return S_OK;
3526 static HRESULT WINAPI ImageListImpl_BeginDrag(IImageList2 *iface, int iTrack,
3527 int dxHotspot, int dyHotspot)
3529 HIMAGELIST imgl = impl_from_IImageList2(iface);
3530 return ImageList_BeginDrag(imgl, iTrack, dxHotspot, dyHotspot) ? S_OK : E_FAIL;
3533 static HRESULT WINAPI ImageListImpl_EndDrag(IImageList2 *iface)
3535 ImageList_EndDrag();
3536 return S_OK;
3539 static HRESULT WINAPI ImageListImpl_DragEnter(IImageList2 *iface, HWND hwndLock,
3540 int x, int y)
3542 return ImageList_DragEnter(hwndLock, x, y) ? S_OK : E_FAIL;
3545 static HRESULT WINAPI ImageListImpl_DragLeave(IImageList2 *iface, HWND hwndLock)
3547 return ImageList_DragLeave(hwndLock) ? S_OK : E_FAIL;
3550 static HRESULT WINAPI ImageListImpl_DragMove(IImageList2 *iface, int x, int y)
3552 return ImageList_DragMove(x, y) ? S_OK : E_FAIL;
3555 static HRESULT WINAPI ImageListImpl_SetDragCursorImage(IImageList2 *iface,
3556 IUnknown *punk, int iDrag, int dxHotspot, int dyHotspot)
3558 IImageList *iml2 = NULL;
3559 BOOL ret;
3561 if (!punk)
3562 return E_FAIL;
3564 /* TODO: Add test for IID_ImageList2 too */
3565 if (FAILED(IUnknown_QueryInterface(punk, &IID_IImageList,
3566 (void **) &iml2)))
3567 return E_FAIL;
3569 ret = ImageList_SetDragCursorImage((HIMAGELIST) iml2, iDrag, dxHotspot,
3570 dyHotspot);
3572 IImageList_Release(iml2);
3574 return ret ? S_OK : E_FAIL;
3577 static HRESULT WINAPI ImageListImpl_DragShowNolock(IImageList2 *iface, BOOL fShow)
3579 return ImageList_DragShowNolock(fShow) ? S_OK : E_FAIL;
3582 static HRESULT WINAPI ImageListImpl_GetDragImage(IImageList2 *iface, POINT *ppt,
3583 POINT *pptHotspot, REFIID riid, PVOID *ppv)
3585 HRESULT ret = E_FAIL;
3586 HIMAGELIST hNew;
3588 if (!ppv)
3589 return E_FAIL;
3591 hNew = ImageList_GetDragImage(ppt, pptHotspot);
3593 /* Get the interface for the new image list */
3594 if (hNew)
3596 IImageList *idrag = (IImageList*)hNew;
3598 ret = HIMAGELIST_QueryInterface(hNew, riid, ppv);
3599 IImageList_Release(idrag);
3602 return ret;
3605 static HRESULT WINAPI ImageListImpl_GetItemFlags(IImageList2 *iface, int i, DWORD *flags)
3607 HIMAGELIST This = impl_from_IImageList2(iface);
3609 if (i < 0 || i >= This->cCurImage)
3610 return E_INVALIDARG;
3612 *flags = This->item_flags[i];
3614 return S_OK;
3617 static HRESULT WINAPI ImageListImpl_GetOverlayImage(IImageList2 *iface, int iOverlay,
3618 int *piIndex)
3620 HIMAGELIST This = impl_from_IImageList2(iface);
3621 int i;
3623 if ((iOverlay < 0) || (iOverlay > This->cCurImage))
3624 return E_FAIL;
3626 for (i = 0; i < MAX_OVERLAYIMAGE; i++)
3628 if (This->nOvlIdx[i] == iOverlay)
3630 *piIndex = i + 1;
3631 return S_OK;
3635 return E_FAIL;
3638 static HRESULT WINAPI ImageListImpl_Resize(IImageList2 *iface, INT cx, INT cy)
3640 FIXME("(%p)->(%d %d): stub\n", iface, cx, cy);
3641 return E_NOTIMPL;
3644 static HRESULT WINAPI ImageListImpl_GetOriginalSize(IImageList2 *iface, INT image, DWORD flags, INT *cx, INT *cy)
3646 FIXME("(%p)->(%d %x %p %p): stub\n", iface, image, flags, cx, cy);
3647 return E_NOTIMPL;
3650 static HRESULT WINAPI ImageListImpl_SetOriginalSize(IImageList2 *iface, INT image, INT cx, INT cy)
3652 FIXME("(%p)->(%d %d %d): stub\n", iface, image, cx, cy);
3653 return E_NOTIMPL;
3656 static HRESULT WINAPI ImageListImpl_SetCallback(IImageList2 *iface, IUnknown *callback)
3658 FIXME("(%p)->(%p): stub\n", iface, callback);
3659 return E_NOTIMPL;
3662 static HRESULT WINAPI ImageListImpl_GetCallback(IImageList2 *iface, REFIID riid, void **ppv)
3664 FIXME("(%p)->(%s %p): stub\n", iface, debugstr_guid(riid), ppv);
3665 return E_NOTIMPL;
3668 static HRESULT WINAPI ImageListImpl_ForceImagePresent(IImageList2 *iface, INT image, DWORD flags)
3670 FIXME("(%p)->(%d %x): stub\n", iface, image, flags);
3671 return E_NOTIMPL;
3674 static HRESULT WINAPI ImageListImpl_DiscardImages(IImageList2 *iface, INT first_image, INT last_image, DWORD flags)
3676 FIXME("(%p)->(%d %d %x): stub\n", iface, first_image, last_image, flags);
3677 return E_NOTIMPL;
3680 static HRESULT WINAPI ImageListImpl_PreloadImages(IImageList2 *iface, IMAGELISTDRAWPARAMS *params)
3682 FIXME("(%p)->(%p): stub\n", iface, params);
3683 return E_NOTIMPL;
3686 static HRESULT WINAPI ImageListImpl_GetStatistics(IImageList2 *iface, IMAGELISTSTATS *stats)
3688 FIXME("(%p)->(%p): stub\n", iface, stats);
3689 return E_NOTIMPL;
3692 static HRESULT WINAPI ImageListImpl_Initialize(IImageList2 *iface, INT cx, INT cy, UINT flags, INT initial, INT grow)
3694 FIXME("(%p)->(%d %d %d %d %d): stub\n", iface, cx, cy, flags, initial, grow);
3695 return E_NOTIMPL;
3698 static HRESULT WINAPI ImageListImpl_Replace2(IImageList2 *iface, INT i, HBITMAP image, HBITMAP mask, IUnknown *unk, DWORD flags)
3700 FIXME("(%p)->(%d %p %p %p %x): stub\n", iface, i, image, mask, unk, flags);
3701 return E_NOTIMPL;
3704 static HRESULT WINAPI ImageListImpl_ReplaceFromImageList(IImageList2 *iface, INT i, IImageList *imagelist, INT src,
3705 IUnknown *unk, DWORD flags)
3707 FIXME("(%p)->(%d %p %d %p %x): stub\n", iface, i, imagelist, src, unk, flags);
3708 return E_NOTIMPL;
3711 static const IImageList2Vtbl ImageListImpl_Vtbl = {
3712 ImageListImpl_QueryInterface,
3713 ImageListImpl_AddRef,
3714 ImageListImpl_Release,
3715 ImageListImpl_Add,
3716 ImageListImpl_ReplaceIcon,
3717 ImageListImpl_SetOverlayImage,
3718 ImageListImpl_Replace,
3719 ImageListImpl_AddMasked,
3720 ImageListImpl_Draw,
3721 ImageListImpl_Remove,
3722 ImageListImpl_GetIcon,
3723 ImageListImpl_GetImageInfo,
3724 ImageListImpl_Copy,
3725 ImageListImpl_Merge,
3726 ImageListImpl_Clone,
3727 ImageListImpl_GetImageRect,
3728 ImageListImpl_GetIconSize,
3729 ImageListImpl_SetIconSize,
3730 ImageListImpl_GetImageCount,
3731 ImageListImpl_SetImageCount,
3732 ImageListImpl_SetBkColor,
3733 ImageListImpl_GetBkColor,
3734 ImageListImpl_BeginDrag,
3735 ImageListImpl_EndDrag,
3736 ImageListImpl_DragEnter,
3737 ImageListImpl_DragLeave,
3738 ImageListImpl_DragMove,
3739 ImageListImpl_SetDragCursorImage,
3740 ImageListImpl_DragShowNolock,
3741 ImageListImpl_GetDragImage,
3742 ImageListImpl_GetItemFlags,
3743 ImageListImpl_GetOverlayImage,
3744 ImageListImpl_Resize,
3745 ImageListImpl_GetOriginalSize,
3746 ImageListImpl_SetOriginalSize,
3747 ImageListImpl_SetCallback,
3748 ImageListImpl_GetCallback,
3749 ImageListImpl_ForceImagePresent,
3750 ImageListImpl_DiscardImages,
3751 ImageListImpl_PreloadImages,
3752 ImageListImpl_GetStatistics,
3753 ImageListImpl_Initialize,
3754 ImageListImpl_Replace2,
3755 ImageListImpl_ReplaceFromImageList
3758 static BOOL is_valid(HIMAGELIST himl)
3760 BOOL valid;
3761 __TRY
3763 valid = himl && himl->IImageList2_iface.lpVtbl == &ImageListImpl_Vtbl;
3765 __EXCEPT_PAGE_FAULT
3767 valid = FALSE;
3769 __ENDTRY
3770 return valid;
3773 /*************************************************************************
3774 * HIMAGELIST_QueryInterface [COMCTL32.@]
3776 * Returns a pointer to an IImageList or IImageList2 object for the given
3777 * HIMAGELIST.
3779 * PARAMS
3780 * himl [I] Image list handle.
3781 * riid [I] Identifier of the requested interface.
3782 * ppv [O] Returns the address of the pointer requested, or NULL.
3784 * RETURNS
3785 * Success: S_OK.
3786 * Failure: Error value.
3788 HRESULT WINAPI
3789 HIMAGELIST_QueryInterface (HIMAGELIST himl, REFIID riid, void **ppv)
3791 TRACE("(%p,%s,%p)\n", himl, debugstr_guid(riid), ppv);
3792 return IImageList2_QueryInterface((IImageList2 *) himl, riid, ppv);
3795 static HRESULT ImageListImpl_CreateInstance(const IUnknown *pUnkOuter, REFIID iid, void** ppv)
3797 HIMAGELIST This;
3798 HRESULT ret;
3800 TRACE("(%p,%s,%p)\n", pUnkOuter, debugstr_guid(iid), ppv);
3802 *ppv = NULL;
3804 if (pUnkOuter) return CLASS_E_NOAGGREGATION;
3806 This = heap_alloc_zero(sizeof(struct _IMAGELIST));
3807 if (!This) return E_OUTOFMEMORY;
3809 This->IImageList2_iface.lpVtbl = &ImageListImpl_Vtbl;
3810 This->ref = 1;
3812 ret = IImageList2_QueryInterface(&This->IImageList2_iface, iid, ppv);
3813 IImageList2_Release(&This->IImageList2_iface);
3815 return ret;