quartz: Free two assert calls from having side effects.
[wine/testsucceed.git] / dlls / comctl32 / imagelist.c
blob83b8b447474e0aa957b035e4b03499dbd1fd63ac
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 if (!(mask = CreateDIBSection( himl->hdcMask, info, DIB_RGB_COLORS, &mask_bits, 0, 0 )))
1230 goto done;
1231 SelectObject( hdc, mask );
1232 BitBlt( hdc, 0, 0, cx, cy, himl->hdcMask, src_x, src_y, SRCCOPY );
1233 SelectObject( hdc, bmp );
1234 for (i = 0, ptr = bits; i < cy; i++)
1235 for (j = 0; j < cx; j++, ptr++)
1236 if ((((BYTE *)mask_bits)[i * width_bytes + j / 8] << (j % 8)) & 0x80) *ptr = 0;
1237 else *ptr |= 0xff000000;
1240 ret = GdiAlphaBlend( dest_dc, dest_x, dest_y, cx, cy, hdc, 0, 0, cx, cy, func );
1242 done:
1243 DeleteDC( hdc );
1244 if (bmp) DeleteObject( bmp );
1245 if (mask) DeleteObject( mask );
1246 HeapFree( GetProcessHeap(), 0, info );
1247 return ret;
1250 /*************************************************************************
1251 * ImageList_DrawIndirect [COMCTL32.@]
1253 * Draws an image using various parameters specified in pimldp.
1255 * PARAMS
1256 * pimldp [I] pointer to IMAGELISTDRAWPARAMS structure.
1258 * RETURNS
1259 * Success: TRUE
1260 * Failure: FALSE
1263 BOOL WINAPI
1264 ImageList_DrawIndirect (IMAGELISTDRAWPARAMS *pimldp)
1266 INT cx, cy, nOvlIdx;
1267 DWORD fState, dwRop;
1268 UINT fStyle;
1269 COLORREF oldImageBk, oldImageFg;
1270 HDC hImageDC, hImageListDC, hMaskListDC;
1271 HBITMAP hImageBmp, hOldImageBmp, hBlendMaskBmp;
1272 BOOL bIsTransparent, bBlend, bResult = FALSE, bMask;
1273 HIMAGELIST himl;
1274 HBRUSH hOldBrush;
1275 POINT pt;
1276 BOOL has_alpha;
1278 if (!pimldp || !(himl = pimldp->himl)) return FALSE;
1279 if (!is_valid(himl)) return FALSE;
1280 if ((pimldp->i < 0) || (pimldp->i >= himl->cCurImage)) return FALSE;
1282 imagelist_point_from_index( himl, pimldp->i, &pt );
1283 pt.x += pimldp->xBitmap;
1284 pt.y += pimldp->yBitmap;
1286 fState = pimldp->cbSize < sizeof(IMAGELISTDRAWPARAMS) ? ILS_NORMAL : pimldp->fState;
1287 fStyle = pimldp->fStyle & ~ILD_OVERLAYMASK;
1288 cx = (pimldp->cx == 0) ? himl->cx : pimldp->cx;
1289 cy = (pimldp->cy == 0) ? himl->cy : pimldp->cy;
1291 bIsTransparent = (fStyle & ILD_TRANSPARENT);
1292 if( pimldp->rgbBk == CLR_NONE )
1293 bIsTransparent = TRUE;
1294 if( ( pimldp->rgbBk == CLR_DEFAULT ) && ( himl->clrBk == CLR_NONE ) )
1295 bIsTransparent = TRUE;
1296 bMask = (himl->flags & ILC_MASK) && (fStyle & ILD_MASK) ;
1297 bBlend = (fStyle & (ILD_BLEND25 | ILD_BLEND50) ) && !bMask;
1299 TRACE("himl(%p) hbmMask(%p) iImage(%d) x(%d) y(%d) cx(%d) cy(%d)\n",
1300 himl, himl->hbmMask, pimldp->i, pimldp->x, pimldp->y, cx, cy);
1302 /* we will use these DCs to access the images and masks in the ImageList */
1303 hImageListDC = himl->hdcImage;
1304 hMaskListDC = himl->hdcMask;
1306 /* these will accumulate the image and mask for the image we're drawing */
1307 hImageDC = CreateCompatibleDC( pimldp->hdcDst );
1308 hImageBmp = CreateCompatibleBitmap( pimldp->hdcDst, cx, cy );
1309 hBlendMaskBmp = bBlend ? CreateBitmap(cx, cy, 1, 1, NULL) : 0;
1311 /* Create a compatible DC. */
1312 if (!hImageListDC || !hImageDC || !hImageBmp ||
1313 (bBlend && !hBlendMaskBmp) || (himl->hbmMask && !hMaskListDC))
1314 goto cleanup;
1316 hOldImageBmp = SelectObject(hImageDC, hImageBmp);
1319 * To obtain a transparent look, background color should be set
1320 * to white and foreground color to black when blitting the
1321 * monochrome mask.
1323 oldImageFg = SetTextColor( hImageDC, RGB( 0, 0, 0 ) );
1324 oldImageBk = SetBkColor( hImageDC, RGB( 0xff, 0xff, 0xff ) );
1326 has_alpha = (himl->has_alpha && himl->has_alpha[pimldp->i]);
1327 if (!bMask && (has_alpha || (fState & ILS_ALPHA)))
1329 COLORREF colour, blend_col = CLR_NONE;
1330 BLENDFUNCTION func;
1332 if (bBlend)
1334 blend_col = pimldp->rgbFg;
1335 if (blend_col == CLR_DEFAULT) blend_col = GetSysColor( COLOR_HIGHLIGHT );
1336 else if (blend_col == CLR_NONE) blend_col = GetTextColor( pimldp->hdcDst );
1339 func.BlendOp = AC_SRC_OVER;
1340 func.BlendFlags = 0;
1341 func.SourceConstantAlpha = (fState & ILS_ALPHA) ? pimldp->Frame : 255;
1342 func.AlphaFormat = AC_SRC_ALPHA;
1344 if (bIsTransparent)
1346 bResult = alpha_blend_image( himl, pimldp->hdcDst, pimldp->x, pimldp->y,
1347 pt.x, pt.y, cx, cy, func, fStyle, blend_col );
1348 goto end;
1350 colour = pimldp->rgbBk;
1351 if (colour == CLR_DEFAULT) colour = himl->clrBk;
1352 if (colour == CLR_NONE) colour = GetBkColor( pimldp->hdcDst );
1354 hOldBrush = SelectObject (hImageDC, CreateSolidBrush (colour));
1355 PatBlt( hImageDC, 0, 0, cx, cy, PATCOPY );
1356 alpha_blend_image( himl, hImageDC, 0, 0, pt.x, pt.y, cx, cy, func, fStyle, blend_col );
1357 DeleteObject (SelectObject (hImageDC, hOldBrush));
1358 bResult = BitBlt( pimldp->hdcDst, pimldp->x, pimldp->y, cx, cy, hImageDC, 0, 0, SRCCOPY );
1359 goto end;
1363 * Draw the initial image
1365 if( bMask ) {
1366 if (himl->hbmMask) {
1367 hOldBrush = SelectObject (hImageDC, CreateSolidBrush (GetTextColor(pimldp->hdcDst)));
1368 PatBlt( hImageDC, 0, 0, cx, cy, PATCOPY );
1369 BitBlt(hImageDC, 0, 0, cx, cy, hMaskListDC, pt.x, pt.y, SRCPAINT);
1370 DeleteObject (SelectObject (hImageDC, hOldBrush));
1371 if( bIsTransparent )
1373 BitBlt ( pimldp->hdcDst, pimldp->x, pimldp->y, cx, cy, hImageDC, 0, 0, SRCAND);
1374 bResult = TRUE;
1375 goto end;
1377 } else {
1378 hOldBrush = SelectObject (hImageDC, GetStockObject(BLACK_BRUSH));
1379 PatBlt( hImageDC, 0, 0, cx, cy, PATCOPY);
1380 SelectObject(hImageDC, hOldBrush);
1382 } else {
1383 /* blend the image with the needed solid background */
1384 COLORREF colour = RGB(0,0,0);
1386 if( !bIsTransparent )
1388 colour = pimldp->rgbBk;
1389 if( colour == CLR_DEFAULT )
1390 colour = himl->clrBk;
1391 if( colour == CLR_NONE )
1392 colour = GetBkColor(pimldp->hdcDst);
1395 hOldBrush = SelectObject (hImageDC, CreateSolidBrush (colour));
1396 PatBlt( hImageDC, 0, 0, cx, cy, PATCOPY );
1397 if (himl->hbmMask)
1399 BitBlt( hImageDC, 0, 0, cx, cy, hMaskListDC, pt.x, pt.y, SRCAND );
1400 BitBlt( hImageDC, 0, 0, cx, cy, hImageListDC, pt.x, pt.y, SRCPAINT );
1402 else
1403 BitBlt( hImageDC, 0, 0, cx, cy, hImageListDC, pt.x, pt.y, SRCCOPY);
1404 DeleteObject (SelectObject (hImageDC, hOldBrush));
1407 /* Time for blending, if required */
1408 if (bBlend) {
1409 HBRUSH hBlendBrush;
1410 COLORREF clrBlend = pimldp->rgbFg;
1411 HDC hBlendMaskDC = hImageListDC;
1412 HBITMAP hOldBitmap;
1414 /* Create the blend Mask */
1415 hOldBitmap = SelectObject(hBlendMaskDC, hBlendMaskBmp);
1416 hBlendBrush = fStyle & ILD_BLEND50 ? himl->hbrBlend50 : himl->hbrBlend25;
1417 hOldBrush = SelectObject(hBlendMaskDC, hBlendBrush);
1418 PatBlt(hBlendMaskDC, 0, 0, cx, cy, PATCOPY);
1419 SelectObject(hBlendMaskDC, hOldBrush);
1421 /* Modify the blend mask if an Image Mask exist */
1422 if(himl->hbmMask) {
1423 BitBlt(hBlendMaskDC, 0, 0, cx, cy, hMaskListDC, pt.x, pt.y, 0x220326); /* NOTSRCAND */
1424 BitBlt(hBlendMaskDC, 0, 0, cx, cy, hBlendMaskDC, 0, 0, NOTSRCCOPY);
1427 /* now apply blend to the current image given the BlendMask */
1428 if (clrBlend == CLR_DEFAULT) clrBlend = GetSysColor (COLOR_HIGHLIGHT);
1429 else if (clrBlend == CLR_NONE) clrBlend = GetTextColor (pimldp->hdcDst);
1430 hOldBrush = SelectObject (hImageDC, CreateSolidBrush(clrBlend));
1431 BitBlt (hImageDC, 0, 0, cx, cy, hBlendMaskDC, 0, 0, 0xB8074A); /* PSDPxax */
1432 DeleteObject(SelectObject(hImageDC, hOldBrush));
1433 SelectObject(hBlendMaskDC, hOldBitmap);
1436 /* Now do the overlay image, if any */
1437 nOvlIdx = (pimldp->fStyle & ILD_OVERLAYMASK) >> 8;
1438 if ( (nOvlIdx >= 1) && (nOvlIdx <= MAX_OVERLAYIMAGE)) {
1439 nOvlIdx = himl->nOvlIdx[nOvlIdx - 1];
1440 if ((nOvlIdx >= 0) && (nOvlIdx < himl->cCurImage)) {
1441 POINT ptOvl;
1442 imagelist_point_from_index( himl, nOvlIdx, &ptOvl );
1443 ptOvl.x += pimldp->xBitmap;
1444 if (himl->hbmMask && !(fStyle & ILD_IMAGE))
1445 BitBlt (hImageDC, 0, 0, cx, cy, hMaskListDC, ptOvl.x, ptOvl.y, SRCAND);
1446 BitBlt (hImageDC, 0, 0, cx, cy, hImageListDC, ptOvl.x, ptOvl.y, SRCPAINT);
1450 if (fState & ILS_SATURATE) FIXME("ILS_SATURATE: unimplemented!\n");
1451 if (fState & ILS_GLOW) FIXME("ILS_GLOW: unimplemented!\n");
1452 if (fState & ILS_SHADOW) FIXME("ILS_SHADOW: unimplemented!\n");
1454 if (fStyle & ILD_PRESERVEALPHA) FIXME("ILD_PRESERVEALPHA: unimplemented!\n");
1455 if (fStyle & ILD_SCALE) FIXME("ILD_SCALE: unimplemented!\n");
1456 if (fStyle & ILD_DPISCALE) FIXME("ILD_DPISCALE: unimplemented!\n");
1458 /* now copy the image to the screen */
1459 dwRop = SRCCOPY;
1460 if (himl->hbmMask && bIsTransparent ) {
1461 COLORREF oldDstFg = SetTextColor(pimldp->hdcDst, RGB( 0, 0, 0 ) );
1462 COLORREF oldDstBk = SetBkColor(pimldp->hdcDst, RGB( 0xff, 0xff, 0xff ));
1463 BitBlt (pimldp->hdcDst, pimldp->x, pimldp->y, cx, cy, hMaskListDC, pt.x, pt.y, SRCAND);
1464 SetBkColor(pimldp->hdcDst, oldDstBk);
1465 SetTextColor(pimldp->hdcDst, oldDstFg);
1466 dwRop = SRCPAINT;
1468 if (fStyle & ILD_ROP) dwRop = pimldp->dwRop;
1469 BitBlt (pimldp->hdcDst, pimldp->x, pimldp->y, cx, cy, hImageDC, 0, 0, dwRop);
1471 bResult = TRUE;
1472 end:
1473 /* cleanup the mess */
1474 SetBkColor(hImageDC, oldImageBk);
1475 SetTextColor(hImageDC, oldImageFg);
1476 SelectObject(hImageDC, hOldImageBmp);
1477 cleanup:
1478 DeleteObject(hBlendMaskBmp);
1479 DeleteObject(hImageBmp);
1480 DeleteDC(hImageDC);
1482 return bResult;
1486 /*************************************************************************
1487 * ImageList_Duplicate [COMCTL32.@]
1489 * Duplicates an image list.
1491 * PARAMS
1492 * himlSrc [I] source image list handle
1494 * RETURNS
1495 * Success: Handle of duplicated image list.
1496 * Failure: NULL
1499 HIMAGELIST WINAPI
1500 ImageList_Duplicate (HIMAGELIST himlSrc)
1502 HIMAGELIST himlDst;
1504 if (!is_valid(himlSrc)) {
1505 ERR("Invalid image list handle!\n");
1506 return NULL;
1509 himlDst = ImageList_Create (himlSrc->cx, himlSrc->cy, himlSrc->flags,
1510 himlSrc->cCurImage, himlSrc->cGrow);
1512 if (himlDst)
1514 SIZE sz;
1516 imagelist_get_bitmap_size(himlSrc, himlSrc->cCurImage, &sz);
1517 BitBlt (himlDst->hdcImage, 0, 0, sz.cx, sz.cy,
1518 himlSrc->hdcImage, 0, 0, SRCCOPY);
1520 if (himlDst->hbmMask)
1521 BitBlt (himlDst->hdcMask, 0, 0, sz.cx, sz.cy,
1522 himlSrc->hdcMask, 0, 0, SRCCOPY);
1524 himlDst->cCurImage = himlSrc->cCurImage;
1525 if (himlSrc->has_alpha && himlDst->has_alpha)
1526 memcpy( himlDst->has_alpha, himlSrc->has_alpha, himlDst->cCurImage );
1528 return himlDst;
1532 /*************************************************************************
1533 * ImageList_EndDrag [COMCTL32.@]
1535 * Finishes a drag operation.
1537 * PARAMS
1538 * no Parameters
1540 * RETURNS
1541 * Success: TRUE
1542 * Failure: FALSE
1545 VOID WINAPI
1546 ImageList_EndDrag (void)
1548 /* cleanup the InternalDrag struct */
1549 InternalDrag.hwnd = 0;
1550 ImageList_Destroy (InternalDrag.himl);
1551 InternalDrag.himl = 0;
1552 InternalDrag.x= 0;
1553 InternalDrag.y= 0;
1554 InternalDrag.dxHotspot = 0;
1555 InternalDrag.dyHotspot = 0;
1556 InternalDrag.bShow = FALSE;
1557 DeleteObject(InternalDrag.hbmBg);
1558 InternalDrag.hbmBg = 0;
1562 /*************************************************************************
1563 * ImageList_GetBkColor [COMCTL32.@]
1565 * Returns the background color of an image list.
1567 * PARAMS
1568 * himl [I] Image list handle.
1570 * RETURNS
1571 * Success: background color
1572 * Failure: CLR_NONE
1575 COLORREF WINAPI
1576 ImageList_GetBkColor (HIMAGELIST himl)
1578 return himl ? himl->clrBk : CLR_NONE;
1582 /*************************************************************************
1583 * ImageList_GetDragImage [COMCTL32.@]
1585 * Returns the handle to the internal drag image list.
1587 * PARAMS
1588 * ppt [O] Pointer to the drag position. Can be NULL.
1589 * pptHotspot [O] Pointer to the position of the hot spot. Can be NULL.
1591 * RETURNS
1592 * Success: Handle of the drag image list.
1593 * Failure: NULL.
1596 HIMAGELIST WINAPI
1597 ImageList_GetDragImage (POINT *ppt, POINT *pptHotspot)
1599 if (is_valid(InternalDrag.himl)) {
1600 if (ppt) {
1601 ppt->x = InternalDrag.x;
1602 ppt->y = InternalDrag.y;
1604 if (pptHotspot) {
1605 pptHotspot->x = InternalDrag.dxHotspot;
1606 pptHotspot->y = InternalDrag.dyHotspot;
1608 return (InternalDrag.himl);
1611 return NULL;
1615 /*************************************************************************
1616 * ImageList_GetFlags [COMCTL32.@]
1618 * Gets the flags of the specified image list.
1620 * PARAMS
1621 * himl [I] Handle to image list
1623 * RETURNS
1624 * Image list flags.
1626 * BUGS
1627 * Stub.
1630 DWORD WINAPI
1631 ImageList_GetFlags(HIMAGELIST himl)
1633 TRACE("%p\n", himl);
1635 return is_valid(himl) ? himl->flags : 0;
1639 /*************************************************************************
1640 * ImageList_GetIcon [COMCTL32.@]
1642 * Creates an icon from a masked image of an image list.
1644 * PARAMS
1645 * himl [I] handle to image list
1646 * i [I] image index
1647 * flags [I] drawing style flags
1649 * RETURNS
1650 * Success: icon handle
1651 * Failure: NULL
1654 HICON WINAPI
1655 ImageList_GetIcon (HIMAGELIST himl, INT i, UINT fStyle)
1657 ICONINFO ii;
1658 HICON hIcon;
1659 HBITMAP hOldDstBitmap;
1660 HDC hdcDst;
1661 POINT pt;
1663 TRACE("%p %d %d\n", himl, i, fStyle);
1664 if (!is_valid(himl) || (i < 0) || (i >= himl->cCurImage)) return NULL;
1666 ii.fIcon = TRUE;
1667 ii.xHotspot = 0;
1668 ii.yHotspot = 0;
1670 /* create colour bitmap */
1671 hdcDst = GetDC(0);
1672 ii.hbmColor = CreateCompatibleBitmap(hdcDst, himl->cx, himl->cy);
1673 ReleaseDC(0, hdcDst);
1675 hdcDst = CreateCompatibleDC(0);
1677 imagelist_point_from_index( himl, i, &pt );
1679 /* draw mask*/
1680 ii.hbmMask = CreateBitmap (himl->cx, himl->cy, 1, 1, NULL);
1681 hOldDstBitmap = SelectObject (hdcDst, ii.hbmMask);
1682 if (himl->hbmMask) {
1683 BitBlt (hdcDst, 0, 0, himl->cx, himl->cy,
1684 himl->hdcMask, pt.x, pt.y, SRCCOPY);
1686 else
1687 PatBlt (hdcDst, 0, 0, himl->cx, himl->cy, BLACKNESS);
1689 /* draw image*/
1690 SelectObject (hdcDst, ii.hbmColor);
1691 BitBlt (hdcDst, 0, 0, himl->cx, himl->cy,
1692 himl->hdcImage, pt.x, pt.y, SRCCOPY);
1695 * CreateIconIndirect requires us to deselect the bitmaps from
1696 * the DCs before calling
1698 SelectObject(hdcDst, hOldDstBitmap);
1700 hIcon = CreateIconIndirect (&ii);
1702 DeleteObject (ii.hbmMask);
1703 DeleteObject (ii.hbmColor);
1704 DeleteDC (hdcDst);
1706 return hIcon;
1710 /*************************************************************************
1711 * ImageList_GetIconSize [COMCTL32.@]
1713 * Retrieves the size of an image in an image list.
1715 * PARAMS
1716 * himl [I] handle to image list
1717 * cx [O] pointer to the image width.
1718 * cy [O] pointer to the image height.
1720 * RETURNS
1721 * Success: TRUE
1722 * Failure: FALSE
1724 * NOTES
1725 * All images in an image list have the same size.
1728 BOOL WINAPI
1729 ImageList_GetIconSize (HIMAGELIST himl, INT *cx, INT *cy)
1731 if (!is_valid(himl) || !cx || !cy)
1732 return FALSE;
1733 if ((himl->cx <= 0) || (himl->cy <= 0))
1734 return FALSE;
1736 *cx = himl->cx;
1737 *cy = himl->cy;
1739 return TRUE;
1743 /*************************************************************************
1744 * ImageList_GetImageCount [COMCTL32.@]
1746 * Returns the number of images in an image list.
1748 * PARAMS
1749 * himl [I] handle to image list
1751 * RETURNS
1752 * Success: Number of images.
1753 * Failure: 0
1756 INT WINAPI
1757 ImageList_GetImageCount (HIMAGELIST himl)
1759 if (!is_valid(himl))
1760 return 0;
1762 return himl->cCurImage;
1766 /*************************************************************************
1767 * ImageList_GetImageInfo [COMCTL32.@]
1769 * Returns information about an image in an image list.
1771 * PARAMS
1772 * himl [I] handle to image list
1773 * i [I] image index
1774 * pImageInfo [O] pointer to the image information
1776 * RETURNS
1777 * Success: TRUE
1778 * Failure: FALSE
1781 BOOL WINAPI
1782 ImageList_GetImageInfo (HIMAGELIST himl, INT i, IMAGEINFO *pImageInfo)
1784 POINT pt;
1786 if (!is_valid(himl) || (pImageInfo == NULL))
1787 return FALSE;
1788 if ((i < 0) || (i >= himl->cCurImage))
1789 return FALSE;
1791 pImageInfo->hbmImage = himl->hbmImage;
1792 pImageInfo->hbmMask = himl->hbmMask;
1794 imagelist_point_from_index( himl, i, &pt );
1795 pImageInfo->rcImage.top = pt.y;
1796 pImageInfo->rcImage.bottom = pt.y + himl->cy;
1797 pImageInfo->rcImage.left = pt.x;
1798 pImageInfo->rcImage.right = pt.x + himl->cx;
1800 return TRUE;
1804 /*************************************************************************
1805 * ImageList_GetImageRect [COMCTL32.@]
1807 * Retrieves the rectangle of the specified image in an image list.
1809 * PARAMS
1810 * himl [I] handle to image list
1811 * i [I] image index
1812 * lpRect [O] pointer to the image rectangle
1814 * RETURNS
1815 * Success: TRUE
1816 * Failure: FALSE
1818 * NOTES
1819 * This is an UNDOCUMENTED function!!!
1822 BOOL WINAPI
1823 ImageList_GetImageRect (HIMAGELIST himl, INT i, LPRECT lpRect)
1825 POINT pt;
1827 if (!is_valid(himl) || (lpRect == NULL))
1828 return FALSE;
1829 if ((i < 0) || (i >= himl->cCurImage))
1830 return FALSE;
1832 imagelist_point_from_index( himl, i, &pt );
1833 lpRect->left = pt.x;
1834 lpRect->top = pt.y;
1835 lpRect->right = pt.x + himl->cx;
1836 lpRect->bottom = pt.y + himl->cy;
1838 return TRUE;
1842 /*************************************************************************
1843 * ImageList_LoadImage [COMCTL32.@]
1844 * ImageList_LoadImageA [COMCTL32.@]
1846 * Creates an image list from a bitmap, icon or cursor.
1848 * See ImageList_LoadImageW.
1851 HIMAGELIST WINAPI
1852 ImageList_LoadImageA (HINSTANCE hi, LPCSTR lpbmp, INT cx, INT cGrow,
1853 COLORREF clrMask, UINT uType, UINT uFlags)
1855 HIMAGELIST himl;
1856 LPWSTR lpbmpW;
1857 DWORD len;
1859 if (IS_INTRESOURCE(lpbmp))
1860 return ImageList_LoadImageW(hi, (LPCWSTR)lpbmp, cx, cGrow, clrMask,
1861 uType, uFlags);
1863 len = MultiByteToWideChar(CP_ACP, 0, lpbmp, -1, NULL, 0);
1864 lpbmpW = Alloc(len * sizeof(WCHAR));
1865 MultiByteToWideChar(CP_ACP, 0, lpbmp, -1, lpbmpW, len);
1867 himl = ImageList_LoadImageW(hi, lpbmpW, cx, cGrow, clrMask, uType, uFlags);
1868 Free (lpbmpW);
1869 return himl;
1873 /*************************************************************************
1874 * ImageList_LoadImageW [COMCTL32.@]
1876 * Creates an image list from a bitmap, icon or cursor.
1878 * PARAMS
1879 * hi [I] instance handle
1880 * lpbmp [I] name or id of the image
1881 * cx [I] width of each image
1882 * cGrow [I] number of images to expand
1883 * clrMask [I] mask color
1884 * uType [I] type of image to load
1885 * uFlags [I] loading flags
1887 * RETURNS
1888 * Success: handle to the loaded image list
1889 * Failure: NULL
1891 * SEE
1892 * LoadImage ()
1895 HIMAGELIST WINAPI
1896 ImageList_LoadImageW (HINSTANCE hi, LPCWSTR lpbmp, INT cx, INT cGrow,
1897 COLORREF clrMask, UINT uType, UINT uFlags)
1899 HIMAGELIST himl = NULL;
1900 HANDLE handle;
1901 INT nImageCount;
1903 handle = LoadImageW (hi, lpbmp, uType, 0, 0, uFlags);
1904 if (!handle) {
1905 WARN("Couldn't load image\n");
1906 return NULL;
1909 if (uType == IMAGE_BITMAP) {
1910 DIBSECTION dib;
1911 UINT color;
1913 if (GetObjectW (handle, sizeof(dib), &dib) == sizeof(BITMAP)) color = ILC_COLOR;
1914 else color = dib.dsBm.bmBitsPixel;
1916 /* To match windows behavior, if cx is set to zero and
1917 the flag DI_DEFAULTSIZE is specified, cx becomes the
1918 system metric value for icons. If the flag is not specified
1919 the function sets the size to the height of the bitmap */
1920 if (cx == 0)
1922 if (uFlags & DI_DEFAULTSIZE)
1923 cx = GetSystemMetrics (SM_CXICON);
1924 else
1925 cx = dib.dsBm.bmHeight;
1928 nImageCount = dib.dsBm.bmWidth / cx;
1930 himl = ImageList_Create (cx, dib.dsBm.bmHeight, ILC_MASK | color, nImageCount, cGrow);
1931 if (!himl) {
1932 DeleteObject (handle);
1933 return NULL;
1935 ImageList_AddMasked (himl, handle, clrMask);
1937 else if ((uType == IMAGE_ICON) || (uType == IMAGE_CURSOR)) {
1938 ICONINFO ii;
1939 BITMAP bmp;
1941 GetIconInfo (handle, &ii);
1942 GetObjectW (ii.hbmColor, sizeof(BITMAP), &bmp);
1943 himl = ImageList_Create (bmp.bmWidth, bmp.bmHeight,
1944 ILC_MASK | ILC_COLOR, 1, cGrow);
1945 if (!himl) {
1946 DeleteObject (ii.hbmColor);
1947 DeleteObject (ii.hbmMask);
1948 DeleteObject (handle);
1949 return NULL;
1951 ImageList_Add (himl, ii.hbmColor, ii.hbmMask);
1952 DeleteObject (ii.hbmColor);
1953 DeleteObject (ii.hbmMask);
1956 DeleteObject (handle);
1958 return himl;
1962 /*************************************************************************
1963 * ImageList_Merge [COMCTL32.@]
1965 * Create an image list containing a merged image from two image lists.
1967 * PARAMS
1968 * himl1 [I] handle to first image list
1969 * i1 [I] first image index
1970 * himl2 [I] handle to second image list
1971 * i2 [I] second image index
1972 * dx [I] X offset of the second image relative to the first.
1973 * dy [I] Y offset of the second image relative to the first.
1975 * RETURNS
1976 * Success: The newly created image list. It contains a single image
1977 * consisting of the second image merged with the first.
1978 * Failure: NULL, if either himl1 or himl2 are invalid.
1980 * NOTES
1981 * - The returned image list should be deleted by the caller using
1982 * ImageList_Destroy() when it is no longer required.
1983 * - If either i1 or i2 are not valid image indices they will be treated
1984 * as a blank image.
1986 HIMAGELIST WINAPI
1987 ImageList_Merge (HIMAGELIST himl1, INT i1, HIMAGELIST himl2, INT i2,
1988 INT dx, INT dy)
1990 HIMAGELIST himlDst = NULL;
1991 INT cxDst, cyDst;
1992 INT xOff1, yOff1, xOff2, yOff2;
1993 POINT pt1, pt2;
1995 TRACE("(himl1=%p i1=%d himl2=%p i2=%d dx=%d dy=%d)\n", himl1, i1, himl2,
1996 i2, dx, dy);
1998 if (!is_valid(himl1) || !is_valid(himl2))
1999 return NULL;
2001 if (dx > 0) {
2002 cxDst = max (himl1->cx, dx + himl2->cx);
2003 xOff1 = 0;
2004 xOff2 = dx;
2006 else if (dx < 0) {
2007 cxDst = max (himl2->cx, himl1->cx - dx);
2008 xOff1 = -dx;
2009 xOff2 = 0;
2011 else {
2012 cxDst = max (himl1->cx, himl2->cx);
2013 xOff1 = 0;
2014 xOff2 = 0;
2017 if (dy > 0) {
2018 cyDst = max (himl1->cy, dy + himl2->cy);
2019 yOff1 = 0;
2020 yOff2 = dy;
2022 else if (dy < 0) {
2023 cyDst = max (himl2->cy, himl1->cy - dy);
2024 yOff1 = -dy;
2025 yOff2 = 0;
2027 else {
2028 cyDst = max (himl1->cy, himl2->cy);
2029 yOff1 = 0;
2030 yOff2 = 0;
2033 himlDst = ImageList_Create (cxDst, cyDst, ILC_MASK | ILC_COLOR, 1, 1);
2035 if (himlDst)
2037 imagelist_point_from_index( himl1, i1, &pt1 );
2038 imagelist_point_from_index( himl2, i2, &pt2 );
2040 /* copy image */
2041 BitBlt (himlDst->hdcImage, 0, 0, cxDst, cyDst, himl1->hdcImage, 0, 0, BLACKNESS);
2042 if (i1 >= 0 && i1 < himl1->cCurImage)
2043 BitBlt (himlDst->hdcImage, xOff1, yOff1, himl1->cx, himl1->cy, himl1->hdcImage, pt1.x, pt1.y, SRCCOPY);
2044 if (i2 >= 0 && i2 < himl2->cCurImage)
2046 BitBlt (himlDst->hdcImage, xOff2, yOff2, himl2->cx, himl2->cy, himl2->hdcMask , pt2.x, pt2.y, SRCAND);
2047 BitBlt (himlDst->hdcImage, xOff2, yOff2, himl2->cx, himl2->cy, himl2->hdcImage, pt2.x, pt2.y, SRCPAINT);
2050 /* copy mask */
2051 BitBlt (himlDst->hdcMask, 0, 0, cxDst, cyDst, himl1->hdcMask, 0, 0, WHITENESS);
2052 if (i1 >= 0 && i1 < himl1->cCurImage)
2053 BitBlt (himlDst->hdcMask, xOff1, yOff1, himl1->cx, himl1->cy, himl1->hdcMask, pt1.x, pt1.y, SRCCOPY);
2054 if (i2 >= 0 && i2 < himl2->cCurImage)
2055 BitBlt (himlDst->hdcMask, xOff2, yOff2, himl2->cx, himl2->cy, himl2->hdcMask, pt2.x, pt2.y, SRCAND);
2057 himlDst->cCurImage = 1;
2060 return himlDst;
2064 /***********************************************************************
2065 * DIB_GetDIBImageBytes
2067 * Return the number of bytes used to hold the image in a DIB bitmap.
2069 static int DIB_GetDIBImageBytes( int width, int height, int depth )
2071 return (((width * depth + 31) / 8) & ~3) * abs( height );
2075 /* helper for ImageList_Read, see comments below */
2076 static BOOL _read_bitmap(HDC hdcIml, LPSTREAM pstm)
2078 BITMAPFILEHEADER bmfh;
2079 int bitsperpixel, palspace;
2080 char bmi_buf[sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * 256];
2081 LPBITMAPINFO bmi = (LPBITMAPINFO)bmi_buf;
2082 int result = FALSE;
2083 LPBYTE bits = NULL;
2085 if (FAILED(IStream_Read ( pstm, &bmfh, sizeof(bmfh), NULL)))
2086 return FALSE;
2088 if (bmfh.bfType != (('M'<<8)|'B'))
2089 return FALSE;
2091 if (FAILED(IStream_Read ( pstm, &bmi->bmiHeader, sizeof(bmi->bmiHeader), NULL)))
2092 return FALSE;
2094 if ((bmi->bmiHeader.biSize != sizeof(bmi->bmiHeader)))
2095 return FALSE;
2097 TRACE("width %u, height %u, planes %u, bpp %u\n",
2098 bmi->bmiHeader.biWidth, bmi->bmiHeader.biHeight,
2099 bmi->bmiHeader.biPlanes, bmi->bmiHeader.biBitCount);
2101 bitsperpixel = bmi->bmiHeader.biPlanes * bmi->bmiHeader.biBitCount;
2102 if (bitsperpixel<=8)
2103 palspace = (1<<bitsperpixel)*sizeof(RGBQUAD);
2104 else
2105 palspace = 0;
2107 bmi->bmiHeader.biSizeImage = DIB_GetDIBImageBytes(bmi->bmiHeader.biWidth, bmi->bmiHeader.biHeight, bitsperpixel);
2109 /* read the palette right after the end of the bitmapinfoheader */
2110 if (palspace && FAILED(IStream_Read(pstm, bmi->bmiColors, palspace, NULL)))
2111 goto error;
2113 bits = Alloc(bmi->bmiHeader.biSizeImage);
2114 if (!bits)
2115 goto error;
2116 if (FAILED(IStream_Read(pstm, bits, bmi->bmiHeader.biSizeImage, NULL)))
2117 goto error;
2119 if (!StretchDIBits(hdcIml, 0, 0, bmi->bmiHeader.biWidth, bmi->bmiHeader.biHeight,
2120 0, 0, bmi->bmiHeader.biWidth, bmi->bmiHeader.biHeight,
2121 bits, bmi, DIB_RGB_COLORS, SRCCOPY))
2122 goto error;
2123 result = TRUE;
2125 error:
2126 Free(bits);
2127 return result;
2130 /*************************************************************************
2131 * ImageList_Read [COMCTL32.@]
2133 * Reads an image list from a stream.
2135 * PARAMS
2136 * pstm [I] pointer to a stream
2138 * RETURNS
2139 * Success: handle to image list
2140 * Failure: NULL
2142 * The format is like this:
2143 * ILHEAD ilheadstruct;
2145 * for the color image part:
2146 * BITMAPFILEHEADER bmfh;
2147 * BITMAPINFOHEADER bmih;
2148 * only if it has a palette:
2149 * RGBQUAD rgbs[nr_of_paletted_colors];
2151 * BYTE colorbits[imagesize];
2153 * the following only if the ILC_MASK bit is set in ILHEAD.ilFlags:
2154 * BITMAPFILEHEADER bmfh_mask;
2155 * BITMAPINFOHEADER bmih_mask;
2156 * only if it has a palette (it usually does not):
2157 * RGBQUAD rgbs[nr_of_paletted_colors];
2159 * BYTE maskbits[imagesize];
2161 HIMAGELIST WINAPI ImageList_Read (LPSTREAM pstm)
2163 ILHEAD ilHead;
2164 HIMAGELIST himl;
2165 int i;
2167 TRACE("%p\n", pstm);
2169 if (FAILED(IStream_Read (pstm, &ilHead, sizeof(ILHEAD), NULL)))
2170 return NULL;
2171 if (ilHead.usMagic != (('L' << 8) | 'I'))
2172 return NULL;
2173 if (ilHead.usVersion != 0x101) /* probably version? */
2174 return NULL;
2176 TRACE("cx %u, cy %u, flags 0x%04x, cCurImage %u, cMaxImage %u\n",
2177 ilHead.cx, ilHead.cy, ilHead.flags, ilHead.cCurImage, ilHead.cMaxImage);
2179 himl = ImageList_Create(ilHead.cx, ilHead.cy, ilHead.flags, ilHead.cCurImage, ilHead.cMaxImage);
2180 if (!himl)
2181 return NULL;
2183 if (!_read_bitmap(himl->hdcImage, pstm))
2185 WARN("failed to read bitmap from stream\n");
2186 return NULL;
2188 if (ilHead.flags & ILC_MASK)
2190 if (!_read_bitmap(himl->hdcMask, pstm))
2192 WARN("failed to read mask bitmap from stream\n");
2193 return NULL;
2197 himl->cCurImage = ilHead.cCurImage;
2198 himl->cMaxImage = ilHead.cMaxImage;
2200 ImageList_SetBkColor(himl,ilHead.bkcolor);
2201 for (i=0;i<4;i++)
2202 ImageList_SetOverlayImage(himl,ilHead.ovls[i],i+1);
2203 return himl;
2207 /*************************************************************************
2208 * ImageList_Remove [COMCTL32.@]
2210 * Removes an image from an image list
2212 * PARAMS
2213 * himl [I] image list handle
2214 * i [I] image index
2216 * RETURNS
2217 * Success: TRUE
2218 * Failure: FALSE
2220 * FIXME: as the image list storage test shows, native comctl32 simply shifts
2221 * images without creating a new bitmap.
2223 BOOL WINAPI
2224 ImageList_Remove (HIMAGELIST himl, INT i)
2226 HBITMAP hbmNewImage, hbmNewMask;
2227 HDC hdcBmp;
2228 SIZE sz;
2230 TRACE("(himl=%p i=%d)\n", himl, i);
2232 if (!is_valid(himl)) {
2233 ERR("Invalid image list handle!\n");
2234 return FALSE;
2237 if ((i < -1) || (i >= himl->cCurImage)) {
2238 TRACE("index out of range! %d\n", i);
2239 return FALSE;
2242 if (i == -1) {
2243 INT nCount;
2245 /* remove all */
2246 if (himl->cCurImage == 0) {
2247 /* remove all on empty ImageList is allowed */
2248 TRACE("remove all on empty ImageList!\n");
2249 return TRUE;
2252 himl->cMaxImage = himl->cInitial + himl->cGrow - 1;
2253 himl->cCurImage = 0;
2254 for (nCount = 0; nCount < MAX_OVERLAYIMAGE; nCount++)
2255 himl->nOvlIdx[nCount] = -1;
2257 hbmNewImage = ImageList_CreateImage(himl->hdcImage, himl, himl->cMaxImage);
2258 SelectObject (himl->hdcImage, hbmNewImage);
2259 DeleteObject (himl->hbmImage);
2260 himl->hbmImage = hbmNewImage;
2262 if (himl->hbmMask) {
2264 imagelist_get_bitmap_size(himl, himl->cMaxImage, &sz);
2265 hbmNewMask = CreateBitmap (sz.cx, sz.cy, 1, 1, NULL);
2266 SelectObject (himl->hdcMask, hbmNewMask);
2267 DeleteObject (himl->hbmMask);
2268 himl->hbmMask = hbmNewMask;
2271 else {
2272 /* delete one image */
2273 TRACE("Remove single image! %d\n", i);
2275 /* create new bitmap(s) */
2276 TRACE(" - Number of images: %d / %d (Old/New)\n",
2277 himl->cCurImage, himl->cCurImage - 1);
2279 hbmNewImage = ImageList_CreateImage(himl->hdcImage, himl, himl->cMaxImage);
2281 imagelist_get_bitmap_size(himl, himl->cMaxImage, &sz );
2282 if (himl->hbmMask)
2283 hbmNewMask = CreateBitmap (sz.cx, sz.cy, 1, 1, NULL);
2284 else
2285 hbmNewMask = 0; /* Just to keep compiler happy! */
2287 hdcBmp = CreateCompatibleDC (0);
2289 /* copy all images and masks prior to the "removed" image */
2290 if (i > 0) {
2291 TRACE("Pre image copy: Copy %d images\n", i);
2293 SelectObject (hdcBmp, hbmNewImage);
2294 imagelist_copy_images( himl, himl->hdcImage, hdcBmp, 0, i, 0 );
2296 if (himl->hbmMask) {
2297 SelectObject (hdcBmp, hbmNewMask);
2298 imagelist_copy_images( himl, himl->hdcMask, hdcBmp, 0, i, 0 );
2302 /* copy all images and masks behind the removed image */
2303 if (i < himl->cCurImage - 1) {
2304 TRACE("Post image copy!\n");
2306 SelectObject (hdcBmp, hbmNewImage);
2307 imagelist_copy_images( himl, himl->hdcImage, hdcBmp, i + 1,
2308 (himl->cCurImage - i), i );
2310 if (himl->hbmMask) {
2311 SelectObject (hdcBmp, hbmNewMask);
2312 imagelist_copy_images( himl, himl->hdcMask, hdcBmp, i + 1,
2313 (himl->cCurImage - i), i );
2317 DeleteDC (hdcBmp);
2319 /* delete old images and insert new ones */
2320 SelectObject (himl->hdcImage, hbmNewImage);
2321 DeleteObject (himl->hbmImage);
2322 himl->hbmImage = hbmNewImage;
2323 if (himl->hbmMask) {
2324 SelectObject (himl->hdcMask, hbmNewMask);
2325 DeleteObject (himl->hbmMask);
2326 himl->hbmMask = hbmNewMask;
2329 himl->cCurImage--;
2332 return TRUE;
2336 /*************************************************************************
2337 * ImageList_Replace [COMCTL32.@]
2339 * Replaces an image in an image list with a new image.
2341 * PARAMS
2342 * himl [I] handle to image list
2343 * i [I] image index
2344 * hbmImage [I] handle to image bitmap
2345 * hbmMask [I] handle to mask bitmap. Can be NULL.
2347 * RETURNS
2348 * Success: TRUE
2349 * Failure: FALSE
2352 BOOL WINAPI
2353 ImageList_Replace (HIMAGELIST himl, INT i, HBITMAP hbmImage,
2354 HBITMAP hbmMask)
2356 HDC hdcImage;
2357 BITMAP bmp;
2358 POINT pt;
2360 TRACE("%p %d %p %p\n", himl, i, hbmImage, hbmMask);
2362 if (!is_valid(himl)) {
2363 ERR("Invalid image list handle!\n");
2364 return FALSE;
2367 if ((i >= himl->cMaxImage) || (i < 0)) {
2368 ERR("Invalid image index!\n");
2369 return FALSE;
2372 if (!GetObjectW(hbmImage, sizeof(BITMAP), &bmp))
2373 return FALSE;
2375 hdcImage = CreateCompatibleDC (0);
2377 /* Replace Image */
2378 SelectObject (hdcImage, hbmImage);
2380 if (add_with_alpha( himl, hdcImage, i, 1, bmp.bmWidth, bmp.bmHeight, hbmImage, hbmMask ))
2381 goto done;
2383 imagelist_point_from_index(himl, i, &pt);
2384 StretchBlt (himl->hdcImage, pt.x, pt.y, himl->cx, himl->cy,
2385 hdcImage, 0, 0, bmp.bmWidth, bmp.bmHeight, SRCCOPY);
2387 if (himl->hbmMask)
2389 HDC hdcTemp;
2390 HBITMAP hOldBitmapTemp;
2392 hdcTemp = CreateCompatibleDC(0);
2393 hOldBitmapTemp = SelectObject(hdcTemp, hbmMask);
2395 StretchBlt (himl->hdcMask, pt.x, pt.y, himl->cx, himl->cy,
2396 hdcTemp, 0, 0, bmp.bmWidth, bmp.bmHeight, SRCCOPY);
2397 SelectObject(hdcTemp, hOldBitmapTemp);
2398 DeleteDC(hdcTemp);
2400 /* Remove the background from the image
2402 BitBlt (himl->hdcImage, pt.x, pt.y, bmp.bmWidth, bmp.bmHeight,
2403 himl->hdcMask, pt.x, pt.y, 0x220326); /* NOTSRCAND */
2406 done:
2407 DeleteDC (hdcImage);
2409 return TRUE;
2413 /*************************************************************************
2414 * ImageList_ReplaceIcon [COMCTL32.@]
2416 * Replaces an image in an image list using an icon.
2418 * PARAMS
2419 * himl [I] handle to image list
2420 * i [I] image index
2421 * hIcon [I] handle to icon
2423 * RETURNS
2424 * Success: index of the replaced image
2425 * Failure: -1
2428 INT WINAPI
2429 ImageList_ReplaceIcon (HIMAGELIST himl, INT nIndex, HICON hIcon)
2431 HDC hdcImage;
2432 HICON hBestFitIcon;
2433 ICONINFO ii;
2434 BITMAP bmp;
2435 BOOL ret;
2436 POINT pt;
2438 TRACE("(%p %d %p)\n", himl, nIndex, hIcon);
2440 if (!is_valid(himl)) {
2441 ERR("invalid image list\n");
2442 return -1;
2444 if ((nIndex >= himl->cMaxImage) || (nIndex < -1)) {
2445 ERR("invalid image index %d / %d\n", nIndex, himl->cMaxImage);
2446 return -1;
2449 hBestFitIcon = CopyImage(
2450 hIcon, IMAGE_ICON,
2451 himl->cx, himl->cy,
2452 LR_COPYFROMRESOURCE);
2453 /* the above will fail if the icon wasn't loaded from a resource, so try
2454 * again without LR_COPYFROMRESOURCE flag */
2455 if (!hBestFitIcon)
2456 hBestFitIcon = CopyImage(
2457 hIcon, IMAGE_ICON,
2458 himl->cx, himl->cy,
2460 if (!hBestFitIcon)
2461 return -1;
2463 ret = GetIconInfo (hBestFitIcon, &ii);
2464 if (!ret) {
2465 DestroyIcon(hBestFitIcon);
2466 return -1;
2469 ret = GetObjectW (ii.hbmMask, sizeof(BITMAP), &bmp);
2470 if (!ret) {
2471 ERR("couldn't get mask bitmap info\n");
2472 if (ii.hbmColor)
2473 DeleteObject (ii.hbmColor);
2474 if (ii.hbmMask)
2475 DeleteObject (ii.hbmMask);
2476 DestroyIcon(hBestFitIcon);
2477 return -1;
2480 if (nIndex == -1) {
2481 if (himl->cCurImage + 1 > himl->cMaxImage)
2482 IMAGELIST_InternalExpandBitmaps(himl, 1);
2484 nIndex = himl->cCurImage;
2485 himl->cCurImage++;
2488 hdcImage = CreateCompatibleDC (0);
2489 TRACE("hdcImage=%p\n", hdcImage);
2490 if (hdcImage == 0)
2491 ERR("invalid hdcImage!\n");
2493 if (himl->has_alpha)
2495 if (!ii.hbmColor)
2497 UINT height = bmp.bmHeight / 2;
2498 HDC hdcMask = CreateCompatibleDC( 0 );
2499 HBITMAP color = CreateBitmap( bmp.bmWidth, height, 1, 1, NULL );
2500 SelectObject( hdcImage, color );
2501 SelectObject( hdcMask, ii.hbmMask );
2502 BitBlt( hdcImage, 0, 0, bmp.bmWidth, height, hdcMask, 0, height, SRCCOPY );
2503 ret = add_with_alpha( himl, hdcImage, nIndex, 1, bmp.bmWidth, height, color, ii.hbmMask );
2504 DeleteDC( hdcMask );
2505 DeleteObject( color );
2506 if (ret) goto done;
2508 else if (add_with_alpha( himl, hdcImage, nIndex, 1, bmp.bmWidth, bmp.bmHeight,
2509 ii.hbmColor, ii.hbmMask )) goto done;
2512 imagelist_point_from_index(himl, nIndex, &pt);
2514 SetTextColor(himl->hdcImage, RGB(0,0,0));
2515 SetBkColor (himl->hdcImage, RGB(255,255,255));
2517 if (ii.hbmColor)
2519 SelectObject (hdcImage, ii.hbmColor);
2520 StretchBlt (himl->hdcImage, pt.x, pt.y, himl->cx, himl->cy,
2521 hdcImage, 0, 0, bmp.bmWidth, bmp.bmHeight, SRCCOPY);
2522 if (himl->hbmMask)
2524 SelectObject (hdcImage, ii.hbmMask);
2525 StretchBlt (himl->hdcMask, pt.x, pt.y, himl->cx, himl->cy,
2526 hdcImage, 0, 0, bmp.bmWidth, bmp.bmHeight, SRCCOPY);
2529 else
2531 UINT height = bmp.bmHeight / 2;
2532 SelectObject (hdcImage, ii.hbmMask);
2533 StretchBlt (himl->hdcImage, pt.x, pt.y, himl->cx, himl->cy,
2534 hdcImage, 0, height, bmp.bmWidth, height, SRCCOPY);
2535 if (himl->hbmMask)
2536 StretchBlt (himl->hdcMask, pt.x, pt.y, himl->cx, himl->cy,
2537 hdcImage, 0, 0, bmp.bmWidth, height, SRCCOPY);
2540 done:
2541 DestroyIcon(hBestFitIcon);
2542 if (hdcImage)
2543 DeleteDC (hdcImage);
2544 if (ii.hbmColor)
2545 DeleteObject (ii.hbmColor);
2546 if (ii.hbmMask)
2547 DeleteObject (ii.hbmMask);
2549 TRACE("Insert index = %d, himl->cCurImage = %d\n", nIndex, himl->cCurImage);
2550 return nIndex;
2554 /*************************************************************************
2555 * ImageList_SetBkColor [COMCTL32.@]
2557 * Sets the background color of an image list.
2559 * PARAMS
2560 * himl [I] handle to image list
2561 * clrBk [I] background color
2563 * RETURNS
2564 * Success: previous background color
2565 * Failure: CLR_NONE
2568 COLORREF WINAPI
2569 ImageList_SetBkColor (HIMAGELIST himl, COLORREF clrBk)
2571 COLORREF clrOldBk;
2573 if (!is_valid(himl))
2574 return CLR_NONE;
2576 clrOldBk = himl->clrBk;
2577 himl->clrBk = clrBk;
2578 return clrOldBk;
2582 /*************************************************************************
2583 * ImageList_SetDragCursorImage [COMCTL32.@]
2585 * Combines the specified image with the current drag image
2587 * PARAMS
2588 * himlDrag [I] handle to drag image list
2589 * iDrag [I] drag image index
2590 * dxHotspot [I] X position of the hot spot
2591 * dyHotspot [I] Y position of the hot spot
2593 * RETURNS
2594 * Success: TRUE
2595 * Failure: FALSE
2597 * NOTES
2598 * - The names dxHotspot, dyHotspot are misleading because they have nothing
2599 * to do with a hotspot but are only the offset of the origin of the new
2600 * image relative to the origin of the old image.
2602 * - When this function is called and the drag image is visible, a
2603 * short flickering occurs but this matches the Win9x behavior. It is
2604 * possible to fix the flickering using code like in ImageList_DragMove.
2607 BOOL WINAPI
2608 ImageList_SetDragCursorImage (HIMAGELIST himlDrag, INT iDrag,
2609 INT dxHotspot, INT dyHotspot)
2611 HIMAGELIST himlTemp;
2612 BOOL visible;
2614 if (!is_valid(InternalDrag.himl) || !is_valid(himlDrag))
2615 return FALSE;
2617 TRACE(" dxH=%d dyH=%d nX=%d nY=%d\n",
2618 dxHotspot, dyHotspot, InternalDrag.dxHotspot, InternalDrag.dyHotspot);
2620 visible = InternalDrag.bShow;
2622 himlTemp = ImageList_Merge (InternalDrag.himl, 0, himlDrag, iDrag,
2623 dxHotspot, dyHotspot);
2625 if (visible) {
2626 /* hide the drag image */
2627 ImageList_DragShowNolock(FALSE);
2629 if ((InternalDrag.himl->cx != himlTemp->cx) ||
2630 (InternalDrag.himl->cy != himlTemp->cy)) {
2631 /* the size of the drag image changed, invalidate the buffer */
2632 DeleteObject(InternalDrag.hbmBg);
2633 InternalDrag.hbmBg = 0;
2636 ImageList_Destroy (InternalDrag.himl);
2637 InternalDrag.himl = himlTemp;
2639 if (visible) {
2640 /* show the drag image */
2641 ImageList_DragShowNolock(TRUE);
2644 return TRUE;
2648 /*************************************************************************
2649 * ImageList_SetFilter [COMCTL32.@]
2651 * Sets a filter (or does something completely different)!!???
2652 * It removes 12 Bytes from the stack (3 Parameters).
2654 * PARAMS
2655 * himl [I] SHOULD be a handle to image list
2656 * i [I] COULD be an index?
2657 * dwFilter [I] ???
2659 * RETURNS
2660 * Success: TRUE ???
2661 * Failure: FALSE ???
2663 * BUGS
2664 * This is an UNDOCUMENTED function!!!!
2665 * empty stub.
2668 BOOL WINAPI
2669 ImageList_SetFilter (HIMAGELIST himl, INT i, DWORD dwFilter)
2671 FIXME("(%p 0x%x 0x%x):empty stub!\n", himl, i, dwFilter);
2673 return FALSE;
2677 /*************************************************************************
2678 * ImageList_SetFlags [COMCTL32.@]
2680 * Sets the image list flags.
2682 * PARAMS
2683 * himl [I] Handle to image list
2684 * flags [I] Flags to set
2686 * RETURNS
2687 * Old flags?
2689 * BUGS
2690 * Stub.
2693 DWORD WINAPI
2694 ImageList_SetFlags(HIMAGELIST himl, DWORD flags)
2696 FIXME("(%p %08x):empty stub\n", himl, flags);
2697 return 0;
2701 /*************************************************************************
2702 * ImageList_SetIconSize [COMCTL32.@]
2704 * Sets the image size of the bitmap and deletes all images.
2706 * PARAMS
2707 * himl [I] handle to image list
2708 * cx [I] image width
2709 * cy [I] image height
2711 * RETURNS
2712 * Success: TRUE
2713 * Failure: FALSE
2716 BOOL WINAPI
2717 ImageList_SetIconSize (HIMAGELIST himl, INT cx, INT cy)
2719 INT nCount;
2720 HBITMAP hbmNew;
2722 if (!is_valid(himl))
2723 return FALSE;
2725 /* remove all images */
2726 himl->cMaxImage = himl->cInitial + 1;
2727 himl->cCurImage = 0;
2728 himl->cx = cx;
2729 himl->cy = cy;
2731 /* initialize overlay mask indices */
2732 for (nCount = 0; nCount < MAX_OVERLAYIMAGE; nCount++)
2733 himl->nOvlIdx[nCount] = -1;
2735 hbmNew = ImageList_CreateImage(himl->hdcImage, himl, himl->cMaxImage);
2736 SelectObject (himl->hdcImage, hbmNew);
2737 DeleteObject (himl->hbmImage);
2738 himl->hbmImage = hbmNew;
2740 if (himl->hbmMask) {
2741 SIZE sz;
2742 imagelist_get_bitmap_size(himl, himl->cMaxImage, &sz);
2743 hbmNew = CreateBitmap (sz.cx, sz.cy, 1, 1, NULL);
2744 SelectObject (himl->hdcMask, hbmNew);
2745 DeleteObject (himl->hbmMask);
2746 himl->hbmMask = hbmNew;
2749 return TRUE;
2753 /*************************************************************************
2754 * ImageList_SetImageCount [COMCTL32.@]
2756 * Resizes an image list to the specified number of images.
2758 * PARAMS
2759 * himl [I] handle to image list
2760 * iImageCount [I] number of images in the image list
2762 * RETURNS
2763 * Success: TRUE
2764 * Failure: FALSE
2767 BOOL WINAPI
2768 ImageList_SetImageCount (HIMAGELIST himl, UINT iImageCount)
2770 HDC hdcBitmap;
2771 HBITMAP hbmNewBitmap, hbmOld;
2772 INT nNewCount, nCopyCount;
2774 TRACE("%p %d\n",himl,iImageCount);
2776 if (!is_valid(himl))
2777 return FALSE;
2778 if (himl->cMaxImage > iImageCount)
2780 himl->cCurImage = iImageCount;
2781 /* TODO: shrink the bitmap when cMaxImage-cCurImage>cGrow ? */
2782 return TRUE;
2785 nNewCount = iImageCount + himl->cGrow;
2786 nCopyCount = min(himl->cCurImage, iImageCount);
2788 hdcBitmap = CreateCompatibleDC (0);
2790 hbmNewBitmap = ImageList_CreateImage(hdcBitmap, himl, nNewCount);
2792 if (hbmNewBitmap != 0)
2794 hbmOld = SelectObject (hdcBitmap, hbmNewBitmap);
2795 imagelist_copy_images( himl, himl->hdcImage, hdcBitmap, 0, nCopyCount, 0 );
2796 SelectObject (hdcBitmap, hbmOld);
2798 /* FIXME: delete 'empty' image space? */
2800 SelectObject (himl->hdcImage, hbmNewBitmap);
2801 DeleteObject (himl->hbmImage);
2802 himl->hbmImage = hbmNewBitmap;
2804 else
2805 ERR("Could not create new image bitmap !\n");
2807 if (himl->hbmMask)
2809 SIZE sz;
2810 imagelist_get_bitmap_size( himl, nNewCount, &sz );
2811 hbmNewBitmap = CreateBitmap (sz.cx, sz.cy, 1, 1, NULL);
2812 if (hbmNewBitmap != 0)
2814 hbmOld = SelectObject (hdcBitmap, hbmNewBitmap);
2815 imagelist_copy_images( himl, himl->hdcMask, hdcBitmap, 0, nCopyCount, 0 );
2816 SelectObject (hdcBitmap, hbmOld);
2818 /* FIXME: delete 'empty' image space? */
2820 SelectObject (himl->hdcMask, hbmNewBitmap);
2821 DeleteObject (himl->hbmMask);
2822 himl->hbmMask = hbmNewBitmap;
2824 else
2825 ERR("Could not create new mask bitmap!\n");
2828 DeleteDC (hdcBitmap);
2830 if (himl->has_alpha)
2832 char *new_alpha = HeapReAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, himl->has_alpha, nNewCount );
2833 if (new_alpha) himl->has_alpha = new_alpha;
2834 else
2836 HeapFree( GetProcessHeap(), 0, himl->has_alpha );
2837 himl->has_alpha = NULL;
2841 /* Update max image count and current image count */
2842 himl->cMaxImage = nNewCount;
2843 himl->cCurImage = iImageCount;
2845 return TRUE;
2849 /*************************************************************************
2850 * ImageList_SetOverlayImage [COMCTL32.@]
2852 * Assigns an overlay mask index to an existing image in an image list.
2854 * PARAMS
2855 * himl [I] handle to image list
2856 * iImage [I] image index
2857 * iOverlay [I] overlay mask index
2859 * RETURNS
2860 * Success: TRUE
2861 * Failure: FALSE
2864 BOOL WINAPI
2865 ImageList_SetOverlayImage (HIMAGELIST himl, INT iImage, INT iOverlay)
2867 if (!is_valid(himl))
2868 return FALSE;
2869 if ((iOverlay < 1) || (iOverlay > MAX_OVERLAYIMAGE))
2870 return FALSE;
2871 if ((iImage!=-1) && ((iImage < 0) || (iImage > himl->cCurImage)))
2872 return FALSE;
2873 himl->nOvlIdx[iOverlay - 1] = iImage;
2874 return TRUE;
2879 /* helper for ImageList_Write - write bitmap to pstm
2880 * currently everything is written as 24 bit RGB, except masks
2882 static BOOL
2883 _write_bitmap(HBITMAP hBitmap, LPSTREAM pstm)
2885 LPBITMAPFILEHEADER bmfh;
2886 LPBITMAPINFOHEADER bmih;
2887 LPBYTE data = NULL, lpBits;
2888 BITMAP bm;
2889 INT bitCount, sizeImage, offBits, totalSize;
2890 HDC xdc;
2891 BOOL result = FALSE;
2893 if (!GetObjectW(hBitmap, sizeof(BITMAP), &bm))
2894 return FALSE;
2896 bitCount = bm.bmBitsPixel == 1 ? 1 : 24;
2897 sizeImage = DIB_GetDIBImageBytes(bm.bmWidth, bm.bmHeight, bitCount);
2899 totalSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
2900 if(bitCount != 24)
2901 totalSize += (1 << bitCount) * sizeof(RGBQUAD);
2902 offBits = totalSize;
2903 totalSize += sizeImage;
2905 data = Alloc(totalSize);
2906 bmfh = (LPBITMAPFILEHEADER)data;
2907 bmih = (LPBITMAPINFOHEADER)(data + sizeof(BITMAPFILEHEADER));
2908 lpBits = data + offBits;
2910 /* setup BITMAPFILEHEADER */
2911 bmfh->bfType = (('M' << 8) | 'B');
2912 bmfh->bfSize = offBits;
2913 bmfh->bfReserved1 = 0;
2914 bmfh->bfReserved2 = 0;
2915 bmfh->bfOffBits = offBits;
2917 /* setup BITMAPINFOHEADER */
2918 bmih->biSize = sizeof(BITMAPINFOHEADER);
2919 bmih->biWidth = bm.bmWidth;
2920 bmih->biHeight = bm.bmHeight;
2921 bmih->biPlanes = 1;
2922 bmih->biBitCount = bitCount;
2923 bmih->biCompression = BI_RGB;
2924 bmih->biSizeImage = sizeImage;
2925 bmih->biXPelsPerMeter = 0;
2926 bmih->biYPelsPerMeter = 0;
2927 bmih->biClrUsed = 0;
2928 bmih->biClrImportant = 0;
2930 xdc = GetDC(0);
2931 result = GetDIBits(xdc, hBitmap, 0, bm.bmHeight, lpBits, (BITMAPINFO *)bmih, DIB_RGB_COLORS) == bm.bmHeight;
2932 ReleaseDC(0, xdc);
2933 if (!result)
2934 goto failed;
2936 TRACE("width %u, height %u, planes %u, bpp %u\n",
2937 bmih->biWidth, bmih->biHeight,
2938 bmih->biPlanes, bmih->biBitCount);
2940 if(bitCount == 1) {
2941 /* Hack. */
2942 LPBITMAPINFO inf = (LPBITMAPINFO)bmih;
2943 inf->bmiColors[0].rgbRed = inf->bmiColors[0].rgbGreen = inf->bmiColors[0].rgbBlue = 0;
2944 inf->bmiColors[1].rgbRed = inf->bmiColors[1].rgbGreen = inf->bmiColors[1].rgbBlue = 0xff;
2947 if(FAILED(IStream_Write(pstm, data, totalSize, NULL)))
2948 goto failed;
2950 result = TRUE;
2952 failed:
2953 Free(data);
2955 return result;
2959 /*************************************************************************
2960 * ImageList_Write [COMCTL32.@]
2962 * Writes an image list to a stream.
2964 * PARAMS
2965 * himl [I] handle to image list
2966 * pstm [O] Pointer to a stream.
2968 * RETURNS
2969 * Success: TRUE
2970 * Failure: FALSE
2972 * BUGS
2973 * probably.
2976 BOOL WINAPI
2977 ImageList_Write (HIMAGELIST himl, LPSTREAM pstm)
2979 ILHEAD ilHead;
2980 int i;
2982 TRACE("%p %p\n", himl, pstm);
2984 if (!is_valid(himl))
2985 return FALSE;
2987 ilHead.usMagic = (('L' << 8) | 'I');
2988 ilHead.usVersion = 0x101;
2989 ilHead.cCurImage = himl->cCurImage;
2990 ilHead.cMaxImage = himl->cMaxImage;
2991 ilHead.cGrow = himl->cGrow;
2992 ilHead.cx = himl->cx;
2993 ilHead.cy = himl->cy;
2994 ilHead.bkcolor = himl->clrBk;
2995 ilHead.flags = himl->flags;
2996 for(i = 0; i < 4; i++) {
2997 ilHead.ovls[i] = himl->nOvlIdx[i];
3000 TRACE("cx %u, cy %u, flags 0x04%x, cCurImage %u, cMaxImage %u\n",
3001 ilHead.cx, ilHead.cy, ilHead.flags, ilHead.cCurImage, ilHead.cMaxImage);
3003 if(FAILED(IStream_Write(pstm, &ilHead, sizeof(ILHEAD), NULL)))
3004 return FALSE;
3006 /* write the bitmap */
3007 if(!_write_bitmap(himl->hbmImage, pstm))
3008 return FALSE;
3010 /* write the mask if we have one */
3011 if(himl->flags & ILC_MASK) {
3012 if(!_write_bitmap(himl->hbmMask, pstm))
3013 return FALSE;
3016 return TRUE;
3020 static HBITMAP ImageList_CreateImage(HDC hdc, HIMAGELIST himl, UINT count)
3022 HBITMAP hbmNewBitmap;
3023 UINT ilc = (himl->flags & 0xFE);
3024 SIZE sz;
3026 imagelist_get_bitmap_size( himl, count, &sz );
3028 if ((ilc >= ILC_COLOR4 && ilc <= ILC_COLOR32) || ilc == ILC_COLOR)
3030 VOID* bits;
3031 BITMAPINFO *bmi;
3033 TRACE("Creating DIBSection %d x %d, %d Bits per Pixel\n",
3034 sz.cx, sz.cy, himl->uBitsPixel);
3036 if (himl->uBitsPixel <= ILC_COLOR8)
3038 LPPALETTEENTRY pal;
3039 ULONG i, colors;
3040 BYTE temp;
3042 colors = 1 << himl->uBitsPixel;
3043 bmi = Alloc(sizeof(BITMAPINFOHEADER) +
3044 sizeof(PALETTEENTRY) * colors);
3046 pal = (LPPALETTEENTRY)bmi->bmiColors;
3047 GetPaletteEntries(GetStockObject(DEFAULT_PALETTE), 0, colors, pal);
3049 /* Swap colors returned by GetPaletteEntries so we can use them for
3050 * CreateDIBSection call. */
3051 for (i = 0; i < colors; i++)
3053 temp = pal[i].peBlue;
3054 bmi->bmiColors[i].rgbRed = pal[i].peRed;
3055 bmi->bmiColors[i].rgbBlue = temp;
3058 else
3060 bmi = Alloc(sizeof(BITMAPINFOHEADER));
3063 bmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
3064 bmi->bmiHeader.biWidth = sz.cx;
3065 bmi->bmiHeader.biHeight = sz.cy;
3066 bmi->bmiHeader.biPlanes = 1;
3067 bmi->bmiHeader.biBitCount = himl->uBitsPixel;
3068 bmi->bmiHeader.biCompression = BI_RGB;
3069 bmi->bmiHeader.biSizeImage = 0;
3070 bmi->bmiHeader.biXPelsPerMeter = 0;
3071 bmi->bmiHeader.biYPelsPerMeter = 0;
3072 bmi->bmiHeader.biClrUsed = 0;
3073 bmi->bmiHeader.biClrImportant = 0;
3075 hbmNewBitmap = CreateDIBSection(hdc, bmi, DIB_RGB_COLORS, &bits, 0, 0);
3077 Free (bmi);
3079 else /*if (ilc == ILC_COLORDDB)*/
3081 TRACE("Creating Bitmap: %d Bits per Pixel\n", himl->uBitsPixel);
3083 hbmNewBitmap = CreateBitmap (sz.cx, sz.cy, 1, himl->uBitsPixel, NULL);
3085 TRACE("returning %p\n", hbmNewBitmap);
3086 return hbmNewBitmap;
3089 /*************************************************************************
3090 * ImageList_SetColorTable [COMCTL32.@]
3092 * Sets the color table of an image list.
3094 * PARAMS
3095 * himl [I] Handle to the image list.
3096 * uStartIndex [I] The first index to set.
3097 * cEntries [I] Number of entries to set.
3098 * prgb [I] New color information for color table for the image list.
3100 * RETURNS
3101 * Success: Number of entries in the table that were set.
3102 * Failure: Zero.
3104 * SEE
3105 * ImageList_Create(), SetDIBColorTable()
3108 UINT WINAPI
3109 ImageList_SetColorTable (HIMAGELIST himl, UINT uStartIndex, UINT cEntries, CONST RGBQUAD * prgb)
3111 return SetDIBColorTable(himl->hdcImage, uStartIndex, cEntries, prgb);
3114 /*************************************************************************
3115 * ImageList_CoCreateInstance [COMCTL32.@]
3117 * Creates a new imagelist instance and returns an interface pointer to it.
3119 * PARAMS
3120 * rclsid [I] A reference to the CLSID (CLSID_ImageList).
3121 * punkOuter [I] Pointer to IUnknown interface for aggregation, if desired
3122 * riid [I] Identifier of the requested interface.
3123 * ppv [O] Returns the address of the pointer requested, or NULL.
3125 * RETURNS
3126 * Success: S_OK.
3127 * Failure: Error value.
3129 HRESULT WINAPI
3130 ImageList_CoCreateInstance (REFCLSID rclsid, const IUnknown *punkOuter, REFIID riid, void **ppv)
3132 TRACE("(%s,%p,%s,%p)\n", debugstr_guid(rclsid), punkOuter, debugstr_guid(riid), ppv);
3134 if (!IsEqualCLSID(&CLSID_ImageList, rclsid))
3135 return E_NOINTERFACE;
3137 return ImageListImpl_CreateInstance(punkOuter, riid, ppv);
3141 /*************************************************************************
3142 * IImageList implementation
3145 static HRESULT WINAPI ImageListImpl_QueryInterface(IImageList *iface,
3146 REFIID iid, void **ppv)
3148 HIMAGELIST This = (HIMAGELIST) iface;
3149 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(iid), ppv);
3151 if (!ppv) return E_INVALIDARG;
3153 if (IsEqualIID(&IID_IUnknown, iid) || IsEqualIID(&IID_IImageList, iid))
3154 *ppv = This;
3155 else
3157 *ppv = NULL;
3158 return E_NOINTERFACE;
3161 IUnknown_AddRef((IUnknown*)*ppv);
3162 return S_OK;
3165 static ULONG WINAPI ImageListImpl_AddRef(IImageList *iface)
3167 HIMAGELIST This = (HIMAGELIST) iface;
3168 ULONG ref = InterlockedIncrement(&This->ref);
3170 TRACE("(%p) refcount=%u\n", iface, ref);
3171 return ref;
3174 static ULONG WINAPI ImageListImpl_Release(IImageList *iface)
3176 HIMAGELIST This = (HIMAGELIST) iface;
3177 ULONG ref = InterlockedDecrement(&This->ref);
3179 TRACE("(%p) refcount=%u\n", iface, ref);
3181 if (ref == 0)
3183 /* delete image bitmaps */
3184 if (This->hbmImage) DeleteObject (This->hbmImage);
3185 if (This->hbmMask) DeleteObject (This->hbmMask);
3187 /* delete image & mask DCs */
3188 if (This->hdcImage) DeleteDC (This->hdcImage);
3189 if (This->hdcMask) DeleteDC (This->hdcMask);
3191 /* delete blending brushes */
3192 if (This->hbrBlend25) DeleteObject (This->hbrBlend25);
3193 if (This->hbrBlend50) DeleteObject (This->hbrBlend50);
3195 This->lpVtbl = NULL;
3196 HeapFree(GetProcessHeap(), 0, This->has_alpha);
3197 HeapFree(GetProcessHeap(), 0, This);
3200 return ref;
3203 static HRESULT WINAPI ImageListImpl_Add(IImageList *iface, HBITMAP hbmImage,
3204 HBITMAP hbmMask, int *pi)
3206 HIMAGELIST This = (HIMAGELIST) iface;
3207 int ret;
3209 if (!pi)
3210 return E_FAIL;
3212 ret = ImageList_Add(This, hbmImage, hbmMask);
3214 if (ret == -1)
3215 return E_FAIL;
3217 *pi = ret;
3218 return S_OK;
3221 static HRESULT WINAPI ImageListImpl_ReplaceIcon(IImageList *iface, int i,
3222 HICON hicon, int *pi)
3224 HIMAGELIST This = (HIMAGELIST) iface;
3225 int ret;
3227 if (!pi)
3228 return E_FAIL;
3230 ret = ImageList_ReplaceIcon(This, i, hicon);
3232 if (ret == -1)
3233 return E_FAIL;
3235 *pi = ret;
3236 return S_OK;
3239 static HRESULT WINAPI ImageListImpl_SetOverlayImage(IImageList *iface,
3240 int iImage, int iOverlay)
3242 return ImageList_SetOverlayImage((HIMAGELIST) iface, iImage, iOverlay)
3243 ? S_OK : E_FAIL;
3246 static HRESULT WINAPI ImageListImpl_Replace(IImageList *iface, int i,
3247 HBITMAP hbmImage, HBITMAP hbmMask)
3249 return ImageList_Replace((HIMAGELIST) iface, i, hbmImage, hbmMask) ? S_OK :
3250 E_FAIL;
3253 static HRESULT WINAPI ImageListImpl_AddMasked(IImageList *iface, HBITMAP hbmImage,
3254 COLORREF crMask, int *pi)
3256 HIMAGELIST This = (HIMAGELIST) iface;
3257 int ret;
3259 if (!pi)
3260 return E_FAIL;
3262 ret = ImageList_AddMasked(This, hbmImage, crMask);
3264 if (ret == -1)
3265 return E_FAIL;
3267 *pi = ret;
3268 return S_OK;
3271 static HRESULT WINAPI ImageListImpl_Draw(IImageList *iface,
3272 IMAGELISTDRAWPARAMS *pimldp)
3274 HIMAGELIST This = (HIMAGELIST) iface;
3275 HIMAGELIST old_himl;
3276 int ret;
3278 /* As far as I can tell, Windows simply ignores the contents of pimldp->himl
3279 so we shall simulate the same */
3280 old_himl = pimldp->himl;
3281 pimldp->himl = This;
3283 ret = ImageList_DrawIndirect(pimldp);
3285 pimldp->himl = old_himl;
3286 return ret ? S_OK : E_INVALIDARG;
3289 static HRESULT WINAPI ImageListImpl_Remove(IImageList *iface, int i)
3291 return (ImageList_Remove((HIMAGELIST) iface, i) == 0) ? E_INVALIDARG : S_OK;
3294 static HRESULT WINAPI ImageListImpl_GetIcon(IImageList *iface, int i, UINT flags,
3295 HICON *picon)
3297 HICON hIcon;
3299 if (!picon)
3300 return E_FAIL;
3302 hIcon = ImageList_GetIcon((HIMAGELIST) iface, i, flags);
3304 if (hIcon == NULL)
3305 return E_FAIL;
3307 *picon = hIcon;
3308 return S_OK;
3311 static HRESULT WINAPI ImageListImpl_GetImageInfo(IImageList *iface, int i,
3312 IMAGEINFO *pImageInfo)
3314 return ImageList_GetImageInfo((HIMAGELIST) iface, i, pImageInfo) ? S_OK : E_FAIL;
3317 static HRESULT WINAPI ImageListImpl_Copy(IImageList *iface, int iDst,
3318 IUnknown *punkSrc, int iSrc, UINT uFlags)
3320 HIMAGELIST This = (HIMAGELIST) iface;
3321 IImageList *src = NULL;
3322 HRESULT ret;
3324 if (!punkSrc)
3325 return E_FAIL;
3327 /* TODO: Add test for IID_ImageList2 too */
3328 if (FAILED(IImageList_QueryInterface(punkSrc, &IID_IImageList,
3329 (void **) &src)))
3330 return E_FAIL;
3332 if (ImageList_Copy(This, iDst, (HIMAGELIST) src, iSrc, uFlags))
3333 ret = S_OK;
3334 else
3335 ret = E_FAIL;
3337 IImageList_Release(src);
3338 return ret;
3341 static HRESULT WINAPI ImageListImpl_Merge(IImageList *iface, int i1,
3342 IUnknown *punk2, int i2, int dx, int dy, REFIID riid, void **ppv)
3344 HIMAGELIST This = (HIMAGELIST) iface;
3345 IImageList *iml2 = NULL;
3346 HIMAGELIST hNew;
3347 HRESULT ret = E_FAIL;
3349 TRACE("(%p)->(%d %p %d %d %d %s %p)\n", iface, i1, punk2, i2, dx, dy, debugstr_guid(riid), ppv);
3351 /* TODO: Add test for IID_ImageList2 too */
3352 if (FAILED(IImageList_QueryInterface(punk2, &IID_IImageList,
3353 (void **) &iml2)))
3354 return E_FAIL;
3356 hNew = ImageList_Merge(This, i1, (HIMAGELIST) iml2, i2, dx, dy);
3358 /* Get the interface for the new image list */
3359 if (hNew)
3361 IImageList *imerge = (IImageList*)hNew;
3363 ret = HIMAGELIST_QueryInterface(hNew, riid, ppv);
3364 IImageList_Release(imerge);
3367 IImageList_Release(iml2);
3368 return ret;
3371 static HRESULT WINAPI ImageListImpl_Clone(IImageList *iface, REFIID riid, void **ppv)
3373 HIMAGELIST This = (HIMAGELIST) iface;
3374 HIMAGELIST clone;
3375 HRESULT ret = E_FAIL;
3377 TRACE("(%p)->(%s %p)\n", iface, debugstr_guid(riid), ppv);
3379 clone = ImageList_Duplicate(This);
3381 /* Get the interface for the new image list */
3382 if (clone)
3384 IImageList *iclone = (IImageList*)clone;
3386 ret = HIMAGELIST_QueryInterface(clone, riid, ppv);
3387 IImageList_Release(iclone);
3390 return ret;
3393 static HRESULT WINAPI ImageListImpl_GetImageRect(IImageList *iface, int i,
3394 RECT *prc)
3396 HIMAGELIST This = (HIMAGELIST) iface;
3397 IMAGEINFO info;
3399 if (!prc)
3400 return E_FAIL;
3402 if (!ImageList_GetImageInfo(This, i, &info))
3403 return E_FAIL;
3405 return CopyRect(prc, &info.rcImage) ? S_OK : E_FAIL;
3408 static HRESULT WINAPI ImageListImpl_GetIconSize(IImageList *iface, int *cx,
3409 int *cy)
3411 HIMAGELIST This = (HIMAGELIST) iface;
3413 return ImageList_GetIconSize(This, cx, cy) ? S_OK : E_INVALIDARG;
3416 static HRESULT WINAPI ImageListImpl_SetIconSize(IImageList *iface, int cx,
3417 int cy)
3419 return ImageList_SetIconSize((HIMAGELIST) iface, cx, cy) ? S_OK : E_FAIL;
3422 static HRESULT WINAPI ImageListImpl_GetImageCount(IImageList *iface, int *pi)
3424 *pi = ImageList_GetImageCount((HIMAGELIST) iface);
3425 return S_OK;
3428 static HRESULT WINAPI ImageListImpl_SetImageCount(IImageList *iface,
3429 UINT uNewCount)
3431 return ImageList_SetImageCount((HIMAGELIST) iface, uNewCount) ? S_OK : E_FAIL;
3434 static HRESULT WINAPI ImageListImpl_SetBkColor(IImageList *iface, COLORREF clrBk,
3435 COLORREF *pclr)
3437 *pclr = ImageList_SetBkColor((HIMAGELIST) iface, clrBk);
3438 return S_OK;
3441 static HRESULT WINAPI ImageListImpl_GetBkColor(IImageList *iface, COLORREF *pclr)
3443 *pclr = ImageList_GetBkColor((HIMAGELIST) iface);
3444 return S_OK;
3447 static HRESULT WINAPI ImageListImpl_BeginDrag(IImageList *iface, int iTrack,
3448 int dxHotspot, int dyHotspot)
3450 return ImageList_BeginDrag((HIMAGELIST) iface, iTrack, dxHotspot, dyHotspot) ? S_OK : E_FAIL;
3453 static HRESULT WINAPI ImageListImpl_EndDrag(IImageList *iface)
3455 ImageList_EndDrag();
3456 return S_OK;
3459 static HRESULT WINAPI ImageListImpl_DragEnter(IImageList *iface, HWND hwndLock,
3460 int x, int y)
3462 return ImageList_DragEnter(hwndLock, x, y) ? S_OK : E_FAIL;
3465 static HRESULT WINAPI ImageListImpl_DragLeave(IImageList *iface, HWND hwndLock)
3467 return ImageList_DragLeave(hwndLock) ? S_OK : E_FAIL;
3470 static HRESULT WINAPI ImageListImpl_DragMove(IImageList *iface, int x, int y)
3472 return ImageList_DragMove(x, y) ? S_OK : E_FAIL;
3475 static HRESULT WINAPI ImageListImpl_SetDragCursorImage(IImageList *iface,
3476 IUnknown *punk, int iDrag, int dxHotspot, int dyHotspot)
3478 IImageList *iml2 = NULL;
3479 HRESULT ret;
3481 if (!punk)
3482 return E_FAIL;
3484 /* TODO: Add test for IID_ImageList2 too */
3485 if (FAILED(IImageList_QueryInterface(punk, &IID_IImageList,
3486 (void **) &iml2)))
3487 return E_FAIL;
3489 ret = ImageList_SetDragCursorImage((HIMAGELIST) iml2, iDrag, dxHotspot,
3490 dyHotspot);
3492 IImageList_Release(iml2);
3494 return ret ? S_OK : E_FAIL;
3497 static HRESULT WINAPI ImageListImpl_DragShowNolock(IImageList *iface, BOOL fShow)
3499 return ImageList_DragShowNolock(fShow) ? S_OK : E_FAIL;
3502 static HRESULT WINAPI ImageListImpl_GetDragImage(IImageList *iface, POINT *ppt,
3503 POINT *pptHotspot, REFIID riid, PVOID *ppv)
3505 HRESULT ret = E_FAIL;
3506 HIMAGELIST hNew;
3508 if (!ppv)
3509 return E_FAIL;
3511 hNew = ImageList_GetDragImage(ppt, pptHotspot);
3513 /* Get the interface for the new image list */
3514 if (hNew)
3516 IImageList *idrag = (IImageList*)hNew;
3518 ret = HIMAGELIST_QueryInterface(hNew, riid, ppv);
3519 IImageList_Release(idrag);
3522 return ret;
3525 static HRESULT WINAPI ImageListImpl_GetItemFlags(IImageList *iface, int i,
3526 DWORD *dwFlags)
3528 FIXME("STUB: %p %d %p\n", iface, i, dwFlags);
3529 return E_NOTIMPL;
3532 static HRESULT WINAPI ImageListImpl_GetOverlayImage(IImageList *iface, int iOverlay,
3533 int *piIndex)
3535 HIMAGELIST This = (HIMAGELIST) iface;
3536 int i;
3538 if ((iOverlay < 0) || (iOverlay > This->cCurImage))
3539 return E_FAIL;
3541 for (i = 0; i < MAX_OVERLAYIMAGE; i++)
3543 if (This->nOvlIdx[i] == iOverlay)
3545 *piIndex = i + 1;
3546 return S_OK;
3550 return E_FAIL;
3554 static const IImageListVtbl ImageListImpl_Vtbl = {
3555 ImageListImpl_QueryInterface,
3556 ImageListImpl_AddRef,
3557 ImageListImpl_Release,
3558 ImageListImpl_Add,
3559 ImageListImpl_ReplaceIcon,
3560 ImageListImpl_SetOverlayImage,
3561 ImageListImpl_Replace,
3562 ImageListImpl_AddMasked,
3563 ImageListImpl_Draw,
3564 ImageListImpl_Remove,
3565 ImageListImpl_GetIcon,
3566 ImageListImpl_GetImageInfo,
3567 ImageListImpl_Copy,
3568 ImageListImpl_Merge,
3569 ImageListImpl_Clone,
3570 ImageListImpl_GetImageRect,
3571 ImageListImpl_GetIconSize,
3572 ImageListImpl_SetIconSize,
3573 ImageListImpl_GetImageCount,
3574 ImageListImpl_SetImageCount,
3575 ImageListImpl_SetBkColor,
3576 ImageListImpl_GetBkColor,
3577 ImageListImpl_BeginDrag,
3578 ImageListImpl_EndDrag,
3579 ImageListImpl_DragEnter,
3580 ImageListImpl_DragLeave,
3581 ImageListImpl_DragMove,
3582 ImageListImpl_SetDragCursorImage,
3583 ImageListImpl_DragShowNolock,
3584 ImageListImpl_GetDragImage,
3585 ImageListImpl_GetItemFlags,
3586 ImageListImpl_GetOverlayImage
3589 static inline BOOL is_valid(HIMAGELIST himl)
3591 BOOL valid;
3592 __TRY
3594 valid = himl && himl->lpVtbl == &ImageListImpl_Vtbl;
3596 __EXCEPT_PAGE_FAULT
3598 valid = FALSE;
3600 __ENDTRY
3601 return valid;
3604 /*************************************************************************
3605 * HIMAGELIST_QueryInterface [COMCTL32.@]
3607 * Returns a pointer to an IImageList or IImageList2 object for the given
3608 * HIMAGELIST.
3610 * PARAMS
3611 * himl [I] Image list handle.
3612 * riid [I] Identifier of the requested interface.
3613 * ppv [O] Returns the address of the pointer requested, or NULL.
3615 * RETURNS
3616 * Success: S_OK.
3617 * Failure: Error value.
3619 HRESULT WINAPI
3620 HIMAGELIST_QueryInterface (HIMAGELIST himl, REFIID riid, void **ppv)
3622 TRACE("(%p,%s,%p)\n", himl, debugstr_guid(riid), ppv);
3623 return IImageList_QueryInterface((IImageList *) himl, riid, ppv);
3626 static HRESULT ImageListImpl_CreateInstance(const IUnknown *pUnkOuter, REFIID iid, void** ppv)
3628 HIMAGELIST This;
3629 HRESULT ret;
3631 TRACE("(%p,%s,%p)\n", pUnkOuter, debugstr_guid(iid), ppv);
3633 *ppv = NULL;
3635 if (pUnkOuter) return CLASS_E_NOAGGREGATION;
3637 This = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(struct _IMAGELIST));
3638 if (!This) return E_OUTOFMEMORY;
3640 This->lpVtbl = &ImageListImpl_Vtbl;
3641 This->ref = 1;
3643 ret = IUnknown_QueryInterface((IUnknown*)This, iid, ppv);
3644 IUnknown_Release((IUnknown*)This);
3646 return ret;