dinput: Implement device creation using product GUID.
[wine/zf.git] / dlls / comctl32 / imagelist.c
blob307ccc0336c819a88edf41d5fb7a4c3da6b4eda4
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"
48 #include "wine/heap.h"
50 WINE_DEFAULT_DEBUG_CHANNEL(imagelist);
52 #define MAX_OVERLAYIMAGE 15
54 struct _IMAGELIST
56 IImageList2 IImageList2_iface; /* 00: IImageList vtable */
57 INT cCurImage; /* 04: ImageCount */
58 INT cMaxImage; /* 08: maximages */
59 INT cGrow; /* 0C: cGrow */
60 INT cx; /* 10: cx */
61 INT cy; /* 14: cy */
62 DWORD x4;
63 UINT flags; /* 1C: flags */
64 COLORREF clrFg; /* 20: foreground color */
65 COLORREF clrBk; /* 24: background color */
68 HBITMAP hbmImage; /* 28: images Bitmap */
69 HBITMAP hbmMask; /* 2C: masks Bitmap */
70 HDC hdcImage; /* 30: images MemDC */
71 HDC hdcMask; /* 34: masks MemDC */
72 INT nOvlIdx[MAX_OVERLAYIMAGE]; /* 38: overlay images index */
74 /* not yet found out */
75 HBRUSH hbrBlend25;
76 HBRUSH hbrBlend50;
77 INT cInitial;
78 UINT uBitsPixel;
79 DWORD *item_flags;
80 BOOL color_table_set;
82 LONG ref; /* reference count */
85 #define IMAGELIST_MAGIC 0x53414D58
87 /* Header used by ImageList_Read() and ImageList_Write() */
88 #include "pshpack2.h"
89 typedef struct _ILHEAD
91 USHORT usMagic;
92 USHORT usVersion;
93 WORD cCurImage;
94 WORD cMaxImage;
95 WORD cGrow;
96 WORD cx;
97 WORD cy;
98 COLORREF bkcolor;
99 WORD flags;
100 SHORT ovls[4];
101 } ILHEAD;
102 #include "poppack.h"
104 /* internal image list data used for Drag & Drop operations */
105 typedef struct
107 HWND hwnd;
108 HIMAGELIST himl;
109 HIMAGELIST himlNoCursor;
110 /* position of the drag image relative to the window */
111 INT x;
112 INT y;
113 /* offset of the hotspot relative to the origin of the image */
114 INT dxHotspot;
115 INT dyHotspot;
116 /* is the drag image visible */
117 BOOL bShow;
118 /* saved background */
119 HBITMAP hbmBg;
120 } INTERNALDRAG;
122 static INTERNALDRAG InternalDrag = { 0, 0, 0, 0, 0, 0, 0, FALSE, 0 };
124 static inline HIMAGELIST impl_from_IImageList2(IImageList2 *iface)
126 return CONTAINING_RECORD(iface, struct _IMAGELIST, IImageList2_iface);
129 static HBITMAP ImageList_CreateImage(HDC hdc, HIMAGELIST himl, UINT count);
130 static HRESULT ImageListImpl_CreateInstance(const IUnknown *pUnkOuter, REFIID iid, void** ppv);
131 static BOOL is_valid(HIMAGELIST himl);
134 * An imagelist with N images is tiled like this:
136 * N/4 ->
138 * 4 048C..
139 * 159D..
140 * | 26AE.N
141 * V 37BF.
144 #define TILE_COUNT 4
146 BOOL imagelist_has_alpha( HIMAGELIST himl, UINT index )
148 return himl->item_flags[index] & ILIF_ALPHA;
151 static inline UINT imagelist_height( UINT count )
153 return ((count + TILE_COUNT - 1)/TILE_COUNT);
156 static inline void imagelist_point_from_index( HIMAGELIST himl, UINT index, LPPOINT pt )
158 pt->x = (index%TILE_COUNT) * himl->cx;
159 pt->y = (index/TILE_COUNT) * himl->cy;
162 static inline void imagelist_get_bitmap_size( HIMAGELIST himl, UINT count, SIZE *sz )
164 sz->cx = himl->cx * TILE_COUNT;
165 sz->cy = imagelist_height( count ) * himl->cy;
168 static inline int get_dib_stride( int width, int bpp )
170 return ((width * bpp + 31) >> 3) & ~3;
173 static inline int get_dib_image_size( const BITMAPINFO *info )
175 return get_dib_stride( info->bmiHeader.biWidth, info->bmiHeader.biBitCount )
176 * abs( info->bmiHeader.biHeight );
180 * imagelist_copy_images()
182 * Copies a block of count images from offset src in the list to offset dest.
183 * Images are copied a row at at time. Assumes hdcSrc and hdcDest are different.
185 static inline void imagelist_copy_images( HIMAGELIST himl, HDC hdcSrc, HDC hdcDest,
186 UINT src, UINT count, UINT dest )
188 POINT ptSrc, ptDest;
189 SIZE sz;
190 UINT i;
192 for ( i=0; i<TILE_COUNT; i++ )
194 imagelist_point_from_index( himl, src+i, &ptSrc );
195 imagelist_point_from_index( himl, dest+i, &ptDest );
196 sz.cx = himl->cx;
197 sz.cy = himl->cy * imagelist_height( count - i );
199 BitBlt( hdcDest, ptDest.x, ptDest.y, sz.cx, sz.cy,
200 hdcSrc, ptSrc.x, ptSrc.y, SRCCOPY );
204 static void add_dib_bits( HIMAGELIST himl, int pos, int count, int width, int height,
205 BITMAPINFO *info, BITMAPINFO *mask_info, DWORD *bits, BYTE *mask_bits )
207 int i, j, n;
208 POINT pt;
209 int stride = info->bmiHeader.biWidth;
210 int mask_stride = (info->bmiHeader.biWidth + 31) / 32 * 4;
212 for (n = 0; n < count; n++)
214 BOOL has_alpha = FALSE;
216 imagelist_point_from_index( himl, pos + n, &pt );
218 /* check if bitmap has an alpha channel */
219 for (i = 0; i < height && !has_alpha; i++)
220 for (j = n * width; j < (n + 1) * width; j++)
221 if ((has_alpha = ((bits[i * stride + j] & 0xff000000) != 0))) break;
223 if (has_alpha)
225 himl->item_flags[pos + n] = ILIF_ALPHA;
227 if (mask_info && himl->hbmMask) /* generate the mask from the alpha channel */
229 for (i = 0; i < height; i++)
230 for (j = n * width; j < (n + 1) * width; j++)
231 if ((bits[i * stride + j] >> 24) > 25) /* more than 10% alpha */
232 mask_bits[i * mask_stride + j / 8] &= ~(0x80 >> (j % 8));
233 else
234 mask_bits[i * mask_stride + j / 8] |= 0x80 >> (j % 8);
237 else if (mask_info) /* mask out the background */
239 for (i = 0; i < height; i++)
240 for (j = n * width; j < (n + 1) * width; j++)
241 if ((mask_bits[i * mask_stride + j / 8] << (j % 8)) & 0x80)
242 bits[i * stride + j] = 0;
244 StretchDIBits( himl->hdcImage, pt.x, pt.y, himl->cx, himl->cy,
245 n * width, 0, width, height, bits, info, DIB_RGB_COLORS, SRCCOPY );
246 if (mask_info)
247 StretchDIBits( himl->hdcMask, pt.x, pt.y, himl->cx, himl->cy,
248 n * width, 0, width, height, mask_bits, mask_info, DIB_RGB_COLORS, SRCCOPY );
252 /* add images with an alpha channel when the image list is 32 bpp */
253 static BOOL add_with_alpha( HIMAGELIST himl, HDC hdc, int pos, int count,
254 int width, int height, HBITMAP hbmImage, HBITMAP hbmMask )
256 BOOL ret = FALSE;
257 BITMAP bm;
258 BITMAPINFO *info, *mask_info = NULL;
259 DWORD *bits = NULL;
260 BYTE *mask_bits = NULL;
261 DWORD mask_width;
263 if (!GetObjectW( hbmImage, sizeof(bm), &bm )) return FALSE;
265 /* if either the imagelist or the source bitmap don't have an alpha channel, bail out now */
266 if ((himl->flags & 0xfe) != ILC_COLOR32) return FALSE;
267 if (bm.bmBitsPixel != 32) return FALSE;
269 SelectObject( hdc, hbmImage );
270 mask_width = (bm.bmWidth + 31) / 32 * 4;
272 if (!(info = heap_alloc( FIELD_OFFSET( BITMAPINFO, bmiColors[256] )))) goto done;
273 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
274 info->bmiHeader.biWidth = bm.bmWidth;
275 info->bmiHeader.biHeight = -height;
276 info->bmiHeader.biPlanes = 1;
277 info->bmiHeader.biBitCount = 32;
278 info->bmiHeader.biCompression = BI_RGB;
279 info->bmiHeader.biSizeImage = bm.bmWidth * height * 4;
280 info->bmiHeader.biXPelsPerMeter = 0;
281 info->bmiHeader.biYPelsPerMeter = 0;
282 info->bmiHeader.biClrUsed = 0;
283 info->bmiHeader.biClrImportant = 0;
284 if (!(bits = heap_alloc( info->bmiHeader.biSizeImage ))) goto done;
285 if (!GetDIBits( hdc, hbmImage, 0, height, bits, info, DIB_RGB_COLORS )) goto done;
287 if (hbmMask)
289 if (!(mask_info = heap_alloc( FIELD_OFFSET( BITMAPINFO, bmiColors[2] ))))
290 goto done;
291 mask_info->bmiHeader = info->bmiHeader;
292 mask_info->bmiHeader.biBitCount = 1;
293 mask_info->bmiHeader.biSizeImage = mask_width * height;
294 if (!(mask_bits = heap_alloc_zero( mask_info->bmiHeader.biSizeImage )))
295 goto done;
296 if (!GetDIBits( hdc, hbmMask, 0, height, mask_bits, mask_info, DIB_RGB_COLORS )) goto done;
299 add_dib_bits( himl, pos, count, width, height, info, mask_info, bits, mask_bits );
300 ret = TRUE;
302 done:
303 heap_free( info );
304 heap_free( mask_info );
305 heap_free( bits );
306 heap_free( mask_bits );
307 return ret;
310 UINT WINAPI
311 ImageList_SetColorTable(HIMAGELIST himl, UINT uStartIndex, UINT cEntries, const RGBQUAD *prgb);
313 /*************************************************************************
314 * IMAGELIST_InternalExpandBitmaps [Internal]
316 * Expands the bitmaps of an image list by the given number of images.
318 * PARAMS
319 * himl [I] handle to image list
320 * nImageCount [I] number of images to add
322 * RETURNS
323 * nothing
325 * NOTES
326 * This function CANNOT be used to reduce the number of images.
328 static void
329 IMAGELIST_InternalExpandBitmaps(HIMAGELIST himl, INT nImageCount)
331 HDC hdcBitmap;
332 HBITMAP hbmNewBitmap, hbmNull;
333 INT nNewCount;
334 SIZE sz;
336 TRACE("%p has allocated %d, max %d, grow %d images\n", himl, himl->cCurImage, himl->cMaxImage, himl->cGrow);
338 if (himl->cCurImage + nImageCount < himl->cMaxImage)
339 return;
341 nNewCount = himl->cMaxImage + max(nImageCount, himl->cGrow) + 1;
343 imagelist_get_bitmap_size(himl, nNewCount, &sz);
345 TRACE("Create expanded bitmaps : himl=%p x=%d y=%d count=%d\n", himl, sz.cx, sz.cy, nNewCount);
346 hdcBitmap = CreateCompatibleDC (0);
348 hbmNewBitmap = ImageList_CreateImage(hdcBitmap, himl, nNewCount);
350 if (hbmNewBitmap == 0)
351 ERR("creating new image bitmap (x=%d y=%d)!\n", sz.cx, sz.cy);
353 if (himl->cCurImage)
355 hbmNull = SelectObject (hdcBitmap, hbmNewBitmap);
356 BitBlt (hdcBitmap, 0, 0, sz.cx, sz.cy,
357 himl->hdcImage, 0, 0, SRCCOPY);
358 SelectObject (hdcBitmap, hbmNull);
360 SelectObject (himl->hdcImage, hbmNewBitmap);
361 DeleteObject (himl->hbmImage);
362 himl->hbmImage = hbmNewBitmap;
364 if (himl->flags & ILC_MASK)
366 hbmNewBitmap = CreateBitmap (sz.cx, sz.cy, 1, 1, NULL);
368 if (hbmNewBitmap == 0)
369 ERR("creating new mask bitmap!\n");
371 if(himl->cCurImage)
373 hbmNull = SelectObject (hdcBitmap, hbmNewBitmap);
374 BitBlt (hdcBitmap, 0, 0, sz.cx, sz.cy,
375 himl->hdcMask, 0, 0, SRCCOPY);
376 SelectObject (hdcBitmap, hbmNull);
378 SelectObject (himl->hdcMask, hbmNewBitmap);
379 DeleteObject (himl->hbmMask);
380 himl->hbmMask = hbmNewBitmap;
383 himl->item_flags = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, himl->item_flags,
384 nNewCount * sizeof(*himl->item_flags));
385 himl->cMaxImage = nNewCount;
387 DeleteDC (hdcBitmap);
391 /*************************************************************************
392 * ImageList_Add [COMCTL32.@]
394 * Add an image or images to an image list.
396 * PARAMS
397 * himl [I] handle to image list
398 * hbmImage [I] handle to image bitmap
399 * hbmMask [I] handle to mask bitmap
401 * RETURNS
402 * Success: Index of the first new image.
403 * Failure: -1
406 INT WINAPI
407 ImageList_Add (HIMAGELIST himl, HBITMAP hbmImage, HBITMAP hbmMask)
409 HDC hdcBitmap, hdcTemp = 0;
410 INT nFirstIndex, nImageCount, i;
411 BITMAP bmp;
412 POINT pt;
414 TRACE("himl=%p hbmimage=%p hbmmask=%p\n", himl, hbmImage, hbmMask);
415 if (!is_valid(himl))
416 return -1;
418 if (!GetObjectW(hbmImage, sizeof(BITMAP), &bmp))
419 return -1;
421 TRACE("himl %p, cCurImage %d, cMaxImage %d, cGrow %d, cx %d, cy %d\n",
422 himl, himl->cCurImage, himl->cMaxImage, himl->cGrow, himl->cx, himl->cy);
424 nImageCount = bmp.bmWidth / himl->cx;
426 TRACE("%p has %d images (%d x %d) bpp %d\n", hbmImage, nImageCount, bmp.bmWidth, bmp.bmHeight,
427 bmp.bmBitsPixel);
429 IMAGELIST_InternalExpandBitmaps(himl, nImageCount);
431 hdcBitmap = CreateCompatibleDC(0);
433 SelectObject(hdcBitmap, hbmImage);
435 if (add_with_alpha( himl, hdcBitmap, himl->cCurImage, nImageCount,
436 himl->cx, min( himl->cy, bmp.bmHeight), hbmImage, hbmMask ))
437 goto done;
439 if (himl->hbmMask)
441 hdcTemp = CreateCompatibleDC(0);
442 SelectObject(hdcTemp, hbmMask);
445 if (himl->uBitsPixel <= 8 && bmp.bmBitsPixel <= 8 &&
446 !himl->color_table_set && himl->cCurImage == 0)
448 RGBQUAD colors[256];
449 UINT num = GetDIBColorTable( hdcBitmap, 0, 1 << bmp.bmBitsPixel, colors );
450 if (num) ImageList_SetColorTable( himl, 0, num, colors );
453 for (i=0; i<nImageCount; i++)
455 imagelist_point_from_index( himl, himl->cCurImage + i, &pt );
457 /* Copy result to the imagelist
459 BitBlt( himl->hdcImage, pt.x, pt.y, himl->cx, bmp.bmHeight,
460 hdcBitmap, i*himl->cx, 0, SRCCOPY );
462 if (!himl->hbmMask)
463 continue;
465 BitBlt( himl->hdcMask, pt.x, pt.y, himl->cx, bmp.bmHeight,
466 hdcTemp, i*himl->cx, 0, SRCCOPY );
468 /* Remove the background from the image
470 BitBlt( himl->hdcImage, pt.x, pt.y, himl->cx, bmp.bmHeight,
471 himl->hdcMask, pt.x, pt.y, 0x220326 ); /* NOTSRCAND */
473 if (hdcTemp) DeleteDC(hdcTemp);
475 done:
476 DeleteDC(hdcBitmap);
478 nFirstIndex = himl->cCurImage;
479 himl->cCurImage += nImageCount;
481 return nFirstIndex;
485 /*************************************************************************
486 * ImageList_AddIcon [COMCTL32.@]
488 * Adds an icon to an image list.
490 * PARAMS
491 * himl [I] handle to image list
492 * hIcon [I] handle to icon
494 * RETURNS
495 * Success: index of the new image
496 * Failure: -1
498 #undef ImageList_AddIcon
499 INT WINAPI ImageList_AddIcon (HIMAGELIST himl, HICON hIcon)
501 return ImageList_ReplaceIcon (himl, -1, hIcon);
505 /*************************************************************************
506 * ImageList_AddMasked [COMCTL32.@]
508 * Adds an image or images to an image list and creates a mask from the
509 * specified bitmap using the mask color.
511 * PARAMS
512 * himl [I] handle to image list.
513 * hBitmap [I] handle to bitmap
514 * clrMask [I] mask color.
516 * RETURNS
517 * Success: Index of the first new image.
518 * Failure: -1
521 INT WINAPI
522 ImageList_AddMasked (HIMAGELIST himl, HBITMAP hBitmap, COLORREF clrMask)
524 HDC hdcMask, hdcBitmap;
525 INT ret;
526 BITMAP bmp;
527 HBITMAP hMaskBitmap;
528 COLORREF bkColor;
530 TRACE("himl=%p hbitmap=%p clrmask=%x\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 = heap_alloc_zero( 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 = heap_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 heap_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 = heap_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 heap_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 %u, height %u, 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 = heap_alloc_zero(bmi->bmiHeader.biSizeImage);
2229 if (!bits) return NULL;
2231 if (FAILED(IStream_Read(pstm, bits, bmi->bmiHeader.biSizeImage, NULL)))
2233 heap_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 heap_free( image_bits );
2346 heap_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 heap_free( himl->item_flags );
2409 himl->item_flags = heap_alloc_zero( 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 0x%x 0x%x):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 %08x):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 = heap_alloc_zero(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 %u, height %u, 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 heap_free(data);
3058 return result;
3062 /*************************************************************************
3063 * ImageList_Write [COMCTL32.@]
3065 * Writes an image list to a stream.
3067 * PARAMS
3068 * himl [I] handle to image list
3069 * pstm [O] Pointer to a stream.
3071 * RETURNS
3072 * Success: TRUE
3073 * Failure: FALSE
3075 * BUGS
3076 * probably.
3079 BOOL WINAPI ImageList_Write(HIMAGELIST himl, IStream *pstm)
3081 ILHEAD ilHead;
3082 int i;
3084 TRACE("%p %p\n", himl, pstm);
3086 if (!is_valid(himl))
3087 return FALSE;
3089 ilHead.usMagic = (('L' << 8) | 'I');
3090 ilHead.usVersion = 0x101;
3091 ilHead.cCurImage = himl->cCurImage;
3092 ilHead.cMaxImage = himl->cMaxImage;
3093 ilHead.cGrow = himl->cGrow;
3094 ilHead.cx = himl->cx;
3095 ilHead.cy = himl->cy;
3096 ilHead.bkcolor = himl->clrBk;
3097 ilHead.flags = himl->flags;
3098 for(i = 0; i < 4; i++) {
3099 ilHead.ovls[i] = himl->nOvlIdx[i];
3102 TRACE("cx %u, cy %u, flags 0x04%x, cCurImage %u, cMaxImage %u\n",
3103 ilHead.cx, ilHead.cy, ilHead.flags, ilHead.cCurImage, ilHead.cMaxImage);
3105 if(FAILED(IStream_Write(pstm, &ilHead, sizeof(ILHEAD), NULL)))
3106 return FALSE;
3108 /* write the bitmap */
3109 if(!_write_bitmap(himl->hbmImage, pstm))
3110 return FALSE;
3112 /* write the mask if we have one */
3113 if(himl->flags & ILC_MASK) {
3114 if(!_write_bitmap(himl->hbmMask, pstm))
3115 return FALSE;
3118 return TRUE;
3122 static HBITMAP ImageList_CreateImage(HDC hdc, HIMAGELIST himl, UINT count)
3124 HBITMAP hbmNewBitmap;
3125 UINT ilc = (himl->flags & 0xFE);
3126 SIZE sz;
3128 imagelist_get_bitmap_size( himl, count, &sz );
3130 if ((ilc >= ILC_COLOR4 && ilc <= ILC_COLOR32) || ilc == ILC_COLOR)
3132 char buffer[sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD)];
3133 BITMAPINFO *bmi = (BITMAPINFO *)buffer;
3135 TRACE("Creating DIBSection %d x %d, %d Bits per Pixel\n",
3136 sz.cx, sz.cy, himl->uBitsPixel);
3138 memset( buffer, 0, sizeof(buffer) );
3139 bmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
3140 bmi->bmiHeader.biWidth = sz.cx;
3141 bmi->bmiHeader.biHeight = sz.cy;
3142 bmi->bmiHeader.biPlanes = 1;
3143 bmi->bmiHeader.biBitCount = himl->uBitsPixel;
3144 bmi->bmiHeader.biCompression = BI_RGB;
3146 if (himl->uBitsPixel <= ILC_COLOR8)
3148 if (!himl->color_table_set)
3150 /* retrieve the default color map */
3151 HBITMAP tmp = CreateBitmap( 1, 1, 1, 1, NULL );
3152 GetDIBits( hdc, tmp, 0, 0, NULL, bmi, DIB_RGB_COLORS );
3153 DeleteObject( tmp );
3154 if (ilc == ILC_COLOR4)
3156 RGBQUAD tmp;
3157 tmp = bmi->bmiColors[7];
3158 bmi->bmiColors[7] = bmi->bmiColors[8];
3159 bmi->bmiColors[8] = tmp;
3162 else
3164 GetDIBColorTable(himl->hdcImage, 0, 1 << himl->uBitsPixel, bmi->bmiColors);
3167 hbmNewBitmap = CreateDIBSection(hdc, bmi, DIB_RGB_COLORS, NULL, 0, 0);
3169 else /*if (ilc == ILC_COLORDDB)*/
3171 TRACE("Creating Bitmap: %d Bits per Pixel\n", himl->uBitsPixel);
3173 hbmNewBitmap = CreateBitmap (sz.cx, sz.cy, 1, himl->uBitsPixel, NULL);
3175 TRACE("returning %p\n", hbmNewBitmap);
3176 return hbmNewBitmap;
3179 /*************************************************************************
3180 * ImageList_SetColorTable [COMCTL32.@]
3182 * Sets the color table of an image list.
3184 * PARAMS
3185 * himl [I] Handle to the image list.
3186 * uStartIndex [I] The first index to set.
3187 * cEntries [I] Number of entries to set.
3188 * prgb [I] New color information for color table for the image list.
3190 * RETURNS
3191 * Success: Number of entries in the table that were set.
3192 * Failure: Zero.
3194 * SEE
3195 * ImageList_Create(), SetDIBColorTable()
3198 UINT WINAPI
3199 ImageList_SetColorTable(HIMAGELIST himl, UINT uStartIndex, UINT cEntries, const RGBQUAD *prgb)
3201 TRACE("(%p, %d, %d, %p)\n", himl, uStartIndex, cEntries, prgb);
3202 himl->color_table_set = TRUE;
3203 return SetDIBColorTable(himl->hdcImage, uStartIndex, cEntries, prgb);
3206 /*************************************************************************
3207 * ImageList_CoCreateInstance [COMCTL32.@]
3209 * Creates a new imagelist instance and returns an interface pointer to it.
3211 * PARAMS
3212 * rclsid [I] A reference to the CLSID (CLSID_ImageList).
3213 * punkOuter [I] Pointer to IUnknown interface for aggregation, if desired
3214 * riid [I] Identifier of the requested interface.
3215 * ppv [O] Returns the address of the pointer requested, or NULL.
3217 * RETURNS
3218 * Success: S_OK.
3219 * Failure: Error value.
3221 HRESULT WINAPI
3222 ImageList_CoCreateInstance (REFCLSID rclsid, const IUnknown *punkOuter, REFIID riid, void **ppv)
3224 TRACE("(%s,%p,%s,%p)\n", debugstr_guid(rclsid), punkOuter, debugstr_guid(riid), ppv);
3226 if (!IsEqualCLSID(&CLSID_ImageList, rclsid))
3227 return E_NOINTERFACE;
3229 return ImageListImpl_CreateInstance(punkOuter, riid, ppv);
3233 /*************************************************************************
3234 * IImageList implementation
3237 static HRESULT WINAPI ImageListImpl_QueryInterface(IImageList2 *iface,
3238 REFIID iid, void **ppv)
3240 HIMAGELIST imgl = impl_from_IImageList2(iface);
3242 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
3244 if (!ppv) return E_INVALIDARG;
3246 if (IsEqualIID(&IID_IUnknown, iid) ||
3247 IsEqualIID(&IID_IImageList, iid) ||
3248 IsEqualIID(&IID_IImageList2, iid))
3250 *ppv = &imgl->IImageList2_iface;
3252 else
3254 *ppv = NULL;
3255 return E_NOINTERFACE;
3258 IImageList2_AddRef(iface);
3259 return S_OK;
3262 static ULONG WINAPI ImageListImpl_AddRef(IImageList2 *iface)
3264 HIMAGELIST imgl = impl_from_IImageList2(iface);
3265 ULONG ref = InterlockedIncrement(&imgl->ref);
3267 TRACE("(%p) refcount=%u\n", iface, ref);
3268 return ref;
3271 static ULONG WINAPI ImageListImpl_Release(IImageList2 *iface)
3273 HIMAGELIST This = impl_from_IImageList2(iface);
3274 ULONG ref = InterlockedDecrement(&This->ref);
3276 TRACE("(%p) refcount=%u\n", iface, ref);
3278 if (ref == 0)
3280 /* delete image bitmaps */
3281 if (This->hbmImage) DeleteObject (This->hbmImage);
3282 if (This->hbmMask) DeleteObject (This->hbmMask);
3284 /* delete image & mask DCs */
3285 if (This->hdcImage) DeleteDC (This->hdcImage);
3286 if (This->hdcMask) DeleteDC (This->hdcMask);
3288 /* delete blending brushes */
3289 if (This->hbrBlend25) DeleteObject (This->hbrBlend25);
3290 if (This->hbrBlend50) DeleteObject (This->hbrBlend50);
3292 This->IImageList2_iface.lpVtbl = NULL;
3293 heap_free(This->item_flags);
3294 heap_free(This);
3297 return ref;
3300 static HRESULT WINAPI ImageListImpl_Add(IImageList2 *iface, HBITMAP hbmImage,
3301 HBITMAP hbmMask, int *pi)
3303 HIMAGELIST imgl = impl_from_IImageList2(iface);
3304 int ret;
3306 if (!pi)
3307 return E_FAIL;
3309 ret = ImageList_Add(imgl, hbmImage, hbmMask);
3311 if (ret == -1)
3312 return E_FAIL;
3314 *pi = ret;
3315 return S_OK;
3318 static HRESULT WINAPI ImageListImpl_ReplaceIcon(IImageList2 *iface, int i,
3319 HICON hicon, int *pi)
3321 HIMAGELIST imgl = impl_from_IImageList2(iface);
3322 int ret;
3324 if (!pi)
3325 return E_FAIL;
3327 ret = ImageList_ReplaceIcon(imgl, i, hicon);
3329 if (ret == -1)
3330 return E_FAIL;
3332 *pi = ret;
3333 return S_OK;
3336 static HRESULT WINAPI ImageListImpl_SetOverlayImage(IImageList2 *iface,
3337 int iImage, int iOverlay)
3339 HIMAGELIST imgl = impl_from_IImageList2(iface);
3340 return ImageList_SetOverlayImage(imgl, iImage, iOverlay) ? S_OK : E_FAIL;
3343 static HRESULT WINAPI ImageListImpl_Replace(IImageList2 *iface, int i,
3344 HBITMAP hbmImage, HBITMAP hbmMask)
3346 HIMAGELIST imgl = impl_from_IImageList2(iface);
3347 return ImageList_Replace(imgl, i, hbmImage, hbmMask) ? S_OK : E_FAIL;
3350 static HRESULT WINAPI ImageListImpl_AddMasked(IImageList2 *iface, HBITMAP hbmImage,
3351 COLORREF crMask, int *pi)
3353 HIMAGELIST imgl = impl_from_IImageList2(iface);
3354 int ret;
3356 if (!pi)
3357 return E_FAIL;
3359 ret = ImageList_AddMasked(imgl, hbmImage, crMask);
3361 if (ret == -1)
3362 return E_FAIL;
3364 *pi = ret;
3365 return S_OK;
3368 static HRESULT WINAPI ImageListImpl_Draw(IImageList2 *iface,
3369 IMAGELISTDRAWPARAMS *pimldp)
3371 HIMAGELIST imgl = impl_from_IImageList2(iface);
3372 HIMAGELIST old_himl;
3373 int ret;
3375 /* As far as I can tell, Windows simply ignores the contents of pimldp->himl
3376 so we shall simulate the same */
3377 old_himl = pimldp->himl;
3378 pimldp->himl = imgl;
3380 ret = ImageList_DrawIndirect(pimldp);
3382 pimldp->himl = old_himl;
3383 return ret ? S_OK : E_INVALIDARG;
3386 static HRESULT WINAPI ImageListImpl_Remove(IImageList2 *iface, int i)
3388 HIMAGELIST imgl = impl_from_IImageList2(iface);
3389 return (ImageList_Remove(imgl, i) == 0) ? E_INVALIDARG : S_OK;
3392 static HRESULT WINAPI ImageListImpl_GetIcon(IImageList2 *iface, int i, UINT flags,
3393 HICON *picon)
3395 HIMAGELIST imgl = impl_from_IImageList2(iface);
3396 HICON hIcon;
3398 if (!picon)
3399 return E_FAIL;
3401 hIcon = ImageList_GetIcon(imgl, i, flags);
3403 if (hIcon == NULL)
3404 return E_FAIL;
3406 *picon = hIcon;
3407 return S_OK;
3410 static HRESULT WINAPI ImageListImpl_GetImageInfo(IImageList2 *iface, int i,
3411 IMAGEINFO *pImageInfo)
3413 HIMAGELIST imgl = impl_from_IImageList2(iface);
3414 return ImageList_GetImageInfo(imgl, i, pImageInfo) ? S_OK : E_FAIL;
3417 static HRESULT WINAPI ImageListImpl_Copy(IImageList2 *iface, int dst_index,
3418 IUnknown *unk_src, int src_index, UINT flags)
3420 HIMAGELIST imgl = impl_from_IImageList2(iface);
3421 IImageList *src = NULL;
3422 HRESULT ret;
3424 if (!unk_src)
3425 return E_FAIL;
3427 /* TODO: Add test for IID_ImageList2 too */
3428 if (FAILED(IUnknown_QueryInterface(unk_src, &IID_IImageList,
3429 (void **) &src)))
3430 return E_FAIL;
3432 if (ImageList_Copy(imgl, dst_index, (HIMAGELIST) src, src_index, flags))
3433 ret = S_OK;
3434 else
3435 ret = E_FAIL;
3437 IImageList_Release(src);
3438 return ret;
3441 static HRESULT WINAPI ImageListImpl_Merge(IImageList2 *iface, int i1,
3442 IUnknown *punk2, int i2, int dx, int dy, REFIID riid, void **ppv)
3444 HIMAGELIST imgl = impl_from_IImageList2(iface);
3445 IImageList *iml2 = NULL;
3446 HIMAGELIST merged;
3447 HRESULT ret = E_FAIL;
3449 TRACE("(%p)->(%d %p %d %d %d %s %p)\n", iface, i1, punk2, i2, dx, dy, debugstr_guid(riid), ppv);
3451 /* TODO: Add test for IID_ImageList2 too */
3452 if (FAILED(IUnknown_QueryInterface(punk2, &IID_IImageList,
3453 (void **) &iml2)))
3454 return E_FAIL;
3456 merged = ImageList_Merge(imgl, i1, (HIMAGELIST) iml2, i2, dx, dy);
3458 /* Get the interface for the new image list */
3459 if (merged)
3461 ret = HIMAGELIST_QueryInterface(merged, riid, ppv);
3462 ImageList_Destroy(merged);
3465 IImageList_Release(iml2);
3466 return ret;
3469 static HRESULT WINAPI ImageListImpl_Clone(IImageList2 *iface, REFIID riid, void **ppv)
3471 HIMAGELIST imgl = impl_from_IImageList2(iface);
3472 HIMAGELIST clone;
3473 HRESULT ret = E_FAIL;
3475 TRACE("(%p)->(%s %p)\n", iface, debugstr_guid(riid), ppv);
3477 clone = ImageList_Duplicate(imgl);
3479 /* Get the interface for the new image list */
3480 if (clone)
3482 ret = HIMAGELIST_QueryInterface(clone, riid, ppv);
3483 ImageList_Destroy(clone);
3486 return ret;
3489 static HRESULT WINAPI ImageListImpl_GetImageRect(IImageList2 *iface, int i,
3490 RECT *prc)
3492 HIMAGELIST imgl = impl_from_IImageList2(iface);
3493 IMAGEINFO info;
3495 if (!prc)
3496 return E_FAIL;
3498 if (!ImageList_GetImageInfo(imgl, i, &info))
3499 return E_FAIL;
3501 *prc = info.rcImage;
3503 return S_OK;
3506 static HRESULT WINAPI ImageListImpl_GetIconSize(IImageList2 *iface, int *cx,
3507 int *cy)
3509 HIMAGELIST imgl = impl_from_IImageList2(iface);
3510 return ImageList_GetIconSize(imgl, cx, cy) ? S_OK : E_INVALIDARG;
3513 static HRESULT WINAPI ImageListImpl_SetIconSize(IImageList2 *iface, int cx,
3514 int cy)
3516 HIMAGELIST imgl = impl_from_IImageList2(iface);
3517 return ImageList_SetIconSize(imgl, cx, cy) ? S_OK : E_FAIL;
3520 static HRESULT WINAPI ImageListImpl_GetImageCount(IImageList2 *iface, int *pi)
3522 HIMAGELIST imgl = impl_from_IImageList2(iface);
3523 *pi = ImageList_GetImageCount(imgl);
3524 return S_OK;
3527 static HRESULT WINAPI ImageListImpl_SetImageCount(IImageList2 *iface, UINT count)
3529 HIMAGELIST imgl = impl_from_IImageList2(iface);
3530 return ImageList_SetImageCount(imgl, count) ? S_OK : E_FAIL;
3533 static HRESULT WINAPI ImageListImpl_SetBkColor(IImageList2 *iface, COLORREF clrBk,
3534 COLORREF *pclr)
3536 HIMAGELIST imgl = impl_from_IImageList2(iface);
3537 *pclr = ImageList_SetBkColor(imgl, clrBk);
3538 return S_OK;
3541 static HRESULT WINAPI ImageListImpl_GetBkColor(IImageList2 *iface, COLORREF *pclr)
3543 HIMAGELIST imgl = impl_from_IImageList2(iface);
3544 *pclr = ImageList_GetBkColor(imgl);
3545 return S_OK;
3548 static HRESULT WINAPI ImageListImpl_BeginDrag(IImageList2 *iface, int iTrack,
3549 int dxHotspot, int dyHotspot)
3551 HIMAGELIST imgl = impl_from_IImageList2(iface);
3552 return ImageList_BeginDrag(imgl, iTrack, dxHotspot, dyHotspot) ? S_OK : E_FAIL;
3555 static HRESULT WINAPI ImageListImpl_EndDrag(IImageList2 *iface)
3557 ImageList_EndDrag();
3558 return S_OK;
3561 static HRESULT WINAPI ImageListImpl_DragEnter(IImageList2 *iface, HWND hwndLock,
3562 int x, int y)
3564 return ImageList_DragEnter(hwndLock, x, y) ? S_OK : E_FAIL;
3567 static HRESULT WINAPI ImageListImpl_DragLeave(IImageList2 *iface, HWND hwndLock)
3569 return ImageList_DragLeave(hwndLock) ? S_OK : E_FAIL;
3572 static HRESULT WINAPI ImageListImpl_DragMove(IImageList2 *iface, int x, int y)
3574 return ImageList_DragMove(x, y) ? S_OK : E_FAIL;
3577 static HRESULT WINAPI ImageListImpl_SetDragCursorImage(IImageList2 *iface,
3578 IUnknown *punk, int iDrag, int dxHotspot, int dyHotspot)
3580 IImageList *iml2 = NULL;
3581 BOOL ret;
3583 if (!punk)
3584 return E_FAIL;
3586 /* TODO: Add test for IID_ImageList2 too */
3587 if (FAILED(IUnknown_QueryInterface(punk, &IID_IImageList,
3588 (void **) &iml2)))
3589 return E_FAIL;
3591 ret = ImageList_SetDragCursorImage((HIMAGELIST) iml2, iDrag, dxHotspot,
3592 dyHotspot);
3594 IImageList_Release(iml2);
3596 return ret ? S_OK : E_FAIL;
3599 static HRESULT WINAPI ImageListImpl_DragShowNolock(IImageList2 *iface, BOOL fShow)
3601 return ImageList_DragShowNolock(fShow) ? S_OK : E_FAIL;
3604 static HRESULT WINAPI ImageListImpl_GetDragImage(IImageList2 *iface, POINT *ppt,
3605 POINT *pptHotspot, REFIID riid, PVOID *ppv)
3607 HRESULT ret = E_FAIL;
3608 HIMAGELIST hNew;
3610 if (!ppv)
3611 return E_FAIL;
3613 hNew = ImageList_GetDragImage(ppt, pptHotspot);
3615 /* Get the interface for the new image list */
3616 if (hNew)
3618 IImageList *idrag = (IImageList*)hNew;
3620 ret = HIMAGELIST_QueryInterface(hNew, riid, ppv);
3621 IImageList_Release(idrag);
3624 return ret;
3627 static HRESULT WINAPI ImageListImpl_GetItemFlags(IImageList2 *iface, int i, DWORD *flags)
3629 HIMAGELIST This = impl_from_IImageList2(iface);
3631 if (i < 0 || i >= This->cCurImage)
3632 return E_INVALIDARG;
3634 *flags = This->item_flags[i];
3636 return S_OK;
3639 static HRESULT WINAPI ImageListImpl_GetOverlayImage(IImageList2 *iface, int iOverlay,
3640 int *piIndex)
3642 HIMAGELIST This = impl_from_IImageList2(iface);
3643 int i;
3645 if ((iOverlay < 0) || (iOverlay > This->cCurImage))
3646 return E_FAIL;
3648 for (i = 0; i < MAX_OVERLAYIMAGE; i++)
3650 if (This->nOvlIdx[i] == iOverlay)
3652 *piIndex = i + 1;
3653 return S_OK;
3657 return E_FAIL;
3660 static HRESULT WINAPI ImageListImpl_Resize(IImageList2 *iface, INT cx, INT cy)
3662 FIXME("(%p)->(%d %d): stub\n", iface, cx, cy);
3663 return E_NOTIMPL;
3666 static HRESULT WINAPI ImageListImpl_GetOriginalSize(IImageList2 *iface, INT image, DWORD flags, INT *cx, INT *cy)
3668 FIXME("(%p)->(%d %x %p %p): stub\n", iface, image, flags, cx, cy);
3669 return E_NOTIMPL;
3672 static HRESULT WINAPI ImageListImpl_SetOriginalSize(IImageList2 *iface, INT image, INT cx, INT cy)
3674 FIXME("(%p)->(%d %d %d): stub\n", iface, image, cx, cy);
3675 return E_NOTIMPL;
3678 static HRESULT WINAPI ImageListImpl_SetCallback(IImageList2 *iface, IUnknown *callback)
3680 FIXME("(%p)->(%p): stub\n", iface, callback);
3681 return E_NOTIMPL;
3684 static HRESULT WINAPI ImageListImpl_GetCallback(IImageList2 *iface, REFIID riid, void **ppv)
3686 FIXME("(%p)->(%s %p): stub\n", iface, debugstr_guid(riid), ppv);
3687 return E_NOTIMPL;
3690 static HRESULT WINAPI ImageListImpl_ForceImagePresent(IImageList2 *iface, INT image, DWORD flags)
3692 FIXME("(%p)->(%d %x): stub\n", iface, image, flags);
3693 return E_NOTIMPL;
3696 static HRESULT WINAPI ImageListImpl_DiscardImages(IImageList2 *iface, INT first_image, INT last_image, DWORD flags)
3698 FIXME("(%p)->(%d %d %x): stub\n", iface, first_image, last_image, flags);
3699 return E_NOTIMPL;
3702 static HRESULT WINAPI ImageListImpl_PreloadImages(IImageList2 *iface, IMAGELISTDRAWPARAMS *params)
3704 FIXME("(%p)->(%p): stub\n", iface, params);
3705 return E_NOTIMPL;
3708 static HRESULT WINAPI ImageListImpl_GetStatistics(IImageList2 *iface, IMAGELISTSTATS *stats)
3710 FIXME("(%p)->(%p): stub\n", iface, stats);
3711 return E_NOTIMPL;
3714 static HRESULT WINAPI ImageListImpl_Initialize(IImageList2 *iface, INT cx, INT cy, UINT flags, INT initial, INT grow)
3716 FIXME("(%p)->(%d %d %d %d %d): stub\n", iface, cx, cy, flags, initial, grow);
3717 return E_NOTIMPL;
3720 static HRESULT WINAPI ImageListImpl_Replace2(IImageList2 *iface, INT i, HBITMAP image, HBITMAP mask, IUnknown *unk, DWORD flags)
3722 FIXME("(%p)->(%d %p %p %p %x): stub\n", iface, i, image, mask, unk, flags);
3723 return E_NOTIMPL;
3726 static HRESULT WINAPI ImageListImpl_ReplaceFromImageList(IImageList2 *iface, INT i, IImageList *imagelist, INT src,
3727 IUnknown *unk, DWORD flags)
3729 FIXME("(%p)->(%d %p %d %p %x): stub\n", iface, i, imagelist, src, unk, flags);
3730 return E_NOTIMPL;
3733 static const IImageList2Vtbl ImageListImpl_Vtbl = {
3734 ImageListImpl_QueryInterface,
3735 ImageListImpl_AddRef,
3736 ImageListImpl_Release,
3737 ImageListImpl_Add,
3738 ImageListImpl_ReplaceIcon,
3739 ImageListImpl_SetOverlayImage,
3740 ImageListImpl_Replace,
3741 ImageListImpl_AddMasked,
3742 ImageListImpl_Draw,
3743 ImageListImpl_Remove,
3744 ImageListImpl_GetIcon,
3745 ImageListImpl_GetImageInfo,
3746 ImageListImpl_Copy,
3747 ImageListImpl_Merge,
3748 ImageListImpl_Clone,
3749 ImageListImpl_GetImageRect,
3750 ImageListImpl_GetIconSize,
3751 ImageListImpl_SetIconSize,
3752 ImageListImpl_GetImageCount,
3753 ImageListImpl_SetImageCount,
3754 ImageListImpl_SetBkColor,
3755 ImageListImpl_GetBkColor,
3756 ImageListImpl_BeginDrag,
3757 ImageListImpl_EndDrag,
3758 ImageListImpl_DragEnter,
3759 ImageListImpl_DragLeave,
3760 ImageListImpl_DragMove,
3761 ImageListImpl_SetDragCursorImage,
3762 ImageListImpl_DragShowNolock,
3763 ImageListImpl_GetDragImage,
3764 ImageListImpl_GetItemFlags,
3765 ImageListImpl_GetOverlayImage,
3766 ImageListImpl_Resize,
3767 ImageListImpl_GetOriginalSize,
3768 ImageListImpl_SetOriginalSize,
3769 ImageListImpl_SetCallback,
3770 ImageListImpl_GetCallback,
3771 ImageListImpl_ForceImagePresent,
3772 ImageListImpl_DiscardImages,
3773 ImageListImpl_PreloadImages,
3774 ImageListImpl_GetStatistics,
3775 ImageListImpl_Initialize,
3776 ImageListImpl_Replace2,
3777 ImageListImpl_ReplaceFromImageList
3780 static BOOL is_valid(HIMAGELIST himl)
3782 BOOL valid;
3783 __TRY
3785 valid = himl && himl->IImageList2_iface.lpVtbl == &ImageListImpl_Vtbl;
3787 __EXCEPT_PAGE_FAULT
3789 valid = FALSE;
3791 __ENDTRY
3792 return valid;
3795 /*************************************************************************
3796 * HIMAGELIST_QueryInterface [COMCTL32.@]
3798 * Returns a pointer to an IImageList or IImageList2 object for the given
3799 * HIMAGELIST.
3801 * PARAMS
3802 * himl [I] Image list handle.
3803 * riid [I] Identifier of the requested interface.
3804 * ppv [O] Returns the address of the pointer requested, or NULL.
3806 * RETURNS
3807 * Success: S_OK.
3808 * Failure: Error value.
3810 HRESULT WINAPI
3811 HIMAGELIST_QueryInterface (HIMAGELIST himl, REFIID riid, void **ppv)
3813 TRACE("(%p,%s,%p)\n", himl, debugstr_guid(riid), ppv);
3814 return IImageList2_QueryInterface((IImageList2 *) himl, riid, ppv);
3817 static HRESULT ImageListImpl_CreateInstance(const IUnknown *pUnkOuter, REFIID iid, void** ppv)
3819 HIMAGELIST This;
3820 HRESULT ret;
3822 TRACE("(%p,%s,%p)\n", pUnkOuter, debugstr_guid(iid), ppv);
3824 *ppv = NULL;
3826 if (pUnkOuter) return CLASS_E_NOAGGREGATION;
3828 This = heap_alloc_zero(sizeof(struct _IMAGELIST));
3829 if (!This) return E_OUTOFMEMORY;
3831 This->IImageList2_iface.lpVtbl = &ImageListImpl_Vtbl;
3832 This->ref = 1;
3834 ret = IImageList2_QueryInterface(&This->IImageList2_iface, iid, ppv);
3835 IImageList2_Release(&This->IImageList2_iface);
3837 return ret;