comctl32: Don't try to mask out the image for palette-based bitmaps.
[wine/multimedia.git] / dlls / comctl32 / imagelist.c
blob0914c091a4a6381ee85c0b7c4edd63d3ebb3f8c3
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 const struct IImageListVtbl *lpVtbl; /* 00: IImageList vtable */
66 INT cCurImage; /* 04: ImageCount */
67 INT cMaxImage; /* 08: maximages */
68 INT cGrow; /* 0C: cGrow */
69 INT cx; /* 10: cx */
70 INT cy; /* 14: cy */
71 DWORD x4;
72 UINT flags; /* 1C: flags */
73 COLORREF clrFg; /* 20: foreground color */
74 COLORREF clrBk; /* 24: background color */
77 HBITMAP hbmImage; /* 28: images Bitmap */
78 HBITMAP hbmMask; /* 2C: masks Bitmap */
79 HDC hdcImage; /* 30: images MemDC */
80 HDC hdcMask; /* 34: masks MemDC */
81 INT nOvlIdx[MAX_OVERLAYIMAGE]; /* 38: overlay images index */
83 /* not yet found out */
84 HBRUSH hbrBlend25;
85 HBRUSH hbrBlend50;
86 INT cInitial;
87 UINT uBitsPixel;
88 char *has_alpha;
90 LONG ref; /* reference count */
93 #define IMAGELIST_MAGIC 0x53414D58
95 /* Header used by ImageList_Read() and ImageList_Write() */
96 #include "pshpack2.h"
97 typedef struct _ILHEAD
99 USHORT usMagic;
100 USHORT usVersion;
101 WORD cCurImage;
102 WORD cMaxImage;
103 WORD cGrow;
104 WORD cx;
105 WORD cy;
106 COLORREF bkcolor;
107 WORD flags;
108 SHORT ovls[4];
109 } ILHEAD;
110 #include "poppack.h"
112 /* internal image list data used for Drag & Drop operations */
113 typedef struct
115 HWND hwnd;
116 HIMAGELIST himl;
117 /* 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, FALSE, 0 };
131 static HBITMAP ImageList_CreateImage(HDC hdc, HIMAGELIST himl, UINT count);
132 static HRESULT ImageListImpl_CreateInstance(const IUnknown *pUnkOuter, REFIID iid, void** ppv);
133 static inline BOOL is_valid(HIMAGELIST himl);
136 * An imagelist with N images is tiled like this:
138 * N/4 ->
140 * 4 048C..
141 * 159D..
142 * | 26AE.N
143 * V 37BF.
146 #define TILE_COUNT 4
148 static inline UINT imagelist_height( UINT count )
150 return ((count + TILE_COUNT - 1)/TILE_COUNT);
153 static inline void imagelist_point_from_index( HIMAGELIST himl, UINT index, LPPOINT pt )
155 pt->x = (index%TILE_COUNT) * himl->cx;
156 pt->y = (index/TILE_COUNT) * himl->cy;
159 static inline void imagelist_get_bitmap_size( HIMAGELIST himl, UINT count, SIZE *sz )
161 sz->cx = himl->cx * TILE_COUNT;
162 sz->cy = imagelist_height( count ) * himl->cy;
165 static inline int get_dib_stride( int width, int bpp )
167 return ((width * bpp + 31) >> 3) & ~3;
170 static inline int get_dib_image_size( const BITMAPINFO *info )
172 return get_dib_stride( info->bmiHeader.biWidth, info->bmiHeader.biBitCount )
173 * abs( info->bmiHeader.biHeight );
177 * imagelist_copy_images()
179 * Copies a block of count images from offset src in the list to offset dest.
180 * Images are copied a row at at time. Assumes hdcSrc and hdcDest are different.
182 static inline void imagelist_copy_images( HIMAGELIST himl, HDC hdcSrc, HDC hdcDest,
183 UINT src, UINT count, UINT dest )
185 POINT ptSrc, ptDest;
186 SIZE sz;
187 UINT i;
189 for ( i=0; i<TILE_COUNT; i++ )
191 imagelist_point_from_index( himl, src+i, &ptSrc );
192 imagelist_point_from_index( himl, dest+i, &ptDest );
193 sz.cx = himl->cx;
194 sz.cy = himl->cy * imagelist_height( count - i );
196 BitBlt( hdcDest, ptDest.x, ptDest.y, sz.cx, sz.cy,
197 hdcSrc, ptSrc.x, ptSrc.y, SRCCOPY );
201 static void add_dib_bits( HIMAGELIST himl, int pos, int count, int width, int height,
202 BITMAPINFO *info, BITMAPINFO *mask_info, DWORD *bits, BYTE *mask_bits )
204 int i, j, n;
205 POINT pt;
206 int stride = info->bmiHeader.biWidth;
207 int mask_stride = (info->bmiHeader.biWidth + 31) / 32 * 4;
209 for (n = 0; n < count; n++)
211 int has_alpha = 0;
213 imagelist_point_from_index( himl, pos + n, &pt );
215 /* check if bitmap has an alpha channel */
216 for (i = 0; i < height && !has_alpha; i++)
217 for (j = n * width; j < (n + 1) * width; j++)
218 if ((has_alpha = ((bits[i * stride + j] & 0xff000000) != 0))) break;
220 if (!has_alpha) /* generate alpha channel from the mask */
222 for (i = 0; i < height; i++)
223 for (j = n * width; j < (n + 1) * width; j++)
224 if (!mask_info || !((mask_bits[i * mask_stride + j / 8] << (j % 8)) & 0x80))
225 bits[i * stride + j] |= 0xff000000;
226 else
227 bits[i * stride + j] = 0;
229 else
231 himl->has_alpha[pos + n] = 1;
233 if (mask_info && himl->hbmMask) /* generate the mask from the alpha channel */
235 for (i = 0; i < height; i++)
236 for (j = n * width; j < (n + 1) * width; j++)
237 if ((bits[i * stride + j] >> 24) > 25) /* more than 10% alpha */
238 mask_bits[i * mask_stride + j / 8] &= ~(0x80 >> (j % 8));
239 else
240 mask_bits[i * mask_stride + j / 8] |= 0x80 >> (j % 8);
243 StretchDIBits( himl->hdcImage, pt.x, pt.y, himl->cx, himl->cy,
244 n * width, 0, width, height, bits, info, DIB_RGB_COLORS, SRCCOPY );
245 if (mask_info)
246 StretchDIBits( himl->hdcMask, pt.x, pt.y, himl->cx, himl->cy,
247 n * width, 0, width, height, mask_bits, mask_info, DIB_RGB_COLORS, SRCCOPY );
251 /* add images with an alpha channel when the image list is 32 bpp */
252 static BOOL add_with_alpha( HIMAGELIST himl, HDC hdc, int pos, int count,
253 int width, int height, HBITMAP hbmImage, HBITMAP hbmMask )
255 BOOL ret = FALSE;
256 BITMAP bm;
257 BITMAPINFO *info, *mask_info = NULL;
258 DWORD *bits = NULL;
259 BYTE *mask_bits = NULL;
260 DWORD mask_width;
262 if (!GetObjectW( hbmImage, sizeof(bm), &bm )) return FALSE;
264 /* if either the imagelist or the source bitmap don't have an alpha channel, bail out now */
265 if (!himl->has_alpha) return FALSE;
266 if (bm.bmBitsPixel != 32) return FALSE;
268 SelectObject( hdc, hbmImage );
269 mask_width = (bm.bmWidth + 31) / 32 * 4;
271 if (!(info = HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET( BITMAPINFO, bmiColors[256] )))) goto done;
272 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
273 info->bmiHeader.biWidth = bm.bmWidth;
274 info->bmiHeader.biHeight = -height;
275 info->bmiHeader.biPlanes = 1;
276 info->bmiHeader.biBitCount = 32;
277 info->bmiHeader.biCompression = BI_RGB;
278 info->bmiHeader.biSizeImage = bm.bmWidth * height * 4;
279 info->bmiHeader.biXPelsPerMeter = 0;
280 info->bmiHeader.biYPelsPerMeter = 0;
281 info->bmiHeader.biClrUsed = 0;
282 info->bmiHeader.biClrImportant = 0;
283 if (!(bits = HeapAlloc( GetProcessHeap(), 0, info->bmiHeader.biSizeImage ))) goto done;
284 if (!GetDIBits( hdc, hbmImage, 0, height, bits, info, DIB_RGB_COLORS )) goto done;
286 if (hbmMask)
288 if (!(mask_info = HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET( BITMAPINFO, bmiColors[2] ))))
289 goto done;
290 mask_info->bmiHeader = info->bmiHeader;
291 mask_info->bmiHeader.biBitCount = 1;
292 mask_info->bmiHeader.biSizeImage = mask_width * height;
293 if (!(mask_bits = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, info->bmiHeader.biSizeImage )))
294 goto done;
295 if (!GetDIBits( hdc, hbmMask, 0, height, mask_bits, mask_info, DIB_RGB_COLORS )) goto done;
298 add_dib_bits( himl, pos, count, width, height, info, mask_info, bits, mask_bits );
299 ret = TRUE;
301 done:
302 HeapFree( GetProcessHeap(), 0, info );
303 HeapFree( GetProcessHeap(), 0, mask_info );
304 HeapFree( GetProcessHeap(), 0, bits );
305 HeapFree( GetProcessHeap(), 0, mask_bits );
306 return ret;
309 /*************************************************************************
310 * IMAGELIST_InternalExpandBitmaps [Internal]
312 * Expands the bitmaps of an image list by the given number of images.
314 * PARAMS
315 * himl [I] handle to image list
316 * nImageCount [I] number of images to add
318 * RETURNS
319 * nothing
321 * NOTES
322 * This function CANNOT be used to reduce the number of images.
324 static void
325 IMAGELIST_InternalExpandBitmaps(HIMAGELIST himl, INT nImageCount)
327 HDC hdcBitmap;
328 HBITMAP hbmNewBitmap, hbmNull;
329 INT nNewCount;
330 SIZE sz;
332 TRACE("%p has allocated %d, max %d, grow %d images\n", himl, himl->cCurImage, himl->cMaxImage, himl->cGrow);
334 if (himl->cCurImage + nImageCount < himl->cMaxImage)
335 return;
337 nNewCount = himl->cMaxImage + max(nImageCount, himl->cGrow) + 1;
339 imagelist_get_bitmap_size(himl, nNewCount, &sz);
341 TRACE("Create expanded bitmaps : himl=%p x=%d y=%d count=%d\n", himl, sz.cx, sz.cy, nNewCount);
342 hdcBitmap = CreateCompatibleDC (0);
344 hbmNewBitmap = ImageList_CreateImage(hdcBitmap, himl, nNewCount);
346 if (hbmNewBitmap == 0)
347 ERR("creating new image bitmap (x=%d y=%d)!\n", sz.cx, sz.cy);
349 if (himl->cCurImage)
351 hbmNull = SelectObject (hdcBitmap, hbmNewBitmap);
352 BitBlt (hdcBitmap, 0, 0, sz.cx, sz.cy,
353 himl->hdcImage, 0, 0, SRCCOPY);
354 SelectObject (hdcBitmap, hbmNull);
356 SelectObject (himl->hdcImage, hbmNewBitmap);
357 DeleteObject (himl->hbmImage);
358 himl->hbmImage = hbmNewBitmap;
360 if (himl->flags & ILC_MASK)
362 hbmNewBitmap = CreateBitmap (sz.cx, sz.cy, 1, 1, NULL);
364 if (hbmNewBitmap == 0)
365 ERR("creating new mask bitmap!\n");
367 if(himl->cCurImage)
369 hbmNull = SelectObject (hdcBitmap, hbmNewBitmap);
370 BitBlt (hdcBitmap, 0, 0, sz.cx, sz.cy,
371 himl->hdcMask, 0, 0, SRCCOPY);
372 SelectObject (hdcBitmap, hbmNull);
374 SelectObject (himl->hdcMask, hbmNewBitmap);
375 DeleteObject (himl->hbmMask);
376 himl->hbmMask = hbmNewBitmap;
379 if (himl->has_alpha)
381 char *new_alpha = HeapReAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, himl->has_alpha, nNewCount );
382 if (new_alpha) himl->has_alpha = new_alpha;
383 else
385 HeapFree( GetProcessHeap(), 0, himl->has_alpha );
386 himl->has_alpha = NULL;
390 himl->cMaxImage = nNewCount;
392 DeleteDC (hdcBitmap);
396 /*************************************************************************
397 * ImageList_Add [COMCTL32.@]
399 * Add an image or images to an image list.
401 * PARAMS
402 * himl [I] handle to image list
403 * hbmImage [I] handle to image bitmap
404 * hbmMask [I] handle to mask bitmap
406 * RETURNS
407 * Success: Index of the first new image.
408 * Failure: -1
411 INT WINAPI
412 ImageList_Add (HIMAGELIST himl, HBITMAP hbmImage, HBITMAP hbmMask)
414 HDC hdcBitmap, hdcTemp = 0;
415 INT nFirstIndex, nImageCount, i;
416 BITMAP bmp;
417 POINT pt;
419 TRACE("himl=%p hbmimage=%p hbmmask=%p\n", himl, hbmImage, hbmMask);
420 if (!is_valid(himl))
421 return -1;
423 if (!GetObjectW(hbmImage, sizeof(BITMAP), &bmp))
424 return -1;
426 TRACE("himl %p, cCurImage %d, cMaxImage %d, cGrow %d, cx %d, cy %d\n",
427 himl, himl->cCurImage, himl->cMaxImage, himl->cGrow, himl->cx, himl->cy);
429 nImageCount = bmp.bmWidth / himl->cx;
431 TRACE("%p has %d images (%d x %d)\n", hbmImage, nImageCount, bmp.bmWidth, bmp.bmHeight);
433 IMAGELIST_InternalExpandBitmaps(himl, nImageCount);
435 hdcBitmap = CreateCompatibleDC(0);
437 SelectObject(hdcBitmap, hbmImage);
439 if (add_with_alpha( himl, hdcBitmap, himl->cCurImage, nImageCount,
440 himl->cx, min( himl->cy, bmp.bmHeight), hbmImage, hbmMask ))
441 goto done;
443 if (himl->hbmMask)
445 hdcTemp = CreateCompatibleDC(0);
446 SelectObject(hdcTemp, hbmMask);
449 for (i=0; i<nImageCount; i++)
451 imagelist_point_from_index( himl, himl->cCurImage + i, &pt );
453 /* Copy result to the imagelist
455 BitBlt( himl->hdcImage, pt.x, pt.y, himl->cx, bmp.bmHeight,
456 hdcBitmap, i*himl->cx, 0, SRCCOPY );
458 if (!himl->hbmMask)
459 continue;
461 BitBlt( himl->hdcMask, pt.x, pt.y, himl->cx, bmp.bmHeight,
462 hdcTemp, i*himl->cx, 0, SRCCOPY );
464 /* Remove the background from the image
466 BitBlt( himl->hdcImage, pt.x, pt.y, himl->cx, bmp.bmHeight,
467 himl->hdcMask, pt.x, pt.y, 0x220326 ); /* NOTSRCAND */
469 if (hdcTemp) DeleteDC(hdcTemp);
471 done:
472 DeleteDC(hdcBitmap);
474 nFirstIndex = himl->cCurImage;
475 himl->cCurImage += nImageCount;
477 return nFirstIndex;
481 /*************************************************************************
482 * ImageList_AddIcon [COMCTL32.@]
484 * Adds an icon to an image list.
486 * PARAMS
487 * himl [I] handle to image list
488 * hIcon [I] handle to icon
490 * RETURNS
491 * Success: index of the new image
492 * Failure: -1
494 #undef ImageList_AddIcon
495 INT WINAPI ImageList_AddIcon (HIMAGELIST himl, HICON hIcon)
497 return ImageList_ReplaceIcon (himl, -1, hIcon);
501 /*************************************************************************
502 * ImageList_AddMasked [COMCTL32.@]
504 * Adds an image or images to an image list and creates a mask from the
505 * specified bitmap using the mask color.
507 * PARAMS
508 * himl [I] handle to image list.
509 * hBitmap [I] handle to bitmap
510 * clrMask [I] mask color.
512 * RETURNS
513 * Success: Index of the first new image.
514 * Failure: -1
517 INT WINAPI
518 ImageList_AddMasked (HIMAGELIST himl, HBITMAP hBitmap, COLORREF clrMask)
520 HDC hdcMask, hdcBitmap;
521 INT ret;
522 BITMAP bmp;
523 HBITMAP hMaskBitmap;
524 COLORREF bkColor;
526 TRACE("himl=%p hbitmap=%p clrmask=%x\n", himl, hBitmap, clrMask);
527 if (!is_valid(himl))
528 return -1;
530 if (!GetObjectW(hBitmap, sizeof(BITMAP), &bmp))
531 return -1;
533 hdcBitmap = CreateCompatibleDC(0);
534 SelectObject(hdcBitmap, hBitmap);
536 /* Create a temp Mask so we can remove the background of the Image */
537 hdcMask = CreateCompatibleDC(0);
538 hMaskBitmap = CreateBitmap(bmp.bmWidth, bmp.bmHeight, 1, 1, NULL);
539 SelectObject(hdcMask, hMaskBitmap);
541 /* create monochrome image to the mask bitmap */
542 bkColor = (clrMask != CLR_DEFAULT) ? clrMask : GetPixel (hdcBitmap, 0, 0);
543 SetBkColor (hdcBitmap, bkColor);
544 BitBlt (hdcMask, 0, 0, bmp.bmWidth, bmp.bmHeight, hdcBitmap, 0, 0, SRCCOPY);
547 * Remove the background from the image
549 * WINDOWS BUG ALERT!!!!!!
550 * The statement below should not be done in common practice
551 * but this is how ImageList_AddMasked works in Windows.
552 * It overwrites the original bitmap passed, this was discovered
553 * by using the same bitmap to iterate the different styles
554 * on windows where it failed (BUT ImageList_Add is OK)
555 * This is here in case some apps rely on this bug
557 * Blt mode 0x220326 is NOTSRCAND
559 if (bmp.bmBitsPixel > 8) /* NOTSRCAND can't work with palettes */
561 SetBkColor(hdcBitmap, RGB(255,255,255));
562 BitBlt(hdcBitmap, 0, 0, bmp.bmWidth, bmp.bmHeight, hdcMask, 0, 0, 0x220326);
565 DeleteDC(hdcBitmap);
566 DeleteDC(hdcMask);
568 ret = ImageList_Add( himl, hBitmap, hMaskBitmap );
570 DeleteObject(hMaskBitmap);
571 return ret;
575 /*************************************************************************
576 * ImageList_BeginDrag [COMCTL32.@]
578 * Creates a temporary image list that contains one image. It will be used
579 * as a drag image.
581 * PARAMS
582 * himlTrack [I] handle to the source image list
583 * iTrack [I] index of the drag image in the source image list
584 * dxHotspot [I] X position of the hot spot of the drag image
585 * dyHotspot [I] Y position of the hot spot of the drag image
587 * RETURNS
588 * Success: TRUE
589 * Failure: FALSE
592 BOOL WINAPI
593 ImageList_BeginDrag (HIMAGELIST himlTrack, INT iTrack,
594 INT dxHotspot, INT dyHotspot)
596 INT cx, cy;
598 TRACE("(himlTrack=%p iTrack=%d dx=%d dy=%d)\n", himlTrack, iTrack,
599 dxHotspot, dyHotspot);
601 if (!is_valid(himlTrack))
602 return FALSE;
604 if (InternalDrag.himl)
605 ImageList_EndDrag ();
607 cx = himlTrack->cx;
608 cy = himlTrack->cy;
610 InternalDrag.himl = ImageList_Create (cx, cy, himlTrack->flags, 1, 1);
611 if (InternalDrag.himl == NULL) {
612 WARN("Error creating drag image list!\n");
613 return FALSE;
616 InternalDrag.dxHotspot = dxHotspot;
617 InternalDrag.dyHotspot = dyHotspot;
619 /* copy image */
620 BitBlt (InternalDrag.himl->hdcImage, 0, 0, cx, cy, himlTrack->hdcImage, iTrack * cx, 0, SRCCOPY);
622 /* copy mask */
623 BitBlt (InternalDrag.himl->hdcMask, 0, 0, cx, cy, himlTrack->hdcMask, iTrack * cx, 0, SRCCOPY);
625 InternalDrag.himl->cCurImage = 1;
627 return TRUE;
631 /*************************************************************************
632 * ImageList_Copy [COMCTL32.@]
634 * Copies an image of the source image list to an image of the
635 * destination image list. Images can be copied or swapped.
637 * PARAMS
638 * himlDst [I] handle to the destination image list
639 * iDst [I] destination image index.
640 * himlSrc [I] handle to the source image list
641 * iSrc [I] source image index
642 * uFlags [I] flags for the copy operation
644 * RETURNS
645 * Success: TRUE
646 * Failure: FALSE
648 * NOTES
649 * Copying from one image list to another is possible. The original
650 * implementation just copies or swaps within one image list.
651 * Could this feature become a bug??? ;-)
654 BOOL WINAPI
655 ImageList_Copy (HIMAGELIST himlDst, INT iDst, HIMAGELIST himlSrc,
656 INT iSrc, UINT uFlags)
658 POINT ptSrc, ptDst;
660 TRACE("himlDst=%p iDst=%d himlSrc=%p iSrc=%d\n", himlDst, iDst, himlSrc, iSrc);
662 if (!is_valid(himlSrc) || !is_valid(himlDst))
663 return FALSE;
664 if ((iDst < 0) || (iDst >= himlDst->cCurImage))
665 return FALSE;
666 if ((iSrc < 0) || (iSrc >= himlSrc->cCurImage))
667 return FALSE;
669 imagelist_point_from_index( himlDst, iDst, &ptDst );
670 imagelist_point_from_index( himlSrc, iSrc, &ptSrc );
672 if (uFlags & ILCF_SWAP) {
673 /* swap */
674 HDC hdcBmp;
675 HBITMAP hbmTempImage, hbmTempMask;
677 hdcBmp = CreateCompatibleDC (0);
679 /* create temporary bitmaps */
680 hbmTempImage = CreateBitmap (himlSrc->cx, himlSrc->cy, 1,
681 himlSrc->uBitsPixel, NULL);
682 hbmTempMask = CreateBitmap (himlSrc->cx, himlSrc->cy, 1,
683 1, NULL);
685 /* copy (and stretch) destination to temporary bitmaps.(save) */
686 /* image */
687 SelectObject (hdcBmp, hbmTempImage);
688 StretchBlt (hdcBmp, 0, 0, himlSrc->cx, himlSrc->cy,
689 himlDst->hdcImage, ptDst.x, ptDst.y, himlDst->cx, himlDst->cy,
690 SRCCOPY);
691 /* mask */
692 SelectObject (hdcBmp, hbmTempMask);
693 StretchBlt (hdcBmp, 0, 0, himlSrc->cx, himlSrc->cy,
694 himlDst->hdcMask, ptDst.x, ptDst.y, himlDst->cx, himlDst->cy,
695 SRCCOPY);
697 /* copy (and stretch) source to destination */
698 /* image */
699 StretchBlt (himlDst->hdcImage, ptDst.x, ptDst.y, himlDst->cx, himlDst->cy,
700 himlSrc->hdcImage, ptSrc.x, ptSrc.y, himlSrc->cx, himlSrc->cy,
701 SRCCOPY);
702 /* mask */
703 StretchBlt (himlDst->hdcMask, ptDst.x, ptDst.y, himlDst->cx, himlDst->cy,
704 himlSrc->hdcMask, ptSrc.x, ptSrc.y, himlSrc->cx, himlSrc->cy,
705 SRCCOPY);
707 /* copy (without stretching) temporary bitmaps to source (restore) */
708 /* mask */
709 BitBlt (himlSrc->hdcMask, ptSrc.x, ptSrc.y, himlSrc->cx, himlSrc->cy,
710 hdcBmp, 0, 0, SRCCOPY);
712 /* image */
713 BitBlt (himlSrc->hdcImage, ptSrc.x, ptSrc.y, himlSrc->cx, himlSrc->cy,
714 hdcBmp, 0, 0, SRCCOPY);
715 /* delete temporary bitmaps */
716 DeleteObject (hbmTempMask);
717 DeleteObject (hbmTempImage);
718 DeleteDC(hdcBmp);
720 else {
721 /* copy image */
722 StretchBlt (himlDst->hdcImage, ptDst.x, ptDst.y, himlDst->cx, himlDst->cy,
723 himlSrc->hdcImage, ptSrc.x, ptSrc.y, himlSrc->cx, himlSrc->cy,
724 SRCCOPY);
726 /* copy mask */
727 StretchBlt (himlDst->hdcMask, ptDst.x, ptDst.y, himlDst->cx, himlDst->cy,
728 himlSrc->hdcMask, ptSrc.x, ptSrc.y, himlSrc->cx, himlSrc->cy,
729 SRCCOPY);
732 return TRUE;
736 /*************************************************************************
737 * ImageList_Create [COMCTL32.@]
739 * Creates a new image list.
741 * PARAMS
742 * cx [I] image height
743 * cy [I] image width
744 * flags [I] creation flags
745 * cInitial [I] initial number of images in the image list
746 * cGrow [I] number of images by which image list grows
748 * RETURNS
749 * Success: Handle to the created image list
750 * Failure: NULL
752 HIMAGELIST WINAPI
753 ImageList_Create (INT cx, INT cy, UINT flags,
754 INT cInitial, INT cGrow)
756 HIMAGELIST himl;
757 INT nCount;
758 HBITMAP hbmTemp;
759 UINT ilc = (flags & 0xFE);
760 static const WORD aBitBlend25[] =
761 {0xAA, 0x00, 0x55, 0x00, 0xAA, 0x00, 0x55, 0x00};
763 static const WORD aBitBlend50[] =
764 {0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA};
766 TRACE("(%d %d 0x%x %d %d)\n", cx, cy, flags, cInitial, cGrow);
768 if (cx <= 0 || cy <= 0) return NULL;
770 /* Create the IImageList interface for the image list */
771 if (FAILED(ImageListImpl_CreateInstance(NULL, &IID_IImageList, (void **)&himl)))
772 return NULL;
774 cGrow = (cGrow < 4) ? 4 : (cGrow + 3) & ~3;
776 himl->cx = cx;
777 himl->cy = cy;
778 himl->flags = flags;
779 himl->cMaxImage = cInitial + 1;
780 himl->cInitial = cInitial;
781 himl->cGrow = cGrow;
782 himl->clrFg = CLR_DEFAULT;
783 himl->clrBk = CLR_NONE;
785 /* initialize overlay mask indices */
786 for (nCount = 0; nCount < MAX_OVERLAYIMAGE; nCount++)
787 himl->nOvlIdx[nCount] = -1;
789 /* Create Image & Mask DCs */
790 himl->hdcImage = CreateCompatibleDC (0);
791 if (!himl->hdcImage)
792 goto cleanup;
793 if (himl->flags & ILC_MASK){
794 himl->hdcMask = CreateCompatibleDC(0);
795 if (!himl->hdcMask)
796 goto cleanup;
799 /* Default to ILC_COLOR4 if none of the ILC_COLOR* flags are specified */
800 if (ilc == ILC_COLOR)
801 ilc = ILC_COLOR4;
803 if (ilc >= ILC_COLOR4 && ilc <= ILC_COLOR32)
804 himl->uBitsPixel = ilc;
805 else
806 himl->uBitsPixel = (UINT)GetDeviceCaps (himl->hdcImage, BITSPIXEL);
808 if (himl->cMaxImage > 0) {
809 himl->hbmImage = ImageList_CreateImage(himl->hdcImage, himl, himl->cMaxImage);
810 SelectObject(himl->hdcImage, himl->hbmImage);
811 } else
812 himl->hbmImage = 0;
814 if ((himl->cMaxImage > 0) && (himl->flags & ILC_MASK)) {
815 SIZE sz;
817 imagelist_get_bitmap_size(himl, himl->cMaxImage, &sz);
818 himl->hbmMask = CreateBitmap (sz.cx, sz.cy, 1, 1, NULL);
819 if (himl->hbmMask == 0) {
820 ERR("Error creating mask bitmap!\n");
821 goto cleanup;
823 SelectObject(himl->hdcMask, himl->hbmMask);
825 else
826 himl->hbmMask = 0;
828 if (ilc == ILC_COLOR32)
829 himl->has_alpha = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, himl->cMaxImage );
830 else
831 himl->has_alpha = NULL;
833 /* create blending brushes */
834 hbmTemp = CreateBitmap (8, 8, 1, 1, aBitBlend25);
835 himl->hbrBlend25 = CreatePatternBrush (hbmTemp);
836 DeleteObject (hbmTemp);
838 hbmTemp = CreateBitmap (8, 8, 1, 1, aBitBlend50);
839 himl->hbrBlend50 = CreatePatternBrush (hbmTemp);
840 DeleteObject (hbmTemp);
842 TRACE("created imagelist %p\n", himl);
843 return himl;
845 cleanup:
846 ImageList_Destroy(himl);
847 return NULL;
851 /*************************************************************************
852 * ImageList_Destroy [COMCTL32.@]
854 * Destroys an image list.
856 * PARAMS
857 * himl [I] handle to image list
859 * RETURNS
860 * Success: TRUE
861 * Failure: FALSE
864 BOOL WINAPI
865 ImageList_Destroy (HIMAGELIST himl)
867 if (!is_valid(himl))
868 return FALSE;
870 IImageList_Release((IImageList *) himl);
871 return TRUE;
875 /*************************************************************************
876 * ImageList_DragEnter [COMCTL32.@]
878 * Locks window update and displays the drag image at the given position.
880 * PARAMS
881 * hwndLock [I] handle of the window that owns the drag image.
882 * x [I] X position of the drag image.
883 * y [I] Y position of the drag image.
885 * RETURNS
886 * Success: TRUE
887 * Failure: FALSE
889 * NOTES
890 * The position of the drag image is relative to the window, not
891 * the client area.
894 BOOL WINAPI
895 ImageList_DragEnter (HWND hwndLock, INT x, INT y)
897 TRACE("(hwnd=%p x=%d y=%d)\n", hwndLock, x, y);
899 if (!is_valid(InternalDrag.himl))
900 return FALSE;
902 if (hwndLock)
903 InternalDrag.hwnd = hwndLock;
904 else
905 InternalDrag.hwnd = GetDesktopWindow ();
907 InternalDrag.x = x;
908 InternalDrag.y = y;
910 /* draw the drag image and save the background */
911 if (!ImageList_DragShowNolock(TRUE)) {
912 return FALSE;
915 return TRUE;
919 /*************************************************************************
920 * ImageList_DragLeave [COMCTL32.@]
922 * Unlocks window update and hides the drag image.
924 * PARAMS
925 * hwndLock [I] handle of the window that owns the drag image.
927 * RETURNS
928 * Success: TRUE
929 * Failure: FALSE
932 BOOL WINAPI
933 ImageList_DragLeave (HWND hwndLock)
935 /* As we don't save drag info in the window this can lead to problems if
936 an app does not supply the same window as DragEnter */
937 /* if (hwndLock)
938 InternalDrag.hwnd = hwndLock;
939 else
940 InternalDrag.hwnd = GetDesktopWindow (); */
941 if(!hwndLock)
942 hwndLock = GetDesktopWindow();
943 if(InternalDrag.hwnd != hwndLock)
944 FIXME("DragLeave hWnd != DragEnter hWnd\n");
946 ImageList_DragShowNolock (FALSE);
948 return TRUE;
952 /*************************************************************************
953 * ImageList_InternalDragDraw [Internal]
955 * Draws the drag image.
957 * PARAMS
958 * hdc [I] device context to draw into.
959 * x [I] X position of the drag image.
960 * y [I] Y position of the drag image.
962 * RETURNS
963 * Success: TRUE
964 * Failure: FALSE
966 * NOTES
967 * The position of the drag image is relative to the window, not
968 * the client area.
972 static inline void
973 ImageList_InternalDragDraw (HDC hdc, INT x, INT y)
975 IMAGELISTDRAWPARAMS imldp;
977 ZeroMemory (&imldp, sizeof(imldp));
978 imldp.cbSize = sizeof(imldp);
979 imldp.himl = InternalDrag.himl;
980 imldp.i = 0;
981 imldp.hdcDst = hdc,
982 imldp.x = x;
983 imldp.y = y;
984 imldp.rgbBk = CLR_DEFAULT;
985 imldp.rgbFg = CLR_DEFAULT;
986 imldp.fStyle = ILD_NORMAL;
987 imldp.fState = ILS_ALPHA;
988 imldp.Frame = 192;
989 ImageList_DrawIndirect (&imldp);
992 /*************************************************************************
993 * ImageList_DragMove [COMCTL32.@]
995 * Moves the drag image.
997 * PARAMS
998 * x [I] X position of the drag image.
999 * y [I] Y position of the drag image.
1001 * RETURNS
1002 * Success: TRUE
1003 * Failure: FALSE
1005 * NOTES
1006 * The position of the drag image is relative to the window, not
1007 * the client area.
1010 BOOL WINAPI
1011 ImageList_DragMove (INT x, INT y)
1013 TRACE("(x=%d y=%d)\n", x, y);
1015 if (!is_valid(InternalDrag.himl))
1016 return FALSE;
1018 /* draw/update the drag image */
1019 if (InternalDrag.bShow) {
1020 HDC hdcDrag;
1021 HDC hdcOffScreen;
1022 HDC hdcBg;
1023 HBITMAP hbmOffScreen;
1024 INT origNewX, origNewY;
1025 INT origOldX, origOldY;
1026 INT origRegX, origRegY;
1027 INT sizeRegX, sizeRegY;
1030 /* calculate the update region */
1031 origNewX = x - InternalDrag.dxHotspot;
1032 origNewY = y - InternalDrag.dyHotspot;
1033 origOldX = InternalDrag.x - InternalDrag.dxHotspot;
1034 origOldY = InternalDrag.y - InternalDrag.dyHotspot;
1035 origRegX = min(origNewX, origOldX);
1036 origRegY = min(origNewY, origOldY);
1037 sizeRegX = InternalDrag.himl->cx + abs(x - InternalDrag.x);
1038 sizeRegY = InternalDrag.himl->cy + abs(y - InternalDrag.y);
1040 hdcDrag = GetDCEx(InternalDrag.hwnd, 0,
1041 DCX_WINDOW | DCX_CACHE | DCX_LOCKWINDOWUPDATE);
1042 hdcOffScreen = CreateCompatibleDC(hdcDrag);
1043 hdcBg = CreateCompatibleDC(hdcDrag);
1045 hbmOffScreen = CreateCompatibleBitmap(hdcDrag, sizeRegX, sizeRegY);
1046 SelectObject(hdcOffScreen, hbmOffScreen);
1047 SelectObject(hdcBg, InternalDrag.hbmBg);
1049 /* get the actual background of the update region */
1050 BitBlt(hdcOffScreen, 0, 0, sizeRegX, sizeRegY, hdcDrag,
1051 origRegX, origRegY, SRCCOPY);
1052 /* erase the old image */
1053 BitBlt(hdcOffScreen, origOldX - origRegX, origOldY - origRegY,
1054 InternalDrag.himl->cx, InternalDrag.himl->cy, hdcBg, 0, 0,
1055 SRCCOPY);
1056 /* save the background */
1057 BitBlt(hdcBg, 0, 0, InternalDrag.himl->cx, InternalDrag.himl->cy,
1058 hdcOffScreen, origNewX - origRegX, origNewY - origRegY, SRCCOPY);
1059 /* draw the image */
1060 ImageList_InternalDragDraw(hdcOffScreen, origNewX - origRegX,
1061 origNewY - origRegY);
1062 /* draw the update region to the screen */
1063 BitBlt(hdcDrag, origRegX, origRegY, sizeRegX, sizeRegY,
1064 hdcOffScreen, 0, 0, SRCCOPY);
1066 DeleteDC(hdcBg);
1067 DeleteDC(hdcOffScreen);
1068 DeleteObject(hbmOffScreen);
1069 ReleaseDC(InternalDrag.hwnd, hdcDrag);
1072 /* update the image position */
1073 InternalDrag.x = x;
1074 InternalDrag.y = y;
1076 return TRUE;
1080 /*************************************************************************
1081 * ImageList_DragShowNolock [COMCTL32.@]
1083 * Shows or hides the drag image.
1085 * PARAMS
1086 * bShow [I] TRUE shows the drag image, FALSE hides it.
1088 * RETURNS
1089 * Success: TRUE
1090 * Failure: FALSE
1093 BOOL WINAPI
1094 ImageList_DragShowNolock (BOOL bShow)
1096 HDC hdcDrag;
1097 HDC hdcBg;
1098 INT x, y;
1100 if (!is_valid(InternalDrag.himl))
1101 return FALSE;
1103 TRACE("bShow=0x%X!\n", bShow);
1105 /* DragImage is already visible/hidden */
1106 if ((InternalDrag.bShow && bShow) || (!InternalDrag.bShow && !bShow)) {
1107 return FALSE;
1110 /* position of the origin of the DragImage */
1111 x = InternalDrag.x - InternalDrag.dxHotspot;
1112 y = InternalDrag.y - InternalDrag.dyHotspot;
1114 hdcDrag = GetDCEx (InternalDrag.hwnd, 0,
1115 DCX_WINDOW | DCX_CACHE | DCX_LOCKWINDOWUPDATE);
1116 if (!hdcDrag) {
1117 return FALSE;
1120 hdcBg = CreateCompatibleDC(hdcDrag);
1121 if (!InternalDrag.hbmBg) {
1122 InternalDrag.hbmBg = CreateCompatibleBitmap(hdcDrag,
1123 InternalDrag.himl->cx, InternalDrag.himl->cy);
1125 SelectObject(hdcBg, InternalDrag.hbmBg);
1127 if (bShow) {
1128 /* save the background */
1129 BitBlt(hdcBg, 0, 0, InternalDrag.himl->cx, InternalDrag.himl->cy,
1130 hdcDrag, x, y, SRCCOPY);
1131 /* show the image */
1132 ImageList_InternalDragDraw(hdcDrag, x, y);
1133 } else {
1134 /* hide the image */
1135 BitBlt(hdcDrag, x, y, InternalDrag.himl->cx, InternalDrag.himl->cy,
1136 hdcBg, 0, 0, SRCCOPY);
1139 InternalDrag.bShow = !InternalDrag.bShow;
1141 DeleteDC(hdcBg);
1142 ReleaseDC (InternalDrag.hwnd, hdcDrag);
1143 return TRUE;
1147 /*************************************************************************
1148 * ImageList_Draw [COMCTL32.@]
1150 * Draws an image.
1152 * PARAMS
1153 * himl [I] handle to image list
1154 * i [I] image index
1155 * hdc [I] handle to device context
1156 * x [I] x position
1157 * y [I] y position
1158 * fStyle [I] drawing flags
1160 * RETURNS
1161 * Success: TRUE
1162 * Failure: FALSE
1164 * SEE
1165 * ImageList_DrawEx.
1168 BOOL WINAPI
1169 ImageList_Draw (HIMAGELIST himl, INT i, HDC hdc, INT x, INT y, UINT fStyle)
1171 return ImageList_DrawEx (himl, i, hdc, x, y, 0, 0,
1172 CLR_DEFAULT, CLR_DEFAULT, fStyle);
1176 /*************************************************************************
1177 * ImageList_DrawEx [COMCTL32.@]
1179 * Draws an image and allows to use extended drawing features.
1181 * PARAMS
1182 * himl [I] handle to image list
1183 * i [I] image index
1184 * hdc [I] handle to device context
1185 * x [I] X position
1186 * y [I] Y position
1187 * dx [I] X offset
1188 * dy [I] Y offset
1189 * rgbBk [I] background color
1190 * rgbFg [I] foreground color
1191 * fStyle [I] drawing flags
1193 * RETURNS
1194 * Success: TRUE
1195 * Failure: FALSE
1197 * NOTES
1198 * Calls ImageList_DrawIndirect.
1200 * SEE
1201 * ImageList_DrawIndirect.
1204 BOOL WINAPI
1205 ImageList_DrawEx (HIMAGELIST himl, INT i, HDC hdc, INT x, INT y,
1206 INT dx, INT dy, COLORREF rgbBk, COLORREF rgbFg,
1207 UINT fStyle)
1209 IMAGELISTDRAWPARAMS imldp;
1211 ZeroMemory (&imldp, sizeof(imldp));
1212 imldp.cbSize = sizeof(imldp);
1213 imldp.himl = himl;
1214 imldp.i = i;
1215 imldp.hdcDst = hdc,
1216 imldp.x = x;
1217 imldp.y = y;
1218 imldp.cx = dx;
1219 imldp.cy = dy;
1220 imldp.rgbBk = rgbBk;
1221 imldp.rgbFg = rgbFg;
1222 imldp.fStyle = fStyle;
1224 return ImageList_DrawIndirect (&imldp);
1228 static BOOL alpha_blend_image( HIMAGELIST himl, HDC dest_dc, int dest_x, int dest_y,
1229 int src_x, int src_y, int cx, int cy, BLENDFUNCTION func,
1230 UINT style, COLORREF blend_col )
1232 BOOL ret = FALSE;
1233 HDC hdc;
1234 HBITMAP bmp = 0, mask = 0;
1235 BITMAPINFO *info;
1236 void *bits, *mask_bits;
1237 unsigned int *ptr;
1238 int i, j;
1240 if (!(hdc = CreateCompatibleDC( 0 ))) return FALSE;
1241 if (!(info = HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET( BITMAPINFO, bmiColors[256] )))) goto done;
1242 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1243 info->bmiHeader.biWidth = cx;
1244 info->bmiHeader.biHeight = cy;
1245 info->bmiHeader.biPlanes = 1;
1246 info->bmiHeader.biBitCount = 32;
1247 info->bmiHeader.biCompression = BI_RGB;
1248 info->bmiHeader.biSizeImage = cx * cy * 4;
1249 info->bmiHeader.biXPelsPerMeter = 0;
1250 info->bmiHeader.biYPelsPerMeter = 0;
1251 info->bmiHeader.biClrUsed = 0;
1252 info->bmiHeader.biClrImportant = 0;
1253 if (!(bmp = CreateDIBSection( himl->hdcImage, info, DIB_RGB_COLORS, &bits, 0, 0 ))) goto done;
1254 SelectObject( hdc, bmp );
1255 BitBlt( hdc, 0, 0, cx, cy, himl->hdcImage, src_x, src_y, SRCCOPY );
1257 if (blend_col != CLR_NONE)
1259 BYTE r = GetRValue( blend_col );
1260 BYTE g = GetGValue( blend_col );
1261 BYTE b = GetBValue( blend_col );
1263 if (style & ILD_BLEND25)
1265 for (i = 0, ptr = bits; i < cx * cy; i++, ptr++)
1266 *ptr = ((*ptr & 0xff000000) |
1267 ((((*ptr & 0x00ff0000) * 3 + (r << 16)) / 4) & 0x00ff0000) |
1268 ((((*ptr & 0x0000ff00) * 3 + (g << 8)) / 4) & 0x0000ff00) |
1269 ((((*ptr & 0x000000ff) * 3 + (b << 0)) / 4) & 0x000000ff));
1271 else if (style & ILD_BLEND50)
1273 for (i = 0, ptr = bits; i < cx * cy; i++, ptr++)
1274 *ptr = ((*ptr & 0xff000000) |
1275 ((((*ptr & 0x00ff0000) + (r << 16)) / 2) & 0x00ff0000) |
1276 ((((*ptr & 0x0000ff00) + (g << 8)) / 2) & 0x0000ff00) |
1277 ((((*ptr & 0x000000ff) + (b << 0)) / 2) & 0x000000ff));
1281 if (himl->has_alpha) /* we already have an alpha channel in this case */
1283 /* pre-multiply by the alpha channel */
1284 for (i = 0, ptr = bits; i < cx * cy; i++, ptr++)
1286 DWORD alpha = *ptr >> 24;
1287 *ptr = ((*ptr & 0xff000000) |
1288 (((*ptr & 0x00ff0000) * alpha / 255) & 0x00ff0000) |
1289 (((*ptr & 0x0000ff00) * alpha / 255) & 0x0000ff00) |
1290 (((*ptr & 0x000000ff) * alpha / 255)));
1293 else if (himl->hbmMask)
1295 unsigned int width_bytes = (cx + 31) / 32 * 4;
1296 /* generate alpha channel from the mask */
1297 info->bmiHeader.biBitCount = 1;
1298 info->bmiHeader.biSizeImage = width_bytes * cy;
1299 info->bmiColors[0].rgbRed = 0;
1300 info->bmiColors[0].rgbGreen = 0;
1301 info->bmiColors[0].rgbBlue = 0;
1302 info->bmiColors[0].rgbReserved = 0;
1303 info->bmiColors[1].rgbRed = 0xff;
1304 info->bmiColors[1].rgbGreen = 0xff;
1305 info->bmiColors[1].rgbBlue = 0xff;
1306 info->bmiColors[1].rgbReserved = 0;
1307 if (!(mask = CreateDIBSection( himl->hdcMask, info, DIB_RGB_COLORS, &mask_bits, 0, 0 )))
1308 goto done;
1309 SelectObject( hdc, mask );
1310 BitBlt( hdc, 0, 0, cx, cy, himl->hdcMask, src_x, src_y, SRCCOPY );
1311 SelectObject( hdc, bmp );
1312 for (i = 0, ptr = bits; i < cy; i++)
1313 for (j = 0; j < cx; j++, ptr++)
1314 if ((((BYTE *)mask_bits)[i * width_bytes + j / 8] << (j % 8)) & 0x80) *ptr = 0;
1315 else *ptr |= 0xff000000;
1318 ret = GdiAlphaBlend( dest_dc, dest_x, dest_y, cx, cy, hdc, 0, 0, cx, cy, func );
1320 done:
1321 DeleteDC( hdc );
1322 if (bmp) DeleteObject( bmp );
1323 if (mask) DeleteObject( mask );
1324 HeapFree( GetProcessHeap(), 0, info );
1325 return ret;
1328 /*************************************************************************
1329 * ImageList_DrawIndirect [COMCTL32.@]
1331 * Draws an image using various parameters specified in pimldp.
1333 * PARAMS
1334 * pimldp [I] pointer to IMAGELISTDRAWPARAMS structure.
1336 * RETURNS
1337 * Success: TRUE
1338 * Failure: FALSE
1341 BOOL WINAPI
1342 ImageList_DrawIndirect (IMAGELISTDRAWPARAMS *pimldp)
1344 INT cx, cy, nOvlIdx;
1345 DWORD fState, dwRop;
1346 UINT fStyle;
1347 COLORREF oldImageBk, oldImageFg;
1348 HDC hImageDC, hImageListDC, hMaskListDC;
1349 HBITMAP hImageBmp, hOldImageBmp, hBlendMaskBmp;
1350 BOOL bIsTransparent, bBlend, bResult = FALSE, bMask;
1351 HIMAGELIST himl;
1352 HBRUSH hOldBrush;
1353 POINT pt;
1354 BOOL has_alpha;
1356 if (!pimldp || !(himl = pimldp->himl)) return FALSE;
1357 if (!is_valid(himl)) return FALSE;
1358 if ((pimldp->i < 0) || (pimldp->i >= himl->cCurImage)) return FALSE;
1360 imagelist_point_from_index( himl, pimldp->i, &pt );
1361 pt.x += pimldp->xBitmap;
1362 pt.y += pimldp->yBitmap;
1364 fState = pimldp->cbSize < sizeof(IMAGELISTDRAWPARAMS) ? ILS_NORMAL : pimldp->fState;
1365 fStyle = pimldp->fStyle & ~ILD_OVERLAYMASK;
1366 cx = (pimldp->cx == 0) ? himl->cx : pimldp->cx;
1367 cy = (pimldp->cy == 0) ? himl->cy : pimldp->cy;
1369 bIsTransparent = (fStyle & ILD_TRANSPARENT);
1370 if( pimldp->rgbBk == CLR_NONE )
1371 bIsTransparent = TRUE;
1372 if( ( pimldp->rgbBk == CLR_DEFAULT ) && ( himl->clrBk == CLR_NONE ) )
1373 bIsTransparent = TRUE;
1374 bMask = (himl->flags & ILC_MASK) && (fStyle & ILD_MASK) ;
1375 bBlend = (fStyle & (ILD_BLEND25 | ILD_BLEND50) ) && !bMask;
1377 TRACE("himl(%p) hbmMask(%p) iImage(%d) x(%d) y(%d) cx(%d) cy(%d)\n",
1378 himl, himl->hbmMask, pimldp->i, pimldp->x, pimldp->y, cx, cy);
1380 /* we will use these DCs to access the images and masks in the ImageList */
1381 hImageListDC = himl->hdcImage;
1382 hMaskListDC = himl->hdcMask;
1384 /* these will accumulate the image and mask for the image we're drawing */
1385 hImageDC = CreateCompatibleDC( pimldp->hdcDst );
1386 hImageBmp = CreateCompatibleBitmap( pimldp->hdcDst, cx, cy );
1387 hBlendMaskBmp = bBlend ? CreateBitmap(cx, cy, 1, 1, NULL) : 0;
1389 /* Create a compatible DC. */
1390 if (!hImageListDC || !hImageDC || !hImageBmp ||
1391 (bBlend && !hBlendMaskBmp) || (himl->hbmMask && !hMaskListDC))
1392 goto cleanup;
1394 hOldImageBmp = SelectObject(hImageDC, hImageBmp);
1397 * To obtain a transparent look, background color should be set
1398 * to white and foreground color to black when blitting the
1399 * monochrome mask.
1401 oldImageFg = SetTextColor( hImageDC, RGB( 0, 0, 0 ) );
1402 oldImageBk = SetBkColor( hImageDC, RGB( 0xff, 0xff, 0xff ) );
1404 has_alpha = (himl->has_alpha && himl->has_alpha[pimldp->i]);
1405 if (!bMask && (has_alpha || (fState & ILS_ALPHA)))
1407 COLORREF colour, blend_col = CLR_NONE;
1408 BLENDFUNCTION func;
1410 if (bBlend)
1412 blend_col = pimldp->rgbFg;
1413 if (blend_col == CLR_DEFAULT) blend_col = GetSysColor( COLOR_HIGHLIGHT );
1414 else if (blend_col == CLR_NONE) blend_col = GetTextColor( pimldp->hdcDst );
1417 func.BlendOp = AC_SRC_OVER;
1418 func.BlendFlags = 0;
1419 func.SourceConstantAlpha = (fState & ILS_ALPHA) ? pimldp->Frame : 255;
1420 func.AlphaFormat = AC_SRC_ALPHA;
1422 if (bIsTransparent)
1424 bResult = alpha_blend_image( himl, pimldp->hdcDst, pimldp->x, pimldp->y,
1425 pt.x, pt.y, cx, cy, func, fStyle, blend_col );
1426 goto end;
1428 colour = pimldp->rgbBk;
1429 if (colour == CLR_DEFAULT) colour = himl->clrBk;
1430 if (colour == CLR_NONE) colour = GetBkColor( pimldp->hdcDst );
1432 hOldBrush = SelectObject (hImageDC, CreateSolidBrush (colour));
1433 PatBlt( hImageDC, 0, 0, cx, cy, PATCOPY );
1434 alpha_blend_image( himl, hImageDC, 0, 0, pt.x, pt.y, cx, cy, func, fStyle, blend_col );
1435 DeleteObject (SelectObject (hImageDC, hOldBrush));
1436 bResult = BitBlt( pimldp->hdcDst, pimldp->x, pimldp->y, cx, cy, hImageDC, 0, 0, SRCCOPY );
1437 goto end;
1441 * Draw the initial image
1443 if( bMask ) {
1444 if (himl->hbmMask) {
1445 hOldBrush = SelectObject (hImageDC, CreateSolidBrush (GetTextColor(pimldp->hdcDst)));
1446 PatBlt( hImageDC, 0, 0, cx, cy, PATCOPY );
1447 BitBlt(hImageDC, 0, 0, cx, cy, hMaskListDC, pt.x, pt.y, SRCPAINT);
1448 DeleteObject (SelectObject (hImageDC, hOldBrush));
1449 if( bIsTransparent )
1451 BitBlt ( pimldp->hdcDst, pimldp->x, pimldp->y, cx, cy, hImageDC, 0, 0, SRCAND);
1452 bResult = TRUE;
1453 goto end;
1455 } else {
1456 hOldBrush = SelectObject (hImageDC, GetStockObject(BLACK_BRUSH));
1457 PatBlt( hImageDC, 0, 0, cx, cy, PATCOPY);
1458 SelectObject(hImageDC, hOldBrush);
1460 } else {
1461 /* blend the image with the needed solid background */
1462 COLORREF colour = RGB(0,0,0);
1464 if( !bIsTransparent )
1466 colour = pimldp->rgbBk;
1467 if( colour == CLR_DEFAULT )
1468 colour = himl->clrBk;
1469 if( colour == CLR_NONE )
1470 colour = GetBkColor(pimldp->hdcDst);
1473 hOldBrush = SelectObject (hImageDC, CreateSolidBrush (colour));
1474 PatBlt( hImageDC, 0, 0, cx, cy, PATCOPY );
1475 if (himl->hbmMask)
1477 BitBlt( hImageDC, 0, 0, cx, cy, hMaskListDC, pt.x, pt.y, SRCAND );
1478 BitBlt( hImageDC, 0, 0, cx, cy, hImageListDC, pt.x, pt.y, SRCPAINT );
1480 else
1481 BitBlt( hImageDC, 0, 0, cx, cy, hImageListDC, pt.x, pt.y, SRCCOPY);
1482 DeleteObject (SelectObject (hImageDC, hOldBrush));
1485 /* Time for blending, if required */
1486 if (bBlend) {
1487 HBRUSH hBlendBrush;
1488 COLORREF clrBlend = pimldp->rgbFg;
1489 HDC hBlendMaskDC = hImageListDC;
1490 HBITMAP hOldBitmap;
1492 /* Create the blend Mask */
1493 hOldBitmap = SelectObject(hBlendMaskDC, hBlendMaskBmp);
1494 hBlendBrush = fStyle & ILD_BLEND50 ? himl->hbrBlend50 : himl->hbrBlend25;
1495 hOldBrush = SelectObject(hBlendMaskDC, hBlendBrush);
1496 PatBlt(hBlendMaskDC, 0, 0, cx, cy, PATCOPY);
1497 SelectObject(hBlendMaskDC, hOldBrush);
1499 /* Modify the blend mask if an Image Mask exist */
1500 if(himl->hbmMask) {
1501 BitBlt(hBlendMaskDC, 0, 0, cx, cy, hMaskListDC, pt.x, pt.y, 0x220326); /* NOTSRCAND */
1502 BitBlt(hBlendMaskDC, 0, 0, cx, cy, hBlendMaskDC, 0, 0, NOTSRCCOPY);
1505 /* now apply blend to the current image given the BlendMask */
1506 if (clrBlend == CLR_DEFAULT) clrBlend = GetSysColor (COLOR_HIGHLIGHT);
1507 else if (clrBlend == CLR_NONE) clrBlend = GetTextColor (pimldp->hdcDst);
1508 hOldBrush = SelectObject (hImageDC, CreateSolidBrush(clrBlend));
1509 BitBlt (hImageDC, 0, 0, cx, cy, hBlendMaskDC, 0, 0, 0xB8074A); /* PSDPxax */
1510 DeleteObject(SelectObject(hImageDC, hOldBrush));
1511 SelectObject(hBlendMaskDC, hOldBitmap);
1514 /* Now do the overlay image, if any */
1515 nOvlIdx = (pimldp->fStyle & ILD_OVERLAYMASK) >> 8;
1516 if ( (nOvlIdx >= 1) && (nOvlIdx <= MAX_OVERLAYIMAGE)) {
1517 nOvlIdx = himl->nOvlIdx[nOvlIdx - 1];
1518 if ((nOvlIdx >= 0) && (nOvlIdx < himl->cCurImage)) {
1519 POINT ptOvl;
1520 imagelist_point_from_index( himl, nOvlIdx, &ptOvl );
1521 ptOvl.x += pimldp->xBitmap;
1522 if (himl->hbmMask && !(fStyle & ILD_IMAGE))
1523 BitBlt (hImageDC, 0, 0, cx, cy, hMaskListDC, ptOvl.x, ptOvl.y, SRCAND);
1524 BitBlt (hImageDC, 0, 0, cx, cy, hImageListDC, ptOvl.x, ptOvl.y, SRCPAINT);
1528 if (fState & ILS_SATURATE) FIXME("ILS_SATURATE: unimplemented!\n");
1529 if (fState & ILS_GLOW) FIXME("ILS_GLOW: unimplemented!\n");
1530 if (fState & ILS_SHADOW) FIXME("ILS_SHADOW: unimplemented!\n");
1532 if (fStyle & ILD_PRESERVEALPHA) FIXME("ILD_PRESERVEALPHA: unimplemented!\n");
1533 if (fStyle & ILD_SCALE) FIXME("ILD_SCALE: unimplemented!\n");
1534 if (fStyle & ILD_DPISCALE) FIXME("ILD_DPISCALE: unimplemented!\n");
1536 /* now copy the image to the screen */
1537 dwRop = SRCCOPY;
1538 if (himl->hbmMask && bIsTransparent ) {
1539 COLORREF oldDstFg = SetTextColor(pimldp->hdcDst, RGB( 0, 0, 0 ) );
1540 COLORREF oldDstBk = SetBkColor(pimldp->hdcDst, RGB( 0xff, 0xff, 0xff ));
1541 BitBlt (pimldp->hdcDst, pimldp->x, pimldp->y, cx, cy, hMaskListDC, pt.x, pt.y, SRCAND);
1542 SetBkColor(pimldp->hdcDst, oldDstBk);
1543 SetTextColor(pimldp->hdcDst, oldDstFg);
1544 dwRop = SRCPAINT;
1546 if (fStyle & ILD_ROP) dwRop = pimldp->dwRop;
1547 BitBlt (pimldp->hdcDst, pimldp->x, pimldp->y, cx, cy, hImageDC, 0, 0, dwRop);
1549 bResult = TRUE;
1550 end:
1551 /* cleanup the mess */
1552 SetBkColor(hImageDC, oldImageBk);
1553 SetTextColor(hImageDC, oldImageFg);
1554 SelectObject(hImageDC, hOldImageBmp);
1555 cleanup:
1556 DeleteObject(hBlendMaskBmp);
1557 DeleteObject(hImageBmp);
1558 DeleteDC(hImageDC);
1560 return bResult;
1564 /*************************************************************************
1565 * ImageList_Duplicate [COMCTL32.@]
1567 * Duplicates an image list.
1569 * PARAMS
1570 * himlSrc [I] source image list handle
1572 * RETURNS
1573 * Success: Handle of duplicated image list.
1574 * Failure: NULL
1577 HIMAGELIST WINAPI
1578 ImageList_Duplicate (HIMAGELIST himlSrc)
1580 HIMAGELIST himlDst;
1582 if (!is_valid(himlSrc)) {
1583 ERR("Invalid image list handle!\n");
1584 return NULL;
1587 himlDst = ImageList_Create (himlSrc->cx, himlSrc->cy, himlSrc->flags,
1588 himlSrc->cCurImage, himlSrc->cGrow);
1590 if (himlDst)
1592 SIZE sz;
1594 imagelist_get_bitmap_size(himlSrc, himlSrc->cCurImage, &sz);
1595 BitBlt (himlDst->hdcImage, 0, 0, sz.cx, sz.cy,
1596 himlSrc->hdcImage, 0, 0, SRCCOPY);
1598 if (himlDst->hbmMask)
1599 BitBlt (himlDst->hdcMask, 0, 0, sz.cx, sz.cy,
1600 himlSrc->hdcMask, 0, 0, SRCCOPY);
1602 himlDst->cCurImage = himlSrc->cCurImage;
1603 if (himlSrc->has_alpha && himlDst->has_alpha)
1604 memcpy( himlDst->has_alpha, himlSrc->has_alpha, himlDst->cCurImage );
1606 return himlDst;
1610 /*************************************************************************
1611 * ImageList_EndDrag [COMCTL32.@]
1613 * Finishes a drag operation.
1615 * PARAMS
1616 * no Parameters
1618 * RETURNS
1619 * Success: TRUE
1620 * Failure: FALSE
1623 VOID WINAPI
1624 ImageList_EndDrag (void)
1626 /* cleanup the InternalDrag struct */
1627 InternalDrag.hwnd = 0;
1628 ImageList_Destroy (InternalDrag.himl);
1629 InternalDrag.himl = 0;
1630 InternalDrag.x= 0;
1631 InternalDrag.y= 0;
1632 InternalDrag.dxHotspot = 0;
1633 InternalDrag.dyHotspot = 0;
1634 InternalDrag.bShow = FALSE;
1635 DeleteObject(InternalDrag.hbmBg);
1636 InternalDrag.hbmBg = 0;
1640 /*************************************************************************
1641 * ImageList_GetBkColor [COMCTL32.@]
1643 * Returns the background color of an image list.
1645 * PARAMS
1646 * himl [I] Image list handle.
1648 * RETURNS
1649 * Success: background color
1650 * Failure: CLR_NONE
1653 COLORREF WINAPI
1654 ImageList_GetBkColor (HIMAGELIST himl)
1656 return himl ? himl->clrBk : CLR_NONE;
1660 /*************************************************************************
1661 * ImageList_GetDragImage [COMCTL32.@]
1663 * Returns the handle to the internal drag image list.
1665 * PARAMS
1666 * ppt [O] Pointer to the drag position. Can be NULL.
1667 * pptHotspot [O] Pointer to the position of the hot spot. Can be NULL.
1669 * RETURNS
1670 * Success: Handle of the drag image list.
1671 * Failure: NULL.
1674 HIMAGELIST WINAPI
1675 ImageList_GetDragImage (POINT *ppt, POINT *pptHotspot)
1677 if (is_valid(InternalDrag.himl)) {
1678 if (ppt) {
1679 ppt->x = InternalDrag.x;
1680 ppt->y = InternalDrag.y;
1682 if (pptHotspot) {
1683 pptHotspot->x = InternalDrag.dxHotspot;
1684 pptHotspot->y = InternalDrag.dyHotspot;
1686 return (InternalDrag.himl);
1689 return NULL;
1693 /*************************************************************************
1694 * ImageList_GetFlags [COMCTL32.@]
1696 * Gets the flags of the specified image list.
1698 * PARAMS
1699 * himl [I] Handle to image list
1701 * RETURNS
1702 * Image list flags.
1704 * BUGS
1705 * Stub.
1708 DWORD WINAPI
1709 ImageList_GetFlags(HIMAGELIST himl)
1711 TRACE("%p\n", himl);
1713 return is_valid(himl) ? himl->flags : 0;
1717 /*************************************************************************
1718 * ImageList_GetIcon [COMCTL32.@]
1720 * Creates an icon from a masked image of an image list.
1722 * PARAMS
1723 * himl [I] handle to image list
1724 * i [I] image index
1725 * flags [I] drawing style flags
1727 * RETURNS
1728 * Success: icon handle
1729 * Failure: NULL
1732 HICON WINAPI
1733 ImageList_GetIcon (HIMAGELIST himl, INT i, UINT fStyle)
1735 ICONINFO ii;
1736 HICON hIcon;
1737 HBITMAP hOldDstBitmap;
1738 HDC hdcDst;
1739 POINT pt;
1741 TRACE("%p %d %d\n", himl, i, fStyle);
1742 if (!is_valid(himl) || (i < 0) || (i >= himl->cCurImage)) return NULL;
1744 ii.fIcon = TRUE;
1745 ii.xHotspot = 0;
1746 ii.yHotspot = 0;
1748 /* create colour bitmap */
1749 hdcDst = GetDC(0);
1750 ii.hbmColor = CreateCompatibleBitmap(hdcDst, himl->cx, himl->cy);
1751 ReleaseDC(0, hdcDst);
1753 hdcDst = CreateCompatibleDC(0);
1755 imagelist_point_from_index( himl, i, &pt );
1757 /* draw mask*/
1758 ii.hbmMask = CreateBitmap (himl->cx, himl->cy, 1, 1, NULL);
1759 hOldDstBitmap = SelectObject (hdcDst, ii.hbmMask);
1760 if (himl->hbmMask) {
1761 BitBlt (hdcDst, 0, 0, himl->cx, himl->cy,
1762 himl->hdcMask, pt.x, pt.y, SRCCOPY);
1764 else
1765 PatBlt (hdcDst, 0, 0, himl->cx, himl->cy, BLACKNESS);
1767 /* draw image*/
1768 SelectObject (hdcDst, ii.hbmColor);
1769 BitBlt (hdcDst, 0, 0, himl->cx, himl->cy,
1770 himl->hdcImage, pt.x, pt.y, SRCCOPY);
1773 * CreateIconIndirect requires us to deselect the bitmaps from
1774 * the DCs before calling
1776 SelectObject(hdcDst, hOldDstBitmap);
1778 hIcon = CreateIconIndirect (&ii);
1780 DeleteObject (ii.hbmMask);
1781 DeleteObject (ii.hbmColor);
1782 DeleteDC (hdcDst);
1784 return hIcon;
1788 /*************************************************************************
1789 * ImageList_GetIconSize [COMCTL32.@]
1791 * Retrieves the size of an image in an image list.
1793 * PARAMS
1794 * himl [I] handle to image list
1795 * cx [O] pointer to the image width.
1796 * cy [O] pointer to the image height.
1798 * RETURNS
1799 * Success: TRUE
1800 * Failure: FALSE
1802 * NOTES
1803 * All images in an image list have the same size.
1806 BOOL WINAPI
1807 ImageList_GetIconSize (HIMAGELIST himl, INT *cx, INT *cy)
1809 if (!is_valid(himl) || !cx || !cy)
1810 return FALSE;
1811 if ((himl->cx <= 0) || (himl->cy <= 0))
1812 return FALSE;
1814 *cx = himl->cx;
1815 *cy = himl->cy;
1817 return TRUE;
1821 /*************************************************************************
1822 * ImageList_GetImageCount [COMCTL32.@]
1824 * Returns the number of images in an image list.
1826 * PARAMS
1827 * himl [I] handle to image list
1829 * RETURNS
1830 * Success: Number of images.
1831 * Failure: 0
1834 INT WINAPI
1835 ImageList_GetImageCount (HIMAGELIST himl)
1837 if (!is_valid(himl))
1838 return 0;
1840 return himl->cCurImage;
1844 /*************************************************************************
1845 * ImageList_GetImageInfo [COMCTL32.@]
1847 * Returns information about an image in an image list.
1849 * PARAMS
1850 * himl [I] handle to image list
1851 * i [I] image index
1852 * pImageInfo [O] pointer to the image information
1854 * RETURNS
1855 * Success: TRUE
1856 * Failure: FALSE
1859 BOOL WINAPI
1860 ImageList_GetImageInfo (HIMAGELIST himl, INT i, IMAGEINFO *pImageInfo)
1862 POINT pt;
1864 if (!is_valid(himl) || (pImageInfo == NULL))
1865 return FALSE;
1866 if ((i < 0) || (i >= himl->cCurImage))
1867 return FALSE;
1869 pImageInfo->hbmImage = himl->hbmImage;
1870 pImageInfo->hbmMask = himl->hbmMask;
1872 imagelist_point_from_index( himl, i, &pt );
1873 pImageInfo->rcImage.top = pt.y;
1874 pImageInfo->rcImage.bottom = pt.y + himl->cy;
1875 pImageInfo->rcImage.left = pt.x;
1876 pImageInfo->rcImage.right = pt.x + himl->cx;
1878 return TRUE;
1882 /*************************************************************************
1883 * ImageList_GetImageRect [COMCTL32.@]
1885 * Retrieves the rectangle of the specified image in an image list.
1887 * PARAMS
1888 * himl [I] handle to image list
1889 * i [I] image index
1890 * lpRect [O] pointer to the image rectangle
1892 * RETURNS
1893 * Success: TRUE
1894 * Failure: FALSE
1896 * NOTES
1897 * This is an UNDOCUMENTED function!!!
1900 BOOL WINAPI
1901 ImageList_GetImageRect (HIMAGELIST himl, INT i, LPRECT lpRect)
1903 POINT pt;
1905 if (!is_valid(himl) || (lpRect == NULL))
1906 return FALSE;
1907 if ((i < 0) || (i >= himl->cCurImage))
1908 return FALSE;
1910 imagelist_point_from_index( himl, i, &pt );
1911 lpRect->left = pt.x;
1912 lpRect->top = pt.y;
1913 lpRect->right = pt.x + himl->cx;
1914 lpRect->bottom = pt.y + himl->cy;
1916 return TRUE;
1920 /*************************************************************************
1921 * ImageList_LoadImage [COMCTL32.@]
1922 * ImageList_LoadImageA [COMCTL32.@]
1924 * Creates an image list from a bitmap, icon or cursor.
1926 * See ImageList_LoadImageW.
1929 HIMAGELIST WINAPI
1930 ImageList_LoadImageA (HINSTANCE hi, LPCSTR lpbmp, INT cx, INT cGrow,
1931 COLORREF clrMask, UINT uType, UINT uFlags)
1933 HIMAGELIST himl;
1934 LPWSTR lpbmpW;
1935 DWORD len;
1937 if (IS_INTRESOURCE(lpbmp))
1938 return ImageList_LoadImageW(hi, (LPCWSTR)lpbmp, cx, cGrow, clrMask,
1939 uType, uFlags);
1941 len = MultiByteToWideChar(CP_ACP, 0, lpbmp, -1, NULL, 0);
1942 lpbmpW = Alloc(len * sizeof(WCHAR));
1943 MultiByteToWideChar(CP_ACP, 0, lpbmp, -1, lpbmpW, len);
1945 himl = ImageList_LoadImageW(hi, lpbmpW, cx, cGrow, clrMask, uType, uFlags);
1946 Free (lpbmpW);
1947 return himl;
1951 /*************************************************************************
1952 * ImageList_LoadImageW [COMCTL32.@]
1954 * Creates an image list from a bitmap, icon or cursor.
1956 * PARAMS
1957 * hi [I] instance handle
1958 * lpbmp [I] name or id of the image
1959 * cx [I] width of each image
1960 * cGrow [I] number of images to expand
1961 * clrMask [I] mask color
1962 * uType [I] type of image to load
1963 * uFlags [I] loading flags
1965 * RETURNS
1966 * Success: handle to the loaded image list
1967 * Failure: NULL
1969 * SEE
1970 * LoadImage ()
1973 HIMAGELIST WINAPI
1974 ImageList_LoadImageW (HINSTANCE hi, LPCWSTR lpbmp, INT cx, INT cGrow,
1975 COLORREF clrMask, UINT uType, UINT uFlags)
1977 HIMAGELIST himl = NULL;
1978 HANDLE handle;
1979 INT nImageCount;
1981 handle = LoadImageW (hi, lpbmp, uType, 0, 0, uFlags);
1982 if (!handle) {
1983 WARN("Couldn't load image\n");
1984 return NULL;
1987 if (uType == IMAGE_BITMAP) {
1988 DIBSECTION dib;
1989 UINT color;
1991 if (GetObjectW (handle, sizeof(dib), &dib) == sizeof(BITMAP)) color = ILC_COLOR;
1992 else color = dib.dsBm.bmBitsPixel;
1994 /* To match windows behavior, if cx is set to zero and
1995 the flag DI_DEFAULTSIZE is specified, cx becomes the
1996 system metric value for icons. If the flag is not specified
1997 the function sets the size to the height of the bitmap */
1998 if (cx == 0)
2000 if (uFlags & DI_DEFAULTSIZE)
2001 cx = GetSystemMetrics (SM_CXICON);
2002 else
2003 cx = dib.dsBm.bmHeight;
2006 nImageCount = dib.dsBm.bmWidth / cx;
2008 himl = ImageList_Create (cx, dib.dsBm.bmHeight, ILC_MASK | color, nImageCount, cGrow);
2009 if (!himl) {
2010 DeleteObject (handle);
2011 return NULL;
2013 ImageList_AddMasked (himl, handle, clrMask);
2015 else if ((uType == IMAGE_ICON) || (uType == IMAGE_CURSOR)) {
2016 ICONINFO ii;
2017 BITMAP bmp;
2019 GetIconInfo (handle, &ii);
2020 GetObjectW (ii.hbmColor, sizeof(BITMAP), &bmp);
2021 himl = ImageList_Create (bmp.bmWidth, bmp.bmHeight,
2022 ILC_MASK | ILC_COLOR, 1, cGrow);
2023 if (!himl) {
2024 DeleteObject (ii.hbmColor);
2025 DeleteObject (ii.hbmMask);
2026 DeleteObject (handle);
2027 return NULL;
2029 ImageList_Add (himl, ii.hbmColor, ii.hbmMask);
2030 DeleteObject (ii.hbmColor);
2031 DeleteObject (ii.hbmMask);
2034 DeleteObject (handle);
2036 return himl;
2040 /*************************************************************************
2041 * ImageList_Merge [COMCTL32.@]
2043 * Create an image list containing a merged image from two image lists.
2045 * PARAMS
2046 * himl1 [I] handle to first image list
2047 * i1 [I] first image index
2048 * himl2 [I] handle to second image list
2049 * i2 [I] second image index
2050 * dx [I] X offset of the second image relative to the first.
2051 * dy [I] Y offset of the second image relative to the first.
2053 * RETURNS
2054 * Success: The newly created image list. It contains a single image
2055 * consisting of the second image merged with the first.
2056 * Failure: NULL, if either himl1 or himl2 are invalid.
2058 * NOTES
2059 * - The returned image list should be deleted by the caller using
2060 * ImageList_Destroy() when it is no longer required.
2061 * - If either i1 or i2 are not valid image indices they will be treated
2062 * as a blank image.
2064 HIMAGELIST WINAPI
2065 ImageList_Merge (HIMAGELIST himl1, INT i1, HIMAGELIST himl2, INT i2,
2066 INT dx, INT dy)
2068 HIMAGELIST himlDst = NULL;
2069 INT cxDst, cyDst;
2070 INT xOff1, yOff1, xOff2, yOff2;
2071 POINT pt1, pt2;
2073 TRACE("(himl1=%p i1=%d himl2=%p i2=%d dx=%d dy=%d)\n", himl1, i1, himl2,
2074 i2, dx, dy);
2076 if (!is_valid(himl1) || !is_valid(himl2))
2077 return NULL;
2079 if (dx > 0) {
2080 cxDst = max (himl1->cx, dx + himl2->cx);
2081 xOff1 = 0;
2082 xOff2 = dx;
2084 else if (dx < 0) {
2085 cxDst = max (himl2->cx, himl1->cx - dx);
2086 xOff1 = -dx;
2087 xOff2 = 0;
2089 else {
2090 cxDst = max (himl1->cx, himl2->cx);
2091 xOff1 = 0;
2092 xOff2 = 0;
2095 if (dy > 0) {
2096 cyDst = max (himl1->cy, dy + himl2->cy);
2097 yOff1 = 0;
2098 yOff2 = dy;
2100 else if (dy < 0) {
2101 cyDst = max (himl2->cy, himl1->cy - dy);
2102 yOff1 = -dy;
2103 yOff2 = 0;
2105 else {
2106 cyDst = max (himl1->cy, himl2->cy);
2107 yOff1 = 0;
2108 yOff2 = 0;
2111 himlDst = ImageList_Create (cxDst, cyDst, ILC_MASK | ILC_COLOR, 1, 1);
2113 if (himlDst)
2115 imagelist_point_from_index( himl1, i1, &pt1 );
2116 imagelist_point_from_index( himl2, i2, &pt2 );
2118 /* copy image */
2119 BitBlt (himlDst->hdcImage, 0, 0, cxDst, cyDst, himl1->hdcImage, 0, 0, BLACKNESS);
2120 if (i1 >= 0 && i1 < himl1->cCurImage)
2121 BitBlt (himlDst->hdcImage, xOff1, yOff1, himl1->cx, himl1->cy, himl1->hdcImage, pt1.x, pt1.y, SRCCOPY);
2122 if (i2 >= 0 && i2 < himl2->cCurImage)
2124 BitBlt (himlDst->hdcImage, xOff2, yOff2, himl2->cx, himl2->cy, himl2->hdcMask , pt2.x, pt2.y, SRCAND);
2125 BitBlt (himlDst->hdcImage, xOff2, yOff2, himl2->cx, himl2->cy, himl2->hdcImage, pt2.x, pt2.y, SRCPAINT);
2128 /* copy mask */
2129 BitBlt (himlDst->hdcMask, 0, 0, cxDst, cyDst, himl1->hdcMask, 0, 0, WHITENESS);
2130 if (i1 >= 0 && i1 < himl1->cCurImage)
2131 BitBlt (himlDst->hdcMask, xOff1, yOff1, himl1->cx, himl1->cy, himl1->hdcMask, pt1.x, pt1.y, SRCCOPY);
2132 if (i2 >= 0 && i2 < himl2->cCurImage)
2133 BitBlt (himlDst->hdcMask, xOff2, yOff2, himl2->cx, himl2->cy, himl2->hdcMask, pt2.x, pt2.y, SRCAND);
2135 himlDst->cCurImage = 1;
2138 return himlDst;
2142 /* helper for ImageList_Read, see comments below */
2143 static void *read_bitmap(LPSTREAM pstm, BITMAPINFO *bmi)
2145 BITMAPFILEHEADER bmfh;
2146 int bitsperpixel, palspace;
2147 void *bits;
2149 if (FAILED(IStream_Read ( pstm, &bmfh, sizeof(bmfh), NULL)))
2150 return NULL;
2152 if (bmfh.bfType != (('M'<<8)|'B'))
2153 return NULL;
2155 if (FAILED(IStream_Read ( pstm, &bmi->bmiHeader, sizeof(bmi->bmiHeader), NULL)))
2156 return NULL;
2158 if ((bmi->bmiHeader.biSize != sizeof(bmi->bmiHeader)))
2159 return NULL;
2161 TRACE("width %u, height %u, planes %u, bpp %u\n",
2162 bmi->bmiHeader.biWidth, bmi->bmiHeader.biHeight,
2163 bmi->bmiHeader.biPlanes, bmi->bmiHeader.biBitCount);
2165 bitsperpixel = bmi->bmiHeader.biPlanes * bmi->bmiHeader.biBitCount;
2166 if (bitsperpixel<=8)
2167 palspace = (1<<bitsperpixel)*sizeof(RGBQUAD);
2168 else
2169 palspace = 0;
2171 bmi->bmiHeader.biSizeImage = get_dib_image_size( bmi );
2173 /* read the palette right after the end of the bitmapinfoheader */
2174 if (palspace && FAILED(IStream_Read(pstm, bmi->bmiColors, palspace, NULL)))
2175 return NULL;
2177 bits = Alloc(bmi->bmiHeader.biSizeImage);
2178 if (!bits) return NULL;
2180 if (FAILED(IStream_Read(pstm, bits, bmi->bmiHeader.biSizeImage, NULL)))
2182 Free(bits);
2183 return NULL;
2185 return bits;
2188 /*************************************************************************
2189 * ImageList_Read [COMCTL32.@]
2191 * Reads an image list from a stream.
2193 * PARAMS
2194 * pstm [I] pointer to a stream
2196 * RETURNS
2197 * Success: handle to image list
2198 * Failure: NULL
2200 * The format is like this:
2201 * ILHEAD ilheadstruct;
2203 * for the color image part:
2204 * BITMAPFILEHEADER bmfh;
2205 * BITMAPINFOHEADER bmih;
2206 * only if it has a palette:
2207 * RGBQUAD rgbs[nr_of_paletted_colors];
2209 * BYTE colorbits[imagesize];
2211 * the following only if the ILC_MASK bit is set in ILHEAD.ilFlags:
2212 * BITMAPFILEHEADER bmfh_mask;
2213 * BITMAPINFOHEADER bmih_mask;
2214 * only if it has a palette (it usually does not):
2215 * RGBQUAD rgbs[nr_of_paletted_colors];
2217 * BYTE maskbits[imagesize];
2219 HIMAGELIST WINAPI ImageList_Read (LPSTREAM pstm)
2221 char image_buf[sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * 256];
2222 char mask_buf[sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * 256];
2223 BITMAPINFO *image_info = (BITMAPINFO *)image_buf;
2224 BITMAPINFO *mask_info = (BITMAPINFO *)mask_buf;
2225 void *image_bits, *mask_bits = NULL;
2226 ILHEAD ilHead;
2227 HIMAGELIST himl;
2228 int i;
2230 TRACE("%p\n", pstm);
2232 if (FAILED(IStream_Read (pstm, &ilHead, sizeof(ILHEAD), NULL)))
2233 return NULL;
2234 if (ilHead.usMagic != (('L' << 8) | 'I'))
2235 return NULL;
2236 if (ilHead.usVersion != 0x101) /* probably version? */
2237 return NULL;
2239 TRACE("cx %u, cy %u, flags 0x%04x, cCurImage %u, cMaxImage %u\n",
2240 ilHead.cx, ilHead.cy, ilHead.flags, ilHead.cCurImage, ilHead.cMaxImage);
2242 himl = ImageList_Create(ilHead.cx, ilHead.cy, ilHead.flags, ilHead.cCurImage, ilHead.cMaxImage);
2243 if (!himl)
2244 return NULL;
2246 if (!(image_bits = read_bitmap(pstm, image_info)))
2248 WARN("failed to read bitmap from stream\n");
2249 return NULL;
2251 if (ilHead.flags & ILC_MASK)
2253 if (!(mask_bits = read_bitmap(pstm, mask_info)))
2255 WARN("failed to read mask bitmap from stream\n");
2256 return NULL;
2259 else mask_info = NULL;
2261 if (himl->has_alpha && image_info->bmiHeader.biBitCount == 32)
2263 DWORD *ptr = image_bits;
2264 BYTE *mask_ptr = mask_bits;
2265 int stride = himl->cy * image_info->bmiHeader.biWidth;
2267 if (image_info->bmiHeader.biHeight > 0) /* bottom-up */
2269 ptr += image_info->bmiHeader.biHeight * image_info->bmiHeader.biWidth - stride;
2270 mask_ptr += (image_info->bmiHeader.biHeight * image_info->bmiHeader.biWidth - stride) / 8;
2271 stride = -stride;
2272 image_info->bmiHeader.biHeight = himl->cy;
2274 else image_info->bmiHeader.biHeight = -himl->cy;
2276 for (i = 0; i < ilHead.cCurImage; i += TILE_COUNT)
2278 add_dib_bits( himl, i, min( ilHead.cCurImage - i, TILE_COUNT ),
2279 himl->cx, himl->cy, image_info, mask_info, ptr, mask_ptr );
2280 ptr += stride;
2281 mask_ptr += stride / 8;
2284 else
2286 StretchDIBits( himl->hdcImage, 0, 0, image_info->bmiHeader.biWidth, image_info->bmiHeader.biHeight,
2287 0, 0, image_info->bmiHeader.biWidth, image_info->bmiHeader.biHeight,
2288 image_bits, image_info, DIB_RGB_COLORS, SRCCOPY);
2289 if (mask_info)
2290 StretchDIBits( himl->hdcMask, 0, 0, mask_info->bmiHeader.biWidth, mask_info->bmiHeader.biHeight,
2291 0, 0, mask_info->bmiHeader.biWidth, mask_info->bmiHeader.biHeight,
2292 mask_bits, mask_info, DIB_RGB_COLORS, SRCCOPY);
2294 Free( image_bits );
2295 Free( mask_bits );
2297 himl->cCurImage = ilHead.cCurImage;
2298 himl->cMaxImage = ilHead.cMaxImage;
2300 ImageList_SetBkColor(himl,ilHead.bkcolor);
2301 for (i=0;i<4;i++)
2302 ImageList_SetOverlayImage(himl,ilHead.ovls[i],i+1);
2303 return himl;
2307 /*************************************************************************
2308 * ImageList_Remove [COMCTL32.@]
2310 * Removes an image from an image list
2312 * PARAMS
2313 * himl [I] image list handle
2314 * i [I] image index
2316 * RETURNS
2317 * Success: TRUE
2318 * Failure: FALSE
2320 * FIXME: as the image list storage test shows, native comctl32 simply shifts
2321 * images without creating a new bitmap.
2323 BOOL WINAPI
2324 ImageList_Remove (HIMAGELIST himl, INT i)
2326 HBITMAP hbmNewImage, hbmNewMask;
2327 HDC hdcBmp;
2328 SIZE sz;
2330 TRACE("(himl=%p i=%d)\n", himl, i);
2332 if (!is_valid(himl)) {
2333 ERR("Invalid image list handle!\n");
2334 return FALSE;
2337 if ((i < -1) || (i >= himl->cCurImage)) {
2338 TRACE("index out of range! %d\n", i);
2339 return FALSE;
2342 if (i == -1) {
2343 INT nCount;
2345 /* remove all */
2346 if (himl->cCurImage == 0) {
2347 /* remove all on empty ImageList is allowed */
2348 TRACE("remove all on empty ImageList!\n");
2349 return TRUE;
2352 himl->cMaxImage = himl->cInitial + himl->cGrow - 1;
2353 himl->cCurImage = 0;
2354 for (nCount = 0; nCount < MAX_OVERLAYIMAGE; nCount++)
2355 himl->nOvlIdx[nCount] = -1;
2357 hbmNewImage = ImageList_CreateImage(himl->hdcImage, himl, himl->cMaxImage);
2358 SelectObject (himl->hdcImage, hbmNewImage);
2359 DeleteObject (himl->hbmImage);
2360 himl->hbmImage = hbmNewImage;
2362 if (himl->hbmMask) {
2364 imagelist_get_bitmap_size(himl, himl->cMaxImage, &sz);
2365 hbmNewMask = CreateBitmap (sz.cx, sz.cy, 1, 1, NULL);
2366 SelectObject (himl->hdcMask, hbmNewMask);
2367 DeleteObject (himl->hbmMask);
2368 himl->hbmMask = hbmNewMask;
2371 else {
2372 /* delete one image */
2373 TRACE("Remove single image! %d\n", i);
2375 /* create new bitmap(s) */
2376 TRACE(" - Number of images: %d / %d (Old/New)\n",
2377 himl->cCurImage, himl->cCurImage - 1);
2379 hbmNewImage = ImageList_CreateImage(himl->hdcImage, himl, himl->cMaxImage);
2381 imagelist_get_bitmap_size(himl, himl->cMaxImage, &sz );
2382 if (himl->hbmMask)
2383 hbmNewMask = CreateBitmap (sz.cx, sz.cy, 1, 1, NULL);
2384 else
2385 hbmNewMask = 0; /* Just to keep compiler happy! */
2387 hdcBmp = CreateCompatibleDC (0);
2389 /* copy all images and masks prior to the "removed" image */
2390 if (i > 0) {
2391 TRACE("Pre image copy: Copy %d images\n", i);
2393 SelectObject (hdcBmp, hbmNewImage);
2394 imagelist_copy_images( himl, himl->hdcImage, hdcBmp, 0, i, 0 );
2396 if (himl->hbmMask) {
2397 SelectObject (hdcBmp, hbmNewMask);
2398 imagelist_copy_images( himl, himl->hdcMask, hdcBmp, 0, i, 0 );
2402 /* copy all images and masks behind the removed image */
2403 if (i < himl->cCurImage - 1) {
2404 TRACE("Post image copy!\n");
2406 SelectObject (hdcBmp, hbmNewImage);
2407 imagelist_copy_images( himl, himl->hdcImage, hdcBmp, i + 1,
2408 (himl->cCurImage - i), i );
2410 if (himl->hbmMask) {
2411 SelectObject (hdcBmp, hbmNewMask);
2412 imagelist_copy_images( himl, himl->hdcMask, hdcBmp, i + 1,
2413 (himl->cCurImage - i), i );
2417 DeleteDC (hdcBmp);
2419 /* delete old images and insert new ones */
2420 SelectObject (himl->hdcImage, hbmNewImage);
2421 DeleteObject (himl->hbmImage);
2422 himl->hbmImage = hbmNewImage;
2423 if (himl->hbmMask) {
2424 SelectObject (himl->hdcMask, hbmNewMask);
2425 DeleteObject (himl->hbmMask);
2426 himl->hbmMask = hbmNewMask;
2429 himl->cCurImage--;
2432 return TRUE;
2436 /*************************************************************************
2437 * ImageList_Replace [COMCTL32.@]
2439 * Replaces an image in an image list with a new image.
2441 * PARAMS
2442 * himl [I] handle to image list
2443 * i [I] image index
2444 * hbmImage [I] handle to image bitmap
2445 * hbmMask [I] handle to mask bitmap. Can be NULL.
2447 * RETURNS
2448 * Success: TRUE
2449 * Failure: FALSE
2452 BOOL WINAPI
2453 ImageList_Replace (HIMAGELIST himl, INT i, HBITMAP hbmImage,
2454 HBITMAP hbmMask)
2456 HDC hdcImage;
2457 BITMAP bmp;
2458 POINT pt;
2460 TRACE("%p %d %p %p\n", himl, i, hbmImage, hbmMask);
2462 if (!is_valid(himl)) {
2463 ERR("Invalid image list handle!\n");
2464 return FALSE;
2467 if ((i >= himl->cMaxImage) || (i < 0)) {
2468 ERR("Invalid image index!\n");
2469 return FALSE;
2472 if (!GetObjectW(hbmImage, sizeof(BITMAP), &bmp))
2473 return FALSE;
2475 hdcImage = CreateCompatibleDC (0);
2477 /* Replace Image */
2478 SelectObject (hdcImage, hbmImage);
2480 if (add_with_alpha( himl, hdcImage, i, 1, bmp.bmWidth, bmp.bmHeight, hbmImage, hbmMask ))
2481 goto done;
2483 imagelist_point_from_index(himl, i, &pt);
2484 StretchBlt (himl->hdcImage, pt.x, pt.y, himl->cx, himl->cy,
2485 hdcImage, 0, 0, bmp.bmWidth, bmp.bmHeight, SRCCOPY);
2487 if (himl->hbmMask)
2489 HDC hdcTemp;
2490 HBITMAP hOldBitmapTemp;
2492 hdcTemp = CreateCompatibleDC(0);
2493 hOldBitmapTemp = SelectObject(hdcTemp, hbmMask);
2495 StretchBlt (himl->hdcMask, pt.x, pt.y, himl->cx, himl->cy,
2496 hdcTemp, 0, 0, bmp.bmWidth, bmp.bmHeight, SRCCOPY);
2497 SelectObject(hdcTemp, hOldBitmapTemp);
2498 DeleteDC(hdcTemp);
2500 /* Remove the background from the image
2502 BitBlt (himl->hdcImage, pt.x, pt.y, bmp.bmWidth, bmp.bmHeight,
2503 himl->hdcMask, pt.x, pt.y, 0x220326); /* NOTSRCAND */
2506 done:
2507 DeleteDC (hdcImage);
2509 return TRUE;
2513 /*************************************************************************
2514 * ImageList_ReplaceIcon [COMCTL32.@]
2516 * Replaces an image in an image list using an icon.
2518 * PARAMS
2519 * himl [I] handle to image list
2520 * i [I] image index
2521 * hIcon [I] handle to icon
2523 * RETURNS
2524 * Success: index of the replaced image
2525 * Failure: -1
2528 INT WINAPI
2529 ImageList_ReplaceIcon (HIMAGELIST himl, INT nIndex, HICON hIcon)
2531 HDC hdcImage;
2532 HICON hBestFitIcon;
2533 ICONINFO ii;
2534 BITMAP bmp;
2535 BOOL ret;
2536 POINT pt;
2538 TRACE("(%p %d %p)\n", himl, nIndex, hIcon);
2540 if (!is_valid(himl)) {
2541 ERR("invalid image list\n");
2542 return -1;
2544 if ((nIndex >= himl->cMaxImage) || (nIndex < -1)) {
2545 ERR("invalid image index %d / %d\n", nIndex, himl->cMaxImage);
2546 return -1;
2549 hBestFitIcon = CopyImage(
2550 hIcon, IMAGE_ICON,
2551 himl->cx, himl->cy,
2552 LR_COPYFROMRESOURCE);
2553 /* the above will fail if the icon wasn't loaded from a resource, so try
2554 * again without LR_COPYFROMRESOURCE flag */
2555 if (!hBestFitIcon)
2556 hBestFitIcon = CopyImage(
2557 hIcon, IMAGE_ICON,
2558 himl->cx, himl->cy,
2560 if (!hBestFitIcon)
2561 return -1;
2563 ret = GetIconInfo (hBestFitIcon, &ii);
2564 if (!ret) {
2565 DestroyIcon(hBestFitIcon);
2566 return -1;
2569 ret = GetObjectW (ii.hbmMask, sizeof(BITMAP), &bmp);
2570 if (!ret) {
2571 ERR("couldn't get mask bitmap info\n");
2572 if (ii.hbmColor)
2573 DeleteObject (ii.hbmColor);
2574 if (ii.hbmMask)
2575 DeleteObject (ii.hbmMask);
2576 DestroyIcon(hBestFitIcon);
2577 return -1;
2580 if (nIndex == -1) {
2581 if (himl->cCurImage + 1 > himl->cMaxImage)
2582 IMAGELIST_InternalExpandBitmaps(himl, 1);
2584 nIndex = himl->cCurImage;
2585 himl->cCurImage++;
2588 hdcImage = CreateCompatibleDC (0);
2589 TRACE("hdcImage=%p\n", hdcImage);
2590 if (hdcImage == 0)
2591 ERR("invalid hdcImage!\n");
2593 if (himl->has_alpha)
2595 if (!ii.hbmColor)
2597 UINT height = bmp.bmHeight / 2;
2598 HDC hdcMask = CreateCompatibleDC( 0 );
2599 HBITMAP color = CreateBitmap( bmp.bmWidth, height, 1, 1, NULL );
2600 SelectObject( hdcImage, color );
2601 SelectObject( hdcMask, ii.hbmMask );
2602 BitBlt( hdcImage, 0, 0, bmp.bmWidth, height, hdcMask, 0, height, SRCCOPY );
2603 ret = add_with_alpha( himl, hdcImage, nIndex, 1, bmp.bmWidth, height, color, ii.hbmMask );
2604 DeleteDC( hdcMask );
2605 DeleteObject( color );
2606 if (ret) goto done;
2608 else if (add_with_alpha( himl, hdcImage, nIndex, 1, bmp.bmWidth, bmp.bmHeight,
2609 ii.hbmColor, ii.hbmMask )) goto done;
2612 imagelist_point_from_index(himl, nIndex, &pt);
2614 SetTextColor(himl->hdcImage, RGB(0,0,0));
2615 SetBkColor (himl->hdcImage, RGB(255,255,255));
2617 if (ii.hbmColor)
2619 SelectObject (hdcImage, ii.hbmColor);
2620 StretchBlt (himl->hdcImage, pt.x, pt.y, himl->cx, himl->cy,
2621 hdcImage, 0, 0, bmp.bmWidth, bmp.bmHeight, SRCCOPY);
2622 if (himl->hbmMask)
2624 SelectObject (hdcImage, ii.hbmMask);
2625 StretchBlt (himl->hdcMask, pt.x, pt.y, himl->cx, himl->cy,
2626 hdcImage, 0, 0, bmp.bmWidth, bmp.bmHeight, SRCCOPY);
2629 else
2631 UINT height = bmp.bmHeight / 2;
2632 SelectObject (hdcImage, ii.hbmMask);
2633 StretchBlt (himl->hdcImage, pt.x, pt.y, himl->cx, himl->cy,
2634 hdcImage, 0, height, bmp.bmWidth, height, SRCCOPY);
2635 if (himl->hbmMask)
2636 StretchBlt (himl->hdcMask, pt.x, pt.y, himl->cx, himl->cy,
2637 hdcImage, 0, 0, bmp.bmWidth, height, SRCCOPY);
2640 done:
2641 DestroyIcon(hBestFitIcon);
2642 if (hdcImage)
2643 DeleteDC (hdcImage);
2644 if (ii.hbmColor)
2645 DeleteObject (ii.hbmColor);
2646 if (ii.hbmMask)
2647 DeleteObject (ii.hbmMask);
2649 TRACE("Insert index = %d, himl->cCurImage = %d\n", nIndex, himl->cCurImage);
2650 return nIndex;
2654 /*************************************************************************
2655 * ImageList_SetBkColor [COMCTL32.@]
2657 * Sets the background color of an image list.
2659 * PARAMS
2660 * himl [I] handle to image list
2661 * clrBk [I] background color
2663 * RETURNS
2664 * Success: previous background color
2665 * Failure: CLR_NONE
2668 COLORREF WINAPI
2669 ImageList_SetBkColor (HIMAGELIST himl, COLORREF clrBk)
2671 COLORREF clrOldBk;
2673 if (!is_valid(himl))
2674 return CLR_NONE;
2676 clrOldBk = himl->clrBk;
2677 himl->clrBk = clrBk;
2678 return clrOldBk;
2682 /*************************************************************************
2683 * ImageList_SetDragCursorImage [COMCTL32.@]
2685 * Combines the specified image with the current drag image
2687 * PARAMS
2688 * himlDrag [I] handle to drag image list
2689 * iDrag [I] drag image index
2690 * dxHotspot [I] X position of the hot spot
2691 * dyHotspot [I] Y position of the hot spot
2693 * RETURNS
2694 * Success: TRUE
2695 * Failure: FALSE
2697 * NOTES
2698 * - The names dxHotspot, dyHotspot are misleading because they have nothing
2699 * to do with a hotspot but are only the offset of the origin of the new
2700 * image relative to the origin of the old image.
2702 * - When this function is called and the drag image is visible, a
2703 * short flickering occurs but this matches the Win9x behavior. It is
2704 * possible to fix the flickering using code like in ImageList_DragMove.
2707 BOOL WINAPI
2708 ImageList_SetDragCursorImage (HIMAGELIST himlDrag, INT iDrag,
2709 INT dxHotspot, INT dyHotspot)
2711 HIMAGELIST himlTemp;
2712 BOOL visible;
2714 if (!is_valid(InternalDrag.himl) || !is_valid(himlDrag))
2715 return FALSE;
2717 TRACE(" dxH=%d dyH=%d nX=%d nY=%d\n",
2718 dxHotspot, dyHotspot, InternalDrag.dxHotspot, InternalDrag.dyHotspot);
2720 visible = InternalDrag.bShow;
2722 himlTemp = ImageList_Merge (InternalDrag.himl, 0, himlDrag, iDrag,
2723 dxHotspot, dyHotspot);
2725 if (visible) {
2726 /* hide the drag image */
2727 ImageList_DragShowNolock(FALSE);
2729 if ((InternalDrag.himl->cx != himlTemp->cx) ||
2730 (InternalDrag.himl->cy != himlTemp->cy)) {
2731 /* the size of the drag image changed, invalidate the buffer */
2732 DeleteObject(InternalDrag.hbmBg);
2733 InternalDrag.hbmBg = 0;
2736 ImageList_Destroy (InternalDrag.himl);
2737 InternalDrag.himl = himlTemp;
2739 if (visible) {
2740 /* show the drag image */
2741 ImageList_DragShowNolock(TRUE);
2744 return TRUE;
2748 /*************************************************************************
2749 * ImageList_SetFilter [COMCTL32.@]
2751 * Sets a filter (or does something completely different)!!???
2752 * It removes 12 Bytes from the stack (3 Parameters).
2754 * PARAMS
2755 * himl [I] SHOULD be a handle to image list
2756 * i [I] COULD be an index?
2757 * dwFilter [I] ???
2759 * RETURNS
2760 * Success: TRUE ???
2761 * Failure: FALSE ???
2763 * BUGS
2764 * This is an UNDOCUMENTED function!!!!
2765 * empty stub.
2768 BOOL WINAPI
2769 ImageList_SetFilter (HIMAGELIST himl, INT i, DWORD dwFilter)
2771 FIXME("(%p 0x%x 0x%x):empty stub!\n", himl, i, dwFilter);
2773 return FALSE;
2777 /*************************************************************************
2778 * ImageList_SetFlags [COMCTL32.@]
2780 * Sets the image list flags.
2782 * PARAMS
2783 * himl [I] Handle to image list
2784 * flags [I] Flags to set
2786 * RETURNS
2787 * Old flags?
2789 * BUGS
2790 * Stub.
2793 DWORD WINAPI
2794 ImageList_SetFlags(HIMAGELIST himl, DWORD flags)
2796 FIXME("(%p %08x):empty stub\n", himl, flags);
2797 return 0;
2801 /*************************************************************************
2802 * ImageList_SetIconSize [COMCTL32.@]
2804 * Sets the image size of the bitmap and deletes all images.
2806 * PARAMS
2807 * himl [I] handle to image list
2808 * cx [I] image width
2809 * cy [I] image height
2811 * RETURNS
2812 * Success: TRUE
2813 * Failure: FALSE
2816 BOOL WINAPI
2817 ImageList_SetIconSize (HIMAGELIST himl, INT cx, INT cy)
2819 INT nCount;
2820 HBITMAP hbmNew;
2822 if (!is_valid(himl))
2823 return FALSE;
2825 /* remove all images */
2826 himl->cMaxImage = himl->cInitial + 1;
2827 himl->cCurImage = 0;
2828 himl->cx = cx;
2829 himl->cy = cy;
2831 /* initialize overlay mask indices */
2832 for (nCount = 0; nCount < MAX_OVERLAYIMAGE; nCount++)
2833 himl->nOvlIdx[nCount] = -1;
2835 hbmNew = ImageList_CreateImage(himl->hdcImage, himl, himl->cMaxImage);
2836 SelectObject (himl->hdcImage, hbmNew);
2837 DeleteObject (himl->hbmImage);
2838 himl->hbmImage = hbmNew;
2840 if (himl->hbmMask) {
2841 SIZE sz;
2842 imagelist_get_bitmap_size(himl, himl->cMaxImage, &sz);
2843 hbmNew = CreateBitmap (sz.cx, sz.cy, 1, 1, NULL);
2844 SelectObject (himl->hdcMask, hbmNew);
2845 DeleteObject (himl->hbmMask);
2846 himl->hbmMask = hbmNew;
2849 return TRUE;
2853 /*************************************************************************
2854 * ImageList_SetImageCount [COMCTL32.@]
2856 * Resizes an image list to the specified number of images.
2858 * PARAMS
2859 * himl [I] handle to image list
2860 * iImageCount [I] number of images in the image list
2862 * RETURNS
2863 * Success: TRUE
2864 * Failure: FALSE
2867 BOOL WINAPI
2868 ImageList_SetImageCount (HIMAGELIST himl, UINT iImageCount)
2870 HDC hdcBitmap;
2871 HBITMAP hbmNewBitmap, hbmOld;
2872 INT nNewCount, nCopyCount;
2874 TRACE("%p %d\n",himl,iImageCount);
2876 if (!is_valid(himl))
2877 return FALSE;
2878 if (himl->cMaxImage > iImageCount)
2880 himl->cCurImage = iImageCount;
2881 /* TODO: shrink the bitmap when cMaxImage-cCurImage>cGrow ? */
2882 return TRUE;
2885 nNewCount = iImageCount + himl->cGrow;
2886 nCopyCount = min(himl->cCurImage, iImageCount);
2888 hdcBitmap = CreateCompatibleDC (0);
2890 hbmNewBitmap = ImageList_CreateImage(hdcBitmap, himl, nNewCount);
2892 if (hbmNewBitmap != 0)
2894 hbmOld = SelectObject (hdcBitmap, hbmNewBitmap);
2895 imagelist_copy_images( himl, himl->hdcImage, hdcBitmap, 0, nCopyCount, 0 );
2896 SelectObject (hdcBitmap, hbmOld);
2898 /* FIXME: delete 'empty' image space? */
2900 SelectObject (himl->hdcImage, hbmNewBitmap);
2901 DeleteObject (himl->hbmImage);
2902 himl->hbmImage = hbmNewBitmap;
2904 else
2905 ERR("Could not create new image bitmap !\n");
2907 if (himl->hbmMask)
2909 SIZE sz;
2910 imagelist_get_bitmap_size( himl, nNewCount, &sz );
2911 hbmNewBitmap = CreateBitmap (sz.cx, sz.cy, 1, 1, NULL);
2912 if (hbmNewBitmap != 0)
2914 hbmOld = SelectObject (hdcBitmap, hbmNewBitmap);
2915 imagelist_copy_images( himl, himl->hdcMask, hdcBitmap, 0, nCopyCount, 0 );
2916 SelectObject (hdcBitmap, hbmOld);
2918 /* FIXME: delete 'empty' image space? */
2920 SelectObject (himl->hdcMask, hbmNewBitmap);
2921 DeleteObject (himl->hbmMask);
2922 himl->hbmMask = hbmNewBitmap;
2924 else
2925 ERR("Could not create new mask bitmap!\n");
2928 DeleteDC (hdcBitmap);
2930 if (himl->has_alpha)
2932 char *new_alpha = HeapReAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, himl->has_alpha, nNewCount );
2933 if (new_alpha) himl->has_alpha = new_alpha;
2934 else
2936 HeapFree( GetProcessHeap(), 0, himl->has_alpha );
2937 himl->has_alpha = NULL;
2941 /* Update max image count and current image count */
2942 himl->cMaxImage = nNewCount;
2943 himl->cCurImage = iImageCount;
2945 return TRUE;
2949 /*************************************************************************
2950 * ImageList_SetOverlayImage [COMCTL32.@]
2952 * Assigns an overlay mask index to an existing image in an image list.
2954 * PARAMS
2955 * himl [I] handle to image list
2956 * iImage [I] image index
2957 * iOverlay [I] overlay mask index
2959 * RETURNS
2960 * Success: TRUE
2961 * Failure: FALSE
2964 BOOL WINAPI
2965 ImageList_SetOverlayImage (HIMAGELIST himl, INT iImage, INT iOverlay)
2967 if (!is_valid(himl))
2968 return FALSE;
2969 if ((iOverlay < 1) || (iOverlay > MAX_OVERLAYIMAGE))
2970 return FALSE;
2971 if ((iImage!=-1) && ((iImage < 0) || (iImage > himl->cCurImage)))
2972 return FALSE;
2973 himl->nOvlIdx[iOverlay - 1] = iImage;
2974 return TRUE;
2979 /* helper for ImageList_Write - write bitmap to pstm
2980 * currently everything is written as 24 bit RGB, except masks
2982 static BOOL
2983 _write_bitmap(HBITMAP hBitmap, LPSTREAM pstm)
2985 LPBITMAPFILEHEADER bmfh;
2986 LPBITMAPINFOHEADER bmih;
2987 LPBYTE data = NULL, lpBits;
2988 BITMAP bm;
2989 INT bitCount, sizeImage, offBits, totalSize;
2990 HDC xdc;
2991 BOOL result = FALSE;
2993 if (!GetObjectW(hBitmap, sizeof(BITMAP), &bm))
2994 return FALSE;
2996 bitCount = bm.bmBitsPixel == 1 ? 1 : 24;
2997 sizeImage = get_dib_stride(bm.bmWidth, bitCount) * bm.bmHeight;
2999 totalSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
3000 if(bitCount != 24)
3001 totalSize += (1 << bitCount) * sizeof(RGBQUAD);
3002 offBits = totalSize;
3003 totalSize += sizeImage;
3005 data = Alloc(totalSize);
3006 bmfh = (LPBITMAPFILEHEADER)data;
3007 bmih = (LPBITMAPINFOHEADER)(data + sizeof(BITMAPFILEHEADER));
3008 lpBits = data + offBits;
3010 /* setup BITMAPFILEHEADER */
3011 bmfh->bfType = (('M' << 8) | 'B');
3012 bmfh->bfSize = offBits;
3013 bmfh->bfReserved1 = 0;
3014 bmfh->bfReserved2 = 0;
3015 bmfh->bfOffBits = offBits;
3017 /* setup BITMAPINFOHEADER */
3018 bmih->biSize = sizeof(BITMAPINFOHEADER);
3019 bmih->biWidth = bm.bmWidth;
3020 bmih->biHeight = bm.bmHeight;
3021 bmih->biPlanes = 1;
3022 bmih->biBitCount = bitCount;
3023 bmih->biCompression = BI_RGB;
3024 bmih->biSizeImage = sizeImage;
3025 bmih->biXPelsPerMeter = 0;
3026 bmih->biYPelsPerMeter = 0;
3027 bmih->biClrUsed = 0;
3028 bmih->biClrImportant = 0;
3030 xdc = GetDC(0);
3031 result = GetDIBits(xdc, hBitmap, 0, bm.bmHeight, lpBits, (BITMAPINFO *)bmih, DIB_RGB_COLORS) == bm.bmHeight;
3032 ReleaseDC(0, xdc);
3033 if (!result)
3034 goto failed;
3036 TRACE("width %u, height %u, planes %u, bpp %u\n",
3037 bmih->biWidth, bmih->biHeight,
3038 bmih->biPlanes, bmih->biBitCount);
3040 if(bitCount == 1) {
3041 /* Hack. */
3042 LPBITMAPINFO inf = (LPBITMAPINFO)bmih;
3043 inf->bmiColors[0].rgbRed = inf->bmiColors[0].rgbGreen = inf->bmiColors[0].rgbBlue = 0;
3044 inf->bmiColors[1].rgbRed = inf->bmiColors[1].rgbGreen = inf->bmiColors[1].rgbBlue = 0xff;
3047 if(FAILED(IStream_Write(pstm, data, totalSize, NULL)))
3048 goto failed;
3050 result = TRUE;
3052 failed:
3053 Free(data);
3055 return result;
3059 /*************************************************************************
3060 * ImageList_Write [COMCTL32.@]
3062 * Writes an image list to a stream.
3064 * PARAMS
3065 * himl [I] handle to image list
3066 * pstm [O] Pointer to a stream.
3068 * RETURNS
3069 * Success: TRUE
3070 * Failure: FALSE
3072 * BUGS
3073 * probably.
3076 BOOL WINAPI
3077 ImageList_Write (HIMAGELIST himl, LPSTREAM pstm)
3079 ILHEAD ilHead;
3080 int i;
3082 TRACE("%p %p\n", himl, pstm);
3084 if (!is_valid(himl))
3085 return FALSE;
3087 ilHead.usMagic = (('L' << 8) | 'I');
3088 ilHead.usVersion = 0x101;
3089 ilHead.cCurImage = himl->cCurImage;
3090 ilHead.cMaxImage = himl->cMaxImage;
3091 ilHead.cGrow = himl->cGrow;
3092 ilHead.cx = himl->cx;
3093 ilHead.cy = himl->cy;
3094 ilHead.bkcolor = himl->clrBk;
3095 ilHead.flags = himl->flags;
3096 for(i = 0; i < 4; i++) {
3097 ilHead.ovls[i] = himl->nOvlIdx[i];
3100 TRACE("cx %u, cy %u, flags 0x04%x, cCurImage %u, cMaxImage %u\n",
3101 ilHead.cx, ilHead.cy, ilHead.flags, ilHead.cCurImage, ilHead.cMaxImage);
3103 if(FAILED(IStream_Write(pstm, &ilHead, sizeof(ILHEAD), NULL)))
3104 return FALSE;
3106 /* write the bitmap */
3107 if(!_write_bitmap(himl->hbmImage, pstm))
3108 return FALSE;
3110 /* write the mask if we have one */
3111 if(himl->flags & ILC_MASK) {
3112 if(!_write_bitmap(himl->hbmMask, pstm))
3113 return FALSE;
3116 return TRUE;
3120 static HBITMAP ImageList_CreateImage(HDC hdc, HIMAGELIST himl, UINT count)
3122 HBITMAP hbmNewBitmap;
3123 UINT ilc = (himl->flags & 0xFE);
3124 SIZE sz;
3126 imagelist_get_bitmap_size( himl, count, &sz );
3128 if ((ilc >= ILC_COLOR4 && ilc <= ILC_COLOR32) || ilc == ILC_COLOR)
3130 VOID* bits;
3131 BITMAPINFO *bmi;
3133 TRACE("Creating DIBSection %d x %d, %d Bits per Pixel\n",
3134 sz.cx, sz.cy, himl->uBitsPixel);
3136 if (himl->uBitsPixel <= ILC_COLOR8)
3138 LPPALETTEENTRY pal;
3139 ULONG i, colors;
3140 BYTE temp;
3142 colors = 1 << himl->uBitsPixel;
3143 bmi = Alloc(sizeof(BITMAPINFOHEADER) +
3144 sizeof(PALETTEENTRY) * colors);
3146 pal = (LPPALETTEENTRY)bmi->bmiColors;
3147 GetPaletteEntries(GetStockObject(DEFAULT_PALETTE), 0, colors, pal);
3149 /* Swap colors returned by GetPaletteEntries so we can use them for
3150 * CreateDIBSection call. */
3151 for (i = 0; i < colors; i++)
3153 temp = pal[i].peBlue;
3154 bmi->bmiColors[i].rgbRed = pal[i].peRed;
3155 bmi->bmiColors[i].rgbBlue = temp;
3158 else
3160 bmi = Alloc(sizeof(BITMAPINFOHEADER));
3163 bmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
3164 bmi->bmiHeader.biWidth = sz.cx;
3165 bmi->bmiHeader.biHeight = sz.cy;
3166 bmi->bmiHeader.biPlanes = 1;
3167 bmi->bmiHeader.biBitCount = himl->uBitsPixel;
3168 bmi->bmiHeader.biCompression = BI_RGB;
3169 bmi->bmiHeader.biSizeImage = 0;
3170 bmi->bmiHeader.biXPelsPerMeter = 0;
3171 bmi->bmiHeader.biYPelsPerMeter = 0;
3172 bmi->bmiHeader.biClrUsed = 0;
3173 bmi->bmiHeader.biClrImportant = 0;
3175 hbmNewBitmap = CreateDIBSection(hdc, bmi, DIB_RGB_COLORS, &bits, 0, 0);
3177 Free (bmi);
3179 else /*if (ilc == ILC_COLORDDB)*/
3181 TRACE("Creating Bitmap: %d Bits per Pixel\n", himl->uBitsPixel);
3183 hbmNewBitmap = CreateBitmap (sz.cx, sz.cy, 1, himl->uBitsPixel, NULL);
3185 TRACE("returning %p\n", hbmNewBitmap);
3186 return hbmNewBitmap;
3189 /*************************************************************************
3190 * ImageList_SetColorTable [COMCTL32.@]
3192 * Sets the color table of an image list.
3194 * PARAMS
3195 * himl [I] Handle to the image list.
3196 * uStartIndex [I] The first index to set.
3197 * cEntries [I] Number of entries to set.
3198 * prgb [I] New color information for color table for the image list.
3200 * RETURNS
3201 * Success: Number of entries in the table that were set.
3202 * Failure: Zero.
3204 * SEE
3205 * ImageList_Create(), SetDIBColorTable()
3208 UINT WINAPI
3209 ImageList_SetColorTable (HIMAGELIST himl, UINT uStartIndex, UINT cEntries, CONST RGBQUAD * prgb)
3211 return SetDIBColorTable(himl->hdcImage, uStartIndex, cEntries, prgb);
3214 /*************************************************************************
3215 * ImageList_CoCreateInstance [COMCTL32.@]
3217 * Creates a new imagelist instance and returns an interface pointer to it.
3219 * PARAMS
3220 * rclsid [I] A reference to the CLSID (CLSID_ImageList).
3221 * punkOuter [I] Pointer to IUnknown interface for aggregation, if desired
3222 * riid [I] Identifier of the requested interface.
3223 * ppv [O] Returns the address of the pointer requested, or NULL.
3225 * RETURNS
3226 * Success: S_OK.
3227 * Failure: Error value.
3229 HRESULT WINAPI
3230 ImageList_CoCreateInstance (REFCLSID rclsid, const IUnknown *punkOuter, REFIID riid, void **ppv)
3232 TRACE("(%s,%p,%s,%p)\n", debugstr_guid(rclsid), punkOuter, debugstr_guid(riid), ppv);
3234 if (!IsEqualCLSID(&CLSID_ImageList, rclsid))
3235 return E_NOINTERFACE;
3237 return ImageListImpl_CreateInstance(punkOuter, riid, ppv);
3241 /*************************************************************************
3242 * IImageList implementation
3245 static HRESULT WINAPI ImageListImpl_QueryInterface(IImageList *iface,
3246 REFIID iid, void **ppv)
3248 HIMAGELIST This = (HIMAGELIST) iface;
3249 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
3251 if (!ppv) return E_INVALIDARG;
3253 if (IsEqualIID(&IID_IUnknown, iid) || IsEqualIID(&IID_IImageList, iid))
3254 *ppv = This;
3255 else
3257 *ppv = NULL;
3258 return E_NOINTERFACE;
3261 IUnknown_AddRef((IUnknown*)*ppv);
3262 return S_OK;
3265 static ULONG WINAPI ImageListImpl_AddRef(IImageList *iface)
3267 HIMAGELIST This = (HIMAGELIST) iface;
3268 ULONG ref = InterlockedIncrement(&This->ref);
3270 TRACE("(%p) refcount=%u\n", iface, ref);
3271 return ref;
3274 static ULONG WINAPI ImageListImpl_Release(IImageList *iface)
3276 HIMAGELIST This = (HIMAGELIST) iface;
3277 ULONG ref = InterlockedDecrement(&This->ref);
3279 TRACE("(%p) refcount=%u\n", iface, ref);
3281 if (ref == 0)
3283 /* delete image bitmaps */
3284 if (This->hbmImage) DeleteObject (This->hbmImage);
3285 if (This->hbmMask) DeleteObject (This->hbmMask);
3287 /* delete image & mask DCs */
3288 if (This->hdcImage) DeleteDC (This->hdcImage);
3289 if (This->hdcMask) DeleteDC (This->hdcMask);
3291 /* delete blending brushes */
3292 if (This->hbrBlend25) DeleteObject (This->hbrBlend25);
3293 if (This->hbrBlend50) DeleteObject (This->hbrBlend50);
3295 This->lpVtbl = NULL;
3296 HeapFree(GetProcessHeap(), 0, This->has_alpha);
3297 HeapFree(GetProcessHeap(), 0, This);
3300 return ref;
3303 static HRESULT WINAPI ImageListImpl_Add(IImageList *iface, HBITMAP hbmImage,
3304 HBITMAP hbmMask, int *pi)
3306 HIMAGELIST This = (HIMAGELIST) iface;
3307 int ret;
3309 if (!pi)
3310 return E_FAIL;
3312 ret = ImageList_Add(This, hbmImage, hbmMask);
3314 if (ret == -1)
3315 return E_FAIL;
3317 *pi = ret;
3318 return S_OK;
3321 static HRESULT WINAPI ImageListImpl_ReplaceIcon(IImageList *iface, int i,
3322 HICON hicon, int *pi)
3324 HIMAGELIST This = (HIMAGELIST) iface;
3325 int ret;
3327 if (!pi)
3328 return E_FAIL;
3330 ret = ImageList_ReplaceIcon(This, i, hicon);
3332 if (ret == -1)
3333 return E_FAIL;
3335 *pi = ret;
3336 return S_OK;
3339 static HRESULT WINAPI ImageListImpl_SetOverlayImage(IImageList *iface,
3340 int iImage, int iOverlay)
3342 return ImageList_SetOverlayImage((HIMAGELIST) iface, iImage, iOverlay)
3343 ? S_OK : E_FAIL;
3346 static HRESULT WINAPI ImageListImpl_Replace(IImageList *iface, int i,
3347 HBITMAP hbmImage, HBITMAP hbmMask)
3349 return ImageList_Replace((HIMAGELIST) iface, i, hbmImage, hbmMask) ? S_OK :
3350 E_FAIL;
3353 static HRESULT WINAPI ImageListImpl_AddMasked(IImageList *iface, HBITMAP hbmImage,
3354 COLORREF crMask, int *pi)
3356 HIMAGELIST This = (HIMAGELIST) iface;
3357 int ret;
3359 if (!pi)
3360 return E_FAIL;
3362 ret = ImageList_AddMasked(This, hbmImage, crMask);
3364 if (ret == -1)
3365 return E_FAIL;
3367 *pi = ret;
3368 return S_OK;
3371 static HRESULT WINAPI ImageListImpl_Draw(IImageList *iface,
3372 IMAGELISTDRAWPARAMS *pimldp)
3374 HIMAGELIST This = (HIMAGELIST) iface;
3375 HIMAGELIST old_himl;
3376 int ret;
3378 /* As far as I can tell, Windows simply ignores the contents of pimldp->himl
3379 so we shall simulate the same */
3380 old_himl = pimldp->himl;
3381 pimldp->himl = This;
3383 ret = ImageList_DrawIndirect(pimldp);
3385 pimldp->himl = old_himl;
3386 return ret ? S_OK : E_INVALIDARG;
3389 static HRESULT WINAPI ImageListImpl_Remove(IImageList *iface, int i)
3391 return (ImageList_Remove((HIMAGELIST) iface, i) == 0) ? E_INVALIDARG : S_OK;
3394 static HRESULT WINAPI ImageListImpl_GetIcon(IImageList *iface, int i, UINT flags,
3395 HICON *picon)
3397 HICON hIcon;
3399 if (!picon)
3400 return E_FAIL;
3402 hIcon = ImageList_GetIcon((HIMAGELIST) iface, i, flags);
3404 if (hIcon == NULL)
3405 return E_FAIL;
3407 *picon = hIcon;
3408 return S_OK;
3411 static HRESULT WINAPI ImageListImpl_GetImageInfo(IImageList *iface, int i,
3412 IMAGEINFO *pImageInfo)
3414 return ImageList_GetImageInfo((HIMAGELIST) iface, i, pImageInfo) ? S_OK : E_FAIL;
3417 static HRESULT WINAPI ImageListImpl_Copy(IImageList *iface, int iDst,
3418 IUnknown *punkSrc, int iSrc, UINT uFlags)
3420 HIMAGELIST This = (HIMAGELIST) iface;
3421 IImageList *src = NULL;
3422 HRESULT ret;
3424 if (!punkSrc)
3425 return E_FAIL;
3427 /* TODO: Add test for IID_ImageList2 too */
3428 if (FAILED(IImageList_QueryInterface(punkSrc, &IID_IImageList,
3429 (void **) &src)))
3430 return E_FAIL;
3432 if (ImageList_Copy(This, iDst, (HIMAGELIST) src, iSrc, uFlags))
3433 ret = S_OK;
3434 else
3435 ret = E_FAIL;
3437 IImageList_Release(src);
3438 return ret;
3441 static HRESULT WINAPI ImageListImpl_Merge(IImageList *iface, int i1,
3442 IUnknown *punk2, int i2, int dx, int dy, REFIID riid, void **ppv)
3444 HIMAGELIST This = (HIMAGELIST) iface;
3445 IImageList *iml2 = NULL;
3446 HIMAGELIST hNew;
3447 HRESULT ret = E_FAIL;
3449 TRACE("(%p)->(%d %p %d %d %d %s %p)\n", iface, i1, punk2, i2, dx, dy, debugstr_guid(riid), ppv);
3451 /* TODO: Add test for IID_ImageList2 too */
3452 if (FAILED(IImageList_QueryInterface(punk2, &IID_IImageList,
3453 (void **) &iml2)))
3454 return E_FAIL;
3456 hNew = ImageList_Merge(This, i1, (HIMAGELIST) iml2, i2, dx, dy);
3458 /* Get the interface for the new image list */
3459 if (hNew)
3461 IImageList *imerge = (IImageList*)hNew;
3463 ret = HIMAGELIST_QueryInterface(hNew, riid, ppv);
3464 IImageList_Release(imerge);
3467 IImageList_Release(iml2);
3468 return ret;
3471 static HRESULT WINAPI ImageListImpl_Clone(IImageList *iface, REFIID riid, void **ppv)
3473 HIMAGELIST This = (HIMAGELIST) iface;
3474 HIMAGELIST clone;
3475 HRESULT ret = E_FAIL;
3477 TRACE("(%p)->(%s %p)\n", iface, debugstr_guid(riid), ppv);
3479 clone = ImageList_Duplicate(This);
3481 /* Get the interface for the new image list */
3482 if (clone)
3484 IImageList *iclone = (IImageList*)clone;
3486 ret = HIMAGELIST_QueryInterface(clone, riid, ppv);
3487 IImageList_Release(iclone);
3490 return ret;
3493 static HRESULT WINAPI ImageListImpl_GetImageRect(IImageList *iface, int i,
3494 RECT *prc)
3496 HIMAGELIST This = (HIMAGELIST) iface;
3497 IMAGEINFO info;
3499 if (!prc)
3500 return E_FAIL;
3502 if (!ImageList_GetImageInfo(This, i, &info))
3503 return E_FAIL;
3505 return CopyRect(prc, &info.rcImage) ? S_OK : E_FAIL;
3508 static HRESULT WINAPI ImageListImpl_GetIconSize(IImageList *iface, int *cx,
3509 int *cy)
3511 HIMAGELIST This = (HIMAGELIST) iface;
3513 return ImageList_GetIconSize(This, cx, cy) ? S_OK : E_INVALIDARG;
3516 static HRESULT WINAPI ImageListImpl_SetIconSize(IImageList *iface, int cx,
3517 int cy)
3519 return ImageList_SetIconSize((HIMAGELIST) iface, cx, cy) ? S_OK : E_FAIL;
3522 static HRESULT WINAPI ImageListImpl_GetImageCount(IImageList *iface, int *pi)
3524 *pi = ImageList_GetImageCount((HIMAGELIST) iface);
3525 return S_OK;
3528 static HRESULT WINAPI ImageListImpl_SetImageCount(IImageList *iface,
3529 UINT uNewCount)
3531 return ImageList_SetImageCount((HIMAGELIST) iface, uNewCount) ? S_OK : E_FAIL;
3534 static HRESULT WINAPI ImageListImpl_SetBkColor(IImageList *iface, COLORREF clrBk,
3535 COLORREF *pclr)
3537 *pclr = ImageList_SetBkColor((HIMAGELIST) iface, clrBk);
3538 return S_OK;
3541 static HRESULT WINAPI ImageListImpl_GetBkColor(IImageList *iface, COLORREF *pclr)
3543 *pclr = ImageList_GetBkColor((HIMAGELIST) iface);
3544 return S_OK;
3547 static HRESULT WINAPI ImageListImpl_BeginDrag(IImageList *iface, int iTrack,
3548 int dxHotspot, int dyHotspot)
3550 return ImageList_BeginDrag((HIMAGELIST) iface, iTrack, dxHotspot, dyHotspot) ? S_OK : E_FAIL;
3553 static HRESULT WINAPI ImageListImpl_EndDrag(IImageList *iface)
3555 ImageList_EndDrag();
3556 return S_OK;
3559 static HRESULT WINAPI ImageListImpl_DragEnter(IImageList *iface, HWND hwndLock,
3560 int x, int y)
3562 return ImageList_DragEnter(hwndLock, x, y) ? S_OK : E_FAIL;
3565 static HRESULT WINAPI ImageListImpl_DragLeave(IImageList *iface, HWND hwndLock)
3567 return ImageList_DragLeave(hwndLock) ? S_OK : E_FAIL;
3570 static HRESULT WINAPI ImageListImpl_DragMove(IImageList *iface, int x, int y)
3572 return ImageList_DragMove(x, y) ? S_OK : E_FAIL;
3575 static HRESULT WINAPI ImageListImpl_SetDragCursorImage(IImageList *iface,
3576 IUnknown *punk, int iDrag, int dxHotspot, int dyHotspot)
3578 IImageList *iml2 = NULL;
3579 HRESULT ret;
3581 if (!punk)
3582 return E_FAIL;
3584 /* TODO: Add test for IID_ImageList2 too */
3585 if (FAILED(IImageList_QueryInterface(punk, &IID_IImageList,
3586 (void **) &iml2)))
3587 return E_FAIL;
3589 ret = ImageList_SetDragCursorImage((HIMAGELIST) iml2, iDrag, dxHotspot,
3590 dyHotspot);
3592 IImageList_Release(iml2);
3594 return ret ? S_OK : E_FAIL;
3597 static HRESULT WINAPI ImageListImpl_DragShowNolock(IImageList *iface, BOOL fShow)
3599 return ImageList_DragShowNolock(fShow) ? S_OK : E_FAIL;
3602 static HRESULT WINAPI ImageListImpl_GetDragImage(IImageList *iface, POINT *ppt,
3603 POINT *pptHotspot, REFIID riid, PVOID *ppv)
3605 HRESULT ret = E_FAIL;
3606 HIMAGELIST hNew;
3608 if (!ppv)
3609 return E_FAIL;
3611 hNew = ImageList_GetDragImage(ppt, pptHotspot);
3613 /* Get the interface for the new image list */
3614 if (hNew)
3616 IImageList *idrag = (IImageList*)hNew;
3618 ret = HIMAGELIST_QueryInterface(hNew, riid, ppv);
3619 IImageList_Release(idrag);
3622 return ret;
3625 static HRESULT WINAPI ImageListImpl_GetItemFlags(IImageList *iface, int i,
3626 DWORD *dwFlags)
3628 FIXME("STUB: %p %d %p\n", iface, i, dwFlags);
3629 return E_NOTIMPL;
3632 static HRESULT WINAPI ImageListImpl_GetOverlayImage(IImageList *iface, int iOverlay,
3633 int *piIndex)
3635 HIMAGELIST This = (HIMAGELIST) iface;
3636 int i;
3638 if ((iOverlay < 0) || (iOverlay > This->cCurImage))
3639 return E_FAIL;
3641 for (i = 0; i < MAX_OVERLAYIMAGE; i++)
3643 if (This->nOvlIdx[i] == iOverlay)
3645 *piIndex = i + 1;
3646 return S_OK;
3650 return E_FAIL;
3654 static const IImageListVtbl ImageListImpl_Vtbl = {
3655 ImageListImpl_QueryInterface,
3656 ImageListImpl_AddRef,
3657 ImageListImpl_Release,
3658 ImageListImpl_Add,
3659 ImageListImpl_ReplaceIcon,
3660 ImageListImpl_SetOverlayImage,
3661 ImageListImpl_Replace,
3662 ImageListImpl_AddMasked,
3663 ImageListImpl_Draw,
3664 ImageListImpl_Remove,
3665 ImageListImpl_GetIcon,
3666 ImageListImpl_GetImageInfo,
3667 ImageListImpl_Copy,
3668 ImageListImpl_Merge,
3669 ImageListImpl_Clone,
3670 ImageListImpl_GetImageRect,
3671 ImageListImpl_GetIconSize,
3672 ImageListImpl_SetIconSize,
3673 ImageListImpl_GetImageCount,
3674 ImageListImpl_SetImageCount,
3675 ImageListImpl_SetBkColor,
3676 ImageListImpl_GetBkColor,
3677 ImageListImpl_BeginDrag,
3678 ImageListImpl_EndDrag,
3679 ImageListImpl_DragEnter,
3680 ImageListImpl_DragLeave,
3681 ImageListImpl_DragMove,
3682 ImageListImpl_SetDragCursorImage,
3683 ImageListImpl_DragShowNolock,
3684 ImageListImpl_GetDragImage,
3685 ImageListImpl_GetItemFlags,
3686 ImageListImpl_GetOverlayImage
3689 static BOOL is_valid(HIMAGELIST himl)
3691 BOOL valid;
3692 __TRY
3694 valid = himl && himl->lpVtbl == &ImageListImpl_Vtbl;
3696 __EXCEPT_PAGE_FAULT
3698 valid = FALSE;
3700 __ENDTRY
3701 return valid;
3704 /*************************************************************************
3705 * HIMAGELIST_QueryInterface [COMCTL32.@]
3707 * Returns a pointer to an IImageList or IImageList2 object for the given
3708 * HIMAGELIST.
3710 * PARAMS
3711 * himl [I] Image list handle.
3712 * riid [I] Identifier of the requested interface.
3713 * ppv [O] Returns the address of the pointer requested, or NULL.
3715 * RETURNS
3716 * Success: S_OK.
3717 * Failure: Error value.
3719 HRESULT WINAPI
3720 HIMAGELIST_QueryInterface (HIMAGELIST himl, REFIID riid, void **ppv)
3722 TRACE("(%p,%s,%p)\n", himl, debugstr_guid(riid), ppv);
3723 return IImageList_QueryInterface((IImageList *) himl, riid, ppv);
3726 static HRESULT ImageListImpl_CreateInstance(const IUnknown *pUnkOuter, REFIID iid, void** ppv)
3728 HIMAGELIST This;
3729 HRESULT ret;
3731 TRACE("(%p,%s,%p)\n", pUnkOuter, debugstr_guid(iid), ppv);
3733 *ppv = NULL;
3735 if (pUnkOuter) return CLASS_E_NOAGGREGATION;
3737 This = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct _IMAGELIST));
3738 if (!This) return E_OUTOFMEMORY;
3740 This->lpVtbl = &ImageListImpl_Vtbl;
3741 This->ref = 1;
3743 ret = IUnknown_QueryInterface((IUnknown*)This, iid, ppv);
3744 IUnknown_Release((IUnknown*)This);
3746 return ret;