msvcirt: Add implementation of streambuf::sputbackc.
[wine.git] / dlls / comctl32 / imagelist.c
blobc915472e17896ab9a9b1466c3778ec96917ec61a
1 /*
2 * ImageList implementation
4 * Copyright 1998 Eric Kohl
5 * Copyright 2000 Jason Mawdsley
6 * Copyright 2001, 2004 Michael Stefaniuc
7 * Copyright 2001 Charles Loep for CodeWeavers
8 * Copyright 2002 Dimitrie O. Paun
9 * Copyright 2009 Owen Rudge for CodeWeavers
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 * NOTE
27 * This code was audited for completeness against the documented features
28 * of Comctl32.dll version 6.0 on Sep. 12, 2002, by Dimitrie O. Paun.
30 * Unless otherwise noted, we believe this code to be complete, as per
31 * the specification mentioned above.
32 * If you discover missing features, or bugs, please note them below.
34 * TODO:
35 * - Add support for ILD_PRESERVEALPHA, ILD_SCALE, ILD_DPISCALE
36 * - Add support for ILS_GLOW, ILS_SHADOW, ILS_SATURATE
37 * - Thread-safe locking
40 #include <stdarg.h>
41 #include <stdlib.h>
42 #include <string.h>
44 #define COBJMACROS
46 #include "winerror.h"
47 #include "windef.h"
48 #include "winbase.h"
49 #include "objbase.h"
50 #include "wingdi.h"
51 #include "winuser.h"
52 #include "commctrl.h"
53 #include "comctl32.h"
54 #include "commoncontrols.h"
55 #include "wine/debug.h"
56 #include "wine/exception.h"
58 WINE_DEFAULT_DEBUG_CHANNEL(imagelist);
60 #define MAX_OVERLAYIMAGE 15
62 struct _IMAGELIST
64 IImageList2 IImageList2_iface; /* 00: IImageList vtable */
65 INT cCurImage; /* 04: ImageCount */
66 INT cMaxImage; /* 08: maximages */
67 INT cGrow; /* 0C: cGrow */
68 INT cx; /* 10: cx */
69 INT cy; /* 14: cy */
70 DWORD x4;
71 UINT flags; /* 1C: flags */
72 COLORREF clrFg; /* 20: foreground color */
73 COLORREF clrBk; /* 24: background color */
76 HBITMAP hbmImage; /* 28: images Bitmap */
77 HBITMAP hbmMask; /* 2C: masks Bitmap */
78 HDC hdcImage; /* 30: images MemDC */
79 HDC hdcMask; /* 34: masks MemDC */
80 INT nOvlIdx[MAX_OVERLAYIMAGE]; /* 38: overlay images index */
82 /* not yet found out */
83 HBRUSH hbrBlend25;
84 HBRUSH hbrBlend50;
85 INT cInitial;
86 UINT uBitsPixel;
87 char *has_alpha;
89 LONG ref; /* reference count */
92 #define IMAGELIST_MAGIC 0x53414D58
94 /* Header used by ImageList_Read() and ImageList_Write() */
95 #include "pshpack2.h"
96 typedef struct _ILHEAD
98 USHORT usMagic;
99 USHORT usVersion;
100 WORD cCurImage;
101 WORD cMaxImage;
102 WORD cGrow;
103 WORD cx;
104 WORD cy;
105 COLORREF bkcolor;
106 WORD flags;
107 SHORT ovls[4];
108 } ILHEAD;
109 #include "poppack.h"
111 /* internal image list data used for Drag & Drop operations */
112 typedef struct
114 HWND hwnd;
115 HIMAGELIST himl;
116 HIMAGELIST himlNoCursor;
117 /* position of the drag image relative to the window */
118 INT x;
119 INT y;
120 /* offset of the hotspot relative to the origin of the image */
121 INT dxHotspot;
122 INT dyHotspot;
123 /* is the drag image visible */
124 BOOL bShow;
125 /* saved background */
126 HBITMAP hbmBg;
127 } INTERNALDRAG;
129 static INTERNALDRAG InternalDrag = { 0, 0, 0, 0, 0, 0, 0, FALSE, 0 };
131 static inline HIMAGELIST impl_from_IImageList2(IImageList2 *iface)
133 return CONTAINING_RECORD(iface, struct _IMAGELIST, IImageList2_iface);
136 static HBITMAP ImageList_CreateImage(HDC hdc, HIMAGELIST himl, UINT count);
137 static HRESULT ImageListImpl_CreateInstance(const IUnknown *pUnkOuter, REFIID iid, void** ppv);
138 static BOOL is_valid(HIMAGELIST himl);
141 * An imagelist with N images is tiled like this:
143 * N/4 ->
145 * 4 048C..
146 * 159D..
147 * | 26AE.N
148 * V 37BF.
151 #define TILE_COUNT 4
153 static inline UINT imagelist_height( UINT count )
155 return ((count + TILE_COUNT - 1)/TILE_COUNT);
158 static inline void imagelist_point_from_index( HIMAGELIST himl, UINT index, LPPOINT pt )
160 pt->x = (index%TILE_COUNT) * himl->cx;
161 pt->y = (index/TILE_COUNT) * himl->cy;
164 static inline void imagelist_get_bitmap_size( HIMAGELIST himl, UINT count, SIZE *sz )
166 sz->cx = himl->cx * TILE_COUNT;
167 sz->cy = imagelist_height( count ) * himl->cy;
170 static inline int get_dib_stride( int width, int bpp )
172 return ((width * bpp + 31) >> 3) & ~3;
175 static inline int get_dib_image_size( const BITMAPINFO *info )
177 return get_dib_stride( info->bmiHeader.biWidth, info->bmiHeader.biBitCount )
178 * abs( info->bmiHeader.biHeight );
182 * imagelist_copy_images()
184 * Copies a block of count images from offset src in the list to offset dest.
185 * Images are copied a row at at time. Assumes hdcSrc and hdcDest are different.
187 static inline void imagelist_copy_images( HIMAGELIST himl, HDC hdcSrc, HDC hdcDest,
188 UINT src, UINT count, UINT dest )
190 POINT ptSrc, ptDest;
191 SIZE sz;
192 UINT i;
194 for ( i=0; i<TILE_COUNT; i++ )
196 imagelist_point_from_index( himl, src+i, &ptSrc );
197 imagelist_point_from_index( himl, dest+i, &ptDest );
198 sz.cx = himl->cx;
199 sz.cy = himl->cy * imagelist_height( count - i );
201 BitBlt( hdcDest, ptDest.x, ptDest.y, sz.cx, sz.cy,
202 hdcSrc, ptSrc.x, ptSrc.y, SRCCOPY );
206 static void add_dib_bits( HIMAGELIST himl, int pos, int count, int width, int height,
207 BITMAPINFO *info, BITMAPINFO *mask_info, DWORD *bits, BYTE *mask_bits )
209 int i, j, n;
210 POINT pt;
211 int stride = info->bmiHeader.biWidth;
212 int mask_stride = (info->bmiHeader.biWidth + 31) / 32 * 4;
214 for (n = 0; n < count; n++)
216 BOOL has_alpha = FALSE;
218 imagelist_point_from_index( himl, pos + n, &pt );
220 /* check if bitmap has an alpha channel */
221 for (i = 0; i < height && !has_alpha; i++)
222 for (j = n * width; j < (n + 1) * width; j++)
223 if ((has_alpha = ((bits[i * stride + j] & 0xff000000) != 0))) break;
225 if (!has_alpha) /* generate alpha channel from the mask */
227 for (i = 0; i < height; i++)
228 for (j = n * width; j < (n + 1) * width; j++)
229 if (!mask_info || !((mask_bits[i * mask_stride + j / 8] << (j % 8)) & 0x80))
230 bits[i * stride + j] |= 0xff000000;
231 else
232 bits[i * stride + j] = 0;
234 else
236 himl->has_alpha[pos + n] = 1;
238 if (mask_info && himl->hbmMask) /* generate the mask from the alpha channel */
240 for (i = 0; i < height; i++)
241 for (j = n * width; j < (n + 1) * width; j++)
242 if ((bits[i * stride + j] >> 24) > 25) /* more than 10% alpha */
243 mask_bits[i * mask_stride + j / 8] &= ~(0x80 >> (j % 8));
244 else
245 mask_bits[i * mask_stride + j / 8] |= 0x80 >> (j % 8);
248 StretchDIBits( himl->hdcImage, pt.x, pt.y, himl->cx, himl->cy,
249 n * width, 0, width, height, bits, info, DIB_RGB_COLORS, SRCCOPY );
250 if (mask_info)
251 StretchDIBits( himl->hdcMask, pt.x, pt.y, himl->cx, himl->cy,
252 n * width, 0, width, height, mask_bits, mask_info, DIB_RGB_COLORS, SRCCOPY );
256 /* add images with an alpha channel when the image list is 32 bpp */
257 static BOOL add_with_alpha( HIMAGELIST himl, HDC hdc, int pos, int count,
258 int width, int height, HBITMAP hbmImage, HBITMAP hbmMask )
260 BOOL ret = FALSE;
261 BITMAP bm;
262 BITMAPINFO *info, *mask_info = NULL;
263 DWORD *bits = NULL;
264 BYTE *mask_bits = NULL;
265 DWORD mask_width;
267 if (!GetObjectW( hbmImage, sizeof(bm), &bm )) return FALSE;
269 /* if either the imagelist or the source bitmap don't have an alpha channel, bail out now */
270 if (!himl->has_alpha) return FALSE;
271 if (bm.bmBitsPixel != 32) return FALSE;
273 SelectObject( hdc, hbmImage );
274 mask_width = (bm.bmWidth + 31) / 32 * 4;
276 if (!(info = HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET( BITMAPINFO, bmiColors[256] )))) goto done;
277 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
278 info->bmiHeader.biWidth = bm.bmWidth;
279 info->bmiHeader.biHeight = -height;
280 info->bmiHeader.biPlanes = 1;
281 info->bmiHeader.biBitCount = 32;
282 info->bmiHeader.biCompression = BI_RGB;
283 info->bmiHeader.biSizeImage = bm.bmWidth * height * 4;
284 info->bmiHeader.biXPelsPerMeter = 0;
285 info->bmiHeader.biYPelsPerMeter = 0;
286 info->bmiHeader.biClrUsed = 0;
287 info->bmiHeader.biClrImportant = 0;
288 if (!(bits = HeapAlloc( GetProcessHeap(), 0, info->bmiHeader.biSizeImage ))) goto done;
289 if (!GetDIBits( hdc, hbmImage, 0, height, bits, info, DIB_RGB_COLORS )) goto done;
291 if (hbmMask)
293 if (!(mask_info = HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET( BITMAPINFO, bmiColors[2] ))))
294 goto done;
295 mask_info->bmiHeader = info->bmiHeader;
296 mask_info->bmiHeader.biBitCount = 1;
297 mask_info->bmiHeader.biSizeImage = mask_width * height;
298 if (!(mask_bits = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, info->bmiHeader.biSizeImage )))
299 goto done;
300 if (!GetDIBits( hdc, hbmMask, 0, height, mask_bits, mask_info, DIB_RGB_COLORS )) goto done;
303 add_dib_bits( himl, pos, count, width, height, info, mask_info, bits, mask_bits );
304 ret = TRUE;
306 done:
307 HeapFree( GetProcessHeap(), 0, info );
308 HeapFree( GetProcessHeap(), 0, mask_info );
309 HeapFree( GetProcessHeap(), 0, bits );
310 HeapFree( GetProcessHeap(), 0, mask_bits );
311 return ret;
314 /*************************************************************************
315 * IMAGELIST_InternalExpandBitmaps [Internal]
317 * Expands the bitmaps of an image list by the given number of images.
319 * PARAMS
320 * himl [I] handle to image list
321 * nImageCount [I] number of images to add
323 * RETURNS
324 * nothing
326 * NOTES
327 * This function CANNOT be used to reduce the number of images.
329 static void
330 IMAGELIST_InternalExpandBitmaps(HIMAGELIST himl, INT nImageCount)
332 HDC hdcBitmap;
333 HBITMAP hbmNewBitmap, hbmNull;
334 INT nNewCount;
335 SIZE sz;
337 TRACE("%p has allocated %d, max %d, grow %d images\n", himl, himl->cCurImage, himl->cMaxImage, himl->cGrow);
339 if (himl->cCurImage + nImageCount < himl->cMaxImage)
340 return;
342 nNewCount = himl->cMaxImage + max(nImageCount, himl->cGrow) + 1;
344 imagelist_get_bitmap_size(himl, nNewCount, &sz);
346 TRACE("Create expanded bitmaps : himl=%p x=%d y=%d count=%d\n", himl, sz.cx, sz.cy, nNewCount);
347 hdcBitmap = CreateCompatibleDC (0);
349 hbmNewBitmap = ImageList_CreateImage(hdcBitmap, himl, nNewCount);
351 if (hbmNewBitmap == 0)
352 ERR("creating new image bitmap (x=%d y=%d)!\n", sz.cx, sz.cy);
354 if (himl->cCurImage)
356 hbmNull = SelectObject (hdcBitmap, hbmNewBitmap);
357 BitBlt (hdcBitmap, 0, 0, sz.cx, sz.cy,
358 himl->hdcImage, 0, 0, SRCCOPY);
359 SelectObject (hdcBitmap, hbmNull);
361 SelectObject (himl->hdcImage, hbmNewBitmap);
362 DeleteObject (himl->hbmImage);
363 himl->hbmImage = hbmNewBitmap;
365 if (himl->flags & ILC_MASK)
367 hbmNewBitmap = CreateBitmap (sz.cx, sz.cy, 1, 1, NULL);
369 if (hbmNewBitmap == 0)
370 ERR("creating new mask bitmap!\n");
372 if(himl->cCurImage)
374 hbmNull = SelectObject (hdcBitmap, hbmNewBitmap);
375 BitBlt (hdcBitmap, 0, 0, sz.cx, sz.cy,
376 himl->hdcMask, 0, 0, SRCCOPY);
377 SelectObject (hdcBitmap, hbmNull);
379 SelectObject (himl->hdcMask, hbmNewBitmap);
380 DeleteObject (himl->hbmMask);
381 himl->hbmMask = hbmNewBitmap;
384 if (himl->has_alpha)
386 char *new_alpha = HeapReAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, himl->has_alpha, nNewCount );
387 if (new_alpha) himl->has_alpha = new_alpha;
388 else
390 HeapFree( GetProcessHeap(), 0, himl->has_alpha );
391 himl->has_alpha = NULL;
395 himl->cMaxImage = nNewCount;
397 DeleteDC (hdcBitmap);
401 /*************************************************************************
402 * ImageList_Add [COMCTL32.@]
404 * Add an image or images to an image list.
406 * PARAMS
407 * himl [I] handle to image list
408 * hbmImage [I] handle to image bitmap
409 * hbmMask [I] handle to mask bitmap
411 * RETURNS
412 * Success: Index of the first new image.
413 * Failure: -1
416 INT WINAPI
417 ImageList_Add (HIMAGELIST himl, HBITMAP hbmImage, HBITMAP hbmMask)
419 HDC hdcBitmap, hdcTemp = 0;
420 INT nFirstIndex, nImageCount, i;
421 BITMAP bmp;
422 POINT pt;
424 TRACE("himl=%p hbmimage=%p hbmmask=%p\n", himl, hbmImage, hbmMask);
425 if (!is_valid(himl))
426 return -1;
428 if (!GetObjectW(hbmImage, sizeof(BITMAP), &bmp))
429 return -1;
431 TRACE("himl %p, cCurImage %d, cMaxImage %d, cGrow %d, cx %d, cy %d\n",
432 himl, himl->cCurImage, himl->cMaxImage, himl->cGrow, himl->cx, himl->cy);
434 nImageCount = bmp.bmWidth / himl->cx;
436 TRACE("%p has %d images (%d x %d)\n", hbmImage, nImageCount, bmp.bmWidth, bmp.bmHeight);
438 IMAGELIST_InternalExpandBitmaps(himl, nImageCount);
440 hdcBitmap = CreateCompatibleDC(0);
442 SelectObject(hdcBitmap, hbmImage);
444 if (add_with_alpha( himl, hdcBitmap, himl->cCurImage, nImageCount,
445 himl->cx, min( himl->cy, bmp.bmHeight), hbmImage, hbmMask ))
446 goto done;
448 if (himl->hbmMask)
450 hdcTemp = CreateCompatibleDC(0);
451 SelectObject(hdcTemp, hbmMask);
454 for (i=0; i<nImageCount; i++)
456 imagelist_point_from_index( himl, himl->cCurImage + i, &pt );
458 /* Copy result to the imagelist
460 BitBlt( himl->hdcImage, pt.x, pt.y, himl->cx, bmp.bmHeight,
461 hdcBitmap, i*himl->cx, 0, SRCCOPY );
463 if (!himl->hbmMask)
464 continue;
466 BitBlt( himl->hdcMask, pt.x, pt.y, himl->cx, bmp.bmHeight,
467 hdcTemp, i*himl->cx, 0, SRCCOPY );
469 /* Remove the background from the image
471 BitBlt( himl->hdcImage, pt.x, pt.y, himl->cx, bmp.bmHeight,
472 himl->hdcMask, pt.x, pt.y, 0x220326 ); /* NOTSRCAND */
474 if (hdcTemp) DeleteDC(hdcTemp);
476 done:
477 DeleteDC(hdcBitmap);
479 nFirstIndex = himl->cCurImage;
480 himl->cCurImage += nImageCount;
482 return nFirstIndex;
486 /*************************************************************************
487 * ImageList_AddIcon [COMCTL32.@]
489 * Adds an icon to an image list.
491 * PARAMS
492 * himl [I] handle to image list
493 * hIcon [I] handle to icon
495 * RETURNS
496 * Success: index of the new image
497 * Failure: -1
499 #undef ImageList_AddIcon
500 INT WINAPI ImageList_AddIcon (HIMAGELIST himl, HICON hIcon)
502 return ImageList_ReplaceIcon (himl, -1, hIcon);
506 /*************************************************************************
507 * ImageList_AddMasked [COMCTL32.@]
509 * Adds an image or images to an image list and creates a mask from the
510 * specified bitmap using the mask color.
512 * PARAMS
513 * himl [I] handle to image list.
514 * hBitmap [I] handle to bitmap
515 * clrMask [I] mask color.
517 * RETURNS
518 * Success: Index of the first new image.
519 * Failure: -1
522 INT WINAPI
523 ImageList_AddMasked (HIMAGELIST himl, HBITMAP hBitmap, COLORREF clrMask)
525 HDC hdcMask, hdcBitmap;
526 INT ret;
527 BITMAP bmp;
528 HBITMAP hMaskBitmap;
529 COLORREF bkColor;
531 TRACE("himl=%p hbitmap=%p clrmask=%x\n", himl, hBitmap, clrMask);
532 if (!is_valid(himl))
533 return -1;
535 if (!GetObjectW(hBitmap, sizeof(BITMAP), &bmp))
536 return -1;
538 hdcBitmap = CreateCompatibleDC(0);
539 SelectObject(hdcBitmap, hBitmap);
541 /* Create a temp Mask so we can remove the background of the Image */
542 hdcMask = CreateCompatibleDC(0);
543 hMaskBitmap = CreateBitmap(bmp.bmWidth, bmp.bmHeight, 1, 1, NULL);
544 SelectObject(hdcMask, hMaskBitmap);
546 /* create monochrome image to the mask bitmap */
547 bkColor = (clrMask != CLR_DEFAULT) ? clrMask : GetPixel (hdcBitmap, 0, 0);
548 SetBkColor (hdcBitmap, bkColor);
549 BitBlt (hdcMask, 0, 0, bmp.bmWidth, bmp.bmHeight, hdcBitmap, 0, 0, SRCCOPY);
552 * Remove the background from the image
554 * WINDOWS BUG ALERT!!!!!!
555 * The statement below should not be done in common practice
556 * but this is how ImageList_AddMasked works in Windows.
557 * It overwrites the original bitmap passed, this was discovered
558 * by using the same bitmap to iterate the different styles
559 * on windows where it failed (BUT ImageList_Add is OK)
560 * This is here in case some apps rely on this bug
562 * Blt mode 0x220326 is NOTSRCAND
564 if (bmp.bmBitsPixel > 8) /* NOTSRCAND can't work with palettes */
566 SetBkColor(hdcBitmap, RGB(255,255,255));
567 BitBlt(hdcBitmap, 0, 0, bmp.bmWidth, bmp.bmHeight, hdcMask, 0, 0, 0x220326);
570 DeleteDC(hdcBitmap);
571 DeleteDC(hdcMask);
573 ret = ImageList_Add( himl, hBitmap, hMaskBitmap );
575 DeleteObject(hMaskBitmap);
576 return ret;
580 /*************************************************************************
581 * ImageList_BeginDrag [COMCTL32.@]
583 * Creates a temporary image list that contains one image. It will be used
584 * as a drag image.
586 * PARAMS
587 * himlTrack [I] handle to the source image list
588 * iTrack [I] index of the drag image in the source image list
589 * dxHotspot [I] X position of the hot spot of the drag image
590 * dyHotspot [I] Y position of the hot spot of the drag image
592 * RETURNS
593 * Success: TRUE
594 * Failure: FALSE
597 BOOL WINAPI
598 ImageList_BeginDrag (HIMAGELIST himlTrack, INT iTrack,
599 INT dxHotspot, INT dyHotspot)
601 INT cx, cy;
602 POINT src, dst;
604 TRACE("(himlTrack=%p iTrack=%d dx=%d dy=%d)\n", himlTrack, iTrack,
605 dxHotspot, dyHotspot);
607 if (!is_valid(himlTrack))
608 return FALSE;
610 if (iTrack >= himlTrack->cCurImage)
611 return FALSE;
613 if (InternalDrag.himl)
614 return FALSE;
616 cx = himlTrack->cx;
617 cy = himlTrack->cy;
619 InternalDrag.himlNoCursor = InternalDrag.himl = ImageList_Create (cx, cy, himlTrack->flags, 1, 1);
620 if (InternalDrag.himl == NULL) {
621 WARN("Error creating drag image list!\n");
622 return FALSE;
625 InternalDrag.dxHotspot = dxHotspot;
626 InternalDrag.dyHotspot = dyHotspot;
628 /* copy image */
629 imagelist_point_from_index(InternalDrag.himl, 0, &dst);
630 imagelist_point_from_index(himlTrack, iTrack, &src);
631 BitBlt(InternalDrag.himl->hdcImage, dst.x, dst.y, cx, cy, himlTrack->hdcImage, src.x, src.y,
632 SRCCOPY);
633 BitBlt(InternalDrag.himl->hdcMask, dst.x, dst.y, cx, cy, himlTrack->hdcMask, src.x, src.y,
634 SRCCOPY);
636 InternalDrag.himl->cCurImage = 1;
638 return TRUE;
642 /*************************************************************************
643 * ImageList_Copy [COMCTL32.@]
645 * Copies an image of the source image list to an image of the
646 * destination image list. Images can be copied or swapped.
648 * PARAMS
649 * himlDst [I] handle to the destination image list
650 * iDst [I] destination image index.
651 * himlSrc [I] handle to the source image list
652 * iSrc [I] source image index
653 * uFlags [I] flags for the copy operation
655 * RETURNS
656 * Success: TRUE
657 * Failure: FALSE
659 * NOTES
660 * Copying from one image list to another is possible. The original
661 * implementation just copies or swaps within one image list.
662 * Could this feature become a bug??? ;-)
665 BOOL WINAPI
666 ImageList_Copy (HIMAGELIST himlDst, INT iDst, HIMAGELIST himlSrc,
667 INT iSrc, UINT uFlags)
669 POINT ptSrc, ptDst;
671 TRACE("himlDst=%p iDst=%d himlSrc=%p iSrc=%d\n", himlDst, iDst, himlSrc, iSrc);
673 if (!is_valid(himlSrc) || !is_valid(himlDst))
674 return FALSE;
675 if ((iDst < 0) || (iDst >= himlDst->cCurImage))
676 return FALSE;
677 if ((iSrc < 0) || (iSrc >= himlSrc->cCurImage))
678 return FALSE;
680 imagelist_point_from_index( himlDst, iDst, &ptDst );
681 imagelist_point_from_index( himlSrc, iSrc, &ptSrc );
683 if (uFlags & ILCF_SWAP) {
684 /* swap */
685 HDC hdcBmp;
686 HBITMAP hbmTempImage, hbmTempMask;
688 hdcBmp = CreateCompatibleDC (0);
690 /* create temporary bitmaps */
691 hbmTempImage = CreateBitmap (himlSrc->cx, himlSrc->cy, 1,
692 himlSrc->uBitsPixel, NULL);
693 hbmTempMask = CreateBitmap (himlSrc->cx, himlSrc->cy, 1,
694 1, NULL);
696 /* copy (and stretch) destination to temporary bitmaps.(save) */
697 /* image */
698 SelectObject (hdcBmp, hbmTempImage);
699 StretchBlt (hdcBmp, 0, 0, himlSrc->cx, himlSrc->cy,
700 himlDst->hdcImage, ptDst.x, ptDst.y, himlDst->cx, himlDst->cy,
701 SRCCOPY);
702 /* mask */
703 SelectObject (hdcBmp, hbmTempMask);
704 StretchBlt (hdcBmp, 0, 0, himlSrc->cx, himlSrc->cy,
705 himlDst->hdcMask, ptDst.x, ptDst.y, himlDst->cx, himlDst->cy,
706 SRCCOPY);
708 /* copy (and stretch) source to destination */
709 /* image */
710 StretchBlt (himlDst->hdcImage, ptDst.x, ptDst.y, himlDst->cx, himlDst->cy,
711 himlSrc->hdcImage, ptSrc.x, ptSrc.y, himlSrc->cx, himlSrc->cy,
712 SRCCOPY);
713 /* mask */
714 StretchBlt (himlDst->hdcMask, ptDst.x, ptDst.y, himlDst->cx, himlDst->cy,
715 himlSrc->hdcMask, ptSrc.x, ptSrc.y, himlSrc->cx, himlSrc->cy,
716 SRCCOPY);
718 /* copy (without stretching) temporary bitmaps to source (restore) */
719 /* mask */
720 BitBlt (himlSrc->hdcMask, ptSrc.x, ptSrc.y, himlSrc->cx, himlSrc->cy,
721 hdcBmp, 0, 0, SRCCOPY);
723 /* image */
724 BitBlt (himlSrc->hdcImage, ptSrc.x, ptSrc.y, himlSrc->cx, himlSrc->cy,
725 hdcBmp, 0, 0, SRCCOPY);
726 /* delete temporary bitmaps */
727 DeleteObject (hbmTempMask);
728 DeleteObject (hbmTempImage);
729 DeleteDC(hdcBmp);
731 else {
732 /* copy image */
733 StretchBlt (himlDst->hdcImage, ptDst.x, ptDst.y, himlDst->cx, himlDst->cy,
734 himlSrc->hdcImage, ptSrc.x, ptSrc.y, himlSrc->cx, himlSrc->cy,
735 SRCCOPY);
737 /* copy mask */
738 StretchBlt (himlDst->hdcMask, ptDst.x, ptDst.y, himlDst->cx, himlDst->cy,
739 himlSrc->hdcMask, ptSrc.x, ptSrc.y, himlSrc->cx, himlSrc->cy,
740 SRCCOPY);
743 return TRUE;
747 /*************************************************************************
748 * ImageList_Create [COMCTL32.@]
750 * Creates a new image list.
752 * PARAMS
753 * cx [I] image height
754 * cy [I] image width
755 * flags [I] creation flags
756 * cInitial [I] initial number of images in the image list
757 * cGrow [I] number of images by which image list grows
759 * RETURNS
760 * Success: Handle to the created image list
761 * Failure: NULL
763 HIMAGELIST WINAPI
764 ImageList_Create (INT cx, INT cy, UINT flags,
765 INT cInitial, INT cGrow)
767 HIMAGELIST himl;
768 INT nCount;
769 HBITMAP hbmTemp;
770 UINT ilc = (flags & 0xFE);
771 static const WORD aBitBlend25[] =
772 {0xAA, 0x00, 0x55, 0x00, 0xAA, 0x00, 0x55, 0x00};
774 static const WORD aBitBlend50[] =
775 {0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA};
777 TRACE("(%d %d 0x%x %d %d)\n", cx, cy, flags, cInitial, cGrow);
779 if (cx <= 0 || cy <= 0) return NULL;
781 /* Create the IImageList interface for the image list */
782 if (FAILED(ImageListImpl_CreateInstance(NULL, &IID_IImageList, (void **)&himl)))
783 return NULL;
785 cGrow = (WORD)((max( cGrow, 1 ) + 3) & ~3);
787 if (cGrow > 256)
789 /* Windows doesn't limit the size here, but X11 doesn't let us allocate such huge bitmaps */
790 WARN( "grow %d too large, limiting to 256\n", cGrow );
791 cGrow = 256;
794 himl->cx = cx;
795 himl->cy = cy;
796 himl->flags = flags;
797 himl->cMaxImage = cInitial + 1;
798 himl->cInitial = cInitial;
799 himl->cGrow = cGrow;
800 himl->clrFg = CLR_DEFAULT;
801 himl->clrBk = CLR_NONE;
803 /* initialize overlay mask indices */
804 for (nCount = 0; nCount < MAX_OVERLAYIMAGE; nCount++)
805 himl->nOvlIdx[nCount] = -1;
807 /* Create Image & Mask DCs */
808 himl->hdcImage = CreateCompatibleDC (0);
809 if (!himl->hdcImage)
810 goto cleanup;
811 if (himl->flags & ILC_MASK){
812 himl->hdcMask = CreateCompatibleDC(0);
813 if (!himl->hdcMask)
814 goto cleanup;
817 /* Default to ILC_COLOR4 if none of the ILC_COLOR* flags are specified */
818 if (ilc == ILC_COLOR)
820 ilc = ILC_COLOR4;
821 himl->flags |= ILC_COLOR4;
824 if (ilc >= ILC_COLOR4 && ilc <= ILC_COLOR32)
825 himl->uBitsPixel = ilc;
826 else
827 himl->uBitsPixel = (UINT)GetDeviceCaps (himl->hdcImage, BITSPIXEL);
829 if (himl->cMaxImage > 0) {
830 himl->hbmImage = ImageList_CreateImage(himl->hdcImage, himl, himl->cMaxImage);
831 SelectObject(himl->hdcImage, himl->hbmImage);
832 } else
833 himl->hbmImage = 0;
835 if ((himl->cMaxImage > 0) && (himl->flags & ILC_MASK)) {
836 SIZE sz;
838 imagelist_get_bitmap_size(himl, himl->cMaxImage, &sz);
839 himl->hbmMask = CreateBitmap (sz.cx, sz.cy, 1, 1, NULL);
840 if (himl->hbmMask == 0) {
841 ERR("Error creating mask bitmap!\n");
842 goto cleanup;
844 SelectObject(himl->hdcMask, himl->hbmMask);
846 else
847 himl->hbmMask = 0;
849 if (ilc == ILC_COLOR32)
850 himl->has_alpha = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, himl->cMaxImage );
851 else
852 himl->has_alpha = NULL;
854 /* create blending brushes */
855 hbmTemp = CreateBitmap (8, 8, 1, 1, aBitBlend25);
856 himl->hbrBlend25 = CreatePatternBrush (hbmTemp);
857 DeleteObject (hbmTemp);
859 hbmTemp = CreateBitmap (8, 8, 1, 1, aBitBlend50);
860 himl->hbrBlend50 = CreatePatternBrush (hbmTemp);
861 DeleteObject (hbmTemp);
863 TRACE("created imagelist %p\n", himl);
864 return himl;
866 cleanup:
867 ImageList_Destroy(himl);
868 return NULL;
872 /*************************************************************************
873 * ImageList_Destroy [COMCTL32.@]
875 * Destroys an image list.
877 * PARAMS
878 * himl [I] handle to image list
880 * RETURNS
881 * Success: TRUE
882 * Failure: FALSE
885 BOOL WINAPI
886 ImageList_Destroy (HIMAGELIST himl)
888 if (!is_valid(himl))
889 return FALSE;
891 IImageList_Release((IImageList *) himl);
892 return TRUE;
896 /*************************************************************************
897 * ImageList_DragEnter [COMCTL32.@]
899 * Locks window update and displays the drag image at the given position.
901 * PARAMS
902 * hwndLock [I] handle of the window that owns the drag image.
903 * x [I] X position of the drag image.
904 * y [I] Y position of the drag image.
906 * RETURNS
907 * Success: TRUE
908 * Failure: FALSE
910 * NOTES
911 * The position of the drag image is relative to the window, not
912 * the client area.
915 BOOL WINAPI
916 ImageList_DragEnter (HWND hwndLock, INT x, INT y)
918 TRACE("(hwnd=%p x=%d y=%d)\n", hwndLock, x, y);
920 if (!is_valid(InternalDrag.himl))
921 return FALSE;
923 if (hwndLock)
924 InternalDrag.hwnd = hwndLock;
925 else
926 InternalDrag.hwnd = GetDesktopWindow ();
928 InternalDrag.x = x;
929 InternalDrag.y = y;
931 /* draw the drag image and save the background */
932 if (!ImageList_DragShowNolock(TRUE)) {
933 return FALSE;
936 return TRUE;
940 /*************************************************************************
941 * ImageList_DragLeave [COMCTL32.@]
943 * Unlocks window update and hides the drag image.
945 * PARAMS
946 * hwndLock [I] handle of the window that owns the drag image.
948 * RETURNS
949 * Success: TRUE
950 * Failure: FALSE
953 BOOL WINAPI
954 ImageList_DragLeave (HWND hwndLock)
956 /* As we don't save drag info in the window this can lead to problems if
957 an app does not supply the same window as DragEnter */
958 /* if (hwndLock)
959 InternalDrag.hwnd = hwndLock;
960 else
961 InternalDrag.hwnd = GetDesktopWindow (); */
962 if(!hwndLock)
963 hwndLock = GetDesktopWindow();
964 if(InternalDrag.hwnd != hwndLock)
965 FIXME("DragLeave hWnd != DragEnter hWnd\n");
967 ImageList_DragShowNolock (FALSE);
969 return TRUE;
973 /*************************************************************************
974 * ImageList_InternalDragDraw [Internal]
976 * Draws the drag image.
978 * PARAMS
979 * hdc [I] device context to draw into.
980 * x [I] X position of the drag image.
981 * y [I] Y position of the drag image.
983 * RETURNS
984 * Success: TRUE
985 * Failure: FALSE
987 * NOTES
988 * The position of the drag image is relative to the window, not
989 * the client area.
993 static inline void
994 ImageList_InternalDragDraw (HDC hdc, INT x, INT y)
996 IMAGELISTDRAWPARAMS imldp;
998 ZeroMemory (&imldp, sizeof(imldp));
999 imldp.cbSize = sizeof(imldp);
1000 imldp.himl = InternalDrag.himl;
1001 imldp.i = 0;
1002 imldp.hdcDst = hdc,
1003 imldp.x = x;
1004 imldp.y = y;
1005 imldp.rgbBk = CLR_DEFAULT;
1006 imldp.rgbFg = CLR_DEFAULT;
1007 imldp.fStyle = ILD_NORMAL;
1008 imldp.fState = ILS_ALPHA;
1009 imldp.Frame = 192;
1010 ImageList_DrawIndirect (&imldp);
1013 /*************************************************************************
1014 * ImageList_DragMove [COMCTL32.@]
1016 * Moves the drag image.
1018 * PARAMS
1019 * x [I] X position of the drag image.
1020 * y [I] Y position of the drag image.
1022 * RETURNS
1023 * Success: TRUE
1024 * Failure: FALSE
1026 * NOTES
1027 * The position of the drag image is relative to the window, not
1028 * the client area.
1031 BOOL WINAPI
1032 ImageList_DragMove (INT x, INT y)
1034 TRACE("(x=%d y=%d)\n", x, y);
1036 if (!is_valid(InternalDrag.himl))
1037 return FALSE;
1039 /* draw/update the drag image */
1040 if (InternalDrag.bShow) {
1041 HDC hdcDrag;
1042 HDC hdcOffScreen;
1043 HDC hdcBg;
1044 HBITMAP hbmOffScreen;
1045 INT origNewX, origNewY;
1046 INT origOldX, origOldY;
1047 INT origRegX, origRegY;
1048 INT sizeRegX, sizeRegY;
1051 /* calculate the update region */
1052 origNewX = x - InternalDrag.dxHotspot;
1053 origNewY = y - InternalDrag.dyHotspot;
1054 origOldX = InternalDrag.x - InternalDrag.dxHotspot;
1055 origOldY = InternalDrag.y - InternalDrag.dyHotspot;
1056 origRegX = min(origNewX, origOldX);
1057 origRegY = min(origNewY, origOldY);
1058 sizeRegX = InternalDrag.himl->cx + abs(x - InternalDrag.x);
1059 sizeRegY = InternalDrag.himl->cy + abs(y - InternalDrag.y);
1061 hdcDrag = GetDCEx(InternalDrag.hwnd, 0,
1062 DCX_WINDOW | DCX_CACHE | DCX_LOCKWINDOWUPDATE);
1063 hdcOffScreen = CreateCompatibleDC(hdcDrag);
1064 hdcBg = CreateCompatibleDC(hdcDrag);
1066 hbmOffScreen = CreateCompatibleBitmap(hdcDrag, sizeRegX, sizeRegY);
1067 SelectObject(hdcOffScreen, hbmOffScreen);
1068 SelectObject(hdcBg, InternalDrag.hbmBg);
1070 /* get the actual background of the update region */
1071 BitBlt(hdcOffScreen, 0, 0, sizeRegX, sizeRegY, hdcDrag,
1072 origRegX, origRegY, SRCCOPY);
1073 /* erase the old image */
1074 BitBlt(hdcOffScreen, origOldX - origRegX, origOldY - origRegY,
1075 InternalDrag.himl->cx, InternalDrag.himl->cy, hdcBg, 0, 0,
1076 SRCCOPY);
1077 /* save the background */
1078 BitBlt(hdcBg, 0, 0, InternalDrag.himl->cx, InternalDrag.himl->cy,
1079 hdcOffScreen, origNewX - origRegX, origNewY - origRegY, SRCCOPY);
1080 /* draw the image */
1081 ImageList_InternalDragDraw(hdcOffScreen, origNewX - origRegX,
1082 origNewY - origRegY);
1083 /* draw the update region to the screen */
1084 BitBlt(hdcDrag, origRegX, origRegY, sizeRegX, sizeRegY,
1085 hdcOffScreen, 0, 0, SRCCOPY);
1087 DeleteDC(hdcBg);
1088 DeleteDC(hdcOffScreen);
1089 DeleteObject(hbmOffScreen);
1090 ReleaseDC(InternalDrag.hwnd, hdcDrag);
1093 /* update the image position */
1094 InternalDrag.x = x;
1095 InternalDrag.y = y;
1097 return TRUE;
1101 /*************************************************************************
1102 * ImageList_DragShowNolock [COMCTL32.@]
1104 * Shows or hides the drag image.
1106 * PARAMS
1107 * bShow [I] TRUE shows the drag image, FALSE hides it.
1109 * RETURNS
1110 * Success: TRUE
1111 * Failure: FALSE
1114 BOOL WINAPI
1115 ImageList_DragShowNolock (BOOL bShow)
1117 HDC hdcDrag;
1118 HDC hdcBg;
1119 INT x, y;
1121 if (!is_valid(InternalDrag.himl))
1122 return FALSE;
1124 TRACE("bShow=0x%X!\n", bShow);
1126 /* DragImage is already visible/hidden */
1127 if ((InternalDrag.bShow && bShow) || (!InternalDrag.bShow && !bShow)) {
1128 return FALSE;
1131 /* position of the origin of the DragImage */
1132 x = InternalDrag.x - InternalDrag.dxHotspot;
1133 y = InternalDrag.y - InternalDrag.dyHotspot;
1135 hdcDrag = GetDCEx (InternalDrag.hwnd, 0,
1136 DCX_WINDOW | DCX_CACHE | DCX_LOCKWINDOWUPDATE);
1137 if (!hdcDrag) {
1138 return FALSE;
1141 hdcBg = CreateCompatibleDC(hdcDrag);
1142 if (!InternalDrag.hbmBg) {
1143 InternalDrag.hbmBg = CreateCompatibleBitmap(hdcDrag,
1144 InternalDrag.himl->cx, InternalDrag.himl->cy);
1146 SelectObject(hdcBg, InternalDrag.hbmBg);
1148 if (bShow) {
1149 /* save the background */
1150 BitBlt(hdcBg, 0, 0, InternalDrag.himl->cx, InternalDrag.himl->cy,
1151 hdcDrag, x, y, SRCCOPY);
1152 /* show the image */
1153 ImageList_InternalDragDraw(hdcDrag, x, y);
1154 } else {
1155 /* hide the image */
1156 BitBlt(hdcDrag, x, y, InternalDrag.himl->cx, InternalDrag.himl->cy,
1157 hdcBg, 0, 0, SRCCOPY);
1160 InternalDrag.bShow = !InternalDrag.bShow;
1162 DeleteDC(hdcBg);
1163 ReleaseDC (InternalDrag.hwnd, hdcDrag);
1164 return TRUE;
1168 /*************************************************************************
1169 * ImageList_Draw [COMCTL32.@]
1171 * Draws an image.
1173 * PARAMS
1174 * himl [I] handle to image list
1175 * i [I] image index
1176 * hdc [I] handle to device context
1177 * x [I] x position
1178 * y [I] y position
1179 * fStyle [I] drawing flags
1181 * RETURNS
1182 * Success: TRUE
1183 * Failure: FALSE
1185 * SEE
1186 * ImageList_DrawEx.
1189 BOOL WINAPI
1190 ImageList_Draw (HIMAGELIST himl, INT i, HDC hdc, INT x, INT y, UINT fStyle)
1192 return ImageList_DrawEx (himl, i, hdc, x, y, 0, 0,
1193 CLR_DEFAULT, CLR_DEFAULT, fStyle);
1197 /*************************************************************************
1198 * ImageList_DrawEx [COMCTL32.@]
1200 * Draws an image and allows using extended drawing features.
1202 * PARAMS
1203 * himl [I] handle to image list
1204 * i [I] image index
1205 * hdc [I] handle to device context
1206 * x [I] X position
1207 * y [I] Y position
1208 * dx [I] X offset
1209 * dy [I] Y offset
1210 * rgbBk [I] background color
1211 * rgbFg [I] foreground color
1212 * fStyle [I] drawing flags
1214 * RETURNS
1215 * Success: TRUE
1216 * Failure: FALSE
1218 * NOTES
1219 * Calls ImageList_DrawIndirect.
1221 * SEE
1222 * ImageList_DrawIndirect.
1225 BOOL WINAPI
1226 ImageList_DrawEx (HIMAGELIST himl, INT i, HDC hdc, INT x, INT y,
1227 INT dx, INT dy, COLORREF rgbBk, COLORREF rgbFg,
1228 UINT fStyle)
1230 IMAGELISTDRAWPARAMS imldp;
1232 ZeroMemory (&imldp, sizeof(imldp));
1233 imldp.cbSize = sizeof(imldp);
1234 imldp.himl = himl;
1235 imldp.i = i;
1236 imldp.hdcDst = hdc,
1237 imldp.x = x;
1238 imldp.y = y;
1239 imldp.cx = dx;
1240 imldp.cy = dy;
1241 imldp.rgbBk = rgbBk;
1242 imldp.rgbFg = rgbFg;
1243 imldp.fStyle = fStyle;
1245 return ImageList_DrawIndirect (&imldp);
1249 static BOOL alpha_blend_image( HIMAGELIST himl, HDC dest_dc, int dest_x, int dest_y,
1250 int src_x, int src_y, int cx, int cy, BLENDFUNCTION func,
1251 UINT style, COLORREF blend_col )
1253 BOOL ret = FALSE;
1254 HDC hdc;
1255 HBITMAP bmp = 0, mask = 0;
1256 BITMAPINFO *info;
1257 void *bits, *mask_bits;
1258 unsigned int *ptr;
1259 int i, j;
1261 if (!(hdc = CreateCompatibleDC( 0 ))) return FALSE;
1262 if (!(info = HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET( BITMAPINFO, bmiColors[256] )))) goto done;
1263 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1264 info->bmiHeader.biWidth = cx;
1265 info->bmiHeader.biHeight = cy;
1266 info->bmiHeader.biPlanes = 1;
1267 info->bmiHeader.biBitCount = 32;
1268 info->bmiHeader.biCompression = BI_RGB;
1269 info->bmiHeader.biSizeImage = cx * cy * 4;
1270 info->bmiHeader.biXPelsPerMeter = 0;
1271 info->bmiHeader.biYPelsPerMeter = 0;
1272 info->bmiHeader.biClrUsed = 0;
1273 info->bmiHeader.biClrImportant = 0;
1274 if (!(bmp = CreateDIBSection( himl->hdcImage, info, DIB_RGB_COLORS, &bits, 0, 0 ))) goto done;
1275 SelectObject( hdc, bmp );
1276 BitBlt( hdc, 0, 0, cx, cy, himl->hdcImage, src_x, src_y, SRCCOPY );
1278 if (blend_col != CLR_NONE)
1280 BYTE r = GetRValue( blend_col );
1281 BYTE g = GetGValue( blend_col );
1282 BYTE b = GetBValue( blend_col );
1284 if (style & ILD_BLEND25)
1286 for (i = 0, ptr = bits; i < cx * cy; i++, ptr++)
1287 *ptr = ((*ptr & 0xff000000) |
1288 ((((*ptr & 0x00ff0000) * 3 + (r << 16)) / 4) & 0x00ff0000) |
1289 ((((*ptr & 0x0000ff00) * 3 + (g << 8)) / 4) & 0x0000ff00) |
1290 ((((*ptr & 0x000000ff) * 3 + (b << 0)) / 4) & 0x000000ff));
1292 else if (style & ILD_BLEND50)
1294 for (i = 0, ptr = bits; i < cx * cy; i++, ptr++)
1295 *ptr = ((*ptr & 0xff000000) |
1296 ((((*ptr & 0x00ff0000) + (r << 16)) / 2) & 0x00ff0000) |
1297 ((((*ptr & 0x0000ff00) + (g << 8)) / 2) & 0x0000ff00) |
1298 ((((*ptr & 0x000000ff) + (b << 0)) / 2) & 0x000000ff));
1302 if (himl->has_alpha) /* we already have an alpha channel in this case */
1304 /* pre-multiply by the alpha channel */
1305 for (i = 0, ptr = bits; i < cx * cy; i++, ptr++)
1307 DWORD alpha = *ptr >> 24;
1308 *ptr = ((*ptr & 0xff000000) |
1309 (((*ptr & 0x00ff0000) * alpha / 255) & 0x00ff0000) |
1310 (((*ptr & 0x0000ff00) * alpha / 255) & 0x0000ff00) |
1311 (((*ptr & 0x000000ff) * alpha / 255)));
1314 else if (himl->hbmMask)
1316 unsigned int width_bytes = (cx + 31) / 32 * 4;
1317 /* generate alpha channel from the mask */
1318 info->bmiHeader.biBitCount = 1;
1319 info->bmiHeader.biSizeImage = width_bytes * cy;
1320 info->bmiColors[0].rgbRed = 0;
1321 info->bmiColors[0].rgbGreen = 0;
1322 info->bmiColors[0].rgbBlue = 0;
1323 info->bmiColors[0].rgbReserved = 0;
1324 info->bmiColors[1].rgbRed = 0xff;
1325 info->bmiColors[1].rgbGreen = 0xff;
1326 info->bmiColors[1].rgbBlue = 0xff;
1327 info->bmiColors[1].rgbReserved = 0;
1328 if (!(mask = CreateDIBSection( himl->hdcMask, info, DIB_RGB_COLORS, &mask_bits, 0, 0 )))
1329 goto done;
1330 SelectObject( hdc, mask );
1331 BitBlt( hdc, 0, 0, cx, cy, himl->hdcMask, src_x, src_y, SRCCOPY );
1332 SelectObject( hdc, bmp );
1333 for (i = 0, ptr = bits; i < cy; i++)
1334 for (j = 0; j < cx; j++, ptr++)
1335 if ((((BYTE *)mask_bits)[i * width_bytes + j / 8] << (j % 8)) & 0x80) *ptr = 0;
1336 else *ptr |= 0xff000000;
1339 ret = GdiAlphaBlend( dest_dc, dest_x, dest_y, cx, cy, hdc, 0, 0, cx, cy, func );
1341 done:
1342 DeleteDC( hdc );
1343 if (bmp) DeleteObject( bmp );
1344 if (mask) DeleteObject( mask );
1345 HeapFree( GetProcessHeap(), 0, info );
1346 return ret;
1349 /*************************************************************************
1350 * ImageList_DrawIndirect [COMCTL32.@]
1352 * Draws an image using various parameters specified in pimldp.
1354 * PARAMS
1355 * pimldp [I] pointer to IMAGELISTDRAWPARAMS structure.
1357 * RETURNS
1358 * Success: TRUE
1359 * Failure: FALSE
1362 BOOL WINAPI
1363 ImageList_DrawIndirect (IMAGELISTDRAWPARAMS *pimldp)
1365 INT cx, cy, nOvlIdx;
1366 DWORD fState, dwRop;
1367 UINT fStyle;
1368 COLORREF oldImageBk, oldImageFg;
1369 HDC hImageDC, hImageListDC, hMaskListDC;
1370 HBITMAP hImageBmp, hOldImageBmp, hBlendMaskBmp;
1371 BOOL bIsTransparent, bBlend, bResult = FALSE, bMask;
1372 HIMAGELIST himl;
1373 HBRUSH hOldBrush;
1374 POINT pt;
1375 BOOL has_alpha;
1377 if (!pimldp || !(himl = pimldp->himl)) return FALSE;
1378 if (!is_valid(himl)) return FALSE;
1379 if ((pimldp->i < 0) || (pimldp->i >= himl->cCurImage)) return FALSE;
1381 imagelist_point_from_index( himl, pimldp->i, &pt );
1382 pt.x += pimldp->xBitmap;
1383 pt.y += pimldp->yBitmap;
1385 fState = pimldp->cbSize < sizeof(IMAGELISTDRAWPARAMS) ? ILS_NORMAL : pimldp->fState;
1386 fStyle = pimldp->fStyle & ~ILD_OVERLAYMASK;
1387 cx = (pimldp->cx == 0) ? himl->cx : pimldp->cx;
1388 cy = (pimldp->cy == 0) ? himl->cy : pimldp->cy;
1390 bIsTransparent = (fStyle & ILD_TRANSPARENT);
1391 if( pimldp->rgbBk == CLR_NONE )
1392 bIsTransparent = TRUE;
1393 if( ( pimldp->rgbBk == CLR_DEFAULT ) && ( himl->clrBk == CLR_NONE ) )
1394 bIsTransparent = TRUE;
1395 bMask = (himl->flags & ILC_MASK) && (fStyle & ILD_MASK) ;
1396 bBlend = (fStyle & (ILD_BLEND25 | ILD_BLEND50) ) && !bMask;
1398 TRACE("himl(%p) hbmMask(%p) iImage(%d) x(%d) y(%d) cx(%d) cy(%d)\n",
1399 himl, himl->hbmMask, pimldp->i, pimldp->x, pimldp->y, cx, cy);
1401 /* we will use these DCs to access the images and masks in the ImageList */
1402 hImageListDC = himl->hdcImage;
1403 hMaskListDC = himl->hdcMask;
1405 /* these will accumulate the image and mask for the image we're drawing */
1406 hImageDC = CreateCompatibleDC( pimldp->hdcDst );
1407 hImageBmp = CreateCompatibleBitmap( pimldp->hdcDst, cx, cy );
1408 hBlendMaskBmp = bBlend ? CreateBitmap(cx, cy, 1, 1, NULL) : 0;
1410 /* Create a compatible DC. */
1411 if (!hImageListDC || !hImageDC || !hImageBmp ||
1412 (bBlend && !hBlendMaskBmp) || (himl->hbmMask && !hMaskListDC))
1413 goto cleanup;
1415 hOldImageBmp = SelectObject(hImageDC, hImageBmp);
1418 * To obtain a transparent look, background color should be set
1419 * to white and foreground color to black when blitting the
1420 * monochrome mask.
1422 oldImageFg = SetTextColor( hImageDC, RGB( 0, 0, 0 ) );
1423 oldImageBk = SetBkColor( hImageDC, RGB( 0xff, 0xff, 0xff ) );
1425 has_alpha = (himl->has_alpha && himl->has_alpha[pimldp->i]);
1426 if (!bMask && (has_alpha || (fState & ILS_ALPHA)))
1428 COLORREF colour, blend_col = CLR_NONE;
1429 BLENDFUNCTION func;
1431 if (bBlend)
1433 blend_col = pimldp->rgbFg;
1434 if (blend_col == CLR_DEFAULT) blend_col = GetSysColor( COLOR_HIGHLIGHT );
1435 else if (blend_col == CLR_NONE) blend_col = GetTextColor( pimldp->hdcDst );
1438 func.BlendOp = AC_SRC_OVER;
1439 func.BlendFlags = 0;
1440 func.SourceConstantAlpha = (fState & ILS_ALPHA) ? pimldp->Frame : 255;
1441 func.AlphaFormat = AC_SRC_ALPHA;
1443 if (bIsTransparent)
1445 bResult = alpha_blend_image( himl, pimldp->hdcDst, pimldp->x, pimldp->y,
1446 pt.x, pt.y, cx, cy, func, fStyle, blend_col );
1447 goto end;
1449 colour = pimldp->rgbBk;
1450 if (colour == CLR_DEFAULT) colour = himl->clrBk;
1451 if (colour == CLR_NONE) colour = GetBkColor( pimldp->hdcDst );
1453 hOldBrush = SelectObject (hImageDC, CreateSolidBrush (colour));
1454 PatBlt( hImageDC, 0, 0, cx, cy, PATCOPY );
1455 alpha_blend_image( himl, hImageDC, 0, 0, pt.x, pt.y, cx, cy, func, fStyle, blend_col );
1456 DeleteObject (SelectObject (hImageDC, hOldBrush));
1457 bResult = BitBlt( pimldp->hdcDst, pimldp->x, pimldp->y, cx, cy, hImageDC, 0, 0, SRCCOPY );
1458 goto end;
1462 * Draw the initial image
1464 if( bMask ) {
1465 if (himl->hbmMask) {
1466 hOldBrush = SelectObject (hImageDC, CreateSolidBrush (GetTextColor(pimldp->hdcDst)));
1467 PatBlt( hImageDC, 0, 0, cx, cy, PATCOPY );
1468 BitBlt(hImageDC, 0, 0, cx, cy, hMaskListDC, pt.x, pt.y, SRCPAINT);
1469 DeleteObject (SelectObject (hImageDC, hOldBrush));
1470 if( bIsTransparent )
1472 BitBlt ( pimldp->hdcDst, pimldp->x, pimldp->y, cx, cy, hImageDC, 0, 0, SRCAND);
1473 bResult = TRUE;
1474 goto end;
1476 } else {
1477 hOldBrush = SelectObject (hImageDC, GetStockObject(BLACK_BRUSH));
1478 PatBlt( hImageDC, 0, 0, cx, cy, PATCOPY);
1479 SelectObject(hImageDC, hOldBrush);
1481 } else {
1482 /* blend the image with the needed solid background */
1483 COLORREF colour = RGB(0,0,0);
1485 if( !bIsTransparent )
1487 colour = pimldp->rgbBk;
1488 if( colour == CLR_DEFAULT )
1489 colour = himl->clrBk;
1490 if( colour == CLR_NONE )
1491 colour = GetBkColor(pimldp->hdcDst);
1494 hOldBrush = SelectObject (hImageDC, CreateSolidBrush (colour));
1495 PatBlt( hImageDC, 0, 0, cx, cy, PATCOPY );
1496 if (himl->hbmMask)
1498 BitBlt( hImageDC, 0, 0, cx, cy, hMaskListDC, pt.x, pt.y, SRCAND );
1499 BitBlt( hImageDC, 0, 0, cx, cy, hImageListDC, pt.x, pt.y, SRCPAINT );
1501 else
1502 BitBlt( hImageDC, 0, 0, cx, cy, hImageListDC, pt.x, pt.y, SRCCOPY);
1503 DeleteObject (SelectObject (hImageDC, hOldBrush));
1506 /* Time for blending, if required */
1507 if (bBlend) {
1508 HBRUSH hBlendBrush;
1509 COLORREF clrBlend = pimldp->rgbFg;
1510 HDC hBlendMaskDC = hImageListDC;
1511 HBITMAP hOldBitmap;
1513 /* Create the blend Mask */
1514 hOldBitmap = SelectObject(hBlendMaskDC, hBlendMaskBmp);
1515 hBlendBrush = fStyle & ILD_BLEND50 ? himl->hbrBlend50 : himl->hbrBlend25;
1516 hOldBrush = SelectObject(hBlendMaskDC, hBlendBrush);
1517 PatBlt(hBlendMaskDC, 0, 0, cx, cy, PATCOPY);
1518 SelectObject(hBlendMaskDC, hOldBrush);
1520 /* Modify the blend mask if an Image Mask exist */
1521 if(himl->hbmMask) {
1522 BitBlt(hBlendMaskDC, 0, 0, cx, cy, hMaskListDC, pt.x, pt.y, 0x220326); /* NOTSRCAND */
1523 BitBlt(hBlendMaskDC, 0, 0, cx, cy, hBlendMaskDC, 0, 0, NOTSRCCOPY);
1526 /* now apply blend to the current image given the BlendMask */
1527 if (clrBlend == CLR_DEFAULT) clrBlend = GetSysColor (COLOR_HIGHLIGHT);
1528 else if (clrBlend == CLR_NONE) clrBlend = GetTextColor (pimldp->hdcDst);
1529 hOldBrush = SelectObject (hImageDC, CreateSolidBrush(clrBlend));
1530 BitBlt (hImageDC, 0, 0, cx, cy, hBlendMaskDC, 0, 0, 0xB8074A); /* PSDPxax */
1531 DeleteObject(SelectObject(hImageDC, hOldBrush));
1532 SelectObject(hBlendMaskDC, hOldBitmap);
1535 /* Now do the overlay image, if any */
1536 nOvlIdx = (pimldp->fStyle & ILD_OVERLAYMASK) >> 8;
1537 if ( (nOvlIdx >= 1) && (nOvlIdx <= MAX_OVERLAYIMAGE)) {
1538 nOvlIdx = himl->nOvlIdx[nOvlIdx - 1];
1539 if ((nOvlIdx >= 0) && (nOvlIdx < himl->cCurImage)) {
1540 POINT ptOvl;
1541 imagelist_point_from_index( himl, nOvlIdx, &ptOvl );
1542 ptOvl.x += pimldp->xBitmap;
1543 if (himl->hbmMask && !(fStyle & ILD_IMAGE))
1544 BitBlt (hImageDC, 0, 0, cx, cy, hMaskListDC, ptOvl.x, ptOvl.y, SRCAND);
1545 BitBlt (hImageDC, 0, 0, cx, cy, hImageListDC, ptOvl.x, ptOvl.y, SRCPAINT);
1549 if (fState & ILS_SATURATE) FIXME("ILS_SATURATE: unimplemented!\n");
1550 if (fState & ILS_GLOW) FIXME("ILS_GLOW: unimplemented!\n");
1551 if (fState & ILS_SHADOW) FIXME("ILS_SHADOW: unimplemented!\n");
1553 if (fStyle & ILD_PRESERVEALPHA) FIXME("ILD_PRESERVEALPHA: unimplemented!\n");
1554 if (fStyle & ILD_SCALE) FIXME("ILD_SCALE: unimplemented!\n");
1555 if (fStyle & ILD_DPISCALE) FIXME("ILD_DPISCALE: unimplemented!\n");
1557 /* now copy the image to the screen */
1558 dwRop = SRCCOPY;
1559 if (himl->hbmMask && bIsTransparent ) {
1560 COLORREF oldDstFg = SetTextColor(pimldp->hdcDst, RGB( 0, 0, 0 ) );
1561 COLORREF oldDstBk = SetBkColor(pimldp->hdcDst, RGB( 0xff, 0xff, 0xff ));
1562 BitBlt (pimldp->hdcDst, pimldp->x, pimldp->y, cx, cy, hMaskListDC, pt.x, pt.y, SRCAND);
1563 SetBkColor(pimldp->hdcDst, oldDstBk);
1564 SetTextColor(pimldp->hdcDst, oldDstFg);
1565 dwRop = SRCPAINT;
1567 if (fStyle & ILD_ROP) dwRop = pimldp->dwRop;
1568 BitBlt (pimldp->hdcDst, pimldp->x, pimldp->y, cx, cy, hImageDC, 0, 0, dwRop);
1570 bResult = TRUE;
1571 end:
1572 /* cleanup the mess */
1573 SetBkColor(hImageDC, oldImageBk);
1574 SetTextColor(hImageDC, oldImageFg);
1575 SelectObject(hImageDC, hOldImageBmp);
1576 cleanup:
1577 DeleteObject(hBlendMaskBmp);
1578 DeleteObject(hImageBmp);
1579 DeleteDC(hImageDC);
1581 return bResult;
1585 /*************************************************************************
1586 * ImageList_Duplicate [COMCTL32.@]
1588 * Duplicates an image list.
1590 * PARAMS
1591 * himlSrc [I] source image list handle
1593 * RETURNS
1594 * Success: Handle of duplicated image list.
1595 * Failure: NULL
1598 HIMAGELIST WINAPI
1599 ImageList_Duplicate (HIMAGELIST himlSrc)
1601 HIMAGELIST himlDst;
1603 if (!is_valid(himlSrc)) {
1604 ERR("Invalid image list handle!\n");
1605 return NULL;
1608 himlDst = ImageList_Create (himlSrc->cx, himlSrc->cy, himlSrc->flags,
1609 himlSrc->cCurImage, himlSrc->cGrow);
1611 if (himlDst)
1613 SIZE sz;
1615 imagelist_get_bitmap_size(himlSrc, himlSrc->cCurImage, &sz);
1616 BitBlt (himlDst->hdcImage, 0, 0, sz.cx, sz.cy,
1617 himlSrc->hdcImage, 0, 0, SRCCOPY);
1619 if (himlDst->hbmMask)
1620 BitBlt (himlDst->hdcMask, 0, 0, sz.cx, sz.cy,
1621 himlSrc->hdcMask, 0, 0, SRCCOPY);
1623 himlDst->cCurImage = himlSrc->cCurImage;
1624 if (himlSrc->has_alpha && himlDst->has_alpha)
1625 memcpy( himlDst->has_alpha, himlSrc->has_alpha, himlDst->cCurImage );
1627 return himlDst;
1631 /*************************************************************************
1632 * ImageList_EndDrag [COMCTL32.@]
1634 * Finishes a drag operation.
1636 * PARAMS
1637 * no Parameters
1639 * RETURNS
1640 * Success: TRUE
1641 * Failure: FALSE
1644 VOID WINAPI
1645 ImageList_EndDrag (void)
1647 /* cleanup the InternalDrag struct */
1648 InternalDrag.hwnd = 0;
1649 if (InternalDrag.himl != InternalDrag.himlNoCursor)
1650 ImageList_Destroy (InternalDrag.himlNoCursor);
1651 ImageList_Destroy (InternalDrag.himl);
1652 InternalDrag.himlNoCursor = InternalDrag.himl = 0;
1653 InternalDrag.x= 0;
1654 InternalDrag.y= 0;
1655 InternalDrag.dxHotspot = 0;
1656 InternalDrag.dyHotspot = 0;
1657 InternalDrag.bShow = FALSE;
1658 DeleteObject(InternalDrag.hbmBg);
1659 InternalDrag.hbmBg = 0;
1663 /*************************************************************************
1664 * ImageList_GetBkColor [COMCTL32.@]
1666 * Returns the background color of an image list.
1668 * PARAMS
1669 * himl [I] Image list handle.
1671 * RETURNS
1672 * Success: background color
1673 * Failure: CLR_NONE
1676 COLORREF WINAPI
1677 ImageList_GetBkColor (HIMAGELIST himl)
1679 return himl ? himl->clrBk : CLR_NONE;
1683 /*************************************************************************
1684 * ImageList_GetDragImage [COMCTL32.@]
1686 * Returns the handle to the internal drag image list.
1688 * PARAMS
1689 * ppt [O] Pointer to the drag position. Can be NULL.
1690 * pptHotspot [O] Pointer to the position of the hot spot. Can be NULL.
1692 * RETURNS
1693 * Success: Handle of the drag image list.
1694 * Failure: NULL.
1697 HIMAGELIST WINAPI
1698 ImageList_GetDragImage (POINT *ppt, POINT *pptHotspot)
1700 if (is_valid(InternalDrag.himl)) {
1701 if (ppt) {
1702 ppt->x = InternalDrag.x;
1703 ppt->y = InternalDrag.y;
1705 if (pptHotspot) {
1706 pptHotspot->x = InternalDrag.dxHotspot;
1707 pptHotspot->y = InternalDrag.dyHotspot;
1709 return (InternalDrag.himl);
1712 return NULL;
1716 /*************************************************************************
1717 * ImageList_GetFlags [COMCTL32.@]
1719 * Gets the flags of the specified image list.
1721 * PARAMS
1722 * himl [I] Handle to image list
1724 * RETURNS
1725 * Image list flags.
1727 * BUGS
1728 * Stub.
1731 DWORD WINAPI
1732 ImageList_GetFlags(HIMAGELIST himl)
1734 TRACE("%p\n", himl);
1736 return is_valid(himl) ? himl->flags : 0;
1740 /*************************************************************************
1741 * ImageList_GetIcon [COMCTL32.@]
1743 * Creates an icon from a masked image of an image list.
1745 * PARAMS
1746 * himl [I] handle to image list
1747 * i [I] image index
1748 * flags [I] drawing style flags
1750 * RETURNS
1751 * Success: icon handle
1752 * Failure: NULL
1755 HICON WINAPI
1756 ImageList_GetIcon (HIMAGELIST himl, INT i, UINT fStyle)
1758 ICONINFO ii;
1759 HICON hIcon;
1760 HBITMAP hOldDstBitmap;
1761 HDC hdcDst;
1762 POINT pt;
1764 TRACE("%p %d %d\n", himl, i, fStyle);
1765 if (!is_valid(himl) || (i < 0) || (i >= himl->cCurImage)) return NULL;
1767 ii.fIcon = TRUE;
1768 ii.xHotspot = 0;
1769 ii.yHotspot = 0;
1771 /* create colour bitmap */
1772 hdcDst = GetDC(0);
1773 ii.hbmColor = CreateCompatibleBitmap(hdcDst, himl->cx, himl->cy);
1774 ReleaseDC(0, hdcDst);
1776 hdcDst = CreateCompatibleDC(0);
1778 imagelist_point_from_index( himl, i, &pt );
1780 /* draw mask*/
1781 ii.hbmMask = CreateBitmap (himl->cx, himl->cy, 1, 1, NULL);
1782 hOldDstBitmap = SelectObject (hdcDst, ii.hbmMask);
1783 if (himl->hbmMask) {
1784 BitBlt (hdcDst, 0, 0, himl->cx, himl->cy,
1785 himl->hdcMask, pt.x, pt.y, SRCCOPY);
1787 else
1788 PatBlt (hdcDst, 0, 0, himl->cx, himl->cy, BLACKNESS);
1790 /* draw image*/
1791 SelectObject (hdcDst, ii.hbmColor);
1792 BitBlt (hdcDst, 0, 0, himl->cx, himl->cy,
1793 himl->hdcImage, pt.x, pt.y, SRCCOPY);
1796 * CreateIconIndirect requires us to deselect the bitmaps from
1797 * the DCs before calling
1799 SelectObject(hdcDst, hOldDstBitmap);
1801 hIcon = CreateIconIndirect (&ii);
1803 DeleteObject (ii.hbmMask);
1804 DeleteObject (ii.hbmColor);
1805 DeleteDC (hdcDst);
1807 return hIcon;
1811 /*************************************************************************
1812 * ImageList_GetIconSize [COMCTL32.@]
1814 * Retrieves the size of an image in an image list.
1816 * PARAMS
1817 * himl [I] handle to image list
1818 * cx [O] pointer to the image width.
1819 * cy [O] pointer to the image height.
1821 * RETURNS
1822 * Success: TRUE
1823 * Failure: FALSE
1825 * NOTES
1826 * All images in an image list have the same size.
1829 BOOL WINAPI
1830 ImageList_GetIconSize (HIMAGELIST himl, INT *cx, INT *cy)
1832 if (!is_valid(himl) || !cx || !cy)
1833 return FALSE;
1834 if ((himl->cx <= 0) || (himl->cy <= 0))
1835 return FALSE;
1837 *cx = himl->cx;
1838 *cy = himl->cy;
1840 return TRUE;
1844 /*************************************************************************
1845 * ImageList_GetImageCount [COMCTL32.@]
1847 * Returns the number of images in an image list.
1849 * PARAMS
1850 * himl [I] handle to image list
1852 * RETURNS
1853 * Success: Number of images.
1854 * Failure: 0
1857 INT WINAPI
1858 ImageList_GetImageCount (HIMAGELIST himl)
1860 if (!is_valid(himl))
1861 return 0;
1863 return himl->cCurImage;
1867 /*************************************************************************
1868 * ImageList_GetImageInfo [COMCTL32.@]
1870 * Returns information about an image in an image list.
1872 * PARAMS
1873 * himl [I] handle to image list
1874 * i [I] image index
1875 * pImageInfo [O] pointer to the image information
1877 * RETURNS
1878 * Success: TRUE
1879 * Failure: FALSE
1882 BOOL WINAPI
1883 ImageList_GetImageInfo (HIMAGELIST himl, INT i, IMAGEINFO *pImageInfo)
1885 POINT pt;
1887 if (!is_valid(himl) || (pImageInfo == NULL))
1888 return FALSE;
1889 if ((i < 0) || (i >= himl->cCurImage))
1890 return FALSE;
1892 pImageInfo->hbmImage = himl->hbmImage;
1893 pImageInfo->hbmMask = himl->hbmMask;
1895 imagelist_point_from_index( himl, i, &pt );
1896 pImageInfo->rcImage.top = pt.y;
1897 pImageInfo->rcImage.bottom = pt.y + himl->cy;
1898 pImageInfo->rcImage.left = pt.x;
1899 pImageInfo->rcImage.right = pt.x + himl->cx;
1901 return TRUE;
1905 /*************************************************************************
1906 * ImageList_GetImageRect [COMCTL32.@]
1908 * Retrieves the rectangle of the specified image in an image list.
1910 * PARAMS
1911 * himl [I] handle to image list
1912 * i [I] image index
1913 * lpRect [O] pointer to the image rectangle
1915 * RETURNS
1916 * Success: TRUE
1917 * Failure: FALSE
1919 * NOTES
1920 * This is an UNDOCUMENTED function!!!
1923 BOOL WINAPI
1924 ImageList_GetImageRect (HIMAGELIST himl, INT i, LPRECT lpRect)
1926 POINT pt;
1928 if (!is_valid(himl) || (lpRect == NULL))
1929 return FALSE;
1930 if ((i < 0) || (i >= himl->cCurImage))
1931 return FALSE;
1933 imagelist_point_from_index( himl, i, &pt );
1934 lpRect->left = pt.x;
1935 lpRect->top = pt.y;
1936 lpRect->right = pt.x + himl->cx;
1937 lpRect->bottom = pt.y + himl->cy;
1939 return TRUE;
1943 /*************************************************************************
1944 * ImageList_LoadImage [COMCTL32.@]
1945 * ImageList_LoadImageA [COMCTL32.@]
1947 * Creates an image list from a bitmap, icon or cursor.
1949 * See ImageList_LoadImageW.
1952 HIMAGELIST WINAPI
1953 ImageList_LoadImageA (HINSTANCE hi, LPCSTR lpbmp, INT cx, INT cGrow,
1954 COLORREF clrMask, UINT uType, UINT uFlags)
1956 HIMAGELIST himl;
1957 LPWSTR lpbmpW;
1958 DWORD len;
1960 if (IS_INTRESOURCE(lpbmp))
1961 return ImageList_LoadImageW(hi, (LPCWSTR)lpbmp, cx, cGrow, clrMask,
1962 uType, uFlags);
1964 len = MultiByteToWideChar(CP_ACP, 0, lpbmp, -1, NULL, 0);
1965 lpbmpW = Alloc(len * sizeof(WCHAR));
1966 MultiByteToWideChar(CP_ACP, 0, lpbmp, -1, lpbmpW, len);
1968 himl = ImageList_LoadImageW(hi, lpbmpW, cx, cGrow, clrMask, uType, uFlags);
1969 Free (lpbmpW);
1970 return himl;
1974 /*************************************************************************
1975 * ImageList_LoadImageW [COMCTL32.@]
1977 * Creates an image list from a bitmap, icon or cursor.
1979 * PARAMS
1980 * hi [I] instance handle
1981 * lpbmp [I] name or id of the image
1982 * cx [I] width of each image
1983 * cGrow [I] number of images to expand
1984 * clrMask [I] mask color
1985 * uType [I] type of image to load
1986 * uFlags [I] loading flags
1988 * RETURNS
1989 * Success: handle to the loaded image list
1990 * Failure: NULL
1992 * SEE
1993 * LoadImage ()
1996 HIMAGELIST WINAPI
1997 ImageList_LoadImageW (HINSTANCE hi, LPCWSTR lpbmp, INT cx, INT cGrow,
1998 COLORREF clrMask, UINT uType, UINT uFlags)
2000 HIMAGELIST himl = NULL;
2001 HANDLE handle;
2002 INT nImageCount;
2004 handle = LoadImageW (hi, lpbmp, uType, 0, 0, uFlags);
2005 if (!handle) {
2006 WARN("Couldn't load image\n");
2007 return NULL;
2010 if (uType == IMAGE_BITMAP) {
2011 DIBSECTION dib;
2012 UINT color;
2014 if (GetObjectW (handle, sizeof(dib), &dib) == sizeof(BITMAP)) color = ILC_COLOR;
2015 else color = dib.dsBm.bmBitsPixel;
2017 /* To match windows behavior, if cx is set to zero and
2018 the flag DI_DEFAULTSIZE is specified, cx becomes the
2019 system metric value for icons. If the flag is not specified
2020 the function sets the size to the height of the bitmap */
2021 if (cx == 0)
2023 if (uFlags & DI_DEFAULTSIZE)
2024 cx = GetSystemMetrics (SM_CXICON);
2025 else
2026 cx = dib.dsBm.bmHeight;
2029 nImageCount = dib.dsBm.bmWidth / cx;
2031 himl = ImageList_Create (cx, dib.dsBm.bmHeight, ILC_MASK | color, nImageCount, cGrow);
2032 if (!himl) {
2033 DeleteObject (handle);
2034 return NULL;
2036 ImageList_AddMasked (himl, handle, clrMask);
2038 else if ((uType == IMAGE_ICON) || (uType == IMAGE_CURSOR)) {
2039 ICONINFO ii;
2040 BITMAP bmp;
2042 GetIconInfo (handle, &ii);
2043 GetObjectW (ii.hbmColor, sizeof(BITMAP), &bmp);
2044 himl = ImageList_Create (bmp.bmWidth, bmp.bmHeight,
2045 ILC_MASK | ILC_COLOR, 1, cGrow);
2046 if (!himl) {
2047 DeleteObject (ii.hbmColor);
2048 DeleteObject (ii.hbmMask);
2049 DeleteObject (handle);
2050 return NULL;
2052 ImageList_Add (himl, ii.hbmColor, ii.hbmMask);
2053 DeleteObject (ii.hbmColor);
2054 DeleteObject (ii.hbmMask);
2057 DeleteObject (handle);
2059 return himl;
2063 /*************************************************************************
2064 * ImageList_Merge [COMCTL32.@]
2066 * Create an image list containing a merged image from two image lists.
2068 * PARAMS
2069 * himl1 [I] handle to first image list
2070 * i1 [I] first image index
2071 * himl2 [I] handle to second image list
2072 * i2 [I] second image index
2073 * dx [I] X offset of the second image relative to the first.
2074 * dy [I] Y offset of the second image relative to the first.
2076 * RETURNS
2077 * Success: The newly created image list. It contains a single image
2078 * consisting of the second image merged with the first.
2079 * Failure: NULL, if either himl1 or himl2 is invalid.
2081 * NOTES
2082 * - The returned image list should be deleted by the caller using
2083 * ImageList_Destroy() when it is no longer required.
2084 * - If either i1 or i2 is not a valid image index, they will be treated
2085 * as blank images.
2087 HIMAGELIST WINAPI
2088 ImageList_Merge (HIMAGELIST himl1, INT i1, HIMAGELIST himl2, INT i2,
2089 INT dx, INT dy)
2091 HIMAGELIST himlDst = NULL;
2092 INT cxDst, cyDst;
2093 INT xOff1, yOff1, xOff2, yOff2;
2094 POINT pt1, pt2;
2095 INT newFlags;
2097 TRACE("(himl1=%p i1=%d himl2=%p i2=%d dx=%d dy=%d)\n", himl1, i1, himl2,
2098 i2, dx, dy);
2100 if (!is_valid(himl1) || !is_valid(himl2))
2101 return NULL;
2103 if (dx > 0) {
2104 cxDst = max (himl1->cx, dx + himl2->cx);
2105 xOff1 = 0;
2106 xOff2 = dx;
2108 else if (dx < 0) {
2109 cxDst = max (himl2->cx, himl1->cx - dx);
2110 xOff1 = -dx;
2111 xOff2 = 0;
2113 else {
2114 cxDst = max (himl1->cx, himl2->cx);
2115 xOff1 = 0;
2116 xOff2 = 0;
2119 if (dy > 0) {
2120 cyDst = max (himl1->cy, dy + himl2->cy);
2121 yOff1 = 0;
2122 yOff2 = dy;
2124 else if (dy < 0) {
2125 cyDst = max (himl2->cy, himl1->cy - dy);
2126 yOff1 = -dy;
2127 yOff2 = 0;
2129 else {
2130 cyDst = max (himl1->cy, himl2->cy);
2131 yOff1 = 0;
2132 yOff2 = 0;
2135 newFlags = (himl1->flags > himl2->flags ? himl1->flags : himl2->flags) & ILC_COLORDDB;
2136 if (newFlags == ILC_COLORDDB && (himl1->flags & ILC_COLORDDB) == ILC_COLOR16)
2137 newFlags = ILC_COLOR16; /* this is what native (at least v5) does, don't know why */
2138 himlDst = ImageList_Create (cxDst, cyDst, ILC_MASK | newFlags, 1, 1);
2140 if (himlDst)
2142 imagelist_point_from_index( himl1, i1, &pt1 );
2143 imagelist_point_from_index( himl2, i2, &pt2 );
2145 /* copy image */
2146 BitBlt (himlDst->hdcImage, 0, 0, cxDst, cyDst, himl1->hdcImage, 0, 0, BLACKNESS);
2147 if (i1 >= 0 && i1 < himl1->cCurImage)
2148 BitBlt (himlDst->hdcImage, xOff1, yOff1, himl1->cx, himl1->cy, himl1->hdcImage, pt1.x, pt1.y, SRCCOPY);
2149 if (i2 >= 0 && i2 < himl2->cCurImage)
2151 if (himl2->flags & ILC_MASK)
2153 BitBlt (himlDst->hdcImage, xOff2, yOff2, himl2->cx, himl2->cy, himl2->hdcMask , pt2.x, pt2.y, SRCAND);
2154 BitBlt (himlDst->hdcImage, xOff2, yOff2, himl2->cx, himl2->cy, himl2->hdcImage, pt2.x, pt2.y, SRCPAINT);
2156 else
2157 BitBlt (himlDst->hdcImage, xOff2, yOff2, himl2->cx, himl2->cy, himl2->hdcImage, pt2.x, pt2.y, SRCCOPY);
2160 /* copy mask */
2161 BitBlt (himlDst->hdcMask, 0, 0, cxDst, cyDst, himl1->hdcMask, 0, 0, WHITENESS);
2162 if (i1 >= 0 && i1 < himl1->cCurImage)
2163 BitBlt (himlDst->hdcMask, xOff1, yOff1, himl1->cx, himl1->cy, himl1->hdcMask, pt1.x, pt1.y, SRCCOPY);
2164 if (i2 >= 0 && i2 < himl2->cCurImage)
2165 BitBlt (himlDst->hdcMask, xOff2, yOff2, himl2->cx, himl2->cy, himl2->hdcMask, pt2.x, pt2.y, SRCAND);
2167 himlDst->cCurImage = 1;
2170 return himlDst;
2174 /* helper for ImageList_Read, see comments below */
2175 static void *read_bitmap(LPSTREAM pstm, BITMAPINFO *bmi)
2177 BITMAPFILEHEADER bmfh;
2178 int bitsperpixel, palspace;
2179 void *bits;
2181 if (FAILED(IStream_Read ( pstm, &bmfh, sizeof(bmfh), NULL)))
2182 return NULL;
2184 if (bmfh.bfType != (('M'<<8)|'B'))
2185 return NULL;
2187 if (FAILED(IStream_Read ( pstm, &bmi->bmiHeader, sizeof(bmi->bmiHeader), NULL)))
2188 return NULL;
2190 if ((bmi->bmiHeader.biSize != sizeof(bmi->bmiHeader)))
2191 return NULL;
2193 TRACE("width %u, height %u, planes %u, bpp %u\n",
2194 bmi->bmiHeader.biWidth, bmi->bmiHeader.biHeight,
2195 bmi->bmiHeader.biPlanes, bmi->bmiHeader.biBitCount);
2197 bitsperpixel = bmi->bmiHeader.biPlanes * bmi->bmiHeader.biBitCount;
2198 if (bitsperpixel<=8)
2199 palspace = (1<<bitsperpixel)*sizeof(RGBQUAD);
2200 else
2201 palspace = 0;
2203 bmi->bmiHeader.biSizeImage = get_dib_image_size( bmi );
2205 /* read the palette right after the end of the bitmapinfoheader */
2206 if (palspace && FAILED(IStream_Read(pstm, bmi->bmiColors, palspace, NULL)))
2207 return NULL;
2209 bits = Alloc(bmi->bmiHeader.biSizeImage);
2210 if (!bits) return NULL;
2212 if (FAILED(IStream_Read(pstm, bits, bmi->bmiHeader.biSizeImage, NULL)))
2214 Free(bits);
2215 return NULL;
2217 return bits;
2220 /*************************************************************************
2221 * ImageList_Read [COMCTL32.@]
2223 * Reads an image list from a stream.
2225 * PARAMS
2226 * pstm [I] pointer to a stream
2228 * RETURNS
2229 * Success: handle to image list
2230 * Failure: NULL
2232 * The format is like this:
2233 * ILHEAD ilheadstruct;
2235 * for the color image part:
2236 * BITMAPFILEHEADER bmfh;
2237 * BITMAPINFOHEADER bmih;
2238 * only if it has a palette:
2239 * RGBQUAD rgbs[nr_of_paletted_colors];
2241 * BYTE colorbits[imagesize];
2243 * the following only if the ILC_MASK bit is set in ILHEAD.ilFlags:
2244 * BITMAPFILEHEADER bmfh_mask;
2245 * BITMAPINFOHEADER bmih_mask;
2246 * only if it has a palette (it usually does not):
2247 * RGBQUAD rgbs[nr_of_paletted_colors];
2249 * BYTE maskbits[imagesize];
2251 HIMAGELIST WINAPI ImageList_Read (LPSTREAM pstm)
2253 char image_buf[sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * 256];
2254 char mask_buf[sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * 256];
2255 BITMAPINFO *image_info = (BITMAPINFO *)image_buf;
2256 BITMAPINFO *mask_info = (BITMAPINFO *)mask_buf;
2257 void *image_bits, *mask_bits = NULL;
2258 ILHEAD ilHead;
2259 HIMAGELIST himl;
2260 unsigned int i;
2262 TRACE("%p\n", pstm);
2264 if (FAILED(IStream_Read (pstm, &ilHead, sizeof(ILHEAD), NULL)))
2265 return NULL;
2266 if (ilHead.usMagic != (('L' << 8) | 'I'))
2267 return NULL;
2268 if (ilHead.usVersion != 0x101) /* probably version? */
2269 return NULL;
2271 TRACE("cx %u, cy %u, flags 0x%04x, cCurImage %u, cMaxImage %u\n",
2272 ilHead.cx, ilHead.cy, ilHead.flags, ilHead.cCurImage, ilHead.cMaxImage);
2274 himl = ImageList_Create(ilHead.cx, ilHead.cy, ilHead.flags, ilHead.cCurImage, ilHead.cMaxImage);
2275 if (!himl)
2276 return NULL;
2278 if (!(image_bits = read_bitmap(pstm, image_info)))
2280 WARN("failed to read bitmap from stream\n");
2281 return NULL;
2283 if (ilHead.flags & ILC_MASK)
2285 if (!(mask_bits = read_bitmap(pstm, mask_info)))
2287 WARN("failed to read mask bitmap from stream\n");
2288 return NULL;
2291 else mask_info = NULL;
2293 if (himl->has_alpha && image_info->bmiHeader.biBitCount == 32)
2295 DWORD *ptr = image_bits;
2296 BYTE *mask_ptr = mask_bits;
2297 int stride = himl->cy * image_info->bmiHeader.biWidth;
2299 if (image_info->bmiHeader.biHeight > 0) /* bottom-up */
2301 ptr += image_info->bmiHeader.biHeight * image_info->bmiHeader.biWidth - stride;
2302 mask_ptr += (image_info->bmiHeader.biHeight * image_info->bmiHeader.biWidth - stride) / 8;
2303 stride = -stride;
2304 image_info->bmiHeader.biHeight = himl->cy;
2306 else image_info->bmiHeader.biHeight = -himl->cy;
2308 for (i = 0; i < ilHead.cCurImage; i += TILE_COUNT)
2310 add_dib_bits( himl, i, min( ilHead.cCurImage - i, TILE_COUNT ),
2311 himl->cx, himl->cy, image_info, mask_info, ptr, mask_ptr );
2312 ptr += stride;
2313 mask_ptr += stride / 8;
2316 else
2318 StretchDIBits( himl->hdcImage, 0, 0, image_info->bmiHeader.biWidth, image_info->bmiHeader.biHeight,
2319 0, 0, image_info->bmiHeader.biWidth, image_info->bmiHeader.biHeight,
2320 image_bits, image_info, DIB_RGB_COLORS, SRCCOPY);
2321 if (mask_info)
2322 StretchDIBits( himl->hdcMask, 0, 0, mask_info->bmiHeader.biWidth, mask_info->bmiHeader.biHeight,
2323 0, 0, mask_info->bmiHeader.biWidth, mask_info->bmiHeader.biHeight,
2324 mask_bits, mask_info, DIB_RGB_COLORS, SRCCOPY);
2326 Free( image_bits );
2327 Free( mask_bits );
2329 himl->cCurImage = ilHead.cCurImage;
2330 himl->cMaxImage = ilHead.cMaxImage;
2332 ImageList_SetBkColor(himl,ilHead.bkcolor);
2333 for (i=0;i<4;i++)
2334 ImageList_SetOverlayImage(himl,ilHead.ovls[i],i+1);
2335 return himl;
2339 /*************************************************************************
2340 * ImageList_Remove [COMCTL32.@]
2342 * Removes an image from an image list
2344 * PARAMS
2345 * himl [I] image list handle
2346 * i [I] image index
2348 * RETURNS
2349 * Success: TRUE
2350 * Failure: FALSE
2352 * FIXME: as the image list storage test shows, native comctl32 simply shifts
2353 * images without creating a new bitmap.
2355 BOOL WINAPI
2356 ImageList_Remove (HIMAGELIST himl, INT i)
2358 HBITMAP hbmNewImage, hbmNewMask;
2359 HDC hdcBmp;
2360 SIZE sz;
2362 TRACE("(himl=%p i=%d)\n", himl, i);
2364 if (!is_valid(himl)) {
2365 ERR("Invalid image list handle!\n");
2366 return FALSE;
2369 if ((i < -1) || (i >= himl->cCurImage)) {
2370 TRACE("index out of range! %d\n", i);
2371 return FALSE;
2374 if (i == -1) {
2375 INT nCount;
2377 /* remove all */
2378 if (himl->cCurImage == 0) {
2379 /* remove all on empty ImageList is allowed */
2380 TRACE("remove all on empty ImageList!\n");
2381 return TRUE;
2384 himl->cMaxImage = himl->cGrow;
2385 himl->cCurImage = 0;
2386 for (nCount = 0; nCount < MAX_OVERLAYIMAGE; nCount++)
2387 himl->nOvlIdx[nCount] = -1;
2389 if (himl->has_alpha)
2391 HeapFree( GetProcessHeap(), 0, himl->has_alpha );
2392 himl->has_alpha = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, himl->cMaxImage );
2395 hbmNewImage = ImageList_CreateImage(himl->hdcImage, himl, himl->cMaxImage);
2396 SelectObject (himl->hdcImage, hbmNewImage);
2397 DeleteObject (himl->hbmImage);
2398 himl->hbmImage = hbmNewImage;
2400 if (himl->hbmMask) {
2402 imagelist_get_bitmap_size(himl, himl->cMaxImage, &sz);
2403 hbmNewMask = CreateBitmap (sz.cx, sz.cy, 1, 1, NULL);
2404 SelectObject (himl->hdcMask, hbmNewMask);
2405 DeleteObject (himl->hbmMask);
2406 himl->hbmMask = hbmNewMask;
2409 else {
2410 /* delete one image */
2411 TRACE("Remove single image! %d\n", i);
2413 /* create new bitmap(s) */
2414 TRACE(" - Number of images: %d / %d (Old/New)\n",
2415 himl->cCurImage, himl->cCurImage - 1);
2417 hbmNewImage = ImageList_CreateImage(himl->hdcImage, himl, himl->cMaxImage);
2419 imagelist_get_bitmap_size(himl, himl->cMaxImage, &sz );
2420 if (himl->hbmMask)
2421 hbmNewMask = CreateBitmap (sz.cx, sz.cy, 1, 1, NULL);
2422 else
2423 hbmNewMask = 0; /* Just to keep compiler happy! */
2425 hdcBmp = CreateCompatibleDC (0);
2427 /* copy all images and masks prior to the "removed" image */
2428 if (i > 0) {
2429 TRACE("Pre image copy: Copy %d images\n", i);
2431 SelectObject (hdcBmp, hbmNewImage);
2432 imagelist_copy_images( himl, himl->hdcImage, hdcBmp, 0, i, 0 );
2434 if (himl->hbmMask) {
2435 SelectObject (hdcBmp, hbmNewMask);
2436 imagelist_copy_images( himl, himl->hdcMask, hdcBmp, 0, i, 0 );
2440 /* copy all images and masks behind the removed image */
2441 if (i < himl->cCurImage - 1) {
2442 TRACE("Post image copy!\n");
2444 SelectObject (hdcBmp, hbmNewImage);
2445 imagelist_copy_images( himl, himl->hdcImage, hdcBmp, i + 1,
2446 (himl->cCurImage - i), i );
2448 if (himl->hbmMask) {
2449 SelectObject (hdcBmp, hbmNewMask);
2450 imagelist_copy_images( himl, himl->hdcMask, hdcBmp, i + 1,
2451 (himl->cCurImage - i), i );
2455 DeleteDC (hdcBmp);
2457 /* delete old images and insert new ones */
2458 SelectObject (himl->hdcImage, hbmNewImage);
2459 DeleteObject (himl->hbmImage);
2460 himl->hbmImage = hbmNewImage;
2461 if (himl->hbmMask) {
2462 SelectObject (himl->hdcMask, hbmNewMask);
2463 DeleteObject (himl->hbmMask);
2464 himl->hbmMask = hbmNewMask;
2467 himl->cCurImage--;
2470 return TRUE;
2474 /*************************************************************************
2475 * ImageList_Replace [COMCTL32.@]
2477 * Replaces an image in an image list with a new image.
2479 * PARAMS
2480 * himl [I] handle to image list
2481 * i [I] image index
2482 * hbmImage [I] handle to image bitmap
2483 * hbmMask [I] handle to mask bitmap. Can be NULL.
2485 * RETURNS
2486 * Success: TRUE
2487 * Failure: FALSE
2490 BOOL WINAPI
2491 ImageList_Replace (HIMAGELIST himl, INT i, HBITMAP hbmImage,
2492 HBITMAP hbmMask)
2494 HDC hdcImage;
2495 BITMAP bmp;
2496 POINT pt;
2498 TRACE("%p %d %p %p\n", himl, i, hbmImage, hbmMask);
2500 if (!is_valid(himl)) {
2501 ERR("Invalid image list handle!\n");
2502 return FALSE;
2505 if ((i >= himl->cMaxImage) || (i < 0)) {
2506 ERR("Invalid image index!\n");
2507 return FALSE;
2510 if (!GetObjectW(hbmImage, sizeof(BITMAP), &bmp))
2511 return FALSE;
2513 hdcImage = CreateCompatibleDC (0);
2515 /* Replace Image */
2516 SelectObject (hdcImage, hbmImage);
2518 if (add_with_alpha( himl, hdcImage, i, 1, bmp.bmWidth, bmp.bmHeight, hbmImage, hbmMask ))
2519 goto done;
2521 imagelist_point_from_index(himl, i, &pt);
2522 StretchBlt (himl->hdcImage, pt.x, pt.y, himl->cx, himl->cy,
2523 hdcImage, 0, 0, bmp.bmWidth, bmp.bmHeight, SRCCOPY);
2525 if (himl->hbmMask)
2527 HDC hdcTemp;
2528 HBITMAP hOldBitmapTemp;
2530 hdcTemp = CreateCompatibleDC(0);
2531 hOldBitmapTemp = SelectObject(hdcTemp, hbmMask);
2533 StretchBlt (himl->hdcMask, pt.x, pt.y, himl->cx, himl->cy,
2534 hdcTemp, 0, 0, bmp.bmWidth, bmp.bmHeight, SRCCOPY);
2535 SelectObject(hdcTemp, hOldBitmapTemp);
2536 DeleteDC(hdcTemp);
2538 /* Remove the background from the image
2540 BitBlt (himl->hdcImage, pt.x, pt.y, bmp.bmWidth, bmp.bmHeight,
2541 himl->hdcMask, pt.x, pt.y, 0x220326); /* NOTSRCAND */
2544 done:
2545 DeleteDC (hdcImage);
2547 return TRUE;
2551 /*************************************************************************
2552 * ImageList_ReplaceIcon [COMCTL32.@]
2554 * Replaces an image in an image list using an icon.
2556 * PARAMS
2557 * himl [I] handle to image list
2558 * i [I] image index
2559 * hIcon [I] handle to icon
2561 * RETURNS
2562 * Success: index of the replaced image
2563 * Failure: -1
2566 INT WINAPI
2567 ImageList_ReplaceIcon (HIMAGELIST himl, INT nIndex, HICON hIcon)
2569 HICON hBestFitIcon;
2570 ICONINFO ii;
2571 BITMAP bmp;
2572 BOOL ret;
2573 POINT pt;
2575 TRACE("(%p %d %p)\n", himl, nIndex, hIcon);
2577 if (!is_valid(himl)) {
2578 ERR("invalid image list\n");
2579 return -1;
2581 if ((nIndex >= himl->cMaxImage) || (nIndex < -1)) {
2582 ERR("invalid image index %d / %d\n", nIndex, himl->cMaxImage);
2583 return -1;
2586 hBestFitIcon = CopyImage(
2587 hIcon, IMAGE_ICON,
2588 himl->cx, himl->cy,
2589 LR_COPYFROMRESOURCE);
2590 /* the above will fail if the icon wasn't loaded from a resource, so try
2591 * again without LR_COPYFROMRESOURCE flag */
2592 if (!hBestFitIcon)
2593 hBestFitIcon = CopyImage(
2594 hIcon, IMAGE_ICON,
2595 himl->cx, himl->cy,
2597 if (!hBestFitIcon)
2598 return -1;
2600 if (nIndex == -1) {
2601 if (himl->cCurImage + 1 >= himl->cMaxImage)
2602 IMAGELIST_InternalExpandBitmaps(himl, 1);
2604 nIndex = himl->cCurImage;
2605 himl->cCurImage++;
2608 if (himl->has_alpha && GetIconInfo (hBestFitIcon, &ii))
2610 HDC hdcImage = CreateCompatibleDC( 0 );
2611 GetObjectW (ii.hbmMask, sizeof(BITMAP), &bmp);
2613 if (!ii.hbmColor)
2615 UINT height = bmp.bmHeight / 2;
2616 HDC hdcMask = CreateCompatibleDC( 0 );
2617 HBITMAP color = CreateBitmap( bmp.bmWidth, height, 1, 1, NULL );
2618 SelectObject( hdcImage, color );
2619 SelectObject( hdcMask, ii.hbmMask );
2620 BitBlt( hdcImage, 0, 0, bmp.bmWidth, height, hdcMask, 0, height, SRCCOPY );
2621 ret = add_with_alpha( himl, hdcImage, nIndex, 1, bmp.bmWidth, height, color, ii.hbmMask );
2622 DeleteDC( hdcMask );
2623 DeleteObject( color );
2625 else ret = add_with_alpha( himl, hdcImage, nIndex, 1, bmp.bmWidth, bmp.bmHeight,
2626 ii.hbmColor, ii.hbmMask );
2628 DeleteDC( hdcImage );
2629 DeleteObject (ii.hbmMask);
2630 if (ii.hbmColor) DeleteObject (ii.hbmColor);
2631 if (ret) goto done;
2634 imagelist_point_from_index(himl, nIndex, &pt);
2636 if (himl->hbmMask)
2638 DrawIconEx( himl->hdcImage, pt.x, pt.y, hBestFitIcon, himl->cx, himl->cy, 0, 0, DI_IMAGE );
2639 PatBlt( himl->hdcMask, pt.x, pt.y, himl->cx, himl->cy, WHITENESS );
2640 DrawIconEx( himl->hdcMask, pt.x, pt.y, hBestFitIcon, himl->cx, himl->cy, 0, 0, DI_MASK );
2642 else
2644 COLORREF color = himl->clrBk != CLR_NONE ? himl->clrBk : comctl32_color.clrWindow;
2645 HBRUSH brush = CreateSolidBrush( GetNearestColor( himl->hdcImage, color ));
2647 SelectObject( himl->hdcImage, brush );
2648 PatBlt( himl->hdcImage, pt.x, pt.y, himl->cx, himl->cy, PATCOPY );
2649 SelectObject( himl->hdcImage, GetStockObject(BLACK_BRUSH) );
2650 DeleteObject( brush );
2651 DrawIconEx( himl->hdcImage, pt.x, pt.y, hBestFitIcon, himl->cx, himl->cy, 0, 0, DI_NORMAL );
2654 done:
2655 DestroyIcon(hBestFitIcon);
2657 TRACE("Insert index = %d, himl->cCurImage = %d\n", nIndex, himl->cCurImage);
2658 return nIndex;
2662 /*************************************************************************
2663 * ImageList_SetBkColor [COMCTL32.@]
2665 * Sets the background color of an image list.
2667 * PARAMS
2668 * himl [I] handle to image list
2669 * clrBk [I] background color
2671 * RETURNS
2672 * Success: previous background color
2673 * Failure: CLR_NONE
2676 COLORREF WINAPI
2677 ImageList_SetBkColor (HIMAGELIST himl, COLORREF clrBk)
2679 COLORREF clrOldBk;
2681 if (!is_valid(himl))
2682 return CLR_NONE;
2684 clrOldBk = himl->clrBk;
2685 himl->clrBk = clrBk;
2686 return clrOldBk;
2690 /*************************************************************************
2691 * ImageList_SetDragCursorImage [COMCTL32.@]
2693 * Combines the specified image with the current drag image
2695 * PARAMS
2696 * himlDrag [I] handle to drag image list
2697 * iDrag [I] drag image index
2698 * dxHotspot [I] X position of the hot spot
2699 * dyHotspot [I] Y position of the hot spot
2701 * RETURNS
2702 * Success: TRUE
2703 * Failure: FALSE
2705 * NOTES
2706 * - The names dxHotspot, dyHotspot are misleading because they have nothing
2707 * to do with a hotspot but are only the offset of the origin of the new
2708 * image relative to the origin of the old image.
2710 * - When this function is called and the drag image is visible, a
2711 * short flickering occurs but this matches the Win9x behavior. It is
2712 * possible to fix the flickering using code like in ImageList_DragMove.
2715 BOOL WINAPI
2716 ImageList_SetDragCursorImage (HIMAGELIST himlDrag, INT iDrag,
2717 INT dxHotspot, INT dyHotspot)
2719 HIMAGELIST himlTemp;
2720 BOOL visible;
2722 if (!is_valid(InternalDrag.himl) || !is_valid(himlDrag))
2723 return FALSE;
2725 TRACE(" dxH=%d dyH=%d nX=%d nY=%d\n",
2726 dxHotspot, dyHotspot, InternalDrag.dxHotspot, InternalDrag.dyHotspot);
2728 visible = InternalDrag.bShow;
2730 himlTemp = ImageList_Merge (InternalDrag.himlNoCursor, 0, himlDrag, iDrag,
2731 dxHotspot, dyHotspot);
2733 if (visible) {
2734 /* hide the drag image */
2735 ImageList_DragShowNolock(FALSE);
2737 if ((InternalDrag.himl->cx != himlTemp->cx) ||
2738 (InternalDrag.himl->cy != himlTemp->cy)) {
2739 /* the size of the drag image changed, invalidate the buffer */
2740 DeleteObject(InternalDrag.hbmBg);
2741 InternalDrag.hbmBg = 0;
2744 if (InternalDrag.himl != InternalDrag.himlNoCursor)
2745 ImageList_Destroy (InternalDrag.himl);
2746 InternalDrag.himl = himlTemp;
2748 if (visible) {
2749 /* show the drag image */
2750 ImageList_DragShowNolock(TRUE);
2753 return TRUE;
2757 /*************************************************************************
2758 * ImageList_SetFilter [COMCTL32.@]
2760 * Sets a filter (or does something completely different)!!???
2761 * It removes 12 Bytes from the stack (3 Parameters).
2763 * PARAMS
2764 * himl [I] SHOULD be a handle to image list
2765 * i [I] COULD be an index?
2766 * dwFilter [I] ???
2768 * RETURNS
2769 * Success: TRUE ???
2770 * Failure: FALSE ???
2772 * BUGS
2773 * This is an UNDOCUMENTED function!!!!
2774 * empty stub.
2777 BOOL WINAPI
2778 ImageList_SetFilter (HIMAGELIST himl, INT i, DWORD dwFilter)
2780 FIXME("(%p 0x%x 0x%x):empty stub!\n", himl, i, dwFilter);
2782 return FALSE;
2786 /*************************************************************************
2787 * ImageList_SetFlags [COMCTL32.@]
2789 * Sets the image list flags.
2791 * PARAMS
2792 * himl [I] Handle to image list
2793 * flags [I] Flags to set
2795 * RETURNS
2796 * Old flags?
2798 * BUGS
2799 * Stub.
2802 DWORD WINAPI
2803 ImageList_SetFlags(HIMAGELIST himl, DWORD flags)
2805 FIXME("(%p %08x):empty stub\n", himl, flags);
2806 return 0;
2810 /*************************************************************************
2811 * ImageList_SetIconSize [COMCTL32.@]
2813 * Sets the image size of the bitmap and deletes all images.
2815 * PARAMS
2816 * himl [I] handle to image list
2817 * cx [I] image width
2818 * cy [I] image height
2820 * RETURNS
2821 * Success: TRUE
2822 * Failure: FALSE
2825 BOOL WINAPI
2826 ImageList_SetIconSize (HIMAGELIST himl, INT cx, INT cy)
2828 INT nCount;
2829 HBITMAP hbmNew;
2831 if (!is_valid(himl))
2832 return FALSE;
2834 /* remove all images */
2835 himl->cMaxImage = himl->cInitial + 1;
2836 himl->cCurImage = 0;
2837 himl->cx = cx;
2838 himl->cy = cy;
2840 /* initialize overlay mask indices */
2841 for (nCount = 0; nCount < MAX_OVERLAYIMAGE; nCount++)
2842 himl->nOvlIdx[nCount] = -1;
2844 hbmNew = ImageList_CreateImage(himl->hdcImage, himl, himl->cMaxImage);
2845 SelectObject (himl->hdcImage, hbmNew);
2846 DeleteObject (himl->hbmImage);
2847 himl->hbmImage = hbmNew;
2849 if (himl->hbmMask) {
2850 SIZE sz;
2851 imagelist_get_bitmap_size(himl, himl->cMaxImage, &sz);
2852 hbmNew = CreateBitmap (sz.cx, sz.cy, 1, 1, NULL);
2853 SelectObject (himl->hdcMask, hbmNew);
2854 DeleteObject (himl->hbmMask);
2855 himl->hbmMask = hbmNew;
2858 return TRUE;
2862 /*************************************************************************
2863 * ImageList_SetImageCount [COMCTL32.@]
2865 * Resizes an image list to the specified number of images.
2867 * PARAMS
2868 * himl [I] handle to image list
2869 * iImageCount [I] number of images in the image list
2871 * RETURNS
2872 * Success: TRUE
2873 * Failure: FALSE
2876 BOOL WINAPI
2877 ImageList_SetImageCount (HIMAGELIST himl, UINT iImageCount)
2879 HDC hdcBitmap;
2880 HBITMAP hbmNewBitmap, hbmOld;
2881 INT nNewCount, nCopyCount;
2883 TRACE("%p %d\n",himl,iImageCount);
2885 if (!is_valid(himl))
2886 return FALSE;
2888 nNewCount = iImageCount + 1;
2889 nCopyCount = min(himl->cCurImage, iImageCount);
2891 hdcBitmap = CreateCompatibleDC (0);
2893 hbmNewBitmap = ImageList_CreateImage(hdcBitmap, himl, nNewCount);
2895 if (hbmNewBitmap != 0)
2897 hbmOld = SelectObject (hdcBitmap, hbmNewBitmap);
2898 imagelist_copy_images( himl, himl->hdcImage, hdcBitmap, 0, nCopyCount, 0 );
2899 SelectObject (hdcBitmap, hbmOld);
2901 /* FIXME: delete 'empty' image space? */
2903 SelectObject (himl->hdcImage, hbmNewBitmap);
2904 DeleteObject (himl->hbmImage);
2905 himl->hbmImage = hbmNewBitmap;
2907 else
2908 ERR("Could not create new image bitmap!\n");
2910 if (himl->hbmMask)
2912 SIZE sz;
2913 imagelist_get_bitmap_size( himl, nNewCount, &sz );
2914 hbmNewBitmap = CreateBitmap (sz.cx, sz.cy, 1, 1, NULL);
2915 if (hbmNewBitmap != 0)
2917 hbmOld = SelectObject (hdcBitmap, hbmNewBitmap);
2918 imagelist_copy_images( himl, himl->hdcMask, hdcBitmap, 0, nCopyCount, 0 );
2919 SelectObject (hdcBitmap, hbmOld);
2921 /* FIXME: delete 'empty' image space? */
2923 SelectObject (himl->hdcMask, hbmNewBitmap);
2924 DeleteObject (himl->hbmMask);
2925 himl->hbmMask = hbmNewBitmap;
2927 else
2928 ERR("Could not create new mask bitmap!\n");
2931 DeleteDC (hdcBitmap);
2933 if (himl->has_alpha)
2935 char *new_alpha = HeapReAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, himl->has_alpha, nNewCount );
2936 if (new_alpha) himl->has_alpha = new_alpha;
2937 else
2939 HeapFree( GetProcessHeap(), 0, himl->has_alpha );
2940 himl->has_alpha = NULL;
2944 /* Update max image count and current image count */
2945 himl->cMaxImage = nNewCount;
2946 himl->cCurImage = iImageCount;
2948 return TRUE;
2952 /*************************************************************************
2953 * ImageList_SetOverlayImage [COMCTL32.@]
2955 * Assigns an overlay mask index to an existing image in an image list.
2957 * PARAMS
2958 * himl [I] handle to image list
2959 * iImage [I] image index
2960 * iOverlay [I] overlay mask index
2962 * RETURNS
2963 * Success: TRUE
2964 * Failure: FALSE
2967 BOOL WINAPI
2968 ImageList_SetOverlayImage (HIMAGELIST himl, INT iImage, INT iOverlay)
2970 if (!is_valid(himl))
2971 return FALSE;
2972 if ((iOverlay < 1) || (iOverlay > MAX_OVERLAYIMAGE))
2973 return FALSE;
2974 if ((iImage!=-1) && ((iImage < 0) || (iImage > himl->cCurImage)))
2975 return FALSE;
2976 himl->nOvlIdx[iOverlay - 1] = iImage;
2977 return TRUE;
2982 /* helper for ImageList_Write - write bitmap to pstm
2983 * currently everything is written as 24 bit RGB, except masks
2985 static BOOL
2986 _write_bitmap(HBITMAP hBitmap, LPSTREAM pstm)
2988 LPBITMAPFILEHEADER bmfh;
2989 LPBITMAPINFOHEADER bmih;
2990 LPBYTE data = NULL, lpBits;
2991 BITMAP bm;
2992 INT bitCount, sizeImage, offBits, totalSize;
2993 HDC xdc;
2994 BOOL result = FALSE;
2996 if (!GetObjectW(hBitmap, sizeof(BITMAP), &bm))
2997 return FALSE;
2999 bitCount = bm.bmBitsPixel;
3000 sizeImage = get_dib_stride(bm.bmWidth, bitCount) * bm.bmHeight;
3002 totalSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
3003 if(bitCount <= 8)
3004 totalSize += (1 << bitCount) * sizeof(RGBQUAD);
3005 offBits = totalSize;
3006 totalSize += sizeImage;
3008 data = Alloc(totalSize);
3009 bmfh = (LPBITMAPFILEHEADER)data;
3010 bmih = (LPBITMAPINFOHEADER)(data + sizeof(BITMAPFILEHEADER));
3011 lpBits = data + offBits;
3013 /* setup BITMAPFILEHEADER */
3014 bmfh->bfType = (('M' << 8) | 'B');
3015 bmfh->bfSize = offBits;
3016 bmfh->bfReserved1 = 0;
3017 bmfh->bfReserved2 = 0;
3018 bmfh->bfOffBits = offBits;
3020 /* setup BITMAPINFOHEADER */
3021 bmih->biSize = sizeof(BITMAPINFOHEADER);
3022 bmih->biWidth = bm.bmWidth;
3023 bmih->biHeight = bm.bmHeight;
3024 bmih->biPlanes = 1;
3025 bmih->biBitCount = bitCount;
3026 bmih->biCompression = BI_RGB;
3027 bmih->biSizeImage = sizeImage;
3028 bmih->biXPelsPerMeter = 0;
3029 bmih->biYPelsPerMeter = 0;
3030 bmih->biClrUsed = 0;
3031 bmih->biClrImportant = 0;
3033 xdc = GetDC(0);
3034 result = GetDIBits(xdc, hBitmap, 0, bm.bmHeight, lpBits, (BITMAPINFO *)bmih, DIB_RGB_COLORS) == bm.bmHeight;
3035 ReleaseDC(0, xdc);
3036 if (!result)
3037 goto failed;
3039 TRACE("width %u, height %u, planes %u, bpp %u\n",
3040 bmih->biWidth, bmih->biHeight,
3041 bmih->biPlanes, bmih->biBitCount);
3043 if(FAILED(IStream_Write(pstm, data, totalSize, NULL)))
3044 goto failed;
3046 result = TRUE;
3048 failed:
3049 Free(data);
3051 return result;
3055 /*************************************************************************
3056 * ImageList_Write [COMCTL32.@]
3058 * Writes an image list to a stream.
3060 * PARAMS
3061 * himl [I] handle to image list
3062 * pstm [O] Pointer to a stream.
3064 * RETURNS
3065 * Success: TRUE
3066 * Failure: FALSE
3068 * BUGS
3069 * probably.
3072 BOOL WINAPI
3073 ImageList_Write (HIMAGELIST himl, LPSTREAM 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 /* retrieve the default color map */
3143 HBITMAP tmp = CreateBitmap( 1, 1, 1, 1, NULL );
3144 GetDIBits( hdc, tmp, 0, 0, NULL, bmi, DIB_RGB_COLORS );
3145 DeleteObject( tmp );
3147 hbmNewBitmap = CreateDIBSection(hdc, bmi, DIB_RGB_COLORS, NULL, 0, 0);
3149 else /*if (ilc == ILC_COLORDDB)*/
3151 TRACE("Creating Bitmap: %d Bits per Pixel\n", himl->uBitsPixel);
3153 hbmNewBitmap = CreateBitmap (sz.cx, sz.cy, 1, himl->uBitsPixel, NULL);
3155 TRACE("returning %p\n", hbmNewBitmap);
3156 return hbmNewBitmap;
3159 /*************************************************************************
3160 * ImageList_SetColorTable [COMCTL32.@]
3162 * Sets the color table of an image list.
3164 * PARAMS
3165 * himl [I] Handle to the image list.
3166 * uStartIndex [I] The first index to set.
3167 * cEntries [I] Number of entries to set.
3168 * prgb [I] New color information for color table for the image list.
3170 * RETURNS
3171 * Success: Number of entries in the table that were set.
3172 * Failure: Zero.
3174 * SEE
3175 * ImageList_Create(), SetDIBColorTable()
3178 UINT WINAPI
3179 ImageList_SetColorTable(HIMAGELIST himl, UINT uStartIndex, UINT cEntries, const RGBQUAD *prgb)
3181 return SetDIBColorTable(himl->hdcImage, uStartIndex, cEntries, prgb);
3184 /*************************************************************************
3185 * ImageList_CoCreateInstance [COMCTL32.@]
3187 * Creates a new imagelist instance and returns an interface pointer to it.
3189 * PARAMS
3190 * rclsid [I] A reference to the CLSID (CLSID_ImageList).
3191 * punkOuter [I] Pointer to IUnknown interface for aggregation, if desired
3192 * riid [I] Identifier of the requested interface.
3193 * ppv [O] Returns the address of the pointer requested, or NULL.
3195 * RETURNS
3196 * Success: S_OK.
3197 * Failure: Error value.
3199 HRESULT WINAPI
3200 ImageList_CoCreateInstance (REFCLSID rclsid, const IUnknown *punkOuter, REFIID riid, void **ppv)
3202 TRACE("(%s,%p,%s,%p)\n", debugstr_guid(rclsid), punkOuter, debugstr_guid(riid), ppv);
3204 if (!IsEqualCLSID(&CLSID_ImageList, rclsid))
3205 return E_NOINTERFACE;
3207 return ImageListImpl_CreateInstance(punkOuter, riid, ppv);
3211 /*************************************************************************
3212 * IImageList implementation
3215 static HRESULT WINAPI ImageListImpl_QueryInterface(IImageList2 *iface,
3216 REFIID iid, void **ppv)
3218 HIMAGELIST imgl = impl_from_IImageList2(iface);
3220 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
3222 if (!ppv) return E_INVALIDARG;
3224 if (IsEqualIID(&IID_IUnknown, iid) ||
3225 IsEqualIID(&IID_IImageList, iid) ||
3226 IsEqualIID(&IID_IImageList2, iid))
3228 *ppv = &imgl->IImageList2_iface;
3230 else
3232 *ppv = NULL;
3233 return E_NOINTERFACE;
3236 IImageList2_AddRef(iface);
3237 return S_OK;
3240 static ULONG WINAPI ImageListImpl_AddRef(IImageList2 *iface)
3242 HIMAGELIST imgl = impl_from_IImageList2(iface);
3243 ULONG ref = InterlockedIncrement(&imgl->ref);
3245 TRACE("(%p) refcount=%u\n", iface, ref);
3246 return ref;
3249 static ULONG WINAPI ImageListImpl_Release(IImageList2 *iface)
3251 HIMAGELIST This = impl_from_IImageList2(iface);
3252 ULONG ref = InterlockedDecrement(&This->ref);
3254 TRACE("(%p) refcount=%u\n", iface, ref);
3256 if (ref == 0)
3258 /* delete image bitmaps */
3259 if (This->hbmImage) DeleteObject (This->hbmImage);
3260 if (This->hbmMask) DeleteObject (This->hbmMask);
3262 /* delete image & mask DCs */
3263 if (This->hdcImage) DeleteDC (This->hdcImage);
3264 if (This->hdcMask) DeleteDC (This->hdcMask);
3266 /* delete blending brushes */
3267 if (This->hbrBlend25) DeleteObject (This->hbrBlend25);
3268 if (This->hbrBlend50) DeleteObject (This->hbrBlend50);
3270 This->IImageList2_iface.lpVtbl = NULL;
3271 HeapFree(GetProcessHeap(), 0, This->has_alpha);
3272 HeapFree(GetProcessHeap(), 0, This);
3275 return ref;
3278 static HRESULT WINAPI ImageListImpl_Add(IImageList2 *iface, HBITMAP hbmImage,
3279 HBITMAP hbmMask, int *pi)
3281 HIMAGELIST imgl = impl_from_IImageList2(iface);
3282 int ret;
3284 if (!pi)
3285 return E_FAIL;
3287 ret = ImageList_Add(imgl, hbmImage, hbmMask);
3289 if (ret == -1)
3290 return E_FAIL;
3292 *pi = ret;
3293 return S_OK;
3296 static HRESULT WINAPI ImageListImpl_ReplaceIcon(IImageList2 *iface, int i,
3297 HICON hicon, int *pi)
3299 HIMAGELIST imgl = impl_from_IImageList2(iface);
3300 int ret;
3302 if (!pi)
3303 return E_FAIL;
3305 ret = ImageList_ReplaceIcon(imgl, i, hicon);
3307 if (ret == -1)
3308 return E_FAIL;
3310 *pi = ret;
3311 return S_OK;
3314 static HRESULT WINAPI ImageListImpl_SetOverlayImage(IImageList2 *iface,
3315 int iImage, int iOverlay)
3317 HIMAGELIST imgl = impl_from_IImageList2(iface);
3318 return ImageList_SetOverlayImage(imgl, iImage, iOverlay) ? S_OK : E_FAIL;
3321 static HRESULT WINAPI ImageListImpl_Replace(IImageList2 *iface, int i,
3322 HBITMAP hbmImage, HBITMAP hbmMask)
3324 HIMAGELIST imgl = impl_from_IImageList2(iface);
3325 return ImageList_Replace(imgl, i, hbmImage, hbmMask) ? S_OK : E_FAIL;
3328 static HRESULT WINAPI ImageListImpl_AddMasked(IImageList2 *iface, HBITMAP hbmImage,
3329 COLORREF crMask, int *pi)
3331 HIMAGELIST imgl = impl_from_IImageList2(iface);
3332 int ret;
3334 if (!pi)
3335 return E_FAIL;
3337 ret = ImageList_AddMasked(imgl, hbmImage, crMask);
3339 if (ret == -1)
3340 return E_FAIL;
3342 *pi = ret;
3343 return S_OK;
3346 static HRESULT WINAPI ImageListImpl_Draw(IImageList2 *iface,
3347 IMAGELISTDRAWPARAMS *pimldp)
3349 HIMAGELIST imgl = impl_from_IImageList2(iface);
3350 HIMAGELIST old_himl;
3351 int ret;
3353 /* As far as I can tell, Windows simply ignores the contents of pimldp->himl
3354 so we shall simulate the same */
3355 old_himl = pimldp->himl;
3356 pimldp->himl = imgl;
3358 ret = ImageList_DrawIndirect(pimldp);
3360 pimldp->himl = old_himl;
3361 return ret ? S_OK : E_INVALIDARG;
3364 static HRESULT WINAPI ImageListImpl_Remove(IImageList2 *iface, int i)
3366 HIMAGELIST imgl = impl_from_IImageList2(iface);
3367 return (ImageList_Remove(imgl, i) == 0) ? E_INVALIDARG : S_OK;
3370 static HRESULT WINAPI ImageListImpl_GetIcon(IImageList2 *iface, int i, UINT flags,
3371 HICON *picon)
3373 HIMAGELIST imgl = impl_from_IImageList2(iface);
3374 HICON hIcon;
3376 if (!picon)
3377 return E_FAIL;
3379 hIcon = ImageList_GetIcon(imgl, i, flags);
3381 if (hIcon == NULL)
3382 return E_FAIL;
3384 *picon = hIcon;
3385 return S_OK;
3388 static HRESULT WINAPI ImageListImpl_GetImageInfo(IImageList2 *iface, int i,
3389 IMAGEINFO *pImageInfo)
3391 HIMAGELIST imgl = impl_from_IImageList2(iface);
3392 return ImageList_GetImageInfo(imgl, i, pImageInfo) ? S_OK : E_FAIL;
3395 static HRESULT WINAPI ImageListImpl_Copy(IImageList2 *iface, int dst_index,
3396 IUnknown *unk_src, int src_index, UINT flags)
3398 HIMAGELIST imgl = impl_from_IImageList2(iface);
3399 IImageList *src = NULL;
3400 HRESULT ret;
3402 if (!unk_src)
3403 return E_FAIL;
3405 /* TODO: Add test for IID_ImageList2 too */
3406 if (FAILED(IUnknown_QueryInterface(unk_src, &IID_IImageList,
3407 (void **) &src)))
3408 return E_FAIL;
3410 if (ImageList_Copy(imgl, dst_index, (HIMAGELIST) src, src_index, flags))
3411 ret = S_OK;
3412 else
3413 ret = E_FAIL;
3415 IImageList_Release(src);
3416 return ret;
3419 static HRESULT WINAPI ImageListImpl_Merge(IImageList2 *iface, int i1,
3420 IUnknown *punk2, int i2, int dx, int dy, REFIID riid, void **ppv)
3422 HIMAGELIST imgl = impl_from_IImageList2(iface);
3423 IImageList *iml2 = NULL;
3424 HIMAGELIST merged;
3425 HRESULT ret = E_FAIL;
3427 TRACE("(%p)->(%d %p %d %d %d %s %p)\n", iface, i1, punk2, i2, dx, dy, debugstr_guid(riid), ppv);
3429 /* TODO: Add test for IID_ImageList2 too */
3430 if (FAILED(IUnknown_QueryInterface(punk2, &IID_IImageList,
3431 (void **) &iml2)))
3432 return E_FAIL;
3434 merged = ImageList_Merge(imgl, i1, (HIMAGELIST) iml2, i2, dx, dy);
3436 /* Get the interface for the new image list */
3437 if (merged)
3439 ret = HIMAGELIST_QueryInterface(merged, riid, ppv);
3440 ImageList_Destroy(merged);
3443 IImageList_Release(iml2);
3444 return ret;
3447 static HRESULT WINAPI ImageListImpl_Clone(IImageList2 *iface, REFIID riid, void **ppv)
3449 HIMAGELIST imgl = impl_from_IImageList2(iface);
3450 HIMAGELIST clone;
3451 HRESULT ret = E_FAIL;
3453 TRACE("(%p)->(%s %p)\n", iface, debugstr_guid(riid), ppv);
3455 clone = ImageList_Duplicate(imgl);
3457 /* Get the interface for the new image list */
3458 if (clone)
3460 ret = HIMAGELIST_QueryInterface(clone, riid, ppv);
3461 ImageList_Destroy(clone);
3464 return ret;
3467 static HRESULT WINAPI ImageListImpl_GetImageRect(IImageList2 *iface, int i,
3468 RECT *prc)
3470 HIMAGELIST imgl = impl_from_IImageList2(iface);
3471 IMAGEINFO info;
3473 if (!prc)
3474 return E_FAIL;
3476 if (!ImageList_GetImageInfo(imgl, i, &info))
3477 return E_FAIL;
3479 return CopyRect(prc, &info.rcImage) ? S_OK : E_FAIL;
3482 static HRESULT WINAPI ImageListImpl_GetIconSize(IImageList2 *iface, int *cx,
3483 int *cy)
3485 HIMAGELIST imgl = impl_from_IImageList2(iface);
3486 return ImageList_GetIconSize(imgl, cx, cy) ? S_OK : E_INVALIDARG;
3489 static HRESULT WINAPI ImageListImpl_SetIconSize(IImageList2 *iface, int cx,
3490 int cy)
3492 HIMAGELIST imgl = impl_from_IImageList2(iface);
3493 return ImageList_SetIconSize(imgl, cx, cy) ? S_OK : E_FAIL;
3496 static HRESULT WINAPI ImageListImpl_GetImageCount(IImageList2 *iface, int *pi)
3498 HIMAGELIST imgl = impl_from_IImageList2(iface);
3499 *pi = ImageList_GetImageCount(imgl);
3500 return S_OK;
3503 static HRESULT WINAPI ImageListImpl_SetImageCount(IImageList2 *iface, UINT count)
3505 HIMAGELIST imgl = impl_from_IImageList2(iface);
3506 return ImageList_SetImageCount(imgl, count) ? S_OK : E_FAIL;
3509 static HRESULT WINAPI ImageListImpl_SetBkColor(IImageList2 *iface, COLORREF clrBk,
3510 COLORREF *pclr)
3512 HIMAGELIST imgl = impl_from_IImageList2(iface);
3513 *pclr = ImageList_SetBkColor(imgl, clrBk);
3514 return S_OK;
3517 static HRESULT WINAPI ImageListImpl_GetBkColor(IImageList2 *iface, COLORREF *pclr)
3519 HIMAGELIST imgl = impl_from_IImageList2(iface);
3520 *pclr = ImageList_GetBkColor(imgl);
3521 return S_OK;
3524 static HRESULT WINAPI ImageListImpl_BeginDrag(IImageList2 *iface, int iTrack,
3525 int dxHotspot, int dyHotspot)
3527 HIMAGELIST imgl = impl_from_IImageList2(iface);
3528 return ImageList_BeginDrag(imgl, iTrack, dxHotspot, dyHotspot) ? S_OK : E_FAIL;
3531 static HRESULT WINAPI ImageListImpl_EndDrag(IImageList2 *iface)
3533 ImageList_EndDrag();
3534 return S_OK;
3537 static HRESULT WINAPI ImageListImpl_DragEnter(IImageList2 *iface, HWND hwndLock,
3538 int x, int y)
3540 return ImageList_DragEnter(hwndLock, x, y) ? S_OK : E_FAIL;
3543 static HRESULT WINAPI ImageListImpl_DragLeave(IImageList2 *iface, HWND hwndLock)
3545 return ImageList_DragLeave(hwndLock) ? S_OK : E_FAIL;
3548 static HRESULT WINAPI ImageListImpl_DragMove(IImageList2 *iface, int x, int y)
3550 return ImageList_DragMove(x, y) ? S_OK : E_FAIL;
3553 static HRESULT WINAPI ImageListImpl_SetDragCursorImage(IImageList2 *iface,
3554 IUnknown *punk, int iDrag, int dxHotspot, int dyHotspot)
3556 IImageList *iml2 = NULL;
3557 HRESULT ret;
3559 if (!punk)
3560 return E_FAIL;
3562 /* TODO: Add test for IID_ImageList2 too */
3563 if (FAILED(IUnknown_QueryInterface(punk, &IID_IImageList,
3564 (void **) &iml2)))
3565 return E_FAIL;
3567 ret = ImageList_SetDragCursorImage((HIMAGELIST) iml2, iDrag, dxHotspot,
3568 dyHotspot);
3570 IImageList_Release(iml2);
3572 return ret ? S_OK : E_FAIL;
3575 static HRESULT WINAPI ImageListImpl_DragShowNolock(IImageList2 *iface, BOOL fShow)
3577 return ImageList_DragShowNolock(fShow) ? S_OK : E_FAIL;
3580 static HRESULT WINAPI ImageListImpl_GetDragImage(IImageList2 *iface, POINT *ppt,
3581 POINT *pptHotspot, REFIID riid, PVOID *ppv)
3583 HRESULT ret = E_FAIL;
3584 HIMAGELIST hNew;
3586 if (!ppv)
3587 return E_FAIL;
3589 hNew = ImageList_GetDragImage(ppt, pptHotspot);
3591 /* Get the interface for the new image list */
3592 if (hNew)
3594 IImageList *idrag = (IImageList*)hNew;
3596 ret = HIMAGELIST_QueryInterface(hNew, riid, ppv);
3597 IImageList_Release(idrag);
3600 return ret;
3603 static HRESULT WINAPI ImageListImpl_GetItemFlags(IImageList2 *iface, int i,
3604 DWORD *dwFlags)
3606 FIXME("STUB: %p %d %p\n", iface, i, dwFlags);
3607 return E_NOTIMPL;
3610 static HRESULT WINAPI ImageListImpl_GetOverlayImage(IImageList2 *iface, int iOverlay,
3611 int *piIndex)
3613 HIMAGELIST This = impl_from_IImageList2(iface);
3614 int i;
3616 if ((iOverlay < 0) || (iOverlay > This->cCurImage))
3617 return E_FAIL;
3619 for (i = 0; i < MAX_OVERLAYIMAGE; i++)
3621 if (This->nOvlIdx[i] == iOverlay)
3623 *piIndex = i + 1;
3624 return S_OK;
3628 return E_FAIL;
3631 static HRESULT WINAPI ImageListImpl_Resize(IImageList2 *iface, INT cx, INT cy)
3633 FIXME("(%p)->(%d %d): stub\n", iface, cx, cy);
3634 return E_NOTIMPL;
3637 static HRESULT WINAPI ImageListImpl_GetOriginalSize(IImageList2 *iface, INT image, DWORD flags, INT *cx, INT *cy)
3639 FIXME("(%p)->(%d %x %p %p): stub\n", iface, image, flags, cx, cy);
3640 return E_NOTIMPL;
3643 static HRESULT WINAPI ImageListImpl_SetOriginalSize(IImageList2 *iface, INT image, INT cx, INT cy)
3645 FIXME("(%p)->(%d %d %d): stub\n", iface, image, cx, cy);
3646 return E_NOTIMPL;
3649 static HRESULT WINAPI ImageListImpl_SetCallback(IImageList2 *iface, IUnknown *callback)
3651 FIXME("(%p)->(%p): stub\n", iface, callback);
3652 return E_NOTIMPL;
3655 static HRESULT WINAPI ImageListImpl_GetCallback(IImageList2 *iface, REFIID riid, void **ppv)
3657 FIXME("(%p)->(%s %p): stub\n", iface, debugstr_guid(riid), ppv);
3658 return E_NOTIMPL;
3661 static HRESULT WINAPI ImageListImpl_ForceImagePresent(IImageList2 *iface, INT image, DWORD flags)
3663 FIXME("(%p)->(%d %x): stub\n", iface, image, flags);
3664 return E_NOTIMPL;
3667 static HRESULT WINAPI ImageListImpl_DiscardImages(IImageList2 *iface, INT first_image, INT last_image, DWORD flags)
3669 FIXME("(%p)->(%d %d %x): stub\n", iface, first_image, last_image, flags);
3670 return E_NOTIMPL;
3673 static HRESULT WINAPI ImageListImpl_PreloadImages(IImageList2 *iface, IMAGELISTDRAWPARAMS *params)
3675 FIXME("(%p)->(%p): stub\n", iface, params);
3676 return E_NOTIMPL;
3679 static HRESULT WINAPI ImageListImpl_GetStatistics(IImageList2 *iface, IMAGELISTSTATS *stats)
3681 FIXME("(%p)->(%p): stub\n", iface, stats);
3682 return E_NOTIMPL;
3685 static HRESULT WINAPI ImageListImpl_Initialize(IImageList2 *iface, INT cx, INT cy, UINT flags, INT initial, INT grow)
3687 FIXME("(%p)->(%d %d %d %d %d): stub\n", iface, cx, cy, flags, initial, grow);
3688 return E_NOTIMPL;
3691 static HRESULT WINAPI ImageListImpl_Replace2(IImageList2 *iface, INT i, HBITMAP image, HBITMAP mask, IUnknown *unk, DWORD flags)
3693 FIXME("(%p)->(%d %p %p %p %x): stub\n", iface, i, image, mask, unk, flags);
3694 return E_NOTIMPL;
3697 static HRESULT WINAPI ImageListImpl_ReplaceFromImageList(IImageList2 *iface, INT i, IImageList *imagelist, INT src,
3698 IUnknown *unk, DWORD flags)
3700 FIXME("(%p)->(%d %p %d %p %x): stub\n", iface, i, imagelist, src, unk, flags);
3701 return E_NOTIMPL;
3704 static const IImageList2Vtbl ImageListImpl_Vtbl = {
3705 ImageListImpl_QueryInterface,
3706 ImageListImpl_AddRef,
3707 ImageListImpl_Release,
3708 ImageListImpl_Add,
3709 ImageListImpl_ReplaceIcon,
3710 ImageListImpl_SetOverlayImage,
3711 ImageListImpl_Replace,
3712 ImageListImpl_AddMasked,
3713 ImageListImpl_Draw,
3714 ImageListImpl_Remove,
3715 ImageListImpl_GetIcon,
3716 ImageListImpl_GetImageInfo,
3717 ImageListImpl_Copy,
3718 ImageListImpl_Merge,
3719 ImageListImpl_Clone,
3720 ImageListImpl_GetImageRect,
3721 ImageListImpl_GetIconSize,
3722 ImageListImpl_SetIconSize,
3723 ImageListImpl_GetImageCount,
3724 ImageListImpl_SetImageCount,
3725 ImageListImpl_SetBkColor,
3726 ImageListImpl_GetBkColor,
3727 ImageListImpl_BeginDrag,
3728 ImageListImpl_EndDrag,
3729 ImageListImpl_DragEnter,
3730 ImageListImpl_DragLeave,
3731 ImageListImpl_DragMove,
3732 ImageListImpl_SetDragCursorImage,
3733 ImageListImpl_DragShowNolock,
3734 ImageListImpl_GetDragImage,
3735 ImageListImpl_GetItemFlags,
3736 ImageListImpl_GetOverlayImage,
3737 ImageListImpl_Resize,
3738 ImageListImpl_GetOriginalSize,
3739 ImageListImpl_SetOriginalSize,
3740 ImageListImpl_SetCallback,
3741 ImageListImpl_GetCallback,
3742 ImageListImpl_ForceImagePresent,
3743 ImageListImpl_DiscardImages,
3744 ImageListImpl_PreloadImages,
3745 ImageListImpl_GetStatistics,
3746 ImageListImpl_Initialize,
3747 ImageListImpl_Replace2,
3748 ImageListImpl_ReplaceFromImageList
3751 static BOOL is_valid(HIMAGELIST himl)
3753 BOOL valid;
3754 __TRY
3756 valid = himl && himl->IImageList2_iface.lpVtbl == &ImageListImpl_Vtbl;
3758 __EXCEPT_PAGE_FAULT
3760 valid = FALSE;
3762 __ENDTRY
3763 return valid;
3766 /*************************************************************************
3767 * HIMAGELIST_QueryInterface [COMCTL32.@]
3769 * Returns a pointer to an IImageList or IImageList2 object for the given
3770 * HIMAGELIST.
3772 * PARAMS
3773 * himl [I] Image list handle.
3774 * riid [I] Identifier of the requested interface.
3775 * ppv [O] Returns the address of the pointer requested, or NULL.
3777 * RETURNS
3778 * Success: S_OK.
3779 * Failure: Error value.
3781 HRESULT WINAPI
3782 HIMAGELIST_QueryInterface (HIMAGELIST himl, REFIID riid, void **ppv)
3784 TRACE("(%p,%s,%p)\n", himl, debugstr_guid(riid), ppv);
3785 return IImageList2_QueryInterface((IImageList2 *) himl, riid, ppv);
3788 static HRESULT ImageListImpl_CreateInstance(const IUnknown *pUnkOuter, REFIID iid, void** ppv)
3790 HIMAGELIST This;
3791 HRESULT ret;
3793 TRACE("(%p,%s,%p)\n", pUnkOuter, debugstr_guid(iid), ppv);
3795 *ppv = NULL;
3797 if (pUnkOuter) return CLASS_E_NOAGGREGATION;
3799 This = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct _IMAGELIST));
3800 if (!This) return E_OUTOFMEMORY;
3802 This->IImageList2_iface.lpVtbl = &ImageListImpl_Vtbl;
3803 This->ref = 1;
3805 ret = IImageList2_QueryInterface(&This->IImageList2_iface, iid, ppv);
3806 IImageList2_Release(&This->IImageList2_iface);
3808 return ret;