reg: Do not allow combinations of /v, /ve or /va in the 'delete' function.
[wine.git] / dlls / comctl32 / imagelist.c
blobc34eb0a984a2d91024c6aa54916253763ff293c1
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;
89 LONG ref; /* reference count */
92 #define IMAGELIST_MAGIC 0x53414D58
94 /* Header used by ImageList_Read() and ImageList_Write() */
95 #include "pshpack2.h"
96 typedef struct _ILHEAD
98 USHORT usMagic;
99 USHORT usVersion;
100 WORD cCurImage;
101 WORD cMaxImage;
102 WORD cGrow;
103 WORD cx;
104 WORD cy;
105 COLORREF bkcolor;
106 WORD flags;
107 SHORT ovls[4];
108 } ILHEAD;
109 #include "poppack.h"
111 /* internal image list data used for Drag & Drop operations */
112 typedef struct
114 HWND hwnd;
115 HIMAGELIST himl;
116 HIMAGELIST himlNoCursor;
117 /* position of the drag image relative to the window */
118 INT x;
119 INT y;
120 /* offset of the hotspot relative to the origin of the image */
121 INT dxHotspot;
122 INT dyHotspot;
123 /* is the drag image visible */
124 BOOL bShow;
125 /* saved background */
126 HBITMAP hbmBg;
127 } INTERNALDRAG;
129 static INTERNALDRAG InternalDrag = { 0, 0, 0, 0, 0, 0, 0, FALSE, 0 };
131 static inline HIMAGELIST impl_from_IImageList2(IImageList2 *iface)
133 return CONTAINING_RECORD(iface, struct _IMAGELIST, IImageList2_iface);
136 static HBITMAP ImageList_CreateImage(HDC hdc, HIMAGELIST himl, UINT count);
137 static HRESULT ImageListImpl_CreateInstance(const IUnknown *pUnkOuter, REFIID iid, void** ppv);
138 static BOOL is_valid(HIMAGELIST himl);
141 * An imagelist with N images is tiled like this:
143 * N/4 ->
145 * 4 048C..
146 * 159D..
147 * | 26AE.N
148 * V 37BF.
151 #define TILE_COUNT 4
153 static inline UINT imagelist_height( UINT count )
155 return ((count + TILE_COUNT - 1)/TILE_COUNT);
158 static inline void imagelist_point_from_index( HIMAGELIST himl, UINT index, LPPOINT pt )
160 pt->x = (index%TILE_COUNT) * himl->cx;
161 pt->y = (index/TILE_COUNT) * himl->cy;
164 static inline void imagelist_get_bitmap_size( HIMAGELIST himl, UINT count, SIZE *sz )
166 sz->cx = himl->cx * TILE_COUNT;
167 sz->cy = imagelist_height( count ) * himl->cy;
170 static inline int get_dib_stride( int width, int bpp )
172 return ((width * bpp + 31) >> 3) & ~3;
175 static inline int get_dib_image_size( const BITMAPINFO *info )
177 return get_dib_stride( info->bmiHeader.biWidth, info->bmiHeader.biBitCount )
178 * abs( info->bmiHeader.biHeight );
182 * imagelist_copy_images()
184 * Copies a block of count images from offset src in the list to offset dest.
185 * Images are copied a row at at time. Assumes hdcSrc and hdcDest are different.
187 static inline void imagelist_copy_images( HIMAGELIST himl, HDC hdcSrc, HDC hdcDest,
188 UINT src, UINT count, UINT dest )
190 POINT ptSrc, ptDest;
191 SIZE sz;
192 UINT i;
194 for ( i=0; i<TILE_COUNT; i++ )
196 imagelist_point_from_index( himl, src+i, &ptSrc );
197 imagelist_point_from_index( himl, dest+i, &ptDest );
198 sz.cx = himl->cx;
199 sz.cy = himl->cy * imagelist_height( count - i );
201 BitBlt( hdcDest, ptDest.x, ptDest.y, sz.cx, sz.cy,
202 hdcSrc, ptSrc.x, ptSrc.y, SRCCOPY );
206 static void add_dib_bits( HIMAGELIST himl, int pos, int count, int width, int height,
207 BITMAPINFO *info, BITMAPINFO *mask_info, DWORD *bits, BYTE *mask_bits )
209 int i, j, n;
210 POINT pt;
211 int stride = info->bmiHeader.biWidth;
212 int mask_stride = (info->bmiHeader.biWidth + 31) / 32 * 4;
214 for (n = 0; n < count; n++)
216 BOOL has_alpha = FALSE;
218 imagelist_point_from_index( himl, pos + n, &pt );
220 /* check if bitmap has an alpha channel */
221 for (i = 0; i < height && !has_alpha; i++)
222 for (j = n * width; j < (n + 1) * width; j++)
223 if ((has_alpha = ((bits[i * stride + j] & 0xff000000) != 0))) break;
225 if (!has_alpha) /* generate alpha channel from the mask */
227 for (i = 0; i < height; i++)
228 for (j = n * width; j < (n + 1) * width; j++)
229 if (!mask_info || !((mask_bits[i * mask_stride + j / 8] << (j % 8)) & 0x80))
230 bits[i * stride + j] |= 0xff000000;
231 else
232 bits[i * stride + j] = 0;
234 else
236 himl->has_alpha[pos + n] = 1;
238 if (mask_info && himl->hbmMask) /* generate the mask from the alpha channel */
240 for (i = 0; i < height; i++)
241 for (j = n * width; j < (n + 1) * width; j++)
242 if ((bits[i * stride + j] >> 24) > 25) /* more than 10% alpha */
243 mask_bits[i * mask_stride + j / 8] &= ~(0x80 >> (j % 8));
244 else
245 mask_bits[i * mask_stride + j / 8] |= 0x80 >> (j % 8);
248 StretchDIBits( himl->hdcImage, pt.x, pt.y, himl->cx, himl->cy,
249 n * width, 0, width, height, bits, info, DIB_RGB_COLORS, SRCCOPY );
250 if (mask_info)
251 StretchDIBits( himl->hdcMask, pt.x, pt.y, himl->cx, himl->cy,
252 n * width, 0, width, height, mask_bits, mask_info, DIB_RGB_COLORS, SRCCOPY );
256 /* add images with an alpha channel when the image list is 32 bpp */
257 static BOOL add_with_alpha( HIMAGELIST himl, HDC hdc, int pos, int count,
258 int width, int height, HBITMAP hbmImage, HBITMAP hbmMask )
260 BOOL ret = FALSE;
261 BITMAP bm;
262 BITMAPINFO *info, *mask_info = NULL;
263 DWORD *bits = NULL;
264 BYTE *mask_bits = NULL;
265 DWORD mask_width;
267 if (!GetObjectW( hbmImage, sizeof(bm), &bm )) return FALSE;
269 /* if either the imagelist or the source bitmap don't have an alpha channel, bail out now */
270 if (!himl->has_alpha) return FALSE;
271 if (bm.bmBitsPixel != 32) return FALSE;
273 SelectObject( hdc, hbmImage );
274 mask_width = (bm.bmWidth + 31) / 32 * 4;
276 if (!(info = HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET( BITMAPINFO, bmiColors[256] )))) goto done;
277 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
278 info->bmiHeader.biWidth = bm.bmWidth;
279 info->bmiHeader.biHeight = -height;
280 info->bmiHeader.biPlanes = 1;
281 info->bmiHeader.biBitCount = 32;
282 info->bmiHeader.biCompression = BI_RGB;
283 info->bmiHeader.biSizeImage = bm.bmWidth * height * 4;
284 info->bmiHeader.biXPelsPerMeter = 0;
285 info->bmiHeader.biYPelsPerMeter = 0;
286 info->bmiHeader.biClrUsed = 0;
287 info->bmiHeader.biClrImportant = 0;
288 if (!(bits = HeapAlloc( GetProcessHeap(), 0, info->bmiHeader.biSizeImage ))) goto done;
289 if (!GetDIBits( hdc, hbmImage, 0, height, bits, info, DIB_RGB_COLORS )) goto done;
291 if (hbmMask)
293 if (!(mask_info = HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET( BITMAPINFO, bmiColors[2] ))))
294 goto done;
295 mask_info->bmiHeader = info->bmiHeader;
296 mask_info->bmiHeader.biBitCount = 1;
297 mask_info->bmiHeader.biSizeImage = mask_width * height;
298 if (!(mask_bits = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, info->bmiHeader.biSizeImage )))
299 goto done;
300 if (!GetDIBits( hdc, hbmMask, 0, height, mask_bits, mask_info, DIB_RGB_COLORS )) goto done;
303 add_dib_bits( himl, pos, count, width, height, info, mask_info, bits, mask_bits );
304 ret = TRUE;
306 done:
307 HeapFree( GetProcessHeap(), 0, info );
308 HeapFree( GetProcessHeap(), 0, mask_info );
309 HeapFree( GetProcessHeap(), 0, bits );
310 HeapFree( GetProcessHeap(), 0, mask_bits );
311 return ret;
314 /*************************************************************************
315 * IMAGELIST_InternalExpandBitmaps [Internal]
317 * Expands the bitmaps of an image list by the given number of images.
319 * PARAMS
320 * himl [I] handle to image list
321 * nImageCount [I] number of images to add
323 * RETURNS
324 * nothing
326 * NOTES
327 * This function CANNOT be used to reduce the number of images.
329 static void
330 IMAGELIST_InternalExpandBitmaps(HIMAGELIST himl, INT nImageCount)
332 HDC hdcBitmap;
333 HBITMAP hbmNewBitmap, hbmNull;
334 INT nNewCount;
335 SIZE sz;
337 TRACE("%p has allocated %d, max %d, grow %d images\n", himl, himl->cCurImage, himl->cMaxImage, himl->cGrow);
339 if (himl->cCurImage + nImageCount < himl->cMaxImage)
340 return;
342 nNewCount = himl->cMaxImage + max(nImageCount, himl->cGrow) + 1;
344 imagelist_get_bitmap_size(himl, nNewCount, &sz);
346 TRACE("Create expanded bitmaps : himl=%p x=%d y=%d count=%d\n", himl, sz.cx, sz.cy, nNewCount);
347 hdcBitmap = CreateCompatibleDC (0);
349 hbmNewBitmap = ImageList_CreateImage(hdcBitmap, himl, nNewCount);
351 if (hbmNewBitmap == 0)
352 ERR("creating new image bitmap (x=%d y=%d)!\n", sz.cx, sz.cy);
354 if (himl->cCurImage)
356 hbmNull = SelectObject (hdcBitmap, hbmNewBitmap);
357 BitBlt (hdcBitmap, 0, 0, sz.cx, sz.cy,
358 himl->hdcImage, 0, 0, SRCCOPY);
359 SelectObject (hdcBitmap, hbmNull);
361 SelectObject (himl->hdcImage, hbmNewBitmap);
362 DeleteObject (himl->hbmImage);
363 himl->hbmImage = hbmNewBitmap;
365 if (himl->flags & ILC_MASK)
367 hbmNewBitmap = CreateBitmap (sz.cx, sz.cy, 1, 1, NULL);
369 if (hbmNewBitmap == 0)
370 ERR("creating new mask bitmap!\n");
372 if(himl->cCurImage)
374 hbmNull = SelectObject (hdcBitmap, hbmNewBitmap);
375 BitBlt (hdcBitmap, 0, 0, sz.cx, sz.cy,
376 himl->hdcMask, 0, 0, SRCCOPY);
377 SelectObject (hdcBitmap, hbmNull);
379 SelectObject (himl->hdcMask, hbmNewBitmap);
380 DeleteObject (himl->hbmMask);
381 himl->hbmMask = hbmNewBitmap;
384 if (himl->has_alpha)
386 char *new_alpha = HeapReAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, himl->has_alpha, nNewCount );
387 if (new_alpha) himl->has_alpha = new_alpha;
388 else
390 HeapFree( GetProcessHeap(), 0, himl->has_alpha );
391 himl->has_alpha = NULL;
395 himl->cMaxImage = nNewCount;
397 DeleteDC (hdcBitmap);
401 /*************************************************************************
402 * ImageList_Add [COMCTL32.@]
404 * Add an image or images to an image list.
406 * PARAMS
407 * himl [I] handle to image list
408 * hbmImage [I] handle to image bitmap
409 * hbmMask [I] handle to mask bitmap
411 * RETURNS
412 * Success: Index of the first new image.
413 * Failure: -1
416 INT WINAPI
417 ImageList_Add (HIMAGELIST himl, HBITMAP hbmImage, HBITMAP hbmMask)
419 HDC hdcBitmap, hdcTemp = 0;
420 INT nFirstIndex, nImageCount, i;
421 BITMAP bmp;
422 POINT pt;
424 TRACE("himl=%p hbmimage=%p hbmmask=%p\n", himl, hbmImage, hbmMask);
425 if (!is_valid(himl))
426 return -1;
428 if (!GetObjectW(hbmImage, sizeof(BITMAP), &bmp))
429 return -1;
431 TRACE("himl %p, cCurImage %d, cMaxImage %d, cGrow %d, cx %d, cy %d\n",
432 himl, himl->cCurImage, himl->cMaxImage, himl->cGrow, himl->cx, himl->cy);
434 nImageCount = bmp.bmWidth / himl->cx;
436 TRACE("%p has %d images (%d x %d)\n", hbmImage, nImageCount, bmp.bmWidth, bmp.bmHeight);
438 IMAGELIST_InternalExpandBitmaps(himl, nImageCount);
440 hdcBitmap = CreateCompatibleDC(0);
442 SelectObject(hdcBitmap, hbmImage);
444 if (add_with_alpha( himl, hdcBitmap, himl->cCurImage, nImageCount,
445 himl->cx, min( himl->cy, bmp.bmHeight), hbmImage, hbmMask ))
446 goto done;
448 if (himl->hbmMask)
450 hdcTemp = CreateCompatibleDC(0);
451 SelectObject(hdcTemp, hbmMask);
454 for (i=0; i<nImageCount; i++)
456 imagelist_point_from_index( himl, himl->cCurImage + i, &pt );
458 /* Copy result to the imagelist
460 BitBlt( himl->hdcImage, pt.x, pt.y, himl->cx, bmp.bmHeight,
461 hdcBitmap, i*himl->cx, 0, SRCCOPY );
463 if (!himl->hbmMask)
464 continue;
466 BitBlt( himl->hdcMask, pt.x, pt.y, himl->cx, bmp.bmHeight,
467 hdcTemp, i*himl->cx, 0, SRCCOPY );
469 /* Remove the background from the image
471 BitBlt( himl->hdcImage, pt.x, pt.y, himl->cx, bmp.bmHeight,
472 himl->hdcMask, pt.x, pt.y, 0x220326 ); /* NOTSRCAND */
474 if (hdcTemp) DeleteDC(hdcTemp);
476 done:
477 DeleteDC(hdcBitmap);
479 nFirstIndex = himl->cCurImage;
480 himl->cCurImage += nImageCount;
482 return nFirstIndex;
486 /*************************************************************************
487 * ImageList_AddIcon [COMCTL32.@]
489 * Adds an icon to an image list.
491 * PARAMS
492 * himl [I] handle to image list
493 * hIcon [I] handle to icon
495 * RETURNS
496 * Success: index of the new image
497 * Failure: -1
499 #undef ImageList_AddIcon
500 INT WINAPI ImageList_AddIcon (HIMAGELIST himl, HICON hIcon)
502 return ImageList_ReplaceIcon (himl, -1, hIcon);
506 /*************************************************************************
507 * ImageList_AddMasked [COMCTL32.@]
509 * Adds an image or images to an image list and creates a mask from the
510 * specified bitmap using the mask color.
512 * PARAMS
513 * himl [I] handle to image list.
514 * hBitmap [I] handle to bitmap
515 * clrMask [I] mask color.
517 * RETURNS
518 * Success: Index of the first new image.
519 * Failure: -1
522 INT WINAPI
523 ImageList_AddMasked (HIMAGELIST himl, HBITMAP hBitmap, COLORREF clrMask)
525 HDC hdcMask, hdcBitmap;
526 INT ret;
527 BITMAP bmp;
528 HBITMAP hMaskBitmap;
529 COLORREF bkColor;
531 TRACE("himl=%p hbitmap=%p clrmask=%x\n", himl, hBitmap, clrMask);
532 if (!is_valid(himl))
533 return -1;
535 if (!GetObjectW(hBitmap, sizeof(BITMAP), &bmp))
536 return -1;
538 hdcBitmap = CreateCompatibleDC(0);
539 SelectObject(hdcBitmap, hBitmap);
541 /* Create a temp Mask so we can remove the background of the Image */
542 hdcMask = CreateCompatibleDC(0);
543 hMaskBitmap = CreateBitmap(bmp.bmWidth, bmp.bmHeight, 1, 1, NULL);
544 SelectObject(hdcMask, hMaskBitmap);
546 /* create monochrome image to the mask bitmap */
547 bkColor = (clrMask != CLR_DEFAULT) ? clrMask : GetPixel (hdcBitmap, 0, 0);
548 SetBkColor (hdcBitmap, bkColor);
549 BitBlt (hdcMask, 0, 0, bmp.bmWidth, bmp.bmHeight, hdcBitmap, 0, 0, SRCCOPY);
552 * Remove the background from the image
554 * WINDOWS BUG ALERT!!!!!!
555 * The statement below should not be done in common practice
556 * but this is how ImageList_AddMasked works in Windows.
557 * It overwrites the original bitmap passed, this was discovered
558 * by using the same bitmap to iterate the different styles
559 * on windows where it failed (BUT ImageList_Add is OK)
560 * This is here in case some apps rely on this bug
562 * Blt mode 0x220326 is NOTSRCAND
564 if (bmp.bmBitsPixel > 8) /* NOTSRCAND can't work with palettes */
566 SetBkColor(hdcBitmap, RGB(255,255,255));
567 BitBlt(hdcBitmap, 0, 0, bmp.bmWidth, bmp.bmHeight, hdcMask, 0, 0, 0x220326);
570 DeleteDC(hdcBitmap);
571 DeleteDC(hdcMask);
573 ret = ImageList_Add( himl, hBitmap, hMaskBitmap );
575 DeleteObject(hMaskBitmap);
576 return ret;
580 /*************************************************************************
581 * ImageList_BeginDrag [COMCTL32.@]
583 * Creates a temporary image list that contains one image. It will be used
584 * as a drag image.
586 * PARAMS
587 * himlTrack [I] handle to the source image list
588 * iTrack [I] index of the drag image in the source image list
589 * dxHotspot [I] X position of the hot spot of the drag image
590 * dyHotspot [I] Y position of the hot spot of the drag image
592 * RETURNS
593 * Success: TRUE
594 * Failure: FALSE
597 BOOL WINAPI
598 ImageList_BeginDrag (HIMAGELIST himlTrack, INT iTrack,
599 INT dxHotspot, INT dyHotspot)
601 INT cx, cy;
602 POINT src, dst;
604 TRACE("(himlTrack=%p iTrack=%d dx=%d dy=%d)\n", himlTrack, iTrack,
605 dxHotspot, dyHotspot);
607 if (!is_valid(himlTrack))
608 return FALSE;
610 if (iTrack >= himlTrack->cCurImage)
611 return FALSE;
613 if (InternalDrag.himl)
614 return FALSE;
616 cx = himlTrack->cx;
617 cy = himlTrack->cy;
619 InternalDrag.himlNoCursor = InternalDrag.himl = ImageList_Create (cx, cy, himlTrack->flags, 1, 1);
620 if (InternalDrag.himl == NULL) {
621 WARN("Error creating drag image list!\n");
622 return FALSE;
625 InternalDrag.dxHotspot = dxHotspot;
626 InternalDrag.dyHotspot = dyHotspot;
628 /* copy image */
629 imagelist_point_from_index(InternalDrag.himl, 0, &dst);
630 imagelist_point_from_index(himlTrack, iTrack, &src);
631 BitBlt(InternalDrag.himl->hdcImage, dst.x, dst.y, cx, cy, himlTrack->hdcImage, src.x, src.y,
632 SRCCOPY);
633 BitBlt(InternalDrag.himl->hdcMask, dst.x, dst.y, cx, cy, himlTrack->hdcMask, src.x, src.y,
634 SRCCOPY);
636 InternalDrag.himl->cCurImage = 1;
638 return TRUE;
642 /*************************************************************************
643 * ImageList_Copy [COMCTL32.@]
645 * Copies an image of the source image list to an image of the
646 * destination image list. Images can be copied or swapped.
648 * PARAMS
649 * himlDst [I] handle to the destination image list
650 * iDst [I] destination image index.
651 * himlSrc [I] handle to the source image list
652 * iSrc [I] source image index
653 * uFlags [I] flags for the copy operation
655 * RETURNS
656 * Success: TRUE
657 * Failure: FALSE
659 * NOTES
660 * Copying from one image list to another is possible. The original
661 * implementation just copies or swaps within one image list.
662 * Could this feature become a bug??? ;-)
665 BOOL WINAPI
666 ImageList_Copy (HIMAGELIST himlDst, INT iDst, HIMAGELIST himlSrc,
667 INT iSrc, UINT uFlags)
669 POINT ptSrc, ptDst;
671 TRACE("himlDst=%p iDst=%d himlSrc=%p iSrc=%d\n", himlDst, iDst, himlSrc, iSrc);
673 if (!is_valid(himlSrc) || !is_valid(himlDst))
674 return FALSE;
675 if ((iDst < 0) || (iDst >= himlDst->cCurImage))
676 return FALSE;
677 if ((iSrc < 0) || (iSrc >= himlSrc->cCurImage))
678 return FALSE;
680 imagelist_point_from_index( himlDst, iDst, &ptDst );
681 imagelist_point_from_index( himlSrc, iSrc, &ptSrc );
683 if (uFlags & ILCF_SWAP) {
684 /* swap */
685 HDC hdcBmp;
686 HBITMAP hbmTempImage, hbmTempMask;
688 hdcBmp = CreateCompatibleDC (0);
690 /* create temporary bitmaps */
691 hbmTempImage = CreateBitmap (himlSrc->cx, himlSrc->cy, 1,
692 himlSrc->uBitsPixel, NULL);
693 hbmTempMask = CreateBitmap (himlSrc->cx, himlSrc->cy, 1,
694 1, NULL);
696 /* copy (and stretch) destination to temporary bitmaps.(save) */
697 /* image */
698 SelectObject (hdcBmp, hbmTempImage);
699 StretchBlt (hdcBmp, 0, 0, himlSrc->cx, himlSrc->cy,
700 himlDst->hdcImage, ptDst.x, ptDst.y, himlDst->cx, himlDst->cy,
701 SRCCOPY);
702 /* mask */
703 SelectObject (hdcBmp, hbmTempMask);
704 StretchBlt (hdcBmp, 0, 0, himlSrc->cx, himlSrc->cy,
705 himlDst->hdcMask, ptDst.x, ptDst.y, himlDst->cx, himlDst->cy,
706 SRCCOPY);
708 /* copy (and stretch) source to destination */
709 /* image */
710 StretchBlt (himlDst->hdcImage, ptDst.x, ptDst.y, himlDst->cx, himlDst->cy,
711 himlSrc->hdcImage, ptSrc.x, ptSrc.y, himlSrc->cx, himlSrc->cy,
712 SRCCOPY);
713 /* mask */
714 StretchBlt (himlDst->hdcMask, ptDst.x, ptDst.y, himlDst->cx, himlDst->cy,
715 himlSrc->hdcMask, ptSrc.x, ptSrc.y, himlSrc->cx, himlSrc->cy,
716 SRCCOPY);
718 /* copy (without stretching) temporary bitmaps to source (restore) */
719 /* mask */
720 BitBlt (himlSrc->hdcMask, ptSrc.x, ptSrc.y, himlSrc->cx, himlSrc->cy,
721 hdcBmp, 0, 0, SRCCOPY);
723 /* image */
724 BitBlt (himlSrc->hdcImage, ptSrc.x, ptSrc.y, himlSrc->cx, himlSrc->cy,
725 hdcBmp, 0, 0, SRCCOPY);
726 /* delete temporary bitmaps */
727 DeleteObject (hbmTempMask);
728 DeleteObject (hbmTempImage);
729 DeleteDC(hdcBmp);
731 else {
732 /* copy image */
733 StretchBlt (himlDst->hdcImage, ptDst.x, ptDst.y, himlDst->cx, himlDst->cy,
734 himlSrc->hdcImage, ptSrc.x, ptSrc.y, himlSrc->cx, himlSrc->cy,
735 SRCCOPY);
737 /* copy mask */
738 StretchBlt (himlDst->hdcMask, ptDst.x, ptDst.y, himlDst->cx, himlDst->cy,
739 himlSrc->hdcMask, ptSrc.x, ptSrc.y, himlSrc->cx, himlSrc->cy,
740 SRCCOPY);
743 return TRUE;
747 /*************************************************************************
748 * ImageList_Create [COMCTL32.@]
750 * Creates a new image list.
752 * PARAMS
753 * cx [I] image height
754 * cy [I] image width
755 * flags [I] creation flags
756 * cInitial [I] initial number of images in the image list
757 * cGrow [I] number of images by which image list grows
759 * RETURNS
760 * Success: Handle to the created image list
761 * Failure: NULL
763 HIMAGELIST WINAPI
764 ImageList_Create (INT cx, INT cy, UINT flags,
765 INT cInitial, INT cGrow)
767 HIMAGELIST himl;
768 INT nCount;
769 HBITMAP hbmTemp;
770 UINT ilc = (flags & 0xFE);
771 static const WORD aBitBlend25[] =
772 {0xAA, 0x00, 0x55, 0x00, 0xAA, 0x00, 0x55, 0x00};
774 static const WORD aBitBlend50[] =
775 {0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA};
777 TRACE("(%d %d 0x%x %d %d)\n", cx, cy, flags, cInitial, cGrow);
779 if (cx < 0 || cy < 0) return NULL;
780 if (!((flags&ILC_COLORDDB) == ILC_COLORDDB) && (cx == 0 || cy == 0)) return NULL;
782 /* Create the IImageList interface for the image list */
783 if (FAILED(ImageListImpl_CreateInstance(NULL, &IID_IImageList, (void **)&himl)))
784 return NULL;
786 cGrow = (WORD)((max( cGrow, 1 ) + 3) & ~3);
788 if (cGrow > 256)
790 /* Windows doesn't limit the size here, but X11 doesn't let us allocate such huge bitmaps */
791 WARN( "grow %d too large, limiting to 256\n", cGrow );
792 cGrow = 256;
795 himl->cx = cx;
796 himl->cy = cy;
797 himl->flags = flags;
798 himl->cMaxImage = cInitial + 1;
799 himl->cInitial = cInitial;
800 himl->cGrow = cGrow;
801 himl->clrFg = CLR_DEFAULT;
802 himl->clrBk = CLR_NONE;
804 /* initialize overlay mask indices */
805 for (nCount = 0; nCount < MAX_OVERLAYIMAGE; nCount++)
806 himl->nOvlIdx[nCount] = -1;
808 /* Create Image & Mask DCs */
809 himl->hdcImage = CreateCompatibleDC (0);
810 if (!himl->hdcImage)
811 goto cleanup;
812 if (himl->flags & ILC_MASK){
813 himl->hdcMask = CreateCompatibleDC(0);
814 if (!himl->hdcMask)
815 goto cleanup;
818 /* Default to ILC_COLOR4 if none of the ILC_COLOR* flags are specified */
819 if (ilc == ILC_COLOR)
821 ilc = ILC_COLOR4;
822 himl->flags |= ILC_COLOR4;
825 if (ilc >= ILC_COLOR4 && ilc <= ILC_COLOR32)
826 himl->uBitsPixel = ilc;
827 else
828 himl->uBitsPixel = (UINT)GetDeviceCaps (himl->hdcImage, BITSPIXEL);
830 if (himl->cMaxImage > 0) {
831 himl->hbmImage = ImageList_CreateImage(himl->hdcImage, himl, himl->cMaxImage);
832 SelectObject(himl->hdcImage, himl->hbmImage);
833 } else
834 himl->hbmImage = 0;
836 if ((himl->cMaxImage > 0) && (himl->flags & ILC_MASK)) {
837 SIZE sz;
839 imagelist_get_bitmap_size(himl, himl->cMaxImage, &sz);
840 himl->hbmMask = CreateBitmap (sz.cx, sz.cy, 1, 1, NULL);
841 if (himl->hbmMask == 0) {
842 ERR("Error creating mask bitmap!\n");
843 goto cleanup;
845 SelectObject(himl->hdcMask, himl->hbmMask);
847 else
848 himl->hbmMask = 0;
850 if (ilc == ILC_COLOR32)
851 himl->has_alpha = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, himl->cMaxImage );
852 else
853 himl->has_alpha = NULL;
855 /* create blending brushes */
856 hbmTemp = CreateBitmap (8, 8, 1, 1, aBitBlend25);
857 himl->hbrBlend25 = CreatePatternBrush (hbmTemp);
858 DeleteObject (hbmTemp);
860 hbmTemp = CreateBitmap (8, 8, 1, 1, aBitBlend50);
861 himl->hbrBlend50 = CreatePatternBrush (hbmTemp);
862 DeleteObject (hbmTemp);
864 TRACE("created imagelist %p\n", himl);
865 return himl;
867 cleanup:
868 ImageList_Destroy(himl);
869 return NULL;
873 /*************************************************************************
874 * ImageList_Destroy [COMCTL32.@]
876 * Destroys an image list.
878 * PARAMS
879 * himl [I] handle to image list
881 * RETURNS
882 * Success: TRUE
883 * Failure: FALSE
886 BOOL WINAPI
887 ImageList_Destroy (HIMAGELIST himl)
889 if (!is_valid(himl))
890 return FALSE;
892 IImageList_Release((IImageList *) himl);
893 return TRUE;
897 /*************************************************************************
898 * ImageList_DragEnter [COMCTL32.@]
900 * Locks window update and displays the drag image at the given position.
902 * PARAMS
903 * hwndLock [I] handle of the window that owns the drag image.
904 * x [I] X position of the drag image.
905 * y [I] Y position of the drag image.
907 * RETURNS
908 * Success: TRUE
909 * Failure: FALSE
911 * NOTES
912 * The position of the drag image is relative to the window, not
913 * the client area.
916 BOOL WINAPI
917 ImageList_DragEnter (HWND hwndLock, INT x, INT y)
919 TRACE("(hwnd=%p x=%d y=%d)\n", hwndLock, x, y);
921 if (!is_valid(InternalDrag.himl))
922 return FALSE;
924 if (hwndLock)
925 InternalDrag.hwnd = hwndLock;
926 else
927 InternalDrag.hwnd = GetDesktopWindow ();
929 InternalDrag.x = x;
930 InternalDrag.y = y;
932 /* draw the drag image and save the background */
933 if (!ImageList_DragShowNolock(TRUE)) {
934 return FALSE;
937 return TRUE;
941 /*************************************************************************
942 * ImageList_DragLeave [COMCTL32.@]
944 * Unlocks window update and hides the drag image.
946 * PARAMS
947 * hwndLock [I] handle of the window that owns the drag image.
949 * RETURNS
950 * Success: TRUE
951 * Failure: FALSE
954 BOOL WINAPI
955 ImageList_DragLeave (HWND hwndLock)
957 /* As we don't save drag info in the window this can lead to problems if
958 an app does not supply the same window as DragEnter */
959 /* if (hwndLock)
960 InternalDrag.hwnd = hwndLock;
961 else
962 InternalDrag.hwnd = GetDesktopWindow (); */
963 if(!hwndLock)
964 hwndLock = GetDesktopWindow();
965 if(InternalDrag.hwnd != hwndLock)
966 FIXME("DragLeave hWnd != DragEnter hWnd\n");
968 ImageList_DragShowNolock (FALSE);
970 return TRUE;
974 /*************************************************************************
975 * ImageList_InternalDragDraw [Internal]
977 * Draws the drag image.
979 * PARAMS
980 * hdc [I] device context to draw into.
981 * x [I] X position of the drag image.
982 * y [I] Y position of the drag image.
984 * RETURNS
985 * Success: TRUE
986 * Failure: FALSE
988 * NOTES
989 * The position of the drag image is relative to the window, not
990 * the client area.
994 static inline void
995 ImageList_InternalDragDraw (HDC hdc, INT x, INT y)
997 IMAGELISTDRAWPARAMS imldp;
999 ZeroMemory (&imldp, sizeof(imldp));
1000 imldp.cbSize = sizeof(imldp);
1001 imldp.himl = InternalDrag.himl;
1002 imldp.i = 0;
1003 imldp.hdcDst = hdc,
1004 imldp.x = x;
1005 imldp.y = y;
1006 imldp.rgbBk = CLR_DEFAULT;
1007 imldp.rgbFg = CLR_DEFAULT;
1008 imldp.fStyle = ILD_NORMAL;
1009 imldp.fState = ILS_ALPHA;
1010 imldp.Frame = 192;
1011 ImageList_DrawIndirect (&imldp);
1014 /*************************************************************************
1015 * ImageList_DragMove [COMCTL32.@]
1017 * Moves the drag image.
1019 * PARAMS
1020 * x [I] X position of the drag image.
1021 * y [I] Y position of the drag image.
1023 * RETURNS
1024 * Success: TRUE
1025 * Failure: FALSE
1027 * NOTES
1028 * The position of the drag image is relative to the window, not
1029 * the client area.
1032 BOOL WINAPI
1033 ImageList_DragMove (INT x, INT y)
1035 TRACE("(x=%d y=%d)\n", x, y);
1037 if (!is_valid(InternalDrag.himl))
1038 return FALSE;
1040 /* draw/update the drag image */
1041 if (InternalDrag.bShow) {
1042 HDC hdcDrag;
1043 HDC hdcOffScreen;
1044 HDC hdcBg;
1045 HBITMAP hbmOffScreen;
1046 INT origNewX, origNewY;
1047 INT origOldX, origOldY;
1048 INT origRegX, origRegY;
1049 INT sizeRegX, sizeRegY;
1052 /* calculate the update region */
1053 origNewX = x - InternalDrag.dxHotspot;
1054 origNewY = y - InternalDrag.dyHotspot;
1055 origOldX = InternalDrag.x - InternalDrag.dxHotspot;
1056 origOldY = InternalDrag.y - InternalDrag.dyHotspot;
1057 origRegX = min(origNewX, origOldX);
1058 origRegY = min(origNewY, origOldY);
1059 sizeRegX = InternalDrag.himl->cx + abs(x - InternalDrag.x);
1060 sizeRegY = InternalDrag.himl->cy + abs(y - InternalDrag.y);
1062 hdcDrag = GetDCEx(InternalDrag.hwnd, 0,
1063 DCX_WINDOW | DCX_CACHE | DCX_LOCKWINDOWUPDATE);
1064 hdcOffScreen = CreateCompatibleDC(hdcDrag);
1065 hdcBg = CreateCompatibleDC(hdcDrag);
1067 hbmOffScreen = CreateCompatibleBitmap(hdcDrag, sizeRegX, sizeRegY);
1068 SelectObject(hdcOffScreen, hbmOffScreen);
1069 SelectObject(hdcBg, InternalDrag.hbmBg);
1071 /* get the actual background of the update region */
1072 BitBlt(hdcOffScreen, 0, 0, sizeRegX, sizeRegY, hdcDrag,
1073 origRegX, origRegY, SRCCOPY);
1074 /* erase the old image */
1075 BitBlt(hdcOffScreen, origOldX - origRegX, origOldY - origRegY,
1076 InternalDrag.himl->cx, InternalDrag.himl->cy, hdcBg, 0, 0,
1077 SRCCOPY);
1078 /* save the background */
1079 BitBlt(hdcBg, 0, 0, InternalDrag.himl->cx, InternalDrag.himl->cy,
1080 hdcOffScreen, origNewX - origRegX, origNewY - origRegY, SRCCOPY);
1081 /* draw the image */
1082 ImageList_InternalDragDraw(hdcOffScreen, origNewX - origRegX,
1083 origNewY - origRegY);
1084 /* draw the update region to the screen */
1085 BitBlt(hdcDrag, origRegX, origRegY, sizeRegX, sizeRegY,
1086 hdcOffScreen, 0, 0, SRCCOPY);
1088 DeleteDC(hdcBg);
1089 DeleteDC(hdcOffScreen);
1090 DeleteObject(hbmOffScreen);
1091 ReleaseDC(InternalDrag.hwnd, hdcDrag);
1094 /* update the image position */
1095 InternalDrag.x = x;
1096 InternalDrag.y = y;
1098 return TRUE;
1102 /*************************************************************************
1103 * ImageList_DragShowNolock [COMCTL32.@]
1105 * Shows or hides the drag image.
1107 * PARAMS
1108 * bShow [I] TRUE shows the drag image, FALSE hides it.
1110 * RETURNS
1111 * Success: TRUE
1112 * Failure: FALSE
1115 BOOL WINAPI
1116 ImageList_DragShowNolock (BOOL bShow)
1118 HDC hdcDrag;
1119 HDC hdcBg;
1120 INT x, y;
1122 if (!is_valid(InternalDrag.himl))
1123 return FALSE;
1125 TRACE("bShow=0x%X!\n", bShow);
1127 /* DragImage is already visible/hidden */
1128 if ((InternalDrag.bShow && bShow) || (!InternalDrag.bShow && !bShow)) {
1129 return FALSE;
1132 /* position of the origin of the DragImage */
1133 x = InternalDrag.x - InternalDrag.dxHotspot;
1134 y = InternalDrag.y - InternalDrag.dyHotspot;
1136 hdcDrag = GetDCEx (InternalDrag.hwnd, 0,
1137 DCX_WINDOW | DCX_CACHE | DCX_LOCKWINDOWUPDATE);
1138 if (!hdcDrag) {
1139 return FALSE;
1142 hdcBg = CreateCompatibleDC(hdcDrag);
1143 if (!InternalDrag.hbmBg) {
1144 InternalDrag.hbmBg = CreateCompatibleBitmap(hdcDrag,
1145 InternalDrag.himl->cx, InternalDrag.himl->cy);
1147 SelectObject(hdcBg, InternalDrag.hbmBg);
1149 if (bShow) {
1150 /* save the background */
1151 BitBlt(hdcBg, 0, 0, InternalDrag.himl->cx, InternalDrag.himl->cy,
1152 hdcDrag, x, y, SRCCOPY);
1153 /* show the image */
1154 ImageList_InternalDragDraw(hdcDrag, x, y);
1155 } else {
1156 /* hide the image */
1157 BitBlt(hdcDrag, x, y, InternalDrag.himl->cx, InternalDrag.himl->cy,
1158 hdcBg, 0, 0, SRCCOPY);
1161 InternalDrag.bShow = !InternalDrag.bShow;
1163 DeleteDC(hdcBg);
1164 ReleaseDC (InternalDrag.hwnd, hdcDrag);
1165 return TRUE;
1169 /*************************************************************************
1170 * ImageList_Draw [COMCTL32.@]
1172 * Draws an image.
1174 * PARAMS
1175 * himl [I] handle to image list
1176 * i [I] image index
1177 * hdc [I] handle to device context
1178 * x [I] x position
1179 * y [I] y position
1180 * fStyle [I] drawing flags
1182 * RETURNS
1183 * Success: TRUE
1184 * Failure: FALSE
1186 * SEE
1187 * ImageList_DrawEx.
1190 BOOL WINAPI
1191 ImageList_Draw (HIMAGELIST himl, INT i, HDC hdc, INT x, INT y, UINT fStyle)
1193 return ImageList_DrawEx (himl, i, hdc, x, y, 0, 0,
1194 CLR_DEFAULT, CLR_DEFAULT, fStyle);
1198 /*************************************************************************
1199 * ImageList_DrawEx [COMCTL32.@]
1201 * Draws an image and allows using extended drawing features.
1203 * PARAMS
1204 * himl [I] handle to image list
1205 * i [I] image index
1206 * hdc [I] handle to device context
1207 * x [I] X position
1208 * y [I] Y position
1209 * dx [I] X offset
1210 * dy [I] Y offset
1211 * rgbBk [I] background color
1212 * rgbFg [I] foreground color
1213 * fStyle [I] drawing flags
1215 * RETURNS
1216 * Success: TRUE
1217 * Failure: FALSE
1219 * NOTES
1220 * Calls ImageList_DrawIndirect.
1222 * SEE
1223 * ImageList_DrawIndirect.
1226 BOOL WINAPI
1227 ImageList_DrawEx (HIMAGELIST himl, INT i, HDC hdc, INT x, INT y,
1228 INT dx, INT dy, COLORREF rgbBk, COLORREF rgbFg,
1229 UINT fStyle)
1231 IMAGELISTDRAWPARAMS imldp;
1233 ZeroMemory (&imldp, sizeof(imldp));
1234 imldp.cbSize = sizeof(imldp);
1235 imldp.himl = himl;
1236 imldp.i = i;
1237 imldp.hdcDst = hdc,
1238 imldp.x = x;
1239 imldp.y = y;
1240 imldp.cx = dx;
1241 imldp.cy = dy;
1242 imldp.rgbBk = rgbBk;
1243 imldp.rgbFg = rgbFg;
1244 imldp.fStyle = fStyle;
1246 return ImageList_DrawIndirect (&imldp);
1250 static BOOL alpha_blend_image( HIMAGELIST himl, HDC dest_dc, int dest_x, int dest_y,
1251 int src_x, int src_y, int cx, int cy, BLENDFUNCTION func,
1252 UINT style, COLORREF blend_col )
1254 BOOL ret = FALSE;
1255 HDC hdc;
1256 HBITMAP bmp = 0, mask = 0;
1257 BITMAPINFO *info;
1258 void *bits, *mask_bits;
1259 unsigned int *ptr;
1260 int i, j;
1262 if (!(hdc = CreateCompatibleDC( 0 ))) return FALSE;
1263 if (!(info = HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET( BITMAPINFO, bmiColors[256] )))) goto done;
1264 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1265 info->bmiHeader.biWidth = cx;
1266 info->bmiHeader.biHeight = cy;
1267 info->bmiHeader.biPlanes = 1;
1268 info->bmiHeader.biBitCount = 32;
1269 info->bmiHeader.biCompression = BI_RGB;
1270 info->bmiHeader.biSizeImage = cx * cy * 4;
1271 info->bmiHeader.biXPelsPerMeter = 0;
1272 info->bmiHeader.biYPelsPerMeter = 0;
1273 info->bmiHeader.biClrUsed = 0;
1274 info->bmiHeader.biClrImportant = 0;
1275 if (!(bmp = CreateDIBSection( himl->hdcImage, info, DIB_RGB_COLORS, &bits, 0, 0 ))) goto done;
1276 SelectObject( hdc, bmp );
1277 BitBlt( hdc, 0, 0, cx, cy, himl->hdcImage, src_x, src_y, SRCCOPY );
1279 if (blend_col != CLR_NONE)
1281 BYTE r = GetRValue( blend_col );
1282 BYTE g = GetGValue( blend_col );
1283 BYTE b = GetBValue( blend_col );
1285 if (style & ILD_BLEND25)
1287 for (i = 0, ptr = bits; i < cx * cy; i++, ptr++)
1288 *ptr = ((*ptr & 0xff000000) |
1289 ((((*ptr & 0x00ff0000) * 3 + (r << 16)) / 4) & 0x00ff0000) |
1290 ((((*ptr & 0x0000ff00) * 3 + (g << 8)) / 4) & 0x0000ff00) |
1291 ((((*ptr & 0x000000ff) * 3 + (b << 0)) / 4) & 0x000000ff));
1293 else if (style & ILD_BLEND50)
1295 for (i = 0, ptr = bits; i < cx * cy; i++, ptr++)
1296 *ptr = ((*ptr & 0xff000000) |
1297 ((((*ptr & 0x00ff0000) + (r << 16)) / 2) & 0x00ff0000) |
1298 ((((*ptr & 0x0000ff00) + (g << 8)) / 2) & 0x0000ff00) |
1299 ((((*ptr & 0x000000ff) + (b << 0)) / 2) & 0x000000ff));
1303 if (himl->has_alpha) /* we already have an alpha channel in this case */
1305 /* pre-multiply by the alpha channel */
1306 for (i = 0, ptr = bits; i < cx * cy; i++, ptr++)
1308 DWORD alpha = *ptr >> 24;
1309 *ptr = ((*ptr & 0xff000000) |
1310 (((*ptr & 0x00ff0000) * alpha / 255) & 0x00ff0000) |
1311 (((*ptr & 0x0000ff00) * alpha / 255) & 0x0000ff00) |
1312 (((*ptr & 0x000000ff) * alpha / 255)));
1315 else if (himl->hbmMask)
1317 unsigned int width_bytes = (cx + 31) / 32 * 4;
1318 /* generate alpha channel from the mask */
1319 info->bmiHeader.biBitCount = 1;
1320 info->bmiHeader.biSizeImage = width_bytes * cy;
1321 info->bmiColors[0].rgbRed = 0;
1322 info->bmiColors[0].rgbGreen = 0;
1323 info->bmiColors[0].rgbBlue = 0;
1324 info->bmiColors[0].rgbReserved = 0;
1325 info->bmiColors[1].rgbRed = 0xff;
1326 info->bmiColors[1].rgbGreen = 0xff;
1327 info->bmiColors[1].rgbBlue = 0xff;
1328 info->bmiColors[1].rgbReserved = 0;
1329 if (!(mask = CreateDIBSection( himl->hdcMask, info, DIB_RGB_COLORS, &mask_bits, 0, 0 )))
1330 goto done;
1331 SelectObject( hdc, mask );
1332 BitBlt( hdc, 0, 0, cx, cy, himl->hdcMask, src_x, src_y, SRCCOPY );
1333 SelectObject( hdc, bmp );
1334 for (i = 0, ptr = bits; i < cy; i++)
1335 for (j = 0; j < cx; j++, ptr++)
1336 if ((((BYTE *)mask_bits)[i * width_bytes + j / 8] << (j % 8)) & 0x80) *ptr = 0;
1337 else *ptr |= 0xff000000;
1340 ret = GdiAlphaBlend( dest_dc, dest_x, dest_y, cx, cy, hdc, 0, 0, cx, cy, func );
1342 done:
1343 DeleteDC( hdc );
1344 if (bmp) DeleteObject( bmp );
1345 if (mask) DeleteObject( mask );
1346 HeapFree( GetProcessHeap(), 0, info );
1347 return ret;
1350 /*************************************************************************
1351 * ImageList_DrawIndirect [COMCTL32.@]
1353 * Draws an image using various parameters specified in pimldp.
1355 * PARAMS
1356 * pimldp [I] pointer to IMAGELISTDRAWPARAMS structure.
1358 * RETURNS
1359 * Success: TRUE
1360 * Failure: FALSE
1363 BOOL WINAPI
1364 ImageList_DrawIndirect (IMAGELISTDRAWPARAMS *pimldp)
1366 INT cx, cy, nOvlIdx;
1367 DWORD fState, dwRop;
1368 UINT fStyle;
1369 COLORREF oldImageBk, oldImageFg;
1370 HDC hImageDC, hImageListDC, hMaskListDC;
1371 HBITMAP hImageBmp, hOldImageBmp, hBlendMaskBmp;
1372 BOOL bIsTransparent, bBlend, bResult = FALSE, bMask;
1373 HIMAGELIST himl;
1374 HBRUSH hOldBrush;
1375 POINT pt;
1376 BOOL has_alpha;
1378 if (!pimldp || !(himl = pimldp->himl)) return FALSE;
1379 if (!is_valid(himl)) return FALSE;
1380 if ((pimldp->i < 0) || (pimldp->i >= himl->cCurImage)) return FALSE;
1382 imagelist_point_from_index( himl, pimldp->i, &pt );
1383 pt.x += pimldp->xBitmap;
1384 pt.y += pimldp->yBitmap;
1386 fState = pimldp->cbSize < sizeof(IMAGELISTDRAWPARAMS) ? ILS_NORMAL : pimldp->fState;
1387 fStyle = pimldp->fStyle & ~ILD_OVERLAYMASK;
1388 cx = (pimldp->cx == 0) ? himl->cx : pimldp->cx;
1389 cy = (pimldp->cy == 0) ? himl->cy : pimldp->cy;
1391 bIsTransparent = (fStyle & ILD_TRANSPARENT);
1392 if( pimldp->rgbBk == CLR_NONE )
1393 bIsTransparent = TRUE;
1394 if( ( pimldp->rgbBk == CLR_DEFAULT ) && ( himl->clrBk == CLR_NONE ) )
1395 bIsTransparent = TRUE;
1396 bMask = (himl->flags & ILC_MASK) && (fStyle & ILD_MASK) ;
1397 bBlend = (fStyle & (ILD_BLEND25 | ILD_BLEND50) ) && !bMask;
1399 TRACE("himl(%p) hbmMask(%p) iImage(%d) x(%d) y(%d) cx(%d) cy(%d)\n",
1400 himl, himl->hbmMask, pimldp->i, pimldp->x, pimldp->y, cx, cy);
1402 /* we will use these DCs to access the images and masks in the ImageList */
1403 hImageListDC = himl->hdcImage;
1404 hMaskListDC = himl->hdcMask;
1406 /* these will accumulate the image and mask for the image we're drawing */
1407 hImageDC = CreateCompatibleDC( pimldp->hdcDst );
1408 hImageBmp = CreateCompatibleBitmap( pimldp->hdcDst, cx, cy );
1409 hBlendMaskBmp = bBlend ? CreateBitmap(cx, cy, 1, 1, NULL) : 0;
1411 /* Create a compatible DC. */
1412 if (!hImageListDC || !hImageDC || !hImageBmp ||
1413 (bBlend && !hBlendMaskBmp) || (himl->hbmMask && !hMaskListDC))
1414 goto cleanup;
1416 hOldImageBmp = SelectObject(hImageDC, hImageBmp);
1419 * To obtain a transparent look, background color should be set
1420 * to white and foreground color to black when blitting the
1421 * monochrome mask.
1423 oldImageFg = SetTextColor( hImageDC, RGB( 0, 0, 0 ) );
1424 oldImageBk = SetBkColor( hImageDC, RGB( 0xff, 0xff, 0xff ) );
1426 has_alpha = (himl->has_alpha && himl->has_alpha[pimldp->i]);
1427 if (!bMask && (has_alpha || (fState & ILS_ALPHA)))
1429 COLORREF colour, blend_col = CLR_NONE;
1430 BLENDFUNCTION func;
1432 if (bBlend)
1434 blend_col = pimldp->rgbFg;
1435 if (blend_col == CLR_DEFAULT) blend_col = GetSysColor( COLOR_HIGHLIGHT );
1436 else if (blend_col == CLR_NONE) blend_col = GetTextColor( pimldp->hdcDst );
1439 func.BlendOp = AC_SRC_OVER;
1440 func.BlendFlags = 0;
1441 func.SourceConstantAlpha = (fState & ILS_ALPHA) ? pimldp->Frame : 255;
1442 func.AlphaFormat = AC_SRC_ALPHA;
1444 if (bIsTransparent)
1446 bResult = alpha_blend_image( himl, pimldp->hdcDst, pimldp->x, pimldp->y,
1447 pt.x, pt.y, cx, cy, func, fStyle, blend_col );
1448 goto end;
1450 colour = pimldp->rgbBk;
1451 if (colour == CLR_DEFAULT) colour = himl->clrBk;
1452 if (colour == CLR_NONE) colour = GetBkColor( pimldp->hdcDst );
1454 hOldBrush = SelectObject (hImageDC, CreateSolidBrush (colour));
1455 PatBlt( hImageDC, 0, 0, cx, cy, PATCOPY );
1456 alpha_blend_image( himl, hImageDC, 0, 0, pt.x, pt.y, cx, cy, func, fStyle, blend_col );
1457 DeleteObject (SelectObject (hImageDC, hOldBrush));
1458 bResult = BitBlt( pimldp->hdcDst, pimldp->x, pimldp->y, cx, cy, hImageDC, 0, 0, SRCCOPY );
1459 goto end;
1463 * Draw the initial image
1465 if( bMask ) {
1466 if (himl->hbmMask) {
1467 hOldBrush = SelectObject (hImageDC, CreateSolidBrush (GetTextColor(pimldp->hdcDst)));
1468 PatBlt( hImageDC, 0, 0, cx, cy, PATCOPY );
1469 BitBlt(hImageDC, 0, 0, cx, cy, hMaskListDC, pt.x, pt.y, SRCPAINT);
1470 DeleteObject (SelectObject (hImageDC, hOldBrush));
1471 if( bIsTransparent )
1473 BitBlt ( pimldp->hdcDst, pimldp->x, pimldp->y, cx, cy, hImageDC, 0, 0, SRCAND);
1474 bResult = TRUE;
1475 goto end;
1477 } else {
1478 hOldBrush = SelectObject (hImageDC, GetStockObject(BLACK_BRUSH));
1479 PatBlt( hImageDC, 0, 0, cx, cy, PATCOPY);
1480 SelectObject(hImageDC, hOldBrush);
1482 } else {
1483 /* blend the image with the needed solid background */
1484 COLORREF colour = RGB(0,0,0);
1486 if( !bIsTransparent )
1488 colour = pimldp->rgbBk;
1489 if( colour == CLR_DEFAULT )
1490 colour = himl->clrBk;
1491 if( colour == CLR_NONE )
1492 colour = GetBkColor(pimldp->hdcDst);
1495 hOldBrush = SelectObject (hImageDC, CreateSolidBrush (colour));
1496 PatBlt( hImageDC, 0, 0, cx, cy, PATCOPY );
1497 if (himl->hbmMask)
1499 BitBlt( hImageDC, 0, 0, cx, cy, hMaskListDC, pt.x, pt.y, SRCAND );
1500 BitBlt( hImageDC, 0, 0, cx, cy, hImageListDC, pt.x, pt.y, SRCPAINT );
1502 else
1503 BitBlt( hImageDC, 0, 0, cx, cy, hImageListDC, pt.x, pt.y, SRCCOPY);
1504 DeleteObject (SelectObject (hImageDC, hOldBrush));
1507 /* Time for blending, if required */
1508 if (bBlend) {
1509 HBRUSH hBlendBrush;
1510 COLORREF clrBlend = pimldp->rgbFg;
1511 HDC hBlendMaskDC = hImageListDC;
1512 HBITMAP hOldBitmap;
1514 /* Create the blend Mask */
1515 hOldBitmap = SelectObject(hBlendMaskDC, hBlendMaskBmp);
1516 hBlendBrush = fStyle & ILD_BLEND50 ? himl->hbrBlend50 : himl->hbrBlend25;
1517 hOldBrush = SelectObject(hBlendMaskDC, hBlendBrush);
1518 PatBlt(hBlendMaskDC, 0, 0, cx, cy, PATCOPY);
1519 SelectObject(hBlendMaskDC, hOldBrush);
1521 /* Modify the blend mask if an Image Mask exist */
1522 if(himl->hbmMask) {
1523 BitBlt(hBlendMaskDC, 0, 0, cx, cy, hMaskListDC, pt.x, pt.y, 0x220326); /* NOTSRCAND */
1524 BitBlt(hBlendMaskDC, 0, 0, cx, cy, hBlendMaskDC, 0, 0, NOTSRCCOPY);
1527 /* now apply blend to the current image given the BlendMask */
1528 if (clrBlend == CLR_DEFAULT) clrBlend = GetSysColor (COLOR_HIGHLIGHT);
1529 else if (clrBlend == CLR_NONE) clrBlend = GetTextColor (pimldp->hdcDst);
1530 hOldBrush = SelectObject (hImageDC, CreateSolidBrush(clrBlend));
1531 BitBlt (hImageDC, 0, 0, cx, cy, hBlendMaskDC, 0, 0, 0xB8074A); /* PSDPxax */
1532 DeleteObject(SelectObject(hImageDC, hOldBrush));
1533 SelectObject(hBlendMaskDC, hOldBitmap);
1536 /* Now do the overlay image, if any */
1537 nOvlIdx = (pimldp->fStyle & ILD_OVERLAYMASK) >> 8;
1538 if ( (nOvlIdx >= 1) && (nOvlIdx <= MAX_OVERLAYIMAGE)) {
1539 nOvlIdx = himl->nOvlIdx[nOvlIdx - 1];
1540 if ((nOvlIdx >= 0) && (nOvlIdx < himl->cCurImage)) {
1541 POINT ptOvl;
1542 imagelist_point_from_index( himl, nOvlIdx, &ptOvl );
1543 ptOvl.x += pimldp->xBitmap;
1544 if (himl->hbmMask && !(fStyle & ILD_IMAGE))
1545 BitBlt (hImageDC, 0, 0, cx, cy, hMaskListDC, ptOvl.x, ptOvl.y, SRCAND);
1546 BitBlt (hImageDC, 0, 0, cx, cy, hImageListDC, ptOvl.x, ptOvl.y, SRCPAINT);
1550 if (fState & ILS_SATURATE) FIXME("ILS_SATURATE: unimplemented!\n");
1551 if (fState & ILS_GLOW) FIXME("ILS_GLOW: unimplemented!\n");
1552 if (fState & ILS_SHADOW) FIXME("ILS_SHADOW: unimplemented!\n");
1554 if (fStyle & ILD_PRESERVEALPHA) FIXME("ILD_PRESERVEALPHA: unimplemented!\n");
1555 if (fStyle & ILD_SCALE) FIXME("ILD_SCALE: unimplemented!\n");
1556 if (fStyle & ILD_DPISCALE) FIXME("ILD_DPISCALE: unimplemented!\n");
1558 /* now copy the image to the screen */
1559 dwRop = SRCCOPY;
1560 if (himl->hbmMask && bIsTransparent ) {
1561 COLORREF oldDstFg = SetTextColor(pimldp->hdcDst, RGB( 0, 0, 0 ) );
1562 COLORREF oldDstBk = SetBkColor(pimldp->hdcDst, RGB( 0xff, 0xff, 0xff ));
1563 BitBlt (pimldp->hdcDst, pimldp->x, pimldp->y, cx, cy, hMaskListDC, pt.x, pt.y, SRCAND);
1564 SetBkColor(pimldp->hdcDst, oldDstBk);
1565 SetTextColor(pimldp->hdcDst, oldDstFg);
1566 dwRop = SRCPAINT;
1568 if (fStyle & ILD_ROP) dwRop = pimldp->dwRop;
1569 BitBlt (pimldp->hdcDst, pimldp->x, pimldp->y, cx, cy, hImageDC, 0, 0, dwRop);
1571 bResult = TRUE;
1572 end:
1573 /* cleanup the mess */
1574 SetBkColor(hImageDC, oldImageBk);
1575 SetTextColor(hImageDC, oldImageFg);
1576 SelectObject(hImageDC, hOldImageBmp);
1577 cleanup:
1578 DeleteObject(hBlendMaskBmp);
1579 DeleteObject(hImageBmp);
1580 DeleteDC(hImageDC);
1582 return bResult;
1586 /*************************************************************************
1587 * ImageList_Duplicate [COMCTL32.@]
1589 * Duplicates an image list.
1591 * PARAMS
1592 * himlSrc [I] source image list handle
1594 * RETURNS
1595 * Success: Handle of duplicated image list.
1596 * Failure: NULL
1599 HIMAGELIST WINAPI
1600 ImageList_Duplicate (HIMAGELIST himlSrc)
1602 HIMAGELIST himlDst;
1604 if (!is_valid(himlSrc)) {
1605 ERR("Invalid image list handle!\n");
1606 return NULL;
1609 himlDst = ImageList_Create (himlSrc->cx, himlSrc->cy, himlSrc->flags,
1610 himlSrc->cCurImage, himlSrc->cGrow);
1612 if (himlDst)
1614 SIZE sz;
1616 imagelist_get_bitmap_size(himlSrc, himlSrc->cCurImage, &sz);
1617 BitBlt (himlDst->hdcImage, 0, 0, sz.cx, sz.cy,
1618 himlSrc->hdcImage, 0, 0, SRCCOPY);
1620 if (himlDst->hbmMask)
1621 BitBlt (himlDst->hdcMask, 0, 0, sz.cx, sz.cy,
1622 himlSrc->hdcMask, 0, 0, SRCCOPY);
1624 himlDst->cCurImage = himlSrc->cCurImage;
1625 if (himlSrc->has_alpha && himlDst->has_alpha)
1626 memcpy( himlDst->has_alpha, himlSrc->has_alpha, himlDst->cCurImage );
1628 return himlDst;
1632 /*************************************************************************
1633 * ImageList_EndDrag [COMCTL32.@]
1635 * Finishes a drag operation.
1637 * PARAMS
1638 * no Parameters
1640 * RETURNS
1641 * Success: TRUE
1642 * Failure: FALSE
1645 VOID WINAPI
1646 ImageList_EndDrag (void)
1648 /* cleanup the InternalDrag struct */
1649 InternalDrag.hwnd = 0;
1650 if (InternalDrag.himl != InternalDrag.himlNoCursor)
1651 ImageList_Destroy (InternalDrag.himlNoCursor);
1652 ImageList_Destroy (InternalDrag.himl);
1653 InternalDrag.himlNoCursor = InternalDrag.himl = 0;
1654 InternalDrag.x= 0;
1655 InternalDrag.y= 0;
1656 InternalDrag.dxHotspot = 0;
1657 InternalDrag.dyHotspot = 0;
1658 InternalDrag.bShow = FALSE;
1659 DeleteObject(InternalDrag.hbmBg);
1660 InternalDrag.hbmBg = 0;
1664 /*************************************************************************
1665 * ImageList_GetBkColor [COMCTL32.@]
1667 * Returns the background color of an image list.
1669 * PARAMS
1670 * himl [I] Image list handle.
1672 * RETURNS
1673 * Success: background color
1674 * Failure: CLR_NONE
1677 COLORREF WINAPI
1678 ImageList_GetBkColor (HIMAGELIST himl)
1680 return himl ? himl->clrBk : CLR_NONE;
1684 /*************************************************************************
1685 * ImageList_GetDragImage [COMCTL32.@]
1687 * Returns the handle to the internal drag image list.
1689 * PARAMS
1690 * ppt [O] Pointer to the drag position. Can be NULL.
1691 * pptHotspot [O] Pointer to the position of the hot spot. Can be NULL.
1693 * RETURNS
1694 * Success: Handle of the drag image list.
1695 * Failure: NULL.
1698 HIMAGELIST WINAPI
1699 ImageList_GetDragImage (POINT *ppt, POINT *pptHotspot)
1701 if (is_valid(InternalDrag.himl)) {
1702 if (ppt) {
1703 ppt->x = InternalDrag.x;
1704 ppt->y = InternalDrag.y;
1706 if (pptHotspot) {
1707 pptHotspot->x = InternalDrag.dxHotspot;
1708 pptHotspot->y = InternalDrag.dyHotspot;
1710 return (InternalDrag.himl);
1713 return NULL;
1717 /*************************************************************************
1718 * ImageList_GetFlags [COMCTL32.@]
1720 * Gets the flags of the specified image list.
1722 * PARAMS
1723 * himl [I] Handle to image list
1725 * RETURNS
1726 * Image list flags.
1728 * BUGS
1729 * Stub.
1732 DWORD WINAPI
1733 ImageList_GetFlags(HIMAGELIST himl)
1735 TRACE("%p\n", himl);
1737 return is_valid(himl) ? himl->flags : 0;
1741 /*************************************************************************
1742 * ImageList_GetIcon [COMCTL32.@]
1744 * Creates an icon from a masked image of an image list.
1746 * PARAMS
1747 * himl [I] handle to image list
1748 * i [I] image index
1749 * flags [I] drawing style flags
1751 * RETURNS
1752 * Success: icon handle
1753 * Failure: NULL
1756 HICON WINAPI
1757 ImageList_GetIcon (HIMAGELIST himl, INT i, UINT fStyle)
1759 ICONINFO ii;
1760 HICON hIcon;
1761 HBITMAP hOldDstBitmap;
1762 HDC hdcDst;
1763 POINT pt;
1765 TRACE("%p %d %d\n", himl, i, fStyle);
1766 if (!is_valid(himl) || (i < 0) || (i >= himl->cCurImage)) return NULL;
1768 ii.fIcon = TRUE;
1769 ii.xHotspot = 0;
1770 ii.yHotspot = 0;
1772 /* create colour bitmap */
1773 hdcDst = GetDC(0);
1774 ii.hbmColor = CreateCompatibleBitmap(hdcDst, himl->cx, himl->cy);
1775 ReleaseDC(0, hdcDst);
1777 hdcDst = CreateCompatibleDC(0);
1779 imagelist_point_from_index( himl, i, &pt );
1781 /* draw mask*/
1782 ii.hbmMask = CreateBitmap (himl->cx, himl->cy, 1, 1, NULL);
1783 hOldDstBitmap = SelectObject (hdcDst, ii.hbmMask);
1784 if (himl->hbmMask) {
1785 BitBlt (hdcDst, 0, 0, himl->cx, himl->cy,
1786 himl->hdcMask, pt.x, pt.y, SRCCOPY);
1788 else
1789 PatBlt (hdcDst, 0, 0, himl->cx, himl->cy, BLACKNESS);
1791 /* draw image*/
1792 SelectObject (hdcDst, ii.hbmColor);
1793 BitBlt (hdcDst, 0, 0, himl->cx, himl->cy,
1794 himl->hdcImage, pt.x, pt.y, SRCCOPY);
1797 * CreateIconIndirect requires us to deselect the bitmaps from
1798 * the DCs before calling
1800 SelectObject(hdcDst, hOldDstBitmap);
1802 hIcon = CreateIconIndirect (&ii);
1804 DeleteObject (ii.hbmMask);
1805 DeleteObject (ii.hbmColor);
1806 DeleteDC (hdcDst);
1808 return hIcon;
1812 /*************************************************************************
1813 * ImageList_GetIconSize [COMCTL32.@]
1815 * Retrieves the size of an image in an image list.
1817 * PARAMS
1818 * himl [I] handle to image list
1819 * cx [O] pointer to the image width.
1820 * cy [O] pointer to the image height.
1822 * RETURNS
1823 * Success: TRUE
1824 * Failure: FALSE
1826 * NOTES
1827 * All images in an image list have the same size.
1830 BOOL WINAPI
1831 ImageList_GetIconSize (HIMAGELIST himl, INT *cx, INT *cy)
1833 if (!is_valid(himl) || !cx || !cy)
1834 return FALSE;
1836 *cx = himl->cx;
1837 *cy = himl->cy;
1839 return TRUE;
1843 /*************************************************************************
1844 * ImageList_GetImageCount [COMCTL32.@]
1846 * Returns the number of images in an image list.
1848 * PARAMS
1849 * himl [I] handle to image list
1851 * RETURNS
1852 * Success: Number of images.
1853 * Failure: 0
1856 INT WINAPI
1857 ImageList_GetImageCount (HIMAGELIST himl)
1859 if (!is_valid(himl))
1860 return 0;
1862 return himl->cCurImage;
1866 /*************************************************************************
1867 * ImageList_GetImageInfo [COMCTL32.@]
1869 * Returns information about an image in an image list.
1871 * PARAMS
1872 * himl [I] handle to image list
1873 * i [I] image index
1874 * pImageInfo [O] pointer to the image information
1876 * RETURNS
1877 * Success: TRUE
1878 * Failure: FALSE
1881 BOOL WINAPI
1882 ImageList_GetImageInfo (HIMAGELIST himl, INT i, IMAGEINFO *pImageInfo)
1884 POINT pt;
1886 if (!is_valid(himl) || (pImageInfo == NULL))
1887 return FALSE;
1888 if ((i < 0) || (i >= himl->cCurImage))
1889 return FALSE;
1891 pImageInfo->hbmImage = himl->hbmImage;
1892 pImageInfo->hbmMask = himl->hbmMask;
1894 imagelist_point_from_index( himl, i, &pt );
1895 pImageInfo->rcImage.top = pt.y;
1896 pImageInfo->rcImage.bottom = pt.y + himl->cy;
1897 pImageInfo->rcImage.left = pt.x;
1898 pImageInfo->rcImage.right = pt.x + himl->cx;
1900 return TRUE;
1904 /*************************************************************************
1905 * ImageList_GetImageRect [COMCTL32.@]
1907 * Retrieves the rectangle of the specified image in an image list.
1909 * PARAMS
1910 * himl [I] handle to image list
1911 * i [I] image index
1912 * lpRect [O] pointer to the image rectangle
1914 * RETURNS
1915 * Success: TRUE
1916 * Failure: FALSE
1918 * NOTES
1919 * This is an UNDOCUMENTED function!!!
1922 BOOL WINAPI
1923 ImageList_GetImageRect (HIMAGELIST himl, INT i, LPRECT lpRect)
1925 POINT pt;
1927 if (!is_valid(himl) || (lpRect == NULL))
1928 return FALSE;
1929 if ((i < 0) || (i >= himl->cCurImage))
1930 return FALSE;
1932 imagelist_point_from_index( himl, i, &pt );
1933 lpRect->left = pt.x;
1934 lpRect->top = pt.y;
1935 lpRect->right = pt.x + himl->cx;
1936 lpRect->bottom = pt.y + himl->cy;
1938 return TRUE;
1942 /*************************************************************************
1943 * ImageList_LoadImage [COMCTL32.@]
1944 * ImageList_LoadImageA [COMCTL32.@]
1946 * Creates an image list from a bitmap, icon or cursor.
1948 * See ImageList_LoadImageW.
1951 HIMAGELIST WINAPI
1952 ImageList_LoadImageA (HINSTANCE hi, LPCSTR lpbmp, INT cx, INT cGrow,
1953 COLORREF clrMask, UINT uType, UINT uFlags)
1955 HIMAGELIST himl;
1956 LPWSTR lpbmpW;
1957 DWORD len;
1959 if (IS_INTRESOURCE(lpbmp))
1960 return ImageList_LoadImageW(hi, (LPCWSTR)lpbmp, cx, cGrow, clrMask,
1961 uType, uFlags);
1963 len = MultiByteToWideChar(CP_ACP, 0, lpbmp, -1, NULL, 0);
1964 lpbmpW = Alloc(len * sizeof(WCHAR));
1965 MultiByteToWideChar(CP_ACP, 0, lpbmp, -1, lpbmpW, len);
1967 himl = ImageList_LoadImageW(hi, lpbmpW, cx, cGrow, clrMask, uType, uFlags);
1968 Free (lpbmpW);
1969 return himl;
1973 /*************************************************************************
1974 * ImageList_LoadImageW [COMCTL32.@]
1976 * Creates an image list from a bitmap, icon or cursor.
1978 * PARAMS
1979 * hi [I] instance handle
1980 * lpbmp [I] name or id of the image
1981 * cx [I] width of each image
1982 * cGrow [I] number of images to expand
1983 * clrMask [I] mask color
1984 * uType [I] type of image to load
1985 * uFlags [I] loading flags
1987 * RETURNS
1988 * Success: handle to the loaded image list
1989 * Failure: NULL
1991 * SEE
1992 * LoadImage ()
1995 HIMAGELIST WINAPI
1996 ImageList_LoadImageW (HINSTANCE hi, LPCWSTR lpbmp, INT cx, INT cGrow,
1997 COLORREF clrMask, UINT uType, UINT uFlags)
1999 HIMAGELIST himl = NULL;
2000 HANDLE handle;
2001 INT nImageCount;
2003 handle = LoadImageW (hi, lpbmp, uType, 0, 0, uFlags);
2004 if (!handle) {
2005 WARN("Couldn't load image\n");
2006 return NULL;
2009 if (uType == IMAGE_BITMAP) {
2010 DIBSECTION dib;
2011 UINT color;
2013 if (GetObjectW (handle, sizeof(dib), &dib) == sizeof(BITMAP)) color = ILC_COLOR;
2014 else color = dib.dsBm.bmBitsPixel;
2016 /* To match windows behavior, if cx is set to zero and
2017 the flag DI_DEFAULTSIZE is specified, cx becomes the
2018 system metric value for icons. If the flag is not specified
2019 the function sets the size to the height of the bitmap */
2020 if (cx == 0)
2022 if (uFlags & DI_DEFAULTSIZE)
2023 cx = GetSystemMetrics (SM_CXICON);
2024 else
2025 cx = dib.dsBm.bmHeight;
2028 nImageCount = dib.dsBm.bmWidth / cx;
2030 himl = ImageList_Create (cx, dib.dsBm.bmHeight, ILC_MASK | color, nImageCount, cGrow);
2031 if (!himl) {
2032 DeleteObject (handle);
2033 return NULL;
2035 ImageList_AddMasked (himl, handle, clrMask);
2037 else if ((uType == IMAGE_ICON) || (uType == IMAGE_CURSOR)) {
2038 ICONINFO ii;
2039 BITMAP bmp;
2041 GetIconInfo (handle, &ii);
2042 GetObjectW (ii.hbmColor, sizeof(BITMAP), &bmp);
2043 himl = ImageList_Create (bmp.bmWidth, bmp.bmHeight,
2044 ILC_MASK | ILC_COLOR, 1, cGrow);
2045 if (!himl) {
2046 DeleteObject (ii.hbmColor);
2047 DeleteObject (ii.hbmMask);
2048 DeleteObject (handle);
2049 return NULL;
2051 ImageList_Add (himl, ii.hbmColor, ii.hbmMask);
2052 DeleteObject (ii.hbmColor);
2053 DeleteObject (ii.hbmMask);
2056 DeleteObject (handle);
2058 return himl;
2062 /*************************************************************************
2063 * ImageList_Merge [COMCTL32.@]
2065 * Create an image list containing a merged image from two image lists.
2067 * PARAMS
2068 * himl1 [I] handle to first image list
2069 * i1 [I] first image index
2070 * himl2 [I] handle to second image list
2071 * i2 [I] second image index
2072 * dx [I] X offset of the second image relative to the first.
2073 * dy [I] Y offset of the second image relative to the first.
2075 * RETURNS
2076 * Success: The newly created image list. It contains a single image
2077 * consisting of the second image merged with the first.
2078 * Failure: NULL, if either himl1 or himl2 is invalid.
2080 * NOTES
2081 * - The returned image list should be deleted by the caller using
2082 * ImageList_Destroy() when it is no longer required.
2083 * - If either i1 or i2 is not a valid image index, they will be treated
2084 * as blank images.
2086 HIMAGELIST WINAPI
2087 ImageList_Merge (HIMAGELIST himl1, INT i1, HIMAGELIST himl2, INT i2,
2088 INT dx, INT dy)
2090 HIMAGELIST himlDst = NULL;
2091 INT cxDst, cyDst;
2092 INT xOff1, yOff1, xOff2, yOff2;
2093 POINT pt1, pt2;
2094 INT newFlags;
2096 TRACE("(himl1=%p i1=%d himl2=%p i2=%d dx=%d dy=%d)\n", himl1, i1, himl2,
2097 i2, dx, dy);
2099 if (!is_valid(himl1) || !is_valid(himl2))
2100 return NULL;
2102 if (dx > 0) {
2103 cxDst = max (himl1->cx, dx + himl2->cx);
2104 xOff1 = 0;
2105 xOff2 = dx;
2107 else if (dx < 0) {
2108 cxDst = max (himl2->cx, himl1->cx - dx);
2109 xOff1 = -dx;
2110 xOff2 = 0;
2112 else {
2113 cxDst = max (himl1->cx, himl2->cx);
2114 xOff1 = 0;
2115 xOff2 = 0;
2118 if (dy > 0) {
2119 cyDst = max (himl1->cy, dy + himl2->cy);
2120 yOff1 = 0;
2121 yOff2 = dy;
2123 else if (dy < 0) {
2124 cyDst = max (himl2->cy, himl1->cy - dy);
2125 yOff1 = -dy;
2126 yOff2 = 0;
2128 else {
2129 cyDst = max (himl1->cy, himl2->cy);
2130 yOff1 = 0;
2131 yOff2 = 0;
2134 newFlags = (himl1->flags > himl2->flags ? himl1->flags : himl2->flags) & ILC_COLORDDB;
2135 if (newFlags == ILC_COLORDDB && (himl1->flags & ILC_COLORDDB) == ILC_COLOR16)
2136 newFlags = ILC_COLOR16; /* this is what native (at least v5) does, don't know why */
2137 himlDst = ImageList_Create (cxDst, cyDst, ILC_MASK | newFlags, 1, 1);
2139 if (himlDst)
2141 imagelist_point_from_index( himl1, i1, &pt1 );
2142 imagelist_point_from_index( himl2, i2, &pt2 );
2144 /* copy image */
2145 BitBlt (himlDst->hdcImage, 0, 0, cxDst, cyDst, himl1->hdcImage, 0, 0, BLACKNESS);
2146 if (i1 >= 0 && i1 < himl1->cCurImage)
2147 BitBlt (himlDst->hdcImage, xOff1, yOff1, himl1->cx, himl1->cy, himl1->hdcImage, pt1.x, pt1.y, SRCCOPY);
2148 if (i2 >= 0 && i2 < himl2->cCurImage)
2150 if (himl2->flags & ILC_MASK)
2152 BitBlt (himlDst->hdcImage, xOff2, yOff2, himl2->cx, himl2->cy, himl2->hdcMask , pt2.x, pt2.y, SRCAND);
2153 BitBlt (himlDst->hdcImage, xOff2, yOff2, himl2->cx, himl2->cy, himl2->hdcImage, pt2.x, pt2.y, SRCPAINT);
2155 else
2156 BitBlt (himlDst->hdcImage, xOff2, yOff2, himl2->cx, himl2->cy, himl2->hdcImage, pt2.x, pt2.y, SRCCOPY);
2159 /* copy mask */
2160 BitBlt (himlDst->hdcMask, 0, 0, cxDst, cyDst, himl1->hdcMask, 0, 0, WHITENESS);
2161 if (i1 >= 0 && i1 < himl1->cCurImage)
2162 BitBlt (himlDst->hdcMask, xOff1, yOff1, himl1->cx, himl1->cy, himl1->hdcMask, pt1.x, pt1.y, SRCCOPY);
2163 if (i2 >= 0 && i2 < himl2->cCurImage)
2164 BitBlt (himlDst->hdcMask, xOff2, yOff2, himl2->cx, himl2->cy, himl2->hdcMask, pt2.x, pt2.y, SRCAND);
2166 himlDst->cCurImage = 1;
2169 return himlDst;
2173 /* helper for ImageList_Read, see comments below */
2174 static void *read_bitmap(LPSTREAM pstm, BITMAPINFO *bmi)
2176 BITMAPFILEHEADER bmfh;
2177 int bitsperpixel, palspace;
2178 void *bits;
2180 if (FAILED(IStream_Read ( pstm, &bmfh, sizeof(bmfh), NULL)))
2181 return NULL;
2183 if (bmfh.bfType != (('M'<<8)|'B'))
2184 return NULL;
2186 if (FAILED(IStream_Read ( pstm, &bmi->bmiHeader, sizeof(bmi->bmiHeader), NULL)))
2187 return NULL;
2189 if ((bmi->bmiHeader.biSize != sizeof(bmi->bmiHeader)))
2190 return NULL;
2192 TRACE("width %u, height %u, planes %u, bpp %u\n",
2193 bmi->bmiHeader.biWidth, bmi->bmiHeader.biHeight,
2194 bmi->bmiHeader.biPlanes, bmi->bmiHeader.biBitCount);
2196 bitsperpixel = bmi->bmiHeader.biPlanes * bmi->bmiHeader.biBitCount;
2197 if (bitsperpixel<=8)
2198 palspace = (1<<bitsperpixel)*sizeof(RGBQUAD);
2199 else
2200 palspace = 0;
2202 bmi->bmiHeader.biSizeImage = get_dib_image_size( bmi );
2204 /* read the palette right after the end of the bitmapinfoheader */
2205 if (palspace && FAILED(IStream_Read(pstm, bmi->bmiColors, palspace, NULL)))
2206 return NULL;
2208 bits = Alloc(bmi->bmiHeader.biSizeImage);
2209 if (!bits) return NULL;
2211 if (FAILED(IStream_Read(pstm, bits, bmi->bmiHeader.biSizeImage, NULL)))
2213 Free(bits);
2214 return NULL;
2216 return bits;
2219 /*************************************************************************
2220 * ImageList_Read [COMCTL32.@]
2222 * Reads an image list from a stream.
2224 * PARAMS
2225 * pstm [I] pointer to a stream
2227 * RETURNS
2228 * Success: handle to image list
2229 * Failure: NULL
2231 * The format is like this:
2232 * ILHEAD ilheadstruct;
2234 * for the color image part:
2235 * BITMAPFILEHEADER bmfh;
2236 * BITMAPINFOHEADER bmih;
2237 * only if it has a palette:
2238 * RGBQUAD rgbs[nr_of_paletted_colors];
2240 * BYTE colorbits[imagesize];
2242 * the following only if the ILC_MASK bit is set in ILHEAD.ilFlags:
2243 * BITMAPFILEHEADER bmfh_mask;
2244 * BITMAPINFOHEADER bmih_mask;
2245 * only if it has a palette (it usually does not):
2246 * RGBQUAD rgbs[nr_of_paletted_colors];
2248 * BYTE maskbits[imagesize];
2250 HIMAGELIST WINAPI ImageList_Read (LPSTREAM pstm)
2252 char image_buf[sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * 256];
2253 char mask_buf[sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * 256];
2254 BITMAPINFO *image_info = (BITMAPINFO *)image_buf;
2255 BITMAPINFO *mask_info = (BITMAPINFO *)mask_buf;
2256 void *image_bits, *mask_bits = NULL;
2257 ILHEAD ilHead;
2258 HIMAGELIST himl;
2259 unsigned int i;
2261 TRACE("%p\n", pstm);
2263 if (FAILED(IStream_Read (pstm, &ilHead, sizeof(ILHEAD), NULL)))
2264 return NULL;
2265 if (ilHead.usMagic != (('L' << 8) | 'I'))
2266 return NULL;
2267 if (ilHead.usVersion != 0x101) /* probably version? */
2268 return NULL;
2270 TRACE("cx %u, cy %u, flags 0x%04x, cCurImage %u, cMaxImage %u\n",
2271 ilHead.cx, ilHead.cy, ilHead.flags, ilHead.cCurImage, ilHead.cMaxImage);
2273 himl = ImageList_Create(ilHead.cx, ilHead.cy, ilHead.flags, ilHead.cCurImage, ilHead.cMaxImage);
2274 if (!himl)
2275 return NULL;
2277 if (!(image_bits = read_bitmap(pstm, image_info)))
2279 WARN("failed to read bitmap from stream\n");
2280 return NULL;
2282 if (ilHead.flags & ILC_MASK)
2284 if (!(mask_bits = read_bitmap(pstm, mask_info)))
2286 WARN("failed to read mask bitmap from stream\n");
2287 return NULL;
2290 else mask_info = NULL;
2292 if (himl->has_alpha && image_info->bmiHeader.biBitCount == 32)
2294 DWORD *ptr = image_bits;
2295 BYTE *mask_ptr = mask_bits;
2296 int stride = himl->cy * image_info->bmiHeader.biWidth;
2298 if (image_info->bmiHeader.biHeight > 0) /* bottom-up */
2300 ptr += image_info->bmiHeader.biHeight * image_info->bmiHeader.biWidth - stride;
2301 mask_ptr += (image_info->bmiHeader.biHeight * image_info->bmiHeader.biWidth - stride) / 8;
2302 stride = -stride;
2303 image_info->bmiHeader.biHeight = himl->cy;
2305 else image_info->bmiHeader.biHeight = -himl->cy;
2307 for (i = 0; i < ilHead.cCurImage; i += TILE_COUNT)
2309 add_dib_bits( himl, i, min( ilHead.cCurImage - i, TILE_COUNT ),
2310 himl->cx, himl->cy, image_info, mask_info, ptr, mask_ptr );
2311 ptr += stride;
2312 mask_ptr += stride / 8;
2315 else
2317 StretchDIBits( himl->hdcImage, 0, 0, image_info->bmiHeader.biWidth, image_info->bmiHeader.biHeight,
2318 0, 0, image_info->bmiHeader.biWidth, image_info->bmiHeader.biHeight,
2319 image_bits, image_info, DIB_RGB_COLORS, SRCCOPY);
2320 if (mask_info)
2321 StretchDIBits( himl->hdcMask, 0, 0, mask_info->bmiHeader.biWidth, mask_info->bmiHeader.biHeight,
2322 0, 0, mask_info->bmiHeader.biWidth, mask_info->bmiHeader.biHeight,
2323 mask_bits, mask_info, DIB_RGB_COLORS, SRCCOPY);
2325 Free( image_bits );
2326 Free( mask_bits );
2328 himl->cCurImage = ilHead.cCurImage;
2329 himl->cMaxImage = ilHead.cMaxImage;
2331 ImageList_SetBkColor(himl,ilHead.bkcolor);
2332 for (i=0;i<4;i++)
2333 ImageList_SetOverlayImage(himl,ilHead.ovls[i],i+1);
2334 return himl;
2338 /*************************************************************************
2339 * ImageList_Remove [COMCTL32.@]
2341 * Removes an image from an image list
2343 * PARAMS
2344 * himl [I] image list handle
2345 * i [I] image index
2347 * RETURNS
2348 * Success: TRUE
2349 * Failure: FALSE
2351 * FIXME: as the image list storage test shows, native comctl32 simply shifts
2352 * images without creating a new bitmap.
2354 BOOL WINAPI
2355 ImageList_Remove (HIMAGELIST himl, INT i)
2357 HBITMAP hbmNewImage, hbmNewMask;
2358 HDC hdcBmp;
2359 SIZE sz;
2361 TRACE("(himl=%p i=%d)\n", himl, i);
2363 if (!is_valid(himl)) {
2364 ERR("Invalid image list handle!\n");
2365 return FALSE;
2368 if ((i < -1) || (i >= himl->cCurImage)) {
2369 TRACE("index out of range! %d\n", i);
2370 return FALSE;
2373 if (i == -1) {
2374 INT nCount;
2376 /* remove all */
2377 if (himl->cCurImage == 0) {
2378 /* remove all on empty ImageList is allowed */
2379 TRACE("remove all on empty ImageList!\n");
2380 return TRUE;
2383 himl->cMaxImage = himl->cGrow;
2384 himl->cCurImage = 0;
2385 for (nCount = 0; nCount < MAX_OVERLAYIMAGE; nCount++)
2386 himl->nOvlIdx[nCount] = -1;
2388 if (himl->has_alpha)
2390 HeapFree( GetProcessHeap(), 0, himl->has_alpha );
2391 himl->has_alpha = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, himl->cMaxImage );
2394 hbmNewImage = ImageList_CreateImage(himl->hdcImage, himl, himl->cMaxImage);
2395 SelectObject (himl->hdcImage, hbmNewImage);
2396 DeleteObject (himl->hbmImage);
2397 himl->hbmImage = hbmNewImage;
2399 if (himl->hbmMask) {
2401 imagelist_get_bitmap_size(himl, himl->cMaxImage, &sz);
2402 hbmNewMask = CreateBitmap (sz.cx, sz.cy, 1, 1, NULL);
2403 SelectObject (himl->hdcMask, hbmNewMask);
2404 DeleteObject (himl->hbmMask);
2405 himl->hbmMask = hbmNewMask;
2408 else {
2409 /* delete one image */
2410 TRACE("Remove single image! %d\n", i);
2412 /* create new bitmap(s) */
2413 TRACE(" - Number of images: %d / %d (Old/New)\n",
2414 himl->cCurImage, himl->cCurImage - 1);
2416 hbmNewImage = ImageList_CreateImage(himl->hdcImage, himl, himl->cMaxImage);
2418 imagelist_get_bitmap_size(himl, himl->cMaxImage, &sz );
2419 if (himl->hbmMask)
2420 hbmNewMask = CreateBitmap (sz.cx, sz.cy, 1, 1, NULL);
2421 else
2422 hbmNewMask = 0; /* Just to keep compiler happy! */
2424 hdcBmp = CreateCompatibleDC (0);
2426 /* copy all images and masks prior to the "removed" image */
2427 if (i > 0) {
2428 TRACE("Pre image copy: Copy %d images\n", i);
2430 SelectObject (hdcBmp, hbmNewImage);
2431 imagelist_copy_images( himl, himl->hdcImage, hdcBmp, 0, i, 0 );
2433 if (himl->hbmMask) {
2434 SelectObject (hdcBmp, hbmNewMask);
2435 imagelist_copy_images( himl, himl->hdcMask, hdcBmp, 0, i, 0 );
2439 /* copy all images and masks behind the removed image */
2440 if (i < himl->cCurImage - 1) {
2441 TRACE("Post image copy!\n");
2443 SelectObject (hdcBmp, hbmNewImage);
2444 imagelist_copy_images( himl, himl->hdcImage, hdcBmp, i + 1,
2445 (himl->cCurImage - i), i );
2447 if (himl->hbmMask) {
2448 SelectObject (hdcBmp, hbmNewMask);
2449 imagelist_copy_images( himl, himl->hdcMask, hdcBmp, i + 1,
2450 (himl->cCurImage - i), i );
2454 DeleteDC (hdcBmp);
2456 /* delete old images and insert new ones */
2457 SelectObject (himl->hdcImage, hbmNewImage);
2458 DeleteObject (himl->hbmImage);
2459 himl->hbmImage = hbmNewImage;
2460 if (himl->hbmMask) {
2461 SelectObject (himl->hdcMask, hbmNewMask);
2462 DeleteObject (himl->hbmMask);
2463 himl->hbmMask = hbmNewMask;
2466 himl->cCurImage--;
2469 return TRUE;
2473 /*************************************************************************
2474 * ImageList_Replace [COMCTL32.@]
2476 * Replaces an image in an image list with a new image.
2478 * PARAMS
2479 * himl [I] handle to image list
2480 * i [I] image index
2481 * hbmImage [I] handle to image bitmap
2482 * hbmMask [I] handle to mask bitmap. Can be NULL.
2484 * RETURNS
2485 * Success: TRUE
2486 * Failure: FALSE
2489 BOOL WINAPI
2490 ImageList_Replace (HIMAGELIST himl, INT i, HBITMAP hbmImage,
2491 HBITMAP hbmMask)
2493 HDC hdcImage;
2494 BITMAP bmp;
2495 POINT pt;
2497 TRACE("%p %d %p %p\n", himl, i, hbmImage, hbmMask);
2499 if (!is_valid(himl)) {
2500 ERR("Invalid image list handle!\n");
2501 return FALSE;
2504 if ((i >= himl->cMaxImage) || (i < 0)) {
2505 ERR("Invalid image index!\n");
2506 return FALSE;
2509 if (!GetObjectW(hbmImage, sizeof(BITMAP), &bmp))
2510 return FALSE;
2512 hdcImage = CreateCompatibleDC (0);
2514 /* Replace Image */
2515 SelectObject (hdcImage, hbmImage);
2517 if (add_with_alpha( himl, hdcImage, i, 1, bmp.bmWidth, bmp.bmHeight, hbmImage, hbmMask ))
2518 goto done;
2520 imagelist_point_from_index(himl, i, &pt);
2521 StretchBlt (himl->hdcImage, pt.x, pt.y, himl->cx, himl->cy,
2522 hdcImage, 0, 0, bmp.bmWidth, bmp.bmHeight, SRCCOPY);
2524 if (himl->hbmMask)
2526 HDC hdcTemp;
2527 HBITMAP hOldBitmapTemp;
2529 hdcTemp = CreateCompatibleDC(0);
2530 hOldBitmapTemp = SelectObject(hdcTemp, hbmMask);
2532 StretchBlt (himl->hdcMask, pt.x, pt.y, himl->cx, himl->cy,
2533 hdcTemp, 0, 0, bmp.bmWidth, bmp.bmHeight, SRCCOPY);
2534 SelectObject(hdcTemp, hOldBitmapTemp);
2535 DeleteDC(hdcTemp);
2537 /* Remove the background from the image
2539 BitBlt (himl->hdcImage, pt.x, pt.y, bmp.bmWidth, bmp.bmHeight,
2540 himl->hdcMask, pt.x, pt.y, 0x220326); /* NOTSRCAND */
2543 done:
2544 DeleteDC (hdcImage);
2546 return TRUE;
2550 /*************************************************************************
2551 * ImageList_ReplaceIcon [COMCTL32.@]
2553 * Replaces an image in an image list using an icon.
2555 * PARAMS
2556 * himl [I] handle to image list
2557 * i [I] image index
2558 * hIcon [I] handle to icon
2560 * RETURNS
2561 * Success: index of the replaced image
2562 * Failure: -1
2565 INT WINAPI
2566 ImageList_ReplaceIcon (HIMAGELIST himl, INT nIndex, HICON hIcon)
2568 HICON hBestFitIcon;
2569 ICONINFO ii;
2570 BITMAP bmp;
2571 BOOL ret;
2572 POINT pt;
2574 TRACE("(%p %d %p)\n", himl, nIndex, hIcon);
2576 if (!is_valid(himl)) {
2577 ERR("invalid image list\n");
2578 return -1;
2580 if ((nIndex >= himl->cMaxImage) || (nIndex < -1)) {
2581 ERR("invalid image index %d / %d\n", nIndex, himl->cMaxImage);
2582 return -1;
2585 hBestFitIcon = CopyImage(
2586 hIcon, IMAGE_ICON,
2587 himl->cx, himl->cy,
2588 LR_COPYFROMRESOURCE);
2589 /* the above will fail if the icon wasn't loaded from a resource, so try
2590 * again without LR_COPYFROMRESOURCE flag */
2591 if (!hBestFitIcon)
2592 hBestFitIcon = CopyImage(
2593 hIcon, IMAGE_ICON,
2594 himl->cx, himl->cy,
2596 if (!hBestFitIcon)
2597 return -1;
2599 if (nIndex == -1) {
2600 if (himl->cCurImage + 1 >= himl->cMaxImage)
2601 IMAGELIST_InternalExpandBitmaps(himl, 1);
2603 nIndex = himl->cCurImage;
2604 himl->cCurImage++;
2607 if (himl->has_alpha && GetIconInfo (hBestFitIcon, &ii))
2609 HDC hdcImage = CreateCompatibleDC( 0 );
2610 GetObjectW (ii.hbmMask, sizeof(BITMAP), &bmp);
2612 if (!ii.hbmColor)
2614 UINT height = bmp.bmHeight / 2;
2615 HDC hdcMask = CreateCompatibleDC( 0 );
2616 HBITMAP color = CreateBitmap( bmp.bmWidth, height, 1, 1, NULL );
2617 SelectObject( hdcImage, color );
2618 SelectObject( hdcMask, ii.hbmMask );
2619 BitBlt( hdcImage, 0, 0, bmp.bmWidth, height, hdcMask, 0, height, SRCCOPY );
2620 ret = add_with_alpha( himl, hdcImage, nIndex, 1, bmp.bmWidth, height, color, ii.hbmMask );
2621 DeleteDC( hdcMask );
2622 DeleteObject( color );
2624 else ret = add_with_alpha( himl, hdcImage, nIndex, 1, bmp.bmWidth, bmp.bmHeight,
2625 ii.hbmColor, ii.hbmMask );
2627 DeleteDC( hdcImage );
2628 DeleteObject (ii.hbmMask);
2629 if (ii.hbmColor) DeleteObject (ii.hbmColor);
2630 if (ret) goto done;
2633 imagelist_point_from_index(himl, nIndex, &pt);
2635 if (himl->hbmMask)
2637 DrawIconEx( himl->hdcImage, pt.x, pt.y, hBestFitIcon, himl->cx, himl->cy, 0, 0, DI_IMAGE );
2638 PatBlt( himl->hdcMask, pt.x, pt.y, himl->cx, himl->cy, WHITENESS );
2639 DrawIconEx( himl->hdcMask, pt.x, pt.y, hBestFitIcon, himl->cx, himl->cy, 0, 0, DI_MASK );
2641 else
2643 COLORREF color = himl->clrBk != CLR_NONE ? himl->clrBk : comctl32_color.clrWindow;
2644 HBRUSH brush = CreateSolidBrush( GetNearestColor( himl->hdcImage, color ));
2646 SelectObject( himl->hdcImage, brush );
2647 PatBlt( himl->hdcImage, pt.x, pt.y, himl->cx, himl->cy, PATCOPY );
2648 SelectObject( himl->hdcImage, GetStockObject(BLACK_BRUSH) );
2649 DeleteObject( brush );
2650 DrawIconEx( himl->hdcImage, pt.x, pt.y, hBestFitIcon, himl->cx, himl->cy, 0, 0, DI_NORMAL );
2653 done:
2654 DestroyIcon(hBestFitIcon);
2656 TRACE("Insert index = %d, himl->cCurImage = %d\n", nIndex, himl->cCurImage);
2657 return nIndex;
2661 /*************************************************************************
2662 * ImageList_SetBkColor [COMCTL32.@]
2664 * Sets the background color of an image list.
2666 * PARAMS
2667 * himl [I] handle to image list
2668 * clrBk [I] background color
2670 * RETURNS
2671 * Success: previous background color
2672 * Failure: CLR_NONE
2675 COLORREF WINAPI
2676 ImageList_SetBkColor (HIMAGELIST himl, COLORREF clrBk)
2678 COLORREF clrOldBk;
2680 if (!is_valid(himl))
2681 return CLR_NONE;
2683 clrOldBk = himl->clrBk;
2684 himl->clrBk = clrBk;
2685 return clrOldBk;
2689 /*************************************************************************
2690 * ImageList_SetDragCursorImage [COMCTL32.@]
2692 * Combines the specified image with the current drag image
2694 * PARAMS
2695 * himlDrag [I] handle to drag image list
2696 * iDrag [I] drag image index
2697 * dxHotspot [I] X position of the hot spot
2698 * dyHotspot [I] Y position of the hot spot
2700 * RETURNS
2701 * Success: TRUE
2702 * Failure: FALSE
2704 * NOTES
2705 * - The names dxHotspot, dyHotspot are misleading because they have nothing
2706 * to do with a hotspot but are only the offset of the origin of the new
2707 * image relative to the origin of the old image.
2709 * - When this function is called and the drag image is visible, a
2710 * short flickering occurs but this matches the Win9x behavior. It is
2711 * possible to fix the flickering using code like in ImageList_DragMove.
2714 BOOL WINAPI
2715 ImageList_SetDragCursorImage (HIMAGELIST himlDrag, INT iDrag,
2716 INT dxHotspot, INT dyHotspot)
2718 HIMAGELIST himlTemp;
2719 BOOL visible;
2721 if (!is_valid(InternalDrag.himl) || !is_valid(himlDrag))
2722 return FALSE;
2724 TRACE(" dxH=%d dyH=%d nX=%d nY=%d\n",
2725 dxHotspot, dyHotspot, InternalDrag.dxHotspot, InternalDrag.dyHotspot);
2727 visible = InternalDrag.bShow;
2729 himlTemp = ImageList_Merge (InternalDrag.himlNoCursor, 0, himlDrag, iDrag,
2730 dxHotspot, dyHotspot);
2732 if (visible) {
2733 /* hide the drag image */
2734 ImageList_DragShowNolock(FALSE);
2736 if ((InternalDrag.himl->cx != himlTemp->cx) ||
2737 (InternalDrag.himl->cy != himlTemp->cy)) {
2738 /* the size of the drag image changed, invalidate the buffer */
2739 DeleteObject(InternalDrag.hbmBg);
2740 InternalDrag.hbmBg = 0;
2743 if (InternalDrag.himl != InternalDrag.himlNoCursor)
2744 ImageList_Destroy (InternalDrag.himl);
2745 InternalDrag.himl = himlTemp;
2747 if (visible) {
2748 /* show the drag image */
2749 ImageList_DragShowNolock(TRUE);
2752 return TRUE;
2756 /*************************************************************************
2757 * ImageList_SetFilter [COMCTL32.@]
2759 * Sets a filter (or does something completely different)!!???
2760 * It removes 12 Bytes from the stack (3 Parameters).
2762 * PARAMS
2763 * himl [I] SHOULD be a handle to image list
2764 * i [I] COULD be an index?
2765 * dwFilter [I] ???
2767 * RETURNS
2768 * Success: TRUE ???
2769 * Failure: FALSE ???
2771 * BUGS
2772 * This is an UNDOCUMENTED function!!!!
2773 * empty stub.
2776 BOOL WINAPI
2777 ImageList_SetFilter (HIMAGELIST himl, INT i, DWORD dwFilter)
2779 FIXME("(%p 0x%x 0x%x):empty stub!\n", himl, i, dwFilter);
2781 return FALSE;
2785 /*************************************************************************
2786 * ImageList_SetFlags [COMCTL32.@]
2788 * Sets the image list flags.
2790 * PARAMS
2791 * himl [I] Handle to image list
2792 * flags [I] Flags to set
2794 * RETURNS
2795 * Old flags?
2797 * BUGS
2798 * Stub.
2801 DWORD WINAPI
2802 ImageList_SetFlags(HIMAGELIST himl, DWORD flags)
2804 FIXME("(%p %08x):empty stub\n", himl, flags);
2805 return 0;
2809 /*************************************************************************
2810 * ImageList_SetIconSize [COMCTL32.@]
2812 * Sets the image size of the bitmap and deletes all images.
2814 * PARAMS
2815 * himl [I] handle to image list
2816 * cx [I] image width
2817 * cy [I] image height
2819 * RETURNS
2820 * Success: TRUE
2821 * Failure: FALSE
2824 BOOL WINAPI
2825 ImageList_SetIconSize (HIMAGELIST himl, INT cx, INT cy)
2827 INT nCount;
2828 HBITMAP hbmNew;
2830 if (!is_valid(himl))
2831 return FALSE;
2833 /* remove all images */
2834 himl->cMaxImage = himl->cInitial + 1;
2835 himl->cCurImage = 0;
2836 himl->cx = cx;
2837 himl->cy = cy;
2839 /* initialize overlay mask indices */
2840 for (nCount = 0; nCount < MAX_OVERLAYIMAGE; nCount++)
2841 himl->nOvlIdx[nCount] = -1;
2843 hbmNew = ImageList_CreateImage(himl->hdcImage, himl, himl->cMaxImage);
2844 SelectObject (himl->hdcImage, hbmNew);
2845 DeleteObject (himl->hbmImage);
2846 himl->hbmImage = hbmNew;
2848 if (himl->hbmMask) {
2849 SIZE sz;
2850 imagelist_get_bitmap_size(himl, himl->cMaxImage, &sz);
2851 hbmNew = CreateBitmap (sz.cx, sz.cy, 1, 1, NULL);
2852 SelectObject (himl->hdcMask, hbmNew);
2853 DeleteObject (himl->hbmMask);
2854 himl->hbmMask = hbmNew;
2857 return TRUE;
2861 /*************************************************************************
2862 * ImageList_SetImageCount [COMCTL32.@]
2864 * Resizes an image list to the specified number of images.
2866 * PARAMS
2867 * himl [I] handle to image list
2868 * iImageCount [I] number of images in the image list
2870 * RETURNS
2871 * Success: TRUE
2872 * Failure: FALSE
2875 BOOL WINAPI
2876 ImageList_SetImageCount (HIMAGELIST himl, UINT iImageCount)
2878 HDC hdcBitmap;
2879 HBITMAP hbmNewBitmap, hbmOld;
2880 INT nNewCount, nCopyCount;
2882 TRACE("%p %d\n",himl,iImageCount);
2884 if (!is_valid(himl))
2885 return FALSE;
2887 nNewCount = iImageCount + 1;
2888 nCopyCount = min(himl->cCurImage, iImageCount);
2890 hdcBitmap = CreateCompatibleDC (0);
2892 hbmNewBitmap = ImageList_CreateImage(hdcBitmap, himl, nNewCount);
2894 if (hbmNewBitmap != 0)
2896 hbmOld = SelectObject (hdcBitmap, hbmNewBitmap);
2897 imagelist_copy_images( himl, himl->hdcImage, hdcBitmap, 0, nCopyCount, 0 );
2898 SelectObject (hdcBitmap, hbmOld);
2900 /* FIXME: delete 'empty' image space? */
2902 SelectObject (himl->hdcImage, hbmNewBitmap);
2903 DeleteObject (himl->hbmImage);
2904 himl->hbmImage = hbmNewBitmap;
2906 else
2907 ERR("Could not create new image bitmap!\n");
2909 if (himl->hbmMask)
2911 SIZE sz;
2912 imagelist_get_bitmap_size( himl, nNewCount, &sz );
2913 hbmNewBitmap = CreateBitmap (sz.cx, sz.cy, 1, 1, NULL);
2914 if (hbmNewBitmap != 0)
2916 hbmOld = SelectObject (hdcBitmap, hbmNewBitmap);
2917 imagelist_copy_images( himl, himl->hdcMask, hdcBitmap, 0, nCopyCount, 0 );
2918 SelectObject (hdcBitmap, hbmOld);
2920 /* FIXME: delete 'empty' image space? */
2922 SelectObject (himl->hdcMask, hbmNewBitmap);
2923 DeleteObject (himl->hbmMask);
2924 himl->hbmMask = hbmNewBitmap;
2926 else
2927 ERR("Could not create new mask bitmap!\n");
2930 DeleteDC (hdcBitmap);
2932 if (himl->has_alpha)
2934 char *new_alpha = HeapReAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, himl->has_alpha, nNewCount );
2935 if (new_alpha) himl->has_alpha = new_alpha;
2936 else
2938 HeapFree( GetProcessHeap(), 0, himl->has_alpha );
2939 himl->has_alpha = NULL;
2943 /* Update max image count and current image count */
2944 himl->cMaxImage = nNewCount;
2945 himl->cCurImage = iImageCount;
2947 return TRUE;
2951 /*************************************************************************
2952 * ImageList_SetOverlayImage [COMCTL32.@]
2954 * Assigns an overlay mask index to an existing image in an image list.
2956 * PARAMS
2957 * himl [I] handle to image list
2958 * iImage [I] image index
2959 * iOverlay [I] overlay mask index
2961 * RETURNS
2962 * Success: TRUE
2963 * Failure: FALSE
2966 BOOL WINAPI
2967 ImageList_SetOverlayImage (HIMAGELIST himl, INT iImage, INT iOverlay)
2969 if (!is_valid(himl))
2970 return FALSE;
2971 if ((iOverlay < 1) || (iOverlay > MAX_OVERLAYIMAGE))
2972 return FALSE;
2973 if ((iImage!=-1) && ((iImage < 0) || (iImage > himl->cCurImage)))
2974 return FALSE;
2975 himl->nOvlIdx[iOverlay - 1] = iImage;
2976 return TRUE;
2981 /* helper for ImageList_Write - write bitmap to pstm
2982 * currently everything is written as 24 bit RGB, except masks
2984 static BOOL
2985 _write_bitmap(HBITMAP hBitmap, LPSTREAM pstm)
2987 LPBITMAPFILEHEADER bmfh;
2988 LPBITMAPINFOHEADER bmih;
2989 LPBYTE data = NULL, lpBits;
2990 BITMAP bm;
2991 INT bitCount, sizeImage, offBits, totalSize;
2992 HDC xdc;
2993 BOOL result = FALSE;
2995 if (!GetObjectW(hBitmap, sizeof(BITMAP), &bm))
2996 return FALSE;
2998 bitCount = bm.bmBitsPixel;
2999 sizeImage = get_dib_stride(bm.bmWidth, bitCount) * bm.bmHeight;
3001 totalSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
3002 if(bitCount <= 8)
3003 totalSize += (1 << bitCount) * sizeof(RGBQUAD);
3004 offBits = totalSize;
3005 totalSize += sizeImage;
3007 data = Alloc(totalSize);
3008 bmfh = (LPBITMAPFILEHEADER)data;
3009 bmih = (LPBITMAPINFOHEADER)(data + sizeof(BITMAPFILEHEADER));
3010 lpBits = data + offBits;
3012 /* setup BITMAPFILEHEADER */
3013 bmfh->bfType = (('M' << 8) | 'B');
3014 bmfh->bfSize = offBits;
3015 bmfh->bfReserved1 = 0;
3016 bmfh->bfReserved2 = 0;
3017 bmfh->bfOffBits = offBits;
3019 /* setup BITMAPINFOHEADER */
3020 bmih->biSize = sizeof(BITMAPINFOHEADER);
3021 bmih->biWidth = bm.bmWidth;
3022 bmih->biHeight = bm.bmHeight;
3023 bmih->biPlanes = 1;
3024 bmih->biBitCount = bitCount;
3025 bmih->biCompression = BI_RGB;
3026 bmih->biSizeImage = sizeImage;
3027 bmih->biXPelsPerMeter = 0;
3028 bmih->biYPelsPerMeter = 0;
3029 bmih->biClrUsed = 0;
3030 bmih->biClrImportant = 0;
3032 xdc = GetDC(0);
3033 result = GetDIBits(xdc, hBitmap, 0, bm.bmHeight, lpBits, (BITMAPINFO *)bmih, DIB_RGB_COLORS) == bm.bmHeight;
3034 ReleaseDC(0, xdc);
3035 if (!result)
3036 goto failed;
3038 TRACE("width %u, height %u, planes %u, bpp %u\n",
3039 bmih->biWidth, bmih->biHeight,
3040 bmih->biPlanes, bmih->biBitCount);
3042 if(FAILED(IStream_Write(pstm, data, totalSize, NULL)))
3043 goto failed;
3045 result = TRUE;
3047 failed:
3048 Free(data);
3050 return result;
3054 /*************************************************************************
3055 * ImageList_Write [COMCTL32.@]
3057 * Writes an image list to a stream.
3059 * PARAMS
3060 * himl [I] handle to image list
3061 * pstm [O] Pointer to a stream.
3063 * RETURNS
3064 * Success: TRUE
3065 * Failure: FALSE
3067 * BUGS
3068 * probably.
3071 BOOL WINAPI
3072 ImageList_Write (HIMAGELIST himl, LPSTREAM pstm)
3074 ILHEAD ilHead;
3075 int i;
3077 TRACE("%p %p\n", himl, pstm);
3079 if (!is_valid(himl))
3080 return FALSE;
3082 ilHead.usMagic = (('L' << 8) | 'I');
3083 ilHead.usVersion = 0x101;
3084 ilHead.cCurImage = himl->cCurImage;
3085 ilHead.cMaxImage = himl->cMaxImage;
3086 ilHead.cGrow = himl->cGrow;
3087 ilHead.cx = himl->cx;
3088 ilHead.cy = himl->cy;
3089 ilHead.bkcolor = himl->clrBk;
3090 ilHead.flags = himl->flags;
3091 for(i = 0; i < 4; i++) {
3092 ilHead.ovls[i] = himl->nOvlIdx[i];
3095 TRACE("cx %u, cy %u, flags 0x04%x, cCurImage %u, cMaxImage %u\n",
3096 ilHead.cx, ilHead.cy, ilHead.flags, ilHead.cCurImage, ilHead.cMaxImage);
3098 if(FAILED(IStream_Write(pstm, &ilHead, sizeof(ILHEAD), NULL)))
3099 return FALSE;
3101 /* write the bitmap */
3102 if(!_write_bitmap(himl->hbmImage, pstm))
3103 return FALSE;
3105 /* write the mask if we have one */
3106 if(himl->flags & ILC_MASK) {
3107 if(!_write_bitmap(himl->hbmMask, pstm))
3108 return FALSE;
3111 return TRUE;
3115 static HBITMAP ImageList_CreateImage(HDC hdc, HIMAGELIST himl, UINT count)
3117 HBITMAP hbmNewBitmap;
3118 UINT ilc = (himl->flags & 0xFE);
3119 SIZE sz;
3121 imagelist_get_bitmap_size( himl, count, &sz );
3123 if ((ilc >= ILC_COLOR4 && ilc <= ILC_COLOR32) || ilc == ILC_COLOR)
3125 char buffer[sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD)];
3126 BITMAPINFO *bmi = (BITMAPINFO *)buffer;
3128 TRACE("Creating DIBSection %d x %d, %d Bits per Pixel\n",
3129 sz.cx, sz.cy, himl->uBitsPixel);
3131 memset( buffer, 0, sizeof(buffer) );
3132 bmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
3133 bmi->bmiHeader.biWidth = sz.cx;
3134 bmi->bmiHeader.biHeight = sz.cy;
3135 bmi->bmiHeader.biPlanes = 1;
3136 bmi->bmiHeader.biBitCount = himl->uBitsPixel;
3137 bmi->bmiHeader.biCompression = BI_RGB;
3139 if (himl->uBitsPixel <= ILC_COLOR8)
3141 /* retrieve the default color map */
3142 HBITMAP tmp = CreateBitmap( 1, 1, 1, 1, NULL );
3143 GetDIBits( hdc, tmp, 0, 0, NULL, bmi, DIB_RGB_COLORS );
3144 DeleteObject( tmp );
3146 hbmNewBitmap = CreateDIBSection(hdc, bmi, DIB_RGB_COLORS, NULL, 0, 0);
3148 else /*if (ilc == ILC_COLORDDB)*/
3150 TRACE("Creating Bitmap: %d Bits per Pixel\n", himl->uBitsPixel);
3152 hbmNewBitmap = CreateBitmap (sz.cx, sz.cy, 1, himl->uBitsPixel, NULL);
3154 TRACE("returning %p\n", hbmNewBitmap);
3155 return hbmNewBitmap;
3158 /*************************************************************************
3159 * ImageList_SetColorTable [COMCTL32.@]
3161 * Sets the color table of an image list.
3163 * PARAMS
3164 * himl [I] Handle to the image list.
3165 * uStartIndex [I] The first index to set.
3166 * cEntries [I] Number of entries to set.
3167 * prgb [I] New color information for color table for the image list.
3169 * RETURNS
3170 * Success: Number of entries in the table that were set.
3171 * Failure: Zero.
3173 * SEE
3174 * ImageList_Create(), SetDIBColorTable()
3177 UINT WINAPI
3178 ImageList_SetColorTable(HIMAGELIST himl, UINT uStartIndex, UINT cEntries, const RGBQUAD *prgb)
3180 return SetDIBColorTable(himl->hdcImage, uStartIndex, cEntries, prgb);
3183 /*************************************************************************
3184 * ImageList_CoCreateInstance [COMCTL32.@]
3186 * Creates a new imagelist instance and returns an interface pointer to it.
3188 * PARAMS
3189 * rclsid [I] A reference to the CLSID (CLSID_ImageList).
3190 * punkOuter [I] Pointer to IUnknown interface for aggregation, if desired
3191 * riid [I] Identifier of the requested interface.
3192 * ppv [O] Returns the address of the pointer requested, or NULL.
3194 * RETURNS
3195 * Success: S_OK.
3196 * Failure: Error value.
3198 HRESULT WINAPI
3199 ImageList_CoCreateInstance (REFCLSID rclsid, const IUnknown *punkOuter, REFIID riid, void **ppv)
3201 TRACE("(%s,%p,%s,%p)\n", debugstr_guid(rclsid), punkOuter, debugstr_guid(riid), ppv);
3203 if (!IsEqualCLSID(&CLSID_ImageList, rclsid))
3204 return E_NOINTERFACE;
3206 return ImageListImpl_CreateInstance(punkOuter, riid, ppv);
3210 /*************************************************************************
3211 * IImageList implementation
3214 static HRESULT WINAPI ImageListImpl_QueryInterface(IImageList2 *iface,
3215 REFIID iid, void **ppv)
3217 HIMAGELIST imgl = impl_from_IImageList2(iface);
3219 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
3221 if (!ppv) return E_INVALIDARG;
3223 if (IsEqualIID(&IID_IUnknown, iid) ||
3224 IsEqualIID(&IID_IImageList, iid) ||
3225 IsEqualIID(&IID_IImageList2, iid))
3227 *ppv = &imgl->IImageList2_iface;
3229 else
3231 *ppv = NULL;
3232 return E_NOINTERFACE;
3235 IImageList2_AddRef(iface);
3236 return S_OK;
3239 static ULONG WINAPI ImageListImpl_AddRef(IImageList2 *iface)
3241 HIMAGELIST imgl = impl_from_IImageList2(iface);
3242 ULONG ref = InterlockedIncrement(&imgl->ref);
3244 TRACE("(%p) refcount=%u\n", iface, ref);
3245 return ref;
3248 static ULONG WINAPI ImageListImpl_Release(IImageList2 *iface)
3250 HIMAGELIST This = impl_from_IImageList2(iface);
3251 ULONG ref = InterlockedDecrement(&This->ref);
3253 TRACE("(%p) refcount=%u\n", iface, ref);
3255 if (ref == 0)
3257 /* delete image bitmaps */
3258 if (This->hbmImage) DeleteObject (This->hbmImage);
3259 if (This->hbmMask) DeleteObject (This->hbmMask);
3261 /* delete image & mask DCs */
3262 if (This->hdcImage) DeleteDC (This->hdcImage);
3263 if (This->hdcMask) DeleteDC (This->hdcMask);
3265 /* delete blending brushes */
3266 if (This->hbrBlend25) DeleteObject (This->hbrBlend25);
3267 if (This->hbrBlend50) DeleteObject (This->hbrBlend50);
3269 This->IImageList2_iface.lpVtbl = NULL;
3270 HeapFree(GetProcessHeap(), 0, This->has_alpha);
3271 HeapFree(GetProcessHeap(), 0, This);
3274 return ref;
3277 static HRESULT WINAPI ImageListImpl_Add(IImageList2 *iface, HBITMAP hbmImage,
3278 HBITMAP hbmMask, int *pi)
3280 HIMAGELIST imgl = impl_from_IImageList2(iface);
3281 int ret;
3283 if (!pi)
3284 return E_FAIL;
3286 ret = ImageList_Add(imgl, hbmImage, hbmMask);
3288 if (ret == -1)
3289 return E_FAIL;
3291 *pi = ret;
3292 return S_OK;
3295 static HRESULT WINAPI ImageListImpl_ReplaceIcon(IImageList2 *iface, int i,
3296 HICON hicon, int *pi)
3298 HIMAGELIST imgl = impl_from_IImageList2(iface);
3299 int ret;
3301 if (!pi)
3302 return E_FAIL;
3304 ret = ImageList_ReplaceIcon(imgl, i, hicon);
3306 if (ret == -1)
3307 return E_FAIL;
3309 *pi = ret;
3310 return S_OK;
3313 static HRESULT WINAPI ImageListImpl_SetOverlayImage(IImageList2 *iface,
3314 int iImage, int iOverlay)
3316 HIMAGELIST imgl = impl_from_IImageList2(iface);
3317 return ImageList_SetOverlayImage(imgl, iImage, iOverlay) ? S_OK : E_FAIL;
3320 static HRESULT WINAPI ImageListImpl_Replace(IImageList2 *iface, int i,
3321 HBITMAP hbmImage, HBITMAP hbmMask)
3323 HIMAGELIST imgl = impl_from_IImageList2(iface);
3324 return ImageList_Replace(imgl, i, hbmImage, hbmMask) ? S_OK : E_FAIL;
3327 static HRESULT WINAPI ImageListImpl_AddMasked(IImageList2 *iface, HBITMAP hbmImage,
3328 COLORREF crMask, int *pi)
3330 HIMAGELIST imgl = impl_from_IImageList2(iface);
3331 int ret;
3333 if (!pi)
3334 return E_FAIL;
3336 ret = ImageList_AddMasked(imgl, hbmImage, crMask);
3338 if (ret == -1)
3339 return E_FAIL;
3341 *pi = ret;
3342 return S_OK;
3345 static HRESULT WINAPI ImageListImpl_Draw(IImageList2 *iface,
3346 IMAGELISTDRAWPARAMS *pimldp)
3348 HIMAGELIST imgl = impl_from_IImageList2(iface);
3349 HIMAGELIST old_himl;
3350 int ret;
3352 /* As far as I can tell, Windows simply ignores the contents of pimldp->himl
3353 so we shall simulate the same */
3354 old_himl = pimldp->himl;
3355 pimldp->himl = imgl;
3357 ret = ImageList_DrawIndirect(pimldp);
3359 pimldp->himl = old_himl;
3360 return ret ? S_OK : E_INVALIDARG;
3363 static HRESULT WINAPI ImageListImpl_Remove(IImageList2 *iface, int i)
3365 HIMAGELIST imgl = impl_from_IImageList2(iface);
3366 return (ImageList_Remove(imgl, i) == 0) ? E_INVALIDARG : S_OK;
3369 static HRESULT WINAPI ImageListImpl_GetIcon(IImageList2 *iface, int i, UINT flags,
3370 HICON *picon)
3372 HIMAGELIST imgl = impl_from_IImageList2(iface);
3373 HICON hIcon;
3375 if (!picon)
3376 return E_FAIL;
3378 hIcon = ImageList_GetIcon(imgl, i, flags);
3380 if (hIcon == NULL)
3381 return E_FAIL;
3383 *picon = hIcon;
3384 return S_OK;
3387 static HRESULT WINAPI ImageListImpl_GetImageInfo(IImageList2 *iface, int i,
3388 IMAGEINFO *pImageInfo)
3390 HIMAGELIST imgl = impl_from_IImageList2(iface);
3391 return ImageList_GetImageInfo(imgl, i, pImageInfo) ? S_OK : E_FAIL;
3394 static HRESULT WINAPI ImageListImpl_Copy(IImageList2 *iface, int dst_index,
3395 IUnknown *unk_src, int src_index, UINT flags)
3397 HIMAGELIST imgl = impl_from_IImageList2(iface);
3398 IImageList *src = NULL;
3399 HRESULT ret;
3401 if (!unk_src)
3402 return E_FAIL;
3404 /* TODO: Add test for IID_ImageList2 too */
3405 if (FAILED(IUnknown_QueryInterface(unk_src, &IID_IImageList,
3406 (void **) &src)))
3407 return E_FAIL;
3409 if (ImageList_Copy(imgl, dst_index, (HIMAGELIST) src, src_index, flags))
3410 ret = S_OK;
3411 else
3412 ret = E_FAIL;
3414 IImageList_Release(src);
3415 return ret;
3418 static HRESULT WINAPI ImageListImpl_Merge(IImageList2 *iface, int i1,
3419 IUnknown *punk2, int i2, int dx, int dy, REFIID riid, void **ppv)
3421 HIMAGELIST imgl = impl_from_IImageList2(iface);
3422 IImageList *iml2 = NULL;
3423 HIMAGELIST merged;
3424 HRESULT ret = E_FAIL;
3426 TRACE("(%p)->(%d %p %d %d %d %s %p)\n", iface, i1, punk2, i2, dx, dy, debugstr_guid(riid), ppv);
3428 /* TODO: Add test for IID_ImageList2 too */
3429 if (FAILED(IUnknown_QueryInterface(punk2, &IID_IImageList,
3430 (void **) &iml2)))
3431 return E_FAIL;
3433 merged = ImageList_Merge(imgl, i1, (HIMAGELIST) iml2, i2, dx, dy);
3435 /* Get the interface for the new image list */
3436 if (merged)
3438 ret = HIMAGELIST_QueryInterface(merged, riid, ppv);
3439 ImageList_Destroy(merged);
3442 IImageList_Release(iml2);
3443 return ret;
3446 static HRESULT WINAPI ImageListImpl_Clone(IImageList2 *iface, REFIID riid, void **ppv)
3448 HIMAGELIST imgl = impl_from_IImageList2(iface);
3449 HIMAGELIST clone;
3450 HRESULT ret = E_FAIL;
3452 TRACE("(%p)->(%s %p)\n", iface, debugstr_guid(riid), ppv);
3454 clone = ImageList_Duplicate(imgl);
3456 /* Get the interface for the new image list */
3457 if (clone)
3459 ret = HIMAGELIST_QueryInterface(clone, riid, ppv);
3460 ImageList_Destroy(clone);
3463 return ret;
3466 static HRESULT WINAPI ImageListImpl_GetImageRect(IImageList2 *iface, int i,
3467 RECT *prc)
3469 HIMAGELIST imgl = impl_from_IImageList2(iface);
3470 IMAGEINFO info;
3472 if (!prc)
3473 return E_FAIL;
3475 if (!ImageList_GetImageInfo(imgl, i, &info))
3476 return E_FAIL;
3478 return CopyRect(prc, &info.rcImage) ? S_OK : E_FAIL;
3481 static HRESULT WINAPI ImageListImpl_GetIconSize(IImageList2 *iface, int *cx,
3482 int *cy)
3484 HIMAGELIST imgl = impl_from_IImageList2(iface);
3485 return ImageList_GetIconSize(imgl, cx, cy) ? S_OK : E_INVALIDARG;
3488 static HRESULT WINAPI ImageListImpl_SetIconSize(IImageList2 *iface, int cx,
3489 int cy)
3491 HIMAGELIST imgl = impl_from_IImageList2(iface);
3492 return ImageList_SetIconSize(imgl, cx, cy) ? S_OK : E_FAIL;
3495 static HRESULT WINAPI ImageListImpl_GetImageCount(IImageList2 *iface, int *pi)
3497 HIMAGELIST imgl = impl_from_IImageList2(iface);
3498 *pi = ImageList_GetImageCount(imgl);
3499 return S_OK;
3502 static HRESULT WINAPI ImageListImpl_SetImageCount(IImageList2 *iface, UINT count)
3504 HIMAGELIST imgl = impl_from_IImageList2(iface);
3505 return ImageList_SetImageCount(imgl, count) ? S_OK : E_FAIL;
3508 static HRESULT WINAPI ImageListImpl_SetBkColor(IImageList2 *iface, COLORREF clrBk,
3509 COLORREF *pclr)
3511 HIMAGELIST imgl = impl_from_IImageList2(iface);
3512 *pclr = ImageList_SetBkColor(imgl, clrBk);
3513 return S_OK;
3516 static HRESULT WINAPI ImageListImpl_GetBkColor(IImageList2 *iface, COLORREF *pclr)
3518 HIMAGELIST imgl = impl_from_IImageList2(iface);
3519 *pclr = ImageList_GetBkColor(imgl);
3520 return S_OK;
3523 static HRESULT WINAPI ImageListImpl_BeginDrag(IImageList2 *iface, int iTrack,
3524 int dxHotspot, int dyHotspot)
3526 HIMAGELIST imgl = impl_from_IImageList2(iface);
3527 return ImageList_BeginDrag(imgl, iTrack, dxHotspot, dyHotspot) ? S_OK : E_FAIL;
3530 static HRESULT WINAPI ImageListImpl_EndDrag(IImageList2 *iface)
3532 ImageList_EndDrag();
3533 return S_OK;
3536 static HRESULT WINAPI ImageListImpl_DragEnter(IImageList2 *iface, HWND hwndLock,
3537 int x, int y)
3539 return ImageList_DragEnter(hwndLock, x, y) ? S_OK : E_FAIL;
3542 static HRESULT WINAPI ImageListImpl_DragLeave(IImageList2 *iface, HWND hwndLock)
3544 return ImageList_DragLeave(hwndLock) ? S_OK : E_FAIL;
3547 static HRESULT WINAPI ImageListImpl_DragMove(IImageList2 *iface, int x, int y)
3549 return ImageList_DragMove(x, y) ? S_OK : E_FAIL;
3552 static HRESULT WINAPI ImageListImpl_SetDragCursorImage(IImageList2 *iface,
3553 IUnknown *punk, int iDrag, int dxHotspot, int dyHotspot)
3555 IImageList *iml2 = NULL;
3556 BOOL ret;
3558 if (!punk)
3559 return E_FAIL;
3561 /* TODO: Add test for IID_ImageList2 too */
3562 if (FAILED(IUnknown_QueryInterface(punk, &IID_IImageList,
3563 (void **) &iml2)))
3564 return E_FAIL;
3566 ret = ImageList_SetDragCursorImage((HIMAGELIST) iml2, iDrag, dxHotspot,
3567 dyHotspot);
3569 IImageList_Release(iml2);
3571 return ret ? S_OK : E_FAIL;
3574 static HRESULT WINAPI ImageListImpl_DragShowNolock(IImageList2 *iface, BOOL fShow)
3576 return ImageList_DragShowNolock(fShow) ? S_OK : E_FAIL;
3579 static HRESULT WINAPI ImageListImpl_GetDragImage(IImageList2 *iface, POINT *ppt,
3580 POINT *pptHotspot, REFIID riid, PVOID *ppv)
3582 HRESULT ret = E_FAIL;
3583 HIMAGELIST hNew;
3585 if (!ppv)
3586 return E_FAIL;
3588 hNew = ImageList_GetDragImage(ppt, pptHotspot);
3590 /* Get the interface for the new image list */
3591 if (hNew)
3593 IImageList *idrag = (IImageList*)hNew;
3595 ret = HIMAGELIST_QueryInterface(hNew, riid, ppv);
3596 IImageList_Release(idrag);
3599 return ret;
3602 static HRESULT WINAPI ImageListImpl_GetItemFlags(IImageList2 *iface, int i,
3603 DWORD *dwFlags)
3605 FIXME("STUB: %p %d %p\n", iface, i, dwFlags);
3606 return E_NOTIMPL;
3609 static HRESULT WINAPI ImageListImpl_GetOverlayImage(IImageList2 *iface, int iOverlay,
3610 int *piIndex)
3612 HIMAGELIST This = impl_from_IImageList2(iface);
3613 int i;
3615 if ((iOverlay < 0) || (iOverlay > This->cCurImage))
3616 return E_FAIL;
3618 for (i = 0; i < MAX_OVERLAYIMAGE; i++)
3620 if (This->nOvlIdx[i] == iOverlay)
3622 *piIndex = i + 1;
3623 return S_OK;
3627 return E_FAIL;
3630 static HRESULT WINAPI ImageListImpl_Resize(IImageList2 *iface, INT cx, INT cy)
3632 FIXME("(%p)->(%d %d): stub\n", iface, cx, cy);
3633 return E_NOTIMPL;
3636 static HRESULT WINAPI ImageListImpl_GetOriginalSize(IImageList2 *iface, INT image, DWORD flags, INT *cx, INT *cy)
3638 FIXME("(%p)->(%d %x %p %p): stub\n", iface, image, flags, cx, cy);
3639 return E_NOTIMPL;
3642 static HRESULT WINAPI ImageListImpl_SetOriginalSize(IImageList2 *iface, INT image, INT cx, INT cy)
3644 FIXME("(%p)->(%d %d %d): stub\n", iface, image, cx, cy);
3645 return E_NOTIMPL;
3648 static HRESULT WINAPI ImageListImpl_SetCallback(IImageList2 *iface, IUnknown *callback)
3650 FIXME("(%p)->(%p): stub\n", iface, callback);
3651 return E_NOTIMPL;
3654 static HRESULT WINAPI ImageListImpl_GetCallback(IImageList2 *iface, REFIID riid, void **ppv)
3656 FIXME("(%p)->(%s %p): stub\n", iface, debugstr_guid(riid), ppv);
3657 return E_NOTIMPL;
3660 static HRESULT WINAPI ImageListImpl_ForceImagePresent(IImageList2 *iface, INT image, DWORD flags)
3662 FIXME("(%p)->(%d %x): stub\n", iface, image, flags);
3663 return E_NOTIMPL;
3666 static HRESULT WINAPI ImageListImpl_DiscardImages(IImageList2 *iface, INT first_image, INT last_image, DWORD flags)
3668 FIXME("(%p)->(%d %d %x): stub\n", iface, first_image, last_image, flags);
3669 return E_NOTIMPL;
3672 static HRESULT WINAPI ImageListImpl_PreloadImages(IImageList2 *iface, IMAGELISTDRAWPARAMS *params)
3674 FIXME("(%p)->(%p): stub\n", iface, params);
3675 return E_NOTIMPL;
3678 static HRESULT WINAPI ImageListImpl_GetStatistics(IImageList2 *iface, IMAGELISTSTATS *stats)
3680 FIXME("(%p)->(%p): stub\n", iface, stats);
3681 return E_NOTIMPL;
3684 static HRESULT WINAPI ImageListImpl_Initialize(IImageList2 *iface, INT cx, INT cy, UINT flags, INT initial, INT grow)
3686 FIXME("(%p)->(%d %d %d %d %d): stub\n", iface, cx, cy, flags, initial, grow);
3687 return E_NOTIMPL;
3690 static HRESULT WINAPI ImageListImpl_Replace2(IImageList2 *iface, INT i, HBITMAP image, HBITMAP mask, IUnknown *unk, DWORD flags)
3692 FIXME("(%p)->(%d %p %p %p %x): stub\n", iface, i, image, mask, unk, flags);
3693 return E_NOTIMPL;
3696 static HRESULT WINAPI ImageListImpl_ReplaceFromImageList(IImageList2 *iface, INT i, IImageList *imagelist, INT src,
3697 IUnknown *unk, DWORD flags)
3699 FIXME("(%p)->(%d %p %d %p %x): stub\n", iface, i, imagelist, src, unk, flags);
3700 return E_NOTIMPL;
3703 static const IImageList2Vtbl ImageListImpl_Vtbl = {
3704 ImageListImpl_QueryInterface,
3705 ImageListImpl_AddRef,
3706 ImageListImpl_Release,
3707 ImageListImpl_Add,
3708 ImageListImpl_ReplaceIcon,
3709 ImageListImpl_SetOverlayImage,
3710 ImageListImpl_Replace,
3711 ImageListImpl_AddMasked,
3712 ImageListImpl_Draw,
3713 ImageListImpl_Remove,
3714 ImageListImpl_GetIcon,
3715 ImageListImpl_GetImageInfo,
3716 ImageListImpl_Copy,
3717 ImageListImpl_Merge,
3718 ImageListImpl_Clone,
3719 ImageListImpl_GetImageRect,
3720 ImageListImpl_GetIconSize,
3721 ImageListImpl_SetIconSize,
3722 ImageListImpl_GetImageCount,
3723 ImageListImpl_SetImageCount,
3724 ImageListImpl_SetBkColor,
3725 ImageListImpl_GetBkColor,
3726 ImageListImpl_BeginDrag,
3727 ImageListImpl_EndDrag,
3728 ImageListImpl_DragEnter,
3729 ImageListImpl_DragLeave,
3730 ImageListImpl_DragMove,
3731 ImageListImpl_SetDragCursorImage,
3732 ImageListImpl_DragShowNolock,
3733 ImageListImpl_GetDragImage,
3734 ImageListImpl_GetItemFlags,
3735 ImageListImpl_GetOverlayImage,
3736 ImageListImpl_Resize,
3737 ImageListImpl_GetOriginalSize,
3738 ImageListImpl_SetOriginalSize,
3739 ImageListImpl_SetCallback,
3740 ImageListImpl_GetCallback,
3741 ImageListImpl_ForceImagePresent,
3742 ImageListImpl_DiscardImages,
3743 ImageListImpl_PreloadImages,
3744 ImageListImpl_GetStatistics,
3745 ImageListImpl_Initialize,
3746 ImageListImpl_Replace2,
3747 ImageListImpl_ReplaceFromImageList
3750 static BOOL is_valid(HIMAGELIST himl)
3752 BOOL valid;
3753 __TRY
3755 valid = himl && himl->IImageList2_iface.lpVtbl == &ImageListImpl_Vtbl;
3757 __EXCEPT_PAGE_FAULT
3759 valid = FALSE;
3761 __ENDTRY
3762 return valid;
3765 /*************************************************************************
3766 * HIMAGELIST_QueryInterface [COMCTL32.@]
3768 * Returns a pointer to an IImageList or IImageList2 object for the given
3769 * HIMAGELIST.
3771 * PARAMS
3772 * himl [I] Image list handle.
3773 * riid [I] Identifier of the requested interface.
3774 * ppv [O] Returns the address of the pointer requested, or NULL.
3776 * RETURNS
3777 * Success: S_OK.
3778 * Failure: Error value.
3780 HRESULT WINAPI
3781 HIMAGELIST_QueryInterface (HIMAGELIST himl, REFIID riid, void **ppv)
3783 TRACE("(%p,%s,%p)\n", himl, debugstr_guid(riid), ppv);
3784 return IImageList2_QueryInterface((IImageList2 *) himl, riid, ppv);
3787 static HRESULT ImageListImpl_CreateInstance(const IUnknown *pUnkOuter, REFIID iid, void** ppv)
3789 HIMAGELIST This;
3790 HRESULT ret;
3792 TRACE("(%p,%s,%p)\n", pUnkOuter, debugstr_guid(iid), ppv);
3794 *ppv = NULL;
3796 if (pUnkOuter) return CLASS_E_NOAGGREGATION;
3798 This = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct _IMAGELIST));
3799 if (!This) return E_OUTOFMEMORY;
3801 This->IImageList2_iface.lpVtbl = &ImageListImpl_Vtbl;
3802 This->ref = 1;
3804 ret = IImageList2_QueryInterface(&This->IImageList2_iface, iid, ppv);
3805 IImageList2_Release(&This->IImageList2_iface);
3807 return ret;