wined3d: Recognize dynamic linking shader instructions.
[wine.git] / dlls / comctl32 / imagelist.c
blob9811da374b26998e3256f4d3378b72480be84ee2
1 /*
2 * ImageList implementation
4 * Copyright 1998 Eric Kohl
5 * Copyright 2000 Jason Mawdsley
6 * Copyright 2001, 2004 Michael Stefaniuc
7 * Copyright 2001 Charles Loep for CodeWeavers
8 * Copyright 2002 Dimitrie O. Paun
9 * Copyright 2009 Owen Rudge for CodeWeavers
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 * NOTE
27 * This code was audited for completeness against the documented features
28 * of Comctl32.dll version 6.0 on Sep. 12, 2002, by Dimitrie O. Paun.
30 * Unless otherwise noted, we believe this code to be complete, as per
31 * the specification mentioned above.
32 * If you discover missing features, or bugs, please note them below.
34 * TODO:
35 * - Add support for ILD_PRESERVEALPHA, ILD_SCALE, ILD_DPISCALE
36 * - Add support for ILS_GLOW, ILS_SHADOW, ILS_SATURATE
37 * - Thread-safe locking
40 #include <stdarg.h>
41 #include <stdlib.h>
42 #include <string.h>
44 #define COBJMACROS
46 #include "winerror.h"
47 #include "windef.h"
48 #include "winbase.h"
49 #include "objbase.h"
50 #include "wingdi.h"
51 #include "winuser.h"
52 #include "commctrl.h"
53 #include "comctl32.h"
54 #include "commoncontrols.h"
55 #include "wine/debug.h"
56 #include "wine/exception.h"
58 WINE_DEFAULT_DEBUG_CHANNEL(imagelist);
60 #define MAX_OVERLAYIMAGE 15
62 struct _IMAGELIST
64 IImageList2 IImageList2_iface; /* 00: IImageList vtable */
65 INT cCurImage; /* 04: ImageCount */
66 INT cMaxImage; /* 08: maximages */
67 INT cGrow; /* 0C: cGrow */
68 INT cx; /* 10: cx */
69 INT cy; /* 14: cy */
70 DWORD x4;
71 UINT flags; /* 1C: flags */
72 COLORREF clrFg; /* 20: foreground color */
73 COLORREF clrBk; /* 24: background color */
76 HBITMAP hbmImage; /* 28: images Bitmap */
77 HBITMAP hbmMask; /* 2C: masks Bitmap */
78 HDC hdcImage; /* 30: images MemDC */
79 HDC hdcMask; /* 34: masks MemDC */
80 INT nOvlIdx[MAX_OVERLAYIMAGE]; /* 38: overlay images index */
82 /* not yet found out */
83 HBRUSH hbrBlend25;
84 HBRUSH hbrBlend50;
85 INT cInitial;
86 UINT uBitsPixel;
87 char *has_alpha;
89 LONG ref; /* reference count */
92 #define IMAGELIST_MAGIC 0x53414D58
94 /* Header used by ImageList_Read() and ImageList_Write() */
95 #include "pshpack2.h"
96 typedef struct _ILHEAD
98 USHORT usMagic;
99 USHORT usVersion;
100 WORD cCurImage;
101 WORD cMaxImage;
102 WORD cGrow;
103 WORD cx;
104 WORD cy;
105 COLORREF bkcolor;
106 WORD flags;
107 SHORT ovls[4];
108 } ILHEAD;
109 #include "poppack.h"
111 /* internal image list data used for Drag & Drop operations */
112 typedef struct
114 HWND hwnd;
115 HIMAGELIST himl;
116 HIMAGELIST himlNoCursor;
117 /* position of the drag image relative to the window */
118 INT x;
119 INT y;
120 /* offset of the hotspot relative to the origin of the image */
121 INT dxHotspot;
122 INT dyHotspot;
123 /* is the drag image visible */
124 BOOL bShow;
125 /* saved background */
126 HBITMAP hbmBg;
127 } INTERNALDRAG;
129 static INTERNALDRAG InternalDrag = { 0, 0, 0, 0, 0, 0, 0, FALSE, 0 };
131 static inline HIMAGELIST impl_from_IImageList2(IImageList2 *iface)
133 return CONTAINING_RECORD(iface, struct _IMAGELIST, IImageList2_iface);
136 static HBITMAP ImageList_CreateImage(HDC hdc, HIMAGELIST himl, UINT count);
137 static HRESULT ImageListImpl_CreateInstance(const IUnknown *pUnkOuter, REFIID iid, void** ppv);
138 static BOOL is_valid(HIMAGELIST himl);
141 * An imagelist with N images is tiled like this:
143 * N/4 ->
145 * 4 048C..
146 * 159D..
147 * | 26AE.N
148 * V 37BF.
151 #define TILE_COUNT 4
153 static inline UINT imagelist_height( UINT count )
155 return ((count + TILE_COUNT - 1)/TILE_COUNT);
158 static inline void imagelist_point_from_index( HIMAGELIST himl, UINT index, LPPOINT pt )
160 pt->x = (index%TILE_COUNT) * himl->cx;
161 pt->y = (index/TILE_COUNT) * himl->cy;
164 static inline void imagelist_get_bitmap_size( HIMAGELIST himl, UINT count, SIZE *sz )
166 sz->cx = himl->cx * TILE_COUNT;
167 sz->cy = imagelist_height( count ) * himl->cy;
170 static inline int get_dib_stride( int width, int bpp )
172 return ((width * bpp + 31) >> 3) & ~3;
175 static inline int get_dib_image_size( const BITMAPINFO *info )
177 return get_dib_stride( info->bmiHeader.biWidth, info->bmiHeader.biBitCount )
178 * abs( info->bmiHeader.biHeight );
182 * imagelist_copy_images()
184 * Copies a block of count images from offset src in the list to offset dest.
185 * Images are copied a row at at time. Assumes hdcSrc and hdcDest are different.
187 static inline void imagelist_copy_images( HIMAGELIST himl, HDC hdcSrc, HDC hdcDest,
188 UINT src, UINT count, UINT dest )
190 POINT ptSrc, ptDest;
191 SIZE sz;
192 UINT i;
194 for ( i=0; i<TILE_COUNT; i++ )
196 imagelist_point_from_index( himl, src+i, &ptSrc );
197 imagelist_point_from_index( himl, dest+i, &ptDest );
198 sz.cx = himl->cx;
199 sz.cy = himl->cy * imagelist_height( count - i );
201 BitBlt( hdcDest, ptDest.x, ptDest.y, sz.cx, sz.cy,
202 hdcSrc, ptSrc.x, ptSrc.y, SRCCOPY );
206 static void add_dib_bits( HIMAGELIST himl, int pos, int count, int width, int height,
207 BITMAPINFO *info, BITMAPINFO *mask_info, DWORD *bits, BYTE *mask_bits )
209 int i, j, n;
210 POINT pt;
211 int stride = info->bmiHeader.biWidth;
212 int mask_stride = (info->bmiHeader.biWidth + 31) / 32 * 4;
214 for (n = 0; n < count; n++)
216 BOOL has_alpha = FALSE;
218 imagelist_point_from_index( himl, pos + n, &pt );
220 /* check if bitmap has an alpha channel */
221 for (i = 0; i < height && !has_alpha; i++)
222 for (j = n * width; j < (n + 1) * width; j++)
223 if ((has_alpha = ((bits[i * stride + j] & 0xff000000) != 0))) break;
225 if (!has_alpha) /* generate alpha channel from the mask */
227 for (i = 0; i < height; i++)
228 for (j = n * width; j < (n + 1) * width; j++)
229 if (!mask_info || !((mask_bits[i * mask_stride + j / 8] << (j % 8)) & 0x80))
230 bits[i * stride + j] |= 0xff000000;
231 else
232 bits[i * stride + j] = 0;
234 else
236 himl->has_alpha[pos + n] = 1;
238 if (mask_info && himl->hbmMask) /* generate the mask from the alpha channel */
240 for (i = 0; i < height; i++)
241 for (j = n * width; j < (n + 1) * width; j++)
242 if ((bits[i * stride + j] >> 24) > 25) /* more than 10% alpha */
243 mask_bits[i * mask_stride + j / 8] &= ~(0x80 >> (j % 8));
244 else
245 mask_bits[i * mask_stride + j / 8] |= 0x80 >> (j % 8);
248 StretchDIBits( himl->hdcImage, pt.x, pt.y, himl->cx, himl->cy,
249 n * width, 0, width, height, bits, info, DIB_RGB_COLORS, SRCCOPY );
250 if (mask_info)
251 StretchDIBits( himl->hdcMask, pt.x, pt.y, himl->cx, himl->cy,
252 n * width, 0, width, height, mask_bits, mask_info, DIB_RGB_COLORS, SRCCOPY );
256 /* add images with an alpha channel when the image list is 32 bpp */
257 static BOOL add_with_alpha( HIMAGELIST himl, HDC hdc, int pos, int count,
258 int width, int height, HBITMAP hbmImage, HBITMAP hbmMask )
260 BOOL ret = FALSE;
261 BITMAP bm;
262 BITMAPINFO *info, *mask_info = NULL;
263 DWORD *bits = NULL;
264 BYTE *mask_bits = NULL;
265 DWORD mask_width;
267 if (!GetObjectW( hbmImage, sizeof(bm), &bm )) return FALSE;
269 /* if either the imagelist or the source bitmap don't have an alpha channel, bail out now */
270 if (!himl->has_alpha) return FALSE;
271 if (bm.bmBitsPixel != 32) return FALSE;
273 SelectObject( hdc, hbmImage );
274 mask_width = (bm.bmWidth + 31) / 32 * 4;
276 if (!(info = HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET( BITMAPINFO, bmiColors[256] )))) goto done;
277 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
278 info->bmiHeader.biWidth = bm.bmWidth;
279 info->bmiHeader.biHeight = -height;
280 info->bmiHeader.biPlanes = 1;
281 info->bmiHeader.biBitCount = 32;
282 info->bmiHeader.biCompression = BI_RGB;
283 info->bmiHeader.biSizeImage = bm.bmWidth * height * 4;
284 info->bmiHeader.biXPelsPerMeter = 0;
285 info->bmiHeader.biYPelsPerMeter = 0;
286 info->bmiHeader.biClrUsed = 0;
287 info->bmiHeader.biClrImportant = 0;
288 if (!(bits = HeapAlloc( GetProcessHeap(), 0, info->bmiHeader.biSizeImage ))) goto done;
289 if (!GetDIBits( hdc, hbmImage, 0, height, bits, info, DIB_RGB_COLORS )) goto done;
291 if (hbmMask)
293 if (!(mask_info = HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET( BITMAPINFO, bmiColors[2] ))))
294 goto done;
295 mask_info->bmiHeader = info->bmiHeader;
296 mask_info->bmiHeader.biBitCount = 1;
297 mask_info->bmiHeader.biSizeImage = mask_width * height;
298 if (!(mask_bits = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, info->bmiHeader.biSizeImage )))
299 goto done;
300 if (!GetDIBits( hdc, hbmMask, 0, height, mask_bits, mask_info, DIB_RGB_COLORS )) goto done;
303 add_dib_bits( himl, pos, count, width, height, info, mask_info, bits, mask_bits );
304 ret = TRUE;
306 done:
307 HeapFree( GetProcessHeap(), 0, info );
308 HeapFree( GetProcessHeap(), 0, mask_info );
309 HeapFree( GetProcessHeap(), 0, bits );
310 HeapFree( GetProcessHeap(), 0, mask_bits );
311 return ret;
314 /*************************************************************************
315 * IMAGELIST_InternalExpandBitmaps [Internal]
317 * Expands the bitmaps of an image list by the given number of images.
319 * PARAMS
320 * himl [I] handle to image list
321 * nImageCount [I] number of images to add
323 * RETURNS
324 * nothing
326 * NOTES
327 * This function CANNOT be used to reduce the number of images.
329 static void
330 IMAGELIST_InternalExpandBitmaps(HIMAGELIST himl, INT nImageCount)
332 HDC hdcBitmap;
333 HBITMAP hbmNewBitmap, hbmNull;
334 INT nNewCount;
335 SIZE sz;
337 TRACE("%p has allocated %d, max %d, grow %d images\n", himl, himl->cCurImage, himl->cMaxImage, himl->cGrow);
339 if (himl->cCurImage + nImageCount < himl->cMaxImage)
340 return;
342 nNewCount = himl->cMaxImage + max(nImageCount, himl->cGrow) + 1;
344 imagelist_get_bitmap_size(himl, nNewCount, &sz);
346 TRACE("Create expanded bitmaps : himl=%p x=%d y=%d count=%d\n", himl, sz.cx, sz.cy, nNewCount);
347 hdcBitmap = CreateCompatibleDC (0);
349 hbmNewBitmap = ImageList_CreateImage(hdcBitmap, himl, nNewCount);
351 if (hbmNewBitmap == 0)
352 ERR("creating new image bitmap (x=%d y=%d)!\n", sz.cx, sz.cy);
354 if (himl->cCurImage)
356 hbmNull = SelectObject (hdcBitmap, hbmNewBitmap);
357 BitBlt (hdcBitmap, 0, 0, sz.cx, sz.cy,
358 himl->hdcImage, 0, 0, SRCCOPY);
359 SelectObject (hdcBitmap, hbmNull);
361 SelectObject (himl->hdcImage, hbmNewBitmap);
362 DeleteObject (himl->hbmImage);
363 himl->hbmImage = hbmNewBitmap;
365 if (himl->flags & ILC_MASK)
367 hbmNewBitmap = CreateBitmap (sz.cx, sz.cy, 1, 1, NULL);
369 if (hbmNewBitmap == 0)
370 ERR("creating new mask bitmap!\n");
372 if(himl->cCurImage)
374 hbmNull = SelectObject (hdcBitmap, hbmNewBitmap);
375 BitBlt (hdcBitmap, 0, 0, sz.cx, sz.cy,
376 himl->hdcMask, 0, 0, SRCCOPY);
377 SelectObject (hdcBitmap, hbmNull);
379 SelectObject (himl->hdcMask, hbmNewBitmap);
380 DeleteObject (himl->hbmMask);
381 himl->hbmMask = hbmNewBitmap;
384 if (himl->has_alpha)
386 char *new_alpha = HeapReAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, himl->has_alpha, nNewCount );
387 if (new_alpha) himl->has_alpha = new_alpha;
388 else
390 HeapFree( GetProcessHeap(), 0, himl->has_alpha );
391 himl->has_alpha = NULL;
395 himl->cMaxImage = nNewCount;
397 DeleteDC (hdcBitmap);
401 /*************************************************************************
402 * ImageList_Add [COMCTL32.@]
404 * Add an image or images to an image list.
406 * PARAMS
407 * himl [I] handle to image list
408 * hbmImage [I] handle to image bitmap
409 * hbmMask [I] handle to mask bitmap
411 * RETURNS
412 * Success: Index of the first new image.
413 * Failure: -1
416 INT WINAPI
417 ImageList_Add (HIMAGELIST himl, HBITMAP hbmImage, HBITMAP hbmMask)
419 HDC hdcBitmap, hdcTemp = 0;
420 INT nFirstIndex, nImageCount, i;
421 BITMAP bmp;
422 POINT pt;
424 TRACE("himl=%p hbmimage=%p hbmmask=%p\n", himl, hbmImage, hbmMask);
425 if (!is_valid(himl))
426 return -1;
428 if (!GetObjectW(hbmImage, sizeof(BITMAP), &bmp))
429 return -1;
431 TRACE("himl %p, cCurImage %d, cMaxImage %d, cGrow %d, cx %d, cy %d\n",
432 himl, himl->cCurImage, himl->cMaxImage, himl->cGrow, himl->cx, himl->cy);
434 nImageCount = bmp.bmWidth / himl->cx;
436 TRACE("%p has %d images (%d x %d)\n", hbmImage, nImageCount, bmp.bmWidth, bmp.bmHeight);
438 IMAGELIST_InternalExpandBitmaps(himl, nImageCount);
440 hdcBitmap = CreateCompatibleDC(0);
442 SelectObject(hdcBitmap, hbmImage);
444 if (add_with_alpha( himl, hdcBitmap, himl->cCurImage, nImageCount,
445 himl->cx, min( himl->cy, bmp.bmHeight), hbmImage, hbmMask ))
446 goto done;
448 if (himl->hbmMask)
450 hdcTemp = CreateCompatibleDC(0);
451 SelectObject(hdcTemp, hbmMask);
454 for (i=0; i<nImageCount; i++)
456 imagelist_point_from_index( himl, himl->cCurImage + i, &pt );
458 /* Copy result to the imagelist
460 BitBlt( himl->hdcImage, pt.x, pt.y, himl->cx, bmp.bmHeight,
461 hdcBitmap, i*himl->cx, 0, SRCCOPY );
463 if (!himl->hbmMask)
464 continue;
466 BitBlt( himl->hdcMask, pt.x, pt.y, himl->cx, bmp.bmHeight,
467 hdcTemp, i*himl->cx, 0, SRCCOPY );
469 /* Remove the background from the image
471 BitBlt( himl->hdcImage, pt.x, pt.y, himl->cx, bmp.bmHeight,
472 himl->hdcMask, pt.x, pt.y, 0x220326 ); /* NOTSRCAND */
474 if (hdcTemp) DeleteDC(hdcTemp);
476 done:
477 DeleteDC(hdcBitmap);
479 nFirstIndex = himl->cCurImage;
480 himl->cCurImage += nImageCount;
482 return nFirstIndex;
486 /*************************************************************************
487 * ImageList_AddIcon [COMCTL32.@]
489 * Adds an icon to an image list.
491 * PARAMS
492 * himl [I] handle to image list
493 * hIcon [I] handle to icon
495 * RETURNS
496 * Success: index of the new image
497 * Failure: -1
499 #undef ImageList_AddIcon
500 INT WINAPI ImageList_AddIcon (HIMAGELIST himl, HICON hIcon)
502 return ImageList_ReplaceIcon (himl, -1, hIcon);
506 /*************************************************************************
507 * ImageList_AddMasked [COMCTL32.@]
509 * Adds an image or images to an image list and creates a mask from the
510 * specified bitmap using the mask color.
512 * PARAMS
513 * himl [I] handle to image list.
514 * hBitmap [I] handle to bitmap
515 * clrMask [I] mask color.
517 * RETURNS
518 * Success: Index of the first new image.
519 * Failure: -1
522 INT WINAPI
523 ImageList_AddMasked (HIMAGELIST himl, HBITMAP hBitmap, COLORREF clrMask)
525 HDC hdcMask, hdcBitmap;
526 INT ret;
527 BITMAP bmp;
528 HBITMAP hMaskBitmap;
529 COLORREF bkColor;
531 TRACE("himl=%p hbitmap=%p clrmask=%x\n", himl, hBitmap, clrMask);
532 if (!is_valid(himl))
533 return -1;
535 if (!GetObjectW(hBitmap, sizeof(BITMAP), &bmp))
536 return -1;
538 hdcBitmap = CreateCompatibleDC(0);
539 SelectObject(hdcBitmap, hBitmap);
541 /* Create a temp Mask so we can remove the background of the Image */
542 hdcMask = CreateCompatibleDC(0);
543 hMaskBitmap = CreateBitmap(bmp.bmWidth, bmp.bmHeight, 1, 1, NULL);
544 SelectObject(hdcMask, hMaskBitmap);
546 /* create monochrome image to the mask bitmap */
547 bkColor = (clrMask != CLR_DEFAULT) ? clrMask : GetPixel (hdcBitmap, 0, 0);
548 SetBkColor (hdcBitmap, bkColor);
549 BitBlt (hdcMask, 0, 0, bmp.bmWidth, bmp.bmHeight, hdcBitmap, 0, 0, SRCCOPY);
552 * Remove the background from the image
554 * WINDOWS BUG ALERT!!!!!!
555 * The statement below should not be done in common practice
556 * but this is how ImageList_AddMasked works in Windows.
557 * It overwrites the original bitmap passed, this was discovered
558 * by using the same bitmap to iterate the different styles
559 * on windows where it failed (BUT ImageList_Add is OK)
560 * This is here in case some apps rely on this bug
562 * Blt mode 0x220326 is NOTSRCAND
564 if (bmp.bmBitsPixel > 8) /* NOTSRCAND can't work with palettes */
566 SetBkColor(hdcBitmap, RGB(255,255,255));
567 BitBlt(hdcBitmap, 0, 0, bmp.bmWidth, bmp.bmHeight, hdcMask, 0, 0, 0x220326);
570 DeleteDC(hdcBitmap);
571 DeleteDC(hdcMask);
573 ret = ImageList_Add( himl, hBitmap, hMaskBitmap );
575 DeleteObject(hMaskBitmap);
576 return ret;
580 /*************************************************************************
581 * ImageList_BeginDrag [COMCTL32.@]
583 * Creates a temporary image list that contains one image. It will be used
584 * as a drag image.
586 * PARAMS
587 * himlTrack [I] handle to the source image list
588 * iTrack [I] index of the drag image in the source image list
589 * dxHotspot [I] X position of the hot spot of the drag image
590 * dyHotspot [I] Y position of the hot spot of the drag image
592 * RETURNS
593 * Success: TRUE
594 * Failure: FALSE
597 BOOL WINAPI
598 ImageList_BeginDrag (HIMAGELIST himlTrack, INT iTrack,
599 INT dxHotspot, INT dyHotspot)
601 INT cx, cy;
602 POINT src, dst;
604 TRACE("(himlTrack=%p iTrack=%d dx=%d dy=%d)\n", himlTrack, iTrack,
605 dxHotspot, dyHotspot);
607 if (!is_valid(himlTrack))
608 return FALSE;
610 if (iTrack >= himlTrack->cCurImage)
611 return FALSE;
613 if (InternalDrag.himl)
614 return FALSE;
616 cx = himlTrack->cx;
617 cy = himlTrack->cy;
619 InternalDrag.himlNoCursor = InternalDrag.himl = ImageList_Create (cx, cy, himlTrack->flags, 1, 1);
620 if (InternalDrag.himl == NULL) {
621 WARN("Error creating drag image list!\n");
622 return FALSE;
625 InternalDrag.dxHotspot = dxHotspot;
626 InternalDrag.dyHotspot = dyHotspot;
628 /* copy image */
629 imagelist_point_from_index(InternalDrag.himl, 0, &dst);
630 imagelist_point_from_index(himlTrack, iTrack, &src);
631 BitBlt(InternalDrag.himl->hdcImage, dst.x, dst.y, cx, cy, himlTrack->hdcImage, src.x, src.y,
632 SRCCOPY);
633 BitBlt(InternalDrag.himl->hdcMask, dst.x, dst.y, cx, cy, himlTrack->hdcMask, src.x, src.y,
634 SRCCOPY);
636 InternalDrag.himl->cCurImage = 1;
638 return TRUE;
642 /*************************************************************************
643 * ImageList_Copy [COMCTL32.@]
645 * Copies an image of the source image list to an image of the
646 * destination image list. Images can be copied or swapped.
648 * PARAMS
649 * himlDst [I] handle to the destination image list
650 * iDst [I] destination image index.
651 * himlSrc [I] handle to the source image list
652 * iSrc [I] source image index
653 * uFlags [I] flags for the copy operation
655 * RETURNS
656 * Success: TRUE
657 * Failure: FALSE
659 * NOTES
660 * Copying from one image list to another is possible. The original
661 * implementation just copies or swaps within one image list.
662 * Could this feature become a bug??? ;-)
665 BOOL WINAPI
666 ImageList_Copy (HIMAGELIST himlDst, INT iDst, HIMAGELIST himlSrc,
667 INT iSrc, UINT uFlags)
669 POINT ptSrc, ptDst;
671 TRACE("himlDst=%p iDst=%d himlSrc=%p iSrc=%d\n", himlDst, iDst, himlSrc, iSrc);
673 if (!is_valid(himlSrc) || !is_valid(himlDst))
674 return FALSE;
675 if ((iDst < 0) || (iDst >= himlDst->cCurImage))
676 return FALSE;
677 if ((iSrc < 0) || (iSrc >= himlSrc->cCurImage))
678 return FALSE;
680 imagelist_point_from_index( himlDst, iDst, &ptDst );
681 imagelist_point_from_index( himlSrc, iSrc, &ptSrc );
683 if (uFlags & ILCF_SWAP) {
684 /* swap */
685 HDC hdcBmp;
686 HBITMAP hbmTempImage, hbmTempMask;
688 hdcBmp = CreateCompatibleDC (0);
690 /* create temporary bitmaps */
691 hbmTempImage = CreateBitmap (himlSrc->cx, himlSrc->cy, 1,
692 himlSrc->uBitsPixel, NULL);
693 hbmTempMask = CreateBitmap (himlSrc->cx, himlSrc->cy, 1,
694 1, NULL);
696 /* copy (and stretch) destination to temporary bitmaps.(save) */
697 /* image */
698 SelectObject (hdcBmp, hbmTempImage);
699 StretchBlt (hdcBmp, 0, 0, himlSrc->cx, himlSrc->cy,
700 himlDst->hdcImage, ptDst.x, ptDst.y, himlDst->cx, himlDst->cy,
701 SRCCOPY);
702 /* mask */
703 SelectObject (hdcBmp, hbmTempMask);
704 StretchBlt (hdcBmp, 0, 0, himlSrc->cx, himlSrc->cy,
705 himlDst->hdcMask, ptDst.x, ptDst.y, himlDst->cx, himlDst->cy,
706 SRCCOPY);
708 /* copy (and stretch) source to destination */
709 /* image */
710 StretchBlt (himlDst->hdcImage, ptDst.x, ptDst.y, himlDst->cx, himlDst->cy,
711 himlSrc->hdcImage, ptSrc.x, ptSrc.y, himlSrc->cx, himlSrc->cy,
712 SRCCOPY);
713 /* mask */
714 StretchBlt (himlDst->hdcMask, ptDst.x, ptDst.y, himlDst->cx, himlDst->cy,
715 himlSrc->hdcMask, ptSrc.x, ptSrc.y, himlSrc->cx, himlSrc->cy,
716 SRCCOPY);
718 /* copy (without stretching) temporary bitmaps to source (restore) */
719 /* mask */
720 BitBlt (himlSrc->hdcMask, ptSrc.x, ptSrc.y, himlSrc->cx, himlSrc->cy,
721 hdcBmp, 0, 0, SRCCOPY);
723 /* image */
724 BitBlt (himlSrc->hdcImage, ptSrc.x, ptSrc.y, himlSrc->cx, himlSrc->cy,
725 hdcBmp, 0, 0, SRCCOPY);
726 /* delete temporary bitmaps */
727 DeleteObject (hbmTempMask);
728 DeleteObject (hbmTempImage);
729 DeleteDC(hdcBmp);
731 else {
732 /* copy image */
733 StretchBlt (himlDst->hdcImage, ptDst.x, ptDst.y, himlDst->cx, himlDst->cy,
734 himlSrc->hdcImage, ptSrc.x, ptSrc.y, himlSrc->cx, himlSrc->cy,
735 SRCCOPY);
737 /* copy mask */
738 StretchBlt (himlDst->hdcMask, ptDst.x, ptDst.y, himlDst->cx, himlDst->cy,
739 himlSrc->hdcMask, ptSrc.x, ptSrc.y, himlSrc->cx, himlSrc->cy,
740 SRCCOPY);
743 return TRUE;
747 /*************************************************************************
748 * ImageList_Create [COMCTL32.@]
750 * Creates a new image list.
752 * PARAMS
753 * cx [I] image height
754 * cy [I] image width
755 * flags [I] creation flags
756 * cInitial [I] initial number of images in the image list
757 * cGrow [I] number of images by which image list grows
759 * RETURNS
760 * Success: Handle to the created image list
761 * Failure: NULL
763 HIMAGELIST WINAPI
764 ImageList_Create (INT cx, INT cy, UINT flags,
765 INT cInitial, INT cGrow)
767 HIMAGELIST himl;
768 INT nCount;
769 HBITMAP hbmTemp;
770 UINT ilc = (flags & 0xFE);
771 static const WORD aBitBlend25[] =
772 {0xAA, 0x00, 0x55, 0x00, 0xAA, 0x00, 0x55, 0x00};
774 static const WORD aBitBlend50[] =
775 {0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA};
777 TRACE("(%d %d 0x%x %d %d)\n", cx, cy, flags, cInitial, cGrow);
779 if (cx < 0 || cy < 0) return NULL;
780 if (!((flags&ILC_COLORDDB) == ILC_COLORDDB) && (cx == 0 || cy == 0)) return NULL;
782 /* Create the IImageList interface for the image list */
783 if (FAILED(ImageListImpl_CreateInstance(NULL, &IID_IImageList, (void **)&himl)))
784 return NULL;
786 cGrow = (WORD)((max( cGrow, 1 ) + 3) & ~3);
788 if (cGrow > 256)
790 /* Windows doesn't limit the size here, but X11 doesn't let us allocate such huge bitmaps */
791 WARN( "grow %d too large, limiting to 256\n", cGrow );
792 cGrow = 256;
795 himl->cx = cx;
796 himl->cy = cy;
797 himl->flags = flags;
798 himl->cMaxImage = cInitial + 1;
799 himl->cInitial = cInitial;
800 himl->cGrow = cGrow;
801 himl->clrFg = CLR_DEFAULT;
802 himl->clrBk = CLR_NONE;
804 /* initialize overlay mask indices */
805 for (nCount = 0; nCount < MAX_OVERLAYIMAGE; nCount++)
806 himl->nOvlIdx[nCount] = -1;
808 /* Create Image & Mask DCs */
809 himl->hdcImage = CreateCompatibleDC (0);
810 if (!himl->hdcImage)
811 goto cleanup;
812 if (himl->flags & ILC_MASK){
813 himl->hdcMask = CreateCompatibleDC(0);
814 if (!himl->hdcMask)
815 goto cleanup;
818 /* Default to ILC_COLOR4 if none of the ILC_COLOR* flags are specified */
819 if (ilc == ILC_COLOR)
821 ilc = ILC_COLOR4;
822 himl->flags |= ILC_COLOR4;
825 if (ilc >= ILC_COLOR4 && ilc <= ILC_COLOR32)
826 himl->uBitsPixel = ilc;
827 else
828 himl->uBitsPixel = (UINT)GetDeviceCaps (himl->hdcImage, BITSPIXEL);
830 if (himl->cMaxImage > 0) {
831 himl->hbmImage = ImageList_CreateImage(himl->hdcImage, himl, himl->cMaxImage);
832 SelectObject(himl->hdcImage, himl->hbmImage);
833 } else
834 himl->hbmImage = 0;
836 if ((himl->cMaxImage > 0) && (himl->flags & ILC_MASK)) {
837 SIZE sz;
839 imagelist_get_bitmap_size(himl, himl->cMaxImage, &sz);
840 himl->hbmMask = CreateBitmap (sz.cx, sz.cy, 1, 1, NULL);
841 if (himl->hbmMask == 0) {
842 ERR("Error creating mask bitmap!\n");
843 goto cleanup;
845 SelectObject(himl->hdcMask, himl->hbmMask);
847 else
848 himl->hbmMask = 0;
850 if (ilc == ILC_COLOR32)
851 himl->has_alpha = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, himl->cMaxImage );
852 else
853 himl->has_alpha = NULL;
855 /* create blending brushes */
856 hbmTemp = CreateBitmap (8, 8, 1, 1, aBitBlend25);
857 himl->hbrBlend25 = CreatePatternBrush (hbmTemp);
858 DeleteObject (hbmTemp);
860 hbmTemp = CreateBitmap (8, 8, 1, 1, aBitBlend50);
861 himl->hbrBlend50 = CreatePatternBrush (hbmTemp);
862 DeleteObject (hbmTemp);
864 TRACE("created imagelist %p\n", himl);
865 return himl;
867 cleanup:
868 ImageList_Destroy(himl);
869 return NULL;
873 /*************************************************************************
874 * ImageList_Destroy [COMCTL32.@]
876 * Destroys an image list.
878 * PARAMS
879 * himl [I] handle to image list
881 * RETURNS
882 * Success: TRUE
883 * Failure: FALSE
886 BOOL WINAPI
887 ImageList_Destroy (HIMAGELIST himl)
889 if (!is_valid(himl))
890 return FALSE;
892 IImageList_Release((IImageList *) himl);
893 return TRUE;
897 /*************************************************************************
898 * ImageList_DragEnter [COMCTL32.@]
900 * Locks window update and displays the drag image at the given position.
902 * PARAMS
903 * hwndLock [I] handle of the window that owns the drag image.
904 * x [I] X position of the drag image.
905 * y [I] Y position of the drag image.
907 * RETURNS
908 * Success: TRUE
909 * Failure: FALSE
911 * NOTES
912 * The position of the drag image is relative to the window, not
913 * the client area.
916 BOOL WINAPI
917 ImageList_DragEnter (HWND hwndLock, INT x, INT y)
919 TRACE("(hwnd=%p x=%d y=%d)\n", hwndLock, x, y);
921 if (!is_valid(InternalDrag.himl))
922 return FALSE;
924 if (hwndLock)
925 InternalDrag.hwnd = hwndLock;
926 else
927 InternalDrag.hwnd = GetDesktopWindow ();
929 InternalDrag.x = x;
930 InternalDrag.y = y;
932 /* draw the drag image and save the background */
933 return ImageList_DragShowNolock(TRUE);
937 /*************************************************************************
938 * ImageList_DragLeave [COMCTL32.@]
940 * Unlocks window update and hides the drag image.
942 * PARAMS
943 * hwndLock [I] handle of the window that owns the drag image.
945 * RETURNS
946 * Success: TRUE
947 * Failure: FALSE
950 BOOL WINAPI
951 ImageList_DragLeave (HWND hwndLock)
953 /* As we don't save drag info in the window this can lead to problems if
954 an app does not supply the same window as DragEnter */
955 /* if (hwndLock)
956 InternalDrag.hwnd = hwndLock;
957 else
958 InternalDrag.hwnd = GetDesktopWindow (); */
959 if(!hwndLock)
960 hwndLock = GetDesktopWindow();
961 if(InternalDrag.hwnd != hwndLock)
962 FIXME("DragLeave hWnd != DragEnter hWnd\n");
964 ImageList_DragShowNolock (FALSE);
966 return TRUE;
970 /*************************************************************************
971 * ImageList_InternalDragDraw [Internal]
973 * Draws the drag image.
975 * PARAMS
976 * hdc [I] device context to draw into.
977 * x [I] X position of the drag image.
978 * y [I] Y position of the drag image.
980 * RETURNS
981 * Success: TRUE
982 * Failure: FALSE
984 * NOTES
985 * The position of the drag image is relative to the window, not
986 * the client area.
990 static inline void
991 ImageList_InternalDragDraw (HDC hdc, INT x, INT y)
993 IMAGELISTDRAWPARAMS imldp;
995 ZeroMemory (&imldp, sizeof(imldp));
996 imldp.cbSize = sizeof(imldp);
997 imldp.himl = InternalDrag.himl;
998 imldp.i = 0;
999 imldp.hdcDst = hdc,
1000 imldp.x = x;
1001 imldp.y = y;
1002 imldp.rgbBk = CLR_DEFAULT;
1003 imldp.rgbFg = CLR_DEFAULT;
1004 imldp.fStyle = ILD_NORMAL;
1005 imldp.fState = ILS_ALPHA;
1006 imldp.Frame = 192;
1007 ImageList_DrawIndirect (&imldp);
1010 /*************************************************************************
1011 * ImageList_DragMove [COMCTL32.@]
1013 * Moves the drag image.
1015 * PARAMS
1016 * x [I] X position of the drag image.
1017 * y [I] Y position of the drag image.
1019 * RETURNS
1020 * Success: TRUE
1021 * Failure: FALSE
1023 * NOTES
1024 * The position of the drag image is relative to the window, not
1025 * the client area.
1028 BOOL WINAPI
1029 ImageList_DragMove (INT x, INT y)
1031 TRACE("(x=%d y=%d)\n", x, y);
1033 if (!is_valid(InternalDrag.himl))
1034 return FALSE;
1036 /* draw/update the drag image */
1037 if (InternalDrag.bShow) {
1038 HDC hdcDrag;
1039 HDC hdcOffScreen;
1040 HDC hdcBg;
1041 HBITMAP hbmOffScreen;
1042 INT origNewX, origNewY;
1043 INT origOldX, origOldY;
1044 INT origRegX, origRegY;
1045 INT sizeRegX, sizeRegY;
1048 /* calculate the update region */
1049 origNewX = x - InternalDrag.dxHotspot;
1050 origNewY = y - InternalDrag.dyHotspot;
1051 origOldX = InternalDrag.x - InternalDrag.dxHotspot;
1052 origOldY = InternalDrag.y - InternalDrag.dyHotspot;
1053 origRegX = min(origNewX, origOldX);
1054 origRegY = min(origNewY, origOldY);
1055 sizeRegX = InternalDrag.himl->cx + abs(x - InternalDrag.x);
1056 sizeRegY = InternalDrag.himl->cy + abs(y - InternalDrag.y);
1058 hdcDrag = GetDCEx(InternalDrag.hwnd, 0,
1059 DCX_WINDOW | DCX_CACHE | DCX_LOCKWINDOWUPDATE);
1060 hdcOffScreen = CreateCompatibleDC(hdcDrag);
1061 hdcBg = CreateCompatibleDC(hdcDrag);
1063 hbmOffScreen = CreateCompatibleBitmap(hdcDrag, sizeRegX, sizeRegY);
1064 SelectObject(hdcOffScreen, hbmOffScreen);
1065 SelectObject(hdcBg, InternalDrag.hbmBg);
1067 /* get the actual background of the update region */
1068 BitBlt(hdcOffScreen, 0, 0, sizeRegX, sizeRegY, hdcDrag,
1069 origRegX, origRegY, SRCCOPY);
1070 /* erase the old image */
1071 BitBlt(hdcOffScreen, origOldX - origRegX, origOldY - origRegY,
1072 InternalDrag.himl->cx, InternalDrag.himl->cy, hdcBg, 0, 0,
1073 SRCCOPY);
1074 /* save the background */
1075 BitBlt(hdcBg, 0, 0, InternalDrag.himl->cx, InternalDrag.himl->cy,
1076 hdcOffScreen, origNewX - origRegX, origNewY - origRegY, SRCCOPY);
1077 /* draw the image */
1078 ImageList_InternalDragDraw(hdcOffScreen, origNewX - origRegX,
1079 origNewY - origRegY);
1080 /* draw the update region to the screen */
1081 BitBlt(hdcDrag, origRegX, origRegY, sizeRegX, sizeRegY,
1082 hdcOffScreen, 0, 0, SRCCOPY);
1084 DeleteDC(hdcBg);
1085 DeleteDC(hdcOffScreen);
1086 DeleteObject(hbmOffScreen);
1087 ReleaseDC(InternalDrag.hwnd, hdcDrag);
1090 /* update the image position */
1091 InternalDrag.x = x;
1092 InternalDrag.y = y;
1094 return TRUE;
1098 /*************************************************************************
1099 * ImageList_DragShowNolock [COMCTL32.@]
1101 * Shows or hides the drag image.
1103 * PARAMS
1104 * bShow [I] TRUE shows the drag image, FALSE hides it.
1106 * RETURNS
1107 * Success: TRUE
1108 * Failure: FALSE
1111 BOOL WINAPI
1112 ImageList_DragShowNolock (BOOL bShow)
1114 HDC hdcDrag;
1115 HDC hdcBg;
1116 INT x, y;
1118 if (!is_valid(InternalDrag.himl))
1119 return FALSE;
1121 TRACE("bShow=0x%X!\n", bShow);
1123 /* DragImage is already visible/hidden */
1124 if ((InternalDrag.bShow && bShow) || (!InternalDrag.bShow && !bShow)) {
1125 return FALSE;
1128 /* position of the origin of the DragImage */
1129 x = InternalDrag.x - InternalDrag.dxHotspot;
1130 y = InternalDrag.y - InternalDrag.dyHotspot;
1132 hdcDrag = GetDCEx (InternalDrag.hwnd, 0,
1133 DCX_WINDOW | DCX_CACHE | DCX_LOCKWINDOWUPDATE);
1134 if (!hdcDrag) {
1135 return FALSE;
1138 hdcBg = CreateCompatibleDC(hdcDrag);
1139 if (!InternalDrag.hbmBg) {
1140 InternalDrag.hbmBg = CreateCompatibleBitmap(hdcDrag,
1141 InternalDrag.himl->cx, InternalDrag.himl->cy);
1143 SelectObject(hdcBg, InternalDrag.hbmBg);
1145 if (bShow) {
1146 /* save the background */
1147 BitBlt(hdcBg, 0, 0, InternalDrag.himl->cx, InternalDrag.himl->cy,
1148 hdcDrag, x, y, SRCCOPY);
1149 /* show the image */
1150 ImageList_InternalDragDraw(hdcDrag, x, y);
1151 } else {
1152 /* hide the image */
1153 BitBlt(hdcDrag, x, y, InternalDrag.himl->cx, InternalDrag.himl->cy,
1154 hdcBg, 0, 0, SRCCOPY);
1157 InternalDrag.bShow = !InternalDrag.bShow;
1159 DeleteDC(hdcBg);
1160 ReleaseDC (InternalDrag.hwnd, hdcDrag);
1161 return TRUE;
1165 /*************************************************************************
1166 * ImageList_Draw [COMCTL32.@]
1168 * Draws an image.
1170 * PARAMS
1171 * himl [I] handle to image list
1172 * i [I] image index
1173 * hdc [I] handle to device context
1174 * x [I] x position
1175 * y [I] y position
1176 * fStyle [I] drawing flags
1178 * RETURNS
1179 * Success: TRUE
1180 * Failure: FALSE
1182 * SEE
1183 * ImageList_DrawEx.
1186 BOOL WINAPI
1187 ImageList_Draw (HIMAGELIST himl, INT i, HDC hdc, INT x, INT y, UINT fStyle)
1189 return ImageList_DrawEx (himl, i, hdc, x, y, 0, 0,
1190 CLR_DEFAULT, CLR_DEFAULT, fStyle);
1194 /*************************************************************************
1195 * ImageList_DrawEx [COMCTL32.@]
1197 * Draws an image and allows using extended drawing features.
1199 * PARAMS
1200 * himl [I] handle to image list
1201 * i [I] image index
1202 * hdc [I] handle to device context
1203 * x [I] X position
1204 * y [I] Y position
1205 * dx [I] X offset
1206 * dy [I] Y offset
1207 * rgbBk [I] background color
1208 * rgbFg [I] foreground color
1209 * fStyle [I] drawing flags
1211 * RETURNS
1212 * Success: TRUE
1213 * Failure: FALSE
1215 * NOTES
1216 * Calls ImageList_DrawIndirect.
1218 * SEE
1219 * ImageList_DrawIndirect.
1222 BOOL WINAPI
1223 ImageList_DrawEx (HIMAGELIST himl, INT i, HDC hdc, INT x, INT y,
1224 INT dx, INT dy, COLORREF rgbBk, COLORREF rgbFg,
1225 UINT fStyle)
1227 IMAGELISTDRAWPARAMS imldp;
1229 ZeroMemory (&imldp, sizeof(imldp));
1230 imldp.cbSize = sizeof(imldp);
1231 imldp.himl = himl;
1232 imldp.i = i;
1233 imldp.hdcDst = hdc,
1234 imldp.x = x;
1235 imldp.y = y;
1236 imldp.cx = dx;
1237 imldp.cy = dy;
1238 imldp.rgbBk = rgbBk;
1239 imldp.rgbFg = rgbFg;
1240 imldp.fStyle = fStyle;
1242 return ImageList_DrawIndirect (&imldp);
1246 static BOOL alpha_blend_image( HIMAGELIST himl, HDC dest_dc, int dest_x, int dest_y,
1247 int src_x, int src_y, int cx, int cy, BLENDFUNCTION func,
1248 UINT style, COLORREF blend_col )
1250 BOOL ret = FALSE;
1251 HDC hdc;
1252 HBITMAP bmp = 0, mask = 0;
1253 BITMAPINFO *info;
1254 void *bits, *mask_bits;
1255 unsigned int *ptr;
1256 int i, j;
1258 if (!(hdc = CreateCompatibleDC( 0 ))) return FALSE;
1259 if (!(info = HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET( BITMAPINFO, bmiColors[256] )))) goto done;
1260 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1261 info->bmiHeader.biWidth = cx;
1262 info->bmiHeader.biHeight = cy;
1263 info->bmiHeader.biPlanes = 1;
1264 info->bmiHeader.biBitCount = 32;
1265 info->bmiHeader.biCompression = BI_RGB;
1266 info->bmiHeader.biSizeImage = cx * cy * 4;
1267 info->bmiHeader.biXPelsPerMeter = 0;
1268 info->bmiHeader.biYPelsPerMeter = 0;
1269 info->bmiHeader.biClrUsed = 0;
1270 info->bmiHeader.biClrImportant = 0;
1271 if (!(bmp = CreateDIBSection( himl->hdcImage, info, DIB_RGB_COLORS, &bits, 0, 0 ))) goto done;
1272 SelectObject( hdc, bmp );
1273 BitBlt( hdc, 0, 0, cx, cy, himl->hdcImage, src_x, src_y, SRCCOPY );
1275 if (blend_col != CLR_NONE)
1277 BYTE r = GetRValue( blend_col );
1278 BYTE g = GetGValue( blend_col );
1279 BYTE b = GetBValue( blend_col );
1281 if (style & ILD_BLEND25)
1283 for (i = 0, ptr = bits; i < cx * cy; i++, ptr++)
1284 *ptr = ((*ptr & 0xff000000) |
1285 ((((*ptr & 0x00ff0000) * 3 + (r << 16)) / 4) & 0x00ff0000) |
1286 ((((*ptr & 0x0000ff00) * 3 + (g << 8)) / 4) & 0x0000ff00) |
1287 ((((*ptr & 0x000000ff) * 3 + (b << 0)) / 4) & 0x000000ff));
1289 else if (style & ILD_BLEND50)
1291 for (i = 0, ptr = bits; i < cx * cy; i++, ptr++)
1292 *ptr = ((*ptr & 0xff000000) |
1293 ((((*ptr & 0x00ff0000) + (r << 16)) / 2) & 0x00ff0000) |
1294 ((((*ptr & 0x0000ff00) + (g << 8)) / 2) & 0x0000ff00) |
1295 ((((*ptr & 0x000000ff) + (b << 0)) / 2) & 0x000000ff));
1299 if (himl->has_alpha) /* we already have an alpha channel in this case */
1301 /* pre-multiply by the alpha channel */
1302 for (i = 0, ptr = bits; i < cx * cy; i++, ptr++)
1304 DWORD alpha = *ptr >> 24;
1305 *ptr = ((*ptr & 0xff000000) |
1306 (((*ptr & 0x00ff0000) * alpha / 255) & 0x00ff0000) |
1307 (((*ptr & 0x0000ff00) * alpha / 255) & 0x0000ff00) |
1308 (((*ptr & 0x000000ff) * alpha / 255)));
1311 else if (himl->hbmMask)
1313 unsigned int width_bytes = (cx + 31) / 32 * 4;
1314 /* generate alpha channel from the mask */
1315 info->bmiHeader.biBitCount = 1;
1316 info->bmiHeader.biSizeImage = width_bytes * cy;
1317 info->bmiColors[0].rgbRed = 0;
1318 info->bmiColors[0].rgbGreen = 0;
1319 info->bmiColors[0].rgbBlue = 0;
1320 info->bmiColors[0].rgbReserved = 0;
1321 info->bmiColors[1].rgbRed = 0xff;
1322 info->bmiColors[1].rgbGreen = 0xff;
1323 info->bmiColors[1].rgbBlue = 0xff;
1324 info->bmiColors[1].rgbReserved = 0;
1325 if (!(mask = CreateDIBSection( himl->hdcMask, info, DIB_RGB_COLORS, &mask_bits, 0, 0 )))
1326 goto done;
1327 SelectObject( hdc, mask );
1328 BitBlt( hdc, 0, 0, cx, cy, himl->hdcMask, src_x, src_y, SRCCOPY );
1329 SelectObject( hdc, bmp );
1330 for (i = 0, ptr = bits; i < cy; i++)
1331 for (j = 0; j < cx; j++, ptr++)
1332 if ((((BYTE *)mask_bits)[i * width_bytes + j / 8] << (j % 8)) & 0x80) *ptr = 0;
1333 else *ptr |= 0xff000000;
1336 ret = GdiAlphaBlend( dest_dc, dest_x, dest_y, cx, cy, hdc, 0, 0, cx, cy, func );
1338 done:
1339 DeleteDC( hdc );
1340 if (bmp) DeleteObject( bmp );
1341 if (mask) DeleteObject( mask );
1342 HeapFree( GetProcessHeap(), 0, info );
1343 return ret;
1346 /*************************************************************************
1347 * ImageList_DrawIndirect [COMCTL32.@]
1349 * Draws an image using various parameters specified in pimldp.
1351 * PARAMS
1352 * pimldp [I] pointer to IMAGELISTDRAWPARAMS structure.
1354 * RETURNS
1355 * Success: TRUE
1356 * Failure: FALSE
1359 BOOL WINAPI
1360 ImageList_DrawIndirect (IMAGELISTDRAWPARAMS *pimldp)
1362 INT cx, cy, nOvlIdx;
1363 DWORD fState, dwRop;
1364 UINT fStyle;
1365 COLORREF oldImageBk, oldImageFg;
1366 HDC hImageDC, hImageListDC, hMaskListDC;
1367 HBITMAP hImageBmp, hOldImageBmp, hBlendMaskBmp;
1368 BOOL bIsTransparent, bBlend, bResult = FALSE, bMask;
1369 HIMAGELIST himl;
1370 HBRUSH hOldBrush;
1371 POINT pt;
1372 BOOL has_alpha;
1374 if (!pimldp || !(himl = pimldp->himl)) return FALSE;
1375 if (!is_valid(himl)) return FALSE;
1376 if ((pimldp->i < 0) || (pimldp->i >= himl->cCurImage)) return FALSE;
1378 imagelist_point_from_index( himl, pimldp->i, &pt );
1379 pt.x += pimldp->xBitmap;
1380 pt.y += pimldp->yBitmap;
1382 fState = pimldp->cbSize < sizeof(IMAGELISTDRAWPARAMS) ? ILS_NORMAL : pimldp->fState;
1383 fStyle = pimldp->fStyle & ~ILD_OVERLAYMASK;
1384 cx = (pimldp->cx == 0) ? himl->cx : pimldp->cx;
1385 cy = (pimldp->cy == 0) ? himl->cy : pimldp->cy;
1387 bIsTransparent = (fStyle & ILD_TRANSPARENT);
1388 if( pimldp->rgbBk == CLR_NONE )
1389 bIsTransparent = TRUE;
1390 if( ( pimldp->rgbBk == CLR_DEFAULT ) && ( himl->clrBk == CLR_NONE ) )
1391 bIsTransparent = TRUE;
1392 bMask = (himl->flags & ILC_MASK) && (fStyle & ILD_MASK) ;
1393 bBlend = (fStyle & (ILD_BLEND25 | ILD_BLEND50) ) && !bMask;
1395 TRACE("himl(%p) hbmMask(%p) iImage(%d) x(%d) y(%d) cx(%d) cy(%d)\n",
1396 himl, himl->hbmMask, pimldp->i, pimldp->x, pimldp->y, cx, cy);
1398 /* we will use these DCs to access the images and masks in the ImageList */
1399 hImageListDC = himl->hdcImage;
1400 hMaskListDC = himl->hdcMask;
1402 /* these will accumulate the image and mask for the image we're drawing */
1403 hImageDC = CreateCompatibleDC( pimldp->hdcDst );
1404 hImageBmp = CreateCompatibleBitmap( pimldp->hdcDst, cx, cy );
1405 hBlendMaskBmp = bBlend ? CreateBitmap(cx, cy, 1, 1, NULL) : 0;
1407 /* Create a compatible DC. */
1408 if (!hImageListDC || !hImageDC || !hImageBmp ||
1409 (bBlend && !hBlendMaskBmp) || (himl->hbmMask && !hMaskListDC))
1410 goto cleanup;
1412 hOldImageBmp = SelectObject(hImageDC, hImageBmp);
1415 * To obtain a transparent look, background color should be set
1416 * to white and foreground color to black when blitting the
1417 * monochrome mask.
1419 oldImageFg = SetTextColor( hImageDC, RGB( 0, 0, 0 ) );
1420 oldImageBk = SetBkColor( hImageDC, RGB( 0xff, 0xff, 0xff ) );
1422 has_alpha = (himl->has_alpha && himl->has_alpha[pimldp->i]);
1423 if (!bMask && (has_alpha || (fState & ILS_ALPHA)))
1425 COLORREF colour, blend_col = CLR_NONE;
1426 BLENDFUNCTION func;
1428 if (bBlend)
1430 blend_col = pimldp->rgbFg;
1431 if (blend_col == CLR_DEFAULT) blend_col = GetSysColor( COLOR_HIGHLIGHT );
1432 else if (blend_col == CLR_NONE) blend_col = GetTextColor( pimldp->hdcDst );
1435 func.BlendOp = AC_SRC_OVER;
1436 func.BlendFlags = 0;
1437 func.SourceConstantAlpha = (fState & ILS_ALPHA) ? pimldp->Frame : 255;
1438 func.AlphaFormat = AC_SRC_ALPHA;
1440 if (bIsTransparent)
1442 bResult = alpha_blend_image( himl, pimldp->hdcDst, pimldp->x, pimldp->y,
1443 pt.x, pt.y, cx, cy, func, fStyle, blend_col );
1444 goto end;
1446 colour = pimldp->rgbBk;
1447 if (colour == CLR_DEFAULT) colour = himl->clrBk;
1448 if (colour == CLR_NONE) colour = GetBkColor( pimldp->hdcDst );
1450 hOldBrush = SelectObject (hImageDC, CreateSolidBrush (colour));
1451 PatBlt( hImageDC, 0, 0, cx, cy, PATCOPY );
1452 alpha_blend_image( himl, hImageDC, 0, 0, pt.x, pt.y, cx, cy, func, fStyle, blend_col );
1453 DeleteObject (SelectObject (hImageDC, hOldBrush));
1454 bResult = BitBlt( pimldp->hdcDst, pimldp->x, pimldp->y, cx, cy, hImageDC, 0, 0, SRCCOPY );
1455 goto end;
1459 * Draw the initial image
1461 if( bMask ) {
1462 if (himl->hbmMask) {
1463 hOldBrush = SelectObject (hImageDC, CreateSolidBrush (GetTextColor(pimldp->hdcDst)));
1464 PatBlt( hImageDC, 0, 0, cx, cy, PATCOPY );
1465 BitBlt(hImageDC, 0, 0, cx, cy, hMaskListDC, pt.x, pt.y, SRCPAINT);
1466 DeleteObject (SelectObject (hImageDC, hOldBrush));
1467 if( bIsTransparent )
1469 BitBlt ( pimldp->hdcDst, pimldp->x, pimldp->y, cx, cy, hImageDC, 0, 0, SRCAND);
1470 bResult = TRUE;
1471 goto end;
1473 } else {
1474 hOldBrush = SelectObject (hImageDC, GetStockObject(BLACK_BRUSH));
1475 PatBlt( hImageDC, 0, 0, cx, cy, PATCOPY);
1476 SelectObject(hImageDC, hOldBrush);
1478 } else {
1479 /* blend the image with the needed solid background */
1480 COLORREF colour = RGB(0,0,0);
1482 if( !bIsTransparent )
1484 colour = pimldp->rgbBk;
1485 if( colour == CLR_DEFAULT )
1486 colour = himl->clrBk;
1487 if( colour == CLR_NONE )
1488 colour = GetBkColor(pimldp->hdcDst);
1491 hOldBrush = SelectObject (hImageDC, CreateSolidBrush (colour));
1492 PatBlt( hImageDC, 0, 0, cx, cy, PATCOPY );
1493 if (himl->hbmMask)
1495 BitBlt( hImageDC, 0, 0, cx, cy, hMaskListDC, pt.x, pt.y, SRCAND );
1496 BitBlt( hImageDC, 0, 0, cx, cy, hImageListDC, pt.x, pt.y, SRCPAINT );
1498 else
1499 BitBlt( hImageDC, 0, 0, cx, cy, hImageListDC, pt.x, pt.y, SRCCOPY);
1500 DeleteObject (SelectObject (hImageDC, hOldBrush));
1503 /* Time for blending, if required */
1504 if (bBlend) {
1505 HBRUSH hBlendBrush;
1506 COLORREF clrBlend = pimldp->rgbFg;
1507 HDC hBlendMaskDC = hImageListDC;
1508 HBITMAP hOldBitmap;
1510 /* Create the blend Mask */
1511 hOldBitmap = SelectObject(hBlendMaskDC, hBlendMaskBmp);
1512 hBlendBrush = fStyle & ILD_BLEND50 ? himl->hbrBlend50 : himl->hbrBlend25;
1513 hOldBrush = SelectObject(hBlendMaskDC, hBlendBrush);
1514 PatBlt(hBlendMaskDC, 0, 0, cx, cy, PATCOPY);
1515 SelectObject(hBlendMaskDC, hOldBrush);
1517 /* Modify the blend mask if an Image Mask exist */
1518 if(himl->hbmMask) {
1519 BitBlt(hBlendMaskDC, 0, 0, cx, cy, hMaskListDC, pt.x, pt.y, 0x220326); /* NOTSRCAND */
1520 BitBlt(hBlendMaskDC, 0, 0, cx, cy, hBlendMaskDC, 0, 0, NOTSRCCOPY);
1523 /* now apply blend to the current image given the BlendMask */
1524 if (clrBlend == CLR_DEFAULT) clrBlend = GetSysColor (COLOR_HIGHLIGHT);
1525 else if (clrBlend == CLR_NONE) clrBlend = GetTextColor (pimldp->hdcDst);
1526 hOldBrush = SelectObject (hImageDC, CreateSolidBrush(clrBlend));
1527 BitBlt (hImageDC, 0, 0, cx, cy, hBlendMaskDC, 0, 0, 0xB8074A); /* PSDPxax */
1528 DeleteObject(SelectObject(hImageDC, hOldBrush));
1529 SelectObject(hBlendMaskDC, hOldBitmap);
1532 /* Now do the overlay image, if any */
1533 nOvlIdx = (pimldp->fStyle & ILD_OVERLAYMASK) >> 8;
1534 if ( (nOvlIdx >= 1) && (nOvlIdx <= MAX_OVERLAYIMAGE)) {
1535 nOvlIdx = himl->nOvlIdx[nOvlIdx - 1];
1536 if ((nOvlIdx >= 0) && (nOvlIdx < himl->cCurImage)) {
1537 POINT ptOvl;
1538 imagelist_point_from_index( himl, nOvlIdx, &ptOvl );
1539 ptOvl.x += pimldp->xBitmap;
1540 if (himl->hbmMask && !(fStyle & ILD_IMAGE))
1541 BitBlt (hImageDC, 0, 0, cx, cy, hMaskListDC, ptOvl.x, ptOvl.y, SRCAND);
1542 BitBlt (hImageDC, 0, 0, cx, cy, hImageListDC, ptOvl.x, ptOvl.y, SRCPAINT);
1546 if (fState & ILS_SATURATE) FIXME("ILS_SATURATE: unimplemented!\n");
1547 if (fState & ILS_GLOW) FIXME("ILS_GLOW: unimplemented!\n");
1548 if (fState & ILS_SHADOW) FIXME("ILS_SHADOW: unimplemented!\n");
1550 if (fStyle & ILD_PRESERVEALPHA) FIXME("ILD_PRESERVEALPHA: unimplemented!\n");
1551 if (fStyle & ILD_SCALE) FIXME("ILD_SCALE: unimplemented!\n");
1552 if (fStyle & ILD_DPISCALE) FIXME("ILD_DPISCALE: unimplemented!\n");
1554 /* now copy the image to the screen */
1555 dwRop = SRCCOPY;
1556 if (himl->hbmMask && bIsTransparent ) {
1557 COLORREF oldDstFg = SetTextColor(pimldp->hdcDst, RGB( 0, 0, 0 ) );
1558 COLORREF oldDstBk = SetBkColor(pimldp->hdcDst, RGB( 0xff, 0xff, 0xff ));
1559 BitBlt (pimldp->hdcDst, pimldp->x, pimldp->y, cx, cy, hMaskListDC, pt.x, pt.y, SRCAND);
1560 SetBkColor(pimldp->hdcDst, oldDstBk);
1561 SetTextColor(pimldp->hdcDst, oldDstFg);
1562 dwRop = SRCPAINT;
1564 if (fStyle & ILD_ROP) dwRop = pimldp->dwRop;
1565 BitBlt (pimldp->hdcDst, pimldp->x, pimldp->y, cx, cy, hImageDC, 0, 0, dwRop);
1567 bResult = TRUE;
1568 end:
1569 /* cleanup the mess */
1570 SetBkColor(hImageDC, oldImageBk);
1571 SetTextColor(hImageDC, oldImageFg);
1572 SelectObject(hImageDC, hOldImageBmp);
1573 cleanup:
1574 DeleteObject(hBlendMaskBmp);
1575 DeleteObject(hImageBmp);
1576 DeleteDC(hImageDC);
1578 return bResult;
1582 /*************************************************************************
1583 * ImageList_Duplicate [COMCTL32.@]
1585 * Duplicates an image list.
1587 * PARAMS
1588 * himlSrc [I] source image list handle
1590 * RETURNS
1591 * Success: Handle of duplicated image list.
1592 * Failure: NULL
1595 HIMAGELIST WINAPI
1596 ImageList_Duplicate (HIMAGELIST himlSrc)
1598 HIMAGELIST himlDst;
1600 if (!is_valid(himlSrc)) {
1601 ERR("Invalid image list handle!\n");
1602 return NULL;
1605 himlDst = ImageList_Create (himlSrc->cx, himlSrc->cy, himlSrc->flags,
1606 himlSrc->cCurImage, himlSrc->cGrow);
1608 if (himlDst)
1610 SIZE sz;
1612 imagelist_get_bitmap_size(himlSrc, himlSrc->cCurImage, &sz);
1613 BitBlt (himlDst->hdcImage, 0, 0, sz.cx, sz.cy,
1614 himlSrc->hdcImage, 0, 0, SRCCOPY);
1616 if (himlDst->hbmMask)
1617 BitBlt (himlDst->hdcMask, 0, 0, sz.cx, sz.cy,
1618 himlSrc->hdcMask, 0, 0, SRCCOPY);
1620 himlDst->cCurImage = himlSrc->cCurImage;
1621 if (himlSrc->has_alpha && himlDst->has_alpha)
1622 memcpy( himlDst->has_alpha, himlSrc->has_alpha, himlDst->cCurImage );
1624 return himlDst;
1628 /*************************************************************************
1629 * ImageList_EndDrag [COMCTL32.@]
1631 * Finishes a drag operation.
1633 * PARAMS
1634 * no Parameters
1636 * RETURNS
1637 * Success: TRUE
1638 * Failure: FALSE
1641 VOID WINAPI
1642 ImageList_EndDrag (void)
1644 /* cleanup the InternalDrag struct */
1645 InternalDrag.hwnd = 0;
1646 if (InternalDrag.himl != InternalDrag.himlNoCursor)
1647 ImageList_Destroy (InternalDrag.himlNoCursor);
1648 ImageList_Destroy (InternalDrag.himl);
1649 InternalDrag.himlNoCursor = InternalDrag.himl = 0;
1650 InternalDrag.x= 0;
1651 InternalDrag.y= 0;
1652 InternalDrag.dxHotspot = 0;
1653 InternalDrag.dyHotspot = 0;
1654 InternalDrag.bShow = FALSE;
1655 DeleteObject(InternalDrag.hbmBg);
1656 InternalDrag.hbmBg = 0;
1660 /*************************************************************************
1661 * ImageList_GetBkColor [COMCTL32.@]
1663 * Returns the background color of an image list.
1665 * PARAMS
1666 * himl [I] Image list handle.
1668 * RETURNS
1669 * Success: background color
1670 * Failure: CLR_NONE
1673 COLORREF WINAPI
1674 ImageList_GetBkColor (HIMAGELIST himl)
1676 return himl ? himl->clrBk : CLR_NONE;
1680 /*************************************************************************
1681 * ImageList_GetDragImage [COMCTL32.@]
1683 * Returns the handle to the internal drag image list.
1685 * PARAMS
1686 * ppt [O] Pointer to the drag position. Can be NULL.
1687 * pptHotspot [O] Pointer to the position of the hot spot. Can be NULL.
1689 * RETURNS
1690 * Success: Handle of the drag image list.
1691 * Failure: NULL.
1694 HIMAGELIST WINAPI
1695 ImageList_GetDragImage (POINT *ppt, POINT *pptHotspot)
1697 if (is_valid(InternalDrag.himl)) {
1698 if (ppt) {
1699 ppt->x = InternalDrag.x;
1700 ppt->y = InternalDrag.y;
1702 if (pptHotspot) {
1703 pptHotspot->x = InternalDrag.dxHotspot;
1704 pptHotspot->y = InternalDrag.dyHotspot;
1706 return (InternalDrag.himl);
1709 return NULL;
1713 /*************************************************************************
1714 * ImageList_GetFlags [COMCTL32.@]
1716 * Gets the flags of the specified image list.
1718 * PARAMS
1719 * himl [I] Handle to image list
1721 * RETURNS
1722 * Image list flags.
1724 * BUGS
1725 * Stub.
1728 DWORD WINAPI
1729 ImageList_GetFlags(HIMAGELIST himl)
1731 TRACE("%p\n", himl);
1733 return is_valid(himl) ? himl->flags : 0;
1737 /*************************************************************************
1738 * ImageList_GetIcon [COMCTL32.@]
1740 * Creates an icon from a masked image of an image list.
1742 * PARAMS
1743 * himl [I] handle to image list
1744 * i [I] image index
1745 * flags [I] drawing style flags
1747 * RETURNS
1748 * Success: icon handle
1749 * Failure: NULL
1752 HICON WINAPI
1753 ImageList_GetIcon (HIMAGELIST himl, INT i, UINT fStyle)
1755 ICONINFO ii;
1756 HICON hIcon;
1757 HBITMAP hOldDstBitmap;
1758 HDC hdcDst;
1759 POINT pt;
1761 TRACE("%p %d %d\n", himl, i, fStyle);
1762 if (!is_valid(himl) || (i < 0) || (i >= himl->cCurImage)) return NULL;
1764 ii.fIcon = TRUE;
1765 ii.xHotspot = 0;
1766 ii.yHotspot = 0;
1768 /* create colour bitmap */
1769 hdcDst = GetDC(0);
1770 ii.hbmColor = CreateCompatibleBitmap(hdcDst, himl->cx, himl->cy);
1771 ReleaseDC(0, hdcDst);
1773 hdcDst = CreateCompatibleDC(0);
1775 imagelist_point_from_index( himl, i, &pt );
1777 /* draw mask*/
1778 ii.hbmMask = CreateBitmap (himl->cx, himl->cy, 1, 1, NULL);
1779 hOldDstBitmap = SelectObject (hdcDst, ii.hbmMask);
1780 if (himl->hbmMask) {
1781 BitBlt (hdcDst, 0, 0, himl->cx, himl->cy,
1782 himl->hdcMask, pt.x, pt.y, SRCCOPY);
1784 else
1785 PatBlt (hdcDst, 0, 0, himl->cx, himl->cy, BLACKNESS);
1787 /* draw image*/
1788 SelectObject (hdcDst, ii.hbmColor);
1789 BitBlt (hdcDst, 0, 0, himl->cx, himl->cy,
1790 himl->hdcImage, pt.x, pt.y, SRCCOPY);
1793 * CreateIconIndirect requires us to deselect the bitmaps from
1794 * the DCs before calling
1796 SelectObject(hdcDst, hOldDstBitmap);
1798 hIcon = CreateIconIndirect (&ii);
1800 DeleteObject (ii.hbmMask);
1801 DeleteObject (ii.hbmColor);
1802 DeleteDC (hdcDst);
1804 return hIcon;
1808 /*************************************************************************
1809 * ImageList_GetIconSize [COMCTL32.@]
1811 * Retrieves the size of an image in an image list.
1813 * PARAMS
1814 * himl [I] handle to image list
1815 * cx [O] pointer to the image width.
1816 * cy [O] pointer to the image height.
1818 * RETURNS
1819 * Success: TRUE
1820 * Failure: FALSE
1822 * NOTES
1823 * All images in an image list have the same size.
1826 BOOL WINAPI
1827 ImageList_GetIconSize (HIMAGELIST himl, INT *cx, INT *cy)
1829 if (!is_valid(himl) || !cx || !cy)
1830 return FALSE;
1832 *cx = himl->cx;
1833 *cy = himl->cy;
1835 return TRUE;
1839 /*************************************************************************
1840 * ImageList_GetImageCount [COMCTL32.@]
1842 * Returns the number of images in an image list.
1844 * PARAMS
1845 * himl [I] handle to image list
1847 * RETURNS
1848 * Success: Number of images.
1849 * Failure: 0
1852 INT WINAPI
1853 ImageList_GetImageCount (HIMAGELIST himl)
1855 if (!is_valid(himl))
1856 return 0;
1858 return himl->cCurImage;
1862 /*************************************************************************
1863 * ImageList_GetImageInfo [COMCTL32.@]
1865 * Returns information about an image in an image list.
1867 * PARAMS
1868 * himl [I] handle to image list
1869 * i [I] image index
1870 * pImageInfo [O] pointer to the image information
1872 * RETURNS
1873 * Success: TRUE
1874 * Failure: FALSE
1877 BOOL WINAPI
1878 ImageList_GetImageInfo (HIMAGELIST himl, INT i, IMAGEINFO *pImageInfo)
1880 POINT pt;
1882 if (!is_valid(himl) || (pImageInfo == NULL))
1883 return FALSE;
1884 if ((i < 0) || (i >= himl->cCurImage))
1885 return FALSE;
1887 pImageInfo->hbmImage = himl->hbmImage;
1888 pImageInfo->hbmMask = himl->hbmMask;
1890 imagelist_point_from_index( himl, i, &pt );
1891 pImageInfo->rcImage.top = pt.y;
1892 pImageInfo->rcImage.bottom = pt.y + himl->cy;
1893 pImageInfo->rcImage.left = pt.x;
1894 pImageInfo->rcImage.right = pt.x + himl->cx;
1896 return TRUE;
1900 /*************************************************************************
1901 * ImageList_GetImageRect [COMCTL32.@]
1903 * Retrieves the rectangle of the specified image in an image list.
1905 * PARAMS
1906 * himl [I] handle to image list
1907 * i [I] image index
1908 * lpRect [O] pointer to the image rectangle
1910 * RETURNS
1911 * Success: TRUE
1912 * Failure: FALSE
1914 * NOTES
1915 * This is an UNDOCUMENTED function!!!
1918 BOOL WINAPI
1919 ImageList_GetImageRect (HIMAGELIST himl, INT i, LPRECT lpRect)
1921 POINT pt;
1923 if (!is_valid(himl) || (lpRect == NULL))
1924 return FALSE;
1925 if ((i < 0) || (i >= himl->cCurImage))
1926 return FALSE;
1928 imagelist_point_from_index( himl, i, &pt );
1929 lpRect->left = pt.x;
1930 lpRect->top = pt.y;
1931 lpRect->right = pt.x + himl->cx;
1932 lpRect->bottom = pt.y + himl->cy;
1934 return TRUE;
1938 /*************************************************************************
1939 * ImageList_LoadImage [COMCTL32.@]
1940 * ImageList_LoadImageA [COMCTL32.@]
1942 * Creates an image list from a bitmap, icon or cursor.
1944 * See ImageList_LoadImageW.
1947 HIMAGELIST WINAPI
1948 ImageList_LoadImageA (HINSTANCE hi, LPCSTR lpbmp, INT cx, INT cGrow,
1949 COLORREF clrMask, UINT uType, UINT uFlags)
1951 HIMAGELIST himl;
1952 LPWSTR lpbmpW;
1953 DWORD len;
1955 if (IS_INTRESOURCE(lpbmp))
1956 return ImageList_LoadImageW(hi, (LPCWSTR)lpbmp, cx, cGrow, clrMask,
1957 uType, uFlags);
1959 len = MultiByteToWideChar(CP_ACP, 0, lpbmp, -1, NULL, 0);
1960 lpbmpW = Alloc(len * sizeof(WCHAR));
1961 MultiByteToWideChar(CP_ACP, 0, lpbmp, -1, lpbmpW, len);
1963 himl = ImageList_LoadImageW(hi, lpbmpW, cx, cGrow, clrMask, uType, uFlags);
1964 Free (lpbmpW);
1965 return himl;
1969 /*************************************************************************
1970 * ImageList_LoadImageW [COMCTL32.@]
1972 * Creates an image list from a bitmap, icon or cursor.
1974 * PARAMS
1975 * hi [I] instance handle
1976 * lpbmp [I] name or id of the image
1977 * cx [I] width of each image
1978 * cGrow [I] number of images to expand
1979 * clrMask [I] mask color
1980 * uType [I] type of image to load
1981 * uFlags [I] loading flags
1983 * RETURNS
1984 * Success: handle to the loaded image list
1985 * Failure: NULL
1987 * SEE
1988 * LoadImage ()
1991 HIMAGELIST WINAPI
1992 ImageList_LoadImageW (HINSTANCE hi, LPCWSTR lpbmp, INT cx, INT cGrow,
1993 COLORREF clrMask, UINT uType, UINT uFlags)
1995 HIMAGELIST himl = NULL;
1996 HANDLE handle;
1997 INT nImageCount;
1999 handle = LoadImageW (hi, lpbmp, uType, 0, 0, uFlags);
2000 if (!handle) {
2001 WARN("Couldn't load image\n");
2002 return NULL;
2005 if (uType == IMAGE_BITMAP) {
2006 DIBSECTION dib;
2007 UINT color;
2009 if (GetObjectW (handle, sizeof(dib), &dib) == sizeof(BITMAP)) color = ILC_COLOR;
2010 else color = dib.dsBm.bmBitsPixel;
2012 /* To match windows behavior, if cx is set to zero and
2013 the flag DI_DEFAULTSIZE is specified, cx becomes the
2014 system metric value for icons. If the flag is not specified
2015 the function sets the size to the height of the bitmap */
2016 if (cx == 0)
2018 if (uFlags & DI_DEFAULTSIZE)
2019 cx = GetSystemMetrics (SM_CXICON);
2020 else
2021 cx = dib.dsBm.bmHeight;
2024 nImageCount = dib.dsBm.bmWidth / cx;
2026 himl = ImageList_Create (cx, dib.dsBm.bmHeight, ILC_MASK | color, nImageCount, cGrow);
2027 if (!himl) {
2028 DeleteObject (handle);
2029 return NULL;
2031 ImageList_AddMasked (himl, handle, clrMask);
2033 else if ((uType == IMAGE_ICON) || (uType == IMAGE_CURSOR)) {
2034 ICONINFO ii;
2035 BITMAP bmp;
2037 GetIconInfo (handle, &ii);
2038 GetObjectW (ii.hbmColor, sizeof(BITMAP), &bmp);
2039 himl = ImageList_Create (bmp.bmWidth, bmp.bmHeight,
2040 ILC_MASK | ILC_COLOR, 1, cGrow);
2041 if (!himl) {
2042 DeleteObject (ii.hbmColor);
2043 DeleteObject (ii.hbmMask);
2044 DeleteObject (handle);
2045 return NULL;
2047 ImageList_Add (himl, ii.hbmColor, ii.hbmMask);
2048 DeleteObject (ii.hbmColor);
2049 DeleteObject (ii.hbmMask);
2052 DeleteObject (handle);
2054 return himl;
2058 /*************************************************************************
2059 * ImageList_Merge [COMCTL32.@]
2061 * Create an image list containing a merged image from two image lists.
2063 * PARAMS
2064 * himl1 [I] handle to first image list
2065 * i1 [I] first image index
2066 * himl2 [I] handle to second image list
2067 * i2 [I] second image index
2068 * dx [I] X offset of the second image relative to the first.
2069 * dy [I] Y offset of the second image relative to the first.
2071 * RETURNS
2072 * Success: The newly created image list. It contains a single image
2073 * consisting of the second image merged with the first.
2074 * Failure: NULL, if either himl1 or himl2 is invalid.
2076 * NOTES
2077 * - The returned image list should be deleted by the caller using
2078 * ImageList_Destroy() when it is no longer required.
2079 * - If either i1 or i2 is not a valid image index, they will be treated
2080 * as blank images.
2082 HIMAGELIST WINAPI
2083 ImageList_Merge (HIMAGELIST himl1, INT i1, HIMAGELIST himl2, INT i2,
2084 INT dx, INT dy)
2086 HIMAGELIST himlDst = NULL;
2087 INT cxDst, cyDst;
2088 INT xOff1, yOff1, xOff2, yOff2;
2089 POINT pt1, pt2;
2090 INT newFlags;
2092 TRACE("(himl1=%p i1=%d himl2=%p i2=%d dx=%d dy=%d)\n", himl1, i1, himl2,
2093 i2, dx, dy);
2095 if (!is_valid(himl1) || !is_valid(himl2))
2096 return NULL;
2098 if (dx > 0) {
2099 cxDst = max (himl1->cx, dx + himl2->cx);
2100 xOff1 = 0;
2101 xOff2 = dx;
2103 else if (dx < 0) {
2104 cxDst = max (himl2->cx, himl1->cx - dx);
2105 xOff1 = -dx;
2106 xOff2 = 0;
2108 else {
2109 cxDst = max (himl1->cx, himl2->cx);
2110 xOff1 = 0;
2111 xOff2 = 0;
2114 if (dy > 0) {
2115 cyDst = max (himl1->cy, dy + himl2->cy);
2116 yOff1 = 0;
2117 yOff2 = dy;
2119 else if (dy < 0) {
2120 cyDst = max (himl2->cy, himl1->cy - dy);
2121 yOff1 = -dy;
2122 yOff2 = 0;
2124 else {
2125 cyDst = max (himl1->cy, himl2->cy);
2126 yOff1 = 0;
2127 yOff2 = 0;
2130 newFlags = (himl1->flags > himl2->flags ? himl1->flags : himl2->flags) & ILC_COLORDDB;
2131 if (newFlags == ILC_COLORDDB && (himl1->flags & ILC_COLORDDB) == ILC_COLOR16)
2132 newFlags = ILC_COLOR16; /* this is what native (at least v5) does, don't know why */
2133 himlDst = ImageList_Create (cxDst, cyDst, ILC_MASK | newFlags, 1, 1);
2135 if (himlDst)
2137 imagelist_point_from_index( himl1, i1, &pt1 );
2138 imagelist_point_from_index( himl2, i2, &pt2 );
2140 /* copy image */
2141 BitBlt (himlDst->hdcImage, 0, 0, cxDst, cyDst, himl1->hdcImage, 0, 0, BLACKNESS);
2142 if (i1 >= 0 && i1 < himl1->cCurImage)
2143 BitBlt (himlDst->hdcImage, xOff1, yOff1, himl1->cx, himl1->cy, himl1->hdcImage, pt1.x, pt1.y, SRCCOPY);
2144 if (i2 >= 0 && i2 < himl2->cCurImage)
2146 if (himl2->flags & ILC_MASK)
2148 BitBlt (himlDst->hdcImage, xOff2, yOff2, himl2->cx, himl2->cy, himl2->hdcMask , pt2.x, pt2.y, SRCAND);
2149 BitBlt (himlDst->hdcImage, xOff2, yOff2, himl2->cx, himl2->cy, himl2->hdcImage, pt2.x, pt2.y, SRCPAINT);
2151 else
2152 BitBlt (himlDst->hdcImage, xOff2, yOff2, himl2->cx, himl2->cy, himl2->hdcImage, pt2.x, pt2.y, SRCCOPY);
2155 /* copy mask */
2156 BitBlt (himlDst->hdcMask, 0, 0, cxDst, cyDst, himl1->hdcMask, 0, 0, WHITENESS);
2157 if (i1 >= 0 && i1 < himl1->cCurImage)
2158 BitBlt (himlDst->hdcMask, xOff1, yOff1, himl1->cx, himl1->cy, himl1->hdcMask, pt1.x, pt1.y, SRCCOPY);
2159 if (i2 >= 0 && i2 < himl2->cCurImage)
2160 BitBlt (himlDst->hdcMask, xOff2, yOff2, himl2->cx, himl2->cy, himl2->hdcMask, pt2.x, pt2.y, SRCAND);
2162 himlDst->cCurImage = 1;
2165 return himlDst;
2169 /* helper for ImageList_Read, see comments below */
2170 static void *read_bitmap(IStream *pstm, BITMAPINFO *bmi)
2172 BITMAPFILEHEADER bmfh;
2173 int bitsperpixel, palspace;
2174 void *bits;
2176 if (FAILED(IStream_Read ( pstm, &bmfh, sizeof(bmfh), NULL)))
2177 return NULL;
2179 if (bmfh.bfType != (('M'<<8)|'B'))
2180 return NULL;
2182 if (FAILED(IStream_Read ( pstm, &bmi->bmiHeader, sizeof(bmi->bmiHeader), NULL)))
2183 return NULL;
2185 if ((bmi->bmiHeader.biSize != sizeof(bmi->bmiHeader)))
2186 return NULL;
2188 TRACE("width %u, height %u, planes %u, bpp %u\n",
2189 bmi->bmiHeader.biWidth, bmi->bmiHeader.biHeight,
2190 bmi->bmiHeader.biPlanes, bmi->bmiHeader.biBitCount);
2192 bitsperpixel = bmi->bmiHeader.biPlanes * bmi->bmiHeader.biBitCount;
2193 if (bitsperpixel<=8)
2194 palspace = (1<<bitsperpixel)*sizeof(RGBQUAD);
2195 else
2196 palspace = 0;
2198 bmi->bmiHeader.biSizeImage = get_dib_image_size( bmi );
2200 /* read the palette right after the end of the bitmapinfoheader */
2201 if (palspace && FAILED(IStream_Read(pstm, bmi->bmiColors, palspace, NULL)))
2202 return NULL;
2204 bits = Alloc(bmi->bmiHeader.biSizeImage);
2205 if (!bits) return NULL;
2207 if (FAILED(IStream_Read(pstm, bits, bmi->bmiHeader.biSizeImage, NULL)))
2209 Free(bits);
2210 return NULL;
2212 return bits;
2215 /*************************************************************************
2216 * ImageList_Read [COMCTL32.@]
2218 * Reads an image list from a stream.
2220 * PARAMS
2221 * pstm [I] pointer to a stream
2223 * RETURNS
2224 * Success: handle to image list
2225 * Failure: NULL
2227 * The format is like this:
2228 * ILHEAD ilheadstruct;
2230 * for the color image part:
2231 * BITMAPFILEHEADER bmfh;
2232 * BITMAPINFOHEADER bmih;
2233 * only if it has a palette:
2234 * RGBQUAD rgbs[nr_of_paletted_colors];
2236 * BYTE colorbits[imagesize];
2238 * the following only if the ILC_MASK bit is set in ILHEAD.ilFlags:
2239 * BITMAPFILEHEADER bmfh_mask;
2240 * BITMAPINFOHEADER bmih_mask;
2241 * only if it has a palette (it usually does not):
2242 * RGBQUAD rgbs[nr_of_paletted_colors];
2244 * BYTE maskbits[imagesize];
2246 HIMAGELIST WINAPI ImageList_Read(IStream *pstm)
2248 char image_buf[sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * 256];
2249 char mask_buf[sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * 256];
2250 BITMAPINFO *image_info = (BITMAPINFO *)image_buf;
2251 BITMAPINFO *mask_info = (BITMAPINFO *)mask_buf;
2252 void *image_bits, *mask_bits = NULL;
2253 ILHEAD ilHead;
2254 HIMAGELIST himl;
2255 unsigned int i;
2257 TRACE("%p\n", pstm);
2259 if (FAILED(IStream_Read (pstm, &ilHead, sizeof(ILHEAD), NULL)))
2260 return NULL;
2261 if (ilHead.usMagic != (('L' << 8) | 'I'))
2262 return NULL;
2263 if (ilHead.usVersion != 0x101) /* probably version? */
2264 return NULL;
2266 TRACE("cx %u, cy %u, flags 0x%04x, cCurImage %u, cMaxImage %u\n",
2267 ilHead.cx, ilHead.cy, ilHead.flags, ilHead.cCurImage, ilHead.cMaxImage);
2269 himl = ImageList_Create(ilHead.cx, ilHead.cy, ilHead.flags, ilHead.cCurImage, ilHead.cMaxImage);
2270 if (!himl)
2271 return NULL;
2273 if (!(image_bits = read_bitmap(pstm, image_info)))
2275 WARN("failed to read bitmap from stream\n");
2276 return NULL;
2278 if (ilHead.flags & ILC_MASK)
2280 if (!(mask_bits = read_bitmap(pstm, mask_info)))
2282 WARN("failed to read mask bitmap from stream\n");
2283 return NULL;
2286 else mask_info = NULL;
2288 if (himl->has_alpha && image_info->bmiHeader.biBitCount == 32)
2290 DWORD *ptr = image_bits;
2291 BYTE *mask_ptr = mask_bits;
2292 int stride = himl->cy * image_info->bmiHeader.biWidth;
2294 if (image_info->bmiHeader.biHeight > 0) /* bottom-up */
2296 ptr += image_info->bmiHeader.biHeight * image_info->bmiHeader.biWidth - stride;
2297 mask_ptr += (image_info->bmiHeader.biHeight * image_info->bmiHeader.biWidth - stride) / 8;
2298 stride = -stride;
2299 image_info->bmiHeader.biHeight = himl->cy;
2301 else image_info->bmiHeader.biHeight = -himl->cy;
2303 for (i = 0; i < ilHead.cCurImage; i += TILE_COUNT)
2305 add_dib_bits( himl, i, min( ilHead.cCurImage - i, TILE_COUNT ),
2306 himl->cx, himl->cy, image_info, mask_info, ptr, mask_ptr );
2307 ptr += stride;
2308 mask_ptr += stride / 8;
2311 else
2313 StretchDIBits( himl->hdcImage, 0, 0, image_info->bmiHeader.biWidth, image_info->bmiHeader.biHeight,
2314 0, 0, image_info->bmiHeader.biWidth, image_info->bmiHeader.biHeight,
2315 image_bits, image_info, DIB_RGB_COLORS, SRCCOPY);
2316 if (mask_info)
2317 StretchDIBits( himl->hdcMask, 0, 0, mask_info->bmiHeader.biWidth, mask_info->bmiHeader.biHeight,
2318 0, 0, mask_info->bmiHeader.biWidth, mask_info->bmiHeader.biHeight,
2319 mask_bits, mask_info, DIB_RGB_COLORS, SRCCOPY);
2321 Free( image_bits );
2322 Free( mask_bits );
2324 himl->cCurImage = ilHead.cCurImage;
2325 himl->cMaxImage = ilHead.cMaxImage;
2327 ImageList_SetBkColor(himl,ilHead.bkcolor);
2328 for (i=0;i<4;i++)
2329 ImageList_SetOverlayImage(himl,ilHead.ovls[i],i+1);
2330 return himl;
2334 /*************************************************************************
2335 * ImageList_Remove [COMCTL32.@]
2337 * Removes an image from an image list
2339 * PARAMS
2340 * himl [I] image list handle
2341 * i [I] image index
2343 * RETURNS
2344 * Success: TRUE
2345 * Failure: FALSE
2347 * FIXME: as the image list storage test shows, native comctl32 simply shifts
2348 * images without creating a new bitmap.
2350 BOOL WINAPI
2351 ImageList_Remove (HIMAGELIST himl, INT i)
2353 HBITMAP hbmNewImage, hbmNewMask;
2354 HDC hdcBmp;
2355 SIZE sz;
2357 TRACE("(himl=%p i=%d)\n", himl, i);
2359 if (!is_valid(himl)) {
2360 ERR("Invalid image list handle!\n");
2361 return FALSE;
2364 if ((i < -1) || (i >= himl->cCurImage)) {
2365 TRACE("index out of range! %d\n", i);
2366 return FALSE;
2369 if (i == -1) {
2370 INT nCount;
2372 /* remove all */
2373 if (himl->cCurImage == 0) {
2374 /* remove all on empty ImageList is allowed */
2375 TRACE("remove all on empty ImageList!\n");
2376 return TRUE;
2379 himl->cMaxImage = himl->cGrow;
2380 himl->cCurImage = 0;
2381 for (nCount = 0; nCount < MAX_OVERLAYIMAGE; nCount++)
2382 himl->nOvlIdx[nCount] = -1;
2384 if (himl->has_alpha)
2386 HeapFree( GetProcessHeap(), 0, himl->has_alpha );
2387 himl->has_alpha = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, himl->cMaxImage );
2390 hbmNewImage = ImageList_CreateImage(himl->hdcImage, himl, himl->cMaxImage);
2391 SelectObject (himl->hdcImage, hbmNewImage);
2392 DeleteObject (himl->hbmImage);
2393 himl->hbmImage = hbmNewImage;
2395 if (himl->hbmMask) {
2397 imagelist_get_bitmap_size(himl, himl->cMaxImage, &sz);
2398 hbmNewMask = CreateBitmap (sz.cx, sz.cy, 1, 1, NULL);
2399 SelectObject (himl->hdcMask, hbmNewMask);
2400 DeleteObject (himl->hbmMask);
2401 himl->hbmMask = hbmNewMask;
2404 else {
2405 /* delete one image */
2406 TRACE("Remove single image! %d\n", i);
2408 /* create new bitmap(s) */
2409 TRACE(" - Number of images: %d / %d (Old/New)\n",
2410 himl->cCurImage, himl->cCurImage - 1);
2412 hbmNewImage = ImageList_CreateImage(himl->hdcImage, himl, himl->cMaxImage);
2414 imagelist_get_bitmap_size(himl, himl->cMaxImage, &sz );
2415 if (himl->hbmMask)
2416 hbmNewMask = CreateBitmap (sz.cx, sz.cy, 1, 1, NULL);
2417 else
2418 hbmNewMask = 0; /* Just to keep compiler happy! */
2420 hdcBmp = CreateCompatibleDC (0);
2422 /* copy all images and masks prior to the "removed" image */
2423 if (i > 0) {
2424 TRACE("Pre image copy: Copy %d images\n", i);
2426 SelectObject (hdcBmp, hbmNewImage);
2427 imagelist_copy_images( himl, himl->hdcImage, hdcBmp, 0, i, 0 );
2429 if (himl->hbmMask) {
2430 SelectObject (hdcBmp, hbmNewMask);
2431 imagelist_copy_images( himl, himl->hdcMask, hdcBmp, 0, i, 0 );
2435 /* copy all images and masks behind the removed image */
2436 if (i < himl->cCurImage - 1) {
2437 TRACE("Post image copy!\n");
2439 SelectObject (hdcBmp, hbmNewImage);
2440 imagelist_copy_images( himl, himl->hdcImage, hdcBmp, i + 1,
2441 (himl->cCurImage - i), i );
2443 if (himl->hbmMask) {
2444 SelectObject (hdcBmp, hbmNewMask);
2445 imagelist_copy_images( himl, himl->hdcMask, hdcBmp, i + 1,
2446 (himl->cCurImage - i), i );
2450 DeleteDC (hdcBmp);
2452 /* delete old images and insert new ones */
2453 SelectObject (himl->hdcImage, hbmNewImage);
2454 DeleteObject (himl->hbmImage);
2455 himl->hbmImage = hbmNewImage;
2456 if (himl->hbmMask) {
2457 SelectObject (himl->hdcMask, hbmNewMask);
2458 DeleteObject (himl->hbmMask);
2459 himl->hbmMask = hbmNewMask;
2462 himl->cCurImage--;
2465 return TRUE;
2469 /*************************************************************************
2470 * ImageList_Replace [COMCTL32.@]
2472 * Replaces an image in an image list with a new image.
2474 * PARAMS
2475 * himl [I] handle to image list
2476 * i [I] image index
2477 * hbmImage [I] handle to image bitmap
2478 * hbmMask [I] handle to mask bitmap. Can be NULL.
2480 * RETURNS
2481 * Success: TRUE
2482 * Failure: FALSE
2485 BOOL WINAPI
2486 ImageList_Replace (HIMAGELIST himl, INT i, HBITMAP hbmImage,
2487 HBITMAP hbmMask)
2489 HDC hdcImage;
2490 BITMAP bmp;
2491 POINT pt;
2493 TRACE("%p %d %p %p\n", himl, i, hbmImage, hbmMask);
2495 if (!is_valid(himl)) {
2496 ERR("Invalid image list handle!\n");
2497 return FALSE;
2500 if ((i >= himl->cMaxImage) || (i < 0)) {
2501 ERR("Invalid image index!\n");
2502 return FALSE;
2505 if (!GetObjectW(hbmImage, sizeof(BITMAP), &bmp))
2506 return FALSE;
2508 hdcImage = CreateCompatibleDC (0);
2510 /* Replace Image */
2511 SelectObject (hdcImage, hbmImage);
2513 if (add_with_alpha( himl, hdcImage, i, 1, bmp.bmWidth, bmp.bmHeight, hbmImage, hbmMask ))
2514 goto done;
2516 imagelist_point_from_index(himl, i, &pt);
2517 StretchBlt (himl->hdcImage, pt.x, pt.y, himl->cx, himl->cy,
2518 hdcImage, 0, 0, bmp.bmWidth, bmp.bmHeight, SRCCOPY);
2520 if (himl->hbmMask)
2522 HDC hdcTemp;
2523 HBITMAP hOldBitmapTemp;
2525 hdcTemp = CreateCompatibleDC(0);
2526 hOldBitmapTemp = SelectObject(hdcTemp, hbmMask);
2528 StretchBlt (himl->hdcMask, pt.x, pt.y, himl->cx, himl->cy,
2529 hdcTemp, 0, 0, bmp.bmWidth, bmp.bmHeight, SRCCOPY);
2530 SelectObject(hdcTemp, hOldBitmapTemp);
2531 DeleteDC(hdcTemp);
2533 /* Remove the background from the image
2535 BitBlt (himl->hdcImage, pt.x, pt.y, bmp.bmWidth, bmp.bmHeight,
2536 himl->hdcMask, pt.x, pt.y, 0x220326); /* NOTSRCAND */
2539 done:
2540 DeleteDC (hdcImage);
2542 return TRUE;
2546 /*************************************************************************
2547 * ImageList_ReplaceIcon [COMCTL32.@]
2549 * Replaces an image in an image list using an icon.
2551 * PARAMS
2552 * himl [I] handle to image list
2553 * i [I] image index
2554 * hIcon [I] handle to icon
2556 * RETURNS
2557 * Success: index of the replaced image
2558 * Failure: -1
2561 INT WINAPI
2562 ImageList_ReplaceIcon (HIMAGELIST himl, INT nIndex, HICON hIcon)
2564 HICON hBestFitIcon;
2565 ICONINFO ii;
2566 BITMAP bmp;
2567 BOOL ret;
2568 POINT pt;
2570 TRACE("(%p %d %p)\n", himl, nIndex, hIcon);
2572 if (!is_valid(himl)) {
2573 ERR("invalid image list\n");
2574 return -1;
2576 if ((nIndex >= himl->cMaxImage) || (nIndex < -1)) {
2577 ERR("invalid image index %d / %d\n", nIndex, himl->cMaxImage);
2578 return -1;
2581 hBestFitIcon = CopyImage(
2582 hIcon, IMAGE_ICON,
2583 himl->cx, himl->cy,
2584 LR_COPYFROMRESOURCE);
2585 /* the above will fail if the icon wasn't loaded from a resource, so try
2586 * again without LR_COPYFROMRESOURCE flag */
2587 if (!hBestFitIcon)
2588 hBestFitIcon = CopyImage(
2589 hIcon, IMAGE_ICON,
2590 himl->cx, himl->cy,
2592 if (!hBestFitIcon)
2593 return -1;
2595 if (nIndex == -1) {
2596 if (himl->cCurImage + 1 >= himl->cMaxImage)
2597 IMAGELIST_InternalExpandBitmaps(himl, 1);
2599 nIndex = himl->cCurImage;
2600 himl->cCurImage++;
2603 if (himl->has_alpha && GetIconInfo (hBestFitIcon, &ii))
2605 HDC hdcImage = CreateCompatibleDC( 0 );
2606 GetObjectW (ii.hbmMask, sizeof(BITMAP), &bmp);
2608 if (!ii.hbmColor)
2610 UINT height = bmp.bmHeight / 2;
2611 HDC hdcMask = CreateCompatibleDC( 0 );
2612 HBITMAP color = CreateBitmap( bmp.bmWidth, height, 1, 1, NULL );
2613 SelectObject( hdcImage, color );
2614 SelectObject( hdcMask, ii.hbmMask );
2615 BitBlt( hdcImage, 0, 0, bmp.bmWidth, height, hdcMask, 0, height, SRCCOPY );
2616 ret = add_with_alpha( himl, hdcImage, nIndex, 1, bmp.bmWidth, height, color, ii.hbmMask );
2617 DeleteDC( hdcMask );
2618 DeleteObject( color );
2620 else ret = add_with_alpha( himl, hdcImage, nIndex, 1, bmp.bmWidth, bmp.bmHeight,
2621 ii.hbmColor, ii.hbmMask );
2623 DeleteDC( hdcImage );
2624 DeleteObject (ii.hbmMask);
2625 if (ii.hbmColor) DeleteObject (ii.hbmColor);
2626 if (ret) goto done;
2629 imagelist_point_from_index(himl, nIndex, &pt);
2631 if (himl->hbmMask)
2633 DrawIconEx( himl->hdcImage, pt.x, pt.y, hBestFitIcon, himl->cx, himl->cy, 0, 0, DI_IMAGE );
2634 PatBlt( himl->hdcMask, pt.x, pt.y, himl->cx, himl->cy, WHITENESS );
2635 DrawIconEx( himl->hdcMask, pt.x, pt.y, hBestFitIcon, himl->cx, himl->cy, 0, 0, DI_MASK );
2637 else
2639 COLORREF color = himl->clrBk != CLR_NONE ? himl->clrBk : comctl32_color.clrWindow;
2640 HBRUSH brush = CreateSolidBrush( GetNearestColor( himl->hdcImage, color ));
2642 SelectObject( himl->hdcImage, brush );
2643 PatBlt( himl->hdcImage, pt.x, pt.y, himl->cx, himl->cy, PATCOPY );
2644 SelectObject( himl->hdcImage, GetStockObject(BLACK_BRUSH) );
2645 DeleteObject( brush );
2646 DrawIconEx( himl->hdcImage, pt.x, pt.y, hBestFitIcon, himl->cx, himl->cy, 0, 0, DI_NORMAL );
2649 done:
2650 DestroyIcon(hBestFitIcon);
2652 TRACE("Insert index = %d, himl->cCurImage = %d\n", nIndex, himl->cCurImage);
2653 return nIndex;
2657 /*************************************************************************
2658 * ImageList_SetBkColor [COMCTL32.@]
2660 * Sets the background color of an image list.
2662 * PARAMS
2663 * himl [I] handle to image list
2664 * clrBk [I] background color
2666 * RETURNS
2667 * Success: previous background color
2668 * Failure: CLR_NONE
2671 COLORREF WINAPI
2672 ImageList_SetBkColor (HIMAGELIST himl, COLORREF clrBk)
2674 COLORREF clrOldBk;
2676 if (!is_valid(himl))
2677 return CLR_NONE;
2679 clrOldBk = himl->clrBk;
2680 himl->clrBk = clrBk;
2681 return clrOldBk;
2685 /*************************************************************************
2686 * ImageList_SetDragCursorImage [COMCTL32.@]
2688 * Combines the specified image with the current drag image
2690 * PARAMS
2691 * himlDrag [I] handle to drag image list
2692 * iDrag [I] drag image index
2693 * dxHotspot [I] X position of the hot spot
2694 * dyHotspot [I] Y position of the hot spot
2696 * RETURNS
2697 * Success: TRUE
2698 * Failure: FALSE
2700 * NOTES
2701 * - The names dxHotspot, dyHotspot are misleading because they have nothing
2702 * to do with a hotspot but are only the offset of the origin of the new
2703 * image relative to the origin of the old image.
2705 * - When this function is called and the drag image is visible, a
2706 * short flickering occurs but this matches the Win9x behavior. It is
2707 * possible to fix the flickering using code like in ImageList_DragMove.
2710 BOOL WINAPI
2711 ImageList_SetDragCursorImage (HIMAGELIST himlDrag, INT iDrag,
2712 INT dxHotspot, INT dyHotspot)
2714 HIMAGELIST himlTemp;
2715 BOOL visible;
2717 if (!is_valid(InternalDrag.himl) || !is_valid(himlDrag))
2718 return FALSE;
2720 TRACE(" dxH=%d dyH=%d nX=%d nY=%d\n",
2721 dxHotspot, dyHotspot, InternalDrag.dxHotspot, InternalDrag.dyHotspot);
2723 visible = InternalDrag.bShow;
2725 himlTemp = ImageList_Merge (InternalDrag.himlNoCursor, 0, himlDrag, iDrag,
2726 dxHotspot, dyHotspot);
2728 if (visible) {
2729 /* hide the drag image */
2730 ImageList_DragShowNolock(FALSE);
2732 if ((InternalDrag.himl->cx != himlTemp->cx) ||
2733 (InternalDrag.himl->cy != himlTemp->cy)) {
2734 /* the size of the drag image changed, invalidate the buffer */
2735 DeleteObject(InternalDrag.hbmBg);
2736 InternalDrag.hbmBg = 0;
2739 if (InternalDrag.himl != InternalDrag.himlNoCursor)
2740 ImageList_Destroy (InternalDrag.himl);
2741 InternalDrag.himl = himlTemp;
2743 if (visible) {
2744 /* show the drag image */
2745 ImageList_DragShowNolock(TRUE);
2748 return TRUE;
2752 /*************************************************************************
2753 * ImageList_SetFilter [COMCTL32.@]
2755 * Sets a filter (or does something completely different)!!???
2756 * It removes 12 Bytes from the stack (3 Parameters).
2758 * PARAMS
2759 * himl [I] SHOULD be a handle to image list
2760 * i [I] COULD be an index?
2761 * dwFilter [I] ???
2763 * RETURNS
2764 * Success: TRUE ???
2765 * Failure: FALSE ???
2767 * BUGS
2768 * This is an UNDOCUMENTED function!!!!
2769 * empty stub.
2772 BOOL WINAPI
2773 ImageList_SetFilter (HIMAGELIST himl, INT i, DWORD dwFilter)
2775 FIXME("(%p 0x%x 0x%x):empty stub!\n", himl, i, dwFilter);
2777 return FALSE;
2781 /*************************************************************************
2782 * ImageList_SetFlags [COMCTL32.@]
2784 * Sets the image list flags.
2786 * PARAMS
2787 * himl [I] Handle to image list
2788 * flags [I] Flags to set
2790 * RETURNS
2791 * Old flags?
2793 * BUGS
2794 * Stub.
2797 DWORD WINAPI
2798 ImageList_SetFlags(HIMAGELIST himl, DWORD flags)
2800 FIXME("(%p %08x):empty stub\n", himl, flags);
2801 return 0;
2805 /*************************************************************************
2806 * ImageList_SetIconSize [COMCTL32.@]
2808 * Sets the image size of the bitmap and deletes all images.
2810 * PARAMS
2811 * himl [I] handle to image list
2812 * cx [I] image width
2813 * cy [I] image height
2815 * RETURNS
2816 * Success: TRUE
2817 * Failure: FALSE
2820 BOOL WINAPI
2821 ImageList_SetIconSize (HIMAGELIST himl, INT cx, INT cy)
2823 INT nCount;
2824 HBITMAP hbmNew;
2826 if (!is_valid(himl))
2827 return FALSE;
2829 /* remove all images */
2830 himl->cMaxImage = himl->cInitial + 1;
2831 himl->cCurImage = 0;
2832 himl->cx = cx;
2833 himl->cy = cy;
2835 /* initialize overlay mask indices */
2836 for (nCount = 0; nCount < MAX_OVERLAYIMAGE; nCount++)
2837 himl->nOvlIdx[nCount] = -1;
2839 hbmNew = ImageList_CreateImage(himl->hdcImage, himl, himl->cMaxImage);
2840 SelectObject (himl->hdcImage, hbmNew);
2841 DeleteObject (himl->hbmImage);
2842 himl->hbmImage = hbmNew;
2844 if (himl->hbmMask) {
2845 SIZE sz;
2846 imagelist_get_bitmap_size(himl, himl->cMaxImage, &sz);
2847 hbmNew = CreateBitmap (sz.cx, sz.cy, 1, 1, NULL);
2848 SelectObject (himl->hdcMask, hbmNew);
2849 DeleteObject (himl->hbmMask);
2850 himl->hbmMask = hbmNew;
2853 return TRUE;
2857 /*************************************************************************
2858 * ImageList_SetImageCount [COMCTL32.@]
2860 * Resizes an image list to the specified number of images.
2862 * PARAMS
2863 * himl [I] handle to image list
2864 * iImageCount [I] number of images in the image list
2866 * RETURNS
2867 * Success: TRUE
2868 * Failure: FALSE
2871 BOOL WINAPI
2872 ImageList_SetImageCount (HIMAGELIST himl, UINT iImageCount)
2874 HDC hdcBitmap;
2875 HBITMAP hbmNewBitmap, hbmOld;
2876 INT nNewCount, nCopyCount;
2878 TRACE("%p %d\n",himl,iImageCount);
2880 if (!is_valid(himl))
2881 return FALSE;
2883 nNewCount = iImageCount + 1;
2884 nCopyCount = min(himl->cCurImage, iImageCount);
2886 hdcBitmap = CreateCompatibleDC (0);
2888 hbmNewBitmap = ImageList_CreateImage(hdcBitmap, himl, nNewCount);
2890 if (hbmNewBitmap != 0)
2892 hbmOld = SelectObject (hdcBitmap, hbmNewBitmap);
2893 imagelist_copy_images( himl, himl->hdcImage, hdcBitmap, 0, nCopyCount, 0 );
2894 SelectObject (hdcBitmap, hbmOld);
2896 /* FIXME: delete 'empty' image space? */
2898 SelectObject (himl->hdcImage, hbmNewBitmap);
2899 DeleteObject (himl->hbmImage);
2900 himl->hbmImage = hbmNewBitmap;
2902 else
2903 ERR("Could not create new image bitmap!\n");
2905 if (himl->hbmMask)
2907 SIZE sz;
2908 imagelist_get_bitmap_size( himl, nNewCount, &sz );
2909 hbmNewBitmap = CreateBitmap (sz.cx, sz.cy, 1, 1, NULL);
2910 if (hbmNewBitmap != 0)
2912 hbmOld = SelectObject (hdcBitmap, hbmNewBitmap);
2913 imagelist_copy_images( himl, himl->hdcMask, hdcBitmap, 0, nCopyCount, 0 );
2914 SelectObject (hdcBitmap, hbmOld);
2916 /* FIXME: delete 'empty' image space? */
2918 SelectObject (himl->hdcMask, hbmNewBitmap);
2919 DeleteObject (himl->hbmMask);
2920 himl->hbmMask = hbmNewBitmap;
2922 else
2923 ERR("Could not create new mask bitmap!\n");
2926 DeleteDC (hdcBitmap);
2928 if (himl->has_alpha)
2930 char *new_alpha = HeapReAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, himl->has_alpha, nNewCount );
2931 if (new_alpha) himl->has_alpha = new_alpha;
2932 else
2934 HeapFree( GetProcessHeap(), 0, himl->has_alpha );
2935 himl->has_alpha = NULL;
2939 /* Update max image count and current image count */
2940 himl->cMaxImage = nNewCount;
2941 himl->cCurImage = iImageCount;
2943 return TRUE;
2947 /*************************************************************************
2948 * ImageList_SetOverlayImage [COMCTL32.@]
2950 * Assigns an overlay mask index to an existing image in an image list.
2952 * PARAMS
2953 * himl [I] handle to image list
2954 * iImage [I] image index
2955 * iOverlay [I] overlay mask index
2957 * RETURNS
2958 * Success: TRUE
2959 * Failure: FALSE
2962 BOOL WINAPI
2963 ImageList_SetOverlayImage (HIMAGELIST himl, INT iImage, INT iOverlay)
2965 if (!is_valid(himl))
2966 return FALSE;
2967 if ((iOverlay < 1) || (iOverlay > MAX_OVERLAYIMAGE))
2968 return FALSE;
2969 if ((iImage!=-1) && ((iImage < 0) || (iImage > himl->cCurImage)))
2970 return FALSE;
2971 himl->nOvlIdx[iOverlay - 1] = iImage;
2972 return TRUE;
2977 /* helper for ImageList_Write - write bitmap to pstm
2978 * currently everything is written as 24 bit RGB, except masks
2980 static BOOL _write_bitmap(HBITMAP hBitmap, IStream *pstm)
2982 LPBITMAPFILEHEADER bmfh;
2983 LPBITMAPINFOHEADER bmih;
2984 LPBYTE data = NULL, lpBits;
2985 BITMAP bm;
2986 INT bitCount, sizeImage, offBits, totalSize;
2987 HDC xdc;
2988 BOOL result = FALSE;
2990 if (!GetObjectW(hBitmap, sizeof(BITMAP), &bm))
2991 return FALSE;
2993 bitCount = bm.bmBitsPixel;
2994 sizeImage = get_dib_stride(bm.bmWidth, bitCount) * bm.bmHeight;
2996 totalSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
2997 if(bitCount <= 8)
2998 totalSize += (1 << bitCount) * sizeof(RGBQUAD);
2999 offBits = totalSize;
3000 totalSize += sizeImage;
3002 data = Alloc(totalSize);
3003 bmfh = (LPBITMAPFILEHEADER)data;
3004 bmih = (LPBITMAPINFOHEADER)(data + sizeof(BITMAPFILEHEADER));
3005 lpBits = data + offBits;
3007 /* setup BITMAPFILEHEADER */
3008 bmfh->bfType = (('M' << 8) | 'B');
3009 bmfh->bfSize = offBits;
3010 bmfh->bfReserved1 = 0;
3011 bmfh->bfReserved2 = 0;
3012 bmfh->bfOffBits = offBits;
3014 /* setup BITMAPINFOHEADER */
3015 bmih->biSize = sizeof(BITMAPINFOHEADER);
3016 bmih->biWidth = bm.bmWidth;
3017 bmih->biHeight = bm.bmHeight;
3018 bmih->biPlanes = 1;
3019 bmih->biBitCount = bitCount;
3020 bmih->biCompression = BI_RGB;
3021 bmih->biSizeImage = sizeImage;
3022 bmih->biXPelsPerMeter = 0;
3023 bmih->biYPelsPerMeter = 0;
3024 bmih->biClrUsed = 0;
3025 bmih->biClrImportant = 0;
3027 xdc = GetDC(0);
3028 result = GetDIBits(xdc, hBitmap, 0, bm.bmHeight, lpBits, (BITMAPINFO *)bmih, DIB_RGB_COLORS) == bm.bmHeight;
3029 ReleaseDC(0, xdc);
3030 if (!result)
3031 goto failed;
3033 TRACE("width %u, height %u, planes %u, bpp %u\n",
3034 bmih->biWidth, bmih->biHeight,
3035 bmih->biPlanes, bmih->biBitCount);
3037 if(FAILED(IStream_Write(pstm, data, totalSize, NULL)))
3038 goto failed;
3040 result = TRUE;
3042 failed:
3043 Free(data);
3045 return result;
3049 /*************************************************************************
3050 * ImageList_Write [COMCTL32.@]
3052 * Writes an image list to a stream.
3054 * PARAMS
3055 * himl [I] handle to image list
3056 * pstm [O] Pointer to a stream.
3058 * RETURNS
3059 * Success: TRUE
3060 * Failure: FALSE
3062 * BUGS
3063 * probably.
3066 BOOL WINAPI ImageList_Write(HIMAGELIST himl, IStream *pstm)
3068 ILHEAD ilHead;
3069 int i;
3071 TRACE("%p %p\n", himl, pstm);
3073 if (!is_valid(himl))
3074 return FALSE;
3076 ilHead.usMagic = (('L' << 8) | 'I');
3077 ilHead.usVersion = 0x101;
3078 ilHead.cCurImage = himl->cCurImage;
3079 ilHead.cMaxImage = himl->cMaxImage;
3080 ilHead.cGrow = himl->cGrow;
3081 ilHead.cx = himl->cx;
3082 ilHead.cy = himl->cy;
3083 ilHead.bkcolor = himl->clrBk;
3084 ilHead.flags = himl->flags;
3085 for(i = 0; i < 4; i++) {
3086 ilHead.ovls[i] = himl->nOvlIdx[i];
3089 TRACE("cx %u, cy %u, flags 0x04%x, cCurImage %u, cMaxImage %u\n",
3090 ilHead.cx, ilHead.cy, ilHead.flags, ilHead.cCurImage, ilHead.cMaxImage);
3092 if(FAILED(IStream_Write(pstm, &ilHead, sizeof(ILHEAD), NULL)))
3093 return FALSE;
3095 /* write the bitmap */
3096 if(!_write_bitmap(himl->hbmImage, pstm))
3097 return FALSE;
3099 /* write the mask if we have one */
3100 if(himl->flags & ILC_MASK) {
3101 if(!_write_bitmap(himl->hbmMask, pstm))
3102 return FALSE;
3105 return TRUE;
3109 static HBITMAP ImageList_CreateImage(HDC hdc, HIMAGELIST himl, UINT count)
3111 HBITMAP hbmNewBitmap;
3112 UINT ilc = (himl->flags & 0xFE);
3113 SIZE sz;
3115 imagelist_get_bitmap_size( himl, count, &sz );
3117 if ((ilc >= ILC_COLOR4 && ilc <= ILC_COLOR32) || ilc == ILC_COLOR)
3119 char buffer[sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD)];
3120 BITMAPINFO *bmi = (BITMAPINFO *)buffer;
3122 TRACE("Creating DIBSection %d x %d, %d Bits per Pixel\n",
3123 sz.cx, sz.cy, himl->uBitsPixel);
3125 memset( buffer, 0, sizeof(buffer) );
3126 bmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
3127 bmi->bmiHeader.biWidth = sz.cx;
3128 bmi->bmiHeader.biHeight = sz.cy;
3129 bmi->bmiHeader.biPlanes = 1;
3130 bmi->bmiHeader.biBitCount = himl->uBitsPixel;
3131 bmi->bmiHeader.biCompression = BI_RGB;
3133 if (himl->uBitsPixel <= ILC_COLOR8)
3135 /* retrieve the default color map */
3136 HBITMAP tmp = CreateBitmap( 1, 1, 1, 1, NULL );
3137 GetDIBits( hdc, tmp, 0, 0, NULL, bmi, DIB_RGB_COLORS );
3138 DeleteObject( tmp );
3140 hbmNewBitmap = CreateDIBSection(hdc, bmi, DIB_RGB_COLORS, NULL, 0, 0);
3142 else /*if (ilc == ILC_COLORDDB)*/
3144 TRACE("Creating Bitmap: %d Bits per Pixel\n", himl->uBitsPixel);
3146 hbmNewBitmap = CreateBitmap (sz.cx, sz.cy, 1, himl->uBitsPixel, NULL);
3148 TRACE("returning %p\n", hbmNewBitmap);
3149 return hbmNewBitmap;
3152 /*************************************************************************
3153 * ImageList_SetColorTable [COMCTL32.@]
3155 * Sets the color table of an image list.
3157 * PARAMS
3158 * himl [I] Handle to the image list.
3159 * uStartIndex [I] The first index to set.
3160 * cEntries [I] Number of entries to set.
3161 * prgb [I] New color information for color table for the image list.
3163 * RETURNS
3164 * Success: Number of entries in the table that were set.
3165 * Failure: Zero.
3167 * SEE
3168 * ImageList_Create(), SetDIBColorTable()
3171 UINT WINAPI
3172 ImageList_SetColorTable(HIMAGELIST himl, UINT uStartIndex, UINT cEntries, const RGBQUAD *prgb)
3174 return SetDIBColorTable(himl->hdcImage, uStartIndex, cEntries, prgb);
3177 /*************************************************************************
3178 * ImageList_CoCreateInstance [COMCTL32.@]
3180 * Creates a new imagelist instance and returns an interface pointer to it.
3182 * PARAMS
3183 * rclsid [I] A reference to the CLSID (CLSID_ImageList).
3184 * punkOuter [I] Pointer to IUnknown interface for aggregation, if desired
3185 * riid [I] Identifier of the requested interface.
3186 * ppv [O] Returns the address of the pointer requested, or NULL.
3188 * RETURNS
3189 * Success: S_OK.
3190 * Failure: Error value.
3192 HRESULT WINAPI
3193 ImageList_CoCreateInstance (REFCLSID rclsid, const IUnknown *punkOuter, REFIID riid, void **ppv)
3195 TRACE("(%s,%p,%s,%p)\n", debugstr_guid(rclsid), punkOuter, debugstr_guid(riid), ppv);
3197 if (!IsEqualCLSID(&CLSID_ImageList, rclsid))
3198 return E_NOINTERFACE;
3200 return ImageListImpl_CreateInstance(punkOuter, riid, ppv);
3204 /*************************************************************************
3205 * IImageList implementation
3208 static HRESULT WINAPI ImageListImpl_QueryInterface(IImageList2 *iface,
3209 REFIID iid, void **ppv)
3211 HIMAGELIST imgl = impl_from_IImageList2(iface);
3213 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
3215 if (!ppv) return E_INVALIDARG;
3217 if (IsEqualIID(&IID_IUnknown, iid) ||
3218 IsEqualIID(&IID_IImageList, iid) ||
3219 IsEqualIID(&IID_IImageList2, iid))
3221 *ppv = &imgl->IImageList2_iface;
3223 else
3225 *ppv = NULL;
3226 return E_NOINTERFACE;
3229 IImageList2_AddRef(iface);
3230 return S_OK;
3233 static ULONG WINAPI ImageListImpl_AddRef(IImageList2 *iface)
3235 HIMAGELIST imgl = impl_from_IImageList2(iface);
3236 ULONG ref = InterlockedIncrement(&imgl->ref);
3238 TRACE("(%p) refcount=%u\n", iface, ref);
3239 return ref;
3242 static ULONG WINAPI ImageListImpl_Release(IImageList2 *iface)
3244 HIMAGELIST This = impl_from_IImageList2(iface);
3245 ULONG ref = InterlockedDecrement(&This->ref);
3247 TRACE("(%p) refcount=%u\n", iface, ref);
3249 if (ref == 0)
3251 /* delete image bitmaps */
3252 if (This->hbmImage) DeleteObject (This->hbmImage);
3253 if (This->hbmMask) DeleteObject (This->hbmMask);
3255 /* delete image & mask DCs */
3256 if (This->hdcImage) DeleteDC (This->hdcImage);
3257 if (This->hdcMask) DeleteDC (This->hdcMask);
3259 /* delete blending brushes */
3260 if (This->hbrBlend25) DeleteObject (This->hbrBlend25);
3261 if (This->hbrBlend50) DeleteObject (This->hbrBlend50);
3263 This->IImageList2_iface.lpVtbl = NULL;
3264 HeapFree(GetProcessHeap(), 0, This->has_alpha);
3265 HeapFree(GetProcessHeap(), 0, This);
3268 return ref;
3271 static HRESULT WINAPI ImageListImpl_Add(IImageList2 *iface, HBITMAP hbmImage,
3272 HBITMAP hbmMask, int *pi)
3274 HIMAGELIST imgl = impl_from_IImageList2(iface);
3275 int ret;
3277 if (!pi)
3278 return E_FAIL;
3280 ret = ImageList_Add(imgl, hbmImage, hbmMask);
3282 if (ret == -1)
3283 return E_FAIL;
3285 *pi = ret;
3286 return S_OK;
3289 static HRESULT WINAPI ImageListImpl_ReplaceIcon(IImageList2 *iface, int i,
3290 HICON hicon, int *pi)
3292 HIMAGELIST imgl = impl_from_IImageList2(iface);
3293 int ret;
3295 if (!pi)
3296 return E_FAIL;
3298 ret = ImageList_ReplaceIcon(imgl, i, hicon);
3300 if (ret == -1)
3301 return E_FAIL;
3303 *pi = ret;
3304 return S_OK;
3307 static HRESULT WINAPI ImageListImpl_SetOverlayImage(IImageList2 *iface,
3308 int iImage, int iOverlay)
3310 HIMAGELIST imgl = impl_from_IImageList2(iface);
3311 return ImageList_SetOverlayImage(imgl, iImage, iOverlay) ? S_OK : E_FAIL;
3314 static HRESULT WINAPI ImageListImpl_Replace(IImageList2 *iface, int i,
3315 HBITMAP hbmImage, HBITMAP hbmMask)
3317 HIMAGELIST imgl = impl_from_IImageList2(iface);
3318 return ImageList_Replace(imgl, i, hbmImage, hbmMask) ? S_OK : E_FAIL;
3321 static HRESULT WINAPI ImageListImpl_AddMasked(IImageList2 *iface, HBITMAP hbmImage,
3322 COLORREF crMask, int *pi)
3324 HIMAGELIST imgl = impl_from_IImageList2(iface);
3325 int ret;
3327 if (!pi)
3328 return E_FAIL;
3330 ret = ImageList_AddMasked(imgl, hbmImage, crMask);
3332 if (ret == -1)
3333 return E_FAIL;
3335 *pi = ret;
3336 return S_OK;
3339 static HRESULT WINAPI ImageListImpl_Draw(IImageList2 *iface,
3340 IMAGELISTDRAWPARAMS *pimldp)
3342 HIMAGELIST imgl = impl_from_IImageList2(iface);
3343 HIMAGELIST old_himl;
3344 int ret;
3346 /* As far as I can tell, Windows simply ignores the contents of pimldp->himl
3347 so we shall simulate the same */
3348 old_himl = pimldp->himl;
3349 pimldp->himl = imgl;
3351 ret = ImageList_DrawIndirect(pimldp);
3353 pimldp->himl = old_himl;
3354 return ret ? S_OK : E_INVALIDARG;
3357 static HRESULT WINAPI ImageListImpl_Remove(IImageList2 *iface, int i)
3359 HIMAGELIST imgl = impl_from_IImageList2(iface);
3360 return (ImageList_Remove(imgl, i) == 0) ? E_INVALIDARG : S_OK;
3363 static HRESULT WINAPI ImageListImpl_GetIcon(IImageList2 *iface, int i, UINT flags,
3364 HICON *picon)
3366 HIMAGELIST imgl = impl_from_IImageList2(iface);
3367 HICON hIcon;
3369 if (!picon)
3370 return E_FAIL;
3372 hIcon = ImageList_GetIcon(imgl, i, flags);
3374 if (hIcon == NULL)
3375 return E_FAIL;
3377 *picon = hIcon;
3378 return S_OK;
3381 static HRESULT WINAPI ImageListImpl_GetImageInfo(IImageList2 *iface, int i,
3382 IMAGEINFO *pImageInfo)
3384 HIMAGELIST imgl = impl_from_IImageList2(iface);
3385 return ImageList_GetImageInfo(imgl, i, pImageInfo) ? S_OK : E_FAIL;
3388 static HRESULT WINAPI ImageListImpl_Copy(IImageList2 *iface, int dst_index,
3389 IUnknown *unk_src, int src_index, UINT flags)
3391 HIMAGELIST imgl = impl_from_IImageList2(iface);
3392 IImageList *src = NULL;
3393 HRESULT ret;
3395 if (!unk_src)
3396 return E_FAIL;
3398 /* TODO: Add test for IID_ImageList2 too */
3399 if (FAILED(IUnknown_QueryInterface(unk_src, &IID_IImageList,
3400 (void **) &src)))
3401 return E_FAIL;
3403 if (ImageList_Copy(imgl, dst_index, (HIMAGELIST) src, src_index, flags))
3404 ret = S_OK;
3405 else
3406 ret = E_FAIL;
3408 IImageList_Release(src);
3409 return ret;
3412 static HRESULT WINAPI ImageListImpl_Merge(IImageList2 *iface, int i1,
3413 IUnknown *punk2, int i2, int dx, int dy, REFIID riid, void **ppv)
3415 HIMAGELIST imgl = impl_from_IImageList2(iface);
3416 IImageList *iml2 = NULL;
3417 HIMAGELIST merged;
3418 HRESULT ret = E_FAIL;
3420 TRACE("(%p)->(%d %p %d %d %d %s %p)\n", iface, i1, punk2, i2, dx, dy, debugstr_guid(riid), ppv);
3422 /* TODO: Add test for IID_ImageList2 too */
3423 if (FAILED(IUnknown_QueryInterface(punk2, &IID_IImageList,
3424 (void **) &iml2)))
3425 return E_FAIL;
3427 merged = ImageList_Merge(imgl, i1, (HIMAGELIST) iml2, i2, dx, dy);
3429 /* Get the interface for the new image list */
3430 if (merged)
3432 ret = HIMAGELIST_QueryInterface(merged, riid, ppv);
3433 ImageList_Destroy(merged);
3436 IImageList_Release(iml2);
3437 return ret;
3440 static HRESULT WINAPI ImageListImpl_Clone(IImageList2 *iface, REFIID riid, void **ppv)
3442 HIMAGELIST imgl = impl_from_IImageList2(iface);
3443 HIMAGELIST clone;
3444 HRESULT ret = E_FAIL;
3446 TRACE("(%p)->(%s %p)\n", iface, debugstr_guid(riid), ppv);
3448 clone = ImageList_Duplicate(imgl);
3450 /* Get the interface for the new image list */
3451 if (clone)
3453 ret = HIMAGELIST_QueryInterface(clone, riid, ppv);
3454 ImageList_Destroy(clone);
3457 return ret;
3460 static HRESULT WINAPI ImageListImpl_GetImageRect(IImageList2 *iface, int i,
3461 RECT *prc)
3463 HIMAGELIST imgl = impl_from_IImageList2(iface);
3464 IMAGEINFO info;
3466 if (!prc)
3467 return E_FAIL;
3469 if (!ImageList_GetImageInfo(imgl, i, &info))
3470 return E_FAIL;
3472 *prc = info.rcImage;
3474 return S_OK;
3477 static HRESULT WINAPI ImageListImpl_GetIconSize(IImageList2 *iface, int *cx,
3478 int *cy)
3480 HIMAGELIST imgl = impl_from_IImageList2(iface);
3481 return ImageList_GetIconSize(imgl, cx, cy) ? S_OK : E_INVALIDARG;
3484 static HRESULT WINAPI ImageListImpl_SetIconSize(IImageList2 *iface, int cx,
3485 int cy)
3487 HIMAGELIST imgl = impl_from_IImageList2(iface);
3488 return ImageList_SetIconSize(imgl, cx, cy) ? S_OK : E_FAIL;
3491 static HRESULT WINAPI ImageListImpl_GetImageCount(IImageList2 *iface, int *pi)
3493 HIMAGELIST imgl = impl_from_IImageList2(iface);
3494 *pi = ImageList_GetImageCount(imgl);
3495 return S_OK;
3498 static HRESULT WINAPI ImageListImpl_SetImageCount(IImageList2 *iface, UINT count)
3500 HIMAGELIST imgl = impl_from_IImageList2(iface);
3501 return ImageList_SetImageCount(imgl, count) ? S_OK : E_FAIL;
3504 static HRESULT WINAPI ImageListImpl_SetBkColor(IImageList2 *iface, COLORREF clrBk,
3505 COLORREF *pclr)
3507 HIMAGELIST imgl = impl_from_IImageList2(iface);
3508 *pclr = ImageList_SetBkColor(imgl, clrBk);
3509 return S_OK;
3512 static HRESULT WINAPI ImageListImpl_GetBkColor(IImageList2 *iface, COLORREF *pclr)
3514 HIMAGELIST imgl = impl_from_IImageList2(iface);
3515 *pclr = ImageList_GetBkColor(imgl);
3516 return S_OK;
3519 static HRESULT WINAPI ImageListImpl_BeginDrag(IImageList2 *iface, int iTrack,
3520 int dxHotspot, int dyHotspot)
3522 HIMAGELIST imgl = impl_from_IImageList2(iface);
3523 return ImageList_BeginDrag(imgl, iTrack, dxHotspot, dyHotspot) ? S_OK : E_FAIL;
3526 static HRESULT WINAPI ImageListImpl_EndDrag(IImageList2 *iface)
3528 ImageList_EndDrag();
3529 return S_OK;
3532 static HRESULT WINAPI ImageListImpl_DragEnter(IImageList2 *iface, HWND hwndLock,
3533 int x, int y)
3535 return ImageList_DragEnter(hwndLock, x, y) ? S_OK : E_FAIL;
3538 static HRESULT WINAPI ImageListImpl_DragLeave(IImageList2 *iface, HWND hwndLock)
3540 return ImageList_DragLeave(hwndLock) ? S_OK : E_FAIL;
3543 static HRESULT WINAPI ImageListImpl_DragMove(IImageList2 *iface, int x, int y)
3545 return ImageList_DragMove(x, y) ? S_OK : E_FAIL;
3548 static HRESULT WINAPI ImageListImpl_SetDragCursorImage(IImageList2 *iface,
3549 IUnknown *punk, int iDrag, int dxHotspot, int dyHotspot)
3551 IImageList *iml2 = NULL;
3552 BOOL ret;
3554 if (!punk)
3555 return E_FAIL;
3557 /* TODO: Add test for IID_ImageList2 too */
3558 if (FAILED(IUnknown_QueryInterface(punk, &IID_IImageList,
3559 (void **) &iml2)))
3560 return E_FAIL;
3562 ret = ImageList_SetDragCursorImage((HIMAGELIST) iml2, iDrag, dxHotspot,
3563 dyHotspot);
3565 IImageList_Release(iml2);
3567 return ret ? S_OK : E_FAIL;
3570 static HRESULT WINAPI ImageListImpl_DragShowNolock(IImageList2 *iface, BOOL fShow)
3572 return ImageList_DragShowNolock(fShow) ? S_OK : E_FAIL;
3575 static HRESULT WINAPI ImageListImpl_GetDragImage(IImageList2 *iface, POINT *ppt,
3576 POINT *pptHotspot, REFIID riid, PVOID *ppv)
3578 HRESULT ret = E_FAIL;
3579 HIMAGELIST hNew;
3581 if (!ppv)
3582 return E_FAIL;
3584 hNew = ImageList_GetDragImage(ppt, pptHotspot);
3586 /* Get the interface for the new image list */
3587 if (hNew)
3589 IImageList *idrag = (IImageList*)hNew;
3591 ret = HIMAGELIST_QueryInterface(hNew, riid, ppv);
3592 IImageList_Release(idrag);
3595 return ret;
3598 static HRESULT WINAPI ImageListImpl_GetItemFlags(IImageList2 *iface, int i,
3599 DWORD *dwFlags)
3601 FIXME("STUB: %p %d %p\n", iface, i, dwFlags);
3602 return E_NOTIMPL;
3605 static HRESULT WINAPI ImageListImpl_GetOverlayImage(IImageList2 *iface, int iOverlay,
3606 int *piIndex)
3608 HIMAGELIST This = impl_from_IImageList2(iface);
3609 int i;
3611 if ((iOverlay < 0) || (iOverlay > This->cCurImage))
3612 return E_FAIL;
3614 for (i = 0; i < MAX_OVERLAYIMAGE; i++)
3616 if (This->nOvlIdx[i] == iOverlay)
3618 *piIndex = i + 1;
3619 return S_OK;
3623 return E_FAIL;
3626 static HRESULT WINAPI ImageListImpl_Resize(IImageList2 *iface, INT cx, INT cy)
3628 FIXME("(%p)->(%d %d): stub\n", iface, cx, cy);
3629 return E_NOTIMPL;
3632 static HRESULT WINAPI ImageListImpl_GetOriginalSize(IImageList2 *iface, INT image, DWORD flags, INT *cx, INT *cy)
3634 FIXME("(%p)->(%d %x %p %p): stub\n", iface, image, flags, cx, cy);
3635 return E_NOTIMPL;
3638 static HRESULT WINAPI ImageListImpl_SetOriginalSize(IImageList2 *iface, INT image, INT cx, INT cy)
3640 FIXME("(%p)->(%d %d %d): stub\n", iface, image, cx, cy);
3641 return E_NOTIMPL;
3644 static HRESULT WINAPI ImageListImpl_SetCallback(IImageList2 *iface, IUnknown *callback)
3646 FIXME("(%p)->(%p): stub\n", iface, callback);
3647 return E_NOTIMPL;
3650 static HRESULT WINAPI ImageListImpl_GetCallback(IImageList2 *iface, REFIID riid, void **ppv)
3652 FIXME("(%p)->(%s %p): stub\n", iface, debugstr_guid(riid), ppv);
3653 return E_NOTIMPL;
3656 static HRESULT WINAPI ImageListImpl_ForceImagePresent(IImageList2 *iface, INT image, DWORD flags)
3658 FIXME("(%p)->(%d %x): stub\n", iface, image, flags);
3659 return E_NOTIMPL;
3662 static HRESULT WINAPI ImageListImpl_DiscardImages(IImageList2 *iface, INT first_image, INT last_image, DWORD flags)
3664 FIXME("(%p)->(%d %d %x): stub\n", iface, first_image, last_image, flags);
3665 return E_NOTIMPL;
3668 static HRESULT WINAPI ImageListImpl_PreloadImages(IImageList2 *iface, IMAGELISTDRAWPARAMS *params)
3670 FIXME("(%p)->(%p): stub\n", iface, params);
3671 return E_NOTIMPL;
3674 static HRESULT WINAPI ImageListImpl_GetStatistics(IImageList2 *iface, IMAGELISTSTATS *stats)
3676 FIXME("(%p)->(%p): stub\n", iface, stats);
3677 return E_NOTIMPL;
3680 static HRESULT WINAPI ImageListImpl_Initialize(IImageList2 *iface, INT cx, INT cy, UINT flags, INT initial, INT grow)
3682 FIXME("(%p)->(%d %d %d %d %d): stub\n", iface, cx, cy, flags, initial, grow);
3683 return E_NOTIMPL;
3686 static HRESULT WINAPI ImageListImpl_Replace2(IImageList2 *iface, INT i, HBITMAP image, HBITMAP mask, IUnknown *unk, DWORD flags)
3688 FIXME("(%p)->(%d %p %p %p %x): stub\n", iface, i, image, mask, unk, flags);
3689 return E_NOTIMPL;
3692 static HRESULT WINAPI ImageListImpl_ReplaceFromImageList(IImageList2 *iface, INT i, IImageList *imagelist, INT src,
3693 IUnknown *unk, DWORD flags)
3695 FIXME("(%p)->(%d %p %d %p %x): stub\n", iface, i, imagelist, src, unk, flags);
3696 return E_NOTIMPL;
3699 static const IImageList2Vtbl ImageListImpl_Vtbl = {
3700 ImageListImpl_QueryInterface,
3701 ImageListImpl_AddRef,
3702 ImageListImpl_Release,
3703 ImageListImpl_Add,
3704 ImageListImpl_ReplaceIcon,
3705 ImageListImpl_SetOverlayImage,
3706 ImageListImpl_Replace,
3707 ImageListImpl_AddMasked,
3708 ImageListImpl_Draw,
3709 ImageListImpl_Remove,
3710 ImageListImpl_GetIcon,
3711 ImageListImpl_GetImageInfo,
3712 ImageListImpl_Copy,
3713 ImageListImpl_Merge,
3714 ImageListImpl_Clone,
3715 ImageListImpl_GetImageRect,
3716 ImageListImpl_GetIconSize,
3717 ImageListImpl_SetIconSize,
3718 ImageListImpl_GetImageCount,
3719 ImageListImpl_SetImageCount,
3720 ImageListImpl_SetBkColor,
3721 ImageListImpl_GetBkColor,
3722 ImageListImpl_BeginDrag,
3723 ImageListImpl_EndDrag,
3724 ImageListImpl_DragEnter,
3725 ImageListImpl_DragLeave,
3726 ImageListImpl_DragMove,
3727 ImageListImpl_SetDragCursorImage,
3728 ImageListImpl_DragShowNolock,
3729 ImageListImpl_GetDragImage,
3730 ImageListImpl_GetItemFlags,
3731 ImageListImpl_GetOverlayImage,
3732 ImageListImpl_Resize,
3733 ImageListImpl_GetOriginalSize,
3734 ImageListImpl_SetOriginalSize,
3735 ImageListImpl_SetCallback,
3736 ImageListImpl_GetCallback,
3737 ImageListImpl_ForceImagePresent,
3738 ImageListImpl_DiscardImages,
3739 ImageListImpl_PreloadImages,
3740 ImageListImpl_GetStatistics,
3741 ImageListImpl_Initialize,
3742 ImageListImpl_Replace2,
3743 ImageListImpl_ReplaceFromImageList
3746 static BOOL is_valid(HIMAGELIST himl)
3748 BOOL valid;
3749 __TRY
3751 valid = himl && himl->IImageList2_iface.lpVtbl == &ImageListImpl_Vtbl;
3753 __EXCEPT_PAGE_FAULT
3755 valid = FALSE;
3757 __ENDTRY
3758 return valid;
3761 /*************************************************************************
3762 * HIMAGELIST_QueryInterface [COMCTL32.@]
3764 * Returns a pointer to an IImageList or IImageList2 object for the given
3765 * HIMAGELIST.
3767 * PARAMS
3768 * himl [I] Image list handle.
3769 * riid [I] Identifier of the requested interface.
3770 * ppv [O] Returns the address of the pointer requested, or NULL.
3772 * RETURNS
3773 * Success: S_OK.
3774 * Failure: Error value.
3776 HRESULT WINAPI
3777 HIMAGELIST_QueryInterface (HIMAGELIST himl, REFIID riid, void **ppv)
3779 TRACE("(%p,%s,%p)\n", himl, debugstr_guid(riid), ppv);
3780 return IImageList2_QueryInterface((IImageList2 *) himl, riid, ppv);
3783 static HRESULT ImageListImpl_CreateInstance(const IUnknown *pUnkOuter, REFIID iid, void** ppv)
3785 HIMAGELIST This;
3786 HRESULT ret;
3788 TRACE("(%p,%s,%p)\n", pUnkOuter, debugstr_guid(iid), ppv);
3790 *ppv = NULL;
3792 if (pUnkOuter) return CLASS_E_NOAGGREGATION;
3794 This = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct _IMAGELIST));
3795 if (!This) return E_OUTOFMEMORY;
3797 This->IImageList2_iface.lpVtbl = &ImageListImpl_Vtbl;
3798 This->ref = 1;
3800 ret = IImageList2_QueryInterface(&This->IImageList2_iface, iid, ppv);
3801 IImageList2_Release(&This->IImageList2_iface);
3803 return ret;