comctl32/imagelist: Update to IImageList2.
[wine.git] / dlls / comctl32 / imagelist.c
blob4e0a58d1cda59d34cc01776d4cbe7d4cf723cb8f
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 inline 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;
603 TRACE("(himlTrack=%p iTrack=%d dx=%d dy=%d)\n", himlTrack, iTrack,
604 dxHotspot, dyHotspot);
606 if (!is_valid(himlTrack))
607 return FALSE;
609 if (InternalDrag.himl)
610 ImageList_EndDrag ();
612 cx = himlTrack->cx;
613 cy = himlTrack->cy;
615 InternalDrag.himlNoCursor = InternalDrag.himl = ImageList_Create (cx, cy, himlTrack->flags, 1, 1);
616 if (InternalDrag.himl == NULL) {
617 WARN("Error creating drag image list!\n");
618 return FALSE;
621 InternalDrag.dxHotspot = dxHotspot;
622 InternalDrag.dyHotspot = dyHotspot;
624 /* copy image */
625 BitBlt (InternalDrag.himl->hdcImage, 0, 0, cx, cy, himlTrack->hdcImage, iTrack * cx, 0, SRCCOPY);
627 /* copy mask */
628 BitBlt (InternalDrag.himl->hdcMask, 0, 0, cx, cy, himlTrack->hdcMask, iTrack * cx, 0, SRCCOPY);
630 InternalDrag.himl->cCurImage = 1;
632 return TRUE;
636 /*************************************************************************
637 * ImageList_Copy [COMCTL32.@]
639 * Copies an image of the source image list to an image of the
640 * destination image list. Images can be copied or swapped.
642 * PARAMS
643 * himlDst [I] handle to the destination image list
644 * iDst [I] destination image index.
645 * himlSrc [I] handle to the source image list
646 * iSrc [I] source image index
647 * uFlags [I] flags for the copy operation
649 * RETURNS
650 * Success: TRUE
651 * Failure: FALSE
653 * NOTES
654 * Copying from one image list to another is possible. The original
655 * implementation just copies or swaps within one image list.
656 * Could this feature become a bug??? ;-)
659 BOOL WINAPI
660 ImageList_Copy (HIMAGELIST himlDst, INT iDst, HIMAGELIST himlSrc,
661 INT iSrc, UINT uFlags)
663 POINT ptSrc, ptDst;
665 TRACE("himlDst=%p iDst=%d himlSrc=%p iSrc=%d\n", himlDst, iDst, himlSrc, iSrc);
667 if (!is_valid(himlSrc) || !is_valid(himlDst))
668 return FALSE;
669 if ((iDst < 0) || (iDst >= himlDst->cCurImage))
670 return FALSE;
671 if ((iSrc < 0) || (iSrc >= himlSrc->cCurImage))
672 return FALSE;
674 imagelist_point_from_index( himlDst, iDst, &ptDst );
675 imagelist_point_from_index( himlSrc, iSrc, &ptSrc );
677 if (uFlags & ILCF_SWAP) {
678 /* swap */
679 HDC hdcBmp;
680 HBITMAP hbmTempImage, hbmTempMask;
682 hdcBmp = CreateCompatibleDC (0);
684 /* create temporary bitmaps */
685 hbmTempImage = CreateBitmap (himlSrc->cx, himlSrc->cy, 1,
686 himlSrc->uBitsPixel, NULL);
687 hbmTempMask = CreateBitmap (himlSrc->cx, himlSrc->cy, 1,
688 1, NULL);
690 /* copy (and stretch) destination to temporary bitmaps.(save) */
691 /* image */
692 SelectObject (hdcBmp, hbmTempImage);
693 StretchBlt (hdcBmp, 0, 0, himlSrc->cx, himlSrc->cy,
694 himlDst->hdcImage, ptDst.x, ptDst.y, himlDst->cx, himlDst->cy,
695 SRCCOPY);
696 /* mask */
697 SelectObject (hdcBmp, hbmTempMask);
698 StretchBlt (hdcBmp, 0, 0, himlSrc->cx, himlSrc->cy,
699 himlDst->hdcMask, ptDst.x, ptDst.y, himlDst->cx, himlDst->cy,
700 SRCCOPY);
702 /* copy (and stretch) source to destination */
703 /* image */
704 StretchBlt (himlDst->hdcImage, ptDst.x, ptDst.y, himlDst->cx, himlDst->cy,
705 himlSrc->hdcImage, ptSrc.x, ptSrc.y, himlSrc->cx, himlSrc->cy,
706 SRCCOPY);
707 /* mask */
708 StretchBlt (himlDst->hdcMask, ptDst.x, ptDst.y, himlDst->cx, himlDst->cy,
709 himlSrc->hdcMask, ptSrc.x, ptSrc.y, himlSrc->cx, himlSrc->cy,
710 SRCCOPY);
712 /* copy (without stretching) temporary bitmaps to source (restore) */
713 /* mask */
714 BitBlt (himlSrc->hdcMask, ptSrc.x, ptSrc.y, himlSrc->cx, himlSrc->cy,
715 hdcBmp, 0, 0, SRCCOPY);
717 /* image */
718 BitBlt (himlSrc->hdcImage, ptSrc.x, ptSrc.y, himlSrc->cx, himlSrc->cy,
719 hdcBmp, 0, 0, SRCCOPY);
720 /* delete temporary bitmaps */
721 DeleteObject (hbmTempMask);
722 DeleteObject (hbmTempImage);
723 DeleteDC(hdcBmp);
725 else {
726 /* copy image */
727 StretchBlt (himlDst->hdcImage, ptDst.x, ptDst.y, himlDst->cx, himlDst->cy,
728 himlSrc->hdcImage, ptSrc.x, ptSrc.y, himlSrc->cx, himlSrc->cy,
729 SRCCOPY);
731 /* copy mask */
732 StretchBlt (himlDst->hdcMask, ptDst.x, ptDst.y, himlDst->cx, himlDst->cy,
733 himlSrc->hdcMask, ptSrc.x, ptSrc.y, himlSrc->cx, himlSrc->cy,
734 SRCCOPY);
737 return TRUE;
741 /*************************************************************************
742 * ImageList_Create [COMCTL32.@]
744 * Creates a new image list.
746 * PARAMS
747 * cx [I] image height
748 * cy [I] image width
749 * flags [I] creation flags
750 * cInitial [I] initial number of images in the image list
751 * cGrow [I] number of images by which image list grows
753 * RETURNS
754 * Success: Handle to the created image list
755 * Failure: NULL
757 HIMAGELIST WINAPI
758 ImageList_Create (INT cx, INT cy, UINT flags,
759 INT cInitial, INT cGrow)
761 HIMAGELIST himl;
762 INT nCount;
763 HBITMAP hbmTemp;
764 UINT ilc = (flags & 0xFE);
765 static const WORD aBitBlend25[] =
766 {0xAA, 0x00, 0x55, 0x00, 0xAA, 0x00, 0x55, 0x00};
768 static const WORD aBitBlend50[] =
769 {0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA};
771 TRACE("(%d %d 0x%x %d %d)\n", cx, cy, flags, cInitial, cGrow);
773 if (cx <= 0 || cy <= 0) return NULL;
775 /* Create the IImageList interface for the image list */
776 if (FAILED(ImageListImpl_CreateInstance(NULL, &IID_IImageList, (void **)&himl)))
777 return NULL;
779 cGrow = (WORD)((max( cGrow, 1 ) + 3) & ~3);
781 if (cGrow > 256)
783 /* Windows doesn't limit the size here, but X11 doesn't let us allocate such huge bitmaps */
784 WARN( "grow %d too large, limiting to 256\n", cGrow );
785 cGrow = 256;
788 himl->cx = cx;
789 himl->cy = cy;
790 himl->flags = flags;
791 himl->cMaxImage = cInitial + 1;
792 himl->cInitial = cInitial;
793 himl->cGrow = cGrow;
794 himl->clrFg = CLR_DEFAULT;
795 himl->clrBk = CLR_NONE;
797 /* initialize overlay mask indices */
798 for (nCount = 0; nCount < MAX_OVERLAYIMAGE; nCount++)
799 himl->nOvlIdx[nCount] = -1;
801 /* Create Image & Mask DCs */
802 himl->hdcImage = CreateCompatibleDC (0);
803 if (!himl->hdcImage)
804 goto cleanup;
805 if (himl->flags & ILC_MASK){
806 himl->hdcMask = CreateCompatibleDC(0);
807 if (!himl->hdcMask)
808 goto cleanup;
811 /* Default to ILC_COLOR4 if none of the ILC_COLOR* flags are specified */
812 if (ilc == ILC_COLOR)
814 ilc = ILC_COLOR4;
815 himl->flags |= ILC_COLOR4;
818 if (ilc >= ILC_COLOR4 && ilc <= ILC_COLOR32)
819 himl->uBitsPixel = ilc;
820 else
821 himl->uBitsPixel = (UINT)GetDeviceCaps (himl->hdcImage, BITSPIXEL);
823 if (himl->cMaxImage > 0) {
824 himl->hbmImage = ImageList_CreateImage(himl->hdcImage, himl, himl->cMaxImage);
825 SelectObject(himl->hdcImage, himl->hbmImage);
826 } else
827 himl->hbmImage = 0;
829 if ((himl->cMaxImage > 0) && (himl->flags & ILC_MASK)) {
830 SIZE sz;
832 imagelist_get_bitmap_size(himl, himl->cMaxImage, &sz);
833 himl->hbmMask = CreateBitmap (sz.cx, sz.cy, 1, 1, NULL);
834 if (himl->hbmMask == 0) {
835 ERR("Error creating mask bitmap!\n");
836 goto cleanup;
838 SelectObject(himl->hdcMask, himl->hbmMask);
840 else
841 himl->hbmMask = 0;
843 if (ilc == ILC_COLOR32)
844 himl->has_alpha = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, himl->cMaxImage );
845 else
846 himl->has_alpha = NULL;
848 /* create blending brushes */
849 hbmTemp = CreateBitmap (8, 8, 1, 1, aBitBlend25);
850 himl->hbrBlend25 = CreatePatternBrush (hbmTemp);
851 DeleteObject (hbmTemp);
853 hbmTemp = CreateBitmap (8, 8, 1, 1, aBitBlend50);
854 himl->hbrBlend50 = CreatePatternBrush (hbmTemp);
855 DeleteObject (hbmTemp);
857 TRACE("created imagelist %p\n", himl);
858 return himl;
860 cleanup:
861 ImageList_Destroy(himl);
862 return NULL;
866 /*************************************************************************
867 * ImageList_Destroy [COMCTL32.@]
869 * Destroys an image list.
871 * PARAMS
872 * himl [I] handle to image list
874 * RETURNS
875 * Success: TRUE
876 * Failure: FALSE
879 BOOL WINAPI
880 ImageList_Destroy (HIMAGELIST himl)
882 if (!is_valid(himl))
883 return FALSE;
885 IImageList_Release((IImageList *) himl);
886 return TRUE;
890 /*************************************************************************
891 * ImageList_DragEnter [COMCTL32.@]
893 * Locks window update and displays the drag image at the given position.
895 * PARAMS
896 * hwndLock [I] handle of the window that owns the drag image.
897 * x [I] X position of the drag image.
898 * y [I] Y position of the drag image.
900 * RETURNS
901 * Success: TRUE
902 * Failure: FALSE
904 * NOTES
905 * The position of the drag image is relative to the window, not
906 * the client area.
909 BOOL WINAPI
910 ImageList_DragEnter (HWND hwndLock, INT x, INT y)
912 TRACE("(hwnd=%p x=%d y=%d)\n", hwndLock, x, y);
914 if (!is_valid(InternalDrag.himl))
915 return FALSE;
917 if (hwndLock)
918 InternalDrag.hwnd = hwndLock;
919 else
920 InternalDrag.hwnd = GetDesktopWindow ();
922 InternalDrag.x = x;
923 InternalDrag.y = y;
925 /* draw the drag image and save the background */
926 if (!ImageList_DragShowNolock(TRUE)) {
927 return FALSE;
930 return TRUE;
934 /*************************************************************************
935 * ImageList_DragLeave [COMCTL32.@]
937 * Unlocks window update and hides the drag image.
939 * PARAMS
940 * hwndLock [I] handle of the window that owns the drag image.
942 * RETURNS
943 * Success: TRUE
944 * Failure: FALSE
947 BOOL WINAPI
948 ImageList_DragLeave (HWND hwndLock)
950 /* As we don't save drag info in the window this can lead to problems if
951 an app does not supply the same window as DragEnter */
952 /* if (hwndLock)
953 InternalDrag.hwnd = hwndLock;
954 else
955 InternalDrag.hwnd = GetDesktopWindow (); */
956 if(!hwndLock)
957 hwndLock = GetDesktopWindow();
958 if(InternalDrag.hwnd != hwndLock)
959 FIXME("DragLeave hWnd != DragEnter hWnd\n");
961 ImageList_DragShowNolock (FALSE);
963 return TRUE;
967 /*************************************************************************
968 * ImageList_InternalDragDraw [Internal]
970 * Draws the drag image.
972 * PARAMS
973 * hdc [I] device context to draw into.
974 * x [I] X position of the drag image.
975 * y [I] Y position of the drag image.
977 * RETURNS
978 * Success: TRUE
979 * Failure: FALSE
981 * NOTES
982 * The position of the drag image is relative to the window, not
983 * the client area.
987 static inline void
988 ImageList_InternalDragDraw (HDC hdc, INT x, INT y)
990 IMAGELISTDRAWPARAMS imldp;
992 ZeroMemory (&imldp, sizeof(imldp));
993 imldp.cbSize = sizeof(imldp);
994 imldp.himl = InternalDrag.himl;
995 imldp.i = 0;
996 imldp.hdcDst = hdc,
997 imldp.x = x;
998 imldp.y = y;
999 imldp.rgbBk = CLR_DEFAULT;
1000 imldp.rgbFg = CLR_DEFAULT;
1001 imldp.fStyle = ILD_NORMAL;
1002 imldp.fState = ILS_ALPHA;
1003 imldp.Frame = 192;
1004 ImageList_DrawIndirect (&imldp);
1007 /*************************************************************************
1008 * ImageList_DragMove [COMCTL32.@]
1010 * Moves the drag image.
1012 * PARAMS
1013 * x [I] X position of the drag image.
1014 * y [I] Y position of the drag image.
1016 * RETURNS
1017 * Success: TRUE
1018 * Failure: FALSE
1020 * NOTES
1021 * The position of the drag image is relative to the window, not
1022 * the client area.
1025 BOOL WINAPI
1026 ImageList_DragMove (INT x, INT y)
1028 TRACE("(x=%d y=%d)\n", x, y);
1030 if (!is_valid(InternalDrag.himl))
1031 return FALSE;
1033 /* draw/update the drag image */
1034 if (InternalDrag.bShow) {
1035 HDC hdcDrag;
1036 HDC hdcOffScreen;
1037 HDC hdcBg;
1038 HBITMAP hbmOffScreen;
1039 INT origNewX, origNewY;
1040 INT origOldX, origOldY;
1041 INT origRegX, origRegY;
1042 INT sizeRegX, sizeRegY;
1045 /* calculate the update region */
1046 origNewX = x - InternalDrag.dxHotspot;
1047 origNewY = y - InternalDrag.dyHotspot;
1048 origOldX = InternalDrag.x - InternalDrag.dxHotspot;
1049 origOldY = InternalDrag.y - InternalDrag.dyHotspot;
1050 origRegX = min(origNewX, origOldX);
1051 origRegY = min(origNewY, origOldY);
1052 sizeRegX = InternalDrag.himl->cx + abs(x - InternalDrag.x);
1053 sizeRegY = InternalDrag.himl->cy + abs(y - InternalDrag.y);
1055 hdcDrag = GetDCEx(InternalDrag.hwnd, 0,
1056 DCX_WINDOW | DCX_CACHE | DCX_LOCKWINDOWUPDATE);
1057 hdcOffScreen = CreateCompatibleDC(hdcDrag);
1058 hdcBg = CreateCompatibleDC(hdcDrag);
1060 hbmOffScreen = CreateCompatibleBitmap(hdcDrag, sizeRegX, sizeRegY);
1061 SelectObject(hdcOffScreen, hbmOffScreen);
1062 SelectObject(hdcBg, InternalDrag.hbmBg);
1064 /* get the actual background of the update region */
1065 BitBlt(hdcOffScreen, 0, 0, sizeRegX, sizeRegY, hdcDrag,
1066 origRegX, origRegY, SRCCOPY);
1067 /* erase the old image */
1068 BitBlt(hdcOffScreen, origOldX - origRegX, origOldY - origRegY,
1069 InternalDrag.himl->cx, InternalDrag.himl->cy, hdcBg, 0, 0,
1070 SRCCOPY);
1071 /* save the background */
1072 BitBlt(hdcBg, 0, 0, InternalDrag.himl->cx, InternalDrag.himl->cy,
1073 hdcOffScreen, origNewX - origRegX, origNewY - origRegY, SRCCOPY);
1074 /* draw the image */
1075 ImageList_InternalDragDraw(hdcOffScreen, origNewX - origRegX,
1076 origNewY - origRegY);
1077 /* draw the update region to the screen */
1078 BitBlt(hdcDrag, origRegX, origRegY, sizeRegX, sizeRegY,
1079 hdcOffScreen, 0, 0, SRCCOPY);
1081 DeleteDC(hdcBg);
1082 DeleteDC(hdcOffScreen);
1083 DeleteObject(hbmOffScreen);
1084 ReleaseDC(InternalDrag.hwnd, hdcDrag);
1087 /* update the image position */
1088 InternalDrag.x = x;
1089 InternalDrag.y = y;
1091 return TRUE;
1095 /*************************************************************************
1096 * ImageList_DragShowNolock [COMCTL32.@]
1098 * Shows or hides the drag image.
1100 * PARAMS
1101 * bShow [I] TRUE shows the drag image, FALSE hides it.
1103 * RETURNS
1104 * Success: TRUE
1105 * Failure: FALSE
1108 BOOL WINAPI
1109 ImageList_DragShowNolock (BOOL bShow)
1111 HDC hdcDrag;
1112 HDC hdcBg;
1113 INT x, y;
1115 if (!is_valid(InternalDrag.himl))
1116 return FALSE;
1118 TRACE("bShow=0x%X!\n", bShow);
1120 /* DragImage is already visible/hidden */
1121 if ((InternalDrag.bShow && bShow) || (!InternalDrag.bShow && !bShow)) {
1122 return FALSE;
1125 /* position of the origin of the DragImage */
1126 x = InternalDrag.x - InternalDrag.dxHotspot;
1127 y = InternalDrag.y - InternalDrag.dyHotspot;
1129 hdcDrag = GetDCEx (InternalDrag.hwnd, 0,
1130 DCX_WINDOW | DCX_CACHE | DCX_LOCKWINDOWUPDATE);
1131 if (!hdcDrag) {
1132 return FALSE;
1135 hdcBg = CreateCompatibleDC(hdcDrag);
1136 if (!InternalDrag.hbmBg) {
1137 InternalDrag.hbmBg = CreateCompatibleBitmap(hdcDrag,
1138 InternalDrag.himl->cx, InternalDrag.himl->cy);
1140 SelectObject(hdcBg, InternalDrag.hbmBg);
1142 if (bShow) {
1143 /* save the background */
1144 BitBlt(hdcBg, 0, 0, InternalDrag.himl->cx, InternalDrag.himl->cy,
1145 hdcDrag, x, y, SRCCOPY);
1146 /* show the image */
1147 ImageList_InternalDragDraw(hdcDrag, x, y);
1148 } else {
1149 /* hide the image */
1150 BitBlt(hdcDrag, x, y, InternalDrag.himl->cx, InternalDrag.himl->cy,
1151 hdcBg, 0, 0, SRCCOPY);
1154 InternalDrag.bShow = !InternalDrag.bShow;
1156 DeleteDC(hdcBg);
1157 ReleaseDC (InternalDrag.hwnd, hdcDrag);
1158 return TRUE;
1162 /*************************************************************************
1163 * ImageList_Draw [COMCTL32.@]
1165 * Draws an image.
1167 * PARAMS
1168 * himl [I] handle to image list
1169 * i [I] image index
1170 * hdc [I] handle to device context
1171 * x [I] x position
1172 * y [I] y position
1173 * fStyle [I] drawing flags
1175 * RETURNS
1176 * Success: TRUE
1177 * Failure: FALSE
1179 * SEE
1180 * ImageList_DrawEx.
1183 BOOL WINAPI
1184 ImageList_Draw (HIMAGELIST himl, INT i, HDC hdc, INT x, INT y, UINT fStyle)
1186 return ImageList_DrawEx (himl, i, hdc, x, y, 0, 0,
1187 CLR_DEFAULT, CLR_DEFAULT, fStyle);
1191 /*************************************************************************
1192 * ImageList_DrawEx [COMCTL32.@]
1194 * Draws an image and allows using extended drawing features.
1196 * PARAMS
1197 * himl [I] handle to image list
1198 * i [I] image index
1199 * hdc [I] handle to device context
1200 * x [I] X position
1201 * y [I] Y position
1202 * dx [I] X offset
1203 * dy [I] Y offset
1204 * rgbBk [I] background color
1205 * rgbFg [I] foreground color
1206 * fStyle [I] drawing flags
1208 * RETURNS
1209 * Success: TRUE
1210 * Failure: FALSE
1212 * NOTES
1213 * Calls ImageList_DrawIndirect.
1215 * SEE
1216 * ImageList_DrawIndirect.
1219 BOOL WINAPI
1220 ImageList_DrawEx (HIMAGELIST himl, INT i, HDC hdc, INT x, INT y,
1221 INT dx, INT dy, COLORREF rgbBk, COLORREF rgbFg,
1222 UINT fStyle)
1224 IMAGELISTDRAWPARAMS imldp;
1226 ZeroMemory (&imldp, sizeof(imldp));
1227 imldp.cbSize = sizeof(imldp);
1228 imldp.himl = himl;
1229 imldp.i = i;
1230 imldp.hdcDst = hdc,
1231 imldp.x = x;
1232 imldp.y = y;
1233 imldp.cx = dx;
1234 imldp.cy = dy;
1235 imldp.rgbBk = rgbBk;
1236 imldp.rgbFg = rgbFg;
1237 imldp.fStyle = fStyle;
1239 return ImageList_DrawIndirect (&imldp);
1243 static BOOL alpha_blend_image( HIMAGELIST himl, HDC dest_dc, int dest_x, int dest_y,
1244 int src_x, int src_y, int cx, int cy, BLENDFUNCTION func,
1245 UINT style, COLORREF blend_col )
1247 BOOL ret = FALSE;
1248 HDC hdc;
1249 HBITMAP bmp = 0, mask = 0;
1250 BITMAPINFO *info;
1251 void *bits, *mask_bits;
1252 unsigned int *ptr;
1253 int i, j;
1255 if (!(hdc = CreateCompatibleDC( 0 ))) return FALSE;
1256 if (!(info = HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET( BITMAPINFO, bmiColors[256] )))) goto done;
1257 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1258 info->bmiHeader.biWidth = cx;
1259 info->bmiHeader.biHeight = cy;
1260 info->bmiHeader.biPlanes = 1;
1261 info->bmiHeader.biBitCount = 32;
1262 info->bmiHeader.biCompression = BI_RGB;
1263 info->bmiHeader.biSizeImage = cx * cy * 4;
1264 info->bmiHeader.biXPelsPerMeter = 0;
1265 info->bmiHeader.biYPelsPerMeter = 0;
1266 info->bmiHeader.biClrUsed = 0;
1267 info->bmiHeader.biClrImportant = 0;
1268 if (!(bmp = CreateDIBSection( himl->hdcImage, info, DIB_RGB_COLORS, &bits, 0, 0 ))) goto done;
1269 SelectObject( hdc, bmp );
1270 BitBlt( hdc, 0, 0, cx, cy, himl->hdcImage, src_x, src_y, SRCCOPY );
1272 if (blend_col != CLR_NONE)
1274 BYTE r = GetRValue( blend_col );
1275 BYTE g = GetGValue( blend_col );
1276 BYTE b = GetBValue( blend_col );
1278 if (style & ILD_BLEND25)
1280 for (i = 0, ptr = bits; i < cx * cy; i++, ptr++)
1281 *ptr = ((*ptr & 0xff000000) |
1282 ((((*ptr & 0x00ff0000) * 3 + (r << 16)) / 4) & 0x00ff0000) |
1283 ((((*ptr & 0x0000ff00) * 3 + (g << 8)) / 4) & 0x0000ff00) |
1284 ((((*ptr & 0x000000ff) * 3 + (b << 0)) / 4) & 0x000000ff));
1286 else if (style & ILD_BLEND50)
1288 for (i = 0, ptr = bits; i < cx * cy; i++, ptr++)
1289 *ptr = ((*ptr & 0xff000000) |
1290 ((((*ptr & 0x00ff0000) + (r << 16)) / 2) & 0x00ff0000) |
1291 ((((*ptr & 0x0000ff00) + (g << 8)) / 2) & 0x0000ff00) |
1292 ((((*ptr & 0x000000ff) + (b << 0)) / 2) & 0x000000ff));
1296 if (himl->has_alpha) /* we already have an alpha channel in this case */
1298 /* pre-multiply by the alpha channel */
1299 for (i = 0, ptr = bits; i < cx * cy; i++, ptr++)
1301 DWORD alpha = *ptr >> 24;
1302 *ptr = ((*ptr & 0xff000000) |
1303 (((*ptr & 0x00ff0000) * alpha / 255) & 0x00ff0000) |
1304 (((*ptr & 0x0000ff00) * alpha / 255) & 0x0000ff00) |
1305 (((*ptr & 0x000000ff) * alpha / 255)));
1308 else if (himl->hbmMask)
1310 unsigned int width_bytes = (cx + 31) / 32 * 4;
1311 /* generate alpha channel from the mask */
1312 info->bmiHeader.biBitCount = 1;
1313 info->bmiHeader.biSizeImage = width_bytes * cy;
1314 info->bmiColors[0].rgbRed = 0;
1315 info->bmiColors[0].rgbGreen = 0;
1316 info->bmiColors[0].rgbBlue = 0;
1317 info->bmiColors[0].rgbReserved = 0;
1318 info->bmiColors[1].rgbRed = 0xff;
1319 info->bmiColors[1].rgbGreen = 0xff;
1320 info->bmiColors[1].rgbBlue = 0xff;
1321 info->bmiColors[1].rgbReserved = 0;
1322 if (!(mask = CreateDIBSection( himl->hdcMask, info, DIB_RGB_COLORS, &mask_bits, 0, 0 )))
1323 goto done;
1324 SelectObject( hdc, mask );
1325 BitBlt( hdc, 0, 0, cx, cy, himl->hdcMask, src_x, src_y, SRCCOPY );
1326 SelectObject( hdc, bmp );
1327 for (i = 0, ptr = bits; i < cy; i++)
1328 for (j = 0; j < cx; j++, ptr++)
1329 if ((((BYTE *)mask_bits)[i * width_bytes + j / 8] << (j % 8)) & 0x80) *ptr = 0;
1330 else *ptr |= 0xff000000;
1333 ret = GdiAlphaBlend( dest_dc, dest_x, dest_y, cx, cy, hdc, 0, 0, cx, cy, func );
1335 done:
1336 DeleteDC( hdc );
1337 if (bmp) DeleteObject( bmp );
1338 if (mask) DeleteObject( mask );
1339 HeapFree( GetProcessHeap(), 0, info );
1340 return ret;
1343 /*************************************************************************
1344 * ImageList_DrawIndirect [COMCTL32.@]
1346 * Draws an image using various parameters specified in pimldp.
1348 * PARAMS
1349 * pimldp [I] pointer to IMAGELISTDRAWPARAMS structure.
1351 * RETURNS
1352 * Success: TRUE
1353 * Failure: FALSE
1356 BOOL WINAPI
1357 ImageList_DrawIndirect (IMAGELISTDRAWPARAMS *pimldp)
1359 INT cx, cy, nOvlIdx;
1360 DWORD fState, dwRop;
1361 UINT fStyle;
1362 COLORREF oldImageBk, oldImageFg;
1363 HDC hImageDC, hImageListDC, hMaskListDC;
1364 HBITMAP hImageBmp, hOldImageBmp, hBlendMaskBmp;
1365 BOOL bIsTransparent, bBlend, bResult = FALSE, bMask;
1366 HIMAGELIST himl;
1367 HBRUSH hOldBrush;
1368 POINT pt;
1369 BOOL has_alpha;
1371 if (!pimldp || !(himl = pimldp->himl)) return FALSE;
1372 if (!is_valid(himl)) return FALSE;
1373 if ((pimldp->i < 0) || (pimldp->i >= himl->cCurImage)) return FALSE;
1375 imagelist_point_from_index( himl, pimldp->i, &pt );
1376 pt.x += pimldp->xBitmap;
1377 pt.y += pimldp->yBitmap;
1379 fState = pimldp->cbSize < sizeof(IMAGELISTDRAWPARAMS) ? ILS_NORMAL : pimldp->fState;
1380 fStyle = pimldp->fStyle & ~ILD_OVERLAYMASK;
1381 cx = (pimldp->cx == 0) ? himl->cx : pimldp->cx;
1382 cy = (pimldp->cy == 0) ? himl->cy : pimldp->cy;
1384 bIsTransparent = (fStyle & ILD_TRANSPARENT);
1385 if( pimldp->rgbBk == CLR_NONE )
1386 bIsTransparent = TRUE;
1387 if( ( pimldp->rgbBk == CLR_DEFAULT ) && ( himl->clrBk == CLR_NONE ) )
1388 bIsTransparent = TRUE;
1389 bMask = (himl->flags & ILC_MASK) && (fStyle & ILD_MASK) ;
1390 bBlend = (fStyle & (ILD_BLEND25 | ILD_BLEND50) ) && !bMask;
1392 TRACE("himl(%p) hbmMask(%p) iImage(%d) x(%d) y(%d) cx(%d) cy(%d)\n",
1393 himl, himl->hbmMask, pimldp->i, pimldp->x, pimldp->y, cx, cy);
1395 /* we will use these DCs to access the images and masks in the ImageList */
1396 hImageListDC = himl->hdcImage;
1397 hMaskListDC = himl->hdcMask;
1399 /* these will accumulate the image and mask for the image we're drawing */
1400 hImageDC = CreateCompatibleDC( pimldp->hdcDst );
1401 hImageBmp = CreateCompatibleBitmap( pimldp->hdcDst, cx, cy );
1402 hBlendMaskBmp = bBlend ? CreateBitmap(cx, cy, 1, 1, NULL) : 0;
1404 /* Create a compatible DC. */
1405 if (!hImageListDC || !hImageDC || !hImageBmp ||
1406 (bBlend && !hBlendMaskBmp) || (himl->hbmMask && !hMaskListDC))
1407 goto cleanup;
1409 hOldImageBmp = SelectObject(hImageDC, hImageBmp);
1412 * To obtain a transparent look, background color should be set
1413 * to white and foreground color to black when blitting the
1414 * monochrome mask.
1416 oldImageFg = SetTextColor( hImageDC, RGB( 0, 0, 0 ) );
1417 oldImageBk = SetBkColor( hImageDC, RGB( 0xff, 0xff, 0xff ) );
1419 has_alpha = (himl->has_alpha && himl->has_alpha[pimldp->i]);
1420 if (!bMask && (has_alpha || (fState & ILS_ALPHA)))
1422 COLORREF colour, blend_col = CLR_NONE;
1423 BLENDFUNCTION func;
1425 if (bBlend)
1427 blend_col = pimldp->rgbFg;
1428 if (blend_col == CLR_DEFAULT) blend_col = GetSysColor( COLOR_HIGHLIGHT );
1429 else if (blend_col == CLR_NONE) blend_col = GetTextColor( pimldp->hdcDst );
1432 func.BlendOp = AC_SRC_OVER;
1433 func.BlendFlags = 0;
1434 func.SourceConstantAlpha = (fState & ILS_ALPHA) ? pimldp->Frame : 255;
1435 func.AlphaFormat = AC_SRC_ALPHA;
1437 if (bIsTransparent)
1439 bResult = alpha_blend_image( himl, pimldp->hdcDst, pimldp->x, pimldp->y,
1440 pt.x, pt.y, cx, cy, func, fStyle, blend_col );
1441 goto end;
1443 colour = pimldp->rgbBk;
1444 if (colour == CLR_DEFAULT) colour = himl->clrBk;
1445 if (colour == CLR_NONE) colour = GetBkColor( pimldp->hdcDst );
1447 hOldBrush = SelectObject (hImageDC, CreateSolidBrush (colour));
1448 PatBlt( hImageDC, 0, 0, cx, cy, PATCOPY );
1449 alpha_blend_image( himl, hImageDC, 0, 0, pt.x, pt.y, cx, cy, func, fStyle, blend_col );
1450 DeleteObject (SelectObject (hImageDC, hOldBrush));
1451 bResult = BitBlt( pimldp->hdcDst, pimldp->x, pimldp->y, cx, cy, hImageDC, 0, 0, SRCCOPY );
1452 goto end;
1456 * Draw the initial image
1458 if( bMask ) {
1459 if (himl->hbmMask) {
1460 hOldBrush = SelectObject (hImageDC, CreateSolidBrush (GetTextColor(pimldp->hdcDst)));
1461 PatBlt( hImageDC, 0, 0, cx, cy, PATCOPY );
1462 BitBlt(hImageDC, 0, 0, cx, cy, hMaskListDC, pt.x, pt.y, SRCPAINT);
1463 DeleteObject (SelectObject (hImageDC, hOldBrush));
1464 if( bIsTransparent )
1466 BitBlt ( pimldp->hdcDst, pimldp->x, pimldp->y, cx, cy, hImageDC, 0, 0, SRCAND);
1467 bResult = TRUE;
1468 goto end;
1470 } else {
1471 hOldBrush = SelectObject (hImageDC, GetStockObject(BLACK_BRUSH));
1472 PatBlt( hImageDC, 0, 0, cx, cy, PATCOPY);
1473 SelectObject(hImageDC, hOldBrush);
1475 } else {
1476 /* blend the image with the needed solid background */
1477 COLORREF colour = RGB(0,0,0);
1479 if( !bIsTransparent )
1481 colour = pimldp->rgbBk;
1482 if( colour == CLR_DEFAULT )
1483 colour = himl->clrBk;
1484 if( colour == CLR_NONE )
1485 colour = GetBkColor(pimldp->hdcDst);
1488 hOldBrush = SelectObject (hImageDC, CreateSolidBrush (colour));
1489 PatBlt( hImageDC, 0, 0, cx, cy, PATCOPY );
1490 if (himl->hbmMask)
1492 BitBlt( hImageDC, 0, 0, cx, cy, hMaskListDC, pt.x, pt.y, SRCAND );
1493 BitBlt( hImageDC, 0, 0, cx, cy, hImageListDC, pt.x, pt.y, SRCPAINT );
1495 else
1496 BitBlt( hImageDC, 0, 0, cx, cy, hImageListDC, pt.x, pt.y, SRCCOPY);
1497 DeleteObject (SelectObject (hImageDC, hOldBrush));
1500 /* Time for blending, if required */
1501 if (bBlend) {
1502 HBRUSH hBlendBrush;
1503 COLORREF clrBlend = pimldp->rgbFg;
1504 HDC hBlendMaskDC = hImageListDC;
1505 HBITMAP hOldBitmap;
1507 /* Create the blend Mask */
1508 hOldBitmap = SelectObject(hBlendMaskDC, hBlendMaskBmp);
1509 hBlendBrush = fStyle & ILD_BLEND50 ? himl->hbrBlend50 : himl->hbrBlend25;
1510 hOldBrush = SelectObject(hBlendMaskDC, hBlendBrush);
1511 PatBlt(hBlendMaskDC, 0, 0, cx, cy, PATCOPY);
1512 SelectObject(hBlendMaskDC, hOldBrush);
1514 /* Modify the blend mask if an Image Mask exist */
1515 if(himl->hbmMask) {
1516 BitBlt(hBlendMaskDC, 0, 0, cx, cy, hMaskListDC, pt.x, pt.y, 0x220326); /* NOTSRCAND */
1517 BitBlt(hBlendMaskDC, 0, 0, cx, cy, hBlendMaskDC, 0, 0, NOTSRCCOPY);
1520 /* now apply blend to the current image given the BlendMask */
1521 if (clrBlend == CLR_DEFAULT) clrBlend = GetSysColor (COLOR_HIGHLIGHT);
1522 else if (clrBlend == CLR_NONE) clrBlend = GetTextColor (pimldp->hdcDst);
1523 hOldBrush = SelectObject (hImageDC, CreateSolidBrush(clrBlend));
1524 BitBlt (hImageDC, 0, 0, cx, cy, hBlendMaskDC, 0, 0, 0xB8074A); /* PSDPxax */
1525 DeleteObject(SelectObject(hImageDC, hOldBrush));
1526 SelectObject(hBlendMaskDC, hOldBitmap);
1529 /* Now do the overlay image, if any */
1530 nOvlIdx = (pimldp->fStyle & ILD_OVERLAYMASK) >> 8;
1531 if ( (nOvlIdx >= 1) && (nOvlIdx <= MAX_OVERLAYIMAGE)) {
1532 nOvlIdx = himl->nOvlIdx[nOvlIdx - 1];
1533 if ((nOvlIdx >= 0) && (nOvlIdx < himl->cCurImage)) {
1534 POINT ptOvl;
1535 imagelist_point_from_index( himl, nOvlIdx, &ptOvl );
1536 ptOvl.x += pimldp->xBitmap;
1537 if (himl->hbmMask && !(fStyle & ILD_IMAGE))
1538 BitBlt (hImageDC, 0, 0, cx, cy, hMaskListDC, ptOvl.x, ptOvl.y, SRCAND);
1539 BitBlt (hImageDC, 0, 0, cx, cy, hImageListDC, ptOvl.x, ptOvl.y, SRCPAINT);
1543 if (fState & ILS_SATURATE) FIXME("ILS_SATURATE: unimplemented!\n");
1544 if (fState & ILS_GLOW) FIXME("ILS_GLOW: unimplemented!\n");
1545 if (fState & ILS_SHADOW) FIXME("ILS_SHADOW: unimplemented!\n");
1547 if (fStyle & ILD_PRESERVEALPHA) FIXME("ILD_PRESERVEALPHA: unimplemented!\n");
1548 if (fStyle & ILD_SCALE) FIXME("ILD_SCALE: unimplemented!\n");
1549 if (fStyle & ILD_DPISCALE) FIXME("ILD_DPISCALE: unimplemented!\n");
1551 /* now copy the image to the screen */
1552 dwRop = SRCCOPY;
1553 if (himl->hbmMask && bIsTransparent ) {
1554 COLORREF oldDstFg = SetTextColor(pimldp->hdcDst, RGB( 0, 0, 0 ) );
1555 COLORREF oldDstBk = SetBkColor(pimldp->hdcDst, RGB( 0xff, 0xff, 0xff ));
1556 BitBlt (pimldp->hdcDst, pimldp->x, pimldp->y, cx, cy, hMaskListDC, pt.x, pt.y, SRCAND);
1557 SetBkColor(pimldp->hdcDst, oldDstBk);
1558 SetTextColor(pimldp->hdcDst, oldDstFg);
1559 dwRop = SRCPAINT;
1561 if (fStyle & ILD_ROP) dwRop = pimldp->dwRop;
1562 BitBlt (pimldp->hdcDst, pimldp->x, pimldp->y, cx, cy, hImageDC, 0, 0, dwRop);
1564 bResult = TRUE;
1565 end:
1566 /* cleanup the mess */
1567 SetBkColor(hImageDC, oldImageBk);
1568 SetTextColor(hImageDC, oldImageFg);
1569 SelectObject(hImageDC, hOldImageBmp);
1570 cleanup:
1571 DeleteObject(hBlendMaskBmp);
1572 DeleteObject(hImageBmp);
1573 DeleteDC(hImageDC);
1575 return bResult;
1579 /*************************************************************************
1580 * ImageList_Duplicate [COMCTL32.@]
1582 * Duplicates an image list.
1584 * PARAMS
1585 * himlSrc [I] source image list handle
1587 * RETURNS
1588 * Success: Handle of duplicated image list.
1589 * Failure: NULL
1592 HIMAGELIST WINAPI
1593 ImageList_Duplicate (HIMAGELIST himlSrc)
1595 HIMAGELIST himlDst;
1597 if (!is_valid(himlSrc)) {
1598 ERR("Invalid image list handle!\n");
1599 return NULL;
1602 himlDst = ImageList_Create (himlSrc->cx, himlSrc->cy, himlSrc->flags,
1603 himlSrc->cCurImage, himlSrc->cGrow);
1605 if (himlDst)
1607 SIZE sz;
1609 imagelist_get_bitmap_size(himlSrc, himlSrc->cCurImage, &sz);
1610 BitBlt (himlDst->hdcImage, 0, 0, sz.cx, sz.cy,
1611 himlSrc->hdcImage, 0, 0, SRCCOPY);
1613 if (himlDst->hbmMask)
1614 BitBlt (himlDst->hdcMask, 0, 0, sz.cx, sz.cy,
1615 himlSrc->hdcMask, 0, 0, SRCCOPY);
1617 himlDst->cCurImage = himlSrc->cCurImage;
1618 if (himlSrc->has_alpha && himlDst->has_alpha)
1619 memcpy( himlDst->has_alpha, himlSrc->has_alpha, himlDst->cCurImage );
1621 return himlDst;
1625 /*************************************************************************
1626 * ImageList_EndDrag [COMCTL32.@]
1628 * Finishes a drag operation.
1630 * PARAMS
1631 * no Parameters
1633 * RETURNS
1634 * Success: TRUE
1635 * Failure: FALSE
1638 VOID WINAPI
1639 ImageList_EndDrag (void)
1641 /* cleanup the InternalDrag struct */
1642 InternalDrag.hwnd = 0;
1643 if (InternalDrag.himl != InternalDrag.himlNoCursor)
1644 ImageList_Destroy (InternalDrag.himlNoCursor);
1645 ImageList_Destroy (InternalDrag.himl);
1646 InternalDrag.himlNoCursor = InternalDrag.himl = 0;
1647 InternalDrag.x= 0;
1648 InternalDrag.y= 0;
1649 InternalDrag.dxHotspot = 0;
1650 InternalDrag.dyHotspot = 0;
1651 InternalDrag.bShow = FALSE;
1652 DeleteObject(InternalDrag.hbmBg);
1653 InternalDrag.hbmBg = 0;
1657 /*************************************************************************
1658 * ImageList_GetBkColor [COMCTL32.@]
1660 * Returns the background color of an image list.
1662 * PARAMS
1663 * himl [I] Image list handle.
1665 * RETURNS
1666 * Success: background color
1667 * Failure: CLR_NONE
1670 COLORREF WINAPI
1671 ImageList_GetBkColor (HIMAGELIST himl)
1673 return himl ? himl->clrBk : CLR_NONE;
1677 /*************************************************************************
1678 * ImageList_GetDragImage [COMCTL32.@]
1680 * Returns the handle to the internal drag image list.
1682 * PARAMS
1683 * ppt [O] Pointer to the drag position. Can be NULL.
1684 * pptHotspot [O] Pointer to the position of the hot spot. Can be NULL.
1686 * RETURNS
1687 * Success: Handle of the drag image list.
1688 * Failure: NULL.
1691 HIMAGELIST WINAPI
1692 ImageList_GetDragImage (POINT *ppt, POINT *pptHotspot)
1694 if (is_valid(InternalDrag.himl)) {
1695 if (ppt) {
1696 ppt->x = InternalDrag.x;
1697 ppt->y = InternalDrag.y;
1699 if (pptHotspot) {
1700 pptHotspot->x = InternalDrag.dxHotspot;
1701 pptHotspot->y = InternalDrag.dyHotspot;
1703 return (InternalDrag.himl);
1706 return NULL;
1710 /*************************************************************************
1711 * ImageList_GetFlags [COMCTL32.@]
1713 * Gets the flags of the specified image list.
1715 * PARAMS
1716 * himl [I] Handle to image list
1718 * RETURNS
1719 * Image list flags.
1721 * BUGS
1722 * Stub.
1725 DWORD WINAPI
1726 ImageList_GetFlags(HIMAGELIST himl)
1728 TRACE("%p\n", himl);
1730 return is_valid(himl) ? himl->flags : 0;
1734 /*************************************************************************
1735 * ImageList_GetIcon [COMCTL32.@]
1737 * Creates an icon from a masked image of an image list.
1739 * PARAMS
1740 * himl [I] handle to image list
1741 * i [I] image index
1742 * flags [I] drawing style flags
1744 * RETURNS
1745 * Success: icon handle
1746 * Failure: NULL
1749 HICON WINAPI
1750 ImageList_GetIcon (HIMAGELIST himl, INT i, UINT fStyle)
1752 ICONINFO ii;
1753 HICON hIcon;
1754 HBITMAP hOldDstBitmap;
1755 HDC hdcDst;
1756 POINT pt;
1758 TRACE("%p %d %d\n", himl, i, fStyle);
1759 if (!is_valid(himl) || (i < 0) || (i >= himl->cCurImage)) return NULL;
1761 ii.fIcon = TRUE;
1762 ii.xHotspot = 0;
1763 ii.yHotspot = 0;
1765 /* create colour bitmap */
1766 hdcDst = GetDC(0);
1767 ii.hbmColor = CreateCompatibleBitmap(hdcDst, himl->cx, himl->cy);
1768 ReleaseDC(0, hdcDst);
1770 hdcDst = CreateCompatibleDC(0);
1772 imagelist_point_from_index( himl, i, &pt );
1774 /* draw mask*/
1775 ii.hbmMask = CreateBitmap (himl->cx, himl->cy, 1, 1, NULL);
1776 hOldDstBitmap = SelectObject (hdcDst, ii.hbmMask);
1777 if (himl->hbmMask) {
1778 BitBlt (hdcDst, 0, 0, himl->cx, himl->cy,
1779 himl->hdcMask, pt.x, pt.y, SRCCOPY);
1781 else
1782 PatBlt (hdcDst, 0, 0, himl->cx, himl->cy, BLACKNESS);
1784 /* draw image*/
1785 SelectObject (hdcDst, ii.hbmColor);
1786 BitBlt (hdcDst, 0, 0, himl->cx, himl->cy,
1787 himl->hdcImage, pt.x, pt.y, SRCCOPY);
1790 * CreateIconIndirect requires us to deselect the bitmaps from
1791 * the DCs before calling
1793 SelectObject(hdcDst, hOldDstBitmap);
1795 hIcon = CreateIconIndirect (&ii);
1797 DeleteObject (ii.hbmMask);
1798 DeleteObject (ii.hbmColor);
1799 DeleteDC (hdcDst);
1801 return hIcon;
1805 /*************************************************************************
1806 * ImageList_GetIconSize [COMCTL32.@]
1808 * Retrieves the size of an image in an image list.
1810 * PARAMS
1811 * himl [I] handle to image list
1812 * cx [O] pointer to the image width.
1813 * cy [O] pointer to the image height.
1815 * RETURNS
1816 * Success: TRUE
1817 * Failure: FALSE
1819 * NOTES
1820 * All images in an image list have the same size.
1823 BOOL WINAPI
1824 ImageList_GetIconSize (HIMAGELIST himl, INT *cx, INT *cy)
1826 if (!is_valid(himl) || !cx || !cy)
1827 return FALSE;
1828 if ((himl->cx <= 0) || (himl->cy <= 0))
1829 return FALSE;
1831 *cx = himl->cx;
1832 *cy = himl->cy;
1834 return TRUE;
1838 /*************************************************************************
1839 * ImageList_GetImageCount [COMCTL32.@]
1841 * Returns the number of images in an image list.
1843 * PARAMS
1844 * himl [I] handle to image list
1846 * RETURNS
1847 * Success: Number of images.
1848 * Failure: 0
1851 INT WINAPI
1852 ImageList_GetImageCount (HIMAGELIST himl)
1854 if (!is_valid(himl))
1855 return 0;
1857 return himl->cCurImage;
1861 /*************************************************************************
1862 * ImageList_GetImageInfo [COMCTL32.@]
1864 * Returns information about an image in an image list.
1866 * PARAMS
1867 * himl [I] handle to image list
1868 * i [I] image index
1869 * pImageInfo [O] pointer to the image information
1871 * RETURNS
1872 * Success: TRUE
1873 * Failure: FALSE
1876 BOOL WINAPI
1877 ImageList_GetImageInfo (HIMAGELIST himl, INT i, IMAGEINFO *pImageInfo)
1879 POINT pt;
1881 if (!is_valid(himl) || (pImageInfo == NULL))
1882 return FALSE;
1883 if ((i < 0) || (i >= himl->cCurImage))
1884 return FALSE;
1886 pImageInfo->hbmImage = himl->hbmImage;
1887 pImageInfo->hbmMask = himl->hbmMask;
1889 imagelist_point_from_index( himl, i, &pt );
1890 pImageInfo->rcImage.top = pt.y;
1891 pImageInfo->rcImage.bottom = pt.y + himl->cy;
1892 pImageInfo->rcImage.left = pt.x;
1893 pImageInfo->rcImage.right = pt.x + himl->cx;
1895 return TRUE;
1899 /*************************************************************************
1900 * ImageList_GetImageRect [COMCTL32.@]
1902 * Retrieves the rectangle of the specified image in an image list.
1904 * PARAMS
1905 * himl [I] handle to image list
1906 * i [I] image index
1907 * lpRect [O] pointer to the image rectangle
1909 * RETURNS
1910 * Success: TRUE
1911 * Failure: FALSE
1913 * NOTES
1914 * This is an UNDOCUMENTED function!!!
1917 BOOL WINAPI
1918 ImageList_GetImageRect (HIMAGELIST himl, INT i, LPRECT lpRect)
1920 POINT pt;
1922 if (!is_valid(himl) || (lpRect == NULL))
1923 return FALSE;
1924 if ((i < 0) || (i >= himl->cCurImage))
1925 return FALSE;
1927 imagelist_point_from_index( himl, i, &pt );
1928 lpRect->left = pt.x;
1929 lpRect->top = pt.y;
1930 lpRect->right = pt.x + himl->cx;
1931 lpRect->bottom = pt.y + himl->cy;
1933 return TRUE;
1937 /*************************************************************************
1938 * ImageList_LoadImage [COMCTL32.@]
1939 * ImageList_LoadImageA [COMCTL32.@]
1941 * Creates an image list from a bitmap, icon or cursor.
1943 * See ImageList_LoadImageW.
1946 HIMAGELIST WINAPI
1947 ImageList_LoadImageA (HINSTANCE hi, LPCSTR lpbmp, INT cx, INT cGrow,
1948 COLORREF clrMask, UINT uType, UINT uFlags)
1950 HIMAGELIST himl;
1951 LPWSTR lpbmpW;
1952 DWORD len;
1954 if (IS_INTRESOURCE(lpbmp))
1955 return ImageList_LoadImageW(hi, (LPCWSTR)lpbmp, cx, cGrow, clrMask,
1956 uType, uFlags);
1958 len = MultiByteToWideChar(CP_ACP, 0, lpbmp, -1, NULL, 0);
1959 lpbmpW = Alloc(len * sizeof(WCHAR));
1960 MultiByteToWideChar(CP_ACP, 0, lpbmp, -1, lpbmpW, len);
1962 himl = ImageList_LoadImageW(hi, lpbmpW, cx, cGrow, clrMask, uType, uFlags);
1963 Free (lpbmpW);
1964 return himl;
1968 /*************************************************************************
1969 * ImageList_LoadImageW [COMCTL32.@]
1971 * Creates an image list from a bitmap, icon or cursor.
1973 * PARAMS
1974 * hi [I] instance handle
1975 * lpbmp [I] name or id of the image
1976 * cx [I] width of each image
1977 * cGrow [I] number of images to expand
1978 * clrMask [I] mask color
1979 * uType [I] type of image to load
1980 * uFlags [I] loading flags
1982 * RETURNS
1983 * Success: handle to the loaded image list
1984 * Failure: NULL
1986 * SEE
1987 * LoadImage ()
1990 HIMAGELIST WINAPI
1991 ImageList_LoadImageW (HINSTANCE hi, LPCWSTR lpbmp, INT cx, INT cGrow,
1992 COLORREF clrMask, UINT uType, UINT uFlags)
1994 HIMAGELIST himl = NULL;
1995 HANDLE handle;
1996 INT nImageCount;
1998 handle = LoadImageW (hi, lpbmp, uType, 0, 0, uFlags);
1999 if (!handle) {
2000 WARN("Couldn't load image\n");
2001 return NULL;
2004 if (uType == IMAGE_BITMAP) {
2005 DIBSECTION dib;
2006 UINT color;
2008 if (GetObjectW (handle, sizeof(dib), &dib) == sizeof(BITMAP)) color = ILC_COLOR;
2009 else color = dib.dsBm.bmBitsPixel;
2011 /* To match windows behavior, if cx is set to zero and
2012 the flag DI_DEFAULTSIZE is specified, cx becomes the
2013 system metric value for icons. If the flag is not specified
2014 the function sets the size to the height of the bitmap */
2015 if (cx == 0)
2017 if (uFlags & DI_DEFAULTSIZE)
2018 cx = GetSystemMetrics (SM_CXICON);
2019 else
2020 cx = dib.dsBm.bmHeight;
2023 nImageCount = dib.dsBm.bmWidth / cx;
2025 himl = ImageList_Create (cx, dib.dsBm.bmHeight, ILC_MASK | color, nImageCount, cGrow);
2026 if (!himl) {
2027 DeleteObject (handle);
2028 return NULL;
2030 ImageList_AddMasked (himl, handle, clrMask);
2032 else if ((uType == IMAGE_ICON) || (uType == IMAGE_CURSOR)) {
2033 ICONINFO ii;
2034 BITMAP bmp;
2036 GetIconInfo (handle, &ii);
2037 GetObjectW (ii.hbmColor, sizeof(BITMAP), &bmp);
2038 himl = ImageList_Create (bmp.bmWidth, bmp.bmHeight,
2039 ILC_MASK | ILC_COLOR, 1, cGrow);
2040 if (!himl) {
2041 DeleteObject (ii.hbmColor);
2042 DeleteObject (ii.hbmMask);
2043 DeleteObject (handle);
2044 return NULL;
2046 ImageList_Add (himl, ii.hbmColor, ii.hbmMask);
2047 DeleteObject (ii.hbmColor);
2048 DeleteObject (ii.hbmMask);
2051 DeleteObject (handle);
2053 return himl;
2057 /*************************************************************************
2058 * ImageList_Merge [COMCTL32.@]
2060 * Create an image list containing a merged image from two image lists.
2062 * PARAMS
2063 * himl1 [I] handle to first image list
2064 * i1 [I] first image index
2065 * himl2 [I] handle to second image list
2066 * i2 [I] second image index
2067 * dx [I] X offset of the second image relative to the first.
2068 * dy [I] Y offset of the second image relative to the first.
2070 * RETURNS
2071 * Success: The newly created image list. It contains a single image
2072 * consisting of the second image merged with the first.
2073 * Failure: NULL, if either himl1 or himl2 is invalid.
2075 * NOTES
2076 * - The returned image list should be deleted by the caller using
2077 * ImageList_Destroy() when it is no longer required.
2078 * - If either i1 or i2 is not a valid image index, they will be treated
2079 * as blank images.
2081 HIMAGELIST WINAPI
2082 ImageList_Merge (HIMAGELIST himl1, INT i1, HIMAGELIST himl2, INT i2,
2083 INT dx, INT dy)
2085 HIMAGELIST himlDst = NULL;
2086 INT cxDst, cyDst;
2087 INT xOff1, yOff1, xOff2, yOff2;
2088 POINT pt1, pt2;
2089 INT newFlags;
2091 TRACE("(himl1=%p i1=%d himl2=%p i2=%d dx=%d dy=%d)\n", himl1, i1, himl2,
2092 i2, dx, dy);
2094 if (!is_valid(himl1) || !is_valid(himl2))
2095 return NULL;
2097 if (dx > 0) {
2098 cxDst = max (himl1->cx, dx + himl2->cx);
2099 xOff1 = 0;
2100 xOff2 = dx;
2102 else if (dx < 0) {
2103 cxDst = max (himl2->cx, himl1->cx - dx);
2104 xOff1 = -dx;
2105 xOff2 = 0;
2107 else {
2108 cxDst = max (himl1->cx, himl2->cx);
2109 xOff1 = 0;
2110 xOff2 = 0;
2113 if (dy > 0) {
2114 cyDst = max (himl1->cy, dy + himl2->cy);
2115 yOff1 = 0;
2116 yOff2 = dy;
2118 else if (dy < 0) {
2119 cyDst = max (himl2->cy, himl1->cy - dy);
2120 yOff1 = -dy;
2121 yOff2 = 0;
2123 else {
2124 cyDst = max (himl1->cy, himl2->cy);
2125 yOff1 = 0;
2126 yOff2 = 0;
2129 newFlags = (himl1->flags > himl2->flags ? himl1->flags : himl2->flags) & ILC_COLORDDB;
2130 if (newFlags == ILC_COLORDDB && (himl1->flags & ILC_COLORDDB) == ILC_COLOR16)
2131 newFlags = ILC_COLOR16; /* this is what native (at least v5) does, don't know why */
2132 himlDst = ImageList_Create (cxDst, cyDst, ILC_MASK | newFlags, 1, 1);
2134 if (himlDst)
2136 imagelist_point_from_index( himl1, i1, &pt1 );
2137 imagelist_point_from_index( himl2, i2, &pt2 );
2139 /* copy image */
2140 BitBlt (himlDst->hdcImage, 0, 0, cxDst, cyDst, himl1->hdcImage, 0, 0, BLACKNESS);
2141 if (i1 >= 0 && i1 < himl1->cCurImage)
2142 BitBlt (himlDst->hdcImage, xOff1, yOff1, himl1->cx, himl1->cy, himl1->hdcImage, pt1.x, pt1.y, SRCCOPY);
2143 if (i2 >= 0 && i2 < himl2->cCurImage)
2145 if (himl2->flags & ILC_MASK)
2147 BitBlt (himlDst->hdcImage, xOff2, yOff2, himl2->cx, himl2->cy, himl2->hdcMask , pt2.x, pt2.y, SRCAND);
2148 BitBlt (himlDst->hdcImage, xOff2, yOff2, himl2->cx, himl2->cy, himl2->hdcImage, pt2.x, pt2.y, SRCPAINT);
2150 else
2151 BitBlt (himlDst->hdcImage, xOff2, yOff2, himl2->cx, himl2->cy, himl2->hdcImage, pt2.x, pt2.y, SRCCOPY);
2154 /* copy mask */
2155 BitBlt (himlDst->hdcMask, 0, 0, cxDst, cyDst, himl1->hdcMask, 0, 0, WHITENESS);
2156 if (i1 >= 0 && i1 < himl1->cCurImage)
2157 BitBlt (himlDst->hdcMask, xOff1, yOff1, himl1->cx, himl1->cy, himl1->hdcMask, pt1.x, pt1.y, SRCCOPY);
2158 if (i2 >= 0 && i2 < himl2->cCurImage)
2159 BitBlt (himlDst->hdcMask, xOff2, yOff2, himl2->cx, himl2->cy, himl2->hdcMask, pt2.x, pt2.y, SRCAND);
2161 himlDst->cCurImage = 1;
2164 return himlDst;
2168 /* helper for ImageList_Read, see comments below */
2169 static void *read_bitmap(LPSTREAM pstm, BITMAPINFO *bmi)
2171 BITMAPFILEHEADER bmfh;
2172 int bitsperpixel, palspace;
2173 void *bits;
2175 if (FAILED(IStream_Read ( pstm, &bmfh, sizeof(bmfh), NULL)))
2176 return NULL;
2178 if (bmfh.bfType != (('M'<<8)|'B'))
2179 return NULL;
2181 if (FAILED(IStream_Read ( pstm, &bmi->bmiHeader, sizeof(bmi->bmiHeader), NULL)))
2182 return NULL;
2184 if ((bmi->bmiHeader.biSize != sizeof(bmi->bmiHeader)))
2185 return NULL;
2187 TRACE("width %u, height %u, planes %u, bpp %u\n",
2188 bmi->bmiHeader.biWidth, bmi->bmiHeader.biHeight,
2189 bmi->bmiHeader.biPlanes, bmi->bmiHeader.biBitCount);
2191 bitsperpixel = bmi->bmiHeader.biPlanes * bmi->bmiHeader.biBitCount;
2192 if (bitsperpixel<=8)
2193 palspace = (1<<bitsperpixel)*sizeof(RGBQUAD);
2194 else
2195 palspace = 0;
2197 bmi->bmiHeader.biSizeImage = get_dib_image_size( bmi );
2199 /* read the palette right after the end of the bitmapinfoheader */
2200 if (palspace && FAILED(IStream_Read(pstm, bmi->bmiColors, palspace, NULL)))
2201 return NULL;
2203 bits = Alloc(bmi->bmiHeader.biSizeImage);
2204 if (!bits) return NULL;
2206 if (FAILED(IStream_Read(pstm, bits, bmi->bmiHeader.biSizeImage, NULL)))
2208 Free(bits);
2209 return NULL;
2211 return bits;
2214 /*************************************************************************
2215 * ImageList_Read [COMCTL32.@]
2217 * Reads an image list from a stream.
2219 * PARAMS
2220 * pstm [I] pointer to a stream
2222 * RETURNS
2223 * Success: handle to image list
2224 * Failure: NULL
2226 * The format is like this:
2227 * ILHEAD ilheadstruct;
2229 * for the color image part:
2230 * BITMAPFILEHEADER bmfh;
2231 * BITMAPINFOHEADER bmih;
2232 * only if it has a palette:
2233 * RGBQUAD rgbs[nr_of_paletted_colors];
2235 * BYTE colorbits[imagesize];
2237 * the following only if the ILC_MASK bit is set in ILHEAD.ilFlags:
2238 * BITMAPFILEHEADER bmfh_mask;
2239 * BITMAPINFOHEADER bmih_mask;
2240 * only if it has a palette (it usually does not):
2241 * RGBQUAD rgbs[nr_of_paletted_colors];
2243 * BYTE maskbits[imagesize];
2245 HIMAGELIST WINAPI ImageList_Read (LPSTREAM pstm)
2247 char image_buf[sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * 256];
2248 char mask_buf[sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * 256];
2249 BITMAPINFO *image_info = (BITMAPINFO *)image_buf;
2250 BITMAPINFO *mask_info = (BITMAPINFO *)mask_buf;
2251 void *image_bits, *mask_bits = NULL;
2252 ILHEAD ilHead;
2253 HIMAGELIST himl;
2254 unsigned int i;
2256 TRACE("%p\n", pstm);
2258 if (FAILED(IStream_Read (pstm, &ilHead, sizeof(ILHEAD), NULL)))
2259 return NULL;
2260 if (ilHead.usMagic != (('L' << 8) | 'I'))
2261 return NULL;
2262 if (ilHead.usVersion != 0x101) /* probably version? */
2263 return NULL;
2265 TRACE("cx %u, cy %u, flags 0x%04x, cCurImage %u, cMaxImage %u\n",
2266 ilHead.cx, ilHead.cy, ilHead.flags, ilHead.cCurImage, ilHead.cMaxImage);
2268 himl = ImageList_Create(ilHead.cx, ilHead.cy, ilHead.flags, ilHead.cCurImage, ilHead.cMaxImage);
2269 if (!himl)
2270 return NULL;
2272 if (!(image_bits = read_bitmap(pstm, image_info)))
2274 WARN("failed to read bitmap from stream\n");
2275 return NULL;
2277 if (ilHead.flags & ILC_MASK)
2279 if (!(mask_bits = read_bitmap(pstm, mask_info)))
2281 WARN("failed to read mask bitmap from stream\n");
2282 return NULL;
2285 else mask_info = NULL;
2287 if (himl->has_alpha && image_info->bmiHeader.biBitCount == 32)
2289 DWORD *ptr = image_bits;
2290 BYTE *mask_ptr = mask_bits;
2291 int stride = himl->cy * image_info->bmiHeader.biWidth;
2293 if (image_info->bmiHeader.biHeight > 0) /* bottom-up */
2295 ptr += image_info->bmiHeader.biHeight * image_info->bmiHeader.biWidth - stride;
2296 mask_ptr += (image_info->bmiHeader.biHeight * image_info->bmiHeader.biWidth - stride) / 8;
2297 stride = -stride;
2298 image_info->bmiHeader.biHeight = himl->cy;
2300 else image_info->bmiHeader.biHeight = -himl->cy;
2302 for (i = 0; i < ilHead.cCurImage; i += TILE_COUNT)
2304 add_dib_bits( himl, i, min( ilHead.cCurImage - i, TILE_COUNT ),
2305 himl->cx, himl->cy, image_info, mask_info, ptr, mask_ptr );
2306 ptr += stride;
2307 mask_ptr += stride / 8;
2310 else
2312 StretchDIBits( himl->hdcImage, 0, 0, image_info->bmiHeader.biWidth, image_info->bmiHeader.biHeight,
2313 0, 0, image_info->bmiHeader.biWidth, image_info->bmiHeader.biHeight,
2314 image_bits, image_info, DIB_RGB_COLORS, SRCCOPY);
2315 if (mask_info)
2316 StretchDIBits( himl->hdcMask, 0, 0, mask_info->bmiHeader.biWidth, mask_info->bmiHeader.biHeight,
2317 0, 0, mask_info->bmiHeader.biWidth, mask_info->bmiHeader.biHeight,
2318 mask_bits, mask_info, DIB_RGB_COLORS, SRCCOPY);
2320 Free( image_bits );
2321 Free( mask_bits );
2323 himl->cCurImage = ilHead.cCurImage;
2324 himl->cMaxImage = ilHead.cMaxImage;
2326 ImageList_SetBkColor(himl,ilHead.bkcolor);
2327 for (i=0;i<4;i++)
2328 ImageList_SetOverlayImage(himl,ilHead.ovls[i],i+1);
2329 return himl;
2333 /*************************************************************************
2334 * ImageList_Remove [COMCTL32.@]
2336 * Removes an image from an image list
2338 * PARAMS
2339 * himl [I] image list handle
2340 * i [I] image index
2342 * RETURNS
2343 * Success: TRUE
2344 * Failure: FALSE
2346 * FIXME: as the image list storage test shows, native comctl32 simply shifts
2347 * images without creating a new bitmap.
2349 BOOL WINAPI
2350 ImageList_Remove (HIMAGELIST himl, INT i)
2352 HBITMAP hbmNewImage, hbmNewMask;
2353 HDC hdcBmp;
2354 SIZE sz;
2356 TRACE("(himl=%p i=%d)\n", himl, i);
2358 if (!is_valid(himl)) {
2359 ERR("Invalid image list handle!\n");
2360 return FALSE;
2363 if ((i < -1) || (i >= himl->cCurImage)) {
2364 TRACE("index out of range! %d\n", i);
2365 return FALSE;
2368 if (i == -1) {
2369 INT nCount;
2371 /* remove all */
2372 if (himl->cCurImage == 0) {
2373 /* remove all on empty ImageList is allowed */
2374 TRACE("remove all on empty ImageList!\n");
2375 return TRUE;
2378 himl->cMaxImage = himl->cGrow;
2379 himl->cCurImage = 0;
2380 for (nCount = 0; nCount < MAX_OVERLAYIMAGE; nCount++)
2381 himl->nOvlIdx[nCount] = -1;
2383 if (himl->has_alpha)
2385 HeapFree( GetProcessHeap(), 0, himl->has_alpha );
2386 himl->has_alpha = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, himl->cMaxImage );
2389 hbmNewImage = ImageList_CreateImage(himl->hdcImage, himl, himl->cMaxImage);
2390 SelectObject (himl->hdcImage, hbmNewImage);
2391 DeleteObject (himl->hbmImage);
2392 himl->hbmImage = hbmNewImage;
2394 if (himl->hbmMask) {
2396 imagelist_get_bitmap_size(himl, himl->cMaxImage, &sz);
2397 hbmNewMask = CreateBitmap (sz.cx, sz.cy, 1, 1, NULL);
2398 SelectObject (himl->hdcMask, hbmNewMask);
2399 DeleteObject (himl->hbmMask);
2400 himl->hbmMask = hbmNewMask;
2403 else {
2404 /* delete one image */
2405 TRACE("Remove single image! %d\n", i);
2407 /* create new bitmap(s) */
2408 TRACE(" - Number of images: %d / %d (Old/New)\n",
2409 himl->cCurImage, himl->cCurImage - 1);
2411 hbmNewImage = ImageList_CreateImage(himl->hdcImage, himl, himl->cMaxImage);
2413 imagelist_get_bitmap_size(himl, himl->cMaxImage, &sz );
2414 if (himl->hbmMask)
2415 hbmNewMask = CreateBitmap (sz.cx, sz.cy, 1, 1, NULL);
2416 else
2417 hbmNewMask = 0; /* Just to keep compiler happy! */
2419 hdcBmp = CreateCompatibleDC (0);
2421 /* copy all images and masks prior to the "removed" image */
2422 if (i > 0) {
2423 TRACE("Pre image copy: Copy %d images\n", i);
2425 SelectObject (hdcBmp, hbmNewImage);
2426 imagelist_copy_images( himl, himl->hdcImage, hdcBmp, 0, i, 0 );
2428 if (himl->hbmMask) {
2429 SelectObject (hdcBmp, hbmNewMask);
2430 imagelist_copy_images( himl, himl->hdcMask, hdcBmp, 0, i, 0 );
2434 /* copy all images and masks behind the removed image */
2435 if (i < himl->cCurImage - 1) {
2436 TRACE("Post image copy!\n");
2438 SelectObject (hdcBmp, hbmNewImage);
2439 imagelist_copy_images( himl, himl->hdcImage, hdcBmp, i + 1,
2440 (himl->cCurImage - i), i );
2442 if (himl->hbmMask) {
2443 SelectObject (hdcBmp, hbmNewMask);
2444 imagelist_copy_images( himl, himl->hdcMask, hdcBmp, i + 1,
2445 (himl->cCurImage - i), i );
2449 DeleteDC (hdcBmp);
2451 /* delete old images and insert new ones */
2452 SelectObject (himl->hdcImage, hbmNewImage);
2453 DeleteObject (himl->hbmImage);
2454 himl->hbmImage = hbmNewImage;
2455 if (himl->hbmMask) {
2456 SelectObject (himl->hdcMask, hbmNewMask);
2457 DeleteObject (himl->hbmMask);
2458 himl->hbmMask = hbmNewMask;
2461 himl->cCurImage--;
2464 return TRUE;
2468 /*************************************************************************
2469 * ImageList_Replace [COMCTL32.@]
2471 * Replaces an image in an image list with a new image.
2473 * PARAMS
2474 * himl [I] handle to image list
2475 * i [I] image index
2476 * hbmImage [I] handle to image bitmap
2477 * hbmMask [I] handle to mask bitmap. Can be NULL.
2479 * RETURNS
2480 * Success: TRUE
2481 * Failure: FALSE
2484 BOOL WINAPI
2485 ImageList_Replace (HIMAGELIST himl, INT i, HBITMAP hbmImage,
2486 HBITMAP hbmMask)
2488 HDC hdcImage;
2489 BITMAP bmp;
2490 POINT pt;
2492 TRACE("%p %d %p %p\n", himl, i, hbmImage, hbmMask);
2494 if (!is_valid(himl)) {
2495 ERR("Invalid image list handle!\n");
2496 return FALSE;
2499 if ((i >= himl->cMaxImage) || (i < 0)) {
2500 ERR("Invalid image index!\n");
2501 return FALSE;
2504 if (!GetObjectW(hbmImage, sizeof(BITMAP), &bmp))
2505 return FALSE;
2507 hdcImage = CreateCompatibleDC (0);
2509 /* Replace Image */
2510 SelectObject (hdcImage, hbmImage);
2512 if (add_with_alpha( himl, hdcImage, i, 1, bmp.bmWidth, bmp.bmHeight, hbmImage, hbmMask ))
2513 goto done;
2515 imagelist_point_from_index(himl, i, &pt);
2516 StretchBlt (himl->hdcImage, pt.x, pt.y, himl->cx, himl->cy,
2517 hdcImage, 0, 0, bmp.bmWidth, bmp.bmHeight, SRCCOPY);
2519 if (himl->hbmMask)
2521 HDC hdcTemp;
2522 HBITMAP hOldBitmapTemp;
2524 hdcTemp = CreateCompatibleDC(0);
2525 hOldBitmapTemp = SelectObject(hdcTemp, hbmMask);
2527 StretchBlt (himl->hdcMask, pt.x, pt.y, himl->cx, himl->cy,
2528 hdcTemp, 0, 0, bmp.bmWidth, bmp.bmHeight, SRCCOPY);
2529 SelectObject(hdcTemp, hOldBitmapTemp);
2530 DeleteDC(hdcTemp);
2532 /* Remove the background from the image
2534 BitBlt (himl->hdcImage, pt.x, pt.y, bmp.bmWidth, bmp.bmHeight,
2535 himl->hdcMask, pt.x, pt.y, 0x220326); /* NOTSRCAND */
2538 done:
2539 DeleteDC (hdcImage);
2541 return TRUE;
2545 /*************************************************************************
2546 * ImageList_ReplaceIcon [COMCTL32.@]
2548 * Replaces an image in an image list using an icon.
2550 * PARAMS
2551 * himl [I] handle to image list
2552 * i [I] image index
2553 * hIcon [I] handle to icon
2555 * RETURNS
2556 * Success: index of the replaced image
2557 * Failure: -1
2560 INT WINAPI
2561 ImageList_ReplaceIcon (HIMAGELIST himl, INT nIndex, HICON hIcon)
2563 HICON hBestFitIcon;
2564 ICONINFO ii;
2565 BITMAP bmp;
2566 BOOL ret;
2567 POINT pt;
2569 TRACE("(%p %d %p)\n", himl, nIndex, hIcon);
2571 if (!is_valid(himl)) {
2572 ERR("invalid image list\n");
2573 return -1;
2575 if ((nIndex >= himl->cMaxImage) || (nIndex < -1)) {
2576 ERR("invalid image index %d / %d\n", nIndex, himl->cMaxImage);
2577 return -1;
2580 hBestFitIcon = CopyImage(
2581 hIcon, IMAGE_ICON,
2582 himl->cx, himl->cy,
2583 LR_COPYFROMRESOURCE);
2584 /* the above will fail if the icon wasn't loaded from a resource, so try
2585 * again without LR_COPYFROMRESOURCE flag */
2586 if (!hBestFitIcon)
2587 hBestFitIcon = CopyImage(
2588 hIcon, IMAGE_ICON,
2589 himl->cx, himl->cy,
2591 if (!hBestFitIcon)
2592 return -1;
2594 if (nIndex == -1) {
2595 if (himl->cCurImage + 1 >= himl->cMaxImage)
2596 IMAGELIST_InternalExpandBitmaps(himl, 1);
2598 nIndex = himl->cCurImage;
2599 himl->cCurImage++;
2602 if (himl->has_alpha && GetIconInfo (hBestFitIcon, &ii))
2604 HDC hdcImage = CreateCompatibleDC( 0 );
2605 GetObjectW (ii.hbmMask, sizeof(BITMAP), &bmp);
2607 if (!ii.hbmColor)
2609 UINT height = bmp.bmHeight / 2;
2610 HDC hdcMask = CreateCompatibleDC( 0 );
2611 HBITMAP color = CreateBitmap( bmp.bmWidth, height, 1, 1, NULL );
2612 SelectObject( hdcImage, color );
2613 SelectObject( hdcMask, ii.hbmMask );
2614 BitBlt( hdcImage, 0, 0, bmp.bmWidth, height, hdcMask, 0, height, SRCCOPY );
2615 ret = add_with_alpha( himl, hdcImage, nIndex, 1, bmp.bmWidth, height, color, ii.hbmMask );
2616 DeleteDC( hdcMask );
2617 DeleteObject( color );
2619 else ret = add_with_alpha( himl, hdcImage, nIndex, 1, bmp.bmWidth, bmp.bmHeight,
2620 ii.hbmColor, ii.hbmMask );
2622 DeleteDC( hdcImage );
2623 DeleteObject (ii.hbmMask);
2624 if (ii.hbmColor) DeleteObject (ii.hbmColor);
2625 if (ret) goto done;
2628 imagelist_point_from_index(himl, nIndex, &pt);
2630 if (himl->hbmMask)
2632 DrawIconEx( himl->hdcImage, pt.x, pt.y, hBestFitIcon, himl->cx, himl->cy, 0, 0, DI_IMAGE );
2633 PatBlt( himl->hdcMask, pt.x, pt.y, himl->cx, himl->cy, WHITENESS );
2634 DrawIconEx( himl->hdcMask, pt.x, pt.y, hBestFitIcon, himl->cx, himl->cy, 0, 0, DI_MASK );
2636 else
2638 COLORREF color = himl->clrBk != CLR_NONE ? himl->clrBk : comctl32_color.clrWindow;
2639 HBRUSH brush = CreateSolidBrush( GetNearestColor( himl->hdcImage, color ));
2641 SelectObject( himl->hdcImage, brush );
2642 PatBlt( himl->hdcImage, pt.x, pt.y, himl->cx, himl->cy, PATCOPY );
2643 SelectObject( himl->hdcImage, GetStockObject(BLACK_BRUSH) );
2644 DeleteObject( brush );
2645 DrawIconEx( himl->hdcImage, pt.x, pt.y, hBestFitIcon, himl->cx, himl->cy, 0, 0, DI_NORMAL );
2648 done:
2649 DestroyIcon(hBestFitIcon);
2651 TRACE("Insert index = %d, himl->cCurImage = %d\n", nIndex, himl->cCurImage);
2652 return nIndex;
2656 /*************************************************************************
2657 * ImageList_SetBkColor [COMCTL32.@]
2659 * Sets the background color of an image list.
2661 * PARAMS
2662 * himl [I] handle to image list
2663 * clrBk [I] background color
2665 * RETURNS
2666 * Success: previous background color
2667 * Failure: CLR_NONE
2670 COLORREF WINAPI
2671 ImageList_SetBkColor (HIMAGELIST himl, COLORREF clrBk)
2673 COLORREF clrOldBk;
2675 if (!is_valid(himl))
2676 return CLR_NONE;
2678 clrOldBk = himl->clrBk;
2679 himl->clrBk = clrBk;
2680 return clrOldBk;
2684 /*************************************************************************
2685 * ImageList_SetDragCursorImage [COMCTL32.@]
2687 * Combines the specified image with the current drag image
2689 * PARAMS
2690 * himlDrag [I] handle to drag image list
2691 * iDrag [I] drag image index
2692 * dxHotspot [I] X position of the hot spot
2693 * dyHotspot [I] Y position of the hot spot
2695 * RETURNS
2696 * Success: TRUE
2697 * Failure: FALSE
2699 * NOTES
2700 * - The names dxHotspot, dyHotspot are misleading because they have nothing
2701 * to do with a hotspot but are only the offset of the origin of the new
2702 * image relative to the origin of the old image.
2704 * - When this function is called and the drag image is visible, a
2705 * short flickering occurs but this matches the Win9x behavior. It is
2706 * possible to fix the flickering using code like in ImageList_DragMove.
2709 BOOL WINAPI
2710 ImageList_SetDragCursorImage (HIMAGELIST himlDrag, INT iDrag,
2711 INT dxHotspot, INT dyHotspot)
2713 HIMAGELIST himlTemp;
2714 BOOL visible;
2716 if (!is_valid(InternalDrag.himl) || !is_valid(himlDrag))
2717 return FALSE;
2719 TRACE(" dxH=%d dyH=%d nX=%d nY=%d\n",
2720 dxHotspot, dyHotspot, InternalDrag.dxHotspot, InternalDrag.dyHotspot);
2722 visible = InternalDrag.bShow;
2724 himlTemp = ImageList_Merge (InternalDrag.himlNoCursor, 0, himlDrag, iDrag,
2725 dxHotspot, dyHotspot);
2727 if (visible) {
2728 /* hide the drag image */
2729 ImageList_DragShowNolock(FALSE);
2731 if ((InternalDrag.himl->cx != himlTemp->cx) ||
2732 (InternalDrag.himl->cy != himlTemp->cy)) {
2733 /* the size of the drag image changed, invalidate the buffer */
2734 DeleteObject(InternalDrag.hbmBg);
2735 InternalDrag.hbmBg = 0;
2738 if (InternalDrag.himl != InternalDrag.himlNoCursor)
2739 ImageList_Destroy (InternalDrag.himl);
2740 InternalDrag.himl = himlTemp;
2742 if (visible) {
2743 /* show the drag image */
2744 ImageList_DragShowNolock(TRUE);
2747 return TRUE;
2751 /*************************************************************************
2752 * ImageList_SetFilter [COMCTL32.@]
2754 * Sets a filter (or does something completely different)!!???
2755 * It removes 12 Bytes from the stack (3 Parameters).
2757 * PARAMS
2758 * himl [I] SHOULD be a handle to image list
2759 * i [I] COULD be an index?
2760 * dwFilter [I] ???
2762 * RETURNS
2763 * Success: TRUE ???
2764 * Failure: FALSE ???
2766 * BUGS
2767 * This is an UNDOCUMENTED function!!!!
2768 * empty stub.
2771 BOOL WINAPI
2772 ImageList_SetFilter (HIMAGELIST himl, INT i, DWORD dwFilter)
2774 FIXME("(%p 0x%x 0x%x):empty stub!\n", himl, i, dwFilter);
2776 return FALSE;
2780 /*************************************************************************
2781 * ImageList_SetFlags [COMCTL32.@]
2783 * Sets the image list flags.
2785 * PARAMS
2786 * himl [I] Handle to image list
2787 * flags [I] Flags to set
2789 * RETURNS
2790 * Old flags?
2792 * BUGS
2793 * Stub.
2796 DWORD WINAPI
2797 ImageList_SetFlags(HIMAGELIST himl, DWORD flags)
2799 FIXME("(%p %08x):empty stub\n", himl, flags);
2800 return 0;
2804 /*************************************************************************
2805 * ImageList_SetIconSize [COMCTL32.@]
2807 * Sets the image size of the bitmap and deletes all images.
2809 * PARAMS
2810 * himl [I] handle to image list
2811 * cx [I] image width
2812 * cy [I] image height
2814 * RETURNS
2815 * Success: TRUE
2816 * Failure: FALSE
2819 BOOL WINAPI
2820 ImageList_SetIconSize (HIMAGELIST himl, INT cx, INT cy)
2822 INT nCount;
2823 HBITMAP hbmNew;
2825 if (!is_valid(himl))
2826 return FALSE;
2828 /* remove all images */
2829 himl->cMaxImage = himl->cInitial + 1;
2830 himl->cCurImage = 0;
2831 himl->cx = cx;
2832 himl->cy = cy;
2834 /* initialize overlay mask indices */
2835 for (nCount = 0; nCount < MAX_OVERLAYIMAGE; nCount++)
2836 himl->nOvlIdx[nCount] = -1;
2838 hbmNew = ImageList_CreateImage(himl->hdcImage, himl, himl->cMaxImage);
2839 SelectObject (himl->hdcImage, hbmNew);
2840 DeleteObject (himl->hbmImage);
2841 himl->hbmImage = hbmNew;
2843 if (himl->hbmMask) {
2844 SIZE sz;
2845 imagelist_get_bitmap_size(himl, himl->cMaxImage, &sz);
2846 hbmNew = CreateBitmap (sz.cx, sz.cy, 1, 1, NULL);
2847 SelectObject (himl->hdcMask, hbmNew);
2848 DeleteObject (himl->hbmMask);
2849 himl->hbmMask = hbmNew;
2852 return TRUE;
2856 /*************************************************************************
2857 * ImageList_SetImageCount [COMCTL32.@]
2859 * Resizes an image list to the specified number of images.
2861 * PARAMS
2862 * himl [I] handle to image list
2863 * iImageCount [I] number of images in the image list
2865 * RETURNS
2866 * Success: TRUE
2867 * Failure: FALSE
2870 BOOL WINAPI
2871 ImageList_SetImageCount (HIMAGELIST himl, UINT iImageCount)
2873 HDC hdcBitmap;
2874 HBITMAP hbmNewBitmap, hbmOld;
2875 INT nNewCount, nCopyCount;
2877 TRACE("%p %d\n",himl,iImageCount);
2879 if (!is_valid(himl))
2880 return FALSE;
2882 nNewCount = iImageCount + 1;
2883 nCopyCount = min(himl->cCurImage, iImageCount);
2885 hdcBitmap = CreateCompatibleDC (0);
2887 hbmNewBitmap = ImageList_CreateImage(hdcBitmap, himl, nNewCount);
2889 if (hbmNewBitmap != 0)
2891 hbmOld = SelectObject (hdcBitmap, hbmNewBitmap);
2892 imagelist_copy_images( himl, himl->hdcImage, hdcBitmap, 0, nCopyCount, 0 );
2893 SelectObject (hdcBitmap, hbmOld);
2895 /* FIXME: delete 'empty' image space? */
2897 SelectObject (himl->hdcImage, hbmNewBitmap);
2898 DeleteObject (himl->hbmImage);
2899 himl->hbmImage = hbmNewBitmap;
2901 else
2902 ERR("Could not create new image bitmap!\n");
2904 if (himl->hbmMask)
2906 SIZE sz;
2907 imagelist_get_bitmap_size( himl, nNewCount, &sz );
2908 hbmNewBitmap = CreateBitmap (sz.cx, sz.cy, 1, 1, NULL);
2909 if (hbmNewBitmap != 0)
2911 hbmOld = SelectObject (hdcBitmap, hbmNewBitmap);
2912 imagelist_copy_images( himl, himl->hdcMask, hdcBitmap, 0, nCopyCount, 0 );
2913 SelectObject (hdcBitmap, hbmOld);
2915 /* FIXME: delete 'empty' image space? */
2917 SelectObject (himl->hdcMask, hbmNewBitmap);
2918 DeleteObject (himl->hbmMask);
2919 himl->hbmMask = hbmNewBitmap;
2921 else
2922 ERR("Could not create new mask bitmap!\n");
2925 DeleteDC (hdcBitmap);
2927 if (himl->has_alpha)
2929 char *new_alpha = HeapReAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, himl->has_alpha, nNewCount );
2930 if (new_alpha) himl->has_alpha = new_alpha;
2931 else
2933 HeapFree( GetProcessHeap(), 0, himl->has_alpha );
2934 himl->has_alpha = NULL;
2938 /* Update max image count and current image count */
2939 himl->cMaxImage = nNewCount;
2940 himl->cCurImage = iImageCount;
2942 return TRUE;
2946 /*************************************************************************
2947 * ImageList_SetOverlayImage [COMCTL32.@]
2949 * Assigns an overlay mask index to an existing image in an image list.
2951 * PARAMS
2952 * himl [I] handle to image list
2953 * iImage [I] image index
2954 * iOverlay [I] overlay mask index
2956 * RETURNS
2957 * Success: TRUE
2958 * Failure: FALSE
2961 BOOL WINAPI
2962 ImageList_SetOverlayImage (HIMAGELIST himl, INT iImage, INT iOverlay)
2964 if (!is_valid(himl))
2965 return FALSE;
2966 if ((iOverlay < 1) || (iOverlay > MAX_OVERLAYIMAGE))
2967 return FALSE;
2968 if ((iImage!=-1) && ((iImage < 0) || (iImage > himl->cCurImage)))
2969 return FALSE;
2970 himl->nOvlIdx[iOverlay - 1] = iImage;
2971 return TRUE;
2976 /* helper for ImageList_Write - write bitmap to pstm
2977 * currently everything is written as 24 bit RGB, except masks
2979 static BOOL
2980 _write_bitmap(HBITMAP hBitmap, LPSTREAM pstm)
2982 LPBITMAPFILEHEADER bmfh;
2983 LPBITMAPINFOHEADER bmih;
2984 LPBYTE data = NULL, lpBits;
2985 BITMAP bm;
2986 INT bitCount, sizeImage, offBits, totalSize;
2987 HDC xdc;
2988 BOOL result = FALSE;
2990 if (!GetObjectW(hBitmap, sizeof(BITMAP), &bm))
2991 return FALSE;
2993 bitCount = bm.bmBitsPixel;
2994 sizeImage = get_dib_stride(bm.bmWidth, bitCount) * bm.bmHeight;
2996 totalSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
2997 if(bitCount <= 8)
2998 totalSize += (1 << bitCount) * sizeof(RGBQUAD);
2999 offBits = totalSize;
3000 totalSize += sizeImage;
3002 data = Alloc(totalSize);
3003 bmfh = (LPBITMAPFILEHEADER)data;
3004 bmih = (LPBITMAPINFOHEADER)(data + sizeof(BITMAPFILEHEADER));
3005 lpBits = data + offBits;
3007 /* setup BITMAPFILEHEADER */
3008 bmfh->bfType = (('M' << 8) | 'B');
3009 bmfh->bfSize = offBits;
3010 bmfh->bfReserved1 = 0;
3011 bmfh->bfReserved2 = 0;
3012 bmfh->bfOffBits = offBits;
3014 /* setup BITMAPINFOHEADER */
3015 bmih->biSize = sizeof(BITMAPINFOHEADER);
3016 bmih->biWidth = bm.bmWidth;
3017 bmih->biHeight = bm.bmHeight;
3018 bmih->biPlanes = 1;
3019 bmih->biBitCount = bitCount;
3020 bmih->biCompression = BI_RGB;
3021 bmih->biSizeImage = sizeImage;
3022 bmih->biXPelsPerMeter = 0;
3023 bmih->biYPelsPerMeter = 0;
3024 bmih->biClrUsed = 0;
3025 bmih->biClrImportant = 0;
3027 xdc = GetDC(0);
3028 result = GetDIBits(xdc, hBitmap, 0, bm.bmHeight, lpBits, (BITMAPINFO *)bmih, DIB_RGB_COLORS) == bm.bmHeight;
3029 ReleaseDC(0, xdc);
3030 if (!result)
3031 goto failed;
3033 TRACE("width %u, height %u, planes %u, bpp %u\n",
3034 bmih->biWidth, bmih->biHeight,
3035 bmih->biPlanes, bmih->biBitCount);
3037 if(FAILED(IStream_Write(pstm, data, totalSize, NULL)))
3038 goto failed;
3040 result = TRUE;
3042 failed:
3043 Free(data);
3045 return result;
3049 /*************************************************************************
3050 * ImageList_Write [COMCTL32.@]
3052 * Writes an image list to a stream.
3054 * PARAMS
3055 * himl [I] handle to image list
3056 * pstm [O] Pointer to a stream.
3058 * RETURNS
3059 * Success: TRUE
3060 * Failure: FALSE
3062 * BUGS
3063 * probably.
3066 BOOL WINAPI
3067 ImageList_Write (HIMAGELIST himl, LPSTREAM pstm)
3069 ILHEAD ilHead;
3070 int i;
3072 TRACE("%p %p\n", himl, pstm);
3074 if (!is_valid(himl))
3075 return FALSE;
3077 ilHead.usMagic = (('L' << 8) | 'I');
3078 ilHead.usVersion = 0x101;
3079 ilHead.cCurImage = himl->cCurImage;
3080 ilHead.cMaxImage = himl->cMaxImage;
3081 ilHead.cGrow = himl->cGrow;
3082 ilHead.cx = himl->cx;
3083 ilHead.cy = himl->cy;
3084 ilHead.bkcolor = himl->clrBk;
3085 ilHead.flags = himl->flags;
3086 for(i = 0; i < 4; i++) {
3087 ilHead.ovls[i] = himl->nOvlIdx[i];
3090 TRACE("cx %u, cy %u, flags 0x04%x, cCurImage %u, cMaxImage %u\n",
3091 ilHead.cx, ilHead.cy, ilHead.flags, ilHead.cCurImage, ilHead.cMaxImage);
3093 if(FAILED(IStream_Write(pstm, &ilHead, sizeof(ILHEAD), NULL)))
3094 return FALSE;
3096 /* write the bitmap */
3097 if(!_write_bitmap(himl->hbmImage, pstm))
3098 return FALSE;
3100 /* write the mask if we have one */
3101 if(himl->flags & ILC_MASK) {
3102 if(!_write_bitmap(himl->hbmMask, pstm))
3103 return FALSE;
3106 return TRUE;
3110 static HBITMAP ImageList_CreateImage(HDC hdc, HIMAGELIST himl, UINT count)
3112 HBITMAP hbmNewBitmap;
3113 UINT ilc = (himl->flags & 0xFE);
3114 SIZE sz;
3116 imagelist_get_bitmap_size( himl, count, &sz );
3118 if ((ilc >= ILC_COLOR4 && ilc <= ILC_COLOR32) || ilc == ILC_COLOR)
3120 char buffer[sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD)];
3121 BITMAPINFO *bmi = (BITMAPINFO *)buffer;
3123 TRACE("Creating DIBSection %d x %d, %d Bits per Pixel\n",
3124 sz.cx, sz.cy, himl->uBitsPixel);
3126 memset( buffer, 0, sizeof(buffer) );
3127 bmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
3128 bmi->bmiHeader.biWidth = sz.cx;
3129 bmi->bmiHeader.biHeight = sz.cy;
3130 bmi->bmiHeader.biPlanes = 1;
3131 bmi->bmiHeader.biBitCount = himl->uBitsPixel;
3132 bmi->bmiHeader.biCompression = BI_RGB;
3134 if (himl->uBitsPixel <= ILC_COLOR8)
3136 /* retrieve the default color map */
3137 HBITMAP tmp = CreateBitmap( 1, 1, 1, 1, NULL );
3138 GetDIBits( hdc, tmp, 0, 0, NULL, bmi, DIB_RGB_COLORS );
3139 DeleteObject( tmp );
3141 hbmNewBitmap = CreateDIBSection(hdc, bmi, DIB_RGB_COLORS, NULL, 0, 0);
3143 else /*if (ilc == ILC_COLORDDB)*/
3145 TRACE("Creating Bitmap: %d Bits per Pixel\n", himl->uBitsPixel);
3147 hbmNewBitmap = CreateBitmap (sz.cx, sz.cy, 1, himl->uBitsPixel, NULL);
3149 TRACE("returning %p\n", hbmNewBitmap);
3150 return hbmNewBitmap;
3153 /*************************************************************************
3154 * ImageList_SetColorTable [COMCTL32.@]
3156 * Sets the color table of an image list.
3158 * PARAMS
3159 * himl [I] Handle to the image list.
3160 * uStartIndex [I] The first index to set.
3161 * cEntries [I] Number of entries to set.
3162 * prgb [I] New color information for color table for the image list.
3164 * RETURNS
3165 * Success: Number of entries in the table that were set.
3166 * Failure: Zero.
3168 * SEE
3169 * ImageList_Create(), SetDIBColorTable()
3172 UINT WINAPI
3173 ImageList_SetColorTable(HIMAGELIST himl, UINT uStartIndex, UINT cEntries, const RGBQUAD *prgb)
3175 return SetDIBColorTable(himl->hdcImage, uStartIndex, cEntries, prgb);
3178 /*************************************************************************
3179 * ImageList_CoCreateInstance [COMCTL32.@]
3181 * Creates a new imagelist instance and returns an interface pointer to it.
3183 * PARAMS
3184 * rclsid [I] A reference to the CLSID (CLSID_ImageList).
3185 * punkOuter [I] Pointer to IUnknown interface for aggregation, if desired
3186 * riid [I] Identifier of the requested interface.
3187 * ppv [O] Returns the address of the pointer requested, or NULL.
3189 * RETURNS
3190 * Success: S_OK.
3191 * Failure: Error value.
3193 HRESULT WINAPI
3194 ImageList_CoCreateInstance (REFCLSID rclsid, const IUnknown *punkOuter, REFIID riid, void **ppv)
3196 TRACE("(%s,%p,%s,%p)\n", debugstr_guid(rclsid), punkOuter, debugstr_guid(riid), ppv);
3198 if (!IsEqualCLSID(&CLSID_ImageList, rclsid))
3199 return E_NOINTERFACE;
3201 return ImageListImpl_CreateInstance(punkOuter, riid, ppv);
3205 /*************************************************************************
3206 * IImageList implementation
3209 static HRESULT WINAPI ImageListImpl_QueryInterface(IImageList2 *iface,
3210 REFIID iid, void **ppv)
3212 HIMAGELIST imgl = impl_from_IImageList2(iface);
3214 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
3216 if (!ppv) return E_INVALIDARG;
3218 if (IsEqualIID(&IID_IUnknown, iid) ||
3219 IsEqualIID(&IID_IImageList, iid) ||
3220 IsEqualIID(&IID_IImageList2, iid))
3222 *ppv = &imgl->IImageList2_iface;
3224 else
3226 *ppv = NULL;
3227 return E_NOINTERFACE;
3230 IImageList2_AddRef(iface);
3231 return S_OK;
3234 static ULONG WINAPI ImageListImpl_AddRef(IImageList2 *iface)
3236 HIMAGELIST imgl = impl_from_IImageList2(iface);
3237 ULONG ref = InterlockedIncrement(&imgl->ref);
3239 TRACE("(%p) refcount=%u\n", iface, ref);
3240 return ref;
3243 static ULONG WINAPI ImageListImpl_Release(IImageList2 *iface)
3245 HIMAGELIST This = impl_from_IImageList2(iface);
3246 ULONG ref = InterlockedDecrement(&This->ref);
3248 TRACE("(%p) refcount=%u\n", iface, ref);
3250 if (ref == 0)
3252 /* delete image bitmaps */
3253 if (This->hbmImage) DeleteObject (This->hbmImage);
3254 if (This->hbmMask) DeleteObject (This->hbmMask);
3256 /* delete image & mask DCs */
3257 if (This->hdcImage) DeleteDC (This->hdcImage);
3258 if (This->hdcMask) DeleteDC (This->hdcMask);
3260 /* delete blending brushes */
3261 if (This->hbrBlend25) DeleteObject (This->hbrBlend25);
3262 if (This->hbrBlend50) DeleteObject (This->hbrBlend50);
3264 This->IImageList2_iface.lpVtbl = NULL;
3265 HeapFree(GetProcessHeap(), 0, This->has_alpha);
3266 HeapFree(GetProcessHeap(), 0, This);
3269 return ref;
3272 static HRESULT WINAPI ImageListImpl_Add(IImageList2 *iface, HBITMAP hbmImage,
3273 HBITMAP hbmMask, int *pi)
3275 HIMAGELIST imgl = impl_from_IImageList2(iface);
3276 int ret;
3278 if (!pi)
3279 return E_FAIL;
3281 ret = ImageList_Add(imgl, hbmImage, hbmMask);
3283 if (ret == -1)
3284 return E_FAIL;
3286 *pi = ret;
3287 return S_OK;
3290 static HRESULT WINAPI ImageListImpl_ReplaceIcon(IImageList2 *iface, int i,
3291 HICON hicon, int *pi)
3293 HIMAGELIST imgl = impl_from_IImageList2(iface);
3294 int ret;
3296 if (!pi)
3297 return E_FAIL;
3299 ret = ImageList_ReplaceIcon(imgl, i, hicon);
3301 if (ret == -1)
3302 return E_FAIL;
3304 *pi = ret;
3305 return S_OK;
3308 static HRESULT WINAPI ImageListImpl_SetOverlayImage(IImageList2 *iface,
3309 int iImage, int iOverlay)
3311 HIMAGELIST imgl = impl_from_IImageList2(iface);
3312 return ImageList_SetOverlayImage(imgl, iImage, iOverlay) ? S_OK : E_FAIL;
3315 static HRESULT WINAPI ImageListImpl_Replace(IImageList2 *iface, int i,
3316 HBITMAP hbmImage, HBITMAP hbmMask)
3318 HIMAGELIST imgl = impl_from_IImageList2(iface);
3319 return ImageList_Replace(imgl, i, hbmImage, hbmMask) ? S_OK : E_FAIL;
3322 static HRESULT WINAPI ImageListImpl_AddMasked(IImageList2 *iface, HBITMAP hbmImage,
3323 COLORREF crMask, int *pi)
3325 HIMAGELIST imgl = impl_from_IImageList2(iface);
3326 int ret;
3328 if (!pi)
3329 return E_FAIL;
3331 ret = ImageList_AddMasked(imgl, hbmImage, crMask);
3333 if (ret == -1)
3334 return E_FAIL;
3336 *pi = ret;
3337 return S_OK;
3340 static HRESULT WINAPI ImageListImpl_Draw(IImageList2 *iface,
3341 IMAGELISTDRAWPARAMS *pimldp)
3343 HIMAGELIST imgl = impl_from_IImageList2(iface);
3344 HIMAGELIST old_himl;
3345 int ret;
3347 /* As far as I can tell, Windows simply ignores the contents of pimldp->himl
3348 so we shall simulate the same */
3349 old_himl = pimldp->himl;
3350 pimldp->himl = imgl;
3352 ret = ImageList_DrawIndirect(pimldp);
3354 pimldp->himl = old_himl;
3355 return ret ? S_OK : E_INVALIDARG;
3358 static HRESULT WINAPI ImageListImpl_Remove(IImageList2 *iface, int i)
3360 HIMAGELIST imgl = impl_from_IImageList2(iface);
3361 return (ImageList_Remove(imgl, i) == 0) ? E_INVALIDARG : S_OK;
3364 static HRESULT WINAPI ImageListImpl_GetIcon(IImageList2 *iface, int i, UINT flags,
3365 HICON *picon)
3367 HIMAGELIST imgl = impl_from_IImageList2(iface);
3368 HICON hIcon;
3370 if (!picon)
3371 return E_FAIL;
3373 hIcon = ImageList_GetIcon(imgl, i, flags);
3375 if (hIcon == NULL)
3376 return E_FAIL;
3378 *picon = hIcon;
3379 return S_OK;
3382 static HRESULT WINAPI ImageListImpl_GetImageInfo(IImageList2 *iface, int i,
3383 IMAGEINFO *pImageInfo)
3385 HIMAGELIST imgl = impl_from_IImageList2(iface);
3386 return ImageList_GetImageInfo(imgl, i, pImageInfo) ? S_OK : E_FAIL;
3389 static HRESULT WINAPI ImageListImpl_Copy(IImageList2 *iface, int dst_index,
3390 IUnknown *unk_src, int src_index, UINT flags)
3392 HIMAGELIST imgl = impl_from_IImageList2(iface);
3393 IImageList *src = NULL;
3394 HRESULT ret;
3396 if (!unk_src)
3397 return E_FAIL;
3399 /* TODO: Add test for IID_ImageList2 too */
3400 if (FAILED(IUnknown_QueryInterface(unk_src, &IID_IImageList,
3401 (void **) &src)))
3402 return E_FAIL;
3404 if (ImageList_Copy(imgl, dst_index, (HIMAGELIST) src, src_index, flags))
3405 ret = S_OK;
3406 else
3407 ret = E_FAIL;
3409 IImageList_Release(src);
3410 return ret;
3413 static HRESULT WINAPI ImageListImpl_Merge(IImageList2 *iface, int i1,
3414 IUnknown *punk2, int i2, int dx, int dy, REFIID riid, void **ppv)
3416 HIMAGELIST imgl = impl_from_IImageList2(iface);
3417 IImageList *iml2 = NULL;
3418 HIMAGELIST merged;
3419 HRESULT ret = E_FAIL;
3421 TRACE("(%p)->(%d %p %d %d %d %s %p)\n", iface, i1, punk2, i2, dx, dy, debugstr_guid(riid), ppv);
3423 /* TODO: Add test for IID_ImageList2 too */
3424 if (FAILED(IUnknown_QueryInterface(punk2, &IID_IImageList,
3425 (void **) &iml2)))
3426 return E_FAIL;
3428 merged = ImageList_Merge(imgl, i1, (HIMAGELIST) iml2, i2, dx, dy);
3430 /* Get the interface for the new image list */
3431 if (merged)
3433 ret = HIMAGELIST_QueryInterface(merged, riid, ppv);
3434 ImageList_Destroy(merged);
3437 IImageList_Release(iml2);
3438 return ret;
3441 static HRESULT WINAPI ImageListImpl_Clone(IImageList2 *iface, REFIID riid, void **ppv)
3443 HIMAGELIST imgl = impl_from_IImageList2(iface);
3444 HIMAGELIST clone;
3445 HRESULT ret = E_FAIL;
3447 TRACE("(%p)->(%s %p)\n", iface, debugstr_guid(riid), ppv);
3449 clone = ImageList_Duplicate(imgl);
3451 /* Get the interface for the new image list */
3452 if (clone)
3454 ret = HIMAGELIST_QueryInterface(clone, riid, ppv);
3455 ImageList_Destroy(clone);
3458 return ret;
3461 static HRESULT WINAPI ImageListImpl_GetImageRect(IImageList2 *iface, int i,
3462 RECT *prc)
3464 HIMAGELIST imgl = impl_from_IImageList2(iface);
3465 IMAGEINFO info;
3467 if (!prc)
3468 return E_FAIL;
3470 if (!ImageList_GetImageInfo(imgl, i, &info))
3471 return E_FAIL;
3473 return CopyRect(prc, &info.rcImage) ? S_OK : E_FAIL;
3476 static HRESULT WINAPI ImageListImpl_GetIconSize(IImageList2 *iface, int *cx,
3477 int *cy)
3479 HIMAGELIST imgl = impl_from_IImageList2(iface);
3480 return ImageList_GetIconSize(imgl, cx, cy) ? S_OK : E_INVALIDARG;
3483 static HRESULT WINAPI ImageListImpl_SetIconSize(IImageList2 *iface, int cx,
3484 int cy)
3486 HIMAGELIST imgl = impl_from_IImageList2(iface);
3487 return ImageList_SetIconSize(imgl, cx, cy) ? S_OK : E_FAIL;
3490 static HRESULT WINAPI ImageListImpl_GetImageCount(IImageList2 *iface, int *pi)
3492 HIMAGELIST imgl = impl_from_IImageList2(iface);
3493 *pi = ImageList_GetImageCount(imgl);
3494 return S_OK;
3497 static HRESULT WINAPI ImageListImpl_SetImageCount(IImageList2 *iface, UINT count)
3499 HIMAGELIST imgl = impl_from_IImageList2(iface);
3500 return ImageList_SetImageCount(imgl, count) ? S_OK : E_FAIL;
3503 static HRESULT WINAPI ImageListImpl_SetBkColor(IImageList2 *iface, COLORREF clrBk,
3504 COLORREF *pclr)
3506 HIMAGELIST imgl = impl_from_IImageList2(iface);
3507 *pclr = ImageList_SetBkColor(imgl, clrBk);
3508 return S_OK;
3511 static HRESULT WINAPI ImageListImpl_GetBkColor(IImageList2 *iface, COLORREF *pclr)
3513 HIMAGELIST imgl = impl_from_IImageList2(iface);
3514 *pclr = ImageList_GetBkColor(imgl);
3515 return S_OK;
3518 static HRESULT WINAPI ImageListImpl_BeginDrag(IImageList2 *iface, int iTrack,
3519 int dxHotspot, int dyHotspot)
3521 HIMAGELIST imgl = impl_from_IImageList2(iface);
3522 return ImageList_BeginDrag(imgl, iTrack, dxHotspot, dyHotspot) ? S_OK : E_FAIL;
3525 static HRESULT WINAPI ImageListImpl_EndDrag(IImageList2 *iface)
3527 ImageList_EndDrag();
3528 return S_OK;
3531 static HRESULT WINAPI ImageListImpl_DragEnter(IImageList2 *iface, HWND hwndLock,
3532 int x, int y)
3534 return ImageList_DragEnter(hwndLock, x, y) ? S_OK : E_FAIL;
3537 static HRESULT WINAPI ImageListImpl_DragLeave(IImageList2 *iface, HWND hwndLock)
3539 return ImageList_DragLeave(hwndLock) ? S_OK : E_FAIL;
3542 static HRESULT WINAPI ImageListImpl_DragMove(IImageList2 *iface, int x, int y)
3544 return ImageList_DragMove(x, y) ? S_OK : E_FAIL;
3547 static HRESULT WINAPI ImageListImpl_SetDragCursorImage(IImageList2 *iface,
3548 IUnknown *punk, int iDrag, int dxHotspot, int dyHotspot)
3550 IImageList *iml2 = NULL;
3551 HRESULT ret;
3553 if (!punk)
3554 return E_FAIL;
3556 /* TODO: Add test for IID_ImageList2 too */
3557 if (FAILED(IUnknown_QueryInterface(punk, &IID_IImageList,
3558 (void **) &iml2)))
3559 return E_FAIL;
3561 ret = ImageList_SetDragCursorImage((HIMAGELIST) iml2, iDrag, dxHotspot,
3562 dyHotspot);
3564 IImageList_Release(iml2);
3566 return ret ? S_OK : E_FAIL;
3569 static HRESULT WINAPI ImageListImpl_DragShowNolock(IImageList2 *iface, BOOL fShow)
3571 return ImageList_DragShowNolock(fShow) ? S_OK : E_FAIL;
3574 static HRESULT WINAPI ImageListImpl_GetDragImage(IImageList2 *iface, POINT *ppt,
3575 POINT *pptHotspot, REFIID riid, PVOID *ppv)
3577 HRESULT ret = E_FAIL;
3578 HIMAGELIST hNew;
3580 if (!ppv)
3581 return E_FAIL;
3583 hNew = ImageList_GetDragImage(ppt, pptHotspot);
3585 /* Get the interface for the new image list */
3586 if (hNew)
3588 IImageList *idrag = (IImageList*)hNew;
3590 ret = HIMAGELIST_QueryInterface(hNew, riid, ppv);
3591 IImageList_Release(idrag);
3594 return ret;
3597 static HRESULT WINAPI ImageListImpl_GetItemFlags(IImageList2 *iface, int i,
3598 DWORD *dwFlags)
3600 FIXME("STUB: %p %d %p\n", iface, i, dwFlags);
3601 return E_NOTIMPL;
3604 static HRESULT WINAPI ImageListImpl_GetOverlayImage(IImageList2 *iface, int iOverlay,
3605 int *piIndex)
3607 HIMAGELIST This = impl_from_IImageList2(iface);
3608 int i;
3610 if ((iOverlay < 0) || (iOverlay > This->cCurImage))
3611 return E_FAIL;
3613 for (i = 0; i < MAX_OVERLAYIMAGE; i++)
3615 if (This->nOvlIdx[i] == iOverlay)
3617 *piIndex = i + 1;
3618 return S_OK;
3622 return E_FAIL;
3625 static HRESULT WINAPI ImageListImpl_Resize(IImageList2 *iface, INT cx, INT cy)
3627 FIXME("(%p)->(%d %d): stub\n", iface, cx, cy);
3628 return E_NOTIMPL;
3631 static HRESULT WINAPI ImageListImpl_GetOriginalSize(IImageList2 *iface, INT image, DWORD flags, INT *cx, INT *cy)
3633 FIXME("(%p)->(%d %x %p %p): stub\n", iface, image, flags, cx, cy);
3634 return E_NOTIMPL;
3637 static HRESULT WINAPI ImageListImpl_SetOriginalSize(IImageList2 *iface, INT image, INT cx, INT cy)
3639 FIXME("(%p)->(%d %d %d): stub\n", iface, image, cx, cy);
3640 return E_NOTIMPL;
3643 static HRESULT WINAPI ImageListImpl_SetCallback(IImageList2 *iface, IUnknown *callback)
3645 FIXME("(%p)->(%p): stub\n", iface, callback);
3646 return E_NOTIMPL;
3649 static HRESULT WINAPI ImageListImpl_GetCallback(IImageList2 *iface, REFIID riid, void **ppv)
3651 FIXME("(%p)->(%s %p): stub\n", iface, debugstr_guid(riid), ppv);
3652 return E_NOTIMPL;
3655 static HRESULT WINAPI ImageListImpl_ForceImagePresent(IImageList2 *iface, INT image, DWORD flags)
3657 FIXME("(%p)->(%d %x): stub\n", iface, image, flags);
3658 return E_NOTIMPL;
3661 static HRESULT WINAPI ImageListImpl_DiscardImages(IImageList2 *iface, INT first_image, INT last_image, DWORD flags)
3663 FIXME("(%p)->(%d %d %x): stub\n", iface, first_image, last_image, flags);
3664 return E_NOTIMPL;
3667 static HRESULT WINAPI ImageListImpl_PreloadImages(IImageList2 *iface, IMAGELISTDRAWPARAMS *params)
3669 FIXME("(%p)->(%p): stub\n", iface, params);
3670 return E_NOTIMPL;
3673 static HRESULT WINAPI ImageListImpl_GetStatistics(IImageList2 *iface, IMAGELISTSTATS *stats)
3675 FIXME("(%p)->(%p): stub\n", iface, stats);
3676 return E_NOTIMPL;
3679 static HRESULT WINAPI ImageListImpl_Initialize(IImageList2 *iface, INT cx, INT cy, UINT flags, INT initial, INT grow)
3681 FIXME("(%p)->(%d %d %d %d %d): stub\n", iface, cx, cy, flags, initial, grow);
3682 return E_NOTIMPL;
3685 static HRESULT WINAPI ImageListImpl_Replace2(IImageList2 *iface, INT i, HBITMAP image, HBITMAP mask, IUnknown *unk, DWORD flags)
3687 FIXME("(%p)->(%d %p %p %p %x): stub\n", iface, i, image, mask, unk, flags);
3688 return E_NOTIMPL;
3691 static HRESULT WINAPI ImageListImpl_ReplaceFromImageList(IImageList2 *iface, INT i, IImageList *imagelist, INT src,
3692 IUnknown *unk, DWORD flags)
3694 FIXME("(%p)->(%d %p %d %p %x): stub\n", iface, i, imagelist, src, unk, flags);
3695 return E_NOTIMPL;
3698 static const IImageList2Vtbl ImageListImpl_Vtbl = {
3699 ImageListImpl_QueryInterface,
3700 ImageListImpl_AddRef,
3701 ImageListImpl_Release,
3702 ImageListImpl_Add,
3703 ImageListImpl_ReplaceIcon,
3704 ImageListImpl_SetOverlayImage,
3705 ImageListImpl_Replace,
3706 ImageListImpl_AddMasked,
3707 ImageListImpl_Draw,
3708 ImageListImpl_Remove,
3709 ImageListImpl_GetIcon,
3710 ImageListImpl_GetImageInfo,
3711 ImageListImpl_Copy,
3712 ImageListImpl_Merge,
3713 ImageListImpl_Clone,
3714 ImageListImpl_GetImageRect,
3715 ImageListImpl_GetIconSize,
3716 ImageListImpl_SetIconSize,
3717 ImageListImpl_GetImageCount,
3718 ImageListImpl_SetImageCount,
3719 ImageListImpl_SetBkColor,
3720 ImageListImpl_GetBkColor,
3721 ImageListImpl_BeginDrag,
3722 ImageListImpl_EndDrag,
3723 ImageListImpl_DragEnter,
3724 ImageListImpl_DragLeave,
3725 ImageListImpl_DragMove,
3726 ImageListImpl_SetDragCursorImage,
3727 ImageListImpl_DragShowNolock,
3728 ImageListImpl_GetDragImage,
3729 ImageListImpl_GetItemFlags,
3730 ImageListImpl_GetOverlayImage,
3731 ImageListImpl_Resize,
3732 ImageListImpl_GetOriginalSize,
3733 ImageListImpl_SetOriginalSize,
3734 ImageListImpl_SetCallback,
3735 ImageListImpl_GetCallback,
3736 ImageListImpl_ForceImagePresent,
3737 ImageListImpl_DiscardImages,
3738 ImageListImpl_PreloadImages,
3739 ImageListImpl_GetStatistics,
3740 ImageListImpl_Initialize,
3741 ImageListImpl_Replace2,
3742 ImageListImpl_ReplaceFromImageList
3745 static BOOL is_valid(HIMAGELIST himl)
3747 BOOL valid;
3748 __TRY
3750 valid = himl && himl->IImageList2_iface.lpVtbl == &ImageListImpl_Vtbl;
3752 __EXCEPT_PAGE_FAULT
3754 valid = FALSE;
3756 __ENDTRY
3757 return valid;
3760 /*************************************************************************
3761 * HIMAGELIST_QueryInterface [COMCTL32.@]
3763 * Returns a pointer to an IImageList or IImageList2 object for the given
3764 * HIMAGELIST.
3766 * PARAMS
3767 * himl [I] Image list handle.
3768 * riid [I] Identifier of the requested interface.
3769 * ppv [O] Returns the address of the pointer requested, or NULL.
3771 * RETURNS
3772 * Success: S_OK.
3773 * Failure: Error value.
3775 HRESULT WINAPI
3776 HIMAGELIST_QueryInterface (HIMAGELIST himl, REFIID riid, void **ppv)
3778 TRACE("(%p,%s,%p)\n", himl, debugstr_guid(riid), ppv);
3779 return IImageList2_QueryInterface((IImageList2 *) himl, riid, ppv);
3782 static HRESULT ImageListImpl_CreateInstance(const IUnknown *pUnkOuter, REFIID iid, void** ppv)
3784 HIMAGELIST This;
3785 HRESULT ret;
3787 TRACE("(%p,%s,%p)\n", pUnkOuter, debugstr_guid(iid), ppv);
3789 *ppv = NULL;
3791 if (pUnkOuter) return CLASS_E_NOAGGREGATION;
3793 This = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct _IMAGELIST));
3794 if (!This) return E_OUTOFMEMORY;
3796 This->IImageList2_iface.lpVtbl = &ImageListImpl_Vtbl;
3797 This->ref = 1;
3799 ret = IImageList2_QueryInterface(&This->IImageList2_iface, iid, ppv);
3800 IImageList2_Release(&This->IImageList2_iface);
3802 return ret;