Assorted typos fixes.
[wine.git] / dlls / comctl32 / imagelist.c
blobec90a9c66cf6ed9610797256e537b17dbbbf6515
1 /*
2 * ImageList implementation
4 * Copyright 1998 Eric Kohl
5 * Copyright 2000 Jason Mawdsley
6 * Copyright 2001, 2004 Michael Stefaniuc
7 * Copyright 2001 Charles Loep for CodeWeavers
8 * Copyright 2002 Dimitrie O. Paun
9 * Copyright 2009 Owen Rudge for CodeWeavers
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
16 * This library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 * NOTE
27 * This code was audited for completeness against the documented features
28 * of Comctl32.dll version 6.0 on Sep. 12, 2002, by Dimitrie O. Paun.
30 * Unless otherwise noted, we believe this code to be complete, as per
31 * the specification mentioned above.
32 * If you discover missing features, or bugs, please note them below.
34 * TODO:
35 * - Add support for ILD_PRESERVEALPHA, ILD_SCALE, ILD_DPISCALE
36 * - Add support for ILS_GLOW, ILS_SHADOW, ILS_SATURATE
37 * - Thread-safe locking
40 #include <stdarg.h>
41 #include <stdlib.h>
42 #include <string.h>
44 #define COBJMACROS
46 #include "winerror.h"
47 #include "windef.h"
48 #include "winbase.h"
49 #include "objbase.h"
50 #include "wingdi.h"
51 #include "winuser.h"
52 #include "commctrl.h"
53 #include "comctl32.h"
54 #include "commoncontrols.h"
55 #include "wine/debug.h"
56 #include "wine/exception.h"
58 WINE_DEFAULT_DEBUG_CHANNEL(imagelist);
60 #define MAX_OVERLAYIMAGE 15
62 struct _IMAGELIST
64 const struct IImageListVtbl *lpVtbl; /* 00: IImageList vtable */
66 INT cCurImage; /* 04: ImageCount */
67 INT cMaxImage; /* 08: maximages */
68 INT cGrow; /* 0C: cGrow */
69 INT cx; /* 10: cx */
70 INT cy; /* 14: cy */
71 DWORD x4;
72 UINT flags; /* 1C: flags */
73 COLORREF clrFg; /* 20: foreground color */
74 COLORREF clrBk; /* 24: background color */
77 HBITMAP hbmImage; /* 28: images Bitmap */
78 HBITMAP hbmMask; /* 2C: masks Bitmap */
79 HDC hdcImage; /* 30: images MemDC */
80 HDC hdcMask; /* 34: masks MemDC */
81 INT nOvlIdx[MAX_OVERLAYIMAGE]; /* 38: overlay images index */
83 /* not yet found out */
84 HBRUSH hbrBlend25;
85 HBRUSH hbrBlend50;
86 INT cInitial;
87 UINT uBitsPixel;
88 char *has_alpha;
90 LONG ref; /* reference count */
93 #define IMAGELIST_MAGIC 0x53414D58
95 /* Header used by ImageList_Read() and ImageList_Write() */
96 #include "pshpack2.h"
97 typedef struct _ILHEAD
99 USHORT usMagic;
100 USHORT usVersion;
101 WORD cCurImage;
102 WORD cMaxImage;
103 WORD cGrow;
104 WORD cx;
105 WORD cy;
106 COLORREF bkcolor;
107 WORD flags;
108 SHORT ovls[4];
109 } ILHEAD;
110 #include "poppack.h"
112 /* internal image list data used for Drag & Drop operations */
113 typedef struct
115 HWND hwnd;
116 HIMAGELIST himl;
117 HIMAGELIST himlNoCursor;
118 /* position of the drag image relative to the window */
119 INT x;
120 INT y;
121 /* offset of the hotspot relative to the origin of the image */
122 INT dxHotspot;
123 INT dyHotspot;
124 /* is the drag image visible */
125 BOOL bShow;
126 /* saved background */
127 HBITMAP hbmBg;
128 } INTERNALDRAG;
130 static INTERNALDRAG InternalDrag = { 0, 0, 0, 0, 0, 0, 0, FALSE, 0 };
132 static HBITMAP ImageList_CreateImage(HDC hdc, HIMAGELIST himl, UINT count);
133 static HRESULT ImageListImpl_CreateInstance(const IUnknown *pUnkOuter, REFIID iid, void** ppv);
134 static inline BOOL is_valid(HIMAGELIST himl);
137 * An imagelist with N images is tiled like this:
139 * N/4 ->
141 * 4 048C..
142 * 159D..
143 * | 26AE.N
144 * V 37BF.
147 #define TILE_COUNT 4
149 static inline UINT imagelist_height( UINT count )
151 return ((count + TILE_COUNT - 1)/TILE_COUNT);
154 static inline void imagelist_point_from_index( HIMAGELIST himl, UINT index, LPPOINT pt )
156 pt->x = (index%TILE_COUNT) * himl->cx;
157 pt->y = (index/TILE_COUNT) * himl->cy;
160 static inline void imagelist_get_bitmap_size( HIMAGELIST himl, UINT count, SIZE *sz )
162 sz->cx = himl->cx * TILE_COUNT;
163 sz->cy = imagelist_height( count ) * himl->cy;
166 static inline int get_dib_stride( int width, int bpp )
168 return ((width * bpp + 31) >> 3) & ~3;
171 static inline int get_dib_image_size( const BITMAPINFO *info )
173 return get_dib_stride( info->bmiHeader.biWidth, info->bmiHeader.biBitCount )
174 * abs( info->bmiHeader.biHeight );
178 * imagelist_copy_images()
180 * Copies a block of count images from offset src in the list to offset dest.
181 * Images are copied a row at at time. Assumes hdcSrc and hdcDest are different.
183 static inline void imagelist_copy_images( HIMAGELIST himl, HDC hdcSrc, HDC hdcDest,
184 UINT src, UINT count, UINT dest )
186 POINT ptSrc, ptDest;
187 SIZE sz;
188 UINT i;
190 for ( i=0; i<TILE_COUNT; i++ )
192 imagelist_point_from_index( himl, src+i, &ptSrc );
193 imagelist_point_from_index( himl, dest+i, &ptDest );
194 sz.cx = himl->cx;
195 sz.cy = himl->cy * imagelist_height( count - i );
197 BitBlt( hdcDest, ptDest.x, ptDest.y, sz.cx, sz.cy,
198 hdcSrc, ptSrc.x, ptSrc.y, SRCCOPY );
202 static void add_dib_bits( HIMAGELIST himl, int pos, int count, int width, int height,
203 BITMAPINFO *info, BITMAPINFO *mask_info, DWORD *bits, BYTE *mask_bits )
205 int i, j, n;
206 POINT pt;
207 int stride = info->bmiHeader.biWidth;
208 int mask_stride = (info->bmiHeader.biWidth + 31) / 32 * 4;
210 for (n = 0; n < count; n++)
212 BOOL has_alpha = FALSE;
214 imagelist_point_from_index( himl, pos + n, &pt );
216 /* check if bitmap has an alpha channel */
217 for (i = 0; i < height && !has_alpha; i++)
218 for (j = n * width; j < (n + 1) * width; j++)
219 if ((has_alpha = ((bits[i * stride + j] & 0xff000000) != 0))) break;
221 if (!has_alpha) /* generate alpha channel from the mask */
223 for (i = 0; i < height; i++)
224 for (j = n * width; j < (n + 1) * width; j++)
225 if (!mask_info || !((mask_bits[i * mask_stride + j / 8] << (j % 8)) & 0x80))
226 bits[i * stride + j] |= 0xff000000;
227 else
228 bits[i * stride + j] = 0;
230 else
232 himl->has_alpha[pos + n] = 1;
234 if (mask_info && himl->hbmMask) /* generate the mask from the alpha channel */
236 for (i = 0; i < height; i++)
237 for (j = n * width; j < (n + 1) * width; j++)
238 if ((bits[i * stride + j] >> 24) > 25) /* more than 10% alpha */
239 mask_bits[i * mask_stride + j / 8] &= ~(0x80 >> (j % 8));
240 else
241 mask_bits[i * mask_stride + j / 8] |= 0x80 >> (j % 8);
244 StretchDIBits( himl->hdcImage, pt.x, pt.y, himl->cx, himl->cy,
245 n * width, 0, width, height, bits, info, DIB_RGB_COLORS, SRCCOPY );
246 if (mask_info)
247 StretchDIBits( himl->hdcMask, pt.x, pt.y, himl->cx, himl->cy,
248 n * width, 0, width, height, mask_bits, mask_info, DIB_RGB_COLORS, SRCCOPY );
252 /* add images with an alpha channel when the image list is 32 bpp */
253 static BOOL add_with_alpha( HIMAGELIST himl, HDC hdc, int pos, int count,
254 int width, int height, HBITMAP hbmImage, HBITMAP hbmMask )
256 BOOL ret = FALSE;
257 BITMAP bm;
258 BITMAPINFO *info, *mask_info = NULL;
259 DWORD *bits = NULL;
260 BYTE *mask_bits = NULL;
261 DWORD mask_width;
263 if (!GetObjectW( hbmImage, sizeof(bm), &bm )) return FALSE;
265 /* if either the imagelist or the source bitmap don't have an alpha channel, bail out now */
266 if (!himl->has_alpha) return FALSE;
267 if (bm.bmBitsPixel != 32) return FALSE;
269 SelectObject( hdc, hbmImage );
270 mask_width = (bm.bmWidth + 31) / 32 * 4;
272 if (!(info = HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET( BITMAPINFO, bmiColors[256] )))) goto done;
273 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
274 info->bmiHeader.biWidth = bm.bmWidth;
275 info->bmiHeader.biHeight = -height;
276 info->bmiHeader.biPlanes = 1;
277 info->bmiHeader.biBitCount = 32;
278 info->bmiHeader.biCompression = BI_RGB;
279 info->bmiHeader.biSizeImage = bm.bmWidth * height * 4;
280 info->bmiHeader.biXPelsPerMeter = 0;
281 info->bmiHeader.biYPelsPerMeter = 0;
282 info->bmiHeader.biClrUsed = 0;
283 info->bmiHeader.biClrImportant = 0;
284 if (!(bits = HeapAlloc( GetProcessHeap(), 0, info->bmiHeader.biSizeImage ))) goto done;
285 if (!GetDIBits( hdc, hbmImage, 0, height, bits, info, DIB_RGB_COLORS )) goto done;
287 if (hbmMask)
289 if (!(mask_info = HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET( BITMAPINFO, bmiColors[2] ))))
290 goto done;
291 mask_info->bmiHeader = info->bmiHeader;
292 mask_info->bmiHeader.biBitCount = 1;
293 mask_info->bmiHeader.biSizeImage = mask_width * height;
294 if (!(mask_bits = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, info->bmiHeader.biSizeImage )))
295 goto done;
296 if (!GetDIBits( hdc, hbmMask, 0, height, mask_bits, mask_info, DIB_RGB_COLORS )) goto done;
299 add_dib_bits( himl, pos, count, width, height, info, mask_info, bits, mask_bits );
300 ret = TRUE;
302 done:
303 HeapFree( GetProcessHeap(), 0, info );
304 HeapFree( GetProcessHeap(), 0, mask_info );
305 HeapFree( GetProcessHeap(), 0, bits );
306 HeapFree( GetProcessHeap(), 0, mask_bits );
307 return ret;
310 /*************************************************************************
311 * IMAGELIST_InternalExpandBitmaps [Internal]
313 * Expands the bitmaps of an image list by the given number of images.
315 * PARAMS
316 * himl [I] handle to image list
317 * nImageCount [I] number of images to add
319 * RETURNS
320 * nothing
322 * NOTES
323 * This function CANNOT be used to reduce the number of images.
325 static void
326 IMAGELIST_InternalExpandBitmaps(HIMAGELIST himl, INT nImageCount)
328 HDC hdcBitmap;
329 HBITMAP hbmNewBitmap, hbmNull;
330 INT nNewCount;
331 SIZE sz;
333 TRACE("%p has allocated %d, max %d, grow %d images\n", himl, himl->cCurImage, himl->cMaxImage, himl->cGrow);
335 if (himl->cCurImage + nImageCount < himl->cMaxImage)
336 return;
338 nNewCount = himl->cMaxImage + max(nImageCount, himl->cGrow) + 1;
340 imagelist_get_bitmap_size(himl, nNewCount, &sz);
342 TRACE("Create expanded bitmaps : himl=%p x=%d y=%d count=%d\n", himl, sz.cx, sz.cy, nNewCount);
343 hdcBitmap = CreateCompatibleDC (0);
345 hbmNewBitmap = ImageList_CreateImage(hdcBitmap, himl, nNewCount);
347 if (hbmNewBitmap == 0)
348 ERR("creating new image bitmap (x=%d y=%d)!\n", sz.cx, sz.cy);
350 if (himl->cCurImage)
352 hbmNull = SelectObject (hdcBitmap, hbmNewBitmap);
353 BitBlt (hdcBitmap, 0, 0, sz.cx, sz.cy,
354 himl->hdcImage, 0, 0, SRCCOPY);
355 SelectObject (hdcBitmap, hbmNull);
357 SelectObject (himl->hdcImage, hbmNewBitmap);
358 DeleteObject (himl->hbmImage);
359 himl->hbmImage = hbmNewBitmap;
361 if (himl->flags & ILC_MASK)
363 hbmNewBitmap = CreateBitmap (sz.cx, sz.cy, 1, 1, NULL);
365 if (hbmNewBitmap == 0)
366 ERR("creating new mask bitmap!\n");
368 if(himl->cCurImage)
370 hbmNull = SelectObject (hdcBitmap, hbmNewBitmap);
371 BitBlt (hdcBitmap, 0, 0, sz.cx, sz.cy,
372 himl->hdcMask, 0, 0, SRCCOPY);
373 SelectObject (hdcBitmap, hbmNull);
375 SelectObject (himl->hdcMask, hbmNewBitmap);
376 DeleteObject (himl->hbmMask);
377 himl->hbmMask = hbmNewBitmap;
380 if (himl->has_alpha)
382 char *new_alpha = HeapReAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, himl->has_alpha, nNewCount );
383 if (new_alpha) himl->has_alpha = new_alpha;
384 else
386 HeapFree( GetProcessHeap(), 0, himl->has_alpha );
387 himl->has_alpha = NULL;
391 himl->cMaxImage = nNewCount;
393 DeleteDC (hdcBitmap);
397 /*************************************************************************
398 * ImageList_Add [COMCTL32.@]
400 * Add an image or images to an image list.
402 * PARAMS
403 * himl [I] handle to image list
404 * hbmImage [I] handle to image bitmap
405 * hbmMask [I] handle to mask bitmap
407 * RETURNS
408 * Success: Index of the first new image.
409 * Failure: -1
412 INT WINAPI
413 ImageList_Add (HIMAGELIST himl, HBITMAP hbmImage, HBITMAP hbmMask)
415 HDC hdcBitmap, hdcTemp = 0;
416 INT nFirstIndex, nImageCount, i;
417 BITMAP bmp;
418 POINT pt;
420 TRACE("himl=%p hbmimage=%p hbmmask=%p\n", himl, hbmImage, hbmMask);
421 if (!is_valid(himl))
422 return -1;
424 if (!GetObjectW(hbmImage, sizeof(BITMAP), &bmp))
425 return -1;
427 TRACE("himl %p, cCurImage %d, cMaxImage %d, cGrow %d, cx %d, cy %d\n",
428 himl, himl->cCurImage, himl->cMaxImage, himl->cGrow, himl->cx, himl->cy);
430 nImageCount = bmp.bmWidth / himl->cx;
432 TRACE("%p has %d images (%d x %d)\n", hbmImage, nImageCount, bmp.bmWidth, bmp.bmHeight);
434 IMAGELIST_InternalExpandBitmaps(himl, nImageCount);
436 hdcBitmap = CreateCompatibleDC(0);
438 SelectObject(hdcBitmap, hbmImage);
440 if (add_with_alpha( himl, hdcBitmap, himl->cCurImage, nImageCount,
441 himl->cx, min( himl->cy, bmp.bmHeight), hbmImage, hbmMask ))
442 goto done;
444 if (himl->hbmMask)
446 hdcTemp = CreateCompatibleDC(0);
447 SelectObject(hdcTemp, hbmMask);
450 for (i=0; i<nImageCount; i++)
452 imagelist_point_from_index( himl, himl->cCurImage + i, &pt );
454 /* Copy result to the imagelist
456 BitBlt( himl->hdcImage, pt.x, pt.y, himl->cx, bmp.bmHeight,
457 hdcBitmap, i*himl->cx, 0, SRCCOPY );
459 if (!himl->hbmMask)
460 continue;
462 BitBlt( himl->hdcMask, pt.x, pt.y, himl->cx, bmp.bmHeight,
463 hdcTemp, i*himl->cx, 0, SRCCOPY );
465 /* Remove the background from the image
467 BitBlt( himl->hdcImage, pt.x, pt.y, himl->cx, bmp.bmHeight,
468 himl->hdcMask, pt.x, pt.y, 0x220326 ); /* NOTSRCAND */
470 if (hdcTemp) DeleteDC(hdcTemp);
472 done:
473 DeleteDC(hdcBitmap);
475 nFirstIndex = himl->cCurImage;
476 himl->cCurImage += nImageCount;
478 return nFirstIndex;
482 /*************************************************************************
483 * ImageList_AddIcon [COMCTL32.@]
485 * Adds an icon to an image list.
487 * PARAMS
488 * himl [I] handle to image list
489 * hIcon [I] handle to icon
491 * RETURNS
492 * Success: index of the new image
493 * Failure: -1
495 #undef ImageList_AddIcon
496 INT WINAPI ImageList_AddIcon (HIMAGELIST himl, HICON hIcon)
498 return ImageList_ReplaceIcon (himl, -1, hIcon);
502 /*************************************************************************
503 * ImageList_AddMasked [COMCTL32.@]
505 * Adds an image or images to an image list and creates a mask from the
506 * specified bitmap using the mask color.
508 * PARAMS
509 * himl [I] handle to image list.
510 * hBitmap [I] handle to bitmap
511 * clrMask [I] mask color.
513 * RETURNS
514 * Success: Index of the first new image.
515 * Failure: -1
518 INT WINAPI
519 ImageList_AddMasked (HIMAGELIST himl, HBITMAP hBitmap, COLORREF clrMask)
521 HDC hdcMask, hdcBitmap;
522 INT ret;
523 BITMAP bmp;
524 HBITMAP hMaskBitmap;
525 COLORREF bkColor;
527 TRACE("himl=%p hbitmap=%p clrmask=%x\n", himl, hBitmap, clrMask);
528 if (!is_valid(himl))
529 return -1;
531 if (!GetObjectW(hBitmap, sizeof(BITMAP), &bmp))
532 return -1;
534 hdcBitmap = CreateCompatibleDC(0);
535 SelectObject(hdcBitmap, hBitmap);
537 /* Create a temp Mask so we can remove the background of the Image */
538 hdcMask = CreateCompatibleDC(0);
539 hMaskBitmap = CreateBitmap(bmp.bmWidth, bmp.bmHeight, 1, 1, NULL);
540 SelectObject(hdcMask, hMaskBitmap);
542 /* create monochrome image to the mask bitmap */
543 bkColor = (clrMask != CLR_DEFAULT) ? clrMask : GetPixel (hdcBitmap, 0, 0);
544 SetBkColor (hdcBitmap, bkColor);
545 BitBlt (hdcMask, 0, 0, bmp.bmWidth, bmp.bmHeight, hdcBitmap, 0, 0, SRCCOPY);
548 * Remove the background from the image
550 * WINDOWS BUG ALERT!!!!!!
551 * The statement below should not be done in common practice
552 * but this is how ImageList_AddMasked works in Windows.
553 * It overwrites the original bitmap passed, this was discovered
554 * by using the same bitmap to iterate the different styles
555 * on windows where it failed (BUT ImageList_Add is OK)
556 * This is here in case some apps rely on this bug
558 * Blt mode 0x220326 is NOTSRCAND
560 if (bmp.bmBitsPixel > 8) /* NOTSRCAND can't work with palettes */
562 SetBkColor(hdcBitmap, RGB(255,255,255));
563 BitBlt(hdcBitmap, 0, 0, bmp.bmWidth, bmp.bmHeight, hdcMask, 0, 0, 0x220326);
566 DeleteDC(hdcBitmap);
567 DeleteDC(hdcMask);
569 ret = ImageList_Add( himl, hBitmap, hMaskBitmap );
571 DeleteObject(hMaskBitmap);
572 return ret;
576 /*************************************************************************
577 * ImageList_BeginDrag [COMCTL32.@]
579 * Creates a temporary image list that contains one image. It will be used
580 * as a drag image.
582 * PARAMS
583 * himlTrack [I] handle to the source image list
584 * iTrack [I] index of the drag image in the source image list
585 * dxHotspot [I] X position of the hot spot of the drag image
586 * dyHotspot [I] Y position of the hot spot of the drag image
588 * RETURNS
589 * Success: TRUE
590 * Failure: FALSE
593 BOOL WINAPI
594 ImageList_BeginDrag (HIMAGELIST himlTrack, INT iTrack,
595 INT dxHotspot, INT dyHotspot)
597 INT cx, cy;
599 TRACE("(himlTrack=%p iTrack=%d dx=%d dy=%d)\n", himlTrack, iTrack,
600 dxHotspot, dyHotspot);
602 if (!is_valid(himlTrack))
603 return FALSE;
605 if (InternalDrag.himl)
606 ImageList_EndDrag ();
608 cx = himlTrack->cx;
609 cy = himlTrack->cy;
611 InternalDrag.himlNoCursor = InternalDrag.himl = ImageList_Create (cx, cy, himlTrack->flags, 1, 1);
612 if (InternalDrag.himl == NULL) {
613 WARN("Error creating drag image list!\n");
614 return FALSE;
617 InternalDrag.dxHotspot = dxHotspot;
618 InternalDrag.dyHotspot = dyHotspot;
620 /* copy image */
621 BitBlt (InternalDrag.himl->hdcImage, 0, 0, cx, cy, himlTrack->hdcImage, iTrack * cx, 0, SRCCOPY);
623 /* copy mask */
624 BitBlt (InternalDrag.himl->hdcMask, 0, 0, cx, cy, himlTrack->hdcMask, iTrack * cx, 0, SRCCOPY);
626 InternalDrag.himl->cCurImage = 1;
628 return TRUE;
632 /*************************************************************************
633 * ImageList_Copy [COMCTL32.@]
635 * Copies an image of the source image list to an image of the
636 * destination image list. Images can be copied or swapped.
638 * PARAMS
639 * himlDst [I] handle to the destination image list
640 * iDst [I] destination image index.
641 * himlSrc [I] handle to the source image list
642 * iSrc [I] source image index
643 * uFlags [I] flags for the copy operation
645 * RETURNS
646 * Success: TRUE
647 * Failure: FALSE
649 * NOTES
650 * Copying from one image list to another is possible. The original
651 * implementation just copies or swaps within one image list.
652 * Could this feature become a bug??? ;-)
655 BOOL WINAPI
656 ImageList_Copy (HIMAGELIST himlDst, INT iDst, HIMAGELIST himlSrc,
657 INT iSrc, UINT uFlags)
659 POINT ptSrc, ptDst;
661 TRACE("himlDst=%p iDst=%d himlSrc=%p iSrc=%d\n", himlDst, iDst, himlSrc, iSrc);
663 if (!is_valid(himlSrc) || !is_valid(himlDst))
664 return FALSE;
665 if ((iDst < 0) || (iDst >= himlDst->cCurImage))
666 return FALSE;
667 if ((iSrc < 0) || (iSrc >= himlSrc->cCurImage))
668 return FALSE;
670 imagelist_point_from_index( himlDst, iDst, &ptDst );
671 imagelist_point_from_index( himlSrc, iSrc, &ptSrc );
673 if (uFlags & ILCF_SWAP) {
674 /* swap */
675 HDC hdcBmp;
676 HBITMAP hbmTempImage, hbmTempMask;
678 hdcBmp = CreateCompatibleDC (0);
680 /* create temporary bitmaps */
681 hbmTempImage = CreateBitmap (himlSrc->cx, himlSrc->cy, 1,
682 himlSrc->uBitsPixel, NULL);
683 hbmTempMask = CreateBitmap (himlSrc->cx, himlSrc->cy, 1,
684 1, NULL);
686 /* copy (and stretch) destination to temporary bitmaps.(save) */
687 /* image */
688 SelectObject (hdcBmp, hbmTempImage);
689 StretchBlt (hdcBmp, 0, 0, himlSrc->cx, himlSrc->cy,
690 himlDst->hdcImage, ptDst.x, ptDst.y, himlDst->cx, himlDst->cy,
691 SRCCOPY);
692 /* mask */
693 SelectObject (hdcBmp, hbmTempMask);
694 StretchBlt (hdcBmp, 0, 0, himlSrc->cx, himlSrc->cy,
695 himlDst->hdcMask, ptDst.x, ptDst.y, himlDst->cx, himlDst->cy,
696 SRCCOPY);
698 /* copy (and stretch) source to destination */
699 /* image */
700 StretchBlt (himlDst->hdcImage, ptDst.x, ptDst.y, himlDst->cx, himlDst->cy,
701 himlSrc->hdcImage, ptSrc.x, ptSrc.y, himlSrc->cx, himlSrc->cy,
702 SRCCOPY);
703 /* mask */
704 StretchBlt (himlDst->hdcMask, ptDst.x, ptDst.y, himlDst->cx, himlDst->cy,
705 himlSrc->hdcMask, ptSrc.x, ptSrc.y, himlSrc->cx, himlSrc->cy,
706 SRCCOPY);
708 /* copy (without stretching) temporary bitmaps to source (restore) */
709 /* mask */
710 BitBlt (himlSrc->hdcMask, ptSrc.x, ptSrc.y, himlSrc->cx, himlSrc->cy,
711 hdcBmp, 0, 0, SRCCOPY);
713 /* image */
714 BitBlt (himlSrc->hdcImage, ptSrc.x, ptSrc.y, himlSrc->cx, himlSrc->cy,
715 hdcBmp, 0, 0, SRCCOPY);
716 /* delete temporary bitmaps */
717 DeleteObject (hbmTempMask);
718 DeleteObject (hbmTempImage);
719 DeleteDC(hdcBmp);
721 else {
722 /* copy image */
723 StretchBlt (himlDst->hdcImage, ptDst.x, ptDst.y, himlDst->cx, himlDst->cy,
724 himlSrc->hdcImage, ptSrc.x, ptSrc.y, himlSrc->cx, himlSrc->cy,
725 SRCCOPY);
727 /* copy mask */
728 StretchBlt (himlDst->hdcMask, ptDst.x, ptDst.y, himlDst->cx, himlDst->cy,
729 himlSrc->hdcMask, ptSrc.x, ptSrc.y, himlSrc->cx, himlSrc->cy,
730 SRCCOPY);
733 return TRUE;
737 /*************************************************************************
738 * ImageList_Create [COMCTL32.@]
740 * Creates a new image list.
742 * PARAMS
743 * cx [I] image height
744 * cy [I] image width
745 * flags [I] creation flags
746 * cInitial [I] initial number of images in the image list
747 * cGrow [I] number of images by which image list grows
749 * RETURNS
750 * Success: Handle to the created image list
751 * Failure: NULL
753 HIMAGELIST WINAPI
754 ImageList_Create (INT cx, INT cy, UINT flags,
755 INT cInitial, INT cGrow)
757 HIMAGELIST himl;
758 INT nCount;
759 HBITMAP hbmTemp;
760 UINT ilc = (flags & 0xFE);
761 static const WORD aBitBlend25[] =
762 {0xAA, 0x00, 0x55, 0x00, 0xAA, 0x00, 0x55, 0x00};
764 static const WORD aBitBlend50[] =
765 {0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA};
767 TRACE("(%d %d 0x%x %d %d)\n", cx, cy, flags, cInitial, cGrow);
769 if (cx <= 0 || cy <= 0) return NULL;
771 /* Create the IImageList interface for the image list */
772 if (FAILED(ImageListImpl_CreateInstance(NULL, &IID_IImageList, (void **)&himl)))
773 return NULL;
775 cGrow = (WORD)((max( cGrow, 1 ) + 3) & ~3);
777 if (cGrow > 256)
779 /* Windows doesn't limit the size here, but X11 doesn't let us allocate such huge bitmaps */
780 WARN( "grow %d too large, limiting to 256\n", cGrow );
781 cGrow = 256;
784 himl->cx = cx;
785 himl->cy = cy;
786 himl->flags = flags;
787 himl->cMaxImage = cInitial + 1;
788 himl->cInitial = cInitial;
789 himl->cGrow = cGrow;
790 himl->clrFg = CLR_DEFAULT;
791 himl->clrBk = CLR_NONE;
793 /* initialize overlay mask indices */
794 for (nCount = 0; nCount < MAX_OVERLAYIMAGE; nCount++)
795 himl->nOvlIdx[nCount] = -1;
797 /* Create Image & Mask DCs */
798 himl->hdcImage = CreateCompatibleDC (0);
799 if (!himl->hdcImage)
800 goto cleanup;
801 if (himl->flags & ILC_MASK){
802 himl->hdcMask = CreateCompatibleDC(0);
803 if (!himl->hdcMask)
804 goto cleanup;
807 /* Default to ILC_COLOR4 if none of the ILC_COLOR* flags are specified */
808 if (ilc == ILC_COLOR)
810 ilc = ILC_COLOR4;
811 himl->flags |= ILC_COLOR4;
814 if (ilc >= ILC_COLOR4 && ilc <= ILC_COLOR32)
815 himl->uBitsPixel = ilc;
816 else
817 himl->uBitsPixel = (UINT)GetDeviceCaps (himl->hdcImage, BITSPIXEL);
819 if (himl->cMaxImage > 0) {
820 himl->hbmImage = ImageList_CreateImage(himl->hdcImage, himl, himl->cMaxImage);
821 SelectObject(himl->hdcImage, himl->hbmImage);
822 } else
823 himl->hbmImage = 0;
825 if ((himl->cMaxImage > 0) && (himl->flags & ILC_MASK)) {
826 SIZE sz;
828 imagelist_get_bitmap_size(himl, himl->cMaxImage, &sz);
829 himl->hbmMask = CreateBitmap (sz.cx, sz.cy, 1, 1, NULL);
830 if (himl->hbmMask == 0) {
831 ERR("Error creating mask bitmap!\n");
832 goto cleanup;
834 SelectObject(himl->hdcMask, himl->hbmMask);
836 else
837 himl->hbmMask = 0;
839 if (ilc == ILC_COLOR32)
840 himl->has_alpha = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, himl->cMaxImage );
841 else
842 himl->has_alpha = NULL;
844 /* create blending brushes */
845 hbmTemp = CreateBitmap (8, 8, 1, 1, aBitBlend25);
846 himl->hbrBlend25 = CreatePatternBrush (hbmTemp);
847 DeleteObject (hbmTemp);
849 hbmTemp = CreateBitmap (8, 8, 1, 1, aBitBlend50);
850 himl->hbrBlend50 = CreatePatternBrush (hbmTemp);
851 DeleteObject (hbmTemp);
853 TRACE("created imagelist %p\n", himl);
854 return himl;
856 cleanup:
857 ImageList_Destroy(himl);
858 return NULL;
862 /*************************************************************************
863 * ImageList_Destroy [COMCTL32.@]
865 * Destroys an image list.
867 * PARAMS
868 * himl [I] handle to image list
870 * RETURNS
871 * Success: TRUE
872 * Failure: FALSE
875 BOOL WINAPI
876 ImageList_Destroy (HIMAGELIST himl)
878 if (!is_valid(himl))
879 return FALSE;
881 IImageList_Release((IImageList *) himl);
882 return TRUE;
886 /*************************************************************************
887 * ImageList_DragEnter [COMCTL32.@]
889 * Locks window update and displays the drag image at the given position.
891 * PARAMS
892 * hwndLock [I] handle of the window that owns the drag image.
893 * x [I] X position of the drag image.
894 * y [I] Y position of the drag image.
896 * RETURNS
897 * Success: TRUE
898 * Failure: FALSE
900 * NOTES
901 * The position of the drag image is relative to the window, not
902 * the client area.
905 BOOL WINAPI
906 ImageList_DragEnter (HWND hwndLock, INT x, INT y)
908 TRACE("(hwnd=%p x=%d y=%d)\n", hwndLock, x, y);
910 if (!is_valid(InternalDrag.himl))
911 return FALSE;
913 if (hwndLock)
914 InternalDrag.hwnd = hwndLock;
915 else
916 InternalDrag.hwnd = GetDesktopWindow ();
918 InternalDrag.x = x;
919 InternalDrag.y = y;
921 /* draw the drag image and save the background */
922 if (!ImageList_DragShowNolock(TRUE)) {
923 return FALSE;
926 return TRUE;
930 /*************************************************************************
931 * ImageList_DragLeave [COMCTL32.@]
933 * Unlocks window update and hides the drag image.
935 * PARAMS
936 * hwndLock [I] handle of the window that owns the drag image.
938 * RETURNS
939 * Success: TRUE
940 * Failure: FALSE
943 BOOL WINAPI
944 ImageList_DragLeave (HWND hwndLock)
946 /* As we don't save drag info in the window this can lead to problems if
947 an app does not supply the same window as DragEnter */
948 /* if (hwndLock)
949 InternalDrag.hwnd = hwndLock;
950 else
951 InternalDrag.hwnd = GetDesktopWindow (); */
952 if(!hwndLock)
953 hwndLock = GetDesktopWindow();
954 if(InternalDrag.hwnd != hwndLock)
955 FIXME("DragLeave hWnd != DragEnter hWnd\n");
957 ImageList_DragShowNolock (FALSE);
959 return TRUE;
963 /*************************************************************************
964 * ImageList_InternalDragDraw [Internal]
966 * Draws the drag image.
968 * PARAMS
969 * hdc [I] device context to draw into.
970 * x [I] X position of the drag image.
971 * y [I] Y position of the drag image.
973 * RETURNS
974 * Success: TRUE
975 * Failure: FALSE
977 * NOTES
978 * The position of the drag image is relative to the window, not
979 * the client area.
983 static inline void
984 ImageList_InternalDragDraw (HDC hdc, INT x, INT y)
986 IMAGELISTDRAWPARAMS imldp;
988 ZeroMemory (&imldp, sizeof(imldp));
989 imldp.cbSize = sizeof(imldp);
990 imldp.himl = InternalDrag.himl;
991 imldp.i = 0;
992 imldp.hdcDst = hdc,
993 imldp.x = x;
994 imldp.y = y;
995 imldp.rgbBk = CLR_DEFAULT;
996 imldp.rgbFg = CLR_DEFAULT;
997 imldp.fStyle = ILD_NORMAL;
998 imldp.fState = ILS_ALPHA;
999 imldp.Frame = 192;
1000 ImageList_DrawIndirect (&imldp);
1003 /*************************************************************************
1004 * ImageList_DragMove [COMCTL32.@]
1006 * Moves the drag image.
1008 * PARAMS
1009 * x [I] X position of the drag image.
1010 * y [I] Y position of the drag image.
1012 * RETURNS
1013 * Success: TRUE
1014 * Failure: FALSE
1016 * NOTES
1017 * The position of the drag image is relative to the window, not
1018 * the client area.
1021 BOOL WINAPI
1022 ImageList_DragMove (INT x, INT y)
1024 TRACE("(x=%d y=%d)\n", x, y);
1026 if (!is_valid(InternalDrag.himl))
1027 return FALSE;
1029 /* draw/update the drag image */
1030 if (InternalDrag.bShow) {
1031 HDC hdcDrag;
1032 HDC hdcOffScreen;
1033 HDC hdcBg;
1034 HBITMAP hbmOffScreen;
1035 INT origNewX, origNewY;
1036 INT origOldX, origOldY;
1037 INT origRegX, origRegY;
1038 INT sizeRegX, sizeRegY;
1041 /* calculate the update region */
1042 origNewX = x - InternalDrag.dxHotspot;
1043 origNewY = y - InternalDrag.dyHotspot;
1044 origOldX = InternalDrag.x - InternalDrag.dxHotspot;
1045 origOldY = InternalDrag.y - InternalDrag.dyHotspot;
1046 origRegX = min(origNewX, origOldX);
1047 origRegY = min(origNewY, origOldY);
1048 sizeRegX = InternalDrag.himl->cx + abs(x - InternalDrag.x);
1049 sizeRegY = InternalDrag.himl->cy + abs(y - InternalDrag.y);
1051 hdcDrag = GetDCEx(InternalDrag.hwnd, 0,
1052 DCX_WINDOW | DCX_CACHE | DCX_LOCKWINDOWUPDATE);
1053 hdcOffScreen = CreateCompatibleDC(hdcDrag);
1054 hdcBg = CreateCompatibleDC(hdcDrag);
1056 hbmOffScreen = CreateCompatibleBitmap(hdcDrag, sizeRegX, sizeRegY);
1057 SelectObject(hdcOffScreen, hbmOffScreen);
1058 SelectObject(hdcBg, InternalDrag.hbmBg);
1060 /* get the actual background of the update region */
1061 BitBlt(hdcOffScreen, 0, 0, sizeRegX, sizeRegY, hdcDrag,
1062 origRegX, origRegY, SRCCOPY);
1063 /* erase the old image */
1064 BitBlt(hdcOffScreen, origOldX - origRegX, origOldY - origRegY,
1065 InternalDrag.himl->cx, InternalDrag.himl->cy, hdcBg, 0, 0,
1066 SRCCOPY);
1067 /* save the background */
1068 BitBlt(hdcBg, 0, 0, InternalDrag.himl->cx, InternalDrag.himl->cy,
1069 hdcOffScreen, origNewX - origRegX, origNewY - origRegY, SRCCOPY);
1070 /* draw the image */
1071 ImageList_InternalDragDraw(hdcOffScreen, origNewX - origRegX,
1072 origNewY - origRegY);
1073 /* draw the update region to the screen */
1074 BitBlt(hdcDrag, origRegX, origRegY, sizeRegX, sizeRegY,
1075 hdcOffScreen, 0, 0, SRCCOPY);
1077 DeleteDC(hdcBg);
1078 DeleteDC(hdcOffScreen);
1079 DeleteObject(hbmOffScreen);
1080 ReleaseDC(InternalDrag.hwnd, hdcDrag);
1083 /* update the image position */
1084 InternalDrag.x = x;
1085 InternalDrag.y = y;
1087 return TRUE;
1091 /*************************************************************************
1092 * ImageList_DragShowNolock [COMCTL32.@]
1094 * Shows or hides the drag image.
1096 * PARAMS
1097 * bShow [I] TRUE shows the drag image, FALSE hides it.
1099 * RETURNS
1100 * Success: TRUE
1101 * Failure: FALSE
1104 BOOL WINAPI
1105 ImageList_DragShowNolock (BOOL bShow)
1107 HDC hdcDrag;
1108 HDC hdcBg;
1109 INT x, y;
1111 if (!is_valid(InternalDrag.himl))
1112 return FALSE;
1114 TRACE("bShow=0x%X!\n", bShow);
1116 /* DragImage is already visible/hidden */
1117 if ((InternalDrag.bShow && bShow) || (!InternalDrag.bShow && !bShow)) {
1118 return FALSE;
1121 /* position of the origin of the DragImage */
1122 x = InternalDrag.x - InternalDrag.dxHotspot;
1123 y = InternalDrag.y - InternalDrag.dyHotspot;
1125 hdcDrag = GetDCEx (InternalDrag.hwnd, 0,
1126 DCX_WINDOW | DCX_CACHE | DCX_LOCKWINDOWUPDATE);
1127 if (!hdcDrag) {
1128 return FALSE;
1131 hdcBg = CreateCompatibleDC(hdcDrag);
1132 if (!InternalDrag.hbmBg) {
1133 InternalDrag.hbmBg = CreateCompatibleBitmap(hdcDrag,
1134 InternalDrag.himl->cx, InternalDrag.himl->cy);
1136 SelectObject(hdcBg, InternalDrag.hbmBg);
1138 if (bShow) {
1139 /* save the background */
1140 BitBlt(hdcBg, 0, 0, InternalDrag.himl->cx, InternalDrag.himl->cy,
1141 hdcDrag, x, y, SRCCOPY);
1142 /* show the image */
1143 ImageList_InternalDragDraw(hdcDrag, x, y);
1144 } else {
1145 /* hide the image */
1146 BitBlt(hdcDrag, x, y, InternalDrag.himl->cx, InternalDrag.himl->cy,
1147 hdcBg, 0, 0, SRCCOPY);
1150 InternalDrag.bShow = !InternalDrag.bShow;
1152 DeleteDC(hdcBg);
1153 ReleaseDC (InternalDrag.hwnd, hdcDrag);
1154 return TRUE;
1158 /*************************************************************************
1159 * ImageList_Draw [COMCTL32.@]
1161 * Draws an image.
1163 * PARAMS
1164 * himl [I] handle to image list
1165 * i [I] image index
1166 * hdc [I] handle to device context
1167 * x [I] x position
1168 * y [I] y position
1169 * fStyle [I] drawing flags
1171 * RETURNS
1172 * Success: TRUE
1173 * Failure: FALSE
1175 * SEE
1176 * ImageList_DrawEx.
1179 BOOL WINAPI
1180 ImageList_Draw (HIMAGELIST himl, INT i, HDC hdc, INT x, INT y, UINT fStyle)
1182 return ImageList_DrawEx (himl, i, hdc, x, y, 0, 0,
1183 CLR_DEFAULT, CLR_DEFAULT, fStyle);
1187 /*************************************************************************
1188 * ImageList_DrawEx [COMCTL32.@]
1190 * Draws an image and allows using extended drawing features.
1192 * PARAMS
1193 * himl [I] handle to image list
1194 * i [I] image index
1195 * hdc [I] handle to device context
1196 * x [I] X position
1197 * y [I] Y position
1198 * dx [I] X offset
1199 * dy [I] Y offset
1200 * rgbBk [I] background color
1201 * rgbFg [I] foreground color
1202 * fStyle [I] drawing flags
1204 * RETURNS
1205 * Success: TRUE
1206 * Failure: FALSE
1208 * NOTES
1209 * Calls ImageList_DrawIndirect.
1211 * SEE
1212 * ImageList_DrawIndirect.
1215 BOOL WINAPI
1216 ImageList_DrawEx (HIMAGELIST himl, INT i, HDC hdc, INT x, INT y,
1217 INT dx, INT dy, COLORREF rgbBk, COLORREF rgbFg,
1218 UINT fStyle)
1220 IMAGELISTDRAWPARAMS imldp;
1222 ZeroMemory (&imldp, sizeof(imldp));
1223 imldp.cbSize = sizeof(imldp);
1224 imldp.himl = himl;
1225 imldp.i = i;
1226 imldp.hdcDst = hdc,
1227 imldp.x = x;
1228 imldp.y = y;
1229 imldp.cx = dx;
1230 imldp.cy = dy;
1231 imldp.rgbBk = rgbBk;
1232 imldp.rgbFg = rgbFg;
1233 imldp.fStyle = fStyle;
1235 return ImageList_DrawIndirect (&imldp);
1239 static BOOL alpha_blend_image( HIMAGELIST himl, HDC dest_dc, int dest_x, int dest_y,
1240 int src_x, int src_y, int cx, int cy, BLENDFUNCTION func,
1241 UINT style, COLORREF blend_col )
1243 BOOL ret = FALSE;
1244 HDC hdc;
1245 HBITMAP bmp = 0, mask = 0;
1246 BITMAPINFO *info;
1247 void *bits, *mask_bits;
1248 unsigned int *ptr;
1249 int i, j;
1251 if (!(hdc = CreateCompatibleDC( 0 ))) return FALSE;
1252 if (!(info = HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET( BITMAPINFO, bmiColors[256] )))) goto done;
1253 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1254 info->bmiHeader.biWidth = cx;
1255 info->bmiHeader.biHeight = cy;
1256 info->bmiHeader.biPlanes = 1;
1257 info->bmiHeader.biBitCount = 32;
1258 info->bmiHeader.biCompression = BI_RGB;
1259 info->bmiHeader.biSizeImage = cx * cy * 4;
1260 info->bmiHeader.biXPelsPerMeter = 0;
1261 info->bmiHeader.biYPelsPerMeter = 0;
1262 info->bmiHeader.biClrUsed = 0;
1263 info->bmiHeader.biClrImportant = 0;
1264 if (!(bmp = CreateDIBSection( himl->hdcImage, info, DIB_RGB_COLORS, &bits, 0, 0 ))) goto done;
1265 SelectObject( hdc, bmp );
1266 BitBlt( hdc, 0, 0, cx, cy, himl->hdcImage, src_x, src_y, SRCCOPY );
1268 if (blend_col != CLR_NONE)
1270 BYTE r = GetRValue( blend_col );
1271 BYTE g = GetGValue( blend_col );
1272 BYTE b = GetBValue( blend_col );
1274 if (style & ILD_BLEND25)
1276 for (i = 0, ptr = bits; i < cx * cy; i++, ptr++)
1277 *ptr = ((*ptr & 0xff000000) |
1278 ((((*ptr & 0x00ff0000) * 3 + (r << 16)) / 4) & 0x00ff0000) |
1279 ((((*ptr & 0x0000ff00) * 3 + (g << 8)) / 4) & 0x0000ff00) |
1280 ((((*ptr & 0x000000ff) * 3 + (b << 0)) / 4) & 0x000000ff));
1282 else if (style & ILD_BLEND50)
1284 for (i = 0, ptr = bits; i < cx * cy; i++, ptr++)
1285 *ptr = ((*ptr & 0xff000000) |
1286 ((((*ptr & 0x00ff0000) + (r << 16)) / 2) & 0x00ff0000) |
1287 ((((*ptr & 0x0000ff00) + (g << 8)) / 2) & 0x0000ff00) |
1288 ((((*ptr & 0x000000ff) + (b << 0)) / 2) & 0x000000ff));
1292 if (himl->has_alpha) /* we already have an alpha channel in this case */
1294 /* pre-multiply by the alpha channel */
1295 for (i = 0, ptr = bits; i < cx * cy; i++, ptr++)
1297 DWORD alpha = *ptr >> 24;
1298 *ptr = ((*ptr & 0xff000000) |
1299 (((*ptr & 0x00ff0000) * alpha / 255) & 0x00ff0000) |
1300 (((*ptr & 0x0000ff00) * alpha / 255) & 0x0000ff00) |
1301 (((*ptr & 0x000000ff) * alpha / 255)));
1304 else if (himl->hbmMask)
1306 unsigned int width_bytes = (cx + 31) / 32 * 4;
1307 /* generate alpha channel from the mask */
1308 info->bmiHeader.biBitCount = 1;
1309 info->bmiHeader.biSizeImage = width_bytes * cy;
1310 info->bmiColors[0].rgbRed = 0;
1311 info->bmiColors[0].rgbGreen = 0;
1312 info->bmiColors[0].rgbBlue = 0;
1313 info->bmiColors[0].rgbReserved = 0;
1314 info->bmiColors[1].rgbRed = 0xff;
1315 info->bmiColors[1].rgbGreen = 0xff;
1316 info->bmiColors[1].rgbBlue = 0xff;
1317 info->bmiColors[1].rgbReserved = 0;
1318 if (!(mask = CreateDIBSection( himl->hdcMask, info, DIB_RGB_COLORS, &mask_bits, 0, 0 )))
1319 goto done;
1320 SelectObject( hdc, mask );
1321 BitBlt( hdc, 0, 0, cx, cy, himl->hdcMask, src_x, src_y, SRCCOPY );
1322 SelectObject( hdc, bmp );
1323 for (i = 0, ptr = bits; i < cy; i++)
1324 for (j = 0; j < cx; j++, ptr++)
1325 if ((((BYTE *)mask_bits)[i * width_bytes + j / 8] << (j % 8)) & 0x80) *ptr = 0;
1326 else *ptr |= 0xff000000;
1329 ret = GdiAlphaBlend( dest_dc, dest_x, dest_y, cx, cy, hdc, 0, 0, cx, cy, func );
1331 done:
1332 DeleteDC( hdc );
1333 if (bmp) DeleteObject( bmp );
1334 if (mask) DeleteObject( mask );
1335 HeapFree( GetProcessHeap(), 0, info );
1336 return ret;
1339 /*************************************************************************
1340 * ImageList_DrawIndirect [COMCTL32.@]
1342 * Draws an image using various parameters specified in pimldp.
1344 * PARAMS
1345 * pimldp [I] pointer to IMAGELISTDRAWPARAMS structure.
1347 * RETURNS
1348 * Success: TRUE
1349 * Failure: FALSE
1352 BOOL WINAPI
1353 ImageList_DrawIndirect (IMAGELISTDRAWPARAMS *pimldp)
1355 INT cx, cy, nOvlIdx;
1356 DWORD fState, dwRop;
1357 UINT fStyle;
1358 COLORREF oldImageBk, oldImageFg;
1359 HDC hImageDC, hImageListDC, hMaskListDC;
1360 HBITMAP hImageBmp, hOldImageBmp, hBlendMaskBmp;
1361 BOOL bIsTransparent, bBlend, bResult = FALSE, bMask;
1362 HIMAGELIST himl;
1363 HBRUSH hOldBrush;
1364 POINT pt;
1365 BOOL has_alpha;
1367 if (!pimldp || !(himl = pimldp->himl)) return FALSE;
1368 if (!is_valid(himl)) return FALSE;
1369 if ((pimldp->i < 0) || (pimldp->i >= himl->cCurImage)) return FALSE;
1371 imagelist_point_from_index( himl, pimldp->i, &pt );
1372 pt.x += pimldp->xBitmap;
1373 pt.y += pimldp->yBitmap;
1375 fState = pimldp->cbSize < sizeof(IMAGELISTDRAWPARAMS) ? ILS_NORMAL : pimldp->fState;
1376 fStyle = pimldp->fStyle & ~ILD_OVERLAYMASK;
1377 cx = (pimldp->cx == 0) ? himl->cx : pimldp->cx;
1378 cy = (pimldp->cy == 0) ? himl->cy : pimldp->cy;
1380 bIsTransparent = (fStyle & ILD_TRANSPARENT);
1381 if( pimldp->rgbBk == CLR_NONE )
1382 bIsTransparent = TRUE;
1383 if( ( pimldp->rgbBk == CLR_DEFAULT ) && ( himl->clrBk == CLR_NONE ) )
1384 bIsTransparent = TRUE;
1385 bMask = (himl->flags & ILC_MASK) && (fStyle & ILD_MASK) ;
1386 bBlend = (fStyle & (ILD_BLEND25 | ILD_BLEND50) ) && !bMask;
1388 TRACE("himl(%p) hbmMask(%p) iImage(%d) x(%d) y(%d) cx(%d) cy(%d)\n",
1389 himl, himl->hbmMask, pimldp->i, pimldp->x, pimldp->y, cx, cy);
1391 /* we will use these DCs to access the images and masks in the ImageList */
1392 hImageListDC = himl->hdcImage;
1393 hMaskListDC = himl->hdcMask;
1395 /* these will accumulate the image and mask for the image we're drawing */
1396 hImageDC = CreateCompatibleDC( pimldp->hdcDst );
1397 hImageBmp = CreateCompatibleBitmap( pimldp->hdcDst, cx, cy );
1398 hBlendMaskBmp = bBlend ? CreateBitmap(cx, cy, 1, 1, NULL) : 0;
1400 /* Create a compatible DC. */
1401 if (!hImageListDC || !hImageDC || !hImageBmp ||
1402 (bBlend && !hBlendMaskBmp) || (himl->hbmMask && !hMaskListDC))
1403 goto cleanup;
1405 hOldImageBmp = SelectObject(hImageDC, hImageBmp);
1408 * To obtain a transparent look, background color should be set
1409 * to white and foreground color to black when blitting the
1410 * monochrome mask.
1412 oldImageFg = SetTextColor( hImageDC, RGB( 0, 0, 0 ) );
1413 oldImageBk = SetBkColor( hImageDC, RGB( 0xff, 0xff, 0xff ) );
1415 has_alpha = (himl->has_alpha && himl->has_alpha[pimldp->i]);
1416 if (!bMask && (has_alpha || (fState & ILS_ALPHA)))
1418 COLORREF colour, blend_col = CLR_NONE;
1419 BLENDFUNCTION func;
1421 if (bBlend)
1423 blend_col = pimldp->rgbFg;
1424 if (blend_col == CLR_DEFAULT) blend_col = GetSysColor( COLOR_HIGHLIGHT );
1425 else if (blend_col == CLR_NONE) blend_col = GetTextColor( pimldp->hdcDst );
1428 func.BlendOp = AC_SRC_OVER;
1429 func.BlendFlags = 0;
1430 func.SourceConstantAlpha = (fState & ILS_ALPHA) ? pimldp->Frame : 255;
1431 func.AlphaFormat = AC_SRC_ALPHA;
1433 if (bIsTransparent)
1435 bResult = alpha_blend_image( himl, pimldp->hdcDst, pimldp->x, pimldp->y,
1436 pt.x, pt.y, cx, cy, func, fStyle, blend_col );
1437 goto end;
1439 colour = pimldp->rgbBk;
1440 if (colour == CLR_DEFAULT) colour = himl->clrBk;
1441 if (colour == CLR_NONE) colour = GetBkColor( pimldp->hdcDst );
1443 hOldBrush = SelectObject (hImageDC, CreateSolidBrush (colour));
1444 PatBlt( hImageDC, 0, 0, cx, cy, PATCOPY );
1445 alpha_blend_image( himl, hImageDC, 0, 0, pt.x, pt.y, cx, cy, func, fStyle, blend_col );
1446 DeleteObject (SelectObject (hImageDC, hOldBrush));
1447 bResult = BitBlt( pimldp->hdcDst, pimldp->x, pimldp->y, cx, cy, hImageDC, 0, 0, SRCCOPY );
1448 goto end;
1452 * Draw the initial image
1454 if( bMask ) {
1455 if (himl->hbmMask) {
1456 hOldBrush = SelectObject (hImageDC, CreateSolidBrush (GetTextColor(pimldp->hdcDst)));
1457 PatBlt( hImageDC, 0, 0, cx, cy, PATCOPY );
1458 BitBlt(hImageDC, 0, 0, cx, cy, hMaskListDC, pt.x, pt.y, SRCPAINT);
1459 DeleteObject (SelectObject (hImageDC, hOldBrush));
1460 if( bIsTransparent )
1462 BitBlt ( pimldp->hdcDst, pimldp->x, pimldp->y, cx, cy, hImageDC, 0, 0, SRCAND);
1463 bResult = TRUE;
1464 goto end;
1466 } else {
1467 hOldBrush = SelectObject (hImageDC, GetStockObject(BLACK_BRUSH));
1468 PatBlt( hImageDC, 0, 0, cx, cy, PATCOPY);
1469 SelectObject(hImageDC, hOldBrush);
1471 } else {
1472 /* blend the image with the needed solid background */
1473 COLORREF colour = RGB(0,0,0);
1475 if( !bIsTransparent )
1477 colour = pimldp->rgbBk;
1478 if( colour == CLR_DEFAULT )
1479 colour = himl->clrBk;
1480 if( colour == CLR_NONE )
1481 colour = GetBkColor(pimldp->hdcDst);
1484 hOldBrush = SelectObject (hImageDC, CreateSolidBrush (colour));
1485 PatBlt( hImageDC, 0, 0, cx, cy, PATCOPY );
1486 if (himl->hbmMask)
1488 BitBlt( hImageDC, 0, 0, cx, cy, hMaskListDC, pt.x, pt.y, SRCAND );
1489 BitBlt( hImageDC, 0, 0, cx, cy, hImageListDC, pt.x, pt.y, SRCPAINT );
1491 else
1492 BitBlt( hImageDC, 0, 0, cx, cy, hImageListDC, pt.x, pt.y, SRCCOPY);
1493 DeleteObject (SelectObject (hImageDC, hOldBrush));
1496 /* Time for blending, if required */
1497 if (bBlend) {
1498 HBRUSH hBlendBrush;
1499 COLORREF clrBlend = pimldp->rgbFg;
1500 HDC hBlendMaskDC = hImageListDC;
1501 HBITMAP hOldBitmap;
1503 /* Create the blend Mask */
1504 hOldBitmap = SelectObject(hBlendMaskDC, hBlendMaskBmp);
1505 hBlendBrush = fStyle & ILD_BLEND50 ? himl->hbrBlend50 : himl->hbrBlend25;
1506 hOldBrush = SelectObject(hBlendMaskDC, hBlendBrush);
1507 PatBlt(hBlendMaskDC, 0, 0, cx, cy, PATCOPY);
1508 SelectObject(hBlendMaskDC, hOldBrush);
1510 /* Modify the blend mask if an Image Mask exist */
1511 if(himl->hbmMask) {
1512 BitBlt(hBlendMaskDC, 0, 0, cx, cy, hMaskListDC, pt.x, pt.y, 0x220326); /* NOTSRCAND */
1513 BitBlt(hBlendMaskDC, 0, 0, cx, cy, hBlendMaskDC, 0, 0, NOTSRCCOPY);
1516 /* now apply blend to the current image given the BlendMask */
1517 if (clrBlend == CLR_DEFAULT) clrBlend = GetSysColor (COLOR_HIGHLIGHT);
1518 else if (clrBlend == CLR_NONE) clrBlend = GetTextColor (pimldp->hdcDst);
1519 hOldBrush = SelectObject (hImageDC, CreateSolidBrush(clrBlend));
1520 BitBlt (hImageDC, 0, 0, cx, cy, hBlendMaskDC, 0, 0, 0xB8074A); /* PSDPxax */
1521 DeleteObject(SelectObject(hImageDC, hOldBrush));
1522 SelectObject(hBlendMaskDC, hOldBitmap);
1525 /* Now do the overlay image, if any */
1526 nOvlIdx = (pimldp->fStyle & ILD_OVERLAYMASK) >> 8;
1527 if ( (nOvlIdx >= 1) && (nOvlIdx <= MAX_OVERLAYIMAGE)) {
1528 nOvlIdx = himl->nOvlIdx[nOvlIdx - 1];
1529 if ((nOvlIdx >= 0) && (nOvlIdx < himl->cCurImage)) {
1530 POINT ptOvl;
1531 imagelist_point_from_index( himl, nOvlIdx, &ptOvl );
1532 ptOvl.x += pimldp->xBitmap;
1533 if (himl->hbmMask && !(fStyle & ILD_IMAGE))
1534 BitBlt (hImageDC, 0, 0, cx, cy, hMaskListDC, ptOvl.x, ptOvl.y, SRCAND);
1535 BitBlt (hImageDC, 0, 0, cx, cy, hImageListDC, ptOvl.x, ptOvl.y, SRCPAINT);
1539 if (fState & ILS_SATURATE) FIXME("ILS_SATURATE: unimplemented!\n");
1540 if (fState & ILS_GLOW) FIXME("ILS_GLOW: unimplemented!\n");
1541 if (fState & ILS_SHADOW) FIXME("ILS_SHADOW: unimplemented!\n");
1543 if (fStyle & ILD_PRESERVEALPHA) FIXME("ILD_PRESERVEALPHA: unimplemented!\n");
1544 if (fStyle & ILD_SCALE) FIXME("ILD_SCALE: unimplemented!\n");
1545 if (fStyle & ILD_DPISCALE) FIXME("ILD_DPISCALE: unimplemented!\n");
1547 /* now copy the image to the screen */
1548 dwRop = SRCCOPY;
1549 if (himl->hbmMask && bIsTransparent ) {
1550 COLORREF oldDstFg = SetTextColor(pimldp->hdcDst, RGB( 0, 0, 0 ) );
1551 COLORREF oldDstBk = SetBkColor(pimldp->hdcDst, RGB( 0xff, 0xff, 0xff ));
1552 BitBlt (pimldp->hdcDst, pimldp->x, pimldp->y, cx, cy, hMaskListDC, pt.x, pt.y, SRCAND);
1553 SetBkColor(pimldp->hdcDst, oldDstBk);
1554 SetTextColor(pimldp->hdcDst, oldDstFg);
1555 dwRop = SRCPAINT;
1557 if (fStyle & ILD_ROP) dwRop = pimldp->dwRop;
1558 BitBlt (pimldp->hdcDst, pimldp->x, pimldp->y, cx, cy, hImageDC, 0, 0, dwRop);
1560 bResult = TRUE;
1561 end:
1562 /* cleanup the mess */
1563 SetBkColor(hImageDC, oldImageBk);
1564 SetTextColor(hImageDC, oldImageFg);
1565 SelectObject(hImageDC, hOldImageBmp);
1566 cleanup:
1567 DeleteObject(hBlendMaskBmp);
1568 DeleteObject(hImageBmp);
1569 DeleteDC(hImageDC);
1571 return bResult;
1575 /*************************************************************************
1576 * ImageList_Duplicate [COMCTL32.@]
1578 * Duplicates an image list.
1580 * PARAMS
1581 * himlSrc [I] source image list handle
1583 * RETURNS
1584 * Success: Handle of duplicated image list.
1585 * Failure: NULL
1588 HIMAGELIST WINAPI
1589 ImageList_Duplicate (HIMAGELIST himlSrc)
1591 HIMAGELIST himlDst;
1593 if (!is_valid(himlSrc)) {
1594 ERR("Invalid image list handle!\n");
1595 return NULL;
1598 himlDst = ImageList_Create (himlSrc->cx, himlSrc->cy, himlSrc->flags,
1599 himlSrc->cCurImage, himlSrc->cGrow);
1601 if (himlDst)
1603 SIZE sz;
1605 imagelist_get_bitmap_size(himlSrc, himlSrc->cCurImage, &sz);
1606 BitBlt (himlDst->hdcImage, 0, 0, sz.cx, sz.cy,
1607 himlSrc->hdcImage, 0, 0, SRCCOPY);
1609 if (himlDst->hbmMask)
1610 BitBlt (himlDst->hdcMask, 0, 0, sz.cx, sz.cy,
1611 himlSrc->hdcMask, 0, 0, SRCCOPY);
1613 himlDst->cCurImage = himlSrc->cCurImage;
1614 if (himlSrc->has_alpha && himlDst->has_alpha)
1615 memcpy( himlDst->has_alpha, himlSrc->has_alpha, himlDst->cCurImage );
1617 return himlDst;
1621 /*************************************************************************
1622 * ImageList_EndDrag [COMCTL32.@]
1624 * Finishes a drag operation.
1626 * PARAMS
1627 * no Parameters
1629 * RETURNS
1630 * Success: TRUE
1631 * Failure: FALSE
1634 VOID WINAPI
1635 ImageList_EndDrag (void)
1637 /* cleanup the InternalDrag struct */
1638 InternalDrag.hwnd = 0;
1639 if (InternalDrag.himl != InternalDrag.himlNoCursor)
1640 ImageList_Destroy (InternalDrag.himlNoCursor);
1641 ImageList_Destroy (InternalDrag.himl);
1642 InternalDrag.himlNoCursor = InternalDrag.himl = 0;
1643 InternalDrag.x= 0;
1644 InternalDrag.y= 0;
1645 InternalDrag.dxHotspot = 0;
1646 InternalDrag.dyHotspot = 0;
1647 InternalDrag.bShow = FALSE;
1648 DeleteObject(InternalDrag.hbmBg);
1649 InternalDrag.hbmBg = 0;
1653 /*************************************************************************
1654 * ImageList_GetBkColor [COMCTL32.@]
1656 * Returns the background color of an image list.
1658 * PARAMS
1659 * himl [I] Image list handle.
1661 * RETURNS
1662 * Success: background color
1663 * Failure: CLR_NONE
1666 COLORREF WINAPI
1667 ImageList_GetBkColor (HIMAGELIST himl)
1669 return himl ? himl->clrBk : CLR_NONE;
1673 /*************************************************************************
1674 * ImageList_GetDragImage [COMCTL32.@]
1676 * Returns the handle to the internal drag image list.
1678 * PARAMS
1679 * ppt [O] Pointer to the drag position. Can be NULL.
1680 * pptHotspot [O] Pointer to the position of the hot spot. Can be NULL.
1682 * RETURNS
1683 * Success: Handle of the drag image list.
1684 * Failure: NULL.
1687 HIMAGELIST WINAPI
1688 ImageList_GetDragImage (POINT *ppt, POINT *pptHotspot)
1690 if (is_valid(InternalDrag.himl)) {
1691 if (ppt) {
1692 ppt->x = InternalDrag.x;
1693 ppt->y = InternalDrag.y;
1695 if (pptHotspot) {
1696 pptHotspot->x = InternalDrag.dxHotspot;
1697 pptHotspot->y = InternalDrag.dyHotspot;
1699 return (InternalDrag.himl);
1702 return NULL;
1706 /*************************************************************************
1707 * ImageList_GetFlags [COMCTL32.@]
1709 * Gets the flags of the specified image list.
1711 * PARAMS
1712 * himl [I] Handle to image list
1714 * RETURNS
1715 * Image list flags.
1717 * BUGS
1718 * Stub.
1721 DWORD WINAPI
1722 ImageList_GetFlags(HIMAGELIST himl)
1724 TRACE("%p\n", himl);
1726 return is_valid(himl) ? himl->flags : 0;
1730 /*************************************************************************
1731 * ImageList_GetIcon [COMCTL32.@]
1733 * Creates an icon from a masked image of an image list.
1735 * PARAMS
1736 * himl [I] handle to image list
1737 * i [I] image index
1738 * flags [I] drawing style flags
1740 * RETURNS
1741 * Success: icon handle
1742 * Failure: NULL
1745 HICON WINAPI
1746 ImageList_GetIcon (HIMAGELIST himl, INT i, UINT fStyle)
1748 ICONINFO ii;
1749 HICON hIcon;
1750 HBITMAP hOldDstBitmap;
1751 HDC hdcDst;
1752 POINT pt;
1754 TRACE("%p %d %d\n", himl, i, fStyle);
1755 if (!is_valid(himl) || (i < 0) || (i >= himl->cCurImage)) return NULL;
1757 ii.fIcon = TRUE;
1758 ii.xHotspot = 0;
1759 ii.yHotspot = 0;
1761 /* create colour bitmap */
1762 hdcDst = GetDC(0);
1763 ii.hbmColor = CreateCompatibleBitmap(hdcDst, himl->cx, himl->cy);
1764 ReleaseDC(0, hdcDst);
1766 hdcDst = CreateCompatibleDC(0);
1768 imagelist_point_from_index( himl, i, &pt );
1770 /* draw mask*/
1771 ii.hbmMask = CreateBitmap (himl->cx, himl->cy, 1, 1, NULL);
1772 hOldDstBitmap = SelectObject (hdcDst, ii.hbmMask);
1773 if (himl->hbmMask) {
1774 BitBlt (hdcDst, 0, 0, himl->cx, himl->cy,
1775 himl->hdcMask, pt.x, pt.y, SRCCOPY);
1777 else
1778 PatBlt (hdcDst, 0, 0, himl->cx, himl->cy, BLACKNESS);
1780 /* draw image*/
1781 SelectObject (hdcDst, ii.hbmColor);
1782 BitBlt (hdcDst, 0, 0, himl->cx, himl->cy,
1783 himl->hdcImage, pt.x, pt.y, SRCCOPY);
1786 * CreateIconIndirect requires us to deselect the bitmaps from
1787 * the DCs before calling
1789 SelectObject(hdcDst, hOldDstBitmap);
1791 hIcon = CreateIconIndirect (&ii);
1793 DeleteObject (ii.hbmMask);
1794 DeleteObject (ii.hbmColor);
1795 DeleteDC (hdcDst);
1797 return hIcon;
1801 /*************************************************************************
1802 * ImageList_GetIconSize [COMCTL32.@]
1804 * Retrieves the size of an image in an image list.
1806 * PARAMS
1807 * himl [I] handle to image list
1808 * cx [O] pointer to the image width.
1809 * cy [O] pointer to the image height.
1811 * RETURNS
1812 * Success: TRUE
1813 * Failure: FALSE
1815 * NOTES
1816 * All images in an image list have the same size.
1819 BOOL WINAPI
1820 ImageList_GetIconSize (HIMAGELIST himl, INT *cx, INT *cy)
1822 if (!is_valid(himl) || !cx || !cy)
1823 return FALSE;
1824 if ((himl->cx <= 0) || (himl->cy <= 0))
1825 return FALSE;
1827 *cx = himl->cx;
1828 *cy = himl->cy;
1830 return TRUE;
1834 /*************************************************************************
1835 * ImageList_GetImageCount [COMCTL32.@]
1837 * Returns the number of images in an image list.
1839 * PARAMS
1840 * himl [I] handle to image list
1842 * RETURNS
1843 * Success: Number of images.
1844 * Failure: 0
1847 INT WINAPI
1848 ImageList_GetImageCount (HIMAGELIST himl)
1850 if (!is_valid(himl))
1851 return 0;
1853 return himl->cCurImage;
1857 /*************************************************************************
1858 * ImageList_GetImageInfo [COMCTL32.@]
1860 * Returns information about an image in an image list.
1862 * PARAMS
1863 * himl [I] handle to image list
1864 * i [I] image index
1865 * pImageInfo [O] pointer to the image information
1867 * RETURNS
1868 * Success: TRUE
1869 * Failure: FALSE
1872 BOOL WINAPI
1873 ImageList_GetImageInfo (HIMAGELIST himl, INT i, IMAGEINFO *pImageInfo)
1875 POINT pt;
1877 if (!is_valid(himl) || (pImageInfo == NULL))
1878 return FALSE;
1879 if ((i < 0) || (i >= himl->cCurImage))
1880 return FALSE;
1882 pImageInfo->hbmImage = himl->hbmImage;
1883 pImageInfo->hbmMask = himl->hbmMask;
1885 imagelist_point_from_index( himl, i, &pt );
1886 pImageInfo->rcImage.top = pt.y;
1887 pImageInfo->rcImage.bottom = pt.y + himl->cy;
1888 pImageInfo->rcImage.left = pt.x;
1889 pImageInfo->rcImage.right = pt.x + himl->cx;
1891 return TRUE;
1895 /*************************************************************************
1896 * ImageList_GetImageRect [COMCTL32.@]
1898 * Retrieves the rectangle of the specified image in an image list.
1900 * PARAMS
1901 * himl [I] handle to image list
1902 * i [I] image index
1903 * lpRect [O] pointer to the image rectangle
1905 * RETURNS
1906 * Success: TRUE
1907 * Failure: FALSE
1909 * NOTES
1910 * This is an UNDOCUMENTED function!!!
1913 BOOL WINAPI
1914 ImageList_GetImageRect (HIMAGELIST himl, INT i, LPRECT lpRect)
1916 POINT pt;
1918 if (!is_valid(himl) || (lpRect == NULL))
1919 return FALSE;
1920 if ((i < 0) || (i >= himl->cCurImage))
1921 return FALSE;
1923 imagelist_point_from_index( himl, i, &pt );
1924 lpRect->left = pt.x;
1925 lpRect->top = pt.y;
1926 lpRect->right = pt.x + himl->cx;
1927 lpRect->bottom = pt.y + himl->cy;
1929 return TRUE;
1933 /*************************************************************************
1934 * ImageList_LoadImage [COMCTL32.@]
1935 * ImageList_LoadImageA [COMCTL32.@]
1937 * Creates an image list from a bitmap, icon or cursor.
1939 * See ImageList_LoadImageW.
1942 HIMAGELIST WINAPI
1943 ImageList_LoadImageA (HINSTANCE hi, LPCSTR lpbmp, INT cx, INT cGrow,
1944 COLORREF clrMask, UINT uType, UINT uFlags)
1946 HIMAGELIST himl;
1947 LPWSTR lpbmpW;
1948 DWORD len;
1950 if (IS_INTRESOURCE(lpbmp))
1951 return ImageList_LoadImageW(hi, (LPCWSTR)lpbmp, cx, cGrow, clrMask,
1952 uType, uFlags);
1954 len = MultiByteToWideChar(CP_ACP, 0, lpbmp, -1, NULL, 0);
1955 lpbmpW = Alloc(len * sizeof(WCHAR));
1956 MultiByteToWideChar(CP_ACP, 0, lpbmp, -1, lpbmpW, len);
1958 himl = ImageList_LoadImageW(hi, lpbmpW, cx, cGrow, clrMask, uType, uFlags);
1959 Free (lpbmpW);
1960 return himl;
1964 /*************************************************************************
1965 * ImageList_LoadImageW [COMCTL32.@]
1967 * Creates an image list from a bitmap, icon or cursor.
1969 * PARAMS
1970 * hi [I] instance handle
1971 * lpbmp [I] name or id of the image
1972 * cx [I] width of each image
1973 * cGrow [I] number of images to expand
1974 * clrMask [I] mask color
1975 * uType [I] type of image to load
1976 * uFlags [I] loading flags
1978 * RETURNS
1979 * Success: handle to the loaded image list
1980 * Failure: NULL
1982 * SEE
1983 * LoadImage ()
1986 HIMAGELIST WINAPI
1987 ImageList_LoadImageW (HINSTANCE hi, LPCWSTR lpbmp, INT cx, INT cGrow,
1988 COLORREF clrMask, UINT uType, UINT uFlags)
1990 HIMAGELIST himl = NULL;
1991 HANDLE handle;
1992 INT nImageCount;
1994 handle = LoadImageW (hi, lpbmp, uType, 0, 0, uFlags);
1995 if (!handle) {
1996 WARN("Couldn't load image\n");
1997 return NULL;
2000 if (uType == IMAGE_BITMAP) {
2001 DIBSECTION dib;
2002 UINT color;
2004 if (GetObjectW (handle, sizeof(dib), &dib) == sizeof(BITMAP)) color = ILC_COLOR;
2005 else color = dib.dsBm.bmBitsPixel;
2007 /* To match windows behavior, if cx is set to zero and
2008 the flag DI_DEFAULTSIZE is specified, cx becomes the
2009 system metric value for icons. If the flag is not specified
2010 the function sets the size to the height of the bitmap */
2011 if (cx == 0)
2013 if (uFlags & DI_DEFAULTSIZE)
2014 cx = GetSystemMetrics (SM_CXICON);
2015 else
2016 cx = dib.dsBm.bmHeight;
2019 nImageCount = dib.dsBm.bmWidth / cx;
2021 himl = ImageList_Create (cx, dib.dsBm.bmHeight, ILC_MASK | color, nImageCount, cGrow);
2022 if (!himl) {
2023 DeleteObject (handle);
2024 return NULL;
2026 ImageList_AddMasked (himl, handle, clrMask);
2028 else if ((uType == IMAGE_ICON) || (uType == IMAGE_CURSOR)) {
2029 ICONINFO ii;
2030 BITMAP bmp;
2032 GetIconInfo (handle, &ii);
2033 GetObjectW (ii.hbmColor, sizeof(BITMAP), &bmp);
2034 himl = ImageList_Create (bmp.bmWidth, bmp.bmHeight,
2035 ILC_MASK | ILC_COLOR, 1, cGrow);
2036 if (!himl) {
2037 DeleteObject (ii.hbmColor);
2038 DeleteObject (ii.hbmMask);
2039 DeleteObject (handle);
2040 return NULL;
2042 ImageList_Add (himl, ii.hbmColor, ii.hbmMask);
2043 DeleteObject (ii.hbmColor);
2044 DeleteObject (ii.hbmMask);
2047 DeleteObject (handle);
2049 return himl;
2053 /*************************************************************************
2054 * ImageList_Merge [COMCTL32.@]
2056 * Create an image list containing a merged image from two image lists.
2058 * PARAMS
2059 * himl1 [I] handle to first image list
2060 * i1 [I] first image index
2061 * himl2 [I] handle to second image list
2062 * i2 [I] second image index
2063 * dx [I] X offset of the second image relative to the first.
2064 * dy [I] Y offset of the second image relative to the first.
2066 * RETURNS
2067 * Success: The newly created image list. It contains a single image
2068 * consisting of the second image merged with the first.
2069 * Failure: NULL, if either himl1 or himl2 are invalid.
2071 * NOTES
2072 * - The returned image list should be deleted by the caller using
2073 * ImageList_Destroy() when it is no longer required.
2074 * - If either i1 or i2 are not valid image indices they will be treated
2075 * as a blank image.
2077 HIMAGELIST WINAPI
2078 ImageList_Merge (HIMAGELIST himl1, INT i1, HIMAGELIST himl2, INT i2,
2079 INT dx, INT dy)
2081 HIMAGELIST himlDst = NULL;
2082 INT cxDst, cyDst;
2083 INT xOff1, yOff1, xOff2, yOff2;
2084 POINT pt1, pt2;
2085 INT newFlags;
2087 TRACE("(himl1=%p i1=%d himl2=%p i2=%d dx=%d dy=%d)\n", himl1, i1, himl2,
2088 i2, dx, dy);
2090 if (!is_valid(himl1) || !is_valid(himl2))
2091 return NULL;
2093 if (dx > 0) {
2094 cxDst = max (himl1->cx, dx + himl2->cx);
2095 xOff1 = 0;
2096 xOff2 = dx;
2098 else if (dx < 0) {
2099 cxDst = max (himl2->cx, himl1->cx - dx);
2100 xOff1 = -dx;
2101 xOff2 = 0;
2103 else {
2104 cxDst = max (himl1->cx, himl2->cx);
2105 xOff1 = 0;
2106 xOff2 = 0;
2109 if (dy > 0) {
2110 cyDst = max (himl1->cy, dy + himl2->cy);
2111 yOff1 = 0;
2112 yOff2 = dy;
2114 else if (dy < 0) {
2115 cyDst = max (himl2->cy, himl1->cy - dy);
2116 yOff1 = -dy;
2117 yOff2 = 0;
2119 else {
2120 cyDst = max (himl1->cy, himl2->cy);
2121 yOff1 = 0;
2122 yOff2 = 0;
2125 newFlags = (himl1->flags > himl2->flags ? himl1->flags : himl2->flags) & ILC_COLORDDB;
2126 if (newFlags == ILC_COLORDDB && (himl1->flags & ILC_COLORDDB) == ILC_COLOR16)
2127 newFlags = ILC_COLOR16; /* this is what native (at least v5) does, don't know why */
2128 himlDst = ImageList_Create (cxDst, cyDst, ILC_MASK | newFlags, 1, 1);
2130 if (himlDst)
2132 imagelist_point_from_index( himl1, i1, &pt1 );
2133 imagelist_point_from_index( himl2, i2, &pt2 );
2135 /* copy image */
2136 BitBlt (himlDst->hdcImage, 0, 0, cxDst, cyDst, himl1->hdcImage, 0, 0, BLACKNESS);
2137 if (i1 >= 0 && i1 < himl1->cCurImage)
2138 BitBlt (himlDst->hdcImage, xOff1, yOff1, himl1->cx, himl1->cy, himl1->hdcImage, pt1.x, pt1.y, SRCCOPY);
2139 if (i2 >= 0 && i2 < himl2->cCurImage)
2141 if (himl2->flags & ILC_MASK)
2143 BitBlt (himlDst->hdcImage, xOff2, yOff2, himl2->cx, himl2->cy, himl2->hdcMask , pt2.x, pt2.y, SRCAND);
2144 BitBlt (himlDst->hdcImage, xOff2, yOff2, himl2->cx, himl2->cy, himl2->hdcImage, pt2.x, pt2.y, SRCPAINT);
2146 else
2147 BitBlt (himlDst->hdcImage, xOff2, yOff2, himl2->cx, himl2->cy, himl2->hdcImage, pt2.x, pt2.y, SRCCOPY);
2150 /* copy mask */
2151 BitBlt (himlDst->hdcMask, 0, 0, cxDst, cyDst, himl1->hdcMask, 0, 0, WHITENESS);
2152 if (i1 >= 0 && i1 < himl1->cCurImage)
2153 BitBlt (himlDst->hdcMask, xOff1, yOff1, himl1->cx, himl1->cy, himl1->hdcMask, pt1.x, pt1.y, SRCCOPY);
2154 if (i2 >= 0 && i2 < himl2->cCurImage)
2155 BitBlt (himlDst->hdcMask, xOff2, yOff2, himl2->cx, himl2->cy, himl2->hdcMask, pt2.x, pt2.y, SRCAND);
2157 himlDst->cCurImage = 1;
2160 return himlDst;
2164 /* helper for ImageList_Read, see comments below */
2165 static void *read_bitmap(LPSTREAM pstm, BITMAPINFO *bmi)
2167 BITMAPFILEHEADER bmfh;
2168 int bitsperpixel, palspace;
2169 void *bits;
2171 if (FAILED(IStream_Read ( pstm, &bmfh, sizeof(bmfh), NULL)))
2172 return NULL;
2174 if (bmfh.bfType != (('M'<<8)|'B'))
2175 return NULL;
2177 if (FAILED(IStream_Read ( pstm, &bmi->bmiHeader, sizeof(bmi->bmiHeader), NULL)))
2178 return NULL;
2180 if ((bmi->bmiHeader.biSize != sizeof(bmi->bmiHeader)))
2181 return NULL;
2183 TRACE("width %u, height %u, planes %u, bpp %u\n",
2184 bmi->bmiHeader.biWidth, bmi->bmiHeader.biHeight,
2185 bmi->bmiHeader.biPlanes, bmi->bmiHeader.biBitCount);
2187 bitsperpixel = bmi->bmiHeader.biPlanes * bmi->bmiHeader.biBitCount;
2188 if (bitsperpixel<=8)
2189 palspace = (1<<bitsperpixel)*sizeof(RGBQUAD);
2190 else
2191 palspace = 0;
2193 bmi->bmiHeader.biSizeImage = get_dib_image_size( bmi );
2195 /* read the palette right after the end of the bitmapinfoheader */
2196 if (palspace && FAILED(IStream_Read(pstm, bmi->bmiColors, palspace, NULL)))
2197 return NULL;
2199 bits = Alloc(bmi->bmiHeader.biSizeImage);
2200 if (!bits) return NULL;
2202 if (FAILED(IStream_Read(pstm, bits, bmi->bmiHeader.biSizeImage, NULL)))
2204 Free(bits);
2205 return NULL;
2207 return bits;
2210 /*************************************************************************
2211 * ImageList_Read [COMCTL32.@]
2213 * Reads an image list from a stream.
2215 * PARAMS
2216 * pstm [I] pointer to a stream
2218 * RETURNS
2219 * Success: handle to image list
2220 * Failure: NULL
2222 * The format is like this:
2223 * ILHEAD ilheadstruct;
2225 * for the color image part:
2226 * BITMAPFILEHEADER bmfh;
2227 * BITMAPINFOHEADER bmih;
2228 * only if it has a palette:
2229 * RGBQUAD rgbs[nr_of_paletted_colors];
2231 * BYTE colorbits[imagesize];
2233 * the following only if the ILC_MASK bit is set in ILHEAD.ilFlags:
2234 * BITMAPFILEHEADER bmfh_mask;
2235 * BITMAPINFOHEADER bmih_mask;
2236 * only if it has a palette (it usually does not):
2237 * RGBQUAD rgbs[nr_of_paletted_colors];
2239 * BYTE maskbits[imagesize];
2241 HIMAGELIST WINAPI ImageList_Read (LPSTREAM pstm)
2243 char image_buf[sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * 256];
2244 char mask_buf[sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * 256];
2245 BITMAPINFO *image_info = (BITMAPINFO *)image_buf;
2246 BITMAPINFO *mask_info = (BITMAPINFO *)mask_buf;
2247 void *image_bits, *mask_bits = NULL;
2248 ILHEAD ilHead;
2249 HIMAGELIST himl;
2250 unsigned int i;
2252 TRACE("%p\n", pstm);
2254 if (FAILED(IStream_Read (pstm, &ilHead, sizeof(ILHEAD), NULL)))
2255 return NULL;
2256 if (ilHead.usMagic != (('L' << 8) | 'I'))
2257 return NULL;
2258 if (ilHead.usVersion != 0x101) /* probably version? */
2259 return NULL;
2261 TRACE("cx %u, cy %u, flags 0x%04x, cCurImage %u, cMaxImage %u\n",
2262 ilHead.cx, ilHead.cy, ilHead.flags, ilHead.cCurImage, ilHead.cMaxImage);
2264 himl = ImageList_Create(ilHead.cx, ilHead.cy, ilHead.flags, ilHead.cCurImage, ilHead.cMaxImage);
2265 if (!himl)
2266 return NULL;
2268 if (!(image_bits = read_bitmap(pstm, image_info)))
2270 WARN("failed to read bitmap from stream\n");
2271 return NULL;
2273 if (ilHead.flags & ILC_MASK)
2275 if (!(mask_bits = read_bitmap(pstm, mask_info)))
2277 WARN("failed to read mask bitmap from stream\n");
2278 return NULL;
2281 else mask_info = NULL;
2283 if (himl->has_alpha && image_info->bmiHeader.biBitCount == 32)
2285 DWORD *ptr = image_bits;
2286 BYTE *mask_ptr = mask_bits;
2287 int stride = himl->cy * image_info->bmiHeader.biWidth;
2289 if (image_info->bmiHeader.biHeight > 0) /* bottom-up */
2291 ptr += image_info->bmiHeader.biHeight * image_info->bmiHeader.biWidth - stride;
2292 mask_ptr += (image_info->bmiHeader.biHeight * image_info->bmiHeader.biWidth - stride) / 8;
2293 stride = -stride;
2294 image_info->bmiHeader.biHeight = himl->cy;
2296 else image_info->bmiHeader.biHeight = -himl->cy;
2298 for (i = 0; i < ilHead.cCurImage; i += TILE_COUNT)
2300 add_dib_bits( himl, i, min( ilHead.cCurImage - i, TILE_COUNT ),
2301 himl->cx, himl->cy, image_info, mask_info, ptr, mask_ptr );
2302 ptr += stride;
2303 mask_ptr += stride / 8;
2306 else
2308 StretchDIBits( himl->hdcImage, 0, 0, image_info->bmiHeader.biWidth, image_info->bmiHeader.biHeight,
2309 0, 0, image_info->bmiHeader.biWidth, image_info->bmiHeader.biHeight,
2310 image_bits, image_info, DIB_RGB_COLORS, SRCCOPY);
2311 if (mask_info)
2312 StretchDIBits( himl->hdcMask, 0, 0, mask_info->bmiHeader.biWidth, mask_info->bmiHeader.biHeight,
2313 0, 0, mask_info->bmiHeader.biWidth, mask_info->bmiHeader.biHeight,
2314 mask_bits, mask_info, DIB_RGB_COLORS, SRCCOPY);
2316 Free( image_bits );
2317 Free( mask_bits );
2319 himl->cCurImage = ilHead.cCurImage;
2320 himl->cMaxImage = ilHead.cMaxImage;
2322 ImageList_SetBkColor(himl,ilHead.bkcolor);
2323 for (i=0;i<4;i++)
2324 ImageList_SetOverlayImage(himl,ilHead.ovls[i],i+1);
2325 return himl;
2329 /*************************************************************************
2330 * ImageList_Remove [COMCTL32.@]
2332 * Removes an image from an image list
2334 * PARAMS
2335 * himl [I] image list handle
2336 * i [I] image index
2338 * RETURNS
2339 * Success: TRUE
2340 * Failure: FALSE
2342 * FIXME: as the image list storage test shows, native comctl32 simply shifts
2343 * images without creating a new bitmap.
2345 BOOL WINAPI
2346 ImageList_Remove (HIMAGELIST himl, INT i)
2348 HBITMAP hbmNewImage, hbmNewMask;
2349 HDC hdcBmp;
2350 SIZE sz;
2352 TRACE("(himl=%p i=%d)\n", himl, i);
2354 if (!is_valid(himl)) {
2355 ERR("Invalid image list handle!\n");
2356 return FALSE;
2359 if ((i < -1) || (i >= himl->cCurImage)) {
2360 TRACE("index out of range! %d\n", i);
2361 return FALSE;
2364 if (i == -1) {
2365 INT nCount;
2367 /* remove all */
2368 if (himl->cCurImage == 0) {
2369 /* remove all on empty ImageList is allowed */
2370 TRACE("remove all on empty ImageList!\n");
2371 return TRUE;
2374 himl->cMaxImage = himl->cGrow;
2375 himl->cCurImage = 0;
2376 for (nCount = 0; nCount < MAX_OVERLAYIMAGE; nCount++)
2377 himl->nOvlIdx[nCount] = -1;
2379 hbmNewImage = ImageList_CreateImage(himl->hdcImage, himl, himl->cMaxImage);
2380 SelectObject (himl->hdcImage, hbmNewImage);
2381 DeleteObject (himl->hbmImage);
2382 himl->hbmImage = hbmNewImage;
2384 if (himl->hbmMask) {
2386 imagelist_get_bitmap_size(himl, himl->cMaxImage, &sz);
2387 hbmNewMask = CreateBitmap (sz.cx, sz.cy, 1, 1, NULL);
2388 SelectObject (himl->hdcMask, hbmNewMask);
2389 DeleteObject (himl->hbmMask);
2390 himl->hbmMask = hbmNewMask;
2393 else {
2394 /* delete one image */
2395 TRACE("Remove single image! %d\n", i);
2397 /* create new bitmap(s) */
2398 TRACE(" - Number of images: %d / %d (Old/New)\n",
2399 himl->cCurImage, himl->cCurImage - 1);
2401 hbmNewImage = ImageList_CreateImage(himl->hdcImage, himl, himl->cMaxImage);
2403 imagelist_get_bitmap_size(himl, himl->cMaxImage, &sz );
2404 if (himl->hbmMask)
2405 hbmNewMask = CreateBitmap (sz.cx, sz.cy, 1, 1, NULL);
2406 else
2407 hbmNewMask = 0; /* Just to keep compiler happy! */
2409 hdcBmp = CreateCompatibleDC (0);
2411 /* copy all images and masks prior to the "removed" image */
2412 if (i > 0) {
2413 TRACE("Pre image copy: Copy %d images\n", i);
2415 SelectObject (hdcBmp, hbmNewImage);
2416 imagelist_copy_images( himl, himl->hdcImage, hdcBmp, 0, i, 0 );
2418 if (himl->hbmMask) {
2419 SelectObject (hdcBmp, hbmNewMask);
2420 imagelist_copy_images( himl, himl->hdcMask, hdcBmp, 0, i, 0 );
2424 /* copy all images and masks behind the removed image */
2425 if (i < himl->cCurImage - 1) {
2426 TRACE("Post image copy!\n");
2428 SelectObject (hdcBmp, hbmNewImage);
2429 imagelist_copy_images( himl, himl->hdcImage, hdcBmp, i + 1,
2430 (himl->cCurImage - i), i );
2432 if (himl->hbmMask) {
2433 SelectObject (hdcBmp, hbmNewMask);
2434 imagelist_copy_images( himl, himl->hdcMask, hdcBmp, i + 1,
2435 (himl->cCurImage - i), i );
2439 DeleteDC (hdcBmp);
2441 /* delete old images and insert new ones */
2442 SelectObject (himl->hdcImage, hbmNewImage);
2443 DeleteObject (himl->hbmImage);
2444 himl->hbmImage = hbmNewImage;
2445 if (himl->hbmMask) {
2446 SelectObject (himl->hdcMask, hbmNewMask);
2447 DeleteObject (himl->hbmMask);
2448 himl->hbmMask = hbmNewMask;
2451 himl->cCurImage--;
2454 return TRUE;
2458 /*************************************************************************
2459 * ImageList_Replace [COMCTL32.@]
2461 * Replaces an image in an image list with a new image.
2463 * PARAMS
2464 * himl [I] handle to image list
2465 * i [I] image index
2466 * hbmImage [I] handle to image bitmap
2467 * hbmMask [I] handle to mask bitmap. Can be NULL.
2469 * RETURNS
2470 * Success: TRUE
2471 * Failure: FALSE
2474 BOOL WINAPI
2475 ImageList_Replace (HIMAGELIST himl, INT i, HBITMAP hbmImage,
2476 HBITMAP hbmMask)
2478 HDC hdcImage;
2479 BITMAP bmp;
2480 POINT pt;
2482 TRACE("%p %d %p %p\n", himl, i, hbmImage, hbmMask);
2484 if (!is_valid(himl)) {
2485 ERR("Invalid image list handle!\n");
2486 return FALSE;
2489 if ((i >= himl->cMaxImage) || (i < 0)) {
2490 ERR("Invalid image index!\n");
2491 return FALSE;
2494 if (!GetObjectW(hbmImage, sizeof(BITMAP), &bmp))
2495 return FALSE;
2497 hdcImage = CreateCompatibleDC (0);
2499 /* Replace Image */
2500 SelectObject (hdcImage, hbmImage);
2502 if (add_with_alpha( himl, hdcImage, i, 1, bmp.bmWidth, bmp.bmHeight, hbmImage, hbmMask ))
2503 goto done;
2505 imagelist_point_from_index(himl, i, &pt);
2506 StretchBlt (himl->hdcImage, pt.x, pt.y, himl->cx, himl->cy,
2507 hdcImage, 0, 0, bmp.bmWidth, bmp.bmHeight, SRCCOPY);
2509 if (himl->hbmMask)
2511 HDC hdcTemp;
2512 HBITMAP hOldBitmapTemp;
2514 hdcTemp = CreateCompatibleDC(0);
2515 hOldBitmapTemp = SelectObject(hdcTemp, hbmMask);
2517 StretchBlt (himl->hdcMask, pt.x, pt.y, himl->cx, himl->cy,
2518 hdcTemp, 0, 0, bmp.bmWidth, bmp.bmHeight, SRCCOPY);
2519 SelectObject(hdcTemp, hOldBitmapTemp);
2520 DeleteDC(hdcTemp);
2522 /* Remove the background from the image
2524 BitBlt (himl->hdcImage, pt.x, pt.y, bmp.bmWidth, bmp.bmHeight,
2525 himl->hdcMask, pt.x, pt.y, 0x220326); /* NOTSRCAND */
2528 done:
2529 DeleteDC (hdcImage);
2531 return TRUE;
2535 /*************************************************************************
2536 * ImageList_ReplaceIcon [COMCTL32.@]
2538 * Replaces an image in an image list using an icon.
2540 * PARAMS
2541 * himl [I] handle to image list
2542 * i [I] image index
2543 * hIcon [I] handle to icon
2545 * RETURNS
2546 * Success: index of the replaced image
2547 * Failure: -1
2550 INT WINAPI
2551 ImageList_ReplaceIcon (HIMAGELIST himl, INT nIndex, HICON hIcon)
2553 HICON hBestFitIcon;
2554 ICONINFO ii;
2555 BITMAP bmp;
2556 BOOL ret;
2557 POINT pt;
2559 TRACE("(%p %d %p)\n", himl, nIndex, hIcon);
2561 if (!is_valid(himl)) {
2562 ERR("invalid image list\n");
2563 return -1;
2565 if ((nIndex >= himl->cMaxImage) || (nIndex < -1)) {
2566 ERR("invalid image index %d / %d\n", nIndex, himl->cMaxImage);
2567 return -1;
2570 hBestFitIcon = CopyImage(
2571 hIcon, IMAGE_ICON,
2572 himl->cx, himl->cy,
2573 LR_COPYFROMRESOURCE);
2574 /* the above will fail if the icon wasn't loaded from a resource, so try
2575 * again without LR_COPYFROMRESOURCE flag */
2576 if (!hBestFitIcon)
2577 hBestFitIcon = CopyImage(
2578 hIcon, IMAGE_ICON,
2579 himl->cx, himl->cy,
2581 if (!hBestFitIcon)
2582 return -1;
2584 if (nIndex == -1) {
2585 if (himl->cCurImage + 1 >= himl->cMaxImage)
2586 IMAGELIST_InternalExpandBitmaps(himl, 1);
2588 nIndex = himl->cCurImage;
2589 himl->cCurImage++;
2592 if (himl->has_alpha && GetIconInfo (hBestFitIcon, &ii))
2594 HDC hdcImage = CreateCompatibleDC( 0 );
2595 GetObjectW (ii.hbmMask, sizeof(BITMAP), &bmp);
2597 if (!ii.hbmColor)
2599 UINT height = bmp.bmHeight / 2;
2600 HDC hdcMask = CreateCompatibleDC( 0 );
2601 HBITMAP color = CreateBitmap( bmp.bmWidth, height, 1, 1, NULL );
2602 SelectObject( hdcImage, color );
2603 SelectObject( hdcMask, ii.hbmMask );
2604 BitBlt( hdcImage, 0, 0, bmp.bmWidth, height, hdcMask, 0, height, SRCCOPY );
2605 ret = add_with_alpha( himl, hdcImage, nIndex, 1, bmp.bmWidth, height, color, ii.hbmMask );
2606 DeleteDC( hdcMask );
2607 DeleteObject( color );
2609 else ret = add_with_alpha( himl, hdcImage, nIndex, 1, bmp.bmWidth, bmp.bmHeight,
2610 ii.hbmColor, ii.hbmMask );
2612 DeleteDC( hdcImage );
2613 DeleteObject (ii.hbmMask);
2614 if (ii.hbmColor) DeleteObject (ii.hbmColor);
2615 if (ret) goto done;
2618 imagelist_point_from_index(himl, nIndex, &pt);
2620 if (himl->hbmMask)
2622 DrawIconEx( himl->hdcImage, pt.x, pt.y, hBestFitIcon, himl->cx, himl->cy, 0, 0, DI_IMAGE );
2623 PatBlt( himl->hdcMask, pt.x, pt.y, himl->cx, himl->cy, WHITENESS );
2624 DrawIconEx( himl->hdcMask, pt.x, pt.y, hBestFitIcon, himl->cx, himl->cy, 0, 0, DI_MASK );
2626 else
2628 COLORREF color = himl->clrBk != CLR_NONE ? himl->clrBk : comctl32_color.clrWindow;
2629 HBRUSH brush = CreateSolidBrush( GetNearestColor( himl->hdcImage, color ));
2631 SelectObject( himl->hdcImage, brush );
2632 PatBlt( himl->hdcImage, pt.x, pt.y, himl->cx, himl->cy, PATCOPY );
2633 SelectObject( himl->hdcImage, GetStockObject(BLACK_BRUSH) );
2634 DeleteObject( brush );
2635 DrawIconEx( himl->hdcImage, pt.x, pt.y, hBestFitIcon, himl->cx, himl->cy, 0, 0, DI_NORMAL );
2638 done:
2639 DestroyIcon(hBestFitIcon);
2641 TRACE("Insert index = %d, himl->cCurImage = %d\n", nIndex, himl->cCurImage);
2642 return nIndex;
2646 /*************************************************************************
2647 * ImageList_SetBkColor [COMCTL32.@]
2649 * Sets the background color of an image list.
2651 * PARAMS
2652 * himl [I] handle to image list
2653 * clrBk [I] background color
2655 * RETURNS
2656 * Success: previous background color
2657 * Failure: CLR_NONE
2660 COLORREF WINAPI
2661 ImageList_SetBkColor (HIMAGELIST himl, COLORREF clrBk)
2663 COLORREF clrOldBk;
2665 if (!is_valid(himl))
2666 return CLR_NONE;
2668 clrOldBk = himl->clrBk;
2669 himl->clrBk = clrBk;
2670 return clrOldBk;
2674 /*************************************************************************
2675 * ImageList_SetDragCursorImage [COMCTL32.@]
2677 * Combines the specified image with the current drag image
2679 * PARAMS
2680 * himlDrag [I] handle to drag image list
2681 * iDrag [I] drag image index
2682 * dxHotspot [I] X position of the hot spot
2683 * dyHotspot [I] Y position of the hot spot
2685 * RETURNS
2686 * Success: TRUE
2687 * Failure: FALSE
2689 * NOTES
2690 * - The names dxHotspot, dyHotspot are misleading because they have nothing
2691 * to do with a hotspot but are only the offset of the origin of the new
2692 * image relative to the origin of the old image.
2694 * - When this function is called and the drag image is visible, a
2695 * short flickering occurs but this matches the Win9x behavior. It is
2696 * possible to fix the flickering using code like in ImageList_DragMove.
2699 BOOL WINAPI
2700 ImageList_SetDragCursorImage (HIMAGELIST himlDrag, INT iDrag,
2701 INT dxHotspot, INT dyHotspot)
2703 HIMAGELIST himlTemp;
2704 BOOL visible;
2706 if (!is_valid(InternalDrag.himl) || !is_valid(himlDrag))
2707 return FALSE;
2709 TRACE(" dxH=%d dyH=%d nX=%d nY=%d\n",
2710 dxHotspot, dyHotspot, InternalDrag.dxHotspot, InternalDrag.dyHotspot);
2712 visible = InternalDrag.bShow;
2714 himlTemp = ImageList_Merge (InternalDrag.himlNoCursor, 0, himlDrag, iDrag,
2715 dxHotspot, dyHotspot);
2717 if (visible) {
2718 /* hide the drag image */
2719 ImageList_DragShowNolock(FALSE);
2721 if ((InternalDrag.himl->cx != himlTemp->cx) ||
2722 (InternalDrag.himl->cy != himlTemp->cy)) {
2723 /* the size of the drag image changed, invalidate the buffer */
2724 DeleteObject(InternalDrag.hbmBg);
2725 InternalDrag.hbmBg = 0;
2728 if (InternalDrag.himl != InternalDrag.himlNoCursor)
2729 ImageList_Destroy (InternalDrag.himl);
2730 InternalDrag.himl = himlTemp;
2732 if (visible) {
2733 /* show the drag image */
2734 ImageList_DragShowNolock(TRUE);
2737 return TRUE;
2741 /*************************************************************************
2742 * ImageList_SetFilter [COMCTL32.@]
2744 * Sets a filter (or does something completely different)!!???
2745 * It removes 12 Bytes from the stack (3 Parameters).
2747 * PARAMS
2748 * himl [I] SHOULD be a handle to image list
2749 * i [I] COULD be an index?
2750 * dwFilter [I] ???
2752 * RETURNS
2753 * Success: TRUE ???
2754 * Failure: FALSE ???
2756 * BUGS
2757 * This is an UNDOCUMENTED function!!!!
2758 * empty stub.
2761 BOOL WINAPI
2762 ImageList_SetFilter (HIMAGELIST himl, INT i, DWORD dwFilter)
2764 FIXME("(%p 0x%x 0x%x):empty stub!\n", himl, i, dwFilter);
2766 return FALSE;
2770 /*************************************************************************
2771 * ImageList_SetFlags [COMCTL32.@]
2773 * Sets the image list flags.
2775 * PARAMS
2776 * himl [I] Handle to image list
2777 * flags [I] Flags to set
2779 * RETURNS
2780 * Old flags?
2782 * BUGS
2783 * Stub.
2786 DWORD WINAPI
2787 ImageList_SetFlags(HIMAGELIST himl, DWORD flags)
2789 FIXME("(%p %08x):empty stub\n", himl, flags);
2790 return 0;
2794 /*************************************************************************
2795 * ImageList_SetIconSize [COMCTL32.@]
2797 * Sets the image size of the bitmap and deletes all images.
2799 * PARAMS
2800 * himl [I] handle to image list
2801 * cx [I] image width
2802 * cy [I] image height
2804 * RETURNS
2805 * Success: TRUE
2806 * Failure: FALSE
2809 BOOL WINAPI
2810 ImageList_SetIconSize (HIMAGELIST himl, INT cx, INT cy)
2812 INT nCount;
2813 HBITMAP hbmNew;
2815 if (!is_valid(himl))
2816 return FALSE;
2818 /* remove all images */
2819 himl->cMaxImage = himl->cInitial + 1;
2820 himl->cCurImage = 0;
2821 himl->cx = cx;
2822 himl->cy = cy;
2824 /* initialize overlay mask indices */
2825 for (nCount = 0; nCount < MAX_OVERLAYIMAGE; nCount++)
2826 himl->nOvlIdx[nCount] = -1;
2828 hbmNew = ImageList_CreateImage(himl->hdcImage, himl, himl->cMaxImage);
2829 SelectObject (himl->hdcImage, hbmNew);
2830 DeleteObject (himl->hbmImage);
2831 himl->hbmImage = hbmNew;
2833 if (himl->hbmMask) {
2834 SIZE sz;
2835 imagelist_get_bitmap_size(himl, himl->cMaxImage, &sz);
2836 hbmNew = CreateBitmap (sz.cx, sz.cy, 1, 1, NULL);
2837 SelectObject (himl->hdcMask, hbmNew);
2838 DeleteObject (himl->hbmMask);
2839 himl->hbmMask = hbmNew;
2842 return TRUE;
2846 /*************************************************************************
2847 * ImageList_SetImageCount [COMCTL32.@]
2849 * Resizes an image list to the specified number of images.
2851 * PARAMS
2852 * himl [I] handle to image list
2853 * iImageCount [I] number of images in the image list
2855 * RETURNS
2856 * Success: TRUE
2857 * Failure: FALSE
2860 BOOL WINAPI
2861 ImageList_SetImageCount (HIMAGELIST himl, UINT iImageCount)
2863 HDC hdcBitmap;
2864 HBITMAP hbmNewBitmap, hbmOld;
2865 INT nNewCount, nCopyCount;
2867 TRACE("%p %d\n",himl,iImageCount);
2869 if (!is_valid(himl))
2870 return FALSE;
2872 nNewCount = iImageCount + 1;
2873 nCopyCount = min(himl->cCurImage, iImageCount);
2875 hdcBitmap = CreateCompatibleDC (0);
2877 hbmNewBitmap = ImageList_CreateImage(hdcBitmap, himl, nNewCount);
2879 if (hbmNewBitmap != 0)
2881 hbmOld = SelectObject (hdcBitmap, hbmNewBitmap);
2882 imagelist_copy_images( himl, himl->hdcImage, hdcBitmap, 0, nCopyCount, 0 );
2883 SelectObject (hdcBitmap, hbmOld);
2885 /* FIXME: delete 'empty' image space? */
2887 SelectObject (himl->hdcImage, hbmNewBitmap);
2888 DeleteObject (himl->hbmImage);
2889 himl->hbmImage = hbmNewBitmap;
2891 else
2892 ERR("Could not create new image bitmap!\n");
2894 if (himl->hbmMask)
2896 SIZE sz;
2897 imagelist_get_bitmap_size( himl, nNewCount, &sz );
2898 hbmNewBitmap = CreateBitmap (sz.cx, sz.cy, 1, 1, NULL);
2899 if (hbmNewBitmap != 0)
2901 hbmOld = SelectObject (hdcBitmap, hbmNewBitmap);
2902 imagelist_copy_images( himl, himl->hdcMask, hdcBitmap, 0, nCopyCount, 0 );
2903 SelectObject (hdcBitmap, hbmOld);
2905 /* FIXME: delete 'empty' image space? */
2907 SelectObject (himl->hdcMask, hbmNewBitmap);
2908 DeleteObject (himl->hbmMask);
2909 himl->hbmMask = hbmNewBitmap;
2911 else
2912 ERR("Could not create new mask bitmap!\n");
2915 DeleteDC (hdcBitmap);
2917 if (himl->has_alpha)
2919 char *new_alpha = HeapReAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, himl->has_alpha, nNewCount );
2920 if (new_alpha) himl->has_alpha = new_alpha;
2921 else
2923 HeapFree( GetProcessHeap(), 0, himl->has_alpha );
2924 himl->has_alpha = NULL;
2928 /* Update max image count and current image count */
2929 himl->cMaxImage = nNewCount;
2930 himl->cCurImage = iImageCount;
2932 return TRUE;
2936 /*************************************************************************
2937 * ImageList_SetOverlayImage [COMCTL32.@]
2939 * Assigns an overlay mask index to an existing image in an image list.
2941 * PARAMS
2942 * himl [I] handle to image list
2943 * iImage [I] image index
2944 * iOverlay [I] overlay mask index
2946 * RETURNS
2947 * Success: TRUE
2948 * Failure: FALSE
2951 BOOL WINAPI
2952 ImageList_SetOverlayImage (HIMAGELIST himl, INT iImage, INT iOverlay)
2954 if (!is_valid(himl))
2955 return FALSE;
2956 if ((iOverlay < 1) || (iOverlay > MAX_OVERLAYIMAGE))
2957 return FALSE;
2958 if ((iImage!=-1) && ((iImage < 0) || (iImage > himl->cCurImage)))
2959 return FALSE;
2960 himl->nOvlIdx[iOverlay - 1] = iImage;
2961 return TRUE;
2966 /* helper for ImageList_Write - write bitmap to pstm
2967 * currently everything is written as 24 bit RGB, except masks
2969 static BOOL
2970 _write_bitmap(HBITMAP hBitmap, LPSTREAM pstm)
2972 LPBITMAPFILEHEADER bmfh;
2973 LPBITMAPINFOHEADER bmih;
2974 LPBYTE data = NULL, lpBits;
2975 BITMAP bm;
2976 INT bitCount, sizeImage, offBits, totalSize;
2977 HDC xdc;
2978 BOOL result = FALSE;
2980 if (!GetObjectW(hBitmap, sizeof(BITMAP), &bm))
2981 return FALSE;
2983 bitCount = bm.bmBitsPixel;
2984 sizeImage = get_dib_stride(bm.bmWidth, bitCount) * bm.bmHeight;
2986 totalSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
2987 if(bitCount <= 8)
2988 totalSize += (1 << bitCount) * sizeof(RGBQUAD);
2989 offBits = totalSize;
2990 totalSize += sizeImage;
2992 data = Alloc(totalSize);
2993 bmfh = (LPBITMAPFILEHEADER)data;
2994 bmih = (LPBITMAPINFOHEADER)(data + sizeof(BITMAPFILEHEADER));
2995 lpBits = data + offBits;
2997 /* setup BITMAPFILEHEADER */
2998 bmfh->bfType = (('M' << 8) | 'B');
2999 bmfh->bfSize = offBits;
3000 bmfh->bfReserved1 = 0;
3001 bmfh->bfReserved2 = 0;
3002 bmfh->bfOffBits = offBits;
3004 /* setup BITMAPINFOHEADER */
3005 bmih->biSize = sizeof(BITMAPINFOHEADER);
3006 bmih->biWidth = bm.bmWidth;
3007 bmih->biHeight = bm.bmHeight;
3008 bmih->biPlanes = 1;
3009 bmih->biBitCount = bitCount;
3010 bmih->biCompression = BI_RGB;
3011 bmih->biSizeImage = sizeImage;
3012 bmih->biXPelsPerMeter = 0;
3013 bmih->biYPelsPerMeter = 0;
3014 bmih->biClrUsed = 0;
3015 bmih->biClrImportant = 0;
3017 xdc = GetDC(0);
3018 result = GetDIBits(xdc, hBitmap, 0, bm.bmHeight, lpBits, (BITMAPINFO *)bmih, DIB_RGB_COLORS) == bm.bmHeight;
3019 ReleaseDC(0, xdc);
3020 if (!result)
3021 goto failed;
3023 TRACE("width %u, height %u, planes %u, bpp %u\n",
3024 bmih->biWidth, bmih->biHeight,
3025 bmih->biPlanes, bmih->biBitCount);
3027 if(FAILED(IStream_Write(pstm, data, totalSize, NULL)))
3028 goto failed;
3030 result = TRUE;
3032 failed:
3033 Free(data);
3035 return result;
3039 /*************************************************************************
3040 * ImageList_Write [COMCTL32.@]
3042 * Writes an image list to a stream.
3044 * PARAMS
3045 * himl [I] handle to image list
3046 * pstm [O] Pointer to a stream.
3048 * RETURNS
3049 * Success: TRUE
3050 * Failure: FALSE
3052 * BUGS
3053 * probably.
3056 BOOL WINAPI
3057 ImageList_Write (HIMAGELIST himl, LPSTREAM pstm)
3059 ILHEAD ilHead;
3060 int i;
3062 TRACE("%p %p\n", himl, pstm);
3064 if (!is_valid(himl))
3065 return FALSE;
3067 ilHead.usMagic = (('L' << 8) | 'I');
3068 ilHead.usVersion = 0x101;
3069 ilHead.cCurImage = himl->cCurImage;
3070 ilHead.cMaxImage = himl->cMaxImage;
3071 ilHead.cGrow = himl->cGrow;
3072 ilHead.cx = himl->cx;
3073 ilHead.cy = himl->cy;
3074 ilHead.bkcolor = himl->clrBk;
3075 ilHead.flags = himl->flags;
3076 for(i = 0; i < 4; i++) {
3077 ilHead.ovls[i] = himl->nOvlIdx[i];
3080 TRACE("cx %u, cy %u, flags 0x04%x, cCurImage %u, cMaxImage %u\n",
3081 ilHead.cx, ilHead.cy, ilHead.flags, ilHead.cCurImage, ilHead.cMaxImage);
3083 if(FAILED(IStream_Write(pstm, &ilHead, sizeof(ILHEAD), NULL)))
3084 return FALSE;
3086 /* write the bitmap */
3087 if(!_write_bitmap(himl->hbmImage, pstm))
3088 return FALSE;
3090 /* write the mask if we have one */
3091 if(himl->flags & ILC_MASK) {
3092 if(!_write_bitmap(himl->hbmMask, pstm))
3093 return FALSE;
3096 return TRUE;
3100 static HBITMAP ImageList_CreateImage(HDC hdc, HIMAGELIST himl, UINT count)
3102 HBITMAP hbmNewBitmap;
3103 UINT ilc = (himl->flags & 0xFE);
3104 SIZE sz;
3106 imagelist_get_bitmap_size( himl, count, &sz );
3108 if ((ilc >= ILC_COLOR4 && ilc <= ILC_COLOR32) || ilc == ILC_COLOR)
3110 char buffer[sizeof(BITMAPINFOHEADER) + 256 * sizeof(RGBQUAD)];
3111 BITMAPINFO *bmi = (BITMAPINFO *)buffer;
3113 TRACE("Creating DIBSection %d x %d, %d Bits per Pixel\n",
3114 sz.cx, sz.cy, himl->uBitsPixel);
3116 memset( buffer, 0, sizeof(buffer) );
3117 bmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
3118 bmi->bmiHeader.biWidth = sz.cx;
3119 bmi->bmiHeader.biHeight = sz.cy;
3120 bmi->bmiHeader.biPlanes = 1;
3121 bmi->bmiHeader.biBitCount = himl->uBitsPixel;
3122 bmi->bmiHeader.biCompression = BI_RGB;
3124 if (himl->uBitsPixel <= ILC_COLOR8)
3126 /* retrieve the default color map */
3127 HBITMAP tmp = CreateBitmap( 1, 1, 1, 1, NULL );
3128 GetDIBits( hdc, tmp, 0, 0, NULL, bmi, DIB_RGB_COLORS );
3129 DeleteObject( tmp );
3131 hbmNewBitmap = CreateDIBSection(hdc, bmi, DIB_RGB_COLORS, NULL, 0, 0);
3133 else /*if (ilc == ILC_COLORDDB)*/
3135 TRACE("Creating Bitmap: %d Bits per Pixel\n", himl->uBitsPixel);
3137 hbmNewBitmap = CreateBitmap (sz.cx, sz.cy, 1, himl->uBitsPixel, NULL);
3139 TRACE("returning %p\n", hbmNewBitmap);
3140 return hbmNewBitmap;
3143 /*************************************************************************
3144 * ImageList_SetColorTable [COMCTL32.@]
3146 * Sets the color table of an image list.
3148 * PARAMS
3149 * himl [I] Handle to the image list.
3150 * uStartIndex [I] The first index to set.
3151 * cEntries [I] Number of entries to set.
3152 * prgb [I] New color information for color table for the image list.
3154 * RETURNS
3155 * Success: Number of entries in the table that were set.
3156 * Failure: Zero.
3158 * SEE
3159 * ImageList_Create(), SetDIBColorTable()
3162 UINT WINAPI
3163 ImageList_SetColorTable(HIMAGELIST himl, UINT uStartIndex, UINT cEntries, const RGBQUAD *prgb)
3165 return SetDIBColorTable(himl->hdcImage, uStartIndex, cEntries, prgb);
3168 /*************************************************************************
3169 * ImageList_CoCreateInstance [COMCTL32.@]
3171 * Creates a new imagelist instance and returns an interface pointer to it.
3173 * PARAMS
3174 * rclsid [I] A reference to the CLSID (CLSID_ImageList).
3175 * punkOuter [I] Pointer to IUnknown interface for aggregation, if desired
3176 * riid [I] Identifier of the requested interface.
3177 * ppv [O] Returns the address of the pointer requested, or NULL.
3179 * RETURNS
3180 * Success: S_OK.
3181 * Failure: Error value.
3183 HRESULT WINAPI
3184 ImageList_CoCreateInstance (REFCLSID rclsid, const IUnknown *punkOuter, REFIID riid, void **ppv)
3186 TRACE("(%s,%p,%s,%p)\n", debugstr_guid(rclsid), punkOuter, debugstr_guid(riid), ppv);
3188 if (!IsEqualCLSID(&CLSID_ImageList, rclsid))
3189 return E_NOINTERFACE;
3191 return ImageListImpl_CreateInstance(punkOuter, riid, ppv);
3195 /*************************************************************************
3196 * IImageList implementation
3199 static HRESULT WINAPI ImageListImpl_QueryInterface(IImageList *iface,
3200 REFIID iid, void **ppv)
3202 HIMAGELIST This = (HIMAGELIST) iface;
3203 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
3205 if (!ppv) return E_INVALIDARG;
3207 if (IsEqualIID(&IID_IUnknown, iid) || IsEqualIID(&IID_IImageList, iid))
3208 *ppv = This;
3209 else
3211 *ppv = NULL;
3212 return E_NOINTERFACE;
3215 IUnknown_AddRef((IUnknown*)*ppv);
3216 return S_OK;
3219 static ULONG WINAPI ImageListImpl_AddRef(IImageList *iface)
3221 HIMAGELIST This = (HIMAGELIST) iface;
3222 ULONG ref = InterlockedIncrement(&This->ref);
3224 TRACE("(%p) refcount=%u\n", iface, ref);
3225 return ref;
3228 static ULONG WINAPI ImageListImpl_Release(IImageList *iface)
3230 HIMAGELIST This = (HIMAGELIST) iface;
3231 ULONG ref = InterlockedDecrement(&This->ref);
3233 TRACE("(%p) refcount=%u\n", iface, ref);
3235 if (ref == 0)
3237 /* delete image bitmaps */
3238 if (This->hbmImage) DeleteObject (This->hbmImage);
3239 if (This->hbmMask) DeleteObject (This->hbmMask);
3241 /* delete image & mask DCs */
3242 if (This->hdcImage) DeleteDC (This->hdcImage);
3243 if (This->hdcMask) DeleteDC (This->hdcMask);
3245 /* delete blending brushes */
3246 if (This->hbrBlend25) DeleteObject (This->hbrBlend25);
3247 if (This->hbrBlend50) DeleteObject (This->hbrBlend50);
3249 This->lpVtbl = NULL;
3250 HeapFree(GetProcessHeap(), 0, This->has_alpha);
3251 HeapFree(GetProcessHeap(), 0, This);
3254 return ref;
3257 static HRESULT WINAPI ImageListImpl_Add(IImageList *iface, HBITMAP hbmImage,
3258 HBITMAP hbmMask, int *pi)
3260 HIMAGELIST This = (HIMAGELIST) iface;
3261 int ret;
3263 if (!pi)
3264 return E_FAIL;
3266 ret = ImageList_Add(This, hbmImage, hbmMask);
3268 if (ret == -1)
3269 return E_FAIL;
3271 *pi = ret;
3272 return S_OK;
3275 static HRESULT WINAPI ImageListImpl_ReplaceIcon(IImageList *iface, int i,
3276 HICON hicon, int *pi)
3278 HIMAGELIST This = (HIMAGELIST) iface;
3279 int ret;
3281 if (!pi)
3282 return E_FAIL;
3284 ret = ImageList_ReplaceIcon(This, i, hicon);
3286 if (ret == -1)
3287 return E_FAIL;
3289 *pi = ret;
3290 return S_OK;
3293 static HRESULT WINAPI ImageListImpl_SetOverlayImage(IImageList *iface,
3294 int iImage, int iOverlay)
3296 return ImageList_SetOverlayImage((HIMAGELIST) iface, iImage, iOverlay)
3297 ? S_OK : E_FAIL;
3300 static HRESULT WINAPI ImageListImpl_Replace(IImageList *iface, int i,
3301 HBITMAP hbmImage, HBITMAP hbmMask)
3303 return ImageList_Replace((HIMAGELIST) iface, i, hbmImage, hbmMask) ? S_OK :
3304 E_FAIL;
3307 static HRESULT WINAPI ImageListImpl_AddMasked(IImageList *iface, HBITMAP hbmImage,
3308 COLORREF crMask, int *pi)
3310 HIMAGELIST This = (HIMAGELIST) iface;
3311 int ret;
3313 if (!pi)
3314 return E_FAIL;
3316 ret = ImageList_AddMasked(This, hbmImage, crMask);
3318 if (ret == -1)
3319 return E_FAIL;
3321 *pi = ret;
3322 return S_OK;
3325 static HRESULT WINAPI ImageListImpl_Draw(IImageList *iface,
3326 IMAGELISTDRAWPARAMS *pimldp)
3328 HIMAGELIST This = (HIMAGELIST) iface;
3329 HIMAGELIST old_himl;
3330 int ret;
3332 /* As far as I can tell, Windows simply ignores the contents of pimldp->himl
3333 so we shall simulate the same */
3334 old_himl = pimldp->himl;
3335 pimldp->himl = This;
3337 ret = ImageList_DrawIndirect(pimldp);
3339 pimldp->himl = old_himl;
3340 return ret ? S_OK : E_INVALIDARG;
3343 static HRESULT WINAPI ImageListImpl_Remove(IImageList *iface, int i)
3345 return (ImageList_Remove((HIMAGELIST) iface, i) == 0) ? E_INVALIDARG : S_OK;
3348 static HRESULT WINAPI ImageListImpl_GetIcon(IImageList *iface, int i, UINT flags,
3349 HICON *picon)
3351 HICON hIcon;
3353 if (!picon)
3354 return E_FAIL;
3356 hIcon = ImageList_GetIcon((HIMAGELIST) iface, i, flags);
3358 if (hIcon == NULL)
3359 return E_FAIL;
3361 *picon = hIcon;
3362 return S_OK;
3365 static HRESULT WINAPI ImageListImpl_GetImageInfo(IImageList *iface, int i,
3366 IMAGEINFO *pImageInfo)
3368 return ImageList_GetImageInfo((HIMAGELIST) iface, i, pImageInfo) ? S_OK : E_FAIL;
3371 static HRESULT WINAPI ImageListImpl_Copy(IImageList *iface, int iDst,
3372 IUnknown *punkSrc, int iSrc, UINT uFlags)
3374 HIMAGELIST This = (HIMAGELIST) iface;
3375 IImageList *src = NULL;
3376 HRESULT ret;
3378 if (!punkSrc)
3379 return E_FAIL;
3381 /* TODO: Add test for IID_ImageList2 too */
3382 if (FAILED(IUnknown_QueryInterface(punkSrc, &IID_IImageList,
3383 (void **) &src)))
3384 return E_FAIL;
3386 if (ImageList_Copy(This, iDst, (HIMAGELIST) src, iSrc, uFlags))
3387 ret = S_OK;
3388 else
3389 ret = E_FAIL;
3391 IImageList_Release(src);
3392 return ret;
3395 static HRESULT WINAPI ImageListImpl_Merge(IImageList *iface, int i1,
3396 IUnknown *punk2, int i2, int dx, int dy, REFIID riid, void **ppv)
3398 HIMAGELIST This = (HIMAGELIST) iface;
3399 IImageList *iml2 = NULL;
3400 HIMAGELIST hNew;
3401 HRESULT ret = E_FAIL;
3403 TRACE("(%p)->(%d %p %d %d %d %s %p)\n", iface, i1, punk2, i2, dx, dy, debugstr_guid(riid), ppv);
3405 /* TODO: Add test for IID_ImageList2 too */
3406 if (FAILED(IUnknown_QueryInterface(punk2, &IID_IImageList,
3407 (void **) &iml2)))
3408 return E_FAIL;
3410 hNew = ImageList_Merge(This, i1, (HIMAGELIST) iml2, i2, dx, dy);
3412 /* Get the interface for the new image list */
3413 if (hNew)
3415 IImageList *imerge = (IImageList*)hNew;
3417 ret = HIMAGELIST_QueryInterface(hNew, riid, ppv);
3418 IImageList_Release(imerge);
3421 IImageList_Release(iml2);
3422 return ret;
3425 static HRESULT WINAPI ImageListImpl_Clone(IImageList *iface, REFIID riid, void **ppv)
3427 HIMAGELIST This = (HIMAGELIST) iface;
3428 HIMAGELIST clone;
3429 HRESULT ret = E_FAIL;
3431 TRACE("(%p)->(%s %p)\n", iface, debugstr_guid(riid), ppv);
3433 clone = ImageList_Duplicate(This);
3435 /* Get the interface for the new image list */
3436 if (clone)
3438 IImageList *iclone = (IImageList*)clone;
3440 ret = HIMAGELIST_QueryInterface(clone, riid, ppv);
3441 IImageList_Release(iclone);
3444 return ret;
3447 static HRESULT WINAPI ImageListImpl_GetImageRect(IImageList *iface, int i,
3448 RECT *prc)
3450 HIMAGELIST This = (HIMAGELIST) iface;
3451 IMAGEINFO info;
3453 if (!prc)
3454 return E_FAIL;
3456 if (!ImageList_GetImageInfo(This, i, &info))
3457 return E_FAIL;
3459 return CopyRect(prc, &info.rcImage) ? S_OK : E_FAIL;
3462 static HRESULT WINAPI ImageListImpl_GetIconSize(IImageList *iface, int *cx,
3463 int *cy)
3465 HIMAGELIST This = (HIMAGELIST) iface;
3467 return ImageList_GetIconSize(This, cx, cy) ? S_OK : E_INVALIDARG;
3470 static HRESULT WINAPI ImageListImpl_SetIconSize(IImageList *iface, int cx,
3471 int cy)
3473 return ImageList_SetIconSize((HIMAGELIST) iface, cx, cy) ? S_OK : E_FAIL;
3476 static HRESULT WINAPI ImageListImpl_GetImageCount(IImageList *iface, int *pi)
3478 *pi = ImageList_GetImageCount((HIMAGELIST) iface);
3479 return S_OK;
3482 static HRESULT WINAPI ImageListImpl_SetImageCount(IImageList *iface,
3483 UINT uNewCount)
3485 return ImageList_SetImageCount((HIMAGELIST) iface, uNewCount) ? S_OK : E_FAIL;
3488 static HRESULT WINAPI ImageListImpl_SetBkColor(IImageList *iface, COLORREF clrBk,
3489 COLORREF *pclr)
3491 *pclr = ImageList_SetBkColor((HIMAGELIST) iface, clrBk);
3492 return S_OK;
3495 static HRESULT WINAPI ImageListImpl_GetBkColor(IImageList *iface, COLORREF *pclr)
3497 *pclr = ImageList_GetBkColor((HIMAGELIST) iface);
3498 return S_OK;
3501 static HRESULT WINAPI ImageListImpl_BeginDrag(IImageList *iface, int iTrack,
3502 int dxHotspot, int dyHotspot)
3504 return ImageList_BeginDrag((HIMAGELIST) iface, iTrack, dxHotspot, dyHotspot) ? S_OK : E_FAIL;
3507 static HRESULT WINAPI ImageListImpl_EndDrag(IImageList *iface)
3509 ImageList_EndDrag();
3510 return S_OK;
3513 static HRESULT WINAPI ImageListImpl_DragEnter(IImageList *iface, HWND hwndLock,
3514 int x, int y)
3516 return ImageList_DragEnter(hwndLock, x, y) ? S_OK : E_FAIL;
3519 static HRESULT WINAPI ImageListImpl_DragLeave(IImageList *iface, HWND hwndLock)
3521 return ImageList_DragLeave(hwndLock) ? S_OK : E_FAIL;
3524 static HRESULT WINAPI ImageListImpl_DragMove(IImageList *iface, int x, int y)
3526 return ImageList_DragMove(x, y) ? S_OK : E_FAIL;
3529 static HRESULT WINAPI ImageListImpl_SetDragCursorImage(IImageList *iface,
3530 IUnknown *punk, int iDrag, int dxHotspot, int dyHotspot)
3532 IImageList *iml2 = NULL;
3533 HRESULT ret;
3535 if (!punk)
3536 return E_FAIL;
3538 /* TODO: Add test for IID_ImageList2 too */
3539 if (FAILED(IUnknown_QueryInterface(punk, &IID_IImageList,
3540 (void **) &iml2)))
3541 return E_FAIL;
3543 ret = ImageList_SetDragCursorImage((HIMAGELIST) iml2, iDrag, dxHotspot,
3544 dyHotspot);
3546 IImageList_Release(iml2);
3548 return ret ? S_OK : E_FAIL;
3551 static HRESULT WINAPI ImageListImpl_DragShowNolock(IImageList *iface, BOOL fShow)
3553 return ImageList_DragShowNolock(fShow) ? S_OK : E_FAIL;
3556 static HRESULT WINAPI ImageListImpl_GetDragImage(IImageList *iface, POINT *ppt,
3557 POINT *pptHotspot, REFIID riid, PVOID *ppv)
3559 HRESULT ret = E_FAIL;
3560 HIMAGELIST hNew;
3562 if (!ppv)
3563 return E_FAIL;
3565 hNew = ImageList_GetDragImage(ppt, pptHotspot);
3567 /* Get the interface for the new image list */
3568 if (hNew)
3570 IImageList *idrag = (IImageList*)hNew;
3572 ret = HIMAGELIST_QueryInterface(hNew, riid, ppv);
3573 IImageList_Release(idrag);
3576 return ret;
3579 static HRESULT WINAPI ImageListImpl_GetItemFlags(IImageList *iface, int i,
3580 DWORD *dwFlags)
3582 FIXME("STUB: %p %d %p\n", iface, i, dwFlags);
3583 return E_NOTIMPL;
3586 static HRESULT WINAPI ImageListImpl_GetOverlayImage(IImageList *iface, int iOverlay,
3587 int *piIndex)
3589 HIMAGELIST This = (HIMAGELIST) iface;
3590 int i;
3592 if ((iOverlay < 0) || (iOverlay > This->cCurImage))
3593 return E_FAIL;
3595 for (i = 0; i < MAX_OVERLAYIMAGE; i++)
3597 if (This->nOvlIdx[i] == iOverlay)
3599 *piIndex = i + 1;
3600 return S_OK;
3604 return E_FAIL;
3608 static const IImageListVtbl ImageListImpl_Vtbl = {
3609 ImageListImpl_QueryInterface,
3610 ImageListImpl_AddRef,
3611 ImageListImpl_Release,
3612 ImageListImpl_Add,
3613 ImageListImpl_ReplaceIcon,
3614 ImageListImpl_SetOverlayImage,
3615 ImageListImpl_Replace,
3616 ImageListImpl_AddMasked,
3617 ImageListImpl_Draw,
3618 ImageListImpl_Remove,
3619 ImageListImpl_GetIcon,
3620 ImageListImpl_GetImageInfo,
3621 ImageListImpl_Copy,
3622 ImageListImpl_Merge,
3623 ImageListImpl_Clone,
3624 ImageListImpl_GetImageRect,
3625 ImageListImpl_GetIconSize,
3626 ImageListImpl_SetIconSize,
3627 ImageListImpl_GetImageCount,
3628 ImageListImpl_SetImageCount,
3629 ImageListImpl_SetBkColor,
3630 ImageListImpl_GetBkColor,
3631 ImageListImpl_BeginDrag,
3632 ImageListImpl_EndDrag,
3633 ImageListImpl_DragEnter,
3634 ImageListImpl_DragLeave,
3635 ImageListImpl_DragMove,
3636 ImageListImpl_SetDragCursorImage,
3637 ImageListImpl_DragShowNolock,
3638 ImageListImpl_GetDragImage,
3639 ImageListImpl_GetItemFlags,
3640 ImageListImpl_GetOverlayImage
3643 static BOOL is_valid(HIMAGELIST himl)
3645 BOOL valid;
3646 __TRY
3648 valid = himl && himl->lpVtbl == &ImageListImpl_Vtbl;
3650 __EXCEPT_PAGE_FAULT
3652 valid = FALSE;
3654 __ENDTRY
3655 return valid;
3658 /*************************************************************************
3659 * HIMAGELIST_QueryInterface [COMCTL32.@]
3661 * Returns a pointer to an IImageList or IImageList2 object for the given
3662 * HIMAGELIST.
3664 * PARAMS
3665 * himl [I] Image list handle.
3666 * riid [I] Identifier of the requested interface.
3667 * ppv [O] Returns the address of the pointer requested, or NULL.
3669 * RETURNS
3670 * Success: S_OK.
3671 * Failure: Error value.
3673 HRESULT WINAPI
3674 HIMAGELIST_QueryInterface (HIMAGELIST himl, REFIID riid, void **ppv)
3676 TRACE("(%p,%s,%p)\n", himl, debugstr_guid(riid), ppv);
3677 return IImageList_QueryInterface((IImageList *) himl, riid, ppv);
3680 static HRESULT ImageListImpl_CreateInstance(const IUnknown *pUnkOuter, REFIID iid, void** ppv)
3682 HIMAGELIST This;
3683 HRESULT ret;
3685 TRACE("(%p,%s,%p)\n", pUnkOuter, debugstr_guid(iid), ppv);
3687 *ppv = NULL;
3689 if (pUnkOuter) return CLASS_E_NOAGGREGATION;
3691 This = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct _IMAGELIST));
3692 if (!This) return E_OUTOFMEMORY;
3694 This->lpVtbl = &ImageListImpl_Vtbl;
3695 This->ref = 1;
3697 ret = IUnknown_QueryInterface((IUnknown*)This, iid, ppv);
3698 IUnknown_Release((IUnknown*)This);
3700 return ret;