comctl32: Initialise the mask's colour table.
[wine.git] / dlls / comctl32 / imagelist.c
blobbd706ac6d53b41cc414ee9ab360c64c55b1849b9
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;
118 * imagelist_copy_images()
120 * Copies a block of count images from offset src in the list to offset dest.
121 * Images are copied a row at at time. Assumes hdcSrc and hdcDest are different.
123 static inline void imagelist_copy_images( HIMAGELIST himl, HDC hdcSrc, HDC hdcDest,
124 UINT src, UINT count, UINT dest )
126 POINT ptSrc, ptDest;
127 SIZE sz;
128 UINT i;
130 for ( i=0; i<TILE_COUNT; i++ )
132 imagelist_point_from_index( himl, src+i, &ptSrc );
133 imagelist_point_from_index( himl, dest+i, &ptDest );
134 sz.cx = himl->cx;
135 sz.cy = himl->cy * imagelist_height( count - i );
137 BitBlt( hdcDest, ptDest.x, ptDest.y, sz.cx, sz.cy,
138 hdcSrc, ptSrc.x, ptSrc.y, SRCCOPY );
142 /* add images with an alpha channel when the image list is 32 bpp */
143 static BOOL add_with_alpha( HIMAGELIST himl, HDC hdc, int pos, int count,
144 int width, int height, HBITMAP hbmImage, HBITMAP hbmMask )
146 BOOL ret = FALSE;
147 BITMAP bm;
148 BITMAPINFO *info, *mask_info = NULL;
149 DWORD *bits = NULL;
150 BYTE *mask_bits = NULL;
151 int i, j, n;
152 POINT pt;
153 DWORD mask_width;
155 if (!GetObjectW( hbmImage, sizeof(bm), &bm )) return FALSE;
157 /* if either the imagelist or the source bitmap don't have an alpha channel, bail out now */
158 if (!himl->has_alpha) return FALSE;
159 if (bm.bmBitsPixel != 32) return FALSE;
161 SelectObject( hdc, hbmImage );
162 mask_width = (bm.bmWidth + 31) / 32 * 4;
164 if (!(info = HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET( BITMAPINFO, bmiColors[256] )))) goto done;
165 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
166 info->bmiHeader.biWidth = bm.bmWidth;
167 info->bmiHeader.biHeight = -height;
168 info->bmiHeader.biPlanes = 1;
169 info->bmiHeader.biBitCount = 32;
170 info->bmiHeader.biCompression = BI_RGB;
171 info->bmiHeader.biSizeImage = bm.bmWidth * height * 4;
172 info->bmiHeader.biXPelsPerMeter = 0;
173 info->bmiHeader.biYPelsPerMeter = 0;
174 info->bmiHeader.biClrUsed = 0;
175 info->bmiHeader.biClrImportant = 0;
176 if (!(bits = HeapAlloc( GetProcessHeap(), 0, info->bmiHeader.biSizeImage ))) goto done;
177 if (!GetDIBits( hdc, hbmImage, 0, height, bits, info, DIB_RGB_COLORS )) goto done;
179 if (hbmMask)
181 if (!(mask_info = HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET( BITMAPINFO, bmiColors[2] ))))
182 goto done;
183 mask_info->bmiHeader = info->bmiHeader;
184 mask_info->bmiHeader.biBitCount = 1;
185 mask_info->bmiHeader.biSizeImage = mask_width * height;
186 if (!(mask_bits = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, info->bmiHeader.biSizeImage )))
187 goto done;
188 if (!GetDIBits( hdc, hbmMask, 0, height, mask_bits, mask_info, DIB_RGB_COLORS )) goto done;
191 for (n = 0; n < count; n++)
193 int has_alpha = 0;
195 imagelist_point_from_index( himl, pos + n, &pt );
197 /* check if bitmap has an alpha channel */
198 for (i = 0; i < height && !has_alpha; i++)
199 for (j = n * width; j < (n + 1) * width; j++)
200 if ((has_alpha = ((bits[i * bm.bmWidth + j] & 0xff000000) != 0))) break;
202 if (!has_alpha) /* generate alpha channel from the mask */
204 for (i = 0; i < height; i++)
205 for (j = n * width; j < (n + 1) * width; j++)
206 if (!mask_bits || !((mask_bits[i * mask_width + j / 8] << (j % 8)) & 0x80))
207 bits[i * bm.bmWidth + j] |= 0xff000000;
208 else
209 bits[i * bm.bmWidth + j] = 0;
211 else
213 himl->has_alpha[pos + n] = 1;
215 if (mask_info && himl->hbmMask) /* generate the mask from the alpha channel */
217 for (i = 0; i < height; i++)
218 for (j = n * width; j < (n + 1) * width; j++)
219 if ((bits[i * bm.bmWidth + j] >> 24) > 25) /* more than 10% alpha */
220 mask_bits[i * mask_width + j / 8] &= ~(0x80 >> (j % 8));
221 else
222 mask_bits[i * mask_width + j / 8] |= 0x80 >> (j % 8);
225 StretchDIBits( himl->hdcImage, pt.x, pt.y, himl->cx, himl->cy,
226 n * width, 0, width, height, bits, info, DIB_RGB_COLORS, SRCCOPY );
227 if (mask_info)
228 StretchDIBits( himl->hdcMask, pt.x, pt.y, himl->cx, himl->cy,
229 n * width, 0, width, height, mask_bits, mask_info, DIB_RGB_COLORS, SRCCOPY );
231 ret = TRUE;
233 done:
234 HeapFree( GetProcessHeap(), 0, info );
235 HeapFree( GetProcessHeap(), 0, mask_info );
236 HeapFree( GetProcessHeap(), 0, bits );
237 HeapFree( GetProcessHeap(), 0, mask_bits );
238 return ret;
241 /*************************************************************************
242 * IMAGELIST_InternalExpandBitmaps [Internal]
244 * Expands the bitmaps of an image list by the given number of images.
246 * PARAMS
247 * himl [I] handle to image list
248 * nImageCount [I] number of images to add
250 * RETURNS
251 * nothing
253 * NOTES
254 * This function CANNOT be used to reduce the number of images.
256 static void
257 IMAGELIST_InternalExpandBitmaps(HIMAGELIST himl, INT nImageCount)
259 HDC hdcBitmap;
260 HBITMAP hbmNewBitmap, hbmNull;
261 INT nNewCount;
262 SIZE sz;
264 TRACE("%p has allocated %d, max %d, grow %d images\n", himl, himl->cCurImage, himl->cMaxImage, himl->cGrow);
266 if (himl->cCurImage + nImageCount < himl->cMaxImage)
267 return;
269 nNewCount = himl->cMaxImage + max(nImageCount, himl->cGrow) + 1;
271 imagelist_get_bitmap_size(himl, nNewCount, &sz);
273 TRACE("Create expanded bitmaps : himl=%p x=%d y=%d count=%d\n", himl, sz.cx, sz.cy, nNewCount);
274 hdcBitmap = CreateCompatibleDC (0);
276 hbmNewBitmap = ImageList_CreateImage(hdcBitmap, himl, nNewCount);
278 if (hbmNewBitmap == 0)
279 ERR("creating new image bitmap (x=%d y=%d)!\n", sz.cx, sz.cy);
281 if (himl->cCurImage)
283 hbmNull = SelectObject (hdcBitmap, hbmNewBitmap);
284 BitBlt (hdcBitmap, 0, 0, sz.cx, sz.cy,
285 himl->hdcImage, 0, 0, SRCCOPY);
286 SelectObject (hdcBitmap, hbmNull);
288 SelectObject (himl->hdcImage, hbmNewBitmap);
289 DeleteObject (himl->hbmImage);
290 himl->hbmImage = hbmNewBitmap;
292 if (himl->flags & ILC_MASK)
294 hbmNewBitmap = CreateBitmap (sz.cx, sz.cy, 1, 1, NULL);
296 if (hbmNewBitmap == 0)
297 ERR("creating new mask bitmap!\n");
299 if(himl->cCurImage)
301 hbmNull = SelectObject (hdcBitmap, hbmNewBitmap);
302 BitBlt (hdcBitmap, 0, 0, sz.cx, sz.cy,
303 himl->hdcMask, 0, 0, SRCCOPY);
304 SelectObject (hdcBitmap, hbmNull);
306 SelectObject (himl->hdcMask, hbmNewBitmap);
307 DeleteObject (himl->hbmMask);
308 himl->hbmMask = hbmNewBitmap;
311 if (himl->has_alpha)
313 char *new_alpha = HeapReAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, himl->has_alpha, nNewCount );
314 if (new_alpha) himl->has_alpha = new_alpha;
315 else
317 HeapFree( GetProcessHeap(), 0, himl->has_alpha );
318 himl->has_alpha = NULL;
322 himl->cMaxImage = nNewCount;
324 DeleteDC (hdcBitmap);
328 /*************************************************************************
329 * ImageList_Add [COMCTL32.@]
331 * Add an image or images to an image list.
333 * PARAMS
334 * himl [I] handle to image list
335 * hbmImage [I] handle to image bitmap
336 * hbmMask [I] handle to mask bitmap
338 * RETURNS
339 * Success: Index of the first new image.
340 * Failure: -1
343 INT WINAPI
344 ImageList_Add (HIMAGELIST himl, HBITMAP hbmImage, HBITMAP hbmMask)
346 HDC hdcBitmap, hdcTemp = 0;
347 INT nFirstIndex, nImageCount, i;
348 BITMAP bmp;
349 POINT pt;
351 TRACE("himl=%p hbmimage=%p hbmmask=%p\n", himl, hbmImage, hbmMask);
352 if (!is_valid(himl))
353 return -1;
355 if (!GetObjectW(hbmImage, sizeof(BITMAP), &bmp))
356 return -1;
358 TRACE("himl %p, cCurImage %d, cMaxImage %d, cGrow %d, cx %d, cy %d\n",
359 himl, himl->cCurImage, himl->cMaxImage, himl->cGrow, himl->cx, himl->cy);
361 nImageCount = bmp.bmWidth / himl->cx;
363 TRACE("%p has %d images (%d x %d)\n", hbmImage, nImageCount, bmp.bmWidth, bmp.bmHeight);
365 IMAGELIST_InternalExpandBitmaps(himl, nImageCount);
367 hdcBitmap = CreateCompatibleDC(0);
369 SelectObject(hdcBitmap, hbmImage);
371 if (add_with_alpha( himl, hdcBitmap, himl->cCurImage, nImageCount,
372 himl->cx, min( himl->cy, bmp.bmHeight), hbmImage, hbmMask ))
373 goto done;
375 if (himl->hbmMask)
377 hdcTemp = CreateCompatibleDC(0);
378 SelectObject(hdcTemp, hbmMask);
381 for (i=0; i<nImageCount; i++)
383 imagelist_point_from_index( himl, himl->cCurImage + i, &pt );
385 /* Copy result to the imagelist
387 BitBlt( himl->hdcImage, pt.x, pt.y, himl->cx, bmp.bmHeight,
388 hdcBitmap, i*himl->cx, 0, SRCCOPY );
390 if (!himl->hbmMask)
391 continue;
393 BitBlt( himl->hdcMask, pt.x, pt.y, himl->cx, bmp.bmHeight,
394 hdcTemp, i*himl->cx, 0, SRCCOPY );
396 /* Remove the background from the image
398 BitBlt( himl->hdcImage, pt.x, pt.y, himl->cx, bmp.bmHeight,
399 himl->hdcMask, pt.x, pt.y, 0x220326 ); /* NOTSRCAND */
401 if (hdcTemp) DeleteDC(hdcTemp);
403 done:
404 DeleteDC(hdcBitmap);
406 nFirstIndex = himl->cCurImage;
407 himl->cCurImage += nImageCount;
409 return nFirstIndex;
413 /*************************************************************************
414 * ImageList_AddIcon [COMCTL32.@]
416 * Adds an icon to an image list.
418 * PARAMS
419 * himl [I] handle to image list
420 * hIcon [I] handle to icon
422 * RETURNS
423 * Success: index of the new image
424 * Failure: -1
426 #undef ImageList_AddIcon
427 INT WINAPI ImageList_AddIcon (HIMAGELIST himl, HICON hIcon)
429 return ImageList_ReplaceIcon (himl, -1, hIcon);
433 /*************************************************************************
434 * ImageList_AddMasked [COMCTL32.@]
436 * Adds an image or images to an image list and creates a mask from the
437 * specified bitmap using the mask color.
439 * PARAMS
440 * himl [I] handle to image list.
441 * hBitmap [I] handle to bitmap
442 * clrMask [I] mask color.
444 * RETURNS
445 * Success: Index of the first new image.
446 * Failure: -1
449 INT WINAPI
450 ImageList_AddMasked (HIMAGELIST himl, HBITMAP hBitmap, COLORREF clrMask)
452 HDC hdcMask, hdcBitmap;
453 INT ret;
454 BITMAP bmp;
455 HBITMAP hMaskBitmap;
456 COLORREF bkColor;
458 TRACE("himl=%p hbitmap=%p clrmask=%x\n", himl, hBitmap, clrMask);
459 if (!is_valid(himl))
460 return -1;
462 if (!GetObjectW(hBitmap, sizeof(BITMAP), &bmp))
463 return -1;
465 hdcBitmap = CreateCompatibleDC(0);
466 SelectObject(hdcBitmap, hBitmap);
468 /* Create a temp Mask so we can remove the background of the Image */
469 hdcMask = CreateCompatibleDC(0);
470 hMaskBitmap = CreateBitmap(bmp.bmWidth, bmp.bmHeight, 1, 1, NULL);
471 SelectObject(hdcMask, hMaskBitmap);
473 /* create monochrome image to the mask bitmap */
474 bkColor = (clrMask != CLR_DEFAULT) ? clrMask : GetPixel (hdcBitmap, 0, 0);
475 SetBkColor (hdcBitmap, bkColor);
476 BitBlt (hdcMask, 0, 0, bmp.bmWidth, bmp.bmHeight, hdcBitmap, 0, 0, SRCCOPY);
478 SetBkColor(hdcBitmap, RGB(255,255,255));
481 * Remove the background from the image
483 * WINDOWS BUG ALERT!!!!!!
484 * The statement below should not be done in common practice
485 * but this is how ImageList_AddMasked works in Windows.
486 * It overwrites the original bitmap passed, this was discovered
487 * by using the same bitmap to iterate the different styles
488 * on windows where it failed (BUT ImageList_Add is OK)
489 * This is here in case some apps rely on this bug
491 * Blt mode 0x220326 is NOTSRCAND
493 BitBlt(hdcBitmap, 0, 0, bmp.bmWidth, bmp.bmHeight, hdcMask, 0, 0, 0x220326);
495 DeleteDC(hdcBitmap);
496 DeleteDC(hdcMask);
498 ret = ImageList_Add( himl, hBitmap, hMaskBitmap );
500 DeleteObject(hMaskBitmap);
501 return ret;
505 /*************************************************************************
506 * ImageList_BeginDrag [COMCTL32.@]
508 * Creates a temporary image list that contains one image. It will be used
509 * as a drag image.
511 * PARAMS
512 * himlTrack [I] handle to the source image list
513 * iTrack [I] index of the drag image in the source image list
514 * dxHotspot [I] X position of the hot spot of the drag image
515 * dyHotspot [I] Y position of the hot spot of the drag image
517 * RETURNS
518 * Success: TRUE
519 * Failure: FALSE
522 BOOL WINAPI
523 ImageList_BeginDrag (HIMAGELIST himlTrack, INT iTrack,
524 INT dxHotspot, INT dyHotspot)
526 INT cx, cy;
528 TRACE("(himlTrack=%p iTrack=%d dx=%d dy=%d)\n", himlTrack, iTrack,
529 dxHotspot, dyHotspot);
531 if (!is_valid(himlTrack))
532 return FALSE;
534 if (InternalDrag.himl)
535 ImageList_EndDrag ();
537 cx = himlTrack->cx;
538 cy = himlTrack->cy;
540 InternalDrag.himl = ImageList_Create (cx, cy, himlTrack->flags, 1, 1);
541 if (InternalDrag.himl == NULL) {
542 WARN("Error creating drag image list!\n");
543 return FALSE;
546 InternalDrag.dxHotspot = dxHotspot;
547 InternalDrag.dyHotspot = dyHotspot;
549 /* copy image */
550 BitBlt (InternalDrag.himl->hdcImage, 0, 0, cx, cy, himlTrack->hdcImage, iTrack * cx, 0, SRCCOPY);
552 /* copy mask */
553 BitBlt (InternalDrag.himl->hdcMask, 0, 0, cx, cy, himlTrack->hdcMask, iTrack * cx, 0, SRCCOPY);
555 InternalDrag.himl->cCurImage = 1;
557 return TRUE;
561 /*************************************************************************
562 * ImageList_Copy [COMCTL32.@]
564 * Copies an image of the source image list to an image of the
565 * destination image list. Images can be copied or swapped.
567 * PARAMS
568 * himlDst [I] handle to the destination image list
569 * iDst [I] destination image index.
570 * himlSrc [I] handle to the source image list
571 * iSrc [I] source image index
572 * uFlags [I] flags for the copy operation
574 * RETURNS
575 * Success: TRUE
576 * Failure: FALSE
578 * NOTES
579 * Copying from one image list to another is possible. The original
580 * implementation just copies or swaps within one image list.
581 * Could this feature become a bug??? ;-)
584 BOOL WINAPI
585 ImageList_Copy (HIMAGELIST himlDst, INT iDst, HIMAGELIST himlSrc,
586 INT iSrc, UINT uFlags)
588 POINT ptSrc, ptDst;
590 TRACE("himlDst=%p iDst=%d himlSrc=%p iSrc=%d\n", himlDst, iDst, himlSrc, iSrc);
592 if (!is_valid(himlSrc) || !is_valid(himlDst))
593 return FALSE;
594 if ((iDst < 0) || (iDst >= himlDst->cCurImage))
595 return FALSE;
596 if ((iSrc < 0) || (iSrc >= himlSrc->cCurImage))
597 return FALSE;
599 imagelist_point_from_index( himlDst, iDst, &ptDst );
600 imagelist_point_from_index( himlSrc, iSrc, &ptSrc );
602 if (uFlags & ILCF_SWAP) {
603 /* swap */
604 HDC hdcBmp;
605 HBITMAP hbmTempImage, hbmTempMask;
607 hdcBmp = CreateCompatibleDC (0);
609 /* create temporary bitmaps */
610 hbmTempImage = CreateBitmap (himlSrc->cx, himlSrc->cy, 1,
611 himlSrc->uBitsPixel, NULL);
612 hbmTempMask = CreateBitmap (himlSrc->cx, himlSrc->cy, 1,
613 1, NULL);
615 /* copy (and stretch) destination to temporary bitmaps.(save) */
616 /* image */
617 SelectObject (hdcBmp, hbmTempImage);
618 StretchBlt (hdcBmp, 0, 0, himlSrc->cx, himlSrc->cy,
619 himlDst->hdcImage, ptDst.x, ptDst.y, himlDst->cx, himlDst->cy,
620 SRCCOPY);
621 /* mask */
622 SelectObject (hdcBmp, hbmTempMask);
623 StretchBlt (hdcBmp, 0, 0, himlSrc->cx, himlSrc->cy,
624 himlDst->hdcMask, ptDst.x, ptDst.y, himlDst->cx, himlDst->cy,
625 SRCCOPY);
627 /* copy (and stretch) source to destination */
628 /* image */
629 StretchBlt (himlDst->hdcImage, ptDst.x, ptDst.y, himlDst->cx, himlDst->cy,
630 himlSrc->hdcImage, ptSrc.x, ptSrc.y, himlSrc->cx, himlSrc->cy,
631 SRCCOPY);
632 /* mask */
633 StretchBlt (himlDst->hdcMask, ptDst.x, ptDst.y, himlDst->cx, himlDst->cy,
634 himlSrc->hdcMask, ptSrc.x, ptSrc.y, himlSrc->cx, himlSrc->cy,
635 SRCCOPY);
637 /* copy (without stretching) temporary bitmaps to source (restore) */
638 /* mask */
639 BitBlt (himlSrc->hdcMask, ptSrc.x, ptSrc.y, himlSrc->cx, himlSrc->cy,
640 hdcBmp, 0, 0, SRCCOPY);
642 /* image */
643 BitBlt (himlSrc->hdcImage, ptSrc.x, ptSrc.y, himlSrc->cx, himlSrc->cy,
644 hdcBmp, 0, 0, SRCCOPY);
645 /* delete temporary bitmaps */
646 DeleteObject (hbmTempMask);
647 DeleteObject (hbmTempImage);
648 DeleteDC(hdcBmp);
650 else {
651 /* copy image */
652 StretchBlt (himlDst->hdcImage, ptDst.x, ptDst.y, himlDst->cx, himlDst->cy,
653 himlSrc->hdcImage, ptSrc.x, ptSrc.y, himlSrc->cx, himlSrc->cy,
654 SRCCOPY);
656 /* copy mask */
657 StretchBlt (himlDst->hdcMask, ptDst.x, ptDst.y, himlDst->cx, himlDst->cy,
658 himlSrc->hdcMask, ptSrc.x, ptSrc.y, himlSrc->cx, himlSrc->cy,
659 SRCCOPY);
662 return TRUE;
666 /*************************************************************************
667 * ImageList_Create [COMCTL32.@]
669 * Creates a new image list.
671 * PARAMS
672 * cx [I] image height
673 * cy [I] image width
674 * flags [I] creation flags
675 * cInitial [I] initial number of images in the image list
676 * cGrow [I] number of images by which image list grows
678 * RETURNS
679 * Success: Handle to the created image list
680 * Failure: NULL
682 HIMAGELIST WINAPI
683 ImageList_Create (INT cx, INT cy, UINT flags,
684 INT cInitial, INT cGrow)
686 HIMAGELIST himl;
687 INT nCount;
688 HBITMAP hbmTemp;
689 UINT ilc = (flags & 0xFE);
690 static const WORD aBitBlend25[] =
691 {0xAA, 0x00, 0x55, 0x00, 0xAA, 0x00, 0x55, 0x00};
693 static const WORD aBitBlend50[] =
694 {0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA, 0x55, 0xAA};
696 TRACE("(%d %d 0x%x %d %d)\n", cx, cy, flags, cInitial, cGrow);
698 if (cx <= 0 || cy <= 0) return NULL;
700 /* Create the IImageList interface for the image list */
701 if (FAILED(ImageListImpl_CreateInstance(NULL, &IID_IImageList, (void **)&himl)))
702 return NULL;
704 cGrow = (cGrow < 4) ? 4 : (cGrow + 3) & ~3;
706 himl->cx = cx;
707 himl->cy = cy;
708 himl->flags = flags;
709 himl->cMaxImage = cInitial + 1;
710 himl->cInitial = cInitial;
711 himl->cGrow = cGrow;
712 himl->clrFg = CLR_DEFAULT;
713 himl->clrBk = CLR_NONE;
715 /* initialize overlay mask indices */
716 for (nCount = 0; nCount < MAX_OVERLAYIMAGE; nCount++)
717 himl->nOvlIdx[nCount] = -1;
719 /* Create Image & Mask DCs */
720 himl->hdcImage = CreateCompatibleDC (0);
721 if (!himl->hdcImage)
722 goto cleanup;
723 if (himl->flags & ILC_MASK){
724 himl->hdcMask = CreateCompatibleDC(0);
725 if (!himl->hdcMask)
726 goto cleanup;
729 /* Default to ILC_COLOR4 if none of the ILC_COLOR* flags are specified */
730 if (ilc == ILC_COLOR)
731 ilc = ILC_COLOR4;
733 if (ilc >= ILC_COLOR4 && ilc <= ILC_COLOR32)
734 himl->uBitsPixel = ilc;
735 else
736 himl->uBitsPixel = (UINT)GetDeviceCaps (himl->hdcImage, BITSPIXEL);
738 if (himl->cMaxImage > 0) {
739 himl->hbmImage = ImageList_CreateImage(himl->hdcImage, himl, himl->cMaxImage);
740 SelectObject(himl->hdcImage, himl->hbmImage);
741 } else
742 himl->hbmImage = 0;
744 if ((himl->cMaxImage > 0) && (himl->flags & ILC_MASK)) {
745 SIZE sz;
747 imagelist_get_bitmap_size(himl, himl->cMaxImage, &sz);
748 himl->hbmMask = CreateBitmap (sz.cx, sz.cy, 1, 1, NULL);
749 if (himl->hbmMask == 0) {
750 ERR("Error creating mask bitmap!\n");
751 goto cleanup;
753 SelectObject(himl->hdcMask, himl->hbmMask);
755 else
756 himl->hbmMask = 0;
758 if (ilc == ILC_COLOR32)
759 himl->has_alpha = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, himl->cMaxImage );
760 else
761 himl->has_alpha = NULL;
763 /* create blending brushes */
764 hbmTemp = CreateBitmap (8, 8, 1, 1, aBitBlend25);
765 himl->hbrBlend25 = CreatePatternBrush (hbmTemp);
766 DeleteObject (hbmTemp);
768 hbmTemp = CreateBitmap (8, 8, 1, 1, aBitBlend50);
769 himl->hbrBlend50 = CreatePatternBrush (hbmTemp);
770 DeleteObject (hbmTemp);
772 TRACE("created imagelist %p\n", himl);
773 return himl;
775 cleanup:
776 ImageList_Destroy(himl);
777 return NULL;
781 /*************************************************************************
782 * ImageList_Destroy [COMCTL32.@]
784 * Destroys an image list.
786 * PARAMS
787 * himl [I] handle to image list
789 * RETURNS
790 * Success: TRUE
791 * Failure: FALSE
794 BOOL WINAPI
795 ImageList_Destroy (HIMAGELIST himl)
797 if (!is_valid(himl))
798 return FALSE;
800 IImageList_Release((IImageList *) himl);
801 return TRUE;
805 /*************************************************************************
806 * ImageList_DragEnter [COMCTL32.@]
808 * Locks window update and displays the drag image at the given position.
810 * PARAMS
811 * hwndLock [I] handle of the window that owns the drag image.
812 * x [I] X position of the drag image.
813 * y [I] Y position of the drag image.
815 * RETURNS
816 * Success: TRUE
817 * Failure: FALSE
819 * NOTES
820 * The position of the drag image is relative to the window, not
821 * the client area.
824 BOOL WINAPI
825 ImageList_DragEnter (HWND hwndLock, INT x, INT y)
827 TRACE("(hwnd=%p x=%d y=%d)\n", hwndLock, x, y);
829 if (!is_valid(InternalDrag.himl))
830 return FALSE;
832 if (hwndLock)
833 InternalDrag.hwnd = hwndLock;
834 else
835 InternalDrag.hwnd = GetDesktopWindow ();
837 InternalDrag.x = x;
838 InternalDrag.y = y;
840 /* draw the drag image and save the background */
841 if (!ImageList_DragShowNolock(TRUE)) {
842 return FALSE;
845 return TRUE;
849 /*************************************************************************
850 * ImageList_DragLeave [COMCTL32.@]
852 * Unlocks window update and hides the drag image.
854 * PARAMS
855 * hwndLock [I] handle of the window that owns the drag image.
857 * RETURNS
858 * Success: TRUE
859 * Failure: FALSE
862 BOOL WINAPI
863 ImageList_DragLeave (HWND hwndLock)
865 /* As we don't save drag info in the window this can lead to problems if
866 an app does not supply the same window as DragEnter */
867 /* if (hwndLock)
868 InternalDrag.hwnd = hwndLock;
869 else
870 InternalDrag.hwnd = GetDesktopWindow (); */
871 if(!hwndLock)
872 hwndLock = GetDesktopWindow();
873 if(InternalDrag.hwnd != hwndLock)
874 FIXME("DragLeave hWnd != DragEnter hWnd\n");
876 ImageList_DragShowNolock (FALSE);
878 return TRUE;
882 /*************************************************************************
883 * ImageList_InternalDragDraw [Internal]
885 * Draws the drag image.
887 * PARAMS
888 * hdc [I] device context to draw into.
889 * x [I] X position of the drag image.
890 * y [I] Y position of the drag image.
892 * RETURNS
893 * Success: TRUE
894 * Failure: FALSE
896 * NOTES
897 * The position of the drag image is relative to the window, not
898 * the client area.
902 static inline void
903 ImageList_InternalDragDraw (HDC hdc, INT x, INT y)
905 IMAGELISTDRAWPARAMS imldp;
907 ZeroMemory (&imldp, sizeof(imldp));
908 imldp.cbSize = sizeof(imldp);
909 imldp.himl = InternalDrag.himl;
910 imldp.i = 0;
911 imldp.hdcDst = hdc,
912 imldp.x = x;
913 imldp.y = y;
914 imldp.rgbBk = CLR_DEFAULT;
915 imldp.rgbFg = CLR_DEFAULT;
916 imldp.fStyle = ILD_NORMAL;
917 imldp.fState = ILS_ALPHA;
918 imldp.Frame = 192;
919 ImageList_DrawIndirect (&imldp);
922 /*************************************************************************
923 * ImageList_DragMove [COMCTL32.@]
925 * Moves the drag image.
927 * PARAMS
928 * x [I] X position of the drag image.
929 * y [I] Y position of the drag image.
931 * RETURNS
932 * Success: TRUE
933 * Failure: FALSE
935 * NOTES
936 * The position of the drag image is relative to the window, not
937 * the client area.
940 BOOL WINAPI
941 ImageList_DragMove (INT x, INT y)
943 TRACE("(x=%d y=%d)\n", x, y);
945 if (!is_valid(InternalDrag.himl))
946 return FALSE;
948 /* draw/update the drag image */
949 if (InternalDrag.bShow) {
950 HDC hdcDrag;
951 HDC hdcOffScreen;
952 HDC hdcBg;
953 HBITMAP hbmOffScreen;
954 INT origNewX, origNewY;
955 INT origOldX, origOldY;
956 INT origRegX, origRegY;
957 INT sizeRegX, sizeRegY;
960 /* calculate the update region */
961 origNewX = x - InternalDrag.dxHotspot;
962 origNewY = y - InternalDrag.dyHotspot;
963 origOldX = InternalDrag.x - InternalDrag.dxHotspot;
964 origOldY = InternalDrag.y - InternalDrag.dyHotspot;
965 origRegX = min(origNewX, origOldX);
966 origRegY = min(origNewY, origOldY);
967 sizeRegX = InternalDrag.himl->cx + abs(x - InternalDrag.x);
968 sizeRegY = InternalDrag.himl->cy + abs(y - InternalDrag.y);
970 hdcDrag = GetDCEx(InternalDrag.hwnd, 0,
971 DCX_WINDOW | DCX_CACHE | DCX_LOCKWINDOWUPDATE);
972 hdcOffScreen = CreateCompatibleDC(hdcDrag);
973 hdcBg = CreateCompatibleDC(hdcDrag);
975 hbmOffScreen = CreateCompatibleBitmap(hdcDrag, sizeRegX, sizeRegY);
976 SelectObject(hdcOffScreen, hbmOffScreen);
977 SelectObject(hdcBg, InternalDrag.hbmBg);
979 /* get the actual background of the update region */
980 BitBlt(hdcOffScreen, 0, 0, sizeRegX, sizeRegY, hdcDrag,
981 origRegX, origRegY, SRCCOPY);
982 /* erase the old image */
983 BitBlt(hdcOffScreen, origOldX - origRegX, origOldY - origRegY,
984 InternalDrag.himl->cx, InternalDrag.himl->cy, hdcBg, 0, 0,
985 SRCCOPY);
986 /* save the background */
987 BitBlt(hdcBg, 0, 0, InternalDrag.himl->cx, InternalDrag.himl->cy,
988 hdcOffScreen, origNewX - origRegX, origNewY - origRegY, SRCCOPY);
989 /* draw the image */
990 ImageList_InternalDragDraw(hdcOffScreen, origNewX - origRegX,
991 origNewY - origRegY);
992 /* draw the update region to the screen */
993 BitBlt(hdcDrag, origRegX, origRegY, sizeRegX, sizeRegY,
994 hdcOffScreen, 0, 0, SRCCOPY);
996 DeleteDC(hdcBg);
997 DeleteDC(hdcOffScreen);
998 DeleteObject(hbmOffScreen);
999 ReleaseDC(InternalDrag.hwnd, hdcDrag);
1002 /* update the image position */
1003 InternalDrag.x = x;
1004 InternalDrag.y = y;
1006 return TRUE;
1010 /*************************************************************************
1011 * ImageList_DragShowNolock [COMCTL32.@]
1013 * Shows or hides the drag image.
1015 * PARAMS
1016 * bShow [I] TRUE shows the drag image, FALSE hides it.
1018 * RETURNS
1019 * Success: TRUE
1020 * Failure: FALSE
1023 BOOL WINAPI
1024 ImageList_DragShowNolock (BOOL bShow)
1026 HDC hdcDrag;
1027 HDC hdcBg;
1028 INT x, y;
1030 if (!is_valid(InternalDrag.himl))
1031 return FALSE;
1033 TRACE("bShow=0x%X!\n", bShow);
1035 /* DragImage is already visible/hidden */
1036 if ((InternalDrag.bShow && bShow) || (!InternalDrag.bShow && !bShow)) {
1037 return FALSE;
1040 /* position of the origin of the DragImage */
1041 x = InternalDrag.x - InternalDrag.dxHotspot;
1042 y = InternalDrag.y - InternalDrag.dyHotspot;
1044 hdcDrag = GetDCEx (InternalDrag.hwnd, 0,
1045 DCX_WINDOW | DCX_CACHE | DCX_LOCKWINDOWUPDATE);
1046 if (!hdcDrag) {
1047 return FALSE;
1050 hdcBg = CreateCompatibleDC(hdcDrag);
1051 if (!InternalDrag.hbmBg) {
1052 InternalDrag.hbmBg = CreateCompatibleBitmap(hdcDrag,
1053 InternalDrag.himl->cx, InternalDrag.himl->cy);
1055 SelectObject(hdcBg, InternalDrag.hbmBg);
1057 if (bShow) {
1058 /* save the background */
1059 BitBlt(hdcBg, 0, 0, InternalDrag.himl->cx, InternalDrag.himl->cy,
1060 hdcDrag, x, y, SRCCOPY);
1061 /* show the image */
1062 ImageList_InternalDragDraw(hdcDrag, x, y);
1063 } else {
1064 /* hide the image */
1065 BitBlt(hdcDrag, x, y, InternalDrag.himl->cx, InternalDrag.himl->cy,
1066 hdcBg, 0, 0, SRCCOPY);
1069 InternalDrag.bShow = !InternalDrag.bShow;
1071 DeleteDC(hdcBg);
1072 ReleaseDC (InternalDrag.hwnd, hdcDrag);
1073 return TRUE;
1077 /*************************************************************************
1078 * ImageList_Draw [COMCTL32.@]
1080 * Draws an image.
1082 * PARAMS
1083 * himl [I] handle to image list
1084 * i [I] image index
1085 * hdc [I] handle to device context
1086 * x [I] x position
1087 * y [I] y position
1088 * fStyle [I] drawing flags
1090 * RETURNS
1091 * Success: TRUE
1092 * Failure: FALSE
1094 * SEE
1095 * ImageList_DrawEx.
1098 BOOL WINAPI
1099 ImageList_Draw (HIMAGELIST himl, INT i, HDC hdc, INT x, INT y, UINT fStyle)
1101 return ImageList_DrawEx (himl, i, hdc, x, y, 0, 0,
1102 CLR_DEFAULT, CLR_DEFAULT, fStyle);
1106 /*************************************************************************
1107 * ImageList_DrawEx [COMCTL32.@]
1109 * Draws an image and allows to use extended drawing features.
1111 * PARAMS
1112 * himl [I] handle to image list
1113 * i [I] image index
1114 * hdc [I] handle to device context
1115 * x [I] X position
1116 * y [I] Y position
1117 * dx [I] X offset
1118 * dy [I] Y offset
1119 * rgbBk [I] background color
1120 * rgbFg [I] foreground color
1121 * fStyle [I] drawing flags
1123 * RETURNS
1124 * Success: TRUE
1125 * Failure: FALSE
1127 * NOTES
1128 * Calls ImageList_DrawIndirect.
1130 * SEE
1131 * ImageList_DrawIndirect.
1134 BOOL WINAPI
1135 ImageList_DrawEx (HIMAGELIST himl, INT i, HDC hdc, INT x, INT y,
1136 INT dx, INT dy, COLORREF rgbBk, COLORREF rgbFg,
1137 UINT fStyle)
1139 IMAGELISTDRAWPARAMS imldp;
1141 ZeroMemory (&imldp, sizeof(imldp));
1142 imldp.cbSize = sizeof(imldp);
1143 imldp.himl = himl;
1144 imldp.i = i;
1145 imldp.hdcDst = hdc,
1146 imldp.x = x;
1147 imldp.y = y;
1148 imldp.cx = dx;
1149 imldp.cy = dy;
1150 imldp.rgbBk = rgbBk;
1151 imldp.rgbFg = rgbFg;
1152 imldp.fStyle = fStyle;
1154 return ImageList_DrawIndirect (&imldp);
1158 static BOOL alpha_blend_image( HIMAGELIST himl, HDC dest_dc, int dest_x, int dest_y,
1159 int src_x, int src_y, int cx, int cy, BLENDFUNCTION func,
1160 UINT style, COLORREF blend_col )
1162 BOOL ret = FALSE;
1163 HDC hdc;
1164 HBITMAP bmp = 0, mask = 0;
1165 BITMAPINFO *info;
1166 void *bits, *mask_bits;
1167 unsigned int *ptr;
1168 int i, j;
1170 if (!(hdc = CreateCompatibleDC( 0 ))) return FALSE;
1171 if (!(info = HeapAlloc( GetProcessHeap(), 0, FIELD_OFFSET( BITMAPINFO, bmiColors[256] )))) goto done;
1172 info->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
1173 info->bmiHeader.biWidth = cx;
1174 info->bmiHeader.biHeight = cy;
1175 info->bmiHeader.biPlanes = 1;
1176 info->bmiHeader.biBitCount = 32;
1177 info->bmiHeader.biCompression = BI_RGB;
1178 info->bmiHeader.biSizeImage = cx * cy * 4;
1179 info->bmiHeader.biXPelsPerMeter = 0;
1180 info->bmiHeader.biYPelsPerMeter = 0;
1181 info->bmiHeader.biClrUsed = 0;
1182 info->bmiHeader.biClrImportant = 0;
1183 if (!(bmp = CreateDIBSection( himl->hdcImage, info, DIB_RGB_COLORS, &bits, 0, 0 ))) goto done;
1184 SelectObject( hdc, bmp );
1185 BitBlt( hdc, 0, 0, cx, cy, himl->hdcImage, src_x, src_y, SRCCOPY );
1187 if (blend_col != CLR_NONE)
1189 BYTE r = GetRValue( blend_col );
1190 BYTE g = GetGValue( blend_col );
1191 BYTE b = GetBValue( blend_col );
1193 if (style & ILD_BLEND25)
1195 for (i = 0, ptr = bits; i < cx * cy; i++, ptr++)
1196 *ptr = ((*ptr & 0xff000000) |
1197 ((((*ptr & 0x00ff0000) * 3 + (r << 16)) / 4) & 0x00ff0000) |
1198 ((((*ptr & 0x0000ff00) * 3 + (g << 8)) / 4) & 0x0000ff00) |
1199 ((((*ptr & 0x000000ff) * 3 + (b << 0)) / 4) & 0x000000ff));
1201 else if (style & ILD_BLEND50)
1203 for (i = 0, ptr = bits; i < cx * cy; i++, ptr++)
1204 *ptr = ((*ptr & 0xff000000) |
1205 ((((*ptr & 0x00ff0000) + (r << 16)) / 2) & 0x00ff0000) |
1206 ((((*ptr & 0x0000ff00) + (g << 8)) / 2) & 0x0000ff00) |
1207 ((((*ptr & 0x000000ff) + (b << 0)) / 2) & 0x000000ff));
1211 if (himl->has_alpha) /* we already have an alpha channel in this case */
1213 /* pre-multiply by the alpha channel */
1214 for (i = 0, ptr = bits; i < cx * cy; i++, ptr++)
1216 DWORD alpha = *ptr >> 24;
1217 *ptr = ((*ptr & 0xff000000) |
1218 (((*ptr & 0x00ff0000) * alpha / 255) & 0x00ff0000) |
1219 (((*ptr & 0x0000ff00) * alpha / 255) & 0x0000ff00) |
1220 (((*ptr & 0x000000ff) * alpha / 255)));
1223 else if (himl->hbmMask)
1225 unsigned int width_bytes = (cx + 31) / 32 * 4;
1226 /* generate alpha channel from the mask */
1227 info->bmiHeader.biBitCount = 1;
1228 info->bmiHeader.biSizeImage = width_bytes * cy;
1229 info->bmiColors[0].rgbRed = 0;
1230 info->bmiColors[0].rgbGreen = 0;
1231 info->bmiColors[0].rgbBlue = 0;
1232 info->bmiColors[0].rgbReserved = 0;
1233 info->bmiColors[1].rgbRed = 0xff;
1234 info->bmiColors[1].rgbGreen = 0xff;
1235 info->bmiColors[1].rgbBlue = 0xff;
1236 info->bmiColors[1].rgbReserved = 0;
1237 if (!(mask = CreateDIBSection( himl->hdcMask, info, DIB_RGB_COLORS, &mask_bits, 0, 0 )))
1238 goto done;
1239 SelectObject( hdc, mask );
1240 BitBlt( hdc, 0, 0, cx, cy, himl->hdcMask, src_x, src_y, SRCCOPY );
1241 SelectObject( hdc, bmp );
1242 for (i = 0, ptr = bits; i < cy; i++)
1243 for (j = 0; j < cx; j++, ptr++)
1244 if ((((BYTE *)mask_bits)[i * width_bytes + j / 8] << (j % 8)) & 0x80) *ptr = 0;
1245 else *ptr |= 0xff000000;
1248 ret = GdiAlphaBlend( dest_dc, dest_x, dest_y, cx, cy, hdc, 0, 0, cx, cy, func );
1250 done:
1251 DeleteDC( hdc );
1252 if (bmp) DeleteObject( bmp );
1253 if (mask) DeleteObject( mask );
1254 HeapFree( GetProcessHeap(), 0, info );
1255 return ret;
1258 /*************************************************************************
1259 * ImageList_DrawIndirect [COMCTL32.@]
1261 * Draws an image using various parameters specified in pimldp.
1263 * PARAMS
1264 * pimldp [I] pointer to IMAGELISTDRAWPARAMS structure.
1266 * RETURNS
1267 * Success: TRUE
1268 * Failure: FALSE
1271 BOOL WINAPI
1272 ImageList_DrawIndirect (IMAGELISTDRAWPARAMS *pimldp)
1274 INT cx, cy, nOvlIdx;
1275 DWORD fState, dwRop;
1276 UINT fStyle;
1277 COLORREF oldImageBk, oldImageFg;
1278 HDC hImageDC, hImageListDC, hMaskListDC;
1279 HBITMAP hImageBmp, hOldImageBmp, hBlendMaskBmp;
1280 BOOL bIsTransparent, bBlend, bResult = FALSE, bMask;
1281 HIMAGELIST himl;
1282 HBRUSH hOldBrush;
1283 POINT pt;
1284 BOOL has_alpha;
1286 if (!pimldp || !(himl = pimldp->himl)) return FALSE;
1287 if (!is_valid(himl)) return FALSE;
1288 if ((pimldp->i < 0) || (pimldp->i >= himl->cCurImage)) return FALSE;
1290 imagelist_point_from_index( himl, pimldp->i, &pt );
1291 pt.x += pimldp->xBitmap;
1292 pt.y += pimldp->yBitmap;
1294 fState = pimldp->cbSize < sizeof(IMAGELISTDRAWPARAMS) ? ILS_NORMAL : pimldp->fState;
1295 fStyle = pimldp->fStyle & ~ILD_OVERLAYMASK;
1296 cx = (pimldp->cx == 0) ? himl->cx : pimldp->cx;
1297 cy = (pimldp->cy == 0) ? himl->cy : pimldp->cy;
1299 bIsTransparent = (fStyle & ILD_TRANSPARENT);
1300 if( pimldp->rgbBk == CLR_NONE )
1301 bIsTransparent = TRUE;
1302 if( ( pimldp->rgbBk == CLR_DEFAULT ) && ( himl->clrBk == CLR_NONE ) )
1303 bIsTransparent = TRUE;
1304 bMask = (himl->flags & ILC_MASK) && (fStyle & ILD_MASK) ;
1305 bBlend = (fStyle & (ILD_BLEND25 | ILD_BLEND50) ) && !bMask;
1307 TRACE("himl(%p) hbmMask(%p) iImage(%d) x(%d) y(%d) cx(%d) cy(%d)\n",
1308 himl, himl->hbmMask, pimldp->i, pimldp->x, pimldp->y, cx, cy);
1310 /* we will use these DCs to access the images and masks in the ImageList */
1311 hImageListDC = himl->hdcImage;
1312 hMaskListDC = himl->hdcMask;
1314 /* these will accumulate the image and mask for the image we're drawing */
1315 hImageDC = CreateCompatibleDC( pimldp->hdcDst );
1316 hImageBmp = CreateCompatibleBitmap( pimldp->hdcDst, cx, cy );
1317 hBlendMaskBmp = bBlend ? CreateBitmap(cx, cy, 1, 1, NULL) : 0;
1319 /* Create a compatible DC. */
1320 if (!hImageListDC || !hImageDC || !hImageBmp ||
1321 (bBlend && !hBlendMaskBmp) || (himl->hbmMask && !hMaskListDC))
1322 goto cleanup;
1324 hOldImageBmp = SelectObject(hImageDC, hImageBmp);
1327 * To obtain a transparent look, background color should be set
1328 * to white and foreground color to black when blitting the
1329 * monochrome mask.
1331 oldImageFg = SetTextColor( hImageDC, RGB( 0, 0, 0 ) );
1332 oldImageBk = SetBkColor( hImageDC, RGB( 0xff, 0xff, 0xff ) );
1334 has_alpha = (himl->has_alpha && himl->has_alpha[pimldp->i]);
1335 if (!bMask && (has_alpha || (fState & ILS_ALPHA)))
1337 COLORREF colour, blend_col = CLR_NONE;
1338 BLENDFUNCTION func;
1340 if (bBlend)
1342 blend_col = pimldp->rgbFg;
1343 if (blend_col == CLR_DEFAULT) blend_col = GetSysColor( COLOR_HIGHLIGHT );
1344 else if (blend_col == CLR_NONE) blend_col = GetTextColor( pimldp->hdcDst );
1347 func.BlendOp = AC_SRC_OVER;
1348 func.BlendFlags = 0;
1349 func.SourceConstantAlpha = (fState & ILS_ALPHA) ? pimldp->Frame : 255;
1350 func.AlphaFormat = AC_SRC_ALPHA;
1352 if (bIsTransparent)
1354 bResult = alpha_blend_image( himl, pimldp->hdcDst, pimldp->x, pimldp->y,
1355 pt.x, pt.y, cx, cy, func, fStyle, blend_col );
1356 goto end;
1358 colour = pimldp->rgbBk;
1359 if (colour == CLR_DEFAULT) colour = himl->clrBk;
1360 if (colour == CLR_NONE) colour = GetBkColor( pimldp->hdcDst );
1362 hOldBrush = SelectObject (hImageDC, CreateSolidBrush (colour));
1363 PatBlt( hImageDC, 0, 0, cx, cy, PATCOPY );
1364 alpha_blend_image( himl, hImageDC, 0, 0, pt.x, pt.y, cx, cy, func, fStyle, blend_col );
1365 DeleteObject (SelectObject (hImageDC, hOldBrush));
1366 bResult = BitBlt( pimldp->hdcDst, pimldp->x, pimldp->y, cx, cy, hImageDC, 0, 0, SRCCOPY );
1367 goto end;
1371 * Draw the initial image
1373 if( bMask ) {
1374 if (himl->hbmMask) {
1375 hOldBrush = SelectObject (hImageDC, CreateSolidBrush (GetTextColor(pimldp->hdcDst)));
1376 PatBlt( hImageDC, 0, 0, cx, cy, PATCOPY );
1377 BitBlt(hImageDC, 0, 0, cx, cy, hMaskListDC, pt.x, pt.y, SRCPAINT);
1378 DeleteObject (SelectObject (hImageDC, hOldBrush));
1379 if( bIsTransparent )
1381 BitBlt ( pimldp->hdcDst, pimldp->x, pimldp->y, cx, cy, hImageDC, 0, 0, SRCAND);
1382 bResult = TRUE;
1383 goto end;
1385 } else {
1386 hOldBrush = SelectObject (hImageDC, GetStockObject(BLACK_BRUSH));
1387 PatBlt( hImageDC, 0, 0, cx, cy, PATCOPY);
1388 SelectObject(hImageDC, hOldBrush);
1390 } else {
1391 /* blend the image with the needed solid background */
1392 COLORREF colour = RGB(0,0,0);
1394 if( !bIsTransparent )
1396 colour = pimldp->rgbBk;
1397 if( colour == CLR_DEFAULT )
1398 colour = himl->clrBk;
1399 if( colour == CLR_NONE )
1400 colour = GetBkColor(pimldp->hdcDst);
1403 hOldBrush = SelectObject (hImageDC, CreateSolidBrush (colour));
1404 PatBlt( hImageDC, 0, 0, cx, cy, PATCOPY );
1405 if (himl->hbmMask)
1407 BitBlt( hImageDC, 0, 0, cx, cy, hMaskListDC, pt.x, pt.y, SRCAND );
1408 BitBlt( hImageDC, 0, 0, cx, cy, hImageListDC, pt.x, pt.y, SRCPAINT );
1410 else
1411 BitBlt( hImageDC, 0, 0, cx, cy, hImageListDC, pt.x, pt.y, SRCCOPY);
1412 DeleteObject (SelectObject (hImageDC, hOldBrush));
1415 /* Time for blending, if required */
1416 if (bBlend) {
1417 HBRUSH hBlendBrush;
1418 COLORREF clrBlend = pimldp->rgbFg;
1419 HDC hBlendMaskDC = hImageListDC;
1420 HBITMAP hOldBitmap;
1422 /* Create the blend Mask */
1423 hOldBitmap = SelectObject(hBlendMaskDC, hBlendMaskBmp);
1424 hBlendBrush = fStyle & ILD_BLEND50 ? himl->hbrBlend50 : himl->hbrBlend25;
1425 hOldBrush = SelectObject(hBlendMaskDC, hBlendBrush);
1426 PatBlt(hBlendMaskDC, 0, 0, cx, cy, PATCOPY);
1427 SelectObject(hBlendMaskDC, hOldBrush);
1429 /* Modify the blend mask if an Image Mask exist */
1430 if(himl->hbmMask) {
1431 BitBlt(hBlendMaskDC, 0, 0, cx, cy, hMaskListDC, pt.x, pt.y, 0x220326); /* NOTSRCAND */
1432 BitBlt(hBlendMaskDC, 0, 0, cx, cy, hBlendMaskDC, 0, 0, NOTSRCCOPY);
1435 /* now apply blend to the current image given the BlendMask */
1436 if (clrBlend == CLR_DEFAULT) clrBlend = GetSysColor (COLOR_HIGHLIGHT);
1437 else if (clrBlend == CLR_NONE) clrBlend = GetTextColor (pimldp->hdcDst);
1438 hOldBrush = SelectObject (hImageDC, CreateSolidBrush(clrBlend));
1439 BitBlt (hImageDC, 0, 0, cx, cy, hBlendMaskDC, 0, 0, 0xB8074A); /* PSDPxax */
1440 DeleteObject(SelectObject(hImageDC, hOldBrush));
1441 SelectObject(hBlendMaskDC, hOldBitmap);
1444 /* Now do the overlay image, if any */
1445 nOvlIdx = (pimldp->fStyle & ILD_OVERLAYMASK) >> 8;
1446 if ( (nOvlIdx >= 1) && (nOvlIdx <= MAX_OVERLAYIMAGE)) {
1447 nOvlIdx = himl->nOvlIdx[nOvlIdx - 1];
1448 if ((nOvlIdx >= 0) && (nOvlIdx < himl->cCurImage)) {
1449 POINT ptOvl;
1450 imagelist_point_from_index( himl, nOvlIdx, &ptOvl );
1451 ptOvl.x += pimldp->xBitmap;
1452 if (himl->hbmMask && !(fStyle & ILD_IMAGE))
1453 BitBlt (hImageDC, 0, 0, cx, cy, hMaskListDC, ptOvl.x, ptOvl.y, SRCAND);
1454 BitBlt (hImageDC, 0, 0, cx, cy, hImageListDC, ptOvl.x, ptOvl.y, SRCPAINT);
1458 if (fState & ILS_SATURATE) FIXME("ILS_SATURATE: unimplemented!\n");
1459 if (fState & ILS_GLOW) FIXME("ILS_GLOW: unimplemented!\n");
1460 if (fState & ILS_SHADOW) FIXME("ILS_SHADOW: unimplemented!\n");
1462 if (fStyle & ILD_PRESERVEALPHA) FIXME("ILD_PRESERVEALPHA: unimplemented!\n");
1463 if (fStyle & ILD_SCALE) FIXME("ILD_SCALE: unimplemented!\n");
1464 if (fStyle & ILD_DPISCALE) FIXME("ILD_DPISCALE: unimplemented!\n");
1466 /* now copy the image to the screen */
1467 dwRop = SRCCOPY;
1468 if (himl->hbmMask && bIsTransparent ) {
1469 COLORREF oldDstFg = SetTextColor(pimldp->hdcDst, RGB( 0, 0, 0 ) );
1470 COLORREF oldDstBk = SetBkColor(pimldp->hdcDst, RGB( 0xff, 0xff, 0xff ));
1471 BitBlt (pimldp->hdcDst, pimldp->x, pimldp->y, cx, cy, hMaskListDC, pt.x, pt.y, SRCAND);
1472 SetBkColor(pimldp->hdcDst, oldDstBk);
1473 SetTextColor(pimldp->hdcDst, oldDstFg);
1474 dwRop = SRCPAINT;
1476 if (fStyle & ILD_ROP) dwRop = pimldp->dwRop;
1477 BitBlt (pimldp->hdcDst, pimldp->x, pimldp->y, cx, cy, hImageDC, 0, 0, dwRop);
1479 bResult = TRUE;
1480 end:
1481 /* cleanup the mess */
1482 SetBkColor(hImageDC, oldImageBk);
1483 SetTextColor(hImageDC, oldImageFg);
1484 SelectObject(hImageDC, hOldImageBmp);
1485 cleanup:
1486 DeleteObject(hBlendMaskBmp);
1487 DeleteObject(hImageBmp);
1488 DeleteDC(hImageDC);
1490 return bResult;
1494 /*************************************************************************
1495 * ImageList_Duplicate [COMCTL32.@]
1497 * Duplicates an image list.
1499 * PARAMS
1500 * himlSrc [I] source image list handle
1502 * RETURNS
1503 * Success: Handle of duplicated image list.
1504 * Failure: NULL
1507 HIMAGELIST WINAPI
1508 ImageList_Duplicate (HIMAGELIST himlSrc)
1510 HIMAGELIST himlDst;
1512 if (!is_valid(himlSrc)) {
1513 ERR("Invalid image list handle!\n");
1514 return NULL;
1517 himlDst = ImageList_Create (himlSrc->cx, himlSrc->cy, himlSrc->flags,
1518 himlSrc->cCurImage, himlSrc->cGrow);
1520 if (himlDst)
1522 SIZE sz;
1524 imagelist_get_bitmap_size(himlSrc, himlSrc->cCurImage, &sz);
1525 BitBlt (himlDst->hdcImage, 0, 0, sz.cx, sz.cy,
1526 himlSrc->hdcImage, 0, 0, SRCCOPY);
1528 if (himlDst->hbmMask)
1529 BitBlt (himlDst->hdcMask, 0, 0, sz.cx, sz.cy,
1530 himlSrc->hdcMask, 0, 0, SRCCOPY);
1532 himlDst->cCurImage = himlSrc->cCurImage;
1533 if (himlSrc->has_alpha && himlDst->has_alpha)
1534 memcpy( himlDst->has_alpha, himlSrc->has_alpha, himlDst->cCurImage );
1536 return himlDst;
1540 /*************************************************************************
1541 * ImageList_EndDrag [COMCTL32.@]
1543 * Finishes a drag operation.
1545 * PARAMS
1546 * no Parameters
1548 * RETURNS
1549 * Success: TRUE
1550 * Failure: FALSE
1553 VOID WINAPI
1554 ImageList_EndDrag (void)
1556 /* cleanup the InternalDrag struct */
1557 InternalDrag.hwnd = 0;
1558 ImageList_Destroy (InternalDrag.himl);
1559 InternalDrag.himl = 0;
1560 InternalDrag.x= 0;
1561 InternalDrag.y= 0;
1562 InternalDrag.dxHotspot = 0;
1563 InternalDrag.dyHotspot = 0;
1564 InternalDrag.bShow = FALSE;
1565 DeleteObject(InternalDrag.hbmBg);
1566 InternalDrag.hbmBg = 0;
1570 /*************************************************************************
1571 * ImageList_GetBkColor [COMCTL32.@]
1573 * Returns the background color of an image list.
1575 * PARAMS
1576 * himl [I] Image list handle.
1578 * RETURNS
1579 * Success: background color
1580 * Failure: CLR_NONE
1583 COLORREF WINAPI
1584 ImageList_GetBkColor (HIMAGELIST himl)
1586 return himl ? himl->clrBk : CLR_NONE;
1590 /*************************************************************************
1591 * ImageList_GetDragImage [COMCTL32.@]
1593 * Returns the handle to the internal drag image list.
1595 * PARAMS
1596 * ppt [O] Pointer to the drag position. Can be NULL.
1597 * pptHotspot [O] Pointer to the position of the hot spot. Can be NULL.
1599 * RETURNS
1600 * Success: Handle of the drag image list.
1601 * Failure: NULL.
1604 HIMAGELIST WINAPI
1605 ImageList_GetDragImage (POINT *ppt, POINT *pptHotspot)
1607 if (is_valid(InternalDrag.himl)) {
1608 if (ppt) {
1609 ppt->x = InternalDrag.x;
1610 ppt->y = InternalDrag.y;
1612 if (pptHotspot) {
1613 pptHotspot->x = InternalDrag.dxHotspot;
1614 pptHotspot->y = InternalDrag.dyHotspot;
1616 return (InternalDrag.himl);
1619 return NULL;
1623 /*************************************************************************
1624 * ImageList_GetFlags [COMCTL32.@]
1626 * Gets the flags of the specified image list.
1628 * PARAMS
1629 * himl [I] Handle to image list
1631 * RETURNS
1632 * Image list flags.
1634 * BUGS
1635 * Stub.
1638 DWORD WINAPI
1639 ImageList_GetFlags(HIMAGELIST himl)
1641 TRACE("%p\n", himl);
1643 return is_valid(himl) ? himl->flags : 0;
1647 /*************************************************************************
1648 * ImageList_GetIcon [COMCTL32.@]
1650 * Creates an icon from a masked image of an image list.
1652 * PARAMS
1653 * himl [I] handle to image list
1654 * i [I] image index
1655 * flags [I] drawing style flags
1657 * RETURNS
1658 * Success: icon handle
1659 * Failure: NULL
1662 HICON WINAPI
1663 ImageList_GetIcon (HIMAGELIST himl, INT i, UINT fStyle)
1665 ICONINFO ii;
1666 HICON hIcon;
1667 HBITMAP hOldDstBitmap;
1668 HDC hdcDst;
1669 POINT pt;
1671 TRACE("%p %d %d\n", himl, i, fStyle);
1672 if (!is_valid(himl) || (i < 0) || (i >= himl->cCurImage)) return NULL;
1674 ii.fIcon = TRUE;
1675 ii.xHotspot = 0;
1676 ii.yHotspot = 0;
1678 /* create colour bitmap */
1679 hdcDst = GetDC(0);
1680 ii.hbmColor = CreateCompatibleBitmap(hdcDst, himl->cx, himl->cy);
1681 ReleaseDC(0, hdcDst);
1683 hdcDst = CreateCompatibleDC(0);
1685 imagelist_point_from_index( himl, i, &pt );
1687 /* draw mask*/
1688 ii.hbmMask = CreateBitmap (himl->cx, himl->cy, 1, 1, NULL);
1689 hOldDstBitmap = SelectObject (hdcDst, ii.hbmMask);
1690 if (himl->hbmMask) {
1691 BitBlt (hdcDst, 0, 0, himl->cx, himl->cy,
1692 himl->hdcMask, pt.x, pt.y, SRCCOPY);
1694 else
1695 PatBlt (hdcDst, 0, 0, himl->cx, himl->cy, BLACKNESS);
1697 /* draw image*/
1698 SelectObject (hdcDst, ii.hbmColor);
1699 BitBlt (hdcDst, 0, 0, himl->cx, himl->cy,
1700 himl->hdcImage, pt.x, pt.y, SRCCOPY);
1703 * CreateIconIndirect requires us to deselect the bitmaps from
1704 * the DCs before calling
1706 SelectObject(hdcDst, hOldDstBitmap);
1708 hIcon = CreateIconIndirect (&ii);
1710 DeleteObject (ii.hbmMask);
1711 DeleteObject (ii.hbmColor);
1712 DeleteDC (hdcDst);
1714 return hIcon;
1718 /*************************************************************************
1719 * ImageList_GetIconSize [COMCTL32.@]
1721 * Retrieves the size of an image in an image list.
1723 * PARAMS
1724 * himl [I] handle to image list
1725 * cx [O] pointer to the image width.
1726 * cy [O] pointer to the image height.
1728 * RETURNS
1729 * Success: TRUE
1730 * Failure: FALSE
1732 * NOTES
1733 * All images in an image list have the same size.
1736 BOOL WINAPI
1737 ImageList_GetIconSize (HIMAGELIST himl, INT *cx, INT *cy)
1739 if (!is_valid(himl) || !cx || !cy)
1740 return FALSE;
1741 if ((himl->cx <= 0) || (himl->cy <= 0))
1742 return FALSE;
1744 *cx = himl->cx;
1745 *cy = himl->cy;
1747 return TRUE;
1751 /*************************************************************************
1752 * ImageList_GetImageCount [COMCTL32.@]
1754 * Returns the number of images in an image list.
1756 * PARAMS
1757 * himl [I] handle to image list
1759 * RETURNS
1760 * Success: Number of images.
1761 * Failure: 0
1764 INT WINAPI
1765 ImageList_GetImageCount (HIMAGELIST himl)
1767 if (!is_valid(himl))
1768 return 0;
1770 return himl->cCurImage;
1774 /*************************************************************************
1775 * ImageList_GetImageInfo [COMCTL32.@]
1777 * Returns information about an image in an image list.
1779 * PARAMS
1780 * himl [I] handle to image list
1781 * i [I] image index
1782 * pImageInfo [O] pointer to the image information
1784 * RETURNS
1785 * Success: TRUE
1786 * Failure: FALSE
1789 BOOL WINAPI
1790 ImageList_GetImageInfo (HIMAGELIST himl, INT i, IMAGEINFO *pImageInfo)
1792 POINT pt;
1794 if (!is_valid(himl) || (pImageInfo == NULL))
1795 return FALSE;
1796 if ((i < 0) || (i >= himl->cCurImage))
1797 return FALSE;
1799 pImageInfo->hbmImage = himl->hbmImage;
1800 pImageInfo->hbmMask = himl->hbmMask;
1802 imagelist_point_from_index( himl, i, &pt );
1803 pImageInfo->rcImage.top = pt.y;
1804 pImageInfo->rcImage.bottom = pt.y + himl->cy;
1805 pImageInfo->rcImage.left = pt.x;
1806 pImageInfo->rcImage.right = pt.x + himl->cx;
1808 return TRUE;
1812 /*************************************************************************
1813 * ImageList_GetImageRect [COMCTL32.@]
1815 * Retrieves the rectangle of the specified image in an image list.
1817 * PARAMS
1818 * himl [I] handle to image list
1819 * i [I] image index
1820 * lpRect [O] pointer to the image rectangle
1822 * RETURNS
1823 * Success: TRUE
1824 * Failure: FALSE
1826 * NOTES
1827 * This is an UNDOCUMENTED function!!!
1830 BOOL WINAPI
1831 ImageList_GetImageRect (HIMAGELIST himl, INT i, LPRECT lpRect)
1833 POINT pt;
1835 if (!is_valid(himl) || (lpRect == NULL))
1836 return FALSE;
1837 if ((i < 0) || (i >= himl->cCurImage))
1838 return FALSE;
1840 imagelist_point_from_index( himl, i, &pt );
1841 lpRect->left = pt.x;
1842 lpRect->top = pt.y;
1843 lpRect->right = pt.x + himl->cx;
1844 lpRect->bottom = pt.y + himl->cy;
1846 return TRUE;
1850 /*************************************************************************
1851 * ImageList_LoadImage [COMCTL32.@]
1852 * ImageList_LoadImageA [COMCTL32.@]
1854 * Creates an image list from a bitmap, icon or cursor.
1856 * See ImageList_LoadImageW.
1859 HIMAGELIST WINAPI
1860 ImageList_LoadImageA (HINSTANCE hi, LPCSTR lpbmp, INT cx, INT cGrow,
1861 COLORREF clrMask, UINT uType, UINT uFlags)
1863 HIMAGELIST himl;
1864 LPWSTR lpbmpW;
1865 DWORD len;
1867 if (IS_INTRESOURCE(lpbmp))
1868 return ImageList_LoadImageW(hi, (LPCWSTR)lpbmp, cx, cGrow, clrMask,
1869 uType, uFlags);
1871 len = MultiByteToWideChar(CP_ACP, 0, lpbmp, -1, NULL, 0);
1872 lpbmpW = Alloc(len * sizeof(WCHAR));
1873 MultiByteToWideChar(CP_ACP, 0, lpbmp, -1, lpbmpW, len);
1875 himl = ImageList_LoadImageW(hi, lpbmpW, cx, cGrow, clrMask, uType, uFlags);
1876 Free (lpbmpW);
1877 return himl;
1881 /*************************************************************************
1882 * ImageList_LoadImageW [COMCTL32.@]
1884 * Creates an image list from a bitmap, icon or cursor.
1886 * PARAMS
1887 * hi [I] instance handle
1888 * lpbmp [I] name or id of the image
1889 * cx [I] width of each image
1890 * cGrow [I] number of images to expand
1891 * clrMask [I] mask color
1892 * uType [I] type of image to load
1893 * uFlags [I] loading flags
1895 * RETURNS
1896 * Success: handle to the loaded image list
1897 * Failure: NULL
1899 * SEE
1900 * LoadImage ()
1903 HIMAGELIST WINAPI
1904 ImageList_LoadImageW (HINSTANCE hi, LPCWSTR lpbmp, INT cx, INT cGrow,
1905 COLORREF clrMask, UINT uType, UINT uFlags)
1907 HIMAGELIST himl = NULL;
1908 HANDLE handle;
1909 INT nImageCount;
1911 handle = LoadImageW (hi, lpbmp, uType, 0, 0, uFlags);
1912 if (!handle) {
1913 WARN("Couldn't load image\n");
1914 return NULL;
1917 if (uType == IMAGE_BITMAP) {
1918 DIBSECTION dib;
1919 UINT color;
1921 if (GetObjectW (handle, sizeof(dib), &dib) == sizeof(BITMAP)) color = ILC_COLOR;
1922 else color = dib.dsBm.bmBitsPixel;
1924 /* To match windows behavior, if cx is set to zero and
1925 the flag DI_DEFAULTSIZE is specified, cx becomes the
1926 system metric value for icons. If the flag is not specified
1927 the function sets the size to the height of the bitmap */
1928 if (cx == 0)
1930 if (uFlags & DI_DEFAULTSIZE)
1931 cx = GetSystemMetrics (SM_CXICON);
1932 else
1933 cx = dib.dsBm.bmHeight;
1936 nImageCount = dib.dsBm.bmWidth / cx;
1938 himl = ImageList_Create (cx, dib.dsBm.bmHeight, ILC_MASK | color, nImageCount, cGrow);
1939 if (!himl) {
1940 DeleteObject (handle);
1941 return NULL;
1943 ImageList_AddMasked (himl, handle, clrMask);
1945 else if ((uType == IMAGE_ICON) || (uType == IMAGE_CURSOR)) {
1946 ICONINFO ii;
1947 BITMAP bmp;
1949 GetIconInfo (handle, &ii);
1950 GetObjectW (ii.hbmColor, sizeof(BITMAP), &bmp);
1951 himl = ImageList_Create (bmp.bmWidth, bmp.bmHeight,
1952 ILC_MASK | ILC_COLOR, 1, cGrow);
1953 if (!himl) {
1954 DeleteObject (ii.hbmColor);
1955 DeleteObject (ii.hbmMask);
1956 DeleteObject (handle);
1957 return NULL;
1959 ImageList_Add (himl, ii.hbmColor, ii.hbmMask);
1960 DeleteObject (ii.hbmColor);
1961 DeleteObject (ii.hbmMask);
1964 DeleteObject (handle);
1966 return himl;
1970 /*************************************************************************
1971 * ImageList_Merge [COMCTL32.@]
1973 * Create an image list containing a merged image from two image lists.
1975 * PARAMS
1976 * himl1 [I] handle to first image list
1977 * i1 [I] first image index
1978 * himl2 [I] handle to second image list
1979 * i2 [I] second image index
1980 * dx [I] X offset of the second image relative to the first.
1981 * dy [I] Y offset of the second image relative to the first.
1983 * RETURNS
1984 * Success: The newly created image list. It contains a single image
1985 * consisting of the second image merged with the first.
1986 * Failure: NULL, if either himl1 or himl2 are invalid.
1988 * NOTES
1989 * - The returned image list should be deleted by the caller using
1990 * ImageList_Destroy() when it is no longer required.
1991 * - If either i1 or i2 are not valid image indices they will be treated
1992 * as a blank image.
1994 HIMAGELIST WINAPI
1995 ImageList_Merge (HIMAGELIST himl1, INT i1, HIMAGELIST himl2, INT i2,
1996 INT dx, INT dy)
1998 HIMAGELIST himlDst = NULL;
1999 INT cxDst, cyDst;
2000 INT xOff1, yOff1, xOff2, yOff2;
2001 POINT pt1, pt2;
2003 TRACE("(himl1=%p i1=%d himl2=%p i2=%d dx=%d dy=%d)\n", himl1, i1, himl2,
2004 i2, dx, dy);
2006 if (!is_valid(himl1) || !is_valid(himl2))
2007 return NULL;
2009 if (dx > 0) {
2010 cxDst = max (himl1->cx, dx + himl2->cx);
2011 xOff1 = 0;
2012 xOff2 = dx;
2014 else if (dx < 0) {
2015 cxDst = max (himl2->cx, himl1->cx - dx);
2016 xOff1 = -dx;
2017 xOff2 = 0;
2019 else {
2020 cxDst = max (himl1->cx, himl2->cx);
2021 xOff1 = 0;
2022 xOff2 = 0;
2025 if (dy > 0) {
2026 cyDst = max (himl1->cy, dy + himl2->cy);
2027 yOff1 = 0;
2028 yOff2 = dy;
2030 else if (dy < 0) {
2031 cyDst = max (himl2->cy, himl1->cy - dy);
2032 yOff1 = -dy;
2033 yOff2 = 0;
2035 else {
2036 cyDst = max (himl1->cy, himl2->cy);
2037 yOff1 = 0;
2038 yOff2 = 0;
2041 himlDst = ImageList_Create (cxDst, cyDst, ILC_MASK | ILC_COLOR, 1, 1);
2043 if (himlDst)
2045 imagelist_point_from_index( himl1, i1, &pt1 );
2046 imagelist_point_from_index( himl2, i2, &pt2 );
2048 /* copy image */
2049 BitBlt (himlDst->hdcImage, 0, 0, cxDst, cyDst, himl1->hdcImage, 0, 0, BLACKNESS);
2050 if (i1 >= 0 && i1 < himl1->cCurImage)
2051 BitBlt (himlDst->hdcImage, xOff1, yOff1, himl1->cx, himl1->cy, himl1->hdcImage, pt1.x, pt1.y, SRCCOPY);
2052 if (i2 >= 0 && i2 < himl2->cCurImage)
2054 BitBlt (himlDst->hdcImage, xOff2, yOff2, himl2->cx, himl2->cy, himl2->hdcMask , pt2.x, pt2.y, SRCAND);
2055 BitBlt (himlDst->hdcImage, xOff2, yOff2, himl2->cx, himl2->cy, himl2->hdcImage, pt2.x, pt2.y, SRCPAINT);
2058 /* copy mask */
2059 BitBlt (himlDst->hdcMask, 0, 0, cxDst, cyDst, himl1->hdcMask, 0, 0, WHITENESS);
2060 if (i1 >= 0 && i1 < himl1->cCurImage)
2061 BitBlt (himlDst->hdcMask, xOff1, yOff1, himl1->cx, himl1->cy, himl1->hdcMask, pt1.x, pt1.y, SRCCOPY);
2062 if (i2 >= 0 && i2 < himl2->cCurImage)
2063 BitBlt (himlDst->hdcMask, xOff2, yOff2, himl2->cx, himl2->cy, himl2->hdcMask, pt2.x, pt2.y, SRCAND);
2065 himlDst->cCurImage = 1;
2068 return himlDst;
2072 /***********************************************************************
2073 * DIB_GetDIBImageBytes
2075 * Return the number of bytes used to hold the image in a DIB bitmap.
2077 static int DIB_GetDIBImageBytes( int width, int height, int depth )
2079 return (((width * depth + 31) / 8) & ~3) * abs( height );
2083 /* helper for ImageList_Read, see comments below */
2084 static BOOL _read_bitmap(HDC hdcIml, LPSTREAM pstm)
2086 BITMAPFILEHEADER bmfh;
2087 int bitsperpixel, palspace;
2088 char bmi_buf[sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * 256];
2089 LPBITMAPINFO bmi = (LPBITMAPINFO)bmi_buf;
2090 int result = FALSE;
2091 LPBYTE bits = NULL;
2093 if (FAILED(IStream_Read ( pstm, &bmfh, sizeof(bmfh), NULL)))
2094 return FALSE;
2096 if (bmfh.bfType != (('M'<<8)|'B'))
2097 return FALSE;
2099 if (FAILED(IStream_Read ( pstm, &bmi->bmiHeader, sizeof(bmi->bmiHeader), NULL)))
2100 return FALSE;
2102 if ((bmi->bmiHeader.biSize != sizeof(bmi->bmiHeader)))
2103 return FALSE;
2105 TRACE("width %u, height %u, planes %u, bpp %u\n",
2106 bmi->bmiHeader.biWidth, bmi->bmiHeader.biHeight,
2107 bmi->bmiHeader.biPlanes, bmi->bmiHeader.biBitCount);
2109 bitsperpixel = bmi->bmiHeader.biPlanes * bmi->bmiHeader.biBitCount;
2110 if (bitsperpixel<=8)
2111 palspace = (1<<bitsperpixel)*sizeof(RGBQUAD);
2112 else
2113 palspace = 0;
2115 bmi->bmiHeader.biSizeImage = DIB_GetDIBImageBytes(bmi->bmiHeader.biWidth, bmi->bmiHeader.biHeight, bitsperpixel);
2117 /* read the palette right after the end of the bitmapinfoheader */
2118 if (palspace && FAILED(IStream_Read(pstm, bmi->bmiColors, palspace, NULL)))
2119 goto error;
2121 bits = Alloc(bmi->bmiHeader.biSizeImage);
2122 if (!bits)
2123 goto error;
2124 if (FAILED(IStream_Read(pstm, bits, bmi->bmiHeader.biSizeImage, NULL)))
2125 goto error;
2127 if (!StretchDIBits(hdcIml, 0, 0, bmi->bmiHeader.biWidth, bmi->bmiHeader.biHeight,
2128 0, 0, bmi->bmiHeader.biWidth, bmi->bmiHeader.biHeight,
2129 bits, bmi, DIB_RGB_COLORS, SRCCOPY))
2130 goto error;
2131 result = TRUE;
2133 error:
2134 Free(bits);
2135 return result;
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 ILHEAD ilHead;
2172 HIMAGELIST himl;
2173 int i;
2175 TRACE("%p\n", pstm);
2177 if (FAILED(IStream_Read (pstm, &ilHead, sizeof(ILHEAD), NULL)))
2178 return NULL;
2179 if (ilHead.usMagic != (('L' << 8) | 'I'))
2180 return NULL;
2181 if (ilHead.usVersion != 0x101) /* probably version? */
2182 return NULL;
2184 TRACE("cx %u, cy %u, flags 0x%04x, cCurImage %u, cMaxImage %u\n",
2185 ilHead.cx, ilHead.cy, ilHead.flags, ilHead.cCurImage, ilHead.cMaxImage);
2187 himl = ImageList_Create(ilHead.cx, ilHead.cy, ilHead.flags, ilHead.cCurImage, ilHead.cMaxImage);
2188 if (!himl)
2189 return NULL;
2191 if (!_read_bitmap(himl->hdcImage, pstm))
2193 WARN("failed to read bitmap from stream\n");
2194 return NULL;
2196 if (ilHead.flags & ILC_MASK)
2198 if (!_read_bitmap(himl->hdcMask, pstm))
2200 WARN("failed to read mask bitmap from stream\n");
2201 return NULL;
2205 himl->cCurImage = ilHead.cCurImage;
2206 himl->cMaxImage = ilHead.cMaxImage;
2208 ImageList_SetBkColor(himl,ilHead.bkcolor);
2209 for (i=0;i<4;i++)
2210 ImageList_SetOverlayImage(himl,ilHead.ovls[i],i+1);
2211 return himl;
2215 /*************************************************************************
2216 * ImageList_Remove [COMCTL32.@]
2218 * Removes an image from an image list
2220 * PARAMS
2221 * himl [I] image list handle
2222 * i [I] image index
2224 * RETURNS
2225 * Success: TRUE
2226 * Failure: FALSE
2228 * FIXME: as the image list storage test shows, native comctl32 simply shifts
2229 * images without creating a new bitmap.
2231 BOOL WINAPI
2232 ImageList_Remove (HIMAGELIST himl, INT i)
2234 HBITMAP hbmNewImage, hbmNewMask;
2235 HDC hdcBmp;
2236 SIZE sz;
2238 TRACE("(himl=%p i=%d)\n", himl, i);
2240 if (!is_valid(himl)) {
2241 ERR("Invalid image list handle!\n");
2242 return FALSE;
2245 if ((i < -1) || (i >= himl->cCurImage)) {
2246 TRACE("index out of range! %d\n", i);
2247 return FALSE;
2250 if (i == -1) {
2251 INT nCount;
2253 /* remove all */
2254 if (himl->cCurImage == 0) {
2255 /* remove all on empty ImageList is allowed */
2256 TRACE("remove all on empty ImageList!\n");
2257 return TRUE;
2260 himl->cMaxImage = himl->cInitial + himl->cGrow - 1;
2261 himl->cCurImage = 0;
2262 for (nCount = 0; nCount < MAX_OVERLAYIMAGE; nCount++)
2263 himl->nOvlIdx[nCount] = -1;
2265 hbmNewImage = ImageList_CreateImage(himl->hdcImage, himl, himl->cMaxImage);
2266 SelectObject (himl->hdcImage, hbmNewImage);
2267 DeleteObject (himl->hbmImage);
2268 himl->hbmImage = hbmNewImage;
2270 if (himl->hbmMask) {
2272 imagelist_get_bitmap_size(himl, himl->cMaxImage, &sz);
2273 hbmNewMask = CreateBitmap (sz.cx, sz.cy, 1, 1, NULL);
2274 SelectObject (himl->hdcMask, hbmNewMask);
2275 DeleteObject (himl->hbmMask);
2276 himl->hbmMask = hbmNewMask;
2279 else {
2280 /* delete one image */
2281 TRACE("Remove single image! %d\n", i);
2283 /* create new bitmap(s) */
2284 TRACE(" - Number of images: %d / %d (Old/New)\n",
2285 himl->cCurImage, himl->cCurImage - 1);
2287 hbmNewImage = ImageList_CreateImage(himl->hdcImage, himl, himl->cMaxImage);
2289 imagelist_get_bitmap_size(himl, himl->cMaxImage, &sz );
2290 if (himl->hbmMask)
2291 hbmNewMask = CreateBitmap (sz.cx, sz.cy, 1, 1, NULL);
2292 else
2293 hbmNewMask = 0; /* Just to keep compiler happy! */
2295 hdcBmp = CreateCompatibleDC (0);
2297 /* copy all images and masks prior to the "removed" image */
2298 if (i > 0) {
2299 TRACE("Pre image copy: Copy %d images\n", i);
2301 SelectObject (hdcBmp, hbmNewImage);
2302 imagelist_copy_images( himl, himl->hdcImage, hdcBmp, 0, i, 0 );
2304 if (himl->hbmMask) {
2305 SelectObject (hdcBmp, hbmNewMask);
2306 imagelist_copy_images( himl, himl->hdcMask, hdcBmp, 0, i, 0 );
2310 /* copy all images and masks behind the removed image */
2311 if (i < himl->cCurImage - 1) {
2312 TRACE("Post image copy!\n");
2314 SelectObject (hdcBmp, hbmNewImage);
2315 imagelist_copy_images( himl, himl->hdcImage, hdcBmp, i + 1,
2316 (himl->cCurImage - i), i );
2318 if (himl->hbmMask) {
2319 SelectObject (hdcBmp, hbmNewMask);
2320 imagelist_copy_images( himl, himl->hdcMask, hdcBmp, i + 1,
2321 (himl->cCurImage - i), i );
2325 DeleteDC (hdcBmp);
2327 /* delete old images and insert new ones */
2328 SelectObject (himl->hdcImage, hbmNewImage);
2329 DeleteObject (himl->hbmImage);
2330 himl->hbmImage = hbmNewImage;
2331 if (himl->hbmMask) {
2332 SelectObject (himl->hdcMask, hbmNewMask);
2333 DeleteObject (himl->hbmMask);
2334 himl->hbmMask = hbmNewMask;
2337 himl->cCurImage--;
2340 return TRUE;
2344 /*************************************************************************
2345 * ImageList_Replace [COMCTL32.@]
2347 * Replaces an image in an image list with a new image.
2349 * PARAMS
2350 * himl [I] handle to image list
2351 * i [I] image index
2352 * hbmImage [I] handle to image bitmap
2353 * hbmMask [I] handle to mask bitmap. Can be NULL.
2355 * RETURNS
2356 * Success: TRUE
2357 * Failure: FALSE
2360 BOOL WINAPI
2361 ImageList_Replace (HIMAGELIST himl, INT i, HBITMAP hbmImage,
2362 HBITMAP hbmMask)
2364 HDC hdcImage;
2365 BITMAP bmp;
2366 POINT pt;
2368 TRACE("%p %d %p %p\n", himl, i, hbmImage, hbmMask);
2370 if (!is_valid(himl)) {
2371 ERR("Invalid image list handle!\n");
2372 return FALSE;
2375 if ((i >= himl->cMaxImage) || (i < 0)) {
2376 ERR("Invalid image index!\n");
2377 return FALSE;
2380 if (!GetObjectW(hbmImage, sizeof(BITMAP), &bmp))
2381 return FALSE;
2383 hdcImage = CreateCompatibleDC (0);
2385 /* Replace Image */
2386 SelectObject (hdcImage, hbmImage);
2388 if (add_with_alpha( himl, hdcImage, i, 1, bmp.bmWidth, bmp.bmHeight, hbmImage, hbmMask ))
2389 goto done;
2391 imagelist_point_from_index(himl, i, &pt);
2392 StretchBlt (himl->hdcImage, pt.x, pt.y, himl->cx, himl->cy,
2393 hdcImage, 0, 0, bmp.bmWidth, bmp.bmHeight, SRCCOPY);
2395 if (himl->hbmMask)
2397 HDC hdcTemp;
2398 HBITMAP hOldBitmapTemp;
2400 hdcTemp = CreateCompatibleDC(0);
2401 hOldBitmapTemp = SelectObject(hdcTemp, hbmMask);
2403 StretchBlt (himl->hdcMask, pt.x, pt.y, himl->cx, himl->cy,
2404 hdcTemp, 0, 0, bmp.bmWidth, bmp.bmHeight, SRCCOPY);
2405 SelectObject(hdcTemp, hOldBitmapTemp);
2406 DeleteDC(hdcTemp);
2408 /* Remove the background from the image
2410 BitBlt (himl->hdcImage, pt.x, pt.y, bmp.bmWidth, bmp.bmHeight,
2411 himl->hdcMask, pt.x, pt.y, 0x220326); /* NOTSRCAND */
2414 done:
2415 DeleteDC (hdcImage);
2417 return TRUE;
2421 /*************************************************************************
2422 * ImageList_ReplaceIcon [COMCTL32.@]
2424 * Replaces an image in an image list using an icon.
2426 * PARAMS
2427 * himl [I] handle to image list
2428 * i [I] image index
2429 * hIcon [I] handle to icon
2431 * RETURNS
2432 * Success: index of the replaced image
2433 * Failure: -1
2436 INT WINAPI
2437 ImageList_ReplaceIcon (HIMAGELIST himl, INT nIndex, HICON hIcon)
2439 HDC hdcImage;
2440 HICON hBestFitIcon;
2441 ICONINFO ii;
2442 BITMAP bmp;
2443 BOOL ret;
2444 POINT pt;
2446 TRACE("(%p %d %p)\n", himl, nIndex, hIcon);
2448 if (!is_valid(himl)) {
2449 ERR("invalid image list\n");
2450 return -1;
2452 if ((nIndex >= himl->cMaxImage) || (nIndex < -1)) {
2453 ERR("invalid image index %d / %d\n", nIndex, himl->cMaxImage);
2454 return -1;
2457 hBestFitIcon = CopyImage(
2458 hIcon, IMAGE_ICON,
2459 himl->cx, himl->cy,
2460 LR_COPYFROMRESOURCE);
2461 /* the above will fail if the icon wasn't loaded from a resource, so try
2462 * again without LR_COPYFROMRESOURCE flag */
2463 if (!hBestFitIcon)
2464 hBestFitIcon = CopyImage(
2465 hIcon, IMAGE_ICON,
2466 himl->cx, himl->cy,
2468 if (!hBestFitIcon)
2469 return -1;
2471 ret = GetIconInfo (hBestFitIcon, &ii);
2472 if (!ret) {
2473 DestroyIcon(hBestFitIcon);
2474 return -1;
2477 ret = GetObjectW (ii.hbmMask, sizeof(BITMAP), &bmp);
2478 if (!ret) {
2479 ERR("couldn't get mask bitmap info\n");
2480 if (ii.hbmColor)
2481 DeleteObject (ii.hbmColor);
2482 if (ii.hbmMask)
2483 DeleteObject (ii.hbmMask);
2484 DestroyIcon(hBestFitIcon);
2485 return -1;
2488 if (nIndex == -1) {
2489 if (himl->cCurImage + 1 > himl->cMaxImage)
2490 IMAGELIST_InternalExpandBitmaps(himl, 1);
2492 nIndex = himl->cCurImage;
2493 himl->cCurImage++;
2496 hdcImage = CreateCompatibleDC (0);
2497 TRACE("hdcImage=%p\n", hdcImage);
2498 if (hdcImage == 0)
2499 ERR("invalid hdcImage!\n");
2501 if (himl->has_alpha)
2503 if (!ii.hbmColor)
2505 UINT height = bmp.bmHeight / 2;
2506 HDC hdcMask = CreateCompatibleDC( 0 );
2507 HBITMAP color = CreateBitmap( bmp.bmWidth, height, 1, 1, NULL );
2508 SelectObject( hdcImage, color );
2509 SelectObject( hdcMask, ii.hbmMask );
2510 BitBlt( hdcImage, 0, 0, bmp.bmWidth, height, hdcMask, 0, height, SRCCOPY );
2511 ret = add_with_alpha( himl, hdcImage, nIndex, 1, bmp.bmWidth, height, color, ii.hbmMask );
2512 DeleteDC( hdcMask );
2513 DeleteObject( color );
2514 if (ret) goto done;
2516 else if (add_with_alpha( himl, hdcImage, nIndex, 1, bmp.bmWidth, bmp.bmHeight,
2517 ii.hbmColor, ii.hbmMask )) goto done;
2520 imagelist_point_from_index(himl, nIndex, &pt);
2522 SetTextColor(himl->hdcImage, RGB(0,0,0));
2523 SetBkColor (himl->hdcImage, RGB(255,255,255));
2525 if (ii.hbmColor)
2527 SelectObject (hdcImage, ii.hbmColor);
2528 StretchBlt (himl->hdcImage, pt.x, pt.y, himl->cx, himl->cy,
2529 hdcImage, 0, 0, bmp.bmWidth, bmp.bmHeight, SRCCOPY);
2530 if (himl->hbmMask)
2532 SelectObject (hdcImage, ii.hbmMask);
2533 StretchBlt (himl->hdcMask, pt.x, pt.y, himl->cx, himl->cy,
2534 hdcImage, 0, 0, bmp.bmWidth, bmp.bmHeight, SRCCOPY);
2537 else
2539 UINT height = bmp.bmHeight / 2;
2540 SelectObject (hdcImage, ii.hbmMask);
2541 StretchBlt (himl->hdcImage, pt.x, pt.y, himl->cx, himl->cy,
2542 hdcImage, 0, height, bmp.bmWidth, height, SRCCOPY);
2543 if (himl->hbmMask)
2544 StretchBlt (himl->hdcMask, pt.x, pt.y, himl->cx, himl->cy,
2545 hdcImage, 0, 0, bmp.bmWidth, height, SRCCOPY);
2548 done:
2549 DestroyIcon(hBestFitIcon);
2550 if (hdcImage)
2551 DeleteDC (hdcImage);
2552 if (ii.hbmColor)
2553 DeleteObject (ii.hbmColor);
2554 if (ii.hbmMask)
2555 DeleteObject (ii.hbmMask);
2557 TRACE("Insert index = %d, himl->cCurImage = %d\n", nIndex, himl->cCurImage);
2558 return nIndex;
2562 /*************************************************************************
2563 * ImageList_SetBkColor [COMCTL32.@]
2565 * Sets the background color of an image list.
2567 * PARAMS
2568 * himl [I] handle to image list
2569 * clrBk [I] background color
2571 * RETURNS
2572 * Success: previous background color
2573 * Failure: CLR_NONE
2576 COLORREF WINAPI
2577 ImageList_SetBkColor (HIMAGELIST himl, COLORREF clrBk)
2579 COLORREF clrOldBk;
2581 if (!is_valid(himl))
2582 return CLR_NONE;
2584 clrOldBk = himl->clrBk;
2585 himl->clrBk = clrBk;
2586 return clrOldBk;
2590 /*************************************************************************
2591 * ImageList_SetDragCursorImage [COMCTL32.@]
2593 * Combines the specified image with the current drag image
2595 * PARAMS
2596 * himlDrag [I] handle to drag image list
2597 * iDrag [I] drag image index
2598 * dxHotspot [I] X position of the hot spot
2599 * dyHotspot [I] Y position of the hot spot
2601 * RETURNS
2602 * Success: TRUE
2603 * Failure: FALSE
2605 * NOTES
2606 * - The names dxHotspot, dyHotspot are misleading because they have nothing
2607 * to do with a hotspot but are only the offset of the origin of the new
2608 * image relative to the origin of the old image.
2610 * - When this function is called and the drag image is visible, a
2611 * short flickering occurs but this matches the Win9x behavior. It is
2612 * possible to fix the flickering using code like in ImageList_DragMove.
2615 BOOL WINAPI
2616 ImageList_SetDragCursorImage (HIMAGELIST himlDrag, INT iDrag,
2617 INT dxHotspot, INT dyHotspot)
2619 HIMAGELIST himlTemp;
2620 BOOL visible;
2622 if (!is_valid(InternalDrag.himl) || !is_valid(himlDrag))
2623 return FALSE;
2625 TRACE(" dxH=%d dyH=%d nX=%d nY=%d\n",
2626 dxHotspot, dyHotspot, InternalDrag.dxHotspot, InternalDrag.dyHotspot);
2628 visible = InternalDrag.bShow;
2630 himlTemp = ImageList_Merge (InternalDrag.himl, 0, himlDrag, iDrag,
2631 dxHotspot, dyHotspot);
2633 if (visible) {
2634 /* hide the drag image */
2635 ImageList_DragShowNolock(FALSE);
2637 if ((InternalDrag.himl->cx != himlTemp->cx) ||
2638 (InternalDrag.himl->cy != himlTemp->cy)) {
2639 /* the size of the drag image changed, invalidate the buffer */
2640 DeleteObject(InternalDrag.hbmBg);
2641 InternalDrag.hbmBg = 0;
2644 ImageList_Destroy (InternalDrag.himl);
2645 InternalDrag.himl = himlTemp;
2647 if (visible) {
2648 /* show the drag image */
2649 ImageList_DragShowNolock(TRUE);
2652 return TRUE;
2656 /*************************************************************************
2657 * ImageList_SetFilter [COMCTL32.@]
2659 * Sets a filter (or does something completely different)!!???
2660 * It removes 12 Bytes from the stack (3 Parameters).
2662 * PARAMS
2663 * himl [I] SHOULD be a handle to image list
2664 * i [I] COULD be an index?
2665 * dwFilter [I] ???
2667 * RETURNS
2668 * Success: TRUE ???
2669 * Failure: FALSE ???
2671 * BUGS
2672 * This is an UNDOCUMENTED function!!!!
2673 * empty stub.
2676 BOOL WINAPI
2677 ImageList_SetFilter (HIMAGELIST himl, INT i, DWORD dwFilter)
2679 FIXME("(%p 0x%x 0x%x):empty stub!\n", himl, i, dwFilter);
2681 return FALSE;
2685 /*************************************************************************
2686 * ImageList_SetFlags [COMCTL32.@]
2688 * Sets the image list flags.
2690 * PARAMS
2691 * himl [I] Handle to image list
2692 * flags [I] Flags to set
2694 * RETURNS
2695 * Old flags?
2697 * BUGS
2698 * Stub.
2701 DWORD WINAPI
2702 ImageList_SetFlags(HIMAGELIST himl, DWORD flags)
2704 FIXME("(%p %08x):empty stub\n", himl, flags);
2705 return 0;
2709 /*************************************************************************
2710 * ImageList_SetIconSize [COMCTL32.@]
2712 * Sets the image size of the bitmap and deletes all images.
2714 * PARAMS
2715 * himl [I] handle to image list
2716 * cx [I] image width
2717 * cy [I] image height
2719 * RETURNS
2720 * Success: TRUE
2721 * Failure: FALSE
2724 BOOL WINAPI
2725 ImageList_SetIconSize (HIMAGELIST himl, INT cx, INT cy)
2727 INT nCount;
2728 HBITMAP hbmNew;
2730 if (!is_valid(himl))
2731 return FALSE;
2733 /* remove all images */
2734 himl->cMaxImage = himl->cInitial + 1;
2735 himl->cCurImage = 0;
2736 himl->cx = cx;
2737 himl->cy = cy;
2739 /* initialize overlay mask indices */
2740 for (nCount = 0; nCount < MAX_OVERLAYIMAGE; nCount++)
2741 himl->nOvlIdx[nCount] = -1;
2743 hbmNew = ImageList_CreateImage(himl->hdcImage, himl, himl->cMaxImage);
2744 SelectObject (himl->hdcImage, hbmNew);
2745 DeleteObject (himl->hbmImage);
2746 himl->hbmImage = hbmNew;
2748 if (himl->hbmMask) {
2749 SIZE sz;
2750 imagelist_get_bitmap_size(himl, himl->cMaxImage, &sz);
2751 hbmNew = CreateBitmap (sz.cx, sz.cy, 1, 1, NULL);
2752 SelectObject (himl->hdcMask, hbmNew);
2753 DeleteObject (himl->hbmMask);
2754 himl->hbmMask = hbmNew;
2757 return TRUE;
2761 /*************************************************************************
2762 * ImageList_SetImageCount [COMCTL32.@]
2764 * Resizes an image list to the specified number of images.
2766 * PARAMS
2767 * himl [I] handle to image list
2768 * iImageCount [I] number of images in the image list
2770 * RETURNS
2771 * Success: TRUE
2772 * Failure: FALSE
2775 BOOL WINAPI
2776 ImageList_SetImageCount (HIMAGELIST himl, UINT iImageCount)
2778 HDC hdcBitmap;
2779 HBITMAP hbmNewBitmap, hbmOld;
2780 INT nNewCount, nCopyCount;
2782 TRACE("%p %d\n",himl,iImageCount);
2784 if (!is_valid(himl))
2785 return FALSE;
2786 if (himl->cMaxImage > iImageCount)
2788 himl->cCurImage = iImageCount;
2789 /* TODO: shrink the bitmap when cMaxImage-cCurImage>cGrow ? */
2790 return TRUE;
2793 nNewCount = iImageCount + himl->cGrow;
2794 nCopyCount = min(himl->cCurImage, iImageCount);
2796 hdcBitmap = CreateCompatibleDC (0);
2798 hbmNewBitmap = ImageList_CreateImage(hdcBitmap, himl, nNewCount);
2800 if (hbmNewBitmap != 0)
2802 hbmOld = SelectObject (hdcBitmap, hbmNewBitmap);
2803 imagelist_copy_images( himl, himl->hdcImage, hdcBitmap, 0, nCopyCount, 0 );
2804 SelectObject (hdcBitmap, hbmOld);
2806 /* FIXME: delete 'empty' image space? */
2808 SelectObject (himl->hdcImage, hbmNewBitmap);
2809 DeleteObject (himl->hbmImage);
2810 himl->hbmImage = hbmNewBitmap;
2812 else
2813 ERR("Could not create new image bitmap !\n");
2815 if (himl->hbmMask)
2817 SIZE sz;
2818 imagelist_get_bitmap_size( himl, nNewCount, &sz );
2819 hbmNewBitmap = CreateBitmap (sz.cx, sz.cy, 1, 1, NULL);
2820 if (hbmNewBitmap != 0)
2822 hbmOld = SelectObject (hdcBitmap, hbmNewBitmap);
2823 imagelist_copy_images( himl, himl->hdcMask, hdcBitmap, 0, nCopyCount, 0 );
2824 SelectObject (hdcBitmap, hbmOld);
2826 /* FIXME: delete 'empty' image space? */
2828 SelectObject (himl->hdcMask, hbmNewBitmap);
2829 DeleteObject (himl->hbmMask);
2830 himl->hbmMask = hbmNewBitmap;
2832 else
2833 ERR("Could not create new mask bitmap!\n");
2836 DeleteDC (hdcBitmap);
2838 if (himl->has_alpha)
2840 char *new_alpha = HeapReAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, himl->has_alpha, nNewCount );
2841 if (new_alpha) himl->has_alpha = new_alpha;
2842 else
2844 HeapFree( GetProcessHeap(), 0, himl->has_alpha );
2845 himl->has_alpha = NULL;
2849 /* Update max image count and current image count */
2850 himl->cMaxImage = nNewCount;
2851 himl->cCurImage = iImageCount;
2853 return TRUE;
2857 /*************************************************************************
2858 * ImageList_SetOverlayImage [COMCTL32.@]
2860 * Assigns an overlay mask index to an existing image in an image list.
2862 * PARAMS
2863 * himl [I] handle to image list
2864 * iImage [I] image index
2865 * iOverlay [I] overlay mask index
2867 * RETURNS
2868 * Success: TRUE
2869 * Failure: FALSE
2872 BOOL WINAPI
2873 ImageList_SetOverlayImage (HIMAGELIST himl, INT iImage, INT iOverlay)
2875 if (!is_valid(himl))
2876 return FALSE;
2877 if ((iOverlay < 1) || (iOverlay > MAX_OVERLAYIMAGE))
2878 return FALSE;
2879 if ((iImage!=-1) && ((iImage < 0) || (iImage > himl->cCurImage)))
2880 return FALSE;
2881 himl->nOvlIdx[iOverlay - 1] = iImage;
2882 return TRUE;
2887 /* helper for ImageList_Write - write bitmap to pstm
2888 * currently everything is written as 24 bit RGB, except masks
2890 static BOOL
2891 _write_bitmap(HBITMAP hBitmap, LPSTREAM pstm)
2893 LPBITMAPFILEHEADER bmfh;
2894 LPBITMAPINFOHEADER bmih;
2895 LPBYTE data = NULL, lpBits;
2896 BITMAP bm;
2897 INT bitCount, sizeImage, offBits, totalSize;
2898 HDC xdc;
2899 BOOL result = FALSE;
2901 if (!GetObjectW(hBitmap, sizeof(BITMAP), &bm))
2902 return FALSE;
2904 bitCount = bm.bmBitsPixel == 1 ? 1 : 24;
2905 sizeImage = DIB_GetDIBImageBytes(bm.bmWidth, bm.bmHeight, bitCount);
2907 totalSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
2908 if(bitCount != 24)
2909 totalSize += (1 << bitCount) * sizeof(RGBQUAD);
2910 offBits = totalSize;
2911 totalSize += sizeImage;
2913 data = Alloc(totalSize);
2914 bmfh = (LPBITMAPFILEHEADER)data;
2915 bmih = (LPBITMAPINFOHEADER)(data + sizeof(BITMAPFILEHEADER));
2916 lpBits = data + offBits;
2918 /* setup BITMAPFILEHEADER */
2919 bmfh->bfType = (('M' << 8) | 'B');
2920 bmfh->bfSize = offBits;
2921 bmfh->bfReserved1 = 0;
2922 bmfh->bfReserved2 = 0;
2923 bmfh->bfOffBits = offBits;
2925 /* setup BITMAPINFOHEADER */
2926 bmih->biSize = sizeof(BITMAPINFOHEADER);
2927 bmih->biWidth = bm.bmWidth;
2928 bmih->biHeight = bm.bmHeight;
2929 bmih->biPlanes = 1;
2930 bmih->biBitCount = bitCount;
2931 bmih->biCompression = BI_RGB;
2932 bmih->biSizeImage = sizeImage;
2933 bmih->biXPelsPerMeter = 0;
2934 bmih->biYPelsPerMeter = 0;
2935 bmih->biClrUsed = 0;
2936 bmih->biClrImportant = 0;
2938 xdc = GetDC(0);
2939 result = GetDIBits(xdc, hBitmap, 0, bm.bmHeight, lpBits, (BITMAPINFO *)bmih, DIB_RGB_COLORS) == bm.bmHeight;
2940 ReleaseDC(0, xdc);
2941 if (!result)
2942 goto failed;
2944 TRACE("width %u, height %u, planes %u, bpp %u\n",
2945 bmih->biWidth, bmih->biHeight,
2946 bmih->biPlanes, bmih->biBitCount);
2948 if(bitCount == 1) {
2949 /* Hack. */
2950 LPBITMAPINFO inf = (LPBITMAPINFO)bmih;
2951 inf->bmiColors[0].rgbRed = inf->bmiColors[0].rgbGreen = inf->bmiColors[0].rgbBlue = 0;
2952 inf->bmiColors[1].rgbRed = inf->bmiColors[1].rgbGreen = inf->bmiColors[1].rgbBlue = 0xff;
2955 if(FAILED(IStream_Write(pstm, data, totalSize, NULL)))
2956 goto failed;
2958 result = TRUE;
2960 failed:
2961 Free(data);
2963 return result;
2967 /*************************************************************************
2968 * ImageList_Write [COMCTL32.@]
2970 * Writes an image list to a stream.
2972 * PARAMS
2973 * himl [I] handle to image list
2974 * pstm [O] Pointer to a stream.
2976 * RETURNS
2977 * Success: TRUE
2978 * Failure: FALSE
2980 * BUGS
2981 * probably.
2984 BOOL WINAPI
2985 ImageList_Write (HIMAGELIST himl, LPSTREAM pstm)
2987 ILHEAD ilHead;
2988 int i;
2990 TRACE("%p %p\n", himl, pstm);
2992 if (!is_valid(himl))
2993 return FALSE;
2995 ilHead.usMagic = (('L' << 8) | 'I');
2996 ilHead.usVersion = 0x101;
2997 ilHead.cCurImage = himl->cCurImage;
2998 ilHead.cMaxImage = himl->cMaxImage;
2999 ilHead.cGrow = himl->cGrow;
3000 ilHead.cx = himl->cx;
3001 ilHead.cy = himl->cy;
3002 ilHead.bkcolor = himl->clrBk;
3003 ilHead.flags = himl->flags;
3004 for(i = 0; i < 4; i++) {
3005 ilHead.ovls[i] = himl->nOvlIdx[i];
3008 TRACE("cx %u, cy %u, flags 0x04%x, cCurImage %u, cMaxImage %u\n",
3009 ilHead.cx, ilHead.cy, ilHead.flags, ilHead.cCurImage, ilHead.cMaxImage);
3011 if(FAILED(IStream_Write(pstm, &ilHead, sizeof(ILHEAD), NULL)))
3012 return FALSE;
3014 /* write the bitmap */
3015 if(!_write_bitmap(himl->hbmImage, pstm))
3016 return FALSE;
3018 /* write the mask if we have one */
3019 if(himl->flags & ILC_MASK) {
3020 if(!_write_bitmap(himl->hbmMask, pstm))
3021 return FALSE;
3024 return TRUE;
3028 static HBITMAP ImageList_CreateImage(HDC hdc, HIMAGELIST himl, UINT count)
3030 HBITMAP hbmNewBitmap;
3031 UINT ilc = (himl->flags & 0xFE);
3032 SIZE sz;
3034 imagelist_get_bitmap_size( himl, count, &sz );
3036 if ((ilc >= ILC_COLOR4 && ilc <= ILC_COLOR32) || ilc == ILC_COLOR)
3038 VOID* bits;
3039 BITMAPINFO *bmi;
3041 TRACE("Creating DIBSection %d x %d, %d Bits per Pixel\n",
3042 sz.cx, sz.cy, himl->uBitsPixel);
3044 if (himl->uBitsPixel <= ILC_COLOR8)
3046 LPPALETTEENTRY pal;
3047 ULONG i, colors;
3048 BYTE temp;
3050 colors = 1 << himl->uBitsPixel;
3051 bmi = Alloc(sizeof(BITMAPINFOHEADER) +
3052 sizeof(PALETTEENTRY) * colors);
3054 pal = (LPPALETTEENTRY)bmi->bmiColors;
3055 GetPaletteEntries(GetStockObject(DEFAULT_PALETTE), 0, colors, pal);
3057 /* Swap colors returned by GetPaletteEntries so we can use them for
3058 * CreateDIBSection call. */
3059 for (i = 0; i < colors; i++)
3061 temp = pal[i].peBlue;
3062 bmi->bmiColors[i].rgbRed = pal[i].peRed;
3063 bmi->bmiColors[i].rgbBlue = temp;
3066 else
3068 bmi = Alloc(sizeof(BITMAPINFOHEADER));
3071 bmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
3072 bmi->bmiHeader.biWidth = sz.cx;
3073 bmi->bmiHeader.biHeight = sz.cy;
3074 bmi->bmiHeader.biPlanes = 1;
3075 bmi->bmiHeader.biBitCount = himl->uBitsPixel;
3076 bmi->bmiHeader.biCompression = BI_RGB;
3077 bmi->bmiHeader.biSizeImage = 0;
3078 bmi->bmiHeader.biXPelsPerMeter = 0;
3079 bmi->bmiHeader.biYPelsPerMeter = 0;
3080 bmi->bmiHeader.biClrUsed = 0;
3081 bmi->bmiHeader.biClrImportant = 0;
3083 hbmNewBitmap = CreateDIBSection(hdc, bmi, DIB_RGB_COLORS, &bits, 0, 0);
3085 Free (bmi);
3087 else /*if (ilc == ILC_COLORDDB)*/
3089 TRACE("Creating Bitmap: %d Bits per Pixel\n", himl->uBitsPixel);
3091 hbmNewBitmap = CreateBitmap (sz.cx, sz.cy, 1, himl->uBitsPixel, NULL);
3093 TRACE("returning %p\n", hbmNewBitmap);
3094 return hbmNewBitmap;
3097 /*************************************************************************
3098 * ImageList_SetColorTable [COMCTL32.@]
3100 * Sets the color table of an image list.
3102 * PARAMS
3103 * himl [I] Handle to the image list.
3104 * uStartIndex [I] The first index to set.
3105 * cEntries [I] Number of entries to set.
3106 * prgb [I] New color information for color table for the image list.
3108 * RETURNS
3109 * Success: Number of entries in the table that were set.
3110 * Failure: Zero.
3112 * SEE
3113 * ImageList_Create(), SetDIBColorTable()
3116 UINT WINAPI
3117 ImageList_SetColorTable (HIMAGELIST himl, UINT uStartIndex, UINT cEntries, CONST RGBQUAD * prgb)
3119 return SetDIBColorTable(himl->hdcImage, uStartIndex, cEntries, prgb);
3122 /*************************************************************************
3123 * ImageList_CoCreateInstance [COMCTL32.@]
3125 * Creates a new imagelist instance and returns an interface pointer to it.
3127 * PARAMS
3128 * rclsid [I] A reference to the CLSID (CLSID_ImageList).
3129 * punkOuter [I] Pointer to IUnknown interface for aggregation, if desired
3130 * riid [I] Identifier of the requested interface.
3131 * ppv [O] Returns the address of the pointer requested, or NULL.
3133 * RETURNS
3134 * Success: S_OK.
3135 * Failure: Error value.
3137 HRESULT WINAPI
3138 ImageList_CoCreateInstance (REFCLSID rclsid, const IUnknown *punkOuter, REFIID riid, void **ppv)
3140 TRACE("(%s,%p,%s,%p)\n", debugstr_guid(rclsid), punkOuter, debugstr_guid(riid), ppv);
3142 if (!IsEqualCLSID(&CLSID_ImageList, rclsid))
3143 return E_NOINTERFACE;
3145 return ImageListImpl_CreateInstance(punkOuter, riid, ppv);
3149 /*************************************************************************
3150 * IImageList implementation
3153 static HRESULT WINAPI ImageListImpl_QueryInterface(IImageList *iface,
3154 REFIID iid, void **ppv)
3156 HIMAGELIST This = (HIMAGELIST) iface;
3157 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
3159 if (!ppv) return E_INVALIDARG;
3161 if (IsEqualIID(&IID_IUnknown, iid) || IsEqualIID(&IID_IImageList, iid))
3162 *ppv = This;
3163 else
3165 *ppv = NULL;
3166 return E_NOINTERFACE;
3169 IUnknown_AddRef((IUnknown*)*ppv);
3170 return S_OK;
3173 static ULONG WINAPI ImageListImpl_AddRef(IImageList *iface)
3175 HIMAGELIST This = (HIMAGELIST) iface;
3176 ULONG ref = InterlockedIncrement(&This->ref);
3178 TRACE("(%p) refcount=%u\n", iface, ref);
3179 return ref;
3182 static ULONG WINAPI ImageListImpl_Release(IImageList *iface)
3184 HIMAGELIST This = (HIMAGELIST) iface;
3185 ULONG ref = InterlockedDecrement(&This->ref);
3187 TRACE("(%p) refcount=%u\n", iface, ref);
3189 if (ref == 0)
3191 /* delete image bitmaps */
3192 if (This->hbmImage) DeleteObject (This->hbmImage);
3193 if (This->hbmMask) DeleteObject (This->hbmMask);
3195 /* delete image & mask DCs */
3196 if (This->hdcImage) DeleteDC (This->hdcImage);
3197 if (This->hdcMask) DeleteDC (This->hdcMask);
3199 /* delete blending brushes */
3200 if (This->hbrBlend25) DeleteObject (This->hbrBlend25);
3201 if (This->hbrBlend50) DeleteObject (This->hbrBlend50);
3203 This->lpVtbl = NULL;
3204 HeapFree(GetProcessHeap(), 0, This->has_alpha);
3205 HeapFree(GetProcessHeap(), 0, This);
3208 return ref;
3211 static HRESULT WINAPI ImageListImpl_Add(IImageList *iface, HBITMAP hbmImage,
3212 HBITMAP hbmMask, int *pi)
3214 HIMAGELIST This = (HIMAGELIST) iface;
3215 int ret;
3217 if (!pi)
3218 return E_FAIL;
3220 ret = ImageList_Add(This, hbmImage, hbmMask);
3222 if (ret == -1)
3223 return E_FAIL;
3225 *pi = ret;
3226 return S_OK;
3229 static HRESULT WINAPI ImageListImpl_ReplaceIcon(IImageList *iface, int i,
3230 HICON hicon, int *pi)
3232 HIMAGELIST This = (HIMAGELIST) iface;
3233 int ret;
3235 if (!pi)
3236 return E_FAIL;
3238 ret = ImageList_ReplaceIcon(This, i, hicon);
3240 if (ret == -1)
3241 return E_FAIL;
3243 *pi = ret;
3244 return S_OK;
3247 static HRESULT WINAPI ImageListImpl_SetOverlayImage(IImageList *iface,
3248 int iImage, int iOverlay)
3250 return ImageList_SetOverlayImage((HIMAGELIST) iface, iImage, iOverlay)
3251 ? S_OK : E_FAIL;
3254 static HRESULT WINAPI ImageListImpl_Replace(IImageList *iface, int i,
3255 HBITMAP hbmImage, HBITMAP hbmMask)
3257 return ImageList_Replace((HIMAGELIST) iface, i, hbmImage, hbmMask) ? S_OK :
3258 E_FAIL;
3261 static HRESULT WINAPI ImageListImpl_AddMasked(IImageList *iface, HBITMAP hbmImage,
3262 COLORREF crMask, int *pi)
3264 HIMAGELIST This = (HIMAGELIST) iface;
3265 int ret;
3267 if (!pi)
3268 return E_FAIL;
3270 ret = ImageList_AddMasked(This, hbmImage, crMask);
3272 if (ret == -1)
3273 return E_FAIL;
3275 *pi = ret;
3276 return S_OK;
3279 static HRESULT WINAPI ImageListImpl_Draw(IImageList *iface,
3280 IMAGELISTDRAWPARAMS *pimldp)
3282 HIMAGELIST This = (HIMAGELIST) iface;
3283 HIMAGELIST old_himl;
3284 int ret;
3286 /* As far as I can tell, Windows simply ignores the contents of pimldp->himl
3287 so we shall simulate the same */
3288 old_himl = pimldp->himl;
3289 pimldp->himl = This;
3291 ret = ImageList_DrawIndirect(pimldp);
3293 pimldp->himl = old_himl;
3294 return ret ? S_OK : E_INVALIDARG;
3297 static HRESULT WINAPI ImageListImpl_Remove(IImageList *iface, int i)
3299 return (ImageList_Remove((HIMAGELIST) iface, i) == 0) ? E_INVALIDARG : S_OK;
3302 static HRESULT WINAPI ImageListImpl_GetIcon(IImageList *iface, int i, UINT flags,
3303 HICON *picon)
3305 HICON hIcon;
3307 if (!picon)
3308 return E_FAIL;
3310 hIcon = ImageList_GetIcon((HIMAGELIST) iface, i, flags);
3312 if (hIcon == NULL)
3313 return E_FAIL;
3315 *picon = hIcon;
3316 return S_OK;
3319 static HRESULT WINAPI ImageListImpl_GetImageInfo(IImageList *iface, int i,
3320 IMAGEINFO *pImageInfo)
3322 return ImageList_GetImageInfo((HIMAGELIST) iface, i, pImageInfo) ? S_OK : E_FAIL;
3325 static HRESULT WINAPI ImageListImpl_Copy(IImageList *iface, int iDst,
3326 IUnknown *punkSrc, int iSrc, UINT uFlags)
3328 HIMAGELIST This = (HIMAGELIST) iface;
3329 IImageList *src = NULL;
3330 HRESULT ret;
3332 if (!punkSrc)
3333 return E_FAIL;
3335 /* TODO: Add test for IID_ImageList2 too */
3336 if (FAILED(IImageList_QueryInterface(punkSrc, &IID_IImageList,
3337 (void **) &src)))
3338 return E_FAIL;
3340 if (ImageList_Copy(This, iDst, (HIMAGELIST) src, iSrc, uFlags))
3341 ret = S_OK;
3342 else
3343 ret = E_FAIL;
3345 IImageList_Release(src);
3346 return ret;
3349 static HRESULT WINAPI ImageListImpl_Merge(IImageList *iface, int i1,
3350 IUnknown *punk2, int i2, int dx, int dy, REFIID riid, void **ppv)
3352 HIMAGELIST This = (HIMAGELIST) iface;
3353 IImageList *iml2 = NULL;
3354 HIMAGELIST hNew;
3355 HRESULT ret = E_FAIL;
3357 TRACE("(%p)->(%d %p %d %d %d %s %p)\n", iface, i1, punk2, i2, dx, dy, debugstr_guid(riid), ppv);
3359 /* TODO: Add test for IID_ImageList2 too */
3360 if (FAILED(IImageList_QueryInterface(punk2, &IID_IImageList,
3361 (void **) &iml2)))
3362 return E_FAIL;
3364 hNew = ImageList_Merge(This, i1, (HIMAGELIST) iml2, i2, dx, dy);
3366 /* Get the interface for the new image list */
3367 if (hNew)
3369 IImageList *imerge = (IImageList*)hNew;
3371 ret = HIMAGELIST_QueryInterface(hNew, riid, ppv);
3372 IImageList_Release(imerge);
3375 IImageList_Release(iml2);
3376 return ret;
3379 static HRESULT WINAPI ImageListImpl_Clone(IImageList *iface, REFIID riid, void **ppv)
3381 HIMAGELIST This = (HIMAGELIST) iface;
3382 HIMAGELIST clone;
3383 HRESULT ret = E_FAIL;
3385 TRACE("(%p)->(%s %p)\n", iface, debugstr_guid(riid), ppv);
3387 clone = ImageList_Duplicate(This);
3389 /* Get the interface for the new image list */
3390 if (clone)
3392 IImageList *iclone = (IImageList*)clone;
3394 ret = HIMAGELIST_QueryInterface(clone, riid, ppv);
3395 IImageList_Release(iclone);
3398 return ret;
3401 static HRESULT WINAPI ImageListImpl_GetImageRect(IImageList *iface, int i,
3402 RECT *prc)
3404 HIMAGELIST This = (HIMAGELIST) iface;
3405 IMAGEINFO info;
3407 if (!prc)
3408 return E_FAIL;
3410 if (!ImageList_GetImageInfo(This, i, &info))
3411 return E_FAIL;
3413 return CopyRect(prc, &info.rcImage) ? S_OK : E_FAIL;
3416 static HRESULT WINAPI ImageListImpl_GetIconSize(IImageList *iface, int *cx,
3417 int *cy)
3419 HIMAGELIST This = (HIMAGELIST) iface;
3421 return ImageList_GetIconSize(This, cx, cy) ? S_OK : E_INVALIDARG;
3424 static HRESULT WINAPI ImageListImpl_SetIconSize(IImageList *iface, int cx,
3425 int cy)
3427 return ImageList_SetIconSize((HIMAGELIST) iface, cx, cy) ? S_OK : E_FAIL;
3430 static HRESULT WINAPI ImageListImpl_GetImageCount(IImageList *iface, int *pi)
3432 *pi = ImageList_GetImageCount((HIMAGELIST) iface);
3433 return S_OK;
3436 static HRESULT WINAPI ImageListImpl_SetImageCount(IImageList *iface,
3437 UINT uNewCount)
3439 return ImageList_SetImageCount((HIMAGELIST) iface, uNewCount) ? S_OK : E_FAIL;
3442 static HRESULT WINAPI ImageListImpl_SetBkColor(IImageList *iface, COLORREF clrBk,
3443 COLORREF *pclr)
3445 *pclr = ImageList_SetBkColor((HIMAGELIST) iface, clrBk);
3446 return S_OK;
3449 static HRESULT WINAPI ImageListImpl_GetBkColor(IImageList *iface, COLORREF *pclr)
3451 *pclr = ImageList_GetBkColor((HIMAGELIST) iface);
3452 return S_OK;
3455 static HRESULT WINAPI ImageListImpl_BeginDrag(IImageList *iface, int iTrack,
3456 int dxHotspot, int dyHotspot)
3458 return ImageList_BeginDrag((HIMAGELIST) iface, iTrack, dxHotspot, dyHotspot) ? S_OK : E_FAIL;
3461 static HRESULT WINAPI ImageListImpl_EndDrag(IImageList *iface)
3463 ImageList_EndDrag();
3464 return S_OK;
3467 static HRESULT WINAPI ImageListImpl_DragEnter(IImageList *iface, HWND hwndLock,
3468 int x, int y)
3470 return ImageList_DragEnter(hwndLock, x, y) ? S_OK : E_FAIL;
3473 static HRESULT WINAPI ImageListImpl_DragLeave(IImageList *iface, HWND hwndLock)
3475 return ImageList_DragLeave(hwndLock) ? S_OK : E_FAIL;
3478 static HRESULT WINAPI ImageListImpl_DragMove(IImageList *iface, int x, int y)
3480 return ImageList_DragMove(x, y) ? S_OK : E_FAIL;
3483 static HRESULT WINAPI ImageListImpl_SetDragCursorImage(IImageList *iface,
3484 IUnknown *punk, int iDrag, int dxHotspot, int dyHotspot)
3486 IImageList *iml2 = NULL;
3487 HRESULT ret;
3489 if (!punk)
3490 return E_FAIL;
3492 /* TODO: Add test for IID_ImageList2 too */
3493 if (FAILED(IImageList_QueryInterface(punk, &IID_IImageList,
3494 (void **) &iml2)))
3495 return E_FAIL;
3497 ret = ImageList_SetDragCursorImage((HIMAGELIST) iml2, iDrag, dxHotspot,
3498 dyHotspot);
3500 IImageList_Release(iml2);
3502 return ret ? S_OK : E_FAIL;
3505 static HRESULT WINAPI ImageListImpl_DragShowNolock(IImageList *iface, BOOL fShow)
3507 return ImageList_DragShowNolock(fShow) ? S_OK : E_FAIL;
3510 static HRESULT WINAPI ImageListImpl_GetDragImage(IImageList *iface, POINT *ppt,
3511 POINT *pptHotspot, REFIID riid, PVOID *ppv)
3513 HRESULT ret = E_FAIL;
3514 HIMAGELIST hNew;
3516 if (!ppv)
3517 return E_FAIL;
3519 hNew = ImageList_GetDragImage(ppt, pptHotspot);
3521 /* Get the interface for the new image list */
3522 if (hNew)
3524 IImageList *idrag = (IImageList*)hNew;
3526 ret = HIMAGELIST_QueryInterface(hNew, riid, ppv);
3527 IImageList_Release(idrag);
3530 return ret;
3533 static HRESULT WINAPI ImageListImpl_GetItemFlags(IImageList *iface, int i,
3534 DWORD *dwFlags)
3536 FIXME("STUB: %p %d %p\n", iface, i, dwFlags);
3537 return E_NOTIMPL;
3540 static HRESULT WINAPI ImageListImpl_GetOverlayImage(IImageList *iface, int iOverlay,
3541 int *piIndex)
3543 HIMAGELIST This = (HIMAGELIST) iface;
3544 int i;
3546 if ((iOverlay < 0) || (iOverlay > This->cCurImage))
3547 return E_FAIL;
3549 for (i = 0; i < MAX_OVERLAYIMAGE; i++)
3551 if (This->nOvlIdx[i] == iOverlay)
3553 *piIndex = i + 1;
3554 return S_OK;
3558 return E_FAIL;
3562 static const IImageListVtbl ImageListImpl_Vtbl = {
3563 ImageListImpl_QueryInterface,
3564 ImageListImpl_AddRef,
3565 ImageListImpl_Release,
3566 ImageListImpl_Add,
3567 ImageListImpl_ReplaceIcon,
3568 ImageListImpl_SetOverlayImage,
3569 ImageListImpl_Replace,
3570 ImageListImpl_AddMasked,
3571 ImageListImpl_Draw,
3572 ImageListImpl_Remove,
3573 ImageListImpl_GetIcon,
3574 ImageListImpl_GetImageInfo,
3575 ImageListImpl_Copy,
3576 ImageListImpl_Merge,
3577 ImageListImpl_Clone,
3578 ImageListImpl_GetImageRect,
3579 ImageListImpl_GetIconSize,
3580 ImageListImpl_SetIconSize,
3581 ImageListImpl_GetImageCount,
3582 ImageListImpl_SetImageCount,
3583 ImageListImpl_SetBkColor,
3584 ImageListImpl_GetBkColor,
3585 ImageListImpl_BeginDrag,
3586 ImageListImpl_EndDrag,
3587 ImageListImpl_DragEnter,
3588 ImageListImpl_DragLeave,
3589 ImageListImpl_DragMove,
3590 ImageListImpl_SetDragCursorImage,
3591 ImageListImpl_DragShowNolock,
3592 ImageListImpl_GetDragImage,
3593 ImageListImpl_GetItemFlags,
3594 ImageListImpl_GetOverlayImage
3597 static inline BOOL is_valid(HIMAGELIST himl)
3599 BOOL valid;
3600 __TRY
3602 valid = himl && himl->lpVtbl == &ImageListImpl_Vtbl;
3604 __EXCEPT_PAGE_FAULT
3606 valid = FALSE;
3608 __ENDTRY
3609 return valid;
3612 /*************************************************************************
3613 * HIMAGELIST_QueryInterface [COMCTL32.@]
3615 * Returns a pointer to an IImageList or IImageList2 object for the given
3616 * HIMAGELIST.
3618 * PARAMS
3619 * himl [I] Image list handle.
3620 * riid [I] Identifier of the requested interface.
3621 * ppv [O] Returns the address of the pointer requested, or NULL.
3623 * RETURNS
3624 * Success: S_OK.
3625 * Failure: Error value.
3627 HRESULT WINAPI
3628 HIMAGELIST_QueryInterface (HIMAGELIST himl, REFIID riid, void **ppv)
3630 TRACE("(%p,%s,%p)\n", himl, debugstr_guid(riid), ppv);
3631 return IImageList_QueryInterface((IImageList *) himl, riid, ppv);
3634 static HRESULT ImageListImpl_CreateInstance(const IUnknown *pUnkOuter, REFIID iid, void** ppv)
3636 HIMAGELIST This;
3637 HRESULT ret;
3639 TRACE("(%p,%s,%p)\n", pUnkOuter, debugstr_guid(iid), ppv);
3641 *ppv = NULL;
3643 if (pUnkOuter) return CLASS_E_NOAGGREGATION;
3645 This = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct _IMAGELIST));
3646 if (!This) return E_OUTOFMEMORY;
3648 This->lpVtbl = &ImageListImpl_Vtbl;
3649 This->ref = 1;
3651 ret = IUnknown_QueryInterface((IUnknown*)This, iid, ppv);
3652 IUnknown_Release((IUnknown*)This);
3654 return ret;