po: Update Lithuanian translation.
[wine.git] / dlls / comctl32 / imagelist.c
blob9a6ea3bbffcc50147dcdb18efa99a5743068a5f1
1 /*
2 * ImageList implementation
4 * Copyright 1998 Eric Kohl
5 * Copyright 2000 Jason Mawdsley
6 * Copyright 2001, 2004 Michael Stefaniuc
7 * Copyright 2001 Charles Loep for CodeWeavers
8 * Copyright 2002 Dimitrie O. Paun
9 * Copyright 2009 Owen Rudge for CodeWeavers
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 * TODO:
26 * - Add support for ILD_PRESERVEALPHA, ILD_SCALE, ILD_DPISCALE
27 * - Add support for ILS_GLOW, ILS_SHADOW, ILS_SATURATE
28 * - Thread-safe locking
31 #include <stdarg.h>
32 #include <stdlib.h>
33 #include <string.h>
35 #define COBJMACROS
37 #include "winerror.h"
38 #include "windef.h"
39 #include "winbase.h"
40 #include "objbase.h"
41 #include "wingdi.h"
42 #include "winuser.h"
43 #include "commctrl.h"
44 #include "comctl32.h"
45 #include "commoncontrols.h"
46 #include "wine/debug.h"
47 #include "wine/exception.h"
48 #include "wine/heap.h"
50 WINE_DEFAULT_DEBUG_CHANNEL(imagelist);
52 #define MAX_OVERLAYIMAGE 15
54 struct _IMAGELIST
56 IImageList2 IImageList2_iface; /* 00: IImageList vtable */
57 INT cCurImage; /* 04: ImageCount */
58 INT cMaxImage; /* 08: maximages */
59 INT cGrow; /* 0C: cGrow */
60 INT cx; /* 10: cx */
61 INT cy; /* 14: cy */
62 DWORD x4;
63 UINT flags; /* 1C: flags */
64 COLORREF clrFg; /* 20: foreground color */
65 COLORREF clrBk; /* 24: background color */
68 HBITMAP hbmImage; /* 28: images Bitmap */
69 HBITMAP hbmMask; /* 2C: masks Bitmap */
70 HDC hdcImage; /* 30: images MemDC */
71 HDC hdcMask; /* 34: masks MemDC */
72 INT nOvlIdx[MAX_OVERLAYIMAGE]; /* 38: overlay images index */
74 /* not yet found out */
75 HBRUSH hbrBlend25;
76 HBRUSH hbrBlend50;
77 INT cInitial;
78 UINT uBitsPixel;
79 char *has_alpha;
80 BOOL color_table_set;
82 LONG ref; /* reference count */
85 #define IMAGELIST_MAGIC 0x53414D58
87 /* Header used by ImageList_Read() and ImageList_Write() */
88 #include "pshpack2.h"
89 typedef struct _ILHEAD
91 USHORT usMagic;
92 USHORT usVersion;
93 WORD cCurImage;
94 WORD cMaxImage;
95 WORD cGrow;
96 WORD cx;
97 WORD cy;
98 COLORREF bkcolor;
99 WORD flags;
100 SHORT ovls[4];
101 } ILHEAD;
102 #include "poppack.h"
104 /* internal image list data used for Drag & Drop operations */
105 typedef struct
107 HWND hwnd;
108 HIMAGELIST himl;
109 HIMAGELIST himlNoCursor;
110 /* position of the drag image relative to the window */
111 INT x;
112 INT y;
113 /* offset of the hotspot relative to the origin of the image */
114 INT dxHotspot;
115 INT dyHotspot;
116 /* is the drag image visible */
117 BOOL bShow;
118 /* saved background */
119 HBITMAP hbmBg;
120 } INTERNALDRAG;
122 static INTERNALDRAG InternalDrag = { 0, 0, 0, 0, 0, 0, 0, FALSE, 0 };
124 static inline HIMAGELIST impl_from_IImageList2(IImageList2 *iface)
126 return CONTAINING_RECORD(iface, struct _IMAGELIST, IImageList2_iface);
129 static HBITMAP ImageList_CreateImage(HDC hdc, HIMAGELIST himl, UINT count);
130 static HRESULT ImageListImpl_CreateInstance(const IUnknown *pUnkOuter, REFIID iid, void** ppv);
131 static BOOL is_valid(HIMAGELIST himl);
134 * An imagelist with N images is tiled like this:
136 * N/4 ->
138 * 4 048C..
139 * 159D..
140 * | 26AE.N
141 * V 37BF.
144 #define TILE_COUNT 4
146 static inline UINT imagelist_height( UINT count )
148 return ((count + TILE_COUNT - 1)/TILE_COUNT);
151 static inline void imagelist_point_from_index( HIMAGELIST himl, UINT index, LPPOINT pt )
153 pt->x = (index%TILE_COUNT) * himl->cx;
154 pt->y = (index/TILE_COUNT) * himl->cy;
157 static inline void imagelist_get_bitmap_size( HIMAGELIST himl, UINT count, SIZE *sz )
159 sz->cx = himl->cx * TILE_COUNT;
160 sz->cy = imagelist_height( count ) * himl->cy;
163 static inline int get_dib_stride( int width, int bpp )
165 return ((width * bpp + 31) >> 3) & ~3;
168 static inline int get_dib_image_size( const BITMAPINFO *info )
170 return get_dib_stride( info->bmiHeader.biWidth, info->bmiHeader.biBitCount )
171 * abs( info->bmiHeader.biHeight );
175 * imagelist_copy_images()
177 * Copies a block of count images from offset src in the list to offset dest.
178 * Images are copied a row at at time. Assumes hdcSrc and hdcDest are different.
180 static inline void imagelist_copy_images( HIMAGELIST himl, HDC hdcSrc, HDC hdcDest,
181 UINT src, UINT count, UINT dest )
183 POINT ptSrc, ptDest;
184 SIZE sz;
185 UINT i;
187 for ( i=0; i<TILE_COUNT; i++ )
189 imagelist_point_from_index( himl, src+i, &ptSrc );
190 imagelist_point_from_index( himl, dest+i, &ptDest );
191 sz.cx = himl->cx;
192 sz.cy = himl->cy * imagelist_height( count - i );
194 BitBlt( hdcDest, ptDest.x, ptDest.y, sz.cx, sz.cy,
195 hdcSrc, ptSrc.x, ptSrc.y, SRCCOPY );
199 static void add_dib_bits( HIMAGELIST himl, int pos, int count, int width, int height,
200 BITMAPINFO *info, BITMAPINFO *mask_info, DWORD *bits, BYTE *mask_bits )
202 int i, j, n;
203 POINT pt;
204 int stride = info->bmiHeader.biWidth;
205 int mask_stride = (info->bmiHeader.biWidth + 31) / 32 * 4;
207 for (n = 0; n < count; n++)
209 BOOL has_alpha = FALSE;
211 imagelist_point_from_index( himl, pos + n, &pt );
213 /* check if bitmap has an alpha channel */
214 for (i = 0; i < height && !has_alpha; i++)
215 for (j = n * width; j < (n + 1) * width; j++)
216 if ((has_alpha = ((bits[i * stride + j] & 0xff000000) != 0))) break;
218 if (!has_alpha) /* generate alpha channel from the mask */
220 for (i = 0; i < height; i++)
221 for (j = n * width; j < (n + 1) * width; j++)
222 if (!mask_info || !((mask_bits[i * mask_stride + j / 8] << (j % 8)) & 0x80))
223 bits[i * stride + j] |= 0xff000000;
224 else
225 bits[i * stride + j] = 0;
227 else
229 himl->has_alpha[pos + n] = 1;
231 if (mask_info && himl->hbmMask) /* generate the mask from the alpha channel */
233 for (i = 0; i < height; i++)
234 for (j = n * width; j < (n + 1) * width; j++)
235 if ((bits[i * stride + j] >> 24) > 25) /* more than 10% alpha */
236 mask_bits[i * mask_stride + j / 8] &= ~(0x80 >> (j % 8));
237 else
238 mask_bits[i * mask_stride + j / 8] |= 0x80 >> (j % 8);
241 StretchDIBits( himl->hdcImage, pt.x, pt.y, himl->cx, himl->cy,
242 n * width, 0, width, height, bits, info, DIB_RGB_COLORS, SRCCOPY );
243 if (mask_info)
244 StretchDIBits( himl->hdcMask, pt.x, pt.y, himl->cx, himl->cy,
245 n * width, 0, width, height, mask_bits, mask_info, DIB_RGB_COLORS, SRCCOPY );
249 /* add images with an alpha channel when the image list is 32 bpp */
250 static BOOL add_with_alpha( HIMAGELIST himl, HDC hdc, int pos, int count,
251 int width, int height, HBITMAP hbmImage, HBITMAP hbmMask )
253 BOOL ret = FALSE;
254 BITMAP bm;
255 BITMAPINFO *info, *mask_info = NULL;
256 DWORD *bits = NULL;
257 BYTE *mask_bits = NULL;
258 DWORD mask_width;
260 if (!GetObjectW( hbmImage, sizeof(bm), &bm )) return FALSE;
262 /* if either the imagelist or the source bitmap don't have an alpha channel, bail out now */
263 if (!himl->has_alpha) return FALSE;
264 if (bm.bmBitsPixel != 32) return FALSE;
266 SelectObject( hdc, hbmImage );
267 mask_width = (bm.bmWidth + 31) / 32 * 4;
269 if (!(info = heap_alloc( FIELD_OFFSET( BITMAPINFO, bmiColors[256] )))) goto done;
270 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
271 info->bmiHeader.biWidth = bm.bmWidth;
272 info->bmiHeader.biHeight = -height;
273 info->bmiHeader.biPlanes = 1;
274 info->bmiHeader.biBitCount = 32;
275 info->bmiHeader.biCompression = BI_RGB;
276 info->bmiHeader.biSizeImage = bm.bmWidth * height * 4;
277 info->bmiHeader.biXPelsPerMeter = 0;
278 info->bmiHeader.biYPelsPerMeter = 0;
279 info->bmiHeader.biClrUsed = 0;
280 info->bmiHeader.biClrImportant = 0;
281 if (!(bits = heap_alloc( info->bmiHeader.biSizeImage ))) goto done;
282 if (!GetDIBits( hdc, hbmImage, 0, height, bits, info, DIB_RGB_COLORS )) goto done;
284 if (hbmMask)
286 if (!(mask_info = heap_alloc( FIELD_OFFSET( BITMAPINFO, bmiColors[2] ))))
287 goto done;
288 mask_info->bmiHeader = info->bmiHeader;
289 mask_info->bmiHeader.biBitCount = 1;
290 mask_info->bmiHeader.biSizeImage = mask_width * height;
291 if (!(mask_bits = heap_alloc_zero( mask_info->bmiHeader.biSizeImage )))
292 goto done;
293 if (!GetDIBits( hdc, hbmMask, 0, height, mask_bits, mask_info, DIB_RGB_COLORS )) goto done;
296 add_dib_bits( himl, pos, count, width, height, info, mask_info, bits, mask_bits );
297 ret = TRUE;
299 done:
300 heap_free( info );
301 heap_free( mask_info );
302 heap_free( bits );
303 heap_free( mask_bits );
304 return ret;
307 UINT WINAPI
308 ImageList_SetColorTable(HIMAGELIST himl, UINT uStartIndex, UINT cEntries, const RGBQUAD *prgb);
310 /*************************************************************************
311 * IMAGELIST_InternalExpandBitmaps [Internal]
313 * Expands the bitmaps of an image list by the given number of images.
315 * PARAMS
316 * himl [I] handle to image list
317 * nImageCount [I] number of images to add
319 * RETURNS
320 * nothing
322 * NOTES
323 * This function CANNOT be used to reduce the number of images.
325 static void
326 IMAGELIST_InternalExpandBitmaps(HIMAGELIST himl, INT nImageCount)
328 HDC hdcBitmap;
329 HBITMAP hbmNewBitmap, hbmNull;
330 INT nNewCount;
331 SIZE sz;
333 TRACE("%p has allocated %d, max %d, grow %d images\n", himl, himl->cCurImage, himl->cMaxImage, himl->cGrow);
335 if (himl->cCurImage + nImageCount < himl->cMaxImage)
336 return;
338 nNewCount = himl->cMaxImage + max(nImageCount, himl->cGrow) + 1;
340 imagelist_get_bitmap_size(himl, nNewCount, &sz);
342 TRACE("Create expanded bitmaps : himl=%p x=%d y=%d count=%d\n", himl, sz.cx, sz.cy, nNewCount);
343 hdcBitmap = CreateCompatibleDC (0);
345 hbmNewBitmap = ImageList_CreateImage(hdcBitmap, himl, nNewCount);
347 if (hbmNewBitmap == 0)
348 ERR("creating new image bitmap (x=%d y=%d)!\n", sz.cx, sz.cy);
350 if (himl->cCurImage)
352 hbmNull = SelectObject (hdcBitmap, hbmNewBitmap);
353 BitBlt (hdcBitmap, 0, 0, sz.cx, sz.cy,
354 himl->hdcImage, 0, 0, SRCCOPY);
355 SelectObject (hdcBitmap, hbmNull);
357 SelectObject (himl->hdcImage, hbmNewBitmap);
358 DeleteObject (himl->hbmImage);
359 himl->hbmImage = hbmNewBitmap;
361 if (himl->flags & ILC_MASK)
363 hbmNewBitmap = CreateBitmap (sz.cx, sz.cy, 1, 1, NULL);
365 if (hbmNewBitmap == 0)
366 ERR("creating new mask bitmap!\n");
368 if(himl->cCurImage)
370 hbmNull = SelectObject (hdcBitmap, hbmNewBitmap);
371 BitBlt (hdcBitmap, 0, 0, sz.cx, sz.cy,
372 himl->hdcMask, 0, 0, SRCCOPY);
373 SelectObject (hdcBitmap, hbmNull);
375 SelectObject (himl->hdcMask, hbmNewBitmap);
376 DeleteObject (himl->hbmMask);
377 himl->hbmMask = hbmNewBitmap;
380 if (himl->has_alpha)
382 char *new_alpha = HeapReAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, himl->has_alpha, nNewCount );
383 if (new_alpha) himl->has_alpha = new_alpha;
384 else
386 heap_free( himl->has_alpha );
387 himl->has_alpha = NULL;
391 himl->cMaxImage = nNewCount;
393 DeleteDC (hdcBitmap);
397 /*************************************************************************
398 * ImageList_Add [COMCTL32.@]
400 * Add an image or images to an image list.
402 * PARAMS
403 * himl [I] handle to image list
404 * hbmImage [I] handle to image bitmap
405 * hbmMask [I] handle to mask bitmap
407 * RETURNS
408 * Success: Index of the first new image.
409 * Failure: -1
412 INT WINAPI
413 ImageList_Add (HIMAGELIST himl, HBITMAP hbmImage, HBITMAP hbmMask)
415 HDC hdcBitmap, hdcTemp = 0;
416 INT nFirstIndex, nImageCount, i;
417 BITMAP bmp;
418 POINT pt;
420 TRACE("himl=%p hbmimage=%p hbmmask=%p\n", himl, hbmImage, hbmMask);
421 if (!is_valid(himl))
422 return -1;
424 if (!GetObjectW(hbmImage, sizeof(BITMAP), &bmp))
425 return -1;
427 TRACE("himl %p, cCurImage %d, cMaxImage %d, cGrow %d, cx %d, cy %d\n",
428 himl, himl->cCurImage, himl->cMaxImage, himl->cGrow, himl->cx, himl->cy);
430 nImageCount = bmp.bmWidth / himl->cx;
432 TRACE("%p has %d images (%d x %d) bpp %d\n", hbmImage, nImageCount, bmp.bmWidth, bmp.bmHeight,
433 bmp.bmBitsPixel);
435 IMAGELIST_InternalExpandBitmaps(himl, nImageCount);
437 hdcBitmap = CreateCompatibleDC(0);
439 SelectObject(hdcBitmap, hbmImage);
441 if (add_with_alpha( himl, hdcBitmap, himl->cCurImage, nImageCount,
442 himl->cx, min( himl->cy, bmp.bmHeight), hbmImage, hbmMask ))
443 goto done;
445 if (himl->hbmMask)
447 hdcTemp = CreateCompatibleDC(0);
448 SelectObject(hdcTemp, hbmMask);
451 if (himl->uBitsPixel <= 8 && bmp.bmBitsPixel <= 8 &&
452 !himl->color_table_set && himl->cCurImage == 0)
454 RGBQUAD colors[256];
455 UINT num = GetDIBColorTable( hdcBitmap, 0, 1 << bmp.bmBitsPixel, colors );
456 if (num) ImageList_SetColorTable( himl, 0, num, colors );
459 for (i=0; i<nImageCount; i++)
461 imagelist_point_from_index( himl, himl->cCurImage + i, &pt );
463 /* Copy result to the imagelist
465 BitBlt( himl->hdcImage, pt.x, pt.y, himl->cx, bmp.bmHeight,
466 hdcBitmap, i*himl->cx, 0, SRCCOPY );
468 if (!himl->hbmMask)
469 continue;
471 BitBlt( himl->hdcMask, pt.x, pt.y, himl->cx, bmp.bmHeight,
472 hdcTemp, i*himl->cx, 0, SRCCOPY );
474 /* Remove the background from the image
476 BitBlt( himl->hdcImage, pt.x, pt.y, himl->cx, bmp.bmHeight,
477 himl->hdcMask, pt.x, pt.y, 0x220326 ); /* NOTSRCAND */
479 if (hdcTemp) DeleteDC(hdcTemp);
481 done:
482 DeleteDC(hdcBitmap);
484 nFirstIndex = himl->cCurImage;
485 himl->cCurImage += nImageCount;
487 return nFirstIndex;
491 /*************************************************************************
492 * ImageList_AddIcon [COMCTL32.@]
494 * Adds an icon to an image list.
496 * PARAMS
497 * himl [I] handle to image list
498 * hIcon [I] handle to icon
500 * RETURNS
501 * Success: index of the new image
502 * Failure: -1
504 #undef ImageList_AddIcon
505 INT WINAPI ImageList_AddIcon (HIMAGELIST himl, HICON hIcon)
507 return ImageList_ReplaceIcon (himl, -1, hIcon);
511 /*************************************************************************
512 * ImageList_AddMasked [COMCTL32.@]
514 * Adds an image or images to an image list and creates a mask from the
515 * specified bitmap using the mask color.
517 * PARAMS
518 * himl [I] handle to image list.
519 * hBitmap [I] handle to bitmap
520 * clrMask [I] mask color.
522 * RETURNS
523 * Success: Index of the first new image.
524 * Failure: -1
527 INT WINAPI
528 ImageList_AddMasked (HIMAGELIST himl, HBITMAP hBitmap, COLORREF clrMask)
530 HDC hdcMask, hdcBitmap;
531 INT ret;
532 BITMAP bmp;
533 HBITMAP hMaskBitmap;
534 COLORREF bkColor;
536 TRACE("himl=%p hbitmap=%p clrmask=%x\n", himl, hBitmap, clrMask);
537 if (!is_valid(himl))
538 return -1;
540 if (!GetObjectW(hBitmap, sizeof(BITMAP), &bmp))
541 return -1;
543 hdcBitmap = CreateCompatibleDC(0);
544 SelectObject(hdcBitmap, hBitmap);
546 /* Create a temp Mask so we can remove the background of the Image */
547 hdcMask = CreateCompatibleDC(0);
548 hMaskBitmap = CreateBitmap(bmp.bmWidth, bmp.bmHeight, 1, 1, NULL);
549 SelectObject(hdcMask, hMaskBitmap);
551 /* create monochrome image to the mask bitmap */
552 bkColor = (clrMask != CLR_DEFAULT) ? clrMask : GetPixel (hdcBitmap, 0, 0);
553 SetBkColor (hdcBitmap, bkColor);
554 BitBlt (hdcMask, 0, 0, bmp.bmWidth, bmp.bmHeight, hdcBitmap, 0, 0, SRCCOPY);
557 * Remove the background from the image
559 * WINDOWS BUG ALERT!!!!!!
560 * The statement below should not be done in common practice
561 * but this is how ImageList_AddMasked works in Windows.
562 * It overwrites the original bitmap passed, this was discovered
563 * by using the same bitmap to iterate the different styles
564 * on windows where it failed (BUT ImageList_Add is OK)
565 * This is here in case some apps rely on this bug
567 * Blt mode 0x220326 is NOTSRCAND
569 if (bmp.bmBitsPixel > 8) /* NOTSRCAND can't work with palettes */
571 SetBkColor(hdcBitmap, RGB(255,255,255));
572 BitBlt(hdcBitmap, 0, 0, bmp.bmWidth, bmp.bmHeight, hdcMask, 0, 0, 0x220326);
575 DeleteDC(hdcBitmap);
576 DeleteDC(hdcMask);
578 ret = ImageList_Add( himl, hBitmap, hMaskBitmap );
580 DeleteObject(hMaskBitmap);
581 return ret;
585 /*************************************************************************
586 * ImageList_BeginDrag [COMCTL32.@]
588 * Creates a temporary image list that contains one image. It will be used
589 * as a drag image.
591 * PARAMS
592 * himlTrack [I] handle to the source image list
593 * iTrack [I] index of the drag image in the source image list
594 * dxHotspot [I] X position of the hot spot of the drag image
595 * dyHotspot [I] Y position of the hot spot of the drag image
597 * RETURNS
598 * Success: TRUE
599 * Failure: FALSE
602 BOOL WINAPI
603 ImageList_BeginDrag (HIMAGELIST himlTrack, INT iTrack,
604 INT dxHotspot, INT dyHotspot)
606 INT cx, cy;
607 POINT src, dst;
609 TRACE("(himlTrack=%p iTrack=%d dx=%d dy=%d)\n", himlTrack, iTrack,
610 dxHotspot, dyHotspot);
612 if (!is_valid(himlTrack))
613 return FALSE;
615 if (iTrack >= himlTrack->cCurImage)
616 return FALSE;
618 if (InternalDrag.himl)
619 return FALSE;
621 cx = himlTrack->cx;
622 cy = himlTrack->cy;
624 InternalDrag.himlNoCursor = InternalDrag.himl = ImageList_Create (cx, cy, himlTrack->flags, 1, 1);
625 if (InternalDrag.himl == NULL) {
626 WARN("Error creating drag image list!\n");
627 return FALSE;
630 InternalDrag.dxHotspot = dxHotspot;
631 InternalDrag.dyHotspot = dyHotspot;
633 /* copy image */
634 imagelist_point_from_index(InternalDrag.himl, 0, &dst);
635 imagelist_point_from_index(himlTrack, iTrack, &src);
636 BitBlt(InternalDrag.himl->hdcImage, dst.x, dst.y, cx, cy, himlTrack->hdcImage, src.x, src.y,
637 SRCCOPY);
638 BitBlt(InternalDrag.himl->hdcMask, dst.x, dst.y, cx, cy, himlTrack->hdcMask, src.x, src.y,
639 SRCCOPY);
641 InternalDrag.himl->cCurImage = 1;
643 return TRUE;
647 /*************************************************************************
648 * ImageList_Copy [COMCTL32.@]
650 * Copies an image of the source image list to an image of the
651 * destination image list. Images can be copied or swapped.
653 * PARAMS
654 * himlDst [I] handle to the destination image list
655 * iDst [I] destination image index.
656 * himlSrc [I] handle to the source image list
657 * iSrc [I] source image index
658 * uFlags [I] flags for the copy operation
660 * RETURNS
661 * Success: TRUE
662 * Failure: FALSE
664 * NOTES
665 * Copying from one image list to another is possible. The original
666 * implementation just copies or swaps within one image list.
667 * Could this feature become a bug??? ;-)
670 BOOL WINAPI
671 ImageList_Copy (HIMAGELIST himlDst, INT iDst, HIMAGELIST himlSrc,
672 INT iSrc, UINT uFlags)
674 POINT ptSrc, ptDst;
676 TRACE("himlDst=%p iDst=%d himlSrc=%p iSrc=%d\n", himlDst, iDst, himlSrc, iSrc);
678 if (!is_valid(himlSrc) || !is_valid(himlDst))
679 return FALSE;
680 if ((iDst < 0) || (iDst >= himlDst->cCurImage))
681 return FALSE;
682 if ((iSrc < 0) || (iSrc >= himlSrc->cCurImage))
683 return FALSE;
685 imagelist_point_from_index( himlDst, iDst, &ptDst );
686 imagelist_point_from_index( himlSrc, iSrc, &ptSrc );
688 if (uFlags & ILCF_SWAP) {
689 /* swap */
690 HDC hdcBmp;
691 HBITMAP hbmTempImage, hbmTempMask;
693 hdcBmp = CreateCompatibleDC (0);
695 /* create temporary bitmaps */
696 hbmTempImage = CreateBitmap (himlSrc->cx, himlSrc->cy, 1,
697 himlSrc->uBitsPixel, NULL);
698 hbmTempMask = CreateBitmap (himlSrc->cx, himlSrc->cy, 1,
699 1, NULL);
701 /* copy (and stretch) destination to temporary bitmaps.(save) */
702 /* image */
703 SelectObject (hdcBmp, hbmTempImage);
704 StretchBlt (hdcBmp, 0, 0, himlSrc->cx, himlSrc->cy,
705 himlDst->hdcImage, ptDst.x, ptDst.y, himlDst->cx, himlDst->cy,
706 SRCCOPY);
707 /* mask */
708 SelectObject (hdcBmp, hbmTempMask);
709 StretchBlt (hdcBmp, 0, 0, himlSrc->cx, himlSrc->cy,
710 himlDst->hdcMask, ptDst.x, ptDst.y, himlDst->cx, himlDst->cy,
711 SRCCOPY);
713 /* copy (and stretch) source to destination */
714 /* image */
715 StretchBlt (himlDst->hdcImage, ptDst.x, ptDst.y, himlDst->cx, himlDst->cy,
716 himlSrc->hdcImage, ptSrc.x, ptSrc.y, himlSrc->cx, himlSrc->cy,
717 SRCCOPY);
718 /* mask */
719 StretchBlt (himlDst->hdcMask, ptDst.x, ptDst.y, himlDst->cx, himlDst->cy,
720 himlSrc->hdcMask, ptSrc.x, ptSrc.y, himlSrc->cx, himlSrc->cy,
721 SRCCOPY);
723 /* copy (without stretching) temporary bitmaps to source (restore) */
724 /* mask */
725 BitBlt (himlSrc->hdcMask, ptSrc.x, ptSrc.y, himlSrc->cx, himlSrc->cy,
726 hdcBmp, 0, 0, SRCCOPY);
728 /* image */
729 BitBlt (himlSrc->hdcImage, ptSrc.x, ptSrc.y, himlSrc->cx, himlSrc->cy,
730 hdcBmp, 0, 0, SRCCOPY);
731 /* delete temporary bitmaps */
732 DeleteObject (hbmTempMask);
733 DeleteObject (hbmTempImage);
734 DeleteDC(hdcBmp);
736 else {
737 /* copy image */
738 StretchBlt (himlDst->hdcImage, ptDst.x, ptDst.y, himlDst->cx, himlDst->cy,
739 himlSrc->hdcImage, ptSrc.x, ptSrc.y, himlSrc->cx, himlSrc->cy,
740 SRCCOPY);
742 /* copy mask */
743 StretchBlt (himlDst->hdcMask, ptDst.x, ptDst.y, himlDst->cx, himlDst->cy,
744 himlSrc->hdcMask, ptSrc.x, ptSrc.y, himlSrc->cx, himlSrc->cy,
745 SRCCOPY);
748 return TRUE;
752 /*************************************************************************
753 * ImageList_Create [COMCTL32.@]
755 * Creates a new image list.
757 * PARAMS
758 * cx [I] image height
759 * cy [I] image width
760 * flags [I] creation flags
761 * cInitial [I] initial number of images in the image list
762 * cGrow [I] number of images by which image list grows
764 * RETURNS
765 * Success: Handle to the created image list
766 * Failure: NULL
768 HIMAGELIST WINAPI
769 ImageList_Create (INT cx, INT cy, UINT flags,
770 INT cInitial, INT cGrow)
772 HIMAGELIST himl;
773 INT nCount;
774 HBITMAP hbmTemp;
775 UINT ilc = (flags & 0xFE);
776 static const WORD aBitBlend25[] =
777 {0xAA, 0x00, 0x55, 0x00, 0xAA, 0x00, 0x55, 0x00};
779 static const WORD aBitBlend50[] =
780 {0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA};
782 TRACE("(%d %d 0x%x %d %d)\n", cx, cy, flags, cInitial, cGrow);
784 if (cx < 0 || cy < 0) return NULL;
785 if (!((flags&ILC_COLORDDB) == ILC_COLORDDB) && (cx == 0 || cy == 0)) return NULL;
787 /* Create the IImageList interface for the image list */
788 if (FAILED(ImageListImpl_CreateInstance(NULL, &IID_IImageList, (void **)&himl)))
789 return NULL;
791 cGrow = (WORD)((max( cGrow, 1 ) + 3) & ~3);
793 if (cGrow > 256)
795 /* Windows doesn't limit the size here, but X11 doesn't let us allocate such huge bitmaps */
796 WARN( "grow %d too large, limiting to 256\n", cGrow );
797 cGrow = 256;
800 himl->cx = cx;
801 himl->cy = cy;
802 himl->flags = flags;
803 himl->cMaxImage = cInitial + 1;
804 himl->cInitial = cInitial;
805 himl->cGrow = cGrow;
806 himl->clrFg = CLR_DEFAULT;
807 himl->clrBk = CLR_NONE;
808 himl->color_table_set = FALSE;
810 /* initialize overlay mask indices */
811 for (nCount = 0; nCount < MAX_OVERLAYIMAGE; nCount++)
812 himl->nOvlIdx[nCount] = -1;
814 /* Create Image & Mask DCs */
815 himl->hdcImage = CreateCompatibleDC (0);
816 if (!himl->hdcImage)
817 goto cleanup;
818 if (himl->flags & ILC_MASK){
819 himl->hdcMask = CreateCompatibleDC(0);
820 if (!himl->hdcMask)
821 goto cleanup;
824 /* Default to ILC_COLOR4 if none of the ILC_COLOR* flags are specified */
825 if (ilc == ILC_COLOR)
827 ilc = ILC_COLOR4;
828 himl->flags |= ILC_COLOR4;
831 if (ilc >= ILC_COLOR4 && ilc <= ILC_COLOR32)
832 himl->uBitsPixel = ilc;
833 else
834 himl->uBitsPixel = (UINT)GetDeviceCaps (himl->hdcImage, BITSPIXEL);
836 if (himl->cMaxImage > 0) {
837 himl->hbmImage = ImageList_CreateImage(himl->hdcImage, himl, himl->cMaxImage);
838 SelectObject(himl->hdcImage, himl->hbmImage);
839 } else
840 himl->hbmImage = 0;
842 if ((himl->cMaxImage > 0) && (himl->flags & ILC_MASK)) {
843 SIZE sz;
845 imagelist_get_bitmap_size(himl, himl->cMaxImage, &sz);
846 himl->hbmMask = CreateBitmap (sz.cx, sz.cy, 1, 1, NULL);
847 if (himl->hbmMask == 0) {
848 ERR("Error creating mask bitmap!\n");
849 goto cleanup;
851 SelectObject(himl->hdcMask, himl->hbmMask);
853 else
854 himl->hbmMask = 0;
856 if (ilc == ILC_COLOR32)
857 himl->has_alpha = heap_alloc_zero( himl->cMaxImage );
858 else
859 himl->has_alpha = NULL;
861 /* create blending brushes */
862 hbmTemp = CreateBitmap (8, 8, 1, 1, aBitBlend25);
863 himl->hbrBlend25 = CreatePatternBrush (hbmTemp);
864 DeleteObject (hbmTemp);
866 hbmTemp = CreateBitmap (8, 8, 1, 1, aBitBlend50);
867 himl->hbrBlend50 = CreatePatternBrush (hbmTemp);
868 DeleteObject (hbmTemp);
870 TRACE("created imagelist %p\n", himl);
871 return himl;
873 cleanup:
874 ImageList_Destroy(himl);
875 return NULL;
879 /*************************************************************************
880 * ImageList_Destroy [COMCTL32.@]
882 * Destroys an image list.
884 * PARAMS
885 * himl [I] handle to image list
887 * RETURNS
888 * Success: TRUE
889 * Failure: FALSE
892 BOOL WINAPI
893 ImageList_Destroy (HIMAGELIST himl)
895 if (!is_valid(himl))
896 return FALSE;
898 IImageList_Release((IImageList *) himl);
899 return TRUE;
903 /*************************************************************************
904 * ImageList_DragEnter [COMCTL32.@]
906 * Locks window update and displays the drag image at the given position.
908 * PARAMS
909 * hwndLock [I] handle of the window that owns the drag image.
910 * x [I] X position of the drag image.
911 * y [I] Y position of the drag image.
913 * RETURNS
914 * Success: TRUE
915 * Failure: FALSE
917 * NOTES
918 * The position of the drag image is relative to the window, not
919 * the client area.
922 BOOL WINAPI
923 ImageList_DragEnter (HWND hwndLock, INT x, INT y)
925 TRACE("(hwnd=%p x=%d y=%d)\n", hwndLock, x, y);
927 if (!is_valid(InternalDrag.himl))
928 return FALSE;
930 if (hwndLock)
931 InternalDrag.hwnd = hwndLock;
932 else
933 InternalDrag.hwnd = GetDesktopWindow ();
935 InternalDrag.x = x;
936 InternalDrag.y = y;
938 /* draw the drag image and save the background */
939 return ImageList_DragShowNolock(TRUE);
943 /*************************************************************************
944 * ImageList_DragLeave [COMCTL32.@]
946 * Unlocks window update and hides the drag image.
948 * PARAMS
949 * hwndLock [I] handle of the window that owns the drag image.
951 * RETURNS
952 * Success: TRUE
953 * Failure: FALSE
956 BOOL WINAPI
957 ImageList_DragLeave (HWND hwndLock)
959 /* As we don't save drag info in the window this can lead to problems if
960 an app does not supply the same window as DragEnter */
961 /* if (hwndLock)
962 InternalDrag.hwnd = hwndLock;
963 else
964 InternalDrag.hwnd = GetDesktopWindow (); */
965 if(!hwndLock)
966 hwndLock = GetDesktopWindow();
967 if(InternalDrag.hwnd != hwndLock)
968 FIXME("DragLeave hWnd != DragEnter hWnd\n");
970 ImageList_DragShowNolock (FALSE);
972 return TRUE;
976 /*************************************************************************
977 * ImageList_InternalDragDraw [Internal]
979 * Draws the drag image.
981 * PARAMS
982 * hdc [I] device context to draw into.
983 * x [I] X position of the drag image.
984 * y [I] Y position of the drag image.
986 * RETURNS
987 * Success: TRUE
988 * Failure: FALSE
990 * NOTES
991 * The position of the drag image is relative to the window, not
992 * the client area.
996 static inline void
997 ImageList_InternalDragDraw (HDC hdc, INT x, INT y)
999 IMAGELISTDRAWPARAMS imldp;
1001 ZeroMemory (&imldp, sizeof(imldp));
1002 imldp.cbSize = sizeof(imldp);
1003 imldp.himl = InternalDrag.himl;
1004 imldp.i = 0;
1005 imldp.hdcDst = hdc;
1006 imldp.x = x;
1007 imldp.y = y;
1008 imldp.rgbBk = CLR_DEFAULT;
1009 imldp.rgbFg = CLR_DEFAULT;
1010 imldp.fStyle = ILD_NORMAL;
1011 imldp.fState = ILS_ALPHA;
1012 imldp.Frame = 192;
1013 ImageList_DrawIndirect (&imldp);
1016 /*************************************************************************
1017 * ImageList_DragMove [COMCTL32.@]
1019 * Moves the drag image.
1021 * PARAMS
1022 * x [I] X position of the drag image.
1023 * y [I] Y position of the drag image.
1025 * RETURNS
1026 * Success: TRUE
1027 * Failure: FALSE
1029 * NOTES
1030 * The position of the drag image is relative to the window, not
1031 * the client area.
1034 BOOL WINAPI
1035 ImageList_DragMove (INT x, INT y)
1037 TRACE("(x=%d y=%d)\n", x, y);
1039 if (!is_valid(InternalDrag.himl))
1040 return FALSE;
1042 /* draw/update the drag image */
1043 if (InternalDrag.bShow) {
1044 HDC hdcDrag;
1045 HDC hdcOffScreen;
1046 HDC hdcBg;
1047 HBITMAP hbmOffScreen;
1048 INT origNewX, origNewY;
1049 INT origOldX, origOldY;
1050 INT origRegX, origRegY;
1051 INT sizeRegX, sizeRegY;
1054 /* calculate the update region */
1055 origNewX = x - InternalDrag.dxHotspot;
1056 origNewY = y - InternalDrag.dyHotspot;
1057 origOldX = InternalDrag.x - InternalDrag.dxHotspot;
1058 origOldY = InternalDrag.y - InternalDrag.dyHotspot;
1059 origRegX = min(origNewX, origOldX);
1060 origRegY = min(origNewY, origOldY);
1061 sizeRegX = InternalDrag.himl->cx + abs(x - InternalDrag.x);
1062 sizeRegY = InternalDrag.himl->cy + abs(y - InternalDrag.y);
1064 hdcDrag = GetDCEx(InternalDrag.hwnd, 0,
1065 DCX_WINDOW | DCX_CACHE | DCX_LOCKWINDOWUPDATE);
1066 hdcOffScreen = CreateCompatibleDC(hdcDrag);
1067 hdcBg = CreateCompatibleDC(hdcDrag);
1069 hbmOffScreen = CreateCompatibleBitmap(hdcDrag, sizeRegX, sizeRegY);
1070 SelectObject(hdcOffScreen, hbmOffScreen);
1071 SelectObject(hdcBg, InternalDrag.hbmBg);
1073 /* get the actual background of the update region */
1074 BitBlt(hdcOffScreen, 0, 0, sizeRegX, sizeRegY, hdcDrag,
1075 origRegX, origRegY, SRCCOPY);
1076 /* erase the old image */
1077 BitBlt(hdcOffScreen, origOldX - origRegX, origOldY - origRegY,
1078 InternalDrag.himl->cx, InternalDrag.himl->cy, hdcBg, 0, 0,
1079 SRCCOPY);
1080 /* save the background */
1081 BitBlt(hdcBg, 0, 0, InternalDrag.himl->cx, InternalDrag.himl->cy,
1082 hdcOffScreen, origNewX - origRegX, origNewY - origRegY, SRCCOPY);
1083 /* draw the image */
1084 ImageList_InternalDragDraw(hdcOffScreen, origNewX - origRegX,
1085 origNewY - origRegY);
1086 /* draw the update region to the screen */
1087 BitBlt(hdcDrag, origRegX, origRegY, sizeRegX, sizeRegY,
1088 hdcOffScreen, 0, 0, SRCCOPY);
1090 DeleteDC(hdcBg);
1091 DeleteDC(hdcOffScreen);
1092 DeleteObject(hbmOffScreen);
1093 ReleaseDC(InternalDrag.hwnd, hdcDrag);
1096 /* update the image position */
1097 InternalDrag.x = x;
1098 InternalDrag.y = y;
1100 return TRUE;
1104 /*************************************************************************
1105 * ImageList_DragShowNolock [COMCTL32.@]
1107 * Shows or hides the drag image.
1109 * PARAMS
1110 * bShow [I] TRUE shows the drag image, FALSE hides it.
1112 * RETURNS
1113 * Success: TRUE
1114 * Failure: FALSE
1117 BOOL WINAPI
1118 ImageList_DragShowNolock (BOOL bShow)
1120 HDC hdcDrag;
1121 HDC hdcBg;
1122 INT x, y;
1124 if (!is_valid(InternalDrag.himl))
1125 return FALSE;
1127 TRACE("bShow=0x%X!\n", bShow);
1129 /* DragImage is already visible/hidden */
1130 if ((InternalDrag.bShow && bShow) || (!InternalDrag.bShow && !bShow)) {
1131 return FALSE;
1134 /* position of the origin of the DragImage */
1135 x = InternalDrag.x - InternalDrag.dxHotspot;
1136 y = InternalDrag.y - InternalDrag.dyHotspot;
1138 hdcDrag = GetDCEx (InternalDrag.hwnd, 0,
1139 DCX_WINDOW | DCX_CACHE | DCX_LOCKWINDOWUPDATE);
1140 if (!hdcDrag) {
1141 return FALSE;
1144 hdcBg = CreateCompatibleDC(hdcDrag);
1145 if (!InternalDrag.hbmBg) {
1146 InternalDrag.hbmBg = CreateCompatibleBitmap(hdcDrag,
1147 InternalDrag.himl->cx, InternalDrag.himl->cy);
1149 SelectObject(hdcBg, InternalDrag.hbmBg);
1151 if (bShow) {
1152 /* save the background */
1153 BitBlt(hdcBg, 0, 0, InternalDrag.himl->cx, InternalDrag.himl->cy,
1154 hdcDrag, x, y, SRCCOPY);
1155 /* show the image */
1156 ImageList_InternalDragDraw(hdcDrag, x, y);
1157 } else {
1158 /* hide the image */
1159 BitBlt(hdcDrag, x, y, InternalDrag.himl->cx, InternalDrag.himl->cy,
1160 hdcBg, 0, 0, SRCCOPY);
1163 InternalDrag.bShow = !InternalDrag.bShow;
1165 DeleteDC(hdcBg);
1166 ReleaseDC (InternalDrag.hwnd, hdcDrag);
1167 return TRUE;
1171 /*************************************************************************
1172 * ImageList_Draw [COMCTL32.@]
1174 * Draws an image.
1176 * PARAMS
1177 * himl [I] handle to image list
1178 * i [I] image index
1179 * hdc [I] handle to device context
1180 * x [I] x position
1181 * y [I] y position
1182 * fStyle [I] drawing flags
1184 * RETURNS
1185 * Success: TRUE
1186 * Failure: FALSE
1188 * SEE
1189 * ImageList_DrawEx.
1192 BOOL WINAPI
1193 ImageList_Draw (HIMAGELIST himl, INT i, HDC hdc, INT x, INT y, UINT fStyle)
1195 return ImageList_DrawEx (himl, i, hdc, x, y, 0, 0,
1196 CLR_DEFAULT, CLR_DEFAULT, fStyle);
1200 /*************************************************************************
1201 * ImageList_DrawEx [COMCTL32.@]
1203 * Draws an image and allows using extended drawing features.
1205 * PARAMS
1206 * himl [I] handle to image list
1207 * i [I] image index
1208 * hdc [I] handle to device context
1209 * x [I] X position
1210 * y [I] Y position
1211 * dx [I] X offset
1212 * dy [I] Y offset
1213 * rgbBk [I] background color
1214 * rgbFg [I] foreground color
1215 * fStyle [I] drawing flags
1217 * RETURNS
1218 * Success: TRUE
1219 * Failure: FALSE
1221 * NOTES
1222 * Calls ImageList_DrawIndirect.
1224 * SEE
1225 * ImageList_DrawIndirect.
1228 BOOL WINAPI
1229 ImageList_DrawEx (HIMAGELIST himl, INT i, HDC hdc, INT x, INT y,
1230 INT dx, INT dy, COLORREF rgbBk, COLORREF rgbFg,
1231 UINT fStyle)
1233 IMAGELISTDRAWPARAMS imldp;
1235 ZeroMemory (&imldp, sizeof(imldp));
1236 imldp.cbSize = sizeof(imldp);
1237 imldp.himl = himl;
1238 imldp.i = i;
1239 imldp.hdcDst = hdc;
1240 imldp.x = x;
1241 imldp.y = y;
1242 imldp.cx = dx;
1243 imldp.cy = dy;
1244 imldp.rgbBk = rgbBk;
1245 imldp.rgbFg = rgbFg;
1246 imldp.fStyle = fStyle;
1248 return ImageList_DrawIndirect (&imldp);
1252 static BOOL alpha_blend_image( HIMAGELIST himl, HDC dest_dc, int dest_x, int dest_y,
1253 int src_x, int src_y, int cx, int cy, BLENDFUNCTION func,
1254 UINT style, COLORREF blend_col )
1256 BOOL ret = FALSE;
1257 HDC hdc;
1258 HBITMAP bmp = 0, mask = 0;
1259 BITMAPINFO *info;
1260 void *bits, *mask_bits;
1261 unsigned int *ptr;
1262 int i, j;
1264 if (!(hdc = CreateCompatibleDC( 0 ))) return FALSE;
1265 if (!(info = heap_alloc( FIELD_OFFSET( BITMAPINFO, bmiColors[256] )))) goto done;
1266 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1267 info->bmiHeader.biWidth = cx;
1268 info->bmiHeader.biHeight = cy;
1269 info->bmiHeader.biPlanes = 1;
1270 info->bmiHeader.biBitCount = 32;
1271 info->bmiHeader.biCompression = BI_RGB;
1272 info->bmiHeader.biSizeImage = cx * cy * 4;
1273 info->bmiHeader.biXPelsPerMeter = 0;
1274 info->bmiHeader.biYPelsPerMeter = 0;
1275 info->bmiHeader.biClrUsed = 0;
1276 info->bmiHeader.biClrImportant = 0;
1277 if (!(bmp = CreateDIBSection( himl->hdcImage, info, DIB_RGB_COLORS, &bits, 0, 0 ))) goto done;
1278 SelectObject( hdc, bmp );
1279 BitBlt( hdc, 0, 0, cx, cy, himl->hdcImage, src_x, src_y, SRCCOPY );
1281 if (blend_col != CLR_NONE)
1283 BYTE r = GetRValue( blend_col );
1284 BYTE g = GetGValue( blend_col );
1285 BYTE b = GetBValue( blend_col );
1287 if (style & ILD_BLEND25)
1289 for (i = 0, ptr = bits; i < cx * cy; i++, ptr++)
1290 *ptr = ((*ptr & 0xff000000) |
1291 ((((*ptr & 0x00ff0000) * 3 + (r << 16)) / 4) & 0x00ff0000) |
1292 ((((*ptr & 0x0000ff00) * 3 + (g << 8)) / 4) & 0x0000ff00) |
1293 ((((*ptr & 0x000000ff) * 3 + (b << 0)) / 4) & 0x000000ff));
1295 else if (style & ILD_BLEND50)
1297 for (i = 0, ptr = bits; i < cx * cy; i++, ptr++)
1298 *ptr = ((*ptr & 0xff000000) |
1299 ((((*ptr & 0x00ff0000) + (r << 16)) / 2) & 0x00ff0000) |
1300 ((((*ptr & 0x0000ff00) + (g << 8)) / 2) & 0x0000ff00) |
1301 ((((*ptr & 0x000000ff) + (b << 0)) / 2) & 0x000000ff));
1305 if (himl->has_alpha) /* we already have an alpha channel in this case */
1307 /* pre-multiply by the alpha channel */
1308 for (i = 0, ptr = bits; i < cx * cy; i++, ptr++)
1310 DWORD alpha = *ptr >> 24;
1311 *ptr = ((*ptr & 0xff000000) |
1312 (((*ptr & 0x00ff0000) * alpha / 255) & 0x00ff0000) |
1313 (((*ptr & 0x0000ff00) * alpha / 255) & 0x0000ff00) |
1314 (((*ptr & 0x000000ff) * alpha / 255)));
1317 else if (himl->hbmMask)
1319 unsigned int width_bytes = (cx + 31) / 32 * 4;
1320 /* generate alpha channel from the mask */
1321 info->bmiHeader.biBitCount = 1;
1322 info->bmiHeader.biSizeImage = width_bytes * cy;
1323 info->bmiColors[0].rgbRed = 0;
1324 info->bmiColors[0].rgbGreen = 0;
1325 info->bmiColors[0].rgbBlue = 0;
1326 info->bmiColors[0].rgbReserved = 0;
1327 info->bmiColors[1].rgbRed = 0xff;
1328 info->bmiColors[1].rgbGreen = 0xff;
1329 info->bmiColors[1].rgbBlue = 0xff;
1330 info->bmiColors[1].rgbReserved = 0;
1331 if (!(mask = CreateDIBSection( himl->hdcMask, info, DIB_RGB_COLORS, &mask_bits, 0, 0 )))
1332 goto done;
1333 SelectObject( hdc, mask );
1334 BitBlt( hdc, 0, 0, cx, cy, himl->hdcMask, src_x, src_y, SRCCOPY );
1335 SelectObject( hdc, bmp );
1336 for (i = 0, ptr = bits; i < cy; i++)
1337 for (j = 0; j < cx; j++, ptr++)
1338 if ((((BYTE *)mask_bits)[i * width_bytes + j / 8] << (j % 8)) & 0x80) *ptr = 0;
1339 else *ptr |= 0xff000000;
1342 ret = GdiAlphaBlend( dest_dc, dest_x, dest_y, cx, cy, hdc, 0, 0, cx, cy, func );
1344 done:
1345 DeleteDC( hdc );
1346 if (bmp) DeleteObject( bmp );
1347 if (mask) DeleteObject( mask );
1348 heap_free( info );
1349 return ret;
1352 /*************************************************************************
1353 * ImageList_DrawIndirect [COMCTL32.@]
1355 * Draws an image using various parameters specified in pimldp.
1357 * PARAMS
1358 * pimldp [I] pointer to IMAGELISTDRAWPARAMS structure.
1360 * RETURNS
1361 * Success: TRUE
1362 * Failure: FALSE
1365 BOOL WINAPI
1366 ImageList_DrawIndirect (IMAGELISTDRAWPARAMS *pimldp)
1368 INT cx, cy, nOvlIdx;
1369 DWORD fState, dwRop;
1370 UINT fStyle;
1371 COLORREF oldImageBk, oldImageFg;
1372 HDC hImageDC, hImageListDC, hMaskListDC;
1373 HBITMAP hImageBmp, hOldImageBmp, hBlendMaskBmp;
1374 BOOL bIsTransparent, bBlend, bResult = FALSE, bMask;
1375 HIMAGELIST himl;
1376 HBRUSH hOldBrush;
1377 POINT pt;
1378 BOOL has_alpha;
1380 if (!pimldp || !(himl = pimldp->himl)) return FALSE;
1381 if (!is_valid(himl)) return FALSE;
1382 if ((pimldp->i < 0) || (pimldp->i >= himl->cCurImage)) return FALSE;
1384 imagelist_point_from_index( himl, pimldp->i, &pt );
1385 pt.x += pimldp->xBitmap;
1386 pt.y += pimldp->yBitmap;
1388 fState = pimldp->cbSize < sizeof(IMAGELISTDRAWPARAMS) ? ILS_NORMAL : pimldp->fState;
1389 fStyle = pimldp->fStyle & ~ILD_OVERLAYMASK;
1390 cx = (pimldp->cx == 0) ? himl->cx : pimldp->cx;
1391 cy = (pimldp->cy == 0) ? himl->cy : pimldp->cy;
1393 bIsTransparent = (fStyle & ILD_TRANSPARENT);
1394 if( pimldp->rgbBk == CLR_NONE )
1395 bIsTransparent = TRUE;
1396 if( ( pimldp->rgbBk == CLR_DEFAULT ) && ( himl->clrBk == CLR_NONE ) )
1397 bIsTransparent = TRUE;
1398 bMask = (himl->flags & ILC_MASK) && (fStyle & ILD_MASK) ;
1399 bBlend = (fStyle & (ILD_BLEND25 | ILD_BLEND50) ) && !bMask;
1401 TRACE("himl(%p) hbmMask(%p) iImage(%d) x(%d) y(%d) cx(%d) cy(%d)\n",
1402 himl, himl->hbmMask, pimldp->i, pimldp->x, pimldp->y, cx, cy);
1404 /* we will use these DCs to access the images and masks in the ImageList */
1405 hImageListDC = himl->hdcImage;
1406 hMaskListDC = himl->hdcMask;
1408 /* these will accumulate the image and mask for the image we're drawing */
1409 hImageDC = CreateCompatibleDC( pimldp->hdcDst );
1410 hImageBmp = CreateCompatibleBitmap( pimldp->hdcDst, cx, cy );
1411 hBlendMaskBmp = bBlend ? CreateBitmap(cx, cy, 1, 1, NULL) : 0;
1413 /* Create a compatible DC. */
1414 if (!hImageListDC || !hImageDC || !hImageBmp ||
1415 (bBlend && !hBlendMaskBmp) || (himl->hbmMask && !hMaskListDC))
1416 goto cleanup;
1418 hOldImageBmp = SelectObject(hImageDC, hImageBmp);
1421 * To obtain a transparent look, background color should be set
1422 * to white and foreground color to black when blitting the
1423 * monochrome mask.
1425 oldImageFg = SetTextColor( hImageDC, RGB( 0, 0, 0 ) );
1426 oldImageBk = SetBkColor( hImageDC, RGB( 0xff, 0xff, 0xff ) );
1428 has_alpha = (himl->has_alpha && himl->has_alpha[pimldp->i]);
1429 if (!bMask && (has_alpha || (fState & ILS_ALPHA)))
1431 COLORREF colour, blend_col = CLR_NONE;
1432 BLENDFUNCTION func;
1434 if (bBlend)
1436 blend_col = pimldp->rgbFg;
1437 if (blend_col == CLR_DEFAULT) blend_col = GetSysColor( COLOR_HIGHLIGHT );
1438 else if (blend_col == CLR_NONE) blend_col = GetTextColor( pimldp->hdcDst );
1441 func.BlendOp = AC_SRC_OVER;
1442 func.BlendFlags = 0;
1443 func.SourceConstantAlpha = (fState & ILS_ALPHA) ? pimldp->Frame : 255;
1444 func.AlphaFormat = AC_SRC_ALPHA;
1446 if (bIsTransparent)
1448 bResult = alpha_blend_image( himl, pimldp->hdcDst, pimldp->x, pimldp->y,
1449 pt.x, pt.y, cx, cy, func, fStyle, blend_col );
1450 goto end;
1452 colour = pimldp->rgbBk;
1453 if (colour == CLR_DEFAULT) colour = himl->clrBk;
1454 if (colour == CLR_NONE) colour = GetBkColor( pimldp->hdcDst );
1456 hOldBrush = SelectObject (hImageDC, CreateSolidBrush (colour));
1457 PatBlt( hImageDC, 0, 0, cx, cy, PATCOPY );
1458 alpha_blend_image( himl, hImageDC, 0, 0, pt.x, pt.y, cx, cy, func, fStyle, blend_col );
1459 DeleteObject (SelectObject (hImageDC, hOldBrush));
1460 bResult = BitBlt( pimldp->hdcDst, pimldp->x, pimldp->y, cx, cy, hImageDC, 0, 0, SRCCOPY );
1461 goto end;
1465 * Draw the initial image
1467 if( bMask ) {
1468 if (himl->hbmMask) {
1469 hOldBrush = SelectObject (hImageDC, CreateSolidBrush (GetTextColor(pimldp->hdcDst)));
1470 PatBlt( hImageDC, 0, 0, cx, cy, PATCOPY );
1471 BitBlt(hImageDC, 0, 0, cx, cy, hMaskListDC, pt.x, pt.y, SRCPAINT);
1472 DeleteObject (SelectObject (hImageDC, hOldBrush));
1473 if( bIsTransparent )
1475 BitBlt ( pimldp->hdcDst, pimldp->x, pimldp->y, cx, cy, hImageDC, 0, 0, SRCAND);
1476 bResult = TRUE;
1477 goto end;
1479 } else {
1480 hOldBrush = SelectObject (hImageDC, GetStockObject(BLACK_BRUSH));
1481 PatBlt( hImageDC, 0, 0, cx, cy, PATCOPY);
1482 SelectObject(hImageDC, hOldBrush);
1484 } else {
1485 /* blend the image with the needed solid background */
1486 COLORREF colour = RGB(0,0,0);
1488 if( !bIsTransparent )
1490 colour = pimldp->rgbBk;
1491 if( colour == CLR_DEFAULT )
1492 colour = himl->clrBk;
1493 if( colour == CLR_NONE )
1494 colour = GetBkColor(pimldp->hdcDst);
1497 hOldBrush = SelectObject (hImageDC, CreateSolidBrush (colour));
1498 PatBlt( hImageDC, 0, 0, cx, cy, PATCOPY );
1499 if (himl->hbmMask)
1501 BitBlt( hImageDC, 0, 0, cx, cy, hMaskListDC, pt.x, pt.y, SRCAND );
1502 BitBlt( hImageDC, 0, 0, cx, cy, hImageListDC, pt.x, pt.y, SRCPAINT );
1504 else
1505 BitBlt( hImageDC, 0, 0, cx, cy, hImageListDC, pt.x, pt.y, SRCCOPY);
1506 DeleteObject (SelectObject (hImageDC, hOldBrush));
1509 /* Time for blending, if required */
1510 if (bBlend) {
1511 HBRUSH hBlendBrush;
1512 COLORREF clrBlend = pimldp->rgbFg;
1513 HDC hBlendMaskDC = hImageListDC;
1514 HBITMAP hOldBitmap;
1516 /* Create the blend Mask */
1517 hOldBitmap = SelectObject(hBlendMaskDC, hBlendMaskBmp);
1518 hBlendBrush = fStyle & ILD_BLEND50 ? himl->hbrBlend50 : himl->hbrBlend25;
1519 hOldBrush = SelectObject(hBlendMaskDC, hBlendBrush);
1520 PatBlt(hBlendMaskDC, 0, 0, cx, cy, PATCOPY);
1521 SelectObject(hBlendMaskDC, hOldBrush);
1523 /* Modify the blend mask if an Image Mask exist */
1524 if(himl->hbmMask) {
1525 BitBlt(hBlendMaskDC, 0, 0, cx, cy, hMaskListDC, pt.x, pt.y, 0x220326); /* NOTSRCAND */
1526 BitBlt(hBlendMaskDC, 0, 0, cx, cy, hBlendMaskDC, 0, 0, NOTSRCCOPY);
1529 /* now apply blend to the current image given the BlendMask */
1530 if (clrBlend == CLR_DEFAULT) clrBlend = GetSysColor (COLOR_HIGHLIGHT);
1531 else if (clrBlend == CLR_NONE) clrBlend = GetTextColor (pimldp->hdcDst);
1532 hOldBrush = SelectObject (hImageDC, CreateSolidBrush(clrBlend));
1533 BitBlt (hImageDC, 0, 0, cx, cy, hBlendMaskDC, 0, 0, 0xB8074A); /* PSDPxax */
1534 DeleteObject(SelectObject(hImageDC, hOldBrush));
1535 SelectObject(hBlendMaskDC, hOldBitmap);
1538 /* Now do the overlay image, if any */
1539 nOvlIdx = (pimldp->fStyle & ILD_OVERLAYMASK) >> 8;
1540 if ( (nOvlIdx >= 1) && (nOvlIdx <= MAX_OVERLAYIMAGE)) {
1541 nOvlIdx = himl->nOvlIdx[nOvlIdx - 1];
1542 if ((nOvlIdx >= 0) && (nOvlIdx < himl->cCurImage)) {
1543 POINT ptOvl;
1544 imagelist_point_from_index( himl, nOvlIdx, &ptOvl );
1545 ptOvl.x += pimldp->xBitmap;
1546 if (himl->hbmMask && !(fStyle & ILD_IMAGE))
1547 BitBlt (hImageDC, 0, 0, cx, cy, hMaskListDC, ptOvl.x, ptOvl.y, SRCAND);
1548 BitBlt (hImageDC, 0, 0, cx, cy, hImageListDC, ptOvl.x, ptOvl.y, SRCPAINT);
1552 if (fState & ILS_SATURATE) FIXME("ILS_SATURATE: unimplemented!\n");
1553 if (fState & ILS_GLOW) FIXME("ILS_GLOW: unimplemented!\n");
1554 if (fState & ILS_SHADOW) FIXME("ILS_SHADOW: unimplemented!\n");
1556 if (fStyle & ILD_PRESERVEALPHA) FIXME("ILD_PRESERVEALPHA: unimplemented!\n");
1557 if (fStyle & ILD_SCALE) FIXME("ILD_SCALE: unimplemented!\n");
1558 if (fStyle & ILD_DPISCALE) FIXME("ILD_DPISCALE: unimplemented!\n");
1560 /* now copy the image to the screen */
1561 dwRop = SRCCOPY;
1562 if (himl->hbmMask && bIsTransparent ) {
1563 COLORREF oldDstFg = SetTextColor(pimldp->hdcDst, RGB( 0, 0, 0 ) );
1564 COLORREF oldDstBk = SetBkColor(pimldp->hdcDst, RGB( 0xff, 0xff, 0xff ));
1565 BitBlt (pimldp->hdcDst, pimldp->x, pimldp->y, cx, cy, hMaskListDC, pt.x, pt.y, SRCAND);
1566 SetBkColor(pimldp->hdcDst, oldDstBk);
1567 SetTextColor(pimldp->hdcDst, oldDstFg);
1568 dwRop = SRCPAINT;
1570 if (fStyle & ILD_ROP) dwRop = pimldp->dwRop;
1571 BitBlt (pimldp->hdcDst, pimldp->x, pimldp->y, cx, cy, hImageDC, 0, 0, dwRop);
1573 bResult = TRUE;
1574 end:
1575 /* cleanup the mess */
1576 SetBkColor(hImageDC, oldImageBk);
1577 SetTextColor(hImageDC, oldImageFg);
1578 SelectObject(hImageDC, hOldImageBmp);
1579 cleanup:
1580 DeleteObject(hBlendMaskBmp);
1581 DeleteObject(hImageBmp);
1582 DeleteDC(hImageDC);
1584 return bResult;
1588 /*************************************************************************
1589 * ImageList_Duplicate [COMCTL32.@]
1591 * Duplicates an image list.
1593 * PARAMS
1594 * himlSrc [I] source image list handle
1596 * RETURNS
1597 * Success: Handle of duplicated image list.
1598 * Failure: NULL
1601 HIMAGELIST WINAPI
1602 ImageList_Duplicate (HIMAGELIST himlSrc)
1604 HIMAGELIST himlDst;
1606 if (!is_valid(himlSrc)) {
1607 ERR("Invalid image list handle!\n");
1608 return NULL;
1611 himlDst = ImageList_Create (himlSrc->cx, himlSrc->cy, himlSrc->flags,
1612 himlSrc->cCurImage, himlSrc->cGrow);
1614 if (himlDst)
1616 SIZE sz;
1618 imagelist_get_bitmap_size(himlSrc, himlSrc->cCurImage, &sz);
1619 BitBlt (himlDst->hdcImage, 0, 0, sz.cx, sz.cy,
1620 himlSrc->hdcImage, 0, 0, SRCCOPY);
1622 if (himlDst->hbmMask)
1623 BitBlt (himlDst->hdcMask, 0, 0, sz.cx, sz.cy,
1624 himlSrc->hdcMask, 0, 0, SRCCOPY);
1626 himlDst->cCurImage = himlSrc->cCurImage;
1627 if (himlSrc->has_alpha && himlDst->has_alpha)
1628 memcpy( himlDst->has_alpha, himlSrc->has_alpha, himlDst->cCurImage );
1630 return himlDst;
1634 /*************************************************************************
1635 * ImageList_EndDrag [COMCTL32.@]
1637 * Finishes a drag operation.
1639 * PARAMS
1640 * no Parameters
1642 * RETURNS
1643 * Success: TRUE
1644 * Failure: FALSE
1647 VOID WINAPI
1648 ImageList_EndDrag (void)
1650 /* cleanup the InternalDrag struct */
1651 InternalDrag.hwnd = 0;
1652 if (InternalDrag.himl != InternalDrag.himlNoCursor)
1653 ImageList_Destroy (InternalDrag.himlNoCursor);
1654 ImageList_Destroy (InternalDrag.himl);
1655 InternalDrag.himlNoCursor = InternalDrag.himl = 0;
1656 InternalDrag.x= 0;
1657 InternalDrag.y= 0;
1658 InternalDrag.dxHotspot = 0;
1659 InternalDrag.dyHotspot = 0;
1660 InternalDrag.bShow = FALSE;
1661 DeleteObject(InternalDrag.hbmBg);
1662 InternalDrag.hbmBg = 0;
1666 /*************************************************************************
1667 * ImageList_GetBkColor [COMCTL32.@]
1669 * Returns the background color of an image list.
1671 * PARAMS
1672 * himl [I] Image list handle.
1674 * RETURNS
1675 * Success: background color
1676 * Failure: CLR_NONE
1679 COLORREF WINAPI
1680 ImageList_GetBkColor (HIMAGELIST himl)
1682 return himl ? himl->clrBk : CLR_NONE;
1686 /*************************************************************************
1687 * ImageList_GetDragImage [COMCTL32.@]
1689 * Returns the handle to the internal drag image list.
1691 * PARAMS
1692 * ppt [O] Pointer to the drag position. Can be NULL.
1693 * pptHotspot [O] Pointer to the position of the hot spot. Can be NULL.
1695 * RETURNS
1696 * Success: Handle of the drag image list.
1697 * Failure: NULL.
1700 HIMAGELIST WINAPI
1701 ImageList_GetDragImage (POINT *ppt, POINT *pptHotspot)
1703 if (is_valid(InternalDrag.himl)) {
1704 if (ppt) {
1705 ppt->x = InternalDrag.x;
1706 ppt->y = InternalDrag.y;
1708 if (pptHotspot) {
1709 pptHotspot->x = InternalDrag.dxHotspot;
1710 pptHotspot->y = InternalDrag.dyHotspot;
1712 return (InternalDrag.himl);
1715 return NULL;
1719 /*************************************************************************
1720 * ImageList_GetFlags [COMCTL32.@]
1722 * Gets the flags of the specified image list.
1724 * PARAMS
1725 * himl [I] Handle to image list
1727 * RETURNS
1728 * Image list flags.
1730 * BUGS
1731 * Stub.
1734 DWORD WINAPI
1735 ImageList_GetFlags(HIMAGELIST himl)
1737 TRACE("%p\n", himl);
1739 return is_valid(himl) ? himl->flags : 0;
1743 /*************************************************************************
1744 * ImageList_GetIcon [COMCTL32.@]
1746 * Creates an icon from a masked image of an image list.
1748 * PARAMS
1749 * himl [I] handle to image list
1750 * i [I] image index
1751 * flags [I] drawing style flags
1753 * RETURNS
1754 * Success: icon handle
1755 * Failure: NULL
1758 HICON WINAPI
1759 ImageList_GetIcon (HIMAGELIST himl, INT i, UINT fStyle)
1761 ICONINFO ii;
1762 HICON hIcon;
1763 HBITMAP hOldDstBitmap;
1764 HDC hdcDst;
1765 POINT pt;
1767 TRACE("%p %d %d\n", himl, i, fStyle);
1768 if (!is_valid(himl) || (i < 0) || (i >= himl->cCurImage)) return NULL;
1770 ii.fIcon = TRUE;
1771 ii.xHotspot = 0;
1772 ii.yHotspot = 0;
1774 /* create colour bitmap */
1775 hdcDst = GetDC(0);
1776 ii.hbmColor = CreateCompatibleBitmap(hdcDst, himl->cx, himl->cy);
1777 ReleaseDC(0, hdcDst);
1779 hdcDst = CreateCompatibleDC(0);
1781 imagelist_point_from_index( himl, i, &pt );
1783 /* draw mask*/
1784 ii.hbmMask = CreateBitmap (himl->cx, himl->cy, 1, 1, NULL);
1785 hOldDstBitmap = SelectObject (hdcDst, ii.hbmMask);
1786 if (himl->hbmMask) {
1787 BitBlt (hdcDst, 0, 0, himl->cx, himl->cy,
1788 himl->hdcMask, pt.x, pt.y, SRCCOPY);
1790 else
1791 PatBlt (hdcDst, 0, 0, himl->cx, himl->cy, BLACKNESS);
1793 /* draw image*/
1794 SelectObject (hdcDst, ii.hbmColor);
1795 BitBlt (hdcDst, 0, 0, himl->cx, himl->cy,
1796 himl->hdcImage, pt.x, pt.y, SRCCOPY);
1799 * CreateIconIndirect requires us to deselect the bitmaps from
1800 * the DCs before calling
1802 SelectObject(hdcDst, hOldDstBitmap);
1804 hIcon = CreateIconIndirect (&ii);
1806 DeleteObject (ii.hbmMask);
1807 DeleteObject (ii.hbmColor);
1808 DeleteDC (hdcDst);
1810 return hIcon;
1814 /*************************************************************************
1815 * ImageList_GetIconSize [COMCTL32.@]
1817 * Retrieves the size of an image in an image list.
1819 * PARAMS
1820 * himl [I] handle to image list
1821 * cx [O] pointer to the image width.
1822 * cy [O] pointer to the image height.
1824 * RETURNS
1825 * Success: TRUE
1826 * Failure: FALSE
1828 * NOTES
1829 * All images in an image list have the same size.
1832 BOOL WINAPI
1833 ImageList_GetIconSize (HIMAGELIST himl, INT *cx, INT *cy)
1835 if (!is_valid(himl) || !cx || !cy)
1836 return FALSE;
1838 *cx = himl->cx;
1839 *cy = himl->cy;
1841 return TRUE;
1845 /*************************************************************************
1846 * ImageList_GetImageCount [COMCTL32.@]
1848 * Returns the number of images in an image list.
1850 * PARAMS
1851 * himl [I] handle to image list
1853 * RETURNS
1854 * Success: Number of images.
1855 * Failure: 0
1858 INT WINAPI
1859 ImageList_GetImageCount (HIMAGELIST himl)
1861 if (!is_valid(himl))
1862 return 0;
1864 return himl->cCurImage;
1868 /*************************************************************************
1869 * ImageList_GetImageInfo [COMCTL32.@]
1871 * Returns information about an image in an image list.
1873 * PARAMS
1874 * himl [I] handle to image list
1875 * i [I] image index
1876 * pImageInfo [O] pointer to the image information
1878 * RETURNS
1879 * Success: TRUE
1880 * Failure: FALSE
1883 BOOL WINAPI
1884 ImageList_GetImageInfo (HIMAGELIST himl, INT i, IMAGEINFO *pImageInfo)
1886 POINT pt;
1888 if (!is_valid(himl) || (pImageInfo == NULL))
1889 return FALSE;
1890 if ((i < 0) || (i >= himl->cCurImage))
1891 return FALSE;
1893 pImageInfo->hbmImage = himl->hbmImage;
1894 pImageInfo->hbmMask = himl->hbmMask;
1896 imagelist_point_from_index( himl, i, &pt );
1897 pImageInfo->rcImage.top = pt.y;
1898 pImageInfo->rcImage.bottom = pt.y + himl->cy;
1899 pImageInfo->rcImage.left = pt.x;
1900 pImageInfo->rcImage.right = pt.x + himl->cx;
1902 return TRUE;
1906 /*************************************************************************
1907 * ImageList_GetImageRect [COMCTL32.@]
1909 * Retrieves the rectangle of the specified image in an image list.
1911 * PARAMS
1912 * himl [I] handle to image list
1913 * i [I] image index
1914 * lpRect [O] pointer to the image rectangle
1916 * RETURNS
1917 * Success: TRUE
1918 * Failure: FALSE
1920 * NOTES
1921 * This is an UNDOCUMENTED function!!!
1924 BOOL WINAPI
1925 ImageList_GetImageRect (HIMAGELIST himl, INT i, LPRECT lpRect)
1927 POINT pt;
1929 if (!is_valid(himl) || (lpRect == NULL))
1930 return FALSE;
1931 if ((i < 0) || (i >= himl->cCurImage))
1932 return FALSE;
1934 imagelist_point_from_index( himl, i, &pt );
1935 lpRect->left = pt.x;
1936 lpRect->top = pt.y;
1937 lpRect->right = pt.x + himl->cx;
1938 lpRect->bottom = pt.y + himl->cy;
1940 return TRUE;
1944 /*************************************************************************
1945 * ImageList_LoadImage [COMCTL32.@]
1946 * ImageList_LoadImageA [COMCTL32.@]
1948 * Creates an image list from a bitmap, icon or cursor.
1950 * See ImageList_LoadImageW.
1953 HIMAGELIST WINAPI
1954 ImageList_LoadImageA (HINSTANCE hi, LPCSTR lpbmp, INT cx, INT cGrow,
1955 COLORREF clrMask, UINT uType, UINT uFlags)
1957 HIMAGELIST himl;
1958 LPWSTR lpbmpW;
1959 DWORD len;
1961 if (IS_INTRESOURCE(lpbmp))
1962 return ImageList_LoadImageW(hi, (LPCWSTR)lpbmp, cx, cGrow, clrMask,
1963 uType, uFlags);
1965 len = MultiByteToWideChar(CP_ACP, 0, lpbmp, -1, NULL, 0);
1966 lpbmpW = heap_alloc(len * sizeof(WCHAR));
1967 MultiByteToWideChar(CP_ACP, 0, lpbmp, -1, lpbmpW, len);
1969 himl = ImageList_LoadImageW(hi, lpbmpW, cx, cGrow, clrMask, uType, uFlags);
1970 heap_free (lpbmpW);
1971 return himl;
1975 /*************************************************************************
1976 * ImageList_LoadImageW [COMCTL32.@]
1978 * Creates an image list from a bitmap, icon or cursor.
1980 * PARAMS
1981 * hi [I] instance handle
1982 * lpbmp [I] name or id of the image
1983 * cx [I] width of each image
1984 * cGrow [I] number of images to expand
1985 * clrMask [I] mask color
1986 * uType [I] type of image to load
1987 * uFlags [I] loading flags
1989 * RETURNS
1990 * Success: handle to the loaded image list
1991 * Failure: NULL
1993 * SEE
1994 * LoadImage ()
1997 HIMAGELIST WINAPI
1998 ImageList_LoadImageW (HINSTANCE hi, LPCWSTR lpbmp, INT cx, INT cGrow,
1999 COLORREF clrMask, UINT uType, UINT uFlags)
2001 HIMAGELIST himl = NULL;
2002 HANDLE handle;
2003 INT nImageCount;
2005 handle = LoadImageW (hi, lpbmp, uType, 0, 0, uFlags);
2006 if (!handle) {
2007 WARN("Couldn't load image\n");
2008 return NULL;
2011 if (uType == IMAGE_BITMAP) {
2012 DIBSECTION dib;
2013 UINT color;
2015 if (GetObjectW (handle, sizeof(dib), &dib) == sizeof(BITMAP)) color = ILC_COLOR;
2016 else color = dib.dsBm.bmBitsPixel;
2018 /* To match windows behavior, if cx is set to zero and
2019 the flag DI_DEFAULTSIZE is specified, cx becomes the
2020 system metric value for icons. If the flag is not specified
2021 the function sets the size to the height of the bitmap */
2022 if (cx == 0)
2024 if (uFlags & DI_DEFAULTSIZE)
2025 cx = GetSystemMetrics (SM_CXICON);
2026 else
2027 cx = dib.dsBm.bmHeight;
2030 nImageCount = dib.dsBm.bmWidth / cx;
2032 if (clrMask != CLR_NONE) color |= ILC_MASK;
2033 himl = ImageList_Create (cx, dib.dsBm.bmHeight, color, nImageCount, cGrow);
2034 if (!himl) {
2035 DeleteObject (handle);
2036 return NULL;
2038 ImageList_AddMasked (himl, handle, clrMask);
2040 else if ((uType == IMAGE_ICON) || (uType == IMAGE_CURSOR)) {
2041 ICONINFO ii;
2042 BITMAP bmp;
2044 GetIconInfo (handle, &ii);
2045 GetObjectW (ii.hbmColor, sizeof(BITMAP), &bmp);
2046 himl = ImageList_Create (bmp.bmWidth, bmp.bmHeight,
2047 ILC_MASK | ILC_COLOR, 1, cGrow);
2048 if (!himl) {
2049 DeleteObject (ii.hbmColor);
2050 DeleteObject (ii.hbmMask);
2051 DeleteObject (handle);
2052 return NULL;
2054 ImageList_Add (himl, ii.hbmColor, ii.hbmMask);
2055 DeleteObject (ii.hbmColor);
2056 DeleteObject (ii.hbmMask);
2059 DeleteObject (handle);
2061 return himl;
2065 /*************************************************************************
2066 * ImageList_Merge [COMCTL32.@]
2068 * Create an image list containing a merged image from two image lists.
2070 * PARAMS
2071 * himl1 [I] handle to first image list
2072 * i1 [I] first image index
2073 * himl2 [I] handle to second image list
2074 * i2 [I] second image index
2075 * dx [I] X offset of the second image relative to the first.
2076 * dy [I] Y offset of the second image relative to the first.
2078 * RETURNS
2079 * Success: The newly created image list. It contains a single image
2080 * consisting of the second image merged with the first.
2081 * Failure: NULL, if either himl1 or himl2 is invalid.
2083 * NOTES
2084 * - The returned image list should be deleted by the caller using
2085 * ImageList_Destroy() when it is no longer required.
2086 * - If either i1 or i2 is not a valid image index, they will be treated
2087 * as blank images.
2089 HIMAGELIST WINAPI
2090 ImageList_Merge (HIMAGELIST himl1, INT i1, HIMAGELIST himl2, INT i2,
2091 INT dx, INT dy)
2093 HIMAGELIST himlDst = NULL;
2094 INT cxDst, cyDst;
2095 INT xOff1, yOff1, xOff2, yOff2;
2096 POINT pt1, pt2;
2097 INT newFlags;
2099 TRACE("(himl1=%p i1=%d himl2=%p i2=%d dx=%d dy=%d)\n", himl1, i1, himl2,
2100 i2, dx, dy);
2102 if (!is_valid(himl1) || !is_valid(himl2))
2103 return NULL;
2105 if (dx > 0) {
2106 cxDst = max (himl1->cx, dx + himl2->cx);
2107 xOff1 = 0;
2108 xOff2 = dx;
2110 else if (dx < 0) {
2111 cxDst = max (himl2->cx, himl1->cx - dx);
2112 xOff1 = -dx;
2113 xOff2 = 0;
2115 else {
2116 cxDst = max (himl1->cx, himl2->cx);
2117 xOff1 = 0;
2118 xOff2 = 0;
2121 if (dy > 0) {
2122 cyDst = max (himl1->cy, dy + himl2->cy);
2123 yOff1 = 0;
2124 yOff2 = dy;
2126 else if (dy < 0) {
2127 cyDst = max (himl2->cy, himl1->cy - dy);
2128 yOff1 = -dy;
2129 yOff2 = 0;
2131 else {
2132 cyDst = max (himl1->cy, himl2->cy);
2133 yOff1 = 0;
2134 yOff2 = 0;
2137 newFlags = (himl1->flags > himl2->flags ? himl1->flags : himl2->flags) & ILC_COLORDDB;
2138 if (newFlags == ILC_COLORDDB && (himl1->flags & ILC_COLORDDB) == ILC_COLOR16)
2139 newFlags = ILC_COLOR16; /* this is what native (at least v5) does, don't know why */
2140 himlDst = ImageList_Create (cxDst, cyDst, ILC_MASK | newFlags, 1, 1);
2142 if (himlDst)
2144 imagelist_point_from_index( himl1, i1, &pt1 );
2145 imagelist_point_from_index( himl2, i2, &pt2 );
2147 /* copy image */
2148 BitBlt (himlDst->hdcImage, 0, 0, cxDst, cyDst, himl1->hdcImage, 0, 0, BLACKNESS);
2149 if (i1 >= 0 && i1 < himl1->cCurImage)
2150 BitBlt (himlDst->hdcImage, xOff1, yOff1, himl1->cx, himl1->cy, himl1->hdcImage, pt1.x, pt1.y, SRCCOPY);
2151 if (i2 >= 0 && i2 < himl2->cCurImage)
2153 if (himl2->flags & ILC_MASK)
2155 BitBlt (himlDst->hdcImage, xOff2, yOff2, himl2->cx, himl2->cy, himl2->hdcMask , pt2.x, pt2.y, SRCAND);
2156 BitBlt (himlDst->hdcImage, xOff2, yOff2, himl2->cx, himl2->cy, himl2->hdcImage, pt2.x, pt2.y, SRCPAINT);
2158 else
2159 BitBlt (himlDst->hdcImage, xOff2, yOff2, himl2->cx, himl2->cy, himl2->hdcImage, pt2.x, pt2.y, SRCCOPY);
2162 /* copy mask */
2163 BitBlt (himlDst->hdcMask, 0, 0, cxDst, cyDst, himl1->hdcMask, 0, 0, WHITENESS);
2164 if (i1 >= 0 && i1 < himl1->cCurImage)
2165 BitBlt (himlDst->hdcMask, xOff1, yOff1, himl1->cx, himl1->cy, himl1->hdcMask, pt1.x, pt1.y, SRCCOPY);
2166 if (i2 >= 0 && i2 < himl2->cCurImage)
2167 BitBlt (himlDst->hdcMask, xOff2, yOff2, himl2->cx, himl2->cy, himl2->hdcMask, pt2.x, pt2.y, SRCAND);
2169 himlDst->cCurImage = 1;
2172 return himlDst;
2176 /* helper for ImageList_Read, see comments below */
2177 static void *read_bitmap(IStream *pstm, BITMAPINFO *bmi)
2179 BITMAPFILEHEADER bmfh;
2180 int bitsperpixel, palspace;
2181 void *bits;
2183 if (FAILED(IStream_Read ( pstm, &bmfh, sizeof(bmfh), NULL)))
2184 return NULL;
2186 if (bmfh.bfType != (('M'<<8)|'B'))
2187 return NULL;
2189 if (FAILED(IStream_Read ( pstm, &bmi->bmiHeader, sizeof(bmi->bmiHeader), NULL)))
2190 return NULL;
2192 if ((bmi->bmiHeader.biSize != sizeof(bmi->bmiHeader)))
2193 return NULL;
2195 TRACE("width %u, height %u, planes %u, bpp %u\n",
2196 bmi->bmiHeader.biWidth, bmi->bmiHeader.biHeight,
2197 bmi->bmiHeader.biPlanes, bmi->bmiHeader.biBitCount);
2199 bitsperpixel = bmi->bmiHeader.biPlanes * bmi->bmiHeader.biBitCount;
2200 if (bitsperpixel<=8)
2201 palspace = (1<<bitsperpixel)*sizeof(RGBQUAD);
2202 else
2203 palspace = 0;
2205 bmi->bmiHeader.biSizeImage = get_dib_image_size( bmi );
2207 /* read the palette right after the end of the bitmapinfoheader */
2208 if (palspace && FAILED(IStream_Read(pstm, bmi->bmiColors, palspace, NULL)))
2209 return NULL;
2211 bits = heap_alloc_zero(bmi->bmiHeader.biSizeImage);
2212 if (!bits) return NULL;
2214 if (FAILED(IStream_Read(pstm, bits, bmi->bmiHeader.biSizeImage, NULL)))
2216 heap_free(bits);
2217 return NULL;
2219 return bits;
2222 /*************************************************************************
2223 * ImageList_Read [COMCTL32.@]
2225 * Reads an image list from a stream.
2227 * PARAMS
2228 * pstm [I] pointer to a stream
2230 * RETURNS
2231 * Success: handle to image list
2232 * Failure: NULL
2234 * The format is like this:
2235 * ILHEAD ilheadstruct;
2237 * for the color image part:
2238 * BITMAPFILEHEADER bmfh;
2239 * BITMAPINFOHEADER bmih;
2240 * only if it has a palette:
2241 * RGBQUAD rgbs[nr_of_paletted_colors];
2243 * BYTE colorbits[imagesize];
2245 * the following only if the ILC_MASK bit is set in ILHEAD.ilFlags:
2246 * BITMAPFILEHEADER bmfh_mask;
2247 * BITMAPINFOHEADER bmih_mask;
2248 * only if it has a palette (it usually does not):
2249 * RGBQUAD rgbs[nr_of_paletted_colors];
2251 * BYTE maskbits[imagesize];
2253 HIMAGELIST WINAPI ImageList_Read(IStream *pstm)
2255 char image_buf[sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * 256];
2256 char mask_buf[sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * 256];
2257 BITMAPINFO *image_info = (BITMAPINFO *)image_buf;
2258 BITMAPINFO *mask_info = (BITMAPINFO *)mask_buf;
2259 void *image_bits, *mask_bits = NULL;
2260 ILHEAD ilHead;
2261 HIMAGELIST himl;
2262 unsigned int i;
2264 TRACE("%p\n", pstm);
2266 if (FAILED(IStream_Read (pstm, &ilHead, sizeof(ILHEAD), NULL)))
2267 return NULL;
2268 if (ilHead.usMagic != (('L' << 8) | 'I'))
2269 return NULL;
2270 if (ilHead.usVersion != 0x101) /* probably version? */
2271 return NULL;
2273 TRACE("cx %u, cy %u, flags 0x%04x, cCurImage %u, cMaxImage %u\n",
2274 ilHead.cx, ilHead.cy, ilHead.flags, ilHead.cCurImage, ilHead.cMaxImage);
2276 himl = ImageList_Create(ilHead.cx, ilHead.cy, ilHead.flags, ilHead.cMaxImage, ilHead.cGrow);
2277 if (!himl)
2278 return NULL;
2280 if (!(image_bits = read_bitmap(pstm, image_info)))
2282 WARN("failed to read bitmap from stream\n");
2283 return NULL;
2285 if (ilHead.flags & ILC_MASK)
2287 if (!(mask_bits = read_bitmap(pstm, mask_info)))
2289 WARN("failed to read mask bitmap from stream\n");
2290 return NULL;
2293 else mask_info = NULL;
2295 if (himl->has_alpha && image_info->bmiHeader.biBitCount == 32)
2297 DWORD *ptr = image_bits;
2298 BYTE *mask_ptr = mask_bits;
2299 int stride = himl->cy * image_info->bmiHeader.biWidth;
2301 if (image_info->bmiHeader.biHeight > 0) /* bottom-up */
2303 ptr += image_info->bmiHeader.biHeight * image_info->bmiHeader.biWidth - stride;
2304 mask_ptr += (image_info->bmiHeader.biHeight * image_info->bmiHeader.biWidth - stride) / 8;
2305 stride = -stride;
2306 image_info->bmiHeader.biHeight = himl->cy;
2308 else image_info->bmiHeader.biHeight = -himl->cy;
2310 for (i = 0; i < ilHead.cCurImage; i += TILE_COUNT)
2312 add_dib_bits( himl, i, min( ilHead.cCurImage - i, TILE_COUNT ),
2313 himl->cx, himl->cy, image_info, mask_info, ptr, mask_ptr );
2314 ptr += stride;
2315 mask_ptr += stride / 8;
2318 else
2320 StretchDIBits( himl->hdcImage, 0, 0, image_info->bmiHeader.biWidth, image_info->bmiHeader.biHeight,
2321 0, 0, image_info->bmiHeader.biWidth, image_info->bmiHeader.biHeight,
2322 image_bits, image_info, DIB_RGB_COLORS, SRCCOPY);
2323 if (mask_info)
2324 StretchDIBits( himl->hdcMask, 0, 0, mask_info->bmiHeader.biWidth, mask_info->bmiHeader.biHeight,
2325 0, 0, mask_info->bmiHeader.biWidth, mask_info->bmiHeader.biHeight,
2326 mask_bits, mask_info, DIB_RGB_COLORS, SRCCOPY);
2328 heap_free( image_bits );
2329 heap_free( mask_bits );
2331 himl->cCurImage = ilHead.cCurImage;
2332 himl->cMaxImage = ilHead.cMaxImage;
2334 ImageList_SetBkColor(himl,ilHead.bkcolor);
2335 for (i=0;i<4;i++)
2336 ImageList_SetOverlayImage(himl,ilHead.ovls[i],i+1);
2337 return himl;
2341 /*************************************************************************
2342 * ImageList_Remove [COMCTL32.@]
2344 * Removes an image from an image list
2346 * PARAMS
2347 * himl [I] image list handle
2348 * i [I] image index
2350 * RETURNS
2351 * Success: TRUE
2352 * Failure: FALSE
2354 * FIXME: as the image list storage test shows, native comctl32 simply shifts
2355 * images without creating a new bitmap.
2357 BOOL WINAPI
2358 ImageList_Remove (HIMAGELIST himl, INT i)
2360 HBITMAP hbmNewImage, hbmNewMask;
2361 HDC hdcBmp;
2362 SIZE sz;
2364 TRACE("(himl=%p i=%d)\n", himl, i);
2366 if (!is_valid(himl)) {
2367 ERR("Invalid image list handle!\n");
2368 return FALSE;
2371 if ((i < -1) || (i >= himl->cCurImage)) {
2372 TRACE("index out of range! %d\n", i);
2373 return FALSE;
2376 if (i == -1) {
2377 INT nCount;
2379 /* remove all */
2380 if (himl->cCurImage == 0) {
2381 /* remove all on empty ImageList is allowed */
2382 TRACE("remove all on empty ImageList!\n");
2383 return TRUE;
2386 himl->cMaxImage = himl->cGrow;
2387 himl->cCurImage = 0;
2388 for (nCount = 0; nCount < MAX_OVERLAYIMAGE; nCount++)
2389 himl->nOvlIdx[nCount] = -1;
2391 if (himl->has_alpha)
2393 heap_free( himl->has_alpha );
2394 himl->has_alpha = heap_alloc_zero( himl->cMaxImage );
2397 hbmNewImage = ImageList_CreateImage(himl->hdcImage, himl, himl->cMaxImage);
2398 SelectObject (himl->hdcImage, hbmNewImage);
2399 DeleteObject (himl->hbmImage);
2400 himl->hbmImage = hbmNewImage;
2402 if (himl->hbmMask) {
2404 imagelist_get_bitmap_size(himl, himl->cMaxImage, &sz);
2405 hbmNewMask = CreateBitmap (sz.cx, sz.cy, 1, 1, NULL);
2406 SelectObject (himl->hdcMask, hbmNewMask);
2407 DeleteObject (himl->hbmMask);
2408 himl->hbmMask = hbmNewMask;
2411 else {
2412 /* delete one image */
2413 TRACE("Remove single image! %d\n", i);
2415 /* create new bitmap(s) */
2416 TRACE(" - Number of images: %d / %d (Old/New)\n",
2417 himl->cCurImage, himl->cCurImage - 1);
2419 hbmNewImage = ImageList_CreateImage(himl->hdcImage, himl, himl->cMaxImage);
2421 imagelist_get_bitmap_size(himl, himl->cMaxImage, &sz );
2422 if (himl->hbmMask)
2423 hbmNewMask = CreateBitmap (sz.cx, sz.cy, 1, 1, NULL);
2424 else
2425 hbmNewMask = 0; /* Just to keep compiler happy! */
2427 hdcBmp = CreateCompatibleDC (0);
2429 /* copy all images and masks prior to the "removed" image */
2430 if (i > 0) {
2431 TRACE("Pre image copy: Copy %d images\n", i);
2433 SelectObject (hdcBmp, hbmNewImage);
2434 imagelist_copy_images( himl, himl->hdcImage, hdcBmp, 0, i, 0 );
2436 if (himl->hbmMask) {
2437 SelectObject (hdcBmp, hbmNewMask);
2438 imagelist_copy_images( himl, himl->hdcMask, hdcBmp, 0, i, 0 );
2442 /* copy all images and masks behind the removed image */
2443 if (i < himl->cCurImage - 1) {
2444 TRACE("Post image copy!\n");
2446 SelectObject (hdcBmp, hbmNewImage);
2447 imagelist_copy_images( himl, himl->hdcImage, hdcBmp, i + 1,
2448 (himl->cCurImage - i), i );
2450 if (himl->hbmMask) {
2451 SelectObject (hdcBmp, hbmNewMask);
2452 imagelist_copy_images( himl, himl->hdcMask, hdcBmp, i + 1,
2453 (himl->cCurImage - i), i );
2457 DeleteDC (hdcBmp);
2459 /* delete old images and insert new ones */
2460 SelectObject (himl->hdcImage, hbmNewImage);
2461 DeleteObject (himl->hbmImage);
2462 himl->hbmImage = hbmNewImage;
2463 if (himl->hbmMask) {
2464 SelectObject (himl->hdcMask, hbmNewMask);
2465 DeleteObject (himl->hbmMask);
2466 himl->hbmMask = hbmNewMask;
2469 himl->cCurImage--;
2472 return TRUE;
2476 /*************************************************************************
2477 * ImageList_Replace [COMCTL32.@]
2479 * Replaces an image in an image list with a new image.
2481 * PARAMS
2482 * himl [I] handle to image list
2483 * i [I] image index
2484 * hbmImage [I] handle to image bitmap
2485 * hbmMask [I] handle to mask bitmap. Can be NULL.
2487 * RETURNS
2488 * Success: TRUE
2489 * Failure: FALSE
2492 BOOL WINAPI
2493 ImageList_Replace (HIMAGELIST himl, INT i, HBITMAP hbmImage,
2494 HBITMAP hbmMask)
2496 HDC hdcImage;
2497 BITMAP bmp;
2498 POINT pt;
2500 TRACE("%p %d %p %p\n", himl, i, hbmImage, hbmMask);
2502 if (!is_valid(himl)) {
2503 ERR("Invalid image list handle!\n");
2504 return FALSE;
2507 if ((i >= himl->cMaxImage) || (i < 0)) {
2508 ERR("Invalid image index!\n");
2509 return FALSE;
2512 if (!GetObjectW(hbmImage, sizeof(BITMAP), &bmp))
2513 return FALSE;
2515 hdcImage = CreateCompatibleDC (0);
2517 /* Replace Image */
2518 SelectObject (hdcImage, hbmImage);
2520 if (add_with_alpha( himl, hdcImage, i, 1, bmp.bmWidth, bmp.bmHeight, hbmImage, hbmMask ))
2521 goto done;
2523 imagelist_point_from_index(himl, i, &pt);
2524 StretchBlt (himl->hdcImage, pt.x, pt.y, himl->cx, himl->cy,
2525 hdcImage, 0, 0, bmp.bmWidth, bmp.bmHeight, SRCCOPY);
2527 if (himl->hbmMask)
2529 HDC hdcTemp;
2530 HBITMAP hOldBitmapTemp;
2532 hdcTemp = CreateCompatibleDC(0);
2533 hOldBitmapTemp = SelectObject(hdcTemp, hbmMask);
2535 StretchBlt (himl->hdcMask, pt.x, pt.y, himl->cx, himl->cy,
2536 hdcTemp, 0, 0, bmp.bmWidth, bmp.bmHeight, SRCCOPY);
2537 SelectObject(hdcTemp, hOldBitmapTemp);
2538 DeleteDC(hdcTemp);
2540 /* Remove the background from the image
2542 BitBlt (himl->hdcImage, pt.x, pt.y, bmp.bmWidth, bmp.bmHeight,
2543 himl->hdcMask, pt.x, pt.y, 0x220326); /* NOTSRCAND */
2546 done:
2547 DeleteDC (hdcImage);
2549 return TRUE;
2553 /*************************************************************************
2554 * ImageList_ReplaceIcon [COMCTL32.@]
2556 * Replaces an image in an image list using an icon.
2558 * PARAMS
2559 * himl [I] handle to image list
2560 * i [I] image index
2561 * hIcon [I] handle to icon
2563 * RETURNS
2564 * Success: index of the replaced image
2565 * Failure: -1
2568 INT WINAPI
2569 ImageList_ReplaceIcon (HIMAGELIST himl, INT nIndex, HICON hIcon)
2571 HICON hBestFitIcon;
2572 ICONINFO ii;
2573 BITMAP bmp;
2574 BOOL ret;
2575 POINT pt;
2577 TRACE("(%p %d %p)\n", himl, nIndex, hIcon);
2579 if (!is_valid(himl)) {
2580 ERR("invalid image list\n");
2581 return -1;
2583 if ((nIndex >= himl->cMaxImage) || (nIndex < -1)) {
2584 ERR("invalid image index %d / %d\n", nIndex, himl->cMaxImage);
2585 return -1;
2588 hBestFitIcon = CopyImage(
2589 hIcon, IMAGE_ICON,
2590 himl->cx, himl->cy,
2591 LR_COPYFROMRESOURCE);
2592 /* the above will fail if the icon wasn't loaded from a resource, so try
2593 * again without LR_COPYFROMRESOURCE flag */
2594 if (!hBestFitIcon)
2595 hBestFitIcon = CopyImage(
2596 hIcon, IMAGE_ICON,
2597 himl->cx, himl->cy,
2599 if (!hBestFitIcon)
2600 return -1;
2602 if (nIndex == -1) {
2603 if (himl->cCurImage + 1 >= himl->cMaxImage)
2604 IMAGELIST_InternalExpandBitmaps(himl, 1);
2606 nIndex = himl->cCurImage;
2607 himl->cCurImage++;
2610 if (himl->has_alpha && GetIconInfo (hBestFitIcon, &ii))
2612 HDC hdcImage = CreateCompatibleDC( 0 );
2613 GetObjectW (ii.hbmMask, sizeof(BITMAP), &bmp);
2615 if (!ii.hbmColor)
2617 UINT height = bmp.bmHeight / 2;
2618 HDC hdcMask = CreateCompatibleDC( 0 );
2619 HBITMAP color = CreateBitmap( bmp.bmWidth, height, 1, 1, NULL );
2620 SelectObject( hdcImage, color );
2621 SelectObject( hdcMask, ii.hbmMask );
2622 BitBlt( hdcImage, 0, 0, bmp.bmWidth, height, hdcMask, 0, height, SRCCOPY );
2623 ret = add_with_alpha( himl, hdcImage, nIndex, 1, bmp.bmWidth, height, color, ii.hbmMask );
2624 DeleteDC( hdcMask );
2625 DeleteObject( color );
2627 else ret = add_with_alpha( himl, hdcImage, nIndex, 1, bmp.bmWidth, bmp.bmHeight,
2628 ii.hbmColor, ii.hbmMask );
2630 DeleteDC( hdcImage );
2631 DeleteObject (ii.hbmMask);
2632 if (ii.hbmColor) DeleteObject (ii.hbmColor);
2633 if (ret) goto done;
2636 imagelist_point_from_index(himl, nIndex, &pt);
2638 if (himl->hbmMask)
2640 DrawIconEx( himl->hdcImage, pt.x, pt.y, hBestFitIcon, himl->cx, himl->cy, 0, 0, DI_IMAGE );
2641 PatBlt( himl->hdcMask, pt.x, pt.y, himl->cx, himl->cy, WHITENESS );
2642 DrawIconEx( himl->hdcMask, pt.x, pt.y, hBestFitIcon, himl->cx, himl->cy, 0, 0, DI_MASK );
2644 else
2646 COLORREF color = himl->clrBk != CLR_NONE ? himl->clrBk : comctl32_color.clrWindow;
2647 HBRUSH brush = CreateSolidBrush( GetNearestColor( himl->hdcImage, color ));
2649 SelectObject( himl->hdcImage, brush );
2650 PatBlt( himl->hdcImage, pt.x, pt.y, himl->cx, himl->cy, PATCOPY );
2651 SelectObject( himl->hdcImage, GetStockObject(BLACK_BRUSH) );
2652 DeleteObject( brush );
2653 DrawIconEx( himl->hdcImage, pt.x, pt.y, hBestFitIcon, himl->cx, himl->cy, 0, 0, DI_NORMAL );
2656 done:
2657 DestroyIcon(hBestFitIcon);
2659 TRACE("Insert index = %d, himl->cCurImage = %d\n", nIndex, himl->cCurImage);
2660 return nIndex;
2664 /*************************************************************************
2665 * ImageList_SetBkColor [COMCTL32.@]
2667 * Sets the background color of an image list.
2669 * PARAMS
2670 * himl [I] handle to image list
2671 * clrBk [I] background color
2673 * RETURNS
2674 * Success: previous background color
2675 * Failure: CLR_NONE
2678 COLORREF WINAPI
2679 ImageList_SetBkColor (HIMAGELIST himl, COLORREF clrBk)
2681 COLORREF clrOldBk;
2683 if (!is_valid(himl))
2684 return CLR_NONE;
2686 clrOldBk = himl->clrBk;
2687 himl->clrBk = clrBk;
2688 return clrOldBk;
2692 /*************************************************************************
2693 * ImageList_SetDragCursorImage [COMCTL32.@]
2695 * Combines the specified image with the current drag image
2697 * PARAMS
2698 * himlDrag [I] handle to drag image list
2699 * iDrag [I] drag image index
2700 * dxHotspot [I] X position of the hot spot
2701 * dyHotspot [I] Y position of the hot spot
2703 * RETURNS
2704 * Success: TRUE
2705 * Failure: FALSE
2707 * NOTES
2708 * - The names dxHotspot, dyHotspot are misleading because they have nothing
2709 * to do with a hotspot but are only the offset of the origin of the new
2710 * image relative to the origin of the old image.
2712 * - When this function is called and the drag image is visible, a
2713 * short flickering occurs but this matches the Win9x behavior. It is
2714 * possible to fix the flickering using code like in ImageList_DragMove.
2717 BOOL WINAPI
2718 ImageList_SetDragCursorImage (HIMAGELIST himlDrag, INT iDrag,
2719 INT dxHotspot, INT dyHotspot)
2721 HIMAGELIST himlTemp;
2722 BOOL visible;
2724 if (!is_valid(InternalDrag.himl) || !is_valid(himlDrag))
2725 return FALSE;
2727 TRACE(" dxH=%d dyH=%d nX=%d nY=%d\n",
2728 dxHotspot, dyHotspot, InternalDrag.dxHotspot, InternalDrag.dyHotspot);
2730 visible = InternalDrag.bShow;
2732 himlTemp = ImageList_Merge (InternalDrag.himlNoCursor, 0, himlDrag, iDrag,
2733 dxHotspot, dyHotspot);
2735 if (visible) {
2736 /* hide the drag image */
2737 ImageList_DragShowNolock(FALSE);
2739 if ((InternalDrag.himl->cx != himlTemp->cx) ||
2740 (InternalDrag.himl->cy != himlTemp->cy)) {
2741 /* the size of the drag image changed, invalidate the buffer */
2742 DeleteObject(InternalDrag.hbmBg);
2743 InternalDrag.hbmBg = 0;
2746 if (InternalDrag.himl != InternalDrag.himlNoCursor)
2747 ImageList_Destroy (InternalDrag.himl);
2748 InternalDrag.himl = himlTemp;
2750 if (visible) {
2751 /* show the drag image */
2752 ImageList_DragShowNolock(TRUE);
2755 return TRUE;
2759 /*************************************************************************
2760 * ImageList_SetFilter [COMCTL32.@]
2762 * Sets a filter (or does something completely different)!!???
2763 * It removes 12 Bytes from the stack (3 Parameters).
2765 * PARAMS
2766 * himl [I] SHOULD be a handle to image list
2767 * i [I] COULD be an index?
2768 * dwFilter [I] ???
2770 * RETURNS
2771 * Success: TRUE ???
2772 * Failure: FALSE ???
2774 * BUGS
2775 * This is an UNDOCUMENTED function!!!!
2776 * empty stub.
2779 BOOL WINAPI
2780 ImageList_SetFilter (HIMAGELIST himl, INT i, DWORD dwFilter)
2782 FIXME("(%p 0x%x 0x%x):empty stub!\n", himl, i, dwFilter);
2784 return FALSE;
2788 /*************************************************************************
2789 * ImageList_SetFlags [COMCTL32.@]
2791 * Sets the image list flags.
2793 * PARAMS
2794 * himl [I] Handle to image list
2795 * flags [I] Flags to set
2797 * RETURNS
2798 * Old flags?
2800 * BUGS
2801 * Stub.
2804 DWORD WINAPI
2805 ImageList_SetFlags(HIMAGELIST himl, DWORD flags)
2807 FIXME("(%p %08x):empty stub\n", himl, flags);
2808 return 0;
2812 /*************************************************************************
2813 * ImageList_SetIconSize [COMCTL32.@]
2815 * Sets the image size of the bitmap and deletes all images.
2817 * PARAMS
2818 * himl [I] handle to image list
2819 * cx [I] image width
2820 * cy [I] image height
2822 * RETURNS
2823 * Success: TRUE
2824 * Failure: FALSE
2827 BOOL WINAPI
2828 ImageList_SetIconSize (HIMAGELIST himl, INT cx, INT cy)
2830 INT nCount;
2831 HBITMAP hbmNew;
2833 if (!is_valid(himl))
2834 return FALSE;
2836 /* remove all images */
2837 himl->cMaxImage = himl->cInitial + 1;
2838 himl->cCurImage = 0;
2839 himl->cx = cx;
2840 himl->cy = cy;
2842 /* initialize overlay mask indices */
2843 for (nCount = 0; nCount < MAX_OVERLAYIMAGE; nCount++)
2844 himl->nOvlIdx[nCount] = -1;
2846 hbmNew = ImageList_CreateImage(himl->hdcImage, himl, himl->cMaxImage);
2847 SelectObject (himl->hdcImage, hbmNew);
2848 DeleteObject (himl->hbmImage);
2849 himl->hbmImage = hbmNew;
2851 if (himl->hbmMask) {
2852 SIZE sz;
2853 imagelist_get_bitmap_size(himl, himl->cMaxImage, &sz);
2854 hbmNew = CreateBitmap (sz.cx, sz.cy, 1, 1, NULL);
2855 SelectObject (himl->hdcMask, hbmNew);
2856 DeleteObject (himl->hbmMask);
2857 himl->hbmMask = hbmNew;
2860 return TRUE;
2864 /*************************************************************************
2865 * ImageList_SetImageCount [COMCTL32.@]
2867 * Resizes an image list to the specified number of images.
2869 * PARAMS
2870 * himl [I] handle to image list
2871 * iImageCount [I] number of images in the image list
2873 * RETURNS
2874 * Success: TRUE
2875 * Failure: FALSE
2878 BOOL WINAPI
2879 ImageList_SetImageCount (HIMAGELIST himl, UINT iImageCount)
2881 HDC hdcBitmap;
2882 HBITMAP hbmNewBitmap, hbmOld;
2883 INT nNewCount, nCopyCount;
2885 TRACE("%p %d\n",himl,iImageCount);
2887 if (!is_valid(himl))
2888 return FALSE;
2890 nNewCount = iImageCount + 1;
2891 nCopyCount = min(himl->cCurImage, iImageCount);
2893 hdcBitmap = CreateCompatibleDC (0);
2895 hbmNewBitmap = ImageList_CreateImage(hdcBitmap, himl, nNewCount);
2897 if (hbmNewBitmap != 0)
2899 hbmOld = SelectObject (hdcBitmap, hbmNewBitmap);
2900 imagelist_copy_images( himl, himl->hdcImage, hdcBitmap, 0, nCopyCount, 0 );
2901 SelectObject (hdcBitmap, hbmOld);
2903 /* FIXME: delete 'empty' image space? */
2905 SelectObject (himl->hdcImage, hbmNewBitmap);
2906 DeleteObject (himl->hbmImage);
2907 himl->hbmImage = hbmNewBitmap;
2909 else
2910 ERR("Could not create new image bitmap!\n");
2912 if (himl->hbmMask)
2914 SIZE sz;
2915 imagelist_get_bitmap_size( himl, nNewCount, &sz );
2916 hbmNewBitmap = CreateBitmap (sz.cx, sz.cy, 1, 1, NULL);
2917 if (hbmNewBitmap != 0)
2919 hbmOld = SelectObject (hdcBitmap, hbmNewBitmap);
2920 imagelist_copy_images( himl, himl->hdcMask, hdcBitmap, 0, nCopyCount, 0 );
2921 SelectObject (hdcBitmap, hbmOld);
2923 /* FIXME: delete 'empty' image space? */
2925 SelectObject (himl->hdcMask, hbmNewBitmap);
2926 DeleteObject (himl->hbmMask);
2927 himl->hbmMask = hbmNewBitmap;
2929 else
2930 ERR("Could not create new mask bitmap!\n");
2933 DeleteDC (hdcBitmap);
2935 if (himl->has_alpha)
2937 char *new_alpha = HeapReAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, himl->has_alpha, nNewCount );
2938 if (new_alpha) himl->has_alpha = new_alpha;
2939 else
2941 heap_free( himl->has_alpha );
2942 himl->has_alpha = NULL;
2946 /* Update max image count and current image count */
2947 himl->cMaxImage = nNewCount;
2948 himl->cCurImage = iImageCount;
2950 return TRUE;
2954 /*************************************************************************
2955 * ImageList_SetOverlayImage [COMCTL32.@]
2957 * Assigns an overlay mask index to an existing image in an image list.
2959 * PARAMS
2960 * himl [I] handle to image list
2961 * iImage [I] image index
2962 * iOverlay [I] overlay mask index
2964 * RETURNS
2965 * Success: TRUE
2966 * Failure: FALSE
2969 BOOL WINAPI
2970 ImageList_SetOverlayImage (HIMAGELIST himl, INT iImage, INT iOverlay)
2972 if (!is_valid(himl))
2973 return FALSE;
2974 if ((iOverlay < 1) || (iOverlay > MAX_OVERLAYIMAGE))
2975 return FALSE;
2976 if ((iImage!=-1) && ((iImage < 0) || (iImage > himl->cCurImage)))
2977 return FALSE;
2978 himl->nOvlIdx[iOverlay - 1] = iImage;
2979 return TRUE;
2984 /* helper for ImageList_Write - write bitmap to pstm
2985 * currently everything is written as 24 bit RGB, except masks
2987 static BOOL _write_bitmap(HBITMAP hBitmap, IStream *pstm)
2989 LPBITMAPFILEHEADER bmfh;
2990 LPBITMAPINFOHEADER bmih;
2991 LPBYTE data = NULL, lpBits;
2992 BITMAP bm;
2993 INT bitCount, sizeImage, offBits, totalSize;
2994 HDC xdc;
2995 BOOL result = FALSE;
2997 if (!GetObjectW(hBitmap, sizeof(BITMAP), &bm))
2998 return FALSE;
3000 bitCount = bm.bmBitsPixel;
3001 sizeImage = get_dib_stride(bm.bmWidth, bitCount) * bm.bmHeight;
3003 totalSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
3004 if(bitCount <= 8)
3005 totalSize += (1 << bitCount) * sizeof(RGBQUAD);
3006 offBits = totalSize;
3007 totalSize += sizeImage;
3009 data = heap_alloc_zero(totalSize);
3010 bmfh = (LPBITMAPFILEHEADER)data;
3011 bmih = (LPBITMAPINFOHEADER)(data + sizeof(BITMAPFILEHEADER));
3012 lpBits = data + offBits;
3014 /* setup BITMAPFILEHEADER */
3015 bmfh->bfType = (('M' << 8) | 'B');
3016 bmfh->bfSize = offBits;
3017 bmfh->bfReserved1 = 0;
3018 bmfh->bfReserved2 = 0;
3019 bmfh->bfOffBits = offBits;
3021 /* setup BITMAPINFOHEADER */
3022 bmih->biSize = sizeof(BITMAPINFOHEADER);
3023 bmih->biWidth = bm.bmWidth;
3024 bmih->biHeight = bm.bmHeight;
3025 bmih->biPlanes = 1;
3026 bmih->biBitCount = bitCount;
3027 bmih->biCompression = BI_RGB;
3028 bmih->biSizeImage = sizeImage;
3029 bmih->biXPelsPerMeter = 0;
3030 bmih->biYPelsPerMeter = 0;
3031 bmih->biClrUsed = 0;
3032 bmih->biClrImportant = 0;
3034 xdc = GetDC(0);
3035 result = GetDIBits(xdc, hBitmap, 0, bm.bmHeight, lpBits, (BITMAPINFO *)bmih, DIB_RGB_COLORS) == bm.bmHeight;
3036 ReleaseDC(0, xdc);
3037 if (!result)
3038 goto failed;
3040 TRACE("width %u, height %u, planes %u, bpp %u\n",
3041 bmih->biWidth, bmih->biHeight,
3042 bmih->biPlanes, bmih->biBitCount);
3044 if(FAILED(IStream_Write(pstm, data, totalSize, NULL)))
3045 goto failed;
3047 result = TRUE;
3049 failed:
3050 heap_free(data);
3052 return result;
3056 /*************************************************************************
3057 * ImageList_Write [COMCTL32.@]
3059 * Writes an image list to a stream.
3061 * PARAMS
3062 * himl [I] handle to image list
3063 * pstm [O] Pointer to a stream.
3065 * RETURNS
3066 * Success: TRUE
3067 * Failure: FALSE
3069 * BUGS
3070 * probably.
3073 BOOL WINAPI ImageList_Write(HIMAGELIST himl, IStream *pstm)
3075 ILHEAD ilHead;
3076 int i;
3078 TRACE("%p %p\n", himl, pstm);
3080 if (!is_valid(himl))
3081 return FALSE;
3083 ilHead.usMagic = (('L' << 8) | 'I');
3084 ilHead.usVersion = 0x101;
3085 ilHead.cCurImage = himl->cCurImage;
3086 ilHead.cMaxImage = himl->cMaxImage;
3087 ilHead.cGrow = himl->cGrow;
3088 ilHead.cx = himl->cx;
3089 ilHead.cy = himl->cy;
3090 ilHead.bkcolor = himl->clrBk;
3091 ilHead.flags = himl->flags;
3092 for(i = 0; i < 4; i++) {
3093 ilHead.ovls[i] = himl->nOvlIdx[i];
3096 TRACE("cx %u, cy %u, flags 0x04%x, cCurImage %u, cMaxImage %u\n",
3097 ilHead.cx, ilHead.cy, ilHead.flags, ilHead.cCurImage, ilHead.cMaxImage);
3099 if(FAILED(IStream_Write(pstm, &ilHead, sizeof(ILHEAD), NULL)))
3100 return FALSE;
3102 /* write the bitmap */
3103 if(!_write_bitmap(himl->hbmImage, pstm))
3104 return FALSE;
3106 /* write the mask if we have one */
3107 if(himl->flags & ILC_MASK) {
3108 if(!_write_bitmap(himl->hbmMask, pstm))
3109 return FALSE;
3112 return TRUE;
3116 static HBITMAP ImageList_CreateImage(HDC hdc, HIMAGELIST himl, UINT count)
3118 HBITMAP hbmNewBitmap;
3119 UINT ilc = (himl->flags & 0xFE);
3120 SIZE sz;
3122 imagelist_get_bitmap_size( himl, count, &sz );
3124 if ((ilc >= ILC_COLOR4 && ilc <= ILC_COLOR32) || ilc == ILC_COLOR)
3126 char buffer[sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD)];
3127 BITMAPINFO *bmi = (BITMAPINFO *)buffer;
3129 TRACE("Creating DIBSection %d x %d, %d Bits per Pixel\n",
3130 sz.cx, sz.cy, himl->uBitsPixel);
3132 memset( buffer, 0, sizeof(buffer) );
3133 bmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
3134 bmi->bmiHeader.biWidth = sz.cx;
3135 bmi->bmiHeader.biHeight = sz.cy;
3136 bmi->bmiHeader.biPlanes = 1;
3137 bmi->bmiHeader.biBitCount = himl->uBitsPixel;
3138 bmi->bmiHeader.biCompression = BI_RGB;
3140 if (himl->uBitsPixel <= ILC_COLOR8)
3142 if (!himl->color_table_set)
3144 /* retrieve the default color map */
3145 HBITMAP tmp = CreateBitmap( 1, 1, 1, 1, NULL );
3146 GetDIBits( hdc, tmp, 0, 0, NULL, bmi, DIB_RGB_COLORS );
3147 DeleteObject( tmp );
3148 if (ilc == ILC_COLOR4)
3150 RGBQUAD tmp;
3151 tmp = bmi->bmiColors[7];
3152 bmi->bmiColors[7] = bmi->bmiColors[8];
3153 bmi->bmiColors[8] = tmp;
3156 else
3158 GetDIBColorTable(himl->hdcImage, 0, 1 << himl->uBitsPixel, bmi->bmiColors);
3161 hbmNewBitmap = CreateDIBSection(hdc, bmi, DIB_RGB_COLORS, NULL, 0, 0);
3163 else /*if (ilc == ILC_COLORDDB)*/
3165 TRACE("Creating Bitmap: %d Bits per Pixel\n", himl->uBitsPixel);
3167 hbmNewBitmap = CreateBitmap (sz.cx, sz.cy, 1, himl->uBitsPixel, NULL);
3169 TRACE("returning %p\n", hbmNewBitmap);
3170 return hbmNewBitmap;
3173 /*************************************************************************
3174 * ImageList_SetColorTable [COMCTL32.@]
3176 * Sets the color table of an image list.
3178 * PARAMS
3179 * himl [I] Handle to the image list.
3180 * uStartIndex [I] The first index to set.
3181 * cEntries [I] Number of entries to set.
3182 * prgb [I] New color information for color table for the image list.
3184 * RETURNS
3185 * Success: Number of entries in the table that were set.
3186 * Failure: Zero.
3188 * SEE
3189 * ImageList_Create(), SetDIBColorTable()
3192 UINT WINAPI
3193 ImageList_SetColorTable(HIMAGELIST himl, UINT uStartIndex, UINT cEntries, const RGBQUAD *prgb)
3195 TRACE("(%p, %d, %d, %p)\n", himl, uStartIndex, cEntries, prgb);
3196 himl->color_table_set = TRUE;
3197 return SetDIBColorTable(himl->hdcImage, uStartIndex, cEntries, prgb);
3200 /*************************************************************************
3201 * ImageList_CoCreateInstance [COMCTL32.@]
3203 * Creates a new imagelist instance and returns an interface pointer to it.
3205 * PARAMS
3206 * rclsid [I] A reference to the CLSID (CLSID_ImageList).
3207 * punkOuter [I] Pointer to IUnknown interface for aggregation, if desired
3208 * riid [I] Identifier of the requested interface.
3209 * ppv [O] Returns the address of the pointer requested, or NULL.
3211 * RETURNS
3212 * Success: S_OK.
3213 * Failure: Error value.
3215 HRESULT WINAPI
3216 ImageList_CoCreateInstance (REFCLSID rclsid, const IUnknown *punkOuter, REFIID riid, void **ppv)
3218 TRACE("(%s,%p,%s,%p)\n", debugstr_guid(rclsid), punkOuter, debugstr_guid(riid), ppv);
3220 if (!IsEqualCLSID(&CLSID_ImageList, rclsid))
3221 return E_NOINTERFACE;
3223 return ImageListImpl_CreateInstance(punkOuter, riid, ppv);
3227 /*************************************************************************
3228 * IImageList implementation
3231 static HRESULT WINAPI ImageListImpl_QueryInterface(IImageList2 *iface,
3232 REFIID iid, void **ppv)
3234 HIMAGELIST imgl = impl_from_IImageList2(iface);
3236 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
3238 if (!ppv) return E_INVALIDARG;
3240 if (IsEqualIID(&IID_IUnknown, iid) ||
3241 IsEqualIID(&IID_IImageList, iid) ||
3242 IsEqualIID(&IID_IImageList2, iid))
3244 *ppv = &imgl->IImageList2_iface;
3246 else
3248 *ppv = NULL;
3249 return E_NOINTERFACE;
3252 IImageList2_AddRef(iface);
3253 return S_OK;
3256 static ULONG WINAPI ImageListImpl_AddRef(IImageList2 *iface)
3258 HIMAGELIST imgl = impl_from_IImageList2(iface);
3259 ULONG ref = InterlockedIncrement(&imgl->ref);
3261 TRACE("(%p) refcount=%u\n", iface, ref);
3262 return ref;
3265 static ULONG WINAPI ImageListImpl_Release(IImageList2 *iface)
3267 HIMAGELIST This = impl_from_IImageList2(iface);
3268 ULONG ref = InterlockedDecrement(&This->ref);
3270 TRACE("(%p) refcount=%u\n", iface, ref);
3272 if (ref == 0)
3274 /* delete image bitmaps */
3275 if (This->hbmImage) DeleteObject (This->hbmImage);
3276 if (This->hbmMask) DeleteObject (This->hbmMask);
3278 /* delete image & mask DCs */
3279 if (This->hdcImage) DeleteDC (This->hdcImage);
3280 if (This->hdcMask) DeleteDC (This->hdcMask);
3282 /* delete blending brushes */
3283 if (This->hbrBlend25) DeleteObject (This->hbrBlend25);
3284 if (This->hbrBlend50) DeleteObject (This->hbrBlend50);
3286 This->IImageList2_iface.lpVtbl = NULL;
3287 heap_free(This->has_alpha);
3288 heap_free(This);
3291 return ref;
3294 static HRESULT WINAPI ImageListImpl_Add(IImageList2 *iface, HBITMAP hbmImage,
3295 HBITMAP hbmMask, int *pi)
3297 HIMAGELIST imgl = impl_from_IImageList2(iface);
3298 int ret;
3300 if (!pi)
3301 return E_FAIL;
3303 ret = ImageList_Add(imgl, hbmImage, hbmMask);
3305 if (ret == -1)
3306 return E_FAIL;
3308 *pi = ret;
3309 return S_OK;
3312 static HRESULT WINAPI ImageListImpl_ReplaceIcon(IImageList2 *iface, int i,
3313 HICON hicon, int *pi)
3315 HIMAGELIST imgl = impl_from_IImageList2(iface);
3316 int ret;
3318 if (!pi)
3319 return E_FAIL;
3321 ret = ImageList_ReplaceIcon(imgl, i, hicon);
3323 if (ret == -1)
3324 return E_FAIL;
3326 *pi = ret;
3327 return S_OK;
3330 static HRESULT WINAPI ImageListImpl_SetOverlayImage(IImageList2 *iface,
3331 int iImage, int iOverlay)
3333 HIMAGELIST imgl = impl_from_IImageList2(iface);
3334 return ImageList_SetOverlayImage(imgl, iImage, iOverlay) ? S_OK : E_FAIL;
3337 static HRESULT WINAPI ImageListImpl_Replace(IImageList2 *iface, int i,
3338 HBITMAP hbmImage, HBITMAP hbmMask)
3340 HIMAGELIST imgl = impl_from_IImageList2(iface);
3341 return ImageList_Replace(imgl, i, hbmImage, hbmMask) ? S_OK : E_FAIL;
3344 static HRESULT WINAPI ImageListImpl_AddMasked(IImageList2 *iface, HBITMAP hbmImage,
3345 COLORREF crMask, int *pi)
3347 HIMAGELIST imgl = impl_from_IImageList2(iface);
3348 int ret;
3350 if (!pi)
3351 return E_FAIL;
3353 ret = ImageList_AddMasked(imgl, hbmImage, crMask);
3355 if (ret == -1)
3356 return E_FAIL;
3358 *pi = ret;
3359 return S_OK;
3362 static HRESULT WINAPI ImageListImpl_Draw(IImageList2 *iface,
3363 IMAGELISTDRAWPARAMS *pimldp)
3365 HIMAGELIST imgl = impl_from_IImageList2(iface);
3366 HIMAGELIST old_himl;
3367 int ret;
3369 /* As far as I can tell, Windows simply ignores the contents of pimldp->himl
3370 so we shall simulate the same */
3371 old_himl = pimldp->himl;
3372 pimldp->himl = imgl;
3374 ret = ImageList_DrawIndirect(pimldp);
3376 pimldp->himl = old_himl;
3377 return ret ? S_OK : E_INVALIDARG;
3380 static HRESULT WINAPI ImageListImpl_Remove(IImageList2 *iface, int i)
3382 HIMAGELIST imgl = impl_from_IImageList2(iface);
3383 return (ImageList_Remove(imgl, i) == 0) ? E_INVALIDARG : S_OK;
3386 static HRESULT WINAPI ImageListImpl_GetIcon(IImageList2 *iface, int i, UINT flags,
3387 HICON *picon)
3389 HIMAGELIST imgl = impl_from_IImageList2(iface);
3390 HICON hIcon;
3392 if (!picon)
3393 return E_FAIL;
3395 hIcon = ImageList_GetIcon(imgl, i, flags);
3397 if (hIcon == NULL)
3398 return E_FAIL;
3400 *picon = hIcon;
3401 return S_OK;
3404 static HRESULT WINAPI ImageListImpl_GetImageInfo(IImageList2 *iface, int i,
3405 IMAGEINFO *pImageInfo)
3407 HIMAGELIST imgl = impl_from_IImageList2(iface);
3408 return ImageList_GetImageInfo(imgl, i, pImageInfo) ? S_OK : E_FAIL;
3411 static HRESULT WINAPI ImageListImpl_Copy(IImageList2 *iface, int dst_index,
3412 IUnknown *unk_src, int src_index, UINT flags)
3414 HIMAGELIST imgl = impl_from_IImageList2(iface);
3415 IImageList *src = NULL;
3416 HRESULT ret;
3418 if (!unk_src)
3419 return E_FAIL;
3421 /* TODO: Add test for IID_ImageList2 too */
3422 if (FAILED(IUnknown_QueryInterface(unk_src, &IID_IImageList,
3423 (void **) &src)))
3424 return E_FAIL;
3426 if (ImageList_Copy(imgl, dst_index, (HIMAGELIST) src, src_index, flags))
3427 ret = S_OK;
3428 else
3429 ret = E_FAIL;
3431 IImageList_Release(src);
3432 return ret;
3435 static HRESULT WINAPI ImageListImpl_Merge(IImageList2 *iface, int i1,
3436 IUnknown *punk2, int i2, int dx, int dy, REFIID riid, void **ppv)
3438 HIMAGELIST imgl = impl_from_IImageList2(iface);
3439 IImageList *iml2 = NULL;
3440 HIMAGELIST merged;
3441 HRESULT ret = E_FAIL;
3443 TRACE("(%p)->(%d %p %d %d %d %s %p)\n", iface, i1, punk2, i2, dx, dy, debugstr_guid(riid), ppv);
3445 /* TODO: Add test for IID_ImageList2 too */
3446 if (FAILED(IUnknown_QueryInterface(punk2, &IID_IImageList,
3447 (void **) &iml2)))
3448 return E_FAIL;
3450 merged = ImageList_Merge(imgl, i1, (HIMAGELIST) iml2, i2, dx, dy);
3452 /* Get the interface for the new image list */
3453 if (merged)
3455 ret = HIMAGELIST_QueryInterface(merged, riid, ppv);
3456 ImageList_Destroy(merged);
3459 IImageList_Release(iml2);
3460 return ret;
3463 static HRESULT WINAPI ImageListImpl_Clone(IImageList2 *iface, REFIID riid, void **ppv)
3465 HIMAGELIST imgl = impl_from_IImageList2(iface);
3466 HIMAGELIST clone;
3467 HRESULT ret = E_FAIL;
3469 TRACE("(%p)->(%s %p)\n", iface, debugstr_guid(riid), ppv);
3471 clone = ImageList_Duplicate(imgl);
3473 /* Get the interface for the new image list */
3474 if (clone)
3476 ret = HIMAGELIST_QueryInterface(clone, riid, ppv);
3477 ImageList_Destroy(clone);
3480 return ret;
3483 static HRESULT WINAPI ImageListImpl_GetImageRect(IImageList2 *iface, int i,
3484 RECT *prc)
3486 HIMAGELIST imgl = impl_from_IImageList2(iface);
3487 IMAGEINFO info;
3489 if (!prc)
3490 return E_FAIL;
3492 if (!ImageList_GetImageInfo(imgl, i, &info))
3493 return E_FAIL;
3495 *prc = info.rcImage;
3497 return S_OK;
3500 static HRESULT WINAPI ImageListImpl_GetIconSize(IImageList2 *iface, int *cx,
3501 int *cy)
3503 HIMAGELIST imgl = impl_from_IImageList2(iface);
3504 return ImageList_GetIconSize(imgl, cx, cy) ? S_OK : E_INVALIDARG;
3507 static HRESULT WINAPI ImageListImpl_SetIconSize(IImageList2 *iface, int cx,
3508 int cy)
3510 HIMAGELIST imgl = impl_from_IImageList2(iface);
3511 return ImageList_SetIconSize(imgl, cx, cy) ? S_OK : E_FAIL;
3514 static HRESULT WINAPI ImageListImpl_GetImageCount(IImageList2 *iface, int *pi)
3516 HIMAGELIST imgl = impl_from_IImageList2(iface);
3517 *pi = ImageList_GetImageCount(imgl);
3518 return S_OK;
3521 static HRESULT WINAPI ImageListImpl_SetImageCount(IImageList2 *iface, UINT count)
3523 HIMAGELIST imgl = impl_from_IImageList2(iface);
3524 return ImageList_SetImageCount(imgl, count) ? S_OK : E_FAIL;
3527 static HRESULT WINAPI ImageListImpl_SetBkColor(IImageList2 *iface, COLORREF clrBk,
3528 COLORREF *pclr)
3530 HIMAGELIST imgl = impl_from_IImageList2(iface);
3531 *pclr = ImageList_SetBkColor(imgl, clrBk);
3532 return S_OK;
3535 static HRESULT WINAPI ImageListImpl_GetBkColor(IImageList2 *iface, COLORREF *pclr)
3537 HIMAGELIST imgl = impl_from_IImageList2(iface);
3538 *pclr = ImageList_GetBkColor(imgl);
3539 return S_OK;
3542 static HRESULT WINAPI ImageListImpl_BeginDrag(IImageList2 *iface, int iTrack,
3543 int dxHotspot, int dyHotspot)
3545 HIMAGELIST imgl = impl_from_IImageList2(iface);
3546 return ImageList_BeginDrag(imgl, iTrack, dxHotspot, dyHotspot) ? S_OK : E_FAIL;
3549 static HRESULT WINAPI ImageListImpl_EndDrag(IImageList2 *iface)
3551 ImageList_EndDrag();
3552 return S_OK;
3555 static HRESULT WINAPI ImageListImpl_DragEnter(IImageList2 *iface, HWND hwndLock,
3556 int x, int y)
3558 return ImageList_DragEnter(hwndLock, x, y) ? S_OK : E_FAIL;
3561 static HRESULT WINAPI ImageListImpl_DragLeave(IImageList2 *iface, HWND hwndLock)
3563 return ImageList_DragLeave(hwndLock) ? S_OK : E_FAIL;
3566 static HRESULT WINAPI ImageListImpl_DragMove(IImageList2 *iface, int x, int y)
3568 return ImageList_DragMove(x, y) ? S_OK : E_FAIL;
3571 static HRESULT WINAPI ImageListImpl_SetDragCursorImage(IImageList2 *iface,
3572 IUnknown *punk, int iDrag, int dxHotspot, int dyHotspot)
3574 IImageList *iml2 = NULL;
3575 BOOL ret;
3577 if (!punk)
3578 return E_FAIL;
3580 /* TODO: Add test for IID_ImageList2 too */
3581 if (FAILED(IUnknown_QueryInterface(punk, &IID_IImageList,
3582 (void **) &iml2)))
3583 return E_FAIL;
3585 ret = ImageList_SetDragCursorImage((HIMAGELIST) iml2, iDrag, dxHotspot,
3586 dyHotspot);
3588 IImageList_Release(iml2);
3590 return ret ? S_OK : E_FAIL;
3593 static HRESULT WINAPI ImageListImpl_DragShowNolock(IImageList2 *iface, BOOL fShow)
3595 return ImageList_DragShowNolock(fShow) ? S_OK : E_FAIL;
3598 static HRESULT WINAPI ImageListImpl_GetDragImage(IImageList2 *iface, POINT *ppt,
3599 POINT *pptHotspot, REFIID riid, PVOID *ppv)
3601 HRESULT ret = E_FAIL;
3602 HIMAGELIST hNew;
3604 if (!ppv)
3605 return E_FAIL;
3607 hNew = ImageList_GetDragImage(ppt, pptHotspot);
3609 /* Get the interface for the new image list */
3610 if (hNew)
3612 IImageList *idrag = (IImageList*)hNew;
3614 ret = HIMAGELIST_QueryInterface(hNew, riid, ppv);
3615 IImageList_Release(idrag);
3618 return ret;
3621 static HRESULT WINAPI ImageListImpl_GetItemFlags(IImageList2 *iface, int i,
3622 DWORD *dwFlags)
3624 FIXME("STUB: %p %d %p\n", iface, i, dwFlags);
3625 return E_NOTIMPL;
3628 static HRESULT WINAPI ImageListImpl_GetOverlayImage(IImageList2 *iface, int iOverlay,
3629 int *piIndex)
3631 HIMAGELIST This = impl_from_IImageList2(iface);
3632 int i;
3634 if ((iOverlay < 0) || (iOverlay > This->cCurImage))
3635 return E_FAIL;
3637 for (i = 0; i < MAX_OVERLAYIMAGE; i++)
3639 if (This->nOvlIdx[i] == iOverlay)
3641 *piIndex = i + 1;
3642 return S_OK;
3646 return E_FAIL;
3649 static HRESULT WINAPI ImageListImpl_Resize(IImageList2 *iface, INT cx, INT cy)
3651 FIXME("(%p)->(%d %d): stub\n", iface, cx, cy);
3652 return E_NOTIMPL;
3655 static HRESULT WINAPI ImageListImpl_GetOriginalSize(IImageList2 *iface, INT image, DWORD flags, INT *cx, INT *cy)
3657 FIXME("(%p)->(%d %x %p %p): stub\n", iface, image, flags, cx, cy);
3658 return E_NOTIMPL;
3661 static HRESULT WINAPI ImageListImpl_SetOriginalSize(IImageList2 *iface, INT image, INT cx, INT cy)
3663 FIXME("(%p)->(%d %d %d): stub\n", iface, image, cx, cy);
3664 return E_NOTIMPL;
3667 static HRESULT WINAPI ImageListImpl_SetCallback(IImageList2 *iface, IUnknown *callback)
3669 FIXME("(%p)->(%p): stub\n", iface, callback);
3670 return E_NOTIMPL;
3673 static HRESULT WINAPI ImageListImpl_GetCallback(IImageList2 *iface, REFIID riid, void **ppv)
3675 FIXME("(%p)->(%s %p): stub\n", iface, debugstr_guid(riid), ppv);
3676 return E_NOTIMPL;
3679 static HRESULT WINAPI ImageListImpl_ForceImagePresent(IImageList2 *iface, INT image, DWORD flags)
3681 FIXME("(%p)->(%d %x): stub\n", iface, image, flags);
3682 return E_NOTIMPL;
3685 static HRESULT WINAPI ImageListImpl_DiscardImages(IImageList2 *iface, INT first_image, INT last_image, DWORD flags)
3687 FIXME("(%p)->(%d %d %x): stub\n", iface, first_image, last_image, flags);
3688 return E_NOTIMPL;
3691 static HRESULT WINAPI ImageListImpl_PreloadImages(IImageList2 *iface, IMAGELISTDRAWPARAMS *params)
3693 FIXME("(%p)->(%p): stub\n", iface, params);
3694 return E_NOTIMPL;
3697 static HRESULT WINAPI ImageListImpl_GetStatistics(IImageList2 *iface, IMAGELISTSTATS *stats)
3699 FIXME("(%p)->(%p): stub\n", iface, stats);
3700 return E_NOTIMPL;
3703 static HRESULT WINAPI ImageListImpl_Initialize(IImageList2 *iface, INT cx, INT cy, UINT flags, INT initial, INT grow)
3705 FIXME("(%p)->(%d %d %d %d %d): stub\n", iface, cx, cy, flags, initial, grow);
3706 return E_NOTIMPL;
3709 static HRESULT WINAPI ImageListImpl_Replace2(IImageList2 *iface, INT i, HBITMAP image, HBITMAP mask, IUnknown *unk, DWORD flags)
3711 FIXME("(%p)->(%d %p %p %p %x): stub\n", iface, i, image, mask, unk, flags);
3712 return E_NOTIMPL;
3715 static HRESULT WINAPI ImageListImpl_ReplaceFromImageList(IImageList2 *iface, INT i, IImageList *imagelist, INT src,
3716 IUnknown *unk, DWORD flags)
3718 FIXME("(%p)->(%d %p %d %p %x): stub\n", iface, i, imagelist, src, unk, flags);
3719 return E_NOTIMPL;
3722 static const IImageList2Vtbl ImageListImpl_Vtbl = {
3723 ImageListImpl_QueryInterface,
3724 ImageListImpl_AddRef,
3725 ImageListImpl_Release,
3726 ImageListImpl_Add,
3727 ImageListImpl_ReplaceIcon,
3728 ImageListImpl_SetOverlayImage,
3729 ImageListImpl_Replace,
3730 ImageListImpl_AddMasked,
3731 ImageListImpl_Draw,
3732 ImageListImpl_Remove,
3733 ImageListImpl_GetIcon,
3734 ImageListImpl_GetImageInfo,
3735 ImageListImpl_Copy,
3736 ImageListImpl_Merge,
3737 ImageListImpl_Clone,
3738 ImageListImpl_GetImageRect,
3739 ImageListImpl_GetIconSize,
3740 ImageListImpl_SetIconSize,
3741 ImageListImpl_GetImageCount,
3742 ImageListImpl_SetImageCount,
3743 ImageListImpl_SetBkColor,
3744 ImageListImpl_GetBkColor,
3745 ImageListImpl_BeginDrag,
3746 ImageListImpl_EndDrag,
3747 ImageListImpl_DragEnter,
3748 ImageListImpl_DragLeave,
3749 ImageListImpl_DragMove,
3750 ImageListImpl_SetDragCursorImage,
3751 ImageListImpl_DragShowNolock,
3752 ImageListImpl_GetDragImage,
3753 ImageListImpl_GetItemFlags,
3754 ImageListImpl_GetOverlayImage,
3755 ImageListImpl_Resize,
3756 ImageListImpl_GetOriginalSize,
3757 ImageListImpl_SetOriginalSize,
3758 ImageListImpl_SetCallback,
3759 ImageListImpl_GetCallback,
3760 ImageListImpl_ForceImagePresent,
3761 ImageListImpl_DiscardImages,
3762 ImageListImpl_PreloadImages,
3763 ImageListImpl_GetStatistics,
3764 ImageListImpl_Initialize,
3765 ImageListImpl_Replace2,
3766 ImageListImpl_ReplaceFromImageList
3769 static BOOL is_valid(HIMAGELIST himl)
3771 BOOL valid;
3772 __TRY
3774 valid = himl && himl->IImageList2_iface.lpVtbl == &ImageListImpl_Vtbl;
3776 __EXCEPT_PAGE_FAULT
3778 valid = FALSE;
3780 __ENDTRY
3781 return valid;
3784 /*************************************************************************
3785 * HIMAGELIST_QueryInterface [COMCTL32.@]
3787 * Returns a pointer to an IImageList or IImageList2 object for the given
3788 * HIMAGELIST.
3790 * PARAMS
3791 * himl [I] Image list handle.
3792 * riid [I] Identifier of the requested interface.
3793 * ppv [O] Returns the address of the pointer requested, or NULL.
3795 * RETURNS
3796 * Success: S_OK.
3797 * Failure: Error value.
3799 HRESULT WINAPI
3800 HIMAGELIST_QueryInterface (HIMAGELIST himl, REFIID riid, void **ppv)
3802 TRACE("(%p,%s,%p)\n", himl, debugstr_guid(riid), ppv);
3803 return IImageList2_QueryInterface((IImageList2 *) himl, riid, ppv);
3806 static HRESULT ImageListImpl_CreateInstance(const IUnknown *pUnkOuter, REFIID iid, void** ppv)
3808 HIMAGELIST This;
3809 HRESULT ret;
3811 TRACE("(%p,%s,%p)\n", pUnkOuter, debugstr_guid(iid), ppv);
3813 *ppv = NULL;
3815 if (pUnkOuter) return CLASS_E_NOAGGREGATION;
3817 This = heap_alloc_zero(sizeof(struct _IMAGELIST));
3818 if (!This) return E_OUTOFMEMORY;
3820 This->IImageList2_iface.lpVtbl = &ImageListImpl_Vtbl;
3821 This->ref = 1;
3823 ret = IImageList2_QueryInterface(&This->IImageList2_iface, iid, ppv);
3824 IImageList2_Release(&This->IImageList2_iface);
3826 return ret;