libport: Remove support for PPC32.
[wine.git] / dlls / comctl32 / imagelist.c
bloba6d9c73c49b21285d5e3b401be8fc30fb3e07411
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 StretchDIBits( himl->hdcImage, pt.x, pt.y, himl->cx, himl->cy,
238 n * width, 0, width, height, bits, info, DIB_RGB_COLORS, SRCCOPY );
239 if (mask_info)
240 StretchDIBits( himl->hdcMask, pt.x, pt.y, himl->cx, himl->cy,
241 n * width, 0, width, height, mask_bits, mask_info, DIB_RGB_COLORS, SRCCOPY );
245 /* add images with an alpha channel when the image list is 32 bpp */
246 static BOOL add_with_alpha( HIMAGELIST himl, HDC hdc, int pos, int count,
247 int width, int height, HBITMAP hbmImage, HBITMAP hbmMask )
249 BOOL ret = FALSE;
250 BITMAP bm;
251 BITMAPINFO *info, *mask_info = NULL;
252 DWORD *bits = NULL;
253 BYTE *mask_bits = NULL;
254 DWORD mask_width;
256 if (!GetObjectW( hbmImage, sizeof(bm), &bm )) return FALSE;
258 /* if either the imagelist or the source bitmap don't have an alpha channel, bail out now */
259 if ((himl->flags & 0xfe) != ILC_COLOR32) return FALSE;
260 if (bm.bmBitsPixel != 32) return FALSE;
262 SelectObject( hdc, hbmImage );
263 mask_width = (bm.bmWidth + 31) / 32 * 4;
265 if (!(info = heap_alloc( FIELD_OFFSET( BITMAPINFO, bmiColors[256] )))) goto done;
266 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
267 info->bmiHeader.biWidth = bm.bmWidth;
268 info->bmiHeader.biHeight = -height;
269 info->bmiHeader.biPlanes = 1;
270 info->bmiHeader.biBitCount = 32;
271 info->bmiHeader.biCompression = BI_RGB;
272 info->bmiHeader.biSizeImage = bm.bmWidth * height * 4;
273 info->bmiHeader.biXPelsPerMeter = 0;
274 info->bmiHeader.biYPelsPerMeter = 0;
275 info->bmiHeader.biClrUsed = 0;
276 info->bmiHeader.biClrImportant = 0;
277 if (!(bits = heap_alloc( info->bmiHeader.biSizeImage ))) goto done;
278 if (!GetDIBits( hdc, hbmImage, 0, height, bits, info, DIB_RGB_COLORS )) goto done;
280 if (hbmMask)
282 if (!(mask_info = heap_alloc( FIELD_OFFSET( BITMAPINFO, bmiColors[2] ))))
283 goto done;
284 mask_info->bmiHeader = info->bmiHeader;
285 mask_info->bmiHeader.biBitCount = 1;
286 mask_info->bmiHeader.biSizeImage = mask_width * height;
287 if (!(mask_bits = heap_alloc_zero( mask_info->bmiHeader.biSizeImage )))
288 goto done;
289 if (!GetDIBits( hdc, hbmMask, 0, height, mask_bits, mask_info, DIB_RGB_COLORS )) goto done;
292 add_dib_bits( himl, pos, count, width, height, info, mask_info, bits, mask_bits );
293 ret = TRUE;
295 done:
296 heap_free( info );
297 heap_free( mask_info );
298 heap_free( bits );
299 heap_free( mask_bits );
300 return ret;
303 UINT WINAPI
304 ImageList_SetColorTable(HIMAGELIST himl, UINT uStartIndex, UINT cEntries, const RGBQUAD *prgb);
306 /*************************************************************************
307 * IMAGELIST_InternalExpandBitmaps [Internal]
309 * Expands the bitmaps of an image list by the given number of images.
311 * PARAMS
312 * himl [I] handle to image list
313 * nImageCount [I] number of images to add
315 * RETURNS
316 * nothing
318 * NOTES
319 * This function CANNOT be used to reduce the number of images.
321 static void
322 IMAGELIST_InternalExpandBitmaps(HIMAGELIST himl, INT nImageCount)
324 HDC hdcBitmap;
325 HBITMAP hbmNewBitmap, hbmNull;
326 INT nNewCount;
327 SIZE sz;
329 TRACE("%p has allocated %d, max %d, grow %d images\n", himl, himl->cCurImage, himl->cMaxImage, himl->cGrow);
331 if (himl->cCurImage + nImageCount < himl->cMaxImage)
332 return;
334 nNewCount = himl->cMaxImage + max(nImageCount, himl->cGrow) + 1;
336 imagelist_get_bitmap_size(himl, nNewCount, &sz);
338 TRACE("Create expanded bitmaps : himl=%p x=%d y=%d count=%d\n", himl, sz.cx, sz.cy, nNewCount);
339 hdcBitmap = CreateCompatibleDC (0);
341 hbmNewBitmap = ImageList_CreateImage(hdcBitmap, himl, nNewCount);
343 if (hbmNewBitmap == 0)
344 ERR("creating new image bitmap (x=%d y=%d)!\n", sz.cx, sz.cy);
346 if (himl->cCurImage)
348 hbmNull = SelectObject (hdcBitmap, hbmNewBitmap);
349 BitBlt (hdcBitmap, 0, 0, sz.cx, sz.cy,
350 himl->hdcImage, 0, 0, SRCCOPY);
351 SelectObject (hdcBitmap, hbmNull);
353 SelectObject (himl->hdcImage, hbmNewBitmap);
354 DeleteObject (himl->hbmImage);
355 himl->hbmImage = hbmNewBitmap;
357 if (himl->flags & ILC_MASK)
359 hbmNewBitmap = CreateBitmap (sz.cx, sz.cy, 1, 1, NULL);
361 if (hbmNewBitmap == 0)
362 ERR("creating new mask bitmap!\n");
364 if(himl->cCurImage)
366 hbmNull = SelectObject (hdcBitmap, hbmNewBitmap);
367 BitBlt (hdcBitmap, 0, 0, sz.cx, sz.cy,
368 himl->hdcMask, 0, 0, SRCCOPY);
369 SelectObject (hdcBitmap, hbmNull);
371 SelectObject (himl->hdcMask, hbmNewBitmap);
372 DeleteObject (himl->hbmMask);
373 himl->hbmMask = hbmNewBitmap;
376 himl->item_flags = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, himl->item_flags,
377 nNewCount * sizeof(*himl->item_flags));
378 himl->cMaxImage = nNewCount;
380 DeleteDC (hdcBitmap);
384 /*************************************************************************
385 * ImageList_Add [COMCTL32.@]
387 * Add an image or images to an image list.
389 * PARAMS
390 * himl [I] handle to image list
391 * hbmImage [I] handle to image bitmap
392 * hbmMask [I] handle to mask bitmap
394 * RETURNS
395 * Success: Index of the first new image.
396 * Failure: -1
399 INT WINAPI
400 ImageList_Add (HIMAGELIST himl, HBITMAP hbmImage, HBITMAP hbmMask)
402 HDC hdcBitmap, hdcTemp = 0;
403 INT nFirstIndex, nImageCount, i;
404 BITMAP bmp;
405 POINT pt;
407 TRACE("himl=%p hbmimage=%p hbmmask=%p\n", himl, hbmImage, hbmMask);
408 if (!is_valid(himl))
409 return -1;
411 if (!GetObjectW(hbmImage, sizeof(BITMAP), &bmp))
412 return -1;
414 TRACE("himl %p, cCurImage %d, cMaxImage %d, cGrow %d, cx %d, cy %d\n",
415 himl, himl->cCurImage, himl->cMaxImage, himl->cGrow, himl->cx, himl->cy);
417 nImageCount = bmp.bmWidth / himl->cx;
419 TRACE("%p has %d images (%d x %d) bpp %d\n", hbmImage, nImageCount, bmp.bmWidth, bmp.bmHeight,
420 bmp.bmBitsPixel);
422 IMAGELIST_InternalExpandBitmaps(himl, nImageCount);
424 hdcBitmap = CreateCompatibleDC(0);
426 SelectObject(hdcBitmap, hbmImage);
428 if (add_with_alpha( himl, hdcBitmap, himl->cCurImage, nImageCount,
429 himl->cx, min( himl->cy, bmp.bmHeight), hbmImage, hbmMask ))
430 goto done;
432 if (himl->hbmMask)
434 hdcTemp = CreateCompatibleDC(0);
435 SelectObject(hdcTemp, hbmMask);
438 if (himl->uBitsPixel <= 8 && bmp.bmBitsPixel <= 8 &&
439 !himl->color_table_set && himl->cCurImage == 0)
441 RGBQUAD colors[256];
442 UINT num = GetDIBColorTable( hdcBitmap, 0, 1 << bmp.bmBitsPixel, colors );
443 if (num) ImageList_SetColorTable( himl, 0, num, colors );
446 for (i=0; i<nImageCount; i++)
448 imagelist_point_from_index( himl, himl->cCurImage + i, &pt );
450 /* Copy result to the imagelist
452 BitBlt( himl->hdcImage, pt.x, pt.y, himl->cx, bmp.bmHeight,
453 hdcBitmap, i*himl->cx, 0, SRCCOPY );
455 if (!himl->hbmMask)
456 continue;
458 BitBlt( himl->hdcMask, pt.x, pt.y, himl->cx, bmp.bmHeight,
459 hdcTemp, i*himl->cx, 0, SRCCOPY );
461 /* Remove the background from the image
463 BitBlt( himl->hdcImage, pt.x, pt.y, himl->cx, bmp.bmHeight,
464 himl->hdcMask, pt.x, pt.y, 0x220326 ); /* NOTSRCAND */
466 if (hdcTemp) DeleteDC(hdcTemp);
468 done:
469 DeleteDC(hdcBitmap);
471 nFirstIndex = himl->cCurImage;
472 himl->cCurImage += nImageCount;
474 return nFirstIndex;
478 /*************************************************************************
479 * ImageList_AddIcon [COMCTL32.@]
481 * Adds an icon to an image list.
483 * PARAMS
484 * himl [I] handle to image list
485 * hIcon [I] handle to icon
487 * RETURNS
488 * Success: index of the new image
489 * Failure: -1
491 #undef ImageList_AddIcon
492 INT WINAPI ImageList_AddIcon (HIMAGELIST himl, HICON hIcon)
494 return ImageList_ReplaceIcon (himl, -1, hIcon);
498 /*************************************************************************
499 * ImageList_AddMasked [COMCTL32.@]
501 * Adds an image or images to an image list and creates a mask from the
502 * specified bitmap using the mask color.
504 * PARAMS
505 * himl [I] handle to image list.
506 * hBitmap [I] handle to bitmap
507 * clrMask [I] mask color.
509 * RETURNS
510 * Success: Index of the first new image.
511 * Failure: -1
514 INT WINAPI
515 ImageList_AddMasked (HIMAGELIST himl, HBITMAP hBitmap, COLORREF clrMask)
517 HDC hdcMask, hdcBitmap;
518 INT ret;
519 BITMAP bmp;
520 HBITMAP hMaskBitmap;
521 COLORREF bkColor;
523 TRACE("himl=%p hbitmap=%p clrmask=%x\n", himl, hBitmap, clrMask);
524 if (!is_valid(himl))
525 return -1;
527 if (!GetObjectW(hBitmap, sizeof(BITMAP), &bmp))
528 return -1;
530 hdcBitmap = CreateCompatibleDC(0);
531 SelectObject(hdcBitmap, hBitmap);
533 /* Create a temp Mask so we can remove the background of the Image */
534 hdcMask = CreateCompatibleDC(0);
535 hMaskBitmap = CreateBitmap(bmp.bmWidth, bmp.bmHeight, 1, 1, NULL);
536 SelectObject(hdcMask, hMaskBitmap);
538 /* create monochrome image to the mask bitmap */
539 bkColor = (clrMask != CLR_DEFAULT) ? clrMask : GetPixel (hdcBitmap, 0, 0);
540 SetBkColor (hdcBitmap, bkColor);
541 BitBlt (hdcMask, 0, 0, bmp.bmWidth, bmp.bmHeight, hdcBitmap, 0, 0, SRCCOPY);
544 * Remove the background from the image
546 * WINDOWS BUG ALERT!!!!!!
547 * The statement below should not be done in common practice
548 * but this is how ImageList_AddMasked works in Windows.
549 * It overwrites the original bitmap passed, this was discovered
550 * by using the same bitmap to iterate the different styles
551 * on windows where it failed (BUT ImageList_Add is OK)
552 * This is here in case some apps rely on this bug
554 * Blt mode 0x220326 is NOTSRCAND
556 if (bmp.bmBitsPixel > 8) /* NOTSRCAND can't work with palettes */
558 SetBkColor(hdcBitmap, RGB(255,255,255));
559 BitBlt(hdcBitmap, 0, 0, bmp.bmWidth, bmp.bmHeight, hdcMask, 0, 0, 0x220326);
562 DeleteDC(hdcBitmap);
563 DeleteDC(hdcMask);
565 ret = ImageList_Add( himl, hBitmap, hMaskBitmap );
567 DeleteObject(hMaskBitmap);
568 return ret;
572 /*************************************************************************
573 * ImageList_BeginDrag [COMCTL32.@]
575 * Creates a temporary image list that contains one image. It will be used
576 * as a drag image.
578 * PARAMS
579 * himlTrack [I] handle to the source image list
580 * iTrack [I] index of the drag image in the source image list
581 * dxHotspot [I] X position of the hot spot of the drag image
582 * dyHotspot [I] Y position of the hot spot of the drag image
584 * RETURNS
585 * Success: TRUE
586 * Failure: FALSE
589 BOOL WINAPI
590 ImageList_BeginDrag (HIMAGELIST himlTrack, INT iTrack,
591 INT dxHotspot, INT dyHotspot)
593 INT cx, cy;
594 POINT src, dst;
596 TRACE("(himlTrack=%p iTrack=%d dx=%d dy=%d)\n", himlTrack, iTrack,
597 dxHotspot, dyHotspot);
599 if (!is_valid(himlTrack))
600 return FALSE;
602 if (iTrack >= himlTrack->cCurImage)
603 return FALSE;
605 if (InternalDrag.himl)
606 return FALSE;
608 cx = himlTrack->cx;
609 cy = himlTrack->cy;
611 InternalDrag.himlNoCursor = InternalDrag.himl = ImageList_Create (cx, cy, himlTrack->flags, 1, 1);
612 if (InternalDrag.himl == NULL) {
613 WARN("Error creating drag image list!\n");
614 return FALSE;
617 InternalDrag.dxHotspot = dxHotspot;
618 InternalDrag.dyHotspot = dyHotspot;
620 /* copy image */
621 imagelist_point_from_index(InternalDrag.himl, 0, &dst);
622 imagelist_point_from_index(himlTrack, iTrack, &src);
623 BitBlt(InternalDrag.himl->hdcImage, dst.x, dst.y, cx, cy, himlTrack->hdcImage, src.x, src.y,
624 SRCCOPY);
625 BitBlt(InternalDrag.himl->hdcMask, dst.x, dst.y, cx, cy, himlTrack->hdcMask, src.x, src.y,
626 SRCCOPY);
628 InternalDrag.himl->cCurImage = 1;
630 return TRUE;
634 /*************************************************************************
635 * ImageList_Copy [COMCTL32.@]
637 * Copies an image of the source image list to an image of the
638 * destination image list. Images can be copied or swapped.
640 * PARAMS
641 * himlDst [I] handle to the destination image list
642 * iDst [I] destination image index.
643 * himlSrc [I] handle to the source image list
644 * iSrc [I] source image index
645 * uFlags [I] flags for the copy operation
647 * RETURNS
648 * Success: TRUE
649 * Failure: FALSE
651 * NOTES
652 * Copying from one image list to another is possible. The original
653 * implementation just copies or swaps within one image list.
654 * Could this feature become a bug??? ;-)
657 BOOL WINAPI
658 ImageList_Copy (HIMAGELIST himlDst, INT iDst, HIMAGELIST himlSrc,
659 INT iSrc, UINT uFlags)
661 POINT ptSrc, ptDst;
663 TRACE("himlDst=%p iDst=%d himlSrc=%p iSrc=%d\n", himlDst, iDst, himlSrc, iSrc);
665 if (!is_valid(himlSrc) || !is_valid(himlDst))
666 return FALSE;
667 if ((iDst < 0) || (iDst >= himlDst->cCurImage))
668 return FALSE;
669 if ((iSrc < 0) || (iSrc >= himlSrc->cCurImage))
670 return FALSE;
672 imagelist_point_from_index( himlDst, iDst, &ptDst );
673 imagelist_point_from_index( himlSrc, iSrc, &ptSrc );
675 if (uFlags & ILCF_SWAP) {
676 /* swap */
677 HDC hdcBmp;
678 HBITMAP hbmTempImage, hbmTempMask;
680 hdcBmp = CreateCompatibleDC (0);
682 /* create temporary bitmaps */
683 hbmTempImage = CreateBitmap (himlSrc->cx, himlSrc->cy, 1,
684 himlSrc->uBitsPixel, NULL);
685 hbmTempMask = CreateBitmap (himlSrc->cx, himlSrc->cy, 1,
686 1, NULL);
688 /* copy (and stretch) destination to temporary bitmaps.(save) */
689 /* image */
690 SelectObject (hdcBmp, hbmTempImage);
691 StretchBlt (hdcBmp, 0, 0, himlSrc->cx, himlSrc->cy,
692 himlDst->hdcImage, ptDst.x, ptDst.y, himlDst->cx, himlDst->cy,
693 SRCCOPY);
694 /* mask */
695 SelectObject (hdcBmp, hbmTempMask);
696 StretchBlt (hdcBmp, 0, 0, himlSrc->cx, himlSrc->cy,
697 himlDst->hdcMask, ptDst.x, ptDst.y, himlDst->cx, himlDst->cy,
698 SRCCOPY);
700 /* copy (and stretch) source to destination */
701 /* image */
702 StretchBlt (himlDst->hdcImage, ptDst.x, ptDst.y, himlDst->cx, himlDst->cy,
703 himlSrc->hdcImage, ptSrc.x, ptSrc.y, himlSrc->cx, himlSrc->cy,
704 SRCCOPY);
705 /* mask */
706 StretchBlt (himlDst->hdcMask, ptDst.x, ptDst.y, himlDst->cx, himlDst->cy,
707 himlSrc->hdcMask, ptSrc.x, ptSrc.y, himlSrc->cx, himlSrc->cy,
708 SRCCOPY);
710 /* copy (without stretching) temporary bitmaps to source (restore) */
711 /* mask */
712 BitBlt (himlSrc->hdcMask, ptSrc.x, ptSrc.y, himlSrc->cx, himlSrc->cy,
713 hdcBmp, 0, 0, SRCCOPY);
715 /* image */
716 BitBlt (himlSrc->hdcImage, ptSrc.x, ptSrc.y, himlSrc->cx, himlSrc->cy,
717 hdcBmp, 0, 0, SRCCOPY);
718 /* delete temporary bitmaps */
719 DeleteObject (hbmTempMask);
720 DeleteObject (hbmTempImage);
721 DeleteDC(hdcBmp);
723 else {
724 /* copy image */
725 StretchBlt (himlDst->hdcImage, ptDst.x, ptDst.y, himlDst->cx, himlDst->cy,
726 himlSrc->hdcImage, ptSrc.x, ptSrc.y, himlSrc->cx, himlSrc->cy,
727 SRCCOPY);
729 /* copy mask */
730 StretchBlt (himlDst->hdcMask, ptDst.x, ptDst.y, himlDst->cx, himlDst->cy,
731 himlSrc->hdcMask, ptSrc.x, ptSrc.y, himlSrc->cx, himlSrc->cy,
732 SRCCOPY);
735 return TRUE;
739 /*************************************************************************
740 * ImageList_Create [COMCTL32.@]
742 * Creates a new image list.
744 * PARAMS
745 * cx [I] image height
746 * cy [I] image width
747 * flags [I] creation flags
748 * cInitial [I] initial number of images in the image list
749 * cGrow [I] number of images by which image list grows
751 * RETURNS
752 * Success: Handle to the created image list
753 * Failure: NULL
755 HIMAGELIST WINAPI
756 ImageList_Create (INT cx, INT cy, UINT flags,
757 INT cInitial, INT cGrow)
759 HIMAGELIST himl;
760 INT nCount;
761 HBITMAP hbmTemp;
762 UINT ilc = (flags & 0xFE);
763 static const WORD aBitBlend25[] =
764 {0xAA, 0x00, 0x55, 0x00, 0xAA, 0x00, 0x55, 0x00};
766 static const WORD aBitBlend50[] =
767 {0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA};
769 TRACE("(%d %d 0x%x %d %d)\n", cx, cy, flags, cInitial, cGrow);
771 if (cx < 0 || cy < 0) return NULL;
772 if (!((flags&ILC_COLORDDB) == ILC_COLORDDB) && (cx == 0 || cy == 0)) return NULL;
774 /* Create the IImageList interface for the image list */
775 if (FAILED(ImageListImpl_CreateInstance(NULL, &IID_IImageList, (void **)&himl)))
776 return NULL;
778 cGrow = (WORD)((max( cGrow, 1 ) + 3) & ~3);
780 if (cGrow > 256)
782 /* Windows doesn't limit the size here, but X11 doesn't let us allocate such huge bitmaps */
783 WARN( "grow %d too large, limiting to 256\n", cGrow );
784 cGrow = 256;
787 himl->cx = cx;
788 himl->cy = cy;
789 himl->flags = flags;
790 himl->cMaxImage = cInitial + 1;
791 himl->cInitial = cInitial;
792 himl->cGrow = cGrow;
793 himl->clrFg = CLR_DEFAULT;
794 himl->clrBk = CLR_NONE;
795 himl->color_table_set = FALSE;
797 /* initialize overlay mask indices */
798 for (nCount = 0; nCount < MAX_OVERLAYIMAGE; nCount++)
799 himl->nOvlIdx[nCount] = -1;
801 /* Create Image & Mask DCs */
802 himl->hdcImage = CreateCompatibleDC (0);
803 if (!himl->hdcImage)
804 goto cleanup;
805 if (himl->flags & ILC_MASK){
806 himl->hdcMask = CreateCompatibleDC(0);
807 if (!himl->hdcMask)
808 goto cleanup;
811 /* Default to ILC_COLOR4 if none of the ILC_COLOR* flags are specified */
812 if (ilc == ILC_COLOR)
814 ilc = ILC_COLOR4;
815 himl->flags |= ILC_COLOR4;
818 if (ilc >= ILC_COLOR4 && ilc <= ILC_COLOR32)
819 himl->uBitsPixel = ilc;
820 else
821 himl->uBitsPixel = (UINT)GetDeviceCaps (himl->hdcImage, BITSPIXEL);
823 if (himl->cMaxImage > 0) {
824 himl->hbmImage = ImageList_CreateImage(himl->hdcImage, himl, himl->cMaxImage);
825 SelectObject(himl->hdcImage, himl->hbmImage);
826 } else
827 himl->hbmImage = 0;
829 if ((himl->cMaxImage > 0) && (himl->flags & ILC_MASK)) {
830 SIZE sz;
832 imagelist_get_bitmap_size(himl, himl->cMaxImage, &sz);
833 himl->hbmMask = CreateBitmap (sz.cx, sz.cy, 1, 1, NULL);
834 if (himl->hbmMask == 0) {
835 ERR("Error creating mask bitmap!\n");
836 goto cleanup;
838 SelectObject(himl->hdcMask, himl->hbmMask);
840 else
841 himl->hbmMask = 0;
843 himl->item_flags = heap_alloc_zero( himl->cMaxImage * sizeof(*himl->item_flags) );
845 /* create blending brushes */
846 hbmTemp = CreateBitmap (8, 8, 1, 1, aBitBlend25);
847 himl->hbrBlend25 = CreatePatternBrush (hbmTemp);
848 DeleteObject (hbmTemp);
850 hbmTemp = CreateBitmap (8, 8, 1, 1, aBitBlend50);
851 himl->hbrBlend50 = CreatePatternBrush (hbmTemp);
852 DeleteObject (hbmTemp);
854 TRACE("created imagelist %p\n", himl);
855 return himl;
857 cleanup:
858 ImageList_Destroy(himl);
859 return NULL;
863 /*************************************************************************
864 * ImageList_Destroy [COMCTL32.@]
866 * Destroys an image list.
868 * PARAMS
869 * himl [I] handle to image list
871 * RETURNS
872 * Success: TRUE
873 * Failure: FALSE
876 BOOL WINAPI
877 ImageList_Destroy (HIMAGELIST himl)
879 if (!is_valid(himl))
880 return FALSE;
882 IImageList_Release((IImageList *) himl);
883 return TRUE;
887 /*************************************************************************
888 * ImageList_DragEnter [COMCTL32.@]
890 * Locks window update and displays the drag image at the given position.
892 * PARAMS
893 * hwndLock [I] handle of the window that owns the drag image.
894 * x [I] X position of the drag image.
895 * y [I] Y position of the drag image.
897 * RETURNS
898 * Success: TRUE
899 * Failure: FALSE
901 * NOTES
902 * The position of the drag image is relative to the window, not
903 * the client area.
906 BOOL WINAPI
907 ImageList_DragEnter (HWND hwndLock, INT x, INT y)
909 TRACE("(hwnd=%p x=%d y=%d)\n", hwndLock, x, y);
911 if (!is_valid(InternalDrag.himl))
912 return FALSE;
914 if (hwndLock)
915 InternalDrag.hwnd = hwndLock;
916 else
917 InternalDrag.hwnd = GetDesktopWindow ();
919 InternalDrag.x = x;
920 InternalDrag.y = y;
922 /* draw the drag image and save the background */
923 return ImageList_DragShowNolock(TRUE);
927 /*************************************************************************
928 * ImageList_DragLeave [COMCTL32.@]
930 * Unlocks window update and hides the drag image.
932 * PARAMS
933 * hwndLock [I] handle of the window that owns the drag image.
935 * RETURNS
936 * Success: TRUE
937 * Failure: FALSE
940 BOOL WINAPI
941 ImageList_DragLeave (HWND hwndLock)
943 /* As we don't save drag info in the window this can lead to problems if
944 an app does not supply the same window as DragEnter */
945 /* if (hwndLock)
946 InternalDrag.hwnd = hwndLock;
947 else
948 InternalDrag.hwnd = GetDesktopWindow (); */
949 if(!hwndLock)
950 hwndLock = GetDesktopWindow();
951 if(InternalDrag.hwnd != hwndLock)
952 FIXME("DragLeave hWnd != DragEnter hWnd\n");
954 ImageList_DragShowNolock (FALSE);
956 return TRUE;
960 /*************************************************************************
961 * ImageList_InternalDragDraw [Internal]
963 * Draws the drag image.
965 * PARAMS
966 * hdc [I] device context to draw into.
967 * x [I] X position of the drag image.
968 * y [I] Y position of the drag image.
970 * RETURNS
971 * Success: TRUE
972 * Failure: FALSE
974 * NOTES
975 * The position of the drag image is relative to the window, not
976 * the client area.
980 static inline void
981 ImageList_InternalDragDraw (HDC hdc, INT x, INT y)
983 IMAGELISTDRAWPARAMS imldp;
985 ZeroMemory (&imldp, sizeof(imldp));
986 imldp.cbSize = sizeof(imldp);
987 imldp.himl = InternalDrag.himl;
988 imldp.i = 0;
989 imldp.hdcDst = hdc;
990 imldp.x = x;
991 imldp.y = y;
992 imldp.rgbBk = CLR_DEFAULT;
993 imldp.rgbFg = CLR_DEFAULT;
994 imldp.fStyle = ILD_NORMAL;
995 imldp.fState = ILS_ALPHA;
996 imldp.Frame = 192;
997 ImageList_DrawIndirect (&imldp);
1000 /*************************************************************************
1001 * ImageList_DragMove [COMCTL32.@]
1003 * Moves the drag image.
1005 * PARAMS
1006 * x [I] X position of the drag image.
1007 * y [I] Y position of the drag image.
1009 * RETURNS
1010 * Success: TRUE
1011 * Failure: FALSE
1013 * NOTES
1014 * The position of the drag image is relative to the window, not
1015 * the client area.
1018 BOOL WINAPI
1019 ImageList_DragMove (INT x, INT y)
1021 TRACE("(x=%d y=%d)\n", x, y);
1023 if (!is_valid(InternalDrag.himl))
1024 return FALSE;
1026 /* draw/update the drag image */
1027 if (InternalDrag.bShow) {
1028 HDC hdcDrag;
1029 HDC hdcOffScreen;
1030 HDC hdcBg;
1031 HBITMAP hbmOffScreen;
1032 INT origNewX, origNewY;
1033 INT origOldX, origOldY;
1034 INT origRegX, origRegY;
1035 INT sizeRegX, sizeRegY;
1038 /* calculate the update region */
1039 origNewX = x - InternalDrag.dxHotspot;
1040 origNewY = y - InternalDrag.dyHotspot;
1041 origOldX = InternalDrag.x - InternalDrag.dxHotspot;
1042 origOldY = InternalDrag.y - InternalDrag.dyHotspot;
1043 origRegX = min(origNewX, origOldX);
1044 origRegY = min(origNewY, origOldY);
1045 sizeRegX = InternalDrag.himl->cx + abs(x - InternalDrag.x);
1046 sizeRegY = InternalDrag.himl->cy + abs(y - InternalDrag.y);
1048 hdcDrag = GetDCEx(InternalDrag.hwnd, 0,
1049 DCX_WINDOW | DCX_CACHE | DCX_LOCKWINDOWUPDATE);
1050 hdcOffScreen = CreateCompatibleDC(hdcDrag);
1051 hdcBg = CreateCompatibleDC(hdcDrag);
1053 hbmOffScreen = CreateCompatibleBitmap(hdcDrag, sizeRegX, sizeRegY);
1054 SelectObject(hdcOffScreen, hbmOffScreen);
1055 SelectObject(hdcBg, InternalDrag.hbmBg);
1057 /* get the actual background of the update region */
1058 BitBlt(hdcOffScreen, 0, 0, sizeRegX, sizeRegY, hdcDrag,
1059 origRegX, origRegY, SRCCOPY);
1060 /* erase the old image */
1061 BitBlt(hdcOffScreen, origOldX - origRegX, origOldY - origRegY,
1062 InternalDrag.himl->cx, InternalDrag.himl->cy, hdcBg, 0, 0,
1063 SRCCOPY);
1064 /* save the background */
1065 BitBlt(hdcBg, 0, 0, InternalDrag.himl->cx, InternalDrag.himl->cy,
1066 hdcOffScreen, origNewX - origRegX, origNewY - origRegY, SRCCOPY);
1067 /* draw the image */
1068 ImageList_InternalDragDraw(hdcOffScreen, origNewX - origRegX,
1069 origNewY - origRegY);
1070 /* draw the update region to the screen */
1071 BitBlt(hdcDrag, origRegX, origRegY, sizeRegX, sizeRegY,
1072 hdcOffScreen, 0, 0, SRCCOPY);
1074 DeleteDC(hdcBg);
1075 DeleteDC(hdcOffScreen);
1076 DeleteObject(hbmOffScreen);
1077 ReleaseDC(InternalDrag.hwnd, hdcDrag);
1080 /* update the image position */
1081 InternalDrag.x = x;
1082 InternalDrag.y = y;
1084 return TRUE;
1088 /*************************************************************************
1089 * ImageList_DragShowNolock [COMCTL32.@]
1091 * Shows or hides the drag image.
1093 * PARAMS
1094 * bShow [I] TRUE shows the drag image, FALSE hides it.
1096 * RETURNS
1097 * Success: TRUE
1098 * Failure: FALSE
1101 BOOL WINAPI
1102 ImageList_DragShowNolock (BOOL bShow)
1104 HDC hdcDrag;
1105 HDC hdcBg;
1106 INT x, y;
1108 if (!is_valid(InternalDrag.himl))
1109 return FALSE;
1111 TRACE("bShow=0x%X!\n", bShow);
1113 /* DragImage is already visible/hidden */
1114 if ((InternalDrag.bShow && bShow) || (!InternalDrag.bShow && !bShow)) {
1115 return FALSE;
1118 /* position of the origin of the DragImage */
1119 x = InternalDrag.x - InternalDrag.dxHotspot;
1120 y = InternalDrag.y - InternalDrag.dyHotspot;
1122 hdcDrag = GetDCEx (InternalDrag.hwnd, 0,
1123 DCX_WINDOW | DCX_CACHE | DCX_LOCKWINDOWUPDATE);
1124 if (!hdcDrag) {
1125 return FALSE;
1128 hdcBg = CreateCompatibleDC(hdcDrag);
1129 if (!InternalDrag.hbmBg) {
1130 InternalDrag.hbmBg = CreateCompatibleBitmap(hdcDrag,
1131 InternalDrag.himl->cx, InternalDrag.himl->cy);
1133 SelectObject(hdcBg, InternalDrag.hbmBg);
1135 if (bShow) {
1136 /* save the background */
1137 BitBlt(hdcBg, 0, 0, InternalDrag.himl->cx, InternalDrag.himl->cy,
1138 hdcDrag, x, y, SRCCOPY);
1139 /* show the image */
1140 ImageList_InternalDragDraw(hdcDrag, x, y);
1141 } else {
1142 /* hide the image */
1143 BitBlt(hdcDrag, x, y, InternalDrag.himl->cx, InternalDrag.himl->cy,
1144 hdcBg, 0, 0, SRCCOPY);
1147 InternalDrag.bShow = !InternalDrag.bShow;
1149 DeleteDC(hdcBg);
1150 ReleaseDC (InternalDrag.hwnd, hdcDrag);
1151 return TRUE;
1155 /*************************************************************************
1156 * ImageList_Draw [COMCTL32.@]
1158 * Draws an image.
1160 * PARAMS
1161 * himl [I] handle to image list
1162 * i [I] image index
1163 * hdc [I] handle to device context
1164 * x [I] x position
1165 * y [I] y position
1166 * fStyle [I] drawing flags
1168 * RETURNS
1169 * Success: TRUE
1170 * Failure: FALSE
1172 * SEE
1173 * ImageList_DrawEx.
1176 BOOL WINAPI
1177 ImageList_Draw (HIMAGELIST himl, INT i, HDC hdc, INT x, INT y, UINT fStyle)
1179 return ImageList_DrawEx (himl, i, hdc, x, y, 0, 0,
1180 CLR_DEFAULT, CLR_DEFAULT, fStyle);
1184 /*************************************************************************
1185 * ImageList_DrawEx [COMCTL32.@]
1187 * Draws an image and allows using extended drawing features.
1189 * PARAMS
1190 * himl [I] handle to image list
1191 * i [I] image index
1192 * hdc [I] handle to device context
1193 * x [I] X position
1194 * y [I] Y position
1195 * dx [I] X offset
1196 * dy [I] Y offset
1197 * rgbBk [I] background color
1198 * rgbFg [I] foreground color
1199 * fStyle [I] drawing flags
1201 * RETURNS
1202 * Success: TRUE
1203 * Failure: FALSE
1205 * NOTES
1206 * Calls ImageList_DrawIndirect.
1208 * SEE
1209 * ImageList_DrawIndirect.
1212 BOOL WINAPI
1213 ImageList_DrawEx (HIMAGELIST himl, INT i, HDC hdc, INT x, INT y,
1214 INT dx, INT dy, COLORREF rgbBk, COLORREF rgbFg,
1215 UINT fStyle)
1217 IMAGELISTDRAWPARAMS imldp;
1219 ZeroMemory (&imldp, sizeof(imldp));
1220 imldp.cbSize = sizeof(imldp);
1221 imldp.himl = himl;
1222 imldp.i = i;
1223 imldp.hdcDst = hdc;
1224 imldp.x = x;
1225 imldp.y = y;
1226 imldp.cx = dx;
1227 imldp.cy = dy;
1228 imldp.rgbBk = rgbBk;
1229 imldp.rgbFg = rgbFg;
1230 imldp.fStyle = fStyle;
1232 return ImageList_DrawIndirect (&imldp);
1236 static BOOL alpha_blend_image( HIMAGELIST himl, HDC dest_dc, int dest_x, int dest_y,
1237 int src_x, int src_y, int cx, int cy, UINT style, UINT state,
1238 DWORD frame, COLORREF blend_col, BOOL has_alpha )
1240 BOOL ret = FALSE;
1241 HDC hdc;
1242 HBITMAP bmp = 0, mask = 0;
1243 BITMAPINFO *info;
1244 BLENDFUNCTION func;
1245 void *bits, *mask_bits;
1246 unsigned int *ptr;
1247 int i, j;
1249 func.BlendOp = AC_SRC_OVER;
1250 func.BlendFlags = 0;
1251 func.SourceConstantAlpha = 255;
1252 func.AlphaFormat = AC_SRC_ALPHA;
1254 if (!(hdc = CreateCompatibleDC( 0 ))) return FALSE;
1255 if (!(info = heap_alloc( FIELD_OFFSET( BITMAPINFO, bmiColors[256] )))) goto done;
1256 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1257 info->bmiHeader.biWidth = cx;
1258 info->bmiHeader.biHeight = cy;
1259 info->bmiHeader.biPlanes = 1;
1260 info->bmiHeader.biBitCount = 32;
1261 info->bmiHeader.biCompression = BI_RGB;
1262 info->bmiHeader.biSizeImage = cx * cy * 4;
1263 info->bmiHeader.biXPelsPerMeter = 0;
1264 info->bmiHeader.biYPelsPerMeter = 0;
1265 info->bmiHeader.biClrUsed = 0;
1266 info->bmiHeader.biClrImportant = 0;
1267 if (!(bmp = CreateDIBSection( himl->hdcImage, info, DIB_RGB_COLORS, &bits, 0, 0 ))) goto done;
1268 SelectObject( hdc, bmp );
1269 BitBlt( hdc, 0, 0, cx, cy, himl->hdcImage, src_x, src_y, SRCCOPY );
1271 if (blend_col != CLR_NONE)
1273 BYTE r = GetRValue( blend_col );
1274 BYTE g = GetGValue( blend_col );
1275 BYTE b = GetBValue( blend_col );
1277 if (style & ILD_BLEND25)
1279 for (i = 0, ptr = bits; i < cx * cy; i++, ptr++)
1280 *ptr = ((*ptr & 0xff000000) |
1281 ((((*ptr & 0x00ff0000) * 3 + (r << 16)) / 4) & 0x00ff0000) |
1282 ((((*ptr & 0x0000ff00) * 3 + (g << 8)) / 4) & 0x0000ff00) |
1283 ((((*ptr & 0x000000ff) * 3 + (b << 0)) / 4) & 0x000000ff));
1285 else if (style & ILD_BLEND50)
1287 for (i = 0, ptr = bits; i < cx * cy; i++, ptr++)
1288 *ptr = ((*ptr & 0xff000000) |
1289 ((((*ptr & 0x00ff0000) + (r << 16)) / 2) & 0x00ff0000) |
1290 ((((*ptr & 0x0000ff00) + (g << 8)) / 2) & 0x0000ff00) |
1291 ((((*ptr & 0x000000ff) + (b << 0)) / 2) & 0x000000ff));
1296 if (state & ILS_ALPHA)
1298 func.SourceConstantAlpha = (BYTE)frame;
1300 else if (state & ILS_SATURATE)
1302 for (i = 0, ptr = bits; i < cx * cy; i++, ptr++)
1304 DWORD gray = (((*ptr & 0x00ff0000) >> 16) * 299 +
1305 ((*ptr & 0x0000ff00) >> 8) * 587 +
1306 ((*ptr & 0x000000ff) >> 0) * 114 + 500) / 1000;
1307 if (has_alpha) gray = gray * (*ptr >> 24) / 255;
1308 *ptr = (*ptr & 0xff000000)| (gray << 16) | (gray << 8) | gray;
1311 else if (style & ILD_PRESERVEALPHA)
1313 HBRUSH old_brush = SelectObject( dest_dc, GetStockObject(BLACK_BRUSH) );
1314 PatBlt( dest_dc, dest_x, dest_y, cx, cy, PATCOPY );
1315 SelectObject( dest_dc, old_brush );
1318 if (has_alpha) /* we already have an alpha channel in this case */
1320 /* pre-multiply by the alpha channel */
1321 for (i = 0, ptr = bits; i < cx * cy; i++, ptr++)
1323 DWORD alpha = *ptr >> 24;
1324 *ptr = ((*ptr & 0xff000000) |
1325 (((*ptr & 0x00ff0000) * alpha / 255) & 0x00ff0000) |
1326 (((*ptr & 0x0000ff00) * alpha / 255) & 0x0000ff00) |
1327 (((*ptr & 0x000000ff) * alpha / 255)));
1330 else if (himl->hbmMask)
1332 unsigned int width_bytes = (cx + 31) / 32 * 4;
1333 /* generate alpha channel from the mask */
1334 info->bmiHeader.biBitCount = 1;
1335 info->bmiHeader.biSizeImage = width_bytes * cy;
1336 info->bmiColors[0].rgbRed = 0;
1337 info->bmiColors[0].rgbGreen = 0;
1338 info->bmiColors[0].rgbBlue = 0;
1339 info->bmiColors[0].rgbReserved = 0;
1340 info->bmiColors[1].rgbRed = 0xff;
1341 info->bmiColors[1].rgbGreen = 0xff;
1342 info->bmiColors[1].rgbBlue = 0xff;
1343 info->bmiColors[1].rgbReserved = 0;
1344 if (!(mask = CreateDIBSection( himl->hdcMask, info, DIB_RGB_COLORS, &mask_bits, 0, 0 )))
1345 goto done;
1346 SelectObject( hdc, mask );
1347 BitBlt( hdc, 0, 0, cx, cy, himl->hdcMask, src_x, src_y, SRCCOPY );
1348 SelectObject( hdc, bmp );
1349 for (i = 0, ptr = bits; i < cy; i++)
1350 for (j = 0; j < cx; j++, ptr++)
1351 if ((((BYTE *)mask_bits)[i * width_bytes + j / 8] << (j % 8)) & 0x80) *ptr = 0;
1352 else *ptr |= 0xff000000;
1354 else
1356 for (i = 0, ptr = bits; i < cx * cy; i++, ptr++)
1357 *ptr |= 0xff000000;
1360 ret = GdiAlphaBlend( dest_dc, dest_x, dest_y, cx, cy, hdc, 0, 0, cx, cy, func );
1362 done:
1363 DeleteDC( hdc );
1364 if (bmp) DeleteObject( bmp );
1365 if (mask) DeleteObject( mask );
1366 heap_free( info );
1367 return ret;
1370 /*************************************************************************
1371 * ImageList_DrawIndirect [COMCTL32.@]
1373 * Draws an image using various parameters specified in pimldp.
1375 * PARAMS
1376 * pimldp [I] pointer to IMAGELISTDRAWPARAMS structure.
1378 * RETURNS
1379 * Success: TRUE
1380 * Failure: FALSE
1383 BOOL WINAPI
1384 ImageList_DrawIndirect (IMAGELISTDRAWPARAMS *pimldp)
1386 INT cx, cy, nOvlIdx;
1387 DWORD fState, dwRop;
1388 UINT fStyle;
1389 COLORREF oldImageBk, oldImageFg;
1390 HDC hImageDC, hImageListDC, hMaskListDC;
1391 HBITMAP hImageBmp, hOldImageBmp, hBlendMaskBmp;
1392 BOOL bIsTransparent, bBlend, bResult = FALSE, bMask;
1393 HIMAGELIST himl;
1394 HBRUSH hOldBrush;
1395 POINT pt;
1396 BOOL has_alpha;
1398 if (!pimldp || !(himl = pimldp->himl)) return FALSE;
1399 if (!is_valid(himl)) return FALSE;
1400 if ((pimldp->i < 0) || (pimldp->i >= himl->cCurImage)) return FALSE;
1402 imagelist_point_from_index( himl, pimldp->i, &pt );
1403 pt.x += pimldp->xBitmap;
1404 pt.y += pimldp->yBitmap;
1406 fState = pimldp->cbSize < sizeof(IMAGELISTDRAWPARAMS) ? ILS_NORMAL : pimldp->fState;
1407 fStyle = pimldp->fStyle & ~ILD_OVERLAYMASK;
1408 cx = (pimldp->cx == 0) ? himl->cx : pimldp->cx;
1409 cy = (pimldp->cy == 0) ? himl->cy : pimldp->cy;
1411 bIsTransparent = (fStyle & ILD_TRANSPARENT);
1412 if( pimldp->rgbBk == CLR_NONE )
1413 bIsTransparent = TRUE;
1414 if( ( pimldp->rgbBk == CLR_DEFAULT ) && ( himl->clrBk == CLR_NONE ) )
1415 bIsTransparent = TRUE;
1416 bMask = (himl->flags & ILC_MASK) && (fStyle & ILD_MASK) ;
1417 bBlend = (fStyle & (ILD_BLEND25 | ILD_BLEND50) ) && !bMask;
1419 TRACE("himl(%p) hbmMask(%p) iImage(%d) x(%d) y(%d) cx(%d) cy(%d)\n",
1420 himl, himl->hbmMask, pimldp->i, pimldp->x, pimldp->y, cx, cy);
1422 /* we will use these DCs to access the images and masks in the ImageList */
1423 hImageListDC = himl->hdcImage;
1424 hMaskListDC = himl->hdcMask;
1426 /* these will accumulate the image and mask for the image we're drawing */
1427 hImageDC = CreateCompatibleDC( pimldp->hdcDst );
1428 hImageBmp = CreateCompatibleBitmap( pimldp->hdcDst, cx, cy );
1429 hBlendMaskBmp = bBlend ? CreateBitmap(cx, cy, 1, 1, NULL) : 0;
1431 /* Create a compatible DC. */
1432 if (!hImageListDC || !hImageDC || !hImageBmp ||
1433 (bBlend && !hBlendMaskBmp) || (himl->hbmMask && !hMaskListDC))
1434 goto cleanup;
1436 hOldImageBmp = SelectObject(hImageDC, hImageBmp);
1439 * To obtain a transparent look, background color should be set
1440 * to white and foreground color to black when blitting the
1441 * monochrome mask.
1443 oldImageFg = SetTextColor( hImageDC, RGB( 0, 0, 0 ) );
1444 oldImageBk = SetBkColor( hImageDC, RGB( 0xff, 0xff, 0xff ) );
1446 has_alpha = himl->item_flags[pimldp->i] & ILIF_ALPHA;
1447 if (!bMask && (has_alpha || (fState & ILS_ALPHA) || (fState & ILS_SATURATE)))
1449 COLORREF colour, blend_col = CLR_NONE;
1451 if (bBlend)
1453 blend_col = pimldp->rgbFg;
1454 if (blend_col == CLR_DEFAULT) blend_col = GetSysColor( COLOR_HIGHLIGHT );
1455 else if (blend_col == CLR_NONE) blend_col = GetTextColor( pimldp->hdcDst );
1458 if (bIsTransparent)
1460 bResult = alpha_blend_image( himl, pimldp->hdcDst, pimldp->x, pimldp->y, pt.x, pt.y, cx, cy,
1461 fStyle, fState, pimldp->Frame, blend_col, has_alpha );
1462 goto end;
1464 colour = pimldp->rgbBk;
1465 if (colour == CLR_DEFAULT) colour = himl->clrBk;
1466 if (colour == CLR_NONE) colour = GetBkColor( pimldp->hdcDst );
1468 hOldBrush = SelectObject (hImageDC, CreateSolidBrush (colour));
1469 PatBlt( hImageDC, 0, 0, cx, cy, PATCOPY );
1470 alpha_blend_image( himl, hImageDC, 0, 0, pt.x, pt.y, cx, cy, fStyle, fState,
1471 pimldp->Frame, blend_col, has_alpha );
1472 DeleteObject (SelectObject (hImageDC, hOldBrush));
1473 bResult = BitBlt( pimldp->hdcDst, pimldp->x, pimldp->y, cx, cy, hImageDC, 0, 0, SRCCOPY );
1474 goto end;
1478 * Draw the initial image
1480 if( bMask ) {
1481 if (himl->hbmMask) {
1482 hOldBrush = SelectObject (hImageDC, CreateSolidBrush (GetTextColor(pimldp->hdcDst)));
1483 PatBlt( hImageDC, 0, 0, cx, cy, PATCOPY );
1484 BitBlt(hImageDC, 0, 0, cx, cy, hMaskListDC, pt.x, pt.y, SRCPAINT);
1485 DeleteObject (SelectObject (hImageDC, hOldBrush));
1486 if( bIsTransparent )
1488 BitBlt ( pimldp->hdcDst, pimldp->x, pimldp->y, cx, cy, hImageDC, 0, 0, SRCAND);
1489 bResult = TRUE;
1490 goto end;
1492 } else {
1493 hOldBrush = SelectObject (hImageDC, GetStockObject(BLACK_BRUSH));
1494 PatBlt( hImageDC, 0, 0, cx, cy, PATCOPY);
1495 SelectObject(hImageDC, hOldBrush);
1497 } else {
1498 /* blend the image with the needed solid background */
1499 COLORREF colour = RGB(0,0,0);
1501 if( !bIsTransparent )
1503 colour = pimldp->rgbBk;
1504 if( colour == CLR_DEFAULT )
1505 colour = himl->clrBk;
1506 if( colour == CLR_NONE )
1507 colour = GetBkColor(pimldp->hdcDst);
1510 hOldBrush = SelectObject (hImageDC, CreateSolidBrush (colour));
1511 PatBlt( hImageDC, 0, 0, cx, cy, PATCOPY );
1512 if (himl->hbmMask)
1514 BitBlt( hImageDC, 0, 0, cx, cy, hMaskListDC, pt.x, pt.y, SRCAND );
1515 BitBlt( hImageDC, 0, 0, cx, cy, hImageListDC, pt.x, pt.y, SRCPAINT );
1517 else
1518 BitBlt( hImageDC, 0, 0, cx, cy, hImageListDC, pt.x, pt.y, SRCCOPY);
1519 DeleteObject (SelectObject (hImageDC, hOldBrush));
1522 /* Time for blending, if required */
1523 if (bBlend) {
1524 HBRUSH hBlendBrush;
1525 COLORREF clrBlend = pimldp->rgbFg;
1526 HDC hBlendMaskDC = hImageListDC;
1527 HBITMAP hOldBitmap;
1529 /* Create the blend Mask */
1530 hOldBitmap = SelectObject(hBlendMaskDC, hBlendMaskBmp);
1531 hBlendBrush = fStyle & ILD_BLEND50 ? himl->hbrBlend50 : himl->hbrBlend25;
1532 hOldBrush = SelectObject(hBlendMaskDC, hBlendBrush);
1533 PatBlt(hBlendMaskDC, 0, 0, cx, cy, PATCOPY);
1534 SelectObject(hBlendMaskDC, hOldBrush);
1536 /* Modify the blend mask if an Image Mask exist */
1537 if(himl->hbmMask) {
1538 BitBlt(hBlendMaskDC, 0, 0, cx, cy, hMaskListDC, pt.x, pt.y, 0x220326); /* NOTSRCAND */
1539 BitBlt(hBlendMaskDC, 0, 0, cx, cy, hBlendMaskDC, 0, 0, NOTSRCCOPY);
1542 /* now apply blend to the current image given the BlendMask */
1543 if (clrBlend == CLR_DEFAULT) clrBlend = GetSysColor (COLOR_HIGHLIGHT);
1544 else if (clrBlend == CLR_NONE) clrBlend = GetTextColor (pimldp->hdcDst);
1545 hOldBrush = SelectObject (hImageDC, CreateSolidBrush(clrBlend));
1546 BitBlt (hImageDC, 0, 0, cx, cy, hBlendMaskDC, 0, 0, 0xB8074A); /* PSDPxax */
1547 DeleteObject(SelectObject(hImageDC, hOldBrush));
1548 SelectObject(hBlendMaskDC, hOldBitmap);
1551 /* Now do the overlay image, if any */
1552 nOvlIdx = (pimldp->fStyle & ILD_OVERLAYMASK) >> 8;
1553 if ( (nOvlIdx >= 1) && (nOvlIdx <= MAX_OVERLAYIMAGE)) {
1554 nOvlIdx = himl->nOvlIdx[nOvlIdx - 1];
1555 if ((nOvlIdx >= 0) && (nOvlIdx < himl->cCurImage)) {
1556 POINT ptOvl;
1557 imagelist_point_from_index( himl, nOvlIdx, &ptOvl );
1558 ptOvl.x += pimldp->xBitmap;
1559 if (himl->hbmMask && !(fStyle & ILD_IMAGE))
1560 BitBlt (hImageDC, 0, 0, cx, cy, hMaskListDC, ptOvl.x, ptOvl.y, SRCAND);
1561 BitBlt (hImageDC, 0, 0, cx, cy, hImageListDC, ptOvl.x, ptOvl.y, SRCPAINT);
1565 if (fState & ILS_GLOW) FIXME("ILS_GLOW: unimplemented!\n");
1566 if (fState & ILS_SHADOW) FIXME("ILS_SHADOW: unimplemented!\n");
1568 if (fStyle & ILD_SCALE) FIXME("ILD_SCALE: unimplemented!\n");
1569 if (fStyle & ILD_DPISCALE) FIXME("ILD_DPISCALE: unimplemented!\n");
1571 /* now copy the image to the screen */
1572 dwRop = SRCCOPY;
1573 if (himl->hbmMask && bIsTransparent ) {
1574 COLORREF oldDstFg = SetTextColor(pimldp->hdcDst, RGB( 0, 0, 0 ) );
1575 COLORREF oldDstBk = SetBkColor(pimldp->hdcDst, RGB( 0xff, 0xff, 0xff ));
1576 BitBlt (pimldp->hdcDst, pimldp->x, pimldp->y, cx, cy, hMaskListDC, pt.x, pt.y, SRCAND);
1577 SetBkColor(pimldp->hdcDst, oldDstBk);
1578 SetTextColor(pimldp->hdcDst, oldDstFg);
1579 dwRop = SRCPAINT;
1581 if (fStyle & ILD_ROP) dwRop = pimldp->dwRop;
1582 BitBlt (pimldp->hdcDst, pimldp->x, pimldp->y, cx, cy, hImageDC, 0, 0, dwRop);
1584 bResult = TRUE;
1585 end:
1586 /* cleanup the mess */
1587 SetBkColor(hImageDC, oldImageBk);
1588 SetTextColor(hImageDC, oldImageFg);
1589 SelectObject(hImageDC, hOldImageBmp);
1590 cleanup:
1591 DeleteObject(hBlendMaskBmp);
1592 DeleteObject(hImageBmp);
1593 DeleteDC(hImageDC);
1595 return bResult;
1599 /*************************************************************************
1600 * ImageList_Duplicate [COMCTL32.@]
1602 * Duplicates an image list.
1604 * PARAMS
1605 * himlSrc [I] source image list handle
1607 * RETURNS
1608 * Success: Handle of duplicated image list.
1609 * Failure: NULL
1612 HIMAGELIST WINAPI
1613 ImageList_Duplicate (HIMAGELIST himlSrc)
1615 HIMAGELIST himlDst;
1617 if (!is_valid(himlSrc)) {
1618 ERR("Invalid image list handle!\n");
1619 return NULL;
1622 himlDst = ImageList_Create (himlSrc->cx, himlSrc->cy, himlSrc->flags,
1623 himlSrc->cCurImage, himlSrc->cGrow);
1625 if (himlDst)
1627 SIZE sz;
1629 imagelist_get_bitmap_size(himlSrc, himlSrc->cCurImage, &sz);
1630 BitBlt (himlDst->hdcImage, 0, 0, sz.cx, sz.cy,
1631 himlSrc->hdcImage, 0, 0, SRCCOPY);
1633 if (himlDst->hbmMask)
1634 BitBlt (himlDst->hdcMask, 0, 0, sz.cx, sz.cy,
1635 himlSrc->hdcMask, 0, 0, SRCCOPY);
1637 himlDst->cCurImage = himlSrc->cCurImage;
1638 memcpy( himlDst->item_flags, himlSrc->item_flags, himlDst->cCurImage * sizeof(*himlDst->item_flags) );
1640 return himlDst;
1644 /*************************************************************************
1645 * ImageList_EndDrag [COMCTL32.@]
1647 * Finishes a drag operation.
1649 * PARAMS
1650 * no Parameters
1652 * RETURNS
1653 * Success: TRUE
1654 * Failure: FALSE
1657 VOID WINAPI
1658 ImageList_EndDrag (void)
1660 /* cleanup the InternalDrag struct */
1661 InternalDrag.hwnd = 0;
1662 if (InternalDrag.himl != InternalDrag.himlNoCursor)
1663 ImageList_Destroy (InternalDrag.himlNoCursor);
1664 ImageList_Destroy (InternalDrag.himl);
1665 InternalDrag.himlNoCursor = InternalDrag.himl = 0;
1666 InternalDrag.x= 0;
1667 InternalDrag.y= 0;
1668 InternalDrag.dxHotspot = 0;
1669 InternalDrag.dyHotspot = 0;
1670 InternalDrag.bShow = FALSE;
1671 DeleteObject(InternalDrag.hbmBg);
1672 InternalDrag.hbmBg = 0;
1676 /*************************************************************************
1677 * ImageList_GetBkColor [COMCTL32.@]
1679 * Returns the background color of an image list.
1681 * PARAMS
1682 * himl [I] Image list handle.
1684 * RETURNS
1685 * Success: background color
1686 * Failure: CLR_NONE
1689 COLORREF WINAPI
1690 ImageList_GetBkColor (HIMAGELIST himl)
1692 return himl ? himl->clrBk : CLR_NONE;
1696 /*************************************************************************
1697 * ImageList_GetDragImage [COMCTL32.@]
1699 * Returns the handle to the internal drag image list.
1701 * PARAMS
1702 * ppt [O] Pointer to the drag position. Can be NULL.
1703 * pptHotspot [O] Pointer to the position of the hot spot. Can be NULL.
1705 * RETURNS
1706 * Success: Handle of the drag image list.
1707 * Failure: NULL.
1710 HIMAGELIST WINAPI
1711 ImageList_GetDragImage (POINT *ppt, POINT *pptHotspot)
1713 if (is_valid(InternalDrag.himl)) {
1714 if (ppt) {
1715 ppt->x = InternalDrag.x;
1716 ppt->y = InternalDrag.y;
1718 if (pptHotspot) {
1719 pptHotspot->x = InternalDrag.dxHotspot;
1720 pptHotspot->y = InternalDrag.dyHotspot;
1722 return (InternalDrag.himl);
1725 return NULL;
1729 /*************************************************************************
1730 * ImageList_GetFlags [COMCTL32.@]
1732 * Gets the flags of the specified image list.
1734 * PARAMS
1735 * himl [I] Handle to image list
1737 * RETURNS
1738 * Image list flags.
1740 * BUGS
1741 * Stub.
1744 DWORD WINAPI
1745 ImageList_GetFlags(HIMAGELIST himl)
1747 TRACE("%p\n", himl);
1749 return is_valid(himl) ? himl->flags : 0;
1753 /*************************************************************************
1754 * ImageList_GetIcon [COMCTL32.@]
1756 * Creates an icon from a masked image of an image list.
1758 * PARAMS
1759 * himl [I] handle to image list
1760 * i [I] image index
1761 * flags [I] drawing style flags
1763 * RETURNS
1764 * Success: icon handle
1765 * Failure: NULL
1768 HICON WINAPI
1769 ImageList_GetIcon (HIMAGELIST himl, INT i, UINT fStyle)
1771 ICONINFO ii;
1772 HICON hIcon;
1773 HBITMAP hOldDstBitmap;
1774 HDC hdcDst;
1775 POINT pt;
1777 TRACE("%p %d %d\n", himl, i, fStyle);
1778 if (!is_valid(himl) || (i < 0) || (i >= himl->cCurImage)) return NULL;
1780 ii.fIcon = TRUE;
1781 ii.xHotspot = 0;
1782 ii.yHotspot = 0;
1784 /* create colour bitmap */
1785 hdcDst = GetDC(0);
1786 ii.hbmColor = CreateCompatibleBitmap(hdcDst, himl->cx, himl->cy);
1787 ReleaseDC(0, hdcDst);
1789 hdcDst = CreateCompatibleDC(0);
1791 imagelist_point_from_index( himl, i, &pt );
1793 /* draw mask*/
1794 ii.hbmMask = CreateBitmap (himl->cx, himl->cy, 1, 1, NULL);
1795 hOldDstBitmap = SelectObject (hdcDst, ii.hbmMask);
1796 if (himl->hbmMask) {
1797 BitBlt (hdcDst, 0, 0, himl->cx, himl->cy,
1798 himl->hdcMask, pt.x, pt.y, SRCCOPY);
1800 else
1801 PatBlt (hdcDst, 0, 0, himl->cx, himl->cy, BLACKNESS);
1803 /* draw image*/
1804 SelectObject (hdcDst, ii.hbmColor);
1805 BitBlt (hdcDst, 0, 0, himl->cx, himl->cy,
1806 himl->hdcImage, pt.x, pt.y, SRCCOPY);
1809 * CreateIconIndirect requires us to deselect the bitmaps from
1810 * the DCs before calling
1812 SelectObject(hdcDst, hOldDstBitmap);
1814 hIcon = CreateIconIndirect (&ii);
1816 DeleteObject (ii.hbmMask);
1817 DeleteObject (ii.hbmColor);
1818 DeleteDC (hdcDst);
1820 return hIcon;
1824 /*************************************************************************
1825 * ImageList_GetIconSize [COMCTL32.@]
1827 * Retrieves the size of an image in an image list.
1829 * PARAMS
1830 * himl [I] handle to image list
1831 * cx [O] pointer to the image width.
1832 * cy [O] pointer to the image height.
1834 * RETURNS
1835 * Success: TRUE
1836 * Failure: FALSE
1838 * NOTES
1839 * All images in an image list have the same size.
1842 BOOL WINAPI
1843 ImageList_GetIconSize (HIMAGELIST himl, INT *cx, INT *cy)
1845 if (!is_valid(himl) || !cx || !cy)
1846 return FALSE;
1848 *cx = himl->cx;
1849 *cy = himl->cy;
1851 return TRUE;
1855 /*************************************************************************
1856 * ImageList_GetImageCount [COMCTL32.@]
1858 * Returns the number of images in an image list.
1860 * PARAMS
1861 * himl [I] handle to image list
1863 * RETURNS
1864 * Success: Number of images.
1865 * Failure: 0
1868 INT WINAPI
1869 ImageList_GetImageCount (HIMAGELIST himl)
1871 if (!is_valid(himl))
1872 return 0;
1874 return himl->cCurImage;
1878 /*************************************************************************
1879 * ImageList_GetImageInfo [COMCTL32.@]
1881 * Returns information about an image in an image list.
1883 * PARAMS
1884 * himl [I] handle to image list
1885 * i [I] image index
1886 * pImageInfo [O] pointer to the image information
1888 * RETURNS
1889 * Success: TRUE
1890 * Failure: FALSE
1893 BOOL WINAPI
1894 ImageList_GetImageInfo (HIMAGELIST himl, INT i, IMAGEINFO *pImageInfo)
1896 POINT pt;
1898 if (!is_valid(himl) || (pImageInfo == NULL))
1899 return FALSE;
1900 if ((i < 0) || (i >= himl->cCurImage))
1901 return FALSE;
1903 pImageInfo->hbmImage = himl->hbmImage;
1904 pImageInfo->hbmMask = himl->hbmMask;
1906 imagelist_point_from_index( himl, i, &pt );
1907 pImageInfo->rcImage.top = pt.y;
1908 pImageInfo->rcImage.bottom = pt.y + himl->cy;
1909 pImageInfo->rcImage.left = pt.x;
1910 pImageInfo->rcImage.right = pt.x + himl->cx;
1912 return TRUE;
1916 /*************************************************************************
1917 * ImageList_GetImageRect [COMCTL32.@]
1919 * Retrieves the rectangle of the specified image in an image list.
1921 * PARAMS
1922 * himl [I] handle to image list
1923 * i [I] image index
1924 * lpRect [O] pointer to the image rectangle
1926 * RETURNS
1927 * Success: TRUE
1928 * Failure: FALSE
1930 * NOTES
1931 * This is an UNDOCUMENTED function!!!
1934 BOOL WINAPI
1935 ImageList_GetImageRect (HIMAGELIST himl, INT i, LPRECT lpRect)
1937 POINT pt;
1939 if (!is_valid(himl) || (lpRect == NULL))
1940 return FALSE;
1941 if ((i < 0) || (i >= himl->cCurImage))
1942 return FALSE;
1944 imagelist_point_from_index( himl, i, &pt );
1945 lpRect->left = pt.x;
1946 lpRect->top = pt.y;
1947 lpRect->right = pt.x + himl->cx;
1948 lpRect->bottom = pt.y + himl->cy;
1950 return TRUE;
1954 /*************************************************************************
1955 * ImageList_LoadImage [COMCTL32.@]
1956 * ImageList_LoadImageA [COMCTL32.@]
1958 * Creates an image list from a bitmap, icon or cursor.
1960 * See ImageList_LoadImageW.
1963 HIMAGELIST WINAPI
1964 ImageList_LoadImageA (HINSTANCE hi, LPCSTR lpbmp, INT cx, INT cGrow,
1965 COLORREF clrMask, UINT uType, UINT uFlags)
1967 HIMAGELIST himl;
1968 LPWSTR lpbmpW;
1969 DWORD len;
1971 if (IS_INTRESOURCE(lpbmp))
1972 return ImageList_LoadImageW(hi, (LPCWSTR)lpbmp, cx, cGrow, clrMask,
1973 uType, uFlags);
1975 len = MultiByteToWideChar(CP_ACP, 0, lpbmp, -1, NULL, 0);
1976 lpbmpW = heap_alloc(len * sizeof(WCHAR));
1977 MultiByteToWideChar(CP_ACP, 0, lpbmp, -1, lpbmpW, len);
1979 himl = ImageList_LoadImageW(hi, lpbmpW, cx, cGrow, clrMask, uType, uFlags);
1980 heap_free (lpbmpW);
1981 return himl;
1985 /*************************************************************************
1986 * ImageList_LoadImageW [COMCTL32.@]
1988 * Creates an image list from a bitmap, icon or cursor.
1990 * PARAMS
1991 * hi [I] instance handle
1992 * lpbmp [I] name or id of the image
1993 * cx [I] width of each image
1994 * cGrow [I] number of images to expand
1995 * clrMask [I] mask color
1996 * uType [I] type of image to load
1997 * uFlags [I] loading flags
1999 * RETURNS
2000 * Success: handle to the loaded image list
2001 * Failure: NULL
2003 * SEE
2004 * LoadImage ()
2007 HIMAGELIST WINAPI
2008 ImageList_LoadImageW (HINSTANCE hi, LPCWSTR lpbmp, INT cx, INT cGrow,
2009 COLORREF clrMask, UINT uType, UINT uFlags)
2011 HIMAGELIST himl = NULL;
2012 HANDLE handle;
2013 INT nImageCount;
2015 handle = LoadImageW (hi, lpbmp, uType, 0, 0, uFlags);
2016 if (!handle) {
2017 WARN("Couldn't load image\n");
2018 return NULL;
2021 if (uType == IMAGE_BITMAP) {
2022 DIBSECTION dib;
2023 UINT color;
2025 if (GetObjectW (handle, sizeof(dib), &dib) == sizeof(BITMAP)) color = ILC_COLOR;
2026 else color = dib.dsBm.bmBitsPixel;
2028 /* To match windows behavior, if cx is set to zero and
2029 the flag DI_DEFAULTSIZE is specified, cx becomes the
2030 system metric value for icons. If the flag is not specified
2031 the function sets the size to the height of the bitmap */
2032 if (cx == 0)
2034 if (uFlags & DI_DEFAULTSIZE)
2035 cx = GetSystemMetrics (SM_CXICON);
2036 else
2037 cx = dib.dsBm.bmHeight;
2040 nImageCount = dib.dsBm.bmWidth / cx;
2042 if (clrMask != CLR_NONE) color |= ILC_MASK;
2043 himl = ImageList_Create (cx, dib.dsBm.bmHeight, color, nImageCount, cGrow);
2044 if (!himl) {
2045 DeleteObject (handle);
2046 return NULL;
2048 ImageList_AddMasked (himl, handle, clrMask);
2050 else if ((uType == IMAGE_ICON) || (uType == IMAGE_CURSOR)) {
2051 ICONINFO ii;
2052 BITMAP bmp;
2054 GetIconInfo (handle, &ii);
2055 GetObjectW (ii.hbmColor, sizeof(BITMAP), &bmp);
2056 himl = ImageList_Create (bmp.bmWidth, bmp.bmHeight,
2057 ILC_MASK | ILC_COLOR, 1, cGrow);
2058 if (!himl) {
2059 DeleteObject (ii.hbmColor);
2060 DeleteObject (ii.hbmMask);
2061 DeleteObject (handle);
2062 return NULL;
2064 ImageList_Add (himl, ii.hbmColor, ii.hbmMask);
2065 DeleteObject (ii.hbmColor);
2066 DeleteObject (ii.hbmMask);
2069 DeleteObject (handle);
2071 return himl;
2075 /*************************************************************************
2076 * ImageList_Merge [COMCTL32.@]
2078 * Create an image list containing a merged image from two image lists.
2080 * PARAMS
2081 * himl1 [I] handle to first image list
2082 * i1 [I] first image index
2083 * himl2 [I] handle to second image list
2084 * i2 [I] second image index
2085 * dx [I] X offset of the second image relative to the first.
2086 * dy [I] Y offset of the second image relative to the first.
2088 * RETURNS
2089 * Success: The newly created image list. It contains a single image
2090 * consisting of the second image merged with the first.
2091 * Failure: NULL, if either himl1 or himl2 is invalid.
2093 * NOTES
2094 * - The returned image list should be deleted by the caller using
2095 * ImageList_Destroy() when it is no longer required.
2096 * - If either i1 or i2 is not a valid image index, they will be treated
2097 * as blank images.
2099 HIMAGELIST WINAPI
2100 ImageList_Merge (HIMAGELIST himl1, INT i1, HIMAGELIST himl2, INT i2,
2101 INT dx, INT dy)
2103 HIMAGELIST himlDst = NULL;
2104 INT cxDst, cyDst;
2105 INT xOff1, yOff1, xOff2, yOff2;
2106 POINT pt1, pt2;
2107 INT newFlags;
2109 TRACE("(himl1=%p i1=%d himl2=%p i2=%d dx=%d dy=%d)\n", himl1, i1, himl2,
2110 i2, dx, dy);
2112 if (!is_valid(himl1) || !is_valid(himl2))
2113 return NULL;
2115 if (dx > 0) {
2116 cxDst = max (himl1->cx, dx + himl2->cx);
2117 xOff1 = 0;
2118 xOff2 = dx;
2120 else if (dx < 0) {
2121 cxDst = max (himl2->cx, himl1->cx - dx);
2122 xOff1 = -dx;
2123 xOff2 = 0;
2125 else {
2126 cxDst = max (himl1->cx, himl2->cx);
2127 xOff1 = 0;
2128 xOff2 = 0;
2131 if (dy > 0) {
2132 cyDst = max (himl1->cy, dy + himl2->cy);
2133 yOff1 = 0;
2134 yOff2 = dy;
2136 else if (dy < 0) {
2137 cyDst = max (himl2->cy, himl1->cy - dy);
2138 yOff1 = -dy;
2139 yOff2 = 0;
2141 else {
2142 cyDst = max (himl1->cy, himl2->cy);
2143 yOff1 = 0;
2144 yOff2 = 0;
2147 newFlags = (himl1->flags > himl2->flags ? himl1->flags : himl2->flags) & ILC_COLORDDB;
2148 if (newFlags == ILC_COLORDDB && (himl1->flags & ILC_COLORDDB) == ILC_COLOR16)
2149 newFlags = ILC_COLOR16; /* this is what native (at least v5) does, don't know why */
2150 himlDst = ImageList_Create (cxDst, cyDst, ILC_MASK | newFlags, 1, 1);
2152 if (himlDst)
2154 imagelist_point_from_index( himl1, i1, &pt1 );
2155 imagelist_point_from_index( himl2, i2, &pt2 );
2157 /* copy image */
2158 BitBlt (himlDst->hdcImage, 0, 0, cxDst, cyDst, himl1->hdcImage, 0, 0, BLACKNESS);
2159 if (i1 >= 0 && i1 < himl1->cCurImage)
2160 BitBlt (himlDst->hdcImage, xOff1, yOff1, himl1->cx, himl1->cy, himl1->hdcImage, pt1.x, pt1.y, SRCCOPY);
2161 if (i2 >= 0 && i2 < himl2->cCurImage)
2163 if (himl2->flags & ILC_MASK)
2165 BitBlt (himlDst->hdcImage, xOff2, yOff2, himl2->cx, himl2->cy, himl2->hdcMask , pt2.x, pt2.y, SRCAND);
2166 BitBlt (himlDst->hdcImage, xOff2, yOff2, himl2->cx, himl2->cy, himl2->hdcImage, pt2.x, pt2.y, SRCPAINT);
2168 else
2169 BitBlt (himlDst->hdcImage, xOff2, yOff2, himl2->cx, himl2->cy, himl2->hdcImage, pt2.x, pt2.y, SRCCOPY);
2172 /* copy mask */
2173 BitBlt (himlDst->hdcMask, 0, 0, cxDst, cyDst, himl1->hdcMask, 0, 0, WHITENESS);
2174 if (i1 >= 0 && i1 < himl1->cCurImage)
2175 BitBlt (himlDst->hdcMask, xOff1, yOff1, himl1->cx, himl1->cy, himl1->hdcMask, pt1.x, pt1.y, SRCCOPY);
2176 if (i2 >= 0 && i2 < himl2->cCurImage)
2177 BitBlt (himlDst->hdcMask, xOff2, yOff2, himl2->cx, himl2->cy, himl2->hdcMask, pt2.x, pt2.y, SRCAND);
2179 himlDst->cCurImage = 1;
2182 return himlDst;
2186 /* helper for ImageList_Read, see comments below */
2187 static void *read_bitmap(IStream *pstm, BITMAPINFO *bmi)
2189 BITMAPFILEHEADER bmfh;
2190 int bitsperpixel, palspace;
2191 void *bits;
2193 if (FAILED(IStream_Read ( pstm, &bmfh, sizeof(bmfh), NULL)))
2194 return NULL;
2196 if (bmfh.bfType != (('M'<<8)|'B'))
2197 return NULL;
2199 if (FAILED(IStream_Read ( pstm, &bmi->bmiHeader, sizeof(bmi->bmiHeader), NULL)))
2200 return NULL;
2202 if ((bmi->bmiHeader.biSize != sizeof(bmi->bmiHeader)))
2203 return NULL;
2205 TRACE("width %u, height %u, planes %u, bpp %u\n",
2206 bmi->bmiHeader.biWidth, bmi->bmiHeader.biHeight,
2207 bmi->bmiHeader.biPlanes, bmi->bmiHeader.biBitCount);
2209 bitsperpixel = bmi->bmiHeader.biPlanes * bmi->bmiHeader.biBitCount;
2210 if (bitsperpixel<=8)
2211 palspace = (1<<bitsperpixel)*sizeof(RGBQUAD);
2212 else
2213 palspace = 0;
2215 bmi->bmiHeader.biSizeImage = get_dib_image_size( bmi );
2217 /* read the palette right after the end of the bitmapinfoheader */
2218 if (palspace && FAILED(IStream_Read(pstm, bmi->bmiColors, palspace, NULL)))
2219 return NULL;
2221 bits = heap_alloc_zero(bmi->bmiHeader.biSizeImage);
2222 if (!bits) return NULL;
2224 if (FAILED(IStream_Read(pstm, bits, bmi->bmiHeader.biSizeImage, NULL)))
2226 heap_free(bits);
2227 return NULL;
2229 return bits;
2232 /*************************************************************************
2233 * ImageList_Read [COMCTL32.@]
2235 * Reads an image list from a stream.
2237 * PARAMS
2238 * pstm [I] pointer to a stream
2240 * RETURNS
2241 * Success: handle to image list
2242 * Failure: NULL
2244 * The format is like this:
2245 * ILHEAD ilheadstruct;
2247 * for the color image part:
2248 * BITMAPFILEHEADER bmfh;
2249 * BITMAPINFOHEADER bmih;
2250 * only if it has a palette:
2251 * RGBQUAD rgbs[nr_of_paletted_colors];
2253 * BYTE colorbits[imagesize];
2255 * the following only if the ILC_MASK bit is set in ILHEAD.ilFlags:
2256 * BITMAPFILEHEADER bmfh_mask;
2257 * BITMAPINFOHEADER bmih_mask;
2258 * only if it has a palette (it usually does not):
2259 * RGBQUAD rgbs[nr_of_paletted_colors];
2261 * BYTE maskbits[imagesize];
2263 HIMAGELIST WINAPI ImageList_Read(IStream *pstm)
2265 char image_buf[sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * 256];
2266 char mask_buf[sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * 256];
2267 BITMAPINFO *image_info = (BITMAPINFO *)image_buf;
2268 BITMAPINFO *mask_info = (BITMAPINFO *)mask_buf;
2269 void *image_bits, *mask_bits = NULL;
2270 ILHEAD ilHead;
2271 HIMAGELIST himl;
2272 unsigned int i;
2274 TRACE("%p\n", pstm);
2276 if (FAILED(IStream_Read (pstm, &ilHead, sizeof(ILHEAD), NULL)))
2277 return NULL;
2278 if (ilHead.usMagic != (('L' << 8) | 'I'))
2279 return NULL;
2280 if (ilHead.usVersion != 0x101) /* probably version? */
2281 return NULL;
2283 TRACE("cx %u, cy %u, flags 0x%04x, cCurImage %u, cMaxImage %u\n",
2284 ilHead.cx, ilHead.cy, ilHead.flags, ilHead.cCurImage, ilHead.cMaxImage);
2286 himl = ImageList_Create(ilHead.cx, ilHead.cy, ilHead.flags, ilHead.cMaxImage, ilHead.cGrow);
2287 if (!himl)
2288 return NULL;
2290 if (!(image_bits = read_bitmap(pstm, image_info)))
2292 WARN("failed to read bitmap from stream\n");
2293 return NULL;
2295 if (ilHead.flags & ILC_MASK)
2297 if (!(mask_bits = read_bitmap(pstm, mask_info)))
2299 WARN("failed to read mask bitmap from stream\n");
2300 return NULL;
2303 else mask_info = NULL;
2305 if ((himl->flags & 0xfe) == ILC_COLOR32 && image_info->bmiHeader.biBitCount == 32)
2307 DWORD *ptr = image_bits;
2308 BYTE *mask_ptr = mask_bits;
2309 int stride = himl->cy * image_info->bmiHeader.biWidth;
2311 if (image_info->bmiHeader.biHeight > 0) /* bottom-up */
2313 ptr += image_info->bmiHeader.biHeight * image_info->bmiHeader.biWidth - stride;
2314 mask_ptr += (image_info->bmiHeader.biHeight * image_info->bmiHeader.biWidth - stride) / 8;
2315 stride = -stride;
2316 image_info->bmiHeader.biHeight = himl->cy;
2318 else image_info->bmiHeader.biHeight = -himl->cy;
2320 for (i = 0; i < ilHead.cCurImage; i += TILE_COUNT)
2322 add_dib_bits( himl, i, min( ilHead.cCurImage - i, TILE_COUNT ),
2323 himl->cx, himl->cy, image_info, mask_info, ptr, mask_ptr );
2324 ptr += stride;
2325 mask_ptr += stride / 8;
2328 else
2330 StretchDIBits( himl->hdcImage, 0, 0, image_info->bmiHeader.biWidth, image_info->bmiHeader.biHeight,
2331 0, 0, image_info->bmiHeader.biWidth, image_info->bmiHeader.biHeight,
2332 image_bits, image_info, DIB_RGB_COLORS, SRCCOPY);
2333 if (mask_info)
2334 StretchDIBits( himl->hdcMask, 0, 0, mask_info->bmiHeader.biWidth, mask_info->bmiHeader.biHeight,
2335 0, 0, mask_info->bmiHeader.biWidth, mask_info->bmiHeader.biHeight,
2336 mask_bits, mask_info, DIB_RGB_COLORS, SRCCOPY);
2338 heap_free( image_bits );
2339 heap_free( mask_bits );
2341 himl->cCurImage = ilHead.cCurImage;
2342 himl->cMaxImage = ilHead.cMaxImage;
2344 ImageList_SetBkColor(himl,ilHead.bkcolor);
2345 for (i=0;i<4;i++)
2346 ImageList_SetOverlayImage(himl,ilHead.ovls[i],i+1);
2347 return himl;
2351 /*************************************************************************
2352 * ImageList_Remove [COMCTL32.@]
2354 * Removes an image from an image list
2356 * PARAMS
2357 * himl [I] image list handle
2358 * i [I] image index
2360 * RETURNS
2361 * Success: TRUE
2362 * Failure: FALSE
2364 * FIXME: as the image list storage test shows, native comctl32 simply shifts
2365 * images without creating a new bitmap.
2367 BOOL WINAPI
2368 ImageList_Remove (HIMAGELIST himl, INT i)
2370 HBITMAP hbmNewImage, hbmNewMask;
2371 HDC hdcBmp;
2372 SIZE sz;
2374 TRACE("(himl=%p i=%d)\n", himl, i);
2376 if (!is_valid(himl)) {
2377 ERR("Invalid image list handle!\n");
2378 return FALSE;
2381 if ((i < -1) || (i >= himl->cCurImage)) {
2382 TRACE("index out of range! %d\n", i);
2383 return FALSE;
2386 if (i == -1) {
2387 INT nCount;
2389 /* remove all */
2390 if (himl->cCurImage == 0) {
2391 /* remove all on empty ImageList is allowed */
2392 TRACE("remove all on empty ImageList!\n");
2393 return TRUE;
2396 himl->cMaxImage = himl->cGrow;
2397 himl->cCurImage = 0;
2398 for (nCount = 0; nCount < MAX_OVERLAYIMAGE; nCount++)
2399 himl->nOvlIdx[nCount] = -1;
2401 heap_free( himl->item_flags );
2402 himl->item_flags = heap_alloc_zero( himl->cMaxImage * sizeof(*himl->item_flags) );
2404 hbmNewImage = ImageList_CreateImage(himl->hdcImage, himl, himl->cMaxImage);
2405 SelectObject (himl->hdcImage, hbmNewImage);
2406 DeleteObject (himl->hbmImage);
2407 himl->hbmImage = hbmNewImage;
2409 if (himl->hbmMask) {
2411 imagelist_get_bitmap_size(himl, himl->cMaxImage, &sz);
2412 hbmNewMask = CreateBitmap (sz.cx, sz.cy, 1, 1, NULL);
2413 SelectObject (himl->hdcMask, hbmNewMask);
2414 DeleteObject (himl->hbmMask);
2415 himl->hbmMask = hbmNewMask;
2418 else {
2419 /* delete one image */
2420 TRACE("Remove single image! %d\n", i);
2422 /* create new bitmap(s) */
2423 TRACE(" - Number of images: %d / %d (Old/New)\n",
2424 himl->cCurImage, himl->cCurImage - 1);
2426 hbmNewImage = ImageList_CreateImage(himl->hdcImage, himl, himl->cMaxImage);
2428 imagelist_get_bitmap_size(himl, himl->cMaxImage, &sz );
2429 if (himl->hbmMask)
2430 hbmNewMask = CreateBitmap (sz.cx, sz.cy, 1, 1, NULL);
2431 else
2432 hbmNewMask = 0; /* Just to keep compiler happy! */
2434 hdcBmp = CreateCompatibleDC (0);
2436 /* copy all images and masks prior to the "removed" image */
2437 if (i > 0) {
2438 TRACE("Pre image copy: Copy %d images\n", i);
2440 SelectObject (hdcBmp, hbmNewImage);
2441 imagelist_copy_images( himl, himl->hdcImage, hdcBmp, 0, i, 0 );
2443 if (himl->hbmMask) {
2444 SelectObject (hdcBmp, hbmNewMask);
2445 imagelist_copy_images( himl, himl->hdcMask, hdcBmp, 0, i, 0 );
2449 /* copy all images and masks behind the removed image */
2450 if (i < himl->cCurImage - 1) {
2451 TRACE("Post image copy!\n");
2453 SelectObject (hdcBmp, hbmNewImage);
2454 imagelist_copy_images( himl, himl->hdcImage, hdcBmp, i + 1,
2455 (himl->cCurImage - i), i );
2457 if (himl->hbmMask) {
2458 SelectObject (hdcBmp, hbmNewMask);
2459 imagelist_copy_images( himl, himl->hdcMask, hdcBmp, i + 1,
2460 (himl->cCurImage - i), i );
2464 DeleteDC (hdcBmp);
2466 /* delete old images and insert new ones */
2467 SelectObject (himl->hdcImage, hbmNewImage);
2468 DeleteObject (himl->hbmImage);
2469 himl->hbmImage = hbmNewImage;
2470 if (himl->hbmMask) {
2471 SelectObject (himl->hdcMask, hbmNewMask);
2472 DeleteObject (himl->hbmMask);
2473 himl->hbmMask = hbmNewMask;
2476 himl->cCurImage--;
2479 return TRUE;
2483 /*************************************************************************
2484 * ImageList_Replace [COMCTL32.@]
2486 * Replaces an image in an image list with a new image.
2488 * PARAMS
2489 * himl [I] handle to image list
2490 * i [I] image index
2491 * hbmImage [I] handle to image bitmap
2492 * hbmMask [I] handle to mask bitmap. Can be NULL.
2494 * RETURNS
2495 * Success: TRUE
2496 * Failure: FALSE
2499 BOOL WINAPI
2500 ImageList_Replace (HIMAGELIST himl, INT i, HBITMAP hbmImage,
2501 HBITMAP hbmMask)
2503 HDC hdcImage;
2504 BITMAP bmp;
2505 POINT pt;
2507 TRACE("%p %d %p %p\n", himl, i, hbmImage, hbmMask);
2509 if (!is_valid(himl)) {
2510 ERR("Invalid image list handle!\n");
2511 return FALSE;
2514 if ((i >= himl->cMaxImage) || (i < 0)) {
2515 ERR("Invalid image index!\n");
2516 return FALSE;
2519 if (!GetObjectW(hbmImage, sizeof(BITMAP), &bmp))
2520 return FALSE;
2522 hdcImage = CreateCompatibleDC (0);
2524 /* Replace Image */
2525 SelectObject (hdcImage, hbmImage);
2527 if (add_with_alpha( himl, hdcImage, i, 1, bmp.bmWidth, bmp.bmHeight, hbmImage, hbmMask ))
2528 goto done;
2530 imagelist_point_from_index(himl, i, &pt);
2531 StretchBlt (himl->hdcImage, pt.x, pt.y, himl->cx, himl->cy,
2532 hdcImage, 0, 0, bmp.bmWidth, bmp.bmHeight, SRCCOPY);
2534 if (himl->hbmMask)
2536 HDC hdcTemp;
2537 HBITMAP hOldBitmapTemp;
2539 hdcTemp = CreateCompatibleDC(0);
2540 hOldBitmapTemp = SelectObject(hdcTemp, hbmMask);
2542 StretchBlt (himl->hdcMask, pt.x, pt.y, himl->cx, himl->cy,
2543 hdcTemp, 0, 0, bmp.bmWidth, bmp.bmHeight, SRCCOPY);
2544 SelectObject(hdcTemp, hOldBitmapTemp);
2545 DeleteDC(hdcTemp);
2547 /* Remove the background from the image
2549 BitBlt (himl->hdcImage, pt.x, pt.y, bmp.bmWidth, bmp.bmHeight,
2550 himl->hdcMask, pt.x, pt.y, 0x220326); /* NOTSRCAND */
2553 done:
2554 DeleteDC (hdcImage);
2556 return TRUE;
2560 /*************************************************************************
2561 * ImageList_ReplaceIcon [COMCTL32.@]
2563 * Replaces an image in an image list using an icon.
2565 * PARAMS
2566 * himl [I] handle to image list
2567 * i [I] image index
2568 * hIcon [I] handle to icon
2570 * RETURNS
2571 * Success: index of the replaced image
2572 * Failure: -1
2575 INT WINAPI
2576 ImageList_ReplaceIcon (HIMAGELIST himl, INT nIndex, HICON hIcon)
2578 HICON hBestFitIcon;
2579 ICONINFO ii;
2580 BITMAP bmp;
2581 BOOL ret;
2582 POINT pt;
2584 TRACE("(%p %d %p)\n", himl, nIndex, hIcon);
2586 if (!is_valid(himl)) {
2587 ERR("invalid image list\n");
2588 return -1;
2590 if ((nIndex >= himl->cMaxImage) || (nIndex < -1)) {
2591 ERR("invalid image index %d / %d\n", nIndex, himl->cMaxImage);
2592 return -1;
2595 hBestFitIcon = CopyImage(
2596 hIcon, IMAGE_ICON,
2597 himl->cx, himl->cy,
2598 LR_COPYFROMRESOURCE);
2599 /* the above will fail if the icon wasn't loaded from a resource, so try
2600 * again without LR_COPYFROMRESOURCE flag */
2601 if (!hBestFitIcon)
2602 hBestFitIcon = CopyImage(
2603 hIcon, IMAGE_ICON,
2604 himl->cx, himl->cy,
2606 if (!hBestFitIcon)
2607 return -1;
2609 if (nIndex == -1) {
2610 if (himl->cCurImage + 1 >= himl->cMaxImage)
2611 IMAGELIST_InternalExpandBitmaps(himl, 1);
2613 nIndex = himl->cCurImage;
2614 himl->cCurImage++;
2617 if ((himl->flags & 0xfe) == ILC_COLOR32 && GetIconInfo (hBestFitIcon, &ii))
2619 HDC hdcImage = CreateCompatibleDC( 0 );
2620 GetObjectW (ii.hbmMask, sizeof(BITMAP), &bmp);
2622 if (!ii.hbmColor)
2624 UINT height = bmp.bmHeight / 2;
2625 HDC hdcMask = CreateCompatibleDC( 0 );
2626 HBITMAP color = CreateBitmap( bmp.bmWidth, height, 1, 1, NULL );
2627 SelectObject( hdcImage, color );
2628 SelectObject( hdcMask, ii.hbmMask );
2629 BitBlt( hdcImage, 0, 0, bmp.bmWidth, height, hdcMask, 0, height, SRCCOPY );
2630 ret = add_with_alpha( himl, hdcImage, nIndex, 1, bmp.bmWidth, height, color, ii.hbmMask );
2631 DeleteDC( hdcMask );
2632 DeleteObject( color );
2634 else ret = add_with_alpha( himl, hdcImage, nIndex, 1, bmp.bmWidth, bmp.bmHeight,
2635 ii.hbmColor, ii.hbmMask );
2637 DeleteDC( hdcImage );
2638 DeleteObject (ii.hbmMask);
2639 if (ii.hbmColor) DeleteObject (ii.hbmColor);
2640 if (ret) goto done;
2643 imagelist_point_from_index(himl, nIndex, &pt);
2645 if (himl->hbmMask)
2647 DrawIconEx( himl->hdcImage, pt.x, pt.y, hBestFitIcon, himl->cx, himl->cy, 0, 0, DI_IMAGE );
2648 PatBlt( himl->hdcMask, pt.x, pt.y, himl->cx, himl->cy, WHITENESS );
2649 DrawIconEx( himl->hdcMask, pt.x, pt.y, hBestFitIcon, himl->cx, himl->cy, 0, 0, DI_MASK );
2651 else
2653 COLORREF color = himl->clrBk != CLR_NONE ? himl->clrBk : comctl32_color.clrWindow;
2654 HBRUSH brush = CreateSolidBrush( GetNearestColor( himl->hdcImage, color ));
2656 SelectObject( himl->hdcImage, brush );
2657 PatBlt( himl->hdcImage, pt.x, pt.y, himl->cx, himl->cy, PATCOPY );
2658 SelectObject( himl->hdcImage, GetStockObject(BLACK_BRUSH) );
2659 DeleteObject( brush );
2660 DrawIconEx( himl->hdcImage, pt.x, pt.y, hBestFitIcon, himl->cx, himl->cy, 0, 0, DI_NORMAL );
2663 done:
2664 DestroyIcon(hBestFitIcon);
2666 TRACE("Insert index = %d, himl->cCurImage = %d\n", nIndex, himl->cCurImage);
2667 return nIndex;
2671 /*************************************************************************
2672 * ImageList_SetBkColor [COMCTL32.@]
2674 * Sets the background color of an image list.
2676 * PARAMS
2677 * himl [I] handle to image list
2678 * clrBk [I] background color
2680 * RETURNS
2681 * Success: previous background color
2682 * Failure: CLR_NONE
2685 COLORREF WINAPI
2686 ImageList_SetBkColor (HIMAGELIST himl, COLORREF clrBk)
2688 COLORREF clrOldBk;
2690 if (!is_valid(himl))
2691 return CLR_NONE;
2693 clrOldBk = himl->clrBk;
2694 himl->clrBk = clrBk;
2695 return clrOldBk;
2699 /*************************************************************************
2700 * ImageList_SetDragCursorImage [COMCTL32.@]
2702 * Combines the specified image with the current drag image
2704 * PARAMS
2705 * himlDrag [I] handle to drag image list
2706 * iDrag [I] drag image index
2707 * dxHotspot [I] X position of the hot spot
2708 * dyHotspot [I] Y position of the hot spot
2710 * RETURNS
2711 * Success: TRUE
2712 * Failure: FALSE
2714 * NOTES
2715 * - The names dxHotspot, dyHotspot are misleading because they have nothing
2716 * to do with a hotspot but are only the offset of the origin of the new
2717 * image relative to the origin of the old image.
2719 * - When this function is called and the drag image is visible, a
2720 * short flickering occurs but this matches the Win9x behavior. It is
2721 * possible to fix the flickering using code like in ImageList_DragMove.
2724 BOOL WINAPI
2725 ImageList_SetDragCursorImage (HIMAGELIST himlDrag, INT iDrag,
2726 INT dxHotspot, INT dyHotspot)
2728 HIMAGELIST himlTemp;
2729 BOOL visible;
2731 if (!is_valid(InternalDrag.himl) || !is_valid(himlDrag))
2732 return FALSE;
2734 TRACE(" dxH=%d dyH=%d nX=%d nY=%d\n",
2735 dxHotspot, dyHotspot, InternalDrag.dxHotspot, InternalDrag.dyHotspot);
2737 visible = InternalDrag.bShow;
2739 himlTemp = ImageList_Merge (InternalDrag.himlNoCursor, 0, himlDrag, iDrag,
2740 dxHotspot, dyHotspot);
2742 if (visible) {
2743 /* hide the drag image */
2744 ImageList_DragShowNolock(FALSE);
2746 if ((InternalDrag.himl->cx != himlTemp->cx) ||
2747 (InternalDrag.himl->cy != himlTemp->cy)) {
2748 /* the size of the drag image changed, invalidate the buffer */
2749 DeleteObject(InternalDrag.hbmBg);
2750 InternalDrag.hbmBg = 0;
2753 if (InternalDrag.himl != InternalDrag.himlNoCursor)
2754 ImageList_Destroy (InternalDrag.himl);
2755 InternalDrag.himl = himlTemp;
2757 if (visible) {
2758 /* show the drag image */
2759 ImageList_DragShowNolock(TRUE);
2762 return TRUE;
2766 /*************************************************************************
2767 * ImageList_SetFilter [COMCTL32.@]
2769 * Sets a filter (or does something completely different)!!???
2770 * It removes 12 Bytes from the stack (3 Parameters).
2772 * PARAMS
2773 * himl [I] SHOULD be a handle to image list
2774 * i [I] COULD be an index?
2775 * dwFilter [I] ???
2777 * RETURNS
2778 * Success: TRUE ???
2779 * Failure: FALSE ???
2781 * BUGS
2782 * This is an UNDOCUMENTED function!!!!
2783 * empty stub.
2786 BOOL WINAPI
2787 ImageList_SetFilter (HIMAGELIST himl, INT i, DWORD dwFilter)
2789 FIXME("(%p 0x%x 0x%x):empty stub!\n", himl, i, dwFilter);
2791 return FALSE;
2795 /*************************************************************************
2796 * ImageList_SetFlags [COMCTL32.@]
2798 * Sets the image list flags.
2800 * PARAMS
2801 * himl [I] Handle to image list
2802 * flags [I] Flags to set
2804 * RETURNS
2805 * Old flags?
2807 * BUGS
2808 * Stub.
2811 DWORD WINAPI
2812 ImageList_SetFlags(HIMAGELIST himl, DWORD flags)
2814 FIXME("(%p %08x):empty stub\n", himl, flags);
2815 return 0;
2819 /*************************************************************************
2820 * ImageList_SetIconSize [COMCTL32.@]
2822 * Sets the image size of the bitmap and deletes all images.
2824 * PARAMS
2825 * himl [I] handle to image list
2826 * cx [I] image width
2827 * cy [I] image height
2829 * RETURNS
2830 * Success: TRUE
2831 * Failure: FALSE
2834 BOOL WINAPI
2835 ImageList_SetIconSize (HIMAGELIST himl, INT cx, INT cy)
2837 INT nCount;
2838 HBITMAP hbmNew;
2840 if (!is_valid(himl))
2841 return FALSE;
2843 /* remove all images */
2844 himl->cMaxImage = himl->cInitial + 1;
2845 himl->cCurImage = 0;
2846 himl->cx = cx;
2847 himl->cy = cy;
2849 /* initialize overlay mask indices */
2850 for (nCount = 0; nCount < MAX_OVERLAYIMAGE; nCount++)
2851 himl->nOvlIdx[nCount] = -1;
2853 hbmNew = ImageList_CreateImage(himl->hdcImage, himl, himl->cMaxImage);
2854 SelectObject (himl->hdcImage, hbmNew);
2855 DeleteObject (himl->hbmImage);
2856 himl->hbmImage = hbmNew;
2858 if (himl->hbmMask) {
2859 SIZE sz;
2860 imagelist_get_bitmap_size(himl, himl->cMaxImage, &sz);
2861 hbmNew = CreateBitmap (sz.cx, sz.cy, 1, 1, NULL);
2862 SelectObject (himl->hdcMask, hbmNew);
2863 DeleteObject (himl->hbmMask);
2864 himl->hbmMask = hbmNew;
2867 return TRUE;
2871 /*************************************************************************
2872 * ImageList_SetImageCount [COMCTL32.@]
2874 * Resizes an image list to the specified number of images.
2876 * PARAMS
2877 * himl [I] handle to image list
2878 * iImageCount [I] number of images in the image list
2880 * RETURNS
2881 * Success: TRUE
2882 * Failure: FALSE
2885 BOOL WINAPI
2886 ImageList_SetImageCount (HIMAGELIST himl, UINT iImageCount)
2888 HDC hdcBitmap;
2889 HBITMAP hbmNewBitmap, hbmOld;
2890 INT nNewCount, nCopyCount;
2892 TRACE("%p %d\n",himl,iImageCount);
2894 if (!is_valid(himl))
2895 return FALSE;
2897 nNewCount = iImageCount + 1;
2898 nCopyCount = min(himl->cCurImage, iImageCount);
2900 hdcBitmap = CreateCompatibleDC (0);
2902 hbmNewBitmap = ImageList_CreateImage(hdcBitmap, himl, nNewCount);
2904 if (hbmNewBitmap != 0)
2906 hbmOld = SelectObject (hdcBitmap, hbmNewBitmap);
2907 imagelist_copy_images( himl, himl->hdcImage, hdcBitmap, 0, nCopyCount, 0 );
2908 SelectObject (hdcBitmap, hbmOld);
2910 /* FIXME: delete 'empty' image space? */
2912 SelectObject (himl->hdcImage, hbmNewBitmap);
2913 DeleteObject (himl->hbmImage);
2914 himl->hbmImage = hbmNewBitmap;
2916 else
2917 ERR("Could not create new image bitmap!\n");
2919 if (himl->hbmMask)
2921 SIZE sz;
2922 imagelist_get_bitmap_size( himl, nNewCount, &sz );
2923 hbmNewBitmap = CreateBitmap (sz.cx, sz.cy, 1, 1, NULL);
2924 if (hbmNewBitmap != 0)
2926 hbmOld = SelectObject (hdcBitmap, hbmNewBitmap);
2927 imagelist_copy_images( himl, himl->hdcMask, hdcBitmap, 0, nCopyCount, 0 );
2928 SelectObject (hdcBitmap, hbmOld);
2930 /* FIXME: delete 'empty' image space? */
2932 SelectObject (himl->hdcMask, hbmNewBitmap);
2933 DeleteObject (himl->hbmMask);
2934 himl->hbmMask = hbmNewBitmap;
2936 else
2937 ERR("Could not create new mask bitmap!\n");
2940 DeleteDC (hdcBitmap);
2942 himl->item_flags = HeapReAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, himl->item_flags,
2943 nNewCount * sizeof(*himl->item_flags));
2945 /* Update max image count and current image count */
2946 himl->cMaxImage = nNewCount;
2947 himl->cCurImage = iImageCount;
2949 return TRUE;
2953 /*************************************************************************
2954 * ImageList_SetOverlayImage [COMCTL32.@]
2956 * Assigns an overlay mask index to an existing image in an image list.
2958 * PARAMS
2959 * himl [I] handle to image list
2960 * iImage [I] image index
2961 * iOverlay [I] overlay mask index
2963 * RETURNS
2964 * Success: TRUE
2965 * Failure: FALSE
2968 BOOL WINAPI
2969 ImageList_SetOverlayImage (HIMAGELIST himl, INT iImage, INT iOverlay)
2971 if (!is_valid(himl))
2972 return FALSE;
2973 if ((iOverlay < 1) || (iOverlay > MAX_OVERLAYIMAGE))
2974 return FALSE;
2975 if ((iImage!=-1) && ((iImage < 0) || (iImage > himl->cCurImage)))
2976 return FALSE;
2977 himl->nOvlIdx[iOverlay - 1] = iImage;
2978 return TRUE;
2983 /* helper for ImageList_Write - write bitmap to pstm
2984 * currently everything is written as 24 bit RGB, except masks
2986 static BOOL _write_bitmap(HBITMAP hBitmap, IStream *pstm)
2988 LPBITMAPFILEHEADER bmfh;
2989 LPBITMAPINFOHEADER bmih;
2990 LPBYTE data = NULL, lpBits;
2991 BITMAP bm;
2992 INT bitCount, sizeImage, offBits, totalSize;
2993 HDC xdc;
2994 BOOL result = FALSE;
2996 if (!GetObjectW(hBitmap, sizeof(BITMAP), &bm))
2997 return FALSE;
2999 bitCount = bm.bmBitsPixel;
3000 sizeImage = get_dib_stride(bm.bmWidth, bitCount) * bm.bmHeight;
3002 totalSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
3003 if(bitCount <= 8)
3004 totalSize += (1 << bitCount) * sizeof(RGBQUAD);
3005 offBits = totalSize;
3006 totalSize += sizeImage;
3008 data = heap_alloc_zero(totalSize);
3009 bmfh = (LPBITMAPFILEHEADER)data;
3010 bmih = (LPBITMAPINFOHEADER)(data + sizeof(BITMAPFILEHEADER));
3011 lpBits = data + offBits;
3013 /* setup BITMAPFILEHEADER */
3014 bmfh->bfType = (('M' << 8) | 'B');
3015 bmfh->bfSize = offBits;
3016 bmfh->bfReserved1 = 0;
3017 bmfh->bfReserved2 = 0;
3018 bmfh->bfOffBits = offBits;
3020 /* setup BITMAPINFOHEADER */
3021 bmih->biSize = sizeof(BITMAPINFOHEADER);
3022 bmih->biWidth = bm.bmWidth;
3023 bmih->biHeight = bm.bmHeight;
3024 bmih->biPlanes = 1;
3025 bmih->biBitCount = bitCount;
3026 bmih->biCompression = BI_RGB;
3027 bmih->biSizeImage = sizeImage;
3028 bmih->biXPelsPerMeter = 0;
3029 bmih->biYPelsPerMeter = 0;
3030 bmih->biClrUsed = 0;
3031 bmih->biClrImportant = 0;
3033 xdc = GetDC(0);
3034 result = GetDIBits(xdc, hBitmap, 0, bm.bmHeight, lpBits, (BITMAPINFO *)bmih, DIB_RGB_COLORS) == bm.bmHeight;
3035 ReleaseDC(0, xdc);
3036 if (!result)
3037 goto failed;
3039 TRACE("width %u, height %u, planes %u, bpp %u\n",
3040 bmih->biWidth, bmih->biHeight,
3041 bmih->biPlanes, bmih->biBitCount);
3043 if(FAILED(IStream_Write(pstm, data, totalSize, NULL)))
3044 goto failed;
3046 result = TRUE;
3048 failed:
3049 heap_free(data);
3051 return result;
3055 /*************************************************************************
3056 * ImageList_Write [COMCTL32.@]
3058 * Writes an image list to a stream.
3060 * PARAMS
3061 * himl [I] handle to image list
3062 * pstm [O] Pointer to a stream.
3064 * RETURNS
3065 * Success: TRUE
3066 * Failure: FALSE
3068 * BUGS
3069 * probably.
3072 BOOL WINAPI ImageList_Write(HIMAGELIST himl, IStream *pstm)
3074 ILHEAD ilHead;
3075 int i;
3077 TRACE("%p %p\n", himl, pstm);
3079 if (!is_valid(himl))
3080 return FALSE;
3082 ilHead.usMagic = (('L' << 8) | 'I');
3083 ilHead.usVersion = 0x101;
3084 ilHead.cCurImage = himl->cCurImage;
3085 ilHead.cMaxImage = himl->cMaxImage;
3086 ilHead.cGrow = himl->cGrow;
3087 ilHead.cx = himl->cx;
3088 ilHead.cy = himl->cy;
3089 ilHead.bkcolor = himl->clrBk;
3090 ilHead.flags = himl->flags;
3091 for(i = 0; i < 4; i++) {
3092 ilHead.ovls[i] = himl->nOvlIdx[i];
3095 TRACE("cx %u, cy %u, flags 0x04%x, cCurImage %u, cMaxImage %u\n",
3096 ilHead.cx, ilHead.cy, ilHead.flags, ilHead.cCurImage, ilHead.cMaxImage);
3098 if(FAILED(IStream_Write(pstm, &ilHead, sizeof(ILHEAD), NULL)))
3099 return FALSE;
3101 /* write the bitmap */
3102 if(!_write_bitmap(himl->hbmImage, pstm))
3103 return FALSE;
3105 /* write the mask if we have one */
3106 if(himl->flags & ILC_MASK) {
3107 if(!_write_bitmap(himl->hbmMask, pstm))
3108 return FALSE;
3111 return TRUE;
3115 static HBITMAP ImageList_CreateImage(HDC hdc, HIMAGELIST himl, UINT count)
3117 HBITMAP hbmNewBitmap;
3118 UINT ilc = (himl->flags & 0xFE);
3119 SIZE sz;
3121 imagelist_get_bitmap_size( himl, count, &sz );
3123 if ((ilc >= ILC_COLOR4 && ilc <= ILC_COLOR32) || ilc == ILC_COLOR)
3125 char buffer[sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD)];
3126 BITMAPINFO *bmi = (BITMAPINFO *)buffer;
3128 TRACE("Creating DIBSection %d x %d, %d Bits per Pixel\n",
3129 sz.cx, sz.cy, himl->uBitsPixel);
3131 memset( buffer, 0, sizeof(buffer) );
3132 bmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
3133 bmi->bmiHeader.biWidth = sz.cx;
3134 bmi->bmiHeader.biHeight = sz.cy;
3135 bmi->bmiHeader.biPlanes = 1;
3136 bmi->bmiHeader.biBitCount = himl->uBitsPixel;
3137 bmi->bmiHeader.biCompression = BI_RGB;
3139 if (himl->uBitsPixel <= ILC_COLOR8)
3141 if (!himl->color_table_set)
3143 /* retrieve the default color map */
3144 HBITMAP tmp = CreateBitmap( 1, 1, 1, 1, NULL );
3145 GetDIBits( hdc, tmp, 0, 0, NULL, bmi, DIB_RGB_COLORS );
3146 DeleteObject( tmp );
3147 if (ilc == ILC_COLOR4)
3149 RGBQUAD tmp;
3150 tmp = bmi->bmiColors[7];
3151 bmi->bmiColors[7] = bmi->bmiColors[8];
3152 bmi->bmiColors[8] = tmp;
3155 else
3157 GetDIBColorTable(himl->hdcImage, 0, 1 << himl->uBitsPixel, bmi->bmiColors);
3160 hbmNewBitmap = CreateDIBSection(hdc, bmi, DIB_RGB_COLORS, NULL, 0, 0);
3162 else /*if (ilc == ILC_COLORDDB)*/
3164 TRACE("Creating Bitmap: %d Bits per Pixel\n", himl->uBitsPixel);
3166 hbmNewBitmap = CreateBitmap (sz.cx, sz.cy, 1, himl->uBitsPixel, NULL);
3168 TRACE("returning %p\n", hbmNewBitmap);
3169 return hbmNewBitmap;
3172 /*************************************************************************
3173 * ImageList_SetColorTable [COMCTL32.@]
3175 * Sets the color table of an image list.
3177 * PARAMS
3178 * himl [I] Handle to the image list.
3179 * uStartIndex [I] The first index to set.
3180 * cEntries [I] Number of entries to set.
3181 * prgb [I] New color information for color table for the image list.
3183 * RETURNS
3184 * Success: Number of entries in the table that were set.
3185 * Failure: Zero.
3187 * SEE
3188 * ImageList_Create(), SetDIBColorTable()
3191 UINT WINAPI
3192 ImageList_SetColorTable(HIMAGELIST himl, UINT uStartIndex, UINT cEntries, const RGBQUAD *prgb)
3194 TRACE("(%p, %d, %d, %p)\n", himl, uStartIndex, cEntries, prgb);
3195 himl->color_table_set = TRUE;
3196 return SetDIBColorTable(himl->hdcImage, uStartIndex, cEntries, prgb);
3199 /*************************************************************************
3200 * ImageList_CoCreateInstance [COMCTL32.@]
3202 * Creates a new imagelist instance and returns an interface pointer to it.
3204 * PARAMS
3205 * rclsid [I] A reference to the CLSID (CLSID_ImageList).
3206 * punkOuter [I] Pointer to IUnknown interface for aggregation, if desired
3207 * riid [I] Identifier of the requested interface.
3208 * ppv [O] Returns the address of the pointer requested, or NULL.
3210 * RETURNS
3211 * Success: S_OK.
3212 * Failure: Error value.
3214 HRESULT WINAPI
3215 ImageList_CoCreateInstance (REFCLSID rclsid, const IUnknown *punkOuter, REFIID riid, void **ppv)
3217 TRACE("(%s,%p,%s,%p)\n", debugstr_guid(rclsid), punkOuter, debugstr_guid(riid), ppv);
3219 if (!IsEqualCLSID(&CLSID_ImageList, rclsid))
3220 return E_NOINTERFACE;
3222 return ImageListImpl_CreateInstance(punkOuter, riid, ppv);
3226 /*************************************************************************
3227 * IImageList implementation
3230 static HRESULT WINAPI ImageListImpl_QueryInterface(IImageList2 *iface,
3231 REFIID iid, void **ppv)
3233 HIMAGELIST imgl = impl_from_IImageList2(iface);
3235 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
3237 if (!ppv) return E_INVALIDARG;
3239 if (IsEqualIID(&IID_IUnknown, iid) ||
3240 IsEqualIID(&IID_IImageList, iid) ||
3241 IsEqualIID(&IID_IImageList2, iid))
3243 *ppv = &imgl->IImageList2_iface;
3245 else
3247 *ppv = NULL;
3248 return E_NOINTERFACE;
3251 IImageList2_AddRef(iface);
3252 return S_OK;
3255 static ULONG WINAPI ImageListImpl_AddRef(IImageList2 *iface)
3257 HIMAGELIST imgl = impl_from_IImageList2(iface);
3258 ULONG ref = InterlockedIncrement(&imgl->ref);
3260 TRACE("(%p) refcount=%u\n", iface, ref);
3261 return ref;
3264 static ULONG WINAPI ImageListImpl_Release(IImageList2 *iface)
3266 HIMAGELIST This = impl_from_IImageList2(iface);
3267 ULONG ref = InterlockedDecrement(&This->ref);
3269 TRACE("(%p) refcount=%u\n", iface, ref);
3271 if (ref == 0)
3273 /* delete image bitmaps */
3274 if (This->hbmImage) DeleteObject (This->hbmImage);
3275 if (This->hbmMask) DeleteObject (This->hbmMask);
3277 /* delete image & mask DCs */
3278 if (This->hdcImage) DeleteDC (This->hdcImage);
3279 if (This->hdcMask) DeleteDC (This->hdcMask);
3281 /* delete blending brushes */
3282 if (This->hbrBlend25) DeleteObject (This->hbrBlend25);
3283 if (This->hbrBlend50) DeleteObject (This->hbrBlend50);
3285 This->IImageList2_iface.lpVtbl = NULL;
3286 heap_free(This->item_flags);
3287 heap_free(This);
3290 return ref;
3293 static HRESULT WINAPI ImageListImpl_Add(IImageList2 *iface, HBITMAP hbmImage,
3294 HBITMAP hbmMask, int *pi)
3296 HIMAGELIST imgl = impl_from_IImageList2(iface);
3297 int ret;
3299 if (!pi)
3300 return E_FAIL;
3302 ret = ImageList_Add(imgl, hbmImage, hbmMask);
3304 if (ret == -1)
3305 return E_FAIL;
3307 *pi = ret;
3308 return S_OK;
3311 static HRESULT WINAPI ImageListImpl_ReplaceIcon(IImageList2 *iface, int i,
3312 HICON hicon, int *pi)
3314 HIMAGELIST imgl = impl_from_IImageList2(iface);
3315 int ret;
3317 if (!pi)
3318 return E_FAIL;
3320 ret = ImageList_ReplaceIcon(imgl, i, hicon);
3322 if (ret == -1)
3323 return E_FAIL;
3325 *pi = ret;
3326 return S_OK;
3329 static HRESULT WINAPI ImageListImpl_SetOverlayImage(IImageList2 *iface,
3330 int iImage, int iOverlay)
3332 HIMAGELIST imgl = impl_from_IImageList2(iface);
3333 return ImageList_SetOverlayImage(imgl, iImage, iOverlay) ? S_OK : E_FAIL;
3336 static HRESULT WINAPI ImageListImpl_Replace(IImageList2 *iface, int i,
3337 HBITMAP hbmImage, HBITMAP hbmMask)
3339 HIMAGELIST imgl = impl_from_IImageList2(iface);
3340 return ImageList_Replace(imgl, i, hbmImage, hbmMask) ? S_OK : E_FAIL;
3343 static HRESULT WINAPI ImageListImpl_AddMasked(IImageList2 *iface, HBITMAP hbmImage,
3344 COLORREF crMask, int *pi)
3346 HIMAGELIST imgl = impl_from_IImageList2(iface);
3347 int ret;
3349 if (!pi)
3350 return E_FAIL;
3352 ret = ImageList_AddMasked(imgl, hbmImage, crMask);
3354 if (ret == -1)
3355 return E_FAIL;
3357 *pi = ret;
3358 return S_OK;
3361 static HRESULT WINAPI ImageListImpl_Draw(IImageList2 *iface,
3362 IMAGELISTDRAWPARAMS *pimldp)
3364 HIMAGELIST imgl = impl_from_IImageList2(iface);
3365 HIMAGELIST old_himl;
3366 int ret;
3368 /* As far as I can tell, Windows simply ignores the contents of pimldp->himl
3369 so we shall simulate the same */
3370 old_himl = pimldp->himl;
3371 pimldp->himl = imgl;
3373 ret = ImageList_DrawIndirect(pimldp);
3375 pimldp->himl = old_himl;
3376 return ret ? S_OK : E_INVALIDARG;
3379 static HRESULT WINAPI ImageListImpl_Remove(IImageList2 *iface, int i)
3381 HIMAGELIST imgl = impl_from_IImageList2(iface);
3382 return (ImageList_Remove(imgl, i) == 0) ? E_INVALIDARG : S_OK;
3385 static HRESULT WINAPI ImageListImpl_GetIcon(IImageList2 *iface, int i, UINT flags,
3386 HICON *picon)
3388 HIMAGELIST imgl = impl_from_IImageList2(iface);
3389 HICON hIcon;
3391 if (!picon)
3392 return E_FAIL;
3394 hIcon = ImageList_GetIcon(imgl, i, flags);
3396 if (hIcon == NULL)
3397 return E_FAIL;
3399 *picon = hIcon;
3400 return S_OK;
3403 static HRESULT WINAPI ImageListImpl_GetImageInfo(IImageList2 *iface, int i,
3404 IMAGEINFO *pImageInfo)
3406 HIMAGELIST imgl = impl_from_IImageList2(iface);
3407 return ImageList_GetImageInfo(imgl, i, pImageInfo) ? S_OK : E_FAIL;
3410 static HRESULT WINAPI ImageListImpl_Copy(IImageList2 *iface, int dst_index,
3411 IUnknown *unk_src, int src_index, UINT flags)
3413 HIMAGELIST imgl = impl_from_IImageList2(iface);
3414 IImageList *src = NULL;
3415 HRESULT ret;
3417 if (!unk_src)
3418 return E_FAIL;
3420 /* TODO: Add test for IID_ImageList2 too */
3421 if (FAILED(IUnknown_QueryInterface(unk_src, &IID_IImageList,
3422 (void **) &src)))
3423 return E_FAIL;
3425 if (ImageList_Copy(imgl, dst_index, (HIMAGELIST) src, src_index, flags))
3426 ret = S_OK;
3427 else
3428 ret = E_FAIL;
3430 IImageList_Release(src);
3431 return ret;
3434 static HRESULT WINAPI ImageListImpl_Merge(IImageList2 *iface, int i1,
3435 IUnknown *punk2, int i2, int dx, int dy, REFIID riid, void **ppv)
3437 HIMAGELIST imgl = impl_from_IImageList2(iface);
3438 IImageList *iml2 = NULL;
3439 HIMAGELIST merged;
3440 HRESULT ret = E_FAIL;
3442 TRACE("(%p)->(%d %p %d %d %d %s %p)\n", iface, i1, punk2, i2, dx, dy, debugstr_guid(riid), ppv);
3444 /* TODO: Add test for IID_ImageList2 too */
3445 if (FAILED(IUnknown_QueryInterface(punk2, &IID_IImageList,
3446 (void **) &iml2)))
3447 return E_FAIL;
3449 merged = ImageList_Merge(imgl, i1, (HIMAGELIST) iml2, i2, dx, dy);
3451 /* Get the interface for the new image list */
3452 if (merged)
3454 ret = HIMAGELIST_QueryInterface(merged, riid, ppv);
3455 ImageList_Destroy(merged);
3458 IImageList_Release(iml2);
3459 return ret;
3462 static HRESULT WINAPI ImageListImpl_Clone(IImageList2 *iface, REFIID riid, void **ppv)
3464 HIMAGELIST imgl = impl_from_IImageList2(iface);
3465 HIMAGELIST clone;
3466 HRESULT ret = E_FAIL;
3468 TRACE("(%p)->(%s %p)\n", iface, debugstr_guid(riid), ppv);
3470 clone = ImageList_Duplicate(imgl);
3472 /* Get the interface for the new image list */
3473 if (clone)
3475 ret = HIMAGELIST_QueryInterface(clone, riid, ppv);
3476 ImageList_Destroy(clone);
3479 return ret;
3482 static HRESULT WINAPI ImageListImpl_GetImageRect(IImageList2 *iface, int i,
3483 RECT *prc)
3485 HIMAGELIST imgl = impl_from_IImageList2(iface);
3486 IMAGEINFO info;
3488 if (!prc)
3489 return E_FAIL;
3491 if (!ImageList_GetImageInfo(imgl, i, &info))
3492 return E_FAIL;
3494 *prc = info.rcImage;
3496 return S_OK;
3499 static HRESULT WINAPI ImageListImpl_GetIconSize(IImageList2 *iface, int *cx,
3500 int *cy)
3502 HIMAGELIST imgl = impl_from_IImageList2(iface);
3503 return ImageList_GetIconSize(imgl, cx, cy) ? S_OK : E_INVALIDARG;
3506 static HRESULT WINAPI ImageListImpl_SetIconSize(IImageList2 *iface, int cx,
3507 int cy)
3509 HIMAGELIST imgl = impl_from_IImageList2(iface);
3510 return ImageList_SetIconSize(imgl, cx, cy) ? S_OK : E_FAIL;
3513 static HRESULT WINAPI ImageListImpl_GetImageCount(IImageList2 *iface, int *pi)
3515 HIMAGELIST imgl = impl_from_IImageList2(iface);
3516 *pi = ImageList_GetImageCount(imgl);
3517 return S_OK;
3520 static HRESULT WINAPI ImageListImpl_SetImageCount(IImageList2 *iface, UINT count)
3522 HIMAGELIST imgl = impl_from_IImageList2(iface);
3523 return ImageList_SetImageCount(imgl, count) ? S_OK : E_FAIL;
3526 static HRESULT WINAPI ImageListImpl_SetBkColor(IImageList2 *iface, COLORREF clrBk,
3527 COLORREF *pclr)
3529 HIMAGELIST imgl = impl_from_IImageList2(iface);
3530 *pclr = ImageList_SetBkColor(imgl, clrBk);
3531 return S_OK;
3534 static HRESULT WINAPI ImageListImpl_GetBkColor(IImageList2 *iface, COLORREF *pclr)
3536 HIMAGELIST imgl = impl_from_IImageList2(iface);
3537 *pclr = ImageList_GetBkColor(imgl);
3538 return S_OK;
3541 static HRESULT WINAPI ImageListImpl_BeginDrag(IImageList2 *iface, int iTrack,
3542 int dxHotspot, int dyHotspot)
3544 HIMAGELIST imgl = impl_from_IImageList2(iface);
3545 return ImageList_BeginDrag(imgl, iTrack, dxHotspot, dyHotspot) ? S_OK : E_FAIL;
3548 static HRESULT WINAPI ImageListImpl_EndDrag(IImageList2 *iface)
3550 ImageList_EndDrag();
3551 return S_OK;
3554 static HRESULT WINAPI ImageListImpl_DragEnter(IImageList2 *iface, HWND hwndLock,
3555 int x, int y)
3557 return ImageList_DragEnter(hwndLock, x, y) ? S_OK : E_FAIL;
3560 static HRESULT WINAPI ImageListImpl_DragLeave(IImageList2 *iface, HWND hwndLock)
3562 return ImageList_DragLeave(hwndLock) ? S_OK : E_FAIL;
3565 static HRESULT WINAPI ImageListImpl_DragMove(IImageList2 *iface, int x, int y)
3567 return ImageList_DragMove(x, y) ? S_OK : E_FAIL;
3570 static HRESULT WINAPI ImageListImpl_SetDragCursorImage(IImageList2 *iface,
3571 IUnknown *punk, int iDrag, int dxHotspot, int dyHotspot)
3573 IImageList *iml2 = NULL;
3574 BOOL ret;
3576 if (!punk)
3577 return E_FAIL;
3579 /* TODO: Add test for IID_ImageList2 too */
3580 if (FAILED(IUnknown_QueryInterface(punk, &IID_IImageList,
3581 (void **) &iml2)))
3582 return E_FAIL;
3584 ret = ImageList_SetDragCursorImage((HIMAGELIST) iml2, iDrag, dxHotspot,
3585 dyHotspot);
3587 IImageList_Release(iml2);
3589 return ret ? S_OK : E_FAIL;
3592 static HRESULT WINAPI ImageListImpl_DragShowNolock(IImageList2 *iface, BOOL fShow)
3594 return ImageList_DragShowNolock(fShow) ? S_OK : E_FAIL;
3597 static HRESULT WINAPI ImageListImpl_GetDragImage(IImageList2 *iface, POINT *ppt,
3598 POINT *pptHotspot, REFIID riid, PVOID *ppv)
3600 HRESULT ret = E_FAIL;
3601 HIMAGELIST hNew;
3603 if (!ppv)
3604 return E_FAIL;
3606 hNew = ImageList_GetDragImage(ppt, pptHotspot);
3608 /* Get the interface for the new image list */
3609 if (hNew)
3611 IImageList *idrag = (IImageList*)hNew;
3613 ret = HIMAGELIST_QueryInterface(hNew, riid, ppv);
3614 IImageList_Release(idrag);
3617 return ret;
3620 static HRESULT WINAPI ImageListImpl_GetItemFlags(IImageList2 *iface, int i, DWORD *flags)
3622 HIMAGELIST This = impl_from_IImageList2(iface);
3624 if (i < 0 || i >= This->cCurImage)
3625 return E_INVALIDARG;
3627 *flags = This->item_flags[i];
3629 return S_OK;
3632 static HRESULT WINAPI ImageListImpl_GetOverlayImage(IImageList2 *iface, int iOverlay,
3633 int *piIndex)
3635 HIMAGELIST This = impl_from_IImageList2(iface);
3636 int i;
3638 if ((iOverlay < 0) || (iOverlay > This->cCurImage))
3639 return E_FAIL;
3641 for (i = 0; i < MAX_OVERLAYIMAGE; i++)
3643 if (This->nOvlIdx[i] == iOverlay)
3645 *piIndex = i + 1;
3646 return S_OK;
3650 return E_FAIL;
3653 static HRESULT WINAPI ImageListImpl_Resize(IImageList2 *iface, INT cx, INT cy)
3655 FIXME("(%p)->(%d %d): stub\n", iface, cx, cy);
3656 return E_NOTIMPL;
3659 static HRESULT WINAPI ImageListImpl_GetOriginalSize(IImageList2 *iface, INT image, DWORD flags, INT *cx, INT *cy)
3661 FIXME("(%p)->(%d %x %p %p): stub\n", iface, image, flags, cx, cy);
3662 return E_NOTIMPL;
3665 static HRESULT WINAPI ImageListImpl_SetOriginalSize(IImageList2 *iface, INT image, INT cx, INT cy)
3667 FIXME("(%p)->(%d %d %d): stub\n", iface, image, cx, cy);
3668 return E_NOTIMPL;
3671 static HRESULT WINAPI ImageListImpl_SetCallback(IImageList2 *iface, IUnknown *callback)
3673 FIXME("(%p)->(%p): stub\n", iface, callback);
3674 return E_NOTIMPL;
3677 static HRESULT WINAPI ImageListImpl_GetCallback(IImageList2 *iface, REFIID riid, void **ppv)
3679 FIXME("(%p)->(%s %p): stub\n", iface, debugstr_guid(riid), ppv);
3680 return E_NOTIMPL;
3683 static HRESULT WINAPI ImageListImpl_ForceImagePresent(IImageList2 *iface, INT image, DWORD flags)
3685 FIXME("(%p)->(%d %x): stub\n", iface, image, flags);
3686 return E_NOTIMPL;
3689 static HRESULT WINAPI ImageListImpl_DiscardImages(IImageList2 *iface, INT first_image, INT last_image, DWORD flags)
3691 FIXME("(%p)->(%d %d %x): stub\n", iface, first_image, last_image, flags);
3692 return E_NOTIMPL;
3695 static HRESULT WINAPI ImageListImpl_PreloadImages(IImageList2 *iface, IMAGELISTDRAWPARAMS *params)
3697 FIXME("(%p)->(%p): stub\n", iface, params);
3698 return E_NOTIMPL;
3701 static HRESULT WINAPI ImageListImpl_GetStatistics(IImageList2 *iface, IMAGELISTSTATS *stats)
3703 FIXME("(%p)->(%p): stub\n", iface, stats);
3704 return E_NOTIMPL;
3707 static HRESULT WINAPI ImageListImpl_Initialize(IImageList2 *iface, INT cx, INT cy, UINT flags, INT initial, INT grow)
3709 FIXME("(%p)->(%d %d %d %d %d): stub\n", iface, cx, cy, flags, initial, grow);
3710 return E_NOTIMPL;
3713 static HRESULT WINAPI ImageListImpl_Replace2(IImageList2 *iface, INT i, HBITMAP image, HBITMAP mask, IUnknown *unk, DWORD flags)
3715 FIXME("(%p)->(%d %p %p %p %x): stub\n", iface, i, image, mask, unk, flags);
3716 return E_NOTIMPL;
3719 static HRESULT WINAPI ImageListImpl_ReplaceFromImageList(IImageList2 *iface, INT i, IImageList *imagelist, INT src,
3720 IUnknown *unk, DWORD flags)
3722 FIXME("(%p)->(%d %p %d %p %x): stub\n", iface, i, imagelist, src, unk, flags);
3723 return E_NOTIMPL;
3726 static const IImageList2Vtbl ImageListImpl_Vtbl = {
3727 ImageListImpl_QueryInterface,
3728 ImageListImpl_AddRef,
3729 ImageListImpl_Release,
3730 ImageListImpl_Add,
3731 ImageListImpl_ReplaceIcon,
3732 ImageListImpl_SetOverlayImage,
3733 ImageListImpl_Replace,
3734 ImageListImpl_AddMasked,
3735 ImageListImpl_Draw,
3736 ImageListImpl_Remove,
3737 ImageListImpl_GetIcon,
3738 ImageListImpl_GetImageInfo,
3739 ImageListImpl_Copy,
3740 ImageListImpl_Merge,
3741 ImageListImpl_Clone,
3742 ImageListImpl_GetImageRect,
3743 ImageListImpl_GetIconSize,
3744 ImageListImpl_SetIconSize,
3745 ImageListImpl_GetImageCount,
3746 ImageListImpl_SetImageCount,
3747 ImageListImpl_SetBkColor,
3748 ImageListImpl_GetBkColor,
3749 ImageListImpl_BeginDrag,
3750 ImageListImpl_EndDrag,
3751 ImageListImpl_DragEnter,
3752 ImageListImpl_DragLeave,
3753 ImageListImpl_DragMove,
3754 ImageListImpl_SetDragCursorImage,
3755 ImageListImpl_DragShowNolock,
3756 ImageListImpl_GetDragImage,
3757 ImageListImpl_GetItemFlags,
3758 ImageListImpl_GetOverlayImage,
3759 ImageListImpl_Resize,
3760 ImageListImpl_GetOriginalSize,
3761 ImageListImpl_SetOriginalSize,
3762 ImageListImpl_SetCallback,
3763 ImageListImpl_GetCallback,
3764 ImageListImpl_ForceImagePresent,
3765 ImageListImpl_DiscardImages,
3766 ImageListImpl_PreloadImages,
3767 ImageListImpl_GetStatistics,
3768 ImageListImpl_Initialize,
3769 ImageListImpl_Replace2,
3770 ImageListImpl_ReplaceFromImageList
3773 static BOOL is_valid(HIMAGELIST himl)
3775 BOOL valid;
3776 __TRY
3778 valid = himl && himl->IImageList2_iface.lpVtbl == &ImageListImpl_Vtbl;
3780 __EXCEPT_PAGE_FAULT
3782 valid = FALSE;
3784 __ENDTRY
3785 return valid;
3788 /*************************************************************************
3789 * HIMAGELIST_QueryInterface [COMCTL32.@]
3791 * Returns a pointer to an IImageList or IImageList2 object for the given
3792 * HIMAGELIST.
3794 * PARAMS
3795 * himl [I] Image list handle.
3796 * riid [I] Identifier of the requested interface.
3797 * ppv [O] Returns the address of the pointer requested, or NULL.
3799 * RETURNS
3800 * Success: S_OK.
3801 * Failure: Error value.
3803 HRESULT WINAPI
3804 HIMAGELIST_QueryInterface (HIMAGELIST himl, REFIID riid, void **ppv)
3806 TRACE("(%p,%s,%p)\n", himl, debugstr_guid(riid), ppv);
3807 return IImageList2_QueryInterface((IImageList2 *) himl, riid, ppv);
3810 static HRESULT ImageListImpl_CreateInstance(const IUnknown *pUnkOuter, REFIID iid, void** ppv)
3812 HIMAGELIST This;
3813 HRESULT ret;
3815 TRACE("(%p,%s,%p)\n", pUnkOuter, debugstr_guid(iid), ppv);
3817 *ppv = NULL;
3819 if (pUnkOuter) return CLASS_E_NOAGGREGATION;
3821 This = heap_alloc_zero(sizeof(struct _IMAGELIST));
3822 if (!This) return E_OUTOFMEMORY;
3824 This->IImageList2_iface.lpVtbl = &ImageListImpl_Vtbl;
3825 This->ref = 1;
3827 ret = IImageList2_QueryInterface(&This->IImageList2_iface, iid, ppv);
3828 IImageList2_Release(&This->IImageList2_iface);
3830 return ret;