gdiplus: In GdipImageSelectActiveFrame rely on codec->select_func() to fail.
[wine.git] / dlls / comctl32 / imagelist.c
blob85bb2fe0026f15db78ef0ad0ceeba382a9aea2fe
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_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"
49 WINE_DEFAULT_DEBUG_CHANNEL(imagelist);
51 #define MAX_OVERLAYIMAGE 15
53 struct _IMAGELIST
55 IImageList2 IImageList2_iface; /* 00: IImageList vtable */
56 INT cCurImage; /* 04: ImageCount */
57 INT cMaxImage; /* 08: maximages */
58 INT cGrow; /* 0C: cGrow */
59 INT cx; /* 10: cx */
60 INT cy; /* 14: cy */
61 DWORD x4;
62 UINT flags; /* 1C: flags */
63 COLORREF clrFg; /* 20: foreground color */
64 COLORREF clrBk; /* 24: background color */
67 HBITMAP hbmImage; /* 28: images Bitmap */
68 HBITMAP hbmMask; /* 2C: masks Bitmap */
69 HDC hdcImage; /* 30: images MemDC */
70 HDC hdcMask; /* 34: masks MemDC */
71 INT nOvlIdx[MAX_OVERLAYIMAGE]; /* 38: overlay images index */
73 /* not yet found out */
74 HBRUSH hbrBlend25;
75 HBRUSH hbrBlend50;
76 INT cInitial;
77 UINT uBitsPixel;
78 DWORD *item_flags;
79 BOOL color_table_set;
81 LONG ref; /* reference count */
84 #define IMAGELIST_MAGIC 0x53414D58
86 /* Header used by ImageList_Read() and ImageList_Write() */
87 #include "pshpack2.h"
88 typedef struct _ILHEAD
90 USHORT usMagic;
91 USHORT usVersion;
92 WORD cCurImage;
93 WORD cMaxImage;
94 WORD cGrow;
95 WORD cx;
96 WORD cy;
97 COLORREF bkcolor;
98 WORD flags;
99 SHORT ovls[4];
100 } ILHEAD;
101 #include "poppack.h"
103 /* internal image list data used for Drag & Drop operations */
104 typedef struct
106 HWND hwnd;
107 HIMAGELIST himl;
108 HIMAGELIST himlNoCursor;
109 /* position of the drag image relative to the window */
110 INT x;
111 INT y;
112 /* offset of the hotspot relative to the origin of the image */
113 INT dxHotspot;
114 INT dyHotspot;
115 /* is the drag image visible */
116 BOOL bShow;
117 /* saved background */
118 HBITMAP hbmBg;
119 } INTERNALDRAG;
121 static INTERNALDRAG InternalDrag = { 0, 0, 0, 0, 0, 0, 0, FALSE, 0 };
123 static inline HIMAGELIST impl_from_IImageList2(IImageList2 *iface)
125 return CONTAINING_RECORD(iface, struct _IMAGELIST, IImageList2_iface);
128 static HBITMAP ImageList_CreateImage(HDC hdc, HIMAGELIST himl, UINT count);
129 static HRESULT ImageListImpl_CreateInstance(const IUnknown *pUnkOuter, REFIID iid, void** ppv);
130 static BOOL is_valid(HIMAGELIST himl);
133 * An imagelist with N images is tiled like this:
135 * N/4 ->
137 * 4 048C..
138 * 159D..
139 * | 26AE.N
140 * V 37BF.
143 #define TILE_COUNT 4
145 BOOL imagelist_has_alpha( HIMAGELIST himl, UINT index )
147 return himl->item_flags[index] & ILIF_ALPHA;
150 static inline UINT imagelist_height( UINT count )
152 return ((count + TILE_COUNT - 1)/TILE_COUNT);
155 static inline void imagelist_point_from_index( HIMAGELIST himl, UINT index, LPPOINT pt )
157 pt->x = (index%TILE_COUNT) * himl->cx;
158 pt->y = (index/TILE_COUNT) * himl->cy;
161 static inline void imagelist_get_bitmap_size( HIMAGELIST himl, UINT count, SIZE *sz )
163 sz->cx = himl->cx * TILE_COUNT;
164 sz->cy = imagelist_height( count ) * himl->cy;
167 static inline int get_dib_stride( int width, int bpp )
169 return ((width * bpp + 31) >> 3) & ~3;
172 static inline int get_dib_image_size( const BITMAPINFO *info )
174 return get_dib_stride( info->bmiHeader.biWidth, info->bmiHeader.biBitCount )
175 * abs( info->bmiHeader.biHeight );
179 * imagelist_copy_images()
181 * Copies a block of count images from offset src in the list to offset dest.
182 * Images are copied a row at at time. Assumes hdcSrc and hdcDest are different.
184 static inline void imagelist_copy_images( HIMAGELIST himl, HDC hdcSrc, HDC hdcDest,
185 UINT src, UINT count, UINT dest )
187 POINT ptSrc, ptDest;
188 SIZE sz;
189 UINT i;
191 for ( i=0; i<TILE_COUNT; i++ )
193 imagelist_point_from_index( himl, src+i, &ptSrc );
194 imagelist_point_from_index( himl, dest+i, &ptDest );
195 sz.cx = himl->cx;
196 sz.cy = himl->cy * imagelist_height( count - i );
198 BitBlt( hdcDest, ptDest.x, ptDest.y, sz.cx, sz.cy,
199 hdcSrc, ptSrc.x, ptSrc.y, SRCCOPY );
203 static void add_dib_bits( HIMAGELIST himl, int pos, int count, int width, int height,
204 BITMAPINFO *info, BITMAPINFO *mask_info, DWORD *bits, BYTE *mask_bits )
206 int i, j, n;
207 POINT pt;
208 int stride = info->bmiHeader.biWidth;
209 int mask_stride = (info->bmiHeader.biWidth + 31) / 32 * 4;
211 for (n = 0; n < count; n++)
213 BOOL has_alpha = FALSE;
215 imagelist_point_from_index( himl, pos + n, &pt );
217 /* check if bitmap has an alpha channel */
218 for (i = 0; i < height && !has_alpha; i++)
219 for (j = n * width; j < (n + 1) * width; j++)
220 if ((has_alpha = ((bits[i * stride + j] & 0xff000000) != 0))) break;
222 if (has_alpha)
224 himl->item_flags[pos + n] = ILIF_ALPHA;
226 if (mask_info && himl->hbmMask) /* generate the mask from the alpha channel */
228 for (i = 0; i < height; i++)
229 for (j = n * width; j < (n + 1) * width; j++)
230 if ((bits[i * stride + j] >> 24) > 25) /* more than 10% alpha */
231 mask_bits[i * mask_stride + j / 8] &= ~(0x80 >> (j % 8));
232 else
233 mask_bits[i * mask_stride + j / 8] |= 0x80 >> (j % 8);
236 else if (mask_info) /* mask out the background */
238 for (i = 0; i < height; i++)
239 for (j = n * width; j < (n + 1) * width; j++)
240 if ((mask_bits[i * mask_stride + j / 8] << (j % 8)) & 0x80)
241 bits[i * stride + j] = 0;
243 StretchDIBits( himl->hdcImage, pt.x, pt.y, himl->cx, himl->cy,
244 n * width, 0, width, height, bits, info, DIB_RGB_COLORS, SRCCOPY );
245 if (mask_info)
246 StretchDIBits( himl->hdcMask, pt.x, pt.y, himl->cx, himl->cy,
247 n * width, 0, width, height, mask_bits, mask_info, DIB_RGB_COLORS, SRCCOPY );
251 /* add images with an alpha channel when the image list is 32 bpp */
252 static BOOL add_with_alpha( HIMAGELIST himl, HDC hdc, int pos, int count,
253 int width, int height, HBITMAP hbmImage, HBITMAP hbmMask )
255 BOOL ret = FALSE;
256 BITMAP bm;
257 BITMAPINFO *info, *mask_info = NULL;
258 DWORD *bits = NULL;
259 BYTE *mask_bits = NULL;
260 DWORD mask_width;
262 if (!GetObjectW( hbmImage, sizeof(bm), &bm )) return FALSE;
264 /* if either the imagelist or the source bitmap don't have an alpha channel, bail out now */
265 if ((himl->flags & 0xfe) != ILC_COLOR32) return FALSE;
266 if (bm.bmBitsPixel != 32) return FALSE;
268 SelectObject( hdc, hbmImage );
269 mask_width = (bm.bmWidth + 31) / 32 * 4;
271 if (!(info = Alloc( FIELD_OFFSET( BITMAPINFO, bmiColors[256] )))) goto done;
272 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
273 info->bmiHeader.biWidth = bm.bmWidth;
274 info->bmiHeader.biHeight = -height;
275 info->bmiHeader.biPlanes = 1;
276 info->bmiHeader.biBitCount = 32;
277 info->bmiHeader.biCompression = BI_RGB;
278 info->bmiHeader.biSizeImage = bm.bmWidth * height * 4;
279 info->bmiHeader.biXPelsPerMeter = 0;
280 info->bmiHeader.biYPelsPerMeter = 0;
281 info->bmiHeader.biClrUsed = 0;
282 info->bmiHeader.biClrImportant = 0;
283 if (!(bits = Alloc( info->bmiHeader.biSizeImage ))) goto done;
284 if (!GetDIBits( hdc, hbmImage, 0, height, bits, info, DIB_RGB_COLORS )) goto done;
286 if (hbmMask)
288 if (!(mask_info = Alloc( FIELD_OFFSET( BITMAPINFO, bmiColors[2] ))))
289 goto done;
290 mask_info->bmiHeader = info->bmiHeader;
291 mask_info->bmiHeader.biBitCount = 1;
292 mask_info->bmiHeader.biSizeImage = mask_width * height;
293 if (!(mask_bits = Alloc( mask_info->bmiHeader.biSizeImage )))
294 goto done;
295 if (!GetDIBits( hdc, hbmMask, 0, height, mask_bits, mask_info, DIB_RGB_COLORS )) goto done;
298 add_dib_bits( himl, pos, count, width, height, info, mask_info, bits, mask_bits );
299 ret = TRUE;
301 done:
302 Free( info );
303 Free( mask_info );
304 Free( bits );
305 Free( mask_bits );
306 return ret;
309 UINT WINAPI
310 ImageList_SetColorTable(HIMAGELIST himl, UINT uStartIndex, UINT cEntries, const RGBQUAD *prgb);
312 /*************************************************************************
313 * IMAGELIST_InternalExpandBitmaps [Internal]
315 * Expands the bitmaps of an image list by the given number of images.
317 * PARAMS
318 * himl [I] handle to image list
319 * nImageCount [I] number of images to add
321 * RETURNS
322 * nothing
324 * NOTES
325 * This function CANNOT be used to reduce the number of images.
327 static void
328 IMAGELIST_InternalExpandBitmaps(HIMAGELIST himl, INT nImageCount)
330 HDC hdcBitmap;
331 HBITMAP hbmNewBitmap, hbmNull;
332 INT nNewCount;
333 SIZE sz;
335 TRACE("%p has allocated %d, max %d, grow %d images\n", himl, himl->cCurImage, himl->cMaxImage, himl->cGrow);
337 if (himl->cCurImage + nImageCount < himl->cMaxImage)
338 return;
340 nNewCount = himl->cMaxImage + max(nImageCount, himl->cGrow) + 1;
342 imagelist_get_bitmap_size(himl, nNewCount, &sz);
344 TRACE("Create expanded bitmaps : himl=%p x=%ld y=%ld count=%d\n", himl, sz.cx, sz.cy, nNewCount);
345 hdcBitmap = CreateCompatibleDC (0);
347 hbmNewBitmap = ImageList_CreateImage(hdcBitmap, himl, nNewCount);
349 if (hbmNewBitmap == 0)
350 ERR("creating new image bitmap (x=%ld y=%ld)!\n", sz.cx, sz.cy);
352 if (himl->cCurImage)
354 hbmNull = SelectObject (hdcBitmap, hbmNewBitmap);
355 BitBlt (hdcBitmap, 0, 0, sz.cx, sz.cy,
356 himl->hdcImage, 0, 0, SRCCOPY);
357 SelectObject (hdcBitmap, hbmNull);
359 SelectObject (himl->hdcImage, hbmNewBitmap);
360 DeleteObject (himl->hbmImage);
361 himl->hbmImage = hbmNewBitmap;
363 if (himl->flags & ILC_MASK)
365 hbmNewBitmap = CreateBitmap (sz.cx, sz.cy, 1, 1, NULL);
367 if (hbmNewBitmap == 0)
368 ERR("creating new mask bitmap!\n");
370 if(himl->cCurImage)
372 hbmNull = SelectObject (hdcBitmap, hbmNewBitmap);
373 BitBlt (hdcBitmap, 0, 0, sz.cx, sz.cy,
374 himl->hdcMask, 0, 0, SRCCOPY);
375 SelectObject (hdcBitmap, hbmNull);
377 SelectObject (himl->hdcMask, hbmNewBitmap);
378 DeleteObject (himl->hbmMask);
379 himl->hbmMask = hbmNewBitmap;
382 himl->item_flags = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, himl->item_flags,
383 nNewCount * sizeof(*himl->item_flags));
384 himl->cMaxImage = nNewCount;
386 DeleteDC (hdcBitmap);
390 /*************************************************************************
391 * ImageList_Add [COMCTL32.@]
393 * Add an image or images to an image list.
395 * PARAMS
396 * himl [I] handle to image list
397 * hbmImage [I] handle to image bitmap
398 * hbmMask [I] handle to mask bitmap
400 * RETURNS
401 * Success: Index of the first new image.
402 * Failure: -1
405 INT WINAPI
406 ImageList_Add (HIMAGELIST himl, HBITMAP hbmImage, HBITMAP hbmMask)
408 HDC hdcBitmap, hdcTemp = 0;
409 INT nFirstIndex, nImageCount, i;
410 BITMAP bmp;
411 POINT pt;
413 TRACE("himl=%p hbmimage=%p hbmmask=%p\n", himl, hbmImage, hbmMask);
414 if (!is_valid(himl))
415 return -1;
417 if (!GetObjectW(hbmImage, sizeof(BITMAP), &bmp))
418 return -1;
420 TRACE("himl %p, cCurImage %d, cMaxImage %d, cGrow %d, cx %d, cy %d\n",
421 himl, himl->cCurImage, himl->cMaxImage, himl->cGrow, himl->cx, himl->cy);
423 nImageCount = bmp.bmWidth / himl->cx;
425 TRACE("%p has %d images (%d x %d) bpp %d\n", hbmImage, nImageCount, bmp.bmWidth, bmp.bmHeight,
426 bmp.bmBitsPixel);
428 IMAGELIST_InternalExpandBitmaps(himl, nImageCount);
430 hdcBitmap = CreateCompatibleDC(0);
432 SelectObject(hdcBitmap, hbmImage);
434 if (add_with_alpha( himl, hdcBitmap, himl->cCurImage, nImageCount,
435 himl->cx, min( himl->cy, bmp.bmHeight), hbmImage, hbmMask ))
436 goto done;
438 if (himl->hbmMask)
440 hdcTemp = CreateCompatibleDC(0);
441 SelectObject(hdcTemp, hbmMask);
444 if (himl->uBitsPixel <= 8 && bmp.bmBitsPixel <= 8 &&
445 !himl->color_table_set && himl->cCurImage == 0)
447 RGBQUAD colors[256];
448 UINT num = GetDIBColorTable( hdcBitmap, 0, 1 << bmp.bmBitsPixel, colors );
449 if (num) ImageList_SetColorTable( himl, 0, num, colors );
452 for (i=0; i<nImageCount; i++)
454 imagelist_point_from_index( himl, himl->cCurImage + i, &pt );
456 /* Copy result to the imagelist
458 BitBlt( himl->hdcImage, pt.x, pt.y, himl->cx, bmp.bmHeight,
459 hdcBitmap, i*himl->cx, 0, SRCCOPY );
461 if (!himl->hbmMask)
462 continue;
464 BitBlt( himl->hdcMask, pt.x, pt.y, himl->cx, bmp.bmHeight,
465 hdcTemp, i*himl->cx, 0, SRCCOPY );
467 /* Remove the background from the image
469 BitBlt( himl->hdcImage, pt.x, pt.y, himl->cx, bmp.bmHeight,
470 himl->hdcMask, pt.x, pt.y, 0x220326 ); /* NOTSRCAND */
472 if (hdcTemp) DeleteDC(hdcTemp);
474 done:
475 DeleteDC(hdcBitmap);
477 nFirstIndex = himl->cCurImage;
478 himl->cCurImage += nImageCount;
480 return nFirstIndex;
484 /*************************************************************************
485 * ImageList_AddIcon [COMCTL32.@]
487 * Adds an icon to an image list.
489 * PARAMS
490 * himl [I] handle to image list
491 * hIcon [I] handle to icon
493 * RETURNS
494 * Success: index of the new image
495 * Failure: -1
497 #undef ImageList_AddIcon
498 INT WINAPI ImageList_AddIcon (HIMAGELIST himl, HICON hIcon)
500 return ImageList_ReplaceIcon (himl, -1, hIcon);
504 /*************************************************************************
505 * ImageList_AddMasked [COMCTL32.@]
507 * Adds an image or images to an image list and creates a mask from the
508 * specified bitmap using the mask color.
510 * PARAMS
511 * himl [I] handle to image list.
512 * hBitmap [I] handle to bitmap
513 * clrMask [I] mask color.
515 * RETURNS
516 * Success: Index of the first new image.
517 * Failure: -1
520 INT WINAPI
521 ImageList_AddMasked (HIMAGELIST himl, HBITMAP hBitmap, COLORREF clrMask)
523 HDC hdcMask, hdcBitmap;
524 INT ret;
525 BITMAP bmp;
526 HBITMAP hMaskBitmap;
527 COLORREF bkColor;
529 TRACE("himl %p, hbitmap %p, clrmask %#lx\n", himl, hBitmap, clrMask);
531 if (!is_valid(himl))
532 return -1;
534 if (!GetObjectW(hBitmap, sizeof(BITMAP), &bmp))
535 return -1;
537 hdcBitmap = CreateCompatibleDC(0);
538 SelectObject(hdcBitmap, hBitmap);
540 /* Create a temp Mask so we can remove the background of the Image */
541 hdcMask = CreateCompatibleDC(0);
542 hMaskBitmap = CreateBitmap(bmp.bmWidth, bmp.bmHeight, 1, 1, NULL);
543 SelectObject(hdcMask, hMaskBitmap);
545 /* create monochrome image to the mask bitmap */
546 bkColor = (clrMask != CLR_DEFAULT) ? clrMask : GetPixel (hdcBitmap, 0, 0);
547 SetBkColor (hdcBitmap, bkColor);
548 BitBlt (hdcMask, 0, 0, bmp.bmWidth, bmp.bmHeight, hdcBitmap, 0, 0, SRCCOPY);
551 * Remove the background from the image
553 * WINDOWS BUG ALERT!!!!!!
554 * The statement below should not be done in common practice
555 * but this is how ImageList_AddMasked works in Windows.
556 * It overwrites the original bitmap passed, this was discovered
557 * by using the same bitmap to iterate the different styles
558 * on windows where it failed (BUT ImageList_Add is OK)
559 * This is here in case some apps rely on this bug
561 * Blt mode 0x220326 is NOTSRCAND
563 if (bmp.bmBitsPixel > 8) /* NOTSRCAND can't work with palettes */
565 SetBkColor(hdcBitmap, RGB(255,255,255));
566 BitBlt(hdcBitmap, 0, 0, bmp.bmWidth, bmp.bmHeight, hdcMask, 0, 0, 0x220326);
569 DeleteDC(hdcBitmap);
570 DeleteDC(hdcMask);
572 ret = ImageList_Add( himl, hBitmap, hMaskBitmap );
574 DeleteObject(hMaskBitmap);
575 return ret;
579 /*************************************************************************
580 * ImageList_BeginDrag [COMCTL32.@]
582 * Creates a temporary image list that contains one image. It will be used
583 * as a drag image.
585 * PARAMS
586 * himlTrack [I] handle to the source image list
587 * iTrack [I] index of the drag image in the source image list
588 * dxHotspot [I] X position of the hot spot of the drag image
589 * dyHotspot [I] Y position of the hot spot of the drag image
591 * RETURNS
592 * Success: TRUE
593 * Failure: FALSE
596 BOOL WINAPI
597 ImageList_BeginDrag (HIMAGELIST himlTrack, INT iTrack,
598 INT dxHotspot, INT dyHotspot)
600 INT cx, cy;
601 POINT src, dst;
603 TRACE("(himlTrack=%p iTrack=%d dx=%d dy=%d)\n", himlTrack, iTrack,
604 dxHotspot, dyHotspot);
606 if (!is_valid(himlTrack))
607 return FALSE;
609 if (iTrack >= himlTrack->cCurImage)
610 return FALSE;
612 if (InternalDrag.himl)
613 return FALSE;
615 cx = himlTrack->cx;
616 cy = himlTrack->cy;
618 InternalDrag.himlNoCursor = InternalDrag.himl = ImageList_Create (cx, cy, himlTrack->flags, 1, 1);
619 if (InternalDrag.himl == NULL) {
620 WARN("Error creating drag image list!\n");
621 return FALSE;
624 InternalDrag.dxHotspot = dxHotspot;
625 InternalDrag.dyHotspot = dyHotspot;
627 /* copy image */
628 imagelist_point_from_index(InternalDrag.himl, 0, &dst);
629 imagelist_point_from_index(himlTrack, iTrack, &src);
630 BitBlt(InternalDrag.himl->hdcImage, dst.x, dst.y, cx, cy, himlTrack->hdcImage, src.x, src.y,
631 SRCCOPY);
632 BitBlt(InternalDrag.himl->hdcMask, dst.x, dst.y, cx, cy, himlTrack->hdcMask, src.x, src.y,
633 SRCCOPY);
635 InternalDrag.himl->cCurImage = 1;
637 return TRUE;
641 /*************************************************************************
642 * ImageList_Copy [COMCTL32.@]
644 * Copies an image of the source image list to an image of the
645 * destination image list. Images can be copied or swapped.
647 * PARAMS
648 * himlDst [I] handle to the destination image list
649 * iDst [I] destination image index.
650 * himlSrc [I] handle to the source image list
651 * iSrc [I] source image index
652 * uFlags [I] flags for the copy operation
654 * RETURNS
655 * Success: TRUE
656 * Failure: FALSE
658 * NOTES
659 * Copying from one image list to another is possible. The original
660 * implementation just copies or swaps within one image list.
661 * Could this feature become a bug??? ;-)
664 BOOL WINAPI
665 ImageList_Copy (HIMAGELIST himlDst, INT iDst, HIMAGELIST himlSrc,
666 INT iSrc, UINT uFlags)
668 POINT ptSrc, ptDst;
670 TRACE("himlDst=%p iDst=%d himlSrc=%p iSrc=%d\n", himlDst, iDst, himlSrc, iSrc);
672 if (!is_valid(himlSrc) || !is_valid(himlDst))
673 return FALSE;
674 if ((iDst < 0) || (iDst >= himlDst->cCurImage))
675 return FALSE;
676 if ((iSrc < 0) || (iSrc >= himlSrc->cCurImage))
677 return FALSE;
679 imagelist_point_from_index( himlDst, iDst, &ptDst );
680 imagelist_point_from_index( himlSrc, iSrc, &ptSrc );
682 if (uFlags & ILCF_SWAP) {
683 /* swap */
684 HDC hdcBmp;
685 HBITMAP hbmTempImage, hbmTempMask;
687 hdcBmp = CreateCompatibleDC (0);
689 /* create temporary bitmaps */
690 hbmTempImage = CreateBitmap (himlSrc->cx, himlSrc->cy, 1,
691 himlSrc->uBitsPixel, NULL);
692 hbmTempMask = CreateBitmap (himlSrc->cx, himlSrc->cy, 1,
693 1, NULL);
695 /* copy (and stretch) destination to temporary bitmaps.(save) */
696 /* image */
697 SelectObject (hdcBmp, hbmTempImage);
698 StretchBlt (hdcBmp, 0, 0, himlSrc->cx, himlSrc->cy,
699 himlDst->hdcImage, ptDst.x, ptDst.y, himlDst->cx, himlDst->cy,
700 SRCCOPY);
701 /* mask */
702 SelectObject (hdcBmp, hbmTempMask);
703 StretchBlt (hdcBmp, 0, 0, himlSrc->cx, himlSrc->cy,
704 himlDst->hdcMask, ptDst.x, ptDst.y, himlDst->cx, himlDst->cy,
705 SRCCOPY);
707 /* copy (and stretch) source to destination */
708 /* image */
709 StretchBlt (himlDst->hdcImage, ptDst.x, ptDst.y, himlDst->cx, himlDst->cy,
710 himlSrc->hdcImage, ptSrc.x, ptSrc.y, himlSrc->cx, himlSrc->cy,
711 SRCCOPY);
712 /* mask */
713 StretchBlt (himlDst->hdcMask, ptDst.x, ptDst.y, himlDst->cx, himlDst->cy,
714 himlSrc->hdcMask, ptSrc.x, ptSrc.y, himlSrc->cx, himlSrc->cy,
715 SRCCOPY);
717 /* copy (without stretching) temporary bitmaps to source (restore) */
718 /* mask */
719 BitBlt (himlSrc->hdcMask, ptSrc.x, ptSrc.y, himlSrc->cx, himlSrc->cy,
720 hdcBmp, 0, 0, SRCCOPY);
722 /* image */
723 BitBlt (himlSrc->hdcImage, ptSrc.x, ptSrc.y, himlSrc->cx, himlSrc->cy,
724 hdcBmp, 0, 0, SRCCOPY);
725 /* delete temporary bitmaps */
726 DeleteObject (hbmTempMask);
727 DeleteObject (hbmTempImage);
728 DeleteDC(hdcBmp);
730 else {
731 /* copy image */
732 StretchBlt (himlDst->hdcImage, ptDst.x, ptDst.y, himlDst->cx, himlDst->cy,
733 himlSrc->hdcImage, ptSrc.x, ptSrc.y, himlSrc->cx, himlSrc->cy,
734 SRCCOPY);
736 /* copy mask */
737 StretchBlt (himlDst->hdcMask, ptDst.x, ptDst.y, himlDst->cx, himlDst->cy,
738 himlSrc->hdcMask, ptSrc.x, ptSrc.y, himlSrc->cx, himlSrc->cy,
739 SRCCOPY);
742 return TRUE;
746 /*************************************************************************
747 * ImageList_Create [COMCTL32.@]
749 * Creates a new image list.
751 * PARAMS
752 * cx [I] image height
753 * cy [I] image width
754 * flags [I] creation flags
755 * cInitial [I] initial number of images in the image list
756 * cGrow [I] number of images by which image list grows
758 * RETURNS
759 * Success: Handle to the created image list
760 * Failure: NULL
762 HIMAGELIST WINAPI
763 ImageList_Create (INT cx, INT cy, UINT flags,
764 INT cInitial, INT cGrow)
766 HIMAGELIST himl;
767 INT nCount;
768 HBITMAP hbmTemp;
769 UINT ilc = (flags & 0xFE);
770 static const WORD aBitBlend25[] =
771 {0xAA, 0x00, 0x55, 0x00, 0xAA, 0x00, 0x55, 0x00};
773 static const WORD aBitBlend50[] =
774 {0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA};
776 TRACE("(%d %d 0x%x %d %d)\n", cx, cy, flags, cInitial, cGrow);
778 if (cx < 0 || cy < 0) return NULL;
779 if (!((flags&ILC_COLORDDB) == ILC_COLORDDB) && (cx == 0 || cy == 0)) return NULL;
781 /* Create the IImageList interface for the image list */
782 if (FAILED(ImageListImpl_CreateInstance(NULL, &IID_IImageList, (void **)&himl)))
783 return NULL;
785 cGrow = (WORD)((max( cGrow, 1 ) + 3) & ~3);
787 if (cGrow > 256)
789 /* Windows doesn't limit the size here, but X11 doesn't let us allocate such huge bitmaps */
790 WARN( "grow %d too large, limiting to 256\n", cGrow );
791 cGrow = 256;
794 himl->cx = cx;
795 himl->cy = cy;
796 himl->flags = flags;
797 himl->cMaxImage = cInitial + 1;
798 himl->cInitial = cInitial;
799 himl->cGrow = cGrow;
800 himl->clrFg = CLR_DEFAULT;
801 himl->clrBk = CLR_NONE;
802 himl->color_table_set = FALSE;
804 /* initialize overlay mask indices */
805 for (nCount = 0; nCount < MAX_OVERLAYIMAGE; nCount++)
806 himl->nOvlIdx[nCount] = -1;
808 /* Create Image & Mask DCs */
809 himl->hdcImage = CreateCompatibleDC (0);
810 if (!himl->hdcImage)
811 goto cleanup;
812 if (himl->flags & ILC_MASK){
813 himl->hdcMask = CreateCompatibleDC(0);
814 if (!himl->hdcMask)
815 goto cleanup;
818 /* Default to ILC_COLOR4 if none of the ILC_COLOR* flags are specified */
819 if (ilc == ILC_COLOR)
821 ilc = ILC_COLOR4;
822 himl->flags |= ILC_COLOR4;
825 if (ilc >= ILC_COLOR4 && ilc <= ILC_COLOR32)
826 himl->uBitsPixel = ilc;
827 else
828 himl->uBitsPixel = (UINT)GetDeviceCaps (himl->hdcImage, BITSPIXEL);
830 if (himl->cMaxImage > 0) {
831 himl->hbmImage = ImageList_CreateImage(himl->hdcImage, himl, himl->cMaxImage);
832 SelectObject(himl->hdcImage, himl->hbmImage);
833 } else
834 himl->hbmImage = 0;
836 if ((himl->cMaxImage > 0) && (himl->flags & ILC_MASK)) {
837 SIZE sz;
839 imagelist_get_bitmap_size(himl, himl->cMaxImage, &sz);
840 himl->hbmMask = CreateBitmap (sz.cx, sz.cy, 1, 1, NULL);
841 if (himl->hbmMask == 0) {
842 ERR("Error creating mask bitmap!\n");
843 goto cleanup;
845 SelectObject(himl->hdcMask, himl->hbmMask);
847 else
848 himl->hbmMask = 0;
850 himl->item_flags = Alloc( himl->cMaxImage * sizeof(*himl->item_flags) );
852 /* create blending brushes */
853 hbmTemp = CreateBitmap (8, 8, 1, 1, aBitBlend25);
854 himl->hbrBlend25 = CreatePatternBrush (hbmTemp);
855 DeleteObject (hbmTemp);
857 hbmTemp = CreateBitmap (8, 8, 1, 1, aBitBlend50);
858 himl->hbrBlend50 = CreatePatternBrush (hbmTemp);
859 DeleteObject (hbmTemp);
861 TRACE("created imagelist %p\n", himl);
862 return himl;
864 cleanup:
865 ImageList_Destroy(himl);
866 return NULL;
870 /*************************************************************************
871 * ImageList_Destroy [COMCTL32.@]
873 * Destroys an image list.
875 * PARAMS
876 * himl [I] handle to image list
878 * RETURNS
879 * Success: TRUE
880 * Failure: FALSE
883 BOOL WINAPI
884 ImageList_Destroy (HIMAGELIST himl)
886 if (!is_valid(himl))
887 return FALSE;
889 IImageList_Release((IImageList *) himl);
890 return TRUE;
894 /*************************************************************************
895 * ImageList_DragEnter [COMCTL32.@]
897 * Locks window update and displays the drag image at the given position.
899 * PARAMS
900 * hwndLock [I] handle of the window that owns the drag image.
901 * x [I] X position of the drag image.
902 * y [I] Y position of the drag image.
904 * RETURNS
905 * Success: TRUE
906 * Failure: FALSE
908 * NOTES
909 * The position of the drag image is relative to the window, not
910 * the client area.
913 BOOL WINAPI
914 ImageList_DragEnter (HWND hwndLock, INT x, INT y)
916 TRACE("(hwnd=%p x=%d y=%d)\n", hwndLock, x, y);
918 if (!is_valid(InternalDrag.himl))
919 return FALSE;
921 if (hwndLock)
922 InternalDrag.hwnd = hwndLock;
923 else
924 InternalDrag.hwnd = GetDesktopWindow ();
926 InternalDrag.x = x;
927 InternalDrag.y = y;
929 /* draw the drag image and save the background */
930 return ImageList_DragShowNolock(TRUE);
934 /*************************************************************************
935 * ImageList_DragLeave [COMCTL32.@]
937 * Unlocks window update and hides the drag image.
939 * PARAMS
940 * hwndLock [I] handle of the window that owns the drag image.
942 * RETURNS
943 * Success: TRUE
944 * Failure: FALSE
947 BOOL WINAPI
948 ImageList_DragLeave (HWND hwndLock)
950 /* As we don't save drag info in the window this can lead to problems if
951 an app does not supply the same window as DragEnter */
952 /* if (hwndLock)
953 InternalDrag.hwnd = hwndLock;
954 else
955 InternalDrag.hwnd = GetDesktopWindow (); */
956 if(!hwndLock)
957 hwndLock = GetDesktopWindow();
958 if(InternalDrag.hwnd != hwndLock)
959 FIXME("DragLeave hWnd != DragEnter hWnd\n");
961 ImageList_DragShowNolock (FALSE);
963 return TRUE;
967 /*************************************************************************
968 * ImageList_InternalDragDraw [Internal]
970 * Draws the drag image.
972 * PARAMS
973 * hdc [I] device context to draw into.
974 * x [I] X position of the drag image.
975 * y [I] Y position of the drag image.
977 * RETURNS
978 * Success: TRUE
979 * Failure: FALSE
981 * NOTES
982 * The position of the drag image is relative to the window, not
983 * the client area.
987 static inline void
988 ImageList_InternalDragDraw (HDC hdc, INT x, INT y)
990 IMAGELISTDRAWPARAMS imldp;
992 ZeroMemory (&imldp, sizeof(imldp));
993 imldp.cbSize = sizeof(imldp);
994 imldp.himl = InternalDrag.himl;
995 imldp.i = 0;
996 imldp.hdcDst = hdc;
997 imldp.x = x;
998 imldp.y = y;
999 imldp.rgbBk = CLR_DEFAULT;
1000 imldp.rgbFg = CLR_DEFAULT;
1001 imldp.fStyle = ILD_NORMAL;
1002 imldp.fState = ILS_ALPHA;
1003 imldp.Frame = 192;
1004 ImageList_DrawIndirect (&imldp);
1007 /*************************************************************************
1008 * ImageList_DragMove [COMCTL32.@]
1010 * Moves the drag image.
1012 * PARAMS
1013 * x [I] X position of the drag image.
1014 * y [I] Y position of the drag image.
1016 * RETURNS
1017 * Success: TRUE
1018 * Failure: FALSE
1020 * NOTES
1021 * The position of the drag image is relative to the window, not
1022 * the client area.
1025 BOOL WINAPI
1026 ImageList_DragMove (INT x, INT y)
1028 TRACE("(x=%d y=%d)\n", x, y);
1030 if (!is_valid(InternalDrag.himl))
1031 return FALSE;
1033 /* draw/update the drag image */
1034 if (InternalDrag.bShow) {
1035 HDC hdcDrag;
1036 HDC hdcOffScreen;
1037 HDC hdcBg;
1038 HBITMAP hbmOffScreen;
1039 INT origNewX, origNewY;
1040 INT origOldX, origOldY;
1041 INT origRegX, origRegY;
1042 INT sizeRegX, sizeRegY;
1045 /* calculate the update region */
1046 origNewX = x - InternalDrag.dxHotspot;
1047 origNewY = y - InternalDrag.dyHotspot;
1048 origOldX = InternalDrag.x - InternalDrag.dxHotspot;
1049 origOldY = InternalDrag.y - InternalDrag.dyHotspot;
1050 origRegX = min(origNewX, origOldX);
1051 origRegY = min(origNewY, origOldY);
1052 sizeRegX = InternalDrag.himl->cx + abs(x - InternalDrag.x);
1053 sizeRegY = InternalDrag.himl->cy + abs(y - InternalDrag.y);
1055 hdcDrag = GetDCEx(InternalDrag.hwnd, 0,
1056 DCX_WINDOW | DCX_CACHE | DCX_LOCKWINDOWUPDATE);
1057 hdcOffScreen = CreateCompatibleDC(hdcDrag);
1058 hdcBg = CreateCompatibleDC(hdcDrag);
1060 hbmOffScreen = CreateCompatibleBitmap(hdcDrag, sizeRegX, sizeRegY);
1061 SelectObject(hdcOffScreen, hbmOffScreen);
1062 SelectObject(hdcBg, InternalDrag.hbmBg);
1064 /* get the actual background of the update region */
1065 BitBlt(hdcOffScreen, 0, 0, sizeRegX, sizeRegY, hdcDrag,
1066 origRegX, origRegY, SRCCOPY);
1067 /* erase the old image */
1068 BitBlt(hdcOffScreen, origOldX - origRegX, origOldY - origRegY,
1069 InternalDrag.himl->cx, InternalDrag.himl->cy, hdcBg, 0, 0,
1070 SRCCOPY);
1071 /* save the background */
1072 BitBlt(hdcBg, 0, 0, InternalDrag.himl->cx, InternalDrag.himl->cy,
1073 hdcOffScreen, origNewX - origRegX, origNewY - origRegY, SRCCOPY);
1074 /* draw the image */
1075 ImageList_InternalDragDraw(hdcOffScreen, origNewX - origRegX,
1076 origNewY - origRegY);
1077 /* draw the update region to the screen */
1078 BitBlt(hdcDrag, origRegX, origRegY, sizeRegX, sizeRegY,
1079 hdcOffScreen, 0, 0, SRCCOPY);
1081 DeleteDC(hdcBg);
1082 DeleteDC(hdcOffScreen);
1083 DeleteObject(hbmOffScreen);
1084 ReleaseDC(InternalDrag.hwnd, hdcDrag);
1087 /* update the image position */
1088 InternalDrag.x = x;
1089 InternalDrag.y = y;
1091 return TRUE;
1095 /*************************************************************************
1096 * ImageList_DragShowNolock [COMCTL32.@]
1098 * Shows or hides the drag image.
1100 * PARAMS
1101 * bShow [I] TRUE shows the drag image, FALSE hides it.
1103 * RETURNS
1104 * Success: TRUE
1105 * Failure: FALSE
1108 BOOL WINAPI
1109 ImageList_DragShowNolock (BOOL bShow)
1111 HDC hdcDrag;
1112 HDC hdcBg;
1113 INT x, y;
1115 if (!is_valid(InternalDrag.himl))
1116 return FALSE;
1118 TRACE("bShow=0x%X!\n", bShow);
1120 /* DragImage is already visible/hidden */
1121 if ((InternalDrag.bShow && bShow) || (!InternalDrag.bShow && !bShow)) {
1122 return FALSE;
1125 /* position of the origin of the DragImage */
1126 x = InternalDrag.x - InternalDrag.dxHotspot;
1127 y = InternalDrag.y - InternalDrag.dyHotspot;
1129 hdcDrag = GetDCEx (InternalDrag.hwnd, 0,
1130 DCX_WINDOW | DCX_CACHE | DCX_LOCKWINDOWUPDATE);
1131 if (!hdcDrag) {
1132 return FALSE;
1135 hdcBg = CreateCompatibleDC(hdcDrag);
1136 if (!InternalDrag.hbmBg) {
1137 InternalDrag.hbmBg = CreateCompatibleBitmap(hdcDrag,
1138 InternalDrag.himl->cx, InternalDrag.himl->cy);
1140 SelectObject(hdcBg, InternalDrag.hbmBg);
1142 if (bShow) {
1143 /* save the background */
1144 BitBlt(hdcBg, 0, 0, InternalDrag.himl->cx, InternalDrag.himl->cy,
1145 hdcDrag, x, y, SRCCOPY);
1146 /* show the image */
1147 ImageList_InternalDragDraw(hdcDrag, x, y);
1148 } else {
1149 /* hide the image */
1150 BitBlt(hdcDrag, x, y, InternalDrag.himl->cx, InternalDrag.himl->cy,
1151 hdcBg, 0, 0, SRCCOPY);
1154 InternalDrag.bShow = !InternalDrag.bShow;
1156 DeleteDC(hdcBg);
1157 ReleaseDC (InternalDrag.hwnd, hdcDrag);
1158 return TRUE;
1162 /*************************************************************************
1163 * ImageList_Draw [COMCTL32.@]
1165 * Draws an image.
1167 * PARAMS
1168 * himl [I] handle to image list
1169 * i [I] image index
1170 * hdc [I] handle to device context
1171 * x [I] x position
1172 * y [I] y position
1173 * fStyle [I] drawing flags
1175 * RETURNS
1176 * Success: TRUE
1177 * Failure: FALSE
1179 * SEE
1180 * ImageList_DrawEx.
1183 BOOL WINAPI
1184 ImageList_Draw (HIMAGELIST himl, INT i, HDC hdc, INT x, INT y, UINT fStyle)
1186 return ImageList_DrawEx (himl, i, hdc, x, y, 0, 0,
1187 CLR_DEFAULT, CLR_DEFAULT, fStyle);
1191 /*************************************************************************
1192 * ImageList_DrawEx [COMCTL32.@]
1194 * Draws an image and allows using extended drawing features.
1196 * PARAMS
1197 * himl [I] handle to image list
1198 * i [I] image index
1199 * hdc [I] handle to device context
1200 * x [I] X position
1201 * y [I] Y position
1202 * dx [I] X offset
1203 * dy [I] Y offset
1204 * rgbBk [I] background color
1205 * rgbFg [I] foreground color
1206 * fStyle [I] drawing flags
1208 * RETURNS
1209 * Success: TRUE
1210 * Failure: FALSE
1212 * NOTES
1213 * Calls ImageList_DrawIndirect.
1215 * SEE
1216 * ImageList_DrawIndirect.
1219 BOOL WINAPI
1220 ImageList_DrawEx (HIMAGELIST himl, INT i, HDC hdc, INT x, INT y,
1221 INT dx, INT dy, COLORREF rgbBk, COLORREF rgbFg,
1222 UINT fStyle)
1224 IMAGELISTDRAWPARAMS imldp;
1226 ZeroMemory (&imldp, sizeof(imldp));
1227 imldp.cbSize = sizeof(imldp);
1228 imldp.himl = himl;
1229 imldp.i = i;
1230 imldp.hdcDst = hdc;
1231 imldp.x = x;
1232 imldp.y = y;
1233 imldp.cx = dx;
1234 imldp.cy = dy;
1235 imldp.rgbBk = rgbBk;
1236 imldp.rgbFg = rgbFg;
1237 imldp.fStyle = fStyle;
1239 return ImageList_DrawIndirect (&imldp);
1243 static BOOL alpha_blend_image( HIMAGELIST himl, HDC dest_dc, int dest_x, int dest_y,
1244 int src_x, int src_y, int cx, int cy, UINT style, UINT state,
1245 DWORD frame, COLORREF blend_col, BOOL has_alpha )
1247 BOOL ret = FALSE;
1248 HDC hdc;
1249 HBITMAP bmp = 0, mask = 0;
1250 BITMAPINFO *info;
1251 BLENDFUNCTION func;
1252 void *bits, *mask_bits;
1253 unsigned int *ptr;
1254 int i, j;
1256 func.BlendOp = AC_SRC_OVER;
1257 func.BlendFlags = 0;
1258 func.SourceConstantAlpha = 255;
1259 func.AlphaFormat = AC_SRC_ALPHA;
1261 if (!(hdc = CreateCompatibleDC( 0 ))) return FALSE;
1262 if (!(info = Alloc( FIELD_OFFSET( BITMAPINFO, bmiColors[256] )))) goto done;
1263 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1264 info->bmiHeader.biWidth = cx;
1265 info->bmiHeader.biHeight = cy;
1266 info->bmiHeader.biPlanes = 1;
1267 info->bmiHeader.biBitCount = 32;
1268 info->bmiHeader.biCompression = BI_RGB;
1269 info->bmiHeader.biSizeImage = cx * cy * 4;
1270 info->bmiHeader.biXPelsPerMeter = 0;
1271 info->bmiHeader.biYPelsPerMeter = 0;
1272 info->bmiHeader.biClrUsed = 0;
1273 info->bmiHeader.biClrImportant = 0;
1274 if (!(bmp = CreateDIBSection( himl->hdcImage, info, DIB_RGB_COLORS, &bits, 0, 0 ))) goto done;
1275 SelectObject( hdc, bmp );
1276 BitBlt( hdc, 0, 0, cx, cy, himl->hdcImage, src_x, src_y, SRCCOPY );
1278 if (blend_col != CLR_NONE)
1280 BYTE r = GetRValue( blend_col );
1281 BYTE g = GetGValue( blend_col );
1282 BYTE b = GetBValue( blend_col );
1284 if (style & ILD_BLEND25)
1286 for (i = 0, ptr = bits; i < cx * cy; i++, ptr++)
1287 *ptr = ((*ptr & 0xff000000) |
1288 ((((*ptr & 0x00ff0000) * 3 + (r << 16)) / 4) & 0x00ff0000) |
1289 ((((*ptr & 0x0000ff00) * 3 + (g << 8)) / 4) & 0x0000ff00) |
1290 ((((*ptr & 0x000000ff) * 3 + (b << 0)) / 4) & 0x000000ff));
1292 else if (style & ILD_BLEND50)
1294 for (i = 0, ptr = bits; i < cx * cy; i++, ptr++)
1295 *ptr = ((*ptr & 0xff000000) |
1296 ((((*ptr & 0x00ff0000) + (r << 16)) / 2) & 0x00ff0000) |
1297 ((((*ptr & 0x0000ff00) + (g << 8)) / 2) & 0x0000ff00) |
1298 ((((*ptr & 0x000000ff) + (b << 0)) / 2) & 0x000000ff));
1303 if (state & ILS_ALPHA)
1305 func.SourceConstantAlpha = (BYTE)frame;
1307 else if (state & ILS_SATURATE)
1309 for (i = 0, ptr = bits; i < cx * cy; i++, ptr++)
1311 DWORD gray = (((*ptr & 0x00ff0000) >> 16) * 299 +
1312 ((*ptr & 0x0000ff00) >> 8) * 587 +
1313 ((*ptr & 0x000000ff) >> 0) * 114 + 500) / 1000;
1314 if (has_alpha) gray = gray * (*ptr >> 24) / 255;
1315 *ptr = (*ptr & 0xff000000)| (gray << 16) | (gray << 8) | gray;
1318 else if (style & ILD_PRESERVEALPHA)
1320 HBRUSH old_brush = SelectObject( dest_dc, GetStockObject(BLACK_BRUSH) );
1321 PatBlt( dest_dc, dest_x, dest_y, cx, cy, PATCOPY );
1322 SelectObject( dest_dc, old_brush );
1325 if (has_alpha) /* we already have an alpha channel in this case */
1327 /* pre-multiply by the alpha channel */
1328 for (i = 0, ptr = bits; i < cx * cy; i++, ptr++)
1330 DWORD alpha = *ptr >> 24;
1331 *ptr = ((*ptr & 0xff000000) |
1332 (((*ptr & 0x00ff0000) * alpha / 255) & 0x00ff0000) |
1333 (((*ptr & 0x0000ff00) * alpha / 255) & 0x0000ff00) |
1334 (((*ptr & 0x000000ff) * alpha / 255)));
1337 else if (himl->hbmMask)
1339 unsigned int width_bytes = (cx + 31) / 32 * 4;
1340 /* generate alpha channel from the mask */
1341 info->bmiHeader.biBitCount = 1;
1342 info->bmiHeader.biSizeImage = width_bytes * cy;
1343 info->bmiColors[0].rgbRed = 0;
1344 info->bmiColors[0].rgbGreen = 0;
1345 info->bmiColors[0].rgbBlue = 0;
1346 info->bmiColors[0].rgbReserved = 0;
1347 info->bmiColors[1].rgbRed = 0xff;
1348 info->bmiColors[1].rgbGreen = 0xff;
1349 info->bmiColors[1].rgbBlue = 0xff;
1350 info->bmiColors[1].rgbReserved = 0;
1351 if (!(mask = CreateDIBSection( himl->hdcMask, info, DIB_RGB_COLORS, &mask_bits, 0, 0 )))
1352 goto done;
1353 SelectObject( hdc, mask );
1354 BitBlt( hdc, 0, 0, cx, cy, himl->hdcMask, src_x, src_y, SRCCOPY );
1355 SelectObject( hdc, bmp );
1356 for (i = 0, ptr = bits; i < cy; i++)
1357 for (j = 0; j < cx; j++, ptr++)
1358 if ((((BYTE *)mask_bits)[i * width_bytes + j / 8] << (j % 8)) & 0x80) *ptr = 0;
1359 else *ptr |= 0xff000000;
1361 else
1363 for (i = 0, ptr = bits; i < cx * cy; i++, ptr++)
1364 *ptr |= 0xff000000;
1367 ret = GdiAlphaBlend( dest_dc, dest_x, dest_y, cx, cy, hdc, 0, 0, cx, cy, func );
1369 done:
1370 DeleteDC( hdc );
1371 if (bmp) DeleteObject( bmp );
1372 if (mask) DeleteObject( mask );
1373 Free( info );
1374 return ret;
1377 /*************************************************************************
1378 * ImageList_DrawIndirect [COMCTL32.@]
1380 * Draws an image using various parameters specified in pimldp.
1382 * PARAMS
1383 * pimldp [I] pointer to IMAGELISTDRAWPARAMS structure.
1385 * RETURNS
1386 * Success: TRUE
1387 * Failure: FALSE
1390 BOOL WINAPI
1391 ImageList_DrawIndirect (IMAGELISTDRAWPARAMS *pimldp)
1393 INT cx, cy, nOvlIdx;
1394 DWORD fState, dwRop;
1395 UINT fStyle;
1396 COLORREF oldImageBk, oldImageFg;
1397 HDC hImageDC, hImageListDC, hMaskListDC;
1398 HBITMAP hImageBmp, hOldImageBmp, hBlendMaskBmp;
1399 BOOL bIsTransparent, bBlend, bResult = FALSE, bMask;
1400 HIMAGELIST himl;
1401 HBRUSH hOldBrush;
1402 POINT pt;
1403 BOOL has_alpha;
1405 if (!pimldp || !(himl = pimldp->himl)) return FALSE;
1406 if (!is_valid(himl)) return FALSE;
1407 if ((pimldp->i < 0) || (pimldp->i >= himl->cCurImage)) return FALSE;
1409 imagelist_point_from_index( himl, pimldp->i, &pt );
1410 pt.x += pimldp->xBitmap;
1411 pt.y += pimldp->yBitmap;
1413 fState = pimldp->cbSize < sizeof(IMAGELISTDRAWPARAMS) ? ILS_NORMAL : pimldp->fState;
1414 fStyle = pimldp->fStyle & ~ILD_OVERLAYMASK;
1415 cx = (pimldp->cx == 0) ? himl->cx : pimldp->cx;
1416 cy = (pimldp->cy == 0) ? himl->cy : pimldp->cy;
1418 bIsTransparent = (fStyle & ILD_TRANSPARENT);
1419 if( pimldp->rgbBk == CLR_NONE )
1420 bIsTransparent = TRUE;
1421 if( ( pimldp->rgbBk == CLR_DEFAULT ) && ( himl->clrBk == CLR_NONE ) )
1422 bIsTransparent = TRUE;
1423 bMask = (himl->flags & ILC_MASK) && (fStyle & ILD_MASK) ;
1424 bBlend = (fStyle & (ILD_BLEND25 | ILD_BLEND50) ) && !bMask;
1426 TRACE("himl(%p) hbmMask(%p) iImage(%d) x(%d) y(%d) cx(%d) cy(%d)\n",
1427 himl, himl->hbmMask, pimldp->i, pimldp->x, pimldp->y, cx, cy);
1429 /* we will use these DCs to access the images and masks in the ImageList */
1430 hImageListDC = himl->hdcImage;
1431 hMaskListDC = himl->hdcMask;
1433 /* these will accumulate the image and mask for the image we're drawing */
1434 hImageDC = CreateCompatibleDC( pimldp->hdcDst );
1435 hImageBmp = CreateCompatibleBitmap( pimldp->hdcDst, cx, cy );
1436 hBlendMaskBmp = bBlend ? CreateBitmap(cx, cy, 1, 1, NULL) : 0;
1438 /* Create a compatible DC. */
1439 if (!hImageListDC || !hImageDC || !hImageBmp ||
1440 (bBlend && !hBlendMaskBmp) || (himl->hbmMask && !hMaskListDC))
1441 goto cleanup;
1443 hOldImageBmp = SelectObject(hImageDC, hImageBmp);
1446 * To obtain a transparent look, background color should be set
1447 * to white and foreground color to black when blitting the
1448 * monochrome mask.
1450 oldImageFg = SetTextColor( hImageDC, RGB( 0, 0, 0 ) );
1451 oldImageBk = SetBkColor( hImageDC, RGB( 0xff, 0xff, 0xff ) );
1453 has_alpha = himl->item_flags[pimldp->i] & ILIF_ALPHA;
1454 if (!bMask && (has_alpha || (fState & ILS_ALPHA) || (fState & ILS_SATURATE)))
1456 COLORREF colour, blend_col = CLR_NONE;
1458 if (bBlend)
1460 blend_col = pimldp->rgbFg;
1461 if (blend_col == CLR_DEFAULT) blend_col = GetSysColor( COLOR_HIGHLIGHT );
1462 else if (blend_col == CLR_NONE) blend_col = GetTextColor( pimldp->hdcDst );
1465 if (bIsTransparent)
1467 bResult = alpha_blend_image( himl, pimldp->hdcDst, pimldp->x, pimldp->y, pt.x, pt.y, cx, cy,
1468 fStyle, fState, pimldp->Frame, blend_col, has_alpha );
1469 goto end;
1471 colour = pimldp->rgbBk;
1472 if (colour == CLR_DEFAULT) colour = himl->clrBk;
1473 if (colour == CLR_NONE) colour = GetBkColor( pimldp->hdcDst );
1475 hOldBrush = SelectObject (hImageDC, CreateSolidBrush (colour));
1476 PatBlt( hImageDC, 0, 0, cx, cy, PATCOPY );
1477 alpha_blend_image( himl, hImageDC, 0, 0, pt.x, pt.y, cx, cy, fStyle, fState,
1478 pimldp->Frame, blend_col, has_alpha );
1479 DeleteObject (SelectObject (hImageDC, hOldBrush));
1480 bResult = BitBlt( pimldp->hdcDst, pimldp->x, pimldp->y, cx, cy, hImageDC, 0, 0, SRCCOPY );
1481 goto end;
1485 * Draw the initial image
1487 if( bMask ) {
1488 if (himl->hbmMask) {
1489 hOldBrush = SelectObject (hImageDC, CreateSolidBrush (GetTextColor(pimldp->hdcDst)));
1490 PatBlt( hImageDC, 0, 0, cx, cy, PATCOPY );
1491 BitBlt(hImageDC, 0, 0, cx, cy, hMaskListDC, pt.x, pt.y, SRCPAINT);
1492 DeleteObject (SelectObject (hImageDC, hOldBrush));
1493 if( bIsTransparent )
1495 BitBlt ( pimldp->hdcDst, pimldp->x, pimldp->y, cx, cy, hImageDC, 0, 0, SRCAND);
1496 bResult = TRUE;
1497 goto end;
1499 } else {
1500 hOldBrush = SelectObject (hImageDC, GetStockObject(BLACK_BRUSH));
1501 PatBlt( hImageDC, 0, 0, cx, cy, PATCOPY);
1502 SelectObject(hImageDC, hOldBrush);
1504 } else {
1505 /* blend the image with the needed solid background */
1506 COLORREF colour = RGB(0,0,0);
1508 if( !bIsTransparent )
1510 colour = pimldp->rgbBk;
1511 if( colour == CLR_DEFAULT )
1512 colour = himl->clrBk;
1513 if( colour == CLR_NONE )
1514 colour = GetBkColor(pimldp->hdcDst);
1517 hOldBrush = SelectObject (hImageDC, CreateSolidBrush (colour));
1518 PatBlt( hImageDC, 0, 0, cx, cy, PATCOPY );
1519 if (himl->hbmMask)
1521 BitBlt( hImageDC, 0, 0, cx, cy, hMaskListDC, pt.x, pt.y, SRCAND );
1522 BitBlt( hImageDC, 0, 0, cx, cy, hImageListDC, pt.x, pt.y, SRCPAINT );
1524 else
1525 BitBlt( hImageDC, 0, 0, cx, cy, hImageListDC, pt.x, pt.y, SRCCOPY);
1526 DeleteObject (SelectObject (hImageDC, hOldBrush));
1529 /* Time for blending, if required */
1530 if (bBlend) {
1531 HBRUSH hBlendBrush;
1532 COLORREF clrBlend = pimldp->rgbFg;
1533 HDC hBlendMaskDC = hImageListDC;
1534 HBITMAP hOldBitmap;
1536 /* Create the blend Mask */
1537 hOldBitmap = SelectObject(hBlendMaskDC, hBlendMaskBmp);
1538 hBlendBrush = fStyle & ILD_BLEND50 ? himl->hbrBlend50 : himl->hbrBlend25;
1539 hOldBrush = SelectObject(hBlendMaskDC, hBlendBrush);
1540 PatBlt(hBlendMaskDC, 0, 0, cx, cy, PATCOPY);
1541 SelectObject(hBlendMaskDC, hOldBrush);
1543 /* Modify the blend mask if an Image Mask exist */
1544 if(himl->hbmMask) {
1545 BitBlt(hBlendMaskDC, 0, 0, cx, cy, hMaskListDC, pt.x, pt.y, 0x220326); /* NOTSRCAND */
1546 BitBlt(hBlendMaskDC, 0, 0, cx, cy, hBlendMaskDC, 0, 0, NOTSRCCOPY);
1549 /* now apply blend to the current image given the BlendMask */
1550 if (clrBlend == CLR_DEFAULT) clrBlend = GetSysColor (COLOR_HIGHLIGHT);
1551 else if (clrBlend == CLR_NONE) clrBlend = GetTextColor (pimldp->hdcDst);
1552 hOldBrush = SelectObject (hImageDC, CreateSolidBrush(clrBlend));
1553 BitBlt (hImageDC, 0, 0, cx, cy, hBlendMaskDC, 0, 0, 0xB8074A); /* PSDPxax */
1554 DeleteObject(SelectObject(hImageDC, hOldBrush));
1555 SelectObject(hBlendMaskDC, hOldBitmap);
1558 /* Now do the overlay image, if any */
1559 nOvlIdx = (pimldp->fStyle & ILD_OVERLAYMASK) >> 8;
1560 if ( (nOvlIdx >= 1) && (nOvlIdx <= MAX_OVERLAYIMAGE)) {
1561 nOvlIdx = himl->nOvlIdx[nOvlIdx - 1];
1562 if ((nOvlIdx >= 0) && (nOvlIdx < himl->cCurImage)) {
1563 POINT ptOvl;
1564 imagelist_point_from_index( himl, nOvlIdx, &ptOvl );
1565 ptOvl.x += pimldp->xBitmap;
1566 if (himl->hbmMask && !(fStyle & ILD_IMAGE))
1567 BitBlt (hImageDC, 0, 0, cx, cy, hMaskListDC, ptOvl.x, ptOvl.y, SRCAND);
1568 BitBlt (hImageDC, 0, 0, cx, cy, hImageListDC, ptOvl.x, ptOvl.y, SRCPAINT);
1572 if (fState & ILS_GLOW) FIXME("ILS_GLOW: unimplemented!\n");
1573 if (fState & ILS_SHADOW) FIXME("ILS_SHADOW: unimplemented!\n");
1575 if (fStyle & ILD_SCALE) FIXME("ILD_SCALE: unimplemented!\n");
1576 if (fStyle & ILD_DPISCALE) FIXME("ILD_DPISCALE: unimplemented!\n");
1578 /* now copy the image to the screen */
1579 dwRop = SRCCOPY;
1580 if (himl->hbmMask && bIsTransparent ) {
1581 COLORREF oldDstFg = SetTextColor(pimldp->hdcDst, RGB( 0, 0, 0 ) );
1582 COLORREF oldDstBk = SetBkColor(pimldp->hdcDst, RGB( 0xff, 0xff, 0xff ));
1583 BitBlt (pimldp->hdcDst, pimldp->x, pimldp->y, cx, cy, hMaskListDC, pt.x, pt.y, SRCAND);
1584 SetBkColor(pimldp->hdcDst, oldDstBk);
1585 SetTextColor(pimldp->hdcDst, oldDstFg);
1586 dwRop = SRCPAINT;
1588 if (fStyle & ILD_ROP) dwRop = pimldp->dwRop;
1589 BitBlt (pimldp->hdcDst, pimldp->x, pimldp->y, cx, cy, hImageDC, 0, 0, dwRop);
1591 bResult = TRUE;
1592 end:
1593 /* cleanup the mess */
1594 SetBkColor(hImageDC, oldImageBk);
1595 SetTextColor(hImageDC, oldImageFg);
1596 SelectObject(hImageDC, hOldImageBmp);
1597 cleanup:
1598 DeleteObject(hBlendMaskBmp);
1599 DeleteObject(hImageBmp);
1600 DeleteDC(hImageDC);
1602 return bResult;
1606 /*************************************************************************
1607 * ImageList_Duplicate [COMCTL32.@]
1609 * Duplicates an image list.
1611 * PARAMS
1612 * himlSrc [I] source image list handle
1614 * RETURNS
1615 * Success: Handle of duplicated image list.
1616 * Failure: NULL
1619 HIMAGELIST WINAPI
1620 ImageList_Duplicate (HIMAGELIST himlSrc)
1622 HIMAGELIST himlDst;
1624 if (!is_valid(himlSrc)) {
1625 ERR("Invalid image list handle!\n");
1626 return NULL;
1629 himlDst = ImageList_Create (himlSrc->cx, himlSrc->cy, himlSrc->flags,
1630 himlSrc->cCurImage, himlSrc->cGrow);
1632 if (himlDst)
1634 SIZE sz;
1636 imagelist_get_bitmap_size(himlSrc, himlSrc->cCurImage, &sz);
1637 BitBlt (himlDst->hdcImage, 0, 0, sz.cx, sz.cy,
1638 himlSrc->hdcImage, 0, 0, SRCCOPY);
1640 if (himlDst->hbmMask)
1641 BitBlt (himlDst->hdcMask, 0, 0, sz.cx, sz.cy,
1642 himlSrc->hdcMask, 0, 0, SRCCOPY);
1644 himlDst->cCurImage = himlSrc->cCurImage;
1645 memcpy( himlDst->item_flags, himlSrc->item_flags, himlDst->cCurImage * sizeof(*himlDst->item_flags) );
1647 return himlDst;
1651 /*************************************************************************
1652 * ImageList_EndDrag [COMCTL32.@]
1654 * Finishes a drag operation.
1656 * PARAMS
1657 * no Parameters
1659 * RETURNS
1660 * Success: TRUE
1661 * Failure: FALSE
1664 VOID WINAPI
1665 ImageList_EndDrag (void)
1667 /* cleanup the InternalDrag struct */
1668 InternalDrag.hwnd = 0;
1669 if (InternalDrag.himl != InternalDrag.himlNoCursor)
1670 ImageList_Destroy (InternalDrag.himlNoCursor);
1671 ImageList_Destroy (InternalDrag.himl);
1672 InternalDrag.himlNoCursor = InternalDrag.himl = 0;
1673 InternalDrag.x= 0;
1674 InternalDrag.y= 0;
1675 InternalDrag.dxHotspot = 0;
1676 InternalDrag.dyHotspot = 0;
1677 InternalDrag.bShow = FALSE;
1678 DeleteObject(InternalDrag.hbmBg);
1679 InternalDrag.hbmBg = 0;
1683 /*************************************************************************
1684 * ImageList_GetBkColor [COMCTL32.@]
1686 * Returns the background color of an image list.
1688 * PARAMS
1689 * himl [I] Image list handle.
1691 * RETURNS
1692 * Success: background color
1693 * Failure: CLR_NONE
1696 COLORREF WINAPI
1697 ImageList_GetBkColor (HIMAGELIST himl)
1699 return himl ? himl->clrBk : CLR_NONE;
1703 /*************************************************************************
1704 * ImageList_GetDragImage [COMCTL32.@]
1706 * Returns the handle to the internal drag image list.
1708 * PARAMS
1709 * ppt [O] Pointer to the drag position. Can be NULL.
1710 * pptHotspot [O] Pointer to the position of the hot spot. Can be NULL.
1712 * RETURNS
1713 * Success: Handle of the drag image list.
1714 * Failure: NULL.
1717 HIMAGELIST WINAPI
1718 ImageList_GetDragImage (POINT *ppt, POINT *pptHotspot)
1720 if (is_valid(InternalDrag.himl)) {
1721 if (ppt) {
1722 ppt->x = InternalDrag.x;
1723 ppt->y = InternalDrag.y;
1725 if (pptHotspot) {
1726 pptHotspot->x = InternalDrag.dxHotspot;
1727 pptHotspot->y = InternalDrag.dyHotspot;
1729 return (InternalDrag.himl);
1732 return NULL;
1736 /*************************************************************************
1737 * ImageList_GetFlags [COMCTL32.@]
1739 * Gets the flags of the specified image list.
1741 * PARAMS
1742 * himl [I] Handle to image list
1744 * RETURNS
1745 * Image list flags.
1747 * BUGS
1748 * Stub.
1751 DWORD WINAPI
1752 ImageList_GetFlags(HIMAGELIST himl)
1754 TRACE("%p\n", himl);
1756 return is_valid(himl) ? himl->flags : 0;
1760 /*************************************************************************
1761 * ImageList_GetIcon [COMCTL32.@]
1763 * Creates an icon from a masked image of an image list.
1765 * PARAMS
1766 * himl [I] handle to image list
1767 * i [I] image index
1768 * flags [I] drawing style flags
1770 * RETURNS
1771 * Success: icon handle
1772 * Failure: NULL
1775 HICON WINAPI
1776 ImageList_GetIcon (HIMAGELIST himl, INT i, UINT fStyle)
1778 ICONINFO ii;
1779 HICON hIcon;
1780 HBITMAP hOldDstBitmap;
1781 HDC hdcDst;
1782 POINT pt;
1784 TRACE("%p %d %d\n", himl, i, fStyle);
1785 if (!is_valid(himl) || (i < 0) || (i >= himl->cCurImage)) return NULL;
1787 ii.fIcon = TRUE;
1788 ii.xHotspot = 0;
1789 ii.yHotspot = 0;
1791 /* create colour bitmap */
1792 hdcDst = GetDC(0);
1793 ii.hbmColor = CreateCompatibleBitmap(hdcDst, himl->cx, himl->cy);
1794 ReleaseDC(0, hdcDst);
1796 hdcDst = CreateCompatibleDC(0);
1798 imagelist_point_from_index( himl, i, &pt );
1800 /* draw mask*/
1801 ii.hbmMask = CreateBitmap (himl->cx, himl->cy, 1, 1, NULL);
1802 hOldDstBitmap = SelectObject (hdcDst, ii.hbmMask);
1803 if (himl->hbmMask) {
1804 BitBlt (hdcDst, 0, 0, himl->cx, himl->cy,
1805 himl->hdcMask, pt.x, pt.y, SRCCOPY);
1807 else
1808 PatBlt (hdcDst, 0, 0, himl->cx, himl->cy, BLACKNESS);
1810 /* draw image*/
1811 SelectObject (hdcDst, ii.hbmColor);
1812 BitBlt (hdcDst, 0, 0, himl->cx, himl->cy,
1813 himl->hdcImage, pt.x, pt.y, SRCCOPY);
1816 * CreateIconIndirect requires us to deselect the bitmaps from
1817 * the DCs before calling
1819 SelectObject(hdcDst, hOldDstBitmap);
1821 hIcon = CreateIconIndirect (&ii);
1823 DeleteObject (ii.hbmMask);
1824 DeleteObject (ii.hbmColor);
1825 DeleteDC (hdcDst);
1827 return hIcon;
1831 /*************************************************************************
1832 * ImageList_GetIconSize [COMCTL32.@]
1834 * Retrieves the size of an image in an image list.
1836 * PARAMS
1837 * himl [I] handle to image list
1838 * cx [O] pointer to the image width.
1839 * cy [O] pointer to the image height.
1841 * RETURNS
1842 * Success: TRUE
1843 * Failure: FALSE
1845 * NOTES
1846 * All images in an image list have the same size.
1849 BOOL WINAPI
1850 ImageList_GetIconSize (HIMAGELIST himl, INT *cx, INT *cy)
1852 if (!is_valid(himl) || !cx || !cy)
1853 return FALSE;
1855 *cx = himl->cx;
1856 *cy = himl->cy;
1858 return TRUE;
1862 /*************************************************************************
1863 * ImageList_GetImageCount [COMCTL32.@]
1865 * Returns the number of images in an image list.
1867 * PARAMS
1868 * himl [I] handle to image list
1870 * RETURNS
1871 * Success: Number of images.
1872 * Failure: 0
1875 INT WINAPI
1876 ImageList_GetImageCount (HIMAGELIST himl)
1878 if (!is_valid(himl))
1879 return 0;
1881 return himl->cCurImage;
1885 /*************************************************************************
1886 * ImageList_GetImageInfo [COMCTL32.@]
1888 * Returns information about an image in an image list.
1890 * PARAMS
1891 * himl [I] handle to image list
1892 * i [I] image index
1893 * pImageInfo [O] pointer to the image information
1895 * RETURNS
1896 * Success: TRUE
1897 * Failure: FALSE
1900 BOOL WINAPI
1901 ImageList_GetImageInfo (HIMAGELIST himl, INT i, IMAGEINFO *pImageInfo)
1903 POINT pt;
1905 if (!is_valid(himl) || (pImageInfo == NULL))
1906 return FALSE;
1907 if ((i < 0) || (i >= himl->cCurImage))
1908 return FALSE;
1910 pImageInfo->hbmImage = himl->hbmImage;
1911 pImageInfo->hbmMask = himl->hbmMask;
1913 imagelist_point_from_index( himl, i, &pt );
1914 pImageInfo->rcImage.top = pt.y;
1915 pImageInfo->rcImage.bottom = pt.y + himl->cy;
1916 pImageInfo->rcImage.left = pt.x;
1917 pImageInfo->rcImage.right = pt.x + himl->cx;
1919 return TRUE;
1923 /*************************************************************************
1924 * ImageList_GetImageRect [COMCTL32.@]
1926 * Retrieves the rectangle of the specified image in an image list.
1928 * PARAMS
1929 * himl [I] handle to image list
1930 * i [I] image index
1931 * lpRect [O] pointer to the image rectangle
1933 * RETURNS
1934 * Success: TRUE
1935 * Failure: FALSE
1937 * NOTES
1938 * This is an UNDOCUMENTED function!!!
1941 BOOL WINAPI
1942 ImageList_GetImageRect (HIMAGELIST himl, INT i, LPRECT lpRect)
1944 POINT pt;
1946 if (!is_valid(himl) || (lpRect == NULL))
1947 return FALSE;
1948 if ((i < 0) || (i >= himl->cCurImage))
1949 return FALSE;
1951 imagelist_point_from_index( himl, i, &pt );
1952 lpRect->left = pt.x;
1953 lpRect->top = pt.y;
1954 lpRect->right = pt.x + himl->cx;
1955 lpRect->bottom = pt.y + himl->cy;
1957 return TRUE;
1961 /*************************************************************************
1962 * ImageList_LoadImage [COMCTL32.@]
1963 * ImageList_LoadImageA [COMCTL32.@]
1965 * Creates an image list from a bitmap, icon or cursor.
1967 * See ImageList_LoadImageW.
1970 HIMAGELIST WINAPI
1971 ImageList_LoadImageA (HINSTANCE hi, LPCSTR lpbmp, INT cx, INT cGrow,
1972 COLORREF clrMask, UINT uType, UINT uFlags)
1974 HIMAGELIST himl;
1975 LPWSTR lpbmpW;
1976 DWORD len;
1978 if (IS_INTRESOURCE(lpbmp))
1979 return ImageList_LoadImageW(hi, (LPCWSTR)lpbmp, cx, cGrow, clrMask,
1980 uType, uFlags);
1982 len = MultiByteToWideChar(CP_ACP, 0, lpbmp, -1, NULL, 0);
1983 lpbmpW = Alloc(len * sizeof(WCHAR));
1984 MultiByteToWideChar(CP_ACP, 0, lpbmp, -1, lpbmpW, len);
1986 himl = ImageList_LoadImageW(hi, lpbmpW, cx, cGrow, clrMask, uType, uFlags);
1987 Free (lpbmpW);
1988 return himl;
1992 /*************************************************************************
1993 * ImageList_LoadImageW [COMCTL32.@]
1995 * Creates an image list from a bitmap, icon or cursor.
1997 * PARAMS
1998 * hi [I] instance handle
1999 * lpbmp [I] name or id of the image
2000 * cx [I] width of each image
2001 * cGrow [I] number of images to expand
2002 * clrMask [I] mask color
2003 * uType [I] type of image to load
2004 * uFlags [I] loading flags
2006 * RETURNS
2007 * Success: handle to the loaded image list
2008 * Failure: NULL
2010 * SEE
2011 * LoadImage ()
2014 HIMAGELIST WINAPI
2015 ImageList_LoadImageW (HINSTANCE hi, LPCWSTR lpbmp, INT cx, INT cGrow,
2016 COLORREF clrMask, UINT uType, UINT uFlags)
2018 HIMAGELIST himl = NULL;
2019 HANDLE handle;
2020 INT nImageCount;
2022 handle = LoadImageW (hi, lpbmp, uType, 0, 0, uFlags);
2023 if (!handle) {
2024 WARN("Couldn't load image\n");
2025 return NULL;
2028 if (uType == IMAGE_BITMAP) {
2029 DIBSECTION dib;
2030 UINT color;
2032 if (GetObjectW (handle, sizeof(dib), &dib) == sizeof(BITMAP)) color = ILC_COLOR;
2033 else color = dib.dsBm.bmBitsPixel;
2035 /* To match windows behavior, if cx is set to zero and
2036 the flag DI_DEFAULTSIZE is specified, cx becomes the
2037 system metric value for icons. If the flag is not specified
2038 the function sets the size to the height of the bitmap */
2039 if (cx == 0)
2041 if (uFlags & DI_DEFAULTSIZE)
2042 cx = GetSystemMetrics (SM_CXICON);
2043 else
2044 cx = dib.dsBm.bmHeight;
2047 nImageCount = dib.dsBm.bmWidth / cx;
2049 if (clrMask != CLR_NONE) color |= ILC_MASK;
2050 himl = ImageList_Create (cx, dib.dsBm.bmHeight, color, nImageCount, cGrow);
2051 if (!himl) {
2052 DeleteObject (handle);
2053 return NULL;
2055 ImageList_AddMasked (himl, handle, clrMask);
2057 else if ((uType == IMAGE_ICON) || (uType == IMAGE_CURSOR)) {
2058 ICONINFO ii;
2059 BITMAP bmp;
2061 GetIconInfo (handle, &ii);
2062 GetObjectW (ii.hbmColor, sizeof(BITMAP), &bmp);
2063 himl = ImageList_Create (bmp.bmWidth, bmp.bmHeight,
2064 ILC_MASK | ILC_COLOR, 1, cGrow);
2065 if (!himl) {
2066 DeleteObject (ii.hbmColor);
2067 DeleteObject (ii.hbmMask);
2068 DeleteObject (handle);
2069 return NULL;
2071 ImageList_Add (himl, ii.hbmColor, ii.hbmMask);
2072 DeleteObject (ii.hbmColor);
2073 DeleteObject (ii.hbmMask);
2076 DeleteObject (handle);
2078 return himl;
2082 /*************************************************************************
2083 * ImageList_Merge [COMCTL32.@]
2085 * Create an image list containing a merged image from two image lists.
2087 * PARAMS
2088 * himl1 [I] handle to first image list
2089 * i1 [I] first image index
2090 * himl2 [I] handle to second image list
2091 * i2 [I] second image index
2092 * dx [I] X offset of the second image relative to the first.
2093 * dy [I] Y offset of the second image relative to the first.
2095 * RETURNS
2096 * Success: The newly created image list. It contains a single image
2097 * consisting of the second image merged with the first.
2098 * Failure: NULL, if either himl1 or himl2 is invalid.
2100 * NOTES
2101 * - The returned image list should be deleted by the caller using
2102 * ImageList_Destroy() when it is no longer required.
2103 * - If either i1 or i2 is not a valid image index, they will be treated
2104 * as blank images.
2106 HIMAGELIST WINAPI
2107 ImageList_Merge (HIMAGELIST himl1, INT i1, HIMAGELIST himl2, INT i2,
2108 INT dx, INT dy)
2110 HIMAGELIST himlDst = NULL;
2111 INT cxDst, cyDst;
2112 INT xOff1, yOff1, xOff2, yOff2;
2113 POINT pt1, pt2;
2114 INT newFlags;
2116 TRACE("(himl1=%p i1=%d himl2=%p i2=%d dx=%d dy=%d)\n", himl1, i1, himl2,
2117 i2, dx, dy);
2119 if (!is_valid(himl1) || !is_valid(himl2))
2120 return NULL;
2122 if (dx > 0) {
2123 cxDst = max (himl1->cx, dx + himl2->cx);
2124 xOff1 = 0;
2125 xOff2 = dx;
2127 else if (dx < 0) {
2128 cxDst = max (himl2->cx, himl1->cx - dx);
2129 xOff1 = -dx;
2130 xOff2 = 0;
2132 else {
2133 cxDst = max (himl1->cx, himl2->cx);
2134 xOff1 = 0;
2135 xOff2 = 0;
2138 if (dy > 0) {
2139 cyDst = max (himl1->cy, dy + himl2->cy);
2140 yOff1 = 0;
2141 yOff2 = dy;
2143 else if (dy < 0) {
2144 cyDst = max (himl2->cy, himl1->cy - dy);
2145 yOff1 = -dy;
2146 yOff2 = 0;
2148 else {
2149 cyDst = max (himl1->cy, himl2->cy);
2150 yOff1 = 0;
2151 yOff2 = 0;
2154 newFlags = (himl1->flags > himl2->flags ? himl1->flags : himl2->flags) & ILC_COLORDDB;
2155 if (newFlags == ILC_COLORDDB && (himl1->flags & ILC_COLORDDB) == ILC_COLOR16)
2156 newFlags = ILC_COLOR16; /* this is what native (at least v5) does, don't know why */
2157 himlDst = ImageList_Create (cxDst, cyDst, ILC_MASK | newFlags, 1, 1);
2159 if (himlDst)
2161 imagelist_point_from_index( himl1, i1, &pt1 );
2162 imagelist_point_from_index( himl2, i2, &pt2 );
2164 /* copy image */
2165 BitBlt (himlDst->hdcImage, 0, 0, cxDst, cyDst, himl1->hdcImage, 0, 0, BLACKNESS);
2166 if (i1 >= 0 && i1 < himl1->cCurImage)
2167 BitBlt (himlDst->hdcImage, xOff1, yOff1, himl1->cx, himl1->cy, himl1->hdcImage, pt1.x, pt1.y, SRCCOPY);
2168 if (i2 >= 0 && i2 < himl2->cCurImage)
2170 if (himl2->flags & ILC_MASK)
2172 BitBlt (himlDst->hdcImage, xOff2, yOff2, himl2->cx, himl2->cy, himl2->hdcMask , pt2.x, pt2.y, SRCAND);
2173 BitBlt (himlDst->hdcImage, xOff2, yOff2, himl2->cx, himl2->cy, himl2->hdcImage, pt2.x, pt2.y, SRCPAINT);
2175 else
2176 BitBlt (himlDst->hdcImage, xOff2, yOff2, himl2->cx, himl2->cy, himl2->hdcImage, pt2.x, pt2.y, SRCCOPY);
2179 /* copy mask */
2180 BitBlt (himlDst->hdcMask, 0, 0, cxDst, cyDst, himl1->hdcMask, 0, 0, WHITENESS);
2181 if (i1 >= 0 && i1 < himl1->cCurImage)
2182 BitBlt (himlDst->hdcMask, xOff1, yOff1, himl1->cx, himl1->cy, himl1->hdcMask, pt1.x, pt1.y, SRCCOPY);
2183 if (i2 >= 0 && i2 < himl2->cCurImage)
2184 BitBlt (himlDst->hdcMask, xOff2, yOff2, himl2->cx, himl2->cy, himl2->hdcMask, pt2.x, pt2.y, SRCAND);
2186 himlDst->cCurImage = 1;
2189 return himlDst;
2193 /* helper for ImageList_Read, see comments below */
2194 static void *read_bitmap(IStream *pstm, BITMAPINFO *bmi)
2196 BITMAPFILEHEADER bmfh;
2197 int bitsperpixel, palspace;
2198 void *bits;
2200 if (FAILED(IStream_Read ( pstm, &bmfh, sizeof(bmfh), NULL)))
2201 return NULL;
2203 if (bmfh.bfType != (('M'<<8)|'B'))
2204 return NULL;
2206 if (FAILED(IStream_Read ( pstm, &bmi->bmiHeader, sizeof(bmi->bmiHeader), NULL)))
2207 return NULL;
2209 if ((bmi->bmiHeader.biSize != sizeof(bmi->bmiHeader)))
2210 return NULL;
2212 TRACE("width %lu, height %lu, planes %u, bpp %u\n",
2213 bmi->bmiHeader.biWidth, bmi->bmiHeader.biHeight,
2214 bmi->bmiHeader.biPlanes, bmi->bmiHeader.biBitCount);
2216 bitsperpixel = bmi->bmiHeader.biPlanes * bmi->bmiHeader.biBitCount;
2217 if (bitsperpixel<=8)
2218 palspace = (1<<bitsperpixel)*sizeof(RGBQUAD);
2219 else
2220 palspace = 0;
2222 bmi->bmiHeader.biSizeImage = get_dib_image_size( bmi );
2224 /* read the palette right after the end of the bitmapinfoheader */
2225 if (palspace && FAILED(IStream_Read(pstm, bmi->bmiColors, palspace, NULL)))
2226 return NULL;
2228 bits = Alloc(bmi->bmiHeader.biSizeImage);
2229 if (!bits) return NULL;
2231 if (FAILED(IStream_Read(pstm, bits, bmi->bmiHeader.biSizeImage, NULL)))
2233 Free(bits);
2234 return NULL;
2236 return bits;
2239 /*************************************************************************
2240 * ImageList_Read [COMCTL32.@]
2242 * Reads an image list from a stream.
2244 * PARAMS
2245 * pstm [I] pointer to a stream
2247 * RETURNS
2248 * Success: handle to image list
2249 * Failure: NULL
2251 * The format is like this:
2252 * ILHEAD ilheadstruct;
2254 * for the color image part:
2255 * BITMAPFILEHEADER bmfh;
2256 * BITMAPINFOHEADER bmih;
2257 * only if it has a palette:
2258 * RGBQUAD rgbs[nr_of_paletted_colors];
2260 * BYTE colorbits[imagesize];
2262 * the following only if the ILC_MASK bit is set in ILHEAD.ilFlags:
2263 * BITMAPFILEHEADER bmfh_mask;
2264 * BITMAPINFOHEADER bmih_mask;
2265 * only if it has a palette (it usually does not):
2266 * RGBQUAD rgbs[nr_of_paletted_colors];
2268 * BYTE maskbits[imagesize];
2270 HIMAGELIST WINAPI ImageList_Read(IStream *pstm)
2272 char image_buf[sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * 256];
2273 char mask_buf[sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * 256];
2274 BITMAPINFO *image_info = (BITMAPINFO *)image_buf;
2275 BITMAPINFO *mask_info = (BITMAPINFO *)mask_buf;
2276 void *image_bits, *mask_bits = NULL;
2277 ILHEAD ilHead;
2278 HIMAGELIST himl;
2279 unsigned int i;
2281 TRACE("%p\n", pstm);
2283 if (FAILED(IStream_Read (pstm, &ilHead, sizeof(ILHEAD), NULL)))
2284 return NULL;
2285 if (ilHead.usMagic != (('L' << 8) | 'I'))
2286 return NULL;
2287 if (ilHead.usVersion != 0x101) /* probably version? */
2288 return NULL;
2290 TRACE("cx %u, cy %u, flags 0x%04x, cCurImage %u, cMaxImage %u\n",
2291 ilHead.cx, ilHead.cy, ilHead.flags, ilHead.cCurImage, ilHead.cMaxImage);
2293 himl = ImageList_Create(ilHead.cx, ilHead.cy, ilHead.flags, ilHead.cMaxImage, ilHead.cGrow);
2294 if (!himl)
2295 return NULL;
2297 if (!(image_bits = read_bitmap(pstm, image_info)))
2299 WARN("failed to read bitmap from stream\n");
2300 return NULL;
2302 if (ilHead.flags & ILC_MASK)
2304 if (!(mask_bits = read_bitmap(pstm, mask_info)))
2306 WARN("failed to read mask bitmap from stream\n");
2307 return NULL;
2310 else mask_info = NULL;
2312 if ((himl->flags & 0xfe) == ILC_COLOR32 && image_info->bmiHeader.biBitCount == 32)
2314 DWORD *ptr = image_bits;
2315 BYTE *mask_ptr = mask_bits;
2316 int stride = himl->cy * image_info->bmiHeader.biWidth;
2318 if (image_info->bmiHeader.biHeight > 0) /* bottom-up */
2320 ptr += image_info->bmiHeader.biHeight * image_info->bmiHeader.biWidth - stride;
2321 mask_ptr += (image_info->bmiHeader.biHeight * image_info->bmiHeader.biWidth - stride) / 8;
2322 stride = -stride;
2323 image_info->bmiHeader.biHeight = himl->cy;
2325 else image_info->bmiHeader.biHeight = -himl->cy;
2327 for (i = 0; i < ilHead.cCurImage; i += TILE_COUNT)
2329 add_dib_bits( himl, i, min( ilHead.cCurImage - i, TILE_COUNT ),
2330 himl->cx, himl->cy, image_info, mask_info, ptr, mask_ptr );
2331 ptr += stride;
2332 mask_ptr += stride / 8;
2335 else
2337 StretchDIBits( himl->hdcImage, 0, 0, image_info->bmiHeader.biWidth, image_info->bmiHeader.biHeight,
2338 0, 0, image_info->bmiHeader.biWidth, image_info->bmiHeader.biHeight,
2339 image_bits, image_info, DIB_RGB_COLORS, SRCCOPY);
2340 if (mask_info)
2341 StretchDIBits( himl->hdcMask, 0, 0, mask_info->bmiHeader.biWidth, mask_info->bmiHeader.biHeight,
2342 0, 0, mask_info->bmiHeader.biWidth, mask_info->bmiHeader.biHeight,
2343 mask_bits, mask_info, DIB_RGB_COLORS, SRCCOPY);
2345 Free( image_bits );
2346 Free( mask_bits );
2348 himl->cCurImage = ilHead.cCurImage;
2349 himl->cMaxImage = ilHead.cMaxImage;
2351 ImageList_SetBkColor(himl,ilHead.bkcolor);
2352 for (i=0;i<4;i++)
2353 ImageList_SetOverlayImage(himl,ilHead.ovls[i],i+1);
2354 return himl;
2358 /*************************************************************************
2359 * ImageList_Remove [COMCTL32.@]
2361 * Removes an image from an image list
2363 * PARAMS
2364 * himl [I] image list handle
2365 * i [I] image index
2367 * RETURNS
2368 * Success: TRUE
2369 * Failure: FALSE
2371 * FIXME: as the image list storage test shows, native comctl32 simply shifts
2372 * images without creating a new bitmap.
2374 BOOL WINAPI
2375 ImageList_Remove (HIMAGELIST himl, INT i)
2377 HBITMAP hbmNewImage, hbmNewMask;
2378 HDC hdcBmp;
2379 SIZE sz;
2381 TRACE("(himl=%p i=%d)\n", himl, i);
2383 if (!is_valid(himl)) {
2384 ERR("Invalid image list handle!\n");
2385 return FALSE;
2388 if ((i < -1) || (i >= himl->cCurImage)) {
2389 TRACE("index out of range! %d\n", i);
2390 return FALSE;
2393 if (i == -1) {
2394 INT nCount;
2396 /* remove all */
2397 if (himl->cCurImage == 0) {
2398 /* remove all on empty ImageList is allowed */
2399 TRACE("remove all on empty ImageList!\n");
2400 return TRUE;
2403 himl->cMaxImage = himl->cGrow;
2404 himl->cCurImage = 0;
2405 for (nCount = 0; nCount < MAX_OVERLAYIMAGE; nCount++)
2406 himl->nOvlIdx[nCount] = -1;
2408 Free( himl->item_flags );
2409 himl->item_flags = Alloc( himl->cMaxImage * sizeof(*himl->item_flags) );
2411 hbmNewImage = ImageList_CreateImage(himl->hdcImage, himl, himl->cMaxImage);
2412 SelectObject (himl->hdcImage, hbmNewImage);
2413 DeleteObject (himl->hbmImage);
2414 himl->hbmImage = hbmNewImage;
2416 if (himl->hbmMask) {
2418 imagelist_get_bitmap_size(himl, himl->cMaxImage, &sz);
2419 hbmNewMask = CreateBitmap (sz.cx, sz.cy, 1, 1, NULL);
2420 SelectObject (himl->hdcMask, hbmNewMask);
2421 DeleteObject (himl->hbmMask);
2422 himl->hbmMask = hbmNewMask;
2425 else {
2426 /* delete one image */
2427 TRACE("Remove single image! %d\n", i);
2429 /* create new bitmap(s) */
2430 TRACE(" - Number of images: %d / %d (Old/New)\n",
2431 himl->cCurImage, himl->cCurImage - 1);
2433 hbmNewImage = ImageList_CreateImage(himl->hdcImage, himl, himl->cMaxImage);
2435 imagelist_get_bitmap_size(himl, himl->cMaxImage, &sz );
2436 if (himl->hbmMask)
2437 hbmNewMask = CreateBitmap (sz.cx, sz.cy, 1, 1, NULL);
2438 else
2439 hbmNewMask = 0; /* Just to keep compiler happy! */
2441 hdcBmp = CreateCompatibleDC (0);
2443 /* copy all images and masks prior to the "removed" image */
2444 if (i > 0) {
2445 TRACE("Pre image copy: Copy %d images\n", i);
2447 SelectObject (hdcBmp, hbmNewImage);
2448 imagelist_copy_images( himl, himl->hdcImage, hdcBmp, 0, i, 0 );
2450 if (himl->hbmMask) {
2451 SelectObject (hdcBmp, hbmNewMask);
2452 imagelist_copy_images( himl, himl->hdcMask, hdcBmp, 0, i, 0 );
2456 /* copy all images and masks behind the removed image */
2457 if (i < himl->cCurImage - 1) {
2458 TRACE("Post image copy!\n");
2460 SelectObject (hdcBmp, hbmNewImage);
2461 imagelist_copy_images( himl, himl->hdcImage, hdcBmp, i + 1,
2462 (himl->cCurImage - i), i );
2464 if (himl->hbmMask) {
2465 SelectObject (hdcBmp, hbmNewMask);
2466 imagelist_copy_images( himl, himl->hdcMask, hdcBmp, i + 1,
2467 (himl->cCurImage - i), i );
2471 DeleteDC (hdcBmp);
2473 /* delete old images and insert new ones */
2474 SelectObject (himl->hdcImage, hbmNewImage);
2475 DeleteObject (himl->hbmImage);
2476 himl->hbmImage = hbmNewImage;
2477 if (himl->hbmMask) {
2478 SelectObject (himl->hdcMask, hbmNewMask);
2479 DeleteObject (himl->hbmMask);
2480 himl->hbmMask = hbmNewMask;
2483 himl->cCurImage--;
2486 return TRUE;
2490 /*************************************************************************
2491 * ImageList_Replace [COMCTL32.@]
2493 * Replaces an image in an image list with a new image.
2495 * PARAMS
2496 * himl [I] handle to image list
2497 * i [I] image index
2498 * hbmImage [I] handle to image bitmap
2499 * hbmMask [I] handle to mask bitmap. Can be NULL.
2501 * RETURNS
2502 * Success: TRUE
2503 * Failure: FALSE
2506 BOOL WINAPI
2507 ImageList_Replace (HIMAGELIST himl, INT i, HBITMAP hbmImage,
2508 HBITMAP hbmMask)
2510 HDC hdcImage;
2511 BITMAP bmp;
2512 POINT pt;
2514 TRACE("%p %d %p %p\n", himl, i, hbmImage, hbmMask);
2516 if (!is_valid(himl)) {
2517 ERR("Invalid image list handle!\n");
2518 return FALSE;
2521 if ((i >= himl->cMaxImage) || (i < 0)) {
2522 ERR("Invalid image index!\n");
2523 return FALSE;
2526 if (!GetObjectW(hbmImage, sizeof(BITMAP), &bmp))
2527 return FALSE;
2529 hdcImage = CreateCompatibleDC (0);
2531 /* Replace Image */
2532 SelectObject (hdcImage, hbmImage);
2534 if (add_with_alpha( himl, hdcImage, i, 1, bmp.bmWidth, bmp.bmHeight, hbmImage, hbmMask ))
2535 goto done;
2537 imagelist_point_from_index(himl, i, &pt);
2538 StretchBlt (himl->hdcImage, pt.x, pt.y, himl->cx, himl->cy,
2539 hdcImage, 0, 0, bmp.bmWidth, bmp.bmHeight, SRCCOPY);
2541 if (himl->hbmMask)
2543 HDC hdcTemp;
2544 HBITMAP hOldBitmapTemp;
2546 hdcTemp = CreateCompatibleDC(0);
2547 hOldBitmapTemp = SelectObject(hdcTemp, hbmMask);
2549 StretchBlt (himl->hdcMask, pt.x, pt.y, himl->cx, himl->cy,
2550 hdcTemp, 0, 0, bmp.bmWidth, bmp.bmHeight, SRCCOPY);
2551 SelectObject(hdcTemp, hOldBitmapTemp);
2552 DeleteDC(hdcTemp);
2554 /* Remove the background from the image
2556 BitBlt (himl->hdcImage, pt.x, pt.y, bmp.bmWidth, bmp.bmHeight,
2557 himl->hdcMask, pt.x, pt.y, 0x220326); /* NOTSRCAND */
2560 done:
2561 DeleteDC (hdcImage);
2563 return TRUE;
2567 /*************************************************************************
2568 * ImageList_ReplaceIcon [COMCTL32.@]
2570 * Replaces an image in an image list using an icon.
2572 * PARAMS
2573 * himl [I] handle to image list
2574 * i [I] image index
2575 * hIcon [I] handle to icon
2577 * RETURNS
2578 * Success: index of the replaced image
2579 * Failure: -1
2582 INT WINAPI
2583 ImageList_ReplaceIcon (HIMAGELIST himl, INT nIndex, HICON hIcon)
2585 HICON hBestFitIcon;
2586 ICONINFO ii;
2587 BITMAP bmp;
2588 BOOL ret;
2589 POINT pt;
2591 TRACE("(%p %d %p)\n", himl, nIndex, hIcon);
2593 if (!is_valid(himl)) {
2594 ERR("invalid image list\n");
2595 return -1;
2597 if ((nIndex >= himl->cMaxImage) || (nIndex < -1)) {
2598 ERR("invalid image index %d / %d\n", nIndex, himl->cMaxImage);
2599 return -1;
2602 hBestFitIcon = CopyImage(
2603 hIcon, IMAGE_ICON,
2604 himl->cx, himl->cy,
2605 LR_COPYFROMRESOURCE);
2606 /* the above will fail if the icon wasn't loaded from a resource, so try
2607 * again without LR_COPYFROMRESOURCE flag */
2608 if (!hBestFitIcon)
2609 hBestFitIcon = CopyImage(
2610 hIcon, IMAGE_ICON,
2611 himl->cx, himl->cy,
2613 if (!hBestFitIcon)
2614 return -1;
2616 if (nIndex == -1) {
2617 if (himl->cCurImage + 1 >= himl->cMaxImage)
2618 IMAGELIST_InternalExpandBitmaps(himl, 1);
2620 nIndex = himl->cCurImage;
2621 himl->cCurImage++;
2624 if ((himl->flags & 0xfe) == ILC_COLOR32 && GetIconInfo (hBestFitIcon, &ii))
2626 HDC hdcImage = CreateCompatibleDC( 0 );
2627 GetObjectW (ii.hbmMask, sizeof(BITMAP), &bmp);
2629 if (!ii.hbmColor)
2631 UINT height = bmp.bmHeight / 2;
2632 HDC hdcMask = CreateCompatibleDC( 0 );
2633 HBITMAP color = CreateBitmap( bmp.bmWidth, height, 1, 1, NULL );
2634 SelectObject( hdcImage, color );
2635 SelectObject( hdcMask, ii.hbmMask );
2636 BitBlt( hdcImage, 0, 0, bmp.bmWidth, height, hdcMask, 0, height, SRCCOPY );
2637 ret = add_with_alpha( himl, hdcImage, nIndex, 1, bmp.bmWidth, height, color, ii.hbmMask );
2638 DeleteDC( hdcMask );
2639 DeleteObject( color );
2641 else ret = add_with_alpha( himl, hdcImage, nIndex, 1, bmp.bmWidth, bmp.bmHeight,
2642 ii.hbmColor, ii.hbmMask );
2644 DeleteDC( hdcImage );
2645 DeleteObject (ii.hbmMask);
2646 if (ii.hbmColor) DeleteObject (ii.hbmColor);
2647 if (ret) goto done;
2650 imagelist_point_from_index(himl, nIndex, &pt);
2652 if (himl->hbmMask)
2654 DrawIconEx( himl->hdcImage, pt.x, pt.y, hBestFitIcon, himl->cx, himl->cy, 0, 0, DI_IMAGE );
2655 PatBlt( himl->hdcMask, pt.x, pt.y, himl->cx, himl->cy, WHITENESS );
2656 DrawIconEx( himl->hdcMask, pt.x, pt.y, hBestFitIcon, himl->cx, himl->cy, 0, 0, DI_MASK );
2658 else
2660 COLORREF color = himl->clrBk != CLR_NONE ? himl->clrBk : comctl32_color.clrWindow;
2661 HBRUSH brush = CreateSolidBrush( GetNearestColor( himl->hdcImage, color ));
2663 SelectObject( himl->hdcImage, brush );
2664 PatBlt( himl->hdcImage, pt.x, pt.y, himl->cx, himl->cy, PATCOPY );
2665 SelectObject( himl->hdcImage, GetStockObject(BLACK_BRUSH) );
2666 DeleteObject( brush );
2667 DrawIconEx( himl->hdcImage, pt.x, pt.y, hBestFitIcon, himl->cx, himl->cy, 0, 0, DI_NORMAL );
2670 done:
2671 DestroyIcon(hBestFitIcon);
2673 TRACE("Insert index = %d, himl->cCurImage = %d\n", nIndex, himl->cCurImage);
2674 return nIndex;
2678 /*************************************************************************
2679 * ImageList_SetBkColor [COMCTL32.@]
2681 * Sets the background color of an image list.
2683 * PARAMS
2684 * himl [I] handle to image list
2685 * clrBk [I] background color
2687 * RETURNS
2688 * Success: previous background color
2689 * Failure: CLR_NONE
2692 COLORREF WINAPI
2693 ImageList_SetBkColor (HIMAGELIST himl, COLORREF clrBk)
2695 COLORREF clrOldBk;
2697 if (!is_valid(himl))
2698 return CLR_NONE;
2700 clrOldBk = himl->clrBk;
2701 himl->clrBk = clrBk;
2702 return clrOldBk;
2706 /*************************************************************************
2707 * ImageList_SetDragCursorImage [COMCTL32.@]
2709 * Combines the specified image with the current drag image
2711 * PARAMS
2712 * himlDrag [I] handle to drag image list
2713 * iDrag [I] drag image index
2714 * dxHotspot [I] X position of the hot spot
2715 * dyHotspot [I] Y position of the hot spot
2717 * RETURNS
2718 * Success: TRUE
2719 * Failure: FALSE
2721 * NOTES
2722 * - The names dxHotspot, dyHotspot are misleading because they have nothing
2723 * to do with a hotspot but are only the offset of the origin of the new
2724 * image relative to the origin of the old image.
2726 * - When this function is called and the drag image is visible, a
2727 * short flickering occurs but this matches the Win9x behavior. It is
2728 * possible to fix the flickering using code like in ImageList_DragMove.
2731 BOOL WINAPI
2732 ImageList_SetDragCursorImage (HIMAGELIST himlDrag, INT iDrag,
2733 INT dxHotspot, INT dyHotspot)
2735 HIMAGELIST himlTemp;
2736 BOOL visible;
2738 if (!is_valid(InternalDrag.himl) || !is_valid(himlDrag))
2739 return FALSE;
2741 TRACE(" dxH=%d dyH=%d nX=%d nY=%d\n",
2742 dxHotspot, dyHotspot, InternalDrag.dxHotspot, InternalDrag.dyHotspot);
2744 visible = InternalDrag.bShow;
2746 himlTemp = ImageList_Merge (InternalDrag.himlNoCursor, 0, himlDrag, iDrag,
2747 dxHotspot, dyHotspot);
2749 if (visible) {
2750 /* hide the drag image */
2751 ImageList_DragShowNolock(FALSE);
2753 if ((InternalDrag.himl->cx != himlTemp->cx) ||
2754 (InternalDrag.himl->cy != himlTemp->cy)) {
2755 /* the size of the drag image changed, invalidate the buffer */
2756 DeleteObject(InternalDrag.hbmBg);
2757 InternalDrag.hbmBg = 0;
2760 if (InternalDrag.himl != InternalDrag.himlNoCursor)
2761 ImageList_Destroy (InternalDrag.himl);
2762 InternalDrag.himl = himlTemp;
2764 if (visible) {
2765 /* show the drag image */
2766 ImageList_DragShowNolock(TRUE);
2769 return TRUE;
2773 /*************************************************************************
2774 * ImageList_SetFilter [COMCTL32.@]
2776 * Sets a filter (or does something completely different)!!???
2777 * It removes 12 Bytes from the stack (3 Parameters).
2779 * PARAMS
2780 * himl [I] SHOULD be a handle to image list
2781 * i [I] COULD be an index?
2782 * dwFilter [I] ???
2784 * RETURNS
2785 * Success: TRUE ???
2786 * Failure: FALSE ???
2788 * BUGS
2789 * This is an UNDOCUMENTED function!!!!
2790 * empty stub.
2793 BOOL WINAPI
2794 ImageList_SetFilter (HIMAGELIST himl, INT i, DWORD dwFilter)
2796 FIXME("%p, %d, %#lx:empty stub!\n", himl, i, dwFilter);
2798 return FALSE;
2802 /*************************************************************************
2803 * ImageList_SetFlags [COMCTL32.@]
2805 * Sets the image list flags.
2807 * PARAMS
2808 * himl [I] Handle to image list
2809 * flags [I] Flags to set
2811 * RETURNS
2812 * Old flags?
2814 * BUGS
2815 * Stub.
2818 DWORD WINAPI
2819 ImageList_SetFlags(HIMAGELIST himl, DWORD flags)
2821 FIXME("(%p %#lx):empty stub\n", himl, flags);
2822 return 0;
2826 /*************************************************************************
2827 * ImageList_SetIconSize [COMCTL32.@]
2829 * Sets the image size of the bitmap and deletes all images.
2831 * PARAMS
2832 * himl [I] handle to image list
2833 * cx [I] image width
2834 * cy [I] image height
2836 * RETURNS
2837 * Success: TRUE
2838 * Failure: FALSE
2841 BOOL WINAPI
2842 ImageList_SetIconSize (HIMAGELIST himl, INT cx, INT cy)
2844 INT nCount;
2845 HBITMAP hbmNew;
2847 if (!is_valid(himl))
2848 return FALSE;
2850 /* remove all images */
2851 himl->cMaxImage = himl->cInitial + 1;
2852 himl->cCurImage = 0;
2853 himl->cx = cx;
2854 himl->cy = cy;
2856 /* initialize overlay mask indices */
2857 for (nCount = 0; nCount < MAX_OVERLAYIMAGE; nCount++)
2858 himl->nOvlIdx[nCount] = -1;
2860 hbmNew = ImageList_CreateImage(himl->hdcImage, himl, himl->cMaxImage);
2861 SelectObject (himl->hdcImage, hbmNew);
2862 DeleteObject (himl->hbmImage);
2863 himl->hbmImage = hbmNew;
2865 if (himl->hbmMask) {
2866 SIZE sz;
2867 imagelist_get_bitmap_size(himl, himl->cMaxImage, &sz);
2868 hbmNew = CreateBitmap (sz.cx, sz.cy, 1, 1, NULL);
2869 SelectObject (himl->hdcMask, hbmNew);
2870 DeleteObject (himl->hbmMask);
2871 himl->hbmMask = hbmNew;
2874 return TRUE;
2878 /*************************************************************************
2879 * ImageList_SetImageCount [COMCTL32.@]
2881 * Resizes an image list to the specified number of images.
2883 * PARAMS
2884 * himl [I] handle to image list
2885 * iImageCount [I] number of images in the image list
2887 * RETURNS
2888 * Success: TRUE
2889 * Failure: FALSE
2892 BOOL WINAPI
2893 ImageList_SetImageCount (HIMAGELIST himl, UINT iImageCount)
2895 HDC hdcBitmap;
2896 HBITMAP hbmNewBitmap, hbmOld;
2897 INT nNewCount, nCopyCount;
2899 TRACE("%p %d\n",himl,iImageCount);
2901 if (!is_valid(himl))
2902 return FALSE;
2904 nNewCount = iImageCount + 1;
2905 nCopyCount = min(himl->cCurImage, iImageCount);
2907 hdcBitmap = CreateCompatibleDC (0);
2909 hbmNewBitmap = ImageList_CreateImage(hdcBitmap, himl, nNewCount);
2911 if (hbmNewBitmap != 0)
2913 hbmOld = SelectObject (hdcBitmap, hbmNewBitmap);
2914 imagelist_copy_images( himl, himl->hdcImage, hdcBitmap, 0, nCopyCount, 0 );
2915 SelectObject (hdcBitmap, hbmOld);
2917 /* FIXME: delete 'empty' image space? */
2919 SelectObject (himl->hdcImage, hbmNewBitmap);
2920 DeleteObject (himl->hbmImage);
2921 himl->hbmImage = hbmNewBitmap;
2923 else
2924 ERR("Could not create new image bitmap!\n");
2926 if (himl->hbmMask)
2928 SIZE sz;
2929 imagelist_get_bitmap_size( himl, nNewCount, &sz );
2930 hbmNewBitmap = CreateBitmap (sz.cx, sz.cy, 1, 1, NULL);
2931 if (hbmNewBitmap != 0)
2933 hbmOld = SelectObject (hdcBitmap, hbmNewBitmap);
2934 imagelist_copy_images( himl, himl->hdcMask, hdcBitmap, 0, nCopyCount, 0 );
2935 SelectObject (hdcBitmap, hbmOld);
2937 /* FIXME: delete 'empty' image space? */
2939 SelectObject (himl->hdcMask, hbmNewBitmap);
2940 DeleteObject (himl->hbmMask);
2941 himl->hbmMask = hbmNewBitmap;
2943 else
2944 ERR("Could not create new mask bitmap!\n");
2947 DeleteDC (hdcBitmap);
2949 himl->item_flags = HeapReAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, himl->item_flags,
2950 nNewCount * sizeof(*himl->item_flags));
2952 /* Update max image count and current image count */
2953 himl->cMaxImage = nNewCount;
2954 himl->cCurImage = iImageCount;
2956 return TRUE;
2960 /*************************************************************************
2961 * ImageList_SetOverlayImage [COMCTL32.@]
2963 * Assigns an overlay mask index to an existing image in an image list.
2965 * PARAMS
2966 * himl [I] handle to image list
2967 * iImage [I] image index
2968 * iOverlay [I] overlay mask index
2970 * RETURNS
2971 * Success: TRUE
2972 * Failure: FALSE
2975 BOOL WINAPI
2976 ImageList_SetOverlayImage (HIMAGELIST himl, INT iImage, INT iOverlay)
2978 if (!is_valid(himl))
2979 return FALSE;
2980 if ((iOverlay < 1) || (iOverlay > MAX_OVERLAYIMAGE))
2981 return FALSE;
2982 if ((iImage!=-1) && ((iImage < 0) || (iImage > himl->cCurImage)))
2983 return FALSE;
2984 himl->nOvlIdx[iOverlay - 1] = iImage;
2985 return TRUE;
2990 /* helper for ImageList_Write - write bitmap to pstm
2991 * currently everything is written as 24 bit RGB, except masks
2993 static BOOL _write_bitmap(HBITMAP hBitmap, IStream *pstm)
2995 LPBITMAPFILEHEADER bmfh;
2996 LPBITMAPINFOHEADER bmih;
2997 LPBYTE data = NULL, lpBits;
2998 BITMAP bm;
2999 INT bitCount, sizeImage, offBits, totalSize;
3000 HDC xdc;
3001 BOOL result = FALSE;
3003 if (!GetObjectW(hBitmap, sizeof(BITMAP), &bm))
3004 return FALSE;
3006 bitCount = bm.bmBitsPixel;
3007 sizeImage = get_dib_stride(bm.bmWidth, bitCount) * bm.bmHeight;
3009 totalSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
3010 if(bitCount <= 8)
3011 totalSize += (1 << bitCount) * sizeof(RGBQUAD);
3012 offBits = totalSize;
3013 totalSize += sizeImage;
3015 data = Alloc(totalSize);
3016 bmfh = (LPBITMAPFILEHEADER)data;
3017 bmih = (LPBITMAPINFOHEADER)(data + sizeof(BITMAPFILEHEADER));
3018 lpBits = data + offBits;
3020 /* setup BITMAPFILEHEADER */
3021 bmfh->bfType = (('M' << 8) | 'B');
3022 bmfh->bfSize = offBits;
3023 bmfh->bfReserved1 = 0;
3024 bmfh->bfReserved2 = 0;
3025 bmfh->bfOffBits = offBits;
3027 /* setup BITMAPINFOHEADER */
3028 bmih->biSize = sizeof(BITMAPINFOHEADER);
3029 bmih->biWidth = bm.bmWidth;
3030 bmih->biHeight = bm.bmHeight;
3031 bmih->biPlanes = 1;
3032 bmih->biBitCount = bitCount;
3033 bmih->biCompression = BI_RGB;
3034 bmih->biSizeImage = sizeImage;
3035 bmih->biXPelsPerMeter = 0;
3036 bmih->biYPelsPerMeter = 0;
3037 bmih->biClrUsed = 0;
3038 bmih->biClrImportant = 0;
3040 xdc = GetDC(0);
3041 result = GetDIBits(xdc, hBitmap, 0, bm.bmHeight, lpBits, (BITMAPINFO *)bmih, DIB_RGB_COLORS) == bm.bmHeight;
3042 ReleaseDC(0, xdc);
3043 if (!result)
3044 goto failed;
3046 TRACE("width %lu, height %lu, planes %u, bpp %u\n",
3047 bmih->biWidth, bmih->biHeight,
3048 bmih->biPlanes, bmih->biBitCount);
3050 if(FAILED(IStream_Write(pstm, data, totalSize, NULL)))
3051 goto failed;
3053 result = TRUE;
3055 failed:
3056 Free(data);
3058 return result;
3061 /*************************************************************************
3062 * ImageList_WriteEx [COMCTL32.@]
3064 HRESULT WINAPI ImageList_WriteEx(HIMAGELIST himl, DWORD flags, IStream *pstm)
3066 FIXME("%p %#lx %p: semi-stub\n", himl, flags, pstm);
3067 return ImageList_Write(himl, pstm) ? S_OK : E_FAIL;
3070 /*************************************************************************
3071 * ImageList_Write [COMCTL32.@]
3073 * Writes an image list to a stream.
3075 * PARAMS
3076 * himl [I] handle to image list
3077 * pstm [O] Pointer to a stream.
3079 * RETURNS
3080 * Success: TRUE
3081 * Failure: FALSE
3083 * BUGS
3084 * probably.
3087 BOOL WINAPI ImageList_Write(HIMAGELIST himl, IStream *pstm)
3089 ILHEAD ilHead;
3090 int i;
3092 TRACE("%p %p\n", himl, pstm);
3094 if (!is_valid(himl))
3095 return FALSE;
3097 ilHead.usMagic = (('L' << 8) | 'I');
3098 ilHead.usVersion = 0x101;
3099 ilHead.cCurImage = himl->cCurImage;
3100 ilHead.cMaxImage = himl->cMaxImage;
3101 ilHead.cGrow = himl->cGrow;
3102 ilHead.cx = himl->cx;
3103 ilHead.cy = himl->cy;
3104 ilHead.bkcolor = himl->clrBk;
3105 ilHead.flags = himl->flags;
3106 for(i = 0; i < 4; i++) {
3107 ilHead.ovls[i] = himl->nOvlIdx[i];
3110 TRACE("cx %u, cy %u, flags 0x04%x, cCurImage %u, cMaxImage %u\n",
3111 ilHead.cx, ilHead.cy, ilHead.flags, ilHead.cCurImage, ilHead.cMaxImage);
3113 if(FAILED(IStream_Write(pstm, &ilHead, sizeof(ILHEAD), NULL)))
3114 return FALSE;
3116 /* write the bitmap */
3117 if(!_write_bitmap(himl->hbmImage, pstm))
3118 return FALSE;
3120 /* write the mask if we have one */
3121 if(himl->flags & ILC_MASK) {
3122 if(!_write_bitmap(himl->hbmMask, pstm))
3123 return FALSE;
3126 return TRUE;
3130 static HBITMAP ImageList_CreateImage(HDC hdc, HIMAGELIST himl, UINT count)
3132 HBITMAP hbmNewBitmap;
3133 UINT ilc = (himl->flags & 0xFE);
3134 SIZE sz;
3136 imagelist_get_bitmap_size( himl, count, &sz );
3138 if ((ilc >= ILC_COLOR4 && ilc <= ILC_COLOR32) || ilc == ILC_COLOR)
3140 char buffer[sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD)];
3141 BITMAPINFO *bmi = (BITMAPINFO *)buffer;
3143 TRACE("Creating DIBSection %ld x %ld, %d Bits per Pixel\n",
3144 sz.cx, sz.cy, himl->uBitsPixel);
3146 memset( buffer, 0, sizeof(buffer) );
3147 bmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
3148 bmi->bmiHeader.biWidth = sz.cx;
3149 bmi->bmiHeader.biHeight = sz.cy;
3150 bmi->bmiHeader.biPlanes = 1;
3151 bmi->bmiHeader.biBitCount = himl->uBitsPixel;
3152 bmi->bmiHeader.biCompression = BI_RGB;
3154 if (himl->uBitsPixel <= ILC_COLOR8)
3156 if (!himl->color_table_set)
3158 /* retrieve the default color map */
3159 HBITMAP tmp = CreateBitmap( 1, 1, 1, 1, NULL );
3160 GetDIBits( hdc, tmp, 0, 0, NULL, bmi, DIB_RGB_COLORS );
3161 DeleteObject( tmp );
3162 if (ilc == ILC_COLOR4)
3164 RGBQUAD tmp;
3165 tmp = bmi->bmiColors[7];
3166 bmi->bmiColors[7] = bmi->bmiColors[8];
3167 bmi->bmiColors[8] = tmp;
3170 else
3172 GetDIBColorTable(himl->hdcImage, 0, 1 << himl->uBitsPixel, bmi->bmiColors);
3175 hbmNewBitmap = CreateDIBSection(hdc, bmi, DIB_RGB_COLORS, NULL, 0, 0);
3177 else /*if (ilc == ILC_COLORDDB)*/
3179 TRACE("Creating Bitmap: %d Bits per Pixel\n", himl->uBitsPixel);
3181 hbmNewBitmap = CreateBitmap (sz.cx, sz.cy, 1, himl->uBitsPixel, NULL);
3183 TRACE("returning %p\n", hbmNewBitmap);
3184 return hbmNewBitmap;
3187 /*************************************************************************
3188 * ImageList_SetColorTable [COMCTL32.@]
3190 * Sets the color table of an image list.
3192 * PARAMS
3193 * himl [I] Handle to the image list.
3194 * uStartIndex [I] The first index to set.
3195 * cEntries [I] Number of entries to set.
3196 * prgb [I] New color information for color table for the image list.
3198 * RETURNS
3199 * Success: Number of entries in the table that were set.
3200 * Failure: Zero.
3202 * SEE
3203 * ImageList_Create(), SetDIBColorTable()
3206 UINT WINAPI
3207 ImageList_SetColorTable(HIMAGELIST himl, UINT uStartIndex, UINT cEntries, const RGBQUAD *prgb)
3209 TRACE("(%p, %d, %d, %p)\n", himl, uStartIndex, cEntries, prgb);
3210 himl->color_table_set = TRUE;
3211 return SetDIBColorTable(himl->hdcImage, uStartIndex, cEntries, prgb);
3214 /*************************************************************************
3215 * ImageList_CoCreateInstance [COMCTL32.@]
3217 * Creates a new imagelist instance and returns an interface pointer to it.
3219 * PARAMS
3220 * rclsid [I] A reference to the CLSID (CLSID_ImageList).
3221 * punkOuter [I] Pointer to IUnknown interface for aggregation, if desired
3222 * riid [I] Identifier of the requested interface.
3223 * ppv [O] Returns the address of the pointer requested, or NULL.
3225 * RETURNS
3226 * Success: S_OK.
3227 * Failure: Error value.
3229 HRESULT WINAPI
3230 ImageList_CoCreateInstance (REFCLSID rclsid, const IUnknown *punkOuter, REFIID riid, void **ppv)
3232 TRACE("(%s,%p,%s,%p)\n", debugstr_guid(rclsid), punkOuter, debugstr_guid(riid), ppv);
3234 if (!IsEqualCLSID(&CLSID_ImageList, rclsid))
3235 return E_NOINTERFACE;
3237 return ImageListImpl_CreateInstance(punkOuter, riid, ppv);
3241 /*************************************************************************
3242 * IImageList implementation
3245 static HRESULT WINAPI ImageListImpl_QueryInterface(IImageList2 *iface,
3246 REFIID iid, void **ppv)
3248 HIMAGELIST imgl = impl_from_IImageList2(iface);
3250 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
3252 if (!ppv) return E_INVALIDARG;
3254 if (IsEqualIID(&IID_IUnknown, iid) ||
3255 IsEqualIID(&IID_IImageList, iid) ||
3256 IsEqualIID(&IID_IImageList2, iid))
3258 *ppv = &imgl->IImageList2_iface;
3260 else
3262 *ppv = NULL;
3263 return E_NOINTERFACE;
3266 IImageList2_AddRef(iface);
3267 return S_OK;
3270 static ULONG WINAPI ImageListImpl_AddRef(IImageList2 *iface)
3272 HIMAGELIST imgl = impl_from_IImageList2(iface);
3273 ULONG ref = InterlockedIncrement(&imgl->ref);
3275 TRACE("%p, refcount %lu\n", iface, ref);
3276 return ref;
3279 static ULONG WINAPI ImageListImpl_Release(IImageList2 *iface)
3281 HIMAGELIST This = impl_from_IImageList2(iface);
3282 ULONG ref = InterlockedDecrement(&This->ref);
3284 TRACE("%p, refcount %lu\n", iface, ref);
3286 if (ref == 0)
3288 /* delete image bitmaps */
3289 if (This->hbmImage) DeleteObject (This->hbmImage);
3290 if (This->hbmMask) DeleteObject (This->hbmMask);
3292 /* delete image & mask DCs */
3293 if (This->hdcImage) DeleteDC (This->hdcImage);
3294 if (This->hdcMask) DeleteDC (This->hdcMask);
3296 /* delete blending brushes */
3297 if (This->hbrBlend25) DeleteObject (This->hbrBlend25);
3298 if (This->hbrBlend50) DeleteObject (This->hbrBlend50);
3300 This->IImageList2_iface.lpVtbl = NULL;
3301 Free(This->item_flags);
3302 Free(This);
3305 return ref;
3308 static HRESULT WINAPI ImageListImpl_Add(IImageList2 *iface, HBITMAP hbmImage,
3309 HBITMAP hbmMask, int *pi)
3311 HIMAGELIST imgl = impl_from_IImageList2(iface);
3312 int ret;
3314 if (!pi)
3315 return E_FAIL;
3317 ret = ImageList_Add(imgl, hbmImage, hbmMask);
3319 if (ret == -1)
3320 return E_FAIL;
3322 *pi = ret;
3323 return S_OK;
3326 static HRESULT WINAPI ImageListImpl_ReplaceIcon(IImageList2 *iface, int i,
3327 HICON hicon, int *pi)
3329 HIMAGELIST imgl = impl_from_IImageList2(iface);
3330 int ret;
3332 if (!pi)
3333 return E_FAIL;
3335 ret = ImageList_ReplaceIcon(imgl, i, hicon);
3337 if (ret == -1)
3338 return E_FAIL;
3340 *pi = ret;
3341 return S_OK;
3344 static HRESULT WINAPI ImageListImpl_SetOverlayImage(IImageList2 *iface,
3345 int iImage, int iOverlay)
3347 HIMAGELIST imgl = impl_from_IImageList2(iface);
3348 return ImageList_SetOverlayImage(imgl, iImage, iOverlay) ? S_OK : E_FAIL;
3351 static HRESULT WINAPI ImageListImpl_Replace(IImageList2 *iface, int i,
3352 HBITMAP hbmImage, HBITMAP hbmMask)
3354 HIMAGELIST imgl = impl_from_IImageList2(iface);
3355 return ImageList_Replace(imgl, i, hbmImage, hbmMask) ? S_OK : E_FAIL;
3358 static HRESULT WINAPI ImageListImpl_AddMasked(IImageList2 *iface, HBITMAP hbmImage,
3359 COLORREF crMask, int *pi)
3361 HIMAGELIST imgl = impl_from_IImageList2(iface);
3362 int ret;
3364 if (!pi)
3365 return E_FAIL;
3367 ret = ImageList_AddMasked(imgl, hbmImage, crMask);
3369 if (ret == -1)
3370 return E_FAIL;
3372 *pi = ret;
3373 return S_OK;
3376 static HRESULT WINAPI ImageListImpl_Draw(IImageList2 *iface,
3377 IMAGELISTDRAWPARAMS *pimldp)
3379 HIMAGELIST imgl = impl_from_IImageList2(iface);
3380 HIMAGELIST old_himl;
3381 int ret;
3383 /* As far as I can tell, Windows simply ignores the contents of pimldp->himl
3384 so we shall simulate the same */
3385 old_himl = pimldp->himl;
3386 pimldp->himl = imgl;
3388 ret = ImageList_DrawIndirect(pimldp);
3390 pimldp->himl = old_himl;
3391 return ret ? S_OK : E_INVALIDARG;
3394 static HRESULT WINAPI ImageListImpl_Remove(IImageList2 *iface, int i)
3396 HIMAGELIST imgl = impl_from_IImageList2(iface);
3397 return (ImageList_Remove(imgl, i) == 0) ? E_INVALIDARG : S_OK;
3400 static HRESULT WINAPI ImageListImpl_GetIcon(IImageList2 *iface, int i, UINT flags,
3401 HICON *picon)
3403 HIMAGELIST imgl = impl_from_IImageList2(iface);
3404 HICON hIcon;
3406 if (!picon)
3407 return E_FAIL;
3409 hIcon = ImageList_GetIcon(imgl, i, flags);
3411 if (hIcon == NULL)
3412 return E_FAIL;
3414 *picon = hIcon;
3415 return S_OK;
3418 static HRESULT WINAPI ImageListImpl_GetImageInfo(IImageList2 *iface, int i,
3419 IMAGEINFO *pImageInfo)
3421 HIMAGELIST imgl = impl_from_IImageList2(iface);
3422 return ImageList_GetImageInfo(imgl, i, pImageInfo) ? S_OK : E_FAIL;
3425 static HRESULT WINAPI ImageListImpl_Copy(IImageList2 *iface, int dst_index,
3426 IUnknown *unk_src, int src_index, UINT flags)
3428 HIMAGELIST imgl = impl_from_IImageList2(iface);
3429 IImageList *src = NULL;
3430 HRESULT ret;
3432 if (!unk_src)
3433 return E_FAIL;
3435 /* TODO: Add test for IID_ImageList2 too */
3436 if (FAILED(IUnknown_QueryInterface(unk_src, &IID_IImageList,
3437 (void **) &src)))
3438 return E_FAIL;
3440 if (ImageList_Copy(imgl, dst_index, (HIMAGELIST) src, src_index, flags))
3441 ret = S_OK;
3442 else
3443 ret = E_FAIL;
3445 IImageList_Release(src);
3446 return ret;
3449 static HRESULT WINAPI ImageListImpl_Merge(IImageList2 *iface, int i1,
3450 IUnknown *punk2, int i2, int dx, int dy, REFIID riid, void **ppv)
3452 HIMAGELIST imgl = impl_from_IImageList2(iface);
3453 IImageList *iml2 = NULL;
3454 HIMAGELIST merged;
3455 HRESULT ret = E_FAIL;
3457 TRACE("(%p)->(%d %p %d %d %d %s %p)\n", iface, i1, punk2, i2, dx, dy, debugstr_guid(riid), ppv);
3459 /* TODO: Add test for IID_ImageList2 too */
3460 if (FAILED(IUnknown_QueryInterface(punk2, &IID_IImageList,
3461 (void **) &iml2)))
3462 return E_FAIL;
3464 merged = ImageList_Merge(imgl, i1, (HIMAGELIST) iml2, i2, dx, dy);
3466 /* Get the interface for the new image list */
3467 if (merged)
3469 ret = HIMAGELIST_QueryInterface(merged, riid, ppv);
3470 ImageList_Destroy(merged);
3473 IImageList_Release(iml2);
3474 return ret;
3477 static HRESULT WINAPI ImageListImpl_Clone(IImageList2 *iface, REFIID riid, void **ppv)
3479 HIMAGELIST imgl = impl_from_IImageList2(iface);
3480 HIMAGELIST clone;
3481 HRESULT ret = E_FAIL;
3483 TRACE("(%p)->(%s %p)\n", iface, debugstr_guid(riid), ppv);
3485 clone = ImageList_Duplicate(imgl);
3487 /* Get the interface for the new image list */
3488 if (clone)
3490 ret = HIMAGELIST_QueryInterface(clone, riid, ppv);
3491 ImageList_Destroy(clone);
3494 return ret;
3497 static HRESULT WINAPI ImageListImpl_GetImageRect(IImageList2 *iface, int i,
3498 RECT *prc)
3500 HIMAGELIST imgl = impl_from_IImageList2(iface);
3501 IMAGEINFO info;
3503 if (!prc)
3504 return E_FAIL;
3506 if (!ImageList_GetImageInfo(imgl, i, &info))
3507 return E_FAIL;
3509 *prc = info.rcImage;
3511 return S_OK;
3514 static HRESULT WINAPI ImageListImpl_GetIconSize(IImageList2 *iface, int *cx,
3515 int *cy)
3517 HIMAGELIST imgl = impl_from_IImageList2(iface);
3518 return ImageList_GetIconSize(imgl, cx, cy) ? S_OK : E_INVALIDARG;
3521 static HRESULT WINAPI ImageListImpl_SetIconSize(IImageList2 *iface, int cx,
3522 int cy)
3524 HIMAGELIST imgl = impl_from_IImageList2(iface);
3525 return ImageList_SetIconSize(imgl, cx, cy) ? S_OK : E_FAIL;
3528 static HRESULT WINAPI ImageListImpl_GetImageCount(IImageList2 *iface, int *pi)
3530 HIMAGELIST imgl = impl_from_IImageList2(iface);
3531 *pi = ImageList_GetImageCount(imgl);
3532 return S_OK;
3535 static HRESULT WINAPI ImageListImpl_SetImageCount(IImageList2 *iface, UINT count)
3537 HIMAGELIST imgl = impl_from_IImageList2(iface);
3538 return ImageList_SetImageCount(imgl, count) ? S_OK : E_FAIL;
3541 static HRESULT WINAPI ImageListImpl_SetBkColor(IImageList2 *iface, COLORREF clrBk,
3542 COLORREF *pclr)
3544 HIMAGELIST imgl = impl_from_IImageList2(iface);
3545 *pclr = ImageList_SetBkColor(imgl, clrBk);
3546 return S_OK;
3549 static HRESULT WINAPI ImageListImpl_GetBkColor(IImageList2 *iface, COLORREF *pclr)
3551 HIMAGELIST imgl = impl_from_IImageList2(iface);
3552 *pclr = ImageList_GetBkColor(imgl);
3553 return S_OK;
3556 static HRESULT WINAPI ImageListImpl_BeginDrag(IImageList2 *iface, int iTrack,
3557 int dxHotspot, int dyHotspot)
3559 HIMAGELIST imgl = impl_from_IImageList2(iface);
3560 return ImageList_BeginDrag(imgl, iTrack, dxHotspot, dyHotspot) ? S_OK : E_FAIL;
3563 static HRESULT WINAPI ImageListImpl_EndDrag(IImageList2 *iface)
3565 ImageList_EndDrag();
3566 return S_OK;
3569 static HRESULT WINAPI ImageListImpl_DragEnter(IImageList2 *iface, HWND hwndLock,
3570 int x, int y)
3572 return ImageList_DragEnter(hwndLock, x, y) ? S_OK : E_FAIL;
3575 static HRESULT WINAPI ImageListImpl_DragLeave(IImageList2 *iface, HWND hwndLock)
3577 return ImageList_DragLeave(hwndLock) ? S_OK : E_FAIL;
3580 static HRESULT WINAPI ImageListImpl_DragMove(IImageList2 *iface, int x, int y)
3582 return ImageList_DragMove(x, y) ? S_OK : E_FAIL;
3585 static HRESULT WINAPI ImageListImpl_SetDragCursorImage(IImageList2 *iface,
3586 IUnknown *punk, int iDrag, int dxHotspot, int dyHotspot)
3588 IImageList *iml2 = NULL;
3589 BOOL ret;
3591 if (!punk)
3592 return E_FAIL;
3594 /* TODO: Add test for IID_ImageList2 too */
3595 if (FAILED(IUnknown_QueryInterface(punk, &IID_IImageList,
3596 (void **) &iml2)))
3597 return E_FAIL;
3599 ret = ImageList_SetDragCursorImage((HIMAGELIST) iml2, iDrag, dxHotspot,
3600 dyHotspot);
3602 IImageList_Release(iml2);
3604 return ret ? S_OK : E_FAIL;
3607 static HRESULT WINAPI ImageListImpl_DragShowNolock(IImageList2 *iface, BOOL fShow)
3609 return ImageList_DragShowNolock(fShow) ? S_OK : E_FAIL;
3612 static HRESULT WINAPI ImageListImpl_GetDragImage(IImageList2 *iface, POINT *ppt,
3613 POINT *pptHotspot, REFIID riid, PVOID *ppv)
3615 HRESULT ret = E_FAIL;
3616 HIMAGELIST hNew;
3618 if (!ppv)
3619 return E_FAIL;
3621 hNew = ImageList_GetDragImage(ppt, pptHotspot);
3623 /* Get the interface for the new image list */
3624 if (hNew)
3626 IImageList *idrag = (IImageList*)hNew;
3628 ret = HIMAGELIST_QueryInterface(hNew, riid, ppv);
3629 IImageList_Release(idrag);
3632 return ret;
3635 static HRESULT WINAPI ImageListImpl_GetItemFlags(IImageList2 *iface, int i, DWORD *flags)
3637 HIMAGELIST This = impl_from_IImageList2(iface);
3639 if (i < 0 || i >= This->cCurImage)
3640 return E_INVALIDARG;
3642 *flags = This->item_flags[i];
3644 return S_OK;
3647 static HRESULT WINAPI ImageListImpl_GetOverlayImage(IImageList2 *iface, int iOverlay,
3648 int *piIndex)
3650 HIMAGELIST This = impl_from_IImageList2(iface);
3651 int i;
3653 if ((iOverlay < 0) || (iOverlay > This->cCurImage))
3654 return E_FAIL;
3656 for (i = 0; i < MAX_OVERLAYIMAGE; i++)
3658 if (This->nOvlIdx[i] == iOverlay)
3660 *piIndex = i + 1;
3661 return S_OK;
3665 return E_FAIL;
3668 static HRESULT WINAPI ImageListImpl_Resize(IImageList2 *iface, INT cx, INT cy)
3670 FIXME("(%p)->(%d %d): stub\n", iface, cx, cy);
3671 return E_NOTIMPL;
3674 static HRESULT WINAPI ImageListImpl_GetOriginalSize(IImageList2 *iface, INT image, DWORD flags, INT *cx, INT *cy)
3676 FIXME("(%p)->(%d %lx %p %p): stub\n", iface, image, flags, cx, cy);
3677 return E_NOTIMPL;
3680 static HRESULT WINAPI ImageListImpl_SetOriginalSize(IImageList2 *iface, INT image, INT cx, INT cy)
3682 FIXME("(%p)->(%d %d %d): stub\n", iface, image, cx, cy);
3683 return E_NOTIMPL;
3686 static HRESULT WINAPI ImageListImpl_SetCallback(IImageList2 *iface, IUnknown *callback)
3688 FIXME("(%p)->(%p): stub\n", iface, callback);
3689 return E_NOTIMPL;
3692 static HRESULT WINAPI ImageListImpl_GetCallback(IImageList2 *iface, REFIID riid, void **ppv)
3694 FIXME("(%p)->(%s %p): stub\n", iface, debugstr_guid(riid), ppv);
3695 return E_NOTIMPL;
3698 static HRESULT WINAPI ImageListImpl_ForceImagePresent(IImageList2 *iface, INT image, DWORD flags)
3700 FIXME("(%p)->(%d %lx): stub\n", iface, image, flags);
3701 return E_NOTIMPL;
3704 static HRESULT WINAPI ImageListImpl_DiscardImages(IImageList2 *iface, INT first_image, INT last_image, DWORD flags)
3706 FIXME("(%p)->(%d %d %lx): stub\n", iface, first_image, last_image, flags);
3707 return E_NOTIMPL;
3710 static HRESULT WINAPI ImageListImpl_PreloadImages(IImageList2 *iface, IMAGELISTDRAWPARAMS *params)
3712 FIXME("(%p)->(%p): stub\n", iface, params);
3713 return E_NOTIMPL;
3716 static HRESULT WINAPI ImageListImpl_GetStatistics(IImageList2 *iface, IMAGELISTSTATS *stats)
3718 FIXME("(%p)->(%p): stub\n", iface, stats);
3719 return E_NOTIMPL;
3722 static HRESULT WINAPI ImageListImpl_Initialize(IImageList2 *iface, INT cx, INT cy, UINT flags, INT initial, INT grow)
3724 FIXME("(%p)->(%d %d %d %d %d): stub\n", iface, cx, cy, flags, initial, grow);
3725 return E_NOTIMPL;
3728 static HRESULT WINAPI ImageListImpl_Replace2(IImageList2 *iface, INT i, HBITMAP image, HBITMAP mask, IUnknown *unk, DWORD flags)
3730 FIXME("(%p)->(%d %p %p %p %lx): stub\n", iface, i, image, mask, unk, flags);
3731 return E_NOTIMPL;
3734 static HRESULT WINAPI ImageListImpl_ReplaceFromImageList(IImageList2 *iface, INT i, IImageList *imagelist, INT src,
3735 IUnknown *unk, DWORD flags)
3737 FIXME("(%p)->(%d %p %d %p %lx): stub\n", iface, i, imagelist, src, unk, flags);
3738 return E_NOTIMPL;
3741 static const IImageList2Vtbl ImageListImpl_Vtbl = {
3742 ImageListImpl_QueryInterface,
3743 ImageListImpl_AddRef,
3744 ImageListImpl_Release,
3745 ImageListImpl_Add,
3746 ImageListImpl_ReplaceIcon,
3747 ImageListImpl_SetOverlayImage,
3748 ImageListImpl_Replace,
3749 ImageListImpl_AddMasked,
3750 ImageListImpl_Draw,
3751 ImageListImpl_Remove,
3752 ImageListImpl_GetIcon,
3753 ImageListImpl_GetImageInfo,
3754 ImageListImpl_Copy,
3755 ImageListImpl_Merge,
3756 ImageListImpl_Clone,
3757 ImageListImpl_GetImageRect,
3758 ImageListImpl_GetIconSize,
3759 ImageListImpl_SetIconSize,
3760 ImageListImpl_GetImageCount,
3761 ImageListImpl_SetImageCount,
3762 ImageListImpl_SetBkColor,
3763 ImageListImpl_GetBkColor,
3764 ImageListImpl_BeginDrag,
3765 ImageListImpl_EndDrag,
3766 ImageListImpl_DragEnter,
3767 ImageListImpl_DragLeave,
3768 ImageListImpl_DragMove,
3769 ImageListImpl_SetDragCursorImage,
3770 ImageListImpl_DragShowNolock,
3771 ImageListImpl_GetDragImage,
3772 ImageListImpl_GetItemFlags,
3773 ImageListImpl_GetOverlayImage,
3774 ImageListImpl_Resize,
3775 ImageListImpl_GetOriginalSize,
3776 ImageListImpl_SetOriginalSize,
3777 ImageListImpl_SetCallback,
3778 ImageListImpl_GetCallback,
3779 ImageListImpl_ForceImagePresent,
3780 ImageListImpl_DiscardImages,
3781 ImageListImpl_PreloadImages,
3782 ImageListImpl_GetStatistics,
3783 ImageListImpl_Initialize,
3784 ImageListImpl_Replace2,
3785 ImageListImpl_ReplaceFromImageList
3788 static BOOL is_valid(HIMAGELIST himl)
3790 BOOL valid;
3791 __TRY
3793 valid = himl && himl->IImageList2_iface.lpVtbl == &ImageListImpl_Vtbl;
3795 __EXCEPT_PAGE_FAULT
3797 valid = FALSE;
3799 __ENDTRY
3800 return valid;
3803 /*************************************************************************
3804 * HIMAGELIST_QueryInterface [COMCTL32.@]
3806 * Returns a pointer to an IImageList or IImageList2 object for the given
3807 * HIMAGELIST.
3809 * PARAMS
3810 * himl [I] Image list handle.
3811 * riid [I] Identifier of the requested interface.
3812 * ppv [O] Returns the address of the pointer requested, or NULL.
3814 * RETURNS
3815 * Success: S_OK.
3816 * Failure: Error value.
3818 HRESULT WINAPI
3819 HIMAGELIST_QueryInterface (HIMAGELIST himl, REFIID riid, void **ppv)
3821 TRACE("(%p,%s,%p)\n", himl, debugstr_guid(riid), ppv);
3822 return IImageList2_QueryInterface((IImageList2 *) himl, riid, ppv);
3825 static HRESULT ImageListImpl_CreateInstance(const IUnknown *pUnkOuter, REFIID iid, void** ppv)
3827 HIMAGELIST This;
3828 HRESULT ret;
3830 TRACE("(%p,%s,%p)\n", pUnkOuter, debugstr_guid(iid), ppv);
3832 *ppv = NULL;
3834 if (pUnkOuter) return CLASS_E_NOAGGREGATION;
3836 This = Alloc(sizeof(struct _IMAGELIST));
3837 if (!This) return E_OUTOFMEMORY;
3839 This->IImageList2_iface.lpVtbl = &ImageListImpl_Vtbl;
3840 This->ref = 1;
3842 ret = IImageList2_QueryInterface(&This->IImageList2_iface, iid, ppv);
3843 IImageList2_Release(&This->IImageList2_iface);
3845 return ret;