include: Use the hard-float calling convention for Windows APIs on ARM
[wine.git] / dlls / comctl32 / imagelist.c
blob157344c633dbe9f1d1e1582c3a49dcc4d40d8f2e
1 /*
2 * ImageList implementation
4 * Copyright 1998 Eric Kohl
5 * Copyright 2000 Jason Mawdsley
6 * Copyright 2001, 2004 Michael Stefaniuc
7 * Copyright 2001 Charles Loep for CodeWeavers
8 * Copyright 2002 Dimitrie O. Paun
9 * Copyright 2009 Owen Rudge for CodeWeavers
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 * NOTE
27 * This code was audited for completeness against the documented features
28 * of Comctl32.dll version 6.0 on Sep. 12, 2002, by Dimitrie O. Paun.
30 * Unless otherwise noted, we believe this code to be complete, as per
31 * the specification mentioned above.
32 * If you discover missing features, or bugs, please note them below.
34 * TODO:
35 * - Add support for ILD_PRESERVEALPHA, ILD_SCALE, ILD_DPISCALE
36 * - Add support for ILS_GLOW, ILS_SHADOW, ILS_SATURATE
37 * - Thread-safe locking
40 #include <stdarg.h>
41 #include <stdlib.h>
42 #include <string.h>
44 #define COBJMACROS
46 #include "winerror.h"
47 #include "windef.h"
48 #include "winbase.h"
49 #include "objbase.h"
50 #include "wingdi.h"
51 #include "winuser.h"
52 #include "commctrl.h"
53 #include "comctl32.h"
54 #include "commoncontrols.h"
55 #include "wine/debug.h"
56 #include "wine/exception.h"
58 WINE_DEFAULT_DEBUG_CHANNEL(imagelist);
60 #define MAX_OVERLAYIMAGE 15
62 struct _IMAGELIST
64 IImageList2 IImageList2_iface; /* 00: IImageList vtable */
65 INT cCurImage; /* 04: ImageCount */
66 INT cMaxImage; /* 08: maximages */
67 INT cGrow; /* 0C: cGrow */
68 INT cx; /* 10: cx */
69 INT cy; /* 14: cy */
70 DWORD x4;
71 UINT flags; /* 1C: flags */
72 COLORREF clrFg; /* 20: foreground color */
73 COLORREF clrBk; /* 24: background color */
76 HBITMAP hbmImage; /* 28: images Bitmap */
77 HBITMAP hbmMask; /* 2C: masks Bitmap */
78 HDC hdcImage; /* 30: images MemDC */
79 HDC hdcMask; /* 34: masks MemDC */
80 INT nOvlIdx[MAX_OVERLAYIMAGE]; /* 38: overlay images index */
82 /* not yet found out */
83 HBRUSH hbrBlend25;
84 HBRUSH hbrBlend50;
85 INT cInitial;
86 UINT uBitsPixel;
87 char *has_alpha;
88 BOOL color_table_set;
90 LONG ref; /* reference count */
93 #define IMAGELIST_MAGIC 0x53414D58
95 /* Header used by ImageList_Read() and ImageList_Write() */
96 #include "pshpack2.h"
97 typedef struct _ILHEAD
99 USHORT usMagic;
100 USHORT usVersion;
101 WORD cCurImage;
102 WORD cMaxImage;
103 WORD cGrow;
104 WORD cx;
105 WORD cy;
106 COLORREF bkcolor;
107 WORD flags;
108 SHORT ovls[4];
109 } ILHEAD;
110 #include "poppack.h"
112 /* internal image list data used for Drag & Drop operations */
113 typedef struct
115 HWND hwnd;
116 HIMAGELIST himl;
117 HIMAGELIST himlNoCursor;
118 /* position of the drag image relative to the window */
119 INT x;
120 INT y;
121 /* offset of the hotspot relative to the origin of the image */
122 INT dxHotspot;
123 INT dyHotspot;
124 /* is the drag image visible */
125 BOOL bShow;
126 /* saved background */
127 HBITMAP hbmBg;
128 } INTERNALDRAG;
130 static INTERNALDRAG InternalDrag = { 0, 0, 0, 0, 0, 0, 0, FALSE, 0 };
132 static inline HIMAGELIST impl_from_IImageList2(IImageList2 *iface)
134 return CONTAINING_RECORD(iface, struct _IMAGELIST, IImageList2_iface);
137 static HBITMAP ImageList_CreateImage(HDC hdc, HIMAGELIST himl, UINT count);
138 static HRESULT ImageListImpl_CreateInstance(const IUnknown *pUnkOuter, REFIID iid, void** ppv);
139 static BOOL is_valid(HIMAGELIST himl);
142 * An imagelist with N images is tiled like this:
144 * N/4 ->
146 * 4 048C..
147 * 159D..
148 * | 26AE.N
149 * V 37BF.
152 #define TILE_COUNT 4
154 static inline UINT imagelist_height( UINT count )
156 return ((count + TILE_COUNT - 1)/TILE_COUNT);
159 static inline void imagelist_point_from_index( HIMAGELIST himl, UINT index, LPPOINT pt )
161 pt->x = (index%TILE_COUNT) * himl->cx;
162 pt->y = (index/TILE_COUNT) * himl->cy;
165 static inline void imagelist_get_bitmap_size( HIMAGELIST himl, UINT count, SIZE *sz )
167 sz->cx = himl->cx * TILE_COUNT;
168 sz->cy = imagelist_height( count ) * himl->cy;
171 static inline int get_dib_stride( int width, int bpp )
173 return ((width * bpp + 31) >> 3) & ~3;
176 static inline int get_dib_image_size( const BITMAPINFO *info )
178 return get_dib_stride( info->bmiHeader.biWidth, info->bmiHeader.biBitCount )
179 * abs( info->bmiHeader.biHeight );
183 * imagelist_copy_images()
185 * Copies a block of count images from offset src in the list to offset dest.
186 * Images are copied a row at at time. Assumes hdcSrc and hdcDest are different.
188 static inline void imagelist_copy_images( HIMAGELIST himl, HDC hdcSrc, HDC hdcDest,
189 UINT src, UINT count, UINT dest )
191 POINT ptSrc, ptDest;
192 SIZE sz;
193 UINT i;
195 for ( i=0; i<TILE_COUNT; i++ )
197 imagelist_point_from_index( himl, src+i, &ptSrc );
198 imagelist_point_from_index( himl, dest+i, &ptDest );
199 sz.cx = himl->cx;
200 sz.cy = himl->cy * imagelist_height( count - i );
202 BitBlt( hdcDest, ptDest.x, ptDest.y, sz.cx, sz.cy,
203 hdcSrc, ptSrc.x, ptSrc.y, SRCCOPY );
207 static void add_dib_bits( HIMAGELIST himl, int pos, int count, int width, int height,
208 BITMAPINFO *info, BITMAPINFO *mask_info, DWORD *bits, BYTE *mask_bits )
210 int i, j, n;
211 POINT pt;
212 int stride = info->bmiHeader.biWidth;
213 int mask_stride = (info->bmiHeader.biWidth + 31) / 32 * 4;
215 for (n = 0; n < count; n++)
217 BOOL has_alpha = FALSE;
219 imagelist_point_from_index( himl, pos + n, &pt );
221 /* check if bitmap has an alpha channel */
222 for (i = 0; i < height && !has_alpha; i++)
223 for (j = n * width; j < (n + 1) * width; j++)
224 if ((has_alpha = ((bits[i * stride + j] & 0xff000000) != 0))) break;
226 if (!has_alpha) /* generate alpha channel from the mask */
228 for (i = 0; i < height; i++)
229 for (j = n * width; j < (n + 1) * width; j++)
230 if (!mask_info || !((mask_bits[i * mask_stride + j / 8] << (j % 8)) & 0x80))
231 bits[i * stride + j] |= 0xff000000;
232 else
233 bits[i * stride + j] = 0;
235 else
237 himl->has_alpha[pos + n] = 1;
239 if (mask_info && himl->hbmMask) /* generate the mask from the alpha channel */
241 for (i = 0; i < height; i++)
242 for (j = n * width; j < (n + 1) * width; j++)
243 if ((bits[i * stride + j] >> 24) > 25) /* more than 10% alpha */
244 mask_bits[i * mask_stride + j / 8] &= ~(0x80 >> (j % 8));
245 else
246 mask_bits[i * mask_stride + j / 8] |= 0x80 >> (j % 8);
249 StretchDIBits( himl->hdcImage, pt.x, pt.y, himl->cx, himl->cy,
250 n * width, 0, width, height, bits, info, DIB_RGB_COLORS, SRCCOPY );
251 if (mask_info)
252 StretchDIBits( himl->hdcMask, pt.x, pt.y, himl->cx, himl->cy,
253 n * width, 0, width, height, mask_bits, mask_info, DIB_RGB_COLORS, SRCCOPY );
257 /* add images with an alpha channel when the image list is 32 bpp */
258 static BOOL add_with_alpha( HIMAGELIST himl, HDC hdc, int pos, int count,
259 int width, int height, HBITMAP hbmImage, HBITMAP hbmMask )
261 BOOL ret = FALSE;
262 BITMAP bm;
263 BITMAPINFO *info, *mask_info = NULL;
264 DWORD *bits = NULL;
265 BYTE *mask_bits = NULL;
266 DWORD mask_width;
268 if (!GetObjectW( hbmImage, sizeof(bm), &bm )) return FALSE;
270 /* if either the imagelist or the source bitmap don't have an alpha channel, bail out now */
271 if (!himl->has_alpha) return FALSE;
272 if (bm.bmBitsPixel != 32) return FALSE;
274 SelectObject( hdc, hbmImage );
275 mask_width = (bm.bmWidth + 31) / 32 * 4;
277 if (!(info = HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET( BITMAPINFO, bmiColors[256] )))) goto done;
278 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
279 info->bmiHeader.biWidth = bm.bmWidth;
280 info->bmiHeader.biHeight = -height;
281 info->bmiHeader.biPlanes = 1;
282 info->bmiHeader.biBitCount = 32;
283 info->bmiHeader.biCompression = BI_RGB;
284 info->bmiHeader.biSizeImage = bm.bmWidth * height * 4;
285 info->bmiHeader.biXPelsPerMeter = 0;
286 info->bmiHeader.biYPelsPerMeter = 0;
287 info->bmiHeader.biClrUsed = 0;
288 info->bmiHeader.biClrImportant = 0;
289 if (!(bits = HeapAlloc( GetProcessHeap(), 0, info->bmiHeader.biSizeImage ))) goto done;
290 if (!GetDIBits( hdc, hbmImage, 0, height, bits, info, DIB_RGB_COLORS )) goto done;
292 if (hbmMask)
294 if (!(mask_info = HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET( BITMAPINFO, bmiColors[2] ))))
295 goto done;
296 mask_info->bmiHeader = info->bmiHeader;
297 mask_info->bmiHeader.biBitCount = 1;
298 mask_info->bmiHeader.biSizeImage = mask_width * height;
299 if (!(mask_bits = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, info->bmiHeader.biSizeImage )))
300 goto done;
301 if (!GetDIBits( hdc, hbmMask, 0, height, mask_bits, mask_info, DIB_RGB_COLORS )) goto done;
304 add_dib_bits( himl, pos, count, width, height, info, mask_info, bits, mask_bits );
305 ret = TRUE;
307 done:
308 HeapFree( GetProcessHeap(), 0, info );
309 HeapFree( GetProcessHeap(), 0, mask_info );
310 HeapFree( GetProcessHeap(), 0, bits );
311 HeapFree( GetProcessHeap(), 0, mask_bits );
312 return ret;
315 UINT WINAPI
316 ImageList_SetColorTable(HIMAGELIST himl, UINT uStartIndex, UINT cEntries, const RGBQUAD *prgb);
318 /*************************************************************************
319 * IMAGELIST_InternalExpandBitmaps [Internal]
321 * Expands the bitmaps of an image list by the given number of images.
323 * PARAMS
324 * himl [I] handle to image list
325 * nImageCount [I] number of images to add
327 * RETURNS
328 * nothing
330 * NOTES
331 * This function CANNOT be used to reduce the number of images.
333 static void
334 IMAGELIST_InternalExpandBitmaps(HIMAGELIST himl, INT nImageCount)
336 HDC hdcBitmap;
337 HBITMAP hbmNewBitmap, hbmNull;
338 INT nNewCount;
339 SIZE sz;
341 TRACE("%p has allocated %d, max %d, grow %d images\n", himl, himl->cCurImage, himl->cMaxImage, himl->cGrow);
343 if (himl->cCurImage + nImageCount < himl->cMaxImage)
344 return;
346 nNewCount = himl->cMaxImage + max(nImageCount, himl->cGrow) + 1;
348 imagelist_get_bitmap_size(himl, nNewCount, &sz);
350 TRACE("Create expanded bitmaps : himl=%p x=%d y=%d count=%d\n", himl, sz.cx, sz.cy, nNewCount);
351 hdcBitmap = CreateCompatibleDC (0);
353 hbmNewBitmap = ImageList_CreateImage(hdcBitmap, himl, nNewCount);
355 if (hbmNewBitmap == 0)
356 ERR("creating new image bitmap (x=%d y=%d)!\n", sz.cx, sz.cy);
358 if (himl->cCurImage)
360 hbmNull = SelectObject (hdcBitmap, hbmNewBitmap);
361 BitBlt (hdcBitmap, 0, 0, sz.cx, sz.cy,
362 himl->hdcImage, 0, 0, SRCCOPY);
363 SelectObject (hdcBitmap, hbmNull);
365 SelectObject (himl->hdcImage, hbmNewBitmap);
366 DeleteObject (himl->hbmImage);
367 himl->hbmImage = hbmNewBitmap;
369 if (himl->flags & ILC_MASK)
371 hbmNewBitmap = CreateBitmap (sz.cx, sz.cy, 1, 1, NULL);
373 if (hbmNewBitmap == 0)
374 ERR("creating new mask bitmap!\n");
376 if(himl->cCurImage)
378 hbmNull = SelectObject (hdcBitmap, hbmNewBitmap);
379 BitBlt (hdcBitmap, 0, 0, sz.cx, sz.cy,
380 himl->hdcMask, 0, 0, SRCCOPY);
381 SelectObject (hdcBitmap, hbmNull);
383 SelectObject (himl->hdcMask, hbmNewBitmap);
384 DeleteObject (himl->hbmMask);
385 himl->hbmMask = hbmNewBitmap;
388 if (himl->has_alpha)
390 char *new_alpha = HeapReAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, himl->has_alpha, nNewCount );
391 if (new_alpha) himl->has_alpha = new_alpha;
392 else
394 HeapFree( GetProcessHeap(), 0, himl->has_alpha );
395 himl->has_alpha = NULL;
399 himl->cMaxImage = nNewCount;
401 DeleteDC (hdcBitmap);
405 /*************************************************************************
406 * ImageList_Add [COMCTL32.@]
408 * Add an image or images to an image list.
410 * PARAMS
411 * himl [I] handle to image list
412 * hbmImage [I] handle to image bitmap
413 * hbmMask [I] handle to mask bitmap
415 * RETURNS
416 * Success: Index of the first new image.
417 * Failure: -1
420 INT WINAPI
421 ImageList_Add (HIMAGELIST himl, HBITMAP hbmImage, HBITMAP hbmMask)
423 HDC hdcBitmap, hdcTemp = 0;
424 INT nFirstIndex, nImageCount, i;
425 BITMAP bmp;
426 POINT pt;
428 TRACE("himl=%p hbmimage=%p hbmmask=%p\n", himl, hbmImage, hbmMask);
429 if (!is_valid(himl))
430 return -1;
432 if (!GetObjectW(hbmImage, sizeof(BITMAP), &bmp))
433 return -1;
435 TRACE("himl %p, cCurImage %d, cMaxImage %d, cGrow %d, cx %d, cy %d\n",
436 himl, himl->cCurImage, himl->cMaxImage, himl->cGrow, himl->cx, himl->cy);
438 nImageCount = bmp.bmWidth / himl->cx;
440 TRACE("%p has %d images (%d x %d) bpp %d\n", hbmImage, nImageCount, bmp.bmWidth, bmp.bmHeight,
441 bmp.bmBitsPixel);
443 IMAGELIST_InternalExpandBitmaps(himl, nImageCount);
445 hdcBitmap = CreateCompatibleDC(0);
447 SelectObject(hdcBitmap, hbmImage);
449 if (add_with_alpha( himl, hdcBitmap, himl->cCurImage, nImageCount,
450 himl->cx, min( himl->cy, bmp.bmHeight), hbmImage, hbmMask ))
451 goto done;
453 if (himl->hbmMask)
455 hdcTemp = CreateCompatibleDC(0);
456 SelectObject(hdcTemp, hbmMask);
459 if (himl->uBitsPixel <= 8 && bmp.bmBitsPixel <= 8 &&
460 !himl->color_table_set && himl->cCurImage == 0)
462 RGBQUAD colors[256];
463 UINT num = GetDIBColorTable( hdcBitmap, 0, 1 << bmp.bmBitsPixel, colors );
464 if (num) ImageList_SetColorTable( himl, 0, num, colors );
467 for (i=0; i<nImageCount; i++)
469 imagelist_point_from_index( himl, himl->cCurImage + i, &pt );
471 /* Copy result to the imagelist
473 BitBlt( himl->hdcImage, pt.x, pt.y, himl->cx, bmp.bmHeight,
474 hdcBitmap, i*himl->cx, 0, SRCCOPY );
476 if (!himl->hbmMask)
477 continue;
479 BitBlt( himl->hdcMask, pt.x, pt.y, himl->cx, bmp.bmHeight,
480 hdcTemp, i*himl->cx, 0, SRCCOPY );
482 /* Remove the background from the image
484 BitBlt( himl->hdcImage, pt.x, pt.y, himl->cx, bmp.bmHeight,
485 himl->hdcMask, pt.x, pt.y, 0x220326 ); /* NOTSRCAND */
487 if (hdcTemp) DeleteDC(hdcTemp);
489 done:
490 DeleteDC(hdcBitmap);
492 nFirstIndex = himl->cCurImage;
493 himl->cCurImage += nImageCount;
495 return nFirstIndex;
499 /*************************************************************************
500 * ImageList_AddIcon [COMCTL32.@]
502 * Adds an icon to an image list.
504 * PARAMS
505 * himl [I] handle to image list
506 * hIcon [I] handle to icon
508 * RETURNS
509 * Success: index of the new image
510 * Failure: -1
512 #undef ImageList_AddIcon
513 INT WINAPI ImageList_AddIcon (HIMAGELIST himl, HICON hIcon)
515 return ImageList_ReplaceIcon (himl, -1, hIcon);
519 /*************************************************************************
520 * ImageList_AddMasked [COMCTL32.@]
522 * Adds an image or images to an image list and creates a mask from the
523 * specified bitmap using the mask color.
525 * PARAMS
526 * himl [I] handle to image list.
527 * hBitmap [I] handle to bitmap
528 * clrMask [I] mask color.
530 * RETURNS
531 * Success: Index of the first new image.
532 * Failure: -1
535 INT WINAPI
536 ImageList_AddMasked (HIMAGELIST himl, HBITMAP hBitmap, COLORREF clrMask)
538 HDC hdcMask, hdcBitmap;
539 INT ret;
540 BITMAP bmp;
541 HBITMAP hMaskBitmap;
542 COLORREF bkColor;
544 TRACE("himl=%p hbitmap=%p clrmask=%x\n", himl, hBitmap, clrMask);
545 if (!is_valid(himl))
546 return -1;
548 if (!GetObjectW(hBitmap, sizeof(BITMAP), &bmp))
549 return -1;
551 hdcBitmap = CreateCompatibleDC(0);
552 SelectObject(hdcBitmap, hBitmap);
554 /* Create a temp Mask so we can remove the background of the Image */
555 hdcMask = CreateCompatibleDC(0);
556 hMaskBitmap = CreateBitmap(bmp.bmWidth, bmp.bmHeight, 1, 1, NULL);
557 SelectObject(hdcMask, hMaskBitmap);
559 /* create monochrome image to the mask bitmap */
560 bkColor = (clrMask != CLR_DEFAULT) ? clrMask : GetPixel (hdcBitmap, 0, 0);
561 SetBkColor (hdcBitmap, bkColor);
562 BitBlt (hdcMask, 0, 0, bmp.bmWidth, bmp.bmHeight, hdcBitmap, 0, 0, SRCCOPY);
565 * Remove the background from the image
567 * WINDOWS BUG ALERT!!!!!!
568 * The statement below should not be done in common practice
569 * but this is how ImageList_AddMasked works in Windows.
570 * It overwrites the original bitmap passed, this was discovered
571 * by using the same bitmap to iterate the different styles
572 * on windows where it failed (BUT ImageList_Add is OK)
573 * This is here in case some apps rely on this bug
575 * Blt mode 0x220326 is NOTSRCAND
577 if (bmp.bmBitsPixel > 8) /* NOTSRCAND can't work with palettes */
579 SetBkColor(hdcBitmap, RGB(255,255,255));
580 BitBlt(hdcBitmap, 0, 0, bmp.bmWidth, bmp.bmHeight, hdcMask, 0, 0, 0x220326);
583 DeleteDC(hdcBitmap);
584 DeleteDC(hdcMask);
586 ret = ImageList_Add( himl, hBitmap, hMaskBitmap );
588 DeleteObject(hMaskBitmap);
589 return ret;
593 /*************************************************************************
594 * ImageList_BeginDrag [COMCTL32.@]
596 * Creates a temporary image list that contains one image. It will be used
597 * as a drag image.
599 * PARAMS
600 * himlTrack [I] handle to the source image list
601 * iTrack [I] index of the drag image in the source image list
602 * dxHotspot [I] X position of the hot spot of the drag image
603 * dyHotspot [I] Y position of the hot spot of the drag image
605 * RETURNS
606 * Success: TRUE
607 * Failure: FALSE
610 BOOL WINAPI
611 ImageList_BeginDrag (HIMAGELIST himlTrack, INT iTrack,
612 INT dxHotspot, INT dyHotspot)
614 INT cx, cy;
615 POINT src, dst;
617 TRACE("(himlTrack=%p iTrack=%d dx=%d dy=%d)\n", himlTrack, iTrack,
618 dxHotspot, dyHotspot);
620 if (!is_valid(himlTrack))
621 return FALSE;
623 if (iTrack >= himlTrack->cCurImage)
624 return FALSE;
626 if (InternalDrag.himl)
627 return FALSE;
629 cx = himlTrack->cx;
630 cy = himlTrack->cy;
632 InternalDrag.himlNoCursor = InternalDrag.himl = ImageList_Create (cx, cy, himlTrack->flags, 1, 1);
633 if (InternalDrag.himl == NULL) {
634 WARN("Error creating drag image list!\n");
635 return FALSE;
638 InternalDrag.dxHotspot = dxHotspot;
639 InternalDrag.dyHotspot = dyHotspot;
641 /* copy image */
642 imagelist_point_from_index(InternalDrag.himl, 0, &dst);
643 imagelist_point_from_index(himlTrack, iTrack, &src);
644 BitBlt(InternalDrag.himl->hdcImage, dst.x, dst.y, cx, cy, himlTrack->hdcImage, src.x, src.y,
645 SRCCOPY);
646 BitBlt(InternalDrag.himl->hdcMask, dst.x, dst.y, cx, cy, himlTrack->hdcMask, src.x, src.y,
647 SRCCOPY);
649 InternalDrag.himl->cCurImage = 1;
651 return TRUE;
655 /*************************************************************************
656 * ImageList_Copy [COMCTL32.@]
658 * Copies an image of the source image list to an image of the
659 * destination image list. Images can be copied or swapped.
661 * PARAMS
662 * himlDst [I] handle to the destination image list
663 * iDst [I] destination image index.
664 * himlSrc [I] handle to the source image list
665 * iSrc [I] source image index
666 * uFlags [I] flags for the copy operation
668 * RETURNS
669 * Success: TRUE
670 * Failure: FALSE
672 * NOTES
673 * Copying from one image list to another is possible. The original
674 * implementation just copies or swaps within one image list.
675 * Could this feature become a bug??? ;-)
678 BOOL WINAPI
679 ImageList_Copy (HIMAGELIST himlDst, INT iDst, HIMAGELIST himlSrc,
680 INT iSrc, UINT uFlags)
682 POINT ptSrc, ptDst;
684 TRACE("himlDst=%p iDst=%d himlSrc=%p iSrc=%d\n", himlDst, iDst, himlSrc, iSrc);
686 if (!is_valid(himlSrc) || !is_valid(himlDst))
687 return FALSE;
688 if ((iDst < 0) || (iDst >= himlDst->cCurImage))
689 return FALSE;
690 if ((iSrc < 0) || (iSrc >= himlSrc->cCurImage))
691 return FALSE;
693 imagelist_point_from_index( himlDst, iDst, &ptDst );
694 imagelist_point_from_index( himlSrc, iSrc, &ptSrc );
696 if (uFlags & ILCF_SWAP) {
697 /* swap */
698 HDC hdcBmp;
699 HBITMAP hbmTempImage, hbmTempMask;
701 hdcBmp = CreateCompatibleDC (0);
703 /* create temporary bitmaps */
704 hbmTempImage = CreateBitmap (himlSrc->cx, himlSrc->cy, 1,
705 himlSrc->uBitsPixel, NULL);
706 hbmTempMask = CreateBitmap (himlSrc->cx, himlSrc->cy, 1,
707 1, NULL);
709 /* copy (and stretch) destination to temporary bitmaps.(save) */
710 /* image */
711 SelectObject (hdcBmp, hbmTempImage);
712 StretchBlt (hdcBmp, 0, 0, himlSrc->cx, himlSrc->cy,
713 himlDst->hdcImage, ptDst.x, ptDst.y, himlDst->cx, himlDst->cy,
714 SRCCOPY);
715 /* mask */
716 SelectObject (hdcBmp, hbmTempMask);
717 StretchBlt (hdcBmp, 0, 0, himlSrc->cx, himlSrc->cy,
718 himlDst->hdcMask, ptDst.x, ptDst.y, himlDst->cx, himlDst->cy,
719 SRCCOPY);
721 /* copy (and stretch) source to destination */
722 /* image */
723 StretchBlt (himlDst->hdcImage, ptDst.x, ptDst.y, himlDst->cx, himlDst->cy,
724 himlSrc->hdcImage, ptSrc.x, ptSrc.y, himlSrc->cx, himlSrc->cy,
725 SRCCOPY);
726 /* mask */
727 StretchBlt (himlDst->hdcMask, ptDst.x, ptDst.y, himlDst->cx, himlDst->cy,
728 himlSrc->hdcMask, ptSrc.x, ptSrc.y, himlSrc->cx, himlSrc->cy,
729 SRCCOPY);
731 /* copy (without stretching) temporary bitmaps to source (restore) */
732 /* mask */
733 BitBlt (himlSrc->hdcMask, ptSrc.x, ptSrc.y, himlSrc->cx, himlSrc->cy,
734 hdcBmp, 0, 0, SRCCOPY);
736 /* image */
737 BitBlt (himlSrc->hdcImage, ptSrc.x, ptSrc.y, himlSrc->cx, himlSrc->cy,
738 hdcBmp, 0, 0, SRCCOPY);
739 /* delete temporary bitmaps */
740 DeleteObject (hbmTempMask);
741 DeleteObject (hbmTempImage);
742 DeleteDC(hdcBmp);
744 else {
745 /* copy image */
746 StretchBlt (himlDst->hdcImage, ptDst.x, ptDst.y, himlDst->cx, himlDst->cy,
747 himlSrc->hdcImage, ptSrc.x, ptSrc.y, himlSrc->cx, himlSrc->cy,
748 SRCCOPY);
750 /* copy mask */
751 StretchBlt (himlDst->hdcMask, ptDst.x, ptDst.y, himlDst->cx, himlDst->cy,
752 himlSrc->hdcMask, ptSrc.x, ptSrc.y, himlSrc->cx, himlSrc->cy,
753 SRCCOPY);
756 return TRUE;
760 /*************************************************************************
761 * ImageList_Create [COMCTL32.@]
763 * Creates a new image list.
765 * PARAMS
766 * cx [I] image height
767 * cy [I] image width
768 * flags [I] creation flags
769 * cInitial [I] initial number of images in the image list
770 * cGrow [I] number of images by which image list grows
772 * RETURNS
773 * Success: Handle to the created image list
774 * Failure: NULL
776 HIMAGELIST WINAPI
777 ImageList_Create (INT cx, INT cy, UINT flags,
778 INT cInitial, INT cGrow)
780 HIMAGELIST himl;
781 INT nCount;
782 HBITMAP hbmTemp;
783 UINT ilc = (flags & 0xFE);
784 static const WORD aBitBlend25[] =
785 {0xAA, 0x00, 0x55, 0x00, 0xAA, 0x00, 0x55, 0x00};
787 static const WORD aBitBlend50[] =
788 {0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA};
790 TRACE("(%d %d 0x%x %d %d)\n", cx, cy, flags, cInitial, cGrow);
792 if (cx < 0 || cy < 0) return NULL;
793 if (!((flags&ILC_COLORDDB) == ILC_COLORDDB) && (cx == 0 || cy == 0)) return NULL;
795 /* Create the IImageList interface for the image list */
796 if (FAILED(ImageListImpl_CreateInstance(NULL, &IID_IImageList, (void **)&himl)))
797 return NULL;
799 cGrow = (WORD)((max( cGrow, 1 ) + 3) & ~3);
801 if (cGrow > 256)
803 /* Windows doesn't limit the size here, but X11 doesn't let us allocate such huge bitmaps */
804 WARN( "grow %d too large, limiting to 256\n", cGrow );
805 cGrow = 256;
808 himl->cx = cx;
809 himl->cy = cy;
810 himl->flags = flags;
811 himl->cMaxImage = cInitial + 1;
812 himl->cInitial = cInitial;
813 himl->cGrow = cGrow;
814 himl->clrFg = CLR_DEFAULT;
815 himl->clrBk = CLR_NONE;
816 himl->color_table_set = FALSE;
818 /* initialize overlay mask indices */
819 for (nCount = 0; nCount < MAX_OVERLAYIMAGE; nCount++)
820 himl->nOvlIdx[nCount] = -1;
822 /* Create Image & Mask DCs */
823 himl->hdcImage = CreateCompatibleDC (0);
824 if (!himl->hdcImage)
825 goto cleanup;
826 if (himl->flags & ILC_MASK){
827 himl->hdcMask = CreateCompatibleDC(0);
828 if (!himl->hdcMask)
829 goto cleanup;
832 /* Default to ILC_COLOR4 if none of the ILC_COLOR* flags are specified */
833 if (ilc == ILC_COLOR)
835 ilc = ILC_COLOR4;
836 himl->flags |= ILC_COLOR4;
839 if (ilc >= ILC_COLOR4 && ilc <= ILC_COLOR32)
840 himl->uBitsPixel = ilc;
841 else
842 himl->uBitsPixel = (UINT)GetDeviceCaps (himl->hdcImage, BITSPIXEL);
844 if (himl->cMaxImage > 0) {
845 himl->hbmImage = ImageList_CreateImage(himl->hdcImage, himl, himl->cMaxImage);
846 SelectObject(himl->hdcImage, himl->hbmImage);
847 } else
848 himl->hbmImage = 0;
850 if ((himl->cMaxImage > 0) && (himl->flags & ILC_MASK)) {
851 SIZE sz;
853 imagelist_get_bitmap_size(himl, himl->cMaxImage, &sz);
854 himl->hbmMask = CreateBitmap (sz.cx, sz.cy, 1, 1, NULL);
855 if (himl->hbmMask == 0) {
856 ERR("Error creating mask bitmap!\n");
857 goto cleanup;
859 SelectObject(himl->hdcMask, himl->hbmMask);
861 else
862 himl->hbmMask = 0;
864 if (ilc == ILC_COLOR32)
865 himl->has_alpha = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, himl->cMaxImage );
866 else
867 himl->has_alpha = NULL;
869 /* create blending brushes */
870 hbmTemp = CreateBitmap (8, 8, 1, 1, aBitBlend25);
871 himl->hbrBlend25 = CreatePatternBrush (hbmTemp);
872 DeleteObject (hbmTemp);
874 hbmTemp = CreateBitmap (8, 8, 1, 1, aBitBlend50);
875 himl->hbrBlend50 = CreatePatternBrush (hbmTemp);
876 DeleteObject (hbmTemp);
878 TRACE("created imagelist %p\n", himl);
879 return himl;
881 cleanup:
882 ImageList_Destroy(himl);
883 return NULL;
887 /*************************************************************************
888 * ImageList_Destroy [COMCTL32.@]
890 * Destroys an image list.
892 * PARAMS
893 * himl [I] handle to image list
895 * RETURNS
896 * Success: TRUE
897 * Failure: FALSE
900 BOOL WINAPI
901 ImageList_Destroy (HIMAGELIST himl)
903 if (!is_valid(himl))
904 return FALSE;
906 IImageList_Release((IImageList *) himl);
907 return TRUE;
911 /*************************************************************************
912 * ImageList_DragEnter [COMCTL32.@]
914 * Locks window update and displays the drag image at the given position.
916 * PARAMS
917 * hwndLock [I] handle of the window that owns the drag image.
918 * x [I] X position of the drag image.
919 * y [I] Y position of the drag image.
921 * RETURNS
922 * Success: TRUE
923 * Failure: FALSE
925 * NOTES
926 * The position of the drag image is relative to the window, not
927 * the client area.
930 BOOL WINAPI
931 ImageList_DragEnter (HWND hwndLock, INT x, INT y)
933 TRACE("(hwnd=%p x=%d y=%d)\n", hwndLock, x, y);
935 if (!is_valid(InternalDrag.himl))
936 return FALSE;
938 if (hwndLock)
939 InternalDrag.hwnd = hwndLock;
940 else
941 InternalDrag.hwnd = GetDesktopWindow ();
943 InternalDrag.x = x;
944 InternalDrag.y = y;
946 /* draw the drag image and save the background */
947 return ImageList_DragShowNolock(TRUE);
951 /*************************************************************************
952 * ImageList_DragLeave [COMCTL32.@]
954 * Unlocks window update and hides the drag image.
956 * PARAMS
957 * hwndLock [I] handle of the window that owns the drag image.
959 * RETURNS
960 * Success: TRUE
961 * Failure: FALSE
964 BOOL WINAPI
965 ImageList_DragLeave (HWND hwndLock)
967 /* As we don't save drag info in the window this can lead to problems if
968 an app does not supply the same window as DragEnter */
969 /* if (hwndLock)
970 InternalDrag.hwnd = hwndLock;
971 else
972 InternalDrag.hwnd = GetDesktopWindow (); */
973 if(!hwndLock)
974 hwndLock = GetDesktopWindow();
975 if(InternalDrag.hwnd != hwndLock)
976 FIXME("DragLeave hWnd != DragEnter hWnd\n");
978 ImageList_DragShowNolock (FALSE);
980 return TRUE;
984 /*************************************************************************
985 * ImageList_InternalDragDraw [Internal]
987 * Draws the drag image.
989 * PARAMS
990 * hdc [I] device context to draw into.
991 * x [I] X position of the drag image.
992 * y [I] Y position of the drag image.
994 * RETURNS
995 * Success: TRUE
996 * Failure: FALSE
998 * NOTES
999 * The position of the drag image is relative to the window, not
1000 * the client area.
1004 static inline void
1005 ImageList_InternalDragDraw (HDC hdc, INT x, INT y)
1007 IMAGELISTDRAWPARAMS imldp;
1009 ZeroMemory (&imldp, sizeof(imldp));
1010 imldp.cbSize = sizeof(imldp);
1011 imldp.himl = InternalDrag.himl;
1012 imldp.i = 0;
1013 imldp.hdcDst = hdc,
1014 imldp.x = x;
1015 imldp.y = y;
1016 imldp.rgbBk = CLR_DEFAULT;
1017 imldp.rgbFg = CLR_DEFAULT;
1018 imldp.fStyle = ILD_NORMAL;
1019 imldp.fState = ILS_ALPHA;
1020 imldp.Frame = 192;
1021 ImageList_DrawIndirect (&imldp);
1024 /*************************************************************************
1025 * ImageList_DragMove [COMCTL32.@]
1027 * Moves the drag image.
1029 * PARAMS
1030 * x [I] X position of the drag image.
1031 * y [I] Y position of the drag image.
1033 * RETURNS
1034 * Success: TRUE
1035 * Failure: FALSE
1037 * NOTES
1038 * The position of the drag image is relative to the window, not
1039 * the client area.
1042 BOOL WINAPI
1043 ImageList_DragMove (INT x, INT y)
1045 TRACE("(x=%d y=%d)\n", x, y);
1047 if (!is_valid(InternalDrag.himl))
1048 return FALSE;
1050 /* draw/update the drag image */
1051 if (InternalDrag.bShow) {
1052 HDC hdcDrag;
1053 HDC hdcOffScreen;
1054 HDC hdcBg;
1055 HBITMAP hbmOffScreen;
1056 INT origNewX, origNewY;
1057 INT origOldX, origOldY;
1058 INT origRegX, origRegY;
1059 INT sizeRegX, sizeRegY;
1062 /* calculate the update region */
1063 origNewX = x - InternalDrag.dxHotspot;
1064 origNewY = y - InternalDrag.dyHotspot;
1065 origOldX = InternalDrag.x - InternalDrag.dxHotspot;
1066 origOldY = InternalDrag.y - InternalDrag.dyHotspot;
1067 origRegX = min(origNewX, origOldX);
1068 origRegY = min(origNewY, origOldY);
1069 sizeRegX = InternalDrag.himl->cx + abs(x - InternalDrag.x);
1070 sizeRegY = InternalDrag.himl->cy + abs(y - InternalDrag.y);
1072 hdcDrag = GetDCEx(InternalDrag.hwnd, 0,
1073 DCX_WINDOW | DCX_CACHE | DCX_LOCKWINDOWUPDATE);
1074 hdcOffScreen = CreateCompatibleDC(hdcDrag);
1075 hdcBg = CreateCompatibleDC(hdcDrag);
1077 hbmOffScreen = CreateCompatibleBitmap(hdcDrag, sizeRegX, sizeRegY);
1078 SelectObject(hdcOffScreen, hbmOffScreen);
1079 SelectObject(hdcBg, InternalDrag.hbmBg);
1081 /* get the actual background of the update region */
1082 BitBlt(hdcOffScreen, 0, 0, sizeRegX, sizeRegY, hdcDrag,
1083 origRegX, origRegY, SRCCOPY);
1084 /* erase the old image */
1085 BitBlt(hdcOffScreen, origOldX - origRegX, origOldY - origRegY,
1086 InternalDrag.himl->cx, InternalDrag.himl->cy, hdcBg, 0, 0,
1087 SRCCOPY);
1088 /* save the background */
1089 BitBlt(hdcBg, 0, 0, InternalDrag.himl->cx, InternalDrag.himl->cy,
1090 hdcOffScreen, origNewX - origRegX, origNewY - origRegY, SRCCOPY);
1091 /* draw the image */
1092 ImageList_InternalDragDraw(hdcOffScreen, origNewX - origRegX,
1093 origNewY - origRegY);
1094 /* draw the update region to the screen */
1095 BitBlt(hdcDrag, origRegX, origRegY, sizeRegX, sizeRegY,
1096 hdcOffScreen, 0, 0, SRCCOPY);
1098 DeleteDC(hdcBg);
1099 DeleteDC(hdcOffScreen);
1100 DeleteObject(hbmOffScreen);
1101 ReleaseDC(InternalDrag.hwnd, hdcDrag);
1104 /* update the image position */
1105 InternalDrag.x = x;
1106 InternalDrag.y = y;
1108 return TRUE;
1112 /*************************************************************************
1113 * ImageList_DragShowNolock [COMCTL32.@]
1115 * Shows or hides the drag image.
1117 * PARAMS
1118 * bShow [I] TRUE shows the drag image, FALSE hides it.
1120 * RETURNS
1121 * Success: TRUE
1122 * Failure: FALSE
1125 BOOL WINAPI
1126 ImageList_DragShowNolock (BOOL bShow)
1128 HDC hdcDrag;
1129 HDC hdcBg;
1130 INT x, y;
1132 if (!is_valid(InternalDrag.himl))
1133 return FALSE;
1135 TRACE("bShow=0x%X!\n", bShow);
1137 /* DragImage is already visible/hidden */
1138 if ((InternalDrag.bShow && bShow) || (!InternalDrag.bShow && !bShow)) {
1139 return FALSE;
1142 /* position of the origin of the DragImage */
1143 x = InternalDrag.x - InternalDrag.dxHotspot;
1144 y = InternalDrag.y - InternalDrag.dyHotspot;
1146 hdcDrag = GetDCEx (InternalDrag.hwnd, 0,
1147 DCX_WINDOW | DCX_CACHE | DCX_LOCKWINDOWUPDATE);
1148 if (!hdcDrag) {
1149 return FALSE;
1152 hdcBg = CreateCompatibleDC(hdcDrag);
1153 if (!InternalDrag.hbmBg) {
1154 InternalDrag.hbmBg = CreateCompatibleBitmap(hdcDrag,
1155 InternalDrag.himl->cx, InternalDrag.himl->cy);
1157 SelectObject(hdcBg, InternalDrag.hbmBg);
1159 if (bShow) {
1160 /* save the background */
1161 BitBlt(hdcBg, 0, 0, InternalDrag.himl->cx, InternalDrag.himl->cy,
1162 hdcDrag, x, y, SRCCOPY);
1163 /* show the image */
1164 ImageList_InternalDragDraw(hdcDrag, x, y);
1165 } else {
1166 /* hide the image */
1167 BitBlt(hdcDrag, x, y, InternalDrag.himl->cx, InternalDrag.himl->cy,
1168 hdcBg, 0, 0, SRCCOPY);
1171 InternalDrag.bShow = !InternalDrag.bShow;
1173 DeleteDC(hdcBg);
1174 ReleaseDC (InternalDrag.hwnd, hdcDrag);
1175 return TRUE;
1179 /*************************************************************************
1180 * ImageList_Draw [COMCTL32.@]
1182 * Draws an image.
1184 * PARAMS
1185 * himl [I] handle to image list
1186 * i [I] image index
1187 * hdc [I] handle to device context
1188 * x [I] x position
1189 * y [I] y position
1190 * fStyle [I] drawing flags
1192 * RETURNS
1193 * Success: TRUE
1194 * Failure: FALSE
1196 * SEE
1197 * ImageList_DrawEx.
1200 BOOL WINAPI
1201 ImageList_Draw (HIMAGELIST himl, INT i, HDC hdc, INT x, INT y, UINT fStyle)
1203 return ImageList_DrawEx (himl, i, hdc, x, y, 0, 0,
1204 CLR_DEFAULT, CLR_DEFAULT, fStyle);
1208 /*************************************************************************
1209 * ImageList_DrawEx [COMCTL32.@]
1211 * Draws an image and allows using extended drawing features.
1213 * PARAMS
1214 * himl [I] handle to image list
1215 * i [I] image index
1216 * hdc [I] handle to device context
1217 * x [I] X position
1218 * y [I] Y position
1219 * dx [I] X offset
1220 * dy [I] Y offset
1221 * rgbBk [I] background color
1222 * rgbFg [I] foreground color
1223 * fStyle [I] drawing flags
1225 * RETURNS
1226 * Success: TRUE
1227 * Failure: FALSE
1229 * NOTES
1230 * Calls ImageList_DrawIndirect.
1232 * SEE
1233 * ImageList_DrawIndirect.
1236 BOOL WINAPI
1237 ImageList_DrawEx (HIMAGELIST himl, INT i, HDC hdc, INT x, INT y,
1238 INT dx, INT dy, COLORREF rgbBk, COLORREF rgbFg,
1239 UINT fStyle)
1241 IMAGELISTDRAWPARAMS imldp;
1243 ZeroMemory (&imldp, sizeof(imldp));
1244 imldp.cbSize = sizeof(imldp);
1245 imldp.himl = himl;
1246 imldp.i = i;
1247 imldp.hdcDst = hdc,
1248 imldp.x = x;
1249 imldp.y = y;
1250 imldp.cx = dx;
1251 imldp.cy = dy;
1252 imldp.rgbBk = rgbBk;
1253 imldp.rgbFg = rgbFg;
1254 imldp.fStyle = fStyle;
1256 return ImageList_DrawIndirect (&imldp);
1260 static BOOL alpha_blend_image( HIMAGELIST himl, HDC dest_dc, int dest_x, int dest_y,
1261 int src_x, int src_y, int cx, int cy, BLENDFUNCTION func,
1262 UINT style, COLORREF blend_col )
1264 BOOL ret = FALSE;
1265 HDC hdc;
1266 HBITMAP bmp = 0, mask = 0;
1267 BITMAPINFO *info;
1268 void *bits, *mask_bits;
1269 unsigned int *ptr;
1270 int i, j;
1272 if (!(hdc = CreateCompatibleDC( 0 ))) return FALSE;
1273 if (!(info = HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET( BITMAPINFO, bmiColors[256] )))) goto done;
1274 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1275 info->bmiHeader.biWidth = cx;
1276 info->bmiHeader.biHeight = cy;
1277 info->bmiHeader.biPlanes = 1;
1278 info->bmiHeader.biBitCount = 32;
1279 info->bmiHeader.biCompression = BI_RGB;
1280 info->bmiHeader.biSizeImage = cx * cy * 4;
1281 info->bmiHeader.biXPelsPerMeter = 0;
1282 info->bmiHeader.biYPelsPerMeter = 0;
1283 info->bmiHeader.biClrUsed = 0;
1284 info->bmiHeader.biClrImportant = 0;
1285 if (!(bmp = CreateDIBSection( himl->hdcImage, info, DIB_RGB_COLORS, &bits, 0, 0 ))) goto done;
1286 SelectObject( hdc, bmp );
1287 BitBlt( hdc, 0, 0, cx, cy, himl->hdcImage, src_x, src_y, SRCCOPY );
1289 if (blend_col != CLR_NONE)
1291 BYTE r = GetRValue( blend_col );
1292 BYTE g = GetGValue( blend_col );
1293 BYTE b = GetBValue( blend_col );
1295 if (style & ILD_BLEND25)
1297 for (i = 0, ptr = bits; i < cx * cy; i++, ptr++)
1298 *ptr = ((*ptr & 0xff000000) |
1299 ((((*ptr & 0x00ff0000) * 3 + (r << 16)) / 4) & 0x00ff0000) |
1300 ((((*ptr & 0x0000ff00) * 3 + (g << 8)) / 4) & 0x0000ff00) |
1301 ((((*ptr & 0x000000ff) * 3 + (b << 0)) / 4) & 0x000000ff));
1303 else if (style & ILD_BLEND50)
1305 for (i = 0, ptr = bits; i < cx * cy; i++, ptr++)
1306 *ptr = ((*ptr & 0xff000000) |
1307 ((((*ptr & 0x00ff0000) + (r << 16)) / 2) & 0x00ff0000) |
1308 ((((*ptr & 0x0000ff00) + (g << 8)) / 2) & 0x0000ff00) |
1309 ((((*ptr & 0x000000ff) + (b << 0)) / 2) & 0x000000ff));
1313 if (himl->has_alpha) /* we already have an alpha channel in this case */
1315 /* pre-multiply by the alpha channel */
1316 for (i = 0, ptr = bits; i < cx * cy; i++, ptr++)
1318 DWORD alpha = *ptr >> 24;
1319 *ptr = ((*ptr & 0xff000000) |
1320 (((*ptr & 0x00ff0000) * alpha / 255) & 0x00ff0000) |
1321 (((*ptr & 0x0000ff00) * alpha / 255) & 0x0000ff00) |
1322 (((*ptr & 0x000000ff) * alpha / 255)));
1325 else if (himl->hbmMask)
1327 unsigned int width_bytes = (cx + 31) / 32 * 4;
1328 /* generate alpha channel from the mask */
1329 info->bmiHeader.biBitCount = 1;
1330 info->bmiHeader.biSizeImage = width_bytes * cy;
1331 info->bmiColors[0].rgbRed = 0;
1332 info->bmiColors[0].rgbGreen = 0;
1333 info->bmiColors[0].rgbBlue = 0;
1334 info->bmiColors[0].rgbReserved = 0;
1335 info->bmiColors[1].rgbRed = 0xff;
1336 info->bmiColors[1].rgbGreen = 0xff;
1337 info->bmiColors[1].rgbBlue = 0xff;
1338 info->bmiColors[1].rgbReserved = 0;
1339 if (!(mask = CreateDIBSection( himl->hdcMask, info, DIB_RGB_COLORS, &mask_bits, 0, 0 )))
1340 goto done;
1341 SelectObject( hdc, mask );
1342 BitBlt( hdc, 0, 0, cx, cy, himl->hdcMask, src_x, src_y, SRCCOPY );
1343 SelectObject( hdc, bmp );
1344 for (i = 0, ptr = bits; i < cy; i++)
1345 for (j = 0; j < cx; j++, ptr++)
1346 if ((((BYTE *)mask_bits)[i * width_bytes + j / 8] << (j % 8)) & 0x80) *ptr = 0;
1347 else *ptr |= 0xff000000;
1350 ret = GdiAlphaBlend( dest_dc, dest_x, dest_y, cx, cy, hdc, 0, 0, cx, cy, func );
1352 done:
1353 DeleteDC( hdc );
1354 if (bmp) DeleteObject( bmp );
1355 if (mask) DeleteObject( mask );
1356 HeapFree( GetProcessHeap(), 0, info );
1357 return ret;
1360 /*************************************************************************
1361 * ImageList_DrawIndirect [COMCTL32.@]
1363 * Draws an image using various parameters specified in pimldp.
1365 * PARAMS
1366 * pimldp [I] pointer to IMAGELISTDRAWPARAMS structure.
1368 * RETURNS
1369 * Success: TRUE
1370 * Failure: FALSE
1373 BOOL WINAPI
1374 ImageList_DrawIndirect (IMAGELISTDRAWPARAMS *pimldp)
1376 INT cx, cy, nOvlIdx;
1377 DWORD fState, dwRop;
1378 UINT fStyle;
1379 COLORREF oldImageBk, oldImageFg;
1380 HDC hImageDC, hImageListDC, hMaskListDC;
1381 HBITMAP hImageBmp, hOldImageBmp, hBlendMaskBmp;
1382 BOOL bIsTransparent, bBlend, bResult = FALSE, bMask;
1383 HIMAGELIST himl;
1384 HBRUSH hOldBrush;
1385 POINT pt;
1386 BOOL has_alpha;
1388 if (!pimldp || !(himl = pimldp->himl)) return FALSE;
1389 if (!is_valid(himl)) return FALSE;
1390 if ((pimldp->i < 0) || (pimldp->i >= himl->cCurImage)) return FALSE;
1392 imagelist_point_from_index( himl, pimldp->i, &pt );
1393 pt.x += pimldp->xBitmap;
1394 pt.y += pimldp->yBitmap;
1396 fState = pimldp->cbSize < sizeof(IMAGELISTDRAWPARAMS) ? ILS_NORMAL : pimldp->fState;
1397 fStyle = pimldp->fStyle & ~ILD_OVERLAYMASK;
1398 cx = (pimldp->cx == 0) ? himl->cx : pimldp->cx;
1399 cy = (pimldp->cy == 0) ? himl->cy : pimldp->cy;
1401 bIsTransparent = (fStyle & ILD_TRANSPARENT);
1402 if( pimldp->rgbBk == CLR_NONE )
1403 bIsTransparent = TRUE;
1404 if( ( pimldp->rgbBk == CLR_DEFAULT ) && ( himl->clrBk == CLR_NONE ) )
1405 bIsTransparent = TRUE;
1406 bMask = (himl->flags & ILC_MASK) && (fStyle & ILD_MASK) ;
1407 bBlend = (fStyle & (ILD_BLEND25 | ILD_BLEND50) ) && !bMask;
1409 TRACE("himl(%p) hbmMask(%p) iImage(%d) x(%d) y(%d) cx(%d) cy(%d)\n",
1410 himl, himl->hbmMask, pimldp->i, pimldp->x, pimldp->y, cx, cy);
1412 /* we will use these DCs to access the images and masks in the ImageList */
1413 hImageListDC = himl->hdcImage;
1414 hMaskListDC = himl->hdcMask;
1416 /* these will accumulate the image and mask for the image we're drawing */
1417 hImageDC = CreateCompatibleDC( pimldp->hdcDst );
1418 hImageBmp = CreateCompatibleBitmap( pimldp->hdcDst, cx, cy );
1419 hBlendMaskBmp = bBlend ? CreateBitmap(cx, cy, 1, 1, NULL) : 0;
1421 /* Create a compatible DC. */
1422 if (!hImageListDC || !hImageDC || !hImageBmp ||
1423 (bBlend && !hBlendMaskBmp) || (himl->hbmMask && !hMaskListDC))
1424 goto cleanup;
1426 hOldImageBmp = SelectObject(hImageDC, hImageBmp);
1429 * To obtain a transparent look, background color should be set
1430 * to white and foreground color to black when blitting the
1431 * monochrome mask.
1433 oldImageFg = SetTextColor( hImageDC, RGB( 0, 0, 0 ) );
1434 oldImageBk = SetBkColor( hImageDC, RGB( 0xff, 0xff, 0xff ) );
1436 has_alpha = (himl->has_alpha && himl->has_alpha[pimldp->i]);
1437 if (!bMask && (has_alpha || (fState & ILS_ALPHA)))
1439 COLORREF colour, blend_col = CLR_NONE;
1440 BLENDFUNCTION func;
1442 if (bBlend)
1444 blend_col = pimldp->rgbFg;
1445 if (blend_col == CLR_DEFAULT) blend_col = GetSysColor( COLOR_HIGHLIGHT );
1446 else if (blend_col == CLR_NONE) blend_col = GetTextColor( pimldp->hdcDst );
1449 func.BlendOp = AC_SRC_OVER;
1450 func.BlendFlags = 0;
1451 func.SourceConstantAlpha = (fState & ILS_ALPHA) ? pimldp->Frame : 255;
1452 func.AlphaFormat = AC_SRC_ALPHA;
1454 if (bIsTransparent)
1456 bResult = alpha_blend_image( himl, pimldp->hdcDst, pimldp->x, pimldp->y,
1457 pt.x, pt.y, cx, cy, func, fStyle, blend_col );
1458 goto end;
1460 colour = pimldp->rgbBk;
1461 if (colour == CLR_DEFAULT) colour = himl->clrBk;
1462 if (colour == CLR_NONE) colour = GetBkColor( pimldp->hdcDst );
1464 hOldBrush = SelectObject (hImageDC, CreateSolidBrush (colour));
1465 PatBlt( hImageDC, 0, 0, cx, cy, PATCOPY );
1466 alpha_blend_image( himl, hImageDC, 0, 0, pt.x, pt.y, cx, cy, func, fStyle, blend_col );
1467 DeleteObject (SelectObject (hImageDC, hOldBrush));
1468 bResult = BitBlt( pimldp->hdcDst, pimldp->x, pimldp->y, cx, cy, hImageDC, 0, 0, SRCCOPY );
1469 goto end;
1473 * Draw the initial image
1475 if( bMask ) {
1476 if (himl->hbmMask) {
1477 hOldBrush = SelectObject (hImageDC, CreateSolidBrush (GetTextColor(pimldp->hdcDst)));
1478 PatBlt( hImageDC, 0, 0, cx, cy, PATCOPY );
1479 BitBlt(hImageDC, 0, 0, cx, cy, hMaskListDC, pt.x, pt.y, SRCPAINT);
1480 DeleteObject (SelectObject (hImageDC, hOldBrush));
1481 if( bIsTransparent )
1483 BitBlt ( pimldp->hdcDst, pimldp->x, pimldp->y, cx, cy, hImageDC, 0, 0, SRCAND);
1484 bResult = TRUE;
1485 goto end;
1487 } else {
1488 hOldBrush = SelectObject (hImageDC, GetStockObject(BLACK_BRUSH));
1489 PatBlt( hImageDC, 0, 0, cx, cy, PATCOPY);
1490 SelectObject(hImageDC, hOldBrush);
1492 } else {
1493 /* blend the image with the needed solid background */
1494 COLORREF colour = RGB(0,0,0);
1496 if( !bIsTransparent )
1498 colour = pimldp->rgbBk;
1499 if( colour == CLR_DEFAULT )
1500 colour = himl->clrBk;
1501 if( colour == CLR_NONE )
1502 colour = GetBkColor(pimldp->hdcDst);
1505 hOldBrush = SelectObject (hImageDC, CreateSolidBrush (colour));
1506 PatBlt( hImageDC, 0, 0, cx, cy, PATCOPY );
1507 if (himl->hbmMask)
1509 BitBlt( hImageDC, 0, 0, cx, cy, hMaskListDC, pt.x, pt.y, SRCAND );
1510 BitBlt( hImageDC, 0, 0, cx, cy, hImageListDC, pt.x, pt.y, SRCPAINT );
1512 else
1513 BitBlt( hImageDC, 0, 0, cx, cy, hImageListDC, pt.x, pt.y, SRCCOPY);
1514 DeleteObject (SelectObject (hImageDC, hOldBrush));
1517 /* Time for blending, if required */
1518 if (bBlend) {
1519 HBRUSH hBlendBrush;
1520 COLORREF clrBlend = pimldp->rgbFg;
1521 HDC hBlendMaskDC = hImageListDC;
1522 HBITMAP hOldBitmap;
1524 /* Create the blend Mask */
1525 hOldBitmap = SelectObject(hBlendMaskDC, hBlendMaskBmp);
1526 hBlendBrush = fStyle & ILD_BLEND50 ? himl->hbrBlend50 : himl->hbrBlend25;
1527 hOldBrush = SelectObject(hBlendMaskDC, hBlendBrush);
1528 PatBlt(hBlendMaskDC, 0, 0, cx, cy, PATCOPY);
1529 SelectObject(hBlendMaskDC, hOldBrush);
1531 /* Modify the blend mask if an Image Mask exist */
1532 if(himl->hbmMask) {
1533 BitBlt(hBlendMaskDC, 0, 0, cx, cy, hMaskListDC, pt.x, pt.y, 0x220326); /* NOTSRCAND */
1534 BitBlt(hBlendMaskDC, 0, 0, cx, cy, hBlendMaskDC, 0, 0, NOTSRCCOPY);
1537 /* now apply blend to the current image given the BlendMask */
1538 if (clrBlend == CLR_DEFAULT) clrBlend = GetSysColor (COLOR_HIGHLIGHT);
1539 else if (clrBlend == CLR_NONE) clrBlend = GetTextColor (pimldp->hdcDst);
1540 hOldBrush = SelectObject (hImageDC, CreateSolidBrush(clrBlend));
1541 BitBlt (hImageDC, 0, 0, cx, cy, hBlendMaskDC, 0, 0, 0xB8074A); /* PSDPxax */
1542 DeleteObject(SelectObject(hImageDC, hOldBrush));
1543 SelectObject(hBlendMaskDC, hOldBitmap);
1546 /* Now do the overlay image, if any */
1547 nOvlIdx = (pimldp->fStyle & ILD_OVERLAYMASK) >> 8;
1548 if ( (nOvlIdx >= 1) && (nOvlIdx <= MAX_OVERLAYIMAGE)) {
1549 nOvlIdx = himl->nOvlIdx[nOvlIdx - 1];
1550 if ((nOvlIdx >= 0) && (nOvlIdx < himl->cCurImage)) {
1551 POINT ptOvl;
1552 imagelist_point_from_index( himl, nOvlIdx, &ptOvl );
1553 ptOvl.x += pimldp->xBitmap;
1554 if (himl->hbmMask && !(fStyle & ILD_IMAGE))
1555 BitBlt (hImageDC, 0, 0, cx, cy, hMaskListDC, ptOvl.x, ptOvl.y, SRCAND);
1556 BitBlt (hImageDC, 0, 0, cx, cy, hImageListDC, ptOvl.x, ptOvl.y, SRCPAINT);
1560 if (fState & ILS_SATURATE) FIXME("ILS_SATURATE: unimplemented!\n");
1561 if (fState & ILS_GLOW) FIXME("ILS_GLOW: unimplemented!\n");
1562 if (fState & ILS_SHADOW) FIXME("ILS_SHADOW: unimplemented!\n");
1564 if (fStyle & ILD_PRESERVEALPHA) FIXME("ILD_PRESERVEALPHA: unimplemented!\n");
1565 if (fStyle & ILD_SCALE) FIXME("ILD_SCALE: unimplemented!\n");
1566 if (fStyle & ILD_DPISCALE) FIXME("ILD_DPISCALE: unimplemented!\n");
1568 /* now copy the image to the screen */
1569 dwRop = SRCCOPY;
1570 if (himl->hbmMask && bIsTransparent ) {
1571 COLORREF oldDstFg = SetTextColor(pimldp->hdcDst, RGB( 0, 0, 0 ) );
1572 COLORREF oldDstBk = SetBkColor(pimldp->hdcDst, RGB( 0xff, 0xff, 0xff ));
1573 BitBlt (pimldp->hdcDst, pimldp->x, pimldp->y, cx, cy, hMaskListDC, pt.x, pt.y, SRCAND);
1574 SetBkColor(pimldp->hdcDst, oldDstBk);
1575 SetTextColor(pimldp->hdcDst, oldDstFg);
1576 dwRop = SRCPAINT;
1578 if (fStyle & ILD_ROP) dwRop = pimldp->dwRop;
1579 BitBlt (pimldp->hdcDst, pimldp->x, pimldp->y, cx, cy, hImageDC, 0, 0, dwRop);
1581 bResult = TRUE;
1582 end:
1583 /* cleanup the mess */
1584 SetBkColor(hImageDC, oldImageBk);
1585 SetTextColor(hImageDC, oldImageFg);
1586 SelectObject(hImageDC, hOldImageBmp);
1587 cleanup:
1588 DeleteObject(hBlendMaskBmp);
1589 DeleteObject(hImageBmp);
1590 DeleteDC(hImageDC);
1592 return bResult;
1596 /*************************************************************************
1597 * ImageList_Duplicate [COMCTL32.@]
1599 * Duplicates an image list.
1601 * PARAMS
1602 * himlSrc [I] source image list handle
1604 * RETURNS
1605 * Success: Handle of duplicated image list.
1606 * Failure: NULL
1609 HIMAGELIST WINAPI
1610 ImageList_Duplicate (HIMAGELIST himlSrc)
1612 HIMAGELIST himlDst;
1614 if (!is_valid(himlSrc)) {
1615 ERR("Invalid image list handle!\n");
1616 return NULL;
1619 himlDst = ImageList_Create (himlSrc->cx, himlSrc->cy, himlSrc->flags,
1620 himlSrc->cCurImage, himlSrc->cGrow);
1622 if (himlDst)
1624 SIZE sz;
1626 imagelist_get_bitmap_size(himlSrc, himlSrc->cCurImage, &sz);
1627 BitBlt (himlDst->hdcImage, 0, 0, sz.cx, sz.cy,
1628 himlSrc->hdcImage, 0, 0, SRCCOPY);
1630 if (himlDst->hbmMask)
1631 BitBlt (himlDst->hdcMask, 0, 0, sz.cx, sz.cy,
1632 himlSrc->hdcMask, 0, 0, SRCCOPY);
1634 himlDst->cCurImage = himlSrc->cCurImage;
1635 if (himlSrc->has_alpha && himlDst->has_alpha)
1636 memcpy( himlDst->has_alpha, himlSrc->has_alpha, himlDst->cCurImage );
1638 return himlDst;
1642 /*************************************************************************
1643 * ImageList_EndDrag [COMCTL32.@]
1645 * Finishes a drag operation.
1647 * PARAMS
1648 * no Parameters
1650 * RETURNS
1651 * Success: TRUE
1652 * Failure: FALSE
1655 VOID WINAPI
1656 ImageList_EndDrag (void)
1658 /* cleanup the InternalDrag struct */
1659 InternalDrag.hwnd = 0;
1660 if (InternalDrag.himl != InternalDrag.himlNoCursor)
1661 ImageList_Destroy (InternalDrag.himlNoCursor);
1662 ImageList_Destroy (InternalDrag.himl);
1663 InternalDrag.himlNoCursor = InternalDrag.himl = 0;
1664 InternalDrag.x= 0;
1665 InternalDrag.y= 0;
1666 InternalDrag.dxHotspot = 0;
1667 InternalDrag.dyHotspot = 0;
1668 InternalDrag.bShow = FALSE;
1669 DeleteObject(InternalDrag.hbmBg);
1670 InternalDrag.hbmBg = 0;
1674 /*************************************************************************
1675 * ImageList_GetBkColor [COMCTL32.@]
1677 * Returns the background color of an image list.
1679 * PARAMS
1680 * himl [I] Image list handle.
1682 * RETURNS
1683 * Success: background color
1684 * Failure: CLR_NONE
1687 COLORREF WINAPI
1688 ImageList_GetBkColor (HIMAGELIST himl)
1690 return himl ? himl->clrBk : CLR_NONE;
1694 /*************************************************************************
1695 * ImageList_GetDragImage [COMCTL32.@]
1697 * Returns the handle to the internal drag image list.
1699 * PARAMS
1700 * ppt [O] Pointer to the drag position. Can be NULL.
1701 * pptHotspot [O] Pointer to the position of the hot spot. Can be NULL.
1703 * RETURNS
1704 * Success: Handle of the drag image list.
1705 * Failure: NULL.
1708 HIMAGELIST WINAPI
1709 ImageList_GetDragImage (POINT *ppt, POINT *pptHotspot)
1711 if (is_valid(InternalDrag.himl)) {
1712 if (ppt) {
1713 ppt->x = InternalDrag.x;
1714 ppt->y = InternalDrag.y;
1716 if (pptHotspot) {
1717 pptHotspot->x = InternalDrag.dxHotspot;
1718 pptHotspot->y = InternalDrag.dyHotspot;
1720 return (InternalDrag.himl);
1723 return NULL;
1727 /*************************************************************************
1728 * ImageList_GetFlags [COMCTL32.@]
1730 * Gets the flags of the specified image list.
1732 * PARAMS
1733 * himl [I] Handle to image list
1735 * RETURNS
1736 * Image list flags.
1738 * BUGS
1739 * Stub.
1742 DWORD WINAPI
1743 ImageList_GetFlags(HIMAGELIST himl)
1745 TRACE("%p\n", himl);
1747 return is_valid(himl) ? himl->flags : 0;
1751 /*************************************************************************
1752 * ImageList_GetIcon [COMCTL32.@]
1754 * Creates an icon from a masked image of an image list.
1756 * PARAMS
1757 * himl [I] handle to image list
1758 * i [I] image index
1759 * flags [I] drawing style flags
1761 * RETURNS
1762 * Success: icon handle
1763 * Failure: NULL
1766 HICON WINAPI
1767 ImageList_GetIcon (HIMAGELIST himl, INT i, UINT fStyle)
1769 ICONINFO ii;
1770 HICON hIcon;
1771 HBITMAP hOldDstBitmap;
1772 HDC hdcDst;
1773 POINT pt;
1775 TRACE("%p %d %d\n", himl, i, fStyle);
1776 if (!is_valid(himl) || (i < 0) || (i >= himl->cCurImage)) return NULL;
1778 ii.fIcon = TRUE;
1779 ii.xHotspot = 0;
1780 ii.yHotspot = 0;
1782 /* create colour bitmap */
1783 hdcDst = GetDC(0);
1784 ii.hbmColor = CreateCompatibleBitmap(hdcDst, himl->cx, himl->cy);
1785 ReleaseDC(0, hdcDst);
1787 hdcDst = CreateCompatibleDC(0);
1789 imagelist_point_from_index( himl, i, &pt );
1791 /* draw mask*/
1792 ii.hbmMask = CreateBitmap (himl->cx, himl->cy, 1, 1, NULL);
1793 hOldDstBitmap = SelectObject (hdcDst, ii.hbmMask);
1794 if (himl->hbmMask) {
1795 BitBlt (hdcDst, 0, 0, himl->cx, himl->cy,
1796 himl->hdcMask, pt.x, pt.y, SRCCOPY);
1798 else
1799 PatBlt (hdcDst, 0, 0, himl->cx, himl->cy, BLACKNESS);
1801 /* draw image*/
1802 SelectObject (hdcDst, ii.hbmColor);
1803 BitBlt (hdcDst, 0, 0, himl->cx, himl->cy,
1804 himl->hdcImage, pt.x, pt.y, SRCCOPY);
1807 * CreateIconIndirect requires us to deselect the bitmaps from
1808 * the DCs before calling
1810 SelectObject(hdcDst, hOldDstBitmap);
1812 hIcon = CreateIconIndirect (&ii);
1814 DeleteObject (ii.hbmMask);
1815 DeleteObject (ii.hbmColor);
1816 DeleteDC (hdcDst);
1818 return hIcon;
1822 /*************************************************************************
1823 * ImageList_GetIconSize [COMCTL32.@]
1825 * Retrieves the size of an image in an image list.
1827 * PARAMS
1828 * himl [I] handle to image list
1829 * cx [O] pointer to the image width.
1830 * cy [O] pointer to the image height.
1832 * RETURNS
1833 * Success: TRUE
1834 * Failure: FALSE
1836 * NOTES
1837 * All images in an image list have the same size.
1840 BOOL WINAPI
1841 ImageList_GetIconSize (HIMAGELIST himl, INT *cx, INT *cy)
1843 if (!is_valid(himl) || !cx || !cy)
1844 return FALSE;
1846 *cx = himl->cx;
1847 *cy = himl->cy;
1849 return TRUE;
1853 /*************************************************************************
1854 * ImageList_GetImageCount [COMCTL32.@]
1856 * Returns the number of images in an image list.
1858 * PARAMS
1859 * himl [I] handle to image list
1861 * RETURNS
1862 * Success: Number of images.
1863 * Failure: 0
1866 INT WINAPI
1867 ImageList_GetImageCount (HIMAGELIST himl)
1869 if (!is_valid(himl))
1870 return 0;
1872 return himl->cCurImage;
1876 /*************************************************************************
1877 * ImageList_GetImageInfo [COMCTL32.@]
1879 * Returns information about an image in an image list.
1881 * PARAMS
1882 * himl [I] handle to image list
1883 * i [I] image index
1884 * pImageInfo [O] pointer to the image information
1886 * RETURNS
1887 * Success: TRUE
1888 * Failure: FALSE
1891 BOOL WINAPI
1892 ImageList_GetImageInfo (HIMAGELIST himl, INT i, IMAGEINFO *pImageInfo)
1894 POINT pt;
1896 if (!is_valid(himl) || (pImageInfo == NULL))
1897 return FALSE;
1898 if ((i < 0) || (i >= himl->cCurImage))
1899 return FALSE;
1901 pImageInfo->hbmImage = himl->hbmImage;
1902 pImageInfo->hbmMask = himl->hbmMask;
1904 imagelist_point_from_index( himl, i, &pt );
1905 pImageInfo->rcImage.top = pt.y;
1906 pImageInfo->rcImage.bottom = pt.y + himl->cy;
1907 pImageInfo->rcImage.left = pt.x;
1908 pImageInfo->rcImage.right = pt.x + himl->cx;
1910 return TRUE;
1914 /*************************************************************************
1915 * ImageList_GetImageRect [COMCTL32.@]
1917 * Retrieves the rectangle of the specified image in an image list.
1919 * PARAMS
1920 * himl [I] handle to image list
1921 * i [I] image index
1922 * lpRect [O] pointer to the image rectangle
1924 * RETURNS
1925 * Success: TRUE
1926 * Failure: FALSE
1928 * NOTES
1929 * This is an UNDOCUMENTED function!!!
1932 BOOL WINAPI
1933 ImageList_GetImageRect (HIMAGELIST himl, INT i, LPRECT lpRect)
1935 POINT pt;
1937 if (!is_valid(himl) || (lpRect == NULL))
1938 return FALSE;
1939 if ((i < 0) || (i >= himl->cCurImage))
1940 return FALSE;
1942 imagelist_point_from_index( himl, i, &pt );
1943 lpRect->left = pt.x;
1944 lpRect->top = pt.y;
1945 lpRect->right = pt.x + himl->cx;
1946 lpRect->bottom = pt.y + himl->cy;
1948 return TRUE;
1952 /*************************************************************************
1953 * ImageList_LoadImage [COMCTL32.@]
1954 * ImageList_LoadImageA [COMCTL32.@]
1956 * Creates an image list from a bitmap, icon or cursor.
1958 * See ImageList_LoadImageW.
1961 HIMAGELIST WINAPI
1962 ImageList_LoadImageA (HINSTANCE hi, LPCSTR lpbmp, INT cx, INT cGrow,
1963 COLORREF clrMask, UINT uType, UINT uFlags)
1965 HIMAGELIST himl;
1966 LPWSTR lpbmpW;
1967 DWORD len;
1969 if (IS_INTRESOURCE(lpbmp))
1970 return ImageList_LoadImageW(hi, (LPCWSTR)lpbmp, cx, cGrow, clrMask,
1971 uType, uFlags);
1973 len = MultiByteToWideChar(CP_ACP, 0, lpbmp, -1, NULL, 0);
1974 lpbmpW = Alloc(len * sizeof(WCHAR));
1975 MultiByteToWideChar(CP_ACP, 0, lpbmp, -1, lpbmpW, len);
1977 himl = ImageList_LoadImageW(hi, lpbmpW, cx, cGrow, clrMask, uType, uFlags);
1978 Free (lpbmpW);
1979 return himl;
1983 /*************************************************************************
1984 * ImageList_LoadImageW [COMCTL32.@]
1986 * Creates an image list from a bitmap, icon or cursor.
1988 * PARAMS
1989 * hi [I] instance handle
1990 * lpbmp [I] name or id of the image
1991 * cx [I] width of each image
1992 * cGrow [I] number of images to expand
1993 * clrMask [I] mask color
1994 * uType [I] type of image to load
1995 * uFlags [I] loading flags
1997 * RETURNS
1998 * Success: handle to the loaded image list
1999 * Failure: NULL
2001 * SEE
2002 * LoadImage ()
2005 HIMAGELIST WINAPI
2006 ImageList_LoadImageW (HINSTANCE hi, LPCWSTR lpbmp, INT cx, INT cGrow,
2007 COLORREF clrMask, UINT uType, UINT uFlags)
2009 HIMAGELIST himl = NULL;
2010 HANDLE handle;
2011 INT nImageCount;
2013 handle = LoadImageW (hi, lpbmp, uType, 0, 0, uFlags);
2014 if (!handle) {
2015 WARN("Couldn't load image\n");
2016 return NULL;
2019 if (uType == IMAGE_BITMAP) {
2020 DIBSECTION dib;
2021 UINT color;
2023 if (GetObjectW (handle, sizeof(dib), &dib) == sizeof(BITMAP)) color = ILC_COLOR;
2024 else color = dib.dsBm.bmBitsPixel;
2026 /* To match windows behavior, if cx is set to zero and
2027 the flag DI_DEFAULTSIZE is specified, cx becomes the
2028 system metric value for icons. If the flag is not specified
2029 the function sets the size to the height of the bitmap */
2030 if (cx == 0)
2032 if (uFlags & DI_DEFAULTSIZE)
2033 cx = GetSystemMetrics (SM_CXICON);
2034 else
2035 cx = dib.dsBm.bmHeight;
2038 nImageCount = dib.dsBm.bmWidth / cx;
2040 himl = ImageList_Create (cx, dib.dsBm.bmHeight, ILC_MASK | color, nImageCount, cGrow);
2041 if (!himl) {
2042 DeleteObject (handle);
2043 return NULL;
2045 ImageList_AddMasked (himl, handle, clrMask);
2047 else if ((uType == IMAGE_ICON) || (uType == IMAGE_CURSOR)) {
2048 ICONINFO ii;
2049 BITMAP bmp;
2051 GetIconInfo (handle, &ii);
2052 GetObjectW (ii.hbmColor, sizeof(BITMAP), &bmp);
2053 himl = ImageList_Create (bmp.bmWidth, bmp.bmHeight,
2054 ILC_MASK | ILC_COLOR, 1, cGrow);
2055 if (!himl) {
2056 DeleteObject (ii.hbmColor);
2057 DeleteObject (ii.hbmMask);
2058 DeleteObject (handle);
2059 return NULL;
2061 ImageList_Add (himl, ii.hbmColor, ii.hbmMask);
2062 DeleteObject (ii.hbmColor);
2063 DeleteObject (ii.hbmMask);
2066 DeleteObject (handle);
2068 return himl;
2072 /*************************************************************************
2073 * ImageList_Merge [COMCTL32.@]
2075 * Create an image list containing a merged image from two image lists.
2077 * PARAMS
2078 * himl1 [I] handle to first image list
2079 * i1 [I] first image index
2080 * himl2 [I] handle to second image list
2081 * i2 [I] second image index
2082 * dx [I] X offset of the second image relative to the first.
2083 * dy [I] Y offset of the second image relative to the first.
2085 * RETURNS
2086 * Success: The newly created image list. It contains a single image
2087 * consisting of the second image merged with the first.
2088 * Failure: NULL, if either himl1 or himl2 is invalid.
2090 * NOTES
2091 * - The returned image list should be deleted by the caller using
2092 * ImageList_Destroy() when it is no longer required.
2093 * - If either i1 or i2 is not a valid image index, they will be treated
2094 * as blank images.
2096 HIMAGELIST WINAPI
2097 ImageList_Merge (HIMAGELIST himl1, INT i1, HIMAGELIST himl2, INT i2,
2098 INT dx, INT dy)
2100 HIMAGELIST himlDst = NULL;
2101 INT cxDst, cyDst;
2102 INT xOff1, yOff1, xOff2, yOff2;
2103 POINT pt1, pt2;
2104 INT newFlags;
2106 TRACE("(himl1=%p i1=%d himl2=%p i2=%d dx=%d dy=%d)\n", himl1, i1, himl2,
2107 i2, dx, dy);
2109 if (!is_valid(himl1) || !is_valid(himl2))
2110 return NULL;
2112 if (dx > 0) {
2113 cxDst = max (himl1->cx, dx + himl2->cx);
2114 xOff1 = 0;
2115 xOff2 = dx;
2117 else if (dx < 0) {
2118 cxDst = max (himl2->cx, himl1->cx - dx);
2119 xOff1 = -dx;
2120 xOff2 = 0;
2122 else {
2123 cxDst = max (himl1->cx, himl2->cx);
2124 xOff1 = 0;
2125 xOff2 = 0;
2128 if (dy > 0) {
2129 cyDst = max (himl1->cy, dy + himl2->cy);
2130 yOff1 = 0;
2131 yOff2 = dy;
2133 else if (dy < 0) {
2134 cyDst = max (himl2->cy, himl1->cy - dy);
2135 yOff1 = -dy;
2136 yOff2 = 0;
2138 else {
2139 cyDst = max (himl1->cy, himl2->cy);
2140 yOff1 = 0;
2141 yOff2 = 0;
2144 newFlags = (himl1->flags > himl2->flags ? himl1->flags : himl2->flags) & ILC_COLORDDB;
2145 if (newFlags == ILC_COLORDDB && (himl1->flags & ILC_COLORDDB) == ILC_COLOR16)
2146 newFlags = ILC_COLOR16; /* this is what native (at least v5) does, don't know why */
2147 himlDst = ImageList_Create (cxDst, cyDst, ILC_MASK | newFlags, 1, 1);
2149 if (himlDst)
2151 imagelist_point_from_index( himl1, i1, &pt1 );
2152 imagelist_point_from_index( himl2, i2, &pt2 );
2154 /* copy image */
2155 BitBlt (himlDst->hdcImage, 0, 0, cxDst, cyDst, himl1->hdcImage, 0, 0, BLACKNESS);
2156 if (i1 >= 0 && i1 < himl1->cCurImage)
2157 BitBlt (himlDst->hdcImage, xOff1, yOff1, himl1->cx, himl1->cy, himl1->hdcImage, pt1.x, pt1.y, SRCCOPY);
2158 if (i2 >= 0 && i2 < himl2->cCurImage)
2160 if (himl2->flags & ILC_MASK)
2162 BitBlt (himlDst->hdcImage, xOff2, yOff2, himl2->cx, himl2->cy, himl2->hdcMask , pt2.x, pt2.y, SRCAND);
2163 BitBlt (himlDst->hdcImage, xOff2, yOff2, himl2->cx, himl2->cy, himl2->hdcImage, pt2.x, pt2.y, SRCPAINT);
2165 else
2166 BitBlt (himlDst->hdcImage, xOff2, yOff2, himl2->cx, himl2->cy, himl2->hdcImage, pt2.x, pt2.y, SRCCOPY);
2169 /* copy mask */
2170 BitBlt (himlDst->hdcMask, 0, 0, cxDst, cyDst, himl1->hdcMask, 0, 0, WHITENESS);
2171 if (i1 >= 0 && i1 < himl1->cCurImage)
2172 BitBlt (himlDst->hdcMask, xOff1, yOff1, himl1->cx, himl1->cy, himl1->hdcMask, pt1.x, pt1.y, SRCCOPY);
2173 if (i2 >= 0 && i2 < himl2->cCurImage)
2174 BitBlt (himlDst->hdcMask, xOff2, yOff2, himl2->cx, himl2->cy, himl2->hdcMask, pt2.x, pt2.y, SRCAND);
2176 himlDst->cCurImage = 1;
2179 return himlDst;
2183 /* helper for ImageList_Read, see comments below */
2184 static void *read_bitmap(IStream *pstm, BITMAPINFO *bmi)
2186 BITMAPFILEHEADER bmfh;
2187 int bitsperpixel, palspace;
2188 void *bits;
2190 if (FAILED(IStream_Read ( pstm, &bmfh, sizeof(bmfh), NULL)))
2191 return NULL;
2193 if (bmfh.bfType != (('M'<<8)|'B'))
2194 return NULL;
2196 if (FAILED(IStream_Read ( pstm, &bmi->bmiHeader, sizeof(bmi->bmiHeader), NULL)))
2197 return NULL;
2199 if ((bmi->bmiHeader.biSize != sizeof(bmi->bmiHeader)))
2200 return NULL;
2202 TRACE("width %u, height %u, planes %u, bpp %u\n",
2203 bmi->bmiHeader.biWidth, bmi->bmiHeader.biHeight,
2204 bmi->bmiHeader.biPlanes, bmi->bmiHeader.biBitCount);
2206 bitsperpixel = bmi->bmiHeader.biPlanes * bmi->bmiHeader.biBitCount;
2207 if (bitsperpixel<=8)
2208 palspace = (1<<bitsperpixel)*sizeof(RGBQUAD);
2209 else
2210 palspace = 0;
2212 bmi->bmiHeader.biSizeImage = get_dib_image_size( bmi );
2214 /* read the palette right after the end of the bitmapinfoheader */
2215 if (palspace && FAILED(IStream_Read(pstm, bmi->bmiColors, palspace, NULL)))
2216 return NULL;
2218 bits = Alloc(bmi->bmiHeader.biSizeImage);
2219 if (!bits) return NULL;
2221 if (FAILED(IStream_Read(pstm, bits, bmi->bmiHeader.biSizeImage, NULL)))
2223 Free(bits);
2224 return NULL;
2226 return bits;
2229 /*************************************************************************
2230 * ImageList_Read [COMCTL32.@]
2232 * Reads an image list from a stream.
2234 * PARAMS
2235 * pstm [I] pointer to a stream
2237 * RETURNS
2238 * Success: handle to image list
2239 * Failure: NULL
2241 * The format is like this:
2242 * ILHEAD ilheadstruct;
2244 * for the color image part:
2245 * BITMAPFILEHEADER bmfh;
2246 * BITMAPINFOHEADER bmih;
2247 * only if it has a palette:
2248 * RGBQUAD rgbs[nr_of_paletted_colors];
2250 * BYTE colorbits[imagesize];
2252 * the following only if the ILC_MASK bit is set in ILHEAD.ilFlags:
2253 * BITMAPFILEHEADER bmfh_mask;
2254 * BITMAPINFOHEADER bmih_mask;
2255 * only if it has a palette (it usually does not):
2256 * RGBQUAD rgbs[nr_of_paletted_colors];
2258 * BYTE maskbits[imagesize];
2260 HIMAGELIST WINAPI ImageList_Read(IStream *pstm)
2262 char image_buf[sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * 256];
2263 char mask_buf[sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * 256];
2264 BITMAPINFO *image_info = (BITMAPINFO *)image_buf;
2265 BITMAPINFO *mask_info = (BITMAPINFO *)mask_buf;
2266 void *image_bits, *mask_bits = NULL;
2267 ILHEAD ilHead;
2268 HIMAGELIST himl;
2269 unsigned int i;
2271 TRACE("%p\n", pstm);
2273 if (FAILED(IStream_Read (pstm, &ilHead, sizeof(ILHEAD), NULL)))
2274 return NULL;
2275 if (ilHead.usMagic != (('L' << 8) | 'I'))
2276 return NULL;
2277 if (ilHead.usVersion != 0x101) /* probably version? */
2278 return NULL;
2280 TRACE("cx %u, cy %u, flags 0x%04x, cCurImage %u, cMaxImage %u\n",
2281 ilHead.cx, ilHead.cy, ilHead.flags, ilHead.cCurImage, ilHead.cMaxImage);
2283 himl = ImageList_Create(ilHead.cx, ilHead.cy, ilHead.flags, ilHead.cCurImage, ilHead.cMaxImage);
2284 if (!himl)
2285 return NULL;
2287 if (!(image_bits = read_bitmap(pstm, image_info)))
2289 WARN("failed to read bitmap from stream\n");
2290 return NULL;
2292 if (ilHead.flags & ILC_MASK)
2294 if (!(mask_bits = read_bitmap(pstm, mask_info)))
2296 WARN("failed to read mask bitmap from stream\n");
2297 return NULL;
2300 else mask_info = NULL;
2302 if (himl->has_alpha && image_info->bmiHeader.biBitCount == 32)
2304 DWORD *ptr = image_bits;
2305 BYTE *mask_ptr = mask_bits;
2306 int stride = himl->cy * image_info->bmiHeader.biWidth;
2308 if (image_info->bmiHeader.biHeight > 0) /* bottom-up */
2310 ptr += image_info->bmiHeader.biHeight * image_info->bmiHeader.biWidth - stride;
2311 mask_ptr += (image_info->bmiHeader.biHeight * image_info->bmiHeader.biWidth - stride) / 8;
2312 stride = -stride;
2313 image_info->bmiHeader.biHeight = himl->cy;
2315 else image_info->bmiHeader.biHeight = -himl->cy;
2317 for (i = 0; i < ilHead.cCurImage; i += TILE_COUNT)
2319 add_dib_bits( himl, i, min( ilHead.cCurImage - i, TILE_COUNT ),
2320 himl->cx, himl->cy, image_info, mask_info, ptr, mask_ptr );
2321 ptr += stride;
2322 mask_ptr += stride / 8;
2325 else
2327 StretchDIBits( himl->hdcImage, 0, 0, image_info->bmiHeader.biWidth, image_info->bmiHeader.biHeight,
2328 0, 0, image_info->bmiHeader.biWidth, image_info->bmiHeader.biHeight,
2329 image_bits, image_info, DIB_RGB_COLORS, SRCCOPY);
2330 if (mask_info)
2331 StretchDIBits( himl->hdcMask, 0, 0, mask_info->bmiHeader.biWidth, mask_info->bmiHeader.biHeight,
2332 0, 0, mask_info->bmiHeader.biWidth, mask_info->bmiHeader.biHeight,
2333 mask_bits, mask_info, DIB_RGB_COLORS, SRCCOPY);
2335 Free( image_bits );
2336 Free( mask_bits );
2338 himl->cCurImage = ilHead.cCurImage;
2339 himl->cMaxImage = ilHead.cMaxImage;
2341 ImageList_SetBkColor(himl,ilHead.bkcolor);
2342 for (i=0;i<4;i++)
2343 ImageList_SetOverlayImage(himl,ilHead.ovls[i],i+1);
2344 return himl;
2348 /*************************************************************************
2349 * ImageList_Remove [COMCTL32.@]
2351 * Removes an image from an image list
2353 * PARAMS
2354 * himl [I] image list handle
2355 * i [I] image index
2357 * RETURNS
2358 * Success: TRUE
2359 * Failure: FALSE
2361 * FIXME: as the image list storage test shows, native comctl32 simply shifts
2362 * images without creating a new bitmap.
2364 BOOL WINAPI
2365 ImageList_Remove (HIMAGELIST himl, INT i)
2367 HBITMAP hbmNewImage, hbmNewMask;
2368 HDC hdcBmp;
2369 SIZE sz;
2371 TRACE("(himl=%p i=%d)\n", himl, i);
2373 if (!is_valid(himl)) {
2374 ERR("Invalid image list handle!\n");
2375 return FALSE;
2378 if ((i < -1) || (i >= himl->cCurImage)) {
2379 TRACE("index out of range! %d\n", i);
2380 return FALSE;
2383 if (i == -1) {
2384 INT nCount;
2386 /* remove all */
2387 if (himl->cCurImage == 0) {
2388 /* remove all on empty ImageList is allowed */
2389 TRACE("remove all on empty ImageList!\n");
2390 return TRUE;
2393 himl->cMaxImage = himl->cGrow;
2394 himl->cCurImage = 0;
2395 for (nCount = 0; nCount < MAX_OVERLAYIMAGE; nCount++)
2396 himl->nOvlIdx[nCount] = -1;
2398 if (himl->has_alpha)
2400 HeapFree( GetProcessHeap(), 0, himl->has_alpha );
2401 himl->has_alpha = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, himl->cMaxImage );
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->has_alpha && 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 if (himl->has_alpha)
2944 char *new_alpha = HeapReAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, himl->has_alpha, nNewCount );
2945 if (new_alpha) himl->has_alpha = new_alpha;
2946 else
2948 HeapFree( GetProcessHeap(), 0, himl->has_alpha );
2949 himl->has_alpha = NULL;
2953 /* Update max image count and current image count */
2954 himl->cMaxImage = nNewCount;
2955 himl->cCurImage = iImageCount;
2957 return TRUE;
2961 /*************************************************************************
2962 * ImageList_SetOverlayImage [COMCTL32.@]
2964 * Assigns an overlay mask index to an existing image in an image list.
2966 * PARAMS
2967 * himl [I] handle to image list
2968 * iImage [I] image index
2969 * iOverlay [I] overlay mask index
2971 * RETURNS
2972 * Success: TRUE
2973 * Failure: FALSE
2976 BOOL WINAPI
2977 ImageList_SetOverlayImage (HIMAGELIST himl, INT iImage, INT iOverlay)
2979 if (!is_valid(himl))
2980 return FALSE;
2981 if ((iOverlay < 1) || (iOverlay > MAX_OVERLAYIMAGE))
2982 return FALSE;
2983 if ((iImage!=-1) && ((iImage < 0) || (iImage > himl->cCurImage)))
2984 return FALSE;
2985 himl->nOvlIdx[iOverlay - 1] = iImage;
2986 return TRUE;
2991 /* helper for ImageList_Write - write bitmap to pstm
2992 * currently everything is written as 24 bit RGB, except masks
2994 static BOOL _write_bitmap(HBITMAP hBitmap, IStream *pstm)
2996 LPBITMAPFILEHEADER bmfh;
2997 LPBITMAPINFOHEADER bmih;
2998 LPBYTE data = NULL, lpBits;
2999 BITMAP bm;
3000 INT bitCount, sizeImage, offBits, totalSize;
3001 HDC xdc;
3002 BOOL result = FALSE;
3004 if (!GetObjectW(hBitmap, sizeof(BITMAP), &bm))
3005 return FALSE;
3007 bitCount = bm.bmBitsPixel;
3008 sizeImage = get_dib_stride(bm.bmWidth, bitCount) * bm.bmHeight;
3010 totalSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
3011 if(bitCount <= 8)
3012 totalSize += (1 << bitCount) * sizeof(RGBQUAD);
3013 offBits = totalSize;
3014 totalSize += sizeImage;
3016 data = Alloc(totalSize);
3017 bmfh = (LPBITMAPFILEHEADER)data;
3018 bmih = (LPBITMAPINFOHEADER)(data + sizeof(BITMAPFILEHEADER));
3019 lpBits = data + offBits;
3021 /* setup BITMAPFILEHEADER */
3022 bmfh->bfType = (('M' << 8) | 'B');
3023 bmfh->bfSize = offBits;
3024 bmfh->bfReserved1 = 0;
3025 bmfh->bfReserved2 = 0;
3026 bmfh->bfOffBits = offBits;
3028 /* setup BITMAPINFOHEADER */
3029 bmih->biSize = sizeof(BITMAPINFOHEADER);
3030 bmih->biWidth = bm.bmWidth;
3031 bmih->biHeight = bm.bmHeight;
3032 bmih->biPlanes = 1;
3033 bmih->biBitCount = bitCount;
3034 bmih->biCompression = BI_RGB;
3035 bmih->biSizeImage = sizeImage;
3036 bmih->biXPelsPerMeter = 0;
3037 bmih->biYPelsPerMeter = 0;
3038 bmih->biClrUsed = 0;
3039 bmih->biClrImportant = 0;
3041 xdc = GetDC(0);
3042 result = GetDIBits(xdc, hBitmap, 0, bm.bmHeight, lpBits, (BITMAPINFO *)bmih, DIB_RGB_COLORS) == bm.bmHeight;
3043 ReleaseDC(0, xdc);
3044 if (!result)
3045 goto failed;
3047 TRACE("width %u, height %u, planes %u, bpp %u\n",
3048 bmih->biWidth, bmih->biHeight,
3049 bmih->biPlanes, bmih->biBitCount);
3051 if(FAILED(IStream_Write(pstm, data, totalSize, NULL)))
3052 goto failed;
3054 result = TRUE;
3056 failed:
3057 Free(data);
3059 return result;
3063 /*************************************************************************
3064 * ImageList_Write [COMCTL32.@]
3066 * Writes an image list to a stream.
3068 * PARAMS
3069 * himl [I] handle to image list
3070 * pstm [O] Pointer to a stream.
3072 * RETURNS
3073 * Success: TRUE
3074 * Failure: FALSE
3076 * BUGS
3077 * probably.
3080 BOOL WINAPI ImageList_Write(HIMAGELIST himl, IStream *pstm)
3082 ILHEAD ilHead;
3083 int i;
3085 TRACE("%p %p\n", himl, pstm);
3087 if (!is_valid(himl))
3088 return FALSE;
3090 ilHead.usMagic = (('L' << 8) | 'I');
3091 ilHead.usVersion = 0x101;
3092 ilHead.cCurImage = himl->cCurImage;
3093 ilHead.cMaxImage = himl->cMaxImage;
3094 ilHead.cGrow = himl->cGrow;
3095 ilHead.cx = himl->cx;
3096 ilHead.cy = himl->cy;
3097 ilHead.bkcolor = himl->clrBk;
3098 ilHead.flags = himl->flags;
3099 for(i = 0; i < 4; i++) {
3100 ilHead.ovls[i] = himl->nOvlIdx[i];
3103 TRACE("cx %u, cy %u, flags 0x04%x, cCurImage %u, cMaxImage %u\n",
3104 ilHead.cx, ilHead.cy, ilHead.flags, ilHead.cCurImage, ilHead.cMaxImage);
3106 if(FAILED(IStream_Write(pstm, &ilHead, sizeof(ILHEAD), NULL)))
3107 return FALSE;
3109 /* write the bitmap */
3110 if(!_write_bitmap(himl->hbmImage, pstm))
3111 return FALSE;
3113 /* write the mask if we have one */
3114 if(himl->flags & ILC_MASK) {
3115 if(!_write_bitmap(himl->hbmMask, pstm))
3116 return FALSE;
3119 return TRUE;
3123 static HBITMAP ImageList_CreateImage(HDC hdc, HIMAGELIST himl, UINT count)
3125 HBITMAP hbmNewBitmap;
3126 UINT ilc = (himl->flags & 0xFE);
3127 SIZE sz;
3129 imagelist_get_bitmap_size( himl, count, &sz );
3131 if ((ilc >= ILC_COLOR4 && ilc <= ILC_COLOR32) || ilc == ILC_COLOR)
3133 char buffer[sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD)];
3134 BITMAPINFO *bmi = (BITMAPINFO *)buffer;
3136 TRACE("Creating DIBSection %d x %d, %d Bits per Pixel\n",
3137 sz.cx, sz.cy, himl->uBitsPixel);
3139 memset( buffer, 0, sizeof(buffer) );
3140 bmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
3141 bmi->bmiHeader.biWidth = sz.cx;
3142 bmi->bmiHeader.biHeight = sz.cy;
3143 bmi->bmiHeader.biPlanes = 1;
3144 bmi->bmiHeader.biBitCount = himl->uBitsPixel;
3145 bmi->bmiHeader.biCompression = BI_RGB;
3147 if (himl->uBitsPixel <= ILC_COLOR8)
3149 if (!himl->color_table_set)
3151 /* retrieve the default color map */
3152 HBITMAP tmp = CreateBitmap( 1, 1, 1, 1, NULL );
3153 GetDIBits( hdc, tmp, 0, 0, NULL, bmi, DIB_RGB_COLORS );
3154 DeleteObject( tmp );
3155 if (ilc == ILC_COLOR4)
3157 RGBQUAD tmp;
3158 tmp = bmi->bmiColors[7];
3159 bmi->bmiColors[7] = bmi->bmiColors[8];
3160 bmi->bmiColors[8] = tmp;
3163 else
3165 GetDIBColorTable(himl->hdcImage, 0, 1 << himl->uBitsPixel, bmi->bmiColors);
3168 hbmNewBitmap = CreateDIBSection(hdc, bmi, DIB_RGB_COLORS, NULL, 0, 0);
3170 else /*if (ilc == ILC_COLORDDB)*/
3172 TRACE("Creating Bitmap: %d Bits per Pixel\n", himl->uBitsPixel);
3174 hbmNewBitmap = CreateBitmap (sz.cx, sz.cy, 1, himl->uBitsPixel, NULL);
3176 TRACE("returning %p\n", hbmNewBitmap);
3177 return hbmNewBitmap;
3180 /*************************************************************************
3181 * ImageList_SetColorTable [COMCTL32.@]
3183 * Sets the color table of an image list.
3185 * PARAMS
3186 * himl [I] Handle to the image list.
3187 * uStartIndex [I] The first index to set.
3188 * cEntries [I] Number of entries to set.
3189 * prgb [I] New color information for color table for the image list.
3191 * RETURNS
3192 * Success: Number of entries in the table that were set.
3193 * Failure: Zero.
3195 * SEE
3196 * ImageList_Create(), SetDIBColorTable()
3199 UINT WINAPI
3200 ImageList_SetColorTable(HIMAGELIST himl, UINT uStartIndex, UINT cEntries, const RGBQUAD *prgb)
3202 TRACE("(%p, %d, %d, %p)\n", himl, uStartIndex, cEntries, prgb);
3203 himl->color_table_set = TRUE;
3204 return SetDIBColorTable(himl->hdcImage, uStartIndex, cEntries, prgb);
3207 /*************************************************************************
3208 * ImageList_CoCreateInstance [COMCTL32.@]
3210 * Creates a new imagelist instance and returns an interface pointer to it.
3212 * PARAMS
3213 * rclsid [I] A reference to the CLSID (CLSID_ImageList).
3214 * punkOuter [I] Pointer to IUnknown interface for aggregation, if desired
3215 * riid [I] Identifier of the requested interface.
3216 * ppv [O] Returns the address of the pointer requested, or NULL.
3218 * RETURNS
3219 * Success: S_OK.
3220 * Failure: Error value.
3222 HRESULT WINAPI
3223 ImageList_CoCreateInstance (REFCLSID rclsid, const IUnknown *punkOuter, REFIID riid, void **ppv)
3225 TRACE("(%s,%p,%s,%p)\n", debugstr_guid(rclsid), punkOuter, debugstr_guid(riid), ppv);
3227 if (!IsEqualCLSID(&CLSID_ImageList, rclsid))
3228 return E_NOINTERFACE;
3230 return ImageListImpl_CreateInstance(punkOuter, riid, ppv);
3234 /*************************************************************************
3235 * IImageList implementation
3238 static HRESULT WINAPI ImageListImpl_QueryInterface(IImageList2 *iface,
3239 REFIID iid, void **ppv)
3241 HIMAGELIST imgl = impl_from_IImageList2(iface);
3243 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
3245 if (!ppv) return E_INVALIDARG;
3247 if (IsEqualIID(&IID_IUnknown, iid) ||
3248 IsEqualIID(&IID_IImageList, iid) ||
3249 IsEqualIID(&IID_IImageList2, iid))
3251 *ppv = &imgl->IImageList2_iface;
3253 else
3255 *ppv = NULL;
3256 return E_NOINTERFACE;
3259 IImageList2_AddRef(iface);
3260 return S_OK;
3263 static ULONG WINAPI ImageListImpl_AddRef(IImageList2 *iface)
3265 HIMAGELIST imgl = impl_from_IImageList2(iface);
3266 ULONG ref = InterlockedIncrement(&imgl->ref);
3268 TRACE("(%p) refcount=%u\n", iface, ref);
3269 return ref;
3272 static ULONG WINAPI ImageListImpl_Release(IImageList2 *iface)
3274 HIMAGELIST This = impl_from_IImageList2(iface);
3275 ULONG ref = InterlockedDecrement(&This->ref);
3277 TRACE("(%p) refcount=%u\n", iface, ref);
3279 if (ref == 0)
3281 /* delete image bitmaps */
3282 if (This->hbmImage) DeleteObject (This->hbmImage);
3283 if (This->hbmMask) DeleteObject (This->hbmMask);
3285 /* delete image & mask DCs */
3286 if (This->hdcImage) DeleteDC (This->hdcImage);
3287 if (This->hdcMask) DeleteDC (This->hdcMask);
3289 /* delete blending brushes */
3290 if (This->hbrBlend25) DeleteObject (This->hbrBlend25);
3291 if (This->hbrBlend50) DeleteObject (This->hbrBlend50);
3293 This->IImageList2_iface.lpVtbl = NULL;
3294 HeapFree(GetProcessHeap(), 0, This->has_alpha);
3295 HeapFree(GetProcessHeap(), 0, This);
3298 return ref;
3301 static HRESULT WINAPI ImageListImpl_Add(IImageList2 *iface, HBITMAP hbmImage,
3302 HBITMAP hbmMask, int *pi)
3304 HIMAGELIST imgl = impl_from_IImageList2(iface);
3305 int ret;
3307 if (!pi)
3308 return E_FAIL;
3310 ret = ImageList_Add(imgl, hbmImage, hbmMask);
3312 if (ret == -1)
3313 return E_FAIL;
3315 *pi = ret;
3316 return S_OK;
3319 static HRESULT WINAPI ImageListImpl_ReplaceIcon(IImageList2 *iface, int i,
3320 HICON hicon, int *pi)
3322 HIMAGELIST imgl = impl_from_IImageList2(iface);
3323 int ret;
3325 if (!pi)
3326 return E_FAIL;
3328 ret = ImageList_ReplaceIcon(imgl, i, hicon);
3330 if (ret == -1)
3331 return E_FAIL;
3333 *pi = ret;
3334 return S_OK;
3337 static HRESULT WINAPI ImageListImpl_SetOverlayImage(IImageList2 *iface,
3338 int iImage, int iOverlay)
3340 HIMAGELIST imgl = impl_from_IImageList2(iface);
3341 return ImageList_SetOverlayImage(imgl, iImage, iOverlay) ? S_OK : E_FAIL;
3344 static HRESULT WINAPI ImageListImpl_Replace(IImageList2 *iface, int i,
3345 HBITMAP hbmImage, HBITMAP hbmMask)
3347 HIMAGELIST imgl = impl_from_IImageList2(iface);
3348 return ImageList_Replace(imgl, i, hbmImage, hbmMask) ? S_OK : E_FAIL;
3351 static HRESULT WINAPI ImageListImpl_AddMasked(IImageList2 *iface, HBITMAP hbmImage,
3352 COLORREF crMask, int *pi)
3354 HIMAGELIST imgl = impl_from_IImageList2(iface);
3355 int ret;
3357 if (!pi)
3358 return E_FAIL;
3360 ret = ImageList_AddMasked(imgl, hbmImage, crMask);
3362 if (ret == -1)
3363 return E_FAIL;
3365 *pi = ret;
3366 return S_OK;
3369 static HRESULT WINAPI ImageListImpl_Draw(IImageList2 *iface,
3370 IMAGELISTDRAWPARAMS *pimldp)
3372 HIMAGELIST imgl = impl_from_IImageList2(iface);
3373 HIMAGELIST old_himl;
3374 int ret;
3376 /* As far as I can tell, Windows simply ignores the contents of pimldp->himl
3377 so we shall simulate the same */
3378 old_himl = pimldp->himl;
3379 pimldp->himl = imgl;
3381 ret = ImageList_DrawIndirect(pimldp);
3383 pimldp->himl = old_himl;
3384 return ret ? S_OK : E_INVALIDARG;
3387 static HRESULT WINAPI ImageListImpl_Remove(IImageList2 *iface, int i)
3389 HIMAGELIST imgl = impl_from_IImageList2(iface);
3390 return (ImageList_Remove(imgl, i) == 0) ? E_INVALIDARG : S_OK;
3393 static HRESULT WINAPI ImageListImpl_GetIcon(IImageList2 *iface, int i, UINT flags,
3394 HICON *picon)
3396 HIMAGELIST imgl = impl_from_IImageList2(iface);
3397 HICON hIcon;
3399 if (!picon)
3400 return E_FAIL;
3402 hIcon = ImageList_GetIcon(imgl, i, flags);
3404 if (hIcon == NULL)
3405 return E_FAIL;
3407 *picon = hIcon;
3408 return S_OK;
3411 static HRESULT WINAPI ImageListImpl_GetImageInfo(IImageList2 *iface, int i,
3412 IMAGEINFO *pImageInfo)
3414 HIMAGELIST imgl = impl_from_IImageList2(iface);
3415 return ImageList_GetImageInfo(imgl, i, pImageInfo) ? S_OK : E_FAIL;
3418 static HRESULT WINAPI ImageListImpl_Copy(IImageList2 *iface, int dst_index,
3419 IUnknown *unk_src, int src_index, UINT flags)
3421 HIMAGELIST imgl = impl_from_IImageList2(iface);
3422 IImageList *src = NULL;
3423 HRESULT ret;
3425 if (!unk_src)
3426 return E_FAIL;
3428 /* TODO: Add test for IID_ImageList2 too */
3429 if (FAILED(IUnknown_QueryInterface(unk_src, &IID_IImageList,
3430 (void **) &src)))
3431 return E_FAIL;
3433 if (ImageList_Copy(imgl, dst_index, (HIMAGELIST) src, src_index, flags))
3434 ret = S_OK;
3435 else
3436 ret = E_FAIL;
3438 IImageList_Release(src);
3439 return ret;
3442 static HRESULT WINAPI ImageListImpl_Merge(IImageList2 *iface, int i1,
3443 IUnknown *punk2, int i2, int dx, int dy, REFIID riid, void **ppv)
3445 HIMAGELIST imgl = impl_from_IImageList2(iface);
3446 IImageList *iml2 = NULL;
3447 HIMAGELIST merged;
3448 HRESULT ret = E_FAIL;
3450 TRACE("(%p)->(%d %p %d %d %d %s %p)\n", iface, i1, punk2, i2, dx, dy, debugstr_guid(riid), ppv);
3452 /* TODO: Add test for IID_ImageList2 too */
3453 if (FAILED(IUnknown_QueryInterface(punk2, &IID_IImageList,
3454 (void **) &iml2)))
3455 return E_FAIL;
3457 merged = ImageList_Merge(imgl, i1, (HIMAGELIST) iml2, i2, dx, dy);
3459 /* Get the interface for the new image list */
3460 if (merged)
3462 ret = HIMAGELIST_QueryInterface(merged, riid, ppv);
3463 ImageList_Destroy(merged);
3466 IImageList_Release(iml2);
3467 return ret;
3470 static HRESULT WINAPI ImageListImpl_Clone(IImageList2 *iface, REFIID riid, void **ppv)
3472 HIMAGELIST imgl = impl_from_IImageList2(iface);
3473 HIMAGELIST clone;
3474 HRESULT ret = E_FAIL;
3476 TRACE("(%p)->(%s %p)\n", iface, debugstr_guid(riid), ppv);
3478 clone = ImageList_Duplicate(imgl);
3480 /* Get the interface for the new image list */
3481 if (clone)
3483 ret = HIMAGELIST_QueryInterface(clone, riid, ppv);
3484 ImageList_Destroy(clone);
3487 return ret;
3490 static HRESULT WINAPI ImageListImpl_GetImageRect(IImageList2 *iface, int i,
3491 RECT *prc)
3493 HIMAGELIST imgl = impl_from_IImageList2(iface);
3494 IMAGEINFO info;
3496 if (!prc)
3497 return E_FAIL;
3499 if (!ImageList_GetImageInfo(imgl, i, &info))
3500 return E_FAIL;
3502 *prc = info.rcImage;
3504 return S_OK;
3507 static HRESULT WINAPI ImageListImpl_GetIconSize(IImageList2 *iface, int *cx,
3508 int *cy)
3510 HIMAGELIST imgl = impl_from_IImageList2(iface);
3511 return ImageList_GetIconSize(imgl, cx, cy) ? S_OK : E_INVALIDARG;
3514 static HRESULT WINAPI ImageListImpl_SetIconSize(IImageList2 *iface, int cx,
3515 int cy)
3517 HIMAGELIST imgl = impl_from_IImageList2(iface);
3518 return ImageList_SetIconSize(imgl, cx, cy) ? S_OK : E_FAIL;
3521 static HRESULT WINAPI ImageListImpl_GetImageCount(IImageList2 *iface, int *pi)
3523 HIMAGELIST imgl = impl_from_IImageList2(iface);
3524 *pi = ImageList_GetImageCount(imgl);
3525 return S_OK;
3528 static HRESULT WINAPI ImageListImpl_SetImageCount(IImageList2 *iface, UINT count)
3530 HIMAGELIST imgl = impl_from_IImageList2(iface);
3531 return ImageList_SetImageCount(imgl, count) ? S_OK : E_FAIL;
3534 static HRESULT WINAPI ImageListImpl_SetBkColor(IImageList2 *iface, COLORREF clrBk,
3535 COLORREF *pclr)
3537 HIMAGELIST imgl = impl_from_IImageList2(iface);
3538 *pclr = ImageList_SetBkColor(imgl, clrBk);
3539 return S_OK;
3542 static HRESULT WINAPI ImageListImpl_GetBkColor(IImageList2 *iface, COLORREF *pclr)
3544 HIMAGELIST imgl = impl_from_IImageList2(iface);
3545 *pclr = ImageList_GetBkColor(imgl);
3546 return S_OK;
3549 static HRESULT WINAPI ImageListImpl_BeginDrag(IImageList2 *iface, int iTrack,
3550 int dxHotspot, int dyHotspot)
3552 HIMAGELIST imgl = impl_from_IImageList2(iface);
3553 return ImageList_BeginDrag(imgl, iTrack, dxHotspot, dyHotspot) ? S_OK : E_FAIL;
3556 static HRESULT WINAPI ImageListImpl_EndDrag(IImageList2 *iface)
3558 ImageList_EndDrag();
3559 return S_OK;
3562 static HRESULT WINAPI ImageListImpl_DragEnter(IImageList2 *iface, HWND hwndLock,
3563 int x, int y)
3565 return ImageList_DragEnter(hwndLock, x, y) ? S_OK : E_FAIL;
3568 static HRESULT WINAPI ImageListImpl_DragLeave(IImageList2 *iface, HWND hwndLock)
3570 return ImageList_DragLeave(hwndLock) ? S_OK : E_FAIL;
3573 static HRESULT WINAPI ImageListImpl_DragMove(IImageList2 *iface, int x, int y)
3575 return ImageList_DragMove(x, y) ? S_OK : E_FAIL;
3578 static HRESULT WINAPI ImageListImpl_SetDragCursorImage(IImageList2 *iface,
3579 IUnknown *punk, int iDrag, int dxHotspot, int dyHotspot)
3581 IImageList *iml2 = NULL;
3582 BOOL ret;
3584 if (!punk)
3585 return E_FAIL;
3587 /* TODO: Add test for IID_ImageList2 too */
3588 if (FAILED(IUnknown_QueryInterface(punk, &IID_IImageList,
3589 (void **) &iml2)))
3590 return E_FAIL;
3592 ret = ImageList_SetDragCursorImage((HIMAGELIST) iml2, iDrag, dxHotspot,
3593 dyHotspot);
3595 IImageList_Release(iml2);
3597 return ret ? S_OK : E_FAIL;
3600 static HRESULT WINAPI ImageListImpl_DragShowNolock(IImageList2 *iface, BOOL fShow)
3602 return ImageList_DragShowNolock(fShow) ? S_OK : E_FAIL;
3605 static HRESULT WINAPI ImageListImpl_GetDragImage(IImageList2 *iface, POINT *ppt,
3606 POINT *pptHotspot, REFIID riid, PVOID *ppv)
3608 HRESULT ret = E_FAIL;
3609 HIMAGELIST hNew;
3611 if (!ppv)
3612 return E_FAIL;
3614 hNew = ImageList_GetDragImage(ppt, pptHotspot);
3616 /* Get the interface for the new image list */
3617 if (hNew)
3619 IImageList *idrag = (IImageList*)hNew;
3621 ret = HIMAGELIST_QueryInterface(hNew, riid, ppv);
3622 IImageList_Release(idrag);
3625 return ret;
3628 static HRESULT WINAPI ImageListImpl_GetItemFlags(IImageList2 *iface, int i,
3629 DWORD *dwFlags)
3631 FIXME("STUB: %p %d %p\n", iface, i, dwFlags);
3632 return E_NOTIMPL;
3635 static HRESULT WINAPI ImageListImpl_GetOverlayImage(IImageList2 *iface, int iOverlay,
3636 int *piIndex)
3638 HIMAGELIST This = impl_from_IImageList2(iface);
3639 int i;
3641 if ((iOverlay < 0) || (iOverlay > This->cCurImage))
3642 return E_FAIL;
3644 for (i = 0; i < MAX_OVERLAYIMAGE; i++)
3646 if (This->nOvlIdx[i] == iOverlay)
3648 *piIndex = i + 1;
3649 return S_OK;
3653 return E_FAIL;
3656 static HRESULT WINAPI ImageListImpl_Resize(IImageList2 *iface, INT cx, INT cy)
3658 FIXME("(%p)->(%d %d): stub\n", iface, cx, cy);
3659 return E_NOTIMPL;
3662 static HRESULT WINAPI ImageListImpl_GetOriginalSize(IImageList2 *iface, INT image, DWORD flags, INT *cx, INT *cy)
3664 FIXME("(%p)->(%d %x %p %p): stub\n", iface, image, flags, cx, cy);
3665 return E_NOTIMPL;
3668 static HRESULT WINAPI ImageListImpl_SetOriginalSize(IImageList2 *iface, INT image, INT cx, INT cy)
3670 FIXME("(%p)->(%d %d %d): stub\n", iface, image, cx, cy);
3671 return E_NOTIMPL;
3674 static HRESULT WINAPI ImageListImpl_SetCallback(IImageList2 *iface, IUnknown *callback)
3676 FIXME("(%p)->(%p): stub\n", iface, callback);
3677 return E_NOTIMPL;
3680 static HRESULT WINAPI ImageListImpl_GetCallback(IImageList2 *iface, REFIID riid, void **ppv)
3682 FIXME("(%p)->(%s %p): stub\n", iface, debugstr_guid(riid), ppv);
3683 return E_NOTIMPL;
3686 static HRESULT WINAPI ImageListImpl_ForceImagePresent(IImageList2 *iface, INT image, DWORD flags)
3688 FIXME("(%p)->(%d %x): stub\n", iface, image, flags);
3689 return E_NOTIMPL;
3692 static HRESULT WINAPI ImageListImpl_DiscardImages(IImageList2 *iface, INT first_image, INT last_image, DWORD flags)
3694 FIXME("(%p)->(%d %d %x): stub\n", iface, first_image, last_image, flags);
3695 return E_NOTIMPL;
3698 static HRESULT WINAPI ImageListImpl_PreloadImages(IImageList2 *iface, IMAGELISTDRAWPARAMS *params)
3700 FIXME("(%p)->(%p): stub\n", iface, params);
3701 return E_NOTIMPL;
3704 static HRESULT WINAPI ImageListImpl_GetStatistics(IImageList2 *iface, IMAGELISTSTATS *stats)
3706 FIXME("(%p)->(%p): stub\n", iface, stats);
3707 return E_NOTIMPL;
3710 static HRESULT WINAPI ImageListImpl_Initialize(IImageList2 *iface, INT cx, INT cy, UINT flags, INT initial, INT grow)
3712 FIXME("(%p)->(%d %d %d %d %d): stub\n", iface, cx, cy, flags, initial, grow);
3713 return E_NOTIMPL;
3716 static HRESULT WINAPI ImageListImpl_Replace2(IImageList2 *iface, INT i, HBITMAP image, HBITMAP mask, IUnknown *unk, DWORD flags)
3718 FIXME("(%p)->(%d %p %p %p %x): stub\n", iface, i, image, mask, unk, flags);
3719 return E_NOTIMPL;
3722 static HRESULT WINAPI ImageListImpl_ReplaceFromImageList(IImageList2 *iface, INT i, IImageList *imagelist, INT src,
3723 IUnknown *unk, DWORD flags)
3725 FIXME("(%p)->(%d %p %d %p %x): stub\n", iface, i, imagelist, src, unk, flags);
3726 return E_NOTIMPL;
3729 static const IImageList2Vtbl ImageListImpl_Vtbl = {
3730 ImageListImpl_QueryInterface,
3731 ImageListImpl_AddRef,
3732 ImageListImpl_Release,
3733 ImageListImpl_Add,
3734 ImageListImpl_ReplaceIcon,
3735 ImageListImpl_SetOverlayImage,
3736 ImageListImpl_Replace,
3737 ImageListImpl_AddMasked,
3738 ImageListImpl_Draw,
3739 ImageListImpl_Remove,
3740 ImageListImpl_GetIcon,
3741 ImageListImpl_GetImageInfo,
3742 ImageListImpl_Copy,
3743 ImageListImpl_Merge,
3744 ImageListImpl_Clone,
3745 ImageListImpl_GetImageRect,
3746 ImageListImpl_GetIconSize,
3747 ImageListImpl_SetIconSize,
3748 ImageListImpl_GetImageCount,
3749 ImageListImpl_SetImageCount,
3750 ImageListImpl_SetBkColor,
3751 ImageListImpl_GetBkColor,
3752 ImageListImpl_BeginDrag,
3753 ImageListImpl_EndDrag,
3754 ImageListImpl_DragEnter,
3755 ImageListImpl_DragLeave,
3756 ImageListImpl_DragMove,
3757 ImageListImpl_SetDragCursorImage,
3758 ImageListImpl_DragShowNolock,
3759 ImageListImpl_GetDragImage,
3760 ImageListImpl_GetItemFlags,
3761 ImageListImpl_GetOverlayImage,
3762 ImageListImpl_Resize,
3763 ImageListImpl_GetOriginalSize,
3764 ImageListImpl_SetOriginalSize,
3765 ImageListImpl_SetCallback,
3766 ImageListImpl_GetCallback,
3767 ImageListImpl_ForceImagePresent,
3768 ImageListImpl_DiscardImages,
3769 ImageListImpl_PreloadImages,
3770 ImageListImpl_GetStatistics,
3771 ImageListImpl_Initialize,
3772 ImageListImpl_Replace2,
3773 ImageListImpl_ReplaceFromImageList
3776 static BOOL is_valid(HIMAGELIST himl)
3778 BOOL valid;
3779 __TRY
3781 valid = himl && himl->IImageList2_iface.lpVtbl == &ImageListImpl_Vtbl;
3783 __EXCEPT_PAGE_FAULT
3785 valid = FALSE;
3787 __ENDTRY
3788 return valid;
3791 /*************************************************************************
3792 * HIMAGELIST_QueryInterface [COMCTL32.@]
3794 * Returns a pointer to an IImageList or IImageList2 object for the given
3795 * HIMAGELIST.
3797 * PARAMS
3798 * himl [I] Image list handle.
3799 * riid [I] Identifier of the requested interface.
3800 * ppv [O] Returns the address of the pointer requested, or NULL.
3802 * RETURNS
3803 * Success: S_OK.
3804 * Failure: Error value.
3806 HRESULT WINAPI
3807 HIMAGELIST_QueryInterface (HIMAGELIST himl, REFIID riid, void **ppv)
3809 TRACE("(%p,%s,%p)\n", himl, debugstr_guid(riid), ppv);
3810 return IImageList2_QueryInterface((IImageList2 *) himl, riid, ppv);
3813 static HRESULT ImageListImpl_CreateInstance(const IUnknown *pUnkOuter, REFIID iid, void** ppv)
3815 HIMAGELIST This;
3816 HRESULT ret;
3818 TRACE("(%p,%s,%p)\n", pUnkOuter, debugstr_guid(iid), ppv);
3820 *ppv = NULL;
3822 if (pUnkOuter) return CLASS_E_NOAGGREGATION;
3824 This = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct _IMAGELIST));
3825 if (!This) return E_OUTOFMEMORY;
3827 This->IImageList2_iface.lpVtbl = &ImageListImpl_Vtbl;
3828 This->ref = 1;
3830 ret = IImageList2_QueryInterface(&This->IImageList2_iface, iid, ppv);
3831 IImageList2_Release(&This->IImageList2_iface);
3833 return ret;