comctl32: Take the total bitmap size into account when reading a bottom-up image...
[wine/multimedia.git] / dlls / comctl32 / imagelist.c
blobca6579fb1f57d7e4d01667c03676120359b166b2
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 "imagelist.h"
56 #include "wine/debug.h"
57 #include "wine/exception.h"
59 WINE_DEFAULT_DEBUG_CHANNEL(imagelist);
62 #define MAX_OVERLAYIMAGE 15
64 /* internal image list data used for Drag & Drop operations */
65 typedef struct
67 HWND hwnd;
68 HIMAGELIST himl;
69 /* position of the drag image relative to the window */
70 INT x;
71 INT y;
72 /* offset of the hotspot relative to the origin of the image */
73 INT dxHotspot;
74 INT dyHotspot;
75 /* is the drag image visible */
76 BOOL bShow;
77 /* saved background */
78 HBITMAP hbmBg;
79 } INTERNALDRAG;
81 static INTERNALDRAG InternalDrag = { 0, 0, 0, 0, 0, 0, FALSE, 0 };
83 static HBITMAP ImageList_CreateImage(HDC hdc, HIMAGELIST himl, UINT count);
84 static HRESULT ImageListImpl_CreateInstance(const IUnknown *pUnkOuter, REFIID iid, void** ppv);
85 static inline BOOL is_valid(HIMAGELIST himl);
88 * An imagelist with N images is tiled like this:
90 * N/4 ->
92 * 4 048C..
93 * 159D..
94 * | 26AE.N
95 * V 37BF.
98 #define TILE_COUNT 4
100 static inline UINT imagelist_height( UINT count )
102 return ((count + TILE_COUNT - 1)/TILE_COUNT);
105 static inline void imagelist_point_from_index( HIMAGELIST himl, UINT index, LPPOINT pt )
107 pt->x = (index%TILE_COUNT) * himl->cx;
108 pt->y = (index/TILE_COUNT) * himl->cy;
111 static inline void imagelist_get_bitmap_size( HIMAGELIST himl, UINT count, SIZE *sz )
113 sz->cx = himl->cx * TILE_COUNT;
114 sz->cy = imagelist_height( count ) * himl->cy;
117 static inline int get_dib_stride( int width, int bpp )
119 return ((width * bpp + 31) >> 3) & ~3;
122 static inline int get_dib_image_size( const BITMAPINFO *info )
124 return get_dib_stride( info->bmiHeader.biWidth, info->bmiHeader.biBitCount )
125 * abs( info->bmiHeader.biHeight );
129 * imagelist_copy_images()
131 * Copies a block of count images from offset src in the list to offset dest.
132 * Images are copied a row at at time. Assumes hdcSrc and hdcDest are different.
134 static inline void imagelist_copy_images( HIMAGELIST himl, HDC hdcSrc, HDC hdcDest,
135 UINT src, UINT count, UINT dest )
137 POINT ptSrc, ptDest;
138 SIZE sz;
139 UINT i;
141 for ( i=0; i<TILE_COUNT; i++ )
143 imagelist_point_from_index( himl, src+i, &ptSrc );
144 imagelist_point_from_index( himl, dest+i, &ptDest );
145 sz.cx = himl->cx;
146 sz.cy = himl->cy * imagelist_height( count - i );
148 BitBlt( hdcDest, ptDest.x, ptDest.y, sz.cx, sz.cy,
149 hdcSrc, ptSrc.x, ptSrc.y, SRCCOPY );
153 static void add_dib_bits( HIMAGELIST himl, int pos, int count, int width, int height,
154 BITMAPINFO *info, BITMAPINFO *mask_info, DWORD *bits, BYTE *mask_bits )
156 int i, j, n;
157 POINT pt;
158 int stride = info->bmiHeader.biWidth;
159 int mask_stride = (info->bmiHeader.biWidth + 31) / 32 * 4;
161 for (n = 0; n < count; n++)
163 int has_alpha = 0;
165 imagelist_point_from_index( himl, pos + n, &pt );
167 /* check if bitmap has an alpha channel */
168 for (i = 0; i < height && !has_alpha; i++)
169 for (j = n * width; j < (n + 1) * width; j++)
170 if ((has_alpha = ((bits[i * stride + j] & 0xff000000) != 0))) break;
172 if (!has_alpha) /* generate alpha channel from the mask */
174 for (i = 0; i < height; i++)
175 for (j = n * width; j < (n + 1) * width; j++)
176 if (!mask_info || !((mask_bits[i * mask_stride + j / 8] << (j % 8)) & 0x80))
177 bits[i * stride + j] |= 0xff000000;
178 else
179 bits[i * stride + j] = 0;
181 else
183 himl->has_alpha[pos + n] = 1;
185 if (mask_info && himl->hbmMask) /* generate the mask from the alpha channel */
187 for (i = 0; i < height; i++)
188 for (j = n * width; j < (n + 1) * width; j++)
189 if ((bits[i * stride + j] >> 24) > 25) /* more than 10% alpha */
190 mask_bits[i * mask_stride + j / 8] &= ~(0x80 >> (j % 8));
191 else
192 mask_bits[i * mask_stride + j / 8] |= 0x80 >> (j % 8);
195 StretchDIBits( himl->hdcImage, pt.x, pt.y, himl->cx, himl->cy,
196 n * width, 0, width, height, bits, info, DIB_RGB_COLORS, SRCCOPY );
197 if (mask_info)
198 StretchDIBits( himl->hdcMask, pt.x, pt.y, himl->cx, himl->cy,
199 n * width, 0, width, height, mask_bits, mask_info, DIB_RGB_COLORS, SRCCOPY );
203 /* add images with an alpha channel when the image list is 32 bpp */
204 static BOOL add_with_alpha( HIMAGELIST himl, HDC hdc, int pos, int count,
205 int width, int height, HBITMAP hbmImage, HBITMAP hbmMask )
207 BOOL ret = FALSE;
208 BITMAP bm;
209 BITMAPINFO *info, *mask_info = NULL;
210 DWORD *bits = NULL;
211 BYTE *mask_bits = NULL;
212 DWORD mask_width;
214 if (!GetObjectW( hbmImage, sizeof(bm), &bm )) return FALSE;
216 /* if either the imagelist or the source bitmap don't have an alpha channel, bail out now */
217 if (!himl->has_alpha) return FALSE;
218 if (bm.bmBitsPixel != 32) return FALSE;
220 SelectObject( hdc, hbmImage );
221 mask_width = (bm.bmWidth + 31) / 32 * 4;
223 if (!(info = HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET( BITMAPINFO, bmiColors[256] )))) goto done;
224 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
225 info->bmiHeader.biWidth = bm.bmWidth;
226 info->bmiHeader.biHeight = -height;
227 info->bmiHeader.biPlanes = 1;
228 info->bmiHeader.biBitCount = 32;
229 info->bmiHeader.biCompression = BI_RGB;
230 info->bmiHeader.biSizeImage = bm.bmWidth * height * 4;
231 info->bmiHeader.biXPelsPerMeter = 0;
232 info->bmiHeader.biYPelsPerMeter = 0;
233 info->bmiHeader.biClrUsed = 0;
234 info->bmiHeader.biClrImportant = 0;
235 if (!(bits = HeapAlloc( GetProcessHeap(), 0, info->bmiHeader.biSizeImage ))) goto done;
236 if (!GetDIBits( hdc, hbmImage, 0, height, bits, info, DIB_RGB_COLORS )) goto done;
238 if (hbmMask)
240 if (!(mask_info = HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET( BITMAPINFO, bmiColors[2] ))))
241 goto done;
242 mask_info->bmiHeader = info->bmiHeader;
243 mask_info->bmiHeader.biBitCount = 1;
244 mask_info->bmiHeader.biSizeImage = mask_width * height;
245 if (!(mask_bits = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, info->bmiHeader.biSizeImage )))
246 goto done;
247 if (!GetDIBits( hdc, hbmMask, 0, height, mask_bits, mask_info, DIB_RGB_COLORS )) goto done;
250 add_dib_bits( himl, pos, count, width, height, info, mask_info, bits, mask_bits );
251 ret = TRUE;
253 done:
254 HeapFree( GetProcessHeap(), 0, info );
255 HeapFree( GetProcessHeap(), 0, mask_info );
256 HeapFree( GetProcessHeap(), 0, bits );
257 HeapFree( GetProcessHeap(), 0, mask_bits );
258 return ret;
261 /*************************************************************************
262 * IMAGELIST_InternalExpandBitmaps [Internal]
264 * Expands the bitmaps of an image list by the given number of images.
266 * PARAMS
267 * himl [I] handle to image list
268 * nImageCount [I] number of images to add
270 * RETURNS
271 * nothing
273 * NOTES
274 * This function CANNOT be used to reduce the number of images.
276 static void
277 IMAGELIST_InternalExpandBitmaps(HIMAGELIST himl, INT nImageCount)
279 HDC hdcBitmap;
280 HBITMAP hbmNewBitmap, hbmNull;
281 INT nNewCount;
282 SIZE sz;
284 TRACE("%p has allocated %d, max %d, grow %d images\n", himl, himl->cCurImage, himl->cMaxImage, himl->cGrow);
286 if (himl->cCurImage + nImageCount < himl->cMaxImage)
287 return;
289 nNewCount = himl->cMaxImage + max(nImageCount, himl->cGrow) + 1;
291 imagelist_get_bitmap_size(himl, nNewCount, &sz);
293 TRACE("Create expanded bitmaps : himl=%p x=%d y=%d count=%d\n", himl, sz.cx, sz.cy, nNewCount);
294 hdcBitmap = CreateCompatibleDC (0);
296 hbmNewBitmap = ImageList_CreateImage(hdcBitmap, himl, nNewCount);
298 if (hbmNewBitmap == 0)
299 ERR("creating new image bitmap (x=%d y=%d)!\n", sz.cx, sz.cy);
301 if (himl->cCurImage)
303 hbmNull = SelectObject (hdcBitmap, hbmNewBitmap);
304 BitBlt (hdcBitmap, 0, 0, sz.cx, sz.cy,
305 himl->hdcImage, 0, 0, SRCCOPY);
306 SelectObject (hdcBitmap, hbmNull);
308 SelectObject (himl->hdcImage, hbmNewBitmap);
309 DeleteObject (himl->hbmImage);
310 himl->hbmImage = hbmNewBitmap;
312 if (himl->flags & ILC_MASK)
314 hbmNewBitmap = CreateBitmap (sz.cx, sz.cy, 1, 1, NULL);
316 if (hbmNewBitmap == 0)
317 ERR("creating new mask bitmap!\n");
319 if(himl->cCurImage)
321 hbmNull = SelectObject (hdcBitmap, hbmNewBitmap);
322 BitBlt (hdcBitmap, 0, 0, sz.cx, sz.cy,
323 himl->hdcMask, 0, 0, SRCCOPY);
324 SelectObject (hdcBitmap, hbmNull);
326 SelectObject (himl->hdcMask, hbmNewBitmap);
327 DeleteObject (himl->hbmMask);
328 himl->hbmMask = hbmNewBitmap;
331 if (himl->has_alpha)
333 char *new_alpha = HeapReAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, himl->has_alpha, nNewCount );
334 if (new_alpha) himl->has_alpha = new_alpha;
335 else
337 HeapFree( GetProcessHeap(), 0, himl->has_alpha );
338 himl->has_alpha = NULL;
342 himl->cMaxImage = nNewCount;
344 DeleteDC (hdcBitmap);
348 /*************************************************************************
349 * ImageList_Add [COMCTL32.@]
351 * Add an image or images to an image list.
353 * PARAMS
354 * himl [I] handle to image list
355 * hbmImage [I] handle to image bitmap
356 * hbmMask [I] handle to mask bitmap
358 * RETURNS
359 * Success: Index of the first new image.
360 * Failure: -1
363 INT WINAPI
364 ImageList_Add (HIMAGELIST himl, HBITMAP hbmImage, HBITMAP hbmMask)
366 HDC hdcBitmap, hdcTemp = 0;
367 INT nFirstIndex, nImageCount, i;
368 BITMAP bmp;
369 POINT pt;
371 TRACE("himl=%p hbmimage=%p hbmmask=%p\n", himl, hbmImage, hbmMask);
372 if (!is_valid(himl))
373 return -1;
375 if (!GetObjectW(hbmImage, sizeof(BITMAP), &bmp))
376 return -1;
378 TRACE("himl %p, cCurImage %d, cMaxImage %d, cGrow %d, cx %d, cy %d\n",
379 himl, himl->cCurImage, himl->cMaxImage, himl->cGrow, himl->cx, himl->cy);
381 nImageCount = bmp.bmWidth / himl->cx;
383 TRACE("%p has %d images (%d x %d)\n", hbmImage, nImageCount, bmp.bmWidth, bmp.bmHeight);
385 IMAGELIST_InternalExpandBitmaps(himl, nImageCount);
387 hdcBitmap = CreateCompatibleDC(0);
389 SelectObject(hdcBitmap, hbmImage);
391 if (add_with_alpha( himl, hdcBitmap, himl->cCurImage, nImageCount,
392 himl->cx, min( himl->cy, bmp.bmHeight), hbmImage, hbmMask ))
393 goto done;
395 if (himl->hbmMask)
397 hdcTemp = CreateCompatibleDC(0);
398 SelectObject(hdcTemp, hbmMask);
401 for (i=0; i<nImageCount; i++)
403 imagelist_point_from_index( himl, himl->cCurImage + i, &pt );
405 /* Copy result to the imagelist
407 BitBlt( himl->hdcImage, pt.x, pt.y, himl->cx, bmp.bmHeight,
408 hdcBitmap, i*himl->cx, 0, SRCCOPY );
410 if (!himl->hbmMask)
411 continue;
413 BitBlt( himl->hdcMask, pt.x, pt.y, himl->cx, bmp.bmHeight,
414 hdcTemp, i*himl->cx, 0, SRCCOPY );
416 /* Remove the background from the image
418 BitBlt( himl->hdcImage, pt.x, pt.y, himl->cx, bmp.bmHeight,
419 himl->hdcMask, pt.x, pt.y, 0x220326 ); /* NOTSRCAND */
421 if (hdcTemp) DeleteDC(hdcTemp);
423 done:
424 DeleteDC(hdcBitmap);
426 nFirstIndex = himl->cCurImage;
427 himl->cCurImage += nImageCount;
429 return nFirstIndex;
433 /*************************************************************************
434 * ImageList_AddIcon [COMCTL32.@]
436 * Adds an icon to an image list.
438 * PARAMS
439 * himl [I] handle to image list
440 * hIcon [I] handle to icon
442 * RETURNS
443 * Success: index of the new image
444 * Failure: -1
446 #undef ImageList_AddIcon
447 INT WINAPI ImageList_AddIcon (HIMAGELIST himl, HICON hIcon)
449 return ImageList_ReplaceIcon (himl, -1, hIcon);
453 /*************************************************************************
454 * ImageList_AddMasked [COMCTL32.@]
456 * Adds an image or images to an image list and creates a mask from the
457 * specified bitmap using the mask color.
459 * PARAMS
460 * himl [I] handle to image list.
461 * hBitmap [I] handle to bitmap
462 * clrMask [I] mask color.
464 * RETURNS
465 * Success: Index of the first new image.
466 * Failure: -1
469 INT WINAPI
470 ImageList_AddMasked (HIMAGELIST himl, HBITMAP hBitmap, COLORREF clrMask)
472 HDC hdcMask, hdcBitmap;
473 INT ret;
474 BITMAP bmp;
475 HBITMAP hMaskBitmap;
476 COLORREF bkColor;
478 TRACE("himl=%p hbitmap=%p clrmask=%x\n", himl, hBitmap, clrMask);
479 if (!is_valid(himl))
480 return -1;
482 if (!GetObjectW(hBitmap, sizeof(BITMAP), &bmp))
483 return -1;
485 hdcBitmap = CreateCompatibleDC(0);
486 SelectObject(hdcBitmap, hBitmap);
488 /* Create a temp Mask so we can remove the background of the Image */
489 hdcMask = CreateCompatibleDC(0);
490 hMaskBitmap = CreateBitmap(bmp.bmWidth, bmp.bmHeight, 1, 1, NULL);
491 SelectObject(hdcMask, hMaskBitmap);
493 /* create monochrome image to the mask bitmap */
494 bkColor = (clrMask != CLR_DEFAULT) ? clrMask : GetPixel (hdcBitmap, 0, 0);
495 SetBkColor (hdcBitmap, bkColor);
496 BitBlt (hdcMask, 0, 0, bmp.bmWidth, bmp.bmHeight, hdcBitmap, 0, 0, SRCCOPY);
498 SetBkColor(hdcBitmap, RGB(255,255,255));
501 * Remove the background from the image
503 * WINDOWS BUG ALERT!!!!!!
504 * The statement below should not be done in common practice
505 * but this is how ImageList_AddMasked works in Windows.
506 * It overwrites the original bitmap passed, this was discovered
507 * by using the same bitmap to iterate the different styles
508 * on windows where it failed (BUT ImageList_Add is OK)
509 * This is here in case some apps rely on this bug
511 * Blt mode 0x220326 is NOTSRCAND
513 BitBlt(hdcBitmap, 0, 0, bmp.bmWidth, bmp.bmHeight, hdcMask, 0, 0, 0x220326);
515 DeleteDC(hdcBitmap);
516 DeleteDC(hdcMask);
518 ret = ImageList_Add( himl, hBitmap, hMaskBitmap );
520 DeleteObject(hMaskBitmap);
521 return ret;
525 /*************************************************************************
526 * ImageList_BeginDrag [COMCTL32.@]
528 * Creates a temporary image list that contains one image. It will be used
529 * as a drag image.
531 * PARAMS
532 * himlTrack [I] handle to the source image list
533 * iTrack [I] index of the drag image in the source image list
534 * dxHotspot [I] X position of the hot spot of the drag image
535 * dyHotspot [I] Y position of the hot spot of the drag image
537 * RETURNS
538 * Success: TRUE
539 * Failure: FALSE
542 BOOL WINAPI
543 ImageList_BeginDrag (HIMAGELIST himlTrack, INT iTrack,
544 INT dxHotspot, INT dyHotspot)
546 INT cx, cy;
548 TRACE("(himlTrack=%p iTrack=%d dx=%d dy=%d)\n", himlTrack, iTrack,
549 dxHotspot, dyHotspot);
551 if (!is_valid(himlTrack))
552 return FALSE;
554 if (InternalDrag.himl)
555 ImageList_EndDrag ();
557 cx = himlTrack->cx;
558 cy = himlTrack->cy;
560 InternalDrag.himl = ImageList_Create (cx, cy, himlTrack->flags, 1, 1);
561 if (InternalDrag.himl == NULL) {
562 WARN("Error creating drag image list!\n");
563 return FALSE;
566 InternalDrag.dxHotspot = dxHotspot;
567 InternalDrag.dyHotspot = dyHotspot;
569 /* copy image */
570 BitBlt (InternalDrag.himl->hdcImage, 0, 0, cx, cy, himlTrack->hdcImage, iTrack * cx, 0, SRCCOPY);
572 /* copy mask */
573 BitBlt (InternalDrag.himl->hdcMask, 0, 0, cx, cy, himlTrack->hdcMask, iTrack * cx, 0, SRCCOPY);
575 InternalDrag.himl->cCurImage = 1;
577 return TRUE;
581 /*************************************************************************
582 * ImageList_Copy [COMCTL32.@]
584 * Copies an image of the source image list to an image of the
585 * destination image list. Images can be copied or swapped.
587 * PARAMS
588 * himlDst [I] handle to the destination image list
589 * iDst [I] destination image index.
590 * himlSrc [I] handle to the source image list
591 * iSrc [I] source image index
592 * uFlags [I] flags for the copy operation
594 * RETURNS
595 * Success: TRUE
596 * Failure: FALSE
598 * NOTES
599 * Copying from one image list to another is possible. The original
600 * implementation just copies or swaps within one image list.
601 * Could this feature become a bug??? ;-)
604 BOOL WINAPI
605 ImageList_Copy (HIMAGELIST himlDst, INT iDst, HIMAGELIST himlSrc,
606 INT iSrc, UINT uFlags)
608 POINT ptSrc, ptDst;
610 TRACE("himlDst=%p iDst=%d himlSrc=%p iSrc=%d\n", himlDst, iDst, himlSrc, iSrc);
612 if (!is_valid(himlSrc) || !is_valid(himlDst))
613 return FALSE;
614 if ((iDst < 0) || (iDst >= himlDst->cCurImage))
615 return FALSE;
616 if ((iSrc < 0) || (iSrc >= himlSrc->cCurImage))
617 return FALSE;
619 imagelist_point_from_index( himlDst, iDst, &ptDst );
620 imagelist_point_from_index( himlSrc, iSrc, &ptSrc );
622 if (uFlags & ILCF_SWAP) {
623 /* swap */
624 HDC hdcBmp;
625 HBITMAP hbmTempImage, hbmTempMask;
627 hdcBmp = CreateCompatibleDC (0);
629 /* create temporary bitmaps */
630 hbmTempImage = CreateBitmap (himlSrc->cx, himlSrc->cy, 1,
631 himlSrc->uBitsPixel, NULL);
632 hbmTempMask = CreateBitmap (himlSrc->cx, himlSrc->cy, 1,
633 1, NULL);
635 /* copy (and stretch) destination to temporary bitmaps.(save) */
636 /* image */
637 SelectObject (hdcBmp, hbmTempImage);
638 StretchBlt (hdcBmp, 0, 0, himlSrc->cx, himlSrc->cy,
639 himlDst->hdcImage, ptDst.x, ptDst.y, himlDst->cx, himlDst->cy,
640 SRCCOPY);
641 /* mask */
642 SelectObject (hdcBmp, hbmTempMask);
643 StretchBlt (hdcBmp, 0, 0, himlSrc->cx, himlSrc->cy,
644 himlDst->hdcMask, ptDst.x, ptDst.y, himlDst->cx, himlDst->cy,
645 SRCCOPY);
647 /* copy (and stretch) source to destination */
648 /* image */
649 StretchBlt (himlDst->hdcImage, ptDst.x, ptDst.y, himlDst->cx, himlDst->cy,
650 himlSrc->hdcImage, ptSrc.x, ptSrc.y, himlSrc->cx, himlSrc->cy,
651 SRCCOPY);
652 /* mask */
653 StretchBlt (himlDst->hdcMask, ptDst.x, ptDst.y, himlDst->cx, himlDst->cy,
654 himlSrc->hdcMask, ptSrc.x, ptSrc.y, himlSrc->cx, himlSrc->cy,
655 SRCCOPY);
657 /* copy (without stretching) temporary bitmaps to source (restore) */
658 /* mask */
659 BitBlt (himlSrc->hdcMask, ptSrc.x, ptSrc.y, himlSrc->cx, himlSrc->cy,
660 hdcBmp, 0, 0, SRCCOPY);
662 /* image */
663 BitBlt (himlSrc->hdcImage, ptSrc.x, ptSrc.y, himlSrc->cx, himlSrc->cy,
664 hdcBmp, 0, 0, SRCCOPY);
665 /* delete temporary bitmaps */
666 DeleteObject (hbmTempMask);
667 DeleteObject (hbmTempImage);
668 DeleteDC(hdcBmp);
670 else {
671 /* copy image */
672 StretchBlt (himlDst->hdcImage, ptDst.x, ptDst.y, himlDst->cx, himlDst->cy,
673 himlSrc->hdcImage, ptSrc.x, ptSrc.y, himlSrc->cx, himlSrc->cy,
674 SRCCOPY);
676 /* copy mask */
677 StretchBlt (himlDst->hdcMask, ptDst.x, ptDst.y, himlDst->cx, himlDst->cy,
678 himlSrc->hdcMask, ptSrc.x, ptSrc.y, himlSrc->cx, himlSrc->cy,
679 SRCCOPY);
682 return TRUE;
686 /*************************************************************************
687 * ImageList_Create [COMCTL32.@]
689 * Creates a new image list.
691 * PARAMS
692 * cx [I] image height
693 * cy [I] image width
694 * flags [I] creation flags
695 * cInitial [I] initial number of images in the image list
696 * cGrow [I] number of images by which image list grows
698 * RETURNS
699 * Success: Handle to the created image list
700 * Failure: NULL
702 HIMAGELIST WINAPI
703 ImageList_Create (INT cx, INT cy, UINT flags,
704 INT cInitial, INT cGrow)
706 HIMAGELIST himl;
707 INT nCount;
708 HBITMAP hbmTemp;
709 UINT ilc = (flags & 0xFE);
710 static const WORD aBitBlend25[] =
711 {0xAA, 0x00, 0x55, 0x00, 0xAA, 0x00, 0x55, 0x00};
713 static const WORD aBitBlend50[] =
714 {0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA};
716 TRACE("(%d %d 0x%x %d %d)\n", cx, cy, flags, cInitial, cGrow);
718 if (cx <= 0 || cy <= 0) return NULL;
720 /* Create the IImageList interface for the image list */
721 if (FAILED(ImageListImpl_CreateInstance(NULL, &IID_IImageList, (void **)&himl)))
722 return NULL;
724 cGrow = (cGrow < 4) ? 4 : (cGrow + 3) & ~3;
726 himl->cx = cx;
727 himl->cy = cy;
728 himl->flags = flags;
729 himl->cMaxImage = cInitial + 1;
730 himl->cInitial = cInitial;
731 himl->cGrow = cGrow;
732 himl->clrFg = CLR_DEFAULT;
733 himl->clrBk = CLR_NONE;
735 /* initialize overlay mask indices */
736 for (nCount = 0; nCount < MAX_OVERLAYIMAGE; nCount++)
737 himl->nOvlIdx[nCount] = -1;
739 /* Create Image & Mask DCs */
740 himl->hdcImage = CreateCompatibleDC (0);
741 if (!himl->hdcImage)
742 goto cleanup;
743 if (himl->flags & ILC_MASK){
744 himl->hdcMask = CreateCompatibleDC(0);
745 if (!himl->hdcMask)
746 goto cleanup;
749 /* Default to ILC_COLOR4 if none of the ILC_COLOR* flags are specified */
750 if (ilc == ILC_COLOR)
751 ilc = ILC_COLOR4;
753 if (ilc >= ILC_COLOR4 && ilc <= ILC_COLOR32)
754 himl->uBitsPixel = ilc;
755 else
756 himl->uBitsPixel = (UINT)GetDeviceCaps (himl->hdcImage, BITSPIXEL);
758 if (himl->cMaxImage > 0) {
759 himl->hbmImage = ImageList_CreateImage(himl->hdcImage, himl, himl->cMaxImage);
760 SelectObject(himl->hdcImage, himl->hbmImage);
761 } else
762 himl->hbmImage = 0;
764 if ((himl->cMaxImage > 0) && (himl->flags & ILC_MASK)) {
765 SIZE sz;
767 imagelist_get_bitmap_size(himl, himl->cMaxImage, &sz);
768 himl->hbmMask = CreateBitmap (sz.cx, sz.cy, 1, 1, NULL);
769 if (himl->hbmMask == 0) {
770 ERR("Error creating mask bitmap!\n");
771 goto cleanup;
773 SelectObject(himl->hdcMask, himl->hbmMask);
775 else
776 himl->hbmMask = 0;
778 if (ilc == ILC_COLOR32)
779 himl->has_alpha = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, himl->cMaxImage );
780 else
781 himl->has_alpha = NULL;
783 /* create blending brushes */
784 hbmTemp = CreateBitmap (8, 8, 1, 1, aBitBlend25);
785 himl->hbrBlend25 = CreatePatternBrush (hbmTemp);
786 DeleteObject (hbmTemp);
788 hbmTemp = CreateBitmap (8, 8, 1, 1, aBitBlend50);
789 himl->hbrBlend50 = CreatePatternBrush (hbmTemp);
790 DeleteObject (hbmTemp);
792 TRACE("created imagelist %p\n", himl);
793 return himl;
795 cleanup:
796 ImageList_Destroy(himl);
797 return NULL;
801 /*************************************************************************
802 * ImageList_Destroy [COMCTL32.@]
804 * Destroys an image list.
806 * PARAMS
807 * himl [I] handle to image list
809 * RETURNS
810 * Success: TRUE
811 * Failure: FALSE
814 BOOL WINAPI
815 ImageList_Destroy (HIMAGELIST himl)
817 if (!is_valid(himl))
818 return FALSE;
820 IImageList_Release((IImageList *) himl);
821 return TRUE;
825 /*************************************************************************
826 * ImageList_DragEnter [COMCTL32.@]
828 * Locks window update and displays the drag image at the given position.
830 * PARAMS
831 * hwndLock [I] handle of the window that owns the drag image.
832 * x [I] X position of the drag image.
833 * y [I] Y position of the drag image.
835 * RETURNS
836 * Success: TRUE
837 * Failure: FALSE
839 * NOTES
840 * The position of the drag image is relative to the window, not
841 * the client area.
844 BOOL WINAPI
845 ImageList_DragEnter (HWND hwndLock, INT x, INT y)
847 TRACE("(hwnd=%p x=%d y=%d)\n", hwndLock, x, y);
849 if (!is_valid(InternalDrag.himl))
850 return FALSE;
852 if (hwndLock)
853 InternalDrag.hwnd = hwndLock;
854 else
855 InternalDrag.hwnd = GetDesktopWindow ();
857 InternalDrag.x = x;
858 InternalDrag.y = y;
860 /* draw the drag image and save the background */
861 if (!ImageList_DragShowNolock(TRUE)) {
862 return FALSE;
865 return TRUE;
869 /*************************************************************************
870 * ImageList_DragLeave [COMCTL32.@]
872 * Unlocks window update and hides the drag image.
874 * PARAMS
875 * hwndLock [I] handle of the window that owns the drag image.
877 * RETURNS
878 * Success: TRUE
879 * Failure: FALSE
882 BOOL WINAPI
883 ImageList_DragLeave (HWND hwndLock)
885 /* As we don't save drag info in the window this can lead to problems if
886 an app does not supply the same window as DragEnter */
887 /* if (hwndLock)
888 InternalDrag.hwnd = hwndLock;
889 else
890 InternalDrag.hwnd = GetDesktopWindow (); */
891 if(!hwndLock)
892 hwndLock = GetDesktopWindow();
893 if(InternalDrag.hwnd != hwndLock)
894 FIXME("DragLeave hWnd != DragEnter hWnd\n");
896 ImageList_DragShowNolock (FALSE);
898 return TRUE;
902 /*************************************************************************
903 * ImageList_InternalDragDraw [Internal]
905 * Draws the drag image.
907 * PARAMS
908 * hdc [I] device context to draw into.
909 * x [I] X position of the drag image.
910 * y [I] Y position of the drag image.
912 * RETURNS
913 * Success: TRUE
914 * Failure: FALSE
916 * NOTES
917 * The position of the drag image is relative to the window, not
918 * the client area.
922 static inline void
923 ImageList_InternalDragDraw (HDC hdc, INT x, INT y)
925 IMAGELISTDRAWPARAMS imldp;
927 ZeroMemory (&imldp, sizeof(imldp));
928 imldp.cbSize = sizeof(imldp);
929 imldp.himl = InternalDrag.himl;
930 imldp.i = 0;
931 imldp.hdcDst = hdc,
932 imldp.x = x;
933 imldp.y = y;
934 imldp.rgbBk = CLR_DEFAULT;
935 imldp.rgbFg = CLR_DEFAULT;
936 imldp.fStyle = ILD_NORMAL;
937 imldp.fState = ILS_ALPHA;
938 imldp.Frame = 192;
939 ImageList_DrawIndirect (&imldp);
942 /*************************************************************************
943 * ImageList_DragMove [COMCTL32.@]
945 * Moves the drag image.
947 * PARAMS
948 * x [I] X position of the drag image.
949 * y [I] Y position of the drag image.
951 * RETURNS
952 * Success: TRUE
953 * Failure: FALSE
955 * NOTES
956 * The position of the drag image is relative to the window, not
957 * the client area.
960 BOOL WINAPI
961 ImageList_DragMove (INT x, INT y)
963 TRACE("(x=%d y=%d)\n", x, y);
965 if (!is_valid(InternalDrag.himl))
966 return FALSE;
968 /* draw/update the drag image */
969 if (InternalDrag.bShow) {
970 HDC hdcDrag;
971 HDC hdcOffScreen;
972 HDC hdcBg;
973 HBITMAP hbmOffScreen;
974 INT origNewX, origNewY;
975 INT origOldX, origOldY;
976 INT origRegX, origRegY;
977 INT sizeRegX, sizeRegY;
980 /* calculate the update region */
981 origNewX = x - InternalDrag.dxHotspot;
982 origNewY = y - InternalDrag.dyHotspot;
983 origOldX = InternalDrag.x - InternalDrag.dxHotspot;
984 origOldY = InternalDrag.y - InternalDrag.dyHotspot;
985 origRegX = min(origNewX, origOldX);
986 origRegY = min(origNewY, origOldY);
987 sizeRegX = InternalDrag.himl->cx + abs(x - InternalDrag.x);
988 sizeRegY = InternalDrag.himl->cy + abs(y - InternalDrag.y);
990 hdcDrag = GetDCEx(InternalDrag.hwnd, 0,
991 DCX_WINDOW | DCX_CACHE | DCX_LOCKWINDOWUPDATE);
992 hdcOffScreen = CreateCompatibleDC(hdcDrag);
993 hdcBg = CreateCompatibleDC(hdcDrag);
995 hbmOffScreen = CreateCompatibleBitmap(hdcDrag, sizeRegX, sizeRegY);
996 SelectObject(hdcOffScreen, hbmOffScreen);
997 SelectObject(hdcBg, InternalDrag.hbmBg);
999 /* get the actual background of the update region */
1000 BitBlt(hdcOffScreen, 0, 0, sizeRegX, sizeRegY, hdcDrag,
1001 origRegX, origRegY, SRCCOPY);
1002 /* erase the old image */
1003 BitBlt(hdcOffScreen, origOldX - origRegX, origOldY - origRegY,
1004 InternalDrag.himl->cx, InternalDrag.himl->cy, hdcBg, 0, 0,
1005 SRCCOPY);
1006 /* save the background */
1007 BitBlt(hdcBg, 0, 0, InternalDrag.himl->cx, InternalDrag.himl->cy,
1008 hdcOffScreen, origNewX - origRegX, origNewY - origRegY, SRCCOPY);
1009 /* draw the image */
1010 ImageList_InternalDragDraw(hdcOffScreen, origNewX - origRegX,
1011 origNewY - origRegY);
1012 /* draw the update region to the screen */
1013 BitBlt(hdcDrag, origRegX, origRegY, sizeRegX, sizeRegY,
1014 hdcOffScreen, 0, 0, SRCCOPY);
1016 DeleteDC(hdcBg);
1017 DeleteDC(hdcOffScreen);
1018 DeleteObject(hbmOffScreen);
1019 ReleaseDC(InternalDrag.hwnd, hdcDrag);
1022 /* update the image position */
1023 InternalDrag.x = x;
1024 InternalDrag.y = y;
1026 return TRUE;
1030 /*************************************************************************
1031 * ImageList_DragShowNolock [COMCTL32.@]
1033 * Shows or hides the drag image.
1035 * PARAMS
1036 * bShow [I] TRUE shows the drag image, FALSE hides it.
1038 * RETURNS
1039 * Success: TRUE
1040 * Failure: FALSE
1043 BOOL WINAPI
1044 ImageList_DragShowNolock (BOOL bShow)
1046 HDC hdcDrag;
1047 HDC hdcBg;
1048 INT x, y;
1050 if (!is_valid(InternalDrag.himl))
1051 return FALSE;
1053 TRACE("bShow=0x%X!\n", bShow);
1055 /* DragImage is already visible/hidden */
1056 if ((InternalDrag.bShow && bShow) || (!InternalDrag.bShow && !bShow)) {
1057 return FALSE;
1060 /* position of the origin of the DragImage */
1061 x = InternalDrag.x - InternalDrag.dxHotspot;
1062 y = InternalDrag.y - InternalDrag.dyHotspot;
1064 hdcDrag = GetDCEx (InternalDrag.hwnd, 0,
1065 DCX_WINDOW | DCX_CACHE | DCX_LOCKWINDOWUPDATE);
1066 if (!hdcDrag) {
1067 return FALSE;
1070 hdcBg = CreateCompatibleDC(hdcDrag);
1071 if (!InternalDrag.hbmBg) {
1072 InternalDrag.hbmBg = CreateCompatibleBitmap(hdcDrag,
1073 InternalDrag.himl->cx, InternalDrag.himl->cy);
1075 SelectObject(hdcBg, InternalDrag.hbmBg);
1077 if (bShow) {
1078 /* save the background */
1079 BitBlt(hdcBg, 0, 0, InternalDrag.himl->cx, InternalDrag.himl->cy,
1080 hdcDrag, x, y, SRCCOPY);
1081 /* show the image */
1082 ImageList_InternalDragDraw(hdcDrag, x, y);
1083 } else {
1084 /* hide the image */
1085 BitBlt(hdcDrag, x, y, InternalDrag.himl->cx, InternalDrag.himl->cy,
1086 hdcBg, 0, 0, SRCCOPY);
1089 InternalDrag.bShow = !InternalDrag.bShow;
1091 DeleteDC(hdcBg);
1092 ReleaseDC (InternalDrag.hwnd, hdcDrag);
1093 return TRUE;
1097 /*************************************************************************
1098 * ImageList_Draw [COMCTL32.@]
1100 * Draws an image.
1102 * PARAMS
1103 * himl [I] handle to image list
1104 * i [I] image index
1105 * hdc [I] handle to device context
1106 * x [I] x position
1107 * y [I] y position
1108 * fStyle [I] drawing flags
1110 * RETURNS
1111 * Success: TRUE
1112 * Failure: FALSE
1114 * SEE
1115 * ImageList_DrawEx.
1118 BOOL WINAPI
1119 ImageList_Draw (HIMAGELIST himl, INT i, HDC hdc, INT x, INT y, UINT fStyle)
1121 return ImageList_DrawEx (himl, i, hdc, x, y, 0, 0,
1122 CLR_DEFAULT, CLR_DEFAULT, fStyle);
1126 /*************************************************************************
1127 * ImageList_DrawEx [COMCTL32.@]
1129 * Draws an image and allows to use extended drawing features.
1131 * PARAMS
1132 * himl [I] handle to image list
1133 * i [I] image index
1134 * hdc [I] handle to device context
1135 * x [I] X position
1136 * y [I] Y position
1137 * dx [I] X offset
1138 * dy [I] Y offset
1139 * rgbBk [I] background color
1140 * rgbFg [I] foreground color
1141 * fStyle [I] drawing flags
1143 * RETURNS
1144 * Success: TRUE
1145 * Failure: FALSE
1147 * NOTES
1148 * Calls ImageList_DrawIndirect.
1150 * SEE
1151 * ImageList_DrawIndirect.
1154 BOOL WINAPI
1155 ImageList_DrawEx (HIMAGELIST himl, INT i, HDC hdc, INT x, INT y,
1156 INT dx, INT dy, COLORREF rgbBk, COLORREF rgbFg,
1157 UINT fStyle)
1159 IMAGELISTDRAWPARAMS imldp;
1161 ZeroMemory (&imldp, sizeof(imldp));
1162 imldp.cbSize = sizeof(imldp);
1163 imldp.himl = himl;
1164 imldp.i = i;
1165 imldp.hdcDst = hdc,
1166 imldp.x = x;
1167 imldp.y = y;
1168 imldp.cx = dx;
1169 imldp.cy = dy;
1170 imldp.rgbBk = rgbBk;
1171 imldp.rgbFg = rgbFg;
1172 imldp.fStyle = fStyle;
1174 return ImageList_DrawIndirect (&imldp);
1178 static BOOL alpha_blend_image( HIMAGELIST himl, HDC dest_dc, int dest_x, int dest_y,
1179 int src_x, int src_y, int cx, int cy, BLENDFUNCTION func,
1180 UINT style, COLORREF blend_col )
1182 BOOL ret = FALSE;
1183 HDC hdc;
1184 HBITMAP bmp = 0, mask = 0;
1185 BITMAPINFO *info;
1186 void *bits, *mask_bits;
1187 unsigned int *ptr;
1188 int i, j;
1190 if (!(hdc = CreateCompatibleDC( 0 ))) return FALSE;
1191 if (!(info = HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET( BITMAPINFO, bmiColors[256] )))) goto done;
1192 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1193 info->bmiHeader.biWidth = cx;
1194 info->bmiHeader.biHeight = cy;
1195 info->bmiHeader.biPlanes = 1;
1196 info->bmiHeader.biBitCount = 32;
1197 info->bmiHeader.biCompression = BI_RGB;
1198 info->bmiHeader.biSizeImage = cx * cy * 4;
1199 info->bmiHeader.biXPelsPerMeter = 0;
1200 info->bmiHeader.biYPelsPerMeter = 0;
1201 info->bmiHeader.biClrUsed = 0;
1202 info->bmiHeader.biClrImportant = 0;
1203 if (!(bmp = CreateDIBSection( himl->hdcImage, info, DIB_RGB_COLORS, &bits, 0, 0 ))) goto done;
1204 SelectObject( hdc, bmp );
1205 BitBlt( hdc, 0, 0, cx, cy, himl->hdcImage, src_x, src_y, SRCCOPY );
1207 if (blend_col != CLR_NONE)
1209 BYTE r = GetRValue( blend_col );
1210 BYTE g = GetGValue( blend_col );
1211 BYTE b = GetBValue( blend_col );
1213 if (style & ILD_BLEND25)
1215 for (i = 0, ptr = bits; i < cx * cy; i++, ptr++)
1216 *ptr = ((*ptr & 0xff000000) |
1217 ((((*ptr & 0x00ff0000) * 3 + (r << 16)) / 4) & 0x00ff0000) |
1218 ((((*ptr & 0x0000ff00) * 3 + (g << 8)) / 4) & 0x0000ff00) |
1219 ((((*ptr & 0x000000ff) * 3 + (b << 0)) / 4) & 0x000000ff));
1221 else if (style & ILD_BLEND50)
1223 for (i = 0, ptr = bits; i < cx * cy; i++, ptr++)
1224 *ptr = ((*ptr & 0xff000000) |
1225 ((((*ptr & 0x00ff0000) + (r << 16)) / 2) & 0x00ff0000) |
1226 ((((*ptr & 0x0000ff00) + (g << 8)) / 2) & 0x0000ff00) |
1227 ((((*ptr & 0x000000ff) + (b << 0)) / 2) & 0x000000ff));
1231 if (himl->has_alpha) /* we already have an alpha channel in this case */
1233 /* pre-multiply by the alpha channel */
1234 for (i = 0, ptr = bits; i < cx * cy; i++, ptr++)
1236 DWORD alpha = *ptr >> 24;
1237 *ptr = ((*ptr & 0xff000000) |
1238 (((*ptr & 0x00ff0000) * alpha / 255) & 0x00ff0000) |
1239 (((*ptr & 0x0000ff00) * alpha / 255) & 0x0000ff00) |
1240 (((*ptr & 0x000000ff) * alpha / 255)));
1243 else if (himl->hbmMask)
1245 unsigned int width_bytes = (cx + 31) / 32 * 4;
1246 /* generate alpha channel from the mask */
1247 info->bmiHeader.biBitCount = 1;
1248 info->bmiHeader.biSizeImage = width_bytes * cy;
1249 info->bmiColors[0].rgbRed = 0;
1250 info->bmiColors[0].rgbGreen = 0;
1251 info->bmiColors[0].rgbBlue = 0;
1252 info->bmiColors[0].rgbReserved = 0;
1253 info->bmiColors[1].rgbRed = 0xff;
1254 info->bmiColors[1].rgbGreen = 0xff;
1255 info->bmiColors[1].rgbBlue = 0xff;
1256 info->bmiColors[1].rgbReserved = 0;
1257 if (!(mask = CreateDIBSection( himl->hdcMask, info, DIB_RGB_COLORS, &mask_bits, 0, 0 )))
1258 goto done;
1259 SelectObject( hdc, mask );
1260 BitBlt( hdc, 0, 0, cx, cy, himl->hdcMask, src_x, src_y, SRCCOPY );
1261 SelectObject( hdc, bmp );
1262 for (i = 0, ptr = bits; i < cy; i++)
1263 for (j = 0; j < cx; j++, ptr++)
1264 if ((((BYTE *)mask_bits)[i * width_bytes + j / 8] << (j % 8)) & 0x80) *ptr = 0;
1265 else *ptr |= 0xff000000;
1268 ret = GdiAlphaBlend( dest_dc, dest_x, dest_y, cx, cy, hdc, 0, 0, cx, cy, func );
1270 done:
1271 DeleteDC( hdc );
1272 if (bmp) DeleteObject( bmp );
1273 if (mask) DeleteObject( mask );
1274 HeapFree( GetProcessHeap(), 0, info );
1275 return ret;
1278 /*************************************************************************
1279 * ImageList_DrawIndirect [COMCTL32.@]
1281 * Draws an image using various parameters specified in pimldp.
1283 * PARAMS
1284 * pimldp [I] pointer to IMAGELISTDRAWPARAMS structure.
1286 * RETURNS
1287 * Success: TRUE
1288 * Failure: FALSE
1291 BOOL WINAPI
1292 ImageList_DrawIndirect (IMAGELISTDRAWPARAMS *pimldp)
1294 INT cx, cy, nOvlIdx;
1295 DWORD fState, dwRop;
1296 UINT fStyle;
1297 COLORREF oldImageBk, oldImageFg;
1298 HDC hImageDC, hImageListDC, hMaskListDC;
1299 HBITMAP hImageBmp, hOldImageBmp, hBlendMaskBmp;
1300 BOOL bIsTransparent, bBlend, bResult = FALSE, bMask;
1301 HIMAGELIST himl;
1302 HBRUSH hOldBrush;
1303 POINT pt;
1304 BOOL has_alpha;
1306 if (!pimldp || !(himl = pimldp->himl)) return FALSE;
1307 if (!is_valid(himl)) return FALSE;
1308 if ((pimldp->i < 0) || (pimldp->i >= himl->cCurImage)) return FALSE;
1310 imagelist_point_from_index( himl, pimldp->i, &pt );
1311 pt.x += pimldp->xBitmap;
1312 pt.y += pimldp->yBitmap;
1314 fState = pimldp->cbSize < sizeof(IMAGELISTDRAWPARAMS) ? ILS_NORMAL : pimldp->fState;
1315 fStyle = pimldp->fStyle & ~ILD_OVERLAYMASK;
1316 cx = (pimldp->cx == 0) ? himl->cx : pimldp->cx;
1317 cy = (pimldp->cy == 0) ? himl->cy : pimldp->cy;
1319 bIsTransparent = (fStyle & ILD_TRANSPARENT);
1320 if( pimldp->rgbBk == CLR_NONE )
1321 bIsTransparent = TRUE;
1322 if( ( pimldp->rgbBk == CLR_DEFAULT ) && ( himl->clrBk == CLR_NONE ) )
1323 bIsTransparent = TRUE;
1324 bMask = (himl->flags & ILC_MASK) && (fStyle & ILD_MASK) ;
1325 bBlend = (fStyle & (ILD_BLEND25 | ILD_BLEND50) ) && !bMask;
1327 TRACE("himl(%p) hbmMask(%p) iImage(%d) x(%d) y(%d) cx(%d) cy(%d)\n",
1328 himl, himl->hbmMask, pimldp->i, pimldp->x, pimldp->y, cx, cy);
1330 /* we will use these DCs to access the images and masks in the ImageList */
1331 hImageListDC = himl->hdcImage;
1332 hMaskListDC = himl->hdcMask;
1334 /* these will accumulate the image and mask for the image we're drawing */
1335 hImageDC = CreateCompatibleDC( pimldp->hdcDst );
1336 hImageBmp = CreateCompatibleBitmap( pimldp->hdcDst, cx, cy );
1337 hBlendMaskBmp = bBlend ? CreateBitmap(cx, cy, 1, 1, NULL) : 0;
1339 /* Create a compatible DC. */
1340 if (!hImageListDC || !hImageDC || !hImageBmp ||
1341 (bBlend && !hBlendMaskBmp) || (himl->hbmMask && !hMaskListDC))
1342 goto cleanup;
1344 hOldImageBmp = SelectObject(hImageDC, hImageBmp);
1347 * To obtain a transparent look, background color should be set
1348 * to white and foreground color to black when blitting the
1349 * monochrome mask.
1351 oldImageFg = SetTextColor( hImageDC, RGB( 0, 0, 0 ) );
1352 oldImageBk = SetBkColor( hImageDC, RGB( 0xff, 0xff, 0xff ) );
1354 has_alpha = (himl->has_alpha && himl->has_alpha[pimldp->i]);
1355 if (!bMask && (has_alpha || (fState & ILS_ALPHA)))
1357 COLORREF colour, blend_col = CLR_NONE;
1358 BLENDFUNCTION func;
1360 if (bBlend)
1362 blend_col = pimldp->rgbFg;
1363 if (blend_col == CLR_DEFAULT) blend_col = GetSysColor( COLOR_HIGHLIGHT );
1364 else if (blend_col == CLR_NONE) blend_col = GetTextColor( pimldp->hdcDst );
1367 func.BlendOp = AC_SRC_OVER;
1368 func.BlendFlags = 0;
1369 func.SourceConstantAlpha = (fState & ILS_ALPHA) ? pimldp->Frame : 255;
1370 func.AlphaFormat = AC_SRC_ALPHA;
1372 if (bIsTransparent)
1374 bResult = alpha_blend_image( himl, pimldp->hdcDst, pimldp->x, pimldp->y,
1375 pt.x, pt.y, cx, cy, func, fStyle, blend_col );
1376 goto end;
1378 colour = pimldp->rgbBk;
1379 if (colour == CLR_DEFAULT) colour = himl->clrBk;
1380 if (colour == CLR_NONE) colour = GetBkColor( pimldp->hdcDst );
1382 hOldBrush = SelectObject (hImageDC, CreateSolidBrush (colour));
1383 PatBlt( hImageDC, 0, 0, cx, cy, PATCOPY );
1384 alpha_blend_image( himl, hImageDC, 0, 0, pt.x, pt.y, cx, cy, func, fStyle, blend_col );
1385 DeleteObject (SelectObject (hImageDC, hOldBrush));
1386 bResult = BitBlt( pimldp->hdcDst, pimldp->x, pimldp->y, cx, cy, hImageDC, 0, 0, SRCCOPY );
1387 goto end;
1391 * Draw the initial image
1393 if( bMask ) {
1394 if (himl->hbmMask) {
1395 hOldBrush = SelectObject (hImageDC, CreateSolidBrush (GetTextColor(pimldp->hdcDst)));
1396 PatBlt( hImageDC, 0, 0, cx, cy, PATCOPY );
1397 BitBlt(hImageDC, 0, 0, cx, cy, hMaskListDC, pt.x, pt.y, SRCPAINT);
1398 DeleteObject (SelectObject (hImageDC, hOldBrush));
1399 if( bIsTransparent )
1401 BitBlt ( pimldp->hdcDst, pimldp->x, pimldp->y, cx, cy, hImageDC, 0, 0, SRCAND);
1402 bResult = TRUE;
1403 goto end;
1405 } else {
1406 hOldBrush = SelectObject (hImageDC, GetStockObject(BLACK_BRUSH));
1407 PatBlt( hImageDC, 0, 0, cx, cy, PATCOPY);
1408 SelectObject(hImageDC, hOldBrush);
1410 } else {
1411 /* blend the image with the needed solid background */
1412 COLORREF colour = RGB(0,0,0);
1414 if( !bIsTransparent )
1416 colour = pimldp->rgbBk;
1417 if( colour == CLR_DEFAULT )
1418 colour = himl->clrBk;
1419 if( colour == CLR_NONE )
1420 colour = GetBkColor(pimldp->hdcDst);
1423 hOldBrush = SelectObject (hImageDC, CreateSolidBrush (colour));
1424 PatBlt( hImageDC, 0, 0, cx, cy, PATCOPY );
1425 if (himl->hbmMask)
1427 BitBlt( hImageDC, 0, 0, cx, cy, hMaskListDC, pt.x, pt.y, SRCAND );
1428 BitBlt( hImageDC, 0, 0, cx, cy, hImageListDC, pt.x, pt.y, SRCPAINT );
1430 else
1431 BitBlt( hImageDC, 0, 0, cx, cy, hImageListDC, pt.x, pt.y, SRCCOPY);
1432 DeleteObject (SelectObject (hImageDC, hOldBrush));
1435 /* Time for blending, if required */
1436 if (bBlend) {
1437 HBRUSH hBlendBrush;
1438 COLORREF clrBlend = pimldp->rgbFg;
1439 HDC hBlendMaskDC = hImageListDC;
1440 HBITMAP hOldBitmap;
1442 /* Create the blend Mask */
1443 hOldBitmap = SelectObject(hBlendMaskDC, hBlendMaskBmp);
1444 hBlendBrush = fStyle & ILD_BLEND50 ? himl->hbrBlend50 : himl->hbrBlend25;
1445 hOldBrush = SelectObject(hBlendMaskDC, hBlendBrush);
1446 PatBlt(hBlendMaskDC, 0, 0, cx, cy, PATCOPY);
1447 SelectObject(hBlendMaskDC, hOldBrush);
1449 /* Modify the blend mask if an Image Mask exist */
1450 if(himl->hbmMask) {
1451 BitBlt(hBlendMaskDC, 0, 0, cx, cy, hMaskListDC, pt.x, pt.y, 0x220326); /* NOTSRCAND */
1452 BitBlt(hBlendMaskDC, 0, 0, cx, cy, hBlendMaskDC, 0, 0, NOTSRCCOPY);
1455 /* now apply blend to the current image given the BlendMask */
1456 if (clrBlend == CLR_DEFAULT) clrBlend = GetSysColor (COLOR_HIGHLIGHT);
1457 else if (clrBlend == CLR_NONE) clrBlend = GetTextColor (pimldp->hdcDst);
1458 hOldBrush = SelectObject (hImageDC, CreateSolidBrush(clrBlend));
1459 BitBlt (hImageDC, 0, 0, cx, cy, hBlendMaskDC, 0, 0, 0xB8074A); /* PSDPxax */
1460 DeleteObject(SelectObject(hImageDC, hOldBrush));
1461 SelectObject(hBlendMaskDC, hOldBitmap);
1464 /* Now do the overlay image, if any */
1465 nOvlIdx = (pimldp->fStyle & ILD_OVERLAYMASK) >> 8;
1466 if ( (nOvlIdx >= 1) && (nOvlIdx <= MAX_OVERLAYIMAGE)) {
1467 nOvlIdx = himl->nOvlIdx[nOvlIdx - 1];
1468 if ((nOvlIdx >= 0) && (nOvlIdx < himl->cCurImage)) {
1469 POINT ptOvl;
1470 imagelist_point_from_index( himl, nOvlIdx, &ptOvl );
1471 ptOvl.x += pimldp->xBitmap;
1472 if (himl->hbmMask && !(fStyle & ILD_IMAGE))
1473 BitBlt (hImageDC, 0, 0, cx, cy, hMaskListDC, ptOvl.x, ptOvl.y, SRCAND);
1474 BitBlt (hImageDC, 0, 0, cx, cy, hImageListDC, ptOvl.x, ptOvl.y, SRCPAINT);
1478 if (fState & ILS_SATURATE) FIXME("ILS_SATURATE: unimplemented!\n");
1479 if (fState & ILS_GLOW) FIXME("ILS_GLOW: unimplemented!\n");
1480 if (fState & ILS_SHADOW) FIXME("ILS_SHADOW: unimplemented!\n");
1482 if (fStyle & ILD_PRESERVEALPHA) FIXME("ILD_PRESERVEALPHA: unimplemented!\n");
1483 if (fStyle & ILD_SCALE) FIXME("ILD_SCALE: unimplemented!\n");
1484 if (fStyle & ILD_DPISCALE) FIXME("ILD_DPISCALE: unimplemented!\n");
1486 /* now copy the image to the screen */
1487 dwRop = SRCCOPY;
1488 if (himl->hbmMask && bIsTransparent ) {
1489 COLORREF oldDstFg = SetTextColor(pimldp->hdcDst, RGB( 0, 0, 0 ) );
1490 COLORREF oldDstBk = SetBkColor(pimldp->hdcDst, RGB( 0xff, 0xff, 0xff ));
1491 BitBlt (pimldp->hdcDst, pimldp->x, pimldp->y, cx, cy, hMaskListDC, pt.x, pt.y, SRCAND);
1492 SetBkColor(pimldp->hdcDst, oldDstBk);
1493 SetTextColor(pimldp->hdcDst, oldDstFg);
1494 dwRop = SRCPAINT;
1496 if (fStyle & ILD_ROP) dwRop = pimldp->dwRop;
1497 BitBlt (pimldp->hdcDst, pimldp->x, pimldp->y, cx, cy, hImageDC, 0, 0, dwRop);
1499 bResult = TRUE;
1500 end:
1501 /* cleanup the mess */
1502 SetBkColor(hImageDC, oldImageBk);
1503 SetTextColor(hImageDC, oldImageFg);
1504 SelectObject(hImageDC, hOldImageBmp);
1505 cleanup:
1506 DeleteObject(hBlendMaskBmp);
1507 DeleteObject(hImageBmp);
1508 DeleteDC(hImageDC);
1510 return bResult;
1514 /*************************************************************************
1515 * ImageList_Duplicate [COMCTL32.@]
1517 * Duplicates an image list.
1519 * PARAMS
1520 * himlSrc [I] source image list handle
1522 * RETURNS
1523 * Success: Handle of duplicated image list.
1524 * Failure: NULL
1527 HIMAGELIST WINAPI
1528 ImageList_Duplicate (HIMAGELIST himlSrc)
1530 HIMAGELIST himlDst;
1532 if (!is_valid(himlSrc)) {
1533 ERR("Invalid image list handle!\n");
1534 return NULL;
1537 himlDst = ImageList_Create (himlSrc->cx, himlSrc->cy, himlSrc->flags,
1538 himlSrc->cCurImage, himlSrc->cGrow);
1540 if (himlDst)
1542 SIZE sz;
1544 imagelist_get_bitmap_size(himlSrc, himlSrc->cCurImage, &sz);
1545 BitBlt (himlDst->hdcImage, 0, 0, sz.cx, sz.cy,
1546 himlSrc->hdcImage, 0, 0, SRCCOPY);
1548 if (himlDst->hbmMask)
1549 BitBlt (himlDst->hdcMask, 0, 0, sz.cx, sz.cy,
1550 himlSrc->hdcMask, 0, 0, SRCCOPY);
1552 himlDst->cCurImage = himlSrc->cCurImage;
1553 if (himlSrc->has_alpha && himlDst->has_alpha)
1554 memcpy( himlDst->has_alpha, himlSrc->has_alpha, himlDst->cCurImage );
1556 return himlDst;
1560 /*************************************************************************
1561 * ImageList_EndDrag [COMCTL32.@]
1563 * Finishes a drag operation.
1565 * PARAMS
1566 * no Parameters
1568 * RETURNS
1569 * Success: TRUE
1570 * Failure: FALSE
1573 VOID WINAPI
1574 ImageList_EndDrag (void)
1576 /* cleanup the InternalDrag struct */
1577 InternalDrag.hwnd = 0;
1578 ImageList_Destroy (InternalDrag.himl);
1579 InternalDrag.himl = 0;
1580 InternalDrag.x= 0;
1581 InternalDrag.y= 0;
1582 InternalDrag.dxHotspot = 0;
1583 InternalDrag.dyHotspot = 0;
1584 InternalDrag.bShow = FALSE;
1585 DeleteObject(InternalDrag.hbmBg);
1586 InternalDrag.hbmBg = 0;
1590 /*************************************************************************
1591 * ImageList_GetBkColor [COMCTL32.@]
1593 * Returns the background color of an image list.
1595 * PARAMS
1596 * himl [I] Image list handle.
1598 * RETURNS
1599 * Success: background color
1600 * Failure: CLR_NONE
1603 COLORREF WINAPI
1604 ImageList_GetBkColor (HIMAGELIST himl)
1606 return himl ? himl->clrBk : CLR_NONE;
1610 /*************************************************************************
1611 * ImageList_GetDragImage [COMCTL32.@]
1613 * Returns the handle to the internal drag image list.
1615 * PARAMS
1616 * ppt [O] Pointer to the drag position. Can be NULL.
1617 * pptHotspot [O] Pointer to the position of the hot spot. Can be NULL.
1619 * RETURNS
1620 * Success: Handle of the drag image list.
1621 * Failure: NULL.
1624 HIMAGELIST WINAPI
1625 ImageList_GetDragImage (POINT *ppt, POINT *pptHotspot)
1627 if (is_valid(InternalDrag.himl)) {
1628 if (ppt) {
1629 ppt->x = InternalDrag.x;
1630 ppt->y = InternalDrag.y;
1632 if (pptHotspot) {
1633 pptHotspot->x = InternalDrag.dxHotspot;
1634 pptHotspot->y = InternalDrag.dyHotspot;
1636 return (InternalDrag.himl);
1639 return NULL;
1643 /*************************************************************************
1644 * ImageList_GetFlags [COMCTL32.@]
1646 * Gets the flags of the specified image list.
1648 * PARAMS
1649 * himl [I] Handle to image list
1651 * RETURNS
1652 * Image list flags.
1654 * BUGS
1655 * Stub.
1658 DWORD WINAPI
1659 ImageList_GetFlags(HIMAGELIST himl)
1661 TRACE("%p\n", himl);
1663 return is_valid(himl) ? himl->flags : 0;
1667 /*************************************************************************
1668 * ImageList_GetIcon [COMCTL32.@]
1670 * Creates an icon from a masked image of an image list.
1672 * PARAMS
1673 * himl [I] handle to image list
1674 * i [I] image index
1675 * flags [I] drawing style flags
1677 * RETURNS
1678 * Success: icon handle
1679 * Failure: NULL
1682 HICON WINAPI
1683 ImageList_GetIcon (HIMAGELIST himl, INT i, UINT fStyle)
1685 ICONINFO ii;
1686 HICON hIcon;
1687 HBITMAP hOldDstBitmap;
1688 HDC hdcDst;
1689 POINT pt;
1691 TRACE("%p %d %d\n", himl, i, fStyle);
1692 if (!is_valid(himl) || (i < 0) || (i >= himl->cCurImage)) return NULL;
1694 ii.fIcon = TRUE;
1695 ii.xHotspot = 0;
1696 ii.yHotspot = 0;
1698 /* create colour bitmap */
1699 hdcDst = GetDC(0);
1700 ii.hbmColor = CreateCompatibleBitmap(hdcDst, himl->cx, himl->cy);
1701 ReleaseDC(0, hdcDst);
1703 hdcDst = CreateCompatibleDC(0);
1705 imagelist_point_from_index( himl, i, &pt );
1707 /* draw mask*/
1708 ii.hbmMask = CreateBitmap (himl->cx, himl->cy, 1, 1, NULL);
1709 hOldDstBitmap = SelectObject (hdcDst, ii.hbmMask);
1710 if (himl->hbmMask) {
1711 BitBlt (hdcDst, 0, 0, himl->cx, himl->cy,
1712 himl->hdcMask, pt.x, pt.y, SRCCOPY);
1714 else
1715 PatBlt (hdcDst, 0, 0, himl->cx, himl->cy, BLACKNESS);
1717 /* draw image*/
1718 SelectObject (hdcDst, ii.hbmColor);
1719 BitBlt (hdcDst, 0, 0, himl->cx, himl->cy,
1720 himl->hdcImage, pt.x, pt.y, SRCCOPY);
1723 * CreateIconIndirect requires us to deselect the bitmaps from
1724 * the DCs before calling
1726 SelectObject(hdcDst, hOldDstBitmap);
1728 hIcon = CreateIconIndirect (&ii);
1730 DeleteObject (ii.hbmMask);
1731 DeleteObject (ii.hbmColor);
1732 DeleteDC (hdcDst);
1734 return hIcon;
1738 /*************************************************************************
1739 * ImageList_GetIconSize [COMCTL32.@]
1741 * Retrieves the size of an image in an image list.
1743 * PARAMS
1744 * himl [I] handle to image list
1745 * cx [O] pointer to the image width.
1746 * cy [O] pointer to the image height.
1748 * RETURNS
1749 * Success: TRUE
1750 * Failure: FALSE
1752 * NOTES
1753 * All images in an image list have the same size.
1756 BOOL WINAPI
1757 ImageList_GetIconSize (HIMAGELIST himl, INT *cx, INT *cy)
1759 if (!is_valid(himl) || !cx || !cy)
1760 return FALSE;
1761 if ((himl->cx <= 0) || (himl->cy <= 0))
1762 return FALSE;
1764 *cx = himl->cx;
1765 *cy = himl->cy;
1767 return TRUE;
1771 /*************************************************************************
1772 * ImageList_GetImageCount [COMCTL32.@]
1774 * Returns the number of images in an image list.
1776 * PARAMS
1777 * himl [I] handle to image list
1779 * RETURNS
1780 * Success: Number of images.
1781 * Failure: 0
1784 INT WINAPI
1785 ImageList_GetImageCount (HIMAGELIST himl)
1787 if (!is_valid(himl))
1788 return 0;
1790 return himl->cCurImage;
1794 /*************************************************************************
1795 * ImageList_GetImageInfo [COMCTL32.@]
1797 * Returns information about an image in an image list.
1799 * PARAMS
1800 * himl [I] handle to image list
1801 * i [I] image index
1802 * pImageInfo [O] pointer to the image information
1804 * RETURNS
1805 * Success: TRUE
1806 * Failure: FALSE
1809 BOOL WINAPI
1810 ImageList_GetImageInfo (HIMAGELIST himl, INT i, IMAGEINFO *pImageInfo)
1812 POINT pt;
1814 if (!is_valid(himl) || (pImageInfo == NULL))
1815 return FALSE;
1816 if ((i < 0) || (i >= himl->cCurImage))
1817 return FALSE;
1819 pImageInfo->hbmImage = himl->hbmImage;
1820 pImageInfo->hbmMask = himl->hbmMask;
1822 imagelist_point_from_index( himl, i, &pt );
1823 pImageInfo->rcImage.top = pt.y;
1824 pImageInfo->rcImage.bottom = pt.y + himl->cy;
1825 pImageInfo->rcImage.left = pt.x;
1826 pImageInfo->rcImage.right = pt.x + himl->cx;
1828 return TRUE;
1832 /*************************************************************************
1833 * ImageList_GetImageRect [COMCTL32.@]
1835 * Retrieves the rectangle of the specified image in an image list.
1837 * PARAMS
1838 * himl [I] handle to image list
1839 * i [I] image index
1840 * lpRect [O] pointer to the image rectangle
1842 * RETURNS
1843 * Success: TRUE
1844 * Failure: FALSE
1846 * NOTES
1847 * This is an UNDOCUMENTED function!!!
1850 BOOL WINAPI
1851 ImageList_GetImageRect (HIMAGELIST himl, INT i, LPRECT lpRect)
1853 POINT pt;
1855 if (!is_valid(himl) || (lpRect == NULL))
1856 return FALSE;
1857 if ((i < 0) || (i >= himl->cCurImage))
1858 return FALSE;
1860 imagelist_point_from_index( himl, i, &pt );
1861 lpRect->left = pt.x;
1862 lpRect->top = pt.y;
1863 lpRect->right = pt.x + himl->cx;
1864 lpRect->bottom = pt.y + himl->cy;
1866 return TRUE;
1870 /*************************************************************************
1871 * ImageList_LoadImage [COMCTL32.@]
1872 * ImageList_LoadImageA [COMCTL32.@]
1874 * Creates an image list from a bitmap, icon or cursor.
1876 * See ImageList_LoadImageW.
1879 HIMAGELIST WINAPI
1880 ImageList_LoadImageA (HINSTANCE hi, LPCSTR lpbmp, INT cx, INT cGrow,
1881 COLORREF clrMask, UINT uType, UINT uFlags)
1883 HIMAGELIST himl;
1884 LPWSTR lpbmpW;
1885 DWORD len;
1887 if (IS_INTRESOURCE(lpbmp))
1888 return ImageList_LoadImageW(hi, (LPCWSTR)lpbmp, cx, cGrow, clrMask,
1889 uType, uFlags);
1891 len = MultiByteToWideChar(CP_ACP, 0, lpbmp, -1, NULL, 0);
1892 lpbmpW = Alloc(len * sizeof(WCHAR));
1893 MultiByteToWideChar(CP_ACP, 0, lpbmp, -1, lpbmpW, len);
1895 himl = ImageList_LoadImageW(hi, lpbmpW, cx, cGrow, clrMask, uType, uFlags);
1896 Free (lpbmpW);
1897 return himl;
1901 /*************************************************************************
1902 * ImageList_LoadImageW [COMCTL32.@]
1904 * Creates an image list from a bitmap, icon or cursor.
1906 * PARAMS
1907 * hi [I] instance handle
1908 * lpbmp [I] name or id of the image
1909 * cx [I] width of each image
1910 * cGrow [I] number of images to expand
1911 * clrMask [I] mask color
1912 * uType [I] type of image to load
1913 * uFlags [I] loading flags
1915 * RETURNS
1916 * Success: handle to the loaded image list
1917 * Failure: NULL
1919 * SEE
1920 * LoadImage ()
1923 HIMAGELIST WINAPI
1924 ImageList_LoadImageW (HINSTANCE hi, LPCWSTR lpbmp, INT cx, INT cGrow,
1925 COLORREF clrMask, UINT uType, UINT uFlags)
1927 HIMAGELIST himl = NULL;
1928 HANDLE handle;
1929 INT nImageCount;
1931 handle = LoadImageW (hi, lpbmp, uType, 0, 0, uFlags);
1932 if (!handle) {
1933 WARN("Couldn't load image\n");
1934 return NULL;
1937 if (uType == IMAGE_BITMAP) {
1938 DIBSECTION dib;
1939 UINT color;
1941 if (GetObjectW (handle, sizeof(dib), &dib) == sizeof(BITMAP)) color = ILC_COLOR;
1942 else color = dib.dsBm.bmBitsPixel;
1944 /* To match windows behavior, if cx is set to zero and
1945 the flag DI_DEFAULTSIZE is specified, cx becomes the
1946 system metric value for icons. If the flag is not specified
1947 the function sets the size to the height of the bitmap */
1948 if (cx == 0)
1950 if (uFlags & DI_DEFAULTSIZE)
1951 cx = GetSystemMetrics (SM_CXICON);
1952 else
1953 cx = dib.dsBm.bmHeight;
1956 nImageCount = dib.dsBm.bmWidth / cx;
1958 himl = ImageList_Create (cx, dib.dsBm.bmHeight, ILC_MASK | color, nImageCount, cGrow);
1959 if (!himl) {
1960 DeleteObject (handle);
1961 return NULL;
1963 ImageList_AddMasked (himl, handle, clrMask);
1965 else if ((uType == IMAGE_ICON) || (uType == IMAGE_CURSOR)) {
1966 ICONINFO ii;
1967 BITMAP bmp;
1969 GetIconInfo (handle, &ii);
1970 GetObjectW (ii.hbmColor, sizeof(BITMAP), &bmp);
1971 himl = ImageList_Create (bmp.bmWidth, bmp.bmHeight,
1972 ILC_MASK | ILC_COLOR, 1, cGrow);
1973 if (!himl) {
1974 DeleteObject (ii.hbmColor);
1975 DeleteObject (ii.hbmMask);
1976 DeleteObject (handle);
1977 return NULL;
1979 ImageList_Add (himl, ii.hbmColor, ii.hbmMask);
1980 DeleteObject (ii.hbmColor);
1981 DeleteObject (ii.hbmMask);
1984 DeleteObject (handle);
1986 return himl;
1990 /*************************************************************************
1991 * ImageList_Merge [COMCTL32.@]
1993 * Create an image list containing a merged image from two image lists.
1995 * PARAMS
1996 * himl1 [I] handle to first image list
1997 * i1 [I] first image index
1998 * himl2 [I] handle to second image list
1999 * i2 [I] second image index
2000 * dx [I] X offset of the second image relative to the first.
2001 * dy [I] Y offset of the second image relative to the first.
2003 * RETURNS
2004 * Success: The newly created image list. It contains a single image
2005 * consisting of the second image merged with the first.
2006 * Failure: NULL, if either himl1 or himl2 are invalid.
2008 * NOTES
2009 * - The returned image list should be deleted by the caller using
2010 * ImageList_Destroy() when it is no longer required.
2011 * - If either i1 or i2 are not valid image indices they will be treated
2012 * as a blank image.
2014 HIMAGELIST WINAPI
2015 ImageList_Merge (HIMAGELIST himl1, INT i1, HIMAGELIST himl2, INT i2,
2016 INT dx, INT dy)
2018 HIMAGELIST himlDst = NULL;
2019 INT cxDst, cyDst;
2020 INT xOff1, yOff1, xOff2, yOff2;
2021 POINT pt1, pt2;
2023 TRACE("(himl1=%p i1=%d himl2=%p i2=%d dx=%d dy=%d)\n", himl1, i1, himl2,
2024 i2, dx, dy);
2026 if (!is_valid(himl1) || !is_valid(himl2))
2027 return NULL;
2029 if (dx > 0) {
2030 cxDst = max (himl1->cx, dx + himl2->cx);
2031 xOff1 = 0;
2032 xOff2 = dx;
2034 else if (dx < 0) {
2035 cxDst = max (himl2->cx, himl1->cx - dx);
2036 xOff1 = -dx;
2037 xOff2 = 0;
2039 else {
2040 cxDst = max (himl1->cx, himl2->cx);
2041 xOff1 = 0;
2042 xOff2 = 0;
2045 if (dy > 0) {
2046 cyDst = max (himl1->cy, dy + himl2->cy);
2047 yOff1 = 0;
2048 yOff2 = dy;
2050 else if (dy < 0) {
2051 cyDst = max (himl2->cy, himl1->cy - dy);
2052 yOff1 = -dy;
2053 yOff2 = 0;
2055 else {
2056 cyDst = max (himl1->cy, himl2->cy);
2057 yOff1 = 0;
2058 yOff2 = 0;
2061 himlDst = ImageList_Create (cxDst, cyDst, ILC_MASK | ILC_COLOR, 1, 1);
2063 if (himlDst)
2065 imagelist_point_from_index( himl1, i1, &pt1 );
2066 imagelist_point_from_index( himl2, i2, &pt2 );
2068 /* copy image */
2069 BitBlt (himlDst->hdcImage, 0, 0, cxDst, cyDst, himl1->hdcImage, 0, 0, BLACKNESS);
2070 if (i1 >= 0 && i1 < himl1->cCurImage)
2071 BitBlt (himlDst->hdcImage, xOff1, yOff1, himl1->cx, himl1->cy, himl1->hdcImage, pt1.x, pt1.y, SRCCOPY);
2072 if (i2 >= 0 && i2 < himl2->cCurImage)
2074 BitBlt (himlDst->hdcImage, xOff2, yOff2, himl2->cx, himl2->cy, himl2->hdcMask , pt2.x, pt2.y, SRCAND);
2075 BitBlt (himlDst->hdcImage, xOff2, yOff2, himl2->cx, himl2->cy, himl2->hdcImage, pt2.x, pt2.y, SRCPAINT);
2078 /* copy mask */
2079 BitBlt (himlDst->hdcMask, 0, 0, cxDst, cyDst, himl1->hdcMask, 0, 0, WHITENESS);
2080 if (i1 >= 0 && i1 < himl1->cCurImage)
2081 BitBlt (himlDst->hdcMask, xOff1, yOff1, himl1->cx, himl1->cy, himl1->hdcMask, pt1.x, pt1.y, SRCCOPY);
2082 if (i2 >= 0 && i2 < himl2->cCurImage)
2083 BitBlt (himlDst->hdcMask, xOff2, yOff2, himl2->cx, himl2->cy, himl2->hdcMask, pt2.x, pt2.y, SRCAND);
2085 himlDst->cCurImage = 1;
2088 return himlDst;
2092 /* helper for ImageList_Read, see comments below */
2093 static void *read_bitmap(LPSTREAM pstm, BITMAPINFO *bmi)
2095 BITMAPFILEHEADER bmfh;
2096 int bitsperpixel, palspace;
2097 void *bits;
2099 if (FAILED(IStream_Read ( pstm, &bmfh, sizeof(bmfh), NULL)))
2100 return NULL;
2102 if (bmfh.bfType != (('M'<<8)|'B'))
2103 return NULL;
2105 if (FAILED(IStream_Read ( pstm, &bmi->bmiHeader, sizeof(bmi->bmiHeader), NULL)))
2106 return NULL;
2108 if ((bmi->bmiHeader.biSize != sizeof(bmi->bmiHeader)))
2109 return NULL;
2111 TRACE("width %u, height %u, planes %u, bpp %u\n",
2112 bmi->bmiHeader.biWidth, bmi->bmiHeader.biHeight,
2113 bmi->bmiHeader.biPlanes, bmi->bmiHeader.biBitCount);
2115 bitsperpixel = bmi->bmiHeader.biPlanes * bmi->bmiHeader.biBitCount;
2116 if (bitsperpixel<=8)
2117 palspace = (1<<bitsperpixel)*sizeof(RGBQUAD);
2118 else
2119 palspace = 0;
2121 bmi->bmiHeader.biSizeImage = get_dib_image_size( bmi );
2123 /* read the palette right after the end of the bitmapinfoheader */
2124 if (palspace && FAILED(IStream_Read(pstm, bmi->bmiColors, palspace, NULL)))
2125 return NULL;
2127 bits = Alloc(bmi->bmiHeader.biSizeImage);
2128 if (!bits) return NULL;
2130 if (FAILED(IStream_Read(pstm, bits, bmi->bmiHeader.biSizeImage, NULL)))
2132 Free(bits);
2133 return NULL;
2135 return bits;
2138 /*************************************************************************
2139 * ImageList_Read [COMCTL32.@]
2141 * Reads an image list from a stream.
2143 * PARAMS
2144 * pstm [I] pointer to a stream
2146 * RETURNS
2147 * Success: handle to image list
2148 * Failure: NULL
2150 * The format is like this:
2151 * ILHEAD ilheadstruct;
2153 * for the color image part:
2154 * BITMAPFILEHEADER bmfh;
2155 * BITMAPINFOHEADER bmih;
2156 * only if it has a palette:
2157 * RGBQUAD rgbs[nr_of_paletted_colors];
2159 * BYTE colorbits[imagesize];
2161 * the following only if the ILC_MASK bit is set in ILHEAD.ilFlags:
2162 * BITMAPFILEHEADER bmfh_mask;
2163 * BITMAPINFOHEADER bmih_mask;
2164 * only if it has a palette (it usually does not):
2165 * RGBQUAD rgbs[nr_of_paletted_colors];
2167 * BYTE maskbits[imagesize];
2169 HIMAGELIST WINAPI ImageList_Read (LPSTREAM pstm)
2171 char image_buf[sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * 256];
2172 char mask_buf[sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * 256];
2173 BITMAPINFO *image_info = (BITMAPINFO *)image_buf;
2174 BITMAPINFO *mask_info = (BITMAPINFO *)mask_buf;
2175 void *image_bits, *mask_bits = NULL;
2176 ILHEAD ilHead;
2177 HIMAGELIST himl;
2178 int i;
2180 TRACE("%p\n", pstm);
2182 if (FAILED(IStream_Read (pstm, &ilHead, sizeof(ILHEAD), NULL)))
2183 return NULL;
2184 if (ilHead.usMagic != (('L' << 8) | 'I'))
2185 return NULL;
2186 if (ilHead.usVersion != 0x101) /* probably version? */
2187 return NULL;
2189 TRACE("cx %u, cy %u, flags 0x%04x, cCurImage %u, cMaxImage %u\n",
2190 ilHead.cx, ilHead.cy, ilHead.flags, ilHead.cCurImage, ilHead.cMaxImage);
2192 himl = ImageList_Create(ilHead.cx, ilHead.cy, ilHead.flags, ilHead.cCurImage, ilHead.cMaxImage);
2193 if (!himl)
2194 return NULL;
2196 if (!(image_bits = read_bitmap(pstm, image_info)))
2198 WARN("failed to read bitmap from stream\n");
2199 return NULL;
2201 if (ilHead.flags & ILC_MASK)
2203 if (!(mask_bits = read_bitmap(pstm, mask_info)))
2205 WARN("failed to read mask bitmap from stream\n");
2206 return NULL;
2209 else mask_info = NULL;
2211 if (himl->has_alpha && image_info->bmiHeader.biBitCount == 32)
2213 DWORD *ptr = image_bits;
2214 BYTE *mask_ptr = mask_bits;
2215 int stride = himl->cy * image_info->bmiHeader.biWidth;
2217 if (image_info->bmiHeader.biHeight > 0) /* bottom-up */
2219 ptr += image_info->bmiHeader.biHeight * image_info->bmiHeader.biWidth - stride;
2220 mask_ptr += (image_info->bmiHeader.biHeight * image_info->bmiHeader.biWidth - stride) / 8;
2221 stride = -stride;
2222 image_info->bmiHeader.biHeight = himl->cy;
2224 else image_info->bmiHeader.biHeight = -himl->cy;
2226 for (i = 0; i < ilHead.cCurImage; i += TILE_COUNT)
2228 add_dib_bits( himl, i, min( ilHead.cCurImage - i, TILE_COUNT ),
2229 himl->cx, himl->cy, image_info, mask_info, ptr, mask_ptr );
2230 ptr += stride;
2231 mask_ptr += stride / 8;
2234 else
2236 StretchDIBits( himl->hdcImage, 0, 0, image_info->bmiHeader.biWidth, image_info->bmiHeader.biHeight,
2237 0, 0, image_info->bmiHeader.biWidth, image_info->bmiHeader.biHeight,
2238 image_bits, image_info, DIB_RGB_COLORS, SRCCOPY);
2239 if (mask_info)
2240 StretchDIBits( himl->hdcMask, 0, 0, mask_info->bmiHeader.biWidth, mask_info->bmiHeader.biHeight,
2241 0, 0, mask_info->bmiHeader.biWidth, mask_info->bmiHeader.biHeight,
2242 mask_bits, mask_info, DIB_RGB_COLORS, SRCCOPY);
2244 Free( image_bits );
2245 Free( mask_bits );
2247 himl->cCurImage = ilHead.cCurImage;
2248 himl->cMaxImage = ilHead.cMaxImage;
2250 ImageList_SetBkColor(himl,ilHead.bkcolor);
2251 for (i=0;i<4;i++)
2252 ImageList_SetOverlayImage(himl,ilHead.ovls[i],i+1);
2253 return himl;
2257 /*************************************************************************
2258 * ImageList_Remove [COMCTL32.@]
2260 * Removes an image from an image list
2262 * PARAMS
2263 * himl [I] image list handle
2264 * i [I] image index
2266 * RETURNS
2267 * Success: TRUE
2268 * Failure: FALSE
2270 * FIXME: as the image list storage test shows, native comctl32 simply shifts
2271 * images without creating a new bitmap.
2273 BOOL WINAPI
2274 ImageList_Remove (HIMAGELIST himl, INT i)
2276 HBITMAP hbmNewImage, hbmNewMask;
2277 HDC hdcBmp;
2278 SIZE sz;
2280 TRACE("(himl=%p i=%d)\n", himl, i);
2282 if (!is_valid(himl)) {
2283 ERR("Invalid image list handle!\n");
2284 return FALSE;
2287 if ((i < -1) || (i >= himl->cCurImage)) {
2288 TRACE("index out of range! %d\n", i);
2289 return FALSE;
2292 if (i == -1) {
2293 INT nCount;
2295 /* remove all */
2296 if (himl->cCurImage == 0) {
2297 /* remove all on empty ImageList is allowed */
2298 TRACE("remove all on empty ImageList!\n");
2299 return TRUE;
2302 himl->cMaxImage = himl->cInitial + himl->cGrow - 1;
2303 himl->cCurImage = 0;
2304 for (nCount = 0; nCount < MAX_OVERLAYIMAGE; nCount++)
2305 himl->nOvlIdx[nCount] = -1;
2307 hbmNewImage = ImageList_CreateImage(himl->hdcImage, himl, himl->cMaxImage);
2308 SelectObject (himl->hdcImage, hbmNewImage);
2309 DeleteObject (himl->hbmImage);
2310 himl->hbmImage = hbmNewImage;
2312 if (himl->hbmMask) {
2314 imagelist_get_bitmap_size(himl, himl->cMaxImage, &sz);
2315 hbmNewMask = CreateBitmap (sz.cx, sz.cy, 1, 1, NULL);
2316 SelectObject (himl->hdcMask, hbmNewMask);
2317 DeleteObject (himl->hbmMask);
2318 himl->hbmMask = hbmNewMask;
2321 else {
2322 /* delete one image */
2323 TRACE("Remove single image! %d\n", i);
2325 /* create new bitmap(s) */
2326 TRACE(" - Number of images: %d / %d (Old/New)\n",
2327 himl->cCurImage, himl->cCurImage - 1);
2329 hbmNewImage = ImageList_CreateImage(himl->hdcImage, himl, himl->cMaxImage);
2331 imagelist_get_bitmap_size(himl, himl->cMaxImage, &sz );
2332 if (himl->hbmMask)
2333 hbmNewMask = CreateBitmap (sz.cx, sz.cy, 1, 1, NULL);
2334 else
2335 hbmNewMask = 0; /* Just to keep compiler happy! */
2337 hdcBmp = CreateCompatibleDC (0);
2339 /* copy all images and masks prior to the "removed" image */
2340 if (i > 0) {
2341 TRACE("Pre image copy: Copy %d images\n", i);
2343 SelectObject (hdcBmp, hbmNewImage);
2344 imagelist_copy_images( himl, himl->hdcImage, hdcBmp, 0, i, 0 );
2346 if (himl->hbmMask) {
2347 SelectObject (hdcBmp, hbmNewMask);
2348 imagelist_copy_images( himl, himl->hdcMask, hdcBmp, 0, i, 0 );
2352 /* copy all images and masks behind the removed image */
2353 if (i < himl->cCurImage - 1) {
2354 TRACE("Post image copy!\n");
2356 SelectObject (hdcBmp, hbmNewImage);
2357 imagelist_copy_images( himl, himl->hdcImage, hdcBmp, i + 1,
2358 (himl->cCurImage - i), i );
2360 if (himl->hbmMask) {
2361 SelectObject (hdcBmp, hbmNewMask);
2362 imagelist_copy_images( himl, himl->hdcMask, hdcBmp, i + 1,
2363 (himl->cCurImage - i), i );
2367 DeleteDC (hdcBmp);
2369 /* delete old images and insert new ones */
2370 SelectObject (himl->hdcImage, hbmNewImage);
2371 DeleteObject (himl->hbmImage);
2372 himl->hbmImage = hbmNewImage;
2373 if (himl->hbmMask) {
2374 SelectObject (himl->hdcMask, hbmNewMask);
2375 DeleteObject (himl->hbmMask);
2376 himl->hbmMask = hbmNewMask;
2379 himl->cCurImage--;
2382 return TRUE;
2386 /*************************************************************************
2387 * ImageList_Replace [COMCTL32.@]
2389 * Replaces an image in an image list with a new image.
2391 * PARAMS
2392 * himl [I] handle to image list
2393 * i [I] image index
2394 * hbmImage [I] handle to image bitmap
2395 * hbmMask [I] handle to mask bitmap. Can be NULL.
2397 * RETURNS
2398 * Success: TRUE
2399 * Failure: FALSE
2402 BOOL WINAPI
2403 ImageList_Replace (HIMAGELIST himl, INT i, HBITMAP hbmImage,
2404 HBITMAP hbmMask)
2406 HDC hdcImage;
2407 BITMAP bmp;
2408 POINT pt;
2410 TRACE("%p %d %p %p\n", himl, i, hbmImage, hbmMask);
2412 if (!is_valid(himl)) {
2413 ERR("Invalid image list handle!\n");
2414 return FALSE;
2417 if ((i >= himl->cMaxImage) || (i < 0)) {
2418 ERR("Invalid image index!\n");
2419 return FALSE;
2422 if (!GetObjectW(hbmImage, sizeof(BITMAP), &bmp))
2423 return FALSE;
2425 hdcImage = CreateCompatibleDC (0);
2427 /* Replace Image */
2428 SelectObject (hdcImage, hbmImage);
2430 if (add_with_alpha( himl, hdcImage, i, 1, bmp.bmWidth, bmp.bmHeight, hbmImage, hbmMask ))
2431 goto done;
2433 imagelist_point_from_index(himl, i, &pt);
2434 StretchBlt (himl->hdcImage, pt.x, pt.y, himl->cx, himl->cy,
2435 hdcImage, 0, 0, bmp.bmWidth, bmp.bmHeight, SRCCOPY);
2437 if (himl->hbmMask)
2439 HDC hdcTemp;
2440 HBITMAP hOldBitmapTemp;
2442 hdcTemp = CreateCompatibleDC(0);
2443 hOldBitmapTemp = SelectObject(hdcTemp, hbmMask);
2445 StretchBlt (himl->hdcMask, pt.x, pt.y, himl->cx, himl->cy,
2446 hdcTemp, 0, 0, bmp.bmWidth, bmp.bmHeight, SRCCOPY);
2447 SelectObject(hdcTemp, hOldBitmapTemp);
2448 DeleteDC(hdcTemp);
2450 /* Remove the background from the image
2452 BitBlt (himl->hdcImage, pt.x, pt.y, bmp.bmWidth, bmp.bmHeight,
2453 himl->hdcMask, pt.x, pt.y, 0x220326); /* NOTSRCAND */
2456 done:
2457 DeleteDC (hdcImage);
2459 return TRUE;
2463 /*************************************************************************
2464 * ImageList_ReplaceIcon [COMCTL32.@]
2466 * Replaces an image in an image list using an icon.
2468 * PARAMS
2469 * himl [I] handle to image list
2470 * i [I] image index
2471 * hIcon [I] handle to icon
2473 * RETURNS
2474 * Success: index of the replaced image
2475 * Failure: -1
2478 INT WINAPI
2479 ImageList_ReplaceIcon (HIMAGELIST himl, INT nIndex, HICON hIcon)
2481 HDC hdcImage;
2482 HICON hBestFitIcon;
2483 ICONINFO ii;
2484 BITMAP bmp;
2485 BOOL ret;
2486 POINT pt;
2488 TRACE("(%p %d %p)\n", himl, nIndex, hIcon);
2490 if (!is_valid(himl)) {
2491 ERR("invalid image list\n");
2492 return -1;
2494 if ((nIndex >= himl->cMaxImage) || (nIndex < -1)) {
2495 ERR("invalid image index %d / %d\n", nIndex, himl->cMaxImage);
2496 return -1;
2499 hBestFitIcon = CopyImage(
2500 hIcon, IMAGE_ICON,
2501 himl->cx, himl->cy,
2502 LR_COPYFROMRESOURCE);
2503 /* the above will fail if the icon wasn't loaded from a resource, so try
2504 * again without LR_COPYFROMRESOURCE flag */
2505 if (!hBestFitIcon)
2506 hBestFitIcon = CopyImage(
2507 hIcon, IMAGE_ICON,
2508 himl->cx, himl->cy,
2510 if (!hBestFitIcon)
2511 return -1;
2513 ret = GetIconInfo (hBestFitIcon, &ii);
2514 if (!ret) {
2515 DestroyIcon(hBestFitIcon);
2516 return -1;
2519 ret = GetObjectW (ii.hbmMask, sizeof(BITMAP), &bmp);
2520 if (!ret) {
2521 ERR("couldn't get mask bitmap info\n");
2522 if (ii.hbmColor)
2523 DeleteObject (ii.hbmColor);
2524 if (ii.hbmMask)
2525 DeleteObject (ii.hbmMask);
2526 DestroyIcon(hBestFitIcon);
2527 return -1;
2530 if (nIndex == -1) {
2531 if (himl->cCurImage + 1 > himl->cMaxImage)
2532 IMAGELIST_InternalExpandBitmaps(himl, 1);
2534 nIndex = himl->cCurImage;
2535 himl->cCurImage++;
2538 hdcImage = CreateCompatibleDC (0);
2539 TRACE("hdcImage=%p\n", hdcImage);
2540 if (hdcImage == 0)
2541 ERR("invalid hdcImage!\n");
2543 if (himl->has_alpha)
2545 if (!ii.hbmColor)
2547 UINT height = bmp.bmHeight / 2;
2548 HDC hdcMask = CreateCompatibleDC( 0 );
2549 HBITMAP color = CreateBitmap( bmp.bmWidth, height, 1, 1, NULL );
2550 SelectObject( hdcImage, color );
2551 SelectObject( hdcMask, ii.hbmMask );
2552 BitBlt( hdcImage, 0, 0, bmp.bmWidth, height, hdcMask, 0, height, SRCCOPY );
2553 ret = add_with_alpha( himl, hdcImage, nIndex, 1, bmp.bmWidth, height, color, ii.hbmMask );
2554 DeleteDC( hdcMask );
2555 DeleteObject( color );
2556 if (ret) goto done;
2558 else if (add_with_alpha( himl, hdcImage, nIndex, 1, bmp.bmWidth, bmp.bmHeight,
2559 ii.hbmColor, ii.hbmMask )) goto done;
2562 imagelist_point_from_index(himl, nIndex, &pt);
2564 SetTextColor(himl->hdcImage, RGB(0,0,0));
2565 SetBkColor (himl->hdcImage, RGB(255,255,255));
2567 if (ii.hbmColor)
2569 SelectObject (hdcImage, ii.hbmColor);
2570 StretchBlt (himl->hdcImage, pt.x, pt.y, himl->cx, himl->cy,
2571 hdcImage, 0, 0, bmp.bmWidth, bmp.bmHeight, SRCCOPY);
2572 if (himl->hbmMask)
2574 SelectObject (hdcImage, ii.hbmMask);
2575 StretchBlt (himl->hdcMask, pt.x, pt.y, himl->cx, himl->cy,
2576 hdcImage, 0, 0, bmp.bmWidth, bmp.bmHeight, SRCCOPY);
2579 else
2581 UINT height = bmp.bmHeight / 2;
2582 SelectObject (hdcImage, ii.hbmMask);
2583 StretchBlt (himl->hdcImage, pt.x, pt.y, himl->cx, himl->cy,
2584 hdcImage, 0, height, bmp.bmWidth, height, SRCCOPY);
2585 if (himl->hbmMask)
2586 StretchBlt (himl->hdcMask, pt.x, pt.y, himl->cx, himl->cy,
2587 hdcImage, 0, 0, bmp.bmWidth, height, SRCCOPY);
2590 done:
2591 DestroyIcon(hBestFitIcon);
2592 if (hdcImage)
2593 DeleteDC (hdcImage);
2594 if (ii.hbmColor)
2595 DeleteObject (ii.hbmColor);
2596 if (ii.hbmMask)
2597 DeleteObject (ii.hbmMask);
2599 TRACE("Insert index = %d, himl->cCurImage = %d\n", nIndex, himl->cCurImage);
2600 return nIndex;
2604 /*************************************************************************
2605 * ImageList_SetBkColor [COMCTL32.@]
2607 * Sets the background color of an image list.
2609 * PARAMS
2610 * himl [I] handle to image list
2611 * clrBk [I] background color
2613 * RETURNS
2614 * Success: previous background color
2615 * Failure: CLR_NONE
2618 COLORREF WINAPI
2619 ImageList_SetBkColor (HIMAGELIST himl, COLORREF clrBk)
2621 COLORREF clrOldBk;
2623 if (!is_valid(himl))
2624 return CLR_NONE;
2626 clrOldBk = himl->clrBk;
2627 himl->clrBk = clrBk;
2628 return clrOldBk;
2632 /*************************************************************************
2633 * ImageList_SetDragCursorImage [COMCTL32.@]
2635 * Combines the specified image with the current drag image
2637 * PARAMS
2638 * himlDrag [I] handle to drag image list
2639 * iDrag [I] drag image index
2640 * dxHotspot [I] X position of the hot spot
2641 * dyHotspot [I] Y position of the hot spot
2643 * RETURNS
2644 * Success: TRUE
2645 * Failure: FALSE
2647 * NOTES
2648 * - The names dxHotspot, dyHotspot are misleading because they have nothing
2649 * to do with a hotspot but are only the offset of the origin of the new
2650 * image relative to the origin of the old image.
2652 * - When this function is called and the drag image is visible, a
2653 * short flickering occurs but this matches the Win9x behavior. It is
2654 * possible to fix the flickering using code like in ImageList_DragMove.
2657 BOOL WINAPI
2658 ImageList_SetDragCursorImage (HIMAGELIST himlDrag, INT iDrag,
2659 INT dxHotspot, INT dyHotspot)
2661 HIMAGELIST himlTemp;
2662 BOOL visible;
2664 if (!is_valid(InternalDrag.himl) || !is_valid(himlDrag))
2665 return FALSE;
2667 TRACE(" dxH=%d dyH=%d nX=%d nY=%d\n",
2668 dxHotspot, dyHotspot, InternalDrag.dxHotspot, InternalDrag.dyHotspot);
2670 visible = InternalDrag.bShow;
2672 himlTemp = ImageList_Merge (InternalDrag.himl, 0, himlDrag, iDrag,
2673 dxHotspot, dyHotspot);
2675 if (visible) {
2676 /* hide the drag image */
2677 ImageList_DragShowNolock(FALSE);
2679 if ((InternalDrag.himl->cx != himlTemp->cx) ||
2680 (InternalDrag.himl->cy != himlTemp->cy)) {
2681 /* the size of the drag image changed, invalidate the buffer */
2682 DeleteObject(InternalDrag.hbmBg);
2683 InternalDrag.hbmBg = 0;
2686 ImageList_Destroy (InternalDrag.himl);
2687 InternalDrag.himl = himlTemp;
2689 if (visible) {
2690 /* show the drag image */
2691 ImageList_DragShowNolock(TRUE);
2694 return TRUE;
2698 /*************************************************************************
2699 * ImageList_SetFilter [COMCTL32.@]
2701 * Sets a filter (or does something completely different)!!???
2702 * It removes 12 Bytes from the stack (3 Parameters).
2704 * PARAMS
2705 * himl [I] SHOULD be a handle to image list
2706 * i [I] COULD be an index?
2707 * dwFilter [I] ???
2709 * RETURNS
2710 * Success: TRUE ???
2711 * Failure: FALSE ???
2713 * BUGS
2714 * This is an UNDOCUMENTED function!!!!
2715 * empty stub.
2718 BOOL WINAPI
2719 ImageList_SetFilter (HIMAGELIST himl, INT i, DWORD dwFilter)
2721 FIXME("(%p 0x%x 0x%x):empty stub!\n", himl, i, dwFilter);
2723 return FALSE;
2727 /*************************************************************************
2728 * ImageList_SetFlags [COMCTL32.@]
2730 * Sets the image list flags.
2732 * PARAMS
2733 * himl [I] Handle to image list
2734 * flags [I] Flags to set
2736 * RETURNS
2737 * Old flags?
2739 * BUGS
2740 * Stub.
2743 DWORD WINAPI
2744 ImageList_SetFlags(HIMAGELIST himl, DWORD flags)
2746 FIXME("(%p %08x):empty stub\n", himl, flags);
2747 return 0;
2751 /*************************************************************************
2752 * ImageList_SetIconSize [COMCTL32.@]
2754 * Sets the image size of the bitmap and deletes all images.
2756 * PARAMS
2757 * himl [I] handle to image list
2758 * cx [I] image width
2759 * cy [I] image height
2761 * RETURNS
2762 * Success: TRUE
2763 * Failure: FALSE
2766 BOOL WINAPI
2767 ImageList_SetIconSize (HIMAGELIST himl, INT cx, INT cy)
2769 INT nCount;
2770 HBITMAP hbmNew;
2772 if (!is_valid(himl))
2773 return FALSE;
2775 /* remove all images */
2776 himl->cMaxImage = himl->cInitial + 1;
2777 himl->cCurImage = 0;
2778 himl->cx = cx;
2779 himl->cy = cy;
2781 /* initialize overlay mask indices */
2782 for (nCount = 0; nCount < MAX_OVERLAYIMAGE; nCount++)
2783 himl->nOvlIdx[nCount] = -1;
2785 hbmNew = ImageList_CreateImage(himl->hdcImage, himl, himl->cMaxImage);
2786 SelectObject (himl->hdcImage, hbmNew);
2787 DeleteObject (himl->hbmImage);
2788 himl->hbmImage = hbmNew;
2790 if (himl->hbmMask) {
2791 SIZE sz;
2792 imagelist_get_bitmap_size(himl, himl->cMaxImage, &sz);
2793 hbmNew = CreateBitmap (sz.cx, sz.cy, 1, 1, NULL);
2794 SelectObject (himl->hdcMask, hbmNew);
2795 DeleteObject (himl->hbmMask);
2796 himl->hbmMask = hbmNew;
2799 return TRUE;
2803 /*************************************************************************
2804 * ImageList_SetImageCount [COMCTL32.@]
2806 * Resizes an image list to the specified number of images.
2808 * PARAMS
2809 * himl [I] handle to image list
2810 * iImageCount [I] number of images in the image list
2812 * RETURNS
2813 * Success: TRUE
2814 * Failure: FALSE
2817 BOOL WINAPI
2818 ImageList_SetImageCount (HIMAGELIST himl, UINT iImageCount)
2820 HDC hdcBitmap;
2821 HBITMAP hbmNewBitmap, hbmOld;
2822 INT nNewCount, nCopyCount;
2824 TRACE("%p %d\n",himl,iImageCount);
2826 if (!is_valid(himl))
2827 return FALSE;
2828 if (himl->cMaxImage > iImageCount)
2830 himl->cCurImage = iImageCount;
2831 /* TODO: shrink the bitmap when cMaxImage-cCurImage>cGrow ? */
2832 return TRUE;
2835 nNewCount = iImageCount + himl->cGrow;
2836 nCopyCount = min(himl->cCurImage, iImageCount);
2838 hdcBitmap = CreateCompatibleDC (0);
2840 hbmNewBitmap = ImageList_CreateImage(hdcBitmap, himl, nNewCount);
2842 if (hbmNewBitmap != 0)
2844 hbmOld = SelectObject (hdcBitmap, hbmNewBitmap);
2845 imagelist_copy_images( himl, himl->hdcImage, hdcBitmap, 0, nCopyCount, 0 );
2846 SelectObject (hdcBitmap, hbmOld);
2848 /* FIXME: delete 'empty' image space? */
2850 SelectObject (himl->hdcImage, hbmNewBitmap);
2851 DeleteObject (himl->hbmImage);
2852 himl->hbmImage = hbmNewBitmap;
2854 else
2855 ERR("Could not create new image bitmap !\n");
2857 if (himl->hbmMask)
2859 SIZE sz;
2860 imagelist_get_bitmap_size( himl, nNewCount, &sz );
2861 hbmNewBitmap = CreateBitmap (sz.cx, sz.cy, 1, 1, NULL);
2862 if (hbmNewBitmap != 0)
2864 hbmOld = SelectObject (hdcBitmap, hbmNewBitmap);
2865 imagelist_copy_images( himl, himl->hdcMask, hdcBitmap, 0, nCopyCount, 0 );
2866 SelectObject (hdcBitmap, hbmOld);
2868 /* FIXME: delete 'empty' image space? */
2870 SelectObject (himl->hdcMask, hbmNewBitmap);
2871 DeleteObject (himl->hbmMask);
2872 himl->hbmMask = hbmNewBitmap;
2874 else
2875 ERR("Could not create new mask bitmap!\n");
2878 DeleteDC (hdcBitmap);
2880 if (himl->has_alpha)
2882 char *new_alpha = HeapReAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, himl->has_alpha, nNewCount );
2883 if (new_alpha) himl->has_alpha = new_alpha;
2884 else
2886 HeapFree( GetProcessHeap(), 0, himl->has_alpha );
2887 himl->has_alpha = NULL;
2891 /* Update max image count and current image count */
2892 himl->cMaxImage = nNewCount;
2893 himl->cCurImage = iImageCount;
2895 return TRUE;
2899 /*************************************************************************
2900 * ImageList_SetOverlayImage [COMCTL32.@]
2902 * Assigns an overlay mask index to an existing image in an image list.
2904 * PARAMS
2905 * himl [I] handle to image list
2906 * iImage [I] image index
2907 * iOverlay [I] overlay mask index
2909 * RETURNS
2910 * Success: TRUE
2911 * Failure: FALSE
2914 BOOL WINAPI
2915 ImageList_SetOverlayImage (HIMAGELIST himl, INT iImage, INT iOverlay)
2917 if (!is_valid(himl))
2918 return FALSE;
2919 if ((iOverlay < 1) || (iOverlay > MAX_OVERLAYIMAGE))
2920 return FALSE;
2921 if ((iImage!=-1) && ((iImage < 0) || (iImage > himl->cCurImage)))
2922 return FALSE;
2923 himl->nOvlIdx[iOverlay - 1] = iImage;
2924 return TRUE;
2929 /* helper for ImageList_Write - write bitmap to pstm
2930 * currently everything is written as 24 bit RGB, except masks
2932 static BOOL
2933 _write_bitmap(HBITMAP hBitmap, LPSTREAM pstm)
2935 LPBITMAPFILEHEADER bmfh;
2936 LPBITMAPINFOHEADER bmih;
2937 LPBYTE data = NULL, lpBits;
2938 BITMAP bm;
2939 INT bitCount, sizeImage, offBits, totalSize;
2940 HDC xdc;
2941 BOOL result = FALSE;
2943 if (!GetObjectW(hBitmap, sizeof(BITMAP), &bm))
2944 return FALSE;
2946 bitCount = bm.bmBitsPixel == 1 ? 1 : 24;
2947 sizeImage = get_dib_stride(bm.bmWidth, bitCount) * bm.bmHeight;
2949 totalSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
2950 if(bitCount != 24)
2951 totalSize += (1 << bitCount) * sizeof(RGBQUAD);
2952 offBits = totalSize;
2953 totalSize += sizeImage;
2955 data = Alloc(totalSize);
2956 bmfh = (LPBITMAPFILEHEADER)data;
2957 bmih = (LPBITMAPINFOHEADER)(data + sizeof(BITMAPFILEHEADER));
2958 lpBits = data + offBits;
2960 /* setup BITMAPFILEHEADER */
2961 bmfh->bfType = (('M' << 8) | 'B');
2962 bmfh->bfSize = offBits;
2963 bmfh->bfReserved1 = 0;
2964 bmfh->bfReserved2 = 0;
2965 bmfh->bfOffBits = offBits;
2967 /* setup BITMAPINFOHEADER */
2968 bmih->biSize = sizeof(BITMAPINFOHEADER);
2969 bmih->biWidth = bm.bmWidth;
2970 bmih->biHeight = bm.bmHeight;
2971 bmih->biPlanes = 1;
2972 bmih->biBitCount = bitCount;
2973 bmih->biCompression = BI_RGB;
2974 bmih->biSizeImage = sizeImage;
2975 bmih->biXPelsPerMeter = 0;
2976 bmih->biYPelsPerMeter = 0;
2977 bmih->biClrUsed = 0;
2978 bmih->biClrImportant = 0;
2980 xdc = GetDC(0);
2981 result = GetDIBits(xdc, hBitmap, 0, bm.bmHeight, lpBits, (BITMAPINFO *)bmih, DIB_RGB_COLORS) == bm.bmHeight;
2982 ReleaseDC(0, xdc);
2983 if (!result)
2984 goto failed;
2986 TRACE("width %u, height %u, planes %u, bpp %u\n",
2987 bmih->biWidth, bmih->biHeight,
2988 bmih->biPlanes, bmih->biBitCount);
2990 if(bitCount == 1) {
2991 /* Hack. */
2992 LPBITMAPINFO inf = (LPBITMAPINFO)bmih;
2993 inf->bmiColors[0].rgbRed = inf->bmiColors[0].rgbGreen = inf->bmiColors[0].rgbBlue = 0;
2994 inf->bmiColors[1].rgbRed = inf->bmiColors[1].rgbGreen = inf->bmiColors[1].rgbBlue = 0xff;
2997 if(FAILED(IStream_Write(pstm, data, totalSize, NULL)))
2998 goto failed;
3000 result = TRUE;
3002 failed:
3003 Free(data);
3005 return result;
3009 /*************************************************************************
3010 * ImageList_Write [COMCTL32.@]
3012 * Writes an image list to a stream.
3014 * PARAMS
3015 * himl [I] handle to image list
3016 * pstm [O] Pointer to a stream.
3018 * RETURNS
3019 * Success: TRUE
3020 * Failure: FALSE
3022 * BUGS
3023 * probably.
3026 BOOL WINAPI
3027 ImageList_Write (HIMAGELIST himl, LPSTREAM pstm)
3029 ILHEAD ilHead;
3030 int i;
3032 TRACE("%p %p\n", himl, pstm);
3034 if (!is_valid(himl))
3035 return FALSE;
3037 ilHead.usMagic = (('L' << 8) | 'I');
3038 ilHead.usVersion = 0x101;
3039 ilHead.cCurImage = himl->cCurImage;
3040 ilHead.cMaxImage = himl->cMaxImage;
3041 ilHead.cGrow = himl->cGrow;
3042 ilHead.cx = himl->cx;
3043 ilHead.cy = himl->cy;
3044 ilHead.bkcolor = himl->clrBk;
3045 ilHead.flags = himl->flags;
3046 for(i = 0; i < 4; i++) {
3047 ilHead.ovls[i] = himl->nOvlIdx[i];
3050 TRACE("cx %u, cy %u, flags 0x04%x, cCurImage %u, cMaxImage %u\n",
3051 ilHead.cx, ilHead.cy, ilHead.flags, ilHead.cCurImage, ilHead.cMaxImage);
3053 if(FAILED(IStream_Write(pstm, &ilHead, sizeof(ILHEAD), NULL)))
3054 return FALSE;
3056 /* write the bitmap */
3057 if(!_write_bitmap(himl->hbmImage, pstm))
3058 return FALSE;
3060 /* write the mask if we have one */
3061 if(himl->flags & ILC_MASK) {
3062 if(!_write_bitmap(himl->hbmMask, pstm))
3063 return FALSE;
3066 return TRUE;
3070 static HBITMAP ImageList_CreateImage(HDC hdc, HIMAGELIST himl, UINT count)
3072 HBITMAP hbmNewBitmap;
3073 UINT ilc = (himl->flags & 0xFE);
3074 SIZE sz;
3076 imagelist_get_bitmap_size( himl, count, &sz );
3078 if ((ilc >= ILC_COLOR4 && ilc <= ILC_COLOR32) || ilc == ILC_COLOR)
3080 VOID* bits;
3081 BITMAPINFO *bmi;
3083 TRACE("Creating DIBSection %d x %d, %d Bits per Pixel\n",
3084 sz.cx, sz.cy, himl->uBitsPixel);
3086 if (himl->uBitsPixel <= ILC_COLOR8)
3088 LPPALETTEENTRY pal;
3089 ULONG i, colors;
3090 BYTE temp;
3092 colors = 1 << himl->uBitsPixel;
3093 bmi = Alloc(sizeof(BITMAPINFOHEADER) +
3094 sizeof(PALETTEENTRY) * colors);
3096 pal = (LPPALETTEENTRY)bmi->bmiColors;
3097 GetPaletteEntries(GetStockObject(DEFAULT_PALETTE), 0, colors, pal);
3099 /* Swap colors returned by GetPaletteEntries so we can use them for
3100 * CreateDIBSection call. */
3101 for (i = 0; i < colors; i++)
3103 temp = pal[i].peBlue;
3104 bmi->bmiColors[i].rgbRed = pal[i].peRed;
3105 bmi->bmiColors[i].rgbBlue = temp;
3108 else
3110 bmi = Alloc(sizeof(BITMAPINFOHEADER));
3113 bmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
3114 bmi->bmiHeader.biWidth = sz.cx;
3115 bmi->bmiHeader.biHeight = sz.cy;
3116 bmi->bmiHeader.biPlanes = 1;
3117 bmi->bmiHeader.biBitCount = himl->uBitsPixel;
3118 bmi->bmiHeader.biCompression = BI_RGB;
3119 bmi->bmiHeader.biSizeImage = 0;
3120 bmi->bmiHeader.biXPelsPerMeter = 0;
3121 bmi->bmiHeader.biYPelsPerMeter = 0;
3122 bmi->bmiHeader.biClrUsed = 0;
3123 bmi->bmiHeader.biClrImportant = 0;
3125 hbmNewBitmap = CreateDIBSection(hdc, bmi, DIB_RGB_COLORS, &bits, 0, 0);
3127 Free (bmi);
3129 else /*if (ilc == ILC_COLORDDB)*/
3131 TRACE("Creating Bitmap: %d Bits per Pixel\n", himl->uBitsPixel);
3133 hbmNewBitmap = CreateBitmap (sz.cx, sz.cy, 1, himl->uBitsPixel, NULL);
3135 TRACE("returning %p\n", hbmNewBitmap);
3136 return hbmNewBitmap;
3139 /*************************************************************************
3140 * ImageList_SetColorTable [COMCTL32.@]
3142 * Sets the color table of an image list.
3144 * PARAMS
3145 * himl [I] Handle to the image list.
3146 * uStartIndex [I] The first index to set.
3147 * cEntries [I] Number of entries to set.
3148 * prgb [I] New color information for color table for the image list.
3150 * RETURNS
3151 * Success: Number of entries in the table that were set.
3152 * Failure: Zero.
3154 * SEE
3155 * ImageList_Create(), SetDIBColorTable()
3158 UINT WINAPI
3159 ImageList_SetColorTable (HIMAGELIST himl, UINT uStartIndex, UINT cEntries, CONST RGBQUAD * prgb)
3161 return SetDIBColorTable(himl->hdcImage, uStartIndex, cEntries, prgb);
3164 /*************************************************************************
3165 * ImageList_CoCreateInstance [COMCTL32.@]
3167 * Creates a new imagelist instance and returns an interface pointer to it.
3169 * PARAMS
3170 * rclsid [I] A reference to the CLSID (CLSID_ImageList).
3171 * punkOuter [I] Pointer to IUnknown interface for aggregation, if desired
3172 * riid [I] Identifier of the requested interface.
3173 * ppv [O] Returns the address of the pointer requested, or NULL.
3175 * RETURNS
3176 * Success: S_OK.
3177 * Failure: Error value.
3179 HRESULT WINAPI
3180 ImageList_CoCreateInstance (REFCLSID rclsid, const IUnknown *punkOuter, REFIID riid, void **ppv)
3182 TRACE("(%s,%p,%s,%p)\n", debugstr_guid(rclsid), punkOuter, debugstr_guid(riid), ppv);
3184 if (!IsEqualCLSID(&CLSID_ImageList, rclsid))
3185 return E_NOINTERFACE;
3187 return ImageListImpl_CreateInstance(punkOuter, riid, ppv);
3191 /*************************************************************************
3192 * IImageList implementation
3195 static HRESULT WINAPI ImageListImpl_QueryInterface(IImageList *iface,
3196 REFIID iid, void **ppv)
3198 HIMAGELIST This = (HIMAGELIST) iface;
3199 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
3201 if (!ppv) return E_INVALIDARG;
3203 if (IsEqualIID(&IID_IUnknown, iid) || IsEqualIID(&IID_IImageList, iid))
3204 *ppv = This;
3205 else
3207 *ppv = NULL;
3208 return E_NOINTERFACE;
3211 IUnknown_AddRef((IUnknown*)*ppv);
3212 return S_OK;
3215 static ULONG WINAPI ImageListImpl_AddRef(IImageList *iface)
3217 HIMAGELIST This = (HIMAGELIST) iface;
3218 ULONG ref = InterlockedIncrement(&This->ref);
3220 TRACE("(%p) refcount=%u\n", iface, ref);
3221 return ref;
3224 static ULONG WINAPI ImageListImpl_Release(IImageList *iface)
3226 HIMAGELIST This = (HIMAGELIST) iface;
3227 ULONG ref = InterlockedDecrement(&This->ref);
3229 TRACE("(%p) refcount=%u\n", iface, ref);
3231 if (ref == 0)
3233 /* delete image bitmaps */
3234 if (This->hbmImage) DeleteObject (This->hbmImage);
3235 if (This->hbmMask) DeleteObject (This->hbmMask);
3237 /* delete image & mask DCs */
3238 if (This->hdcImage) DeleteDC (This->hdcImage);
3239 if (This->hdcMask) DeleteDC (This->hdcMask);
3241 /* delete blending brushes */
3242 if (This->hbrBlend25) DeleteObject (This->hbrBlend25);
3243 if (This->hbrBlend50) DeleteObject (This->hbrBlend50);
3245 This->lpVtbl = NULL;
3246 HeapFree(GetProcessHeap(), 0, This->has_alpha);
3247 HeapFree(GetProcessHeap(), 0, This);
3250 return ref;
3253 static HRESULT WINAPI ImageListImpl_Add(IImageList *iface, HBITMAP hbmImage,
3254 HBITMAP hbmMask, int *pi)
3256 HIMAGELIST This = (HIMAGELIST) iface;
3257 int ret;
3259 if (!pi)
3260 return E_FAIL;
3262 ret = ImageList_Add(This, hbmImage, hbmMask);
3264 if (ret == -1)
3265 return E_FAIL;
3267 *pi = ret;
3268 return S_OK;
3271 static HRESULT WINAPI ImageListImpl_ReplaceIcon(IImageList *iface, int i,
3272 HICON hicon, int *pi)
3274 HIMAGELIST This = (HIMAGELIST) iface;
3275 int ret;
3277 if (!pi)
3278 return E_FAIL;
3280 ret = ImageList_ReplaceIcon(This, i, hicon);
3282 if (ret == -1)
3283 return E_FAIL;
3285 *pi = ret;
3286 return S_OK;
3289 static HRESULT WINAPI ImageListImpl_SetOverlayImage(IImageList *iface,
3290 int iImage, int iOverlay)
3292 return ImageList_SetOverlayImage((HIMAGELIST) iface, iImage, iOverlay)
3293 ? S_OK : E_FAIL;
3296 static HRESULT WINAPI ImageListImpl_Replace(IImageList *iface, int i,
3297 HBITMAP hbmImage, HBITMAP hbmMask)
3299 return ImageList_Replace((HIMAGELIST) iface, i, hbmImage, hbmMask) ? S_OK :
3300 E_FAIL;
3303 static HRESULT WINAPI ImageListImpl_AddMasked(IImageList *iface, HBITMAP hbmImage,
3304 COLORREF crMask, int *pi)
3306 HIMAGELIST This = (HIMAGELIST) iface;
3307 int ret;
3309 if (!pi)
3310 return E_FAIL;
3312 ret = ImageList_AddMasked(This, hbmImage, crMask);
3314 if (ret == -1)
3315 return E_FAIL;
3317 *pi = ret;
3318 return S_OK;
3321 static HRESULT WINAPI ImageListImpl_Draw(IImageList *iface,
3322 IMAGELISTDRAWPARAMS *pimldp)
3324 HIMAGELIST This = (HIMAGELIST) iface;
3325 HIMAGELIST old_himl;
3326 int ret;
3328 /* As far as I can tell, Windows simply ignores the contents of pimldp->himl
3329 so we shall simulate the same */
3330 old_himl = pimldp->himl;
3331 pimldp->himl = This;
3333 ret = ImageList_DrawIndirect(pimldp);
3335 pimldp->himl = old_himl;
3336 return ret ? S_OK : E_INVALIDARG;
3339 static HRESULT WINAPI ImageListImpl_Remove(IImageList *iface, int i)
3341 return (ImageList_Remove((HIMAGELIST) iface, i) == 0) ? E_INVALIDARG : S_OK;
3344 static HRESULT WINAPI ImageListImpl_GetIcon(IImageList *iface, int i, UINT flags,
3345 HICON *picon)
3347 HICON hIcon;
3349 if (!picon)
3350 return E_FAIL;
3352 hIcon = ImageList_GetIcon((HIMAGELIST) iface, i, flags);
3354 if (hIcon == NULL)
3355 return E_FAIL;
3357 *picon = hIcon;
3358 return S_OK;
3361 static HRESULT WINAPI ImageListImpl_GetImageInfo(IImageList *iface, int i,
3362 IMAGEINFO *pImageInfo)
3364 return ImageList_GetImageInfo((HIMAGELIST) iface, i, pImageInfo) ? S_OK : E_FAIL;
3367 static HRESULT WINAPI ImageListImpl_Copy(IImageList *iface, int iDst,
3368 IUnknown *punkSrc, int iSrc, UINT uFlags)
3370 HIMAGELIST This = (HIMAGELIST) iface;
3371 IImageList *src = NULL;
3372 HRESULT ret;
3374 if (!punkSrc)
3375 return E_FAIL;
3377 /* TODO: Add test for IID_ImageList2 too */
3378 if (FAILED(IImageList_QueryInterface(punkSrc, &IID_IImageList,
3379 (void **) &src)))
3380 return E_FAIL;
3382 if (ImageList_Copy(This, iDst, (HIMAGELIST) src, iSrc, uFlags))
3383 ret = S_OK;
3384 else
3385 ret = E_FAIL;
3387 IImageList_Release(src);
3388 return ret;
3391 static HRESULT WINAPI ImageListImpl_Merge(IImageList *iface, int i1,
3392 IUnknown *punk2, int i2, int dx, int dy, REFIID riid, void **ppv)
3394 HIMAGELIST This = (HIMAGELIST) iface;
3395 IImageList *iml2 = NULL;
3396 HIMAGELIST hNew;
3397 HRESULT ret = E_FAIL;
3399 TRACE("(%p)->(%d %p %d %d %d %s %p)\n", iface, i1, punk2, i2, dx, dy, debugstr_guid(riid), ppv);
3401 /* TODO: Add test for IID_ImageList2 too */
3402 if (FAILED(IImageList_QueryInterface(punk2, &IID_IImageList,
3403 (void **) &iml2)))
3404 return E_FAIL;
3406 hNew = ImageList_Merge(This, i1, (HIMAGELIST) iml2, i2, dx, dy);
3408 /* Get the interface for the new image list */
3409 if (hNew)
3411 IImageList *imerge = (IImageList*)hNew;
3413 ret = HIMAGELIST_QueryInterface(hNew, riid, ppv);
3414 IImageList_Release(imerge);
3417 IImageList_Release(iml2);
3418 return ret;
3421 static HRESULT WINAPI ImageListImpl_Clone(IImageList *iface, REFIID riid, void **ppv)
3423 HIMAGELIST This = (HIMAGELIST) iface;
3424 HIMAGELIST clone;
3425 HRESULT ret = E_FAIL;
3427 TRACE("(%p)->(%s %p)\n", iface, debugstr_guid(riid), ppv);
3429 clone = ImageList_Duplicate(This);
3431 /* Get the interface for the new image list */
3432 if (clone)
3434 IImageList *iclone = (IImageList*)clone;
3436 ret = HIMAGELIST_QueryInterface(clone, riid, ppv);
3437 IImageList_Release(iclone);
3440 return ret;
3443 static HRESULT WINAPI ImageListImpl_GetImageRect(IImageList *iface, int i,
3444 RECT *prc)
3446 HIMAGELIST This = (HIMAGELIST) iface;
3447 IMAGEINFO info;
3449 if (!prc)
3450 return E_FAIL;
3452 if (!ImageList_GetImageInfo(This, i, &info))
3453 return E_FAIL;
3455 return CopyRect(prc, &info.rcImage) ? S_OK : E_FAIL;
3458 static HRESULT WINAPI ImageListImpl_GetIconSize(IImageList *iface, int *cx,
3459 int *cy)
3461 HIMAGELIST This = (HIMAGELIST) iface;
3463 return ImageList_GetIconSize(This, cx, cy) ? S_OK : E_INVALIDARG;
3466 static HRESULT WINAPI ImageListImpl_SetIconSize(IImageList *iface, int cx,
3467 int cy)
3469 return ImageList_SetIconSize((HIMAGELIST) iface, cx, cy) ? S_OK : E_FAIL;
3472 static HRESULT WINAPI ImageListImpl_GetImageCount(IImageList *iface, int *pi)
3474 *pi = ImageList_GetImageCount((HIMAGELIST) iface);
3475 return S_OK;
3478 static HRESULT WINAPI ImageListImpl_SetImageCount(IImageList *iface,
3479 UINT uNewCount)
3481 return ImageList_SetImageCount((HIMAGELIST) iface, uNewCount) ? S_OK : E_FAIL;
3484 static HRESULT WINAPI ImageListImpl_SetBkColor(IImageList *iface, COLORREF clrBk,
3485 COLORREF *pclr)
3487 *pclr = ImageList_SetBkColor((HIMAGELIST) iface, clrBk);
3488 return S_OK;
3491 static HRESULT WINAPI ImageListImpl_GetBkColor(IImageList *iface, COLORREF *pclr)
3493 *pclr = ImageList_GetBkColor((HIMAGELIST) iface);
3494 return S_OK;
3497 static HRESULT WINAPI ImageListImpl_BeginDrag(IImageList *iface, int iTrack,
3498 int dxHotspot, int dyHotspot)
3500 return ImageList_BeginDrag((HIMAGELIST) iface, iTrack, dxHotspot, dyHotspot) ? S_OK : E_FAIL;
3503 static HRESULT WINAPI ImageListImpl_EndDrag(IImageList *iface)
3505 ImageList_EndDrag();
3506 return S_OK;
3509 static HRESULT WINAPI ImageListImpl_DragEnter(IImageList *iface, HWND hwndLock,
3510 int x, int y)
3512 return ImageList_DragEnter(hwndLock, x, y) ? S_OK : E_FAIL;
3515 static HRESULT WINAPI ImageListImpl_DragLeave(IImageList *iface, HWND hwndLock)
3517 return ImageList_DragLeave(hwndLock) ? S_OK : E_FAIL;
3520 static HRESULT WINAPI ImageListImpl_DragMove(IImageList *iface, int x, int y)
3522 return ImageList_DragMove(x, y) ? S_OK : E_FAIL;
3525 static HRESULT WINAPI ImageListImpl_SetDragCursorImage(IImageList *iface,
3526 IUnknown *punk, int iDrag, int dxHotspot, int dyHotspot)
3528 IImageList *iml2 = NULL;
3529 HRESULT ret;
3531 if (!punk)
3532 return E_FAIL;
3534 /* TODO: Add test for IID_ImageList2 too */
3535 if (FAILED(IImageList_QueryInterface(punk, &IID_IImageList,
3536 (void **) &iml2)))
3537 return E_FAIL;
3539 ret = ImageList_SetDragCursorImage((HIMAGELIST) iml2, iDrag, dxHotspot,
3540 dyHotspot);
3542 IImageList_Release(iml2);
3544 return ret ? S_OK : E_FAIL;
3547 static HRESULT WINAPI ImageListImpl_DragShowNolock(IImageList *iface, BOOL fShow)
3549 return ImageList_DragShowNolock(fShow) ? S_OK : E_FAIL;
3552 static HRESULT WINAPI ImageListImpl_GetDragImage(IImageList *iface, POINT *ppt,
3553 POINT *pptHotspot, REFIID riid, PVOID *ppv)
3555 HRESULT ret = E_FAIL;
3556 HIMAGELIST hNew;
3558 if (!ppv)
3559 return E_FAIL;
3561 hNew = ImageList_GetDragImage(ppt, pptHotspot);
3563 /* Get the interface for the new image list */
3564 if (hNew)
3566 IImageList *idrag = (IImageList*)hNew;
3568 ret = HIMAGELIST_QueryInterface(hNew, riid, ppv);
3569 IImageList_Release(idrag);
3572 return ret;
3575 static HRESULT WINAPI ImageListImpl_GetItemFlags(IImageList *iface, int i,
3576 DWORD *dwFlags)
3578 FIXME("STUB: %p %d %p\n", iface, i, dwFlags);
3579 return E_NOTIMPL;
3582 static HRESULT WINAPI ImageListImpl_GetOverlayImage(IImageList *iface, int iOverlay,
3583 int *piIndex)
3585 HIMAGELIST This = (HIMAGELIST) iface;
3586 int i;
3588 if ((iOverlay < 0) || (iOverlay > This->cCurImage))
3589 return E_FAIL;
3591 for (i = 0; i < MAX_OVERLAYIMAGE; i++)
3593 if (This->nOvlIdx[i] == iOverlay)
3595 *piIndex = i + 1;
3596 return S_OK;
3600 return E_FAIL;
3604 static const IImageListVtbl ImageListImpl_Vtbl = {
3605 ImageListImpl_QueryInterface,
3606 ImageListImpl_AddRef,
3607 ImageListImpl_Release,
3608 ImageListImpl_Add,
3609 ImageListImpl_ReplaceIcon,
3610 ImageListImpl_SetOverlayImage,
3611 ImageListImpl_Replace,
3612 ImageListImpl_AddMasked,
3613 ImageListImpl_Draw,
3614 ImageListImpl_Remove,
3615 ImageListImpl_GetIcon,
3616 ImageListImpl_GetImageInfo,
3617 ImageListImpl_Copy,
3618 ImageListImpl_Merge,
3619 ImageListImpl_Clone,
3620 ImageListImpl_GetImageRect,
3621 ImageListImpl_GetIconSize,
3622 ImageListImpl_SetIconSize,
3623 ImageListImpl_GetImageCount,
3624 ImageListImpl_SetImageCount,
3625 ImageListImpl_SetBkColor,
3626 ImageListImpl_GetBkColor,
3627 ImageListImpl_BeginDrag,
3628 ImageListImpl_EndDrag,
3629 ImageListImpl_DragEnter,
3630 ImageListImpl_DragLeave,
3631 ImageListImpl_DragMove,
3632 ImageListImpl_SetDragCursorImage,
3633 ImageListImpl_DragShowNolock,
3634 ImageListImpl_GetDragImage,
3635 ImageListImpl_GetItemFlags,
3636 ImageListImpl_GetOverlayImage
3639 static BOOL is_valid(HIMAGELIST himl)
3641 BOOL valid;
3642 __TRY
3644 valid = himl && himl->lpVtbl == &ImageListImpl_Vtbl;
3646 __EXCEPT_PAGE_FAULT
3648 valid = FALSE;
3650 __ENDTRY
3651 return valid;
3654 /*************************************************************************
3655 * HIMAGELIST_QueryInterface [COMCTL32.@]
3657 * Returns a pointer to an IImageList or IImageList2 object for the given
3658 * HIMAGELIST.
3660 * PARAMS
3661 * himl [I] Image list handle.
3662 * riid [I] Identifier of the requested interface.
3663 * ppv [O] Returns the address of the pointer requested, or NULL.
3665 * RETURNS
3666 * Success: S_OK.
3667 * Failure: Error value.
3669 HRESULT WINAPI
3670 HIMAGELIST_QueryInterface (HIMAGELIST himl, REFIID riid, void **ppv)
3672 TRACE("(%p,%s,%p)\n", himl, debugstr_guid(riid), ppv);
3673 return IImageList_QueryInterface((IImageList *) himl, riid, ppv);
3676 static HRESULT ImageListImpl_CreateInstance(const IUnknown *pUnkOuter, REFIID iid, void** ppv)
3678 HIMAGELIST This;
3679 HRESULT ret;
3681 TRACE("(%p,%s,%p)\n", pUnkOuter, debugstr_guid(iid), ppv);
3683 *ppv = NULL;
3685 if (pUnkOuter) return CLASS_E_NOAGGREGATION;
3687 This = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct _IMAGELIST));
3688 if (!This) return E_OUTOFMEMORY;
3690 This->lpVtbl = &ImageListImpl_Vtbl;
3691 This->ref = 1;
3693 ret = IUnknown_QueryInterface((IUnknown*)This, iid, ppv);
3694 IUnknown_Release((IUnknown*)This);
3696 return ret;